[PR c++/82878] pass-by-invisiref in lambda
[official-gcc.git] / gcc / cp / call.c
blobd242b07a06bcb4bf2a84fde860876a0983ded0b2
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_instantiation_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 no aligned allocation function matches, try again without the
4376 alignment. */
4379 /* Figure out what function is being called. */
4380 if (!cand)
4381 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
4382 complain);
4384 /* If no suitable function could be found, issue an error message
4385 and give up. */
4386 if (!cand)
4388 if (complain & tf_error)
4389 print_error_for_call_failure (fns, *args, candidates);
4390 return error_mark_node;
4393 /* If a cookie is required, add some extra space. Whether
4394 or not a cookie is required cannot be determined until
4395 after we know which function was called. */
4396 if (*cookie_size)
4398 bool use_cookie = true;
4399 tree arg_types;
4401 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
4402 /* Skip the size_t parameter. */
4403 arg_types = TREE_CHAIN (arg_types);
4404 /* Check the remaining parameters (if any). */
4405 if (arg_types
4406 && TREE_CHAIN (arg_types) == void_list_node
4407 && same_type_p (TREE_VALUE (arg_types),
4408 ptr_type_node))
4409 use_cookie = false;
4410 /* If we need a cookie, adjust the number of bytes allocated. */
4411 if (use_cookie)
4413 /* Update the total size. */
4414 *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
4415 if (size_check)
4417 /* Set to (size_t)-1 if the size check fails. */
4418 gcc_assert (size_check != NULL_TREE);
4419 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4420 *size, TYPE_MAX_VALUE (sizetype));
4422 /* Update the argument list to reflect the adjusted size. */
4423 (**args)[0] = *size;
4425 else
4426 *cookie_size = NULL_TREE;
4429 /* Tell our caller which function we decided to call. */
4430 if (fn)
4431 *fn = cand->fn;
4433 /* Build the CALL_EXPR. */
4434 return build_over_call (cand, LOOKUP_NORMAL, complain);
4437 /* Build a new call to operator(). This may change ARGS. */
4439 static tree
4440 build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4442 struct z_candidate *candidates = 0, *cand;
4443 tree fns, convs, first_mem_arg = NULL_TREE;
4444 bool any_viable_p;
4445 tree result = NULL_TREE;
4446 void *p;
4448 obj = mark_lvalue_use (obj);
4450 if (error_operand_p (obj))
4451 return error_mark_node;
4453 tree type = TREE_TYPE (obj);
4455 obj = prep_operand (obj);
4457 if (TYPE_PTRMEMFUNC_P (type))
4459 if (complain & tf_error)
4460 /* It's no good looking for an overloaded operator() on a
4461 pointer-to-member-function. */
4462 error ("pointer-to-member function %qE cannot be called without "
4463 "an object; consider using %<.*%> or %<->*%>", obj);
4464 return error_mark_node;
4467 if (TYPE_BINFO (type))
4469 fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1);
4470 if (fns == error_mark_node)
4471 return error_mark_node;
4473 else
4474 fns = NULL_TREE;
4476 if (args != NULL && *args != NULL)
4478 *args = resolve_args (*args, complain);
4479 if (*args == NULL)
4480 return error_mark_node;
4483 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4484 p = conversion_obstack_alloc (0);
4486 if (fns)
4488 first_mem_arg = obj;
4490 add_candidates (BASELINK_FUNCTIONS (fns),
4491 first_mem_arg, *args, NULL_TREE,
4492 NULL_TREE, false,
4493 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
4494 LOOKUP_NORMAL, &candidates, complain);
4497 convs = lookup_conversions (type);
4499 for (; convs; convs = TREE_CHAIN (convs))
4501 tree totype = TREE_TYPE (convs);
4503 if (TYPE_PTRFN_P (totype)
4504 || TYPE_REFFN_P (totype)
4505 || (TREE_CODE (totype) == REFERENCE_TYPE
4506 && TYPE_PTRFN_P (TREE_TYPE (totype))))
4507 for (ovl_iterator iter (TREE_VALUE (convs)); iter; ++iter)
4509 tree fn = *iter;
4511 if (DECL_NONCONVERTING_P (fn))
4512 continue;
4514 if (TREE_CODE (fn) == TEMPLATE_DECL)
4515 add_template_conv_candidate
4516 (&candidates, fn, obj, *args, totype,
4517 /*access_path=*/NULL_TREE,
4518 /*conversion_path=*/NULL_TREE, complain);
4519 else
4520 add_conv_candidate (&candidates, fn, obj,
4521 *args, /*conversion_path=*/NULL_TREE,
4522 /*access_path=*/NULL_TREE, complain);
4526 /* Be strict here because if we choose a bad conversion candidate, the
4527 errors we get won't mention the call context. */
4528 candidates = splice_viable (candidates, true, &any_viable_p);
4529 if (!any_viable_p)
4531 if (complain & tf_error)
4533 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
4534 build_tree_list_vec (*args));
4535 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4537 result = error_mark_node;
4539 else
4541 cand = tourney (candidates, complain);
4542 if (cand == 0)
4544 if (complain & tf_error)
4546 error ("call of %<(%T) (%A)%> is ambiguous",
4547 TREE_TYPE (obj), build_tree_list_vec (*args));
4548 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4550 result = error_mark_node;
4552 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
4553 && DECL_OVERLOADED_OPERATOR_P (cand->fn)
4554 && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
4555 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4556 else
4558 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
4559 obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
4560 -1, complain);
4561 else
4563 gcc_checking_assert (TYPE_P (cand->fn));
4564 obj = convert_like (cand->convs[0], obj, complain);
4566 obj = convert_from_reference (obj);
4567 result = cp_build_function_call_vec (obj, args, complain);
4571 /* Free all the conversions we allocated. */
4572 obstack_free (&conversion_obstack, p);
4574 return result;
4577 /* Wrapper for above. */
4579 tree
4580 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4582 tree ret;
4583 bool subtime = timevar_cond_start (TV_OVERLOAD);
4584 ret = build_op_call_1 (obj, args, complain);
4585 timevar_cond_stop (TV_OVERLOAD, subtime);
4586 return ret;
4589 /* Called by op_error to prepare format strings suitable for the error
4590 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4591 and a suffix (controlled by NTYPES). */
4593 static const char *
4594 op_error_string (const char *errmsg, int ntypes, bool match)
4596 const char *msg;
4598 const char *msgp = concat (match ? G_("ambiguous overload for ")
4599 : G_("no match for "), errmsg, NULL);
4601 if (ntypes == 3)
4602 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
4603 else if (ntypes == 2)
4604 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
4605 else
4606 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
4608 return msg;
4611 static void
4612 op_error (location_t loc, enum tree_code code, enum tree_code code2,
4613 tree arg1, tree arg2, tree arg3, bool match)
4615 bool assop = code == MODIFY_EXPR;
4616 const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
4618 switch (code)
4620 case COND_EXPR:
4621 if (flag_diagnostics_show_caret)
4622 error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
4623 3, match),
4624 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4625 else
4626 error_at (loc, op_error_string (G_("ternary %<operator?:%> "
4627 "in %<%E ? %E : %E%>"), 3, match),
4628 arg1, arg2, arg3,
4629 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4630 break;
4632 case POSTINCREMENT_EXPR:
4633 case POSTDECREMENT_EXPR:
4634 if (flag_diagnostics_show_caret)
4635 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4636 opname, TREE_TYPE (arg1));
4637 else
4638 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4639 1, match),
4640 opname, arg1, opname, TREE_TYPE (arg1));
4641 break;
4643 case ARRAY_REF:
4644 if (flag_diagnostics_show_caret)
4645 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
4646 TREE_TYPE (arg1), TREE_TYPE (arg2));
4647 else
4648 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
4649 2, match),
4650 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
4651 break;
4653 case REALPART_EXPR:
4654 case IMAGPART_EXPR:
4655 if (flag_diagnostics_show_caret)
4656 error_at (loc, op_error_string (G_("%qs"), 1, match),
4657 opname, TREE_TYPE (arg1));
4658 else
4659 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
4660 opname, opname, arg1, TREE_TYPE (arg1));
4661 break;
4663 default:
4664 if (arg2)
4665 if (flag_diagnostics_show_caret)
4666 error_at (loc, op_error_string (G_("%<operator%s%>"), 2, match),
4667 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
4668 else
4669 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
4670 2, match),
4671 opname, arg1, opname, arg2,
4672 TREE_TYPE (arg1), TREE_TYPE (arg2));
4673 else
4674 if (flag_diagnostics_show_caret)
4675 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4676 opname, TREE_TYPE (arg1));
4677 else
4678 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
4679 1, match),
4680 opname, opname, arg1, TREE_TYPE (arg1));
4681 break;
4685 /* Return the implicit conversion sequence that could be used to
4686 convert E1 to E2 in [expr.cond]. */
4688 static conversion *
4689 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
4691 tree t1 = non_reference (TREE_TYPE (e1));
4692 tree t2 = non_reference (TREE_TYPE (e2));
4693 conversion *conv;
4694 bool good_base;
4696 /* [expr.cond]
4698 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
4699 implicitly converted (clause _conv_) to the type "lvalue reference to
4700 T2", subject to the constraint that in the conversion the
4701 reference must bind directly (_dcl.init.ref_) to an lvalue.
4703 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
4704 implicitly converted to the type "rvalue reference to T2", subject to
4705 the constraint that the reference must bind directly. */
4706 if (glvalue_p (e2))
4708 tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
4709 conv = implicit_conversion (rtype,
4712 /*c_cast_p=*/false,
4713 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
4714 |LOOKUP_ONLYCONVERTING,
4715 complain);
4716 if (conv && !conv->bad_p)
4717 return conv;
4720 /* If E2 is a prvalue or if neither of the conversions above can be done
4721 and at least one of the operands has (possibly cv-qualified) class
4722 type: */
4723 if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
4724 return NULL;
4726 /* [expr.cond]
4728 If E1 and E2 have class type, and the underlying class types are
4729 the same or one is a base class of the other: E1 can be converted
4730 to match E2 if the class of T2 is the same type as, or a base
4731 class of, the class of T1, and the cv-qualification of T2 is the
4732 same cv-qualification as, or a greater cv-qualification than, the
4733 cv-qualification of T1. If the conversion is applied, E1 is
4734 changed to an rvalue of type T2 that still refers to the original
4735 source class object (or the appropriate subobject thereof). */
4736 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
4737 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
4739 if (good_base && at_least_as_qualified_p (t2, t1))
4741 conv = build_identity_conv (t1, e1);
4742 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
4743 TYPE_MAIN_VARIANT (t2)))
4744 conv = build_conv (ck_base, t2, conv);
4745 else
4746 conv = build_conv (ck_rvalue, t2, conv);
4747 return conv;
4749 else
4750 return NULL;
4752 else
4753 /* [expr.cond]
4755 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
4756 converted to the type that expression E2 would have if E2 were
4757 converted to an rvalue (or the type it has, if E2 is an rvalue). */
4758 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
4759 LOOKUP_IMPLICIT, complain);
4762 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
4763 arguments to the conditional expression. */
4765 static tree
4766 build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
4767 tsubst_flags_t complain)
4769 tree arg2_type;
4770 tree arg3_type;
4771 tree result = NULL_TREE;
4772 tree result_type = NULL_TREE;
4773 bool is_lvalue = true;
4774 struct z_candidate *candidates = 0;
4775 struct z_candidate *cand;
4776 void *p;
4777 tree orig_arg2, orig_arg3;
4779 /* As a G++ extension, the second argument to the conditional can be
4780 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
4781 c'.) If the second operand is omitted, make sure it is
4782 calculated only once. */
4783 if (!arg2)
4785 if (complain & tf_error)
4786 pedwarn (loc, OPT_Wpedantic,
4787 "ISO C++ forbids omitting the middle term of a ?: expression");
4789 if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
4790 warn_for_omitted_condop (loc, arg1);
4792 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
4793 if (lvalue_p (arg1))
4794 arg2 = arg1 = cp_stabilize_reference (arg1);
4795 else
4796 arg2 = arg1 = save_expr (arg1);
4799 /* If something has already gone wrong, just pass that fact up the
4800 tree. */
4801 if (error_operand_p (arg1)
4802 || error_operand_p (arg2)
4803 || error_operand_p (arg3))
4804 return error_mark_node;
4806 orig_arg2 = arg2;
4807 orig_arg3 = arg3;
4809 if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
4811 tree arg1_type = TREE_TYPE (arg1);
4813 /* If arg1 is another cond_expr choosing between -1 and 0,
4814 then we can use its comparison. It may help to avoid
4815 additional comparison, produce more accurate diagnostics
4816 and enables folding. */
4817 if (TREE_CODE (arg1) == VEC_COND_EXPR
4818 && integer_minus_onep (TREE_OPERAND (arg1, 1))
4819 && integer_zerop (TREE_OPERAND (arg1, 2)))
4820 arg1 = TREE_OPERAND (arg1, 0);
4822 arg1 = force_rvalue (arg1, complain);
4823 arg2 = force_rvalue (arg2, complain);
4824 arg3 = force_rvalue (arg3, complain);
4826 /* force_rvalue can return error_mark on valid arguments. */
4827 if (error_operand_p (arg1)
4828 || error_operand_p (arg2)
4829 || error_operand_p (arg3))
4830 return error_mark_node;
4832 arg2_type = TREE_TYPE (arg2);
4833 arg3_type = TREE_TYPE (arg3);
4835 if (!VECTOR_TYPE_P (arg2_type)
4836 && !VECTOR_TYPE_P (arg3_type))
4838 /* Rely on the error messages of the scalar version. */
4839 tree scal = build_conditional_expr_1 (loc, integer_one_node,
4840 orig_arg2, orig_arg3, complain);
4841 if (scal == error_mark_node)
4842 return error_mark_node;
4843 tree stype = TREE_TYPE (scal);
4844 tree ctype = TREE_TYPE (arg1_type);
4845 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
4846 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
4848 if (complain & tf_error)
4849 error_at (loc, "inferred scalar type %qT is not an integer or "
4850 "floating point type of the same size as %qT", stype,
4851 COMPARISON_CLASS_P (arg1)
4852 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4853 : ctype);
4854 return error_mark_node;
4857 tree vtype = build_opaque_vector_type (stype,
4858 TYPE_VECTOR_SUBPARTS (arg1_type));
4859 /* We could pass complain & tf_warning to unsafe_conversion_p,
4860 but the warnings (like Wsign-conversion) have already been
4861 given by the scalar build_conditional_expr_1. We still check
4862 unsafe_conversion_p to forbid truncating long long -> float. */
4863 if (unsafe_conversion_p (loc, stype, arg2, NULL_TREE, false))
4865 if (complain & tf_error)
4866 error_at (loc, "conversion of scalar %qH to vector %qI "
4867 "involves truncation", arg2_type, vtype);
4868 return error_mark_node;
4870 if (unsafe_conversion_p (loc, stype, arg3, NULL_TREE, false))
4872 if (complain & tf_error)
4873 error_at (loc, "conversion of scalar %qH to vector %qI "
4874 "involves truncation", arg3_type, vtype);
4875 return error_mark_node;
4878 arg2 = cp_convert (stype, arg2, complain);
4879 arg2 = save_expr (arg2);
4880 arg2 = build_vector_from_val (vtype, arg2);
4881 arg2_type = vtype;
4882 arg3 = cp_convert (stype, arg3, complain);
4883 arg3 = save_expr (arg3);
4884 arg3 = build_vector_from_val (vtype, arg3);
4885 arg3_type = vtype;
4888 if (VECTOR_TYPE_P (arg2_type) != VECTOR_TYPE_P (arg3_type))
4890 enum stv_conv convert_flag =
4891 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
4892 complain & tf_error);
4894 switch (convert_flag)
4896 case stv_error:
4897 return error_mark_node;
4898 case stv_firstarg:
4900 arg2 = save_expr (arg2);
4901 arg2 = convert (TREE_TYPE (arg3_type), arg2);
4902 arg2 = build_vector_from_val (arg3_type, arg2);
4903 arg2_type = TREE_TYPE (arg2);
4904 break;
4906 case stv_secondarg:
4908 arg3 = save_expr (arg3);
4909 arg3 = convert (TREE_TYPE (arg2_type), arg3);
4910 arg3 = build_vector_from_val (arg2_type, arg3);
4911 arg3_type = TREE_TYPE (arg3);
4912 break;
4914 default:
4915 break;
4919 if (!same_type_p (arg2_type, arg3_type)
4920 || TYPE_VECTOR_SUBPARTS (arg1_type)
4921 != TYPE_VECTOR_SUBPARTS (arg2_type)
4922 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
4924 if (complain & tf_error)
4925 error_at (loc,
4926 "incompatible vector types in conditional expression: "
4927 "%qT, %qT and %qT", TREE_TYPE (arg1),
4928 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
4929 return error_mark_node;
4932 if (!COMPARISON_CLASS_P (arg1))
4934 tree cmp_type = build_same_sized_truth_vector_type (arg1_type);
4935 arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
4937 return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
4940 /* [expr.cond]
4942 The first expression is implicitly converted to bool (clause
4943 _conv_). */
4944 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
4945 LOOKUP_NORMAL);
4946 if (error_operand_p (arg1))
4947 return error_mark_node;
4949 /* [expr.cond]
4951 If either the second or the third operand has type (possibly
4952 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
4953 array-to-pointer (_conv.array_), and function-to-pointer
4954 (_conv.func_) standard conversions are performed on the second
4955 and third operands. */
4956 arg2_type = unlowered_expr_type (arg2);
4957 arg3_type = unlowered_expr_type (arg3);
4958 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
4960 /* Do the conversions. We don't these for `void' type arguments
4961 since it can't have any effect and since decay_conversion
4962 does not handle that case gracefully. */
4963 if (!VOID_TYPE_P (arg2_type))
4964 arg2 = decay_conversion (arg2, complain);
4965 if (!VOID_TYPE_P (arg3_type))
4966 arg3 = decay_conversion (arg3, complain);
4967 arg2_type = TREE_TYPE (arg2);
4968 arg3_type = TREE_TYPE (arg3);
4970 /* [expr.cond]
4972 One of the following shall hold:
4974 --The second or the third operand (but not both) is a
4975 throw-expression (_except.throw_); the result is of the
4976 type of the other and is an rvalue.
4978 --Both the second and the third operands have type void; the
4979 result is of type void and is an rvalue.
4981 We must avoid calling force_rvalue for expressions of type
4982 "void" because it will complain that their value is being
4983 used. */
4984 if (TREE_CODE (arg2) == THROW_EXPR
4985 && TREE_CODE (arg3) != THROW_EXPR)
4987 if (!VOID_TYPE_P (arg3_type))
4989 arg3 = force_rvalue (arg3, complain);
4990 if (arg3 == error_mark_node)
4991 return error_mark_node;
4993 arg3_type = TREE_TYPE (arg3);
4994 result_type = arg3_type;
4996 else if (TREE_CODE (arg2) != THROW_EXPR
4997 && TREE_CODE (arg3) == THROW_EXPR)
4999 if (!VOID_TYPE_P (arg2_type))
5001 arg2 = force_rvalue (arg2, complain);
5002 if (arg2 == error_mark_node)
5003 return error_mark_node;
5005 arg2_type = TREE_TYPE (arg2);
5006 result_type = arg2_type;
5008 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
5009 result_type = void_type_node;
5010 else
5012 if (complain & tf_error)
5014 if (VOID_TYPE_P (arg2_type))
5015 error_at (EXPR_LOC_OR_LOC (arg3, loc),
5016 "second operand to the conditional operator "
5017 "is of type %<void%>, but the third operand is "
5018 "neither a throw-expression nor of type %<void%>");
5019 else
5020 error_at (EXPR_LOC_OR_LOC (arg2, loc),
5021 "third operand to the conditional operator "
5022 "is of type %<void%>, but the second operand is "
5023 "neither a throw-expression nor of type %<void%>");
5025 return error_mark_node;
5028 is_lvalue = false;
5029 goto valid_operands;
5031 /* [expr.cond]
5033 Otherwise, if the second and third operand have different types,
5034 and either has (possibly cv-qualified) class type, or if both are
5035 glvalues of the same value category and the same type except for
5036 cv-qualification, an attempt is made to convert each of those operands
5037 to the type of the other. */
5038 else if (!same_type_p (arg2_type, arg3_type)
5039 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
5040 || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
5041 arg3_type)
5042 && glvalue_p (arg2) && glvalue_p (arg3)
5043 && lvalue_p (arg2) == lvalue_p (arg3))))
5045 conversion *conv2;
5046 conversion *conv3;
5047 bool converted = false;
5049 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5050 p = conversion_obstack_alloc (0);
5052 conv2 = conditional_conversion (arg2, arg3, complain);
5053 conv3 = conditional_conversion (arg3, arg2, complain);
5055 /* [expr.cond]
5057 If both can be converted, or one can be converted but the
5058 conversion is ambiguous, the program is ill-formed. If
5059 neither can be converted, the operands are left unchanged and
5060 further checking is performed as described below. If exactly
5061 one conversion is possible, that conversion is applied to the
5062 chosen operand and the converted operand is used in place of
5063 the original operand for the remainder of this section. */
5064 if ((conv2 && !conv2->bad_p
5065 && conv3 && !conv3->bad_p)
5066 || (conv2 && conv2->kind == ck_ambig)
5067 || (conv3 && conv3->kind == ck_ambig))
5069 if (complain & tf_error)
5071 error_at (loc, "operands to ?: have different types %qT and %qT",
5072 arg2_type, arg3_type);
5073 if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
5074 inform (loc, " and each type can be converted to the other");
5075 else if (conv2 && conv2->kind == ck_ambig)
5076 convert_like (conv2, arg2, complain);
5077 else
5078 convert_like (conv3, arg3, complain);
5080 result = error_mark_node;
5082 else if (conv2 && !conv2->bad_p)
5084 arg2 = convert_like (conv2, arg2, complain);
5085 arg2 = convert_from_reference (arg2);
5086 arg2_type = TREE_TYPE (arg2);
5087 /* Even if CONV2 is a valid conversion, the result of the
5088 conversion may be invalid. For example, if ARG3 has type
5089 "volatile X", and X does not have a copy constructor
5090 accepting a "volatile X&", then even if ARG2 can be
5091 converted to X, the conversion will fail. */
5092 if (error_operand_p (arg2))
5093 result = error_mark_node;
5094 converted = true;
5096 else if (conv3 && !conv3->bad_p)
5098 arg3 = convert_like (conv3, arg3, complain);
5099 arg3 = convert_from_reference (arg3);
5100 arg3_type = TREE_TYPE (arg3);
5101 if (error_operand_p (arg3))
5102 result = error_mark_node;
5103 converted = true;
5106 /* Free all the conversions we allocated. */
5107 obstack_free (&conversion_obstack, p);
5109 if (result)
5110 return result;
5112 /* If, after the conversion, both operands have class type,
5113 treat the cv-qualification of both operands as if it were the
5114 union of the cv-qualification of the operands.
5116 The standard is not clear about what to do in this
5117 circumstance. For example, if the first operand has type
5118 "const X" and the second operand has a user-defined
5119 conversion to "volatile X", what is the type of the second
5120 operand after this step? Making it be "const X" (matching
5121 the first operand) seems wrong, as that discards the
5122 qualification without actually performing a copy. Leaving it
5123 as "volatile X" seems wrong as that will result in the
5124 conditional expression failing altogether, even though,
5125 according to this step, the one operand could be converted to
5126 the type of the other. */
5127 if (converted
5128 && CLASS_TYPE_P (arg2_type)
5129 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
5130 arg2_type = arg3_type =
5131 cp_build_qualified_type (arg2_type,
5132 cp_type_quals (arg2_type)
5133 | cp_type_quals (arg3_type));
5136 /* [expr.cond]
5138 If the second and third operands are glvalues of the same value
5139 category and have the same type, the result is of that type and
5140 value category. */
5141 if (((lvalue_p (arg2) && lvalue_p (arg3))
5142 || (xvalue_p (arg2) && xvalue_p (arg3)))
5143 && same_type_p (arg2_type, arg3_type))
5145 result_type = arg2_type;
5146 arg2 = mark_lvalue_use (arg2);
5147 arg3 = mark_lvalue_use (arg3);
5148 goto valid_operands;
5151 /* [expr.cond]
5153 Otherwise, the result is an rvalue. If the second and third
5154 operand do not have the same type, and either has (possibly
5155 cv-qualified) class type, overload resolution is used to
5156 determine the conversions (if any) to be applied to the operands
5157 (_over.match.oper_, _over.built_). */
5158 is_lvalue = false;
5159 if (!same_type_p (arg2_type, arg3_type)
5160 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
5162 tree args[3];
5163 conversion *conv;
5164 bool any_viable_p;
5166 /* Rearrange the arguments so that add_builtin_candidate only has
5167 to know about two args. In build_builtin_candidate, the
5168 arguments are unscrambled. */
5169 args[0] = arg2;
5170 args[1] = arg3;
5171 args[2] = arg1;
5172 add_builtin_candidates (&candidates,
5173 COND_EXPR,
5174 NOP_EXPR,
5175 ovl_op_identifier (false, COND_EXPR),
5176 args,
5177 LOOKUP_NORMAL, complain);
5179 /* [expr.cond]
5181 If the overload resolution fails, the program is
5182 ill-formed. */
5183 candidates = splice_viable (candidates, false, &any_viable_p);
5184 if (!any_viable_p)
5186 if (complain & tf_error)
5187 error_at (loc, "operands to ?: have different types %qT and %qT",
5188 arg2_type, arg3_type);
5189 return error_mark_node;
5191 cand = tourney (candidates, complain);
5192 if (!cand)
5194 if (complain & tf_error)
5196 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
5197 print_z_candidates (loc, candidates);
5199 return error_mark_node;
5202 /* [expr.cond]
5204 Otherwise, the conversions thus determined are applied, and
5205 the converted operands are used in place of the original
5206 operands for the remainder of this section. */
5207 conv = cand->convs[0];
5208 arg1 = convert_like (conv, arg1, complain);
5209 conv = cand->convs[1];
5210 arg2 = convert_like (conv, arg2, complain);
5211 arg2_type = TREE_TYPE (arg2);
5212 conv = cand->convs[2];
5213 arg3 = convert_like (conv, arg3, complain);
5214 arg3_type = TREE_TYPE (arg3);
5217 /* [expr.cond]
5219 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
5220 and function-to-pointer (_conv.func_) standard conversions are
5221 performed on the second and third operands.
5223 We need to force the lvalue-to-rvalue conversion here for class types,
5224 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
5225 that isn't wrapped with a TARGET_EXPR plays havoc with exception
5226 regions. */
5228 arg2 = force_rvalue (arg2, complain);
5229 if (!CLASS_TYPE_P (arg2_type))
5230 arg2_type = TREE_TYPE (arg2);
5232 arg3 = force_rvalue (arg3, complain);
5233 if (!CLASS_TYPE_P (arg3_type))
5234 arg3_type = TREE_TYPE (arg3);
5236 if (arg2 == error_mark_node || arg3 == error_mark_node)
5237 return error_mark_node;
5239 /* [expr.cond]
5241 After those conversions, one of the following shall hold:
5243 --The second and third operands have the same type; the result is of
5244 that type. */
5245 if (same_type_p (arg2_type, arg3_type))
5246 result_type = arg2_type;
5247 /* [expr.cond]
5249 --The second and third operands have arithmetic or enumeration
5250 type; the usual arithmetic conversions are performed to bring
5251 them to a common type, and the result is of that type. */
5252 else if ((ARITHMETIC_TYPE_P (arg2_type)
5253 || UNSCOPED_ENUM_P (arg2_type))
5254 && (ARITHMETIC_TYPE_P (arg3_type)
5255 || UNSCOPED_ENUM_P (arg3_type)))
5257 /* In this case, there is always a common type. */
5258 result_type = type_after_usual_arithmetic_conversions (arg2_type,
5259 arg3_type);
5260 if (complain & tf_warning)
5261 do_warn_double_promotion (result_type, arg2_type, arg3_type,
5262 "implicit conversion from %qH to %qI to "
5263 "match other result of conditional",
5264 loc);
5266 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
5267 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
5269 if (TREE_CODE (orig_arg2) == CONST_DECL
5270 && TREE_CODE (orig_arg3) == CONST_DECL
5271 && DECL_CONTEXT (orig_arg2) == DECL_CONTEXT (orig_arg3))
5272 /* Two enumerators from the same enumeration can have different
5273 types when the enumeration is still being defined. */;
5274 else if (complain & tf_warning)
5275 warning_at (loc, OPT_Wenum_compare, "enumeral mismatch in "
5276 "conditional expression: %qT vs %qT",
5277 arg2_type, arg3_type);
5279 else if (extra_warnings
5280 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
5281 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
5282 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
5283 && !same_type_p (arg2_type,
5284 type_promotes_to (arg3_type)))))
5286 if (complain & tf_warning)
5287 warning_at (loc, OPT_Wextra, "enumeral and non-enumeral type in "
5288 "conditional expression");
5291 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5292 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5294 /* [expr.cond]
5296 --The second and third operands have pointer type, or one has
5297 pointer type and the other is a null pointer constant; pointer
5298 conversions (_conv.ptr_) and qualification conversions
5299 (_conv.qual_) are performed to bring them to their composite
5300 pointer type (_expr.rel_). The result is of the composite
5301 pointer type.
5303 --The second and third operands have pointer to member type, or
5304 one has pointer to member type and the other is a null pointer
5305 constant; pointer to member conversions (_conv.mem_) and
5306 qualification conversions (_conv.qual_) are performed to bring
5307 them to a common type, whose cv-qualification shall match the
5308 cv-qualification of either the second or the third operand.
5309 The result is of the common type. */
5310 else if ((null_ptr_cst_p (arg2)
5311 && TYPE_PTR_OR_PTRMEM_P (arg3_type))
5312 || (null_ptr_cst_p (arg3)
5313 && TYPE_PTR_OR_PTRMEM_P (arg2_type))
5314 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
5315 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
5316 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
5318 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
5319 arg3, CPO_CONDITIONAL_EXPR,
5320 complain);
5321 if (result_type == error_mark_node)
5322 return error_mark_node;
5323 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5324 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5327 if (!result_type)
5329 if (complain & tf_error)
5330 error_at (loc, "operands to ?: have different types %qT and %qT",
5331 arg2_type, arg3_type);
5332 return error_mark_node;
5335 if (arg2 == error_mark_node || arg3 == error_mark_node)
5336 return error_mark_node;
5338 valid_operands:
5339 result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
5341 /* If the ARG2 and ARG3 are the same and don't have side-effects,
5342 warn here, because the COND_EXPR will be turned into ARG2. */
5343 if (warn_duplicated_branches
5344 && (arg2 == arg3 || operand_equal_p (arg2, arg3, 0)))
5345 warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
5346 "this condition has identical branches");
5348 /* We can't use result_type below, as fold might have returned a
5349 throw_expr. */
5351 if (!is_lvalue)
5353 /* Expand both sides into the same slot, hopefully the target of
5354 the ?: expression. We used to check for TARGET_EXPRs here,
5355 but now we sometimes wrap them in NOP_EXPRs so the test would
5356 fail. */
5357 if (CLASS_TYPE_P (TREE_TYPE (result)))
5358 result = get_target_expr_sfinae (result, complain);
5359 /* If this expression is an rvalue, but might be mistaken for an
5360 lvalue, we must add a NON_LVALUE_EXPR. */
5361 result = rvalue (result);
5363 else
5364 result = force_paren_expr (result);
5366 return result;
5369 /* Wrapper for above. */
5371 tree
5372 build_conditional_expr (location_t loc, tree arg1, tree arg2, tree arg3,
5373 tsubst_flags_t complain)
5375 tree ret;
5376 bool subtime = timevar_cond_start (TV_OVERLOAD);
5377 ret = build_conditional_expr_1 (loc, arg1, arg2, arg3, complain);
5378 timevar_cond_stop (TV_OVERLOAD, subtime);
5379 return ret;
5382 /* OPERAND is an operand to an expression. Perform necessary steps
5383 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
5384 returned. */
5386 static tree
5387 prep_operand (tree operand)
5389 if (operand)
5391 if (CLASS_TYPE_P (TREE_TYPE (operand))
5392 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
5393 /* Make sure the template type is instantiated now. */
5394 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
5397 return operand;
5400 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5401 OVERLOAD) to the CANDIDATES, returning an updated list of
5402 CANDIDATES. The ARGS are the arguments provided to the call;
5403 if FIRST_ARG is non-null it is the implicit object argument,
5404 otherwise the first element of ARGS is used if needed. The
5405 EXPLICIT_TARGS are explicit template arguments provided.
5406 TEMPLATE_ONLY is true if only template functions should be
5407 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5408 add_function_candidate. */
5410 static void
5411 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
5412 tree return_type,
5413 tree explicit_targs, bool template_only,
5414 tree conversion_path, tree access_path,
5415 int flags,
5416 struct z_candidate **candidates,
5417 tsubst_flags_t complain)
5419 tree ctype;
5420 const vec<tree, va_gc> *non_static_args;
5421 bool check_list_ctor = false;
5422 bool check_converting = false;
5423 unification_kind_t strict;
5425 if (!fns)
5426 return;
5428 /* Precalculate special handling of constructors and conversion ops. */
5429 tree fn = OVL_FIRST (fns);
5430 if (DECL_CONV_FN_P (fn))
5432 check_list_ctor = false;
5433 check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
5434 if (flags & LOOKUP_NO_CONVERSION)
5435 /* We're doing return_type(x). */
5436 strict = DEDUCE_CONV;
5437 else
5438 /* We're doing x.operator return_type(). */
5439 strict = DEDUCE_EXACT;
5440 /* [over.match.funcs] For conversion functions, the function
5441 is considered to be a member of the class of the implicit
5442 object argument for the purpose of defining the type of
5443 the implicit object parameter. */
5444 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
5446 else
5448 if (DECL_CONSTRUCTOR_P (fn))
5450 check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
5451 /* For list-initialization we consider explicit constructors
5452 and complain if one is chosen. */
5453 check_converting
5454 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
5455 == LOOKUP_ONLYCONVERTING);
5457 strict = DEDUCE_CALL;
5458 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
5461 if (first_arg)
5462 non_static_args = args;
5463 else
5464 /* Delay creating the implicit this parameter until it is needed. */
5465 non_static_args = NULL;
5467 for (lkp_iterator iter (fns); iter; ++iter)
5469 fn = *iter;
5471 if (check_converting && DECL_NONCONVERTING_P (fn))
5472 continue;
5473 if (check_list_ctor && !is_list_ctor (fn))
5474 continue;
5476 tree fn_first_arg = NULL_TREE;
5477 const vec<tree, va_gc> *fn_args = args;
5479 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
5481 /* Figure out where the object arg comes from. If this
5482 function is a non-static member and we didn't get an
5483 implicit object argument, move it out of args. */
5484 if (first_arg == NULL_TREE)
5486 unsigned int ix;
5487 tree arg;
5488 vec<tree, va_gc> *tempvec;
5489 vec_alloc (tempvec, args->length () - 1);
5490 for (ix = 1; args->iterate (ix, &arg); ++ix)
5491 tempvec->quick_push (arg);
5492 non_static_args = tempvec;
5493 first_arg = (*args)[0];
5496 fn_first_arg = first_arg;
5497 fn_args = non_static_args;
5500 if (TREE_CODE (fn) == TEMPLATE_DECL)
5501 add_template_candidate (candidates,
5503 ctype,
5504 explicit_targs,
5505 fn_first_arg,
5506 fn_args,
5507 return_type,
5508 access_path,
5509 conversion_path,
5510 flags,
5511 strict,
5512 complain);
5513 else if (!template_only)
5514 add_function_candidate (candidates,
5516 ctype,
5517 fn_first_arg,
5518 fn_args,
5519 access_path,
5520 conversion_path,
5521 flags,
5522 complain);
5526 /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
5527 -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
5529 static int
5530 op_is_ordered (tree_code code)
5532 switch (code)
5534 // 5. b @= a
5535 case MODIFY_EXPR:
5536 return (flag_strong_eval_order > 1 ? -1 : 0);
5538 // 6. a[b]
5539 case ARRAY_REF:
5540 return (flag_strong_eval_order > 1 ? 1 : 0);
5542 // 1. a.b
5543 // Not overloadable (yet).
5544 // 2. a->b
5545 // Only one argument.
5546 // 3. a->*b
5547 case MEMBER_REF:
5548 // 7. a << b
5549 case LSHIFT_EXPR:
5550 // 8. a >> b
5551 case RSHIFT_EXPR:
5552 return (flag_strong_eval_order ? 1 : 0);
5554 default:
5555 return 0;
5559 static tree
5560 build_new_op_1 (location_t loc, enum tree_code code, int flags, tree arg1,
5561 tree arg2, tree arg3, tree *overload, tsubst_flags_t complain)
5563 struct z_candidate *candidates = 0, *cand;
5564 vec<tree, va_gc> *arglist;
5565 tree args[3];
5566 tree result = NULL_TREE;
5567 bool result_valid_p = false;
5568 enum tree_code code2 = NOP_EXPR;
5569 enum tree_code code_orig_arg1 = ERROR_MARK;
5570 enum tree_code code_orig_arg2 = ERROR_MARK;
5571 conversion *conv;
5572 void *p;
5573 bool strict_p;
5574 bool any_viable_p;
5576 if (error_operand_p (arg1)
5577 || error_operand_p (arg2)
5578 || error_operand_p (arg3))
5579 return error_mark_node;
5581 bool ismodop = code == MODIFY_EXPR;
5582 if (ismodop)
5584 code2 = TREE_CODE (arg3);
5585 arg3 = NULL_TREE;
5587 tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
5589 arg1 = prep_operand (arg1);
5591 bool memonly = false;
5592 switch (code)
5594 case NEW_EXPR:
5595 case VEC_NEW_EXPR:
5596 case VEC_DELETE_EXPR:
5597 case DELETE_EXPR:
5598 /* Use build_op_new_call and build_op_delete_call instead. */
5599 gcc_unreachable ();
5601 case CALL_EXPR:
5602 /* Use build_op_call instead. */
5603 gcc_unreachable ();
5605 case TRUTH_ORIF_EXPR:
5606 case TRUTH_ANDIF_EXPR:
5607 case TRUTH_AND_EXPR:
5608 case TRUTH_OR_EXPR:
5609 /* These are saved for the sake of warn_logical_operator. */
5610 code_orig_arg1 = TREE_CODE (arg1);
5611 code_orig_arg2 = TREE_CODE (arg2);
5612 break;
5613 case GT_EXPR:
5614 case LT_EXPR:
5615 case GE_EXPR:
5616 case LE_EXPR:
5617 case EQ_EXPR:
5618 case NE_EXPR:
5619 /* These are saved for the sake of maybe_warn_bool_compare. */
5620 code_orig_arg1 = TREE_CODE (TREE_TYPE (arg1));
5621 code_orig_arg2 = TREE_CODE (TREE_TYPE (arg2));
5622 break;
5624 /* =, ->, [], () must be non-static member functions. */
5625 case MODIFY_EXPR:
5626 if (code2 != NOP_EXPR)
5627 break;
5628 /* FALLTHRU */
5629 case COMPONENT_REF:
5630 case ARRAY_REF:
5631 memonly = true;
5632 break;
5634 default:
5635 break;
5638 arg2 = prep_operand (arg2);
5639 arg3 = prep_operand (arg3);
5641 if (code == COND_EXPR)
5642 /* Use build_conditional_expr instead. */
5643 gcc_unreachable ();
5644 else if (! OVERLOAD_TYPE_P (TREE_TYPE (arg1))
5645 && (! arg2 || ! OVERLOAD_TYPE_P (TREE_TYPE (arg2))))
5646 goto builtin;
5648 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5649 arg2 = integer_zero_node;
5651 vec_alloc (arglist, 3);
5652 arglist->quick_push (arg1);
5653 if (arg2 != NULL_TREE)
5654 arglist->quick_push (arg2);
5655 if (arg3 != NULL_TREE)
5656 arglist->quick_push (arg3);
5658 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5659 p = conversion_obstack_alloc (0);
5661 /* Add namespace-scope operators to the list of functions to
5662 consider. */
5663 if (!memonly)
5665 tree fns = lookup_name_real (fnname, 0, 1, /*block_p=*/true, 0, 0);
5666 fns = lookup_arg_dependent (fnname, fns, arglist);
5667 add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
5668 NULL_TREE, false, NULL_TREE, NULL_TREE,
5669 flags, &candidates, complain);
5672 args[0] = arg1;
5673 args[1] = arg2;
5674 args[2] = NULL_TREE;
5676 /* Add class-member operators to the candidate set. */
5677 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
5679 tree fns;
5681 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
5682 if (fns == error_mark_node)
5684 result = error_mark_node;
5685 goto user_defined_result_ready;
5687 if (fns)
5688 add_candidates (BASELINK_FUNCTIONS (fns),
5689 NULL_TREE, arglist, NULL_TREE,
5690 NULL_TREE, false,
5691 BASELINK_BINFO (fns),
5692 BASELINK_ACCESS_BINFO (fns),
5693 flags, &candidates, complain);
5695 /* Per 13.3.1.2/3, 2nd bullet, if no operand has a class type, then
5696 only non-member functions that have type T1 or reference to
5697 cv-qualified-opt T1 for the first argument, if the first argument
5698 has an enumeration type, or T2 or reference to cv-qualified-opt
5699 T2 for the second argument, if the second argument has an
5700 enumeration type. Filter out those that don't match. */
5701 else if (! arg2 || ! CLASS_TYPE_P (TREE_TYPE (arg2)))
5703 struct z_candidate **candp, **next;
5705 for (candp = &candidates; *candp; candp = next)
5707 tree parmlist, parmtype;
5708 int i, nargs = (arg2 ? 2 : 1);
5710 cand = *candp;
5711 next = &cand->next;
5713 parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5715 for (i = 0; i < nargs; ++i)
5717 parmtype = TREE_VALUE (parmlist);
5719 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
5720 parmtype = TREE_TYPE (parmtype);
5721 if (TREE_CODE (TREE_TYPE (args[i])) == ENUMERAL_TYPE
5722 && (same_type_ignoring_top_level_qualifiers_p
5723 (TREE_TYPE (args[i]), parmtype)))
5724 break;
5726 parmlist = TREE_CHAIN (parmlist);
5729 /* No argument has an appropriate type, so remove this
5730 candidate function from the list. */
5731 if (i == nargs)
5733 *candp = cand->next;
5734 next = candp;
5739 add_builtin_candidates (&candidates, code, code2, fnname, args,
5740 flags, complain);
5742 switch (code)
5744 case COMPOUND_EXPR:
5745 case ADDR_EXPR:
5746 /* For these, the built-in candidates set is empty
5747 [over.match.oper]/3. We don't want non-strict matches
5748 because exact matches are always possible with built-in
5749 operators. The built-in candidate set for COMPONENT_REF
5750 would be empty too, but since there are no such built-in
5751 operators, we accept non-strict matches for them. */
5752 strict_p = true;
5753 break;
5755 default:
5756 strict_p = false;
5757 break;
5760 candidates = splice_viable (candidates, strict_p, &any_viable_p);
5761 if (!any_viable_p)
5763 switch (code)
5765 case POSTINCREMENT_EXPR:
5766 case POSTDECREMENT_EXPR:
5767 /* Don't try anything fancy if we're not allowed to produce
5768 errors. */
5769 if (!(complain & tf_error))
5770 return error_mark_node;
5772 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
5773 distinguish between prefix and postfix ++ and
5774 operator++() was used for both, so we allow this with
5775 -fpermissive. */
5776 else
5778 const char *msg = (flag_permissive)
5779 ? G_("no %<%D(int)%> declared for postfix %qs,"
5780 " trying prefix operator instead")
5781 : G_("no %<%D(int)%> declared for postfix %qs");
5782 permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
5785 if (!flag_permissive)
5786 return error_mark_node;
5788 if (code == POSTINCREMENT_EXPR)
5789 code = PREINCREMENT_EXPR;
5790 else
5791 code = PREDECREMENT_EXPR;
5792 result = build_new_op_1 (loc, code, flags, arg1, NULL_TREE,
5793 NULL_TREE, overload, complain);
5794 break;
5796 /* The caller will deal with these. */
5797 case ADDR_EXPR:
5798 case COMPOUND_EXPR:
5799 case COMPONENT_REF:
5800 result = NULL_TREE;
5801 result_valid_p = true;
5802 break;
5804 default:
5805 if (complain & tf_error)
5807 /* If one of the arguments of the operator represents
5808 an invalid use of member function pointer, try to report
5809 a meaningful error ... */
5810 if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
5811 || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
5812 || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
5813 /* We displayed the error message. */;
5814 else
5816 /* ... Otherwise, report the more generic
5817 "no matching operator found" error */
5818 op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
5819 print_z_candidates (loc, candidates);
5822 result = error_mark_node;
5823 break;
5826 else
5828 cand = tourney (candidates, complain);
5829 if (cand == 0)
5831 if (complain & tf_error)
5833 op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
5834 print_z_candidates (loc, candidates);
5836 result = error_mark_node;
5838 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5840 if (overload)
5841 *overload = cand->fn;
5843 if (resolve_args (arglist, complain) == NULL)
5844 result = error_mark_node;
5845 else
5846 result = build_over_call (cand, LOOKUP_NORMAL, complain);
5848 if (trivial_fn_p (cand->fn))
5849 /* There won't be a CALL_EXPR. */;
5850 else if (result && result != error_mark_node)
5852 tree call = extract_call_expr (result);
5853 CALL_EXPR_OPERATOR_SYNTAX (call) = true;
5855 if (processing_template_decl && DECL_HIDDEN_FRIEND_P (cand->fn))
5856 /* This prevents build_new_function_call from discarding this
5857 function during instantiation of the enclosing template. */
5858 KOENIG_LOOKUP_P (call) = 1;
5860 /* Specify evaluation order as per P0145R2. */
5861 CALL_EXPR_ORDERED_ARGS (call) = false;
5862 switch (op_is_ordered (code))
5864 case -1:
5865 CALL_EXPR_REVERSE_ARGS (call) = true;
5866 break;
5868 case 1:
5869 CALL_EXPR_ORDERED_ARGS (call) = true;
5870 break;
5872 default:
5873 break;
5877 else
5879 /* Give any warnings we noticed during overload resolution. */
5880 if (cand->warnings && (complain & tf_warning))
5882 struct candidate_warning *w;
5883 for (w = cand->warnings; w; w = w->next)
5884 joust (cand, w->loser, 1, complain);
5887 /* Check for comparison of different enum types. */
5888 switch (code)
5890 case GT_EXPR:
5891 case LT_EXPR:
5892 case GE_EXPR:
5893 case LE_EXPR:
5894 case EQ_EXPR:
5895 case NE_EXPR:
5896 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
5897 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
5898 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5899 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
5900 && (complain & tf_warning))
5902 warning (OPT_Wenum_compare,
5903 "comparison between %q#T and %q#T",
5904 TREE_TYPE (arg1), TREE_TYPE (arg2));
5906 break;
5907 default:
5908 break;
5911 /* We need to strip any leading REF_BIND so that bitfields
5912 don't cause errors. This should not remove any important
5913 conversions, because builtins don't apply to class
5914 objects directly. */
5915 conv = cand->convs[0];
5916 if (conv->kind == ck_ref_bind)
5917 conv = next_conversion (conv);
5918 arg1 = convert_like (conv, arg1, complain);
5920 if (arg2)
5922 conv = cand->convs[1];
5923 if (conv->kind == ck_ref_bind)
5924 conv = next_conversion (conv);
5925 else
5926 arg2 = decay_conversion (arg2, complain);
5928 /* We need to call warn_logical_operator before
5929 converting arg2 to a boolean_type, but after
5930 decaying an enumerator to its value. */
5931 if (complain & tf_warning)
5932 warn_logical_operator (loc, code, boolean_type_node,
5933 code_orig_arg1, arg1,
5934 code_orig_arg2, arg2);
5936 arg2 = convert_like (conv, arg2, complain);
5938 if (arg3)
5940 conv = cand->convs[2];
5941 if (conv->kind == ck_ref_bind)
5942 conv = next_conversion (conv);
5943 arg3 = convert_like (conv, arg3, complain);
5949 user_defined_result_ready:
5951 /* Free all the conversions we allocated. */
5952 obstack_free (&conversion_obstack, p);
5954 if (result || result_valid_p)
5955 return result;
5957 builtin:
5958 switch (code)
5960 case MODIFY_EXPR:
5961 return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
5963 case INDIRECT_REF:
5964 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
5966 case TRUTH_ANDIF_EXPR:
5967 case TRUTH_ORIF_EXPR:
5968 case TRUTH_AND_EXPR:
5969 case TRUTH_OR_EXPR:
5970 if (complain & tf_warning)
5971 warn_logical_operator (loc, code, boolean_type_node,
5972 code_orig_arg1, arg1,
5973 code_orig_arg2, arg2);
5974 /* Fall through. */
5975 case GT_EXPR:
5976 case LT_EXPR:
5977 case GE_EXPR:
5978 case LE_EXPR:
5979 case EQ_EXPR:
5980 case NE_EXPR:
5981 if ((complain & tf_warning)
5982 && ((code_orig_arg1 == BOOLEAN_TYPE)
5983 ^ (code_orig_arg2 == BOOLEAN_TYPE)))
5984 maybe_warn_bool_compare (loc, code, arg1, arg2);
5985 if (complain & tf_warning && warn_tautological_compare)
5986 warn_tautological_cmp (loc, code, arg1, arg2);
5987 /* Fall through. */
5988 case PLUS_EXPR:
5989 case MINUS_EXPR:
5990 case MULT_EXPR:
5991 case TRUNC_DIV_EXPR:
5992 case MAX_EXPR:
5993 case MIN_EXPR:
5994 case LSHIFT_EXPR:
5995 case RSHIFT_EXPR:
5996 case TRUNC_MOD_EXPR:
5997 case BIT_AND_EXPR:
5998 case BIT_IOR_EXPR:
5999 case BIT_XOR_EXPR:
6000 return cp_build_binary_op (loc, code, arg1, arg2, complain);
6002 case UNARY_PLUS_EXPR:
6003 case NEGATE_EXPR:
6004 case BIT_NOT_EXPR:
6005 case TRUTH_NOT_EXPR:
6006 case PREINCREMENT_EXPR:
6007 case POSTINCREMENT_EXPR:
6008 case PREDECREMENT_EXPR:
6009 case POSTDECREMENT_EXPR:
6010 case REALPART_EXPR:
6011 case IMAGPART_EXPR:
6012 case ABS_EXPR:
6013 return cp_build_unary_op (code, arg1, candidates != 0, complain);
6015 case ARRAY_REF:
6016 return cp_build_array_ref (input_location, arg1, arg2, complain);
6018 case MEMBER_REF:
6019 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_ARROW_STAR,
6020 complain),
6021 arg2, complain);
6023 /* The caller will deal with these. */
6024 case ADDR_EXPR:
6025 case COMPONENT_REF:
6026 case COMPOUND_EXPR:
6027 return NULL_TREE;
6029 default:
6030 gcc_unreachable ();
6032 return NULL_TREE;
6035 /* Wrapper for above. */
6037 tree
6038 build_new_op (location_t loc, enum tree_code code, int flags,
6039 tree arg1, tree arg2, tree arg3,
6040 tree *overload, tsubst_flags_t complain)
6042 tree ret;
6043 bool subtime = timevar_cond_start (TV_OVERLOAD);
6044 ret = build_new_op_1 (loc, code, flags, arg1, arg2, arg3,
6045 overload, complain);
6046 timevar_cond_stop (TV_OVERLOAD, subtime);
6047 return ret;
6050 /* CALL was returned by some call-building function; extract the actual
6051 CALL_EXPR from any bits that have been tacked on, e.g. by
6052 convert_from_reference. */
6054 tree
6055 extract_call_expr (tree call)
6057 while (TREE_CODE (call) == COMPOUND_EXPR)
6058 call = TREE_OPERAND (call, 1);
6059 if (REFERENCE_REF_P (call))
6060 call = TREE_OPERAND (call, 0);
6061 if (TREE_CODE (call) == TARGET_EXPR)
6062 call = TARGET_EXPR_INITIAL (call);
6063 gcc_assert (TREE_CODE (call) == CALL_EXPR
6064 || TREE_CODE (call) == AGGR_INIT_EXPR
6065 || call == error_mark_node);
6066 return call;
6069 /* Returns true if FN has two parameters, of which the second has type
6070 size_t. */
6072 static bool
6073 second_parm_is_size_t (tree fn)
6075 tree t = FUNCTION_ARG_CHAIN (fn);
6076 if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
6077 return false;
6078 t = TREE_CHAIN (t);
6079 if (t == void_list_node)
6080 return true;
6081 if (aligned_new_threshold && t
6082 && same_type_p (TREE_VALUE (t), align_type_node)
6083 && TREE_CHAIN (t) == void_list_node)
6084 return true;
6085 return false;
6088 /* True if T, an allocation function, has std::align_val_t as its second
6089 argument. */
6091 bool
6092 aligned_allocation_fn_p (tree t)
6094 if (!aligned_new_threshold)
6095 return false;
6097 tree a = FUNCTION_ARG_CHAIN (t);
6098 return (a && same_type_p (TREE_VALUE (a), align_type_node));
6101 /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
6102 function (3.7.4.2 [basic.stc.dynamic.deallocation]) with a parameter of
6103 std::align_val_t. */
6105 static bool
6106 aligned_deallocation_fn_p (tree t)
6108 if (!aligned_new_threshold)
6109 return false;
6111 /* A template instance is never a usual deallocation function,
6112 regardless of its signature. */
6113 if (TREE_CODE (t) == TEMPLATE_DECL
6114 || primary_template_instantiation_p (t))
6115 return false;
6117 tree a = FUNCTION_ARG_CHAIN (t);
6118 if (same_type_p (TREE_VALUE (a), align_type_node)
6119 && TREE_CHAIN (a) == void_list_node)
6120 return true;
6121 if (!same_type_p (TREE_VALUE (a), size_type_node))
6122 return false;
6123 a = TREE_CHAIN (a);
6124 if (a && same_type_p (TREE_VALUE (a), align_type_node)
6125 && TREE_CHAIN (a) == void_list_node)
6126 return true;
6127 return false;
6130 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
6131 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
6133 bool
6134 usual_deallocation_fn_p (tree t)
6136 /* A template instance is never a usual deallocation function,
6137 regardless of its signature. */
6138 if (TREE_CODE (t) == TEMPLATE_DECL
6139 || primary_template_instantiation_p (t))
6140 return false;
6142 /* If a class T has a member deallocation function named operator delete
6143 with exactly one parameter, then that function is a usual
6144 (non-placement) deallocation function. If class T does not declare
6145 such an operator delete but does declare a member deallocation
6146 function named operator delete with exactly two parameters, the second
6147 of which has type std::size_t (18.2), then this function is a usual
6148 deallocation function. */
6149 bool global = DECL_NAMESPACE_SCOPE_P (t);
6150 tree chain = FUNCTION_ARG_CHAIN (t);
6151 if (!chain)
6152 return false;
6153 if (chain == void_list_node
6154 || ((!global || flag_sized_deallocation)
6155 && second_parm_is_size_t (t)))
6156 return true;
6157 if (aligned_deallocation_fn_p (t))
6158 return true;
6159 return false;
6162 /* Build a call to operator delete. This has to be handled very specially,
6163 because the restrictions on what signatures match are different from all
6164 other call instances. For a normal delete, only a delete taking (void *)
6165 or (void *, size_t) is accepted. For a placement delete, only an exact
6166 match with the placement new is accepted.
6168 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
6169 ADDR is the pointer to be deleted.
6170 SIZE is the size of the memory block to be deleted.
6171 GLOBAL_P is true if the delete-expression should not consider
6172 class-specific delete operators.
6173 PLACEMENT is the corresponding placement new call, or NULL_TREE.
6175 If this call to "operator delete" is being generated as part to
6176 deallocate memory allocated via a new-expression (as per [expr.new]
6177 which requires that if the initialization throws an exception then
6178 we call a deallocation function), then ALLOC_FN is the allocation
6179 function. */
6181 tree
6182 build_op_delete_call (enum tree_code code, tree addr, tree size,
6183 bool global_p, tree placement,
6184 tree alloc_fn, tsubst_flags_t complain)
6186 tree fn = NULL_TREE;
6187 tree fns, fnname, type, t;
6189 if (addr == error_mark_node)
6190 return error_mark_node;
6192 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
6194 fnname = ovl_op_identifier (false, code);
6196 if (CLASS_TYPE_P (type)
6197 && COMPLETE_TYPE_P (complete_type (type))
6198 && !global_p)
6199 /* In [class.free]
6201 If the result of the lookup is ambiguous or inaccessible, or if
6202 the lookup selects a placement deallocation function, the
6203 program is ill-formed.
6205 Therefore, we ask lookup_fnfields to complain about ambiguity. */
6207 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
6208 if (fns == error_mark_node)
6209 return error_mark_node;
6211 else
6212 fns = NULL_TREE;
6214 if (fns == NULL_TREE)
6215 fns = lookup_name_nonclass (fnname);
6217 /* Strip const and volatile from addr. */
6218 addr = cp_convert (ptr_type_node, addr, complain);
6220 if (placement)
6222 /* "A declaration of a placement deallocation function matches the
6223 declaration of a placement allocation function if it has the same
6224 number of parameters and, after parameter transformations (8.3.5),
6225 all parameter types except the first are identical."
6227 So we build up the function type we want and ask instantiate_type
6228 to get it for us. */
6229 t = FUNCTION_ARG_CHAIN (alloc_fn);
6230 t = tree_cons (NULL_TREE, ptr_type_node, t);
6231 t = build_function_type (void_type_node, t);
6233 fn = instantiate_type (t, fns, tf_none);
6234 if (fn == error_mark_node)
6235 return NULL_TREE;
6237 fn = MAYBE_BASELINK_FUNCTIONS (fn);
6239 /* "If the lookup finds the two-parameter form of a usual deallocation
6240 function (3.7.4.2) and that function, considered as a placement
6241 deallocation function, would have been selected as a match for the
6242 allocation function, the program is ill-formed." */
6243 if (second_parm_is_size_t (fn))
6245 const char *const msg1
6246 = G_("exception cleanup for this placement new selects "
6247 "non-placement operator delete");
6248 const char *const msg2
6249 = G_("%qD is a usual (non-placement) deallocation "
6250 "function in C++14 (or with -fsized-deallocation)");
6252 /* But if the class has an operator delete (void *), then that is
6253 the usual deallocation function, so we shouldn't complain
6254 about using the operator delete (void *, size_t). */
6255 if (DECL_CLASS_SCOPE_P (fn))
6256 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns));
6257 iter; ++iter)
6259 tree elt = *iter;
6260 if (usual_deallocation_fn_p (elt)
6261 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
6262 goto ok;
6264 /* Before C++14 a two-parameter global deallocation function is
6265 always a placement deallocation function, but warn if
6266 -Wc++14-compat. */
6267 else if (!flag_sized_deallocation)
6269 if ((complain & tf_warning)
6270 && warning (OPT_Wc__14_compat, msg1))
6271 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6272 goto ok;
6275 if (complain & tf_warning_or_error)
6277 if (permerror (input_location, msg1))
6279 /* Only mention C++14 for namespace-scope delete. */
6280 if (DECL_NAMESPACE_SCOPE_P (fn))
6281 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6282 else
6283 inform (DECL_SOURCE_LOCATION (fn),
6284 "%qD is a usual (non-placement) deallocation "
6285 "function", fn);
6288 else
6289 return error_mark_node;
6290 ok:;
6293 else
6294 /* "Any non-placement deallocation function matches a non-placement
6295 allocation function. If the lookup finds a single matching
6296 deallocation function, that function will be called; otherwise, no
6297 deallocation function will be called." */
6298 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
6300 tree elt = *iter;
6301 if (usual_deallocation_fn_p (elt))
6303 if (!fn)
6305 fn = elt;
6306 continue;
6309 /* -- If the type has new-extended alignment, a function with a
6310 parameter of type std::align_val_t is preferred; otherwise a
6311 function without such a parameter is preferred. If exactly one
6312 preferred function is found, that function is selected and the
6313 selection process terminates. If more than one preferred
6314 function is found, all non-preferred functions are eliminated
6315 from further consideration. */
6316 if (aligned_new_threshold)
6318 bool want_align = type_has_new_extended_alignment (type);
6319 bool fn_align = aligned_deallocation_fn_p (fn);
6320 bool elt_align = aligned_deallocation_fn_p (elt);
6322 if (elt_align != fn_align)
6324 if (want_align == elt_align)
6325 fn = elt;
6326 continue;
6330 /* -- If the deallocation functions have class scope, the one
6331 without a parameter of type std::size_t is selected. */
6332 bool want_size;
6333 if (DECL_CLASS_SCOPE_P (fn))
6334 want_size = false;
6336 /* -- If the type is complete and if, for the second alternative
6337 (delete array) only, the operand is a pointer to a class type
6338 with a non-trivial destructor or a (possibly multi-dimensional)
6339 array thereof, the function with a parameter of type std::size_t
6340 is selected.
6342 -- Otherwise, it is unspecified whether a deallocation function
6343 with a parameter of type std::size_t is selected. */
6344 else
6346 want_size = COMPLETE_TYPE_P (type);
6347 if (code == VEC_DELETE_EXPR
6348 && !TYPE_VEC_NEW_USES_COOKIE (type))
6349 /* We need a cookie to determine the array size. */
6350 want_size = false;
6352 bool fn_size = second_parm_is_size_t (fn);
6353 bool elt_size = second_parm_is_size_t (elt);
6354 gcc_assert (fn_size != elt_size);
6355 if (want_size == elt_size)
6356 fn = elt;
6360 /* If we have a matching function, call it. */
6361 if (fn)
6363 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6365 /* If the FN is a member function, make sure that it is
6366 accessible. */
6367 if (BASELINK_P (fns))
6368 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
6369 complain);
6371 /* Core issue 901: It's ok to new a type with deleted delete. */
6372 if (DECL_DELETED_FN (fn) && alloc_fn)
6373 return NULL_TREE;
6375 if (placement)
6377 /* The placement args might not be suitable for overload
6378 resolution at this point, so build the call directly. */
6379 int nargs = call_expr_nargs (placement);
6380 tree *argarray = XALLOCAVEC (tree, nargs);
6381 int i;
6382 argarray[0] = addr;
6383 for (i = 1; i < nargs; i++)
6384 argarray[i] = CALL_EXPR_ARG (placement, i);
6385 if (!mark_used (fn, complain) && !(complain & tf_error))
6386 return error_mark_node;
6387 return build_cxx_call (fn, nargs, argarray, complain);
6389 else
6391 tree ret;
6392 vec<tree, va_gc> *args = make_tree_vector ();
6393 args->quick_push (addr);
6394 if (second_parm_is_size_t (fn))
6395 args->quick_push (size);
6396 if (aligned_deallocation_fn_p (fn))
6398 tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
6399 args->quick_push (al);
6401 ret = cp_build_function_call_vec (fn, &args, complain);
6402 release_tree_vector (args);
6403 return ret;
6407 /* [expr.new]
6409 If no unambiguous matching deallocation function can be found,
6410 propagating the exception does not cause the object's memory to
6411 be freed. */
6412 if (alloc_fn)
6414 if ((complain & tf_warning)
6415 && !placement)
6416 warning (0, "no corresponding deallocation function for %qD",
6417 alloc_fn);
6418 return NULL_TREE;
6421 if (complain & tf_error)
6422 error ("no suitable %<operator %s%> for %qT",
6423 OVL_OP_INFO (false, code)->name, type);
6424 return error_mark_node;
6427 /* If the current scope isn't allowed to access DECL along
6428 BASETYPE_PATH, give an error. The most derived class in
6429 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
6430 the declaration to use in the error diagnostic. */
6432 bool
6433 enforce_access (tree basetype_path, tree decl, tree diag_decl,
6434 tsubst_flags_t complain, access_failure_info *afi)
6436 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
6438 if (flag_new_inheriting_ctors
6439 && DECL_INHERITED_CTOR (decl))
6441 /* 7.3.3/18: The additional constructors are accessible if they would be
6442 accessible when used to construct an object of the corresponding base
6443 class. */
6444 decl = strip_inheriting_ctors (decl);
6445 basetype_path = lookup_base (basetype_path, DECL_CONTEXT (decl),
6446 ba_any, NULL, complain);
6449 if (!accessible_p (basetype_path, decl, true))
6451 if (complain & tf_error)
6453 if (flag_new_inheriting_ctors)
6454 diag_decl = strip_inheriting_ctors (diag_decl);
6455 if (TREE_PRIVATE (decl))
6457 error ("%q#D is private within this context", diag_decl);
6458 inform (DECL_SOURCE_LOCATION (diag_decl),
6459 "declared private here");
6460 if (afi)
6461 afi->record_access_failure (basetype_path, diag_decl);
6463 else if (TREE_PROTECTED (decl))
6465 error ("%q#D is protected within this context", diag_decl);
6466 inform (DECL_SOURCE_LOCATION (diag_decl),
6467 "declared protected here");
6468 if (afi)
6469 afi->record_access_failure (basetype_path, diag_decl);
6471 else
6473 error ("%q#D is inaccessible within this context", diag_decl);
6474 inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
6475 if (afi)
6476 afi->record_access_failure (basetype_path, diag_decl);
6479 return false;
6482 return true;
6485 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
6486 bitwise or of LOOKUP_* values. If any errors are warnings are
6487 generated, set *DIAGNOSTIC_FN to "error" or "warning",
6488 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
6489 to NULL. */
6491 static tree
6492 build_temp (tree expr, tree type, int flags,
6493 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
6495 int savew, savee;
6496 vec<tree, va_gc> *args;
6498 *diagnostic_kind = DK_UNSPECIFIED;
6500 /* If the source is a packed field, calling the copy constructor will require
6501 binding the field to the reference parameter to the copy constructor, and
6502 we'll end up with an infinite loop. If we can use a bitwise copy, then
6503 do that now. */
6504 if ((lvalue_kind (expr) & clk_packed)
6505 && CLASS_TYPE_P (TREE_TYPE (expr))
6506 && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
6507 return get_target_expr_sfinae (expr, complain);
6509 savew = warningcount + werrorcount, savee = errorcount;
6510 args = make_tree_vector_single (expr);
6511 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6512 &args, type, flags, complain);
6513 release_tree_vector (args);
6514 if (warningcount + werrorcount > savew)
6515 *diagnostic_kind = DK_WARNING;
6516 else if (errorcount > savee)
6517 *diagnostic_kind = DK_ERROR;
6518 return expr;
6521 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
6522 EXPR is implicitly converted to type TOTYPE.
6523 FN and ARGNUM are used for diagnostics. */
6525 static void
6526 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
6528 /* Issue warnings about peculiar, but valid, uses of NULL. */
6529 if (expr == null_node && TREE_CODE (totype) != BOOLEAN_TYPE
6530 && ARITHMETIC_TYPE_P (totype))
6532 source_location loc =
6533 expansion_point_location_if_in_system_header (input_location);
6535 if (fn)
6536 warning_at (loc, OPT_Wconversion_null,
6537 "passing NULL to non-pointer argument %P of %qD",
6538 argnum, fn);
6539 else
6540 warning_at (loc, OPT_Wconversion_null,
6541 "converting to non-pointer type %qT from NULL", totype);
6544 /* Issue warnings if "false" is converted to a NULL pointer */
6545 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
6546 && TYPE_PTR_P (totype))
6548 if (fn)
6549 warning_at (input_location, OPT_Wconversion_null,
6550 "converting %<false%> to pointer type for argument %P "
6551 "of %qD", argnum, fn);
6552 else
6553 warning_at (input_location, OPT_Wconversion_null,
6554 "converting %<false%> to pointer type %qT", totype);
6558 /* We gave a diagnostic during a conversion. If this was in the second
6559 standard conversion sequence of a user-defined conversion sequence, say
6560 which user-defined conversion. */
6562 static void
6563 maybe_print_user_conv_context (conversion *convs)
6565 if (convs->user_conv_p)
6566 for (conversion *t = convs; t; t = next_conversion (t))
6567 if (t->kind == ck_user)
6569 print_z_candidate (0, " after user-defined conversion:",
6570 t->cand);
6571 break;
6575 /* Locate the parameter with the given index within FNDECL.
6576 ARGNUM is zero based, -1 indicates the `this' argument of a method.
6577 Return the location of the FNDECL itself if there are problems. */
6579 static location_t
6580 get_fndecl_argument_location (tree fndecl, int argnum)
6582 int i;
6583 tree param;
6585 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6586 for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
6587 i < argnum && param;
6588 i++, param = TREE_CHAIN (param))
6591 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6592 return the location of FNDECL. */
6593 if (param == NULL)
6594 return DECL_SOURCE_LOCATION (fndecl);
6596 return DECL_SOURCE_LOCATION (param);
6599 /* Perform the conversions in CONVS on the expression EXPR. FN and
6600 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
6601 indicates the `this' argument of a method. INNER is nonzero when
6602 being called to continue a conversion chain. It is negative when a
6603 reference binding will be applied, positive otherwise. If
6604 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
6605 conversions will be emitted if appropriate. If C_CAST_P is true,
6606 this conversion is coming from a C-style cast; in that case,
6607 conversions to inaccessible bases are permitted. */
6609 static tree
6610 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
6611 bool issue_conversion_warnings,
6612 bool c_cast_p, tsubst_flags_t complain)
6614 tree totype = convs->type;
6615 diagnostic_t diag_kind;
6616 int flags;
6617 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6619 if (convs->bad_p && !(complain & tf_error))
6620 return error_mark_node;
6622 if (convs->bad_p
6623 && convs->kind != ck_user
6624 && convs->kind != ck_list
6625 && convs->kind != ck_ambig
6626 && (convs->kind != ck_ref_bind
6627 || (convs->user_conv_p && next_conversion (convs)->bad_p))
6628 && (convs->kind != ck_rvalue
6629 || SCALAR_TYPE_P (totype))
6630 && convs->kind != ck_base)
6632 bool complained = false;
6633 conversion *t = convs;
6635 /* Give a helpful error if this is bad because of excess braces. */
6636 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6637 && SCALAR_TYPE_P (totype)
6638 && CONSTRUCTOR_NELTS (expr) > 0
6639 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
6641 complained = permerror (loc, "too many braces around initializer "
6642 "for %qT", totype);
6643 while (BRACE_ENCLOSED_INITIALIZER_P (expr)
6644 && CONSTRUCTOR_NELTS (expr) == 1)
6645 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6648 /* Give a helpful error if this is bad because a conversion to bool
6649 from std::nullptr_t requires direct-initialization. */
6650 if (NULLPTR_TYPE_P (TREE_TYPE (expr))
6651 && TREE_CODE (totype) == BOOLEAN_TYPE)
6652 complained = permerror (loc, "converting to %qH from %qI requires "
6653 "direct-initialization",
6654 totype, TREE_TYPE (expr));
6656 for (; t ; t = next_conversion (t))
6658 if (t->kind == ck_user && t->cand->reason)
6660 complained = permerror (loc, "invalid user-defined conversion "
6661 "from %qH to %qI", TREE_TYPE (expr),
6662 totype);
6663 if (complained)
6664 print_z_candidate (loc, "candidate is:", t->cand);
6665 expr = convert_like_real (t, expr, fn, argnum,
6666 /*issue_conversion_warnings=*/false,
6667 /*c_cast_p=*/false,
6668 complain);
6669 if (convs->kind == ck_ref_bind)
6670 expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
6671 LOOKUP_NORMAL, NULL_TREE,
6672 complain);
6673 else
6674 expr = cp_convert (totype, expr, complain);
6675 if (complained && fn)
6676 inform (DECL_SOURCE_LOCATION (fn),
6677 " initializing argument %P of %qD", argnum, fn);
6678 return expr;
6680 else if (t->kind == ck_user || !t->bad_p)
6682 expr = convert_like_real (t, expr, fn, argnum,
6683 /*issue_conversion_warnings=*/false,
6684 /*c_cast_p=*/false,
6685 complain);
6686 break;
6688 else if (t->kind == ck_ambig)
6689 return convert_like_real (t, expr, fn, argnum,
6690 /*issue_conversion_warnings=*/false,
6691 /*c_cast_p=*/false,
6692 complain);
6693 else if (t->kind == ck_identity)
6694 break;
6696 if (!complained)
6697 complained = permerror (loc, "invalid conversion from %qH to %qI",
6698 TREE_TYPE (expr), totype);
6699 if (complained && fn)
6700 inform (get_fndecl_argument_location (fn, argnum),
6701 " initializing argument %P of %qD", argnum, fn);
6703 return cp_convert (totype, expr, complain);
6706 if (issue_conversion_warnings && (complain & tf_warning))
6707 conversion_null_warnings (totype, expr, fn, argnum);
6709 switch (convs->kind)
6711 case ck_user:
6713 struct z_candidate *cand = convs->cand;
6715 if (cand == NULL)
6716 /* We chose the surrogate function from add_conv_candidate, now we
6717 actually need to build the conversion. */
6718 cand = build_user_type_conversion_1 (totype, expr,
6719 LOOKUP_NO_CONVERSION, complain);
6721 tree convfn = cand->fn;
6723 /* When converting from an init list we consider explicit
6724 constructors, but actually trying to call one is an error. */
6725 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
6726 && BRACE_ENCLOSED_INITIALIZER_P (expr)
6727 /* Unless this is for direct-list-initialization. */
6728 && !CONSTRUCTOR_IS_DIRECT_INIT (expr)
6729 /* And in C++98 a default constructor can't be explicit. */
6730 && cxx_dialect >= cxx11)
6732 if (!(complain & tf_error))
6733 return error_mark_node;
6734 location_t loc = location_of (expr);
6735 if (CONSTRUCTOR_NELTS (expr) == 0
6736 && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
6738 if (pedwarn (loc, 0, "converting to %qT from initializer list "
6739 "would use explicit constructor %qD",
6740 totype, convfn))
6741 inform (loc, "in C++11 and above a default constructor "
6742 "can be explicit");
6744 else
6745 error ("converting to %qT from initializer list would use "
6746 "explicit constructor %qD", totype, convfn);
6749 /* If we're initializing from {}, it's value-initialization. */
6750 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6751 && CONSTRUCTOR_NELTS (expr) == 0
6752 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
6754 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
6755 expr = build_value_init (totype, complain);
6756 expr = get_target_expr_sfinae (expr, complain);
6757 if (expr != error_mark_node)
6759 TARGET_EXPR_LIST_INIT_P (expr) = true;
6760 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
6762 return expr;
6765 expr = mark_rvalue_use (expr);
6767 /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
6768 any more UDCs. */
6769 expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
6770 complain);
6772 /* If this is a constructor or a function returning an aggr type,
6773 we need to build up a TARGET_EXPR. */
6774 if (DECL_CONSTRUCTOR_P (convfn))
6776 expr = build_cplus_new (totype, expr, complain);
6778 /* Remember that this was list-initialization. */
6779 if (convs->check_narrowing && expr != error_mark_node)
6780 TARGET_EXPR_LIST_INIT_P (expr) = true;
6783 return expr;
6785 case ck_identity:
6786 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
6788 int nelts = CONSTRUCTOR_NELTS (expr);
6789 if (nelts == 0)
6790 expr = build_value_init (totype, complain);
6791 else if (nelts == 1)
6792 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6793 else
6794 gcc_unreachable ();
6796 expr = mark_rvalue_use (expr);
6798 if (type_unknown_p (expr))
6799 expr = instantiate_type (totype, expr, complain);
6800 return expr;
6801 case ck_ambig:
6802 /* We leave bad_p off ck_ambig because overload resolution considers
6803 it valid, it just fails when we try to perform it. So we need to
6804 check complain here, too. */
6805 if (complain & tf_error)
6807 /* Call build_user_type_conversion again for the error. */
6808 build_user_type_conversion (totype, convs->u.expr, LOOKUP_IMPLICIT,
6809 complain);
6810 if (fn)
6811 inform (DECL_SOURCE_LOCATION (fn),
6812 " initializing argument %P of %qD", argnum, fn);
6814 return error_mark_node;
6816 case ck_list:
6818 /* Conversion to std::initializer_list<T>. */
6819 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
6820 tree new_ctor = build_constructor (init_list_type_node, NULL);
6821 unsigned len = CONSTRUCTOR_NELTS (expr);
6822 tree array, val, field;
6823 vec<constructor_elt, va_gc> *vec = NULL;
6824 unsigned ix;
6826 /* Convert all the elements. */
6827 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
6829 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
6830 false, false, complain);
6831 if (sub == error_mark_node)
6832 return sub;
6833 if (!BRACE_ENCLOSED_INITIALIZER_P (val)
6834 && !check_narrowing (TREE_TYPE (sub), val, complain))
6835 return error_mark_node;
6836 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
6837 if (!TREE_CONSTANT (sub))
6838 TREE_CONSTANT (new_ctor) = false;
6840 /* Build up the array. */
6841 elttype = cp_build_qualified_type
6842 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
6843 array = build_array_of_n_type (elttype, len);
6844 array = finish_compound_literal (array, new_ctor, complain);
6845 /* Take the address explicitly rather than via decay_conversion
6846 to avoid the error about taking the address of a temporary. */
6847 array = cp_build_addr_expr (array, complain);
6848 array = cp_convert (build_pointer_type (elttype), array, complain);
6849 if (array == error_mark_node)
6850 return error_mark_node;
6852 /* Build up the initializer_list object. */
6853 totype = complete_type (totype);
6854 field = next_initializable_field (TYPE_FIELDS (totype));
6855 CONSTRUCTOR_APPEND_ELT (vec, field, array);
6856 field = next_initializable_field (DECL_CHAIN (field));
6857 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
6858 new_ctor = build_constructor (totype, vec);
6859 return get_target_expr_sfinae (new_ctor, complain);
6862 case ck_aggr:
6863 if (TREE_CODE (totype) == COMPLEX_TYPE)
6865 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
6866 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
6867 real = perform_implicit_conversion (TREE_TYPE (totype),
6868 real, complain);
6869 imag = perform_implicit_conversion (TREE_TYPE (totype),
6870 imag, complain);
6871 expr = build2 (COMPLEX_EXPR, totype, real, imag);
6872 return expr;
6874 expr = reshape_init (totype, expr, complain);
6875 expr = get_target_expr_sfinae (digest_init (totype, expr, complain),
6876 complain);
6877 if (expr != error_mark_node)
6878 TARGET_EXPR_LIST_INIT_P (expr) = true;
6879 return expr;
6881 default:
6882 break;
6885 expr = convert_like_real (next_conversion (convs), expr, fn, argnum,
6886 convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
6887 c_cast_p,
6888 complain);
6889 if (expr == error_mark_node)
6890 return error_mark_node;
6892 switch (convs->kind)
6894 case ck_rvalue:
6895 expr = decay_conversion (expr, complain);
6896 if (expr == error_mark_node)
6898 if (complain & tf_error)
6900 maybe_print_user_conv_context (convs);
6901 if (fn)
6902 inform (DECL_SOURCE_LOCATION (fn),
6903 " initializing argument %P of %qD", argnum, fn);
6905 return error_mark_node;
6908 if (! MAYBE_CLASS_TYPE_P (totype))
6909 return expr;
6911 /* Don't introduce copies when passing arguments along to the inherited
6912 constructor. */
6913 if (current_function_decl
6914 && flag_new_inheriting_ctors
6915 && DECL_INHERITED_CTOR (current_function_decl))
6916 return expr;
6918 /* Fall through. */
6919 case ck_base:
6920 if (convs->kind == ck_base && !convs->need_temporary_p)
6922 /* We are going to bind a reference directly to a base-class
6923 subobject of EXPR. */
6924 /* Build an expression for `*((base*) &expr)'. */
6925 expr = convert_to_base (expr, totype,
6926 !c_cast_p, /*nonnull=*/true, complain);
6927 return expr;
6930 /* Copy-initialization where the cv-unqualified version of the source
6931 type is the same class as, or a derived class of, the class of the
6932 destination [is treated as direct-initialization]. [dcl.init] */
6933 flags = LOOKUP_NORMAL;
6934 if (convs->user_conv_p)
6935 /* This conversion is being done in the context of a user-defined
6936 conversion (i.e. the second step of copy-initialization), so
6937 don't allow any more. */
6938 flags |= LOOKUP_NO_CONVERSION;
6939 else
6940 flags |= LOOKUP_ONLYCONVERTING;
6941 if (convs->rvaluedness_matches_p)
6942 /* standard_conversion got LOOKUP_PREFER_RVALUE. */
6943 flags |= LOOKUP_PREFER_RVALUE;
6944 if (TREE_CODE (expr) == TARGET_EXPR
6945 && TARGET_EXPR_LIST_INIT_P (expr))
6946 /* Copy-list-initialization doesn't actually involve a copy. */
6947 return expr;
6948 expr = build_temp (expr, totype, flags, &diag_kind, complain);
6949 if (diag_kind && complain)
6951 maybe_print_user_conv_context (convs);
6952 if (fn)
6953 inform (DECL_SOURCE_LOCATION (fn),
6954 " initializing argument %P of %qD", argnum, fn);
6957 return build_cplus_new (totype, expr, complain);
6959 case ck_ref_bind:
6961 tree ref_type = totype;
6963 if (convs->bad_p && !next_conversion (convs)->bad_p)
6965 tree extype = TREE_TYPE (expr);
6966 if (TYPE_REF_IS_RVALUE (ref_type)
6967 && lvalue_p (expr))
6968 error_at (loc, "cannot bind rvalue reference of type %qH to "
6969 "lvalue of type %qI", totype, extype);
6970 else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
6971 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
6972 error_at (loc, "cannot bind non-const lvalue reference of "
6973 "type %qH to an rvalue of type %qI", totype, extype);
6974 else if (!reference_compatible_p (TREE_TYPE (totype), extype))
6975 error_at (loc, "binding reference of type %qH to %qI "
6976 "discards qualifiers", totype, extype);
6977 else
6978 gcc_unreachable ();
6979 maybe_print_user_conv_context (convs);
6980 if (fn)
6981 inform (DECL_SOURCE_LOCATION (fn),
6982 " initializing argument %P of %qD", argnum, fn);
6983 return error_mark_node;
6986 /* If necessary, create a temporary.
6988 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
6989 that need temporaries, even when their types are reference
6990 compatible with the type of reference being bound, so the
6991 upcoming call to cp_build_addr_expr doesn't fail. */
6992 if (convs->need_temporary_p
6993 || TREE_CODE (expr) == CONSTRUCTOR
6994 || TREE_CODE (expr) == VA_ARG_EXPR)
6996 /* Otherwise, a temporary of type "cv1 T1" is created and
6997 initialized from the initializer expression using the rules
6998 for a non-reference copy-initialization (8.5). */
7000 tree type = TREE_TYPE (ref_type);
7001 cp_lvalue_kind lvalue = lvalue_kind (expr);
7003 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7004 (type, next_conversion (convs)->type));
7005 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
7006 && !TYPE_REF_IS_RVALUE (ref_type))
7008 /* If the reference is volatile or non-const, we
7009 cannot create a temporary. */
7010 if (lvalue & clk_bitfield)
7011 error_at (loc, "cannot bind bitfield %qE to %qT",
7012 expr, ref_type);
7013 else if (lvalue & clk_packed)
7014 error_at (loc, "cannot bind packed field %qE to %qT",
7015 expr, ref_type);
7016 else
7017 error_at (loc, "cannot bind rvalue %qE to %qT",
7018 expr, ref_type);
7019 return error_mark_node;
7021 /* If the source is a packed field, and we must use a copy
7022 constructor, then building the target expr will require
7023 binding the field to the reference parameter to the
7024 copy constructor, and we'll end up with an infinite
7025 loop. If we can use a bitwise copy, then we'll be
7026 OK. */
7027 if ((lvalue & clk_packed)
7028 && CLASS_TYPE_P (type)
7029 && type_has_nontrivial_copy_init (type))
7031 error_at (loc, "cannot bind packed field %qE to %qT",
7032 expr, ref_type);
7033 return error_mark_node;
7035 if (lvalue & clk_bitfield)
7037 expr = convert_bitfield_to_declared_type (expr);
7038 expr = fold_convert (type, expr);
7040 expr = build_target_expr_with_type (expr, type, complain);
7043 /* Take the address of the thing to which we will bind the
7044 reference. */
7045 expr = cp_build_addr_expr (expr, complain);
7046 if (expr == error_mark_node)
7047 return error_mark_node;
7049 /* Convert it to a pointer to the type referred to by the
7050 reference. This will adjust the pointer if a derived to
7051 base conversion is being performed. */
7052 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
7053 expr, complain);
7054 /* Convert the pointer to the desired reference type. */
7055 return build_nop (ref_type, expr);
7058 case ck_lvalue:
7059 return decay_conversion (expr, complain);
7061 case ck_fnptr:
7062 /* ??? Should the address of a transaction-safe pointer point to the TM
7063 clone, and this conversion look up the primary function? */
7064 return build_nop (totype, expr);
7066 case ck_qual:
7067 /* Warn about deprecated conversion if appropriate. */
7068 string_conv_p (totype, expr, 1);
7069 break;
7071 case ck_ptr:
7072 if (convs->base_p)
7073 expr = convert_to_base (expr, totype, !c_cast_p,
7074 /*nonnull=*/false, complain);
7075 return build_nop (totype, expr);
7077 case ck_pmem:
7078 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
7079 c_cast_p, complain);
7081 default:
7082 break;
7085 if (convs->check_narrowing
7086 && !check_narrowing (totype, expr, complain))
7087 return error_mark_node;
7089 if (issue_conversion_warnings)
7090 expr = cp_convert_and_check (totype, expr, complain);
7091 else
7092 expr = cp_convert (totype, expr, complain);
7094 return expr;
7097 /* ARG is being passed to a varargs function. Perform any conversions
7098 required. Return the converted value. */
7100 tree
7101 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
7103 tree arg_type;
7104 location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
7106 /* [expr.call]
7108 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7109 standard conversions are performed. */
7110 arg = decay_conversion (arg, complain);
7111 arg_type = TREE_TYPE (arg);
7112 /* [expr.call]
7114 If the argument has integral or enumeration type that is subject
7115 to the integral promotions (_conv.prom_), or a floating point
7116 type that is subject to the floating point promotion
7117 (_conv.fpprom_), the value of the argument is converted to the
7118 promoted type before the call. */
7119 if (TREE_CODE (arg_type) == REAL_TYPE
7120 && (TYPE_PRECISION (arg_type)
7121 < TYPE_PRECISION (double_type_node))
7122 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
7124 if ((complain & tf_warning)
7125 && warn_double_promotion && !c_inhibit_evaluation_warnings)
7126 warning_at (loc, OPT_Wdouble_promotion,
7127 "implicit conversion from %qH to %qI when passing "
7128 "argument to function",
7129 arg_type, double_type_node);
7130 arg = convert_to_real_nofold (double_type_node, arg);
7132 else if (NULLPTR_TYPE_P (arg_type))
7133 arg = null_pointer_node;
7134 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
7136 if (SCOPED_ENUM_P (arg_type))
7138 tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
7139 complain);
7140 prom = cp_perform_integral_promotions (prom, complain);
7141 if (abi_version_crosses (6)
7142 && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
7143 && (complain & tf_warning))
7144 warning_at (loc, OPT_Wabi, "scoped enum %qT passed through ... as "
7145 "%qT before -fabi-version=6, %qT after", arg_type,
7146 TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
7147 if (!abi_version_at_least (6))
7148 arg = prom;
7150 else
7151 arg = cp_perform_integral_promotions (arg, complain);
7154 arg = require_complete_type_sfinae (arg, complain);
7155 arg_type = TREE_TYPE (arg);
7157 if (arg != error_mark_node
7158 /* In a template (or ill-formed code), we can have an incomplete type
7159 even after require_complete_type_sfinae, in which case we don't know
7160 whether it has trivial copy or not. */
7161 && COMPLETE_TYPE_P (arg_type)
7162 && !cp_unevaluated_operand)
7164 /* [expr.call] 5.2.2/7:
7165 Passing a potentially-evaluated argument of class type (Clause 9)
7166 with a non-trivial copy constructor or a non-trivial destructor
7167 with no corresponding parameter is conditionally-supported, with
7168 implementation-defined semantics.
7170 We support it as pass-by-invisible-reference, just like a normal
7171 value parameter.
7173 If the call appears in the context of a sizeof expression,
7174 it is not potentially-evaluated. */
7175 if (type_has_nontrivial_copy_init (arg_type)
7176 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
7178 arg = force_rvalue (arg, complain);
7179 if (complain & tf_warning)
7180 warning (OPT_Wconditionally_supported,
7181 "passing objects of non-trivially-copyable "
7182 "type %q#T through %<...%> is conditionally supported",
7183 arg_type);
7184 return cp_build_addr_expr (arg, complain);
7186 /* Build up a real lvalue-to-rvalue conversion in case the
7187 copy constructor is trivial but not callable. */
7188 else if (CLASS_TYPE_P (arg_type))
7189 force_rvalue (arg, complain);
7193 return arg;
7196 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
7198 tree
7199 build_x_va_arg (source_location loc, tree expr, tree type)
7201 if (processing_template_decl)
7203 tree r = build_min (VA_ARG_EXPR, type, expr);
7204 SET_EXPR_LOCATION (r, loc);
7205 return r;
7208 type = complete_type_or_else (type, NULL_TREE);
7210 if (expr == error_mark_node || !type)
7211 return error_mark_node;
7213 expr = mark_lvalue_use (expr);
7215 if (TREE_CODE (type) == REFERENCE_TYPE)
7217 error ("cannot receive reference type %qT through %<...%>", type);
7218 return error_mark_node;
7221 if (type_has_nontrivial_copy_init (type)
7222 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7224 /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
7225 it as pass by invisible reference. */
7226 warning_at (loc, OPT_Wconditionally_supported,
7227 "receiving objects of non-trivially-copyable type %q#T "
7228 "through %<...%> is conditionally-supported", type);
7230 tree ref = cp_build_reference_type (type, false);
7231 expr = build_va_arg (loc, expr, ref);
7232 return convert_from_reference (expr);
7235 tree ret = build_va_arg (loc, expr, type);
7236 if (CLASS_TYPE_P (type))
7237 /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
7238 know how to handle it. */
7239 ret = get_target_expr (ret);
7240 return ret;
7243 /* TYPE has been given to va_arg. Apply the default conversions which
7244 would have happened when passed via ellipsis. Return the promoted
7245 type, or the passed type if there is no change. */
7247 tree
7248 cxx_type_promotes_to (tree type)
7250 tree promote;
7252 /* Perform the array-to-pointer and function-to-pointer
7253 conversions. */
7254 type = type_decays_to (type);
7256 promote = type_promotes_to (type);
7257 if (same_type_p (type, promote))
7258 promote = type;
7260 return promote;
7263 /* ARG is a default argument expression being passed to a parameter of
7264 the indicated TYPE, which is a parameter to FN. PARMNUM is the
7265 zero-based argument number. Do any required conversions. Return
7266 the converted value. */
7268 static GTY(()) vec<tree, va_gc> *default_arg_context;
7269 void
7270 push_defarg_context (tree fn)
7271 { vec_safe_push (default_arg_context, fn); }
7273 void
7274 pop_defarg_context (void)
7275 { default_arg_context->pop (); }
7277 tree
7278 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
7279 tsubst_flags_t complain)
7281 int i;
7282 tree t;
7284 /* See through clones. */
7285 fn = DECL_ORIGIN (fn);
7286 /* And inheriting ctors. */
7287 if (flag_new_inheriting_ctors)
7288 fn = strip_inheriting_ctors (fn);
7290 /* Detect recursion. */
7291 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
7292 if (t == fn)
7294 if (complain & tf_error)
7295 error ("recursive evaluation of default argument for %q#D", fn);
7296 return error_mark_node;
7299 /* If the ARG is an unparsed default argument expression, the
7300 conversion cannot be performed. */
7301 if (TREE_CODE (arg) == DEFAULT_ARG)
7303 if (complain & tf_error)
7304 error ("call to %qD uses the default argument for parameter %P, which "
7305 "is not yet defined", fn, parmnum);
7306 return error_mark_node;
7309 push_defarg_context (fn);
7311 if (fn && DECL_TEMPLATE_INFO (fn))
7312 arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
7314 /* Due to:
7316 [dcl.fct.default]
7318 The names in the expression are bound, and the semantic
7319 constraints are checked, at the point where the default
7320 expressions appears.
7322 we must not perform access checks here. */
7323 push_deferring_access_checks (dk_no_check);
7324 /* We must make a copy of ARG, in case subsequent processing
7325 alters any part of it. */
7326 arg = break_out_target_exprs (arg);
7327 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
7328 ICR_DEFAULT_ARGUMENT, fn, parmnum,
7329 complain);
7330 arg = convert_for_arg_passing (type, arg, complain);
7331 pop_deferring_access_checks();
7333 pop_defarg_context ();
7335 return arg;
7338 /* Returns the type which will really be used for passing an argument of
7339 type TYPE. */
7341 tree
7342 type_passed_as (tree type)
7344 /* Pass classes with copy ctors by invisible reference. */
7345 if (TREE_ADDRESSABLE (type))
7347 type = build_reference_type (type);
7348 /* There are no other pointers to this temporary. */
7349 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
7351 else if (targetm.calls.promote_prototypes (type)
7352 && INTEGRAL_TYPE_P (type)
7353 && COMPLETE_TYPE_P (type)
7354 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
7355 type = integer_type_node;
7357 return type;
7360 /* Actually perform the appropriate conversion. */
7362 tree
7363 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
7365 tree bitfield_type;
7367 /* If VAL is a bitfield, then -- since it has already been converted
7368 to TYPE -- it cannot have a precision greater than TYPE.
7370 If it has a smaller precision, we must widen it here. For
7371 example, passing "int f:3;" to a function expecting an "int" will
7372 not result in any conversion before this point.
7374 If the precision is the same we must not risk widening. For
7375 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
7376 often have type "int", even though the C++ type for the field is
7377 "long long". If the value is being passed to a function
7378 expecting an "int", then no conversions will be required. But,
7379 if we call convert_bitfield_to_declared_type, the bitfield will
7380 be converted to "long long". */
7381 bitfield_type = is_bitfield_expr_with_lowered_type (val);
7382 if (bitfield_type
7383 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
7384 val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
7386 if (val == error_mark_node)
7388 /* Pass classes with copy ctors by invisible reference. */
7389 else if (TREE_ADDRESSABLE (type))
7390 val = build1 (ADDR_EXPR, build_reference_type (type), val);
7391 else if (targetm.calls.promote_prototypes (type)
7392 && INTEGRAL_TYPE_P (type)
7393 && COMPLETE_TYPE_P (type)
7394 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
7395 val = cp_perform_integral_promotions (val, complain);
7396 if (complain & tf_warning)
7398 if (warn_suggest_attribute_format)
7400 tree rhstype = TREE_TYPE (val);
7401 const enum tree_code coder = TREE_CODE (rhstype);
7402 const enum tree_code codel = TREE_CODE (type);
7403 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7404 && coder == codel
7405 && check_missing_format_attribute (type, rhstype))
7406 warning (OPT_Wsuggest_attribute_format,
7407 "argument of function call might be a candidate "
7408 "for a format attribute");
7410 maybe_warn_parm_abi (type, EXPR_LOC_OR_LOC (val, input_location));
7412 return val;
7415 /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
7416 which just decay_conversion or no conversions at all should be done.
7417 This is true for some builtins which don't act like normal functions.
7418 Return 2 if no conversions at all should be done, 1 if just
7419 decay_conversion. Return 3 for special treatment of the 3rd argument
7420 for __builtin_*_overflow_p. */
7423 magic_varargs_p (tree fn)
7425 if (flag_cilkplus && is_cilkplus_reduce_builtin (fn) != BUILT_IN_NONE)
7426 return 2;
7428 if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
7429 switch (DECL_FUNCTION_CODE (fn))
7431 case BUILT_IN_CLASSIFY_TYPE:
7432 case BUILT_IN_CONSTANT_P:
7433 case BUILT_IN_NEXT_ARG:
7434 case BUILT_IN_VA_START:
7435 return 1;
7437 case BUILT_IN_ADD_OVERFLOW_P:
7438 case BUILT_IN_SUB_OVERFLOW_P:
7439 case BUILT_IN_MUL_OVERFLOW_P:
7440 return 3;
7442 default:;
7443 return lookup_attribute ("type generic",
7444 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
7447 return 0;
7450 /* Returns the decl of the dispatcher function if FN is a function version. */
7452 tree
7453 get_function_version_dispatcher (tree fn)
7455 tree dispatcher_decl = NULL;
7457 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7458 && DECL_FUNCTION_VERSIONED (fn));
7460 gcc_assert (targetm.get_function_versions_dispatcher);
7461 dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
7463 if (dispatcher_decl == NULL)
7465 error_at (input_location, "use of multiversioned function "
7466 "without a default");
7467 return NULL;
7470 retrofit_lang_decl (dispatcher_decl);
7471 gcc_assert (dispatcher_decl != NULL);
7472 return dispatcher_decl;
7475 /* fn is a function version dispatcher that is marked used. Mark all the
7476 semantically identical function versions it will dispatch as used. */
7478 void
7479 mark_versions_used (tree fn)
7481 struct cgraph_node *node;
7482 struct cgraph_function_version_info *node_v;
7483 struct cgraph_function_version_info *it_v;
7485 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7487 node = cgraph_node::get (fn);
7488 if (node == NULL)
7489 return;
7491 gcc_assert (node->dispatcher_function);
7493 node_v = node->function_version ();
7494 if (node_v == NULL)
7495 return;
7497 /* All semantically identical versions are chained. Traverse and mark each
7498 one of them as used. */
7499 it_v = node_v->next;
7500 while (it_v != NULL)
7502 mark_used (it_v->this_node->decl);
7503 it_v = it_v->next;
7507 /* Build a call to "the copy constructor" for the type of A, even if it
7508 wouldn't be selected by normal overload resolution. Used for
7509 diagnostics. */
7511 static tree
7512 call_copy_ctor (tree a, tsubst_flags_t complain)
7514 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
7515 tree binfo = TYPE_BINFO (ctype);
7516 tree copy = get_copy_ctor (ctype, complain);
7517 copy = build_baselink (binfo, binfo, copy, NULL_TREE);
7518 tree ob = build_dummy_object (ctype);
7519 vec<tree, va_gc>* args = make_tree_vector_single (a);
7520 tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
7521 LOOKUP_NORMAL, NULL, complain);
7522 release_tree_vector (args);
7523 return r;
7526 /* Return true iff T refers to a base field. */
7528 static bool
7529 is_base_field_ref (tree t)
7531 STRIP_NOPS (t);
7532 if (TREE_CODE (t) == ADDR_EXPR)
7533 t = TREE_OPERAND (t, 0);
7534 if (TREE_CODE (t) == COMPONENT_REF)
7535 t = TREE_OPERAND (t, 1);
7536 if (TREE_CODE (t) == FIELD_DECL)
7537 return DECL_FIELD_IS_BASE (t);
7538 return false;
7541 /* We can't elide a copy from a function returning by value to a base
7542 subobject, as the callee might clobber tail padding. Return true iff this
7543 could be that case. */
7545 static bool
7546 unsafe_copy_elision_p (tree target, tree exp)
7548 /* Copy elision only happens with a TARGET_EXPR. */
7549 if (TREE_CODE (exp) != TARGET_EXPR)
7550 return false;
7551 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7552 /* It's safe to elide the copy for a class with no tail padding. */
7553 if (tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
7554 return false;
7555 /* It's safe to elide the copy if we aren't initializing a base object. */
7556 if (!is_base_field_ref (target))
7557 return false;
7558 tree init = TARGET_EXPR_INITIAL (exp);
7559 /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
7560 while (TREE_CODE (init) == COMPOUND_EXPR)
7561 init = TREE_OPERAND (init, 1);
7562 return (TREE_CODE (init) == AGGR_INIT_EXPR
7563 && !AGGR_INIT_VIA_CTOR_P (init));
7566 /* Subroutine of the various build_*_call functions. Overload resolution
7567 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
7568 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
7569 bitmask of various LOOKUP_* flags which apply to the call itself. */
7571 static tree
7572 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
7574 tree fn = cand->fn;
7575 const vec<tree, va_gc> *args = cand->args;
7576 tree first_arg = cand->first_arg;
7577 conversion **convs = cand->convs;
7578 conversion *conv;
7579 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
7580 int parmlen;
7581 tree val;
7582 int i = 0;
7583 int j = 0;
7584 unsigned int arg_index = 0;
7585 int is_method = 0;
7586 int nargs;
7587 tree *argarray;
7588 bool already_used = false;
7590 /* In a template, there is no need to perform all of the work that
7591 is normally done. We are only interested in the type of the call
7592 expression, i.e., the return type of the function. Any semantic
7593 errors will be deferred until the template is instantiated. */
7594 if (processing_template_decl)
7596 tree expr, addr;
7597 tree return_type;
7598 const tree *argarray;
7599 unsigned int nargs;
7601 if (undeduced_auto_decl (fn))
7602 mark_used (fn, complain);
7604 return_type = TREE_TYPE (TREE_TYPE (fn));
7605 nargs = vec_safe_length (args);
7606 if (first_arg == NULL_TREE)
7607 argarray = args->address ();
7608 else
7610 tree *alcarray;
7611 unsigned int ix;
7612 tree arg;
7614 ++nargs;
7615 alcarray = XALLOCAVEC (tree, nargs);
7616 alcarray[0] = build_this (first_arg);
7617 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
7618 alcarray[ix + 1] = arg;
7619 argarray = alcarray;
7622 addr = build_addr_func (fn, complain);
7623 if (addr == error_mark_node)
7624 return error_mark_node;
7625 expr = build_call_array_loc (input_location, return_type,
7626 addr, nargs, argarray);
7627 if (TREE_THIS_VOLATILE (fn) && cfun)
7628 current_function_returns_abnormally = 1;
7629 return convert_from_reference (expr);
7632 /* Give any warnings we noticed during overload resolution. */
7633 if (cand->warnings && (complain & tf_warning))
7635 struct candidate_warning *w;
7636 for (w = cand->warnings; w; w = w->next)
7637 joust (cand, w->loser, 1, complain);
7640 /* OK, we're actually calling this inherited constructor; set its deletedness
7641 appropriately. We can get away with doing this here because calling is
7642 the only way to refer to a constructor. */
7643 if (DECL_INHERITED_CTOR (fn))
7644 deduce_inheriting_ctor (fn);
7646 /* Make =delete work with SFINAE. */
7647 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7648 return error_mark_node;
7650 if (DECL_FUNCTION_MEMBER_P (fn))
7652 tree access_fn;
7653 /* If FN is a template function, two cases must be considered.
7654 For example:
7656 struct A {
7657 protected:
7658 template <class T> void f();
7660 template <class T> struct B {
7661 protected:
7662 void g();
7664 struct C : A, B<int> {
7665 using A::f; // #1
7666 using B<int>::g; // #2
7669 In case #1 where `A::f' is a member template, DECL_ACCESS is
7670 recorded in the primary template but not in its specialization.
7671 We check access of FN using its primary template.
7673 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
7674 because it is a member of class template B, DECL_ACCESS is
7675 recorded in the specialization `B<int>::g'. We cannot use its
7676 primary template because `B<T>::g' and `B<int>::g' may have
7677 different access. */
7678 if (DECL_TEMPLATE_INFO (fn)
7679 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
7680 access_fn = DECL_TI_TEMPLATE (fn);
7681 else
7682 access_fn = fn;
7683 if (!perform_or_defer_access_check (cand->access_path, access_fn,
7684 fn, complain))
7685 return error_mark_node;
7688 /* If we're checking for implicit delete, don't bother with argument
7689 conversions. */
7690 if (flags & LOOKUP_SPECULATIVE)
7692 if (DECL_DELETED_FN (fn))
7694 if (complain & tf_error)
7695 mark_used (fn);
7696 return error_mark_node;
7698 if (cand->viable == 1)
7699 return fn;
7700 else if (!(complain & tf_error))
7701 /* Reject bad conversions now. */
7702 return error_mark_node;
7703 /* else continue to get conversion error. */
7706 /* N3276 magic doesn't apply to nested calls. */
7707 tsubst_flags_t decltype_flag = (complain & tf_decltype);
7708 complain &= ~tf_decltype;
7709 /* No-Cleanup doesn't apply to nested calls either. */
7710 tsubst_flags_t no_cleanup_complain = complain;
7711 complain &= ~tf_no_cleanup;
7713 /* Find maximum size of vector to hold converted arguments. */
7714 parmlen = list_length (parm);
7715 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
7716 if (parmlen > nargs)
7717 nargs = parmlen;
7718 argarray = XALLOCAVEC (tree, nargs);
7720 /* The implicit parameters to a constructor are not considered by overload
7721 resolution, and must be of the proper type. */
7722 if (DECL_CONSTRUCTOR_P (fn))
7724 tree object_arg;
7725 if (first_arg != NULL_TREE)
7727 object_arg = first_arg;
7728 first_arg = NULL_TREE;
7730 else
7732 object_arg = (*args)[arg_index];
7733 ++arg_index;
7735 argarray[j++] = build_this (object_arg);
7736 parm = TREE_CHAIN (parm);
7737 /* We should never try to call the abstract constructor. */
7738 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
7740 if (DECL_HAS_VTT_PARM_P (fn))
7742 argarray[j++] = (*args)[arg_index];
7743 ++arg_index;
7744 parm = TREE_CHAIN (parm);
7747 if (flags & LOOKUP_PREFER_RVALUE)
7749 /* The implicit move specified in 15.8.3/3 fails "...if the type of
7750 the first parameter of the selected constructor is not an rvalue
7751 reference to the object’s type (possibly cv-qualified)...." */
7752 gcc_assert (!(complain & tf_error));
7753 tree ptype = convs[0]->type;
7754 if (TREE_CODE (ptype) != REFERENCE_TYPE
7755 || !TYPE_REF_IS_RVALUE (ptype)
7756 || CONVERSION_RANK (convs[0]) > cr_exact)
7757 return error_mark_node;
7760 /* Bypass access control for 'this' parameter. */
7761 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7763 tree parmtype = TREE_VALUE (parm);
7764 tree arg = build_this (first_arg != NULL_TREE
7765 ? first_arg
7766 : (*args)[arg_index]);
7767 tree argtype = TREE_TYPE (arg);
7768 tree converted_arg;
7769 tree base_binfo;
7771 if (arg == error_mark_node)
7772 return error_mark_node;
7774 if (convs[i]->bad_p)
7776 if (complain & tf_error)
7778 if (permerror (input_location, "passing %qT as %<this%> "
7779 "argument discards qualifiers",
7780 TREE_TYPE (argtype)))
7781 inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
7783 else
7784 return error_mark_node;
7787 /* See if the function member or the whole class type is declared
7788 final and the call can be devirtualized. */
7789 if (DECL_FINAL_P (fn)
7790 || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
7791 flags |= LOOKUP_NONVIRTUAL;
7793 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
7794 X is called for an object that is not of type X, or of a type
7795 derived from X, the behavior is undefined.
7797 So we can assume that anything passed as 'this' is non-null, and
7798 optimize accordingly. */
7799 gcc_assert (TYPE_PTR_P (parmtype));
7800 /* Convert to the base in which the function was declared. */
7801 gcc_assert (cand->conversion_path != NULL_TREE);
7802 converted_arg = build_base_path (PLUS_EXPR,
7803 arg,
7804 cand->conversion_path,
7805 1, complain);
7806 /* Check that the base class is accessible. */
7807 if (!accessible_base_p (TREE_TYPE (argtype),
7808 BINFO_TYPE (cand->conversion_path), true))
7810 if (complain & tf_error)
7811 error ("%qT is not an accessible base of %qT",
7812 BINFO_TYPE (cand->conversion_path),
7813 TREE_TYPE (argtype));
7814 else
7815 return error_mark_node;
7817 /* If fn was found by a using declaration, the conversion path
7818 will be to the derived class, not the base declaring fn. We
7819 must convert from derived to base. */
7820 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
7821 TREE_TYPE (parmtype), ba_unique,
7822 NULL, complain);
7823 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
7824 base_binfo, 1, complain);
7826 argarray[j++] = converted_arg;
7827 parm = TREE_CHAIN (parm);
7828 if (first_arg != NULL_TREE)
7829 first_arg = NULL_TREE;
7830 else
7831 ++arg_index;
7832 ++i;
7833 is_method = 1;
7836 gcc_assert (first_arg == NULL_TREE);
7837 for (; arg_index < vec_safe_length (args) && parm;
7838 parm = TREE_CHAIN (parm), ++arg_index, ++i)
7840 tree type = TREE_VALUE (parm);
7841 tree arg = (*args)[arg_index];
7842 bool conversion_warning = true;
7844 conv = convs[i];
7846 /* If the argument is NULL and used to (implicitly) instantiate a
7847 template function (and bind one of the template arguments to
7848 the type of 'long int'), we don't want to warn about passing NULL
7849 to non-pointer argument.
7850 For example, if we have this template function:
7852 template<typename T> void func(T x) {}
7854 we want to warn (when -Wconversion is enabled) in this case:
7856 void foo() {
7857 func<int>(NULL);
7860 but not in this case:
7862 void foo() {
7863 func(NULL);
7866 if (arg == null_node
7867 && DECL_TEMPLATE_INFO (fn)
7868 && cand->template_decl
7869 && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
7870 conversion_warning = false;
7872 /* Warn about initializer_list deduction that isn't currently in the
7873 working draft. */
7874 if (cxx_dialect > cxx98
7875 && flag_deduce_init_list
7876 && cand->template_decl
7877 && is_std_init_list (non_reference (type))
7878 && BRACE_ENCLOSED_INITIALIZER_P (arg))
7880 tree tmpl = TI_TEMPLATE (cand->template_decl);
7881 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
7882 tree patparm = get_pattern_parm (realparm, tmpl);
7883 tree pattype = TREE_TYPE (patparm);
7884 if (PACK_EXPANSION_P (pattype))
7885 pattype = PACK_EXPANSION_PATTERN (pattype);
7886 pattype = non_reference (pattype);
7888 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
7889 && (cand->explicit_targs == NULL_TREE
7890 || (TREE_VEC_LENGTH (cand->explicit_targs)
7891 <= TEMPLATE_TYPE_IDX (pattype))))
7893 pedwarn (input_location, 0, "deducing %qT as %qT",
7894 non_reference (TREE_TYPE (patparm)),
7895 non_reference (type));
7896 pedwarn (DECL_SOURCE_LOCATION (cand->fn), 0,
7897 " in call to %qD", cand->fn);
7898 pedwarn (input_location, 0,
7899 " (you can disable this with -fno-deduce-init-list)");
7903 /* Set user_conv_p on the argument conversions, so rvalue/base handling
7904 knows not to allow any more UDCs. This needs to happen after we
7905 process cand->warnings. */
7906 if (flags & LOOKUP_NO_CONVERSION)
7907 conv->user_conv_p = true;
7909 tsubst_flags_t arg_complain = complain;
7910 if (!conversion_warning)
7911 arg_complain &= ~tf_warning;
7913 val = convert_like_with_context (conv, arg, fn, i - is_method,
7914 arg_complain);
7915 val = convert_for_arg_passing (type, val, arg_complain);
7917 if (val == error_mark_node)
7918 return error_mark_node;
7919 else
7920 argarray[j++] = val;
7923 /* Default arguments */
7924 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
7926 if (TREE_VALUE (parm) == error_mark_node)
7927 return error_mark_node;
7928 val = convert_default_arg (TREE_VALUE (parm),
7929 TREE_PURPOSE (parm),
7930 fn, i - is_method,
7931 complain);
7932 if (val == error_mark_node)
7933 return error_mark_node;
7934 argarray[j++] = val;
7937 /* Ellipsis */
7938 int magic = magic_varargs_p (fn);
7939 for (; arg_index < vec_safe_length (args); ++arg_index)
7941 tree a = (*args)[arg_index];
7942 if ((magic == 3 && arg_index == 2) || magic == 2)
7944 /* Do no conversions for certain magic varargs. */
7945 a = mark_type_use (a);
7946 if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
7947 return error_mark_node;
7949 else if (magic != 0)
7950 /* For other magic varargs only do decay_conversion. */
7951 a = decay_conversion (a, complain);
7952 else if (DECL_CONSTRUCTOR_P (fn)
7953 && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
7954 TREE_TYPE (a)))
7956 /* Avoid infinite recursion trying to call A(...). */
7957 if (complain & tf_error)
7958 /* Try to call the actual copy constructor for a good error. */
7959 call_copy_ctor (a, complain);
7960 return error_mark_node;
7962 else
7963 a = convert_arg_to_ellipsis (a, complain);
7964 if (a == error_mark_node)
7965 return error_mark_node;
7966 argarray[j++] = a;
7969 gcc_assert (j <= nargs);
7970 nargs = j;
7972 /* Avoid to do argument-transformation, if warnings for format, and for
7973 nonnull are disabled. Just in case that at least one of them is active
7974 the check_function_arguments function might warn about something. */
7976 bool warned_p = false;
7977 if (warn_nonnull
7978 || warn_format
7979 || warn_suggest_attribute_format
7980 || warn_restrict)
7982 tree *fargs = (!nargs ? argarray
7983 : (tree *) alloca (nargs * sizeof (tree)));
7984 for (j = 0; j < nargs; j++)
7985 fargs[j] = maybe_constant_value (argarray[j]);
7987 warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
7988 nargs, fargs, NULL);
7991 if (DECL_INHERITED_CTOR (fn))
7993 /* Check for passing ellipsis arguments to an inherited constructor. We
7994 could handle this by open-coding the inherited constructor rather than
7995 defining it, but let's not bother now. */
7996 if (!cp_unevaluated_operand
7997 && cand->num_convs
7998 && cand->convs[cand->num_convs-1]->ellipsis_p)
8000 if (complain & tf_error)
8002 sorry ("passing arguments to ellipsis of inherited constructor "
8003 "%qD", cand->fn);
8004 inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
8006 return error_mark_node;
8009 /* A base constructor inheriting from a virtual base doesn't get the
8010 inherited arguments, just this and __vtt. */
8011 if (ctor_omit_inherited_parms (fn))
8012 nargs = 2;
8015 /* Avoid actually calling copy constructors and copy assignment operators,
8016 if possible. */
8018 if (! flag_elide_constructors)
8019 /* Do things the hard way. */;
8020 else if (cand->num_convs == 1
8021 && (DECL_COPY_CONSTRUCTOR_P (fn)
8022 || DECL_MOVE_CONSTRUCTOR_P (fn))
8023 /* It's unsafe to elide the constructor when handling
8024 a noexcept-expression, it may evaluate to the wrong
8025 value (c++/53025). */
8026 && cp_noexcept_operand == 0)
8028 tree targ;
8029 tree arg = argarray[num_artificial_parms_for (fn)];
8030 tree fa;
8031 bool trivial = trivial_fn_p (fn);
8033 /* Pull out the real argument, disregarding const-correctness. */
8034 targ = arg;
8035 /* Strip the reference binding for the constructor parameter. */
8036 if (CONVERT_EXPR_P (targ)
8037 && TREE_CODE (TREE_TYPE (targ)) == REFERENCE_TYPE)
8038 targ = TREE_OPERAND (targ, 0);
8039 /* But don't strip any other reference bindings; binding a temporary to a
8040 reference prevents copy elision. */
8041 while ((CONVERT_EXPR_P (targ)
8042 && TREE_CODE (TREE_TYPE (targ)) != REFERENCE_TYPE)
8043 || TREE_CODE (targ) == NON_LVALUE_EXPR)
8044 targ = TREE_OPERAND (targ, 0);
8045 if (TREE_CODE (targ) == ADDR_EXPR)
8047 targ = TREE_OPERAND (targ, 0);
8048 if (!same_type_ignoring_top_level_qualifiers_p
8049 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
8050 targ = NULL_TREE;
8052 else
8053 targ = NULL_TREE;
8055 if (targ)
8056 arg = targ;
8057 else
8058 arg = cp_build_fold_indirect_ref (arg);
8060 /* In C++17 we shouldn't be copying a TARGET_EXPR except into a base
8061 subobject. */
8062 if (CHECKING_P && cxx_dialect >= cxx17)
8063 gcc_assert (TREE_CODE (arg) != TARGET_EXPR
8064 /* It's from binding the ref parm to a packed field. */
8065 || convs[0]->need_temporary_p
8066 || seen_error ()
8067 /* See unsafe_copy_elision_p. */
8068 || DECL_BASE_CONSTRUCTOR_P (fn));
8070 /* [class.copy]: the copy constructor is implicitly defined even if
8071 the implementation elided its use. */
8072 if (!trivial || DECL_DELETED_FN (fn))
8074 if (!mark_used (fn, complain) && !(complain & tf_error))
8075 return error_mark_node;
8076 already_used = true;
8079 /* If we're creating a temp and we already have one, don't create a
8080 new one. If we're not creating a temp but we get one, use
8081 INIT_EXPR to collapse the temp into our target. Otherwise, if the
8082 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
8083 temp or an INIT_EXPR otherwise. */
8084 fa = argarray[0];
8085 if (is_dummy_object (fa))
8087 if (TREE_CODE (arg) == TARGET_EXPR)
8088 return arg;
8089 else if (trivial)
8090 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
8092 else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
8093 && !unsafe_copy_elision_p (fa, arg))
8095 tree to = cp_stabilize_reference (cp_build_fold_indirect_ref (fa));
8097 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
8098 return val;
8101 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
8102 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
8103 && trivial_fn_p (fn)
8104 && !DECL_DELETED_FN (fn))
8106 tree to = cp_stabilize_reference
8107 (cp_build_fold_indirect_ref (argarray[0]));
8108 tree type = TREE_TYPE (to);
8109 tree as_base = CLASSTYPE_AS_BASE (type);
8110 tree arg = argarray[1];
8112 if (is_really_empty_class (type))
8114 /* Avoid copying empty classes. */
8115 val = build2 (COMPOUND_EXPR, type, arg, to);
8116 TREE_NO_WARNING (val) = 1;
8118 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
8120 arg = cp_build_fold_indirect_ref (arg);
8121 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
8122 /* Handle NSDMI that refer to the object being initialized. */
8123 replace_placeholders (arg, to);
8125 else
8127 /* We must only copy the non-tail padding parts. */
8128 tree arg0, arg2, t;
8129 tree array_type, alias_set;
8131 arg2 = TYPE_SIZE_UNIT (as_base);
8132 arg0 = cp_build_addr_expr (to, complain);
8134 array_type = build_array_type (unsigned_char_type_node,
8135 build_index_type
8136 (size_binop (MINUS_EXPR,
8137 arg2, size_int (1))));
8138 alias_set = build_int_cst (build_pointer_type (type), 0);
8139 t = build2 (MODIFY_EXPR, void_type_node,
8140 build2 (MEM_REF, array_type, arg0, alias_set),
8141 build2 (MEM_REF, array_type, arg, alias_set));
8142 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
8143 TREE_NO_WARNING (val) = 1;
8146 return val;
8148 else if (!DECL_DELETED_FN (fn)
8149 && trivial_fn_p (fn))
8151 if (DECL_DESTRUCTOR_P (fn))
8152 return fold_convert (void_type_node, argarray[0]);
8153 else if (default_ctor_p (fn))
8155 if (is_dummy_object (argarray[0]))
8156 return force_target_expr (DECL_CONTEXT (fn), void_node,
8157 no_cleanup_complain);
8158 else
8159 return cp_build_fold_indirect_ref (argarray[0]);
8163 /* For calls to a multi-versioned function, overload resolution
8164 returns the function with the highest target priority, that is,
8165 the version that will checked for dispatching first. If this
8166 version is inlinable, a direct call to this version can be made
8167 otherwise the call should go through the dispatcher. */
8169 if (DECL_FUNCTION_VERSIONED (fn)
8170 && (current_function_decl == NULL
8171 || !targetm.target_option.can_inline_p (current_function_decl, fn)))
8173 fn = get_function_version_dispatcher (fn);
8174 if (fn == NULL)
8175 return NULL;
8176 if (!already_used)
8177 mark_versions_used (fn);
8180 if (!already_used
8181 && !mark_used (fn, complain))
8182 return error_mark_node;
8184 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
8185 /* Don't mess with virtual lookup in instantiate_non_dependent_expr;
8186 virtual functions can't be constexpr. */
8187 && !in_template_function ())
8189 tree t;
8190 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
8191 DECL_CONTEXT (fn),
8192 ba_any, NULL, complain);
8193 gcc_assert (binfo && binfo != error_mark_node);
8195 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
8196 complain);
8197 if (TREE_SIDE_EFFECTS (argarray[0]))
8198 argarray[0] = save_expr (argarray[0]);
8199 t = build_pointer_type (TREE_TYPE (fn));
8200 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
8201 TREE_TYPE (fn) = t;
8203 else
8205 fn = build_addr_func (fn, complain);
8206 if (fn == error_mark_node)
8207 return error_mark_node;
8210 tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
8211 if (call == error_mark_node)
8212 return call;
8213 if (cand->flags & LOOKUP_LIST_INIT_CTOR)
8215 tree c = extract_call_expr (call);
8216 /* build_new_op_1 will clear this when appropriate. */
8217 CALL_EXPR_ORDERED_ARGS (c) = true;
8219 if (warned_p)
8221 tree c = extract_call_expr (call);
8222 if (TREE_CODE (c) == CALL_EXPR)
8223 TREE_NO_WARNING (c) = 1;
8225 return call;
8228 /* Return the DECL of the first non-public data member of class TYPE
8229 or null if none can be found. */
8231 static tree
8232 first_non_public_field (tree type)
8234 if (!CLASS_TYPE_P (type))
8235 return NULL_TREE;
8237 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8239 if (TREE_CODE (field) != FIELD_DECL)
8240 continue;
8241 if (TREE_STATIC (field))
8242 continue;
8243 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
8244 return field;
8247 int i = 0;
8249 for (tree base_binfo, binfo = TYPE_BINFO (type);
8250 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8252 tree base = TREE_TYPE (base_binfo);
8254 if (tree field = first_non_public_field (base))
8255 return field;
8258 return NULL_TREE;
8261 /* Return true if all copy and move assignment operator overloads for
8262 class TYPE are trivial and at least one of them is not deleted and,
8263 when ACCESS is set, accessible. Return false otherwise. Set
8264 HASASSIGN to true when the TYPE has a (not necessarily trivial)
8265 copy or move assignment. */
8267 static bool
8268 has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
8270 tree fns = get_class_binding (type, assign_op_identifier);
8271 bool all_trivial = true;
8273 /* Iterate over overloads of the assignment operator, checking
8274 accessible copy assignments for triviality. */
8276 for (ovl_iterator oi (fns); oi; ++oi)
8278 tree f = *oi;
8280 /* Skip operators that aren't copy assignments. */
8281 if (!copy_fn_p (f))
8282 continue;
8284 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
8285 || accessible_p (TYPE_BINFO (type), f, true));
8287 /* Skip template assignment operators and deleted functions. */
8288 if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
8289 continue;
8291 if (accessible)
8292 *hasassign = true;
8294 if (!accessible || !trivial_fn_p (f))
8295 all_trivial = false;
8297 /* Break early when both properties have been determined. */
8298 if (*hasassign && !all_trivial)
8299 break;
8302 /* Return true if they're all trivial and one of the expressions
8303 TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
8304 tree ref = cp_build_reference_type (type, false);
8305 return (all_trivial
8306 && (is_trivially_xible (MODIFY_EXPR, type, type)
8307 || is_trivially_xible (MODIFY_EXPR, type, ref)));
8310 /* Return true if all copy and move ctor overloads for class TYPE are
8311 trivial and at least one of them is not deleted and, when ACCESS is
8312 set, accessible. Return false otherwise. Set each element of HASCTOR[]
8313 to true when the TYPE has a (not necessarily trivial) default and copy
8314 (or move) ctor, respectively. */
8316 static bool
8317 has_trivial_copy_p (tree type, bool access, bool hasctor[2])
8319 tree fns = get_class_binding (type, complete_ctor_identifier);
8320 bool all_trivial = true;
8322 for (ovl_iterator oi (fns); oi; ++oi)
8324 tree f = *oi;
8326 /* Skip template constructors. */
8327 if (TREE_CODE (f) != FUNCTION_DECL)
8328 continue;
8330 bool cpy_or_move_ctor_p = copy_fn_p (f);
8332 /* Skip ctors other than default, copy, and move. */
8333 if (!cpy_or_move_ctor_p && !default_ctor_p (f))
8334 continue;
8336 if (DECL_DELETED_FN (f))
8337 continue;
8339 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
8340 || accessible_p (TYPE_BINFO (type), f, true));
8342 if (accessible)
8343 hasctor[cpy_or_move_ctor_p] = true;
8345 if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
8346 all_trivial = false;
8348 /* Break early when both properties have been determined. */
8349 if (hasctor[0] && hasctor[1] && !all_trivial)
8350 break;
8353 return all_trivial;
8356 /* Issue a warning on a call to the built-in function FNDECL if it is
8357 a raw memory write whose destination is not an object of (something
8358 like) trivial or standard layout type with a non-deleted assignment
8359 and copy ctor. Detects const correctness violations, corrupting
8360 references, virtual table pointers, and bypassing non-trivial
8361 assignments. */
8363 static void
8364 maybe_warn_class_memaccess (location_t loc, tree fndecl, tree *args)
8366 /* Except for bcopy where it's second, the destination pointer is
8367 the first argument for all functions handled here. Compute
8368 the index of the destination and source arguments. */
8369 unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
8370 unsigned srcidx = !dstidx;
8372 tree dest = args[dstidx];
8373 if (!dest || !TREE_TYPE (dest) || !POINTER_TYPE_P (TREE_TYPE (dest)))
8374 return;
8376 /* Remove the outermost (usually implicit) conversion to the void*
8377 argument type. */
8378 if (TREE_CODE (dest) == NOP_EXPR)
8379 dest = TREE_OPERAND (dest, 0);
8381 tree srctype = NULL_TREE;
8383 /* Determine the type of the pointed-to object and whether it's
8384 a complete class type. */
8385 tree desttype = TREE_TYPE (TREE_TYPE (dest));
8387 if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
8388 return;
8390 /* Check to see if the raw memory call is made by a ctor or dtor
8391 with this as the destination argument for the destination type.
8392 If so, be more permissive. */
8393 if (current_function_decl
8394 && (DECL_CONSTRUCTOR_P (current_function_decl)
8395 || DECL_DESTRUCTOR_P (current_function_decl))
8396 && is_this_parameter (tree_strip_nop_conversions (dest)))
8398 tree ctx = DECL_CONTEXT (current_function_decl);
8399 bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
8401 tree binfo = TYPE_BINFO (ctx);
8403 /* A ctor and dtor for a class with no bases and no virtual functions
8404 can do whatever they want. Bail early with no further checking. */
8405 if (special && !BINFO_VTABLE (binfo) && !BINFO_N_BASE_BINFOS (binfo))
8406 return;
8409 /* True if the class is trivial. */
8410 bool trivial = trivial_type_p (desttype);
8412 /* Set to true if DESTYPE has an accessible copy assignment. */
8413 bool hasassign = false;
8414 /* True if all of the class' overloaded copy assignment operators
8415 are all trivial (and not deleted) and at least one of them is
8416 accessible. */
8417 bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
8419 /* Set to true if DESTTYPE has an accessible default and copy ctor,
8420 respectively. */
8421 bool hasctors[2] = { false, false };
8423 /* True if all of the class' overloaded copy constructors are all
8424 trivial (and not deleted) and at least one of them is accessible. */
8425 bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
8427 /* Set FLD to the first private/protected member of the class. */
8428 tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
8430 /* The warning format string. */
8431 const char *warnfmt = NULL;
8432 /* A suggested alternative to offer instead of the raw memory call.
8433 Empty string when none can be come up with. */
8434 const char *suggest = "";
8435 bool warned = false;
8437 switch (DECL_FUNCTION_CODE (fndecl))
8439 case BUILT_IN_MEMSET:
8440 if (!integer_zerop (args[1]))
8442 /* Diagnose setting non-copy-assignable or non-trivial types,
8443 or types with a private member, to (potentially) non-zero
8444 bytes. Since the value of the bytes being written is unknown,
8445 suggest using assignment instead (if one exists). Also warn
8446 for writes into objects for which zero-initialization doesn't
8447 mean all bits clear (pointer-to-member data, where null is all
8448 bits set). Since the value being written is (most likely)
8449 non-zero, simply suggest assignment (but not copy assignment). */
8450 suggest = "; use assignment instead";
8451 if (!trivassign)
8452 warnfmt = G_("%qD writing to an object of type %#qT with "
8453 "no trivial copy-assignment");
8454 else if (!trivial)
8455 warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
8456 else if (fld)
8458 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
8459 warned = warning_at (loc, OPT_Wclass_memaccess,
8460 "%qD writing to an object of type %#qT with "
8461 "%qs member %qD",
8462 fndecl, desttype, access, fld);
8464 else if (!zero_init_p (desttype))
8465 warnfmt = G_("%qD writing to an object of type %#qT containing "
8466 "a pointer to data member%s");
8468 break;
8470 /* Fall through. */
8472 case BUILT_IN_BZERO:
8473 /* Similarly to the above, diagnose clearing non-trivial or non-
8474 standard layout objects, or objects of types with no assignmenmt.
8475 Since the value being written is known to be zero, suggest either
8476 copy assignment, copy ctor, or default ctor as an alternative,
8477 depending on what's available. */
8479 if (hasassign && hasctors[0])
8480 suggest = G_("; use assignment or value-initialization instead");
8481 else if (hasassign)
8482 suggest = G_("; use assignment instead");
8483 else if (hasctors[0])
8484 suggest = G_("; use value-initialization instead");
8486 if (!trivassign)
8487 warnfmt = G_("%qD clearing an object of type %#qT with "
8488 "no trivial copy-assignment%s");
8489 else if (!trivial)
8490 warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
8491 else if (!zero_init_p (desttype))
8492 warnfmt = G_("%qD clearing an object of type %#qT containing "
8493 "a pointer-to-member%s");
8494 break;
8496 case BUILT_IN_BCOPY:
8497 case BUILT_IN_MEMCPY:
8498 case BUILT_IN_MEMMOVE:
8499 case BUILT_IN_MEMPCPY:
8500 /* Determine the type of the source object. */
8501 srctype = STRIP_NOPS (args[srcidx]);
8502 srctype = TREE_TYPE (TREE_TYPE (srctype));
8504 /* Since it's impossible to determine wheter the byte copy is
8505 being used in place of assignment to an existing object or
8506 as a substitute for initialization, assume it's the former.
8507 Determine the best alternative to use instead depending on
8508 what's not deleted. */
8509 if (hasassign && hasctors[1])
8510 suggest = G_("; use copy-assignment or copy-initialization instead");
8511 else if (hasassign)
8512 suggest = G_("; use copy-assignment instead");
8513 else if (hasctors[1])
8514 suggest = G_("; use copy-initialization instead");
8516 if (!trivassign)
8517 warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
8518 "copy-assignment%s");
8519 else if (!trivially_copyable_p (desttype))
8520 warnfmt = G_("%qD writing to an object of non-trivially copyable "
8521 "type %#qT%s");
8522 else if (!trivcopy)
8523 warnfmt = G_("%qD writing to an object with a deleted copy constructor");
8525 else if (!trivial
8526 && !VOID_TYPE_P (srctype)
8527 && !char_type_p (TYPE_MAIN_VARIANT (srctype))
8528 && !same_type_ignoring_top_level_qualifiers_p (desttype,
8529 srctype))
8531 /* Warn when copying into a non-trivial object from an object
8532 of a different type other than void or char. */
8533 warned = warning_at (loc, OPT_Wclass_memaccess,
8534 "%qD copying an object of non-trivial type "
8535 "%#qT from an array of %#qT",
8536 fndecl, desttype, srctype);
8538 else if (fld
8539 && !VOID_TYPE_P (srctype)
8540 && !char_type_p (TYPE_MAIN_VARIANT (srctype))
8541 && !same_type_ignoring_top_level_qualifiers_p (desttype,
8542 srctype))
8544 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
8545 warned = warning_at (loc, OPT_Wclass_memaccess,
8546 "%qD copying an object of type %#qT with "
8547 "%qs member %qD from an array of %#qT; use "
8548 "assignment or copy-initialization instead",
8549 fndecl, desttype, access, fld, srctype);
8551 else if (!trivial && TREE_CODE (args[2]) == INTEGER_CST)
8553 /* Finally, warn on partial copies. */
8554 unsigned HOST_WIDE_INT typesize
8555 = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
8556 if (unsigned HOST_WIDE_INT partial
8557 = tree_to_uhwi (args[2]) % typesize)
8558 warned = warning_at (loc, OPT_Wclass_memaccess,
8559 (typesize - partial > 1
8560 ? G_("%qD writing to an object of "
8561 "a non-trivial type %#qT leaves %wu "
8562 "bytes unchanged")
8563 : G_("%qD writing to an object of "
8564 "a non-trivial type %#qT leaves %wu "
8565 "byte unchanged")),
8566 fndecl, desttype, typesize - partial);
8568 break;
8570 case BUILT_IN_REALLOC:
8572 if (!trivially_copyable_p (desttype))
8573 warnfmt = G_("%qD moving an object of non-trivially copyable type "
8574 "%#qT; use %<new%> and %<delete%> instead");
8575 else if (!trivcopy)
8576 warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
8577 "constructor; use %<new%> and %<delete%> instead");
8578 else if (!get_dtor (desttype, tf_none))
8579 warnfmt = G_("%qD moving an object of type %#qT with deleted "
8580 "destructor");
8581 else if (!trivial
8582 && TREE_CODE (args[1]) == INTEGER_CST
8583 && tree_int_cst_lt (args[1], TYPE_SIZE_UNIT (desttype)))
8585 /* Finally, warn on reallocation into insufficient space. */
8586 warned = warning_at (loc, OPT_Wclass_memaccess,
8587 "%qD moving an object of non-trivial type "
8588 "%#qT and size %E into a region of size %E",
8589 fndecl, desttype, TYPE_SIZE_UNIT (desttype),
8590 args[1]);
8592 break;
8594 default:
8595 return;
8598 if (!warned && !warnfmt)
8599 return;
8601 if (warnfmt)
8603 if (suggest)
8604 warned = warning_at (loc, OPT_Wclass_memaccess,
8605 warnfmt, fndecl, desttype, suggest);
8606 else
8607 warned = warning_at (loc, OPT_Wclass_memaccess,
8608 warnfmt, fndecl, desttype);
8611 if (warned)
8612 inform (location_of (desttype), "%#qT declared here", desttype);
8615 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
8616 This function performs no overload resolution, conversion, or other
8617 high-level operations. */
8619 tree
8620 build_cxx_call (tree fn, int nargs, tree *argarray,
8621 tsubst_flags_t complain)
8623 tree fndecl;
8625 /* Remember roughly where this call is. */
8626 location_t loc = EXPR_LOC_OR_LOC (fn, input_location);
8627 fn = build_call_a (fn, nargs, argarray);
8628 SET_EXPR_LOCATION (fn, loc);
8630 fndecl = get_callee_fndecl (fn);
8632 /* Check that arguments to builtin functions match the expectations. */
8633 if (fndecl
8634 && DECL_BUILT_IN (fndecl)
8635 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
8637 int i;
8639 /* We need to take care that values to BUILT_IN_NORMAL
8640 are reduced. */
8641 for (i = 0; i < nargs; i++)
8642 argarray[i] = fold_non_dependent_expr (argarray[i]);
8644 if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
8645 nargs, argarray))
8646 return error_mark_node;
8648 /* Warn if the built-in writes to an object of a non-trivial type. */
8649 if (nargs)
8650 maybe_warn_class_memaccess (loc, fndecl, argarray);
8653 /* If it is a built-in array notation function, then the return type of
8654 the function is the element type of the array passed in as array
8655 notation (i.e. the first parameter of the function). */
8656 if (flag_cilkplus && TREE_CODE (fn) == CALL_EXPR)
8658 enum built_in_function bif =
8659 is_cilkplus_reduce_builtin (CALL_EXPR_FN (fn));
8660 if (bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
8661 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
8662 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
8663 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
8664 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE
8665 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
8667 if (call_expr_nargs (fn) == 0)
8669 error_at (EXPR_LOCATION (fn), "Invalid builtin arguments");
8670 return error_mark_node;
8672 /* for bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO or
8673 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO or
8674 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO or
8675 BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO or
8676 BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND or
8677 BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
8678 The pre-defined return-type is the correct one. */
8679 tree array_ntn = CALL_EXPR_ARG (fn, 0);
8680 TREE_TYPE (fn) = TREE_TYPE (array_ntn);
8681 return fn;
8685 if (VOID_TYPE_P (TREE_TYPE (fn)))
8686 return fn;
8688 /* 5.2.2/11: If a function call is a prvalue of object type: if the
8689 function call is either the operand of a decltype-specifier or the
8690 right operand of a comma operator that is the operand of a
8691 decltype-specifier, a temporary object is not introduced for the
8692 prvalue. The type of the prvalue may be incomplete. */
8693 if (!(complain & tf_decltype))
8695 fn = require_complete_type_sfinae (fn, complain);
8696 if (fn == error_mark_node)
8697 return error_mark_node;
8699 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
8701 fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
8702 maybe_warn_parm_abi (TREE_TYPE (fn), loc);
8705 return convert_from_reference (fn);
8708 /* Returns the value to use for the in-charge parameter when making a
8709 call to a function with the indicated NAME.
8711 FIXME:Can't we find a neater way to do this mapping? */
8713 tree
8714 in_charge_arg_for_name (tree name)
8716 if (IDENTIFIER_CTOR_P (name))
8718 if (name == complete_ctor_identifier)
8719 return integer_one_node;
8720 gcc_checking_assert (name == base_ctor_identifier);
8722 else
8724 if (name == complete_dtor_identifier)
8725 return integer_two_node;
8726 else if (name == deleting_dtor_identifier)
8727 return integer_three_node;
8728 gcc_checking_assert (name == base_dtor_identifier);
8731 return integer_zero_node;
8734 /* We've built up a constructor call RET. Complain if it delegates to the
8735 constructor we're currently compiling. */
8737 static void
8738 check_self_delegation (tree ret)
8740 if (TREE_CODE (ret) == TARGET_EXPR)
8741 ret = TARGET_EXPR_INITIAL (ret);
8742 tree fn = cp_get_callee_fndecl (ret);
8743 if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
8744 error ("constructor delegates to itself");
8747 /* Build a call to a constructor, destructor, or an assignment
8748 operator for INSTANCE, an expression with class type. NAME
8749 indicates the special member function to call; *ARGS are the
8750 arguments. ARGS may be NULL. This may change ARGS. BINFO
8751 indicates the base of INSTANCE that is to be passed as the `this'
8752 parameter to the member function called.
8754 FLAGS are the LOOKUP_* flags to use when processing the call.
8756 If NAME indicates a complete object constructor, INSTANCE may be
8757 NULL_TREE. In this case, the caller will call build_cplus_new to
8758 store the newly constructed object into a VAR_DECL. */
8760 tree
8761 build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
8762 tree binfo, int flags, tsubst_flags_t complain)
8764 tree fns;
8765 /* The type of the subobject to be constructed or destroyed. */
8766 tree class_type;
8767 vec<tree, va_gc> *allocated = NULL;
8768 tree ret;
8770 gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
8771 if (TYPE_P (binfo))
8773 /* Resolve the name. */
8774 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
8775 return error_mark_node;
8777 binfo = TYPE_BINFO (binfo);
8780 gcc_assert (binfo != NULL_TREE);
8782 class_type = BINFO_TYPE (binfo);
8784 /* Handle the special case where INSTANCE is NULL_TREE. */
8785 if (name == complete_ctor_identifier && !instance)
8786 instance = build_dummy_object (class_type);
8787 else
8789 if (IDENTIFIER_DTOR_P (name))
8790 gcc_assert (args == NULL || vec_safe_is_empty (*args));
8792 /* Convert to the base class, if necessary. */
8793 if (!same_type_ignoring_top_level_qualifiers_p
8794 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
8796 if (IDENTIFIER_CDTOR_P (name))
8797 /* For constructors and destructors, either the base is
8798 non-virtual, or it is virtual but we are doing the
8799 conversion from a constructor or destructor for the
8800 complete object. In either case, we can convert
8801 statically. */
8802 instance = convert_to_base_statically (instance, binfo);
8803 else
8805 /* However, for assignment operators, we must convert
8806 dynamically if the base is virtual. */
8807 gcc_checking_assert (name == assign_op_identifier);
8808 instance = build_base_path (PLUS_EXPR, instance,
8809 binfo, /*nonnull=*/1, complain);
8814 gcc_assert (instance != NULL_TREE);
8816 /* In C++17, "If the initializer expression is a prvalue and the
8817 cv-unqualified version of the source type is the same class as the class
8818 of the destination, the initializer expression is used to initialize the
8819 destination object." Handle that here to avoid doing overload
8820 resolution. */
8821 if (cxx_dialect >= cxx17
8822 && args && vec_safe_length (*args) == 1
8823 && name == complete_ctor_identifier)
8825 tree arg = (**args)[0];
8827 /* FIXME P0135 doesn't say how to handle direct initialization from a
8828 type with a suitable conversion operator. Let's handle it like
8829 copy-initialization, but allowing explict conversions. */
8830 tsubst_flags_t sub_complain = tf_warning;
8831 if (!is_dummy_object (instance))
8832 /* If we're using this to initialize a non-temporary object, don't
8833 require the destructor to be accessible. */
8834 sub_complain |= tf_no_cleanup;
8835 if (!reference_related_p (class_type, TREE_TYPE (arg)))
8836 arg = perform_implicit_conversion_flags (class_type, arg,
8837 sub_complain,
8838 flags);
8839 if ((TREE_CODE (arg) == TARGET_EXPR
8840 || TREE_CODE (arg) == CONSTRUCTOR)
8841 && (same_type_ignoring_top_level_qualifiers_p
8842 (class_type, TREE_TYPE (arg))))
8844 if (is_dummy_object (instance))
8845 return arg;
8846 if ((complain & tf_error)
8847 && (flags & LOOKUP_DELEGATING_CONS))
8848 check_self_delegation (arg);
8849 /* Avoid change of behavior on Wunused-var-2.C. */
8850 instance = mark_lvalue_use (instance);
8851 return build2 (INIT_EXPR, class_type, instance, arg);
8855 fns = lookup_fnfields (binfo, name, 1);
8857 /* When making a call to a constructor or destructor for a subobject
8858 that uses virtual base classes, pass down a pointer to a VTT for
8859 the subobject. */
8860 if ((name == base_ctor_identifier
8861 || name == base_dtor_identifier)
8862 && CLASSTYPE_VBASECLASSES (class_type))
8864 tree vtt;
8865 tree sub_vtt;
8867 /* If the current function is a complete object constructor
8868 or destructor, then we fetch the VTT directly.
8869 Otherwise, we look it up using the VTT we were given. */
8870 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
8871 vtt = decay_conversion (vtt, complain);
8872 if (vtt == error_mark_node)
8873 return error_mark_node;
8874 vtt = build_if_in_charge (vtt, current_vtt_parm);
8875 if (BINFO_SUBVTT_INDEX (binfo))
8876 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
8877 else
8878 sub_vtt = vtt;
8880 if (args == NULL)
8882 allocated = make_tree_vector ();
8883 args = &allocated;
8886 vec_safe_insert (*args, 0, sub_vtt);
8889 ret = build_new_method_call (instance, fns, args,
8890 TYPE_BINFO (BINFO_TYPE (binfo)),
8891 flags, /*fn=*/NULL,
8892 complain);
8894 if (allocated != NULL)
8895 release_tree_vector (allocated);
8897 if ((complain & tf_error)
8898 && (flags & LOOKUP_DELEGATING_CONS)
8899 && name == complete_ctor_identifier)
8900 check_self_delegation (ret);
8902 return ret;
8905 /* Return the NAME, as a C string. The NAME indicates a function that
8906 is a member of TYPE. *FREE_P is set to true if the caller must
8907 free the memory returned.
8909 Rather than go through all of this, we should simply set the names
8910 of constructors and destructors appropriately, and dispense with
8911 ctor_identifier, dtor_identifier, etc. */
8913 static char *
8914 name_as_c_string (tree name, tree type, bool *free_p)
8916 const char *pretty_name;
8918 /* Assume that we will not allocate memory. */
8919 *free_p = false;
8920 /* Constructors and destructors are special. */
8921 if (IDENTIFIER_CDTOR_P (name))
8923 pretty_name
8924 = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
8925 /* For a destructor, add the '~'. */
8926 if (IDENTIFIER_DTOR_P (name))
8928 pretty_name = concat ("~", pretty_name, NULL);
8929 /* Remember that we need to free the memory allocated. */
8930 *free_p = true;
8933 else if (IDENTIFIER_CONV_OP_P (name))
8935 pretty_name = concat ("operator ",
8936 type_as_string_translate (TREE_TYPE (name),
8937 TFF_PLAIN_IDENTIFIER),
8938 NULL);
8939 /* Remember that we need to free the memory allocated. */
8940 *free_p = true;
8942 else
8943 pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
8945 return CONST_CAST (char *, pretty_name);
8948 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
8949 be set, upon return, to the function called. ARGS may be NULL.
8950 This may change ARGS. */
8952 static tree
8953 build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
8954 tree conversion_path, int flags,
8955 tree *fn_p, tsubst_flags_t complain)
8957 struct z_candidate *candidates = 0, *cand;
8958 tree explicit_targs = NULL_TREE;
8959 tree basetype = NULL_TREE;
8960 tree access_binfo, binfo;
8961 tree optype;
8962 tree first_mem_arg = NULL_TREE;
8963 tree name;
8964 bool skip_first_for_error;
8965 vec<tree, va_gc> *user_args;
8966 tree call;
8967 tree fn;
8968 int template_only = 0;
8969 bool any_viable_p;
8970 tree orig_instance;
8971 tree orig_fns;
8972 vec<tree, va_gc> *orig_args = NULL;
8973 void *p;
8975 gcc_assert (instance != NULL_TREE);
8977 /* We don't know what function we're going to call, yet. */
8978 if (fn_p)
8979 *fn_p = NULL_TREE;
8981 if (error_operand_p (instance)
8982 || !fns || error_operand_p (fns))
8983 return error_mark_node;
8985 if (!BASELINK_P (fns))
8987 if (complain & tf_error)
8988 error ("call to non-function %qD", fns);
8989 return error_mark_node;
8992 orig_instance = instance;
8993 orig_fns = fns;
8995 /* Dismantle the baselink to collect all the information we need. */
8996 if (!conversion_path)
8997 conversion_path = BASELINK_BINFO (fns);
8998 access_binfo = BASELINK_ACCESS_BINFO (fns);
8999 binfo = BASELINK_BINFO (fns);
9000 optype = BASELINK_OPTYPE (fns);
9001 fns = BASELINK_FUNCTIONS (fns);
9002 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
9004 explicit_targs = TREE_OPERAND (fns, 1);
9005 fns = TREE_OPERAND (fns, 0);
9006 template_only = 1;
9008 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
9009 || TREE_CODE (fns) == TEMPLATE_DECL
9010 || TREE_CODE (fns) == OVERLOAD);
9011 fn = OVL_FIRST (fns);
9012 name = DECL_NAME (fn);
9014 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
9015 gcc_assert (CLASS_TYPE_P (basetype));
9017 if (processing_template_decl)
9019 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
9020 instance = build_non_dependent_expr (instance);
9021 if (args != NULL)
9022 make_args_non_dependent (*args);
9025 user_args = args == NULL ? NULL : *args;
9026 /* Under DR 147 A::A() is an invalid constructor call,
9027 not a functional cast. */
9028 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
9030 if (! (complain & tf_error))
9031 return error_mark_node;
9033 basetype = DECL_CONTEXT (fn);
9034 name = constructor_name (basetype);
9035 if (permerror (input_location,
9036 "cannot call constructor %<%T::%D%> directly",
9037 basetype, name))
9038 inform (input_location, "for a function-style cast, remove the "
9039 "redundant %<::%D%>", name);
9040 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
9041 complain);
9042 return call;
9045 /* Process the argument list. */
9046 if (args != NULL && *args != NULL)
9048 *args = resolve_args (*args, complain);
9049 if (*args == NULL)
9050 return error_mark_node;
9053 /* Consider the object argument to be used even if we end up selecting a
9054 static member function. */
9055 instance = mark_type_use (instance);
9057 /* Figure out whether to skip the first argument for the error
9058 message we will display to users if an error occurs. We don't
9059 want to display any compiler-generated arguments. The "this"
9060 pointer hasn't been added yet. However, we must remove the VTT
9061 pointer if this is a call to a base-class constructor or
9062 destructor. */
9063 skip_first_for_error = false;
9064 if (IDENTIFIER_CDTOR_P (name))
9066 /* Callers should explicitly indicate whether they want to ctor
9067 the complete object or just the part without virtual bases. */
9068 gcc_assert (name != ctor_identifier);
9070 /* Remove the VTT pointer, if present. */
9071 if ((name == base_ctor_identifier || name == base_dtor_identifier)
9072 && CLASSTYPE_VBASECLASSES (basetype))
9073 skip_first_for_error = true;
9075 /* It's OK to call destructors and constructors on cv-qualified
9076 objects. Therefore, convert the INSTANCE to the unqualified
9077 type, if necessary. */
9078 if (!same_type_p (basetype, TREE_TYPE (instance)))
9080 instance = build_this (instance);
9081 instance = build_nop (build_pointer_type (basetype), instance);
9082 instance = build_fold_indirect_ref (instance);
9085 else
9086 gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
9088 /* For the overload resolution we need to find the actual `this`
9089 that would be captured if the call turns out to be to a
9090 non-static member function. Do not actually capture it at this
9091 point. */
9092 if (DECL_CONSTRUCTOR_P (fn))
9093 /* Constructors don't use the enclosing 'this'. */
9094 first_mem_arg = instance;
9095 else
9096 first_mem_arg = maybe_resolve_dummy (instance, false);
9098 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9099 p = conversion_obstack_alloc (0);
9101 /* The number of arguments artificial parms in ARGS; we subtract one because
9102 there's no 'this' in ARGS. */
9103 unsigned skip = num_artificial_parms_for (fn) - 1;
9105 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
9106 initializer, not T({ }). */
9107 if (DECL_CONSTRUCTOR_P (fn)
9108 && vec_safe_length (user_args) > skip
9109 && DIRECT_LIST_INIT_P ((*user_args)[skip]))
9111 tree init_list = (*user_args)[skip];
9112 tree init = NULL_TREE;
9114 gcc_assert (user_args->length () == skip + 1
9115 && !(flags & LOOKUP_ONLYCONVERTING));
9117 /* If the initializer list has no elements and T is a class type with
9118 a default constructor, the object is value-initialized. Handle
9119 this here so we don't need to handle it wherever we use
9120 build_special_member_call. */
9121 if (CONSTRUCTOR_NELTS (init_list) == 0
9122 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
9123 /* For a user-provided default constructor, use the normal
9124 mechanisms so that protected access works. */
9125 && type_has_non_user_provided_default_constructor (basetype)
9126 && !processing_template_decl)
9127 init = build_value_init (basetype, complain);
9129 /* If BASETYPE is an aggregate, we need to do aggregate
9130 initialization. */
9131 else if (CP_AGGREGATE_TYPE_P (basetype))
9133 init = reshape_init (basetype, init_list, complain);
9134 init = digest_init (basetype, init, complain);
9137 if (init)
9139 if (is_dummy_object (instance))
9140 return get_target_expr_sfinae (init, complain);
9141 init = build2 (INIT_EXPR, TREE_TYPE (instance), instance, init);
9142 TREE_SIDE_EFFECTS (init) = true;
9143 return init;
9146 /* Otherwise go ahead with overload resolution. */
9147 add_list_candidates (fns, first_mem_arg, user_args,
9148 basetype, explicit_targs, template_only,
9149 conversion_path, access_binfo, flags,
9150 &candidates, complain);
9152 else
9153 add_candidates (fns, first_mem_arg, user_args, optype,
9154 explicit_targs, template_only, conversion_path,
9155 access_binfo, flags, &candidates, complain);
9157 any_viable_p = false;
9158 candidates = splice_viable (candidates, false, &any_viable_p);
9160 if (!any_viable_p)
9162 if (complain & tf_error)
9164 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
9165 cxx_incomplete_type_error (instance, basetype);
9166 else if (optype)
9167 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
9168 basetype, optype, build_tree_list_vec (user_args),
9169 TREE_TYPE (instance));
9170 else
9172 tree arglist = build_tree_list_vec (user_args);
9173 tree errname = name;
9174 bool twiddle = false;
9175 if (IDENTIFIER_CDTOR_P (errname))
9177 twiddle = IDENTIFIER_DTOR_P (errname);
9178 errname = constructor_name (basetype);
9180 if (explicit_targs)
9181 errname = lookup_template_function (errname, explicit_targs);
9182 if (skip_first_for_error)
9183 arglist = TREE_CHAIN (arglist);
9184 error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
9185 basetype, &"~"[!twiddle], errname, arglist,
9186 TREE_TYPE (instance));
9188 print_z_candidates (location_of (name), candidates);
9190 call = error_mark_node;
9192 else
9194 cand = tourney (candidates, complain);
9195 if (cand == 0)
9197 char *pretty_name;
9198 bool free_p;
9199 tree arglist;
9201 if (complain & tf_error)
9203 pretty_name = name_as_c_string (name, basetype, &free_p);
9204 arglist = build_tree_list_vec (user_args);
9205 if (skip_first_for_error)
9206 arglist = TREE_CHAIN (arglist);
9207 if (!any_strictly_viable (candidates))
9208 error ("no matching function for call to %<%s(%A)%>",
9209 pretty_name, arglist);
9210 else
9211 error ("call of overloaded %<%s(%A)%> is ambiguous",
9212 pretty_name, arglist);
9213 print_z_candidates (location_of (name), candidates);
9214 if (free_p)
9215 free (pretty_name);
9217 call = error_mark_node;
9219 else
9221 fn = cand->fn;
9222 call = NULL_TREE;
9224 if (!(flags & LOOKUP_NONVIRTUAL)
9225 && DECL_PURE_VIRTUAL_P (fn)
9226 && instance == current_class_ref
9227 && (complain & tf_warning))
9229 /* This is not an error, it is runtime undefined
9230 behavior. */
9231 if (!current_function_decl)
9232 warning (0, "pure virtual %q#D called from "
9233 "non-static data member initializer", fn);
9234 else if (DECL_CONSTRUCTOR_P (current_function_decl)
9235 || DECL_DESTRUCTOR_P (current_function_decl))
9236 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
9237 ? G_("pure virtual %q#D called from constructor")
9238 : G_("pure virtual %q#D called from destructor")),
9239 fn);
9242 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
9243 && !DECL_CONSTRUCTOR_P (fn)
9244 && is_dummy_object (instance))
9246 instance = maybe_resolve_dummy (instance, true);
9247 if (instance == error_mark_node)
9248 call = error_mark_node;
9249 else if (!is_dummy_object (instance))
9251 /* We captured 'this' in the current lambda now that
9252 we know we really need it. */
9253 cand->first_arg = instance;
9255 else if (any_dependent_bases_p ())
9256 /* We can't tell until instantiation time whether we can use
9257 *this as the implicit object argument. */;
9258 else
9260 if (complain & tf_error)
9261 error ("cannot call member function %qD without object",
9262 fn);
9263 call = error_mark_node;
9267 if (call != error_mark_node)
9269 /* Optimize away vtable lookup if we know that this
9270 function can't be overridden. We need to check if
9271 the context and the type where we found fn are the same,
9272 actually FN might be defined in a different class
9273 type because of a using-declaration. In this case, we
9274 do not want to perform a non-virtual call. */
9275 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
9276 && same_type_ignoring_top_level_qualifiers_p
9277 (DECL_CONTEXT (fn), BINFO_TYPE (binfo))
9278 && resolves_to_fixed_type_p (instance, 0))
9279 flags |= LOOKUP_NONVIRTUAL;
9280 if (explicit_targs)
9281 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
9282 /* Now we know what function is being called. */
9283 if (fn_p)
9284 *fn_p = fn;
9285 /* Build the actual CALL_EXPR. */
9286 call = build_over_call (cand, flags, complain);
9287 /* In an expression of the form `a->f()' where `f' turns
9288 out to be a static member function, `a' is
9289 none-the-less evaluated. */
9290 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
9291 && !is_dummy_object (instance)
9292 && TREE_SIDE_EFFECTS (instance))
9293 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
9294 instance, call);
9295 else if (call != error_mark_node
9296 && DECL_DESTRUCTOR_P (cand->fn)
9297 && !VOID_TYPE_P (TREE_TYPE (call)))
9298 /* An explicit call of the form "x->~X()" has type
9299 "void". However, on platforms where destructors
9300 return "this" (i.e., those where
9301 targetm.cxx.cdtor_returns_this is true), such calls
9302 will appear to have a return value of pointer type
9303 to the low-level call machinery. We do not want to
9304 change the low-level machinery, since we want to be
9305 able to optimize "delete f()" on such platforms as
9306 "operator delete(~X(f()))" (rather than generating
9307 "t = f(), ~X(t), operator delete (t)"). */
9308 call = build_nop (void_type_node, call);
9313 if (processing_template_decl && call != error_mark_node)
9315 bool cast_to_void = false;
9317 if (TREE_CODE (call) == COMPOUND_EXPR)
9318 call = TREE_OPERAND (call, 1);
9319 else if (TREE_CODE (call) == NOP_EXPR)
9321 cast_to_void = true;
9322 call = TREE_OPERAND (call, 0);
9324 if (INDIRECT_REF_P (call))
9325 call = TREE_OPERAND (call, 0);
9326 call = (build_min_non_dep_call_vec
9327 (call,
9328 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
9329 orig_instance, orig_fns, NULL_TREE),
9330 orig_args));
9331 SET_EXPR_LOCATION (call, input_location);
9332 call = convert_from_reference (call);
9333 if (cast_to_void)
9334 call = build_nop (void_type_node, call);
9337 /* Free all the conversions we allocated. */
9338 obstack_free (&conversion_obstack, p);
9340 if (orig_args != NULL)
9341 release_tree_vector (orig_args);
9343 return call;
9346 /* Wrapper for above. */
9348 tree
9349 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
9350 tree conversion_path, int flags,
9351 tree *fn_p, tsubst_flags_t complain)
9353 tree ret;
9354 bool subtime = timevar_cond_start (TV_OVERLOAD);
9355 ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
9356 fn_p, complain);
9357 timevar_cond_stop (TV_OVERLOAD, subtime);
9358 return ret;
9361 /* Returns true iff standard conversion sequence ICS1 is a proper
9362 subsequence of ICS2. */
9364 static bool
9365 is_subseq (conversion *ics1, conversion *ics2)
9367 /* We can assume that a conversion of the same code
9368 between the same types indicates a subsequence since we only get
9369 here if the types we are converting from are the same. */
9371 while (ics1->kind == ck_rvalue
9372 || ics1->kind == ck_lvalue)
9373 ics1 = next_conversion (ics1);
9375 while (1)
9377 while (ics2->kind == ck_rvalue
9378 || ics2->kind == ck_lvalue)
9379 ics2 = next_conversion (ics2);
9381 if (ics2->kind == ck_user
9382 || ics2->kind == ck_ambig
9383 || ics2->kind == ck_aggr
9384 || ics2->kind == ck_list
9385 || ics2->kind == ck_identity)
9386 /* At this point, ICS1 cannot be a proper subsequence of
9387 ICS2. We can get a USER_CONV when we are comparing the
9388 second standard conversion sequence of two user conversion
9389 sequences. */
9390 return false;
9392 ics2 = next_conversion (ics2);
9394 while (ics2->kind == ck_rvalue
9395 || ics2->kind == ck_lvalue)
9396 ics2 = next_conversion (ics2);
9398 if (ics2->kind == ics1->kind
9399 && same_type_p (ics2->type, ics1->type)
9400 && (ics1->kind == ck_identity
9401 || same_type_p (next_conversion (ics2)->type,
9402 next_conversion (ics1)->type)))
9403 return true;
9407 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
9408 be any _TYPE nodes. */
9410 bool
9411 is_properly_derived_from (tree derived, tree base)
9413 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
9414 return false;
9416 /* We only allow proper derivation here. The DERIVED_FROM_P macro
9417 considers every class derived from itself. */
9418 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
9419 && DERIVED_FROM_P (base, derived));
9422 /* We build the ICS for an implicit object parameter as a pointer
9423 conversion sequence. However, such a sequence should be compared
9424 as if it were a reference conversion sequence. If ICS is the
9425 implicit conversion sequence for an implicit object parameter,
9426 modify it accordingly. */
9428 static void
9429 maybe_handle_implicit_object (conversion **ics)
9431 if ((*ics)->this_p)
9433 /* [over.match.funcs]
9435 For non-static member functions, the type of the
9436 implicit object parameter is "reference to cv X"
9437 where X is the class of which the function is a
9438 member and cv is the cv-qualification on the member
9439 function declaration. */
9440 conversion *t = *ics;
9441 tree reference_type;
9443 /* The `this' parameter is a pointer to a class type. Make the
9444 implicit conversion talk about a reference to that same class
9445 type. */
9446 reference_type = TREE_TYPE (t->type);
9447 reference_type = build_reference_type (reference_type);
9449 if (t->kind == ck_qual)
9450 t = next_conversion (t);
9451 if (t->kind == ck_ptr)
9452 t = next_conversion (t);
9453 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
9454 t = direct_reference_binding (reference_type, t);
9455 t->this_p = 1;
9456 t->rvaluedness_matches_p = 0;
9457 *ics = t;
9461 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
9462 and return the initial reference binding conversion. Otherwise,
9463 leave *ICS unchanged and return NULL. */
9465 static conversion *
9466 maybe_handle_ref_bind (conversion **ics)
9468 if ((*ics)->kind == ck_ref_bind)
9470 conversion *old_ics = *ics;
9471 *ics = next_conversion (old_ics);
9472 (*ics)->user_conv_p = old_ics->user_conv_p;
9473 return old_ics;
9476 return NULL;
9479 /* Compare two implicit conversion sequences according to the rules set out in
9480 [over.ics.rank]. Return values:
9482 1: ics1 is better than ics2
9483 -1: ics2 is better than ics1
9484 0: ics1 and ics2 are indistinguishable */
9486 static int
9487 compare_ics (conversion *ics1, conversion *ics2)
9489 tree from_type1;
9490 tree from_type2;
9491 tree to_type1;
9492 tree to_type2;
9493 tree deref_from_type1 = NULL_TREE;
9494 tree deref_from_type2 = NULL_TREE;
9495 tree deref_to_type1 = NULL_TREE;
9496 tree deref_to_type2 = NULL_TREE;
9497 conversion_rank rank1, rank2;
9499 /* REF_BINDING is nonzero if the result of the conversion sequence
9500 is a reference type. In that case REF_CONV is the reference
9501 binding conversion. */
9502 conversion *ref_conv1;
9503 conversion *ref_conv2;
9505 /* Compare badness before stripping the reference conversion. */
9506 if (ics1->bad_p > ics2->bad_p)
9507 return -1;
9508 else if (ics1->bad_p < ics2->bad_p)
9509 return 1;
9511 /* Handle implicit object parameters. */
9512 maybe_handle_implicit_object (&ics1);
9513 maybe_handle_implicit_object (&ics2);
9515 /* Handle reference parameters. */
9516 ref_conv1 = maybe_handle_ref_bind (&ics1);
9517 ref_conv2 = maybe_handle_ref_bind (&ics2);
9519 /* List-initialization sequence L1 is a better conversion sequence than
9520 list-initialization sequence L2 if L1 converts to
9521 std::initializer_list<X> for some X and L2 does not. */
9522 if (ics1->kind == ck_list && ics2->kind != ck_list)
9523 return 1;
9524 if (ics2->kind == ck_list && ics1->kind != ck_list)
9525 return -1;
9527 /* [over.ics.rank]
9529 When comparing the basic forms of implicit conversion sequences (as
9530 defined in _over.best.ics_)
9532 --a standard conversion sequence (_over.ics.scs_) is a better
9533 conversion sequence than a user-defined conversion sequence
9534 or an ellipsis conversion sequence, and
9536 --a user-defined conversion sequence (_over.ics.user_) is a
9537 better conversion sequence than an ellipsis conversion sequence
9538 (_over.ics.ellipsis_). */
9539 /* Use BAD_CONVERSION_RANK because we already checked for a badness
9540 mismatch. If both ICS are bad, we try to make a decision based on
9541 what would have happened if they'd been good. This is not an
9542 extension, we'll still give an error when we build up the call; this
9543 just helps us give a more helpful error message. */
9544 rank1 = BAD_CONVERSION_RANK (ics1);
9545 rank2 = BAD_CONVERSION_RANK (ics2);
9547 if (rank1 > rank2)
9548 return -1;
9549 else if (rank1 < rank2)
9550 return 1;
9552 if (ics1->ellipsis_p)
9553 /* Both conversions are ellipsis conversions. */
9554 return 0;
9556 /* User-defined conversion sequence U1 is a better conversion sequence
9557 than another user-defined conversion sequence U2 if they contain the
9558 same user-defined conversion operator or constructor and if the sec-
9559 ond standard conversion sequence of U1 is better than the second
9560 standard conversion sequence of U2. */
9562 /* Handle list-conversion with the same code even though it isn't always
9563 ranked as a user-defined conversion and it doesn't have a second
9564 standard conversion sequence; it will still have the desired effect.
9565 Specifically, we need to do the reference binding comparison at the
9566 end of this function. */
9568 if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
9570 conversion *t1;
9571 conversion *t2;
9573 for (t1 = ics1; t1->kind != ck_user; t1 = next_conversion (t1))
9574 if (t1->kind == ck_ambig || t1->kind == ck_aggr
9575 || t1->kind == ck_list)
9576 break;
9577 for (t2 = ics2; t2->kind != ck_user; t2 = next_conversion (t2))
9578 if (t2->kind == ck_ambig || t2->kind == ck_aggr
9579 || t2->kind == ck_list)
9580 break;
9582 if (t1->kind != t2->kind)
9583 return 0;
9584 else if (t1->kind == ck_user)
9586 tree f1 = t1->cand ? t1->cand->fn : t1->type;
9587 tree f2 = t2->cand ? t2->cand->fn : t2->type;
9588 if (f1 != f2)
9589 return 0;
9591 else
9593 /* For ambiguous or aggregate conversions, use the target type as
9594 a proxy for the conversion function. */
9595 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
9596 return 0;
9599 /* We can just fall through here, after setting up
9600 FROM_TYPE1 and FROM_TYPE2. */
9601 from_type1 = t1->type;
9602 from_type2 = t2->type;
9604 else
9606 conversion *t1;
9607 conversion *t2;
9609 /* We're dealing with two standard conversion sequences.
9611 [over.ics.rank]
9613 Standard conversion sequence S1 is a better conversion
9614 sequence than standard conversion sequence S2 if
9616 --S1 is a proper subsequence of S2 (comparing the conversion
9617 sequences in the canonical form defined by _over.ics.scs_,
9618 excluding any Lvalue Transformation; the identity
9619 conversion sequence is considered to be a subsequence of
9620 any non-identity conversion sequence */
9622 t1 = ics1;
9623 while (t1->kind != ck_identity)
9624 t1 = next_conversion (t1);
9625 from_type1 = t1->type;
9627 t2 = ics2;
9628 while (t2->kind != ck_identity)
9629 t2 = next_conversion (t2);
9630 from_type2 = t2->type;
9633 /* One sequence can only be a subsequence of the other if they start with
9634 the same type. They can start with different types when comparing the
9635 second standard conversion sequence in two user-defined conversion
9636 sequences. */
9637 if (same_type_p (from_type1, from_type2))
9639 if (is_subseq (ics1, ics2))
9640 return 1;
9641 if (is_subseq (ics2, ics1))
9642 return -1;
9645 /* [over.ics.rank]
9647 Or, if not that,
9649 --the rank of S1 is better than the rank of S2 (by the rules
9650 defined below):
9652 Standard conversion sequences are ordered by their ranks: an Exact
9653 Match is a better conversion than a Promotion, which is a better
9654 conversion than a Conversion.
9656 Two conversion sequences with the same rank are indistinguishable
9657 unless one of the following rules applies:
9659 --A conversion that does not a convert a pointer, pointer to member,
9660 or std::nullptr_t to bool is better than one that does.
9662 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
9663 so that we do not have to check it explicitly. */
9664 if (ics1->rank < ics2->rank)
9665 return 1;
9666 else if (ics2->rank < ics1->rank)
9667 return -1;
9669 to_type1 = ics1->type;
9670 to_type2 = ics2->type;
9672 /* A conversion from scalar arithmetic type to complex is worse than a
9673 conversion between scalar arithmetic types. */
9674 if (same_type_p (from_type1, from_type2)
9675 && ARITHMETIC_TYPE_P (from_type1)
9676 && ARITHMETIC_TYPE_P (to_type1)
9677 && ARITHMETIC_TYPE_P (to_type2)
9678 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
9679 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
9681 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
9682 return -1;
9683 else
9684 return 1;
9687 if (TYPE_PTR_P (from_type1)
9688 && TYPE_PTR_P (from_type2)
9689 && TYPE_PTR_P (to_type1)
9690 && TYPE_PTR_P (to_type2))
9692 deref_from_type1 = TREE_TYPE (from_type1);
9693 deref_from_type2 = TREE_TYPE (from_type2);
9694 deref_to_type1 = TREE_TYPE (to_type1);
9695 deref_to_type2 = TREE_TYPE (to_type2);
9697 /* The rules for pointers to members A::* are just like the rules
9698 for pointers A*, except opposite: if B is derived from A then
9699 A::* converts to B::*, not vice versa. For that reason, we
9700 switch the from_ and to_ variables here. */
9701 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
9702 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
9703 || (TYPE_PTRMEMFUNC_P (from_type1)
9704 && TYPE_PTRMEMFUNC_P (from_type2)
9705 && TYPE_PTRMEMFUNC_P (to_type1)
9706 && TYPE_PTRMEMFUNC_P (to_type2)))
9708 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
9709 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
9710 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
9711 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
9714 if (deref_from_type1 != NULL_TREE
9715 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
9716 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
9718 /* This was one of the pointer or pointer-like conversions.
9720 [over.ics.rank]
9722 --If class B is derived directly or indirectly from class A,
9723 conversion of B* to A* is better than conversion of B* to
9724 void*, and conversion of A* to void* is better than
9725 conversion of B* to void*. */
9726 if (VOID_TYPE_P (deref_to_type1)
9727 && VOID_TYPE_P (deref_to_type2))
9729 if (is_properly_derived_from (deref_from_type1,
9730 deref_from_type2))
9731 return -1;
9732 else if (is_properly_derived_from (deref_from_type2,
9733 deref_from_type1))
9734 return 1;
9736 else if (VOID_TYPE_P (deref_to_type1)
9737 || VOID_TYPE_P (deref_to_type2))
9739 if (same_type_p (deref_from_type1, deref_from_type2))
9741 if (VOID_TYPE_P (deref_to_type2))
9743 if (is_properly_derived_from (deref_from_type1,
9744 deref_to_type1))
9745 return 1;
9747 /* We know that DEREF_TO_TYPE1 is `void' here. */
9748 else if (is_properly_derived_from (deref_from_type1,
9749 deref_to_type2))
9750 return -1;
9753 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
9754 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
9756 /* [over.ics.rank]
9758 --If class B is derived directly or indirectly from class A
9759 and class C is derived directly or indirectly from B,
9761 --conversion of C* to B* is better than conversion of C* to
9764 --conversion of B* to A* is better than conversion of C* to
9765 A* */
9766 if (same_type_p (deref_from_type1, deref_from_type2))
9768 if (is_properly_derived_from (deref_to_type1,
9769 deref_to_type2))
9770 return 1;
9771 else if (is_properly_derived_from (deref_to_type2,
9772 deref_to_type1))
9773 return -1;
9775 else if (same_type_p (deref_to_type1, deref_to_type2))
9777 if (is_properly_derived_from (deref_from_type2,
9778 deref_from_type1))
9779 return 1;
9780 else if (is_properly_derived_from (deref_from_type1,
9781 deref_from_type2))
9782 return -1;
9786 else if (CLASS_TYPE_P (non_reference (from_type1))
9787 && same_type_p (from_type1, from_type2))
9789 tree from = non_reference (from_type1);
9791 /* [over.ics.rank]
9793 --binding of an expression of type C to a reference of type
9794 B& is better than binding an expression of type C to a
9795 reference of type A&
9797 --conversion of C to B is better than conversion of C to A, */
9798 if (is_properly_derived_from (from, to_type1)
9799 && is_properly_derived_from (from, to_type2))
9801 if (is_properly_derived_from (to_type1, to_type2))
9802 return 1;
9803 else if (is_properly_derived_from (to_type2, to_type1))
9804 return -1;
9807 else if (CLASS_TYPE_P (non_reference (to_type1))
9808 && same_type_p (to_type1, to_type2))
9810 tree to = non_reference (to_type1);
9812 /* [over.ics.rank]
9814 --binding of an expression of type B to a reference of type
9815 A& is better than binding an expression of type C to a
9816 reference of type A&,
9818 --conversion of B to A is better than conversion of C to A */
9819 if (is_properly_derived_from (from_type1, to)
9820 && is_properly_derived_from (from_type2, to))
9822 if (is_properly_derived_from (from_type2, from_type1))
9823 return 1;
9824 else if (is_properly_derived_from (from_type1, from_type2))
9825 return -1;
9829 /* [over.ics.rank]
9831 --S1 and S2 differ only in their qualification conversion and yield
9832 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
9833 qualification signature of type T1 is a proper subset of the cv-
9834 qualification signature of type T2 */
9835 if (ics1->kind == ck_qual
9836 && ics2->kind == ck_qual
9837 && same_type_p (from_type1, from_type2))
9839 int result = comp_cv_qual_signature (to_type1, to_type2);
9840 if (result != 0)
9841 return result;
9844 /* [over.ics.rank]
9846 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
9847 to an implicit object parameter of a non-static member function
9848 declared without a ref-qualifier, and either S1 binds an lvalue
9849 reference to an lvalue and S2 binds an rvalue reference or S1 binds an
9850 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
9851 draft standard, 13.3.3.2)
9853 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
9854 types to which the references refer are the same type except for
9855 top-level cv-qualifiers, and the type to which the reference
9856 initialized by S2 refers is more cv-qualified than the type to
9857 which the reference initialized by S1 refers.
9859 DR 1328 [over.match.best]: the context is an initialization by
9860 conversion function for direct reference binding (13.3.1.6) of a
9861 reference to function type, the return type of F1 is the same kind of
9862 reference (i.e. lvalue or rvalue) as the reference being initialized,
9863 and the return type of F2 is not. */
9865 if (ref_conv1 && ref_conv2)
9867 if (!ref_conv1->this_p && !ref_conv2->this_p
9868 && (ref_conv1->rvaluedness_matches_p
9869 != ref_conv2->rvaluedness_matches_p)
9870 && (same_type_p (ref_conv1->type, ref_conv2->type)
9871 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
9872 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
9874 if (ref_conv1->bad_p
9875 && !same_type_p (TREE_TYPE (ref_conv1->type),
9876 TREE_TYPE (ref_conv2->type)))
9877 /* Don't prefer a bad conversion that drops cv-quals to a bad
9878 conversion with the wrong rvalueness. */
9879 return 0;
9880 return (ref_conv1->rvaluedness_matches_p
9881 - ref_conv2->rvaluedness_matches_p);
9884 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
9886 int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
9887 int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
9888 if (ref_conv1->bad_p)
9890 /* Prefer the one that drops fewer cv-quals. */
9891 tree ftype = next_conversion (ref_conv1)->type;
9892 int fquals = cp_type_quals (ftype);
9893 q1 ^= fquals;
9894 q2 ^= fquals;
9896 return comp_cv_qualification (q2, q1);
9900 /* Neither conversion sequence is better than the other. */
9901 return 0;
9904 /* The source type for this standard conversion sequence. */
9906 static tree
9907 source_type (conversion *t)
9909 for (;; t = next_conversion (t))
9911 if (t->kind == ck_user
9912 || t->kind == ck_ambig
9913 || t->kind == ck_identity)
9914 return t->type;
9916 gcc_unreachable ();
9919 /* Note a warning about preferring WINNER to LOSER. We do this by storing
9920 a pointer to LOSER and re-running joust to produce the warning if WINNER
9921 is actually used. */
9923 static void
9924 add_warning (struct z_candidate *winner, struct z_candidate *loser)
9926 candidate_warning *cw = (candidate_warning *)
9927 conversion_obstack_alloc (sizeof (candidate_warning));
9928 cw->loser = loser;
9929 cw->next = winner->warnings;
9930 winner->warnings = cw;
9933 /* Compare two candidates for overloading as described in
9934 [over.match.best]. Return values:
9936 1: cand1 is better than cand2
9937 -1: cand2 is better than cand1
9938 0: cand1 and cand2 are indistinguishable */
9940 static int
9941 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
9942 tsubst_flags_t complain)
9944 int winner = 0;
9945 int off1 = 0, off2 = 0;
9946 size_t i;
9947 size_t len;
9949 /* Candidates that involve bad conversions are always worse than those
9950 that don't. */
9951 if (cand1->viable > cand2->viable)
9952 return 1;
9953 if (cand1->viable < cand2->viable)
9954 return -1;
9956 /* If we have two pseudo-candidates for conversions to the same type,
9957 or two candidates for the same function, arbitrarily pick one. */
9958 if (cand1->fn == cand2->fn
9959 && (IS_TYPE_OR_DECL_P (cand1->fn)))
9960 return 1;
9962 /* Prefer a non-deleted function over an implicitly deleted move
9963 constructor or assignment operator. This differs slightly from the
9964 wording for issue 1402 (which says the move op is ignored by overload
9965 resolution), but this way produces better error messages. */
9966 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
9967 && TREE_CODE (cand2->fn) == FUNCTION_DECL
9968 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
9970 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
9971 && move_fn_p (cand1->fn))
9972 return -1;
9973 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
9974 && move_fn_p (cand2->fn))
9975 return 1;
9978 /* a viable function F1
9979 is defined to be a better function than another viable function F2 if
9980 for all arguments i, ICSi(F1) is not a worse conversion sequence than
9981 ICSi(F2), and then */
9983 /* for some argument j, ICSj(F1) is a better conversion sequence than
9984 ICSj(F2) */
9986 /* For comparing static and non-static member functions, we ignore
9987 the implicit object parameter of the non-static function. The
9988 standard says to pretend that the static function has an object
9989 parm, but that won't work with operator overloading. */
9990 len = cand1->num_convs;
9991 if (len != cand2->num_convs)
9993 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
9994 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
9996 if (DECL_CONSTRUCTOR_P (cand1->fn)
9997 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
9998 /* We're comparing a near-match list constructor and a near-match
9999 non-list constructor. Just treat them as unordered. */
10000 return 0;
10002 gcc_assert (static_1 != static_2);
10004 if (static_1)
10005 off2 = 1;
10006 else
10008 off1 = 1;
10009 --len;
10013 for (i = 0; i < len; ++i)
10015 conversion *t1 = cand1->convs[i + off1];
10016 conversion *t2 = cand2->convs[i + off2];
10017 int comp = compare_ics (t1, t2);
10019 if (comp != 0)
10021 if ((complain & tf_warning)
10022 && warn_sign_promo
10023 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
10024 == cr_std + cr_promotion)
10025 && t1->kind == ck_std
10026 && t2->kind == ck_std
10027 && TREE_CODE (t1->type) == INTEGER_TYPE
10028 && TREE_CODE (t2->type) == INTEGER_TYPE
10029 && (TYPE_PRECISION (t1->type)
10030 == TYPE_PRECISION (t2->type))
10031 && (TYPE_UNSIGNED (next_conversion (t1)->type)
10032 || (TREE_CODE (next_conversion (t1)->type)
10033 == ENUMERAL_TYPE)))
10035 tree type = next_conversion (t1)->type;
10036 tree type1, type2;
10037 struct z_candidate *w, *l;
10038 if (comp > 0)
10039 type1 = t1->type, type2 = t2->type,
10040 w = cand1, l = cand2;
10041 else
10042 type1 = t2->type, type2 = t1->type,
10043 w = cand2, l = cand1;
10045 if (warn)
10047 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
10048 type, type1, type2);
10049 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
10051 else
10052 add_warning (w, l);
10055 if (winner && comp != winner)
10057 winner = 0;
10058 goto tweak;
10060 winner = comp;
10064 /* warn about confusing overload resolution for user-defined conversions,
10065 either between a constructor and a conversion op, or between two
10066 conversion ops. */
10067 if ((complain & tf_warning)
10068 && winner && warn_conversion && cand1->second_conv
10069 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
10070 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
10072 struct z_candidate *w, *l;
10073 bool give_warning = false;
10075 if (winner == 1)
10076 w = cand1, l = cand2;
10077 else
10078 w = cand2, l = cand1;
10080 /* We don't want to complain about `X::operator T1 ()'
10081 beating `X::operator T2 () const', when T2 is a no less
10082 cv-qualified version of T1. */
10083 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
10084 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
10086 tree t = TREE_TYPE (TREE_TYPE (l->fn));
10087 tree f = TREE_TYPE (TREE_TYPE (w->fn));
10089 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
10091 t = TREE_TYPE (t);
10092 f = TREE_TYPE (f);
10094 if (!comp_ptr_ttypes (t, f))
10095 give_warning = true;
10097 else
10098 give_warning = true;
10100 if (!give_warning)
10101 /*NOP*/;
10102 else if (warn)
10104 tree source = source_type (w->convs[0]);
10105 if (! DECL_CONSTRUCTOR_P (w->fn))
10106 source = TREE_TYPE (source);
10107 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
10108 && warning (OPT_Wconversion, " for conversion from %qH to %qI",
10109 source, w->second_conv->type))
10111 inform (input_location, " because conversion sequence for the argument is better");
10114 else
10115 add_warning (w, l);
10118 if (winner)
10119 return winner;
10121 /* DR 495 moved this tiebreaker above the template ones. */
10122 /* or, if not that,
10123 the context is an initialization by user-defined conversion (see
10124 _dcl.init_ and _over.match.user_) and the standard conversion
10125 sequence from the return type of F1 to the destination type (i.e.,
10126 the type of the entity being initialized) is a better conversion
10127 sequence than the standard conversion sequence from the return type
10128 of F2 to the destination type. */
10130 if (cand1->second_conv)
10132 winner = compare_ics (cand1->second_conv, cand2->second_conv);
10133 if (winner)
10134 return winner;
10137 /* or, if not that,
10138 F1 is a non-template function and F2 is a template function
10139 specialization. */
10141 if (!cand1->template_decl && cand2->template_decl)
10142 return 1;
10143 else if (cand1->template_decl && !cand2->template_decl)
10144 return -1;
10146 /* or, if not that,
10147 F1 and F2 are template functions and the function template for F1 is
10148 more specialized than the template for F2 according to the partial
10149 ordering rules. */
10151 if (cand1->template_decl && cand2->template_decl)
10153 winner = more_specialized_fn
10154 (TI_TEMPLATE (cand1->template_decl),
10155 TI_TEMPLATE (cand2->template_decl),
10156 /* [temp.func.order]: The presence of unused ellipsis and default
10157 arguments has no effect on the partial ordering of function
10158 templates. add_function_candidate() will not have
10159 counted the "this" argument for constructors. */
10160 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
10161 if (winner)
10162 return winner;
10165 // C++ Concepts
10166 // or, if not that, F1 is more constrained than F2.
10167 if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn))
10169 winner = more_constrained (cand1->fn, cand2->fn);
10170 if (winner)
10171 return winner;
10174 /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
10175 if (deduction_guide_p (cand1->fn))
10177 gcc_assert (deduction_guide_p (cand2->fn));
10178 /* We distinguish between candidates from an explicit deduction guide and
10179 candidates built from a constructor based on DECL_ARTIFICIAL. */
10180 int art1 = DECL_ARTIFICIAL (cand1->fn);
10181 int art2 = DECL_ARTIFICIAL (cand2->fn);
10182 if (art1 != art2)
10183 return art2 - art1;
10185 if (art1)
10187 /* Prefer the special copy guide over a declared copy/move
10188 constructor. */
10189 if (copy_guide_p (cand1->fn))
10190 return 1;
10191 if (copy_guide_p (cand2->fn))
10192 return -1;
10194 /* Prefer a candidate generated from a non-template constructor. */
10195 int tg1 = template_guide_p (cand1->fn);
10196 int tg2 = template_guide_p (cand2->fn);
10197 if (tg1 != tg2)
10198 return tg2 - tg1;
10202 /* F1 is a member of a class D, F2 is a member of a base class B of D, and
10203 for all arguments the corresponding parameters of F1 and F2 have the same
10204 type (CWG 2273/2277). */
10205 if (DECL_P (cand1->fn) && DECL_CLASS_SCOPE_P (cand1->fn)
10206 && !DECL_CONV_FN_P (cand1->fn)
10207 && DECL_P (cand2->fn) && DECL_CLASS_SCOPE_P (cand2->fn)
10208 && !DECL_CONV_FN_P (cand2->fn))
10210 tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
10211 tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
10213 bool used1 = false;
10214 bool used2 = false;
10215 if (base1 == base2)
10216 /* No difference. */;
10217 else if (DERIVED_FROM_P (base1, base2))
10218 used1 = true;
10219 else if (DERIVED_FROM_P (base2, base1))
10220 used2 = true;
10222 if (int diff = used2 - used1)
10224 for (i = 0; i < len; ++i)
10226 conversion *t1 = cand1->convs[i + off1];
10227 conversion *t2 = cand2->convs[i + off2];
10228 if (!same_type_p (t1->type, t2->type))
10229 break;
10231 if (i == len)
10232 return diff;
10236 /* Check whether we can discard a builtin candidate, either because we
10237 have two identical ones or matching builtin and non-builtin candidates.
10239 (Pedantically in the latter case the builtin which matched the user
10240 function should not be added to the overload set, but we spot it here.
10242 [over.match.oper]
10243 ... the builtin candidates include ...
10244 - do not have the same parameter type list as any non-template
10245 non-member candidate. */
10247 if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
10249 for (i = 0; i < len; ++i)
10250 if (!same_type_p (cand1->convs[i]->type,
10251 cand2->convs[i]->type))
10252 break;
10253 if (i == cand1->num_convs)
10255 if (cand1->fn == cand2->fn)
10256 /* Two built-in candidates; arbitrarily pick one. */
10257 return 1;
10258 else if (identifier_p (cand1->fn))
10259 /* cand1 is built-in; prefer cand2. */
10260 return -1;
10261 else
10262 /* cand2 is built-in; prefer cand1. */
10263 return 1;
10267 /* For candidates of a multi-versioned function, make the version with
10268 the highest priority win. This version will be checked for dispatching
10269 first. If this version can be inlined into the caller, the front-end
10270 will simply make a direct call to this function. */
10272 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
10273 && DECL_FUNCTION_VERSIONED (cand1->fn)
10274 && TREE_CODE (cand2->fn) == FUNCTION_DECL
10275 && DECL_FUNCTION_VERSIONED (cand2->fn))
10277 tree f1 = TREE_TYPE (cand1->fn);
10278 tree f2 = TREE_TYPE (cand2->fn);
10279 tree p1 = TYPE_ARG_TYPES (f1);
10280 tree p2 = TYPE_ARG_TYPES (f2);
10282 /* Check if cand1->fn and cand2->fn are versions of the same function. It
10283 is possible that cand1->fn and cand2->fn are function versions but of
10284 different functions. Check types to see if they are versions of the same
10285 function. */
10286 if (compparms (p1, p2)
10287 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
10289 /* Always make the version with the higher priority, more
10290 specialized, win. */
10291 gcc_assert (targetm.compare_version_priority);
10292 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
10293 return 1;
10294 else
10295 return -1;
10299 /* If the two function declarations represent the same function (this can
10300 happen with declarations in multiple scopes and arg-dependent lookup),
10301 arbitrarily choose one. But first make sure the default args we're
10302 using match. */
10303 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
10304 && equal_functions (cand1->fn, cand2->fn))
10306 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
10307 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
10309 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
10311 for (i = 0; i < len; ++i)
10313 /* Don't crash if the fn is variadic. */
10314 if (!parms1)
10315 break;
10316 parms1 = TREE_CHAIN (parms1);
10317 parms2 = TREE_CHAIN (parms2);
10320 if (off1)
10321 parms1 = TREE_CHAIN (parms1);
10322 else if (off2)
10323 parms2 = TREE_CHAIN (parms2);
10325 for (; parms1; ++i)
10327 if (!cp_tree_equal (TREE_PURPOSE (parms1),
10328 TREE_PURPOSE (parms2)))
10330 if (warn)
10332 if (complain & tf_error)
10334 if (permerror (input_location,
10335 "default argument mismatch in "
10336 "overload resolution"))
10338 inform (DECL_SOURCE_LOCATION (cand1->fn),
10339 " candidate 1: %q#F", cand1->fn);
10340 inform (DECL_SOURCE_LOCATION (cand2->fn),
10341 " candidate 2: %q#F", cand2->fn);
10344 else
10345 return 0;
10347 else
10348 add_warning (cand1, cand2);
10349 break;
10351 parms1 = TREE_CHAIN (parms1);
10352 parms2 = TREE_CHAIN (parms2);
10355 return 1;
10358 tweak:
10360 /* Extension: If the worst conversion for one candidate is worse than the
10361 worst conversion for the other, take the first. */
10362 if (!pedantic && (complain & tf_warning_or_error))
10364 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
10365 struct z_candidate *w = 0, *l = 0;
10367 for (i = 0; i < len; ++i)
10369 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
10370 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
10371 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
10372 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
10374 if (rank1 < rank2)
10375 winner = 1, w = cand1, l = cand2;
10376 if (rank1 > rank2)
10377 winner = -1, w = cand2, l = cand1;
10378 if (winner)
10380 /* Don't choose a deleted function over ambiguity. */
10381 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
10382 return 0;
10383 if (warn)
10385 pedwarn (input_location, 0,
10386 "ISO C++ says that these are ambiguous, even "
10387 "though the worst conversion for the first is better than "
10388 "the worst conversion for the second:");
10389 print_z_candidate (input_location, _("candidate 1:"), w);
10390 print_z_candidate (input_location, _("candidate 2:"), l);
10392 else
10393 add_warning (w, l);
10394 return winner;
10398 gcc_assert (!winner);
10399 return 0;
10402 /* Given a list of candidates for overloading, find the best one, if any.
10403 This algorithm has a worst case of O(2n) (winner is last), and a best
10404 case of O(n/2) (totally ambiguous); much better than a sorting
10405 algorithm. */
10407 static struct z_candidate *
10408 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
10410 struct z_candidate *champ = candidates, *challenger;
10411 int fate;
10412 int champ_compared_to_predecessor = 0;
10414 /* Walk through the list once, comparing each current champ to the next
10415 candidate, knocking out a candidate or two with each comparison. */
10417 for (challenger = champ->next; challenger; )
10419 fate = joust (champ, challenger, 0, complain);
10420 if (fate == 1)
10421 challenger = challenger->next;
10422 else
10424 if (fate == 0)
10426 champ = challenger->next;
10427 if (champ == 0)
10428 return NULL;
10429 champ_compared_to_predecessor = 0;
10431 else
10433 champ = challenger;
10434 champ_compared_to_predecessor = 1;
10437 challenger = champ->next;
10441 /* Make sure the champ is better than all the candidates it hasn't yet
10442 been compared to. */
10444 for (challenger = candidates;
10445 challenger != champ
10446 && !(champ_compared_to_predecessor && challenger->next == champ);
10447 challenger = challenger->next)
10449 fate = joust (champ, challenger, 0, complain);
10450 if (fate != 1)
10451 return NULL;
10454 return champ;
10457 /* Returns nonzero if things of type FROM can be converted to TO. */
10459 bool
10460 can_convert (tree to, tree from, tsubst_flags_t complain)
10462 tree arg = NULL_TREE;
10463 /* implicit_conversion only considers user-defined conversions
10464 if it has an expression for the call argument list. */
10465 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
10466 arg = build1 (CAST_EXPR, from, NULL_TREE);
10467 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
10470 /* Returns nonzero if things of type FROM can be converted to TO with a
10471 standard conversion. */
10473 bool
10474 can_convert_standard (tree to, tree from, tsubst_flags_t complain)
10476 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
10479 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
10481 bool
10482 can_convert_arg (tree to, tree from, tree arg, int flags,
10483 tsubst_flags_t complain)
10485 conversion *t;
10486 void *p;
10487 bool ok_p;
10489 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10490 p = conversion_obstack_alloc (0);
10491 /* We want to discard any access checks done for this test,
10492 as we might not be in the appropriate access context and
10493 we'll do the check again when we actually perform the
10494 conversion. */
10495 push_deferring_access_checks (dk_deferred);
10497 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
10498 flags, complain);
10499 ok_p = (t && !t->bad_p);
10501 /* Discard the access checks now. */
10502 pop_deferring_access_checks ();
10503 /* Free all the conversions we allocated. */
10504 obstack_free (&conversion_obstack, p);
10506 return ok_p;
10509 /* Like can_convert_arg, but allows dubious conversions as well. */
10511 bool
10512 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
10513 tsubst_flags_t complain)
10515 conversion *t;
10516 void *p;
10518 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10519 p = conversion_obstack_alloc (0);
10520 /* Try to perform the conversion. */
10521 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
10522 flags, complain);
10523 /* Free all the conversions we allocated. */
10524 obstack_free (&conversion_obstack, p);
10526 return t != NULL;
10529 /* Convert EXPR to TYPE. Return the converted expression.
10531 Note that we allow bad conversions here because by the time we get to
10532 this point we are committed to doing the conversion. If we end up
10533 doing a bad conversion, convert_like will complain. */
10535 tree
10536 perform_implicit_conversion_flags (tree type, tree expr,
10537 tsubst_flags_t complain, int flags)
10539 conversion *conv;
10540 void *p;
10541 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
10543 if (error_operand_p (expr))
10544 return error_mark_node;
10546 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10547 p = conversion_obstack_alloc (0);
10549 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10550 /*c_cast_p=*/false,
10551 flags, complain);
10553 if (!conv)
10555 if (complain & tf_error)
10557 /* If expr has unknown type, then it is an overloaded function.
10558 Call instantiate_type to get good error messages. */
10559 if (TREE_TYPE (expr) == unknown_type_node)
10560 instantiate_type (type, expr, complain);
10561 else if (invalid_nonstatic_memfn_p (loc, expr, complain))
10562 /* We gave an error. */;
10563 else
10564 error_at (loc, "could not convert %qE from %qH to %qI", expr,
10565 TREE_TYPE (expr), type);
10567 expr = error_mark_node;
10569 else if (processing_template_decl && conv->kind != ck_identity)
10571 /* In a template, we are only concerned about determining the
10572 type of non-dependent expressions, so we do not have to
10573 perform the actual conversion. But for initializers, we
10574 need to be able to perform it at instantiation
10575 (or instantiate_non_dependent_expr) time. */
10576 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
10577 if (!(flags & LOOKUP_ONLYCONVERTING))
10578 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
10580 else
10581 expr = convert_like (conv, expr, complain);
10583 /* Free all the conversions we allocated. */
10584 obstack_free (&conversion_obstack, p);
10586 return expr;
10589 tree
10590 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
10592 return perform_implicit_conversion_flags (type, expr, complain,
10593 LOOKUP_IMPLICIT);
10596 /* Convert EXPR to TYPE (as a direct-initialization) if that is
10597 permitted. If the conversion is valid, the converted expression is
10598 returned. Otherwise, NULL_TREE is returned, except in the case
10599 that TYPE is a class type; in that case, an error is issued. If
10600 C_CAST_P is true, then this direct-initialization is taking
10601 place as part of a static_cast being attempted as part of a C-style
10602 cast. */
10604 tree
10605 perform_direct_initialization_if_possible (tree type,
10606 tree expr,
10607 bool c_cast_p,
10608 tsubst_flags_t complain)
10610 conversion *conv;
10611 void *p;
10613 if (type == error_mark_node || error_operand_p (expr))
10614 return error_mark_node;
10615 /* [dcl.init]
10617 If the destination type is a (possibly cv-qualified) class type:
10619 -- If the initialization is direct-initialization ...,
10620 constructors are considered. ... If no constructor applies, or
10621 the overload resolution is ambiguous, the initialization is
10622 ill-formed. */
10623 if (CLASS_TYPE_P (type))
10625 vec<tree, va_gc> *args = make_tree_vector_single (expr);
10626 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
10627 &args, type, LOOKUP_NORMAL, complain);
10628 release_tree_vector (args);
10629 return build_cplus_new (type, expr, complain);
10632 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10633 p = conversion_obstack_alloc (0);
10635 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10636 c_cast_p,
10637 LOOKUP_NORMAL, complain);
10638 if (!conv || conv->bad_p)
10639 expr = NULL_TREE;
10640 else if (processing_template_decl && conv->kind != ck_identity)
10642 /* In a template, we are only concerned about determining the
10643 type of non-dependent expressions, so we do not have to
10644 perform the actual conversion. But for initializers, we
10645 need to be able to perform it at instantiation
10646 (or instantiate_non_dependent_expr) time. */
10647 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
10648 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
10650 else
10651 expr = convert_like_real (conv, expr, NULL_TREE, 0,
10652 /*issue_conversion_warnings=*/false,
10653 c_cast_p,
10654 complain);
10656 /* Free all the conversions we allocated. */
10657 obstack_free (&conversion_obstack, p);
10659 return expr;
10662 /* When initializing a reference that lasts longer than a full-expression,
10663 this special rule applies:
10665 [class.temporary]
10667 The temporary to which the reference is bound or the temporary
10668 that is the complete object to which the reference is bound
10669 persists for the lifetime of the reference.
10671 The temporaries created during the evaluation of the expression
10672 initializing the reference, except the temporary to which the
10673 reference is bound, are destroyed at the end of the
10674 full-expression in which they are created.
10676 In that case, we store the converted expression into a new
10677 VAR_DECL in a new scope.
10679 However, we want to be careful not to create temporaries when
10680 they are not required. For example, given:
10682 struct B {};
10683 struct D : public B {};
10684 D f();
10685 const B& b = f();
10687 there is no need to copy the return value from "f"; we can just
10688 extend its lifetime. Similarly, given:
10690 struct S {};
10691 struct T { operator S(); };
10692 T t;
10693 const S& s = t;
10695 we can extend the lifetime of the return value of the conversion
10696 operator.
10698 The next several functions are involved in this lifetime extension. */
10700 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
10701 reference is being bound to a temporary. Create and return a new
10702 VAR_DECL with the indicated TYPE; this variable will store the value to
10703 which the reference is bound. */
10705 tree
10706 make_temporary_var_for_ref_to_temp (tree decl, tree type)
10708 tree var = create_temporary_var (type);
10710 /* Register the variable. */
10711 if (VAR_P (decl)
10712 && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
10714 /* Namespace-scope or local static; give it a mangled name. */
10715 /* FIXME share comdat with decl? */
10717 TREE_STATIC (var) = TREE_STATIC (decl);
10718 CP_DECL_THREAD_LOCAL_P (var) = CP_DECL_THREAD_LOCAL_P (decl);
10719 set_decl_tls_model (var, DECL_TLS_MODEL (decl));
10721 tree name = mangle_ref_init_variable (decl);
10722 DECL_NAME (var) = name;
10723 SET_DECL_ASSEMBLER_NAME (var, name);
10725 var = pushdecl (var);
10727 else
10728 /* Create a new cleanup level if necessary. */
10729 maybe_push_cleanup_level (type);
10731 return var;
10734 /* EXPR is the initializer for a variable DECL of reference or
10735 std::initializer_list type. Create, push and return a new VAR_DECL
10736 for the initializer so that it will live as long as DECL. Any
10737 cleanup for the new variable is returned through CLEANUP, and the
10738 code to initialize the new variable is returned through INITP. */
10740 static tree
10741 set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
10742 tree *initp)
10744 tree init;
10745 tree type;
10746 tree var;
10748 /* Create the temporary variable. */
10749 type = TREE_TYPE (expr);
10750 var = make_temporary_var_for_ref_to_temp (decl, type);
10751 layout_decl (var, 0);
10752 /* If the rvalue is the result of a function call it will be
10753 a TARGET_EXPR. If it is some other construct (such as a
10754 member access expression where the underlying object is
10755 itself the result of a function call), turn it into a
10756 TARGET_EXPR here. It is important that EXPR be a
10757 TARGET_EXPR below since otherwise the INIT_EXPR will
10758 attempt to make a bitwise copy of EXPR to initialize
10759 VAR. */
10760 if (TREE_CODE (expr) != TARGET_EXPR)
10761 expr = get_target_expr (expr);
10763 if (TREE_CODE (decl) == FIELD_DECL
10764 && extra_warnings && !TREE_NO_WARNING (decl))
10766 warning (OPT_Wextra, "a temporary bound to %qD only persists "
10767 "until the constructor exits", decl);
10768 TREE_NO_WARNING (decl) = true;
10771 /* Recursively extend temps in this initializer. */
10772 TARGET_EXPR_INITIAL (expr)
10773 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups);
10775 /* Any reference temp has a non-trivial initializer. */
10776 DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
10778 /* If the initializer is constant, put it in DECL_INITIAL so we get
10779 static initialization and use in constant expressions. */
10780 init = maybe_constant_init (expr);
10781 if (TREE_CONSTANT (init))
10783 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
10785 /* 5.19 says that a constant expression can include an
10786 lvalue-rvalue conversion applied to "a glvalue of literal type
10787 that refers to a non-volatile temporary object initialized
10788 with a constant expression". Rather than try to communicate
10789 that this VAR_DECL is a temporary, just mark it constexpr.
10791 Currently this is only useful for initializer_list temporaries,
10792 since reference vars can't appear in constant expressions. */
10793 DECL_DECLARED_CONSTEXPR_P (var) = true;
10794 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
10795 TREE_CONSTANT (var) = true;
10797 DECL_INITIAL (var) = init;
10798 init = NULL_TREE;
10800 else
10801 /* Create the INIT_EXPR that will initialize the temporary
10802 variable. */
10803 init = split_nonconstant_init (var, expr);
10804 if (at_function_scope_p ())
10806 add_decl_expr (var);
10808 if (TREE_STATIC (var))
10809 init = add_stmt_to_compound (init, register_dtor_fn (var));
10810 else
10812 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
10813 if (cleanup)
10814 vec_safe_push (*cleanups, cleanup);
10817 /* We must be careful to destroy the temporary only
10818 after its initialization has taken place. If the
10819 initialization throws an exception, then the
10820 destructor should not be run. We cannot simply
10821 transform INIT into something like:
10823 (INIT, ({ CLEANUP_STMT; }))
10825 because emit_local_var always treats the
10826 initializer as a full-expression. Thus, the
10827 destructor would run too early; it would run at the
10828 end of initializing the reference variable, rather
10829 than at the end of the block enclosing the
10830 reference variable.
10832 The solution is to pass back a cleanup expression
10833 which the caller is responsible for attaching to
10834 the statement tree. */
10836 else
10838 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
10839 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
10841 if (CP_DECL_THREAD_LOCAL_P (var))
10842 tls_aggregates = tree_cons (NULL_TREE, var,
10843 tls_aggregates);
10844 else
10845 static_aggregates = tree_cons (NULL_TREE, var,
10846 static_aggregates);
10848 else
10849 /* Check whether the dtor is callable. */
10850 cxx_maybe_build_cleanup (var, tf_warning_or_error);
10852 /* Avoid -Wunused-variable warning (c++/38958). */
10853 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
10854 && VAR_P (decl))
10855 TREE_USED (decl) = DECL_READ_P (decl) = true;
10857 *initp = init;
10858 return var;
10861 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
10862 initializing a variable of that TYPE. */
10864 tree
10865 initialize_reference (tree type, tree expr,
10866 int flags, tsubst_flags_t complain)
10868 conversion *conv;
10869 void *p;
10870 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
10872 if (type == error_mark_node || error_operand_p (expr))
10873 return error_mark_node;
10875 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10876 p = conversion_obstack_alloc (0);
10878 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
10879 flags, complain);
10880 if (!conv || conv->bad_p)
10882 if (complain & tf_error)
10884 if (conv)
10885 convert_like (conv, expr, complain);
10886 else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
10887 && !TYPE_REF_IS_RVALUE (type)
10888 && !lvalue_p (expr))
10889 error_at (loc, "invalid initialization of non-const reference of "
10890 "type %qH from an rvalue of type %qI",
10891 type, TREE_TYPE (expr));
10892 else
10893 error_at (loc, "invalid initialization of reference of type "
10894 "%qH from expression of type %qI", type,
10895 TREE_TYPE (expr));
10897 return error_mark_node;
10900 if (conv->kind == ck_ref_bind)
10901 /* Perform the conversion. */
10902 expr = convert_like (conv, expr, complain);
10903 else if (conv->kind == ck_ambig)
10904 /* We gave an error in build_user_type_conversion_1. */
10905 expr = error_mark_node;
10906 else
10907 gcc_unreachable ();
10909 /* Free all the conversions we allocated. */
10910 obstack_free (&conversion_obstack, p);
10912 return expr;
10915 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
10916 which is bound either to a reference or a std::initializer_list. */
10918 static tree
10919 extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups)
10921 tree sub = init;
10922 tree *p;
10923 STRIP_NOPS (sub);
10924 if (TREE_CODE (sub) == COMPOUND_EXPR)
10926 TREE_OPERAND (sub, 1)
10927 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups);
10928 return init;
10930 if (TREE_CODE (sub) != ADDR_EXPR)
10931 return init;
10932 /* Deal with binding to a subobject. */
10933 for (p = &TREE_OPERAND (sub, 0); TREE_CODE (*p) == COMPONENT_REF; )
10934 p = &TREE_OPERAND (*p, 0);
10935 if (TREE_CODE (*p) == TARGET_EXPR)
10937 tree subinit = NULL_TREE;
10938 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit);
10939 recompute_tree_invariant_for_addr_expr (sub);
10940 if (init != sub)
10941 init = fold_convert (TREE_TYPE (init), sub);
10942 if (subinit)
10943 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
10945 return init;
10948 /* INIT is part of the initializer for DECL. If there are any
10949 reference or initializer lists being initialized, extend their
10950 lifetime to match that of DECL. */
10952 tree
10953 extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
10955 tree type = TREE_TYPE (init);
10956 if (processing_template_decl)
10957 return init;
10958 if (TREE_CODE (type) == REFERENCE_TYPE)
10959 init = extend_ref_init_temps_1 (decl, init, cleanups);
10960 else
10962 tree ctor = init;
10963 if (TREE_CODE (ctor) == TARGET_EXPR)
10964 ctor = TARGET_EXPR_INITIAL (ctor);
10965 if (TREE_CODE (ctor) == CONSTRUCTOR)
10967 if (is_std_init_list (type))
10969 /* The temporary array underlying a std::initializer_list
10970 is handled like a reference temporary. */
10971 tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
10972 array = extend_ref_init_temps_1 (decl, array, cleanups);
10973 CONSTRUCTOR_ELT (ctor, 0)->value = array;
10975 else
10977 unsigned i;
10978 constructor_elt *p;
10979 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
10980 FOR_EACH_VEC_SAFE_ELT (elts, i, p)
10981 p->value = extend_ref_init_temps (decl, p->value, cleanups);
10983 recompute_constructor_flags (ctor);
10984 if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
10985 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10989 return init;
10992 /* Returns true iff an initializer for TYPE could contain temporaries that
10993 need to be extended because they are bound to references or
10994 std::initializer_list. */
10996 bool
10997 type_has_extended_temps (tree type)
10999 type = strip_array_types (type);
11000 if (TREE_CODE (type) == REFERENCE_TYPE)
11001 return true;
11002 if (CLASS_TYPE_P (type))
11004 if (is_std_init_list (type))
11005 return true;
11006 for (tree f = next_initializable_field (TYPE_FIELDS (type));
11007 f; f = next_initializable_field (DECL_CHAIN (f)))
11008 if (type_has_extended_temps (TREE_TYPE (f)))
11009 return true;
11011 return false;
11014 /* Returns true iff TYPE is some variant of std::initializer_list. */
11016 bool
11017 is_std_init_list (tree type)
11019 if (!TYPE_P (type))
11020 return false;
11021 if (cxx_dialect == cxx98)
11022 return false;
11023 /* Look through typedefs. */
11024 type = TYPE_MAIN_VARIANT (type);
11025 return (CLASS_TYPE_P (type)
11026 && CP_TYPE_CONTEXT (type) == std_node
11027 && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
11030 /* Returns true iff DECL is a list constructor: i.e. a constructor which
11031 will accept an argument list of a single std::initializer_list<T>. */
11033 bool
11034 is_list_ctor (tree decl)
11036 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
11037 tree arg;
11039 if (!args || args == void_list_node)
11040 return false;
11042 arg = non_reference (TREE_VALUE (args));
11043 if (!is_std_init_list (arg))
11044 return false;
11046 args = TREE_CHAIN (args);
11048 if (args && args != void_list_node && !TREE_PURPOSE (args))
11049 /* There are more non-defaulted parms. */
11050 return false;
11052 return true;
11055 #include "gt-cp-call.h"