1 /* Functions related to invoking -*- C++ -*- methods and overloaded functions.
2 Copyright (C) 1987-2018 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
45 /* The various kinds of conversion. */
46
47 enum conversion_kind {
48 ck_identity,
49 ck_lvalue,
50 ck_fnptr,
51 ck_qual,
52 ck_std,
53 ck_ptr,
54 ck_pmem,
55 ck_base,
56 ck_ref_bind,
57 ck_user,
58 ck_ambig,
59 ck_list,
60 ck_aggr,
61 ck_rvalue
62 };
63
64 /* The rank of the conversion. Order of the enumerals matters; better
65 conversions should come earlier in the list. */
66
67 enum conversion_rank {
68 cr_identity,
69 cr_exact,
70 cr_promotion,
71 cr_std,
72 cr_pbool,
73 cr_user,
74 cr_ellipsis,
75 cr_bad
76 };
77
78 /* An implicit conversion sequence, in the sense of [over.best.ics].
79 The first conversion to be performed is at the end of the chain.
80 That conversion is always a cr_identity conversion. */
81
82 struct conversion {
83 /* The kind of conversion represented by this step. */
84 conversion_kind kind;
85 /* The rank of this conversion. */
86 conversion_rank rank;
87 BOOL_BITFIELD user_conv_p : 1;
88 BOOL_BITFIELD ellipsis_p : 1;
89 BOOL_BITFIELD this_p : 1;
90 /* True if this conversion would be permitted with a bending of
91 language standards, e.g. disregarding pointer qualifiers or
92 converting integers to pointers. */
93 BOOL_BITFIELD bad_p : 1;
94 /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
95 temporary should be created to hold the result of the
96 conversion. If KIND is ck_ambig, true if the context is
97 copy-initialization. */
98 BOOL_BITFIELD need_temporary_p : 1;
99 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
100 from a pointer-to-derived to pointer-to-base is being performed. */
101 BOOL_BITFIELD base_p : 1;
102 /* If KIND is ck_ref_bind, true when either an lvalue reference is
103 being bound to an lvalue expression or an rvalue reference is
104 being bound to an rvalue expression. If KIND is ck_rvalue,
105 true when we are treating an lvalue as an rvalue (12.8p33). If
106 KIND is ck_base, always false. If ck_identity, we will be
107 binding a reference directly or decaying to a pointer. */
108 BOOL_BITFIELD rvaluedness_matches_p: 1;
109 BOOL_BITFIELD check_narrowing: 1;
110 /* The type of the expression resulting from the conversion. */
111 tree type;
112 union {
113 /* The next conversion in the chain. Since the conversions are
114 arranged from outermost to innermost, the NEXT conversion will
115 actually be performed before this conversion. This variant is
116 used only when KIND is neither ck_identity, ck_ambig nor
117 ck_list. Please use the next_conversion function instead
118 of using this field directly. */
119 conversion *next;
120 /* The expression at the beginning of the conversion chain. This
121 variant is used only if KIND is ck_identity or ck_ambig. */
122 tree expr;
123 /* The array of conversions for an initializer_list, so this
124 variant is used only when KIN D is ck_list. */
125 conversion **list;
126 } u;
127 /* The function candidate corresponding to this conversion
128 sequence. This field is only used if KIND is ck_user. */
129 struct z_candidate *cand;
130 };
131
132 #define CONVERSION_RANK(NODE) \
133 ((NODE)->bad_p ? cr_bad \
134 : (NODE)->ellipsis_p ? cr_ellipsis \
135 : (NODE)->user_conv_p ? cr_user \
136 : (NODE)->rank)
137
138 #define BAD_CONVERSION_RANK(NODE) \
139 ((NODE)->ellipsis_p ? cr_ellipsis \
140 : (NODE)->user_conv_p ? cr_user \
141 : (NODE)->rank)
142
143 static struct obstack conversion_obstack;
144 static bool conversion_obstack_initialized;
145 struct rejection_reason;
146
147 static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
148 static int equal_functions (tree, tree);
149 static int joust (struct z_candidate *, struct z_candidate *, bool,
150 tsubst_flags_t);
151 static int compare_ics (conversion *, conversion *);
152 static void maybe_warn_class_memaccess (location_t, tree,
153 const vec<tree, va_gc> *);
154 static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
155 #define convert_like(CONV, EXPR, COMPLAIN) \
156 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, \
157 /*issue_conversion_warnings=*/true, \
158 /*c_cast_p=*/false, (COMPLAIN))
159 #define convert_like_with_context(CONV, EXPR, FN, ARGNO, COMPLAIN ) \
160 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), \
161 /*issue_conversion_warnings=*/true, \
162 /*c_cast_p=*/false, (COMPLAIN))
163 static tree convert_like_real (conversion *, tree, tree, int, bool,
164 bool, tsubst_flags_t);
165 static void op_error (location_t, enum tree_code, enum tree_code, tree,
166 tree, tree, bool);
167 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
168 tsubst_flags_t);
169 static void print_z_candidate (location_t, const char *, struct z_candidate *);
170 static void print_z_candidates (location_t, struct z_candidate *);
171 static tree build_this (tree);
172 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
173 static bool any_strictly_viable (struct z_candidate *);
174 static struct z_candidate *add_template_candidate
175 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
176 tree, tree, tree, int, unification_kind_t, tsubst_flags_t);
177 static struct z_candidate *add_template_candidate_real
178 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
179 tree, tree, tree, int, tree, unification_kind_t, tsubst_flags_t);
180 static void add_builtin_candidates
181 (struct z_candidate **, enum tree_code, enum tree_code,
182 tree, tree *, int, tsubst_flags_t);
183 static void add_builtin_candidate
184 (struct z_candidate **, enum tree_code, enum tree_code,
185 tree, tree, tree, tree *, tree *, int, tsubst_flags_t);
186 static bool is_complete (tree);
187 static void build_builtin_candidate
188 (struct z_candidate **, tree, tree, tree, tree *, tree *,
189 int, tsubst_flags_t);
190 static struct z_candidate *add_conv_candidate
191 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, tree,
192 tree, tsubst_flags_t);
193 static struct z_candidate *add_function_candidate
194 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
195 tree, int, tsubst_flags_t);
196 static conversion *implicit_conversion (tree, tree, tree, bool, int,
197 tsubst_flags_t);
198 static conversion *reference_binding (tree, tree, tree, bool, int,
199 tsubst_flags_t);
200 static conversion *build_conv (conversion_kind, tree, conversion *);
201 static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
202 static conversion *next_conversion (conversion *);
203 static bool is_subseq (conversion *, conversion *);
204 static conversion *maybe_handle_ref_bind (conversion **);
205 static void maybe_handle_implicit_object (conversion **);
206 static struct z_candidate *add_candidate
207 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
208 conversion **, tree, tree, int, struct rejection_reason *, int);
209 static tree source_type (conversion *);
210 static void add_warning (struct z_candidate *, struct z_candidate *);
211 static bool reference_compatible_p (tree, tree);
212 static conversion *direct_reference_binding (tree, conversion *);
213 static bool promoted_arithmetic_type_p (tree);
214 static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
215 static char *name_as_c_string (tree, tree, bool *);
216 static tree prep_operand (tree);
217 static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
218 bool, tree, tree, int, struct z_candidate **,
219 tsubst_flags_t);
220 static conversion *merge_conversion_sequences (conversion *, conversion *);
221 static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
222
223 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
224 NAME can take many forms... */
225
226 bool
check_dtor_name(tree basetype,tree name)227 check_dtor_name (tree basetype, tree name)
228 {
229 /* Just accept something we've already complained about. */
230 if (name == error_mark_node)
231 return true;
232
233 if (TREE_CODE (name) == TYPE_DECL)
234 name = TREE_TYPE (name);
235 else if (TYPE_P (name))
236 /* OK */;
237 else if (identifier_p (name))
238 {
239 if ((MAYBE_CLASS_TYPE_P (basetype)
240 || TREE_CODE (basetype) == ENUMERAL_TYPE)
241 && name == constructor_name (basetype))
242 return true;
243 else
244 name = get_type_value (name);
245 }
246 else
247 {
248 /* In the case of:
249
250 template <class T> struct S { ~S(); };
251 int i;
252 i.~S();
253
254 NAME will be a class template. */
255 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
256 return false;
257 }
258
259 if (!name || name == error_mark_node)
260 return false;
261 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
262 }
263
264 /* We want the address of a function or method. We avoid creating a
265 pointer-to-member function. */
266
267 tree
build_addr_func(tree function,tsubst_flags_t complain)268 build_addr_func (tree function, tsubst_flags_t complain)
269 {
270 tree type = TREE_TYPE (function);
271
272 /* We have to do these by hand to avoid real pointer to member
273 functions. */
274 if (TREE_CODE (type) == METHOD_TYPE)
275 {
276 if (TREE_CODE (function) == OFFSET_REF)
277 {
278 tree object = build_address (TREE_OPERAND (function, 0));
279 return get_member_function_from_ptrfunc (&object,
280 TREE_OPERAND (function, 1),
281 complain);
282 }
283 function = build_address (function);
284 }
285 else
286 function = decay_conversion (function, complain, /*reject_builtin=*/false);
287
288 return function;
289 }
290
291 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
292 POINTER_TYPE to those. Note, pointer to member function types
293 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
294 two variants. build_call_a is the primitive taking an array of
295 arguments, while build_call_n is a wrapper that handles varargs. */
296
297 tree
build_call_n(tree function,int n,...)298 build_call_n (tree function, int n, ...)
299 {
300 if (n == 0)
301 return build_call_a (function, 0, NULL);
302 else
303 {
304 tree *argarray = XALLOCAVEC (tree, n);
305 va_list ap;
306 int i;
307
308 va_start (ap, n);
309 for (i = 0; i < n; i++)
310 argarray[i] = va_arg (ap, tree);
311 va_end (ap);
312 return build_call_a (function, n, argarray);
313 }
314 }
315
316 /* Update various flags in cfun and the call itself based on what is being
317 called. Split out of build_call_a so that bot_manip can use it too. */
318
319 void
set_flags_from_callee(tree call)320 set_flags_from_callee (tree call)
321 {
322 bool nothrow;
323 tree decl = get_callee_fndecl (call);
324
325 /* We check both the decl and the type; a function may be known not to
326 throw without being declared throw(). */
327 nothrow = decl && TREE_NOTHROW (decl);
328 if (CALL_EXPR_FN (call))
329 nothrow |= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (call))));
330 else if (internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW)
331 nothrow = true;
332
333 if (!nothrow && at_function_scope_p () && cfun && cp_function_chain)
334 cp_function_chain->can_throw = 1;
335
336 if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
337 current_function_returns_abnormally = 1;
338
339 TREE_NOTHROW (call) = nothrow;
340 }
341
342 tree
build_call_a(tree function,int n,tree * argarray)343 build_call_a (tree function, int n, tree *argarray)
344 {
345 tree decl;
346 tree result_type;
347 tree fntype;
348 int i;
349
350 function = build_addr_func (function, tf_warning_or_error);
351
352 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
353 fntype = TREE_TYPE (TREE_TYPE (function));
354 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
355 || TREE_CODE (fntype) == METHOD_TYPE);
356 result_type = TREE_TYPE (fntype);
357 /* An rvalue has no cv-qualifiers. */
358 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
359 result_type = cv_unqualified (result_type);
360
361 function = build_call_array_loc (input_location,
362 result_type, function, n, argarray);
363 set_flags_from_callee (function);
364
365 decl = get_callee_fndecl (function);
366
367 if (decl && !TREE_USED (decl))
368 {
369 /* We invoke build_call directly for several library
370 functions. These may have been declared normally if
371 we're building libgcc, so we can't just check
372 DECL_ARTIFICIAL. */
373 gcc_assert (DECL_ARTIFICIAL (decl)
374 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
375 "__", 2));
376 mark_used (decl);
377 }
378
379 require_complete_eh_spec_types (fntype, decl);
380
381 TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
382
383 /* Don't pass empty class objects by value. This is useful
384 for tags in STL, which are used to control overload resolution.
385 We don't need to handle other cases of copying empty classes. */
386 if (! decl || ! DECL_BUILT_IN (decl))
387 for (i = 0; i < n; i++)
388 {
389 tree arg = CALL_EXPR_ARG (function, i);
390 if (is_empty_class (TREE_TYPE (arg))
391 && ! TREE_ADDRESSABLE (TREE_TYPE (arg)))
392 {
393 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
394 arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
395 CALL_EXPR_ARG (function, i) = arg;
396 }
397 }
398
399 return function;
400 }
401
402 /* New overloading code. */
403
404 struct z_candidate;
405
406 struct candidate_warning {
407 z_candidate *loser;
408 candidate_warning *next;
409 };
410
411 /* Information for providing diagnostics about why overloading failed. */
412
413 enum rejection_reason_code {
414 rr_none,
415 rr_arity,
416 rr_explicit_conversion,
417 rr_template_conversion,
418 rr_arg_conversion,
419 rr_bad_arg_conversion,
420 rr_template_unification,
421 rr_invalid_copy,
422 rr_inherited_ctor,
423 rr_constraint_failure
424 };
425
426 struct conversion_info {
427 /* The index of the argument, 0-based. */
428 int n_arg;
429 /* The actual argument or its type. */
430 tree from;
431 /* The type of the parameter. */
432 tree to_type;
433 };
434
435 struct rejection_reason {
436 enum rejection_reason_code code;
437 union {
438 /* Information about an arity mismatch. */
439 struct {
440 /* The expected number of arguments. */
441 int expected;
442 /* The actual number of arguments in the call. */
443 int actual;
444 /* Whether the call was a varargs call. */
445 bool call_varargs_p;
446 } arity;
447 /* Information about an argument conversion mismatch. */
448 struct conversion_info conversion;
449 /* Same, but for bad argument conversions. */
450 struct conversion_info bad_conversion;
451 /* Information about template unification failures. These are the
452 parameters passed to fn_type_unification. */
453 struct {
454 tree tmpl;
455 tree explicit_targs;
456 int num_targs;
457 const tree *args;
458 unsigned int nargs;
459 tree return_type;
460 unification_kind_t strict;
461 int flags;
462 } template_unification;
463 /* Information about template instantiation failures. These are the
464 parameters passed to instantiate_template. */
465 struct {
466 tree tmpl;
467 tree targs;
468 } template_instantiation;
469 } u;
470 };
471
472 struct z_candidate {
473 /* The FUNCTION_DECL that will be called if this candidate is
474 selected by overload resolution. */
475 tree fn;
476 /* If not NULL_TREE, the first argument to use when calling this
477 function. */
478 tree first_arg;
479 /* The rest of the arguments to use when calling this function. If
480 there are no further arguments this may be NULL or it may be an
481 empty vector. */
482 const vec<tree, va_gc> *args;
483 /* The implicit conversion sequences for each of the arguments to
484 FN. */
485 conversion **convs;
486 /* The number of implicit conversion sequences. */
487 size_t num_convs;
488 /* If FN is a user-defined conversion, the standard conversion
489 sequence from the type returned by FN to the desired destination
490 type. */
491 conversion *second_conv;
492 struct rejection_reason *reason;
493 /* If FN is a member function, the binfo indicating the path used to
494 qualify the name of FN at the call site. This path is used to
495 determine whether or not FN is accessible if it is selected by
496 overload resolution. The DECL_CONTEXT of FN will always be a
497 (possibly improper) base of this binfo. */
498 tree access_path;
499 /* If FN is a non-static member function, the binfo indicating the
500 subobject to which the `this' pointer should be converted if FN
501 is selected by overload resolution. The type pointed to by
502 the `this' pointer must correspond to the most derived class
503 indicated by the CONVERSION_PATH. */
504 tree conversion_path;
505 tree template_decl;
506 tree explicit_targs;
507 candidate_warning *warnings;
508 z_candidate *next;
509 int viable;
510
511 /* The flags active in add_candidate. */
512 int flags;
513 };
514
515 /* Returns true iff T is a null pointer constant in the sense of
516 [conv.ptr]. */
517
518 bool
null_ptr_cst_p(tree t)519 null_ptr_cst_p (tree t)
520 {
521 tree type = TREE_TYPE (t);
522
523 /* [conv.ptr]
524
525 A null pointer constant is an integral constant expression
526 (_expr.const_) rvalue of integer type that evaluates to zero or
527 an rvalue of type std::nullptr_t. */
528 if (NULLPTR_TYPE_P (type))
529 return true;
530
531 if (cxx_dialect >= cxx11)
532 {
533 STRIP_ANY_LOCATION_WRAPPER (t);
534
535 /* Core issue 903 says only literal 0 is a null pointer constant. */
536 if (TREE_CODE (type) == INTEGER_TYPE
537 && !char_type_p (type)
538 && TREE_CODE (t) == INTEGER_CST
539 && integer_zerop (t)
540 && !TREE_OVERFLOW (t))
541 return true;
542 }
543 else if (CP_INTEGRAL_TYPE_P (type))
544 {
545 t = fold_non_dependent_expr (t);
546 STRIP_NOPS (t);
547 if (integer_zerop (t) && !TREE_OVERFLOW (t))
548 return true;
549 }
550
551 return false;
552 }
553
554 /* Returns true iff T is a null member pointer value (4.11). */
555
556 bool
null_member_pointer_value_p(tree t)557 null_member_pointer_value_p (tree t)
558 {
559 tree type = TREE_TYPE (t);
560 if (!type)
561 return false;
562 else if (TYPE_PTRMEMFUNC_P (type))
563 return (TREE_CODE (t) == CONSTRUCTOR
564 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
565 else if (TYPE_PTRDATAMEM_P (type))
566 return integer_all_onesp (t);
567 else
568 return false;
569 }
570
571 /* Returns nonzero if PARMLIST consists of only default parms,
572 ellipsis, and/or undeduced parameter packs. */
573
574 bool
sufficient_parms_p(const_tree parmlist)575 sufficient_parms_p (const_tree parmlist)
576 {
577 for (; parmlist && parmlist != void_list_node;
578 parmlist = TREE_CHAIN (parmlist))
579 if (!TREE_PURPOSE (parmlist)
580 && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
581 return false;
582 return true;
583 }
584
585 /* Allocate N bytes of memory from the conversion obstack. The memory
586 is zeroed before being returned. */
587
588 static void *
conversion_obstack_alloc(size_t n)589 conversion_obstack_alloc (size_t n)
590 {
591 void *p;
592 if (!conversion_obstack_initialized)
593 {
594 gcc_obstack_init (&conversion_obstack);
595 conversion_obstack_initialized = true;
596 }
597 p = obstack_alloc (&conversion_obstack, n);
598 memset (p, 0, n);
599 return p;
600 }
601
602 /* Allocate rejection reasons. */
603
604 static struct rejection_reason *
alloc_rejection(enum rejection_reason_code code)605 alloc_rejection (enum rejection_reason_code code)
606 {
607 struct rejection_reason *p;
608 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
609 p->code = code;
610 return p;
611 }
612
613 static struct rejection_reason *
arity_rejection(tree first_arg,int expected,int actual)614 arity_rejection (tree first_arg, int expected, int actual)
615 {
616 struct rejection_reason *r = alloc_rejection (rr_arity);
617 int adjust = first_arg != NULL_TREE;
618 r->u.arity.expected = expected - adjust;
619 r->u.arity.actual = actual - adjust;
620 return r;
621 }
622
623 static struct rejection_reason *
arg_conversion_rejection(tree first_arg,int n_arg,tree from,tree to)624 arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
625 {
626 struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
627 int adjust = first_arg != NULL_TREE;
628 r->u.conversion.n_arg = n_arg - adjust;
629 r->u.conversion.from = from;
630 r->u.conversion.to_type = to;
631 return r;
632 }
633
634 static struct rejection_reason *
bad_arg_conversion_rejection(tree first_arg,int n_arg,tree from,tree to)635 bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
636 {
637 struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
638 int adjust = first_arg != NULL_TREE;
639 r->u.bad_conversion.n_arg = n_arg - adjust;
640 r->u.bad_conversion.from = from;
641 r->u.bad_conversion.to_type = to;
642 return r;
643 }
644
645 static struct rejection_reason *
explicit_conversion_rejection(tree from,tree to)646 explicit_conversion_rejection (tree from, tree to)
647 {
648 struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
649 r->u.conversion.n_arg = 0;
650 r->u.conversion.from = from;
651 r->u.conversion.to_type = to;
652 return r;
653 }
654
655 static struct rejection_reason *
template_conversion_rejection(tree from,tree to)656 template_conversion_rejection (tree from, tree to)
657 {
658 struct rejection_reason *r = alloc_rejection (rr_template_conversion);
659 r->u.conversion.n_arg = 0;
660 r->u.conversion.from = from;
661 r->u.conversion.to_type = to;
662 return r;
663 }
664
665 static struct rejection_reason *
template_unification_rejection(tree tmpl,tree explicit_targs,tree targs,const tree * args,unsigned int nargs,tree return_type,unification_kind_t strict,int flags)666 template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
667 const tree *args, unsigned int nargs,
668 tree return_type, unification_kind_t strict,
669 int flags)
670 {
671 size_t args_n_bytes = sizeof (*args) * nargs;
672 tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
673 struct rejection_reason *r = alloc_rejection (rr_template_unification);
674 r->u.template_unification.tmpl = tmpl;
675 r->u.template_unification.explicit_targs = explicit_targs;
676 r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
677 /* Copy args to our own storage. */
678 memcpy (args1, args, args_n_bytes);
679 r->u.template_unification.args = args1;
680 r->u.template_unification.nargs = nargs;
681 r->u.template_unification.return_type = return_type;
682 r->u.template_unification.strict = strict;
683 r->u.template_unification.flags = flags;
684 return r;
685 }
686
687 static struct rejection_reason *
template_unification_error_rejection(void)688 template_unification_error_rejection (void)
689 {
690 return alloc_rejection (rr_template_unification);
691 }
692
693 static struct rejection_reason *
invalid_copy_with_fn_template_rejection(void)694 invalid_copy_with_fn_template_rejection (void)
695 {
696 struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
697 return r;
698 }
699
700 static struct rejection_reason *
inherited_ctor_rejection(void)701 inherited_ctor_rejection (void)
702 {
703 struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
704 return r;
705 }
706
707 // Build a constraint failure record, saving information into the
708 // template_instantiation field of the rejection. If FN is not a template
709 // declaration, the TMPL member is the FN declaration and TARGS is empty.
710
711 static struct rejection_reason *
constraint_failure(tree fn)712 constraint_failure (tree fn)
713 {
714 struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
715 if (tree ti = DECL_TEMPLATE_INFO (fn))
716 {
717 r->u.template_instantiation.tmpl = TI_TEMPLATE (ti);
718 r->u.template_instantiation.targs = TI_ARGS (ti);
719 }
720 else
721 {
722 r->u.template_instantiation.tmpl = fn;
723 r->u.template_instantiation.targs = NULL_TREE;
724 }
725 return r;
726 }
727
728 /* Dynamically allocate a conversion. */
729
730 static conversion *
alloc_conversion(conversion_kind kind)731 alloc_conversion (conversion_kind kind)
732 {
733 conversion *c;
734 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
735 c->kind = kind;
736 return c;
737 }
738
739 /* Make sure that all memory on the conversion obstack has been
740 freed. */
741
742 void
validate_conversion_obstack(void)743 validate_conversion_obstack (void)
744 {
745 if (conversion_obstack_initialized)
746 gcc_assert ((obstack_next_free (&conversion_obstack)
747 == obstack_base (&conversion_obstack)));
748 }
749
750 /* Dynamically allocate an array of N conversions. */
751
752 static conversion **
alloc_conversions(size_t n)753 alloc_conversions (size_t n)
754 {
755 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
756 }
757
758 static conversion *
build_conv(conversion_kind code,tree type,conversion * from)759 build_conv (conversion_kind code, tree type, conversion *from)
760 {
761 conversion *t;
762 conversion_rank rank = CONVERSION_RANK (from);
763
764 /* Note that the caller is responsible for filling in t->cand for
765 user-defined conversions. */
766 t = alloc_conversion (code);
767 t->type = type;
768 t->u.next = from;
769
770 switch (code)
771 {
772 case ck_ptr:
773 case ck_pmem:
774 case ck_base:
775 case ck_std:
776 if (rank < cr_std)
777 rank = cr_std;
778 break;
779
780 case ck_qual:
781 case ck_fnptr:
782 if (rank < cr_exact)
783 rank = cr_exact;
784 break;
785
786 default:
787 break;
788 }
789 t->rank = rank;
790 t->user_conv_p = (code == ck_user || from->user_conv_p);
791 t->bad_p = from->bad_p;
792 t->base_p = false;
793 return t;
794 }
795
796 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
797 specialization of std::initializer_list<T>, if such a conversion is
798 possible. */
799
800 static conversion *
build_list_conv(tree type,tree ctor,int flags,tsubst_flags_t complain)801 build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
802 {
803 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
804 unsigned len = CONSTRUCTOR_NELTS (ctor);
805 conversion **subconvs = alloc_conversions (len);
806 conversion *t;
807 unsigned i;
808 tree val;
809
810 /* Within a list-initialization we can have more user-defined
811 conversions. */
812 flags &= ~LOOKUP_NO_CONVERSION;
813 /* But no narrowing conversions. */
814 flags |= LOOKUP_NO_NARROWING;
815
816 /* Can't make an array of these types. */
817 if (TREE_CODE (elttype) == REFERENCE_TYPE
818 || TREE_CODE (elttype) == FUNCTION_TYPE
819 || VOID_TYPE_P (elttype))
820 return NULL;
821
822 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
823 {
824 conversion *sub
825 = implicit_conversion (elttype, TREE_TYPE (val), val,
826 false, flags, complain);
827 if (sub == NULL)
828 return NULL;
829
830 subconvs[i] = sub;
831 }
832
833 t = alloc_conversion (ck_list);
834 t->type = type;
835 t->u.list = subconvs;
836 t->rank = cr_exact;
837
838 for (i = 0; i < len; ++i)
839 {
840 conversion *sub = subconvs[i];
841 if (sub->rank > t->rank)
842 t->rank = sub->rank;
843 if (sub->user_conv_p)
844 t->user_conv_p = true;
845 if (sub->bad_p)
846 t->bad_p = true;
847 }
848
849 return t;
850 }
851
852 /* Return the next conversion of the conversion chain (if applicable),
853 or NULL otherwise. Please use this function instead of directly
854 accessing fields of struct conversion. */
855
856 static conversion *
next_conversion(conversion * conv)857 next_conversion (conversion *conv)
858 {
859 if (conv == NULL
860 || conv->kind == ck_identity
861 || conv->kind == ck_ambig
862 || conv->kind == ck_list)
863 return NULL;
864 return conv->u.next;
865 }
866
867 /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
868 is a valid aggregate initializer for array type ATYPE. */
869
870 static bool
can_convert_array(tree atype,tree ctor,int flags,tsubst_flags_t complain)871 can_convert_array (tree atype, tree ctor, int flags, tsubst_flags_t complain)
872 {
873 unsigned i;
874 tree elttype = TREE_TYPE (atype);
875 for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
876 {
877 tree val = CONSTRUCTOR_ELT (ctor, i)->value;
878 bool ok;
879 if (TREE_CODE (elttype) == ARRAY_TYPE
880 && TREE_CODE (val) == CONSTRUCTOR)
881 ok = can_convert_array (elttype, val, flags, complain);
882 else
883 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
884 complain);
885 if (!ok)
886 return false;
887 }
888 return true;
889 }
890
891 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
892 aggregate class, if such a conversion is possible. */
893
894 static conversion *
build_aggr_conv(tree type,tree ctor,int flags,tsubst_flags_t complain)895 build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
896 {
897 unsigned HOST_WIDE_INT i = 0;
898 conversion *c;
899 tree field = next_initializable_field (TYPE_FIELDS (type));
900 tree empty_ctor = NULL_TREE;
901
902 /* We already called reshape_init in implicit_conversion. */
903
904 /* The conversions within the init-list aren't affected by the enclosing
905 context; they're always simple copy-initialization. */
906 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
907
908 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
909 {
910 tree ftype = TREE_TYPE (field);
911 tree val;
912 bool ok;
913
914 if (i < CONSTRUCTOR_NELTS (ctor))
915 val = CONSTRUCTOR_ELT (ctor, i)->value;
916 else if (DECL_INITIAL (field))
917 val = get_nsdmi (field, /*ctor*/false, complain);
918 else if (TREE_CODE (ftype) == REFERENCE_TYPE)
919 /* Value-initialization of reference is ill-formed. */
920 return NULL;
921 else
922 {
923 if (empty_ctor == NULL_TREE)
924 empty_ctor = build_constructor (init_list_type_node, NULL);
925 val = empty_ctor;
926 }
927 ++i;
928
929 if (TREE_CODE (ftype) == ARRAY_TYPE
930 && TREE_CODE (val) == CONSTRUCTOR)
931 ok = can_convert_array (ftype, val, flags, complain);
932 else
933 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
934 complain);
935
936 if (!ok)
937 return NULL;
938
939 if (TREE_CODE (type) == UNION_TYPE)
940 break;
941 }
942
943 if (i < CONSTRUCTOR_NELTS (ctor))
944 return NULL;
945
946 c = alloc_conversion (ck_aggr);
947 c->type = type;
948 c->rank = cr_exact;
949 c->user_conv_p = true;
950 c->check_narrowing = true;
951 c->u.next = NULL;
952 return c;
953 }
954
955 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
956 array type, if such a conversion is possible. */
957
958 static conversion *
build_array_conv(tree type,tree ctor,int flags,tsubst_flags_t complain)959 build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
960 {
961 conversion *c;
962 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
963 tree elttype = TREE_TYPE (type);
964 unsigned i;
965 tree val;
966 bool bad = false;
967 bool user = false;
968 enum conversion_rank rank = cr_exact;
969
970 /* We might need to propagate the size from the element to the array. */
971 complete_type (type);
972
973 if (TYPE_DOMAIN (type)
974 && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
975 {
976 unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
977 if (alen < len)
978 return NULL;
979 }
980
981 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
982
983 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
984 {
985 conversion *sub
986 = implicit_conversion (elttype, TREE_TYPE (val), val,
987 false, flags, complain);
988 if (sub == NULL)
989 return NULL;
990
991 if (sub->rank > rank)
992 rank = sub->rank;
993 if (sub->user_conv_p)
994 user = true;
995 if (sub->bad_p)
996 bad = true;
997 }
998
999 c = alloc_conversion (ck_aggr);
1000 c->type = type;
1001 c->rank = rank;
1002 c->user_conv_p = user;
1003 c->bad_p = bad;
1004 c->u.next = NULL;
1005 return c;
1006 }
1007
1008 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1009 complex type, if such a conversion is possible. */
1010
1011 static conversion *
build_complex_conv(tree type,tree ctor,int flags,tsubst_flags_t complain)1012 build_complex_conv (tree type, tree ctor, int flags,
1013 tsubst_flags_t complain)
1014 {
1015 conversion *c;
1016 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1017 tree elttype = TREE_TYPE (type);
1018 unsigned i;
1019 tree val;
1020 bool bad = false;
1021 bool user = false;
1022 enum conversion_rank rank = cr_exact;
1023
1024 if (len != 2)
1025 return NULL;
1026
1027 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1028
1029 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
1030 {
1031 conversion *sub
1032 = implicit_conversion (elttype, TREE_TYPE (val), val,
1033 false, flags, complain);
1034 if (sub == NULL)
1035 return NULL;
1036
1037 if (sub->rank > rank)
1038 rank = sub->rank;
1039 if (sub->user_conv_p)
1040 user = true;
1041 if (sub->bad_p)
1042 bad = true;
1043 }
1044
1045 c = alloc_conversion (ck_aggr);
1046 c->type = type;
1047 c->rank = rank;
1048 c->user_conv_p = user;
1049 c->bad_p = bad;
1050 c->u.next = NULL;
1051 return c;
1052 }
1053
1054 /* Build a representation of the identity conversion from EXPR to
1055 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1056
1057 static conversion *
build_identity_conv(tree type,tree expr)1058 build_identity_conv (tree type, tree expr)
1059 {
1060 conversion *c;
1061
1062 c = alloc_conversion (ck_identity);
1063 c->type = type;
1064 c->u.expr = expr;
1065
1066 return c;
1067 }
1068
1069 /* Converting from EXPR to TYPE was ambiguous in the sense that there
1070 were multiple user-defined conversions to accomplish the job.
1071 Build a conversion that indicates that ambiguity. */
1072
1073 static conversion *
build_ambiguous_conv(tree type,tree expr)1074 build_ambiguous_conv (tree type, tree expr)
1075 {
1076 conversion *c;
1077
1078 c = alloc_conversion (ck_ambig);
1079 c->type = type;
1080 c->u.expr = expr;
1081
1082 return c;
1083 }
1084
1085 tree
strip_top_quals(tree t)1086 strip_top_quals (tree t)
1087 {
1088 if (TREE_CODE (t) == ARRAY_TYPE)
1089 return t;
1090 return cp_build_qualified_type (t, 0);
1091 }
1092
1093 /* Returns the standard conversion path (see [conv]) from type FROM to type
1094 TO, if any. For proper handling of null pointer constants, you must
1095 also pass the expression EXPR to convert from. If C_CAST_P is true,
1096 this conversion is coming from a C-style cast. */
1097
1098 static conversion *
standard_conversion(tree to,tree from,tree expr,bool c_cast_p,int flags,tsubst_flags_t complain)1099 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1100 int flags, tsubst_flags_t complain)
1101 {
1102 enum tree_code fcode, tcode;
1103 conversion *conv;
1104 bool fromref = false;
1105 tree qualified_to;
1106
1107 to = non_reference (to);
1108 if (TREE_CODE (from) == REFERENCE_TYPE)
1109 {
1110 fromref = true;
1111 from = TREE_TYPE (from);
1112 }
1113 qualified_to = to;
1114 to = strip_top_quals (to);
1115 from = strip_top_quals (from);
1116
1117 if (expr && type_unknown_p (expr))
1118 {
1119 if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1120 {
1121 tsubst_flags_t tflags = tf_conv;
1122 expr = instantiate_type (to, expr, tflags);
1123 if (expr == error_mark_node)
1124 return NULL;
1125 from = TREE_TYPE (expr);
1126 }
1127 else if (TREE_CODE (to) == BOOLEAN_TYPE)
1128 {
1129 /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1130 expr = resolve_nondeduced_context (expr, complain);
1131 from = TREE_TYPE (expr);
1132 }
1133 }
1134
1135 fcode = TREE_CODE (from);
1136 tcode = TREE_CODE (to);
1137
1138 conv = build_identity_conv (from, expr);
1139 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1140 {
1141 from = type_decays_to (from);
1142 fcode = TREE_CODE (from);
1143 /* Tell convert_like_real that we're using the address. */
1144 conv->rvaluedness_matches_p = true;
1145 conv = build_conv (ck_lvalue, from, conv);
1146 }
1147 /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1148 obvalue_p) seems odd, since it's already a prvalue, but that's how we
1149 express the copy constructor call required by copy-initialization. */
1150 else if (fromref || (expr && obvalue_p (expr)))
1151 {
1152 if (expr)
1153 {
1154 tree bitfield_type;
1155 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1156 if (bitfield_type)
1157 {
1158 from = strip_top_quals (bitfield_type);
1159 fcode = TREE_CODE (from);
1160 }
1161 }
1162 conv = build_conv (ck_rvalue, from, conv);
1163 if (flags & LOOKUP_PREFER_RVALUE)
1164 /* Tell convert_like_real to set LOOKUP_PREFER_RVALUE. */
1165 conv->rvaluedness_matches_p = true;
1166 }
1167
1168 /* Allow conversion between `__complex__' data types. */
1169 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1170 {
1171 /* The standard conversion sequence to convert FROM to TO is
1172 the standard conversion sequence to perform componentwise
1173 conversion. */
1174 conversion *part_conv = standard_conversion
1175 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1176 complain);
1177
1178 if (part_conv)
1179 {
1180 conv = build_conv (part_conv->kind, to, conv);
1181 conv->rank = part_conv->rank;
1182 }
1183 else
1184 conv = NULL;
1185
1186 return conv;
1187 }
1188
1189 if (same_type_p (from, to))
1190 {
1191 if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1192 conv->type = qualified_to;
1193 return conv;
1194 }
1195
1196 /* [conv.ptr]
1197 A null pointer constant can be converted to a pointer type; ... A
1198 null pointer constant of integral type can be converted to an
1199 rvalue of type std::nullptr_t. */
1200 if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1201 || NULLPTR_TYPE_P (to))
1202 && ((expr && null_ptr_cst_p (expr))
1203 || NULLPTR_TYPE_P (from)))
1204 conv = build_conv (ck_std, to, conv);
1205 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1206 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1207 {
1208 /* For backwards brain damage compatibility, allow interconversion of
1209 pointers and integers with a pedwarn. */
1210 conv = build_conv (ck_std, to, conv);
1211 conv->bad_p = true;
1212 }
1213 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1214 {
1215 /* For backwards brain damage compatibility, allow interconversion of
1216 enums and integers with a pedwarn. */
1217 conv = build_conv (ck_std, to, conv);
1218 conv->bad_p = true;
1219 }
1220 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1221 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1222 {
1223 tree to_pointee;
1224 tree from_pointee;
1225
1226 if (tcode == POINTER_TYPE)
1227 {
1228 to_pointee = TREE_TYPE (to);
1229 from_pointee = TREE_TYPE (from);
1230
1231 /* Since this is the target of a pointer, it can't have function
1232 qualifiers, so any TYPE_QUALS must be for attributes const or
1233 noreturn. Strip them. */
1234 if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1235 && TYPE_QUALS (to_pointee))
1236 to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1237 if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1238 && TYPE_QUALS (from_pointee))
1239 from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1240 }
1241 else
1242 {
1243 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1244 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1245 }
1246
1247 if (tcode == POINTER_TYPE
1248 && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1249 to_pointee))
1250 ;
1251 else if (VOID_TYPE_P (to_pointee)
1252 && !TYPE_PTRDATAMEM_P (from)
1253 && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1254 {
1255 tree nfrom = TREE_TYPE (from);
1256 /* Don't try to apply restrict to void. */
1257 int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1258 from_pointee = cp_build_qualified_type (void_type_node, quals);
1259 from = build_pointer_type (from_pointee);
1260 conv = build_conv (ck_ptr, from, conv);
1261 }
1262 else if (TYPE_PTRDATAMEM_P (from))
1263 {
1264 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1265 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1266
1267 if (same_type_p (fbase, tbase))
1268 /* No base conversion needed. */;
1269 else if (DERIVED_FROM_P (fbase, tbase)
1270 && (same_type_ignoring_top_level_qualifiers_p
1271 (from_pointee, to_pointee)))
1272 {
1273 from = build_ptrmem_type (tbase, from_pointee);
1274 conv = build_conv (ck_pmem, from, conv);
1275 }
1276 else
1277 return NULL;
1278 }
1279 else if (CLASS_TYPE_P (from_pointee)
1280 && CLASS_TYPE_P (to_pointee)
1281 /* [conv.ptr]
1282
1283 An rvalue of type "pointer to cv D," where D is a
1284 class type, can be converted to an rvalue of type
1285 "pointer to cv B," where B is a base class (clause
1286 _class.derived_) of D. If B is an inaccessible
1287 (clause _class.access_) or ambiguous
1288 (_class.member.lookup_) base class of D, a program
1289 that necessitates this conversion is ill-formed.
1290 Therefore, we use DERIVED_FROM_P, and do not check
1291 access or uniqueness. */
1292 && DERIVED_FROM_P (to_pointee, from_pointee))
1293 {
1294 from_pointee
1295 = cp_build_qualified_type (to_pointee,
1296 cp_type_quals (from_pointee));
1297 from = build_pointer_type (from_pointee);
1298 conv = build_conv (ck_ptr, from, conv);
1299 conv->base_p = true;
1300 }
1301
1302 if (same_type_p (from, to))
1303 /* OK */;
1304 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
1305 /* In a C-style cast, we ignore CV-qualification because we
1306 are allowed to perform a static_cast followed by a
1307 const_cast. */
1308 conv = build_conv (ck_qual, to, conv);
1309 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1310 conv = build_conv (ck_qual, to, conv);
1311 else if (expr && string_conv_p (to, expr, 0))
1312 /* converting from string constant to char *. */
1313 conv = build_conv (ck_qual, to, conv);
1314 else if (fnptr_conv_p (to, from))
1315 conv = build_conv (ck_fnptr, to, conv);
1316 /* Allow conversions among compatible ObjC pointer types (base
1317 conversions have been already handled above). */
1318 else if (c_dialect_objc ()
1319 && objc_compare_types (to, from, -4, NULL_TREE))
1320 conv = build_conv (ck_ptr, to, conv);
1321 else if (ptr_reasonably_similar (to_pointee, from_pointee))
1322 {
1323 conv = build_conv (ck_ptr, to, conv);
1324 conv->bad_p = true;
1325 }
1326 else
1327 return NULL;
1328
1329 from = to;
1330 }
1331 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1332 {
1333 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1334 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1335 tree fbase = class_of_this_parm (fromfn);
1336 tree tbase = class_of_this_parm (tofn);
1337
1338 if (!DERIVED_FROM_P (fbase, tbase))
1339 return NULL;
1340
1341 tree fstat = static_fn_type (fromfn);
1342 tree tstat = static_fn_type (tofn);
1343 if (same_type_p (tstat, fstat)
1344 || fnptr_conv_p (tstat, fstat))
1345 /* OK */;
1346 else
1347 return NULL;
1348
1349 if (!same_type_p (fbase, tbase))
1350 {
1351 from = build_memfn_type (fstat,
1352 tbase,
1353 cp_type_quals (tbase),
1354 type_memfn_rqual (tofn));
1355 from = build_ptrmemfunc_type (build_pointer_type (from));
1356 conv = build_conv (ck_pmem, from, conv);
1357 conv->base_p = true;
1358 }
1359 if (fnptr_conv_p (tstat, fstat))
1360 conv = build_conv (ck_fnptr, to, conv);
1361 }
1362 else if (tcode == BOOLEAN_TYPE)
1363 {
1364 /* [conv.bool]
1365
1366 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1367 to member type can be converted to a prvalue of type bool. ...
1368 For direct-initialization (8.5 [dcl.init]), a prvalue of type
1369 std::nullptr_t can be converted to a prvalue of type bool; */
1370 if (ARITHMETIC_TYPE_P (from)
1371 || UNSCOPED_ENUM_P (from)
1372 || fcode == POINTER_TYPE
1373 || TYPE_PTRMEM_P (from)
1374 || NULLPTR_TYPE_P (from))
1375 {
1376 conv = build_conv (ck_std, to, conv);
1377 if (fcode == POINTER_TYPE
1378 || TYPE_PTRDATAMEM_P (from)
1379 || (TYPE_PTRMEMFUNC_P (from)
1380 && conv->rank < cr_pbool)
1381 || NULLPTR_TYPE_P (from))
1382 conv->rank = cr_pbool;
1383 if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1384 conv->bad_p = true;
1385 return conv;
1386 }
1387
1388 return NULL;
1389 }
1390 /* We don't check for ENUMERAL_TYPE here because there are no standard
1391 conversions to enum type. */
1392 /* As an extension, allow conversion to complex type. */
1393 else if (ARITHMETIC_TYPE_P (to))
1394 {
1395 if (! (INTEGRAL_CODE_P (fcode)
1396 || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1397 || SCOPED_ENUM_P (from))
1398 return NULL;
1399
1400 /* If we're parsing an enum with no fixed underlying type, we're
1401 dealing with an incomplete type, which renders the conversion
1402 ill-formed. */
1403 if (!COMPLETE_TYPE_P (from))
1404 return NULL;
1405
1406 conv = build_conv (ck_std, to, conv);
1407
1408 /* Give this a better rank if it's a promotion. */
1409 if (same_type_p (to, type_promotes_to (from))
1410 && next_conversion (conv)->rank <= cr_promotion)
1411 conv->rank = cr_promotion;
1412 }
1413 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1414 && vector_types_convertible_p (from, to, false))
1415 return build_conv (ck_std, to, conv);
1416 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1417 && is_properly_derived_from (from, to))
1418 {
1419 if (conv->kind == ck_rvalue)
1420 conv = next_conversion (conv);
1421 conv = build_conv (ck_base, to, conv);
1422 /* The derived-to-base conversion indicates the initialization
1423 of a parameter with base type from an object of a derived
1424 type. A temporary object is created to hold the result of
1425 the conversion unless we're binding directly to a reference. */
1426 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1427 }
1428 else
1429 return NULL;
1430
1431 if (flags & LOOKUP_NO_NARROWING)
1432 conv->check_narrowing = true;
1433
1434 return conv;
1435 }
1436
1437 /* Returns nonzero if T1 is reference-related to T2. */
1438
1439 bool
reference_related_p(tree t1,tree t2)1440 reference_related_p (tree t1, tree t2)
1441 {
1442 if (t1 == error_mark_node || t2 == error_mark_node)
1443 return false;
1444
1445 t1 = TYPE_MAIN_VARIANT (t1);
1446 t2 = TYPE_MAIN_VARIANT (t2);
1447
1448 /* [dcl.init.ref]
1449
1450 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1451 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
1452 of T2. */
1453 return (same_type_p (t1, t2)
1454 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1455 && DERIVED_FROM_P (t1, t2)));
1456 }
1457
1458 /* Returns nonzero if T1 is reference-compatible with T2. */
1459
1460 static bool
reference_compatible_p(tree t1,tree t2)1461 reference_compatible_p (tree t1, tree t2)
1462 {
1463 /* [dcl.init.ref]
1464
1465 "cv1 T1" is reference compatible with "cv2 T2" if
1466 * T1 is reference-related to T2 or
1467 * T2 is "noexcept function" and T1 is "function", where the
1468 function types are otherwise the same,
1469 and cv1 is the same cv-qualification as, or greater cv-qualification
1470 than, cv2. */
1471 return ((reference_related_p (t1, t2)
1472 || fnptr_conv_p (t1, t2))
1473 && at_least_as_qualified_p (t1, t2));
1474 }
1475
1476 /* A reference of the indicated TYPE is being bound directly to the
1477 expression represented by the implicit conversion sequence CONV.
1478 Return a conversion sequence for this binding. */
1479
1480 static conversion *
direct_reference_binding(tree type,conversion * conv)1481 direct_reference_binding (tree type, conversion *conv)
1482 {
1483 tree t;
1484
1485 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1486 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1487
1488 t = TREE_TYPE (type);
1489
1490 if (conv->kind == ck_identity)
1491 /* Mark the identity conv as to not decay to rvalue. */
1492 conv->rvaluedness_matches_p = true;
1493
1494 /* [over.ics.rank]
1495
1496 When a parameter of reference type binds directly
1497 (_dcl.init.ref_) to an argument expression, the implicit
1498 conversion sequence is the identity conversion, unless the
1499 argument expression has a type that is a derived class of the
1500 parameter type, in which case the implicit conversion sequence is
1501 a derived-to-base Conversion.
1502
1503 If the parameter binds directly to the result of applying a
1504 conversion function to the argument expression, the implicit
1505 conversion sequence is a user-defined conversion sequence
1506 (_over.ics.user_), with the second standard conversion sequence
1507 either an identity conversion or, if the conversion function
1508 returns an entity of a type that is a derived class of the
1509 parameter type, a derived-to-base conversion. */
1510 if (is_properly_derived_from (conv->type, t))
1511 {
1512 /* Represent the derived-to-base conversion. */
1513 conv = build_conv (ck_base, t, conv);
1514 /* We will actually be binding to the base-class subobject in
1515 the derived class, so we mark this conversion appropriately.
1516 That way, convert_like knows not to generate a temporary. */
1517 conv->need_temporary_p = false;
1518 }
1519
1520 return build_conv (ck_ref_bind, type, conv);
1521 }
1522
1523 /* Returns the conversion path from type FROM to reference type TO for
1524 purposes of reference binding. For lvalue binding, either pass a
1525 reference type to FROM or an lvalue expression to EXPR. If the
1526 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1527 the conversion returned. If C_CAST_P is true, this
1528 conversion is coming from a C-style cast. */
1529
1530 static conversion *
reference_binding(tree rto,tree rfrom,tree expr,bool c_cast_p,int flags,tsubst_flags_t complain)1531 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1532 tsubst_flags_t complain)
1533 {
1534 conversion *conv = NULL;
1535 tree to = TREE_TYPE (rto);
1536 tree from = rfrom;
1537 tree tfrom;
1538 bool related_p;
1539 bool compatible_p;
1540 cp_lvalue_kind gl_kind;
1541 bool is_lvalue;
1542
1543 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1544 {
1545 expr = instantiate_type (to, expr, tf_none);
1546 if (expr == error_mark_node)
1547 return NULL;
1548 from = TREE_TYPE (expr);
1549 }
1550
1551 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1552 {
1553 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1554 /* DR 1288: Otherwise, if the initializer list has a single element
1555 of type E and ... [T's] referenced type is reference-related to E,
1556 the object or reference is initialized from that element... */
1557 if (CONSTRUCTOR_NELTS (expr) == 1)
1558 {
1559 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1560 if (error_operand_p (elt))
1561 return NULL;
1562 tree etype = TREE_TYPE (elt);
1563 if (reference_related_p (to, etype))
1564 {
1565 expr = elt;
1566 from = etype;
1567 goto skip;
1568 }
1569 }
1570 /* Otherwise, if T is a reference type, a prvalue temporary of the
1571 type referenced by T is copy-list-initialized or
1572 direct-list-initialized, depending on the kind of initialization
1573 for the reference, and the reference is bound to that temporary. */
1574 conv = implicit_conversion (to, from, expr, c_cast_p,
1575 flags|LOOKUP_NO_TEMP_BIND, complain);
1576 skip:;
1577 }
1578
1579 if (TREE_CODE (from) == REFERENCE_TYPE)
1580 {
1581 from = TREE_TYPE (from);
1582 if (!TYPE_REF_IS_RVALUE (rfrom)
1583 || TREE_CODE (from) == FUNCTION_TYPE)
1584 gl_kind = clk_ordinary;
1585 else
1586 gl_kind = clk_rvalueref;
1587 }
1588 else if (expr)
1589 gl_kind = lvalue_kind (expr);
1590 else if (CLASS_TYPE_P (from)
1591 || TREE_CODE (from) == ARRAY_TYPE)
1592 gl_kind = clk_class;
1593 else
1594 gl_kind = clk_none;
1595
1596 /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1597 if ((flags & LOOKUP_NO_TEMP_BIND)
1598 && (gl_kind & clk_class))
1599 gl_kind = clk_none;
1600
1601 /* Same mask as real_lvalue_p. */
1602 is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1603
1604 tfrom = from;
1605 if ((gl_kind & clk_bitfield) != 0)
1606 tfrom = unlowered_expr_type (expr);
1607
1608 /* Figure out whether or not the types are reference-related and
1609 reference compatible. We have to do this after stripping
1610 references from FROM. */
1611 related_p = reference_related_p (to, tfrom);
1612 /* If this is a C cast, first convert to an appropriately qualified
1613 type, so that we can later do a const_cast to the desired type. */
1614 if (related_p && c_cast_p
1615 && !at_least_as_qualified_p (to, tfrom))
1616 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1617 compatible_p = reference_compatible_p (to, tfrom);
1618
1619 /* Directly bind reference when target expression's type is compatible with
1620 the reference and expression is an lvalue. In DR391, the wording in
1621 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1622 const and rvalue references to rvalues of compatible class type.
1623 We should also do direct bindings for non-class xvalues. */
1624 if ((related_p || compatible_p) && gl_kind)
1625 {
1626 /* [dcl.init.ref]
1627
1628 If the initializer expression
1629
1630 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1631 is reference-compatible with "cv2 T2,"
1632
1633 the reference is bound directly to the initializer expression
1634 lvalue.
1635
1636 [...]
1637 If the initializer expression is an rvalue, with T2 a class type,
1638 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1639 is bound to the object represented by the rvalue or to a sub-object
1640 within that object. */
1641
1642 conv = build_identity_conv (tfrom, expr);
1643 conv = direct_reference_binding (rto, conv);
1644
1645 if (TREE_CODE (rfrom) == REFERENCE_TYPE)
1646 /* Handle rvalue reference to function properly. */
1647 conv->rvaluedness_matches_p
1648 = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1649 else
1650 conv->rvaluedness_matches_p
1651 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1652
1653 if ((gl_kind & clk_bitfield) != 0
1654 || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1655 /* For the purposes of overload resolution, we ignore the fact
1656 this expression is a bitfield or packed field. (In particular,
1657 [over.ics.ref] says specifically that a function with a
1658 non-const reference parameter is viable even if the
1659 argument is a bitfield.)
1660
1661 However, when we actually call the function we must create
1662 a temporary to which to bind the reference. If the
1663 reference is volatile, or isn't const, then we cannot make
1664 a temporary, so we just issue an error when the conversion
1665 actually occurs. */
1666 conv->need_temporary_p = true;
1667
1668 /* Don't allow binding of lvalues (other than function lvalues) to
1669 rvalue references. */
1670 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1671 && TREE_CODE (to) != FUNCTION_TYPE)
1672 conv->bad_p = true;
1673
1674 /* Nor the reverse. */
1675 if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
1676 && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
1677 || (flags & LOOKUP_NO_RVAL_BIND))
1678 && TREE_CODE (to) != FUNCTION_TYPE)
1679 conv->bad_p = true;
1680
1681 if (!compatible_p)
1682 conv->bad_p = true;
1683
1684 return conv;
1685 }
1686 /* [class.conv.fct] A conversion function is never used to convert a
1687 (possibly cv-qualified) object to the (possibly cv-qualified) same
1688 object type (or a reference to it), to a (possibly cv-qualified) base
1689 class of that type (or a reference to it).... */
1690 else if (CLASS_TYPE_P (from) && !related_p
1691 && !(flags & LOOKUP_NO_CONVERSION))
1692 {
1693 /* [dcl.init.ref]
1694
1695 If the initializer expression
1696
1697 -- has a class type (i.e., T2 is a class type) can be
1698 implicitly converted to an lvalue of type "cv3 T3," where
1699 "cv1 T1" is reference-compatible with "cv3 T3". (this
1700 conversion is selected by enumerating the applicable
1701 conversion functions (_over.match.ref_) and choosing the
1702 best one through overload resolution. (_over.match_).
1703
1704 the reference is bound to the lvalue result of the conversion
1705 in the second case. */
1706 z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
1707 complain);
1708 if (cand)
1709 return cand->second_conv;
1710 }
1711
1712 /* From this point on, we conceptually need temporaries, even if we
1713 elide them. Only the cases above are "direct bindings". */
1714 if (flags & LOOKUP_NO_TEMP_BIND)
1715 return NULL;
1716
1717 /* [over.ics.rank]
1718
1719 When a parameter of reference type is not bound directly to an
1720 argument expression, the conversion sequence is the one required
1721 to convert the argument expression to the underlying type of the
1722 reference according to _over.best.ics_. Conceptually, this
1723 conversion sequence corresponds to copy-initializing a temporary
1724 of the underlying type with the argument expression. Any
1725 difference in top-level cv-qualification is subsumed by the
1726 initialization itself and does not constitute a conversion. */
1727
1728 /* [dcl.init.ref]
1729
1730 Otherwise, the reference shall be an lvalue reference to a
1731 non-volatile const type, or the reference shall be an rvalue
1732 reference.
1733
1734 We try below to treat this as a bad conversion to improve diagnostics,
1735 but if TO is an incomplete class, we need to reject this conversion
1736 now to avoid unnecessary instantiation. */
1737 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto)
1738 && !COMPLETE_TYPE_P (to))
1739 return NULL;
1740
1741 /* We're generating a temporary now, but don't bind any more in the
1742 conversion (specifically, don't slice the temporary returned by a
1743 conversion operator). */
1744 flags |= LOOKUP_NO_TEMP_BIND;
1745
1746 /* Core issue 899: When [copy-]initializing a temporary to be bound
1747 to the first parameter of a copy constructor (12.8) called with
1748 a single argument in the context of direct-initialization,
1749 explicit conversion functions are also considered.
1750
1751 So don't set LOOKUP_ONLYCONVERTING in that case. */
1752 if (!(flags & LOOKUP_COPY_PARM))
1753 flags |= LOOKUP_ONLYCONVERTING;
1754
1755 if (!conv)
1756 conv = implicit_conversion (to, from, expr, c_cast_p,
1757 flags, complain);
1758 if (!conv)
1759 return NULL;
1760
1761 if (conv->user_conv_p)
1762 {
1763 /* If initializing the temporary used a conversion function,
1764 recalculate the second conversion sequence. */
1765 for (conversion *t = conv; t; t = next_conversion (t))
1766 if (t->kind == ck_user
1767 && DECL_CONV_FN_P (t->cand->fn))
1768 {
1769 tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
1770 /* A prvalue of non-class type is cv-unqualified. */
1771 if (TREE_CODE (ftype) != REFERENCE_TYPE && !CLASS_TYPE_P (ftype))
1772 ftype = cv_unqualified (ftype);
1773 int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
1774 conversion *new_second
1775 = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
1776 sflags, complain);
1777 if (!new_second)
1778 return NULL;
1779 return merge_conversion_sequences (t, new_second);
1780 }
1781 }
1782
1783 conv = build_conv (ck_ref_bind, rto, conv);
1784 /* This reference binding, unlike those above, requires the
1785 creation of a temporary. */
1786 conv->need_temporary_p = true;
1787 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1788
1789 /* [dcl.init.ref]
1790
1791 Otherwise, the reference shall be an lvalue reference to a
1792 non-volatile const type, or the reference shall be an rvalue
1793 reference. */
1794 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1795 conv->bad_p = true;
1796
1797 /* [dcl.init.ref]
1798
1799 Otherwise, a temporary of type "cv1 T1" is created and
1800 initialized from the initializer expression using the rules for a
1801 non-reference copy initialization. If T1 is reference-related to
1802 T2, cv1 must be the same cv-qualification as, or greater
1803 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1804 if (related_p && !at_least_as_qualified_p (to, from))
1805 conv->bad_p = true;
1806
1807 return conv;
1808 }
1809
1810 /* Returns the implicit conversion sequence (see [over.ics]) from type
1811 FROM to type TO. The optional expression EXPR may affect the
1812 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1813 true, this conversion is coming from a C-style cast. */
1814
1815 static conversion *
implicit_conversion(tree to,tree from,tree expr,bool c_cast_p,int flags,tsubst_flags_t complain)1816 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1817 int flags, tsubst_flags_t complain)
1818 {
1819 conversion *conv;
1820
1821 if (from == error_mark_node || to == error_mark_node
1822 || expr == error_mark_node)
1823 return NULL;
1824
1825 /* Other flags only apply to the primary function in overload
1826 resolution, or after we've chosen one. */
1827 flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
1828 |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE
1829 |LOOKUP_NO_NARROWING|LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL);
1830
1831 /* FIXME: actually we don't want warnings either, but we can't just
1832 have 'complain &= ~(tf_warning|tf_error)' because it would cause
1833 the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
1834 We really ought not to issue that warning until we've committed
1835 to that conversion. */
1836 complain &= ~tf_error;
1837
1838 /* Call reshape_init early to remove redundant braces. */
1839 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)
1840 && CLASS_TYPE_P (to)
1841 && COMPLETE_TYPE_P (complete_type (to))
1842 && !CLASSTYPE_NON_AGGREGATE (to))
1843 {
1844 expr = reshape_init (to, expr, complain);
1845 if (expr == error_mark_node)
1846 return NULL;
1847 from = TREE_TYPE (expr);
1848 }
1849
1850 if (TREE_CODE (to) == REFERENCE_TYPE)
1851 conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
1852 else
1853 conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
1854
1855 if (conv)
1856 return conv;
1857
1858 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1859 {
1860 if (is_std_init_list (to))
1861 return build_list_conv (to, expr, flags, complain);
1862
1863 /* As an extension, allow list-initialization of _Complex. */
1864 if (TREE_CODE (to) == COMPLEX_TYPE)
1865 {
1866 conv = build_complex_conv (to, expr, flags, complain);
1867 if (conv)
1868 return conv;
1869 }
1870
1871 /* Allow conversion from an initializer-list with one element to a
1872 scalar type. */
1873 if (SCALAR_TYPE_P (to))
1874 {
1875 int nelts = CONSTRUCTOR_NELTS (expr);
1876 tree elt;
1877
1878 if (nelts == 0)
1879 elt = build_value_init (to, tf_none);
1880 else if (nelts == 1)
1881 elt = CONSTRUCTOR_ELT (expr, 0)->value;
1882 else
1883 elt = error_mark_node;
1884
1885 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
1886 c_cast_p, flags, complain);
1887 if (conv)
1888 {
1889 conv->check_narrowing = true;
1890 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
1891 /* Too many levels of braces, i.e. '{{1}}'. */
1892 conv->bad_p = true;
1893 return conv;
1894 }
1895 }
1896 else if (TREE_CODE (to) == ARRAY_TYPE)
1897 return build_array_conv (to, expr, flags, complain);
1898 }
1899
1900 if (expr != NULL_TREE
1901 && (MAYBE_CLASS_TYPE_P (from)
1902 || MAYBE_CLASS_TYPE_P (to))
1903 && (flags & LOOKUP_NO_CONVERSION) == 0)
1904 {
1905 struct z_candidate *cand;
1906
1907 if (CLASS_TYPE_P (to)
1908 && BRACE_ENCLOSED_INITIALIZER_P (expr)
1909 && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
1910 return build_aggr_conv (to, expr, flags, complain);
1911
1912 cand = build_user_type_conversion_1 (to, expr, flags, complain);
1913 if (cand)
1914 {
1915 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
1916 && CONSTRUCTOR_NELTS (expr) == 1
1917 && !is_list_ctor (cand->fn))
1918 {
1919 /* "If C is not an initializer-list constructor and the
1920 initializer list has a single element of type cv U, where U is
1921 X or a class derived from X, the implicit conversion sequence
1922 has Exact Match rank if U is X, or Conversion rank if U is
1923 derived from X." */
1924 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1925 tree elttype = TREE_TYPE (elt);
1926 if (reference_related_p (to, elttype))
1927 return implicit_conversion (to, elttype, elt,
1928 c_cast_p, flags, complain);
1929 }
1930 conv = cand->second_conv;
1931 }
1932
1933 /* We used to try to bind a reference to a temporary here, but that
1934 is now handled after the recursive call to this function at the end
1935 of reference_binding. */
1936 return conv;
1937 }
1938
1939 return NULL;
1940 }
1941
1942 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1943 functions. ARGS will not be changed until a single candidate is
1944 selected. */
1945
1946 static struct z_candidate *
add_candidate(struct z_candidate ** candidates,tree fn,tree first_arg,const vec<tree,va_gc> * args,size_t num_convs,conversion ** convs,tree access_path,tree conversion_path,int viable,struct rejection_reason * reason,int flags)1947 add_candidate (struct z_candidate **candidates,
1948 tree fn, tree first_arg, const vec<tree, va_gc> *args,
1949 size_t num_convs, conversion **convs,
1950 tree access_path, tree conversion_path,
1951 int viable, struct rejection_reason *reason,
1952 int flags)
1953 {
1954 struct z_candidate *cand = (struct z_candidate *)
1955 conversion_obstack_alloc (sizeof (struct z_candidate));
1956
1957 cand->fn = fn;
1958 cand->first_arg = first_arg;
1959 cand->args = args;
1960 cand->convs = convs;
1961 cand->num_convs = num_convs;
1962 cand->access_path = access_path;
1963 cand->conversion_path = conversion_path;
1964 cand->viable = viable;
1965 cand->reason = reason;
1966 cand->next = *candidates;
1967 cand->flags = flags;
1968 *candidates = cand;
1969
1970 return cand;
1971 }
1972
1973 /* Return the number of remaining arguments in the parameter list
1974 beginning with ARG. */
1975
1976 int
remaining_arguments(tree arg)1977 remaining_arguments (tree arg)
1978 {
1979 int n;
1980
1981 for (n = 0; arg != NULL_TREE && arg != void_list_node;
1982 arg = TREE_CHAIN (arg))
1983 n++;
1984
1985 return n;
1986 }
1987
1988 /* Create an overload candidate for the function or method FN called
1989 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
1990 FLAGS is passed on to implicit_conversion.
1991
1992 This does not change ARGS.
1993
1994 CTYPE, if non-NULL, is the type we want to pretend this function
1995 comes from for purposes of overload resolution. */
1996
1997 static struct z_candidate *
add_function_candidate(struct z_candidate ** candidates,tree fn,tree ctype,tree first_arg,const vec<tree,va_gc> * args,tree access_path,tree conversion_path,int flags,tsubst_flags_t complain)1998 add_function_candidate (struct z_candidate **candidates,
1999 tree fn, tree ctype, tree first_arg,
2000 const vec<tree, va_gc> *args, tree access_path,
2001 tree conversion_path, int flags,
2002 tsubst_flags_t complain)
2003 {
2004 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2005 int i, len;
2006 conversion **convs;
2007 tree parmnode;
2008 tree orig_first_arg = first_arg;
2009 int skip;
2010 int viable = 1;
2011 struct rejection_reason *reason = NULL;
2012
2013 /* At this point we should not see any functions which haven't been
2014 explicitly declared, except for friend functions which will have
2015 been found using argument dependent lookup. */
2016 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
2017
2018 /* The `this', `in_chrg' and VTT arguments to constructors are not
2019 considered in overload resolution. */
2020 if (DECL_CONSTRUCTOR_P (fn))
2021 {
2022 if (ctor_omit_inherited_parms (fn))
2023 /* Bring back parameters omitted from an inherited ctor. */
2024 parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2025 else
2026 parmlist = skip_artificial_parms_for (fn, parmlist);
2027 skip = num_artificial_parms_for (fn);
2028 if (skip > 0 && first_arg != NULL_TREE)
2029 {
2030 --skip;
2031 first_arg = NULL_TREE;
2032 }
2033 }
2034 else
2035 skip = 0;
2036
2037 len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2038 convs = alloc_conversions (len);
2039
2040 /* 13.3.2 - Viable functions [over.match.viable]
2041 First, to be a viable function, a candidate function shall have enough
2042 parameters to agree in number with the arguments in the list.
2043
2044 We need to check this first; otherwise, checking the ICSes might cause
2045 us to produce an ill-formed template instantiation. */
2046
2047 parmnode = parmlist;
2048 for (i = 0; i < len; ++i)
2049 {
2050 if (parmnode == NULL_TREE || parmnode == void_list_node)
2051 break;
2052 parmnode = TREE_CHAIN (parmnode);
2053 }
2054
2055 if ((i < len && parmnode)
2056 || !sufficient_parms_p (parmnode))
2057 {
2058 int remaining = remaining_arguments (parmnode);
2059 viable = 0;
2060 reason = arity_rejection (first_arg, i + remaining, len);
2061 }
2062
2063 /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2064 parameter of type "reference to cv C" (including such a constructor
2065 instantiated from a template) is excluded from the set of candidate
2066 functions when used to construct an object of type D with an argument list
2067 containing a single argument if C is reference-related to D. */
2068 if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2069 && flag_new_inheriting_ctors
2070 && DECL_INHERITED_CTOR (fn))
2071 {
2072 tree ptype = non_reference (TREE_VALUE (parmlist));
2073 tree dtype = DECL_CONTEXT (fn);
2074 tree btype = DECL_INHERITED_CTOR_BASE (fn);
2075 if (reference_related_p (ptype, dtype)
2076 && reference_related_p (btype, ptype))
2077 {
2078 viable = false;
2079 reason = inherited_ctor_rejection ();
2080 }
2081 }
2082
2083 /* Second, for a function to be viable, its constraints must be
2084 satisfied. */
2085 if (flag_concepts && viable
2086 && !constraints_satisfied_p (fn))
2087 {
2088 reason = constraint_failure (fn);
2089 viable = false;
2090 }
2091
2092 /* When looking for a function from a subobject from an implicit
2093 copy/move constructor/operator=, don't consider anything that takes (a
2094 reference to) an unrelated type. See c++/44909 and core 1092. */
2095 if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2096 {
2097 if (DECL_CONSTRUCTOR_P (fn))
2098 i = 1;
2099 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2100 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2101 i = 2;
2102 else
2103 i = 0;
2104 if (i && len == i)
2105 {
2106 parmnode = chain_index (i-1, parmlist);
2107 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2108 ctype))
2109 viable = 0;
2110 }
2111
2112 /* This only applies at the top level. */
2113 flags &= ~LOOKUP_DEFAULTED;
2114 }
2115
2116 if (! viable)
2117 goto out;
2118
2119 /* Third, for F to be a viable function, there shall exist for each
2120 argument an implicit conversion sequence that converts that argument
2121 to the corresponding parameter of F. */
2122
2123 parmnode = parmlist;
2124
2125 for (i = 0; i < len; ++i)
2126 {
2127 tree argtype, to_type;
2128 tree arg;
2129 conversion *t;
2130 int is_this;
2131
2132 if (parmnode == void_list_node)
2133 break;
2134
2135 if (i == 0 && first_arg != NULL_TREE)
2136 arg = first_arg;
2137 else
2138 arg = CONST_CAST_TREE (
2139 (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2140 argtype = lvalue_type (arg);
2141
2142 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2143 && ! DECL_CONSTRUCTOR_P (fn));
2144
2145 if (parmnode)
2146 {
2147 tree parmtype = TREE_VALUE (parmnode);
2148 int lflags = flags;
2149
2150 parmnode = TREE_CHAIN (parmnode);
2151
2152 /* The type of the implicit object parameter ('this') for
2153 overload resolution is not always the same as for the
2154 function itself; conversion functions are considered to
2155 be members of the class being converted, and functions
2156 introduced by a using-declaration are considered to be
2157 members of the class that uses them.
2158
2159 Since build_over_call ignores the ICS for the `this'
2160 parameter, we can just change the parm type. */
2161 if (ctype && is_this)
2162 {
2163 parmtype = cp_build_qualified_type
2164 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
2165 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2166 {
2167 /* If the function has a ref-qualifier, the implicit
2168 object parameter has reference type. */
2169 bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2170 parmtype = cp_build_reference_type (parmtype, rv);
2171 /* The special handling of 'this' conversions in compare_ics
2172 does not apply if there is a ref-qualifier. */
2173 is_this = false;
2174 }
2175 else
2176 {
2177 parmtype = build_pointer_type (parmtype);
2178 /* We don't use build_this here because we don't want to
2179 capture the object argument until we've chosen a
2180 non-static member function. */
2181 arg = build_address (arg);
2182 argtype = lvalue_type (arg);
2183 }
2184 }
2185
2186 /* Core issue 899: When [copy-]initializing a temporary to be bound
2187 to the first parameter of a copy constructor (12.8) called with
2188 a single argument in the context of direct-initialization,
2189 explicit conversion functions are also considered.
2190
2191 So set LOOKUP_COPY_PARM to let reference_binding know that
2192 it's being called in that context. We generalize the above
2193 to handle move constructors and template constructors as well;
2194 the standardese should soon be updated similarly. */
2195 if (ctype && i == 0 && (len-skip == 1)
2196 && DECL_CONSTRUCTOR_P (fn)
2197 && parmtype != error_mark_node
2198 && (same_type_ignoring_top_level_qualifiers_p
2199 (non_reference (parmtype), ctype)))
2200 {
2201 if (!(flags & LOOKUP_ONLYCONVERTING))
2202 lflags |= LOOKUP_COPY_PARM;
2203 /* We allow user-defined conversions within init-lists, but
2204 don't list-initialize the copy parm, as that would mean
2205 using two levels of braces for the same type. */
2206 if ((flags & LOOKUP_LIST_INIT_CTOR)
2207 && BRACE_ENCLOSED_INITIALIZER_P (arg))
2208 lflags |= LOOKUP_NO_CONVERSION;
2209 }
2210 else
2211 lflags |= LOOKUP_ONLYCONVERTING;
2212
2213 t = implicit_conversion (parmtype, argtype, arg,
2214 /*c_cast_p=*/false, lflags, complain);
2215 to_type = parmtype;
2216 }
2217 else
2218 {
2219 t = build_identity_conv (argtype, arg);
2220 t->ellipsis_p = true;
2221 to_type = argtype;
2222 }
2223
2224 if (t && is_this)
2225 t->this_p = true;
2226
2227 convs[i] = t;
2228 if (! t)
2229 {
2230 viable = 0;
2231 reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
2232 break;
2233 }
2234
2235 if (t->bad_p)
2236 {
2237 viable = -1;
2238 reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type);
2239 }
2240 }
2241
2242 out:
2243 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2244 access_path, conversion_path, viable, reason, flags);
2245 }
2246
2247 /* Create an overload candidate for the conversion function FN which will
2248 be invoked for expression OBJ, producing a pointer-to-function which
2249 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2250 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2251 passed on to implicit_conversion.
2252
2253 Actually, we don't really care about FN; we care about the type it
2254 converts to. There may be multiple conversion functions that will
2255 convert to that type, and we rely on build_user_type_conversion_1 to
2256 choose the best one; so when we create our candidate, we record the type
2257 instead of the function. */
2258
2259 static struct z_candidate *
add_conv_candidate(struct z_candidate ** candidates,tree fn,tree obj,const vec<tree,va_gc> * arglist,tree access_path,tree conversion_path,tsubst_flags_t complain)2260 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2261 const vec<tree, va_gc> *arglist,
2262 tree access_path, tree conversion_path,
2263 tsubst_flags_t complain)
2264 {
2265 tree totype = TREE_TYPE (TREE_TYPE (fn));
2266 int i, len, viable, flags;
2267 tree parmlist, parmnode;
2268 conversion **convs;
2269 struct rejection_reason *reason;
2270
2271 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2272 parmlist = TREE_TYPE (parmlist);
2273 parmlist = TYPE_ARG_TYPES (parmlist);
2274
2275 len = vec_safe_length (arglist) + 1;
2276 convs = alloc_conversions (len);
2277 parmnode = parmlist;
2278 viable = 1;
2279 flags = LOOKUP_IMPLICIT;
2280 reason = NULL;
2281
2282 /* Don't bother looking up the same type twice. */
2283 if (*candidates && (*candidates)->fn == totype)
2284 return NULL;
2285
2286 for (i = 0; i < len; ++i)
2287 {
2288 tree arg, argtype, convert_type = NULL_TREE;
2289 conversion *t;
2290
2291 if (i == 0)
2292 arg = obj;
2293 else
2294 arg = (*arglist)[i - 1];
2295 argtype = lvalue_type (arg);
2296
2297 if (i == 0)
2298 {
2299 t = build_identity_conv (argtype, NULL_TREE);
2300 t = build_conv (ck_user, totype, t);
2301 /* Leave the 'cand' field null; we'll figure out the conversion in
2302 convert_like_real if this candidate is chosen. */
2303 convert_type = totype;
2304 }
2305 else if (parmnode == void_list_node)
2306 break;
2307 else if (parmnode)
2308 {
2309 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2310 /*c_cast_p=*/false, flags, complain);
2311 convert_type = TREE_VALUE (parmnode);
2312 }
2313 else
2314 {
2315 t = build_identity_conv (argtype, arg);
2316 t->ellipsis_p = true;
2317 convert_type = argtype;
2318 }
2319
2320 convs[i] = t;
2321 if (! t)
2322 break;
2323
2324 if (t->bad_p)
2325 {
2326 viable = -1;
2327 reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type);
2328 }
2329
2330 if (i == 0)
2331 continue;
2332
2333 if (parmnode)
2334 parmnode = TREE_CHAIN (parmnode);
2335 }
2336
2337 if (i < len
2338 || ! sufficient_parms_p (parmnode))
2339 {
2340 int remaining = remaining_arguments (parmnode);
2341 viable = 0;
2342 reason = arity_rejection (NULL_TREE, i + remaining, len);
2343 }
2344
2345 return add_candidate (candidates, totype, obj, arglist, len, convs,
2346 access_path, conversion_path, viable, reason, flags);
2347 }
2348
2349 static void
build_builtin_candidate(struct z_candidate ** candidates,tree fnname,tree type1,tree type2,tree * args,tree * argtypes,int flags,tsubst_flags_t complain)2350 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2351 tree type1, tree type2, tree *args, tree *argtypes,
2352 int flags, tsubst_flags_t complain)
2353 {
2354 conversion *t;
2355 conversion **convs;
2356 size_t num_convs;
2357 int viable = 1, i;
2358 tree types[2];
2359 struct rejection_reason *reason = NULL;
2360
2361 types[0] = type1;
2362 types[1] = type2;
2363
2364 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
2365 convs = alloc_conversions (num_convs);
2366
2367 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2368 conversion ops are allowed. We handle that here by just checking for
2369 boolean_type_node because other operators don't ask for it. COND_EXPR
2370 also does contextual conversion to bool for the first operand, but we
2371 handle that in build_conditional_expr, and type1 here is operand 2. */
2372 if (type1 != boolean_type_node)
2373 flags |= LOOKUP_ONLYCONVERTING;
2374
2375 for (i = 0; i < 2; ++i)
2376 {
2377 if (! args[i])
2378 break;
2379
2380 t = implicit_conversion (types[i], argtypes[i], args[i],
2381 /*c_cast_p=*/false, flags, complain);
2382 if (! t)
2383 {
2384 viable = 0;
2385 /* We need something for printing the candidate. */
2386 t = build_identity_conv (types[i], NULL_TREE);
2387 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2388 types[i]);
2389 }
2390 else if (t->bad_p)
2391 {
2392 viable = 0;
2393 reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2394 types[i]);
2395 }
2396 convs[i] = t;
2397 }
2398
2399 /* For COND_EXPR we rearranged the arguments; undo that now. */
2400 if (args[2])
2401 {
2402 convs[2] = convs[1];
2403 convs[1] = convs[0];
2404 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2405 /*c_cast_p=*/false, flags,
2406 complain);
2407 if (t)
2408 convs[0] = t;
2409 else
2410 {
2411 viable = 0;
2412 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2413 boolean_type_node);
2414 }
2415 }
2416
2417 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2418 num_convs, convs,
2419 /*access_path=*/NULL_TREE,
2420 /*conversion_path=*/NULL_TREE,
2421 viable, reason, flags);
2422 }
2423
2424 static bool
is_complete(tree t)2425 is_complete (tree t)
2426 {
2427 return COMPLETE_TYPE_P (complete_type (t));
2428 }
2429
2430 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2431
2432 static bool
promoted_arithmetic_type_p(tree type)2433 promoted_arithmetic_type_p (tree type)
2434 {
2435 /* [over.built]
2436
2437 In this section, the term promoted integral type is used to refer
2438 to those integral types which are preserved by integral promotion
2439 (including e.g. int and long but excluding e.g. char).
2440 Similarly, the term promoted arithmetic type refers to promoted
2441 integral types plus floating types. */
2442 return ((CP_INTEGRAL_TYPE_P (type)
2443 && same_type_p (type_promotes_to (type), type))
2444 || TREE_CODE (type) == REAL_TYPE);
2445 }
2446
2447 /* Create any builtin operator overload candidates for the operator in
2448 question given the converted operand types TYPE1 and TYPE2. The other
2449 args are passed through from add_builtin_candidates to
2450 build_builtin_candidate.
2451
2452 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2453 If CODE is requires candidates operands of the same type of the kind
2454 of which TYPE1 and TYPE2 are, we add both candidates
2455 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2456
2457 static void
add_builtin_candidate(struct z_candidate ** candidates,enum tree_code code,enum tree_code code2,tree fnname,tree type1,tree type2,tree * args,tree * argtypes,int flags,tsubst_flags_t complain)2458 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2459 enum tree_code code2, tree fnname, tree type1,
2460 tree type2, tree *args, tree *argtypes, int flags,
2461 tsubst_flags_t complain)
2462 {
2463 switch (code)
2464 {
2465 case POSTINCREMENT_EXPR:
2466 case POSTDECREMENT_EXPR:
2467 args[1] = integer_zero_node;
2468 type2 = integer_type_node;
2469 break;
2470 default:
2471 break;
2472 }
2473
2474 switch (code)
2475 {
2476
2477 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2478 and VQ is either volatile or empty, there exist candidate operator
2479 functions of the form
2480 VQ T& operator++(VQ T&);
2481 T operator++(VQ T&, int);
2482 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
2483 type other than bool, and VQ is either volatile or empty, there exist
2484 candidate operator functions of the form
2485 VQ T& operator--(VQ T&);
2486 T operator--(VQ T&, int);
2487 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
2488 complete object type, and VQ is either volatile or empty, there exist
2489 candidate operator functions of the form
2490 T*VQ& operator++(T*VQ&);
2491 T*VQ& operator--(T*VQ&);
2492 T* operator++(T*VQ&, int);
2493 T* operator--(T*VQ&, int); */
2494
2495 case POSTDECREMENT_EXPR:
2496 case PREDECREMENT_EXPR:
2497 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2498 return;
2499 /* FALLTHRU */
2500 case POSTINCREMENT_EXPR:
2501 case PREINCREMENT_EXPR:
2502 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2503 {
2504 type1 = build_reference_type (type1);
2505 break;
2506 }
2507 return;
2508
2509 /* 7 For every cv-qualified or cv-unqualified object type T, there
2510 exist candidate operator functions of the form
2511
2512 T& operator*(T*);
2513
2514 8 For every function type T, there exist candidate operator functions of
2515 the form
2516 T& operator*(T*); */
2517
2518 case INDIRECT_REF:
2519 if (TYPE_PTR_P (type1)
2520 && (TYPE_PTROB_P (type1)
2521 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2522 break;
2523 return;
2524
2525 /* 9 For every type T, there exist candidate operator functions of the form
2526 T* operator+(T*);
2527
2528 10For every promoted arithmetic type T, there exist candidate operator
2529 functions of the form
2530 T operator+(T);
2531 T operator-(T); */
2532
2533 case UNARY_PLUS_EXPR: /* unary + */
2534 if (TYPE_PTR_P (type1))
2535 break;
2536 /* FALLTHRU */
2537 case NEGATE_EXPR:
2538 if (ARITHMETIC_TYPE_P (type1))
2539 break;
2540 return;
2541
2542 /* 11For every promoted integral type T, there exist candidate operator
2543 functions of the form
2544 T operator~(T); */
2545
2546 case BIT_NOT_EXPR:
2547 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2548 break;
2549 return;
2550
2551 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2552 is the same type as C2 or is a derived class of C2, T is a complete
2553 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2554 there exist candidate operator functions of the form
2555 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2556 where CV12 is the union of CV1 and CV2. */
2557
2558 case MEMBER_REF:
2559 if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2560 {
2561 tree c1 = TREE_TYPE (type1);
2562 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2563
2564 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2565 && (TYPE_PTRMEMFUNC_P (type2)
2566 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2567 break;
2568 }
2569 return;
2570
2571 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2572 didate operator functions of the form
2573 LR operator*(L, R);
2574 LR operator/(L, R);
2575 LR operator+(L, R);
2576 LR operator-(L, R);
2577 bool operator<(L, R);
2578 bool operator>(L, R);
2579 bool operator<=(L, R);
2580 bool operator>=(L, R);
2581 bool operator==(L, R);
2582 bool operator!=(L, R);
2583 where LR is the result of the usual arithmetic conversions between
2584 types L and R.
2585
2586 14For every pair of types T and I, where T is a cv-qualified or cv-
2587 unqualified complete object type and I is a promoted integral type,
2588 there exist candidate operator functions of the form
2589 T* operator+(T*, I);
2590 T& operator[](T*, I);
2591 T* operator-(T*, I);
2592 T* operator+(I, T*);
2593 T& operator[](I, T*);
2594
2595 15For every T, where T is a pointer to complete object type, there exist
2596 candidate operator functions of the form112)
2597 ptrdiff_t operator-(T, T);
2598
2599 16For every pointer or enumeration type T, there exist candidate operator
2600 functions of the form
2601 bool operator<(T, T);
2602 bool operator>(T, T);
2603 bool operator<=(T, T);
2604 bool operator>=(T, T);
2605 bool operator==(T, T);
2606 bool operator!=(T, T);
2607
2608 17For every pointer to member type T, there exist candidate operator
2609 functions of the form
2610 bool operator==(T, T);
2611 bool operator!=(T, T); */
2612
2613 case MINUS_EXPR:
2614 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2615 break;
2616 if (TYPE_PTROB_P (type1)
2617 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2618 {
2619 type2 = ptrdiff_type_node;
2620 break;
2621 }
2622 /* FALLTHRU */
2623 case MULT_EXPR:
2624 case TRUNC_DIV_EXPR:
2625 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2626 break;
2627 return;
2628
2629 case EQ_EXPR:
2630 case NE_EXPR:
2631 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2632 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
2633 break;
2634 if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
2635 {
2636 type2 = type1;
2637 break;
2638 }
2639 if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
2640 {
2641 type1 = type2;
2642 break;
2643 }
2644 /* Fall through. */
2645 case LT_EXPR:
2646 case GT_EXPR:
2647 case LE_EXPR:
2648 case GE_EXPR:
2649 case MAX_EXPR:
2650 case MIN_EXPR:
2651 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2652 break;
2653 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2654 break;
2655 if (TREE_CODE (type1) == ENUMERAL_TYPE
2656 && TREE_CODE (type2) == ENUMERAL_TYPE)
2657 break;
2658 if (TYPE_PTR_P (type1)
2659 && null_ptr_cst_p (args[1]))
2660 {
2661 type2 = type1;
2662 break;
2663 }
2664 if (null_ptr_cst_p (args[0])
2665 && TYPE_PTR_P (type2))
2666 {
2667 type1 = type2;
2668 break;
2669 }
2670 return;
2671
2672 case PLUS_EXPR:
2673 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2674 break;
2675 /* FALLTHRU */
2676 case ARRAY_REF:
2677 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2678 {
2679 type1 = ptrdiff_type_node;
2680 break;
2681 }
2682 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2683 {
2684 type2 = ptrdiff_type_node;
2685 break;
2686 }
2687 return;
2688
2689 /* 18For every pair of promoted integral types L and R, there exist candi-
2690 date operator functions of the form
2691 LR operator%(L, R);
2692 LR operator&(L, R);
2693 LR operator^(L, R);
2694 LR operator|(L, R);
2695 L operator<<(L, R);
2696 L operator>>(L, R);
2697 where LR is the result of the usual arithmetic conversions between
2698 types L and R. */
2699
2700 case TRUNC_MOD_EXPR:
2701 case BIT_AND_EXPR:
2702 case BIT_IOR_EXPR:
2703 case BIT_XOR_EXPR:
2704 case LSHIFT_EXPR:
2705 case RSHIFT_EXPR:
2706 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2707 break;
2708 return;
2709
2710 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2711 type, VQ is either volatile or empty, and R is a promoted arithmetic
2712 type, there exist candidate operator functions of the form
2713 VQ L& operator=(VQ L&, R);
2714 VQ L& operator*=(VQ L&, R);
2715 VQ L& operator/=(VQ L&, R);
2716 VQ L& operator+=(VQ L&, R);
2717 VQ L& operator-=(VQ L&, R);
2718
2719 20For every pair T, VQ), where T is any type and VQ is either volatile
2720 or empty, there exist candidate operator functions of the form
2721 T*VQ& operator=(T*VQ&, T*);
2722
2723 21For every pair T, VQ), where T is a pointer to member type and VQ is
2724 either volatile or empty, there exist candidate operator functions of
2725 the form
2726 VQ T& operator=(VQ T&, T);
2727
2728 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2729 unqualified complete object type, VQ is either volatile or empty, and
2730 I is a promoted integral type, there exist candidate operator func-
2731 tions of the form
2732 T*VQ& operator+=(T*VQ&, I);
2733 T*VQ& operator-=(T*VQ&, I);
2734
2735 23For every triple L, VQ, R), where L is an integral or enumeration
2736 type, VQ is either volatile or empty, and R is a promoted integral
2737 type, there exist candidate operator functions of the form
2738
2739 VQ L& operator%=(VQ L&, R);
2740 VQ L& operator<<=(VQ L&, R);
2741 VQ L& operator>>=(VQ L&, R);
2742 VQ L& operator&=(VQ L&, R);
2743 VQ L& operator^=(VQ L&, R);
2744 VQ L& operator|=(VQ L&, R); */
2745
2746 case MODIFY_EXPR:
2747 switch (code2)
2748 {
2749 case PLUS_EXPR:
2750 case MINUS_EXPR:
2751 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2752 {
2753 type2 = ptrdiff_type_node;
2754 break;
2755 }
2756 /* FALLTHRU */
2757 case MULT_EXPR:
2758 case TRUNC_DIV_EXPR:
2759 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2760 break;
2761 return;
2762
2763 case TRUNC_MOD_EXPR:
2764 case BIT_AND_EXPR:
2765 case BIT_IOR_EXPR:
2766 case BIT_XOR_EXPR:
2767 case LSHIFT_EXPR:
2768 case RSHIFT_EXPR:
2769 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2770 break;
2771 return;
2772
2773 case NOP_EXPR:
2774 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2775 break;
2776 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2777 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2778 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2779 || ((TYPE_PTRMEMFUNC_P (type1)
2780 || TYPE_PTR_P (type1))
2781 && null_ptr_cst_p (args[1])))
2782 {
2783 type2 = type1;
2784 break;
2785 }
2786 return;
2787
2788 default:
2789 gcc_unreachable ();
2790 }
2791 type1 = build_reference_type (type1);
2792 break;
2793
2794 case COND_EXPR:
2795 /* [over.built]
2796
2797 For every pair of promoted arithmetic types L and R, there
2798 exist candidate operator functions of the form
2799
2800 LR operator?(bool, L, R);
2801
2802 where LR is the result of the usual arithmetic conversions
2803 between types L and R.
2804
2805 For every type T, where T is a pointer or pointer-to-member
2806 type, there exist candidate operator functions of the form T
2807 operator?(bool, T, T); */
2808
2809 if (promoted_arithmetic_type_p (type1)
2810 && promoted_arithmetic_type_p (type2))
2811 /* That's OK. */
2812 break;
2813
2814 /* Otherwise, the types should be pointers. */
2815 if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
2816 return;
2817
2818 /* We don't check that the two types are the same; the logic
2819 below will actually create two candidates; one in which both
2820 parameter types are TYPE1, and one in which both parameter
2821 types are TYPE2. */
2822 break;
2823
2824 case REALPART_EXPR:
2825 case IMAGPART_EXPR:
2826 if (ARITHMETIC_TYPE_P (type1))
2827 break;
2828 return;
2829
2830 default:
2831 gcc_unreachable ();
2832 }
2833
2834 /* Make sure we don't create builtin candidates with dependent types. */
2835 bool u1 = uses_template_parms (type1);
2836 bool u2 = type2 ? uses_template_parms (type2) : false;
2837 if (u1 || u2)
2838 {
2839 /* Try to recover if one of the types is non-dependent. But if
2840 there's only one type, there's nothing we can do. */
2841 if (!type2)
2842 return;
2843 /* And we lose if both are dependent. */
2844 if (u1 && u2)
2845 return;
2846 /* Or if they have different forms. */
2847 if (TREE_CODE (type1) != TREE_CODE (type2))
2848 return;
2849
2850 if (u1 && !u2)
2851 type1 = type2;
2852 else if (u2 && !u1)
2853 type2 = type1;
2854 }
2855
2856 /* If we're dealing with two pointer types or two enumeral types,
2857 we need candidates for both of them. */
2858 if (type2 && !same_type_p (type1, type2)
2859 && TREE_CODE (type1) == TREE_CODE (type2)
2860 && (TREE_CODE (type1) == REFERENCE_TYPE
2861 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2862 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2863 || TYPE_PTRMEMFUNC_P (type1)
2864 || MAYBE_CLASS_TYPE_P (type1)
2865 || TREE_CODE (type1) == ENUMERAL_TYPE))
2866 {
2867 if (TYPE_PTR_OR_PTRMEM_P (type1))
2868 {
2869 tree cptype = composite_pointer_type (type1, type2,
2870 error_mark_node,
2871 error_mark_node,
2872 CPO_CONVERSION,
2873 tf_none);
2874 if (cptype != error_mark_node)
2875 {
2876 build_builtin_candidate
2877 (candidates, fnname, cptype, cptype, args, argtypes,
2878 flags, complain);
2879 return;
2880 }
2881 }
2882
2883 build_builtin_candidate
2884 (candidates, fnname, type1, type1, args, argtypes, flags, complain);
2885 build_builtin_candidate
2886 (candidates, fnname, type2, type2, args, argtypes, flags, complain);
2887 return;
2888 }
2889
2890 build_builtin_candidate
2891 (candidates, fnname, type1, type2, args, argtypes, flags, complain);
2892 }
2893
2894 tree
type_decays_to(tree type)2895 type_decays_to (tree type)
2896 {
2897 if (TREE_CODE (type) == ARRAY_TYPE)
2898 return build_pointer_type (TREE_TYPE (type));
2899 if (TREE_CODE (type) == FUNCTION_TYPE)
2900 return build_pointer_type (type);
2901 return type;
2902 }
2903
2904 /* There are three conditions of builtin candidates:
2905
2906 1) bool-taking candidates. These are the same regardless of the input.
2907 2) pointer-pair taking candidates. These are generated for each type
2908 one of the input types converts to.
2909 3) arithmetic candidates. According to the standard, we should generate
2910 all of these, but I'm trying not to...
2911
2912 Here we generate a superset of the possible candidates for this particular
2913 case. That is a subset of the full set the standard defines, plus some
2914 other cases which the standard disallows. add_builtin_candidate will
2915 filter out the invalid set. */
2916
2917 static void
add_builtin_candidates(struct z_candidate ** candidates,enum tree_code code,enum tree_code code2,tree fnname,tree * args,int flags,tsubst_flags_t complain)2918 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2919 enum tree_code code2, tree fnname, tree *args,
2920 int flags, tsubst_flags_t complain)
2921 {
2922 int ref1, i;
2923 int enum_p = 0;
2924 tree type, argtypes[3], t;
2925 /* TYPES[i] is the set of possible builtin-operator parameter types
2926 we will consider for the Ith argument. */
2927 vec<tree, va_gc> *types[2];
2928 unsigned ix;
2929
2930 for (i = 0; i < 3; ++i)
2931 {
2932 if (args[i])
2933 argtypes[i] = unlowered_expr_type (args[i]);
2934 else
2935 argtypes[i] = NULL_TREE;
2936 }
2937
2938 switch (code)
2939 {
2940 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2941 and VQ is either volatile or empty, there exist candidate operator
2942 functions of the form
2943 VQ T& operator++(VQ T&); */
2944
2945 case POSTINCREMENT_EXPR:
2946 case PREINCREMENT_EXPR:
2947 case POSTDECREMENT_EXPR:
2948 case PREDECREMENT_EXPR:
2949 case MODIFY_EXPR:
2950 ref1 = 1;
2951 break;
2952
2953 /* 24There also exist candidate operator functions of the form
2954 bool operator!(bool);
2955 bool operator&&(bool, bool);
2956 bool operator||(bool, bool); */
2957
2958 case TRUTH_NOT_EXPR:
2959 build_builtin_candidate
2960 (candidates, fnname, boolean_type_node,
2961 NULL_TREE, args, argtypes, flags, complain);
2962 return;
2963
2964 case TRUTH_ORIF_EXPR:
2965 case TRUTH_ANDIF_EXPR:
2966 build_builtin_candidate
2967 (candidates, fnname, boolean_type_node,
2968 boolean_type_node, args, argtypes, flags, complain);
2969 return;
2970
2971 case ADDR_EXPR:
2972 case COMPOUND_EXPR:
2973 case COMPONENT_REF:
2974 return;
2975
2976 case COND_EXPR:
2977 case EQ_EXPR:
2978 case NE_EXPR:
2979 case LT_EXPR:
2980 case LE_EXPR:
2981 case GT_EXPR:
2982 case GE_EXPR:
2983 enum_p = 1;
2984 /* Fall through. */
2985
2986 default:
2987 ref1 = 0;
2988 }
2989
2990 types[0] = make_tree_vector ();
2991 types[1] = make_tree_vector ();
2992
2993 for (i = 0; i < 2; ++i)
2994 {
2995 if (! args[i])
2996 ;
2997 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
2998 {
2999 tree convs;
3000
3001 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
3002 return;
3003
3004 convs = lookup_conversions (argtypes[i]);
3005
3006 if (code == COND_EXPR)
3007 {
3008 if (lvalue_p (args[i]))
3009 vec_safe_push (types[i], build_reference_type (argtypes[i]));
3010
3011 vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
3012 }
3013
3014 else if (! convs)
3015 return;
3016
3017 for (; convs; convs = TREE_CHAIN (convs))
3018 {
3019 type = TREE_TYPE (convs);
3020
3021 if (i == 0 && ref1
3022 && (TREE_CODE (type) != REFERENCE_TYPE
3023 || CP_TYPE_CONST_P (TREE_TYPE (type))))
3024 continue;
3025
3026 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
3027 vec_safe_push (types[i], type);
3028
3029 type = non_reference (type);
3030 if (i != 0 || ! ref1)
3031 {
3032 type = cv_unqualified (type_decays_to (type));
3033 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3034 vec_safe_push (types[i], type);
3035 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3036 type = type_promotes_to (type);
3037 }
3038
3039 if (! vec_member (type, types[i]))
3040 vec_safe_push (types[i], type);
3041 }
3042 }
3043 else
3044 {
3045 if (code == COND_EXPR && lvalue_p (args[i]))
3046 vec_safe_push (types[i], build_reference_type (argtypes[i]));
3047 type = non_reference (argtypes[i]);
3048 if (i != 0 || ! ref1)
3049 {
3050 type = cv_unqualified (type_decays_to (type));
3051 if (enum_p && UNSCOPED_ENUM_P (type))
3052 vec_safe_push (types[i], type);
3053 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3054 type = type_promotes_to (type);
3055 }
3056 vec_safe_push (types[i], type);
3057 }
3058 }
3059
3060 /* Run through the possible parameter types of both arguments,
3061 creating candidates with those parameter types. */
3062 FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3063 {
3064 unsigned jx;
3065 tree u;
3066
3067 if (!types[1]->is_empty ())
3068 FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3069 add_builtin_candidate
3070 (candidates, code, code2, fnname, t,
3071 u, args, argtypes, flags, complain);
3072 else
3073 add_builtin_candidate
3074 (candidates, code, code2, fnname, t,
3075 NULL_TREE, args, argtypes, flags, complain);
3076 }
3077
3078 release_tree_vector (types[0]);
3079 release_tree_vector (types[1]);
3080 }
3081
3082
3083 /* If TMPL can be successfully instantiated as indicated by
3084 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3085
3086 TMPL is the template. EXPLICIT_TARGS are any explicit template
3087 arguments. ARGLIST is the arguments provided at the call-site.
3088 This does not change ARGLIST. The RETURN_TYPE is the desired type
3089 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3090 as for add_function_candidate. If an OBJ is supplied, FLAGS and
3091 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
3092
3093 static struct z_candidate*
add_template_candidate_real(struct z_candidate ** candidates,tree tmpl,tree ctype,tree explicit_targs,tree first_arg,const vec<tree,va_gc> * arglist,tree return_type,tree access_path,tree conversion_path,int flags,tree obj,unification_kind_t strict,tsubst_flags_t complain)3094 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3095 tree ctype, tree explicit_targs, tree first_arg,
3096 const vec<tree, va_gc> *arglist, tree return_type,
3097 tree access_path, tree conversion_path,
3098 int flags, tree obj, unification_kind_t strict,
3099 tsubst_flags_t complain)
3100 {
3101 int ntparms = DECL_NTPARMS (tmpl);
3102 tree targs = make_tree_vec (ntparms);
3103 unsigned int len = vec_safe_length (arglist);
3104 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3105 unsigned int skip_without_in_chrg = 0;
3106 tree first_arg_without_in_chrg = first_arg;
3107 tree *args_without_in_chrg;
3108 unsigned int nargs_without_in_chrg;
3109 unsigned int ia, ix;
3110 tree arg;
3111 struct z_candidate *cand;
3112 tree fn;
3113 struct rejection_reason *reason = NULL;
3114 int errs;
3115
3116 /* We don't do deduction on the in-charge parameter, the VTT
3117 parameter or 'this'. */
3118 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
3119 {
3120 if (first_arg_without_in_chrg != NULL_TREE)
3121 first_arg_without_in_chrg = NULL_TREE;
3122 else if (return_type && strict == DEDUCE_CALL)
3123 /* We're deducing for a call to the result of a template conversion
3124 function, so the args don't contain 'this'; leave them alone. */;
3125 else
3126 ++skip_without_in_chrg;
3127 }
3128
3129 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3130 || DECL_BASE_CONSTRUCTOR_P (tmpl))
3131 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3132 {
3133 if (first_arg_without_in_chrg != NULL_TREE)
3134 first_arg_without_in_chrg = NULL_TREE;
3135 else
3136 ++skip_without_in_chrg;
3137 }
3138
3139 if (len < skip_without_in_chrg)
3140 return NULL;
3141
3142 if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3143 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3144 TREE_TYPE ((*arglist)[0])))
3145 {
3146 /* 12.8/6 says, "A declaration of a constructor for a class X is
3147 ill-formed if its first parameter is of type (optionally cv-qualified)
3148 X and either there are no other parameters or else all other
3149 parameters have default arguments. A member function template is never
3150 instantiated to produce such a constructor signature."
3151
3152 So if we're trying to copy an object of the containing class, don't
3153 consider a template constructor that has a first parameter type that
3154 is just a template parameter, as we would deduce a signature that we
3155 would then reject in the code below. */
3156 if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3157 {
3158 firstparm = TREE_VALUE (firstparm);
3159 if (PACK_EXPANSION_P (firstparm))
3160 firstparm = PACK_EXPANSION_PATTERN (firstparm);
3161 if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3162 {
3163 gcc_assert (!explicit_targs);
3164 reason = invalid_copy_with_fn_template_rejection ();
3165 goto fail;
3166 }
3167 }
3168 }
3169
3170 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3171 + (len - skip_without_in_chrg));
3172 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3173 ia = 0;
3174 if (first_arg_without_in_chrg != NULL_TREE)
3175 {
3176 args_without_in_chrg[ia] = first_arg_without_in_chrg;
3177 ++ia;
3178 }
3179 for (ix = skip_without_in_chrg;
3180 vec_safe_iterate (arglist, ix, &arg);
3181 ++ix)
3182 {
3183 args_without_in_chrg[ia] = arg;
3184 ++ia;
3185 }
3186 gcc_assert (ia == nargs_without_in_chrg);
3187
3188 errs = errorcount+sorrycount;
3189 fn = fn_type_unification (tmpl, explicit_targs, targs,
3190 args_without_in_chrg,
3191 nargs_without_in_chrg,
3192 return_type, strict, flags, false,
3193 complain & tf_decltype);
3194
3195 if (fn == error_mark_node)
3196 {
3197 /* Don't repeat unification later if it already resulted in errors. */
3198 if (errorcount+sorrycount == errs)
3199 reason = template_unification_rejection (tmpl, explicit_targs,
3200 targs, args_without_in_chrg,
3201 nargs_without_in_chrg,
3202 return_type, strict, flags);
3203 else
3204 reason = template_unification_error_rejection ();
3205 goto fail;
3206 }
3207
3208 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3209 {
3210 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3211 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3212 ctype))
3213 {
3214 /* We're trying to produce a constructor with a prohibited signature,
3215 as discussed above; handle here any cases we didn't catch then,
3216 such as X(X<T>). */
3217 reason = invalid_copy_with_fn_template_rejection ();
3218 goto fail;
3219 }
3220 }
3221
3222 if (obj != NULL_TREE)
3223 /* Aha, this is a conversion function. */
3224 cand = add_conv_candidate (candidates, fn, obj, arglist,
3225 access_path, conversion_path, complain);
3226 else
3227 cand = add_function_candidate (candidates, fn, ctype,
3228 first_arg, arglist, access_path,
3229 conversion_path, flags, complain);
3230 if (DECL_TI_TEMPLATE (fn) != tmpl)
3231 /* This situation can occur if a member template of a template
3232 class is specialized. Then, instantiate_template might return
3233 an instantiation of the specialization, in which case the
3234 DECL_TI_TEMPLATE field will point at the original
3235 specialization. For example:
3236
3237 template <class T> struct S { template <class U> void f(U);
3238 template <> void f(int) {}; };
3239 S<double> sd;
3240 sd.f(3);
3241
3242 Here, TMPL will be template <class U> S<double>::f(U).
3243 And, instantiate template will give us the specialization
3244 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3245 for this will point at template <class T> template <> S<T>::f(int),
3246 so that we can find the definition. For the purposes of
3247 overload resolution, however, we want the original TMPL. */
3248 cand->template_decl = build_template_info (tmpl, targs);
3249 else
3250 cand->template_decl = DECL_TEMPLATE_INFO (fn);
3251 cand->explicit_targs = explicit_targs;
3252
3253 return cand;
3254 fail:
3255 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
3256 access_path, conversion_path, 0, reason, flags);
3257 }
3258
3259
3260 static struct z_candidate *
add_template_candidate(struct z_candidate ** candidates,tree tmpl,tree ctype,tree explicit_targs,tree first_arg,const vec<tree,va_gc> * arglist,tree return_type,tree access_path,tree conversion_path,int flags,unification_kind_t strict,tsubst_flags_t complain)3261 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3262 tree explicit_targs, tree first_arg,
3263 const vec<tree, va_gc> *arglist, tree return_type,
3264 tree access_path, tree conversion_path, int flags,
3265 unification_kind_t strict, tsubst_flags_t complain)
3266 {
3267 return
3268 add_template_candidate_real (candidates, tmpl, ctype,
3269 explicit_targs, first_arg, arglist,
3270 return_type, access_path, conversion_path,
3271 flags, NULL_TREE, strict, complain);
3272 }
3273
3274 /* Create an overload candidate for the conversion function template TMPL,
3275 returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3276 pointer-to-function which will in turn be called with the argument list
3277 ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3278 passed on to implicit_conversion. */
3279
3280 static struct z_candidate *
add_template_conv_candidate(struct z_candidate ** candidates,tree tmpl,tree obj,const vec<tree,va_gc> * arglist,tree return_type,tree access_path,tree conversion_path,tsubst_flags_t complain)3281 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3282 tree obj,
3283 const vec<tree, va_gc> *arglist,
3284 tree return_type, tree access_path,
3285 tree conversion_path, tsubst_flags_t complain)
3286 {
3287 /* Making this work broke PR 71117 and 85118, so until the committee resolves
3288 core issue 2189, let's disable this candidate if there are any call
3289 operators. */
3290 if (*candidates)
3291 return NULL;
3292
3293 return
3294 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3295 NULL_TREE, arglist, return_type, access_path,
3296 conversion_path, 0, obj, DEDUCE_CALL,
3297 complain);
3298 }
3299
3300 /* The CANDS are the set of candidates that were considered for
3301 overload resolution. Return the set of viable candidates, or CANDS
3302 if none are viable. If any of the candidates were viable, set
3303 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3304 considered viable only if it is strictly viable. */
3305
3306 static struct z_candidate*
splice_viable(struct z_candidate * cands,bool strict_p,bool * any_viable_p)3307 splice_viable (struct z_candidate *cands,
3308 bool strict_p,
3309 bool *any_viable_p)
3310 {
3311 struct z_candidate *viable;
3312 struct z_candidate **last_viable;
3313 struct z_candidate **cand;
3314 bool found_strictly_viable = false;
3315
3316 /* Be strict inside templates, since build_over_call won't actually
3317 do the conversions to get pedwarns. */
3318 if (processing_template_decl)
3319 strict_p = true;
3320
3321 viable = NULL;
3322 last_viable = &viable;
3323 *any_viable_p = false;
3324
3325 cand = &cands;
3326 while (*cand)
3327 {
3328 struct z_candidate *c = *cand;
3329 if (!strict_p
3330 && (c->viable == 1 || TREE_CODE (c->fn) == TEMPLATE_DECL))
3331 {
3332 /* Be strict in the presence of a viable candidate. Also if
3333 there are template candidates, so that we get deduction errors
3334 for them instead of silently preferring a bad conversion. */
3335 strict_p = true;
3336 if (viable && !found_strictly_viable)
3337 {
3338 /* Put any spliced near matches back onto the main list so
3339 that we see them if there is no strict match. */
3340 *any_viable_p = false;
3341 *last_viable = cands;
3342 cands = viable;
3343 viable = NULL;
3344 last_viable = &viable;
3345 }
3346 }
3347
3348 if (strict_p ? c->viable == 1 : c->viable)
3349 {
3350 *last_viable = c;
3351 *cand = c->next;
3352 c->next = NULL;
3353 last_viable = &c->next;
3354 *any_viable_p = true;
3355 if (c->viable == 1)
3356 found_strictly_viable = true;
3357 }
3358 else
3359 cand = &c->next;
3360 }
3361
3362 return viable ? viable : cands;
3363 }
3364
3365 static bool
any_strictly_viable(struct z_candidate * cands)3366 any_strictly_viable (struct z_candidate *cands)
3367 {
3368 for (; cands; cands = cands->next)
3369 if (cands->viable == 1)
3370 return true;
3371 return false;
3372 }
3373
3374 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3375 words, it is about to become the "this" pointer for a member
3376 function call. Take the address of the object. */
3377
3378 static tree
build_this(tree obj)3379 build_this (tree obj)
3380 {
3381 /* In a template, we are only concerned about the type of the
3382 expression, so we can take a shortcut. */
3383 if (processing_template_decl)
3384 return build_address (obj);
3385
3386 return cp_build_addr_expr (obj, tf_warning_or_error);
3387 }
3388
3389 /* Returns true iff functions are equivalent. Equivalent functions are
3390 not '==' only if one is a function-local extern function or if
3391 both are extern "C". */
3392
3393 static inline int
equal_functions(tree fn1,tree fn2)3394 equal_functions (tree fn1, tree fn2)
3395 {
3396 if (TREE_CODE (fn1) != TREE_CODE (fn2))
3397 return 0;
3398 if (TREE_CODE (fn1) == TEMPLATE_DECL)
3399 return fn1 == fn2;
3400 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
3401 || DECL_EXTERN_C_FUNCTION_P (fn1))
3402 return decls_match (fn1, fn2);
3403 return fn1 == fn2;
3404 }
3405
3406 /* Print information about a candidate being rejected due to INFO. */
3407
3408 static void
print_conversion_rejection(location_t loc,struct conversion_info * info)3409 print_conversion_rejection (location_t loc, struct conversion_info *info)
3410 {
3411 tree from = info->from;
3412 if (!TYPE_P (from))
3413 from = lvalue_type (from);
3414 if (info->n_arg == -1)
3415 {
3416 /* Conversion of implicit `this' argument failed. */
3417 if (!TYPE_P (info->from))
3418 /* A bad conversion for 'this' must be discarding cv-quals. */
3419 inform (loc, " passing %qT as %<this%> "
3420 "argument discards qualifiers",
3421 from);
3422 else
3423 inform (loc, " no known conversion for implicit "
3424 "%<this%> parameter from %qH to %qI",
3425 from, info->to_type);
3426 }
3427 else if (!TYPE_P (info->from))
3428 {
3429 if (info->n_arg >= 0)
3430 inform (loc, " conversion of argument %d would be ill-formed:",
3431 info->n_arg + 1);
3432 perform_implicit_conversion (info->to_type, info->from,
3433 tf_warning_or_error);
3434 }
3435 else if (info->n_arg == -2)
3436 /* Conversion of conversion function return value failed. */
3437 inform (loc, " no known conversion from %qH to %qI",
3438 from, info->to_type);
3439 else
3440 inform (loc, " no known conversion for argument %d from %qH to %qI",
3441 info->n_arg + 1, from, info->to_type);
3442 }
3443
3444 /* Print information about a candidate with WANT parameters and we found
3445 HAVE. */
3446
3447 static void
print_arity_information(location_t loc,unsigned int have,unsigned int want)3448 print_arity_information (location_t loc, unsigned int have, unsigned int want)
3449 {
3450 inform_n (loc, want,
3451 " candidate expects %d argument, %d provided",
3452 " candidate expects %d arguments, %d provided",
3453 want, have);
3454 }
3455
3456 /* Print information about one overload candidate CANDIDATE. MSGSTR
3457 is the text to print before the candidate itself.
3458
3459 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3460 to have been run through gettext by the caller. This wart makes
3461 life simpler in print_z_candidates and for the translators. */
3462
3463 static void
print_z_candidate(location_t loc,const char * msgstr,struct z_candidate * candidate)3464 print_z_candidate (location_t loc, const char *msgstr,
3465 struct z_candidate *candidate)
3466 {
3467 const char *msg = (msgstr == NULL
3468 ? ""
3469 : ACONCAT ((msgstr, " ", NULL)));
3470 tree fn = candidate->fn;
3471 if (flag_new_inheriting_ctors)
3472 fn = strip_inheriting_ctors (fn);
3473 location_t cloc = location_of (fn);
3474
3475 if (identifier_p (fn))
3476 {
3477 cloc = loc;
3478 if (candidate->num_convs == 3)
3479 inform (cloc, "%s%<%D(%T, %T, %T)%> <built-in>", msg, fn,
3480 candidate->convs[0]->type,
3481 candidate->convs[1]->type,
3482 candidate->convs[2]->type);
3483 else if (candidate->num_convs == 2)
3484 inform (cloc, "%s%<%D(%T, %T)%> <built-in>", msg, fn,
3485 candidate->convs[0]->type,
3486 candidate->convs[1]->type);
3487 else
3488 inform (cloc, "%s%<%D(%T)%> <built-in>", msg, fn,
3489 candidate->convs[0]->type);
3490 }
3491 else if (TYPE_P (fn))
3492 inform (cloc, "%s%qT <conversion>", msg, fn);
3493 else if (candidate->viable == -1)
3494 inform (cloc, "%s%#qD <near match>", msg, fn);
3495 else if (DECL_DELETED_FN (fn))
3496 inform (cloc, "%s%#qD <deleted>", msg, fn);
3497 else
3498 inform (cloc, "%s%#qD", msg, fn);
3499 if (fn != candidate->fn)
3500 {
3501 cloc = location_of (candidate->fn);
3502 inform (cloc, " inherited here");
3503 }
3504 /* Give the user some information about why this candidate failed. */
3505 if (candidate->reason != NULL)
3506 {
3507 struct rejection_reason *r = candidate->reason;
3508
3509 switch (r->code)
3510 {
3511 case rr_arity:
3512 print_arity_information (cloc, r->u.arity.actual,
3513 r->u.arity.expected);
3514 break;
3515 case rr_arg_conversion:
3516 print_conversion_rejection (cloc, &r->u.conversion);
3517 break;
3518 case rr_bad_arg_conversion:
3519 print_conversion_rejection (cloc, &r->u.bad_conversion);
3520 break;
3521 case rr_explicit_conversion:
3522 inform (cloc, " return type %qT of explicit conversion function "
3523 "cannot be converted to %qT with a qualification "
3524 "conversion", r->u.conversion.from,
3525 r->u.conversion.to_type);
3526 break;
3527 case rr_template_conversion:
3528 inform (cloc, " conversion from return type %qT of template "
3529 "conversion function specialization to %qT is not an "
3530 "exact match", r->u.conversion.from,
3531 r->u.conversion.to_type);
3532 break;
3533 case rr_template_unification:
3534 /* We use template_unification_error_rejection if unification caused
3535 actual non-SFINAE errors, in which case we don't need to repeat
3536 them here. */
3537 if (r->u.template_unification.tmpl == NULL_TREE)
3538 {
3539 inform (cloc, " substitution of deduced template arguments "
3540 "resulted in errors seen above");
3541 break;
3542 }
3543 /* Re-run template unification with diagnostics. */
3544 inform (cloc, " template argument deduction/substitution failed:");
3545 fn_type_unification (r->u.template_unification.tmpl,
3546 r->u.template_unification.explicit_targs,
3547 (make_tree_vec
3548 (r->u.template_unification.num_targs)),
3549 r->u.template_unification.args,
3550 r->u.template_unification.nargs,
3551 r->u.template_unification.return_type,
3552 r->u.template_unification.strict,
3553 r->u.template_unification.flags,
3554 true, false);
3555 break;
3556 case rr_invalid_copy:
3557 inform (cloc,
3558 " a constructor taking a single argument of its own "
3559 "class type is invalid");
3560 break;
3561 case rr_constraint_failure:
3562 {
3563 tree tmpl = r->u.template_instantiation.tmpl;
3564 tree args = r->u.template_instantiation.targs;
3565 diagnose_constraints (cloc, tmpl, args);
3566 }
3567 break;
3568 case rr_inherited_ctor:
3569 inform (cloc, " an inherited constructor is not a candidate for "
3570 "initialization from an expression of the same or derived "
3571 "type");
3572 break;
3573 case rr_none:
3574 default:
3575 /* This candidate didn't have any issues or we failed to
3576 handle a particular code. Either way... */
3577 gcc_unreachable ();
3578 }
3579 }
3580 }
3581
3582 static void
print_z_candidates(location_t loc,struct z_candidate * candidates)3583 print_z_candidates (location_t loc, struct z_candidate *candidates)
3584 {
3585 struct z_candidate *cand1;
3586 struct z_candidate **cand2;
3587
3588 if (!candidates)
3589 return;
3590
3591 /* Remove non-viable deleted candidates. */
3592 cand1 = candidates;
3593 for (cand2 = &cand1; *cand2; )
3594 {
3595 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
3596 && !(*cand2)->viable
3597 && DECL_DELETED_FN ((*cand2)->fn))
3598 *cand2 = (*cand2)->next;
3599 else
3600 cand2 = &(*cand2)->next;
3601 }
3602 /* ...if there are any non-deleted ones. */
3603 if (cand1)
3604 candidates = cand1;
3605
3606 /* There may be duplicates in the set of candidates. We put off
3607 checking this condition as long as possible, since we have no way
3608 to eliminate duplicates from a set of functions in less than n^2
3609 time. Now we are about to emit an error message, so it is more
3610 permissible to go slowly. */
3611 for (cand1 = candidates; cand1; cand1 = cand1->next)
3612 {
3613 tree fn = cand1->fn;
3614 /* Skip builtin candidates and conversion functions. */
3615 if (!DECL_P (fn))
3616 continue;
3617 cand2 = &cand1->next;
3618 while (*cand2)
3619 {
3620 if (DECL_P ((*cand2)->fn)
3621 && equal_functions (fn, (*cand2)->fn))
3622 *cand2 = (*cand2)->next;
3623 else
3624 cand2 = &(*cand2)->next;
3625 }
3626 }
3627
3628 for (; candidates; candidates = candidates->next)
3629 print_z_candidate (loc, "candidate:", candidates);
3630 }
3631
3632 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3633 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3634 the result of the conversion function to convert it to the final
3635 desired type. Merge the two sequences into a single sequence,
3636 and return the merged sequence. */
3637
3638 static conversion *
merge_conversion_sequences(conversion * user_seq,conversion * std_seq)3639 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
3640 {
3641 conversion **t;
3642 bool bad = user_seq->bad_p;
3643
3644 gcc_assert (user_seq->kind == ck_user);
3645
3646 /* Find the end of the second conversion sequence. */
3647 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
3648 {
3649 /* The entire sequence is a user-conversion sequence. */
3650 (*t)->user_conv_p = true;
3651 if (bad)
3652 (*t)->bad_p = true;
3653 }
3654
3655 if ((*t)->rvaluedness_matches_p)
3656 /* We're binding a reference directly to the result of the conversion.
3657 build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
3658 type, but we want it back. */
3659 user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn));
3660
3661 /* Replace the identity conversion with the user conversion
3662 sequence. */
3663 *t = user_seq;
3664
3665 return std_seq;
3666 }
3667
3668 /* Handle overload resolution for initializing an object of class type from
3669 an initializer list. First we look for a suitable constructor that
3670 takes a std::initializer_list; if we don't find one, we then look for a
3671 non-list constructor.
3672
3673 Parameters are as for add_candidates, except that the arguments are in
3674 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
3675 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3676
3677 static void
add_list_candidates(tree fns,tree first_arg,const vec<tree,va_gc> * args,tree totype,tree explicit_targs,bool template_only,tree conversion_path,tree access_path,int flags,struct z_candidate ** candidates,tsubst_flags_t complain)3678 add_list_candidates (tree fns, tree first_arg,
3679 const vec<tree, va_gc> *args, tree totype,
3680 tree explicit_targs, bool template_only,
3681 tree conversion_path, tree access_path,
3682 int flags,
3683 struct z_candidate **candidates,
3684 tsubst_flags_t complain)
3685 {
3686 gcc_assert (*candidates == NULL);
3687
3688 /* We're looking for a ctor for list-initialization. */
3689 flags |= LOOKUP_LIST_INIT_CTOR;
3690 /* And we don't allow narrowing conversions. We also use this flag to
3691 avoid the copy constructor call for copy-list-initialization. */
3692 flags |= LOOKUP_NO_NARROWING;
3693
3694 unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
3695 tree init_list = (*args)[nart];
3696
3697 /* Always use the default constructor if the list is empty (DR 990). */
3698 if (CONSTRUCTOR_NELTS (init_list) == 0
3699 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
3700 ;
3701 /* If the class has a list ctor, try passing the list as a single
3702 argument first, but only consider list ctors. */
3703 else if (TYPE_HAS_LIST_CTOR (totype))
3704 {
3705 flags |= LOOKUP_LIST_ONLY;
3706 add_candidates (fns, first_arg, args, NULL_TREE,
3707 explicit_targs, template_only, conversion_path,
3708 access_path, flags, candidates, complain);
3709 if (any_strictly_viable (*candidates))
3710 return;
3711 }
3712
3713 /* Expand the CONSTRUCTOR into a new argument vec. */
3714 vec<tree, va_gc> *new_args;
3715 vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
3716 for (unsigned i = 0; i < nart; ++i)
3717 new_args->quick_push ((*args)[i]);
3718 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (init_list); ++i)
3719 new_args->quick_push (CONSTRUCTOR_ELT (init_list, i)->value);
3720
3721 /* We aren't looking for list-ctors anymore. */
3722 flags &= ~LOOKUP_LIST_ONLY;
3723 /* We allow more user-defined conversions within an init-list. */
3724 flags &= ~LOOKUP_NO_CONVERSION;
3725
3726 add_candidates (fns, first_arg, new_args, NULL_TREE,
3727 explicit_targs, template_only, conversion_path,
3728 access_path, flags, candidates, complain);
3729 }
3730
3731 /* Returns the best overload candidate to perform the requested
3732 conversion. This function is used for three the overloading situations
3733 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3734 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3735 per [dcl.init.ref], so we ignore temporary bindings. */
3736
3737 static struct z_candidate *
build_user_type_conversion_1(tree totype,tree expr,int flags,tsubst_flags_t complain)3738 build_user_type_conversion_1 (tree totype, tree expr, int flags,
3739 tsubst_flags_t complain)
3740 {
3741 struct z_candidate *candidates, *cand;
3742 tree fromtype;
3743 tree ctors = NULL_TREE;
3744 tree conv_fns = NULL_TREE;
3745 conversion *conv = NULL;
3746 tree first_arg = NULL_TREE;
3747 vec<tree, va_gc> *args = NULL;
3748 bool any_viable_p;
3749 int convflags;
3750
3751 if (!expr)
3752 return NULL;
3753
3754 fromtype = TREE_TYPE (expr);
3755
3756 /* We represent conversion within a hierarchy using RVALUE_CONV and
3757 BASE_CONV, as specified by [over.best.ics]; these become plain
3758 constructor calls, as specified in [dcl.init]. */
3759 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
3760 || !DERIVED_FROM_P (totype, fromtype));
3761
3762 if (CLASS_TYPE_P (totype))
3763 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
3764 creating a garbage BASELINK; constructors can't be inherited. */
3765 ctors = get_class_binding (totype, complete_ctor_identifier);
3766
3767 if (MAYBE_CLASS_TYPE_P (fromtype))
3768 {
3769 tree to_nonref = non_reference (totype);
3770 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
3771 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
3772 && DERIVED_FROM_P (to_nonref, fromtype)))
3773 {
3774 /* [class.conv.fct] A conversion function is never used to
3775 convert a (possibly cv-qualified) object to the (possibly
3776 cv-qualified) same object type (or a reference to it), to a
3777 (possibly cv-qualified) base class of that type (or a
3778 reference to it)... */
3779 }
3780 else
3781 conv_fns = lookup_conversions (fromtype);
3782 }
3783
3784 candidates = 0;
3785 flags |= LOOKUP_NO_CONVERSION;
3786 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3787 flags |= LOOKUP_NO_NARROWING;
3788
3789 /* It's OK to bind a temporary for converting constructor arguments, but
3790 not in converting the return value of a conversion operator. */
3791 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
3792 | (flags & LOOKUP_NO_NARROWING));
3793 flags &= ~LOOKUP_NO_TEMP_BIND;
3794
3795 if (ctors)
3796 {
3797 int ctorflags = flags;
3798
3799 first_arg = build_dummy_object (totype);
3800
3801 /* We should never try to call the abstract or base constructor
3802 from here. */
3803 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
3804 && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
3805
3806 args = make_tree_vector_single (expr);
3807 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3808 {
3809 /* List-initialization. */
3810 add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
3811 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
3812 ctorflags, &candidates, complain);
3813 }
3814 else
3815 {
3816 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
3817 TYPE_BINFO (totype), TYPE_BINFO (totype),
3818 ctorflags, &candidates, complain);
3819 }
3820
3821 for (cand = candidates; cand; cand = cand->next)
3822 {
3823 cand->second_conv = build_identity_conv (totype, NULL_TREE);
3824
3825 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
3826 set, then this is copy-initialization. In that case, "The
3827 result of the call is then used to direct-initialize the
3828 object that is the destination of the copy-initialization."
3829 [dcl.init]
3830
3831 We represent this in the conversion sequence with an
3832 rvalue conversion, which means a constructor call. */
3833 if (TREE_CODE (totype) != REFERENCE_TYPE
3834 && !(convflags & LOOKUP_NO_TEMP_BIND))
3835 cand->second_conv
3836 = build_conv (ck_rvalue, totype, cand->second_conv);
3837 }
3838 }
3839
3840 if (conv_fns)
3841 {
3842 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3843 first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
3844 else
3845 first_arg = expr;
3846 }
3847
3848 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
3849 {
3850 tree conversion_path = TREE_PURPOSE (conv_fns);
3851 struct z_candidate *old_candidates;
3852
3853 /* If we are called to convert to a reference type, we are trying to
3854 find a direct binding, so don't even consider temporaries. If
3855 we don't find a direct binding, the caller will try again to
3856 look for a temporary binding. */
3857 if (TREE_CODE (totype) == REFERENCE_TYPE)
3858 convflags |= LOOKUP_NO_TEMP_BIND;
3859
3860 old_candidates = candidates;
3861 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3862 NULL_TREE, false,
3863 conversion_path, TYPE_BINFO (fromtype),
3864 flags, &candidates, complain);
3865
3866 for (cand = candidates; cand != old_candidates; cand = cand->next)
3867 {
3868 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
3869 conversion *ics
3870 = implicit_conversion (totype,
3871 rettype,
3872 0,
3873 /*c_cast_p=*/false, convflags,
3874 complain);
3875
3876 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3877 copy-initialization. In that case, "The result of the
3878 call is then used to direct-initialize the object that is
3879 the destination of the copy-initialization." [dcl.init]
3880
3881 We represent this in the conversion sequence with an
3882 rvalue conversion, which means a constructor call. But
3883 don't add a second rvalue conversion if there's already
3884 one there. Which there really shouldn't be, but it's
3885 harmless since we'd add it here anyway. */
3886 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3887 && !(convflags & LOOKUP_NO_TEMP_BIND))
3888 ics = build_conv (ck_rvalue, totype, ics);
3889
3890 cand->second_conv = ics;
3891
3892 if (!ics)
3893 {
3894 cand->viable = 0;
3895 cand->reason = arg_conversion_rejection (NULL_TREE, -2,
3896 rettype, totype);
3897 }
3898 else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p
3899 /* Limit this to non-templates for now (PR90546). */
3900 && !cand->template_decl
3901 && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE)
3902 {
3903 /* If we are called to convert to a reference type, we are trying
3904 to find a direct binding per [over.match.ref], so rvaluedness
3905 must match for non-functions. */
3906 cand->viable = 0;
3907 }
3908 else if (DECL_NONCONVERTING_P (cand->fn)
3909 && ics->rank > cr_exact)
3910 {
3911 /* 13.3.1.5: For direct-initialization, those explicit
3912 conversion functions that are not hidden within S and
3913 yield type T or a type that can be converted to type T
3914 with a qualification conversion (4.4) are also candidate
3915 functions. */
3916 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
3917 I've raised this issue with the committee. --jason 9/2011 */
3918 cand->viable = -1;
3919 cand->reason = explicit_conversion_rejection (rettype, totype);
3920 }
3921 else if (cand->viable == 1 && ics->bad_p)
3922 {
3923 cand->viable = -1;
3924 cand->reason
3925 = bad_arg_conversion_rejection (NULL_TREE, -2,
3926 rettype, totype);
3927 }
3928 else if (primary_template_specialization_p (cand->fn)
3929 && ics->rank > cr_exact)
3930 {
3931 /* 13.3.3.1.2: If the user-defined conversion is specified by
3932 a specialization of a conversion function template, the
3933 second standard conversion sequence shall have exact match
3934 rank. */
3935 cand->viable = -1;
3936 cand->reason = template_conversion_rejection (rettype, totype);
3937 }
3938 }
3939 }
3940
3941 candidates = splice_viable (candidates, false, &any_viable_p);
3942 if (!any_viable_p)
3943 {
3944 if (args)
3945 release_tree_vector (args);
3946 return NULL;
3947 }
3948
3949 cand = tourney (candidates, complain);
3950 if (cand == 0)
3951 {
3952 if (complain & tf_error)
3953 {
3954 error ("conversion from %qH to %qI is ambiguous",
3955 fromtype, totype);
3956 print_z_candidates (location_of (expr), candidates);
3957 }
3958
3959 cand = candidates; /* any one will do */
3960 cand->second_conv = build_ambiguous_conv (totype, expr);
3961 cand->second_conv->user_conv_p = true;
3962 if (!any_strictly_viable (candidates))
3963 cand->second_conv->bad_p = true;
3964 if (flags & LOOKUP_ONLYCONVERTING)
3965 cand->second_conv->need_temporary_p = true;
3966 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3967 ambiguous conversion is no worse than another user-defined
3968 conversion. */
3969
3970 return cand;
3971 }
3972
3973 tree convtype;
3974 if (!DECL_CONSTRUCTOR_P (cand->fn))
3975 convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
3976 else if (cand->second_conv->kind == ck_rvalue)
3977 /* DR 5: [in the first step of copy-initialization]...if the function
3978 is a constructor, the call initializes a temporary of the
3979 cv-unqualified version of the destination type. */
3980 convtype = cv_unqualified (totype);
3981 else
3982 convtype = totype;
3983 /* Build the user conversion sequence. */
3984 conv = build_conv
3985 (ck_user,
3986 convtype,
3987 build_identity_conv (TREE_TYPE (expr), expr));
3988 conv->cand = cand;
3989 if (cand->viable == -1)
3990 conv->bad_p = true;
3991
3992 /* Remember that this was a list-initialization. */
3993 if (flags & LOOKUP_NO_NARROWING)
3994 conv->check_narrowing = true;
3995
3996 /* Combine it with the second conversion sequence. */
3997 cand->second_conv = merge_conversion_sequences (conv,
3998 cand->second_conv);
3999
4000 return cand;
4001 }
4002
4003 /* Wrapper for above. */
4004
4005 tree
build_user_type_conversion(tree totype,tree expr,int flags,tsubst_flags_t complain)4006 build_user_type_conversion (tree totype, tree expr, int flags,
4007 tsubst_flags_t complain)
4008 {
4009 struct z_candidate *cand;
4010 tree ret;
4011
4012 bool subtime = timevar_cond_start (TV_OVERLOAD);
4013 cand = build_user_type_conversion_1 (totype, expr, flags, complain);
4014
4015 if (cand)
4016 {
4017 if (cand->second_conv->kind == ck_ambig)
4018 ret = error_mark_node;
4019 else
4020 {
4021 expr = convert_like (cand->second_conv, expr, complain);
4022 ret = convert_from_reference (expr);
4023 }
4024 }
4025 else
4026 ret = NULL_TREE;
4027
4028 timevar_cond_stop (TV_OVERLOAD, subtime);
4029 return ret;
4030 }
4031
4032 /* Subroutine of convert_nontype_argument.
4033
4034 EXPR is an expression used in a context that requires a converted
4035 constant-expression, such as a template non-type parameter. Do any
4036 necessary conversions (that are permitted for converted
4037 constant-expressions) to convert it to the desired type.
4038
4039 If conversion is successful, returns the converted expression;
4040 otherwise, returns error_mark_node. */
4041
4042 tree
build_converted_constant_expr(tree type,tree expr,tsubst_flags_t complain)4043 build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
4044 {
4045 conversion *conv;
4046 void *p;
4047 tree t;
4048 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
4049
4050 if (error_operand_p (expr))
4051 return error_mark_node;
4052
4053 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4054 p = conversion_obstack_alloc (0);
4055
4056 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4057 /*c_cast_p=*/false,
4058 LOOKUP_IMPLICIT, complain);
4059
4060 /* A converted constant expression of type T is an expression, implicitly
4061 converted to type T, where the converted expression is a constant
4062 expression and the implicit conversion sequence contains only
4063
4064 * user-defined conversions,
4065 * lvalue-to-rvalue conversions (7.1),
4066 * array-to-pointer conversions (7.2),
4067 * function-to-pointer conversions (7.3),
4068 * qualification conversions (7.5),
4069 * integral promotions (7.6),
4070 * integral conversions (7.8) other than narrowing conversions (11.6.4),
4071 * null pointer conversions (7.11) from std::nullptr_t,
4072 * null member pointer conversions (7.12) from std::nullptr_t, and
4073 * function pointer conversions (7.13),
4074
4075 and where the reference binding (if any) binds directly. */
4076
4077 for (conversion *c = conv;
4078 conv && c->kind != ck_identity;
4079 c = next_conversion (c))
4080 {
4081 switch (c->kind)
4082 {
4083 /* A conversion function is OK. If it isn't constexpr, we'll
4084 complain later that the argument isn't constant. */
4085 case ck_user:
4086 /* The lvalue-to-rvalue conversion is OK. */
4087 case ck_rvalue:
4088 /* Array-to-pointer and function-to-pointer. */
4089 case ck_lvalue:
4090 /* Function pointer conversions. */
4091 case ck_fnptr:
4092 /* Qualification conversions. */
4093 case ck_qual:
4094 break;
4095
4096 case ck_ref_bind:
4097 if (c->need_temporary_p)
4098 {
4099 if (complain & tf_error)
4100 error_at (loc, "initializing %qH with %qI in converted "
4101 "constant expression does not bind directly",
4102 type, next_conversion (c)->type);
4103 conv = NULL;
4104 }
4105 break;
4106
4107 case ck_base:
4108 case ck_pmem:
4109 case ck_ptr:
4110 case ck_std:
4111 t = next_conversion (c)->type;
4112 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
4113 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4114 /* Integral promotion or conversion. */
4115 break;
4116 if (NULLPTR_TYPE_P (t))
4117 /* Conversion from nullptr to pointer or pointer-to-member. */
4118 break;
4119
4120 if (complain & tf_error)
4121 error_at (loc, "conversion from %qH to %qI in a "
4122 "converted constant expression", t, type);
4123 /* fall through. */
4124
4125 default:
4126 conv = NULL;
4127 break;
4128 }
4129 }
4130
4131 /* Avoid confusing convert_nontype_argument by introducing
4132 a redundant conversion to the same reference type. */
4133 if (conv && conv->kind == ck_ref_bind
4134 && REFERENCE_REF_P (expr))
4135 {
4136 tree ref = TREE_OPERAND (expr, 0);
4137 if (same_type_p (type, TREE_TYPE (ref)))
4138 return ref;
4139 }
4140
4141 if (conv)
4142 expr = convert_like (conv, expr, complain);
4143 else
4144 expr = error_mark_node;
4145
4146 /* Free all the conversions we allocated. */
4147 obstack_free (&conversion_obstack, p);
4148
4149 return expr;
4150 }
4151
4152 /* Do any initial processing on the arguments to a function call. */
4153
4154 static vec<tree, va_gc> *
resolve_args(vec<tree,va_gc> * args,tsubst_flags_t complain)4155 resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
4156 {
4157 unsigned int ix;
4158 tree arg;
4159
4160 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
4161 {
4162 if (error_operand_p (arg))
4163 return NULL;
4164 else if (VOID_TYPE_P (TREE_TYPE (arg)))
4165 {
4166 if (complain & tf_error)
4167 error ("invalid use of void expression");
4168 return NULL;
4169 }
4170 else if (invalid_nonstatic_memfn_p (input_location, arg, complain))
4171 return NULL;
4172 }
4173 return args;
4174 }
4175
4176 /* Perform overload resolution on FN, which is called with the ARGS.
4177
4178 Return the candidate function selected by overload resolution, or
4179 NULL if the event that overload resolution failed. In the case
4180 that overload resolution fails, *CANDIDATES will be the set of
4181 candidates considered, and ANY_VIABLE_P will be set to true or
4182 false to indicate whether or not any of the candidates were
4183 viable.
4184
4185 The ARGS should already have gone through RESOLVE_ARGS before this
4186 function is called. */
4187
4188 static struct z_candidate *
perform_overload_resolution(tree fn,const vec<tree,va_gc> * args,struct z_candidate ** candidates,bool * any_viable_p,tsubst_flags_t complain)4189 perform_overload_resolution (tree fn,
4190 const vec<tree, va_gc> *args,
4191 struct z_candidate **candidates,
4192 bool *any_viable_p, tsubst_flags_t complain)
4193 {
4194 struct z_candidate *cand;
4195 tree explicit_targs;
4196 int template_only;
4197
4198 bool subtime = timevar_cond_start (TV_OVERLOAD);
4199
4200 explicit_targs = NULL_TREE;
4201 template_only = 0;
4202
4203 *candidates = NULL;
4204 *any_viable_p = true;
4205
4206 /* Check FN. */
4207 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
4208 || TREE_CODE (fn) == TEMPLATE_DECL
4209 || TREE_CODE (fn) == OVERLOAD
4210 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
4211
4212 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4213 {
4214 explicit_targs = TREE_OPERAND (fn, 1);
4215 fn = TREE_OPERAND (fn, 0);
4216 template_only = 1;
4217 }
4218
4219 /* Add the various candidate functions. */
4220 add_candidates (fn, NULL_TREE, args, NULL_TREE,
4221 explicit_targs, template_only,
4222 /*conversion_path=*/NULL_TREE,
4223 /*access_path=*/NULL_TREE,
4224 LOOKUP_NORMAL,
4225 candidates, complain);
4226
4227 *candidates = splice_viable (*candidates, false, any_viable_p);
4228 if (*any_viable_p)
4229 cand = tourney (*candidates, complain);
4230 else
4231 cand = NULL;
4232
4233 timevar_cond_stop (TV_OVERLOAD, subtime);
4234 return cand;
4235 }
4236
4237 /* Print an error message about being unable to build a call to FN with
4238 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
4239 be located; CANDIDATES is a possibly empty list of such
4240 functions. */
4241
4242 static void
print_error_for_call_failure(tree fn,vec<tree,va_gc> * args,struct z_candidate * candidates)4243 print_error_for_call_failure (tree fn, vec<tree, va_gc> *args,
4244 struct z_candidate *candidates)
4245 {
4246 tree targs = NULL_TREE;
4247 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4248 {
4249 targs = TREE_OPERAND (fn, 1);
4250 fn = TREE_OPERAND (fn, 0);
4251 }
4252 tree name = OVL_NAME (fn);
4253 location_t loc = location_of (name);
4254 if (targs)
4255 name = lookup_template_function (name, targs);
4256
4257 if (!any_strictly_viable (candidates))
4258 error_at (loc, "no matching function for call to %<%D(%A)%>",
4259 name, build_tree_list_vec (args));
4260 else
4261 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
4262 name, build_tree_list_vec (args));
4263 if (candidates)
4264 print_z_candidates (loc, candidates);
4265 }
4266
4267 /* Return an expression for a call to FN (a namespace-scope function,
4268 or a static member function) with the ARGS. This may change
4269 ARGS. */
4270
4271 tree
build_new_function_call(tree fn,vec<tree,va_gc> ** args,tsubst_flags_t complain)4272 build_new_function_call (tree fn, vec<tree, va_gc> **args,
4273 tsubst_flags_t complain)
4274 {
4275 struct z_candidate *candidates, *cand;
4276 bool any_viable_p;
4277 void *p;
4278 tree result;
4279
4280 if (args != NULL && *args != NULL)
4281 {
4282 *args = resolve_args (*args, complain);
4283 if (*args == NULL)
4284 return error_mark_node;
4285 }
4286
4287 if (flag_tm)
4288 tm_malloc_replacement (fn);
4289
4290 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4291 p = conversion_obstack_alloc (0);
4292
4293 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
4294 complain);
4295
4296 if (!cand)
4297 {
4298 if (complain & tf_error)
4299 {
4300 // If there is a single (non-viable) function candidate,
4301 // let the error be diagnosed by cp_build_function_call_vec.
4302 if (!any_viable_p && candidates && ! candidates->next
4303 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
4304 return cp_build_function_call_vec (candidates->fn, args, complain);
4305
4306 // Otherwise, emit notes for non-viable candidates.
4307 print_error_for_call_failure (fn, *args, candidates);
4308 }
4309 result = error_mark_node;
4310 }
4311 else
4312 {
4313 int flags = LOOKUP_NORMAL;
4314 /* If fn is template_id_expr, the call has explicit template arguments
4315 (e.g. func<int>(5)), communicate this info to build_over_call
4316 through flags so that later we can use it to decide whether to warn
4317 about peculiar null pointer conversion. */
4318 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4319 {
4320 /* If overload resolution selects a specialization of a
4321 function concept for non-dependent template arguments,
4322 the expression is true if the constraints are satisfied
4323 and false otherwise.
4324
4325 NOTE: This is an extension of Concepts Lite TS that
4326 allows constraints to be used in expressions. */
4327 if (flag_concepts && !processing_template_decl)
4328 {
4329 tree tmpl = DECL_TI_TEMPLATE (cand->fn);
4330 tree targs = DECL_TI_ARGS (cand->fn);
4331 tree decl = DECL_TEMPLATE_RESULT (tmpl);
4332 if (DECL_DECLARED_CONCEPT_P (decl))
4333 return evaluate_function_concept (decl, targs);
4334 }
4335
4336 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
4337 }
4338
4339 result = build_over_call (cand, flags, complain);
4340 }
4341
4342 /* Free all the conversions we allocated. */
4343 obstack_free (&conversion_obstack, p);
4344
4345 return result;
4346 }
4347
4348 /* Build a call to a global operator new. FNNAME is the name of the
4349 operator (either "operator new" or "operator new[]") and ARGS are
4350 the arguments provided. This may change ARGS. *SIZE points to the
4351 total number of bytes required by the allocation, and is updated if
4352 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
4353 be used. If this function determines that no cookie should be
4354 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
4355 is not NULL_TREE, it is evaluated before calculating the final
4356 array size, and if it fails, the array size is replaced with
4357 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
4358 is non-NULL, it will be set, upon return, to the allocation
4359 function called. */
4360
4361 tree
build_operator_new_call(tree fnname,vec<tree,va_gc> ** args,tree * size,tree * cookie_size,tree align_arg,tree size_check,tree * fn,tsubst_flags_t complain)4362 build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
4363 tree *size, tree *cookie_size,
4364 tree align_arg, tree size_check,
4365 tree *fn, tsubst_flags_t complain)
4366 {
4367 tree original_size = *size;
4368 tree fns;
4369 struct z_candidate *candidates;
4370 struct z_candidate *cand = NULL;
4371 bool any_viable_p;
4372
4373 if (fn)
4374 *fn = NULL_TREE;
4375 /* Set to (size_t)-1 if the size check fails. */
4376 if (size_check != NULL_TREE)
4377 {
4378 tree errval = TYPE_MAX_VALUE (sizetype);
4379 if (cxx_dialect >= cxx11 && flag_exceptions)
4380 errval = throw_bad_array_new_length ();
4381 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4382 original_size, errval);
4383 }
4384 vec_safe_insert (*args, 0, *size);
4385 *args = resolve_args (*args, complain);
4386 if (*args == NULL)
4387 return error_mark_node;
4388
4389 /* Based on:
4390
4391 [expr.new]
4392
4393 If this lookup fails to find the name, or if the allocated type
4394 is not a class type, the allocation function's name is looked
4395 up in the global scope.
4396
4397 we disregard block-scope declarations of "operator new". */
4398 fns = lookup_name_real (fnname, 0, 1, /*block_p=*/false, 0, 0);
4399 fns = lookup_arg_dependent (fnname, fns, *args);
4400
4401 if (align_arg)
4402 {
4403 vec<tree, va_gc>* align_args
4404 = vec_copy_and_insert (*args, align_arg, 1);
4405 cand = perform_overload_resolution (fns, align_args, &candidates,
4406 &any_viable_p, tf_none);
4407 if (cand)
4408 *args = align_args;
4409 /* If no aligned allocation function matches, try again without the
4410 alignment. */
4411 }
4412
4413 /* Figure out what function is being called. */
4414 if (!cand)
4415 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
4416 complain);
4417
4418 /* If no suitable function could be found, issue an error message
4419 and give up. */
4420 if (!cand)
4421 {
4422 if (complain & tf_error)
4423 print_error_for_call_failure (fns, *args, candidates);
4424 return error_mark_node;
4425 }
4426
4427 /* If a cookie is required, add some extra space. Whether
4428 or not a cookie is required cannot be determined until
4429 after we know which function was called. */
4430 if (*cookie_size)
4431 {
4432 bool use_cookie = true;
4433 tree arg_types;
4434
4435 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
4436 /* Skip the size_t parameter. */
4437 arg_types = TREE_CHAIN (arg_types);
4438 /* Check the remaining parameters (if any). */
4439 if (arg_types
4440 && TREE_CHAIN (arg_types) == void_list_node
4441 && same_type_p (TREE_VALUE (arg_types),
4442 ptr_type_node))
4443 use_cookie = false;
4444 /* If we need a cookie, adjust the number of bytes allocated. */
4445 if (use_cookie)
4446 {
4447 /* Update the total size. */
4448 *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
4449 if (size_check)
4450 {
4451 /* Set to (size_t)-1 if the size check fails. */
4452 gcc_assert (size_check != NULL_TREE);
4453 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4454 *size, TYPE_MAX_VALUE (sizetype));
4455 }
4456 /* Update the argument list to reflect the adjusted size. */
4457 (**args)[0] = *size;
4458 }
4459 else
4460 *cookie_size = NULL_TREE;
4461 }
4462
4463 /* Tell our caller which function we decided to call. */
4464 if (fn)
4465 *fn = cand->fn;
4466
4467 /* Build the CALL_EXPR. */
4468 return build_over_call (cand, LOOKUP_NORMAL, complain);
4469 }
4470
4471 /* Build a new call to operator(). This may change ARGS. */
4472
4473 static tree
build_op_call_1(tree obj,vec<tree,va_gc> ** args,tsubst_flags_t complain)4474 build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4475 {
4476 struct z_candidate *candidates = 0, *cand;
4477 tree fns, convs, first_mem_arg = NULL_TREE;
4478 bool any_viable_p;
4479 tree result = NULL_TREE;
4480 void *p;
4481
4482 obj = mark_lvalue_use (obj);
4483
4484 if (error_operand_p (obj))
4485 return error_mark_node;
4486
4487 tree type = TREE_TYPE (obj);
4488
4489 obj = prep_operand (obj);
4490
4491 if (TYPE_PTRMEMFUNC_P (type))
4492 {
4493 if (complain & tf_error)
4494 /* It's no good looking for an overloaded operator() on a
4495 pointer-to-member-function. */
4496 error ("pointer-to-member function %qE cannot be called without "
4497 "an object; consider using %<.*%> or %<->*%>", obj);
4498 return error_mark_node;
4499 }
4500
4501 if (TYPE_BINFO (type))
4502 {
4503 fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1);
4504 if (fns == error_mark_node)
4505 return error_mark_node;
4506 }
4507 else
4508 fns = NULL_TREE;
4509
4510 if (args != NULL && *args != NULL)
4511 {
4512 *args = resolve_args (*args, complain);
4513 if (*args == NULL)
4514 return error_mark_node;
4515 }
4516
4517 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4518 p = conversion_obstack_alloc (0);
4519
4520 if (fns)
4521 {
4522 first_mem_arg = obj;
4523
4524 add_candidates (BASELINK_FUNCTIONS (fns),
4525 first_mem_arg, *args, NULL_TREE,
4526 NULL_TREE, false,
4527 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
4528 LOOKUP_NORMAL, &candidates, complain);
4529 }
4530
4531 convs = lookup_conversions (type);
4532
4533 for (; convs; convs = TREE_CHAIN (convs))
4534 {
4535 tree totype = TREE_TYPE (convs);
4536
4537 if (TYPE_PTRFN_P (totype)
4538 || TYPE_REFFN_P (totype)
4539 || (TREE_CODE (totype) == REFERENCE_TYPE
4540 && TYPE_PTRFN_P (TREE_TYPE (totype))))
4541 for (ovl_iterator iter (TREE_VALUE (convs)); iter; ++iter)
4542 {
4543 tree fn = *iter;
4544
4545 if (DECL_NONCONVERTING_P (fn))
4546 continue;
4547
4548 if (TREE_CODE (fn) == TEMPLATE_DECL)
4549 add_template_conv_candidate
4550 (&candidates, fn, obj, *args, totype,
4551 /*access_path=*/NULL_TREE,
4552 /*conversion_path=*/NULL_TREE, complain);
4553 else
4554 add_conv_candidate (&candidates, fn, obj,
4555 *args, /*conversion_path=*/NULL_TREE,
4556 /*access_path=*/NULL_TREE, complain);
4557 }
4558 }
4559
4560 /* Be strict here because if we choose a bad conversion candidate, the
4561 errors we get won't mention the call context. */
4562 candidates = splice_viable (candidates, true, &any_viable_p);
4563 if (!any_viable_p)
4564 {
4565 if (complain & tf_error)
4566 {
4567 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
4568 build_tree_list_vec (*args));
4569 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4570 }
4571 result = error_mark_node;
4572 }
4573 else
4574 {
4575 cand = tourney (candidates, complain);
4576 if (cand == 0)
4577 {
4578 if (complain & tf_error)
4579 {
4580 error ("call of %<(%T) (%A)%> is ambiguous",
4581 TREE_TYPE (obj), build_tree_list_vec (*args));
4582 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4583 }
4584 result = error_mark_node;
4585 }
4586 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
4587 && DECL_OVERLOADED_OPERATOR_P (cand->fn)
4588 && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
4589 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4590 else
4591 {
4592 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
4593 obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
4594 -1, complain);
4595 else
4596 {
4597 gcc_checking_assert (TYPE_P (cand->fn));
4598 obj = convert_like (cand->convs[0], obj, complain);
4599 }
4600 obj = convert_from_reference (obj);
4601 result = cp_build_function_call_vec (obj, args, complain);
4602 }
4603 }
4604
4605 /* Free all the conversions we allocated. */
4606 obstack_free (&conversion_obstack, p);
4607
4608 return result;
4609 }
4610
4611 /* Wrapper for above. */
4612
4613 tree
build_op_call(tree obj,vec<tree,va_gc> ** args,tsubst_flags_t complain)4614 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4615 {
4616 tree ret;
4617 bool subtime = timevar_cond_start (TV_OVERLOAD);
4618 ret = build_op_call_1 (obj, args, complain);
4619 timevar_cond_stop (TV_OVERLOAD, subtime);
4620 return ret;
4621 }
4622
4623 /* Called by op_error to prepare format strings suitable for the error
4624 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4625 and a suffix (controlled by NTYPES). */
4626
4627 static const char *
op_error_string(const char * errmsg,int ntypes,bool match)4628 op_error_string (const char *errmsg, int ntypes, bool match)
4629 {
4630 const char *msg;
4631
4632 const char *msgp = concat (match ? G_("ambiguous overload for ")
4633 : G_("no match for "), errmsg, NULL);
4634
4635 if (ntypes == 3)
4636 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
4637 else if (ntypes == 2)
4638 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
4639 else
4640 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
4641
4642 return msg;
4643 }
4644
4645 static void
op_error(location_t loc,enum tree_code code,enum tree_code code2,tree arg1,tree arg2,tree arg3,bool match)4646 op_error (location_t loc, enum tree_code code, enum tree_code code2,
4647 tree arg1, tree arg2, tree arg3, bool match)
4648 {
4649 bool assop = code == MODIFY_EXPR;
4650 const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
4651
4652 switch (code)
4653 {
4654 case COND_EXPR:
4655 if (flag_diagnostics_show_caret)
4656 error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
4657 3, match),
4658 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4659 else
4660 error_at (loc, op_error_string (G_("ternary %<operator?:%> "
4661 "in %<%E ? %E : %E%>"), 3, match),
4662 arg1, arg2, arg3,
4663 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4664 break;
4665
4666 case POSTINCREMENT_EXPR:
4667 case POSTDECREMENT_EXPR:
4668 if (flag_diagnostics_show_caret)
4669 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4670 opname, TREE_TYPE (arg1));
4671 else
4672 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4673 1, match),
4674 opname, arg1, opname, TREE_TYPE (arg1));
4675 break;
4676
4677 case ARRAY_REF:
4678 if (flag_diagnostics_show_caret)
4679 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
4680 TREE_TYPE (arg1), TREE_TYPE (arg2));
4681 else
4682 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
4683 2, match),
4684 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
4685 break;
4686
4687 case REALPART_EXPR:
4688 case IMAGPART_EXPR:
4689 if (flag_diagnostics_show_caret)
4690 error_at (loc, op_error_string (G_("%qs"), 1, match),
4691 opname, TREE_TYPE (arg1));
4692 else
4693 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
4694 opname, opname, arg1, TREE_TYPE (arg1));
4695 break;
4696
4697 default:
4698 if (arg2)
4699 if (flag_diagnostics_show_caret)
4700 error_at (loc, op_error_string (G_("%<operator%s%>"), 2, match),
4701 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
4702 else
4703 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
4704 2, match),
4705 opname, arg1, opname, arg2,
4706 TREE_TYPE (arg1), TREE_TYPE (arg2));
4707 else
4708 if (flag_diagnostics_show_caret)
4709 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4710 opname, TREE_TYPE (arg1));
4711 else
4712 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
4713 1, match),
4714 opname, opname, arg1, TREE_TYPE (arg1));
4715 break;
4716 }
4717 }
4718
4719 /* Return the implicit conversion sequence that could be used to
4720 convert E1 to E2 in [expr.cond]. */
4721
4722 static conversion *
conditional_conversion(tree e1,tree e2,tsubst_flags_t complain)4723 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
4724 {
4725 tree t1 = non_reference (TREE_TYPE (e1));
4726 tree t2 = non_reference (TREE_TYPE (e2));
4727 conversion *conv;
4728 bool good_base;
4729
4730 /* [expr.cond]
4731
4732 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
4733 implicitly converted (clause _conv_) to the type "lvalue reference to
4734 T2", subject to the constraint that in the conversion the
4735 reference must bind directly (_dcl.init.ref_) to an lvalue.
4736
4737 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
4738 implicitly converted to the type "rvalue reference to T2", subject to
4739 the constraint that the reference must bind directly. */
4740 if (glvalue_p (e2))
4741 {
4742 tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
4743 conv = implicit_conversion (rtype,
4744 t1,
4745 e1,
4746 /*c_cast_p=*/false,
4747 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
4748 |LOOKUP_ONLYCONVERTING,
4749 complain);
4750 if (conv && !conv->bad_p)
4751 return conv;
4752 }
4753
4754 /* If E2 is a prvalue or if neither of the conversions above can be done
4755 and at least one of the operands has (possibly cv-qualified) class
4756 type: */
4757 if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
4758 return NULL;
4759
4760 /* [expr.cond]
4761
4762 If E1 and E2 have class type, and the underlying class types are
4763 the same or one is a base class of the other: E1 can be converted
4764 to match E2 if the class of T2 is the same type as, or a base
4765 class of, the class of T1, and the cv-qualification of T2 is the
4766 same cv-qualification as, or a greater cv-qualification than, the
4767 cv-qualification of T1. If the conversion is applied, E1 is
4768 changed to an rvalue of type T2 that still refers to the original
4769 source class object (or the appropriate subobject thereof). */
4770 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
4771 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
4772 {
4773 if (good_base && at_least_as_qualified_p (t2, t1))
4774 {
4775 conv = build_identity_conv (t1, e1);
4776 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
4777 TYPE_MAIN_VARIANT (t2)))
4778 conv = build_conv (ck_base, t2, conv);
4779 else
4780 conv = build_conv (ck_rvalue, t2, conv);
4781 return conv;
4782 }
4783 else
4784 return NULL;
4785 }
4786 else
4787 /* [expr.cond]
4788
4789 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
4790 converted to the type that expression E2 would have if E2 were
4791 converted to an rvalue (or the type it has, if E2 is an rvalue). */
4792 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
4793 LOOKUP_IMPLICIT, complain);
4794 }
4795
4796 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
4797 arguments to the conditional expression. */
4798
4799 static tree
build_conditional_expr_1(location_t loc,tree arg1,tree arg2,tree arg3,tsubst_flags_t complain)4800 build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
4801 tsubst_flags_t complain)
4802 {
4803 tree arg2_type;
4804 tree arg3_type;
4805 tree result = NULL_TREE;
4806 tree result_type = NULL_TREE;
4807 bool is_glvalue = true;
4808 struct z_candidate *candidates = 0;
4809 struct z_candidate *cand;
4810 void *p;
4811 tree orig_arg2, orig_arg3;
4812
4813 /* As a G++ extension, the second argument to the conditional can be
4814 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
4815 c'.) If the second operand is omitted, make sure it is
4816 calculated only once. */
4817 if (!arg2)
4818 {
4819 if (complain & tf_error)
4820 pedwarn (loc, OPT_Wpedantic,
4821 "ISO C++ forbids omitting the middle term of a ?: expression");
4822
4823 if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
4824 warn_for_omitted_condop (loc, arg1);
4825
4826 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
4827 if (lvalue_p (arg1))
4828 arg2 = arg1 = cp_stabilize_reference (arg1);
4829 else
4830 arg2 = arg1 = cp_save_expr (arg1);
4831 }
4832
4833 /* If something has already gone wrong, just pass that fact up the
4834 tree. */
4835 if (error_operand_p (arg1)
4836 || error_operand_p (arg2)
4837 || error_operand_p (arg3))
4838 return error_mark_node;
4839
4840 orig_arg2 = arg2;
4841 orig_arg3 = arg3;
4842
4843 if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
4844 {
4845 tree arg1_type = TREE_TYPE (arg1);
4846
4847 /* If arg1 is another cond_expr choosing between -1 and 0,
4848 then we can use its comparison. It may help to avoid
4849 additional comparison, produce more accurate diagnostics
4850 and enables folding. */
4851 if (TREE_CODE (arg1) == VEC_COND_EXPR
4852 && integer_minus_onep (TREE_OPERAND (arg1, 1))
4853 && integer_zerop (TREE_OPERAND (arg1, 2)))
4854 arg1 = TREE_OPERAND (arg1, 0);
4855
4856 arg1 = force_rvalue (arg1, complain);
4857 arg2 = force_rvalue (arg2, complain);
4858 arg3 = force_rvalue (arg3, complain);
4859
4860 /* force_rvalue can return error_mark on valid arguments. */
4861 if (error_operand_p (arg1)
4862 || error_operand_p (arg2)
4863 || error_operand_p (arg3))
4864 return error_mark_node;
4865
4866 arg2_type = TREE_TYPE (arg2);
4867 arg3_type = TREE_TYPE (arg3);
4868
4869 if (!VECTOR_TYPE_P (arg2_type)
4870 && !VECTOR_TYPE_P (arg3_type))
4871 {
4872 /* Rely on the error messages of the scalar version. */
4873 tree scal = build_conditional_expr_1 (loc, integer_one_node,
4874 orig_arg2, orig_arg3, complain);
4875 if (scal == error_mark_node)
4876 return error_mark_node;
4877 tree stype = TREE_TYPE (scal);
4878 tree ctype = TREE_TYPE (arg1_type);
4879 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
4880 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
4881 {
4882 if (complain & tf_error)
4883 error_at (loc, "inferred scalar type %qT is not an integer or "
4884 "floating point type of the same size as %qT", stype,
4885 COMPARISON_CLASS_P (arg1)
4886 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4887 : ctype);
4888 return error_mark_node;
4889 }
4890
4891 tree vtype = build_opaque_vector_type (stype,
4892 TYPE_VECTOR_SUBPARTS (arg1_type));
4893 /* We could pass complain & tf_warning to unsafe_conversion_p,
4894 but the warnings (like Wsign-conversion) have already been
4895 given by the scalar build_conditional_expr_1. We still check
4896 unsafe_conversion_p to forbid truncating long long -> float. */
4897 if (unsafe_conversion_p (loc, stype, arg2, NULL_TREE, false))
4898 {
4899 if (complain & tf_error)
4900 error_at (loc, "conversion of scalar %qH to vector %qI "
4901 "involves truncation", arg2_type, vtype);
4902 return error_mark_node;
4903 }
4904 if (unsafe_conversion_p (loc, stype, arg3, NULL_TREE, false))
4905 {
4906 if (complain & tf_error)
4907 error_at (loc, "conversion of scalar %qH to vector %qI "
4908 "involves truncation", arg3_type, vtype);
4909 return error_mark_node;
4910 }
4911
4912 arg2 = cp_convert (stype, arg2, complain);
4913 arg2 = save_expr (arg2);
4914 arg2 = build_vector_from_val (vtype, arg2);
4915 arg2_type = vtype;
4916 arg3 = cp_convert (stype, arg3, complain);
4917 arg3 = save_expr (arg3);
4918 arg3 = build_vector_from_val (vtype, arg3);
4919 arg3_type = vtype;
4920 }
4921
4922 if (VECTOR_TYPE_P (arg2_type) != VECTOR_TYPE_P (arg3_type))
4923 {
4924 enum stv_conv convert_flag =
4925 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
4926 complain & tf_error);
4927
4928 switch (convert_flag)
4929 {
4930 case stv_error:
4931 return error_mark_node;
4932 case stv_firstarg:
4933 {
4934 arg2 = save_expr (arg2);
4935 arg2 = convert (TREE_TYPE (arg3_type), arg2);
4936 arg2 = build_vector_from_val (arg3_type, arg2);
4937 arg2_type = TREE_TYPE (arg2);
4938 break;
4939 }
4940 case stv_secondarg:
4941 {
4942 arg3 = save_expr (arg3);
4943 arg3 = convert (TREE_TYPE (arg2_type), arg3);
4944 arg3 = build_vector_from_val (arg2_type, arg3);
4945 arg3_type = TREE_TYPE (arg3);
4946 break;
4947 }
4948 default:
4949 break;
4950 }
4951 }
4952
4953 if (!same_type_p (arg2_type, arg3_type)
4954 || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type),
4955 TYPE_VECTOR_SUBPARTS (arg2_type))
4956 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
4957 {
4958 if (complain & tf_error)
4959 error_at (loc,
4960 "incompatible vector types in conditional expression: "
4961 "%qT, %qT and %qT", TREE_TYPE (arg1),
4962 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
4963 return error_mark_node;
4964 }
4965
4966 if (!COMPARISON_CLASS_P (arg1))
4967 {
4968 tree cmp_type = build_same_sized_truth_vector_type (arg1_type);
4969 arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
4970 }
4971 return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
4972 }
4973
4974 /* [expr.cond]
4975
4976 The first expression is implicitly converted to bool (clause
4977 _conv_). */
4978 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
4979 LOOKUP_NORMAL);
4980 if (error_operand_p (arg1))
4981 return error_mark_node;
4982
4983 /* [expr.cond]
4984
4985 If either the second or the third operand has type (possibly
4986 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
4987 array-to-pointer (_conv.array_), and function-to-pointer
4988 (_conv.func_) standard conversions are performed on the second
4989 and third operands. */
4990 arg2_type = unlowered_expr_type (arg2);
4991 arg3_type = unlowered_expr_type (arg3);
4992 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
4993 {
4994 /* Do the conversions. We don't these for `void' type arguments
4995 since it can't have any effect and since decay_conversion
4996 does not handle that case gracefully. */
4997 if (!VOID_TYPE_P (arg2_type))
4998 arg2 = decay_conversion (arg2, complain);
4999 if (!VOID_TYPE_P (arg3_type))
5000 arg3 = decay_conversion (arg3, complain);
5001 arg2_type = TREE_TYPE (arg2);
5002 arg3_type = TREE_TYPE (arg3);
5003
5004 /* [expr.cond]
5005
5006 One of the following shall hold:
5007
5008 --The second or the third operand (but not both) is a
5009 throw-expression (_except.throw_); the result is of the
5010 type of the other and is an rvalue.
5011
5012 --Both the second and the third operands have type void; the
5013 result is of type void and is an rvalue.
5014
5015 We must avoid calling force_rvalue for expressions of type
5016 "void" because it will complain that their value is being
5017 used. */
5018 if (TREE_CODE (arg2) == THROW_EXPR
5019 && TREE_CODE (arg3) != THROW_EXPR)
5020 {
5021 if (!VOID_TYPE_P (arg3_type))
5022 {
5023 arg3 = force_rvalue (arg3, complain);
5024 if (arg3 == error_mark_node)
5025 return error_mark_node;
5026 }
5027 arg3_type = TREE_TYPE (arg3);
5028 result_type = arg3_type;
5029 }
5030 else if (TREE_CODE (arg2) != THROW_EXPR
5031 && TREE_CODE (arg3) == THROW_EXPR)
5032 {
5033 if (!VOID_TYPE_P (arg2_type))
5034 {
5035 arg2 = force_rvalue (arg2, complain);
5036 if (arg2 == error_mark_node)
5037 return error_mark_node;
5038 }
5039 arg2_type = TREE_TYPE (arg2);
5040 result_type = arg2_type;
5041 }
5042 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
5043 result_type = void_type_node;
5044 else
5045 {
5046 if (complain & tf_error)
5047 {
5048 if (VOID_TYPE_P (arg2_type))
5049 error_at (EXPR_LOC_OR_LOC (arg3, loc),
5050 "second operand to the conditional operator "
5051 "is of type %<void%>, but the third operand is "
5052 "neither a throw-expression nor of type %<void%>");
5053 else
5054 error_at (EXPR_LOC_OR_LOC (arg2, loc),
5055 "third operand to the conditional operator "
5056 "is of type %<void%>, but the second operand is "
5057 "neither a throw-expression nor of type %<void%>");
5058 }
5059 return error_mark_node;
5060 }
5061
5062 is_glvalue = false;
5063 goto valid_operands;
5064 }
5065 /* [expr.cond]
5066
5067 Otherwise, if the second and third operand have different types,
5068 and either has (possibly cv-qualified) class type, or if both are
5069 glvalues of the same value category and the same type except for
5070 cv-qualification, an attempt is made to convert each of those operands
5071 to the type of the other. */
5072 else if (!same_type_p (arg2_type, arg3_type)
5073 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
5074 || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
5075 arg3_type)
5076 && glvalue_p (arg2) && glvalue_p (arg3)
5077 && lvalue_p (arg2) == lvalue_p (arg3))))
5078 {
5079 conversion *conv2;
5080 conversion *conv3;
5081 bool converted = false;
5082
5083 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5084 p = conversion_obstack_alloc (0);
5085
5086 conv2 = conditional_conversion (arg2, arg3, complain);
5087 conv3 = conditional_conversion (arg3, arg2, complain);
5088
5089 /* [expr.cond]
5090
5091 If both can be converted, or one can be converted but the
5092 conversion is ambiguous, the program is ill-formed. If
5093 neither can be converted, the operands are left unchanged and
5094 further checking is performed as described below. If exactly
5095 one conversion is possible, that conversion is applied to the
5096 chosen operand and the converted operand is used in place of
5097 the original operand for the remainder of this section. */
5098 if ((conv2 && !conv2->bad_p
5099 && conv3 && !conv3->bad_p)
5100 || (conv2 && conv2->kind == ck_ambig)
5101 || (conv3 && conv3->kind == ck_ambig))
5102 {
5103 if (complain & tf_error)
5104 {
5105 error_at (loc, "operands to ?: have different types %qT and %qT",
5106 arg2_type, arg3_type);
5107 if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
5108 inform (loc, " and each type can be converted to the other");
5109 else if (conv2 && conv2->kind == ck_ambig)
5110 convert_like (conv2, arg2, complain);
5111 else
5112 convert_like (conv3, arg3, complain);
5113 }
5114 result = error_mark_node;
5115 }
5116 else if (conv2 && !conv2->bad_p)
5117 {
5118 arg2 = convert_like (conv2, arg2, complain);
5119 arg2 = convert_from_reference (arg2);
5120 arg2_type = TREE_TYPE (arg2);
5121 /* Even if CONV2 is a valid conversion, the result of the
5122 conversion may be invalid. For example, if ARG3 has type
5123 "volatile X", and X does not have a copy constructor
5124 accepting a "volatile X&", then even if ARG2 can be
5125 converted to X, the conversion will fail. */
5126 if (error_operand_p (arg2))
5127 result = error_mark_node;
5128 converted = true;
5129 }
5130 else if (conv3 && !conv3->bad_p)
5131 {
5132 arg3 = convert_like (conv3, arg3, complain);
5133 arg3 = convert_from_reference (arg3);
5134 arg3_type = TREE_TYPE (arg3);
5135 if (error_operand_p (arg3))
5136 result = error_mark_node;
5137 converted = true;
5138 }
5139
5140 /* Free all the conversions we allocated. */
5141 obstack_free (&conversion_obstack, p);
5142
5143 if (result)
5144 return result;
5145
5146 /* If, after the conversion, both operands have class type,
5147 treat the cv-qualification of both operands as if it were the
5148 union of the cv-qualification of the operands.
5149
5150 The standard is not clear about what to do in this
5151 circumstance. For example, if the first operand has type
5152 "const X" and the second operand has a user-defined
5153 conversion to "volatile X", what is the type of the second
5154 operand after this step? Making it be "const X" (matching
5155 the first operand) seems wrong, as that discards the
5156 qualification without actually performing a copy. Leaving it
5157 as "volatile X" seems wrong as that will result in the
5158 conditional expression failing altogether, even though,
5159 according to this step, the one operand could be converted to
5160 the type of the other. */
5161 if (converted
5162 && CLASS_TYPE_P (arg2_type)
5163 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
5164 arg2_type = arg3_type =
5165 cp_build_qualified_type (arg2_type,
5166 cp_type_quals (arg2_type)
5167 | cp_type_quals (arg3_type));
5168 }
5169
5170 /* [expr.cond]
5171
5172 If the second and third operands are glvalues of the same value
5173 category and have the same type, the result is of that type and
5174 value category. */
5175 if (((lvalue_p (arg2) && lvalue_p (arg3))
5176 || (xvalue_p (arg2) && xvalue_p (arg3)))
5177 && same_type_p (arg2_type, arg3_type))
5178 {
5179 result_type = arg2_type;
5180 if (processing_template_decl)
5181 /* Let lvalue_kind know this was a glvalue. */
5182 result_type = cp_build_reference_type (result_type, xvalue_p (arg2));
5183
5184 arg2 = mark_lvalue_use (arg2);
5185 arg3 = mark_lvalue_use (arg3);
5186 goto valid_operands;
5187 }
5188
5189 /* [expr.cond]
5190
5191 Otherwise, the result is an rvalue. If the second and third
5192 operand do not have the same type, and either has (possibly
5193 cv-qualified) class type, overload resolution is used to
5194 determine the conversions (if any) to be applied to the operands
5195 (_over.match.oper_, _over.built_). */
5196 is_glvalue = false;
5197 if (!same_type_p (arg2_type, arg3_type)
5198 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
5199 {
5200 tree args[3];
5201 conversion *conv;
5202 bool any_viable_p;
5203
5204 /* Rearrange the arguments so that add_builtin_candidate only has
5205 to know about two args. In build_builtin_candidate, the
5206 arguments are unscrambled. */
5207 args[0] = arg2;
5208 args[1] = arg3;
5209 args[2] = arg1;
5210 add_builtin_candidates (&candidates,
5211 COND_EXPR,
5212 NOP_EXPR,
5213 ovl_op_identifier (false, COND_EXPR),
5214 args,
5215 LOOKUP_NORMAL, complain);
5216
5217 /* [expr.cond]
5218
5219 If the overload resolution fails, the program is
5220 ill-formed. */
5221 candidates = splice_viable (candidates, false, &any_viable_p);
5222 if (!any_viable_p)
5223 {
5224 if (complain & tf_error)
5225 error_at (loc, "operands to ?: have different types %qT and %qT",
5226 arg2_type, arg3_type);
5227 return error_mark_node;
5228 }
5229 cand = tourney (candidates, complain);
5230 if (!cand)
5231 {
5232 if (complain & tf_error)
5233 {
5234 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
5235 print_z_candidates (loc, candidates);
5236 }
5237 return error_mark_node;
5238 }
5239
5240 /* [expr.cond]
5241
5242 Otherwise, the conversions thus determined are applied, and
5243 the converted operands are used in place of the original
5244 operands for the remainder of this section. */
5245 conv = cand->convs[0];
5246 arg1 = convert_like (conv, arg1, complain);
5247 conv = cand->convs[1];
5248 arg2 = convert_like (conv, arg2, complain);
5249 arg2_type = TREE_TYPE (arg2);
5250 conv = cand->convs[2];
5251 arg3 = convert_like (conv, arg3, complain);
5252 arg3_type = TREE_TYPE (arg3);
5253 }
5254
5255 /* [expr.cond]
5256
5257 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
5258 and function-to-pointer (_conv.func_) standard conversions are
5259 performed on the second and third operands.
5260
5261 We need to force the lvalue-to-rvalue conversion here for class types,
5262 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
5263 that isn't wrapped with a TARGET_EXPR plays havoc with exception
5264 regions. */
5265
5266 arg2 = force_rvalue (arg2, complain);
5267 if (!CLASS_TYPE_P (arg2_type))
5268 arg2_type = TREE_TYPE (arg2);
5269
5270 arg3 = force_rvalue (arg3, complain);
5271 if (!CLASS_TYPE_P (arg3_type))
5272 arg3_type = TREE_TYPE (arg3);
5273
5274 if (arg2 == error_mark_node || arg3 == error_mark_node)
5275 return error_mark_node;
5276
5277 /* [expr.cond]
5278
5279 After those conversions, one of the following shall hold:
5280
5281 --The second and third operands have the same type; the result is of
5282 that type. */
5283 if (same_type_p (arg2_type, arg3_type))
5284 result_type = arg2_type;
5285 /* [expr.cond]
5286
5287 --The second and third operands have arithmetic or enumeration
5288 type; the usual arithmetic conversions are performed to bring
5289 them to a common type, and the result is of that type. */
5290 else if ((ARITHMETIC_TYPE_P (arg2_type)
5291 || UNSCOPED_ENUM_P (arg2_type))
5292 && (ARITHMETIC_TYPE_P (arg3_type)
5293 || UNSCOPED_ENUM_P (arg3_type)))
5294 {
5295 /* In this case, there is always a common type. */
5296 result_type = type_after_usual_arithmetic_conversions (arg2_type,
5297 arg3_type);
5298 if (complain & tf_warning)
5299 do_warn_double_promotion (result_type, arg2_type, arg3_type,
5300 "implicit conversion from %qH to %qI to "
5301 "match other result of conditional",
5302 loc);
5303
5304 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
5305 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
5306 {
5307 if (TREE_CODE (orig_arg2) == CONST_DECL
5308 && TREE_CODE (orig_arg3) == CONST_DECL
5309 && DECL_CONTEXT (orig_arg2) == DECL_CONTEXT (orig_arg3))
5310 /* Two enumerators from the same enumeration can have different
5311 types when the enumeration is still being defined. */;
5312 else if (complain & tf_warning)
5313 warning_at (loc, OPT_Wenum_compare, "enumeral mismatch in "
5314 "conditional expression: %qT vs %qT",
5315 arg2_type, arg3_type);
5316 }
5317 else if (extra_warnings
5318 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
5319 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
5320 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
5321 && !same_type_p (arg2_type,
5322 type_promotes_to (arg3_type)))))
5323 {
5324 if (complain & tf_warning)
5325 warning_at (loc, OPT_Wextra, "enumeral and non-enumeral type in "
5326 "conditional expression");
5327 }
5328
5329 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5330 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5331 }
5332 /* [expr.cond]
5333
5334 --The second and third operands have pointer type, or one has
5335 pointer type and the other is a null pointer constant; pointer
5336 conversions (_conv.ptr_) and qualification conversions
5337 (_conv.qual_) are performed to bring them to their composite
5338 pointer type (_expr.rel_). The result is of the composite
5339 pointer type.
5340
5341 --The second and third operands have pointer to member type, or
5342 one has pointer to member type and the other is a null pointer
5343 constant; pointer to member conversions (_conv.mem_) and
5344 qualification conversions (_conv.qual_) are performed to bring
5345 them to a common type, whose cv-qualification shall match the
5346 cv-qualification of either the second or the third operand.
5347 The result is of the common type. */
5348 else if ((null_ptr_cst_p (arg2)
5349 && TYPE_PTR_OR_PTRMEM_P (arg3_type))
5350 || (null_ptr_cst_p (arg3)
5351 && TYPE_PTR_OR_PTRMEM_P (arg2_type))
5352 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
5353 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
5354 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
5355 {
5356 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
5357 arg3, CPO_CONDITIONAL_EXPR,
5358 complain);
5359 if (result_type == error_mark_node)
5360 return error_mark_node;
5361 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5362 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5363 }
5364
5365 if (!result_type)
5366 {
5367 if (complain & tf_error)
5368 error_at (loc, "operands to ?: have different types %qT and %qT",
5369 arg2_type, arg3_type);
5370 return error_mark_node;
5371 }
5372
5373 if (arg2 == error_mark_node || arg3 == error_mark_node)
5374 return error_mark_node;
5375
5376 valid_operands:
5377 result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
5378
5379 /* If the ARG2 and ARG3 are the same and don't have side-effects,
5380 warn here, because the COND_EXPR will be turned into ARG2. */
5381 if (warn_duplicated_branches
5382 && (complain & tf_warning)
5383 && (arg2 == arg3 || operand_equal_p (arg2, arg3, 0)))
5384 warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
5385 "this condition has identical branches");
5386
5387 /* We can't use result_type below, as fold might have returned a
5388 throw_expr. */
5389
5390 if (!is_glvalue)
5391 {
5392 /* Expand both sides into the same slot, hopefully the target of
5393 the ?: expression. We used to check for TARGET_EXPRs here,
5394 but now we sometimes wrap them in NOP_EXPRs so the test would
5395 fail. */
5396 if (CLASS_TYPE_P (TREE_TYPE (result)))
5397 result = get_target_expr_sfinae (result, complain);
5398 /* If this expression is an rvalue, but might be mistaken for an
5399 lvalue, we must add a NON_LVALUE_EXPR. */
5400 result = rvalue (result);
5401 }
5402 else
5403 result = force_paren_expr (result);
5404
5405 return result;
5406 }
5407
5408 /* Wrapper for above. */
5409
5410 tree
build_conditional_expr(location_t loc,tree arg1,tree arg2,tree arg3,tsubst_flags_t complain)5411 build_conditional_expr (location_t loc, tree arg1, tree arg2, tree arg3,
5412 tsubst_flags_t complain)
5413 {
5414 tree ret;
5415 bool subtime = timevar_cond_start (TV_OVERLOAD);
5416 ret = build_conditional_expr_1 (loc, arg1, arg2, arg3, complain);
5417 timevar_cond_stop (TV_OVERLOAD, subtime);
5418 return ret;
5419 }
5420
5421 /* OPERAND is an operand to an expression. Perform necessary steps
5422 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
5423 returned. */
5424
5425 static tree
prep_operand(tree operand)5426 prep_operand (tree operand)
5427 {
5428 if (operand)
5429 {
5430 if (CLASS_TYPE_P (TREE_TYPE (operand))
5431 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
5432 /* Make sure the template type is instantiated now. */
5433 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
5434 }
5435
5436 return operand;
5437 }
5438
5439 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5440 OVERLOAD) to the CANDIDATES, returning an updated list of
5441 CANDIDATES. The ARGS are the arguments provided to the call;
5442 if FIRST_ARG is non-null it is the implicit object argument,
5443 otherwise the first element of ARGS is used if needed. The
5444 EXPLICIT_TARGS are explicit template arguments provided.
5445 TEMPLATE_ONLY is true if only template functions should be
5446 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5447 add_function_candidate. */
5448
5449 static void
add_candidates(tree fns,tree first_arg,const vec<tree,va_gc> * args,tree return_type,tree explicit_targs,bool template_only,tree conversion_path,tree access_path,int flags,struct z_candidate ** candidates,tsubst_flags_t complain)5450 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
5451 tree return_type,
5452 tree explicit_targs, bool template_only,
5453 tree conversion_path, tree access_path,
5454 int flags,
5455 struct z_candidate **candidates,
5456 tsubst_flags_t complain)
5457 {
5458 tree ctype;
5459 const vec<tree, va_gc> *non_static_args;
5460 bool check_list_ctor = false;
5461 bool check_converting = false;
5462 unification_kind_t strict;
5463
5464 if (!fns)
5465 return;
5466
5467 /* Precalculate special handling of constructors and conversion ops. */
5468 tree fn = OVL_FIRST (fns);
5469 if (DECL_CONV_FN_P (fn))
5470 {
5471 check_list_ctor = false;
5472 check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
5473 if (flags & LOOKUP_NO_CONVERSION)
5474 /* We're doing return_type(x). */
5475 strict = DEDUCE_CONV;
5476 else
5477 /* We're doing x.operator return_type(). */
5478 strict = DEDUCE_EXACT;
5479 /* [over.match.funcs] For conversion functions, the function
5480 is considered to be a member of the class of the implicit
5481 object argument for the purpose of defining the type of
5482 the implicit object parameter. */
5483 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
5484 }
5485 else
5486 {
5487 if (DECL_CONSTRUCTOR_P (fn))
5488 {
5489 check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
5490 /* For list-initialization we consider explicit constructors
5491 and complain if one is chosen. */
5492 check_converting
5493 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
5494 == LOOKUP_ONLYCONVERTING);
5495 }
5496 strict = DEDUCE_CALL;
5497 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
5498 }
5499
5500 if (first_arg)
5501 non_static_args = args;
5502 else
5503 /* Delay creating the implicit this parameter until it is needed. */
5504 non_static_args = NULL;
5505
5506 for (lkp_iterator iter (fns); iter; ++iter)
5507 {
5508 fn = *iter;
5509
5510 if (check_converting && DECL_NONCONVERTING_P (fn))
5511 continue;
5512 if (check_list_ctor && !is_list_ctor (fn))
5513 continue;
5514
5515 tree fn_first_arg = NULL_TREE;
5516 const vec<tree, va_gc> *fn_args = args;
5517
5518 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
5519 {
5520 /* Figure out where the object arg comes from. If this
5521 function is a non-static member and we didn't get an
5522 implicit object argument, move it out of args. */
5523 if (first_arg == NULL_TREE)
5524 {
5525 unsigned int ix;
5526 tree arg;
5527 vec<tree, va_gc> *tempvec;
5528 vec_alloc (tempvec, args->length () - 1);
5529 for (ix = 1; args->iterate (ix, &arg); ++ix)
5530 tempvec->quick_push (arg);
5531 non_static_args = tempvec;
5532 first_arg = (*args)[0];
5533 }
5534
5535 fn_first_arg = first_arg;
5536 fn_args = non_static_args;
5537 }
5538
5539 if (TREE_CODE (fn) == TEMPLATE_DECL)
5540 add_template_candidate (candidates,
5541 fn,
5542 ctype,
5543 explicit_targs,
5544 fn_first_arg,
5545 fn_args,
5546 return_type,
5547 access_path,
5548 conversion_path,
5549 flags,
5550 strict,
5551 complain);
5552 else if (!template_only)
5553 add_function_candidate (candidates,
5554 fn,
5555 ctype,
5556 fn_first_arg,
5557 fn_args,
5558 access_path,
5559 conversion_path,
5560 flags,
5561 complain);
5562 }
5563 }
5564
5565 /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
5566 -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
5567
5568 static int
op_is_ordered(tree_code code)5569 op_is_ordered (tree_code code)
5570 {
5571 switch (code)
5572 {
5573 // 5. b @= a
5574 case MODIFY_EXPR:
5575 return (flag_strong_eval_order > 1 ? -1 : 0);
5576
5577 // 6. a[b]
5578 case ARRAY_REF:
5579 return (flag_strong_eval_order > 1 ? 1 : 0);
5580
5581 // 1. a.b
5582 // Not overloadable (yet).
5583 // 2. a->b
5584 // Only one argument.
5585 // 3. a->*b
5586 case MEMBER_REF:
5587 // 7. a << b
5588 case LSHIFT_EXPR:
5589 // 8. a >> b
5590 case RSHIFT_EXPR:
5591 // a && b
5592 // Predates P0145R3.
5593 case TRUTH_ANDIF_EXPR:
5594 // a || b
5595 // Predates P0145R3.
5596 case TRUTH_ORIF_EXPR:
5597 // a , b
5598 // Predates P0145R3.
5599 case COMPOUND_EXPR:
5600 return (flag_strong_eval_order ? 1 : 0);
5601
5602 default:
5603 return 0;
5604 }
5605 }
5606
5607 static tree
build_new_op_1(location_t loc,enum tree_code code,int flags,tree arg1,tree arg2,tree arg3,tree * overload,tsubst_flags_t complain)5608 build_new_op_1 (location_t loc, enum tree_code code, int flags, tree arg1,
5609 tree arg2, tree arg3, tree *overload, tsubst_flags_t complain)
5610 {
5611 struct z_candidate *candidates = 0, *cand;
5612 vec<tree, va_gc> *arglist;
5613 tree args[3];
5614 tree result = NULL_TREE;
5615 bool result_valid_p = false;
5616 enum tree_code code2 = NOP_EXPR;
5617 enum tree_code code_orig_arg1 = ERROR_MARK;
5618 enum tree_code code_orig_arg2 = ERROR_MARK;
5619 conversion *conv;
5620 void *p;
5621 bool strict_p;
5622 bool any_viable_p;
5623
5624 if (error_operand_p (arg1)
5625 || error_operand_p (arg2)
5626 || error_operand_p (arg3))
5627 return error_mark_node;
5628
5629 bool ismodop = code == MODIFY_EXPR;
5630 if (ismodop)
5631 {
5632 code2 = TREE_CODE (arg3);
5633 arg3 = NULL_TREE;
5634 }
5635 tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
5636
5637 arg1 = prep_operand (arg1);
5638
5639 bool memonly = false;
5640 switch (code)
5641 {
5642 case NEW_EXPR:
5643 case VEC_NEW_EXPR:
5644 case VEC_DELETE_EXPR:
5645 case DELETE_EXPR:
5646 /* Use build_op_new_call and build_op_delete_call instead. */
5647 gcc_unreachable ();
5648
5649 case CALL_EXPR:
5650 /* Use build_op_call instead. */
5651 gcc_unreachable ();
5652
5653 case TRUTH_ORIF_EXPR:
5654 case TRUTH_ANDIF_EXPR:
5655 case TRUTH_AND_EXPR:
5656 case TRUTH_OR_EXPR:
5657 /* These are saved for the sake of warn_logical_operator. */
5658 code_orig_arg1 = TREE_CODE (arg1);
5659 code_orig_arg2 = TREE_CODE (arg2);
5660 break;
5661 case GT_EXPR:
5662 case LT_EXPR:
5663 case GE_EXPR:
5664 case LE_EXPR:
5665 case EQ_EXPR:
5666 case NE_EXPR:
5667 /* These are saved for the sake of maybe_warn_bool_compare. */
5668 code_orig_arg1 = TREE_CODE (TREE_TYPE (arg1));
5669 code_orig_arg2 = TREE_CODE (TREE_TYPE (arg2));
5670 break;
5671
5672 /* =, ->, [], () must be non-static member functions. */
5673 case MODIFY_EXPR:
5674 if (code2 != NOP_EXPR)
5675 break;
5676 /* FALLTHRU */
5677 case COMPONENT_REF:
5678 case ARRAY_REF:
5679 memonly = true;
5680 break;
5681
5682 default:
5683 break;
5684 }
5685
5686 arg2 = prep_operand (arg2);
5687 arg3 = prep_operand (arg3);
5688
5689 if (code == COND_EXPR)
5690 /* Use build_conditional_expr instead. */
5691 gcc_unreachable ();
5692 else if (! OVERLOAD_TYPE_P (TREE_TYPE (arg1))
5693 && (! arg2 || ! OVERLOAD_TYPE_P (TREE_TYPE (arg2))))
5694 goto builtin;
5695
5696 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5697 arg2 = integer_zero_node;
5698
5699 vec_alloc (arglist, 3);
5700 arglist->quick_push (arg1);
5701 if (arg2 != NULL_TREE)
5702 arglist->quick_push (arg2);
5703 if (arg3 != NULL_TREE)
5704 arglist->quick_push (arg3);
5705
5706 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5707 p = conversion_obstack_alloc (0);
5708
5709 /* Add namespace-scope operators to the list of functions to
5710 consider. */
5711 if (!memonly)
5712 {
5713 tree fns = lookup_name_real (fnname, 0, 1, /*block_p=*/true, 0, 0);
5714 fns = lookup_arg_dependent (fnname, fns, arglist);
5715 add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
5716 NULL_TREE, false, NULL_TREE, NULL_TREE,
5717 flags, &candidates, complain);
5718 }
5719
5720 args[0] = arg1;
5721 args[1] = arg2;
5722 args[2] = NULL_TREE;
5723
5724 /* Add class-member operators to the candidate set. */
5725 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
5726 {
5727 tree fns;
5728
5729 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
5730 if (fns == error_mark_node)
5731 {
5732 result = error_mark_node;
5733 goto user_defined_result_ready;
5734 }
5735 if (fns)
5736 add_candidates (BASELINK_FUNCTIONS (fns),
5737 NULL_TREE, arglist, NULL_TREE,
5738 NULL_TREE, false,
5739 BASELINK_BINFO (fns),
5740 BASELINK_ACCESS_BINFO (fns),
5741 flags, &candidates, complain);
5742 }
5743 /* Per 13.3.1.2/3, 2nd bullet, if no operand has a class type, then
5744 only non-member functions that have type T1 or reference to
5745 cv-qualified-opt T1 for the first argument, if the first argument
5746 has an enumeration type, or T2 or reference to cv-qualified-opt
5747 T2 for the second argument, if the second argument has an
5748 enumeration type. Filter out those that don't match. */
5749 else if (! arg2 || ! CLASS_TYPE_P (TREE_TYPE (arg2)))
5750 {
5751 struct z_candidate **candp, **next;
5752
5753 for (candp = &candidates; *candp; candp = next)
5754 {
5755 tree parmlist, parmtype;
5756 int i, nargs = (arg2 ? 2 : 1);
5757
5758 cand = *candp;
5759 next = &cand->next;
5760
5761 parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5762
5763 for (i = 0; i < nargs; ++i)
5764 {
5765 parmtype = TREE_VALUE (parmlist);
5766
5767 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
5768 parmtype = TREE_TYPE (parmtype);
5769 if (TREE_CODE (TREE_TYPE (args[i])) == ENUMERAL_TYPE
5770 && (same_type_ignoring_top_level_qualifiers_p
5771 (TREE_TYPE (args[i]), parmtype)))
5772 break;
5773
5774 parmlist = TREE_CHAIN (parmlist);
5775 }
5776
5777 /* No argument has an appropriate type, so remove this
5778 candidate function from the list. */
5779 if (i == nargs)
5780 {
5781 *candp = cand->next;
5782 next = candp;
5783 }
5784 }
5785 }
5786
5787 add_builtin_candidates (&candidates, code, code2, fnname, args,
5788 flags, complain);
5789
5790 switch (code)
5791 {
5792 case COMPOUND_EXPR:
5793 case ADDR_EXPR:
5794 /* For these, the built-in candidates set is empty
5795 [over.match.oper]/3. We don't want non-strict matches
5796 because exact matches are always possible with built-in
5797 operators. The built-in candidate set for COMPONENT_REF
5798 would be empty too, but since there are no such built-in
5799 operators, we accept non-strict matches for them. */
5800 strict_p = true;
5801 break;
5802
5803 default:
5804 strict_p = false;
5805 break;
5806 }
5807
5808 candidates = splice_viable (candidates, strict_p, &any_viable_p);
5809 if (!any_viable_p)
5810 {
5811 switch (code)
5812 {
5813 case POSTINCREMENT_EXPR:
5814 case POSTDECREMENT_EXPR:
5815 /* Don't try anything fancy if we're not allowed to produce
5816 errors. */
5817 if (!(complain & tf_error))
5818 return error_mark_node;
5819
5820 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
5821 distinguish between prefix and postfix ++ and
5822 operator++() was used for both, so we allow this with
5823 -fpermissive. */
5824 else
5825 {
5826 const char *msg = (flag_permissive)
5827 ? G_("no %<%D(int)%> declared for postfix %qs,"
5828 " trying prefix operator instead")
5829 : G_("no %<%D(int)%> declared for postfix %qs");
5830 permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
5831 }
5832
5833 if (!flag_permissive)
5834 return error_mark_node;
5835
5836 if (code == POSTINCREMENT_EXPR)
5837 code = PREINCREMENT_EXPR;
5838 else
5839 code = PREDECREMENT_EXPR;
5840 result = build_new_op_1 (loc, code, flags, arg1, NULL_TREE,
5841 NULL_TREE, overload, complain);
5842 break;
5843
5844 /* The caller will deal with these. */
5845 case ADDR_EXPR:
5846 case COMPOUND_EXPR:
5847 case COMPONENT_REF:
5848 result = NULL_TREE;
5849 result_valid_p = true;
5850 break;
5851
5852 default:
5853 if (complain & tf_error)
5854 {
5855 /* If one of the arguments of the operator represents
5856 an invalid use of member function pointer, try to report
5857 a meaningful error ... */
5858 if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
5859 || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
5860 || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
5861 /* We displayed the error message. */;
5862 else
5863 {
5864 /* ... Otherwise, report the more generic
5865 "no matching operator found" error */
5866 op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
5867 print_z_candidates (loc, candidates);
5868 }
5869 }
5870 result = error_mark_node;
5871 break;
5872 }
5873 }
5874 else
5875 {
5876 cand = tourney (candidates, complain);
5877 if (cand == 0)
5878 {
5879 if (complain & tf_error)
5880 {
5881 op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
5882 print_z_candidates (loc, candidates);
5883 }
5884 result = error_mark_node;
5885 }
5886 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5887 {
5888 if (overload)
5889 *overload = cand->fn;
5890
5891 if (resolve_args (arglist, complain) == NULL)
5892 result = error_mark_node;
5893 else
5894 result = build_over_call (cand, LOOKUP_NORMAL, complain);
5895
5896 if (trivial_fn_p (cand->fn))
5897 /* There won't be a CALL_EXPR. */;
5898 else if (result && result != error_mark_node)
5899 {
5900 tree call = extract_call_expr (result);
5901 CALL_EXPR_OPERATOR_SYNTAX (call) = true;
5902
5903 if (processing_template_decl && DECL_HIDDEN_FRIEND_P (cand->fn))
5904 /* This prevents build_new_function_call from discarding this
5905 function during instantiation of the enclosing template. */
5906 KOENIG_LOOKUP_P (call) = 1;
5907
5908 /* Specify evaluation order as per P0145R2. */
5909 CALL_EXPR_ORDERED_ARGS (call) = false;
5910 switch (op_is_ordered (code))
5911 {
5912 case -1:
5913 CALL_EXPR_REVERSE_ARGS (call) = true;
5914 break;
5915
5916 case 1:
5917 CALL_EXPR_ORDERED_ARGS (call) = true;
5918 break;
5919
5920 default:
5921 break;
5922 }
5923 }
5924 }
5925 else
5926 {
5927 /* Give any warnings we noticed during overload resolution. */
5928 if (cand->warnings && (complain & tf_warning))
5929 {
5930 struct candidate_warning *w;
5931 for (w = cand->warnings; w; w = w->next)
5932 joust (cand, w->loser, 1, complain);
5933 }
5934
5935 /* Check for comparison of different enum types. */
5936 switch (code)
5937 {
5938 case GT_EXPR:
5939 case LT_EXPR:
5940 case GE_EXPR:
5941 case LE_EXPR:
5942 case EQ_EXPR:
5943 case NE_EXPR:
5944 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
5945 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
5946 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5947 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
5948 && (complain & tf_warning))
5949 {
5950 warning (OPT_Wenum_compare,
5951 "comparison between %q#T and %q#T",
5952 TREE_TYPE (arg1), TREE_TYPE (arg2));
5953 }
5954 break;
5955 default:
5956 break;
5957 }
5958
5959 /* We need to strip any leading REF_BIND so that bitfields
5960 don't cause errors. This should not remove any important
5961 conversions, because builtins don't apply to class
5962 objects directly. */
5963 conv = cand->convs[0];
5964 if (conv->kind == ck_ref_bind)
5965 conv = next_conversion (conv);
5966 arg1 = convert_like (conv, arg1, complain);
5967
5968 if (arg2)
5969 {
5970 conv = cand->convs[1];
5971 if (conv->kind == ck_ref_bind)
5972 conv = next_conversion (conv);
5973 else
5974 arg2 = decay_conversion (arg2, complain);
5975
5976 /* We need to call warn_logical_operator before
5977 converting arg2 to a boolean_type, but after
5978 decaying an enumerator to its value. */
5979 if (complain & tf_warning)
5980 warn_logical_operator (loc, code, boolean_type_node,
5981 code_orig_arg1, arg1,
5982 code_orig_arg2, arg2);
5983
5984 arg2 = convert_like (conv, arg2, complain);
5985 }
5986 if (arg3)
5987 {
5988 conv = cand->convs[2];
5989 if (conv->kind == ck_ref_bind)
5990 conv = next_conversion (conv);
5991 arg3 = convert_like (conv, arg3, complain);
5992 }
5993
5994 }
5995 }
5996
5997 user_defined_result_ready:
5998
5999 /* Free all the conversions we allocated. */
6000 obstack_free (&conversion_obstack, p);
6001
6002 if (result || result_valid_p)
6003 return result;
6004
6005 builtin:
6006 switch (code)
6007 {
6008 case MODIFY_EXPR:
6009 return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
6010
6011 case INDIRECT_REF:
6012 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
6013
6014 case TRUTH_ANDIF_EXPR:
6015 case TRUTH_ORIF_EXPR:
6016 case TRUTH_AND_EXPR:
6017 case TRUTH_OR_EXPR:
6018 if (complain & tf_warning)
6019 warn_logical_operator (loc, code, boolean_type_node,
6020 code_orig_arg1, arg1,
6021 code_orig_arg2, arg2);
6022 /* Fall through. */
6023 case GT_EXPR:
6024 case LT_EXPR:
6025 case GE_EXPR:
6026 case LE_EXPR:
6027 case EQ_EXPR:
6028 case NE_EXPR:
6029 if ((complain & tf_warning)
6030 && ((code_orig_arg1 == BOOLEAN_TYPE)
6031 ^ (code_orig_arg2 == BOOLEAN_TYPE)))
6032 maybe_warn_bool_compare (loc, code, arg1, arg2);
6033 if (complain & tf_warning && warn_tautological_compare)
6034 warn_tautological_cmp (loc, code, arg1, arg2);
6035 /* Fall through. */
6036 case PLUS_EXPR:
6037 case MINUS_EXPR:
6038 case MULT_EXPR:
6039 case TRUNC_DIV_EXPR:
6040 case MAX_EXPR:
6041 case MIN_EXPR:
6042 case LSHIFT_EXPR:
6043 case RSHIFT_EXPR:
6044 case TRUNC_MOD_EXPR:
6045 case BIT_AND_EXPR:
6046 case BIT_IOR_EXPR:
6047 case BIT_XOR_EXPR:
6048 return cp_build_binary_op (loc, code, arg1, arg2, complain);
6049
6050 case UNARY_PLUS_EXPR:
6051 case NEGATE_EXPR:
6052 case BIT_NOT_EXPR:
6053 case TRUTH_NOT_EXPR:
6054 case PREINCREMENT_EXPR:
6055 case POSTINCREMENT_EXPR:
6056 case PREDECREMENT_EXPR:
6057 case POSTDECREMENT_EXPR:
6058 case REALPART_EXPR:
6059 case IMAGPART_EXPR:
6060 case ABS_EXPR:
6061 return cp_build_unary_op (code, arg1, candidates != 0, complain);
6062
6063 case ARRAY_REF:
6064 return cp_build_array_ref (input_location, arg1, arg2, complain);
6065
6066 case MEMBER_REF:
6067 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_ARROW_STAR,
6068 complain),
6069 arg2, complain);
6070
6071 /* The caller will deal with these. */
6072 case ADDR_EXPR:
6073 case COMPONENT_REF:
6074 case COMPOUND_EXPR:
6075 return NULL_TREE;
6076
6077 default:
6078 gcc_unreachable ();
6079 }
6080 return NULL_TREE;
6081 }
6082
6083 /* Wrapper for above. */
6084
6085 tree
build_new_op(location_t loc,enum tree_code code,int flags,tree arg1,tree arg2,tree arg3,tree * overload,tsubst_flags_t complain)6086 build_new_op (location_t loc, enum tree_code code, int flags,
6087 tree arg1, tree arg2, tree arg3,
6088 tree *overload, tsubst_flags_t complain)
6089 {
6090 tree ret;
6091 bool subtime = timevar_cond_start (TV_OVERLOAD);
6092 ret = build_new_op_1 (loc, code, flags, arg1, arg2, arg3,
6093 overload, complain);
6094 timevar_cond_stop (TV_OVERLOAD, subtime);
6095 return ret;
6096 }
6097
6098 /* CALL was returned by some call-building function; extract the actual
6099 CALL_EXPR from any bits that have been tacked on, e.g. by
6100 convert_from_reference. */
6101
6102 tree
extract_call_expr(tree call)6103 extract_call_expr (tree call)
6104 {
6105 while (TREE_CODE (call) == COMPOUND_EXPR)
6106 call = TREE_OPERAND (call, 1);
6107 if (REFERENCE_REF_P (call))
6108 call = TREE_OPERAND (call, 0);
6109 if (TREE_CODE (call) == TARGET_EXPR)
6110 call = TARGET_EXPR_INITIAL (call);
6111 gcc_assert (TREE_CODE (call) == CALL_EXPR
6112 || TREE_CODE (call) == AGGR_INIT_EXPR
6113 || call == error_mark_node);
6114 return call;
6115 }
6116
6117 /* Returns true if FN has two parameters, of which the second has type
6118 size_t. */
6119
6120 static bool
second_parm_is_size_t(tree fn)6121 second_parm_is_size_t (tree fn)
6122 {
6123 tree t = FUNCTION_ARG_CHAIN (fn);
6124 if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
6125 return false;
6126 t = TREE_CHAIN (t);
6127 if (t == void_list_node)
6128 return true;
6129 if (aligned_new_threshold && t
6130 && same_type_p (TREE_VALUE (t), align_type_node)
6131 && TREE_CHAIN (t) == void_list_node)
6132 return true;
6133 return false;
6134 }
6135
6136 /* True if T, an allocation function, has std::align_val_t as its second
6137 argument. */
6138
6139 bool
aligned_allocation_fn_p(tree t)6140 aligned_allocation_fn_p (tree t)
6141 {
6142 if (!aligned_new_threshold)
6143 return false;
6144
6145 tree a = FUNCTION_ARG_CHAIN (t);
6146 return (a && same_type_p (TREE_VALUE (a), align_type_node));
6147 }
6148
6149 /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
6150 function (3.7.4.2 [basic.stc.dynamic.deallocation]) with a parameter of
6151 std::align_val_t. */
6152
6153 static bool
aligned_deallocation_fn_p(tree t)6154 aligned_deallocation_fn_p (tree t)
6155 {
6156 if (!aligned_new_threshold)
6157 return false;
6158
6159 /* A template instance is never a usual deallocation function,
6160 regardless of its signature. */
6161 if (TREE_CODE (t) == TEMPLATE_DECL
6162 || primary_template_specialization_p (t))
6163 return false;
6164
6165 tree a = FUNCTION_ARG_CHAIN (t);
6166 if (same_type_p (TREE_VALUE (a), align_type_node)
6167 && TREE_CHAIN (a) == void_list_node)
6168 return true;
6169 if (!same_type_p (TREE_VALUE (a), size_type_node))
6170 return false;
6171 a = TREE_CHAIN (a);
6172 if (a && same_type_p (TREE_VALUE (a), align_type_node)
6173 && TREE_CHAIN (a) == void_list_node)
6174 return true;
6175 return false;
6176 }
6177
6178 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
6179 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
6180
6181 bool
usual_deallocation_fn_p(tree t)6182 usual_deallocation_fn_p (tree t)
6183 {
6184 /* A template instance is never a usual deallocation function,
6185 regardless of its signature. */
6186 if (TREE_CODE (t) == TEMPLATE_DECL
6187 || primary_template_specialization_p (t))
6188 return false;
6189
6190 /* If a class T has a member deallocation function named operator delete
6191 with exactly one parameter, then that function is a usual
6192 (non-placement) deallocation function. If class T does not declare
6193 such an operator delete but does declare a member deallocation
6194 function named operator delete with exactly two parameters, the second
6195 of which has type std::size_t (18.2), then this function is a usual
6196 deallocation function. */
6197 bool global = DECL_NAMESPACE_SCOPE_P (t);
6198 tree chain = FUNCTION_ARG_CHAIN (t);
6199 if (!chain)
6200 return false;
6201 if (chain == void_list_node
6202 || ((!global || flag_sized_deallocation)
6203 && second_parm_is_size_t (t)))
6204 return true;
6205 if (aligned_deallocation_fn_p (t))
6206 return true;
6207 return false;
6208 }
6209
6210 /* Build a call to operator delete. This has to be handled very specially,
6211 because the restrictions on what signatures match are different from all
6212 other call instances. For a normal delete, only a delete taking (void *)
6213 or (void *, size_t) is accepted. For a placement delete, only an exact
6214 match with the placement new is accepted.
6215
6216 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
6217 ADDR is the pointer to be deleted.
6218 SIZE is the size of the memory block to be deleted.
6219 GLOBAL_P is true if the delete-expression should not consider
6220 class-specific delete operators.
6221 PLACEMENT is the corresponding placement new call, or NULL_TREE.
6222
6223 If this call to "operator delete" is being generated as part to
6224 deallocate memory allocated via a new-expression (as per [expr.new]
6225 which requires that if the initialization throws an exception then
6226 we call a deallocation function), then ALLOC_FN is the allocation
6227 function. */
6228
6229 tree
build_op_delete_call(enum tree_code code,tree addr,tree size,bool global_p,tree placement,tree alloc_fn,tsubst_flags_t complain)6230 build_op_delete_call (enum tree_code code, tree addr, tree size,
6231 bool global_p, tree placement,
6232 tree alloc_fn, tsubst_flags_t complain)
6233 {
6234 tree fn = NULL_TREE;
6235 tree fns, fnname, type, t;
6236
6237 if (addr == error_mark_node)
6238 return error_mark_node;
6239
6240 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
6241
6242 fnname = ovl_op_identifier (false, code);
6243
6244 if (CLASS_TYPE_P (type)
6245 && COMPLETE_TYPE_P (complete_type (type))
6246 && !global_p)
6247 /* In [class.free]
6248
6249 If the result of the lookup is ambiguous or inaccessible, or if
6250 the lookup selects a placement deallocation function, the
6251 program is ill-formed.
6252
6253 Therefore, we ask lookup_fnfields to complain about ambiguity. */
6254 {
6255 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
6256 if (fns == error_mark_node)
6257 return error_mark_node;
6258 }
6259 else
6260 fns = NULL_TREE;
6261
6262 if (fns == NULL_TREE)
6263 fns = lookup_name_nonclass (fnname);
6264
6265 /* Strip const and volatile from addr. */
6266 addr = cp_convert (ptr_type_node, addr, complain);
6267
6268 if (placement)
6269 {
6270 /* "A declaration of a placement deallocation function matches the
6271 declaration of a placement allocation function if it has the same
6272 number of parameters and, after parameter transformations (8.3.5),
6273 all parameter types except the first are identical."
6274
6275 So we build up the function type we want and ask instantiate_type
6276 to get it for us. */
6277 t = FUNCTION_ARG_CHAIN (alloc_fn);
6278 t = tree_cons (NULL_TREE, ptr_type_node, t);
6279 t = build_function_type (void_type_node, t);
6280
6281 fn = instantiate_type (t, fns, tf_none);
6282 if (fn == error_mark_node)
6283 return NULL_TREE;
6284
6285 fn = MAYBE_BASELINK_FUNCTIONS (fn);
6286
6287 /* "If the lookup finds the two-parameter form of a usual deallocation
6288 function (3.7.4.2) and that function, considered as a placement
6289 deallocation function, would have been selected as a match for the
6290 allocation function, the program is ill-formed." */
6291 if (second_parm_is_size_t (fn))
6292 {
6293 const char *const msg1
6294 = G_("exception cleanup for this placement new selects "
6295 "non-placement operator delete");
6296 const char *const msg2
6297 = G_("%qD is a usual (non-placement) deallocation "
6298 "function in C++14 (or with -fsized-deallocation)");
6299
6300 /* But if the class has an operator delete (void *), then that is
6301 the usual deallocation function, so we shouldn't complain
6302 about using the operator delete (void *, size_t). */
6303 if (DECL_CLASS_SCOPE_P (fn))
6304 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns));
6305 iter; ++iter)
6306 {
6307 tree elt = *iter;
6308 if (usual_deallocation_fn_p (elt)
6309 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
6310 goto ok;
6311 }
6312 /* Before C++14 a two-parameter global deallocation function is
6313 always a placement deallocation function, but warn if
6314 -Wc++14-compat. */
6315 else if (!flag_sized_deallocation)
6316 {
6317 if ((complain & tf_warning)
6318 && warning (OPT_Wc__14_compat, msg1))
6319 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6320 goto ok;
6321 }
6322
6323 if (complain & tf_warning_or_error)
6324 {
6325 if (permerror (input_location, msg1))
6326 {
6327 /* Only mention C++14 for namespace-scope delete. */
6328 if (DECL_NAMESPACE_SCOPE_P (fn))
6329 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6330 else
6331 inform (DECL_SOURCE_LOCATION (fn),
6332 "%qD is a usual (non-placement) deallocation "
6333 "function", fn);
6334 }
6335 }
6336 else
6337 return error_mark_node;
6338 ok:;
6339 }
6340 }
6341 else
6342 /* "Any non-placement deallocation function matches a non-placement
6343 allocation function. If the lookup finds a single matching
6344 deallocation function, that function will be called; otherwise, no
6345 deallocation function will be called." */
6346 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
6347 {
6348 tree elt = *iter;
6349 if (usual_deallocation_fn_p (elt))
6350 {
6351 if (!fn)
6352 {
6353 fn = elt;
6354 continue;
6355 }
6356
6357 /* -- If the type has new-extended alignment, a function with a
6358 parameter of type std::align_val_t is preferred; otherwise a
6359 function without such a parameter is preferred. If exactly one
6360 preferred function is found, that function is selected and the
6361 selection process terminates. If more than one preferred
6362 function is found, all non-preferred functions are eliminated
6363 from further consideration. */
6364 if (aligned_new_threshold)
6365 {
6366 bool want_align = type_has_new_extended_alignment (type);
6367 bool fn_align = aligned_deallocation_fn_p (fn);
6368 bool elt_align = aligned_deallocation_fn_p (elt);
6369
6370 if (elt_align != fn_align)
6371 {
6372 if (want_align == elt_align)
6373 fn = elt;
6374 continue;
6375 }
6376 }
6377
6378 /* -- If the deallocation functions have class scope, the one
6379 without a parameter of type std::size_t is selected. */
6380 bool want_size;
6381 if (DECL_CLASS_SCOPE_P (fn))
6382 want_size = false;
6383
6384 /* -- If the type is complete and if, for the second alternative
6385 (delete array) only, the operand is a pointer to a class type
6386 with a non-trivial destructor or a (possibly multi-dimensional)
6387 array thereof, the function with a parameter of type std::size_t
6388 is selected.
6389
6390 -- Otherwise, it is unspecified whether a deallocation function
6391 with a parameter of type std::size_t is selected. */
6392 else
6393 {
6394 want_size = COMPLETE_TYPE_P (type);
6395 if (code == VEC_DELETE_EXPR
6396 && !TYPE_VEC_NEW_USES_COOKIE (type))
6397 /* We need a cookie to determine the array size. */
6398 want_size = false;
6399 }
6400 bool fn_size = second_parm_is_size_t (fn);
6401 bool elt_size = second_parm_is_size_t (elt);
6402 gcc_assert (fn_size != elt_size);
6403 if (want_size == elt_size)
6404 fn = elt;
6405 }
6406 }
6407
6408 /* If we have a matching function, call it. */
6409 if (fn)
6410 {
6411 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6412
6413 /* If the FN is a member function, make sure that it is
6414 accessible. */
6415 if (BASELINK_P (fns))
6416 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
6417 complain);
6418
6419 /* Core issue 901: It's ok to new a type with deleted delete. */
6420 if (DECL_DELETED_FN (fn) && alloc_fn)
6421 return NULL_TREE;
6422
6423 if (placement)
6424 {
6425 /* The placement args might not be suitable for overload
6426 resolution at this point, so build the call directly. */
6427 int nargs = call_expr_nargs (placement);
6428 tree *argarray = XALLOCAVEC (tree, nargs);
6429 int i;
6430 argarray[0] = addr;
6431 for (i = 1; i < nargs; i++)
6432 argarray[i] = CALL_EXPR_ARG (placement, i);
6433 if (!mark_used (fn, complain) && !(complain & tf_error))
6434 return error_mark_node;
6435 return build_cxx_call (fn, nargs, argarray, complain);
6436 }
6437 else
6438 {
6439 tree ret;
6440 vec<tree, va_gc> *args = make_tree_vector ();
6441 args->quick_push (addr);
6442 if (second_parm_is_size_t (fn))
6443 args->quick_push (size);
6444 if (aligned_deallocation_fn_p (fn))
6445 {
6446 tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
6447 args->quick_push (al);
6448 }
6449 ret = cp_build_function_call_vec (fn, &args, complain);
6450 release_tree_vector (args);
6451 return ret;
6452 }
6453 }
6454
6455 /* [expr.new]
6456
6457 If no unambiguous matching deallocation function can be found,
6458 propagating the exception does not cause the object's memory to
6459 be freed. */
6460 if (alloc_fn)
6461 {
6462 if ((complain & tf_warning)
6463 && !placement)
6464 warning (0, "no corresponding deallocation function for %qD",
6465 alloc_fn);
6466 return NULL_TREE;
6467 }
6468
6469 if (complain & tf_error)
6470 error ("no suitable %<operator %s%> for %qT",
6471 OVL_OP_INFO (false, code)->name, type);
6472 return error_mark_node;
6473 }
6474
6475 /* If the current scope isn't allowed to access DECL along
6476 BASETYPE_PATH, give an error. The most derived class in
6477 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
6478 the declaration to use in the error diagnostic. */
6479
6480 bool
enforce_access(tree basetype_path,tree decl,tree diag_decl,tsubst_flags_t complain,access_failure_info * afi)6481 enforce_access (tree basetype_path, tree decl, tree diag_decl,
6482 tsubst_flags_t complain, access_failure_info *afi)
6483 {
6484 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
6485
6486 if (flag_new_inheriting_ctors
6487 && DECL_INHERITED_CTOR (decl))
6488 {
6489 /* 7.3.3/18: The additional constructors are accessible if they would be
6490 accessible when used to construct an object of the corresponding base
6491 class. */
6492 decl = strip_inheriting_ctors (decl);
6493 basetype_path = lookup_base (basetype_path, DECL_CONTEXT (decl),
6494 ba_any, NULL, complain);
6495 }
6496
6497 if (!accessible_p (basetype_path, decl, true))
6498 {
6499 if (complain & tf_error)
6500 {
6501 if (flag_new_inheriting_ctors)
6502 diag_decl = strip_inheriting_ctors (diag_decl);
6503 if (TREE_PRIVATE (decl))
6504 {
6505 error ("%q#D is private within this context", diag_decl);
6506 inform (DECL_SOURCE_LOCATION (diag_decl),
6507 "declared private here");
6508 if (afi)
6509 afi->record_access_failure (basetype_path, diag_decl);
6510 }
6511 else if (TREE_PROTECTED (decl))
6512 {
6513 error ("%q#D is protected within this context", diag_decl);
6514 inform (DECL_SOURCE_LOCATION (diag_decl),
6515 "declared protected here");
6516 if (afi)
6517 afi->record_access_failure (basetype_path, diag_decl);
6518 }
6519 else
6520 {
6521 error ("%q#D is inaccessible within this context", diag_decl);
6522 inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
6523 if (afi)
6524 afi->record_access_failure (basetype_path, diag_decl);
6525 }
6526 }
6527 return false;
6528 }
6529
6530 return true;
6531 }
6532
6533 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
6534 bitwise or of LOOKUP_* values. If any errors are warnings are
6535 generated, set *DIAGNOSTIC_FN to "error" or "warning",
6536 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
6537 to NULL. */
6538
6539 static tree
build_temp(tree expr,tree type,int flags,diagnostic_t * diagnostic_kind,tsubst_flags_t complain)6540 build_temp (tree expr, tree type, int flags,
6541 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
6542 {
6543 int savew, savee;
6544 vec<tree, va_gc> *args;
6545
6546 *diagnostic_kind = DK_UNSPECIFIED;
6547
6548 /* If the source is a packed field, calling the copy constructor will require
6549 binding the field to the reference parameter to the copy constructor, and
6550 we'll end up with an infinite loop. If we can use a bitwise copy, then
6551 do that now. */
6552 if ((lvalue_kind (expr) & clk_packed)
6553 && CLASS_TYPE_P (TREE_TYPE (expr))
6554 && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
6555 return get_target_expr_sfinae (expr, complain);
6556
6557 savew = warningcount + werrorcount, savee = errorcount;
6558 args = make_tree_vector_single (expr);
6559 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6560 &args, type, flags, complain);
6561 release_tree_vector (args);
6562 if (warningcount + werrorcount > savew)
6563 *diagnostic_kind = DK_WARNING;
6564 else if (errorcount > savee)
6565 *diagnostic_kind = DK_ERROR;
6566 return expr;
6567 }
6568
6569 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
6570 EXPR is implicitly converted to type TOTYPE.
6571 FN and ARGNUM are used for diagnostics. */
6572
6573 static void
conversion_null_warnings(tree totype,tree expr,tree fn,int argnum)6574 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
6575 {
6576 /* Issue warnings about peculiar, but valid, uses of NULL. */
6577 if (null_node_p (expr) && TREE_CODE (totype) != BOOLEAN_TYPE
6578 && ARITHMETIC_TYPE_P (totype))
6579 {
6580 source_location loc =
6581 expansion_point_location_if_in_system_header (input_location);
6582
6583 if (fn)
6584 warning_at (loc, OPT_Wconversion_null,
6585 "passing NULL to non-pointer argument %P of %qD",
6586 argnum, fn);
6587 else
6588 warning_at (loc, OPT_Wconversion_null,
6589 "converting to non-pointer type %qT from NULL", totype);
6590 }
6591
6592 /* Issue warnings if "false" is converted to a NULL pointer */
6593 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
6594 && TYPE_PTR_P (totype))
6595 {
6596 if (fn)
6597 warning_at (input_location, OPT_Wconversion_null,
6598 "converting %<false%> to pointer type for argument %P "
6599 "of %qD", argnum, fn);
6600 else
6601 warning_at (input_location, OPT_Wconversion_null,
6602 "converting %<false%> to pointer type %qT", totype);
6603 }
6604 }
6605
6606 /* We gave a diagnostic during a conversion. If this was in the second
6607 standard conversion sequence of a user-defined conversion sequence, say
6608 which user-defined conversion. */
6609
6610 static void
maybe_print_user_conv_context(conversion * convs)6611 maybe_print_user_conv_context (conversion *convs)
6612 {
6613 if (convs->user_conv_p)
6614 for (conversion *t = convs; t; t = next_conversion (t))
6615 if (t->kind == ck_user)
6616 {
6617 print_z_candidate (0, " after user-defined conversion:",
6618 t->cand);
6619 break;
6620 }
6621 }
6622
6623 /* Locate the parameter with the given index within FNDECL.
6624 ARGNUM is zero based, -1 indicates the `this' argument of a method.
6625 Return the location of the FNDECL itself if there are problems. */
6626
6627 location_t
get_fndecl_argument_location(tree fndecl,int argnum)6628 get_fndecl_argument_location (tree fndecl, int argnum)
6629 {
6630 int i;
6631 tree param;
6632
6633 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6634 for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
6635 i < argnum && param;
6636 i++, param = TREE_CHAIN (param))
6637 ;
6638
6639 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6640 return the location of FNDECL. */
6641 if (param == NULL)
6642 return DECL_SOURCE_LOCATION (fndecl);
6643
6644 return DECL_SOURCE_LOCATION (param);
6645 }
6646
6647 /* Perform the conversions in CONVS on the expression EXPR. FN and
6648 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
6649 indicates the `this' argument of a method. INNER is nonzero when
6650 being called to continue a conversion chain. It is negative when a
6651 reference binding will be applied, positive otherwise. If
6652 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
6653 conversions will be emitted if appropriate. If C_CAST_P is true,
6654 this conversion is coming from a C-style cast; in that case,
6655 conversions to inaccessible bases are permitted. */
6656
6657 static tree
convert_like_real(conversion * convs,tree expr,tree fn,int argnum,bool issue_conversion_warnings,bool c_cast_p,tsubst_flags_t complain)6658 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
6659 bool issue_conversion_warnings,
6660 bool c_cast_p, tsubst_flags_t complain)
6661 {
6662 tree totype = convs->type;
6663 diagnostic_t diag_kind;
6664 int flags;
6665 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6666
6667 if (convs->bad_p && !(complain & tf_error))
6668 return error_mark_node;
6669
6670 if (convs->bad_p
6671 && convs->kind != ck_user
6672 && convs->kind != ck_list
6673 && convs->kind != ck_ambig
6674 && (convs->kind != ck_ref_bind
6675 || (convs->user_conv_p && next_conversion (convs)->bad_p))
6676 && (convs->kind != ck_rvalue
6677 || SCALAR_TYPE_P (totype))
6678 && convs->kind != ck_base)
6679 {
6680 bool complained = false;
6681 conversion *t = convs;
6682
6683 /* Give a helpful error if this is bad because of excess braces. */
6684 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6685 && SCALAR_TYPE_P (totype)
6686 && CONSTRUCTOR_NELTS (expr) > 0
6687 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
6688 {
6689 complained = permerror (loc, "too many braces around initializer "
6690 "for %qT", totype);
6691 while (BRACE_ENCLOSED_INITIALIZER_P (expr)
6692 && CONSTRUCTOR_NELTS (expr) == 1)
6693 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6694 }
6695
6696 /* Give a helpful error if this is bad because a conversion to bool
6697 from std::nullptr_t requires direct-initialization. */
6698 if (NULLPTR_TYPE_P (TREE_TYPE (expr))
6699 && TREE_CODE (totype) == BOOLEAN_TYPE)
6700 complained = permerror (loc, "converting to %qH from %qI requires "
6701 "direct-initialization",
6702 totype, TREE_TYPE (expr));
6703
6704 for (; t ; t = next_conversion (t))
6705 {
6706 if (t->kind == ck_user && t->cand->reason)
6707 {
6708 complained = permerror (loc, "invalid user-defined conversion "
6709 "from %qH to %qI", TREE_TYPE (expr),
6710 totype);
6711 if (complained)
6712 print_z_candidate (loc, "candidate is:", t->cand);
6713 expr = convert_like_real (t, expr, fn, argnum,
6714 /*issue_conversion_warnings=*/false,
6715 /*c_cast_p=*/false,
6716 complain);
6717 if (convs->kind == ck_ref_bind)
6718 expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
6719 LOOKUP_NORMAL, NULL_TREE,
6720 complain);
6721 else
6722 expr = cp_convert (totype, expr, complain);
6723 if (complained && fn)
6724 inform (DECL_SOURCE_LOCATION (fn),
6725 " initializing argument %P of %qD", argnum, fn);
6726 return expr;
6727 }
6728 else if (t->kind == ck_user || !t->bad_p)
6729 {
6730 expr = convert_like_real (t, expr, fn, argnum,
6731 /*issue_conversion_warnings=*/false,
6732 /*c_cast_p=*/false,
6733 complain);
6734 break;
6735 }
6736 else if (t->kind == ck_ambig)
6737 return convert_like_real (t, expr, fn, argnum,
6738 /*issue_conversion_warnings=*/false,
6739 /*c_cast_p=*/false,
6740 complain);
6741 else if (t->kind == ck_identity)
6742 break;
6743 }
6744 if (!complained)
6745 complained = permerror (loc, "invalid conversion from %qH to %qI",
6746 TREE_TYPE (expr), totype);
6747 if (complained && fn)
6748 inform (get_fndecl_argument_location (fn, argnum),
6749 " initializing argument %P of %qD", argnum, fn);
6750
6751 return cp_convert (totype, expr, complain);
6752 }
6753
6754 if (issue_conversion_warnings && (complain & tf_warning))
6755 conversion_null_warnings (totype, expr, fn, argnum);
6756
6757 switch (convs->kind)
6758 {
6759 case ck_user:
6760 {
6761 struct z_candidate *cand = convs->cand;
6762
6763 if (cand == NULL)
6764 /* We chose the surrogate function from add_conv_candidate, now we
6765 actually need to build the conversion. */
6766 cand = build_user_type_conversion_1 (totype, expr,
6767 LOOKUP_NO_CONVERSION, complain);
6768
6769 tree convfn = cand->fn;
6770
6771 /* When converting from an init list we consider explicit
6772 constructors, but actually trying to call one is an error. */
6773 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
6774 && BRACE_ENCLOSED_INITIALIZER_P (expr)
6775 /* Unless this is for direct-list-initialization. */
6776 && !CONSTRUCTOR_IS_DIRECT_INIT (expr)
6777 /* And in C++98 a default constructor can't be explicit. */
6778 && cxx_dialect >= cxx11)
6779 {
6780 if (!(complain & tf_error))
6781 return error_mark_node;
6782 location_t loc = location_of (expr);
6783 if (CONSTRUCTOR_NELTS (expr) == 0
6784 && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
6785 {
6786 if (pedwarn (loc, 0, "converting to %qT from initializer list "
6787 "would use explicit constructor %qD",
6788 totype, convfn))
6789 inform (loc, "in C++11 and above a default constructor "
6790 "can be explicit");
6791 }
6792 else
6793 error ("converting to %qT from initializer list would use "
6794 "explicit constructor %qD", totype, convfn);
6795 }
6796
6797 /* If we're initializing from {}, it's value-initialization. */
6798 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6799 && CONSTRUCTOR_NELTS (expr) == 0
6800 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)
6801 && !processing_template_decl)
6802 {
6803 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
6804 if (abstract_virtuals_error_sfinae (NULL_TREE, totype, complain))
6805 return error_mark_node;
6806 expr = build_value_init (totype, complain);
6807 expr = get_target_expr_sfinae (expr, complain);
6808 if (expr != error_mark_node)
6809 {
6810 TARGET_EXPR_LIST_INIT_P (expr) = true;
6811 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
6812 }
6813 return expr;
6814 }
6815
6816 /* We don't know here whether EXPR is being used as an lvalue or
6817 rvalue, but we know it's read. */
6818 mark_exp_read (expr);
6819
6820 /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
6821 any more UDCs. */
6822 expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
6823 complain);
6824
6825 /* If this is a constructor or a function returning an aggr type,
6826 we need to build up a TARGET_EXPR. */
6827 if (DECL_CONSTRUCTOR_P (convfn))
6828 {
6829 expr = build_cplus_new (totype, expr, complain);
6830
6831 /* Remember that this was list-initialization. */
6832 if (convs->check_narrowing && expr != error_mark_node)
6833 TARGET_EXPR_LIST_INIT_P (expr) = true;
6834 }
6835
6836 return expr;
6837 }
6838 case ck_identity:
6839 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
6840 {
6841 int nelts = CONSTRUCTOR_NELTS (expr);
6842 if (nelts == 0)
6843 expr = build_value_init (totype, complain);
6844 else if (nelts == 1)
6845 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6846 else
6847 gcc_unreachable ();
6848 }
6849 expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p,
6850 /*read_p=*/true, UNKNOWN_LOCATION,
6851 /*reject_builtin=*/true);
6852
6853 if (type_unknown_p (expr))
6854 expr = instantiate_type (totype, expr, complain);
6855 if (expr == null_node
6856 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
6857 /* If __null has been converted to an integer type, we do not want to
6858 continue to warn about uses of EXPR as an integer, rather than as a
6859 pointer. */
6860 expr = build_int_cst (totype, 0);
6861 return expr;
6862 case ck_ambig:
6863 /* We leave bad_p off ck_ambig because overload resolution considers
6864 it valid, it just fails when we try to perform it. So we need to
6865 check complain here, too. */
6866 if (complain & tf_error)
6867 {
6868 /* Call build_user_type_conversion again for the error. */
6869 int flags = (convs->need_temporary_p
6870 ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
6871 build_user_type_conversion (totype, convs->u.expr, flags, complain);
6872 gcc_assert (seen_error ());
6873 if (fn)
6874 inform (DECL_SOURCE_LOCATION (fn),
6875 " initializing argument %P of %qD", argnum, fn);
6876 }
6877 return error_mark_node;
6878
6879 case ck_list:
6880 {
6881 /* Conversion to std::initializer_list<T>. */
6882 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
6883 tree new_ctor = build_constructor (init_list_type_node, NULL);
6884 unsigned len = CONSTRUCTOR_NELTS (expr);
6885 tree array, val, field;
6886 vec<constructor_elt, va_gc> *vec = NULL;
6887 unsigned ix;
6888
6889 /* Convert all the elements. */
6890 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
6891 {
6892 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
6893 false, false, complain);
6894 if (sub == error_mark_node)
6895 return sub;
6896 if (!BRACE_ENCLOSED_INITIALIZER_P (val)
6897 && !check_narrowing (TREE_TYPE (sub), val, complain))
6898 return error_mark_node;
6899 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
6900 if (!TREE_CONSTANT (sub))
6901 TREE_CONSTANT (new_ctor) = false;
6902 }
6903 /* Build up the array. */
6904 elttype = cp_build_qualified_type
6905 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
6906 array = build_array_of_n_type (elttype, len);
6907 array = finish_compound_literal (array, new_ctor, complain, fcl_c99);
6908 /* Take the address explicitly rather than via decay_conversion
6909 to avoid the error about taking the address of a temporary. */
6910 array = cp_build_addr_expr (array, complain);
6911 array = cp_convert (build_pointer_type (elttype), array, complain);
6912 if (array == error_mark_node)
6913 return error_mark_node;
6914
6915 /* Build up the initializer_list object. Note: fail gracefully
6916 if the object cannot be completed because, for example, no
6917 definition is provided (c++/80956). */
6918 totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain);
6919 if (!totype)
6920 return error_mark_node;
6921 field = next_initializable_field (TYPE_FIELDS (totype));
6922 CONSTRUCTOR_APPEND_ELT (vec, field, array);
6923 field = next_initializable_field (DECL_CHAIN (field));
6924 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
6925 new_ctor = build_constructor (totype, vec);
6926 return get_target_expr_sfinae (new_ctor, complain);
6927 }
6928
6929 case ck_aggr:
6930 if (TREE_CODE (totype) == COMPLEX_TYPE)
6931 {
6932 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
6933 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
6934 real = perform_implicit_conversion (TREE_TYPE (totype),
6935 real, complain);
6936 imag = perform_implicit_conversion (TREE_TYPE (totype),
6937 imag, complain);
6938 expr = build2 (COMPLEX_EXPR, totype, real, imag);
6939 return expr;
6940 }
6941 expr = reshape_init (totype, expr, complain);
6942 expr = get_target_expr_sfinae (digest_init (totype, expr, complain),
6943 complain);
6944 if (expr != error_mark_node)
6945 TARGET_EXPR_LIST_INIT_P (expr) = true;
6946 return expr;
6947
6948 default:
6949 break;
6950 };
6951
6952 expr = convert_like_real (next_conversion (convs), expr, fn, argnum,
6953 convs->kind == ck_ref_bind
6954 ? issue_conversion_warnings : false,
6955 c_cast_p, complain);
6956 if (expr == error_mark_node)
6957 return error_mark_node;
6958
6959 switch (convs->kind)
6960 {
6961 case ck_rvalue:
6962 expr = decay_conversion (expr, complain);
6963 if (expr == error_mark_node)
6964 {
6965 if (complain & tf_error)
6966 {
6967 maybe_print_user_conv_context (convs);
6968 if (fn)
6969 inform (DECL_SOURCE_LOCATION (fn),
6970 " initializing argument %P of %qD", argnum, fn);
6971 }
6972 return error_mark_node;
6973 }
6974
6975 if (! MAYBE_CLASS_TYPE_P (totype))
6976 return expr;
6977
6978 /* Don't introduce copies when passing arguments along to the inherited
6979 constructor. */
6980 if (current_function_decl
6981 && flag_new_inheriting_ctors
6982 && DECL_INHERITED_CTOR (current_function_decl))
6983 return expr;
6984
6985 if (TREE_CODE (expr) == TARGET_EXPR
6986 && TARGET_EXPR_LIST_INIT_P (expr))
6987 /* Copy-list-initialization doesn't actually involve a copy. */
6988 return expr;
6989
6990 /* Fall through. */
6991 case ck_base:
6992 if (convs->kind == ck_base && !convs->need_temporary_p)
6993 {
6994 /* We are going to bind a reference directly to a base-class
6995 subobject of EXPR. */
6996 /* Build an expression for `*((base*) &expr)'. */
6997 expr = convert_to_base (expr, totype,
6998 !c_cast_p, /*nonnull=*/true, complain);
6999 return expr;
7000 }
7001
7002 /* Copy-initialization where the cv-unqualified version of the source
7003 type is the same class as, or a derived class of, the class of the
7004 destination [is treated as direct-initialization]. [dcl.init] */
7005 flags = LOOKUP_NORMAL;
7006 if (convs->user_conv_p)
7007 /* This conversion is being done in the context of a user-defined
7008 conversion (i.e. the second step of copy-initialization), so
7009 don't allow any more. */
7010 flags |= LOOKUP_NO_CONVERSION;
7011 else
7012 flags |= LOOKUP_ONLYCONVERTING;
7013 if (convs->rvaluedness_matches_p)
7014 /* standard_conversion got LOOKUP_PREFER_RVALUE. */
7015 flags |= LOOKUP_PREFER_RVALUE;
7016 expr = build_temp (expr, totype, flags, &diag_kind, complain);
7017 if (diag_kind && complain)
7018 {
7019 maybe_print_user_conv_context (convs);
7020 if (fn)
7021 inform (DECL_SOURCE_LOCATION (fn),
7022 " initializing argument %P of %qD", argnum, fn);
7023 }
7024
7025 return build_cplus_new (totype, expr, complain);
7026
7027 case ck_ref_bind:
7028 {
7029 tree ref_type = totype;
7030
7031 if (convs->bad_p && !next_conversion (convs)->bad_p)
7032 {
7033 tree extype = TREE_TYPE (expr);
7034 if (TYPE_REF_IS_RVALUE (ref_type)
7035 && lvalue_p (expr))
7036 error_at (loc, "cannot bind rvalue reference of type %qH to "
7037 "lvalue of type %qI", totype, extype);
7038 else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
7039 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
7040 error_at (loc, "cannot bind non-const lvalue reference of "
7041 "type %qH to an rvalue of type %qI", totype, extype);
7042 else if (!reference_compatible_p (TREE_TYPE (totype), extype))
7043 error_at (loc, "binding reference of type %qH to %qI "
7044 "discards qualifiers", totype, extype);
7045 else
7046 gcc_unreachable ();
7047 maybe_print_user_conv_context (convs);
7048 if (fn)
7049 inform (DECL_SOURCE_LOCATION (fn),
7050 " initializing argument %P of %qD", argnum, fn);
7051 return error_mark_node;
7052 }
7053
7054 /* If necessary, create a temporary.
7055
7056 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
7057 that need temporaries, even when their types are reference
7058 compatible with the type of reference being bound, so the
7059 upcoming call to cp_build_addr_expr doesn't fail. */
7060 if (convs->need_temporary_p
7061 || TREE_CODE (expr) == CONSTRUCTOR
7062 || TREE_CODE (expr) == VA_ARG_EXPR)
7063 {
7064 /* Otherwise, a temporary of type "cv1 T1" is created and
7065 initialized from the initializer expression using the rules
7066 for a non-reference copy-initialization (8.5). */
7067
7068 tree type = TREE_TYPE (ref_type);
7069 cp_lvalue_kind lvalue = lvalue_kind (expr);
7070
7071 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7072 (type, next_conversion (convs)->type));
7073 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
7074 && !TYPE_REF_IS_RVALUE (ref_type))
7075 {
7076 /* If the reference is volatile or non-const, we
7077 cannot create a temporary. */
7078 if (lvalue & clk_bitfield)
7079 error_at (loc, "cannot bind bitfield %qE to %qT",
7080 expr, ref_type);
7081 else if (lvalue & clk_packed)
7082 error_at (loc, "cannot bind packed field %qE to %qT",
7083 expr, ref_type);
7084 else
7085 error_at (loc, "cannot bind rvalue %qE to %qT",
7086 expr, ref_type);
7087 return error_mark_node;
7088 }
7089 /* If the source is a packed field, and we must use a copy
7090 constructor, then building the target expr will require
7091 binding the field to the reference parameter to the
7092 copy constructor, and we'll end up with an infinite
7093 loop. If we can use a bitwise copy, then we'll be
7094 OK. */
7095 if ((lvalue & clk_packed)
7096 && CLASS_TYPE_P (type)
7097 && type_has_nontrivial_copy_init (type))
7098 {
7099 error_at (loc, "cannot bind packed field %qE to %qT",
7100 expr, ref_type);
7101 return error_mark_node;
7102 }
7103 if (lvalue & clk_bitfield)
7104 {
7105 expr = convert_bitfield_to_declared_type (expr);
7106 expr = fold_convert (type, expr);
7107 }
7108 expr = build_target_expr_with_type (expr, type, complain);
7109 }
7110
7111 /* Take the address of the thing to which we will bind the
7112 reference. */
7113 expr = cp_build_addr_expr (expr, complain);
7114 if (expr == error_mark_node)
7115 return error_mark_node;
7116
7117 /* Convert it to a pointer to the type referred to by the
7118 reference. This will adjust the pointer if a derived to
7119 base conversion is being performed. */
7120 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
7121 expr, complain);
7122 /* Convert the pointer to the desired reference type. */
7123 return build_nop (ref_type, expr);
7124 }
7125
7126 case ck_lvalue:
7127 return decay_conversion (expr, complain);
7128
7129 case ck_fnptr:
7130 /* ??? Should the address of a transaction-safe pointer point to the TM
7131 clone, and this conversion look up the primary function? */
7132 return build_nop (totype, expr);
7133
7134 case ck_qual:
7135 /* Warn about deprecated conversion if appropriate. */
7136 if (complain & tf_warning)
7137 string_conv_p (totype, expr, 1);
7138 break;
7139
7140 case ck_ptr:
7141 if (convs->base_p)
7142 expr = convert_to_base (expr, totype, !c_cast_p,
7143 /*nonnull=*/false, complain);
7144 return build_nop (totype, expr);
7145
7146 case ck_pmem:
7147 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
7148 c_cast_p, complain);
7149
7150 default:
7151 break;
7152 }
7153
7154 if (convs->check_narrowing
7155 && !check_narrowing (totype, expr, complain))
7156 return error_mark_node;
7157
7158 if (issue_conversion_warnings)
7159 expr = cp_convert_and_check (totype, expr, complain);
7160 else
7161 expr = cp_convert (totype, expr, complain);
7162
7163 return expr;
7164 }
7165
7166 /* ARG is being passed to a varargs function. Perform any conversions
7167 required. Return the converted value. */
7168
7169 tree
convert_arg_to_ellipsis(tree arg,tsubst_flags_t complain)7170 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
7171 {
7172 tree arg_type;
7173 location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
7174
7175 /* [expr.call]
7176
7177 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7178 standard conversions are performed. */
7179 arg = decay_conversion (arg, complain);
7180 arg_type = TREE_TYPE (arg);
7181 /* [expr.call]
7182
7183 If the argument has integral or enumeration type that is subject
7184 to the integral promotions (_conv.prom_), or a floating point
7185 type that is subject to the floating point promotion
7186 (_conv.fpprom_), the value of the argument is converted to the
7187 promoted type before the call. */
7188 if (TREE_CODE (arg_type) == REAL_TYPE
7189 && (TYPE_PRECISION (arg_type)
7190 < TYPE_PRECISION (double_type_node))
7191 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
7192 {
7193 if ((complain & tf_warning)
7194 && warn_double_promotion && !c_inhibit_evaluation_warnings)
7195 warning_at (loc, OPT_Wdouble_promotion,
7196 "implicit conversion from %qH to %qI when passing "
7197 "argument to function",
7198 arg_type, double_type_node);
7199 arg = convert_to_real_nofold (double_type_node, arg);
7200 }
7201 else if (NULLPTR_TYPE_P (arg_type))
7202 {
7203 if (TREE_SIDE_EFFECTS (arg))
7204 arg = cp_build_compound_expr (arg, null_pointer_node, complain);
7205 else
7206 arg = null_pointer_node;
7207 }
7208 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
7209 {
7210 if (SCOPED_ENUM_P (arg_type))
7211 {
7212 tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
7213 complain);
7214 prom = cp_perform_integral_promotions (prom, complain);
7215 if (abi_version_crosses (6)
7216 && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
7217 && (complain & tf_warning))
7218 warning_at (loc, OPT_Wabi, "scoped enum %qT passed through ... as "
7219 "%qT before -fabi-version=6, %qT after", arg_type,
7220 TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
7221 if (!abi_version_at_least (6))
7222 arg = prom;
7223 }
7224 else
7225 arg = cp_perform_integral_promotions (arg, complain);
7226 }
7227
7228 arg = require_complete_type_sfinae (arg, complain);
7229 arg_type = TREE_TYPE (arg);
7230
7231 if (arg != error_mark_node
7232 /* In a template (or ill-formed code), we can have an incomplete type
7233 even after require_complete_type_sfinae, in which case we don't know
7234 whether it has trivial copy or not. */
7235 && COMPLETE_TYPE_P (arg_type)
7236 && !cp_unevaluated_operand)
7237 {
7238 /* [expr.call] 5.2.2/7:
7239 Passing a potentially-evaluated argument of class type (Clause 9)
7240 with a non-trivial copy constructor or a non-trivial destructor
7241 with no corresponding parameter is conditionally-supported, with
7242 implementation-defined semantics.
7243
7244 We support it as pass-by-invisible-reference, just like a normal
7245 value parameter.
7246
7247 If the call appears in the context of a sizeof expression,
7248 it is not potentially-evaluated. */
7249 if (type_has_nontrivial_copy_init (arg_type)
7250 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
7251 {
7252 arg = force_rvalue (arg, complain);
7253 if (complain & tf_warning)
7254 warning (OPT_Wconditionally_supported,
7255 "passing objects of non-trivially-copyable "
7256 "type %q#T through %<...%> is conditionally supported",
7257 arg_type);
7258 return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
7259 }
7260 /* Build up a real lvalue-to-rvalue conversion in case the
7261 copy constructor is trivial but not callable. */
7262 else if (CLASS_TYPE_P (arg_type))
7263 force_rvalue (arg, complain);
7264
7265 }
7266
7267 return arg;
7268 }
7269
7270 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
7271
7272 tree
build_x_va_arg(source_location loc,tree expr,tree type)7273 build_x_va_arg (source_location loc, tree expr, tree type)
7274 {
7275 if (processing_template_decl)
7276 {
7277 tree r = build_min (VA_ARG_EXPR, type, expr);
7278 SET_EXPR_LOCATION (r, loc);
7279 return r;
7280 }
7281
7282 type = complete_type_or_else (type, NULL_TREE);
7283
7284 if (expr == error_mark_node || !type)
7285 return error_mark_node;
7286
7287 expr = mark_lvalue_use (expr);
7288
7289 if (TREE_CODE (type) == REFERENCE_TYPE)
7290 {
7291 error ("cannot receive reference type %qT through %<...%>", type);
7292 return error_mark_node;
7293 }
7294
7295 if (type_has_nontrivial_copy_init (type)
7296 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7297 {
7298 /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
7299 it as pass by invisible reference. */
7300 warning_at (loc, OPT_Wconditionally_supported,
7301 "receiving objects of non-trivially-copyable type %q#T "
7302 "through %<...%> is conditionally-supported", type);
7303
7304 tree ref = cp_build_reference_type (type, false);
7305 expr = build_va_arg (loc, expr, ref);
7306 return convert_from_reference (expr);
7307 }
7308
7309 tree ret = build_va_arg (loc, expr, type);
7310 if (CLASS_TYPE_P (type))
7311 /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
7312 know how to handle it. */
7313 ret = get_target_expr (ret);
7314 return ret;
7315 }
7316
7317 /* TYPE has been given to va_arg. Apply the default conversions which
7318 would have happened when passed via ellipsis. Return the promoted
7319 type, or the passed type if there is no change. */
7320
7321 tree
cxx_type_promotes_to(tree type)7322 cxx_type_promotes_to (tree type)
7323 {
7324 tree promote;
7325
7326 /* Perform the array-to-pointer and function-to-pointer
7327 conversions. */
7328 type = type_decays_to (type);
7329
7330 promote = type_promotes_to (type);
7331 if (same_type_p (type, promote))
7332 promote = type;
7333
7334 return promote;
7335 }
7336
7337 /* ARG is a default argument expression being passed to a parameter of
7338 the indicated TYPE, which is a parameter to FN. PARMNUM is the
7339 zero-based argument number. Do any required conversions. Return
7340 the converted value. */
7341
7342 static GTY(()) vec<tree, va_gc> *default_arg_context;
7343 void
push_defarg_context(tree fn)7344 push_defarg_context (tree fn)
7345 { vec_safe_push (default_arg_context, fn); }
7346
7347 void
pop_defarg_context(void)7348 pop_defarg_context (void)
7349 { default_arg_context->pop (); }
7350
7351 tree
convert_default_arg(tree type,tree arg,tree fn,int parmnum,tsubst_flags_t complain)7352 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
7353 tsubst_flags_t complain)
7354 {
7355 int i;
7356 tree t;
7357
7358 /* See through clones. */
7359 fn = DECL_ORIGIN (fn);
7360 /* And inheriting ctors. */
7361 if (flag_new_inheriting_ctors)
7362 fn = strip_inheriting_ctors (fn);
7363
7364 /* Detect recursion. */
7365 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
7366 if (t == fn)
7367 {
7368 if (complain & tf_error)
7369 error ("recursive evaluation of default argument for %q#D", fn);
7370 return error_mark_node;
7371 }
7372
7373 /* If the ARG is an unparsed default argument expression, the
7374 conversion cannot be performed. */
7375 if (TREE_CODE (arg) == DEFAULT_ARG)
7376 {
7377 if (complain & tf_error)
7378 error ("call to %qD uses the default argument for parameter %P, which "
7379 "is not yet defined", fn, parmnum);
7380 return error_mark_node;
7381 }
7382
7383 push_defarg_context (fn);
7384
7385 if (fn && DECL_TEMPLATE_INFO (fn))
7386 arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
7387
7388 /* Due to:
7389
7390 [dcl.fct.default]
7391
7392 The names in the expression are bound, and the semantic
7393 constraints are checked, at the point where the default
7394 expressions appears.
7395
7396 we must not perform access checks here. */
7397 push_deferring_access_checks (dk_no_check);
7398 /* We must make a copy of ARG, in case subsequent processing
7399 alters any part of it. */
7400 arg = break_out_target_exprs (arg, /*clear location*/true);
7401
7402 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
7403 ICR_DEFAULT_ARGUMENT, fn, parmnum,
7404 complain);
7405 arg = convert_for_arg_passing (type, arg, complain);
7406 pop_deferring_access_checks();
7407
7408 pop_defarg_context ();
7409
7410 return arg;
7411 }
7412
7413 /* Returns the type which will really be used for passing an argument of
7414 type TYPE. */
7415
7416 tree
type_passed_as(tree type)7417 type_passed_as (tree type)
7418 {
7419 /* Pass classes with copy ctors by invisible reference. */
7420 if (TREE_ADDRESSABLE (type))
7421 {
7422 type = build_reference_type (type);
7423 /* There are no other pointers to this temporary. */
7424 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
7425 }
7426 else if (targetm.calls.promote_prototypes (NULL_TREE)
7427 && INTEGRAL_TYPE_P (type)
7428 && COMPLETE_TYPE_P (type)
7429 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
7430 type = integer_type_node;
7431
7432 return type;
7433 }
7434
7435 /* Actually perform the appropriate conversion. */
7436
7437 tree
convert_for_arg_passing(tree type,tree val,tsubst_flags_t complain)7438 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
7439 {
7440 tree bitfield_type;
7441
7442 /* If VAL is a bitfield, then -- since it has already been converted
7443 to TYPE -- it cannot have a precision greater than TYPE.
7444
7445 If it has a smaller precision, we must widen it here. For
7446 example, passing "int f:3;" to a function expecting an "int" will
7447 not result in any conversion before this point.
7448
7449 If the precision is the same we must not risk widening. For
7450 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
7451 often have type "int", even though the C++ type for the field is
7452 "long long". If the value is being passed to a function
7453 expecting an "int", then no conversions will be required. But,
7454 if we call convert_bitfield_to_declared_type, the bitfield will
7455 be converted to "long long". */
7456 bitfield_type = is_bitfield_expr_with_lowered_type (val);
7457 if (bitfield_type
7458 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
7459 val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
7460
7461 if (val == error_mark_node)
7462 ;
7463 /* Pass classes with copy ctors by invisible reference. */
7464 else if (TREE_ADDRESSABLE (type))
7465 val = build1 (ADDR_EXPR, build_reference_type (type), val);
7466 else if (targetm.calls.promote_prototypes (NULL_TREE)
7467 && INTEGRAL_TYPE_P (type)
7468 && COMPLETE_TYPE_P (type)
7469 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
7470 val = cp_perform_integral_promotions (val, complain);
7471 if (complain & tf_warning)
7472 {
7473 if (warn_suggest_attribute_format)
7474 {
7475 tree rhstype = TREE_TYPE (val);
7476 const enum tree_code coder = TREE_CODE (rhstype);
7477 const enum tree_code codel = TREE_CODE (type);
7478 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7479 && coder == codel
7480 && check_missing_format_attribute (type, rhstype))
7481 warning (OPT_Wsuggest_attribute_format,
7482 "argument of function call might be a candidate "
7483 "for a format attribute");
7484 }
7485 maybe_warn_parm_abi (type, EXPR_LOC_OR_LOC (val, input_location));
7486 }
7487 return val;
7488 }
7489
7490 /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
7491 which just decay_conversion or no conversions at all should be done.
7492 This is true for some builtins which don't act like normal functions.
7493 Return 2 if no conversions at all should be done, 1 if just
7494 decay_conversion. Return 3 for special treatment of the 3rd argument
7495 for __builtin_*_overflow_p. */
7496
7497 int
magic_varargs_p(tree fn)7498 magic_varargs_p (tree fn)
7499 {
7500 if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
7501 switch (DECL_FUNCTION_CODE (fn))
7502 {
7503 case BUILT_IN_CLASSIFY_TYPE:
7504 case BUILT_IN_CONSTANT_P:
7505 case BUILT_IN_NEXT_ARG:
7506 case BUILT_IN_VA_START:
7507 return 1;
7508
7509 case BUILT_IN_ADD_OVERFLOW_P:
7510 case BUILT_IN_SUB_OVERFLOW_P:
7511 case BUILT_IN_MUL_OVERFLOW_P:
7512 return 3;
7513
7514 default:;
7515 return lookup_attribute ("type generic",
7516 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
7517 }
7518
7519 return 0;
7520 }
7521
7522 /* Returns the decl of the dispatcher function if FN is a function version. */
7523
7524 tree
get_function_version_dispatcher(tree fn)7525 get_function_version_dispatcher (tree fn)
7526 {
7527 tree dispatcher_decl = NULL;
7528
7529 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7530 && DECL_FUNCTION_VERSIONED (fn));
7531
7532 gcc_assert (targetm.get_function_versions_dispatcher);
7533 dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
7534
7535 if (dispatcher_decl == NULL)
7536 {
7537 error_at (input_location, "use of multiversioned function "
7538 "without a default");
7539 return NULL;
7540 }
7541
7542 retrofit_lang_decl (dispatcher_decl);
7543 gcc_assert (dispatcher_decl != NULL);
7544 return dispatcher_decl;
7545 }
7546
7547 /* fn is a function version dispatcher that is marked used. Mark all the
7548 semantically identical function versions it will dispatch as used. */
7549
7550 void
mark_versions_used(tree fn)7551 mark_versions_used (tree fn)
7552 {
7553 struct cgraph_node *node;
7554 struct cgraph_function_version_info *node_v;
7555 struct cgraph_function_version_info *it_v;
7556
7557 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7558
7559 node = cgraph_node::get (fn);
7560 if (node == NULL)
7561 return;
7562
7563 gcc_assert (node->dispatcher_function);
7564
7565 node_v = node->function_version ();
7566 if (node_v == NULL)
7567 return;
7568
7569 /* All semantically identical versions are chained. Traverse and mark each
7570 one of them as used. */
7571 it_v = node_v->next;
7572 while (it_v != NULL)
7573 {
7574 mark_used (it_v->this_node->decl);
7575 it_v = it_v->next;
7576 }
7577 }
7578
7579 /* Build a call to "the copy constructor" for the type of A, even if it
7580 wouldn't be selected by normal overload resolution. Used for
7581 diagnostics. */
7582
7583 static tree
call_copy_ctor(tree a,tsubst_flags_t complain)7584 call_copy_ctor (tree a, tsubst_flags_t complain)
7585 {
7586 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
7587 tree binfo = TYPE_BINFO (ctype);
7588 tree copy = get_copy_ctor (ctype, complain);
7589 copy = build_baselink (binfo, binfo, copy, NULL_TREE);
7590 tree ob = build_dummy_object (ctype);
7591 vec<tree, va_gc>* args = make_tree_vector_single (a);
7592 tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
7593 LOOKUP_NORMAL, NULL, complain);
7594 release_tree_vector (args);
7595 return r;
7596 }
7597
7598 /* Return true iff T refers to a base field. */
7599
7600 static bool
is_base_field_ref(tree t)7601 is_base_field_ref (tree t)
7602 {
7603 STRIP_NOPS (t);
7604 if (TREE_CODE (t) == ADDR_EXPR)
7605 t = TREE_OPERAND (t, 0);
7606 if (TREE_CODE (t) == COMPONENT_REF)
7607 t = TREE_OPERAND (t, 1);
7608 if (TREE_CODE (t) == FIELD_DECL)
7609 return DECL_FIELD_IS_BASE (t);
7610 return false;
7611 }
7612
7613 /* We can't elide a copy from a function returning by value to a base
7614 subobject, as the callee might clobber tail padding. Return true iff this
7615 could be that case. */
7616
7617 static bool
unsafe_copy_elision_p(tree target,tree exp)7618 unsafe_copy_elision_p (tree target, tree exp)
7619 {
7620 /* Copy elision only happens with a TARGET_EXPR. */
7621 if (TREE_CODE (exp) != TARGET_EXPR)
7622 return false;
7623 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7624 /* It's safe to elide the copy for a class with no tail padding. */
7625 if (tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
7626 return false;
7627 /* It's safe to elide the copy if we aren't initializing a base object. */
7628 if (!is_base_field_ref (target))
7629 return false;
7630 tree init = TARGET_EXPR_INITIAL (exp);
7631 /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
7632 while (TREE_CODE (init) == COMPOUND_EXPR)
7633 init = TREE_OPERAND (init, 1);
7634 if (TREE_CODE (init) == COND_EXPR)
7635 {
7636 /* We'll end up copying from each of the arms of the COND_EXPR directly
7637 into the target, so look at them. */
7638 if (tree op = TREE_OPERAND (init, 1))
7639 if (unsafe_copy_elision_p (target, op))
7640 return true;
7641 return unsafe_copy_elision_p (target, TREE_OPERAND (init, 2));
7642 }
7643 return (TREE_CODE (init) == AGGR_INIT_EXPR
7644 && !AGGR_INIT_VIA_CTOR_P (init));
7645 }
7646
7647 /* True iff C is a conversion that binds a reference to a prvalue. */
7648
7649 static bool
conv_binds_ref_to_prvalue(conversion * c)7650 conv_binds_ref_to_prvalue (conversion *c)
7651 {
7652 if (c->kind != ck_ref_bind)
7653 return false;
7654 if (c->need_temporary_p)
7655 return true;
7656
7657 c = next_conversion (c);
7658
7659 if (c->kind == ck_rvalue)
7660 return true;
7661 if (c->kind == ck_user && TREE_CODE (c->type) != REFERENCE_TYPE)
7662 return true;
7663 if (c->kind == ck_identity && c->u.expr
7664 && TREE_CODE (c->u.expr) == TARGET_EXPR)
7665 return true;
7666
7667 return false;
7668 }
7669
7670 /* Subroutine of the various build_*_call functions. Overload resolution
7671 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
7672 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
7673 bitmask of various LOOKUP_* flags which apply to the call itself. */
7674
7675 static tree
build_over_call(struct z_candidate * cand,int flags,tsubst_flags_t complain)7676 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
7677 {
7678 tree fn = cand->fn;
7679 const vec<tree, va_gc> *args = cand->args;
7680 tree first_arg = cand->first_arg;
7681 conversion **convs = cand->convs;
7682 conversion *conv;
7683 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
7684 int parmlen;
7685 tree val;
7686 int i = 0;
7687 int j = 0;
7688 unsigned int arg_index = 0;
7689 int is_method = 0;
7690 int nargs;
7691 tree *argarray;
7692 bool already_used = false;
7693
7694 /* In a template, there is no need to perform all of the work that
7695 is normally done. We are only interested in the type of the call
7696 expression, i.e., the return type of the function. Any semantic
7697 errors will be deferred until the template is instantiated. */
7698 if (processing_template_decl)
7699 {
7700 tree expr, addr;
7701 tree return_type;
7702 const tree *argarray;
7703 unsigned int nargs;
7704
7705 if (undeduced_auto_decl (fn))
7706 mark_used (fn, complain);
7707 else
7708 /* Otherwise set TREE_USED for the benefit of -Wunused-function.
7709 See PR80598. */
7710 TREE_USED (fn) = 1;
7711
7712 return_type = TREE_TYPE (TREE_TYPE (fn));
7713 nargs = vec_safe_length (args);
7714 if (first_arg == NULL_TREE)
7715 argarray = args->address ();
7716 else
7717 {
7718 tree *alcarray;
7719 unsigned int ix;
7720 tree arg;
7721
7722 ++nargs;
7723 alcarray = XALLOCAVEC (tree, nargs);
7724 alcarray[0] = build_this (first_arg);
7725 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
7726 alcarray[ix + 1] = arg;
7727 argarray = alcarray;
7728 }
7729
7730 addr = build_addr_func (fn, complain);
7731 if (addr == error_mark_node)
7732 return error_mark_node;
7733 expr = build_call_array_loc (input_location, return_type,
7734 addr, nargs, argarray);
7735 if (TREE_THIS_VOLATILE (fn) && cfun)
7736 current_function_returns_abnormally = 1;
7737 return convert_from_reference (expr);
7738 }
7739
7740 /* Give any warnings we noticed during overload resolution. */
7741 if (cand->warnings && (complain & tf_warning))
7742 {
7743 struct candidate_warning *w;
7744 for (w = cand->warnings; w; w = w->next)
7745 joust (cand, w->loser, 1, complain);
7746 }
7747
7748 /* Core issue 2327: P0135 doesn't say how to handle the case where the
7749 argument to the copy constructor ends up being a prvalue after
7750 conversion. Let's do the normal processing, but pretend we aren't
7751 actually using the copy constructor. */
7752 bool force_elide = false;
7753 if (cxx_dialect >= cxx17
7754 && cand->num_convs == 1
7755 && DECL_COMPLETE_CONSTRUCTOR_P (fn)
7756 && (DECL_COPY_CONSTRUCTOR_P (fn)
7757 || DECL_MOVE_CONSTRUCTOR_P (fn))
7758 && conv_binds_ref_to_prvalue (convs[0]))
7759 {
7760 force_elide = true;
7761 goto not_really_used;
7762 }
7763
7764 /* OK, we're actually calling this inherited constructor; set its deletedness
7765 appropriately. We can get away with doing this here because calling is
7766 the only way to refer to a constructor. */
7767 if (DECL_INHERITED_CTOR (fn))
7768 deduce_inheriting_ctor (fn);
7769
7770 /* Make =delete work with SFINAE. */
7771 if (DECL_DELETED_FN (fn))
7772 {
7773 if (complain & tf_error)
7774 mark_used (fn);
7775 return error_mark_node;
7776 }
7777
7778 if (DECL_FUNCTION_MEMBER_P (fn))
7779 {
7780 tree access_fn;
7781 /* If FN is a template function, two cases must be considered.
7782 For example:
7783
7784 struct A {
7785 protected:
7786 template <class T> void f();
7787 };
7788 template <class T> struct B {
7789 protected:
7790 void g();
7791 };
7792 struct C : A, B<int> {
7793 using A::f; // #1
7794 using B<int>::g; // #2
7795 };
7796
7797 In case #1 where `A::f' is a member template, DECL_ACCESS is
7798 recorded in the primary template but not in its specialization.
7799 We check access of FN using its primary template.
7800
7801 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
7802 because it is a member of class template B, DECL_ACCESS is
7803 recorded in the specialization `B<int>::g'. We cannot use its
7804 primary template because `B<T>::g' and `B<int>::g' may have
7805 different access. */
7806 if (DECL_TEMPLATE_INFO (fn)
7807 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
7808 access_fn = DECL_TI_TEMPLATE (fn);
7809 else
7810 access_fn = fn;
7811 if (!perform_or_defer_access_check (cand->access_path, access_fn,
7812 fn, complain))
7813 return error_mark_node;
7814 }
7815
7816 /* If we're checking for implicit delete, don't bother with argument
7817 conversions. */
7818 if (flags & LOOKUP_SPECULATIVE)
7819 {
7820 if (cand->viable == 1)
7821 return fn;
7822 else if (!(complain & tf_error))
7823 /* Reject bad conversions now. */
7824 return error_mark_node;
7825 /* else continue to get conversion error. */
7826 }
7827
7828 not_really_used:
7829
7830 /* N3276 magic doesn't apply to nested calls. */
7831 tsubst_flags_t decltype_flag = (complain & tf_decltype);
7832 complain &= ~tf_decltype;
7833 /* No-Cleanup doesn't apply to nested calls either. */
7834 tsubst_flags_t no_cleanup_complain = complain;
7835 complain &= ~tf_no_cleanup;
7836
7837 /* Find maximum size of vector to hold converted arguments. */
7838 parmlen = list_length (parm);
7839 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
7840 if (parmlen > nargs)
7841 nargs = parmlen;
7842 argarray = XALLOCAVEC (tree, nargs);
7843
7844 /* The implicit parameters to a constructor are not considered by overload
7845 resolution, and must be of the proper type. */
7846 if (DECL_CONSTRUCTOR_P (fn))
7847 {
7848 tree object_arg;
7849 if (first_arg != NULL_TREE)
7850 {
7851 object_arg = first_arg;
7852 first_arg = NULL_TREE;
7853 }
7854 else
7855 {
7856 object_arg = (*args)[arg_index];
7857 ++arg_index;
7858 }
7859 argarray[j++] = build_this (object_arg);
7860 parm = TREE_CHAIN (parm);
7861 /* We should never try to call the abstract constructor. */
7862 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
7863
7864 if (DECL_HAS_VTT_PARM_P (fn))
7865 {
7866 argarray[j++] = (*args)[arg_index];
7867 ++arg_index;
7868 parm = TREE_CHAIN (parm);
7869 }
7870
7871 if (flags & LOOKUP_PREFER_RVALUE)
7872 {
7873 /* The implicit move specified in 15.8.3/3 fails "...if the type of
7874 the first parameter of the selected constructor is not an rvalue
7875 reference to the object’s type (possibly cv-qualified)...." */
7876 gcc_assert (!(complain & tf_error));
7877 tree ptype = convs[0]->type;
7878 if (TREE_CODE (ptype) != REFERENCE_TYPE
7879 || !TYPE_REF_IS_RVALUE (ptype)
7880 || CONVERSION_RANK (convs[0]) > cr_exact)
7881 return error_mark_node;
7882 }
7883 }
7884 /* Bypass access control for 'this' parameter. */
7885 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7886 {
7887 tree parmtype = TREE_VALUE (parm);
7888 tree arg = build_this (first_arg != NULL_TREE
7889 ? first_arg
7890 : (*args)[arg_index]);
7891 tree argtype = TREE_TYPE (arg);
7892 tree converted_arg;
7893 tree base_binfo;
7894
7895 if (arg == error_mark_node)
7896 return error_mark_node;
7897
7898 if (convs[i]->bad_p)
7899 {
7900 if (complain & tf_error)
7901 {
7902 if (permerror (input_location, "passing %qT as %<this%> "
7903 "argument discards qualifiers",
7904 TREE_TYPE (argtype)))
7905 inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
7906 }
7907 else
7908 return error_mark_node;
7909 }
7910
7911 /* See if the function member or the whole class type is declared
7912 final and the call can be devirtualized. */
7913 if (DECL_FINAL_P (fn)
7914 || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
7915 flags |= LOOKUP_NONVIRTUAL;
7916
7917 /* If we know the dynamic type of the object, look up the final overrider
7918 in the BINFO. */
7919 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
7920 && resolves_to_fixed_type_p (arg))
7921 {
7922 tree binfo = cand->conversion_path;
7923 if (BINFO_TYPE (binfo) != DECL_CONTEXT (fn))
7924 binfo = lookup_base (binfo, DECL_CONTEXT (fn), ba_unique,
7925 NULL, complain);
7926 fn = lookup_vfn_in_binfo (DECL_VINDEX (fn), binfo);
7927 flags |= LOOKUP_NONVIRTUAL;
7928 }
7929
7930 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
7931 X is called for an object that is not of type X, or of a type
7932 derived from X, the behavior is undefined.
7933
7934 So we can assume that anything passed as 'this' is non-null, and
7935 optimize accordingly. */
7936 gcc_assert (TYPE_PTR_P (parmtype));
7937 /* Convert to the base in which the function was declared. */
7938 gcc_assert (cand->conversion_path != NULL_TREE);
7939 converted_arg = build_base_path (PLUS_EXPR,
7940 arg,
7941 cand->conversion_path,
7942 1, complain);
7943 /* Check that the base class is accessible. */
7944 if (!accessible_base_p (TREE_TYPE (argtype),
7945 BINFO_TYPE (cand->conversion_path), true))
7946 {
7947 if (complain & tf_error)
7948 error ("%qT is not an accessible base of %qT",
7949 BINFO_TYPE (cand->conversion_path),
7950 TREE_TYPE (argtype));
7951 else
7952 return error_mark_node;
7953 }
7954 /* If fn was found by a using declaration, the conversion path
7955 will be to the derived class, not the base declaring fn. We
7956 must convert from derived to base. */
7957 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
7958 TREE_TYPE (parmtype), ba_unique,
7959 NULL, complain);
7960 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
7961 base_binfo, 1, complain);
7962
7963 argarray[j++] = converted_arg;
7964 parm = TREE_CHAIN (parm);
7965 if (first_arg != NULL_TREE)
7966 first_arg = NULL_TREE;
7967 else
7968 ++arg_index;
7969 ++i;
7970 is_method = 1;
7971 }
7972
7973 gcc_assert (first_arg == NULL_TREE);
7974 for (; arg_index < vec_safe_length (args) && parm;
7975 parm = TREE_CHAIN (parm), ++arg_index, ++i)
7976 {
7977 tree type = TREE_VALUE (parm);
7978 tree arg = (*args)[arg_index];
7979 bool conversion_warning = true;
7980
7981 conv = convs[i];
7982
7983 /* If the argument is NULL and used to (implicitly) instantiate a
7984 template function (and bind one of the template arguments to
7985 the type of 'long int'), we don't want to warn about passing NULL
7986 to non-pointer argument.
7987 For example, if we have this template function:
7988
7989 template<typename T> void func(T x) {}
7990
7991 we want to warn (when -Wconversion is enabled) in this case:
7992
7993 void foo() {
7994 func<int>(NULL);
7995 }
7996
7997 but not in this case:
7998
7999 void foo() {
8000 func(NULL);
8001 }
8002 */
8003 if (null_node_p (arg)
8004 && DECL_TEMPLATE_INFO (fn)
8005 && cand->template_decl
8006 && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
8007 conversion_warning = false;
8008
8009 /* Warn about initializer_list deduction that isn't currently in the
8010 working draft. */
8011 if (cxx_dialect > cxx98
8012 && flag_deduce_init_list
8013 && cand->template_decl
8014 && is_std_init_list (non_reference (type))
8015 && BRACE_ENCLOSED_INITIALIZER_P (arg))
8016 {
8017 tree tmpl = TI_TEMPLATE (cand->template_decl);
8018 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
8019 tree patparm = get_pattern_parm (realparm, tmpl);
8020 tree pattype = TREE_TYPE (patparm);
8021 if (PACK_EXPANSION_P (pattype))
8022 pattype = PACK_EXPANSION_PATTERN (pattype);
8023 pattype = non_reference (pattype);
8024
8025 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
8026 && (cand->explicit_targs == NULL_TREE
8027 || (TREE_VEC_LENGTH (cand->explicit_targs)
8028 <= TEMPLATE_TYPE_IDX (pattype))))
8029 {
8030 pedwarn (input_location, 0, "deducing %qT as %qT",
8031 non_reference (TREE_TYPE (patparm)),
8032 non_reference (type));
8033 pedwarn (DECL_SOURCE_LOCATION (cand->fn), 0,
8034 " in call to %qD", cand->fn);
8035 pedwarn (input_location, 0,
8036 " (you can disable this with -fno-deduce-init-list)");
8037 }
8038 }
8039
8040 /* Set user_conv_p on the argument conversions, so rvalue/base handling
8041 knows not to allow any more UDCs. This needs to happen after we
8042 process cand->warnings. */
8043 if (flags & LOOKUP_NO_CONVERSION)
8044 conv->user_conv_p = true;
8045
8046 tsubst_flags_t arg_complain = complain;
8047 if (!conversion_warning)
8048 arg_complain &= ~tf_warning;
8049
8050 val = convert_like_with_context (conv, arg, fn, i - is_method,
8051 arg_complain);
8052 val = convert_for_arg_passing (type, val, arg_complain);
8053
8054 if (val == error_mark_node)
8055 return error_mark_node;
8056 else
8057 argarray[j++] = val;
8058 }
8059
8060 /* Default arguments */
8061 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
8062 {
8063 if (TREE_VALUE (parm) == error_mark_node)
8064 return error_mark_node;
8065 val = convert_default_arg (TREE_VALUE (parm),
8066 TREE_PURPOSE (parm),
8067 fn, i - is_method,
8068 complain);
8069 if (val == error_mark_node)
8070 return error_mark_node;
8071 argarray[j++] = val;
8072 }
8073
8074 /* Ellipsis */
8075 int magic = magic_varargs_p (fn);
8076 for (; arg_index < vec_safe_length (args); ++arg_index)
8077 {
8078 tree a = (*args)[arg_index];
8079 if ((magic == 3 && arg_index == 2) || magic == 2)
8080 {
8081 /* Do no conversions for certain magic varargs. */
8082 a = mark_type_use (a);
8083 if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
8084 return error_mark_node;
8085 }
8086 else if (magic != 0)
8087 /* For other magic varargs only do decay_conversion. */
8088 a = decay_conversion (a, complain);
8089 else if (DECL_CONSTRUCTOR_P (fn)
8090 && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
8091 TREE_TYPE (a)))
8092 {
8093 /* Avoid infinite recursion trying to call A(...). */
8094 if (complain & tf_error)
8095 /* Try to call the actual copy constructor for a good error. */
8096 call_copy_ctor (a, complain);
8097 return error_mark_node;
8098 }
8099 else
8100 a = convert_arg_to_ellipsis (a, complain);
8101 if (a == error_mark_node)
8102 return error_mark_node;
8103 argarray[j++] = a;
8104 }
8105
8106 gcc_assert (j <= nargs);
8107 nargs = j;
8108
8109 /* Avoid to do argument-transformation, if warnings for format, and for
8110 nonnull are disabled. Just in case that at least one of them is active
8111 the check_function_arguments function might warn about something. */
8112
8113 bool warned_p = false;
8114 if (warn_nonnull
8115 || warn_format
8116 || warn_suggest_attribute_format
8117 || warn_restrict)
8118 {
8119 tree *fargs = (!nargs ? argarray
8120 : (tree *) alloca (nargs * sizeof (tree)));
8121 for (j = 0; j < nargs; j++)
8122 {
8123 /* For -Wformat undo the implicit passing by hidden reference
8124 done by convert_arg_to_ellipsis. */
8125 if (TREE_CODE (argarray[j]) == ADDR_EXPR
8126 && TREE_CODE (TREE_TYPE (argarray[j])) == REFERENCE_TYPE)
8127 fargs[j] = TREE_OPERAND (argarray[j], 0);
8128 else
8129 fargs[j] = maybe_constant_value (argarray[j]);
8130 }
8131
8132 warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
8133 nargs, fargs, NULL);
8134 }
8135
8136 if (DECL_INHERITED_CTOR (fn))
8137 {
8138 /* Check for passing ellipsis arguments to an inherited constructor. We
8139 could handle this by open-coding the inherited constructor rather than
8140 defining it, but let's not bother now. */
8141 if (!cp_unevaluated_operand
8142 && cand->num_convs
8143 && cand->convs[cand->num_convs-1]->ellipsis_p)
8144 {
8145 if (complain & tf_error)
8146 {
8147 sorry ("passing arguments to ellipsis of inherited constructor "
8148 "%qD", cand->fn);
8149 inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
8150 }
8151 return error_mark_node;
8152 }
8153
8154 /* A base constructor inheriting from a virtual base doesn't get the
8155 inherited arguments, just this and __vtt. */
8156 if (ctor_omit_inherited_parms (fn))
8157 nargs = 2;
8158 }
8159
8160 /* Avoid actually calling copy constructors and copy assignment operators,
8161 if possible. */
8162
8163 if (! flag_elide_constructors && !force_elide)
8164 /* Do things the hard way. */;
8165 else if (cand->num_convs == 1
8166 && (DECL_COPY_CONSTRUCTOR_P (fn)
8167 || DECL_MOVE_CONSTRUCTOR_P (fn))
8168 /* It's unsafe to elide the constructor when handling
8169 a noexcept-expression, it may evaluate to the wrong
8170 value (c++/53025). */
8171 && (force_elide || cp_noexcept_operand == 0))
8172 {
8173 tree targ;
8174 tree arg = argarray[num_artificial_parms_for (fn)];
8175 tree fa;
8176 bool trivial = trivial_fn_p (fn);
8177
8178 /* Pull out the real argument, disregarding const-correctness. */
8179 targ = arg;
8180 /* Strip the reference binding for the constructor parameter. */
8181 if (CONVERT_EXPR_P (targ)
8182 && TREE_CODE (TREE_TYPE (targ)) == REFERENCE_TYPE)
8183 targ = TREE_OPERAND (targ, 0);
8184 /* But don't strip any other reference bindings; binding a temporary to a
8185 reference prevents copy elision. */
8186 while ((CONVERT_EXPR_P (targ)
8187 && TREE_CODE (TREE_TYPE (targ)) != REFERENCE_TYPE)
8188 || TREE_CODE (targ) == NON_LVALUE_EXPR)
8189 targ = TREE_OPERAND (targ, 0);
8190 if (TREE_CODE (targ) == ADDR_EXPR)
8191 {
8192 targ = TREE_OPERAND (targ, 0);
8193 if (!same_type_ignoring_top_level_qualifiers_p
8194 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
8195 targ = NULL_TREE;
8196 }
8197 else
8198 targ = NULL_TREE;
8199
8200 if (targ)
8201 arg = targ;
8202 else
8203 arg = cp_build_fold_indirect_ref (arg);
8204
8205 /* In C++17 we shouldn't be copying a TARGET_EXPR except into a base
8206 subobject. */
8207 if (CHECKING_P && cxx_dialect >= cxx17)
8208 gcc_assert (TREE_CODE (arg) != TARGET_EXPR
8209 || force_elide
8210 /* It's from binding the ref parm to a packed field. */
8211 || convs[0]->need_temporary_p
8212 || seen_error ()
8213 /* See unsafe_copy_elision_p. */
8214 || DECL_BASE_CONSTRUCTOR_P (fn));
8215
8216 /* [class.copy]: the copy constructor is implicitly defined even if
8217 the implementation elided its use. */
8218 if (!trivial && !force_elide)
8219 {
8220 if (!mark_used (fn, complain) && !(complain & tf_error))
8221 return error_mark_node;
8222 already_used = true;
8223 }
8224
8225 /* If we're creating a temp and we already have one, don't create a
8226 new one. If we're not creating a temp but we get one, use
8227 INIT_EXPR to collapse the temp into our target. Otherwise, if the
8228 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
8229 temp or an INIT_EXPR otherwise. */
8230 fa = argarray[0];
8231 if (is_dummy_object (fa))
8232 {
8233 if (TREE_CODE (arg) == TARGET_EXPR)
8234 return arg;
8235 else if (trivial)
8236 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
8237 }
8238 else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
8239 && !unsafe_copy_elision_p (fa, arg))
8240 {
8241 tree to = cp_stabilize_reference (cp_build_fold_indirect_ref (fa));
8242
8243 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
8244 return val;
8245 }
8246 }
8247 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
8248 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
8249 && trivial_fn_p (fn))
8250 {
8251 tree to = cp_stabilize_reference
8252 (cp_build_fold_indirect_ref (argarray[0]));
8253 tree type = TREE_TYPE (to);
8254 tree as_base = CLASSTYPE_AS_BASE (type);
8255 tree arg = argarray[1];
8256
8257 if (is_really_empty_class (type))
8258 {
8259 /* Avoid copying empty classes. */
8260 val = build2 (COMPOUND_EXPR, type, arg, to);
8261 TREE_NO_WARNING (val) = 1;
8262 }
8263 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
8264 {
8265 arg = cp_build_fold_indirect_ref (arg);
8266 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
8267 }
8268 else
8269 {
8270 /* We must only copy the non-tail padding parts. */
8271 tree arg0, arg2, t;
8272 tree array_type, alias_set;
8273
8274 arg2 = TYPE_SIZE_UNIT (as_base);
8275 arg0 = cp_build_addr_expr (to, complain);
8276
8277 array_type = build_array_type (unsigned_char_type_node,
8278 build_index_type
8279 (size_binop (MINUS_EXPR,
8280 arg2, size_int (1))));
8281 alias_set = build_int_cst (build_pointer_type (type), 0);
8282 t = build2 (MODIFY_EXPR, void_type_node,
8283 build2 (MEM_REF, array_type, arg0, alias_set),
8284 build2 (MEM_REF, array_type, arg, alias_set));
8285 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
8286 TREE_NO_WARNING (val) = 1;
8287 }
8288
8289 return val;
8290 }
8291 else if (trivial_fn_p (fn))
8292 {
8293 if (DECL_DESTRUCTOR_P (fn))
8294 return fold_convert (void_type_node, argarray[0]);
8295 else if (default_ctor_p (fn))
8296 {
8297 if (is_dummy_object (argarray[0]))
8298 return force_target_expr (DECL_CONTEXT (fn), void_node,
8299 no_cleanup_complain);
8300 else
8301 return cp_build_fold_indirect_ref (argarray[0]);
8302 }
8303 }
8304
8305 gcc_assert (!force_elide);
8306
8307 if (!already_used
8308 && !mark_used (fn, complain))
8309 return error_mark_node;
8310
8311 /* Warn if the built-in writes to an object of a non-trivial type. */
8312 if (warn_class_memaccess
8313 && vec_safe_length (args) >= 2
8314 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8315 maybe_warn_class_memaccess (input_location, fn, args);
8316
8317 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
8318 /* Don't mess with virtual lookup in instantiate_non_dependent_expr;
8319 virtual functions can't be constexpr. */
8320 && !in_template_function ())
8321 {
8322 tree t;
8323 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
8324 DECL_CONTEXT (fn),
8325 ba_any, NULL, complain);
8326 gcc_assert (binfo && binfo != error_mark_node);
8327
8328 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
8329 complain);
8330 if (TREE_SIDE_EFFECTS (argarray[0]))
8331 argarray[0] = save_expr (argarray[0]);
8332 t = build_pointer_type (TREE_TYPE (fn));
8333 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
8334 TREE_TYPE (fn) = t;
8335 }
8336 else
8337 {
8338 fn = build_addr_func (fn, complain);
8339 if (fn == error_mark_node)
8340 return error_mark_node;
8341 }
8342
8343 tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
8344 if (call == error_mark_node)
8345 return call;
8346 if (cand->flags & LOOKUP_LIST_INIT_CTOR)
8347 {
8348 tree c = extract_call_expr (call);
8349 /* build_new_op_1 will clear this when appropriate. */
8350 CALL_EXPR_ORDERED_ARGS (c) = true;
8351 }
8352 if (warned_p)
8353 {
8354 tree c = extract_call_expr (call);
8355 if (TREE_CODE (c) == CALL_EXPR)
8356 TREE_NO_WARNING (c) = 1;
8357 }
8358 return call;
8359 }
8360
8361 namespace
8362 {
8363
8364 /* Return the DECL of the first non-static subobject of class TYPE
8365 that satisfies the predicate PRED or null if none can be found. */
8366
8367 template <class Predicate>
8368 tree
first_non_static_field(tree type,Predicate pred)8369 first_non_static_field (tree type, Predicate pred)
8370 {
8371 if (!type || !CLASS_TYPE_P (type))
8372 return NULL_TREE;
8373
8374 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8375 {
8376 if (TREE_CODE (field) != FIELD_DECL)
8377 continue;
8378 if (TREE_STATIC (field))
8379 continue;
8380 if (pred (field))
8381 return field;
8382 }
8383
8384 int i = 0;
8385
8386 for (tree base_binfo, binfo = TYPE_BINFO (type);
8387 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8388 {
8389 tree base = TREE_TYPE (base_binfo);
8390 if (pred (base))
8391 return base;
8392 if (tree field = first_non_static_field (base, pred))
8393 return field;
8394 }
8395
8396 return NULL_TREE;
8397 }
8398
8399 struct NonPublicField
8400 {
operatorNonPublicField8401 bool operator() (const_tree t)
8402 {
8403 return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t));
8404 }
8405 };
8406
8407 /* Return the DECL of the first non-public subobject of class TYPE
8408 or null if none can be found. */
8409
8410 static inline tree
first_non_public_field(tree type)8411 first_non_public_field (tree type)
8412 {
8413 return first_non_static_field (type, NonPublicField ());
8414 }
8415
8416 struct NonTrivialField
8417 {
operatorNonTrivialField8418 bool operator() (const_tree t)
8419 {
8420 return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t);
8421 }
8422 };
8423
8424 /* Return the DECL of the first non-trivial subobject of class TYPE
8425 or null if none can be found. */
8426
8427 static inline tree
first_non_trivial_field(tree type)8428 first_non_trivial_field (tree type)
8429 {
8430 return first_non_static_field (type, NonTrivialField ());
8431 }
8432
8433 } /* unnamed namespace */
8434
8435 /* Return true if all copy and move assignment operator overloads for
8436 class TYPE are trivial and at least one of them is not deleted and,
8437 when ACCESS is set, accessible. Return false otherwise. Set
8438 HASASSIGN to true when the TYPE has a (not necessarily trivial)
8439 copy or move assignment. */
8440
8441 static bool
has_trivial_copy_assign_p(tree type,bool access,bool * hasassign)8442 has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
8443 {
8444 tree fns = get_class_binding (type, assign_op_identifier);
8445 bool all_trivial = true;
8446
8447 /* Iterate over overloads of the assignment operator, checking
8448 accessible copy assignments for triviality. */
8449
8450 for (ovl_iterator oi (fns); oi; ++oi)
8451 {
8452 tree f = *oi;
8453
8454 /* Skip operators that aren't copy assignments. */
8455 if (!copy_fn_p (f))
8456 continue;
8457
8458 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
8459 || accessible_p (TYPE_BINFO (type), f, true));
8460
8461 /* Skip template assignment operators and deleted functions. */
8462 if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
8463 continue;
8464
8465 if (accessible)
8466 *hasassign = true;
8467
8468 if (!accessible || !trivial_fn_p (f))
8469 all_trivial = false;
8470
8471 /* Break early when both properties have been determined. */
8472 if (*hasassign && !all_trivial)
8473 break;
8474 }
8475
8476 /* Return true if they're all trivial and one of the expressions
8477 TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
8478 tree ref = cp_build_reference_type (type, false);
8479 return (all_trivial
8480 && (is_trivially_xible (MODIFY_EXPR, type, type)
8481 || is_trivially_xible (MODIFY_EXPR, type, ref)));
8482 }
8483
8484 /* Return true if all copy and move ctor overloads for class TYPE are
8485 trivial and at least one of them is not deleted and, when ACCESS is
8486 set, accessible. Return false otherwise. Set each element of HASCTOR[]
8487 to true when the TYPE has a (not necessarily trivial) default and copy
8488 (or move) ctor, respectively. */
8489
8490 static bool
has_trivial_copy_p(tree type,bool access,bool hasctor[2])8491 has_trivial_copy_p (tree type, bool access, bool hasctor[2])
8492 {
8493 tree fns = get_class_binding (type, complete_ctor_identifier);
8494 bool all_trivial = true;
8495
8496 for (ovl_iterator oi (fns); oi; ++oi)
8497 {
8498 tree f = *oi;
8499
8500 /* Skip template constructors. */
8501 if (TREE_CODE (f) != FUNCTION_DECL)
8502 continue;
8503
8504 bool cpy_or_move_ctor_p = copy_fn_p (f);
8505
8506 /* Skip ctors other than default, copy, and move. */
8507 if (!cpy_or_move_ctor_p && !default_ctor_p (f))
8508 continue;
8509
8510 if (DECL_DELETED_FN (f))
8511 continue;
8512
8513 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
8514 || accessible_p (TYPE_BINFO (type), f, true));
8515
8516 if (accessible)
8517 hasctor[cpy_or_move_ctor_p] = true;
8518
8519 if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
8520 all_trivial = false;
8521
8522 /* Break early when both properties have been determined. */
8523 if (hasctor[0] && hasctor[1] && !all_trivial)
8524 break;
8525 }
8526
8527 return all_trivial;
8528 }
8529
8530 /* Issue a warning on a call to the built-in function FNDECL if it is
8531 a raw memory write whose destination is not an object of (something
8532 like) trivial or standard layout type with a non-deleted assignment
8533 and copy ctor. Detects const correctness violations, corrupting
8534 references, virtual table pointers, and bypassing non-trivial
8535 assignments. */
8536
8537 static void
maybe_warn_class_memaccess(location_t loc,tree fndecl,const vec<tree,va_gc> * args)8538 maybe_warn_class_memaccess (location_t loc, tree fndecl,
8539 const vec<tree, va_gc> *args)
8540 {
8541 /* Except for bcopy where it's second, the destination pointer is
8542 the first argument for all functions handled here. Compute
8543 the index of the destination and source arguments. */
8544 unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
8545 unsigned srcidx = !dstidx;
8546
8547 tree dest = (*args)[dstidx];
8548 if (!TREE_TYPE (dest) || !POINTER_TYPE_P (TREE_TYPE (dest)))
8549 return;
8550
8551 tree srctype = NULL_TREE;
8552
8553 /* Determine the type of the pointed-to object and whether it's
8554 a complete class type. */
8555 tree desttype = TREE_TYPE (TREE_TYPE (dest));
8556
8557 if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
8558 return;
8559
8560 /* Check to see if the raw memory call is made by a non-static member
8561 function with THIS as the destination argument for the destination
8562 type. If so, and if the class has no non-trivial bases or members,
8563 be more permissive. */
8564 if (current_function_decl
8565 && DECL_NONSTATIC_MEMBER_FUNCTION_P (current_function_decl)
8566 && is_this_parameter (tree_strip_nop_conversions (dest)))
8567 {
8568 tree ctx = DECL_CONTEXT (current_function_decl);
8569 bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
8570 tree binfo = TYPE_BINFO (ctx);
8571
8572 /* FIXME: The following if statement is overly permissive (see
8573 bug 84851). Remove it in GCC 9. */
8574 if (special
8575 && !BINFO_VTABLE (binfo)
8576 && !BINFO_N_BASE_BINFOS (binfo)
8577 && (DECL_CONSTRUCTOR_P (current_function_decl)
8578 || DECL_DESTRUCTOR_P (current_function_decl)))
8579 return;
8580
8581 if (special
8582 && !BINFO_VTABLE (binfo)
8583 && !first_non_trivial_field (desttype))
8584 return;
8585 }
8586
8587 /* True if the class is trivial. */
8588 bool trivial = trivial_type_p (desttype);
8589
8590 /* Set to true if DESTYPE has an accessible copy assignment. */
8591 bool hasassign = false;
8592 /* True if all of the class' overloaded copy assignment operators
8593 are all trivial (and not deleted) and at least one of them is
8594 accessible. */
8595 bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
8596
8597 /* Set to true if DESTTYPE has an accessible default and copy ctor,
8598 respectively. */
8599 bool hasctors[2] = { false, false };
8600
8601 /* True if all of the class' overloaded copy constructors are all
8602 trivial (and not deleted) and at least one of them is accessible. */
8603 bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
8604
8605 /* Set FLD to the first private/protected member of the class. */
8606 tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
8607
8608 /* The warning format string. */
8609 const char *warnfmt = NULL;
8610 /* A suggested alternative to offer instead of the raw memory call.
8611 Empty string when none can be come up with. */
8612 const char *suggest = "";
8613 bool warned = false;
8614
8615 switch (DECL_FUNCTION_CODE (fndecl))
8616 {
8617 case BUILT_IN_MEMSET:
8618 if (!integer_zerop (maybe_constant_value ((*args)[1])))
8619 {
8620 /* Diagnose setting non-copy-assignable or non-trivial types,
8621 or types with a private member, to (potentially) non-zero
8622 bytes. Since the value of the bytes being written is unknown,
8623 suggest using assignment instead (if one exists). Also warn
8624 for writes into objects for which zero-initialization doesn't
8625 mean all bits clear (pointer-to-member data, where null is all
8626 bits set). Since the value being written is (most likely)
8627 non-zero, simply suggest assignment (but not copy assignment). */
8628 suggest = "; use assignment instead";
8629 if (!trivassign)
8630 warnfmt = G_("%qD writing to an object of type %#qT with "
8631 "no trivial copy-assignment");
8632 else if (!trivial)
8633 warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
8634 else if (fld)
8635 {
8636 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
8637 warned = warning_at (loc, OPT_Wclass_memaccess,
8638 "%qD writing to an object of type %#qT with "
8639 "%qs member %qD",
8640 fndecl, desttype, access, fld);
8641 }
8642 else if (!zero_init_p (desttype))
8643 warnfmt = G_("%qD writing to an object of type %#qT containing "
8644 "a pointer to data member%s");
8645
8646 break;
8647 }
8648 /* Fall through. */
8649
8650 case BUILT_IN_BZERO:
8651 /* Similarly to the above, diagnose clearing non-trivial or non-
8652 standard layout objects, or objects of types with no assignmenmt.
8653 Since the value being written is known to be zero, suggest either
8654 copy assignment, copy ctor, or default ctor as an alternative,
8655 depending on what's available. */
8656
8657 if (hasassign && hasctors[0])
8658 suggest = G_("; use assignment or value-initialization instead");
8659 else if (hasassign)
8660 suggest = G_("; use assignment instead");
8661 else if (hasctors[0])
8662 suggest = G_("; use value-initialization instead");
8663
8664 if (!trivassign)
8665 warnfmt = G_("%qD clearing an object of type %#qT with "
8666 "no trivial copy-assignment%s");
8667 else if (!trivial)
8668 warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
8669 else if (!zero_init_p (desttype))
8670 warnfmt = G_("%qD clearing an object of type %#qT containing "
8671 "a pointer-to-member%s");
8672 break;
8673
8674 case BUILT_IN_BCOPY:
8675 case BUILT_IN_MEMCPY:
8676 case BUILT_IN_MEMMOVE:
8677 case BUILT_IN_MEMPCPY:
8678 /* Determine the type of the source object. */
8679 srctype = TREE_TYPE ((*args)[srcidx]);
8680 if (!srctype || !POINTER_TYPE_P (srctype))
8681 srctype = void_type_node;
8682 else
8683 srctype = TREE_TYPE (srctype);
8684
8685 /* Since it's impossible to determine wheter the byte copy is
8686 being used in place of assignment to an existing object or
8687 as a substitute for initialization, assume it's the former.
8688 Determine the best alternative to use instead depending on
8689 what's not deleted. */
8690 if (hasassign && hasctors[1])
8691 suggest = G_("; use copy-assignment or copy-initialization instead");
8692 else if (hasassign)
8693 suggest = G_("; use copy-assignment instead");
8694 else if (hasctors[1])
8695 suggest = G_("; use copy-initialization instead");
8696
8697 if (!trivassign)
8698 warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
8699 "copy-assignment%s");
8700 else if (!trivially_copyable_p (desttype))
8701 warnfmt = G_("%qD writing to an object of non-trivially copyable "
8702 "type %#qT%s");
8703 else if (!trivcopy)
8704 warnfmt = G_("%qD writing to an object with a deleted copy constructor");
8705
8706 else if (!trivial
8707 && !VOID_TYPE_P (srctype)
8708 && !char_type_p (TYPE_MAIN_VARIANT (srctype))
8709 && !same_type_ignoring_top_level_qualifiers_p (desttype,
8710 srctype))
8711 {
8712 /* Warn when copying into a non-trivial object from an object
8713 of a different type other than void or char. */
8714 warned = warning_at (loc, OPT_Wclass_memaccess,
8715 "%qD copying an object of non-trivial type "
8716 "%#qT from an array of %#qT",
8717 fndecl, desttype, srctype);
8718 }
8719 else if (fld
8720 && !VOID_TYPE_P (srctype)
8721 && !char_type_p (TYPE_MAIN_VARIANT (srctype))
8722 && !same_type_ignoring_top_level_qualifiers_p (desttype,
8723 srctype))
8724 {
8725 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
8726 warned = warning_at (loc, OPT_Wclass_memaccess,
8727 "%qD copying an object of type %#qT with "
8728 "%qs member %qD from an array of %#qT; use "
8729 "assignment or copy-initialization instead",
8730 fndecl, desttype, access, fld, srctype);
8731 }
8732 else if (!trivial && vec_safe_length (args) > 2)
8733 {
8734 tree sz = maybe_constant_value ((*args)[2]);
8735 if (!tree_fits_uhwi_p (sz))
8736 break;
8737
8738 /* Finally, warn on partial copies. */
8739 unsigned HOST_WIDE_INT typesize
8740 = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
8741 if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
8742 warned = warning_at (loc, OPT_Wclass_memaccess,
8743 (typesize - partial > 1
8744 ? G_("%qD writing to an object of "
8745 "a non-trivial type %#qT leaves %wu "
8746 "bytes unchanged")
8747 : G_("%qD writing to an object of "
8748 "a non-trivial type %#qT leaves %wu "
8749 "byte unchanged")),
8750 fndecl, desttype, typesize - partial);
8751 }
8752 break;
8753
8754 case BUILT_IN_REALLOC:
8755
8756 if (!trivially_copyable_p (desttype))
8757 warnfmt = G_("%qD moving an object of non-trivially copyable type "
8758 "%#qT; use %<new%> and %<delete%> instead");
8759 else if (!trivcopy)
8760 warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
8761 "constructor; use %<new%> and %<delete%> instead");
8762 else if (!get_dtor (desttype, tf_none))
8763 warnfmt = G_("%qD moving an object of type %#qT with deleted "
8764 "destructor");
8765 else if (!trivial)
8766 {
8767 tree sz = maybe_constant_value ((*args)[1]);
8768 if (TREE_CODE (sz) == INTEGER_CST
8769 && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype)))
8770 /* Finally, warn on reallocation into insufficient space. */
8771 warned = warning_at (loc, OPT_Wclass_memaccess,
8772 "%qD moving an object of non-trivial type "
8773 "%#qT and size %E into a region of size %E",
8774 fndecl, desttype, TYPE_SIZE_UNIT (desttype),
8775 sz);
8776 }
8777 break;
8778
8779 default:
8780 return;
8781 }
8782
8783 if (warnfmt)
8784 {
8785 if (suggest)
8786 warned = warning_at (loc, OPT_Wclass_memaccess,
8787 warnfmt, fndecl, desttype, suggest);
8788 else
8789 warned = warning_at (loc, OPT_Wclass_memaccess,
8790 warnfmt, fndecl, desttype);
8791 }
8792
8793 if (warned)
8794 inform (location_of (desttype), "%#qT declared here", desttype);
8795 }
8796
8797 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
8798 This function performs no overload resolution, conversion, or other
8799 high-level operations. */
8800
8801 tree
build_cxx_call(tree fn,int nargs,tree * argarray,tsubst_flags_t complain)8802 build_cxx_call (tree fn, int nargs, tree *argarray,
8803 tsubst_flags_t complain)
8804 {
8805 tree fndecl;
8806
8807 /* Remember roughly where this call is. */
8808 location_t loc = EXPR_LOC_OR_LOC (fn, input_location);
8809 fn = build_call_a (fn, nargs, argarray);
8810 SET_EXPR_LOCATION (fn, loc);
8811
8812 fndecl = get_callee_fndecl (fn);
8813
8814 /* Check that arguments to builtin functions match the expectations. */
8815 if (fndecl
8816 && DECL_BUILT_IN (fndecl)
8817 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
8818 {
8819 int i;
8820
8821 /* We need to take care that values to BUILT_IN_NORMAL
8822 are reduced. */
8823 for (i = 0; i < nargs; i++)
8824 argarray[i] = fold_non_dependent_expr (argarray[i]);
8825
8826 if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
8827 nargs, argarray))
8828 return error_mark_node;
8829 }
8830
8831 if (VOID_TYPE_P (TREE_TYPE (fn)))
8832 return fn;
8833
8834 /* 5.2.2/11: If a function call is a prvalue of object type: if the
8835 function call is either the operand of a decltype-specifier or the
8836 right operand of a comma operator that is the operand of a
8837 decltype-specifier, a temporary object is not introduced for the
8838 prvalue. The type of the prvalue may be incomplete. */
8839 if (!(complain & tf_decltype))
8840 {
8841 fn = require_complete_type_sfinae (fn, complain);
8842 if (fn == error_mark_node)
8843 return error_mark_node;
8844
8845 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
8846 {
8847 fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
8848 maybe_warn_parm_abi (TREE_TYPE (fn), loc);
8849 }
8850 }
8851 return convert_from_reference (fn);
8852 }
8853
8854 /* Returns the value to use for the in-charge parameter when making a
8855 call to a function with the indicated NAME.
8856
8857 FIXME:Can't we find a neater way to do this mapping? */
8858
8859 tree
in_charge_arg_for_name(tree name)8860 in_charge_arg_for_name (tree name)
8861 {
8862 if (IDENTIFIER_CTOR_P (name))
8863 {
8864 if (name == complete_ctor_identifier)
8865 return integer_one_node;
8866 gcc_checking_assert (name == base_ctor_identifier);
8867 }
8868 else
8869 {
8870 if (name == complete_dtor_identifier)
8871 return integer_two_node;
8872 else if (name == deleting_dtor_identifier)
8873 return integer_three_node;
8874 gcc_checking_assert (name == base_dtor_identifier);
8875 }
8876
8877 return integer_zero_node;
8878 }
8879
8880 /* We've built up a constructor call RET. Complain if it delegates to the
8881 constructor we're currently compiling. */
8882
8883 static void
check_self_delegation(tree ret)8884 check_self_delegation (tree ret)
8885 {
8886 if (TREE_CODE (ret) == TARGET_EXPR)
8887 ret = TARGET_EXPR_INITIAL (ret);
8888 tree fn = cp_get_callee_fndecl_nofold (ret);
8889 if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
8890 error ("constructor delegates to itself");
8891 }
8892
8893 /* Build a call to a constructor, destructor, or an assignment
8894 operator for INSTANCE, an expression with class type. NAME
8895 indicates the special member function to call; *ARGS are the
8896 arguments. ARGS may be NULL. This may change ARGS. BINFO
8897 indicates the base of INSTANCE that is to be passed as the `this'
8898 parameter to the member function called.
8899
8900 FLAGS are the LOOKUP_* flags to use when processing the call.
8901
8902 If NAME indicates a complete object constructor, INSTANCE may be
8903 NULL_TREE. In this case, the caller will call build_cplus_new to
8904 store the newly constructed object into a VAR_DECL. */
8905
8906 tree
build_special_member_call(tree instance,tree name,vec<tree,va_gc> ** args,tree binfo,int flags,tsubst_flags_t complain)8907 build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
8908 tree binfo, int flags, tsubst_flags_t complain)
8909 {
8910 tree fns;
8911 /* The type of the subobject to be constructed or destroyed. */
8912 tree class_type;
8913 vec<tree, va_gc> *allocated = NULL;
8914 tree ret;
8915
8916 gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
8917 if (TYPE_P (binfo))
8918 {
8919 /* Resolve the name. */
8920 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
8921 return error_mark_node;
8922
8923 binfo = TYPE_BINFO (binfo);
8924 }
8925
8926 gcc_assert (binfo != NULL_TREE);
8927
8928 class_type = BINFO_TYPE (binfo);
8929
8930 /* Handle the special case where INSTANCE is NULL_TREE. */
8931 if (name == complete_ctor_identifier && !instance)
8932 instance = build_dummy_object (class_type);
8933 else
8934 {
8935 if (IDENTIFIER_DTOR_P (name))
8936 gcc_assert (args == NULL || vec_safe_is_empty (*args));
8937
8938 /* Convert to the base class, if necessary. */
8939 if (!same_type_ignoring_top_level_qualifiers_p
8940 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
8941 {
8942 if (IDENTIFIER_CDTOR_P (name))
8943 /* For constructors and destructors, either the base is
8944 non-virtual, or it is virtual but we are doing the
8945 conversion from a constructor or destructor for the
8946 complete object. In either case, we can convert
8947 statically. */
8948 instance = convert_to_base_statically (instance, binfo);
8949 else
8950 {
8951 /* However, for assignment operators, we must convert
8952 dynamically if the base is virtual. */
8953 gcc_checking_assert (name == assign_op_identifier);
8954 instance = build_base_path (PLUS_EXPR, instance,
8955 binfo, /*nonnull=*/1, complain);
8956 }
8957 }
8958 }
8959
8960 gcc_assert (instance != NULL_TREE);
8961
8962 /* In C++17, "If the initializer expression is a prvalue and the
8963 cv-unqualified version of the source type is the same class as the class
8964 of the destination, the initializer expression is used to initialize the
8965 destination object." Handle that here to avoid doing overload
8966 resolution. */
8967 if (cxx_dialect >= cxx17
8968 && args && vec_safe_length (*args) == 1
8969 && name == complete_ctor_identifier)
8970 {
8971 tree arg = (**args)[0];
8972
8973 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
8974 && !TYPE_HAS_LIST_CTOR (class_type)
8975 && CONSTRUCTOR_NELTS (arg) == 1)
8976 arg = CONSTRUCTOR_ELT (arg, 0)->value;
8977
8978 if ((TREE_CODE (arg) == TARGET_EXPR
8979 || TREE_CODE (arg) == CONSTRUCTOR)
8980 && (same_type_ignoring_top_level_qualifiers_p
8981 (class_type, TREE_TYPE (arg))))
8982 {
8983 if (is_dummy_object (instance))
8984 return arg;
8985 else if (TREE_CODE (arg) == TARGET_EXPR)
8986 TARGET_EXPR_DIRECT_INIT_P (arg) = true;
8987
8988 if ((complain & tf_error)
8989 && (flags & LOOKUP_DELEGATING_CONS))
8990 check_self_delegation (arg);
8991 /* Avoid change of behavior on Wunused-var-2.C. */
8992 instance = mark_lvalue_use (instance);
8993 return build2 (INIT_EXPR, class_type, instance, arg);
8994 }
8995 }
8996
8997 fns = lookup_fnfields (binfo, name, 1);
8998
8999 /* When making a call to a constructor or destructor for a subobject
9000 that uses virtual base classes, pass down a pointer to a VTT for
9001 the subobject. */
9002 if ((name == base_ctor_identifier
9003 || name == base_dtor_identifier)
9004 && CLASSTYPE_VBASECLASSES (class_type))
9005 {
9006 tree vtt;
9007 tree sub_vtt;
9008
9009 /* If the current function is a complete object constructor
9010 or destructor, then we fetch the VTT directly.
9011 Otherwise, we look it up using the VTT we were given. */
9012 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
9013 vtt = decay_conversion (vtt, complain);
9014 if (vtt == error_mark_node)
9015 return error_mark_node;
9016 vtt = build_if_in_charge (vtt, current_vtt_parm);
9017 if (BINFO_SUBVTT_INDEX (binfo))
9018 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
9019 else
9020 sub_vtt = vtt;
9021
9022 if (args == NULL)
9023 {
9024 allocated = make_tree_vector ();
9025 args = &allocated;
9026 }
9027
9028 vec_safe_insert (*args, 0, sub_vtt);
9029 }
9030
9031 ret = build_new_method_call (instance, fns, args,
9032 TYPE_BINFO (BINFO_TYPE (binfo)),
9033 flags, /*fn=*/NULL,
9034 complain);
9035
9036 if (allocated != NULL)
9037 release_tree_vector (allocated);
9038
9039 if ((complain & tf_error)
9040 && (flags & LOOKUP_DELEGATING_CONS)
9041 && name == complete_ctor_identifier)
9042 check_self_delegation (ret);
9043
9044 return ret;
9045 }
9046
9047 /* Return the NAME, as a C string. The NAME indicates a function that
9048 is a member of TYPE. *FREE_P is set to true if the caller must
9049 free the memory returned.
9050
9051 Rather than go through all of this, we should simply set the names
9052 of constructors and destructors appropriately, and dispense with
9053 ctor_identifier, dtor_identifier, etc. */
9054
9055 static char *
name_as_c_string(tree name,tree type,bool * free_p)9056 name_as_c_string (tree name, tree type, bool *free_p)
9057 {
9058 const char *pretty_name;
9059
9060 /* Assume that we will not allocate memory. */
9061 *free_p = false;
9062 /* Constructors and destructors are special. */
9063 if (IDENTIFIER_CDTOR_P (name))
9064 {
9065 pretty_name
9066 = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
9067 /* For a destructor, add the '~'. */
9068 if (IDENTIFIER_DTOR_P (name))
9069 {
9070 pretty_name = concat ("~", pretty_name, NULL);
9071 /* Remember that we need to free the memory allocated. */
9072 *free_p = true;
9073 }
9074 }
9075 else if (IDENTIFIER_CONV_OP_P (name))
9076 {
9077 pretty_name = concat ("operator ",
9078 type_as_string_translate (TREE_TYPE (name),
9079 TFF_PLAIN_IDENTIFIER),
9080 NULL);
9081 /* Remember that we need to free the memory allocated. */
9082 *free_p = true;
9083 }
9084 else
9085 pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
9086
9087 return CONST_CAST (char *, pretty_name);
9088 }
9089
9090 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
9091 be set, upon return, to the function called. ARGS may be NULL.
9092 This may change ARGS. */
9093
9094 static tree
build_new_method_call_1(tree instance,tree fns,vec<tree,va_gc> ** args,tree conversion_path,int flags,tree * fn_p,tsubst_flags_t complain)9095 build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
9096 tree conversion_path, int flags,
9097 tree *fn_p, tsubst_flags_t complain)
9098 {
9099 struct z_candidate *candidates = 0, *cand;
9100 tree explicit_targs = NULL_TREE;
9101 tree basetype = NULL_TREE;
9102 tree access_binfo, binfo;
9103 tree optype;
9104 tree first_mem_arg = NULL_TREE;
9105 tree name;
9106 bool skip_first_for_error;
9107 vec<tree, va_gc> *user_args;
9108 tree call;
9109 tree fn;
9110 int template_only = 0;
9111 bool any_viable_p;
9112 tree orig_instance;
9113 tree orig_fns;
9114 vec<tree, va_gc> *orig_args = NULL;
9115 void *p;
9116
9117 gcc_assert (instance != NULL_TREE);
9118
9119 /* We don't know what function we're going to call, yet. */
9120 if (fn_p)
9121 *fn_p = NULL_TREE;
9122
9123 if (error_operand_p (instance)
9124 || !fns || error_operand_p (fns))
9125 return error_mark_node;
9126
9127 if (!BASELINK_P (fns))
9128 {
9129 if (complain & tf_error)
9130 error ("call to non-function %qD", fns);
9131 return error_mark_node;
9132 }
9133
9134 orig_instance = instance;
9135 orig_fns = fns;
9136
9137 /* Dismantle the baselink to collect all the information we need. */
9138 if (!conversion_path)
9139 conversion_path = BASELINK_BINFO (fns);
9140 access_binfo = BASELINK_ACCESS_BINFO (fns);
9141 binfo = BASELINK_BINFO (fns);
9142 optype = BASELINK_OPTYPE (fns);
9143 fns = BASELINK_FUNCTIONS (fns);
9144 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
9145 {
9146 explicit_targs = TREE_OPERAND (fns, 1);
9147 fns = TREE_OPERAND (fns, 0);
9148 template_only = 1;
9149 }
9150 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
9151 || TREE_CODE (fns) == TEMPLATE_DECL
9152 || TREE_CODE (fns) == OVERLOAD);
9153 fn = OVL_FIRST (fns);
9154 name = DECL_NAME (fn);
9155
9156 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
9157 gcc_assert (CLASS_TYPE_P (basetype));
9158
9159 user_args = args == NULL ? NULL : *args;
9160 /* Under DR 147 A::A() is an invalid constructor call,
9161 not a functional cast. */
9162 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
9163 {
9164 if (! (complain & tf_error))
9165 return error_mark_node;
9166
9167 basetype = DECL_CONTEXT (fn);
9168 name = constructor_name (basetype);
9169 if (permerror (input_location,
9170 "cannot call constructor %<%T::%D%> directly",
9171 basetype, name))
9172 inform (input_location, "for a function-style cast, remove the "
9173 "redundant %<::%D%>", name);
9174 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
9175 complain);
9176 return call;
9177 }
9178
9179 if (processing_template_decl)
9180 {
9181 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
9182 instance = build_non_dependent_expr (instance);
9183 if (args != NULL)
9184 make_args_non_dependent (*args);
9185 }
9186
9187 /* Process the argument list. */
9188 if (args != NULL && *args != NULL)
9189 {
9190 *args = resolve_args (*args, complain);
9191 if (*args == NULL)
9192 return error_mark_node;
9193 user_args = *args;
9194 }
9195
9196 /* Consider the object argument to be used even if we end up selecting a
9197 static member function. */
9198 instance = mark_type_use (instance);
9199
9200 /* Figure out whether to skip the first argument for the error
9201 message we will display to users if an error occurs. We don't
9202 want to display any compiler-generated arguments. The "this"
9203 pointer hasn't been added yet. However, we must remove the VTT
9204 pointer if this is a call to a base-class constructor or
9205 destructor. */
9206 skip_first_for_error = false;
9207 if (IDENTIFIER_CDTOR_P (name))
9208 {
9209 /* Callers should explicitly indicate whether they want to ctor
9210 the complete object or just the part without virtual bases. */
9211 gcc_assert (name != ctor_identifier);
9212
9213 /* Remove the VTT pointer, if present. */
9214 if ((name == base_ctor_identifier || name == base_dtor_identifier)
9215 && CLASSTYPE_VBASECLASSES (basetype))
9216 skip_first_for_error = true;
9217
9218 /* It's OK to call destructors and constructors on cv-qualified
9219 objects. Therefore, convert the INSTANCE to the unqualified
9220 type, if necessary. */
9221 if (!same_type_p (basetype, TREE_TYPE (instance)))
9222 {
9223 instance = build_this (instance);
9224 instance = build_nop (build_pointer_type (basetype), instance);
9225 instance = build_fold_indirect_ref (instance);
9226 }
9227 }
9228 else
9229 gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
9230
9231 /* For the overload resolution we need to find the actual `this`
9232 that would be captured if the call turns out to be to a
9233 non-static member function. Do not actually capture it at this
9234 point. */
9235 if (DECL_CONSTRUCTOR_P (fn))
9236 /* Constructors don't use the enclosing 'this'. */
9237 first_mem_arg = instance;
9238 else
9239 first_mem_arg = maybe_resolve_dummy (instance, false);
9240
9241 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9242 p = conversion_obstack_alloc (0);
9243
9244 /* The number of arguments artificial parms in ARGS; we subtract one because
9245 there's no 'this' in ARGS. */
9246 unsigned skip = num_artificial_parms_for (fn) - 1;
9247
9248 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
9249 initializer, not T({ }). */
9250 if (DECL_CONSTRUCTOR_P (fn)
9251 && vec_safe_length (user_args) > skip
9252 && DIRECT_LIST_INIT_P ((*user_args)[skip]))
9253 {
9254 tree init_list = (*user_args)[skip];
9255 tree init = NULL_TREE;
9256
9257 gcc_assert (user_args->length () == skip + 1
9258 && !(flags & LOOKUP_ONLYCONVERTING));
9259
9260 /* If the initializer list has no elements and T is a class type with
9261 a default constructor, the object is value-initialized. Handle
9262 this here so we don't need to handle it wherever we use
9263 build_special_member_call. */
9264 if (CONSTRUCTOR_NELTS (init_list) == 0
9265 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
9266 /* For a user-provided default constructor, use the normal
9267 mechanisms so that protected access works. */
9268 && type_has_non_user_provided_default_constructor (basetype)
9269 && !processing_template_decl)
9270 init = build_value_init (basetype, complain);
9271
9272 /* If BASETYPE is an aggregate, we need to do aggregate
9273 initialization. */
9274 else if (CP_AGGREGATE_TYPE_P (basetype))
9275 {
9276 init = reshape_init (basetype, init_list, complain);
9277 init = digest_init (basetype, init, complain);
9278 }
9279
9280 if (init)
9281 {
9282 if (is_dummy_object (instance))
9283 return get_target_expr_sfinae (init, complain);
9284 init = build2 (INIT_EXPR, TREE_TYPE (instance), instance, init);
9285 TREE_SIDE_EFFECTS (init) = true;
9286 return init;
9287 }
9288
9289 /* Otherwise go ahead with overload resolution. */
9290 add_list_candidates (fns, first_mem_arg, user_args,
9291 basetype, explicit_targs, template_only,
9292 conversion_path, access_binfo, flags,
9293 &candidates, complain);
9294 }
9295 else
9296 add_candidates (fns, first_mem_arg, user_args, optype,
9297 explicit_targs, template_only, conversion_path,
9298 access_binfo, flags, &candidates, complain);
9299
9300 any_viable_p = false;
9301 candidates = splice_viable (candidates, false, &any_viable_p);
9302
9303 if (!any_viable_p)
9304 {
9305 if (complain & tf_error)
9306 {
9307 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
9308 cxx_incomplete_type_error (instance, basetype);
9309 else if (optype)
9310 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
9311 basetype, optype, build_tree_list_vec (user_args),
9312 TREE_TYPE (instance));
9313 else
9314 {
9315 tree arglist = build_tree_list_vec (user_args);
9316 tree errname = name;
9317 bool twiddle = false;
9318 if (IDENTIFIER_CDTOR_P (errname))
9319 {
9320 twiddle = IDENTIFIER_DTOR_P (errname);
9321 errname = constructor_name (basetype);
9322 }
9323 if (explicit_targs)
9324 errname = lookup_template_function (errname, explicit_targs);
9325 if (skip_first_for_error)
9326 arglist = TREE_CHAIN (arglist);
9327 error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
9328 basetype, &"~"[!twiddle], errname, arglist,
9329 TREE_TYPE (instance));
9330 }
9331 print_z_candidates (location_of (name), candidates);
9332 }
9333 call = error_mark_node;
9334 }
9335 else
9336 {
9337 cand = tourney (candidates, complain);
9338 if (cand == 0)
9339 {
9340 char *pretty_name;
9341 bool free_p;
9342 tree arglist;
9343
9344 if (complain & tf_error)
9345 {
9346 pretty_name = name_as_c_string (name, basetype, &free_p);
9347 arglist = build_tree_list_vec (user_args);
9348 if (skip_first_for_error)
9349 arglist = TREE_CHAIN (arglist);
9350 if (!any_strictly_viable (candidates))
9351 error ("no matching function for call to %<%s(%A)%>",
9352 pretty_name, arglist);
9353 else
9354 error ("call of overloaded %<%s(%A)%> is ambiguous",
9355 pretty_name, arglist);
9356 print_z_candidates (location_of (name), candidates);
9357 if (free_p)
9358 free (pretty_name);
9359 }
9360 call = error_mark_node;
9361 }
9362 else
9363 {
9364 fn = cand->fn;
9365 call = NULL_TREE;
9366
9367 if (!(flags & LOOKUP_NONVIRTUAL)
9368 && DECL_PURE_VIRTUAL_P (fn)
9369 && instance == current_class_ref
9370 && (complain & tf_warning))
9371 {
9372 /* This is not an error, it is runtime undefined
9373 behavior. */
9374 if (!current_function_decl)
9375 warning (0, "pure virtual %q#D called from "
9376 "non-static data member initializer", fn);
9377 else if (DECL_CONSTRUCTOR_P (current_function_decl)
9378 || DECL_DESTRUCTOR_P (current_function_decl))
9379 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
9380 ? G_("pure virtual %q#D called from constructor")
9381 : G_("pure virtual %q#D called from destructor")),
9382 fn);
9383 }
9384
9385 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
9386 && !DECL_CONSTRUCTOR_P (fn)
9387 && is_dummy_object (instance))
9388 {
9389 instance = maybe_resolve_dummy (instance, true);
9390 if (instance == error_mark_node)
9391 call = error_mark_node;
9392 else if (!is_dummy_object (instance))
9393 {
9394 /* We captured 'this' in the current lambda now that
9395 we know we really need it. */
9396 cand->first_arg = instance;
9397 }
9398 else if (any_dependent_bases_p ())
9399 /* We can't tell until instantiation time whether we can use
9400 *this as the implicit object argument. */;
9401 else
9402 {
9403 if (complain & tf_error)
9404 error ("cannot call member function %qD without object",
9405 fn);
9406 call = error_mark_node;
9407 }
9408 }
9409
9410 if (call != error_mark_node)
9411 {
9412 if (explicit_targs)
9413 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
9414 /* Now we know what function is being called. */
9415 if (fn_p)
9416 *fn_p = fn;
9417 /* Build the actual CALL_EXPR. */
9418 call = build_over_call (cand, flags, complain);
9419 /* In an expression of the form `a->f()' where `f' turns
9420 out to be a static member function, `a' is
9421 none-the-less evaluated. */
9422 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
9423 && !is_dummy_object (instance)
9424 && TREE_SIDE_EFFECTS (instance))
9425 {
9426 /* But avoid the implicit lvalue-rvalue conversion when 'a'
9427 is volatile. */
9428 tree a = instance;
9429 if (TREE_THIS_VOLATILE (a))
9430 a = build_this (a);
9431 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), a, call);
9432 }
9433 else if (call != error_mark_node
9434 && DECL_DESTRUCTOR_P (cand->fn)
9435 && !VOID_TYPE_P (TREE_TYPE (call)))
9436 /* An explicit call of the form "x->~X()" has type
9437 "void". However, on platforms where destructors
9438 return "this" (i.e., those where
9439 targetm.cxx.cdtor_returns_this is true), such calls
9440 will appear to have a return value of pointer type
9441 to the low-level call machinery. We do not want to
9442 change the low-level machinery, since we want to be
9443 able to optimize "delete f()" on such platforms as
9444 "operator delete(~X(f()))" (rather than generating
9445 "t = f(), ~X(t), operator delete (t)"). */
9446 call = build_nop (void_type_node, call);
9447 }
9448 }
9449 }
9450
9451 if (processing_template_decl && call != error_mark_node)
9452 {
9453 bool cast_to_void = false;
9454
9455 if (TREE_CODE (call) == COMPOUND_EXPR)
9456 call = TREE_OPERAND (call, 1);
9457 else if (TREE_CODE (call) == NOP_EXPR)
9458 {
9459 cast_to_void = true;
9460 call = TREE_OPERAND (call, 0);
9461 }
9462 if (INDIRECT_REF_P (call))
9463 call = TREE_OPERAND (call, 0);
9464 call = (build_min_non_dep_call_vec
9465 (call,
9466 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
9467 orig_instance, orig_fns, NULL_TREE),
9468 orig_args));
9469 SET_EXPR_LOCATION (call, input_location);
9470 call = convert_from_reference (call);
9471 if (cast_to_void)
9472 call = build_nop (void_type_node, call);
9473 }
9474
9475 /* Free all the conversions we allocated. */
9476 obstack_free (&conversion_obstack, p);
9477
9478 if (orig_args != NULL)
9479 release_tree_vector (orig_args);
9480
9481 return call;
9482 }
9483
9484 /* Wrapper for above. */
9485
9486 tree
build_new_method_call(tree instance,tree fns,vec<tree,va_gc> ** args,tree conversion_path,int flags,tree * fn_p,tsubst_flags_t complain)9487 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
9488 tree conversion_path, int flags,
9489 tree *fn_p, tsubst_flags_t complain)
9490 {
9491 tree ret;
9492 bool subtime = timevar_cond_start (TV_OVERLOAD);
9493 ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
9494 fn_p, complain);
9495 timevar_cond_stop (TV_OVERLOAD, subtime);
9496 return ret;
9497 }
9498
9499 /* Returns true iff standard conversion sequence ICS1 is a proper
9500 subsequence of ICS2. */
9501
9502 static bool
is_subseq(conversion * ics1,conversion * ics2)9503 is_subseq (conversion *ics1, conversion *ics2)
9504 {
9505 /* We can assume that a conversion of the same code
9506 between the same types indicates a subsequence since we only get
9507 here if the types we are converting from are the same. */
9508
9509 while (ics1->kind == ck_rvalue
9510 || ics1->kind == ck_lvalue)
9511 ics1 = next_conversion (ics1);
9512
9513 while (1)
9514 {
9515 while (ics2->kind == ck_rvalue
9516 || ics2->kind == ck_lvalue)
9517 ics2 = next_conversion (ics2);
9518
9519 if (ics2->kind == ck_user
9520 || ics2->kind == ck_ambig
9521 || ics2->kind == ck_aggr
9522 || ics2->kind == ck_list
9523 || ics2->kind == ck_identity)
9524 /* At this point, ICS1 cannot be a proper subsequence of
9525 ICS2. We can get a USER_CONV when we are comparing the
9526 second standard conversion sequence of two user conversion
9527 sequences. */
9528 return false;
9529
9530 ics2 = next_conversion (ics2);
9531
9532 while (ics2->kind == ck_rvalue
9533 || ics2->kind == ck_lvalue)
9534 ics2 = next_conversion (ics2);
9535
9536 if (ics2->kind == ics1->kind
9537 && same_type_p (ics2->type, ics1->type)
9538 && (ics1->kind == ck_identity
9539 || same_type_p (next_conversion (ics2)->type,
9540 next_conversion (ics1)->type)))
9541 return true;
9542 }
9543 }
9544
9545 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
9546 be any _TYPE nodes. */
9547
9548 bool
is_properly_derived_from(tree derived,tree base)9549 is_properly_derived_from (tree derived, tree base)
9550 {
9551 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
9552 return false;
9553
9554 /* We only allow proper derivation here. The DERIVED_FROM_P macro
9555 considers every class derived from itself. */
9556 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
9557 && DERIVED_FROM_P (base, derived));
9558 }
9559
9560 /* We build the ICS for an implicit object parameter as a pointer
9561 conversion sequence. However, such a sequence should be compared
9562 as if it were a reference conversion sequence. If ICS is the
9563 implicit conversion sequence for an implicit object parameter,
9564 modify it accordingly. */
9565
9566 static void
maybe_handle_implicit_object(conversion ** ics)9567 maybe_handle_implicit_object (conversion **ics)
9568 {
9569 if ((*ics)->this_p)
9570 {
9571 /* [over.match.funcs]
9572
9573 For non-static member functions, the type of the
9574 implicit object parameter is "reference to cv X"
9575 where X is the class of which the function is a
9576 member and cv is the cv-qualification on the member
9577 function declaration. */
9578 conversion *t = *ics;
9579 tree reference_type;
9580
9581 /* The `this' parameter is a pointer to a class type. Make the
9582 implicit conversion talk about a reference to that same class
9583 type. */
9584 reference_type = TREE_TYPE (t->type);
9585 reference_type = build_reference_type (reference_type);
9586
9587 if (t->kind == ck_qual)
9588 t = next_conversion (t);
9589 if (t->kind == ck_ptr)
9590 t = next_conversion (t);
9591 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
9592 t = direct_reference_binding (reference_type, t);
9593 t->this_p = 1;
9594 t->rvaluedness_matches_p = 0;
9595 *ics = t;
9596 }
9597 }
9598
9599 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
9600 and return the initial reference binding conversion. Otherwise,
9601 leave *ICS unchanged and return NULL. */
9602
9603 static conversion *
maybe_handle_ref_bind(conversion ** ics)9604 maybe_handle_ref_bind (conversion **ics)
9605 {
9606 if ((*ics)->kind == ck_ref_bind)
9607 {
9608 conversion *old_ics = *ics;
9609 *ics = next_conversion (old_ics);
9610 (*ics)->user_conv_p = old_ics->user_conv_p;
9611 return old_ics;
9612 }
9613
9614 return NULL;
9615 }
9616
9617 /* Compare two implicit conversion sequences according to the rules set out in
9618 [over.ics.rank]. Return values:
9619
9620 1: ics1 is better than ics2
9621 -1: ics2 is better than ics1
9622 0: ics1 and ics2 are indistinguishable */
9623
9624 static int
compare_ics(conversion * ics1,conversion * ics2)9625 compare_ics (conversion *ics1, conversion *ics2)
9626 {
9627 tree from_type1;
9628 tree from_type2;
9629 tree to_type1;
9630 tree to_type2;
9631 tree deref_from_type1 = NULL_TREE;
9632 tree deref_from_type2 = NULL_TREE;
9633 tree deref_to_type1 = NULL_TREE;
9634 tree deref_to_type2 = NULL_TREE;
9635 conversion_rank rank1, rank2;
9636
9637 /* REF_BINDING is nonzero if the result of the conversion sequence
9638 is a reference type. In that case REF_CONV is the reference
9639 binding conversion. */
9640 conversion *ref_conv1;
9641 conversion *ref_conv2;
9642
9643 /* Compare badness before stripping the reference conversion. */
9644 if (ics1->bad_p > ics2->bad_p)
9645 return -1;
9646 else if (ics1->bad_p < ics2->bad_p)
9647 return 1;
9648
9649 /* Handle implicit object parameters. */
9650 maybe_handle_implicit_object (&ics1);
9651 maybe_handle_implicit_object (&ics2);
9652
9653 /* Handle reference parameters. */
9654 ref_conv1 = maybe_handle_ref_bind (&ics1);
9655 ref_conv2 = maybe_handle_ref_bind (&ics2);
9656
9657 /* List-initialization sequence L1 is a better conversion sequence than
9658 list-initialization sequence L2 if L1 converts to
9659 std::initializer_list<X> for some X and L2 does not. */
9660 if (ics1->kind == ck_list && ics2->kind != ck_list)
9661 return 1;
9662 if (ics2->kind == ck_list && ics1->kind != ck_list)
9663 return -1;
9664
9665 /* [over.ics.rank]
9666
9667 When comparing the basic forms of implicit conversion sequences (as
9668 defined in _over.best.ics_)
9669
9670 --a standard conversion sequence (_over.ics.scs_) is a better
9671 conversion sequence than a user-defined conversion sequence
9672 or an ellipsis conversion sequence, and
9673
9674 --a user-defined conversion sequence (_over.ics.user_) is a
9675 better conversion sequence than an ellipsis conversion sequence
9676 (_over.ics.ellipsis_). */
9677 /* Use BAD_CONVERSION_RANK because we already checked for a badness
9678 mismatch. If both ICS are bad, we try to make a decision based on
9679 what would have happened if they'd been good. This is not an
9680 extension, we'll still give an error when we build up the call; this
9681 just helps us give a more helpful error message. */
9682 rank1 = BAD_CONVERSION_RANK (ics1);
9683 rank2 = BAD_CONVERSION_RANK (ics2);
9684
9685 if (rank1 > rank2)
9686 return -1;
9687 else if (rank1 < rank2)
9688 return 1;
9689
9690 if (ics1->ellipsis_p)
9691 /* Both conversions are ellipsis conversions. */
9692 return 0;
9693
9694 /* User-defined conversion sequence U1 is a better conversion sequence
9695 than another user-defined conversion sequence U2 if they contain the
9696 same user-defined conversion operator or constructor and if the sec-
9697 ond standard conversion sequence of U1 is better than the second
9698 standard conversion sequence of U2. */
9699
9700 /* Handle list-conversion with the same code even though it isn't always
9701 ranked as a user-defined conversion and it doesn't have a second
9702 standard conversion sequence; it will still have the desired effect.
9703 Specifically, we need to do the reference binding comparison at the
9704 end of this function. */
9705
9706 if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
9707 {
9708 conversion *t1;
9709 conversion *t2;
9710
9711 for (t1 = ics1; t1->kind != ck_user; t1 = next_conversion (t1))
9712 if (t1->kind == ck_ambig || t1->kind == ck_aggr
9713 || t1->kind == ck_list)
9714 break;
9715 for (t2 = ics2; t2->kind != ck_user; t2 = next_conversion (t2))
9716 if (t2->kind == ck_ambig || t2->kind == ck_aggr
9717 || t2->kind == ck_list)
9718 break;
9719
9720 if (t1->kind != t2->kind)
9721 return 0;
9722 else if (t1->kind == ck_user)
9723 {
9724 tree f1 = t1->cand ? t1->cand->fn : t1->type;
9725 tree f2 = t2->cand ? t2->cand->fn : t2->type;
9726 if (f1 != f2)
9727 return 0;
9728 }
9729 else
9730 {
9731 /* For ambiguous or aggregate conversions, use the target type as
9732 a proxy for the conversion function. */
9733 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
9734 return 0;
9735 }
9736
9737 /* We can just fall through here, after setting up
9738 FROM_TYPE1 and FROM_TYPE2. */
9739 from_type1 = t1->type;
9740 from_type2 = t2->type;
9741 }
9742 else
9743 {
9744 conversion *t1;
9745 conversion *t2;
9746
9747 /* We're dealing with two standard conversion sequences.
9748
9749 [over.ics.rank]
9750
9751 Standard conversion sequence S1 is a better conversion
9752 sequence than standard conversion sequence S2 if
9753
9754 --S1 is a proper subsequence of S2 (comparing the conversion
9755 sequences in the canonical form defined by _over.ics.scs_,
9756 excluding any Lvalue Transformation; the identity
9757 conversion sequence is considered to be a subsequence of
9758 any non-identity conversion sequence */
9759
9760 t1 = ics1;
9761 while (t1->kind != ck_identity)
9762 t1 = next_conversion (t1);
9763 from_type1 = t1->type;
9764
9765 t2 = ics2;
9766 while (t2->kind != ck_identity)
9767 t2 = next_conversion (t2);
9768 from_type2 = t2->type;
9769 }
9770
9771 /* One sequence can only be a subsequence of the other if they start with
9772 the same type. They can start with different types when comparing the
9773 second standard conversion sequence in two user-defined conversion
9774 sequences. */
9775 if (same_type_p (from_type1, from_type2))
9776 {
9777 if (is_subseq (ics1, ics2))
9778 return 1;
9779 if (is_subseq (ics2, ics1))
9780 return -1;
9781 }
9782
9783 /* [over.ics.rank]
9784
9785 Or, if not that,
9786
9787 --the rank of S1 is better than the rank of S2 (by the rules
9788 defined below):
9789
9790 Standard conversion sequences are ordered by their ranks: an Exact
9791 Match is a better conversion than a Promotion, which is a better
9792 conversion than a Conversion.
9793
9794 Two conversion sequences with the same rank are indistinguishable
9795 unless one of the following rules applies:
9796
9797 --A conversion that does not a convert a pointer, pointer to member,
9798 or std::nullptr_t to bool is better than one that does.
9799
9800 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
9801 so that we do not have to check it explicitly. */
9802 if (ics1->rank < ics2->rank)
9803 return 1;
9804 else if (ics2->rank < ics1->rank)
9805 return -1;
9806
9807 to_type1 = ics1->type;
9808 to_type2 = ics2->type;
9809
9810 /* A conversion from scalar arithmetic type to complex is worse than a
9811 conversion between scalar arithmetic types. */
9812 if (same_type_p (from_type1, from_type2)
9813 && ARITHMETIC_TYPE_P (from_type1)
9814 && ARITHMETIC_TYPE_P (to_type1)
9815 && ARITHMETIC_TYPE_P (to_type2)
9816 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
9817 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
9818 {
9819 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
9820 return -1;
9821 else
9822 return 1;
9823 }
9824
9825 if (TYPE_PTR_P (from_type1)
9826 && TYPE_PTR_P (from_type2)
9827 && TYPE_PTR_P (to_type1)
9828 && TYPE_PTR_P (to_type2))
9829 {
9830 deref_from_type1 = TREE_TYPE (from_type1);
9831 deref_from_type2 = TREE_TYPE (from_type2);
9832 deref_to_type1 = TREE_TYPE (to_type1);
9833 deref_to_type2 = TREE_TYPE (to_type2);
9834 }
9835 /* The rules for pointers to members A::* are just like the rules
9836 for pointers A*, except opposite: if B is derived from A then
9837 A::* converts to B::*, not vice versa. For that reason, we
9838 switch the from_ and to_ variables here. */
9839 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
9840 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
9841 || (TYPE_PTRMEMFUNC_P (from_type1)
9842 && TYPE_PTRMEMFUNC_P (from_type2)
9843 && TYPE_PTRMEMFUNC_P (to_type1)
9844 && TYPE_PTRMEMFUNC_P (to_type2)))
9845 {
9846 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
9847 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
9848 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
9849 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
9850 }
9851
9852 if (deref_from_type1 != NULL_TREE
9853 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
9854 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
9855 {
9856 /* This was one of the pointer or pointer-like conversions.
9857
9858 [over.ics.rank]
9859
9860 --If class B is derived directly or indirectly from class A,
9861 conversion of B* to A* is better than conversion of B* to
9862 void*, and conversion of A* to void* is better than
9863 conversion of B* to void*. */
9864 if (VOID_TYPE_P (deref_to_type1)
9865 && VOID_TYPE_P (deref_to_type2))
9866 {
9867 if (is_properly_derived_from (deref_from_type1,
9868 deref_from_type2))
9869 return -1;
9870 else if (is_properly_derived_from (deref_from_type2,
9871 deref_from_type1))
9872 return 1;
9873 }
9874 else if (VOID_TYPE_P (deref_to_type1)
9875 || VOID_TYPE_P (deref_to_type2))
9876 {
9877 if (same_type_p (deref_from_type1, deref_from_type2))
9878 {
9879 if (VOID_TYPE_P (deref_to_type2))
9880 {
9881 if (is_properly_derived_from (deref_from_type1,
9882 deref_to_type1))
9883 return 1;
9884 }
9885 /* We know that DEREF_TO_TYPE1 is `void' here. */
9886 else if (is_properly_derived_from (deref_from_type1,
9887 deref_to_type2))
9888 return -1;
9889 }
9890 }
9891 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
9892 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
9893 {
9894 /* [over.ics.rank]
9895
9896 --If class B is derived directly or indirectly from class A
9897 and class C is derived directly or indirectly from B,
9898
9899 --conversion of C* to B* is better than conversion of C* to
9900 A*,
9901
9902 --conversion of B* to A* is better than conversion of C* to
9903 A* */
9904 if (same_type_p (deref_from_type1, deref_from_type2))
9905 {
9906 if (is_properly_derived_from (deref_to_type1,
9907 deref_to_type2))
9908 return 1;
9909 else if (is_properly_derived_from (deref_to_type2,
9910 deref_to_type1))
9911 return -1;
9912 }
9913 else if (same_type_p (deref_to_type1, deref_to_type2))
9914 {
9915 if (is_properly_derived_from (deref_from_type2,
9916 deref_from_type1))
9917 return 1;
9918 else if (is_properly_derived_from (deref_from_type1,
9919 deref_from_type2))
9920 return -1;
9921 }
9922 }
9923 }
9924 else if (CLASS_TYPE_P (non_reference (from_type1))
9925 && same_type_p (from_type1, from_type2))
9926 {
9927 tree from = non_reference (from_type1);
9928
9929 /* [over.ics.rank]
9930
9931 --binding of an expression of type C to a reference of type
9932 B& is better than binding an expression of type C to a
9933 reference of type A&
9934
9935 --conversion of C to B is better than conversion of C to A, */
9936 if (is_properly_derived_from (from, to_type1)
9937 && is_properly_derived_from (from, to_type2))
9938 {
9939 if (is_properly_derived_from (to_type1, to_type2))
9940 return 1;
9941 else if (is_properly_derived_from (to_type2, to_type1))
9942 return -1;
9943 }
9944 }
9945 else if (CLASS_TYPE_P (non_reference (to_type1))
9946 && same_type_p (to_type1, to_type2))
9947 {
9948 tree to = non_reference (to_type1);
9949
9950 /* [over.ics.rank]
9951
9952 --binding of an expression of type B to a reference of type
9953 A& is better than binding an expression of type C to a
9954 reference of type A&,
9955
9956 --conversion of B to A is better than conversion of C to A */
9957 if (is_properly_derived_from (from_type1, to)
9958 && is_properly_derived_from (from_type2, to))
9959 {
9960 if (is_properly_derived_from (from_type2, from_type1))
9961 return 1;
9962 else if (is_properly_derived_from (from_type1, from_type2))
9963 return -1;
9964 }
9965 }
9966
9967 /* [over.ics.rank]
9968
9969 --S1 and S2 differ only in their qualification conversion and yield
9970 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
9971 qualification signature of type T1 is a proper subset of the cv-
9972 qualification signature of type T2 */
9973 if (ics1->kind == ck_qual
9974 && ics2->kind == ck_qual
9975 && same_type_p (from_type1, from_type2))
9976 {
9977 int result = comp_cv_qual_signature (to_type1, to_type2);
9978 if (result != 0)
9979 return result;
9980 }
9981
9982 /* [over.ics.rank]
9983
9984 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
9985 to an implicit object parameter of a non-static member function
9986 declared without a ref-qualifier, and either S1 binds an lvalue
9987 reference to an lvalue and S2 binds an rvalue reference or S1 binds an
9988 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
9989 draft standard, 13.3.3.2)
9990
9991 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
9992 types to which the references refer are the same type except for
9993 top-level cv-qualifiers, and the type to which the reference
9994 initialized by S2 refers is more cv-qualified than the type to
9995 which the reference initialized by S1 refers.
9996
9997 DR 1328 [over.match.best]: the context is an initialization by
9998 conversion function for direct reference binding (13.3.1.6) of a
9999 reference to function type, the return type of F1 is the same kind of
10000 reference (i.e. lvalue or rvalue) as the reference being initialized,
10001 and the return type of F2 is not. */
10002
10003 if (ref_conv1 && ref_conv2)
10004 {
10005 if (!ref_conv1->this_p && !ref_conv2->this_p
10006 && (ref_conv1->rvaluedness_matches_p
10007 != ref_conv2->rvaluedness_matches_p)
10008 && (same_type_p (ref_conv1->type, ref_conv2->type)
10009 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
10010 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
10011 {
10012 if (ref_conv1->bad_p
10013 && !same_type_p (TREE_TYPE (ref_conv1->type),
10014 TREE_TYPE (ref_conv2->type)))
10015 /* Don't prefer a bad conversion that drops cv-quals to a bad
10016 conversion with the wrong rvalueness. */
10017 return 0;
10018 return (ref_conv1->rvaluedness_matches_p
10019 - ref_conv2->rvaluedness_matches_p);
10020 }
10021
10022 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
10023 {
10024 int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
10025 int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
10026 if (ref_conv1->bad_p)
10027 {
10028 /* Prefer the one that drops fewer cv-quals. */
10029 tree ftype = next_conversion (ref_conv1)->type;
10030 int fquals = cp_type_quals (ftype);
10031 q1 ^= fquals;
10032 q2 ^= fquals;
10033 }
10034 return comp_cv_qualification (q2, q1);
10035 }
10036 }
10037
10038 /* Neither conversion sequence is better than the other. */
10039 return 0;
10040 }
10041
10042 /* The source type for this standard conversion sequence. */
10043
10044 static tree
source_type(conversion * t)10045 source_type (conversion *t)
10046 {
10047 for (;; t = next_conversion (t))
10048 {
10049 if (t->kind == ck_user
10050 || t->kind == ck_ambig
10051 || t->kind == ck_identity)
10052 return t->type;
10053 }
10054 gcc_unreachable ();
10055 }
10056
10057 /* Note a warning about preferring WINNER to LOSER. We do this by storing
10058 a pointer to LOSER and re-running joust to produce the warning if WINNER
10059 is actually used. */
10060
10061 static void
add_warning(struct z_candidate * winner,struct z_candidate * loser)10062 add_warning (struct z_candidate *winner, struct z_candidate *loser)
10063 {
10064 candidate_warning *cw = (candidate_warning *)
10065 conversion_obstack_alloc (sizeof (candidate_warning));
10066 cw->loser = loser;
10067 cw->next = winner->warnings;
10068 winner->warnings = cw;
10069 }
10070
10071 /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
10072 prvalue returned from a conversion function, replace CAND with the candidate
10073 for the conversion and return true. Otherwise, return false. */
10074
10075 static bool
joust_maybe_elide_copy(z_candidate * & cand)10076 joust_maybe_elide_copy (z_candidate *&cand)
10077 {
10078 tree fn = cand->fn;
10079 if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
10080 return false;
10081 conversion *conv = cand->convs[0];
10082 gcc_checking_assert (conv->kind == ck_ref_bind);
10083 conv = next_conversion (conv);
10084 if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
10085 {
10086 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
10087 (conv->type, DECL_CONTEXT (fn)));
10088 z_candidate *uc = conv->cand;
10089 if (DECL_CONV_FN_P (uc->fn))
10090 {
10091 cand = uc;
10092 return true;
10093 }
10094 }
10095 return false;
10096 }
10097
10098 /* Compare two candidates for overloading as described in
10099 [over.match.best]. Return values:
10100
10101 1: cand1 is better than cand2
10102 -1: cand2 is better than cand1
10103 0: cand1 and cand2 are indistinguishable */
10104
10105 static int
joust(struct z_candidate * cand1,struct z_candidate * cand2,bool warn,tsubst_flags_t complain)10106 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
10107 tsubst_flags_t complain)
10108 {
10109 int winner = 0;
10110 int off1 = 0, off2 = 0;
10111 size_t i;
10112 size_t len;
10113
10114 /* Candidates that involve bad conversions are always worse than those
10115 that don't. */
10116 if (cand1->viable > cand2->viable)
10117 return 1;
10118 if (cand1->viable < cand2->viable)
10119 return -1;
10120
10121 /* If we have two pseudo-candidates for conversions to the same type,
10122 or two candidates for the same function, arbitrarily pick one. */
10123 if (cand1->fn == cand2->fn
10124 && (IS_TYPE_OR_DECL_P (cand1->fn)))
10125 return 1;
10126
10127 /* Prefer a non-deleted function over an implicitly deleted move
10128 constructor or assignment operator. This differs slightly from the
10129 wording for issue 1402 (which says the move op is ignored by overload
10130 resolution), but this way produces better error messages. */
10131 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
10132 && TREE_CODE (cand2->fn) == FUNCTION_DECL
10133 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
10134 {
10135 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
10136 && move_fn_p (cand1->fn))
10137 return -1;
10138 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
10139 && move_fn_p (cand2->fn))
10140 return 1;
10141 }
10142
10143 /* a viable function F1
10144 is defined to be a better function than another viable function F2 if
10145 for all arguments i, ICSi(F1) is not a worse conversion sequence than
10146 ICSi(F2), and then */
10147
10148 /* for some argument j, ICSj(F1) is a better conversion sequence than
10149 ICSj(F2) */
10150
10151 /* For comparing static and non-static member functions, we ignore
10152 the implicit object parameter of the non-static function. The
10153 standard says to pretend that the static function has an object
10154 parm, but that won't work with operator overloading. */
10155 len = cand1->num_convs;
10156 if (len != cand2->num_convs)
10157 {
10158 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
10159 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
10160
10161 if (DECL_CONSTRUCTOR_P (cand1->fn)
10162 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
10163 /* We're comparing a near-match list constructor and a near-match
10164 non-list constructor. Just treat them as unordered. */
10165 return 0;
10166
10167 gcc_assert (static_1 != static_2);
10168
10169 if (static_1)
10170 off2 = 1;
10171 else
10172 {
10173 off1 = 1;
10174 --len;
10175 }
10176 }
10177
10178 /* Handle C++17 copy elision in [over.match.ctor] (direct-init) context. The
10179 standard currently says that only constructors are candidates, but if one
10180 copies a prvalue returned by a conversion function we want to treat the
10181 conversion as the candidate instead.
10182
10183 Clang does something similar, as discussed at
10184 http://lists.isocpp.org/core/2017/10/3166.php
10185 http://lists.isocpp.org/core/2019/03/5721.php */
10186 int elided_tiebreaker = 0;
10187 if (len == 1 && cxx_dialect >= cxx17
10188 && DECL_P (cand1->fn)
10189 && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn)
10190 && !(cand1->flags & LOOKUP_ONLYCONVERTING))
10191 {
10192 bool elided1 = joust_maybe_elide_copy (cand1);
10193 bool elided2 = joust_maybe_elide_copy (cand2);
10194 /* As a tiebreaker below we will prefer a constructor to a conversion
10195 operator exposed this way. */
10196 elided_tiebreaker = elided2 - elided1;
10197 }
10198
10199 for (i = 0; i < len; ++i)
10200 {
10201 conversion *t1 = cand1->convs[i + off1];
10202 conversion *t2 = cand2->convs[i + off2];
10203 int comp = compare_ics (t1, t2);
10204
10205 if (comp != 0)
10206 {
10207 if ((complain & tf_warning)
10208 && warn_sign_promo
10209 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
10210 == cr_std + cr_promotion)
10211 && t1->kind == ck_std
10212 && t2->kind == ck_std
10213 && TREE_CODE (t1->type) == INTEGER_TYPE
10214 && TREE_CODE (t2->type) == INTEGER_TYPE
10215 && (TYPE_PRECISION (t1->type)
10216 == TYPE_PRECISION (t2->type))
10217 && (TYPE_UNSIGNED (next_conversion (t1)->type)
10218 || (TREE_CODE (next_conversion (t1)->type)
10219 == ENUMERAL_TYPE)))
10220 {
10221 tree type = next_conversion (t1)->type;
10222 tree type1, type2;
10223 struct z_candidate *w, *l;
10224 if (comp > 0)
10225 type1 = t1->type, type2 = t2->type,
10226 w = cand1, l = cand2;
10227 else
10228 type1 = t2->type, type2 = t1->type,
10229 w = cand2, l = cand1;
10230
10231 if (warn)
10232 {
10233 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
10234 type, type1, type2);
10235 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
10236 }
10237 else
10238 add_warning (w, l);
10239 }
10240
10241 if (winner && comp != winner)
10242 {
10243 winner = 0;
10244 goto tweak;
10245 }
10246 winner = comp;
10247 }
10248 }
10249
10250 /* warn about confusing overload resolution for user-defined conversions,
10251 either between a constructor and a conversion op, or between two
10252 conversion ops. */
10253 if ((complain & tf_warning)
10254 /* In C++17, the constructor might have been elided, which means that
10255 an originally null ->second_conv could become non-null. */
10256 && winner && warn_conversion && cand1->second_conv && cand2->second_conv
10257 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
10258 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
10259 {
10260 struct z_candidate *w, *l;
10261 bool give_warning = false;
10262
10263 if (winner == 1)
10264 w = cand1, l = cand2;
10265 else
10266 w = cand2, l = cand1;
10267
10268 /* We don't want to complain about `X::operator T1 ()'
10269 beating `X::operator T2 () const', when T2 is a no less
10270 cv-qualified version of T1. */
10271 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
10272 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
10273 {
10274 tree t = TREE_TYPE (TREE_TYPE (l->fn));
10275 tree f = TREE_TYPE (TREE_TYPE (w->fn));
10276
10277 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
10278 {
10279 t = TREE_TYPE (t);
10280 f = TREE_TYPE (f);
10281 }
10282 if (!comp_ptr_ttypes (t, f))
10283 give_warning = true;
10284 }
10285 else
10286 give_warning = true;
10287
10288 if (!give_warning)
10289 /*NOP*/;
10290 else if (warn)
10291 {
10292 tree source = source_type (w->convs[0]);
10293 if (POINTER_TYPE_P (source))
10294 source = TREE_TYPE (source);
10295 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
10296 && warning (OPT_Wconversion, " for conversion from %qH to %qI",
10297 source, w->second_conv->type))
10298 {
10299 inform (input_location, " because conversion sequence for the argument is better");
10300 }
10301 }
10302 else
10303 add_warning (w, l);
10304 }
10305
10306 if (winner)
10307 return winner;
10308
10309 /* Put this tiebreaker first, so that we don't try to look at second_conv of
10310 a constructor candidate that doesn't have one. */
10311 if (elided_tiebreaker)
10312 return elided_tiebreaker;
10313
10314 /* DR 495 moved this tiebreaker above the template ones. */
10315 /* or, if not that,
10316 the context is an initialization by user-defined conversion (see
10317 _dcl.init_ and _over.match.user_) and the standard conversion
10318 sequence from the return type of F1 to the destination type (i.e.,
10319 the type of the entity being initialized) is a better conversion
10320 sequence than the standard conversion sequence from the return type
10321 of F2 to the destination type. */
10322
10323 if (cand1->second_conv)
10324 {
10325 winner = compare_ics (cand1->second_conv, cand2->second_conv);
10326 if (winner)
10327 return winner;
10328 }
10329
10330 /* or, if not that,
10331 F1 is a non-template function and F2 is a template function
10332 specialization. */
10333
10334 if (!cand1->template_decl && cand2->template_decl)
10335 return 1;
10336 else if (cand1->template_decl && !cand2->template_decl)
10337 return -1;
10338
10339 /* or, if not that,
10340 F1 and F2 are template functions and the function template for F1 is
10341 more specialized than the template for F2 according to the partial
10342 ordering rules. */
10343
10344 if (cand1->template_decl && cand2->template_decl)
10345 {
10346 winner = more_specialized_fn
10347 (TI_TEMPLATE (cand1->template_decl),
10348 TI_TEMPLATE (cand2->template_decl),
10349 /* [temp.func.order]: The presence of unused ellipsis and default
10350 arguments has no effect on the partial ordering of function
10351 templates. add_function_candidate() will not have
10352 counted the "this" argument for constructors. */
10353 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
10354 if (winner)
10355 return winner;
10356 }
10357
10358 // C++ Concepts
10359 // or, if not that, F1 is more constrained than F2.
10360 if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn))
10361 {
10362 winner = more_constrained (cand1->fn, cand2->fn);
10363 if (winner)
10364 return winner;
10365 }
10366
10367 /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
10368 if (deduction_guide_p (cand1->fn))
10369 {
10370 gcc_assert (deduction_guide_p (cand2->fn));
10371 /* We distinguish between candidates from an explicit deduction guide and
10372 candidates built from a constructor based on DECL_ARTIFICIAL. */
10373 int art1 = DECL_ARTIFICIAL (cand1->fn);
10374 int art2 = DECL_ARTIFICIAL (cand2->fn);
10375 if (art1 != art2)
10376 return art2 - art1;
10377
10378 if (art1)
10379 {
10380 /* Prefer the special copy guide over a declared copy/move
10381 constructor. */
10382 if (copy_guide_p (cand1->fn))
10383 return 1;
10384 if (copy_guide_p (cand2->fn))
10385 return -1;
10386
10387 /* Prefer a candidate generated from a non-template constructor. */
10388 int tg1 = template_guide_p (cand1->fn);
10389 int tg2 = template_guide_p (cand2->fn);
10390 if (tg1 != tg2)
10391 return tg2 - tg1;
10392 }
10393 }
10394
10395 /* F1 is a member of a class D, F2 is a member of a base class B of D, and
10396 for all arguments the corresponding parameters of F1 and F2 have the same
10397 type (CWG 2273/2277). */
10398 if (DECL_P (cand1->fn) && DECL_CLASS_SCOPE_P (cand1->fn)
10399 && !DECL_CONV_FN_P (cand1->fn)
10400 && DECL_P (cand2->fn) && DECL_CLASS_SCOPE_P (cand2->fn)
10401 && !DECL_CONV_FN_P (cand2->fn))
10402 {
10403 tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
10404 tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
10405
10406 bool used1 = false;
10407 bool used2 = false;
10408 if (base1 == base2)
10409 /* No difference. */;
10410 else if (DERIVED_FROM_P (base1, base2))
10411 used1 = true;
10412 else if (DERIVED_FROM_P (base2, base1))
10413 used2 = true;
10414
10415 if (int diff = used2 - used1)
10416 {
10417 for (i = 0; i < len; ++i)
10418 {
10419 conversion *t1 = cand1->convs[i + off1];
10420 conversion *t2 = cand2->convs[i + off2];
10421 if (!same_type_p (t1->type, t2->type))
10422 break;
10423 }
10424 if (i == len)
10425 return diff;
10426 }
10427 }
10428
10429 /* Check whether we can discard a builtin candidate, either because we
10430 have two identical ones or matching builtin and non-builtin candidates.
10431
10432 (Pedantically in the latter case the builtin which matched the user
10433 function should not be added to the overload set, but we spot it here.
10434
10435 [over.match.oper]
10436 ... the builtin candidates include ...
10437 - do not have the same parameter type list as any non-template
10438 non-member candidate. */
10439
10440 if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
10441 {
10442 for (i = 0; i < len; ++i)
10443 if (!same_type_p (cand1->convs[i]->type,
10444 cand2->convs[i]->type))
10445 break;
10446 if (i == cand1->num_convs)
10447 {
10448 if (cand1->fn == cand2->fn)
10449 /* Two built-in candidates; arbitrarily pick one. */
10450 return 1;
10451 else if (identifier_p (cand1->fn))
10452 /* cand1 is built-in; prefer cand2. */
10453 return -1;
10454 else
10455 /* cand2 is built-in; prefer cand1. */
10456 return 1;
10457 }
10458 }
10459
10460 /* For candidates of a multi-versioned function, make the version with
10461 the highest priority win. This version will be checked for dispatching
10462 first. If this version can be inlined into the caller, the front-end
10463 will simply make a direct call to this function. */
10464
10465 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
10466 && DECL_FUNCTION_VERSIONED (cand1->fn)
10467 && TREE_CODE (cand2->fn) == FUNCTION_DECL
10468 && DECL_FUNCTION_VERSIONED (cand2->fn))
10469 {
10470 tree f1 = TREE_TYPE (cand1->fn);
10471 tree f2 = TREE_TYPE (cand2->fn);
10472 tree p1 = TYPE_ARG_TYPES (f1);
10473 tree p2 = TYPE_ARG_TYPES (f2);
10474
10475 /* Check if cand1->fn and cand2->fn are versions of the same function. It
10476 is possible that cand1->fn and cand2->fn are function versions but of
10477 different functions. Check types to see if they are versions of the same
10478 function. */
10479 if (compparms (p1, p2)
10480 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
10481 {
10482 /* Always make the version with the higher priority, more
10483 specialized, win. */
10484 gcc_assert (targetm.compare_version_priority);
10485 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
10486 return 1;
10487 else
10488 return -1;
10489 }
10490 }
10491
10492 /* If the two function declarations represent the same function (this can
10493 happen with declarations in multiple scopes and arg-dependent lookup),
10494 arbitrarily choose one. But first make sure the default args we're
10495 using match. */
10496 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
10497 && equal_functions (cand1->fn, cand2->fn))
10498 {
10499 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
10500 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
10501
10502 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
10503
10504 for (i = 0; i < len; ++i)
10505 {
10506 /* Don't crash if the fn is variadic. */
10507 if (!parms1)
10508 break;
10509 parms1 = TREE_CHAIN (parms1);
10510 parms2 = TREE_CHAIN (parms2);
10511 }
10512
10513 if (off1)
10514 parms1 = TREE_CHAIN (parms1);
10515 else if (off2)
10516 parms2 = TREE_CHAIN (parms2);
10517
10518 for (; parms1; ++i)
10519 {
10520 if (!cp_tree_equal (TREE_PURPOSE (parms1),
10521 TREE_PURPOSE (parms2)))
10522 {
10523 if (warn)
10524 {
10525 if (complain & tf_error)
10526 {
10527 if (permerror (input_location,
10528 "default argument mismatch in "
10529 "overload resolution"))
10530 {
10531 inform (DECL_SOURCE_LOCATION (cand1->fn),
10532 " candidate 1: %q#F", cand1->fn);
10533 inform (DECL_SOURCE_LOCATION (cand2->fn),
10534 " candidate 2: %q#F", cand2->fn);
10535 }
10536 }
10537 else
10538 return 0;
10539 }
10540 else
10541 add_warning (cand1, cand2);
10542 break;
10543 }
10544 parms1 = TREE_CHAIN (parms1);
10545 parms2 = TREE_CHAIN (parms2);
10546 }
10547
10548 return 1;
10549 }
10550
10551 tweak:
10552
10553 /* Extension: If the worst conversion for one candidate is worse than the
10554 worst conversion for the other, take the first. */
10555 if (!pedantic && (complain & tf_warning_or_error))
10556 {
10557 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
10558 struct z_candidate *w = 0, *l = 0;
10559
10560 for (i = 0; i < len; ++i)
10561 {
10562 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
10563 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
10564 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
10565 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
10566 }
10567 if (rank1 < rank2)
10568 winner = 1, w = cand1, l = cand2;
10569 if (rank1 > rank2)
10570 winner = -1, w = cand2, l = cand1;
10571 if (winner)
10572 {
10573 /* Don't choose a deleted function over ambiguity. */
10574 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
10575 return 0;
10576 if (warn)
10577 {
10578 pedwarn (input_location, 0,
10579 "ISO C++ says that these are ambiguous, even "
10580 "though the worst conversion for the first is better than "
10581 "the worst conversion for the second:");
10582 print_z_candidate (input_location, _("candidate 1:"), w);
10583 print_z_candidate (input_location, _("candidate 2:"), l);
10584 }
10585 else
10586 add_warning (w, l);
10587 return winner;
10588 }
10589 }
10590
10591 gcc_assert (!winner);
10592 return 0;
10593 }
10594
10595 /* Given a list of candidates for overloading, find the best one, if any.
10596 This algorithm has a worst case of O(2n) (winner is last), and a best
10597 case of O(n/2) (totally ambiguous); much better than a sorting
10598 algorithm. */
10599
10600 static struct z_candidate *
tourney(struct z_candidate * candidates,tsubst_flags_t complain)10601 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
10602 {
10603 struct z_candidate *champ = candidates, *challenger;
10604 int fate;
10605 int champ_compared_to_predecessor = 0;
10606
10607 /* Walk through the list once, comparing each current champ to the next
10608 candidate, knocking out a candidate or two with each comparison. */
10609
10610 for (challenger = champ->next; challenger; )
10611 {
10612 fate = joust (champ, challenger, 0, complain);
10613 if (fate == 1)
10614 challenger = challenger->next;
10615 else
10616 {
10617 if (fate == 0)
10618 {
10619 champ = challenger->next;
10620 if (champ == 0)
10621 return NULL;
10622 champ_compared_to_predecessor = 0;
10623 }
10624 else
10625 {
10626 champ = challenger;
10627 champ_compared_to_predecessor = 1;
10628 }
10629
10630 challenger = champ->next;
10631 }
10632 }
10633
10634 /* Make sure the champ is better than all the candidates it hasn't yet
10635 been compared to. */
10636
10637 for (challenger = candidates;
10638 challenger != champ
10639 && !(champ_compared_to_predecessor && challenger->next == champ);
10640 challenger = challenger->next)
10641 {
10642 fate = joust (champ, challenger, 0, complain);
10643 if (fate != 1)
10644 return NULL;
10645 }
10646
10647 return champ;
10648 }
10649
10650 /* Returns nonzero if things of type FROM can be converted to TO. */
10651
10652 bool
can_convert(tree to,tree from,tsubst_flags_t complain)10653 can_convert (tree to, tree from, tsubst_flags_t complain)
10654 {
10655 tree arg = NULL_TREE;
10656 /* implicit_conversion only considers user-defined conversions
10657 if it has an expression for the call argument list. */
10658 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
10659 arg = build1 (CAST_EXPR, from, NULL_TREE);
10660 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
10661 }
10662
10663 /* Returns nonzero if things of type FROM can be converted to TO with a
10664 standard conversion. */
10665
10666 bool
can_convert_standard(tree to,tree from,tsubst_flags_t complain)10667 can_convert_standard (tree to, tree from, tsubst_flags_t complain)
10668 {
10669 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
10670 }
10671
10672 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
10673
10674 bool
can_convert_arg(tree to,tree from,tree arg,int flags,tsubst_flags_t complain)10675 can_convert_arg (tree to, tree from, tree arg, int flags,
10676 tsubst_flags_t complain)
10677 {
10678 conversion *t;
10679 void *p;
10680 bool ok_p;
10681
10682 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10683 p = conversion_obstack_alloc (0);
10684 /* We want to discard any access checks done for this test,
10685 as we might not be in the appropriate access context and
10686 we'll do the check again when we actually perform the
10687 conversion. */
10688 push_deferring_access_checks (dk_deferred);
10689
10690 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
10691 flags, complain);
10692 ok_p = (t && !t->bad_p);
10693
10694 /* Discard the access checks now. */
10695 pop_deferring_access_checks ();
10696 /* Free all the conversions we allocated. */
10697 obstack_free (&conversion_obstack, p);
10698
10699 return ok_p;
10700 }
10701
10702 /* Like can_convert_arg, but allows dubious conversions as well. */
10703
10704 bool
can_convert_arg_bad(tree to,tree from,tree arg,int flags,tsubst_flags_t complain)10705 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
10706 tsubst_flags_t complain)
10707 {
10708 conversion *t;
10709 void *p;
10710
10711 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10712 p = conversion_obstack_alloc (0);
10713 /* Try to perform the conversion. */
10714 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
10715 flags, complain);
10716 /* Free all the conversions we allocated. */
10717 obstack_free (&conversion_obstack, p);
10718
10719 return t != NULL;
10720 }
10721
10722 /* Convert EXPR to TYPE. Return the converted expression.
10723
10724 Note that we allow bad conversions here because by the time we get to
10725 this point we are committed to doing the conversion. If we end up
10726 doing a bad conversion, convert_like will complain. */
10727
10728 tree
perform_implicit_conversion_flags(tree type,tree expr,tsubst_flags_t complain,int flags)10729 perform_implicit_conversion_flags (tree type, tree expr,
10730 tsubst_flags_t complain, int flags)
10731 {
10732 conversion *conv;
10733 void *p;
10734 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
10735
10736 if (TREE_CODE (type) == REFERENCE_TYPE)
10737 expr = mark_lvalue_use (expr);
10738 else
10739 expr = mark_rvalue_use (expr);
10740
10741 if (error_operand_p (expr))
10742 return error_mark_node;
10743
10744 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10745 p = conversion_obstack_alloc (0);
10746
10747 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10748 /*c_cast_p=*/false,
10749 flags, complain);
10750
10751 if (!conv)
10752 {
10753 if (complain & tf_error)
10754 {
10755 /* If expr has unknown type, then it is an overloaded function.
10756 Call instantiate_type to get good error messages. */
10757 if (TREE_TYPE (expr) == unknown_type_node)
10758 instantiate_type (type, expr, complain);
10759 else if (invalid_nonstatic_memfn_p (loc, expr, complain))
10760 /* We gave an error. */;
10761 else
10762 error_at (loc, "could not convert %qE from %qH to %qI", expr,
10763 TREE_TYPE (expr), type);
10764 }
10765 expr = error_mark_node;
10766 }
10767 else if (processing_template_decl && conv->kind != ck_identity)
10768 {
10769 /* In a template, we are only concerned about determining the
10770 type of non-dependent expressions, so we do not have to
10771 perform the actual conversion. But for initializers, we
10772 need to be able to perform it at instantiation
10773 (or instantiate_non_dependent_expr) time. */
10774 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
10775 if (!(flags & LOOKUP_ONLYCONVERTING))
10776 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
10777 }
10778 else
10779 expr = convert_like (conv, expr, complain);
10780
10781 /* Free all the conversions we allocated. */
10782 obstack_free (&conversion_obstack, p);
10783
10784 return expr;
10785 }
10786
10787 tree
perform_implicit_conversion(tree type,tree expr,tsubst_flags_t complain)10788 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
10789 {
10790 return perform_implicit_conversion_flags (type, expr, complain,
10791 LOOKUP_IMPLICIT);
10792 }
10793
10794 /* Convert EXPR to TYPE (as a direct-initialization) if that is
10795 permitted. If the conversion is valid, the converted expression is
10796 returned. Otherwise, NULL_TREE is returned, except in the case
10797 that TYPE is a class type; in that case, an error is issued. If
10798 C_CAST_P is true, then this direct-initialization is taking
10799 place as part of a static_cast being attempted as part of a C-style
10800 cast. */
10801
10802 tree
perform_direct_initialization_if_possible(tree type,tree expr,bool c_cast_p,tsubst_flags_t complain)10803 perform_direct_initialization_if_possible (tree type,
10804 tree expr,
10805 bool c_cast_p,
10806 tsubst_flags_t complain)
10807 {
10808 conversion *conv;
10809 void *p;
10810
10811 if (type == error_mark_node || error_operand_p (expr))
10812 return error_mark_node;
10813 /* [dcl.init]
10814
10815 If the destination type is a (possibly cv-qualified) class type:
10816
10817 -- If the initialization is direct-initialization ...,
10818 constructors are considered. ... If no constructor applies, or
10819 the overload resolution is ambiguous, the initialization is
10820 ill-formed. */
10821 if (CLASS_TYPE_P (type))
10822 {
10823 vec<tree, va_gc> *args = make_tree_vector_single (expr);
10824 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
10825 &args, type, LOOKUP_NORMAL, complain);
10826 release_tree_vector (args);
10827 return build_cplus_new (type, expr, complain);
10828 }
10829
10830 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10831 p = conversion_obstack_alloc (0);
10832
10833 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10834 c_cast_p,
10835 LOOKUP_NORMAL, complain);
10836 if (!conv || conv->bad_p)
10837 expr = NULL_TREE;
10838 else if (processing_template_decl && conv->kind != ck_identity)
10839 {
10840 /* In a template, we are only concerned about determining the
10841 type of non-dependent expressions, so we do not have to
10842 perform the actual conversion. But for initializers, we
10843 need to be able to perform it at instantiation
10844 (or instantiate_non_dependent_expr) time. */
10845 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
10846 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
10847 }
10848 else
10849 expr = convert_like_real (conv, expr, NULL_TREE, 0,
10850 /*issue_conversion_warnings=*/false,
10851 c_cast_p,
10852 complain);
10853
10854 /* Free all the conversions we allocated. */
10855 obstack_free (&conversion_obstack, p);
10856
10857 return expr;
10858 }
10859
10860 /* When initializing a reference that lasts longer than a full-expression,
10861 this special rule applies:
10862
10863 [class.temporary]
10864
10865 The temporary to which the reference is bound or the temporary
10866 that is the complete object to which the reference is bound
10867 persists for the lifetime of the reference.
10868
10869 The temporaries created during the evaluation of the expression
10870 initializing the reference, except the temporary to which the
10871 reference is bound, are destroyed at the end of the
10872 full-expression in which they are created.
10873
10874 In that case, we store the converted expression into a new
10875 VAR_DECL in a new scope.
10876
10877 However, we want to be careful not to create temporaries when
10878 they are not required. For example, given:
10879
10880 struct B {};
10881 struct D : public B {};
10882 D f();
10883 const B& b = f();
10884
10885 there is no need to copy the return value from "f"; we can just
10886 extend its lifetime. Similarly, given:
10887
10888 struct S {};
10889 struct T { operator S(); };
10890 T t;
10891 const S& s = t;
10892
10893 we can extend the lifetime of the return value of the conversion
10894 operator.
10895
10896 The next several functions are involved in this lifetime extension. */
10897
10898 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
10899 reference is being bound to a temporary. Create and return a new
10900 VAR_DECL with the indicated TYPE; this variable will store the value to
10901 which the reference is bound. */
10902
10903 tree
make_temporary_var_for_ref_to_temp(tree decl,tree type)10904 make_temporary_var_for_ref_to_temp (tree decl, tree type)
10905 {
10906 tree var = create_temporary_var (type);
10907
10908 /* Register the variable. */
10909 if (VAR_P (decl)
10910 && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
10911 {
10912 /* Namespace-scope or local static; give it a mangled name. */
10913 /* FIXME share comdat with decl? */
10914
10915 TREE_STATIC (var) = TREE_STATIC (decl);
10916 CP_DECL_THREAD_LOCAL_P (var) = CP_DECL_THREAD_LOCAL_P (decl);
10917 set_decl_tls_model (var, DECL_TLS_MODEL (decl));
10918
10919 tree name = mangle_ref_init_variable (decl);
10920 DECL_NAME (var) = name;
10921 SET_DECL_ASSEMBLER_NAME (var, name);
10922 }
10923 else
10924 /* Create a new cleanup level if necessary. */
10925 maybe_push_cleanup_level (type);
10926
10927 return pushdecl (var);
10928 }
10929
10930 /* EXPR is the initializer for a variable DECL of reference or
10931 std::initializer_list type. Create, push and return a new VAR_DECL
10932 for the initializer so that it will live as long as DECL. Any
10933 cleanup for the new variable is returned through CLEANUP, and the
10934 code to initialize the new variable is returned through INITP. */
10935
10936 static tree
set_up_extended_ref_temp(tree decl,tree expr,vec<tree,va_gc> ** cleanups,tree * initp)10937 set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
10938 tree *initp)
10939 {
10940 tree init;
10941 tree type;
10942 tree var;
10943
10944 /* Create the temporary variable. */
10945 type = TREE_TYPE (expr);
10946 var = make_temporary_var_for_ref_to_temp (decl, type);
10947 layout_decl (var, 0);
10948 /* If the rvalue is the result of a function call it will be
10949 a TARGET_EXPR. If it is some other construct (such as a
10950 member access expression where the underlying object is
10951 itself the result of a function call), turn it into a
10952 TARGET_EXPR here. It is important that EXPR be a
10953 TARGET_EXPR below since otherwise the INIT_EXPR will
10954 attempt to make a bitwise copy of EXPR to initialize
10955 VAR. */
10956 if (TREE_CODE (expr) != TARGET_EXPR)
10957 expr = get_target_expr (expr);
10958
10959 if (TREE_CODE (decl) == FIELD_DECL
10960 && extra_warnings && !TREE_NO_WARNING (decl))
10961 {
10962 warning (OPT_Wextra, "a temporary bound to %qD only persists "
10963 "until the constructor exits", decl);
10964 TREE_NO_WARNING (decl) = true;
10965 }
10966
10967 /* Recursively extend temps in this initializer. */
10968 TARGET_EXPR_INITIAL (expr)
10969 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups);
10970
10971 /* Any reference temp has a non-trivial initializer. */
10972 DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
10973
10974 /* If the initializer is constant, put it in DECL_INITIAL so we get
10975 static initialization and use in constant expressions. */
10976 init = maybe_constant_init (expr);
10977 /* As in store_init_value. */
10978 init = cp_fully_fold (init);
10979 if (TREE_CONSTANT (init))
10980 {
10981 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
10982 {
10983 /* 5.19 says that a constant expression can include an
10984 lvalue-rvalue conversion applied to "a glvalue of literal type
10985 that refers to a non-volatile temporary object initialized
10986 with a constant expression". Rather than try to communicate
10987 that this VAR_DECL is a temporary, just mark it constexpr.
10988
10989 Currently this is only useful for initializer_list temporaries,
10990 since reference vars can't appear in constant expressions. */
10991 DECL_DECLARED_CONSTEXPR_P (var) = true;
10992 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
10993 TREE_CONSTANT (var) = true;
10994 }
10995 DECL_INITIAL (var) = init;
10996 init = NULL_TREE;
10997 }
10998 else
10999 /* Create the INIT_EXPR that will initialize the temporary
11000 variable. */
11001 init = split_nonconstant_init (var, expr);
11002 if (at_function_scope_p ())
11003 {
11004 add_decl_expr (var);
11005
11006 if (TREE_STATIC (var))
11007 init = add_stmt_to_compound (init, register_dtor_fn (var));
11008 else
11009 {
11010 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
11011 if (cleanup)
11012 vec_safe_push (*cleanups, cleanup);
11013 }
11014
11015 /* We must be careful to destroy the temporary only
11016 after its initialization has taken place. If the
11017 initialization throws an exception, then the
11018 destructor should not be run. We cannot simply
11019 transform INIT into something like:
11020
11021 (INIT, ({ CLEANUP_STMT; }))
11022
11023 because emit_local_var always treats the
11024 initializer as a full-expression. Thus, the
11025 destructor would run too early; it would run at the
11026 end of initializing the reference variable, rather
11027 than at the end of the block enclosing the
11028 reference variable.
11029
11030 The solution is to pass back a cleanup expression
11031 which the caller is responsible for attaching to
11032 the statement tree. */
11033 }
11034 else
11035 {
11036 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
11037 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
11038 {
11039 if (CP_DECL_THREAD_LOCAL_P (var))
11040 tls_aggregates = tree_cons (NULL_TREE, var,
11041 tls_aggregates);
11042 else
11043 static_aggregates = tree_cons (NULL_TREE, var,
11044 static_aggregates);
11045 }
11046 else
11047 /* Check whether the dtor is callable. */
11048 cxx_maybe_build_cleanup (var, tf_warning_or_error);
11049 }
11050 /* Avoid -Wunused-variable warning (c++/38958). */
11051 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
11052 && VAR_P (decl))
11053 TREE_USED (decl) = DECL_READ_P (decl) = true;
11054
11055 *initp = init;
11056 return var;
11057 }
11058
11059 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
11060 initializing a variable of that TYPE. */
11061
11062 tree
initialize_reference(tree type,tree expr,int flags,tsubst_flags_t complain)11063 initialize_reference (tree type, tree expr,
11064 int flags, tsubst_flags_t complain)
11065 {
11066 conversion *conv;
11067 void *p;
11068 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
11069
11070 if (type == error_mark_node || error_operand_p (expr))
11071 return error_mark_node;
11072
11073 /* Get the high-water mark for the CONVERSION_OBSTACK. */
11074 p = conversion_obstack_alloc (0);
11075
11076 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
11077 flags, complain);
11078 if (!conv || conv->bad_p)
11079 {
11080 if (complain & tf_error)
11081 {
11082 if (conv)
11083 convert_like (conv, expr, complain);
11084 else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
11085 && !TYPE_REF_IS_RVALUE (type)
11086 && !lvalue_p (expr))
11087 error_at (loc, "invalid initialization of non-const reference of "
11088 "type %qH from an rvalue of type %qI",
11089 type, TREE_TYPE (expr));
11090 else
11091 error_at (loc, "invalid initialization of reference of type "
11092 "%qH from expression of type %qI", type,
11093 TREE_TYPE (expr));
11094 }
11095 return error_mark_node;
11096 }
11097
11098 if (conv->kind == ck_ref_bind)
11099 /* Perform the conversion. */
11100 expr = convert_like (conv, expr, complain);
11101 else if (conv->kind == ck_ambig)
11102 /* We gave an error in build_user_type_conversion_1. */
11103 expr = error_mark_node;
11104 else
11105 gcc_unreachable ();
11106
11107 /* Free all the conversions we allocated. */
11108 obstack_free (&conversion_obstack, p);
11109
11110 return expr;
11111 }
11112
11113 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
11114 which is bound either to a reference or a std::initializer_list. */
11115
11116 static tree
extend_ref_init_temps_1(tree decl,tree init,vec<tree,va_gc> ** cleanups)11117 extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups)
11118 {
11119 tree sub = init;
11120 tree *p;
11121 STRIP_NOPS (sub);
11122 if (TREE_CODE (sub) == COMPOUND_EXPR)
11123 {
11124 TREE_OPERAND (sub, 1)
11125 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups);
11126 return init;
11127 }
11128 if (TREE_CODE (sub) != ADDR_EXPR)
11129 return init;
11130 /* Deal with binding to a subobject. */
11131 for (p = &TREE_OPERAND (sub, 0);
11132 (TREE_CODE (*p) == COMPONENT_REF
11133 || TREE_CODE (*p) == ARRAY_REF); )
11134 p = &TREE_OPERAND (*p, 0);
11135 if (TREE_CODE (*p) == TARGET_EXPR)
11136 {
11137 tree subinit = NULL_TREE;
11138 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit);
11139 recompute_tree_invariant_for_addr_expr (sub);
11140 if (init != sub)
11141 init = fold_convert (TREE_TYPE (init), sub);
11142 if (subinit)
11143 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
11144 }
11145 return init;
11146 }
11147
11148 /* INIT is part of the initializer for DECL. If there are any
11149 reference or initializer lists being initialized, extend their
11150 lifetime to match that of DECL. */
11151
11152 tree
extend_ref_init_temps(tree decl,tree init,vec<tree,va_gc> ** cleanups)11153 extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
11154 {
11155 tree type = TREE_TYPE (init);
11156 if (processing_template_decl)
11157 return init;
11158 if (TREE_CODE (type) == REFERENCE_TYPE)
11159 init = extend_ref_init_temps_1 (decl, init, cleanups);
11160 else
11161 {
11162 tree ctor = init;
11163 if (TREE_CODE (ctor) == TARGET_EXPR)
11164 ctor = TARGET_EXPR_INITIAL (ctor);
11165 if (TREE_CODE (ctor) == CONSTRUCTOR)
11166 {
11167 if (is_std_init_list (type))
11168 {
11169 /* The temporary array underlying a std::initializer_list
11170 is handled like a reference temporary. */
11171 tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
11172 array = extend_ref_init_temps_1 (decl, array, cleanups);
11173 CONSTRUCTOR_ELT (ctor, 0)->value = array;
11174 }
11175 else
11176 {
11177 unsigned i;
11178 constructor_elt *p;
11179 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
11180 FOR_EACH_VEC_SAFE_ELT (elts, i, p)
11181 p->value = extend_ref_init_temps (decl, p->value, cleanups);
11182 }
11183 recompute_constructor_flags (ctor);
11184 if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
11185 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
11186 }
11187 }
11188
11189 return init;
11190 }
11191
11192 /* Returns true iff an initializer for TYPE could contain temporaries that
11193 need to be extended because they are bound to references or
11194 std::initializer_list. */
11195
11196 bool
type_has_extended_temps(tree type)11197 type_has_extended_temps (tree type)
11198 {
11199 type = strip_array_types (type);
11200 if (TREE_CODE (type) == REFERENCE_TYPE)
11201 return true;
11202 if (CLASS_TYPE_P (type))
11203 {
11204 if (is_std_init_list (type))
11205 return true;
11206 for (tree f = next_initializable_field (TYPE_FIELDS (type));
11207 f; f = next_initializable_field (DECL_CHAIN (f)))
11208 if (type_has_extended_temps (TREE_TYPE (f)))
11209 return true;
11210 }
11211 return false;
11212 }
11213
11214 /* Returns true iff TYPE is some variant of std::initializer_list. */
11215
11216 bool
is_std_init_list(tree type)11217 is_std_init_list (tree type)
11218 {
11219 if (!TYPE_P (type))
11220 return false;
11221 if (cxx_dialect == cxx98)
11222 return false;
11223 /* Look through typedefs. */
11224 type = TYPE_MAIN_VARIANT (type);
11225 return (CLASS_TYPE_P (type)
11226 && CP_TYPE_CONTEXT (type) == std_node
11227 && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
11228 }
11229
11230 /* Returns true iff DECL is a list constructor: i.e. a constructor which
11231 will accept an argument list of a single std::initializer_list<T>. */
11232
11233 bool
is_list_ctor(tree decl)11234 is_list_ctor (tree decl)
11235 {
11236 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
11237 tree arg;
11238
11239 if (!args || args == void_list_node)
11240 return false;
11241
11242 arg = non_reference (TREE_VALUE (args));
11243 if (!is_std_init_list (arg))
11244 return false;
11245
11246 args = TREE_CHAIN (args);
11247
11248 if (args && args != void_list_node && !TREE_PURPOSE (args))
11249 /* There are more non-defaulted parms. */
11250 return false;
11251
11252 return true;
11253 }
11254
11255 #include "gt-cp-call.h"
11256