PR rtl-optimization/82913
[official-gcc.git] / gcc / cp / call.c
blob6875492e68708877a797406fd6d7972a4c45244b
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 if (current_function_decl && decl
380 && flag_new_inheriting_ctors
381 && DECL_INHERITED_CTOR (current_function_decl)
382 && (DECL_INHERITED_CTOR (current_function_decl)
383 == DECL_CLONED_FUNCTION (decl)))
384 /* Pass arguments directly to the inherited constructor. */
385 CALL_FROM_THUNK_P (function) = true;
387 /* Don't pass empty class objects by value. This is useful
388 for tags in STL, which are used to control overload resolution.
389 We don't need to handle other cases of copying empty classes. */
390 else if (! decl || ! DECL_BUILT_IN (decl))
391 for (i = 0; i < n; i++)
393 tree arg = CALL_EXPR_ARG (function, i);
394 if (is_empty_class (TREE_TYPE (arg))
395 && ! TREE_ADDRESSABLE (TREE_TYPE (arg)))
397 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
398 arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
399 CALL_EXPR_ARG (function, i) = arg;
403 return function;
406 /* New overloading code. */
408 struct z_candidate;
410 struct candidate_warning {
411 z_candidate *loser;
412 candidate_warning *next;
415 /* Information for providing diagnostics about why overloading failed. */
417 enum rejection_reason_code {
418 rr_none,
419 rr_arity,
420 rr_explicit_conversion,
421 rr_template_conversion,
422 rr_arg_conversion,
423 rr_bad_arg_conversion,
424 rr_template_unification,
425 rr_invalid_copy,
426 rr_inherited_ctor,
427 rr_constraint_failure
430 struct conversion_info {
431 /* The index of the argument, 0-based. */
432 int n_arg;
433 /* The actual argument or its type. */
434 tree from;
435 /* The type of the parameter. */
436 tree to_type;
439 struct rejection_reason {
440 enum rejection_reason_code code;
441 union {
442 /* Information about an arity mismatch. */
443 struct {
444 /* The expected number of arguments. */
445 int expected;
446 /* The actual number of arguments in the call. */
447 int actual;
448 /* Whether the call was a varargs call. */
449 bool call_varargs_p;
450 } arity;
451 /* Information about an argument conversion mismatch. */
452 struct conversion_info conversion;
453 /* Same, but for bad argument conversions. */
454 struct conversion_info bad_conversion;
455 /* Information about template unification failures. These are the
456 parameters passed to fn_type_unification. */
457 struct {
458 tree tmpl;
459 tree explicit_targs;
460 int num_targs;
461 const tree *args;
462 unsigned int nargs;
463 tree return_type;
464 unification_kind_t strict;
465 int flags;
466 } template_unification;
467 /* Information about template instantiation failures. These are the
468 parameters passed to instantiate_template. */
469 struct {
470 tree tmpl;
471 tree targs;
472 } template_instantiation;
473 } u;
476 struct z_candidate {
477 /* The FUNCTION_DECL that will be called if this candidate is
478 selected by overload resolution. */
479 tree fn;
480 /* If not NULL_TREE, the first argument to use when calling this
481 function. */
482 tree first_arg;
483 /* The rest of the arguments to use when calling this function. If
484 there are no further arguments this may be NULL or it may be an
485 empty vector. */
486 const vec<tree, va_gc> *args;
487 /* The implicit conversion sequences for each of the arguments to
488 FN. */
489 conversion **convs;
490 /* The number of implicit conversion sequences. */
491 size_t num_convs;
492 /* If FN is a user-defined conversion, the standard conversion
493 sequence from the type returned by FN to the desired destination
494 type. */
495 conversion *second_conv;
496 struct rejection_reason *reason;
497 /* If FN is a member function, the binfo indicating the path used to
498 qualify the name of FN at the call site. This path is used to
499 determine whether or not FN is accessible if it is selected by
500 overload resolution. The DECL_CONTEXT of FN will always be a
501 (possibly improper) base of this binfo. */
502 tree access_path;
503 /* If FN is a non-static member function, the binfo indicating the
504 subobject to which the `this' pointer should be converted if FN
505 is selected by overload resolution. The type pointed to by
506 the `this' pointer must correspond to the most derived class
507 indicated by the CONVERSION_PATH. */
508 tree conversion_path;
509 tree template_decl;
510 tree explicit_targs;
511 candidate_warning *warnings;
512 z_candidate *next;
513 int viable;
515 /* The flags active in add_candidate. */
516 int flags;
519 /* Returns true iff T is a null pointer constant in the sense of
520 [conv.ptr]. */
522 bool
523 null_ptr_cst_p (tree t)
525 tree type = TREE_TYPE (t);
527 /* [conv.ptr]
529 A null pointer constant is an integral constant expression
530 (_expr.const_) rvalue of integer type that evaluates to zero or
531 an rvalue of type std::nullptr_t. */
532 if (NULLPTR_TYPE_P (type))
533 return true;
535 if (cxx_dialect >= cxx11)
537 /* Core issue 903 says only literal 0 is a null pointer constant. */
538 if (TREE_CODE (type) == INTEGER_TYPE
539 && !char_type_p (type)
540 && TREE_CODE (t) == INTEGER_CST
541 && integer_zerop (t)
542 && !TREE_OVERFLOW (t))
543 return true;
545 else if (CP_INTEGRAL_TYPE_P (type))
547 t = fold_non_dependent_expr (t);
548 STRIP_NOPS (t);
549 if (integer_zerop (t) && !TREE_OVERFLOW (t))
550 return true;
553 return false;
556 /* Returns true iff T is a null member pointer value (4.11). */
558 bool
559 null_member_pointer_value_p (tree t)
561 tree type = TREE_TYPE (t);
562 if (!type)
563 return false;
564 else if (TYPE_PTRMEMFUNC_P (type))
565 return (TREE_CODE (t) == CONSTRUCTOR
566 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
567 else if (TYPE_PTRDATAMEM_P (type))
568 return integer_all_onesp (t);
569 else
570 return false;
573 /* Returns nonzero if PARMLIST consists of only default parms,
574 ellipsis, and/or undeduced parameter packs. */
576 bool
577 sufficient_parms_p (const_tree parmlist)
579 for (; parmlist && parmlist != void_list_node;
580 parmlist = TREE_CHAIN (parmlist))
581 if (!TREE_PURPOSE (parmlist)
582 && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
583 return false;
584 return true;
587 /* Allocate N bytes of memory from the conversion obstack. The memory
588 is zeroed before being returned. */
590 static void *
591 conversion_obstack_alloc (size_t n)
593 void *p;
594 if (!conversion_obstack_initialized)
596 gcc_obstack_init (&conversion_obstack);
597 conversion_obstack_initialized = true;
599 p = obstack_alloc (&conversion_obstack, n);
600 memset (p, 0, n);
601 return p;
604 /* Allocate rejection reasons. */
606 static struct rejection_reason *
607 alloc_rejection (enum rejection_reason_code code)
609 struct rejection_reason *p;
610 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
611 p->code = code;
612 return p;
615 static struct rejection_reason *
616 arity_rejection (tree first_arg, int expected, int actual)
618 struct rejection_reason *r = alloc_rejection (rr_arity);
619 int adjust = first_arg != NULL_TREE;
620 r->u.arity.expected = expected - adjust;
621 r->u.arity.actual = actual - adjust;
622 return r;
625 static struct rejection_reason *
626 arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
628 struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
629 int adjust = first_arg != NULL_TREE;
630 r->u.conversion.n_arg = n_arg - adjust;
631 r->u.conversion.from = from;
632 r->u.conversion.to_type = to;
633 return r;
636 static struct rejection_reason *
637 bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
639 struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
640 int adjust = first_arg != NULL_TREE;
641 r->u.bad_conversion.n_arg = n_arg - adjust;
642 r->u.bad_conversion.from = from;
643 r->u.bad_conversion.to_type = to;
644 return r;
647 static struct rejection_reason *
648 explicit_conversion_rejection (tree from, tree to)
650 struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
651 r->u.conversion.n_arg = 0;
652 r->u.conversion.from = from;
653 r->u.conversion.to_type = to;
654 return r;
657 static struct rejection_reason *
658 template_conversion_rejection (tree from, tree to)
660 struct rejection_reason *r = alloc_rejection (rr_template_conversion);
661 r->u.conversion.n_arg = 0;
662 r->u.conversion.from = from;
663 r->u.conversion.to_type = to;
664 return r;
667 static struct rejection_reason *
668 template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
669 const tree *args, unsigned int nargs,
670 tree return_type, unification_kind_t strict,
671 int flags)
673 size_t args_n_bytes = sizeof (*args) * nargs;
674 tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
675 struct rejection_reason *r = alloc_rejection (rr_template_unification);
676 r->u.template_unification.tmpl = tmpl;
677 r->u.template_unification.explicit_targs = explicit_targs;
678 r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
679 /* Copy args to our own storage. */
680 memcpy (args1, args, args_n_bytes);
681 r->u.template_unification.args = args1;
682 r->u.template_unification.nargs = nargs;
683 r->u.template_unification.return_type = return_type;
684 r->u.template_unification.strict = strict;
685 r->u.template_unification.flags = flags;
686 return r;
689 static struct rejection_reason *
690 template_unification_error_rejection (void)
692 return alloc_rejection (rr_template_unification);
695 static struct rejection_reason *
696 invalid_copy_with_fn_template_rejection (void)
698 struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
699 return r;
702 static struct rejection_reason *
703 inherited_ctor_rejection (void)
705 struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
706 return r;
709 // Build a constraint failure record, saving information into the
710 // template_instantiation field of the rejection. If FN is not a template
711 // declaration, the TMPL member is the FN declaration and TARGS is empty.
713 static struct rejection_reason *
714 constraint_failure (tree fn)
716 struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
717 if (tree ti = DECL_TEMPLATE_INFO (fn))
719 r->u.template_instantiation.tmpl = TI_TEMPLATE (ti);
720 r->u.template_instantiation.targs = TI_ARGS (ti);
722 else
724 r->u.template_instantiation.tmpl = fn;
725 r->u.template_instantiation.targs = NULL_TREE;
727 return r;
730 /* Dynamically allocate a conversion. */
732 static conversion *
733 alloc_conversion (conversion_kind kind)
735 conversion *c;
736 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
737 c->kind = kind;
738 return c;
741 /* Make sure that all memory on the conversion obstack has been
742 freed. */
744 void
745 validate_conversion_obstack (void)
747 if (conversion_obstack_initialized)
748 gcc_assert ((obstack_next_free (&conversion_obstack)
749 == obstack_base (&conversion_obstack)));
752 /* Dynamically allocate an array of N conversions. */
754 static conversion **
755 alloc_conversions (size_t n)
757 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
760 static conversion *
761 build_conv (conversion_kind code, tree type, conversion *from)
763 conversion *t;
764 conversion_rank rank = CONVERSION_RANK (from);
766 /* Note that the caller is responsible for filling in t->cand for
767 user-defined conversions. */
768 t = alloc_conversion (code);
769 t->type = type;
770 t->u.next = from;
772 switch (code)
774 case ck_ptr:
775 case ck_pmem:
776 case ck_base:
777 case ck_std:
778 if (rank < cr_std)
779 rank = cr_std;
780 break;
782 case ck_qual:
783 case ck_fnptr:
784 if (rank < cr_exact)
785 rank = cr_exact;
786 break;
788 default:
789 break;
791 t->rank = rank;
792 t->user_conv_p = (code == ck_user || from->user_conv_p);
793 t->bad_p = from->bad_p;
794 t->base_p = false;
795 return t;
798 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
799 specialization of std::initializer_list<T>, if such a conversion is
800 possible. */
802 static conversion *
803 build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
805 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
806 unsigned len = CONSTRUCTOR_NELTS (ctor);
807 conversion **subconvs = alloc_conversions (len);
808 conversion *t;
809 unsigned i;
810 tree val;
812 /* Within a list-initialization we can have more user-defined
813 conversions. */
814 flags &= ~LOOKUP_NO_CONVERSION;
815 /* But no narrowing conversions. */
816 flags |= LOOKUP_NO_NARROWING;
818 /* Can't make an array of these types. */
819 if (TREE_CODE (elttype) == REFERENCE_TYPE
820 || TREE_CODE (elttype) == FUNCTION_TYPE
821 || VOID_TYPE_P (elttype))
822 return NULL;
824 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
826 conversion *sub
827 = implicit_conversion (elttype, TREE_TYPE (val), val,
828 false, flags, complain);
829 if (sub == NULL)
830 return NULL;
832 subconvs[i] = sub;
835 t = alloc_conversion (ck_list);
836 t->type = type;
837 t->u.list = subconvs;
838 t->rank = cr_exact;
840 for (i = 0; i < len; ++i)
842 conversion *sub = subconvs[i];
843 if (sub->rank > t->rank)
844 t->rank = sub->rank;
845 if (sub->user_conv_p)
846 t->user_conv_p = true;
847 if (sub->bad_p)
848 t->bad_p = true;
851 return t;
854 /* Return the next conversion of the conversion chain (if applicable),
855 or NULL otherwise. Please use this function instead of directly
856 accessing fields of struct conversion. */
858 static conversion *
859 next_conversion (conversion *conv)
861 if (conv == NULL
862 || conv->kind == ck_identity
863 || conv->kind == ck_ambig
864 || conv->kind == ck_list)
865 return NULL;
866 return conv->u.next;
869 /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
870 is a valid aggregate initializer for array type ATYPE. */
872 static bool
873 can_convert_array (tree atype, tree ctor, int flags, tsubst_flags_t complain)
875 unsigned i;
876 tree elttype = TREE_TYPE (atype);
877 for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
879 tree val = CONSTRUCTOR_ELT (ctor, i)->value;
880 bool ok;
881 if (TREE_CODE (elttype) == ARRAY_TYPE
882 && TREE_CODE (val) == CONSTRUCTOR)
883 ok = can_convert_array (elttype, val, flags, complain);
884 else
885 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
886 complain);
887 if (!ok)
888 return false;
890 return true;
893 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
894 aggregate class, if such a conversion is possible. */
896 static conversion *
897 build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
899 unsigned HOST_WIDE_INT i = 0;
900 conversion *c;
901 tree field = next_initializable_field (TYPE_FIELDS (type));
902 tree empty_ctor = NULL_TREE;
904 /* We already called reshape_init in implicit_conversion. */
906 /* The conversions within the init-list aren't affected by the enclosing
907 context; they're always simple copy-initialization. */
908 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
910 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
912 tree ftype = TREE_TYPE (field);
913 tree val;
914 bool ok;
916 if (i < CONSTRUCTOR_NELTS (ctor))
917 val = CONSTRUCTOR_ELT (ctor, i)->value;
918 else if (DECL_INITIAL (field))
919 val = get_nsdmi (field, /*ctor*/false, complain);
920 else if (TREE_CODE (ftype) == REFERENCE_TYPE)
921 /* Value-initialization of reference is ill-formed. */
922 return NULL;
923 else
925 if (empty_ctor == NULL_TREE)
926 empty_ctor = build_constructor (init_list_type_node, NULL);
927 val = empty_ctor;
929 ++i;
931 if (TREE_CODE (ftype) == ARRAY_TYPE
932 && TREE_CODE (val) == CONSTRUCTOR)
933 ok = can_convert_array (ftype, val, flags, complain);
934 else
935 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
936 complain);
938 if (!ok)
939 return NULL;
941 if (TREE_CODE (type) == UNION_TYPE)
942 break;
945 if (i < CONSTRUCTOR_NELTS (ctor))
946 return NULL;
948 c = alloc_conversion (ck_aggr);
949 c->type = type;
950 c->rank = cr_exact;
951 c->user_conv_p = true;
952 c->check_narrowing = true;
953 c->u.next = NULL;
954 return c;
957 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
958 array type, if such a conversion is possible. */
960 static conversion *
961 build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
963 conversion *c;
964 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
965 tree elttype = TREE_TYPE (type);
966 unsigned i;
967 tree val;
968 bool bad = false;
969 bool user = false;
970 enum conversion_rank rank = cr_exact;
972 /* We might need to propagate the size from the element to the array. */
973 complete_type (type);
975 if (TYPE_DOMAIN (type)
976 && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
978 unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
979 if (alen < len)
980 return NULL;
983 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
985 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
987 conversion *sub
988 = implicit_conversion (elttype, TREE_TYPE (val), val,
989 false, flags, complain);
990 if (sub == NULL)
991 return NULL;
993 if (sub->rank > rank)
994 rank = sub->rank;
995 if (sub->user_conv_p)
996 user = true;
997 if (sub->bad_p)
998 bad = true;
1001 c = alloc_conversion (ck_aggr);
1002 c->type = type;
1003 c->rank = rank;
1004 c->user_conv_p = user;
1005 c->bad_p = bad;
1006 c->u.next = NULL;
1007 return c;
1010 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1011 complex type, if such a conversion is possible. */
1013 static conversion *
1014 build_complex_conv (tree type, tree ctor, int flags,
1015 tsubst_flags_t complain)
1017 conversion *c;
1018 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1019 tree elttype = TREE_TYPE (type);
1020 unsigned i;
1021 tree val;
1022 bool bad = false;
1023 bool user = false;
1024 enum conversion_rank rank = cr_exact;
1026 if (len != 2)
1027 return NULL;
1029 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1031 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
1033 conversion *sub
1034 = implicit_conversion (elttype, TREE_TYPE (val), val,
1035 false, flags, complain);
1036 if (sub == NULL)
1037 return NULL;
1039 if (sub->rank > rank)
1040 rank = sub->rank;
1041 if (sub->user_conv_p)
1042 user = true;
1043 if (sub->bad_p)
1044 bad = true;
1047 c = alloc_conversion (ck_aggr);
1048 c->type = type;
1049 c->rank = rank;
1050 c->user_conv_p = user;
1051 c->bad_p = bad;
1052 c->u.next = NULL;
1053 return c;
1056 /* Build a representation of the identity conversion from EXPR to
1057 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1059 static conversion *
1060 build_identity_conv (tree type, tree expr)
1062 conversion *c;
1064 c = alloc_conversion (ck_identity);
1065 c->type = type;
1066 c->u.expr = expr;
1068 return c;
1071 /* Converting from EXPR to TYPE was ambiguous in the sense that there
1072 were multiple user-defined conversions to accomplish the job.
1073 Build a conversion that indicates that ambiguity. */
1075 static conversion *
1076 build_ambiguous_conv (tree type, tree expr)
1078 conversion *c;
1080 c = alloc_conversion (ck_ambig);
1081 c->type = type;
1082 c->u.expr = expr;
1084 return c;
1087 tree
1088 strip_top_quals (tree t)
1090 if (TREE_CODE (t) == ARRAY_TYPE)
1091 return t;
1092 return cp_build_qualified_type (t, 0);
1095 /* Returns the standard conversion path (see [conv]) from type FROM to type
1096 TO, if any. For proper handling of null pointer constants, you must
1097 also pass the expression EXPR to convert from. If C_CAST_P is true,
1098 this conversion is coming from a C-style cast. */
1100 static conversion *
1101 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1102 int flags, tsubst_flags_t complain)
1104 enum tree_code fcode, tcode;
1105 conversion *conv;
1106 bool fromref = false;
1107 tree qualified_to;
1109 to = non_reference (to);
1110 if (TREE_CODE (from) == REFERENCE_TYPE)
1112 fromref = true;
1113 from = TREE_TYPE (from);
1115 qualified_to = to;
1116 to = strip_top_quals (to);
1117 from = strip_top_quals (from);
1119 if (expr && type_unknown_p (expr))
1121 if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1123 tsubst_flags_t tflags = tf_conv;
1124 expr = instantiate_type (to, expr, tflags);
1125 if (expr == error_mark_node)
1126 return NULL;
1127 from = TREE_TYPE (expr);
1129 else if (TREE_CODE (to) == BOOLEAN_TYPE)
1131 /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1132 expr = resolve_nondeduced_context (expr, complain);
1133 from = TREE_TYPE (expr);
1137 fcode = TREE_CODE (from);
1138 tcode = TREE_CODE (to);
1140 conv = build_identity_conv (from, expr);
1141 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1143 from = type_decays_to (from);
1144 fcode = TREE_CODE (from);
1145 conv = build_conv (ck_lvalue, from, conv);
1147 /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1148 obvalue_p) seems odd, since it's already a prvalue, but that's how we
1149 express the copy constructor call required by copy-initialization. */
1150 else if (fromref || (expr && obvalue_p (expr)))
1152 if (expr)
1154 tree bitfield_type;
1155 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1156 if (bitfield_type)
1158 from = strip_top_quals (bitfield_type);
1159 fcode = TREE_CODE (from);
1162 conv = build_conv (ck_rvalue, from, conv);
1163 if (flags & LOOKUP_PREFER_RVALUE)
1164 /* Tell convert_like_real to set LOOKUP_PREFER_RVALUE. */
1165 conv->rvaluedness_matches_p = true;
1168 /* Allow conversion between `__complex__' data types. */
1169 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1171 /* The standard conversion sequence to convert FROM to TO is
1172 the standard conversion sequence to perform componentwise
1173 conversion. */
1174 conversion *part_conv = standard_conversion
1175 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1176 complain);
1178 if (part_conv)
1180 conv = build_conv (part_conv->kind, to, conv);
1181 conv->rank = part_conv->rank;
1183 else
1184 conv = NULL;
1186 return conv;
1189 if (same_type_p (from, to))
1191 if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1192 conv->type = qualified_to;
1193 return conv;
1196 /* [conv.ptr]
1197 A null pointer constant can be converted to a pointer type; ... A
1198 null pointer constant of integral type can be converted to an
1199 rvalue of type std::nullptr_t. */
1200 if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1201 || NULLPTR_TYPE_P (to))
1202 && ((expr && null_ptr_cst_p (expr))
1203 || NULLPTR_TYPE_P (from)))
1204 conv = build_conv (ck_std, to, conv);
1205 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1206 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1208 /* For backwards brain damage compatibility, allow interconversion of
1209 pointers and integers with a pedwarn. */
1210 conv = build_conv (ck_std, to, conv);
1211 conv->bad_p = true;
1213 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1215 /* For backwards brain damage compatibility, allow interconversion of
1216 enums and integers with a pedwarn. */
1217 conv = build_conv (ck_std, to, conv);
1218 conv->bad_p = true;
1220 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1221 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1223 tree to_pointee;
1224 tree from_pointee;
1226 if (tcode == POINTER_TYPE)
1228 to_pointee = TREE_TYPE (to);
1229 from_pointee = TREE_TYPE (from);
1231 /* Since this is the target of a pointer, it can't have function
1232 qualifiers, so any TYPE_QUALS must be for attributes const or
1233 noreturn. Strip them. */
1234 if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1235 && TYPE_QUALS (to_pointee))
1236 to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1237 if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1238 && TYPE_QUALS (from_pointee))
1239 from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1241 else
1243 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1244 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1247 if (tcode == POINTER_TYPE
1248 && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1249 to_pointee))
1251 else if (VOID_TYPE_P (to_pointee)
1252 && !TYPE_PTRDATAMEM_P (from)
1253 && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1255 tree nfrom = TREE_TYPE (from);
1256 /* Don't try to apply restrict to void. */
1257 int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1258 from_pointee = cp_build_qualified_type (void_type_node, quals);
1259 from = build_pointer_type (from_pointee);
1260 conv = build_conv (ck_ptr, from, conv);
1262 else if (TYPE_PTRDATAMEM_P (from))
1264 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1265 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1267 if (same_type_p (fbase, tbase))
1268 /* No base conversion needed. */;
1269 else if (DERIVED_FROM_P (fbase, tbase)
1270 && (same_type_ignoring_top_level_qualifiers_p
1271 (from_pointee, to_pointee)))
1273 from = build_ptrmem_type (tbase, from_pointee);
1274 conv = build_conv (ck_pmem, from, conv);
1276 else
1277 return NULL;
1279 else if (CLASS_TYPE_P (from_pointee)
1280 && CLASS_TYPE_P (to_pointee)
1281 /* [conv.ptr]
1283 An rvalue of type "pointer to cv D," where D is a
1284 class type, can be converted to an rvalue of type
1285 "pointer to cv B," where B is a base class (clause
1286 _class.derived_) of D. If B is an inaccessible
1287 (clause _class.access_) or ambiguous
1288 (_class.member.lookup_) base class of D, a program
1289 that necessitates this conversion is ill-formed.
1290 Therefore, we use DERIVED_FROM_P, and do not check
1291 access or uniqueness. */
1292 && DERIVED_FROM_P (to_pointee, from_pointee))
1294 from_pointee
1295 = cp_build_qualified_type (to_pointee,
1296 cp_type_quals (from_pointee));
1297 from = build_pointer_type (from_pointee);
1298 conv = build_conv (ck_ptr, from, conv);
1299 conv->base_p = true;
1302 if (same_type_p (from, to))
1303 /* OK */;
1304 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
1305 /* In a C-style cast, we ignore CV-qualification because we
1306 are allowed to perform a static_cast followed by a
1307 const_cast. */
1308 conv = build_conv (ck_qual, to, conv);
1309 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1310 conv = build_conv (ck_qual, to, conv);
1311 else if (expr && string_conv_p (to, expr, 0))
1312 /* converting from string constant to char *. */
1313 conv = build_conv (ck_qual, to, conv);
1314 else if (fnptr_conv_p (to, from))
1315 conv = build_conv (ck_fnptr, to, conv);
1316 /* Allow conversions among compatible ObjC pointer types (base
1317 conversions have been already handled above). */
1318 else if (c_dialect_objc ()
1319 && objc_compare_types (to, from, -4, NULL_TREE))
1320 conv = build_conv (ck_ptr, to, conv);
1321 else if (ptr_reasonably_similar (to_pointee, from_pointee))
1323 conv = build_conv (ck_ptr, to, conv);
1324 conv->bad_p = true;
1326 else
1327 return NULL;
1329 from = to;
1331 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1333 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1334 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1335 tree fbase = class_of_this_parm (fromfn);
1336 tree tbase = class_of_this_parm (tofn);
1338 if (!DERIVED_FROM_P (fbase, tbase))
1339 return NULL;
1341 tree fstat = static_fn_type (fromfn);
1342 tree tstat = static_fn_type (tofn);
1343 if (same_type_p (tstat, fstat)
1344 || fnptr_conv_p (tstat, fstat))
1345 /* OK */;
1346 else
1347 return NULL;
1349 if (!same_type_p (fbase, tbase))
1351 from = build_memfn_type (fstat,
1352 tbase,
1353 cp_type_quals (tbase),
1354 type_memfn_rqual (tofn));
1355 from = build_ptrmemfunc_type (build_pointer_type (from));
1356 conv = build_conv (ck_pmem, from, conv);
1357 conv->base_p = true;
1359 if (fnptr_conv_p (tstat, fstat))
1360 conv = build_conv (ck_fnptr, to, conv);
1362 else if (tcode == BOOLEAN_TYPE)
1364 /* [conv.bool]
1366 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1367 to member type can be converted to a prvalue of type bool. ...
1368 For direct-initialization (8.5 [dcl.init]), a prvalue of type
1369 std::nullptr_t can be converted to a prvalue of type bool; */
1370 if (ARITHMETIC_TYPE_P (from)
1371 || UNSCOPED_ENUM_P (from)
1372 || fcode == POINTER_TYPE
1373 || TYPE_PTRMEM_P (from)
1374 || NULLPTR_TYPE_P (from))
1376 conv = build_conv (ck_std, to, conv);
1377 if (fcode == POINTER_TYPE
1378 || TYPE_PTRDATAMEM_P (from)
1379 || (TYPE_PTRMEMFUNC_P (from)
1380 && conv->rank < cr_pbool)
1381 || NULLPTR_TYPE_P (from))
1382 conv->rank = cr_pbool;
1383 if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1384 conv->bad_p = true;
1385 return conv;
1388 return NULL;
1390 /* We don't check for ENUMERAL_TYPE here because there are no standard
1391 conversions to enum type. */
1392 /* As an extension, allow conversion to complex type. */
1393 else if (ARITHMETIC_TYPE_P (to))
1395 if (! (INTEGRAL_CODE_P (fcode)
1396 || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1397 || SCOPED_ENUM_P (from))
1398 return NULL;
1399 conv = build_conv (ck_std, to, conv);
1401 /* Give this a better rank if it's a promotion. */
1402 if (same_type_p (to, type_promotes_to (from))
1403 && next_conversion (conv)->rank <= cr_promotion)
1404 conv->rank = cr_promotion;
1406 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1407 && vector_types_convertible_p (from, to, false))
1408 return build_conv (ck_std, to, conv);
1409 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1410 && is_properly_derived_from (from, to))
1412 if (conv->kind == ck_rvalue)
1413 conv = next_conversion (conv);
1414 conv = build_conv (ck_base, to, conv);
1415 /* The derived-to-base conversion indicates the initialization
1416 of a parameter with base type from an object of a derived
1417 type. A temporary object is created to hold the result of
1418 the conversion unless we're binding directly to a reference. */
1419 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1421 else
1422 return NULL;
1424 if (flags & LOOKUP_NO_NARROWING)
1425 conv->check_narrowing = true;
1427 return conv;
1430 /* Returns nonzero if T1 is reference-related to T2. */
1432 bool
1433 reference_related_p (tree t1, tree t2)
1435 if (t1 == error_mark_node || t2 == error_mark_node)
1436 return false;
1438 t1 = TYPE_MAIN_VARIANT (t1);
1439 t2 = TYPE_MAIN_VARIANT (t2);
1441 /* [dcl.init.ref]
1443 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1444 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
1445 of T2. */
1446 return (same_type_p (t1, t2)
1447 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1448 && DERIVED_FROM_P (t1, t2)));
1451 /* Returns nonzero if T1 is reference-compatible with T2. */
1453 static bool
1454 reference_compatible_p (tree t1, tree t2)
1456 /* [dcl.init.ref]
1458 "cv1 T1" is reference compatible with "cv2 T2" if
1459 * T1 is reference-related to T2 or
1460 * T2 is "noexcept function" and T1 is "function", where the
1461 function types are otherwise the same,
1462 and cv1 is the same cv-qualification as, or greater cv-qualification
1463 than, cv2. */
1464 return ((reference_related_p (t1, t2)
1465 || fnptr_conv_p (t1, t2))
1466 && at_least_as_qualified_p (t1, t2));
1469 /* A reference of the indicated TYPE is being bound directly to the
1470 expression represented by the implicit conversion sequence CONV.
1471 Return a conversion sequence for this binding. */
1473 static conversion *
1474 direct_reference_binding (tree type, conversion *conv)
1476 tree t;
1478 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1479 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1481 t = TREE_TYPE (type);
1483 /* [over.ics.rank]
1485 When a parameter of reference type binds directly
1486 (_dcl.init.ref_) to an argument expression, the implicit
1487 conversion sequence is the identity conversion, unless the
1488 argument expression has a type that is a derived class of the
1489 parameter type, in which case the implicit conversion sequence is
1490 a derived-to-base Conversion.
1492 If the parameter binds directly to the result of applying a
1493 conversion function to the argument expression, the implicit
1494 conversion sequence is a user-defined conversion sequence
1495 (_over.ics.user_), with the second standard conversion sequence
1496 either an identity conversion or, if the conversion function
1497 returns an entity of a type that is a derived class of the
1498 parameter type, a derived-to-base conversion. */
1499 if (is_properly_derived_from (conv->type, t))
1501 /* Represent the derived-to-base conversion. */
1502 conv = build_conv (ck_base, t, conv);
1503 /* We will actually be binding to the base-class subobject in
1504 the derived class, so we mark this conversion appropriately.
1505 That way, convert_like knows not to generate a temporary. */
1506 conv->need_temporary_p = false;
1508 return build_conv (ck_ref_bind, type, conv);
1511 /* Returns the conversion path from type FROM to reference type TO for
1512 purposes of reference binding. For lvalue binding, either pass a
1513 reference type to FROM or an lvalue expression to EXPR. If the
1514 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1515 the conversion returned. If C_CAST_P is true, this
1516 conversion is coming from a C-style cast. */
1518 static conversion *
1519 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1520 tsubst_flags_t complain)
1522 conversion *conv = NULL;
1523 tree to = TREE_TYPE (rto);
1524 tree from = rfrom;
1525 tree tfrom;
1526 bool related_p;
1527 bool compatible_p;
1528 cp_lvalue_kind gl_kind;
1529 bool is_lvalue;
1531 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1533 expr = instantiate_type (to, expr, tf_none);
1534 if (expr == error_mark_node)
1535 return NULL;
1536 from = TREE_TYPE (expr);
1539 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1541 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1542 /* DR 1288: Otherwise, if the initializer list has a single element
1543 of type E and ... [T's] referenced type is reference-related to E,
1544 the object or reference is initialized from that element... */
1545 if (CONSTRUCTOR_NELTS (expr) == 1)
1547 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1548 if (error_operand_p (elt))
1549 return NULL;
1550 tree etype = TREE_TYPE (elt);
1551 if (reference_related_p (to, etype))
1553 expr = elt;
1554 from = etype;
1555 goto skip;
1558 /* Otherwise, if T is a reference type, a prvalue temporary of the
1559 type referenced by T is copy-list-initialized or
1560 direct-list-initialized, depending on the kind of initialization
1561 for the reference, and the reference is bound to that temporary. */
1562 conv = implicit_conversion (to, from, expr, c_cast_p,
1563 flags|LOOKUP_NO_TEMP_BIND, complain);
1564 skip:;
1567 if (TREE_CODE (from) == REFERENCE_TYPE)
1569 from = TREE_TYPE (from);
1570 if (!TYPE_REF_IS_RVALUE (rfrom)
1571 || TREE_CODE (from) == FUNCTION_TYPE)
1572 gl_kind = clk_ordinary;
1573 else
1574 gl_kind = clk_rvalueref;
1576 else if (expr)
1577 gl_kind = lvalue_kind (expr);
1578 else if (CLASS_TYPE_P (from)
1579 || TREE_CODE (from) == ARRAY_TYPE)
1580 gl_kind = clk_class;
1581 else
1582 gl_kind = clk_none;
1584 /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1585 if ((flags & LOOKUP_NO_TEMP_BIND)
1586 && (gl_kind & clk_class))
1587 gl_kind = clk_none;
1589 /* Same mask as real_lvalue_p. */
1590 is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1592 tfrom = from;
1593 if ((gl_kind & clk_bitfield) != 0)
1594 tfrom = unlowered_expr_type (expr);
1596 /* Figure out whether or not the types are reference-related and
1597 reference compatible. We have to do this after stripping
1598 references from FROM. */
1599 related_p = reference_related_p (to, tfrom);
1600 /* If this is a C cast, first convert to an appropriately qualified
1601 type, so that we can later do a const_cast to the desired type. */
1602 if (related_p && c_cast_p
1603 && !at_least_as_qualified_p (to, tfrom))
1604 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1605 compatible_p = reference_compatible_p (to, tfrom);
1607 /* Directly bind reference when target expression's type is compatible with
1608 the reference and expression is an lvalue. In DR391, the wording in
1609 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1610 const and rvalue references to rvalues of compatible class type.
1611 We should also do direct bindings for non-class xvalues. */
1612 if ((related_p || compatible_p) && gl_kind)
1614 /* [dcl.init.ref]
1616 If the initializer expression
1618 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1619 is reference-compatible with "cv2 T2,"
1621 the reference is bound directly to the initializer expression
1622 lvalue.
1624 [...]
1625 If the initializer expression is an rvalue, with T2 a class type,
1626 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1627 is bound to the object represented by the rvalue or to a sub-object
1628 within that object. */
1630 conv = build_identity_conv (tfrom, expr);
1631 conv = direct_reference_binding (rto, conv);
1633 if (TREE_CODE (rfrom) == REFERENCE_TYPE)
1634 /* Handle rvalue reference to function properly. */
1635 conv->rvaluedness_matches_p
1636 = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1637 else
1638 conv->rvaluedness_matches_p
1639 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1641 if ((gl_kind & clk_bitfield) != 0
1642 || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1643 /* For the purposes of overload resolution, we ignore the fact
1644 this expression is a bitfield or packed field. (In particular,
1645 [over.ics.ref] says specifically that a function with a
1646 non-const reference parameter is viable even if the
1647 argument is a bitfield.)
1649 However, when we actually call the function we must create
1650 a temporary to which to bind the reference. If the
1651 reference is volatile, or isn't const, then we cannot make
1652 a temporary, so we just issue an error when the conversion
1653 actually occurs. */
1654 conv->need_temporary_p = true;
1656 /* Don't allow binding of lvalues (other than function lvalues) to
1657 rvalue references. */
1658 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1659 && TREE_CODE (to) != FUNCTION_TYPE)
1660 conv->bad_p = true;
1662 /* Nor the reverse. */
1663 if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
1664 && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
1665 || (flags & LOOKUP_NO_RVAL_BIND))
1666 && TREE_CODE (to) != FUNCTION_TYPE)
1667 conv->bad_p = true;
1669 if (!compatible_p)
1670 conv->bad_p = true;
1672 return conv;
1674 /* [class.conv.fct] A conversion function is never used to convert a
1675 (possibly cv-qualified) object to the (possibly cv-qualified) same
1676 object type (or a reference to it), to a (possibly cv-qualified) base
1677 class of that type (or a reference to it).... */
1678 else if (CLASS_TYPE_P (from) && !related_p
1679 && !(flags & LOOKUP_NO_CONVERSION))
1681 /* [dcl.init.ref]
1683 If the initializer expression
1685 -- has a class type (i.e., T2 is a class type) can be
1686 implicitly converted to an lvalue of type "cv3 T3," where
1687 "cv1 T1" is reference-compatible with "cv3 T3". (this
1688 conversion is selected by enumerating the applicable
1689 conversion functions (_over.match.ref_) and choosing the
1690 best one through overload resolution. (_over.match_).
1692 the reference is bound to the lvalue result of the conversion
1693 in the second case. */
1694 z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
1695 complain);
1696 if (cand)
1697 return cand->second_conv;
1700 /* From this point on, we conceptually need temporaries, even if we
1701 elide them. Only the cases above are "direct bindings". */
1702 if (flags & LOOKUP_NO_TEMP_BIND)
1703 return NULL;
1705 /* [over.ics.rank]
1707 When a parameter of reference type is not bound directly to an
1708 argument expression, the conversion sequence is the one required
1709 to convert the argument expression to the underlying type of the
1710 reference according to _over.best.ics_. Conceptually, this
1711 conversion sequence corresponds to copy-initializing a temporary
1712 of the underlying type with the argument expression. Any
1713 difference in top-level cv-qualification is subsumed by the
1714 initialization itself and does not constitute a conversion. */
1716 /* [dcl.init.ref]
1718 Otherwise, the reference shall be an lvalue reference to a
1719 non-volatile const type, or the reference shall be an rvalue
1720 reference.
1722 We try below to treat this as a bad conversion to improve diagnostics,
1723 but if TO is an incomplete class, we need to reject this conversion
1724 now to avoid unnecessary instantiation. */
1725 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto)
1726 && !COMPLETE_TYPE_P (to))
1727 return NULL;
1729 /* We're generating a temporary now, but don't bind any more in the
1730 conversion (specifically, don't slice the temporary returned by a
1731 conversion operator). */
1732 flags |= LOOKUP_NO_TEMP_BIND;
1734 /* Core issue 899: When [copy-]initializing a temporary to be bound
1735 to the first parameter of a copy constructor (12.8) called with
1736 a single argument in the context of direct-initialization,
1737 explicit conversion functions are also considered.
1739 So don't set LOOKUP_ONLYCONVERTING in that case. */
1740 if (!(flags & LOOKUP_COPY_PARM))
1741 flags |= LOOKUP_ONLYCONVERTING;
1743 if (!conv)
1744 conv = implicit_conversion (to, from, expr, c_cast_p,
1745 flags, complain);
1746 if (!conv)
1747 return NULL;
1749 if (conv->user_conv_p)
1751 /* If initializing the temporary used a conversion function,
1752 recalculate the second conversion sequence. */
1753 for (conversion *t = conv; t; t = next_conversion (t))
1754 if (t->kind == ck_user
1755 && DECL_CONV_FN_P (t->cand->fn))
1757 tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
1758 int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
1759 conversion *new_second
1760 = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
1761 sflags, complain);
1762 if (!new_second)
1763 return NULL;
1764 return merge_conversion_sequences (t, new_second);
1768 conv = build_conv (ck_ref_bind, rto, conv);
1769 /* This reference binding, unlike those above, requires the
1770 creation of a temporary. */
1771 conv->need_temporary_p = true;
1772 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1774 /* [dcl.init.ref]
1776 Otherwise, the reference shall be an lvalue reference to a
1777 non-volatile const type, or the reference shall be an rvalue
1778 reference. */
1779 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1780 conv->bad_p = true;
1782 /* [dcl.init.ref]
1784 Otherwise, a temporary of type "cv1 T1" is created and
1785 initialized from the initializer expression using the rules for a
1786 non-reference copy initialization. If T1 is reference-related to
1787 T2, cv1 must be the same cv-qualification as, or greater
1788 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1789 if (related_p && !at_least_as_qualified_p (to, from))
1790 conv->bad_p = true;
1792 return conv;
1795 /* Returns the implicit conversion sequence (see [over.ics]) from type
1796 FROM to type TO. The optional expression EXPR may affect the
1797 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1798 true, this conversion is coming from a C-style cast. */
1800 static conversion *
1801 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1802 int flags, tsubst_flags_t complain)
1804 conversion *conv;
1806 if (from == error_mark_node || to == error_mark_node
1807 || expr == error_mark_node)
1808 return NULL;
1810 /* Other flags only apply to the primary function in overload
1811 resolution, or after we've chosen one. */
1812 flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
1813 |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE
1814 |LOOKUP_NO_NARROWING|LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL);
1816 /* FIXME: actually we don't want warnings either, but we can't just
1817 have 'complain &= ~(tf_warning|tf_error)' because it would cause
1818 the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
1819 We really ought not to issue that warning until we've committed
1820 to that conversion. */
1821 complain &= ~tf_error;
1823 /* Call reshape_init early to remove redundant braces. */
1824 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)
1825 && CLASS_TYPE_P (to)
1826 && COMPLETE_TYPE_P (complete_type (to))
1827 && !CLASSTYPE_NON_AGGREGATE (to))
1829 expr = reshape_init (to, expr, complain);
1830 if (expr == error_mark_node)
1831 return NULL;
1832 from = TREE_TYPE (expr);
1835 if (TREE_CODE (to) == REFERENCE_TYPE)
1836 conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
1837 else
1838 conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
1840 if (conv)
1841 return conv;
1843 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1845 if (is_std_init_list (to))
1846 return build_list_conv (to, expr, flags, complain);
1848 /* As an extension, allow list-initialization of _Complex. */
1849 if (TREE_CODE (to) == COMPLEX_TYPE)
1851 conv = build_complex_conv (to, expr, flags, complain);
1852 if (conv)
1853 return conv;
1856 /* Allow conversion from an initializer-list with one element to a
1857 scalar type. */
1858 if (SCALAR_TYPE_P (to))
1860 int nelts = CONSTRUCTOR_NELTS (expr);
1861 tree elt;
1863 if (nelts == 0)
1864 elt = build_value_init (to, tf_none);
1865 else if (nelts == 1)
1866 elt = CONSTRUCTOR_ELT (expr, 0)->value;
1867 else
1868 elt = error_mark_node;
1870 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
1871 c_cast_p, flags, complain);
1872 if (conv)
1874 conv->check_narrowing = true;
1875 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
1876 /* Too many levels of braces, i.e. '{{1}}'. */
1877 conv->bad_p = true;
1878 return conv;
1881 else if (TREE_CODE (to) == ARRAY_TYPE)
1882 return build_array_conv (to, expr, flags, complain);
1885 if (expr != NULL_TREE
1886 && (MAYBE_CLASS_TYPE_P (from)
1887 || MAYBE_CLASS_TYPE_P (to))
1888 && (flags & LOOKUP_NO_CONVERSION) == 0)
1890 struct z_candidate *cand;
1892 if (CLASS_TYPE_P (to)
1893 && BRACE_ENCLOSED_INITIALIZER_P (expr)
1894 && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
1895 return build_aggr_conv (to, expr, flags, complain);
1897 cand = build_user_type_conversion_1 (to, expr, flags, complain);
1898 if (cand)
1900 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
1901 && CONSTRUCTOR_NELTS (expr) == 1
1902 && !is_list_ctor (cand->fn))
1904 /* "If C is not an initializer-list constructor and the
1905 initializer list has a single element of type cv U, where U is
1906 X or a class derived from X, the implicit conversion sequence
1907 has Exact Match rank if U is X, or Conversion rank if U is
1908 derived from X." */
1909 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1910 tree elttype = TREE_TYPE (elt);
1911 if (reference_related_p (to, elttype))
1912 return implicit_conversion (to, elttype, elt,
1913 c_cast_p, flags, complain);
1915 conv = cand->second_conv;
1918 /* We used to try to bind a reference to a temporary here, but that
1919 is now handled after the recursive call to this function at the end
1920 of reference_binding. */
1921 return conv;
1924 return NULL;
1927 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1928 functions. ARGS will not be changed until a single candidate is
1929 selected. */
1931 static struct z_candidate *
1932 add_candidate (struct z_candidate **candidates,
1933 tree fn, tree first_arg, const vec<tree, va_gc> *args,
1934 size_t num_convs, conversion **convs,
1935 tree access_path, tree conversion_path,
1936 int viable, struct rejection_reason *reason,
1937 int flags)
1939 struct z_candidate *cand = (struct z_candidate *)
1940 conversion_obstack_alloc (sizeof (struct z_candidate));
1942 cand->fn = fn;
1943 cand->first_arg = first_arg;
1944 cand->args = args;
1945 cand->convs = convs;
1946 cand->num_convs = num_convs;
1947 cand->access_path = access_path;
1948 cand->conversion_path = conversion_path;
1949 cand->viable = viable;
1950 cand->reason = reason;
1951 cand->next = *candidates;
1952 cand->flags = flags;
1953 *candidates = cand;
1955 return cand;
1958 /* Return the number of remaining arguments in the parameter list
1959 beginning with ARG. */
1962 remaining_arguments (tree arg)
1964 int n;
1966 for (n = 0; arg != NULL_TREE && arg != void_list_node;
1967 arg = TREE_CHAIN (arg))
1968 n++;
1970 return n;
1973 /* Create an overload candidate for the function or method FN called
1974 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
1975 FLAGS is passed on to implicit_conversion.
1977 This does not change ARGS.
1979 CTYPE, if non-NULL, is the type we want to pretend this function
1980 comes from for purposes of overload resolution. */
1982 static struct z_candidate *
1983 add_function_candidate (struct z_candidate **candidates,
1984 tree fn, tree ctype, tree first_arg,
1985 const vec<tree, va_gc> *args, tree access_path,
1986 tree conversion_path, int flags,
1987 tsubst_flags_t complain)
1989 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1990 int i, len;
1991 conversion **convs;
1992 tree parmnode;
1993 tree orig_first_arg = first_arg;
1994 int skip;
1995 int viable = 1;
1996 struct rejection_reason *reason = NULL;
1998 /* At this point we should not see any functions which haven't been
1999 explicitly declared, except for friend functions which will have
2000 been found using argument dependent lookup. */
2001 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
2003 /* The `this', `in_chrg' and VTT arguments to constructors are not
2004 considered in overload resolution. */
2005 if (DECL_CONSTRUCTOR_P (fn))
2007 if (ctor_omit_inherited_parms (fn))
2008 /* Bring back parameters omitted from an inherited ctor. */
2009 parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2010 else
2011 parmlist = skip_artificial_parms_for (fn, parmlist);
2012 skip = num_artificial_parms_for (fn);
2013 if (skip > 0 && first_arg != NULL_TREE)
2015 --skip;
2016 first_arg = NULL_TREE;
2019 else
2020 skip = 0;
2022 len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2023 convs = alloc_conversions (len);
2025 /* 13.3.2 - Viable functions [over.match.viable]
2026 First, to be a viable function, a candidate function shall have enough
2027 parameters to agree in number with the arguments in the list.
2029 We need to check this first; otherwise, checking the ICSes might cause
2030 us to produce an ill-formed template instantiation. */
2032 parmnode = parmlist;
2033 for (i = 0; i < len; ++i)
2035 if (parmnode == NULL_TREE || parmnode == void_list_node)
2036 break;
2037 parmnode = TREE_CHAIN (parmnode);
2040 if ((i < len && parmnode)
2041 || !sufficient_parms_p (parmnode))
2043 int remaining = remaining_arguments (parmnode);
2044 viable = 0;
2045 reason = arity_rejection (first_arg, i + remaining, len);
2048 /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2049 parameter of type "reference to cv C" (including such a constructor
2050 instantiated from a template) is excluded from the set of candidate
2051 functions when used to construct an object of type D with an argument list
2052 containing a single argument if C is reference-related to D. */
2053 if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2054 && flag_new_inheriting_ctors
2055 && DECL_INHERITED_CTOR (fn))
2057 tree ptype = non_reference (TREE_VALUE (parmlist));
2058 tree dtype = DECL_CONTEXT (fn);
2059 tree btype = DECL_INHERITED_CTOR_BASE (fn);
2060 if (reference_related_p (ptype, dtype)
2061 && reference_related_p (btype, ptype))
2063 viable = false;
2064 reason = inherited_ctor_rejection ();
2068 /* Second, for a function to be viable, its constraints must be
2069 satisfied. */
2070 if (flag_concepts && viable
2071 && !constraints_satisfied_p (fn))
2073 reason = constraint_failure (fn);
2074 viable = false;
2077 /* When looking for a function from a subobject from an implicit
2078 copy/move constructor/operator=, don't consider anything that takes (a
2079 reference to) an unrelated type. See c++/44909 and core 1092. */
2080 if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2082 if (DECL_CONSTRUCTOR_P (fn))
2083 i = 1;
2084 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2085 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2086 i = 2;
2087 else
2088 i = 0;
2089 if (i && len == i)
2091 parmnode = chain_index (i-1, parmlist);
2092 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2093 ctype))
2094 viable = 0;
2097 /* This only applies at the top level. */
2098 flags &= ~LOOKUP_DEFAULTED;
2101 if (! viable)
2102 goto out;
2104 /* Third, for F to be a viable function, there shall exist for each
2105 argument an implicit conversion sequence that converts that argument
2106 to the corresponding parameter of F. */
2108 parmnode = parmlist;
2110 for (i = 0; i < len; ++i)
2112 tree argtype, to_type;
2113 tree arg;
2114 conversion *t;
2115 int is_this;
2117 if (parmnode == void_list_node)
2118 break;
2120 if (i == 0 && first_arg != NULL_TREE)
2121 arg = first_arg;
2122 else
2123 arg = CONST_CAST_TREE (
2124 (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2125 argtype = lvalue_type (arg);
2127 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2128 && ! DECL_CONSTRUCTOR_P (fn));
2130 if (parmnode)
2132 tree parmtype = TREE_VALUE (parmnode);
2133 int lflags = flags;
2135 parmnode = TREE_CHAIN (parmnode);
2137 /* The type of the implicit object parameter ('this') for
2138 overload resolution is not always the same as for the
2139 function itself; conversion functions are considered to
2140 be members of the class being converted, and functions
2141 introduced by a using-declaration are considered to be
2142 members of the class that uses them.
2144 Since build_over_call ignores the ICS for the `this'
2145 parameter, we can just change the parm type. */
2146 if (ctype && is_this)
2148 parmtype = cp_build_qualified_type
2149 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
2150 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2152 /* If the function has a ref-qualifier, the implicit
2153 object parameter has reference type. */
2154 bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2155 parmtype = cp_build_reference_type (parmtype, rv);
2156 /* The special handling of 'this' conversions in compare_ics
2157 does not apply if there is a ref-qualifier. */
2158 is_this = false;
2160 else
2162 parmtype = build_pointer_type (parmtype);
2163 /* We don't use build_this here because we don't want to
2164 capture the object argument until we've chosen a
2165 non-static member function. */
2166 arg = build_address (arg);
2167 argtype = lvalue_type (arg);
2171 /* Core issue 899: When [copy-]initializing a temporary to be bound
2172 to the first parameter of a copy constructor (12.8) called with
2173 a single argument in the context of direct-initialization,
2174 explicit conversion functions are also considered.
2176 So set LOOKUP_COPY_PARM to let reference_binding know that
2177 it's being called in that context. We generalize the above
2178 to handle move constructors and template constructors as well;
2179 the standardese should soon be updated similarly. */
2180 if (ctype && i == 0 && (len-skip == 1)
2181 && DECL_CONSTRUCTOR_P (fn)
2182 && parmtype != error_mark_node
2183 && (same_type_ignoring_top_level_qualifiers_p
2184 (non_reference (parmtype), ctype)))
2186 if (!(flags & LOOKUP_ONLYCONVERTING))
2187 lflags |= LOOKUP_COPY_PARM;
2188 /* We allow user-defined conversions within init-lists, but
2189 don't list-initialize the copy parm, as that would mean
2190 using two levels of braces for the same type. */
2191 if ((flags & LOOKUP_LIST_INIT_CTOR)
2192 && BRACE_ENCLOSED_INITIALIZER_P (arg))
2193 lflags |= LOOKUP_NO_CONVERSION;
2195 else
2196 lflags |= LOOKUP_ONLYCONVERTING;
2198 t = implicit_conversion (parmtype, argtype, arg,
2199 /*c_cast_p=*/false, lflags, complain);
2200 to_type = parmtype;
2202 else
2204 t = build_identity_conv (argtype, arg);
2205 t->ellipsis_p = true;
2206 to_type = argtype;
2209 if (t && is_this)
2210 t->this_p = true;
2212 convs[i] = t;
2213 if (! t)
2215 viable = 0;
2216 reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
2217 break;
2220 if (t->bad_p)
2222 viable = -1;
2223 reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type);
2227 out:
2228 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2229 access_path, conversion_path, viable, reason, flags);
2232 /* Create an overload candidate for the conversion function FN which will
2233 be invoked for expression OBJ, producing a pointer-to-function which
2234 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2235 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2236 passed on to implicit_conversion.
2238 Actually, we don't really care about FN; we care about the type it
2239 converts to. There may be multiple conversion functions that will
2240 convert to that type, and we rely on build_user_type_conversion_1 to
2241 choose the best one; so when we create our candidate, we record the type
2242 instead of the function. */
2244 static struct z_candidate *
2245 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2246 const vec<tree, va_gc> *arglist,
2247 tree access_path, tree conversion_path,
2248 tsubst_flags_t complain)
2250 tree totype = TREE_TYPE (TREE_TYPE (fn));
2251 int i, len, viable, flags;
2252 tree parmlist, parmnode;
2253 conversion **convs;
2254 struct rejection_reason *reason;
2256 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2257 parmlist = TREE_TYPE (parmlist);
2258 parmlist = TYPE_ARG_TYPES (parmlist);
2260 len = vec_safe_length (arglist) + 1;
2261 convs = alloc_conversions (len);
2262 parmnode = parmlist;
2263 viable = 1;
2264 flags = LOOKUP_IMPLICIT;
2265 reason = NULL;
2267 /* Don't bother looking up the same type twice. */
2268 if (*candidates && (*candidates)->fn == totype)
2269 return NULL;
2271 for (i = 0; i < len; ++i)
2273 tree arg, argtype, convert_type = NULL_TREE;
2274 conversion *t;
2276 if (i == 0)
2277 arg = obj;
2278 else
2279 arg = (*arglist)[i - 1];
2280 argtype = lvalue_type (arg);
2282 if (i == 0)
2284 t = build_identity_conv (argtype, NULL_TREE);
2285 t = build_conv (ck_user, totype, t);
2286 /* Leave the 'cand' field null; we'll figure out the conversion in
2287 convert_like_real if this candidate is chosen. */
2288 convert_type = totype;
2290 else if (parmnode == void_list_node)
2291 break;
2292 else if (parmnode)
2294 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2295 /*c_cast_p=*/false, flags, complain);
2296 convert_type = TREE_VALUE (parmnode);
2298 else
2300 t = build_identity_conv (argtype, arg);
2301 t->ellipsis_p = true;
2302 convert_type = argtype;
2305 convs[i] = t;
2306 if (! t)
2307 break;
2309 if (t->bad_p)
2311 viable = -1;
2312 reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type);
2315 if (i == 0)
2316 continue;
2318 if (parmnode)
2319 parmnode = TREE_CHAIN (parmnode);
2322 if (i < len
2323 || ! sufficient_parms_p (parmnode))
2325 int remaining = remaining_arguments (parmnode);
2326 viable = 0;
2327 reason = arity_rejection (NULL_TREE, i + remaining, len);
2330 return add_candidate (candidates, totype, obj, arglist, len, convs,
2331 access_path, conversion_path, viable, reason, flags);
2334 static void
2335 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2336 tree type1, tree type2, tree *args, tree *argtypes,
2337 int flags, tsubst_flags_t complain)
2339 conversion *t;
2340 conversion **convs;
2341 size_t num_convs;
2342 int viable = 1, i;
2343 tree types[2];
2344 struct rejection_reason *reason = NULL;
2346 types[0] = type1;
2347 types[1] = type2;
2349 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
2350 convs = alloc_conversions (num_convs);
2352 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2353 conversion ops are allowed. We handle that here by just checking for
2354 boolean_type_node because other operators don't ask for it. COND_EXPR
2355 also does contextual conversion to bool for the first operand, but we
2356 handle that in build_conditional_expr, and type1 here is operand 2. */
2357 if (type1 != boolean_type_node)
2358 flags |= LOOKUP_ONLYCONVERTING;
2360 for (i = 0; i < 2; ++i)
2362 if (! args[i])
2363 break;
2365 t = implicit_conversion (types[i], argtypes[i], args[i],
2366 /*c_cast_p=*/false, flags, complain);
2367 if (! t)
2369 viable = 0;
2370 /* We need something for printing the candidate. */
2371 t = build_identity_conv (types[i], NULL_TREE);
2372 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2373 types[i]);
2375 else if (t->bad_p)
2377 viable = 0;
2378 reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2379 types[i]);
2381 convs[i] = t;
2384 /* For COND_EXPR we rearranged the arguments; undo that now. */
2385 if (args[2])
2387 convs[2] = convs[1];
2388 convs[1] = convs[0];
2389 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2390 /*c_cast_p=*/false, flags,
2391 complain);
2392 if (t)
2393 convs[0] = t;
2394 else
2396 viable = 0;
2397 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2398 boolean_type_node);
2402 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2403 num_convs, convs,
2404 /*access_path=*/NULL_TREE,
2405 /*conversion_path=*/NULL_TREE,
2406 viable, reason, flags);
2409 static bool
2410 is_complete (tree t)
2412 return COMPLETE_TYPE_P (complete_type (t));
2415 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2417 static bool
2418 promoted_arithmetic_type_p (tree type)
2420 /* [over.built]
2422 In this section, the term promoted integral type is used to refer
2423 to those integral types which are preserved by integral promotion
2424 (including e.g. int and long but excluding e.g. char).
2425 Similarly, the term promoted arithmetic type refers to promoted
2426 integral types plus floating types. */
2427 return ((CP_INTEGRAL_TYPE_P (type)
2428 && same_type_p (type_promotes_to (type), type))
2429 || TREE_CODE (type) == REAL_TYPE);
2432 /* Create any builtin operator overload candidates for the operator in
2433 question given the converted operand types TYPE1 and TYPE2. The other
2434 args are passed through from add_builtin_candidates to
2435 build_builtin_candidate.
2437 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2438 If CODE is requires candidates operands of the same type of the kind
2439 of which TYPE1 and TYPE2 are, we add both candidates
2440 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2442 static void
2443 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2444 enum tree_code code2, tree fnname, tree type1,
2445 tree type2, tree *args, tree *argtypes, int flags,
2446 tsubst_flags_t complain)
2448 switch (code)
2450 case POSTINCREMENT_EXPR:
2451 case POSTDECREMENT_EXPR:
2452 args[1] = integer_zero_node;
2453 type2 = integer_type_node;
2454 break;
2455 default:
2456 break;
2459 switch (code)
2462 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2463 and VQ is either volatile or empty, there exist candidate operator
2464 functions of the form
2465 VQ T& operator++(VQ T&);
2466 T operator++(VQ T&, int);
2467 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
2468 type other than bool, and VQ is either volatile or empty, there exist
2469 candidate operator functions of the form
2470 VQ T& operator--(VQ T&);
2471 T operator--(VQ T&, int);
2472 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
2473 complete object type, and VQ is either volatile or empty, there exist
2474 candidate operator functions of the form
2475 T*VQ& operator++(T*VQ&);
2476 T*VQ& operator--(T*VQ&);
2477 T* operator++(T*VQ&, int);
2478 T* operator--(T*VQ&, int); */
2480 case POSTDECREMENT_EXPR:
2481 case PREDECREMENT_EXPR:
2482 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2483 return;
2484 /* FALLTHRU */
2485 case POSTINCREMENT_EXPR:
2486 case PREINCREMENT_EXPR:
2487 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2489 type1 = build_reference_type (type1);
2490 break;
2492 return;
2494 /* 7 For every cv-qualified or cv-unqualified object type T, there
2495 exist candidate operator functions of the form
2497 T& operator*(T*);
2499 8 For every function type T, there exist candidate operator functions of
2500 the form
2501 T& operator*(T*); */
2503 case INDIRECT_REF:
2504 if (TYPE_PTR_P (type1)
2505 && (TYPE_PTROB_P (type1)
2506 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2507 break;
2508 return;
2510 /* 9 For every type T, there exist candidate operator functions of the form
2511 T* operator+(T*);
2513 10For every promoted arithmetic type T, there exist candidate operator
2514 functions of the form
2515 T operator+(T);
2516 T operator-(T); */
2518 case UNARY_PLUS_EXPR: /* unary + */
2519 if (TYPE_PTR_P (type1))
2520 break;
2521 /* FALLTHRU */
2522 case NEGATE_EXPR:
2523 if (ARITHMETIC_TYPE_P (type1))
2524 break;
2525 return;
2527 /* 11For every promoted integral type T, there exist candidate operator
2528 functions of the form
2529 T operator~(T); */
2531 case BIT_NOT_EXPR:
2532 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2533 break;
2534 return;
2536 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2537 is the same type as C2 or is a derived class of C2, T is a complete
2538 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2539 there exist candidate operator functions of the form
2540 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2541 where CV12 is the union of CV1 and CV2. */
2543 case MEMBER_REF:
2544 if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2546 tree c1 = TREE_TYPE (type1);
2547 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2549 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2550 && (TYPE_PTRMEMFUNC_P (type2)
2551 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2552 break;
2554 return;
2556 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2557 didate operator functions of the form
2558 LR operator*(L, R);
2559 LR operator/(L, R);
2560 LR operator+(L, R);
2561 LR operator-(L, R);
2562 bool operator<(L, R);
2563 bool operator>(L, R);
2564 bool operator<=(L, R);
2565 bool operator>=(L, R);
2566 bool operator==(L, R);
2567 bool operator!=(L, R);
2568 where LR is the result of the usual arithmetic conversions between
2569 types L and R.
2571 14For every pair of types T and I, where T is a cv-qualified or cv-
2572 unqualified complete object type and I is a promoted integral type,
2573 there exist candidate operator functions of the form
2574 T* operator+(T*, I);
2575 T& operator[](T*, I);
2576 T* operator-(T*, I);
2577 T* operator+(I, T*);
2578 T& operator[](I, T*);
2580 15For every T, where T is a pointer to complete object type, there exist
2581 candidate operator functions of the form112)
2582 ptrdiff_t operator-(T, T);
2584 16For every pointer or enumeration type T, there exist candidate operator
2585 functions of the form
2586 bool operator<(T, T);
2587 bool operator>(T, T);
2588 bool operator<=(T, T);
2589 bool operator>=(T, T);
2590 bool operator==(T, T);
2591 bool operator!=(T, T);
2593 17For every pointer to member type T, there exist candidate operator
2594 functions of the form
2595 bool operator==(T, T);
2596 bool operator!=(T, T); */
2598 case MINUS_EXPR:
2599 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2600 break;
2601 if (TYPE_PTROB_P (type1)
2602 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2604 type2 = ptrdiff_type_node;
2605 break;
2607 /* FALLTHRU */
2608 case MULT_EXPR:
2609 case TRUNC_DIV_EXPR:
2610 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2611 break;
2612 return;
2614 case EQ_EXPR:
2615 case NE_EXPR:
2616 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2617 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
2618 break;
2619 if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
2621 type2 = type1;
2622 break;
2624 if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
2626 type1 = type2;
2627 break;
2629 /* Fall through. */
2630 case LT_EXPR:
2631 case GT_EXPR:
2632 case LE_EXPR:
2633 case GE_EXPR:
2634 case MAX_EXPR:
2635 case MIN_EXPR:
2636 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2637 break;
2638 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2639 break;
2640 if (TREE_CODE (type1) == ENUMERAL_TYPE
2641 && TREE_CODE (type2) == ENUMERAL_TYPE)
2642 break;
2643 if (TYPE_PTR_P (type1)
2644 && null_ptr_cst_p (args[1]))
2646 type2 = type1;
2647 break;
2649 if (null_ptr_cst_p (args[0])
2650 && TYPE_PTR_P (type2))
2652 type1 = type2;
2653 break;
2655 return;
2657 case PLUS_EXPR:
2658 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2659 break;
2660 /* FALLTHRU */
2661 case ARRAY_REF:
2662 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2664 type1 = ptrdiff_type_node;
2665 break;
2667 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2669 type2 = ptrdiff_type_node;
2670 break;
2672 return;
2674 /* 18For every pair of promoted integral types L and R, there exist candi-
2675 date operator functions of the form
2676 LR operator%(L, R);
2677 LR operator&(L, R);
2678 LR operator^(L, R);
2679 LR operator|(L, R);
2680 L operator<<(L, R);
2681 L operator>>(L, R);
2682 where LR is the result of the usual arithmetic conversions between
2683 types L and R. */
2685 case TRUNC_MOD_EXPR:
2686 case BIT_AND_EXPR:
2687 case BIT_IOR_EXPR:
2688 case BIT_XOR_EXPR:
2689 case LSHIFT_EXPR:
2690 case RSHIFT_EXPR:
2691 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2692 break;
2693 return;
2695 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2696 type, VQ is either volatile or empty, and R is a promoted arithmetic
2697 type, there exist candidate operator functions of the form
2698 VQ L& operator=(VQ L&, R);
2699 VQ L& operator*=(VQ L&, R);
2700 VQ L& operator/=(VQ L&, R);
2701 VQ L& operator+=(VQ L&, R);
2702 VQ L& operator-=(VQ L&, R);
2704 20For every pair T, VQ), where T is any type and VQ is either volatile
2705 or empty, there exist candidate operator functions of the form
2706 T*VQ& operator=(T*VQ&, T*);
2708 21For every pair T, VQ), where T is a pointer to member type and VQ is
2709 either volatile or empty, there exist candidate operator functions of
2710 the form
2711 VQ T& operator=(VQ T&, T);
2713 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2714 unqualified complete object type, VQ is either volatile or empty, and
2715 I is a promoted integral type, there exist candidate operator func-
2716 tions of the form
2717 T*VQ& operator+=(T*VQ&, I);
2718 T*VQ& operator-=(T*VQ&, I);
2720 23For every triple L, VQ, R), where L is an integral or enumeration
2721 type, VQ is either volatile or empty, and R is a promoted integral
2722 type, there exist candidate operator functions of the form
2724 VQ L& operator%=(VQ L&, R);
2725 VQ L& operator<<=(VQ L&, R);
2726 VQ L& operator>>=(VQ L&, R);
2727 VQ L& operator&=(VQ L&, R);
2728 VQ L& operator^=(VQ L&, R);
2729 VQ L& operator|=(VQ L&, R); */
2731 case MODIFY_EXPR:
2732 switch (code2)
2734 case PLUS_EXPR:
2735 case MINUS_EXPR:
2736 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2738 type2 = ptrdiff_type_node;
2739 break;
2741 /* FALLTHRU */
2742 case MULT_EXPR:
2743 case TRUNC_DIV_EXPR:
2744 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2745 break;
2746 return;
2748 case TRUNC_MOD_EXPR:
2749 case BIT_AND_EXPR:
2750 case BIT_IOR_EXPR:
2751 case BIT_XOR_EXPR:
2752 case LSHIFT_EXPR:
2753 case RSHIFT_EXPR:
2754 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2755 break;
2756 return;
2758 case NOP_EXPR:
2759 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2760 break;
2761 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2762 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2763 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2764 || ((TYPE_PTRMEMFUNC_P (type1)
2765 || TYPE_PTR_P (type1))
2766 && null_ptr_cst_p (args[1])))
2768 type2 = type1;
2769 break;
2771 return;
2773 default:
2774 gcc_unreachable ();
2776 type1 = build_reference_type (type1);
2777 break;
2779 case COND_EXPR:
2780 /* [over.built]
2782 For every pair of promoted arithmetic types L and R, there
2783 exist candidate operator functions of the form
2785 LR operator?(bool, L, R);
2787 where LR is the result of the usual arithmetic conversions
2788 between types L and R.
2790 For every type T, where T is a pointer or pointer-to-member
2791 type, there exist candidate operator functions of the form T
2792 operator?(bool, T, T); */
2794 if (promoted_arithmetic_type_p (type1)
2795 && promoted_arithmetic_type_p (type2))
2796 /* That's OK. */
2797 break;
2799 /* Otherwise, the types should be pointers. */
2800 if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
2801 return;
2803 /* We don't check that the two types are the same; the logic
2804 below will actually create two candidates; one in which both
2805 parameter types are TYPE1, and one in which both parameter
2806 types are TYPE2. */
2807 break;
2809 case REALPART_EXPR:
2810 case IMAGPART_EXPR:
2811 if (ARITHMETIC_TYPE_P (type1))
2812 break;
2813 return;
2815 default:
2816 gcc_unreachable ();
2819 /* Make sure we don't create builtin candidates with dependent types. */
2820 bool u1 = uses_template_parms (type1);
2821 bool u2 = type2 ? uses_template_parms (type2) : false;
2822 if (u1 || u2)
2824 /* Try to recover if one of the types is non-dependent. But if
2825 there's only one type, there's nothing we can do. */
2826 if (!type2)
2827 return;
2828 /* And we lose if both are dependent. */
2829 if (u1 && u2)
2830 return;
2831 /* Or if they have different forms. */
2832 if (TREE_CODE (type1) != TREE_CODE (type2))
2833 return;
2835 if (u1 && !u2)
2836 type1 = type2;
2837 else if (u2 && !u1)
2838 type2 = type1;
2841 /* If we're dealing with two pointer types or two enumeral types,
2842 we need candidates for both of them. */
2843 if (type2 && !same_type_p (type1, type2)
2844 && TREE_CODE (type1) == TREE_CODE (type2)
2845 && (TREE_CODE (type1) == REFERENCE_TYPE
2846 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2847 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2848 || TYPE_PTRMEMFUNC_P (type1)
2849 || MAYBE_CLASS_TYPE_P (type1)
2850 || TREE_CODE (type1) == ENUMERAL_TYPE))
2852 if (TYPE_PTR_OR_PTRMEM_P (type1))
2854 tree cptype = composite_pointer_type (type1, type2,
2855 error_mark_node,
2856 error_mark_node,
2857 CPO_CONVERSION,
2858 tf_none);
2859 if (cptype != error_mark_node)
2861 build_builtin_candidate
2862 (candidates, fnname, cptype, cptype, args, argtypes,
2863 flags, complain);
2864 return;
2868 build_builtin_candidate
2869 (candidates, fnname, type1, type1, args, argtypes, flags, complain);
2870 build_builtin_candidate
2871 (candidates, fnname, type2, type2, args, argtypes, flags, complain);
2872 return;
2875 build_builtin_candidate
2876 (candidates, fnname, type1, type2, args, argtypes, flags, complain);
2879 tree
2880 type_decays_to (tree type)
2882 if (TREE_CODE (type) == ARRAY_TYPE)
2883 return build_pointer_type (TREE_TYPE (type));
2884 if (TREE_CODE (type) == FUNCTION_TYPE)
2885 return build_pointer_type (type);
2886 return type;
2889 /* There are three conditions of builtin candidates:
2891 1) bool-taking candidates. These are the same regardless of the input.
2892 2) pointer-pair taking candidates. These are generated for each type
2893 one of the input types converts to.
2894 3) arithmetic candidates. According to the standard, we should generate
2895 all of these, but I'm trying not to...
2897 Here we generate a superset of the possible candidates for this particular
2898 case. That is a subset of the full set the standard defines, plus some
2899 other cases which the standard disallows. add_builtin_candidate will
2900 filter out the invalid set. */
2902 static void
2903 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2904 enum tree_code code2, tree fnname, tree *args,
2905 int flags, tsubst_flags_t complain)
2907 int ref1, i;
2908 int enum_p = 0;
2909 tree type, argtypes[3], t;
2910 /* TYPES[i] is the set of possible builtin-operator parameter types
2911 we will consider for the Ith argument. */
2912 vec<tree, va_gc> *types[2];
2913 unsigned ix;
2915 for (i = 0; i < 3; ++i)
2917 if (args[i])
2918 argtypes[i] = unlowered_expr_type (args[i]);
2919 else
2920 argtypes[i] = NULL_TREE;
2923 switch (code)
2925 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2926 and VQ is either volatile or empty, there exist candidate operator
2927 functions of the form
2928 VQ T& operator++(VQ T&); */
2930 case POSTINCREMENT_EXPR:
2931 case PREINCREMENT_EXPR:
2932 case POSTDECREMENT_EXPR:
2933 case PREDECREMENT_EXPR:
2934 case MODIFY_EXPR:
2935 ref1 = 1;
2936 break;
2938 /* 24There also exist candidate operator functions of the form
2939 bool operator!(bool);
2940 bool operator&&(bool, bool);
2941 bool operator||(bool, bool); */
2943 case TRUTH_NOT_EXPR:
2944 build_builtin_candidate
2945 (candidates, fnname, boolean_type_node,
2946 NULL_TREE, args, argtypes, flags, complain);
2947 return;
2949 case TRUTH_ORIF_EXPR:
2950 case TRUTH_ANDIF_EXPR:
2951 build_builtin_candidate
2952 (candidates, fnname, boolean_type_node,
2953 boolean_type_node, args, argtypes, flags, complain);
2954 return;
2956 case ADDR_EXPR:
2957 case COMPOUND_EXPR:
2958 case COMPONENT_REF:
2959 return;
2961 case COND_EXPR:
2962 case EQ_EXPR:
2963 case NE_EXPR:
2964 case LT_EXPR:
2965 case LE_EXPR:
2966 case GT_EXPR:
2967 case GE_EXPR:
2968 enum_p = 1;
2969 /* Fall through. */
2971 default:
2972 ref1 = 0;
2975 types[0] = make_tree_vector ();
2976 types[1] = make_tree_vector ();
2978 for (i = 0; i < 2; ++i)
2980 if (! args[i])
2982 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
2984 tree convs;
2986 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2987 return;
2989 convs = lookup_conversions (argtypes[i]);
2991 if (code == COND_EXPR)
2993 if (lvalue_p (args[i]))
2994 vec_safe_push (types[i], build_reference_type (argtypes[i]));
2996 vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
2999 else if (! convs)
3000 return;
3002 for (; convs; convs = TREE_CHAIN (convs))
3004 type = TREE_TYPE (convs);
3006 if (i == 0 && ref1
3007 && (TREE_CODE (type) != REFERENCE_TYPE
3008 || CP_TYPE_CONST_P (TREE_TYPE (type))))
3009 continue;
3011 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
3012 vec_safe_push (types[i], type);
3014 type = non_reference (type);
3015 if (i != 0 || ! ref1)
3017 type = cv_unqualified (type_decays_to (type));
3018 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3019 vec_safe_push (types[i], type);
3020 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3021 type = type_promotes_to (type);
3024 if (! vec_member (type, types[i]))
3025 vec_safe_push (types[i], type);
3028 else
3030 if (code == COND_EXPR && lvalue_p (args[i]))
3031 vec_safe_push (types[i], build_reference_type (argtypes[i]));
3032 type = non_reference (argtypes[i]);
3033 if (i != 0 || ! ref1)
3035 type = cv_unqualified (type_decays_to (type));
3036 if (enum_p && UNSCOPED_ENUM_P (type))
3037 vec_safe_push (types[i], type);
3038 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3039 type = type_promotes_to (type);
3041 vec_safe_push (types[i], type);
3045 /* Run through the possible parameter types of both arguments,
3046 creating candidates with those parameter types. */
3047 FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3049 unsigned jx;
3050 tree u;
3052 if (!types[1]->is_empty ())
3053 FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3054 add_builtin_candidate
3055 (candidates, code, code2, fnname, t,
3056 u, args, argtypes, flags, complain);
3057 else
3058 add_builtin_candidate
3059 (candidates, code, code2, fnname, t,
3060 NULL_TREE, args, argtypes, flags, complain);
3063 release_tree_vector (types[0]);
3064 release_tree_vector (types[1]);
3068 /* If TMPL can be successfully instantiated as indicated by
3069 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3071 TMPL is the template. EXPLICIT_TARGS are any explicit template
3072 arguments. ARGLIST is the arguments provided at the call-site.
3073 This does not change ARGLIST. The RETURN_TYPE is the desired type
3074 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3075 as for add_function_candidate. If an OBJ is supplied, FLAGS and
3076 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
3078 static struct z_candidate*
3079 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3080 tree ctype, tree explicit_targs, tree first_arg,
3081 const vec<tree, va_gc> *arglist, tree return_type,
3082 tree access_path, tree conversion_path,
3083 int flags, tree obj, unification_kind_t strict,
3084 tsubst_flags_t complain)
3086 int ntparms = DECL_NTPARMS (tmpl);
3087 tree targs = make_tree_vec (ntparms);
3088 unsigned int len = vec_safe_length (arglist);
3089 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3090 unsigned int skip_without_in_chrg = 0;
3091 tree first_arg_without_in_chrg = first_arg;
3092 tree *args_without_in_chrg;
3093 unsigned int nargs_without_in_chrg;
3094 unsigned int ia, ix;
3095 tree arg;
3096 struct z_candidate *cand;
3097 tree fn;
3098 struct rejection_reason *reason = NULL;
3099 int errs;
3101 /* We don't do deduction on the in-charge parameter, the VTT
3102 parameter or 'this'. */
3103 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
3105 if (first_arg_without_in_chrg != NULL_TREE)
3106 first_arg_without_in_chrg = NULL_TREE;
3107 else if (return_type && strict == DEDUCE_CALL)
3108 /* We're deducing for a call to the result of a template conversion
3109 function, so the args don't contain 'this'; leave them alone. */;
3110 else
3111 ++skip_without_in_chrg;
3114 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3115 || DECL_BASE_CONSTRUCTOR_P (tmpl))
3116 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3118 if (first_arg_without_in_chrg != NULL_TREE)
3119 first_arg_without_in_chrg = NULL_TREE;
3120 else
3121 ++skip_without_in_chrg;
3124 if (len < skip_without_in_chrg)
3125 return NULL;
3127 if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3128 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3129 TREE_TYPE ((*arglist)[0])))
3131 /* 12.8/6 says, "A declaration of a constructor for a class X is
3132 ill-formed if its first parameter is of type (optionally cv-qualified)
3133 X and either there are no other parameters or else all other
3134 parameters have default arguments. A member function template is never
3135 instantiated to produce such a constructor signature."
3137 So if we're trying to copy an object of the containing class, don't
3138 consider a template constructor that has a first parameter type that
3139 is just a template parameter, as we would deduce a signature that we
3140 would then reject in the code below. */
3141 if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3143 firstparm = TREE_VALUE (firstparm);
3144 if (PACK_EXPANSION_P (firstparm))
3145 firstparm = PACK_EXPANSION_PATTERN (firstparm);
3146 if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3148 gcc_assert (!explicit_targs);
3149 reason = invalid_copy_with_fn_template_rejection ();
3150 goto fail;
3155 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3156 + (len - skip_without_in_chrg));
3157 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3158 ia = 0;
3159 if (first_arg_without_in_chrg != NULL_TREE)
3161 args_without_in_chrg[ia] = first_arg_without_in_chrg;
3162 ++ia;
3164 for (ix = skip_without_in_chrg;
3165 vec_safe_iterate (arglist, ix, &arg);
3166 ++ix)
3168 args_without_in_chrg[ia] = arg;
3169 ++ia;
3171 gcc_assert (ia == nargs_without_in_chrg);
3173 errs = errorcount+sorrycount;
3174 fn = fn_type_unification (tmpl, explicit_targs, targs,
3175 args_without_in_chrg,
3176 nargs_without_in_chrg,
3177 return_type, strict, flags, false,
3178 complain & tf_decltype);
3180 if (fn == error_mark_node)
3182 /* Don't repeat unification later if it already resulted in errors. */
3183 if (errorcount+sorrycount == errs)
3184 reason = template_unification_rejection (tmpl, explicit_targs,
3185 targs, args_without_in_chrg,
3186 nargs_without_in_chrg,
3187 return_type, strict, flags);
3188 else
3189 reason = template_unification_error_rejection ();
3190 goto fail;
3193 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3195 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3196 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3197 ctype))
3199 /* We're trying to produce a constructor with a prohibited signature,
3200 as discussed above; handle here any cases we didn't catch then,
3201 such as X(X<T>). */
3202 reason = invalid_copy_with_fn_template_rejection ();
3203 goto fail;
3207 if (obj != NULL_TREE)
3208 /* Aha, this is a conversion function. */
3209 cand = add_conv_candidate (candidates, fn, obj, arglist,
3210 access_path, conversion_path, complain);
3211 else
3212 cand = add_function_candidate (candidates, fn, ctype,
3213 first_arg, arglist, access_path,
3214 conversion_path, flags, complain);
3215 if (DECL_TI_TEMPLATE (fn) != tmpl)
3216 /* This situation can occur if a member template of a template
3217 class is specialized. Then, instantiate_template might return
3218 an instantiation of the specialization, in which case the
3219 DECL_TI_TEMPLATE field will point at the original
3220 specialization. For example:
3222 template <class T> struct S { template <class U> void f(U);
3223 template <> void f(int) {}; };
3224 S<double> sd;
3225 sd.f(3);
3227 Here, TMPL will be template <class U> S<double>::f(U).
3228 And, instantiate template will give us the specialization
3229 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3230 for this will point at template <class T> template <> S<T>::f(int),
3231 so that we can find the definition. For the purposes of
3232 overload resolution, however, we want the original TMPL. */
3233 cand->template_decl = build_template_info (tmpl, targs);
3234 else
3235 cand->template_decl = DECL_TEMPLATE_INFO (fn);
3236 cand->explicit_targs = explicit_targs;
3238 return cand;
3239 fail:
3240 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
3241 access_path, conversion_path, 0, reason, flags);
3245 static struct z_candidate *
3246 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3247 tree explicit_targs, tree first_arg,
3248 const vec<tree, va_gc> *arglist, tree return_type,
3249 tree access_path, tree conversion_path, int flags,
3250 unification_kind_t strict, tsubst_flags_t complain)
3252 return
3253 add_template_candidate_real (candidates, tmpl, ctype,
3254 explicit_targs, first_arg, arglist,
3255 return_type, access_path, conversion_path,
3256 flags, NULL_TREE, strict, complain);
3259 /* Create an overload candidate for the conversion function template TMPL,
3260 returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3261 pointer-to-function which will in turn be called with the argument list
3262 ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3263 passed on to implicit_conversion. */
3265 static struct z_candidate *
3266 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3267 tree obj,
3268 const vec<tree, va_gc> *arglist,
3269 tree return_type, tree access_path,
3270 tree conversion_path, tsubst_flags_t complain)
3272 /* Making this work broke PR 71117, so until the committee resolves core
3273 issue 2189, let's disable this candidate if there are any viable call
3274 operators. */
3275 if (any_strictly_viable (*candidates))
3276 return NULL;
3278 return
3279 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3280 NULL_TREE, arglist, return_type, access_path,
3281 conversion_path, 0, obj, DEDUCE_CALL,
3282 complain);
3285 /* The CANDS are the set of candidates that were considered for
3286 overload resolution. Return the set of viable candidates, or CANDS
3287 if none are viable. If any of the candidates were viable, set
3288 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3289 considered viable only if it is strictly viable. */
3291 static struct z_candidate*
3292 splice_viable (struct z_candidate *cands,
3293 bool strict_p,
3294 bool *any_viable_p)
3296 struct z_candidate *viable;
3297 struct z_candidate **last_viable;
3298 struct z_candidate **cand;
3299 bool found_strictly_viable = false;
3301 /* Be strict inside templates, since build_over_call won't actually
3302 do the conversions to get pedwarns. */
3303 if (processing_template_decl)
3304 strict_p = true;
3306 viable = NULL;
3307 last_viable = &viable;
3308 *any_viable_p = false;
3310 cand = &cands;
3311 while (*cand)
3313 struct z_candidate *c = *cand;
3314 if (!strict_p
3315 && (c->viable == 1 || TREE_CODE (c->fn) == TEMPLATE_DECL))
3317 /* Be strict in the presence of a viable candidate. Also if
3318 there are template candidates, so that we get deduction errors
3319 for them instead of silently preferring a bad conversion. */
3320 strict_p = true;
3321 if (viable && !found_strictly_viable)
3323 /* Put any spliced near matches back onto the main list so
3324 that we see them if there is no strict match. */
3325 *any_viable_p = false;
3326 *last_viable = cands;
3327 cands = viable;
3328 viable = NULL;
3329 last_viable = &viable;
3333 if (strict_p ? c->viable == 1 : c->viable)
3335 *last_viable = c;
3336 *cand = c->next;
3337 c->next = NULL;
3338 last_viable = &c->next;
3339 *any_viable_p = true;
3340 if (c->viable == 1)
3341 found_strictly_viable = true;
3343 else
3344 cand = &c->next;
3347 return viable ? viable : cands;
3350 static bool
3351 any_strictly_viable (struct z_candidate *cands)
3353 for (; cands; cands = cands->next)
3354 if (cands->viable == 1)
3355 return true;
3356 return false;
3359 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3360 words, it is about to become the "this" pointer for a member
3361 function call. Take the address of the object. */
3363 static tree
3364 build_this (tree obj)
3366 /* In a template, we are only concerned about the type of the
3367 expression, so we can take a shortcut. */
3368 if (processing_nonlambda_template ())
3369 return build_address (obj);
3371 return cp_build_addr_expr (obj, tf_warning_or_error);
3374 /* Returns true iff functions are equivalent. Equivalent functions are
3375 not '==' only if one is a function-local extern function or if
3376 both are extern "C". */
3378 static inline int
3379 equal_functions (tree fn1, tree fn2)
3381 if (TREE_CODE (fn1) != TREE_CODE (fn2))
3382 return 0;
3383 if (TREE_CODE (fn1) == TEMPLATE_DECL)
3384 return fn1 == fn2;
3385 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
3386 || DECL_EXTERN_C_FUNCTION_P (fn1))
3387 return decls_match (fn1, fn2);
3388 return fn1 == fn2;
3391 /* Print information about a candidate being rejected due to INFO. */
3393 static void
3394 print_conversion_rejection (location_t loc, struct conversion_info *info)
3396 tree from = info->from;
3397 if (!TYPE_P (from))
3398 from = lvalue_type (from);
3399 if (info->n_arg == -1)
3401 /* Conversion of implicit `this' argument failed. */
3402 if (!TYPE_P (info->from))
3403 /* A bad conversion for 'this' must be discarding cv-quals. */
3404 inform (loc, " passing %qT as %<this%> "
3405 "argument discards qualifiers",
3406 from);
3407 else
3408 inform (loc, " no known conversion for implicit "
3409 "%<this%> parameter from %qH to %qI",
3410 from, info->to_type);
3412 else if (!TYPE_P (info->from))
3414 if (info->n_arg >= 0)
3415 inform (loc, " conversion of argument %d would be ill-formed:",
3416 info->n_arg + 1);
3417 perform_implicit_conversion (info->to_type, info->from,
3418 tf_warning_or_error);
3420 else if (info->n_arg == -2)
3421 /* Conversion of conversion function return value failed. */
3422 inform (loc, " no known conversion from %qH to %qI",
3423 from, info->to_type);
3424 else
3425 inform (loc, " no known conversion for argument %d from %qH to %qI",
3426 info->n_arg + 1, from, info->to_type);
3429 /* Print information about a candidate with WANT parameters and we found
3430 HAVE. */
3432 static void
3433 print_arity_information (location_t loc, unsigned int have, unsigned int want)
3435 inform_n (loc, want,
3436 " candidate expects %d argument, %d provided",
3437 " candidate expects %d arguments, %d provided",
3438 want, have);
3441 /* Print information about one overload candidate CANDIDATE. MSGSTR
3442 is the text to print before the candidate itself.
3444 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3445 to have been run through gettext by the caller. This wart makes
3446 life simpler in print_z_candidates and for the translators. */
3448 static void
3449 print_z_candidate (location_t loc, const char *msgstr,
3450 struct z_candidate *candidate)
3452 const char *msg = (msgstr == NULL
3453 ? ""
3454 : ACONCAT ((msgstr, " ", NULL)));
3455 tree fn = candidate->fn;
3456 if (flag_new_inheriting_ctors)
3457 fn = strip_inheriting_ctors (fn);
3458 location_t cloc = location_of (fn);
3460 if (identifier_p (fn))
3462 cloc = loc;
3463 if (candidate->num_convs == 3)
3464 inform (cloc, "%s%<%D(%T, %T, %T)%> <built-in>", msg, fn,
3465 candidate->convs[0]->type,
3466 candidate->convs[1]->type,
3467 candidate->convs[2]->type);
3468 else if (candidate->num_convs == 2)
3469 inform (cloc, "%s%<%D(%T, %T)%> <built-in>", msg, fn,
3470 candidate->convs[0]->type,
3471 candidate->convs[1]->type);
3472 else
3473 inform (cloc, "%s%<%D(%T)%> <built-in>", msg, fn,
3474 candidate->convs[0]->type);
3476 else if (TYPE_P (fn))
3477 inform (cloc, "%s%qT <conversion>", msg, fn);
3478 else if (candidate->viable == -1)
3479 inform (cloc, "%s%#qD <near match>", msg, fn);
3480 else if (DECL_DELETED_FN (fn))
3481 inform (cloc, "%s%#qD <deleted>", msg, fn);
3482 else
3483 inform (cloc, "%s%#qD", msg, fn);
3484 if (fn != candidate->fn)
3486 cloc = location_of (candidate->fn);
3487 inform (cloc, " inherited here");
3489 /* Give the user some information about why this candidate failed. */
3490 if (candidate->reason != NULL)
3492 struct rejection_reason *r = candidate->reason;
3494 switch (r->code)
3496 case rr_arity:
3497 print_arity_information (cloc, r->u.arity.actual,
3498 r->u.arity.expected);
3499 break;
3500 case rr_arg_conversion:
3501 print_conversion_rejection (cloc, &r->u.conversion);
3502 break;
3503 case rr_bad_arg_conversion:
3504 print_conversion_rejection (cloc, &r->u.bad_conversion);
3505 break;
3506 case rr_explicit_conversion:
3507 inform (cloc, " return type %qT of explicit conversion function "
3508 "cannot be converted to %qT with a qualification "
3509 "conversion", r->u.conversion.from,
3510 r->u.conversion.to_type);
3511 break;
3512 case rr_template_conversion:
3513 inform (cloc, " conversion from return type %qT of template "
3514 "conversion function specialization to %qT is not an "
3515 "exact match", r->u.conversion.from,
3516 r->u.conversion.to_type);
3517 break;
3518 case rr_template_unification:
3519 /* We use template_unification_error_rejection if unification caused
3520 actual non-SFINAE errors, in which case we don't need to repeat
3521 them here. */
3522 if (r->u.template_unification.tmpl == NULL_TREE)
3524 inform (cloc, " substitution of deduced template arguments "
3525 "resulted in errors seen above");
3526 break;
3528 /* Re-run template unification with diagnostics. */
3529 inform (cloc, " template argument deduction/substitution failed:");
3530 fn_type_unification (r->u.template_unification.tmpl,
3531 r->u.template_unification.explicit_targs,
3532 (make_tree_vec
3533 (r->u.template_unification.num_targs)),
3534 r->u.template_unification.args,
3535 r->u.template_unification.nargs,
3536 r->u.template_unification.return_type,
3537 r->u.template_unification.strict,
3538 r->u.template_unification.flags,
3539 true, false);
3540 break;
3541 case rr_invalid_copy:
3542 inform (cloc,
3543 " a constructor taking a single argument of its own "
3544 "class type is invalid");
3545 break;
3546 case rr_constraint_failure:
3548 tree tmpl = r->u.template_instantiation.tmpl;
3549 tree args = r->u.template_instantiation.targs;
3550 diagnose_constraints (cloc, tmpl, args);
3552 break;
3553 case rr_inherited_ctor:
3554 inform (cloc, " an inherited constructor is not a candidate for "
3555 "initialization from an expression of the same or derived "
3556 "type");
3557 break;
3558 case rr_none:
3559 default:
3560 /* This candidate didn't have any issues or we failed to
3561 handle a particular code. Either way... */
3562 gcc_unreachable ();
3567 static void
3568 print_z_candidates (location_t loc, struct z_candidate *candidates)
3570 struct z_candidate *cand1;
3571 struct z_candidate **cand2;
3573 if (!candidates)
3574 return;
3576 /* Remove non-viable deleted candidates. */
3577 cand1 = candidates;
3578 for (cand2 = &cand1; *cand2; )
3580 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
3581 && !(*cand2)->viable
3582 && DECL_DELETED_FN ((*cand2)->fn))
3583 *cand2 = (*cand2)->next;
3584 else
3585 cand2 = &(*cand2)->next;
3587 /* ...if there are any non-deleted ones. */
3588 if (cand1)
3589 candidates = cand1;
3591 /* There may be duplicates in the set of candidates. We put off
3592 checking this condition as long as possible, since we have no way
3593 to eliminate duplicates from a set of functions in less than n^2
3594 time. Now we are about to emit an error message, so it is more
3595 permissible to go slowly. */
3596 for (cand1 = candidates; cand1; cand1 = cand1->next)
3598 tree fn = cand1->fn;
3599 /* Skip builtin candidates and conversion functions. */
3600 if (!DECL_P (fn))
3601 continue;
3602 cand2 = &cand1->next;
3603 while (*cand2)
3605 if (DECL_P ((*cand2)->fn)
3606 && equal_functions (fn, (*cand2)->fn))
3607 *cand2 = (*cand2)->next;
3608 else
3609 cand2 = &(*cand2)->next;
3613 for (; candidates; candidates = candidates->next)
3614 print_z_candidate (loc, "candidate:", candidates);
3617 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3618 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3619 the result of the conversion function to convert it to the final
3620 desired type. Merge the two sequences into a single sequence,
3621 and return the merged sequence. */
3623 static conversion *
3624 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
3626 conversion **t;
3627 bool bad = user_seq->bad_p;
3629 gcc_assert (user_seq->kind == ck_user);
3631 /* Find the end of the second conversion sequence. */
3632 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
3634 /* The entire sequence is a user-conversion sequence. */
3635 (*t)->user_conv_p = true;
3636 if (bad)
3637 (*t)->bad_p = true;
3640 /* Replace the identity conversion with the user conversion
3641 sequence. */
3642 *t = user_seq;
3644 return std_seq;
3647 /* Handle overload resolution for initializing an object of class type from
3648 an initializer list. First we look for a suitable constructor that
3649 takes a std::initializer_list; if we don't find one, we then look for a
3650 non-list constructor.
3652 Parameters are as for add_candidates, except that the arguments are in
3653 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
3654 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3656 static void
3657 add_list_candidates (tree fns, tree first_arg,
3658 const vec<tree, va_gc> *args, tree totype,
3659 tree explicit_targs, bool template_only,
3660 tree conversion_path, tree access_path,
3661 int flags,
3662 struct z_candidate **candidates,
3663 tsubst_flags_t complain)
3665 gcc_assert (*candidates == NULL);
3667 /* We're looking for a ctor for list-initialization. */
3668 flags |= LOOKUP_LIST_INIT_CTOR;
3669 /* And we don't allow narrowing conversions. We also use this flag to
3670 avoid the copy constructor call for copy-list-initialization. */
3671 flags |= LOOKUP_NO_NARROWING;
3673 unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
3674 tree init_list = (*args)[nart];
3676 /* Always use the default constructor if the list is empty (DR 990). */
3677 if (CONSTRUCTOR_NELTS (init_list) == 0
3678 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
3680 /* If the class has a list ctor, try passing the list as a single
3681 argument first, but only consider list ctors. */
3682 else if (TYPE_HAS_LIST_CTOR (totype))
3684 flags |= LOOKUP_LIST_ONLY;
3685 add_candidates (fns, first_arg, args, NULL_TREE,
3686 explicit_targs, template_only, conversion_path,
3687 access_path, flags, candidates, complain);
3688 if (any_strictly_viable (*candidates))
3689 return;
3692 /* Expand the CONSTRUCTOR into a new argument vec. */
3693 vec<tree, va_gc> *new_args;
3694 vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
3695 for (unsigned i = 0; i < nart; ++i)
3696 new_args->quick_push ((*args)[i]);
3697 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (init_list); ++i)
3698 new_args->quick_push (CONSTRUCTOR_ELT (init_list, i)->value);
3700 /* We aren't looking for list-ctors anymore. */
3701 flags &= ~LOOKUP_LIST_ONLY;
3702 /* We allow more user-defined conversions within an init-list. */
3703 flags &= ~LOOKUP_NO_CONVERSION;
3705 add_candidates (fns, first_arg, new_args, NULL_TREE,
3706 explicit_targs, template_only, conversion_path,
3707 access_path, flags, candidates, complain);
3710 /* Returns the best overload candidate to perform the requested
3711 conversion. This function is used for three the overloading situations
3712 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3713 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3714 per [dcl.init.ref], so we ignore temporary bindings. */
3716 static struct z_candidate *
3717 build_user_type_conversion_1 (tree totype, tree expr, int flags,
3718 tsubst_flags_t complain)
3720 struct z_candidate *candidates, *cand;
3721 tree fromtype;
3722 tree ctors = NULL_TREE;
3723 tree conv_fns = NULL_TREE;
3724 conversion *conv = NULL;
3725 tree first_arg = NULL_TREE;
3726 vec<tree, va_gc> *args = NULL;
3727 bool any_viable_p;
3728 int convflags;
3730 if (!expr)
3731 return NULL;
3733 fromtype = TREE_TYPE (expr);
3735 /* We represent conversion within a hierarchy using RVALUE_CONV and
3736 BASE_CONV, as specified by [over.best.ics]; these become plain
3737 constructor calls, as specified in [dcl.init]. */
3738 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
3739 || !DERIVED_FROM_P (totype, fromtype));
3741 if (CLASS_TYPE_P (totype))
3742 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
3743 creating a garbage BASELINK; constructors can't be inherited. */
3744 ctors = get_class_binding (totype, complete_ctor_identifier);
3746 /* FIXME P0135 doesn't say what to do in C++17 about list-initialization from
3747 a single element. For now, let's handle constructors as before and also
3748 consider conversion operators from the element. */
3749 if (cxx_dialect >= cxx17
3750 && BRACE_ENCLOSED_INITIALIZER_P (expr)
3751 && CONSTRUCTOR_NELTS (expr) == 1)
3752 fromtype = TREE_TYPE (CONSTRUCTOR_ELT (expr, 0)->value);
3754 if (MAYBE_CLASS_TYPE_P (fromtype))
3756 tree to_nonref = non_reference (totype);
3757 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
3758 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
3759 && DERIVED_FROM_P (to_nonref, fromtype)))
3761 /* [class.conv.fct] A conversion function is never used to
3762 convert a (possibly cv-qualified) object to the (possibly
3763 cv-qualified) same object type (or a reference to it), to a
3764 (possibly cv-qualified) base class of that type (or a
3765 reference to it)... */
3767 else
3768 conv_fns = lookup_conversions (fromtype);
3771 candidates = 0;
3772 flags |= LOOKUP_NO_CONVERSION;
3773 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3774 flags |= LOOKUP_NO_NARROWING;
3776 /* It's OK to bind a temporary for converting constructor arguments, but
3777 not in converting the return value of a conversion operator. */
3778 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
3779 | (flags & LOOKUP_NO_NARROWING));
3780 flags &= ~LOOKUP_NO_TEMP_BIND;
3782 if (ctors)
3784 int ctorflags = flags;
3786 first_arg = build_dummy_object (totype);
3788 /* We should never try to call the abstract or base constructor
3789 from here. */
3790 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
3791 && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
3793 args = make_tree_vector_single (expr);
3794 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3796 /* List-initialization. */
3797 add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
3798 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
3799 ctorflags, &candidates, complain);
3801 else
3803 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
3804 TYPE_BINFO (totype), TYPE_BINFO (totype),
3805 ctorflags, &candidates, complain);
3808 for (cand = candidates; cand; cand = cand->next)
3810 cand->second_conv = build_identity_conv (totype, NULL_TREE);
3812 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
3813 set, then this is copy-initialization. In that case, "The
3814 result of the call is then used to direct-initialize the
3815 object that is the destination of the copy-initialization."
3816 [dcl.init]
3818 We represent this in the conversion sequence with an
3819 rvalue conversion, which means a constructor call. */
3820 if (TREE_CODE (totype) != REFERENCE_TYPE
3821 && !(convflags & LOOKUP_NO_TEMP_BIND))
3822 cand->second_conv
3823 = build_conv (ck_rvalue, totype, cand->second_conv);
3827 if (conv_fns)
3829 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3830 /* FIXME see above about C++17. */
3831 first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
3832 else
3833 first_arg = expr;
3836 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
3838 tree conversion_path = TREE_PURPOSE (conv_fns);
3839 struct z_candidate *old_candidates;
3841 /* If we are called to convert to a reference type, we are trying to
3842 find a direct binding, so don't even consider temporaries. If
3843 we don't find a direct binding, the caller will try again to
3844 look for a temporary binding. */
3845 if (TREE_CODE (totype) == REFERENCE_TYPE)
3846 convflags |= LOOKUP_NO_TEMP_BIND;
3848 old_candidates = candidates;
3849 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3850 NULL_TREE, false,
3851 conversion_path, TYPE_BINFO (fromtype),
3852 flags, &candidates, complain);
3854 for (cand = candidates; cand != old_candidates; cand = cand->next)
3856 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
3857 conversion *ics
3858 = implicit_conversion (totype,
3859 rettype,
3861 /*c_cast_p=*/false, convflags,
3862 complain);
3864 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3865 copy-initialization. In that case, "The result of the
3866 call is then used to direct-initialize the object that is
3867 the destination of the copy-initialization." [dcl.init]
3869 We represent this in the conversion sequence with an
3870 rvalue conversion, which means a constructor call. But
3871 don't add a second rvalue conversion if there's already
3872 one there. Which there really shouldn't be, but it's
3873 harmless since we'd add it here anyway. */
3874 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3875 && !(convflags & LOOKUP_NO_TEMP_BIND))
3876 ics = build_conv (ck_rvalue, totype, ics);
3878 cand->second_conv = ics;
3880 if (!ics)
3882 cand->viable = 0;
3883 cand->reason = arg_conversion_rejection (NULL_TREE, -2,
3884 rettype, totype);
3886 else if (DECL_NONCONVERTING_P (cand->fn)
3887 && ics->rank > cr_exact)
3889 /* 13.3.1.5: For direct-initialization, those explicit
3890 conversion functions that are not hidden within S and
3891 yield type T or a type that can be converted to type T
3892 with a qualification conversion (4.4) are also candidate
3893 functions. */
3894 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
3895 I've raised this issue with the committee. --jason 9/2011 */
3896 cand->viable = -1;
3897 cand->reason = explicit_conversion_rejection (rettype, totype);
3899 else if (cand->viable == 1 && ics->bad_p)
3901 cand->viable = -1;
3902 cand->reason
3903 = bad_arg_conversion_rejection (NULL_TREE, -2,
3904 rettype, totype);
3906 else if (primary_template_instantiation_p (cand->fn)
3907 && ics->rank > cr_exact)
3909 /* 13.3.3.1.2: If the user-defined conversion is specified by
3910 a specialization of a conversion function template, the
3911 second standard conversion sequence shall have exact match
3912 rank. */
3913 cand->viable = -1;
3914 cand->reason = template_conversion_rejection (rettype, totype);
3919 candidates = splice_viable (candidates, false, &any_viable_p);
3920 if (!any_viable_p)
3922 if (args)
3923 release_tree_vector (args);
3924 return NULL;
3927 cand = tourney (candidates, complain);
3928 if (cand == 0)
3930 if (complain & tf_error)
3932 error ("conversion from %qH to %qI is ambiguous",
3933 fromtype, totype);
3934 print_z_candidates (location_of (expr), candidates);
3937 cand = candidates; /* any one will do */
3938 cand->second_conv = build_ambiguous_conv (totype, expr);
3939 cand->second_conv->user_conv_p = true;
3940 if (!any_strictly_viable (candidates))
3941 cand->second_conv->bad_p = true;
3942 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3943 ambiguous conversion is no worse than another user-defined
3944 conversion. */
3946 return cand;
3949 tree convtype;
3950 if (!DECL_CONSTRUCTOR_P (cand->fn))
3951 convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
3952 else if (cand->second_conv->kind == ck_rvalue)
3953 /* DR 5: [in the first step of copy-initialization]...if the function
3954 is a constructor, the call initializes a temporary of the
3955 cv-unqualified version of the destination type. */
3956 convtype = cv_unqualified (totype);
3957 else
3958 convtype = totype;
3959 /* Build the user conversion sequence. */
3960 conv = build_conv
3961 (ck_user,
3962 convtype,
3963 build_identity_conv (TREE_TYPE (expr), expr));
3964 conv->cand = cand;
3965 if (cand->viable == -1)
3966 conv->bad_p = true;
3968 /* Remember that this was a list-initialization. */
3969 if (flags & LOOKUP_NO_NARROWING)
3970 conv->check_narrowing = true;
3972 /* Combine it with the second conversion sequence. */
3973 cand->second_conv = merge_conversion_sequences (conv,
3974 cand->second_conv);
3976 return cand;
3979 /* Wrapper for above. */
3981 tree
3982 build_user_type_conversion (tree totype, tree expr, int flags,
3983 tsubst_flags_t complain)
3985 struct z_candidate *cand;
3986 tree ret;
3988 bool subtime = timevar_cond_start (TV_OVERLOAD);
3989 cand = build_user_type_conversion_1 (totype, expr, flags, complain);
3991 if (cand)
3993 if (cand->second_conv->kind == ck_ambig)
3994 ret = error_mark_node;
3995 else
3997 expr = convert_like (cand->second_conv, expr, complain);
3998 ret = convert_from_reference (expr);
4001 else
4002 ret = NULL_TREE;
4004 timevar_cond_stop (TV_OVERLOAD, subtime);
4005 return ret;
4008 /* Subroutine of convert_nontype_argument.
4010 EXPR is an expression used in a context that requires a converted
4011 constant-expression, such as a template non-type parameter. Do any
4012 necessary conversions (that are permitted for converted
4013 constant-expressions) to convert it to the desired type.
4015 If conversion is successful, returns the converted expression;
4016 otherwise, returns error_mark_node. */
4018 tree
4019 build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
4021 conversion *conv;
4022 void *p;
4023 tree t;
4024 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
4026 if (error_operand_p (expr))
4027 return error_mark_node;
4029 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4030 p = conversion_obstack_alloc (0);
4032 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4033 /*c_cast_p=*/false,
4034 LOOKUP_IMPLICIT, complain);
4036 /* A converted constant expression of type T is an expression, implicitly
4037 converted to type T, where the converted expression is a constant
4038 expression and the implicit conversion sequence contains only
4040 * user-defined conversions,
4041 * lvalue-to-rvalue conversions (7.1),
4042 * array-to-pointer conversions (7.2),
4043 * function-to-pointer conversions (7.3),
4044 * qualification conversions (7.5),
4045 * integral promotions (7.6),
4046 * integral conversions (7.8) other than narrowing conversions (11.6.4),
4047 * null pointer conversions (7.11) from std::nullptr_t,
4048 * null member pointer conversions (7.12) from std::nullptr_t, and
4049 * function pointer conversions (7.13),
4051 and where the reference binding (if any) binds directly. */
4053 for (conversion *c = conv;
4054 conv && c->kind != ck_identity;
4055 c = next_conversion (c))
4057 switch (c->kind)
4059 /* A conversion function is OK. If it isn't constexpr, we'll
4060 complain later that the argument isn't constant. */
4061 case ck_user:
4062 /* The lvalue-to-rvalue conversion is OK. */
4063 case ck_rvalue:
4064 /* Array-to-pointer and function-to-pointer. */
4065 case ck_lvalue:
4066 /* Function pointer conversions. */
4067 case ck_fnptr:
4068 /* Qualification conversions. */
4069 case ck_qual:
4070 break;
4072 case ck_ref_bind:
4073 if (c->need_temporary_p)
4075 if (complain & tf_error)
4076 error_at (loc, "initializing %qH with %qI in converted "
4077 "constant expression does not bind directly",
4078 type, next_conversion (c)->type);
4079 conv = NULL;
4081 break;
4083 case ck_base:
4084 case ck_pmem:
4085 case ck_ptr:
4086 case ck_std:
4087 t = next_conversion (c)->type;
4088 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
4089 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4090 /* Integral promotion or conversion. */
4091 break;
4092 if (NULLPTR_TYPE_P (t))
4093 /* Conversion from nullptr to pointer or pointer-to-member. */
4094 break;
4096 if (complain & tf_error)
4097 error_at (loc, "conversion from %qH to %qI in a "
4098 "converted constant expression", t, type);
4099 /* fall through. */
4101 default:
4102 conv = NULL;
4103 break;
4107 /* Avoid confusing convert_nontype_argument by introducing
4108 a redundant conversion to the same reference type. */
4109 if (conv && conv->kind == ck_ref_bind
4110 && REFERENCE_REF_P (expr))
4112 tree ref = TREE_OPERAND (expr, 0);
4113 if (same_type_p (type, TREE_TYPE (ref)))
4114 return ref;
4117 if (conv)
4118 expr = convert_like (conv, expr, complain);
4119 else
4120 expr = error_mark_node;
4122 /* Free all the conversions we allocated. */
4123 obstack_free (&conversion_obstack, p);
4125 return expr;
4128 /* Do any initial processing on the arguments to a function call. */
4130 static vec<tree, va_gc> *
4131 resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
4133 unsigned int ix;
4134 tree arg;
4136 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
4138 if (error_operand_p (arg))
4139 return NULL;
4140 else if (VOID_TYPE_P (TREE_TYPE (arg)))
4142 if (complain & tf_error)
4143 error ("invalid use of void expression");
4144 return NULL;
4146 else if (invalid_nonstatic_memfn_p (input_location, arg, complain))
4147 return NULL;
4149 return args;
4152 /* Perform overload resolution on FN, which is called with the ARGS.
4154 Return the candidate function selected by overload resolution, or
4155 NULL if the event that overload resolution failed. In the case
4156 that overload resolution fails, *CANDIDATES will be the set of
4157 candidates considered, and ANY_VIABLE_P will be set to true or
4158 false to indicate whether or not any of the candidates were
4159 viable.
4161 The ARGS should already have gone through RESOLVE_ARGS before this
4162 function is called. */
4164 static struct z_candidate *
4165 perform_overload_resolution (tree fn,
4166 const vec<tree, va_gc> *args,
4167 struct z_candidate **candidates,
4168 bool *any_viable_p, tsubst_flags_t complain)
4170 struct z_candidate *cand;
4171 tree explicit_targs;
4172 int template_only;
4174 bool subtime = timevar_cond_start (TV_OVERLOAD);
4176 explicit_targs = NULL_TREE;
4177 template_only = 0;
4179 *candidates = NULL;
4180 *any_viable_p = true;
4182 /* Check FN. */
4183 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
4184 || TREE_CODE (fn) == TEMPLATE_DECL
4185 || TREE_CODE (fn) == OVERLOAD
4186 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
4188 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4190 explicit_targs = TREE_OPERAND (fn, 1);
4191 fn = TREE_OPERAND (fn, 0);
4192 template_only = 1;
4195 /* Add the various candidate functions. */
4196 add_candidates (fn, NULL_TREE, args, NULL_TREE,
4197 explicit_targs, template_only,
4198 /*conversion_path=*/NULL_TREE,
4199 /*access_path=*/NULL_TREE,
4200 LOOKUP_NORMAL,
4201 candidates, complain);
4203 *candidates = splice_viable (*candidates, false, any_viable_p);
4204 if (*any_viable_p)
4205 cand = tourney (*candidates, complain);
4206 else
4207 cand = NULL;
4209 timevar_cond_stop (TV_OVERLOAD, subtime);
4210 return cand;
4213 /* Print an error message about being unable to build a call to FN with
4214 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
4215 be located; CANDIDATES is a possibly empty list of such
4216 functions. */
4218 static void
4219 print_error_for_call_failure (tree fn, vec<tree, va_gc> *args,
4220 struct z_candidate *candidates)
4222 tree targs = NULL_TREE;
4223 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4225 targs = TREE_OPERAND (fn, 1);
4226 fn = TREE_OPERAND (fn, 0);
4228 tree name = OVL_NAME (fn);
4229 location_t loc = location_of (name);
4230 if (targs)
4231 name = lookup_template_function (name, targs);
4233 if (!any_strictly_viable (candidates))
4234 error_at (loc, "no matching function for call to %<%D(%A)%>",
4235 name, build_tree_list_vec (args));
4236 else
4237 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
4238 name, build_tree_list_vec (args));
4239 if (candidates)
4240 print_z_candidates (loc, candidates);
4243 /* Return an expression for a call to FN (a namespace-scope function,
4244 or a static member function) with the ARGS. This may change
4245 ARGS. */
4247 tree
4248 build_new_function_call (tree fn, vec<tree, va_gc> **args,
4249 tsubst_flags_t complain)
4251 struct z_candidate *candidates, *cand;
4252 bool any_viable_p;
4253 void *p;
4254 tree result;
4256 if (args != NULL && *args != NULL)
4258 *args = resolve_args (*args, complain);
4259 if (*args == NULL)
4260 return error_mark_node;
4263 if (flag_tm)
4264 tm_malloc_replacement (fn);
4266 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4267 p = conversion_obstack_alloc (0);
4269 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
4270 complain);
4272 if (!cand)
4274 if (complain & tf_error)
4276 // If there is a single (non-viable) function candidate,
4277 // let the error be diagnosed by cp_build_function_call_vec.
4278 if (!any_viable_p && candidates && ! candidates->next
4279 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
4280 return cp_build_function_call_vec (candidates->fn, args, complain);
4282 // Otherwise, emit notes for non-viable candidates.
4283 print_error_for_call_failure (fn, *args, candidates);
4285 result = error_mark_node;
4287 else
4289 int flags = LOOKUP_NORMAL;
4290 /* If fn is template_id_expr, the call has explicit template arguments
4291 (e.g. func<int>(5)), communicate this info to build_over_call
4292 through flags so that later we can use it to decide whether to warn
4293 about peculiar null pointer conversion. */
4294 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4296 /* If overload resolution selects a specialization of a
4297 function concept for non-dependent template arguments,
4298 the expression is true if the constraints are satisfied
4299 and false otherwise.
4301 NOTE: This is an extension of Concepts Lite TS that
4302 allows constraints to be used in expressions. */
4303 if (flag_concepts && !processing_template_decl)
4305 tree tmpl = DECL_TI_TEMPLATE (cand->fn);
4306 tree targs = DECL_TI_ARGS (cand->fn);
4307 tree decl = DECL_TEMPLATE_RESULT (tmpl);
4308 if (DECL_DECLARED_CONCEPT_P (decl))
4309 return evaluate_function_concept (decl, targs);
4312 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
4315 result = build_over_call (cand, flags, complain);
4318 /* Free all the conversions we allocated. */
4319 obstack_free (&conversion_obstack, p);
4321 return result;
4324 /* Build a call to a global operator new. FNNAME is the name of the
4325 operator (either "operator new" or "operator new[]") and ARGS are
4326 the arguments provided. This may change ARGS. *SIZE points to the
4327 total number of bytes required by the allocation, and is updated if
4328 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
4329 be used. If this function determines that no cookie should be
4330 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
4331 is not NULL_TREE, it is evaluated before calculating the final
4332 array size, and if it fails, the array size is replaced with
4333 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
4334 is non-NULL, it will be set, upon return, to the allocation
4335 function called. */
4337 tree
4338 build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
4339 tree *size, tree *cookie_size,
4340 tree align_arg, tree size_check,
4341 tree *fn, tsubst_flags_t complain)
4343 tree original_size = *size;
4344 tree fns;
4345 struct z_candidate *candidates;
4346 struct z_candidate *cand = NULL;
4347 bool any_viable_p;
4349 if (fn)
4350 *fn = NULL_TREE;
4351 /* Set to (size_t)-1 if the size check fails. */
4352 if (size_check != NULL_TREE)
4354 tree errval = TYPE_MAX_VALUE (sizetype);
4355 if (cxx_dialect >= cxx11 && flag_exceptions)
4356 errval = throw_bad_array_new_length ();
4357 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4358 original_size, errval);
4360 vec_safe_insert (*args, 0, *size);
4361 *args = resolve_args (*args, complain);
4362 if (*args == NULL)
4363 return error_mark_node;
4365 /* Based on:
4367 [expr.new]
4369 If this lookup fails to find the name, or if the allocated type
4370 is not a class type, the allocation function's name is looked
4371 up in the global scope.
4373 we disregard block-scope declarations of "operator new". */
4374 fns = lookup_name_real (fnname, 0, 1, /*block_p=*/false, 0, 0);
4375 fns = lookup_arg_dependent (fnname, fns, *args);
4377 if (align_arg)
4379 vec<tree, va_gc>* align_args
4380 = vec_copy_and_insert (*args, align_arg, 1);
4381 cand = perform_overload_resolution (fns, align_args, &candidates,
4382 &any_viable_p, tf_none);
4383 /* If no aligned allocation function matches, try again without the
4384 alignment. */
4387 /* Figure out what function is being called. */
4388 if (!cand)
4389 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
4390 complain);
4392 /* If no suitable function could be found, issue an error message
4393 and give up. */
4394 if (!cand)
4396 if (complain & tf_error)
4397 print_error_for_call_failure (fns, *args, candidates);
4398 return error_mark_node;
4401 /* If a cookie is required, add some extra space. Whether
4402 or not a cookie is required cannot be determined until
4403 after we know which function was called. */
4404 if (*cookie_size)
4406 bool use_cookie = true;
4407 tree arg_types;
4409 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
4410 /* Skip the size_t parameter. */
4411 arg_types = TREE_CHAIN (arg_types);
4412 /* Check the remaining parameters (if any). */
4413 if (arg_types
4414 && TREE_CHAIN (arg_types) == void_list_node
4415 && same_type_p (TREE_VALUE (arg_types),
4416 ptr_type_node))
4417 use_cookie = false;
4418 /* If we need a cookie, adjust the number of bytes allocated. */
4419 if (use_cookie)
4421 /* Update the total size. */
4422 *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
4423 if (size_check)
4425 /* Set to (size_t)-1 if the size check fails. */
4426 gcc_assert (size_check != NULL_TREE);
4427 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4428 *size, TYPE_MAX_VALUE (sizetype));
4430 /* Update the argument list to reflect the adjusted size. */
4431 (**args)[0] = *size;
4433 else
4434 *cookie_size = NULL_TREE;
4437 /* Tell our caller which function we decided to call. */
4438 if (fn)
4439 *fn = cand->fn;
4441 /* Build the CALL_EXPR. */
4442 return build_over_call (cand, LOOKUP_NORMAL, complain);
4445 /* Build a new call to operator(). This may change ARGS. */
4447 static tree
4448 build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4450 struct z_candidate *candidates = 0, *cand;
4451 tree fns, convs, first_mem_arg = NULL_TREE;
4452 bool any_viable_p;
4453 tree result = NULL_TREE;
4454 void *p;
4456 obj = mark_lvalue_use (obj);
4458 if (error_operand_p (obj))
4459 return error_mark_node;
4461 tree type = TREE_TYPE (obj);
4463 obj = prep_operand (obj);
4465 if (TYPE_PTRMEMFUNC_P (type))
4467 if (complain & tf_error)
4468 /* It's no good looking for an overloaded operator() on a
4469 pointer-to-member-function. */
4470 error ("pointer-to-member function %qE cannot be called without "
4471 "an object; consider using %<.*%> or %<->*%>", obj);
4472 return error_mark_node;
4475 if (TYPE_BINFO (type))
4477 fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1);
4478 if (fns == error_mark_node)
4479 return error_mark_node;
4481 else
4482 fns = NULL_TREE;
4484 if (args != NULL && *args != NULL)
4486 *args = resolve_args (*args, complain);
4487 if (*args == NULL)
4488 return error_mark_node;
4491 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4492 p = conversion_obstack_alloc (0);
4494 if (fns)
4496 first_mem_arg = obj;
4498 add_candidates (BASELINK_FUNCTIONS (fns),
4499 first_mem_arg, *args, NULL_TREE,
4500 NULL_TREE, false,
4501 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
4502 LOOKUP_NORMAL, &candidates, complain);
4505 convs = lookup_conversions (type);
4507 for (; convs; convs = TREE_CHAIN (convs))
4509 tree totype = TREE_TYPE (convs);
4511 if (TYPE_PTRFN_P (totype)
4512 || TYPE_REFFN_P (totype)
4513 || (TREE_CODE (totype) == REFERENCE_TYPE
4514 && TYPE_PTRFN_P (TREE_TYPE (totype))))
4515 for (ovl_iterator iter (TREE_VALUE (convs)); iter; ++iter)
4517 tree fn = *iter;
4519 if (DECL_NONCONVERTING_P (fn))
4520 continue;
4522 if (TREE_CODE (fn) == TEMPLATE_DECL)
4523 add_template_conv_candidate
4524 (&candidates, fn, obj, *args, totype,
4525 /*access_path=*/NULL_TREE,
4526 /*conversion_path=*/NULL_TREE, complain);
4527 else
4528 add_conv_candidate (&candidates, fn, obj,
4529 *args, /*conversion_path=*/NULL_TREE,
4530 /*access_path=*/NULL_TREE, complain);
4534 /* Be strict here because if we choose a bad conversion candidate, the
4535 errors we get won't mention the call context. */
4536 candidates = splice_viable (candidates, true, &any_viable_p);
4537 if (!any_viable_p)
4539 if (complain & tf_error)
4541 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
4542 build_tree_list_vec (*args));
4543 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4545 result = error_mark_node;
4547 else
4549 cand = tourney (candidates, complain);
4550 if (cand == 0)
4552 if (complain & tf_error)
4554 error ("call of %<(%T) (%A)%> is ambiguous",
4555 TREE_TYPE (obj), build_tree_list_vec (*args));
4556 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4558 result = error_mark_node;
4560 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
4561 && DECL_OVERLOADED_OPERATOR_P (cand->fn)
4562 && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
4563 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4564 else
4566 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
4567 obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
4568 -1, complain);
4569 else
4571 gcc_checking_assert (TYPE_P (cand->fn));
4572 obj = convert_like (cand->convs[0], obj, complain);
4574 obj = convert_from_reference (obj);
4575 result = cp_build_function_call_vec (obj, args, complain);
4579 /* Free all the conversions we allocated. */
4580 obstack_free (&conversion_obstack, p);
4582 return result;
4585 /* Wrapper for above. */
4587 tree
4588 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4590 tree ret;
4591 bool subtime = timevar_cond_start (TV_OVERLOAD);
4592 ret = build_op_call_1 (obj, args, complain);
4593 timevar_cond_stop (TV_OVERLOAD, subtime);
4594 return ret;
4597 /* Called by op_error to prepare format strings suitable for the error
4598 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4599 and a suffix (controlled by NTYPES). */
4601 static const char *
4602 op_error_string (const char *errmsg, int ntypes, bool match)
4604 const char *msg;
4606 const char *msgp = concat (match ? G_("ambiguous overload for ")
4607 : G_("no match for "), errmsg, NULL);
4609 if (ntypes == 3)
4610 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
4611 else if (ntypes == 2)
4612 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
4613 else
4614 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
4616 return msg;
4619 static void
4620 op_error (location_t loc, enum tree_code code, enum tree_code code2,
4621 tree arg1, tree arg2, tree arg3, bool match)
4623 bool assop = code == MODIFY_EXPR;
4624 const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
4626 switch (code)
4628 case COND_EXPR:
4629 if (flag_diagnostics_show_caret)
4630 error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
4631 3, match),
4632 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4633 else
4634 error_at (loc, op_error_string (G_("ternary %<operator?:%> "
4635 "in %<%E ? %E : %E%>"), 3, match),
4636 arg1, arg2, arg3,
4637 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4638 break;
4640 case POSTINCREMENT_EXPR:
4641 case POSTDECREMENT_EXPR:
4642 if (flag_diagnostics_show_caret)
4643 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4644 opname, TREE_TYPE (arg1));
4645 else
4646 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4647 1, match),
4648 opname, arg1, opname, TREE_TYPE (arg1));
4649 break;
4651 case ARRAY_REF:
4652 if (flag_diagnostics_show_caret)
4653 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
4654 TREE_TYPE (arg1), TREE_TYPE (arg2));
4655 else
4656 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
4657 2, match),
4658 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
4659 break;
4661 case REALPART_EXPR:
4662 case IMAGPART_EXPR:
4663 if (flag_diagnostics_show_caret)
4664 error_at (loc, op_error_string (G_("%qs"), 1, match),
4665 opname, TREE_TYPE (arg1));
4666 else
4667 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
4668 opname, opname, arg1, TREE_TYPE (arg1));
4669 break;
4671 default:
4672 if (arg2)
4673 if (flag_diagnostics_show_caret)
4674 error_at (loc, op_error_string (G_("%<operator%s%>"), 2, match),
4675 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
4676 else
4677 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
4678 2, match),
4679 opname, arg1, opname, arg2,
4680 TREE_TYPE (arg1), TREE_TYPE (arg2));
4681 else
4682 if (flag_diagnostics_show_caret)
4683 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4684 opname, TREE_TYPE (arg1));
4685 else
4686 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
4687 1, match),
4688 opname, opname, arg1, TREE_TYPE (arg1));
4689 break;
4693 /* Return the implicit conversion sequence that could be used to
4694 convert E1 to E2 in [expr.cond]. */
4696 static conversion *
4697 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
4699 tree t1 = non_reference (TREE_TYPE (e1));
4700 tree t2 = non_reference (TREE_TYPE (e2));
4701 conversion *conv;
4702 bool good_base;
4704 /* [expr.cond]
4706 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
4707 implicitly converted (clause _conv_) to the type "lvalue reference to
4708 T2", subject to the constraint that in the conversion the
4709 reference must bind directly (_dcl.init.ref_) to an lvalue.
4711 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
4712 implicitly converted to the type "rvalue reference to T2", subject to
4713 the constraint that the reference must bind directly. */
4714 if (glvalue_p (e2))
4716 tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
4717 conv = implicit_conversion (rtype,
4720 /*c_cast_p=*/false,
4721 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
4722 |LOOKUP_ONLYCONVERTING,
4723 complain);
4724 if (conv && !conv->bad_p)
4725 return conv;
4728 /* If E2 is a prvalue or if neither of the conversions above can be done
4729 and at least one of the operands has (possibly cv-qualified) class
4730 type: */
4731 if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
4732 return NULL;
4734 /* [expr.cond]
4736 If E1 and E2 have class type, and the underlying class types are
4737 the same or one is a base class of the other: E1 can be converted
4738 to match E2 if the class of T2 is the same type as, or a base
4739 class of, the class of T1, and the cv-qualification of T2 is the
4740 same cv-qualification as, or a greater cv-qualification than, the
4741 cv-qualification of T1. If the conversion is applied, E1 is
4742 changed to an rvalue of type T2 that still refers to the original
4743 source class object (or the appropriate subobject thereof). */
4744 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
4745 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
4747 if (good_base && at_least_as_qualified_p (t2, t1))
4749 conv = build_identity_conv (t1, e1);
4750 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
4751 TYPE_MAIN_VARIANT (t2)))
4752 conv = build_conv (ck_base, t2, conv);
4753 else
4754 conv = build_conv (ck_rvalue, t2, conv);
4755 return conv;
4757 else
4758 return NULL;
4760 else
4761 /* [expr.cond]
4763 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
4764 converted to the type that expression E2 would have if E2 were
4765 converted to an rvalue (or the type it has, if E2 is an rvalue). */
4766 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
4767 LOOKUP_IMPLICIT, complain);
4770 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
4771 arguments to the conditional expression. */
4773 static tree
4774 build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
4775 tsubst_flags_t complain)
4777 tree arg2_type;
4778 tree arg3_type;
4779 tree result = NULL_TREE;
4780 tree result_type = NULL_TREE;
4781 bool is_lvalue = true;
4782 struct z_candidate *candidates = 0;
4783 struct z_candidate *cand;
4784 void *p;
4785 tree orig_arg2, orig_arg3;
4787 /* As a G++ extension, the second argument to the conditional can be
4788 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
4789 c'.) If the second operand is omitted, make sure it is
4790 calculated only once. */
4791 if (!arg2)
4793 if (complain & tf_error)
4794 pedwarn (loc, OPT_Wpedantic,
4795 "ISO C++ forbids omitting the middle term of a ?: expression");
4797 if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
4798 warn_for_omitted_condop (loc, arg1);
4800 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
4801 if (lvalue_p (arg1))
4802 arg2 = arg1 = cp_stabilize_reference (arg1);
4803 else
4804 arg2 = arg1 = save_expr (arg1);
4807 /* If something has already gone wrong, just pass that fact up the
4808 tree. */
4809 if (error_operand_p (arg1)
4810 || error_operand_p (arg2)
4811 || error_operand_p (arg3))
4812 return error_mark_node;
4814 orig_arg2 = arg2;
4815 orig_arg3 = arg3;
4817 if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
4819 tree arg1_type = TREE_TYPE (arg1);
4821 /* If arg1 is another cond_expr choosing between -1 and 0,
4822 then we can use its comparison. It may help to avoid
4823 additional comparison, produce more accurate diagnostics
4824 and enables folding. */
4825 if (TREE_CODE (arg1) == VEC_COND_EXPR
4826 && integer_minus_onep (TREE_OPERAND (arg1, 1))
4827 && integer_zerop (TREE_OPERAND (arg1, 2)))
4828 arg1 = TREE_OPERAND (arg1, 0);
4830 arg1 = force_rvalue (arg1, complain);
4831 arg2 = force_rvalue (arg2, complain);
4832 arg3 = force_rvalue (arg3, complain);
4834 /* force_rvalue can return error_mark on valid arguments. */
4835 if (error_operand_p (arg1)
4836 || error_operand_p (arg2)
4837 || error_operand_p (arg3))
4838 return error_mark_node;
4840 arg2_type = TREE_TYPE (arg2);
4841 arg3_type = TREE_TYPE (arg3);
4843 if (!VECTOR_TYPE_P (arg2_type)
4844 && !VECTOR_TYPE_P (arg3_type))
4846 /* Rely on the error messages of the scalar version. */
4847 tree scal = build_conditional_expr_1 (loc, integer_one_node,
4848 orig_arg2, orig_arg3, complain);
4849 if (scal == error_mark_node)
4850 return error_mark_node;
4851 tree stype = TREE_TYPE (scal);
4852 tree ctype = TREE_TYPE (arg1_type);
4853 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
4854 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
4856 if (complain & tf_error)
4857 error_at (loc, "inferred scalar type %qT is not an integer or "
4858 "floating point type of the same size as %qT", stype,
4859 COMPARISON_CLASS_P (arg1)
4860 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4861 : ctype);
4862 return error_mark_node;
4865 tree vtype = build_opaque_vector_type (stype,
4866 TYPE_VECTOR_SUBPARTS (arg1_type));
4867 /* We could pass complain & tf_warning to unsafe_conversion_p,
4868 but the warnings (like Wsign-conversion) have already been
4869 given by the scalar build_conditional_expr_1. We still check
4870 unsafe_conversion_p to forbid truncating long long -> float. */
4871 if (unsafe_conversion_p (loc, stype, arg2, NULL_TREE, false))
4873 if (complain & tf_error)
4874 error_at (loc, "conversion of scalar %qH to vector %qI "
4875 "involves truncation", arg2_type, vtype);
4876 return error_mark_node;
4878 if (unsafe_conversion_p (loc, stype, arg3, NULL_TREE, false))
4880 if (complain & tf_error)
4881 error_at (loc, "conversion of scalar %qH to vector %qI "
4882 "involves truncation", arg3_type, vtype);
4883 return error_mark_node;
4886 arg2 = cp_convert (stype, arg2, complain);
4887 arg2 = save_expr (arg2);
4888 arg2 = build_vector_from_val (vtype, arg2);
4889 arg2_type = vtype;
4890 arg3 = cp_convert (stype, arg3, complain);
4891 arg3 = save_expr (arg3);
4892 arg3 = build_vector_from_val (vtype, arg3);
4893 arg3_type = vtype;
4896 if (VECTOR_TYPE_P (arg2_type) != VECTOR_TYPE_P (arg3_type))
4898 enum stv_conv convert_flag =
4899 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
4900 complain & tf_error);
4902 switch (convert_flag)
4904 case stv_error:
4905 return error_mark_node;
4906 case stv_firstarg:
4908 arg2 = save_expr (arg2);
4909 arg2 = convert (TREE_TYPE (arg3_type), arg2);
4910 arg2 = build_vector_from_val (arg3_type, arg2);
4911 arg2_type = TREE_TYPE (arg2);
4912 break;
4914 case stv_secondarg:
4916 arg3 = save_expr (arg3);
4917 arg3 = convert (TREE_TYPE (arg2_type), arg3);
4918 arg3 = build_vector_from_val (arg2_type, arg3);
4919 arg3_type = TREE_TYPE (arg3);
4920 break;
4922 default:
4923 break;
4927 if (!same_type_p (arg2_type, arg3_type)
4928 || TYPE_VECTOR_SUBPARTS (arg1_type)
4929 != TYPE_VECTOR_SUBPARTS (arg2_type)
4930 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
4932 if (complain & tf_error)
4933 error_at (loc,
4934 "incompatible vector types in conditional expression: "
4935 "%qT, %qT and %qT", TREE_TYPE (arg1),
4936 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
4937 return error_mark_node;
4940 if (!COMPARISON_CLASS_P (arg1))
4942 tree cmp_type = build_same_sized_truth_vector_type (arg1_type);
4943 arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
4945 return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
4948 /* [expr.cond]
4950 The first expression is implicitly converted to bool (clause
4951 _conv_). */
4952 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
4953 LOOKUP_NORMAL);
4954 if (error_operand_p (arg1))
4955 return error_mark_node;
4957 /* [expr.cond]
4959 If either the second or the third operand has type (possibly
4960 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
4961 array-to-pointer (_conv.array_), and function-to-pointer
4962 (_conv.func_) standard conversions are performed on the second
4963 and third operands. */
4964 arg2_type = unlowered_expr_type (arg2);
4965 arg3_type = unlowered_expr_type (arg3);
4966 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
4968 /* Do the conversions. We don't these for `void' type arguments
4969 since it can't have any effect and since decay_conversion
4970 does not handle that case gracefully. */
4971 if (!VOID_TYPE_P (arg2_type))
4972 arg2 = decay_conversion (arg2, complain);
4973 if (!VOID_TYPE_P (arg3_type))
4974 arg3 = decay_conversion (arg3, complain);
4975 arg2_type = TREE_TYPE (arg2);
4976 arg3_type = TREE_TYPE (arg3);
4978 /* [expr.cond]
4980 One of the following shall hold:
4982 --The second or the third operand (but not both) is a
4983 throw-expression (_except.throw_); the result is of the
4984 type of the other and is an rvalue.
4986 --Both the second and the third operands have type void; the
4987 result is of type void and is an rvalue.
4989 We must avoid calling force_rvalue for expressions of type
4990 "void" because it will complain that their value is being
4991 used. */
4992 if (TREE_CODE (arg2) == THROW_EXPR
4993 && TREE_CODE (arg3) != THROW_EXPR)
4995 if (!VOID_TYPE_P (arg3_type))
4997 arg3 = force_rvalue (arg3, complain);
4998 if (arg3 == error_mark_node)
4999 return error_mark_node;
5001 arg3_type = TREE_TYPE (arg3);
5002 result_type = arg3_type;
5004 else if (TREE_CODE (arg2) != THROW_EXPR
5005 && TREE_CODE (arg3) == THROW_EXPR)
5007 if (!VOID_TYPE_P (arg2_type))
5009 arg2 = force_rvalue (arg2, complain);
5010 if (arg2 == error_mark_node)
5011 return error_mark_node;
5013 arg2_type = TREE_TYPE (arg2);
5014 result_type = arg2_type;
5016 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
5017 result_type = void_type_node;
5018 else
5020 if (complain & tf_error)
5022 if (VOID_TYPE_P (arg2_type))
5023 error_at (EXPR_LOC_OR_LOC (arg3, loc),
5024 "second operand to the conditional operator "
5025 "is of type %<void%>, but the third operand is "
5026 "neither a throw-expression nor of type %<void%>");
5027 else
5028 error_at (EXPR_LOC_OR_LOC (arg2, loc),
5029 "third operand to the conditional operator "
5030 "is of type %<void%>, but the second operand is "
5031 "neither a throw-expression nor of type %<void%>");
5033 return error_mark_node;
5036 is_lvalue = false;
5037 goto valid_operands;
5039 /* [expr.cond]
5041 Otherwise, if the second and third operand have different types,
5042 and either has (possibly cv-qualified) class type, or if both are
5043 glvalues of the same value category and the same type except for
5044 cv-qualification, an attempt is made to convert each of those operands
5045 to the type of the other. */
5046 else if (!same_type_p (arg2_type, arg3_type)
5047 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
5048 || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
5049 arg3_type)
5050 && glvalue_p (arg2) && glvalue_p (arg3)
5051 && lvalue_p (arg2) == lvalue_p (arg3))))
5053 conversion *conv2;
5054 conversion *conv3;
5055 bool converted = false;
5057 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5058 p = conversion_obstack_alloc (0);
5060 conv2 = conditional_conversion (arg2, arg3, complain);
5061 conv3 = conditional_conversion (arg3, arg2, complain);
5063 /* [expr.cond]
5065 If both can be converted, or one can be converted but the
5066 conversion is ambiguous, the program is ill-formed. If
5067 neither can be converted, the operands are left unchanged and
5068 further checking is performed as described below. If exactly
5069 one conversion is possible, that conversion is applied to the
5070 chosen operand and the converted operand is used in place of
5071 the original operand for the remainder of this section. */
5072 if ((conv2 && !conv2->bad_p
5073 && conv3 && !conv3->bad_p)
5074 || (conv2 && conv2->kind == ck_ambig)
5075 || (conv3 && conv3->kind == ck_ambig))
5077 if (complain & tf_error)
5079 error_at (loc, "operands to ?: have different types %qT and %qT",
5080 arg2_type, arg3_type);
5081 if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
5082 inform (loc, " and each type can be converted to the other");
5083 else if (conv2 && conv2->kind == ck_ambig)
5084 convert_like (conv2, arg2, complain);
5085 else
5086 convert_like (conv3, arg3, complain);
5088 result = error_mark_node;
5090 else if (conv2 && !conv2->bad_p)
5092 arg2 = convert_like (conv2, arg2, complain);
5093 arg2 = convert_from_reference (arg2);
5094 arg2_type = TREE_TYPE (arg2);
5095 /* Even if CONV2 is a valid conversion, the result of the
5096 conversion may be invalid. For example, if ARG3 has type
5097 "volatile X", and X does not have a copy constructor
5098 accepting a "volatile X&", then even if ARG2 can be
5099 converted to X, the conversion will fail. */
5100 if (error_operand_p (arg2))
5101 result = error_mark_node;
5102 converted = true;
5104 else if (conv3 && !conv3->bad_p)
5106 arg3 = convert_like (conv3, arg3, complain);
5107 arg3 = convert_from_reference (arg3);
5108 arg3_type = TREE_TYPE (arg3);
5109 if (error_operand_p (arg3))
5110 result = error_mark_node;
5111 converted = true;
5114 /* Free all the conversions we allocated. */
5115 obstack_free (&conversion_obstack, p);
5117 if (result)
5118 return result;
5120 /* If, after the conversion, both operands have class type,
5121 treat the cv-qualification of both operands as if it were the
5122 union of the cv-qualification of the operands.
5124 The standard is not clear about what to do in this
5125 circumstance. For example, if the first operand has type
5126 "const X" and the second operand has a user-defined
5127 conversion to "volatile X", what is the type of the second
5128 operand after this step? Making it be "const X" (matching
5129 the first operand) seems wrong, as that discards the
5130 qualification without actually performing a copy. Leaving it
5131 as "volatile X" seems wrong as that will result in the
5132 conditional expression failing altogether, even though,
5133 according to this step, the one operand could be converted to
5134 the type of the other. */
5135 if (converted
5136 && CLASS_TYPE_P (arg2_type)
5137 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
5138 arg2_type = arg3_type =
5139 cp_build_qualified_type (arg2_type,
5140 cp_type_quals (arg2_type)
5141 | cp_type_quals (arg3_type));
5144 /* [expr.cond]
5146 If the second and third operands are glvalues of the same value
5147 category and have the same type, the result is of that type and
5148 value category. */
5149 if (((lvalue_p (arg2) && lvalue_p (arg3))
5150 || (xvalue_p (arg2) && xvalue_p (arg3)))
5151 && same_type_p (arg2_type, arg3_type))
5153 result_type = arg2_type;
5154 arg2 = mark_lvalue_use (arg2);
5155 arg3 = mark_lvalue_use (arg3);
5156 goto valid_operands;
5159 /* [expr.cond]
5161 Otherwise, the result is an rvalue. If the second and third
5162 operand do not have the same type, and either has (possibly
5163 cv-qualified) class type, overload resolution is used to
5164 determine the conversions (if any) to be applied to the operands
5165 (_over.match.oper_, _over.built_). */
5166 is_lvalue = false;
5167 if (!same_type_p (arg2_type, arg3_type)
5168 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
5170 tree args[3];
5171 conversion *conv;
5172 bool any_viable_p;
5174 /* Rearrange the arguments so that add_builtin_candidate only has
5175 to know about two args. In build_builtin_candidate, the
5176 arguments are unscrambled. */
5177 args[0] = arg2;
5178 args[1] = arg3;
5179 args[2] = arg1;
5180 add_builtin_candidates (&candidates,
5181 COND_EXPR,
5182 NOP_EXPR,
5183 ovl_op_identifier (false, COND_EXPR),
5184 args,
5185 LOOKUP_NORMAL, complain);
5187 /* [expr.cond]
5189 If the overload resolution fails, the program is
5190 ill-formed. */
5191 candidates = splice_viable (candidates, false, &any_viable_p);
5192 if (!any_viable_p)
5194 if (complain & tf_error)
5195 error_at (loc, "operands to ?: have different types %qT and %qT",
5196 arg2_type, arg3_type);
5197 return error_mark_node;
5199 cand = tourney (candidates, complain);
5200 if (!cand)
5202 if (complain & tf_error)
5204 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
5205 print_z_candidates (loc, candidates);
5207 return error_mark_node;
5210 /* [expr.cond]
5212 Otherwise, the conversions thus determined are applied, and
5213 the converted operands are used in place of the original
5214 operands for the remainder of this section. */
5215 conv = cand->convs[0];
5216 arg1 = convert_like (conv, arg1, complain);
5217 conv = cand->convs[1];
5218 arg2 = convert_like (conv, arg2, complain);
5219 arg2_type = TREE_TYPE (arg2);
5220 conv = cand->convs[2];
5221 arg3 = convert_like (conv, arg3, complain);
5222 arg3_type = TREE_TYPE (arg3);
5225 /* [expr.cond]
5227 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
5228 and function-to-pointer (_conv.func_) standard conversions are
5229 performed on the second and third operands.
5231 We need to force the lvalue-to-rvalue conversion here for class types,
5232 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
5233 that isn't wrapped with a TARGET_EXPR plays havoc with exception
5234 regions. */
5236 arg2 = force_rvalue (arg2, complain);
5237 if (!CLASS_TYPE_P (arg2_type))
5238 arg2_type = TREE_TYPE (arg2);
5240 arg3 = force_rvalue (arg3, complain);
5241 if (!CLASS_TYPE_P (arg3_type))
5242 arg3_type = TREE_TYPE (arg3);
5244 if (arg2 == error_mark_node || arg3 == error_mark_node)
5245 return error_mark_node;
5247 /* [expr.cond]
5249 After those conversions, one of the following shall hold:
5251 --The second and third operands have the same type; the result is of
5252 that type. */
5253 if (same_type_p (arg2_type, arg3_type))
5254 result_type = arg2_type;
5255 /* [expr.cond]
5257 --The second and third operands have arithmetic or enumeration
5258 type; the usual arithmetic conversions are performed to bring
5259 them to a common type, and the result is of that type. */
5260 else if ((ARITHMETIC_TYPE_P (arg2_type)
5261 || UNSCOPED_ENUM_P (arg2_type))
5262 && (ARITHMETIC_TYPE_P (arg3_type)
5263 || UNSCOPED_ENUM_P (arg3_type)))
5265 /* In this case, there is always a common type. */
5266 result_type = type_after_usual_arithmetic_conversions (arg2_type,
5267 arg3_type);
5268 if (complain & tf_warning)
5269 do_warn_double_promotion (result_type, arg2_type, arg3_type,
5270 "implicit conversion from %qH to %qI to "
5271 "match other result of conditional",
5272 loc);
5274 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
5275 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
5277 if (TREE_CODE (orig_arg2) == CONST_DECL
5278 && TREE_CODE (orig_arg3) == CONST_DECL
5279 && DECL_CONTEXT (orig_arg2) == DECL_CONTEXT (orig_arg3))
5280 /* Two enumerators from the same enumeration can have different
5281 types when the enumeration is still being defined. */;
5282 else if (complain & tf_warning)
5283 warning_at (loc, OPT_Wenum_compare, "enumeral mismatch in "
5284 "conditional expression: %qT vs %qT",
5285 arg2_type, arg3_type);
5287 else if (extra_warnings
5288 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
5289 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
5290 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
5291 && !same_type_p (arg2_type,
5292 type_promotes_to (arg3_type)))))
5294 if (complain & tf_warning)
5295 warning_at (loc, OPT_Wextra, "enumeral and non-enumeral type in "
5296 "conditional expression");
5299 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5300 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5302 /* [expr.cond]
5304 --The second and third operands have pointer type, or one has
5305 pointer type and the other is a null pointer constant; pointer
5306 conversions (_conv.ptr_) and qualification conversions
5307 (_conv.qual_) are performed to bring them to their composite
5308 pointer type (_expr.rel_). The result is of the composite
5309 pointer type.
5311 --The second and third operands have pointer to member type, or
5312 one has pointer to member type and the other is a null pointer
5313 constant; pointer to member conversions (_conv.mem_) and
5314 qualification conversions (_conv.qual_) are performed to bring
5315 them to a common type, whose cv-qualification shall match the
5316 cv-qualification of either the second or the third operand.
5317 The result is of the common type. */
5318 else if ((null_ptr_cst_p (arg2)
5319 && TYPE_PTR_OR_PTRMEM_P (arg3_type))
5320 || (null_ptr_cst_p (arg3)
5321 && TYPE_PTR_OR_PTRMEM_P (arg2_type))
5322 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
5323 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
5324 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
5326 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
5327 arg3, CPO_CONDITIONAL_EXPR,
5328 complain);
5329 if (result_type == error_mark_node)
5330 return error_mark_node;
5331 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5332 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5335 if (!result_type)
5337 if (complain & tf_error)
5338 error_at (loc, "operands to ?: have different types %qT and %qT",
5339 arg2_type, arg3_type);
5340 return error_mark_node;
5343 if (arg2 == error_mark_node || arg3 == error_mark_node)
5344 return error_mark_node;
5346 valid_operands:
5347 result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
5349 /* If the ARG2 and ARG3 are the same and don't have side-effects,
5350 warn here, because the COND_EXPR will be turned into ARG2. */
5351 if (warn_duplicated_branches
5352 && (arg2 == arg3 || operand_equal_p (arg2, arg3, 0)))
5353 warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
5354 "this condition has identical branches");
5356 /* We can't use result_type below, as fold might have returned a
5357 throw_expr. */
5359 if (!is_lvalue)
5361 /* Expand both sides into the same slot, hopefully the target of
5362 the ?: expression. We used to check for TARGET_EXPRs here,
5363 but now we sometimes wrap them in NOP_EXPRs so the test would
5364 fail. */
5365 if (CLASS_TYPE_P (TREE_TYPE (result)))
5366 result = get_target_expr_sfinae (result, complain);
5367 /* If this expression is an rvalue, but might be mistaken for an
5368 lvalue, we must add a NON_LVALUE_EXPR. */
5369 result = rvalue (result);
5371 else
5372 result = force_paren_expr (result);
5374 return result;
5377 /* Wrapper for above. */
5379 tree
5380 build_conditional_expr (location_t loc, tree arg1, tree arg2, tree arg3,
5381 tsubst_flags_t complain)
5383 tree ret;
5384 bool subtime = timevar_cond_start (TV_OVERLOAD);
5385 ret = build_conditional_expr_1 (loc, arg1, arg2, arg3, complain);
5386 timevar_cond_stop (TV_OVERLOAD, subtime);
5387 return ret;
5390 /* OPERAND is an operand to an expression. Perform necessary steps
5391 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
5392 returned. */
5394 static tree
5395 prep_operand (tree operand)
5397 if (operand)
5399 if (CLASS_TYPE_P (TREE_TYPE (operand))
5400 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
5401 /* Make sure the template type is instantiated now. */
5402 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
5405 return operand;
5408 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5409 OVERLOAD) to the CANDIDATES, returning an updated list of
5410 CANDIDATES. The ARGS are the arguments provided to the call;
5411 if FIRST_ARG is non-null it is the implicit object argument,
5412 otherwise the first element of ARGS is used if needed. The
5413 EXPLICIT_TARGS are explicit template arguments provided.
5414 TEMPLATE_ONLY is true if only template functions should be
5415 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5416 add_function_candidate. */
5418 static void
5419 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
5420 tree return_type,
5421 tree explicit_targs, bool template_only,
5422 tree conversion_path, tree access_path,
5423 int flags,
5424 struct z_candidate **candidates,
5425 tsubst_flags_t complain)
5427 tree ctype;
5428 const vec<tree, va_gc> *non_static_args;
5429 bool check_list_ctor = false;
5430 bool check_converting = false;
5431 unification_kind_t strict;
5433 if (!fns)
5434 return;
5436 /* Precalculate special handling of constructors and conversion ops. */
5437 tree fn = OVL_FIRST (fns);
5438 if (DECL_CONV_FN_P (fn))
5440 check_list_ctor = false;
5441 check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
5442 if (flags & LOOKUP_NO_CONVERSION)
5443 /* We're doing return_type(x). */
5444 strict = DEDUCE_CONV;
5445 else
5446 /* We're doing x.operator return_type(). */
5447 strict = DEDUCE_EXACT;
5448 /* [over.match.funcs] For conversion functions, the function
5449 is considered to be a member of the class of the implicit
5450 object argument for the purpose of defining the type of
5451 the implicit object parameter. */
5452 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
5454 else
5456 if (DECL_CONSTRUCTOR_P (fn))
5458 check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
5459 /* For list-initialization we consider explicit constructors
5460 and complain if one is chosen. */
5461 check_converting
5462 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
5463 == LOOKUP_ONLYCONVERTING);
5465 strict = DEDUCE_CALL;
5466 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
5469 if (first_arg)
5470 non_static_args = args;
5471 else
5472 /* Delay creating the implicit this parameter until it is needed. */
5473 non_static_args = NULL;
5475 for (lkp_iterator iter (fns); iter; ++iter)
5477 fn = *iter;
5479 if (check_converting && DECL_NONCONVERTING_P (fn))
5480 continue;
5481 if (check_list_ctor && !is_list_ctor (fn))
5482 continue;
5484 tree fn_first_arg = NULL_TREE;
5485 const vec<tree, va_gc> *fn_args = args;
5487 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
5489 /* Figure out where the object arg comes from. If this
5490 function is a non-static member and we didn't get an
5491 implicit object argument, move it out of args. */
5492 if (first_arg == NULL_TREE)
5494 unsigned int ix;
5495 tree arg;
5496 vec<tree, va_gc> *tempvec;
5497 vec_alloc (tempvec, args->length () - 1);
5498 for (ix = 1; args->iterate (ix, &arg); ++ix)
5499 tempvec->quick_push (arg);
5500 non_static_args = tempvec;
5501 first_arg = (*args)[0];
5504 fn_first_arg = first_arg;
5505 fn_args = non_static_args;
5508 if (TREE_CODE (fn) == TEMPLATE_DECL)
5509 add_template_candidate (candidates,
5511 ctype,
5512 explicit_targs,
5513 fn_first_arg,
5514 fn_args,
5515 return_type,
5516 access_path,
5517 conversion_path,
5518 flags,
5519 strict,
5520 complain);
5521 else if (!template_only)
5522 add_function_candidate (candidates,
5524 ctype,
5525 fn_first_arg,
5526 fn_args,
5527 access_path,
5528 conversion_path,
5529 flags,
5530 complain);
5534 /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
5535 -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
5537 static int
5538 op_is_ordered (tree_code code)
5540 switch (code)
5542 // 5. b @= a
5543 case MODIFY_EXPR:
5544 return (flag_strong_eval_order > 1 ? -1 : 0);
5546 // 6. a[b]
5547 case ARRAY_REF:
5548 return (flag_strong_eval_order > 1 ? 1 : 0);
5550 // 1. a.b
5551 // Not overloadable (yet).
5552 // 2. a->b
5553 // Only one argument.
5554 // 3. a->*b
5555 case MEMBER_REF:
5556 // 7. a << b
5557 case LSHIFT_EXPR:
5558 // 8. a >> b
5559 case RSHIFT_EXPR:
5560 return (flag_strong_eval_order ? 1 : 0);
5562 default:
5563 return 0;
5567 static tree
5568 build_new_op_1 (location_t loc, enum tree_code code, int flags, tree arg1,
5569 tree arg2, tree arg3, tree *overload, tsubst_flags_t complain)
5571 struct z_candidate *candidates = 0, *cand;
5572 vec<tree, va_gc> *arglist;
5573 tree args[3];
5574 tree result = NULL_TREE;
5575 bool result_valid_p = false;
5576 enum tree_code code2 = NOP_EXPR;
5577 enum tree_code code_orig_arg1 = ERROR_MARK;
5578 enum tree_code code_orig_arg2 = ERROR_MARK;
5579 conversion *conv;
5580 void *p;
5581 bool strict_p;
5582 bool any_viable_p;
5584 if (error_operand_p (arg1)
5585 || error_operand_p (arg2)
5586 || error_operand_p (arg3))
5587 return error_mark_node;
5589 bool ismodop = code == MODIFY_EXPR;
5590 if (ismodop)
5592 code2 = TREE_CODE (arg3);
5593 arg3 = NULL_TREE;
5595 tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
5597 arg1 = prep_operand (arg1);
5599 bool memonly = false;
5600 switch (code)
5602 case NEW_EXPR:
5603 case VEC_NEW_EXPR:
5604 case VEC_DELETE_EXPR:
5605 case DELETE_EXPR:
5606 /* Use build_op_new_call and build_op_delete_call instead. */
5607 gcc_unreachable ();
5609 case CALL_EXPR:
5610 /* Use build_op_call instead. */
5611 gcc_unreachable ();
5613 case TRUTH_ORIF_EXPR:
5614 case TRUTH_ANDIF_EXPR:
5615 case TRUTH_AND_EXPR:
5616 case TRUTH_OR_EXPR:
5617 /* These are saved for the sake of warn_logical_operator. */
5618 code_orig_arg1 = TREE_CODE (arg1);
5619 code_orig_arg2 = TREE_CODE (arg2);
5620 break;
5621 case GT_EXPR:
5622 case LT_EXPR:
5623 case GE_EXPR:
5624 case LE_EXPR:
5625 case EQ_EXPR:
5626 case NE_EXPR:
5627 /* These are saved for the sake of maybe_warn_bool_compare. */
5628 code_orig_arg1 = TREE_CODE (TREE_TYPE (arg1));
5629 code_orig_arg2 = TREE_CODE (TREE_TYPE (arg2));
5630 break;
5632 /* =, ->, [], () must be non-static member functions. */
5633 case MODIFY_EXPR:
5634 if (code2 != NOP_EXPR)
5635 break;
5636 /* FALLTHRU */
5637 case COMPONENT_REF:
5638 case ARRAY_REF:
5639 memonly = true;
5640 break;
5642 default:
5643 break;
5646 arg2 = prep_operand (arg2);
5647 arg3 = prep_operand (arg3);
5649 if (code == COND_EXPR)
5650 /* Use build_conditional_expr instead. */
5651 gcc_unreachable ();
5652 else if (! OVERLOAD_TYPE_P (TREE_TYPE (arg1))
5653 && (! arg2 || ! OVERLOAD_TYPE_P (TREE_TYPE (arg2))))
5654 goto builtin;
5656 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5657 arg2 = integer_zero_node;
5659 vec_alloc (arglist, 3);
5660 arglist->quick_push (arg1);
5661 if (arg2 != NULL_TREE)
5662 arglist->quick_push (arg2);
5663 if (arg3 != NULL_TREE)
5664 arglist->quick_push (arg3);
5666 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5667 p = conversion_obstack_alloc (0);
5669 /* Add namespace-scope operators to the list of functions to
5670 consider. */
5671 if (!memonly)
5673 tree fns = lookup_name_real (fnname, 0, 1, /*block_p=*/true, 0, 0);
5674 fns = lookup_arg_dependent (fnname, fns, arglist);
5675 add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
5676 NULL_TREE, false, NULL_TREE, NULL_TREE,
5677 flags, &candidates, complain);
5680 args[0] = arg1;
5681 args[1] = arg2;
5682 args[2] = NULL_TREE;
5684 /* Add class-member operators to the candidate set. */
5685 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
5687 tree fns;
5689 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
5690 if (fns == error_mark_node)
5692 result = error_mark_node;
5693 goto user_defined_result_ready;
5695 if (fns)
5696 add_candidates (BASELINK_FUNCTIONS (fns),
5697 NULL_TREE, arglist, NULL_TREE,
5698 NULL_TREE, false,
5699 BASELINK_BINFO (fns),
5700 BASELINK_ACCESS_BINFO (fns),
5701 flags, &candidates, complain);
5703 /* Per 13.3.1.2/3, 2nd bullet, if no operand has a class type, then
5704 only non-member functions that have type T1 or reference to
5705 cv-qualified-opt T1 for the first argument, if the first argument
5706 has an enumeration type, or T2 or reference to cv-qualified-opt
5707 T2 for the second argument, if the second argument has an
5708 enumeration type. Filter out those that don't match. */
5709 else if (! arg2 || ! CLASS_TYPE_P (TREE_TYPE (arg2)))
5711 struct z_candidate **candp, **next;
5713 for (candp = &candidates; *candp; candp = next)
5715 tree parmlist, parmtype;
5716 int i, nargs = (arg2 ? 2 : 1);
5718 cand = *candp;
5719 next = &cand->next;
5721 parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5723 for (i = 0; i < nargs; ++i)
5725 parmtype = TREE_VALUE (parmlist);
5727 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
5728 parmtype = TREE_TYPE (parmtype);
5729 if (TREE_CODE (TREE_TYPE (args[i])) == ENUMERAL_TYPE
5730 && (same_type_ignoring_top_level_qualifiers_p
5731 (TREE_TYPE (args[i]), parmtype)))
5732 break;
5734 parmlist = TREE_CHAIN (parmlist);
5737 /* No argument has an appropriate type, so remove this
5738 candidate function from the list. */
5739 if (i == nargs)
5741 *candp = cand->next;
5742 next = candp;
5747 add_builtin_candidates (&candidates, code, code2, fnname, args,
5748 flags, complain);
5750 switch (code)
5752 case COMPOUND_EXPR:
5753 case ADDR_EXPR:
5754 /* For these, the built-in candidates set is empty
5755 [over.match.oper]/3. We don't want non-strict matches
5756 because exact matches are always possible with built-in
5757 operators. The built-in candidate set for COMPONENT_REF
5758 would be empty too, but since there are no such built-in
5759 operators, we accept non-strict matches for them. */
5760 strict_p = true;
5761 break;
5763 default:
5764 strict_p = false;
5765 break;
5768 candidates = splice_viable (candidates, strict_p, &any_viable_p);
5769 if (!any_viable_p)
5771 switch (code)
5773 case POSTINCREMENT_EXPR:
5774 case POSTDECREMENT_EXPR:
5775 /* Don't try anything fancy if we're not allowed to produce
5776 errors. */
5777 if (!(complain & tf_error))
5778 return error_mark_node;
5780 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
5781 distinguish between prefix and postfix ++ and
5782 operator++() was used for both, so we allow this with
5783 -fpermissive. */
5784 else
5786 const char *msg = (flag_permissive)
5787 ? G_("no %<%D(int)%> declared for postfix %qs,"
5788 " trying prefix operator instead")
5789 : G_("no %<%D(int)%> declared for postfix %qs");
5790 permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
5793 if (!flag_permissive)
5794 return error_mark_node;
5796 if (code == POSTINCREMENT_EXPR)
5797 code = PREINCREMENT_EXPR;
5798 else
5799 code = PREDECREMENT_EXPR;
5800 result = build_new_op_1 (loc, code, flags, arg1, NULL_TREE,
5801 NULL_TREE, overload, complain);
5802 break;
5804 /* The caller will deal with these. */
5805 case ADDR_EXPR:
5806 case COMPOUND_EXPR:
5807 case COMPONENT_REF:
5808 result = NULL_TREE;
5809 result_valid_p = true;
5810 break;
5812 default:
5813 if (complain & tf_error)
5815 /* If one of the arguments of the operator represents
5816 an invalid use of member function pointer, try to report
5817 a meaningful error ... */
5818 if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
5819 || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
5820 || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
5821 /* We displayed the error message. */;
5822 else
5824 /* ... Otherwise, report the more generic
5825 "no matching operator found" error */
5826 op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
5827 print_z_candidates (loc, candidates);
5830 result = error_mark_node;
5831 break;
5834 else
5836 cand = tourney (candidates, complain);
5837 if (cand == 0)
5839 if (complain & tf_error)
5841 op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
5842 print_z_candidates (loc, candidates);
5844 result = error_mark_node;
5846 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5848 if (overload)
5849 *overload = cand->fn;
5851 if (resolve_args (arglist, complain) == NULL)
5852 result = error_mark_node;
5853 else
5854 result = build_over_call (cand, LOOKUP_NORMAL, complain);
5856 if (trivial_fn_p (cand->fn))
5857 /* There won't be a CALL_EXPR. */;
5858 else if (result && result != error_mark_node)
5860 tree call = extract_call_expr (result);
5861 CALL_EXPR_OPERATOR_SYNTAX (call) = true;
5863 if (processing_template_decl && DECL_HIDDEN_FRIEND_P (cand->fn))
5864 /* This prevents build_new_function_call from discarding this
5865 function during instantiation of the enclosing template. */
5866 KOENIG_LOOKUP_P (call) = 1;
5868 /* Specify evaluation order as per P0145R2. */
5869 CALL_EXPR_ORDERED_ARGS (call) = false;
5870 switch (op_is_ordered (code))
5872 case -1:
5873 CALL_EXPR_REVERSE_ARGS (call) = true;
5874 break;
5876 case 1:
5877 CALL_EXPR_ORDERED_ARGS (call) = true;
5878 break;
5880 default:
5881 break;
5885 else
5887 /* Give any warnings we noticed during overload resolution. */
5888 if (cand->warnings && (complain & tf_warning))
5890 struct candidate_warning *w;
5891 for (w = cand->warnings; w; w = w->next)
5892 joust (cand, w->loser, 1, complain);
5895 /* Check for comparison of different enum types. */
5896 switch (code)
5898 case GT_EXPR:
5899 case LT_EXPR:
5900 case GE_EXPR:
5901 case LE_EXPR:
5902 case EQ_EXPR:
5903 case NE_EXPR:
5904 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
5905 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
5906 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5907 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
5908 && (complain & tf_warning))
5910 warning (OPT_Wenum_compare,
5911 "comparison between %q#T and %q#T",
5912 TREE_TYPE (arg1), TREE_TYPE (arg2));
5914 break;
5915 default:
5916 break;
5919 /* We need to strip any leading REF_BIND so that bitfields
5920 don't cause errors. This should not remove any important
5921 conversions, because builtins don't apply to class
5922 objects directly. */
5923 conv = cand->convs[0];
5924 if (conv->kind == ck_ref_bind)
5925 conv = next_conversion (conv);
5926 arg1 = convert_like (conv, arg1, complain);
5928 if (arg2)
5930 conv = cand->convs[1];
5931 if (conv->kind == ck_ref_bind)
5932 conv = next_conversion (conv);
5933 else
5934 arg2 = decay_conversion (arg2, complain);
5936 /* We need to call warn_logical_operator before
5937 converting arg2 to a boolean_type, but after
5938 decaying an enumerator to its value. */
5939 if (complain & tf_warning)
5940 warn_logical_operator (loc, code, boolean_type_node,
5941 code_orig_arg1, arg1,
5942 code_orig_arg2, arg2);
5944 arg2 = convert_like (conv, arg2, complain);
5946 if (arg3)
5948 conv = cand->convs[2];
5949 if (conv->kind == ck_ref_bind)
5950 conv = next_conversion (conv);
5951 arg3 = convert_like (conv, arg3, complain);
5957 user_defined_result_ready:
5959 /* Free all the conversions we allocated. */
5960 obstack_free (&conversion_obstack, p);
5962 if (result || result_valid_p)
5963 return result;
5965 builtin:
5966 switch (code)
5968 case MODIFY_EXPR:
5969 return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
5971 case INDIRECT_REF:
5972 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
5974 case TRUTH_ANDIF_EXPR:
5975 case TRUTH_ORIF_EXPR:
5976 case TRUTH_AND_EXPR:
5977 case TRUTH_OR_EXPR:
5978 if (complain & tf_warning)
5979 warn_logical_operator (loc, code, boolean_type_node,
5980 code_orig_arg1, arg1,
5981 code_orig_arg2, arg2);
5982 /* Fall through. */
5983 case GT_EXPR:
5984 case LT_EXPR:
5985 case GE_EXPR:
5986 case LE_EXPR:
5987 case EQ_EXPR:
5988 case NE_EXPR:
5989 if ((complain & tf_warning)
5990 && ((code_orig_arg1 == BOOLEAN_TYPE)
5991 ^ (code_orig_arg2 == BOOLEAN_TYPE)))
5992 maybe_warn_bool_compare (loc, code, arg1, arg2);
5993 if (complain & tf_warning && warn_tautological_compare)
5994 warn_tautological_cmp (loc, code, arg1, arg2);
5995 /* Fall through. */
5996 case PLUS_EXPR:
5997 case MINUS_EXPR:
5998 case MULT_EXPR:
5999 case TRUNC_DIV_EXPR:
6000 case MAX_EXPR:
6001 case MIN_EXPR:
6002 case LSHIFT_EXPR:
6003 case RSHIFT_EXPR:
6004 case TRUNC_MOD_EXPR:
6005 case BIT_AND_EXPR:
6006 case BIT_IOR_EXPR:
6007 case BIT_XOR_EXPR:
6008 return cp_build_binary_op (loc, code, arg1, arg2, complain);
6010 case UNARY_PLUS_EXPR:
6011 case NEGATE_EXPR:
6012 case BIT_NOT_EXPR:
6013 case TRUTH_NOT_EXPR:
6014 case PREINCREMENT_EXPR:
6015 case POSTINCREMENT_EXPR:
6016 case PREDECREMENT_EXPR:
6017 case POSTDECREMENT_EXPR:
6018 case REALPART_EXPR:
6019 case IMAGPART_EXPR:
6020 case ABS_EXPR:
6021 return cp_build_unary_op (code, arg1, candidates != 0, complain);
6023 case ARRAY_REF:
6024 return cp_build_array_ref (input_location, arg1, arg2, complain);
6026 case MEMBER_REF:
6027 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_ARROW_STAR,
6028 complain),
6029 arg2, complain);
6031 /* The caller will deal with these. */
6032 case ADDR_EXPR:
6033 case COMPONENT_REF:
6034 case COMPOUND_EXPR:
6035 return NULL_TREE;
6037 default:
6038 gcc_unreachable ();
6040 return NULL_TREE;
6043 /* Wrapper for above. */
6045 tree
6046 build_new_op (location_t loc, enum tree_code code, int flags,
6047 tree arg1, tree arg2, tree arg3,
6048 tree *overload, tsubst_flags_t complain)
6050 tree ret;
6051 bool subtime = timevar_cond_start (TV_OVERLOAD);
6052 ret = build_new_op_1 (loc, code, flags, arg1, arg2, arg3,
6053 overload, complain);
6054 timevar_cond_stop (TV_OVERLOAD, subtime);
6055 return ret;
6058 /* CALL was returned by some call-building function; extract the actual
6059 CALL_EXPR from any bits that have been tacked on, e.g. by
6060 convert_from_reference. */
6062 tree
6063 extract_call_expr (tree call)
6065 while (TREE_CODE (call) == COMPOUND_EXPR)
6066 call = TREE_OPERAND (call, 1);
6067 if (REFERENCE_REF_P (call))
6068 call = TREE_OPERAND (call, 0);
6069 if (TREE_CODE (call) == TARGET_EXPR)
6070 call = TARGET_EXPR_INITIAL (call);
6071 gcc_assert (TREE_CODE (call) == CALL_EXPR
6072 || TREE_CODE (call) == AGGR_INIT_EXPR
6073 || call == error_mark_node);
6074 return call;
6077 /* Returns true if FN has two parameters, of which the second has type
6078 size_t. */
6080 static bool
6081 second_parm_is_size_t (tree fn)
6083 tree t = FUNCTION_ARG_CHAIN (fn);
6084 if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
6085 return false;
6086 t = TREE_CHAIN (t);
6087 if (t == void_list_node)
6088 return true;
6089 if (aligned_new_threshold && t
6090 && same_type_p (TREE_VALUE (t), align_type_node)
6091 && TREE_CHAIN (t) == void_list_node)
6092 return true;
6093 return false;
6096 /* True if T, an allocation function, has std::align_val_t as its second
6097 argument. */
6099 bool
6100 aligned_allocation_fn_p (tree t)
6102 if (!aligned_new_threshold)
6103 return false;
6105 tree a = FUNCTION_ARG_CHAIN (t);
6106 return (a && same_type_p (TREE_VALUE (a), align_type_node));
6109 /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
6110 function (3.7.4.2 [basic.stc.dynamic.deallocation]) with a parameter of
6111 std::align_val_t. */
6113 static bool
6114 aligned_deallocation_fn_p (tree t)
6116 if (!aligned_new_threshold)
6117 return false;
6119 /* A template instance is never a usual deallocation function,
6120 regardless of its signature. */
6121 if (TREE_CODE (t) == TEMPLATE_DECL
6122 || primary_template_instantiation_p (t))
6123 return false;
6125 tree a = FUNCTION_ARG_CHAIN (t);
6126 if (same_type_p (TREE_VALUE (a), align_type_node)
6127 && TREE_CHAIN (a) == void_list_node)
6128 return true;
6129 if (!same_type_p (TREE_VALUE (a), size_type_node))
6130 return false;
6131 a = TREE_CHAIN (a);
6132 if (a && same_type_p (TREE_VALUE (a), align_type_node)
6133 && TREE_CHAIN (a) == void_list_node)
6134 return true;
6135 return false;
6138 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
6139 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
6141 bool
6142 usual_deallocation_fn_p (tree t)
6144 /* A template instance is never a usual deallocation function,
6145 regardless of its signature. */
6146 if (TREE_CODE (t) == TEMPLATE_DECL
6147 || primary_template_instantiation_p (t))
6148 return false;
6150 /* If a class T has a member deallocation function named operator delete
6151 with exactly one parameter, then that function is a usual
6152 (non-placement) deallocation function. If class T does not declare
6153 such an operator delete but does declare a member deallocation
6154 function named operator delete with exactly two parameters, the second
6155 of which has type std::size_t (18.2), then this function is a usual
6156 deallocation function. */
6157 bool global = DECL_NAMESPACE_SCOPE_P (t);
6158 tree chain = FUNCTION_ARG_CHAIN (t);
6159 if (!chain)
6160 return false;
6161 if (chain == void_list_node
6162 || ((!global || flag_sized_deallocation)
6163 && second_parm_is_size_t (t)))
6164 return true;
6165 if (aligned_deallocation_fn_p (t))
6166 return true;
6167 return false;
6170 /* Build a call to operator delete. This has to be handled very specially,
6171 because the restrictions on what signatures match are different from all
6172 other call instances. For a normal delete, only a delete taking (void *)
6173 or (void *, size_t) is accepted. For a placement delete, only an exact
6174 match with the placement new is accepted.
6176 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
6177 ADDR is the pointer to be deleted.
6178 SIZE is the size of the memory block to be deleted.
6179 GLOBAL_P is true if the delete-expression should not consider
6180 class-specific delete operators.
6181 PLACEMENT is the corresponding placement new call, or NULL_TREE.
6183 If this call to "operator delete" is being generated as part to
6184 deallocate memory allocated via a new-expression (as per [expr.new]
6185 which requires that if the initialization throws an exception then
6186 we call a deallocation function), then ALLOC_FN is the allocation
6187 function. */
6189 tree
6190 build_op_delete_call (enum tree_code code, tree addr, tree size,
6191 bool global_p, tree placement,
6192 tree alloc_fn, tsubst_flags_t complain)
6194 tree fn = NULL_TREE;
6195 tree fns, fnname, type, t;
6197 if (addr == error_mark_node)
6198 return error_mark_node;
6200 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
6202 fnname = ovl_op_identifier (false, code);
6204 if (CLASS_TYPE_P (type)
6205 && COMPLETE_TYPE_P (complete_type (type))
6206 && !global_p)
6207 /* In [class.free]
6209 If the result of the lookup is ambiguous or inaccessible, or if
6210 the lookup selects a placement deallocation function, the
6211 program is ill-formed.
6213 Therefore, we ask lookup_fnfields to complain about ambiguity. */
6215 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
6216 if (fns == error_mark_node)
6217 return error_mark_node;
6219 else
6220 fns = NULL_TREE;
6222 if (fns == NULL_TREE)
6223 fns = lookup_name_nonclass (fnname);
6225 /* Strip const and volatile from addr. */
6226 addr = cp_convert (ptr_type_node, addr, complain);
6228 if (placement)
6230 /* "A declaration of a placement deallocation function matches the
6231 declaration of a placement allocation function if it has the same
6232 number of parameters and, after parameter transformations (8.3.5),
6233 all parameter types except the first are identical."
6235 So we build up the function type we want and ask instantiate_type
6236 to get it for us. */
6237 t = FUNCTION_ARG_CHAIN (alloc_fn);
6238 t = tree_cons (NULL_TREE, ptr_type_node, t);
6239 t = build_function_type (void_type_node, t);
6241 fn = instantiate_type (t, fns, tf_none);
6242 if (fn == error_mark_node)
6243 return NULL_TREE;
6245 fn = MAYBE_BASELINK_FUNCTIONS (fn);
6247 /* "If the lookup finds the two-parameter form of a usual deallocation
6248 function (3.7.4.2) and that function, considered as a placement
6249 deallocation function, would have been selected as a match for the
6250 allocation function, the program is ill-formed." */
6251 if (second_parm_is_size_t (fn))
6253 const char *const msg1
6254 = G_("exception cleanup for this placement new selects "
6255 "non-placement operator delete");
6256 const char *const msg2
6257 = G_("%qD is a usual (non-placement) deallocation "
6258 "function in C++14 (or with -fsized-deallocation)");
6260 /* But if the class has an operator delete (void *), then that is
6261 the usual deallocation function, so we shouldn't complain
6262 about using the operator delete (void *, size_t). */
6263 if (DECL_CLASS_SCOPE_P (fn))
6264 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns));
6265 iter; ++iter)
6267 tree elt = *iter;
6268 if (usual_deallocation_fn_p (elt)
6269 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
6270 goto ok;
6272 /* Before C++14 a two-parameter global deallocation function is
6273 always a placement deallocation function, but warn if
6274 -Wc++14-compat. */
6275 else if (!flag_sized_deallocation)
6277 if ((complain & tf_warning)
6278 && warning (OPT_Wc__14_compat, msg1))
6279 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6280 goto ok;
6283 if (complain & tf_warning_or_error)
6285 if (permerror (input_location, msg1))
6287 /* Only mention C++14 for namespace-scope delete. */
6288 if (DECL_NAMESPACE_SCOPE_P (fn))
6289 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6290 else
6291 inform (DECL_SOURCE_LOCATION (fn),
6292 "%qD is a usual (non-placement) deallocation "
6293 "function", fn);
6296 else
6297 return error_mark_node;
6298 ok:;
6301 else
6302 /* "Any non-placement deallocation function matches a non-placement
6303 allocation function. If the lookup finds a single matching
6304 deallocation function, that function will be called; otherwise, no
6305 deallocation function will be called." */
6306 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
6308 tree elt = *iter;
6309 if (usual_deallocation_fn_p (elt))
6311 if (!fn)
6313 fn = elt;
6314 continue;
6317 /* -- If the type has new-extended alignment, a function with a
6318 parameter of type std::align_val_t is preferred; otherwise a
6319 function without such a parameter is preferred. If exactly one
6320 preferred function is found, that function is selected and the
6321 selection process terminates. If more than one preferred
6322 function is found, all non-preferred functions are eliminated
6323 from further consideration. */
6324 if (aligned_new_threshold)
6326 bool want_align = type_has_new_extended_alignment (type);
6327 bool fn_align = aligned_deallocation_fn_p (fn);
6328 bool elt_align = aligned_deallocation_fn_p (elt);
6330 if (elt_align != fn_align)
6332 if (want_align == elt_align)
6333 fn = elt;
6334 continue;
6338 /* -- If the deallocation functions have class scope, the one
6339 without a parameter of type std::size_t is selected. */
6340 bool want_size;
6341 if (DECL_CLASS_SCOPE_P (fn))
6342 want_size = false;
6344 /* -- If the type is complete and if, for the second alternative
6345 (delete array) only, the operand is a pointer to a class type
6346 with a non-trivial destructor or a (possibly multi-dimensional)
6347 array thereof, the function with a parameter of type std::size_t
6348 is selected.
6350 -- Otherwise, it is unspecified whether a deallocation function
6351 with a parameter of type std::size_t is selected. */
6352 else
6354 want_size = COMPLETE_TYPE_P (type);
6355 if (code == VEC_DELETE_EXPR
6356 && !TYPE_VEC_NEW_USES_COOKIE (type))
6357 /* We need a cookie to determine the array size. */
6358 want_size = false;
6360 bool fn_size = second_parm_is_size_t (fn);
6361 bool elt_size = second_parm_is_size_t (elt);
6362 gcc_assert (fn_size != elt_size);
6363 if (want_size == elt_size)
6364 fn = elt;
6368 /* If we have a matching function, call it. */
6369 if (fn)
6371 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6373 /* If the FN is a member function, make sure that it is
6374 accessible. */
6375 if (BASELINK_P (fns))
6376 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
6377 complain);
6379 /* Core issue 901: It's ok to new a type with deleted delete. */
6380 if (DECL_DELETED_FN (fn) && alloc_fn)
6381 return NULL_TREE;
6383 if (placement)
6385 /* The placement args might not be suitable for overload
6386 resolution at this point, so build the call directly. */
6387 int nargs = call_expr_nargs (placement);
6388 tree *argarray = XALLOCAVEC (tree, nargs);
6389 int i;
6390 argarray[0] = addr;
6391 for (i = 1; i < nargs; i++)
6392 argarray[i] = CALL_EXPR_ARG (placement, i);
6393 if (!mark_used (fn, complain) && !(complain & tf_error))
6394 return error_mark_node;
6395 return build_cxx_call (fn, nargs, argarray, complain);
6397 else
6399 tree ret;
6400 vec<tree, va_gc> *args = make_tree_vector ();
6401 args->quick_push (addr);
6402 if (second_parm_is_size_t (fn))
6403 args->quick_push (size);
6404 if (aligned_deallocation_fn_p (fn))
6406 tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
6407 args->quick_push (al);
6409 ret = cp_build_function_call_vec (fn, &args, complain);
6410 release_tree_vector (args);
6411 return ret;
6415 /* [expr.new]
6417 If no unambiguous matching deallocation function can be found,
6418 propagating the exception does not cause the object's memory to
6419 be freed. */
6420 if (alloc_fn)
6422 if ((complain & tf_warning)
6423 && !placement)
6424 warning (0, "no corresponding deallocation function for %qD",
6425 alloc_fn);
6426 return NULL_TREE;
6429 if (complain & tf_error)
6430 error ("no suitable %<operator %s%> for %qT",
6431 OVL_OP_INFO (false, code)->name, type);
6432 return error_mark_node;
6435 /* If the current scope isn't allowed to access DECL along
6436 BASETYPE_PATH, give an error. The most derived class in
6437 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
6438 the declaration to use in the error diagnostic. */
6440 bool
6441 enforce_access (tree basetype_path, tree decl, tree diag_decl,
6442 tsubst_flags_t complain, access_failure_info *afi)
6444 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
6446 if (flag_new_inheriting_ctors
6447 && DECL_INHERITED_CTOR (decl))
6449 /* 7.3.3/18: The additional constructors are accessible if they would be
6450 accessible when used to construct an object of the corresponding base
6451 class. */
6452 decl = strip_inheriting_ctors (decl);
6453 basetype_path = lookup_base (basetype_path, DECL_CONTEXT (decl),
6454 ba_any, NULL, complain);
6457 if (!accessible_p (basetype_path, decl, true))
6459 if (complain & tf_error)
6461 if (flag_new_inheriting_ctors)
6462 diag_decl = strip_inheriting_ctors (diag_decl);
6463 if (TREE_PRIVATE (decl))
6465 error ("%q#D is private within this context", diag_decl);
6466 inform (DECL_SOURCE_LOCATION (diag_decl),
6467 "declared private here");
6468 if (afi)
6469 afi->record_access_failure (basetype_path, diag_decl);
6471 else if (TREE_PROTECTED (decl))
6473 error ("%q#D is protected within this context", diag_decl);
6474 inform (DECL_SOURCE_LOCATION (diag_decl),
6475 "declared protected here");
6476 if (afi)
6477 afi->record_access_failure (basetype_path, diag_decl);
6479 else
6481 error ("%q#D is inaccessible within this context", diag_decl);
6482 inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
6483 if (afi)
6484 afi->record_access_failure (basetype_path, diag_decl);
6487 return false;
6490 return true;
6493 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
6494 bitwise or of LOOKUP_* values. If any errors are warnings are
6495 generated, set *DIAGNOSTIC_FN to "error" or "warning",
6496 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
6497 to NULL. */
6499 static tree
6500 build_temp (tree expr, tree type, int flags,
6501 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
6503 int savew, savee;
6504 vec<tree, va_gc> *args;
6506 *diagnostic_kind = DK_UNSPECIFIED;
6508 /* If the source is a packed field, calling the copy constructor will require
6509 binding the field to the reference parameter to the copy constructor, and
6510 we'll end up with an infinite loop. If we can use a bitwise copy, then
6511 do that now. */
6512 if ((lvalue_kind (expr) & clk_packed)
6513 && CLASS_TYPE_P (TREE_TYPE (expr))
6514 && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
6515 return get_target_expr_sfinae (expr, complain);
6517 savew = warningcount + werrorcount, savee = errorcount;
6518 args = make_tree_vector_single (expr);
6519 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6520 &args, type, flags, complain);
6521 release_tree_vector (args);
6522 if (warningcount + werrorcount > savew)
6523 *diagnostic_kind = DK_WARNING;
6524 else if (errorcount > savee)
6525 *diagnostic_kind = DK_ERROR;
6526 return expr;
6529 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
6530 EXPR is implicitly converted to type TOTYPE.
6531 FN and ARGNUM are used for diagnostics. */
6533 static void
6534 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
6536 /* Issue warnings about peculiar, but valid, uses of NULL. */
6537 if (expr == null_node && TREE_CODE (totype) != BOOLEAN_TYPE
6538 && ARITHMETIC_TYPE_P (totype))
6540 source_location loc =
6541 expansion_point_location_if_in_system_header (input_location);
6543 if (fn)
6544 warning_at (loc, OPT_Wconversion_null,
6545 "passing NULL to non-pointer argument %P of %qD",
6546 argnum, fn);
6547 else
6548 warning_at (loc, OPT_Wconversion_null,
6549 "converting to non-pointer type %qT from NULL", totype);
6552 /* Issue warnings if "false" is converted to a NULL pointer */
6553 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
6554 && TYPE_PTR_P (totype))
6556 if (fn)
6557 warning_at (input_location, OPT_Wconversion_null,
6558 "converting %<false%> to pointer type for argument %P "
6559 "of %qD", argnum, fn);
6560 else
6561 warning_at (input_location, OPT_Wconversion_null,
6562 "converting %<false%> to pointer type %qT", totype);
6566 /* We gave a diagnostic during a conversion. If this was in the second
6567 standard conversion sequence of a user-defined conversion sequence, say
6568 which user-defined conversion. */
6570 static void
6571 maybe_print_user_conv_context (conversion *convs)
6573 if (convs->user_conv_p)
6574 for (conversion *t = convs; t; t = next_conversion (t))
6575 if (t->kind == ck_user)
6577 print_z_candidate (0, " after user-defined conversion:",
6578 t->cand);
6579 break;
6583 /* Locate the parameter with the given index within FNDECL.
6584 ARGNUM is zero based, -1 indicates the `this' argument of a method.
6585 Return the location of the FNDECL itself if there are problems. */
6587 static location_t
6588 get_fndecl_argument_location (tree fndecl, int argnum)
6590 int i;
6591 tree param;
6593 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6594 for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
6595 i < argnum && param;
6596 i++, param = TREE_CHAIN (param))
6599 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6600 return the location of FNDECL. */
6601 if (param == NULL)
6602 return DECL_SOURCE_LOCATION (fndecl);
6604 return DECL_SOURCE_LOCATION (param);
6607 /* Perform the conversions in CONVS on the expression EXPR. FN and
6608 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
6609 indicates the `this' argument of a method. INNER is nonzero when
6610 being called to continue a conversion chain. It is negative when a
6611 reference binding will be applied, positive otherwise. If
6612 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
6613 conversions will be emitted if appropriate. If C_CAST_P is true,
6614 this conversion is coming from a C-style cast; in that case,
6615 conversions to inaccessible bases are permitted. */
6617 static tree
6618 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
6619 bool issue_conversion_warnings,
6620 bool c_cast_p, tsubst_flags_t complain)
6622 tree totype = convs->type;
6623 diagnostic_t diag_kind;
6624 int flags;
6625 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6627 if (convs->bad_p && !(complain & tf_error))
6628 return error_mark_node;
6630 if (convs->bad_p
6631 && convs->kind != ck_user
6632 && convs->kind != ck_list
6633 && convs->kind != ck_ambig
6634 && (convs->kind != ck_ref_bind
6635 || (convs->user_conv_p && next_conversion (convs)->bad_p))
6636 && (convs->kind != ck_rvalue
6637 || SCALAR_TYPE_P (totype))
6638 && convs->kind != ck_base)
6640 bool complained = false;
6641 conversion *t = convs;
6643 /* Give a helpful error if this is bad because of excess braces. */
6644 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6645 && SCALAR_TYPE_P (totype)
6646 && CONSTRUCTOR_NELTS (expr) > 0
6647 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
6649 complained = permerror (loc, "too many braces around initializer "
6650 "for %qT", totype);
6651 while (BRACE_ENCLOSED_INITIALIZER_P (expr)
6652 && CONSTRUCTOR_NELTS (expr) == 1)
6653 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6656 /* Give a helpful error if this is bad because a conversion to bool
6657 from std::nullptr_t requires direct-initialization. */
6658 if (NULLPTR_TYPE_P (TREE_TYPE (expr))
6659 && TREE_CODE (totype) == BOOLEAN_TYPE)
6660 complained = permerror (loc, "converting to %qH from %qI requires "
6661 "direct-initialization",
6662 totype, TREE_TYPE (expr));
6664 for (; t ; t = next_conversion (t))
6666 if (t->kind == ck_user && t->cand->reason)
6668 complained = permerror (loc, "invalid user-defined conversion "
6669 "from %qH to %qI", TREE_TYPE (expr),
6670 totype);
6671 if (complained)
6672 print_z_candidate (loc, "candidate is:", t->cand);
6673 expr = convert_like_real (t, expr, fn, argnum,
6674 /*issue_conversion_warnings=*/false,
6675 /*c_cast_p=*/false,
6676 complain);
6677 if (convs->kind == ck_ref_bind)
6678 expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
6679 LOOKUP_NORMAL, NULL_TREE,
6680 complain);
6681 else
6682 expr = cp_convert (totype, expr, complain);
6683 if (complained && fn)
6684 inform (DECL_SOURCE_LOCATION (fn),
6685 " initializing argument %P of %qD", argnum, fn);
6686 return expr;
6688 else if (t->kind == ck_user || !t->bad_p)
6690 expr = convert_like_real (t, expr, fn, argnum,
6691 /*issue_conversion_warnings=*/false,
6692 /*c_cast_p=*/false,
6693 complain);
6694 break;
6696 else if (t->kind == ck_ambig)
6697 return convert_like_real (t, expr, fn, argnum,
6698 /*issue_conversion_warnings=*/false,
6699 /*c_cast_p=*/false,
6700 complain);
6701 else if (t->kind == ck_identity)
6702 break;
6704 if (!complained)
6705 complained = permerror (loc, "invalid conversion from %qH to %qI",
6706 TREE_TYPE (expr), totype);
6707 if (complained && fn)
6708 inform (get_fndecl_argument_location (fn, argnum),
6709 " initializing argument %P of %qD", argnum, fn);
6711 return cp_convert (totype, expr, complain);
6714 if (issue_conversion_warnings && (complain & tf_warning))
6715 conversion_null_warnings (totype, expr, fn, argnum);
6717 switch (convs->kind)
6719 case ck_user:
6721 struct z_candidate *cand = convs->cand;
6723 if (cand == NULL)
6724 /* We chose the surrogate function from add_conv_candidate, now we
6725 actually need to build the conversion. */
6726 cand = build_user_type_conversion_1 (totype, expr,
6727 LOOKUP_NO_CONVERSION, complain);
6729 tree convfn = cand->fn;
6731 /* When converting from an init list we consider explicit
6732 constructors, but actually trying to call one is an error. */
6733 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
6734 && BRACE_ENCLOSED_INITIALIZER_P (expr)
6735 /* Unless this is for direct-list-initialization. */
6736 && !CONSTRUCTOR_IS_DIRECT_INIT (expr)
6737 /* And in C++98 a default constructor can't be explicit. */
6738 && cxx_dialect >= cxx11)
6740 if (!(complain & tf_error))
6741 return error_mark_node;
6742 location_t loc = location_of (expr);
6743 if (CONSTRUCTOR_NELTS (expr) == 0
6744 && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
6746 if (pedwarn (loc, 0, "converting to %qT from initializer list "
6747 "would use explicit constructor %qD",
6748 totype, convfn))
6749 inform (loc, "in C++11 and above a default constructor "
6750 "can be explicit");
6752 else
6753 error ("converting to %qT from initializer list would use "
6754 "explicit constructor %qD", totype, convfn);
6757 /* If we're initializing from {}, it's value-initialization. */
6758 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6759 && CONSTRUCTOR_NELTS (expr) == 0
6760 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
6762 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
6763 expr = build_value_init (totype, complain);
6764 expr = get_target_expr_sfinae (expr, complain);
6765 if (expr != error_mark_node)
6767 TARGET_EXPR_LIST_INIT_P (expr) = true;
6768 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
6770 return expr;
6773 expr = mark_rvalue_use (expr);
6775 /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
6776 any more UDCs. */
6777 expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
6778 complain);
6780 /* If this is a constructor or a function returning an aggr type,
6781 we need to build up a TARGET_EXPR. */
6782 if (DECL_CONSTRUCTOR_P (convfn))
6784 expr = build_cplus_new (totype, expr, complain);
6786 /* Remember that this was list-initialization. */
6787 if (convs->check_narrowing && expr != error_mark_node)
6788 TARGET_EXPR_LIST_INIT_P (expr) = true;
6791 return expr;
6793 case ck_identity:
6794 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
6796 int nelts = CONSTRUCTOR_NELTS (expr);
6797 if (nelts == 0)
6798 expr = build_value_init (totype, complain);
6799 else if (nelts == 1)
6800 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6801 else
6802 gcc_unreachable ();
6804 expr = mark_rvalue_use (expr);
6806 if (type_unknown_p (expr))
6807 expr = instantiate_type (totype, expr, complain);
6808 return expr;
6809 case ck_ambig:
6810 /* We leave bad_p off ck_ambig because overload resolution considers
6811 it valid, it just fails when we try to perform it. So we need to
6812 check complain here, too. */
6813 if (complain & tf_error)
6815 /* Call build_user_type_conversion again for the error. */
6816 build_user_type_conversion (totype, convs->u.expr, LOOKUP_IMPLICIT,
6817 complain);
6818 if (fn)
6819 inform (DECL_SOURCE_LOCATION (fn),
6820 " initializing argument %P of %qD", argnum, fn);
6822 return error_mark_node;
6824 case ck_list:
6826 /* Conversion to std::initializer_list<T>. */
6827 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
6828 tree new_ctor = build_constructor (init_list_type_node, NULL);
6829 unsigned len = CONSTRUCTOR_NELTS (expr);
6830 tree array, val, field;
6831 vec<constructor_elt, va_gc> *vec = NULL;
6832 unsigned ix;
6834 /* Convert all the elements. */
6835 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
6837 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
6838 false, false, complain);
6839 if (sub == error_mark_node)
6840 return sub;
6841 if (!BRACE_ENCLOSED_INITIALIZER_P (val)
6842 && !check_narrowing (TREE_TYPE (sub), val, complain))
6843 return error_mark_node;
6844 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
6845 if (!TREE_CONSTANT (sub))
6846 TREE_CONSTANT (new_ctor) = false;
6848 /* Build up the array. */
6849 elttype = cp_build_qualified_type
6850 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
6851 array = build_array_of_n_type (elttype, len);
6852 array = finish_compound_literal (array, new_ctor, complain);
6853 /* Take the address explicitly rather than via decay_conversion
6854 to avoid the error about taking the address of a temporary. */
6855 array = cp_build_addr_expr (array, complain);
6856 array = cp_convert (build_pointer_type (elttype), array, complain);
6857 if (array == error_mark_node)
6858 return error_mark_node;
6860 /* Build up the initializer_list object. */
6861 totype = complete_type (totype);
6862 field = next_initializable_field (TYPE_FIELDS (totype));
6863 CONSTRUCTOR_APPEND_ELT (vec, field, array);
6864 field = next_initializable_field (DECL_CHAIN (field));
6865 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
6866 new_ctor = build_constructor (totype, vec);
6867 return get_target_expr_sfinae (new_ctor, complain);
6870 case ck_aggr:
6871 if (TREE_CODE (totype) == COMPLEX_TYPE)
6873 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
6874 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
6875 real = perform_implicit_conversion (TREE_TYPE (totype),
6876 real, complain);
6877 imag = perform_implicit_conversion (TREE_TYPE (totype),
6878 imag, complain);
6879 expr = build2 (COMPLEX_EXPR, totype, real, imag);
6880 return expr;
6882 expr = reshape_init (totype, expr, complain);
6883 expr = get_target_expr_sfinae (digest_init (totype, expr, complain),
6884 complain);
6885 if (expr != error_mark_node)
6886 TARGET_EXPR_LIST_INIT_P (expr) = true;
6887 return expr;
6889 default:
6890 break;
6893 expr = convert_like_real (next_conversion (convs), expr, fn, argnum,
6894 convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
6895 c_cast_p,
6896 complain);
6897 if (expr == error_mark_node)
6898 return error_mark_node;
6900 switch (convs->kind)
6902 case ck_rvalue:
6903 expr = decay_conversion (expr, complain);
6904 if (expr == error_mark_node)
6906 if (complain & tf_error)
6908 maybe_print_user_conv_context (convs);
6909 if (fn)
6910 inform (DECL_SOURCE_LOCATION (fn),
6911 " initializing argument %P of %qD", argnum, fn);
6913 return error_mark_node;
6916 if (! MAYBE_CLASS_TYPE_P (totype))
6917 return expr;
6919 /* Don't introduce copies when passing arguments along to the inherited
6920 constructor. */
6921 if (current_function_decl
6922 && flag_new_inheriting_ctors
6923 && DECL_INHERITED_CTOR (current_function_decl))
6924 return expr;
6926 /* Fall through. */
6927 case ck_base:
6928 if (convs->kind == ck_base && !convs->need_temporary_p)
6930 /* We are going to bind a reference directly to a base-class
6931 subobject of EXPR. */
6932 /* Build an expression for `*((base*) &expr)'. */
6933 expr = convert_to_base (expr, totype,
6934 !c_cast_p, /*nonnull=*/true, complain);
6935 return expr;
6938 /* Copy-initialization where the cv-unqualified version of the source
6939 type is the same class as, or a derived class of, the class of the
6940 destination [is treated as direct-initialization]. [dcl.init] */
6941 flags = LOOKUP_NORMAL;
6942 if (convs->user_conv_p)
6943 /* This conversion is being done in the context of a user-defined
6944 conversion (i.e. the second step of copy-initialization), so
6945 don't allow any more. */
6946 flags |= LOOKUP_NO_CONVERSION;
6947 else
6948 flags |= LOOKUP_ONLYCONVERTING;
6949 if (convs->rvaluedness_matches_p)
6950 /* standard_conversion got LOOKUP_PREFER_RVALUE. */
6951 flags |= LOOKUP_PREFER_RVALUE;
6952 if (TREE_CODE (expr) == TARGET_EXPR
6953 && TARGET_EXPR_LIST_INIT_P (expr))
6954 /* Copy-list-initialization doesn't actually involve a copy. */
6955 return expr;
6956 expr = build_temp (expr, totype, flags, &diag_kind, complain);
6957 if (diag_kind && complain)
6959 maybe_print_user_conv_context (convs);
6960 if (fn)
6961 inform (DECL_SOURCE_LOCATION (fn),
6962 " initializing argument %P of %qD", argnum, fn);
6965 return build_cplus_new (totype, expr, complain);
6967 case ck_ref_bind:
6969 tree ref_type = totype;
6971 if (convs->bad_p && !next_conversion (convs)->bad_p)
6973 tree extype = TREE_TYPE (expr);
6974 if (TYPE_REF_IS_RVALUE (ref_type)
6975 && lvalue_p (expr))
6976 error_at (loc, "cannot bind rvalue reference of type %qH to "
6977 "lvalue of type %qI", totype, extype);
6978 else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
6979 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
6980 error_at (loc, "cannot bind non-const lvalue reference of "
6981 "type %qH to an rvalue of type %qI", totype, extype);
6982 else if (!reference_compatible_p (TREE_TYPE (totype), extype))
6983 error_at (loc, "binding reference of type %qH to %qI "
6984 "discards qualifiers", totype, extype);
6985 else
6986 gcc_unreachable ();
6987 maybe_print_user_conv_context (convs);
6988 if (fn)
6989 inform (DECL_SOURCE_LOCATION (fn),
6990 " initializing argument %P of %qD", argnum, fn);
6991 return error_mark_node;
6994 /* If necessary, create a temporary.
6996 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
6997 that need temporaries, even when their types are reference
6998 compatible with the type of reference being bound, so the
6999 upcoming call to cp_build_addr_expr doesn't fail. */
7000 if (convs->need_temporary_p
7001 || TREE_CODE (expr) == CONSTRUCTOR
7002 || TREE_CODE (expr) == VA_ARG_EXPR)
7004 /* Otherwise, a temporary of type "cv1 T1" is created and
7005 initialized from the initializer expression using the rules
7006 for a non-reference copy-initialization (8.5). */
7008 tree type = TREE_TYPE (ref_type);
7009 cp_lvalue_kind lvalue = lvalue_kind (expr);
7011 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7012 (type, next_conversion (convs)->type));
7013 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
7014 && !TYPE_REF_IS_RVALUE (ref_type))
7016 /* If the reference is volatile or non-const, we
7017 cannot create a temporary. */
7018 if (lvalue & clk_bitfield)
7019 error_at (loc, "cannot bind bitfield %qE to %qT",
7020 expr, ref_type);
7021 else if (lvalue & clk_packed)
7022 error_at (loc, "cannot bind packed field %qE to %qT",
7023 expr, ref_type);
7024 else
7025 error_at (loc, "cannot bind rvalue %qE to %qT",
7026 expr, ref_type);
7027 return error_mark_node;
7029 /* If the source is a packed field, and we must use a copy
7030 constructor, then building the target expr will require
7031 binding the field to the reference parameter to the
7032 copy constructor, and we'll end up with an infinite
7033 loop. If we can use a bitwise copy, then we'll be
7034 OK. */
7035 if ((lvalue & clk_packed)
7036 && CLASS_TYPE_P (type)
7037 && type_has_nontrivial_copy_init (type))
7039 error_at (loc, "cannot bind packed field %qE to %qT",
7040 expr, ref_type);
7041 return error_mark_node;
7043 if (lvalue & clk_bitfield)
7045 expr = convert_bitfield_to_declared_type (expr);
7046 expr = fold_convert (type, expr);
7048 expr = build_target_expr_with_type (expr, type, complain);
7051 /* Take the address of the thing to which we will bind the
7052 reference. */
7053 expr = cp_build_addr_expr (expr, complain);
7054 if (expr == error_mark_node)
7055 return error_mark_node;
7057 /* Convert it to a pointer to the type referred to by the
7058 reference. This will adjust the pointer if a derived to
7059 base conversion is being performed. */
7060 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
7061 expr, complain);
7062 /* Convert the pointer to the desired reference type. */
7063 return build_nop (ref_type, expr);
7066 case ck_lvalue:
7067 return decay_conversion (expr, complain);
7069 case ck_fnptr:
7070 /* ??? Should the address of a transaction-safe pointer point to the TM
7071 clone, and this conversion look up the primary function? */
7072 return build_nop (totype, expr);
7074 case ck_qual:
7075 /* Warn about deprecated conversion if appropriate. */
7076 string_conv_p (totype, expr, 1);
7077 break;
7079 case ck_ptr:
7080 if (convs->base_p)
7081 expr = convert_to_base (expr, totype, !c_cast_p,
7082 /*nonnull=*/false, complain);
7083 return build_nop (totype, expr);
7085 case ck_pmem:
7086 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
7087 c_cast_p, complain);
7089 default:
7090 break;
7093 if (convs->check_narrowing
7094 && !check_narrowing (totype, expr, complain))
7095 return error_mark_node;
7097 if (issue_conversion_warnings)
7098 expr = cp_convert_and_check (totype, expr, complain);
7099 else
7100 expr = cp_convert (totype, expr, complain);
7102 return expr;
7105 /* ARG is being passed to a varargs function. Perform any conversions
7106 required. Return the converted value. */
7108 tree
7109 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
7111 tree arg_type;
7112 location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
7114 /* [expr.call]
7116 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7117 standard conversions are performed. */
7118 arg = decay_conversion (arg, complain);
7119 arg_type = TREE_TYPE (arg);
7120 /* [expr.call]
7122 If the argument has integral or enumeration type that is subject
7123 to the integral promotions (_conv.prom_), or a floating point
7124 type that is subject to the floating point promotion
7125 (_conv.fpprom_), the value of the argument is converted to the
7126 promoted type before the call. */
7127 if (TREE_CODE (arg_type) == REAL_TYPE
7128 && (TYPE_PRECISION (arg_type)
7129 < TYPE_PRECISION (double_type_node))
7130 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
7132 if ((complain & tf_warning)
7133 && warn_double_promotion && !c_inhibit_evaluation_warnings)
7134 warning_at (loc, OPT_Wdouble_promotion,
7135 "implicit conversion from %qH to %qI when passing "
7136 "argument to function",
7137 arg_type, double_type_node);
7138 arg = convert_to_real_nofold (double_type_node, arg);
7140 else if (NULLPTR_TYPE_P (arg_type))
7141 arg = null_pointer_node;
7142 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
7144 if (SCOPED_ENUM_P (arg_type))
7146 tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
7147 complain);
7148 prom = cp_perform_integral_promotions (prom, complain);
7149 if (abi_version_crosses (6)
7150 && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
7151 && (complain & tf_warning))
7152 warning_at (loc, OPT_Wabi, "scoped enum %qT passed through ... as "
7153 "%qT before -fabi-version=6, %qT after", arg_type,
7154 TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
7155 if (!abi_version_at_least (6))
7156 arg = prom;
7158 else
7159 arg = cp_perform_integral_promotions (arg, complain);
7162 arg = require_complete_type_sfinae (arg, complain);
7163 arg_type = TREE_TYPE (arg);
7165 if (arg != error_mark_node
7166 /* In a template (or ill-formed code), we can have an incomplete type
7167 even after require_complete_type_sfinae, in which case we don't know
7168 whether it has trivial copy or not. */
7169 && COMPLETE_TYPE_P (arg_type)
7170 && !cp_unevaluated_operand)
7172 /* [expr.call] 5.2.2/7:
7173 Passing a potentially-evaluated argument of class type (Clause 9)
7174 with a non-trivial copy constructor or a non-trivial destructor
7175 with no corresponding parameter is conditionally-supported, with
7176 implementation-defined semantics.
7178 We support it as pass-by-invisible-reference, just like a normal
7179 value parameter.
7181 If the call appears in the context of a sizeof expression,
7182 it is not potentially-evaluated. */
7183 if (type_has_nontrivial_copy_init (arg_type)
7184 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
7186 arg = force_rvalue (arg, complain);
7187 if (complain & tf_warning)
7188 warning (OPT_Wconditionally_supported,
7189 "passing objects of non-trivially-copyable "
7190 "type %q#T through %<...%> is conditionally supported",
7191 arg_type);
7192 return cp_build_addr_expr (arg, complain);
7194 /* Build up a real lvalue-to-rvalue conversion in case the
7195 copy constructor is trivial but not callable. */
7196 else if (CLASS_TYPE_P (arg_type))
7197 force_rvalue (arg, complain);
7201 return arg;
7204 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
7206 tree
7207 build_x_va_arg (source_location loc, tree expr, tree type)
7209 if (processing_template_decl)
7211 tree r = build_min (VA_ARG_EXPR, type, expr);
7212 SET_EXPR_LOCATION (r, loc);
7213 return r;
7216 type = complete_type_or_else (type, NULL_TREE);
7218 if (expr == error_mark_node || !type)
7219 return error_mark_node;
7221 expr = mark_lvalue_use (expr);
7223 if (TREE_CODE (type) == REFERENCE_TYPE)
7225 error ("cannot receive reference type %qT through %<...%>", type);
7226 return error_mark_node;
7229 if (type_has_nontrivial_copy_init (type)
7230 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7232 /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
7233 it as pass by invisible reference. */
7234 warning_at (loc, OPT_Wconditionally_supported,
7235 "receiving objects of non-trivially-copyable type %q#T "
7236 "through %<...%> is conditionally-supported", type);
7238 tree ref = cp_build_reference_type (type, false);
7239 expr = build_va_arg (loc, expr, ref);
7240 return convert_from_reference (expr);
7243 tree ret = build_va_arg (loc, expr, type);
7244 if (CLASS_TYPE_P (type))
7245 /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
7246 know how to handle it. */
7247 ret = get_target_expr (ret);
7248 return ret;
7251 /* TYPE has been given to va_arg. Apply the default conversions which
7252 would have happened when passed via ellipsis. Return the promoted
7253 type, or the passed type if there is no change. */
7255 tree
7256 cxx_type_promotes_to (tree type)
7258 tree promote;
7260 /* Perform the array-to-pointer and function-to-pointer
7261 conversions. */
7262 type = type_decays_to (type);
7264 promote = type_promotes_to (type);
7265 if (same_type_p (type, promote))
7266 promote = type;
7268 return promote;
7271 /* ARG is a default argument expression being passed to a parameter of
7272 the indicated TYPE, which is a parameter to FN. PARMNUM is the
7273 zero-based argument number. Do any required conversions. Return
7274 the converted value. */
7276 static GTY(()) vec<tree, va_gc> *default_arg_context;
7277 void
7278 push_defarg_context (tree fn)
7279 { vec_safe_push (default_arg_context, fn); }
7281 void
7282 pop_defarg_context (void)
7283 { default_arg_context->pop (); }
7285 tree
7286 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
7287 tsubst_flags_t complain)
7289 int i;
7290 tree t;
7292 /* See through clones. */
7293 fn = DECL_ORIGIN (fn);
7294 /* And inheriting ctors. */
7295 if (flag_new_inheriting_ctors)
7296 fn = strip_inheriting_ctors (fn);
7298 /* Detect recursion. */
7299 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
7300 if (t == fn)
7302 if (complain & tf_error)
7303 error ("recursive evaluation of default argument for %q#D", fn);
7304 return error_mark_node;
7307 /* If the ARG is an unparsed default argument expression, the
7308 conversion cannot be performed. */
7309 if (TREE_CODE (arg) == DEFAULT_ARG)
7311 if (complain & tf_error)
7312 error ("call to %qD uses the default argument for parameter %P, which "
7313 "is not yet defined", fn, parmnum);
7314 return error_mark_node;
7317 push_defarg_context (fn);
7319 if (fn && DECL_TEMPLATE_INFO (fn))
7320 arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
7322 /* Due to:
7324 [dcl.fct.default]
7326 The names in the expression are bound, and the semantic
7327 constraints are checked, at the point where the default
7328 expressions appears.
7330 we must not perform access checks here. */
7331 push_deferring_access_checks (dk_no_check);
7332 /* We must make a copy of ARG, in case subsequent processing
7333 alters any part of it. */
7334 arg = break_out_target_exprs (arg);
7335 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
7336 ICR_DEFAULT_ARGUMENT, fn, parmnum,
7337 complain);
7338 arg = convert_for_arg_passing (type, arg, complain);
7339 pop_deferring_access_checks();
7341 pop_defarg_context ();
7343 return arg;
7346 /* Returns the type which will really be used for passing an argument of
7347 type TYPE. */
7349 tree
7350 type_passed_as (tree type)
7352 /* Pass classes with copy ctors by invisible reference. */
7353 if (TREE_ADDRESSABLE (type))
7355 type = build_reference_type (type);
7356 /* There are no other pointers to this temporary. */
7357 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
7359 else if (targetm.calls.promote_prototypes (type)
7360 && INTEGRAL_TYPE_P (type)
7361 && COMPLETE_TYPE_P (type)
7362 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
7363 type = integer_type_node;
7365 return type;
7368 /* Actually perform the appropriate conversion. */
7370 tree
7371 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
7373 tree bitfield_type;
7375 /* If VAL is a bitfield, then -- since it has already been converted
7376 to TYPE -- it cannot have a precision greater than TYPE.
7378 If it has a smaller precision, we must widen it here. For
7379 example, passing "int f:3;" to a function expecting an "int" will
7380 not result in any conversion before this point.
7382 If the precision is the same we must not risk widening. For
7383 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
7384 often have type "int", even though the C++ type for the field is
7385 "long long". If the value is being passed to a function
7386 expecting an "int", then no conversions will be required. But,
7387 if we call convert_bitfield_to_declared_type, the bitfield will
7388 be converted to "long long". */
7389 bitfield_type = is_bitfield_expr_with_lowered_type (val);
7390 if (bitfield_type
7391 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
7392 val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
7394 if (val == error_mark_node)
7396 /* Pass classes with copy ctors by invisible reference. */
7397 else if (TREE_ADDRESSABLE (type))
7398 val = build1 (ADDR_EXPR, build_reference_type (type), val);
7399 else if (targetm.calls.promote_prototypes (type)
7400 && INTEGRAL_TYPE_P (type)
7401 && COMPLETE_TYPE_P (type)
7402 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
7403 val = cp_perform_integral_promotions (val, complain);
7404 if (complain & tf_warning)
7406 if (warn_suggest_attribute_format)
7408 tree rhstype = TREE_TYPE (val);
7409 const enum tree_code coder = TREE_CODE (rhstype);
7410 const enum tree_code codel = TREE_CODE (type);
7411 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7412 && coder == codel
7413 && check_missing_format_attribute (type, rhstype))
7414 warning (OPT_Wsuggest_attribute_format,
7415 "argument of function call might be a candidate "
7416 "for a format attribute");
7418 maybe_warn_parm_abi (type, EXPR_LOC_OR_LOC (val, input_location));
7420 return val;
7423 /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
7424 which just decay_conversion or no conversions at all should be done.
7425 This is true for some builtins which don't act like normal functions.
7426 Return 2 if no conversions at all should be done, 1 if just
7427 decay_conversion. Return 3 for special treatment of the 3rd argument
7428 for __builtin_*_overflow_p. */
7431 magic_varargs_p (tree fn)
7433 if (flag_cilkplus && is_cilkplus_reduce_builtin (fn) != BUILT_IN_NONE)
7434 return 2;
7436 if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
7437 switch (DECL_FUNCTION_CODE (fn))
7439 case BUILT_IN_CLASSIFY_TYPE:
7440 case BUILT_IN_CONSTANT_P:
7441 case BUILT_IN_NEXT_ARG:
7442 case BUILT_IN_VA_START:
7443 return 1;
7445 case BUILT_IN_ADD_OVERFLOW_P:
7446 case BUILT_IN_SUB_OVERFLOW_P:
7447 case BUILT_IN_MUL_OVERFLOW_P:
7448 return 3;
7450 default:;
7451 return lookup_attribute ("type generic",
7452 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
7455 return 0;
7458 /* Returns the decl of the dispatcher function if FN is a function version. */
7460 tree
7461 get_function_version_dispatcher (tree fn)
7463 tree dispatcher_decl = NULL;
7465 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7466 && DECL_FUNCTION_VERSIONED (fn));
7468 gcc_assert (targetm.get_function_versions_dispatcher);
7469 dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
7471 if (dispatcher_decl == NULL)
7473 error_at (input_location, "use of multiversioned function "
7474 "without a default");
7475 return NULL;
7478 retrofit_lang_decl (dispatcher_decl);
7479 gcc_assert (dispatcher_decl != NULL);
7480 return dispatcher_decl;
7483 /* fn is a function version dispatcher that is marked used. Mark all the
7484 semantically identical function versions it will dispatch as used. */
7486 void
7487 mark_versions_used (tree fn)
7489 struct cgraph_node *node;
7490 struct cgraph_function_version_info *node_v;
7491 struct cgraph_function_version_info *it_v;
7493 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7495 node = cgraph_node::get (fn);
7496 if (node == NULL)
7497 return;
7499 gcc_assert (node->dispatcher_function);
7501 node_v = node->function_version ();
7502 if (node_v == NULL)
7503 return;
7505 /* All semantically identical versions are chained. Traverse and mark each
7506 one of them as used. */
7507 it_v = node_v->next;
7508 while (it_v != NULL)
7510 mark_used (it_v->this_node->decl);
7511 it_v = it_v->next;
7515 /* Build a call to "the copy constructor" for the type of A, even if it
7516 wouldn't be selected by normal overload resolution. Used for
7517 diagnostics. */
7519 static tree
7520 call_copy_ctor (tree a, tsubst_flags_t complain)
7522 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
7523 tree binfo = TYPE_BINFO (ctype);
7524 tree copy = get_copy_ctor (ctype, complain);
7525 copy = build_baselink (binfo, binfo, copy, NULL_TREE);
7526 tree ob = build_dummy_object (ctype);
7527 vec<tree, va_gc>* args = make_tree_vector_single (a);
7528 tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
7529 LOOKUP_NORMAL, NULL, complain);
7530 release_tree_vector (args);
7531 return r;
7534 /* Return true iff T refers to a base field. */
7536 static bool
7537 is_base_field_ref (tree t)
7539 STRIP_NOPS (t);
7540 if (TREE_CODE (t) == ADDR_EXPR)
7541 t = TREE_OPERAND (t, 0);
7542 if (TREE_CODE (t) == COMPONENT_REF)
7543 t = TREE_OPERAND (t, 1);
7544 if (TREE_CODE (t) == FIELD_DECL)
7545 return DECL_FIELD_IS_BASE (t);
7546 return false;
7549 /* We can't elide a copy from a function returning by value to a base
7550 subobject, as the callee might clobber tail padding. Return true iff this
7551 could be that case. */
7553 static bool
7554 unsafe_copy_elision_p (tree target, tree exp)
7556 /* Copy elision only happens with a TARGET_EXPR. */
7557 if (TREE_CODE (exp) != TARGET_EXPR)
7558 return false;
7559 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7560 /* It's safe to elide the copy for a class with no tail padding. */
7561 if (tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
7562 return false;
7563 /* It's safe to elide the copy if we aren't initializing a base object. */
7564 if (!is_base_field_ref (target))
7565 return false;
7566 tree init = TARGET_EXPR_INITIAL (exp);
7567 /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
7568 while (TREE_CODE (init) == COMPOUND_EXPR)
7569 init = TREE_OPERAND (init, 1);
7570 return (TREE_CODE (init) == AGGR_INIT_EXPR
7571 && !AGGR_INIT_VIA_CTOR_P (init));
7574 /* Subroutine of the various build_*_call functions. Overload resolution
7575 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
7576 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
7577 bitmask of various LOOKUP_* flags which apply to the call itself. */
7579 static tree
7580 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
7582 tree fn = cand->fn;
7583 const vec<tree, va_gc> *args = cand->args;
7584 tree first_arg = cand->first_arg;
7585 conversion **convs = cand->convs;
7586 conversion *conv;
7587 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
7588 int parmlen;
7589 tree val;
7590 int i = 0;
7591 int j = 0;
7592 unsigned int arg_index = 0;
7593 int is_method = 0;
7594 int nargs;
7595 tree *argarray;
7596 bool already_used = false;
7598 /* In a template, there is no need to perform all of the work that
7599 is normally done. We are only interested in the type of the call
7600 expression, i.e., the return type of the function. Any semantic
7601 errors will be deferred until the template is instantiated. */
7602 if (processing_template_decl)
7604 tree expr, addr;
7605 tree return_type;
7606 const tree *argarray;
7607 unsigned int nargs;
7609 if (undeduced_auto_decl (fn))
7610 mark_used (fn, complain);
7612 return_type = TREE_TYPE (TREE_TYPE (fn));
7613 nargs = vec_safe_length (args);
7614 if (first_arg == NULL_TREE)
7615 argarray = args->address ();
7616 else
7618 tree *alcarray;
7619 unsigned int ix;
7620 tree arg;
7622 ++nargs;
7623 alcarray = XALLOCAVEC (tree, nargs);
7624 alcarray[0] = build_this (first_arg);
7625 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
7626 alcarray[ix + 1] = arg;
7627 argarray = alcarray;
7630 addr = build_addr_func (fn, complain);
7631 if (addr == error_mark_node)
7632 return error_mark_node;
7633 expr = build_call_array_loc (input_location, return_type,
7634 addr, nargs, argarray);
7635 if (TREE_THIS_VOLATILE (fn) && cfun)
7636 current_function_returns_abnormally = 1;
7637 return convert_from_reference (expr);
7640 /* Give any warnings we noticed during overload resolution. */
7641 if (cand->warnings && (complain & tf_warning))
7643 struct candidate_warning *w;
7644 for (w = cand->warnings; w; w = w->next)
7645 joust (cand, w->loser, 1, complain);
7648 /* OK, we're actually calling this inherited constructor; set its deletedness
7649 appropriately. We can get away with doing this here because calling is
7650 the only way to refer to a constructor. */
7651 if (DECL_INHERITED_CTOR (fn))
7652 deduce_inheriting_ctor (fn);
7654 /* Make =delete work with SFINAE. */
7655 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7656 return error_mark_node;
7658 if (DECL_FUNCTION_MEMBER_P (fn))
7660 tree access_fn;
7661 /* If FN is a template function, two cases must be considered.
7662 For example:
7664 struct A {
7665 protected:
7666 template <class T> void f();
7668 template <class T> struct B {
7669 protected:
7670 void g();
7672 struct C : A, B<int> {
7673 using A::f; // #1
7674 using B<int>::g; // #2
7677 In case #1 where `A::f' is a member template, DECL_ACCESS is
7678 recorded in the primary template but not in its specialization.
7679 We check access of FN using its primary template.
7681 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
7682 because it is a member of class template B, DECL_ACCESS is
7683 recorded in the specialization `B<int>::g'. We cannot use its
7684 primary template because `B<T>::g' and `B<int>::g' may have
7685 different access. */
7686 if (DECL_TEMPLATE_INFO (fn)
7687 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
7688 access_fn = DECL_TI_TEMPLATE (fn);
7689 else
7690 access_fn = fn;
7691 if (!perform_or_defer_access_check (cand->access_path, access_fn,
7692 fn, complain))
7693 return error_mark_node;
7696 /* If we're checking for implicit delete, don't bother with argument
7697 conversions. */
7698 if (flags & LOOKUP_SPECULATIVE)
7700 if (DECL_DELETED_FN (fn))
7702 if (complain & tf_error)
7703 mark_used (fn);
7704 return error_mark_node;
7706 if (cand->viable == 1)
7707 return fn;
7708 else if (!(complain & tf_error))
7709 /* Reject bad conversions now. */
7710 return error_mark_node;
7711 /* else continue to get conversion error. */
7714 /* N3276 magic doesn't apply to nested calls. */
7715 tsubst_flags_t decltype_flag = (complain & tf_decltype);
7716 complain &= ~tf_decltype;
7717 /* No-Cleanup doesn't apply to nested calls either. */
7718 tsubst_flags_t no_cleanup_complain = complain;
7719 complain &= ~tf_no_cleanup;
7721 /* Find maximum size of vector to hold converted arguments. */
7722 parmlen = list_length (parm);
7723 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
7724 if (parmlen > nargs)
7725 nargs = parmlen;
7726 argarray = XALLOCAVEC (tree, nargs);
7728 /* The implicit parameters to a constructor are not considered by overload
7729 resolution, and must be of the proper type. */
7730 if (DECL_CONSTRUCTOR_P (fn))
7732 tree object_arg;
7733 if (first_arg != NULL_TREE)
7735 object_arg = first_arg;
7736 first_arg = NULL_TREE;
7738 else
7740 object_arg = (*args)[arg_index];
7741 ++arg_index;
7743 argarray[j++] = build_this (object_arg);
7744 parm = TREE_CHAIN (parm);
7745 /* We should never try to call the abstract constructor. */
7746 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
7748 if (DECL_HAS_VTT_PARM_P (fn))
7750 argarray[j++] = (*args)[arg_index];
7751 ++arg_index;
7752 parm = TREE_CHAIN (parm);
7755 if (flags & LOOKUP_PREFER_RVALUE)
7757 /* The implicit move specified in 15.8.3/3 fails "...if the type of
7758 the first parameter of the selected constructor is not an rvalue
7759 reference to the object’s type (possibly cv-qualified)...." */
7760 gcc_assert (!(complain & tf_error));
7761 tree ptype = convs[0]->type;
7762 if (TREE_CODE (ptype) != REFERENCE_TYPE
7763 || !TYPE_REF_IS_RVALUE (ptype)
7764 || CONVERSION_RANK (convs[0]) > cr_exact)
7765 return error_mark_node;
7768 /* Bypass access control for 'this' parameter. */
7769 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7771 tree parmtype = TREE_VALUE (parm);
7772 tree arg = build_this (first_arg != NULL_TREE
7773 ? first_arg
7774 : (*args)[arg_index]);
7775 tree argtype = TREE_TYPE (arg);
7776 tree converted_arg;
7777 tree base_binfo;
7779 if (arg == error_mark_node)
7780 return error_mark_node;
7782 if (convs[i]->bad_p)
7784 if (complain & tf_error)
7786 if (permerror (input_location, "passing %qT as %<this%> "
7787 "argument discards qualifiers",
7788 TREE_TYPE (argtype)))
7789 inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
7791 else
7792 return error_mark_node;
7795 /* See if the function member or the whole class type is declared
7796 final and the call can be devirtualized. */
7797 if (DECL_FINAL_P (fn)
7798 || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
7799 flags |= LOOKUP_NONVIRTUAL;
7801 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
7802 X is called for an object that is not of type X, or of a type
7803 derived from X, the behavior is undefined.
7805 So we can assume that anything passed as 'this' is non-null, and
7806 optimize accordingly. */
7807 gcc_assert (TYPE_PTR_P (parmtype));
7808 /* Convert to the base in which the function was declared. */
7809 gcc_assert (cand->conversion_path != NULL_TREE);
7810 converted_arg = build_base_path (PLUS_EXPR,
7811 arg,
7812 cand->conversion_path,
7813 1, complain);
7814 /* Check that the base class is accessible. */
7815 if (!accessible_base_p (TREE_TYPE (argtype),
7816 BINFO_TYPE (cand->conversion_path), true))
7818 if (complain & tf_error)
7819 error ("%qT is not an accessible base of %qT",
7820 BINFO_TYPE (cand->conversion_path),
7821 TREE_TYPE (argtype));
7822 else
7823 return error_mark_node;
7825 /* If fn was found by a using declaration, the conversion path
7826 will be to the derived class, not the base declaring fn. We
7827 must convert from derived to base. */
7828 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
7829 TREE_TYPE (parmtype), ba_unique,
7830 NULL, complain);
7831 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
7832 base_binfo, 1, complain);
7834 argarray[j++] = converted_arg;
7835 parm = TREE_CHAIN (parm);
7836 if (first_arg != NULL_TREE)
7837 first_arg = NULL_TREE;
7838 else
7839 ++arg_index;
7840 ++i;
7841 is_method = 1;
7844 gcc_assert (first_arg == NULL_TREE);
7845 for (; arg_index < vec_safe_length (args) && parm;
7846 parm = TREE_CHAIN (parm), ++arg_index, ++i)
7848 tree type = TREE_VALUE (parm);
7849 tree arg = (*args)[arg_index];
7850 bool conversion_warning = true;
7852 conv = convs[i];
7854 /* If the argument is NULL and used to (implicitly) instantiate a
7855 template function (and bind one of the template arguments to
7856 the type of 'long int'), we don't want to warn about passing NULL
7857 to non-pointer argument.
7858 For example, if we have this template function:
7860 template<typename T> void func(T x) {}
7862 we want to warn (when -Wconversion is enabled) in this case:
7864 void foo() {
7865 func<int>(NULL);
7868 but not in this case:
7870 void foo() {
7871 func(NULL);
7874 if (arg == null_node
7875 && DECL_TEMPLATE_INFO (fn)
7876 && cand->template_decl
7877 && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
7878 conversion_warning = false;
7880 /* Warn about initializer_list deduction that isn't currently in the
7881 working draft. */
7882 if (cxx_dialect > cxx98
7883 && flag_deduce_init_list
7884 && cand->template_decl
7885 && is_std_init_list (non_reference (type))
7886 && BRACE_ENCLOSED_INITIALIZER_P (arg))
7888 tree tmpl = TI_TEMPLATE (cand->template_decl);
7889 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
7890 tree patparm = get_pattern_parm (realparm, tmpl);
7891 tree pattype = TREE_TYPE (patparm);
7892 if (PACK_EXPANSION_P (pattype))
7893 pattype = PACK_EXPANSION_PATTERN (pattype);
7894 pattype = non_reference (pattype);
7896 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
7897 && (cand->explicit_targs == NULL_TREE
7898 || (TREE_VEC_LENGTH (cand->explicit_targs)
7899 <= TEMPLATE_TYPE_IDX (pattype))))
7901 pedwarn (input_location, 0, "deducing %qT as %qT",
7902 non_reference (TREE_TYPE (patparm)),
7903 non_reference (type));
7904 pedwarn (DECL_SOURCE_LOCATION (cand->fn), 0,
7905 " in call to %qD", cand->fn);
7906 pedwarn (input_location, 0,
7907 " (you can disable this with -fno-deduce-init-list)");
7911 /* Set user_conv_p on the argument conversions, so rvalue/base handling
7912 knows not to allow any more UDCs. This needs to happen after we
7913 process cand->warnings. */
7914 if (flags & LOOKUP_NO_CONVERSION)
7915 conv->user_conv_p = true;
7917 tsubst_flags_t arg_complain = complain;
7918 if (!conversion_warning)
7919 arg_complain &= ~tf_warning;
7921 val = convert_like_with_context (conv, arg, fn, i - is_method,
7922 arg_complain);
7923 val = convert_for_arg_passing (type, val, arg_complain);
7925 if (val == error_mark_node)
7926 return error_mark_node;
7927 else
7928 argarray[j++] = val;
7931 /* Default arguments */
7932 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
7934 if (TREE_VALUE (parm) == error_mark_node)
7935 return error_mark_node;
7936 val = convert_default_arg (TREE_VALUE (parm),
7937 TREE_PURPOSE (parm),
7938 fn, i - is_method,
7939 complain);
7940 if (val == error_mark_node)
7941 return error_mark_node;
7942 argarray[j++] = val;
7945 /* Ellipsis */
7946 int magic = magic_varargs_p (fn);
7947 for (; arg_index < vec_safe_length (args); ++arg_index)
7949 tree a = (*args)[arg_index];
7950 if ((magic == 3 && arg_index == 2) || magic == 2)
7952 /* Do no conversions for certain magic varargs. */
7953 a = mark_type_use (a);
7954 if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
7955 return error_mark_node;
7957 else if (magic != 0)
7958 /* For other magic varargs only do decay_conversion. */
7959 a = decay_conversion (a, complain);
7960 else if (DECL_CONSTRUCTOR_P (fn)
7961 && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
7962 TREE_TYPE (a)))
7964 /* Avoid infinite recursion trying to call A(...). */
7965 if (complain & tf_error)
7966 /* Try to call the actual copy constructor for a good error. */
7967 call_copy_ctor (a, complain);
7968 return error_mark_node;
7970 else
7971 a = convert_arg_to_ellipsis (a, complain);
7972 if (a == error_mark_node)
7973 return error_mark_node;
7974 argarray[j++] = a;
7977 gcc_assert (j <= nargs);
7978 nargs = j;
7980 /* Avoid to do argument-transformation, if warnings for format, and for
7981 nonnull are disabled. Just in case that at least one of them is active
7982 the check_function_arguments function might warn about something. */
7984 bool warned_p = false;
7985 if (warn_nonnull
7986 || warn_format
7987 || warn_suggest_attribute_format
7988 || warn_restrict)
7990 tree *fargs = (!nargs ? argarray
7991 : (tree *) alloca (nargs * sizeof (tree)));
7992 for (j = 0; j < nargs; j++)
7993 fargs[j] = maybe_constant_value (argarray[j]);
7995 warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
7996 nargs, fargs, NULL);
7999 if (DECL_INHERITED_CTOR (fn))
8001 /* Check for passing ellipsis arguments to an inherited constructor. We
8002 could handle this by open-coding the inherited constructor rather than
8003 defining it, but let's not bother now. */
8004 if (!cp_unevaluated_operand
8005 && cand->num_convs
8006 && cand->convs[cand->num_convs-1]->ellipsis_p)
8008 if (complain & tf_error)
8010 sorry ("passing arguments to ellipsis of inherited constructor "
8011 "%qD", cand->fn);
8012 inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
8014 return error_mark_node;
8017 /* A base constructor inheriting from a virtual base doesn't get the
8018 inherited arguments, just this and __vtt. */
8019 if (ctor_omit_inherited_parms (fn))
8020 nargs = 2;
8023 /* Avoid actually calling copy constructors and copy assignment operators,
8024 if possible. */
8026 if (! flag_elide_constructors)
8027 /* Do things the hard way. */;
8028 else if (cand->num_convs == 1
8029 && (DECL_COPY_CONSTRUCTOR_P (fn)
8030 || DECL_MOVE_CONSTRUCTOR_P (fn))
8031 /* It's unsafe to elide the constructor when handling
8032 a noexcept-expression, it may evaluate to the wrong
8033 value (c++/53025). */
8034 && cp_noexcept_operand == 0)
8036 tree targ;
8037 tree arg = argarray[num_artificial_parms_for (fn)];
8038 tree fa;
8039 bool trivial = trivial_fn_p (fn);
8041 /* Pull out the real argument, disregarding const-correctness. */
8042 targ = arg;
8043 /* Strip the reference binding for the constructor parameter. */
8044 if (CONVERT_EXPR_P (targ)
8045 && TREE_CODE (TREE_TYPE (targ)) == REFERENCE_TYPE)
8046 targ = TREE_OPERAND (targ, 0);
8047 /* But don't strip any other reference bindings; binding a temporary to a
8048 reference prevents copy elision. */
8049 while ((CONVERT_EXPR_P (targ)
8050 && TREE_CODE (TREE_TYPE (targ)) != REFERENCE_TYPE)
8051 || TREE_CODE (targ) == NON_LVALUE_EXPR)
8052 targ = TREE_OPERAND (targ, 0);
8053 if (TREE_CODE (targ) == ADDR_EXPR)
8055 targ = TREE_OPERAND (targ, 0);
8056 if (!same_type_ignoring_top_level_qualifiers_p
8057 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
8058 targ = NULL_TREE;
8060 else
8061 targ = NULL_TREE;
8063 if (targ)
8064 arg = targ;
8065 else
8066 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
8068 /* In C++17 we shouldn't be copying a TARGET_EXPR except into a base
8069 subobject. */
8070 if (CHECKING_P && cxx_dialect >= cxx17)
8071 gcc_assert (TREE_CODE (arg) != TARGET_EXPR
8072 /* It's from binding the ref parm to a packed field. */
8073 || convs[0]->need_temporary_p
8074 || seen_error ()
8075 /* See unsafe_copy_elision_p. */
8076 || DECL_BASE_CONSTRUCTOR_P (fn));
8078 /* [class.copy]: the copy constructor is implicitly defined even if
8079 the implementation elided its use. */
8080 if (!trivial || DECL_DELETED_FN (fn))
8082 if (!mark_used (fn, complain) && !(complain & tf_error))
8083 return error_mark_node;
8084 already_used = true;
8087 /* If we're creating a temp and we already have one, don't create a
8088 new one. If we're not creating a temp but we get one, use
8089 INIT_EXPR to collapse the temp into our target. Otherwise, if the
8090 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
8091 temp or an INIT_EXPR otherwise. */
8092 fa = argarray[0];
8093 if (is_dummy_object (fa))
8095 if (TREE_CODE (arg) == TARGET_EXPR)
8096 return arg;
8097 else if (trivial)
8098 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
8100 else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
8101 && !unsafe_copy_elision_p (fa, arg))
8103 tree to = cp_stabilize_reference (cp_build_indirect_ref (fa,
8104 RO_NULL,
8105 complain));
8107 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
8108 return val;
8111 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
8112 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
8113 && trivial_fn_p (fn)
8114 && !DECL_DELETED_FN (fn))
8116 tree to = cp_stabilize_reference
8117 (cp_build_indirect_ref (argarray[0], RO_NULL, complain));
8118 tree type = TREE_TYPE (to);
8119 tree as_base = CLASSTYPE_AS_BASE (type);
8120 tree arg = argarray[1];
8122 if (is_really_empty_class (type))
8124 /* Avoid copying empty classes. */
8125 val = build2 (COMPOUND_EXPR, type, arg, to);
8126 TREE_NO_WARNING (val) = 1;
8128 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
8130 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
8131 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
8132 /* Handle NSDMI that refer to the object being initialized. */
8133 replace_placeholders (arg, to);
8135 else
8137 /* We must only copy the non-tail padding parts. */
8138 tree arg0, arg2, t;
8139 tree array_type, alias_set;
8141 arg2 = TYPE_SIZE_UNIT (as_base);
8142 arg0 = cp_build_addr_expr (to, complain);
8144 array_type = build_array_type (unsigned_char_type_node,
8145 build_index_type
8146 (size_binop (MINUS_EXPR,
8147 arg2, size_int (1))));
8148 alias_set = build_int_cst (build_pointer_type (type), 0);
8149 t = build2 (MODIFY_EXPR, void_type_node,
8150 build2 (MEM_REF, array_type, arg0, alias_set),
8151 build2 (MEM_REF, array_type, arg, alias_set));
8152 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
8153 TREE_NO_WARNING (val) = 1;
8156 return val;
8158 else if (!DECL_DELETED_FN (fn)
8159 && trivial_fn_p (fn))
8161 if (DECL_DESTRUCTOR_P (fn))
8162 return fold_convert (void_type_node, argarray[0]);
8163 else if (default_ctor_p (fn))
8165 if (is_dummy_object (argarray[0]))
8166 return force_target_expr (DECL_CONTEXT (fn), void_node,
8167 no_cleanup_complain);
8168 else
8169 return cp_build_indirect_ref (argarray[0], RO_NULL, complain);
8173 /* For calls to a multi-versioned function, overload resolution
8174 returns the function with the highest target priority, that is,
8175 the version that will checked for dispatching first. If this
8176 version is inlinable, a direct call to this version can be made
8177 otherwise the call should go through the dispatcher. */
8179 if (DECL_FUNCTION_VERSIONED (fn)
8180 && (current_function_decl == NULL
8181 || !targetm.target_option.can_inline_p (current_function_decl, fn)))
8183 fn = get_function_version_dispatcher (fn);
8184 if (fn == NULL)
8185 return NULL;
8186 if (!already_used)
8187 mark_versions_used (fn);
8190 if (!already_used
8191 && !mark_used (fn, complain))
8192 return error_mark_node;
8194 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
8195 /* Don't mess with virtual lookup in instantiate_non_dependent_expr;
8196 virtual functions can't be constexpr. */
8197 && !in_template_function ())
8199 tree t;
8200 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
8201 DECL_CONTEXT (fn),
8202 ba_any, NULL, complain);
8203 gcc_assert (binfo && binfo != error_mark_node);
8205 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
8206 complain);
8207 if (TREE_SIDE_EFFECTS (argarray[0]))
8208 argarray[0] = save_expr (argarray[0]);
8209 t = build_pointer_type (TREE_TYPE (fn));
8210 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
8211 TREE_TYPE (fn) = t;
8213 else
8215 fn = build_addr_func (fn, complain);
8216 if (fn == error_mark_node)
8217 return error_mark_node;
8220 tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
8221 if (call == error_mark_node)
8222 return call;
8223 if (cand->flags & LOOKUP_LIST_INIT_CTOR)
8225 tree c = extract_call_expr (call);
8226 /* build_new_op_1 will clear this when appropriate. */
8227 CALL_EXPR_ORDERED_ARGS (c) = true;
8229 if (warned_p)
8231 tree c = extract_call_expr (call);
8232 if (TREE_CODE (c) == CALL_EXPR)
8233 TREE_NO_WARNING (c) = 1;
8235 return call;
8238 /* Return the DECL of the first non-public data member of class TYPE
8239 or null if none can be found. */
8241 static tree
8242 first_non_public_field (tree type)
8244 if (!CLASS_TYPE_P (type))
8245 return NULL_TREE;
8247 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8249 if (TREE_CODE (field) != FIELD_DECL)
8250 continue;
8251 if (TREE_STATIC (field))
8252 continue;
8253 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
8254 return field;
8257 int i = 0;
8259 for (tree base_binfo, binfo = TYPE_BINFO (type);
8260 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8262 tree base = TREE_TYPE (base_binfo);
8264 if (tree field = first_non_public_field (base))
8265 return field;
8268 return NULL_TREE;
8271 /* Return true if all copy and move assignment operator overloads for
8272 class TYPE are trivial and at least one of them is not deleted and,
8273 when ACCESS is set, accessible. Return false otherwise. Set
8274 HASASSIGN to true when the TYPE has a (not necessarily trivial)
8275 copy or move assignment. */
8277 static bool
8278 has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
8280 tree fns = get_class_binding (type, assign_op_identifier);
8281 bool all_trivial = true;
8283 /* Iterate over overloads of the assignment operator, checking
8284 accessible copy assignments for triviality. */
8286 for (ovl_iterator oi (fns); oi; ++oi)
8288 tree f = *oi;
8290 /* Skip operators that aren't copy assignments. */
8291 if (!copy_fn_p (f))
8292 continue;
8294 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
8295 || accessible_p (TYPE_BINFO (type), f, true));
8297 /* Skip template assignment operators and deleted functions. */
8298 if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
8299 continue;
8301 if (accessible)
8302 *hasassign = true;
8304 if (!accessible || !trivial_fn_p (f))
8305 all_trivial = false;
8307 /* Break early when both properties have been determined. */
8308 if (*hasassign && !all_trivial)
8309 break;
8312 /* Return true if they're all trivial and one of the expressions
8313 TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
8314 tree ref = cp_build_reference_type (type, false);
8315 return (all_trivial
8316 && (is_trivially_xible (MODIFY_EXPR, type, type)
8317 || is_trivially_xible (MODIFY_EXPR, type, ref)));
8320 /* Return true if all copy and move ctor overloads for class TYPE are
8321 trivial and at least one of them is not deleted and, when ACCESS is
8322 set, accessible. Return false otherwise. Set each element of HASCTOR[]
8323 to true when the TYPE has a (not necessarily trivial) default and copy
8324 (or move) ctor, respectively. */
8326 static bool
8327 has_trivial_copy_p (tree type, bool access, bool hasctor[2])
8329 tree fns = get_class_binding (type, complete_ctor_identifier);
8330 bool all_trivial = true;
8332 for (ovl_iterator oi (fns); oi; ++oi)
8334 tree f = *oi;
8336 /* Skip template constructors. */
8337 if (TREE_CODE (f) != FUNCTION_DECL)
8338 continue;
8340 bool cpy_or_move_ctor_p = copy_fn_p (f);
8342 /* Skip ctors other than default, copy, and move. */
8343 if (!cpy_or_move_ctor_p && !default_ctor_p (f))
8344 continue;
8346 if (DECL_DELETED_FN (f))
8347 continue;
8349 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
8350 || accessible_p (TYPE_BINFO (type), f, true));
8352 if (accessible)
8353 hasctor[cpy_or_move_ctor_p] = true;
8355 if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
8356 all_trivial = false;
8358 /* Break early when both properties have been determined. */
8359 if (hasctor[0] && hasctor[1] && !all_trivial)
8360 break;
8363 return all_trivial;
8366 /* Issue a warning on a call to the built-in function FNDECL if it is
8367 a raw memory write whose destination is not an object of (something
8368 like) trivial or standard layout type with a non-deleted assignment
8369 and copy ctor. Detects const correctness violations, corrupting
8370 references, virtual table pointers, and bypassing non-trivial
8371 assignments. */
8373 static void
8374 maybe_warn_class_memaccess (location_t loc, tree fndecl, tree *args)
8376 /* Except for bcopy where it's second, the destination pointer is
8377 the first argument for all functions handled here. Compute
8378 the index of the destination and source arguments. */
8379 unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
8380 unsigned srcidx = !dstidx;
8382 tree dest = args[dstidx];
8383 if (!dest || !TREE_TYPE (dest) || !POINTER_TYPE_P (TREE_TYPE (dest)))
8384 return;
8386 /* Remove the outermost (usually implicit) conversion to the void*
8387 argument type. */
8388 if (TREE_CODE (dest) == NOP_EXPR)
8389 dest = TREE_OPERAND (dest, 0);
8391 tree srctype = NULL_TREE;
8393 /* Determine the type of the pointed-to object and whether it's
8394 a complete class type. */
8395 tree desttype = TREE_TYPE (TREE_TYPE (dest));
8397 if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
8398 return;
8400 /* Check to see if the raw memory call is made by a ctor or dtor
8401 with this as the destination argument for the destination type.
8402 If so, be more permissive. */
8403 if (current_function_decl
8404 && (DECL_CONSTRUCTOR_P (current_function_decl)
8405 || DECL_DESTRUCTOR_P (current_function_decl))
8406 && is_this_parameter (tree_strip_nop_conversions (dest)))
8408 tree ctx = DECL_CONTEXT (current_function_decl);
8409 bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
8411 tree binfo = TYPE_BINFO (ctx);
8413 /* A ctor and dtor for a class with no bases and no virtual functions
8414 can do whatever they want. Bail early with no further checking. */
8415 if (special && !BINFO_VTABLE (binfo) && !BINFO_N_BASE_BINFOS (binfo))
8416 return;
8419 /* True if the class is trivial. */
8420 bool trivial = trivial_type_p (desttype);
8422 /* Set to true if DESTYPE has an accessible copy assignment. */
8423 bool hasassign = false;
8424 /* True if all of the class' overloaded copy assignment operators
8425 are all trivial (and not deleted) and at least one of them is
8426 accessible. */
8427 bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
8429 /* Set to true if DESTTYPE has an accessible default and copy ctor,
8430 respectively. */
8431 bool hasctors[2] = { false, false };
8433 /* True if all of the class' overloaded copy constructors are all
8434 trivial (and not deleted) and at least one of them is accessible. */
8435 bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
8437 /* Set FLD to the first private/protected member of the class. */
8438 tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
8440 /* The warning format string. */
8441 const char *warnfmt = NULL;
8442 /* A suggested alternative to offer instead of the raw memory call.
8443 Empty string when none can be come up with. */
8444 const char *suggest = "";
8445 bool warned = false;
8447 switch (DECL_FUNCTION_CODE (fndecl))
8449 case BUILT_IN_MEMSET:
8450 if (!integer_zerop (args[1]))
8452 /* Diagnose setting non-copy-assignable or non-trivial types,
8453 or types with a private member, to (potentially) non-zero
8454 bytes. Since the value of the bytes being written is unknown,
8455 suggest using assignment instead (if one exists). Also warn
8456 for writes into objects for which zero-initialization doesn't
8457 mean all bits clear (pointer-to-member data, where null is all
8458 bits set). Since the value being written is (most likely)
8459 non-zero, simply suggest assignment (but not copy assignment). */
8460 suggest = "; use assignment instead";
8461 if (!trivassign)
8462 warnfmt = G_("%qD writing to an object of type %#qT with "
8463 "no trivial copy-assignment");
8464 else if (!trivial)
8465 warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
8466 else if (fld)
8468 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
8469 warned = warning_at (loc, OPT_Wclass_memaccess,
8470 "%qD writing to an object of type %#qT with "
8471 "%qs member %qD",
8472 fndecl, desttype, access, fld);
8474 else if (!zero_init_p (desttype))
8475 warnfmt = G_("%qD writing to an object of type %#qT containing "
8476 "a pointer to data member%s");
8478 break;
8480 /* Fall through. */
8482 case BUILT_IN_BZERO:
8483 /* Similarly to the above, diagnose clearing non-trivial or non-
8484 standard layout objects, or objects of types with no assignmenmt.
8485 Since the value being written is known to be zero, suggest either
8486 copy assignment, copy ctor, or default ctor as an alternative,
8487 depending on what's available. */
8489 if (hasassign && hasctors[0])
8490 suggest = G_("; use assignment or value-initialization instead");
8491 else if (hasassign)
8492 suggest = G_("; use assignment instead");
8493 else if (hasctors[0])
8494 suggest = G_("; use value-initialization instead");
8496 if (!trivassign)
8497 warnfmt = G_("%qD clearing an object of type %#qT with "
8498 "no trivial copy-assignment%s");
8499 else if (!trivial)
8500 warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
8501 else if (!zero_init_p (desttype))
8502 warnfmt = G_("%qD clearing an object of type %#qT containing "
8503 "a pointer-to-member%s");
8504 break;
8506 case BUILT_IN_BCOPY:
8507 case BUILT_IN_MEMCPY:
8508 case BUILT_IN_MEMMOVE:
8509 case BUILT_IN_MEMPCPY:
8510 /* Determine the type of the source object. */
8511 srctype = STRIP_NOPS (args[srcidx]);
8512 srctype = TREE_TYPE (TREE_TYPE (srctype));
8514 /* Since it's impossible to determine wheter the byte copy is
8515 being used in place of assignment to an existing object or
8516 as a substitute for initialization, assume it's the former.
8517 Determine the best alternative to use instead depending on
8518 what's not deleted. */
8519 if (hasassign && hasctors[1])
8520 suggest = G_("; use copy-assignment or copy-initialization instead");
8521 else if (hasassign)
8522 suggest = G_("; use copy-assignment instead");
8523 else if (hasctors[1])
8524 suggest = G_("; use copy-initialization instead");
8526 if (!trivassign)
8527 warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
8528 "copy-assignment%s");
8529 else if (!trivially_copyable_p (desttype))
8530 warnfmt = G_("%qD writing to an object of non-trivially copyable "
8531 "type %#qT%s");
8532 else if (!trivcopy)
8533 warnfmt = G_("%qD writing to an object with a deleted copy constructor");
8535 else if (!trivial
8536 && !VOID_TYPE_P (srctype)
8537 && !char_type_p (TYPE_MAIN_VARIANT (srctype))
8538 && !same_type_ignoring_top_level_qualifiers_p (desttype,
8539 srctype))
8541 /* Warn when copying into a non-trivial object from an object
8542 of a different type other than void or char. */
8543 warned = warning_at (loc, OPT_Wclass_memaccess,
8544 "%qD copying an object of non-trivial type "
8545 "%#qT from an array of %#qT",
8546 fndecl, desttype, srctype);
8548 else if (fld
8549 && !VOID_TYPE_P (srctype)
8550 && !char_type_p (TYPE_MAIN_VARIANT (srctype))
8551 && !same_type_ignoring_top_level_qualifiers_p (desttype,
8552 srctype))
8554 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
8555 warned = warning_at (loc, OPT_Wclass_memaccess,
8556 "%qD copying an object of type %#qT with "
8557 "%qs member %qD from an array of %#qT; use "
8558 "assignment or copy-initialization instead",
8559 fndecl, desttype, access, fld, srctype);
8561 else if (!trivial && TREE_CODE (args[2]) == INTEGER_CST)
8563 /* Finally, warn on partial copies. */
8564 unsigned HOST_WIDE_INT typesize
8565 = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
8566 if (unsigned HOST_WIDE_INT partial
8567 = tree_to_uhwi (args[2]) % typesize)
8568 warned = warning_at (loc, OPT_Wclass_memaccess,
8569 (typesize - partial > 1
8570 ? G_("%qD writing to an object of "
8571 "a non-trivial type %#qT leaves %wu "
8572 "bytes unchanged")
8573 : G_("%qD writing to an object of "
8574 "a non-trivial type %#qT leaves %wu "
8575 "byte unchanged")),
8576 fndecl, desttype, typesize - partial);
8578 break;
8580 case BUILT_IN_REALLOC:
8582 if (!trivially_copyable_p (desttype))
8583 warnfmt = G_("%qD moving an object of non-trivially copyable type "
8584 "%#qT; use %<new%> and %<delete%> instead");
8585 else if (!trivcopy)
8586 warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
8587 "constructor; use %<new%> and %<delete%> instead");
8588 else if (!get_dtor (desttype, tf_none))
8589 warnfmt = G_("%qD moving an object of type %#qT with deleted "
8590 "destructor");
8591 else if (!trivial
8592 && TREE_CODE (args[1]) == INTEGER_CST
8593 && tree_int_cst_lt (args[1], TYPE_SIZE_UNIT (desttype)))
8595 /* Finally, warn on reallocation into insufficient space. */
8596 warned = warning_at (loc, OPT_Wclass_memaccess,
8597 "%qD moving an object of non-trivial type "
8598 "%#qT and size %E into a region of size %E",
8599 fndecl, desttype, TYPE_SIZE_UNIT (desttype),
8600 args[1]);
8602 break;
8604 default:
8605 return;
8608 if (!warned && !warnfmt)
8609 return;
8611 if (warnfmt)
8613 if (suggest)
8614 warned = warning_at (loc, OPT_Wclass_memaccess,
8615 warnfmt, fndecl, desttype, suggest);
8616 else
8617 warned = warning_at (loc, OPT_Wclass_memaccess,
8618 warnfmt, fndecl, desttype);
8621 if (warned)
8622 inform (location_of (desttype), "%#qT declared here", desttype);
8625 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
8626 This function performs no overload resolution, conversion, or other
8627 high-level operations. */
8629 tree
8630 build_cxx_call (tree fn, int nargs, tree *argarray,
8631 tsubst_flags_t complain)
8633 tree fndecl;
8635 /* Remember roughly where this call is. */
8636 location_t loc = EXPR_LOC_OR_LOC (fn, input_location);
8637 fn = build_call_a (fn, nargs, argarray);
8638 SET_EXPR_LOCATION (fn, loc);
8640 fndecl = get_callee_fndecl (fn);
8642 /* Check that arguments to builtin functions match the expectations. */
8643 if (fndecl
8644 && DECL_BUILT_IN (fndecl)
8645 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
8647 int i;
8649 /* We need to take care that values to BUILT_IN_NORMAL
8650 are reduced. */
8651 for (i = 0; i < nargs; i++)
8652 argarray[i] = fold_non_dependent_expr (argarray[i]);
8654 if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
8655 nargs, argarray))
8656 return error_mark_node;
8658 /* Warn if the built-in writes to an object of a non-trivial type. */
8659 if (nargs)
8660 maybe_warn_class_memaccess (loc, fndecl, argarray);
8663 /* If it is a built-in array notation function, then the return type of
8664 the function is the element type of the array passed in as array
8665 notation (i.e. the first parameter of the function). */
8666 if (flag_cilkplus && TREE_CODE (fn) == CALL_EXPR)
8668 enum built_in_function bif =
8669 is_cilkplus_reduce_builtin (CALL_EXPR_FN (fn));
8670 if (bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
8671 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
8672 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
8673 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
8674 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE
8675 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
8677 if (call_expr_nargs (fn) == 0)
8679 error_at (EXPR_LOCATION (fn), "Invalid builtin arguments");
8680 return error_mark_node;
8682 /* for bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO or
8683 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO or
8684 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO or
8685 BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO or
8686 BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND or
8687 BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
8688 The pre-defined return-type is the correct one. */
8689 tree array_ntn = CALL_EXPR_ARG (fn, 0);
8690 TREE_TYPE (fn) = TREE_TYPE (array_ntn);
8691 return fn;
8695 if (VOID_TYPE_P (TREE_TYPE (fn)))
8696 return fn;
8698 /* 5.2.2/11: If a function call is a prvalue of object type: if the
8699 function call is either the operand of a decltype-specifier or the
8700 right operand of a comma operator that is the operand of a
8701 decltype-specifier, a temporary object is not introduced for the
8702 prvalue. The type of the prvalue may be incomplete. */
8703 if (!(complain & tf_decltype))
8705 fn = require_complete_type_sfinae (fn, complain);
8706 if (fn == error_mark_node)
8707 return error_mark_node;
8709 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
8711 fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
8712 maybe_warn_parm_abi (TREE_TYPE (fn), loc);
8715 return convert_from_reference (fn);
8718 /* Returns the value to use for the in-charge parameter when making a
8719 call to a function with the indicated NAME.
8721 FIXME:Can't we find a neater way to do this mapping? */
8723 tree
8724 in_charge_arg_for_name (tree name)
8726 if (IDENTIFIER_CTOR_P (name))
8728 if (name == complete_ctor_identifier)
8729 return integer_one_node;
8730 gcc_checking_assert (name == base_ctor_identifier);
8732 else
8734 if (name == complete_dtor_identifier)
8735 return integer_two_node;
8736 else if (name == deleting_dtor_identifier)
8737 return integer_three_node;
8738 gcc_checking_assert (name == base_dtor_identifier);
8741 return integer_zero_node;
8744 /* We've built up a constructor call RET. Complain if it delegates to the
8745 constructor we're currently compiling. */
8747 static void
8748 check_self_delegation (tree ret)
8750 if (TREE_CODE (ret) == TARGET_EXPR)
8751 ret = TARGET_EXPR_INITIAL (ret);
8752 tree fn = cp_get_callee_fndecl (ret);
8753 if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
8754 error ("constructor delegates to itself");
8757 /* Build a call to a constructor, destructor, or an assignment
8758 operator for INSTANCE, an expression with class type. NAME
8759 indicates the special member function to call; *ARGS are the
8760 arguments. ARGS may be NULL. This may change ARGS. BINFO
8761 indicates the base of INSTANCE that is to be passed as the `this'
8762 parameter to the member function called.
8764 FLAGS are the LOOKUP_* flags to use when processing the call.
8766 If NAME indicates a complete object constructor, INSTANCE may be
8767 NULL_TREE. In this case, the caller will call build_cplus_new to
8768 store the newly constructed object into a VAR_DECL. */
8770 tree
8771 build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
8772 tree binfo, int flags, tsubst_flags_t complain)
8774 tree fns;
8775 /* The type of the subobject to be constructed or destroyed. */
8776 tree class_type;
8777 vec<tree, va_gc> *allocated = NULL;
8778 tree ret;
8780 gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
8781 if (TYPE_P (binfo))
8783 /* Resolve the name. */
8784 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
8785 return error_mark_node;
8787 binfo = TYPE_BINFO (binfo);
8790 gcc_assert (binfo != NULL_TREE);
8792 class_type = BINFO_TYPE (binfo);
8794 /* Handle the special case where INSTANCE is NULL_TREE. */
8795 if (name == complete_ctor_identifier && !instance)
8796 instance = build_dummy_object (class_type);
8797 else
8799 if (IDENTIFIER_DTOR_P (name))
8800 gcc_assert (args == NULL || vec_safe_is_empty (*args));
8802 /* Convert to the base class, if necessary. */
8803 if (!same_type_ignoring_top_level_qualifiers_p
8804 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
8806 if (IDENTIFIER_CDTOR_P (name))
8807 /* For constructors and destructors, either the base is
8808 non-virtual, or it is virtual but we are doing the
8809 conversion from a constructor or destructor for the
8810 complete object. In either case, we can convert
8811 statically. */
8812 instance = convert_to_base_statically (instance, binfo);
8813 else
8815 /* However, for assignment operators, we must convert
8816 dynamically if the base is virtual. */
8817 gcc_checking_assert (name == assign_op_identifier);
8818 instance = build_base_path (PLUS_EXPR, instance,
8819 binfo, /*nonnull=*/1, complain);
8824 gcc_assert (instance != NULL_TREE);
8826 /* In C++17, "If the initializer expression is a prvalue and the
8827 cv-unqualified version of the source type is the same class as the class
8828 of the destination, the initializer expression is used to initialize the
8829 destination object." Handle that here to avoid doing overload
8830 resolution. */
8831 if (cxx_dialect >= cxx17
8832 && args && vec_safe_length (*args) == 1
8833 && name == complete_ctor_identifier)
8835 tree arg = (**args)[0];
8837 /* FIXME P0135 doesn't say how to handle direct initialization from a
8838 type with a suitable conversion operator. Let's handle it like
8839 copy-initialization, but allowing explict conversions. */
8840 tsubst_flags_t sub_complain = tf_warning;
8841 if (!is_dummy_object (instance))
8842 /* If we're using this to initialize a non-temporary object, don't
8843 require the destructor to be accessible. */
8844 sub_complain |= tf_no_cleanup;
8845 if (!reference_related_p (class_type, TREE_TYPE (arg)))
8846 arg = perform_implicit_conversion_flags (class_type, arg,
8847 sub_complain,
8848 flags);
8849 if ((TREE_CODE (arg) == TARGET_EXPR
8850 || TREE_CODE (arg) == CONSTRUCTOR)
8851 && (same_type_ignoring_top_level_qualifiers_p
8852 (class_type, TREE_TYPE (arg))))
8854 if (is_dummy_object (instance))
8855 return arg;
8856 if ((complain & tf_error)
8857 && (flags & LOOKUP_DELEGATING_CONS))
8858 check_self_delegation (arg);
8859 /* Avoid change of behavior on Wunused-var-2.C. */
8860 instance = mark_lvalue_use (instance);
8861 return build2 (INIT_EXPR, class_type, instance, arg);
8865 fns = lookup_fnfields (binfo, name, 1);
8867 /* When making a call to a constructor or destructor for a subobject
8868 that uses virtual base classes, pass down a pointer to a VTT for
8869 the subobject. */
8870 if ((name == base_ctor_identifier
8871 || name == base_dtor_identifier)
8872 && CLASSTYPE_VBASECLASSES (class_type))
8874 tree vtt;
8875 tree sub_vtt;
8877 /* If the current function is a complete object constructor
8878 or destructor, then we fetch the VTT directly.
8879 Otherwise, we look it up using the VTT we were given. */
8880 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
8881 vtt = decay_conversion (vtt, complain);
8882 if (vtt == error_mark_node)
8883 return error_mark_node;
8884 vtt = build_if_in_charge (vtt, current_vtt_parm);
8885 if (BINFO_SUBVTT_INDEX (binfo))
8886 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
8887 else
8888 sub_vtt = vtt;
8890 if (args == NULL)
8892 allocated = make_tree_vector ();
8893 args = &allocated;
8896 vec_safe_insert (*args, 0, sub_vtt);
8899 ret = build_new_method_call (instance, fns, args,
8900 TYPE_BINFO (BINFO_TYPE (binfo)),
8901 flags, /*fn=*/NULL,
8902 complain);
8904 if (allocated != NULL)
8905 release_tree_vector (allocated);
8907 if ((complain & tf_error)
8908 && (flags & LOOKUP_DELEGATING_CONS)
8909 && name == complete_ctor_identifier)
8910 check_self_delegation (ret);
8912 return ret;
8915 /* Return the NAME, as a C string. The NAME indicates a function that
8916 is a member of TYPE. *FREE_P is set to true if the caller must
8917 free the memory returned.
8919 Rather than go through all of this, we should simply set the names
8920 of constructors and destructors appropriately, and dispense with
8921 ctor_identifier, dtor_identifier, etc. */
8923 static char *
8924 name_as_c_string (tree name, tree type, bool *free_p)
8926 const char *pretty_name;
8928 /* Assume that we will not allocate memory. */
8929 *free_p = false;
8930 /* Constructors and destructors are special. */
8931 if (IDENTIFIER_CDTOR_P (name))
8933 pretty_name
8934 = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
8935 /* For a destructor, add the '~'. */
8936 if (IDENTIFIER_DTOR_P (name))
8938 pretty_name = concat ("~", pretty_name, NULL);
8939 /* Remember that we need to free the memory allocated. */
8940 *free_p = true;
8943 else if (IDENTIFIER_CONV_OP_P (name))
8945 pretty_name = concat ("operator ",
8946 type_as_string_translate (TREE_TYPE (name),
8947 TFF_PLAIN_IDENTIFIER),
8948 NULL);
8949 /* Remember that we need to free the memory allocated. */
8950 *free_p = true;
8952 else
8953 pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
8955 return CONST_CAST (char *, pretty_name);
8958 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
8959 be set, upon return, to the function called. ARGS may be NULL.
8960 This may change ARGS. */
8962 static tree
8963 build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
8964 tree conversion_path, int flags,
8965 tree *fn_p, tsubst_flags_t complain)
8967 struct z_candidate *candidates = 0, *cand;
8968 tree explicit_targs = NULL_TREE;
8969 tree basetype = NULL_TREE;
8970 tree access_binfo, binfo;
8971 tree optype;
8972 tree first_mem_arg = NULL_TREE;
8973 tree name;
8974 bool skip_first_for_error;
8975 vec<tree, va_gc> *user_args;
8976 tree call;
8977 tree fn;
8978 int template_only = 0;
8979 bool any_viable_p;
8980 tree orig_instance;
8981 tree orig_fns;
8982 vec<tree, va_gc> *orig_args = NULL;
8983 void *p;
8985 gcc_assert (instance != NULL_TREE);
8987 /* We don't know what function we're going to call, yet. */
8988 if (fn_p)
8989 *fn_p = NULL_TREE;
8991 if (error_operand_p (instance)
8992 || !fns || error_operand_p (fns))
8993 return error_mark_node;
8995 if (!BASELINK_P (fns))
8997 if (complain & tf_error)
8998 error ("call to non-function %qD", fns);
8999 return error_mark_node;
9002 orig_instance = instance;
9003 orig_fns = fns;
9005 /* Dismantle the baselink to collect all the information we need. */
9006 if (!conversion_path)
9007 conversion_path = BASELINK_BINFO (fns);
9008 access_binfo = BASELINK_ACCESS_BINFO (fns);
9009 binfo = BASELINK_BINFO (fns);
9010 optype = BASELINK_OPTYPE (fns);
9011 fns = BASELINK_FUNCTIONS (fns);
9012 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
9014 explicit_targs = TREE_OPERAND (fns, 1);
9015 fns = TREE_OPERAND (fns, 0);
9016 template_only = 1;
9018 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
9019 || TREE_CODE (fns) == TEMPLATE_DECL
9020 || TREE_CODE (fns) == OVERLOAD);
9021 fn = OVL_FIRST (fns);
9022 name = DECL_NAME (fn);
9024 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
9025 gcc_assert (CLASS_TYPE_P (basetype));
9027 if (processing_template_decl)
9029 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
9030 instance = build_non_dependent_expr (instance);
9031 if (args != NULL)
9032 make_args_non_dependent (*args);
9035 user_args = args == NULL ? NULL : *args;
9036 /* Under DR 147 A::A() is an invalid constructor call,
9037 not a functional cast. */
9038 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
9040 if (! (complain & tf_error))
9041 return error_mark_node;
9043 basetype = DECL_CONTEXT (fn);
9044 name = constructor_name (basetype);
9045 if (permerror (input_location,
9046 "cannot call constructor %<%T::%D%> directly",
9047 basetype, name))
9048 inform (input_location, "for a function-style cast, remove the "
9049 "redundant %<::%D%>", name);
9050 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
9051 complain);
9052 return call;
9055 /* Process the argument list. */
9056 if (args != NULL && *args != NULL)
9058 *args = resolve_args (*args, complain);
9059 if (*args == NULL)
9060 return error_mark_node;
9063 /* Consider the object argument to be used even if we end up selecting a
9064 static member function. */
9065 instance = mark_type_use (instance);
9067 /* Figure out whether to skip the first argument for the error
9068 message we will display to users if an error occurs. We don't
9069 want to display any compiler-generated arguments. The "this"
9070 pointer hasn't been added yet. However, we must remove the VTT
9071 pointer if this is a call to a base-class constructor or
9072 destructor. */
9073 skip_first_for_error = false;
9074 if (IDENTIFIER_CDTOR_P (name))
9076 /* Callers should explicitly indicate whether they want to ctor
9077 the complete object or just the part without virtual bases. */
9078 gcc_assert (name != ctor_identifier);
9080 /* Remove the VTT pointer, if present. */
9081 if ((name == base_ctor_identifier || name == base_dtor_identifier)
9082 && CLASSTYPE_VBASECLASSES (basetype))
9083 skip_first_for_error = true;
9085 /* It's OK to call destructors and constructors on cv-qualified
9086 objects. Therefore, convert the INSTANCE to the unqualified
9087 type, if necessary. */
9088 if (!same_type_p (basetype, TREE_TYPE (instance)))
9090 instance = build_this (instance);
9091 instance = build_nop (build_pointer_type (basetype), instance);
9092 instance = build_fold_indirect_ref (instance);
9095 else
9096 gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
9098 /* For the overload resolution we need to find the actual `this`
9099 that would be captured if the call turns out to be to a
9100 non-static member function. Do not actually capture it at this
9101 point. */
9102 if (DECL_CONSTRUCTOR_P (fn))
9103 /* Constructors don't use the enclosing 'this'. */
9104 first_mem_arg = instance;
9105 else
9106 first_mem_arg = maybe_resolve_dummy (instance, false);
9108 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9109 p = conversion_obstack_alloc (0);
9111 /* The number of arguments artificial parms in ARGS; we subtract one because
9112 there's no 'this' in ARGS. */
9113 unsigned skip = num_artificial_parms_for (fn) - 1;
9115 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
9116 initializer, not T({ }). */
9117 if (DECL_CONSTRUCTOR_P (fn)
9118 && vec_safe_length (user_args) > skip
9119 && DIRECT_LIST_INIT_P ((*user_args)[skip]))
9121 tree init_list = (*user_args)[skip];
9122 tree init = NULL_TREE;
9124 gcc_assert (user_args->length () == skip + 1
9125 && !(flags & LOOKUP_ONLYCONVERTING));
9127 /* If the initializer list has no elements and T is a class type with
9128 a default constructor, the object is value-initialized. Handle
9129 this here so we don't need to handle it wherever we use
9130 build_special_member_call. */
9131 if (CONSTRUCTOR_NELTS (init_list) == 0
9132 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
9133 /* For a user-provided default constructor, use the normal
9134 mechanisms so that protected access works. */
9135 && type_has_non_user_provided_default_constructor (basetype)
9136 && !processing_template_decl)
9137 init = build_value_init (basetype, complain);
9139 /* If BASETYPE is an aggregate, we need to do aggregate
9140 initialization. */
9141 else if (CP_AGGREGATE_TYPE_P (basetype))
9143 init = reshape_init (basetype, init_list, complain);
9144 init = digest_init (basetype, init, complain);
9147 if (init)
9149 if (is_dummy_object (instance))
9150 return get_target_expr_sfinae (init, complain);
9151 init = build2 (INIT_EXPR, TREE_TYPE (instance), instance, init);
9152 TREE_SIDE_EFFECTS (init) = true;
9153 return init;
9156 /* Otherwise go ahead with overload resolution. */
9157 add_list_candidates (fns, first_mem_arg, user_args,
9158 basetype, explicit_targs, template_only,
9159 conversion_path, access_binfo, flags,
9160 &candidates, complain);
9162 else
9163 add_candidates (fns, first_mem_arg, user_args, optype,
9164 explicit_targs, template_only, conversion_path,
9165 access_binfo, flags, &candidates, complain);
9167 any_viable_p = false;
9168 candidates = splice_viable (candidates, false, &any_viable_p);
9170 if (!any_viable_p)
9172 if (complain & tf_error)
9174 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
9175 cxx_incomplete_type_error (instance, basetype);
9176 else if (optype)
9177 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
9178 basetype, optype, build_tree_list_vec (user_args),
9179 TREE_TYPE (instance));
9180 else
9182 tree arglist = build_tree_list_vec (user_args);
9183 tree errname = name;
9184 bool twiddle = false;
9185 if (IDENTIFIER_CDTOR_P (errname))
9187 twiddle = IDENTIFIER_DTOR_P (errname);
9188 errname = constructor_name (basetype);
9190 if (explicit_targs)
9191 errname = lookup_template_function (errname, explicit_targs);
9192 if (skip_first_for_error)
9193 arglist = TREE_CHAIN (arglist);
9194 error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
9195 basetype, &"~"[!twiddle], errname, arglist,
9196 TREE_TYPE (instance));
9198 print_z_candidates (location_of (name), candidates);
9200 call = error_mark_node;
9202 else
9204 cand = tourney (candidates, complain);
9205 if (cand == 0)
9207 char *pretty_name;
9208 bool free_p;
9209 tree arglist;
9211 if (complain & tf_error)
9213 pretty_name = name_as_c_string (name, basetype, &free_p);
9214 arglist = build_tree_list_vec (user_args);
9215 if (skip_first_for_error)
9216 arglist = TREE_CHAIN (arglist);
9217 if (!any_strictly_viable (candidates))
9218 error ("no matching function for call to %<%s(%A)%>",
9219 pretty_name, arglist);
9220 else
9221 error ("call of overloaded %<%s(%A)%> is ambiguous",
9222 pretty_name, arglist);
9223 print_z_candidates (location_of (name), candidates);
9224 if (free_p)
9225 free (pretty_name);
9227 call = error_mark_node;
9229 else
9231 fn = cand->fn;
9232 call = NULL_TREE;
9234 if (!(flags & LOOKUP_NONVIRTUAL)
9235 && DECL_PURE_VIRTUAL_P (fn)
9236 && instance == current_class_ref
9237 && (complain & tf_warning))
9239 /* This is not an error, it is runtime undefined
9240 behavior. */
9241 if (!current_function_decl)
9242 warning (0, "pure virtual %q#D called from "
9243 "non-static data member initializer", fn);
9244 else if (DECL_CONSTRUCTOR_P (current_function_decl)
9245 || DECL_DESTRUCTOR_P (current_function_decl))
9246 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
9247 ? G_("pure virtual %q#D called from constructor")
9248 : G_("pure virtual %q#D called from destructor")),
9249 fn);
9252 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
9253 && !DECL_CONSTRUCTOR_P (fn)
9254 && is_dummy_object (instance))
9256 instance = maybe_resolve_dummy (instance, true);
9257 if (instance == error_mark_node)
9258 call = error_mark_node;
9259 else if (!is_dummy_object (instance))
9261 /* We captured 'this' in the current lambda now that
9262 we know we really need it. */
9263 cand->first_arg = instance;
9265 else if (any_dependent_bases_p ())
9266 /* We can't tell until instantiation time whether we can use
9267 *this as the implicit object argument. */;
9268 else
9270 if (complain & tf_error)
9271 error ("cannot call member function %qD without object",
9272 fn);
9273 call = error_mark_node;
9277 if (call != error_mark_node)
9279 /* Optimize away vtable lookup if we know that this
9280 function can't be overridden. We need to check if
9281 the context and the type where we found fn are the same,
9282 actually FN might be defined in a different class
9283 type because of a using-declaration. In this case, we
9284 do not want to perform a non-virtual call. */
9285 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
9286 && same_type_ignoring_top_level_qualifiers_p
9287 (DECL_CONTEXT (fn), BINFO_TYPE (binfo))
9288 && resolves_to_fixed_type_p (instance, 0))
9289 flags |= LOOKUP_NONVIRTUAL;
9290 if (explicit_targs)
9291 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
9292 /* Now we know what function is being called. */
9293 if (fn_p)
9294 *fn_p = fn;
9295 /* Build the actual CALL_EXPR. */
9296 call = build_over_call (cand, flags, complain);
9297 /* In an expression of the form `a->f()' where `f' turns
9298 out to be a static member function, `a' is
9299 none-the-less evaluated. */
9300 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
9301 && !is_dummy_object (instance)
9302 && TREE_SIDE_EFFECTS (instance))
9303 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
9304 instance, call);
9305 else if (call != error_mark_node
9306 && DECL_DESTRUCTOR_P (cand->fn)
9307 && !VOID_TYPE_P (TREE_TYPE (call)))
9308 /* An explicit call of the form "x->~X()" has type
9309 "void". However, on platforms where destructors
9310 return "this" (i.e., those where
9311 targetm.cxx.cdtor_returns_this is true), such calls
9312 will appear to have a return value of pointer type
9313 to the low-level call machinery. We do not want to
9314 change the low-level machinery, since we want to be
9315 able to optimize "delete f()" on such platforms as
9316 "operator delete(~X(f()))" (rather than generating
9317 "t = f(), ~X(t), operator delete (t)"). */
9318 call = build_nop (void_type_node, call);
9323 if (processing_template_decl && call != error_mark_node)
9325 bool cast_to_void = false;
9327 if (TREE_CODE (call) == COMPOUND_EXPR)
9328 call = TREE_OPERAND (call, 1);
9329 else if (TREE_CODE (call) == NOP_EXPR)
9331 cast_to_void = true;
9332 call = TREE_OPERAND (call, 0);
9334 if (INDIRECT_REF_P (call))
9335 call = TREE_OPERAND (call, 0);
9336 call = (build_min_non_dep_call_vec
9337 (call,
9338 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
9339 orig_instance, orig_fns, NULL_TREE),
9340 orig_args));
9341 SET_EXPR_LOCATION (call, input_location);
9342 call = convert_from_reference (call);
9343 if (cast_to_void)
9344 call = build_nop (void_type_node, call);
9347 /* Free all the conversions we allocated. */
9348 obstack_free (&conversion_obstack, p);
9350 if (orig_args != NULL)
9351 release_tree_vector (orig_args);
9353 return call;
9356 /* Wrapper for above. */
9358 tree
9359 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
9360 tree conversion_path, int flags,
9361 tree *fn_p, tsubst_flags_t complain)
9363 tree ret;
9364 bool subtime = timevar_cond_start (TV_OVERLOAD);
9365 ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
9366 fn_p, complain);
9367 timevar_cond_stop (TV_OVERLOAD, subtime);
9368 return ret;
9371 /* Returns true iff standard conversion sequence ICS1 is a proper
9372 subsequence of ICS2. */
9374 static bool
9375 is_subseq (conversion *ics1, conversion *ics2)
9377 /* We can assume that a conversion of the same code
9378 between the same types indicates a subsequence since we only get
9379 here if the types we are converting from are the same. */
9381 while (ics1->kind == ck_rvalue
9382 || ics1->kind == ck_lvalue)
9383 ics1 = next_conversion (ics1);
9385 while (1)
9387 while (ics2->kind == ck_rvalue
9388 || ics2->kind == ck_lvalue)
9389 ics2 = next_conversion (ics2);
9391 if (ics2->kind == ck_user
9392 || ics2->kind == ck_ambig
9393 || ics2->kind == ck_aggr
9394 || ics2->kind == ck_list
9395 || ics2->kind == ck_identity)
9396 /* At this point, ICS1 cannot be a proper subsequence of
9397 ICS2. We can get a USER_CONV when we are comparing the
9398 second standard conversion sequence of two user conversion
9399 sequences. */
9400 return false;
9402 ics2 = next_conversion (ics2);
9404 while (ics2->kind == ck_rvalue
9405 || ics2->kind == ck_lvalue)
9406 ics2 = next_conversion (ics2);
9408 if (ics2->kind == ics1->kind
9409 && same_type_p (ics2->type, ics1->type)
9410 && (ics1->kind == ck_identity
9411 || same_type_p (next_conversion (ics2)->type,
9412 next_conversion (ics1)->type)))
9413 return true;
9417 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
9418 be any _TYPE nodes. */
9420 bool
9421 is_properly_derived_from (tree derived, tree base)
9423 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
9424 return false;
9426 /* We only allow proper derivation here. The DERIVED_FROM_P macro
9427 considers every class derived from itself. */
9428 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
9429 && DERIVED_FROM_P (base, derived));
9432 /* We build the ICS for an implicit object parameter as a pointer
9433 conversion sequence. However, such a sequence should be compared
9434 as if it were a reference conversion sequence. If ICS is the
9435 implicit conversion sequence for an implicit object parameter,
9436 modify it accordingly. */
9438 static void
9439 maybe_handle_implicit_object (conversion **ics)
9441 if ((*ics)->this_p)
9443 /* [over.match.funcs]
9445 For non-static member functions, the type of the
9446 implicit object parameter is "reference to cv X"
9447 where X is the class of which the function is a
9448 member and cv is the cv-qualification on the member
9449 function declaration. */
9450 conversion *t = *ics;
9451 tree reference_type;
9453 /* The `this' parameter is a pointer to a class type. Make the
9454 implicit conversion talk about a reference to that same class
9455 type. */
9456 reference_type = TREE_TYPE (t->type);
9457 reference_type = build_reference_type (reference_type);
9459 if (t->kind == ck_qual)
9460 t = next_conversion (t);
9461 if (t->kind == ck_ptr)
9462 t = next_conversion (t);
9463 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
9464 t = direct_reference_binding (reference_type, t);
9465 t->this_p = 1;
9466 t->rvaluedness_matches_p = 0;
9467 *ics = t;
9471 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
9472 and return the initial reference binding conversion. Otherwise,
9473 leave *ICS unchanged and return NULL. */
9475 static conversion *
9476 maybe_handle_ref_bind (conversion **ics)
9478 if ((*ics)->kind == ck_ref_bind)
9480 conversion *old_ics = *ics;
9481 *ics = next_conversion (old_ics);
9482 (*ics)->user_conv_p = old_ics->user_conv_p;
9483 return old_ics;
9486 return NULL;
9489 /* Compare two implicit conversion sequences according to the rules set out in
9490 [over.ics.rank]. Return values:
9492 1: ics1 is better than ics2
9493 -1: ics2 is better than ics1
9494 0: ics1 and ics2 are indistinguishable */
9496 static int
9497 compare_ics (conversion *ics1, conversion *ics2)
9499 tree from_type1;
9500 tree from_type2;
9501 tree to_type1;
9502 tree to_type2;
9503 tree deref_from_type1 = NULL_TREE;
9504 tree deref_from_type2 = NULL_TREE;
9505 tree deref_to_type1 = NULL_TREE;
9506 tree deref_to_type2 = NULL_TREE;
9507 conversion_rank rank1, rank2;
9509 /* REF_BINDING is nonzero if the result of the conversion sequence
9510 is a reference type. In that case REF_CONV is the reference
9511 binding conversion. */
9512 conversion *ref_conv1;
9513 conversion *ref_conv2;
9515 /* Compare badness before stripping the reference conversion. */
9516 if (ics1->bad_p > ics2->bad_p)
9517 return -1;
9518 else if (ics1->bad_p < ics2->bad_p)
9519 return 1;
9521 /* Handle implicit object parameters. */
9522 maybe_handle_implicit_object (&ics1);
9523 maybe_handle_implicit_object (&ics2);
9525 /* Handle reference parameters. */
9526 ref_conv1 = maybe_handle_ref_bind (&ics1);
9527 ref_conv2 = maybe_handle_ref_bind (&ics2);
9529 /* List-initialization sequence L1 is a better conversion sequence than
9530 list-initialization sequence L2 if L1 converts to
9531 std::initializer_list<X> for some X and L2 does not. */
9532 if (ics1->kind == ck_list && ics2->kind != ck_list)
9533 return 1;
9534 if (ics2->kind == ck_list && ics1->kind != ck_list)
9535 return -1;
9537 /* [over.ics.rank]
9539 When comparing the basic forms of implicit conversion sequences (as
9540 defined in _over.best.ics_)
9542 --a standard conversion sequence (_over.ics.scs_) is a better
9543 conversion sequence than a user-defined conversion sequence
9544 or an ellipsis conversion sequence, and
9546 --a user-defined conversion sequence (_over.ics.user_) is a
9547 better conversion sequence than an ellipsis conversion sequence
9548 (_over.ics.ellipsis_). */
9549 /* Use BAD_CONVERSION_RANK because we already checked for a badness
9550 mismatch. If both ICS are bad, we try to make a decision based on
9551 what would have happened if they'd been good. This is not an
9552 extension, we'll still give an error when we build up the call; this
9553 just helps us give a more helpful error message. */
9554 rank1 = BAD_CONVERSION_RANK (ics1);
9555 rank2 = BAD_CONVERSION_RANK (ics2);
9557 if (rank1 > rank2)
9558 return -1;
9559 else if (rank1 < rank2)
9560 return 1;
9562 if (ics1->ellipsis_p)
9563 /* Both conversions are ellipsis conversions. */
9564 return 0;
9566 /* User-defined conversion sequence U1 is a better conversion sequence
9567 than another user-defined conversion sequence U2 if they contain the
9568 same user-defined conversion operator or constructor and if the sec-
9569 ond standard conversion sequence of U1 is better than the second
9570 standard conversion sequence of U2. */
9572 /* Handle list-conversion with the same code even though it isn't always
9573 ranked as a user-defined conversion and it doesn't have a second
9574 standard conversion sequence; it will still have the desired effect.
9575 Specifically, we need to do the reference binding comparison at the
9576 end of this function. */
9578 if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
9580 conversion *t1;
9581 conversion *t2;
9583 for (t1 = ics1; t1->kind != ck_user; t1 = next_conversion (t1))
9584 if (t1->kind == ck_ambig || t1->kind == ck_aggr
9585 || t1->kind == ck_list)
9586 break;
9587 for (t2 = ics2; t2->kind != ck_user; t2 = next_conversion (t2))
9588 if (t2->kind == ck_ambig || t2->kind == ck_aggr
9589 || t2->kind == ck_list)
9590 break;
9592 if (t1->kind != t2->kind)
9593 return 0;
9594 else if (t1->kind == ck_user)
9596 tree f1 = t1->cand ? t1->cand->fn : t1->type;
9597 tree f2 = t2->cand ? t2->cand->fn : t2->type;
9598 if (f1 != f2)
9599 return 0;
9601 else
9603 /* For ambiguous or aggregate conversions, use the target type as
9604 a proxy for the conversion function. */
9605 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
9606 return 0;
9609 /* We can just fall through here, after setting up
9610 FROM_TYPE1 and FROM_TYPE2. */
9611 from_type1 = t1->type;
9612 from_type2 = t2->type;
9614 else
9616 conversion *t1;
9617 conversion *t2;
9619 /* We're dealing with two standard conversion sequences.
9621 [over.ics.rank]
9623 Standard conversion sequence S1 is a better conversion
9624 sequence than standard conversion sequence S2 if
9626 --S1 is a proper subsequence of S2 (comparing the conversion
9627 sequences in the canonical form defined by _over.ics.scs_,
9628 excluding any Lvalue Transformation; the identity
9629 conversion sequence is considered to be a subsequence of
9630 any non-identity conversion sequence */
9632 t1 = ics1;
9633 while (t1->kind != ck_identity)
9634 t1 = next_conversion (t1);
9635 from_type1 = t1->type;
9637 t2 = ics2;
9638 while (t2->kind != ck_identity)
9639 t2 = next_conversion (t2);
9640 from_type2 = t2->type;
9643 /* One sequence can only be a subsequence of the other if they start with
9644 the same type. They can start with different types when comparing the
9645 second standard conversion sequence in two user-defined conversion
9646 sequences. */
9647 if (same_type_p (from_type1, from_type2))
9649 if (is_subseq (ics1, ics2))
9650 return 1;
9651 if (is_subseq (ics2, ics1))
9652 return -1;
9655 /* [over.ics.rank]
9657 Or, if not that,
9659 --the rank of S1 is better than the rank of S2 (by the rules
9660 defined below):
9662 Standard conversion sequences are ordered by their ranks: an Exact
9663 Match is a better conversion than a Promotion, which is a better
9664 conversion than a Conversion.
9666 Two conversion sequences with the same rank are indistinguishable
9667 unless one of the following rules applies:
9669 --A conversion that does not a convert a pointer, pointer to member,
9670 or std::nullptr_t to bool is better than one that does.
9672 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
9673 so that we do not have to check it explicitly. */
9674 if (ics1->rank < ics2->rank)
9675 return 1;
9676 else if (ics2->rank < ics1->rank)
9677 return -1;
9679 to_type1 = ics1->type;
9680 to_type2 = ics2->type;
9682 /* A conversion from scalar arithmetic type to complex is worse than a
9683 conversion between scalar arithmetic types. */
9684 if (same_type_p (from_type1, from_type2)
9685 && ARITHMETIC_TYPE_P (from_type1)
9686 && ARITHMETIC_TYPE_P (to_type1)
9687 && ARITHMETIC_TYPE_P (to_type2)
9688 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
9689 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
9691 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
9692 return -1;
9693 else
9694 return 1;
9697 if (TYPE_PTR_P (from_type1)
9698 && TYPE_PTR_P (from_type2)
9699 && TYPE_PTR_P (to_type1)
9700 && TYPE_PTR_P (to_type2))
9702 deref_from_type1 = TREE_TYPE (from_type1);
9703 deref_from_type2 = TREE_TYPE (from_type2);
9704 deref_to_type1 = TREE_TYPE (to_type1);
9705 deref_to_type2 = TREE_TYPE (to_type2);
9707 /* The rules for pointers to members A::* are just like the rules
9708 for pointers A*, except opposite: if B is derived from A then
9709 A::* converts to B::*, not vice versa. For that reason, we
9710 switch the from_ and to_ variables here. */
9711 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
9712 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
9713 || (TYPE_PTRMEMFUNC_P (from_type1)
9714 && TYPE_PTRMEMFUNC_P (from_type2)
9715 && TYPE_PTRMEMFUNC_P (to_type1)
9716 && TYPE_PTRMEMFUNC_P (to_type2)))
9718 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
9719 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
9720 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
9721 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
9724 if (deref_from_type1 != NULL_TREE
9725 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
9726 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
9728 /* This was one of the pointer or pointer-like conversions.
9730 [over.ics.rank]
9732 --If class B is derived directly or indirectly from class A,
9733 conversion of B* to A* is better than conversion of B* to
9734 void*, and conversion of A* to void* is better than
9735 conversion of B* to void*. */
9736 if (VOID_TYPE_P (deref_to_type1)
9737 && VOID_TYPE_P (deref_to_type2))
9739 if (is_properly_derived_from (deref_from_type1,
9740 deref_from_type2))
9741 return -1;
9742 else if (is_properly_derived_from (deref_from_type2,
9743 deref_from_type1))
9744 return 1;
9746 else if (VOID_TYPE_P (deref_to_type1)
9747 || VOID_TYPE_P (deref_to_type2))
9749 if (same_type_p (deref_from_type1, deref_from_type2))
9751 if (VOID_TYPE_P (deref_to_type2))
9753 if (is_properly_derived_from (deref_from_type1,
9754 deref_to_type1))
9755 return 1;
9757 /* We know that DEREF_TO_TYPE1 is `void' here. */
9758 else if (is_properly_derived_from (deref_from_type1,
9759 deref_to_type2))
9760 return -1;
9763 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
9764 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
9766 /* [over.ics.rank]
9768 --If class B is derived directly or indirectly from class A
9769 and class C is derived directly or indirectly from B,
9771 --conversion of C* to B* is better than conversion of C* to
9774 --conversion of B* to A* is better than conversion of C* to
9775 A* */
9776 if (same_type_p (deref_from_type1, deref_from_type2))
9778 if (is_properly_derived_from (deref_to_type1,
9779 deref_to_type2))
9780 return 1;
9781 else if (is_properly_derived_from (deref_to_type2,
9782 deref_to_type1))
9783 return -1;
9785 else if (same_type_p (deref_to_type1, deref_to_type2))
9787 if (is_properly_derived_from (deref_from_type2,
9788 deref_from_type1))
9789 return 1;
9790 else if (is_properly_derived_from (deref_from_type1,
9791 deref_from_type2))
9792 return -1;
9796 else if (CLASS_TYPE_P (non_reference (from_type1))
9797 && same_type_p (from_type1, from_type2))
9799 tree from = non_reference (from_type1);
9801 /* [over.ics.rank]
9803 --binding of an expression of type C to a reference of type
9804 B& is better than binding an expression of type C to a
9805 reference of type A&
9807 --conversion of C to B is better than conversion of C to A, */
9808 if (is_properly_derived_from (from, to_type1)
9809 && is_properly_derived_from (from, to_type2))
9811 if (is_properly_derived_from (to_type1, to_type2))
9812 return 1;
9813 else if (is_properly_derived_from (to_type2, to_type1))
9814 return -1;
9817 else if (CLASS_TYPE_P (non_reference (to_type1))
9818 && same_type_p (to_type1, to_type2))
9820 tree to = non_reference (to_type1);
9822 /* [over.ics.rank]
9824 --binding of an expression of type B to a reference of type
9825 A& is better than binding an expression of type C to a
9826 reference of type A&,
9828 --conversion of B to A is better than conversion of C to A */
9829 if (is_properly_derived_from (from_type1, to)
9830 && is_properly_derived_from (from_type2, to))
9832 if (is_properly_derived_from (from_type2, from_type1))
9833 return 1;
9834 else if (is_properly_derived_from (from_type1, from_type2))
9835 return -1;
9839 /* [over.ics.rank]
9841 --S1 and S2 differ only in their qualification conversion and yield
9842 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
9843 qualification signature of type T1 is a proper subset of the cv-
9844 qualification signature of type T2 */
9845 if (ics1->kind == ck_qual
9846 && ics2->kind == ck_qual
9847 && same_type_p (from_type1, from_type2))
9849 int result = comp_cv_qual_signature (to_type1, to_type2);
9850 if (result != 0)
9851 return result;
9854 /* [over.ics.rank]
9856 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
9857 to an implicit object parameter of a non-static member function
9858 declared without a ref-qualifier, and either S1 binds an lvalue
9859 reference to an lvalue and S2 binds an rvalue reference or S1 binds an
9860 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
9861 draft standard, 13.3.3.2)
9863 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
9864 types to which the references refer are the same type except for
9865 top-level cv-qualifiers, and the type to which the reference
9866 initialized by S2 refers is more cv-qualified than the type to
9867 which the reference initialized by S1 refers.
9869 DR 1328 [over.match.best]: the context is an initialization by
9870 conversion function for direct reference binding (13.3.1.6) of a
9871 reference to function type, the return type of F1 is the same kind of
9872 reference (i.e. lvalue or rvalue) as the reference being initialized,
9873 and the return type of F2 is not. */
9875 if (ref_conv1 && ref_conv2)
9877 if (!ref_conv1->this_p && !ref_conv2->this_p
9878 && (ref_conv1->rvaluedness_matches_p
9879 != ref_conv2->rvaluedness_matches_p)
9880 && (same_type_p (ref_conv1->type, ref_conv2->type)
9881 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
9882 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
9884 if (ref_conv1->bad_p
9885 && !same_type_p (TREE_TYPE (ref_conv1->type),
9886 TREE_TYPE (ref_conv2->type)))
9887 /* Don't prefer a bad conversion that drops cv-quals to a bad
9888 conversion with the wrong rvalueness. */
9889 return 0;
9890 return (ref_conv1->rvaluedness_matches_p
9891 - ref_conv2->rvaluedness_matches_p);
9894 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
9896 int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
9897 int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
9898 if (ref_conv1->bad_p)
9900 /* Prefer the one that drops fewer cv-quals. */
9901 tree ftype = next_conversion (ref_conv1)->type;
9902 int fquals = cp_type_quals (ftype);
9903 q1 ^= fquals;
9904 q2 ^= fquals;
9906 return comp_cv_qualification (q2, q1);
9910 /* Neither conversion sequence is better than the other. */
9911 return 0;
9914 /* The source type for this standard conversion sequence. */
9916 static tree
9917 source_type (conversion *t)
9919 for (;; t = next_conversion (t))
9921 if (t->kind == ck_user
9922 || t->kind == ck_ambig
9923 || t->kind == ck_identity)
9924 return t->type;
9926 gcc_unreachable ();
9929 /* Note a warning about preferring WINNER to LOSER. We do this by storing
9930 a pointer to LOSER and re-running joust to produce the warning if WINNER
9931 is actually used. */
9933 static void
9934 add_warning (struct z_candidate *winner, struct z_candidate *loser)
9936 candidate_warning *cw = (candidate_warning *)
9937 conversion_obstack_alloc (sizeof (candidate_warning));
9938 cw->loser = loser;
9939 cw->next = winner->warnings;
9940 winner->warnings = cw;
9943 /* Compare two candidates for overloading as described in
9944 [over.match.best]. Return values:
9946 1: cand1 is better than cand2
9947 -1: cand2 is better than cand1
9948 0: cand1 and cand2 are indistinguishable */
9950 static int
9951 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
9952 tsubst_flags_t complain)
9954 int winner = 0;
9955 int off1 = 0, off2 = 0;
9956 size_t i;
9957 size_t len;
9959 /* Candidates that involve bad conversions are always worse than those
9960 that don't. */
9961 if (cand1->viable > cand2->viable)
9962 return 1;
9963 if (cand1->viable < cand2->viable)
9964 return -1;
9966 /* If we have two pseudo-candidates for conversions to the same type,
9967 or two candidates for the same function, arbitrarily pick one. */
9968 if (cand1->fn == cand2->fn
9969 && (IS_TYPE_OR_DECL_P (cand1->fn)))
9970 return 1;
9972 /* Prefer a non-deleted function over an implicitly deleted move
9973 constructor or assignment operator. This differs slightly from the
9974 wording for issue 1402 (which says the move op is ignored by overload
9975 resolution), but this way produces better error messages. */
9976 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
9977 && TREE_CODE (cand2->fn) == FUNCTION_DECL
9978 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
9980 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
9981 && move_fn_p (cand1->fn))
9982 return -1;
9983 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
9984 && move_fn_p (cand2->fn))
9985 return 1;
9988 /* a viable function F1
9989 is defined to be a better function than another viable function F2 if
9990 for all arguments i, ICSi(F1) is not a worse conversion sequence than
9991 ICSi(F2), and then */
9993 /* for some argument j, ICSj(F1) is a better conversion sequence than
9994 ICSj(F2) */
9996 /* For comparing static and non-static member functions, we ignore
9997 the implicit object parameter of the non-static function. The
9998 standard says to pretend that the static function has an object
9999 parm, but that won't work with operator overloading. */
10000 len = cand1->num_convs;
10001 if (len != cand2->num_convs)
10003 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
10004 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
10006 if (DECL_CONSTRUCTOR_P (cand1->fn)
10007 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
10008 /* We're comparing a near-match list constructor and a near-match
10009 non-list constructor. Just treat them as unordered. */
10010 return 0;
10012 gcc_assert (static_1 != static_2);
10014 if (static_1)
10015 off2 = 1;
10016 else
10018 off1 = 1;
10019 --len;
10023 for (i = 0; i < len; ++i)
10025 conversion *t1 = cand1->convs[i + off1];
10026 conversion *t2 = cand2->convs[i + off2];
10027 int comp = compare_ics (t1, t2);
10029 if (comp != 0)
10031 if ((complain & tf_warning)
10032 && warn_sign_promo
10033 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
10034 == cr_std + cr_promotion)
10035 && t1->kind == ck_std
10036 && t2->kind == ck_std
10037 && TREE_CODE (t1->type) == INTEGER_TYPE
10038 && TREE_CODE (t2->type) == INTEGER_TYPE
10039 && (TYPE_PRECISION (t1->type)
10040 == TYPE_PRECISION (t2->type))
10041 && (TYPE_UNSIGNED (next_conversion (t1)->type)
10042 || (TREE_CODE (next_conversion (t1)->type)
10043 == ENUMERAL_TYPE)))
10045 tree type = next_conversion (t1)->type;
10046 tree type1, type2;
10047 struct z_candidate *w, *l;
10048 if (comp > 0)
10049 type1 = t1->type, type2 = t2->type,
10050 w = cand1, l = cand2;
10051 else
10052 type1 = t2->type, type2 = t1->type,
10053 w = cand2, l = cand1;
10055 if (warn)
10057 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
10058 type, type1, type2);
10059 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
10061 else
10062 add_warning (w, l);
10065 if (winner && comp != winner)
10067 winner = 0;
10068 goto tweak;
10070 winner = comp;
10074 /* warn about confusing overload resolution for user-defined conversions,
10075 either between a constructor and a conversion op, or between two
10076 conversion ops. */
10077 if ((complain & tf_warning)
10078 && winner && warn_conversion && cand1->second_conv
10079 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
10080 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
10082 struct z_candidate *w, *l;
10083 bool give_warning = false;
10085 if (winner == 1)
10086 w = cand1, l = cand2;
10087 else
10088 w = cand2, l = cand1;
10090 /* We don't want to complain about `X::operator T1 ()'
10091 beating `X::operator T2 () const', when T2 is a no less
10092 cv-qualified version of T1. */
10093 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
10094 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
10096 tree t = TREE_TYPE (TREE_TYPE (l->fn));
10097 tree f = TREE_TYPE (TREE_TYPE (w->fn));
10099 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
10101 t = TREE_TYPE (t);
10102 f = TREE_TYPE (f);
10104 if (!comp_ptr_ttypes (t, f))
10105 give_warning = true;
10107 else
10108 give_warning = true;
10110 if (!give_warning)
10111 /*NOP*/;
10112 else if (warn)
10114 tree source = source_type (w->convs[0]);
10115 if (! DECL_CONSTRUCTOR_P (w->fn))
10116 source = TREE_TYPE (source);
10117 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
10118 && warning (OPT_Wconversion, " for conversion from %qH to %qI",
10119 source, w->second_conv->type))
10121 inform (input_location, " because conversion sequence for the argument is better");
10124 else
10125 add_warning (w, l);
10128 if (winner)
10129 return winner;
10131 /* DR 495 moved this tiebreaker above the template ones. */
10132 /* or, if not that,
10133 the context is an initialization by user-defined conversion (see
10134 _dcl.init_ and _over.match.user_) and the standard conversion
10135 sequence from the return type of F1 to the destination type (i.e.,
10136 the type of the entity being initialized) is a better conversion
10137 sequence than the standard conversion sequence from the return type
10138 of F2 to the destination type. */
10140 if (cand1->second_conv)
10142 winner = compare_ics (cand1->second_conv, cand2->second_conv);
10143 if (winner)
10144 return winner;
10147 /* or, if not that,
10148 F1 is a non-template function and F2 is a template function
10149 specialization. */
10151 if (!cand1->template_decl && cand2->template_decl)
10152 return 1;
10153 else if (cand1->template_decl && !cand2->template_decl)
10154 return -1;
10156 /* or, if not that,
10157 F1 and F2 are template functions and the function template for F1 is
10158 more specialized than the template for F2 according to the partial
10159 ordering rules. */
10161 if (cand1->template_decl && cand2->template_decl)
10163 winner = more_specialized_fn
10164 (TI_TEMPLATE (cand1->template_decl),
10165 TI_TEMPLATE (cand2->template_decl),
10166 /* [temp.func.order]: The presence of unused ellipsis and default
10167 arguments has no effect on the partial ordering of function
10168 templates. add_function_candidate() will not have
10169 counted the "this" argument for constructors. */
10170 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
10171 if (winner)
10172 return winner;
10175 // C++ Concepts
10176 // or, if not that, F1 is more constrained than F2.
10177 if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn))
10179 winner = more_constrained (cand1->fn, cand2->fn);
10180 if (winner)
10181 return winner;
10184 /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
10185 if (deduction_guide_p (cand1->fn))
10187 gcc_assert (deduction_guide_p (cand2->fn));
10188 /* We distinguish between candidates from an explicit deduction guide and
10189 candidates built from a constructor based on DECL_ARTIFICIAL. */
10190 int art1 = DECL_ARTIFICIAL (cand1->fn);
10191 int art2 = DECL_ARTIFICIAL (cand2->fn);
10192 if (art1 != art2)
10193 return art2 - art1;
10195 if (art1)
10197 /* Prefer the special copy guide over a declared copy/move
10198 constructor. */
10199 if (copy_guide_p (cand1->fn))
10200 return 1;
10201 if (copy_guide_p (cand2->fn))
10202 return -1;
10204 /* Prefer a candidate generated from a non-template constructor. */
10205 int tg1 = template_guide_p (cand1->fn);
10206 int tg2 = template_guide_p (cand2->fn);
10207 if (tg1 != tg2)
10208 return tg2 - tg1;
10212 /* F1 is a member of a class D, F2 is a member of a base class B of D, and
10213 for all arguments the corresponding parameters of F1 and F2 have the same
10214 type (CWG 2273/2277). */
10215 if (DECL_P (cand1->fn) && DECL_CLASS_SCOPE_P (cand1->fn)
10216 && !DECL_CONV_FN_P (cand1->fn)
10217 && DECL_P (cand2->fn) && DECL_CLASS_SCOPE_P (cand2->fn)
10218 && !DECL_CONV_FN_P (cand2->fn))
10220 tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
10221 tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
10223 bool used1 = false;
10224 bool used2 = false;
10225 if (base1 == base2)
10226 /* No difference. */;
10227 else if (DERIVED_FROM_P (base1, base2))
10228 used1 = true;
10229 else if (DERIVED_FROM_P (base2, base1))
10230 used2 = true;
10232 if (int diff = used2 - used1)
10234 for (i = 0; i < len; ++i)
10236 conversion *t1 = cand1->convs[i + off1];
10237 conversion *t2 = cand2->convs[i + off2];
10238 if (!same_type_p (t1->type, t2->type))
10239 break;
10241 if (i == len)
10242 return diff;
10246 /* Check whether we can discard a builtin candidate, either because we
10247 have two identical ones or matching builtin and non-builtin candidates.
10249 (Pedantically in the latter case the builtin which matched the user
10250 function should not be added to the overload set, but we spot it here.
10252 [over.match.oper]
10253 ... the builtin candidates include ...
10254 - do not have the same parameter type list as any non-template
10255 non-member candidate. */
10257 if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
10259 for (i = 0; i < len; ++i)
10260 if (!same_type_p (cand1->convs[i]->type,
10261 cand2->convs[i]->type))
10262 break;
10263 if (i == cand1->num_convs)
10265 if (cand1->fn == cand2->fn)
10266 /* Two built-in candidates; arbitrarily pick one. */
10267 return 1;
10268 else if (identifier_p (cand1->fn))
10269 /* cand1 is built-in; prefer cand2. */
10270 return -1;
10271 else
10272 /* cand2 is built-in; prefer cand1. */
10273 return 1;
10277 /* For candidates of a multi-versioned function, make the version with
10278 the highest priority win. This version will be checked for dispatching
10279 first. If this version can be inlined into the caller, the front-end
10280 will simply make a direct call to this function. */
10282 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
10283 && DECL_FUNCTION_VERSIONED (cand1->fn)
10284 && TREE_CODE (cand2->fn) == FUNCTION_DECL
10285 && DECL_FUNCTION_VERSIONED (cand2->fn))
10287 tree f1 = TREE_TYPE (cand1->fn);
10288 tree f2 = TREE_TYPE (cand2->fn);
10289 tree p1 = TYPE_ARG_TYPES (f1);
10290 tree p2 = TYPE_ARG_TYPES (f2);
10292 /* Check if cand1->fn and cand2->fn are versions of the same function. It
10293 is possible that cand1->fn and cand2->fn are function versions but of
10294 different functions. Check types to see if they are versions of the same
10295 function. */
10296 if (compparms (p1, p2)
10297 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
10299 /* Always make the version with the higher priority, more
10300 specialized, win. */
10301 gcc_assert (targetm.compare_version_priority);
10302 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
10303 return 1;
10304 else
10305 return -1;
10309 /* If the two function declarations represent the same function (this can
10310 happen with declarations in multiple scopes and arg-dependent lookup),
10311 arbitrarily choose one. But first make sure the default args we're
10312 using match. */
10313 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
10314 && equal_functions (cand1->fn, cand2->fn))
10316 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
10317 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
10319 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
10321 for (i = 0; i < len; ++i)
10323 /* Don't crash if the fn is variadic. */
10324 if (!parms1)
10325 break;
10326 parms1 = TREE_CHAIN (parms1);
10327 parms2 = TREE_CHAIN (parms2);
10330 if (off1)
10331 parms1 = TREE_CHAIN (parms1);
10332 else if (off2)
10333 parms2 = TREE_CHAIN (parms2);
10335 for (; parms1; ++i)
10337 if (!cp_tree_equal (TREE_PURPOSE (parms1),
10338 TREE_PURPOSE (parms2)))
10340 if (warn)
10342 if (complain & tf_error)
10344 if (permerror (input_location,
10345 "default argument mismatch in "
10346 "overload resolution"))
10348 inform (DECL_SOURCE_LOCATION (cand1->fn),
10349 " candidate 1: %q#F", cand1->fn);
10350 inform (DECL_SOURCE_LOCATION (cand2->fn),
10351 " candidate 2: %q#F", cand2->fn);
10354 else
10355 return 0;
10357 else
10358 add_warning (cand1, cand2);
10359 break;
10361 parms1 = TREE_CHAIN (parms1);
10362 parms2 = TREE_CHAIN (parms2);
10365 return 1;
10368 tweak:
10370 /* Extension: If the worst conversion for one candidate is worse than the
10371 worst conversion for the other, take the first. */
10372 if (!pedantic && (complain & tf_warning_or_error))
10374 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
10375 struct z_candidate *w = 0, *l = 0;
10377 for (i = 0; i < len; ++i)
10379 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
10380 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
10381 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
10382 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
10384 if (rank1 < rank2)
10385 winner = 1, w = cand1, l = cand2;
10386 if (rank1 > rank2)
10387 winner = -1, w = cand2, l = cand1;
10388 if (winner)
10390 /* Don't choose a deleted function over ambiguity. */
10391 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
10392 return 0;
10393 if (warn)
10395 pedwarn (input_location, 0,
10396 "ISO C++ says that these are ambiguous, even "
10397 "though the worst conversion for the first is better than "
10398 "the worst conversion for the second:");
10399 print_z_candidate (input_location, _("candidate 1:"), w);
10400 print_z_candidate (input_location, _("candidate 2:"), l);
10402 else
10403 add_warning (w, l);
10404 return winner;
10408 gcc_assert (!winner);
10409 return 0;
10412 /* Given a list of candidates for overloading, find the best one, if any.
10413 This algorithm has a worst case of O(2n) (winner is last), and a best
10414 case of O(n/2) (totally ambiguous); much better than a sorting
10415 algorithm. */
10417 static struct z_candidate *
10418 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
10420 struct z_candidate *champ = candidates, *challenger;
10421 int fate;
10422 int champ_compared_to_predecessor = 0;
10424 /* Walk through the list once, comparing each current champ to the next
10425 candidate, knocking out a candidate or two with each comparison. */
10427 for (challenger = champ->next; challenger; )
10429 fate = joust (champ, challenger, 0, complain);
10430 if (fate == 1)
10431 challenger = challenger->next;
10432 else
10434 if (fate == 0)
10436 champ = challenger->next;
10437 if (champ == 0)
10438 return NULL;
10439 champ_compared_to_predecessor = 0;
10441 else
10443 champ = challenger;
10444 champ_compared_to_predecessor = 1;
10447 challenger = champ->next;
10451 /* Make sure the champ is better than all the candidates it hasn't yet
10452 been compared to. */
10454 for (challenger = candidates;
10455 challenger != champ
10456 && !(champ_compared_to_predecessor && challenger->next == champ);
10457 challenger = challenger->next)
10459 fate = joust (champ, challenger, 0, complain);
10460 if (fate != 1)
10461 return NULL;
10464 return champ;
10467 /* Returns nonzero if things of type FROM can be converted to TO. */
10469 bool
10470 can_convert (tree to, tree from, tsubst_flags_t complain)
10472 tree arg = NULL_TREE;
10473 /* implicit_conversion only considers user-defined conversions
10474 if it has an expression for the call argument list. */
10475 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
10476 arg = build1 (CAST_EXPR, from, NULL_TREE);
10477 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
10480 /* Returns nonzero if things of type FROM can be converted to TO with a
10481 standard conversion. */
10483 bool
10484 can_convert_standard (tree to, tree from, tsubst_flags_t complain)
10486 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
10489 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
10491 bool
10492 can_convert_arg (tree to, tree from, tree arg, int flags,
10493 tsubst_flags_t complain)
10495 conversion *t;
10496 void *p;
10497 bool ok_p;
10499 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10500 p = conversion_obstack_alloc (0);
10501 /* We want to discard any access checks done for this test,
10502 as we might not be in the appropriate access context and
10503 we'll do the check again when we actually perform the
10504 conversion. */
10505 push_deferring_access_checks (dk_deferred);
10507 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
10508 flags, complain);
10509 ok_p = (t && !t->bad_p);
10511 /* Discard the access checks now. */
10512 pop_deferring_access_checks ();
10513 /* Free all the conversions we allocated. */
10514 obstack_free (&conversion_obstack, p);
10516 return ok_p;
10519 /* Like can_convert_arg, but allows dubious conversions as well. */
10521 bool
10522 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
10523 tsubst_flags_t complain)
10525 conversion *t;
10526 void *p;
10528 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10529 p = conversion_obstack_alloc (0);
10530 /* Try to perform the conversion. */
10531 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
10532 flags, complain);
10533 /* Free all the conversions we allocated. */
10534 obstack_free (&conversion_obstack, p);
10536 return t != NULL;
10539 /* Convert EXPR to TYPE. Return the converted expression.
10541 Note that we allow bad conversions here because by the time we get to
10542 this point we are committed to doing the conversion. If we end up
10543 doing a bad conversion, convert_like will complain. */
10545 tree
10546 perform_implicit_conversion_flags (tree type, tree expr,
10547 tsubst_flags_t complain, int flags)
10549 conversion *conv;
10550 void *p;
10551 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
10553 if (error_operand_p (expr))
10554 return error_mark_node;
10556 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10557 p = conversion_obstack_alloc (0);
10559 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10560 /*c_cast_p=*/false,
10561 flags, complain);
10563 if (!conv)
10565 if (complain & tf_error)
10567 /* If expr has unknown type, then it is an overloaded function.
10568 Call instantiate_type to get good error messages. */
10569 if (TREE_TYPE (expr) == unknown_type_node)
10570 instantiate_type (type, expr, complain);
10571 else if (invalid_nonstatic_memfn_p (loc, expr, complain))
10572 /* We gave an error. */;
10573 else
10574 error_at (loc, "could not convert %qE from %qH to %qI", expr,
10575 TREE_TYPE (expr), type);
10577 expr = error_mark_node;
10579 else if (processing_template_decl && conv->kind != ck_identity)
10581 /* In a template, we are only concerned about determining the
10582 type of non-dependent expressions, so we do not have to
10583 perform the actual conversion. But for initializers, we
10584 need to be able to perform it at instantiation
10585 (or instantiate_non_dependent_expr) time. */
10586 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
10587 if (!(flags & LOOKUP_ONLYCONVERTING))
10588 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
10590 else
10591 expr = convert_like (conv, expr, complain);
10593 /* Free all the conversions we allocated. */
10594 obstack_free (&conversion_obstack, p);
10596 return expr;
10599 tree
10600 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
10602 return perform_implicit_conversion_flags (type, expr, complain,
10603 LOOKUP_IMPLICIT);
10606 /* Convert EXPR to TYPE (as a direct-initialization) if that is
10607 permitted. If the conversion is valid, the converted expression is
10608 returned. Otherwise, NULL_TREE is returned, except in the case
10609 that TYPE is a class type; in that case, an error is issued. If
10610 C_CAST_P is true, then this direct-initialization is taking
10611 place as part of a static_cast being attempted as part of a C-style
10612 cast. */
10614 tree
10615 perform_direct_initialization_if_possible (tree type,
10616 tree expr,
10617 bool c_cast_p,
10618 tsubst_flags_t complain)
10620 conversion *conv;
10621 void *p;
10623 if (type == error_mark_node || error_operand_p (expr))
10624 return error_mark_node;
10625 /* [dcl.init]
10627 If the destination type is a (possibly cv-qualified) class type:
10629 -- If the initialization is direct-initialization ...,
10630 constructors are considered. ... If no constructor applies, or
10631 the overload resolution is ambiguous, the initialization is
10632 ill-formed. */
10633 if (CLASS_TYPE_P (type))
10635 vec<tree, va_gc> *args = make_tree_vector_single (expr);
10636 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
10637 &args, type, LOOKUP_NORMAL, complain);
10638 release_tree_vector (args);
10639 return build_cplus_new (type, expr, complain);
10642 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10643 p = conversion_obstack_alloc (0);
10645 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10646 c_cast_p,
10647 LOOKUP_NORMAL, complain);
10648 if (!conv || conv->bad_p)
10649 expr = NULL_TREE;
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"