PR c++/82760 - memory corruption with aligned new.
[official-gcc.git] / gcc / cp / call.c
blobe04626863af5cde497c83f7d10ce26155a3ab299
1 /* Functions related to invoking -*- C++ -*- methods and overloaded functions.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GCC.
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.
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.
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/>. */
23 /* High-level class interface. */
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"
45 /* The various kinds of conversion. */
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
64 /* The rank of the conversion. Order of the enumerals matters; better
65 conversions should come earlier in the list. */
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
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. */
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. */
97 BOOL_BITFIELD need_temporary_p : 1;
98 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
99 from a pointer-to-derived to pointer-to-base is being performed. */
100 BOOL_BITFIELD base_p : 1;
101 /* If KIND is ck_ref_bind, true when either an lvalue reference is
102 being bound to an lvalue expression or an rvalue reference is
103 being bound to an rvalue expression. If KIND is ck_rvalue,
104 true when we are treating an lvalue as an rvalue (12.8p33). If
105 KIND is ck_base, always false. */
106 BOOL_BITFIELD rvaluedness_matches_p: 1;
107 BOOL_BITFIELD check_narrowing: 1;
108 /* The type of the expression resulting from the conversion. */
109 tree type;
110 union {
111 /* The next conversion in the chain. Since the conversions are
112 arranged from outermost to innermost, the NEXT conversion will
113 actually be performed before this conversion. This variant is
114 used only when KIND is neither ck_identity, ck_ambig nor
115 ck_list. Please use the next_conversion function instead
116 of using this field directly. */
117 conversion *next;
118 /* The expression at the beginning of the conversion chain. This
119 variant is used only if KIND is ck_identity or ck_ambig. */
120 tree expr;
121 /* The array of conversions for an initializer_list, so this
122 variant is used only when KIN D is ck_list. */
123 conversion **list;
124 } u;
125 /* The function candidate corresponding to this conversion
126 sequence. This field is only used if KIND is ck_user. */
127 struct z_candidate *cand;
130 #define CONVERSION_RANK(NODE) \
131 ((NODE)->bad_p ? cr_bad \
132 : (NODE)->ellipsis_p ? cr_ellipsis \
133 : (NODE)->user_conv_p ? cr_user \
134 : (NODE)->rank)
136 #define BAD_CONVERSION_RANK(NODE) \
137 ((NODE)->ellipsis_p ? cr_ellipsis \
138 : (NODE)->user_conv_p ? cr_user \
139 : (NODE)->rank)
141 static struct obstack conversion_obstack;
142 static bool conversion_obstack_initialized;
143 struct rejection_reason;
145 static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
146 static int equal_functions (tree, tree);
147 static int joust (struct z_candidate *, struct z_candidate *, bool,
148 tsubst_flags_t);
149 static int compare_ics (conversion *, conversion *);
150 static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
151 #define convert_like(CONV, EXPR, COMPLAIN) \
152 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, \
153 /*issue_conversion_warnings=*/true, \
154 /*c_cast_p=*/false, (COMPLAIN))
155 #define convert_like_with_context(CONV, EXPR, FN, ARGNO, COMPLAIN ) \
156 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), \
157 /*issue_conversion_warnings=*/true, \
158 /*c_cast_p=*/false, (COMPLAIN))
159 static tree convert_like_real (conversion *, tree, tree, int, bool,
160 bool, tsubst_flags_t);
161 static void op_error (location_t, enum tree_code, enum tree_code, tree,
162 tree, tree, bool);
163 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
164 tsubst_flags_t);
165 static void print_z_candidate (location_t, const char *, struct z_candidate *);
166 static void print_z_candidates (location_t, struct z_candidate *);
167 static tree build_this (tree);
168 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
169 static bool any_strictly_viable (struct z_candidate *);
170 static struct z_candidate *add_template_candidate
171 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
172 tree, tree, tree, int, unification_kind_t, tsubst_flags_t);
173 static struct z_candidate *add_template_candidate_real
174 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
175 tree, tree, tree, int, tree, unification_kind_t, tsubst_flags_t);
176 static void add_builtin_candidates
177 (struct z_candidate **, enum tree_code, enum tree_code,
178 tree, tree *, int, tsubst_flags_t);
179 static void add_builtin_candidate
180 (struct z_candidate **, enum tree_code, enum tree_code,
181 tree, tree, tree, tree *, tree *, int, tsubst_flags_t);
182 static bool is_complete (tree);
183 static void build_builtin_candidate
184 (struct z_candidate **, tree, tree, tree, tree *, tree *,
185 int, tsubst_flags_t);
186 static struct z_candidate *add_conv_candidate
187 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, tree,
188 tree, tsubst_flags_t);
189 static struct z_candidate *add_function_candidate
190 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
191 tree, int, tsubst_flags_t);
192 static conversion *implicit_conversion (tree, tree, tree, bool, int,
193 tsubst_flags_t);
194 static conversion *reference_binding (tree, tree, tree, bool, int,
195 tsubst_flags_t);
196 static conversion *build_conv (conversion_kind, tree, conversion *);
197 static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
198 static conversion *next_conversion (conversion *);
199 static bool is_subseq (conversion *, conversion *);
200 static conversion *maybe_handle_ref_bind (conversion **);
201 static void maybe_handle_implicit_object (conversion **);
202 static struct z_candidate *add_candidate
203 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
204 conversion **, tree, tree, int, struct rejection_reason *, int);
205 static tree source_type (conversion *);
206 static void add_warning (struct z_candidate *, struct z_candidate *);
207 static bool reference_compatible_p (tree, tree);
208 static conversion *direct_reference_binding (tree, conversion *);
209 static bool promoted_arithmetic_type_p (tree);
210 static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
211 static char *name_as_c_string (tree, tree, bool *);
212 static tree prep_operand (tree);
213 static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
214 bool, tree, tree, int, struct z_candidate **,
215 tsubst_flags_t);
216 static conversion *merge_conversion_sequences (conversion *, conversion *);
217 static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
219 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
220 NAME can take many forms... */
222 bool
223 check_dtor_name (tree basetype, tree name)
225 /* Just accept something we've already complained about. */
226 if (name == error_mark_node)
227 return true;
229 if (TREE_CODE (name) == TYPE_DECL)
230 name = TREE_TYPE (name);
231 else if (TYPE_P (name))
232 /* OK */;
233 else if (identifier_p (name))
235 if ((MAYBE_CLASS_TYPE_P (basetype)
236 || TREE_CODE (basetype) == ENUMERAL_TYPE)
237 && name == constructor_name (basetype))
238 return true;
239 else
240 name = get_type_value (name);
242 else
244 /* In the case of:
246 template <class T> struct S { ~S(); };
247 int i;
248 i.~S();
250 NAME will be a class template. */
251 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
252 return false;
255 if (!name || name == error_mark_node)
256 return false;
257 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
260 /* We want the address of a function or method. We avoid creating a
261 pointer-to-member function. */
263 tree
264 build_addr_func (tree function, tsubst_flags_t complain)
266 tree type = TREE_TYPE (function);
268 /* We have to do these by hand to avoid real pointer to member
269 functions. */
270 if (TREE_CODE (type) == METHOD_TYPE)
272 if (TREE_CODE (function) == OFFSET_REF)
274 tree object = build_address (TREE_OPERAND (function, 0));
275 return get_member_function_from_ptrfunc (&object,
276 TREE_OPERAND (function, 1),
277 complain);
279 function = build_address (function);
281 else
282 function = decay_conversion (function, complain, /*reject_builtin=*/false);
284 return function;
287 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
288 POINTER_TYPE to those. Note, pointer to member function types
289 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
290 two variants. build_call_a is the primitive taking an array of
291 arguments, while build_call_n is a wrapper that handles varargs. */
293 tree
294 build_call_n (tree function, int n, ...)
296 if (n == 0)
297 return build_call_a (function, 0, NULL);
298 else
300 tree *argarray = XALLOCAVEC (tree, n);
301 va_list ap;
302 int i;
304 va_start (ap, n);
305 for (i = 0; i < n; i++)
306 argarray[i] = va_arg (ap, tree);
307 va_end (ap);
308 return build_call_a (function, n, argarray);
312 /* Update various flags in cfun and the call itself based on what is being
313 called. Split out of build_call_a so that bot_manip can use it too. */
315 void
316 set_flags_from_callee (tree call)
318 bool nothrow;
319 tree decl = get_callee_fndecl (call);
321 /* We check both the decl and the type; a function may be known not to
322 throw without being declared throw(). */
323 nothrow = decl && TREE_NOTHROW (decl);
324 if (CALL_EXPR_FN (call))
325 nothrow |= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (call))));
326 else if (internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW)
327 nothrow = true;
329 if (!nothrow && at_function_scope_p () && cfun && cp_function_chain)
330 cp_function_chain->can_throw = 1;
332 if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
333 current_function_returns_abnormally = 1;
335 TREE_NOTHROW (call) = nothrow;
338 tree
339 build_call_a (tree function, int n, tree *argarray)
341 tree decl;
342 tree result_type;
343 tree fntype;
344 int i;
346 function = build_addr_func (function, tf_warning_or_error);
348 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
349 fntype = TREE_TYPE (TREE_TYPE (function));
350 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
351 || TREE_CODE (fntype) == METHOD_TYPE);
352 result_type = TREE_TYPE (fntype);
353 /* An rvalue has no cv-qualifiers. */
354 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
355 result_type = cv_unqualified (result_type);
357 function = build_call_array_loc (input_location,
358 result_type, function, n, argarray);
359 set_flags_from_callee (function);
361 decl = get_callee_fndecl (function);
363 if (decl && !TREE_USED (decl))
365 /* We invoke build_call directly for several library
366 functions. These may have been declared normally if
367 we're building libgcc, so we can't just check
368 DECL_ARTIFICIAL. */
369 gcc_assert (DECL_ARTIFICIAL (decl)
370 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
371 "__", 2));
372 mark_used (decl);
375 require_complete_eh_spec_types (fntype, decl);
377 TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
379 /* Don't pass empty class objects by value. This is useful
380 for tags in STL, which are used to control overload resolution.
381 We don't need to handle other cases of copying empty classes. */
382 if (! decl || ! DECL_BUILT_IN (decl))
383 for (i = 0; i < n; i++)
385 tree arg = CALL_EXPR_ARG (function, i);
386 if (is_empty_class (TREE_TYPE (arg))
387 && ! TREE_ADDRESSABLE (TREE_TYPE (arg)))
389 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
390 arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
391 CALL_EXPR_ARG (function, i) = arg;
395 return function;
398 /* New overloading code. */
400 struct z_candidate;
402 struct candidate_warning {
403 z_candidate *loser;
404 candidate_warning *next;
407 /* Information for providing diagnostics about why overloading failed. */
409 enum rejection_reason_code {
410 rr_none,
411 rr_arity,
412 rr_explicit_conversion,
413 rr_template_conversion,
414 rr_arg_conversion,
415 rr_bad_arg_conversion,
416 rr_template_unification,
417 rr_invalid_copy,
418 rr_inherited_ctor,
419 rr_constraint_failure
422 struct conversion_info {
423 /* The index of the argument, 0-based. */
424 int n_arg;
425 /* The actual argument or its type. */
426 tree from;
427 /* The type of the parameter. */
428 tree to_type;
431 struct rejection_reason {
432 enum rejection_reason_code code;
433 union {
434 /* Information about an arity mismatch. */
435 struct {
436 /* The expected number of arguments. */
437 int expected;
438 /* The actual number of arguments in the call. */
439 int actual;
440 /* Whether the call was a varargs call. */
441 bool call_varargs_p;
442 } arity;
443 /* Information about an argument conversion mismatch. */
444 struct conversion_info conversion;
445 /* Same, but for bad argument conversions. */
446 struct conversion_info bad_conversion;
447 /* Information about template unification failures. These are the
448 parameters passed to fn_type_unification. */
449 struct {
450 tree tmpl;
451 tree explicit_targs;
452 int num_targs;
453 const tree *args;
454 unsigned int nargs;
455 tree return_type;
456 unification_kind_t strict;
457 int flags;
458 } template_unification;
459 /* Information about template instantiation failures. These are the
460 parameters passed to instantiate_template. */
461 struct {
462 tree tmpl;
463 tree targs;
464 } template_instantiation;
465 } u;
468 struct z_candidate {
469 /* The FUNCTION_DECL that will be called if this candidate is
470 selected by overload resolution. */
471 tree fn;
472 /* If not NULL_TREE, the first argument to use when calling this
473 function. */
474 tree first_arg;
475 /* The rest of the arguments to use when calling this function. If
476 there are no further arguments this may be NULL or it may be an
477 empty vector. */
478 const vec<tree, va_gc> *args;
479 /* The implicit conversion sequences for each of the arguments to
480 FN. */
481 conversion **convs;
482 /* The number of implicit conversion sequences. */
483 size_t num_convs;
484 /* If FN is a user-defined conversion, the standard conversion
485 sequence from the type returned by FN to the desired destination
486 type. */
487 conversion *second_conv;
488 struct rejection_reason *reason;
489 /* If FN is a member function, the binfo indicating the path used to
490 qualify the name of FN at the call site. This path is used to
491 determine whether or not FN is accessible if it is selected by
492 overload resolution. The DECL_CONTEXT of FN will always be a
493 (possibly improper) base of this binfo. */
494 tree access_path;
495 /* If FN is a non-static member function, the binfo indicating the
496 subobject to which the `this' pointer should be converted if FN
497 is selected by overload resolution. The type pointed to by
498 the `this' pointer must correspond to the most derived class
499 indicated by the CONVERSION_PATH. */
500 tree conversion_path;
501 tree template_decl;
502 tree explicit_targs;
503 candidate_warning *warnings;
504 z_candidate *next;
505 int viable;
507 /* The flags active in add_candidate. */
508 int flags;
511 /* Returns true iff T is a null pointer constant in the sense of
512 [conv.ptr]. */
514 bool
515 null_ptr_cst_p (tree t)
517 tree type = TREE_TYPE (t);
519 /* [conv.ptr]
521 A null pointer constant is an integral constant expression
522 (_expr.const_) rvalue of integer type that evaluates to zero or
523 an rvalue of type std::nullptr_t. */
524 if (NULLPTR_TYPE_P (type))
525 return true;
527 if (cxx_dialect >= cxx11)
529 /* Core issue 903 says only literal 0 is a null pointer constant. */
530 if (TREE_CODE (type) == INTEGER_TYPE
531 && !char_type_p (type)
532 && TREE_CODE (t) == INTEGER_CST
533 && integer_zerop (t)
534 && !TREE_OVERFLOW (t))
535 return true;
537 else if (CP_INTEGRAL_TYPE_P (type))
539 t = fold_non_dependent_expr (t);
540 STRIP_NOPS (t);
541 if (integer_zerop (t) && !TREE_OVERFLOW (t))
542 return true;
545 return false;
548 /* Returns true iff T is a null member pointer value (4.11). */
550 bool
551 null_member_pointer_value_p (tree t)
553 tree type = TREE_TYPE (t);
554 if (!type)
555 return false;
556 else if (TYPE_PTRMEMFUNC_P (type))
557 return (TREE_CODE (t) == CONSTRUCTOR
558 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
559 else if (TYPE_PTRDATAMEM_P (type))
560 return integer_all_onesp (t);
561 else
562 return false;
565 /* Returns nonzero if PARMLIST consists of only default parms,
566 ellipsis, and/or undeduced parameter packs. */
568 bool
569 sufficient_parms_p (const_tree parmlist)
571 for (; parmlist && parmlist != void_list_node;
572 parmlist = TREE_CHAIN (parmlist))
573 if (!TREE_PURPOSE (parmlist)
574 && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
575 return false;
576 return true;
579 /* Allocate N bytes of memory from the conversion obstack. The memory
580 is zeroed before being returned. */
582 static void *
583 conversion_obstack_alloc (size_t n)
585 void *p;
586 if (!conversion_obstack_initialized)
588 gcc_obstack_init (&conversion_obstack);
589 conversion_obstack_initialized = true;
591 p = obstack_alloc (&conversion_obstack, n);
592 memset (p, 0, n);
593 return p;
596 /* Allocate rejection reasons. */
598 static struct rejection_reason *
599 alloc_rejection (enum rejection_reason_code code)
601 struct rejection_reason *p;
602 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
603 p->code = code;
604 return p;
607 static struct rejection_reason *
608 arity_rejection (tree first_arg, int expected, int actual)
610 struct rejection_reason *r = alloc_rejection (rr_arity);
611 int adjust = first_arg != NULL_TREE;
612 r->u.arity.expected = expected - adjust;
613 r->u.arity.actual = actual - adjust;
614 return r;
617 static struct rejection_reason *
618 arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
620 struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
621 int adjust = first_arg != NULL_TREE;
622 r->u.conversion.n_arg = n_arg - adjust;
623 r->u.conversion.from = from;
624 r->u.conversion.to_type = to;
625 return r;
628 static struct rejection_reason *
629 bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
631 struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
632 int adjust = first_arg != NULL_TREE;
633 r->u.bad_conversion.n_arg = n_arg - adjust;
634 r->u.bad_conversion.from = from;
635 r->u.bad_conversion.to_type = to;
636 return r;
639 static struct rejection_reason *
640 explicit_conversion_rejection (tree from, tree to)
642 struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
643 r->u.conversion.n_arg = 0;
644 r->u.conversion.from = from;
645 r->u.conversion.to_type = to;
646 return r;
649 static struct rejection_reason *
650 template_conversion_rejection (tree from, tree to)
652 struct rejection_reason *r = alloc_rejection (rr_template_conversion);
653 r->u.conversion.n_arg = 0;
654 r->u.conversion.from = from;
655 r->u.conversion.to_type = to;
656 return r;
659 static struct rejection_reason *
660 template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
661 const tree *args, unsigned int nargs,
662 tree return_type, unification_kind_t strict,
663 int flags)
665 size_t args_n_bytes = sizeof (*args) * nargs;
666 tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
667 struct rejection_reason *r = alloc_rejection (rr_template_unification);
668 r->u.template_unification.tmpl = tmpl;
669 r->u.template_unification.explicit_targs = explicit_targs;
670 r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
671 /* Copy args to our own storage. */
672 memcpy (args1, args, args_n_bytes);
673 r->u.template_unification.args = args1;
674 r->u.template_unification.nargs = nargs;
675 r->u.template_unification.return_type = return_type;
676 r->u.template_unification.strict = strict;
677 r->u.template_unification.flags = flags;
678 return r;
681 static struct rejection_reason *
682 template_unification_error_rejection (void)
684 return alloc_rejection (rr_template_unification);
687 static struct rejection_reason *
688 invalid_copy_with_fn_template_rejection (void)
690 struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
691 return r;
694 static struct rejection_reason *
695 inherited_ctor_rejection (void)
697 struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
698 return r;
701 // Build a constraint failure record, saving information into the
702 // template_instantiation field of the rejection. If FN is not a template
703 // declaration, the TMPL member is the FN declaration and TARGS is empty.
705 static struct rejection_reason *
706 constraint_failure (tree fn)
708 struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
709 if (tree ti = DECL_TEMPLATE_INFO (fn))
711 r->u.template_instantiation.tmpl = TI_TEMPLATE (ti);
712 r->u.template_instantiation.targs = TI_ARGS (ti);
714 else
716 r->u.template_instantiation.tmpl = fn;
717 r->u.template_instantiation.targs = NULL_TREE;
719 return r;
722 /* Dynamically allocate a conversion. */
724 static conversion *
725 alloc_conversion (conversion_kind kind)
727 conversion *c;
728 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
729 c->kind = kind;
730 return c;
733 /* Make sure that all memory on the conversion obstack has been
734 freed. */
736 void
737 validate_conversion_obstack (void)
739 if (conversion_obstack_initialized)
740 gcc_assert ((obstack_next_free (&conversion_obstack)
741 == obstack_base (&conversion_obstack)));
744 /* Dynamically allocate an array of N conversions. */
746 static conversion **
747 alloc_conversions (size_t n)
749 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
752 static conversion *
753 build_conv (conversion_kind code, tree type, conversion *from)
755 conversion *t;
756 conversion_rank rank = CONVERSION_RANK (from);
758 /* Note that the caller is responsible for filling in t->cand for
759 user-defined conversions. */
760 t = alloc_conversion (code);
761 t->type = type;
762 t->u.next = from;
764 switch (code)
766 case ck_ptr:
767 case ck_pmem:
768 case ck_base:
769 case ck_std:
770 if (rank < cr_std)
771 rank = cr_std;
772 break;
774 case ck_qual:
775 case ck_fnptr:
776 if (rank < cr_exact)
777 rank = cr_exact;
778 break;
780 default:
781 break;
783 t->rank = rank;
784 t->user_conv_p = (code == ck_user || from->user_conv_p);
785 t->bad_p = from->bad_p;
786 t->base_p = false;
787 return t;
790 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
791 specialization of std::initializer_list<T>, if such a conversion is
792 possible. */
794 static conversion *
795 build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
797 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
798 unsigned len = CONSTRUCTOR_NELTS (ctor);
799 conversion **subconvs = alloc_conversions (len);
800 conversion *t;
801 unsigned i;
802 tree val;
804 /* Within a list-initialization we can have more user-defined
805 conversions. */
806 flags &= ~LOOKUP_NO_CONVERSION;
807 /* But no narrowing conversions. */
808 flags |= LOOKUP_NO_NARROWING;
810 /* Can't make an array of these types. */
811 if (TREE_CODE (elttype) == REFERENCE_TYPE
812 || TREE_CODE (elttype) == FUNCTION_TYPE
813 || VOID_TYPE_P (elttype))
814 return NULL;
816 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
818 conversion *sub
819 = implicit_conversion (elttype, TREE_TYPE (val), val,
820 false, flags, complain);
821 if (sub == NULL)
822 return NULL;
824 subconvs[i] = sub;
827 t = alloc_conversion (ck_list);
828 t->type = type;
829 t->u.list = subconvs;
830 t->rank = cr_exact;
832 for (i = 0; i < len; ++i)
834 conversion *sub = subconvs[i];
835 if (sub->rank > t->rank)
836 t->rank = sub->rank;
837 if (sub->user_conv_p)
838 t->user_conv_p = true;
839 if (sub->bad_p)
840 t->bad_p = true;
843 return t;
846 /* Return the next conversion of the conversion chain (if applicable),
847 or NULL otherwise. Please use this function instead of directly
848 accessing fields of struct conversion. */
850 static conversion *
851 next_conversion (conversion *conv)
853 if (conv == NULL
854 || conv->kind == ck_identity
855 || conv->kind == ck_ambig
856 || conv->kind == ck_list)
857 return NULL;
858 return conv->u.next;
861 /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
862 is a valid aggregate initializer for array type ATYPE. */
864 static bool
865 can_convert_array (tree atype, tree ctor, int flags, tsubst_flags_t complain)
867 unsigned i;
868 tree elttype = TREE_TYPE (atype);
869 for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
871 tree val = CONSTRUCTOR_ELT (ctor, i)->value;
872 bool ok;
873 if (TREE_CODE (elttype) == ARRAY_TYPE
874 && TREE_CODE (val) == CONSTRUCTOR)
875 ok = can_convert_array (elttype, val, flags, complain);
876 else
877 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
878 complain);
879 if (!ok)
880 return false;
882 return true;
885 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
886 aggregate class, if such a conversion is possible. */
888 static conversion *
889 build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
891 unsigned HOST_WIDE_INT i = 0;
892 conversion *c;
893 tree field = next_initializable_field (TYPE_FIELDS (type));
894 tree empty_ctor = NULL_TREE;
896 /* We already called reshape_init in implicit_conversion. */
898 /* The conversions within the init-list aren't affected by the enclosing
899 context; they're always simple copy-initialization. */
900 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
902 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
904 tree ftype = TREE_TYPE (field);
905 tree val;
906 bool ok;
908 if (i < CONSTRUCTOR_NELTS (ctor))
909 val = CONSTRUCTOR_ELT (ctor, i)->value;
910 else if (DECL_INITIAL (field))
911 val = get_nsdmi (field, /*ctor*/false, complain);
912 else if (TREE_CODE (ftype) == REFERENCE_TYPE)
913 /* Value-initialization of reference is ill-formed. */
914 return NULL;
915 else
917 if (empty_ctor == NULL_TREE)
918 empty_ctor = build_constructor (init_list_type_node, NULL);
919 val = empty_ctor;
921 ++i;
923 if (TREE_CODE (ftype) == ARRAY_TYPE
924 && TREE_CODE (val) == CONSTRUCTOR)
925 ok = can_convert_array (ftype, val, flags, complain);
926 else
927 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
928 complain);
930 if (!ok)
931 return NULL;
933 if (TREE_CODE (type) == UNION_TYPE)
934 break;
937 if (i < CONSTRUCTOR_NELTS (ctor))
938 return NULL;
940 c = alloc_conversion (ck_aggr);
941 c->type = type;
942 c->rank = cr_exact;
943 c->user_conv_p = true;
944 c->check_narrowing = true;
945 c->u.next = NULL;
946 return c;
949 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
950 array type, if such a conversion is possible. */
952 static conversion *
953 build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
955 conversion *c;
956 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
957 tree elttype = TREE_TYPE (type);
958 unsigned i;
959 tree val;
960 bool bad = false;
961 bool user = false;
962 enum conversion_rank rank = cr_exact;
964 /* We might need to propagate the size from the element to the array. */
965 complete_type (type);
967 if (TYPE_DOMAIN (type)
968 && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
970 unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
971 if (alen < len)
972 return NULL;
975 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
977 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
979 conversion *sub
980 = implicit_conversion (elttype, TREE_TYPE (val), val,
981 false, flags, complain);
982 if (sub == NULL)
983 return NULL;
985 if (sub->rank > rank)
986 rank = sub->rank;
987 if (sub->user_conv_p)
988 user = true;
989 if (sub->bad_p)
990 bad = true;
993 c = alloc_conversion (ck_aggr);
994 c->type = type;
995 c->rank = rank;
996 c->user_conv_p = user;
997 c->bad_p = bad;
998 c->u.next = NULL;
999 return c;
1002 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1003 complex type, if such a conversion is possible. */
1005 static conversion *
1006 build_complex_conv (tree type, tree ctor, int flags,
1007 tsubst_flags_t complain)
1009 conversion *c;
1010 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1011 tree elttype = TREE_TYPE (type);
1012 unsigned i;
1013 tree val;
1014 bool bad = false;
1015 bool user = false;
1016 enum conversion_rank rank = cr_exact;
1018 if (len != 2)
1019 return NULL;
1021 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1023 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
1025 conversion *sub
1026 = implicit_conversion (elttype, TREE_TYPE (val), val,
1027 false, flags, complain);
1028 if (sub == NULL)
1029 return NULL;
1031 if (sub->rank > rank)
1032 rank = sub->rank;
1033 if (sub->user_conv_p)
1034 user = true;
1035 if (sub->bad_p)
1036 bad = true;
1039 c = alloc_conversion (ck_aggr);
1040 c->type = type;
1041 c->rank = rank;
1042 c->user_conv_p = user;
1043 c->bad_p = bad;
1044 c->u.next = NULL;
1045 return c;
1048 /* Build a representation of the identity conversion from EXPR to
1049 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1051 static conversion *
1052 build_identity_conv (tree type, tree expr)
1054 conversion *c;
1056 c = alloc_conversion (ck_identity);
1057 c->type = type;
1058 c->u.expr = expr;
1060 return c;
1063 /* Converting from EXPR to TYPE was ambiguous in the sense that there
1064 were multiple user-defined conversions to accomplish the job.
1065 Build a conversion that indicates that ambiguity. */
1067 static conversion *
1068 build_ambiguous_conv (tree type, tree expr)
1070 conversion *c;
1072 c = alloc_conversion (ck_ambig);
1073 c->type = type;
1074 c->u.expr = expr;
1076 return c;
1079 tree
1080 strip_top_quals (tree t)
1082 if (TREE_CODE (t) == ARRAY_TYPE)
1083 return t;
1084 return cp_build_qualified_type (t, 0);
1087 /* Returns the standard conversion path (see [conv]) from type FROM to type
1088 TO, if any. For proper handling of null pointer constants, you must
1089 also pass the expression EXPR to convert from. If C_CAST_P is true,
1090 this conversion is coming from a C-style cast. */
1092 static conversion *
1093 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1094 int flags, tsubst_flags_t complain)
1096 enum tree_code fcode, tcode;
1097 conversion *conv;
1098 bool fromref = false;
1099 tree qualified_to;
1101 to = non_reference (to);
1102 if (TREE_CODE (from) == REFERENCE_TYPE)
1104 fromref = true;
1105 from = TREE_TYPE (from);
1107 qualified_to = to;
1108 to = strip_top_quals (to);
1109 from = strip_top_quals (from);
1111 if (expr && type_unknown_p (expr))
1113 if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1115 tsubst_flags_t tflags = tf_conv;
1116 expr = instantiate_type (to, expr, tflags);
1117 if (expr == error_mark_node)
1118 return NULL;
1119 from = TREE_TYPE (expr);
1121 else if (TREE_CODE (to) == BOOLEAN_TYPE)
1123 /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1124 expr = resolve_nondeduced_context (expr, complain);
1125 from = TREE_TYPE (expr);
1129 fcode = TREE_CODE (from);
1130 tcode = TREE_CODE (to);
1132 conv = build_identity_conv (from, expr);
1133 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1135 from = type_decays_to (from);
1136 fcode = TREE_CODE (from);
1137 conv = build_conv (ck_lvalue, from, conv);
1139 /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1140 obvalue_p) seems odd, since it's already a prvalue, but that's how we
1141 express the copy constructor call required by copy-initialization. */
1142 else if (fromref || (expr && obvalue_p (expr)))
1144 if (expr)
1146 tree bitfield_type;
1147 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1148 if (bitfield_type)
1150 from = strip_top_quals (bitfield_type);
1151 fcode = TREE_CODE (from);
1154 conv = build_conv (ck_rvalue, from, conv);
1155 if (flags & LOOKUP_PREFER_RVALUE)
1156 /* Tell convert_like_real to set LOOKUP_PREFER_RVALUE. */
1157 conv->rvaluedness_matches_p = true;
1160 /* Allow conversion between `__complex__' data types. */
1161 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1163 /* The standard conversion sequence to convert FROM to TO is
1164 the standard conversion sequence to perform componentwise
1165 conversion. */
1166 conversion *part_conv = standard_conversion
1167 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1168 complain);
1170 if (part_conv)
1172 conv = build_conv (part_conv->kind, to, conv);
1173 conv->rank = part_conv->rank;
1175 else
1176 conv = NULL;
1178 return conv;
1181 if (same_type_p (from, to))
1183 if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1184 conv->type = qualified_to;
1185 return conv;
1188 /* [conv.ptr]
1189 A null pointer constant can be converted to a pointer type; ... A
1190 null pointer constant of integral type can be converted to an
1191 rvalue of type std::nullptr_t. */
1192 if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1193 || NULLPTR_TYPE_P (to))
1194 && ((expr && null_ptr_cst_p (expr))
1195 || NULLPTR_TYPE_P (from)))
1196 conv = build_conv (ck_std, to, conv);
1197 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1198 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1200 /* For backwards brain damage compatibility, allow interconversion of
1201 pointers and integers with a pedwarn. */
1202 conv = build_conv (ck_std, to, conv);
1203 conv->bad_p = true;
1205 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1207 /* For backwards brain damage compatibility, allow interconversion of
1208 enums and integers with a pedwarn. */
1209 conv = build_conv (ck_std, to, conv);
1210 conv->bad_p = true;
1212 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1213 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1215 tree to_pointee;
1216 tree from_pointee;
1218 if (tcode == POINTER_TYPE)
1220 to_pointee = TREE_TYPE (to);
1221 from_pointee = TREE_TYPE (from);
1223 /* Since this is the target of a pointer, it can't have function
1224 qualifiers, so any TYPE_QUALS must be for attributes const or
1225 noreturn. Strip them. */
1226 if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1227 && TYPE_QUALS (to_pointee))
1228 to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1229 if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1230 && TYPE_QUALS (from_pointee))
1231 from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1233 else
1235 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1236 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1239 if (tcode == POINTER_TYPE
1240 && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1241 to_pointee))
1243 else if (VOID_TYPE_P (to_pointee)
1244 && !TYPE_PTRDATAMEM_P (from)
1245 && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1247 tree nfrom = TREE_TYPE (from);
1248 /* Don't try to apply restrict to void. */
1249 int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1250 from_pointee = cp_build_qualified_type (void_type_node, quals);
1251 from = build_pointer_type (from_pointee);
1252 conv = build_conv (ck_ptr, from, conv);
1254 else if (TYPE_PTRDATAMEM_P (from))
1256 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1257 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1259 if (same_type_p (fbase, tbase))
1260 /* No base conversion needed. */;
1261 else if (DERIVED_FROM_P (fbase, tbase)
1262 && (same_type_ignoring_top_level_qualifiers_p
1263 (from_pointee, to_pointee)))
1265 from = build_ptrmem_type (tbase, from_pointee);
1266 conv = build_conv (ck_pmem, from, conv);
1268 else
1269 return NULL;
1271 else if (CLASS_TYPE_P (from_pointee)
1272 && CLASS_TYPE_P (to_pointee)
1273 /* [conv.ptr]
1275 An rvalue of type "pointer to cv D," where D is a
1276 class type, can be converted to an rvalue of type
1277 "pointer to cv B," where B is a base class (clause
1278 _class.derived_) of D. If B is an inaccessible
1279 (clause _class.access_) or ambiguous
1280 (_class.member.lookup_) base class of D, a program
1281 that necessitates this conversion is ill-formed.
1282 Therefore, we use DERIVED_FROM_P, and do not check
1283 access or uniqueness. */
1284 && DERIVED_FROM_P (to_pointee, from_pointee))
1286 from_pointee
1287 = cp_build_qualified_type (to_pointee,
1288 cp_type_quals (from_pointee));
1289 from = build_pointer_type (from_pointee);
1290 conv = build_conv (ck_ptr, from, conv);
1291 conv->base_p = true;
1294 if (same_type_p (from, to))
1295 /* OK */;
1296 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
1297 /* In a C-style cast, we ignore CV-qualification because we
1298 are allowed to perform a static_cast followed by a
1299 const_cast. */
1300 conv = build_conv (ck_qual, to, conv);
1301 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1302 conv = build_conv (ck_qual, to, conv);
1303 else if (expr && string_conv_p (to, expr, 0))
1304 /* converting from string constant to char *. */
1305 conv = build_conv (ck_qual, to, conv);
1306 else if (fnptr_conv_p (to, from))
1307 conv = build_conv (ck_fnptr, to, conv);
1308 /* Allow conversions among compatible ObjC pointer types (base
1309 conversions have been already handled above). */
1310 else if (c_dialect_objc ()
1311 && objc_compare_types (to, from, -4, NULL_TREE))
1312 conv = build_conv (ck_ptr, to, conv);
1313 else if (ptr_reasonably_similar (to_pointee, from_pointee))
1315 conv = build_conv (ck_ptr, to, conv);
1316 conv->bad_p = true;
1318 else
1319 return NULL;
1321 from = to;
1323 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1325 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1326 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1327 tree fbase = class_of_this_parm (fromfn);
1328 tree tbase = class_of_this_parm (tofn);
1330 if (!DERIVED_FROM_P (fbase, tbase))
1331 return NULL;
1333 tree fstat = static_fn_type (fromfn);
1334 tree tstat = static_fn_type (tofn);
1335 if (same_type_p (tstat, fstat)
1336 || fnptr_conv_p (tstat, fstat))
1337 /* OK */;
1338 else
1339 return NULL;
1341 if (!same_type_p (fbase, tbase))
1343 from = build_memfn_type (fstat,
1344 tbase,
1345 cp_type_quals (tbase),
1346 type_memfn_rqual (tofn));
1347 from = build_ptrmemfunc_type (build_pointer_type (from));
1348 conv = build_conv (ck_pmem, from, conv);
1349 conv->base_p = true;
1351 if (fnptr_conv_p (tstat, fstat))
1352 conv = build_conv (ck_fnptr, to, conv);
1354 else if (tcode == BOOLEAN_TYPE)
1356 /* [conv.bool]
1358 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1359 to member type can be converted to a prvalue of type bool. ...
1360 For direct-initialization (8.5 [dcl.init]), a prvalue of type
1361 std::nullptr_t can be converted to a prvalue of type bool; */
1362 if (ARITHMETIC_TYPE_P (from)
1363 || UNSCOPED_ENUM_P (from)
1364 || fcode == POINTER_TYPE
1365 || TYPE_PTRMEM_P (from)
1366 || NULLPTR_TYPE_P (from))
1368 conv = build_conv (ck_std, to, conv);
1369 if (fcode == POINTER_TYPE
1370 || TYPE_PTRDATAMEM_P (from)
1371 || (TYPE_PTRMEMFUNC_P (from)
1372 && conv->rank < cr_pbool)
1373 || NULLPTR_TYPE_P (from))
1374 conv->rank = cr_pbool;
1375 if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1376 conv->bad_p = true;
1377 return conv;
1380 return NULL;
1382 /* We don't check for ENUMERAL_TYPE here because there are no standard
1383 conversions to enum type. */
1384 /* As an extension, allow conversion to complex type. */
1385 else if (ARITHMETIC_TYPE_P (to))
1387 if (! (INTEGRAL_CODE_P (fcode)
1388 || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1389 || SCOPED_ENUM_P (from))
1390 return NULL;
1391 conv = build_conv (ck_std, to, conv);
1393 /* Give this a better rank if it's a promotion. */
1394 if (same_type_p (to, type_promotes_to (from))
1395 && next_conversion (conv)->rank <= cr_promotion)
1396 conv->rank = cr_promotion;
1398 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1399 && vector_types_convertible_p (from, to, false))
1400 return build_conv (ck_std, to, conv);
1401 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1402 && is_properly_derived_from (from, to))
1404 if (conv->kind == ck_rvalue)
1405 conv = next_conversion (conv);
1406 conv = build_conv (ck_base, to, conv);
1407 /* The derived-to-base conversion indicates the initialization
1408 of a parameter with base type from an object of a derived
1409 type. A temporary object is created to hold the result of
1410 the conversion unless we're binding directly to a reference. */
1411 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1413 else
1414 return NULL;
1416 if (flags & LOOKUP_NO_NARROWING)
1417 conv->check_narrowing = true;
1419 return conv;
1422 /* Returns nonzero if T1 is reference-related to T2. */
1424 bool
1425 reference_related_p (tree t1, tree t2)
1427 if (t1 == error_mark_node || t2 == error_mark_node)
1428 return false;
1430 t1 = TYPE_MAIN_VARIANT (t1);
1431 t2 = TYPE_MAIN_VARIANT (t2);
1433 /* [dcl.init.ref]
1435 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1436 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
1437 of T2. */
1438 return (same_type_p (t1, t2)
1439 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1440 && DERIVED_FROM_P (t1, t2)));
1443 /* Returns nonzero if T1 is reference-compatible with T2. */
1445 static bool
1446 reference_compatible_p (tree t1, tree t2)
1448 /* [dcl.init.ref]
1450 "cv1 T1" is reference compatible with "cv2 T2" if
1451 * T1 is reference-related to T2 or
1452 * T2 is "noexcept function" and T1 is "function", where the
1453 function types are otherwise the same,
1454 and cv1 is the same cv-qualification as, or greater cv-qualification
1455 than, cv2. */
1456 return ((reference_related_p (t1, t2)
1457 || fnptr_conv_p (t1, t2))
1458 && at_least_as_qualified_p (t1, t2));
1461 /* A reference of the indicated TYPE is being bound directly to the
1462 expression represented by the implicit conversion sequence CONV.
1463 Return a conversion sequence for this binding. */
1465 static conversion *
1466 direct_reference_binding (tree type, conversion *conv)
1468 tree t;
1470 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1471 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1473 t = TREE_TYPE (type);
1475 /* [over.ics.rank]
1477 When a parameter of reference type binds directly
1478 (_dcl.init.ref_) to an argument expression, the implicit
1479 conversion sequence is the identity conversion, unless the
1480 argument expression has a type that is a derived class of the
1481 parameter type, in which case the implicit conversion sequence is
1482 a derived-to-base Conversion.
1484 If the parameter binds directly to the result of applying a
1485 conversion function to the argument expression, the implicit
1486 conversion sequence is a user-defined conversion sequence
1487 (_over.ics.user_), with the second standard conversion sequence
1488 either an identity conversion or, if the conversion function
1489 returns an entity of a type that is a derived class of the
1490 parameter type, a derived-to-base conversion. */
1491 if (is_properly_derived_from (conv->type, t))
1493 /* Represent the derived-to-base conversion. */
1494 conv = build_conv (ck_base, t, conv);
1495 /* We will actually be binding to the base-class subobject in
1496 the derived class, so we mark this conversion appropriately.
1497 That way, convert_like knows not to generate a temporary. */
1498 conv->need_temporary_p = false;
1500 return build_conv (ck_ref_bind, type, conv);
1503 /* Returns the conversion path from type FROM to reference type TO for
1504 purposes of reference binding. For lvalue binding, either pass a
1505 reference type to FROM or an lvalue expression to EXPR. If the
1506 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1507 the conversion returned. If C_CAST_P is true, this
1508 conversion is coming from a C-style cast. */
1510 static conversion *
1511 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1512 tsubst_flags_t complain)
1514 conversion *conv = NULL;
1515 tree to = TREE_TYPE (rto);
1516 tree from = rfrom;
1517 tree tfrom;
1518 bool related_p;
1519 bool compatible_p;
1520 cp_lvalue_kind gl_kind;
1521 bool is_lvalue;
1523 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1525 expr = instantiate_type (to, expr, tf_none);
1526 if (expr == error_mark_node)
1527 return NULL;
1528 from = TREE_TYPE (expr);
1531 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1533 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1534 /* DR 1288: Otherwise, if the initializer list has a single element
1535 of type E and ... [T's] referenced type is reference-related to E,
1536 the object or reference is initialized from that element... */
1537 if (CONSTRUCTOR_NELTS (expr) == 1)
1539 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1540 if (error_operand_p (elt))
1541 return NULL;
1542 tree etype = TREE_TYPE (elt);
1543 if (reference_related_p (to, etype))
1545 expr = elt;
1546 from = etype;
1547 goto skip;
1550 /* Otherwise, if T is a reference type, a prvalue temporary of the
1551 type referenced by T is copy-list-initialized or
1552 direct-list-initialized, depending on the kind of initialization
1553 for the reference, and the reference is bound to that temporary. */
1554 conv = implicit_conversion (to, from, expr, c_cast_p,
1555 flags|LOOKUP_NO_TEMP_BIND, complain);
1556 skip:;
1559 if (TREE_CODE (from) == REFERENCE_TYPE)
1561 from = TREE_TYPE (from);
1562 if (!TYPE_REF_IS_RVALUE (rfrom)
1563 || TREE_CODE (from) == FUNCTION_TYPE)
1564 gl_kind = clk_ordinary;
1565 else
1566 gl_kind = clk_rvalueref;
1568 else if (expr)
1569 gl_kind = lvalue_kind (expr);
1570 else if (CLASS_TYPE_P (from)
1571 || TREE_CODE (from) == ARRAY_TYPE)
1572 gl_kind = clk_class;
1573 else
1574 gl_kind = clk_none;
1576 /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1577 if ((flags & LOOKUP_NO_TEMP_BIND)
1578 && (gl_kind & clk_class))
1579 gl_kind = clk_none;
1581 /* Same mask as real_lvalue_p. */
1582 is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1584 tfrom = from;
1585 if ((gl_kind & clk_bitfield) != 0)
1586 tfrom = unlowered_expr_type (expr);
1588 /* Figure out whether or not the types are reference-related and
1589 reference compatible. We have to do this after stripping
1590 references from FROM. */
1591 related_p = reference_related_p (to, tfrom);
1592 /* If this is a C cast, first convert to an appropriately qualified
1593 type, so that we can later do a const_cast to the desired type. */
1594 if (related_p && c_cast_p
1595 && !at_least_as_qualified_p (to, tfrom))
1596 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1597 compatible_p = reference_compatible_p (to, tfrom);
1599 /* Directly bind reference when target expression's type is compatible with
1600 the reference and expression is an lvalue. In DR391, the wording in
1601 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1602 const and rvalue references to rvalues of compatible class type.
1603 We should also do direct bindings for non-class xvalues. */
1604 if ((related_p || compatible_p) && gl_kind)
1606 /* [dcl.init.ref]
1608 If the initializer expression
1610 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1611 is reference-compatible with "cv2 T2,"
1613 the reference is bound directly to the initializer expression
1614 lvalue.
1616 [...]
1617 If the initializer expression is an rvalue, with T2 a class type,
1618 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1619 is bound to the object represented by the rvalue or to a sub-object
1620 within that object. */
1622 conv = build_identity_conv (tfrom, expr);
1623 conv = direct_reference_binding (rto, conv);
1625 if (TREE_CODE (rfrom) == REFERENCE_TYPE)
1626 /* Handle rvalue reference to function properly. */
1627 conv->rvaluedness_matches_p
1628 = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1629 else
1630 conv->rvaluedness_matches_p
1631 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1633 if ((gl_kind & clk_bitfield) != 0
1634 || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1635 /* For the purposes of overload resolution, we ignore the fact
1636 this expression is a bitfield or packed field. (In particular,
1637 [over.ics.ref] says specifically that a function with a
1638 non-const reference parameter is viable even if the
1639 argument is a bitfield.)
1641 However, when we actually call the function we must create
1642 a temporary to which to bind the reference. If the
1643 reference is volatile, or isn't const, then we cannot make
1644 a temporary, so we just issue an error when the conversion
1645 actually occurs. */
1646 conv->need_temporary_p = true;
1648 /* Don't allow binding of lvalues (other than function lvalues) to
1649 rvalue references. */
1650 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1651 && TREE_CODE (to) != FUNCTION_TYPE)
1652 conv->bad_p = true;
1654 /* Nor the reverse. */
1655 if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
1656 && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
1657 || (flags & LOOKUP_NO_RVAL_BIND))
1658 && TREE_CODE (to) != FUNCTION_TYPE)
1659 conv->bad_p = true;
1661 if (!compatible_p)
1662 conv->bad_p = true;
1664 return conv;
1666 /* [class.conv.fct] A conversion function is never used to convert a
1667 (possibly cv-qualified) object to the (possibly cv-qualified) same
1668 object type (or a reference to it), to a (possibly cv-qualified) base
1669 class of that type (or a reference to it).... */
1670 else if (CLASS_TYPE_P (from) && !related_p
1671 && !(flags & LOOKUP_NO_CONVERSION))
1673 /* [dcl.init.ref]
1675 If the initializer expression
1677 -- has a class type (i.e., T2 is a class type) can be
1678 implicitly converted to an lvalue of type "cv3 T3," where
1679 "cv1 T1" is reference-compatible with "cv3 T3". (this
1680 conversion is selected by enumerating the applicable
1681 conversion functions (_over.match.ref_) and choosing the
1682 best one through overload resolution. (_over.match_).
1684 the reference is bound to the lvalue result of the conversion
1685 in the second case. */
1686 z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
1687 complain);
1688 if (cand)
1689 return cand->second_conv;
1692 /* From this point on, we conceptually need temporaries, even if we
1693 elide them. Only the cases above are "direct bindings". */
1694 if (flags & LOOKUP_NO_TEMP_BIND)
1695 return NULL;
1697 /* [over.ics.rank]
1699 When a parameter of reference type is not bound directly to an
1700 argument expression, the conversion sequence is the one required
1701 to convert the argument expression to the underlying type of the
1702 reference according to _over.best.ics_. Conceptually, this
1703 conversion sequence corresponds to copy-initializing a temporary
1704 of the underlying type with the argument expression. Any
1705 difference in top-level cv-qualification is subsumed by the
1706 initialization itself and does not constitute a conversion. */
1708 /* [dcl.init.ref]
1710 Otherwise, the reference shall be an lvalue reference to a
1711 non-volatile const type, or the reference shall be an rvalue
1712 reference.
1714 We try below to treat this as a bad conversion to improve diagnostics,
1715 but if TO is an incomplete class, we need to reject this conversion
1716 now to avoid unnecessary instantiation. */
1717 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto)
1718 && !COMPLETE_TYPE_P (to))
1719 return NULL;
1721 /* We're generating a temporary now, but don't bind any more in the
1722 conversion (specifically, don't slice the temporary returned by a
1723 conversion operator). */
1724 flags |= LOOKUP_NO_TEMP_BIND;
1726 /* Core issue 899: When [copy-]initializing a temporary to be bound
1727 to the first parameter of a copy constructor (12.8) called with
1728 a single argument in the context of direct-initialization,
1729 explicit conversion functions are also considered.
1731 So don't set LOOKUP_ONLYCONVERTING in that case. */
1732 if (!(flags & LOOKUP_COPY_PARM))
1733 flags |= LOOKUP_ONLYCONVERTING;
1735 if (!conv)
1736 conv = implicit_conversion (to, from, expr, c_cast_p,
1737 flags, complain);
1738 if (!conv)
1739 return NULL;
1741 if (conv->user_conv_p)
1743 /* If initializing the temporary used a conversion function,
1744 recalculate the second conversion sequence. */
1745 for (conversion *t = conv; t; t = next_conversion (t))
1746 if (t->kind == ck_user
1747 && DECL_CONV_FN_P (t->cand->fn))
1749 tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
1750 int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
1751 conversion *new_second
1752 = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
1753 sflags, complain);
1754 if (!new_second)
1755 return NULL;
1756 return merge_conversion_sequences (t, new_second);
1760 conv = build_conv (ck_ref_bind, rto, conv);
1761 /* This reference binding, unlike those above, requires the
1762 creation of a temporary. */
1763 conv->need_temporary_p = true;
1764 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1766 /* [dcl.init.ref]
1768 Otherwise, the reference shall be an lvalue reference to a
1769 non-volatile const type, or the reference shall be an rvalue
1770 reference. */
1771 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1772 conv->bad_p = true;
1774 /* [dcl.init.ref]
1776 Otherwise, a temporary of type "cv1 T1" is created and
1777 initialized from the initializer expression using the rules for a
1778 non-reference copy initialization. If T1 is reference-related to
1779 T2, cv1 must be the same cv-qualification as, or greater
1780 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1781 if (related_p && !at_least_as_qualified_p (to, from))
1782 conv->bad_p = true;
1784 return conv;
1787 /* Returns the implicit conversion sequence (see [over.ics]) from type
1788 FROM to type TO. The optional expression EXPR may affect the
1789 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1790 true, this conversion is coming from a C-style cast. */
1792 static conversion *
1793 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1794 int flags, tsubst_flags_t complain)
1796 conversion *conv;
1798 if (from == error_mark_node || to == error_mark_node
1799 || expr == error_mark_node)
1800 return NULL;
1802 /* Other flags only apply to the primary function in overload
1803 resolution, or after we've chosen one. */
1804 flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
1805 |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE
1806 |LOOKUP_NO_NARROWING|LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL);
1808 /* FIXME: actually we don't want warnings either, but we can't just
1809 have 'complain &= ~(tf_warning|tf_error)' because it would cause
1810 the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
1811 We really ought not to issue that warning until we've committed
1812 to that conversion. */
1813 complain &= ~tf_error;
1815 /* Call reshape_init early to remove redundant braces. */
1816 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)
1817 && CLASS_TYPE_P (to)
1818 && COMPLETE_TYPE_P (complete_type (to))
1819 && !CLASSTYPE_NON_AGGREGATE (to))
1821 expr = reshape_init (to, expr, complain);
1822 if (expr == error_mark_node)
1823 return NULL;
1824 from = TREE_TYPE (expr);
1827 if (TREE_CODE (to) == REFERENCE_TYPE)
1828 conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
1829 else
1830 conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
1832 if (conv)
1833 return conv;
1835 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1837 if (is_std_init_list (to))
1838 return build_list_conv (to, expr, flags, complain);
1840 /* As an extension, allow list-initialization of _Complex. */
1841 if (TREE_CODE (to) == COMPLEX_TYPE)
1843 conv = build_complex_conv (to, expr, flags, complain);
1844 if (conv)
1845 return conv;
1848 /* Allow conversion from an initializer-list with one element to a
1849 scalar type. */
1850 if (SCALAR_TYPE_P (to))
1852 int nelts = CONSTRUCTOR_NELTS (expr);
1853 tree elt;
1855 if (nelts == 0)
1856 elt = build_value_init (to, tf_none);
1857 else if (nelts == 1)
1858 elt = CONSTRUCTOR_ELT (expr, 0)->value;
1859 else
1860 elt = error_mark_node;
1862 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
1863 c_cast_p, flags, complain);
1864 if (conv)
1866 conv->check_narrowing = true;
1867 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
1868 /* Too many levels of braces, i.e. '{{1}}'. */
1869 conv->bad_p = true;
1870 return conv;
1873 else if (TREE_CODE (to) == ARRAY_TYPE)
1874 return build_array_conv (to, expr, flags, complain);
1877 if (expr != NULL_TREE
1878 && (MAYBE_CLASS_TYPE_P (from)
1879 || MAYBE_CLASS_TYPE_P (to))
1880 && (flags & LOOKUP_NO_CONVERSION) == 0)
1882 struct z_candidate *cand;
1884 if (CLASS_TYPE_P (to)
1885 && BRACE_ENCLOSED_INITIALIZER_P (expr)
1886 && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
1887 return build_aggr_conv (to, expr, flags, complain);
1889 cand = build_user_type_conversion_1 (to, expr, flags, complain);
1890 if (cand)
1892 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
1893 && CONSTRUCTOR_NELTS (expr) == 1
1894 && !is_list_ctor (cand->fn))
1896 /* "If C is not an initializer-list constructor and the
1897 initializer list has a single element of type cv U, where U is
1898 X or a class derived from X, the implicit conversion sequence
1899 has Exact Match rank if U is X, or Conversion rank if U is
1900 derived from X." */
1901 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1902 tree elttype = TREE_TYPE (elt);
1903 if (reference_related_p (to, elttype))
1904 return implicit_conversion (to, elttype, elt,
1905 c_cast_p, flags, complain);
1907 conv = cand->second_conv;
1910 /* We used to try to bind a reference to a temporary here, but that
1911 is now handled after the recursive call to this function at the end
1912 of reference_binding. */
1913 return conv;
1916 return NULL;
1919 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1920 functions. ARGS will not be changed until a single candidate is
1921 selected. */
1923 static struct z_candidate *
1924 add_candidate (struct z_candidate **candidates,
1925 tree fn, tree first_arg, const vec<tree, va_gc> *args,
1926 size_t num_convs, conversion **convs,
1927 tree access_path, tree conversion_path,
1928 int viable, struct rejection_reason *reason,
1929 int flags)
1931 struct z_candidate *cand = (struct z_candidate *)
1932 conversion_obstack_alloc (sizeof (struct z_candidate));
1934 cand->fn = fn;
1935 cand->first_arg = first_arg;
1936 cand->args = args;
1937 cand->convs = convs;
1938 cand->num_convs = num_convs;
1939 cand->access_path = access_path;
1940 cand->conversion_path = conversion_path;
1941 cand->viable = viable;
1942 cand->reason = reason;
1943 cand->next = *candidates;
1944 cand->flags = flags;
1945 *candidates = cand;
1947 return cand;
1950 /* Return the number of remaining arguments in the parameter list
1951 beginning with ARG. */
1954 remaining_arguments (tree arg)
1956 int n;
1958 for (n = 0; arg != NULL_TREE && arg != void_list_node;
1959 arg = TREE_CHAIN (arg))
1960 n++;
1962 return n;
1965 /* Create an overload candidate for the function or method FN called
1966 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
1967 FLAGS is passed on to implicit_conversion.
1969 This does not change ARGS.
1971 CTYPE, if non-NULL, is the type we want to pretend this function
1972 comes from for purposes of overload resolution. */
1974 static struct z_candidate *
1975 add_function_candidate (struct z_candidate **candidates,
1976 tree fn, tree ctype, tree first_arg,
1977 const vec<tree, va_gc> *args, tree access_path,
1978 tree conversion_path, int flags,
1979 tsubst_flags_t complain)
1981 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1982 int i, len;
1983 conversion **convs;
1984 tree parmnode;
1985 tree orig_first_arg = first_arg;
1986 int skip;
1987 int viable = 1;
1988 struct rejection_reason *reason = NULL;
1990 /* At this point we should not see any functions which haven't been
1991 explicitly declared, except for friend functions which will have
1992 been found using argument dependent lookup. */
1993 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
1995 /* The `this', `in_chrg' and VTT arguments to constructors are not
1996 considered in overload resolution. */
1997 if (DECL_CONSTRUCTOR_P (fn))
1999 if (ctor_omit_inherited_parms (fn))
2000 /* Bring back parameters omitted from an inherited ctor. */
2001 parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2002 else
2003 parmlist = skip_artificial_parms_for (fn, parmlist);
2004 skip = num_artificial_parms_for (fn);
2005 if (skip > 0 && first_arg != NULL_TREE)
2007 --skip;
2008 first_arg = NULL_TREE;
2011 else
2012 skip = 0;
2014 len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2015 convs = alloc_conversions (len);
2017 /* 13.3.2 - Viable functions [over.match.viable]
2018 First, to be a viable function, a candidate function shall have enough
2019 parameters to agree in number with the arguments in the list.
2021 We need to check this first; otherwise, checking the ICSes might cause
2022 us to produce an ill-formed template instantiation. */
2024 parmnode = parmlist;
2025 for (i = 0; i < len; ++i)
2027 if (parmnode == NULL_TREE || parmnode == void_list_node)
2028 break;
2029 parmnode = TREE_CHAIN (parmnode);
2032 if ((i < len && parmnode)
2033 || !sufficient_parms_p (parmnode))
2035 int remaining = remaining_arguments (parmnode);
2036 viable = 0;
2037 reason = arity_rejection (first_arg, i + remaining, len);
2040 /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2041 parameter of type "reference to cv C" (including such a constructor
2042 instantiated from a template) is excluded from the set of candidate
2043 functions when used to construct an object of type D with an argument list
2044 containing a single argument if C is reference-related to D. */
2045 if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2046 && flag_new_inheriting_ctors
2047 && DECL_INHERITED_CTOR (fn))
2049 tree ptype = non_reference (TREE_VALUE (parmlist));
2050 tree dtype = DECL_CONTEXT (fn);
2051 tree btype = DECL_INHERITED_CTOR_BASE (fn);
2052 if (reference_related_p (ptype, dtype)
2053 && reference_related_p (btype, ptype))
2055 viable = false;
2056 reason = inherited_ctor_rejection ();
2060 /* Second, for a function to be viable, its constraints must be
2061 satisfied. */
2062 if (flag_concepts && viable
2063 && !constraints_satisfied_p (fn))
2065 reason = constraint_failure (fn);
2066 viable = false;
2069 /* When looking for a function from a subobject from an implicit
2070 copy/move constructor/operator=, don't consider anything that takes (a
2071 reference to) an unrelated type. See c++/44909 and core 1092. */
2072 if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2074 if (DECL_CONSTRUCTOR_P (fn))
2075 i = 1;
2076 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2077 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2078 i = 2;
2079 else
2080 i = 0;
2081 if (i && len == i)
2083 parmnode = chain_index (i-1, parmlist);
2084 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2085 ctype))
2086 viable = 0;
2089 /* This only applies at the top level. */
2090 flags &= ~LOOKUP_DEFAULTED;
2093 if (! viable)
2094 goto out;
2096 /* Third, for F to be a viable function, there shall exist for each
2097 argument an implicit conversion sequence that converts that argument
2098 to the corresponding parameter of F. */
2100 parmnode = parmlist;
2102 for (i = 0; i < len; ++i)
2104 tree argtype, to_type;
2105 tree arg;
2106 conversion *t;
2107 int is_this;
2109 if (parmnode == void_list_node)
2110 break;
2112 if (i == 0 && first_arg != NULL_TREE)
2113 arg = first_arg;
2114 else
2115 arg = CONST_CAST_TREE (
2116 (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2117 argtype = lvalue_type (arg);
2119 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2120 && ! DECL_CONSTRUCTOR_P (fn));
2122 if (parmnode)
2124 tree parmtype = TREE_VALUE (parmnode);
2125 int lflags = flags;
2127 parmnode = TREE_CHAIN (parmnode);
2129 /* The type of the implicit object parameter ('this') for
2130 overload resolution is not always the same as for the
2131 function itself; conversion functions are considered to
2132 be members of the class being converted, and functions
2133 introduced by a using-declaration are considered to be
2134 members of the class that uses them.
2136 Since build_over_call ignores the ICS for the `this'
2137 parameter, we can just change the parm type. */
2138 if (ctype && is_this)
2140 parmtype = cp_build_qualified_type
2141 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
2142 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2144 /* If the function has a ref-qualifier, the implicit
2145 object parameter has reference type. */
2146 bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2147 parmtype = cp_build_reference_type (parmtype, rv);
2148 /* The special handling of 'this' conversions in compare_ics
2149 does not apply if there is a ref-qualifier. */
2150 is_this = false;
2152 else
2154 parmtype = build_pointer_type (parmtype);
2155 /* We don't use build_this here because we don't want to
2156 capture the object argument until we've chosen a
2157 non-static member function. */
2158 arg = build_address (arg);
2159 argtype = lvalue_type (arg);
2163 /* Core issue 899: When [copy-]initializing a temporary to be bound
2164 to the first parameter of a copy constructor (12.8) called with
2165 a single argument in the context of direct-initialization,
2166 explicit conversion functions are also considered.
2168 So set LOOKUP_COPY_PARM to let reference_binding know that
2169 it's being called in that context. We generalize the above
2170 to handle move constructors and template constructors as well;
2171 the standardese should soon be updated similarly. */
2172 if (ctype && i == 0 && (len-skip == 1)
2173 && DECL_CONSTRUCTOR_P (fn)
2174 && parmtype != error_mark_node
2175 && (same_type_ignoring_top_level_qualifiers_p
2176 (non_reference (parmtype), ctype)))
2178 if (!(flags & LOOKUP_ONLYCONVERTING))
2179 lflags |= LOOKUP_COPY_PARM;
2180 /* We allow user-defined conversions within init-lists, but
2181 don't list-initialize the copy parm, as that would mean
2182 using two levels of braces for the same type. */
2183 if ((flags & LOOKUP_LIST_INIT_CTOR)
2184 && BRACE_ENCLOSED_INITIALIZER_P (arg))
2185 lflags |= LOOKUP_NO_CONVERSION;
2187 else
2188 lflags |= LOOKUP_ONLYCONVERTING;
2190 t = implicit_conversion (parmtype, argtype, arg,
2191 /*c_cast_p=*/false, lflags, complain);
2192 to_type = parmtype;
2194 else
2196 t = build_identity_conv (argtype, arg);
2197 t->ellipsis_p = true;
2198 to_type = argtype;
2201 if (t && is_this)
2202 t->this_p = true;
2204 convs[i] = t;
2205 if (! t)
2207 viable = 0;
2208 reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
2209 break;
2212 if (t->bad_p)
2214 viable = -1;
2215 reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type);
2219 out:
2220 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2221 access_path, conversion_path, viable, reason, flags);
2224 /* Create an overload candidate for the conversion function FN which will
2225 be invoked for expression OBJ, producing a pointer-to-function which
2226 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2227 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2228 passed on to implicit_conversion.
2230 Actually, we don't really care about FN; we care about the type it
2231 converts to. There may be multiple conversion functions that will
2232 convert to that type, and we rely on build_user_type_conversion_1 to
2233 choose the best one; so when we create our candidate, we record the type
2234 instead of the function. */
2236 static struct z_candidate *
2237 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2238 const vec<tree, va_gc> *arglist,
2239 tree access_path, tree conversion_path,
2240 tsubst_flags_t complain)
2242 tree totype = TREE_TYPE (TREE_TYPE (fn));
2243 int i, len, viable, flags;
2244 tree parmlist, parmnode;
2245 conversion **convs;
2246 struct rejection_reason *reason;
2248 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2249 parmlist = TREE_TYPE (parmlist);
2250 parmlist = TYPE_ARG_TYPES (parmlist);
2252 len = vec_safe_length (arglist) + 1;
2253 convs = alloc_conversions (len);
2254 parmnode = parmlist;
2255 viable = 1;
2256 flags = LOOKUP_IMPLICIT;
2257 reason = NULL;
2259 /* Don't bother looking up the same type twice. */
2260 if (*candidates && (*candidates)->fn == totype)
2261 return NULL;
2263 for (i = 0; i < len; ++i)
2265 tree arg, argtype, convert_type = NULL_TREE;
2266 conversion *t;
2268 if (i == 0)
2269 arg = obj;
2270 else
2271 arg = (*arglist)[i - 1];
2272 argtype = lvalue_type (arg);
2274 if (i == 0)
2276 t = build_identity_conv (argtype, NULL_TREE);
2277 t = build_conv (ck_user, totype, t);
2278 /* Leave the 'cand' field null; we'll figure out the conversion in
2279 convert_like_real if this candidate is chosen. */
2280 convert_type = totype;
2282 else if (parmnode == void_list_node)
2283 break;
2284 else if (parmnode)
2286 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2287 /*c_cast_p=*/false, flags, complain);
2288 convert_type = TREE_VALUE (parmnode);
2290 else
2292 t = build_identity_conv (argtype, arg);
2293 t->ellipsis_p = true;
2294 convert_type = argtype;
2297 convs[i] = t;
2298 if (! t)
2299 break;
2301 if (t->bad_p)
2303 viable = -1;
2304 reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type);
2307 if (i == 0)
2308 continue;
2310 if (parmnode)
2311 parmnode = TREE_CHAIN (parmnode);
2314 if (i < len
2315 || ! sufficient_parms_p (parmnode))
2317 int remaining = remaining_arguments (parmnode);
2318 viable = 0;
2319 reason = arity_rejection (NULL_TREE, i + remaining, len);
2322 return add_candidate (candidates, totype, obj, arglist, len, convs,
2323 access_path, conversion_path, viable, reason, flags);
2326 static void
2327 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2328 tree type1, tree type2, tree *args, tree *argtypes,
2329 int flags, tsubst_flags_t complain)
2331 conversion *t;
2332 conversion **convs;
2333 size_t num_convs;
2334 int viable = 1, i;
2335 tree types[2];
2336 struct rejection_reason *reason = NULL;
2338 types[0] = type1;
2339 types[1] = type2;
2341 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
2342 convs = alloc_conversions (num_convs);
2344 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2345 conversion ops are allowed. We handle that here by just checking for
2346 boolean_type_node because other operators don't ask for it. COND_EXPR
2347 also does contextual conversion to bool for the first operand, but we
2348 handle that in build_conditional_expr, and type1 here is operand 2. */
2349 if (type1 != boolean_type_node)
2350 flags |= LOOKUP_ONLYCONVERTING;
2352 for (i = 0; i < 2; ++i)
2354 if (! args[i])
2355 break;
2357 t = implicit_conversion (types[i], argtypes[i], args[i],
2358 /*c_cast_p=*/false, flags, complain);
2359 if (! t)
2361 viable = 0;
2362 /* We need something for printing the candidate. */
2363 t = build_identity_conv (types[i], NULL_TREE);
2364 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2365 types[i]);
2367 else if (t->bad_p)
2369 viable = 0;
2370 reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2371 types[i]);
2373 convs[i] = t;
2376 /* For COND_EXPR we rearranged the arguments; undo that now. */
2377 if (args[2])
2379 convs[2] = convs[1];
2380 convs[1] = convs[0];
2381 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2382 /*c_cast_p=*/false, flags,
2383 complain);
2384 if (t)
2385 convs[0] = t;
2386 else
2388 viable = 0;
2389 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2390 boolean_type_node);
2394 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2395 num_convs, convs,
2396 /*access_path=*/NULL_TREE,
2397 /*conversion_path=*/NULL_TREE,
2398 viable, reason, flags);
2401 static bool
2402 is_complete (tree t)
2404 return COMPLETE_TYPE_P (complete_type (t));
2407 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2409 static bool
2410 promoted_arithmetic_type_p (tree type)
2412 /* [over.built]
2414 In this section, the term promoted integral type is used to refer
2415 to those integral types which are preserved by integral promotion
2416 (including e.g. int and long but excluding e.g. char).
2417 Similarly, the term promoted arithmetic type refers to promoted
2418 integral types plus floating types. */
2419 return ((CP_INTEGRAL_TYPE_P (type)
2420 && same_type_p (type_promotes_to (type), type))
2421 || TREE_CODE (type) == REAL_TYPE);
2424 /* Create any builtin operator overload candidates for the operator in
2425 question given the converted operand types TYPE1 and TYPE2. The other
2426 args are passed through from add_builtin_candidates to
2427 build_builtin_candidate.
2429 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2430 If CODE is requires candidates operands of the same type of the kind
2431 of which TYPE1 and TYPE2 are, we add both candidates
2432 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2434 static void
2435 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2436 enum tree_code code2, tree fnname, tree type1,
2437 tree type2, tree *args, tree *argtypes, int flags,
2438 tsubst_flags_t complain)
2440 switch (code)
2442 case POSTINCREMENT_EXPR:
2443 case POSTDECREMENT_EXPR:
2444 args[1] = integer_zero_node;
2445 type2 = integer_type_node;
2446 break;
2447 default:
2448 break;
2451 switch (code)
2454 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2455 and VQ is either volatile or empty, there exist candidate operator
2456 functions of the form
2457 VQ T& operator++(VQ T&);
2458 T operator++(VQ T&, int);
2459 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
2460 type other than bool, and VQ is either volatile or empty, there exist
2461 candidate operator functions of the form
2462 VQ T& operator--(VQ T&);
2463 T operator--(VQ T&, int);
2464 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
2465 complete object type, and VQ is either volatile or empty, there exist
2466 candidate operator functions of the form
2467 T*VQ& operator++(T*VQ&);
2468 T*VQ& operator--(T*VQ&);
2469 T* operator++(T*VQ&, int);
2470 T* operator--(T*VQ&, int); */
2472 case POSTDECREMENT_EXPR:
2473 case PREDECREMENT_EXPR:
2474 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2475 return;
2476 /* FALLTHRU */
2477 case POSTINCREMENT_EXPR:
2478 case PREINCREMENT_EXPR:
2479 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2481 type1 = build_reference_type (type1);
2482 break;
2484 return;
2486 /* 7 For every cv-qualified or cv-unqualified object type T, there
2487 exist candidate operator functions of the form
2489 T& operator*(T*);
2491 8 For every function type T, there exist candidate operator functions of
2492 the form
2493 T& operator*(T*); */
2495 case INDIRECT_REF:
2496 if (TYPE_PTR_P (type1)
2497 && (TYPE_PTROB_P (type1)
2498 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2499 break;
2500 return;
2502 /* 9 For every type T, there exist candidate operator functions of the form
2503 T* operator+(T*);
2505 10For every promoted arithmetic type T, there exist candidate operator
2506 functions of the form
2507 T operator+(T);
2508 T operator-(T); */
2510 case UNARY_PLUS_EXPR: /* unary + */
2511 if (TYPE_PTR_P (type1))
2512 break;
2513 /* FALLTHRU */
2514 case NEGATE_EXPR:
2515 if (ARITHMETIC_TYPE_P (type1))
2516 break;
2517 return;
2519 /* 11For every promoted integral type T, there exist candidate operator
2520 functions of the form
2521 T operator~(T); */
2523 case BIT_NOT_EXPR:
2524 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2525 break;
2526 return;
2528 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2529 is the same type as C2 or is a derived class of C2, T is a complete
2530 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2531 there exist candidate operator functions of the form
2532 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2533 where CV12 is the union of CV1 and CV2. */
2535 case MEMBER_REF:
2536 if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2538 tree c1 = TREE_TYPE (type1);
2539 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2541 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2542 && (TYPE_PTRMEMFUNC_P (type2)
2543 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2544 break;
2546 return;
2548 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2549 didate operator functions of the form
2550 LR operator*(L, R);
2551 LR operator/(L, R);
2552 LR operator+(L, R);
2553 LR operator-(L, R);
2554 bool operator<(L, R);
2555 bool operator>(L, R);
2556 bool operator<=(L, R);
2557 bool operator>=(L, R);
2558 bool operator==(L, R);
2559 bool operator!=(L, R);
2560 where LR is the result of the usual arithmetic conversions between
2561 types L and R.
2563 14For every pair of types T and I, where T is a cv-qualified or cv-
2564 unqualified complete object type and I is a promoted integral type,
2565 there exist candidate operator functions of the form
2566 T* operator+(T*, I);
2567 T& operator[](T*, I);
2568 T* operator-(T*, I);
2569 T* operator+(I, T*);
2570 T& operator[](I, T*);
2572 15For every T, where T is a pointer to complete object type, there exist
2573 candidate operator functions of the form112)
2574 ptrdiff_t operator-(T, T);
2576 16For every pointer or enumeration type T, there exist candidate operator
2577 functions of the form
2578 bool operator<(T, T);
2579 bool operator>(T, T);
2580 bool operator<=(T, T);
2581 bool operator>=(T, T);
2582 bool operator==(T, T);
2583 bool operator!=(T, T);
2585 17For every pointer to member type T, there exist candidate operator
2586 functions of the form
2587 bool operator==(T, T);
2588 bool operator!=(T, T); */
2590 case MINUS_EXPR:
2591 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2592 break;
2593 if (TYPE_PTROB_P (type1)
2594 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2596 type2 = ptrdiff_type_node;
2597 break;
2599 /* FALLTHRU */
2600 case MULT_EXPR:
2601 case TRUNC_DIV_EXPR:
2602 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2603 break;
2604 return;
2606 case EQ_EXPR:
2607 case NE_EXPR:
2608 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2609 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
2610 break;
2611 if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
2613 type2 = type1;
2614 break;
2616 if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
2618 type1 = type2;
2619 break;
2621 /* Fall through. */
2622 case LT_EXPR:
2623 case GT_EXPR:
2624 case LE_EXPR:
2625 case GE_EXPR:
2626 case MAX_EXPR:
2627 case MIN_EXPR:
2628 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2629 break;
2630 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2631 break;
2632 if (TREE_CODE (type1) == ENUMERAL_TYPE
2633 && TREE_CODE (type2) == ENUMERAL_TYPE)
2634 break;
2635 if (TYPE_PTR_P (type1)
2636 && null_ptr_cst_p (args[1]))
2638 type2 = type1;
2639 break;
2641 if (null_ptr_cst_p (args[0])
2642 && TYPE_PTR_P (type2))
2644 type1 = type2;
2645 break;
2647 return;
2649 case PLUS_EXPR:
2650 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2651 break;
2652 /* FALLTHRU */
2653 case ARRAY_REF:
2654 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2656 type1 = ptrdiff_type_node;
2657 break;
2659 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2661 type2 = ptrdiff_type_node;
2662 break;
2664 return;
2666 /* 18For every pair of promoted integral types L and R, there exist candi-
2667 date operator functions of the form
2668 LR operator%(L, R);
2669 LR operator&(L, R);
2670 LR operator^(L, R);
2671 LR operator|(L, R);
2672 L operator<<(L, R);
2673 L operator>>(L, R);
2674 where LR is the result of the usual arithmetic conversions between
2675 types L and R. */
2677 case TRUNC_MOD_EXPR:
2678 case BIT_AND_EXPR:
2679 case BIT_IOR_EXPR:
2680 case BIT_XOR_EXPR:
2681 case LSHIFT_EXPR:
2682 case RSHIFT_EXPR:
2683 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2684 break;
2685 return;
2687 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2688 type, VQ is either volatile or empty, and R is a promoted arithmetic
2689 type, there exist candidate operator functions of the form
2690 VQ L& operator=(VQ L&, R);
2691 VQ L& operator*=(VQ L&, R);
2692 VQ L& operator/=(VQ L&, R);
2693 VQ L& operator+=(VQ L&, R);
2694 VQ L& operator-=(VQ L&, R);
2696 20For every pair T, VQ), where T is any type and VQ is either volatile
2697 or empty, there exist candidate operator functions of the form
2698 T*VQ& operator=(T*VQ&, T*);
2700 21For every pair T, VQ), where T is a pointer to member type and VQ is
2701 either volatile or empty, there exist candidate operator functions of
2702 the form
2703 VQ T& operator=(VQ T&, T);
2705 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2706 unqualified complete object type, VQ is either volatile or empty, and
2707 I is a promoted integral type, there exist candidate operator func-
2708 tions of the form
2709 T*VQ& operator+=(T*VQ&, I);
2710 T*VQ& operator-=(T*VQ&, I);
2712 23For every triple L, VQ, R), where L is an integral or enumeration
2713 type, VQ is either volatile or empty, and R is a promoted integral
2714 type, there exist candidate operator functions of the form
2716 VQ L& operator%=(VQ L&, R);
2717 VQ L& operator<<=(VQ L&, R);
2718 VQ L& operator>>=(VQ L&, R);
2719 VQ L& operator&=(VQ L&, R);
2720 VQ L& operator^=(VQ L&, R);
2721 VQ L& operator|=(VQ L&, R); */
2723 case MODIFY_EXPR:
2724 switch (code2)
2726 case PLUS_EXPR:
2727 case MINUS_EXPR:
2728 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2730 type2 = ptrdiff_type_node;
2731 break;
2733 /* FALLTHRU */
2734 case MULT_EXPR:
2735 case TRUNC_DIV_EXPR:
2736 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2737 break;
2738 return;
2740 case TRUNC_MOD_EXPR:
2741 case BIT_AND_EXPR:
2742 case BIT_IOR_EXPR:
2743 case BIT_XOR_EXPR:
2744 case LSHIFT_EXPR:
2745 case RSHIFT_EXPR:
2746 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2747 break;
2748 return;
2750 case NOP_EXPR:
2751 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2752 break;
2753 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2754 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2755 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2756 || ((TYPE_PTRMEMFUNC_P (type1)
2757 || TYPE_PTR_P (type1))
2758 && null_ptr_cst_p (args[1])))
2760 type2 = type1;
2761 break;
2763 return;
2765 default:
2766 gcc_unreachable ();
2768 type1 = build_reference_type (type1);
2769 break;
2771 case COND_EXPR:
2772 /* [over.built]
2774 For every pair of promoted arithmetic types L and R, there
2775 exist candidate operator functions of the form
2777 LR operator?(bool, L, R);
2779 where LR is the result of the usual arithmetic conversions
2780 between types L and R.
2782 For every type T, where T is a pointer or pointer-to-member
2783 type, there exist candidate operator functions of the form T
2784 operator?(bool, T, T); */
2786 if (promoted_arithmetic_type_p (type1)
2787 && promoted_arithmetic_type_p (type2))
2788 /* That's OK. */
2789 break;
2791 /* Otherwise, the types should be pointers. */
2792 if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
2793 return;
2795 /* We don't check that the two types are the same; the logic
2796 below will actually create two candidates; one in which both
2797 parameter types are TYPE1, and one in which both parameter
2798 types are TYPE2. */
2799 break;
2801 case REALPART_EXPR:
2802 case IMAGPART_EXPR:
2803 if (ARITHMETIC_TYPE_P (type1))
2804 break;
2805 return;
2807 default:
2808 gcc_unreachable ();
2811 /* Make sure we don't create builtin candidates with dependent types. */
2812 bool u1 = uses_template_parms (type1);
2813 bool u2 = type2 ? uses_template_parms (type2) : false;
2814 if (u1 || u2)
2816 /* Try to recover if one of the types is non-dependent. But if
2817 there's only one type, there's nothing we can do. */
2818 if (!type2)
2819 return;
2820 /* And we lose if both are dependent. */
2821 if (u1 && u2)
2822 return;
2823 /* Or if they have different forms. */
2824 if (TREE_CODE (type1) != TREE_CODE (type2))
2825 return;
2827 if (u1 && !u2)
2828 type1 = type2;
2829 else if (u2 && !u1)
2830 type2 = type1;
2833 /* If we're dealing with two pointer types or two enumeral types,
2834 we need candidates for both of them. */
2835 if (type2 && !same_type_p (type1, type2)
2836 && TREE_CODE (type1) == TREE_CODE (type2)
2837 && (TREE_CODE (type1) == REFERENCE_TYPE
2838 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2839 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2840 || TYPE_PTRMEMFUNC_P (type1)
2841 || MAYBE_CLASS_TYPE_P (type1)
2842 || TREE_CODE (type1) == ENUMERAL_TYPE))
2844 if (TYPE_PTR_OR_PTRMEM_P (type1))
2846 tree cptype = composite_pointer_type (type1, type2,
2847 error_mark_node,
2848 error_mark_node,
2849 CPO_CONVERSION,
2850 tf_none);
2851 if (cptype != error_mark_node)
2853 build_builtin_candidate
2854 (candidates, fnname, cptype, cptype, args, argtypes,
2855 flags, complain);
2856 return;
2860 build_builtin_candidate
2861 (candidates, fnname, type1, type1, args, argtypes, flags, complain);
2862 build_builtin_candidate
2863 (candidates, fnname, type2, type2, args, argtypes, flags, complain);
2864 return;
2867 build_builtin_candidate
2868 (candidates, fnname, type1, type2, args, argtypes, flags, complain);
2871 tree
2872 type_decays_to (tree type)
2874 if (TREE_CODE (type) == ARRAY_TYPE)
2875 return build_pointer_type (TREE_TYPE (type));
2876 if (TREE_CODE (type) == FUNCTION_TYPE)
2877 return build_pointer_type (type);
2878 return type;
2881 /* There are three conditions of builtin candidates:
2883 1) bool-taking candidates. These are the same regardless of the input.
2884 2) pointer-pair taking candidates. These are generated for each type
2885 one of the input types converts to.
2886 3) arithmetic candidates. According to the standard, we should generate
2887 all of these, but I'm trying not to...
2889 Here we generate a superset of the possible candidates for this particular
2890 case. That is a subset of the full set the standard defines, plus some
2891 other cases which the standard disallows. add_builtin_candidate will
2892 filter out the invalid set. */
2894 static void
2895 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2896 enum tree_code code2, tree fnname, tree *args,
2897 int flags, tsubst_flags_t complain)
2899 int ref1, i;
2900 int enum_p = 0;
2901 tree type, argtypes[3], t;
2902 /* TYPES[i] is the set of possible builtin-operator parameter types
2903 we will consider for the Ith argument. */
2904 vec<tree, va_gc> *types[2];
2905 unsigned ix;
2907 for (i = 0; i < 3; ++i)
2909 if (args[i])
2910 argtypes[i] = unlowered_expr_type (args[i]);
2911 else
2912 argtypes[i] = NULL_TREE;
2915 switch (code)
2917 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2918 and VQ is either volatile or empty, there exist candidate operator
2919 functions of the form
2920 VQ T& operator++(VQ T&); */
2922 case POSTINCREMENT_EXPR:
2923 case PREINCREMENT_EXPR:
2924 case POSTDECREMENT_EXPR:
2925 case PREDECREMENT_EXPR:
2926 case MODIFY_EXPR:
2927 ref1 = 1;
2928 break;
2930 /* 24There also exist candidate operator functions of the form
2931 bool operator!(bool);
2932 bool operator&&(bool, bool);
2933 bool operator||(bool, bool); */
2935 case TRUTH_NOT_EXPR:
2936 build_builtin_candidate
2937 (candidates, fnname, boolean_type_node,
2938 NULL_TREE, args, argtypes, flags, complain);
2939 return;
2941 case TRUTH_ORIF_EXPR:
2942 case TRUTH_ANDIF_EXPR:
2943 build_builtin_candidate
2944 (candidates, fnname, boolean_type_node,
2945 boolean_type_node, args, argtypes, flags, complain);
2946 return;
2948 case ADDR_EXPR:
2949 case COMPOUND_EXPR:
2950 case COMPONENT_REF:
2951 return;
2953 case COND_EXPR:
2954 case EQ_EXPR:
2955 case NE_EXPR:
2956 case LT_EXPR:
2957 case LE_EXPR:
2958 case GT_EXPR:
2959 case GE_EXPR:
2960 enum_p = 1;
2961 /* Fall through. */
2963 default:
2964 ref1 = 0;
2967 types[0] = make_tree_vector ();
2968 types[1] = make_tree_vector ();
2970 for (i = 0; i < 2; ++i)
2972 if (! args[i])
2974 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
2976 tree convs;
2978 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2979 return;
2981 convs = lookup_conversions (argtypes[i]);
2983 if (code == COND_EXPR)
2985 if (lvalue_p (args[i]))
2986 vec_safe_push (types[i], build_reference_type (argtypes[i]));
2988 vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
2991 else if (! convs)
2992 return;
2994 for (; convs; convs = TREE_CHAIN (convs))
2996 type = TREE_TYPE (convs);
2998 if (i == 0 && ref1
2999 && (TREE_CODE (type) != REFERENCE_TYPE
3000 || CP_TYPE_CONST_P (TREE_TYPE (type))))
3001 continue;
3003 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
3004 vec_safe_push (types[i], type);
3006 type = non_reference (type);
3007 if (i != 0 || ! ref1)
3009 type = cv_unqualified (type_decays_to (type));
3010 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3011 vec_safe_push (types[i], type);
3012 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3013 type = type_promotes_to (type);
3016 if (! vec_member (type, types[i]))
3017 vec_safe_push (types[i], type);
3020 else
3022 if (code == COND_EXPR && lvalue_p (args[i]))
3023 vec_safe_push (types[i], build_reference_type (argtypes[i]));
3024 type = non_reference (argtypes[i]);
3025 if (i != 0 || ! ref1)
3027 type = cv_unqualified (type_decays_to (type));
3028 if (enum_p && UNSCOPED_ENUM_P (type))
3029 vec_safe_push (types[i], type);
3030 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3031 type = type_promotes_to (type);
3033 vec_safe_push (types[i], type);
3037 /* Run through the possible parameter types of both arguments,
3038 creating candidates with those parameter types. */
3039 FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3041 unsigned jx;
3042 tree u;
3044 if (!types[1]->is_empty ())
3045 FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3046 add_builtin_candidate
3047 (candidates, code, code2, fnname, t,
3048 u, args, argtypes, flags, complain);
3049 else
3050 add_builtin_candidate
3051 (candidates, code, code2, fnname, t,
3052 NULL_TREE, args, argtypes, flags, complain);
3055 release_tree_vector (types[0]);
3056 release_tree_vector (types[1]);
3060 /* If TMPL can be successfully instantiated as indicated by
3061 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3063 TMPL is the template. EXPLICIT_TARGS are any explicit template
3064 arguments. ARGLIST is the arguments provided at the call-site.
3065 This does not change ARGLIST. The RETURN_TYPE is the desired type
3066 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3067 as for add_function_candidate. If an OBJ is supplied, FLAGS and
3068 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
3070 static struct z_candidate*
3071 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3072 tree ctype, tree explicit_targs, tree first_arg,
3073 const vec<tree, va_gc> *arglist, tree return_type,
3074 tree access_path, tree conversion_path,
3075 int flags, tree obj, unification_kind_t strict,
3076 tsubst_flags_t complain)
3078 int ntparms = DECL_NTPARMS (tmpl);
3079 tree targs = make_tree_vec (ntparms);
3080 unsigned int len = vec_safe_length (arglist);
3081 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3082 unsigned int skip_without_in_chrg = 0;
3083 tree first_arg_without_in_chrg = first_arg;
3084 tree *args_without_in_chrg;
3085 unsigned int nargs_without_in_chrg;
3086 unsigned int ia, ix;
3087 tree arg;
3088 struct z_candidate *cand;
3089 tree fn;
3090 struct rejection_reason *reason = NULL;
3091 int errs;
3093 /* We don't do deduction on the in-charge parameter, the VTT
3094 parameter or 'this'. */
3095 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
3097 if (first_arg_without_in_chrg != NULL_TREE)
3098 first_arg_without_in_chrg = NULL_TREE;
3099 else if (return_type && strict == DEDUCE_CALL)
3100 /* We're deducing for a call to the result of a template conversion
3101 function, so the args don't contain 'this'; leave them alone. */;
3102 else
3103 ++skip_without_in_chrg;
3106 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3107 || DECL_BASE_CONSTRUCTOR_P (tmpl))
3108 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3110 if (first_arg_without_in_chrg != NULL_TREE)
3111 first_arg_without_in_chrg = NULL_TREE;
3112 else
3113 ++skip_without_in_chrg;
3116 if (len < skip_without_in_chrg)
3117 return NULL;
3119 if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3120 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3121 TREE_TYPE ((*arglist)[0])))
3123 /* 12.8/6 says, "A declaration of a constructor for a class X is
3124 ill-formed if its first parameter is of type (optionally cv-qualified)
3125 X and either there are no other parameters or else all other
3126 parameters have default arguments. A member function template is never
3127 instantiated to produce such a constructor signature."
3129 So if we're trying to copy an object of the containing class, don't
3130 consider a template constructor that has a first parameter type that
3131 is just a template parameter, as we would deduce a signature that we
3132 would then reject in the code below. */
3133 if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3135 firstparm = TREE_VALUE (firstparm);
3136 if (PACK_EXPANSION_P (firstparm))
3137 firstparm = PACK_EXPANSION_PATTERN (firstparm);
3138 if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3140 gcc_assert (!explicit_targs);
3141 reason = invalid_copy_with_fn_template_rejection ();
3142 goto fail;
3147 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3148 + (len - skip_without_in_chrg));
3149 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3150 ia = 0;
3151 if (first_arg_without_in_chrg != NULL_TREE)
3153 args_without_in_chrg[ia] = first_arg_without_in_chrg;
3154 ++ia;
3156 for (ix = skip_without_in_chrg;
3157 vec_safe_iterate (arglist, ix, &arg);
3158 ++ix)
3160 args_without_in_chrg[ia] = arg;
3161 ++ia;
3163 gcc_assert (ia == nargs_without_in_chrg);
3165 errs = errorcount+sorrycount;
3166 fn = fn_type_unification (tmpl, explicit_targs, targs,
3167 args_without_in_chrg,
3168 nargs_without_in_chrg,
3169 return_type, strict, flags, false,
3170 complain & tf_decltype);
3172 if (fn == error_mark_node)
3174 /* Don't repeat unification later if it already resulted in errors. */
3175 if (errorcount+sorrycount == errs)
3176 reason = template_unification_rejection (tmpl, explicit_targs,
3177 targs, args_without_in_chrg,
3178 nargs_without_in_chrg,
3179 return_type, strict, flags);
3180 else
3181 reason = template_unification_error_rejection ();
3182 goto fail;
3185 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3187 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3188 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3189 ctype))
3191 /* We're trying to produce a constructor with a prohibited signature,
3192 as discussed above; handle here any cases we didn't catch then,
3193 such as X(X<T>). */
3194 reason = invalid_copy_with_fn_template_rejection ();
3195 goto fail;
3199 if (obj != NULL_TREE)
3200 /* Aha, this is a conversion function. */
3201 cand = add_conv_candidate (candidates, fn, obj, arglist,
3202 access_path, conversion_path, complain);
3203 else
3204 cand = add_function_candidate (candidates, fn, ctype,
3205 first_arg, arglist, access_path,
3206 conversion_path, flags, complain);
3207 if (DECL_TI_TEMPLATE (fn) != tmpl)
3208 /* This situation can occur if a member template of a template
3209 class is specialized. Then, instantiate_template might return
3210 an instantiation of the specialization, in which case the
3211 DECL_TI_TEMPLATE field will point at the original
3212 specialization. For example:
3214 template <class T> struct S { template <class U> void f(U);
3215 template <> void f(int) {}; };
3216 S<double> sd;
3217 sd.f(3);
3219 Here, TMPL will be template <class U> S<double>::f(U).
3220 And, instantiate template will give us the specialization
3221 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3222 for this will point at template <class T> template <> S<T>::f(int),
3223 so that we can find the definition. For the purposes of
3224 overload resolution, however, we want the original TMPL. */
3225 cand->template_decl = build_template_info (tmpl, targs);
3226 else
3227 cand->template_decl = DECL_TEMPLATE_INFO (fn);
3228 cand->explicit_targs = explicit_targs;
3230 return cand;
3231 fail:
3232 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
3233 access_path, conversion_path, 0, reason, flags);
3237 static struct z_candidate *
3238 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3239 tree explicit_targs, tree first_arg,
3240 const vec<tree, va_gc> *arglist, tree return_type,
3241 tree access_path, tree conversion_path, int flags,
3242 unification_kind_t strict, tsubst_flags_t complain)
3244 return
3245 add_template_candidate_real (candidates, tmpl, ctype,
3246 explicit_targs, first_arg, arglist,
3247 return_type, access_path, conversion_path,
3248 flags, NULL_TREE, strict, complain);
3251 /* Create an overload candidate for the conversion function template TMPL,
3252 returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3253 pointer-to-function which will in turn be called with the argument list
3254 ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3255 passed on to implicit_conversion. */
3257 static struct z_candidate *
3258 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3259 tree obj,
3260 const vec<tree, va_gc> *arglist,
3261 tree return_type, tree access_path,
3262 tree conversion_path, tsubst_flags_t complain)
3264 /* Making this work broke PR 71117, so until the committee resolves core
3265 issue 2189, let's disable this candidate if there are any viable call
3266 operators. */
3267 if (any_strictly_viable (*candidates))
3268 return NULL;
3270 return
3271 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3272 NULL_TREE, arglist, return_type, access_path,
3273 conversion_path, 0, obj, DEDUCE_CALL,
3274 complain);
3277 /* The CANDS are the set of candidates that were considered for
3278 overload resolution. Return the set of viable candidates, or CANDS
3279 if none are viable. If any of the candidates were viable, set
3280 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3281 considered viable only if it is strictly viable. */
3283 static struct z_candidate*
3284 splice_viable (struct z_candidate *cands,
3285 bool strict_p,
3286 bool *any_viable_p)
3288 struct z_candidate *viable;
3289 struct z_candidate **last_viable;
3290 struct z_candidate **cand;
3291 bool found_strictly_viable = false;
3293 /* Be strict inside templates, since build_over_call won't actually
3294 do the conversions to get pedwarns. */
3295 if (processing_template_decl)
3296 strict_p = true;
3298 viable = NULL;
3299 last_viable = &viable;
3300 *any_viable_p = false;
3302 cand = &cands;
3303 while (*cand)
3305 struct z_candidate *c = *cand;
3306 if (!strict_p
3307 && (c->viable == 1 || TREE_CODE (c->fn) == TEMPLATE_DECL))
3309 /* Be strict in the presence of a viable candidate. Also if
3310 there are template candidates, so that we get deduction errors
3311 for them instead of silently preferring a bad conversion. */
3312 strict_p = true;
3313 if (viable && !found_strictly_viable)
3315 /* Put any spliced near matches back onto the main list so
3316 that we see them if there is no strict match. */
3317 *any_viable_p = false;
3318 *last_viable = cands;
3319 cands = viable;
3320 viable = NULL;
3321 last_viable = &viable;
3325 if (strict_p ? c->viable == 1 : c->viable)
3327 *last_viable = c;
3328 *cand = c->next;
3329 c->next = NULL;
3330 last_viable = &c->next;
3331 *any_viable_p = true;
3332 if (c->viable == 1)
3333 found_strictly_viable = true;
3335 else
3336 cand = &c->next;
3339 return viable ? viable : cands;
3342 static bool
3343 any_strictly_viable (struct z_candidate *cands)
3345 for (; cands; cands = cands->next)
3346 if (cands->viable == 1)
3347 return true;
3348 return false;
3351 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3352 words, it is about to become the "this" pointer for a member
3353 function call. Take the address of the object. */
3355 static tree
3356 build_this (tree obj)
3358 /* In a template, we are only concerned about the type of the
3359 expression, so we can take a shortcut. */
3360 if (processing_template_decl)
3361 return build_address (obj);
3363 return cp_build_addr_expr (obj, tf_warning_or_error);
3366 /* Returns true iff functions are equivalent. Equivalent functions are
3367 not '==' only if one is a function-local extern function or if
3368 both are extern "C". */
3370 static inline int
3371 equal_functions (tree fn1, tree fn2)
3373 if (TREE_CODE (fn1) != TREE_CODE (fn2))
3374 return 0;
3375 if (TREE_CODE (fn1) == TEMPLATE_DECL)
3376 return fn1 == fn2;
3377 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
3378 || DECL_EXTERN_C_FUNCTION_P (fn1))
3379 return decls_match (fn1, fn2);
3380 return fn1 == fn2;
3383 /* Print information about a candidate being rejected due to INFO. */
3385 static void
3386 print_conversion_rejection (location_t loc, struct conversion_info *info)
3388 tree from = info->from;
3389 if (!TYPE_P (from))
3390 from = lvalue_type (from);
3391 if (info->n_arg == -1)
3393 /* Conversion of implicit `this' argument failed. */
3394 if (!TYPE_P (info->from))
3395 /* A bad conversion for 'this' must be discarding cv-quals. */
3396 inform (loc, " passing %qT as %<this%> "
3397 "argument discards qualifiers",
3398 from);
3399 else
3400 inform (loc, " no known conversion for implicit "
3401 "%<this%> parameter from %qH to %qI",
3402 from, info->to_type);
3404 else if (!TYPE_P (info->from))
3406 if (info->n_arg >= 0)
3407 inform (loc, " conversion of argument %d would be ill-formed:",
3408 info->n_arg + 1);
3409 perform_implicit_conversion (info->to_type, info->from,
3410 tf_warning_or_error);
3412 else if (info->n_arg == -2)
3413 /* Conversion of conversion function return value failed. */
3414 inform (loc, " no known conversion from %qH to %qI",
3415 from, info->to_type);
3416 else
3417 inform (loc, " no known conversion for argument %d from %qH to %qI",
3418 info->n_arg + 1, from, info->to_type);
3421 /* Print information about a candidate with WANT parameters and we found
3422 HAVE. */
3424 static void
3425 print_arity_information (location_t loc, unsigned int have, unsigned int want)
3427 inform_n (loc, want,
3428 " candidate expects %d argument, %d provided",
3429 " candidate expects %d arguments, %d provided",
3430 want, have);
3433 /* Print information about one overload candidate CANDIDATE. MSGSTR
3434 is the text to print before the candidate itself.
3436 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3437 to have been run through gettext by the caller. This wart makes
3438 life simpler in print_z_candidates and for the translators. */
3440 static void
3441 print_z_candidate (location_t loc, const char *msgstr,
3442 struct z_candidate *candidate)
3444 const char *msg = (msgstr == NULL
3445 ? ""
3446 : ACONCAT ((msgstr, " ", NULL)));
3447 tree fn = candidate->fn;
3448 if (flag_new_inheriting_ctors)
3449 fn = strip_inheriting_ctors (fn);
3450 location_t cloc = location_of (fn);
3452 if (identifier_p (fn))
3454 cloc = loc;
3455 if (candidate->num_convs == 3)
3456 inform (cloc, "%s%<%D(%T, %T, %T)%> <built-in>", msg, fn,
3457 candidate->convs[0]->type,
3458 candidate->convs[1]->type,
3459 candidate->convs[2]->type);
3460 else if (candidate->num_convs == 2)
3461 inform (cloc, "%s%<%D(%T, %T)%> <built-in>", msg, fn,
3462 candidate->convs[0]->type,
3463 candidate->convs[1]->type);
3464 else
3465 inform (cloc, "%s%<%D(%T)%> <built-in>", msg, fn,
3466 candidate->convs[0]->type);
3468 else if (TYPE_P (fn))
3469 inform (cloc, "%s%qT <conversion>", msg, fn);
3470 else if (candidate->viable == -1)
3471 inform (cloc, "%s%#qD <near match>", msg, fn);
3472 else if (DECL_DELETED_FN (fn))
3473 inform (cloc, "%s%#qD <deleted>", msg, fn);
3474 else
3475 inform (cloc, "%s%#qD", msg, fn);
3476 if (fn != candidate->fn)
3478 cloc = location_of (candidate->fn);
3479 inform (cloc, " inherited here");
3481 /* Give the user some information about why this candidate failed. */
3482 if (candidate->reason != NULL)
3484 struct rejection_reason *r = candidate->reason;
3486 switch (r->code)
3488 case rr_arity:
3489 print_arity_information (cloc, r->u.arity.actual,
3490 r->u.arity.expected);
3491 break;
3492 case rr_arg_conversion:
3493 print_conversion_rejection (cloc, &r->u.conversion);
3494 break;
3495 case rr_bad_arg_conversion:
3496 print_conversion_rejection (cloc, &r->u.bad_conversion);
3497 break;
3498 case rr_explicit_conversion:
3499 inform (cloc, " return type %qT of explicit conversion function "
3500 "cannot be converted to %qT with a qualification "
3501 "conversion", r->u.conversion.from,
3502 r->u.conversion.to_type);
3503 break;
3504 case rr_template_conversion:
3505 inform (cloc, " conversion from return type %qT of template "
3506 "conversion function specialization to %qT is not an "
3507 "exact match", r->u.conversion.from,
3508 r->u.conversion.to_type);
3509 break;
3510 case rr_template_unification:
3511 /* We use template_unification_error_rejection if unification caused
3512 actual non-SFINAE errors, in which case we don't need to repeat
3513 them here. */
3514 if (r->u.template_unification.tmpl == NULL_TREE)
3516 inform (cloc, " substitution of deduced template arguments "
3517 "resulted in errors seen above");
3518 break;
3520 /* Re-run template unification with diagnostics. */
3521 inform (cloc, " template argument deduction/substitution failed:");
3522 fn_type_unification (r->u.template_unification.tmpl,
3523 r->u.template_unification.explicit_targs,
3524 (make_tree_vec
3525 (r->u.template_unification.num_targs)),
3526 r->u.template_unification.args,
3527 r->u.template_unification.nargs,
3528 r->u.template_unification.return_type,
3529 r->u.template_unification.strict,
3530 r->u.template_unification.flags,
3531 true, false);
3532 break;
3533 case rr_invalid_copy:
3534 inform (cloc,
3535 " a constructor taking a single argument of its own "
3536 "class type is invalid");
3537 break;
3538 case rr_constraint_failure:
3540 tree tmpl = r->u.template_instantiation.tmpl;
3541 tree args = r->u.template_instantiation.targs;
3542 diagnose_constraints (cloc, tmpl, args);
3544 break;
3545 case rr_inherited_ctor:
3546 inform (cloc, " an inherited constructor is not a candidate for "
3547 "initialization from an expression of the same or derived "
3548 "type");
3549 break;
3550 case rr_none:
3551 default:
3552 /* This candidate didn't have any issues or we failed to
3553 handle a particular code. Either way... */
3554 gcc_unreachable ();
3559 static void
3560 print_z_candidates (location_t loc, struct z_candidate *candidates)
3562 struct z_candidate *cand1;
3563 struct z_candidate **cand2;
3565 if (!candidates)
3566 return;
3568 /* Remove non-viable deleted candidates. */
3569 cand1 = candidates;
3570 for (cand2 = &cand1; *cand2; )
3572 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
3573 && !(*cand2)->viable
3574 && DECL_DELETED_FN ((*cand2)->fn))
3575 *cand2 = (*cand2)->next;
3576 else
3577 cand2 = &(*cand2)->next;
3579 /* ...if there are any non-deleted ones. */
3580 if (cand1)
3581 candidates = cand1;
3583 /* There may be duplicates in the set of candidates. We put off
3584 checking this condition as long as possible, since we have no way
3585 to eliminate duplicates from a set of functions in less than n^2
3586 time. Now we are about to emit an error message, so it is more
3587 permissible to go slowly. */
3588 for (cand1 = candidates; cand1; cand1 = cand1->next)
3590 tree fn = cand1->fn;
3591 /* Skip builtin candidates and conversion functions. */
3592 if (!DECL_P (fn))
3593 continue;
3594 cand2 = &cand1->next;
3595 while (*cand2)
3597 if (DECL_P ((*cand2)->fn)
3598 && equal_functions (fn, (*cand2)->fn))
3599 *cand2 = (*cand2)->next;
3600 else
3601 cand2 = &(*cand2)->next;
3605 for (; candidates; candidates = candidates->next)
3606 print_z_candidate (loc, "candidate:", candidates);
3609 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3610 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3611 the result of the conversion function to convert it to the final
3612 desired type. Merge the two sequences into a single sequence,
3613 and return the merged sequence. */
3615 static conversion *
3616 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
3618 conversion **t;
3619 bool bad = user_seq->bad_p;
3621 gcc_assert (user_seq->kind == ck_user);
3623 /* Find the end of the second conversion sequence. */
3624 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
3626 /* The entire sequence is a user-conversion sequence. */
3627 (*t)->user_conv_p = true;
3628 if (bad)
3629 (*t)->bad_p = true;
3632 /* Replace the identity conversion with the user conversion
3633 sequence. */
3634 *t = user_seq;
3636 return std_seq;
3639 /* Handle overload resolution for initializing an object of class type from
3640 an initializer list. First we look for a suitable constructor that
3641 takes a std::initializer_list; if we don't find one, we then look for a
3642 non-list constructor.
3644 Parameters are as for add_candidates, except that the arguments are in
3645 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
3646 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3648 static void
3649 add_list_candidates (tree fns, tree first_arg,
3650 const vec<tree, va_gc> *args, tree totype,
3651 tree explicit_targs, bool template_only,
3652 tree conversion_path, tree access_path,
3653 int flags,
3654 struct z_candidate **candidates,
3655 tsubst_flags_t complain)
3657 gcc_assert (*candidates == NULL);
3659 /* We're looking for a ctor for list-initialization. */
3660 flags |= LOOKUP_LIST_INIT_CTOR;
3661 /* And we don't allow narrowing conversions. We also use this flag to
3662 avoid the copy constructor call for copy-list-initialization. */
3663 flags |= LOOKUP_NO_NARROWING;
3665 unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
3666 tree init_list = (*args)[nart];
3668 /* Always use the default constructor if the list is empty (DR 990). */
3669 if (CONSTRUCTOR_NELTS (init_list) == 0
3670 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
3672 /* If the class has a list ctor, try passing the list as a single
3673 argument first, but only consider list ctors. */
3674 else if (TYPE_HAS_LIST_CTOR (totype))
3676 flags |= LOOKUP_LIST_ONLY;
3677 add_candidates (fns, first_arg, args, NULL_TREE,
3678 explicit_targs, template_only, conversion_path,
3679 access_path, flags, candidates, complain);
3680 if (any_strictly_viable (*candidates))
3681 return;
3684 /* Expand the CONSTRUCTOR into a new argument vec. */
3685 vec<tree, va_gc> *new_args;
3686 vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
3687 for (unsigned i = 0; i < nart; ++i)
3688 new_args->quick_push ((*args)[i]);
3689 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (init_list); ++i)
3690 new_args->quick_push (CONSTRUCTOR_ELT (init_list, i)->value);
3692 /* We aren't looking for list-ctors anymore. */
3693 flags &= ~LOOKUP_LIST_ONLY;
3694 /* We allow more user-defined conversions within an init-list. */
3695 flags &= ~LOOKUP_NO_CONVERSION;
3697 add_candidates (fns, first_arg, new_args, NULL_TREE,
3698 explicit_targs, template_only, conversion_path,
3699 access_path, flags, candidates, complain);
3702 /* Returns the best overload candidate to perform the requested
3703 conversion. This function is used for three the overloading situations
3704 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3705 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3706 per [dcl.init.ref], so we ignore temporary bindings. */
3708 static struct z_candidate *
3709 build_user_type_conversion_1 (tree totype, tree expr, int flags,
3710 tsubst_flags_t complain)
3712 struct z_candidate *candidates, *cand;
3713 tree fromtype;
3714 tree ctors = NULL_TREE;
3715 tree conv_fns = NULL_TREE;
3716 conversion *conv = NULL;
3717 tree first_arg = NULL_TREE;
3718 vec<tree, va_gc> *args = NULL;
3719 bool any_viable_p;
3720 int convflags;
3722 if (!expr)
3723 return NULL;
3725 fromtype = TREE_TYPE (expr);
3727 /* We represent conversion within a hierarchy using RVALUE_CONV and
3728 BASE_CONV, as specified by [over.best.ics]; these become plain
3729 constructor calls, as specified in [dcl.init]. */
3730 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
3731 || !DERIVED_FROM_P (totype, fromtype));
3733 if (CLASS_TYPE_P (totype))
3734 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
3735 creating a garbage BASELINK; constructors can't be inherited. */
3736 ctors = get_class_binding (totype, complete_ctor_identifier);
3738 /* FIXME P0135 doesn't say what to do in C++17 about list-initialization from
3739 a single element. For now, let's handle constructors as before and also
3740 consider conversion operators from the element. */
3741 if (cxx_dialect >= cxx17
3742 && BRACE_ENCLOSED_INITIALIZER_P (expr)
3743 && CONSTRUCTOR_NELTS (expr) == 1)
3744 fromtype = TREE_TYPE (CONSTRUCTOR_ELT (expr, 0)->value);
3746 if (MAYBE_CLASS_TYPE_P (fromtype))
3748 tree to_nonref = non_reference (totype);
3749 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
3750 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
3751 && DERIVED_FROM_P (to_nonref, fromtype)))
3753 /* [class.conv.fct] A conversion function is never used to
3754 convert a (possibly cv-qualified) object to the (possibly
3755 cv-qualified) same object type (or a reference to it), to a
3756 (possibly cv-qualified) base class of that type (or a
3757 reference to it)... */
3759 else
3760 conv_fns = lookup_conversions (fromtype);
3763 candidates = 0;
3764 flags |= LOOKUP_NO_CONVERSION;
3765 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3766 flags |= LOOKUP_NO_NARROWING;
3768 /* It's OK to bind a temporary for converting constructor arguments, but
3769 not in converting the return value of a conversion operator. */
3770 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
3771 | (flags & LOOKUP_NO_NARROWING));
3772 flags &= ~LOOKUP_NO_TEMP_BIND;
3774 if (ctors)
3776 int ctorflags = flags;
3778 first_arg = build_dummy_object (totype);
3780 /* We should never try to call the abstract or base constructor
3781 from here. */
3782 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
3783 && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
3785 args = make_tree_vector_single (expr);
3786 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3788 /* List-initialization. */
3789 add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
3790 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
3791 ctorflags, &candidates, complain);
3793 else
3795 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
3796 TYPE_BINFO (totype), TYPE_BINFO (totype),
3797 ctorflags, &candidates, complain);
3800 for (cand = candidates; cand; cand = cand->next)
3802 cand->second_conv = build_identity_conv (totype, NULL_TREE);
3804 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
3805 set, then this is copy-initialization. In that case, "The
3806 result of the call is then used to direct-initialize the
3807 object that is the destination of the copy-initialization."
3808 [dcl.init]
3810 We represent this in the conversion sequence with an
3811 rvalue conversion, which means a constructor call. */
3812 if (TREE_CODE (totype) != REFERENCE_TYPE
3813 && !(convflags & LOOKUP_NO_TEMP_BIND))
3814 cand->second_conv
3815 = build_conv (ck_rvalue, totype, cand->second_conv);
3819 if (conv_fns)
3821 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3822 /* FIXME see above about C++17. */
3823 first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
3824 else
3825 first_arg = expr;
3828 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
3830 tree conversion_path = TREE_PURPOSE (conv_fns);
3831 struct z_candidate *old_candidates;
3833 /* If we are called to convert to a reference type, we are trying to
3834 find a direct binding, so don't even consider temporaries. If
3835 we don't find a direct binding, the caller will try again to
3836 look for a temporary binding. */
3837 if (TREE_CODE (totype) == REFERENCE_TYPE)
3838 convflags |= LOOKUP_NO_TEMP_BIND;
3840 old_candidates = candidates;
3841 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3842 NULL_TREE, false,
3843 conversion_path, TYPE_BINFO (fromtype),
3844 flags, &candidates, complain);
3846 for (cand = candidates; cand != old_candidates; cand = cand->next)
3848 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
3849 conversion *ics
3850 = implicit_conversion (totype,
3851 rettype,
3853 /*c_cast_p=*/false, convflags,
3854 complain);
3856 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3857 copy-initialization. In that case, "The result of the
3858 call is then used to direct-initialize the object that is
3859 the destination of the copy-initialization." [dcl.init]
3861 We represent this in the conversion sequence with an
3862 rvalue conversion, which means a constructor call. But
3863 don't add a second rvalue conversion if there's already
3864 one there. Which there really shouldn't be, but it's
3865 harmless since we'd add it here anyway. */
3866 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3867 && !(convflags & LOOKUP_NO_TEMP_BIND))
3868 ics = build_conv (ck_rvalue, totype, ics);
3870 cand->second_conv = ics;
3872 if (!ics)
3874 cand->viable = 0;
3875 cand->reason = arg_conversion_rejection (NULL_TREE, -2,
3876 rettype, totype);
3878 else if (DECL_NONCONVERTING_P (cand->fn)
3879 && ics->rank > cr_exact)
3881 /* 13.3.1.5: For direct-initialization, those explicit
3882 conversion functions that are not hidden within S and
3883 yield type T or a type that can be converted to type T
3884 with a qualification conversion (4.4) are also candidate
3885 functions. */
3886 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
3887 I've raised this issue with the committee. --jason 9/2011 */
3888 cand->viable = -1;
3889 cand->reason = explicit_conversion_rejection (rettype, totype);
3891 else if (cand->viable == 1 && ics->bad_p)
3893 cand->viable = -1;
3894 cand->reason
3895 = bad_arg_conversion_rejection (NULL_TREE, -2,
3896 rettype, totype);
3898 else if (primary_template_specialization_p (cand->fn)
3899 && ics->rank > cr_exact)
3901 /* 13.3.3.1.2: If the user-defined conversion is specified by
3902 a specialization of a conversion function template, the
3903 second standard conversion sequence shall have exact match
3904 rank. */
3905 cand->viable = -1;
3906 cand->reason = template_conversion_rejection (rettype, totype);
3911 candidates = splice_viable (candidates, false, &any_viable_p);
3912 if (!any_viable_p)
3914 if (args)
3915 release_tree_vector (args);
3916 return NULL;
3919 cand = tourney (candidates, complain);
3920 if (cand == 0)
3922 if (complain & tf_error)
3924 error ("conversion from %qH to %qI is ambiguous",
3925 fromtype, totype);
3926 print_z_candidates (location_of (expr), candidates);
3929 cand = candidates; /* any one will do */
3930 cand->second_conv = build_ambiguous_conv (totype, expr);
3931 cand->second_conv->user_conv_p = true;
3932 if (!any_strictly_viable (candidates))
3933 cand->second_conv->bad_p = true;
3934 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3935 ambiguous conversion is no worse than another user-defined
3936 conversion. */
3938 return cand;
3941 tree convtype;
3942 if (!DECL_CONSTRUCTOR_P (cand->fn))
3943 convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
3944 else if (cand->second_conv->kind == ck_rvalue)
3945 /* DR 5: [in the first step of copy-initialization]...if the function
3946 is a constructor, the call initializes a temporary of the
3947 cv-unqualified version of the destination type. */
3948 convtype = cv_unqualified (totype);
3949 else
3950 convtype = totype;
3951 /* Build the user conversion sequence. */
3952 conv = build_conv
3953 (ck_user,
3954 convtype,
3955 build_identity_conv (TREE_TYPE (expr), expr));
3956 conv->cand = cand;
3957 if (cand->viable == -1)
3958 conv->bad_p = true;
3960 /* Remember that this was a list-initialization. */
3961 if (flags & LOOKUP_NO_NARROWING)
3962 conv->check_narrowing = true;
3964 /* Combine it with the second conversion sequence. */
3965 cand->second_conv = merge_conversion_sequences (conv,
3966 cand->second_conv);
3968 return cand;
3971 /* Wrapper for above. */
3973 tree
3974 build_user_type_conversion (tree totype, tree expr, int flags,
3975 tsubst_flags_t complain)
3977 struct z_candidate *cand;
3978 tree ret;
3980 bool subtime = timevar_cond_start (TV_OVERLOAD);
3981 cand = build_user_type_conversion_1 (totype, expr, flags, complain);
3983 if (cand)
3985 if (cand->second_conv->kind == ck_ambig)
3986 ret = error_mark_node;
3987 else
3989 expr = convert_like (cand->second_conv, expr, complain);
3990 ret = convert_from_reference (expr);
3993 else
3994 ret = NULL_TREE;
3996 timevar_cond_stop (TV_OVERLOAD, subtime);
3997 return ret;
4000 /* Subroutine of convert_nontype_argument.
4002 EXPR is an expression used in a context that requires a converted
4003 constant-expression, such as a template non-type parameter. Do any
4004 necessary conversions (that are permitted for converted
4005 constant-expressions) to convert it to the desired type.
4007 If conversion is successful, returns the converted expression;
4008 otherwise, returns error_mark_node. */
4010 tree
4011 build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
4013 conversion *conv;
4014 void *p;
4015 tree t;
4016 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
4018 if (error_operand_p (expr))
4019 return error_mark_node;
4021 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4022 p = conversion_obstack_alloc (0);
4024 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4025 /*c_cast_p=*/false,
4026 LOOKUP_IMPLICIT, complain);
4028 /* A converted constant expression of type T is an expression, implicitly
4029 converted to type T, where the converted expression is a constant
4030 expression and the implicit conversion sequence contains only
4032 * user-defined conversions,
4033 * lvalue-to-rvalue conversions (7.1),
4034 * array-to-pointer conversions (7.2),
4035 * function-to-pointer conversions (7.3),
4036 * qualification conversions (7.5),
4037 * integral promotions (7.6),
4038 * integral conversions (7.8) other than narrowing conversions (11.6.4),
4039 * null pointer conversions (7.11) from std::nullptr_t,
4040 * null member pointer conversions (7.12) from std::nullptr_t, and
4041 * function pointer conversions (7.13),
4043 and where the reference binding (if any) binds directly. */
4045 for (conversion *c = conv;
4046 conv && c->kind != ck_identity;
4047 c = next_conversion (c))
4049 switch (c->kind)
4051 /* A conversion function is OK. If it isn't constexpr, we'll
4052 complain later that the argument isn't constant. */
4053 case ck_user:
4054 /* The lvalue-to-rvalue conversion is OK. */
4055 case ck_rvalue:
4056 /* Array-to-pointer and function-to-pointer. */
4057 case ck_lvalue:
4058 /* Function pointer conversions. */
4059 case ck_fnptr:
4060 /* Qualification conversions. */
4061 case ck_qual:
4062 break;
4064 case ck_ref_bind:
4065 if (c->need_temporary_p)
4067 if (complain & tf_error)
4068 error_at (loc, "initializing %qH with %qI in converted "
4069 "constant expression does not bind directly",
4070 type, next_conversion (c)->type);
4071 conv = NULL;
4073 break;
4075 case ck_base:
4076 case ck_pmem:
4077 case ck_ptr:
4078 case ck_std:
4079 t = next_conversion (c)->type;
4080 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
4081 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4082 /* Integral promotion or conversion. */
4083 break;
4084 if (NULLPTR_TYPE_P (t))
4085 /* Conversion from nullptr to pointer or pointer-to-member. */
4086 break;
4088 if (complain & tf_error)
4089 error_at (loc, "conversion from %qH to %qI in a "
4090 "converted constant expression", t, type);
4091 /* fall through. */
4093 default:
4094 conv = NULL;
4095 break;
4099 /* Avoid confusing convert_nontype_argument by introducing
4100 a redundant conversion to the same reference type. */
4101 if (conv && conv->kind == ck_ref_bind
4102 && REFERENCE_REF_P (expr))
4104 tree ref = TREE_OPERAND (expr, 0);
4105 if (same_type_p (type, TREE_TYPE (ref)))
4106 return ref;
4109 if (conv)
4110 expr = convert_like (conv, expr, complain);
4111 else
4112 expr = error_mark_node;
4114 /* Free all the conversions we allocated. */
4115 obstack_free (&conversion_obstack, p);
4117 return expr;
4120 /* Do any initial processing on the arguments to a function call. */
4122 static vec<tree, va_gc> *
4123 resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
4125 unsigned int ix;
4126 tree arg;
4128 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
4130 if (error_operand_p (arg))
4131 return NULL;
4132 else if (VOID_TYPE_P (TREE_TYPE (arg)))
4134 if (complain & tf_error)
4135 error ("invalid use of void expression");
4136 return NULL;
4138 else if (invalid_nonstatic_memfn_p (input_location, arg, complain))
4139 return NULL;
4141 return args;
4144 /* Perform overload resolution on FN, which is called with the ARGS.
4146 Return the candidate function selected by overload resolution, or
4147 NULL if the event that overload resolution failed. In the case
4148 that overload resolution fails, *CANDIDATES will be the set of
4149 candidates considered, and ANY_VIABLE_P will be set to true or
4150 false to indicate whether or not any of the candidates were
4151 viable.
4153 The ARGS should already have gone through RESOLVE_ARGS before this
4154 function is called. */
4156 static struct z_candidate *
4157 perform_overload_resolution (tree fn,
4158 const vec<tree, va_gc> *args,
4159 struct z_candidate **candidates,
4160 bool *any_viable_p, tsubst_flags_t complain)
4162 struct z_candidate *cand;
4163 tree explicit_targs;
4164 int template_only;
4166 bool subtime = timevar_cond_start (TV_OVERLOAD);
4168 explicit_targs = NULL_TREE;
4169 template_only = 0;
4171 *candidates = NULL;
4172 *any_viable_p = true;
4174 /* Check FN. */
4175 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
4176 || TREE_CODE (fn) == TEMPLATE_DECL
4177 || TREE_CODE (fn) == OVERLOAD
4178 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
4180 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4182 explicit_targs = TREE_OPERAND (fn, 1);
4183 fn = TREE_OPERAND (fn, 0);
4184 template_only = 1;
4187 /* Add the various candidate functions. */
4188 add_candidates (fn, NULL_TREE, args, NULL_TREE,
4189 explicit_targs, template_only,
4190 /*conversion_path=*/NULL_TREE,
4191 /*access_path=*/NULL_TREE,
4192 LOOKUP_NORMAL,
4193 candidates, complain);
4195 *candidates = splice_viable (*candidates, false, any_viable_p);
4196 if (*any_viable_p)
4197 cand = tourney (*candidates, complain);
4198 else
4199 cand = NULL;
4201 timevar_cond_stop (TV_OVERLOAD, subtime);
4202 return cand;
4205 /* Print an error message about being unable to build a call to FN with
4206 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
4207 be located; CANDIDATES is a possibly empty list of such
4208 functions. */
4210 static void
4211 print_error_for_call_failure (tree fn, vec<tree, va_gc> *args,
4212 struct z_candidate *candidates)
4214 tree targs = NULL_TREE;
4215 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4217 targs = TREE_OPERAND (fn, 1);
4218 fn = TREE_OPERAND (fn, 0);
4220 tree name = OVL_NAME (fn);
4221 location_t loc = location_of (name);
4222 if (targs)
4223 name = lookup_template_function (name, targs);
4225 if (!any_strictly_viable (candidates))
4226 error_at (loc, "no matching function for call to %<%D(%A)%>",
4227 name, build_tree_list_vec (args));
4228 else
4229 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
4230 name, build_tree_list_vec (args));
4231 if (candidates)
4232 print_z_candidates (loc, candidates);
4235 /* Return an expression for a call to FN (a namespace-scope function,
4236 or a static member function) with the ARGS. This may change
4237 ARGS. */
4239 tree
4240 build_new_function_call (tree fn, vec<tree, va_gc> **args,
4241 tsubst_flags_t complain)
4243 struct z_candidate *candidates, *cand;
4244 bool any_viable_p;
4245 void *p;
4246 tree result;
4248 if (args != NULL && *args != NULL)
4250 *args = resolve_args (*args, complain);
4251 if (*args == NULL)
4252 return error_mark_node;
4255 if (flag_tm)
4256 tm_malloc_replacement (fn);
4258 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4259 p = conversion_obstack_alloc (0);
4261 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
4262 complain);
4264 if (!cand)
4266 if (complain & tf_error)
4268 // If there is a single (non-viable) function candidate,
4269 // let the error be diagnosed by cp_build_function_call_vec.
4270 if (!any_viable_p && candidates && ! candidates->next
4271 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
4272 return cp_build_function_call_vec (candidates->fn, args, complain);
4274 // Otherwise, emit notes for non-viable candidates.
4275 print_error_for_call_failure (fn, *args, candidates);
4277 result = error_mark_node;
4279 else
4281 int flags = LOOKUP_NORMAL;
4282 /* If fn is template_id_expr, the call has explicit template arguments
4283 (e.g. func<int>(5)), communicate this info to build_over_call
4284 through flags so that later we can use it to decide whether to warn
4285 about peculiar null pointer conversion. */
4286 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4288 /* If overload resolution selects a specialization of a
4289 function concept for non-dependent template arguments,
4290 the expression is true if the constraints are satisfied
4291 and false otherwise.
4293 NOTE: This is an extension of Concepts Lite TS that
4294 allows constraints to be used in expressions. */
4295 if (flag_concepts && !processing_template_decl)
4297 tree tmpl = DECL_TI_TEMPLATE (cand->fn);
4298 tree targs = DECL_TI_ARGS (cand->fn);
4299 tree decl = DECL_TEMPLATE_RESULT (tmpl);
4300 if (DECL_DECLARED_CONCEPT_P (decl))
4301 return evaluate_function_concept (decl, targs);
4304 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
4307 result = build_over_call (cand, flags, complain);
4310 /* Free all the conversions we allocated. */
4311 obstack_free (&conversion_obstack, p);
4313 return result;
4316 /* Build a call to a global operator new. FNNAME is the name of the
4317 operator (either "operator new" or "operator new[]") and ARGS are
4318 the arguments provided. This may change ARGS. *SIZE points to the
4319 total number of bytes required by the allocation, and is updated if
4320 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
4321 be used. If this function determines that no cookie should be
4322 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
4323 is not NULL_TREE, it is evaluated before calculating the final
4324 array size, and if it fails, the array size is replaced with
4325 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
4326 is non-NULL, it will be set, upon return, to the allocation
4327 function called. */
4329 tree
4330 build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
4331 tree *size, tree *cookie_size,
4332 tree align_arg, tree size_check,
4333 tree *fn, tsubst_flags_t complain)
4335 tree original_size = *size;
4336 tree fns;
4337 struct z_candidate *candidates;
4338 struct z_candidate *cand = NULL;
4339 bool any_viable_p;
4341 if (fn)
4342 *fn = NULL_TREE;
4343 /* Set to (size_t)-1 if the size check fails. */
4344 if (size_check != NULL_TREE)
4346 tree errval = TYPE_MAX_VALUE (sizetype);
4347 if (cxx_dialect >= cxx11 && flag_exceptions)
4348 errval = throw_bad_array_new_length ();
4349 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4350 original_size, errval);
4352 vec_safe_insert (*args, 0, *size);
4353 *args = resolve_args (*args, complain);
4354 if (*args == NULL)
4355 return error_mark_node;
4357 /* Based on:
4359 [expr.new]
4361 If this lookup fails to find the name, or if the allocated type
4362 is not a class type, the allocation function's name is looked
4363 up in the global scope.
4365 we disregard block-scope declarations of "operator new". */
4366 fns = lookup_name_real (fnname, 0, 1, /*block_p=*/false, 0, 0);
4367 fns = lookup_arg_dependent (fnname, fns, *args);
4369 if (align_arg)
4371 vec<tree, va_gc>* align_args
4372 = vec_copy_and_insert (*args, align_arg, 1);
4373 cand = perform_overload_resolution (fns, align_args, &candidates,
4374 &any_viable_p, tf_none);
4375 if (cand)
4376 *args = align_args;
4377 /* If no aligned allocation function matches, try again without the
4378 alignment. */
4381 /* Figure out what function is being called. */
4382 if (!cand)
4383 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
4384 complain);
4386 /* If no suitable function could be found, issue an error message
4387 and give up. */
4388 if (!cand)
4390 if (complain & tf_error)
4391 print_error_for_call_failure (fns, *args, candidates);
4392 return error_mark_node;
4395 /* If a cookie is required, add some extra space. Whether
4396 or not a cookie is required cannot be determined until
4397 after we know which function was called. */
4398 if (*cookie_size)
4400 bool use_cookie = true;
4401 tree arg_types;
4403 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
4404 /* Skip the size_t parameter. */
4405 arg_types = TREE_CHAIN (arg_types);
4406 /* Check the remaining parameters (if any). */
4407 if (arg_types
4408 && TREE_CHAIN (arg_types) == void_list_node
4409 && same_type_p (TREE_VALUE (arg_types),
4410 ptr_type_node))
4411 use_cookie = false;
4412 /* If we need a cookie, adjust the number of bytes allocated. */
4413 if (use_cookie)
4415 /* Update the total size. */
4416 *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
4417 if (size_check)
4419 /* Set to (size_t)-1 if the size check fails. */
4420 gcc_assert (size_check != NULL_TREE);
4421 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4422 *size, TYPE_MAX_VALUE (sizetype));
4424 /* Update the argument list to reflect the adjusted size. */
4425 (**args)[0] = *size;
4427 else
4428 *cookie_size = NULL_TREE;
4431 /* Tell our caller which function we decided to call. */
4432 if (fn)
4433 *fn = cand->fn;
4435 /* Build the CALL_EXPR. */
4436 return build_over_call (cand, LOOKUP_NORMAL, complain);
4439 /* Build a new call to operator(). This may change ARGS. */
4441 static tree
4442 build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4444 struct z_candidate *candidates = 0, *cand;
4445 tree fns, convs, first_mem_arg = NULL_TREE;
4446 bool any_viable_p;
4447 tree result = NULL_TREE;
4448 void *p;
4450 obj = mark_lvalue_use (obj);
4452 if (error_operand_p (obj))
4453 return error_mark_node;
4455 tree type = TREE_TYPE (obj);
4457 obj = prep_operand (obj);
4459 if (TYPE_PTRMEMFUNC_P (type))
4461 if (complain & tf_error)
4462 /* It's no good looking for an overloaded operator() on a
4463 pointer-to-member-function. */
4464 error ("pointer-to-member function %qE cannot be called without "
4465 "an object; consider using %<.*%> or %<->*%>", obj);
4466 return error_mark_node;
4469 if (TYPE_BINFO (type))
4471 fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1);
4472 if (fns == error_mark_node)
4473 return error_mark_node;
4475 else
4476 fns = NULL_TREE;
4478 if (args != NULL && *args != NULL)
4480 *args = resolve_args (*args, complain);
4481 if (*args == NULL)
4482 return error_mark_node;
4485 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4486 p = conversion_obstack_alloc (0);
4488 if (fns)
4490 first_mem_arg = obj;
4492 add_candidates (BASELINK_FUNCTIONS (fns),
4493 first_mem_arg, *args, NULL_TREE,
4494 NULL_TREE, false,
4495 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
4496 LOOKUP_NORMAL, &candidates, complain);
4499 convs = lookup_conversions (type);
4501 for (; convs; convs = TREE_CHAIN (convs))
4503 tree totype = TREE_TYPE (convs);
4505 if (TYPE_PTRFN_P (totype)
4506 || TYPE_REFFN_P (totype)
4507 || (TREE_CODE (totype) == REFERENCE_TYPE
4508 && TYPE_PTRFN_P (TREE_TYPE (totype))))
4509 for (ovl_iterator iter (TREE_VALUE (convs)); iter; ++iter)
4511 tree fn = *iter;
4513 if (DECL_NONCONVERTING_P (fn))
4514 continue;
4516 if (TREE_CODE (fn) == TEMPLATE_DECL)
4517 add_template_conv_candidate
4518 (&candidates, fn, obj, *args, totype,
4519 /*access_path=*/NULL_TREE,
4520 /*conversion_path=*/NULL_TREE, complain);
4521 else
4522 add_conv_candidate (&candidates, fn, obj,
4523 *args, /*conversion_path=*/NULL_TREE,
4524 /*access_path=*/NULL_TREE, complain);
4528 /* Be strict here because if we choose a bad conversion candidate, the
4529 errors we get won't mention the call context. */
4530 candidates = splice_viable (candidates, true, &any_viable_p);
4531 if (!any_viable_p)
4533 if (complain & tf_error)
4535 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
4536 build_tree_list_vec (*args));
4537 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4539 result = error_mark_node;
4541 else
4543 cand = tourney (candidates, complain);
4544 if (cand == 0)
4546 if (complain & tf_error)
4548 error ("call of %<(%T) (%A)%> is ambiguous",
4549 TREE_TYPE (obj), build_tree_list_vec (*args));
4550 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4552 result = error_mark_node;
4554 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
4555 && DECL_OVERLOADED_OPERATOR_P (cand->fn)
4556 && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
4557 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4558 else
4560 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
4561 obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
4562 -1, complain);
4563 else
4565 gcc_checking_assert (TYPE_P (cand->fn));
4566 obj = convert_like (cand->convs[0], obj, complain);
4568 obj = convert_from_reference (obj);
4569 result = cp_build_function_call_vec (obj, args, complain);
4573 /* Free all the conversions we allocated. */
4574 obstack_free (&conversion_obstack, p);
4576 return result;
4579 /* Wrapper for above. */
4581 tree
4582 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4584 tree ret;
4585 bool subtime = timevar_cond_start (TV_OVERLOAD);
4586 ret = build_op_call_1 (obj, args, complain);
4587 timevar_cond_stop (TV_OVERLOAD, subtime);
4588 return ret;
4591 /* Called by op_error to prepare format strings suitable for the error
4592 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4593 and a suffix (controlled by NTYPES). */
4595 static const char *
4596 op_error_string (const char *errmsg, int ntypes, bool match)
4598 const char *msg;
4600 const char *msgp = concat (match ? G_("ambiguous overload for ")
4601 : G_("no match for "), errmsg, NULL);
4603 if (ntypes == 3)
4604 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
4605 else if (ntypes == 2)
4606 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
4607 else
4608 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
4610 return msg;
4613 static void
4614 op_error (location_t loc, enum tree_code code, enum tree_code code2,
4615 tree arg1, tree arg2, tree arg3, bool match)
4617 bool assop = code == MODIFY_EXPR;
4618 const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
4620 switch (code)
4622 case COND_EXPR:
4623 if (flag_diagnostics_show_caret)
4624 error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
4625 3, match),
4626 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4627 else
4628 error_at (loc, op_error_string (G_("ternary %<operator?:%> "
4629 "in %<%E ? %E : %E%>"), 3, match),
4630 arg1, arg2, arg3,
4631 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4632 break;
4634 case POSTINCREMENT_EXPR:
4635 case POSTDECREMENT_EXPR:
4636 if (flag_diagnostics_show_caret)
4637 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4638 opname, TREE_TYPE (arg1));
4639 else
4640 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4641 1, match),
4642 opname, arg1, opname, TREE_TYPE (arg1));
4643 break;
4645 case ARRAY_REF:
4646 if (flag_diagnostics_show_caret)
4647 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
4648 TREE_TYPE (arg1), TREE_TYPE (arg2));
4649 else
4650 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
4651 2, match),
4652 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
4653 break;
4655 case REALPART_EXPR:
4656 case IMAGPART_EXPR:
4657 if (flag_diagnostics_show_caret)
4658 error_at (loc, op_error_string (G_("%qs"), 1, match),
4659 opname, TREE_TYPE (arg1));
4660 else
4661 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
4662 opname, opname, arg1, TREE_TYPE (arg1));
4663 break;
4665 default:
4666 if (arg2)
4667 if (flag_diagnostics_show_caret)
4668 error_at (loc, op_error_string (G_("%<operator%s%>"), 2, match),
4669 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
4670 else
4671 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
4672 2, match),
4673 opname, arg1, opname, arg2,
4674 TREE_TYPE (arg1), TREE_TYPE (arg2));
4675 else
4676 if (flag_diagnostics_show_caret)
4677 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4678 opname, TREE_TYPE (arg1));
4679 else
4680 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
4681 1, match),
4682 opname, opname, arg1, TREE_TYPE (arg1));
4683 break;
4687 /* Return the implicit conversion sequence that could be used to
4688 convert E1 to E2 in [expr.cond]. */
4690 static conversion *
4691 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
4693 tree t1 = non_reference (TREE_TYPE (e1));
4694 tree t2 = non_reference (TREE_TYPE (e2));
4695 conversion *conv;
4696 bool good_base;
4698 /* [expr.cond]
4700 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
4701 implicitly converted (clause _conv_) to the type "lvalue reference to
4702 T2", subject to the constraint that in the conversion the
4703 reference must bind directly (_dcl.init.ref_) to an lvalue.
4705 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
4706 implicitly converted to the type "rvalue reference to T2", subject to
4707 the constraint that the reference must bind directly. */
4708 if (glvalue_p (e2))
4710 tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
4711 conv = implicit_conversion (rtype,
4714 /*c_cast_p=*/false,
4715 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
4716 |LOOKUP_ONLYCONVERTING,
4717 complain);
4718 if (conv && !conv->bad_p)
4719 return conv;
4722 /* If E2 is a prvalue or if neither of the conversions above can be done
4723 and at least one of the operands has (possibly cv-qualified) class
4724 type: */
4725 if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
4726 return NULL;
4728 /* [expr.cond]
4730 If E1 and E2 have class type, and the underlying class types are
4731 the same or one is a base class of the other: E1 can be converted
4732 to match E2 if the class of T2 is the same type as, or a base
4733 class of, the class of T1, and the cv-qualification of T2 is the
4734 same cv-qualification as, or a greater cv-qualification than, the
4735 cv-qualification of T1. If the conversion is applied, E1 is
4736 changed to an rvalue of type T2 that still refers to the original
4737 source class object (or the appropriate subobject thereof). */
4738 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
4739 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
4741 if (good_base && at_least_as_qualified_p (t2, t1))
4743 conv = build_identity_conv (t1, e1);
4744 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
4745 TYPE_MAIN_VARIANT (t2)))
4746 conv = build_conv (ck_base, t2, conv);
4747 else
4748 conv = build_conv (ck_rvalue, t2, conv);
4749 return conv;
4751 else
4752 return NULL;
4754 else
4755 /* [expr.cond]
4757 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
4758 converted to the type that expression E2 would have if E2 were
4759 converted to an rvalue (or the type it has, if E2 is an rvalue). */
4760 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
4761 LOOKUP_IMPLICIT, complain);
4764 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
4765 arguments to the conditional expression. */
4767 static tree
4768 build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
4769 tsubst_flags_t complain)
4771 tree arg2_type;
4772 tree arg3_type;
4773 tree result = NULL_TREE;
4774 tree result_type = NULL_TREE;
4775 bool is_lvalue = true;
4776 struct z_candidate *candidates = 0;
4777 struct z_candidate *cand;
4778 void *p;
4779 tree orig_arg2, orig_arg3;
4781 /* As a G++ extension, the second argument to the conditional can be
4782 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
4783 c'.) If the second operand is omitted, make sure it is
4784 calculated only once. */
4785 if (!arg2)
4787 if (complain & tf_error)
4788 pedwarn (loc, OPT_Wpedantic,
4789 "ISO C++ forbids omitting the middle term of a ?: expression");
4791 if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
4792 warn_for_omitted_condop (loc, arg1);
4794 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
4795 if (lvalue_p (arg1))
4796 arg2 = arg1 = cp_stabilize_reference (arg1);
4797 else
4798 arg2 = arg1 = save_expr (arg1);
4801 /* If something has already gone wrong, just pass that fact up the
4802 tree. */
4803 if (error_operand_p (arg1)
4804 || error_operand_p (arg2)
4805 || error_operand_p (arg3))
4806 return error_mark_node;
4808 orig_arg2 = arg2;
4809 orig_arg3 = arg3;
4811 if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
4813 tree arg1_type = TREE_TYPE (arg1);
4815 /* If arg1 is another cond_expr choosing between -1 and 0,
4816 then we can use its comparison. It may help to avoid
4817 additional comparison, produce more accurate diagnostics
4818 and enables folding. */
4819 if (TREE_CODE (arg1) == VEC_COND_EXPR
4820 && integer_minus_onep (TREE_OPERAND (arg1, 1))
4821 && integer_zerop (TREE_OPERAND (arg1, 2)))
4822 arg1 = TREE_OPERAND (arg1, 0);
4824 arg1 = force_rvalue (arg1, complain);
4825 arg2 = force_rvalue (arg2, complain);
4826 arg3 = force_rvalue (arg3, complain);
4828 /* force_rvalue can return error_mark on valid arguments. */
4829 if (error_operand_p (arg1)
4830 || error_operand_p (arg2)
4831 || error_operand_p (arg3))
4832 return error_mark_node;
4834 arg2_type = TREE_TYPE (arg2);
4835 arg3_type = TREE_TYPE (arg3);
4837 if (!VECTOR_TYPE_P (arg2_type)
4838 && !VECTOR_TYPE_P (arg3_type))
4840 /* Rely on the error messages of the scalar version. */
4841 tree scal = build_conditional_expr_1 (loc, integer_one_node,
4842 orig_arg2, orig_arg3, complain);
4843 if (scal == error_mark_node)
4844 return error_mark_node;
4845 tree stype = TREE_TYPE (scal);
4846 tree ctype = TREE_TYPE (arg1_type);
4847 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
4848 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
4850 if (complain & tf_error)
4851 error_at (loc, "inferred scalar type %qT is not an integer or "
4852 "floating point type of the same size as %qT", stype,
4853 COMPARISON_CLASS_P (arg1)
4854 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4855 : ctype);
4856 return error_mark_node;
4859 tree vtype = build_opaque_vector_type (stype,
4860 TYPE_VECTOR_SUBPARTS (arg1_type));
4861 /* We could pass complain & tf_warning to unsafe_conversion_p,
4862 but the warnings (like Wsign-conversion) have already been
4863 given by the scalar build_conditional_expr_1. We still check
4864 unsafe_conversion_p to forbid truncating long long -> float. */
4865 if (unsafe_conversion_p (loc, stype, arg2, NULL_TREE, false))
4867 if (complain & tf_error)
4868 error_at (loc, "conversion of scalar %qH to vector %qI "
4869 "involves truncation", arg2_type, vtype);
4870 return error_mark_node;
4872 if (unsafe_conversion_p (loc, stype, arg3, NULL_TREE, false))
4874 if (complain & tf_error)
4875 error_at (loc, "conversion of scalar %qH to vector %qI "
4876 "involves truncation", arg3_type, vtype);
4877 return error_mark_node;
4880 arg2 = cp_convert (stype, arg2, complain);
4881 arg2 = save_expr (arg2);
4882 arg2 = build_vector_from_val (vtype, arg2);
4883 arg2_type = vtype;
4884 arg3 = cp_convert (stype, arg3, complain);
4885 arg3 = save_expr (arg3);
4886 arg3 = build_vector_from_val (vtype, arg3);
4887 arg3_type = vtype;
4890 if (VECTOR_TYPE_P (arg2_type) != VECTOR_TYPE_P (arg3_type))
4892 enum stv_conv convert_flag =
4893 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
4894 complain & tf_error);
4896 switch (convert_flag)
4898 case stv_error:
4899 return error_mark_node;
4900 case stv_firstarg:
4902 arg2 = save_expr (arg2);
4903 arg2 = convert (TREE_TYPE (arg3_type), arg2);
4904 arg2 = build_vector_from_val (arg3_type, arg2);
4905 arg2_type = TREE_TYPE (arg2);
4906 break;
4908 case stv_secondarg:
4910 arg3 = save_expr (arg3);
4911 arg3 = convert (TREE_TYPE (arg2_type), arg3);
4912 arg3 = build_vector_from_val (arg2_type, arg3);
4913 arg3_type = TREE_TYPE (arg3);
4914 break;
4916 default:
4917 break;
4921 if (!same_type_p (arg2_type, arg3_type)
4922 || TYPE_VECTOR_SUBPARTS (arg1_type)
4923 != TYPE_VECTOR_SUBPARTS (arg2_type)
4924 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
4926 if (complain & tf_error)
4927 error_at (loc,
4928 "incompatible vector types in conditional expression: "
4929 "%qT, %qT and %qT", TREE_TYPE (arg1),
4930 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
4931 return error_mark_node;
4934 if (!COMPARISON_CLASS_P (arg1))
4936 tree cmp_type = build_same_sized_truth_vector_type (arg1_type);
4937 arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
4939 return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
4942 /* [expr.cond]
4944 The first expression is implicitly converted to bool (clause
4945 _conv_). */
4946 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
4947 LOOKUP_NORMAL);
4948 if (error_operand_p (arg1))
4949 return error_mark_node;
4951 /* [expr.cond]
4953 If either the second or the third operand has type (possibly
4954 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
4955 array-to-pointer (_conv.array_), and function-to-pointer
4956 (_conv.func_) standard conversions are performed on the second
4957 and third operands. */
4958 arg2_type = unlowered_expr_type (arg2);
4959 arg3_type = unlowered_expr_type (arg3);
4960 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
4962 /* Do the conversions. We don't these for `void' type arguments
4963 since it can't have any effect and since decay_conversion
4964 does not handle that case gracefully. */
4965 if (!VOID_TYPE_P (arg2_type))
4966 arg2 = decay_conversion (arg2, complain);
4967 if (!VOID_TYPE_P (arg3_type))
4968 arg3 = decay_conversion (arg3, complain);
4969 arg2_type = TREE_TYPE (arg2);
4970 arg3_type = TREE_TYPE (arg3);
4972 /* [expr.cond]
4974 One of the following shall hold:
4976 --The second or the third operand (but not both) is a
4977 throw-expression (_except.throw_); the result is of the
4978 type of the other and is an rvalue.
4980 --Both the second and the third operands have type void; the
4981 result is of type void and is an rvalue.
4983 We must avoid calling force_rvalue for expressions of type
4984 "void" because it will complain that their value is being
4985 used. */
4986 if (TREE_CODE (arg2) == THROW_EXPR
4987 && TREE_CODE (arg3) != THROW_EXPR)
4989 if (!VOID_TYPE_P (arg3_type))
4991 arg3 = force_rvalue (arg3, complain);
4992 if (arg3 == error_mark_node)
4993 return error_mark_node;
4995 arg3_type = TREE_TYPE (arg3);
4996 result_type = arg3_type;
4998 else if (TREE_CODE (arg2) != THROW_EXPR
4999 && TREE_CODE (arg3) == THROW_EXPR)
5001 if (!VOID_TYPE_P (arg2_type))
5003 arg2 = force_rvalue (arg2, complain);
5004 if (arg2 == error_mark_node)
5005 return error_mark_node;
5007 arg2_type = TREE_TYPE (arg2);
5008 result_type = arg2_type;
5010 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
5011 result_type = void_type_node;
5012 else
5014 if (complain & tf_error)
5016 if (VOID_TYPE_P (arg2_type))
5017 error_at (EXPR_LOC_OR_LOC (arg3, loc),
5018 "second operand to the conditional operator "
5019 "is of type %<void%>, but the third operand is "
5020 "neither a throw-expression nor of type %<void%>");
5021 else
5022 error_at (EXPR_LOC_OR_LOC (arg2, loc),
5023 "third operand to the conditional operator "
5024 "is of type %<void%>, but the second operand is "
5025 "neither a throw-expression nor of type %<void%>");
5027 return error_mark_node;
5030 is_lvalue = false;
5031 goto valid_operands;
5033 /* [expr.cond]
5035 Otherwise, if the second and third operand have different types,
5036 and either has (possibly cv-qualified) class type, or if both are
5037 glvalues of the same value category and the same type except for
5038 cv-qualification, an attempt is made to convert each of those operands
5039 to the type of the other. */
5040 else if (!same_type_p (arg2_type, arg3_type)
5041 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
5042 || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
5043 arg3_type)
5044 && glvalue_p (arg2) && glvalue_p (arg3)
5045 && lvalue_p (arg2) == lvalue_p (arg3))))
5047 conversion *conv2;
5048 conversion *conv3;
5049 bool converted = false;
5051 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5052 p = conversion_obstack_alloc (0);
5054 conv2 = conditional_conversion (arg2, arg3, complain);
5055 conv3 = conditional_conversion (arg3, arg2, complain);
5057 /* [expr.cond]
5059 If both can be converted, or one can be converted but the
5060 conversion is ambiguous, the program is ill-formed. If
5061 neither can be converted, the operands are left unchanged and
5062 further checking is performed as described below. If exactly
5063 one conversion is possible, that conversion is applied to the
5064 chosen operand and the converted operand is used in place of
5065 the original operand for the remainder of this section. */
5066 if ((conv2 && !conv2->bad_p
5067 && conv3 && !conv3->bad_p)
5068 || (conv2 && conv2->kind == ck_ambig)
5069 || (conv3 && conv3->kind == ck_ambig))
5071 if (complain & tf_error)
5073 error_at (loc, "operands to ?: have different types %qT and %qT",
5074 arg2_type, arg3_type);
5075 if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
5076 inform (loc, " and each type can be converted to the other");
5077 else if (conv2 && conv2->kind == ck_ambig)
5078 convert_like (conv2, arg2, complain);
5079 else
5080 convert_like (conv3, arg3, complain);
5082 result = error_mark_node;
5084 else if (conv2 && !conv2->bad_p)
5086 arg2 = convert_like (conv2, arg2, complain);
5087 arg2 = convert_from_reference (arg2);
5088 arg2_type = TREE_TYPE (arg2);
5089 /* Even if CONV2 is a valid conversion, the result of the
5090 conversion may be invalid. For example, if ARG3 has type
5091 "volatile X", and X does not have a copy constructor
5092 accepting a "volatile X&", then even if ARG2 can be
5093 converted to X, the conversion will fail. */
5094 if (error_operand_p (arg2))
5095 result = error_mark_node;
5096 converted = true;
5098 else if (conv3 && !conv3->bad_p)
5100 arg3 = convert_like (conv3, arg3, complain);
5101 arg3 = convert_from_reference (arg3);
5102 arg3_type = TREE_TYPE (arg3);
5103 if (error_operand_p (arg3))
5104 result = error_mark_node;
5105 converted = true;
5108 /* Free all the conversions we allocated. */
5109 obstack_free (&conversion_obstack, p);
5111 if (result)
5112 return result;
5114 /* If, after the conversion, both operands have class type,
5115 treat the cv-qualification of both operands as if it were the
5116 union of the cv-qualification of the operands.
5118 The standard is not clear about what to do in this
5119 circumstance. For example, if the first operand has type
5120 "const X" and the second operand has a user-defined
5121 conversion to "volatile X", what is the type of the second
5122 operand after this step? Making it be "const X" (matching
5123 the first operand) seems wrong, as that discards the
5124 qualification without actually performing a copy. Leaving it
5125 as "volatile X" seems wrong as that will result in the
5126 conditional expression failing altogether, even though,
5127 according to this step, the one operand could be converted to
5128 the type of the other. */
5129 if (converted
5130 && CLASS_TYPE_P (arg2_type)
5131 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
5132 arg2_type = arg3_type =
5133 cp_build_qualified_type (arg2_type,
5134 cp_type_quals (arg2_type)
5135 | cp_type_quals (arg3_type));
5138 /* [expr.cond]
5140 If the second and third operands are glvalues of the same value
5141 category and have the same type, the result is of that type and
5142 value category. */
5143 if (((lvalue_p (arg2) && lvalue_p (arg3))
5144 || (xvalue_p (arg2) && xvalue_p (arg3)))
5145 && same_type_p (arg2_type, arg3_type))
5147 result_type = arg2_type;
5148 arg2 = mark_lvalue_use (arg2);
5149 arg3 = mark_lvalue_use (arg3);
5150 goto valid_operands;
5153 /* [expr.cond]
5155 Otherwise, the result is an rvalue. If the second and third
5156 operand do not have the same type, and either has (possibly
5157 cv-qualified) class type, overload resolution is used to
5158 determine the conversions (if any) to be applied to the operands
5159 (_over.match.oper_, _over.built_). */
5160 is_lvalue = false;
5161 if (!same_type_p (arg2_type, arg3_type)
5162 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
5164 tree args[3];
5165 conversion *conv;
5166 bool any_viable_p;
5168 /* Rearrange the arguments so that add_builtin_candidate only has
5169 to know about two args. In build_builtin_candidate, the
5170 arguments are unscrambled. */
5171 args[0] = arg2;
5172 args[1] = arg3;
5173 args[2] = arg1;
5174 add_builtin_candidates (&candidates,
5175 COND_EXPR,
5176 NOP_EXPR,
5177 ovl_op_identifier (false, COND_EXPR),
5178 args,
5179 LOOKUP_NORMAL, complain);
5181 /* [expr.cond]
5183 If the overload resolution fails, the program is
5184 ill-formed. */
5185 candidates = splice_viable (candidates, false, &any_viable_p);
5186 if (!any_viable_p)
5188 if (complain & tf_error)
5189 error_at (loc, "operands to ?: have different types %qT and %qT",
5190 arg2_type, arg3_type);
5191 return error_mark_node;
5193 cand = tourney (candidates, complain);
5194 if (!cand)
5196 if (complain & tf_error)
5198 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
5199 print_z_candidates (loc, candidates);
5201 return error_mark_node;
5204 /* [expr.cond]
5206 Otherwise, the conversions thus determined are applied, and
5207 the converted operands are used in place of the original
5208 operands for the remainder of this section. */
5209 conv = cand->convs[0];
5210 arg1 = convert_like (conv, arg1, complain);
5211 conv = cand->convs[1];
5212 arg2 = convert_like (conv, arg2, complain);
5213 arg2_type = TREE_TYPE (arg2);
5214 conv = cand->convs[2];
5215 arg3 = convert_like (conv, arg3, complain);
5216 arg3_type = TREE_TYPE (arg3);
5219 /* [expr.cond]
5221 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
5222 and function-to-pointer (_conv.func_) standard conversions are
5223 performed on the second and third operands.
5225 We need to force the lvalue-to-rvalue conversion here for class types,
5226 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
5227 that isn't wrapped with a TARGET_EXPR plays havoc with exception
5228 regions. */
5230 arg2 = force_rvalue (arg2, complain);
5231 if (!CLASS_TYPE_P (arg2_type))
5232 arg2_type = TREE_TYPE (arg2);
5234 arg3 = force_rvalue (arg3, complain);
5235 if (!CLASS_TYPE_P (arg3_type))
5236 arg3_type = TREE_TYPE (arg3);
5238 if (arg2 == error_mark_node || arg3 == error_mark_node)
5239 return error_mark_node;
5241 /* [expr.cond]
5243 After those conversions, one of the following shall hold:
5245 --The second and third operands have the same type; the result is of
5246 that type. */
5247 if (same_type_p (arg2_type, arg3_type))
5248 result_type = arg2_type;
5249 /* [expr.cond]
5251 --The second and third operands have arithmetic or enumeration
5252 type; the usual arithmetic conversions are performed to bring
5253 them to a common type, and the result is of that type. */
5254 else if ((ARITHMETIC_TYPE_P (arg2_type)
5255 || UNSCOPED_ENUM_P (arg2_type))
5256 && (ARITHMETIC_TYPE_P (arg3_type)
5257 || UNSCOPED_ENUM_P (arg3_type)))
5259 /* In this case, there is always a common type. */
5260 result_type = type_after_usual_arithmetic_conversions (arg2_type,
5261 arg3_type);
5262 if (complain & tf_warning)
5263 do_warn_double_promotion (result_type, arg2_type, arg3_type,
5264 "implicit conversion from %qH to %qI to "
5265 "match other result of conditional",
5266 loc);
5268 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
5269 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
5271 if (TREE_CODE (orig_arg2) == CONST_DECL
5272 && TREE_CODE (orig_arg3) == CONST_DECL
5273 && DECL_CONTEXT (orig_arg2) == DECL_CONTEXT (orig_arg3))
5274 /* Two enumerators from the same enumeration can have different
5275 types when the enumeration is still being defined. */;
5276 else if (complain & tf_warning)
5277 warning_at (loc, OPT_Wenum_compare, "enumeral mismatch in "
5278 "conditional expression: %qT vs %qT",
5279 arg2_type, arg3_type);
5281 else if (extra_warnings
5282 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
5283 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
5284 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
5285 && !same_type_p (arg2_type,
5286 type_promotes_to (arg3_type)))))
5288 if (complain & tf_warning)
5289 warning_at (loc, OPT_Wextra, "enumeral and non-enumeral type in "
5290 "conditional expression");
5293 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5294 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5296 /* [expr.cond]
5298 --The second and third operands have pointer type, or one has
5299 pointer type and the other is a null pointer constant; pointer
5300 conversions (_conv.ptr_) and qualification conversions
5301 (_conv.qual_) are performed to bring them to their composite
5302 pointer type (_expr.rel_). The result is of the composite
5303 pointer type.
5305 --The second and third operands have pointer to member type, or
5306 one has pointer to member type and the other is a null pointer
5307 constant; pointer to member conversions (_conv.mem_) and
5308 qualification conversions (_conv.qual_) are performed to bring
5309 them to a common type, whose cv-qualification shall match the
5310 cv-qualification of either the second or the third operand.
5311 The result is of the common type. */
5312 else if ((null_ptr_cst_p (arg2)
5313 && TYPE_PTR_OR_PTRMEM_P (arg3_type))
5314 || (null_ptr_cst_p (arg3)
5315 && TYPE_PTR_OR_PTRMEM_P (arg2_type))
5316 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
5317 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
5318 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
5320 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
5321 arg3, CPO_CONDITIONAL_EXPR,
5322 complain);
5323 if (result_type == error_mark_node)
5324 return error_mark_node;
5325 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5326 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5329 if (!result_type)
5331 if (complain & tf_error)
5332 error_at (loc, "operands to ?: have different types %qT and %qT",
5333 arg2_type, arg3_type);
5334 return error_mark_node;
5337 if (arg2 == error_mark_node || arg3 == error_mark_node)
5338 return error_mark_node;
5340 valid_operands:
5341 result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
5343 /* If the ARG2 and ARG3 are the same and don't have side-effects,
5344 warn here, because the COND_EXPR will be turned into ARG2. */
5345 if (warn_duplicated_branches
5346 && (arg2 == arg3 || operand_equal_p (arg2, arg3, 0)))
5347 warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
5348 "this condition has identical branches");
5350 /* We can't use result_type below, as fold might have returned a
5351 throw_expr. */
5353 if (!is_lvalue)
5355 /* Expand both sides into the same slot, hopefully the target of
5356 the ?: expression. We used to check for TARGET_EXPRs here,
5357 but now we sometimes wrap them in NOP_EXPRs so the test would
5358 fail. */
5359 if (CLASS_TYPE_P (TREE_TYPE (result)))
5360 result = get_target_expr_sfinae (result, complain);
5361 /* If this expression is an rvalue, but might be mistaken for an
5362 lvalue, we must add a NON_LVALUE_EXPR. */
5363 result = rvalue (result);
5365 else
5366 result = force_paren_expr (result);
5368 return result;
5371 /* Wrapper for above. */
5373 tree
5374 build_conditional_expr (location_t loc, tree arg1, tree arg2, tree arg3,
5375 tsubst_flags_t complain)
5377 tree ret;
5378 bool subtime = timevar_cond_start (TV_OVERLOAD);
5379 ret = build_conditional_expr_1 (loc, arg1, arg2, arg3, complain);
5380 timevar_cond_stop (TV_OVERLOAD, subtime);
5381 return ret;
5384 /* OPERAND is an operand to an expression. Perform necessary steps
5385 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
5386 returned. */
5388 static tree
5389 prep_operand (tree operand)
5391 if (operand)
5393 if (CLASS_TYPE_P (TREE_TYPE (operand))
5394 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
5395 /* Make sure the template type is instantiated now. */
5396 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
5399 return operand;
5402 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5403 OVERLOAD) to the CANDIDATES, returning an updated list of
5404 CANDIDATES. The ARGS are the arguments provided to the call;
5405 if FIRST_ARG is non-null it is the implicit object argument,
5406 otherwise the first element of ARGS is used if needed. The
5407 EXPLICIT_TARGS are explicit template arguments provided.
5408 TEMPLATE_ONLY is true if only template functions should be
5409 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5410 add_function_candidate. */
5412 static void
5413 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
5414 tree return_type,
5415 tree explicit_targs, bool template_only,
5416 tree conversion_path, tree access_path,
5417 int flags,
5418 struct z_candidate **candidates,
5419 tsubst_flags_t complain)
5421 tree ctype;
5422 const vec<tree, va_gc> *non_static_args;
5423 bool check_list_ctor = false;
5424 bool check_converting = false;
5425 unification_kind_t strict;
5427 if (!fns)
5428 return;
5430 /* Precalculate special handling of constructors and conversion ops. */
5431 tree fn = OVL_FIRST (fns);
5432 if (DECL_CONV_FN_P (fn))
5434 check_list_ctor = false;
5435 check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
5436 if (flags & LOOKUP_NO_CONVERSION)
5437 /* We're doing return_type(x). */
5438 strict = DEDUCE_CONV;
5439 else
5440 /* We're doing x.operator return_type(). */
5441 strict = DEDUCE_EXACT;
5442 /* [over.match.funcs] For conversion functions, the function
5443 is considered to be a member of the class of the implicit
5444 object argument for the purpose of defining the type of
5445 the implicit object parameter. */
5446 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
5448 else
5450 if (DECL_CONSTRUCTOR_P (fn))
5452 check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
5453 /* For list-initialization we consider explicit constructors
5454 and complain if one is chosen. */
5455 check_converting
5456 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
5457 == LOOKUP_ONLYCONVERTING);
5459 strict = DEDUCE_CALL;
5460 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
5463 if (first_arg)
5464 non_static_args = args;
5465 else
5466 /* Delay creating the implicit this parameter until it is needed. */
5467 non_static_args = NULL;
5469 for (lkp_iterator iter (fns); iter; ++iter)
5471 fn = *iter;
5473 if (check_converting && DECL_NONCONVERTING_P (fn))
5474 continue;
5475 if (check_list_ctor && !is_list_ctor (fn))
5476 continue;
5478 tree fn_first_arg = NULL_TREE;
5479 const vec<tree, va_gc> *fn_args = args;
5481 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
5483 /* Figure out where the object arg comes from. If this
5484 function is a non-static member and we didn't get an
5485 implicit object argument, move it out of args. */
5486 if (first_arg == NULL_TREE)
5488 unsigned int ix;
5489 tree arg;
5490 vec<tree, va_gc> *tempvec;
5491 vec_alloc (tempvec, args->length () - 1);
5492 for (ix = 1; args->iterate (ix, &arg); ++ix)
5493 tempvec->quick_push (arg);
5494 non_static_args = tempvec;
5495 first_arg = (*args)[0];
5498 fn_first_arg = first_arg;
5499 fn_args = non_static_args;
5502 if (TREE_CODE (fn) == TEMPLATE_DECL)
5503 add_template_candidate (candidates,
5505 ctype,
5506 explicit_targs,
5507 fn_first_arg,
5508 fn_args,
5509 return_type,
5510 access_path,
5511 conversion_path,
5512 flags,
5513 strict,
5514 complain);
5515 else if (!template_only)
5516 add_function_candidate (candidates,
5518 ctype,
5519 fn_first_arg,
5520 fn_args,
5521 access_path,
5522 conversion_path,
5523 flags,
5524 complain);
5528 /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
5529 -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
5531 static int
5532 op_is_ordered (tree_code code)
5534 switch (code)
5536 // 5. b @= a
5537 case MODIFY_EXPR:
5538 return (flag_strong_eval_order > 1 ? -1 : 0);
5540 // 6. a[b]
5541 case ARRAY_REF:
5542 return (flag_strong_eval_order > 1 ? 1 : 0);
5544 // 1. a.b
5545 // Not overloadable (yet).
5546 // 2. a->b
5547 // Only one argument.
5548 // 3. a->*b
5549 case MEMBER_REF:
5550 // 7. a << b
5551 case LSHIFT_EXPR:
5552 // 8. a >> b
5553 case RSHIFT_EXPR:
5554 return (flag_strong_eval_order ? 1 : 0);
5556 default:
5557 return 0;
5561 static tree
5562 build_new_op_1 (location_t loc, enum tree_code code, int flags, tree arg1,
5563 tree arg2, tree arg3, tree *overload, tsubst_flags_t complain)
5565 struct z_candidate *candidates = 0, *cand;
5566 vec<tree, va_gc> *arglist;
5567 tree args[3];
5568 tree result = NULL_TREE;
5569 bool result_valid_p = false;
5570 enum tree_code code2 = NOP_EXPR;
5571 enum tree_code code_orig_arg1 = ERROR_MARK;
5572 enum tree_code code_orig_arg2 = ERROR_MARK;
5573 conversion *conv;
5574 void *p;
5575 bool strict_p;
5576 bool any_viable_p;
5578 if (error_operand_p (arg1)
5579 || error_operand_p (arg2)
5580 || error_operand_p (arg3))
5581 return error_mark_node;
5583 bool ismodop = code == MODIFY_EXPR;
5584 if (ismodop)
5586 code2 = TREE_CODE (arg3);
5587 arg3 = NULL_TREE;
5589 tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
5591 arg1 = prep_operand (arg1);
5593 bool memonly = false;
5594 switch (code)
5596 case NEW_EXPR:
5597 case VEC_NEW_EXPR:
5598 case VEC_DELETE_EXPR:
5599 case DELETE_EXPR:
5600 /* Use build_op_new_call and build_op_delete_call instead. */
5601 gcc_unreachable ();
5603 case CALL_EXPR:
5604 /* Use build_op_call instead. */
5605 gcc_unreachable ();
5607 case TRUTH_ORIF_EXPR:
5608 case TRUTH_ANDIF_EXPR:
5609 case TRUTH_AND_EXPR:
5610 case TRUTH_OR_EXPR:
5611 /* These are saved for the sake of warn_logical_operator. */
5612 code_orig_arg1 = TREE_CODE (arg1);
5613 code_orig_arg2 = TREE_CODE (arg2);
5614 break;
5615 case GT_EXPR:
5616 case LT_EXPR:
5617 case GE_EXPR:
5618 case LE_EXPR:
5619 case EQ_EXPR:
5620 case NE_EXPR:
5621 /* These are saved for the sake of maybe_warn_bool_compare. */
5622 code_orig_arg1 = TREE_CODE (TREE_TYPE (arg1));
5623 code_orig_arg2 = TREE_CODE (TREE_TYPE (arg2));
5624 break;
5626 /* =, ->, [], () must be non-static member functions. */
5627 case MODIFY_EXPR:
5628 if (code2 != NOP_EXPR)
5629 break;
5630 /* FALLTHRU */
5631 case COMPONENT_REF:
5632 case ARRAY_REF:
5633 memonly = true;
5634 break;
5636 default:
5637 break;
5640 arg2 = prep_operand (arg2);
5641 arg3 = prep_operand (arg3);
5643 if (code == COND_EXPR)
5644 /* Use build_conditional_expr instead. */
5645 gcc_unreachable ();
5646 else if (! OVERLOAD_TYPE_P (TREE_TYPE (arg1))
5647 && (! arg2 || ! OVERLOAD_TYPE_P (TREE_TYPE (arg2))))
5648 goto builtin;
5650 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5651 arg2 = integer_zero_node;
5653 vec_alloc (arglist, 3);
5654 arglist->quick_push (arg1);
5655 if (arg2 != NULL_TREE)
5656 arglist->quick_push (arg2);
5657 if (arg3 != NULL_TREE)
5658 arglist->quick_push (arg3);
5660 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5661 p = conversion_obstack_alloc (0);
5663 /* Add namespace-scope operators to the list of functions to
5664 consider. */
5665 if (!memonly)
5667 tree fns = lookup_name_real (fnname, 0, 1, /*block_p=*/true, 0, 0);
5668 fns = lookup_arg_dependent (fnname, fns, arglist);
5669 add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
5670 NULL_TREE, false, NULL_TREE, NULL_TREE,
5671 flags, &candidates, complain);
5674 args[0] = arg1;
5675 args[1] = arg2;
5676 args[2] = NULL_TREE;
5678 /* Add class-member operators to the candidate set. */
5679 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
5681 tree fns;
5683 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
5684 if (fns == error_mark_node)
5686 result = error_mark_node;
5687 goto user_defined_result_ready;
5689 if (fns)
5690 add_candidates (BASELINK_FUNCTIONS (fns),
5691 NULL_TREE, arglist, NULL_TREE,
5692 NULL_TREE, false,
5693 BASELINK_BINFO (fns),
5694 BASELINK_ACCESS_BINFO (fns),
5695 flags, &candidates, complain);
5697 /* Per 13.3.1.2/3, 2nd bullet, if no operand has a class type, then
5698 only non-member functions that have type T1 or reference to
5699 cv-qualified-opt T1 for the first argument, if the first argument
5700 has an enumeration type, or T2 or reference to cv-qualified-opt
5701 T2 for the second argument, if the second argument has an
5702 enumeration type. Filter out those that don't match. */
5703 else if (! arg2 || ! CLASS_TYPE_P (TREE_TYPE (arg2)))
5705 struct z_candidate **candp, **next;
5707 for (candp = &candidates; *candp; candp = next)
5709 tree parmlist, parmtype;
5710 int i, nargs = (arg2 ? 2 : 1);
5712 cand = *candp;
5713 next = &cand->next;
5715 parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5717 for (i = 0; i < nargs; ++i)
5719 parmtype = TREE_VALUE (parmlist);
5721 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
5722 parmtype = TREE_TYPE (parmtype);
5723 if (TREE_CODE (TREE_TYPE (args[i])) == ENUMERAL_TYPE
5724 && (same_type_ignoring_top_level_qualifiers_p
5725 (TREE_TYPE (args[i]), parmtype)))
5726 break;
5728 parmlist = TREE_CHAIN (parmlist);
5731 /* No argument has an appropriate type, so remove this
5732 candidate function from the list. */
5733 if (i == nargs)
5735 *candp = cand->next;
5736 next = candp;
5741 add_builtin_candidates (&candidates, code, code2, fnname, args,
5742 flags, complain);
5744 switch (code)
5746 case COMPOUND_EXPR:
5747 case ADDR_EXPR:
5748 /* For these, the built-in candidates set is empty
5749 [over.match.oper]/3. We don't want non-strict matches
5750 because exact matches are always possible with built-in
5751 operators. The built-in candidate set for COMPONENT_REF
5752 would be empty too, but since there are no such built-in
5753 operators, we accept non-strict matches for them. */
5754 strict_p = true;
5755 break;
5757 default:
5758 strict_p = false;
5759 break;
5762 candidates = splice_viable (candidates, strict_p, &any_viable_p);
5763 if (!any_viable_p)
5765 switch (code)
5767 case POSTINCREMENT_EXPR:
5768 case POSTDECREMENT_EXPR:
5769 /* Don't try anything fancy if we're not allowed to produce
5770 errors. */
5771 if (!(complain & tf_error))
5772 return error_mark_node;
5774 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
5775 distinguish between prefix and postfix ++ and
5776 operator++() was used for both, so we allow this with
5777 -fpermissive. */
5778 else
5780 const char *msg = (flag_permissive)
5781 ? G_("no %<%D(int)%> declared for postfix %qs,"
5782 " trying prefix operator instead")
5783 : G_("no %<%D(int)%> declared for postfix %qs");
5784 permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
5787 if (!flag_permissive)
5788 return error_mark_node;
5790 if (code == POSTINCREMENT_EXPR)
5791 code = PREINCREMENT_EXPR;
5792 else
5793 code = PREDECREMENT_EXPR;
5794 result = build_new_op_1 (loc, code, flags, arg1, NULL_TREE,
5795 NULL_TREE, overload, complain);
5796 break;
5798 /* The caller will deal with these. */
5799 case ADDR_EXPR:
5800 case COMPOUND_EXPR:
5801 case COMPONENT_REF:
5802 result = NULL_TREE;
5803 result_valid_p = true;
5804 break;
5806 default:
5807 if (complain & tf_error)
5809 /* If one of the arguments of the operator represents
5810 an invalid use of member function pointer, try to report
5811 a meaningful error ... */
5812 if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
5813 || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
5814 || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
5815 /* We displayed the error message. */;
5816 else
5818 /* ... Otherwise, report the more generic
5819 "no matching operator found" error */
5820 op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
5821 print_z_candidates (loc, candidates);
5824 result = error_mark_node;
5825 break;
5828 else
5830 cand = tourney (candidates, complain);
5831 if (cand == 0)
5833 if (complain & tf_error)
5835 op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
5836 print_z_candidates (loc, candidates);
5838 result = error_mark_node;
5840 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5842 if (overload)
5843 *overload = cand->fn;
5845 if (resolve_args (arglist, complain) == NULL)
5846 result = error_mark_node;
5847 else
5848 result = build_over_call (cand, LOOKUP_NORMAL, complain);
5850 if (trivial_fn_p (cand->fn))
5851 /* There won't be a CALL_EXPR. */;
5852 else if (result && result != error_mark_node)
5854 tree call = extract_call_expr (result);
5855 CALL_EXPR_OPERATOR_SYNTAX (call) = true;
5857 if (processing_template_decl && DECL_HIDDEN_FRIEND_P (cand->fn))
5858 /* This prevents build_new_function_call from discarding this
5859 function during instantiation of the enclosing template. */
5860 KOENIG_LOOKUP_P (call) = 1;
5862 /* Specify evaluation order as per P0145R2. */
5863 CALL_EXPR_ORDERED_ARGS (call) = false;
5864 switch (op_is_ordered (code))
5866 case -1:
5867 CALL_EXPR_REVERSE_ARGS (call) = true;
5868 break;
5870 case 1:
5871 CALL_EXPR_ORDERED_ARGS (call) = true;
5872 break;
5874 default:
5875 break;
5879 else
5881 /* Give any warnings we noticed during overload resolution. */
5882 if (cand->warnings && (complain & tf_warning))
5884 struct candidate_warning *w;
5885 for (w = cand->warnings; w; w = w->next)
5886 joust (cand, w->loser, 1, complain);
5889 /* Check for comparison of different enum types. */
5890 switch (code)
5892 case GT_EXPR:
5893 case LT_EXPR:
5894 case GE_EXPR:
5895 case LE_EXPR:
5896 case EQ_EXPR:
5897 case NE_EXPR:
5898 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
5899 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
5900 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5901 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
5902 && (complain & tf_warning))
5904 warning (OPT_Wenum_compare,
5905 "comparison between %q#T and %q#T",
5906 TREE_TYPE (arg1), TREE_TYPE (arg2));
5908 break;
5909 default:
5910 break;
5913 /* We need to strip any leading REF_BIND so that bitfields
5914 don't cause errors. This should not remove any important
5915 conversions, because builtins don't apply to class
5916 objects directly. */
5917 conv = cand->convs[0];
5918 if (conv->kind == ck_ref_bind)
5919 conv = next_conversion (conv);
5920 arg1 = convert_like (conv, arg1, complain);
5922 if (arg2)
5924 conv = cand->convs[1];
5925 if (conv->kind == ck_ref_bind)
5926 conv = next_conversion (conv);
5927 else
5928 arg2 = decay_conversion (arg2, complain);
5930 /* We need to call warn_logical_operator before
5931 converting arg2 to a boolean_type, but after
5932 decaying an enumerator to its value. */
5933 if (complain & tf_warning)
5934 warn_logical_operator (loc, code, boolean_type_node,
5935 code_orig_arg1, arg1,
5936 code_orig_arg2, arg2);
5938 arg2 = convert_like (conv, arg2, complain);
5940 if (arg3)
5942 conv = cand->convs[2];
5943 if (conv->kind == ck_ref_bind)
5944 conv = next_conversion (conv);
5945 arg3 = convert_like (conv, arg3, complain);
5951 user_defined_result_ready:
5953 /* Free all the conversions we allocated. */
5954 obstack_free (&conversion_obstack, p);
5956 if (result || result_valid_p)
5957 return result;
5959 builtin:
5960 switch (code)
5962 case MODIFY_EXPR:
5963 return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
5965 case INDIRECT_REF:
5966 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
5968 case TRUTH_ANDIF_EXPR:
5969 case TRUTH_ORIF_EXPR:
5970 case TRUTH_AND_EXPR:
5971 case TRUTH_OR_EXPR:
5972 if (complain & tf_warning)
5973 warn_logical_operator (loc, code, boolean_type_node,
5974 code_orig_arg1, arg1,
5975 code_orig_arg2, arg2);
5976 /* Fall through. */
5977 case GT_EXPR:
5978 case LT_EXPR:
5979 case GE_EXPR:
5980 case LE_EXPR:
5981 case EQ_EXPR:
5982 case NE_EXPR:
5983 if ((complain & tf_warning)
5984 && ((code_orig_arg1 == BOOLEAN_TYPE)
5985 ^ (code_orig_arg2 == BOOLEAN_TYPE)))
5986 maybe_warn_bool_compare (loc, code, arg1, arg2);
5987 if (complain & tf_warning && warn_tautological_compare)
5988 warn_tautological_cmp (loc, code, arg1, arg2);
5989 /* Fall through. */
5990 case PLUS_EXPR:
5991 case MINUS_EXPR:
5992 case MULT_EXPR:
5993 case TRUNC_DIV_EXPR:
5994 case MAX_EXPR:
5995 case MIN_EXPR:
5996 case LSHIFT_EXPR:
5997 case RSHIFT_EXPR:
5998 case TRUNC_MOD_EXPR:
5999 case BIT_AND_EXPR:
6000 case BIT_IOR_EXPR:
6001 case BIT_XOR_EXPR:
6002 return cp_build_binary_op (loc, code, arg1, arg2, complain);
6004 case UNARY_PLUS_EXPR:
6005 case NEGATE_EXPR:
6006 case BIT_NOT_EXPR:
6007 case TRUTH_NOT_EXPR:
6008 case PREINCREMENT_EXPR:
6009 case POSTINCREMENT_EXPR:
6010 case PREDECREMENT_EXPR:
6011 case POSTDECREMENT_EXPR:
6012 case REALPART_EXPR:
6013 case IMAGPART_EXPR:
6014 case ABS_EXPR:
6015 return cp_build_unary_op (code, arg1, candidates != 0, complain);
6017 case ARRAY_REF:
6018 return cp_build_array_ref (input_location, arg1, arg2, complain);
6020 case MEMBER_REF:
6021 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_ARROW_STAR,
6022 complain),
6023 arg2, complain);
6025 /* The caller will deal with these. */
6026 case ADDR_EXPR:
6027 case COMPONENT_REF:
6028 case COMPOUND_EXPR:
6029 return NULL_TREE;
6031 default:
6032 gcc_unreachable ();
6034 return NULL_TREE;
6037 /* Wrapper for above. */
6039 tree
6040 build_new_op (location_t loc, enum tree_code code, int flags,
6041 tree arg1, tree arg2, tree arg3,
6042 tree *overload, tsubst_flags_t complain)
6044 tree ret;
6045 bool subtime = timevar_cond_start (TV_OVERLOAD);
6046 ret = build_new_op_1 (loc, code, flags, arg1, arg2, arg3,
6047 overload, complain);
6048 timevar_cond_stop (TV_OVERLOAD, subtime);
6049 return ret;
6052 /* CALL was returned by some call-building function; extract the actual
6053 CALL_EXPR from any bits that have been tacked on, e.g. by
6054 convert_from_reference. */
6056 tree
6057 extract_call_expr (tree call)
6059 while (TREE_CODE (call) == COMPOUND_EXPR)
6060 call = TREE_OPERAND (call, 1);
6061 if (REFERENCE_REF_P (call))
6062 call = TREE_OPERAND (call, 0);
6063 if (TREE_CODE (call) == TARGET_EXPR)
6064 call = TARGET_EXPR_INITIAL (call);
6065 gcc_assert (TREE_CODE (call) == CALL_EXPR
6066 || TREE_CODE (call) == AGGR_INIT_EXPR
6067 || call == error_mark_node);
6068 return call;
6071 /* Returns true if FN has two parameters, of which the second has type
6072 size_t. */
6074 static bool
6075 second_parm_is_size_t (tree fn)
6077 tree t = FUNCTION_ARG_CHAIN (fn);
6078 if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
6079 return false;
6080 t = TREE_CHAIN (t);
6081 if (t == void_list_node)
6082 return true;
6083 if (aligned_new_threshold && t
6084 && same_type_p (TREE_VALUE (t), align_type_node)
6085 && TREE_CHAIN (t) == void_list_node)
6086 return true;
6087 return false;
6090 /* True if T, an allocation function, has std::align_val_t as its second
6091 argument. */
6093 bool
6094 aligned_allocation_fn_p (tree t)
6096 if (!aligned_new_threshold)
6097 return false;
6099 tree a = FUNCTION_ARG_CHAIN (t);
6100 return (a && same_type_p (TREE_VALUE (a), align_type_node));
6103 /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
6104 function (3.7.4.2 [basic.stc.dynamic.deallocation]) with a parameter of
6105 std::align_val_t. */
6107 static bool
6108 aligned_deallocation_fn_p (tree t)
6110 if (!aligned_new_threshold)
6111 return false;
6113 /* A template instance is never a usual deallocation function,
6114 regardless of its signature. */
6115 if (TREE_CODE (t) == TEMPLATE_DECL
6116 || primary_template_specialization_p (t))
6117 return false;
6119 tree a = FUNCTION_ARG_CHAIN (t);
6120 if (same_type_p (TREE_VALUE (a), align_type_node)
6121 && TREE_CHAIN (a) == void_list_node)
6122 return true;
6123 if (!same_type_p (TREE_VALUE (a), size_type_node))
6124 return false;
6125 a = TREE_CHAIN (a);
6126 if (a && same_type_p (TREE_VALUE (a), align_type_node)
6127 && TREE_CHAIN (a) == void_list_node)
6128 return true;
6129 return false;
6132 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
6133 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
6135 bool
6136 usual_deallocation_fn_p (tree t)
6138 /* A template instance is never a usual deallocation function,
6139 regardless of its signature. */
6140 if (TREE_CODE (t) == TEMPLATE_DECL
6141 || primary_template_specialization_p (t))
6142 return false;
6144 /* If a class T has a member deallocation function named operator delete
6145 with exactly one parameter, then that function is a usual
6146 (non-placement) deallocation function. If class T does not declare
6147 such an operator delete but does declare a member deallocation
6148 function named operator delete with exactly two parameters, the second
6149 of which has type std::size_t (18.2), then this function is a usual
6150 deallocation function. */
6151 bool global = DECL_NAMESPACE_SCOPE_P (t);
6152 tree chain = FUNCTION_ARG_CHAIN (t);
6153 if (!chain)
6154 return false;
6155 if (chain == void_list_node
6156 || ((!global || flag_sized_deallocation)
6157 && second_parm_is_size_t (t)))
6158 return true;
6159 if (aligned_deallocation_fn_p (t))
6160 return true;
6161 return false;
6164 /* Build a call to operator delete. This has to be handled very specially,
6165 because the restrictions on what signatures match are different from all
6166 other call instances. For a normal delete, only a delete taking (void *)
6167 or (void *, size_t) is accepted. For a placement delete, only an exact
6168 match with the placement new is accepted.
6170 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
6171 ADDR is the pointer to be deleted.
6172 SIZE is the size of the memory block to be deleted.
6173 GLOBAL_P is true if the delete-expression should not consider
6174 class-specific delete operators.
6175 PLACEMENT is the corresponding placement new call, or NULL_TREE.
6177 If this call to "operator delete" is being generated as part to
6178 deallocate memory allocated via a new-expression (as per [expr.new]
6179 which requires that if the initialization throws an exception then
6180 we call a deallocation function), then ALLOC_FN is the allocation
6181 function. */
6183 tree
6184 build_op_delete_call (enum tree_code code, tree addr, tree size,
6185 bool global_p, tree placement,
6186 tree alloc_fn, tsubst_flags_t complain)
6188 tree fn = NULL_TREE;
6189 tree fns, fnname, type, t;
6191 if (addr == error_mark_node)
6192 return error_mark_node;
6194 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
6196 fnname = ovl_op_identifier (false, code);
6198 if (CLASS_TYPE_P (type)
6199 && COMPLETE_TYPE_P (complete_type (type))
6200 && !global_p)
6201 /* In [class.free]
6203 If the result of the lookup is ambiguous or inaccessible, or if
6204 the lookup selects a placement deallocation function, the
6205 program is ill-formed.
6207 Therefore, we ask lookup_fnfields to complain about ambiguity. */
6209 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
6210 if (fns == error_mark_node)
6211 return error_mark_node;
6213 else
6214 fns = NULL_TREE;
6216 if (fns == NULL_TREE)
6217 fns = lookup_name_nonclass (fnname);
6219 /* Strip const and volatile from addr. */
6220 addr = cp_convert (ptr_type_node, addr, complain);
6222 if (placement)
6224 /* "A declaration of a placement deallocation function matches the
6225 declaration of a placement allocation function if it has the same
6226 number of parameters and, after parameter transformations (8.3.5),
6227 all parameter types except the first are identical."
6229 So we build up the function type we want and ask instantiate_type
6230 to get it for us. */
6231 t = FUNCTION_ARG_CHAIN (alloc_fn);
6232 t = tree_cons (NULL_TREE, ptr_type_node, t);
6233 t = build_function_type (void_type_node, t);
6235 fn = instantiate_type (t, fns, tf_none);
6236 if (fn == error_mark_node)
6237 return NULL_TREE;
6239 fn = MAYBE_BASELINK_FUNCTIONS (fn);
6241 /* "If the lookup finds the two-parameter form of a usual deallocation
6242 function (3.7.4.2) and that function, considered as a placement
6243 deallocation function, would have been selected as a match for the
6244 allocation function, the program is ill-formed." */
6245 if (second_parm_is_size_t (fn))
6247 const char *const msg1
6248 = G_("exception cleanup for this placement new selects "
6249 "non-placement operator delete");
6250 const char *const msg2
6251 = G_("%qD is a usual (non-placement) deallocation "
6252 "function in C++14 (or with -fsized-deallocation)");
6254 /* But if the class has an operator delete (void *), then that is
6255 the usual deallocation function, so we shouldn't complain
6256 about using the operator delete (void *, size_t). */
6257 if (DECL_CLASS_SCOPE_P (fn))
6258 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns));
6259 iter; ++iter)
6261 tree elt = *iter;
6262 if (usual_deallocation_fn_p (elt)
6263 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
6264 goto ok;
6266 /* Before C++14 a two-parameter global deallocation function is
6267 always a placement deallocation function, but warn if
6268 -Wc++14-compat. */
6269 else if (!flag_sized_deallocation)
6271 if ((complain & tf_warning)
6272 && warning (OPT_Wc__14_compat, msg1))
6273 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6274 goto ok;
6277 if (complain & tf_warning_or_error)
6279 if (permerror (input_location, msg1))
6281 /* Only mention C++14 for namespace-scope delete. */
6282 if (DECL_NAMESPACE_SCOPE_P (fn))
6283 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6284 else
6285 inform (DECL_SOURCE_LOCATION (fn),
6286 "%qD is a usual (non-placement) deallocation "
6287 "function", fn);
6290 else
6291 return error_mark_node;
6292 ok:;
6295 else
6296 /* "Any non-placement deallocation function matches a non-placement
6297 allocation function. If the lookup finds a single matching
6298 deallocation function, that function will be called; otherwise, no
6299 deallocation function will be called." */
6300 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
6302 tree elt = *iter;
6303 if (usual_deallocation_fn_p (elt))
6305 if (!fn)
6307 fn = elt;
6308 continue;
6311 /* -- If the type has new-extended alignment, a function with a
6312 parameter of type std::align_val_t is preferred; otherwise a
6313 function without such a parameter is preferred. If exactly one
6314 preferred function is found, that function is selected and the
6315 selection process terminates. If more than one preferred
6316 function is found, all non-preferred functions are eliminated
6317 from further consideration. */
6318 if (aligned_new_threshold)
6320 bool want_align = type_has_new_extended_alignment (type);
6321 bool fn_align = aligned_deallocation_fn_p (fn);
6322 bool elt_align = aligned_deallocation_fn_p (elt);
6324 if (elt_align != fn_align)
6326 if (want_align == elt_align)
6327 fn = elt;
6328 continue;
6332 /* -- If the deallocation functions have class scope, the one
6333 without a parameter of type std::size_t is selected. */
6334 bool want_size;
6335 if (DECL_CLASS_SCOPE_P (fn))
6336 want_size = false;
6338 /* -- If the type is complete and if, for the second alternative
6339 (delete array) only, the operand is a pointer to a class type
6340 with a non-trivial destructor or a (possibly multi-dimensional)
6341 array thereof, the function with a parameter of type std::size_t
6342 is selected.
6344 -- Otherwise, it is unspecified whether a deallocation function
6345 with a parameter of type std::size_t is selected. */
6346 else
6348 want_size = COMPLETE_TYPE_P (type);
6349 if (code == VEC_DELETE_EXPR
6350 && !TYPE_VEC_NEW_USES_COOKIE (type))
6351 /* We need a cookie to determine the array size. */
6352 want_size = false;
6354 bool fn_size = second_parm_is_size_t (fn);
6355 bool elt_size = second_parm_is_size_t (elt);
6356 gcc_assert (fn_size != elt_size);
6357 if (want_size == elt_size)
6358 fn = elt;
6362 /* If we have a matching function, call it. */
6363 if (fn)
6365 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6367 /* If the FN is a member function, make sure that it is
6368 accessible. */
6369 if (BASELINK_P (fns))
6370 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
6371 complain);
6373 /* Core issue 901: It's ok to new a type with deleted delete. */
6374 if (DECL_DELETED_FN (fn) && alloc_fn)
6375 return NULL_TREE;
6377 if (placement)
6379 /* The placement args might not be suitable for overload
6380 resolution at this point, so build the call directly. */
6381 int nargs = call_expr_nargs (placement);
6382 tree *argarray = XALLOCAVEC (tree, nargs);
6383 int i;
6384 argarray[0] = addr;
6385 for (i = 1; i < nargs; i++)
6386 argarray[i] = CALL_EXPR_ARG (placement, i);
6387 if (!mark_used (fn, complain) && !(complain & tf_error))
6388 return error_mark_node;
6389 return build_cxx_call (fn, nargs, argarray, complain);
6391 else
6393 tree ret;
6394 vec<tree, va_gc> *args = make_tree_vector ();
6395 args->quick_push (addr);
6396 if (second_parm_is_size_t (fn))
6397 args->quick_push (size);
6398 if (aligned_deallocation_fn_p (fn))
6400 tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
6401 args->quick_push (al);
6403 ret = cp_build_function_call_vec (fn, &args, complain);
6404 release_tree_vector (args);
6405 return ret;
6409 /* [expr.new]
6411 If no unambiguous matching deallocation function can be found,
6412 propagating the exception does not cause the object's memory to
6413 be freed. */
6414 if (alloc_fn)
6416 if ((complain & tf_warning)
6417 && !placement)
6418 warning (0, "no corresponding deallocation function for %qD",
6419 alloc_fn);
6420 return NULL_TREE;
6423 if (complain & tf_error)
6424 error ("no suitable %<operator %s%> for %qT",
6425 OVL_OP_INFO (false, code)->name, type);
6426 return error_mark_node;
6429 /* If the current scope isn't allowed to access DECL along
6430 BASETYPE_PATH, give an error. The most derived class in
6431 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
6432 the declaration to use in the error diagnostic. */
6434 bool
6435 enforce_access (tree basetype_path, tree decl, tree diag_decl,
6436 tsubst_flags_t complain, access_failure_info *afi)
6438 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
6440 if (flag_new_inheriting_ctors
6441 && DECL_INHERITED_CTOR (decl))
6443 /* 7.3.3/18: The additional constructors are accessible if they would be
6444 accessible when used to construct an object of the corresponding base
6445 class. */
6446 decl = strip_inheriting_ctors (decl);
6447 basetype_path = lookup_base (basetype_path, DECL_CONTEXT (decl),
6448 ba_any, NULL, complain);
6451 if (!accessible_p (basetype_path, decl, true))
6453 if (complain & tf_error)
6455 if (flag_new_inheriting_ctors)
6456 diag_decl = strip_inheriting_ctors (diag_decl);
6457 if (TREE_PRIVATE (decl))
6459 error ("%q#D is private within this context", diag_decl);
6460 inform (DECL_SOURCE_LOCATION (diag_decl),
6461 "declared private here");
6462 if (afi)
6463 afi->record_access_failure (basetype_path, diag_decl);
6465 else if (TREE_PROTECTED (decl))
6467 error ("%q#D is protected within this context", diag_decl);
6468 inform (DECL_SOURCE_LOCATION (diag_decl),
6469 "declared protected here");
6470 if (afi)
6471 afi->record_access_failure (basetype_path, diag_decl);
6473 else
6475 error ("%q#D is inaccessible within this context", diag_decl);
6476 inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
6477 if (afi)
6478 afi->record_access_failure (basetype_path, diag_decl);
6481 return false;
6484 return true;
6487 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
6488 bitwise or of LOOKUP_* values. If any errors are warnings are
6489 generated, set *DIAGNOSTIC_FN to "error" or "warning",
6490 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
6491 to NULL. */
6493 static tree
6494 build_temp (tree expr, tree type, int flags,
6495 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
6497 int savew, savee;
6498 vec<tree, va_gc> *args;
6500 *diagnostic_kind = DK_UNSPECIFIED;
6502 /* If the source is a packed field, calling the copy constructor will require
6503 binding the field to the reference parameter to the copy constructor, and
6504 we'll end up with an infinite loop. If we can use a bitwise copy, then
6505 do that now. */
6506 if ((lvalue_kind (expr) & clk_packed)
6507 && CLASS_TYPE_P (TREE_TYPE (expr))
6508 && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
6509 return get_target_expr_sfinae (expr, complain);
6511 savew = warningcount + werrorcount, savee = errorcount;
6512 args = make_tree_vector_single (expr);
6513 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6514 &args, type, flags, complain);
6515 release_tree_vector (args);
6516 if (warningcount + werrorcount > savew)
6517 *diagnostic_kind = DK_WARNING;
6518 else if (errorcount > savee)
6519 *diagnostic_kind = DK_ERROR;
6520 return expr;
6523 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
6524 EXPR is implicitly converted to type TOTYPE.
6525 FN and ARGNUM are used for diagnostics. */
6527 static void
6528 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
6530 /* Issue warnings about peculiar, but valid, uses of NULL. */
6531 if (expr == null_node && TREE_CODE (totype) != BOOLEAN_TYPE
6532 && ARITHMETIC_TYPE_P (totype))
6534 source_location loc =
6535 expansion_point_location_if_in_system_header (input_location);
6537 if (fn)
6538 warning_at (loc, OPT_Wconversion_null,
6539 "passing NULL to non-pointer argument %P of %qD",
6540 argnum, fn);
6541 else
6542 warning_at (loc, OPT_Wconversion_null,
6543 "converting to non-pointer type %qT from NULL", totype);
6546 /* Issue warnings if "false" is converted to a NULL pointer */
6547 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
6548 && TYPE_PTR_P (totype))
6550 if (fn)
6551 warning_at (input_location, OPT_Wconversion_null,
6552 "converting %<false%> to pointer type for argument %P "
6553 "of %qD", argnum, fn);
6554 else
6555 warning_at (input_location, OPT_Wconversion_null,
6556 "converting %<false%> to pointer type %qT", totype);
6560 /* We gave a diagnostic during a conversion. If this was in the second
6561 standard conversion sequence of a user-defined conversion sequence, say
6562 which user-defined conversion. */
6564 static void
6565 maybe_print_user_conv_context (conversion *convs)
6567 if (convs->user_conv_p)
6568 for (conversion *t = convs; t; t = next_conversion (t))
6569 if (t->kind == ck_user)
6571 print_z_candidate (0, " after user-defined conversion:",
6572 t->cand);
6573 break;
6577 /* Locate the parameter with the given index within FNDECL.
6578 ARGNUM is zero based, -1 indicates the `this' argument of a method.
6579 Return the location of the FNDECL itself if there are problems. */
6581 static location_t
6582 get_fndecl_argument_location (tree fndecl, int argnum)
6584 int i;
6585 tree param;
6587 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6588 for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
6589 i < argnum && param;
6590 i++, param = TREE_CHAIN (param))
6593 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6594 return the location of FNDECL. */
6595 if (param == NULL)
6596 return DECL_SOURCE_LOCATION (fndecl);
6598 return DECL_SOURCE_LOCATION (param);
6601 /* Perform the conversions in CONVS on the expression EXPR. FN and
6602 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
6603 indicates the `this' argument of a method. INNER is nonzero when
6604 being called to continue a conversion chain. It is negative when a
6605 reference binding will be applied, positive otherwise. If
6606 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
6607 conversions will be emitted if appropriate. If C_CAST_P is true,
6608 this conversion is coming from a C-style cast; in that case,
6609 conversions to inaccessible bases are permitted. */
6611 static tree
6612 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
6613 bool issue_conversion_warnings,
6614 bool c_cast_p, tsubst_flags_t complain)
6616 tree totype = convs->type;
6617 diagnostic_t diag_kind;
6618 int flags;
6619 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6621 if (convs->bad_p && !(complain & tf_error))
6622 return error_mark_node;
6624 if (convs->bad_p
6625 && convs->kind != ck_user
6626 && convs->kind != ck_list
6627 && convs->kind != ck_ambig
6628 && (convs->kind != ck_ref_bind
6629 || (convs->user_conv_p && next_conversion (convs)->bad_p))
6630 && (convs->kind != ck_rvalue
6631 || SCALAR_TYPE_P (totype))
6632 && convs->kind != ck_base)
6634 bool complained = false;
6635 conversion *t = convs;
6637 /* Give a helpful error if this is bad because of excess braces. */
6638 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6639 && SCALAR_TYPE_P (totype)
6640 && CONSTRUCTOR_NELTS (expr) > 0
6641 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
6643 complained = permerror (loc, "too many braces around initializer "
6644 "for %qT", totype);
6645 while (BRACE_ENCLOSED_INITIALIZER_P (expr)
6646 && CONSTRUCTOR_NELTS (expr) == 1)
6647 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6650 /* Give a helpful error if this is bad because a conversion to bool
6651 from std::nullptr_t requires direct-initialization. */
6652 if (NULLPTR_TYPE_P (TREE_TYPE (expr))
6653 && TREE_CODE (totype) == BOOLEAN_TYPE)
6654 complained = permerror (loc, "converting to %qH from %qI requires "
6655 "direct-initialization",
6656 totype, TREE_TYPE (expr));
6658 for (; t ; t = next_conversion (t))
6660 if (t->kind == ck_user && t->cand->reason)
6662 complained = permerror (loc, "invalid user-defined conversion "
6663 "from %qH to %qI", TREE_TYPE (expr),
6664 totype);
6665 if (complained)
6666 print_z_candidate (loc, "candidate is:", t->cand);
6667 expr = convert_like_real (t, expr, fn, argnum,
6668 /*issue_conversion_warnings=*/false,
6669 /*c_cast_p=*/false,
6670 complain);
6671 if (convs->kind == ck_ref_bind)
6672 expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
6673 LOOKUP_NORMAL, NULL_TREE,
6674 complain);
6675 else
6676 expr = cp_convert (totype, expr, complain);
6677 if (complained && fn)
6678 inform (DECL_SOURCE_LOCATION (fn),
6679 " initializing argument %P of %qD", argnum, fn);
6680 return expr;
6682 else if (t->kind == ck_user || !t->bad_p)
6684 expr = convert_like_real (t, expr, fn, argnum,
6685 /*issue_conversion_warnings=*/false,
6686 /*c_cast_p=*/false,
6687 complain);
6688 break;
6690 else if (t->kind == ck_ambig)
6691 return convert_like_real (t, expr, fn, argnum,
6692 /*issue_conversion_warnings=*/false,
6693 /*c_cast_p=*/false,
6694 complain);
6695 else if (t->kind == ck_identity)
6696 break;
6698 if (!complained)
6699 complained = permerror (loc, "invalid conversion from %qH to %qI",
6700 TREE_TYPE (expr), totype);
6701 if (complained && fn)
6702 inform (get_fndecl_argument_location (fn, argnum),
6703 " initializing argument %P of %qD", argnum, fn);
6705 return cp_convert (totype, expr, complain);
6708 if (issue_conversion_warnings && (complain & tf_warning))
6709 conversion_null_warnings (totype, expr, fn, argnum);
6711 switch (convs->kind)
6713 case ck_user:
6715 struct z_candidate *cand = convs->cand;
6717 if (cand == NULL)
6718 /* We chose the surrogate function from add_conv_candidate, now we
6719 actually need to build the conversion. */
6720 cand = build_user_type_conversion_1 (totype, expr,
6721 LOOKUP_NO_CONVERSION, complain);
6723 tree convfn = cand->fn;
6725 /* When converting from an init list we consider explicit
6726 constructors, but actually trying to call one is an error. */
6727 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
6728 && BRACE_ENCLOSED_INITIALIZER_P (expr)
6729 /* Unless this is for direct-list-initialization. */
6730 && !CONSTRUCTOR_IS_DIRECT_INIT (expr)
6731 /* And in C++98 a default constructor can't be explicit. */
6732 && cxx_dialect >= cxx11)
6734 if (!(complain & tf_error))
6735 return error_mark_node;
6736 location_t loc = location_of (expr);
6737 if (CONSTRUCTOR_NELTS (expr) == 0
6738 && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
6740 if (pedwarn (loc, 0, "converting to %qT from initializer list "
6741 "would use explicit constructor %qD",
6742 totype, convfn))
6743 inform (loc, "in C++11 and above a default constructor "
6744 "can be explicit");
6746 else
6747 error ("converting to %qT from initializer list would use "
6748 "explicit constructor %qD", totype, convfn);
6751 /* If we're initializing from {}, it's value-initialization. */
6752 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6753 && CONSTRUCTOR_NELTS (expr) == 0
6754 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
6756 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
6757 expr = build_value_init (totype, complain);
6758 expr = get_target_expr_sfinae (expr, complain);
6759 if (expr != error_mark_node)
6761 TARGET_EXPR_LIST_INIT_P (expr) = true;
6762 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
6764 return expr;
6767 expr = mark_rvalue_use (expr);
6769 /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
6770 any more UDCs. */
6771 expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
6772 complain);
6774 /* If this is a constructor or a function returning an aggr type,
6775 we need to build up a TARGET_EXPR. */
6776 if (DECL_CONSTRUCTOR_P (convfn))
6778 expr = build_cplus_new (totype, expr, complain);
6780 /* Remember that this was list-initialization. */
6781 if (convs->check_narrowing && expr != error_mark_node)
6782 TARGET_EXPR_LIST_INIT_P (expr) = true;
6785 return expr;
6787 case ck_identity:
6788 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
6790 int nelts = CONSTRUCTOR_NELTS (expr);
6791 if (nelts == 0)
6792 expr = build_value_init (totype, complain);
6793 else if (nelts == 1)
6794 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6795 else
6796 gcc_unreachable ();
6798 expr = mark_rvalue_use (expr);
6800 if (type_unknown_p (expr))
6801 expr = instantiate_type (totype, expr, complain);
6802 return expr;
6803 case ck_ambig:
6804 /* We leave bad_p off ck_ambig because overload resolution considers
6805 it valid, it just fails when we try to perform it. So we need to
6806 check complain here, too. */
6807 if (complain & tf_error)
6809 /* Call build_user_type_conversion again for the error. */
6810 build_user_type_conversion (totype, convs->u.expr, LOOKUP_IMPLICIT,
6811 complain);
6812 if (fn)
6813 inform (DECL_SOURCE_LOCATION (fn),
6814 " initializing argument %P of %qD", argnum, fn);
6816 return error_mark_node;
6818 case ck_list:
6820 /* Conversion to std::initializer_list<T>. */
6821 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
6822 tree new_ctor = build_constructor (init_list_type_node, NULL);
6823 unsigned len = CONSTRUCTOR_NELTS (expr);
6824 tree array, val, field;
6825 vec<constructor_elt, va_gc> *vec = NULL;
6826 unsigned ix;
6828 /* Convert all the elements. */
6829 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
6831 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
6832 false, false, complain);
6833 if (sub == error_mark_node)
6834 return sub;
6835 if (!BRACE_ENCLOSED_INITIALIZER_P (val)
6836 && !check_narrowing (TREE_TYPE (sub), val, complain))
6837 return error_mark_node;
6838 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
6839 if (!TREE_CONSTANT (sub))
6840 TREE_CONSTANT (new_ctor) = false;
6842 /* Build up the array. */
6843 elttype = cp_build_qualified_type
6844 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
6845 array = build_array_of_n_type (elttype, len);
6846 array = finish_compound_literal (array, new_ctor, complain);
6847 /* Take the address explicitly rather than via decay_conversion
6848 to avoid the error about taking the address of a temporary. */
6849 array = cp_build_addr_expr (array, complain);
6850 array = cp_convert (build_pointer_type (elttype), array, complain);
6851 if (array == error_mark_node)
6852 return error_mark_node;
6854 /* Build up the initializer_list object. */
6855 totype = complete_type (totype);
6856 field = next_initializable_field (TYPE_FIELDS (totype));
6857 CONSTRUCTOR_APPEND_ELT (vec, field, array);
6858 field = next_initializable_field (DECL_CHAIN (field));
6859 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
6860 new_ctor = build_constructor (totype, vec);
6861 return get_target_expr_sfinae (new_ctor, complain);
6864 case ck_aggr:
6865 if (TREE_CODE (totype) == COMPLEX_TYPE)
6867 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
6868 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
6869 real = perform_implicit_conversion (TREE_TYPE (totype),
6870 real, complain);
6871 imag = perform_implicit_conversion (TREE_TYPE (totype),
6872 imag, complain);
6873 expr = build2 (COMPLEX_EXPR, totype, real, imag);
6874 return expr;
6876 expr = reshape_init (totype, expr, complain);
6877 expr = get_target_expr_sfinae (digest_init (totype, expr, complain),
6878 complain);
6879 if (expr != error_mark_node)
6880 TARGET_EXPR_LIST_INIT_P (expr) = true;
6881 return expr;
6883 default:
6884 break;
6887 expr = convert_like_real (next_conversion (convs), expr, fn, argnum,
6888 convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
6889 c_cast_p,
6890 complain);
6891 if (expr == error_mark_node)
6892 return error_mark_node;
6894 switch (convs->kind)
6896 case ck_rvalue:
6897 expr = decay_conversion (expr, complain);
6898 if (expr == error_mark_node)
6900 if (complain & tf_error)
6902 maybe_print_user_conv_context (convs);
6903 if (fn)
6904 inform (DECL_SOURCE_LOCATION (fn),
6905 " initializing argument %P of %qD", argnum, fn);
6907 return error_mark_node;
6910 if (! MAYBE_CLASS_TYPE_P (totype))
6911 return expr;
6913 /* Don't introduce copies when passing arguments along to the inherited
6914 constructor. */
6915 if (current_function_decl
6916 && flag_new_inheriting_ctors
6917 && DECL_INHERITED_CTOR (current_function_decl))
6918 return expr;
6920 /* Fall through. */
6921 case ck_base:
6922 if (convs->kind == ck_base && !convs->need_temporary_p)
6924 /* We are going to bind a reference directly to a base-class
6925 subobject of EXPR. */
6926 /* Build an expression for `*((base*) &expr)'. */
6927 expr = convert_to_base (expr, totype,
6928 !c_cast_p, /*nonnull=*/true, complain);
6929 return expr;
6932 /* Copy-initialization where the cv-unqualified version of the source
6933 type is the same class as, or a derived class of, the class of the
6934 destination [is treated as direct-initialization]. [dcl.init] */
6935 flags = LOOKUP_NORMAL;
6936 if (convs->user_conv_p)
6937 /* This conversion is being done in the context of a user-defined
6938 conversion (i.e. the second step of copy-initialization), so
6939 don't allow any more. */
6940 flags |= LOOKUP_NO_CONVERSION;
6941 else
6942 flags |= LOOKUP_ONLYCONVERTING;
6943 if (convs->rvaluedness_matches_p)
6944 /* standard_conversion got LOOKUP_PREFER_RVALUE. */
6945 flags |= LOOKUP_PREFER_RVALUE;
6946 if (TREE_CODE (expr) == TARGET_EXPR
6947 && TARGET_EXPR_LIST_INIT_P (expr))
6948 /* Copy-list-initialization doesn't actually involve a copy. */
6949 return expr;
6950 expr = build_temp (expr, totype, flags, &diag_kind, complain);
6951 if (diag_kind && complain)
6953 maybe_print_user_conv_context (convs);
6954 if (fn)
6955 inform (DECL_SOURCE_LOCATION (fn),
6956 " initializing argument %P of %qD", argnum, fn);
6959 return build_cplus_new (totype, expr, complain);
6961 case ck_ref_bind:
6963 tree ref_type = totype;
6965 if (convs->bad_p && !next_conversion (convs)->bad_p)
6967 tree extype = TREE_TYPE (expr);
6968 if (TYPE_REF_IS_RVALUE (ref_type)
6969 && lvalue_p (expr))
6970 error_at (loc, "cannot bind rvalue reference of type %qH to "
6971 "lvalue of type %qI", totype, extype);
6972 else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
6973 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
6974 error_at (loc, "cannot bind non-const lvalue reference of "
6975 "type %qH to an rvalue of type %qI", totype, extype);
6976 else if (!reference_compatible_p (TREE_TYPE (totype), extype))
6977 error_at (loc, "binding reference of type %qH to %qI "
6978 "discards qualifiers", totype, extype);
6979 else
6980 gcc_unreachable ();
6981 maybe_print_user_conv_context (convs);
6982 if (fn)
6983 inform (DECL_SOURCE_LOCATION (fn),
6984 " initializing argument %P of %qD", argnum, fn);
6985 return error_mark_node;
6988 /* If necessary, create a temporary.
6990 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
6991 that need temporaries, even when their types are reference
6992 compatible with the type of reference being bound, so the
6993 upcoming call to cp_build_addr_expr doesn't fail. */
6994 if (convs->need_temporary_p
6995 || TREE_CODE (expr) == CONSTRUCTOR
6996 || TREE_CODE (expr) == VA_ARG_EXPR)
6998 /* Otherwise, a temporary of type "cv1 T1" is created and
6999 initialized from the initializer expression using the rules
7000 for a non-reference copy-initialization (8.5). */
7002 tree type = TREE_TYPE (ref_type);
7003 cp_lvalue_kind lvalue = lvalue_kind (expr);
7005 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7006 (type, next_conversion (convs)->type));
7007 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
7008 && !TYPE_REF_IS_RVALUE (ref_type))
7010 /* If the reference is volatile or non-const, we
7011 cannot create a temporary. */
7012 if (lvalue & clk_bitfield)
7013 error_at (loc, "cannot bind bitfield %qE to %qT",
7014 expr, ref_type);
7015 else if (lvalue & clk_packed)
7016 error_at (loc, "cannot bind packed field %qE to %qT",
7017 expr, ref_type);
7018 else
7019 error_at (loc, "cannot bind rvalue %qE to %qT",
7020 expr, ref_type);
7021 return error_mark_node;
7023 /* If the source is a packed field, and we must use a copy
7024 constructor, then building the target expr will require
7025 binding the field to the reference parameter to the
7026 copy constructor, and we'll end up with an infinite
7027 loop. If we can use a bitwise copy, then we'll be
7028 OK. */
7029 if ((lvalue & clk_packed)
7030 && CLASS_TYPE_P (type)
7031 && type_has_nontrivial_copy_init (type))
7033 error_at (loc, "cannot bind packed field %qE to %qT",
7034 expr, ref_type);
7035 return error_mark_node;
7037 if (lvalue & clk_bitfield)
7039 expr = convert_bitfield_to_declared_type (expr);
7040 expr = fold_convert (type, expr);
7042 expr = build_target_expr_with_type (expr, type, complain);
7045 /* Take the address of the thing to which we will bind the
7046 reference. */
7047 expr = cp_build_addr_expr (expr, complain);
7048 if (expr == error_mark_node)
7049 return error_mark_node;
7051 /* Convert it to a pointer to the type referred to by the
7052 reference. This will adjust the pointer if a derived to
7053 base conversion is being performed. */
7054 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
7055 expr, complain);
7056 /* Convert the pointer to the desired reference type. */
7057 return build_nop (ref_type, expr);
7060 case ck_lvalue:
7061 return decay_conversion (expr, complain);
7063 case ck_fnptr:
7064 /* ??? Should the address of a transaction-safe pointer point to the TM
7065 clone, and this conversion look up the primary function? */
7066 return build_nop (totype, expr);
7068 case ck_qual:
7069 /* Warn about deprecated conversion if appropriate. */
7070 string_conv_p (totype, expr, 1);
7071 break;
7073 case ck_ptr:
7074 if (convs->base_p)
7075 expr = convert_to_base (expr, totype, !c_cast_p,
7076 /*nonnull=*/false, complain);
7077 return build_nop (totype, expr);
7079 case ck_pmem:
7080 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
7081 c_cast_p, complain);
7083 default:
7084 break;
7087 if (convs->check_narrowing
7088 && !check_narrowing (totype, expr, complain))
7089 return error_mark_node;
7091 if (issue_conversion_warnings)
7092 expr = cp_convert_and_check (totype, expr, complain);
7093 else
7094 expr = cp_convert (totype, expr, complain);
7096 return expr;
7099 /* ARG is being passed to a varargs function. Perform any conversions
7100 required. Return the converted value. */
7102 tree
7103 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
7105 tree arg_type;
7106 location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
7108 /* [expr.call]
7110 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7111 standard conversions are performed. */
7112 arg = decay_conversion (arg, complain);
7113 arg_type = TREE_TYPE (arg);
7114 /* [expr.call]
7116 If the argument has integral or enumeration type that is subject
7117 to the integral promotions (_conv.prom_), or a floating point
7118 type that is subject to the floating point promotion
7119 (_conv.fpprom_), the value of the argument is converted to the
7120 promoted type before the call. */
7121 if (TREE_CODE (arg_type) == REAL_TYPE
7122 && (TYPE_PRECISION (arg_type)
7123 < TYPE_PRECISION (double_type_node))
7124 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
7126 if ((complain & tf_warning)
7127 && warn_double_promotion && !c_inhibit_evaluation_warnings)
7128 warning_at (loc, OPT_Wdouble_promotion,
7129 "implicit conversion from %qH to %qI when passing "
7130 "argument to function",
7131 arg_type, double_type_node);
7132 arg = convert_to_real_nofold (double_type_node, arg);
7134 else if (NULLPTR_TYPE_P (arg_type))
7135 arg = null_pointer_node;
7136 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
7138 if (SCOPED_ENUM_P (arg_type))
7140 tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
7141 complain);
7142 prom = cp_perform_integral_promotions (prom, complain);
7143 if (abi_version_crosses (6)
7144 && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
7145 && (complain & tf_warning))
7146 warning_at (loc, OPT_Wabi, "scoped enum %qT passed through ... as "
7147 "%qT before -fabi-version=6, %qT after", arg_type,
7148 TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
7149 if (!abi_version_at_least (6))
7150 arg = prom;
7152 else
7153 arg = cp_perform_integral_promotions (arg, complain);
7156 arg = require_complete_type_sfinae (arg, complain);
7157 arg_type = TREE_TYPE (arg);
7159 if (arg != error_mark_node
7160 /* In a template (or ill-formed code), we can have an incomplete type
7161 even after require_complete_type_sfinae, in which case we don't know
7162 whether it has trivial copy or not. */
7163 && COMPLETE_TYPE_P (arg_type)
7164 && !cp_unevaluated_operand)
7166 /* [expr.call] 5.2.2/7:
7167 Passing a potentially-evaluated argument of class type (Clause 9)
7168 with a non-trivial copy constructor or a non-trivial destructor
7169 with no corresponding parameter is conditionally-supported, with
7170 implementation-defined semantics.
7172 We support it as pass-by-invisible-reference, just like a normal
7173 value parameter.
7175 If the call appears in the context of a sizeof expression,
7176 it is not potentially-evaluated. */
7177 if (type_has_nontrivial_copy_init (arg_type)
7178 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
7180 arg = force_rvalue (arg, complain);
7181 if (complain & tf_warning)
7182 warning (OPT_Wconditionally_supported,
7183 "passing objects of non-trivially-copyable "
7184 "type %q#T through %<...%> is conditionally supported",
7185 arg_type);
7186 return cp_build_addr_expr (arg, complain);
7188 /* Build up a real lvalue-to-rvalue conversion in case the
7189 copy constructor is trivial but not callable. */
7190 else if (CLASS_TYPE_P (arg_type))
7191 force_rvalue (arg, complain);
7195 return arg;
7198 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
7200 tree
7201 build_x_va_arg (source_location loc, tree expr, tree type)
7203 if (processing_template_decl)
7205 tree r = build_min (VA_ARG_EXPR, type, expr);
7206 SET_EXPR_LOCATION (r, loc);
7207 return r;
7210 type = complete_type_or_else (type, NULL_TREE);
7212 if (expr == error_mark_node || !type)
7213 return error_mark_node;
7215 expr = mark_lvalue_use (expr);
7217 if (TREE_CODE (type) == REFERENCE_TYPE)
7219 error ("cannot receive reference type %qT through %<...%>", type);
7220 return error_mark_node;
7223 if (type_has_nontrivial_copy_init (type)
7224 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7226 /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
7227 it as pass by invisible reference. */
7228 warning_at (loc, OPT_Wconditionally_supported,
7229 "receiving objects of non-trivially-copyable type %q#T "
7230 "through %<...%> is conditionally-supported", type);
7232 tree ref = cp_build_reference_type (type, false);
7233 expr = build_va_arg (loc, expr, ref);
7234 return convert_from_reference (expr);
7237 tree ret = build_va_arg (loc, expr, type);
7238 if (CLASS_TYPE_P (type))
7239 /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
7240 know how to handle it. */
7241 ret = get_target_expr (ret);
7242 return ret;
7245 /* TYPE has been given to va_arg. Apply the default conversions which
7246 would have happened when passed via ellipsis. Return the promoted
7247 type, or the passed type if there is no change. */
7249 tree
7250 cxx_type_promotes_to (tree type)
7252 tree promote;
7254 /* Perform the array-to-pointer and function-to-pointer
7255 conversions. */
7256 type = type_decays_to (type);
7258 promote = type_promotes_to (type);
7259 if (same_type_p (type, promote))
7260 promote = type;
7262 return promote;
7265 /* ARG is a default argument expression being passed to a parameter of
7266 the indicated TYPE, which is a parameter to FN. PARMNUM is the
7267 zero-based argument number. Do any required conversions. Return
7268 the converted value. */
7270 static GTY(()) vec<tree, va_gc> *default_arg_context;
7271 void
7272 push_defarg_context (tree fn)
7273 { vec_safe_push (default_arg_context, fn); }
7275 void
7276 pop_defarg_context (void)
7277 { default_arg_context->pop (); }
7279 tree
7280 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
7281 tsubst_flags_t complain)
7283 int i;
7284 tree t;
7286 /* See through clones. */
7287 fn = DECL_ORIGIN (fn);
7288 /* And inheriting ctors. */
7289 if (flag_new_inheriting_ctors)
7290 fn = strip_inheriting_ctors (fn);
7292 /* Detect recursion. */
7293 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
7294 if (t == fn)
7296 if (complain & tf_error)
7297 error ("recursive evaluation of default argument for %q#D", fn);
7298 return error_mark_node;
7301 /* If the ARG is an unparsed default argument expression, the
7302 conversion cannot be performed. */
7303 if (TREE_CODE (arg) == DEFAULT_ARG)
7305 if (complain & tf_error)
7306 error ("call to %qD uses the default argument for parameter %P, which "
7307 "is not yet defined", fn, parmnum);
7308 return error_mark_node;
7311 push_defarg_context (fn);
7313 if (fn && DECL_TEMPLATE_INFO (fn))
7314 arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
7316 /* Due to:
7318 [dcl.fct.default]
7320 The names in the expression are bound, and the semantic
7321 constraints are checked, at the point where the default
7322 expressions appears.
7324 we must not perform access checks here. */
7325 push_deferring_access_checks (dk_no_check);
7326 /* We must make a copy of ARG, in case subsequent processing
7327 alters any part of it. */
7328 arg = break_out_target_exprs (arg);
7329 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
7330 ICR_DEFAULT_ARGUMENT, fn, parmnum,
7331 complain);
7332 arg = convert_for_arg_passing (type, arg, complain);
7333 pop_deferring_access_checks();
7335 pop_defarg_context ();
7337 return arg;
7340 /* Returns the type which will really be used for passing an argument of
7341 type TYPE. */
7343 tree
7344 type_passed_as (tree type)
7346 /* Pass classes with copy ctors by invisible reference. */
7347 if (TREE_ADDRESSABLE (type))
7349 type = build_reference_type (type);
7350 /* There are no other pointers to this temporary. */
7351 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
7353 else if (targetm.calls.promote_prototypes (type)
7354 && INTEGRAL_TYPE_P (type)
7355 && COMPLETE_TYPE_P (type)
7356 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
7357 type = integer_type_node;
7359 return type;
7362 /* Actually perform the appropriate conversion. */
7364 tree
7365 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
7367 tree bitfield_type;
7369 /* If VAL is a bitfield, then -- since it has already been converted
7370 to TYPE -- it cannot have a precision greater than TYPE.
7372 If it has a smaller precision, we must widen it here. For
7373 example, passing "int f:3;" to a function expecting an "int" will
7374 not result in any conversion before this point.
7376 If the precision is the same we must not risk widening. For
7377 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
7378 often have type "int", even though the C++ type for the field is
7379 "long long". If the value is being passed to a function
7380 expecting an "int", then no conversions will be required. But,
7381 if we call convert_bitfield_to_declared_type, the bitfield will
7382 be converted to "long long". */
7383 bitfield_type = is_bitfield_expr_with_lowered_type (val);
7384 if (bitfield_type
7385 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
7386 val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
7388 if (val == error_mark_node)
7390 /* Pass classes with copy ctors by invisible reference. */
7391 else if (TREE_ADDRESSABLE (type))
7392 val = build1 (ADDR_EXPR, build_reference_type (type), val);
7393 else if (targetm.calls.promote_prototypes (type)
7394 && INTEGRAL_TYPE_P (type)
7395 && COMPLETE_TYPE_P (type)
7396 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
7397 val = cp_perform_integral_promotions (val, complain);
7398 if (complain & tf_warning)
7400 if (warn_suggest_attribute_format)
7402 tree rhstype = TREE_TYPE (val);
7403 const enum tree_code coder = TREE_CODE (rhstype);
7404 const enum tree_code codel = TREE_CODE (type);
7405 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7406 && coder == codel
7407 && check_missing_format_attribute (type, rhstype))
7408 warning (OPT_Wsuggest_attribute_format,
7409 "argument of function call might be a candidate "
7410 "for a format attribute");
7412 maybe_warn_parm_abi (type, EXPR_LOC_OR_LOC (val, input_location));
7414 return val;
7417 /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
7418 which just decay_conversion or no conversions at all should be done.
7419 This is true for some builtins which don't act like normal functions.
7420 Return 2 if no conversions at all should be done, 1 if just
7421 decay_conversion. Return 3 for special treatment of the 3rd argument
7422 for __builtin_*_overflow_p. */
7425 magic_varargs_p (tree fn)
7427 if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
7428 switch (DECL_FUNCTION_CODE (fn))
7430 case BUILT_IN_CLASSIFY_TYPE:
7431 case BUILT_IN_CONSTANT_P:
7432 case BUILT_IN_NEXT_ARG:
7433 case BUILT_IN_VA_START:
7434 return 1;
7436 case BUILT_IN_ADD_OVERFLOW_P:
7437 case BUILT_IN_SUB_OVERFLOW_P:
7438 case BUILT_IN_MUL_OVERFLOW_P:
7439 return 3;
7441 default:;
7442 return lookup_attribute ("type generic",
7443 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
7446 return 0;
7449 /* Returns the decl of the dispatcher function if FN is a function version. */
7451 tree
7452 get_function_version_dispatcher (tree fn)
7454 tree dispatcher_decl = NULL;
7456 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7457 && DECL_FUNCTION_VERSIONED (fn));
7459 gcc_assert (targetm.get_function_versions_dispatcher);
7460 dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
7462 if (dispatcher_decl == NULL)
7464 error_at (input_location, "use of multiversioned function "
7465 "without a default");
7466 return NULL;
7469 retrofit_lang_decl (dispatcher_decl);
7470 gcc_assert (dispatcher_decl != NULL);
7471 return dispatcher_decl;
7474 /* fn is a function version dispatcher that is marked used. Mark all the
7475 semantically identical function versions it will dispatch as used. */
7477 void
7478 mark_versions_used (tree fn)
7480 struct cgraph_node *node;
7481 struct cgraph_function_version_info *node_v;
7482 struct cgraph_function_version_info *it_v;
7484 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7486 node = cgraph_node::get (fn);
7487 if (node == NULL)
7488 return;
7490 gcc_assert (node->dispatcher_function);
7492 node_v = node->function_version ();
7493 if (node_v == NULL)
7494 return;
7496 /* All semantically identical versions are chained. Traverse and mark each
7497 one of them as used. */
7498 it_v = node_v->next;
7499 while (it_v != NULL)
7501 mark_used (it_v->this_node->decl);
7502 it_v = it_v->next;
7506 /* Build a call to "the copy constructor" for the type of A, even if it
7507 wouldn't be selected by normal overload resolution. Used for
7508 diagnostics. */
7510 static tree
7511 call_copy_ctor (tree a, tsubst_flags_t complain)
7513 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
7514 tree binfo = TYPE_BINFO (ctype);
7515 tree copy = get_copy_ctor (ctype, complain);
7516 copy = build_baselink (binfo, binfo, copy, NULL_TREE);
7517 tree ob = build_dummy_object (ctype);
7518 vec<tree, va_gc>* args = make_tree_vector_single (a);
7519 tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
7520 LOOKUP_NORMAL, NULL, complain);
7521 release_tree_vector (args);
7522 return r;
7525 /* Return true iff T refers to a base field. */
7527 static bool
7528 is_base_field_ref (tree t)
7530 STRIP_NOPS (t);
7531 if (TREE_CODE (t) == ADDR_EXPR)
7532 t = TREE_OPERAND (t, 0);
7533 if (TREE_CODE (t) == COMPONENT_REF)
7534 t = TREE_OPERAND (t, 1);
7535 if (TREE_CODE (t) == FIELD_DECL)
7536 return DECL_FIELD_IS_BASE (t);
7537 return false;
7540 /* We can't elide a copy from a function returning by value to a base
7541 subobject, as the callee might clobber tail padding. Return true iff this
7542 could be that case. */
7544 static bool
7545 unsafe_copy_elision_p (tree target, tree exp)
7547 /* Copy elision only happens with a TARGET_EXPR. */
7548 if (TREE_CODE (exp) != TARGET_EXPR)
7549 return false;
7550 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7551 /* It's safe to elide the copy for a class with no tail padding. */
7552 if (tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
7553 return false;
7554 /* It's safe to elide the copy if we aren't initializing a base object. */
7555 if (!is_base_field_ref (target))
7556 return false;
7557 tree init = TARGET_EXPR_INITIAL (exp);
7558 /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
7559 while (TREE_CODE (init) == COMPOUND_EXPR)
7560 init = TREE_OPERAND (init, 1);
7561 return (TREE_CODE (init) == AGGR_INIT_EXPR
7562 && !AGGR_INIT_VIA_CTOR_P (init));
7565 /* Subroutine of the various build_*_call functions. Overload resolution
7566 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
7567 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
7568 bitmask of various LOOKUP_* flags which apply to the call itself. */
7570 static tree
7571 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
7573 tree fn = cand->fn;
7574 const vec<tree, va_gc> *args = cand->args;
7575 tree first_arg = cand->first_arg;
7576 conversion **convs = cand->convs;
7577 conversion *conv;
7578 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
7579 int parmlen;
7580 tree val;
7581 int i = 0;
7582 int j = 0;
7583 unsigned int arg_index = 0;
7584 int is_method = 0;
7585 int nargs;
7586 tree *argarray;
7587 bool already_used = false;
7589 /* In a template, there is no need to perform all of the work that
7590 is normally done. We are only interested in the type of the call
7591 expression, i.e., the return type of the function. Any semantic
7592 errors will be deferred until the template is instantiated. */
7593 if (processing_template_decl)
7595 tree expr, addr;
7596 tree return_type;
7597 const tree *argarray;
7598 unsigned int nargs;
7600 if (undeduced_auto_decl (fn))
7601 mark_used (fn, complain);
7603 return_type = TREE_TYPE (TREE_TYPE (fn));
7604 nargs = vec_safe_length (args);
7605 if (first_arg == NULL_TREE)
7606 argarray = args->address ();
7607 else
7609 tree *alcarray;
7610 unsigned int ix;
7611 tree arg;
7613 ++nargs;
7614 alcarray = XALLOCAVEC (tree, nargs);
7615 alcarray[0] = build_this (first_arg);
7616 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
7617 alcarray[ix + 1] = arg;
7618 argarray = alcarray;
7621 addr = build_addr_func (fn, complain);
7622 if (addr == error_mark_node)
7623 return error_mark_node;
7624 expr = build_call_array_loc (input_location, return_type,
7625 addr, nargs, argarray);
7626 if (TREE_THIS_VOLATILE (fn) && cfun)
7627 current_function_returns_abnormally = 1;
7628 return convert_from_reference (expr);
7631 /* Give any warnings we noticed during overload resolution. */
7632 if (cand->warnings && (complain & tf_warning))
7634 struct candidate_warning *w;
7635 for (w = cand->warnings; w; w = w->next)
7636 joust (cand, w->loser, 1, complain);
7639 /* OK, we're actually calling this inherited constructor; set its deletedness
7640 appropriately. We can get away with doing this here because calling is
7641 the only way to refer to a constructor. */
7642 if (DECL_INHERITED_CTOR (fn))
7643 deduce_inheriting_ctor (fn);
7645 /* Make =delete work with SFINAE. */
7646 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7647 return error_mark_node;
7649 if (DECL_FUNCTION_MEMBER_P (fn))
7651 tree access_fn;
7652 /* If FN is a template function, two cases must be considered.
7653 For example:
7655 struct A {
7656 protected:
7657 template <class T> void f();
7659 template <class T> struct B {
7660 protected:
7661 void g();
7663 struct C : A, B<int> {
7664 using A::f; // #1
7665 using B<int>::g; // #2
7668 In case #1 where `A::f' is a member template, DECL_ACCESS is
7669 recorded in the primary template but not in its specialization.
7670 We check access of FN using its primary template.
7672 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
7673 because it is a member of class template B, DECL_ACCESS is
7674 recorded in the specialization `B<int>::g'. We cannot use its
7675 primary template because `B<T>::g' and `B<int>::g' may have
7676 different access. */
7677 if (DECL_TEMPLATE_INFO (fn)
7678 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
7679 access_fn = DECL_TI_TEMPLATE (fn);
7680 else
7681 access_fn = fn;
7682 if (!perform_or_defer_access_check (cand->access_path, access_fn,
7683 fn, complain))
7684 return error_mark_node;
7687 /* If we're checking for implicit delete, don't bother with argument
7688 conversions. */
7689 if (flags & LOOKUP_SPECULATIVE)
7691 if (DECL_DELETED_FN (fn))
7693 if (complain & tf_error)
7694 mark_used (fn);
7695 return error_mark_node;
7697 if (cand->viable == 1)
7698 return fn;
7699 else if (!(complain & tf_error))
7700 /* Reject bad conversions now. */
7701 return error_mark_node;
7702 /* else continue to get conversion error. */
7705 /* N3276 magic doesn't apply to nested calls. */
7706 tsubst_flags_t decltype_flag = (complain & tf_decltype);
7707 complain &= ~tf_decltype;
7708 /* No-Cleanup doesn't apply to nested calls either. */
7709 tsubst_flags_t no_cleanup_complain = complain;
7710 complain &= ~tf_no_cleanup;
7712 /* Find maximum size of vector to hold converted arguments. */
7713 parmlen = list_length (parm);
7714 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
7715 if (parmlen > nargs)
7716 nargs = parmlen;
7717 argarray = XALLOCAVEC (tree, nargs);
7719 /* The implicit parameters to a constructor are not considered by overload
7720 resolution, and must be of the proper type. */
7721 if (DECL_CONSTRUCTOR_P (fn))
7723 tree object_arg;
7724 if (first_arg != NULL_TREE)
7726 object_arg = first_arg;
7727 first_arg = NULL_TREE;
7729 else
7731 object_arg = (*args)[arg_index];
7732 ++arg_index;
7734 argarray[j++] = build_this (object_arg);
7735 parm = TREE_CHAIN (parm);
7736 /* We should never try to call the abstract constructor. */
7737 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
7739 if (DECL_HAS_VTT_PARM_P (fn))
7741 argarray[j++] = (*args)[arg_index];
7742 ++arg_index;
7743 parm = TREE_CHAIN (parm);
7746 if (flags & LOOKUP_PREFER_RVALUE)
7748 /* The implicit move specified in 15.8.3/3 fails "...if the type of
7749 the first parameter of the selected constructor is not an rvalue
7750 reference to the object’s type (possibly cv-qualified)...." */
7751 gcc_assert (!(complain & tf_error));
7752 tree ptype = convs[0]->type;
7753 if (TREE_CODE (ptype) != REFERENCE_TYPE
7754 || !TYPE_REF_IS_RVALUE (ptype)
7755 || CONVERSION_RANK (convs[0]) > cr_exact)
7756 return error_mark_node;
7759 /* Bypass access control for 'this' parameter. */
7760 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7762 tree parmtype = TREE_VALUE (parm);
7763 tree arg = build_this (first_arg != NULL_TREE
7764 ? first_arg
7765 : (*args)[arg_index]);
7766 tree argtype = TREE_TYPE (arg);
7767 tree converted_arg;
7768 tree base_binfo;
7770 if (arg == error_mark_node)
7771 return error_mark_node;
7773 if (convs[i]->bad_p)
7775 if (complain & tf_error)
7777 if (permerror (input_location, "passing %qT as %<this%> "
7778 "argument discards qualifiers",
7779 TREE_TYPE (argtype)))
7780 inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
7782 else
7783 return error_mark_node;
7786 /* See if the function member or the whole class type is declared
7787 final and the call can be devirtualized. */
7788 if (DECL_FINAL_P (fn)
7789 || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
7790 flags |= LOOKUP_NONVIRTUAL;
7792 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
7793 X is called for an object that is not of type X, or of a type
7794 derived from X, the behavior is undefined.
7796 So we can assume that anything passed as 'this' is non-null, and
7797 optimize accordingly. */
7798 gcc_assert (TYPE_PTR_P (parmtype));
7799 /* Convert to the base in which the function was declared. */
7800 gcc_assert (cand->conversion_path != NULL_TREE);
7801 converted_arg = build_base_path (PLUS_EXPR,
7802 arg,
7803 cand->conversion_path,
7804 1, complain);
7805 /* Check that the base class is accessible. */
7806 if (!accessible_base_p (TREE_TYPE (argtype),
7807 BINFO_TYPE (cand->conversion_path), true))
7809 if (complain & tf_error)
7810 error ("%qT is not an accessible base of %qT",
7811 BINFO_TYPE (cand->conversion_path),
7812 TREE_TYPE (argtype));
7813 else
7814 return error_mark_node;
7816 /* If fn was found by a using declaration, the conversion path
7817 will be to the derived class, not the base declaring fn. We
7818 must convert from derived to base. */
7819 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
7820 TREE_TYPE (parmtype), ba_unique,
7821 NULL, complain);
7822 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
7823 base_binfo, 1, complain);
7825 argarray[j++] = converted_arg;
7826 parm = TREE_CHAIN (parm);
7827 if (first_arg != NULL_TREE)
7828 first_arg = NULL_TREE;
7829 else
7830 ++arg_index;
7831 ++i;
7832 is_method = 1;
7835 gcc_assert (first_arg == NULL_TREE);
7836 for (; arg_index < vec_safe_length (args) && parm;
7837 parm = TREE_CHAIN (parm), ++arg_index, ++i)
7839 tree type = TREE_VALUE (parm);
7840 tree arg = (*args)[arg_index];
7841 bool conversion_warning = true;
7843 conv = convs[i];
7845 /* If the argument is NULL and used to (implicitly) instantiate a
7846 template function (and bind one of the template arguments to
7847 the type of 'long int'), we don't want to warn about passing NULL
7848 to non-pointer argument.
7849 For example, if we have this template function:
7851 template<typename T> void func(T x) {}
7853 we want to warn (when -Wconversion is enabled) in this case:
7855 void foo() {
7856 func<int>(NULL);
7859 but not in this case:
7861 void foo() {
7862 func(NULL);
7865 if (arg == null_node
7866 && DECL_TEMPLATE_INFO (fn)
7867 && cand->template_decl
7868 && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
7869 conversion_warning = false;
7871 /* Warn about initializer_list deduction that isn't currently in the
7872 working draft. */
7873 if (cxx_dialect > cxx98
7874 && flag_deduce_init_list
7875 && cand->template_decl
7876 && is_std_init_list (non_reference (type))
7877 && BRACE_ENCLOSED_INITIALIZER_P (arg))
7879 tree tmpl = TI_TEMPLATE (cand->template_decl);
7880 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
7881 tree patparm = get_pattern_parm (realparm, tmpl);
7882 tree pattype = TREE_TYPE (patparm);
7883 if (PACK_EXPANSION_P (pattype))
7884 pattype = PACK_EXPANSION_PATTERN (pattype);
7885 pattype = non_reference (pattype);
7887 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
7888 && (cand->explicit_targs == NULL_TREE
7889 || (TREE_VEC_LENGTH (cand->explicit_targs)
7890 <= TEMPLATE_TYPE_IDX (pattype))))
7892 pedwarn (input_location, 0, "deducing %qT as %qT",
7893 non_reference (TREE_TYPE (patparm)),
7894 non_reference (type));
7895 pedwarn (DECL_SOURCE_LOCATION (cand->fn), 0,
7896 " in call to %qD", cand->fn);
7897 pedwarn (input_location, 0,
7898 " (you can disable this with -fno-deduce-init-list)");
7902 /* Set user_conv_p on the argument conversions, so rvalue/base handling
7903 knows not to allow any more UDCs. This needs to happen after we
7904 process cand->warnings. */
7905 if (flags & LOOKUP_NO_CONVERSION)
7906 conv->user_conv_p = true;
7908 tsubst_flags_t arg_complain = complain;
7909 if (!conversion_warning)
7910 arg_complain &= ~tf_warning;
7912 val = convert_like_with_context (conv, arg, fn, i - is_method,
7913 arg_complain);
7914 val = convert_for_arg_passing (type, val, arg_complain);
7916 if (val == error_mark_node)
7917 return error_mark_node;
7918 else
7919 argarray[j++] = val;
7922 /* Default arguments */
7923 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
7925 if (TREE_VALUE (parm) == error_mark_node)
7926 return error_mark_node;
7927 val = convert_default_arg (TREE_VALUE (parm),
7928 TREE_PURPOSE (parm),
7929 fn, i - is_method,
7930 complain);
7931 if (val == error_mark_node)
7932 return error_mark_node;
7933 argarray[j++] = val;
7936 /* Ellipsis */
7937 int magic = magic_varargs_p (fn);
7938 for (; arg_index < vec_safe_length (args); ++arg_index)
7940 tree a = (*args)[arg_index];
7941 if ((magic == 3 && arg_index == 2) || magic == 2)
7943 /* Do no conversions for certain magic varargs. */
7944 a = mark_type_use (a);
7945 if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
7946 return error_mark_node;
7948 else if (magic != 0)
7949 /* For other magic varargs only do decay_conversion. */
7950 a = decay_conversion (a, complain);
7951 else if (DECL_CONSTRUCTOR_P (fn)
7952 && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
7953 TREE_TYPE (a)))
7955 /* Avoid infinite recursion trying to call A(...). */
7956 if (complain & tf_error)
7957 /* Try to call the actual copy constructor for a good error. */
7958 call_copy_ctor (a, complain);
7959 return error_mark_node;
7961 else
7962 a = convert_arg_to_ellipsis (a, complain);
7963 if (a == error_mark_node)
7964 return error_mark_node;
7965 argarray[j++] = a;
7968 gcc_assert (j <= nargs);
7969 nargs = j;
7971 /* Avoid to do argument-transformation, if warnings for format, and for
7972 nonnull are disabled. Just in case that at least one of them is active
7973 the check_function_arguments function might warn about something. */
7975 bool warned_p = false;
7976 if (warn_nonnull
7977 || warn_format
7978 || warn_suggest_attribute_format
7979 || warn_restrict)
7981 tree *fargs = (!nargs ? argarray
7982 : (tree *) alloca (nargs * sizeof (tree)));
7983 for (j = 0; j < nargs; j++)
7984 fargs[j] = maybe_constant_value (argarray[j]);
7986 warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
7987 nargs, fargs, NULL);
7990 if (DECL_INHERITED_CTOR (fn))
7992 /* Check for passing ellipsis arguments to an inherited constructor. We
7993 could handle this by open-coding the inherited constructor rather than
7994 defining it, but let's not bother now. */
7995 if (!cp_unevaluated_operand
7996 && cand->num_convs
7997 && cand->convs[cand->num_convs-1]->ellipsis_p)
7999 if (complain & tf_error)
8001 sorry ("passing arguments to ellipsis of inherited constructor "
8002 "%qD", cand->fn);
8003 inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
8005 return error_mark_node;
8008 /* A base constructor inheriting from a virtual base doesn't get the
8009 inherited arguments, just this and __vtt. */
8010 if (ctor_omit_inherited_parms (fn))
8011 nargs = 2;
8014 /* Avoid actually calling copy constructors and copy assignment operators,
8015 if possible. */
8017 if (! flag_elide_constructors)
8018 /* Do things the hard way. */;
8019 else if (cand->num_convs == 1
8020 && (DECL_COPY_CONSTRUCTOR_P (fn)
8021 || DECL_MOVE_CONSTRUCTOR_P (fn))
8022 /* It's unsafe to elide the constructor when handling
8023 a noexcept-expression, it may evaluate to the wrong
8024 value (c++/53025). */
8025 && cp_noexcept_operand == 0)
8027 tree targ;
8028 tree arg = argarray[num_artificial_parms_for (fn)];
8029 tree fa;
8030 bool trivial = trivial_fn_p (fn);
8032 /* Pull out the real argument, disregarding const-correctness. */
8033 targ = arg;
8034 /* Strip the reference binding for the constructor parameter. */
8035 if (CONVERT_EXPR_P (targ)
8036 && TREE_CODE (TREE_TYPE (targ)) == REFERENCE_TYPE)
8037 targ = TREE_OPERAND (targ, 0);
8038 /* But don't strip any other reference bindings; binding a temporary to a
8039 reference prevents copy elision. */
8040 while ((CONVERT_EXPR_P (targ)
8041 && TREE_CODE (TREE_TYPE (targ)) != REFERENCE_TYPE)
8042 || TREE_CODE (targ) == NON_LVALUE_EXPR)
8043 targ = TREE_OPERAND (targ, 0);
8044 if (TREE_CODE (targ) == ADDR_EXPR)
8046 targ = TREE_OPERAND (targ, 0);
8047 if (!same_type_ignoring_top_level_qualifiers_p
8048 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
8049 targ = NULL_TREE;
8051 else
8052 targ = NULL_TREE;
8054 if (targ)
8055 arg = targ;
8056 else
8057 arg = cp_build_fold_indirect_ref (arg);
8059 /* In C++17 we shouldn't be copying a TARGET_EXPR except into a base
8060 subobject. */
8061 if (CHECKING_P && cxx_dialect >= cxx17)
8062 gcc_assert (TREE_CODE (arg) != TARGET_EXPR
8063 /* It's from binding the ref parm to a packed field. */
8064 || convs[0]->need_temporary_p
8065 || seen_error ()
8066 /* See unsafe_copy_elision_p. */
8067 || DECL_BASE_CONSTRUCTOR_P (fn));
8069 /* [class.copy]: the copy constructor is implicitly defined even if
8070 the implementation elided its use. */
8071 if (!trivial || DECL_DELETED_FN (fn))
8073 if (!mark_used (fn, complain) && !(complain & tf_error))
8074 return error_mark_node;
8075 already_used = true;
8078 /* If we're creating a temp and we already have one, don't create a
8079 new one. If we're not creating a temp but we get one, use
8080 INIT_EXPR to collapse the temp into our target. Otherwise, if the
8081 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
8082 temp or an INIT_EXPR otherwise. */
8083 fa = argarray[0];
8084 if (is_dummy_object (fa))
8086 if (TREE_CODE (arg) == TARGET_EXPR)
8087 return arg;
8088 else if (trivial)
8089 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
8091 else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
8092 && !unsafe_copy_elision_p (fa, arg))
8094 tree to = cp_stabilize_reference (cp_build_fold_indirect_ref (fa));
8096 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
8097 return val;
8100 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
8101 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
8102 && trivial_fn_p (fn)
8103 && !DECL_DELETED_FN (fn))
8105 tree to = cp_stabilize_reference
8106 (cp_build_fold_indirect_ref (argarray[0]));
8107 tree type = TREE_TYPE (to);
8108 tree as_base = CLASSTYPE_AS_BASE (type);
8109 tree arg = argarray[1];
8111 if (is_really_empty_class (type))
8113 /* Avoid copying empty classes. */
8114 val = build2 (COMPOUND_EXPR, type, arg, to);
8115 TREE_NO_WARNING (val) = 1;
8117 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
8119 arg = cp_build_fold_indirect_ref (arg);
8120 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
8121 /* Handle NSDMI that refer to the object being initialized. */
8122 replace_placeholders (arg, to);
8124 else
8126 /* We must only copy the non-tail padding parts. */
8127 tree arg0, arg2, t;
8128 tree array_type, alias_set;
8130 arg2 = TYPE_SIZE_UNIT (as_base);
8131 arg0 = cp_build_addr_expr (to, complain);
8133 array_type = build_array_type (unsigned_char_type_node,
8134 build_index_type
8135 (size_binop (MINUS_EXPR,
8136 arg2, size_int (1))));
8137 alias_set = build_int_cst (build_pointer_type (type), 0);
8138 t = build2 (MODIFY_EXPR, void_type_node,
8139 build2 (MEM_REF, array_type, arg0, alias_set),
8140 build2 (MEM_REF, array_type, arg, alias_set));
8141 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
8142 TREE_NO_WARNING (val) = 1;
8145 return val;
8147 else if (!DECL_DELETED_FN (fn)
8148 && trivial_fn_p (fn))
8150 if (DECL_DESTRUCTOR_P (fn))
8151 return fold_convert (void_type_node, argarray[0]);
8152 else if (default_ctor_p (fn))
8154 if (is_dummy_object (argarray[0]))
8155 return force_target_expr (DECL_CONTEXT (fn), void_node,
8156 no_cleanup_complain);
8157 else
8158 return cp_build_fold_indirect_ref (argarray[0]);
8162 /* For calls to a multi-versioned function, overload resolution
8163 returns the function with the highest target priority, that is,
8164 the version that will checked for dispatching first. If this
8165 version is inlinable, a direct call to this version can be made
8166 otherwise the call should go through the dispatcher. */
8168 if (DECL_FUNCTION_VERSIONED (fn)
8169 && (current_function_decl == NULL
8170 || !targetm.target_option.can_inline_p (current_function_decl, fn)))
8172 fn = get_function_version_dispatcher (fn);
8173 if (fn == NULL)
8174 return NULL;
8175 if (!already_used)
8176 mark_versions_used (fn);
8179 if (!already_used
8180 && !mark_used (fn, complain))
8181 return error_mark_node;
8183 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
8184 /* Don't mess with virtual lookup in instantiate_non_dependent_expr;
8185 virtual functions can't be constexpr. */
8186 && !in_template_function ())
8188 tree t;
8189 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
8190 DECL_CONTEXT (fn),
8191 ba_any, NULL, complain);
8192 gcc_assert (binfo && binfo != error_mark_node);
8194 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
8195 complain);
8196 if (TREE_SIDE_EFFECTS (argarray[0]))
8197 argarray[0] = save_expr (argarray[0]);
8198 t = build_pointer_type (TREE_TYPE (fn));
8199 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
8200 TREE_TYPE (fn) = t;
8202 else
8204 fn = build_addr_func (fn, complain);
8205 if (fn == error_mark_node)
8206 return error_mark_node;
8209 tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
8210 if (call == error_mark_node)
8211 return call;
8212 if (cand->flags & LOOKUP_LIST_INIT_CTOR)
8214 tree c = extract_call_expr (call);
8215 /* build_new_op_1 will clear this when appropriate. */
8216 CALL_EXPR_ORDERED_ARGS (c) = true;
8218 if (warned_p)
8220 tree c = extract_call_expr (call);
8221 if (TREE_CODE (c) == CALL_EXPR)
8222 TREE_NO_WARNING (c) = 1;
8224 return call;
8227 /* Return the DECL of the first non-public data member of class TYPE
8228 or null if none can be found. */
8230 static tree
8231 first_non_public_field (tree type)
8233 if (!CLASS_TYPE_P (type))
8234 return NULL_TREE;
8236 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8238 if (TREE_CODE (field) != FIELD_DECL)
8239 continue;
8240 if (TREE_STATIC (field))
8241 continue;
8242 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
8243 return field;
8246 int i = 0;
8248 for (tree base_binfo, binfo = TYPE_BINFO (type);
8249 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8251 tree base = TREE_TYPE (base_binfo);
8253 if (tree field = first_non_public_field (base))
8254 return field;
8257 return NULL_TREE;
8260 /* Return true if all copy and move assignment operator overloads for
8261 class TYPE are trivial and at least one of them is not deleted and,
8262 when ACCESS is set, accessible. Return false otherwise. Set
8263 HASASSIGN to true when the TYPE has a (not necessarily trivial)
8264 copy or move assignment. */
8266 static bool
8267 has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
8269 tree fns = get_class_binding (type, assign_op_identifier);
8270 bool all_trivial = true;
8272 /* Iterate over overloads of the assignment operator, checking
8273 accessible copy assignments for triviality. */
8275 for (ovl_iterator oi (fns); oi; ++oi)
8277 tree f = *oi;
8279 /* Skip operators that aren't copy assignments. */
8280 if (!copy_fn_p (f))
8281 continue;
8283 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
8284 || accessible_p (TYPE_BINFO (type), f, true));
8286 /* Skip template assignment operators and deleted functions. */
8287 if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
8288 continue;
8290 if (accessible)
8291 *hasassign = true;
8293 if (!accessible || !trivial_fn_p (f))
8294 all_trivial = false;
8296 /* Break early when both properties have been determined. */
8297 if (*hasassign && !all_trivial)
8298 break;
8301 /* Return true if they're all trivial and one of the expressions
8302 TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
8303 tree ref = cp_build_reference_type (type, false);
8304 return (all_trivial
8305 && (is_trivially_xible (MODIFY_EXPR, type, type)
8306 || is_trivially_xible (MODIFY_EXPR, type, ref)));
8309 /* Return true if all copy and move ctor overloads for class TYPE are
8310 trivial and at least one of them is not deleted and, when ACCESS is
8311 set, accessible. Return false otherwise. Set each element of HASCTOR[]
8312 to true when the TYPE has a (not necessarily trivial) default and copy
8313 (or move) ctor, respectively. */
8315 static bool
8316 has_trivial_copy_p (tree type, bool access, bool hasctor[2])
8318 tree fns = get_class_binding (type, complete_ctor_identifier);
8319 bool all_trivial = true;
8321 for (ovl_iterator oi (fns); oi; ++oi)
8323 tree f = *oi;
8325 /* Skip template constructors. */
8326 if (TREE_CODE (f) != FUNCTION_DECL)
8327 continue;
8329 bool cpy_or_move_ctor_p = copy_fn_p (f);
8331 /* Skip ctors other than default, copy, and move. */
8332 if (!cpy_or_move_ctor_p && !default_ctor_p (f))
8333 continue;
8335 if (DECL_DELETED_FN (f))
8336 continue;
8338 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
8339 || accessible_p (TYPE_BINFO (type), f, true));
8341 if (accessible)
8342 hasctor[cpy_or_move_ctor_p] = true;
8344 if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
8345 all_trivial = false;
8347 /* Break early when both properties have been determined. */
8348 if (hasctor[0] && hasctor[1] && !all_trivial)
8349 break;
8352 return all_trivial;
8355 /* Issue a warning on a call to the built-in function FNDECL if it is
8356 a raw memory write whose destination is not an object of (something
8357 like) trivial or standard layout type with a non-deleted assignment
8358 and copy ctor. Detects const correctness violations, corrupting
8359 references, virtual table pointers, and bypassing non-trivial
8360 assignments. */
8362 static void
8363 maybe_warn_class_memaccess (location_t loc, tree fndecl, tree *args)
8365 /* Except for bcopy where it's second, the destination pointer is
8366 the first argument for all functions handled here. Compute
8367 the index of the destination and source arguments. */
8368 unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
8369 unsigned srcidx = !dstidx;
8371 tree dest = args[dstidx];
8372 if (!dest || !TREE_TYPE (dest) || !POINTER_TYPE_P (TREE_TYPE (dest)))
8373 return;
8375 /* Remove the outermost (usually implicit) conversion to the void*
8376 argument type. */
8377 if (TREE_CODE (dest) == NOP_EXPR)
8378 dest = TREE_OPERAND (dest, 0);
8380 tree srctype = NULL_TREE;
8382 /* Determine the type of the pointed-to object and whether it's
8383 a complete class type. */
8384 tree desttype = TREE_TYPE (TREE_TYPE (dest));
8386 if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
8387 return;
8389 /* Check to see if the raw memory call is made by a ctor or dtor
8390 with this as the destination argument for the destination type.
8391 If so, be more permissive. */
8392 if (current_function_decl
8393 && (DECL_CONSTRUCTOR_P (current_function_decl)
8394 || DECL_DESTRUCTOR_P (current_function_decl))
8395 && is_this_parameter (tree_strip_nop_conversions (dest)))
8397 tree ctx = DECL_CONTEXT (current_function_decl);
8398 bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
8400 tree binfo = TYPE_BINFO (ctx);
8402 /* A ctor and dtor for a class with no bases and no virtual functions
8403 can do whatever they want. Bail early with no further checking. */
8404 if (special && !BINFO_VTABLE (binfo) && !BINFO_N_BASE_BINFOS (binfo))
8405 return;
8408 /* True if the class is trivial. */
8409 bool trivial = trivial_type_p (desttype);
8411 /* Set to true if DESTYPE has an accessible copy assignment. */
8412 bool hasassign = false;
8413 /* True if all of the class' overloaded copy assignment operators
8414 are all trivial (and not deleted) and at least one of them is
8415 accessible. */
8416 bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
8418 /* Set to true if DESTTYPE has an accessible default and copy ctor,
8419 respectively. */
8420 bool hasctors[2] = { false, false };
8422 /* True if all of the class' overloaded copy constructors are all
8423 trivial (and not deleted) and at least one of them is accessible. */
8424 bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
8426 /* Set FLD to the first private/protected member of the class. */
8427 tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
8429 /* The warning format string. */
8430 const char *warnfmt = NULL;
8431 /* A suggested alternative to offer instead of the raw memory call.
8432 Empty string when none can be come up with. */
8433 const char *suggest = "";
8434 bool warned = false;
8436 switch (DECL_FUNCTION_CODE (fndecl))
8438 case BUILT_IN_MEMSET:
8439 if (!integer_zerop (args[1]))
8441 /* Diagnose setting non-copy-assignable or non-trivial types,
8442 or types with a private member, to (potentially) non-zero
8443 bytes. Since the value of the bytes being written is unknown,
8444 suggest using assignment instead (if one exists). Also warn
8445 for writes into objects for which zero-initialization doesn't
8446 mean all bits clear (pointer-to-member data, where null is all
8447 bits set). Since the value being written is (most likely)
8448 non-zero, simply suggest assignment (but not copy assignment). */
8449 suggest = "; use assignment instead";
8450 if (!trivassign)
8451 warnfmt = G_("%qD writing to an object of type %#qT with "
8452 "no trivial copy-assignment");
8453 else if (!trivial)
8454 warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
8455 else if (fld)
8457 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
8458 warned = warning_at (loc, OPT_Wclass_memaccess,
8459 "%qD writing to an object of type %#qT with "
8460 "%qs member %qD",
8461 fndecl, desttype, access, fld);
8463 else if (!zero_init_p (desttype))
8464 warnfmt = G_("%qD writing to an object of type %#qT containing "
8465 "a pointer to data member%s");
8467 break;
8469 /* Fall through. */
8471 case BUILT_IN_BZERO:
8472 /* Similarly to the above, diagnose clearing non-trivial or non-
8473 standard layout objects, or objects of types with no assignmenmt.
8474 Since the value being written is known to be zero, suggest either
8475 copy assignment, copy ctor, or default ctor as an alternative,
8476 depending on what's available. */
8478 if (hasassign && hasctors[0])
8479 suggest = G_("; use assignment or value-initialization instead");
8480 else if (hasassign)
8481 suggest = G_("; use assignment instead");
8482 else if (hasctors[0])
8483 suggest = G_("; use value-initialization instead");
8485 if (!trivassign)
8486 warnfmt = G_("%qD clearing an object of type %#qT with "
8487 "no trivial copy-assignment%s");
8488 else if (!trivial)
8489 warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
8490 else if (!zero_init_p (desttype))
8491 warnfmt = G_("%qD clearing an object of type %#qT containing "
8492 "a pointer-to-member%s");
8493 break;
8495 case BUILT_IN_BCOPY:
8496 case BUILT_IN_MEMCPY:
8497 case BUILT_IN_MEMMOVE:
8498 case BUILT_IN_MEMPCPY:
8499 /* Determine the type of the source object. */
8500 srctype = STRIP_NOPS (args[srcidx]);
8501 srctype = TREE_TYPE (TREE_TYPE (srctype));
8503 /* Since it's impossible to determine wheter the byte copy is
8504 being used in place of assignment to an existing object or
8505 as a substitute for initialization, assume it's the former.
8506 Determine the best alternative to use instead depending on
8507 what's not deleted. */
8508 if (hasassign && hasctors[1])
8509 suggest = G_("; use copy-assignment or copy-initialization instead");
8510 else if (hasassign)
8511 suggest = G_("; use copy-assignment instead");
8512 else if (hasctors[1])
8513 suggest = G_("; use copy-initialization instead");
8515 if (!trivassign)
8516 warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
8517 "copy-assignment%s");
8518 else if (!trivially_copyable_p (desttype))
8519 warnfmt = G_("%qD writing to an object of non-trivially copyable "
8520 "type %#qT%s");
8521 else if (!trivcopy)
8522 warnfmt = G_("%qD writing to an object with a deleted copy constructor");
8524 else if (!trivial
8525 && !VOID_TYPE_P (srctype)
8526 && !char_type_p (TYPE_MAIN_VARIANT (srctype))
8527 && !same_type_ignoring_top_level_qualifiers_p (desttype,
8528 srctype))
8530 /* Warn when copying into a non-trivial object from an object
8531 of a different type other than void or char. */
8532 warned = warning_at (loc, OPT_Wclass_memaccess,
8533 "%qD copying an object of non-trivial type "
8534 "%#qT from an array of %#qT",
8535 fndecl, desttype, srctype);
8537 else if (fld
8538 && !VOID_TYPE_P (srctype)
8539 && !char_type_p (TYPE_MAIN_VARIANT (srctype))
8540 && !same_type_ignoring_top_level_qualifiers_p (desttype,
8541 srctype))
8543 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
8544 warned = warning_at (loc, OPT_Wclass_memaccess,
8545 "%qD copying an object of type %#qT with "
8546 "%qs member %qD from an array of %#qT; use "
8547 "assignment or copy-initialization instead",
8548 fndecl, desttype, access, fld, srctype);
8550 else if (!trivial && TREE_CODE (args[2]) == INTEGER_CST)
8552 /* Finally, warn on partial copies. */
8553 unsigned HOST_WIDE_INT typesize
8554 = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
8555 if (unsigned HOST_WIDE_INT partial
8556 = tree_to_uhwi (args[2]) % typesize)
8557 warned = warning_at (loc, OPT_Wclass_memaccess,
8558 (typesize - partial > 1
8559 ? G_("%qD writing to an object of "
8560 "a non-trivial type %#qT leaves %wu "
8561 "bytes unchanged")
8562 : G_("%qD writing to an object of "
8563 "a non-trivial type %#qT leaves %wu "
8564 "byte unchanged")),
8565 fndecl, desttype, typesize - partial);
8567 break;
8569 case BUILT_IN_REALLOC:
8571 if (!trivially_copyable_p (desttype))
8572 warnfmt = G_("%qD moving an object of non-trivially copyable type "
8573 "%#qT; use %<new%> and %<delete%> instead");
8574 else if (!trivcopy)
8575 warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
8576 "constructor; use %<new%> and %<delete%> instead");
8577 else if (!get_dtor (desttype, tf_none))
8578 warnfmt = G_("%qD moving an object of type %#qT with deleted "
8579 "destructor");
8580 else if (!trivial
8581 && TREE_CODE (args[1]) == INTEGER_CST
8582 && tree_int_cst_lt (args[1], TYPE_SIZE_UNIT (desttype)))
8584 /* Finally, warn on reallocation into insufficient space. */
8585 warned = warning_at (loc, OPT_Wclass_memaccess,
8586 "%qD moving an object of non-trivial type "
8587 "%#qT and size %E into a region of size %E",
8588 fndecl, desttype, TYPE_SIZE_UNIT (desttype),
8589 args[1]);
8591 break;
8593 default:
8594 return;
8597 if (!warned && !warnfmt)
8598 return;
8600 if (warnfmt)
8602 if (suggest)
8603 warned = warning_at (loc, OPT_Wclass_memaccess,
8604 warnfmt, fndecl, desttype, suggest);
8605 else
8606 warned = warning_at (loc, OPT_Wclass_memaccess,
8607 warnfmt, fndecl, desttype);
8610 if (warned)
8611 inform (location_of (desttype), "%#qT declared here", desttype);
8614 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
8615 This function performs no overload resolution, conversion, or other
8616 high-level operations. */
8618 tree
8619 build_cxx_call (tree fn, int nargs, tree *argarray,
8620 tsubst_flags_t complain)
8622 tree fndecl;
8624 /* Remember roughly where this call is. */
8625 location_t loc = EXPR_LOC_OR_LOC (fn, input_location);
8626 fn = build_call_a (fn, nargs, argarray);
8627 SET_EXPR_LOCATION (fn, loc);
8629 fndecl = get_callee_fndecl (fn);
8631 /* Check that arguments to builtin functions match the expectations. */
8632 if (fndecl
8633 && DECL_BUILT_IN (fndecl)
8634 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
8636 int i;
8638 /* We need to take care that values to BUILT_IN_NORMAL
8639 are reduced. */
8640 for (i = 0; i < nargs; i++)
8641 argarray[i] = fold_non_dependent_expr (argarray[i]);
8643 if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
8644 nargs, argarray))
8645 return error_mark_node;
8647 /* Warn if the built-in writes to an object of a non-trivial type. */
8648 if (nargs)
8649 maybe_warn_class_memaccess (loc, fndecl, argarray);
8652 if (VOID_TYPE_P (TREE_TYPE (fn)))
8653 return fn;
8655 /* 5.2.2/11: If a function call is a prvalue of object type: if the
8656 function call is either the operand of a decltype-specifier or the
8657 right operand of a comma operator that is the operand of a
8658 decltype-specifier, a temporary object is not introduced for the
8659 prvalue. The type of the prvalue may be incomplete. */
8660 if (!(complain & tf_decltype))
8662 fn = require_complete_type_sfinae (fn, complain);
8663 if (fn == error_mark_node)
8664 return error_mark_node;
8666 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
8668 fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
8669 maybe_warn_parm_abi (TREE_TYPE (fn), loc);
8672 return convert_from_reference (fn);
8675 /* Returns the value to use for the in-charge parameter when making a
8676 call to a function with the indicated NAME.
8678 FIXME:Can't we find a neater way to do this mapping? */
8680 tree
8681 in_charge_arg_for_name (tree name)
8683 if (IDENTIFIER_CTOR_P (name))
8685 if (name == complete_ctor_identifier)
8686 return integer_one_node;
8687 gcc_checking_assert (name == base_ctor_identifier);
8689 else
8691 if (name == complete_dtor_identifier)
8692 return integer_two_node;
8693 else if (name == deleting_dtor_identifier)
8694 return integer_three_node;
8695 gcc_checking_assert (name == base_dtor_identifier);
8698 return integer_zero_node;
8701 /* We've built up a constructor call RET. Complain if it delegates to the
8702 constructor we're currently compiling. */
8704 static void
8705 check_self_delegation (tree ret)
8707 if (TREE_CODE (ret) == TARGET_EXPR)
8708 ret = TARGET_EXPR_INITIAL (ret);
8709 tree fn = cp_get_callee_fndecl (ret);
8710 if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
8711 error ("constructor delegates to itself");
8714 /* Build a call to a constructor, destructor, or an assignment
8715 operator for INSTANCE, an expression with class type. NAME
8716 indicates the special member function to call; *ARGS are the
8717 arguments. ARGS may be NULL. This may change ARGS. BINFO
8718 indicates the base of INSTANCE that is to be passed as the `this'
8719 parameter to the member function called.
8721 FLAGS are the LOOKUP_* flags to use when processing the call.
8723 If NAME indicates a complete object constructor, INSTANCE may be
8724 NULL_TREE. In this case, the caller will call build_cplus_new to
8725 store the newly constructed object into a VAR_DECL. */
8727 tree
8728 build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
8729 tree binfo, int flags, tsubst_flags_t complain)
8731 tree fns;
8732 /* The type of the subobject to be constructed or destroyed. */
8733 tree class_type;
8734 vec<tree, va_gc> *allocated = NULL;
8735 tree ret;
8737 gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
8738 if (TYPE_P (binfo))
8740 /* Resolve the name. */
8741 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
8742 return error_mark_node;
8744 binfo = TYPE_BINFO (binfo);
8747 gcc_assert (binfo != NULL_TREE);
8749 class_type = BINFO_TYPE (binfo);
8751 /* Handle the special case where INSTANCE is NULL_TREE. */
8752 if (name == complete_ctor_identifier && !instance)
8753 instance = build_dummy_object (class_type);
8754 else
8756 if (IDENTIFIER_DTOR_P (name))
8757 gcc_assert (args == NULL || vec_safe_is_empty (*args));
8759 /* Convert to the base class, if necessary. */
8760 if (!same_type_ignoring_top_level_qualifiers_p
8761 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
8763 if (IDENTIFIER_CDTOR_P (name))
8764 /* For constructors and destructors, either the base is
8765 non-virtual, or it is virtual but we are doing the
8766 conversion from a constructor or destructor for the
8767 complete object. In either case, we can convert
8768 statically. */
8769 instance = convert_to_base_statically (instance, binfo);
8770 else
8772 /* However, for assignment operators, we must convert
8773 dynamically if the base is virtual. */
8774 gcc_checking_assert (name == assign_op_identifier);
8775 instance = build_base_path (PLUS_EXPR, instance,
8776 binfo, /*nonnull=*/1, complain);
8781 gcc_assert (instance != NULL_TREE);
8783 /* In C++17, "If the initializer expression is a prvalue and the
8784 cv-unqualified version of the source type is the same class as the class
8785 of the destination, the initializer expression is used to initialize the
8786 destination object." Handle that here to avoid doing overload
8787 resolution. */
8788 if (cxx_dialect >= cxx17
8789 && args && vec_safe_length (*args) == 1
8790 && name == complete_ctor_identifier)
8792 tree arg = (**args)[0];
8794 /* FIXME P0135 doesn't say how to handle direct initialization from a
8795 type with a suitable conversion operator. Let's handle it like
8796 copy-initialization, but allowing explict conversions. */
8797 tsubst_flags_t sub_complain = tf_warning;
8798 if (!is_dummy_object (instance))
8799 /* If we're using this to initialize a non-temporary object, don't
8800 require the destructor to be accessible. */
8801 sub_complain |= tf_no_cleanup;
8802 if (!reference_related_p (class_type, TREE_TYPE (arg)))
8803 arg = perform_implicit_conversion_flags (class_type, arg,
8804 sub_complain,
8805 flags);
8806 if ((TREE_CODE (arg) == TARGET_EXPR
8807 || TREE_CODE (arg) == CONSTRUCTOR)
8808 && (same_type_ignoring_top_level_qualifiers_p
8809 (class_type, TREE_TYPE (arg))))
8811 if (is_dummy_object (instance))
8812 return arg;
8813 if ((complain & tf_error)
8814 && (flags & LOOKUP_DELEGATING_CONS))
8815 check_self_delegation (arg);
8816 /* Avoid change of behavior on Wunused-var-2.C. */
8817 instance = mark_lvalue_use (instance);
8818 return build2 (INIT_EXPR, class_type, instance, arg);
8822 fns = lookup_fnfields (binfo, name, 1);
8824 /* When making a call to a constructor or destructor for a subobject
8825 that uses virtual base classes, pass down a pointer to a VTT for
8826 the subobject. */
8827 if ((name == base_ctor_identifier
8828 || name == base_dtor_identifier)
8829 && CLASSTYPE_VBASECLASSES (class_type))
8831 tree vtt;
8832 tree sub_vtt;
8834 /* If the current function is a complete object constructor
8835 or destructor, then we fetch the VTT directly.
8836 Otherwise, we look it up using the VTT we were given. */
8837 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
8838 vtt = decay_conversion (vtt, complain);
8839 if (vtt == error_mark_node)
8840 return error_mark_node;
8841 vtt = build_if_in_charge (vtt, current_vtt_parm);
8842 if (BINFO_SUBVTT_INDEX (binfo))
8843 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
8844 else
8845 sub_vtt = vtt;
8847 if (args == NULL)
8849 allocated = make_tree_vector ();
8850 args = &allocated;
8853 vec_safe_insert (*args, 0, sub_vtt);
8856 ret = build_new_method_call (instance, fns, args,
8857 TYPE_BINFO (BINFO_TYPE (binfo)),
8858 flags, /*fn=*/NULL,
8859 complain);
8861 if (allocated != NULL)
8862 release_tree_vector (allocated);
8864 if ((complain & tf_error)
8865 && (flags & LOOKUP_DELEGATING_CONS)
8866 && name == complete_ctor_identifier)
8867 check_self_delegation (ret);
8869 return ret;
8872 /* Return the NAME, as a C string. The NAME indicates a function that
8873 is a member of TYPE. *FREE_P is set to true if the caller must
8874 free the memory returned.
8876 Rather than go through all of this, we should simply set the names
8877 of constructors and destructors appropriately, and dispense with
8878 ctor_identifier, dtor_identifier, etc. */
8880 static char *
8881 name_as_c_string (tree name, tree type, bool *free_p)
8883 const char *pretty_name;
8885 /* Assume that we will not allocate memory. */
8886 *free_p = false;
8887 /* Constructors and destructors are special. */
8888 if (IDENTIFIER_CDTOR_P (name))
8890 pretty_name
8891 = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
8892 /* For a destructor, add the '~'. */
8893 if (IDENTIFIER_DTOR_P (name))
8895 pretty_name = concat ("~", pretty_name, NULL);
8896 /* Remember that we need to free the memory allocated. */
8897 *free_p = true;
8900 else if (IDENTIFIER_CONV_OP_P (name))
8902 pretty_name = concat ("operator ",
8903 type_as_string_translate (TREE_TYPE (name),
8904 TFF_PLAIN_IDENTIFIER),
8905 NULL);
8906 /* Remember that we need to free the memory allocated. */
8907 *free_p = true;
8909 else
8910 pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
8912 return CONST_CAST (char *, pretty_name);
8915 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
8916 be set, upon return, to the function called. ARGS may be NULL.
8917 This may change ARGS. */
8919 static tree
8920 build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
8921 tree conversion_path, int flags,
8922 tree *fn_p, tsubst_flags_t complain)
8924 struct z_candidate *candidates = 0, *cand;
8925 tree explicit_targs = NULL_TREE;
8926 tree basetype = NULL_TREE;
8927 tree access_binfo, binfo;
8928 tree optype;
8929 tree first_mem_arg = NULL_TREE;
8930 tree name;
8931 bool skip_first_for_error;
8932 vec<tree, va_gc> *user_args;
8933 tree call;
8934 tree fn;
8935 int template_only = 0;
8936 bool any_viable_p;
8937 tree orig_instance;
8938 tree orig_fns;
8939 vec<tree, va_gc> *orig_args = NULL;
8940 void *p;
8942 gcc_assert (instance != NULL_TREE);
8944 /* We don't know what function we're going to call, yet. */
8945 if (fn_p)
8946 *fn_p = NULL_TREE;
8948 if (error_operand_p (instance)
8949 || !fns || error_operand_p (fns))
8950 return error_mark_node;
8952 if (!BASELINK_P (fns))
8954 if (complain & tf_error)
8955 error ("call to non-function %qD", fns);
8956 return error_mark_node;
8959 orig_instance = instance;
8960 orig_fns = fns;
8962 /* Dismantle the baselink to collect all the information we need. */
8963 if (!conversion_path)
8964 conversion_path = BASELINK_BINFO (fns);
8965 access_binfo = BASELINK_ACCESS_BINFO (fns);
8966 binfo = BASELINK_BINFO (fns);
8967 optype = BASELINK_OPTYPE (fns);
8968 fns = BASELINK_FUNCTIONS (fns);
8969 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
8971 explicit_targs = TREE_OPERAND (fns, 1);
8972 fns = TREE_OPERAND (fns, 0);
8973 template_only = 1;
8975 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
8976 || TREE_CODE (fns) == TEMPLATE_DECL
8977 || TREE_CODE (fns) == OVERLOAD);
8978 fn = OVL_FIRST (fns);
8979 name = DECL_NAME (fn);
8981 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
8982 gcc_assert (CLASS_TYPE_P (basetype));
8984 if (processing_template_decl)
8986 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
8987 instance = build_non_dependent_expr (instance);
8988 if (args != NULL)
8989 make_args_non_dependent (*args);
8992 user_args = args == NULL ? NULL : *args;
8993 /* Under DR 147 A::A() is an invalid constructor call,
8994 not a functional cast. */
8995 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
8997 if (! (complain & tf_error))
8998 return error_mark_node;
9000 basetype = DECL_CONTEXT (fn);
9001 name = constructor_name (basetype);
9002 if (permerror (input_location,
9003 "cannot call constructor %<%T::%D%> directly",
9004 basetype, name))
9005 inform (input_location, "for a function-style cast, remove the "
9006 "redundant %<::%D%>", name);
9007 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
9008 complain);
9009 return call;
9012 /* Process the argument list. */
9013 if (args != NULL && *args != NULL)
9015 *args = resolve_args (*args, complain);
9016 if (*args == NULL)
9017 return error_mark_node;
9020 /* Consider the object argument to be used even if we end up selecting a
9021 static member function. */
9022 instance = mark_type_use (instance);
9024 /* Figure out whether to skip the first argument for the error
9025 message we will display to users if an error occurs. We don't
9026 want to display any compiler-generated arguments. The "this"
9027 pointer hasn't been added yet. However, we must remove the VTT
9028 pointer if this is a call to a base-class constructor or
9029 destructor. */
9030 skip_first_for_error = false;
9031 if (IDENTIFIER_CDTOR_P (name))
9033 /* Callers should explicitly indicate whether they want to ctor
9034 the complete object or just the part without virtual bases. */
9035 gcc_assert (name != ctor_identifier);
9037 /* Remove the VTT pointer, if present. */
9038 if ((name == base_ctor_identifier || name == base_dtor_identifier)
9039 && CLASSTYPE_VBASECLASSES (basetype))
9040 skip_first_for_error = true;
9042 /* It's OK to call destructors and constructors on cv-qualified
9043 objects. Therefore, convert the INSTANCE to the unqualified
9044 type, if necessary. */
9045 if (!same_type_p (basetype, TREE_TYPE (instance)))
9047 instance = build_this (instance);
9048 instance = build_nop (build_pointer_type (basetype), instance);
9049 instance = build_fold_indirect_ref (instance);
9052 else
9053 gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
9055 /* For the overload resolution we need to find the actual `this`
9056 that would be captured if the call turns out to be to a
9057 non-static member function. Do not actually capture it at this
9058 point. */
9059 if (DECL_CONSTRUCTOR_P (fn))
9060 /* Constructors don't use the enclosing 'this'. */
9061 first_mem_arg = instance;
9062 else
9063 first_mem_arg = maybe_resolve_dummy (instance, false);
9065 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9066 p = conversion_obstack_alloc (0);
9068 /* The number of arguments artificial parms in ARGS; we subtract one because
9069 there's no 'this' in ARGS. */
9070 unsigned skip = num_artificial_parms_for (fn) - 1;
9072 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
9073 initializer, not T({ }). */
9074 if (DECL_CONSTRUCTOR_P (fn)
9075 && vec_safe_length (user_args) > skip
9076 && DIRECT_LIST_INIT_P ((*user_args)[skip]))
9078 tree init_list = (*user_args)[skip];
9079 tree init = NULL_TREE;
9081 gcc_assert (user_args->length () == skip + 1
9082 && !(flags & LOOKUP_ONLYCONVERTING));
9084 /* If the initializer list has no elements and T is a class type with
9085 a default constructor, the object is value-initialized. Handle
9086 this here so we don't need to handle it wherever we use
9087 build_special_member_call. */
9088 if (CONSTRUCTOR_NELTS (init_list) == 0
9089 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
9090 /* For a user-provided default constructor, use the normal
9091 mechanisms so that protected access works. */
9092 && type_has_non_user_provided_default_constructor (basetype)
9093 && !processing_template_decl)
9094 init = build_value_init (basetype, complain);
9096 /* If BASETYPE is an aggregate, we need to do aggregate
9097 initialization. */
9098 else if (CP_AGGREGATE_TYPE_P (basetype))
9100 init = reshape_init (basetype, init_list, complain);
9101 init = digest_init (basetype, init, complain);
9104 if (init)
9106 if (is_dummy_object (instance))
9107 return get_target_expr_sfinae (init, complain);
9108 init = build2 (INIT_EXPR, TREE_TYPE (instance), instance, init);
9109 TREE_SIDE_EFFECTS (init) = true;
9110 return init;
9113 /* Otherwise go ahead with overload resolution. */
9114 add_list_candidates (fns, first_mem_arg, user_args,
9115 basetype, explicit_targs, template_only,
9116 conversion_path, access_binfo, flags,
9117 &candidates, complain);
9119 else
9120 add_candidates (fns, first_mem_arg, user_args, optype,
9121 explicit_targs, template_only, conversion_path,
9122 access_binfo, flags, &candidates, complain);
9124 any_viable_p = false;
9125 candidates = splice_viable (candidates, false, &any_viable_p);
9127 if (!any_viable_p)
9129 if (complain & tf_error)
9131 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
9132 cxx_incomplete_type_error (instance, basetype);
9133 else if (optype)
9134 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
9135 basetype, optype, build_tree_list_vec (user_args),
9136 TREE_TYPE (instance));
9137 else
9139 tree arglist = build_tree_list_vec (user_args);
9140 tree errname = name;
9141 bool twiddle = false;
9142 if (IDENTIFIER_CDTOR_P (errname))
9144 twiddle = IDENTIFIER_DTOR_P (errname);
9145 errname = constructor_name (basetype);
9147 if (explicit_targs)
9148 errname = lookup_template_function (errname, explicit_targs);
9149 if (skip_first_for_error)
9150 arglist = TREE_CHAIN (arglist);
9151 error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
9152 basetype, &"~"[!twiddle], errname, arglist,
9153 TREE_TYPE (instance));
9155 print_z_candidates (location_of (name), candidates);
9157 call = error_mark_node;
9159 else
9161 cand = tourney (candidates, complain);
9162 if (cand == 0)
9164 char *pretty_name;
9165 bool free_p;
9166 tree arglist;
9168 if (complain & tf_error)
9170 pretty_name = name_as_c_string (name, basetype, &free_p);
9171 arglist = build_tree_list_vec (user_args);
9172 if (skip_first_for_error)
9173 arglist = TREE_CHAIN (arglist);
9174 if (!any_strictly_viable (candidates))
9175 error ("no matching function for call to %<%s(%A)%>",
9176 pretty_name, arglist);
9177 else
9178 error ("call of overloaded %<%s(%A)%> is ambiguous",
9179 pretty_name, arglist);
9180 print_z_candidates (location_of (name), candidates);
9181 if (free_p)
9182 free (pretty_name);
9184 call = error_mark_node;
9186 else
9188 fn = cand->fn;
9189 call = NULL_TREE;
9191 if (!(flags & LOOKUP_NONVIRTUAL)
9192 && DECL_PURE_VIRTUAL_P (fn)
9193 && instance == current_class_ref
9194 && (complain & tf_warning))
9196 /* This is not an error, it is runtime undefined
9197 behavior. */
9198 if (!current_function_decl)
9199 warning (0, "pure virtual %q#D called from "
9200 "non-static data member initializer", fn);
9201 else if (DECL_CONSTRUCTOR_P (current_function_decl)
9202 || DECL_DESTRUCTOR_P (current_function_decl))
9203 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
9204 ? G_("pure virtual %q#D called from constructor")
9205 : G_("pure virtual %q#D called from destructor")),
9206 fn);
9209 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
9210 && !DECL_CONSTRUCTOR_P (fn)
9211 && is_dummy_object (instance))
9213 instance = maybe_resolve_dummy (instance, true);
9214 if (instance == error_mark_node)
9215 call = error_mark_node;
9216 else if (!is_dummy_object (instance))
9218 /* We captured 'this' in the current lambda now that
9219 we know we really need it. */
9220 cand->first_arg = instance;
9222 else if (any_dependent_bases_p ())
9223 /* We can't tell until instantiation time whether we can use
9224 *this as the implicit object argument. */;
9225 else
9227 if (complain & tf_error)
9228 error ("cannot call member function %qD without object",
9229 fn);
9230 call = error_mark_node;
9234 if (call != error_mark_node)
9236 /* Optimize away vtable lookup if we know that this
9237 function can't be overridden. We need to check if
9238 the context and the type where we found fn are the same,
9239 actually FN might be defined in a different class
9240 type because of a using-declaration. In this case, we
9241 do not want to perform a non-virtual call. */
9242 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
9243 && same_type_ignoring_top_level_qualifiers_p
9244 (DECL_CONTEXT (fn), BINFO_TYPE (binfo))
9245 && resolves_to_fixed_type_p (instance, 0))
9246 flags |= LOOKUP_NONVIRTUAL;
9247 if (explicit_targs)
9248 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
9249 /* Now we know what function is being called. */
9250 if (fn_p)
9251 *fn_p = fn;
9252 /* Build the actual CALL_EXPR. */
9253 call = build_over_call (cand, flags, complain);
9254 /* In an expression of the form `a->f()' where `f' turns
9255 out to be a static member function, `a' is
9256 none-the-less evaluated. */
9257 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
9258 && !is_dummy_object (instance)
9259 && TREE_SIDE_EFFECTS (instance))
9260 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
9261 instance, call);
9262 else if (call != error_mark_node
9263 && DECL_DESTRUCTOR_P (cand->fn)
9264 && !VOID_TYPE_P (TREE_TYPE (call)))
9265 /* An explicit call of the form "x->~X()" has type
9266 "void". However, on platforms where destructors
9267 return "this" (i.e., those where
9268 targetm.cxx.cdtor_returns_this is true), such calls
9269 will appear to have a return value of pointer type
9270 to the low-level call machinery. We do not want to
9271 change the low-level machinery, since we want to be
9272 able to optimize "delete f()" on such platforms as
9273 "operator delete(~X(f()))" (rather than generating
9274 "t = f(), ~X(t), operator delete (t)"). */
9275 call = build_nop (void_type_node, call);
9280 if (processing_template_decl && call != error_mark_node)
9282 bool cast_to_void = false;
9284 if (TREE_CODE (call) == COMPOUND_EXPR)
9285 call = TREE_OPERAND (call, 1);
9286 else if (TREE_CODE (call) == NOP_EXPR)
9288 cast_to_void = true;
9289 call = TREE_OPERAND (call, 0);
9291 if (INDIRECT_REF_P (call))
9292 call = TREE_OPERAND (call, 0);
9293 call = (build_min_non_dep_call_vec
9294 (call,
9295 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
9296 orig_instance, orig_fns, NULL_TREE),
9297 orig_args));
9298 SET_EXPR_LOCATION (call, input_location);
9299 call = convert_from_reference (call);
9300 if (cast_to_void)
9301 call = build_nop (void_type_node, call);
9304 /* Free all the conversions we allocated. */
9305 obstack_free (&conversion_obstack, p);
9307 if (orig_args != NULL)
9308 release_tree_vector (orig_args);
9310 return call;
9313 /* Wrapper for above. */
9315 tree
9316 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
9317 tree conversion_path, int flags,
9318 tree *fn_p, tsubst_flags_t complain)
9320 tree ret;
9321 bool subtime = timevar_cond_start (TV_OVERLOAD);
9322 ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
9323 fn_p, complain);
9324 timevar_cond_stop (TV_OVERLOAD, subtime);
9325 return ret;
9328 /* Returns true iff standard conversion sequence ICS1 is a proper
9329 subsequence of ICS2. */
9331 static bool
9332 is_subseq (conversion *ics1, conversion *ics2)
9334 /* We can assume that a conversion of the same code
9335 between the same types indicates a subsequence since we only get
9336 here if the types we are converting from are the same. */
9338 while (ics1->kind == ck_rvalue
9339 || ics1->kind == ck_lvalue)
9340 ics1 = next_conversion (ics1);
9342 while (1)
9344 while (ics2->kind == ck_rvalue
9345 || ics2->kind == ck_lvalue)
9346 ics2 = next_conversion (ics2);
9348 if (ics2->kind == ck_user
9349 || ics2->kind == ck_ambig
9350 || ics2->kind == ck_aggr
9351 || ics2->kind == ck_list
9352 || ics2->kind == ck_identity)
9353 /* At this point, ICS1 cannot be a proper subsequence of
9354 ICS2. We can get a USER_CONV when we are comparing the
9355 second standard conversion sequence of two user conversion
9356 sequences. */
9357 return false;
9359 ics2 = next_conversion (ics2);
9361 while (ics2->kind == ck_rvalue
9362 || ics2->kind == ck_lvalue)
9363 ics2 = next_conversion (ics2);
9365 if (ics2->kind == ics1->kind
9366 && same_type_p (ics2->type, ics1->type)
9367 && (ics1->kind == ck_identity
9368 || same_type_p (next_conversion (ics2)->type,
9369 next_conversion (ics1)->type)))
9370 return true;
9374 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
9375 be any _TYPE nodes. */
9377 bool
9378 is_properly_derived_from (tree derived, tree base)
9380 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
9381 return false;
9383 /* We only allow proper derivation here. The DERIVED_FROM_P macro
9384 considers every class derived from itself. */
9385 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
9386 && DERIVED_FROM_P (base, derived));
9389 /* We build the ICS for an implicit object parameter as a pointer
9390 conversion sequence. However, such a sequence should be compared
9391 as if it were a reference conversion sequence. If ICS is the
9392 implicit conversion sequence for an implicit object parameter,
9393 modify it accordingly. */
9395 static void
9396 maybe_handle_implicit_object (conversion **ics)
9398 if ((*ics)->this_p)
9400 /* [over.match.funcs]
9402 For non-static member functions, the type of the
9403 implicit object parameter is "reference to cv X"
9404 where X is the class of which the function is a
9405 member and cv is the cv-qualification on the member
9406 function declaration. */
9407 conversion *t = *ics;
9408 tree reference_type;
9410 /* The `this' parameter is a pointer to a class type. Make the
9411 implicit conversion talk about a reference to that same class
9412 type. */
9413 reference_type = TREE_TYPE (t->type);
9414 reference_type = build_reference_type (reference_type);
9416 if (t->kind == ck_qual)
9417 t = next_conversion (t);
9418 if (t->kind == ck_ptr)
9419 t = next_conversion (t);
9420 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
9421 t = direct_reference_binding (reference_type, t);
9422 t->this_p = 1;
9423 t->rvaluedness_matches_p = 0;
9424 *ics = t;
9428 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
9429 and return the initial reference binding conversion. Otherwise,
9430 leave *ICS unchanged and return NULL. */
9432 static conversion *
9433 maybe_handle_ref_bind (conversion **ics)
9435 if ((*ics)->kind == ck_ref_bind)
9437 conversion *old_ics = *ics;
9438 *ics = next_conversion (old_ics);
9439 (*ics)->user_conv_p = old_ics->user_conv_p;
9440 return old_ics;
9443 return NULL;
9446 /* Compare two implicit conversion sequences according to the rules set out in
9447 [over.ics.rank]. Return values:
9449 1: ics1 is better than ics2
9450 -1: ics2 is better than ics1
9451 0: ics1 and ics2 are indistinguishable */
9453 static int
9454 compare_ics (conversion *ics1, conversion *ics2)
9456 tree from_type1;
9457 tree from_type2;
9458 tree to_type1;
9459 tree to_type2;
9460 tree deref_from_type1 = NULL_TREE;
9461 tree deref_from_type2 = NULL_TREE;
9462 tree deref_to_type1 = NULL_TREE;
9463 tree deref_to_type2 = NULL_TREE;
9464 conversion_rank rank1, rank2;
9466 /* REF_BINDING is nonzero if the result of the conversion sequence
9467 is a reference type. In that case REF_CONV is the reference
9468 binding conversion. */
9469 conversion *ref_conv1;
9470 conversion *ref_conv2;
9472 /* Compare badness before stripping the reference conversion. */
9473 if (ics1->bad_p > ics2->bad_p)
9474 return -1;
9475 else if (ics1->bad_p < ics2->bad_p)
9476 return 1;
9478 /* Handle implicit object parameters. */
9479 maybe_handle_implicit_object (&ics1);
9480 maybe_handle_implicit_object (&ics2);
9482 /* Handle reference parameters. */
9483 ref_conv1 = maybe_handle_ref_bind (&ics1);
9484 ref_conv2 = maybe_handle_ref_bind (&ics2);
9486 /* List-initialization sequence L1 is a better conversion sequence than
9487 list-initialization sequence L2 if L1 converts to
9488 std::initializer_list<X> for some X and L2 does not. */
9489 if (ics1->kind == ck_list && ics2->kind != ck_list)
9490 return 1;
9491 if (ics2->kind == ck_list && ics1->kind != ck_list)
9492 return -1;
9494 /* [over.ics.rank]
9496 When comparing the basic forms of implicit conversion sequences (as
9497 defined in _over.best.ics_)
9499 --a standard conversion sequence (_over.ics.scs_) is a better
9500 conversion sequence than a user-defined conversion sequence
9501 or an ellipsis conversion sequence, and
9503 --a user-defined conversion sequence (_over.ics.user_) is a
9504 better conversion sequence than an ellipsis conversion sequence
9505 (_over.ics.ellipsis_). */
9506 /* Use BAD_CONVERSION_RANK because we already checked for a badness
9507 mismatch. If both ICS are bad, we try to make a decision based on
9508 what would have happened if they'd been good. This is not an
9509 extension, we'll still give an error when we build up the call; this
9510 just helps us give a more helpful error message. */
9511 rank1 = BAD_CONVERSION_RANK (ics1);
9512 rank2 = BAD_CONVERSION_RANK (ics2);
9514 if (rank1 > rank2)
9515 return -1;
9516 else if (rank1 < rank2)
9517 return 1;
9519 if (ics1->ellipsis_p)
9520 /* Both conversions are ellipsis conversions. */
9521 return 0;
9523 /* User-defined conversion sequence U1 is a better conversion sequence
9524 than another user-defined conversion sequence U2 if they contain the
9525 same user-defined conversion operator or constructor and if the sec-
9526 ond standard conversion sequence of U1 is better than the second
9527 standard conversion sequence of U2. */
9529 /* Handle list-conversion with the same code even though it isn't always
9530 ranked as a user-defined conversion and it doesn't have a second
9531 standard conversion sequence; it will still have the desired effect.
9532 Specifically, we need to do the reference binding comparison at the
9533 end of this function. */
9535 if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
9537 conversion *t1;
9538 conversion *t2;
9540 for (t1 = ics1; t1->kind != ck_user; t1 = next_conversion (t1))
9541 if (t1->kind == ck_ambig || t1->kind == ck_aggr
9542 || t1->kind == ck_list)
9543 break;
9544 for (t2 = ics2; t2->kind != ck_user; t2 = next_conversion (t2))
9545 if (t2->kind == ck_ambig || t2->kind == ck_aggr
9546 || t2->kind == ck_list)
9547 break;
9549 if (t1->kind != t2->kind)
9550 return 0;
9551 else if (t1->kind == ck_user)
9553 tree f1 = t1->cand ? t1->cand->fn : t1->type;
9554 tree f2 = t2->cand ? t2->cand->fn : t2->type;
9555 if (f1 != f2)
9556 return 0;
9558 else
9560 /* For ambiguous or aggregate conversions, use the target type as
9561 a proxy for the conversion function. */
9562 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
9563 return 0;
9566 /* We can just fall through here, after setting up
9567 FROM_TYPE1 and FROM_TYPE2. */
9568 from_type1 = t1->type;
9569 from_type2 = t2->type;
9571 else
9573 conversion *t1;
9574 conversion *t2;
9576 /* We're dealing with two standard conversion sequences.
9578 [over.ics.rank]
9580 Standard conversion sequence S1 is a better conversion
9581 sequence than standard conversion sequence S2 if
9583 --S1 is a proper subsequence of S2 (comparing the conversion
9584 sequences in the canonical form defined by _over.ics.scs_,
9585 excluding any Lvalue Transformation; the identity
9586 conversion sequence is considered to be a subsequence of
9587 any non-identity conversion sequence */
9589 t1 = ics1;
9590 while (t1->kind != ck_identity)
9591 t1 = next_conversion (t1);
9592 from_type1 = t1->type;
9594 t2 = ics2;
9595 while (t2->kind != ck_identity)
9596 t2 = next_conversion (t2);
9597 from_type2 = t2->type;
9600 /* One sequence can only be a subsequence of the other if they start with
9601 the same type. They can start with different types when comparing the
9602 second standard conversion sequence in two user-defined conversion
9603 sequences. */
9604 if (same_type_p (from_type1, from_type2))
9606 if (is_subseq (ics1, ics2))
9607 return 1;
9608 if (is_subseq (ics2, ics1))
9609 return -1;
9612 /* [over.ics.rank]
9614 Or, if not that,
9616 --the rank of S1 is better than the rank of S2 (by the rules
9617 defined below):
9619 Standard conversion sequences are ordered by their ranks: an Exact
9620 Match is a better conversion than a Promotion, which is a better
9621 conversion than a Conversion.
9623 Two conversion sequences with the same rank are indistinguishable
9624 unless one of the following rules applies:
9626 --A conversion that does not a convert a pointer, pointer to member,
9627 or std::nullptr_t to bool is better than one that does.
9629 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
9630 so that we do not have to check it explicitly. */
9631 if (ics1->rank < ics2->rank)
9632 return 1;
9633 else if (ics2->rank < ics1->rank)
9634 return -1;
9636 to_type1 = ics1->type;
9637 to_type2 = ics2->type;
9639 /* A conversion from scalar arithmetic type to complex is worse than a
9640 conversion between scalar arithmetic types. */
9641 if (same_type_p (from_type1, from_type2)
9642 && ARITHMETIC_TYPE_P (from_type1)
9643 && ARITHMETIC_TYPE_P (to_type1)
9644 && ARITHMETIC_TYPE_P (to_type2)
9645 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
9646 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
9648 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
9649 return -1;
9650 else
9651 return 1;
9654 if (TYPE_PTR_P (from_type1)
9655 && TYPE_PTR_P (from_type2)
9656 && TYPE_PTR_P (to_type1)
9657 && TYPE_PTR_P (to_type2))
9659 deref_from_type1 = TREE_TYPE (from_type1);
9660 deref_from_type2 = TREE_TYPE (from_type2);
9661 deref_to_type1 = TREE_TYPE (to_type1);
9662 deref_to_type2 = TREE_TYPE (to_type2);
9664 /* The rules for pointers to members A::* are just like the rules
9665 for pointers A*, except opposite: if B is derived from A then
9666 A::* converts to B::*, not vice versa. For that reason, we
9667 switch the from_ and to_ variables here. */
9668 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
9669 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
9670 || (TYPE_PTRMEMFUNC_P (from_type1)
9671 && TYPE_PTRMEMFUNC_P (from_type2)
9672 && TYPE_PTRMEMFUNC_P (to_type1)
9673 && TYPE_PTRMEMFUNC_P (to_type2)))
9675 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
9676 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
9677 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
9678 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
9681 if (deref_from_type1 != NULL_TREE
9682 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
9683 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
9685 /* This was one of the pointer or pointer-like conversions.
9687 [over.ics.rank]
9689 --If class B is derived directly or indirectly from class A,
9690 conversion of B* to A* is better than conversion of B* to
9691 void*, and conversion of A* to void* is better than
9692 conversion of B* to void*. */
9693 if (VOID_TYPE_P (deref_to_type1)
9694 && VOID_TYPE_P (deref_to_type2))
9696 if (is_properly_derived_from (deref_from_type1,
9697 deref_from_type2))
9698 return -1;
9699 else if (is_properly_derived_from (deref_from_type2,
9700 deref_from_type1))
9701 return 1;
9703 else if (VOID_TYPE_P (deref_to_type1)
9704 || VOID_TYPE_P (deref_to_type2))
9706 if (same_type_p (deref_from_type1, deref_from_type2))
9708 if (VOID_TYPE_P (deref_to_type2))
9710 if (is_properly_derived_from (deref_from_type1,
9711 deref_to_type1))
9712 return 1;
9714 /* We know that DEREF_TO_TYPE1 is `void' here. */
9715 else if (is_properly_derived_from (deref_from_type1,
9716 deref_to_type2))
9717 return -1;
9720 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
9721 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
9723 /* [over.ics.rank]
9725 --If class B is derived directly or indirectly from class A
9726 and class C is derived directly or indirectly from B,
9728 --conversion of C* to B* is better than conversion of C* to
9731 --conversion of B* to A* is better than conversion of C* to
9732 A* */
9733 if (same_type_p (deref_from_type1, deref_from_type2))
9735 if (is_properly_derived_from (deref_to_type1,
9736 deref_to_type2))
9737 return 1;
9738 else if (is_properly_derived_from (deref_to_type2,
9739 deref_to_type1))
9740 return -1;
9742 else if (same_type_p (deref_to_type1, deref_to_type2))
9744 if (is_properly_derived_from (deref_from_type2,
9745 deref_from_type1))
9746 return 1;
9747 else if (is_properly_derived_from (deref_from_type1,
9748 deref_from_type2))
9749 return -1;
9753 else if (CLASS_TYPE_P (non_reference (from_type1))
9754 && same_type_p (from_type1, from_type2))
9756 tree from = non_reference (from_type1);
9758 /* [over.ics.rank]
9760 --binding of an expression of type C to a reference of type
9761 B& is better than binding an expression of type C to a
9762 reference of type A&
9764 --conversion of C to B is better than conversion of C to A, */
9765 if (is_properly_derived_from (from, to_type1)
9766 && is_properly_derived_from (from, to_type2))
9768 if (is_properly_derived_from (to_type1, to_type2))
9769 return 1;
9770 else if (is_properly_derived_from (to_type2, to_type1))
9771 return -1;
9774 else if (CLASS_TYPE_P (non_reference (to_type1))
9775 && same_type_p (to_type1, to_type2))
9777 tree to = non_reference (to_type1);
9779 /* [over.ics.rank]
9781 --binding of an expression of type B to a reference of type
9782 A& is better than binding an expression of type C to a
9783 reference of type A&,
9785 --conversion of B to A is better than conversion of C to A */
9786 if (is_properly_derived_from (from_type1, to)
9787 && is_properly_derived_from (from_type2, to))
9789 if (is_properly_derived_from (from_type2, from_type1))
9790 return 1;
9791 else if (is_properly_derived_from (from_type1, from_type2))
9792 return -1;
9796 /* [over.ics.rank]
9798 --S1 and S2 differ only in their qualification conversion and yield
9799 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
9800 qualification signature of type T1 is a proper subset of the cv-
9801 qualification signature of type T2 */
9802 if (ics1->kind == ck_qual
9803 && ics2->kind == ck_qual
9804 && same_type_p (from_type1, from_type2))
9806 int result = comp_cv_qual_signature (to_type1, to_type2);
9807 if (result != 0)
9808 return result;
9811 /* [over.ics.rank]
9813 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
9814 to an implicit object parameter of a non-static member function
9815 declared without a ref-qualifier, and either S1 binds an lvalue
9816 reference to an lvalue and S2 binds an rvalue reference or S1 binds an
9817 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
9818 draft standard, 13.3.3.2)
9820 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
9821 types to which the references refer are the same type except for
9822 top-level cv-qualifiers, and the type to which the reference
9823 initialized by S2 refers is more cv-qualified than the type to
9824 which the reference initialized by S1 refers.
9826 DR 1328 [over.match.best]: the context is an initialization by
9827 conversion function for direct reference binding (13.3.1.6) of a
9828 reference to function type, the return type of F1 is the same kind of
9829 reference (i.e. lvalue or rvalue) as the reference being initialized,
9830 and the return type of F2 is not. */
9832 if (ref_conv1 && ref_conv2)
9834 if (!ref_conv1->this_p && !ref_conv2->this_p
9835 && (ref_conv1->rvaluedness_matches_p
9836 != ref_conv2->rvaluedness_matches_p)
9837 && (same_type_p (ref_conv1->type, ref_conv2->type)
9838 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
9839 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
9841 if (ref_conv1->bad_p
9842 && !same_type_p (TREE_TYPE (ref_conv1->type),
9843 TREE_TYPE (ref_conv2->type)))
9844 /* Don't prefer a bad conversion that drops cv-quals to a bad
9845 conversion with the wrong rvalueness. */
9846 return 0;
9847 return (ref_conv1->rvaluedness_matches_p
9848 - ref_conv2->rvaluedness_matches_p);
9851 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
9853 int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
9854 int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
9855 if (ref_conv1->bad_p)
9857 /* Prefer the one that drops fewer cv-quals. */
9858 tree ftype = next_conversion (ref_conv1)->type;
9859 int fquals = cp_type_quals (ftype);
9860 q1 ^= fquals;
9861 q2 ^= fquals;
9863 return comp_cv_qualification (q2, q1);
9867 /* Neither conversion sequence is better than the other. */
9868 return 0;
9871 /* The source type for this standard conversion sequence. */
9873 static tree
9874 source_type (conversion *t)
9876 for (;; t = next_conversion (t))
9878 if (t->kind == ck_user
9879 || t->kind == ck_ambig
9880 || t->kind == ck_identity)
9881 return t->type;
9883 gcc_unreachable ();
9886 /* Note a warning about preferring WINNER to LOSER. We do this by storing
9887 a pointer to LOSER and re-running joust to produce the warning if WINNER
9888 is actually used. */
9890 static void
9891 add_warning (struct z_candidate *winner, struct z_candidate *loser)
9893 candidate_warning *cw = (candidate_warning *)
9894 conversion_obstack_alloc (sizeof (candidate_warning));
9895 cw->loser = loser;
9896 cw->next = winner->warnings;
9897 winner->warnings = cw;
9900 /* Compare two candidates for overloading as described in
9901 [over.match.best]. Return values:
9903 1: cand1 is better than cand2
9904 -1: cand2 is better than cand1
9905 0: cand1 and cand2 are indistinguishable */
9907 static int
9908 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
9909 tsubst_flags_t complain)
9911 int winner = 0;
9912 int off1 = 0, off2 = 0;
9913 size_t i;
9914 size_t len;
9916 /* Candidates that involve bad conversions are always worse than those
9917 that don't. */
9918 if (cand1->viable > cand2->viable)
9919 return 1;
9920 if (cand1->viable < cand2->viable)
9921 return -1;
9923 /* If we have two pseudo-candidates for conversions to the same type,
9924 or two candidates for the same function, arbitrarily pick one. */
9925 if (cand1->fn == cand2->fn
9926 && (IS_TYPE_OR_DECL_P (cand1->fn)))
9927 return 1;
9929 /* Prefer a non-deleted function over an implicitly deleted move
9930 constructor or assignment operator. This differs slightly from the
9931 wording for issue 1402 (which says the move op is ignored by overload
9932 resolution), but this way produces better error messages. */
9933 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
9934 && TREE_CODE (cand2->fn) == FUNCTION_DECL
9935 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
9937 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
9938 && move_fn_p (cand1->fn))
9939 return -1;
9940 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
9941 && move_fn_p (cand2->fn))
9942 return 1;
9945 /* a viable function F1
9946 is defined to be a better function than another viable function F2 if
9947 for all arguments i, ICSi(F1) is not a worse conversion sequence than
9948 ICSi(F2), and then */
9950 /* for some argument j, ICSj(F1) is a better conversion sequence than
9951 ICSj(F2) */
9953 /* For comparing static and non-static member functions, we ignore
9954 the implicit object parameter of the non-static function. The
9955 standard says to pretend that the static function has an object
9956 parm, but that won't work with operator overloading. */
9957 len = cand1->num_convs;
9958 if (len != cand2->num_convs)
9960 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
9961 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
9963 if (DECL_CONSTRUCTOR_P (cand1->fn)
9964 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
9965 /* We're comparing a near-match list constructor and a near-match
9966 non-list constructor. Just treat them as unordered. */
9967 return 0;
9969 gcc_assert (static_1 != static_2);
9971 if (static_1)
9972 off2 = 1;
9973 else
9975 off1 = 1;
9976 --len;
9980 for (i = 0; i < len; ++i)
9982 conversion *t1 = cand1->convs[i + off1];
9983 conversion *t2 = cand2->convs[i + off2];
9984 int comp = compare_ics (t1, t2);
9986 if (comp != 0)
9988 if ((complain & tf_warning)
9989 && warn_sign_promo
9990 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
9991 == cr_std + cr_promotion)
9992 && t1->kind == ck_std
9993 && t2->kind == ck_std
9994 && TREE_CODE (t1->type) == INTEGER_TYPE
9995 && TREE_CODE (t2->type) == INTEGER_TYPE
9996 && (TYPE_PRECISION (t1->type)
9997 == TYPE_PRECISION (t2->type))
9998 && (TYPE_UNSIGNED (next_conversion (t1)->type)
9999 || (TREE_CODE (next_conversion (t1)->type)
10000 == ENUMERAL_TYPE)))
10002 tree type = next_conversion (t1)->type;
10003 tree type1, type2;
10004 struct z_candidate *w, *l;
10005 if (comp > 0)
10006 type1 = t1->type, type2 = t2->type,
10007 w = cand1, l = cand2;
10008 else
10009 type1 = t2->type, type2 = t1->type,
10010 w = cand2, l = cand1;
10012 if (warn)
10014 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
10015 type, type1, type2);
10016 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
10018 else
10019 add_warning (w, l);
10022 if (winner && comp != winner)
10024 winner = 0;
10025 goto tweak;
10027 winner = comp;
10031 /* warn about confusing overload resolution for user-defined conversions,
10032 either between a constructor and a conversion op, or between two
10033 conversion ops. */
10034 if ((complain & tf_warning)
10035 && winner && warn_conversion && cand1->second_conv
10036 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
10037 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
10039 struct z_candidate *w, *l;
10040 bool give_warning = false;
10042 if (winner == 1)
10043 w = cand1, l = cand2;
10044 else
10045 w = cand2, l = cand1;
10047 /* We don't want to complain about `X::operator T1 ()'
10048 beating `X::operator T2 () const', when T2 is a no less
10049 cv-qualified version of T1. */
10050 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
10051 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
10053 tree t = TREE_TYPE (TREE_TYPE (l->fn));
10054 tree f = TREE_TYPE (TREE_TYPE (w->fn));
10056 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
10058 t = TREE_TYPE (t);
10059 f = TREE_TYPE (f);
10061 if (!comp_ptr_ttypes (t, f))
10062 give_warning = true;
10064 else
10065 give_warning = true;
10067 if (!give_warning)
10068 /*NOP*/;
10069 else if (warn)
10071 tree source = source_type (w->convs[0]);
10072 if (! DECL_CONSTRUCTOR_P (w->fn))
10073 source = TREE_TYPE (source);
10074 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
10075 && warning (OPT_Wconversion, " for conversion from %qH to %qI",
10076 source, w->second_conv->type))
10078 inform (input_location, " because conversion sequence for the argument is better");
10081 else
10082 add_warning (w, l);
10085 if (winner)
10086 return winner;
10088 /* DR 495 moved this tiebreaker above the template ones. */
10089 /* or, if not that,
10090 the context is an initialization by user-defined conversion (see
10091 _dcl.init_ and _over.match.user_) and the standard conversion
10092 sequence from the return type of F1 to the destination type (i.e.,
10093 the type of the entity being initialized) is a better conversion
10094 sequence than the standard conversion sequence from the return type
10095 of F2 to the destination type. */
10097 if (cand1->second_conv)
10099 winner = compare_ics (cand1->second_conv, cand2->second_conv);
10100 if (winner)
10101 return winner;
10104 /* or, if not that,
10105 F1 is a non-template function and F2 is a template function
10106 specialization. */
10108 if (!cand1->template_decl && cand2->template_decl)
10109 return 1;
10110 else if (cand1->template_decl && !cand2->template_decl)
10111 return -1;
10113 /* or, if not that,
10114 F1 and F2 are template functions and the function template for F1 is
10115 more specialized than the template for F2 according to the partial
10116 ordering rules. */
10118 if (cand1->template_decl && cand2->template_decl)
10120 winner = more_specialized_fn
10121 (TI_TEMPLATE (cand1->template_decl),
10122 TI_TEMPLATE (cand2->template_decl),
10123 /* [temp.func.order]: The presence of unused ellipsis and default
10124 arguments has no effect on the partial ordering of function
10125 templates. add_function_candidate() will not have
10126 counted the "this" argument for constructors. */
10127 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
10128 if (winner)
10129 return winner;
10132 // C++ Concepts
10133 // or, if not that, F1 is more constrained than F2.
10134 if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn))
10136 winner = more_constrained (cand1->fn, cand2->fn);
10137 if (winner)
10138 return winner;
10141 /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
10142 if (deduction_guide_p (cand1->fn))
10144 gcc_assert (deduction_guide_p (cand2->fn));
10145 /* We distinguish between candidates from an explicit deduction guide and
10146 candidates built from a constructor based on DECL_ARTIFICIAL. */
10147 int art1 = DECL_ARTIFICIAL (cand1->fn);
10148 int art2 = DECL_ARTIFICIAL (cand2->fn);
10149 if (art1 != art2)
10150 return art2 - art1;
10152 if (art1)
10154 /* Prefer the special copy guide over a declared copy/move
10155 constructor. */
10156 if (copy_guide_p (cand1->fn))
10157 return 1;
10158 if (copy_guide_p (cand2->fn))
10159 return -1;
10161 /* Prefer a candidate generated from a non-template constructor. */
10162 int tg1 = template_guide_p (cand1->fn);
10163 int tg2 = template_guide_p (cand2->fn);
10164 if (tg1 != tg2)
10165 return tg2 - tg1;
10169 /* F1 is a member of a class D, F2 is a member of a base class B of D, and
10170 for all arguments the corresponding parameters of F1 and F2 have the same
10171 type (CWG 2273/2277). */
10172 if (DECL_P (cand1->fn) && DECL_CLASS_SCOPE_P (cand1->fn)
10173 && !DECL_CONV_FN_P (cand1->fn)
10174 && DECL_P (cand2->fn) && DECL_CLASS_SCOPE_P (cand2->fn)
10175 && !DECL_CONV_FN_P (cand2->fn))
10177 tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
10178 tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
10180 bool used1 = false;
10181 bool used2 = false;
10182 if (base1 == base2)
10183 /* No difference. */;
10184 else if (DERIVED_FROM_P (base1, base2))
10185 used1 = true;
10186 else if (DERIVED_FROM_P (base2, base1))
10187 used2 = true;
10189 if (int diff = used2 - used1)
10191 for (i = 0; i < len; ++i)
10193 conversion *t1 = cand1->convs[i + off1];
10194 conversion *t2 = cand2->convs[i + off2];
10195 if (!same_type_p (t1->type, t2->type))
10196 break;
10198 if (i == len)
10199 return diff;
10203 /* Check whether we can discard a builtin candidate, either because we
10204 have two identical ones or matching builtin and non-builtin candidates.
10206 (Pedantically in the latter case the builtin which matched the user
10207 function should not be added to the overload set, but we spot it here.
10209 [over.match.oper]
10210 ... the builtin candidates include ...
10211 - do not have the same parameter type list as any non-template
10212 non-member candidate. */
10214 if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
10216 for (i = 0; i < len; ++i)
10217 if (!same_type_p (cand1->convs[i]->type,
10218 cand2->convs[i]->type))
10219 break;
10220 if (i == cand1->num_convs)
10222 if (cand1->fn == cand2->fn)
10223 /* Two built-in candidates; arbitrarily pick one. */
10224 return 1;
10225 else if (identifier_p (cand1->fn))
10226 /* cand1 is built-in; prefer cand2. */
10227 return -1;
10228 else
10229 /* cand2 is built-in; prefer cand1. */
10230 return 1;
10234 /* For candidates of a multi-versioned function, make the version with
10235 the highest priority win. This version will be checked for dispatching
10236 first. If this version can be inlined into the caller, the front-end
10237 will simply make a direct call to this function. */
10239 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
10240 && DECL_FUNCTION_VERSIONED (cand1->fn)
10241 && TREE_CODE (cand2->fn) == FUNCTION_DECL
10242 && DECL_FUNCTION_VERSIONED (cand2->fn))
10244 tree f1 = TREE_TYPE (cand1->fn);
10245 tree f2 = TREE_TYPE (cand2->fn);
10246 tree p1 = TYPE_ARG_TYPES (f1);
10247 tree p2 = TYPE_ARG_TYPES (f2);
10249 /* Check if cand1->fn and cand2->fn are versions of the same function. It
10250 is possible that cand1->fn and cand2->fn are function versions but of
10251 different functions. Check types to see if they are versions of the same
10252 function. */
10253 if (compparms (p1, p2)
10254 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
10256 /* Always make the version with the higher priority, more
10257 specialized, win. */
10258 gcc_assert (targetm.compare_version_priority);
10259 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
10260 return 1;
10261 else
10262 return -1;
10266 /* If the two function declarations represent the same function (this can
10267 happen with declarations in multiple scopes and arg-dependent lookup),
10268 arbitrarily choose one. But first make sure the default args we're
10269 using match. */
10270 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
10271 && equal_functions (cand1->fn, cand2->fn))
10273 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
10274 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
10276 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
10278 for (i = 0; i < len; ++i)
10280 /* Don't crash if the fn is variadic. */
10281 if (!parms1)
10282 break;
10283 parms1 = TREE_CHAIN (parms1);
10284 parms2 = TREE_CHAIN (parms2);
10287 if (off1)
10288 parms1 = TREE_CHAIN (parms1);
10289 else if (off2)
10290 parms2 = TREE_CHAIN (parms2);
10292 for (; parms1; ++i)
10294 if (!cp_tree_equal (TREE_PURPOSE (parms1),
10295 TREE_PURPOSE (parms2)))
10297 if (warn)
10299 if (complain & tf_error)
10301 if (permerror (input_location,
10302 "default argument mismatch in "
10303 "overload resolution"))
10305 inform (DECL_SOURCE_LOCATION (cand1->fn),
10306 " candidate 1: %q#F", cand1->fn);
10307 inform (DECL_SOURCE_LOCATION (cand2->fn),
10308 " candidate 2: %q#F", cand2->fn);
10311 else
10312 return 0;
10314 else
10315 add_warning (cand1, cand2);
10316 break;
10318 parms1 = TREE_CHAIN (parms1);
10319 parms2 = TREE_CHAIN (parms2);
10322 return 1;
10325 tweak:
10327 /* Extension: If the worst conversion for one candidate is worse than the
10328 worst conversion for the other, take the first. */
10329 if (!pedantic && (complain & tf_warning_or_error))
10331 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
10332 struct z_candidate *w = 0, *l = 0;
10334 for (i = 0; i < len; ++i)
10336 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
10337 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
10338 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
10339 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
10341 if (rank1 < rank2)
10342 winner = 1, w = cand1, l = cand2;
10343 if (rank1 > rank2)
10344 winner = -1, w = cand2, l = cand1;
10345 if (winner)
10347 /* Don't choose a deleted function over ambiguity. */
10348 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
10349 return 0;
10350 if (warn)
10352 pedwarn (input_location, 0,
10353 "ISO C++ says that these are ambiguous, even "
10354 "though the worst conversion for the first is better than "
10355 "the worst conversion for the second:");
10356 print_z_candidate (input_location, _("candidate 1:"), w);
10357 print_z_candidate (input_location, _("candidate 2:"), l);
10359 else
10360 add_warning (w, l);
10361 return winner;
10365 gcc_assert (!winner);
10366 return 0;
10369 /* Given a list of candidates for overloading, find the best one, if any.
10370 This algorithm has a worst case of O(2n) (winner is last), and a best
10371 case of O(n/2) (totally ambiguous); much better than a sorting
10372 algorithm. */
10374 static struct z_candidate *
10375 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
10377 struct z_candidate *champ = candidates, *challenger;
10378 int fate;
10379 int champ_compared_to_predecessor = 0;
10381 /* Walk through the list once, comparing each current champ to the next
10382 candidate, knocking out a candidate or two with each comparison. */
10384 for (challenger = champ->next; challenger; )
10386 fate = joust (champ, challenger, 0, complain);
10387 if (fate == 1)
10388 challenger = challenger->next;
10389 else
10391 if (fate == 0)
10393 champ = challenger->next;
10394 if (champ == 0)
10395 return NULL;
10396 champ_compared_to_predecessor = 0;
10398 else
10400 champ = challenger;
10401 champ_compared_to_predecessor = 1;
10404 challenger = champ->next;
10408 /* Make sure the champ is better than all the candidates it hasn't yet
10409 been compared to. */
10411 for (challenger = candidates;
10412 challenger != champ
10413 && !(champ_compared_to_predecessor && challenger->next == champ);
10414 challenger = challenger->next)
10416 fate = joust (champ, challenger, 0, complain);
10417 if (fate != 1)
10418 return NULL;
10421 return champ;
10424 /* Returns nonzero if things of type FROM can be converted to TO. */
10426 bool
10427 can_convert (tree to, tree from, tsubst_flags_t complain)
10429 tree arg = NULL_TREE;
10430 /* implicit_conversion only considers user-defined conversions
10431 if it has an expression for the call argument list. */
10432 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
10433 arg = build1 (CAST_EXPR, from, NULL_TREE);
10434 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
10437 /* Returns nonzero if things of type FROM can be converted to TO with a
10438 standard conversion. */
10440 bool
10441 can_convert_standard (tree to, tree from, tsubst_flags_t complain)
10443 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
10446 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
10448 bool
10449 can_convert_arg (tree to, tree from, tree arg, int flags,
10450 tsubst_flags_t complain)
10452 conversion *t;
10453 void *p;
10454 bool ok_p;
10456 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10457 p = conversion_obstack_alloc (0);
10458 /* We want to discard any access checks done for this test,
10459 as we might not be in the appropriate access context and
10460 we'll do the check again when we actually perform the
10461 conversion. */
10462 push_deferring_access_checks (dk_deferred);
10464 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
10465 flags, complain);
10466 ok_p = (t && !t->bad_p);
10468 /* Discard the access checks now. */
10469 pop_deferring_access_checks ();
10470 /* Free all the conversions we allocated. */
10471 obstack_free (&conversion_obstack, p);
10473 return ok_p;
10476 /* Like can_convert_arg, but allows dubious conversions as well. */
10478 bool
10479 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
10480 tsubst_flags_t complain)
10482 conversion *t;
10483 void *p;
10485 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10486 p = conversion_obstack_alloc (0);
10487 /* Try to perform the conversion. */
10488 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
10489 flags, complain);
10490 /* Free all the conversions we allocated. */
10491 obstack_free (&conversion_obstack, p);
10493 return t != NULL;
10496 /* Convert EXPR to TYPE. Return the converted expression.
10498 Note that we allow bad conversions here because by the time we get to
10499 this point we are committed to doing the conversion. If we end up
10500 doing a bad conversion, convert_like will complain. */
10502 tree
10503 perform_implicit_conversion_flags (tree type, tree expr,
10504 tsubst_flags_t complain, int flags)
10506 conversion *conv;
10507 void *p;
10508 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
10510 if (error_operand_p (expr))
10511 return error_mark_node;
10513 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10514 p = conversion_obstack_alloc (0);
10516 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10517 /*c_cast_p=*/false,
10518 flags, complain);
10520 if (!conv)
10522 if (complain & tf_error)
10524 /* If expr has unknown type, then it is an overloaded function.
10525 Call instantiate_type to get good error messages. */
10526 if (TREE_TYPE (expr) == unknown_type_node)
10527 instantiate_type (type, expr, complain);
10528 else if (invalid_nonstatic_memfn_p (loc, expr, complain))
10529 /* We gave an error. */;
10530 else
10531 error_at (loc, "could not convert %qE from %qH to %qI", expr,
10532 TREE_TYPE (expr), type);
10534 expr = error_mark_node;
10536 else if (processing_template_decl && conv->kind != ck_identity)
10538 /* In a template, we are only concerned about determining the
10539 type of non-dependent expressions, so we do not have to
10540 perform the actual conversion. But for initializers, we
10541 need to be able to perform it at instantiation
10542 (or instantiate_non_dependent_expr) time. */
10543 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
10544 if (!(flags & LOOKUP_ONLYCONVERTING))
10545 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
10547 else
10548 expr = convert_like (conv, expr, complain);
10550 /* Free all the conversions we allocated. */
10551 obstack_free (&conversion_obstack, p);
10553 return expr;
10556 tree
10557 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
10559 return perform_implicit_conversion_flags (type, expr, complain,
10560 LOOKUP_IMPLICIT);
10563 /* Convert EXPR to TYPE (as a direct-initialization) if that is
10564 permitted. If the conversion is valid, the converted expression is
10565 returned. Otherwise, NULL_TREE is returned, except in the case
10566 that TYPE is a class type; in that case, an error is issued. If
10567 C_CAST_P is true, then this direct-initialization is taking
10568 place as part of a static_cast being attempted as part of a C-style
10569 cast. */
10571 tree
10572 perform_direct_initialization_if_possible (tree type,
10573 tree expr,
10574 bool c_cast_p,
10575 tsubst_flags_t complain)
10577 conversion *conv;
10578 void *p;
10580 if (type == error_mark_node || error_operand_p (expr))
10581 return error_mark_node;
10582 /* [dcl.init]
10584 If the destination type is a (possibly cv-qualified) class type:
10586 -- If the initialization is direct-initialization ...,
10587 constructors are considered. ... If no constructor applies, or
10588 the overload resolution is ambiguous, the initialization is
10589 ill-formed. */
10590 if (CLASS_TYPE_P (type))
10592 vec<tree, va_gc> *args = make_tree_vector_single (expr);
10593 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
10594 &args, type, LOOKUP_NORMAL, complain);
10595 release_tree_vector (args);
10596 return build_cplus_new (type, expr, complain);
10599 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10600 p = conversion_obstack_alloc (0);
10602 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10603 c_cast_p,
10604 LOOKUP_NORMAL, complain);
10605 if (!conv || conv->bad_p)
10606 expr = NULL_TREE;
10607 else if (processing_template_decl && conv->kind != ck_identity)
10609 /* In a template, we are only concerned about determining the
10610 type of non-dependent expressions, so we do not have to
10611 perform the actual conversion. But for initializers, we
10612 need to be able to perform it at instantiation
10613 (or instantiate_non_dependent_expr) time. */
10614 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
10615 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
10617 else
10618 expr = convert_like_real (conv, expr, NULL_TREE, 0,
10619 /*issue_conversion_warnings=*/false,
10620 c_cast_p,
10621 complain);
10623 /* Free all the conversions we allocated. */
10624 obstack_free (&conversion_obstack, p);
10626 return expr;
10629 /* When initializing a reference that lasts longer than a full-expression,
10630 this special rule applies:
10632 [class.temporary]
10634 The temporary to which the reference is bound or the temporary
10635 that is the complete object to which the reference is bound
10636 persists for the lifetime of the reference.
10638 The temporaries created during the evaluation of the expression
10639 initializing the reference, except the temporary to which the
10640 reference is bound, are destroyed at the end of the
10641 full-expression in which they are created.
10643 In that case, we store the converted expression into a new
10644 VAR_DECL in a new scope.
10646 However, we want to be careful not to create temporaries when
10647 they are not required. For example, given:
10649 struct B {};
10650 struct D : public B {};
10651 D f();
10652 const B& b = f();
10654 there is no need to copy the return value from "f"; we can just
10655 extend its lifetime. Similarly, given:
10657 struct S {};
10658 struct T { operator S(); };
10659 T t;
10660 const S& s = t;
10662 we can extend the lifetime of the return value of the conversion
10663 operator.
10665 The next several functions are involved in this lifetime extension. */
10667 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
10668 reference is being bound to a temporary. Create and return a new
10669 VAR_DECL with the indicated TYPE; this variable will store the value to
10670 which the reference is bound. */
10672 tree
10673 make_temporary_var_for_ref_to_temp (tree decl, tree type)
10675 tree var = create_temporary_var (type);
10677 /* Register the variable. */
10678 if (VAR_P (decl)
10679 && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
10681 /* Namespace-scope or local static; give it a mangled name. */
10682 /* FIXME share comdat with decl? */
10684 TREE_STATIC (var) = TREE_STATIC (decl);
10685 CP_DECL_THREAD_LOCAL_P (var) = CP_DECL_THREAD_LOCAL_P (decl);
10686 set_decl_tls_model (var, DECL_TLS_MODEL (decl));
10688 tree name = mangle_ref_init_variable (decl);
10689 DECL_NAME (var) = name;
10690 SET_DECL_ASSEMBLER_NAME (var, name);
10692 var = pushdecl (var);
10694 else
10695 /* Create a new cleanup level if necessary. */
10696 maybe_push_cleanup_level (type);
10698 return var;
10701 /* EXPR is the initializer for a variable DECL of reference or
10702 std::initializer_list type. Create, push and return a new VAR_DECL
10703 for the initializer so that it will live as long as DECL. Any
10704 cleanup for the new variable is returned through CLEANUP, and the
10705 code to initialize the new variable is returned through INITP. */
10707 static tree
10708 set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
10709 tree *initp)
10711 tree init;
10712 tree type;
10713 tree var;
10715 /* Create the temporary variable. */
10716 type = TREE_TYPE (expr);
10717 var = make_temporary_var_for_ref_to_temp (decl, type);
10718 layout_decl (var, 0);
10719 /* If the rvalue is the result of a function call it will be
10720 a TARGET_EXPR. If it is some other construct (such as a
10721 member access expression where the underlying object is
10722 itself the result of a function call), turn it into a
10723 TARGET_EXPR here. It is important that EXPR be a
10724 TARGET_EXPR below since otherwise the INIT_EXPR will
10725 attempt to make a bitwise copy of EXPR to initialize
10726 VAR. */
10727 if (TREE_CODE (expr) != TARGET_EXPR)
10728 expr = get_target_expr (expr);
10730 if (TREE_CODE (decl) == FIELD_DECL
10731 && extra_warnings && !TREE_NO_WARNING (decl))
10733 warning (OPT_Wextra, "a temporary bound to %qD only persists "
10734 "until the constructor exits", decl);
10735 TREE_NO_WARNING (decl) = true;
10738 /* Recursively extend temps in this initializer. */
10739 TARGET_EXPR_INITIAL (expr)
10740 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups);
10742 /* Any reference temp has a non-trivial initializer. */
10743 DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
10745 /* If the initializer is constant, put it in DECL_INITIAL so we get
10746 static initialization and use in constant expressions. */
10747 init = maybe_constant_init (expr);
10748 if (TREE_CONSTANT (init))
10750 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
10752 /* 5.19 says that a constant expression can include an
10753 lvalue-rvalue conversion applied to "a glvalue of literal type
10754 that refers to a non-volatile temporary object initialized
10755 with a constant expression". Rather than try to communicate
10756 that this VAR_DECL is a temporary, just mark it constexpr.
10758 Currently this is only useful for initializer_list temporaries,
10759 since reference vars can't appear in constant expressions. */
10760 DECL_DECLARED_CONSTEXPR_P (var) = true;
10761 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
10762 TREE_CONSTANT (var) = true;
10764 DECL_INITIAL (var) = init;
10765 init = NULL_TREE;
10767 else
10768 /* Create the INIT_EXPR that will initialize the temporary
10769 variable. */
10770 init = split_nonconstant_init (var, expr);
10771 if (at_function_scope_p ())
10773 add_decl_expr (var);
10775 if (TREE_STATIC (var))
10776 init = add_stmt_to_compound (init, register_dtor_fn (var));
10777 else
10779 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
10780 if (cleanup)
10781 vec_safe_push (*cleanups, cleanup);
10784 /* We must be careful to destroy the temporary only
10785 after its initialization has taken place. If the
10786 initialization throws an exception, then the
10787 destructor should not be run. We cannot simply
10788 transform INIT into something like:
10790 (INIT, ({ CLEANUP_STMT; }))
10792 because emit_local_var always treats the
10793 initializer as a full-expression. Thus, the
10794 destructor would run too early; it would run at the
10795 end of initializing the reference variable, rather
10796 than at the end of the block enclosing the
10797 reference variable.
10799 The solution is to pass back a cleanup expression
10800 which the caller is responsible for attaching to
10801 the statement tree. */
10803 else
10805 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
10806 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
10808 if (CP_DECL_THREAD_LOCAL_P (var))
10809 tls_aggregates = tree_cons (NULL_TREE, var,
10810 tls_aggregates);
10811 else
10812 static_aggregates = tree_cons (NULL_TREE, var,
10813 static_aggregates);
10815 else
10816 /* Check whether the dtor is callable. */
10817 cxx_maybe_build_cleanup (var, tf_warning_or_error);
10819 /* Avoid -Wunused-variable warning (c++/38958). */
10820 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
10821 && VAR_P (decl))
10822 TREE_USED (decl) = DECL_READ_P (decl) = true;
10824 *initp = init;
10825 return var;
10828 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
10829 initializing a variable of that TYPE. */
10831 tree
10832 initialize_reference (tree type, tree expr,
10833 int flags, tsubst_flags_t complain)
10835 conversion *conv;
10836 void *p;
10837 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
10839 if (type == error_mark_node || error_operand_p (expr))
10840 return error_mark_node;
10842 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10843 p = conversion_obstack_alloc (0);
10845 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
10846 flags, complain);
10847 if (!conv || conv->bad_p)
10849 if (complain & tf_error)
10851 if (conv)
10852 convert_like (conv, expr, complain);
10853 else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
10854 && !TYPE_REF_IS_RVALUE (type)
10855 && !lvalue_p (expr))
10856 error_at (loc, "invalid initialization of non-const reference of "
10857 "type %qH from an rvalue of type %qI",
10858 type, TREE_TYPE (expr));
10859 else
10860 error_at (loc, "invalid initialization of reference of type "
10861 "%qH from expression of type %qI", type,
10862 TREE_TYPE (expr));
10864 return error_mark_node;
10867 if (conv->kind == ck_ref_bind)
10868 /* Perform the conversion. */
10869 expr = convert_like (conv, expr, complain);
10870 else if (conv->kind == ck_ambig)
10871 /* We gave an error in build_user_type_conversion_1. */
10872 expr = error_mark_node;
10873 else
10874 gcc_unreachable ();
10876 /* Free all the conversions we allocated. */
10877 obstack_free (&conversion_obstack, p);
10879 return expr;
10882 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
10883 which is bound either to a reference or a std::initializer_list. */
10885 static tree
10886 extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups)
10888 tree sub = init;
10889 tree *p;
10890 STRIP_NOPS (sub);
10891 if (TREE_CODE (sub) == COMPOUND_EXPR)
10893 TREE_OPERAND (sub, 1)
10894 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups);
10895 return init;
10897 if (TREE_CODE (sub) != ADDR_EXPR)
10898 return init;
10899 /* Deal with binding to a subobject. */
10900 for (p = &TREE_OPERAND (sub, 0); TREE_CODE (*p) == COMPONENT_REF; )
10901 p = &TREE_OPERAND (*p, 0);
10902 if (TREE_CODE (*p) == TARGET_EXPR)
10904 tree subinit = NULL_TREE;
10905 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit);
10906 recompute_tree_invariant_for_addr_expr (sub);
10907 if (init != sub)
10908 init = fold_convert (TREE_TYPE (init), sub);
10909 if (subinit)
10910 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
10912 return init;
10915 /* INIT is part of the initializer for DECL. If there are any
10916 reference or initializer lists being initialized, extend their
10917 lifetime to match that of DECL. */
10919 tree
10920 extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
10922 tree type = TREE_TYPE (init);
10923 if (processing_template_decl)
10924 return init;
10925 if (TREE_CODE (type) == REFERENCE_TYPE)
10926 init = extend_ref_init_temps_1 (decl, init, cleanups);
10927 else
10929 tree ctor = init;
10930 if (TREE_CODE (ctor) == TARGET_EXPR)
10931 ctor = TARGET_EXPR_INITIAL (ctor);
10932 if (TREE_CODE (ctor) == CONSTRUCTOR)
10934 if (is_std_init_list (type))
10936 /* The temporary array underlying a std::initializer_list
10937 is handled like a reference temporary. */
10938 tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
10939 array = extend_ref_init_temps_1 (decl, array, cleanups);
10940 CONSTRUCTOR_ELT (ctor, 0)->value = array;
10942 else
10944 unsigned i;
10945 constructor_elt *p;
10946 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
10947 FOR_EACH_VEC_SAFE_ELT (elts, i, p)
10948 p->value = extend_ref_init_temps (decl, p->value, cleanups);
10950 recompute_constructor_flags (ctor);
10951 if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
10952 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10956 return init;
10959 /* Returns true iff an initializer for TYPE could contain temporaries that
10960 need to be extended because they are bound to references or
10961 std::initializer_list. */
10963 bool
10964 type_has_extended_temps (tree type)
10966 type = strip_array_types (type);
10967 if (TREE_CODE (type) == REFERENCE_TYPE)
10968 return true;
10969 if (CLASS_TYPE_P (type))
10971 if (is_std_init_list (type))
10972 return true;
10973 for (tree f = next_initializable_field (TYPE_FIELDS (type));
10974 f; f = next_initializable_field (DECL_CHAIN (f)))
10975 if (type_has_extended_temps (TREE_TYPE (f)))
10976 return true;
10978 return false;
10981 /* Returns true iff TYPE is some variant of std::initializer_list. */
10983 bool
10984 is_std_init_list (tree type)
10986 if (!TYPE_P (type))
10987 return false;
10988 if (cxx_dialect == cxx98)
10989 return false;
10990 /* Look through typedefs. */
10991 type = TYPE_MAIN_VARIANT (type);
10992 return (CLASS_TYPE_P (type)
10993 && CP_TYPE_CONTEXT (type) == std_node
10994 && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
10997 /* Returns true iff DECL is a list constructor: i.e. a constructor which
10998 will accept an argument list of a single std::initializer_list<T>. */
11000 bool
11001 is_list_ctor (tree decl)
11003 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
11004 tree arg;
11006 if (!args || args == void_list_node)
11007 return false;
11009 arg = non_reference (TREE_VALUE (args));
11010 if (!is_std_init_list (arg))
11011 return false;
11013 args = TREE_CHAIN (args);
11015 if (args && args != void_list_node && !TREE_PURPOSE (args))
11016 /* There are more non-defaulted parms. */
11017 return false;
11019 return true;
11022 #include "gt-cp-call.h"