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 : 2991662 : check_dtor_name (tree basetype, tree name)
235 : : {
236 : : /* Just accept something we've already complained about. */
237 : 2991662 : if (name == error_mark_node)
238 : : return true;
239 : :
240 : 2991662 : if (TREE_CODE (name) == TYPE_DECL)
241 : 84 : name = TREE_TYPE (name);
242 : 2991578 : 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 : 2991636 : 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 : 274103312 : build_addr_func (tree function, tsubst_flags_t complain)
281 : : {
282 : 274103312 : tree type = TREE_TYPE (function);
283 : :
284 : : /* We have to do these by hand to avoid real pointer to member
285 : : functions. */
286 : 274103312 : if (TREE_CODE (type) == METHOD_TYPE)
287 : : {
288 : 43479744 : 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 : 43479656 : function = build_address (function);
296 : : }
297 : 230623568 : else if (TREE_CODE (function) == FUNCTION_DECL
298 : 331608402 : && DECL_IMMEDIATE_FUNCTION_P (function))
299 : 813494 : function = build_address (function);
300 : : else
301 : 229810074 : 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 : 167906 : build_call_n (tree function, int n, ...)
314 : : {
315 : 167906 : if (n == 0)
316 : 0 : return build_call_a (function, 0, NULL);
317 : : else
318 : : {
319 : 167906 : tree *argarray = XALLOCAVEC (tree, n);
320 : 167906 : va_list ap;
321 : 167906 : int i;
322 : :
323 : 167906 : va_start (ap, n);
324 : 335812 : for (i = 0; i < n; i++)
325 : 167906 : argarray[i] = va_arg (ap, tree);
326 : 167906 : va_end (ap);
327 : 167906 : 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 : 129914815 : set_flags_from_callee (tree call)
336 : : {
337 : : /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
338 : 129914815 : 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 : 129914815 : bool nothrow = decl && TREE_NOTHROW (decl);
343 : 129914815 : tree callee = cp_get_callee (call);
344 : 129914815 : if (callee)
345 : 129914807 : 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 : 129914815 : if (cfun && cp_function_chain && !cp_unevaluated_operand)
351 : : {
352 : 82710593 : if (!nothrow && at_function_scope_p ())
353 : 21887962 : cp_function_chain->can_throw = 1;
354 : :
355 : 82710593 : if (decl && TREE_THIS_VOLATILE (decl))
356 : 2734844 : current_function_returns_abnormally = 1;
357 : : }
358 : :
359 : 129914815 : TREE_NOTHROW (call) = nothrow;
360 : 129914815 : }
361 : :
362 : : tree
363 : 127956520 : build_call_a (tree function, int n, tree *argarray)
364 : : {
365 : 127956520 : tree decl;
366 : 127956520 : tree result_type;
367 : 127956520 : tree fntype;
368 : 127956520 : int i;
369 : :
370 : 127956520 : function = build_addr_func (function, tf_warning_or_error);
371 : :
372 : 127956520 : gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
373 : 127956520 : fntype = TREE_TYPE (TREE_TYPE (function));
374 : 127956520 : gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
375 : 127956520 : result_type = TREE_TYPE (fntype);
376 : : /* An rvalue has no cv-qualifiers. */
377 : 127956520 : if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
378 : 82174344 : result_type = cv_unqualified (result_type);
379 : :
380 : 127956520 : function = build_call_array_loc (input_location,
381 : : result_type, function, n, argarray);
382 : 127956520 : set_flags_from_callee (function);
383 : :
384 : 127956520 : decl = get_callee_fndecl (function);
385 : :
386 : 127956520 : 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 : 149122 : gcc_assert (DECL_ARTIFICIAL (decl)
393 : : || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
394 : : "__", 2));
395 : 149122 : mark_used (decl);
396 : : }
397 : :
398 : 127956520 : require_complete_eh_spec_types (fntype, decl);
399 : :
400 : 371385043 : 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 : 127956520 : if (!decl || !fndecl_built_in_p (decl))
406 : 259223358 : for (i = 0; i < n; i++)
407 : : {
408 : 138511295 : tree arg = CALL_EXPR_ARG (function, i);
409 : 138511295 : if (is_empty_class (TREE_TYPE (arg))
410 : 138511295 : && simple_empty_class_p (TREE_TYPE (arg), arg, INIT_EXPR))
411 : : {
412 : 5210638 : while (TREE_CODE (arg) == TARGET_EXPR)
413 : : /* We're disconnecting the initializer from its target,
414 : : don't create a temporary. */
415 : 2604538 : arg = TARGET_EXPR_INITIAL (arg);
416 : 2606100 : suppress_warning (arg, OPT_Wunused_result);
417 : 2606100 : tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
418 : 2606100 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
419 : 2606100 : CALL_EXPR_ARG (function, i) = arg;
420 : : }
421 : : }
422 : :
423 : 127956520 : 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 : 45290166 : bool rewritten () const { return (flags & LOOKUP_REWRITTEN); }
542 : 777816332 : 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 : 100015391 : null_ptr_cst_p (tree t)
550 : : {
551 : 100015391 : 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 : 100015391 : if (NULLPTR_TYPE_P (type))
558 : : return true;
559 : :
560 : 92863037 : if (cxx_dialect >= cxx11)
561 : : {
562 : 92536533 : STRIP_ANY_LOCATION_WRAPPER (t);
563 : :
564 : : /* Core issue 903 says only literal 0 is a null pointer constant. */
565 : 92536533 : if (TREE_CODE (t) == INTEGER_CST
566 : 10251460 : && !TREE_OVERFLOW (t)
567 : 10251460 : && TREE_CODE (type) == INTEGER_TYPE
568 : 9648409 : && integer_zerop (t)
569 : 98554807 : && !char_type_p (type))
570 : : return true;
571 : : }
572 : 326504 : else if (CP_INTEGRAL_TYPE_P (type))
573 : : {
574 : 62223 : t = fold_non_dependent_expr (t, tf_none);
575 : 62223 : STRIP_NOPS (t);
576 : 62223 : 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 : 79567689 : null_member_pointer_value_p (tree t)
587 : : {
588 : 79567689 : tree type = TREE_TYPE (t);
589 : 79567689 : if (!type)
590 : : return false;
591 : 79382044 : else if (TYPE_PTRMEMFUNC_P (type))
592 : 599 : return (TREE_CODE (t) == CONSTRUCTOR
593 : 313 : && CONSTRUCTOR_NELTS (t)
594 : 909 : && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
595 : 79381445 : else if (TYPE_PTRDATAMEM_P (type))
596 : 5478 : 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 : 567652395 : sufficient_parms_p (const_tree parmlist)
606 : : {
607 : 576411612 : for (; parmlist && parmlist != void_list_node;
608 : 8759217 : parmlist = TREE_CHAIN (parmlist))
609 : 183403246 : if (!TREE_PURPOSE (parmlist)
610 : 183403246 : && !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 : 5834879086 : conversion_obstack_alloc (size_t n)
620 : : {
621 : 5834879086 : void *p;
622 : 5834879086 : if (!conversion_obstack_initialized)
623 : : {
624 : 82701 : gcc_obstack_init (&conversion_obstack);
625 : 82701 : conversion_obstack_initialized = true;
626 : : }
627 : 5834879086 : p = obstack_alloc (&conversion_obstack, n);
628 : 5834879086 : memset (p, 0, n);
629 : 5834879086 : 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 : 1962867136 : conversion_obstack_sentinel (): p (conversion_obstack_alloc (0)) {}
638 : 981420014 : ~conversion_obstack_sentinel () { obstack_free (&conversion_obstack, p); }
639 : : };
640 : :
641 : : /* Allocate rejection reasons. */
642 : :
643 : : static struct rejection_reason *
644 : 554954675 : alloc_rejection (enum rejection_reason_code code)
645 : : {
646 : 554954675 : struct rejection_reason *p;
647 : 0 : p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
648 : 554954675 : p->code = code;
649 : 554954675 : return p;
650 : : }
651 : :
652 : : static struct rejection_reason *
653 : 155309249 : arity_rejection (tree first_arg, int expected, int actual, bool least_p = false)
654 : : {
655 : 121737528 : struct rejection_reason *r = alloc_rejection (rr_arity);
656 : 155309249 : int adjust = first_arg != NULL_TREE;
657 : 155309249 : r->u.arity.expected = expected - adjust;
658 : 155309249 : r->u.arity.actual = actual - adjust;
659 : 155309249 : r->u.arity.least_p = least_p;
660 : 155309249 : return r;
661 : : }
662 : :
663 : : static struct rejection_reason *
664 : 110050334 : arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
665 : : location_t loc)
666 : : {
667 : 4232096 : struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
668 : 110050334 : int adjust = first_arg != NULL_TREE;
669 : 110050334 : r->u.conversion.n_arg = n_arg - adjust;
670 : 110050334 : r->u.conversion.from = from;
671 : 110050334 : r->u.conversion.to_type = to;
672 : 110050334 : r->u.conversion.loc = loc;
673 : 110050334 : return r;
674 : : }
675 : :
676 : : static struct rejection_reason *
677 : 17340034 : bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
678 : : location_t loc)
679 : : {
680 : 1648 : struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
681 : 17340034 : int adjust = first_arg != NULL_TREE;
682 : 17340034 : r->u.bad_conversion.n_arg = n_arg - adjust;
683 : 17340034 : r->u.bad_conversion.from = from;
684 : 17340034 : r->u.bad_conversion.to_type = to;
685 : 17340034 : r->u.bad_conversion.loc = loc;
686 : 17340034 : return r;
687 : : }
688 : :
689 : : static struct rejection_reason *
690 : 2467 : explicit_conversion_rejection (tree from, tree to)
691 : : {
692 : 2467 : struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
693 : 2467 : r->u.conversion.n_arg = 0;
694 : 2467 : r->u.conversion.from = from;
695 : 2467 : r->u.conversion.to_type = to;
696 : 2467 : r->u.conversion.loc = UNKNOWN_LOCATION;
697 : 2467 : 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 : 271068071 : 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 : 271068071 : size_t args_n_bytes = sizeof (*args) * nargs;
718 : 271068071 : tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
719 : 271068071 : struct rejection_reason *r = alloc_rejection (rr_template_unification);
720 : 271068071 : r->u.template_unification.tmpl = tmpl;
721 : 271068071 : r->u.template_unification.explicit_targs = explicit_targs;
722 : 271068071 : r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
723 : : /* Copy args to our own storage. */
724 : 271068071 : memcpy (args1, args, args_n_bytes);
725 : 271068071 : r->u.template_unification.args = args1;
726 : 271068071 : r->u.template_unification.nargs = nargs;
727 : 271068071 : r->u.template_unification.return_type = return_type;
728 : 271068071 : r->u.template_unification.strict = strict;
729 : 271068071 : r->u.template_unification.flags = flags;
730 : 271068071 : 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 : 602053 : invalid_copy_with_fn_template_rejection (void)
741 : : {
742 : 0 : struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
743 : 602053 : return r;
744 : : }
745 : :
746 : : static struct rejection_reason *
747 : 434067 : inherited_ctor_rejection (void)
748 : : {
749 : 0 : struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
750 : 434067 : return r;
751 : : }
752 : :
753 : : /* Build a constraint failure record. */
754 : :
755 : : static struct rejection_reason *
756 : 148270 : constraint_failure (void)
757 : : {
758 : 0 : struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
759 : 148270 : return r;
760 : : }
761 : :
762 : : /* Dynamically allocate a conversion. */
763 : :
764 : : static conversion *
765 : 2010236998 : alloc_conversion (conversion_kind kind)
766 : : {
767 : 2010236998 : conversion *c;
768 : 0 : c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
769 : 2010236998 : c->kind = kind;
770 : 2010236998 : return c;
771 : : }
772 : :
773 : : /* Make sure that all memory on the conversion obstack has been
774 : : freed. */
775 : :
776 : : void
777 : 94585 : validate_conversion_obstack (void)
778 : : {
779 : 94585 : if (conversion_obstack_initialized)
780 : 82372 : gcc_assert ((obstack_next_free (&conversion_obstack)
781 : : == obstack_base (&conversion_obstack)));
782 : 94585 : }
783 : :
784 : : /* Dynamically allocate an array of N conversions. */
785 : :
786 : : static conversion **
787 : 760851710 : 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 : 1150628304 : has_next (conversion_kind code)
796 : : {
797 : 1070398771 : return !(code == ck_identity
798 : 1150628304 : || code == ck_ambig
799 : : || code == ck_list
800 : 1070472915 : || code == ck_aggr
801 : 1150628304 : || code == ck_deferred_bad);
802 : : }
803 : :
804 : : static conversion *
805 : 587004926 : build_conv (conversion_kind code, tree type, conversion *from)
806 : : {
807 : 587004926 : conversion *t;
808 : 587004926 : conversion_rank rank = CONVERSION_RANK (from);
809 : :
810 : : /* Only call this function for conversions that use u.next. */
811 : 587004926 : 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 : 587004926 : t = alloc_conversion (code);
816 : 587004926 : t->type = type;
817 : 587004926 : t->u.next = from;
818 : :
819 : 587004926 : switch (code)
820 : : {
821 : 157247657 : case ck_ptr:
822 : 157247657 : case ck_pmem:
823 : 157247657 : case ck_base:
824 : 157247657 : case ck_std:
825 : 157247657 : if (rank < cr_std)
826 : 587004926 : rank = cr_std;
827 : : break;
828 : :
829 : 38397524 : case ck_qual:
830 : 38397524 : case ck_fnptr:
831 : 38397524 : if (rank < cr_exact)
832 : 587004926 : rank = cr_exact;
833 : : break;
834 : :
835 : : default:
836 : : break;
837 : : }
838 : 587004926 : t->rank = rank;
839 : 587004926 : t->user_conv_p = (code == ck_user || from->user_conv_p);
840 : 587004926 : t->bad_p = from->bad_p;
841 : 587004926 : t->base_p = false;
842 : 587004926 : 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 : 66546 : build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
851 : : {
852 : 66546 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
853 : 66546 : unsigned len = CONSTRUCTOR_NELTS (ctor);
854 : 66546 : conversion **subconvs = alloc_conversions (len);
855 : 66546 : conversion *t;
856 : 66546 : unsigned i;
857 : 66546 : tree val;
858 : :
859 : : /* Within a list-initialization we can have more user-defined
860 : : conversions. */
861 : 66546 : flags &= ~LOOKUP_NO_CONVERSION;
862 : : /* But no narrowing conversions. */
863 : 66546 : flags |= LOOKUP_NO_NARROWING;
864 : :
865 : : /* Can't make an array of these types. */
866 : 66546 : if (TYPE_REF_P (elttype)
867 : 66540 : || TREE_CODE (elttype) == FUNCTION_TYPE
868 : 66540 : || VOID_TYPE_P (elttype))
869 : : return NULL;
870 : :
871 : 210444 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
872 : : {
873 : 154410 : 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 : 154359 : conversion *sub
935 : 154359 : = implicit_conversion (elttype, TREE_TYPE (val), val,
936 : : false, flags, complain);
937 : 154359 : if (sub == NULL)
938 : : return NULL;
939 : :
940 : 143853 : subconvs[i] = sub;
941 : : }
942 : :
943 : 56034 : t = alloc_conversion (ck_list);
944 : 56034 : t->type = type;
945 : 56034 : t->u.list = subconvs;
946 : 56034 : t->rank = cr_exact;
947 : :
948 : 180480 : for (i = 0; i < len; ++i)
949 : : {
950 : 124446 : conversion *sub = subconvs[i];
951 : 124446 : if (sub->rank > t->rank)
952 : 49902 : t->rank = sub->rank;
953 : 124446 : if (sub->user_conv_p)
954 : 10681 : t->user_conv_p = true;
955 : 124446 : if (sub->bad_p)
956 : 50430 : 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 : 478137743 : next_conversion (conversion *conv)
968 : : {
969 : 478137743 : if (conv == NULL
970 : 478137743 : || !has_next (conv->kind))
971 : : return NULL;
972 : 470468837 : 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 : 833764 : strip_standard_conversion (conversion *conv)
980 : : {
981 : 833764 : while (conv
982 : 837448 : && conv->kind != ck_user
983 : 864104 : && has_next (conv->kind))
984 : 3684 : conv = next_conversion (conv);
985 : 833764 : 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 : 22324 : can_convert_array (tree atype, tree from, int flags, tsubst_flags_t complain)
993 : : {
994 : 22324 : tree elttype = TREE_TYPE (atype);
995 : 22324 : unsigned i;
996 : :
997 : 22324 : if (TREE_CODE (from) == CONSTRUCTOR)
998 : : {
999 : 26579 : for (i = 0; i < CONSTRUCTOR_NELTS (from); ++i)
1000 : : {
1001 : 4300 : tree val = CONSTRUCTOR_ELT (from, i)->value;
1002 : 4300 : bool ok;
1003 : 4300 : if (TREE_CODE (elttype) == ARRAY_TYPE)
1004 : 18 : ok = can_convert_array (elttype, val, flags, complain);
1005 : : else
1006 : 4282 : ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
1007 : : complain);
1008 : 4300 : 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 : 685185 : field_in_pset (hash_set<tree, true> &pset, tree field)
1028 : : {
1029 : 685185 : 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 : 1150821 : build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1049 : : {
1050 : 1150821 : unsigned HOST_WIDE_INT i = 0;
1051 : 1150821 : conversion *c;
1052 : 1150821 : tree field = next_aggregate_field (TYPE_FIELDS (type));
1053 : 1150821 : tree empty_ctor = NULL_TREE;
1054 : 1150821 : 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 : 1150821 : 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 : 1150821 : tree idx, val;
1068 : 1835894 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val)
1069 : : {
1070 : 685114 : if (!idx)
1071 : : break;
1072 : :
1073 : 685107 : gcc_checking_assert (TREE_CODE (idx) == FIELD_DECL);
1074 : :
1075 : 685107 : tree ftype = TREE_TYPE (idx);
1076 : 685107 : bool ok;
1077 : :
1078 : 685107 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1079 : 1680 : ok = can_convert_array (ftype, val, flags, complain);
1080 : : else
1081 : 683427 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1082 : : complain);
1083 : :
1084 : 685107 : if (!ok)
1085 : : return NULL;
1086 : :
1087 : : /* For unions, there should be just one initializer. */
1088 : 685096 : if (TREE_CODE (type) == UNION_TYPE)
1089 : : {
1090 : : field = NULL_TREE;
1091 : : i = 1;
1092 : : break;
1093 : : }
1094 : 685073 : pset.add (idx);
1095 : : }
1096 : :
1097 : 1921377 : for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
1098 : : {
1099 : 770819 : tree ftype = TREE_TYPE (field);
1100 : 770819 : bool ok;
1101 : :
1102 : 770819 : if (!pset.is_empty () && field_in_pset (pset, field))
1103 : 685073 : continue;
1104 : 85746 : 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 : 85737 : else if (DECL_INITIAL (field))
1112 : 347 : val = get_nsdmi (field, /*ctor*/false, complain);
1113 : 85390 : else if (TYPE_REF_P (ftype))
1114 : : /* Value-initialization of reference is ill-formed. */
1115 : : return NULL;
1116 : : else
1117 : : {
1118 : 85384 : if (empty_ctor == NULL_TREE)
1119 : 39004 : empty_ctor = build_constructor (init_list_type_node, NULL);
1120 : : val = empty_ctor;
1121 : : }
1122 : :
1123 : 85740 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1124 : 20626 : ok = can_convert_array (ftype, val, flags, complain);
1125 : : else
1126 : 65114 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1127 : : complain);
1128 : :
1129 : 85740 : if (!ok)
1130 : : return NULL;
1131 : :
1132 : 85732 : if (TREE_CODE (type) == UNION_TYPE)
1133 : : break;
1134 : : }
1135 : :
1136 : 1150796 : if (i < CONSTRUCTOR_NELTS (ctor))
1137 : : return NULL;
1138 : :
1139 : 1150795 : c = alloc_conversion (ck_aggr);
1140 : 1150795 : c->type = type;
1141 : 1150795 : c->rank = cr_exact;
1142 : 1150795 : c->user_conv_p = true;
1143 : 1150795 : c->check_narrowing = true;
1144 : 1150795 : c->u.expr = ctor;
1145 : 1150795 : return c;
1146 : 1150821 : }
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 : 1056 : build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1153 : : {
1154 : 1056 : conversion *c;
1155 : 1056 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1156 : 1056 : tree elttype = TREE_TYPE (type);
1157 : 1056 : bool bad = false;
1158 : 1056 : bool user = false;
1159 : 1056 : enum conversion_rank rank = cr_exact;
1160 : :
1161 : : /* We might need to propagate the size from the element to the array. */
1162 : 1056 : complete_type (type);
1163 : :
1164 : 1056 : if (TYPE_DOMAIN (type)
1165 : 1056 : && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
1166 : : {
1167 : 986 : unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
1168 : 986 : if (alen < len)
1169 : : return NULL;
1170 : : }
1171 : :
1172 : 1041 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1173 : :
1174 : 5202 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1175 : : {
1176 : 2221 : conversion *sub
1177 : 2221 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1178 : : false, flags, complain);
1179 : 2221 : if (sub == NULL)
1180 : : return NULL;
1181 : :
1182 : 2175 : if (sub->rank > rank)
1183 : 202 : rank = sub->rank;
1184 : 2175 : if (sub->user_conv_p)
1185 : 23 : user = true;
1186 : 2175 : if (sub->bad_p)
1187 : 133 : bad = true;
1188 : : }
1189 : :
1190 : 995 : c = alloc_conversion (ck_aggr);
1191 : 995 : c->type = type;
1192 : 995 : c->rank = rank;
1193 : 995 : c->user_conv_p = user;
1194 : 995 : c->bad_p = bad;
1195 : 995 : c->u.expr = ctor;
1196 : 995 : 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 : 58804 : build_complex_conv (tree type, tree ctor, int flags,
1204 : : tsubst_flags_t complain)
1205 : : {
1206 : 58804 : conversion *c;
1207 : 58804 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1208 : 58804 : tree elttype = TREE_TYPE (type);
1209 : 58804 : bool bad = false;
1210 : 58804 : bool user = false;
1211 : 58804 : enum conversion_rank rank = cr_exact;
1212 : :
1213 : 58804 : if (len != 2)
1214 : : return NULL;
1215 : :
1216 : 58764 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1217 : :
1218 : 293820 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1219 : : {
1220 : 117528 : conversion *sub
1221 : 117528 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1222 : : false, flags, complain);
1223 : 117528 : if (sub == NULL)
1224 : : return NULL;
1225 : :
1226 : 117528 : if (sub->rank > rank)
1227 : 44 : rank = sub->rank;
1228 : 117528 : if (sub->user_conv_p)
1229 : 0 : user = true;
1230 : 117528 : if (sub->bad_p)
1231 : 0 : bad = true;
1232 : : }
1233 : :
1234 : 58764 : c = alloc_conversion (ck_aggr);
1235 : 58764 : c->type = type;
1236 : 58764 : c->rank = rank;
1237 : 58764 : c->user_conv_p = user;
1238 : 58764 : c->bad_p = bad;
1239 : 58764 : c->u.expr = ctor;
1240 : 58764 : 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 : 1417138908 : build_identity_conv (tree type, tree expr)
1248 : : {
1249 : 1417138908 : conversion *c;
1250 : :
1251 : 0 : c = alloc_conversion (ck_identity);
1252 : 1417138908 : c->type = type;
1253 : 1417138908 : c->u.expr = expr;
1254 : :
1255 : 1417138908 : 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 : 2573156397 : strip_top_quals (tree t)
1276 : : {
1277 : 2573156397 : if (TREE_CODE (t) == ARRAY_TYPE)
1278 : : return t;
1279 : 2567608744 : 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 : 1146748326 : standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1289 : : int flags, tsubst_flags_t complain)
1290 : : {
1291 : 1146748326 : enum tree_code fcode, tcode;
1292 : 1146748326 : conversion *conv;
1293 : 1146748326 : bool fromref = false;
1294 : 1146748326 : tree qualified_to;
1295 : :
1296 : 1146748326 : to = non_reference (to);
1297 : 1146748326 : if (TYPE_REF_P (from))
1298 : : {
1299 : 72550 : fromref = true;
1300 : 72550 : from = TREE_TYPE (from);
1301 : : }
1302 : 1146748326 : qualified_to = to;
1303 : 1146748326 : to = strip_top_quals (to);
1304 : 1146748326 : from = strip_top_quals (from);
1305 : :
1306 : 1146748326 : if (expr && type_unknown_p (expr))
1307 : : {
1308 : 208495 : if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1309 : : {
1310 : 30203 : tsubst_flags_t tflags = tf_conv;
1311 : 30203 : expr = instantiate_type (to, expr, tflags);
1312 : 30203 : if (expr == error_mark_node)
1313 : : return NULL;
1314 : 20552 : from = TREE_TYPE (expr);
1315 : : }
1316 : 178292 : else if (TREE_CODE (to) == BOOLEAN_TYPE)
1317 : : {
1318 : : /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1319 : 4625 : expr = resolve_nondeduced_context (expr, complain);
1320 : 4625 : from = TREE_TYPE (expr);
1321 : : }
1322 : : }
1323 : :
1324 : 1146738675 : fcode = TREE_CODE (from);
1325 : 1146738675 : tcode = TREE_CODE (to);
1326 : :
1327 : 1146738675 : conv = build_identity_conv (from, expr);
1328 : 1146738675 : if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1329 : : {
1330 : 5576457 : from = type_decays_to (from);
1331 : 5576457 : fcode = TREE_CODE (from);
1332 : : /* Tell convert_like that we're using the address. */
1333 : 5576457 : conv->rvaluedness_matches_p = true;
1334 : 5576457 : 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 : 1141162218 : else if (fromref || (expr && obvalue_p (expr)))
1340 : : {
1341 : 280729197 : if (expr)
1342 : : {
1343 : 280656670 : tree bitfield_type;
1344 : 280656670 : bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1345 : 280656670 : if (bitfield_type)
1346 : : {
1347 : 4832183 : from = strip_top_quals (bitfield_type);
1348 : 4832183 : fcode = TREE_CODE (from);
1349 : : }
1350 : : }
1351 : 280729197 : conv = build_conv (ck_rvalue, from, conv);
1352 : : /* If we're performing copy-initialization, remember to skip
1353 : : explicit constructors. */
1354 : 280729197 : if (flags & LOOKUP_ONLYCONVERTING)
1355 : 244073621 : conv->copy_init_p = true;
1356 : : }
1357 : :
1358 : : /* Allow conversion between `__complex__' data types. */
1359 : 1146738675 : 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 : 2595342 : conversion *part_conv = standard_conversion
1365 : 2595342 : (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1366 : : complain);
1367 : :
1368 : 2595342 : if (!part_conv)
1369 : : conv = NULL;
1370 : 2595342 : else if (part_conv->kind == ck_identity)
1371 : : /* Leave conv alone. */;
1372 : : else
1373 : : {
1374 : 367744 : conv = build_conv (part_conv->kind, to, conv);
1375 : 367744 : conv->rank = part_conv->rank;
1376 : : }
1377 : :
1378 : 2595342 : return conv;
1379 : : }
1380 : :
1381 : 1144143333 : if (same_type_p (from, to))
1382 : : {
1383 : 744722048 : if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1384 : 13878337 : conv->type = qualified_to;
1385 : 730843711 : else if (from != to)
1386 : : /* Use TO in order to not lose TO in diagnostics. */
1387 : 61048534 : conv->type = to;
1388 : 744722048 : 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 : 254549488 : if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1396 : 254458329 : || NULLPTR_TYPE_P (to))
1397 : 403170167 : && ((expr && null_ptr_cst_p (expr))
1398 : 144679123 : || NULLPTR_TYPE_P (from)))
1399 : 3941560 : conv = build_conv (ck_std, to, conv);
1400 : 395479725 : else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1401 : 394970125 : || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1402 : : {
1403 : : /* For backwards brain damage compatibility, allow interconversion of
1404 : : pointers and integers with a pedwarn. */
1405 : 1791871 : conv = build_conv (ck_std, to, conv);
1406 : 1791871 : conv->bad_p = true;
1407 : : }
1408 : 393687854 : 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 : 393361 : conv = build_conv (ck_std, to, conv);
1413 : 393361 : conv->bad_p = true;
1414 : : }
1415 : 393294493 : else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1416 : 257376168 : || (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 : 135918325 : to_pointee = TREE_TYPE (to);
1424 : 135918325 : 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 : 135918325 : if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1430 : 135918325 : && TYPE_QUALS (to_pointee))
1431 : 9 : to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1432 : 135918325 : if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1433 : 135918325 : && 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 : 135918960 : if (tcode == POINTER_TYPE
1443 : 135918960 : && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1444 : : to_pointee))
1445 : : ;
1446 : 94080214 : else if (VOID_TYPE_P (to_pointee)
1447 : 3580030 : && !TYPE_PTRDATAMEM_P (from)
1448 : 3580030 : && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1449 : : {
1450 : 3579747 : tree nfrom = TREE_TYPE (from);
1451 : : /* Don't try to apply restrict to void. */
1452 : 3579747 : int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1453 : 3579747 : from_pointee = cp_build_qualified_type (void_type_node, quals);
1454 : 3579747 : from = build_pointer_type (from_pointee);
1455 : 3579747 : conv = build_conv (ck_ptr, from, conv);
1456 : 3579747 : }
1457 : 90500467 : 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 : 39952553 : else if (CLASS_TYPE_P (from_pointee)
1475 : 39950476 : && 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 : 130007313 : && DERIVED_FROM_P (to_pointee, from_pointee))
1488 : : {
1489 : 5298954 : from_pointee
1490 : 5298954 : = cp_build_qualified_type (to_pointee,
1491 : : cp_type_quals (from_pointee));
1492 : 5298954 : from = build_pointer_type (from_pointee);
1493 : 5298954 : conv = build_conv (ck_ptr, from, conv);
1494 : 5298954 : conv->base_p = true;
1495 : : }
1496 : :
1497 : 135918784 : if (same_type_p (from, to))
1498 : : /* OK */;
1499 : 129061066 : 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 : 129060444 : else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1505 : 38141415 : conv = build_conv (ck_qual, to, conv);
1506 : 90919029 : 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 : 90918825 : else if (fnptr_conv_p (to, from))
1510 : 226608 : conv = build_conv (ck_fnptr, to, conv);
1511 : : /* Allow conversions among compatible ObjC pointer types (base
1512 : : conversions have been already handled above). */
1513 : 90692217 : else if (c_dialect_objc ()
1514 : 90692217 : && objc_compare_types (to, from, -4, NULL_TREE))
1515 : 0 : conv = build_conv (ck_ptr, to, conv);
1516 : 90692217 : else if (ptr_reasonably_similar (to_pointee, from_pointee))
1517 : : {
1518 : 6788208 : conv = build_conv (ck_ptr, to, conv);
1519 : 6788208 : conv->bad_p = true;
1520 : : }
1521 : : else
1522 : : return NULL;
1523 : :
1524 : 182018127 : from = to;
1525 : : }
1526 : 257375533 : else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1527 : : {
1528 : 89067 : tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1529 : 89067 : tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1530 : 89067 : tree fbase = class_of_this_parm (fromfn);
1531 : 89067 : 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 : 89067 : if (!same_type_p (fbase, tbase) && !DERIVED_FROM_P (fbase, tbase))
1536 : : return NULL;
1537 : :
1538 : 88906 : tree fstat = static_fn_type (fromfn);
1539 : 88906 : tree tstat = static_fn_type (tofn);
1540 : 88906 : if (same_type_p (tstat, fstat)
1541 : 88906 : || fnptr_conv_p (tstat, fstat))
1542 : : /* OK */;
1543 : : else
1544 : : return NULL;
1545 : :
1546 : 88638 : if (!same_type_p (fbase, tbase))
1547 : : {
1548 : 88571 : from = build_memfn_type (fstat,
1549 : : tbase,
1550 : : cp_type_quals (tbase),
1551 : : type_memfn_rqual (tofn));
1552 : 88571 : from = build_ptrmemfunc_type (build_pointer_type (from));
1553 : 88571 : conv = build_conv (ck_pmem, from, conv);
1554 : 88571 : conv->base_p = true;
1555 : : }
1556 : 88638 : if (fnptr_conv_p (tstat, fstat))
1557 : 67 : conv = build_conv (ck_fnptr, to, conv);
1558 : : }
1559 : 257286466 : 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 : 5853775 : if (ARITHMETIC_TYPE_P (from)
1568 : 5762611 : || UNSCOPED_ENUM_P (from)
1569 : 3505907 : || fcode == POINTER_TYPE
1570 : 2515119 : || TYPE_PTRMEM_P (from)
1571 : 13310157 : || NULLPTR_TYPE_P (from))
1572 : : {
1573 : 8280432 : conv = build_conv (ck_std, to, conv);
1574 : 8280432 : if (fcode == POINTER_TYPE
1575 : 7289644 : || TYPE_PTRDATAMEM_P (from)
1576 : 7289502 : || (TYPE_PTRMEMFUNC_P (from)
1577 : 77 : && conv->rank < cr_pbool)
1578 : 15569857 : || NULLPTR_TYPE_P (from))
1579 : 991082 : conv->rank = cr_pbool;
1580 : 8280432 : if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1581 : 36 : conv->bad_p = true;
1582 : 8280432 : if (flags & LOOKUP_NO_NARROWING)
1583 : 32359 : conv->check_narrowing = true;
1584 : 8280432 : 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 : 246491209 : else if (ARITHMETIC_TYPE_P (to))
1593 : : {
1594 : 31129913 : if (! (INTEGRAL_CODE_P (fcode)
1595 : 27505492 : || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1596 : 155578735 : || 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 : 123334784 : if (!COMPLETE_TYPE_P (from))
1603 : : return NULL;
1604 : :
1605 : 123334778 : conv = build_conv (ck_std, to, conv);
1606 : :
1607 : 123334778 : tree underlying_type = NULL_TREE;
1608 : 123334778 : if (TREE_CODE (from) == ENUMERAL_TYPE
1609 : 123334778 : && ENUM_FIXED_UNDERLYING_TYPE_P (from))
1610 : 2681538 : 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 : 123334778 : if ((same_type_p (to, type_promotes_to (from))
1618 : 111181787 : || (underlying_type && same_type_p (to, underlying_type)))
1619 : 123334797 : && next_conversion (conv)->rank <= cr_promotion)
1620 : 12153010 : 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 : 123334778 : if (fcode == REAL_TYPE
1632 : 123334778 : && tcode == REAL_TYPE
1633 : 19159973 : && (extended_float_type_p (from)
1634 : 18163898 : || extended_float_type_p (to))
1635 : 129009058 : && cp_compare_floating_point_conversion_ranks (from, to) >= 2)
1636 : 2676068 : conv->bad_p = true;
1637 : : }
1638 : 118417966 : else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1639 : 118417966 : && vector_types_convertible_p (from, to, false))
1640 : 4244 : return build_conv (ck_std, to, conv);
1641 : 118413722 : else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1642 : 40798943 : && is_properly_derived_from (from, to))
1643 : : {
1644 : 453144 : if (conv->kind == ck_rvalue)
1645 : 453093 : conv = next_conversion (conv);
1646 : 453144 : 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 : 453144 : conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1652 : : /* If we're performing copy-initialization, remember to skip
1653 : : explicit constructors. */
1654 : 453144 : if (flags & LOOKUP_ONLYCONVERTING)
1655 : 453077 : conv->copy_init_p = true;
1656 : : }
1657 : : else
1658 : 117960578 : return NULL;
1659 : :
1660 : 182018127 : if (flags & LOOKUP_NO_NARROWING)
1661 : 27876534 : 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 : 186722036 : reference_related_p (tree t1, tree t2)
1672 : : {
1673 : 186722036 : if (t1 == error_mark_node || t2 == error_mark_node)
1674 : : return false;
1675 : :
1676 : 186722032 : t1 = TYPE_MAIN_VARIANT (t1);
1677 : 186722032 : 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 : 186722032 : return (similar_type_p (t1, t2)
1684 : 186722032 : || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1685 : 39966940 : && DERIVED_FROM_P (t1, t2)));
1686 : : }
1687 : :
1688 : : /* Returns nonzero if T1 is reference-compatible with T2. */
1689 : :
1690 : : bool
1691 : 165124063 : 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 : 165124063 : tree ptype1 = build_pointer_type (t1);
1699 : 165124063 : tree ptype2 = build_pointer_type (t2);
1700 : 165124063 : conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE,
1701 : : /*c_cast_p=*/false, 0, tf_none);
1702 : 165124063 : if (!conv || conv->bad_p)
1703 : 88833518 : 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 : 84552171 : 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 : 84552171 : if (!((TYPE_PTR_P (to) && TYPE_PTR_P (from))
1716 : 2707 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from))))
1717 : : return false;
1718 : :
1719 : 3176352 : conversion *conv = standard_conversion (to, from, NULL_TREE,
1720 : : /*c_cast_p=*/false, 0, tf_none);
1721 : 6324114 : for (conversion *t = conv; t; t = next_conversion (t))
1722 : 3176370 : 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 : 87476834 : direct_reference_binding (tree type, conversion *conv)
1784 : : {
1785 : 87476834 : tree t;
1786 : :
1787 : 87476834 : gcc_assert (TYPE_REF_P (type));
1788 : 87476834 : gcc_assert (!TYPE_REF_P (conv->type));
1789 : :
1790 : 87476834 : t = TREE_TYPE (type);
1791 : :
1792 : 87476834 : if (conv->kind == ck_identity)
1793 : : /* Mark the identity conv as to not decay to rvalue. */
1794 : 87476834 : 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 : 87476834 : if (is_properly_derived_from (conv->type, t))
1813 : : {
1814 : : /* Represent the derived-to-base conversion. */
1815 : 2924663 : 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 : 2924663 : conv->need_temporary_p = false;
1820 : : }
1821 : 84552171 : 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 : 28608 : conv = build_conv (ck_qual, strip_top_quals (t), conv);
1845 : :
1846 : 87476834 : 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 : 164659676 : reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1858 : : tsubst_flags_t complain)
1859 : : {
1860 : 164659676 : conversion *conv = NULL;
1861 : 164659676 : conversion *bad_direct_conv = nullptr;
1862 : 164659676 : tree to = TREE_TYPE (rto);
1863 : 164659676 : tree from = rfrom;
1864 : 164659676 : tree tfrom;
1865 : 164659676 : bool related_p;
1866 : 164659676 : bool compatible_p;
1867 : 164659676 : cp_lvalue_kind gl_kind;
1868 : 164659676 : bool is_lvalue;
1869 : :
1870 : 164659676 : 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 : 164659676 : bool copy_list_init = false;
1879 : 164659676 : bool single_list_conv = false;
1880 : 164659676 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1881 : : {
1882 : 193049 : 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 : 193049 : if (CONSTRUCTOR_NELTS (expr) == 1
1894 : 51843 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
1895 : : {
1896 : 1740 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1897 : 1740 : if (error_operand_p (elt))
1898 : : return NULL;
1899 : 1735 : tree etype = TREE_TYPE (elt);
1900 : 1735 : if (reference_related_p (to, etype))
1901 : : {
1902 : 373 : expr = elt;
1903 : 373 : from = etype;
1904 : 373 : goto skip;
1905 : : }
1906 : 1362 : 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 : 164659671 : skip:;
1917 : : }
1918 : :
1919 : 164659671 : if (TYPE_REF_P (from))
1920 : : {
1921 : 53967 : from = TREE_TYPE (from);
1922 : 53967 : if (!TYPE_REF_IS_RVALUE (rfrom)
1923 : 53967 : || TREE_CODE (from) == FUNCTION_TYPE)
1924 : : gl_kind = clk_ordinary;
1925 : : else
1926 : : gl_kind = clk_rvalueref;
1927 : : }
1928 : 164605704 : else if (expr)
1929 : 162446285 : gl_kind = lvalue_kind (expr);
1930 : 2033370 : else if (CLASS_TYPE_P (from)
1931 : 2159419 : || 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 : 164659671 : if ((flags & LOOKUP_NO_TEMP_BIND)
1938 : 2147117 : && (gl_kind & clk_class))
1939 : : gl_kind = clk_none;
1940 : :
1941 : : /* Same mask as real_lvalue_p. */
1942 : 162917882 : is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1943 : :
1944 : 141469878 : tfrom = from;
1945 : 141469878 : if ((gl_kind & clk_bitfield) != 0)
1946 : 3096144 : 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 : 164659671 : 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 : 164659671 : if (related_p && c_cast_p
1955 : 164659671 : && !at_least_as_qualified_p (to, tfrom))
1956 : 194 : to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1957 : 164659671 : 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 : 164659671 : 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 : 77164766 : conv = build_identity_conv (tfrom, expr);
1983 : 77164766 : conv = direct_reference_binding (rto, conv);
1984 : :
1985 : 77164766 : if (TYPE_REF_P (rfrom))
1986 : : /* Handle rvalue reference to function properly. */
1987 : 14040 : conv->rvaluedness_matches_p
1988 : 14040 : = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1989 : : else
1990 : 77150726 : conv->rvaluedness_matches_p
1991 : 77150726 : = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1992 : :
1993 : 77164766 : if ((gl_kind & clk_bitfield) != 0
1994 : 77164766 : || ((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 : 57741792 : if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
2011 : 84657437 : && TREE_CODE (to) != FUNCTION_TYPE)
2012 : 7490346 : conv->bad_p = true;
2013 : :
2014 : : /* Nor the reverse. */
2015 : 19422974 : 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 : 11223575 : && !(cxx_dialect <= cxx20
2020 : 6802092 : && (gl_kind & clk_implicit_rval))
2021 : 10617063 : && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
2022 : 10552171 : || (flags & LOOKUP_NO_RVAL_BIND))
2023 : 77229658 : && TREE_CODE (to) != FUNCTION_TYPE)
2024 : 64892 : conv->bad_p = true;
2025 : :
2026 : 77164766 : if (!compatible_p)
2027 : 4839087 : conv->bad_p = true;
2028 : :
2029 : 77164766 : 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 : 3502829 : else if (!related_p
2036 : 83992076 : && !(flags & LOOKUP_NO_CONVERSION)
2037 : 32408850 : && (CLASS_TYPE_P (from) || single_list_conv))
2038 : : {
2039 : 9940634 : tree rexpr = expr;
2040 : 9940634 : 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 : 9940634 : z_candidate *cand = build_user_type_conversion_1 (rto, rexpr, flags,
2057 : : complain);
2058 : 9940634 : if (cand)
2059 : : {
2060 : 11903 : 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 : 87483312 : 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 : 85446310 : 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 : 111073048 : 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 : 85446310 : 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 : 85446310 : if (!maybe_valid_p && (flags & LOOKUP_SHORTCUT_BAD_CONVS))
2115 : : {
2116 : 4825475 : if (bad_direct_conv)
2117 : : return bad_direct_conv;
2118 : :
2119 : 4825208 : conv = alloc_conversion (ck_deferred_bad);
2120 : 4825208 : conv->bad_p = true;
2121 : 4825208 : 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 : 80620835 : 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 : 80620835 : if (!(flags & LOOKUP_COPY_PARM))
2136 : 70207971 : flags |= LOOKUP_ONLYCONVERTING;
2137 : :
2138 : 80620835 : if (!conv)
2139 : 80620835 : conv = implicit_conversion (to, from, expr, c_cast_p,
2140 : : flags, complain);
2141 : 80620835 : if (!conv)
2142 : : return bad_direct_conv ? bad_direct_conv : nullptr;
2143 : :
2144 : 8021861 : if (conv->user_conv_p)
2145 : : {
2146 : 4843271 : if (copy_list_init)
2147 : : /* Remember this was copy-list-initialization. */
2148 : 118539 : conv->need_temporary_p = true;
2149 : :
2150 : : /* If initializing the temporary used a conversion function,
2151 : : recalculate the second conversion sequence. */
2152 : 13834608 : for (conversion *t = conv; t; t = next_conversion (t))
2153 : 9313824 : if (t->kind == ck_user
2154 : 4791859 : && 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 : 9313812 : else if (t->kind == ck_user
2164 : 9313812 : && DECL_CONV_FN_P (t->cand->fn))
2165 : : {
2166 : 322475 : tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
2167 : : /* A prvalue of non-class type is cv-unqualified. */
2168 : 322475 : if (!TYPE_REF_P (ftype) && !CLASS_TYPE_P (ftype))
2169 : 352 : ftype = cv_unqualified (ftype);
2170 : 322475 : int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
2171 : 322475 : conversion *new_second
2172 : 322475 : = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
2173 : : sflags, complain);
2174 : 322475 : if (!new_second)
2175 : : return bad_direct_conv ? bad_direct_conv : nullptr;
2176 : 322475 : conv = merge_conversion_sequences (t, new_second);
2177 : 322475 : gcc_assert (maybe_valid_p || conv->bad_p);
2178 : : return conv;
2179 : : }
2180 : : }
2181 : :
2182 : 7699386 : conv = build_conv (ck_ref_bind, rto, conv);
2183 : : /* This reference binding, unlike those above, requires the
2184 : : creation of a temporary. */
2185 : 7699386 : conv->need_temporary_p = true;
2186 : 7699386 : conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
2187 : 7699386 : conv->bad_p |= !maybe_valid_p;
2188 : :
2189 : 7699386 : 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 : 1134783644 : implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
2199 : : int flags, tsubst_flags_t complain)
2200 : : {
2201 : 1134783644 : conversion *conv;
2202 : :
2203 : 1134783644 : if (from == error_mark_node || to == error_mark_node
2204 : 1134782594 : || 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 : 1134782594 : 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 : 1134782594 : complain &= ~tf_error;
2219 : :
2220 : : /* Call reshape_init early to remove redundant braces. */
2221 : 1134782594 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to))
2222 : : {
2223 : 1890033 : to = complete_type (to);
2224 : 1890033 : if (!COMPLETE_TYPE_P (to))
2225 : : return nullptr;
2226 : 1890009 : if (!CLASSTYPE_NON_AGGREGATE (to))
2227 : : {
2228 : 1151065 : expr = reshape_init (to, expr, complain);
2229 : 1151065 : if (expr == error_mark_node)
2230 : : return nullptr;
2231 : 1150872 : from = TREE_TYPE (expr);
2232 : : }
2233 : : }
2234 : :
2235 : : /* An argument should have gone through convert_from_reference. */
2236 : 1134782377 : gcc_checking_assert (!expr || !TYPE_REF_P (from));
2237 : :
2238 : 1134782377 : if (TYPE_REF_P (to))
2239 : 158929853 : conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
2240 : : else
2241 : 975852524 : conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
2242 : :
2243 : 1134782377 : if (conv)
2244 : : return conv;
2245 : :
2246 : 200055081 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
2247 : : {
2248 : 3818174 : if (is_std_init_list (to) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2249 : 66546 : return build_list_conv (to, expr, flags, complain);
2250 : :
2251 : : /* As an extension, allow list-initialization of _Complex. */
2252 : 3685073 : if (TREE_CODE (to) == COMPLEX_TYPE
2253 : 3743886 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2254 : : {
2255 : 58804 : conv = build_complex_conv (to, expr, flags, complain);
2256 : 58804 : if (conv)
2257 : : return conv;
2258 : : }
2259 : :
2260 : : /* Allow conversion from an initializer-list with one element to a
2261 : : scalar type. */
2262 : 3626309 : if (SCALAR_TYPE_P (to))
2263 : : {
2264 : 1790357 : int nelts = CONSTRUCTOR_NELTS (expr);
2265 : 297536 : tree elt;
2266 : :
2267 : 297536 : if (nelts == 0)
2268 : 1492821 : elt = build_value_init (to, tf_none);
2269 : 297536 : else if (nelts == 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2270 : 296976 : elt = CONSTRUCTOR_ELT (expr, 0)->value;
2271 : : else
2272 : 560 : elt = error_mark_node;
2273 : :
2274 : 1790357 : conv = implicit_conversion (to, TREE_TYPE (elt), elt,
2275 : : c_cast_p, flags, complain);
2276 : 1790357 : if (conv)
2277 : : {
2278 : 1789310 : conv->check_narrowing = true;
2279 : 1789310 : if (BRACE_ENCLOSED_INITIALIZER_P (elt))
2280 : : /* Too many levels of braces, i.e. '{{1}}'. */
2281 : 14 : conv->bad_p = true;
2282 : 1789310 : return conv;
2283 : : }
2284 : : }
2285 : 1835952 : else if (TREE_CODE (to) == ARRAY_TYPE)
2286 : 1056 : return build_array_conv (to, expr, flags, complain);
2287 : : }
2288 : :
2289 : 198139405 : if (expr != NULL_TREE
2290 : 193906799 : && (MAYBE_CLASS_TYPE_P (from)
2291 : 114395435 : || MAYBE_CLASS_TYPE_P (to))
2292 : 331553975 : && (flags & LOOKUP_NO_CONVERSION) == 0)
2293 : : {
2294 : 51520540 : struct z_candidate *cand;
2295 : :
2296 : 36674178 : if (CLASS_TYPE_P (to)
2297 : 36674128 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
2298 : 53343432 : && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
2299 : 1150821 : return build_aggr_conv (to, expr, flags, complain);
2300 : :
2301 : 50369719 : cand = build_user_type_conversion_1 (to, expr, flags, complain);
2302 : 50369719 : if (cand)
2303 : : {
2304 : 662982 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
2305 : 660301 : && CONSTRUCTOR_NELTS (expr) == 1
2306 : 24581 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
2307 : 9461054 : && !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 : 24225 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
2315 : 24225 : tree elttype = TREE_TYPE (elt);
2316 : 24225 : if (reference_related_p (to, elttype))
2317 : 67 : return implicit_conversion (to, elttype, elt,
2318 : 67 : c_cast_p, flags, complain);
2319 : : }
2320 : 9436406 : 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 : 50369652 : 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 : 5687171 : good_conversion (tree to, tree from, tree expr,
2340 : : int flags, tsubst_flags_t complain)
2341 : : {
2342 : 5687171 : conversion *c = implicit_conversion (to, from, expr, /*cast*/false,
2343 : : flags, complain);
2344 : 5687171 : if (c && c->bad_p)
2345 : 2304618 : c = NULL;
2346 : 5687171 : 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 : 794941074 : 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 : 794941074 : struct z_candidate *cand = (struct z_candidate *)
2362 : 794941074 : conversion_obstack_alloc (sizeof (struct z_candidate));
2363 : :
2364 : 794941074 : cand->fn = fn;
2365 : 794941074 : cand->first_arg = first_arg;
2366 : 794941074 : cand->args = args;
2367 : 794941074 : cand->convs = convs;
2368 : 794941074 : cand->num_convs = num_convs;
2369 : 794941074 : cand->access_path = access_path;
2370 : 794941074 : cand->conversion_path = conversion_path;
2371 : 794941074 : cand->viable = viable;
2372 : 794941074 : cand->reason = reason;
2373 : 794941074 : cand->next = *candidates;
2374 : 794941074 : cand->flags = flags;
2375 : 794941074 : *candidates = cand;
2376 : :
2377 : 794941074 : if (convs && cand->reversed ())
2378 : : /* Swap the conversions for comparison in joust; we'll swap them back
2379 : : before build_over_call. */
2380 : 38277245 : std::swap (convs[0], convs[1]);
2381 : :
2382 : 794941074 : 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 : 461392789 : add_ignored_candidate (z_candidate **candidates, tree fn)
2391 : : {
2392 : : /* No need to dynamically allocate these. */
2393 : 461392789 : static const rejection_reason reason_ignored = { rr_ignored, {} };
2394 : :
2395 : 461392789 : struct z_candidate *cand = (struct z_candidate *)
2396 : 461388921 : conversion_obstack_alloc (sizeof (struct z_candidate));
2397 : :
2398 : 461392789 : cand->fn = fn;
2399 : 461392789 : cand->reason = const_cast<rejection_reason *> (&reason_ignored);
2400 : 461392789 : cand->next = *candidates;
2401 : 461392789 : *candidates = cand;
2402 : :
2403 : 461392789 : return cand;
2404 : : }
2405 : :
2406 : : /* True iff CAND is a candidate added by add_ignored_candidate. */
2407 : :
2408 : : static bool
2409 : 423539491 : ignored_candidate_p (const z_candidate *cand)
2410 : : {
2411 : 423533480 : 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 : 121740854 : remaining_arguments (tree arg)
2419 : : {
2420 : 121740854 : int n;
2421 : :
2422 : 214136495 : for (n = 0; arg != NULL_TREE && arg != void_list_node;
2423 : 92395641 : arg = TREE_CHAIN (arg))
2424 : 92395641 : n++;
2425 : :
2426 : 121740854 : 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 : 333837798 : conv_flags (int i, int nargs, tree fn, tree arg, int flags)
2440 : : {
2441 : 333837798 : int lflags = flags;
2442 : 333837798 : tree t;
2443 : 336259958 : if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn)
2444 : 97174043 : && (t = FUNCTION_FIRST_USER_PARMTYPE (fn))
2445 : 431011841 : && (same_type_ignoring_top_level_qualifiers_p
2446 : 97174043 : (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn))))
2447 : : {
2448 : 75850239 : if (!(flags & LOOKUP_ONLYCONVERTING))
2449 : 22990467 : lflags |= LOOKUP_COPY_PARM;
2450 : 75850239 : if ((flags & LOOKUP_LIST_INIT_CTOR)
2451 : 75850239 : && BRACE_ENCLOSED_INITIALIZER_P (arg))
2452 : 1138 : lflags |= LOOKUP_NO_CONVERSION;
2453 : : }
2454 : : else
2455 : 257987559 : lflags |= LOOKUP_ONLYCONVERTING;
2456 : :
2457 : 333837798 : 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 : 74159681 : build_this_conversion (tree fn, tree ctype,
2466 : : tree& parmtype, tree& argtype, tree& arg,
2467 : : int flags, tsubst_flags_t complain)
2468 : : {
2469 : 148319362 : 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 : 74159681 : parmtype = cp_build_qualified_type (ctype,
2482 : 74159681 : cp_type_quals (TREE_TYPE (parmtype)));
2483 : 74159681 : bool this_p = true;
2484 : 74159681 : 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 : 88298 : bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2489 : 88298 : 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 : 88298 : this_p = false;
2493 : : }
2494 : : else
2495 : : {
2496 : 74071383 : 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 : 74071383 : arg = build_address (arg);
2501 : 74071383 : argtype = lvalue_type (arg);
2502 : : }
2503 : 74159681 : flags |= LOOKUP_ONLYCONVERTING;
2504 : 74159681 : conversion *t = implicit_conversion (parmtype, argtype, arg,
2505 : : /*c_cast_p=*/false, flags, complain);
2506 : 74159681 : t->this_p = this_p;
2507 : 74159681 : 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 : 481583951 : 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 : 481583951 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2536 : 481583951 : int i, len;
2537 : 481583951 : tree parmnode;
2538 : 481583951 : tree orig_first_arg = first_arg;
2539 : 481583951 : int skip;
2540 : 481583951 : int viable = 1;
2541 : 481583951 : struct rejection_reason *reason = NULL;
2542 : :
2543 : : /* The `this', `in_chrg' and VTT arguments to constructors are not
2544 : : considered in overload resolution. */
2545 : 963167902 : if (DECL_CONSTRUCTOR_P (fn))
2546 : : {
2547 : 206023750 : 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 : 206023675 : parmlist = skip_artificial_parms_for (fn, parmlist);
2552 : 206023750 : skip = num_artificial_parms_for (fn);
2553 : 206023750 : if (skip > 0 && first_arg != NULL_TREE)
2554 : : {
2555 : 206023750 : --skip;
2556 : 206023750 : first_arg = NULL_TREE;
2557 : : }
2558 : : }
2559 : : else
2560 : : skip = 0;
2561 : :
2562 : 931319012 : len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2563 : 481583951 : if (!convs)
2564 : 414459490 : 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 : 481583951 : parmnode = parmlist;
2574 : 994880841 : for (i = 0; i < len; ++i)
2575 : : {
2576 : 572371065 : if (parmnode == NULL_TREE || parmnode == void_list_node)
2577 : : break;
2578 : 513296890 : parmnode = TREE_CHAIN (parmnode);
2579 : : }
2580 : :
2581 : 481583951 : if ((i < len && parmnode)
2582 : 481583951 : || !sufficient_parms_p (parmnode))
2583 : : {
2584 : 121737528 : int remaining = remaining_arguments (parmnode);
2585 : 121737528 : viable = 0;
2586 : 121737528 : 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 : 431111899 : if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2595 : 99416267 : && flag_new_inheriting_ctors
2596 : 580991533 : && DECL_INHERITED_CTOR (fn))
2597 : : {
2598 : 641184 : tree ptype = non_reference (TREE_VALUE (parmlist));
2599 : 641184 : tree dtype = DECL_CONTEXT (fn);
2600 : 1282368 : tree btype = DECL_INHERITED_CTOR_BASE (fn);
2601 : 641184 : if (reference_related_p (ptype, dtype)
2602 : 641184 : && reference_related_p (btype, ptype))
2603 : : {
2604 : 434067 : viable = false;
2605 : 434067 : reason = inherited_ctor_rejection ();
2606 : : }
2607 : : }
2608 : :
2609 : : /* Second, for a function to be viable, its constraints must be
2610 : : satisfied. */
2611 : 481583951 : if (flag_concepts && viable && !constraints_satisfied_p (fn))
2612 : : {
2613 : 148261 : reason = constraint_failure ();
2614 : 148261 : 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 : 481583951 : if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2621 : : {
2622 : 63968146 : if (DECL_CONSTRUCTOR_P (fn))
2623 : : i = 1;
2624 : 16673553 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2625 : 16673553 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2626 : : i = 2;
2627 : : else
2628 : : i = 0;
2629 : 31984073 : if (i && len == i)
2630 : : {
2631 : 17130409 : parmnode = chain_index (i-1, parmlist);
2632 : 17130409 : if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2633 : : ctype))
2634 : 3473103 : viable = 0;
2635 : : }
2636 : :
2637 : : /* This only applies at the top level. */
2638 : 28510970 : flags &= ~LOOKUP_DEFAULTED;
2639 : : }
2640 : :
2641 : 481583951 : if (! viable)
2642 : 125792959 : goto out;
2643 : :
2644 : 355790992 : if (shortcut_bad_convs)
2645 : 355721525 : flags |= LOOKUP_SHORTCUT_BAD_CONVS;
2646 : : else
2647 : 69467 : 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 : 355790992 : parmnode = parmlist;
2654 : :
2655 : 628823212 : for (i = 0; i < len; ++i)
2656 : : {
2657 : 395540558 : tree argtype, to_type;
2658 : 395540558 : tree arg;
2659 : :
2660 : 395540558 : if (parmnode == void_list_node)
2661 : : break;
2662 : :
2663 : 395540558 : if (convs[i])
2664 : : {
2665 : : /* Already set during deduction. */
2666 : 5650645 : parmnode = TREE_CHAIN (parmnode);
2667 : 5650645 : continue;
2668 : : }
2669 : :
2670 : 389889913 : if (i == 0 && first_arg != NULL_TREE)
2671 : 71013437 : arg = first_arg;
2672 : : else
2673 : 608992457 : arg = CONST_CAST_TREE (
2674 : : (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2675 : 389889913 : argtype = lvalue_type (arg);
2676 : :
2677 : 389889913 : conversion *t;
2678 : 389889913 : if (parmnode)
2679 : : {
2680 : 384801618 : tree parmtype = TREE_VALUE (parmnode);
2681 : 384801618 : if (i == 0
2682 : 303979751 : && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2683 : 723602948 : && !DECL_CONSTRUCTOR_P (fn))
2684 : 71004847 : t = build_this_conversion (fn, ctype, parmtype, argtype, arg,
2685 : : flags, complain);
2686 : : else
2687 : : {
2688 : 313796771 : int lflags = conv_flags (i, len-skip, fn, arg, flags);
2689 : 313796771 : t = implicit_conversion (parmtype, argtype, arg,
2690 : : /*c_cast_p=*/false, lflags, complain);
2691 : : }
2692 : 384801618 : to_type = parmtype;
2693 : 384801618 : parmnode = TREE_CHAIN (parmnode);
2694 : : }
2695 : : else
2696 : : {
2697 : 5088295 : t = build_identity_conv (argtype, arg);
2698 : 5088295 : t->ellipsis_p = true;
2699 : 5088295 : to_type = argtype;
2700 : : }
2701 : :
2702 : 389889913 : convs[i] = t;
2703 : 389889913 : if (! t)
2704 : : {
2705 : 105243401 : viable = 0;
2706 : 105243401 : reason = arg_conversion_rejection (first_arg, i, argtype, to_type,
2707 : 105243401 : EXPR_LOCATION (arg));
2708 : 105243401 : break;
2709 : : }
2710 : :
2711 : 284646512 : if (t->bad_p)
2712 : : {
2713 : 17305078 : viable = -1;
2714 : 17305078 : reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type,
2715 : 17305078 : EXPR_LOCATION (arg));
2716 : 17305078 : if (shortcut_bad_convs)
2717 : : break;
2718 : : }
2719 : : }
2720 : :
2721 : 233282654 : out:
2722 : 481583951 : return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2723 : 481583951 : 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 : 49122 : 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 : 49122 : tree totype = TREE_TYPE (TREE_TYPE (fn));
2745 : 49122 : int i, len, viable, flags;
2746 : 49122 : tree parmlist, parmnode;
2747 : 49122 : conversion **convs;
2748 : 49122 : struct rejection_reason *reason;
2749 : :
2750 : 98321 : for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2751 : 49199 : parmlist = TREE_TYPE (parmlist);
2752 : 49122 : parmlist = TYPE_ARG_TYPES (parmlist);
2753 : :
2754 : 49122 : len = vec_safe_length (arglist) + 1;
2755 : 49122 : convs = alloc_conversions (len);
2756 : 49122 : parmnode = parmlist;
2757 : 49122 : viable = 1;
2758 : 49122 : flags = LOOKUP_IMPLICIT;
2759 : 49122 : reason = NULL;
2760 : :
2761 : : /* Don't bother looking up the same type twice. */
2762 : 49122 : if (*candidates && (*candidates)->fn == totype)
2763 : : return NULL;
2764 : :
2765 : 49107 : 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 : 160460 : for (i = 0; i < len; ++i)
2774 : : {
2775 : 111453 : tree arg, argtype, convert_type = NULL_TREE;
2776 : 111453 : conversion *t;
2777 : :
2778 : 111453 : if (i == 0)
2779 : : arg = obj;
2780 : : else
2781 : 62355 : arg = (*arglist)[i - 1];
2782 : 111453 : argtype = lvalue_type (arg);
2783 : :
2784 : 111453 : if (i == 0)
2785 : : {
2786 : 49098 : t = build_identity_conv (argtype, NULL_TREE);
2787 : 49098 : 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 : 49098 : convert_type = totype;
2791 : : }
2792 : 62355 : else if (parmnode == void_list_node)
2793 : : break;
2794 : 62323 : else if (parmnode)
2795 : : {
2796 : 62314 : t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2797 : : /*c_cast_p=*/false, flags, complain);
2798 : 62314 : 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 : 111421 : convs[i] = t;
2808 : 111421 : if (! t)
2809 : : break;
2810 : :
2811 : 111362 : 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 : 111362 : if (i == 0)
2819 : 49098 : continue;
2820 : :
2821 : 62264 : if (parmnode)
2822 : 62255 : parmnode = TREE_CHAIN (parmnode);
2823 : : }
2824 : :
2825 : 49098 : if (i < len
2826 : 49098 : || ! 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 : 49098 : return add_candidate (candidates, totype, obj, arglist, len, convs,
2834 : 49098 : access_path, conversion_path, viable, reason, flags);
2835 : : }
2836 : :
2837 : : static void
2838 : 8033016 : 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 : 8033016 : conversion *t;
2843 : 8033016 : conversion **convs;
2844 : 8033016 : size_t num_convs;
2845 : 8033016 : int viable = 1;
2846 : 8033016 : tree types[2];
2847 : 8033016 : struct rejection_reason *reason = NULL;
2848 : :
2849 : 8033016 : types[0] = type1;
2850 : 8033016 : types[1] = type2;
2851 : :
2852 : 8033016 : num_convs = args.length ();
2853 : 8033016 : 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 : 8033016 : if (type1 != boolean_type_node)
2861 : 7483110 : flags |= LOOKUP_ONLYCONVERTING;
2862 : :
2863 : 23467062 : for (unsigned i = 0; i < 2 && i < num_convs; ++i)
2864 : : {
2865 : 15434046 : t = implicit_conversion (types[i], argtypes[i], args[i],
2866 : : /*c_cast_p=*/false, flags, complain);
2867 : 15434046 : if (! t)
2868 : : {
2869 : 574837 : viable = 0;
2870 : : /* We need something for printing the candidate. */
2871 : 574837 : t = build_identity_conv (types[i], NULL_TREE);
2872 : 574837 : reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2873 : 574837 : types[i], EXPR_LOCATION (args[i]));
2874 : : }
2875 : 14859209 : 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 : 15434046 : convs[i] = t;
2883 : : }
2884 : :
2885 : : /* For COND_EXPR we rearranged the arguments; undo that now. */
2886 : 8033016 : 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 : 8033016 : 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 : 8033016 : }
2910 : :
2911 : : static bool
2912 : 9 : is_complete (tree t)
2913 : : {
2914 : 9 : 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 : 11200716 : 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 : 11200716 : 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 : 11200716 : 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 : 92509 : case INDIRECT_REF:
3011 : 92509 : if (TYPE_PTR_P (type1)
3012 : 92509 : && (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 : 240 : case UNARY_PLUS_EXPR: /* unary + */
3026 : 240 : if (TYPE_PTR_P (type1))
3027 : : break;
3028 : : /* FALLTHRU */
3029 : 50387 : case NEGATE_EXPR:
3030 : 50387 : 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 : 120047 : case BIT_NOT_EXPR:
3039 : 120047 : 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 : 28 : case MEMBER_REF:
3051 : 28 : if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
3052 : : {
3053 : 28 : tree c1 = TREE_TYPE (type1);
3054 : 28 : tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
3055 : :
3056 : 25 : if (CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
3057 : 50 : && (TYPE_PTRMEMFUNC_P (type2)
3058 : 9 : || 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 : 139097 : case MINUS_EXPR:
3119 : 139097 : if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
3120 : : break;
3121 : 13 : if (TYPE_PTROB_P (type1)
3122 : 139084 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3123 : : {
3124 : 3 : type2 = ptrdiff_type_node;
3125 : 3 : break;
3126 : : }
3127 : : /* FALLTHRU */
3128 : 310273 : case MULT_EXPR:
3129 : 310273 : case TRUNC_DIV_EXPR:
3130 : 310273 : 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 : 6543497 : case SPACESHIP_EXPR:
3139 : 6543497 : case EQ_EXPR:
3140 : 6543497 : case NE_EXPR:
3141 : 110320 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3142 : 6653817 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
3143 : : break;
3144 : 6543463 : if (NULLPTR_TYPE_P (type1) && NULLPTR_TYPE_P (type2))
3145 : : break;
3146 : 6543457 : if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
3147 : : {
3148 : : type2 = type1;
3149 : : break;
3150 : : }
3151 : 6543454 : if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
3152 : : {
3153 : : type1 = type2;
3154 : : break;
3155 : : }
3156 : : /* Fall through. */
3157 : 6913892 : case LT_EXPR:
3158 : 6913892 : case GT_EXPR:
3159 : 6913892 : case LE_EXPR:
3160 : 6913892 : case GE_EXPR:
3161 : 6913892 : case MAX_EXPR:
3162 : 6913892 : case MIN_EXPR:
3163 : 6913892 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3164 : : break;
3165 : 5385410 : if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3166 : : break;
3167 : 5384986 : if (TREE_CODE (type1) == ENUMERAL_TYPE
3168 : 3842470 : && TREE_CODE (type2) == ENUMERAL_TYPE)
3169 : : break;
3170 : 2810944 : if (TYPE_PTR_P (type1)
3171 : 2810944 : && null_ptr_cst_p (args[1]))
3172 : : {
3173 : : type2 = type1;
3174 : : break;
3175 : : }
3176 : 2810925 : if (null_ptr_cst_p (args[0])
3177 : 2810925 : && TYPE_PTR_P (type2))
3178 : : {
3179 : : type1 = type2;
3180 : : break;
3181 : : }
3182 : : return;
3183 : :
3184 : 184058 : case PLUS_EXPR:
3185 : 184058 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3186 : : break;
3187 : : /* FALLTHRU */
3188 : 141854 : case ARRAY_REF:
3189 : 141854 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
3190 : : {
3191 : 5 : type1 = ptrdiff_type_node;
3192 : 5 : break;
3193 : : }
3194 : 141849 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3195 : : {
3196 : 36892 : type2 = ptrdiff_type_node;
3197 : 36892 : 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 : 2420558 : case TRUNC_MOD_EXPR:
3213 : 2420558 : case BIT_AND_EXPR:
3214 : 2420558 : case BIT_IOR_EXPR:
3215 : 2420558 : case BIT_XOR_EXPR:
3216 : 2420558 : case LSHIFT_EXPR:
3217 : 2420558 : case RSHIFT_EXPR:
3218 : 2420558 : 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 : 1078823 : case MODIFY_EXPR:
3259 : 1078823 : switch (code2)
3260 : : {
3261 : 26544 : case PLUS_EXPR:
3262 : 26544 : case MINUS_EXPR:
3263 : 26544 : 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 : 26549 : case MULT_EXPR:
3270 : 26549 : case TRUNC_DIV_EXPR:
3271 : 26549 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3272 : : break;
3273 : : return;
3274 : :
3275 : 1052274 : case TRUNC_MOD_EXPR:
3276 : 1052274 : case BIT_AND_EXPR:
3277 : 1052274 : case BIT_IOR_EXPR:
3278 : 1052274 : case BIT_XOR_EXPR:
3279 : 1052274 : case LSHIFT_EXPR:
3280 : 1052274 : case RSHIFT_EXPR:
3281 : 1052274 : 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 : 987265 : type1 = build_reference_type (type1);
3304 : 987265 : 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 : 7482977 : bool u1 = uses_template_parms (type1);
3349 : 7482977 : bool u2 = type2 ? uses_template_parms (type2) : false;
3350 : 7482963 : 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 : 7482966 : 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 : 7343619 : if (type2 && !same_type_p (type1, type2)
3372 : 1320562 : && TREE_CODE (type1) == TREE_CODE (type2)
3373 : 7779077 : && (TYPE_REF_P (type1)
3374 : 296111 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3375 : 295994 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3376 : 295976 : || TYPE_PTRMEMFUNC_P (type1)
3377 : 295976 : || MAYBE_CLASS_TYPE_P (type1)
3378 : 295976 : || 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 : 7482737 : build_builtin_candidate
3405 : 7482737 : (candidates, fnname, type1, type2, args, argtypes, flags, complain);
3406 : : }
3407 : :
3408 : : tree
3409 : 704620167 : type_decays_to (tree type)
3410 : : {
3411 : 704620167 : if (TREE_CODE (type) == ARRAY_TYPE)
3412 : 5610805 : return build_pointer_type (TREE_TYPE (type));
3413 : 699009362 : if (TREE_CODE (type) == FUNCTION_TYPE)
3414 : 39960 : 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 : 17055185 : 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 : 17055185 : int ref1;
3438 : 17055185 : int enum_p = 0;
3439 : 17055185 : 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 : 17055185 : vec<tree, va_gc> *types[2];
3443 : 17055185 : unsigned ix;
3444 : 17055185 : vec<tree, va_gc> &args = *argv;
3445 : 17055185 : unsigned len = args.length ();
3446 : :
3447 : 47153697 : for (unsigned i = 0; i < len; ++i)
3448 : : {
3449 : 30098512 : if (args[i])
3450 : 30098512 : argtypes[i] = unlowered_expr_type (args[i]);
3451 : : else
3452 : 0 : argtypes[i] = NULL_TREE;
3453 : : }
3454 : :
3455 : 17055185 : 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 : 492639 : case TRUTH_NOT_EXPR:
3476 : 492639 : build_builtin_candidate
3477 : 492639 : (candidates, fnname, boolean_type_node,
3478 : : NULL_TREE, args, argtypes, flags, complain);
3479 : 9786479 : return;
3480 : :
3481 : 57267 : case TRUTH_ORIF_EXPR:
3482 : 57267 : case TRUTH_ANDIF_EXPR:
3483 : 57267 : build_builtin_candidate
3484 : 57267 : (candidates, fnname, boolean_type_node,
3485 : : boolean_type_node, args, argtypes, flags, complain);
3486 : 57267 : return;
3487 : :
3488 : : case ADDR_EXPR:
3489 : : case COMPOUND_EXPR:
3490 : : case COMPONENT_REF:
3491 : : case CO_AWAIT_EXPR:
3492 : : return;
3493 : :
3494 : 4575317 : case COND_EXPR:
3495 : 4575317 : case EQ_EXPR:
3496 : 4575317 : case NE_EXPR:
3497 : 4575317 : case LT_EXPR:
3498 : 4575317 : case LE_EXPR:
3499 : 4575317 : case GT_EXPR:
3500 : 4575317 : case GE_EXPR:
3501 : 4575317 : case SPACESHIP_EXPR:
3502 : 4575317 : enum_p = 1;
3503 : : /* Fall through. */
3504 : :
3505 : : default:
3506 : : ref1 = 0;
3507 : : }
3508 : :
3509 : 14674284 : types[0] = make_tree_vector ();
3510 : 14674284 : types[1] = make_tree_vector ();
3511 : :
3512 : 14674284 : if (len == 3)
3513 : 862 : len = 2;
3514 : 30194413 : for (unsigned i = 0; i < len; ++i)
3515 : : {
3516 : 22433068 : if (MAYBE_CLASS_TYPE_P (argtypes[i]))
3517 : : {
3518 : 8743039 : tree convs;
3519 : :
3520 : 8743039 : if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
3521 : : return;
3522 : :
3523 : 6314207 : convs = lookup_conversions (argtypes[i]);
3524 : :
3525 : 6314207 : 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 : 6312716 : else if (! convs)
3534 : : return;
3535 : :
3536 : 4175503 : for (; convs; convs = TREE_CHAIN (convs))
3537 : : {
3538 : 2345403 : type = TREE_TYPE (convs);
3539 : :
3540 : 2940090 : if (i == 0 && ref1
3541 : 2345403 : && (!TYPE_REF_P (type)
3542 : 79 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
3543 : 594687 : continue;
3544 : :
3545 : 1750716 : if (code == COND_EXPR && TYPE_REF_P (type))
3546 : 0 : vec_safe_push (types[i], type);
3547 : :
3548 : 1750716 : type = non_reference (type);
3549 : 1750716 : if (i != 0 || ! ref1)
3550 : : {
3551 : 1750677 : type = cv_unqualified (type_decays_to (type));
3552 : 1750677 : if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3553 : 656 : vec_safe_push (types[i], type);
3554 : 1750677 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3555 : 915877 : type = type_promotes_to (type);
3556 : : }
3557 : :
3558 : 1750716 : if (! vec_member (type, types[i]))
3559 : 1747775 : vec_safe_push (types[i], type);
3560 : : }
3561 : : }
3562 : : else
3563 : : {
3564 : 13690029 : if (code == COND_EXPR && lvalue_p (args[i]))
3565 : 125 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3566 : 13690029 : type = non_reference (argtypes[i]);
3567 : 13690029 : if (i != 0 || ! ref1)
3568 : : {
3569 : 12611320 : type = cv_unqualified (type_decays_to (type));
3570 : 12611320 : if (enum_p && UNSCOPED_ENUM_P (type))
3571 : 2465592 : vec_safe_push (types[i], type);
3572 : 12611320 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3573 : 8510078 : type = type_promotes_to (type);
3574 : : }
3575 : 13690029 : 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 : 16464853 : FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3582 : : {
3583 : 8703508 : unsigned jx;
3584 : 8703508 : tree u;
3585 : :
3586 : 8703508 : if (!types[1]->is_empty ())
3587 : 19641201 : FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3588 : 10937693 : add_builtin_candidate
3589 : 10937693 : (candidates, code, code2, fnname, t,
3590 : : u, args, argtypes, flags, complain);
3591 : : else
3592 : 263023 : add_builtin_candidate
3593 : 263023 : (candidates, code, code2, fnname, t,
3594 : : NULL_TREE, args, argtypes, flags, complain);
3595 : : }
3596 : :
3597 : 7761345 : release_tree_vector (types[0]);
3598 : 7761345 : 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 : 372416889 : 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 : 372416889 : int ntparms = DECL_NTPARMS (tmpl);
3623 : 372416889 : tree targs = make_tree_vec (ntparms);
3624 : 372416889 : unsigned int len = vec_safe_length (arglist);
3625 : 372416889 : unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3626 : 372416889 : unsigned int skip_without_in_chrg = 0;
3627 : 372416889 : tree first_arg_without_in_chrg = first_arg;
3628 : 372416889 : tree *args_without_in_chrg;
3629 : 372416889 : unsigned int nargs_without_in_chrg;
3630 : 372416889 : unsigned int ia, ix;
3631 : 372416889 : tree arg;
3632 : 372416889 : struct z_candidate *cand;
3633 : 372416889 : tree fn;
3634 : 372416889 : struct rejection_reason *reason = NULL;
3635 : 372416889 : int errs;
3636 : 372416889 : conversion **convs = NULL;
3637 : :
3638 : : /* We don't do deduction on the in-charge parameter, the VTT
3639 : : parameter or 'this'. */
3640 : 372416889 : if (DECL_IOBJ_MEMBER_FUNCTION_P (tmpl))
3641 : : {
3642 : 44504555 : 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 : 372416889 : ++skip_without_in_chrg;
3649 : : }
3650 : :
3651 : 372416889 : if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3652 : 372416889 : || DECL_BASE_CONSTRUCTOR_P (tmpl))
3653 : 373367313 : && 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 : 372416889 : if (len < skip_without_in_chrg)
3662 : 0 : return add_ignored_candidate (candidates, tmpl);
3663 : :
3664 : 413362620 : if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3665 : 404785896 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3666 : 32369007 : 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 : 1713795 : if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3679 : : {
3680 : 1713795 : firstparm = TREE_VALUE (firstparm);
3681 : 1713795 : if (PACK_EXPANSION_P (firstparm))
3682 : 1607 : firstparm = PACK_EXPANSION_PATTERN (firstparm);
3683 : 1713795 : if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3684 : : {
3685 : 601770 : gcc_assert (!explicit_targs);
3686 : 601770 : reason = invalid_copy_with_fn_template_rejection ();
3687 : 601770 : goto fail;
3688 : : }
3689 : : }
3690 : : }
3691 : :
3692 : 371815119 : nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3693 : 371815119 : + (len - skip_without_in_chrg));
3694 : 371815119 : args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3695 : 371815119 : ia = 0;
3696 : 371815119 : if (first_arg_without_in_chrg != NULL_TREE)
3697 : : {
3698 : 4590 : args_without_in_chrg[ia] = first_arg_without_in_chrg;
3699 : 4590 : ++ia;
3700 : : }
3701 : 615565479 : for (ix = skip_without_in_chrg;
3702 : 987380598 : vec_safe_iterate (arglist, ix, &arg);
3703 : : ++ix)
3704 : : {
3705 : 615565479 : args_without_in_chrg[ia] = arg;
3706 : 615565479 : ++ia;
3707 : : }
3708 : 371815119 : gcc_assert (ia == nargs_without_in_chrg);
3709 : :
3710 : 371815119 : 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 : 371815107 : int min_arity = 0, max_arity = 0;
3718 : 371815107 : tree parms = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
3719 : 371815107 : parms = skip_artificial_parms_for (tmpl, parms);
3720 : 1395470022 : for (; parms != void_list_node; parms = TREE_CHAIN (parms))
3721 : : {
3722 : 1309959965 : if (!parms || PACK_EXPANSION_P (TREE_VALUE (parms)))
3723 : : {
3724 : : max_arity = -1;
3725 : : break;
3726 : : }
3727 : 651839808 : if (TREE_PURPOSE (parms))
3728 : : /* A parameter with a default argument. */
3729 : 8074951 : ++max_arity;
3730 : : else
3731 : 643764857 : ++min_arity, ++max_arity;
3732 : : }
3733 : 371815107 : if (ia < (unsigned)min_arity)
3734 : : {
3735 : : /* Too few arguments. */
3736 : 29080891 : reason = arity_rejection (NULL_TREE, min_arity, ia,
3737 : : /*least_p=*/(max_arity == -1));
3738 : 29080891 : goto fail;
3739 : : }
3740 : 342734216 : else if (max_arity != -1 && ia > (unsigned)max_arity)
3741 : : {
3742 : : /* Too many arguments. */
3743 : 4490713 : reason = arity_rejection (NULL_TREE, max_arity, ia);
3744 : 4490713 : goto fail;
3745 : : }
3746 : :
3747 : 338243503 : convs = alloc_conversions (nargs);
3748 : :
3749 : 338243503 : if (shortcut_bad_convs
3750 : 338239613 : && DECL_IOBJ_MEMBER_FUNCTION_P (tmpl)
3751 : 396065055 : && !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 : 3154834 : tree parmtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (tmpl)));
3758 : 3154834 : tree argtype = lvalue_type (first_arg);
3759 : 3154834 : tree arg = first_arg;
3760 : 3154834 : conversion *t = build_this_conversion (tmpl, ctype,
3761 : : parmtype, argtype, arg,
3762 : : flags, complain);
3763 : 3154834 : convs[0] = t;
3764 : 3154834 : if (t->bad_p)
3765 : : {
3766 : 33163 : reason = bad_arg_conversion_rejection (first_arg, 0,
3767 : : arg, parmtype,
3768 : 33163 : EXPR_LOCATION (arg));
3769 : 33163 : goto fail;
3770 : : }
3771 : : }
3772 : : }
3773 : :
3774 : 338210352 : errs = errorcount+sorrycount;
3775 : 676407153 : 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 : 338210352 : false, complain & tf_decltype);
3780 : :
3781 : 338196801 : if (fn == error_mark_node)
3782 : : {
3783 : : /* Don't repeat unification later if it already resulted in errors. */
3784 : 271068180 : if (errorcount+sorrycount == errs)
3785 : 271068071 : 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 : 271068180 : 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 : 67128621 : if (((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
3798 : : == LOOKUP_ONLYCONVERTING)
3799 : 67128621 : && DECL_NONCONVERTING_P (fn))
3800 : 3868 : return add_ignored_candidate (candidates, fn);
3801 : :
3802 : 134249506 : if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3803 : : {
3804 : 3071705 : tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3805 : 6143383 : 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 : 283 : reason = invalid_copy_with_fn_template_rejection ();
3812 : 283 : goto fail;
3813 : : }
3814 : : }
3815 : :
3816 : 67124470 : 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 : 67124461 : cand = add_function_candidate (candidates, fn, ctype,
3822 : : first_arg, arglist, access_path,
3823 : : conversion_path, flags, convs,
3824 : : shortcut_bad_convs, complain);
3825 : 67124470 : 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 : 3966227 : cand->template_decl = build_template_info (tmpl, targs);
3844 : : else
3845 : 63158243 : cand->template_decl = DECL_TEMPLATE_INFO (fn);
3846 : 67124470 : cand->explicit_targs = explicit_targs;
3847 : :
3848 : 67124470 : return cand;
3849 : 305275000 : fail:
3850 : 305275000 : int viable = (reason->code == rr_bad_arg_conversion ? -1 : 0);
3851 : 305275000 : return add_candidate (candidates, tmpl, first_arg, arglist, nargs, convs,
3852 : 305275000 : access_path, conversion_path, viable, reason, flags);
3853 : : }
3854 : :
3855 : :
3856 : : static struct z_candidate *
3857 : 372416877 : 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 : 372416877 : 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 : 372403326 : 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 : 239871589 : splice_viable (struct z_candidate *cands,
3903 : : bool strict_p,
3904 : : bool *any_viable_p)
3905 : : {
3906 : 239871589 : z_candidate *strictly_viable = nullptr;
3907 : 239871589 : z_candidate **strictly_viable_tail = &strictly_viable;
3908 : :
3909 : 239871589 : z_candidate *non_strictly_viable = nullptr;
3910 : 239871589 : z_candidate **non_strictly_viable_tail = &non_strictly_viable;
3911 : :
3912 : 239871589 : z_candidate *non_viable = nullptr;
3913 : 239871589 : z_candidate **non_viable_tail = &non_viable;
3914 : :
3915 : 239871589 : z_candidate *non_viable_ignored = nullptr;
3916 : 239871589 : 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 : 239871589 : if (processing_template_decl)
3921 : 39802606 : strict_p = true;
3922 : :
3923 : 913479224 : for (z_candidate *cand = cands; cand; cand = cand->next)
3924 : : {
3925 : 673607635 : if (!strict_p
3926 : 237668174 : && (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 : 605662977 : strict_p = true;
3931 : :
3932 : : /* Move this candidate to the appropriate list according to
3933 : : its viability. */
3934 : 673607635 : auto& tail = (cand->viable == 1 ? strictly_viable_tail
3935 : : : cand->viable == -1 ? non_strictly_viable_tail
3936 : 1048972893 : : ignored_candidate_p (cand) ? non_viable_ignored_tail
3937 : 1048972893 : : non_viable_tail);
3938 : 673607635 : *tail = cand;
3939 : 673607635 : tail = &cand->next;
3940 : : }
3941 : :
3942 : 479743178 : *any_viable_p = (strictly_viable != nullptr
3943 : 239871589 : || (!strict_p && non_strictly_viable != nullptr));
3944 : :
3945 : : /* Combine the lists. */
3946 : 239871589 : *non_viable_ignored_tail = nullptr;
3947 : 239871589 : *non_viable_tail = non_viable_ignored;
3948 : 239871589 : *non_strictly_viable_tail = non_viable;
3949 : 239871589 : *strictly_viable_tail = non_strictly_viable;
3950 : :
3951 : 239871589 : return strictly_viable;
3952 : : }
3953 : :
3954 : : static bool
3955 : 228921683 : any_strictly_viable (struct z_candidate *cands)
3956 : : {
3957 : 293388218 : for (; cands; cands = cands->next)
3958 : 68204068 : 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 : 52406251 : 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 : 52406251 : if (processing_template_decl)
3973 : 64303 : return build_address (obj);
3974 : :
3975 : 52341948 : 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 : 5305943 : equal_functions (tree fn1, tree fn2)
3984 : : {
3985 : 5305943 : if (TREE_CODE (fn1) != TREE_CODE (fn2))
3986 : : return 0;
3987 : 5281771 : if (TREE_CODE (fn1) == TEMPLATE_DECL)
3988 : 49210 : return fn1 == fn2;
3989 : 10465104 : if (DECL_LOCAL_DECL_P (fn1) || DECL_LOCAL_DECL_P (fn2)
3990 : 10465101 : || DECL_EXTERN_C_FUNCTION_P (fn1))
3991 : 3135456 : return decls_match (fn1, fn2);
3992 : 2097105 : return fn1 == fn2;
3993 : : }
3994 : :
3995 : : /* Print information about a candidate FN being rejected due to INFO. */
3996 : :
3997 : : static void
3998 : 5493 : print_conversion_rejection (location_t loc, struct conversion_info *info,
3999 : : tree fn)
4000 : : {
4001 : 5493 : tree from = info->from;
4002 : 5493 : if (!TYPE_P (from))
4003 : 4440 : from = lvalue_type (from);
4004 : 5493 : 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 : 5449 : 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 : 1053 : 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 : 1019 : if (TREE_CODE (fn) == FUNCTION_DECL)
4033 : 862 : loc = get_fndecl_argument_location (fn, info->n_arg);
4034 : 1019 : inform (loc, "no known conversion for argument %d from %qH to %qI",
4035 : 1019 : info->n_arg + 1, from, info->to_type);
4036 : : }
4037 : 5493 : }
4038 : :
4039 : : /* Print information about a candidate with WANT parameters and we found
4040 : : HAVE. */
4041 : :
4042 : : static void
4043 : 1462 : print_arity_information (location_t loc, unsigned int have, unsigned int want,
4044 : : bool least_p)
4045 : : {
4046 : 1462 : 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 : 1421 : inform_n (loc, want,
4053 : : "candidate expects %d argument, %d provided",
4054 : : "candidate expects %d arguments, %d provided",
4055 : : want, have);
4056 : 1462 : }
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 : 13711 : print_z_candidate (location_t loc, const char *msgstr,
4067 : : struct z_candidate *candidate)
4068 : : {
4069 : 13711 : const char *msg = (msgstr == NULL
4070 : 13711 : ? ""
4071 : 13711 : : ACONCAT ((_(msgstr), " ", NULL)));
4072 : 13711 : tree fn = candidate->fn;
4073 : 13711 : if (flag_new_inheriting_ctors)
4074 : 13684 : fn = strip_inheriting_ctors (fn);
4075 : 13711 : location_t cloc = location_of (fn);
4076 : :
4077 : 13711 : 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 : 13498 : else if (TYPE_P (fn))
4094 : 9 : inform (cloc, "%s%qT (conversion)", msg, fn);
4095 : 13489 : else if (candidate->viable == -1)
4096 : 4453 : inform (cloc, "%s%#qD (near match)", msg, fn);
4097 : 9036 : else if (ignored_candidate_p (candidate))
4098 : 6 : inform (cloc, "%s%#qD (ignored)", msg, fn);
4099 : 9030 : else if (DECL_DELETED_FN (fn))
4100 : 49 : inform (cloc, "%s%#qD (deleted)", msg, fn);
4101 : 8981 : else if (candidate->reversed ())
4102 : 94 : inform (cloc, "%s%#qD (reversed)", msg, fn);
4103 : 8887 : else if (candidate->rewritten ())
4104 : 0 : inform (cloc, "%s%#qD (rewritten)", msg, fn);
4105 : : else
4106 : 8887 : inform (cloc, "%s%#qD", msg, fn);
4107 : :
4108 : 13711 : auto_diagnostic_nesting_level sentinel;
4109 : :
4110 : 13711 : if (fn != candidate->fn)
4111 : : {
4112 : 51 : cloc = location_of (candidate->fn);
4113 : 51 : inform (cloc, "inherited here");
4114 : : }
4115 : :
4116 : : /* Give the user some information about why this candidate failed. */
4117 : 13711 : if (candidate->reason != NULL)
4118 : : {
4119 : 10648 : struct rejection_reason *r = candidate->reason;
4120 : :
4121 : 10648 : switch (r->code)
4122 : : {
4123 : 1462 : case rr_arity:
4124 : 1462 : print_arity_information (cloc, r->u.arity.actual,
4125 : 1462 : r->u.arity.expected,
4126 : 1462 : r->u.arity.least_p);
4127 : 1462 : break;
4128 : 1025 : case rr_arg_conversion:
4129 : 1025 : print_conversion_rejection (cloc, &r->u.conversion, fn);
4130 : 1025 : break;
4131 : 4468 : case rr_bad_arg_conversion:
4132 : 4468 : print_conversion_rejection (cloc, &r->u.bad_conversion, fn);
4133 : 4468 : break;
4134 : 10 : case rr_explicit_conversion:
4135 : 10 : inform (cloc, "return type %qT of explicit conversion function "
4136 : : "cannot be converted to %qT with a qualification "
4137 : : "conversion", r->u.conversion.from,
4138 : : r->u.conversion.to_type);
4139 : 10 : break;
4140 : 6 : case rr_template_conversion:
4141 : 6 : inform (cloc, "conversion from return type %qT of template "
4142 : : "conversion function specialization to %qT is not an "
4143 : : "exact match", r->u.conversion.from,
4144 : : r->u.conversion.to_type);
4145 : 6 : break;
4146 : 3545 : case rr_template_unification:
4147 : : /* We use template_unification_error_rejection if unification caused
4148 : : actual non-SFINAE errors, in which case we don't need to repeat
4149 : : them here. */
4150 : 3545 : if (r->u.template_unification.tmpl == NULL_TREE)
4151 : : {
4152 : 45 : inform (cloc, "substitution of deduced template arguments "
4153 : : "resulted in errors seen above");
4154 : 45 : break;
4155 : : }
4156 : : /* Re-run template unification with diagnostics. */
4157 : 3500 : inform (cloc, "template argument deduction/substitution failed:");
4158 : 3500 : {
4159 : 3500 : auto_diagnostic_nesting_level sentinel;
4160 : 3500 : fn_type_unification (r->u.template_unification.tmpl,
4161 : : r->u.template_unification.explicit_targs,
4162 : : (make_tree_vec
4163 : : (r->u.template_unification.num_targs)),
4164 : : r->u.template_unification.args,
4165 : : r->u.template_unification.nargs,
4166 : : r->u.template_unification.return_type,
4167 : : r->u.template_unification.strict,
4168 : : r->u.template_unification.flags,
4169 : : NULL, true, false);
4170 : 3500 : }
4171 : 3500 : break;
4172 : 2 : case rr_invalid_copy:
4173 : 2 : inform (cloc,
4174 : : "a constructor taking a single argument of its own "
4175 : : "class type is invalid");
4176 : 2 : break;
4177 : 96 : case rr_constraint_failure:
4178 : 96 : diagnose_constraints (cloc, fn, NULL_TREE);
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 : : }
4194 : 13711 : }
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 : 5404 : print_z_candidates (location_t loc, struct z_candidate *candidates,
4202 : : tristate only_viable_p /* = tristate::unknown () */)
4203 : : {
4204 : 5404 : struct z_candidate *cand1;
4205 : 5404 : struct z_candidate **cand2;
4206 : :
4207 : 5404 : if (!candidates)
4208 : 1001 : return;
4209 : :
4210 : : /* Remove non-viable deleted candidates. */
4211 : 4403 : cand1 = candidates;
4212 : 20482 : for (cand2 = &cand1; *cand2; )
4213 : : {
4214 : 16079 : if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
4215 : 11568 : && !(*cand2)->viable
4216 : 19944 : && DECL_DELETED_FN ((*cand2)->fn))
4217 : 100 : *cand2 = (*cand2)->next;
4218 : : else
4219 : 15979 : cand2 = &(*cand2)->next;
4220 : : }
4221 : : /* ...if there are any non-deleted ones. */
4222 : 4403 : if (cand1)
4223 : 4397 : 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 : 20288 : for (cand1 = candidates; cand1; cand1 = cand1->next)
4231 : : {
4232 : 15885 : tree fn = cand1->fn;
4233 : : /* Skip builtin candidates and conversion functions. */
4234 : 15885 : if (!DECL_P (fn))
4235 : 282 : continue;
4236 : 15603 : cand2 = &cand1->next;
4237 : 167323 : while (*cand2)
4238 : : {
4239 : 151720 : if (DECL_P ((*cand2)->fn)
4240 : 151720 : && equal_functions (fn, (*cand2)->fn))
4241 : 100 : *cand2 = (*cand2)->next;
4242 : : else
4243 : 151620 : 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 : 4403 : if (only_viable_p.is_unknown ())
4252 : 4394 : only_viable_p = candidates->viable == 1;
4253 : :
4254 : 4403 : auto_diagnostic_nesting_level sentinel;
4255 : :
4256 : 4403 : int num_candidates = 0;
4257 : 18005 : for (auto iter = candidates; iter; iter = iter->next)
4258 : : {
4259 : 13943 : if (only_viable_p.is_true () && iter->viable != 1)
4260 : : break;
4261 : 13602 : ++num_candidates;
4262 : : }
4263 : :
4264 : 4403 : inform_n (loc,
4265 : : num_candidates, "there is %i candidate", "there are %i candidates",
4266 : : num_candidates);
4267 : 4403 : auto_diagnostic_nesting_level sentinel2;
4268 : :
4269 : 4403 : int candidate_idx = 0;
4270 : 22378 : for (; candidates; candidates = candidates->next)
4271 : : {
4272 : 13939 : if (only_viable_p.is_true () && candidates->viable != 1)
4273 : : break;
4274 : 13630 : if (ignored_candidate_p (candidates) && !flag_diagnostics_all_candidates)
4275 : : {
4276 : 26 : inform (loc, "some candidates omitted; "
4277 : : "use %<-fdiagnostics-all-candidates%> to display them");
4278 : 26 : break;
4279 : : }
4280 : 13572 : pretty_printer pp;
4281 : 13572 : pp_printf (&pp, N_("candidate %i:"), candidate_idx + 1);
4282 : 13572 : const char *const msgstr = pp_formatted_text (&pp);
4283 : 13572 : print_z_candidate (loc, msgstr, candidates);
4284 : 13572 : ++candidate_idx;
4285 : 13572 : }
4286 : 4403 : }
4287 : :
4288 : : /* USER_SEQ is a user-defined conversion sequence, beginning with a
4289 : : USER_CONV. STD_SEQ is the standard conversion sequence applied to
4290 : : the result of the conversion function to convert it to the final
4291 : : desired type. Merge the two sequences into a single sequence,
4292 : : and return the merged sequence. */
4293 : :
4294 : : static conversion *
4295 : 9792874 : merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
4296 : : {
4297 : 9792874 : conversion **t;
4298 : 9792874 : bool bad = user_seq->bad_p;
4299 : :
4300 : 9792874 : gcc_assert (user_seq->kind == ck_user);
4301 : :
4302 : : /* Find the end of the second conversion sequence. */
4303 : 10374542 : for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
4304 : : {
4305 : : /* The entire sequence is a user-conversion sequence. */
4306 : 581668 : (*t)->user_conv_p = true;
4307 : 581668 : if (bad)
4308 : 2439 : (*t)->bad_p = true;
4309 : : }
4310 : :
4311 : 9792874 : if ((*t)->rvaluedness_matches_p)
4312 : : /* We're binding a reference directly to the result of the conversion.
4313 : : build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
4314 : : type, but we want it back. */
4315 : 334055 : user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn));
4316 : :
4317 : : /* Replace the identity conversion with the user conversion
4318 : : sequence. */
4319 : 9792874 : *t = user_seq;
4320 : :
4321 : 9792874 : return std_seq;
4322 : : }
4323 : :
4324 : : /* Handle overload resolution for initializing an object of class type from
4325 : : an initializer list. First we look for a suitable constructor that
4326 : : takes a std::initializer_list; if we don't find one, we then look for a
4327 : : non-list constructor.
4328 : :
4329 : : Parameters are as for add_candidates, except that the arguments are in
4330 : : the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
4331 : : the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
4332 : :
4333 : : static void
4334 : 1387230 : add_list_candidates (tree fns, tree first_arg,
4335 : : const vec<tree, va_gc> *args, tree totype,
4336 : : tree explicit_targs, bool template_only,
4337 : : tree conversion_path, tree access_path,
4338 : : int flags,
4339 : : struct z_candidate **candidates,
4340 : : tsubst_flags_t complain)
4341 : : {
4342 : 1387230 : gcc_assert (*candidates == NULL);
4343 : :
4344 : : /* We're looking for a ctor for list-initialization. */
4345 : 1387230 : flags |= LOOKUP_LIST_INIT_CTOR;
4346 : : /* And we don't allow narrowing conversions. We also use this flag to
4347 : : avoid the copy constructor call for copy-list-initialization. */
4348 : 1387230 : flags |= LOOKUP_NO_NARROWING;
4349 : :
4350 : 2774460 : unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
4351 : 1387230 : tree init_list = (*args)[nart];
4352 : :
4353 : : /* Always use the default constructor if the list is empty (DR 990). */
4354 : 1387230 : if (CONSTRUCTOR_NELTS (init_list) == 0
4355 : 1387230 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
4356 : : ;
4357 : 1222079 : else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list)
4358 : 1222079 : && !CP_AGGREGATE_TYPE_P (totype))
4359 : : {
4360 : 48 : if (complain & tf_error)
4361 : 12 : error ("designated initializers cannot be used with a "
4362 : : "non-aggregate type %qT", totype);
4363 : 2785 : return;
4364 : : }
4365 : : /* If the class has a list ctor, try passing the list as a single
4366 : : argument first, but only consider list ctors. */
4367 : 1222031 : else if (TYPE_HAS_LIST_CTOR (totype))
4368 : : {
4369 : 76547 : flags |= LOOKUP_LIST_ONLY;
4370 : 76547 : add_candidates (fns, first_arg, args, NULL_TREE,
4371 : : explicit_targs, template_only, conversion_path,
4372 : : access_path, flags, candidates, complain);
4373 : 153094 : if (any_strictly_viable (*candidates))
4374 : : return;
4375 : : }
4376 : :
4377 : : /* Expand the CONSTRUCTOR into a new argument vec. */
4378 : 1384445 : vec<tree, va_gc> *new_args;
4379 : 2603192 : vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
4380 : 1384448 : for (unsigned i = 0; i < nart; ++i)
4381 : 3 : new_args->quick_push ((*args)[i]);
4382 : 1384445 : new_args = append_ctor_to_tree_vector (new_args, init_list);
4383 : :
4384 : : /* We aren't looking for list-ctors anymore. */
4385 : 1384445 : flags &= ~LOOKUP_LIST_ONLY;
4386 : : /* We allow more user-defined conversions within an init-list. */
4387 : 1384445 : flags &= ~LOOKUP_NO_CONVERSION;
4388 : :
4389 : 1384445 : add_candidates (fns, first_arg, new_args, NULL_TREE,
4390 : : explicit_targs, template_only, conversion_path,
4391 : : access_path, flags, candidates, complain);
4392 : : }
4393 : :
4394 : : /* Given C(std::initializer_list<A>), return A. */
4395 : :
4396 : : static tree
4397 : 1391 : list_ctor_element_type (tree fn)
4398 : : {
4399 : 1391 : gcc_checking_assert (is_list_ctor (fn));
4400 : :
4401 : 1391 : tree parm = FUNCTION_FIRST_USER_PARMTYPE (fn);
4402 : 1391 : parm = non_reference (TREE_VALUE (parm));
4403 : 1391 : return TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
4404 : : }
4405 : :
4406 : : /* If EXPR is a braced-init-list where the elements all decay to the same type,
4407 : : return that type. */
4408 : :
4409 : : static tree
4410 : 1284 : braced_init_element_type (tree expr)
4411 : : {
4412 : 1284 : if (TREE_CODE (expr) == CONSTRUCTOR
4413 : 1284 : && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
4414 : 0 : return TREE_TYPE (TREE_TYPE (expr));
4415 : 1284 : if (!BRACE_ENCLOSED_INITIALIZER_P (expr))
4416 : : return NULL_TREE;
4417 : :
4418 : 1284 : tree elttype = NULL_TREE;
4419 : 13779 : for (constructor_elt &e: CONSTRUCTOR_ELTS (expr))
4420 : : {
4421 : 10103 : tree type = TREE_TYPE (e.value);
4422 : 10103 : type = type_decays_to (type);
4423 : 10103 : if (!elttype)
4424 : : elttype = type;
4425 : 8838 : else if (!same_type_p (type, elttype))
4426 : : return NULL_TREE;
4427 : : }
4428 : : return elttype;
4429 : : }
4430 : :
4431 : : /* True iff EXPR contains any temporaries with non-trivial destruction.
4432 : :
4433 : : ??? Also ignore classes with non-trivial but no-op destruction other than
4434 : : std::allocator? */
4435 : :
4436 : : static bool
4437 : 196 : has_non_trivial_temporaries (tree expr)
4438 : : {
4439 : 196 : auto_vec<tree*> temps;
4440 : 196 : cp_walk_tree_without_duplicates (&expr, find_temps_r, &temps);
4441 : 400 : for (tree *p : temps)
4442 : : {
4443 : 68 : tree t = TREE_TYPE (*p);
4444 : 68 : if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (t)
4445 : 68 : && !is_std_allocator (t))
4446 : : return true;
4447 : : }
4448 : : return false;
4449 : 196 : }
4450 : :
4451 : : /* Return number of initialized elements in CTOR. */
4452 : :
4453 : : unsigned HOST_WIDE_INT
4454 : 278 : count_ctor_elements (tree ctor)
4455 : : {
4456 : 278 : unsigned HOST_WIDE_INT len = 0;
4457 : 2223 : for (constructor_elt &e: CONSTRUCTOR_ELTS (ctor))
4458 : 1389 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4459 : 9 : len += RAW_DATA_LENGTH (e.value);
4460 : : else
4461 : 1380 : ++len;
4462 : 278 : return len;
4463 : : }
4464 : :
4465 : : /* We're initializing an array of ELTTYPE from INIT. If it seems useful,
4466 : : return INIT as an array (of its own type) so the caller can initialize the
4467 : : target array in a loop. */
4468 : :
4469 : : static tree
4470 : 5248 : maybe_init_list_as_array (tree elttype, tree init)
4471 : : {
4472 : : /* Only do this if the array can go in rodata but not once converted. */
4473 : 5248 : if (!TYPE_NON_AGGREGATE_CLASS (elttype))
4474 : : return NULL_TREE;
4475 : 1284 : tree init_elttype = braced_init_element_type (init);
4476 : 1284 : if (!init_elttype || !SCALAR_TYPE_P (init_elttype) || !TREE_CONSTANT (init))
4477 : : return NULL_TREE;
4478 : :
4479 : : /* Check with a stub expression to weed out special cases, and check whether
4480 : : we call the same function for direct-init as copy-list-init. */
4481 : 356 : conversion_obstack_sentinel cos;
4482 : 356 : init_elttype = cp_build_qualified_type (init_elttype, TYPE_QUAL_CONST);
4483 : 356 : tree arg = build_stub_object (init_elttype);
4484 : 356 : conversion *c = implicit_conversion (elttype, init_elttype, arg, false,
4485 : : LOOKUP_NORMAL, tf_none);
4486 : 356 : if (c && c->kind == ck_rvalue)
4487 : 0 : c = next_conversion (c);
4488 : 350 : if (!c || c->kind != ck_user)
4489 : : return NULL_TREE;
4490 : : /* Check that we actually can perform the conversion. */
4491 : 347 : if (convert_like (c, arg, tf_none) == error_mark_node)
4492 : : /* Let the normal code give the error. */
4493 : : return NULL_TREE;
4494 : :
4495 : : /* A glvalue initializer might be significant to a reference constructor
4496 : : or conversion operator. */
4497 : 341 : if (!DECL_CONSTRUCTOR_P (c->cand->fn)
4498 : 341 : || (TYPE_REF_P (TREE_VALUE
4499 : : (FUNCTION_FIRST_USER_PARMTYPE (c->cand->fn)))))
4500 : 240 : for (auto &ce : CONSTRUCTOR_ELTS (init))
4501 : 108 : if (non_mergeable_glvalue_p (ce.value))
4502 : : return NULL_TREE;
4503 : :
4504 : 338 : tree first = CONSTRUCTOR_ELT (init, 0)->value;
4505 : 338 : conversion *fc = implicit_conversion (elttype, init_elttype, first, false,
4506 : : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING,
4507 : : tf_none);
4508 : 338 : if (fc && fc->kind == ck_rvalue)
4509 : 48 : fc = next_conversion (fc);
4510 : 338 : if (!fc || fc->kind != ck_user || fc->cand->fn != c->cand->fn)
4511 : : return NULL_TREE;
4512 : 296 : first = convert_like (fc, first, tf_none);
4513 : 296 : if (first == error_mark_node)
4514 : : /* Let the normal code give the error. */
4515 : : return NULL_TREE;
4516 : :
4517 : : /* Don't do this if the conversion would be constant. */
4518 : 293 : first = maybe_constant_init (first);
4519 : 293 : if (TREE_CONSTANT (first))
4520 : : return NULL_TREE;
4521 : :
4522 : : /* We can't do this if the conversion creates temporaries that need
4523 : : to live until the whole array is initialized. */
4524 : 196 : if (has_non_trivial_temporaries (first))
4525 : : return NULL_TREE;
4526 : :
4527 : : /* We can't do this if copying from the initializer_list would be
4528 : : ill-formed. */
4529 : 196 : tree copy_argtypes = make_tree_vec (1);
4530 : 196 : TREE_VEC_ELT (copy_argtypes, 0)
4531 : 196 : = cp_build_qualified_type (elttype, TYPE_QUAL_CONST);
4532 : 196 : if (!is_xible (INIT_EXPR, elttype, copy_argtypes))
4533 : : return NULL_TREE;
4534 : :
4535 : 190 : unsigned HOST_WIDE_INT len = count_ctor_elements (init);
4536 : 190 : tree arr = build_array_of_n_type (init_elttype, len);
4537 : 190 : arr = finish_compound_literal (arr, init, tf_none);
4538 : 190 : DECL_MERGEABLE (TARGET_EXPR_SLOT (arr)) = true;
4539 : 190 : return arr;
4540 : 356 : }
4541 : :
4542 : : /* If we were going to call e.g. vector(initializer_list<string>) starting
4543 : : with a list of string-literals (which is inefficient, see PR105838),
4544 : : instead build an array of const char* and pass it to the range constructor.
4545 : : But only do this for standard library types, where we can assume the
4546 : : transformation makes sense.
4547 : :
4548 : : Really the container classes should have initializer_list<U> constructors to
4549 : : get the same effect more simply; this is working around that lack. */
4550 : :
4551 : : static tree
4552 : 9470399 : maybe_init_list_as_range (tree fn, tree expr)
4553 : : {
4554 : 9470399 : if (!processing_template_decl
4555 : 9215860 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
4556 : 655223 : && is_list_ctor (fn)
4557 : 9471941 : && decl_in_std_namespace_p (fn))
4558 : : {
4559 : 1391 : tree to = list_ctor_element_type (fn);
4560 : 1391 : if (tree init = maybe_init_list_as_array (to, expr))
4561 : : {
4562 : 58 : tree begin = decay_conversion (TARGET_EXPR_SLOT (init), tf_none);
4563 : 58 : tree nelts = array_type_nelts_top (TREE_TYPE (init));
4564 : 58 : tree end = cp_build_binary_op (input_location, PLUS_EXPR, begin,
4565 : : nelts, tf_none);
4566 : 58 : begin = cp_build_compound_expr (init, begin, tf_none);
4567 : 58 : return build_constructor_va (init_list_type_node, 2,
4568 : 58 : NULL_TREE, begin, NULL_TREE, end);
4569 : : }
4570 : : }
4571 : :
4572 : : return NULL_TREE;
4573 : : }
4574 : :
4575 : : /* Returns the best overload candidate to perform the requested
4576 : : conversion. This function is used for three the overloading situations
4577 : : described in [over.match.copy], [over.match.conv], and [over.match.ref].
4578 : : If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
4579 : : per [dcl.init.ref], so we ignore temporary bindings. */
4580 : :
4581 : : static struct z_candidate *
4582 : 60333917 : build_user_type_conversion_1 (tree totype, tree expr, int flags,
4583 : : tsubst_flags_t complain)
4584 : : {
4585 : 60333917 : struct z_candidate *candidates, *cand;
4586 : 60333917 : tree fromtype;
4587 : 60333917 : tree ctors = NULL_TREE;
4588 : 60333917 : tree conv_fns = NULL_TREE;
4589 : 60333917 : conversion *conv = NULL;
4590 : 60333917 : tree first_arg = NULL_TREE;
4591 : 60333917 : vec<tree, va_gc> *args = NULL;
4592 : 60333917 : bool any_viable_p;
4593 : 60333917 : int convflags;
4594 : :
4595 : 60333917 : if (!expr)
4596 : : return NULL;
4597 : :
4598 : 60333911 : fromtype = TREE_TYPE (expr);
4599 : :
4600 : : /* We represent conversion within a hierarchy using RVALUE_CONV and
4601 : : BASE_CONV, as specified by [over.best.ics]; these become plain
4602 : : constructor calls, as specified in [dcl.init]. */
4603 : 60333911 : gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
4604 : : || !DERIVED_FROM_P (totype, fromtype));
4605 : :
4606 : 60333911 : if (CLASS_TYPE_P (totype))
4607 : : /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
4608 : : creating a garbage BASELINK; constructors can't be inherited. */
4609 : 35546590 : ctors = get_class_binding (totype, complete_ctor_identifier);
4610 : :
4611 : 60333911 : tree to_nonref = non_reference (totype);
4612 : 60333911 : if (MAYBE_CLASS_TYPE_P (fromtype))
4613 : : {
4614 : 38971502 : if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
4615 : 38942017 : (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
4616 : 30838280 : && DERIVED_FROM_P (to_nonref, fromtype)))
4617 : : {
4618 : : /* [class.conv.fct] A conversion function is never used to
4619 : : convert a (possibly cv-qualified) object to the (possibly
4620 : : cv-qualified) same object type (or a reference to it), to a
4621 : : (possibly cv-qualified) base class of that type (or a
4622 : : reference to it)... */
4623 : : }
4624 : : else
4625 : 38942014 : conv_fns = lookup_conversions (fromtype);
4626 : : }
4627 : :
4628 : 60333911 : candidates = 0;
4629 : 60333911 : flags |= LOOKUP_NO_CONVERSION;
4630 : 60333911 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4631 : 672129 : flags |= LOOKUP_NO_NARROWING;
4632 : : /* Prevent add_candidates from treating a non-strictly viable candidate
4633 : : as unviable. */
4634 : 60333911 : complain |= tf_conv;
4635 : :
4636 : : /* It's OK to bind a temporary for converting constructor arguments, but
4637 : : not in converting the return value of a conversion operator. */
4638 : 60333911 : convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
4639 : 60333911 : | (flags & LOOKUP_NO_NARROWING));
4640 : 60333911 : flags &= ~LOOKUP_NO_TEMP_BIND;
4641 : :
4642 : 60333911 : if (ctors)
4643 : : {
4644 : 35394937 : int ctorflags = flags;
4645 : :
4646 : 35394937 : first_arg = build_dummy_object (totype);
4647 : :
4648 : : /* We should never try to call the abstract or base constructor
4649 : : from here. */
4650 : 248715319 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
4651 : : && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
4652 : :
4653 : 35394937 : args = make_tree_vector_single (expr);
4654 : 35394937 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4655 : : {
4656 : : /* List-initialization. */
4657 : 672129 : add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
4658 : 672129 : false, TYPE_BINFO (totype), TYPE_BINFO (totype),
4659 : : ctorflags, &candidates, complain);
4660 : : }
4661 : : else
4662 : : {
4663 : 34722808 : add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
4664 : 34722808 : TYPE_BINFO (totype), TYPE_BINFO (totype),
4665 : : ctorflags, &candidates, complain);
4666 : : }
4667 : :
4668 : 203106207 : for (cand = candidates; cand; cand = cand->next)
4669 : : {
4670 : 167711270 : cand->second_conv = build_identity_conv (totype, NULL_TREE);
4671 : :
4672 : : /* If totype isn't a reference, and LOOKUP_ONLYCONVERTING is
4673 : : set, then this is copy-initialization. In that case, "The
4674 : : result of the call is then used to direct-initialize the
4675 : : object that is the destination of the copy-initialization."
4676 : : [dcl.init]
4677 : :
4678 : : We represent this in the conversion sequence with an
4679 : : rvalue conversion, which means a constructor call. */
4680 : 167711270 : if (!TYPE_REF_P (totype)
4681 : 167711270 : && cxx_dialect < cxx17
4682 : 397869 : && (flags & LOOKUP_ONLYCONVERTING)
4683 : 344582 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4684 : 110044 : cand->second_conv
4685 : 110044 : = build_conv (ck_rvalue, totype, cand->second_conv);
4686 : : }
4687 : : }
4688 : :
4689 : 60333911 : if (conv_fns)
4690 : : {
4691 : 14203868 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4692 : 0 : first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
4693 : : else
4694 : 60333911 : first_arg = expr;
4695 : : }
4696 : :
4697 : 76032574 : for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
4698 : : {
4699 : 15698663 : tree conversion_path = TREE_PURPOSE (conv_fns);
4700 : 15698663 : struct z_candidate *old_candidates;
4701 : :
4702 : : /* If LOOKUP_NO_CONVERSION, don't consider a conversion function that
4703 : : would need an addional user-defined conversion, i.e. if the return
4704 : : type differs in class-ness from the desired type. So we avoid
4705 : : considering operator bool when calling a copy constructor.
4706 : :
4707 : : This optimization avoids the failure in PR97600, and is allowed by
4708 : : [temp.inst]/9: "If the function selected by overload resolution can be
4709 : : determined without instantiating a class template definition, it is
4710 : : unspecified whether that instantiation actually takes place." */
4711 : 15698663 : tree convtype = non_reference (TREE_TYPE (conv_fns));
4712 : 21644904 : if ((flags & LOOKUP_NO_CONVERSION)
4713 : 15698663 : && !WILDCARD_TYPE_P (convtype)
4714 : 30519368 : && (CLASS_TYPE_P (to_nonref)
4715 : 15259684 : != CLASS_TYPE_P (convtype)))
4716 : 5946241 : continue;
4717 : :
4718 : : /* If we are called to convert to a reference type, we are trying to
4719 : : find a direct binding, so don't even consider temporaries. If
4720 : : we don't find a direct binding, the caller will try again to
4721 : : look for a temporary binding. */
4722 : 9752422 : if (TYPE_REF_P (totype))
4723 : 2416412 : convflags |= LOOKUP_NO_TEMP_BIND;
4724 : :
4725 : 9752422 : old_candidates = candidates;
4726 : 9752422 : add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
4727 : : NULL_TREE, false,
4728 : 9752422 : conversion_path, TYPE_BINFO (fromtype),
4729 : : flags, &candidates, complain);
4730 : :
4731 : 19504839 : for (cand = candidates; cand != old_candidates; cand = cand->next)
4732 : : {
4733 : 9752417 : if (cand->viable == 0)
4734 : : /* Already rejected, don't change to -1. */
4735 : 2244151 : continue;
4736 : :
4737 : 7508266 : tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
4738 : 7508266 : conversion *ics
4739 : 7508266 : = implicit_conversion (totype,
4740 : : rettype,
4741 : : 0,
4742 : : /*c_cast_p=*/false, convflags,
4743 : : complain);
4744 : :
4745 : : /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
4746 : : copy-initialization. In that case, "The result of the
4747 : : call is then used to direct-initialize the object that is
4748 : : the destination of the copy-initialization." [dcl.init]
4749 : :
4750 : : We represent this in the conversion sequence with an
4751 : : rvalue conversion, which means a constructor call. But
4752 : : don't add a second rvalue conversion if there's already
4753 : : one there. Which there really shouldn't be, but it's
4754 : : harmless since we'd add it here anyway. */
4755 : 3276170 : if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
4756 : 8048353 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4757 : 218845 : ics = build_conv (ck_rvalue, totype, ics);
4758 : :
4759 : 7508266 : cand->second_conv = ics;
4760 : :
4761 : 7508266 : if (!ics)
4762 : : {
4763 : 4232096 : cand->viable = 0;
4764 : 8462653 : cand->reason = arg_conversion_rejection (NULL_TREE, -2,
4765 : : rettype, totype,
4766 : 4232096 : EXPR_LOCATION (expr));
4767 : : }
4768 : 13048 : else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p
4769 : : /* Limit this to non-templates for now (PR90546). */
4770 : 1086 : && !cand->template_decl
4771 : 3277251 : && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE)
4772 : : {
4773 : : /* If we are called to convert to a reference type, we are trying
4774 : : to find a direct binding per [over.match.ref], so rvaluedness
4775 : : must match for non-functions. */
4776 : 1075 : cand->viable = 0;
4777 : : }
4778 : 3275095 : else if (DECL_NONCONVERTING_P (cand->fn)
4779 : 3275095 : && ics->rank > cr_exact)
4780 : : {
4781 : : /* 13.3.1.5: For direct-initialization, those explicit
4782 : : conversion functions that are not hidden within S and
4783 : : yield type T or a type that can be converted to type T
4784 : : with a qualification conversion (4.4) are also candidate
4785 : : functions. */
4786 : : /* 13.3.1.6 doesn't have a parallel restriction, but it should;
4787 : : I've raised this issue with the committee. --jason 9/2011 */
4788 : 2467 : cand->viable = -1;
4789 : 2467 : cand->reason = explicit_conversion_rejection (rettype, totype);
4790 : : }
4791 : 3272628 : else if (cand->viable == 1 && ics->bad_p)
4792 : : {
4793 : 1628 : cand->viable = -1;
4794 : 1628 : cand->reason
4795 : 3256 : = bad_arg_conversion_rejection (NULL_TREE, -2,
4796 : : rettype, totype,
4797 : 1628 : EXPR_LOCATION (expr));
4798 : : }
4799 : 3271000 : else if (primary_template_specialization_p (cand->fn)
4800 : 3271000 : && ics->rank > cr_exact)
4801 : : {
4802 : : /* 13.3.3.1.2: If the user-defined conversion is specified by
4803 : : a specialization of a conversion function template, the
4804 : : second standard conversion sequence shall have exact match
4805 : : rank. */
4806 : 21 : cand->viable = -1;
4807 : 21 : cand->reason = template_conversion_rejection (rettype, totype);
4808 : : }
4809 : : }
4810 : : }
4811 : :
4812 : 60333911 : candidates = splice_viable (candidates, false, &any_viable_p);
4813 : 60333911 : if (!any_viable_p)
4814 : : {
4815 : 50862177 : if (args)
4816 : 28646164 : release_tree_vector (args);
4817 : 50862177 : return NULL;
4818 : : }
4819 : :
4820 : 9471734 : cand = tourney (candidates, complain);
4821 : 9471734 : if (cand == NULL)
4822 : : {
4823 : 1335 : if (complain & tf_error)
4824 : : {
4825 : 71 : auto_diagnostic_group d;
4826 : 74 : error_at (cp_expr_loc_or_input_loc (expr),
4827 : : "conversion from %qH to %qI is ambiguous",
4828 : : fromtype, totype);
4829 : 71 : print_z_candidates (location_of (expr), candidates);
4830 : 71 : }
4831 : :
4832 : 1335 : cand = candidates; /* any one will do */
4833 : 1335 : cand->second_conv = build_ambiguous_conv (totype, expr);
4834 : 1335 : cand->second_conv->user_conv_p = true;
4835 : 2670 : if (!any_strictly_viable (candidates))
4836 : 12 : cand->second_conv->bad_p = true;
4837 : 1335 : if (flags & LOOKUP_ONLYCONVERTING)
4838 : 1265 : cand->second_conv->need_temporary_p = true;
4839 : : /* If there are viable candidates, don't set ICS_BAD_FLAG; an
4840 : : ambiguous conversion is no worse than another user-defined
4841 : : conversion. */
4842 : :
4843 : 1335 : return cand;
4844 : : }
4845 : :
4846 : : /* Maybe pass { } as iterators instead of an initializer_list. */
4847 : 9470399 : if (tree iters = maybe_init_list_as_range (cand->fn, expr))
4848 : 116 : if (z_candidate *cand2
4849 : 58 : = build_user_type_conversion_1 (totype, iters, flags, tf_none))
4850 : 55 : if (cand2->viable == 1 && !is_list_ctor (cand2->fn))
4851 : : {
4852 : : cand = cand2;
4853 : : expr = iters;
4854 : : }
4855 : :
4856 : 9470399 : tree convtype;
4857 : 18940798 : if (!DECL_CONSTRUCTOR_P (cand->fn))
4858 : 3263005 : convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
4859 : 6207394 : else if (cand->second_conv->kind == ck_rvalue)
4860 : : /* DR 5: [in the first step of copy-initialization]...if the function
4861 : : is a constructor, the call initializes a temporary of the
4862 : : cv-unqualified version of the destination type. */
4863 : 11273 : convtype = cv_unqualified (totype);
4864 : : else
4865 : : convtype = totype;
4866 : : /* Build the user conversion sequence. */
4867 : 9470399 : conv = build_conv
4868 : 9470399 : (ck_user,
4869 : : convtype,
4870 : 9470399 : build_identity_conv (TREE_TYPE (expr), expr));
4871 : 9470399 : conv->cand = cand;
4872 : 9470399 : if (cand->viable == -1)
4873 : 20549 : conv->bad_p = true;
4874 : :
4875 : : /* Remember that this was a list-initialization. */
4876 : 9470399 : if (flags & LOOKUP_NO_NARROWING)
4877 : 1654774 : conv->check_narrowing = true;
4878 : :
4879 : : /* Combine it with the second conversion sequence. */
4880 : 9470399 : cand->second_conv = merge_conversion_sequences (conv,
4881 : : cand->second_conv);
4882 : :
4883 : 9470399 : return cand;
4884 : : }
4885 : :
4886 : : /* Wrapper for above. */
4887 : :
4888 : : tree
4889 : 23442 : build_user_type_conversion (tree totype, tree expr, int flags,
4890 : : tsubst_flags_t complain)
4891 : : {
4892 : 23442 : struct z_candidate *cand;
4893 : 23442 : tree ret;
4894 : :
4895 : 23442 : auto_cond_timevar tv (TV_OVERLOAD);
4896 : :
4897 : 23442 : conversion_obstack_sentinel cos;
4898 : :
4899 : 23442 : cand = build_user_type_conversion_1 (totype, expr, flags, complain);
4900 : :
4901 : 23442 : if (cand)
4902 : : {
4903 : 23239 : if (cand->second_conv->kind == ck_ambig)
4904 : 65 : ret = error_mark_node;
4905 : : else
4906 : : {
4907 : 23174 : expr = convert_like (cand->second_conv, expr, complain);
4908 : 23174 : ret = convert_from_reference (expr);
4909 : : }
4910 : : }
4911 : : else
4912 : : ret = NULL_TREE;
4913 : :
4914 : 46884 : return ret;
4915 : 23442 : }
4916 : :
4917 : : /* Give a helpful diagnostic when implicit_conversion fails. */
4918 : :
4919 : : static void
4920 : 654 : implicit_conversion_error (location_t loc, tree type, tree expr)
4921 : : {
4922 : 654 : tsubst_flags_t complain = tf_warning_or_error;
4923 : :
4924 : : /* If expr has unknown type, then it is an overloaded function.
4925 : : Call instantiate_type to get good error messages. */
4926 : 654 : if (TREE_TYPE (expr) == unknown_type_node)
4927 : 66 : instantiate_type (type, expr, complain);
4928 : 588 : else if (invalid_nonstatic_memfn_p (loc, expr, complain))
4929 : : /* We gave an error. */;
4930 : 62 : else if (BRACE_ENCLOSED_INITIALIZER_P (expr)
4931 : 59 : && CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
4932 : 601 : && !CP_AGGREGATE_TYPE_P (type))
4933 : 18 : error_at (loc, "designated initializers cannot be used with a "
4934 : : "non-aggregate type %qT", type);
4935 : 565 : else if (is_stub_object (expr))
4936 : : /* The expression is generated by a trait check, we don't have
4937 : : a useful location to highlight the label. */
4938 : 13 : error_at (loc, "could not convert %qH to %qI",
4939 : 13 : TREE_TYPE (expr), type);
4940 : : else
4941 : : {
4942 : 552 : range_label_for_type_mismatch label (TREE_TYPE (expr), type);
4943 : 552 : gcc_rich_location rich_loc (loc, &label,
4944 : 552 : highlight_colors::percent_h);
4945 : 552 : error_at (&rich_loc, "could not convert %qE from %qH to %qI",
4946 : 552 : expr, TREE_TYPE (expr), type);
4947 : 552 : }
4948 : 654 : }
4949 : :
4950 : : /* Worker for build_converted_constant_expr. */
4951 : :
4952 : : static tree
4953 : 260162144 : build_converted_constant_expr_internal (tree type, tree expr,
4954 : : int flags, tsubst_flags_t complain)
4955 : : {
4956 : 260162144 : conversion *conv;
4957 : 260162144 : tree t;
4958 : 260162144 : location_t loc = cp_expr_loc_or_input_loc (expr);
4959 : :
4960 : 260162144 : if (error_operand_p (expr))
4961 : 49 : return error_mark_node;
4962 : :
4963 : 260162095 : conversion_obstack_sentinel cos;
4964 : :
4965 : 260162095 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4966 : : /*c_cast_p=*/false, flags, complain);
4967 : :
4968 : : /* A converted constant expression of type T is an expression, implicitly
4969 : : converted to type T, where the converted expression is a constant
4970 : : expression and the implicit conversion sequence contains only
4971 : :
4972 : : * user-defined conversions,
4973 : : * lvalue-to-rvalue conversions (7.1),
4974 : : * array-to-pointer conversions (7.2),
4975 : : * function-to-pointer conversions (7.3),
4976 : : * qualification conversions (7.5),
4977 : : * integral promotions (7.6),
4978 : : * integral conversions (7.8) other than narrowing conversions (11.6.4),
4979 : : * null pointer conversions (7.11) from std::nullptr_t,
4980 : : * null member pointer conversions (7.12) from std::nullptr_t, and
4981 : : * function pointer conversions (7.13),
4982 : :
4983 : : and where the reference binding (if any) binds directly. */
4984 : :
4985 : 260162095 : for (conversion *c = conv;
4986 : 296956572 : c && c->kind != ck_identity;
4987 : 36794477 : c = next_conversion (c))
4988 : : {
4989 : 36794477 : switch (c->kind)
4990 : : {
4991 : : /* A conversion function is OK. If it isn't constexpr, we'll
4992 : : complain later that the argument isn't constant. */
4993 : : case ck_user:
4994 : : /* List-initialization is OK. */
4995 : : case ck_aggr:
4996 : : /* The lvalue-to-rvalue conversion is OK. */
4997 : : case ck_rvalue:
4998 : : /* Array-to-pointer and function-to-pointer. */
4999 : : case ck_lvalue:
5000 : : /* Function pointer conversions. */
5001 : : case ck_fnptr:
5002 : : /* Qualification conversions. */
5003 : : case ck_qual:
5004 : : break;
5005 : :
5006 : 361 : case ck_ref_bind:
5007 : 361 : if (c->need_temporary_p)
5008 : : {
5009 : 0 : if (complain & tf_error)
5010 : 0 : error_at (loc, "initializing %qH with %qI in converted "
5011 : : "constant expression does not bind directly",
5012 : 0 : type, next_conversion (c)->type);
5013 : : conv = NULL;
5014 : : }
5015 : : break;
5016 : :
5017 : 7126666 : case ck_base:
5018 : 7126666 : case ck_pmem:
5019 : 7126666 : case ck_ptr:
5020 : 7126666 : case ck_std:
5021 : 7126666 : t = next_conversion (c)->type;
5022 : 7126666 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
5023 : 7126596 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5024 : : /* Integral promotion or conversion. */
5025 : : break;
5026 : 92 : if (NULLPTR_TYPE_P (t))
5027 : : /* Conversion from nullptr to pointer or pointer-to-member. */
5028 : : break;
5029 : :
5030 : 92 : if (complain & tf_error)
5031 : 72 : error_at (loc, "conversion from %qH to %qI in a "
5032 : : "converted constant expression", t, type);
5033 : : /* fall through. */
5034 : :
5035 : : default:
5036 : : conv = NULL;
5037 : : break;
5038 : : }
5039 : : }
5040 : :
5041 : : /* Avoid confusing convert_nontype_argument by introducing
5042 : : a redundant conversion to the same reference type. */
5043 : 260161859 : if (conv && conv->kind == ck_ref_bind
5044 : 260162456 : && REFERENCE_REF_P (expr))
5045 : : {
5046 : 137 : tree ref = TREE_OPERAND (expr, 0);
5047 : 137 : if (same_type_p (type, TREE_TYPE (ref)))
5048 : : return ref;
5049 : : }
5050 : :
5051 : 260161980 : if (conv)
5052 : : {
5053 : : /* Don't copy a class in a template. */
5054 : 9133 : if (CLASS_TYPE_P (type) && conv->kind == ck_rvalue
5055 : 260169690 : && processing_template_decl)
5056 : 51 : conv = next_conversion (conv);
5057 : :
5058 : : /* Issuing conversion warnings for value-dependent expressions is
5059 : : likely too noisy. */
5060 : 260161744 : warning_sentinel w (warn_conversion);
5061 : 260161744 : conv->check_narrowing = true;
5062 : 260161744 : conv->check_narrowing_const_only = true;
5063 : 260161744 : expr = convert_like (conv, expr, complain);
5064 : 260161744 : }
5065 : : else
5066 : : {
5067 : 236 : if (complain & tf_error)
5068 : 184 : implicit_conversion_error (loc, type, expr);
5069 : 236 : expr = error_mark_node;
5070 : : }
5071 : :
5072 : : return expr;
5073 : 260162095 : }
5074 : :
5075 : : /* Subroutine of convert_nontype_argument.
5076 : :
5077 : : EXPR is an expression used in a context that requires a converted
5078 : : constant-expression, such as a template non-type parameter. Do any
5079 : : necessary conversions (that are permitted for converted
5080 : : constant-expressions) to convert it to the desired type.
5081 : :
5082 : : This function doesn't consider explicit conversion functions. If
5083 : : you mean to use "a contextually converted constant expression of type
5084 : : bool", use build_converted_constant_bool_expr.
5085 : :
5086 : : If conversion is successful, returns the converted expression;
5087 : : otherwise, returns error_mark_node. */
5088 : :
5089 : : tree
5090 : 146550152 : build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
5091 : : {
5092 : 146550152 : return build_converted_constant_expr_internal (type, expr, LOOKUP_IMPLICIT,
5093 : 146550152 : complain);
5094 : : }
5095 : :
5096 : : /* Used to create "a contextually converted constant expression of type
5097 : : bool". This differs from build_converted_constant_expr in that it
5098 : : also considers explicit conversion functions. */
5099 : :
5100 : : tree
5101 : 113611992 : build_converted_constant_bool_expr (tree expr, tsubst_flags_t complain)
5102 : : {
5103 : 113611992 : return build_converted_constant_expr_internal (boolean_type_node, expr,
5104 : 113611992 : LOOKUP_NORMAL, complain);
5105 : : }
5106 : :
5107 : : /* Do any initial processing on the arguments to a function call. */
5108 : :
5109 : : vec<tree, va_gc> *
5110 : 162352870 : resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
5111 : : {
5112 : 162352870 : unsigned int ix;
5113 : 162352870 : tree arg;
5114 : :
5115 : 299184186 : FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5116 : : {
5117 : 136831916 : if (error_operand_p (arg))
5118 : : return NULL;
5119 : 136831416 : else if (VOID_TYPE_P (TREE_TYPE (arg)))
5120 : : {
5121 : 73 : if (complain & tf_error)
5122 : 12 : error_at (cp_expr_loc_or_input_loc (arg),
5123 : : "invalid use of void expression");
5124 : 73 : return NULL;
5125 : : }
5126 : 136831343 : else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg), arg, complain))
5127 : : return NULL;
5128 : :
5129 : : /* Force auto deduction now. Omit tf_warning to avoid redundant
5130 : : deprecated warning on deprecated-14.C. */
5131 : 136831316 : if (!mark_single_function (arg, complain & ~tf_warning))
5132 : : return NULL;
5133 : : }
5134 : : return args;
5135 : : }
5136 : :
5137 : : /* Perform overload resolution on FN, which is called with the ARGS.
5138 : :
5139 : : Return the candidate function selected by overload resolution, or
5140 : : NULL if the event that overload resolution failed. In the case
5141 : : that overload resolution fails, *CANDIDATES will be the set of
5142 : : candidates considered, and ANY_VIABLE_P will be set to true or
5143 : : false to indicate whether or not any of the candidates were
5144 : : viable.
5145 : :
5146 : : The ARGS should already have gone through RESOLVE_ARGS before this
5147 : : function is called. */
5148 : :
5149 : : static struct z_candidate *
5150 : 77180655 : perform_overload_resolution (tree fn,
5151 : : const vec<tree, va_gc> *args,
5152 : : struct z_candidate **candidates,
5153 : : bool *any_viable_p, tsubst_flags_t complain)
5154 : : {
5155 : 77180655 : struct z_candidate *cand;
5156 : 77180655 : tree explicit_targs;
5157 : 77180655 : int template_only;
5158 : :
5159 : 77180655 : auto_cond_timevar tv (TV_OVERLOAD);
5160 : :
5161 : 77180655 : explicit_targs = NULL_TREE;
5162 : 77180655 : template_only = 0;
5163 : :
5164 : 77180655 : *candidates = NULL;
5165 : 77180655 : *any_viable_p = true;
5166 : :
5167 : : /* Check FN. */
5168 : 77180655 : gcc_assert (OVL_P (fn) || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
5169 : :
5170 : 77180655 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5171 : : {
5172 : 22312584 : explicit_targs = TREE_OPERAND (fn, 1);
5173 : 22312584 : fn = TREE_OPERAND (fn, 0);
5174 : 22312584 : template_only = 1;
5175 : : }
5176 : :
5177 : : /* Add the various candidate functions. */
5178 : 77180655 : add_candidates (fn, NULL_TREE, args, NULL_TREE,
5179 : : explicit_targs, template_only,
5180 : : /*conversion_path=*/NULL_TREE,
5181 : : /*access_path=*/NULL_TREE,
5182 : : LOOKUP_NORMAL,
5183 : : candidates, complain);
5184 : :
5185 : 77167137 : *candidates = splice_viable (*candidates, false, any_viable_p);
5186 : 77167137 : if (*any_viable_p)
5187 : 77072763 : cand = tourney (*candidates, complain);
5188 : : else
5189 : : cand = NULL;
5190 : :
5191 : 154334274 : return cand;
5192 : 77167137 : }
5193 : :
5194 : : /* Print an error message about being unable to build a call to FN with
5195 : : ARGS. ANY_VIABLE_P indicates whether any candidate functions could
5196 : : be located; CANDIDATES is a possibly empty list of such
5197 : : functions. */
5198 : :
5199 : : static void
5200 : 2828 : print_error_for_call_failure (tree fn, const vec<tree, va_gc> *args,
5201 : : struct z_candidate *candidates)
5202 : : {
5203 : 2828 : tree targs = NULL_TREE;
5204 : 2828 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5205 : : {
5206 : 470 : targs = TREE_OPERAND (fn, 1);
5207 : 470 : fn = TREE_OPERAND (fn, 0);
5208 : : }
5209 : 5656 : tree name = OVL_NAME (fn);
5210 : 2828 : location_t loc = location_of (name);
5211 : 2828 : if (targs)
5212 : 470 : name = lookup_template_function (name, targs);
5213 : :
5214 : 2828 : auto_diagnostic_group d;
5215 : 5656 : if (!any_strictly_viable (candidates))
5216 : 2359 : error_at (loc, "no matching function for call to %<%D(%A)%>",
5217 : : name, build_tree_list_vec (args));
5218 : : else
5219 : 469 : error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
5220 : : name, build_tree_list_vec (args));
5221 : 2828 : if (candidates)
5222 : 2828 : print_z_candidates (loc, candidates);
5223 : 2828 : }
5224 : :
5225 : : /* Perform overload resolution on the set of deduction guides DGUIDES
5226 : : using ARGS. Returns the selected deduction guide, or error_mark_node
5227 : : if overload resolution fails. */
5228 : :
5229 : : tree
5230 : 24243 : perform_dguide_overload_resolution (tree dguides, const vec<tree, va_gc> *args,
5231 : : tsubst_flags_t complain)
5232 : : {
5233 : 24243 : z_candidate *candidates;
5234 : 24243 : bool any_viable_p;
5235 : 24243 : tree result;
5236 : :
5237 : 48486 : gcc_assert (deduction_guide_p (OVL_FIRST (dguides)));
5238 : :
5239 : 24243 : conversion_obstack_sentinel cos;
5240 : :
5241 : 24243 : z_candidate *cand = perform_overload_resolution (dguides, args, &candidates,
5242 : : &any_viable_p, complain);
5243 : 24243 : if (!cand)
5244 : : {
5245 : 877 : if (complain & tf_error)
5246 : 188 : print_error_for_call_failure (dguides, args, candidates);
5247 : 877 : result = error_mark_node;
5248 : : }
5249 : : else
5250 : 23366 : result = cand->fn;
5251 : :
5252 : 48486 : return result;
5253 : 24243 : }
5254 : :
5255 : : /* Return an expression for a call to FN (a namespace-scope function,
5256 : : or a static member function) with the ARGS. This may change
5257 : : ARGS. */
5258 : :
5259 : : tree
5260 : 76638050 : build_new_function_call (tree fn, vec<tree, va_gc> **args,
5261 : : tsubst_flags_t complain)
5262 : : {
5263 : 76638050 : struct z_candidate *candidates, *cand;
5264 : 76638050 : bool any_viable_p;
5265 : 76638050 : tree result;
5266 : :
5267 : 76638050 : if (args != NULL && *args != NULL)
5268 : : {
5269 : 76638050 : *args = resolve_args (*args, complain);
5270 : 76638050 : if (*args == NULL)
5271 : 304 : return error_mark_node;
5272 : : }
5273 : :
5274 : 76637746 : if (flag_tm)
5275 : 2161 : tm_malloc_replacement (fn);
5276 : :
5277 : 76637746 : conversion_obstack_sentinel cos;
5278 : :
5279 : 76637746 : cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
5280 : : complain);
5281 : :
5282 : 76624228 : if (!cand)
5283 : : {
5284 : 118924 : if (complain & tf_error)
5285 : : {
5286 : : // If there is a single (non-viable) function candidate,
5287 : : // let the error be diagnosed by cp_build_function_call_vec.
5288 : 3054 : if (!any_viable_p && candidates && ! candidates->next
5289 : 1299 : && TREE_CODE (candidates->fn) == FUNCTION_DECL
5290 : : /* A template-id callee consisting of a single (ignored)
5291 : : non-template candidate needs to be diagnosed the
5292 : : ordinary way. */
5293 : 431 : && (TREE_CODE (fn) != TEMPLATE_ID_EXPR
5294 : 29 : || candidates->template_decl))
5295 : 423 : return cp_build_function_call_vec (candidates->fn, args, complain);
5296 : :
5297 : : // Otherwise, emit notes for non-viable candidates.
5298 : 2631 : print_error_for_call_failure (fn, *args, candidates);
5299 : : }
5300 : 118501 : result = error_mark_node;
5301 : : }
5302 : : else
5303 : : {
5304 : 76505304 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5305 : : }
5306 : :
5307 : 76623805 : if (flag_coroutines
5308 : 43884115 : && result
5309 : 43884115 : && TREE_CODE (result) == CALL_EXPR
5310 : 100973400 : && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result), 0))
5311 : 24349595 : == BUILT_IN_NORMAL)
5312 : 3026504 : result = coro_validate_builtin_call (result);
5313 : :
5314 : : return result;
5315 : 76624228 : }
5316 : :
5317 : : /* Build a call to a global operator new. FNNAME is the name of the
5318 : : operator (either "operator new" or "operator new[]") and ARGS are
5319 : : the arguments provided. This may change ARGS. *SIZE points to the
5320 : : total number of bytes required by the allocation, and is updated if
5321 : : that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
5322 : : be used. If this function determines that no cookie should be
5323 : : used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
5324 : : is not NULL_TREE, it is evaluated before calculating the final
5325 : : array size, and if it fails, the array size is replaced with
5326 : : (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
5327 : : is non-NULL, it will be set, upon return, to the allocation
5328 : : function called. */
5329 : :
5330 : : tree
5331 : 518651 : build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
5332 : : tree *size, tree *cookie_size,
5333 : : tree align_arg, tree size_check,
5334 : : tree *fn, tsubst_flags_t complain)
5335 : : {
5336 : 518651 : tree original_size = *size;
5337 : 518651 : tree fns;
5338 : 518651 : struct z_candidate *candidates;
5339 : 518651 : struct z_candidate *cand = NULL;
5340 : 518651 : bool any_viable_p;
5341 : :
5342 : 518651 : if (fn)
5343 : 517239 : *fn = NULL_TREE;
5344 : : /* Set to (size_t)-1 if the size check fails. */
5345 : 518651 : if (size_check != NULL_TREE)
5346 : : {
5347 : 13978 : tree errval = TYPE_MAX_VALUE (sizetype);
5348 : 13978 : if (cxx_dialect >= cxx11 && flag_exceptions)
5349 : 13635 : errval = throw_bad_array_new_length ();
5350 : 13978 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5351 : : original_size, errval);
5352 : : }
5353 : 518651 : vec_safe_insert (*args, 0, *size);
5354 : 518651 : *args = resolve_args (*args, complain);
5355 : 518651 : if (*args == NULL)
5356 : 0 : return error_mark_node;
5357 : :
5358 : 518651 : conversion_obstack_sentinel cos;
5359 : :
5360 : : /* Based on:
5361 : :
5362 : : [expr.new]
5363 : :
5364 : : If this lookup fails to find the name, or if the allocated type
5365 : : is not a class type, the allocation function's name is looked
5366 : : up in the global scope.
5367 : :
5368 : : we disregard block-scope declarations of "operator new". */
5369 : 518651 : fns = lookup_qualified_name (global_namespace, fnname);
5370 : :
5371 : 518651 : if (align_arg)
5372 : : {
5373 : 2393 : vec<tree, va_gc>* align_args
5374 : 2393 : = vec_copy_and_insert (*args, align_arg, 1);
5375 : 2393 : cand = perform_overload_resolution (fns, align_args, &candidates,
5376 : : &any_viable_p, tf_none);
5377 : 2393 : if (cand)
5378 : 2378 : *args = align_args;
5379 : : /* If no aligned allocation function matches, try again without the
5380 : : alignment. */
5381 : : }
5382 : :
5383 : : /* Figure out what function is being called. */
5384 : 2378 : if (!cand)
5385 : 516273 : cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
5386 : : complain);
5387 : :
5388 : : /* If no suitable function could be found, issue an error message
5389 : : and give up. */
5390 : 518651 : if (!cand)
5391 : : {
5392 : 9 : if (complain & tf_error)
5393 : 9 : print_error_for_call_failure (fns, *args, candidates);
5394 : 9 : return error_mark_node;
5395 : : }
5396 : :
5397 : : /* If a cookie is required, add some extra space. Whether
5398 : : or not a cookie is required cannot be determined until
5399 : : after we know which function was called. */
5400 : 518642 : if (*cookie_size)
5401 : : {
5402 : 265 : bool use_cookie = true;
5403 : 265 : tree arg_types;
5404 : :
5405 : 265 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5406 : : /* Skip the size_t parameter. */
5407 : 265 : arg_types = TREE_CHAIN (arg_types);
5408 : : /* Check the remaining parameters (if any). */
5409 : 265 : if (arg_types
5410 : 265 : && TREE_CHAIN (arg_types) == void_list_node
5411 : 325 : && same_type_p (TREE_VALUE (arg_types),
5412 : : ptr_type_node))
5413 : 45 : use_cookie = false;
5414 : : /* If we need a cookie, adjust the number of bytes allocated. */
5415 : 265 : if (use_cookie)
5416 : : {
5417 : : /* Update the total size. */
5418 : 220 : *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
5419 : 220 : if (size_check)
5420 : : {
5421 : : /* Set to (size_t)-1 if the size check fails. */
5422 : 47 : gcc_assert (size_check != NULL_TREE);
5423 : 47 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5424 : : *size, TYPE_MAX_VALUE (sizetype));
5425 : : }
5426 : : /* Update the argument list to reflect the adjusted size. */
5427 : 220 : (**args)[0] = *size;
5428 : : }
5429 : : else
5430 : 45 : *cookie_size = NULL_TREE;
5431 : : }
5432 : :
5433 : : /* Tell our caller which function we decided to call. */
5434 : 518642 : if (fn)
5435 : 517230 : *fn = cand->fn;
5436 : :
5437 : : /* Build the CALL_EXPR. */
5438 : 518642 : tree ret = build_over_call (cand, LOOKUP_NORMAL, complain);
5439 : :
5440 : : /* Set this flag for all callers of this function. In addition to
5441 : : new-expressions, this is called for allocating coroutine state; treat
5442 : : that as an implicit new-expression. */
5443 : 518642 : tree call = extract_call_expr (ret);
5444 : 518642 : if (TREE_CODE (call) == CALL_EXPR)
5445 : 518642 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
5446 : :
5447 : : return ret;
5448 : 518651 : }
5449 : :
5450 : : /* Evaluate side-effects from OBJ before evaluating call
5451 : : to FN in RESULT expression.
5452 : : This is for expressions of the form `obj->fn(...)'
5453 : : where `fn' turns out to be a static member function and
5454 : : `obj' needs to be evaluated. `fn' could be also static operator[]
5455 : : or static operator(), in which cases the source expression
5456 : : would be `obj[...]' or `obj(...)'. */
5457 : :
5458 : : tree
5459 : 64332999 : keep_unused_object_arg (tree result, tree obj, tree fn)
5460 : : {
5461 : 64332999 : if (result == NULL_TREE
5462 : 64332999 : || result == error_mark_node
5463 : 63579537 : || DECL_OBJECT_MEMBER_FUNCTION_P (fn)
5464 : 66206248 : || !TREE_SIDE_EFFECTS (obj))
5465 : : return result;
5466 : :
5467 : : /* But avoid the implicit lvalue-rvalue conversion when `obj' is
5468 : : volatile. */
5469 : 61315 : tree a = obj;
5470 : 61315 : if (TREE_THIS_VOLATILE (a))
5471 : 59568 : a = build_this (a);
5472 : 61315 : if (TREE_SIDE_EFFECTS (a))
5473 : 1756 : return cp_build_compound_expr (a, result, tf_error);
5474 : : return result;
5475 : : }
5476 : :
5477 : : /* Build a new call to operator(). This may change ARGS. */
5478 : :
5479 : : tree
5480 : 1882060 : build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
5481 : : {
5482 : 1882060 : struct z_candidate *candidates = 0, *cand;
5483 : 1882060 : tree fns, convs, first_mem_arg = NULL_TREE;
5484 : 1882060 : bool any_viable_p;
5485 : 1882060 : tree result = NULL_TREE;
5486 : :
5487 : 1882060 : auto_cond_timevar tv (TV_OVERLOAD);
5488 : :
5489 : 1882060 : obj = mark_lvalue_use (obj);
5490 : :
5491 : 1882060 : if (error_operand_p (obj))
5492 : 0 : return error_mark_node;
5493 : :
5494 : 1882060 : tree type = TREE_TYPE (obj);
5495 : :
5496 : 1882060 : obj = prep_operand (obj);
5497 : :
5498 : 1882060 : if (TYPE_PTRMEMFUNC_P (type))
5499 : : {
5500 : 0 : if (complain & tf_error)
5501 : : /* It's no good looking for an overloaded operator() on a
5502 : : pointer-to-member-function. */
5503 : 0 : error ("pointer-to-member function %qE cannot be called without "
5504 : : "an object; consider using %<.*%> or %<->*%>", obj);
5505 : 0 : return error_mark_node;
5506 : : }
5507 : :
5508 : 1882060 : if (TYPE_BINFO (type))
5509 : : {
5510 : 1882042 : fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain);
5511 : 1882042 : if (fns == error_mark_node)
5512 : : return error_mark_node;
5513 : : }
5514 : : else
5515 : : fns = NULL_TREE;
5516 : :
5517 : 1882050 : if (args != NULL && *args != NULL)
5518 : : {
5519 : 1882050 : *args = resolve_args (*args, complain);
5520 : 1882050 : if (*args == NULL)
5521 : 162 : return error_mark_node;
5522 : : }
5523 : :
5524 : 1881888 : conversion_obstack_sentinel cos;
5525 : :
5526 : 1881888 : if (fns)
5527 : : {
5528 : 1880426 : first_mem_arg = obj;
5529 : :
5530 : 1880426 : add_candidates (BASELINK_FUNCTIONS (fns),
5531 : : first_mem_arg, *args, NULL_TREE,
5532 : : NULL_TREE, false,
5533 : 1880426 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
5534 : : LOOKUP_NORMAL, &candidates, complain);
5535 : : }
5536 : :
5537 : 1881888 : bool any_call_ops = candidates != nullptr;
5538 : :
5539 : 1881888 : convs = lookup_conversions (type);
5540 : :
5541 : 1961721 : for (; convs; convs = TREE_CHAIN (convs))
5542 : : {
5543 : 79833 : tree totype = TREE_TYPE (convs);
5544 : :
5545 : 66829 : if (TYPE_PTRFN_P (totype)
5546 : 13007 : || TYPE_REFFN_P (totype)
5547 : 92796 : || (TYPE_REF_P (totype)
5548 : 932 : && TYPE_PTRFN_P (TREE_TYPE (totype))))
5549 : 133894 : for (tree fn : ovl_range (TREE_VALUE (convs)))
5550 : : {
5551 : 66947 : if (DECL_NONCONVERTING_P (fn))
5552 : 3 : continue;
5553 : :
5554 : 66944 : if (TREE_CODE (fn) == TEMPLATE_DECL)
5555 : : {
5556 : : /* Making this work broke PR 71117 and 85118, so until the
5557 : : committee resolves core issue 2189, let's disable this
5558 : : candidate if there are any call operators. */
5559 : 17831 : if (any_call_ops)
5560 : 17819 : continue;
5561 : :
5562 : 12 : add_template_conv_candidate
5563 : 12 : (&candidates, fn, obj, *args, totype,
5564 : : /*access_path=*/NULL_TREE,
5565 : : /*conversion_path=*/NULL_TREE, complain);
5566 : : }
5567 : : else
5568 : 49113 : add_conv_candidate (&candidates, fn, obj,
5569 : : *args, /*conversion_path=*/NULL_TREE,
5570 : : /*access_path=*/NULL_TREE, complain);
5571 : : }
5572 : : }
5573 : :
5574 : : /* Be strict here because if we choose a bad conversion candidate, the
5575 : : errors we get won't mention the call context. */
5576 : 1881888 : candidates = splice_viable (candidates, true, &any_viable_p);
5577 : 1881888 : if (!any_viable_p)
5578 : : {
5579 : 40935 : if (complain & tf_error)
5580 : : {
5581 : 297 : auto_diagnostic_group d;
5582 : 297 : error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
5583 : : build_tree_list_vec (*args));
5584 : 297 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5585 : 297 : }
5586 : 40935 : result = error_mark_node;
5587 : : }
5588 : : else
5589 : : {
5590 : 1840953 : cand = tourney (candidates, complain);
5591 : 1840953 : if (cand == 0)
5592 : : {
5593 : 13 : if (complain & tf_error)
5594 : : {
5595 : 6 : auto_diagnostic_group d;
5596 : 12 : error ("call of %<(%T) (%A)%> is ambiguous",
5597 : 6 : TREE_TYPE (obj), build_tree_list_vec (*args));
5598 : 6 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5599 : 6 : }
5600 : 13 : result = error_mark_node;
5601 : : }
5602 : 1840940 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
5603 : 1840873 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
5604 : 3681813 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
5605 : : {
5606 : 1840873 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5607 : : /* In an expression of the form `a()' where cand->fn
5608 : : which is operator() turns out to be a static member function,
5609 : : `a' is none-the-less evaluated. */
5610 : 1840873 : result = keep_unused_object_arg (result, obj, cand->fn);
5611 : : }
5612 : : else
5613 : : {
5614 : 67 : if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5615 : 0 : obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
5616 : : -1, complain);
5617 : : else
5618 : : {
5619 : 67 : gcc_checking_assert (TYPE_P (cand->fn));
5620 : 67 : obj = convert_like (cand->convs[0], obj, complain);
5621 : : }
5622 : 67 : obj = convert_from_reference (obj);
5623 : 67 : result = cp_build_function_call_vec (obj, args, complain);
5624 : : }
5625 : : }
5626 : :
5627 : 1881888 : return result;
5628 : 1882060 : }
5629 : :
5630 : : /* Subroutine for preparing format strings suitable for the error
5631 : : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5632 : : and SUFFIX. */
5633 : :
5634 : : static const char *
5635 : 1464 : concat_op_error_string (bool match, const char *errmsg, const char *suffix)
5636 : : {
5637 : 1464 : return concat (match
5638 : : ? G_("ambiguous overload for ")
5639 : : : G_("no match for "),
5640 : 0 : errmsg, suffix, nullptr);
5641 : : }
5642 : :
5643 : : /* Called by op_error to prepare format strings suitable for the error
5644 : : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5645 : : and a suffix (controlled by NTYPES). */
5646 : :
5647 : : static const char *
5648 : 1443 : op_error_string (const char *errmsg, int ntypes, bool match)
5649 : : {
5650 : 1443 : const char *suffix;
5651 : 0 : if (ntypes == 3)
5652 : : suffix = G_(" (operand types are %qT, %qT, and %qT)");
5653 : 0 : else if (ntypes == 2)
5654 : : suffix = G_(" (operand types are %qT and %qT)");
5655 : : else
5656 : 0 : suffix = G_(" (operand type is %qT)");
5657 : 0 : return concat_op_error_string (match, errmsg, suffix);
5658 : : }
5659 : :
5660 : : /* Similar to op_error_string, but a special-case for binary ops that
5661 : : use %e for the args, rather than %qT. */
5662 : :
5663 : : static const char *
5664 : 21 : binop_error_string (const char *errmsg, bool match)
5665 : : {
5666 : 21 : return concat_op_error_string (match, errmsg,
5667 : 21 : G_(" (operand types are %e and %e)"));
5668 : : }
5669 : :
5670 : : static void
5671 : 1464 : op_error (const op_location_t &loc,
5672 : : enum tree_code code, enum tree_code code2,
5673 : : tree arg1, tree arg2, tree arg3, bool match)
5674 : : {
5675 : 1464 : bool assop = code == MODIFY_EXPR;
5676 : 1464 : const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
5677 : :
5678 : 1464 : switch (code)
5679 : : {
5680 : 0 : case COND_EXPR:
5681 : 0 : if (flag_diagnostics_show_caret)
5682 : 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
5683 : : 3, match),
5684 : 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5685 : : else
5686 : 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%> "
5687 : : "in %<%E ? %E : %E%>"), 3, match),
5688 : : arg1, arg2, arg3,
5689 : 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5690 : : break;
5691 : :
5692 : 0 : case POSTINCREMENT_EXPR:
5693 : 0 : case POSTDECREMENT_EXPR:
5694 : 0 : if (flag_diagnostics_show_caret)
5695 : 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5696 : 0 : opname, TREE_TYPE (arg1));
5697 : : else
5698 : 0 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
5699 : : 1, match),
5700 : 0 : opname, arg1, opname, TREE_TYPE (arg1));
5701 : : break;
5702 : :
5703 : 13 : case ARRAY_REF:
5704 : 13 : if (flag_diagnostics_show_caret)
5705 : 0 : error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
5706 : 0 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5707 : : else
5708 : 26 : error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
5709 : : 2, match),
5710 : 13 : arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
5711 : : break;
5712 : :
5713 : 6 : case REALPART_EXPR:
5714 : 6 : case IMAGPART_EXPR:
5715 : 6 : if (flag_diagnostics_show_caret)
5716 : 0 : error_at (loc, op_error_string (G_("%qs"), 1, match),
5717 : 0 : opname, TREE_TYPE (arg1));
5718 : : else
5719 : 12 : error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
5720 : 6 : opname, opname, arg1, TREE_TYPE (arg1));
5721 : : break;
5722 : :
5723 : 0 : case CO_AWAIT_EXPR:
5724 : 0 : if (flag_diagnostics_show_caret)
5725 : 0 : error_at (loc, op_error_string (G_("%<operator %s%>"), 1, match),
5726 : 0 : opname, TREE_TYPE (arg1));
5727 : : else
5728 : 0 : error_at (loc, op_error_string (G_("%<operator %s%> in %<%s%E%>"),
5729 : : 1, match),
5730 : 0 : opname, opname, arg1, TREE_TYPE (arg1));
5731 : : break;
5732 : :
5733 : 1445 : default:
5734 : 1445 : if (arg2)
5735 : 1345 : if (flag_diagnostics_show_caret)
5736 : : {
5737 : 21 : binary_op_rich_location richloc (loc, arg1, arg2, true);
5738 : 21 : pp_markup::element_quoted_type element_0
5739 : 21 : (TREE_TYPE (arg1), highlight_colors::lhs);
5740 : 21 : pp_markup::element_quoted_type element_1
5741 : 21 : (TREE_TYPE (arg2), highlight_colors::rhs);
5742 : 21 : error_at (&richloc,
5743 : : binop_error_string (G_("%<operator%s%>"), match),
5744 : : opname, &element_0, &element_1);
5745 : 21 : }
5746 : : else
5747 : 2648 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
5748 : : 2, match),
5749 : : opname, arg1, opname, arg2,
5750 : 1324 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5751 : : else
5752 : 100 : if (flag_diagnostics_show_caret)
5753 : 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5754 : 0 : opname, TREE_TYPE (arg1));
5755 : : else
5756 : 200 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
5757 : : 1, match),
5758 : 100 : opname, opname, arg1, TREE_TYPE (arg1));
5759 : : break;
5760 : : }
5761 : 1464 : }
5762 : :
5763 : : /* Return the implicit conversion sequence that could be used to
5764 : : convert E1 to E2 in [expr.cond]. */
5765 : :
5766 : : static conversion *
5767 : 286070 : conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
5768 : : {
5769 : 286070 : tree t1 = non_reference (TREE_TYPE (e1));
5770 : 286070 : tree t2 = non_reference (TREE_TYPE (e2));
5771 : 286070 : conversion *conv;
5772 : 286070 : bool good_base;
5773 : :
5774 : : /* [expr.cond]
5775 : :
5776 : : If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
5777 : : implicitly converted (clause _conv_) to the type "lvalue reference to
5778 : : T2", subject to the constraint that in the conversion the
5779 : : reference must bind directly (_dcl.init.ref_) to an lvalue.
5780 : :
5781 : : If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
5782 : : implicitly converted to the type "rvalue reference to T2", subject to
5783 : : the constraint that the reference must bind directly. */
5784 : 286070 : if (glvalue_p (e2))
5785 : : {
5786 : 256230 : tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
5787 : 256230 : conv = implicit_conversion (rtype,
5788 : : t1,
5789 : : e1,
5790 : : /*c_cast_p=*/false,
5791 : : LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
5792 : : |LOOKUP_ONLYCONVERTING,
5793 : : complain);
5794 : 256230 : if (conv && !conv->bad_p)
5795 : : return conv;
5796 : : }
5797 : :
5798 : : /* If E2 is a prvalue or if neither of the conversions above can be done
5799 : : and at least one of the operands has (possibly cv-qualified) class
5800 : : type: */
5801 : 237462 : if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
5802 : : return NULL;
5803 : :
5804 : : /* [expr.cond]
5805 : :
5806 : : If E1 and E2 have class type, and the underlying class types are
5807 : : the same or one is a base class of the other: E1 can be converted
5808 : : to match E2 if the class of T2 is the same type as, or a base
5809 : : class of, the class of T1, and the cv-qualification of T2 is the
5810 : : same cv-qualification as, or a greater cv-qualification than, the
5811 : : cv-qualification of T1. If the conversion is applied, E1 is
5812 : : changed to an rvalue of type T2 that still refers to the original
5813 : : source class object (or the appropriate subobject thereof). */
5814 : 126882 : if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
5815 : 253844 : && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
5816 : : {
5817 : 59143 : if (good_base && at_least_as_qualified_p (t2, t1))
5818 : : {
5819 : 29491 : conv = build_identity_conv (t1, e1);
5820 : 29491 : if (!same_type_p (TYPE_MAIN_VARIANT (t1),
5821 : : TYPE_MAIN_VARIANT (t2)))
5822 : 6 : conv = build_conv (ck_base, t2, conv);
5823 : : else
5824 : 29485 : conv = build_conv (ck_rvalue, t2, conv);
5825 : 29491 : return conv;
5826 : : }
5827 : : else
5828 : 29652 : return NULL;
5829 : : }
5830 : : else
5831 : : /* [expr.cond]
5832 : :
5833 : : Otherwise: E1 can be converted to match E2 if E1 can be implicitly
5834 : : converted to the type that expression E2 would have if E2 were
5835 : : converted to an rvalue (or the type it has, if E2 is an rvalue). */
5836 : 129955 : return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
5837 : 129955 : LOOKUP_IMPLICIT, complain);
5838 : : }
5839 : :
5840 : : /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
5841 : : arguments to the conditional expression. */
5842 : :
5843 : : tree
5844 : 4947082 : build_conditional_expr (const op_location_t &loc,
5845 : : tree arg1, tree arg2, tree arg3,
5846 : : tsubst_flags_t complain)
5847 : : {
5848 : 4947082 : tree arg2_type;
5849 : 4947082 : tree arg3_type;
5850 : 4947082 : tree result = NULL_TREE;
5851 : 4947082 : tree result_type = NULL_TREE;
5852 : 4947082 : tree semantic_result_type = NULL_TREE;
5853 : 4947082 : bool is_glvalue = true;
5854 : 4947082 : struct z_candidate *candidates = 0;
5855 : 4947082 : struct z_candidate *cand;
5856 : 4947082 : tree orig_arg2, orig_arg3;
5857 : :
5858 : 4947082 : auto_cond_timevar tv (TV_OVERLOAD);
5859 : :
5860 : : /* As a G++ extension, the second argument to the conditional can be
5861 : : omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
5862 : : c'.) If the second operand is omitted, make sure it is
5863 : : calculated only once. */
5864 : 4947082 : if (!arg2)
5865 : : {
5866 : 377 : if (complain & tf_error)
5867 : 371 : pedwarn (loc, OPT_Wpedantic,
5868 : : "ISO C++ forbids omitting the middle term of "
5869 : : "a %<?:%> expression");
5870 : :
5871 : 377 : if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
5872 : 329 : warn_for_omitted_condop (loc, arg1);
5873 : :
5874 : : /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
5875 : 377 : if (glvalue_p (arg1))
5876 : : {
5877 : 86 : arg1 = cp_stabilize_reference (arg1);
5878 : 86 : arg2 = arg1 = prevent_lifetime_extension (arg1);
5879 : : }
5880 : 291 : else if (TREE_CODE (arg1) == TARGET_EXPR)
5881 : : /* arg1 can't be a prvalue result of the conditional
5882 : : expression, since it needs to be materialized for the
5883 : : conversion to bool, so treat it as an xvalue in arg2. */
5884 : 6 : arg2 = move (TARGET_EXPR_SLOT (arg1));
5885 : 285 : else if (TREE_CODE (arg1) == EXCESS_PRECISION_EXPR)
5886 : 3 : arg2 = arg1 = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (arg1),
5887 : 3 : cp_save_expr (TREE_OPERAND (arg1, 0)));
5888 : : else
5889 : 282 : arg2 = arg1 = cp_save_expr (arg1);
5890 : : }
5891 : :
5892 : : /* If something has already gone wrong, just pass that fact up the
5893 : : tree. */
5894 : 4947082 : if (error_operand_p (arg1)
5895 : 4947001 : || error_operand_p (arg2)
5896 : 9894043 : || error_operand_p (arg3))
5897 : 161 : return error_mark_node;
5898 : :
5899 : 4946921 : conversion_obstack_sentinel cos;
5900 : :
5901 : 4946921 : orig_arg2 = arg2;
5902 : 4946921 : orig_arg3 = arg3;
5903 : :
5904 : 4946921 : if (gnu_vector_type_p (TREE_TYPE (arg1))
5905 : 4946921 : && VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
5906 : : {
5907 : 1002 : tree arg1_type = TREE_TYPE (arg1);
5908 : :
5909 : : /* If arg1 is another cond_expr choosing between -1 and 0,
5910 : : then we can use its comparison. It may help to avoid
5911 : : additional comparison, produce more accurate diagnostics
5912 : : and enables folding. */
5913 : 1002 : if (TREE_CODE (arg1) == VEC_COND_EXPR
5914 : 912 : && integer_minus_onep (TREE_OPERAND (arg1, 1))
5915 : 1914 : && integer_zerop (TREE_OPERAND (arg1, 2)))
5916 : 912 : arg1 = TREE_OPERAND (arg1, 0);
5917 : :
5918 : 1002 : arg1 = force_rvalue (arg1, complain);
5919 : 1002 : arg2 = force_rvalue (arg2, complain);
5920 : 1002 : arg3 = force_rvalue (arg3, complain);
5921 : :
5922 : : /* force_rvalue can return error_mark on valid arguments. */
5923 : 1002 : if (error_operand_p (arg1)
5924 : 1002 : || error_operand_p (arg2)
5925 : 2004 : || error_operand_p (arg3))
5926 : 0 : return error_mark_node;
5927 : :
5928 : 1002 : arg2_type = TREE_TYPE (arg2);
5929 : 1002 : arg3_type = TREE_TYPE (arg3);
5930 : :
5931 : 1002 : if (!VECTOR_TYPE_P (arg2_type)
5932 : 75 : && !VECTOR_TYPE_P (arg3_type))
5933 : : {
5934 : : /* Rely on the error messages of the scalar version. */
5935 : 57 : tree scal = build_conditional_expr (loc, integer_one_node,
5936 : : orig_arg2, orig_arg3, complain);
5937 : 57 : if (scal == error_mark_node)
5938 : : return error_mark_node;
5939 : 57 : tree stype = TREE_TYPE (scal);
5940 : 57 : tree ctype = TREE_TYPE (arg1_type);
5941 : 57 : if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
5942 : 57 : || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
5943 : : {
5944 : 9 : if (complain & tf_error)
5945 : 9 : error_at (loc, "inferred scalar type %qT is not an integer or "
5946 : : "floating-point type of the same size as %qT", stype,
5947 : 9 : COMPARISON_CLASS_P (arg1)
5948 : 9 : ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
5949 : : : ctype);
5950 : 9 : return error_mark_node;
5951 : : }
5952 : :
5953 : 48 : tree vtype = build_opaque_vector_type (stype,
5954 : 48 : TYPE_VECTOR_SUBPARTS (arg1_type));
5955 : : /* We could pass complain & tf_warning to unsafe_conversion_p,
5956 : : but the warnings (like Wsign-conversion) have already been
5957 : : given by the scalar build_conditional_expr_1. We still check
5958 : : unsafe_conversion_p to forbid truncating long long -> float. */
5959 : 48 : if (unsafe_conversion_p (stype, arg2, NULL_TREE, false))
5960 : : {
5961 : 0 : if (complain & tf_error)
5962 : 0 : error_at (loc, "conversion of scalar %qH to vector %qI "
5963 : : "involves truncation", arg2_type, vtype);
5964 : 0 : return error_mark_node;
5965 : : }
5966 : 48 : if (unsafe_conversion_p (stype, arg3, NULL_TREE, false))
5967 : : {
5968 : 3 : if (complain & tf_error)
5969 : 3 : error_at (loc, "conversion of scalar %qH to vector %qI "
5970 : : "involves truncation", arg3_type, vtype);
5971 : 3 : return error_mark_node;
5972 : : }
5973 : :
5974 : 45 : arg2 = cp_convert (stype, arg2, complain);
5975 : 45 : arg2 = save_expr (arg2);
5976 : 45 : arg2 = build_vector_from_val (vtype, arg2);
5977 : 45 : arg2_type = vtype;
5978 : 45 : arg3 = cp_convert (stype, arg3, complain);
5979 : 45 : arg3 = save_expr (arg3);
5980 : 45 : arg3 = build_vector_from_val (vtype, arg3);
5981 : 45 : arg3_type = vtype;
5982 : : }
5983 : :
5984 : 1962 : if ((gnu_vector_type_p (arg2_type) && !VECTOR_TYPE_P (arg3_type))
5985 : 1884 : || (gnu_vector_type_p (arg3_type) && !VECTOR_TYPE_P (arg2_type)))
5986 : : {
5987 : 96 : enum stv_conv convert_flag =
5988 : 96 : scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
5989 : : complain & tf_error);
5990 : :
5991 : 96 : switch (convert_flag)
5992 : : {
5993 : 0 : case stv_error:
5994 : 0 : return error_mark_node;
5995 : 15 : case stv_firstarg:
5996 : 15 : {
5997 : 15 : arg2 = save_expr (arg2);
5998 : 15 : arg2 = convert (TREE_TYPE (arg3_type), arg2);
5999 : 15 : arg2 = build_vector_from_val (arg3_type, arg2);
6000 : 15 : arg2_type = TREE_TYPE (arg2);
6001 : 15 : break;
6002 : : }
6003 : 72 : case stv_secondarg:
6004 : 72 : {
6005 : 72 : arg3 = save_expr (arg3);
6006 : 72 : arg3 = convert (TREE_TYPE (arg2_type), arg3);
6007 : 72 : arg3 = build_vector_from_val (arg2_type, arg3);
6008 : 72 : arg3_type = TREE_TYPE (arg3);
6009 : 72 : break;
6010 : : }
6011 : : default:
6012 : : break;
6013 : : }
6014 : : }
6015 : :
6016 : 990 : if (!gnu_vector_type_p (arg2_type)
6017 : 987 : || !gnu_vector_type_p (arg3_type)
6018 : 981 : || !same_type_p (arg2_type, arg3_type)
6019 : 981 : || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type),
6020 : 981 : TYPE_VECTOR_SUBPARTS (arg2_type))
6021 : 1971 : || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
6022 : : {
6023 : 9 : if (complain & tf_error)
6024 : 6 : error_at (loc,
6025 : : "incompatible vector types in conditional expression: "
6026 : 6 : "%qT, %qT and %qT", TREE_TYPE (arg1),
6027 : 6 : TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
6028 : 9 : return error_mark_node;
6029 : : }
6030 : :
6031 : 981 : if (!COMPARISON_CLASS_P (arg1))
6032 : : {
6033 : 87 : tree cmp_type = truth_type_for (arg1_type);
6034 : 87 : arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
6035 : : }
6036 : 981 : return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
6037 : : }
6038 : :
6039 : : /* [expr.cond]
6040 : :
6041 : : The first expression is implicitly converted to bool (clause
6042 : : _conv_). */
6043 : 4945919 : arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
6044 : : LOOKUP_NORMAL);
6045 : 4945919 : if (error_operand_p (arg1))
6046 : 25 : return error_mark_node;
6047 : :
6048 : 4945894 : arg2_type = unlowered_expr_type (arg2);
6049 : 4945894 : arg3_type = unlowered_expr_type (arg3);
6050 : :
6051 : 4945894 : if ((TREE_CODE (arg2) == EXCESS_PRECISION_EXPR
6052 : 4945876 : || TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
6053 : 45 : && (TREE_CODE (arg2_type) == INTEGER_TYPE
6054 : : || SCALAR_FLOAT_TYPE_P (arg2_type)
6055 : : || TREE_CODE (arg2_type) == COMPLEX_TYPE)
6056 : 45 : && (TREE_CODE (arg3_type) == INTEGER_TYPE
6057 : : || SCALAR_FLOAT_TYPE_P (arg3_type)
6058 : : || TREE_CODE (arg3_type) == COMPLEX_TYPE))
6059 : : {
6060 : 45 : semantic_result_type
6061 : 45 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6062 : 45 : if (semantic_result_type == error_mark_node)
6063 : : {
6064 : 0 : tree t1 = arg2_type;
6065 : 0 : tree t2 = arg3_type;
6066 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6067 : 0 : t1 = TREE_TYPE (t1);
6068 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6069 : 0 : t2 = TREE_TYPE (t2);
6070 : 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6071 : : && SCALAR_FLOAT_TYPE_P (t2)
6072 : : && (extended_float_type_p (t1)
6073 : : || extended_float_type_p (t2))
6074 : : && cp_compare_floating_point_conversion_ranks
6075 : : (t1, t2) == 3);
6076 : 0 : if (complain & tf_error)
6077 : 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6078 : : "have unordered conversion rank",
6079 : : arg2_type, arg3_type);
6080 : 0 : return error_mark_node;
6081 : : }
6082 : 45 : if (TREE_CODE (arg2) == EXCESS_PRECISION_EXPR)
6083 : : {
6084 : 18 : arg2 = TREE_OPERAND (arg2, 0);
6085 : 18 : arg2_type = TREE_TYPE (arg2);
6086 : : }
6087 : 45 : if (TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
6088 : : {
6089 : 27 : arg3 = TREE_OPERAND (arg3, 0);
6090 : 27 : arg3_type = TREE_TYPE (arg3);
6091 : : }
6092 : : }
6093 : :
6094 : : /* [expr.cond]
6095 : :
6096 : : If either the second or the third operand has type (possibly
6097 : : cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
6098 : : array-to-pointer (_conv.array_), and function-to-pointer
6099 : : (_conv.func_) standard conversions are performed on the second
6100 : : and third operands. */
6101 : 4945894 : if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
6102 : : {
6103 : : /* 'void' won't help in resolving an overloaded expression on the
6104 : : other side, so require it to resolve by itself. */
6105 : 119076 : if (arg2_type == unknown_type_node)
6106 : : {
6107 : 9 : arg2 = resolve_nondeduced_context_or_error (arg2, complain);
6108 : 9 : arg2_type = TREE_TYPE (arg2);
6109 : : }
6110 : 119076 : if (arg3_type == unknown_type_node)
6111 : : {
6112 : 0 : arg3 = resolve_nondeduced_context_or_error (arg3, complain);
6113 : 0 : arg3_type = TREE_TYPE (arg3);
6114 : : }
6115 : :
6116 : : /* [expr.cond]
6117 : :
6118 : : One of the following shall hold:
6119 : :
6120 : : --The second or the third operand (but not both) is a
6121 : : throw-expression (_except.throw_); the result is of the type
6122 : : and value category of the other.
6123 : :
6124 : : --Both the second and the third operands have type void; the
6125 : : result is of type void and is a prvalue. */
6126 : 119076 : if (TREE_CODE (arg2) == THROW_EXPR
6127 : 80 : && TREE_CODE (arg3) != THROW_EXPR)
6128 : : {
6129 : 68 : result_type = arg3_type;
6130 : 68 : is_glvalue = glvalue_p (arg3);
6131 : : }
6132 : 119008 : else if (TREE_CODE (arg2) != THROW_EXPR
6133 : 118996 : && TREE_CODE (arg3) == THROW_EXPR)
6134 : : {
6135 : 171 : result_type = arg2_type;
6136 : 171 : is_glvalue = glvalue_p (arg2);
6137 : : }
6138 : 118837 : else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
6139 : : {
6140 : 118805 : result_type = void_type_node;
6141 : 118805 : is_glvalue = false;
6142 : : }
6143 : : else
6144 : : {
6145 : 32 : if (complain & tf_error)
6146 : : {
6147 : 18 : if (VOID_TYPE_P (arg2_type))
6148 : 9 : error_at (cp_expr_loc_or_loc (arg3, loc),
6149 : : "second operand to the conditional operator "
6150 : : "is of type %<void%>, but the third operand is "
6151 : : "neither a throw-expression nor of type %<void%>");
6152 : : else
6153 : 9 : error_at (cp_expr_loc_or_loc (arg2, loc),
6154 : : "third operand to the conditional operator "
6155 : : "is of type %<void%>, but the second operand is "
6156 : : "neither a throw-expression nor of type %<void%>");
6157 : : }
6158 : 32 : return error_mark_node;
6159 : : }
6160 : :
6161 : 119044 : goto valid_operands;
6162 : : }
6163 : : /* [expr.cond]
6164 : :
6165 : : Otherwise, if the second and third operand have different types,
6166 : : and either has (possibly cv-qualified) class type, or if both are
6167 : : glvalues of the same value category and the same type except for
6168 : : cv-qualification, an attempt is made to convert each of those operands
6169 : : to the type of the other. */
6170 : 4826818 : else if (!same_type_p (arg2_type, arg3_type)
6171 : 4826818 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
6172 : 1083643 : || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
6173 : : arg3_type)
6174 : 316671 : && glvalue_p (arg2) && glvalue_p (arg3)
6175 : 48588 : && lvalue_p (arg2) == lvalue_p (arg3))))
6176 : : {
6177 : 143035 : conversion *conv2;
6178 : 143035 : conversion *conv3;
6179 : 143035 : bool converted = false;
6180 : :
6181 : 143035 : conv2 = conditional_conversion (arg2, arg3, complain);
6182 : 143035 : conv3 = conditional_conversion (arg3, arg2, complain);
6183 : :
6184 : : /* [expr.cond]
6185 : :
6186 : : If both can be converted, or one can be converted but the
6187 : : conversion is ambiguous, the program is ill-formed. If
6188 : : neither can be converted, the operands are left unchanged and
6189 : : further checking is performed as described below. If exactly
6190 : : one conversion is possible, that conversion is applied to the
6191 : : chosen operand and the converted operand is used in place of
6192 : : the original operand for the remainder of this section. */
6193 : 143035 : if ((conv2 && !conv2->bad_p
6194 : 62923 : && conv3 && !conv3->bad_p)
6195 : 62944 : || (conv2 && conv2->kind == ck_ambig)
6196 : 142924 : || (conv3 && conv3->kind == ck_ambig))
6197 : : {
6198 : 111 : if (complain & tf_error)
6199 : : {
6200 : 6 : error_at (loc, "operands to %<?:%> have different types "
6201 : : "%qT and %qT",
6202 : : arg2_type, arg3_type);
6203 : 6 : if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
6204 : 3 : inform (loc, " and each type can be converted to the other");
6205 : 3 : else if (conv2 && conv2->kind == ck_ambig)
6206 : 3 : convert_like (conv2, arg2, complain);
6207 : : else
6208 : 0 : convert_like (conv3, arg3, complain);
6209 : : }
6210 : 111 : result = error_mark_node;
6211 : : }
6212 : 142924 : else if (conv2 && !conv2->bad_p)
6213 : : {
6214 : 62812 : arg2 = convert_like (conv2, arg2, complain);
6215 : 62812 : arg2 = convert_from_reference (arg2);
6216 : 62812 : arg2_type = TREE_TYPE (arg2);
6217 : : /* Even if CONV2 is a valid conversion, the result of the
6218 : : conversion may be invalid. For example, if ARG3 has type
6219 : : "volatile X", and X does not have a copy constructor
6220 : : accepting a "volatile X&", then even if ARG2 can be
6221 : : converted to X, the conversion will fail. */
6222 : 62812 : if (error_operand_p (arg2))
6223 : 0 : result = error_mark_node;
6224 : 0 : converted = true;
6225 : : }
6226 : 80112 : else if (conv3 && !conv3->bad_p)
6227 : : {
6228 : 79250 : arg3 = convert_like (conv3, arg3, complain);
6229 : 79250 : arg3 = convert_from_reference (arg3);
6230 : 79250 : arg3_type = TREE_TYPE (arg3);
6231 : 79250 : if (error_operand_p (arg3))
6232 : 0 : result = error_mark_node;
6233 : 0 : converted = true;
6234 : : }
6235 : :
6236 : 111 : if (result)
6237 : : return result;
6238 : :
6239 : : /* If, after the conversion, both operands have class type,
6240 : : treat the cv-qualification of both operands as if it were the
6241 : : union of the cv-qualification of the operands.
6242 : :
6243 : : The standard is not clear about what to do in this
6244 : : circumstance. For example, if the first operand has type
6245 : : "const X" and the second operand has a user-defined
6246 : : conversion to "volatile X", what is the type of the second
6247 : : operand after this step? Making it be "const X" (matching
6248 : : the first operand) seems wrong, as that discards the
6249 : : qualification without actually performing a copy. Leaving it
6250 : : as "volatile X" seems wrong as that will result in the
6251 : : conditional expression failing altogether, even though,
6252 : : according to this step, the one operand could be converted to
6253 : : the type of the other. */
6254 : 142924 : if (converted
6255 : 142062 : && CLASS_TYPE_P (arg2_type)
6256 : 175001 : && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
6257 : 0 : arg2_type = arg3_type =
6258 : 0 : cp_build_qualified_type (arg2_type,
6259 : 0 : cp_type_quals (arg2_type)
6260 : 0 : | cp_type_quals (arg3_type));
6261 : : }
6262 : :
6263 : : /* [expr.cond]
6264 : :
6265 : : If the second and third operands are glvalues of the same value
6266 : : category and have the same type, the result is of that type and
6267 : : value category. */
6268 : 6503211 : if (((lvalue_p (arg2) && lvalue_p (arg3))
6269 : 3532005 : || (xvalue_p (arg2) && xvalue_p (arg3)))
6270 : 6149914 : && same_type_p (arg2_type, arg3_type))
6271 : : {
6272 : 1276132 : result_type = arg2_type;
6273 : 1276132 : goto valid_operands;
6274 : : }
6275 : :
6276 : : /* [expr.cond]
6277 : :
6278 : : Otherwise, the result is an rvalue. If the second and third
6279 : : operand do not have the same type, and either has (possibly
6280 : : cv-qualified) class type, overload resolution is used to
6281 : : determine the conversions (if any) to be applied to the operands
6282 : : (_over.match.oper_, _over.built_). */
6283 : 3550575 : is_glvalue = false;
6284 : 3550575 : if (!same_type_p (arg2_type, arg3_type)
6285 : 3550575 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
6286 : : {
6287 : 862 : releasing_vec args;
6288 : 862 : conversion *conv;
6289 : 862 : bool any_viable_p;
6290 : :
6291 : : /* Rearrange the arguments so that add_builtin_candidate only has
6292 : : to know about two args. In build_builtin_candidate, the
6293 : : arguments are unscrambled. */
6294 : 862 : args->quick_push (arg2);
6295 : 862 : args->quick_push (arg3);
6296 : 862 : args->quick_push (arg1);
6297 : 862 : add_builtin_candidates (&candidates,
6298 : : COND_EXPR,
6299 : : NOP_EXPR,
6300 : : ovl_op_identifier (false, COND_EXPR),
6301 : : args,
6302 : : LOOKUP_NORMAL, complain);
6303 : :
6304 : : /* [expr.cond]
6305 : :
6306 : : If the overload resolution fails, the program is
6307 : : ill-formed. */
6308 : 862 : candidates = splice_viable (candidates, false, &any_viable_p);
6309 : 862 : if (!any_viable_p)
6310 : : {
6311 : 805 : if (complain & tf_error)
6312 : 22 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6313 : : arg2_type, arg3_type);
6314 : 805 : return error_mark_node;
6315 : : }
6316 : 57 : cand = tourney (candidates, complain);
6317 : 57 : if (!cand)
6318 : : {
6319 : 0 : if (complain & tf_error)
6320 : : {
6321 : 0 : auto_diagnostic_group d;
6322 : 0 : op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, false);
6323 : 0 : print_z_candidates (loc, candidates);
6324 : 0 : }
6325 : 0 : return error_mark_node;
6326 : : }
6327 : :
6328 : : /* [expr.cond]
6329 : :
6330 : : Otherwise, the conversions thus determined are applied, and
6331 : : the converted operands are used in place of the original
6332 : : operands for the remainder of this section. */
6333 : 57 : conv = cand->convs[0];
6334 : 57 : arg1 = convert_like (conv, arg1, complain);
6335 : 57 : conv = cand->convs[1];
6336 : 57 : arg2 = convert_like (conv, arg2, complain);
6337 : 57 : arg2_type = TREE_TYPE (arg2);
6338 : 57 : conv = cand->convs[2];
6339 : 57 : arg3 = convert_like (conv, arg3, complain);
6340 : 57 : arg3_type = TREE_TYPE (arg3);
6341 : 862 : }
6342 : :
6343 : : /* [expr.cond]
6344 : :
6345 : : Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
6346 : : and function-to-pointer (_conv.func_) standard conversions are
6347 : : performed on the second and third operands.
6348 : :
6349 : : We need to force the lvalue-to-rvalue conversion here for class types,
6350 : : so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
6351 : : that isn't wrapped with a TARGET_EXPR plays havoc with exception
6352 : : regions. */
6353 : :
6354 : 3549770 : arg2 = force_rvalue (arg2, complain);
6355 : 3549770 : if (!CLASS_TYPE_P (arg2_type))
6356 : 3475838 : arg2_type = TREE_TYPE (arg2);
6357 : :
6358 : 3549770 : arg3 = force_rvalue (arg3, complain);
6359 : 3549770 : if (!CLASS_TYPE_P (arg3_type))
6360 : 3475838 : arg3_type = TREE_TYPE (arg3);
6361 : :
6362 : 3549770 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6363 : : return error_mark_node;
6364 : :
6365 : : /* [expr.cond]
6366 : :
6367 : : After those conversions, one of the following shall hold:
6368 : :
6369 : : --The second and third operands have the same type; the result is of
6370 : : that type. */
6371 : 3549728 : if (same_type_p (arg2_type, arg3_type))
6372 : : result_type = arg2_type;
6373 : : /* [expr.cond]
6374 : :
6375 : : --The second and third operands have arithmetic or enumeration
6376 : : type; the usual arithmetic conversions are performed to bring
6377 : : them to a common type, and the result is of that type. */
6378 : 24515 : else if ((ARITHMETIC_TYPE_P (arg2_type)
6379 : 23666 : || UNSCOPED_ENUM_P (arg2_type))
6380 : 701190 : && (ARITHMETIC_TYPE_P (arg3_type)
6381 : 211 : || UNSCOPED_ENUM_P (arg3_type)))
6382 : : {
6383 : : /* A conditional expression between a floating-point
6384 : : type and an integer type should convert the integer type to
6385 : : the evaluation format of the floating-point type, with
6386 : : possible excess precision. */
6387 : 676533 : tree eptype2 = arg2_type;
6388 : 676533 : tree eptype3 = arg3_type;
6389 : 676533 : tree eptype;
6390 : 849 : if (ANY_INTEGRAL_TYPE_P (arg2_type)
6391 : 676536 : && (eptype = excess_precision_type (arg3_type)) != NULL_TREE)
6392 : : {
6393 : 3 : eptype3 = eptype;
6394 : 3 : if (!semantic_result_type)
6395 : 3 : semantic_result_type
6396 : 3 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6397 : : }
6398 : 576 : else if (ANY_INTEGRAL_TYPE_P (arg3_type)
6399 : 676530 : && (eptype = excess_precision_type (arg2_type)) != NULL_TREE)
6400 : : {
6401 : 23 : eptype2 = eptype;
6402 : 23 : if (!semantic_result_type)
6403 : 23 : semantic_result_type
6404 : 23 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6405 : : }
6406 : 676533 : result_type = type_after_usual_arithmetic_conversions (eptype2,
6407 : : eptype3);
6408 : 676533 : if (result_type == error_mark_node)
6409 : : {
6410 : 0 : tree t1 = eptype2;
6411 : 0 : tree t2 = eptype3;
6412 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6413 : 0 : t1 = TREE_TYPE (t1);
6414 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6415 : 0 : t2 = TREE_TYPE (t2);
6416 : 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6417 : : && SCALAR_FLOAT_TYPE_P (t2)
6418 : : && (extended_float_type_p (t1)
6419 : : || extended_float_type_p (t2))
6420 : : && cp_compare_floating_point_conversion_ranks
6421 : : (t1, t2) == 3);
6422 : 0 : if (complain & tf_error)
6423 : 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6424 : : "have unordered conversion rank",
6425 : : eptype2, eptype3);
6426 : 0 : return error_mark_node;
6427 : : }
6428 : 676533 : if (semantic_result_type == error_mark_node)
6429 : : {
6430 : 0 : tree t1 = arg2_type;
6431 : 0 : tree t2 = arg3_type;
6432 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6433 : 0 : t1 = TREE_TYPE (t1);
6434 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6435 : 0 : t2 = TREE_TYPE (t2);
6436 : 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6437 : : && SCALAR_FLOAT_TYPE_P (t2)
6438 : : && (extended_float_type_p (t1)
6439 : : || extended_float_type_p (t2))
6440 : : && cp_compare_floating_point_conversion_ranks
6441 : : (t1, t2) == 3);
6442 : 0 : if (complain & tf_error)
6443 : 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6444 : : "have unordered conversion rank",
6445 : : arg2_type, arg3_type);
6446 : 0 : return error_mark_node;
6447 : : }
6448 : :
6449 : 676533 : if (complain & tf_warning)
6450 : 648246 : do_warn_double_promotion (result_type, arg2_type, arg3_type,
6451 : : "implicit conversion from %qH to %qI to "
6452 : : "match other result of conditional",
6453 : : loc);
6454 : :
6455 : 676533 : if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
6456 : 95 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
6457 : : {
6458 : 34 : tree stripped_orig_arg2 = tree_strip_any_location_wrapper (orig_arg2);
6459 : 34 : tree stripped_orig_arg3 = tree_strip_any_location_wrapper (orig_arg3);
6460 : 34 : if (TREE_CODE (stripped_orig_arg2) == CONST_DECL
6461 : 23 : && TREE_CODE (stripped_orig_arg3) == CONST_DECL
6462 : 57 : && (DECL_CONTEXT (stripped_orig_arg2)
6463 : 23 : == DECL_CONTEXT (stripped_orig_arg3)))
6464 : : /* Two enumerators from the same enumeration can have different
6465 : : types when the enumeration is still being defined. */;
6466 : 28 : else if (complain & (cxx_dialect >= cxx26
6467 : 28 : ? tf_warning_or_error : tf_warning))
6468 : 43 : emit_diagnostic ((cxx_dialect >= cxx26
6469 : : ? diagnostics::kind::pedwarn
6470 : : : diagnostics::kind::warning),
6471 : 25 : loc, OPT_Wenum_compare, "enumerated mismatch "
6472 : : "in conditional expression: %qT vs %qT",
6473 : : arg2_type, arg3_type);
6474 : 3 : else if (cxx_dialect >= cxx26)
6475 : 1 : return error_mark_node;
6476 : : }
6477 : 676499 : else if ((((complain & (cxx_dialect >= cxx26
6478 : 676499 : ? tf_warning_or_error : tf_warning))
6479 : 655378 : && warn_deprecated_enum_float_conv)
6480 : 407812 : || (cxx_dialect >= cxx26
6481 : 3274 : && (complain & tf_warning_or_error) == 0))
6482 : 271872 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6483 : 16 : && SCALAR_FLOAT_TYPE_P (arg3_type))
6484 : 271861 : || (SCALAR_FLOAT_TYPE_P (arg2_type)
6485 : 354 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)))
6486 : : {
6487 : 19 : if (cxx_dialect >= cxx26 && (complain & tf_warning_or_error) == 0)
6488 : 2 : return error_mark_node;
6489 : 17 : if (cxx_dialect >= cxx26 && TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6490 : 4 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6491 : : "conditional expression between enumeration type "
6492 : : "%qT and floating-point type %qT", arg2_type, arg3_type);
6493 : 13 : else if (cxx_dialect >= cxx26)
6494 : 3 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6495 : : "conditional expression between floating-point type "
6496 : : "%qT and enumeration type %qT", arg2_type, arg3_type);
6497 : 10 : else if (TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6498 : 6 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6499 : : "conditional expression between enumeration type "
6500 : : "%qT and floating-point type %qT is deprecated",
6501 : : arg2_type, arg3_type);
6502 : : else
6503 : 4 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6504 : : "conditional expression between floating-point "
6505 : : "type %qT and enumeration type %qT is deprecated",
6506 : : arg2_type, arg3_type);
6507 : : }
6508 : 667196 : else if ((extra_warnings || warn_enum_conversion)
6509 : 676485 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6510 : 15 : && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
6511 : 9277 : || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
6512 : 8 : && !same_type_p (arg2_type,
6513 : : type_promotes_to (arg3_type)))))
6514 : : {
6515 : 17 : if (complain & tf_warning)
6516 : : {
6517 : 12 : enum opt_code opt = (warn_enum_conversion
6518 : 17 : ? OPT_Wenum_conversion
6519 : : : OPT_Wextra);
6520 : 17 : warning_at (loc, opt, "enumerated and "
6521 : : "non-enumerated type in conditional expression");
6522 : : }
6523 : : }
6524 : :
6525 : 676530 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6526 : 676530 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6527 : : }
6528 : : /* [expr.cond]
6529 : :
6530 : : --The second and third operands have pointer type, or one has
6531 : : pointer type and the other is a null pointer constant; pointer
6532 : : conversions (_conv.ptr_) and qualification conversions
6533 : : (_conv.qual_) are performed to bring them to their composite
6534 : : pointer type (_expr.rel_). The result is of the composite
6535 : : pointer type.
6536 : :
6537 : : --The second and third operands have pointer to member type, or
6538 : : one has pointer to member type and the other is a null pointer
6539 : : constant; pointer to member conversions (_conv.mem_) and
6540 : : qualification conversions (_conv.qual_) are performed to bring
6541 : : them to a common type, whose cv-qualification shall match the
6542 : : cv-qualification of either the second or the third operand.
6543 : : The result is of the common type. */
6544 : 23713 : else if ((null_ptr_cst_p (arg2)
6545 : 179 : && TYPE_PTR_OR_PTRMEM_P (arg3_type))
6546 : 23534 : || (null_ptr_cst_p (arg3)
6547 : 22025 : && TYPE_PTR_OR_PTRMEM_P (arg2_type))
6548 : 1509 : || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
6549 : 106 : || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
6550 : 23812 : || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
6551 : : {
6552 : 23623 : result_type = composite_pointer_type (loc,
6553 : : arg2_type, arg3_type, arg2,
6554 : : arg3, CPO_CONDITIONAL_EXPR,
6555 : : complain);
6556 : 23623 : if (result_type == error_mark_node)
6557 : : return error_mark_node;
6558 : 23601 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6559 : 23601 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6560 : : }
6561 : :
6562 : 3549613 : if (!result_type)
6563 : : {
6564 : 90 : if (complain & tf_error)
6565 : 3 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6566 : : arg2_type, arg3_type);
6567 : 90 : return error_mark_node;
6568 : : }
6569 : :
6570 : 3549613 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6571 : : return error_mark_node;
6572 : :
6573 : 3549610 : valid_operands:
6574 : 4944786 : if (processing_template_decl && is_glvalue)
6575 : : {
6576 : : /* Let lvalue_kind know this was a glvalue. */
6577 : 80658 : tree arg = (result_type == arg2_type ? arg2 : arg3);
6578 : 80658 : result_type = cp_build_reference_type (result_type, xvalue_p (arg));
6579 : : }
6580 : :
6581 : 4944786 : result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
6582 : :
6583 : : /* If the ARG2 and ARG3 are the same and don't have side-effects,
6584 : : warn here, because the COND_EXPR will be turned into ARG2. */
6585 : 4944786 : if (warn_duplicated_branches
6586 : 153 : && (complain & tf_warning)
6587 : 4944939 : && (arg2 == arg3 || operand_equal_p (arg2, arg3,
6588 : : OEP_ADDRESS_OF_SAME_FIELD)))
6589 : 57 : warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
6590 : : "this condition has identical branches");
6591 : :
6592 : : /* We can't use result_type below, as fold might have returned a
6593 : : throw_expr. */
6594 : :
6595 : 4944786 : if (!is_glvalue)
6596 : : {
6597 : : /* Expand both sides into the same slot, hopefully the target of
6598 : : the ?: expression. We used to check for TARGET_EXPRs here,
6599 : : but now we sometimes wrap them in NOP_EXPRs so the test would
6600 : : fail. */
6601 : 3668594 : if (CLASS_TYPE_P (TREE_TYPE (result)))
6602 : : {
6603 : 73975 : result = get_target_expr (result, complain);
6604 : : /* Tell gimplify_modify_expr_rhs not to strip this in
6605 : : assignment context: we want both arms to initialize
6606 : : the same temporary. */
6607 : 73975 : TARGET_EXPR_NO_ELIDE (result) = true;
6608 : : }
6609 : : /* If this expression is an rvalue, but might be mistaken for an
6610 : : lvalue, we must add a NON_LVALUE_EXPR. */
6611 : 3668594 : result = rvalue (result);
6612 : 3668594 : if (semantic_result_type)
6613 : 71 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6614 : : result);
6615 : : }
6616 : : else
6617 : : {
6618 : 1276192 : result = force_paren_expr (result);
6619 : 1276192 : gcc_assert (semantic_result_type == NULL_TREE);
6620 : : }
6621 : :
6622 : : return result;
6623 : 4947082 : }
6624 : :
6625 : : /* OPERAND is an operand to an expression. Perform necessary steps
6626 : : required before using it. If OPERAND is NULL_TREE, NULL_TREE is
6627 : : returned. */
6628 : :
6629 : : static tree
6630 : 523380965 : prep_operand (tree operand)
6631 : : {
6632 : 523380965 : if (operand)
6633 : : {
6634 : 602550100 : if (CLASS_TYPE_P (TREE_TYPE (operand))
6635 : 317786002 : && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
6636 : : /* Make sure the template type is instantiated now. */
6637 : 7769658 : instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
6638 : : }
6639 : :
6640 : 523380965 : return operand;
6641 : : }
6642 : :
6643 : : /* True iff CONV represents a conversion sequence which no other can be better
6644 : : than under [over.ics.rank]: in other words, a "conversion" to the exact same
6645 : : type (including binding to a reference to the same type). This is stronger
6646 : : than the standard's "identity" category, which also includes reference
6647 : : bindings that add cv-qualifiers or change rvalueness. */
6648 : :
6649 : : static bool
6650 : 184204624 : perfect_conversion_p (conversion *conv)
6651 : : {
6652 : 184204624 : if (CONVERSION_RANK (conv) != cr_identity)
6653 : : return false;
6654 : 128410156 : if (conv->kind == ck_ref_bind)
6655 : : {
6656 : 30235852 : if (!conv->rvaluedness_matches_p)
6657 : : return false;
6658 : 21646892 : if (!same_type_p (TREE_TYPE (conv->type),
6659 : : next_conversion (conv)->type))
6660 : : return false;
6661 : : }
6662 : 116927567 : if (conv->check_narrowing)
6663 : : /* Brace elision is imperfect. */
6664 : : return false;
6665 : : return true;
6666 : : }
6667 : :
6668 : : /* True if CAND represents a perfect match, i.e. all perfect conversions, so no
6669 : : other candidate can be a better match. Since the template/non-template
6670 : : tiebreaker comes immediately after the conversion comparison in
6671 : : [over.match.best], a perfect non-template candidate is better than all
6672 : : templates. */
6673 : :
6674 : : static bool
6675 : 414459490 : perfect_candidate_p (z_candidate *cand)
6676 : : {
6677 : 414459490 : if (cand->viable < 1)
6678 : : return false;
6679 : : /* CWG1402 makes an implicitly deleted move op worse than other
6680 : : candidates. */
6681 : 170228295 : if (DECL_DELETED_FN (cand->fn) && DECL_DEFAULTED_FN (cand->fn)
6682 : 169336564 : && move_fn_p (cand->fn))
6683 : : return false;
6684 : 167923423 : int len = cand->num_convs;
6685 : 284850940 : for (int i = 0; i < len; ++i)
6686 : 184204624 : if (!perfect_conversion_p (cand->convs[i]))
6687 : : return false;
6688 : 100646316 : if (conversion *conv = cand->second_conv)
6689 : 0 : if (!perfect_conversion_p (conv))
6690 : : return false;
6691 : : return true;
6692 : : }
6693 : :
6694 : : /* True iff one of CAND's argument conversions is missing. */
6695 : :
6696 : : static bool
6697 : 17298100 : missing_conversion_p (const z_candidate *cand)
6698 : : {
6699 : 32800530 : for (unsigned i = 0; i < cand->num_convs; ++i)
6700 : : {
6701 : 21308455 : conversion *conv = cand->convs[i];
6702 : 21308455 : if (!conv)
6703 : : return true;
6704 : 20326971 : if (conv->kind == ck_deferred_bad)
6705 : : {
6706 : : /* We don't know whether this conversion is outright invalid or
6707 : : just bad, so conservatively assume it's missing. */
6708 : 4824541 : gcc_checking_assert (conv->bad_p);
6709 : : return true;
6710 : : }
6711 : : }
6712 : : return false;
6713 : : }
6714 : :
6715 : : /* Add each of the viable functions in FNS (a FUNCTION_DECL or
6716 : : OVERLOAD) to the CANDIDATES, returning an updated list of
6717 : : CANDIDATES. The ARGS are the arguments provided to the call;
6718 : : if FIRST_ARG is non-null it is the implicit object argument,
6719 : : otherwise the first element of ARGS is used if needed. The
6720 : : EXPLICIT_TARGS are explicit template arguments provided.
6721 : : TEMPLATE_ONLY is true if only template functions should be
6722 : : considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
6723 : : add_function_candidate. */
6724 : :
6725 : : static void
6726 : 231569940 : add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
6727 : : tree return_type,
6728 : : tree explicit_targs, bool template_only,
6729 : : tree conversion_path, tree access_path,
6730 : : int flags,
6731 : : struct z_candidate **candidates,
6732 : : tsubst_flags_t complain)
6733 : : {
6734 : 231569940 : tree ctype;
6735 : 231569940 : const vec<tree, va_gc> *non_static_args;
6736 : 231569940 : bool check_list_ctor = false;
6737 : 231569940 : bool check_converting = false;
6738 : 231569940 : unification_kind_t strict;
6739 : 231569940 : tree ne_context = NULL_TREE;
6740 : 231569940 : tree ne_fns = NULL_TREE;
6741 : :
6742 : 231569940 : if (!fns)
6743 : 2729058 : return;
6744 : :
6745 : : /* Precalculate special handling of constructors and conversion ops. */
6746 : 228840882 : tree fn = OVL_FIRST (fns);
6747 : 228840882 : if (DECL_CONV_FN_P (fn))
6748 : : {
6749 : 9755350 : check_list_ctor = false;
6750 : 9755350 : check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
6751 : 9755350 : if (flags & LOOKUP_NO_CONVERSION)
6752 : : /* We're doing return_type(x). */
6753 : : strict = DEDUCE_CONV;
6754 : : else
6755 : : /* We're doing x.operator return_type(). */
6756 : 2928 : strict = DEDUCE_EXACT;
6757 : : /* [over.match.funcs] For conversion functions, the function
6758 : : is considered to be a member of the class of the implicit
6759 : : object argument for the purpose of defining the type of
6760 : : the implicit object parameter. */
6761 : 9755350 : ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
6762 : : }
6763 : : else
6764 : : {
6765 : 438171064 : if (DECL_CONSTRUCTOR_P (fn))
6766 : : {
6767 : 55431487 : check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
6768 : : /* For list-initialization we consider explicit constructors
6769 : : and complain if one is chosen. */
6770 : 55431487 : check_converting
6771 : 55431487 : = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
6772 : : == LOOKUP_ONLYCONVERTING);
6773 : : }
6774 : 219085532 : strict = DEDUCE_CALL;
6775 : 345541877 : ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
6776 : : }
6777 : :
6778 : : /* P2468: Check if operator== is a rewrite target with first operand
6779 : : (*args)[0]; for now just do the lookups. */
6780 : 228840882 : if ((flags & (LOOKUP_REWRITTEN | LOOKUP_REVERSED))
6781 : 228840882 : && DECL_OVERLOADED_OPERATOR_IS (fn, EQ_EXPR))
6782 : : {
6783 : 2876538 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6784 : 2876538 : if (DECL_CLASS_SCOPE_P (fn))
6785 : : {
6786 : 70453 : ne_context = DECL_CONTEXT (fn);
6787 : 70453 : ne_fns = lookup_fnfields (TREE_TYPE ((*args)[0]), ne_name,
6788 : : 1, tf_none);
6789 : 70453 : if (ne_fns == error_mark_node || ne_fns == NULL_TREE)
6790 : : ne_fns = NULL_TREE;
6791 : : else
6792 : 17379 : ne_fns = BASELINK_FUNCTIONS (ne_fns);
6793 : : }
6794 : : else
6795 : : {
6796 : 2806085 : ne_context = decl_namespace_context (fn);
6797 : 2806085 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6798 : : LOOK_want::NORMAL,
6799 : : /*complain*/false);
6800 : 2806085 : if (ne_fns == error_mark_node
6801 : 815900 : || !is_overloaded_fn (ne_fns))
6802 : 1990185 : ne_fns = NULL_TREE;
6803 : : }
6804 : : }
6805 : :
6806 : 228840882 : if (first_arg)
6807 : : non_static_args = args;
6808 : : else
6809 : : /* Delay creating the implicit this parameter until it is needed. */
6810 : 98215172 : non_static_args = NULL;
6811 : :
6812 : 228840882 : bool seen_strictly_viable = any_strictly_viable (*candidates);
6813 : : /* If there's a non-template perfect match, we don't need to consider
6814 : : templates. So check non-templates first. This optimization is only
6815 : : really needed for the defaulted copy constructor of tuple and the like
6816 : : (96926), but it seems like we might as well enable it more generally. */
6817 : 228840882 : bool seen_perfect = false;
6818 : 228840882 : enum { templates, non_templates, either } which = either;
6819 : 228840882 : if (template_only)
6820 : : which = templates;
6821 : : else /*if (flags & LOOKUP_DEFAULTED)*/
6822 : 199765210 : which = non_templates;
6823 : :
6824 : : /* Template candidates that we'll potentially ignore if the
6825 : : perfect candidate optimization succeeds. */
6826 : 228840882 : z_candidate *ignored_template_cands = nullptr;
6827 : :
6828 : : /* During overload resolution, we first consider each function under the
6829 : : assumption that we'll eventually find a strictly viable candidate.
6830 : : This allows us to circumvent our defacto behavior when checking
6831 : : argument conversions and shortcut consideration of the candidate
6832 : : upon encountering the first bad conversion. If this assumption
6833 : : turns out to be false, and all candidates end up being non-strictly
6834 : : viable, then we reconsider such candidates under the defacto behavior.
6835 : : This trick is important for pruning member function overloads according
6836 : : to their const/ref-qualifiers (since all 'this' conversions are at
6837 : : worst bad) without breaking -fpermissive. */
6838 : 228840882 : z_candidate *bad_cands = nullptr;
6839 : 228840882 : bool shortcut_bad_convs = true;
6840 : :
6841 : 328006364 : again:
6842 : 1851439806 : for (tree fn : lkp_range (fns))
6843 : : {
6844 : 1523446993 : if (which == templates && TREE_CODE (fn) != TEMPLATE_DECL)
6845 : : {
6846 : 216762149 : if (template_only)
6847 : 467143 : add_ignored_candidate (candidates, fn);
6848 : 216762149 : continue;
6849 : : }
6850 : 1306684844 : if (which == non_templates && TREE_CODE (fn) == TEMPLATE_DECL)
6851 : : {
6852 : 445555646 : add_ignored_candidate (&ignored_template_cands, fn);
6853 : 445555646 : continue;
6854 : : }
6855 : 164853761 : if ((check_converting && DECL_NONCONVERTING_P (fn))
6856 : 1011756234 : || (check_list_ctor && !is_list_ctor (fn)))
6857 : : {
6858 : 15366132 : add_ignored_candidate (candidates, fn);
6859 : 15366132 : continue;
6860 : : }
6861 : :
6862 : 845763066 : tree fn_first_arg = NULL_TREE;
6863 : 845763066 : const vec<tree, va_gc> *fn_args = args;
6864 : :
6865 : 845763066 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn))
6866 : : {
6867 : : /* Figure out where the object arg comes from. If this
6868 : : function is a non-static member and we didn't get an
6869 : : implicit object argument, move it out of args. */
6870 : 322368953 : if (first_arg == NULL_TREE)
6871 : : {
6872 : 5585985 : unsigned int ix;
6873 : 5585985 : tree arg;
6874 : 5585985 : vec<tree, va_gc> *tempvec;
6875 : 5585985 : vec_alloc (tempvec, args->length () - 1);
6876 : 14967371 : for (ix = 1; args->iterate (ix, &arg); ++ix)
6877 : 3795401 : tempvec->quick_push (arg);
6878 : 5585985 : non_static_args = tempvec;
6879 : 5585985 : first_arg = (*args)[0];
6880 : : }
6881 : :
6882 : : fn_first_arg = first_arg;
6883 : : fn_args = non_static_args;
6884 : : }
6885 : :
6886 : : /* Don't bother reversing an operator with two identical parameters. */
6887 : 523394113 : else if (vec_safe_length (args) == 2 && (flags & LOOKUP_REVERSED))
6888 : : {
6889 : 93213338 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
6890 : 93213338 : if (same_type_p (TREE_VALUE (parmlist),
6891 : : TREE_VALUE (TREE_CHAIN (parmlist))))
6892 : 43742490 : continue;
6893 : : }
6894 : :
6895 : : /* When considering reversed operator==, if there's a corresponding
6896 : : operator!= in the same scope, it's not a rewrite target. */
6897 : 802020576 : if (ne_context)
6898 : : {
6899 : 61971095 : if (TREE_CODE (ne_context) == NAMESPACE_DECL)
6900 : : {
6901 : : /* With argument-dependent lookup, fns can span multiple
6902 : : namespaces; make sure we look in the fn's namespace for a
6903 : : corresponding operator!=. */
6904 : 61890133 : tree fn_ns = decl_namespace_context (fn);
6905 : 61890133 : if (fn_ns != ne_context)
6906 : : {
6907 : 3885201 : ne_context = fn_ns;
6908 : 3885201 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6909 : 3885201 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6910 : : LOOK_want::NORMAL,
6911 : : /*complain*/false);
6912 : 3885201 : if (ne_fns == error_mark_node
6913 : 2396847 : || !is_overloaded_fn (ne_fns))
6914 : 1488354 : ne_fns = NULL_TREE;
6915 : : }
6916 : : }
6917 : 61971095 : bool found = false;
6918 : 703293767 : for (lkp_iterator ne (ne_fns); !found && ne; ++ne)
6919 : 656466881 : if (0 && !ne.using_p ()
6920 : : && DECL_NAMESPACE_SCOPE_P (fn)
6921 : : && DECL_CONTEXT (*ne) != DECL_CONTEXT (fn))
6922 : : /* ??? This kludge excludes inline namespace members for the H
6923 : : test in spaceship-eq15.C, but I don't see why we would want
6924 : : that behavior. Asked Core 2022-11-04. Disabling for now. */;
6925 : 656466881 : else if (fns_correspond (fn, *ne))
6926 : : {
6927 : : found = true;
6928 : : break;
6929 : : }
6930 : 61971095 : if (found)
6931 : 15144209 : continue;
6932 : : }
6933 : :
6934 : : /* Do not resolve any non-default function. Only the default version
6935 : : is resolvable (for the target_version attribute semantics.) */
6936 : 786876367 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
6937 : : && TREE_CODE (fn) == FUNCTION_DECL
6938 : : && !is_function_default_version (fn))
6939 : : {
6940 : : add_ignored_candidate (candidates, fn);
6941 : : continue;
6942 : : }
6943 : :
6944 : 786876367 : if (TREE_CODE (fn) == TEMPLATE_DECL)
6945 : 372416877 : add_template_candidate (candidates,
6946 : : fn,
6947 : : ctype,
6948 : : explicit_targs,
6949 : : fn_first_arg,
6950 : : fn_args,
6951 : : return_type,
6952 : : access_path,
6953 : : conversion_path,
6954 : : flags,
6955 : : strict,
6956 : : shortcut_bad_convs,
6957 : : complain);
6958 : : else
6959 : : {
6960 : 414459490 : add_function_candidate (candidates,
6961 : : fn,
6962 : : ctype,
6963 : : fn_first_arg,
6964 : : fn_args,
6965 : : access_path,
6966 : : conversion_path,
6967 : : flags,
6968 : : NULL,
6969 : : shortcut_bad_convs,
6970 : : complain);
6971 : 414459490 : if (perfect_candidate_p (*candidates))
6972 : 786862816 : seen_perfect = true;
6973 : : }
6974 : :
6975 : 786862816 : z_candidate *cand = *candidates;
6976 : 786862816 : if (cand->viable == 1)
6977 : 233282053 : seen_strictly_viable = true;
6978 : :
6979 : 786862816 : if (cand->viable == -1
6980 : 17298701 : && shortcut_bad_convs
6981 : 804160916 : && (missing_conversion_p (cand)
6982 : 11492075 : || TREE_CODE (cand->fn) == TEMPLATE_DECL))
6983 : : {
6984 : : /* This candidate has been tentatively marked non-strictly viable,
6985 : : and we didn't compute all argument conversions for it (having
6986 : : stopped at the first bad conversion). Move it to BAD_CANDS to
6987 : : to fully reconsider later if we don't find any strictly viable
6988 : : candidates. */
6989 : 5832707 : if (complain & (tf_error | tf_conv))
6990 : : {
6991 : 5577752 : *candidates = cand->next;
6992 : 5577752 : cand->next = bad_cands;
6993 : 5577752 : bad_cands = cand;
6994 : : }
6995 : : else
6996 : : /* But if we're in a SFINAE context, just mark this candidate as
6997 : : unviable outright and avoid potentially reconsidering it.
6998 : : This is safe to do because in a SFINAE context, performing a bad
6999 : : conversion is always an error (even with -fpermissive), so a
7000 : : non-strictly viable candidate is effectively unviable anyway. */
7001 : 254955 : cand->viable = 0;
7002 : : }
7003 : : }
7004 : 327992813 : if (which == non_templates && !seen_perfect)
7005 : : {
7006 : 99124080 : which = templates;
7007 : 99124080 : ignored_template_cands = nullptr;
7008 : 99124080 : goto again;
7009 : : }
7010 : 228868733 : else if (which == templates
7011 : 228868733 : && !seen_strictly_viable
7012 : : && shortcut_bad_convs
7013 : 37497025 : && bad_cands)
7014 : : {
7015 : : /* None of the candidates are strictly viable, so consider again those
7016 : : functions in BAD_CANDS, this time without shortcutting bad conversions
7017 : : so that all their argument conversions are computed. */
7018 : 114635 : which = either;
7019 : : fns = NULL_TREE;
7020 : 114635 : for (z_candidate *cand = bad_cands; cand; cand = cand->next)
7021 : : {
7022 : 73233 : tree fn = cand->fn;
7023 : 73233 : if (tree ti = cand->template_decl)
7024 : 72 : fn = TI_TEMPLATE (ti);
7025 : 73233 : fns = ovl_make (fn, fns);
7026 : : }
7027 : 41402 : shortcut_bad_convs = false;
7028 : 41402 : bad_cands = nullptr;
7029 : 41402 : goto again;
7030 : : }
7031 : :
7032 : 228827331 : if (complain & tf_error)
7033 : : {
7034 : : /* Remember any omitted candidates; we may want to print all candidates
7035 : : as part of overload resolution failure diagnostics. */
7036 : 371819346 : for (z_candidate *omitted_cands : { ignored_template_cands, bad_cands })
7037 : : {
7038 : 247879564 : z_candidate **omitted_cands_tail = &omitted_cands;
7039 : 293970115 : while (*omitted_cands_tail)
7040 : 46090551 : omitted_cands_tail = &(*omitted_cands_tail)->next;
7041 : 247879564 : *omitted_cands_tail = *candidates;
7042 : 247879564 : *candidates = omitted_cands;
7043 : : }
7044 : : }
7045 : : }
7046 : :
7047 : : /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
7048 : : -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
7049 : :
7050 : : static int
7051 : 10418385 : op_is_ordered (tree_code code)
7052 : : {
7053 : 10418169 : switch (code)
7054 : : {
7055 : : // 5. b @= a
7056 : 2323931 : case MODIFY_EXPR:
7057 : 2323931 : return (flag_strong_eval_order > 1 ? -1 : 0);
7058 : :
7059 : : // 6. a[b]
7060 : 504892 : case ARRAY_REF:
7061 : 504676 : return (flag_strong_eval_order > 1 ? 1 : 0);
7062 : :
7063 : : // 1. a.b
7064 : : // Not overloadable (yet).
7065 : : // 2. a->b
7066 : : // Only one argument.
7067 : : // 3. a->*b
7068 : 51084 : case MEMBER_REF:
7069 : : // 7. a << b
7070 : 51084 : case LSHIFT_EXPR:
7071 : : // 8. a >> b
7072 : 51084 : case RSHIFT_EXPR:
7073 : : // a && b
7074 : : // Predates P0145R3.
7075 : 51084 : case TRUTH_ANDIF_EXPR:
7076 : : // a || b
7077 : : // Predates P0145R3.
7078 : 51084 : case TRUTH_ORIF_EXPR:
7079 : : // a , b
7080 : : // Predates P0145R3.
7081 : 51084 : case COMPOUND_EXPR:
7082 : 51084 : return (flag_strong_eval_order ? 1 : 0);
7083 : :
7084 : : default:
7085 : : return 0;
7086 : : }
7087 : : }
7088 : :
7089 : : /* Subroutine of build_new_op: Add to CANDIDATES all candidates for the
7090 : : operator indicated by CODE/CODE2. This function calls itself recursively to
7091 : : handle C++20 rewritten comparison operator candidates. Returns NULL_TREE
7092 : : upon success, and error_mark_node if something went wrong that prevented
7093 : : us from performing overload resolution (e.g. ambiguous member name lookup).
7094 : :
7095 : : LOOKUPS, if non-NULL, is the set of pertinent namespace-scope operator
7096 : : overloads to consider. This parameter is used when instantiating a
7097 : : dependent operator expression and has the same structure as
7098 : : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS. */
7099 : :
7100 : : static tree
7101 : 21311305 : add_operator_candidates (z_candidate **candidates,
7102 : : tree_code code, tree_code code2,
7103 : : vec<tree, va_gc> *arglist, tree lookups,
7104 : : int flags, tsubst_flags_t complain)
7105 : : {
7106 : 21311305 : z_candidate *start_candidates = *candidates;
7107 : 21311305 : bool ismodop = code2 != ERROR_MARK;
7108 : 21311305 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7109 : :
7110 : : /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to
7111 : : rewrite from, and also when we're looking for the e.g. < operator to use
7112 : : on the result of <=>. In the latter case, we don't want the flag set in
7113 : : the candidate, we just want to suppress looking for rewrites. */
7114 : 21311305 : bool rewritten = (flags & LOOKUP_REWRITTEN);
7115 : 21311305 : if (rewritten && code != EQ_EXPR && code != SPACESHIP_EXPR)
7116 : 413780 : flags &= ~LOOKUP_REWRITTEN;
7117 : :
7118 : 20279863 : bool memonly = false;
7119 : 20279863 : switch (code)
7120 : : {
7121 : : /* =, ->, [], () must be non-static member functions. */
7122 : 4112198 : case MODIFY_EXPR:
7123 : 4112198 : if (code2 != NOP_EXPR)
7124 : : break;
7125 : : /* FALLTHRU */
7126 : : case COMPONENT_REF:
7127 : : case ARRAY_REF:
7128 : : memonly = true;
7129 : : break;
7130 : :
7131 : : default:
7132 : : break;
7133 : : }
7134 : :
7135 : : /* Add namespace-scope operators to the list of functions to
7136 : : consider. */
7137 : : if (!memonly)
7138 : : {
7139 : 18177590 : tree fns;
7140 : 18177590 : if (!lookups)
7141 : 12982100 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
7142 : : /* If LOOKUPS is non-NULL, then we're instantiating a dependent operator
7143 : : expression, and LOOKUPS is the result of stage 1 name lookup. */
7144 : 5195490 : else if (tree found = purpose_member (fnname, lookups))
7145 : 1588209 : fns = TREE_VALUE (found);
7146 : : else
7147 : : fns = NULL_TREE;
7148 : 18177590 : fns = lookup_arg_dependent (fnname, fns, arglist);
7149 : 18177590 : add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
7150 : : NULL_TREE, false, NULL_TREE, NULL_TREE,
7151 : : flags, candidates, complain);
7152 : : }
7153 : :
7154 : : /* Add class-member operators to the candidate set. */
7155 : 21311272 : tree arg1_type = TREE_TYPE ((*arglist)[0]);
7156 : 21311272 : unsigned nargs = arglist->length () > 1 ? 2 : 1;
7157 : 17298549 : tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE;
7158 : 21311272 : if (CLASS_TYPE_P (arg1_type))
7159 : : {
7160 : 12009839 : tree fns = lookup_fnfields (arg1_type, fnname, 1, complain);
7161 : 12009839 : if (fns == error_mark_node)
7162 : : return error_mark_node;
7163 : 12009827 : if (fns)
7164 : : {
7165 : 6090683 : if (code == ARRAY_REF)
7166 : : {
7167 : 504698 : vec<tree,va_gc> *restlist = make_tree_vector ();
7168 : 1009396 : for (unsigned i = 1; i < nargs; ++i)
7169 : 504698 : vec_safe_push (restlist, (*arglist)[i]);
7170 : 504698 : z_candidate *save_cand = *candidates;
7171 : 1009396 : add_candidates (BASELINK_FUNCTIONS (fns),
7172 : 504698 : (*arglist)[0], restlist, NULL_TREE,
7173 : : NULL_TREE, false,
7174 : 504698 : BASELINK_BINFO (fns),
7175 : 504698 : BASELINK_ACCESS_BINFO (fns),
7176 : : flags, candidates, complain);
7177 : : /* Release the vec if we didn't add a candidate that uses it. */
7178 : 505229 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7179 : 505229 : if (c->args == restlist)
7180 : : {
7181 : 504698 : restlist = NULL;
7182 : 504698 : break;
7183 : : }
7184 : 504698 : release_tree_vector (restlist);
7185 : : }
7186 : : else
7187 : 5585985 : add_candidates (BASELINK_FUNCTIONS (fns),
7188 : : NULL_TREE, arglist, NULL_TREE,
7189 : : NULL_TREE, false,
7190 : 5585985 : BASELINK_BINFO (fns),
7191 : 5585985 : BASELINK_ACCESS_BINFO (fns),
7192 : : flags, candidates, complain);
7193 : : }
7194 : : }
7195 : : /* Per [over.match.oper]3.2, if no operand has a class type, then
7196 : : only non-member functions that have type T1 or reference to
7197 : : cv-qualified-opt T1 for the first argument, if the first argument
7198 : : has an enumeration type, or T2 or reference to cv-qualified-opt
7199 : : T2 for the second argument, if the second argument has an
7200 : : enumeration type. Filter out those that don't match. */
7201 : 9301433 : else if (! arg2_type || ! CLASS_TYPE_P (arg2_type))
7202 : : {
7203 : : struct z_candidate **candp, **next;
7204 : :
7205 : 190315272 : for (candp = candidates; *candp != start_candidates; candp = next)
7206 : : {
7207 : 181275654 : unsigned i;
7208 : 181275654 : z_candidate *cand = *candp;
7209 : 181275654 : next = &cand->next;
7210 : :
7211 : 181275654 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
7212 : :
7213 : 536282424 : for (i = 0; i < nargs; ++i)
7214 : : {
7215 : 358601746 : tree parmtype = TREE_VALUE (parmlist);
7216 : 358601746 : tree argtype = unlowered_expr_type ((*arglist)[i]);
7217 : :
7218 : 358601746 : if (TYPE_REF_P (parmtype))
7219 : 277566059 : parmtype = TREE_TYPE (parmtype);
7220 : 358601746 : if (TREE_CODE (argtype) == ENUMERAL_TYPE
7221 : 704133712 : && (same_type_ignoring_top_level_qualifiers_p
7222 : 345531966 : (argtype, parmtype)))
7223 : : break;
7224 : :
7225 : 355006770 : parmlist = TREE_CHAIN (parmlist);
7226 : : }
7227 : :
7228 : : /* No argument has an appropriate type, so remove this
7229 : : candidate function from the list. */
7230 : 181275654 : if (i == nargs)
7231 : : {
7232 : 177680678 : *candp = cand->next;
7233 : 177680678 : next = candp;
7234 : : }
7235 : : }
7236 : : }
7237 : :
7238 : 21311260 : if (!rewritten)
7239 : : {
7240 : : /* The standard says to rewrite built-in candidates, too,
7241 : : but there's no point. */
7242 : 17054323 : add_builtin_candidates (candidates, code, code2, fnname, arglist,
7243 : : flags, complain);
7244 : :
7245 : : /* Maybe add C++20 rewritten comparison candidates. */
7246 : 17054323 : tree_code rewrite_code = ERROR_MARK;
7247 : 17054323 : if (cxx_dialect >= cxx20
7248 : 9322434 : && nargs == 2
7249 : 24517461 : && (OVERLOAD_TYPE_P (arg1_type) || OVERLOAD_TYPE_P (arg2_type)))
7250 : 7463135 : switch (code)
7251 : : {
7252 : 537358 : case LT_EXPR:
7253 : 537358 : case LE_EXPR:
7254 : 537358 : case GT_EXPR:
7255 : 537358 : case GE_EXPR:
7256 : 537358 : case SPACESHIP_EXPR:
7257 : 537358 : rewrite_code = SPACESHIP_EXPR;
7258 : 537358 : break;
7259 : :
7260 : : case NE_EXPR:
7261 : : case EQ_EXPR:
7262 : : rewrite_code = EQ_EXPR;
7263 : : break;
7264 : :
7265 : : default:;
7266 : : }
7267 : :
7268 : 537358 : if (rewrite_code)
7269 : : {
7270 : 2634277 : tree r;
7271 : 2634277 : flags |= LOOKUP_REWRITTEN;
7272 : 2634277 : if (rewrite_code != code)
7273 : : {
7274 : : /* Add rewritten candidates in same order. */
7275 : 1208665 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7276 : : arglist, lookups, flags, complain);
7277 : 1208665 : if (r == error_mark_node)
7278 : : return error_mark_node;
7279 : : }
7280 : :
7281 : 2634271 : z_candidate *save_cand = *candidates;
7282 : :
7283 : : /* Add rewritten candidates in reverse order. */
7284 : 2634271 : flags |= LOOKUP_REVERSED;
7285 : 2634271 : vec<tree,va_gc> *revlist = make_tree_vector ();
7286 : 2634271 : revlist->quick_push ((*arglist)[1]);
7287 : 2634271 : revlist->quick_push ((*arglist)[0]);
7288 : 2634271 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7289 : : revlist, lookups, flags, complain);
7290 : 2634271 : if (r == error_mark_node)
7291 : : return error_mark_node;
7292 : :
7293 : : /* Release the vec if we didn't add a candidate that uses it. */
7294 : 2678918 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7295 : 1219162 : if (c->args == revlist)
7296 : : {
7297 : : revlist = NULL;
7298 : : break;
7299 : : }
7300 : 2634271 : release_tree_vector (revlist);
7301 : : }
7302 : : }
7303 : :
7304 : : return NULL_TREE;
7305 : : }
7306 : :
7307 : : tree
7308 : 173842452 : build_new_op (const op_location_t &loc, enum tree_code code, int flags,
7309 : : tree arg1, tree arg2, tree arg3, tree lookups,
7310 : : tree *overload, tsubst_flags_t complain)
7311 : : {
7312 : 173842452 : struct z_candidate *candidates = 0, *cand;
7313 : 173842452 : releasing_vec arglist;
7314 : 173842452 : tree result = NULL_TREE;
7315 : 173842452 : bool result_valid_p = false;
7316 : 173842452 : enum tree_code code2 = ERROR_MARK;
7317 : 173842452 : enum tree_code code_orig_arg1 = ERROR_MARK;
7318 : 173842452 : enum tree_code code_orig_arg2 = ERROR_MARK;
7319 : 173842452 : bool strict_p;
7320 : 173842452 : bool any_viable_p;
7321 : :
7322 : 173842452 : auto_cond_timevar tv (TV_OVERLOAD);
7323 : :
7324 : 173842452 : if (error_operand_p (arg1)
7325 : 173838898 : || error_operand_p (arg2)
7326 : 347675336 : || error_operand_p (arg3))
7327 : 9568 : return error_mark_node;
7328 : :
7329 : 173832884 : conversion_obstack_sentinel cos;
7330 : :
7331 : 173832884 : bool ismodop = code == MODIFY_EXPR;
7332 : 173832884 : if (ismodop)
7333 : : {
7334 : 8251252 : code2 = TREE_CODE (arg3);
7335 : 8251252 : arg3 = NULL_TREE;
7336 : : }
7337 : :
7338 : 173832884 : tree arg1_type = unlowered_expr_type (arg1);
7339 : 173832884 : tree arg2_type = arg2 ? unlowered_expr_type (arg2) : NULL_TREE;
7340 : :
7341 : 173832884 : arg1 = prep_operand (arg1);
7342 : :
7343 : 173832884 : switch (code)
7344 : : {
7345 : 0 : case NEW_EXPR:
7346 : 0 : case VEC_NEW_EXPR:
7347 : 0 : case VEC_DELETE_EXPR:
7348 : 0 : case DELETE_EXPR:
7349 : : /* Use build_operator_new_call and build_op_delete_call instead. */
7350 : 0 : gcc_unreachable ();
7351 : :
7352 : 0 : case CALL_EXPR:
7353 : : /* Use build_op_call instead. */
7354 : 0 : gcc_unreachable ();
7355 : :
7356 : 14064193 : case TRUTH_ORIF_EXPR:
7357 : 14064193 : case TRUTH_ANDIF_EXPR:
7358 : 14064193 : case TRUTH_AND_EXPR:
7359 : 14064193 : case TRUTH_OR_EXPR:
7360 : : /* These are saved for the sake of warn_logical_operator. */
7361 : 14064193 : code_orig_arg1 = TREE_CODE (arg1);
7362 : 14064193 : code_orig_arg2 = TREE_CODE (arg2);
7363 : 14064193 : break;
7364 : 38518672 : case GT_EXPR:
7365 : 38518672 : case LT_EXPR:
7366 : 38518672 : case GE_EXPR:
7367 : 38518672 : case LE_EXPR:
7368 : 38518672 : case EQ_EXPR:
7369 : 38518672 : case NE_EXPR:
7370 : : /* These are saved for the sake of maybe_warn_bool_compare. */
7371 : 38518672 : code_orig_arg1 = TREE_CODE (arg1_type);
7372 : 38518672 : code_orig_arg2 = TREE_CODE (arg2_type);
7373 : 38518672 : break;
7374 : :
7375 : : default:
7376 : : break;
7377 : : }
7378 : :
7379 : 173832884 : arg2 = prep_operand (arg2);
7380 : 173832884 : arg3 = prep_operand (arg3);
7381 : :
7382 : 173832884 : if (code == COND_EXPR)
7383 : : /* Use build_conditional_expr instead. */
7384 : 0 : gcc_unreachable ();
7385 : 173832884 : else if (! OVERLOAD_TYPE_P (arg1_type)
7386 : 330497136 : && (! arg2 || ! OVERLOAD_TYPE_P (arg2_type)))
7387 : 156364515 : goto builtin;
7388 : :
7389 : 17468369 : if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7390 : : {
7391 : 218027 : arg2 = integer_zero_node;
7392 : 218027 : arg2_type = integer_type_node;
7393 : : }
7394 : :
7395 : 17468369 : arglist->quick_push (arg1);
7396 : 17468369 : if (arg2 != NULL_TREE)
7397 : 13455646 : arglist->quick_push (arg2);
7398 : 17468369 : if (arg3 != NULL_TREE)
7399 : 0 : arglist->quick_push (arg3);
7400 : :
7401 : 17468369 : result = add_operator_candidates (&candidates, code, code2, arglist,
7402 : : lookups, flags, complain);
7403 : 17468336 : if (result == error_mark_node)
7404 : : return error_mark_node;
7405 : :
7406 : 17468324 : switch (code)
7407 : : {
7408 : : case COMPOUND_EXPR:
7409 : : case ADDR_EXPR:
7410 : : /* For these, the built-in candidates set is empty
7411 : : [over.match.oper]/3. We don't want non-strict matches
7412 : : because exact matches are always possible with built-in
7413 : : operators. The built-in candidate set for COMPONENT_REF
7414 : : would be empty too, but since there are no such built-in
7415 : : operators, we accept non-strict matches for them. */
7416 : : strict_p = true;
7417 : : break;
7418 : :
7419 : 15839160 : default:
7420 : 15839160 : strict_p = false;
7421 : 15839160 : break;
7422 : : }
7423 : :
7424 : 17468324 : candidates = splice_viable (candidates, strict_p, &any_viable_p);
7425 : 17468324 : if (!any_viable_p)
7426 : : {
7427 : 1648959 : switch (code)
7428 : : {
7429 : 140 : case POSTINCREMENT_EXPR:
7430 : 140 : case POSTDECREMENT_EXPR:
7431 : : /* Don't try anything fancy if we're not allowed to produce
7432 : : errors. */
7433 : 140 : if (!(complain & tf_error))
7434 : : return error_mark_node;
7435 : :
7436 : : /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
7437 : : distinguish between prefix and postfix ++ and
7438 : : operator++() was used for both, so we allow this with
7439 : : -fpermissive. */
7440 : : else
7441 : : {
7442 : 43 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7443 : 31 : const char *msg = (flag_permissive)
7444 : 43 : ? G_("no %<%D(int)%> declared for postfix %qs,"
7445 : : " trying prefix operator instead")
7446 : : : G_("no %<%D(int)%> declared for postfix %qs");
7447 : 43 : permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
7448 : : }
7449 : :
7450 : 43 : if (!flag_permissive)
7451 : 31 : return error_mark_node;
7452 : :
7453 : 12 : if (code == POSTINCREMENT_EXPR)
7454 : : code = PREINCREMENT_EXPR;
7455 : : else
7456 : 0 : code = PREDECREMENT_EXPR;
7457 : 12 : result = build_new_op (loc, code, flags, arg1, NULL_TREE,
7458 : : NULL_TREE, lookups, overload, complain);
7459 : 12 : break;
7460 : :
7461 : : /* The caller will deal with these. */
7462 : : case ADDR_EXPR:
7463 : : case COMPOUND_EXPR:
7464 : : case COMPONENT_REF:
7465 : : case CO_AWAIT_EXPR:
7466 : : result = NULL_TREE;
7467 : : result_valid_p = true;
7468 : : break;
7469 : :
7470 : 15829 : default:
7471 : 15829 : if (complain & tf_error)
7472 : : {
7473 : : /* If one of the arguments of the operator represents
7474 : : an invalid use of member function pointer, try to report
7475 : : a meaningful error ... */
7476 : 1346 : if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
7477 : 1343 : || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
7478 : 2683 : || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
7479 : : /* We displayed the error message. */;
7480 : : else
7481 : : {
7482 : : /* ... Otherwise, report the more generic
7483 : : "no matching operator found" error */
7484 : 1337 : auto_diagnostic_group d;
7485 : 1337 : op_error (loc, code, code2, arg1, arg2, arg3, false);
7486 : 1337 : print_z_candidates (loc, candidates);
7487 : 1337 : }
7488 : : }
7489 : 15829 : result = error_mark_node;
7490 : 15829 : break;
7491 : : }
7492 : : }
7493 : : else
7494 : : {
7495 : 15819365 : cand = tourney (candidates, complain);
7496 : 15819365 : if (cand == 0)
7497 : : {
7498 : 190 : if (complain & tf_error)
7499 : : {
7500 : 127 : auto_diagnostic_group d;
7501 : 127 : op_error (loc, code, code2, arg1, arg2, arg3, true);
7502 : 127 : print_z_candidates (loc, candidates);
7503 : 127 : }
7504 : 190 : result = error_mark_node;
7505 : 190 : if (overload)
7506 : 171 : *overload = error_mark_node;
7507 : : }
7508 : 15819175 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
7509 : : {
7510 : 12203199 : if (overload)
7511 : : {
7512 : 9362274 : if (cand->rewritten ())
7513 : : /* build_min_non_dep_op_overload needs to know whether the
7514 : : candidate is rewritten/reversed. */
7515 : 790277 : *overload = build_tree_list (build_int_cst (integer_type_node,
7516 : 790277 : cand->flags),
7517 : : cand->fn);
7518 : : else
7519 : 8571997 : *overload = cand->fn;
7520 : : }
7521 : :
7522 : 12203199 : if (resolve_args (arglist, complain) == NULL)
7523 : 2 : result = error_mark_node;
7524 : : else
7525 : : {
7526 : 12203197 : tsubst_flags_t ocomplain = complain;
7527 : 12203197 : if (cand->rewritten ())
7528 : : /* We'll wrap this call in another one. */
7529 : 790504 : ocomplain &= ~tf_decltype;
7530 : 12203197 : if (cand->reversed ())
7531 : : {
7532 : : /* We swapped these in add_candidate, swap them back now. */
7533 : 16674 : std::swap (cand->convs[0], cand->convs[1]);
7534 : 16674 : if (cand->fn == current_function_decl)
7535 : 1 : warning_at (loc, 0, "in C++20 this comparison calls the "
7536 : : "current function recursively with reversed "
7537 : : "arguments");
7538 : : }
7539 : 12203197 : result = build_over_call (cand, LOOKUP_NORMAL, ocomplain);
7540 : : }
7541 : :
7542 : 22623139 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7543 : : /* There won't be a CALL_EXPR. */;
7544 : 10419928 : else if (result && result != error_mark_node)
7545 : : {
7546 : 10418169 : tree call = extract_call_expr (result);
7547 : 10418169 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7548 : :
7549 : : /* Specify evaluation order as per P0145R2. */
7550 : 10418169 : CALL_EXPR_ORDERED_ARGS (call) = false;
7551 : 10418169 : switch (op_is_ordered (code))
7552 : : {
7553 : 2300365 : case -1:
7554 : 2300365 : CALL_EXPR_REVERSE_ARGS (call) = true;
7555 : 2300365 : break;
7556 : :
7557 : 554693 : case 1:
7558 : 554693 : CALL_EXPR_ORDERED_ARGS (call) = true;
7559 : 554693 : break;
7560 : :
7561 : : default:
7562 : : break;
7563 : : }
7564 : : }
7565 : :
7566 : : /* If this was a C++20 rewritten comparison, adjust the result. */
7567 : 12203196 : if (cand->rewritten ())
7568 : : {
7569 : 790504 : switch (code)
7570 : : {
7571 : 8616 : case EQ_EXPR:
7572 : 8616 : gcc_checking_assert (cand->reversed ());
7573 : 376159 : gcc_fallthrough ();
7574 : 376159 : case NE_EXPR:
7575 : 376159 : if (result == error_mark_node)
7576 : : ;
7577 : : /* If a rewritten operator== candidate is selected by
7578 : : overload resolution for an operator @, its return type
7579 : : shall be cv bool.... */
7580 : 376155 : else if (TREE_CODE (TREE_TYPE (result)) != BOOLEAN_TYPE)
7581 : : {
7582 : 44 : if (complain & tf_error)
7583 : : {
7584 : 6 : auto_diagnostic_group d;
7585 : 6 : error_at (loc, "return type of %qD is not %qs",
7586 : : cand->fn, "bool");
7587 : 6 : inform (loc, "used as rewritten candidate for "
7588 : : "comparison of %qT and %qT",
7589 : : arg1_type, arg2_type);
7590 : 6 : }
7591 : 44 : result = error_mark_node;
7592 : : }
7593 : 376111 : else if (code == NE_EXPR)
7594 : : /* !(y == x) or !(x == y) */
7595 : 367520 : result = build1_loc (loc, TRUTH_NOT_EXPR,
7596 : : boolean_type_node, result);
7597 : : break;
7598 : :
7599 : : /* If a rewritten operator<=> candidate is selected by
7600 : : overload resolution for an operator @, x @ y is
7601 : : interpreted as 0 @ (y <=> x) if the selected candidate is
7602 : : a synthesized candidate with reversed order of parameters,
7603 : : or (x <=> y) @ 0 otherwise, using the selected rewritten
7604 : : operator<=> candidate. */
7605 : 454 : case SPACESHIP_EXPR:
7606 : 454 : if (!cand->reversed ())
7607 : : /* We're in the build_new_op call below for an outer
7608 : : reversed call; we don't need to do anything more. */
7609 : : break;
7610 : 414118 : gcc_fallthrough ();
7611 : 414118 : case LT_EXPR:
7612 : 414118 : case LE_EXPR:
7613 : 414118 : case GT_EXPR:
7614 : 414118 : case GE_EXPR:
7615 : 414118 : {
7616 : 414118 : tree lhs = result;
7617 : 414118 : tree rhs = integer_zero_node;
7618 : 414118 : if (cand->reversed ())
7619 : 955 : std::swap (lhs, rhs);
7620 : 414118 : warning_sentinel ws (warn_zero_as_null_pointer_constant);
7621 : 414118 : result = build_new_op (loc, code,
7622 : : LOOKUP_NORMAL|LOOKUP_REWRITTEN,
7623 : : lhs, rhs, NULL_TREE, lookups,
7624 : : NULL, complain);
7625 : 414118 : }
7626 : 414118 : break;
7627 : :
7628 : 0 : default:
7629 : 0 : gcc_unreachable ();
7630 : : }
7631 : : }
7632 : :
7633 : : /* In an expression of the form `a[]' where cand->fn
7634 : : which is operator[] turns out to be a static member function,
7635 : : `a' is none-the-less evaluated. */
7636 : 12203196 : if (code == ARRAY_REF)
7637 : 504676 : result = keep_unused_object_arg (result, arg1, cand->fn);
7638 : : }
7639 : : else
7640 : : {
7641 : : /* Give any warnings we noticed during overload resolution. */
7642 : 3615976 : if (cand->warnings && (complain & tf_warning))
7643 : : {
7644 : : struct candidate_warning *w;
7645 : 0 : for (w = cand->warnings; w; w = w->next)
7646 : 0 : joust (cand, w->loser, 1, complain);
7647 : : }
7648 : :
7649 : : /* Check for comparison of different enum types. */
7650 : 3615976 : switch (code)
7651 : : {
7652 : 2786699 : case GT_EXPR:
7653 : 2786699 : case LT_EXPR:
7654 : 2786699 : case GE_EXPR:
7655 : 2786699 : case LE_EXPR:
7656 : 2786699 : case EQ_EXPR:
7657 : 2786699 : case NE_EXPR:
7658 : 2786699 : if (TREE_CODE (arg1_type) == ENUMERAL_TYPE
7659 : 2651765 : && TREE_CODE (arg2_type) == ENUMERAL_TYPE
7660 : 5360037 : && (TYPE_MAIN_VARIANT (arg1_type)
7661 : 2573338 : != TYPE_MAIN_VARIANT (arg2_type)))
7662 : : {
7663 : 79 : if (cxx_dialect >= cxx26
7664 : 24 : && (complain & tf_warning_or_error) == 0)
7665 : 1 : result = error_mark_node;
7666 : 78 : else if (cxx_dialect >= cxx26 || (complain & tf_warning))
7667 : 129 : emit_diagnostic ((cxx_dialect >= cxx26
7668 : : ? diagnostics::kind::pedwarn
7669 : : : diagnostics::kind::warning),
7670 : 76 : loc, OPT_Wenum_compare,
7671 : : "comparison between %q#T and %q#T",
7672 : : arg1_type, arg2_type);
7673 : : }
7674 : : break;
7675 : : default:
7676 : : break;
7677 : : }
7678 : :
7679 : : /* "If a built-in candidate is selected by overload resolution, the
7680 : : operands of class type are converted to the types of the
7681 : : corresponding parameters of the selected operation function,
7682 : : except that the second standard conversion sequence of a
7683 : : user-defined conversion sequence (12.3.3.1.2) is not applied." */
7684 : 3615976 : conversion *conv = cand->convs[0];
7685 : 3615976 : if (conv->user_conv_p)
7686 : : {
7687 : 31510 : conv = strip_standard_conversion (conv);
7688 : 31510 : arg1 = convert_like (conv, arg1, complain);
7689 : : }
7690 : :
7691 : 3615976 : if (arg2)
7692 : : {
7693 : 3124020 : conv = cand->convs[1];
7694 : 3124020 : if (conv->user_conv_p)
7695 : : {
7696 : 8355 : conv = strip_standard_conversion (conv);
7697 : 8355 : arg2 = convert_like (conv, arg2, complain);
7698 : : }
7699 : : }
7700 : :
7701 : 3615976 : if (arg3)
7702 : : {
7703 : 0 : conv = cand->convs[2];
7704 : 0 : if (conv->user_conv_p)
7705 : : {
7706 : 0 : conv = strip_standard_conversion (conv);
7707 : 0 : arg3 = convert_like (conv, arg3, complain);
7708 : : }
7709 : : }
7710 : : }
7711 : : }
7712 : :
7713 : 17468193 : if (result || result_valid_p)
7714 : : return result;
7715 : :
7716 : 3615975 : builtin:
7717 : 159980490 : switch (code)
7718 : : {
7719 : 4139485 : case MODIFY_EXPR:
7720 : 4139485 : return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
7721 : :
7722 : 16578956 : case INDIRECT_REF:
7723 : 16578956 : return cp_build_indirect_ref (loc, arg1, RO_UNARY_STAR, complain);
7724 : :
7725 : 14063604 : case TRUTH_ANDIF_EXPR:
7726 : 14063604 : case TRUTH_ORIF_EXPR:
7727 : 14063604 : case TRUTH_AND_EXPR:
7728 : 14063604 : case TRUTH_OR_EXPR:
7729 : 14063604 : if ((complain & tf_warning) && !processing_template_decl)
7730 : 5669674 : warn_logical_operator (loc, code, boolean_type_node,
7731 : : code_orig_arg1, arg1,
7732 : : code_orig_arg2, arg2);
7733 : : /* Fall through. */
7734 : 47592342 : case GT_EXPR:
7735 : 47592342 : case LT_EXPR:
7736 : 47592342 : case GE_EXPR:
7737 : 47592342 : case LE_EXPR:
7738 : 47592342 : case EQ_EXPR:
7739 : 47592342 : case NE_EXPR:
7740 : 47592342 : if ((complain & tf_warning)
7741 : 44713317 : && ((code_orig_arg1 == BOOLEAN_TYPE)
7742 : 44713317 : ^ (code_orig_arg2 == BOOLEAN_TYPE)))
7743 : 12199 : maybe_warn_bool_compare (loc, code, arg1, arg2);
7744 : 44713317 : if (complain & tf_warning && warn_tautological_compare)
7745 : 404651 : warn_tautological_cmp (loc, code, arg1, arg2);
7746 : : /* Fall through. */
7747 : 109089427 : case SPACESHIP_EXPR:
7748 : 109089427 : case PLUS_EXPR:
7749 : 109089427 : case MINUS_EXPR:
7750 : 109089427 : case MULT_EXPR:
7751 : 109089427 : case TRUNC_DIV_EXPR:
7752 : 109089427 : case MAX_EXPR:
7753 : 109089427 : case MIN_EXPR:
7754 : 109089427 : case LSHIFT_EXPR:
7755 : 109089427 : case RSHIFT_EXPR:
7756 : 109089427 : case TRUNC_MOD_EXPR:
7757 : 109089427 : case BIT_AND_EXPR:
7758 : 109089427 : case BIT_IOR_EXPR:
7759 : 109089427 : case BIT_XOR_EXPR:
7760 : 109089427 : return cp_build_binary_op (loc, code, arg1, arg2, complain);
7761 : :
7762 : 26026666 : case UNARY_PLUS_EXPR:
7763 : 26026666 : case NEGATE_EXPR:
7764 : 26026666 : case BIT_NOT_EXPR:
7765 : 26026666 : case TRUTH_NOT_EXPR:
7766 : 26026666 : case PREINCREMENT_EXPR:
7767 : 26026666 : case POSTINCREMENT_EXPR:
7768 : 26026666 : case PREDECREMENT_EXPR:
7769 : 26026666 : case POSTDECREMENT_EXPR:
7770 : 26026666 : case REALPART_EXPR:
7771 : 26026666 : case IMAGPART_EXPR:
7772 : 26026666 : case ABS_EXPR:
7773 : 26026666 : case CO_AWAIT_EXPR:
7774 : 26026666 : return cp_build_unary_op (code, arg1, false, complain);
7775 : :
7776 : 1731405 : case ARRAY_REF:
7777 : 1731405 : return cp_build_array_ref (input_location, arg1, arg2, complain);
7778 : :
7779 : 760 : case MEMBER_REF:
7780 : 760 : return build_m_component_ref (cp_build_indirect_ref (loc, arg1,
7781 : : RO_ARROW_STAR,
7782 : : complain),
7783 : 760 : arg2, complain);
7784 : :
7785 : : /* The caller will deal with these. */
7786 : : case ADDR_EXPR:
7787 : : case COMPONENT_REF:
7788 : : case COMPOUND_EXPR:
7789 : : return NULL_TREE;
7790 : :
7791 : 0 : default:
7792 : 0 : gcc_unreachable ();
7793 : : }
7794 : : return NULL_TREE;
7795 : 173842416 : }
7796 : :
7797 : : /* Build a new call to operator[]. This may change ARGS. */
7798 : :
7799 : : tree
7800 : 253 : build_op_subscript (const op_location_t &loc, tree obj,
7801 : : vec<tree, va_gc> **args, tree *overload,
7802 : : tsubst_flags_t complain)
7803 : : {
7804 : 253 : struct z_candidate *candidates = 0, *cand;
7805 : 253 : tree fns, first_mem_arg = NULL_TREE;
7806 : 253 : bool any_viable_p;
7807 : 253 : tree result = NULL_TREE;
7808 : :
7809 : 253 : auto_cond_timevar tv (TV_OVERLOAD);
7810 : :
7811 : 253 : obj = mark_lvalue_use (obj);
7812 : :
7813 : 253 : if (error_operand_p (obj))
7814 : 0 : return error_mark_node;
7815 : :
7816 : 253 : tree type = TREE_TYPE (obj);
7817 : :
7818 : 253 : obj = prep_operand (obj);
7819 : :
7820 : 253 : if (TYPE_BINFO (type))
7821 : : {
7822 : 253 : fns = lookup_fnfields (TYPE_BINFO (type), ovl_op_identifier (ARRAY_REF),
7823 : : 1, complain);
7824 : 253 : if (fns == error_mark_node)
7825 : : return error_mark_node;
7826 : : }
7827 : : else
7828 : : fns = NULL_TREE;
7829 : :
7830 : 253 : if (args != NULL && *args != NULL)
7831 : : {
7832 : 253 : *args = resolve_args (*args, complain);
7833 : 253 : if (*args == NULL)
7834 : 0 : return error_mark_node;
7835 : : }
7836 : :
7837 : 253 : conversion_obstack_sentinel cos;
7838 : :
7839 : 253 : if (fns)
7840 : : {
7841 : 251 : first_mem_arg = obj;
7842 : :
7843 : 251 : add_candidates (BASELINK_FUNCTIONS (fns),
7844 : : first_mem_arg, *args, NULL_TREE,
7845 : : NULL_TREE, false,
7846 : 251 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
7847 : : LOOKUP_NORMAL, &candidates, complain);
7848 : : }
7849 : :
7850 : : /* Be strict here because if we choose a bad conversion candidate, the
7851 : : errors we get won't mention the call context. */
7852 : 253 : candidates = splice_viable (candidates, true, &any_viable_p);
7853 : 253 : if (!any_viable_p)
7854 : : {
7855 : 37 : if (complain & tf_error)
7856 : : {
7857 : 1 : auto_diagnostic_group d;
7858 : 2 : error ("no match for call to %<%T::operator[] (%A)%>",
7859 : 1 : TREE_TYPE (obj), build_tree_list_vec (*args));
7860 : 1 : print_z_candidates (loc, candidates);
7861 : 1 : }
7862 : 37 : result = error_mark_node;
7863 : : }
7864 : : else
7865 : : {
7866 : 216 : cand = tourney (candidates, complain);
7867 : 216 : if (cand == 0)
7868 : : {
7869 : 0 : if (complain & tf_error)
7870 : : {
7871 : 0 : auto_diagnostic_group d;
7872 : 0 : error ("call of %<%T::operator[] (%A)%> is ambiguous",
7873 : 0 : TREE_TYPE (obj), build_tree_list_vec (*args));
7874 : 0 : print_z_candidates (loc, candidates);
7875 : 0 : }
7876 : 0 : result = error_mark_node;
7877 : : }
7878 : 216 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
7879 : 216 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
7880 : 432 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, ARRAY_REF))
7881 : : {
7882 : 216 : if (overload)
7883 : 216 : *overload = cand->fn;
7884 : 216 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
7885 : 432 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7886 : : /* There won't be a CALL_EXPR. */;
7887 : 216 : else if (result && result != error_mark_node)
7888 : : {
7889 : 216 : tree call = extract_call_expr (result);
7890 : 216 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7891 : :
7892 : : /* Specify evaluation order as per P0145R2. */
7893 : 216 : CALL_EXPR_ORDERED_ARGS (call) = op_is_ordered (ARRAY_REF) == 1;
7894 : : }
7895 : :
7896 : : /* In an expression of the form `a[]' where cand->fn
7897 : : which is operator[] turns out to be a static member function,
7898 : : `a' is none-the-less evaluated. */
7899 : 216 : result = keep_unused_object_arg (result, obj, cand->fn);
7900 : : }
7901 : : else
7902 : 0 : gcc_unreachable ();
7903 : : }
7904 : :
7905 : 253 : return result;
7906 : 253 : }
7907 : :
7908 : : /* CALL was returned by some call-building function; extract the actual
7909 : : CALL_EXPR from any bits that have been tacked on, e.g. by
7910 : : convert_from_reference. */
7911 : :
7912 : : tree
7913 : 25933753 : extract_call_expr (tree call)
7914 : : {
7915 : 25933973 : while (TREE_CODE (call) == COMPOUND_EXPR)
7916 : 220 : call = TREE_OPERAND (call, 1);
7917 : 25933753 : if (REFERENCE_REF_P (call))
7918 : 7497738 : call = TREE_OPERAND (call, 0);
7919 : 25933753 : if (TREE_CODE (call) == TARGET_EXPR)
7920 : 1303062 : call = TARGET_EXPR_INITIAL (call);
7921 : :
7922 : 25933753 : if (TREE_CODE (call) != CALL_EXPR
7923 : 754829 : && TREE_CODE (call) != AGGR_INIT_EXPR
7924 : 654231 : && call != error_mark_node)
7925 : 654213 : return NULL_TREE;
7926 : : return call;
7927 : : }
7928 : :
7929 : : /* Returns true if FN has two parameters, of which the second has type
7930 : : size_t. */
7931 : :
7932 : : static bool
7933 : 262043 : second_parm_is_size_t (tree fn)
7934 : : {
7935 : 262043 : tree t = FUNCTION_ARG_CHAIN (fn);
7936 : 262043 : if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
7937 : 262028 : return false;
7938 : 15 : t = TREE_CHAIN (t);
7939 : 15 : if (t == void_list_node)
7940 : : return true;
7941 : : return false;
7942 : : }
7943 : :
7944 : : /* True if T, an allocation function, has std::align_val_t as its second
7945 : : argument. */
7946 : :
7947 : : bool
7948 : 317418 : aligned_allocation_fn_p (tree t)
7949 : : {
7950 : 317418 : if (!aligned_new_threshold)
7951 : : return false;
7952 : :
7953 : 308468 : tree a = FUNCTION_ARG_CHAIN (t);
7954 : 308468 : return (a && same_type_p (TREE_VALUE (a), align_type_node));
7955 : : }
7956 : :
7957 : : /* True if T is std::destroying_delete_t. */
7958 : :
7959 : : static bool
7960 : 4339319 : std_destroying_delete_t_p (tree t)
7961 : : {
7962 : 4339319 : return (TYPE_CONTEXT (t) == std_node
7963 : 4339319 : && id_equal (TYPE_IDENTIFIER (t), "destroying_delete_t"));
7964 : : }
7965 : :
7966 : : /* A deallocation function with at least two parameters whose second parameter
7967 : : type is of type std::destroying_delete_t is a destroying operator delete. A
7968 : : destroying operator delete shall be a class member function named operator
7969 : : delete. [ Note: Array deletion cannot use a destroying operator
7970 : : delete. --end note ] */
7971 : :
7972 : : tree
7973 : 4339334 : destroying_delete_p (tree t)
7974 : : {
7975 : 4339334 : tree a = TYPE_ARG_TYPES (TREE_TYPE (t));
7976 : 4339334 : if (!a || !TREE_CHAIN (a))
7977 : : return NULL_TREE;
7978 : 4339319 : tree type = TREE_VALUE (TREE_CHAIN (a));
7979 : 4339319 : return std_destroying_delete_t_p (type) ? type : NULL_TREE;
7980 : : }
7981 : :
7982 : : struct dealloc_info
7983 : : {
7984 : : bool sized;
7985 : : bool aligned;
7986 : : tree destroying;
7987 : : };
7988 : :
7989 : : /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
7990 : : function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is
7991 : : non-null, also set *DI. */
7992 : :
7993 : : static bool
7994 : 3115098 : usual_deallocation_fn_p (tree t, dealloc_info *di)
7995 : : {
7996 : 3115098 : if (di) *di = dealloc_info();
7997 : :
7998 : : /* A template instance is never a usual deallocation function,
7999 : : regardless of its signature. */
8000 : 3115098 : if (TREE_CODE (t) == TEMPLATE_DECL
8001 : 3115098 : || primary_template_specialization_p (t))
8002 : 12 : return false;
8003 : :
8004 : : /* A usual deallocation function is a deallocation function whose parameters
8005 : : after the first are
8006 : : - optionally, a parameter of type std::destroying_delete_t, then
8007 : : - optionally, a parameter of type std::size_t, then
8008 : : - optionally, a parameter of type std::align_val_t. */
8009 : 3115086 : bool global = DECL_NAMESPACE_SCOPE_P (t);
8010 : 3115086 : tree chain = FUNCTION_ARG_CHAIN (t);
8011 : 3115086 : if (chain && destroying_delete_p (t))
8012 : : {
8013 : 81 : if (di) di->destroying = TREE_VALUE (chain);
8014 : 81 : chain = TREE_CHAIN (chain);
8015 : : }
8016 : 3115086 : if (chain
8017 : 3115083 : && (!global || flag_sized_deallocation)
8018 : 6216608 : && same_type_p (TREE_VALUE (chain), size_type_node))
8019 : : {
8020 : 899802 : if (di) di->sized = true;
8021 : 899802 : chain = TREE_CHAIN (chain);
8022 : : }
8023 : 3115083 : if (chain && aligned_new_threshold
8024 : 6215682 : && same_type_p (TREE_VALUE (chain), align_type_node))
8025 : : {
8026 : 1333043 : if (di) di->aligned = true;
8027 : 1333043 : chain = TREE_CHAIN (chain);
8028 : : }
8029 : 3115086 : return (chain == void_list_node);
8030 : : }
8031 : :
8032 : : /* Just return whether FN is a usual deallocation function. */
8033 : :
8034 : : bool
8035 : 8641 : usual_deallocation_fn_p (tree fn)
8036 : : {
8037 : 8641 : return usual_deallocation_fn_p (fn, NULL);
8038 : : }
8039 : :
8040 : : /* Build a call to operator delete. This has to be handled very specially,
8041 : : because the restrictions on what signatures match are different from all
8042 : : other call instances. For a normal delete, only a delete taking (void *)
8043 : : or (void *, size_t) is accepted. For a placement delete, only an exact
8044 : : match with the placement new is accepted.
8045 : :
8046 : : CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
8047 : : ADDR is the pointer to be deleted.
8048 : : SIZE is the size of the memory block to be deleted.
8049 : : GLOBAL_P is true if the delete-expression should not consider
8050 : : class-specific delete operators.
8051 : : CORO_P is true if the allocation is for a coroutine, where the two argument
8052 : : usual deallocation should be chosen in preference to the single argument
8053 : : version in a class context.
8054 : : PLACEMENT is the corresponding placement new call, or NULL_TREE.
8055 : :
8056 : : If this call to "operator delete" is being generated as part to
8057 : : deallocate memory allocated via a new-expression (as per [expr.new]
8058 : : which requires that if the initialization throws an exception then
8059 : : we call a deallocation function), then ALLOC_FN is the allocation
8060 : : function. */
8061 : :
8062 : : static tree
8063 : 717168 : build_op_delete_call_1 (enum tree_code code, tree addr, tree size,
8064 : : bool global_p, bool coro_p, tree placement,
8065 : : tree alloc_fn, tsubst_flags_t complain)
8066 : : {
8067 : 717168 : tree fn = NULL_TREE;
8068 : 717168 : tree fns, fnname, type, t;
8069 : 717168 : dealloc_info di_fn = { };
8070 : :
8071 : 717168 : if (addr == error_mark_node)
8072 : : return error_mark_node;
8073 : :
8074 : 717168 : type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
8075 : :
8076 : 717168 : fnname = ovl_op_identifier (false, code);
8077 : :
8078 : 623503 : if (CLASS_TYPE_P (type)
8079 : 623470 : && COMPLETE_TYPE_P (complete_type (type))
8080 : 1340615 : && !global_p)
8081 : : /* In [class.free]
8082 : :
8083 : : If the result of the lookup is ambiguous or inaccessible, or if
8084 : : the lookup selects a placement deallocation function, the
8085 : : program is ill-formed.
8086 : :
8087 : : Therefore, we ask lookup_fnfields to complain about ambiguity. */
8088 : : {
8089 : 411419 : fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain);
8090 : 411419 : if (fns == error_mark_node)
8091 : : return error_mark_node;
8092 : : }
8093 : : else
8094 : : fns = NULL_TREE;
8095 : :
8096 : 411416 : if (fns == NULL_TREE)
8097 : 716109 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
8098 : :
8099 : : /* Strip const and volatile from addr. */
8100 : 717165 : tree oaddr = addr;
8101 : 717165 : addr = cp_convert (ptr_type_node, addr, complain);
8102 : :
8103 : 717165 : tree excluded_destroying = NULL_TREE;
8104 : :
8105 : 717165 : if (placement)
8106 : : {
8107 : : /* "A declaration of a placement deallocation function matches the
8108 : : declaration of a placement allocation function if it has the same
8109 : : number of parameters and, after parameter transformations (8.3.5),
8110 : : all parameter types except the first are identical."
8111 : :
8112 : : So we build up the function type we want and ask instantiate_type
8113 : : to get it for us. */
8114 : 262842 : t = FUNCTION_ARG_CHAIN (alloc_fn);
8115 : 262842 : t = tree_cons (NULL_TREE, ptr_type_node, t);
8116 : 262842 : t = build_function_type (void_type_node, t);
8117 : :
8118 : 262842 : fn = instantiate_type (t, fns, tf_none);
8119 : 262842 : if (fn == error_mark_node)
8120 : : return NULL_TREE;
8121 : :
8122 : 262043 : fn = MAYBE_BASELINK_FUNCTIONS (fn);
8123 : :
8124 : : /* "If the lookup finds the two-parameter form of a usual deallocation
8125 : : function (3.7.4.2) and that function, considered as a placement
8126 : : deallocation function, would have been selected as a match for the
8127 : : allocation function, the program is ill-formed." */
8128 : 262043 : if (second_parm_is_size_t (fn))
8129 : : {
8130 : 9 : const char *const msg1
8131 : : = G_("exception cleanup for this placement new selects "
8132 : : "non-placement %<operator delete%>");
8133 : 9 : const char *const msg2
8134 : : = G_("%qD is a usual (non-placement) deallocation "
8135 : : "function in C++14 (or with %<-fsized-deallocation%>)");
8136 : :
8137 : : /* But if the class has an operator delete (void *), then that is
8138 : : the usual deallocation function, so we shouldn't complain
8139 : : about using the operator delete (void *, size_t). */
8140 : 9 : if (DECL_CLASS_SCOPE_P (fn))
8141 : 18 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8142 : : {
8143 : 9 : if (usual_deallocation_fn_p (elt)
8144 : 9 : && FUNCTION_ARG_CHAIN (elt) == void_list_node)
8145 : 3 : goto ok;
8146 : : }
8147 : : /* Before C++14 a two-parameter global deallocation function is
8148 : : always a placement deallocation function, but warn if
8149 : : -Wc++14-compat. */
8150 : 3 : else if (!flag_sized_deallocation)
8151 : : {
8152 : 1 : if (complain & tf_warning)
8153 : : {
8154 : 1 : auto_diagnostic_group d;
8155 : 1 : if (warning (OPT_Wc__14_compat, msg1))
8156 : 1 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8157 : 1 : }
8158 : 1 : goto ok;
8159 : : }
8160 : :
8161 : 5 : if (complain & tf_warning_or_error)
8162 : : {
8163 : 5 : auto_diagnostic_group d;
8164 : 5 : if (permerror (input_location, msg1))
8165 : : {
8166 : : /* Only mention C++14 for namespace-scope delete. */
8167 : 5 : if (DECL_NAMESPACE_SCOPE_P (fn))
8168 : 2 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8169 : : else
8170 : 3 : inform (DECL_SOURCE_LOCATION (fn),
8171 : : "%qD is a usual (non-placement) deallocation "
8172 : : "function", fn);
8173 : : }
8174 : 5 : }
8175 : : else
8176 : 0 : return error_mark_node;
8177 : 716366 : ok:;
8178 : : }
8179 : : }
8180 : : else
8181 : : /* "Any non-placement deallocation function matches a non-placement
8182 : : allocation function. If the lookup finds a single matching
8183 : : deallocation function, that function will be called; otherwise, no
8184 : : deallocation function will be called." */
8185 : 4015103 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8186 : : {
8187 : 3106457 : dealloc_info di_elt;
8188 : 3106457 : if (usual_deallocation_fn_p (elt, &di_elt))
8189 : : {
8190 : : /* If we're called for an EH cleanup in a new-expression, we can't
8191 : : use a destroying delete; the exception was thrown before the
8192 : : object was constructed. */
8193 : 1797635 : if (alloc_fn && di_elt.destroying)
8194 : : {
8195 : 14 : excluded_destroying = elt;
8196 : 904369 : continue;
8197 : : }
8198 : :
8199 : 1797621 : if (!fn)
8200 : : {
8201 : 454297 : fn = elt;
8202 : 454297 : di_fn = di_elt;
8203 : 454297 : continue;
8204 : : }
8205 : :
8206 : : /* -- If any of the deallocation functions is a destroying
8207 : : operator delete, all deallocation functions that are not
8208 : : destroying operator deletes are eliminated from further
8209 : : consideration. */
8210 : 1343324 : if (di_elt.destroying != di_fn.destroying)
8211 : : {
8212 : 12 : if (di_elt.destroying)
8213 : : {
8214 : 6 : fn = elt;
8215 : 6 : di_fn = di_elt;
8216 : : }
8217 : 12 : continue;
8218 : : }
8219 : :
8220 : : /* -- If the type has new-extended alignment, a function with a
8221 : : parameter of type std::align_val_t is preferred; otherwise a
8222 : : function without such a parameter is preferred. If exactly one
8223 : : preferred function is found, that function is selected and the
8224 : : selection process terminates. If more than one preferred
8225 : : function is found, all non-preferred functions are eliminated
8226 : : from further consideration. */
8227 : 1343312 : if (aligned_new_threshold)
8228 : : {
8229 : 1343072 : bool want_align = type_has_new_extended_alignment (type);
8230 : 1343072 : if (di_elt.aligned != di_fn.aligned)
8231 : : {
8232 : 450046 : if (want_align == di_elt.aligned)
8233 : : {
8234 : 445323 : fn = elt;
8235 : 445323 : di_fn = di_elt;
8236 : : }
8237 : 450046 : continue;
8238 : : }
8239 : : }
8240 : :
8241 : : /* -- If the deallocation functions have class scope, the one
8242 : : without a parameter of type std::size_t is selected. */
8243 : 893266 : bool want_size;
8244 : 893266 : if (DECL_CLASS_SCOPE_P (fn) && !coro_p)
8245 : : want_size = false;
8246 : :
8247 : : /* -- If the type is complete and if, for the second alternative
8248 : : (delete array) only, the operand is a pointer to a class type
8249 : : with a non-trivial destructor or a (possibly multi-dimensional)
8250 : : array thereof, the function with a parameter of type std::size_t
8251 : : is selected.
8252 : :
8253 : : -- Otherwise, it is unspecified whether a deallocation function
8254 : : with a parameter of type std::size_t is selected. */
8255 : : else
8256 : : {
8257 : 893158 : want_size = COMPLETE_TYPE_P (type);
8258 : 893158 : if (code == VEC_DELETE_EXPR
8259 : 893158 : && !TYPE_VEC_NEW_USES_COOKIE (type))
8260 : : /* We need a cookie to determine the array size. */
8261 : : want_size = false;
8262 : : }
8263 : 893266 : gcc_assert (di_fn.sized != di_elt.sized);
8264 : 893266 : if (want_size == di_elt.sized)
8265 : : {
8266 : 29438 : fn = elt;
8267 : 29438 : di_fn = di_elt;
8268 : : }
8269 : : }
8270 : : }
8271 : :
8272 : : /* If we have a matching function, call it. */
8273 : 716366 : if (fn)
8274 : : {
8275 : 716340 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8276 : :
8277 : : /* If the FN is a member function, make sure that it is
8278 : : accessible. */
8279 : 716340 : if (BASELINK_P (fns))
8280 : 988 : perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
8281 : : complain);
8282 : :
8283 : : /* Core issue 901: It's ok to new a type with deleted delete. */
8284 : 716340 : if (DECL_DELETED_FN (fn) && alloc_fn)
8285 : : return NULL_TREE;
8286 : :
8287 : 716334 : tree ret;
8288 : 716334 : if (placement)
8289 : : {
8290 : : /* The placement args might not be suitable for overload
8291 : : resolution at this point, so build the call directly. */
8292 : 262043 : int nargs = call_expr_nargs (placement);
8293 : 262043 : tree *argarray = XALLOCAVEC (tree, nargs);
8294 : 262043 : int i;
8295 : 262043 : argarray[0] = addr;
8296 : 524137 : for (i = 1; i < nargs; i++)
8297 : 262094 : argarray[i] = CALL_EXPR_ARG (placement, i);
8298 : 262043 : if (!mark_used (fn, complain) && !(complain & tf_error))
8299 : 0 : return error_mark_node;
8300 : 262043 : ret = build_cxx_call (fn, nargs, argarray, complain);
8301 : : }
8302 : : else
8303 : : {
8304 : 454291 : tree destroying = di_fn.destroying;
8305 : 454291 : if (destroying)
8306 : : {
8307 : : /* Strip const and volatile from addr but retain the type of the
8308 : : object. */
8309 : 26 : tree rtype = TREE_TYPE (TREE_TYPE (oaddr));
8310 : 26 : rtype = cv_unqualified (rtype);
8311 : 26 : rtype = TYPE_POINTER_TO (rtype);
8312 : 26 : addr = cp_convert (rtype, oaddr, complain);
8313 : 26 : destroying = build_functional_cast (input_location,
8314 : : destroying, NULL_TREE,
8315 : : complain);
8316 : : }
8317 : :
8318 : 454291 : releasing_vec args;
8319 : 454291 : args->quick_push (addr);
8320 : 454291 : if (destroying)
8321 : 26 : args->quick_push (destroying);
8322 : 454291 : if (di_fn.sized)
8323 : 432312 : args->quick_push (size);
8324 : 454291 : if (di_fn.aligned)
8325 : : {
8326 : 2365 : tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
8327 : 2365 : args->quick_push (al);
8328 : : }
8329 : 454291 : ret = cp_build_function_call_vec (fn, &args, complain);
8330 : 454291 : }
8331 : :
8332 : : /* Set this flag for all callers of this function. In addition to
8333 : : delete-expressions, this is called for deallocating coroutine state;
8334 : : treat that as an implicit delete-expression. This is also called for
8335 : : the delete if the constructor throws in a new-expression, and for a
8336 : : deleting destructor (which implements a delete-expression). */
8337 : : /* But leave this flag off for destroying delete to avoid wrong
8338 : : assumptions in the optimizers. */
8339 : 716334 : tree call = extract_call_expr (ret);
8340 : 716334 : if (TREE_CODE (call) == CALL_EXPR && !destroying_delete_p (fn))
8341 : 716302 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
8342 : :
8343 : 716334 : return ret;
8344 : : }
8345 : :
8346 : : /* If there's only a destroying delete that we can't use because the
8347 : : object isn't constructed yet, and we used global new, use global
8348 : : delete as well. */
8349 : 26 : if (excluded_destroying
8350 : 26 : && DECL_NAMESPACE_SCOPE_P (alloc_fn))
8351 : 8 : return build_op_delete_call (code, addr, size, true, placement,
8352 : 8 : alloc_fn, complain);
8353 : :
8354 : : /* [expr.new]
8355 : :
8356 : : If no unambiguous matching deallocation function can be found,
8357 : : propagating the exception does not cause the object's memory to
8358 : : be freed. */
8359 : 18 : if (alloc_fn)
8360 : : {
8361 : 15 : if ((complain & tf_warning)
8362 : 15 : && !placement)
8363 : : {
8364 : 15 : bool w = warning (0,
8365 : : "no corresponding deallocation function for %qD",
8366 : : alloc_fn);
8367 : 15 : if (w && excluded_destroying)
8368 : 3 : inform (DECL_SOURCE_LOCATION (excluded_destroying), "destroying "
8369 : : "delete %qD cannot be used to release the allocated memory"
8370 : : " if the initialization throws because the object is not "
8371 : : "constructed yet", excluded_destroying);
8372 : : }
8373 : 15 : return NULL_TREE;
8374 : : }
8375 : :
8376 : 3 : if (complain & tf_error)
8377 : 3 : error ("no suitable %<operator %s%> for %qT",
8378 : 3 : OVL_OP_INFO (false, code)->name, type);
8379 : 3 : return error_mark_node;
8380 : : }
8381 : :
8382 : : /* Arguments as per build_op_delete_call_1 (). */
8383 : :
8384 : : tree
8385 : 713997 : build_op_delete_call (enum tree_code code, tree addr, tree size, bool global_p,
8386 : : tree placement, tree alloc_fn, tsubst_flags_t complain)
8387 : : {
8388 : 713997 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/false,
8389 : 713997 : placement, alloc_fn, complain);
8390 : : }
8391 : :
8392 : : /* Arguments as per build_op_delete_call_1 (). */
8393 : :
8394 : : tree
8395 : 3171 : build_coroutine_op_delete_call (enum tree_code code, tree addr, tree size,
8396 : : bool global_p, tree placement, tree alloc_fn,
8397 : : tsubst_flags_t complain)
8398 : : {
8399 : 3171 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/true,
8400 : 3171 : placement, alloc_fn, complain);
8401 : : }
8402 : :
8403 : : /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
8404 : : in the diagnostics.
8405 : :
8406 : : If ISSUE_ERROR is true, then issue an error about the access, followed
8407 : : by a note showing the declaration. Otherwise, just show the note.
8408 : :
8409 : : DIAG_DECL and DIAG_LOCATION will almost always be the same.
8410 : : DIAG_LOCATION is just another DECL. NO_ACCESS_REASON is an optional
8411 : : parameter used to specify why DECL wasn't accessible (e.g. ak_private
8412 : : would be because DECL was private). If not using NO_ACCESS_REASON,
8413 : : then it must be ak_none, and the access failure reason will be
8414 : : figured out by looking at the protection of DECL. */
8415 : :
8416 : : void
8417 : 1172 : complain_about_access (tree decl, tree diag_decl, tree diag_location,
8418 : : bool issue_error, access_kind no_access_reason)
8419 : : {
8420 : : /* If we have not already figured out why DECL is inaccessible... */
8421 : 1172 : if (no_access_reason == ak_none)
8422 : : {
8423 : : /* Examine the access of DECL to find out why. */
8424 : 993 : if (TREE_PRIVATE (decl))
8425 : : no_access_reason = ak_private;
8426 : 225 : else if (TREE_PROTECTED (decl))
8427 : : no_access_reason = ak_protected;
8428 : : }
8429 : :
8430 : : /* Now generate an error message depending on calculated access. */
8431 : 224 : if (no_access_reason == ak_private)
8432 : : {
8433 : 947 : if (issue_error)
8434 : 933 : error ("%q#D is private within this context", diag_decl);
8435 : 947 : inform (DECL_SOURCE_LOCATION (diag_location), "declared private here");
8436 : : }
8437 : 225 : else if (no_access_reason == ak_protected)
8438 : : {
8439 : 180 : if (issue_error)
8440 : 166 : error ("%q#D is protected within this context", diag_decl);
8441 : 180 : inform (DECL_SOURCE_LOCATION (diag_location), "declared protected here");
8442 : : }
8443 : : /* Couldn't figure out why DECL is inaccesible, so just say it's
8444 : : inaccessible. */
8445 : : else
8446 : : {
8447 : 45 : if (issue_error)
8448 : 45 : error ("%q#D is inaccessible within this context", diag_decl);
8449 : 45 : inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
8450 : : }
8451 : 1172 : }
8452 : :
8453 : : /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
8454 : : bitwise or of LOOKUP_* values. If any errors are warnings are
8455 : : generated, set *DIAGNOSTIC_FN to "error" or "warning",
8456 : : respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
8457 : : to NULL. */
8458 : :
8459 : : static tree
8460 : 10761674 : build_temp (tree expr, tree type, int flags,
8461 : : enum diagnostics::kind *diagnostic_kind, tsubst_flags_t complain)
8462 : : {
8463 : 10761674 : int savew, savee;
8464 : :
8465 : 10761674 : *diagnostic_kind = diagnostics::kind::unspecified;
8466 : :
8467 : : /* If the source is a packed field, calling the copy constructor will require
8468 : : binding the field to the reference parameter to the copy constructor, and
8469 : : we'll end up with an infinite loop. If we can use a bitwise copy, then
8470 : : do that now. */
8471 : 10761674 : if ((lvalue_kind (expr) & clk_packed)
8472 : 6 : && CLASS_TYPE_P (TREE_TYPE (expr))
8473 : 10761680 : && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
8474 : 3 : return get_target_expr (expr, complain);
8475 : :
8476 : : /* In decltype, we might have decided not to wrap this call in a TARGET_EXPR.
8477 : : But it turns out to be a subexpression, so perform temporary
8478 : : materialization now. */
8479 : 10761671 : if (TREE_CODE (expr) == CALL_EXPR
8480 : 3 : && CLASS_TYPE_P (type)
8481 : 10761674 : && same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
8482 : 3 : expr = build_cplus_new (type, expr, complain);
8483 : :
8484 : 10761671 : savew = warningcount + werrorcount, savee = errorcount;
8485 : 10761671 : releasing_vec args (make_tree_vector_single (expr));
8486 : 10761671 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8487 : : &args, type, flags, complain);
8488 : 10761671 : if (warningcount + werrorcount > savew)
8489 : 0 : *diagnostic_kind = diagnostics::kind::warning;
8490 : 10761671 : else if (errorcount > savee)
8491 : 78 : *diagnostic_kind = diagnostics::kind::error;
8492 : 10761671 : return expr;
8493 : 10761671 : }
8494 : :
8495 : : /* Get any location for EXPR, falling back to input_location.
8496 : :
8497 : : If the result is in a system header and is the virtual location for
8498 : : a token coming from the expansion of a macro, unwind it to the
8499 : : location of the expansion point of the macro (e.g. to avoid the
8500 : : diagnostic being suppressed for expansions of NULL where "NULL" is
8501 : : in a system header). */
8502 : :
8503 : : static location_t
8504 : 1798774 : get_location_for_expr_unwinding_for_system_header (tree expr)
8505 : : {
8506 : 1798774 : location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
8507 : 1798774 : loc = expansion_point_location_if_in_system_header (loc);
8508 : 1798774 : return loc;
8509 : : }
8510 : :
8511 : : /* Perform warnings about peculiar, but valid, conversions from/to NULL.
8512 : : Also handle a subset of zero as null warnings.
8513 : : EXPR is implicitly converted to type TOTYPE.
8514 : : FN and ARGNUM are used for diagnostics. */
8515 : :
8516 : : static void
8517 : 418060198 : conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
8518 : : {
8519 : : /* Issue warnings about peculiar, but valid, uses of NULL. */
8520 : 418060198 : if (TREE_CODE (totype) != BOOLEAN_TYPE
8521 : 247980615 : && ARITHMETIC_TYPE_P (totype)
8522 : 552516881 : && null_node_p (expr))
8523 : : {
8524 : 94 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8525 : 94 : if (fn)
8526 : : {
8527 : 33 : auto_diagnostic_group d;
8528 : 33 : if (warning_at (loc, OPT_Wconversion_null,
8529 : : "passing NULL to non-pointer argument %P of %qD",
8530 : : argnum, fn))
8531 : 27 : inform (get_fndecl_argument_location (fn, argnum),
8532 : : "declared here");
8533 : 33 : }
8534 : : else
8535 : 61 : warning_at (loc, OPT_Wconversion_null,
8536 : : "converting to non-pointer type %qT from NULL", totype);
8537 : : }
8538 : :
8539 : : /* Issue warnings if "false" is converted to a NULL pointer */
8540 : 418060104 : else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
8541 : 418060104 : && TYPE_PTR_P (totype))
8542 : : {
8543 : 7 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8544 : 7 : if (fn)
8545 : : {
8546 : 3 : auto_diagnostic_group d;
8547 : 3 : if (warning_at (loc, OPT_Wconversion_null,
8548 : : "converting %<false%> to pointer type for argument "
8549 : : "%P of %qD", argnum, fn))
8550 : 3 : inform (get_fndecl_argument_location (fn, argnum),
8551 : : "declared here");
8552 : 3 : }
8553 : : else
8554 : 4 : warning_at (loc, OPT_Wconversion_null,
8555 : : "converting %<false%> to pointer type %qT", totype);
8556 : : }
8557 : : /* Handle zero as null pointer warnings for cases other
8558 : : than EQ_EXPR and NE_EXPR */
8559 : 383286469 : else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
8560 : 418343831 : && null_ptr_cst_p (expr))
8561 : : {
8562 : 1798673 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8563 : 1798673 : maybe_warn_zero_as_null_pointer_constant (expr, loc);
8564 : : }
8565 : 418060198 : }
8566 : :
8567 : : /* We gave a diagnostic during a conversion. If this was in the second
8568 : : standard conversion sequence of a user-defined conversion sequence, say
8569 : : which user-defined conversion. */
8570 : :
8571 : : static void
8572 : 5215 : maybe_print_user_conv_context (conversion *convs)
8573 : : {
8574 : 5215 : if (convs->user_conv_p)
8575 : 160 : for (conversion *t = convs; t; t = next_conversion (t))
8576 : 136 : if (t->kind == ck_user)
8577 : : {
8578 : 41 : print_z_candidate (0, N_(" after user-defined conversion:"),
8579 : : t->cand);
8580 : 41 : break;
8581 : : }
8582 : 5215 : }
8583 : :
8584 : : /* Locate the parameter with the given index within FNDECL.
8585 : : ARGNUM is zero based, -1 indicates the `this' argument of a method.
8586 : : Return the location of the FNDECL itself if there are problems. */
8587 : :
8588 : : location_t
8589 : 7018579 : get_fndecl_argument_location (tree fndecl, int argnum)
8590 : : {
8591 : : /* The locations of implicitly-declared functions are likely to be
8592 : : more meaningful than those of their parameters. */
8593 : 7018579 : if (DECL_ARTIFICIAL (fndecl))
8594 : 316 : return DECL_SOURCE_LOCATION (fndecl);
8595 : :
8596 : 7018263 : int i;
8597 : 7018263 : tree param;
8598 : :
8599 : : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8600 : 7018263 : for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
8601 : 16560589 : i < argnum && param;
8602 : 9542326 : i++, param = TREE_CHAIN (param))
8603 : : ;
8604 : :
8605 : : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8606 : : return the location of FNDECL. */
8607 : 7018263 : if (param == NULL)
8608 : 29 : return DECL_SOURCE_LOCATION (fndecl);
8609 : :
8610 : 7018234 : return DECL_SOURCE_LOCATION (param);
8611 : : }
8612 : :
8613 : : /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
8614 : : within its declaration (or the fndecl itself if something went
8615 : : wrong). */
8616 : :
8617 : : void
8618 : 6988 : maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum,
8619 : : const char *highlight_color)
8620 : : {
8621 : 6988 : if (fn)
8622 : : {
8623 : 1094 : gcc_rich_location richloc (get_fndecl_argument_location (fn, argnum));
8624 : 1094 : richloc.set_highlight_color (highlight_color);
8625 : 1094 : inform (&richloc,
8626 : : "initializing argument %P of %qD", argnum, fn);
8627 : 1094 : }
8628 : 6988 : }
8629 : :
8630 : : /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
8631 : : the conversion, EXPR is the expression we're converting. */
8632 : :
8633 : : static void
8634 : 30372473 : maybe_warn_array_conv (location_t loc, conversion *c, tree expr)
8635 : : {
8636 : 30372473 : if (cxx_dialect >= cxx20)
8637 : : return;
8638 : :
8639 : 15762727 : tree type = TREE_TYPE (expr);
8640 : 15762727 : type = strip_pointer_operator (type);
8641 : :
8642 : 15762727 : if (TREE_CODE (type) != ARRAY_TYPE
8643 : 15762727 : || TYPE_DOMAIN (type) == NULL_TREE)
8644 : : return;
8645 : :
8646 : 13531 : if (pedantic && conv_binds_to_array_of_unknown_bound (c))
8647 : 10 : pedwarn (loc, OPT_Wc__20_extensions,
8648 : : "conversions to arrays of unknown bound "
8649 : : "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
8650 : : }
8651 : :
8652 : : /* We call this recursively in convert_like_internal. */
8653 : : static tree convert_like (conversion *, tree, tree, int, bool, bool, bool,
8654 : : tsubst_flags_t);
8655 : :
8656 : : /* Adjust the result EXPR of a conversion to the expected type TOTYPE, which
8657 : : must be equivalent but might be a typedef. */
8658 : :
8659 : : static tree
8660 : 696472205 : maybe_adjust_type_name (tree type, tree expr, conversion_kind kind)
8661 : : {
8662 : 696472205 : if (expr == error_mark_node
8663 : 696472155 : || processing_template_decl)
8664 : : return expr;
8665 : :
8666 : 603819016 : tree etype = TREE_TYPE (expr);
8667 : 603819016 : if (etype == type)
8668 : : return expr;
8669 : :
8670 : 76854487 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p (etype, type)
8671 : : || is_bitfield_expr_with_lowered_type (expr)
8672 : : || seen_error ());
8673 : :
8674 : 74459123 : if (SCALAR_TYPE_P (type)
8675 : 146704409 : && (kind == ck_rvalue
8676 : : /* ??? We should be able to do this for ck_identity of more prvalue
8677 : : expressions, but checking !obvalue_p here breaks, so for now let's
8678 : : just handle NON_LVALUE_EXPR (such as the location wrapper for a
8679 : : literal). Maybe we want to express already-rvalue in the
8680 : : conversion somehow? */
8681 : 64812112 : || TREE_CODE (expr) == NON_LVALUE_EXPR))
8682 : 8876645 : expr = build_nop (type, expr);
8683 : :
8684 : : return expr;
8685 : : }
8686 : :
8687 : : /* Perform the conversions in CONVS on the expression EXPR. FN and
8688 : : ARGNUM are used for diagnostics. ARGNUM is zero based, -1
8689 : : indicates the `this' argument of a method. INNER is nonzero when
8690 : : being called to continue a conversion chain. It is negative when a
8691 : : reference binding will be applied, positive otherwise. If
8692 : : ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
8693 : : conversions will be emitted if appropriate. If C_CAST_P is true,
8694 : : this conversion is coming from a C-style cast; in that case,
8695 : : conversions to inaccessible bases are permitted. */
8696 : :
8697 : : static tree
8698 : 831706311 : convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
8699 : : bool issue_conversion_warnings, bool c_cast_p,
8700 : : bool nested_p, tsubst_flags_t complain)
8701 : : {
8702 : 831706311 : tree totype = convs->type;
8703 : 831706311 : enum diagnostics::kind diag_kind;
8704 : 831706311 : int flags;
8705 : 831706311 : location_t loc = cp_expr_loc_or_input_loc (expr);
8706 : 831706311 : const bool stub_object_p = is_stub_object (expr);
8707 : :
8708 : 831706311 : if (convs->bad_p && !(complain & tf_error))
8709 : 36334 : return error_mark_node;
8710 : :
8711 : 831669977 : gcc_checking_assert (!TYPE_REF_P (TREE_TYPE (expr)));
8712 : :
8713 : 831669977 : if (convs->bad_p
8714 : 7031 : && convs->kind != ck_user
8715 : 6929 : && convs->kind != ck_list
8716 : 6923 : && convs->kind != ck_ambig
8717 : 6923 : && (convs->kind != ck_ref_bind
8718 : 5286 : || (convs->user_conv_p && next_conversion (convs)->bad_p))
8719 : 1658 : && (convs->kind != ck_rvalue
8720 : 15 : || SCALAR_TYPE_P (totype))
8721 : 831671626 : && convs->kind != ck_base)
8722 : : {
8723 : 1649 : int complained = 0;
8724 : 1649 : conversion *t = convs;
8725 : :
8726 : : /* Give a helpful error if this is bad because of excess braces. */
8727 : 46 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8728 : 46 : && SCALAR_TYPE_P (totype)
8729 : 34 : && CONSTRUCTOR_NELTS (expr) > 0
8730 : 1683 : && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
8731 : : {
8732 : 10 : complained = permerror (loc, "too many braces around initializer "
8733 : : "for %qT", totype);
8734 : 30 : while (BRACE_ENCLOSED_INITIALIZER_P (expr)
8735 : 50 : && CONSTRUCTOR_NELTS (expr) == 1)
8736 : 20 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8737 : : }
8738 : :
8739 : : /* Give a helpful error if this is bad because a conversion to bool
8740 : : from std::nullptr_t requires direct-initialization. */
8741 : 1649 : if (NULLPTR_TYPE_P (TREE_TYPE (expr))
8742 : 1649 : && TREE_CODE (totype) == BOOLEAN_TYPE)
8743 : 15 : complained = permerror (loc, "converting to %qH from %qI requires "
8744 : : "direct-initialization",
8745 : 15 : totype, TREE_TYPE (expr));
8746 : :
8747 : 1649 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (expr))
8748 : 69 : && SCALAR_FLOAT_TYPE_P (totype)
8749 : 1718 : && (extended_float_type_p (TREE_TYPE (expr))
8750 : 18 : || extended_float_type_p (totype)))
8751 : 69 : switch (cp_compare_floating_point_conversion_ranks (TREE_TYPE (expr),
8752 : : totype))
8753 : : {
8754 : 69 : case 2:
8755 : 69 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8756 : : "converting to %qH from %qI with greater "
8757 : 69 : "conversion rank", totype, TREE_TYPE (expr)))
8758 : 1649 : complained = 1;
8759 : 15 : else if (!complained)
8760 : 15 : complained = -1;
8761 : : break;
8762 : 0 : case 3:
8763 : 0 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8764 : : "converting to %qH from %qI with unordered "
8765 : 0 : "conversion rank", totype, TREE_TYPE (expr)))
8766 : : complained = 1;
8767 : 0 : else if (!complained)
8768 : 15 : complained = -1;
8769 : : break;
8770 : : default:
8771 : : break;
8772 : : }
8773 : :
8774 : 3314 : for (; t ; t = next_conversion (t))
8775 : : {
8776 : 3305 : if (t->kind == ck_user && t->cand->reason)
8777 : : {
8778 : 52 : auto_diagnostic_group d;
8779 : 52 : complained = permerror (loc, "invalid user-defined conversion "
8780 : 52 : "from %qH to %qI", TREE_TYPE (expr),
8781 : : totype);
8782 : 52 : if (complained)
8783 : : {
8784 : 52 : auto_diagnostic_nesting_level sentinel;
8785 : 52 : print_z_candidate (loc, N_("candidate is:"), t->cand);
8786 : 52 : }
8787 : 52 : expr = convert_like (t, expr, fn, argnum,
8788 : : /*issue_conversion_warnings=*/false,
8789 : : /*c_cast_p=*/false, /*nested_p=*/true,
8790 : : complain);
8791 : 52 : break;
8792 : 52 : }
8793 : 3253 : else if (t->kind == ck_user || !t->bad_p)
8794 : : {
8795 : 1588 : expr = convert_like (t, expr, fn, argnum,
8796 : : /*issue_conversion_warnings=*/false,
8797 : : /*c_cast_p=*/false, /*nested_p=*/true,
8798 : : complain);
8799 : 1588 : if (t->bad_p)
8800 : 0 : complained = 1;
8801 : : break;
8802 : : }
8803 : 1665 : else if (t->kind == ck_ambig)
8804 : 0 : return convert_like (t, expr, fn, argnum,
8805 : : /*issue_conversion_warnings=*/false,
8806 : : /*c_cast_p=*/false, /*nested_p=*/true,
8807 : 0 : complain);
8808 : 1665 : else if (t->kind == ck_identity)
8809 : : break;
8810 : : }
8811 : 1649 : if (!complained && stub_object_p)
8812 : : {
8813 : : /* An error diagnosed within a trait, don't give extra labels. */
8814 : 12 : error_at (loc, "invalid conversion from %qH to %qI",
8815 : 6 : TREE_TYPE (expr), totype);
8816 : 6 : complained = 1;
8817 : : }
8818 : 1643 : else if (!complained && expr != error_mark_node)
8819 : : {
8820 : 1496 : range_label_for_type_mismatch label (TREE_TYPE (expr), totype);
8821 : 1496 : gcc_rich_location richloc (loc, &label, highlight_colors::percent_h);
8822 : 1496 : complained = permerror (&richloc,
8823 : : "invalid conversion from %qH to %qI",
8824 : 1496 : TREE_TYPE (expr), totype);
8825 : 1496 : if (complained)
8826 : 1441 : maybe_emit_indirection_note (loc, expr, totype);
8827 : 1496 : }
8828 : 1649 : if (convs->kind == ck_ref_bind)
8829 : 21 : expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
8830 : : LOOKUP_NORMAL, NULL_TREE,
8831 : : complain);
8832 : : else
8833 : 1628 : expr = cp_convert (totype, expr, complain);
8834 : 1649 : if (complained == 1)
8835 : 1578 : maybe_inform_about_fndecl_for_bogus_argument_init
8836 : 1578 : (fn, argnum, highlight_colors::percent_i);
8837 : 1649 : return expr;
8838 : : }
8839 : :
8840 : 831668328 : if (issue_conversion_warnings && (complain & tf_warning))
8841 : 418060198 : conversion_null_warnings (totype, expr, fn, argnum);
8842 : :
8843 : 831668328 : switch (convs->kind)
8844 : : {
8845 : 4656407 : case ck_user:
8846 : 4656407 : {
8847 : 4656407 : struct z_candidate *cand = convs->cand;
8848 : :
8849 : 4656407 : if (cand == NULL)
8850 : : /* We chose the surrogate function from add_conv_candidate, now we
8851 : : actually need to build the conversion. */
8852 : 64 : cand = build_user_type_conversion_1 (totype, expr,
8853 : : LOOKUP_NO_CONVERSION, complain);
8854 : :
8855 : 4656407 : tree convfn = cand->fn;
8856 : :
8857 : : /* When converting from an init list we consider explicit
8858 : : constructors, but actually trying to call one is an error. */
8859 : 4858511 : if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
8860 : 3465 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
8861 : : /* Unless this is for direct-list-initialization. */
8862 : 3462 : && (!CONSTRUCTOR_IS_DIRECT_INIT (expr) || convs->need_temporary_p)
8863 : : /* And in C++98 a default constructor can't be explicit. */
8864 : 4656627 : && cxx_dialect >= cxx11)
8865 : : {
8866 : 219 : if (!(complain & tf_error))
8867 : 36 : return error_mark_node;
8868 : 183 : location_t loc = location_of (expr);
8869 : 183 : if (CONSTRUCTOR_NELTS (expr) == 0
8870 : 183 : && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
8871 : : {
8872 : 7 : auto_diagnostic_group d;
8873 : 7 : if (pedwarn (loc, 0, "converting to %qT from initializer list "
8874 : : "would use explicit constructor %qD",
8875 : : totype, convfn))
8876 : : {
8877 : 7 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8878 : : convfn);
8879 : 7 : inform (loc, "in C++11 and above a default constructor "
8880 : : "can be explicit");
8881 : : }
8882 : 7 : }
8883 : : else
8884 : : {
8885 : 176 : auto_diagnostic_group d;
8886 : 176 : error ("converting to %qT from initializer list would use "
8887 : : "explicit constructor %qD", totype, convfn);
8888 : 176 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8889 : : convfn);
8890 : 176 : }
8891 : : }
8892 : :
8893 : : /* If we're initializing from {}, it's value-initialization. */
8894 : 639478 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8895 : 639478 : && CONSTRUCTOR_NELTS (expr) == 0
8896 : 119960 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)
8897 : 4776304 : && !processing_template_decl)
8898 : : {
8899 : 119933 : if (abstract_virtuals_error (NULL_TREE, totype, complain))
8900 : 15 : return error_mark_node;
8901 : 119918 : expr = build_value_init (totype, complain);
8902 : 119918 : expr = get_target_expr (expr, complain);
8903 : 119918 : if (expr != error_mark_node)
8904 : 119741 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8905 : 119918 : return expr;
8906 : : }
8907 : :
8908 : : /* We don't know here whether EXPR is being used as an lvalue or
8909 : : rvalue, but we know it's read. */
8910 : 4536438 : mark_exp_read (expr);
8911 : :
8912 : : /* Give the conversion call the location of EXPR rather than the
8913 : : location of the context that caused the conversion. */
8914 : 4536438 : iloc_sentinel ils (loc);
8915 : :
8916 : : /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
8917 : : any more UDCs. */
8918 : 4536438 : expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
8919 : : complain);
8920 : :
8921 : : /* If this is a constructor or a function returning an aggr type,
8922 : : we need to build up a TARGET_EXPR. */
8923 : 9072876 : if (DECL_CONSTRUCTOR_P (convfn))
8924 : : {
8925 : 1719143 : expr = build_cplus_new (totype, expr, complain);
8926 : :
8927 : : /* Remember that this was list-initialization. */
8928 : 1719143 : if (convs->check_narrowing && expr != error_mark_node)
8929 : 539571 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8930 : : }
8931 : :
8932 : 4536438 : return expr;
8933 : 4536438 : }
8934 : 597193913 : case ck_identity:
8935 : 597193913 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
8936 : : {
8937 : 894464 : int nelts = CONSTRUCTOR_NELTS (expr);
8938 : 148246 : if (nelts == 0)
8939 : 746218 : expr = build_value_init (totype, complain);
8940 : 148246 : else if (nelts == 1)
8941 : 148246 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8942 : : else
8943 : 0 : gcc_unreachable ();
8944 : : }
8945 : 597193913 : expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p,
8946 : : /*read_p=*/true, UNKNOWN_LOCATION,
8947 : : /*reject_builtin=*/true);
8948 : :
8949 : 597193913 : if (type_unknown_p (expr))
8950 : 17820 : expr = instantiate_type (totype, expr, complain);
8951 : 597193913 : if (!nested_p && TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8952 : 70509 : expr = cp_convert (totype, TREE_OPERAND (expr, 0), complain);
8953 : 597193913 : if (expr == null_node
8954 : 597193913 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
8955 : : /* If __null has been converted to an integer type, we do not want to
8956 : : continue to warn about uses of EXPR as an integer, rather than as a
8957 : : pointer. */
8958 : 153704 : expr = build_int_cst (totype, 0);
8959 : 597193913 : return maybe_adjust_type_name (totype, expr, convs->kind);
8960 : 47 : case ck_ambig:
8961 : : /* We leave bad_p off ck_ambig because overload resolution considers
8962 : : it valid, it just fails when we try to perform it. So we need to
8963 : : check complain here, too. */
8964 : 47 : if (complain & tf_error)
8965 : : {
8966 : : /* Call build_user_type_conversion again for the error. */
8967 : 3 : int flags = (convs->need_temporary_p
8968 : 47 : ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
8969 : 47 : build_user_type_conversion (totype, convs->u.expr, flags, complain);
8970 : 47 : gcc_assert (seen_error ());
8971 : 47 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8972 : : }
8973 : 47 : return error_mark_node;
8974 : :
8975 : 3857 : case ck_list:
8976 : 3857 : {
8977 : : /* Conversion to std::initializer_list<T>. */
8978 : 3857 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
8979 : 3857 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (expr);
8980 : 3857 : tree array;
8981 : :
8982 : 3857 : if (tree init = maybe_init_list_as_array (elttype, expr))
8983 : : {
8984 : 132 : elttype
8985 : 132 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
8986 : : | TYPE_QUAL_CONST));
8987 : 132 : tree index_type = TYPE_DOMAIN (TREE_TYPE (init));
8988 : 132 : array = build_cplus_array_type (elttype, index_type);
8989 : 132 : len = TREE_INT_CST_LOW (TYPE_MAX_VALUE (index_type)) + 1;
8990 : 132 : array = build_vec_init_expr (array, init, complain);
8991 : 132 : array = get_target_expr (array);
8992 : 132 : array = cp_build_addr_expr (array, complain);
8993 : : }
8994 : 3725 : else if (len)
8995 : : {
8996 : 3612 : tree val;
8997 : 3612 : unsigned ix;
8998 : 3612 : tree new_ctor = build_constructor (init_list_type_node, NULL);
8999 : :
9000 : : /* Convert all the elements. */
9001 : 21358 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
9002 : : {
9003 : 17761 : if (TREE_CODE (val) == RAW_DATA_CST)
9004 : : {
9005 : : /* For conversion to initializer_list<unsigned char> or
9006 : : initializer_list<char> or initializer_list<signed char>
9007 : : we can optimize and keep RAW_DATA_CST with adjusted
9008 : : type if we report narrowing errors if needed, for
9009 : : others this converts each element separately. */
9010 : 48 : if (convs->u.list[ix]->kind == ck_std)
9011 : : {
9012 : 18 : tree et = convs->u.list[ix]->type;
9013 : 18 : conversion *next = next_conversion (convs->u.list[ix]);
9014 : 18 : gcc_assert (et
9015 : : && (TREE_CODE (et) == INTEGER_TYPE
9016 : : || is_byte_access_type (et))
9017 : : && TYPE_PRECISION (et) == CHAR_BIT
9018 : : && next
9019 : : && next->kind == ck_identity);
9020 : 18 : if (!TYPE_UNSIGNED (et)
9021 : : /* For RAW_DATA_CST, TREE_TYPE (val) can be
9022 : : either integer_type_node (when it has been
9023 : : created by the lexer from CPP_EMBED) or
9024 : : after digestion/conversion some integral
9025 : : type with CHAR_BIT precision. For int with
9026 : : precision higher than CHAR_BIT or unsigned char
9027 : : diagnose narrowing conversions from
9028 : : that int/unsigned char to signed char if any
9029 : : byte has most significant bit set. */
9030 : 18 : && (TYPE_UNSIGNED (TREE_TYPE (val))
9031 : 12 : || (TYPE_PRECISION (TREE_TYPE (val))
9032 : : > CHAR_BIT)))
9033 : 2460 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9034 : : {
9035 : 2454 : if (RAW_DATA_SCHAR_ELT (val, i) >= 0)
9036 : 2448 : continue;
9037 : 6 : else if (complain & tf_error)
9038 : : {
9039 : 6 : location_t loc
9040 : 6 : = cp_expr_loc_or_input_loc (val);
9041 : 6 : int savederrorcount = errorcount;
9042 : 18 : permerror_opt (loc, OPT_Wnarrowing,
9043 : : "narrowing conversion of "
9044 : : "%qd from %qH to %qI",
9045 : 6 : RAW_DATA_UCHAR_ELT (val, i),
9046 : 6 : TREE_TYPE (val), et);
9047 : 6 : if (errorcount != savederrorcount)
9048 : 6 : return error_mark_node;
9049 : : }
9050 : : else
9051 : 0 : return error_mark_node;
9052 : : }
9053 : 12 : tree sub = copy_node (val);
9054 : 12 : TREE_TYPE (sub) = et;
9055 : 12 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9056 : : NULL_TREE, sub);
9057 : : }
9058 : : else
9059 : : {
9060 : 30 : conversion *conv = convs->u.list[ix];
9061 : 30 : gcc_assert (conv->kind == ck_list);
9062 : 15495 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9063 : : {
9064 : 15465 : tree elt
9065 : 15465 : = build_int_cst (TREE_TYPE (val),
9066 : 15465 : RAW_DATA_UCHAR_ELT (val, i));
9067 : 15465 : tree sub
9068 : 15465 : = convert_like (conv->u.list[i], elt,
9069 : : fn, argnum, false, false,
9070 : : /*nested_p=*/true, complain);
9071 : 15465 : if (sub == error_mark_node)
9072 : : return sub;
9073 : 15465 : if (!check_narrowing (TREE_TYPE (sub), elt,
9074 : : complain))
9075 : 0 : return error_mark_node;
9076 : 15465 : tree nc = new_ctor;
9077 : 15465 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (nc),
9078 : : NULL_TREE, sub);
9079 : 15465 : if (!TREE_CONSTANT (sub))
9080 : 11679 : TREE_CONSTANT (new_ctor) = false;
9081 : : }
9082 : : }
9083 : 42 : len += RAW_DATA_LENGTH (val) - 1;
9084 : 42 : continue;
9085 : 42 : }
9086 : 17713 : tree sub = convert_like (convs->u.list[ix], val, fn,
9087 : : argnum, false, false,
9088 : : /*nested_p=*/true, complain);
9089 : 17713 : if (sub == error_mark_node)
9090 : : return sub;
9091 : 1545 : if (!BRACE_ENCLOSED_INITIALIZER_P (val)
9092 : 17724 : && !check_narrowing (TREE_TYPE (sub), val, complain))
9093 : 0 : return error_mark_node;
9094 : 17704 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9095 : : NULL_TREE, sub);
9096 : 17704 : if (!TREE_CONSTANT (sub))
9097 : 9966 : TREE_CONSTANT (new_ctor) = false;
9098 : : }
9099 : : /* Build up the array. */
9100 : 3597 : elttype
9101 : 3597 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
9102 : : | TYPE_QUAL_CONST));
9103 : 3597 : array = build_array_of_n_type (elttype, len);
9104 : 3597 : array = finish_compound_literal (array, new_ctor, complain);
9105 : : /* This is dubious now, should be blessed by P2752. */
9106 : 3597 : DECL_MERGEABLE (TARGET_EXPR_SLOT (array)) = true;
9107 : 3597 : array = cp_build_addr_expr (array, complain);
9108 : : }
9109 : : else
9110 : 113 : array = nullptr_node;
9111 : :
9112 : 3842 : array = cp_convert (build_pointer_type (elttype), array, complain);
9113 : 3842 : if (array == error_mark_node)
9114 : : return error_mark_node;
9115 : :
9116 : : /* Build up the initializer_list object. Note: fail gracefully
9117 : : if the object cannot be completed because, for example, no
9118 : : definition is provided (c++/80956). */
9119 : 3842 : totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain);
9120 : 3842 : if (!totype)
9121 : 0 : return error_mark_node;
9122 : 3842 : tree field = next_aggregate_field (TYPE_FIELDS (totype));
9123 : 3842 : vec<constructor_elt, va_gc> *vec = NULL;
9124 : 3842 : CONSTRUCTOR_APPEND_ELT (vec, field, array);
9125 : 3842 : field = next_aggregate_field (DECL_CHAIN (field));
9126 : 3842 : CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
9127 : 3842 : tree new_ctor = build_constructor (totype, vec);
9128 : 3842 : return get_target_expr (new_ctor, complain);
9129 : : }
9130 : :
9131 : 1130242 : case ck_aggr:
9132 : 1130242 : if (TREE_CODE (totype) == COMPLEX_TYPE)
9133 : : {
9134 : 29382 : tree real = CONSTRUCTOR_ELT (expr, 0)->value;
9135 : 29382 : tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
9136 : 29382 : real = perform_implicit_conversion (TREE_TYPE (totype),
9137 : : real, complain);
9138 : 29382 : imag = perform_implicit_conversion (TREE_TYPE (totype),
9139 : : imag, complain);
9140 : 29382 : expr = build2 (COMPLEX_EXPR, totype, real, imag);
9141 : 29382 : return expr;
9142 : : }
9143 : 1100860 : expr = reshape_init (totype, expr, complain);
9144 : 1100860 : expr = get_target_expr (digest_init (totype, expr, complain),
9145 : : complain);
9146 : 1100860 : if (expr != error_mark_node)
9147 : 1100847 : TARGET_EXPR_LIST_INIT_P (expr) = true;
9148 : : return expr;
9149 : :
9150 : 228683862 : default:
9151 : 228683862 : break;
9152 : 228683862 : };
9153 : :
9154 : 228683862 : conversion *nc = next_conversion (convs);
9155 : 228683862 : if (convs->kind == ck_ref_bind && nc->kind == ck_qual
9156 : 12902 : && !convs->need_temporary_p)
9157 : : /* direct_reference_binding might have inserted a ck_qual under
9158 : : this ck_ref_bind for the benefit of conversion sequence ranking.
9159 : : Don't actually perform that conversion. */
9160 : 9504 : nc = next_conversion (nc);
9161 : :
9162 : 228683862 : expr = convert_like (nc, expr, fn, argnum,
9163 : : convs->kind == ck_ref_bind
9164 : 228683862 : ? issue_conversion_warnings : false,
9165 : : c_cast_p, /*nested_p=*/true, complain & ~tf_no_cleanup);
9166 : 228683862 : if (expr == error_mark_node)
9167 : : return error_mark_node;
9168 : :
9169 : 228683644 : switch (convs->kind)
9170 : : {
9171 : 109833826 : case ck_rvalue:
9172 : 109833826 : expr = decay_conversion (expr, complain);
9173 : 109833826 : if (expr == error_mark_node)
9174 : : {
9175 : 15 : if (complain & tf_error)
9176 : : {
9177 : 12 : auto_diagnostic_group d;
9178 : 12 : maybe_print_user_conv_context (convs);
9179 : 12 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9180 : 12 : }
9181 : 15 : return error_mark_node;
9182 : : }
9183 : :
9184 : 109833811 : if ((complain & tf_warning) && fn
9185 : 36755796 : && warn_suggest_attribute_format)
9186 : : {
9187 : 690 : tree rhstype = TREE_TYPE (expr);
9188 : 690 : const enum tree_code coder = TREE_CODE (rhstype);
9189 : 690 : const enum tree_code codel = TREE_CODE (totype);
9190 : 690 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9191 : 250 : && coder == codel
9192 : 940 : && check_missing_format_attribute (totype, rhstype))
9193 : 6 : warning (OPT_Wsuggest_attribute_format,
9194 : : "argument of function call might be a candidate "
9195 : : "for a format attribute");
9196 : : }
9197 : :
9198 : 109833811 : if (! MAYBE_CLASS_TYPE_P (totype))
9199 : 99278292 : return maybe_adjust_type_name (totype, expr, convs->kind);
9200 : :
9201 : : /* Don't introduce copies when passing arguments along to the inherited
9202 : : constructor. */
9203 : 10555519 : if (current_function_decl
9204 : 7995411 : && flag_new_inheriting_ctors
9205 : 26545861 : && DECL_INHERITED_CTOR (current_function_decl))
9206 : : return expr;
9207 : :
9208 : 10548846 : if (TREE_CODE (expr) == TARGET_EXPR
9209 : 10548846 : && TARGET_EXPR_LIST_INIT_P (expr))
9210 : : /* Copy-list-initialization doesn't actually involve a copy. */
9211 : : return expr;
9212 : :
9213 : : /* Fall through. */
9214 : 12032230 : case ck_base:
9215 : 12032230 : if (convs->kind == ck_base && !convs->need_temporary_p)
9216 : : {
9217 : : /* We are going to bind a reference directly to a base-class
9218 : : subobject of EXPR. */
9219 : : /* Build an expression for `*((base*) &expr)'. */
9220 : 1270556 : expr = convert_to_base (expr, totype,
9221 : 1270556 : !c_cast_p, /*nonnull=*/true, complain);
9222 : 1270556 : return expr;
9223 : : }
9224 : :
9225 : : /* Copy-initialization where the cv-unqualified version of the source
9226 : : type is the same class as, or a derived class of, the class of the
9227 : : destination [is treated as direct-initialization]. [dcl.init] */
9228 : 10761674 : flags = LOOKUP_NORMAL;
9229 : : /* This conversion is being done in the context of a user-defined
9230 : : conversion (i.e. the second step of copy-initialization), so
9231 : : don't allow any more. */
9232 : 10761674 : if (convs->user_conv_p)
9233 : 220760 : flags |= LOOKUP_NO_CONVERSION;
9234 : : /* We might be performing a conversion of the argument
9235 : : to the user-defined conversion, i.e., not a conversion of the
9236 : : result of the user-defined conversion. In which case we skip
9237 : : explicit constructors. */
9238 : 10761674 : if (convs->copy_init_p)
9239 : 10547104 : flags |= LOOKUP_ONLYCONVERTING;
9240 : 10761674 : expr = build_temp (expr, totype, flags, &diag_kind, complain);
9241 : 10761674 : if (diag_kind != diagnostics::kind::unspecified && complain)
9242 : : {
9243 : 78 : auto_diagnostic_group d;
9244 : 78 : maybe_print_user_conv_context (convs);
9245 : 78 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9246 : 78 : }
9247 : :
9248 : 10761674 : return build_cplus_new (totype, expr, complain);
9249 : :
9250 : 45030417 : case ck_ref_bind:
9251 : 45030417 : {
9252 : 45030417 : tree ref_type = totype;
9253 : :
9254 : 45030417 : if (convs->bad_p && !next_conversion (convs)->bad_p)
9255 : : {
9256 : 5125 : tree extype = TREE_TYPE (expr);
9257 : 5125 : auto_diagnostic_group d;
9258 : 5125 : if (TYPE_REF_IS_RVALUE (ref_type)
9259 : 5125 : && lvalue_p (expr))
9260 : 1634 : error_at (loc, "cannot bind rvalue reference of type %qH to "
9261 : : "lvalue of type %qI", totype, extype);
9262 : 6136 : else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
9263 : 4962 : && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
9264 : : {
9265 : 1222 : conversion *next = next_conversion (convs);
9266 : 1222 : if (next->kind == ck_std)
9267 : : {
9268 : 64 : next = next_conversion (next);
9269 : 64 : error_at (loc, "cannot bind non-const lvalue reference of "
9270 : : "type %qH to a value of type %qI",
9271 : : totype, next->type);
9272 : : }
9273 : 1158 : else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type)))
9274 : 817 : error_at (loc, "cannot bind non-const lvalue reference of "
9275 : : "type %qH to an rvalue of type %qI", totype, extype);
9276 : : else // extype is volatile
9277 : 341 : error_at (loc, "cannot bind lvalue reference of type "
9278 : : "%qH to an rvalue of type %qI", totype,
9279 : : extype);
9280 : : }
9281 : 2269 : else if (!reference_compatible_p (TREE_TYPE (totype), extype))
9282 : : {
9283 : : /* If we're converting from T[] to T[N], don't talk
9284 : : about discarding qualifiers. (Converting from T[N] to
9285 : : T[] is allowed by P0388R4.) */
9286 : 2269 : if (TREE_CODE (extype) == ARRAY_TYPE
9287 : 27 : && TYPE_DOMAIN (extype) == NULL_TREE
9288 : 15 : && TREE_CODE (TREE_TYPE (totype)) == ARRAY_TYPE
9289 : 2284 : && TYPE_DOMAIN (TREE_TYPE (totype)) != NULL_TREE)
9290 : 15 : error_at (loc, "cannot bind reference of type %qH to %qI "
9291 : : "due to different array bounds", totype, extype);
9292 : : else
9293 : 2254 : error_at (loc, "binding reference of type %qH to %qI "
9294 : : "discards qualifiers", totype, extype);
9295 : : }
9296 : : else
9297 : 0 : gcc_unreachable ();
9298 : 5125 : maybe_print_user_conv_context (convs);
9299 : 5125 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9300 : :
9301 : 5125 : return error_mark_node;
9302 : 5125 : }
9303 : 45025292 : else if (complain & tf_warning)
9304 : 29312689 : maybe_warn_array_conv (loc, convs, expr);
9305 : :
9306 : : /* If necessary, create a temporary.
9307 : :
9308 : : VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
9309 : : that need temporaries, even when their types are reference
9310 : : compatible with the type of reference being bound, so the
9311 : : upcoming call to cp_build_addr_expr doesn't fail. */
9312 : 45025292 : if (convs->need_temporary_p
9313 : 43020627 : || TREE_CODE (expr) == CONSTRUCTOR
9314 : 43020626 : || TREE_CODE (expr) == VA_ARG_EXPR)
9315 : : {
9316 : : /* Otherwise, a temporary of type "cv1 T1" is created and
9317 : : initialized from the initializer expression using the rules
9318 : : for a non-reference copy-initialization (8.5). */
9319 : :
9320 : 2004666 : tree type = TREE_TYPE (ref_type);
9321 : 2004666 : cp_lvalue_kind lvalue = lvalue_kind (expr);
9322 : :
9323 : 2004666 : gcc_assert (similar_type_p (type, next_conversion (convs)->type));
9324 : 2004666 : if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
9325 : 2805309 : && !TYPE_REF_IS_RVALUE (ref_type))
9326 : : {
9327 : : /* If the reference is volatile or non-const, we
9328 : : cannot create a temporary. */
9329 : 168 : if (complain & tf_error)
9330 : : {
9331 : 167 : if (lvalue & clk_bitfield)
9332 : 30 : error_at (loc, "cannot bind bit-field %qE to %qT",
9333 : : expr, ref_type);
9334 : 137 : else if (lvalue & clk_packed)
9335 : 9 : error_at (loc, "cannot bind packed field %qE to %qT",
9336 : : expr, ref_type);
9337 : : else
9338 : 128 : error_at (loc, "cannot bind rvalue %qE to %qT",
9339 : : expr, ref_type);
9340 : : }
9341 : 168 : return error_mark_node;
9342 : : }
9343 : : /* If the source is a packed field, and we must use a copy
9344 : : constructor, then building the target expr will require
9345 : : binding the field to the reference parameter to the
9346 : : copy constructor, and we'll end up with an infinite
9347 : : loop. If we can use a bitwise copy, then we'll be
9348 : : OK. */
9349 : 2004498 : if ((lvalue & clk_packed)
9350 : 20 : && CLASS_TYPE_P (type)
9351 : 2004515 : && type_has_nontrivial_copy_init (type))
9352 : : {
9353 : 6 : error_at (loc, "cannot bind packed field %qE to %qT",
9354 : : expr, ref_type);
9355 : 6 : return error_mark_node;
9356 : : }
9357 : 2004492 : if (lvalue & clk_bitfield)
9358 : : {
9359 : 24 : expr = convert_bitfield_to_declared_type (expr);
9360 : 24 : expr = fold_convert (type, expr);
9361 : : }
9362 : :
9363 : : /* Creating &TARGET_EXPR<> in a template would break when
9364 : : tsubsting the expression, so use an IMPLICIT_CONV_EXPR
9365 : : instead. This can happen even when there's no class
9366 : : involved, e.g., when converting an integer to a reference
9367 : : type. */
9368 : 2004492 : if (processing_template_decl)
9369 : 2463 : return build1 (IMPLICIT_CONV_EXPR, totype, expr);
9370 : 2002029 : expr = build_target_expr_with_type (expr, type, complain);
9371 : : }
9372 : :
9373 : : /* Take the address of the thing to which we will bind the
9374 : : reference. */
9375 : 45022655 : expr = cp_build_addr_expr (expr, complain);
9376 : 45022655 : if (expr == error_mark_node)
9377 : : return error_mark_node;
9378 : :
9379 : : /* Convert it to a pointer to the type referred to by the
9380 : : reference. This will adjust the pointer if a derived to
9381 : : base conversion is being performed. */
9382 : 45022655 : expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
9383 : : expr, complain);
9384 : : /* Convert the pointer to the desired reference type. */
9385 : 45022655 : return build_nop (ref_type, expr);
9386 : : }
9387 : :
9388 : 2477057 : case ck_lvalue:
9389 : 2477057 : return decay_conversion (expr, complain);
9390 : :
9391 : 221990 : case ck_fnptr:
9392 : : /* ??? Should the address of a transaction-safe pointer point to the TM
9393 : : clone, and this conversion look up the primary function? */
9394 : 221990 : return build_nop (totype, expr);
9395 : :
9396 : 1112250 : case ck_qual:
9397 : : /* Warn about deprecated conversion if appropriate. */
9398 : 1112250 : if (complain & tf_warning)
9399 : : {
9400 : 1059784 : string_conv_p (totype, expr, 1);
9401 : 1059784 : maybe_warn_array_conv (loc, convs, expr);
9402 : : }
9403 : : break;
9404 : :
9405 : 2255792 : case ck_ptr:
9406 : 2255792 : if (convs->base_p)
9407 : 141041 : expr = convert_to_base (expr, totype, !c_cast_p,
9408 : : /*nonnull=*/false, complain);
9409 : 2255792 : return build_nop (totype, expr);
9410 : :
9411 : 29709 : case ck_pmem:
9412 : 29709 : return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
9413 : 29709 : c_cast_p, complain);
9414 : :
9415 : : default:
9416 : : break;
9417 : : }
9418 : :
9419 : 67325232 : if (convs->check_narrowing
9420 : 87804520 : && !check_narrowing (totype, expr, complain,
9421 : 20479288 : convs->check_narrowing_const_only))
9422 : 456 : return error_mark_node;
9423 : :
9424 : 134649552 : warning_sentinel w (warn_zero_as_null_pointer_constant);
9425 : 67324776 : if (issue_conversion_warnings)
9426 : 45449972 : expr = cp_convert_and_check (totype, expr, complain);
9427 : : else
9428 : : {
9429 : 21874804 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
9430 : 36 : expr = TREE_OPERAND (expr, 0);
9431 : 21874804 : expr = cp_convert (totype, expr, complain);
9432 : : }
9433 : :
9434 : 67324776 : return expr;
9435 : : }
9436 : :
9437 : : /* Return true if converting FROM to TO is unsafe in a template. */
9438 : :
9439 : : static bool
9440 : 1502961 : conv_unsafe_in_template_p (tree to, tree from)
9441 : : {
9442 : : /* Converting classes involves TARGET_EXPR. */
9443 : 1502961 : if (CLASS_TYPE_P (to) || CLASS_TYPE_P (from))
9444 : : return true;
9445 : :
9446 : : /* Converting real to integer produces FIX_TRUNC_EXPR which tsubst
9447 : : doesn't handle. */
9448 : 1145872 : if (SCALAR_FLOAT_TYPE_P (from) && INTEGRAL_OR_ENUMERATION_TYPE_P (to))
9449 : : return true;
9450 : :
9451 : : /* Converting integer to real isn't a trivial conversion, either. */
9452 : 1145869 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (from) && SCALAR_FLOAT_TYPE_P (to))
9453 : 3 : return true;
9454 : :
9455 : : return false;
9456 : : }
9457 : :
9458 : : /* Wrapper for convert_like_internal that handles creating
9459 : : IMPLICIT_CONV_EXPR. */
9460 : :
9461 : : static tree
9462 : 832063397 : convert_like (conversion *convs, tree expr, tree fn, int argnum,
9463 : : bool issue_conversion_warnings, bool c_cast_p, bool nested_p,
9464 : : tsubst_flags_t complain)
9465 : : {
9466 : : /* Creating &TARGET_EXPR<> in a template breaks when substituting,
9467 : : and creating a CALL_EXPR in a template breaks in finish_call_expr
9468 : : so use an IMPLICIT_CONV_EXPR for this conversion. We would have
9469 : : created such codes e.g. when calling a user-defined conversion
9470 : : function. */
9471 : 832063397 : tree conv_expr = NULL_TREE;
9472 : 832063397 : if (processing_template_decl
9473 : 93545469 : && convs->kind != ck_identity
9474 : 833566358 : && conv_unsafe_in_template_p (convs->type, TREE_TYPE (expr)))
9475 : : {
9476 : 357095 : conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr);
9477 : 357095 : if (convs->kind != ck_ref_bind)
9478 : 41614 : conv_expr = convert_from_reference (conv_expr);
9479 : 357095 : if (!convs->bad_p)
9480 : : return conv_expr;
9481 : : /* Do the normal processing to give the bad_p errors. But we still
9482 : : need to return the IMPLICIT_CONV_EXPR, unless we're returning
9483 : : error_mark_node. */
9484 : : }
9485 : 831706311 : expr = convert_like_internal (convs, expr, fn, argnum,
9486 : : issue_conversion_warnings, c_cast_p,
9487 : : nested_p, complain);
9488 : 831706311 : if (expr == error_mark_node)
9489 : : return error_mark_node;
9490 : 831663116 : return conv_expr ? conv_expr : expr;
9491 : : }
9492 : :
9493 : : /* Convenience wrapper for convert_like. */
9494 : :
9495 : : static inline tree
9496 : 462329220 : convert_like (conversion *convs, tree expr, tsubst_flags_t complain)
9497 : : {
9498 : 462329220 : return convert_like (convs, expr, NULL_TREE, 0,
9499 : : /*issue_conversion_warnings=*/true,
9500 : 462329220 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9501 : : }
9502 : :
9503 : : /* Convenience wrapper for convert_like. */
9504 : :
9505 : : static inline tree
9506 : 105185459 : convert_like_with_context (conversion *convs, tree expr, tree fn, int argnum,
9507 : : tsubst_flags_t complain)
9508 : : {
9509 : 0 : return convert_like (convs, expr, fn, argnum,
9510 : : /*issue_conversion_warnings=*/true,
9511 : : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9512 : : }
9513 : :
9514 : : /* ARG is being passed to a varargs function. Perform any conversions
9515 : : required. Return the converted value. */
9516 : :
9517 : : tree
9518 : 1487021 : convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
9519 : : {
9520 : 1487021 : tree arg_type = TREE_TYPE (arg);
9521 : 1487021 : location_t loc = cp_expr_loc_or_input_loc (arg);
9522 : :
9523 : : /* [expr.call]
9524 : :
9525 : : If the argument has integral or enumeration type that is subject
9526 : : to the integral promotions (_conv.prom_), or a floating-point
9527 : : type that is subject to the floating-point promotion
9528 : : (_conv.fpprom_), the value of the argument is converted to the
9529 : : promoted type before the call. */
9530 : 1487021 : if (SCALAR_FLOAT_TYPE_P (arg_type)
9531 : 46259 : && (TYPE_PRECISION (arg_type)
9532 : 46259 : < TYPE_PRECISION (double_type_node))
9533 : 11276 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type))
9534 : 1497619 : && !extended_float_type_p (arg_type))
9535 : : {
9536 : 10594 : if ((complain & tf_warning)
9537 : 10591 : && warn_double_promotion && !c_inhibit_evaluation_warnings)
9538 : 3 : warning_at (loc, OPT_Wdouble_promotion,
9539 : : "implicit conversion from %qH to %qI when passing "
9540 : : "argument to function",
9541 : : arg_type, double_type_node);
9542 : 10594 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
9543 : 3 : arg = TREE_OPERAND (arg, 0);
9544 : 10594 : arg = mark_rvalue_use (arg);
9545 : 10594 : arg = convert_to_real_nofold (double_type_node, arg);
9546 : : }
9547 : 1476427 : else if (NULLPTR_TYPE_P (arg_type))
9548 : : {
9549 : 70 : arg = mark_rvalue_use (arg);
9550 : 70 : if (TREE_SIDE_EFFECTS (arg))
9551 : : {
9552 : 6 : warning_sentinel w(warn_unused_result);
9553 : 6 : arg = cp_build_compound_expr (arg, null_pointer_node, complain);
9554 : 6 : }
9555 : : else
9556 : 64 : arg = null_pointer_node;
9557 : : }
9558 : 1476357 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
9559 : : {
9560 : 1018330 : if (SCOPED_ENUM_P (arg_type))
9561 : : {
9562 : 63 : tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
9563 : : complain);
9564 : 63 : prom = cp_perform_integral_promotions (prom, complain);
9565 : 123 : if (abi_version_crosses (6)
9566 : 30 : && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
9567 : 93 : && (complain & tf_warning))
9568 : 60 : warning_at (loc, OPT_Wabi, "scoped enum %qT passed through %<...%>"
9569 : : " as %qT before %<-fabi-version=6%>, %qT after",
9570 : : arg_type,
9571 : 30 : TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
9572 : 63 : if (!abi_version_at_least (6))
9573 : 1487021 : arg = prom;
9574 : : }
9575 : : else
9576 : 1018267 : arg = cp_perform_integral_promotions (arg, complain);
9577 : : }
9578 : : else
9579 : : /* [expr.call]
9580 : :
9581 : : The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9582 : : standard conversions are performed. */
9583 : 458027 : arg = decay_conversion (arg, complain);
9584 : :
9585 : 1487021 : arg = require_complete_type (arg, complain);
9586 : 1487021 : arg_type = TREE_TYPE (arg);
9587 : :
9588 : 1487021 : if (arg != error_mark_node
9589 : : /* In a template (or ill-formed code), we can have an incomplete type
9590 : : even after require_complete_type, in which case we don't know
9591 : : whether it has trivial copy or not. */
9592 : 1487009 : && COMPLETE_TYPE_P (arg_type)
9593 : 2974030 : && !cp_unevaluated_operand)
9594 : : {
9595 : : /* [expr.call] 5.2.2/7:
9596 : : Passing a potentially-evaluated argument of class type (Clause 9)
9597 : : with a non-trivial copy constructor or a non-trivial destructor
9598 : : with no corresponding parameter is conditionally-supported, with
9599 : : implementation-defined semantics.
9600 : :
9601 : : We support it as pass-by-invisible-reference, just like a normal
9602 : : value parameter.
9603 : :
9604 : : If the call appears in the context of a sizeof expression,
9605 : : it is not potentially-evaluated. */
9606 : 650115 : if (type_has_nontrivial_copy_init (arg_type)
9607 : 650115 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
9608 : : {
9609 : 33 : arg = force_rvalue (arg, complain);
9610 : 33 : if (complain & tf_warning)
9611 : 33 : warning (OPT_Wconditionally_supported,
9612 : : "passing objects of non-trivially-copyable "
9613 : : "type %q#T through %<...%> is conditionally supported",
9614 : : arg_type);
9615 : 33 : return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
9616 : : }
9617 : : /* Build up a real lvalue-to-rvalue conversion in case the
9618 : : copy constructor is trivial but not callable. */
9619 : 650082 : else if (CLASS_TYPE_P (arg_type))
9620 : 44003 : force_rvalue (arg, complain);
9621 : :
9622 : : }
9623 : :
9624 : : return arg;
9625 : : }
9626 : :
9627 : : /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
9628 : :
9629 : : tree
9630 : 31136 : build_x_va_arg (location_t loc, tree expr, tree type)
9631 : : {
9632 : 31136 : if (processing_template_decl)
9633 : : {
9634 : 39 : tree r = build_min (VA_ARG_EXPR, type, expr);
9635 : 39 : SET_EXPR_LOCATION (r, loc);
9636 : 39 : return r;
9637 : : }
9638 : :
9639 : 31097 : type = complete_type_or_else (type, NULL_TREE);
9640 : :
9641 : 31097 : if (expr == error_mark_node || !type)
9642 : : return error_mark_node;
9643 : :
9644 : 31076 : expr = mark_lvalue_use (expr);
9645 : :
9646 : 31076 : if (TYPE_REF_P (type))
9647 : : {
9648 : 6 : error ("cannot receive reference type %qT through %<...%>", type);
9649 : 6 : return error_mark_node;
9650 : : }
9651 : :
9652 : 31070 : if (type_has_nontrivial_copy_init (type)
9653 : 31070 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9654 : : {
9655 : : /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
9656 : : it as pass by invisible reference. */
9657 : 12 : warning_at (loc, OPT_Wconditionally_supported,
9658 : : "receiving objects of non-trivially-copyable type %q#T "
9659 : : "through %<...%> is conditionally-supported", type);
9660 : :
9661 : 12 : tree ref = cp_build_reference_type (type, false);
9662 : 12 : expr = build_va_arg (loc, expr, ref);
9663 : 12 : return convert_from_reference (expr);
9664 : : }
9665 : :
9666 : 31058 : tree ret = build_va_arg (loc, expr, type);
9667 : 31058 : if (CLASS_TYPE_P (type))
9668 : : /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
9669 : : know how to handle it. */
9670 : 12094 : ret = get_target_expr (ret);
9671 : : return ret;
9672 : : }
9673 : :
9674 : : /* TYPE has been given to va_arg. Apply the default conversions which
9675 : : would have happened when passed via ellipsis. Return the promoted
9676 : : type, or the passed type if there is no change. */
9677 : :
9678 : : tree
9679 : 2403088 : cxx_type_promotes_to (tree type)
9680 : : {
9681 : 2403088 : tree promote;
9682 : :
9683 : : /* Perform the array-to-pointer and function-to-pointer
9684 : : conversions. */
9685 : 2403088 : type = type_decays_to (type);
9686 : :
9687 : 2403088 : promote = type_promotes_to (type);
9688 : 2403088 : if (same_type_p (type, promote))
9689 : 2403064 : promote = type;
9690 : :
9691 : 2403088 : return promote;
9692 : : }
9693 : :
9694 : : /* ARG is a default argument expression being passed to a parameter of
9695 : : the indicated TYPE, which is a parameter to FN. PARMNUM is the
9696 : : zero-based argument number. Do any required conversions. Return
9697 : : the converted value. */
9698 : :
9699 : : static GTY(()) vec<tree, va_gc> *default_arg_context;
9700 : : void
9701 : 6148750 : push_defarg_context (tree fn)
9702 : 6148750 : { vec_safe_push (default_arg_context, fn); }
9703 : :
9704 : : void
9705 : 6148750 : pop_defarg_context (void)
9706 : 6148750 : { default_arg_context->pop (); }
9707 : :
9708 : : tree
9709 : 1117090 : convert_default_arg (tree type, tree arg, tree fn, int parmnum,
9710 : : tsubst_flags_t complain)
9711 : : {
9712 : 1117090 : int i;
9713 : 1117090 : tree t;
9714 : :
9715 : : /* See through clones. */
9716 : 1117090 : fn = DECL_ORIGIN (fn);
9717 : : /* And inheriting ctors. */
9718 : 1117090 : if (flag_new_inheriting_ctors)
9719 : 1116450 : fn = strip_inheriting_ctors (fn);
9720 : :
9721 : : /* Detect recursion. */
9722 : 1117954 : FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
9723 : 870 : if (t == fn)
9724 : : {
9725 : 6 : if (complain & tf_error)
9726 : 6 : error ("recursive evaluation of default argument for %q#D", fn);
9727 : 6 : return error_mark_node;
9728 : : }
9729 : :
9730 : : /* If the ARG is an unparsed default argument expression, the
9731 : : conversion cannot be performed. */
9732 : 1117084 : if (TREE_CODE (arg) == DEFERRED_PARSE)
9733 : : {
9734 : 15 : if (complain & tf_error)
9735 : 15 : error ("call to %qD uses the default argument for parameter %P, which "
9736 : : "is not yet defined", fn, parmnum);
9737 : 15 : return error_mark_node;
9738 : : }
9739 : :
9740 : 1117069 : push_defarg_context (fn);
9741 : :
9742 : 1117069 : if (fn && DECL_TEMPLATE_INFO (fn))
9743 : 865233 : arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
9744 : :
9745 : : /* Due to:
9746 : :
9747 : : [dcl.fct.default]
9748 : :
9749 : : The names in the expression are bound, and the semantic
9750 : : constraints are checked, at the point where the default
9751 : : expressions appears.
9752 : :
9753 : : we must not perform access checks here. */
9754 : 1117069 : push_deferring_access_checks (dk_no_check);
9755 : : /* We must make a copy of ARG, in case subsequent processing
9756 : : alters any part of it. */
9757 : 1117069 : arg = break_out_target_exprs (arg, /*clear location*/true);
9758 : :
9759 : 1117069 : arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
9760 : : ICR_DEFAULT_ARGUMENT, fn, parmnum,
9761 : : complain);
9762 : 1117069 : arg = convert_for_arg_passing (type, arg, complain);
9763 : 1117069 : pop_deferring_access_checks();
9764 : :
9765 : 1117069 : pop_defarg_context ();
9766 : :
9767 : 1117069 : return arg;
9768 : : }
9769 : :
9770 : : /* Returns the type which will really be used for passing an argument of
9771 : : type TYPE. */
9772 : :
9773 : : tree
9774 : 556112572 : type_passed_as (tree type)
9775 : : {
9776 : : /* Pass classes with copy ctors by invisible reference. */
9777 : 556112572 : if (TREE_ADDRESSABLE (type))
9778 : 593055 : type = build_reference_type (type);
9779 : :
9780 : 556112572 : return type;
9781 : : }
9782 : :
9783 : : /* Actually perform the appropriate conversion. */
9784 : :
9785 : : tree
9786 : 110238265 : convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
9787 : : {
9788 : 110238265 : tree bitfield_type;
9789 : :
9790 : : /* If VAL is a bitfield, then -- since it has already been converted
9791 : : to TYPE -- it cannot have a precision greater than TYPE.
9792 : :
9793 : : If it has a smaller precision, we must widen it here. For
9794 : : example, passing "int f:3;" to a function expecting an "int" will
9795 : : not result in any conversion before this point.
9796 : :
9797 : : If the precision is the same we must not risk widening. For
9798 : : example, the COMPONENT_REF for a 32-bit "long long" bitfield will
9799 : : often have type "int", even though the C++ type for the field is
9800 : : "long long". If the value is being passed to a function
9801 : : expecting an "int", then no conversions will be required. But,
9802 : : if we call convert_bitfield_to_declared_type, the bitfield will
9803 : : be converted to "long long". */
9804 : 110238265 : bitfield_type = is_bitfield_expr_with_lowered_type (val);
9805 : 110238265 : if (bitfield_type
9806 : 110238265 : && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
9807 : 0 : val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
9808 : :
9809 : 110238265 : if (val == error_mark_node)
9810 : : ;
9811 : : /* Pass classes with copy ctors by invisible reference. */
9812 : 110235673 : else if (TREE_ADDRESSABLE (type))
9813 : 205282 : val = build1 (ADDR_EXPR, build_reference_type (type), val);
9814 : 110238265 : if (complain & tf_warning)
9815 : 108421528 : maybe_warn_parm_abi (type, cp_expr_loc_or_input_loc (val));
9816 : :
9817 : 87801404 : if (complain & tf_warning)
9818 : 87801404 : warn_for_address_of_packed_member (type, val);
9819 : :
9820 : : /* gimplify_arg elides TARGET_EXPRs that initialize a function argument,
9821 : : unless the initializer is a CONSTRUCTOR. In that case, we fail to
9822 : : elide the copy anyway. See that function for more information. */
9823 : 6211730 : if (SIMPLE_TARGET_EXPR_P (val)
9824 : 115711983 : && TREE_CODE (TARGET_EXPR_INITIAL (val)) != CONSTRUCTOR)
9825 : 3032631 : set_target_expr_eliding (val);
9826 : :
9827 : 110238265 : return val;
9828 : : }
9829 : :
9830 : : /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
9831 : : which just decay_conversion or no conversions at all should be done.
9832 : : This is true for some builtins which don't act like normal functions.
9833 : : Return 2 if just decay_conversion and removal of excess precision should
9834 : : be done, 1 if just decay_conversion. Return 3 for special treatment of
9835 : : the 3rd argument for __builtin_*_overflow_p. Return 4 for special
9836 : : treatment of the 1st argument for
9837 : : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
9838 : :
9839 : : int
9840 : 131667491 : magic_varargs_p (tree fn)
9841 : : {
9842 : 131667491 : if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
9843 : 5757108 : switch (DECL_FUNCTION_CODE (fn))
9844 : : {
9845 : : case BUILT_IN_CLASSIFY_TYPE:
9846 : : case BUILT_IN_CONSTANT_P:
9847 : : case BUILT_IN_NEXT_ARG:
9848 : : case BUILT_IN_VA_START:
9849 : : return 1;
9850 : :
9851 : 20842 : case BUILT_IN_ADD_OVERFLOW_P:
9852 : 20842 : case BUILT_IN_SUB_OVERFLOW_P:
9853 : 20842 : case BUILT_IN_MUL_OVERFLOW_P:
9854 : 20842 : return 3;
9855 : :
9856 : 249893 : case BUILT_IN_ISFINITE:
9857 : 249893 : case BUILT_IN_ISINF:
9858 : 249893 : case BUILT_IN_ISINF_SIGN:
9859 : 249893 : case BUILT_IN_ISNAN:
9860 : 249893 : case BUILT_IN_ISNORMAL:
9861 : 249893 : case BUILT_IN_FPCLASSIFY:
9862 : 249893 : return 2;
9863 : :
9864 : 57047 : case BUILT_IN_CLZG:
9865 : 57047 : case BUILT_IN_CTZG:
9866 : 57047 : case BUILT_IN_CLRSBG:
9867 : 57047 : case BUILT_IN_FFSG:
9868 : 57047 : case BUILT_IN_PARITYG:
9869 : 57047 : case BUILT_IN_POPCOUNTG:
9870 : 57047 : return 4;
9871 : :
9872 : 5325737 : default:
9873 : 5325737 : return lookup_attribute ("type generic",
9874 : 10651474 : TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
9875 : : }
9876 : :
9877 : : return 0;
9878 : : }
9879 : :
9880 : : /* Returns the decl of the dispatcher function if FN is a function version. */
9881 : :
9882 : : tree
9883 : 237 : get_function_version_dispatcher (tree fn)
9884 : : {
9885 : 237 : tree dispatcher_decl = NULL;
9886 : :
9887 : 237 : if (DECL_LOCAL_DECL_P (fn))
9888 : 9 : fn = DECL_LOCAL_DECL_ALIAS (fn);
9889 : :
9890 : 237 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9891 : : && DECL_FUNCTION_VERSIONED (fn));
9892 : :
9893 : 237 : gcc_assert (targetm.get_function_versions_dispatcher);
9894 : 237 : dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
9895 : :
9896 : 237 : if (dispatcher_decl == NULL)
9897 : : {
9898 : 9 : error_at (input_location, "use of multiversioned function "
9899 : : "without a default");
9900 : 9 : return NULL;
9901 : : }
9902 : :
9903 : 228 : retrofit_lang_decl (dispatcher_decl);
9904 : 228 : gcc_assert (dispatcher_decl != NULL);
9905 : : return dispatcher_decl;
9906 : : }
9907 : :
9908 : : /* fn is a function version dispatcher that is marked used. Mark all the
9909 : : semantically identical function versions it will dispatch as used. */
9910 : :
9911 : : void
9912 : 156 : mark_versions_used (tree fn)
9913 : : {
9914 : 156 : struct cgraph_node *node;
9915 : 156 : struct cgraph_function_version_info *node_v;
9916 : 156 : struct cgraph_function_version_info *it_v;
9917 : :
9918 : 156 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9919 : :
9920 : 156 : node = cgraph_node::get (fn);
9921 : 156 : if (node == NULL)
9922 : : return;
9923 : :
9924 : 156 : gcc_assert (node->dispatcher_function);
9925 : :
9926 : 156 : node_v = node->function_version ();
9927 : 156 : if (node_v == NULL)
9928 : : return;
9929 : :
9930 : : /* All semantically identical versions are chained. Traverse and mark each
9931 : : one of them as used. */
9932 : 156 : it_v = node_v->next;
9933 : 1122 : while (it_v != NULL)
9934 : : {
9935 : 966 : mark_used (it_v->this_node->decl);
9936 : 966 : it_v = it_v->next;
9937 : : }
9938 : : }
9939 : :
9940 : : /* Build a call to "the copy constructor" for the type of A, even if it
9941 : : wouldn't be selected by normal overload resolution. Used for
9942 : : diagnostics. */
9943 : :
9944 : : static tree
9945 : 3 : call_copy_ctor (tree a, tsubst_flags_t complain)
9946 : : {
9947 : 3 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
9948 : 3 : tree binfo = TYPE_BINFO (ctype);
9949 : 3 : tree copy = get_copy_ctor (ctype, complain);
9950 : 3 : copy = build_baselink (binfo, binfo, copy, NULL_TREE);
9951 : 3 : tree ob = build_dummy_object (ctype);
9952 : 3 : releasing_vec args (make_tree_vector_single (a));
9953 : 3 : tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
9954 : : LOOKUP_NORMAL, NULL, complain);
9955 : 3 : return r;
9956 : 3 : }
9957 : :
9958 : : /* Return the base constructor corresponding to COMPLETE_CTOR or NULL_TREE. */
9959 : :
9960 : : static tree
9961 : 42 : base_ctor_for (tree complete_ctor)
9962 : : {
9963 : 42 : tree clone;
9964 : 42 : FOR_EACH_CLONE (clone, DECL_CLONED_FUNCTION (complete_ctor))
9965 : 42 : if (DECL_BASE_CONSTRUCTOR_P (clone))
9966 : : return clone;
9967 : : return NULL_TREE;
9968 : : }
9969 : :
9970 : : /* Try to make EXP suitable to be used as the initializer for a base subobject,
9971 : : and return whether we were successful. EXP must have already been cleared
9972 : : by unsafe_copy_elision_p{,_opt}. */
9973 : :
9974 : : static bool
9975 : 206 : make_base_init_ok (tree exp)
9976 : : {
9977 : 206 : if (TREE_CODE (exp) == TARGET_EXPR)
9978 : 206 : exp = TARGET_EXPR_INITIAL (exp);
9979 : 209 : while (TREE_CODE (exp) == COMPOUND_EXPR)
9980 : 3 : exp = TREE_OPERAND (exp, 1);
9981 : 206 : if (TREE_CODE (exp) == COND_EXPR)
9982 : : {
9983 : 3 : bool ret = make_base_init_ok (TREE_OPERAND (exp, 2));
9984 : 3 : if (tree op1 = TREE_OPERAND (exp, 1))
9985 : : {
9986 : 3 : bool r1 = make_base_init_ok (op1);
9987 : : /* If unsafe_copy_elision_p was false, the arms should match. */
9988 : 3 : gcc_assert (r1 == ret);
9989 : : }
9990 : 3 : return ret;
9991 : : }
9992 : 203 : if (TREE_CODE (exp) != AGGR_INIT_EXPR)
9993 : : /* A trivial copy is OK. */
9994 : : return true;
9995 : 60 : if (!AGGR_INIT_VIA_CTOR_P (exp))
9996 : : /* unsafe_copy_elision_p_opt must have said this is OK. */
9997 : : return true;
9998 : 42 : tree fn = cp_get_callee_fndecl_nofold (exp);
9999 : 42 : if (DECL_BASE_CONSTRUCTOR_P (fn))
10000 : : return true;
10001 : 42 : gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (fn));
10002 : 42 : fn = base_ctor_for (fn);
10003 : 42 : if (!fn || DECL_HAS_VTT_PARM_P (fn))
10004 : : /* The base constructor has more parameters, so we can't just change the
10005 : : call target. It would be possible to splice in the appropriate
10006 : : arguments, but probably not worth the complexity. */
10007 : : return false;
10008 : 33 : mark_used (fn);
10009 : 33 : AGGR_INIT_EXPR_FN (exp) = build_address (fn);
10010 : 33 : return true;
10011 : : }
10012 : :
10013 : : /* Return 2 if T refers to a base, 1 if a potentially-overlapping field,
10014 : : neither of which can be used for return by invisible reference. We avoid
10015 : : doing C++17 mandatory copy elision for either of these cases.
10016 : :
10017 : : This returns non-zero even if the type of T has no tail padding that other
10018 : : data could be allocated into, because that depends on the particular ABI.
10019 : : unsafe_copy_elision_p_opt does consider whether there is padding. */
10020 : :
10021 : : int
10022 : 30744282 : unsafe_return_slot_p (tree t)
10023 : : {
10024 : : /* Check empty bases separately, they don't have fields. */
10025 : 30744282 : if (is_empty_base_ref (t))
10026 : : return 2;
10027 : :
10028 : : /* A delegating constructor might be used to initialize a base. */
10029 : 30284236 : if (current_function_decl
10030 : 40028104 : && DECL_CONSTRUCTOR_P (current_function_decl)
10031 : 33191812 : && (t == current_class_ref
10032 : 2818212 : || tree_strip_nop_conversions (t) == current_class_ptr))
10033 : 101961 : return 2;
10034 : :
10035 : 30182275 : STRIP_NOPS (t);
10036 : 30182275 : if (TREE_CODE (t) == ADDR_EXPR)
10037 : 52335 : t = TREE_OPERAND (t, 0);
10038 : 30182275 : if (TREE_CODE (t) == COMPONENT_REF)
10039 : 2602838 : t = TREE_OPERAND (t, 1);
10040 : 30182275 : if (TREE_CODE (t) != FIELD_DECL)
10041 : : return false;
10042 : 2638834 : if (!CLASS_TYPE_P (TREE_TYPE (t)))
10043 : : /* The middle-end will do the right thing for scalar types. */
10044 : : return false;
10045 : 2112655 : if (DECL_FIELD_IS_BASE (t))
10046 : : return 2;
10047 : 1426133 : if (lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (t)))
10048 : : return 1;
10049 : : return 0;
10050 : : }
10051 : :
10052 : : /* True IFF EXP is a prvalue that represents return by invisible reference. */
10053 : :
10054 : : static bool
10055 : 255400 : init_by_return_slot_p (tree exp)
10056 : : {
10057 : : /* Copy elision only happens with a TARGET_EXPR. */
10058 : 255403 : if (TREE_CODE (exp) != TARGET_EXPR)
10059 : : return false;
10060 : 24771 : tree init = TARGET_EXPR_INITIAL (exp);
10061 : : /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
10062 : 24777 : while (TREE_CODE (init) == COMPOUND_EXPR)
10063 : 6 : init = TREE_OPERAND (init, 1);
10064 : 24771 : if (TREE_CODE (init) == COND_EXPR)
10065 : : {
10066 : : /* We'll end up copying from each of the arms of the COND_EXPR directly
10067 : : into the target, so look at them. */
10068 : 6 : if (tree op = TREE_OPERAND (init, 1))
10069 : 6 : if (init_by_return_slot_p (op))
10070 : : return true;
10071 : 3 : return init_by_return_slot_p (TREE_OPERAND (init, 2));
10072 : : }
10073 : 24765 : return (TREE_CODE (init) == AGGR_INIT_EXPR
10074 : 24765 : && !AGGR_INIT_VIA_CTOR_P (init));
10075 : : }
10076 : :
10077 : : /* We can't elide a copy from a function returning by value to a
10078 : : potentially-overlapping subobject, as the callee might clobber tail padding.
10079 : : Return true iff this could be that case.
10080 : :
10081 : : Places that use this function (or _opt) to decide to elide a copy should
10082 : : probably use make_safe_copy_elision instead. */
10083 : :
10084 : : bool
10085 : 1727079 : unsafe_copy_elision_p (tree target, tree exp)
10086 : : {
10087 : 1727079 : return unsafe_return_slot_p (target) && init_by_return_slot_p (exp);
10088 : : }
10089 : :
10090 : : /* As above, but for optimization allow more cases that are actually safe. */
10091 : :
10092 : : static bool
10093 : 5791688 : unsafe_copy_elision_p_opt (tree target, tree exp)
10094 : : {
10095 : 5791688 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
10096 : : /* It's safe to elide the copy for a class with no tail padding. */
10097 : 5791688 : if (!is_empty_class (type)
10098 : 5791688 : && tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
10099 : : return false;
10100 : 1691083 : return unsafe_copy_elision_p (target, exp);
10101 : : }
10102 : :
10103 : : /* Try to make EXP suitable to be used as the initializer for TARGET,
10104 : : and return whether we were successful. */
10105 : :
10106 : : bool
10107 : 714 : make_safe_copy_elision (tree target, tree exp)
10108 : : {
10109 : 714 : int uns = unsafe_return_slot_p (target);
10110 : 714 : if (!uns)
10111 : : return true;
10112 : 714 : if (init_by_return_slot_p (exp))
10113 : : return false;
10114 : 714 : if (uns == 1)
10115 : : return true;
10116 : 19 : return make_base_init_ok (exp);
10117 : : }
10118 : :
10119 : : /* True IFF the result of the conversion C is a prvalue. */
10120 : :
10121 : : static bool
10122 : 10442094 : conv_is_prvalue (conversion *c)
10123 : : {
10124 : 10442094 : if (c->kind == ck_rvalue)
10125 : : return true;
10126 : 10442094 : if (c->kind == ck_base && c->need_temporary_p)
10127 : : return true;
10128 : 10442094 : if (c->kind == ck_user && !TYPE_REF_P (c->type))
10129 : : return true;
10130 : 10369396 : if (c->kind == ck_identity && c->u.expr
10131 : 10132660 : && TREE_CODE (c->u.expr) == TARGET_EXPR)
10132 : 43 : return true;
10133 : :
10134 : : return false;
10135 : : }
10136 : :
10137 : : /* True iff C is a conversion that binds a reference to a prvalue. */
10138 : :
10139 : : static bool
10140 : 10442951 : conv_binds_ref_to_prvalue (conversion *c)
10141 : : {
10142 : 10442951 : if (c->kind != ck_ref_bind)
10143 : : return false;
10144 : 10442951 : if (c->need_temporary_p)
10145 : : return true;
10146 : :
10147 : 10442094 : return conv_is_prvalue (next_conversion (c));
10148 : : }
10149 : :
10150 : : /* True iff EXPR represents a (subobject of a) temporary. */
10151 : :
10152 : : static bool
10153 : 13758 : expr_represents_temporary_p (tree expr)
10154 : : {
10155 : 13862 : while (handled_component_p (expr))
10156 : 104 : expr = TREE_OPERAND (expr, 0);
10157 : 13758 : return TREE_CODE (expr) == TARGET_EXPR;
10158 : : }
10159 : :
10160 : : /* True iff C is a conversion that binds a reference to a temporary.
10161 : : This is a superset of conv_binds_ref_to_prvalue: here we're also
10162 : : interested in xvalues. */
10163 : :
10164 : : static bool
10165 : 13019 : conv_binds_ref_to_temporary (conversion *c)
10166 : : {
10167 : 13019 : if (conv_binds_ref_to_prvalue (c))
10168 : : return true;
10169 : 12624 : if (c->kind != ck_ref_bind)
10170 : : return false;
10171 : 12624 : c = next_conversion (c);
10172 : : /* This is the case for
10173 : : struct Base {};
10174 : : struct Derived : Base {};
10175 : : const Base& b(Derived{});
10176 : : where we bind 'b' to the Base subobject of a temporary object of type
10177 : : Derived. The subobject is an xvalue; the whole object is a prvalue.
10178 : :
10179 : : The ck_base doesn't have to be present for cases like X{}.m. */
10180 : 12624 : if (c->kind == ck_base)
10181 : 8 : c = next_conversion (c);
10182 : 12569 : if (c->kind == ck_identity && c->u.expr
10183 : 25193 : && expr_represents_temporary_p (c->u.expr))
10184 : : return true;
10185 : : return false;
10186 : : }
10187 : :
10188 : : /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds
10189 : : the reference to a temporary. Return tristate::TS_FALSE if converting
10190 : : EXPR to a reference type TYPE doesn't bind the reference to a temporary. If
10191 : : the conversion is invalid or bad, return tristate::TS_UNKNOWN. DIRECT_INIT_P
10192 : : says whether the conversion should be done in direct- or copy-initialization
10193 : : context. */
10194 : :
10195 : : tristate
10196 : 13498 : ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/)
10197 : : {
10198 : 13498 : gcc_assert (TYPE_REF_P (type));
10199 : :
10200 : 13498 : conversion_obstack_sentinel cos;
10201 : :
10202 : 13498 : const int flags = direct_init_p ? LOOKUP_NORMAL : LOOKUP_IMPLICIT;
10203 : 13498 : conversion *conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10204 : : /*c_cast_p=*/false, flags, tf_none);
10205 : 13498 : tristate ret (tristate::TS_UNKNOWN);
10206 : 13498 : if (conv && !conv->bad_p)
10207 : 13019 : ret = tristate (conv_binds_ref_to_temporary (conv));
10208 : :
10209 : 26996 : return ret;
10210 : 13498 : }
10211 : :
10212 : : /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
10213 : : class type or a pointer to class type. If NO_PTR_DEREF is true and
10214 : : INSTANCE has pointer type, clobber the pointer rather than what it points
10215 : : to. */
10216 : :
10217 : : tree
10218 : 1817112 : build_trivial_dtor_call (tree instance, bool no_ptr_deref)
10219 : : {
10220 : 1817112 : gcc_assert (!is_dummy_object (instance));
10221 : :
10222 : 1817112 : if (!flag_lifetime_dse)
10223 : : {
10224 : 118 : no_clobber:
10225 : 137 : return fold_convert (void_type_node, instance);
10226 : : }
10227 : :
10228 : 1872415 : if (INDIRECT_TYPE_P (TREE_TYPE (instance))
10229 : 1816994 : && (!no_ptr_deref || TYPE_REF_P (TREE_TYPE (instance))))
10230 : : {
10231 : 1731059 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
10232 : 19 : goto no_clobber;
10233 : 1731040 : instance = cp_build_fold_indirect_ref (instance);
10234 : : }
10235 : :
10236 : : /* A trivial destructor should still clobber the object. */
10237 : 1816975 : tree clobber = build_clobber (TREE_TYPE (instance), CLOBBER_OBJECT_END);
10238 : 1816975 : return build2 (MODIFY_EXPR, void_type_node,
10239 : 1816975 : instance, clobber);
10240 : : }
10241 : :
10242 : : /* Return true if in an immediate function context, or an unevaluated operand,
10243 : : or a default argument/member initializer, or a subexpression of an immediate
10244 : : invocation. */
10245 : :
10246 : : bool
10247 : 3122299 : in_immediate_context ()
10248 : : {
10249 : 3122299 : return (cp_unevaluated_operand != 0
10250 : 2662841 : || (current_function_decl != NULL_TREE
10251 : 4533566 : && DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
10252 : : /* DR 2631: default args and DMI aren't immediately evaluated.
10253 : : Return true here so immediate_invocation_p returns false. */
10254 : 2501429 : || current_binding_level->kind == sk_function_parms
10255 : 2501296 : || current_binding_level->kind == sk_template_parms
10256 : 2501275 : || parsing_nsdmi ()
10257 : 5621862 : || in_consteval_if_p);
10258 : : }
10259 : :
10260 : : /* Return true if a call to FN with number of arguments NARGS
10261 : : is an immediate invocation. */
10262 : :
10263 : : bool
10264 : 164947162 : immediate_invocation_p (tree fn)
10265 : : {
10266 : 164947162 : return (TREE_CODE (fn) == FUNCTION_DECL
10267 : 164947162 : && DECL_IMMEDIATE_FUNCTION_P (fn)
10268 : 166221140 : && !in_immediate_context ());
10269 : : }
10270 : :
10271 : : /* Subroutine of the various build_*_call functions. Overload resolution
10272 : : has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
10273 : : ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
10274 : : bitmask of various LOOKUP_* flags which apply to the call itself. */
10275 : :
10276 : : static tree
10277 : 178604343 : build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
10278 : : {
10279 : 178604343 : tree fn = cand->fn;
10280 : 178604343 : const vec<tree, va_gc> *args = cand->args;
10281 : 178604343 : tree first_arg = cand->first_arg;
10282 : 178604343 : conversion **convs = cand->convs;
10283 : 178604343 : tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
10284 : 178604343 : int parmlen;
10285 : 178604343 : tree val;
10286 : 178604343 : int nargs;
10287 : 178604343 : tree *argarray;
10288 : 178604343 : bool already_used = false;
10289 : :
10290 : : /* In a template, there is no need to perform all of the work that
10291 : : is normally done. We are only interested in the type of the call
10292 : : expression, i.e., the return type of the function. Any semantic
10293 : : errors will be deferred until the template is instantiated. */
10294 : 178604343 : if (processing_template_decl)
10295 : : {
10296 : 25148440 : if (undeduced_auto_decl (fn))
10297 : 4112 : mark_used (fn, complain);
10298 : : else
10299 : : /* Otherwise set TREE_USED for the benefit of -Wunused-function.
10300 : : See PR80598. */
10301 : 25144328 : TREE_USED (fn) = 1;
10302 : :
10303 : 25148440 : tree return_type = TREE_TYPE (TREE_TYPE (fn));
10304 : 25148440 : tree callee;
10305 : 25148440 : if (first_arg == NULL_TREE)
10306 : : {
10307 : 19446588 : callee = build_addr_func (fn, complain);
10308 : 19446588 : if (callee == error_mark_node)
10309 : : return error_mark_node;
10310 : : }
10311 : : else
10312 : : {
10313 : 5701852 : callee = build_baselink (cand->conversion_path, cand->access_path,
10314 : : fn, NULL_TREE);
10315 : 5701852 : callee = build_min (COMPONENT_REF, TREE_TYPE (fn),
10316 : : first_arg, callee, NULL_TREE);
10317 : : }
10318 : :
10319 : 25148440 : tree expr = build_call_vec (return_type, callee, args);
10320 : 25148440 : SET_EXPR_LOCATION (expr, input_location);
10321 : 25148440 : if (TREE_THIS_VOLATILE (fn) && cfun)
10322 : 2202966 : current_function_returns_abnormally = 1;
10323 : 25148440 : if (TREE_DEPRECATED (fn)
10324 : 25148440 : && warning_suppressed_at (input_location,
10325 : : OPT_Wdeprecated_declarations))
10326 : : /* Make the expr consistent with the location. */
10327 : 20228 : TREE_NO_WARNING (expr) = true;
10328 : 25148440 : if (immediate_invocation_p (fn))
10329 : : {
10330 : 50909 : tree obj_arg = NULL_TREE, exprimm = expr;
10331 : 101818 : if (DECL_CONSTRUCTOR_P (fn))
10332 : 0 : obj_arg = first_arg;
10333 : 0 : if (obj_arg
10334 : 0 : && is_dummy_object (obj_arg)
10335 : 0 : && !type_dependent_expression_p (obj_arg))
10336 : : {
10337 : 0 : exprimm = build_cplus_new (DECL_CONTEXT (fn), expr, complain);
10338 : 0 : obj_arg = NULL_TREE;
10339 : : }
10340 : : /* Look through *(const T *)&obj. */
10341 : 50909 : else if (obj_arg && INDIRECT_REF_P (obj_arg))
10342 : : {
10343 : 0 : tree addr = TREE_OPERAND (obj_arg, 0);
10344 : 0 : STRIP_NOPS (addr);
10345 : 0 : if (TREE_CODE (addr) == ADDR_EXPR)
10346 : : {
10347 : 0 : tree typeo = TREE_TYPE (obj_arg);
10348 : 0 : tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
10349 : 0 : if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
10350 : 0 : obj_arg = TREE_OPERAND (addr, 0);
10351 : : }
10352 : : }
10353 : 50909 : fold_non_dependent_expr (exprimm, complain,
10354 : : /*manifestly_const_eval=*/true,
10355 : : obj_arg);
10356 : : }
10357 : 25148440 : return convert_from_reference (expr);
10358 : : }
10359 : :
10360 : : /* Give any warnings we noticed during overload resolution. */
10361 : 153455903 : if (cand->warnings && (complain & tf_warning))
10362 : : {
10363 : : struct candidate_warning *w;
10364 : 94 : for (w = cand->warnings; w; w = w->next)
10365 : 47 : joust (cand, w->loser, 1, complain);
10366 : : }
10367 : :
10368 : : /* Core issue 2327: P0135 doesn't say how to handle the case where the
10369 : : argument to the copy constructor ends up being a prvalue after
10370 : : conversion. Let's do the normal processing, but pretend we aren't
10371 : : actually using the copy constructor. */
10372 : 153455903 : bool force_elide = false;
10373 : 153455903 : if (cxx_dialect >= cxx17
10374 : 151577678 : && cand->num_convs == 1
10375 : 87367982 : && DECL_COMPLETE_CONSTRUCTOR_P (fn)
10376 : 26675100 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10377 : 13363414 : || DECL_MOVE_CONSTRUCTOR_P (fn))
10378 : 10460118 : && !unsafe_return_slot_p (first_arg)
10379 : 163885771 : && conv_binds_ref_to_prvalue (convs[0]))
10380 : : {
10381 : 73200 : force_elide = true;
10382 : 73200 : goto not_really_used;
10383 : : }
10384 : :
10385 : : /* OK, we're actually calling this inherited constructor; set its deletedness
10386 : : appropriately. We can get away with doing this here because calling is
10387 : : the only way to refer to a constructor. */
10388 : 306765406 : if (DECL_INHERITED_CTOR (fn)
10389 : 21475507 : && !deduce_inheriting_ctor (fn))
10390 : : {
10391 : 2 : if (complain & tf_error)
10392 : 2 : mark_used (fn);
10393 : 2 : return error_mark_node;
10394 : : }
10395 : :
10396 : : /* Make =delete work with SFINAE. */
10397 : 153382701 : if (DECL_DELETED_FN (fn))
10398 : : {
10399 : 766934 : if (complain & tf_error)
10400 : : {
10401 : 2280 : mark_used (fn);
10402 : 2280 : if (cand->next)
10403 : : {
10404 : 2095 : if (flag_diagnostics_all_candidates)
10405 : 9 : print_z_candidates (input_location, cand, /*only_viable_p=*/false);
10406 : : else
10407 : 2086 : inform (input_location,
10408 : : "use %<-fdiagnostics-all-candidates%> to display "
10409 : : "considered candidates");
10410 : : }
10411 : : }
10412 : 766934 : return error_mark_node;
10413 : : }
10414 : :
10415 : 152615767 : if (DECL_FUNCTION_MEMBER_P (fn))
10416 : : {
10417 : 85039205 : tree access_fn;
10418 : : /* If FN is a template function, two cases must be considered.
10419 : : For example:
10420 : :
10421 : : struct A {
10422 : : protected:
10423 : : template <class T> void f();
10424 : : };
10425 : : template <class T> struct B {
10426 : : protected:
10427 : : void g();
10428 : : };
10429 : : struct C : A, B<int> {
10430 : : using A::f; // #1
10431 : : using B<int>::g; // #2
10432 : : };
10433 : :
10434 : : In case #1 where `A::f' is a member template, DECL_ACCESS is
10435 : : recorded in the primary template but not in its specialization.
10436 : : We check access of FN using its primary template.
10437 : :
10438 : : In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
10439 : : because it is a member of class template B, DECL_ACCESS is
10440 : : recorded in the specialization `B<int>::g'. We cannot use its
10441 : : primary template because `B<T>::g' and `B<int>::g' may have
10442 : : different access. */
10443 : 85039205 : if (DECL_TEMPLATE_INFO (fn)
10444 : 85039205 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
10445 : 8762116 : access_fn = DECL_TI_TEMPLATE (fn);
10446 : : else
10447 : : access_fn = fn;
10448 : 85039205 : if (!perform_or_defer_access_check (cand->access_path, access_fn,
10449 : : fn, complain))
10450 : 21483 : return error_mark_node;
10451 : : }
10452 : :
10453 : : /* If we're checking for implicit delete, don't bother with argument
10454 : : conversions. */
10455 : 152594284 : if (flags & LOOKUP_SPECULATIVE)
10456 : : {
10457 : 20997648 : if (cand->viable == 1)
10458 : : return fn;
10459 : 117 : else if (!(complain & tf_error))
10460 : : /* Reject bad conversions now. */
10461 : 102 : return error_mark_node;
10462 : : /* else continue to get conversion error. */
10463 : : }
10464 : :
10465 : 131596636 : not_really_used:
10466 : :
10467 : : /* N3276 magic doesn't apply to nested calls. */
10468 : 131669851 : tsubst_flags_t decltype_flag = (complain & tf_decltype);
10469 : 131669851 : complain &= ~tf_decltype;
10470 : : /* No-Cleanup doesn't apply to nested calls either. */
10471 : 131669851 : tsubst_flags_t no_cleanup_complain = complain;
10472 : 131669851 : complain &= ~tf_no_cleanup;
10473 : :
10474 : : /* Find maximum size of vector to hold converted arguments. */
10475 : 131669851 : parmlen = list_length (parm);
10476 : 247069023 : nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
10477 : 131669851 : if (parmlen > nargs)
10478 : : nargs = parmlen;
10479 : 131669851 : argarray = XALLOCAVEC (tree, nargs);
10480 : :
10481 : 263339699 : in_consteval_if_p_temp_override icip;
10482 : : /* If the call is immediate function invocation, make sure
10483 : : taking address of immediate functions is allowed in its arguments. */
10484 : 131669851 : if (immediate_invocation_p (STRIP_TEMPLATE (fn)))
10485 : 666295 : in_consteval_if_p = true;
10486 : :
10487 : 131669851 : int argarray_size = 0;
10488 : 131669851 : unsigned int arg_index = 0;
10489 : 131669851 : int conv_index = 0;
10490 : 131669851 : int param_index = 0;
10491 : 131669851 : tree parmd = DECL_ARGUMENTS (fn);
10492 : :
10493 : 183630390 : auto consume_object_arg = [&arg_index, &first_arg, args]()
10494 : : {
10495 : 51960539 : if (!first_arg)
10496 : 0 : return (*args)[arg_index++];
10497 : 51960539 : tree object_arg = first_arg;
10498 : 51960539 : first_arg = NULL_TREE;
10499 : 51960539 : return object_arg;
10500 : 131669851 : };
10501 : :
10502 : : /* The implicit parameters to a constructor are not considered by overload
10503 : : resolution, and must be of the proper type. */
10504 : 131669851 : if (DECL_CONSTRUCTOR_P (fn))
10505 : : {
10506 : 14688322 : tree object_arg = consume_object_arg ();
10507 : 14688322 : argarray[argarray_size++] = build_this (object_arg);
10508 : 14688322 : parm = TREE_CHAIN (parm);
10509 : 14688322 : if (parmd)
10510 : 14688322 : parmd = DECL_CHAIN (parmd);
10511 : : /* We should never try to call the abstract constructor. */
10512 : 14688322 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
10513 : :
10514 : 14688322 : if (DECL_HAS_VTT_PARM_P (fn))
10515 : : {
10516 : 17943 : argarray[argarray_size++] = (*args)[arg_index];
10517 : 17943 : ++arg_index;
10518 : 17943 : parm = TREE_CHAIN (parm);
10519 : 17943 : if (parmd)
10520 : 17943 : parmd = DECL_CHAIN (parmd);
10521 : : }
10522 : : }
10523 : : /* Bypass access control for 'this' parameter. */
10524 : 116981529 : else if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
10525 : : {
10526 : 37266476 : tree arg = build_this (consume_object_arg ());
10527 : 37266476 : tree argtype = TREE_TYPE (arg);
10528 : :
10529 : 37266476 : if (arg == error_mark_node)
10530 : : return error_mark_node;
10531 : 37266473 : if (convs[conv_index++]->bad_p)
10532 : : {
10533 : 1115 : if (complain & tf_error)
10534 : : {
10535 : 104 : auto_diagnostic_group d;
10536 : 104 : if (permerror (input_location, "passing %qT as %<this%> "
10537 : : "argument discards qualifiers",
10538 : 104 : TREE_TYPE (argtype)))
10539 : 101 : inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
10540 : 104 : }
10541 : : else
10542 : : return error_mark_node;
10543 : : }
10544 : :
10545 : : /* The class where FN is defined. */
10546 : 37265462 : tree ctx = DECL_CONTEXT (fn);
10547 : :
10548 : : /* See if the function member or the whole class type is declared
10549 : : final and the call can be devirtualized. */
10550 : 37265462 : if (DECL_FINAL_P (fn) || CLASSTYPE_FINAL (ctx))
10551 : 138204 : flags |= LOOKUP_NONVIRTUAL;
10552 : :
10553 : : /* [class.mfct.non-static]: If a non-static member function of a class
10554 : : X is called for an object that is not of type X, or of a type
10555 : : derived from X, the behavior is undefined.
10556 : :
10557 : : So we can assume that anything passed as 'this' is non-null, and
10558 : : optimize accordingly. */
10559 : : /* Check that the base class is accessible. */
10560 : 37265462 : if (!accessible_base_p (TREE_TYPE (argtype),
10561 : 37265462 : BINFO_TYPE (cand->conversion_path), true))
10562 : : {
10563 : 33 : if (complain & tf_error)
10564 : 30 : error ("%qT is not an accessible base of %qT",
10565 : 30 : BINFO_TYPE (cand->conversion_path),
10566 : 30 : TREE_TYPE (argtype));
10567 : : else
10568 : 3 : return error_mark_node;
10569 : : }
10570 : : /* If fn was found by a using declaration, the conversion path
10571 : : will be to the derived class, not the base declaring fn. We
10572 : : must convert to the base. */
10573 : 37265459 : tree base_binfo = cand->conversion_path;
10574 : 37265459 : if (BINFO_TYPE (base_binfo) != ctx)
10575 : : {
10576 : 93518 : base_binfo = lookup_base (base_binfo, ctx, ba_unique, NULL, complain);
10577 : 93518 : if (base_binfo == error_mark_node)
10578 : : return error_mark_node;
10579 : : }
10580 : :
10581 : : /* If we know the dynamic type of the object, look up the final overrider
10582 : : in the BINFO. */
10583 : 38165776 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
10584 : 37809876 : && resolves_to_fixed_type_p (arg))
10585 : : {
10586 : 953 : tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
10587 : :
10588 : : /* And unwind base_binfo to match. If we don't find the type we're
10589 : : looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond
10590 : : inheritance; for now do a normal virtual call in that case. */
10591 : 953 : tree octx = DECL_CONTEXT (ov);
10592 : 953 : tree obinfo = base_binfo;
10593 : 1969 : while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx))
10594 : 33 : obinfo = BINFO_INHERITANCE_CHAIN (obinfo);
10595 : 953 : if (obinfo)
10596 : : {
10597 : 950 : fn = ov;
10598 : 950 : base_binfo = obinfo;
10599 : 950 : flags |= LOOKUP_NONVIRTUAL;
10600 : : }
10601 : : }
10602 : :
10603 : 37265453 : tree converted_arg = build_base_path (PLUS_EXPR, arg,
10604 : : base_binfo, 1, complain);
10605 : :
10606 : 37265453 : argarray[argarray_size++] = converted_arg;
10607 : 37265453 : parm = TREE_CHAIN (parm);
10608 : 37265453 : if (parmd)
10609 : 37265453 : parmd = DECL_CHAIN (parmd);
10610 : : }
10611 : :
10612 : 236854287 : auto handle_arg = [fn, flags](tree type,
10613 : : tree arg,
10614 : : int const param_index,
10615 : : conversion *conv,
10616 : : tsubst_flags_t const arg_complain)
10617 : : {
10618 : : /* Set user_conv_p on the argument conversions, so rvalue/base handling
10619 : : knows not to allow any more UDCs. This needs to happen after we
10620 : : process cand->warnings. */
10621 : 105185459 : if (flags & LOOKUP_NO_CONVERSION)
10622 : 2288923 : conv->user_conv_p = true;
10623 : :
10624 : 105185459 : if (arg_complain & tf_warning)
10625 : 83004648 : maybe_warn_pessimizing_move (arg, type, /*return_p=*/false);
10626 : :
10627 : 105185459 : tree val = convert_like_with_context (conv, arg, fn,
10628 : : param_index, arg_complain);
10629 : 105185459 : val = convert_for_arg_passing (type, val, arg_complain);
10630 : 105185459 : return val;
10631 : 131668828 : };
10632 : :
10633 : 237970023 : auto handle_indeterminate_arg = [](tree parmd, tree val)
10634 : : {
10635 : 106301195 : if (parmd
10636 : 106301195 : && lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (parmd)))
10637 : : {
10638 : 48 : STRIP_NOPS (val);
10639 : 48 : if (TREE_CODE (val) == ADDR_EXPR
10640 : 48 : && TREE_CODE (TREE_OPERAND (val, 0)) == TARGET_EXPR)
10641 : : {
10642 : 48 : val = TARGET_EXPR_SLOT (TREE_OPERAND (val, 0));
10643 : 48 : if (auto_var_p (val) && DECL_ARTIFICIAL (val))
10644 : : {
10645 : 48 : tree id = get_identifier ("indeterminate");
10646 : 48 : DECL_ATTRIBUTES (val)
10647 : 96 : = tree_cons (build_tree_list (NULL_TREE, id), NULL_TREE,
10648 : 48 : DECL_ATTRIBUTES (val));
10649 : : }
10650 : : }
10651 : : }
10652 : 106301195 : };
10653 : :
10654 : 131668828 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
10655 : : {
10656 : 5741 : gcc_assert (cand->num_convs > 0);
10657 : 5741 : tree object_arg = consume_object_arg ();
10658 : 5741 : val = handle_arg (TREE_VALUE (parm),
10659 : : object_arg,
10660 : : param_index++,
10661 : 5741 : convs[conv_index++],
10662 : : complain);
10663 : :
10664 : 5741 : if (val == error_mark_node)
10665 : : return error_mark_node;
10666 : : else
10667 : : {
10668 : 5629 : argarray[argarray_size++] = val;
10669 : 5629 : handle_indeterminate_arg (parmd, val);
10670 : : }
10671 : 5629 : parm = TREE_CHAIN (parm);
10672 : 5629 : if (parmd)
10673 : 5629 : parmd = DECL_CHAIN (parmd);
10674 : : }
10675 : :
10676 : 131668716 : gcc_assert (first_arg == NULL_TREE);
10677 : 236847301 : for (; arg_index < vec_safe_length (args) && parm;
10678 : 105178585 : parm = TREE_CHAIN (parm), ++arg_index, ++param_index, ++conv_index)
10679 : : {
10680 : 105179718 : tree current_arg = (*args)[arg_index];
10681 : :
10682 : : /* If the argument is NULL and used to (implicitly) instantiate a
10683 : : template function (and bind one of the template arguments to
10684 : : the type of 'long int'), we don't want to warn about passing NULL
10685 : : to non-pointer argument.
10686 : : For example, if we have this template function:
10687 : :
10688 : : template<typename T> void func(T x) {}
10689 : :
10690 : : we want to warn (when -Wconversion is enabled) in this case:
10691 : :
10692 : : void foo() {
10693 : : func<int>(NULL);
10694 : : }
10695 : :
10696 : : but not in this case:
10697 : :
10698 : : void foo() {
10699 : : func(NULL);
10700 : : }
10701 : : */
10702 : 105179718 : bool const conversion_warning = !(null_node_p (current_arg)
10703 : 47358 : && DECL_TEMPLATE_INFO (fn)
10704 : 60 : && cand->template_decl
10705 : 30 : && !cand->explicit_targs);
10706 : :
10707 : 15 : tsubst_flags_t const arg_complain
10708 : : = conversion_warning ? complain : complain & ~tf_warning;
10709 : :
10710 : 105179718 : val = handle_arg (TREE_VALUE (parm),
10711 : : current_arg,
10712 : : param_index,
10713 : 105179718 : convs[conv_index],
10714 : : arg_complain);
10715 : :
10716 : 105179718 : if (val == error_mark_node)
10717 : : return error_mark_node;
10718 : : else
10719 : : {
10720 : 105178585 : argarray[argarray_size++] = val;
10721 : 105178585 : handle_indeterminate_arg (parmd, val);
10722 : : }
10723 : 105178585 : if (parmd)
10724 : 94946773 : parmd = DECL_CHAIN (parmd);
10725 : : }
10726 : :
10727 : : /* Default arguments */
10728 : 132784564 : for (; parm && parm != void_list_node;
10729 : 1116981 : parm = TREE_CHAIN (parm), param_index++)
10730 : : {
10731 : 1117094 : if (TREE_VALUE (parm) == error_mark_node)
10732 : : return error_mark_node;
10733 : 2234164 : val = convert_default_arg (TREE_VALUE (parm),
10734 : 1117082 : TREE_PURPOSE (parm),
10735 : : fn, param_index,
10736 : : complain);
10737 : 1117082 : if (val == error_mark_node)
10738 : : return error_mark_node;
10739 : 1116981 : argarray[argarray_size++] = val;
10740 : 1116981 : handle_indeterminate_arg (parmd, val);
10741 : 1116981 : if (parmd)
10742 : 1116981 : parmd = DECL_CHAIN (parmd);
10743 : : }
10744 : :
10745 : : /* Ellipsis */
10746 : 131667470 : int magic = magic_varargs_p (fn);
10747 : 134334738 : for (; arg_index < vec_safe_length (args); ++arg_index)
10748 : : {
10749 : 2667289 : tree a = (*args)[arg_index];
10750 : 2667289 : if ((magic == 3 && arg_index == 2) || (magic == 4 && arg_index == 0))
10751 : : {
10752 : : /* Do no conversions for certain magic varargs. */
10753 : 77853 : a = mark_type_use (a);
10754 : 77853 : if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
10755 : 0 : return error_mark_node;
10756 : : }
10757 : 2589436 : else if (magic != 0)
10758 : : {
10759 : : /* Don't truncate excess precision to the semantic type. */
10760 : 1102861 : if (magic == 1 && TREE_CODE (a) == EXCESS_PRECISION_EXPR)
10761 : 84 : a = TREE_OPERAND (a, 0);
10762 : : /* For other magic varargs only do decay_conversion. */
10763 : 1102861 : a = decay_conversion (a, complain);
10764 : : }
10765 : 1486575 : else if (DECL_CONSTRUCTOR_P (fn)
10766 : 289 : && vec_safe_length (args) == 1
10767 : 1486698 : && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
10768 : 123 : TREE_TYPE (a)))
10769 : : {
10770 : : /* Avoid infinite recursion trying to call A(...). */
10771 : 3 : if (complain & tf_error)
10772 : : /* Try to call the actual copy constructor for a good error. */
10773 : 3 : call_copy_ctor (a, complain);
10774 : 3 : return error_mark_node;
10775 : : }
10776 : : else
10777 : 1486572 : a = convert_arg_to_ellipsis (a, complain);
10778 : 2667286 : if (a == error_mark_node)
10779 : : return error_mark_node;
10780 : 2667268 : argarray[argarray_size++] = a;
10781 : : }
10782 : :
10783 : 131667449 : gcc_assert (argarray_size <= nargs);
10784 : 131667449 : nargs = argarray_size;
10785 : 131667449 : icip.reset ();
10786 : :
10787 : : /* Avoid performing argument transformation if warnings are disabled.
10788 : : When tf_warning is set and at least one of the warnings is active
10789 : : the check_function_arguments function might warn about something. */
10790 : :
10791 : 131667449 : bool warned_p = false;
10792 : 131667449 : if ((complain & tf_warning)
10793 : 91702257 : && (warn_nonnull
10794 : 89815333 : || warn_format
10795 : 89815330 : || warn_suggest_attribute_format
10796 : 89815234 : || warn_restrict))
10797 : : {
10798 : 1889270 : tree *fargs = (!nargs ? argarray
10799 : 1563485 : : (tree *) alloca (nargs * sizeof (tree)));
10800 : 5787116 : for (int j = 0; j < nargs; j++)
10801 : : {
10802 : : /* For -Wformat undo the implicit passing by hidden reference
10803 : : done by convert_arg_to_ellipsis. */
10804 : 3897846 : if (TREE_CODE (argarray[j]) == ADDR_EXPR
10805 : 3897846 : && TYPE_REF_P (TREE_TYPE (argarray[j])))
10806 : 1604 : fargs[j] = TREE_OPERAND (argarray[j], 0);
10807 : : else
10808 : 3896242 : fargs[j] = argarray[j];
10809 : : }
10810 : :
10811 : 1889270 : warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
10812 : : nargs, fargs, NULL,
10813 : : cp_comp_parm_types);
10814 : : }
10815 : :
10816 : 263334898 : if (DECL_INHERITED_CTOR (fn))
10817 : : {
10818 : : /* Check for passing ellipsis arguments to an inherited constructor. We
10819 : : could handle this by open-coding the inherited constructor rather than
10820 : : defining it, but let's not bother now. */
10821 : 52606 : if (!cp_unevaluated_operand
10822 : 52348 : && cand->num_convs
10823 : 52348 : && cand->convs[cand->num_convs-1]->ellipsis_p)
10824 : : {
10825 : 15 : if (complain & tf_error)
10826 : : {
10827 : 15 : sorry ("passing arguments to ellipsis of inherited constructor "
10828 : : "%qD", cand->fn);
10829 : 15 : inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
10830 : : }
10831 : 15 : return error_mark_node;
10832 : : }
10833 : :
10834 : : /* A base constructor inheriting from a virtual base doesn't get the
10835 : : inherited arguments, just this and __vtt. */
10836 : 52591 : if (ctor_omit_inherited_parms (fn))
10837 : 131667434 : nargs = 2;
10838 : : }
10839 : :
10840 : : /* Avoid actually calling copy constructors and copy assignment operators,
10841 : : if possible. */
10842 : :
10843 : 131667434 : if (!force_elide
10844 : 131594266 : && (!flag_elide_constructors
10845 : : /* It's unsafe to elide the operation when handling
10846 : : a noexcept-expression, it may evaluate to the wrong
10847 : : value (c++/53025, c++/96090). */
10848 : 131594062 : || cp_noexcept_operand != 0))
10849 : : /* Do things the hard way. */;
10850 : 129923003 : else if (cand->num_convs == 1
10851 : 199490618 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10852 : 131474460 : || DECL_MOVE_CONSTRUCTOR_P (fn)))
10853 : : {
10854 : 5791688 : tree targ;
10855 : 5791688 : tree arg = argarray[num_artificial_parms_for (fn)];
10856 : 5791688 : tree fa = argarray[0];
10857 : 5791688 : bool trivial = trivial_fn_p (fn);
10858 : :
10859 : : /* Pull out the real argument, disregarding const-correctness. */
10860 : 5791688 : targ = arg;
10861 : : /* Strip the reference binding for the constructor parameter. */
10862 : 0 : if (CONVERT_EXPR_P (targ)
10863 : 5791688 : && TYPE_REF_P (TREE_TYPE (targ)))
10864 : 5791688 : targ = TREE_OPERAND (targ, 0);
10865 : : /* But don't strip any other reference bindings; binding a temporary to a
10866 : : reference prevents copy elision. */
10867 : 9554389 : while ((CONVERT_EXPR_P (targ)
10868 : 8271844 : && !TYPE_REF_P (TREE_TYPE (targ)))
10869 : 20385908 : || TREE_CODE (targ) == NON_LVALUE_EXPR)
10870 : 7492475 : targ = TREE_OPERAND (targ, 0);
10871 : 5791688 : if (TREE_CODE (targ) == ADDR_EXPR)
10872 : : {
10873 : 1881574 : targ = TREE_OPERAND (targ, 0);
10874 : 1881574 : if (!same_type_ignoring_top_level_qualifiers_p
10875 : 1881574 : (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
10876 : : targ = NULL_TREE;
10877 : : }
10878 : : else
10879 : : targ = NULL_TREE;
10880 : :
10881 : 1881116 : if (targ)
10882 : : arg = targ;
10883 : : else
10884 : 3910572 : arg = cp_build_fold_indirect_ref (arg);
10885 : :
10886 : : /* In C++17 we shouldn't be copying a TARGET_EXPR except into a
10887 : : potentially-overlapping subobject. */
10888 : 5791688 : if (CHECKING_P && cxx_dialect >= cxx17)
10889 : 5719192 : gcc_assert (TREE_CODE (arg) != TARGET_EXPR
10890 : : || force_elide
10891 : : /* It's from binding the ref parm to a packed field. */
10892 : : || convs[0]->need_temporary_p
10893 : : || seen_error ()
10894 : : /* See unsafe_copy_elision_p. */
10895 : : || unsafe_return_slot_p (fa));
10896 : :
10897 : 5791688 : bool unsafe = unsafe_copy_elision_p_opt (fa, arg);
10898 : 5791688 : bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe);
10899 : :
10900 : : /* [class.copy]: the copy constructor is implicitly defined even if the
10901 : : implementation elided its use. But don't warn about deprecation when
10902 : : eliding a temporary, as then no copy is actually performed. */
10903 : 5791688 : warning_sentinel s (warn_deprecated_copy, eliding_temp);
10904 : 5791688 : if (force_elide)
10905 : : /* The language says this isn't called. */;
10906 : 5718520 : else if (!trivial)
10907 : : {
10908 : 1176927 : if (!mark_used (fn, complain) && !(complain & tf_error))
10909 : 0 : return error_mark_node;
10910 : : already_used = true;
10911 : : }
10912 : : else
10913 : 4541593 : cp_handle_deprecated_or_unavailable (fn, complain);
10914 : :
10915 : 110381 : if (eliding_temp && DECL_BASE_CONSTRUCTOR_P (fn)
10916 : 5791869 : && !make_base_init_ok (arg))
10917 : : unsafe = true;
10918 : :
10919 : : /* If we're creating a temp and we already have one, don't create a
10920 : : new one. If we're not creating a temp but we get one, use
10921 : : INIT_EXPR to collapse the temp into our target. Otherwise, if the
10922 : : ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
10923 : : temp or an INIT_EXPR otherwise. */
10924 : 5791688 : if (is_dummy_object (fa))
10925 : : {
10926 : 4313237 : if (TREE_CODE (arg) == TARGET_EXPR)
10927 : : return arg;
10928 : 4212907 : else if (trivial)
10929 : 3680075 : return force_target_expr (DECL_CONTEXT (fn), arg, complain);
10930 : : }
10931 : 1478451 : else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
10932 : 840440 : && !unsafe)
10933 : : {
10934 : 840352 : tree to = cp_build_fold_indirect_ref (fa);
10935 : 840352 : val = cp_build_init_expr (to, arg);
10936 : 840352 : return val;
10937 : : }
10938 : 5791688 : }
10939 : 124131315 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
10940 : 2429743 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
10941 : 126062133 : && trivial_fn_p (fn))
10942 : : {
10943 : 1391264 : tree to = cp_build_fold_indirect_ref (argarray[0]);
10944 : 1391264 : tree type = TREE_TYPE (to);
10945 : 1391264 : tree as_base = CLASSTYPE_AS_BASE (type);
10946 : 1391264 : tree arg = argarray[1];
10947 : 1391264 : location_t loc = cp_expr_loc_or_input_loc (arg);
10948 : :
10949 : 1391264 : if (is_really_empty_class (type, /*ignore_vptr*/true))
10950 : : {
10951 : : /* Avoid copying empty classes, but ensure op= returns an lvalue even
10952 : : if the object argument isn't one. */
10953 : 189647 : to = force_lvalue (to, complain);
10954 : 189647 : val = build2 (COMPOUND_EXPR, type, arg, to);
10955 : 189647 : suppress_warning (val, OPT_Wunused);
10956 : : }
10957 : 1201617 : else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
10958 : : {
10959 : 1074596 : if (is_std_init_list (type)
10960 : 1074596 : && conv_binds_ref_to_prvalue (convs[1]))
10961 : 3 : warning_at (loc, OPT_Winit_list_lifetime,
10962 : : "assignment from temporary %<initializer_list%> does "
10963 : : "not extend the lifetime of the underlying array");
10964 : 1074596 : arg = cp_build_fold_indirect_ref (arg);
10965 : 1074596 : val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
10966 : : }
10967 : : else
10968 : : {
10969 : : /* We must only copy the non-tail padding parts. */
10970 : 127021 : tree arg0, arg2, t;
10971 : 127021 : tree array_type, alias_set;
10972 : :
10973 : 127021 : arg2 = TYPE_SIZE_UNIT (as_base);
10974 : : /* Ensure op= returns an lvalue even if the object argument isn't
10975 : : one. */
10976 : 127021 : to = force_lvalue (to, complain);
10977 : 127021 : to = cp_stabilize_reference (to);
10978 : 127021 : arg0 = cp_build_addr_expr (to, complain);
10979 : :
10980 : 127021 : array_type = build_array_type (unsigned_char_type_node,
10981 : : build_index_type
10982 : : (size_binop (MINUS_EXPR,
10983 : : arg2, size_int (1))));
10984 : 127021 : alias_set = build_int_cst (build_pointer_type (type), 0);
10985 : 127021 : t = build2 (MODIFY_EXPR, void_type_node,
10986 : : build2 (MEM_REF, array_type, arg0, alias_set),
10987 : : build2 (MEM_REF, array_type, arg, alias_set));
10988 : 127021 : val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
10989 : 127021 : suppress_warning (val, OPT_Wunused);
10990 : : }
10991 : :
10992 : 1391264 : cp_handle_deprecated_or_unavailable (fn, complain);
10993 : :
10994 : 1391264 : return val;
10995 : : }
10996 : 122740051 : else if (trivial_fn_p (fn))
10997 : : {
10998 : 4607292 : if (DECL_DESTRUCTOR_P (fn))
10999 : 1722285 : return build_trivial_dtor_call (argarray[0]);
11000 : 581361 : else if (default_ctor_p (fn))
11001 : : {
11002 : 581361 : if (is_dummy_object (argarray[0]))
11003 : 192272 : return force_target_expr (DECL_CONTEXT (fn), void_node,
11004 : 192272 : no_cleanup_complain);
11005 : : else
11006 : 389089 : return cp_build_fold_indirect_ref (argarray[0]);
11007 : : }
11008 : : }
11009 : :
11010 : 123351767 : gcc_assert (!force_elide);
11011 : :
11012 : 123351767 : if (!already_used
11013 : 123351767 : && !mark_used (fn, complain))
11014 : 760 : return error_mark_node;
11015 : :
11016 : : /* Warn if the built-in writes to an object of a non-trivial type. */
11017 : 123351004 : if (warn_class_memaccess
11018 : 1908953 : && vec_safe_length (args) >= 2
11019 : 124288914 : && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
11020 : 66078 : maybe_warn_class_memaccess (input_location, fn, args);
11021 : :
11022 : 123351004 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
11023 : : {
11024 : 543473 : tree t;
11025 : 543473 : tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
11026 : 543473 : DECL_CONTEXT (fn),
11027 : : ba_any, NULL, complain);
11028 : 543473 : gcc_assert (binfo && binfo != error_mark_node);
11029 : :
11030 : 543473 : argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
11031 : : complain);
11032 : 543473 : if (TREE_SIDE_EFFECTS (argarray[0]))
11033 : 62715 : argarray[0] = save_expr (argarray[0]);
11034 : 543473 : t = build_pointer_type (TREE_TYPE (fn));
11035 : 543473 : fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
11036 : 543473 : TREE_TYPE (fn) = t;
11037 : : }
11038 : : else
11039 : : {
11040 : : /* If FN is marked deprecated or unavailable, then we've already
11041 : : issued a diagnostic from mark_used above, so avoid redundantly
11042 : : issuing another one from build_addr_func. */
11043 : 122807531 : auto w = make_temp_override (deprecated_state,
11044 : 122807531 : UNAVAILABLE_DEPRECATED_SUPPRESS);
11045 : :
11046 : 122807531 : fn = build_addr_func (fn, complain);
11047 : 122807531 : if (fn == error_mark_node)
11048 : 0 : return error_mark_node;
11049 : :
11050 : : /* We're actually invoking the function. (Immediate functions get an
11051 : : & when invoking it even though the user didn't use &.) */
11052 : 122807531 : ADDR_EXPR_DENOTES_CALL_P (fn) = true;
11053 : 122807531 : }
11054 : :
11055 : 123351004 : tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
11056 : 123351004 : if (call == error_mark_node)
11057 : : return call;
11058 : 123350155 : if (cand->flags & LOOKUP_LIST_INIT_CTOR)
11059 : : {
11060 : 1160037 : tree c = extract_call_expr (call);
11061 : : /* build_new_op will clear this when appropriate. */
11062 : 1160037 : CALL_EXPR_ORDERED_ARGS (c) = true;
11063 : : }
11064 : 123350155 : if (warned_p)
11065 : : {
11066 : 265 : tree c = extract_call_expr (call);
11067 : 265 : if (TREE_CODE (c) == CALL_EXPR)
11068 : 265 : suppress_warning (c /* Suppress all warnings. */);
11069 : : }
11070 : 123349890 : else if (TREE_DEPRECATED (fn)
11071 : 123349890 : && warning_suppressed_at (input_location,
11072 : : OPT_Wdeprecated_declarations))
11073 : : {
11074 : 0 : tree c = extract_call_expr (call);
11075 : 0 : if (TREE_CODE (c) == CALL_EXPR)
11076 : 0 : TREE_NO_WARNING (c) = true;
11077 : : }
11078 : :
11079 : : return call;
11080 : : }
11081 : :
11082 : : namespace
11083 : : {
11084 : :
11085 : : /* Return the DECL of the first non-static subobject of class TYPE
11086 : : that satisfies the predicate PRED or null if none can be found. */
11087 : :
11088 : : template <class Predicate>
11089 : : tree
11090 : 3545 : first_non_static_field (tree type, Predicate pred)
11091 : : {
11092 : 3545 : if (!type || !CLASS_TYPE_P (type))
11093 : : return NULL_TREE;
11094 : :
11095 : 70903 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11096 : : {
11097 : 67952 : if (TREE_CODE (field) != FIELD_DECL)
11098 : 43198 : continue;
11099 : 24754 : if (TREE_STATIC (field))
11100 : 0 : continue;
11101 : 24754 : if (pred (field))
11102 : : return field;
11103 : : }
11104 : :
11105 : 2951 : int i = 0;
11106 : :
11107 : 3071 : for (tree base_binfo, binfo = TYPE_BINFO (type);
11108 : 3071 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11109 : : {
11110 : 126 : tree base = TREE_TYPE (base_binfo);
11111 : 126 : if (pred (base))
11112 : : return base;
11113 : 120 : if (tree field = first_non_static_field (base, pred))
11114 : : return field;
11115 : : }
11116 : :
11117 : : return NULL_TREE;
11118 : : }
11119 : :
11120 : : struct NonPublicField
11121 : : {
11122 : 24004 : bool operator() (const_tree t) const
11123 : : {
11124 : 24004 : return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t));
11125 : : }
11126 : : };
11127 : :
11128 : : /* Return the DECL of the first non-public subobject of class TYPE
11129 : : or null if none can be found. */
11130 : :
11131 : : static inline tree
11132 : 3251 : first_non_public_field (tree type)
11133 : : {
11134 : 3251 : return first_non_static_field (type, NonPublicField ());
11135 : : }
11136 : :
11137 : : struct NonTrivialField
11138 : : {
11139 : 876 : bool operator() (const_tree t) const
11140 : : {
11141 : 876 : return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t);
11142 : : }
11143 : : };
11144 : :
11145 : : /* Return the DECL of the first non-trivial subobject of class TYPE
11146 : : or null if none can be found. */
11147 : :
11148 : : static inline tree
11149 : 174 : first_non_trivial_field (tree type)
11150 : : {
11151 : 174 : return first_non_static_field (type, NonTrivialField ());
11152 : : }
11153 : :
11154 : : } /* unnamed namespace */
11155 : :
11156 : : /* Return true if all copy and move assignment operator overloads for
11157 : : class TYPE are trivial and at least one of them is not deleted and,
11158 : : when ACCESS is set, accessible. Return false otherwise. Set
11159 : : HASASSIGN to true when the TYPE has a (not necessarily trivial)
11160 : : copy or move assignment. */
11161 : :
11162 : : static bool
11163 : 5010 : has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
11164 : : {
11165 : 5010 : tree fns = get_class_binding (type, assign_op_identifier);
11166 : 5010 : bool all_trivial = true;
11167 : :
11168 : : /* Iterate over overloads of the assignment operator, checking
11169 : : accessible copy assignments for triviality. */
11170 : :
11171 : 16471 : for (tree f : ovl_range (fns))
11172 : : {
11173 : : /* Skip operators that aren't copy assignments. */
11174 : 8549 : if (!copy_fn_p (f))
11175 : 3035 : continue;
11176 : :
11177 : 5514 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11178 : 5802 : || accessible_p (TYPE_BINFO (type), f, true));
11179 : :
11180 : : /* Skip template assignment operators and deleted functions. */
11181 : 5514 : if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
11182 : 694 : continue;
11183 : :
11184 : 4820 : if (accessible)
11185 : 4568 : *hasassign = true;
11186 : :
11187 : 4568 : if (!accessible || !trivial_fn_p (f))
11188 : : all_trivial = false;
11189 : :
11190 : : /* Break early when both properties have been determined. */
11191 : 4820 : if (*hasassign && !all_trivial)
11192 : : break;
11193 : : }
11194 : :
11195 : : /* Return true if they're all trivial and one of the expressions
11196 : : TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
11197 : 5010 : tree ref = cp_build_reference_type (type, false);
11198 : 5010 : return (all_trivial
11199 : 5010 : && (is_trivially_xible (MODIFY_EXPR, type, type)
11200 : 333 : || is_trivially_xible (MODIFY_EXPR, type, ref)));
11201 : : }
11202 : :
11203 : : /* Return true if all copy and move ctor overloads for class TYPE are
11204 : : trivial and at least one of them is not deleted and, when ACCESS is
11205 : : set, accessible. Return false otherwise. Set each element of HASCTOR[]
11206 : : to true when the TYPE has a (not necessarily trivial) default and copy
11207 : : (or move) ctor, respectively. */
11208 : :
11209 : : static bool
11210 : 5010 : has_trivial_copy_p (tree type, bool access, bool hasctor[2])
11211 : : {
11212 : 5010 : tree fns = get_class_binding (type, complete_ctor_identifier);
11213 : 5010 : bool all_trivial = true;
11214 : :
11215 : 25075 : for (tree f : ovl_range (fns))
11216 : : {
11217 : : /* Skip template constructors. */
11218 : 12569 : if (TREE_CODE (f) != FUNCTION_DECL)
11219 : 105 : continue;
11220 : :
11221 : 12464 : bool cpy_or_move_ctor_p = copy_fn_p (f);
11222 : :
11223 : : /* Skip ctors other than default, copy, and move. */
11224 : 12464 : if (!cpy_or_move_ctor_p && !default_ctor_p (f))
11225 : 2746 : continue;
11226 : :
11227 : 9718 : if (DECL_DELETED_FN (f))
11228 : 306 : continue;
11229 : :
11230 : 9412 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11231 : 9604 : || accessible_p (TYPE_BINFO (type), f, true));
11232 : :
11233 : 9292 : if (accessible)
11234 : 9292 : hasctor[cpy_or_move_ctor_p] = true;
11235 : :
11236 : 9412 : if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
11237 : : all_trivial = false;
11238 : :
11239 : : /* Break early when both properties have been determined. */
11240 : 9412 : if (hasctor[0] && hasctor[1] && !all_trivial)
11241 : : break;
11242 : : }
11243 : :
11244 : 5010 : return all_trivial;
11245 : : }
11246 : :
11247 : : /* Issue a warning on a call to the built-in function FNDECL if it is
11248 : : a raw memory write whose destination is not an object of (something
11249 : : like) trivial or standard layout type with a non-deleted assignment
11250 : : and copy ctor. Detects const correctness violations, corrupting
11251 : : references, virtual table pointers, and bypassing non-trivial
11252 : : assignments. */
11253 : :
11254 : : static void
11255 : 66078 : maybe_warn_class_memaccess (location_t loc, tree fndecl,
11256 : : const vec<tree, va_gc> *args)
11257 : : {
11258 : : /* Except for bcopy where it's second, the destination pointer is
11259 : : the first argument for all functions handled here. Compute
11260 : : the index of the destination and source arguments. */
11261 : 66078 : unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
11262 : 66078 : unsigned srcidx = !dstidx;
11263 : :
11264 : 66078 : tree dest = (*args)[dstidx];
11265 : 66078 : if (!TREE_TYPE (dest)
11266 : 66078 : || (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE
11267 : 64923 : && !INDIRECT_TYPE_P (TREE_TYPE (dest))))
11268 : 62703 : return;
11269 : :
11270 : 21293 : tree srctype = NULL_TREE;
11271 : :
11272 : : /* Determine the type of the pointed-to object and whether it's
11273 : : a complete class type. */
11274 : 21293 : tree desttype = TREE_TYPE (TREE_TYPE (dest));
11275 : :
11276 : 21293 : if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
11277 : : return;
11278 : :
11279 : : /* Check to see if the raw memory call is made by a non-static member
11280 : : function with THIS as the destination argument for the destination
11281 : : type. If so, and if the class has no non-trivial bases or members,
11282 : : be more permissive. */
11283 : 5112 : if (current_function_decl
11284 : 5112 : && DECL_OBJECT_MEMBER_FUNCTION_P (current_function_decl)
11285 : 5379 : && is_object_parameter (tree_strip_nop_conversions (dest)))
11286 : : {
11287 : 204 : tree ctx = DECL_CONTEXT (current_function_decl);
11288 : 204 : bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
11289 : 204 : tree binfo = TYPE_BINFO (ctx);
11290 : :
11291 : 204 : if (special
11292 : 204 : && !BINFO_VTABLE (binfo)
11293 : 378 : && !first_non_trivial_field (desttype))
11294 : : return;
11295 : : }
11296 : :
11297 : : /* True if the class is trivial. */
11298 : 5010 : bool trivial = trivial_type_p (desttype);
11299 : :
11300 : : /* Set to true if DESTYPE has an accessible copy assignment. */
11301 : 5010 : bool hasassign = false;
11302 : : /* True if all of the class' overloaded copy assignment operators
11303 : : are all trivial (and not deleted) and at least one of them is
11304 : : accessible. */
11305 : 5010 : bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
11306 : :
11307 : : /* Set to true if DESTTYPE has an accessible default and copy ctor,
11308 : : respectively. */
11309 : 5010 : bool hasctors[2] = { false, false };
11310 : :
11311 : : /* True if all of the class' overloaded copy constructors are all
11312 : : trivial (and not deleted) and at least one of them is accessible. */
11313 : 5010 : bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
11314 : :
11315 : : /* Set FLD to the first private/protected member of the class. */
11316 : 5010 : tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
11317 : :
11318 : : /* The warning format string. */
11319 : 5010 : const char *warnfmt = NULL;
11320 : : /* A suggested alternative to offer instead of the raw memory call.
11321 : : Empty string when none can be come up with. */
11322 : 5010 : const char *suggest = "";
11323 : 5010 : bool warned = false;
11324 : :
11325 : 5010 : switch (DECL_FUNCTION_CODE (fndecl))
11326 : : {
11327 : 560 : case BUILT_IN_MEMSET:
11328 : 560 : if (!integer_zerop (maybe_constant_value ((*args)[1])))
11329 : : {
11330 : : /* Diagnose setting non-copy-assignable or non-trivial types,
11331 : : or types with a private member, to (potentially) non-zero
11332 : : bytes. Since the value of the bytes being written is unknown,
11333 : : suggest using assignment instead (if one exists). Also warn
11334 : : for writes into objects for which zero-initialization doesn't
11335 : : mean all bits clear (pointer-to-member data, where null is all
11336 : : bits set). Since the value being written is (most likely)
11337 : : non-zero, simply suggest assignment (but not copy assignment). */
11338 : 196 : suggest = "; use assignment instead";
11339 : 196 : if (!trivassign)
11340 : : warnfmt = G_("%qD writing to an object of type %#qT with "
11341 : : "no trivial copy-assignment");
11342 : 125 : else if (!trivial)
11343 : : warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
11344 : 85 : else if (fld)
11345 : : {
11346 : 24 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11347 : 24 : warned = warning_at (loc, OPT_Wclass_memaccess,
11348 : : "%qD writing to an object of type %#qT with "
11349 : : "%qs member %qD",
11350 : : fndecl, desttype, access, fld);
11351 : : }
11352 : 61 : else if (!zero_init_p (desttype))
11353 : : warnfmt = G_("%qD writing to an object of type %#qT containing "
11354 : : "a pointer to data member%s");
11355 : :
11356 : : break;
11357 : : }
11358 : : /* Fall through. */
11359 : :
11360 : 481 : case BUILT_IN_BZERO:
11361 : : /* Similarly to the above, diagnose clearing non-trivial or non-
11362 : : standard layout objects, or objects of types with no assignmenmt.
11363 : : Since the value being written is known to be zero, suggest either
11364 : : copy assignment, copy ctor, or default ctor as an alternative,
11365 : : depending on what's available. */
11366 : :
11367 : 481 : if (hasassign && hasctors[0])
11368 : : suggest = G_("; use assignment or value-initialization instead");
11369 : 49 : else if (hasassign)
11370 : : suggest = G_("; use assignment instead");
11371 : 31 : else if (hasctors[0])
11372 : 16 : suggest = G_("; use value-initialization instead");
11373 : :
11374 : 481 : if (!trivassign)
11375 : : warnfmt = G_("%qD clearing an object of type %#qT with "
11376 : : "no trivial copy-assignment%s");
11377 : 374 : else if (!trivial)
11378 : : warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
11379 : 304 : else if (!zero_init_p (desttype))
11380 : : warnfmt = G_("%qD clearing an object of type %#qT containing "
11381 : : "a pointer-to-member%s");
11382 : : break;
11383 : :
11384 : 2437 : case BUILT_IN_BCOPY:
11385 : 2437 : case BUILT_IN_MEMCPY:
11386 : 2437 : case BUILT_IN_MEMMOVE:
11387 : 2437 : case BUILT_IN_MEMPCPY:
11388 : : /* Determine the type of the source object. */
11389 : 2437 : srctype = TREE_TYPE ((*args)[srcidx]);
11390 : 2437 : if (!srctype || !INDIRECT_TYPE_P (srctype))
11391 : 0 : srctype = void_type_node;
11392 : : else
11393 : 2437 : srctype = TREE_TYPE (srctype);
11394 : :
11395 : : /* Since it's impossible to determine wheter the byte copy is
11396 : : being used in place of assignment to an existing object or
11397 : : as a substitute for initialization, assume it's the former.
11398 : : Determine the best alternative to use instead depending on
11399 : : what's not deleted. */
11400 : 2437 : if (hasassign && hasctors[1])
11401 : : suggest = G_("; use copy-assignment or copy-initialization instead");
11402 : 488 : else if (hasassign)
11403 : : suggest = G_("; use copy-assignment instead");
11404 : 341 : else if (hasctors[1])
11405 : 290 : suggest = G_("; use copy-initialization instead");
11406 : :
11407 : 2437 : if (!trivassign)
11408 : : warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
11409 : : "copy-assignment%s");
11410 : 1639 : else if (!trivially_copyable_p (desttype))
11411 : : warnfmt = G_("%qD writing to an object of non-trivially copyable "
11412 : : "type %#qT%s");
11413 : 1315 : else if (!trivcopy)
11414 : : warnfmt = G_("%qD writing to an object with a deleted copy constructor");
11415 : :
11416 : 1315 : else if (!trivial
11417 : 211 : && !VOID_TYPE_P (srctype)
11418 : 160 : && !is_byte_access_type (srctype)
11419 : 1412 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11420 : : srctype))
11421 : : {
11422 : : /* Warn when copying into a non-trivial object from an object
11423 : : of a different type other than void or char. */
11424 : 54 : warned = warning_at (loc, OPT_Wclass_memaccess,
11425 : : "%qD copying an object of non-trivial type "
11426 : : "%#qT from an array of %#qT",
11427 : : fndecl, desttype, srctype);
11428 : : }
11429 : 1261 : else if (fld
11430 : 432 : && !VOID_TYPE_P (srctype)
11431 : 384 : && !is_byte_access_type (srctype)
11432 : 1549 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11433 : : srctype))
11434 : : {
11435 : 216 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11436 : 216 : warned = warning_at (loc, OPT_Wclass_memaccess,
11437 : : "%qD copying an object of type %#qT with "
11438 : : "%qs member %qD from an array of %#qT; use "
11439 : : "assignment or copy-initialization instead",
11440 : : fndecl, desttype, access, fld, srctype);
11441 : : }
11442 : 1045 : else if (!trivial && vec_safe_length (args) > 2)
11443 : : {
11444 : 157 : tree sz = maybe_constant_value ((*args)[2]);
11445 : 157 : if (!tree_fits_uhwi_p (sz))
11446 : : break;
11447 : :
11448 : : /* Finally, warn on partial copies. */
11449 : 99 : unsigned HOST_WIDE_INT typesize
11450 : 99 : = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
11451 : 99 : if (typesize == 0)
11452 : : break;
11453 : 96 : if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
11454 : 12 : warned = warning_at (loc, OPT_Wclass_memaccess,
11455 : : (typesize - partial > 1
11456 : : ? G_("%qD writing to an object of "
11457 : : "a non-trivial type %#qT leaves %wu "
11458 : : "bytes unchanged")
11459 : : : G_("%qD writing to an object of "
11460 : : "a non-trivial type %#qT leaves %wu "
11461 : : "byte unchanged")),
11462 : : fndecl, desttype, typesize - partial);
11463 : : }
11464 : : break;
11465 : :
11466 : 261 : case BUILT_IN_REALLOC:
11467 : :
11468 : 261 : if (!trivially_copyable_p (desttype))
11469 : : warnfmt = G_("%qD moving an object of non-trivially copyable type "
11470 : : "%#qT; use %<new%> and %<delete%> instead");
11471 : 138 : else if (!trivcopy)
11472 : : warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
11473 : : "constructor; use %<new%> and %<delete%> instead");
11474 : 135 : else if (!get_dtor (desttype, tf_none))
11475 : : warnfmt = G_("%qD moving an object of type %#qT with deleted "
11476 : : "destructor");
11477 : 126 : else if (!trivial)
11478 : : {
11479 : 33 : tree sz = maybe_constant_value ((*args)[1]);
11480 : 33 : if (TREE_CODE (sz) == INTEGER_CST
11481 : 33 : && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype)))
11482 : : /* Finally, warn on reallocation into insufficient space. */
11483 : 7 : warned = warning_at (loc, OPT_Wclass_memaccess,
11484 : : "%qD moving an object of non-trivial type "
11485 : : "%#qT and size %E into a region of size %E",
11486 : 7 : fndecl, desttype, TYPE_SIZE_UNIT (desttype),
11487 : : sz);
11488 : : }
11489 : : break;
11490 : :
11491 : : default:
11492 : : return;
11493 : : }
11494 : :
11495 : 310 : if (warnfmt)
11496 : : {
11497 : 1569 : if (suggest)
11498 : 1569 : warned = warning_at (loc, OPT_Wclass_memaccess,
11499 : : warnfmt, fndecl, desttype, suggest);
11500 : : else
11501 : : warned = warning_at (loc, OPT_Wclass_memaccess,
11502 : : warnfmt, fndecl, desttype);
11503 : : }
11504 : :
11505 : 3375 : if (warned)
11506 : 1879 : inform (location_of (desttype), "%#qT declared here", desttype);
11507 : : }
11508 : :
11509 : : /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
11510 : : If FN is the result of resolving an overloaded target built-in,
11511 : : ORIG_FNDECL is the original function decl, otherwise it is null.
11512 : : This function performs no overload resolution, conversion, or other
11513 : : high-level operations. */
11514 : :
11515 : : tree
11516 : 127739423 : build_cxx_call (tree fn, int nargs, tree *argarray,
11517 : : tsubst_flags_t complain, tree orig_fndecl)
11518 : : {
11519 : 127739423 : tree fndecl;
11520 : :
11521 : : /* Remember roughly where this call is. */
11522 : 127739423 : location_t loc = cp_expr_loc_or_input_loc (fn);
11523 : 127739423 : fn = build_call_a (fn, nargs, argarray);
11524 : 127739423 : SET_EXPR_LOCATION (fn, loc);
11525 : :
11526 : 127739423 : fndecl = get_callee_fndecl (fn);
11527 : 127739423 : if (!orig_fndecl)
11528 : 127739423 : orig_fndecl = fndecl;
11529 : :
11530 : : /* Check that arguments to builtin functions match the expectations. */
11531 : 127739423 : if (fndecl
11532 : 124742119 : && !processing_template_decl
11533 : 252427275 : && fndecl_built_in_p (fndecl))
11534 : : {
11535 : : int i;
11536 : :
11537 : : /* We need to take care that values to BUILT_IN_NORMAL
11538 : : are reduced. */
11539 : 20020096 : for (i = 0; i < nargs; i++)
11540 : 12937679 : argarray[i] = maybe_constant_value (argarray[i]);
11541 : :
11542 : 7082417 : if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
11543 : : orig_fndecl, nargs, argarray,
11544 : : complain & tf_error))
11545 : 804 : return error_mark_node;
11546 : 7081613 : else if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING))
11547 : : {
11548 : 10068 : tree arg0 = argarray[0];
11549 : 10068 : STRIP_NOPS (arg0);
11550 : 10068 : if (TREE_CODE (arg0) == ADDR_EXPR
11551 : 265 : && DECL_P (TREE_OPERAND (arg0, 0))
11552 : 10312 : && same_type_ignoring_top_level_qualifiers_p
11553 : 244 : (TREE_TYPE (TREE_TYPE (argarray[0])),
11554 : 244 : TREE_TYPE (TREE_TYPE (arg0))))
11555 : : /* For __builtin_clear_padding (&var) we know the type
11556 : : is for a complete object, so there is no risk in clearing
11557 : : padding that is reused in some derived class member. */;
11558 : 9831 : else if (!trivially_copyable_p (TREE_TYPE (TREE_TYPE (argarray[0]))))
11559 : : {
11560 : 18 : error_at (EXPR_LOC_OR_LOC (argarray[0], input_location),
11561 : : "argument %u in call to function %qE "
11562 : : "has pointer to a non-trivially-copyable type (%qT)",
11563 : 18 : 1, fndecl, TREE_TYPE (argarray[0]));
11564 : 18 : return error_mark_node;
11565 : : }
11566 : : }
11567 : : }
11568 : :
11569 : 127738601 : if (VOID_TYPE_P (TREE_TYPE (fn)))
11570 : : return fn;
11571 : :
11572 : : /* 5.2.2/11: If a function call is a prvalue of object type: if the
11573 : : function call is either the operand of a decltype-specifier or the
11574 : : right operand of a comma operator that is the operand of a
11575 : : decltype-specifier, a temporary object is not introduced for the
11576 : : prvalue. The type of the prvalue may be incomplete. */
11577 : 97444914 : if (!(complain & tf_decltype))
11578 : : {
11579 : 81830214 : fn = require_complete_type (fn, complain);
11580 : 81830214 : if (fn == error_mark_node)
11581 : : return error_mark_node;
11582 : :
11583 : 81830190 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
11584 : : {
11585 : 9017429 : fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
11586 : 9017429 : maybe_warn_parm_abi (TREE_TYPE (fn), loc);
11587 : : }
11588 : : }
11589 : 97444890 : return convert_from_reference (fn);
11590 : : }
11591 : :
11592 : : /* Returns the value to use for the in-charge parameter when making a
11593 : : call to a function with the indicated NAME.
11594 : :
11595 : : FIXME:Can't we find a neater way to do this mapping? */
11596 : :
11597 : : tree
11598 : 40136 : in_charge_arg_for_name (tree name)
11599 : : {
11600 : 40136 : if (IDENTIFIER_CTOR_P (name))
11601 : : {
11602 : 22562 : if (name == complete_ctor_identifier)
11603 : 11281 : return integer_one_node;
11604 : 11281 : gcc_checking_assert (name == base_ctor_identifier);
11605 : : }
11606 : : else
11607 : : {
11608 : 17574 : if (name == complete_dtor_identifier)
11609 : 8787 : return integer_two_node;
11610 : 8787 : else if (name == deleting_dtor_identifier)
11611 : : /* The deleting dtor should now be handled by
11612 : : build_delete_destructor_body. */
11613 : 0 : gcc_unreachable ();
11614 : 8787 : gcc_checking_assert (name == base_dtor_identifier);
11615 : : }
11616 : :
11617 : 20068 : return integer_zero_node;
11618 : : }
11619 : :
11620 : : /* We've built up a constructor call RET. Complain if it delegates to the
11621 : : constructor we're currently compiling. */
11622 : :
11623 : : static void
11624 : 289255 : check_self_delegation (tree ret)
11625 : : {
11626 : 289255 : if (TREE_CODE (ret) == TARGET_EXPR)
11627 : 0 : ret = TARGET_EXPR_INITIAL (ret);
11628 : 289255 : tree fn = cp_get_callee_fndecl_nofold (ret);
11629 : 289255 : if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
11630 : 46 : error ("constructor delegates to itself");
11631 : 289255 : }
11632 : :
11633 : : /* Build a call to a constructor, destructor, or an assignment
11634 : : operator for INSTANCE, an expression with class type. NAME
11635 : : indicates the special member function to call; *ARGS are the
11636 : : arguments. ARGS may be NULL. This may change ARGS. BINFO
11637 : : indicates the base of INSTANCE that is to be passed as the `this'
11638 : : parameter to the member function called.
11639 : :
11640 : : FLAGS are the LOOKUP_* flags to use when processing the call.
11641 : :
11642 : : If NAME indicates a complete object constructor, INSTANCE may be
11643 : : NULL_TREE. In this case, the caller will call build_cplus_new to
11644 : : store the newly constructed object into a VAR_DECL. */
11645 : :
11646 : : tree
11647 : 30254545 : build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
11648 : : tree binfo, int flags, tsubst_flags_t complain)
11649 : : {
11650 : 30254545 : tree fns;
11651 : : /* The type of the subobject to be constructed or destroyed. */
11652 : 30254545 : tree class_type;
11653 : 30254545 : vec<tree, va_gc> *allocated = NULL;
11654 : 30254545 : tree ret;
11655 : :
11656 : 30254545 : gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
11657 : :
11658 : 30254545 : if (error_operand_p (instance))
11659 : 0 : return error_mark_node;
11660 : :
11661 : 30254545 : if (IDENTIFIER_DTOR_P (name))
11662 : : {
11663 : 8734722 : gcc_assert (args == NULL || vec_safe_is_empty (*args));
11664 : 8734722 : if (!type_build_dtor_call (TREE_TYPE (instance)))
11665 : : /* Shortcut to avoid lazy destructor declaration. */
11666 : 38151 : return build_trivial_dtor_call (instance);
11667 : : }
11668 : :
11669 : 30216394 : if (TYPE_P (binfo))
11670 : : {
11671 : : /* Resolve the name. */
11672 : 23393064 : if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
11673 : 24 : return error_mark_node;
11674 : :
11675 : 23393040 : binfo = TYPE_BINFO (binfo);
11676 : : }
11677 : :
11678 : 23393040 : gcc_assert (binfo != NULL_TREE);
11679 : :
11680 : 30216370 : class_type = BINFO_TYPE (binfo);
11681 : :
11682 : : /* Handle the special case where INSTANCE is NULL_TREE. */
11683 : 30216370 : if (name == complete_ctor_identifier && !instance)
11684 : 15293621 : instance = build_dummy_object (class_type);
11685 : : else
11686 : : {
11687 : : /* Convert to the base class, if necessary. */
11688 : 14922749 : if (!same_type_ignoring_top_level_qualifiers_p
11689 : 14922749 : (TREE_TYPE (instance), BINFO_TYPE (binfo)))
11690 : : {
11691 : 1669100 : if (IDENTIFIER_CDTOR_P (name))
11692 : : /* For constructors and destructors, either the base is
11693 : : non-virtual, or it is virtual but we are doing the
11694 : : conversion from a constructor or destructor for the
11695 : : complete object. In either case, we can convert
11696 : : statically. */
11697 : 1648670 : instance = convert_to_base_statically (instance, binfo);
11698 : : else
11699 : : {
11700 : : /* However, for assignment operators, we must convert
11701 : : dynamically if the base is virtual. */
11702 : 20430 : gcc_checking_assert (name == assign_op_identifier);
11703 : 20430 : instance = build_base_path (PLUS_EXPR, instance,
11704 : : binfo, /*nonnull=*/1, complain);
11705 : : }
11706 : : }
11707 : : }
11708 : :
11709 : 30216370 : gcc_assert (instance != NULL_TREE);
11710 : :
11711 : : /* In C++17, "If the initializer expression is a prvalue and the
11712 : : cv-unqualified version of the source type is the same class as the class
11713 : : of the destination, the initializer expression is used to initialize the
11714 : : destination object." Handle that here to avoid doing overload
11715 : : resolution. */
11716 : 30216370 : if (cxx_dialect >= cxx17
11717 : 30032603 : && args && vec_safe_length (*args) == 1
11718 : 47154094 : && !unsafe_return_slot_p (instance))
11719 : : {
11720 : 15867463 : tree arg = (**args)[0];
11721 : :
11722 : 807969 : if (BRACE_ENCLOSED_INITIALIZER_P (arg)
11723 : 807952 : && !TYPE_HAS_LIST_CTOR (class_type)
11724 : 757414 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg)
11725 : 16624863 : && CONSTRUCTOR_NELTS (arg) == 1)
11726 : 442178 : arg = CONSTRUCTOR_ELT (arg, 0)->value;
11727 : :
11728 : 15867463 : if ((TREE_CODE (arg) == TARGET_EXPR
11729 : 7163853 : || TREE_CODE (arg) == CONSTRUCTOR)
11730 : 24936944 : && (same_type_ignoring_top_level_qualifiers_p
11731 : 9069481 : (class_type, TREE_TYPE (arg))))
11732 : : {
11733 : 8310909 : if (is_dummy_object (instance))
11734 : : return arg;
11735 : 160939 : else if (TREE_CODE (arg) == TARGET_EXPR)
11736 : 160939 : TARGET_EXPR_DIRECT_INIT_P (arg) = true;
11737 : :
11738 : 160939 : if ((complain & tf_error)
11739 : 160937 : && (flags & LOOKUP_DELEGATING_CONS))
11740 : 0 : check_self_delegation (arg);
11741 : : /* Avoid change of behavior on Wunused-var-2.C. */
11742 : 160939 : instance = mark_lvalue_use (instance);
11743 : 160939 : return cp_build_init_expr (instance, arg);
11744 : : }
11745 : : }
11746 : :
11747 : 21905461 : fns = lookup_fnfields (binfo, name, 1, complain);
11748 : :
11749 : : /* When making a call to a constructor or destructor for a subobject
11750 : : that uses virtual base classes, pass down a pointer to a VTT for
11751 : : the subobject. */
11752 : 21905461 : if ((name == base_ctor_identifier
11753 : 19789195 : || name == base_dtor_identifier)
11754 : 23554251 : && CLASSTYPE_VBASECLASSES (class_type))
11755 : : {
11756 : 49567 : tree vtt;
11757 : 49567 : tree sub_vtt;
11758 : :
11759 : : /* If the current function is a complete object constructor
11760 : : or destructor, then we fetch the VTT directly.
11761 : : Otherwise, we look it up using the VTT we were given. */
11762 : 49567 : vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
11763 : 49567 : vtt = decay_conversion (vtt, complain);
11764 : 49567 : if (vtt == error_mark_node)
11765 : 0 : return error_mark_node;
11766 : 49567 : vtt = build_if_in_charge (vtt, current_vtt_parm);
11767 : 49567 : if (BINFO_SUBVTT_INDEX (binfo))
11768 : 49307 : sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
11769 : : else
11770 : 260 : sub_vtt = vtt;
11771 : :
11772 : 49567 : if (args == NULL)
11773 : : {
11774 : 31620 : allocated = make_tree_vector ();
11775 : 31620 : args = &allocated;
11776 : : }
11777 : :
11778 : 49567 : vec_safe_insert (*args, 0, sub_vtt);
11779 : : }
11780 : :
11781 : 43810922 : ret = build_new_method_call (instance, fns, args,
11782 : 21905461 : TYPE_BINFO (BINFO_TYPE (binfo)),
11783 : : flags, /*fn=*/NULL,
11784 : : complain);
11785 : :
11786 : 21905461 : if (allocated != NULL)
11787 : 31620 : release_tree_vector (allocated);
11788 : :
11789 : 21905461 : if ((complain & tf_error)
11790 : 19752075 : && (flags & LOOKUP_DELEGATING_CONS)
11791 : 289395 : && name == complete_ctor_identifier)
11792 : 289255 : check_self_delegation (ret);
11793 : :
11794 : : return ret;
11795 : : }
11796 : :
11797 : : /* Return the NAME, as a C string. The NAME indicates a function that
11798 : : is a member of TYPE. *FREE_P is set to true if the caller must
11799 : : free the memory returned.
11800 : :
11801 : : Rather than go through all of this, we should simply set the names
11802 : : of constructors and destructors appropriately, and dispense with
11803 : : ctor_identifier, dtor_identifier, etc. */
11804 : :
11805 : : static char *
11806 : 91 : name_as_c_string (tree name, tree type, bool *free_p)
11807 : : {
11808 : 91 : const char *pretty_name;
11809 : :
11810 : : /* Assume that we will not allocate memory. */
11811 : 91 : *free_p = false;
11812 : : /* Constructors and destructors are special. */
11813 : 91 : if (IDENTIFIER_CDTOR_P (name))
11814 : : {
11815 : 42 : pretty_name
11816 : 42 : = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
11817 : : /* For a destructor, add the '~'. */
11818 : 42 : if (IDENTIFIER_DTOR_P (name))
11819 : : {
11820 : 0 : pretty_name = concat ("~", pretty_name, NULL);
11821 : : /* Remember that we need to free the memory allocated. */
11822 : 0 : *free_p = true;
11823 : : }
11824 : : }
11825 : 49 : else if (IDENTIFIER_CONV_OP_P (name))
11826 : : {
11827 : 0 : pretty_name = concat ("operator ",
11828 : 0 : type_as_string_translate (TREE_TYPE (name),
11829 : : TFF_PLAIN_IDENTIFIER),
11830 : : NULL);
11831 : : /* Remember that we need to free the memory allocated. */
11832 : 0 : *free_p = true;
11833 : : }
11834 : : else
11835 : 49 : pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
11836 : :
11837 : 91 : return CONST_CAST (char *, pretty_name);
11838 : : }
11839 : :
11840 : : /* If CANDIDATES contains exactly one candidate, return it, otherwise
11841 : : return NULL. */
11842 : :
11843 : : static z_candidate *
11844 : 687 : single_z_candidate (z_candidate *candidates)
11845 : : {
11846 : 0 : if (candidates == NULL)
11847 : : return NULL;
11848 : :
11849 : 675 : if (candidates->next)
11850 : 0 : return NULL;
11851 : :
11852 : : return candidates;
11853 : : }
11854 : :
11855 : : /* If CANDIDATE is invalid due to a bad argument type, return the
11856 : : pertinent conversion_info.
11857 : :
11858 : : Otherwise, return NULL. */
11859 : :
11860 : : static const conversion_info *
11861 : 335 : maybe_get_bad_conversion_for_unmatched_call (const z_candidate *candidate)
11862 : : {
11863 : : /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
11864 : 335 : rejection_reason *r = candidate->reason;
11865 : :
11866 : 335 : if (r == NULL)
11867 : : return NULL;
11868 : :
11869 : 335 : switch (r->code)
11870 : : {
11871 : : default:
11872 : : return NULL;
11873 : :
11874 : 50 : case rr_arg_conversion:
11875 : 50 : return &r->u.conversion;
11876 : :
11877 : 6 : case rr_bad_arg_conversion:
11878 : 6 : return &r->u.bad_conversion;
11879 : : }
11880 : : }
11881 : :
11882 : : /* Issue an error and note complaining about a bad argument type at a
11883 : : callsite with a single candidate FNDECL.
11884 : :
11885 : : ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
11886 : : case input_location is used).
11887 : : FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
11888 : : the formal parameter. */
11889 : :
11890 : : void
11891 : 148 : complain_about_bad_argument (location_t arg_loc,
11892 : : tree from_type, tree to_type,
11893 : : tree fndecl, int parmnum)
11894 : : {
11895 : 148 : auto_diagnostic_group d;
11896 : 148 : range_label_for_type_mismatch rhs_label (from_type, to_type);
11897 : 148 : range_label *label = &rhs_label;
11898 : 148 : if (arg_loc == UNKNOWN_LOCATION)
11899 : : {
11900 : 6 : arg_loc = input_location;
11901 : 6 : label = NULL;
11902 : : }
11903 : 148 : gcc_rich_location richloc (arg_loc, label, highlight_colors::percent_h);
11904 : 148 : error_at (&richloc,
11905 : : "cannot convert %qH to %qI",
11906 : : from_type, to_type);
11907 : 148 : maybe_inform_about_fndecl_for_bogus_argument_init
11908 : 148 : (fndecl,
11909 : : parmnum,
11910 : : highlight_colors::percent_i);
11911 : 148 : }
11912 : :
11913 : : /* Subroutine of build_new_method_call_1, for where there are no viable
11914 : : candidates for the call. */
11915 : :
11916 : : static void
11917 : 693 : complain_about_no_candidates_for_method_call (tree instance,
11918 : : z_candidate *candidates,
11919 : : tree explicit_targs,
11920 : : tree basetype,
11921 : : tree optype, tree name,
11922 : : bool skip_first_for_error,
11923 : : vec<tree, va_gc> *user_args)
11924 : : {
11925 : 693 : auto_diagnostic_group d;
11926 : 693 : if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
11927 : 0 : cxx_incomplete_type_error (instance, basetype);
11928 : 693 : else if (optype)
11929 : 6 : error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
11930 : : basetype, optype, build_tree_list_vec (user_args),
11931 : 6 : TREE_TYPE (instance));
11932 : : else
11933 : : {
11934 : : /* Special-case for when there's a single candidate that's failing
11935 : : due to a bad argument type. */
11936 : 687 : if (z_candidate *candidate = single_z_candidate (candidates))
11937 : 335 : if (const conversion_info *conv
11938 : 335 : = maybe_get_bad_conversion_for_unmatched_call (candidate))
11939 : : {
11940 : 56 : tree from_type = conv->from;
11941 : 56 : if (!TYPE_P (conv->from))
11942 : 6 : from_type = lvalue_type (conv->from);
11943 : 56 : complain_about_bad_argument (conv->loc,
11944 : 56 : from_type, conv->to_type,
11945 : 56 : candidate->fn, conv->n_arg);
11946 : 56 : return;
11947 : : }
11948 : :
11949 : 631 : tree arglist = build_tree_list_vec (user_args);
11950 : 631 : tree errname = name;
11951 : 631 : bool twiddle = false;
11952 : 631 : if (IDENTIFIER_CDTOR_P (errname))
11953 : : {
11954 : 330 : twiddle = IDENTIFIER_DTOR_P (errname);
11955 : 330 : errname = constructor_name (basetype);
11956 : : }
11957 : 631 : if (explicit_targs)
11958 : 46 : errname = lookup_template_function (errname, explicit_targs);
11959 : 631 : if (skip_first_for_error)
11960 : 3 : arglist = TREE_CHAIN (arglist);
11961 : 1262 : error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
11962 : 631 : basetype, &"~"[!twiddle], errname, arglist,
11963 : 631 : TREE_TYPE (instance));
11964 : : }
11965 : 637 : print_z_candidates (location_of (name), candidates);
11966 : 693 : }
11967 : :
11968 : : /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
11969 : : be set, upon return, to the function called. ARGS may be NULL.
11970 : : This may change ARGS. */
11971 : :
11972 : : tree
11973 : 84505358 : build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
11974 : : tree conversion_path, int flags,
11975 : : tree *fn_p, tsubst_flags_t complain)
11976 : : {
11977 : 84505358 : struct z_candidate *candidates = 0, *cand;
11978 : 84505358 : tree explicit_targs = NULL_TREE;
11979 : 84505358 : tree basetype = NULL_TREE;
11980 : 84505358 : tree access_binfo;
11981 : 84505358 : tree optype;
11982 : 84505358 : tree first_mem_arg = NULL_TREE;
11983 : 84505358 : tree name;
11984 : 84505358 : bool skip_first_for_error;
11985 : 84505358 : vec<tree, va_gc> *user_args;
11986 : 84505358 : tree call;
11987 : 84505358 : tree fn;
11988 : 84505358 : int template_only = 0;
11989 : 84505358 : bool any_viable_p;
11990 : 84505358 : tree orig_instance;
11991 : 84505358 : tree orig_fns;
11992 : 84505358 : vec<tree, va_gc> *orig_args = NULL;
11993 : :
11994 : 84505358 : auto_cond_timevar tv (TV_OVERLOAD);
11995 : :
11996 : 84505358 : gcc_assert (instance != NULL_TREE);
11997 : :
11998 : : /* We don't know what function we're going to call, yet. */
11999 : 84505358 : if (fn_p)
12000 : 23167790 : *fn_p = NULL_TREE;
12001 : :
12002 : 84505358 : if (error_operand_p (instance)
12003 : 84505358 : || !fns || error_operand_p (fns))
12004 : 1392561 : return error_mark_node;
12005 : :
12006 : 83112797 : if (!BASELINK_P (fns))
12007 : : {
12008 : 0 : if (complain & tf_error)
12009 : 0 : error ("call to non-function %qD", fns);
12010 : 0 : return error_mark_node;
12011 : : }
12012 : :
12013 : 83112797 : orig_instance = instance;
12014 : 83112797 : orig_fns = fns;
12015 : :
12016 : : /* Dismantle the baselink to collect all the information we need. */
12017 : 83112797 : if (!conversion_path)
12018 : 39445885 : conversion_path = BASELINK_BINFO (fns);
12019 : 83112797 : access_binfo = BASELINK_ACCESS_BINFO (fns);
12020 : 83112797 : optype = BASELINK_OPTYPE (fns);
12021 : 83112797 : fns = BASELINK_FUNCTIONS (fns);
12022 : 83112797 : if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12023 : : {
12024 : 6763104 : explicit_targs = TREE_OPERAND (fns, 1);
12025 : 6763104 : fns = TREE_OPERAND (fns, 0);
12026 : 6763104 : template_only = 1;
12027 : : }
12028 : 83112797 : gcc_assert (OVL_P (fns));
12029 : 83112797 : fn = OVL_FIRST (fns);
12030 : 83112797 : name = DECL_NAME (fn);
12031 : :
12032 : 83112797 : basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
12033 : 83112797 : gcc_assert (CLASS_TYPE_P (basetype));
12034 : :
12035 : 83112797 : user_args = args == NULL ? NULL : *args;
12036 : : /* Under DR 147 A::A() is an invalid constructor call,
12037 : : not a functional cast. */
12038 : 83112797 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
12039 : : {
12040 : 54 : if (! (complain & tf_error))
12041 : 0 : return error_mark_node;
12042 : :
12043 : 54 : basetype = DECL_CONTEXT (fn);
12044 : 54 : name = constructor_name (basetype);
12045 : 54 : auto_diagnostic_group d;
12046 : 54 : if (permerror (input_location,
12047 : : "cannot call constructor %<%T::%D%> directly",
12048 : : basetype, name))
12049 : 54 : inform (input_location, "for a function-style cast, remove the "
12050 : : "redundant %<::%D%>", name);
12051 : 54 : call = build_functional_cast (input_location, basetype,
12052 : : build_tree_list_vec (user_args),
12053 : : complain);
12054 : 54 : return call;
12055 : 54 : }
12056 : :
12057 : 83112743 : if (processing_template_decl)
12058 : 8055797 : orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
12059 : :
12060 : : /* Process the argument list. */
12061 : 83010332 : if (args != NULL && *args != NULL)
12062 : : {
12063 : 71081817 : *args = resolve_args (*args, complain);
12064 : 71081817 : if (*args == NULL)
12065 : 120 : return error_mark_node;
12066 : : user_args = *args;
12067 : : }
12068 : :
12069 : : /* Consider the object argument to be used even if we end up selecting a
12070 : : static member function. */
12071 : 83112623 : instance = mark_type_use (instance);
12072 : :
12073 : : /* Figure out whether to skip the first argument for the error
12074 : : message we will display to users if an error occurs. We don't
12075 : : want to display any compiler-generated arguments. The "this"
12076 : : pointer hasn't been added yet. However, we must remove the VTT
12077 : : pointer if this is a call to a base-class constructor or
12078 : : destructor. */
12079 : 83112623 : skip_first_for_error = false;
12080 : 83112623 : if (IDENTIFIER_CDTOR_P (name))
12081 : : {
12082 : : /* Callers should explicitly indicate whether they want to ctor
12083 : : the complete object or just the part without virtual bases. */
12084 : 41566546 : gcc_assert (name != ctor_identifier);
12085 : :
12086 : : /* Remove the VTT pointer, if present. */
12087 : 39450286 : if ((name == base_ctor_identifier || name == base_dtor_identifier)
12088 : 43215336 : && CLASSTYPE_VBASECLASSES (basetype))
12089 : : skip_first_for_error = true;
12090 : :
12091 : : /* It's OK to call destructors and constructors on cv-qualified
12092 : : objects. Therefore, convert the INSTANCE to the unqualified
12093 : : type, if necessary. */
12094 : 41566546 : if (!same_type_p (basetype, TREE_TYPE (instance)))
12095 : : {
12096 : 391885 : instance = build_this (instance);
12097 : 391885 : instance = build_nop (build_pointer_type (basetype), instance);
12098 : 391885 : instance = build_fold_indirect_ref (instance);
12099 : : }
12100 : : }
12101 : : else
12102 : 83092154 : gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
12103 : :
12104 : : /* For the overload resolution we need to find the actual `this`
12105 : : that would be captured if the call turns out to be to a
12106 : : non-static member function. Do not actually capture it at this
12107 : : point. */
12108 : 166225246 : if (DECL_CONSTRUCTOR_P (fn))
12109 : : /* Constructors don't use the enclosing 'this'. */
12110 : : first_mem_arg = instance;
12111 : : else
12112 : 63056426 : first_mem_arg = maybe_resolve_dummy (instance, false);
12113 : :
12114 : 83112623 : conversion_obstack_sentinel cos;
12115 : :
12116 : : /* The number of arguments artificial parms in ARGS; we subtract one because
12117 : : there's no 'this' in ARGS. */
12118 : 83112623 : unsigned skip = num_artificial_parms_for (fn) - 1;
12119 : :
12120 : : /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
12121 : : initializer, not T({ }). */
12122 : 83112623 : if (DECL_CONSTRUCTOR_P (fn)
12123 : 16709807 : && vec_safe_length (user_args) > skip
12124 : 97873896 : && DIRECT_LIST_INIT_P ((*user_args)[skip]))
12125 : : {
12126 : 808510 : tree init_list = (*user_args)[skip];
12127 : 808510 : tree init = NULL_TREE;
12128 : :
12129 : 808510 : gcc_assert (user_args->length () == skip + 1
12130 : : && !(flags & LOOKUP_ONLYCONVERTING));
12131 : :
12132 : : /* If the initializer list has no elements and T is a class type with
12133 : : a default constructor, the object is value-initialized. Handle
12134 : : this here so we don't need to handle it wherever we use
12135 : : build_special_member_call. */
12136 : 808510 : if (CONSTRUCTOR_NELTS (init_list) == 0
12137 : 132535 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
12138 : : /* For a user-provided default constructor, use the normal
12139 : : mechanisms so that protected access works. */
12140 : 132535 : && type_has_non_user_provided_default_constructor (basetype)
12141 : 769345 : && !processing_template_decl)
12142 : 93367 : init = build_value_init (basetype, complain);
12143 : :
12144 : : /* If BASETYPE is an aggregate, we need to do aggregate
12145 : : initialization. */
12146 : 715143 : else if (CP_AGGREGATE_TYPE_P (basetype))
12147 : : {
12148 : 42 : init = reshape_init (basetype, init_list, complain);
12149 : 42 : init = digest_init (basetype, init, complain);
12150 : : }
12151 : :
12152 : 93409 : if (init)
12153 : : {
12154 : 93409 : if (is_dummy_object (instance))
12155 : 30937 : return get_target_expr (init, complain);
12156 : 62472 : return cp_build_init_expr (instance, init);
12157 : : }
12158 : :
12159 : : /* Otherwise go ahead with overload resolution. */
12160 : 715101 : add_list_candidates (fns, first_mem_arg, user_args,
12161 : : basetype, explicit_targs, template_only,
12162 : : conversion_path, access_binfo, flags,
12163 : : &candidates, complain);
12164 : : }
12165 : : else
12166 : 82304113 : add_candidates (fns, first_mem_arg, user_args, optype,
12167 : : explicit_targs, template_only, conversion_path,
12168 : : access_binfo, flags, &candidates, complain);
12169 : :
12170 : 83019214 : any_viable_p = false;
12171 : 83019214 : candidates = splice_viable (candidates, false, &any_viable_p);
12172 : :
12173 : 83019214 : if (!any_viable_p)
12174 : : {
12175 : : /* [dcl.init], 17.6.2.2:
12176 : :
12177 : : Otherwise, if no constructor is viable, the destination type is
12178 : : a (possibly cv-qualified) aggregate class A, and the initializer
12179 : : is a parenthesized expression-list, the object is initialized as
12180 : : follows...
12181 : :
12182 : : We achieve this by building up a CONSTRUCTOR, as for list-init,
12183 : : and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
12184 : : the two. */
12185 : 19220 : if (DECL_CONSTRUCTOR_P (fn)
12186 : 14249 : && !(flags & LOOKUP_ONLYCONVERTING)
12187 : 14231 : && cxx_dialect >= cxx20
12188 : 11581 : && CP_AGGREGATE_TYPE_P (basetype)
12189 : 19220 : && !vec_safe_is_empty (user_args))
12190 : : {
12191 : : /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
12192 : 948 : tree ctor = build_constructor_from_vec (init_list_type_node,
12193 : : user_args);
12194 : 948 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
12195 : 948 : CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
12196 : 948 : if (is_dummy_object (instance))
12197 : : return ctor;
12198 : : else
12199 : : {
12200 : 687 : ctor = digest_init (basetype, ctor, complain);
12201 : 687 : if (ctor == error_mark_node)
12202 : : return error_mark_node;
12203 : 571 : return cp_build_init_expr (instance, ctor);
12204 : : }
12205 : : }
12206 : 18272 : if (complain & tf_error)
12207 : 693 : complain_about_no_candidates_for_method_call (instance, candidates,
12208 : : explicit_targs, basetype,
12209 : : optype, name,
12210 : : skip_first_for_error,
12211 : : user_args);
12212 : 18272 : call = error_mark_node;
12213 : : }
12214 : : else
12215 : : {
12216 : 82999994 : cand = tourney (candidates, complain);
12217 : 82999994 : if (cand == 0)
12218 : : {
12219 : 187 : char *pretty_name;
12220 : 187 : bool free_p;
12221 : 187 : tree arglist;
12222 : :
12223 : 187 : if (complain & tf_error)
12224 : : {
12225 : 91 : pretty_name = name_as_c_string (name, basetype, &free_p);
12226 : 91 : arglist = build_tree_list_vec (user_args);
12227 : 91 : if (skip_first_for_error)
12228 : 0 : arglist = TREE_CHAIN (arglist);
12229 : 91 : auto_diagnostic_group d;
12230 : 182 : if (!any_strictly_viable (candidates))
12231 : 13 : error ("no matching function for call to %<%s(%A)%>",
12232 : : pretty_name, arglist);
12233 : : else
12234 : 78 : error ("call of overloaded %<%s(%A)%> is ambiguous",
12235 : : pretty_name, arglist);
12236 : 91 : print_z_candidates (location_of (name), candidates);
12237 : 91 : if (free_p)
12238 : 0 : free (pretty_name);
12239 : 91 : }
12240 : 187 : call = error_mark_node;
12241 : 187 : if (fn_p)
12242 : 84 : *fn_p = error_mark_node;
12243 : : }
12244 : : else
12245 : : {
12246 : 82999807 : fn = cand->fn;
12247 : 82999807 : call = NULL_TREE;
12248 : :
12249 : 82999807 : if (!(flags & LOOKUP_NONVIRTUAL)
12250 : 60453016 : && DECL_PURE_VIRTUAL_P (fn)
12251 : 206088 : && instance == current_class_ref
12252 : 83161205 : && (complain & tf_warning))
12253 : : {
12254 : : /* This is not an error, it is runtime undefined
12255 : : behavior. */
12256 : 161398 : if (!current_function_decl)
12257 : 3 : warning (0, "pure virtual %q#D called from "
12258 : : "non-static data member initializer", fn);
12259 : 161395 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
12260 : 161395 : || DECL_DESTRUCTOR_P (current_function_decl))
12261 : 9 : warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
12262 : : ? G_("pure virtual %q#D called from constructor")
12263 : : : G_("pure virtual %q#D called from destructor")),
12264 : : fn);
12265 : : }
12266 : :
12267 : 98774594 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
12268 : 134452004 : && !DECL_CONSTRUCTOR_P (fn)
12269 : 130277406 : && is_dummy_object (instance))
12270 : : {
12271 : 22813 : instance = maybe_resolve_dummy (instance, true);
12272 : 22813 : if (instance == error_mark_node)
12273 : : call = error_mark_node;
12274 : 22813 : else if (!is_dummy_object (instance))
12275 : : {
12276 : : /* We captured 'this' in the current lambda now that
12277 : : we know we really need it. */
12278 : 22641 : cand->first_arg = instance;
12279 : : }
12280 : 172 : else if (current_class_ptr && any_dependent_bases_p ())
12281 : : /* We can't tell until instantiation time whether we can use
12282 : : *this as the implicit object argument. */;
12283 : : else
12284 : : {
12285 : 134 : if (complain & tf_error)
12286 : 60 : error ("cannot call member function %qD without object",
12287 : : fn);
12288 : 134 : call = error_mark_node;
12289 : : }
12290 : : }
12291 : :
12292 : 82999807 : if (call != error_mark_node)
12293 : : {
12294 : : /* Now we know what function is being called. */
12295 : 82999673 : if (fn_p)
12296 : 21765657 : *fn_p = fn;
12297 : : /* Build the actual CALL_EXPR. */
12298 : 82999673 : call = build_over_call (cand, flags, complain);
12299 : :
12300 : : /* Suppress warnings for if (my_struct.operator= (x)) where
12301 : : my_struct is implicitly converted to bool. */
12302 : 82999673 : if (TREE_CODE (call) == MODIFY_EXPR)
12303 : 1741901 : suppress_warning (call, OPT_Wparentheses);
12304 : :
12305 : : /* In an expression of the form `a->f()' where `f' turns
12306 : : out to be a static member function, `a' is
12307 : : none-the-less evaluated. */
12308 : 82999673 : if (!is_dummy_object (instance))
12309 : 61987219 : call = keep_unused_object_arg (call, instance, fn);
12310 : 82999673 : if (call != error_mark_node
12311 : 164487496 : && DECL_DESTRUCTOR_P (cand->fn)
12312 : 104509444 : && !VOID_TYPE_P (TREE_TYPE (call)))
12313 : : /* An explicit call of the form "x->~X()" has type
12314 : : "void". However, on platforms where destructors
12315 : : return "this" (i.e., those where
12316 : : targetm.cxx.cdtor_returns_this is true), such calls
12317 : : will appear to have a return value of pointer type
12318 : : to the low-level call machinery. We do not want to
12319 : : change the low-level machinery, since we want to be
12320 : : able to optimize "delete f()" on such platforms as
12321 : : "operator delete(~X(f()))" (rather than generating
12322 : : "t = f(), ~X(t), operator delete (t)"). */
12323 : 12649195 : call = build_nop (void_type_node, call);
12324 : : }
12325 : : }
12326 : : }
12327 : :
12328 : 83018266 : if (processing_template_decl && call != error_mark_node)
12329 : : {
12330 : 8055745 : bool cast_to_void = false;
12331 : :
12332 : 8055745 : if (TREE_CODE (call) == COMPOUND_EXPR)
12333 : 6 : call = TREE_OPERAND (call, 1);
12334 : 8055739 : else if (TREE_CODE (call) == NOP_EXPR)
12335 : : {
12336 : 0 : cast_to_void = true;
12337 : 0 : call = TREE_OPERAND (call, 0);
12338 : : }
12339 : 8055745 : if (INDIRECT_REF_P (call))
12340 : 515091 : call = TREE_OPERAND (call, 0);
12341 : :
12342 : : /* Prune all but the selected function from the original overload
12343 : : set so that we can avoid some duplicate work at instantiation time. */
12344 : 8055745 : if (really_overloaded_fn (fns))
12345 : : {
12346 : 3598331 : if (DECL_TEMPLATE_INFO (fn)
12347 : 3598331 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
12348 : : {
12349 : : /* Use the selected template, not the specialization, so that
12350 : : this looks like an actual lookup result for sake of
12351 : : filter_memfn_lookup. */
12352 : :
12353 : 2209422 : if (OVL_SINGLE_P (fns))
12354 : : /* If the original overload set consists of a single function
12355 : : template, this isn't beneficial. */
12356 : 2176441 : goto skip_prune;
12357 : :
12358 : 32981 : fn = ovl_make (DECL_TI_TEMPLATE (fn));
12359 : 32981 : if (template_only)
12360 : 30380 : fn = lookup_template_function (fn, explicit_targs);
12361 : : }
12362 : 1421890 : orig_fns = copy_node (orig_fns);
12363 : 1421890 : BASELINK_FUNCTIONS (orig_fns) = fn;
12364 : 1421890 : BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (orig_fns) = true;
12365 : : }
12366 : :
12367 : 4457414 : skip_prune:
12368 : 8055745 : call = (build_min_non_dep_call_vec
12369 : 8055745 : (call,
12370 : 8055745 : build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
12371 : : orig_instance, orig_fns, NULL_TREE),
12372 : : orig_args));
12373 : 8055745 : SET_EXPR_LOCATION (call, input_location);
12374 : 8055745 : call = convert_from_reference (call);
12375 : 8055745 : if (cast_to_void)
12376 : 0 : call = build_nop (void_type_node, call);
12377 : : }
12378 : :
12379 : 83018266 : if (orig_args != NULL)
12380 : 7953380 : release_tree_vector (orig_args);
12381 : :
12382 : : return call;
12383 : 84505358 : }
12384 : :
12385 : : /* Returns true iff standard conversion sequence ICS1 is a proper
12386 : : subsequence of ICS2. */
12387 : :
12388 : : static bool
12389 : 72776042 : is_subseq (conversion *ics1, conversion *ics2)
12390 : : {
12391 : : /* We can assume that a conversion of the same code
12392 : : between the same types indicates a subsequence since we only get
12393 : : here if the types we are converting from are the same. */
12394 : :
12395 : 72776042 : while (ics1->kind == ck_rvalue
12396 : 92436816 : || ics1->kind == ck_lvalue)
12397 : 19660774 : ics1 = next_conversion (ics1);
12398 : :
12399 : : while (1)
12400 : : {
12401 : 105351262 : while (ics2->kind == ck_rvalue
12402 : 105351262 : || ics2->kind == ck_lvalue)
12403 : 19660774 : ics2 = next_conversion (ics2);
12404 : :
12405 : 85690488 : if (ics2->kind == ck_user
12406 : 85690488 : || !has_next (ics2->kind))
12407 : : /* At this point, ICS1 cannot be a proper subsequence of
12408 : : ICS2. We can get a USER_CONV when we are comparing the
12409 : : second standard conversion sequence of two user conversion
12410 : : sequences. */
12411 : : return false;
12412 : :
12413 : 12921324 : ics2 = next_conversion (ics2);
12414 : :
12415 : 12921324 : while (ics2->kind == ck_rvalue
12416 : 21037014 : || ics2->kind == ck_lvalue)
12417 : 8115690 : ics2 = next_conversion (ics2);
12418 : :
12419 : 12921324 : if (ics2->kind == ics1->kind
12420 : 6923 : && same_type_p (ics2->type, ics1->type)
12421 : 12928202 : && (ics1->kind == ck_identity
12422 : 6878 : || same_type_p (next_conversion (ics2)->type,
12423 : : next_conversion (ics1)->type)))
12424 : 6878 : return true;
12425 : : }
12426 : : }
12427 : :
12428 : : /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
12429 : : be any _TYPE nodes. */
12430 : :
12431 : : bool
12432 : 290466338 : is_properly_derived_from (tree derived, tree base)
12433 : : {
12434 : 290466338 : if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
12435 : : return false;
12436 : :
12437 : : /* We only allow proper derivation here. The DERIVED_FROM_P macro
12438 : : considers every class derived from itself. */
12439 : 276394289 : return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
12440 : 276394289 : && DERIVED_FROM_P (base, derived));
12441 : : }
12442 : :
12443 : : /* We build the ICS for an implicit object parameter as a pointer
12444 : : conversion sequence. However, such a sequence should be compared
12445 : : as if it were a reference conversion sequence. If ICS is the
12446 : : implicit conversion sequence for an implicit object parameter,
12447 : : modify it accordingly. */
12448 : :
12449 : : static void
12450 : 155333824 : maybe_handle_implicit_object (conversion **ics)
12451 : : {
12452 : 155333824 : if ((*ics)->this_p)
12453 : : {
12454 : : /* [over.match.funcs]
12455 : :
12456 : : For non-static member functions, the type of the
12457 : : implicit object parameter is "reference to cv X"
12458 : : where X is the class of which the function is a
12459 : : member and cv is the cv-qualification on the member
12460 : : function declaration. */
12461 : 10312068 : conversion *t = *ics;
12462 : 10312068 : tree reference_type;
12463 : :
12464 : : /* The `this' parameter is a pointer to a class type. Make the
12465 : : implicit conversion talk about a reference to that same class
12466 : : type. */
12467 : 10312068 : reference_type = TREE_TYPE (t->type);
12468 : 10312068 : reference_type = build_reference_type (reference_type);
12469 : :
12470 : 10312068 : if (t->kind == ck_qual)
12471 : 2677318 : t = next_conversion (t);
12472 : 10312068 : if (t->kind == ck_ptr)
12473 : 430253 : t = next_conversion (t);
12474 : 10312068 : t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
12475 : 10312068 : t = direct_reference_binding (reference_type, t);
12476 : 10312068 : t->this_p = 1;
12477 : 10312068 : t->rvaluedness_matches_p = 0;
12478 : 10312068 : *ics = t;
12479 : : }
12480 : 155333824 : }
12481 : :
12482 : : /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
12483 : : and return the initial reference binding conversion. Otherwise,
12484 : : leave *ICS unchanged and return NULL. */
12485 : :
12486 : : static conversion *
12487 : 155333824 : maybe_handle_ref_bind (conversion **ics)
12488 : : {
12489 : 155333824 : if ((*ics)->kind == ck_ref_bind)
12490 : : {
12491 : 42116995 : conversion *old_ics = *ics;
12492 : 42116995 : *ics = next_conversion (old_ics);
12493 : 42116995 : (*ics)->user_conv_p = old_ics->user_conv_p;
12494 : 42116995 : return old_ics;
12495 : : }
12496 : :
12497 : : return NULL;
12498 : : }
12499 : :
12500 : : /* Get the expression at the beginning of the conversion chain C. */
12501 : :
12502 : : static tree
12503 : 51 : conv_get_original_expr (conversion *c)
12504 : : {
12505 : 60 : for (; c; c = next_conversion (c))
12506 : 60 : if (c->kind == ck_identity || c->kind == ck_ambig || c->kind == ck_aggr)
12507 : 51 : return c->u.expr;
12508 : : return NULL_TREE;
12509 : : }
12510 : :
12511 : : /* Return a tree representing the number of elements initialized by the
12512 : : list-initialization C. The caller must check that C converts to an
12513 : : array type. */
12514 : :
12515 : : static tree
12516 : 126 : nelts_initialized_by_list_init (conversion *c)
12517 : : {
12518 : : /* If the array we're converting to has a dimension, we'll use that. */
12519 : 126 : if (TYPE_DOMAIN (c->type))
12520 : 84 : return array_type_nelts_top (c->type);
12521 : : else
12522 : : {
12523 : : /* Otherwise, we look at how many elements the constructor we're
12524 : : initializing from has. */
12525 : 42 : tree ctor = conv_get_original_expr (c);
12526 : 72 : return size_int (CONSTRUCTOR_NELTS (ctor));
12527 : : }
12528 : : }
12529 : :
12530 : : /* True iff C is a conversion that binds a reference or a pointer to
12531 : : an array of unknown bound. */
12532 : :
12533 : : static inline bool
12534 : 20877100 : conv_binds_to_array_of_unknown_bound (conversion *c)
12535 : : {
12536 : : /* ck_ref_bind won't have the reference stripped. */
12537 : 20877100 : tree type = non_reference (c->type);
12538 : : /* ck_qual won't have the pointer stripped. */
12539 : 20877100 : type = strip_pointer_operator (type);
12540 : 20877100 : return (TREE_CODE (type) == ARRAY_TYPE
12541 : 20877100 : && TYPE_DOMAIN (type) == NULL_TREE);
12542 : : }
12543 : :
12544 : : /* Compare two implicit conversion sequences according to the rules set out in
12545 : : [over.ics.rank]. Return values:
12546 : :
12547 : : 1: ics1 is better than ics2
12548 : : -1: ics2 is better than ics1
12549 : : 0: ics1 and ics2 are indistinguishable */
12550 : :
12551 : : static int
12552 : 77666959 : compare_ics (conversion *ics1, conversion *ics2)
12553 : : {
12554 : 77666959 : tree from_type1;
12555 : 77666959 : tree from_type2;
12556 : 77666959 : tree to_type1;
12557 : 77666959 : tree to_type2;
12558 : 77666959 : tree deref_from_type1 = NULL_TREE;
12559 : 77666959 : tree deref_from_type2 = NULL_TREE;
12560 : 77666959 : tree deref_to_type1 = NULL_TREE;
12561 : 77666959 : tree deref_to_type2 = NULL_TREE;
12562 : 77666959 : conversion_rank rank1, rank2;
12563 : :
12564 : : /* REF_BINDING is nonzero if the result of the conversion sequence
12565 : : is a reference type. In that case REF_CONV is the reference
12566 : : binding conversion. */
12567 : 77666959 : conversion *ref_conv1;
12568 : 77666959 : conversion *ref_conv2;
12569 : :
12570 : : /* Compare badness before stripping the reference conversion. */
12571 : 77666959 : if (ics1->bad_p > ics2->bad_p)
12572 : : return -1;
12573 : 77666942 : else if (ics1->bad_p < ics2->bad_p)
12574 : : return 1;
12575 : :
12576 : : /* Handle implicit object parameters. */
12577 : 77666912 : maybe_handle_implicit_object (&ics1);
12578 : 77666912 : maybe_handle_implicit_object (&ics2);
12579 : :
12580 : : /* Handle reference parameters. */
12581 : 77666912 : ref_conv1 = maybe_handle_ref_bind (&ics1);
12582 : 77666912 : ref_conv2 = maybe_handle_ref_bind (&ics2);
12583 : :
12584 : : /* List-initialization sequence L1 is a better conversion sequence than
12585 : : list-initialization sequence L2 if L1 converts to
12586 : : std::initializer_list<X> for some X and L2 does not. */
12587 : 77666912 : if (ics1->kind == ck_list && ics2->kind != ck_list)
12588 : : return 1;
12589 : 77666348 : if (ics2->kind == ck_list && ics1->kind != ck_list)
12590 : : return -1;
12591 : :
12592 : : /* [over.ics.rank]
12593 : :
12594 : : When comparing the basic forms of implicit conversion sequences (as
12595 : : defined in _over.best.ics_)
12596 : :
12597 : : --a standard conversion sequence (_over.ics.scs_) is a better
12598 : : conversion sequence than a user-defined conversion sequence
12599 : : or an ellipsis conversion sequence, and
12600 : :
12601 : : --a user-defined conversion sequence (_over.ics.user_) is a
12602 : : better conversion sequence than an ellipsis conversion sequence
12603 : : (_over.ics.ellipsis_). */
12604 : : /* Use BAD_CONVERSION_RANK because we already checked for a badness
12605 : : mismatch. If both ICS are bad, we try to make a decision based on
12606 : : what would have happened if they'd been good. This is not an
12607 : : extension, we'll still give an error when we build up the call; this
12608 : : just helps us give a more helpful error message. */
12609 : 77666064 : rank1 = BAD_CONVERSION_RANK (ics1);
12610 : 77666064 : rank2 = BAD_CONVERSION_RANK (ics2);
12611 : :
12612 : 77666064 : if (rank1 > rank2)
12613 : : return -1;
12614 : 67358005 : else if (rank1 < rank2)
12615 : : return 1;
12616 : :
12617 : 36716220 : if (ics1->ellipsis_p)
12618 : : /* Both conversions are ellipsis conversions. */
12619 : : return 0;
12620 : :
12621 : : /* User-defined conversion sequence U1 is a better conversion sequence
12622 : : than another user-defined conversion sequence U2 if they contain the
12623 : : same user-defined conversion operator or constructor and if the sec-
12624 : : ond standard conversion sequence of U1 is better than the second
12625 : : standard conversion sequence of U2. */
12626 : :
12627 : : /* Handle list-conversion with the same code even though it isn't always
12628 : : ranked as a user-defined conversion and it doesn't have a second
12629 : : standard conversion sequence; it will still have the desired effect.
12630 : : Specifically, we need to do the reference binding comparison at the
12631 : : end of this function. */
12632 : :
12633 : 36716168 : if (ics1->user_conv_p || ics1->kind == ck_list
12634 : 36319292 : || ics1->kind == ck_aggr || ics2->kind == ck_aggr)
12635 : : {
12636 : 396945 : conversion *t1 = strip_standard_conversion (ics1);
12637 : 396945 : conversion *t2 = strip_standard_conversion (ics2);
12638 : :
12639 : 396945 : if (!t1 || !t2 || t1->kind != t2->kind)
12640 : : return 0;
12641 : 396926 : else if (t1->kind == ck_user)
12642 : : {
12643 : 385460 : tree f1 = t1->cand ? t1->cand->fn : t1->type;
12644 : 385460 : tree f2 = t2->cand ? t2->cand->fn : t2->type;
12645 : 385460 : if (f1 != f2)
12646 : : return 0;
12647 : : }
12648 : : /* List-initialization sequence L1 is a better conversion sequence than
12649 : : list-initialization sequence L2 if
12650 : :
12651 : : -- L1 and L2 convert to arrays of the same element type, and either
12652 : : the number of elements n1 initialized by L1 is less than the number
12653 : : of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
12654 : : of unknown bound and L1 does not. (Added in CWG 1307 and extended by
12655 : : P0388R4.) */
12656 : 11466 : else if (t1->kind == ck_aggr
12657 : 10805 : && TREE_CODE (t1->type) == ARRAY_TYPE
12658 : 66 : && TREE_CODE (t2->type) == ARRAY_TYPE
12659 : 11532 : && same_type_p (TREE_TYPE (t1->type), TREE_TYPE (t2->type)))
12660 : : {
12661 : 63 : tree n1 = nelts_initialized_by_list_init (t1);
12662 : 63 : tree n2 = nelts_initialized_by_list_init (t2);
12663 : 63 : if (tree_int_cst_lt (n1, n2))
12664 : : return 1;
12665 : 24 : else if (tree_int_cst_lt (n2, n1))
12666 : : return -1;
12667 : : /* The n1 == n2 case. */
12668 : 24 : bool c1 = conv_binds_to_array_of_unknown_bound (t1);
12669 : 24 : bool c2 = conv_binds_to_array_of_unknown_bound (t2);
12670 : 24 : if (c1 && !c2)
12671 : : return -1;
12672 : 6 : else if (!c1 && c2)
12673 : : return 1;
12674 : : else
12675 : : return 0;
12676 : : }
12677 : : else
12678 : : {
12679 : : /* For ambiguous or aggregate conversions, use the target type as
12680 : : a proxy for the conversion function. */
12681 : 11403 : if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
12682 : : return 0;
12683 : : }
12684 : :
12685 : : /* We can just fall through here, after setting up
12686 : : FROM_TYPE1 and FROM_TYPE2. */
12687 : 394105 : from_type1 = t1->type;
12688 : 394105 : from_type2 = t2->type;
12689 : 394105 : }
12690 : : else
12691 : : {
12692 : : conversion *t1;
12693 : : conversion *t2;
12694 : :
12695 : : /* We're dealing with two standard conversion sequences.
12696 : :
12697 : : [over.ics.rank]
12698 : :
12699 : : Standard conversion sequence S1 is a better conversion
12700 : : sequence than standard conversion sequence S2 if
12701 : :
12702 : : --S1 is a proper subsequence of S2 (comparing the conversion
12703 : : sequences in the canonical form defined by _over.ics.scs_,
12704 : : excluding any Lvalue Transformation; the identity
12705 : : conversion sequence is considered to be a subsequence of
12706 : : any non-identity conversion sequence */
12707 : :
12708 : : t1 = ics1;
12709 : 56730423 : while (t1->kind != ck_identity)
12710 : 20411200 : t1 = next_conversion (t1);
12711 : 36319223 : from_type1 = t1->type;
12712 : :
12713 : 36319223 : t2 = ics2;
12714 : 56673922 : while (t2->kind != ck_identity)
12715 : 20354699 : t2 = next_conversion (t2);
12716 : 36319223 : from_type2 = t2->type;
12717 : : }
12718 : :
12719 : : /* One sequence can only be a subsequence of the other if they start with
12720 : : the same type. They can start with different types when comparing the
12721 : : second standard conversion sequence in two user-defined conversion
12722 : : sequences. */
12723 : 36713328 : if (same_type_p (from_type1, from_type2))
12724 : : {
12725 : 36391430 : if (is_subseq (ics1, ics2))
12726 : : return 1;
12727 : 36384612 : if (is_subseq (ics2, ics1))
12728 : : return -1;
12729 : : }
12730 : :
12731 : : /* [over.ics.rank]
12732 : :
12733 : : Or, if not that,
12734 : :
12735 : : --the rank of S1 is better than the rank of S2 (by the rules
12736 : : defined below):
12737 : :
12738 : : Standard conversion sequences are ordered by their ranks: an Exact
12739 : : Match is a better conversion than a Promotion, which is a better
12740 : : conversion than a Conversion.
12741 : :
12742 : : Two conversion sequences with the same rank are indistinguishable
12743 : : unless one of the following rules applies:
12744 : :
12745 : : --A conversion that does not a convert a pointer, pointer to member,
12746 : : or std::nullptr_t to bool is better than one that does.
12747 : :
12748 : : The ICS_STD_RANK automatically handles the pointer-to-bool rule,
12749 : : so that we do not have to check it explicitly. */
12750 : 36706450 : if (ics1->rank < ics2->rank)
12751 : : return 1;
12752 : 36706370 : else if (ics2->rank < ics1->rank)
12753 : : return -1;
12754 : :
12755 : 36706370 : to_type1 = ics1->type;
12756 : 36706370 : to_type2 = ics2->type;
12757 : :
12758 : : /* A conversion from scalar arithmetic type to complex is worse than a
12759 : : conversion between scalar arithmetic types. */
12760 : 36706370 : if (same_type_p (from_type1, from_type2)
12761 : 36384472 : && ARITHMETIC_TYPE_P (from_type1)
12762 : 9593751 : && ARITHMETIC_TYPE_P (to_type1)
12763 : 9593620 : && ARITHMETIC_TYPE_P (to_type2)
12764 : 36706370 : && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
12765 : 9593580 : != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
12766 : : {
12767 : 106 : if (TREE_CODE (to_type1) == COMPLEX_TYPE)
12768 : : return -1;
12769 : : else
12770 : : return 1;
12771 : : }
12772 : :
12773 : 36706264 : {
12774 : : /* A conversion in either direction between floating-point type FP1 and
12775 : : floating-point type FP2 is better than a conversion in the same
12776 : : direction between FP1 and arithmetic type T3 if
12777 : : - the floating-point conversion rank of FP1 is equal to the rank of
12778 : : FP2, and
12779 : : - T3 is not a floating-point type, or T3 is a floating-point type
12780 : : whose rank is not equal to the rank of FP1, or the floating-point
12781 : : conversion subrank of FP2 is greater than the subrank of T3. */
12782 : 36706264 : tree fp1 = from_type1;
12783 : 36706264 : tree fp2 = to_type1;
12784 : 36706264 : tree fp3 = from_type2;
12785 : 36706264 : tree t3 = to_type2;
12786 : 36706264 : int ret = 1;
12787 : 36706264 : if (TYPE_MAIN_VARIANT (fp2) == TYPE_MAIN_VARIANT (t3))
12788 : : {
12789 : 31229087 : std::swap (fp1, fp2);
12790 : 31229087 : std::swap (fp3, t3);
12791 : : }
12792 : 36706264 : if (TYPE_MAIN_VARIANT (fp1) == TYPE_MAIN_VARIANT (fp3)
12793 : 36706161 : && SCALAR_FLOAT_TYPE_P (fp1)
12794 : : /* Only apply this rule if at least one of the 3 types is
12795 : : extended floating-point type, otherwise keep them as
12796 : : before for compatibility reasons with types like __float128.
12797 : : float, double and long double alone have different conversion
12798 : : ranks and so when just those 3 types are involved, this
12799 : : rule doesn't trigger. */
12800 : 41313602 : && (extended_float_type_p (fp1)
12801 : 4497577 : || (SCALAR_FLOAT_TYPE_P (fp2) && extended_float_type_p (fp2))
12802 : 4312503 : || (SCALAR_FLOAT_TYPE_P (t3) && extended_float_type_p (t3))))
12803 : : {
12804 : 294835 : if (TREE_CODE (fp2) != REAL_TYPE)
12805 : : {
12806 : 49930 : ret = -ret;
12807 : 49930 : std::swap (fp2, t3);
12808 : : }
12809 : 294835 : if (SCALAR_FLOAT_TYPE_P (fp2))
12810 : : {
12811 : : /* cp_compare_floating_point_conversion_ranks returns -1, 0 or 1
12812 : : if the conversion rank is equal (-1 or 1 if the subrank is
12813 : : different). */
12814 : 267575 : if (IN_RANGE (cp_compare_floating_point_conversion_ranks (fp1,
12815 : : fp2),
12816 : : -1, 1))
12817 : : {
12818 : : /* Conversion ranks of FP1 and FP2 are equal. */
12819 : 181244 : if (TREE_CODE (t3) != REAL_TYPE
12820 : 181244 : || !IN_RANGE (cp_compare_floating_point_conversion_ranks
12821 : : (fp1, t3),
12822 : : -1, 1))
12823 : : /* FP1 <-> FP2 conversion is better. */
12824 : 180774 : return ret;
12825 : 470 : int c = cp_compare_floating_point_conversion_ranks (fp2, t3);
12826 : 470 : gcc_assert (IN_RANGE (c, -1, 1));
12827 : 470 : if (c == 1)
12828 : : /* Conversion subrank of FP2 is greater than subrank of T3.
12829 : : FP1 <-> FP2 conversion is better. */
12830 : : return ret;
12831 : 470 : else if (c == -1)
12832 : : /* Conversion subrank of FP2 is less than subrank of T3.
12833 : : FP1 <-> T3 conversion is better. */
12834 : 0 : return -ret;
12835 : : }
12836 : 86331 : else if (SCALAR_FLOAT_TYPE_P (t3)
12837 : 86331 : && IN_RANGE (cp_compare_floating_point_conversion_ranks
12838 : : (fp1, t3),
12839 : : -1, 1))
12840 : : /* Conversion ranks of FP1 and FP2 are not equal, conversion
12841 : : ranks of FP1 and T3 are equal.
12842 : : FP1 <-> T3 conversion is better. */
12843 : 14804 : return -ret;
12844 : : }
12845 : : }
12846 : : }
12847 : :
12848 : 36510686 : if (TYPE_PTR_P (from_type1)
12849 : 7678169 : && TYPE_PTR_P (from_type2)
12850 : 7678153 : && TYPE_PTR_P (to_type1)
12851 : 7678129 : && TYPE_PTR_P (to_type2))
12852 : : {
12853 : 7678129 : deref_from_type1 = TREE_TYPE (from_type1);
12854 : 7678129 : deref_from_type2 = TREE_TYPE (from_type2);
12855 : 7678129 : deref_to_type1 = TREE_TYPE (to_type1);
12856 : 7678129 : deref_to_type2 = TREE_TYPE (to_type2);
12857 : : }
12858 : : /* The rules for pointers to members A::* are just like the rules
12859 : : for pointers A*, except opposite: if B is derived from A then
12860 : : A::* converts to B::*, not vice versa. For that reason, we
12861 : : switch the from_ and to_ variables here. */
12862 : 52 : else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
12863 : 52 : && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
12864 : 28832557 : || (TYPE_PTRMEMFUNC_P (from_type1)
12865 : 433 : && TYPE_PTRMEMFUNC_P (from_type2)
12866 : 433 : && TYPE_PTRMEMFUNC_P (to_type1)
12867 : 433 : && TYPE_PTRMEMFUNC_P (to_type2)))
12868 : : {
12869 : 485 : deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
12870 : 485 : deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
12871 : 485 : deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
12872 : 485 : deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
12873 : : }
12874 : :
12875 : 7678614 : if (deref_from_type1 != NULL_TREE
12876 : 7678614 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
12877 : 186626 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
12878 : : {
12879 : : /* This was one of the pointer or pointer-like conversions.
12880 : :
12881 : : [over.ics.rank]
12882 : :
12883 : : --If class B is derived directly or indirectly from class A,
12884 : : conversion of B* to A* is better than conversion of B* to
12885 : : void*, and conversion of A* to void* is better than
12886 : : conversion of B* to void*. */
12887 : 186626 : if (VOID_TYPE_P (deref_to_type1)
12888 : 57 : && VOID_TYPE_P (deref_to_type2))
12889 : : {
12890 : 14 : if (is_properly_derived_from (deref_from_type1,
12891 : : deref_from_type2))
12892 : : return -1;
12893 : 14 : else if (is_properly_derived_from (deref_from_type2,
12894 : : deref_from_type1))
12895 : : return 1;
12896 : : }
12897 : 186612 : else if (VOID_TYPE_P (deref_to_type1)
12898 : 186569 : || VOID_TYPE_P (deref_to_type2))
12899 : : {
12900 : 47 : if (same_type_p (deref_from_type1, deref_from_type2))
12901 : : {
12902 : 47 : if (VOID_TYPE_P (deref_to_type2))
12903 : : {
12904 : 4 : if (is_properly_derived_from (deref_from_type1,
12905 : : deref_to_type1))
12906 : : return 1;
12907 : : }
12908 : : /* We know that DEREF_TO_TYPE1 is `void' here. */
12909 : 43 : else if (is_properly_derived_from (deref_from_type1,
12910 : : deref_to_type2))
12911 : : return -1;
12912 : : }
12913 : : }
12914 : 186565 : else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
12915 : 186565 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
12916 : : {
12917 : : /* [over.ics.rank]
12918 : :
12919 : : --If class B is derived directly or indirectly from class A
12920 : : and class C is derived directly or indirectly from B,
12921 : :
12922 : : --conversion of C* to B* is better than conversion of C* to
12923 : : A*,
12924 : :
12925 : : --conversion of B* to A* is better than conversion of C* to
12926 : : A* */
12927 : 186565 : if (same_type_p (deref_from_type1, deref_from_type2))
12928 : : {
12929 : 186559 : if (is_properly_derived_from (deref_to_type1,
12930 : : deref_to_type2))
12931 : : return 1;
12932 : 186559 : else if (is_properly_derived_from (deref_to_type2,
12933 : : deref_to_type1))
12934 : : return -1;
12935 : : }
12936 : 6 : else if (same_type_p (deref_to_type1, deref_to_type2))
12937 : : {
12938 : 6 : if (is_properly_derived_from (deref_from_type2,
12939 : : deref_from_type1))
12940 : : return 1;
12941 : 0 : else if (is_properly_derived_from (deref_from_type1,
12942 : : deref_from_type2))
12943 : : return -1;
12944 : : }
12945 : : }
12946 : : }
12947 : 72648120 : else if (CLASS_TYPE_P (non_reference (from_type1))
12948 : 53915972 : && same_type_p (from_type1, from_type2))
12949 : : {
12950 : 17310621 : tree from = non_reference (from_type1);
12951 : :
12952 : : /* [over.ics.rank]
12953 : :
12954 : : --binding of an expression of type C to a reference of type
12955 : : B& is better than binding an expression of type C to a
12956 : : reference of type A&
12957 : :
12958 : : --conversion of C to B is better than conversion of C to A, */
12959 : 17310621 : if (is_properly_derived_from (from, to_type1)
12960 : 17310621 : && is_properly_derived_from (from, to_type2))
12961 : : {
12962 : 800131 : if (is_properly_derived_from (to_type1, to_type2))
12963 : : return 1;
12964 : 797885 : else if (is_properly_derived_from (to_type2, to_type1))
12965 : : return -1;
12966 : : }
12967 : : }
12968 : 38026878 : else if (CLASS_TYPE_P (non_reference (to_type1))
12969 : 19294730 : && same_type_p (to_type1, to_type2))
12970 : : {
12971 : 5 : tree to = non_reference (to_type1);
12972 : :
12973 : : /* [over.ics.rank]
12974 : :
12975 : : --binding of an expression of type B to a reference of type
12976 : : A& is better than binding an expression of type C to a
12977 : : reference of type A&,
12978 : :
12979 : : --conversion of B to A is better than conversion of C to A */
12980 : 5 : if (is_properly_derived_from (from_type1, to)
12981 : 5 : && is_properly_derived_from (from_type2, to))
12982 : : {
12983 : 3 : if (is_properly_derived_from (from_type2, from_type1))
12984 : : return 1;
12985 : 3 : else if (is_properly_derived_from (from_type1, from_type2))
12986 : : return -1;
12987 : : }
12988 : : }
12989 : :
12990 : : /* [over.ics.rank]
12991 : :
12992 : : --S1 and S2 differ only in their qualification conversion and yield
12993 : : similar types T1 and T2 (_conv.qual_), respectively, and the cv-
12994 : : qualification signature of type T1 is a proper subset of the cv-
12995 : : qualification signature of type T2 */
12996 : 36454625 : if (ics1->kind == ck_qual
12997 : 381 : && ics2->kind == ck_qual
12998 : 36455006 : && same_type_p (from_type1, from_type2))
12999 : : {
13000 : 381 : int result = comp_cv_qual_signature (to_type1, to_type2);
13001 : 381 : if (result != 0)
13002 : : return result;
13003 : : }
13004 : :
13005 : : /* [over.ics.rank]
13006 : :
13007 : : --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
13008 : : to an implicit object parameter of a non-static member function
13009 : : declared without a ref-qualifier, and either S1 binds an lvalue
13010 : : reference to an lvalue and S2 binds an rvalue reference or S1 binds an
13011 : : rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
13012 : : draft standard, 13.3.3.2)
13013 : :
13014 : : --S1 and S2 are reference bindings (_dcl.init.ref_), and the
13015 : : types to which the references refer are the same type except for
13016 : : top-level cv-qualifiers, and the type to which the reference
13017 : : initialized by S2 refers is more cv-qualified than the type to
13018 : : which the reference initialized by S1 refers.
13019 : :
13020 : : DR 1328 [over.match.best]: the context is an initialization by
13021 : : conversion function for direct reference binding (13.3.1.6) of a
13022 : : reference to function type, the return type of F1 is the same kind of
13023 : : reference (i.e. lvalue or rvalue) as the reference being initialized,
13024 : : and the return type of F2 is not. */
13025 : :
13026 : 36454527 : if (ref_conv1 && ref_conv2)
13027 : : {
13028 : 12375575 : if (!ref_conv1->this_p && !ref_conv2->this_p
13029 : 12213433 : && (ref_conv1->rvaluedness_matches_p
13030 : 12213433 : != ref_conv2->rvaluedness_matches_p)
13031 : 24502159 : && (same_type_p (ref_conv1->type, ref_conv2->type)
13032 : 7031927 : || (TYPE_REF_IS_RVALUE (ref_conv1->type)
13033 : 7031927 : != TYPE_REF_IS_RVALUE (ref_conv2->type))))
13034 : : {
13035 : 7029742 : if (ref_conv1->bad_p
13036 : 7029742 : && !same_type_p (TREE_TYPE (ref_conv1->type),
13037 : : TREE_TYPE (ref_conv2->type)))
13038 : : /* Don't prefer a bad conversion that drops cv-quals to a bad
13039 : : conversion with the wrong rvalueness. */
13040 : : return 0;
13041 : 7028283 : return (ref_conv1->rvaluedness_matches_p
13042 : 7028283 : - ref_conv2->rvaluedness_matches_p);
13043 : : }
13044 : :
13045 : 10440487 : if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
13046 : : {
13047 : : /* Per P0388R4:
13048 : :
13049 : : void f (int(&)[]), // (1)
13050 : : f (int(&)[1]), // (2)
13051 : : f (int*); // (3)
13052 : :
13053 : : (2) is better than (1), but (3) should be equal to (1) and to
13054 : : (2). For that reason we don't use ck_qual for (1) which would
13055 : : give it the cr_exact rank while (3) remains ck_identity.
13056 : : Therefore we compare (1) and (2) here. For (1) we'll have
13057 : :
13058 : : ck_ref_bind <- ck_identity
13059 : : int[] & int[1]
13060 : :
13061 : : so to handle this we must look at ref_conv. */
13062 : 10438143 : bool c1 = conv_binds_to_array_of_unknown_bound (ref_conv1);
13063 : 10438143 : bool c2 = conv_binds_to_array_of_unknown_bound (ref_conv2);
13064 : 10438143 : if (c1 && !c2)
13065 : : return -1;
13066 : 10438137 : else if (!c1 && c2)
13067 : : return 1;
13068 : :
13069 : 10438137 : int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
13070 : 10438137 : int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
13071 : 10438137 : if (ref_conv1->bad_p)
13072 : : {
13073 : : /* Prefer the one that drops fewer cv-quals. */
13074 : 1606 : tree ftype = next_conversion (ref_conv1)->type;
13075 : 1606 : int fquals = cp_type_quals (ftype);
13076 : 1606 : q1 ^= fquals;
13077 : 1606 : q2 ^= fquals;
13078 : : }
13079 : 10438137 : return comp_cv_qualification (q2, q1);
13080 : : }
13081 : : }
13082 : :
13083 : : /* [over.ics.rank]
13084 : :
13085 : : Per CWG 1601:
13086 : : -- A conversion that promotes an enumeration whose underlying type
13087 : : is fixed to its underlying type is better than one that promotes to
13088 : : the promoted underlying type, if the two are different. */
13089 : 18986642 : if (ics1->rank == cr_promotion
13090 : 142 : && ics2->rank == cr_promotion
13091 : 142 : && UNSCOPED_ENUM_P (from_type1)
13092 : 27 : && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1)
13093 : 18986666 : && same_type_p (from_type1, from_type2))
13094 : : {
13095 : 24 : tree utype = ENUM_UNDERLYING_TYPE (from_type1);
13096 : 24 : tree prom = type_promotes_to (from_type1);
13097 : 24 : if (!same_type_p (utype, prom))
13098 : : {
13099 : 12 : if (same_type_p (to_type1, utype)
13100 : 12 : && same_type_p (to_type2, prom))
13101 : : return 1;
13102 : 6 : else if (same_type_p (to_type2, utype)
13103 : 6 : && same_type_p (to_type1, prom))
13104 : : return -1;
13105 : : }
13106 : : }
13107 : :
13108 : : /* Neither conversion sequence is better than the other. */
13109 : : return 0;
13110 : : }
13111 : :
13112 : : /* The source type for this standard conversion sequence. */
13113 : :
13114 : : static tree
13115 : 9 : source_type (conversion *t)
13116 : : {
13117 : 9 : return strip_standard_conversion (t)->type;
13118 : : }
13119 : :
13120 : : /* Note a warning about preferring WINNER to LOSER. We do this by storing
13121 : : a pointer to LOSER and re-running joust to produce the warning if WINNER
13122 : : is actually used. */
13123 : :
13124 : : static void
13125 : 201 : add_warning (struct z_candidate *winner, struct z_candidate *loser)
13126 : : {
13127 : 201 : candidate_warning *cw = (candidate_warning *)
13128 : 0 : conversion_obstack_alloc (sizeof (candidate_warning));
13129 : 201 : cw->loser = loser;
13130 : 201 : cw->next = winner->warnings;
13131 : 201 : winner->warnings = cw;
13132 : 201 : }
13133 : :
13134 : : /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
13135 : : prvalue returned from a conversion function, return true. Otherwise, return
13136 : : false. */
13137 : :
13138 : : static bool
13139 : 601710 : joust_maybe_elide_copy (z_candidate *cand)
13140 : : {
13141 : 601710 : tree fn = cand->fn;
13142 : 1511119 : if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
13143 : 307617 : return false;
13144 : 294093 : conversion *conv = cand->convs[0];
13145 : 294093 : if (conv->kind == ck_ambig)
13146 : : return false;
13147 : 294091 : gcc_checking_assert (conv->kind == ck_ref_bind);
13148 : 294091 : conv = next_conversion (conv);
13149 : 294091 : if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
13150 : : {
13151 : 99 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
13152 : : (conv->type, DECL_CONTEXT (fn)));
13153 : 99 : z_candidate *uc = conv->cand;
13154 : 99 : if (DECL_CONV_FN_P (uc->fn))
13155 : : return true;
13156 : : }
13157 : : return false;
13158 : : }
13159 : :
13160 : : /* Return the class that CAND's implicit object parameter refers to. */
13161 : :
13162 : : static tree
13163 : 78161 : class_of_implicit_object (z_candidate *cand)
13164 : : {
13165 : 78161 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (cand->fn))
13166 : : return NULL_TREE;
13167 : :
13168 : : /* "For conversion functions that are implicit object member functions,
13169 : : the function is considered to be a member of the class of the implied
13170 : : object argument for the purpose of defining the type of the implicit
13171 : : object parameter." */
13172 : 78161 : if (DECL_CONV_FN_P (cand->fn))
13173 : 0 : return TYPE_MAIN_VARIANT (TREE_TYPE (cand->first_arg));
13174 : :
13175 : : /* "For non-conversion functions that are implicit object member
13176 : : functions nominated by a using-declaration in a derived class, the
13177 : : function is considered to be a member of the derived class for the
13178 : : purpose of defining the type of the implicit object parameter."
13179 : :
13180 : : That derived class is reflected in the conversion_path binfo. */
13181 : 78161 : return BINFO_TYPE (cand->conversion_path);
13182 : : }
13183 : :
13184 : : /* Return whether the first parameter of C1 matches the second parameter
13185 : : of C2. */
13186 : :
13187 : : static bool
13188 : 151873 : reversed_match (z_candidate *c1, z_candidate *c2)
13189 : : {
13190 : 151873 : tree fn1 = c1->fn;
13191 : 151873 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (c2->fn));
13192 : 151873 : tree parm2 = TREE_VALUE (TREE_CHAIN (parms2));
13193 : 151873 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn1))
13194 : : {
13195 : 78161 : tree ctx = class_of_implicit_object (c1);
13196 : 78161 : return iobj_parm_corresponds_to (fn1, parm2, ctx);
13197 : : }
13198 : : else
13199 : : {
13200 : 73712 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13201 : 73712 : tree parm1 = TREE_VALUE (parms1);
13202 : 73712 : return same_type_p (parm1, parm2);
13203 : : }
13204 : : }
13205 : :
13206 : : /* True if the defining declarations of the two candidates have equivalent
13207 : : parameters. MATCH_KIND controls whether we're trying to compare the
13208 : : original declarations (for a warning) or the actual candidates. */
13209 : :
13210 : : enum class pmatch { original, current };
13211 : :
13212 : : static bool
13213 : 1951313 : cand_parms_match (z_candidate *c1, z_candidate *c2, pmatch match_kind)
13214 : : {
13215 : 1951313 : tree fn1 = c1->fn;
13216 : 1951313 : tree fn2 = c2->fn;
13217 : 1951313 : bool reversed = (match_kind == pmatch::current
13218 : 1951313 : && c1->reversed () != c2->reversed ());
13219 : 1951313 : if (fn1 == fn2 && !reversed)
13220 : : return true;
13221 : 1951272 : if (identifier_p (fn1) || identifier_p (fn2))
13222 : : return false;
13223 : 1951268 : if (match_kind == pmatch::original)
13224 : : {
13225 : : /* We don't look at c1->template_decl because that's only set for
13226 : : primary templates, not e.g. non-template member functions of
13227 : : class templates. */
13228 : 13 : tree t1 = most_general_template (fn1);
13229 : 13 : tree t2 = most_general_template (fn2);
13230 : 13 : if (t1 || t2)
13231 : : {
13232 : 8 : if (!t1 || !t2)
13233 : : return false;
13234 : 8 : if (t1 == t2)
13235 : : return true;
13236 : 0 : fn1 = DECL_TEMPLATE_RESULT (t1);
13237 : 0 : fn2 = DECL_TEMPLATE_RESULT (t2);
13238 : : }
13239 : : }
13240 : :
13241 : 1951260 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13242 : 1951260 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (fn2));
13243 : :
13244 : 3844255 : if (DECL_FUNCTION_MEMBER_P (fn1)
13245 : 1951657 : && DECL_FUNCTION_MEMBER_P (fn2))
13246 : : {
13247 : 58634 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (fn1));
13248 : 58634 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (fn2));
13249 : 58634 : if (base1 != base2)
13250 : 39670 : return false;
13251 : :
13252 : 58520 : if (reversed)
13253 : 39554 : return (reversed_match (c1, c2)
13254 : 39554 : && reversed_match (c2, c1));
13255 : :
13256 : : /* Use object_parms_correspond to simplify comparing iobj/xobj/static
13257 : : member functions. */
13258 : 18966 : if (!object_parms_correspond (fn1, fn2, base1))
13259 : : return false;
13260 : :
13261 : : /* We just compared the object parameters, if they don't correspond
13262 : : we already returned false. */
13263 : 56892 : auto skip_parms = [] (tree fn, tree parms)
13264 : : {
13265 : 37928 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
13266 : 680 : return TREE_CHAIN (parms);
13267 : : else
13268 : 37248 : return skip_artificial_parms_for (fn, parms);
13269 : : };
13270 : 18964 : parms1 = skip_parms (fn1, parms1);
13271 : 18964 : parms2 = skip_parms (fn2, parms2);
13272 : : }
13273 : 1892626 : else if (reversed)
13274 : 36838 : return (reversed_match (c1, c2)
13275 : 36838 : && reversed_match (c2, c1));
13276 : 1874752 : return compparms (parms1, parms2);
13277 : : }
13278 : :
13279 : : /* True iff FN is a copy or move constructor or assignment operator. */
13280 : :
13281 : : static bool
13282 : 18164460 : sfk_copy_or_move (tree fn)
13283 : : {
13284 : 18164460 : if (TREE_CODE (fn) != FUNCTION_DECL)
13285 : : return false;
13286 : 18164377 : special_function_kind sfk = special_function_p (fn);
13287 : 18164377 : return sfk >= sfk_copy_constructor && sfk <= sfk_move_assignment;
13288 : : }
13289 : :
13290 : : /* Compare two candidates for overloading as described in
13291 : : [over.match.best]. Return values:
13292 : :
13293 : : 1: cand1 is better than cand2
13294 : : -1: cand2 is better than cand1
13295 : : 0: cand1 and cand2 are indistinguishable */
13296 : :
13297 : : static int
13298 : 69096568 : joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
13299 : : tsubst_flags_t complain)
13300 : : {
13301 : 69096568 : int winner = 0;
13302 : 69096568 : int off1 = 0, off2 = 0;
13303 : 69096568 : size_t i;
13304 : 69096568 : size_t len;
13305 : :
13306 : : /* Candidates that involve bad conversions are always worse than those
13307 : : that don't. */
13308 : 69096568 : if (cand1->viable > cand2->viable)
13309 : : return 1;
13310 : 55999037 : if (cand1->viable < cand2->viable)
13311 : : return -1;
13312 : :
13313 : : /* If we have two pseudo-candidates for conversions to the same type,
13314 : : or two candidates for the same function, arbitrarily pick one. */
13315 : 55999037 : if (cand1->fn == cand2->fn
13316 : 1635316 : && cand1->reversed () == cand2->reversed ()
13317 : 57127072 : && (IS_TYPE_OR_DECL_P (cand1->fn)))
13318 : : return 1;
13319 : :
13320 : : /* Prefer a non-deleted function over an implicitly deleted move
13321 : : constructor or assignment operator. This differs slightly from the
13322 : : wording for issue 1402 (which says the move op is ignored by overload
13323 : : resolution), but this way produces better error messages. */
13324 : 55999025 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13325 : 51861988 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13326 : 107854547 : && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
13327 : : {
13328 : 2121318 : if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
13329 : 1802373 : && move_fn_p (cand1->fn))
13330 : : return -1;
13331 : 3282000 : if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
13332 : 2612893 : && move_fn_p (cand2->fn))
13333 : : return 1;
13334 : : }
13335 : :
13336 : : /* a viable function F1
13337 : : is defined to be a better function than another viable function F2 if
13338 : : for all arguments i, ICSi(F1) is not a worse conversion sequence than
13339 : : ICSi(F2), and then */
13340 : :
13341 : : /* for some argument j, ICSj(F1) is a better conversion sequence than
13342 : : ICSj(F2) */
13343 : :
13344 : : /* For comparing static and non-static member functions, we ignore
13345 : : the implicit object parameter of the non-static function. The
13346 : : standard says to pretend that the static function has an object
13347 : : parm, but that won't work with operator overloading. */
13348 : 55975754 : len = cand1->num_convs;
13349 : 55975754 : if (len != cand2->num_convs)
13350 : : {
13351 : 192 : int static_1 = (TREE_CODE (cand1->fn) == FUNCTION_DECL
13352 : 192 : && DECL_STATIC_FUNCTION_P (cand1->fn));
13353 : 192 : int static_2 = (TREE_CODE (cand2->fn) == FUNCTION_DECL
13354 : 192 : && DECL_STATIC_FUNCTION_P (cand2->fn));
13355 : :
13356 : 192 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13357 : 125 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13358 : 125 : && DECL_CONSTRUCTOR_P (cand1->fn)
13359 : 198 : && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
13360 : : /* We're comparing a near-match list constructor and a near-match
13361 : : non-list constructor. Just treat them as unordered. */
13362 : : return 0;
13363 : :
13364 : 186 : gcc_assert (static_1 != static_2);
13365 : :
13366 : 186 : if (static_1)
13367 : : {
13368 : : /* C++23 [over.best.ics.general] says:
13369 : : When the parameter is the implicit object parameter of a static
13370 : : member function, the implicit conversion sequence is a standard
13371 : : conversion sequence that is neither better nor worse than any
13372 : : other standard conversion sequence. */
13373 : 40 : if (CONVERSION_RANK (cand2->convs[0]) >= cr_user)
13374 : 0 : winner = 1;
13375 : : off2 = 1;
13376 : : }
13377 : : else
13378 : : {
13379 : 146 : if (CONVERSION_RANK (cand1->convs[0]) >= cr_user)
13380 : 67 : winner = -1;
13381 : 146 : off1 = 1;
13382 : 146 : --len;
13383 : : }
13384 : : }
13385 : :
13386 : 133592939 : for (i = 0; i < len; ++i)
13387 : : {
13388 : 77617825 : conversion *t1 = cand1->convs[i + off1];
13389 : 77617825 : conversion *t2 = cand2->convs[i + off2];
13390 : 77617825 : int comp = compare_ics (t1, t2);
13391 : :
13392 : 77617825 : if (comp != 0)
13393 : : {
13394 : 51270781 : if ((complain & tf_warning)
13395 : 44231529 : && warn_sign_promo
13396 : 82 : && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
13397 : : == cr_std + cr_promotion)
13398 : 50 : && t1->kind == ck_std
13399 : 31 : && t2->kind == ck_std
13400 : 31 : && TREE_CODE (t1->type) == INTEGER_TYPE
13401 : 31 : && TREE_CODE (t2->type) == INTEGER_TYPE
13402 : 31 : && (TYPE_PRECISION (t1->type)
13403 : 31 : == TYPE_PRECISION (t2->type))
13404 : 51270812 : && (TYPE_UNSIGNED (next_conversion (t1)->type)
13405 : 0 : || (TREE_CODE (next_conversion (t1)->type)
13406 : : == ENUMERAL_TYPE)))
13407 : : {
13408 : 31 : tree type = next_conversion (t1)->type;
13409 : 31 : tree type1, type2;
13410 : 31 : struct z_candidate *w, *l;
13411 : 31 : if (comp > 0)
13412 : : type1 = t1->type, type2 = t2->type,
13413 : : w = cand1, l = cand2;
13414 : : else
13415 : 8 : type1 = t2->type, type2 = t1->type,
13416 : 8 : w = cand2, l = cand1;
13417 : :
13418 : 31 : if (warn)
13419 : : {
13420 : 9 : warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
13421 : : type, type1, type2);
13422 : 9 : warning (OPT_Wsign_promo, " in call to %qD", w->fn);
13423 : : }
13424 : : else
13425 : 22 : add_warning (w, l);
13426 : : }
13427 : :
13428 : 51270781 : if (winner && comp != winner)
13429 : : {
13430 : : /* Ambiguity between normal and reversed comparison operators
13431 : : with the same parameter types. P2468 decided not to go with
13432 : : this approach to resolving the ambiguity, so pedwarn. */
13433 : 634 : if ((complain & tf_warning_or_error)
13434 : 427 : && (cand1->reversed () != cand2->reversed ())
13435 : 692 : && cand_parms_match (cand1, cand2, pmatch::original))
13436 : : {
13437 : 49 : struct z_candidate *w, *l;
13438 : 49 : if (cand2->reversed ())
13439 : : winner = 1, w = cand1, l = cand2;
13440 : : else
13441 : 29 : winner = -1, w = cand2, l = cand1;
13442 : 49 : if (warn)
13443 : : {
13444 : 20 : auto_diagnostic_group d;
13445 : 20 : if (pedwarn (input_location, 0,
13446 : : "C++20 says that these are ambiguous, "
13447 : : "even though the second is reversed:"))
13448 : : {
13449 : 20 : print_z_candidate (input_location,
13450 : : N_("candidate 1:"), w);
13451 : 20 : print_z_candidate (input_location,
13452 : : N_("candidate 2:"), l);
13453 : 20 : if (w->fn == l->fn
13454 : 16 : && DECL_IOBJ_MEMBER_FUNCTION_P (w->fn)
13455 : 35 : && (type_memfn_quals (TREE_TYPE (w->fn))
13456 : 15 : & TYPE_QUAL_CONST) == 0)
13457 : : {
13458 : : /* Suggest adding const to
13459 : : struct A { bool operator==(const A&); }; */
13460 : 12 : tree parmtype
13461 : 12 : = FUNCTION_FIRST_USER_PARMTYPE (w->fn);
13462 : 12 : parmtype = TREE_VALUE (parmtype);
13463 : 12 : if (TYPE_REF_P (parmtype)
13464 : 12 : && TYPE_READONLY (TREE_TYPE (parmtype))
13465 : 24 : && (same_type_ignoring_top_level_qualifiers_p
13466 : 12 : (TREE_TYPE (parmtype),
13467 : 12 : DECL_CONTEXT (w->fn))))
13468 : 12 : inform (DECL_SOURCE_LOCATION (w->fn),
13469 : : "try making the operator a %<const%> "
13470 : : "member function");
13471 : : }
13472 : : }
13473 : 20 : }
13474 : : else
13475 : 29 : add_warning (w, l);
13476 : 49 : return winner;
13477 : : }
13478 : :
13479 : 585 : winner = 0;
13480 : 585 : goto tweak;
13481 : : }
13482 : : winner = comp;
13483 : : }
13484 : : }
13485 : :
13486 : : /* warn about confusing overload resolution for user-defined conversions,
13487 : : either between a constructor and a conversion op, or between two
13488 : : conversion ops. */
13489 : 55975114 : if ((complain & tf_warning)
13490 : : /* In C++17, the constructor might have been elided, which means that
13491 : : an originally null ->second_conv could become non-null. */
13492 : 47750198 : && winner && warn_conversion && cand1->second_conv && cand2->second_conv
13493 : 54 : && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
13494 : 55975141 : && winner != compare_ics (cand1->second_conv, cand2->second_conv))
13495 : : {
13496 : 27 : struct z_candidate *w, *l;
13497 : 27 : bool give_warning = false;
13498 : :
13499 : 27 : if (winner == 1)
13500 : : w = cand1, l = cand2;
13501 : : else
13502 : 6 : w = cand2, l = cand1;
13503 : :
13504 : : /* We don't want to complain about `X::operator T1 ()'
13505 : : beating `X::operator T2 () const', when T2 is a no less
13506 : : cv-qualified version of T1. */
13507 : 27 : if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
13508 : 39 : && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
13509 : : {
13510 : 6 : tree t = TREE_TYPE (TREE_TYPE (l->fn));
13511 : 6 : tree f = TREE_TYPE (TREE_TYPE (w->fn));
13512 : :
13513 : 6 : if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t))
13514 : : {
13515 : 6 : t = TREE_TYPE (t);
13516 : 6 : f = TREE_TYPE (f);
13517 : : }
13518 : 6 : if (!comp_ptr_ttypes (t, f))
13519 : : give_warning = true;
13520 : : }
13521 : : else
13522 : : give_warning = true;
13523 : :
13524 : : if (!give_warning)
13525 : : /*NOP*/;
13526 : 21 : else if (warn)
13527 : : {
13528 : 9 : tree source = source_type (w->convs[0]);
13529 : 9 : if (INDIRECT_TYPE_P (source))
13530 : 6 : source = TREE_TYPE (source);
13531 : 9 : auto_diagnostic_group d;
13532 : 9 : if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
13533 : 18 : && warning (OPT_Wconversion, " for conversion from %qH to %qI",
13534 : 9 : source, w->second_conv->type))
13535 : : {
13536 : 9 : inform (input_location, " because conversion sequence "
13537 : : "for the argument is better");
13538 : : }
13539 : 9 : }
13540 : : else
13541 : 12 : add_warning (w, l);
13542 : : }
13543 : :
13544 : 55975114 : if (winner)
13545 : : return winner;
13546 : :
13547 : : /* DR 495 moved this tiebreaker above the template ones. */
13548 : : /* or, if not that,
13549 : : the context is an initialization by user-defined conversion (see
13550 : : _dcl.init_ and _over.match.user_) and the standard conversion
13551 : : sequence from the return type of F1 to the destination type (i.e.,
13552 : : the type of the entity being initialized) is a better conversion
13553 : : sequence than the standard conversion sequence from the return type
13554 : : of F2 to the destination type. */
13555 : :
13556 : 9082366 : if (cand1->second_conv)
13557 : : {
13558 : 49107 : winner = compare_ics (cand1->second_conv, cand2->second_conv);
13559 : 49107 : if (winner)
13560 : : return winner;
13561 : : }
13562 : :
13563 : : /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an
13564 : : explicit conversion (due to list-initialization) is worse. */
13565 : 9082230 : {
13566 : 9082230 : z_candidate *sp = nullptr;
13567 : 9082230 : if (sfk_copy_or_move (cand1->fn))
13568 : 475 : sp = cand1;
13569 : 9082230 : if (sfk_copy_or_move (cand2->fn))
13570 : 725091 : sp = sp ? nullptr : cand2;
13571 : 9082181 : if (sp)
13572 : : {
13573 : 1450936 : conversion *conv = sp->convs[!DECL_CONSTRUCTOR_P (sp->fn)];
13574 : 725468 : if (conv->user_conv_p)
13575 : 1286 : for (; conv; conv = next_conversion (conv))
13576 : 1162 : if (conv->kind == ck_user
13577 : 519 : && DECL_P (conv->cand->fn)
13578 : 1681 : && DECL_NONCONVERTING_P (conv->cand->fn))
13579 : 453 : return (sp == cand1) ? -1 : 1;
13580 : : }
13581 : : }
13582 : :
13583 : : /* DR2327: C++17 copy elision in [over.match.ctor] (direct-init) context.
13584 : : The standard currently says that only constructors are candidates, but if
13585 : : one copies a prvalue returned by a conversion function we prefer that.
13586 : :
13587 : : Clang does something similar, as discussed at
13588 : : http://lists.isocpp.org/core/2017/10/3166.php
13589 : : http://lists.isocpp.org/core/2019/03/5721.php */
13590 : 6611387 : if (len == 1 && cxx_dialect >= cxx17
13591 : 6597687 : && DECL_P (cand1->fn)
13592 : 6597687 : && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn)
13593 : 9745484 : && !(cand1->flags & LOOKUP_ONLYCONVERTING))
13594 : : {
13595 : 300855 : bool elided1 = joust_maybe_elide_copy (cand1);
13596 : 300855 : bool elided2 = joust_maybe_elide_copy (cand2);
13597 : 300855 : winner = elided1 - elided2;
13598 : 300855 : if (winner)
13599 : : return winner;
13600 : : }
13601 : :
13602 : : /* or, if not that,
13603 : : F1 is a non-template function and F2 is a template function
13604 : : specialization. */
13605 : :
13606 : 9081834 : if (!cand1->template_decl && cand2->template_decl)
13607 : : return 1;
13608 : 9049426 : else if (cand1->template_decl && !cand2->template_decl)
13609 : : return -1;
13610 : :
13611 : : /* or, if not that,
13612 : : F1 and F2 are template functions and the function template for F1 is
13613 : : more specialized than the template for F2 according to the partial
13614 : : ordering rules. */
13615 : :
13616 : 8114377 : if (cand1->template_decl && cand2->template_decl)
13617 : : {
13618 : 2963787 : winner = more_specialized_fn
13619 : 2963787 : (TI_TEMPLATE (cand1->template_decl),
13620 : 2963787 : TI_TEMPLATE (cand2->template_decl),
13621 : : /* [temp.func.order]: The presence of unused ellipsis and default
13622 : : arguments has no effect on the partial ordering of function
13623 : : templates. add_function_candidate() will not have
13624 : : counted the "this" argument for constructors. */
13625 : 5927574 : cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
13626 : 2963787 : if (winner)
13627 : : return winner;
13628 : : }
13629 : :
13630 : : /* F1 and F2 are non-template functions and
13631 : : - they have the same non-object-parameter-type-lists ([dcl.fct]), and
13632 : : - if they are member functions, both are direct members of the same
13633 : : class, and
13634 : : - if both are non-static member functions, they have the same types for
13635 : : their object parameters, and
13636 : : - F1 is more constrained than F2 according to the partial ordering of
13637 : : constraints described in [temp.constr.order]. */
13638 : 2557301 : if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn)
13639 : 2557274 : && !cand1->template_decl && !cand2->template_decl
13640 : 7714508 : && cand_parms_match (cand1, cand2, pmatch::current))
13641 : : {
13642 : 113343 : winner = more_constrained (cand1->fn, cand2->fn);
13643 : 113343 : if (winner)
13644 : : return winner;
13645 : : }
13646 : :
13647 : : /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
13648 : : rewritten candidates, and F2 is a synthesized candidate with reversed
13649 : : order of parameters and F1 is not. */
13650 : 5756306 : if (cand1->rewritten ())
13651 : : {
13652 : 574746 : if (!cand2->rewritten ())
13653 : : return -1;
13654 : 413111 : if (!cand1->reversed () && cand2->reversed ())
13655 : : return 1;
13656 : 413111 : if (cand1->reversed () && !cand2->reversed ())
13657 : : return -1;
13658 : : }
13659 : 5181560 : else if (cand2->rewritten ())
13660 : : return 1;
13661 : :
13662 : 5162951 : if (deduction_guide_p (cand1->fn))
13663 : : {
13664 : 6654 : gcc_assert (deduction_guide_p (cand2->fn));
13665 : :
13666 : : /* F1 and F2 are generated from class template argument deduction for a
13667 : : class D, and F2 is generated from inheriting constructors from a base
13668 : : class of D while F1 is not, and for each explicit function argument,
13669 : : the corresponding parameters of F1 and F2 are either both ellipses or
13670 : : have the same type. */
13671 : 6654 : bool inherited1 = inherited_guide_p (cand1->fn);
13672 : 6654 : bool inherited2 = inherited_guide_p (cand2->fn);
13673 : 6654 : if (int diff = inherited2 - inherited1)
13674 : : {
13675 : 52 : for (i = 0; i < len; ++i)
13676 : : {
13677 : 15 : conversion *t1 = cand1->convs[i + off1];
13678 : 15 : conversion *t2 = cand2->convs[i + off2];
13679 : : /* ??? It seems the ellipses part of this tiebreaker isn't
13680 : : needed since a mismatch should have broken the tie earlier
13681 : : during ICS comparison. */
13682 : 15 : gcc_checking_assert (t1->ellipsis_p == t2->ellipsis_p);
13683 : 15 : if (!same_type_p (t1->type, t2->type))
13684 : : break;
13685 : : }
13686 : 39 : if (i == len)
13687 : : return diff;
13688 : : }
13689 : :
13690 : : /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
13691 : : /* We distinguish between candidates from an explicit deduction guide and
13692 : : candidates built from a constructor based on DECL_ARTIFICIAL. */
13693 : 6617 : int art1 = DECL_ARTIFICIAL (cand1->fn);
13694 : 6617 : int art2 = DECL_ARTIFICIAL (cand2->fn);
13695 : 6617 : if (art1 != art2)
13696 : 1312 : return art2 - art1;
13697 : :
13698 : 5305 : if (art1)
13699 : : {
13700 : : /* Prefer the special copy guide over a declared copy/move
13701 : : constructor. */
13702 : 5302 : if (copy_guide_p (cand1->fn))
13703 : : return 1;
13704 : 74 : if (copy_guide_p (cand2->fn))
13705 : : return -1;
13706 : :
13707 : : /* Prefer a candidate generated from a non-template constructor. */
13708 : 26 : int tg1 = template_guide_p (cand1->fn);
13709 : 26 : int tg2 = template_guide_p (cand2->fn);
13710 : 26 : if (tg1 != tg2)
13711 : 6 : return tg2 - tg1;
13712 : : }
13713 : : }
13714 : :
13715 : : /* F1 is a constructor for a class D, F2 is a constructor for a base class B
13716 : : of D, and for all arguments the corresponding parameters of F1 and F2 have
13717 : : the same type (CWG 2273/2277). */
13718 : 15468799 : if (DECL_INHERITED_CTOR (cand1->fn) || DECL_INHERITED_CTOR (cand2->fn))
13719 : : {
13720 : 92 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
13721 : 92 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
13722 : :
13723 : 92 : bool used1 = false;
13724 : 92 : bool used2 = false;
13725 : 92 : if (base1 == base2)
13726 : : /* No difference. */;
13727 : 92 : else if (DERIVED_FROM_P (base1, base2))
13728 : : used1 = true;
13729 : 18 : else if (DERIVED_FROM_P (base2, base1))
13730 : : used2 = true;
13731 : :
13732 : 83 : if (int diff = used2 - used1)
13733 : : {
13734 : 110 : for (i = 0; i < len; ++i)
13735 : : {
13736 : 36 : conversion *t1 = cand1->convs[i + off1];
13737 : 36 : conversion *t2 = cand2->convs[i + off2];
13738 : 36 : if (!same_type_p (t1->type, t2->type))
13739 : : break;
13740 : : }
13741 : 83 : if (i == len)
13742 : : return diff;
13743 : : }
13744 : : }
13745 : :
13746 : : /* Check whether we can discard a builtin candidate, either because we
13747 : : have two identical ones or matching builtin and non-builtin candidates.
13748 : :
13749 : : (Pedantically in the latter case the builtin which matched the user
13750 : : function should not be added to the overload set, but we spot it here.
13751 : :
13752 : : [over.match.oper]
13753 : : ... the builtin candidates include ...
13754 : : - do not have the same parameter type list as any non-template
13755 : : non-member candidate. */
13756 : :
13757 : 5156246 : if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
13758 : : {
13759 : 71 : for (i = 0; i < len; ++i)
13760 : 56 : if (!same_type_p (cand1->convs[i]->type,
13761 : : cand2->convs[i]->type))
13762 : : break;
13763 : 39 : if (i == cand1->num_convs)
13764 : : {
13765 : 15 : if (cand1->fn == cand2->fn)
13766 : : /* Two built-in candidates; arbitrarily pick one. */
13767 : : return 1;
13768 : 14687844 : else if (identifier_p (cand1->fn))
13769 : : /* cand1 is built-in; prefer cand2. */
13770 : : return -1;
13771 : : else
13772 : : /* cand2 is built-in; prefer cand1. */
13773 : : return 1;
13774 : : }
13775 : : }
13776 : :
13777 : : /* For candidates of a multi-versioned function, make the version with
13778 : : the highest priority win. This version will be checked for dispatching
13779 : : first. If this version can be inlined into the caller, the front-end
13780 : : will simply make a direct call to this function. */
13781 : :
13782 : 5156231 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13783 : 5156204 : && DECL_FUNCTION_VERSIONED (cand1->fn)
13784 : 896 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13785 : 5157127 : && DECL_FUNCTION_VERSIONED (cand2->fn))
13786 : : {
13787 : 896 : tree f1 = TREE_TYPE (cand1->fn);
13788 : 896 : tree f2 = TREE_TYPE (cand2->fn);
13789 : 896 : tree p1 = TYPE_ARG_TYPES (f1);
13790 : 896 : tree p2 = TYPE_ARG_TYPES (f2);
13791 : :
13792 : : /* Check if cand1->fn and cand2->fn are versions of the same function. It
13793 : : is possible that cand1->fn and cand2->fn are function versions but of
13794 : : different functions. Check types to see if they are versions of the same
13795 : : function. */
13796 : 896 : if (compparms (p1, p2)
13797 : 896 : && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
13798 : : {
13799 : : /* Always make the version with the higher priority, more
13800 : : specialized, win. */
13801 : 896 : gcc_assert (targetm.compare_version_priority);
13802 : 896 : if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
13803 : : return 1;
13804 : : else
13805 : : return -1;
13806 : : }
13807 : : }
13808 : :
13809 : : /* If the two function declarations represent the same function (this can
13810 : : happen with declarations in multiple scopes and arg-dependent lookup),
13811 : : arbitrarily choose one. But first make sure the default args we're
13812 : : using match. */
13813 : 5155308 : if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
13814 : 10310643 : && equal_functions (cand1->fn, cand2->fn))
13815 : : {
13816 : 31 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
13817 : 31 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
13818 : :
13819 : 62 : gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
13820 : :
13821 : 44 : for (i = 0; i < len; ++i)
13822 : : {
13823 : : /* Don't crash if the fn is variadic. */
13824 : 13 : if (!parms1)
13825 : : break;
13826 : 13 : parms1 = TREE_CHAIN (parms1);
13827 : 13 : parms2 = TREE_CHAIN (parms2);
13828 : : }
13829 : :
13830 : 31 : if (off1)
13831 : 0 : parms1 = TREE_CHAIN (parms1);
13832 : 31 : else if (off2)
13833 : 0 : parms2 = TREE_CHAIN (parms2);
13834 : :
13835 : 59 : for (; parms1; ++i)
13836 : : {
13837 : 68 : if (!cp_tree_equal (TREE_PURPOSE (parms1),
13838 : 34 : TREE_PURPOSE (parms2)))
13839 : : {
13840 : 6 : if (warn)
13841 : : {
13842 : 3 : if (complain & tf_error)
13843 : : {
13844 : 3 : auto_diagnostic_group d;
13845 : 3 : if (permerror (input_location,
13846 : : "default argument mismatch in "
13847 : : "overload resolution"))
13848 : : {
13849 : 3 : inform (DECL_SOURCE_LOCATION (cand1->fn),
13850 : : " candidate 1: %q#F", cand1->fn);
13851 : 3 : inform (DECL_SOURCE_LOCATION (cand2->fn),
13852 : : " candidate 2: %q#F", cand2->fn);
13853 : : }
13854 : 3 : }
13855 : : else
13856 : : return 0;
13857 : : }
13858 : : else
13859 : 3 : add_warning (cand1, cand2);
13860 : : break;
13861 : : }
13862 : 28 : parms1 = TREE_CHAIN (parms1);
13863 : 28 : parms2 = TREE_CHAIN (parms2);
13864 : : }
13865 : :
13866 : 31 : return 1;
13867 : : }
13868 : :
13869 : 5155889 : tweak:
13870 : :
13871 : : /* Extension: If the worst conversion for one candidate is better than the
13872 : : worst conversion for the other, take the first. */
13873 : 5155889 : if (!pedantic && (complain & tf_warning_or_error))
13874 : : {
13875 : : conversion_rank rank1 = cr_identity, rank2 = cr_identity;
13876 : 12094940 : struct z_candidate *w = 0, *l = 0;
13877 : :
13878 : 12094940 : for (i = 0; i < len; ++i)
13879 : : {
13880 : 7137722 : if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
13881 : 4895171 : rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
13882 : 7137722 : if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
13883 : 4895187 : rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
13884 : : }
13885 : 4957218 : if (rank1 < rank2)
13886 : 32 : winner = 1, w = cand1, l = cand2;
13887 : 4957218 : if (rank1 > rank2)
13888 : : winner = -1, w = cand2, l = cand1;
13889 : 4957109 : if (winner)
13890 : : {
13891 : : /* Don't choose a deleted function over ambiguity. */
13892 : 141 : if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
13893 : : return 0;
13894 : 141 : if (warn)
13895 : : {
13896 : 6 : auto_diagnostic_group d;
13897 : 6 : if (pedwarn (input_location, 0,
13898 : : "ISO C++ says that these are ambiguous, even "
13899 : : "though the worst conversion for the first is "
13900 : : "better than the worst conversion for the second:"))
13901 : : {
13902 : 3 : print_z_candidate (input_location, N_("candidate 1:"), w);
13903 : 3 : print_z_candidate (input_location, N_("candidate 2:"), l);
13904 : : }
13905 : 6 : }
13906 : : else
13907 : 135 : add_warning (w, l);
13908 : 141 : return winner;
13909 : : }
13910 : : }
13911 : :
13912 : : gcc_assert (!winner);
13913 : : return 0;
13914 : : }
13915 : :
13916 : : /* Given a list of candidates for overloading, find the best one, if any.
13917 : : This algorithm has a worst case of O(2n) (winner is last), and a best
13918 : : case of O(n/2) (totally ambiguous); much better than a sorting
13919 : : algorithm. The candidates list is assumed to be sorted according
13920 : : to viability (via splice_viable). */
13921 : :
13922 : : static struct z_candidate *
13923 : 187205082 : tourney (struct z_candidate *candidates, tsubst_flags_t complain)
13924 : : {
13925 : 187205082 : struct z_candidate **champ = &candidates, **challenger;
13926 : 187205082 : int fate;
13927 : 187205082 : struct z_candidate *previous_worse_champ = nullptr;
13928 : :
13929 : : /* Walk through the list once, comparing each current champ to the next
13930 : : candidate, knocking out a candidate or two with each comparison. */
13931 : :
13932 : 244690583 : for (challenger = &candidates->next; *challenger && (*challenger)->viable; )
13933 : : {
13934 : 57510777 : fate = joust (*champ, *challenger, 0, complain);
13935 : 57510777 : if (fate == 1)
13936 : 37741381 : challenger = &(*challenger)->next;
13937 : 19769396 : else if (fate == -1)
13938 : : {
13939 : 14615348 : previous_worse_champ = *champ;
13940 : 14615348 : champ = challenger;
13941 : 14615348 : challenger = &(*challenger)->next;
13942 : : }
13943 : : else
13944 : : {
13945 : 5154048 : previous_worse_champ = nullptr;
13946 : 5154048 : champ = &(*challenger)->next;
13947 : 5154048 : if (!*champ || !(*champ)->viable
13948 : 5128866 : || (*champ)->viable < (*challenger)->viable)
13949 : : {
13950 : : champ = nullptr;
13951 : : break;
13952 : : }
13953 : 5128772 : challenger = &(*champ)->next;
13954 : : }
13955 : : }
13956 : :
13957 : : /* Make sure the champ is better than all the candidates it hasn't yet
13958 : : been compared to. */
13959 : :
13960 : 187205082 : if (champ)
13961 : 25104069 : for (challenger = &candidates;
13962 : 212283875 : challenger != champ;
13963 : 25104069 : challenger = &(*challenger)->next)
13964 : : {
13965 : 25105969 : if (*challenger == previous_worse_champ)
13966 : : /* We already know this candidate is worse than the champ. */
13967 : 13520225 : continue;
13968 : 11585744 : fate = joust (*champ, *challenger, 0, complain);
13969 : 11585744 : if (fate != 1)
13970 : : {
13971 : : champ = nullptr;
13972 : : break;
13973 : : }
13974 : : }
13975 : :
13976 : 187179806 : if (!champ)
13977 : 27176 : return nullptr;
13978 : :
13979 : : /* Move the champ to the front of the candidate list. */
13980 : :
13981 : 187177906 : if (champ != &candidates)
13982 : : {
13983 : 15282376 : z_candidate *saved_champ = *champ;
13984 : 15282376 : *champ = saved_champ->next;
13985 : 15282376 : saved_champ->next = candidates;
13986 : 15282376 : candidates = saved_champ;
13987 : : }
13988 : :
13989 : 187177906 : return candidates;
13990 : : }
13991 : :
13992 : : /* Returns nonzero if things of type FROM can be converted to TO. */
13993 : :
13994 : : bool
13995 : 3362744 : can_convert (tree to, tree from, tsubst_flags_t complain)
13996 : : {
13997 : 3362744 : tree arg = NULL_TREE;
13998 : : /* implicit_conversion only considers user-defined conversions
13999 : : if it has an expression for the call argument list. */
14000 : 3362744 : if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
14001 : 115 : arg = build_stub_object (from);
14002 : 3362744 : return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
14003 : : }
14004 : :
14005 : : /* Returns nonzero if things of type FROM can be converted to TO with a
14006 : : standard conversion. */
14007 : :
14008 : : bool
14009 : 255 : can_convert_standard (tree to, tree from, tsubst_flags_t complain)
14010 : : {
14011 : 255 : return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
14012 : : }
14013 : :
14014 : : /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
14015 : :
14016 : : bool
14017 : 4167377 : can_convert_arg (tree to, tree from, tree arg, int flags,
14018 : : tsubst_flags_t complain)
14019 : : {
14020 : 4167377 : conversion *t;
14021 : 4167377 : bool ok_p;
14022 : :
14023 : 4167377 : conversion_obstack_sentinel cos;
14024 : : /* We want to discard any access checks done for this test,
14025 : : as we might not be in the appropriate access context and
14026 : : we'll do the check again when we actually perform the
14027 : : conversion. */
14028 : 4167377 : push_deferring_access_checks (dk_deferred);
14029 : :
14030 : : /* Handle callers like check_local_shadow forgetting to
14031 : : convert_from_reference. */
14032 : 4167377 : if (TYPE_REF_P (from) && arg)
14033 : : {
14034 : 60 : arg = convert_from_reference (arg);
14035 : 60 : from = TREE_TYPE (arg);
14036 : : }
14037 : :
14038 : 4167377 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14039 : : flags, complain);
14040 : 4167377 : ok_p = (t && !t->bad_p);
14041 : :
14042 : : /* Discard the access checks now. */
14043 : 4167377 : pop_deferring_access_checks ();
14044 : :
14045 : 8334754 : return ok_p;
14046 : 4167377 : }
14047 : :
14048 : : /* Like can_convert_arg, but allows dubious conversions as well. */
14049 : :
14050 : : bool
14051 : 121671717 : can_convert_arg_bad (tree to, tree from, tree arg, int flags,
14052 : : tsubst_flags_t complain)
14053 : : {
14054 : 121671717 : conversion *t;
14055 : :
14056 : 121671717 : conversion_obstack_sentinel cos;
14057 : : /* Try to perform the conversion. */
14058 : 121671717 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14059 : : flags, complain);
14060 : :
14061 : 243343434 : return t != NULL;
14062 : 121671717 : }
14063 : :
14064 : : /* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload
14065 : : resolution FLAGS. */
14066 : :
14067 : : tree
14068 : 11798963 : build_implicit_conv_flags (tree type, tree expr, int flags)
14069 : : {
14070 : : /* In a template, we are only concerned about determining the
14071 : : type of non-dependent expressions, so we do not have to
14072 : : perform the actual conversion. But for initializers, we
14073 : : need to be able to perform it at instantiation
14074 : : (or instantiate_non_dependent_expr) time. */
14075 : 11798963 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14076 : 11798963 : if (!(flags & LOOKUP_ONLYCONVERTING))
14077 : 3276392 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14078 : 11798963 : if (flags & LOOKUP_NO_NARROWING)
14079 : 6118 : IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
14080 : 11798963 : return expr;
14081 : : }
14082 : :
14083 : : /* Convert EXPR to TYPE. Return the converted expression.
14084 : :
14085 : : Note that we allow bad conversions here because by the time we get to
14086 : : this point we are committed to doing the conversion. If we end up
14087 : : doing a bad conversion, convert_like will complain. */
14088 : :
14089 : : tree
14090 : 208523618 : perform_implicit_conversion_flags (tree type, tree expr,
14091 : : tsubst_flags_t complain, int flags)
14092 : : {
14093 : 208523618 : conversion *conv;
14094 : 208523618 : location_t loc = cp_expr_loc_or_input_loc (expr);
14095 : :
14096 : 208523618 : if (error_operand_p (expr))
14097 : 432 : return error_mark_node;
14098 : :
14099 : 208523186 : conversion_obstack_sentinel cos;
14100 : :
14101 : 208523186 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14102 : : /*c_cast_p=*/false,
14103 : : flags, complain);
14104 : :
14105 : 208523186 : if (!conv)
14106 : : {
14107 : 174617 : if (complain & tf_error)
14108 : 470 : implicit_conversion_error (loc, type, expr);
14109 : 174617 : expr = error_mark_node;
14110 : : }
14111 : 208348569 : else if (processing_template_decl && conv->kind != ck_identity)
14112 : 11794149 : expr = build_implicit_conv_flags (type, expr, flags);
14113 : : else
14114 : : {
14115 : : /* Give a conversion call the same location as expr. */
14116 : 196554420 : iloc_sentinel il (loc);
14117 : 196554420 : expr = convert_like (conv, expr, complain);
14118 : 196554420 : }
14119 : :
14120 : 208523186 : return expr;
14121 : 208523186 : }
14122 : :
14123 : : tree
14124 : 25299132 : perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
14125 : : {
14126 : 25299132 : return perform_implicit_conversion_flags (type, expr, complain,
14127 : 25299132 : LOOKUP_IMPLICIT);
14128 : : }
14129 : :
14130 : : /* Convert EXPR to TYPE (as a direct-initialization) if that is
14131 : : permitted. If the conversion is valid, the converted expression is
14132 : : returned. Otherwise, NULL_TREE is returned, except in the case
14133 : : that TYPE is a class type; in that case, an error is issued. If
14134 : : C_CAST_P is true, then this direct-initialization is taking
14135 : : place as part of a static_cast being attempted as part of a C-style
14136 : : cast. */
14137 : :
14138 : : tree
14139 : 42222946 : perform_direct_initialization_if_possible (tree type,
14140 : : tree expr,
14141 : : bool c_cast_p,
14142 : : tsubst_flags_t complain)
14143 : : {
14144 : 42222946 : conversion *conv;
14145 : :
14146 : 42222946 : if (type == error_mark_node || error_operand_p (expr))
14147 : : return error_mark_node;
14148 : : /* [dcl.init]
14149 : :
14150 : : If the destination type is a (possibly cv-qualified) class type:
14151 : :
14152 : : -- If the initialization is direct-initialization ...,
14153 : : constructors are considered.
14154 : :
14155 : : -- If overload resolution is successful, the selected constructor
14156 : : is called to initialize the object, with the initializer expression
14157 : : or expression-list as its argument(s).
14158 : :
14159 : : -- Otherwise, if no constructor is viable, the destination type is
14160 : : a (possibly cv-qualified) aggregate class A, and the initializer is
14161 : : a parenthesized expression-list, the object is initialized as
14162 : : follows... */
14163 : 42222946 : if (CLASS_TYPE_P (type))
14164 : : {
14165 : 1714875 : releasing_vec args (make_tree_vector_single (expr));
14166 : 1714875 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
14167 : : &args, type, LOOKUP_NORMAL, complain);
14168 : 1714875 : return build_cplus_new (type, expr, complain);
14169 : 1714875 : }
14170 : :
14171 : 40508071 : conversion_obstack_sentinel cos;
14172 : :
14173 : 40508071 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14174 : : c_cast_p,
14175 : : LOOKUP_NORMAL, complain);
14176 : 40508071 : if (!conv || conv->bad_p)
14177 : : expr = NULL_TREE;
14178 : 36502591 : else if (processing_template_decl && conv->kind != ck_identity)
14179 : : {
14180 : : /* In a template, we are only concerned about determining the
14181 : : type of non-dependent expressions, so we do not have to
14182 : : perform the actual conversion. But for initializers, we
14183 : : need to be able to perform it at instantiation
14184 : : (or instantiate_non_dependent_expr) time. */
14185 : 672553 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14186 : 672553 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14187 : : }
14188 : : else
14189 : 35830038 : expr = convert_like (conv, expr, NULL_TREE, 0,
14190 : : /*issue_conversion_warnings=*/false,
14191 : : c_cast_p, /*nested_p=*/false, complain);
14192 : :
14193 : 40508071 : return expr;
14194 : 40508071 : }
14195 : :
14196 : : /* When initializing a reference that lasts longer than a full-expression,
14197 : : this special rule applies:
14198 : :
14199 : : [class.temporary]
14200 : :
14201 : : The temporary to which the reference is bound or the temporary
14202 : : that is the complete object to which the reference is bound
14203 : : persists for the lifetime of the reference.
14204 : :
14205 : : The temporaries created during the evaluation of the expression
14206 : : initializing the reference, except the temporary to which the
14207 : : reference is bound, are destroyed at the end of the
14208 : : full-expression in which they are created.
14209 : :
14210 : : In that case, we store the converted expression into a new
14211 : : VAR_DECL in a new scope.
14212 : :
14213 : : However, we want to be careful not to create temporaries when
14214 : : they are not required. For example, given:
14215 : :
14216 : : struct B {};
14217 : : struct D : public B {};
14218 : : D f();
14219 : : const B& b = f();
14220 : :
14221 : : there is no need to copy the return value from "f"; we can just
14222 : : extend its lifetime. Similarly, given:
14223 : :
14224 : : struct S {};
14225 : : struct T { operator S(); };
14226 : : T t;
14227 : : const S& s = t;
14228 : :
14229 : : we can extend the lifetime of the return value of the conversion
14230 : : operator.
14231 : :
14232 : : The next several functions are involved in this lifetime extension. */
14233 : :
14234 : : /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
14235 : : reference is being bound to a temporary. Create and return a new
14236 : : VAR_DECL with the indicated TYPE; this variable will store the value to
14237 : : which the reference is bound. */
14238 : :
14239 : : tree
14240 : 10740 : make_temporary_var_for_ref_to_temp (tree decl, tree type)
14241 : : {
14242 : 10740 : tree var = create_temporary_var (type);
14243 : :
14244 : : /* Register the variable. */
14245 : 10740 : if (VAR_P (decl)
14246 : 10740 : && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
14247 : : {
14248 : : /* Namespace-scope or local static; give it a mangled name. */
14249 : :
14250 : : /* If an initializer is visible to multiple translation units, those
14251 : : translation units must agree on the addresses of the
14252 : : temporaries. Therefore the temporaries must be given a consistent name
14253 : : and vague linkage. The mangled name of a temporary is the name of the
14254 : : non-temporary object in whose initializer they appear, prefixed with
14255 : : GR and suffixed with a sequence number mangled using the usual rules
14256 : : for a seq-id. Temporaries are numbered with a pre-order, depth-first,
14257 : : left-to-right walk of the complete initializer. */
14258 : 786 : copy_linkage (var, decl);
14259 : :
14260 : 786 : tree name = mangle_ref_init_variable (decl);
14261 : 786 : DECL_NAME (var) = name;
14262 : 786 : SET_DECL_ASSEMBLER_NAME (var, name);
14263 : :
14264 : : /* Set the context to make the variable mergeable in modules. */
14265 : 786 : DECL_CONTEXT (var) = current_scope ();
14266 : : }
14267 : : else
14268 : : /* Create a new cleanup level if necessary. */
14269 : 9954 : maybe_push_cleanup_level (type);
14270 : :
14271 : 10740 : return pushdecl (var);
14272 : : }
14273 : :
14274 : : static tree extend_temps_r (tree *, int *, void *);
14275 : :
14276 : : /* EXPR is the initializer for a variable DECL of reference or
14277 : : std::initializer_list type. Create, push and return a new VAR_DECL
14278 : : for the initializer so that it will live as long as DECL. Any
14279 : : cleanup for the new variable is returned through CLEANUP, and the
14280 : : code to initialize the new variable is returned through INITP. */
14281 : :
14282 : : static tree
14283 : 10740 : set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
14284 : : tree *initp, tree *cond_guard,
14285 : : void *walk_data)
14286 : : {
14287 : 10740 : tree init;
14288 : 10740 : tree type;
14289 : 10740 : tree var;
14290 : :
14291 : : /* Create the temporary variable. */
14292 : 10740 : type = TREE_TYPE (expr);
14293 : 10740 : var = make_temporary_var_for_ref_to_temp (decl, type);
14294 : 10740 : layout_decl (var, 0);
14295 : : /* If the rvalue is the result of a function call it will be
14296 : : a TARGET_EXPR. If it is some other construct (such as a
14297 : : member access expression where the underlying object is
14298 : : itself the result of a function call), turn it into a
14299 : : TARGET_EXPR here. It is important that EXPR be a
14300 : : TARGET_EXPR below since otherwise the INIT_EXPR will
14301 : : attempt to make a bitwise copy of EXPR to initialize
14302 : : VAR. */
14303 : 10740 : if (TREE_CODE (expr) != TARGET_EXPR)
14304 : 0 : expr = get_target_expr (expr);
14305 : : else
14306 : : {
14307 : 10740 : if (TREE_ADDRESSABLE (expr))
14308 : 10666 : TREE_ADDRESSABLE (var) = 1;
14309 : 10740 : if (DECL_MERGEABLE (TARGET_EXPR_SLOT (expr)))
14310 : 519 : DECL_MERGEABLE (var) = true;
14311 : : }
14312 : :
14313 : 10740 : if (TREE_CODE (decl) == FIELD_DECL
14314 : 10740 : && extra_warnings && !warning_suppressed_p (decl))
14315 : : {
14316 : 3 : warning (OPT_Wextra, "a temporary bound to %qD only persists "
14317 : : "until the constructor exits", decl);
14318 : 3 : suppress_warning (decl);
14319 : : }
14320 : :
14321 : : /* Recursively extend temps in this initializer. The recursion needs to come
14322 : : after creating the variable to conform to the mangling ABI, and before
14323 : : maybe_constant_init because the extension might change its result. */
14324 : 10740 : if (walk_data)
14325 : 980 : cp_walk_tree (&TARGET_EXPR_INITIAL (expr), extend_temps_r,
14326 : : walk_data, nullptr);
14327 : : else
14328 : 9760 : TARGET_EXPR_INITIAL (expr)
14329 : 19520 : = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups,
14330 : : cond_guard);
14331 : :
14332 : : /* Any reference temp has a non-trivial initializer. */
14333 : 10740 : DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
14334 : :
14335 : : /* If the initializer is constant, put it in DECL_INITIAL so we get
14336 : : static initialization and use in constant expressions. */
14337 : 10740 : init = maybe_constant_init (expr, var, /*manifestly_const_eval=*/true);
14338 : : /* As in store_init_value. */
14339 : 10740 : init = cp_fully_fold (init);
14340 : 10740 : if (TREE_CONSTANT (init))
14341 : : {
14342 : 1019 : if (literal_type_p (type)
14343 : 993 : && CP_TYPE_CONST_NON_VOLATILE_P (type)
14344 : 1626 : && !TYPE_HAS_MUTABLE_P (type))
14345 : : {
14346 : : /* 5.19 says that a constant expression can include an
14347 : : lvalue-rvalue conversion applied to "a glvalue of literal type
14348 : : that refers to a non-volatile temporary object initialized
14349 : : with a constant expression". Rather than try to communicate
14350 : : that this VAR_DECL is a temporary, just mark it constexpr. */
14351 : 604 : DECL_DECLARED_CONSTEXPR_P (var) = true;
14352 : 604 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
14353 : 604 : TREE_CONSTANT (var) = true;
14354 : 604 : TREE_READONLY (var) = true;
14355 : : }
14356 : 1019 : DECL_INITIAL (var) = init;
14357 : 1019 : init = NULL_TREE;
14358 : : }
14359 : : else
14360 : : /* Create the INIT_EXPR that will initialize the temporary
14361 : : variable. */
14362 : 9721 : init = split_nonconstant_init (var, expr);
14363 : 10740 : if (at_function_scope_p ())
14364 : : {
14365 : 10103 : add_decl_expr (var);
14366 : :
14367 : 10103 : if (TREE_STATIC (var))
14368 : 149 : init = add_stmt_to_compound (init, register_dtor_fn (var));
14369 : : else
14370 : : {
14371 : : /* ??? Instead of rebuilding the cleanup, we could replace the slot
14372 : : with var in TARGET_EXPR_CLEANUP (expr). */
14373 : 9954 : tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
14374 : 9954 : if (cleanup)
14375 : : {
14376 : 6211 : if (cond_guard && cleanup != error_mark_node)
14377 : : {
14378 : 23 : if (*cond_guard == NULL_TREE)
14379 : : {
14380 : 23 : *cond_guard = build_local_temp (boolean_type_node);
14381 : 23 : add_decl_expr (*cond_guard);
14382 : 23 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION,
14383 : : *cond_guard, NOP_EXPR,
14384 : : boolean_false_node,
14385 : : tf_warning_or_error);
14386 : 23 : finish_expr_stmt (set);
14387 : : }
14388 : 23 : cleanup = build3 (COND_EXPR, void_type_node,
14389 : : *cond_guard, cleanup, NULL_TREE);
14390 : : }
14391 : 6211 : if (flag_exceptions && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE)
14392 : : {
14393 : : /* The normal cleanup for this extended variable isn't pushed
14394 : : until cp_finish_decl, so we need to retain a TARGET_EXPR
14395 : : to clean it up in case a later initializer throws
14396 : : (g++.dg/eh/ref-temp3.C).
14397 : :
14398 : : We don't do this for array temporaries because they have
14399 : : the array cleanup region from build_vec_init.
14400 : :
14401 : : Unlike maybe_push_temp_cleanup, we don't actually need a
14402 : : flag, but a TARGET_EXPR needs a TARGET_EXPR_SLOT.
14403 : : Perhaps this could use WITH_CLEANUP_EXPR instead, but
14404 : : gimplify.cc doesn't handle that, and front-end handling
14405 : : was removed in r8-1725 and r8-1818.
14406 : :
14407 : : Alternately it might be preferable to flatten an
14408 : : initialization with extended temps into a sequence of
14409 : : (non-full-expression) statements, so we could immediately
14410 : : push_cleanup here for only a single cleanup region, but we
14411 : : don't have a mechanism for that in the front-end, only the
14412 : : gimplifier. */
14413 : 6103 : tree targ = get_internal_target_expr (boolean_true_node);
14414 : 6103 : TARGET_EXPR_CLEANUP (targ) = cleanup;
14415 : 6103 : CLEANUP_EH_ONLY (targ) = true;
14416 : : /* Don't actually initialize the bool. */
14417 : 6103 : init = (!init ? void_node
14418 : 6077 : : convert_to_void (init, ICV_STATEMENT, tf_none));
14419 : 6103 : TARGET_EXPR_INITIAL (targ) = init;
14420 : 6103 : init = targ;
14421 : : }
14422 : 6211 : vec_safe_push (*cleanups, cleanup);
14423 : : }
14424 : : }
14425 : :
14426 : : /* We must be careful to destroy the temporary only
14427 : : after its initialization has taken place. If the
14428 : : initialization throws an exception, then the
14429 : : destructor should not be run. We cannot simply
14430 : : transform INIT into something like:
14431 : :
14432 : : (INIT, ({ CLEANUP_STMT; }))
14433 : :
14434 : : because emit_local_var always treats the
14435 : : initializer as a full-expression. Thus, the
14436 : : destructor would run too early; it would run at the
14437 : : end of initializing the reference variable, rather
14438 : : than at the end of the block enclosing the
14439 : : reference variable.
14440 : :
14441 : : The solution is to pass back a cleanup expression
14442 : : which the caller is responsible for attaching to
14443 : : the statement tree. */
14444 : : }
14445 : : else
14446 : : {
14447 : 637 : rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
14448 : 637 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14449 : : {
14450 : 88 : if (CP_DECL_THREAD_LOCAL_P (var))
14451 : 36 : tls_aggregates = tree_cons (NULL_TREE, var,
14452 : : tls_aggregates);
14453 : : else
14454 : 52 : static_aggregates = tree_cons (NULL_TREE, var,
14455 : : static_aggregates);
14456 : : }
14457 : : else
14458 : : /* Check whether the dtor is callable. */
14459 : 549 : cxx_maybe_build_cleanup (var, tf_warning_or_error);
14460 : : }
14461 : : /* Avoid -Wunused-variable warning (c++/38958). */
14462 : 10740 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
14463 : 10740 : && VAR_P (decl))
14464 : 6266 : TREE_USED (decl) = DECL_READ_P (decl) = true;
14465 : :
14466 : 10740 : *initp = init;
14467 : 10740 : return var;
14468 : : }
14469 : :
14470 : : /* Convert EXPR to the indicated reference TYPE, in a way suitable for
14471 : : initializing a variable of that TYPE. */
14472 : :
14473 : : tree
14474 : 5407409 : initialize_reference (tree type, tree expr,
14475 : : int flags, tsubst_flags_t complain)
14476 : : {
14477 : 5407409 : conversion *conv;
14478 : 5407409 : location_t loc = cp_expr_loc_or_input_loc (expr);
14479 : :
14480 : 5407409 : if (type == error_mark_node || error_operand_p (expr))
14481 : : return error_mark_node;
14482 : :
14483 : 5407344 : conversion_obstack_sentinel cos;
14484 : :
14485 : 5407344 : conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
14486 : : flags, complain);
14487 : : /* If this conversion failed, we're in C++20, and we have something like
14488 : : A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
14489 : 5407344 : if ((!conv || conv->bad_p)
14490 : 576 : && (flags & LOOKUP_AGGREGATE_PAREN_INIT))
14491 : : {
14492 : 4 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
14493 : 4 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
14494 : 4 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
14495 : 4 : conversion *c = reference_binding (type, TREE_TYPE (e), e,
14496 : : /*c_cast_p=*/false, flags, complain);
14497 : : /* If this worked, use it. */
14498 : 4 : if (c && !c->bad_p)
14499 : : expr = e, conv = c;
14500 : : }
14501 : 5407855 : if (!conv || conv->bad_p)
14502 : : {
14503 : 573 : if (complain & tf_error)
14504 : : {
14505 : 348 : if (conv)
14506 : 303 : convert_like (conv, expr, complain);
14507 : 45 : else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
14508 : 13 : && !TYPE_REF_IS_RVALUE (type)
14509 : 58 : && !lvalue_p (expr))
14510 : 6 : error_at (loc, "invalid initialization of non-const reference of "
14511 : : "type %qH from an rvalue of type %qI",
14512 : 6 : type, TREE_TYPE (expr));
14513 : : else
14514 : 39 : error_at (loc, "invalid initialization of reference of type "
14515 : : "%qH from expression of type %qI", type,
14516 : 39 : TREE_TYPE (expr));
14517 : : }
14518 : 573 : return error_mark_node;
14519 : : }
14520 : :
14521 : 5406771 : if (conv->kind == ck_ref_bind)
14522 : : /* Perform the conversion. */
14523 : 5406768 : expr = convert_like (conv, expr, complain);
14524 : 3 : else if (conv->kind == ck_ambig)
14525 : : /* We gave an error in build_user_type_conversion_1. */
14526 : 3 : expr = error_mark_node;
14527 : : else
14528 : 0 : gcc_unreachable ();
14529 : :
14530 : : return expr;
14531 : 5407344 : }
14532 : :
14533 : : /* Return true if T is std::pair<const T&, const T&>. */
14534 : :
14535 : : static bool
14536 : 534041 : std_pair_ref_ref_p (tree t)
14537 : : {
14538 : : /* First, check if we have std::pair. */
14539 : 51706 : if (!NON_UNION_CLASS_TYPE_P (t)
14540 : 585287 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (t))
14541 : : return false;
14542 : 18246 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (t));
14543 : 18246 : if (!decl_in_std_namespace_p (tdecl))
14544 : : return false;
14545 : 13259 : tree name = DECL_NAME (tdecl);
14546 : 13259 : if (!name || !id_equal (name, "pair"))
14547 : : return false;
14548 : :
14549 : : /* Now see if the template arguments are both const T&. */
14550 : 337 : tree args = CLASSTYPE_TI_ARGS (t);
14551 : 337 : if (TREE_VEC_LENGTH (args) != 2)
14552 : : return false;
14553 : 397 : for (int i = 0; i < 2; i++)
14554 : 427 : if (!TYPE_REF_OBJ_P (TREE_VEC_ELT (args, i))
14555 : 427 : || !CP_TYPE_CONST_P (TREE_TYPE (TREE_VEC_ELT (args, i))))
14556 : 307 : return false;
14557 : :
14558 : : return true;
14559 : : }
14560 : :
14561 : : /* Return true if a class T has a reference member. */
14562 : :
14563 : : static bool
14564 : 216 : class_has_reference_member_p (tree t)
14565 : : {
14566 : 216 : for (tree fields = TYPE_FIELDS (t);
14567 : 3361 : fields;
14568 : 3145 : fields = DECL_CHAIN (fields))
14569 : 3208 : if (TREE_CODE (fields) == FIELD_DECL
14570 : 196 : && !DECL_ARTIFICIAL (fields)
14571 : 3375 : && TYPE_REF_P (TREE_TYPE (fields)))
14572 : : return true;
14573 : : return false;
14574 : : }
14575 : :
14576 : : /* A wrapper for the above suitable as a callback for dfs_walk_once. */
14577 : :
14578 : : static tree
14579 : 216 : class_has_reference_member_p_r (tree binfo, void *)
14580 : : {
14581 : 216 : return (class_has_reference_member_p (BINFO_TYPE (binfo))
14582 : 216 : ? integer_one_node : NULL_TREE);
14583 : : }
14584 : :
14585 : :
14586 : : /* Return true if T (either a class or a function) has been marked as
14587 : : not-dangling. */
14588 : :
14589 : : static bool
14590 : 1522 : no_dangling_p (tree t)
14591 : : {
14592 : 1522 : t = lookup_attribute ("no_dangling", TYPE_ATTRIBUTES (t));
14593 : 1522 : if (!t)
14594 : : return false;
14595 : :
14596 : 75 : t = TREE_VALUE (t);
14597 : 75 : if (!t)
14598 : : return true;
14599 : :
14600 : 51 : t = build_converted_constant_bool_expr (TREE_VALUE (t), tf_warning_or_error);
14601 : 51 : t = cxx_constant_value (t);
14602 : 51 : return t == boolean_true_node;
14603 : : }
14604 : :
14605 : : /* Return true if a class CTYPE is either std::reference_wrapper or
14606 : : std::ref_view, or a reference wrapper class. We consider a class
14607 : : a reference wrapper class if it has a reference member. We no
14608 : : longer check that it has a constructor taking the same reference type
14609 : : since that approach still generated too many false positives. */
14610 : :
14611 : : static bool
14612 : 446 : reference_like_class_p (tree ctype)
14613 : : {
14614 : 446 : if (!CLASS_TYPE_P (ctype))
14615 : : return false;
14616 : :
14617 : 314 : if (no_dangling_p (ctype))
14618 : : return true;
14619 : :
14620 : : /* Also accept a std::pair<const T&, const T&>. */
14621 : 290 : if (std_pair_ref_ref_p (ctype))
14622 : : return true;
14623 : :
14624 : 275 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (ctype));
14625 : 275 : if (decl_in_std_namespace_p (tdecl))
14626 : : {
14627 : 38 : tree name = DECL_NAME (tdecl);
14628 : 38 : if (name
14629 : 38 : && (id_equal (name, "reference_wrapper")
14630 : 26 : || id_equal (name, "span")
14631 : 11 : || id_equal (name, "ref_view")))
14632 : : return true;
14633 : : }
14634 : :
14635 : : /* Avoid warning if CTYPE looks like std::span: it has a T* member and
14636 : : a trivial destructor. For example,
14637 : :
14638 : : template<typename T>
14639 : : struct Span {
14640 : : T* data_;
14641 : : std::size len_;
14642 : : };
14643 : :
14644 : : is considered std::span-like. */
14645 : 248 : if (NON_UNION_CLASS_TYPE_P (ctype) && TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
14646 : 228 : for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
14647 : 367 : field; field = next_aggregate_field (DECL_CHAIN (field)))
14648 : 199 : if (TYPE_PTR_P (TREE_TYPE (field)))
14649 : : return true;
14650 : :
14651 : : /* Some classes, such as std::tuple, have the reference member in its
14652 : : (non-direct) base class. */
14653 : 188 : if (dfs_walk_once (TYPE_BINFO (ctype), class_has_reference_member_p_r,
14654 : : nullptr, nullptr))
14655 : : return true;
14656 : :
14657 : : return false;
14658 : : }
14659 : :
14660 : : /* Helper for maybe_warn_dangling_reference to find a problematic temporary
14661 : : in EXPR (as outlined in maybe_warn_dangling_reference), or NULL_TREE
14662 : : if none found. For instance:
14663 : :
14664 : : const S& s = S().self(); // S()
14665 : : const int& r = (42, f(1)); // temporary for passing 1 to f
14666 : : const int& t = b ? f(1) : f(2); // temporary for 1
14667 : : const int& u = b ? f(1) : f(g); // temporary for 1
14668 : : const int& v = b ? f(g) : f(2); // temporary for 2
14669 : : const int& w = b ? f(g) : f(g); // NULL_TREE
14670 : : const int& y = (f(1), 42); // NULL_TREE
14671 : : const int& z = f(f(1)); // temporary for 1
14672 : :
14673 : : EXPR is the initializer. If ARG_P is true, we're processing an argument
14674 : : to a function; the point is to distinguish between, for example,
14675 : :
14676 : : Ref::inner (&TARGET_EXPR <D.2839, F::foo (fm)>)
14677 : :
14678 : : where we shouldn't warn, and
14679 : :
14680 : : Ref::inner (&TARGET_EXPR <D.2908, F::foo (&TARGET_EXPR <...>)>)
14681 : :
14682 : : where we should warn (Ref is a reference_like_class_p so we see through
14683 : : it. */
14684 : :
14685 : : static tree
14686 : 3786 : do_warn_dangling_reference (tree expr, bool arg_p)
14687 : : {
14688 : 3786 : STRIP_NOPS (expr);
14689 : :
14690 : 3786 : if (arg_p && expr_represents_temporary_p (expr))
14691 : : {
14692 : : /* An attempt to reduce the number of -Wdangling-reference
14693 : : false positives concerning reference wrappers (c++/107532).
14694 : : When we encounter a reference_like_class_p, we don't warn
14695 : : just yet; instead, we keep recursing to see if there were
14696 : : any temporaries behind the reference-wrapper class. */
14697 : : tree e = expr;
14698 : 354 : while (handled_component_p (e))
14699 : 15 : e = TREE_OPERAND (e, 0);
14700 : 339 : tree type = TREE_TYPE (e);
14701 : : /* If the temporary represents a lambda, we don't really know
14702 : : what's going on here. */
14703 : 461 : if (!reference_like_class_p (type) && !LAMBDA_TYPE_P (type))
14704 : : return expr;
14705 : : }
14706 : :
14707 : 3554 : switch (TREE_CODE (expr))
14708 : : {
14709 : 1777 : case CALL_EXPR:
14710 : 1777 : {
14711 : 1777 : tree fndecl = cp_get_callee_fndecl_nofold (expr);
14712 : 1777 : if (!fndecl
14713 : 1777 : || warning_suppressed_p (fndecl, OPT_Wdangling_reference)
14714 : 1766 : || !warning_enabled_at (DECL_SOURCE_LOCATION (fndecl),
14715 : 1766 : OPT_Wdangling_reference)
14716 : : /* Don't emit a false positive for:
14717 : : std::vector<int> v = ...;
14718 : : std::vector<int>::const_iterator it = v.begin();
14719 : : const int &r = *it++;
14720 : : because R refers to one of the int elements of V, not to
14721 : : a temporary object. Member operator* may return a reference
14722 : : but probably not to one of its arguments. */
14723 : 1419 : || (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
14724 : 849 : && DECL_OVERLOADED_OPERATOR_P (fndecl)
14725 : 320 : && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF))
14726 : 2985 : || no_dangling_p (TREE_TYPE (fndecl)))
14727 : 593 : return NULL_TREE;
14728 : :
14729 : 1184 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
14730 : : /* If the function doesn't return a reference, don't warn. This
14731 : : can be e.g.
14732 : : const int& z = std::min({1, 2, 3, 4, 5, 6, 7});
14733 : : which doesn't dangle: std::min here returns an int.
14734 : :
14735 : : If the function returns a std::pair<const T&, const T&>, we
14736 : : warn, to detect e.g.
14737 : : std::pair<const int&, const int&> v = std::minmax(1, 2);
14738 : : which also creates a dangling reference, because std::minmax
14739 : : returns std::pair<const T&, const T&>(b, a). */
14740 : 1184 : if (!(TYPE_REF_OBJ_P (rettype) || reference_like_class_p (rettype)))
14741 : : return NULL_TREE;
14742 : :
14743 : : /* Here we're looking to see if any of the arguments is a temporary
14744 : : initializing a reference parameter. */
14745 : 1584 : for (int i = 0; i < call_expr_nargs (expr); ++i)
14746 : : {
14747 : 1206 : tree arg = CALL_EXPR_ARG (expr, i);
14748 : : /* Check that this argument initializes a reference, except for
14749 : : the argument initializing the object of a member function. */
14750 : 1206 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)
14751 : 1206 : && !TYPE_REF_P (TREE_TYPE (arg)))
14752 : 130 : continue;
14753 : 1076 : STRIP_NOPS (arg);
14754 : 1076 : if (TREE_CODE (arg) == ADDR_EXPR)
14755 : 671 : arg = TREE_OPERAND (arg, 0);
14756 : : /* Recurse to see if the argument is a temporary. It could also
14757 : : be another call taking a temporary and returning it and
14758 : : initializing this reference parameter. */
14759 : 1076 : if ((arg = do_warn_dangling_reference (arg, /*arg_p=*/true)))
14760 : : {
14761 : : /* If we know the temporary could not bind to the return type,
14762 : : don't warn. This is for scalars and empty classes only
14763 : : because for other classes we can't be sure we are not
14764 : : returning its sub-object. */
14765 : 277 : if ((SCALAR_TYPE_P (TREE_TYPE (arg))
14766 : 152 : || is_empty_class (TREE_TYPE (arg)))
14767 : 152 : && TYPE_REF_P (rettype)
14768 : 137 : && !reference_related_p (TREE_TYPE (rettype),
14769 : 137 : TREE_TYPE (arg)))
14770 : 21 : continue;
14771 : 256 : return arg;
14772 : : }
14773 : : /* Don't warn about member functions like:
14774 : : std::any a(...);
14775 : : S& s = a.emplace<S>({0}, 0);
14776 : : which construct a new object and return a reference to it, but
14777 : : we still want to detect:
14778 : : struct S { const S& self () { return *this; } };
14779 : : const S& s = S().self();
14780 : : where 's' dangles. If we've gotten here, the object this function
14781 : : is invoked on is not a temporary. */
14782 : 799 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl))
14783 : : break;
14784 : : }
14785 : : return NULL_TREE;
14786 : : }
14787 : 28 : case COMPOUND_EXPR:
14788 : 28 : return do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p);
14789 : 31 : case COND_EXPR:
14790 : 31 : if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p))
14791 : : return t;
14792 : 16 : return do_warn_dangling_reference (TREE_OPERAND (expr, 2), arg_p);
14793 : 0 : case PAREN_EXPR:
14794 : 0 : return do_warn_dangling_reference (TREE_OPERAND (expr, 0), arg_p);
14795 : 122 : case TARGET_EXPR:
14796 : 122 : return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr), arg_p);
14797 : : default:
14798 : : return NULL_TREE;
14799 : : }
14800 : : }
14801 : :
14802 : : /* Implement -Wdangling-reference, to detect cases like
14803 : :
14804 : : int n = 1;
14805 : : const int& r = std::max(n - 1, n + 1); // r is dangling
14806 : :
14807 : : This creates temporaries from the arguments, returns a reference to
14808 : : one of the temporaries, but both temporaries are destroyed at the end
14809 : : of the full expression.
14810 : :
14811 : : This works by checking if a reference is initialized with a function
14812 : : that returns a reference, and at least one parameter of the function
14813 : : is a reference that is bound to a temporary. It assumes that such a
14814 : : function actually returns one of its arguments.
14815 : :
14816 : : DECL is the reference being initialized, INIT is the initializer. */
14817 : :
14818 : : static void
14819 : 61095935 : maybe_warn_dangling_reference (const_tree decl, tree init)
14820 : : {
14821 : 61095935 : if (!warn_dangling_reference)
14822 : : return;
14823 : 536249 : tree type = TREE_TYPE (decl);
14824 : : /* Only warn if what we're initializing has type T&& or const T&, or
14825 : : std::pair<const T&, const T&>. (A non-const lvalue reference can't
14826 : : bind to a temporary.) */
14827 : 1070000 : if (!((TYPE_REF_OBJ_P (type)
14828 : 5396 : && (TYPE_REF_IS_RVALUE (type)
14829 : 5016 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
14830 : 533751 : || std_pair_ref_ref_p (type)))
14831 : : return;
14832 : : /* Don't suppress the diagnostic just because the call comes from
14833 : : a system header. If the DECL is not in a system header, or if
14834 : : -Wsystem-headers was provided, warn. */
14835 : 2513 : auto wsh
14836 : 2513 : = make_temp_override (global_dc->m_warn_system_headers,
14837 : 2513 : (!in_system_header_at (DECL_SOURCE_LOCATION (decl))
14838 : 2513 : || global_dc->m_warn_system_headers));
14839 : 2513 : if (tree call = do_warn_dangling_reference (init, /*arg_p=*/false))
14840 : : {
14841 : 211 : auto_diagnostic_group d;
14842 : 211 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference,
14843 : : "possibly dangling reference to a temporary"))
14844 : 211 : inform (EXPR_LOCATION (call), "%qT temporary created here",
14845 : 211 : TREE_TYPE (call));
14846 : 211 : }
14847 : 2513 : }
14848 : :
14849 : : /* If *P is an xvalue expression, prevent temporary lifetime extension if it
14850 : : gets used to initialize a reference. */
14851 : :
14852 : : static tree
14853 : 86 : prevent_lifetime_extension (tree t)
14854 : : {
14855 : 86 : tree *p = &t;
14856 : 86 : while (TREE_CODE (*p) == COMPOUND_EXPR)
14857 : 0 : p = &TREE_OPERAND (*p, 1);
14858 : 95 : while (handled_component_p (*p))
14859 : 9 : p = &TREE_OPERAND (*p, 0);
14860 : : /* Change a TARGET_EXPR from prvalue to xvalue. */
14861 : 86 : if (TREE_CODE (*p) == TARGET_EXPR)
14862 : 3 : *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p,
14863 : 3 : move (TARGET_EXPR_SLOT (*p)));
14864 : 86 : return t;
14865 : : }
14866 : :
14867 : : /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
14868 : : which is bound either to a reference or a std::initializer_list. */
14869 : :
14870 : : static tree
14871 : 750788 : extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups,
14872 : : tree *cond_guard)
14873 : : {
14874 : : /* CWG1299 (C++20): The temporary object to which the reference is bound or
14875 : : the temporary object that is the complete object of a subobject to which
14876 : : the reference is bound persists for the lifetime of the reference if the
14877 : : glvalue to which the reference is bound was obtained through one of the
14878 : : following:
14879 : : - a temporary materialization conversion ([conv.rval]),
14880 : : - ( expression ), where expression is one of these expressions,
14881 : : - subscripting ([expr.sub]) of an array operand, where that operand is one
14882 : : of these expressions,
14883 : : - a class member access ([expr.ref]) using the . operator where the left
14884 : : operand is one of these expressions and the right operand designates a
14885 : : non-static data member of non-reference type,
14886 : : - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator
14887 : : where the left operand is one of these expressions and the right operand
14888 : : is a pointer to data member of non-reference type,
14889 : : - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]),
14890 : : dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast
14891 : : ([expr.reinterpret.cast]) converting, without a user-defined conversion,
14892 : : a glvalue operand that is one of these expressions to a glvalue that
14893 : : refers to the object designated by the operand, or to its complete
14894 : : object or a subobject thereof,
14895 : : - a conditional expression ([expr.cond]) that is a glvalue where the
14896 : : second or third operand is one of these expressions, or
14897 : : - a comma expression ([expr.comma]) that is a glvalue where the right
14898 : : operand is one of these expressions. */
14899 : :
14900 : : /* FIXME several cases are still handled wrong (101572, 81420). */
14901 : :
14902 : 750788 : tree sub = init;
14903 : 750788 : tree *p;
14904 : 750788 : STRIP_NOPS (sub);
14905 : 750788 : if (TREE_CODE (sub) == COMPOUND_EXPR)
14906 : : {
14907 : 313 : TREE_OPERAND (sub, 1)
14908 : 313 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14909 : : cond_guard);
14910 : 313 : return init;
14911 : : }
14912 : 750475 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14913 : 750475 : && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions
14914 : : (TREE_OPERAND (sub, 1)))))
14915 : : {
14916 : : /* A pointer-to-member operation. */
14917 : 6 : TREE_OPERAND (sub, 0)
14918 : 6 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 0), cleanups,
14919 : : cond_guard);
14920 : 6 : return init;
14921 : : }
14922 : 750469 : if (TREE_CODE (sub) == COND_EXPR)
14923 : : {
14924 : 22816 : tree cur_cond_guard = NULL_TREE;
14925 : 22816 : if (TREE_OPERAND (sub, 1))
14926 : 22816 : TREE_OPERAND (sub, 1)
14927 : 45632 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14928 : : &cur_cond_guard);
14929 : 22816 : if (cur_cond_guard)
14930 : : {
14931 : 9 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14932 : : NOP_EXPR, boolean_true_node,
14933 : : tf_warning_or_error);
14934 : 9 : TREE_OPERAND (sub, 1)
14935 : 18 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 1),
14936 : : tf_warning_or_error);
14937 : : }
14938 : 22816 : cur_cond_guard = NULL_TREE;
14939 : 22816 : if (TREE_OPERAND (sub, 2))
14940 : 22816 : TREE_OPERAND (sub, 2)
14941 : 45632 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 2), cleanups,
14942 : : &cur_cond_guard);
14943 : 22816 : if (cur_cond_guard)
14944 : : {
14945 : 12 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14946 : : NOP_EXPR, boolean_true_node,
14947 : : tf_warning_or_error);
14948 : 12 : TREE_OPERAND (sub, 2)
14949 : 24 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 2),
14950 : : tf_warning_or_error);
14951 : : }
14952 : 22816 : return init;
14953 : : }
14954 : 727653 : if (TREE_CODE (sub) != ADDR_EXPR)
14955 : : return init;
14956 : : /* Deal with binding to a subobject. */
14957 : 317557 : for (p = &TREE_OPERAND (sub, 0);
14958 : 363437 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
14959 : 45880 : p = &TREE_OPERAND (*p, 0);
14960 : 317557 : if (TREE_CODE (*p) == TARGET_EXPR)
14961 : : {
14962 : 9760 : tree subinit = NULL_TREE;
14963 : 9760 : *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit,
14964 : : cond_guard, nullptr);
14965 : 9760 : recompute_tree_invariant_for_addr_expr (sub);
14966 : 9760 : if (init != sub)
14967 : 9742 : init = fold_convert (TREE_TYPE (init), sub);
14968 : 9760 : if (subinit)
14969 : 8849 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
14970 : : }
14971 : : return init;
14972 : : }
14973 : :
14974 : : /* Data for extend_temps_r, mostly matching the parameters of
14975 : : extend_ref_init_temps. */
14976 : :
14977 : : struct extend_temps_data
14978 : : {
14979 : : tree decl;
14980 : : tree init;
14981 : : vec<tree, va_gc> **cleanups;
14982 : : tree* cond_guard;
14983 : : hash_set<tree> *pset; // For avoiding redundant walk_tree.
14984 : : hash_map<tree, tree> *var_map; // For remapping extended temps.
14985 : : };
14986 : :
14987 : : /* Tree walk function for extend_all_temps. Generally parallel to
14988 : : extend_ref_init_temps_1, but adapted for walk_tree. */
14989 : :
14990 : : tree
14991 : 94610 : extend_temps_r (tree *tp, int *walk_subtrees, void *data)
14992 : : {
14993 : 94610 : extend_temps_data *d = (extend_temps_data *)data;
14994 : :
14995 : 94610 : if (TREE_CODE (*tp) == VAR_DECL)
14996 : : {
14997 : 8642 : if (tree *r = d->var_map->get (*tp))
14998 : 0 : *tp = *r;
14999 : 8642 : return NULL_TREE;
15000 : : }
15001 : :
15002 : 85957 : if (TYPE_P (*tp) || TREE_CODE (*tp) == CLEANUP_POINT_EXPR
15003 : 171924 : || d->pset->add (*tp))
15004 : : {
15005 : 2670 : *walk_subtrees = 0;
15006 : 2670 : return NULL_TREE;
15007 : : }
15008 : :
15009 : 83298 : if (TREE_CODE (*tp) == COND_EXPR)
15010 : : {
15011 : 8 : cp_walk_tree (&TREE_OPERAND (*tp, 0), extend_temps_r, d, nullptr);
15012 : :
15013 : 24 : auto walk_arm = [d](tree &op)
15014 : : {
15015 : 16 : tree cur_cond_guard = NULL_TREE;
15016 : 16 : auto ov = make_temp_override (d->cond_guard, &cur_cond_guard);
15017 : 16 : cp_walk_tree (&op, extend_temps_r, d, nullptr);
15018 : 16 : if (cur_cond_guard)
15019 : : {
15020 : 2 : tree set = build2 (MODIFY_EXPR, boolean_type_node,
15021 : : cur_cond_guard, boolean_true_node);
15022 : 2 : op = cp_build_compound_expr (set, op, tf_none);
15023 : : }
15024 : 24 : };
15025 : 8 : walk_arm (TREE_OPERAND (*tp, 1));
15026 : 8 : walk_arm (TREE_OPERAND (*tp, 2));
15027 : :
15028 : 8 : *walk_subtrees = 0;
15029 : 8 : return NULL_TREE;
15030 : : }
15031 : :
15032 : 83290 : tree *p = tp;
15033 : :
15034 : 83290 : if (TREE_CODE (*tp) == ADDR_EXPR)
15035 : 18429 : for (p = &TREE_OPERAND (*tp, 0);
15036 : 20298 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
15037 : 1869 : p = &TREE_OPERAND (*p, 0);
15038 : :
15039 : 83290 : if (TREE_CODE (*p) == TARGET_EXPR
15040 : : /* An eliding TARGET_EXPR isn't a temporary at all. */
15041 : 1363 : && !TARGET_EXPR_ELIDING_P (*p)
15042 : : /* A TARGET_EXPR with TARGET_EXPR_INTERNAL_P is an artificial variable
15043 : : used during initialization that need not be extended. */
15044 : 84577 : && !TARGET_EXPR_INTERNAL_P (*p))
15045 : : {
15046 : : /* A CLEANUP_EH_ONLY expr should also have TARGET_EXPR_INTERNAL_P. */
15047 : 980 : gcc_checking_assert (!CLEANUP_EH_ONLY (*p));
15048 : :
15049 : 980 : tree subinit = NULL_TREE;
15050 : 980 : tree slot = TARGET_EXPR_SLOT (*p);
15051 : 980 : *p = set_up_extended_ref_temp (d->decl, *p, d->cleanups, &subinit,
15052 : : d->cond_guard, d);
15053 : 980 : if (TREE_CODE (*tp) == ADDR_EXPR)
15054 : 966 : recompute_tree_invariant_for_addr_expr (*tp);
15055 : 980 : if (subinit)
15056 : 898 : *tp = cp_build_compound_expr (subinit, *tp, tf_none);
15057 : 980 : d->var_map->put (slot, *p);
15058 : : }
15059 : :
15060 : : return NULL_TREE;
15061 : : }
15062 : :
15063 : : /* Extend all the temporaries in a for-range-initializer. */
15064 : :
15065 : : static tree
15066 : 23765 : extend_all_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
15067 : : {
15068 : 23765 : hash_set<tree> pset;
15069 : 23765 : hash_map<tree, tree> map;
15070 : 23765 : gcc_assert (!TREE_STATIC (decl));
15071 : 23765 : extend_temps_data d = { decl, init, cleanups, nullptr, &pset, &map };
15072 : 23765 : cp_walk_tree (&init, extend_temps_r, &d, nullptr);
15073 : 23765 : return init;
15074 : 23765 : }
15075 : :
15076 : : /* INIT is part of the initializer for DECL. If there are any
15077 : : reference or initializer lists being initialized, extend their
15078 : : lifetime to match that of DECL. */
15079 : :
15080 : : tree
15081 : 62907195 : extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups,
15082 : : tree *cond_guard)
15083 : : {
15084 : 62907195 : tree type = TREE_TYPE (init);
15085 : 62907195 : if (processing_template_decl)
15086 : : return init;
15087 : :
15088 : : /* P2718R0 - in C++23 for-range-initializer, extend all temps. */
15089 : 61119700 : if (DECL_NAME (decl) == for_range__identifier
15090 : 69520 : && flag_range_for_ext_temps
15091 : : /* Iterating expansion statement decl is static right now, but that
15092 : : could change depending on CWG3044 and CWG3043. */
15093 : 61143498 : && !TREE_STATIC (decl))
15094 : : {
15095 : 23765 : gcc_checking_assert (!cond_guard);
15096 : 23765 : return extend_all_temps (decl, init, cleanups);
15097 : : }
15098 : :
15099 : 61095935 : maybe_warn_dangling_reference (decl, init);
15100 : :
15101 : 61095935 : if (TYPE_REF_P (type))
15102 : 704264 : init = extend_ref_init_temps_1 (decl, init, cleanups, cond_guard);
15103 : : else
15104 : : {
15105 : 60391671 : tree ctor = init;
15106 : 60391671 : if (TREE_CODE (ctor) == TARGET_EXPR)
15107 : 1048328 : ctor = TARGET_EXPR_INITIAL (ctor);
15108 : 60391671 : if (TREE_CODE (ctor) == CONSTRUCTOR)
15109 : : {
15110 : : /* [dcl.init] When initializing an aggregate from a parenthesized list
15111 : : of values... a temporary object bound to a reference does not have
15112 : : its lifetime extended. */
15113 : 4612195 : if (CONSTRUCTOR_IS_PAREN_INIT (ctor))
15114 : : return init;
15115 : :
15116 : 4611906 : if (is_std_init_list (type))
15117 : : {
15118 : : /* The temporary array underlying a std::initializer_list
15119 : : is handled like a reference temporary. */
15120 : 573 : tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
15121 : 573 : array = extend_ref_init_temps_1 (decl, array, cleanups,
15122 : : cond_guard);
15123 : 573 : CONSTRUCTOR_ELT (ctor, 0)->value = array;
15124 : : }
15125 : : else
15126 : : {
15127 : 4611333 : unsigned i;
15128 : 4611333 : constructor_elt *p;
15129 : 4611333 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
15130 : 29265502 : FOR_EACH_VEC_SAFE_ELT (elts, i, p)
15131 : 24654169 : p->value = extend_ref_init_temps (decl, p->value, cleanups,
15132 : : cond_guard);
15133 : : }
15134 : 4611906 : recompute_constructor_flags (ctor);
15135 : 4611906 : if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
15136 : 2107846 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
15137 : : }
15138 : : }
15139 : :
15140 : : return init;
15141 : : }
15142 : :
15143 : : /* Returns true iff an initializer for TYPE could contain temporaries that
15144 : : need to be extended because they are bound to references or
15145 : : std::initializer_list. */
15146 : :
15147 : : bool
15148 : 6276 : type_has_extended_temps (tree type)
15149 : : {
15150 : 6276 : type = strip_array_types (type);
15151 : 6276 : if (TYPE_REF_P (type))
15152 : : return true;
15153 : 6219 : if (CLASS_TYPE_P (type))
15154 : : {
15155 : 3319 : if (is_std_init_list (type))
15156 : : return true;
15157 : 3316 : for (tree f = next_aggregate_field (TYPE_FIELDS (type));
15158 : 7972 : f; f = next_aggregate_field (DECL_CHAIN (f)))
15159 : 4785 : if (type_has_extended_temps (TREE_TYPE (f)))
15160 : : return true;
15161 : : }
15162 : : return false;
15163 : : }
15164 : :
15165 : : /* Returns true iff TYPE is some variant of std::initializer_list. */
15166 : :
15167 : : bool
15168 : 143498714 : is_std_init_list (tree type)
15169 : : {
15170 : 143498714 : if (!TYPE_P (type))
15171 : : return false;
15172 : 143498691 : if (cxx_dialect == cxx98)
15173 : : return false;
15174 : : /* Look through typedefs. */
15175 : 143156657 : type = TYPE_MAIN_VARIANT (type);
15176 : 112894144 : return (CLASS_TYPE_P (type)
15177 : 112754325 : && CP_TYPE_CONTEXT (type) == std_node
15178 : 213576157 : && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
15179 : : }
15180 : :
15181 : : /* Returns true iff DECL is a list constructor: i.e. a constructor which
15182 : : will accept an argument list of a single std::initializer_list<T>. */
15183 : :
15184 : : bool
15185 : 124049410 : is_list_ctor (tree decl)
15186 : : {
15187 : 124049410 : tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
15188 : 124049410 : tree arg;
15189 : :
15190 : 124049410 : if (!args || args == void_list_node)
15191 : : return false;
15192 : :
15193 : 100763560 : arg = non_reference (TREE_VALUE (args));
15194 : 100763560 : if (!is_std_init_list (arg))
15195 : : return false;
15196 : :
15197 : 2145698 : args = TREE_CHAIN (args);
15198 : :
15199 : 3645131 : if (args && args != void_list_node && !TREE_PURPOSE (args))
15200 : : /* There are more non-defaulted parms. */
15201 : : return false;
15202 : :
15203 : : return true;
15204 : : }
15205 : :
15206 : : /* We know that can_convert_arg_bad already said "no" when trying to convert
15207 : : FROM to TO with ARG and FLAGS. Try to figure out if it was because
15208 : : an explicit conversion function was skipped when looking for a way to
15209 : : perform the conversion. At this point we've already printed an error. */
15210 : :
15211 : : void
15212 : 1256 : maybe_show_nonconverting_candidate (tree to, tree from, tree arg, int flags)
15213 : : {
15214 : 1256 : if (!(flags & LOOKUP_ONLYCONVERTING))
15215 : 154 : return;
15216 : :
15217 : 1102 : conversion_obstack_sentinel cos;
15218 : 1102 : conversion *c = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
15219 : : flags & ~LOOKUP_ONLYCONVERTING, tf_none);
15220 : 1102 : if (c && !c->bad_p && c->user_conv_p)
15221 : : /* Ay, the conversion would have worked in direct-init context. */
15222 : 258 : for (; c; c = next_conversion (c))
15223 : 172 : if (c->kind == ck_user
15224 : 86 : && DECL_P (c->cand->fn)
15225 : 258 : && DECL_NONCONVERTING_P (c->cand->fn))
15226 : 86 : inform (DECL_SOURCE_LOCATION (c->cand->fn), "explicit conversion "
15227 : : "function was not considered");
15228 : 1102 : }
15229 : :
15230 : : /* We're converting EXPR to TYPE. If that conversion involves a conversion
15231 : : function and we're binding EXPR to a reference parameter of that function,
15232 : : return true. */
15233 : :
15234 : : bool
15235 : 171 : conv_binds_to_reference_parm_p (tree type, tree expr)
15236 : : {
15237 : 171 : conversion_obstack_sentinel cos;
15238 : 171 : conversion *c = implicit_conversion (type, TREE_TYPE (expr), expr,
15239 : : /*c_cast_p=*/false, LOOKUP_NORMAL,
15240 : : tf_none);
15241 : 171 : if (c && !c->bad_p && c->user_conv_p)
15242 : 117 : for (; c; c = next_conversion (c))
15243 : 81 : if (c->kind == ck_user)
15244 : 244 : for (z_candidate *cand = c->cand; cand; cand = cand->next)
15245 : 208 : if (cand->viable == 1)
15246 : 81 : for (size_t i = 0; i < cand->num_convs; ++i)
15247 : 45 : if (cand->convs[i]->kind == ck_ref_bind
15248 : 45 : && conv_get_original_expr (cand->convs[i]) == expr)
15249 : : return true;
15250 : :
15251 : : return false;
15252 : 171 : }
15253 : :
15254 : : #include "gt-cp-call.h"
|