* cfghooks.c (verify_flow_info): Disable check that all probabilities
[official-gcc.git] / gcc / cp / call.c
blob13269024547b04040a4f004f8f05f696fe10752d
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_P (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), cp_operator_id (CALL_EXPR), 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 /* Since cand->fn will be a type, not a function, for a conversion
4561 function, we must be careful not to unconditionally look at
4562 DECL_NAME here. */
4563 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
4564 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
4565 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4566 else
4568 if (DECL_P (cand->fn))
4569 obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
4570 -1, complain);
4571 else
4572 obj = convert_like (cand->convs[0], obj, complain);
4573 obj = convert_from_reference (obj);
4574 result = cp_build_function_call_vec (obj, args, complain);
4578 /* Free all the conversions we allocated. */
4579 obstack_free (&conversion_obstack, p);
4581 return result;
4584 /* Wrapper for above. */
4586 tree
4587 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4589 tree ret;
4590 bool subtime = timevar_cond_start (TV_OVERLOAD);
4591 ret = build_op_call_1 (obj, args, complain);
4592 timevar_cond_stop (TV_OVERLOAD, subtime);
4593 return ret;
4596 /* Called by op_error to prepare format strings suitable for the error
4597 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4598 and a suffix (controlled by NTYPES). */
4600 static const char *
4601 op_error_string (const char *errmsg, int ntypes, bool match)
4603 const char *msg;
4605 const char *msgp = concat (match ? G_("ambiguous overload for ")
4606 : G_("no match for "), errmsg, NULL);
4608 if (ntypes == 3)
4609 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
4610 else if (ntypes == 2)
4611 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
4612 else
4613 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
4615 return msg;
4618 static void
4619 op_error (location_t loc, enum tree_code code, enum tree_code code2,
4620 tree arg1, tree arg2, tree arg3, bool match)
4622 const char *opname;
4624 if (code == MODIFY_EXPR)
4625 opname = assignment_operator_name_info[code2].name;
4626 else
4627 opname = operator_name_info[code].name;
4629 switch (code)
4631 case COND_EXPR:
4632 if (flag_diagnostics_show_caret)
4633 error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
4634 3, match),
4635 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4636 else
4637 error_at (loc, op_error_string (G_("ternary %<operator?:%> "
4638 "in %<%E ? %E : %E%>"), 3, match),
4639 arg1, arg2, arg3,
4640 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4641 break;
4643 case POSTINCREMENT_EXPR:
4644 case POSTDECREMENT_EXPR:
4645 if (flag_diagnostics_show_caret)
4646 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4647 opname, TREE_TYPE (arg1));
4648 else
4649 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4650 1, match),
4651 opname, arg1, opname, TREE_TYPE (arg1));
4652 break;
4654 case ARRAY_REF:
4655 if (flag_diagnostics_show_caret)
4656 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
4657 TREE_TYPE (arg1), TREE_TYPE (arg2));
4658 else
4659 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
4660 2, match),
4661 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
4662 break;
4664 case REALPART_EXPR:
4665 case IMAGPART_EXPR:
4666 if (flag_diagnostics_show_caret)
4667 error_at (loc, op_error_string (G_("%qs"), 1, match),
4668 opname, TREE_TYPE (arg1));
4669 else
4670 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
4671 opname, opname, arg1, TREE_TYPE (arg1));
4672 break;
4674 default:
4675 if (arg2)
4676 if (flag_diagnostics_show_caret)
4677 error_at (loc, op_error_string (G_("%<operator%s%>"), 2, match),
4678 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
4679 else
4680 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
4681 2, match),
4682 opname, arg1, opname, arg2,
4683 TREE_TYPE (arg1), TREE_TYPE (arg2));
4684 else
4685 if (flag_diagnostics_show_caret)
4686 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4687 opname, TREE_TYPE (arg1));
4688 else
4689 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
4690 1, match),
4691 opname, opname, arg1, TREE_TYPE (arg1));
4692 break;
4696 /* Return the implicit conversion sequence that could be used to
4697 convert E1 to E2 in [expr.cond]. */
4699 static conversion *
4700 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
4702 tree t1 = non_reference (TREE_TYPE (e1));
4703 tree t2 = non_reference (TREE_TYPE (e2));
4704 conversion *conv;
4705 bool good_base;
4707 /* [expr.cond]
4709 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
4710 implicitly converted (clause _conv_) to the type "lvalue reference to
4711 T2", subject to the constraint that in the conversion the
4712 reference must bind directly (_dcl.init.ref_) to an lvalue.
4714 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
4715 implicitly converted to the type "rvalue reference to T2", subject to
4716 the constraint that the reference must bind directly. */
4717 if (glvalue_p (e2))
4719 tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
4720 conv = implicit_conversion (rtype,
4723 /*c_cast_p=*/false,
4724 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
4725 |LOOKUP_ONLYCONVERTING,
4726 complain);
4727 if (conv && !conv->bad_p)
4728 return conv;
4731 /* If E2 is a prvalue or if neither of the conversions above can be done
4732 and at least one of the operands has (possibly cv-qualified) class
4733 type: */
4734 if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
4735 return NULL;
4737 /* [expr.cond]
4739 If E1 and E2 have class type, and the underlying class types are
4740 the same or one is a base class of the other: E1 can be converted
4741 to match E2 if the class of T2 is the same type as, or a base
4742 class of, the class of T1, and the cv-qualification of T2 is the
4743 same cv-qualification as, or a greater cv-qualification than, the
4744 cv-qualification of T1. If the conversion is applied, E1 is
4745 changed to an rvalue of type T2 that still refers to the original
4746 source class object (or the appropriate subobject thereof). */
4747 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
4748 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
4750 if (good_base && at_least_as_qualified_p (t2, t1))
4752 conv = build_identity_conv (t1, e1);
4753 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
4754 TYPE_MAIN_VARIANT (t2)))
4755 conv = build_conv (ck_base, t2, conv);
4756 else
4757 conv = build_conv (ck_rvalue, t2, conv);
4758 return conv;
4760 else
4761 return NULL;
4763 else
4764 /* [expr.cond]
4766 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
4767 converted to the type that expression E2 would have if E2 were
4768 converted to an rvalue (or the type it has, if E2 is an rvalue). */
4769 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
4770 LOOKUP_IMPLICIT, complain);
4773 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
4774 arguments to the conditional expression. */
4776 static tree
4777 build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
4778 tsubst_flags_t complain)
4780 tree arg2_type;
4781 tree arg3_type;
4782 tree result = NULL_TREE;
4783 tree result_type = NULL_TREE;
4784 bool is_lvalue = true;
4785 struct z_candidate *candidates = 0;
4786 struct z_candidate *cand;
4787 void *p;
4788 tree orig_arg2, orig_arg3;
4790 /* As a G++ extension, the second argument to the conditional can be
4791 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
4792 c'.) If the second operand is omitted, make sure it is
4793 calculated only once. */
4794 if (!arg2)
4796 if (complain & tf_error)
4797 pedwarn (loc, OPT_Wpedantic,
4798 "ISO C++ forbids omitting the middle term of a ?: expression");
4800 if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
4801 warn_for_omitted_condop (loc, arg1);
4803 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
4804 if (lvalue_p (arg1))
4805 arg2 = arg1 = cp_stabilize_reference (arg1);
4806 else
4807 arg2 = arg1 = save_expr (arg1);
4810 /* If something has already gone wrong, just pass that fact up the
4811 tree. */
4812 if (error_operand_p (arg1)
4813 || error_operand_p (arg2)
4814 || error_operand_p (arg3))
4815 return error_mark_node;
4817 orig_arg2 = arg2;
4818 orig_arg3 = arg3;
4820 if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
4822 tree arg1_type = TREE_TYPE (arg1);
4824 /* If arg1 is another cond_expr choosing between -1 and 0,
4825 then we can use its comparison. It may help to avoid
4826 additional comparison, produce more accurate diagnostics
4827 and enables folding. */
4828 if (TREE_CODE (arg1) == VEC_COND_EXPR
4829 && integer_minus_onep (TREE_OPERAND (arg1, 1))
4830 && integer_zerop (TREE_OPERAND (arg1, 2)))
4831 arg1 = TREE_OPERAND (arg1, 0);
4833 arg1 = force_rvalue (arg1, complain);
4834 arg2 = force_rvalue (arg2, complain);
4835 arg3 = force_rvalue (arg3, complain);
4837 /* force_rvalue can return error_mark on valid arguments. */
4838 if (error_operand_p (arg1)
4839 || error_operand_p (arg2)
4840 || error_operand_p (arg3))
4841 return error_mark_node;
4843 arg2_type = TREE_TYPE (arg2);
4844 arg3_type = TREE_TYPE (arg3);
4846 if (!VECTOR_TYPE_P (arg2_type)
4847 && !VECTOR_TYPE_P (arg3_type))
4849 /* Rely on the error messages of the scalar version. */
4850 tree scal = build_conditional_expr_1 (loc, integer_one_node,
4851 orig_arg2, orig_arg3, complain);
4852 if (scal == error_mark_node)
4853 return error_mark_node;
4854 tree stype = TREE_TYPE (scal);
4855 tree ctype = TREE_TYPE (arg1_type);
4856 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
4857 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
4859 if (complain & tf_error)
4860 error_at (loc, "inferred scalar type %qT is not an integer or "
4861 "floating point type of the same size as %qT", stype,
4862 COMPARISON_CLASS_P (arg1)
4863 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4864 : ctype);
4865 return error_mark_node;
4868 tree vtype = build_opaque_vector_type (stype,
4869 TYPE_VECTOR_SUBPARTS (arg1_type));
4870 /* We could pass complain & tf_warning to unsafe_conversion_p,
4871 but the warnings (like Wsign-conversion) have already been
4872 given by the scalar build_conditional_expr_1. We still check
4873 unsafe_conversion_p to forbid truncating long long -> float. */
4874 if (unsafe_conversion_p (loc, stype, arg2, NULL_TREE, false))
4876 if (complain & tf_error)
4877 error_at (loc, "conversion of scalar %qH to vector %qI "
4878 "involves truncation", arg2_type, vtype);
4879 return error_mark_node;
4881 if (unsafe_conversion_p (loc, stype, arg3, NULL_TREE, false))
4883 if (complain & tf_error)
4884 error_at (loc, "conversion of scalar %qH to vector %qI "
4885 "involves truncation", arg3_type, vtype);
4886 return error_mark_node;
4889 arg2 = cp_convert (stype, arg2, complain);
4890 arg2 = save_expr (arg2);
4891 arg2 = build_vector_from_val (vtype, arg2);
4892 arg2_type = vtype;
4893 arg3 = cp_convert (stype, arg3, complain);
4894 arg3 = save_expr (arg3);
4895 arg3 = build_vector_from_val (vtype, arg3);
4896 arg3_type = vtype;
4899 if (VECTOR_TYPE_P (arg2_type) != VECTOR_TYPE_P (arg3_type))
4901 enum stv_conv convert_flag =
4902 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
4903 complain & tf_error);
4905 switch (convert_flag)
4907 case stv_error:
4908 return error_mark_node;
4909 case stv_firstarg:
4911 arg2 = save_expr (arg2);
4912 arg2 = convert (TREE_TYPE (arg3_type), arg2);
4913 arg2 = build_vector_from_val (arg3_type, arg2);
4914 arg2_type = TREE_TYPE (arg2);
4915 break;
4917 case stv_secondarg:
4919 arg3 = save_expr (arg3);
4920 arg3 = convert (TREE_TYPE (arg2_type), arg3);
4921 arg3 = build_vector_from_val (arg2_type, arg3);
4922 arg3_type = TREE_TYPE (arg3);
4923 break;
4925 default:
4926 break;
4930 if (!same_type_p (arg2_type, arg3_type)
4931 || TYPE_VECTOR_SUBPARTS (arg1_type)
4932 != TYPE_VECTOR_SUBPARTS (arg2_type)
4933 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
4935 if (complain & tf_error)
4936 error_at (loc,
4937 "incompatible vector types in conditional expression: "
4938 "%qT, %qT and %qT", TREE_TYPE (arg1),
4939 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
4940 return error_mark_node;
4943 if (!COMPARISON_CLASS_P (arg1))
4945 tree cmp_type = build_same_sized_truth_vector_type (arg1_type);
4946 arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
4948 return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
4951 /* [expr.cond]
4953 The first expression is implicitly converted to bool (clause
4954 _conv_). */
4955 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
4956 LOOKUP_NORMAL);
4957 if (error_operand_p (arg1))
4958 return error_mark_node;
4960 /* [expr.cond]
4962 If either the second or the third operand has type (possibly
4963 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
4964 array-to-pointer (_conv.array_), and function-to-pointer
4965 (_conv.func_) standard conversions are performed on the second
4966 and third operands. */
4967 arg2_type = unlowered_expr_type (arg2);
4968 arg3_type = unlowered_expr_type (arg3);
4969 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
4971 /* Do the conversions. We don't these for `void' type arguments
4972 since it can't have any effect and since decay_conversion
4973 does not handle that case gracefully. */
4974 if (!VOID_TYPE_P (arg2_type))
4975 arg2 = decay_conversion (arg2, complain);
4976 if (!VOID_TYPE_P (arg3_type))
4977 arg3 = decay_conversion (arg3, complain);
4978 arg2_type = TREE_TYPE (arg2);
4979 arg3_type = TREE_TYPE (arg3);
4981 /* [expr.cond]
4983 One of the following shall hold:
4985 --The second or the third operand (but not both) is a
4986 throw-expression (_except.throw_); the result is of the
4987 type of the other and is an rvalue.
4989 --Both the second and the third operands have type void; the
4990 result is of type void and is an rvalue.
4992 We must avoid calling force_rvalue for expressions of type
4993 "void" because it will complain that their value is being
4994 used. */
4995 if (TREE_CODE (arg2) == THROW_EXPR
4996 && TREE_CODE (arg3) != THROW_EXPR)
4998 if (!VOID_TYPE_P (arg3_type))
5000 arg3 = force_rvalue (arg3, complain);
5001 if (arg3 == error_mark_node)
5002 return error_mark_node;
5004 arg3_type = TREE_TYPE (arg3);
5005 result_type = arg3_type;
5007 else if (TREE_CODE (arg2) != THROW_EXPR
5008 && TREE_CODE (arg3) == THROW_EXPR)
5010 if (!VOID_TYPE_P (arg2_type))
5012 arg2 = force_rvalue (arg2, complain);
5013 if (arg2 == error_mark_node)
5014 return error_mark_node;
5016 arg2_type = TREE_TYPE (arg2);
5017 result_type = arg2_type;
5019 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
5020 result_type = void_type_node;
5021 else
5023 if (complain & tf_error)
5025 if (VOID_TYPE_P (arg2_type))
5026 error_at (EXPR_LOC_OR_LOC (arg3, loc),
5027 "second operand to the conditional operator "
5028 "is of type %<void%>, but the third operand is "
5029 "neither a throw-expression nor of type %<void%>");
5030 else
5031 error_at (EXPR_LOC_OR_LOC (arg2, loc),
5032 "third operand to the conditional operator "
5033 "is of type %<void%>, but the second operand is "
5034 "neither a throw-expression nor of type %<void%>");
5036 return error_mark_node;
5039 is_lvalue = false;
5040 goto valid_operands;
5042 /* [expr.cond]
5044 Otherwise, if the second and third operand have different types,
5045 and either has (possibly cv-qualified) class type, or if both are
5046 glvalues of the same value category and the same type except for
5047 cv-qualification, an attempt is made to convert each of those operands
5048 to the type of the other. */
5049 else if (!same_type_p (arg2_type, arg3_type)
5050 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
5051 || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
5052 arg3_type)
5053 && glvalue_p (arg2) && glvalue_p (arg3)
5054 && lvalue_p (arg2) == lvalue_p (arg3))))
5056 conversion *conv2;
5057 conversion *conv3;
5058 bool converted = false;
5060 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5061 p = conversion_obstack_alloc (0);
5063 conv2 = conditional_conversion (arg2, arg3, complain);
5064 conv3 = conditional_conversion (arg3, arg2, complain);
5066 /* [expr.cond]
5068 If both can be converted, or one can be converted but the
5069 conversion is ambiguous, the program is ill-formed. If
5070 neither can be converted, the operands are left unchanged and
5071 further checking is performed as described below. If exactly
5072 one conversion is possible, that conversion is applied to the
5073 chosen operand and the converted operand is used in place of
5074 the original operand for the remainder of this section. */
5075 if ((conv2 && !conv2->bad_p
5076 && conv3 && !conv3->bad_p)
5077 || (conv2 && conv2->kind == ck_ambig)
5078 || (conv3 && conv3->kind == ck_ambig))
5080 if (complain & tf_error)
5082 error_at (loc, "operands to ?: have different types %qT and %qT",
5083 arg2_type, arg3_type);
5084 if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
5085 inform (loc, " and each type can be converted to the other");
5086 else if (conv2 && conv2->kind == ck_ambig)
5087 convert_like (conv2, arg2, complain);
5088 else
5089 convert_like (conv3, arg3, complain);
5091 result = error_mark_node;
5093 else if (conv2 && !conv2->bad_p)
5095 arg2 = convert_like (conv2, arg2, complain);
5096 arg2 = convert_from_reference (arg2);
5097 arg2_type = TREE_TYPE (arg2);
5098 /* Even if CONV2 is a valid conversion, the result of the
5099 conversion may be invalid. For example, if ARG3 has type
5100 "volatile X", and X does not have a copy constructor
5101 accepting a "volatile X&", then even if ARG2 can be
5102 converted to X, the conversion will fail. */
5103 if (error_operand_p (arg2))
5104 result = error_mark_node;
5105 converted = true;
5107 else if (conv3 && !conv3->bad_p)
5109 arg3 = convert_like (conv3, arg3, complain);
5110 arg3 = convert_from_reference (arg3);
5111 arg3_type = TREE_TYPE (arg3);
5112 if (error_operand_p (arg3))
5113 result = error_mark_node;
5114 converted = true;
5117 /* Free all the conversions we allocated. */
5118 obstack_free (&conversion_obstack, p);
5120 if (result)
5121 return result;
5123 /* If, after the conversion, both operands have class type,
5124 treat the cv-qualification of both operands as if it were the
5125 union of the cv-qualification of the operands.
5127 The standard is not clear about what to do in this
5128 circumstance. For example, if the first operand has type
5129 "const X" and the second operand has a user-defined
5130 conversion to "volatile X", what is the type of the second
5131 operand after this step? Making it be "const X" (matching
5132 the first operand) seems wrong, as that discards the
5133 qualification without actually performing a copy. Leaving it
5134 as "volatile X" seems wrong as that will result in the
5135 conditional expression failing altogether, even though,
5136 according to this step, the one operand could be converted to
5137 the type of the other. */
5138 if (converted
5139 && CLASS_TYPE_P (arg2_type)
5140 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
5141 arg2_type = arg3_type =
5142 cp_build_qualified_type (arg2_type,
5143 cp_type_quals (arg2_type)
5144 | cp_type_quals (arg3_type));
5147 /* [expr.cond]
5149 If the second and third operands are glvalues of the same value
5150 category and have the same type, the result is of that type and
5151 value category. */
5152 if (((lvalue_p (arg2) && lvalue_p (arg3))
5153 || (xvalue_p (arg2) && xvalue_p (arg3)))
5154 && same_type_p (arg2_type, arg3_type))
5156 result_type = arg2_type;
5157 arg2 = mark_lvalue_use (arg2);
5158 arg3 = mark_lvalue_use (arg3);
5159 goto valid_operands;
5162 /* [expr.cond]
5164 Otherwise, the result is an rvalue. If the second and third
5165 operand do not have the same type, and either has (possibly
5166 cv-qualified) class type, overload resolution is used to
5167 determine the conversions (if any) to be applied to the operands
5168 (_over.match.oper_, _over.built_). */
5169 is_lvalue = false;
5170 if (!same_type_p (arg2_type, arg3_type)
5171 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
5173 tree args[3];
5174 conversion *conv;
5175 bool any_viable_p;
5177 /* Rearrange the arguments so that add_builtin_candidate only has
5178 to know about two args. In build_builtin_candidate, the
5179 arguments are unscrambled. */
5180 args[0] = arg2;
5181 args[1] = arg3;
5182 args[2] = arg1;
5183 add_builtin_candidates (&candidates,
5184 COND_EXPR,
5185 NOP_EXPR,
5186 cp_operator_id (COND_EXPR),
5187 args,
5188 LOOKUP_NORMAL, complain);
5190 /* [expr.cond]
5192 If the overload resolution fails, the program is
5193 ill-formed. */
5194 candidates = splice_viable (candidates, false, &any_viable_p);
5195 if (!any_viable_p)
5197 if (complain & tf_error)
5198 error_at (loc, "operands to ?: have different types %qT and %qT",
5199 arg2_type, arg3_type);
5200 return error_mark_node;
5202 cand = tourney (candidates, complain);
5203 if (!cand)
5205 if (complain & tf_error)
5207 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
5208 print_z_candidates (loc, candidates);
5210 return error_mark_node;
5213 /* [expr.cond]
5215 Otherwise, the conversions thus determined are applied, and
5216 the converted operands are used in place of the original
5217 operands for the remainder of this section. */
5218 conv = cand->convs[0];
5219 arg1 = convert_like (conv, arg1, complain);
5220 conv = cand->convs[1];
5221 arg2 = convert_like (conv, arg2, complain);
5222 arg2_type = TREE_TYPE (arg2);
5223 conv = cand->convs[2];
5224 arg3 = convert_like (conv, arg3, complain);
5225 arg3_type = TREE_TYPE (arg3);
5228 /* [expr.cond]
5230 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
5231 and function-to-pointer (_conv.func_) standard conversions are
5232 performed on the second and third operands.
5234 We need to force the lvalue-to-rvalue conversion here for class types,
5235 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
5236 that isn't wrapped with a TARGET_EXPR plays havoc with exception
5237 regions. */
5239 arg2 = force_rvalue (arg2, complain);
5240 if (!CLASS_TYPE_P (arg2_type))
5241 arg2_type = TREE_TYPE (arg2);
5243 arg3 = force_rvalue (arg3, complain);
5244 if (!CLASS_TYPE_P (arg3_type))
5245 arg3_type = TREE_TYPE (arg3);
5247 if (arg2 == error_mark_node || arg3 == error_mark_node)
5248 return error_mark_node;
5250 /* [expr.cond]
5252 After those conversions, one of the following shall hold:
5254 --The second and third operands have the same type; the result is of
5255 that type. */
5256 if (same_type_p (arg2_type, arg3_type))
5257 result_type = arg2_type;
5258 /* [expr.cond]
5260 --The second and third operands have arithmetic or enumeration
5261 type; the usual arithmetic conversions are performed to bring
5262 them to a common type, and the result is of that type. */
5263 else if ((ARITHMETIC_TYPE_P (arg2_type)
5264 || UNSCOPED_ENUM_P (arg2_type))
5265 && (ARITHMETIC_TYPE_P (arg3_type)
5266 || UNSCOPED_ENUM_P (arg3_type)))
5268 /* In this case, there is always a common type. */
5269 result_type = type_after_usual_arithmetic_conversions (arg2_type,
5270 arg3_type);
5271 if (complain & tf_warning)
5272 do_warn_double_promotion (result_type, arg2_type, arg3_type,
5273 "implicit conversion from %qH to %qI to "
5274 "match other result of conditional",
5275 loc);
5277 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
5278 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
5280 if (TREE_CODE (orig_arg2) == CONST_DECL
5281 && TREE_CODE (orig_arg3) == CONST_DECL
5282 && DECL_CONTEXT (orig_arg2) == DECL_CONTEXT (orig_arg3))
5283 /* Two enumerators from the same enumeration can have different
5284 types when the enumeration is still being defined. */;
5285 else if (complain & tf_warning)
5286 warning_at (loc, OPT_Wenum_compare, "enumeral mismatch in "
5287 "conditional expression: %qT vs %qT",
5288 arg2_type, arg3_type);
5290 else if (extra_warnings
5291 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
5292 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
5293 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
5294 && !same_type_p (arg2_type,
5295 type_promotes_to (arg3_type)))))
5297 if (complain & tf_warning)
5298 warning_at (loc, OPT_Wextra, "enumeral and non-enumeral type in "
5299 "conditional expression");
5302 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5303 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5305 /* [expr.cond]
5307 --The second and third operands have pointer type, or one has
5308 pointer type and the other is a null pointer constant; pointer
5309 conversions (_conv.ptr_) and qualification conversions
5310 (_conv.qual_) are performed to bring them to their composite
5311 pointer type (_expr.rel_). The result is of the composite
5312 pointer type.
5314 --The second and third operands have pointer to member type, or
5315 one has pointer to member type and the other is a null pointer
5316 constant; pointer to member conversions (_conv.mem_) and
5317 qualification conversions (_conv.qual_) are performed to bring
5318 them to a common type, whose cv-qualification shall match the
5319 cv-qualification of either the second or the third operand.
5320 The result is of the common type. */
5321 else if ((null_ptr_cst_p (arg2)
5322 && TYPE_PTR_OR_PTRMEM_P (arg3_type))
5323 || (null_ptr_cst_p (arg3)
5324 && TYPE_PTR_OR_PTRMEM_P (arg2_type))
5325 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
5326 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
5327 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
5329 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
5330 arg3, CPO_CONDITIONAL_EXPR,
5331 complain);
5332 if (result_type == error_mark_node)
5333 return error_mark_node;
5334 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5335 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5338 if (!result_type)
5340 if (complain & tf_error)
5341 error_at (loc, "operands to ?: have different types %qT and %qT",
5342 arg2_type, arg3_type);
5343 return error_mark_node;
5346 if (arg2 == error_mark_node || arg3 == error_mark_node)
5347 return error_mark_node;
5349 valid_operands:
5350 result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
5352 /* If the ARG2 and ARG3 are the same and don't have side-effects,
5353 warn here, because the COND_EXPR will be turned into ARG2. */
5354 if (warn_duplicated_branches
5355 && (arg2 == arg3 || operand_equal_p (arg2, arg3, 0)))
5356 warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
5357 "this condition has identical branches");
5359 /* We can't use result_type below, as fold might have returned a
5360 throw_expr. */
5362 if (!is_lvalue)
5364 /* Expand both sides into the same slot, hopefully the target of
5365 the ?: expression. We used to check for TARGET_EXPRs here,
5366 but now we sometimes wrap them in NOP_EXPRs so the test would
5367 fail. */
5368 if (CLASS_TYPE_P (TREE_TYPE (result)))
5369 result = get_target_expr_sfinae (result, complain);
5370 /* If this expression is an rvalue, but might be mistaken for an
5371 lvalue, we must add a NON_LVALUE_EXPR. */
5372 result = rvalue (result);
5374 else
5375 result = force_paren_expr (result);
5377 return result;
5380 /* Wrapper for above. */
5382 tree
5383 build_conditional_expr (location_t loc, tree arg1, tree arg2, tree arg3,
5384 tsubst_flags_t complain)
5386 tree ret;
5387 bool subtime = timevar_cond_start (TV_OVERLOAD);
5388 ret = build_conditional_expr_1 (loc, arg1, arg2, arg3, complain);
5389 timevar_cond_stop (TV_OVERLOAD, subtime);
5390 return ret;
5393 /* OPERAND is an operand to an expression. Perform necessary steps
5394 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
5395 returned. */
5397 static tree
5398 prep_operand (tree operand)
5400 if (operand)
5402 if (CLASS_TYPE_P (TREE_TYPE (operand))
5403 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
5404 /* Make sure the template type is instantiated now. */
5405 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
5408 return operand;
5411 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5412 OVERLOAD) to the CANDIDATES, returning an updated list of
5413 CANDIDATES. The ARGS are the arguments provided to the call;
5414 if FIRST_ARG is non-null it is the implicit object argument,
5415 otherwise the first element of ARGS is used if needed. The
5416 EXPLICIT_TARGS are explicit template arguments provided.
5417 TEMPLATE_ONLY is true if only template functions should be
5418 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5419 add_function_candidate. */
5421 static void
5422 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
5423 tree return_type,
5424 tree explicit_targs, bool template_only,
5425 tree conversion_path, tree access_path,
5426 int flags,
5427 struct z_candidate **candidates,
5428 tsubst_flags_t complain)
5430 tree ctype;
5431 const vec<tree, va_gc> *non_static_args;
5432 bool check_list_ctor = false;
5433 bool check_converting = false;
5434 unification_kind_t strict;
5436 if (!fns)
5437 return;
5439 /* Precalculate special handling of constructors and conversion ops. */
5440 tree fn = OVL_FIRST (fns);
5441 if (DECL_CONV_FN_P (fn))
5443 check_list_ctor = false;
5444 check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
5445 if (flags & LOOKUP_NO_CONVERSION)
5446 /* We're doing return_type(x). */
5447 strict = DEDUCE_CONV;
5448 else
5449 /* We're doing x.operator return_type(). */
5450 strict = DEDUCE_EXACT;
5451 /* [over.match.funcs] For conversion functions, the function
5452 is considered to be a member of the class of the implicit
5453 object argument for the purpose of defining the type of
5454 the implicit object parameter. */
5455 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
5457 else
5459 if (DECL_CONSTRUCTOR_P (fn))
5461 check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
5462 /* For list-initialization we consider explicit constructors
5463 and complain if one is chosen. */
5464 check_converting
5465 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
5466 == LOOKUP_ONLYCONVERTING);
5468 strict = DEDUCE_CALL;
5469 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
5472 if (first_arg)
5473 non_static_args = args;
5474 else
5475 /* Delay creating the implicit this parameter until it is needed. */
5476 non_static_args = NULL;
5478 for (lkp_iterator iter (fns); iter; ++iter)
5480 fn = *iter;
5482 if (check_converting && DECL_NONCONVERTING_P (fn))
5483 continue;
5484 if (check_list_ctor && !is_list_ctor (fn))
5485 continue;
5487 tree fn_first_arg = NULL_TREE;
5488 const vec<tree, va_gc> *fn_args = args;
5490 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
5492 /* Figure out where the object arg comes from. If this
5493 function is a non-static member and we didn't get an
5494 implicit object argument, move it out of args. */
5495 if (first_arg == NULL_TREE)
5497 unsigned int ix;
5498 tree arg;
5499 vec<tree, va_gc> *tempvec;
5500 vec_alloc (tempvec, args->length () - 1);
5501 for (ix = 1; args->iterate (ix, &arg); ++ix)
5502 tempvec->quick_push (arg);
5503 non_static_args = tempvec;
5504 first_arg = (*args)[0];
5507 fn_first_arg = first_arg;
5508 fn_args = non_static_args;
5511 if (TREE_CODE (fn) == TEMPLATE_DECL)
5512 add_template_candidate (candidates,
5514 ctype,
5515 explicit_targs,
5516 fn_first_arg,
5517 fn_args,
5518 return_type,
5519 access_path,
5520 conversion_path,
5521 flags,
5522 strict,
5523 complain);
5524 else if (!template_only)
5525 add_function_candidate (candidates,
5527 ctype,
5528 fn_first_arg,
5529 fn_args,
5530 access_path,
5531 conversion_path,
5532 flags,
5533 complain);
5537 /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
5538 -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
5540 static int
5541 op_is_ordered (tree_code code)
5543 switch (code)
5545 // 5. b @= a
5546 case MODIFY_EXPR:
5547 return (flag_strong_eval_order > 1 ? -1 : 0);
5549 // 6. a[b]
5550 case ARRAY_REF:
5551 return (flag_strong_eval_order > 1 ? 1 : 0);
5553 // 1. a.b
5554 // Not overloadable (yet).
5555 // 2. a->b
5556 // Only one argument.
5557 // 3. a->*b
5558 case MEMBER_REF:
5559 // 7. a << b
5560 case LSHIFT_EXPR:
5561 // 8. a >> b
5562 case RSHIFT_EXPR:
5563 return (flag_strong_eval_order ? 1 : 0);
5565 default:
5566 return 0;
5570 static tree
5571 build_new_op_1 (location_t loc, enum tree_code code, int flags, tree arg1,
5572 tree arg2, tree arg3, tree *overload, tsubst_flags_t complain)
5574 struct z_candidate *candidates = 0, *cand;
5575 vec<tree, va_gc> *arglist;
5576 tree fnname;
5577 tree args[3];
5578 tree result = NULL_TREE;
5579 bool result_valid_p = false;
5580 enum tree_code code2 = NOP_EXPR;
5581 enum tree_code code_orig_arg1 = ERROR_MARK;
5582 enum tree_code code_orig_arg2 = ERROR_MARK;
5583 conversion *conv;
5584 void *p;
5585 bool strict_p;
5586 bool any_viable_p;
5588 if (error_operand_p (arg1)
5589 || error_operand_p (arg2)
5590 || error_operand_p (arg3))
5591 return error_mark_node;
5593 if (code == MODIFY_EXPR)
5595 code2 = TREE_CODE (arg3);
5596 arg3 = NULL_TREE;
5597 fnname = cp_assignment_operator_id (code2);
5599 else
5600 fnname = cp_operator_id (code);
5602 arg1 = prep_operand (arg1);
5604 bool memonly = false;
5605 switch (code)
5607 case NEW_EXPR:
5608 case VEC_NEW_EXPR:
5609 case VEC_DELETE_EXPR:
5610 case DELETE_EXPR:
5611 /* Use build_op_new_call and build_op_delete_call instead. */
5612 gcc_unreachable ();
5614 case CALL_EXPR:
5615 /* Use build_op_call instead. */
5616 gcc_unreachable ();
5618 case TRUTH_ORIF_EXPR:
5619 case TRUTH_ANDIF_EXPR:
5620 case TRUTH_AND_EXPR:
5621 case TRUTH_OR_EXPR:
5622 /* These are saved for the sake of warn_logical_operator. */
5623 code_orig_arg1 = TREE_CODE (arg1);
5624 code_orig_arg2 = TREE_CODE (arg2);
5625 break;
5626 case GT_EXPR:
5627 case LT_EXPR:
5628 case GE_EXPR:
5629 case LE_EXPR:
5630 case EQ_EXPR:
5631 case NE_EXPR:
5632 /* These are saved for the sake of maybe_warn_bool_compare. */
5633 code_orig_arg1 = TREE_CODE (TREE_TYPE (arg1));
5634 code_orig_arg2 = TREE_CODE (TREE_TYPE (arg2));
5635 break;
5637 /* =, ->, [], () must be non-static member functions. */
5638 case MODIFY_EXPR:
5639 if (code2 != NOP_EXPR)
5640 break;
5641 /* FALLTHRU */
5642 case COMPONENT_REF:
5643 case ARRAY_REF:
5644 memonly = true;
5645 break;
5647 default:
5648 break;
5651 arg2 = prep_operand (arg2);
5652 arg3 = prep_operand (arg3);
5654 if (code == COND_EXPR)
5655 /* Use build_conditional_expr instead. */
5656 gcc_unreachable ();
5657 else if (! OVERLOAD_TYPE_P (TREE_TYPE (arg1))
5658 && (! arg2 || ! OVERLOAD_TYPE_P (TREE_TYPE (arg2))))
5659 goto builtin;
5661 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5662 arg2 = integer_zero_node;
5664 vec_alloc (arglist, 3);
5665 arglist->quick_push (arg1);
5666 if (arg2 != NULL_TREE)
5667 arglist->quick_push (arg2);
5668 if (arg3 != NULL_TREE)
5669 arglist->quick_push (arg3);
5671 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5672 p = conversion_obstack_alloc (0);
5674 /* Add namespace-scope operators to the list of functions to
5675 consider. */
5676 if (!memonly)
5678 tree fns = lookup_name_real (fnname, 0, 1, /*block_p=*/true, 0, 0);
5679 fns = lookup_arg_dependent (fnname, fns, arglist);
5680 add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
5681 NULL_TREE, false, NULL_TREE, NULL_TREE,
5682 flags, &candidates, complain);
5685 args[0] = arg1;
5686 args[1] = arg2;
5687 args[2] = NULL_TREE;
5689 /* Add class-member operators to the candidate set. */
5690 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
5692 tree fns;
5694 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
5695 if (fns == error_mark_node)
5697 result = error_mark_node;
5698 goto user_defined_result_ready;
5700 if (fns)
5701 add_candidates (BASELINK_FUNCTIONS (fns),
5702 NULL_TREE, arglist, NULL_TREE,
5703 NULL_TREE, false,
5704 BASELINK_BINFO (fns),
5705 BASELINK_ACCESS_BINFO (fns),
5706 flags, &candidates, complain);
5708 /* Per 13.3.1.2/3, 2nd bullet, if no operand has a class type, then
5709 only non-member functions that have type T1 or reference to
5710 cv-qualified-opt T1 for the first argument, if the first argument
5711 has an enumeration type, or T2 or reference to cv-qualified-opt
5712 T2 for the second argument, if the second argument has an
5713 enumeration type. Filter out those that don't match. */
5714 else if (! arg2 || ! CLASS_TYPE_P (TREE_TYPE (arg2)))
5716 struct z_candidate **candp, **next;
5718 for (candp = &candidates; *candp; candp = next)
5720 tree parmlist, parmtype;
5721 int i, nargs = (arg2 ? 2 : 1);
5723 cand = *candp;
5724 next = &cand->next;
5726 parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5728 for (i = 0; i < nargs; ++i)
5730 parmtype = TREE_VALUE (parmlist);
5732 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
5733 parmtype = TREE_TYPE (parmtype);
5734 if (TREE_CODE (TREE_TYPE (args[i])) == ENUMERAL_TYPE
5735 && (same_type_ignoring_top_level_qualifiers_p
5736 (TREE_TYPE (args[i]), parmtype)))
5737 break;
5739 parmlist = TREE_CHAIN (parmlist);
5742 /* No argument has an appropriate type, so remove this
5743 candidate function from the list. */
5744 if (i == nargs)
5746 *candp = cand->next;
5747 next = candp;
5752 add_builtin_candidates (&candidates, code, code2, fnname, args,
5753 flags, complain);
5755 switch (code)
5757 case COMPOUND_EXPR:
5758 case ADDR_EXPR:
5759 /* For these, the built-in candidates set is empty
5760 [over.match.oper]/3. We don't want non-strict matches
5761 because exact matches are always possible with built-in
5762 operators. The built-in candidate set for COMPONENT_REF
5763 would be empty too, but since there are no such built-in
5764 operators, we accept non-strict matches for them. */
5765 strict_p = true;
5766 break;
5768 default:
5769 strict_p = false;
5770 break;
5773 candidates = splice_viable (candidates, strict_p, &any_viable_p);
5774 if (!any_viable_p)
5776 switch (code)
5778 case POSTINCREMENT_EXPR:
5779 case POSTDECREMENT_EXPR:
5780 /* Don't try anything fancy if we're not allowed to produce
5781 errors. */
5782 if (!(complain & tf_error))
5783 return error_mark_node;
5785 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
5786 distinguish between prefix and postfix ++ and
5787 operator++() was used for both, so we allow this with
5788 -fpermissive. */
5789 else
5791 const char *msg = (flag_permissive)
5792 ? G_("no %<%D(int)%> declared for postfix %qs,"
5793 " trying prefix operator instead")
5794 : G_("no %<%D(int)%> declared for postfix %qs");
5795 permerror (loc, msg, fnname, operator_name_info[code].name);
5798 if (!flag_permissive)
5799 return error_mark_node;
5801 if (code == POSTINCREMENT_EXPR)
5802 code = PREINCREMENT_EXPR;
5803 else
5804 code = PREDECREMENT_EXPR;
5805 result = build_new_op_1 (loc, code, flags, arg1, NULL_TREE,
5806 NULL_TREE, overload, complain);
5807 break;
5809 /* The caller will deal with these. */
5810 case ADDR_EXPR:
5811 case COMPOUND_EXPR:
5812 case COMPONENT_REF:
5813 result = NULL_TREE;
5814 result_valid_p = true;
5815 break;
5817 default:
5818 if (complain & tf_error)
5820 /* If one of the arguments of the operator represents
5821 an invalid use of member function pointer, try to report
5822 a meaningful error ... */
5823 if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
5824 || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
5825 || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
5826 /* We displayed the error message. */;
5827 else
5829 /* ... Otherwise, report the more generic
5830 "no matching operator found" error */
5831 op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
5832 print_z_candidates (loc, candidates);
5835 result = error_mark_node;
5836 break;
5839 else
5841 cand = tourney (candidates, complain);
5842 if (cand == 0)
5844 if (complain & tf_error)
5846 op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
5847 print_z_candidates (loc, candidates);
5849 result = error_mark_node;
5851 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5853 if (overload)
5854 *overload = cand->fn;
5856 if (resolve_args (arglist, complain) == NULL)
5857 result = error_mark_node;
5858 else
5859 result = build_over_call (cand, LOOKUP_NORMAL, complain);
5861 if (trivial_fn_p (cand->fn))
5862 /* There won't be a CALL_EXPR. */;
5863 else if (result && result != error_mark_node)
5865 tree call = extract_call_expr (result);
5866 CALL_EXPR_OPERATOR_SYNTAX (call) = true;
5868 if (processing_template_decl && DECL_HIDDEN_FRIEND_P (cand->fn))
5869 /* This prevents build_new_function_call from discarding this
5870 function during instantiation of the enclosing template. */
5871 KOENIG_LOOKUP_P (call) = 1;
5873 /* Specify evaluation order as per P0145R2. */
5874 CALL_EXPR_ORDERED_ARGS (call) = false;
5875 switch (op_is_ordered (code))
5877 case -1:
5878 CALL_EXPR_REVERSE_ARGS (call) = true;
5879 break;
5881 case 1:
5882 CALL_EXPR_ORDERED_ARGS (call) = true;
5883 break;
5885 default:
5886 break;
5890 else
5892 /* Give any warnings we noticed during overload resolution. */
5893 if (cand->warnings && (complain & tf_warning))
5895 struct candidate_warning *w;
5896 for (w = cand->warnings; w; w = w->next)
5897 joust (cand, w->loser, 1, complain);
5900 /* Check for comparison of different enum types. */
5901 switch (code)
5903 case GT_EXPR:
5904 case LT_EXPR:
5905 case GE_EXPR:
5906 case LE_EXPR:
5907 case EQ_EXPR:
5908 case NE_EXPR:
5909 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
5910 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
5911 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5912 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
5913 && (complain & tf_warning))
5915 warning (OPT_Wenum_compare,
5916 "comparison between %q#T and %q#T",
5917 TREE_TYPE (arg1), TREE_TYPE (arg2));
5919 break;
5920 default:
5921 break;
5924 /* We need to strip any leading REF_BIND so that bitfields
5925 don't cause errors. This should not remove any important
5926 conversions, because builtins don't apply to class
5927 objects directly. */
5928 conv = cand->convs[0];
5929 if (conv->kind == ck_ref_bind)
5930 conv = next_conversion (conv);
5931 arg1 = convert_like (conv, arg1, complain);
5933 if (arg2)
5935 conv = cand->convs[1];
5936 if (conv->kind == ck_ref_bind)
5937 conv = next_conversion (conv);
5938 else
5939 arg2 = decay_conversion (arg2, complain);
5941 /* We need to call warn_logical_operator before
5942 converting arg2 to a boolean_type, but after
5943 decaying an enumerator to its value. */
5944 if (complain & tf_warning)
5945 warn_logical_operator (loc, code, boolean_type_node,
5946 code_orig_arg1, arg1,
5947 code_orig_arg2, arg2);
5949 arg2 = convert_like (conv, arg2, complain);
5951 if (arg3)
5953 conv = cand->convs[2];
5954 if (conv->kind == ck_ref_bind)
5955 conv = next_conversion (conv);
5956 arg3 = convert_like (conv, arg3, complain);
5962 user_defined_result_ready:
5964 /* Free all the conversions we allocated. */
5965 obstack_free (&conversion_obstack, p);
5967 if (result || result_valid_p)
5968 return result;
5970 builtin:
5971 switch (code)
5973 case MODIFY_EXPR:
5974 return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
5976 case INDIRECT_REF:
5977 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
5979 case TRUTH_ANDIF_EXPR:
5980 case TRUTH_ORIF_EXPR:
5981 case TRUTH_AND_EXPR:
5982 case TRUTH_OR_EXPR:
5983 if (complain & tf_warning)
5984 warn_logical_operator (loc, code, boolean_type_node,
5985 code_orig_arg1, arg1,
5986 code_orig_arg2, arg2);
5987 /* Fall through. */
5988 case GT_EXPR:
5989 case LT_EXPR:
5990 case GE_EXPR:
5991 case LE_EXPR:
5992 case EQ_EXPR:
5993 case NE_EXPR:
5994 if ((complain & tf_warning)
5995 && ((code_orig_arg1 == BOOLEAN_TYPE)
5996 ^ (code_orig_arg2 == BOOLEAN_TYPE)))
5997 maybe_warn_bool_compare (loc, code, arg1, arg2);
5998 if (complain & tf_warning && warn_tautological_compare)
5999 warn_tautological_cmp (loc, code, arg1, arg2);
6000 /* Fall through. */
6001 case PLUS_EXPR:
6002 case MINUS_EXPR:
6003 case MULT_EXPR:
6004 case TRUNC_DIV_EXPR:
6005 case MAX_EXPR:
6006 case MIN_EXPR:
6007 case LSHIFT_EXPR:
6008 case RSHIFT_EXPR:
6009 case TRUNC_MOD_EXPR:
6010 case BIT_AND_EXPR:
6011 case BIT_IOR_EXPR:
6012 case BIT_XOR_EXPR:
6013 return cp_build_binary_op (loc, code, arg1, arg2, complain);
6015 case UNARY_PLUS_EXPR:
6016 case NEGATE_EXPR:
6017 case BIT_NOT_EXPR:
6018 case TRUTH_NOT_EXPR:
6019 case PREINCREMENT_EXPR:
6020 case POSTINCREMENT_EXPR:
6021 case PREDECREMENT_EXPR:
6022 case POSTDECREMENT_EXPR:
6023 case REALPART_EXPR:
6024 case IMAGPART_EXPR:
6025 case ABS_EXPR:
6026 return cp_build_unary_op (code, arg1, candidates != 0, complain);
6028 case ARRAY_REF:
6029 return cp_build_array_ref (input_location, arg1, arg2, complain);
6031 case MEMBER_REF:
6032 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_ARROW_STAR,
6033 complain),
6034 arg2, complain);
6036 /* The caller will deal with these. */
6037 case ADDR_EXPR:
6038 case COMPONENT_REF:
6039 case COMPOUND_EXPR:
6040 return NULL_TREE;
6042 default:
6043 gcc_unreachable ();
6045 return NULL_TREE;
6048 /* Wrapper for above. */
6050 tree
6051 build_new_op (location_t loc, enum tree_code code, int flags,
6052 tree arg1, tree arg2, tree arg3,
6053 tree *overload, tsubst_flags_t complain)
6055 tree ret;
6056 bool subtime = timevar_cond_start (TV_OVERLOAD);
6057 ret = build_new_op_1 (loc, code, flags, arg1, arg2, arg3,
6058 overload, complain);
6059 timevar_cond_stop (TV_OVERLOAD, subtime);
6060 return ret;
6063 /* CALL was returned by some call-building function; extract the actual
6064 CALL_EXPR from any bits that have been tacked on, e.g. by
6065 convert_from_reference. */
6067 tree
6068 extract_call_expr (tree call)
6070 while (TREE_CODE (call) == COMPOUND_EXPR)
6071 call = TREE_OPERAND (call, 1);
6072 if (REFERENCE_REF_P (call))
6073 call = TREE_OPERAND (call, 0);
6074 if (TREE_CODE (call) == TARGET_EXPR)
6075 call = TARGET_EXPR_INITIAL (call);
6076 gcc_assert (TREE_CODE (call) == CALL_EXPR
6077 || TREE_CODE (call) == AGGR_INIT_EXPR
6078 || call == error_mark_node);
6079 return call;
6082 /* Returns true if FN has two parameters, of which the second has type
6083 size_t. */
6085 static bool
6086 second_parm_is_size_t (tree fn)
6088 tree t = FUNCTION_ARG_CHAIN (fn);
6089 if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
6090 return false;
6091 t = TREE_CHAIN (t);
6092 if (t == void_list_node)
6093 return true;
6094 if (aligned_new_threshold && t
6095 && same_type_p (TREE_VALUE (t), align_type_node)
6096 && TREE_CHAIN (t) == void_list_node)
6097 return true;
6098 return false;
6101 /* True if T, an allocation function, has std::align_val_t as its second
6102 argument. */
6104 bool
6105 aligned_allocation_fn_p (tree t)
6107 if (!aligned_new_threshold)
6108 return false;
6110 tree a = FUNCTION_ARG_CHAIN (t);
6111 return (a && same_type_p (TREE_VALUE (a), align_type_node));
6114 /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
6115 function (3.7.4.2 [basic.stc.dynamic.deallocation]) with a parameter of
6116 std::align_val_t. */
6118 static bool
6119 aligned_deallocation_fn_p (tree t)
6121 if (!aligned_new_threshold)
6122 return false;
6124 /* A template instance is never a usual deallocation function,
6125 regardless of its signature. */
6126 if (TREE_CODE (t) == TEMPLATE_DECL
6127 || primary_template_instantiation_p (t))
6128 return false;
6130 tree a = FUNCTION_ARG_CHAIN (t);
6131 if (same_type_p (TREE_VALUE (a), align_type_node)
6132 && TREE_CHAIN (a) == void_list_node)
6133 return true;
6134 if (!same_type_p (TREE_VALUE (a), size_type_node))
6135 return false;
6136 a = TREE_CHAIN (a);
6137 if (a && same_type_p (TREE_VALUE (a), align_type_node)
6138 && TREE_CHAIN (a) == void_list_node)
6139 return true;
6140 return false;
6143 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
6144 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
6146 bool
6147 usual_deallocation_fn_p (tree t)
6149 /* A template instance is never a usual deallocation function,
6150 regardless of its signature. */
6151 if (TREE_CODE (t) == TEMPLATE_DECL
6152 || primary_template_instantiation_p (t))
6153 return false;
6155 /* If a class T has a member deallocation function named operator delete
6156 with exactly one parameter, then that function is a usual
6157 (non-placement) deallocation function. If class T does not declare
6158 such an operator delete but does declare a member deallocation
6159 function named operator delete with exactly two parameters, the second
6160 of which has type std::size_t (18.2), then this function is a usual
6161 deallocation function. */
6162 bool global = DECL_NAMESPACE_SCOPE_P (t);
6163 tree chain = FUNCTION_ARG_CHAIN (t);
6164 if (!chain)
6165 return false;
6166 if (chain == void_list_node
6167 || ((!global || flag_sized_deallocation)
6168 && second_parm_is_size_t (t)))
6169 return true;
6170 if (aligned_deallocation_fn_p (t))
6171 return true;
6172 return false;
6175 /* Build a call to operator delete. This has to be handled very specially,
6176 because the restrictions on what signatures match are different from all
6177 other call instances. For a normal delete, only a delete taking (void *)
6178 or (void *, size_t) is accepted. For a placement delete, only an exact
6179 match with the placement new is accepted.
6181 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
6182 ADDR is the pointer to be deleted.
6183 SIZE is the size of the memory block to be deleted.
6184 GLOBAL_P is true if the delete-expression should not consider
6185 class-specific delete operators.
6186 PLACEMENT is the corresponding placement new call, or NULL_TREE.
6188 If this call to "operator delete" is being generated as part to
6189 deallocate memory allocated via a new-expression (as per [expr.new]
6190 which requires that if the initialization throws an exception then
6191 we call a deallocation function), then ALLOC_FN is the allocation
6192 function. */
6194 tree
6195 build_op_delete_call (enum tree_code code, tree addr, tree size,
6196 bool global_p, tree placement,
6197 tree alloc_fn, tsubst_flags_t complain)
6199 tree fn = NULL_TREE;
6200 tree fns, fnname, type, t;
6202 if (addr == error_mark_node)
6203 return error_mark_node;
6205 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
6207 fnname = cp_operator_id (code);
6209 if (CLASS_TYPE_P (type)
6210 && COMPLETE_TYPE_P (complete_type (type))
6211 && !global_p)
6212 /* In [class.free]
6214 If the result of the lookup is ambiguous or inaccessible, or if
6215 the lookup selects a placement deallocation function, the
6216 program is ill-formed.
6218 Therefore, we ask lookup_fnfields to complain about ambiguity. */
6220 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
6221 if (fns == error_mark_node)
6222 return error_mark_node;
6224 else
6225 fns = NULL_TREE;
6227 if (fns == NULL_TREE)
6228 fns = lookup_name_nonclass (fnname);
6230 /* Strip const and volatile from addr. */
6231 addr = cp_convert (ptr_type_node, addr, complain);
6233 if (placement)
6235 /* "A declaration of a placement deallocation function matches the
6236 declaration of a placement allocation function if it has the same
6237 number of parameters and, after parameter transformations (8.3.5),
6238 all parameter types except the first are identical."
6240 So we build up the function type we want and ask instantiate_type
6241 to get it for us. */
6242 t = FUNCTION_ARG_CHAIN (alloc_fn);
6243 t = tree_cons (NULL_TREE, ptr_type_node, t);
6244 t = build_function_type (void_type_node, t);
6246 fn = instantiate_type (t, fns, tf_none);
6247 if (fn == error_mark_node)
6248 return NULL_TREE;
6250 fn = MAYBE_BASELINK_FUNCTIONS (fn);
6252 /* "If the lookup finds the two-parameter form of a usual deallocation
6253 function (3.7.4.2) and that function, considered as a placement
6254 deallocation function, would have been selected as a match for the
6255 allocation function, the program is ill-formed." */
6256 if (second_parm_is_size_t (fn))
6258 const char *const msg1
6259 = G_("exception cleanup for this placement new selects "
6260 "non-placement operator delete");
6261 const char *const msg2
6262 = G_("%qD is a usual (non-placement) deallocation "
6263 "function in C++14 (or with -fsized-deallocation)");
6265 /* But if the class has an operator delete (void *), then that is
6266 the usual deallocation function, so we shouldn't complain
6267 about using the operator delete (void *, size_t). */
6268 if (DECL_CLASS_SCOPE_P (fn))
6269 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns));
6270 iter; ++iter)
6272 tree elt = *iter;
6273 if (usual_deallocation_fn_p (elt)
6274 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
6275 goto ok;
6277 /* Before C++14 a two-parameter global deallocation function is
6278 always a placement deallocation function, but warn if
6279 -Wc++14-compat. */
6280 else if (!flag_sized_deallocation)
6282 if ((complain & tf_warning)
6283 && warning (OPT_Wc__14_compat, msg1))
6284 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6285 goto ok;
6288 if (complain & tf_warning_or_error)
6290 if (permerror (input_location, msg1))
6292 /* Only mention C++14 for namespace-scope delete. */
6293 if (DECL_NAMESPACE_SCOPE_P (fn))
6294 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6295 else
6296 inform (DECL_SOURCE_LOCATION (fn),
6297 "%qD is a usual (non-placement) deallocation "
6298 "function", fn);
6301 else
6302 return error_mark_node;
6303 ok:;
6306 else
6307 /* "Any non-placement deallocation function matches a non-placement
6308 allocation function. If the lookup finds a single matching
6309 deallocation function, that function will be called; otherwise, no
6310 deallocation function will be called." */
6311 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
6313 tree elt = *iter;
6314 if (usual_deallocation_fn_p (elt))
6316 if (!fn)
6318 fn = elt;
6319 continue;
6322 /* -- If the type has new-extended alignment, a function with a
6323 parameter of type std::align_val_t is preferred; otherwise a
6324 function without such a parameter is preferred. If exactly one
6325 preferred function is found, that function is selected and the
6326 selection process terminates. If more than one preferred
6327 function is found, all non-preferred functions are eliminated
6328 from further consideration. */
6329 if (aligned_new_threshold)
6331 bool want_align = type_has_new_extended_alignment (type);
6332 bool fn_align = aligned_deallocation_fn_p (fn);
6333 bool elt_align = aligned_deallocation_fn_p (elt);
6335 if (elt_align != fn_align)
6337 if (want_align == elt_align)
6338 fn = elt;
6339 continue;
6343 /* -- If the deallocation functions have class scope, the one
6344 without a parameter of type std::size_t is selected. */
6345 bool want_size;
6346 if (DECL_CLASS_SCOPE_P (fn))
6347 want_size = false;
6349 /* -- If the type is complete and if, for the second alternative
6350 (delete array) only, the operand is a pointer to a class type
6351 with a non-trivial destructor or a (possibly multi-dimensional)
6352 array thereof, the function with a parameter of type std::size_t
6353 is selected.
6355 -- Otherwise, it is unspecified whether a deallocation function
6356 with a parameter of type std::size_t is selected. */
6357 else
6359 want_size = COMPLETE_TYPE_P (type);
6360 if (code == VEC_DELETE_EXPR
6361 && !TYPE_VEC_NEW_USES_COOKIE (type))
6362 /* We need a cookie to determine the array size. */
6363 want_size = false;
6365 bool fn_size = second_parm_is_size_t (fn);
6366 bool elt_size = second_parm_is_size_t (elt);
6367 gcc_assert (fn_size != elt_size);
6368 if (want_size == elt_size)
6369 fn = elt;
6373 /* If we have a matching function, call it. */
6374 if (fn)
6376 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6378 /* If the FN is a member function, make sure that it is
6379 accessible. */
6380 if (BASELINK_P (fns))
6381 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
6382 complain);
6384 /* Core issue 901: It's ok to new a type with deleted delete. */
6385 if (DECL_DELETED_FN (fn) && alloc_fn)
6386 return NULL_TREE;
6388 if (placement)
6390 /* The placement args might not be suitable for overload
6391 resolution at this point, so build the call directly. */
6392 int nargs = call_expr_nargs (placement);
6393 tree *argarray = XALLOCAVEC (tree, nargs);
6394 int i;
6395 argarray[0] = addr;
6396 for (i = 1; i < nargs; i++)
6397 argarray[i] = CALL_EXPR_ARG (placement, i);
6398 if (!mark_used (fn, complain) && !(complain & tf_error))
6399 return error_mark_node;
6400 return build_cxx_call (fn, nargs, argarray, complain);
6402 else
6404 tree ret;
6405 vec<tree, va_gc> *args = make_tree_vector ();
6406 args->quick_push (addr);
6407 if (second_parm_is_size_t (fn))
6408 args->quick_push (size);
6409 if (aligned_deallocation_fn_p (fn))
6411 tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
6412 args->quick_push (al);
6414 ret = cp_build_function_call_vec (fn, &args, complain);
6415 release_tree_vector (args);
6416 return ret;
6420 /* [expr.new]
6422 If no unambiguous matching deallocation function can be found,
6423 propagating the exception does not cause the object's memory to
6424 be freed. */
6425 if (alloc_fn)
6427 if ((complain & tf_warning)
6428 && !placement)
6429 warning (0, "no corresponding deallocation function for %qD",
6430 alloc_fn);
6431 return NULL_TREE;
6434 if (complain & tf_error)
6435 error ("no suitable %<operator %s%> for %qT",
6436 operator_name_info[(int)code].name, type);
6437 return error_mark_node;
6440 /* If the current scope isn't allowed to access DECL along
6441 BASETYPE_PATH, give an error. The most derived class in
6442 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
6443 the declaration to use in the error diagnostic. */
6445 bool
6446 enforce_access (tree basetype_path, tree decl, tree diag_decl,
6447 tsubst_flags_t complain, access_failure_info *afi)
6449 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
6451 if (flag_new_inheriting_ctors
6452 && DECL_INHERITED_CTOR (decl))
6454 /* 7.3.3/18: The additional constructors are accessible if they would be
6455 accessible when used to construct an object of the corresponding base
6456 class. */
6457 decl = strip_inheriting_ctors (decl);
6458 basetype_path = lookup_base (basetype_path, DECL_CONTEXT (decl),
6459 ba_any, NULL, complain);
6462 if (!accessible_p (basetype_path, decl, true))
6464 if (complain & tf_error)
6466 if (flag_new_inheriting_ctors)
6467 diag_decl = strip_inheriting_ctors (diag_decl);
6468 if (TREE_PRIVATE (decl))
6470 error ("%q#D is private within this context", diag_decl);
6471 inform (DECL_SOURCE_LOCATION (diag_decl),
6472 "declared private here");
6473 if (afi)
6474 afi->record_access_failure (basetype_path, diag_decl);
6476 else if (TREE_PROTECTED (decl))
6478 error ("%q#D is protected within this context", diag_decl);
6479 inform (DECL_SOURCE_LOCATION (diag_decl),
6480 "declared protected here");
6481 if (afi)
6482 afi->record_access_failure (basetype_path, diag_decl);
6484 else
6486 error ("%q#D is inaccessible within this context", diag_decl);
6487 inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
6488 if (afi)
6489 afi->record_access_failure (basetype_path, diag_decl);
6492 return false;
6495 return true;
6498 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
6499 bitwise or of LOOKUP_* values. If any errors are warnings are
6500 generated, set *DIAGNOSTIC_FN to "error" or "warning",
6501 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
6502 to NULL. */
6504 static tree
6505 build_temp (tree expr, tree type, int flags,
6506 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
6508 int savew, savee;
6509 vec<tree, va_gc> *args;
6511 *diagnostic_kind = DK_UNSPECIFIED;
6513 /* If the source is a packed field, calling the copy constructor will require
6514 binding the field to the reference parameter to the copy constructor, and
6515 we'll end up with an infinite loop. If we can use a bitwise copy, then
6516 do that now. */
6517 if ((lvalue_kind (expr) & clk_packed)
6518 && CLASS_TYPE_P (TREE_TYPE (expr))
6519 && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
6520 return get_target_expr_sfinae (expr, complain);
6522 savew = warningcount + werrorcount, savee = errorcount;
6523 args = make_tree_vector_single (expr);
6524 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6525 &args, type, flags, complain);
6526 release_tree_vector (args);
6527 if (warningcount + werrorcount > savew)
6528 *diagnostic_kind = DK_WARNING;
6529 else if (errorcount > savee)
6530 *diagnostic_kind = DK_ERROR;
6531 return expr;
6534 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
6535 EXPR is implicitly converted to type TOTYPE.
6536 FN and ARGNUM are used for diagnostics. */
6538 static void
6539 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
6541 /* Issue warnings about peculiar, but valid, uses of NULL. */
6542 if (expr == null_node && TREE_CODE (totype) != BOOLEAN_TYPE
6543 && ARITHMETIC_TYPE_P (totype))
6545 source_location loc =
6546 expansion_point_location_if_in_system_header (input_location);
6548 if (fn)
6549 warning_at (loc, OPT_Wconversion_null,
6550 "passing NULL to non-pointer argument %P of %qD",
6551 argnum, fn);
6552 else
6553 warning_at (loc, OPT_Wconversion_null,
6554 "converting to non-pointer type %qT from NULL", totype);
6557 /* Issue warnings if "false" is converted to a NULL pointer */
6558 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
6559 && TYPE_PTR_P (totype))
6561 if (fn)
6562 warning_at (input_location, OPT_Wconversion_null,
6563 "converting %<false%> to pointer type for argument %P "
6564 "of %qD", argnum, fn);
6565 else
6566 warning_at (input_location, OPT_Wconversion_null,
6567 "converting %<false%> to pointer type %qT", totype);
6571 /* We gave a diagnostic during a conversion. If this was in the second
6572 standard conversion sequence of a user-defined conversion sequence, say
6573 which user-defined conversion. */
6575 static void
6576 maybe_print_user_conv_context (conversion *convs)
6578 if (convs->user_conv_p)
6579 for (conversion *t = convs; t; t = next_conversion (t))
6580 if (t->kind == ck_user)
6582 print_z_candidate (0, " after user-defined conversion:",
6583 t->cand);
6584 break;
6588 /* Locate the parameter with the given index within FNDECL.
6589 ARGNUM is zero based, -1 indicates the `this' argument of a method.
6590 Return the location of the FNDECL itself if there are problems. */
6592 static location_t
6593 get_fndecl_argument_location (tree fndecl, int argnum)
6595 int i;
6596 tree param;
6598 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6599 for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
6600 i < argnum && param;
6601 i++, param = TREE_CHAIN (param))
6604 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6605 return the location of FNDECL. */
6606 if (param == NULL)
6607 return DECL_SOURCE_LOCATION (fndecl);
6609 return DECL_SOURCE_LOCATION (param);
6612 /* Perform the conversions in CONVS on the expression EXPR. FN and
6613 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
6614 indicates the `this' argument of a method. INNER is nonzero when
6615 being called to continue a conversion chain. It is negative when a
6616 reference binding will be applied, positive otherwise. If
6617 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
6618 conversions will be emitted if appropriate. If C_CAST_P is true,
6619 this conversion is coming from a C-style cast; in that case,
6620 conversions to inaccessible bases are permitted. */
6622 static tree
6623 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
6624 bool issue_conversion_warnings,
6625 bool c_cast_p, tsubst_flags_t complain)
6627 tree totype = convs->type;
6628 diagnostic_t diag_kind;
6629 int flags;
6630 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6632 if (convs->bad_p && !(complain & tf_error))
6633 return error_mark_node;
6635 if (convs->bad_p
6636 && convs->kind != ck_user
6637 && convs->kind != ck_list
6638 && convs->kind != ck_ambig
6639 && (convs->kind != ck_ref_bind
6640 || (convs->user_conv_p && next_conversion (convs)->bad_p))
6641 && (convs->kind != ck_rvalue
6642 || SCALAR_TYPE_P (totype))
6643 && convs->kind != ck_base)
6645 bool complained = false;
6646 conversion *t = convs;
6648 /* Give a helpful error if this is bad because of excess braces. */
6649 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6650 && SCALAR_TYPE_P (totype)
6651 && CONSTRUCTOR_NELTS (expr) > 0
6652 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
6654 complained = permerror (loc, "too many braces around initializer "
6655 "for %qT", totype);
6656 while (BRACE_ENCLOSED_INITIALIZER_P (expr)
6657 && CONSTRUCTOR_NELTS (expr) == 1)
6658 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6661 /* Give a helpful error if this is bad because a conversion to bool
6662 from std::nullptr_t requires direct-initialization. */
6663 if (NULLPTR_TYPE_P (TREE_TYPE (expr))
6664 && TREE_CODE (totype) == BOOLEAN_TYPE)
6665 complained = permerror (loc, "converting to %qH from %qI requires "
6666 "direct-initialization",
6667 totype, TREE_TYPE (expr));
6669 for (; t ; t = next_conversion (t))
6671 if (t->kind == ck_user && t->cand->reason)
6673 complained = permerror (loc, "invalid user-defined conversion "
6674 "from %qH to %qI", TREE_TYPE (expr),
6675 totype);
6676 if (complained)
6677 print_z_candidate (loc, "candidate is:", t->cand);
6678 expr = convert_like_real (t, expr, fn, argnum,
6679 /*issue_conversion_warnings=*/false,
6680 /*c_cast_p=*/false,
6681 complain);
6682 if (convs->kind == ck_ref_bind)
6683 expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
6684 LOOKUP_NORMAL, NULL_TREE,
6685 complain);
6686 else
6687 expr = cp_convert (totype, expr, complain);
6688 if (complained && fn)
6689 inform (DECL_SOURCE_LOCATION (fn),
6690 " initializing argument %P of %qD", argnum, fn);
6691 return expr;
6693 else if (t->kind == ck_user || !t->bad_p)
6695 expr = convert_like_real (t, expr, fn, argnum,
6696 /*issue_conversion_warnings=*/false,
6697 /*c_cast_p=*/false,
6698 complain);
6699 break;
6701 else if (t->kind == ck_ambig)
6702 return convert_like_real (t, expr, fn, argnum,
6703 /*issue_conversion_warnings=*/false,
6704 /*c_cast_p=*/false,
6705 complain);
6706 else if (t->kind == ck_identity)
6707 break;
6709 if (!complained)
6710 complained = permerror (loc, "invalid conversion from %qH to %qI",
6711 TREE_TYPE (expr), totype);
6712 if (complained && fn)
6713 inform (get_fndecl_argument_location (fn, argnum),
6714 " initializing argument %P of %qD", argnum, fn);
6716 return cp_convert (totype, expr, complain);
6719 if (issue_conversion_warnings && (complain & tf_warning))
6720 conversion_null_warnings (totype, expr, fn, argnum);
6722 switch (convs->kind)
6724 case ck_user:
6726 struct z_candidate *cand = convs->cand;
6728 if (cand == NULL)
6729 /* We chose the surrogate function from add_conv_candidate, now we
6730 actually need to build the conversion. */
6731 cand = build_user_type_conversion_1 (totype, expr,
6732 LOOKUP_NO_CONVERSION, complain);
6734 tree convfn = cand->fn;
6736 /* When converting from an init list we consider explicit
6737 constructors, but actually trying to call one is an error. */
6738 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
6739 && BRACE_ENCLOSED_INITIALIZER_P (expr)
6740 /* Unless this is for direct-list-initialization. */
6741 && !CONSTRUCTOR_IS_DIRECT_INIT (expr)
6742 /* And in C++98 a default constructor can't be explicit. */
6743 && cxx_dialect >= cxx11)
6745 if (!(complain & tf_error))
6746 return error_mark_node;
6747 location_t loc = location_of (expr);
6748 if (CONSTRUCTOR_NELTS (expr) == 0
6749 && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
6751 if (pedwarn (loc, 0, "converting to %qT from initializer list "
6752 "would use explicit constructor %qD",
6753 totype, convfn))
6754 inform (loc, "in C++11 and above a default constructor "
6755 "can be explicit");
6757 else
6758 error ("converting to %qT from initializer list would use "
6759 "explicit constructor %qD", totype, convfn);
6762 /* If we're initializing from {}, it's value-initialization. */
6763 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6764 && CONSTRUCTOR_NELTS (expr) == 0
6765 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
6767 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
6768 expr = build_value_init (totype, complain);
6769 expr = get_target_expr_sfinae (expr, complain);
6770 if (expr != error_mark_node)
6772 TARGET_EXPR_LIST_INIT_P (expr) = true;
6773 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
6775 return expr;
6778 expr = mark_rvalue_use (expr);
6780 /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
6781 any more UDCs. */
6782 expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
6783 complain);
6785 /* If this is a constructor or a function returning an aggr type,
6786 we need to build up a TARGET_EXPR. */
6787 if (DECL_CONSTRUCTOR_P (convfn))
6789 expr = build_cplus_new (totype, expr, complain);
6791 /* Remember that this was list-initialization. */
6792 if (convs->check_narrowing && expr != error_mark_node)
6793 TARGET_EXPR_LIST_INIT_P (expr) = true;
6796 return expr;
6798 case ck_identity:
6799 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
6801 int nelts = CONSTRUCTOR_NELTS (expr);
6802 if (nelts == 0)
6803 expr = build_value_init (totype, complain);
6804 else if (nelts == 1)
6805 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6806 else
6807 gcc_unreachable ();
6809 expr = mark_rvalue_use (expr);
6811 if (type_unknown_p (expr))
6812 expr = instantiate_type (totype, expr, complain);
6813 return expr;
6814 case ck_ambig:
6815 /* We leave bad_p off ck_ambig because overload resolution considers
6816 it valid, it just fails when we try to perform it. So we need to
6817 check complain here, too. */
6818 if (complain & tf_error)
6820 /* Call build_user_type_conversion again for the error. */
6821 build_user_type_conversion (totype, convs->u.expr, LOOKUP_IMPLICIT,
6822 complain);
6823 if (fn)
6824 inform (DECL_SOURCE_LOCATION (fn),
6825 " initializing argument %P of %qD", argnum, fn);
6827 return error_mark_node;
6829 case ck_list:
6831 /* Conversion to std::initializer_list<T>. */
6832 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
6833 tree new_ctor = build_constructor (init_list_type_node, NULL);
6834 unsigned len = CONSTRUCTOR_NELTS (expr);
6835 tree array, val, field;
6836 vec<constructor_elt, va_gc> *vec = NULL;
6837 unsigned ix;
6839 /* Convert all the elements. */
6840 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
6842 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
6843 false, false, complain);
6844 if (sub == error_mark_node)
6845 return sub;
6846 if (!BRACE_ENCLOSED_INITIALIZER_P (val)
6847 && !check_narrowing (TREE_TYPE (sub), val, complain))
6848 return error_mark_node;
6849 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
6850 if (!TREE_CONSTANT (sub))
6851 TREE_CONSTANT (new_ctor) = false;
6853 /* Build up the array. */
6854 elttype = cp_build_qualified_type
6855 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
6856 array = build_array_of_n_type (elttype, len);
6857 array = finish_compound_literal (array, new_ctor, complain);
6858 /* Take the address explicitly rather than via decay_conversion
6859 to avoid the error about taking the address of a temporary. */
6860 array = cp_build_addr_expr (array, complain);
6861 array = cp_convert (build_pointer_type (elttype), array, complain);
6862 if (array == error_mark_node)
6863 return error_mark_node;
6865 /* Build up the initializer_list object. */
6866 totype = complete_type (totype);
6867 field = next_initializable_field (TYPE_FIELDS (totype));
6868 CONSTRUCTOR_APPEND_ELT (vec, field, array);
6869 field = next_initializable_field (DECL_CHAIN (field));
6870 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
6871 new_ctor = build_constructor (totype, vec);
6872 return get_target_expr_sfinae (new_ctor, complain);
6875 case ck_aggr:
6876 if (TREE_CODE (totype) == COMPLEX_TYPE)
6878 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
6879 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
6880 real = perform_implicit_conversion (TREE_TYPE (totype),
6881 real, complain);
6882 imag = perform_implicit_conversion (TREE_TYPE (totype),
6883 imag, complain);
6884 expr = build2 (COMPLEX_EXPR, totype, real, imag);
6885 return expr;
6887 expr = reshape_init (totype, expr, complain);
6888 expr = get_target_expr_sfinae (digest_init (totype, expr, complain),
6889 complain);
6890 if (expr != error_mark_node)
6891 TARGET_EXPR_LIST_INIT_P (expr) = true;
6892 return expr;
6894 default:
6895 break;
6898 expr = convert_like_real (next_conversion (convs), expr, fn, argnum,
6899 convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
6900 c_cast_p,
6901 complain);
6902 if (expr == error_mark_node)
6903 return error_mark_node;
6905 switch (convs->kind)
6907 case ck_rvalue:
6908 expr = decay_conversion (expr, complain);
6909 if (expr == error_mark_node)
6911 if (complain & tf_error)
6913 maybe_print_user_conv_context (convs);
6914 if (fn)
6915 inform (DECL_SOURCE_LOCATION (fn),
6916 " initializing argument %P of %qD", argnum, fn);
6918 return error_mark_node;
6921 if (! MAYBE_CLASS_TYPE_P (totype))
6922 return expr;
6924 /* Don't introduce copies when passing arguments along to the inherited
6925 constructor. */
6926 if (current_function_decl
6927 && flag_new_inheriting_ctors
6928 && DECL_INHERITED_CTOR (current_function_decl))
6929 return expr;
6931 /* Fall through. */
6932 case ck_base:
6933 if (convs->kind == ck_base && !convs->need_temporary_p)
6935 /* We are going to bind a reference directly to a base-class
6936 subobject of EXPR. */
6937 /* Build an expression for `*((base*) &expr)'. */
6938 expr = convert_to_base (expr, totype,
6939 !c_cast_p, /*nonnull=*/true, complain);
6940 return expr;
6943 /* Copy-initialization where the cv-unqualified version of the source
6944 type is the same class as, or a derived class of, the class of the
6945 destination [is treated as direct-initialization]. [dcl.init] */
6946 flags = LOOKUP_NORMAL;
6947 if (convs->user_conv_p)
6948 /* This conversion is being done in the context of a user-defined
6949 conversion (i.e. the second step of copy-initialization), so
6950 don't allow any more. */
6951 flags |= LOOKUP_NO_CONVERSION;
6952 else
6953 flags |= LOOKUP_ONLYCONVERTING;
6954 if (convs->rvaluedness_matches_p)
6955 /* standard_conversion got LOOKUP_PREFER_RVALUE. */
6956 flags |= LOOKUP_PREFER_RVALUE;
6957 if (TREE_CODE (expr) == TARGET_EXPR
6958 && TARGET_EXPR_LIST_INIT_P (expr))
6959 /* Copy-list-initialization doesn't actually involve a copy. */
6960 return expr;
6961 expr = build_temp (expr, totype, flags, &diag_kind, complain);
6962 if (diag_kind && complain)
6964 maybe_print_user_conv_context (convs);
6965 if (fn)
6966 inform (DECL_SOURCE_LOCATION (fn),
6967 " initializing argument %P of %qD", argnum, fn);
6970 return build_cplus_new (totype, expr, complain);
6972 case ck_ref_bind:
6974 tree ref_type = totype;
6976 if (convs->bad_p && !next_conversion (convs)->bad_p)
6978 tree extype = TREE_TYPE (expr);
6979 if (TYPE_REF_IS_RVALUE (ref_type)
6980 && lvalue_p (expr))
6981 error_at (loc, "cannot bind rvalue reference of type %qH to "
6982 "lvalue of type %qI", totype, extype);
6983 else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
6984 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
6985 error_at (loc, "cannot bind non-const lvalue reference of "
6986 "type %qH to an rvalue of type %qI", totype, extype);
6987 else if (!reference_compatible_p (TREE_TYPE (totype), extype))
6988 error_at (loc, "binding reference of type %qH to %qI "
6989 "discards qualifiers", totype, extype);
6990 else
6991 gcc_unreachable ();
6992 maybe_print_user_conv_context (convs);
6993 if (fn)
6994 inform (DECL_SOURCE_LOCATION (fn),
6995 " initializing argument %P of %qD", argnum, fn);
6996 return error_mark_node;
6999 /* If necessary, create a temporary.
7001 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
7002 that need temporaries, even when their types are reference
7003 compatible with the type of reference being bound, so the
7004 upcoming call to cp_build_addr_expr doesn't fail. */
7005 if (convs->need_temporary_p
7006 || TREE_CODE (expr) == CONSTRUCTOR
7007 || TREE_CODE (expr) == VA_ARG_EXPR)
7009 /* Otherwise, a temporary of type "cv1 T1" is created and
7010 initialized from the initializer expression using the rules
7011 for a non-reference copy-initialization (8.5). */
7013 tree type = TREE_TYPE (ref_type);
7014 cp_lvalue_kind lvalue = lvalue_kind (expr);
7016 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7017 (type, next_conversion (convs)->type));
7018 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
7019 && !TYPE_REF_IS_RVALUE (ref_type))
7021 /* If the reference is volatile or non-const, we
7022 cannot create a temporary. */
7023 if (lvalue & clk_bitfield)
7024 error_at (loc, "cannot bind bitfield %qE to %qT",
7025 expr, ref_type);
7026 else if (lvalue & clk_packed)
7027 error_at (loc, "cannot bind packed field %qE to %qT",
7028 expr, ref_type);
7029 else
7030 error_at (loc, "cannot bind rvalue %qE to %qT",
7031 expr, ref_type);
7032 return error_mark_node;
7034 /* If the source is a packed field, and we must use a copy
7035 constructor, then building the target expr will require
7036 binding the field to the reference parameter to the
7037 copy constructor, and we'll end up with an infinite
7038 loop. If we can use a bitwise copy, then we'll be
7039 OK. */
7040 if ((lvalue & clk_packed)
7041 && CLASS_TYPE_P (type)
7042 && type_has_nontrivial_copy_init (type))
7044 error_at (loc, "cannot bind packed field %qE to %qT",
7045 expr, ref_type);
7046 return error_mark_node;
7048 if (lvalue & clk_bitfield)
7050 expr = convert_bitfield_to_declared_type (expr);
7051 expr = fold_convert (type, expr);
7053 expr = build_target_expr_with_type (expr, type, complain);
7056 /* Take the address of the thing to which we will bind the
7057 reference. */
7058 expr = cp_build_addr_expr (expr, complain);
7059 if (expr == error_mark_node)
7060 return error_mark_node;
7062 /* Convert it to a pointer to the type referred to by the
7063 reference. This will adjust the pointer if a derived to
7064 base conversion is being performed. */
7065 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
7066 expr, complain);
7067 /* Convert the pointer to the desired reference type. */
7068 return build_nop (ref_type, expr);
7071 case ck_lvalue:
7072 return decay_conversion (expr, complain);
7074 case ck_fnptr:
7075 /* ??? Should the address of a transaction-safe pointer point to the TM
7076 clone, and this conversion look up the primary function? */
7077 return build_nop (totype, expr);
7079 case ck_qual:
7080 /* Warn about deprecated conversion if appropriate. */
7081 string_conv_p (totype, expr, 1);
7082 break;
7084 case ck_ptr:
7085 if (convs->base_p)
7086 expr = convert_to_base (expr, totype, !c_cast_p,
7087 /*nonnull=*/false, complain);
7088 return build_nop (totype, expr);
7090 case ck_pmem:
7091 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
7092 c_cast_p, complain);
7094 default:
7095 break;
7098 if (convs->check_narrowing
7099 && !check_narrowing (totype, expr, complain))
7100 return error_mark_node;
7102 if (issue_conversion_warnings)
7103 expr = cp_convert_and_check (totype, expr, complain);
7104 else
7105 expr = cp_convert (totype, expr, complain);
7107 return expr;
7110 /* ARG is being passed to a varargs function. Perform any conversions
7111 required. Return the converted value. */
7113 tree
7114 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
7116 tree arg_type;
7117 location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
7119 /* [expr.call]
7121 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7122 standard conversions are performed. */
7123 arg = decay_conversion (arg, complain);
7124 arg_type = TREE_TYPE (arg);
7125 /* [expr.call]
7127 If the argument has integral or enumeration type that is subject
7128 to the integral promotions (_conv.prom_), or a floating point
7129 type that is subject to the floating point promotion
7130 (_conv.fpprom_), the value of the argument is converted to the
7131 promoted type before the call. */
7132 if (TREE_CODE (arg_type) == REAL_TYPE
7133 && (TYPE_PRECISION (arg_type)
7134 < TYPE_PRECISION (double_type_node))
7135 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
7137 if ((complain & tf_warning)
7138 && warn_double_promotion && !c_inhibit_evaluation_warnings)
7139 warning_at (loc, OPT_Wdouble_promotion,
7140 "implicit conversion from %qH to %qI when passing "
7141 "argument to function",
7142 arg_type, double_type_node);
7143 arg = convert_to_real_nofold (double_type_node, arg);
7145 else if (NULLPTR_TYPE_P (arg_type))
7146 arg = null_pointer_node;
7147 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
7149 if (SCOPED_ENUM_P (arg_type))
7151 tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
7152 complain);
7153 prom = cp_perform_integral_promotions (prom, complain);
7154 if (abi_version_crosses (6)
7155 && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
7156 && (complain & tf_warning))
7157 warning_at (loc, OPT_Wabi, "scoped enum %qT passed through ... as "
7158 "%qT before -fabi-version=6, %qT after", arg_type,
7159 TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
7160 if (!abi_version_at_least (6))
7161 arg = prom;
7163 else
7164 arg = cp_perform_integral_promotions (arg, complain);
7167 arg = require_complete_type_sfinae (arg, complain);
7168 arg_type = TREE_TYPE (arg);
7170 if (arg != error_mark_node
7171 /* In a template (or ill-formed code), we can have an incomplete type
7172 even after require_complete_type_sfinae, in which case we don't know
7173 whether it has trivial copy or not. */
7174 && COMPLETE_TYPE_P (arg_type)
7175 && !cp_unevaluated_operand)
7177 /* [expr.call] 5.2.2/7:
7178 Passing a potentially-evaluated argument of class type (Clause 9)
7179 with a non-trivial copy constructor or a non-trivial destructor
7180 with no corresponding parameter is conditionally-supported, with
7181 implementation-defined semantics.
7183 We support it as pass-by-invisible-reference, just like a normal
7184 value parameter.
7186 If the call appears in the context of a sizeof expression,
7187 it is not potentially-evaluated. */
7188 if (type_has_nontrivial_copy_init (arg_type)
7189 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
7191 arg = force_rvalue (arg, complain);
7192 if (complain & tf_warning)
7193 warning (OPT_Wconditionally_supported,
7194 "passing objects of non-trivially-copyable "
7195 "type %q#T through %<...%> is conditionally supported",
7196 arg_type);
7197 return cp_build_addr_expr (arg, complain);
7199 /* Build up a real lvalue-to-rvalue conversion in case the
7200 copy constructor is trivial but not callable. */
7201 else if (CLASS_TYPE_P (arg_type))
7202 force_rvalue (arg, complain);
7206 return arg;
7209 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
7211 tree
7212 build_x_va_arg (source_location loc, tree expr, tree type)
7214 if (processing_template_decl)
7216 tree r = build_min (VA_ARG_EXPR, type, expr);
7217 SET_EXPR_LOCATION (r, loc);
7218 return r;
7221 type = complete_type_or_else (type, NULL_TREE);
7223 if (expr == error_mark_node || !type)
7224 return error_mark_node;
7226 expr = mark_lvalue_use (expr);
7228 if (TREE_CODE (type) == REFERENCE_TYPE)
7230 error ("cannot receive reference type %qT through %<...%>", type);
7231 return error_mark_node;
7234 if (type_has_nontrivial_copy_init (type)
7235 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7237 /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
7238 it as pass by invisible reference. */
7239 warning_at (loc, OPT_Wconditionally_supported,
7240 "receiving objects of non-trivially-copyable type %q#T "
7241 "through %<...%> is conditionally-supported", type);
7243 tree ref = cp_build_reference_type (type, false);
7244 expr = build_va_arg (loc, expr, ref);
7245 return convert_from_reference (expr);
7248 tree ret = build_va_arg (loc, expr, type);
7249 if (CLASS_TYPE_P (type))
7250 /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
7251 know how to handle it. */
7252 ret = get_target_expr (ret);
7253 return ret;
7256 /* TYPE has been given to va_arg. Apply the default conversions which
7257 would have happened when passed via ellipsis. Return the promoted
7258 type, or the passed type if there is no change. */
7260 tree
7261 cxx_type_promotes_to (tree type)
7263 tree promote;
7265 /* Perform the array-to-pointer and function-to-pointer
7266 conversions. */
7267 type = type_decays_to (type);
7269 promote = type_promotes_to (type);
7270 if (same_type_p (type, promote))
7271 promote = type;
7273 return promote;
7276 /* ARG is a default argument expression being passed to a parameter of
7277 the indicated TYPE, which is a parameter to FN. PARMNUM is the
7278 zero-based argument number. Do any required conversions. Return
7279 the converted value. */
7281 static GTY(()) vec<tree, va_gc> *default_arg_context;
7282 void
7283 push_defarg_context (tree fn)
7284 { vec_safe_push (default_arg_context, fn); }
7286 void
7287 pop_defarg_context (void)
7288 { default_arg_context->pop (); }
7290 tree
7291 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
7292 tsubst_flags_t complain)
7294 int i;
7295 tree t;
7297 /* See through clones. */
7298 fn = DECL_ORIGIN (fn);
7299 /* And inheriting ctors. */
7300 if (flag_new_inheriting_ctors)
7301 fn = strip_inheriting_ctors (fn);
7303 /* Detect recursion. */
7304 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
7305 if (t == fn)
7307 if (complain & tf_error)
7308 error ("recursive evaluation of default argument for %q#D", fn);
7309 return error_mark_node;
7312 /* If the ARG is an unparsed default argument expression, the
7313 conversion cannot be performed. */
7314 if (TREE_CODE (arg) == DEFAULT_ARG)
7316 if (complain & tf_error)
7317 error ("call to %qD uses the default argument for parameter %P, which "
7318 "is not yet defined", fn, parmnum);
7319 return error_mark_node;
7322 push_defarg_context (fn);
7324 if (fn && DECL_TEMPLATE_INFO (fn))
7325 arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
7327 /* Due to:
7329 [dcl.fct.default]
7331 The names in the expression are bound, and the semantic
7332 constraints are checked, at the point where the default
7333 expressions appears.
7335 we must not perform access checks here. */
7336 push_deferring_access_checks (dk_no_check);
7337 /* We must make a copy of ARG, in case subsequent processing
7338 alters any part of it. */
7339 arg = break_out_target_exprs (arg);
7340 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
7341 ICR_DEFAULT_ARGUMENT, fn, parmnum,
7342 complain);
7343 arg = convert_for_arg_passing (type, arg, complain);
7344 pop_deferring_access_checks();
7346 pop_defarg_context ();
7348 return arg;
7351 /* Returns the type which will really be used for passing an argument of
7352 type TYPE. */
7354 tree
7355 type_passed_as (tree type)
7357 /* Pass classes with copy ctors by invisible reference. */
7358 if (TREE_ADDRESSABLE (type))
7360 type = build_reference_type (type);
7361 /* There are no other pointers to this temporary. */
7362 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
7364 else if (targetm.calls.promote_prototypes (type)
7365 && INTEGRAL_TYPE_P (type)
7366 && COMPLETE_TYPE_P (type)
7367 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
7368 type = integer_type_node;
7370 return type;
7373 /* Actually perform the appropriate conversion. */
7375 tree
7376 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
7378 tree bitfield_type;
7380 /* If VAL is a bitfield, then -- since it has already been converted
7381 to TYPE -- it cannot have a precision greater than TYPE.
7383 If it has a smaller precision, we must widen it here. For
7384 example, passing "int f:3;" to a function expecting an "int" will
7385 not result in any conversion before this point.
7387 If the precision is the same we must not risk widening. For
7388 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
7389 often have type "int", even though the C++ type for the field is
7390 "long long". If the value is being passed to a function
7391 expecting an "int", then no conversions will be required. But,
7392 if we call convert_bitfield_to_declared_type, the bitfield will
7393 be converted to "long long". */
7394 bitfield_type = is_bitfield_expr_with_lowered_type (val);
7395 if (bitfield_type
7396 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
7397 val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
7399 if (val == error_mark_node)
7401 /* Pass classes with copy ctors by invisible reference. */
7402 else if (TREE_ADDRESSABLE (type))
7403 val = build1 (ADDR_EXPR, build_reference_type (type), val);
7404 else if (targetm.calls.promote_prototypes (type)
7405 && INTEGRAL_TYPE_P (type)
7406 && COMPLETE_TYPE_P (type)
7407 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
7408 val = cp_perform_integral_promotions (val, complain);
7409 if (complain & tf_warning)
7411 if (warn_suggest_attribute_format)
7413 tree rhstype = TREE_TYPE (val);
7414 const enum tree_code coder = TREE_CODE (rhstype);
7415 const enum tree_code codel = TREE_CODE (type);
7416 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7417 && coder == codel
7418 && check_missing_format_attribute (type, rhstype))
7419 warning (OPT_Wsuggest_attribute_format,
7420 "argument of function call might be a candidate "
7421 "for a format attribute");
7423 maybe_warn_parm_abi (type, EXPR_LOC_OR_LOC (val, input_location));
7425 return val;
7428 /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
7429 which just decay_conversion or no conversions at all should be done.
7430 This is true for some builtins which don't act like normal functions.
7431 Return 2 if no conversions at all should be done, 1 if just
7432 decay_conversion. Return 3 for special treatment of the 3rd argument
7433 for __builtin_*_overflow_p. */
7436 magic_varargs_p (tree fn)
7438 if (flag_cilkplus && is_cilkplus_reduce_builtin (fn) != BUILT_IN_NONE)
7439 return 2;
7441 if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
7442 switch (DECL_FUNCTION_CODE (fn))
7444 case BUILT_IN_CLASSIFY_TYPE:
7445 case BUILT_IN_CONSTANT_P:
7446 case BUILT_IN_NEXT_ARG:
7447 case BUILT_IN_VA_START:
7448 return 1;
7450 case BUILT_IN_ADD_OVERFLOW_P:
7451 case BUILT_IN_SUB_OVERFLOW_P:
7452 case BUILT_IN_MUL_OVERFLOW_P:
7453 return 3;
7455 default:;
7456 return lookup_attribute ("type generic",
7457 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
7460 return 0;
7463 /* Returns the decl of the dispatcher function if FN is a function version. */
7465 tree
7466 get_function_version_dispatcher (tree fn)
7468 tree dispatcher_decl = NULL;
7470 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7471 && DECL_FUNCTION_VERSIONED (fn));
7473 gcc_assert (targetm.get_function_versions_dispatcher);
7474 dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
7476 if (dispatcher_decl == NULL)
7478 error_at (input_location, "use of multiversioned function "
7479 "without a default");
7480 return NULL;
7483 retrofit_lang_decl (dispatcher_decl);
7484 gcc_assert (dispatcher_decl != NULL);
7485 return dispatcher_decl;
7488 /* fn is a function version dispatcher that is marked used. Mark all the
7489 semantically identical function versions it will dispatch as used. */
7491 void
7492 mark_versions_used (tree fn)
7494 struct cgraph_node *node;
7495 struct cgraph_function_version_info *node_v;
7496 struct cgraph_function_version_info *it_v;
7498 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7500 node = cgraph_node::get (fn);
7501 if (node == NULL)
7502 return;
7504 gcc_assert (node->dispatcher_function);
7506 node_v = node->function_version ();
7507 if (node_v == NULL)
7508 return;
7510 /* All semantically identical versions are chained. Traverse and mark each
7511 one of them as used. */
7512 it_v = node_v->next;
7513 while (it_v != NULL)
7515 mark_used (it_v->this_node->decl);
7516 it_v = it_v->next;
7520 /* Build a call to "the copy constructor" for the type of A, even if it
7521 wouldn't be selected by normal overload resolution. Used for
7522 diagnostics. */
7524 static tree
7525 call_copy_ctor (tree a, tsubst_flags_t complain)
7527 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
7528 tree binfo = TYPE_BINFO (ctype);
7529 tree copy = get_copy_ctor (ctype, complain);
7530 copy = build_baselink (binfo, binfo, copy, NULL_TREE);
7531 tree ob = build_dummy_object (ctype);
7532 vec<tree, va_gc>* args = make_tree_vector_single (a);
7533 tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
7534 LOOKUP_NORMAL, NULL, complain);
7535 release_tree_vector (args);
7536 return r;
7539 /* Return true iff T refers to a base field. */
7541 static bool
7542 is_base_field_ref (tree t)
7544 STRIP_NOPS (t);
7545 if (TREE_CODE (t) == ADDR_EXPR)
7546 t = TREE_OPERAND (t, 0);
7547 if (TREE_CODE (t) == COMPONENT_REF)
7548 t = TREE_OPERAND (t, 1);
7549 if (TREE_CODE (t) == FIELD_DECL)
7550 return DECL_FIELD_IS_BASE (t);
7551 return false;
7554 /* We can't elide a copy from a function returning by value to a base
7555 subobject, as the callee might clobber tail padding. Return true iff this
7556 could be that case. */
7558 static bool
7559 unsafe_copy_elision_p (tree target, tree exp)
7561 /* Copy elision only happens with a TARGET_EXPR. */
7562 if (TREE_CODE (exp) != TARGET_EXPR)
7563 return false;
7564 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7565 /* It's safe to elide the copy for a class with no tail padding. */
7566 if (tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
7567 return false;
7568 /* It's safe to elide the copy if we aren't initializing a base object. */
7569 if (!is_base_field_ref (target))
7570 return false;
7571 tree init = TARGET_EXPR_INITIAL (exp);
7572 /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
7573 while (TREE_CODE (init) == COMPOUND_EXPR)
7574 init = TREE_OPERAND (init, 1);
7575 return (TREE_CODE (init) == AGGR_INIT_EXPR
7576 && !AGGR_INIT_VIA_CTOR_P (init));
7579 /* Subroutine of the various build_*_call functions. Overload resolution
7580 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
7581 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
7582 bitmask of various LOOKUP_* flags which apply to the call itself. */
7584 static tree
7585 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
7587 tree fn = cand->fn;
7588 const vec<tree, va_gc> *args = cand->args;
7589 tree first_arg = cand->first_arg;
7590 conversion **convs = cand->convs;
7591 conversion *conv;
7592 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
7593 int parmlen;
7594 tree val;
7595 int i = 0;
7596 int j = 0;
7597 unsigned int arg_index = 0;
7598 int is_method = 0;
7599 int nargs;
7600 tree *argarray;
7601 bool already_used = false;
7603 /* In a template, there is no need to perform all of the work that
7604 is normally done. We are only interested in the type of the call
7605 expression, i.e., the return type of the function. Any semantic
7606 errors will be deferred until the template is instantiated. */
7607 if (processing_template_decl)
7609 tree expr, addr;
7610 tree return_type;
7611 const tree *argarray;
7612 unsigned int nargs;
7614 if (undeduced_auto_decl (fn))
7615 mark_used (fn, complain);
7617 return_type = TREE_TYPE (TREE_TYPE (fn));
7618 nargs = vec_safe_length (args);
7619 if (first_arg == NULL_TREE)
7620 argarray = args->address ();
7621 else
7623 tree *alcarray;
7624 unsigned int ix;
7625 tree arg;
7627 ++nargs;
7628 alcarray = XALLOCAVEC (tree, nargs);
7629 alcarray[0] = build_this (first_arg);
7630 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
7631 alcarray[ix + 1] = arg;
7632 argarray = alcarray;
7635 addr = build_addr_func (fn, complain);
7636 if (addr == error_mark_node)
7637 return error_mark_node;
7638 expr = build_call_array_loc (input_location, return_type,
7639 addr, nargs, argarray);
7640 if (TREE_THIS_VOLATILE (fn) && cfun)
7641 current_function_returns_abnormally = 1;
7642 return convert_from_reference (expr);
7645 /* Give any warnings we noticed during overload resolution. */
7646 if (cand->warnings && (complain & tf_warning))
7648 struct candidate_warning *w;
7649 for (w = cand->warnings; w; w = w->next)
7650 joust (cand, w->loser, 1, complain);
7653 /* OK, we're actually calling this inherited constructor; set its deletedness
7654 appropriately. We can get away with doing this here because calling is
7655 the only way to refer to a constructor. */
7656 if (DECL_INHERITED_CTOR (fn))
7657 deduce_inheriting_ctor (fn);
7659 /* Make =delete work with SFINAE. */
7660 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7661 return error_mark_node;
7663 if (DECL_FUNCTION_MEMBER_P (fn))
7665 tree access_fn;
7666 /* If FN is a template function, two cases must be considered.
7667 For example:
7669 struct A {
7670 protected:
7671 template <class T> void f();
7673 template <class T> struct B {
7674 protected:
7675 void g();
7677 struct C : A, B<int> {
7678 using A::f; // #1
7679 using B<int>::g; // #2
7682 In case #1 where `A::f' is a member template, DECL_ACCESS is
7683 recorded in the primary template but not in its specialization.
7684 We check access of FN using its primary template.
7686 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
7687 because it is a member of class template B, DECL_ACCESS is
7688 recorded in the specialization `B<int>::g'. We cannot use its
7689 primary template because `B<T>::g' and `B<int>::g' may have
7690 different access. */
7691 if (DECL_TEMPLATE_INFO (fn)
7692 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
7693 access_fn = DECL_TI_TEMPLATE (fn);
7694 else
7695 access_fn = fn;
7696 if (!perform_or_defer_access_check (cand->access_path, access_fn,
7697 fn, complain))
7698 return error_mark_node;
7701 /* If we're checking for implicit delete, don't bother with argument
7702 conversions. */
7703 if (flags & LOOKUP_SPECULATIVE)
7705 if (DECL_DELETED_FN (fn))
7707 if (complain & tf_error)
7708 mark_used (fn);
7709 return error_mark_node;
7711 if (cand->viable == 1)
7712 return fn;
7713 else if (!(complain & tf_error))
7714 /* Reject bad conversions now. */
7715 return error_mark_node;
7716 /* else continue to get conversion error. */
7719 /* N3276 magic doesn't apply to nested calls. */
7720 int decltype_flag = (complain & tf_decltype);
7721 complain &= ~tf_decltype;
7723 /* Find maximum size of vector to hold converted arguments. */
7724 parmlen = list_length (parm);
7725 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
7726 if (parmlen > nargs)
7727 nargs = parmlen;
7728 argarray = XALLOCAVEC (tree, nargs);
7730 /* The implicit parameters to a constructor are not considered by overload
7731 resolution, and must be of the proper type. */
7732 if (DECL_CONSTRUCTOR_P (fn))
7734 tree object_arg;
7735 if (first_arg != NULL_TREE)
7737 object_arg = first_arg;
7738 first_arg = NULL_TREE;
7740 else
7742 object_arg = (*args)[arg_index];
7743 ++arg_index;
7745 argarray[j++] = build_this (object_arg);
7746 parm = TREE_CHAIN (parm);
7747 /* We should never try to call the abstract constructor. */
7748 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
7750 if (DECL_HAS_VTT_PARM_P (fn))
7752 argarray[j++] = (*args)[arg_index];
7753 ++arg_index;
7754 parm = TREE_CHAIN (parm);
7757 if (flags & LOOKUP_PREFER_RVALUE)
7759 /* The implicit move specified in 15.8.3/3 fails "...if the type of
7760 the first parameter of the selected constructor is not an rvalue
7761 reference to the object’s type (possibly cv-qualified)...." */
7762 gcc_assert (!(complain & tf_error));
7763 tree ptype = convs[0]->type;
7764 if (TREE_CODE (ptype) != REFERENCE_TYPE
7765 || !TYPE_REF_IS_RVALUE (ptype)
7766 || CONVERSION_RANK (convs[0]) > cr_exact)
7767 return error_mark_node;
7770 /* Bypass access control for 'this' parameter. */
7771 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7773 tree parmtype = TREE_VALUE (parm);
7774 tree arg = build_this (first_arg != NULL_TREE
7775 ? first_arg
7776 : (*args)[arg_index]);
7777 tree argtype = TREE_TYPE (arg);
7778 tree converted_arg;
7779 tree base_binfo;
7781 if (arg == error_mark_node)
7782 return error_mark_node;
7784 if (convs[i]->bad_p)
7786 if (complain & tf_error)
7788 if (permerror (input_location, "passing %qT as %<this%> "
7789 "argument discards qualifiers",
7790 TREE_TYPE (argtype)))
7791 inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
7793 else
7794 return error_mark_node;
7797 /* See if the function member or the whole class type is declared
7798 final and the call can be devirtualized. */
7799 if (DECL_FINAL_P (fn)
7800 || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
7801 flags |= LOOKUP_NONVIRTUAL;
7803 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
7804 X is called for an object that is not of type X, or of a type
7805 derived from X, the behavior is undefined.
7807 So we can assume that anything passed as 'this' is non-null, and
7808 optimize accordingly. */
7809 gcc_assert (TYPE_PTR_P (parmtype));
7810 /* Convert to the base in which the function was declared. */
7811 gcc_assert (cand->conversion_path != NULL_TREE);
7812 converted_arg = build_base_path (PLUS_EXPR,
7813 arg,
7814 cand->conversion_path,
7815 1, complain);
7816 /* Check that the base class is accessible. */
7817 if (!accessible_base_p (TREE_TYPE (argtype),
7818 BINFO_TYPE (cand->conversion_path), true))
7820 if (complain & tf_error)
7821 error ("%qT is not an accessible base of %qT",
7822 BINFO_TYPE (cand->conversion_path),
7823 TREE_TYPE (argtype));
7824 else
7825 return error_mark_node;
7827 /* If fn was found by a using declaration, the conversion path
7828 will be to the derived class, not the base declaring fn. We
7829 must convert from derived to base. */
7830 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
7831 TREE_TYPE (parmtype), ba_unique,
7832 NULL, complain);
7833 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
7834 base_binfo, 1, complain);
7836 argarray[j++] = converted_arg;
7837 parm = TREE_CHAIN (parm);
7838 if (first_arg != NULL_TREE)
7839 first_arg = NULL_TREE;
7840 else
7841 ++arg_index;
7842 ++i;
7843 is_method = 1;
7846 gcc_assert (first_arg == NULL_TREE);
7847 for (; arg_index < vec_safe_length (args) && parm;
7848 parm = TREE_CHAIN (parm), ++arg_index, ++i)
7850 tree type = TREE_VALUE (parm);
7851 tree arg = (*args)[arg_index];
7852 bool conversion_warning = true;
7854 conv = convs[i];
7856 /* If the argument is NULL and used to (implicitly) instantiate a
7857 template function (and bind one of the template arguments to
7858 the type of 'long int'), we don't want to warn about passing NULL
7859 to non-pointer argument.
7860 For example, if we have this template function:
7862 template<typename T> void func(T x) {}
7864 we want to warn (when -Wconversion is enabled) in this case:
7866 void foo() {
7867 func<int>(NULL);
7870 but not in this case:
7872 void foo() {
7873 func(NULL);
7876 if (arg == null_node
7877 && DECL_TEMPLATE_INFO (fn)
7878 && cand->template_decl
7879 && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
7880 conversion_warning = false;
7882 /* Warn about initializer_list deduction that isn't currently in the
7883 working draft. */
7884 if (cxx_dialect > cxx98
7885 && flag_deduce_init_list
7886 && cand->template_decl
7887 && is_std_init_list (non_reference (type))
7888 && BRACE_ENCLOSED_INITIALIZER_P (arg))
7890 tree tmpl = TI_TEMPLATE (cand->template_decl);
7891 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
7892 tree patparm = get_pattern_parm (realparm, tmpl);
7893 tree pattype = TREE_TYPE (patparm);
7894 if (PACK_EXPANSION_P (pattype))
7895 pattype = PACK_EXPANSION_PATTERN (pattype);
7896 pattype = non_reference (pattype);
7898 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
7899 && (cand->explicit_targs == NULL_TREE
7900 || (TREE_VEC_LENGTH (cand->explicit_targs)
7901 <= TEMPLATE_TYPE_IDX (pattype))))
7903 pedwarn (input_location, 0, "deducing %qT as %qT",
7904 non_reference (TREE_TYPE (patparm)),
7905 non_reference (type));
7906 pedwarn (DECL_SOURCE_LOCATION (cand->fn), 0,
7907 " in call to %qD", cand->fn);
7908 pedwarn (input_location, 0,
7909 " (you can disable this with -fno-deduce-init-list)");
7913 /* Set user_conv_p on the argument conversions, so rvalue/base handling
7914 knows not to allow any more UDCs. This needs to happen after we
7915 process cand->warnings. */
7916 if (flags & LOOKUP_NO_CONVERSION)
7917 conv->user_conv_p = true;
7919 tsubst_flags_t arg_complain = complain & (~tf_no_cleanup);
7920 if (!conversion_warning)
7921 arg_complain &= ~tf_warning;
7923 val = convert_like_with_context (conv, arg, fn, i - is_method,
7924 arg_complain);
7925 val = convert_for_arg_passing (type, val, arg_complain);
7927 if (val == error_mark_node)
7928 return error_mark_node;
7929 else
7930 argarray[j++] = val;
7933 /* Default arguments */
7934 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
7936 if (TREE_VALUE (parm) == error_mark_node)
7937 return error_mark_node;
7938 val = convert_default_arg (TREE_VALUE (parm),
7939 TREE_PURPOSE (parm),
7940 fn, i - is_method,
7941 complain);
7942 if (val == error_mark_node)
7943 return error_mark_node;
7944 argarray[j++] = val;
7947 /* Ellipsis */
7948 int magic = magic_varargs_p (fn);
7949 for (; arg_index < vec_safe_length (args); ++arg_index)
7951 tree a = (*args)[arg_index];
7952 if ((magic == 3 && arg_index == 2) || magic == 2)
7954 /* Do no conversions for certain magic varargs. */
7955 a = mark_type_use (a);
7956 if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
7957 return error_mark_node;
7959 else if (magic != 0)
7960 /* For other magic varargs only do decay_conversion. */
7961 a = decay_conversion (a, complain);
7962 else if (DECL_CONSTRUCTOR_P (fn)
7963 && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
7964 TREE_TYPE (a)))
7966 /* Avoid infinite recursion trying to call A(...). */
7967 if (complain & tf_error)
7968 /* Try to call the actual copy constructor for a good error. */
7969 call_copy_ctor (a, complain);
7970 return error_mark_node;
7972 else
7973 a = convert_arg_to_ellipsis (a, complain);
7974 if (a == error_mark_node)
7975 return error_mark_node;
7976 argarray[j++] = a;
7979 gcc_assert (j <= nargs);
7980 nargs = j;
7982 /* Avoid to do argument-transformation, if warnings for format, and for
7983 nonnull are disabled. Just in case that at least one of them is active
7984 the check_function_arguments function might warn about something. */
7986 bool warned_p = false;
7987 if (warn_nonnull
7988 || warn_format
7989 || warn_suggest_attribute_format
7990 || warn_restrict)
7992 tree *fargs = (!nargs ? argarray
7993 : (tree *) alloca (nargs * sizeof (tree)));
7994 for (j = 0; j < nargs; j++)
7995 fargs[j] = maybe_constant_value (argarray[j]);
7997 warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
7998 nargs, fargs, NULL);
8001 if (DECL_INHERITED_CTOR (fn))
8003 /* Check for passing ellipsis arguments to an inherited constructor. We
8004 could handle this by open-coding the inherited constructor rather than
8005 defining it, but let's not bother now. */
8006 if (!cp_unevaluated_operand
8007 && cand->num_convs
8008 && cand->convs[cand->num_convs-1]->ellipsis_p)
8010 if (complain & tf_error)
8012 sorry ("passing arguments to ellipsis of inherited constructor "
8013 "%qD", cand->fn);
8014 inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
8016 return error_mark_node;
8019 /* A base constructor inheriting from a virtual base doesn't get the
8020 inherited arguments, just this and __vtt. */
8021 if (ctor_omit_inherited_parms (fn))
8022 nargs = 2;
8025 /* Avoid actually calling copy constructors and copy assignment operators,
8026 if possible. */
8028 if (! flag_elide_constructors)
8029 /* Do things the hard way. */;
8030 else if (cand->num_convs == 1
8031 && (DECL_COPY_CONSTRUCTOR_P (fn)
8032 || DECL_MOVE_CONSTRUCTOR_P (fn))
8033 /* It's unsafe to elide the constructor when handling
8034 a noexcept-expression, it may evaluate to the wrong
8035 value (c++/53025). */
8036 && cp_noexcept_operand == 0)
8038 tree targ;
8039 tree arg = argarray[num_artificial_parms_for (fn)];
8040 tree fa;
8041 bool trivial = trivial_fn_p (fn);
8043 /* Pull out the real argument, disregarding const-correctness. */
8044 targ = arg;
8045 /* Strip the reference binding for the constructor parameter. */
8046 if (CONVERT_EXPR_P (targ)
8047 && TREE_CODE (TREE_TYPE (targ)) == REFERENCE_TYPE)
8048 targ = TREE_OPERAND (targ, 0);
8049 /* But don't strip any other reference bindings; binding a temporary to a
8050 reference prevents copy elision. */
8051 while ((CONVERT_EXPR_P (targ)
8052 && TREE_CODE (TREE_TYPE (targ)) != REFERENCE_TYPE)
8053 || TREE_CODE (targ) == NON_LVALUE_EXPR)
8054 targ = TREE_OPERAND (targ, 0);
8055 if (TREE_CODE (targ) == ADDR_EXPR)
8057 targ = TREE_OPERAND (targ, 0);
8058 if (!same_type_ignoring_top_level_qualifiers_p
8059 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
8060 targ = NULL_TREE;
8062 else
8063 targ = NULL_TREE;
8065 if (targ)
8066 arg = targ;
8067 else
8068 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
8070 /* In C++17 we shouldn't be copying a TARGET_EXPR except into a base
8071 subobject. */
8072 if (CHECKING_P && cxx_dialect >= cxx17)
8073 gcc_assert (TREE_CODE (arg) != TARGET_EXPR
8074 /* It's from binding the ref parm to a packed field. */
8075 || convs[0]->need_temporary_p
8076 || seen_error ()
8077 /* See unsafe_copy_elision_p. */
8078 || DECL_BASE_CONSTRUCTOR_P (fn));
8080 /* [class.copy]: the copy constructor is implicitly defined even if
8081 the implementation elided its use. */
8082 if (!trivial || DECL_DELETED_FN (fn))
8084 if (!mark_used (fn, complain) && !(complain & tf_error))
8085 return error_mark_node;
8086 already_used = true;
8089 /* If we're creating a temp and we already have one, don't create a
8090 new one. If we're not creating a temp but we get one, use
8091 INIT_EXPR to collapse the temp into our target. Otherwise, if the
8092 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
8093 temp or an INIT_EXPR otherwise. */
8094 fa = argarray[0];
8095 if (is_dummy_object (fa))
8097 if (TREE_CODE (arg) == TARGET_EXPR)
8098 return arg;
8099 else if (trivial)
8100 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
8102 else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
8103 && !unsafe_copy_elision_p (fa, arg))
8105 tree to = cp_stabilize_reference (cp_build_indirect_ref (fa,
8106 RO_NULL,
8107 complain));
8109 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
8110 return val;
8113 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
8114 && trivial_fn_p (fn)
8115 && !DECL_DELETED_FN (fn))
8117 tree to = cp_stabilize_reference
8118 (cp_build_indirect_ref (argarray[0], RO_NULL, complain));
8119 tree type = TREE_TYPE (to);
8120 tree as_base = CLASSTYPE_AS_BASE (type);
8121 tree arg = argarray[1];
8123 if (is_really_empty_class (type))
8125 /* Avoid copying empty classes. */
8126 val = build2 (COMPOUND_EXPR, type, arg, to);
8127 TREE_NO_WARNING (val) = 1;
8129 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
8131 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
8132 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
8133 /* Handle NSDMI that refer to the object being initialized. */
8134 replace_placeholders (arg, to);
8136 else
8138 /* We must only copy the non-tail padding parts. */
8139 tree arg0, arg2, t;
8140 tree array_type, alias_set;
8142 arg2 = TYPE_SIZE_UNIT (as_base);
8143 arg0 = cp_build_addr_expr (to, complain);
8145 array_type = build_array_type (unsigned_char_type_node,
8146 build_index_type
8147 (size_binop (MINUS_EXPR,
8148 arg2, size_int (1))));
8149 alias_set = build_int_cst (build_pointer_type (type), 0);
8150 t = build2 (MODIFY_EXPR, void_type_node,
8151 build2 (MEM_REF, array_type, arg0, alias_set),
8152 build2 (MEM_REF, array_type, arg, alias_set));
8153 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
8154 TREE_NO_WARNING (val) = 1;
8157 return val;
8159 else if (!DECL_DELETED_FN (fn)
8160 && trivial_fn_p (fn))
8162 if (DECL_DESTRUCTOR_P (fn))
8163 return fold_convert (void_type_node, argarray[0]);
8164 else if (default_ctor_p (fn))
8166 if (is_dummy_object (argarray[0]))
8167 return force_target_expr (DECL_CONTEXT (fn), void_node, 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, cp_assignment_operator_id (NOP_EXPR));
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)
8781 || name == cp_assignment_operator_id (NOP_EXPR));
8782 if (TYPE_P (binfo))
8784 /* Resolve the name. */
8785 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
8786 return error_mark_node;
8788 binfo = TYPE_BINFO (binfo);
8791 gcc_assert (binfo != NULL_TREE);
8793 class_type = BINFO_TYPE (binfo);
8795 /* Handle the special case where INSTANCE is NULL_TREE. */
8796 if (name == complete_ctor_identifier && !instance)
8797 instance = build_dummy_object (class_type);
8798 else
8800 if (IDENTIFIER_DTOR_P (name))
8801 gcc_assert (args == NULL || vec_safe_is_empty (*args));
8803 /* Convert to the base class, if necessary. */
8804 if (!same_type_ignoring_top_level_qualifiers_p
8805 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
8807 if (name != cp_assignment_operator_id (NOP_EXPR))
8808 /* For constructors and destructors, either the base is
8809 non-virtual, or it is virtual but we are doing the
8810 conversion from a constructor or destructor for the
8811 complete object. In either case, we can convert
8812 statically. */
8813 instance = convert_to_base_statically (instance, binfo);
8814 else
8815 /* However, for assignment operators, we must convert
8816 dynamically if the base is virtual. */
8817 instance = build_base_path (PLUS_EXPR, instance,
8818 binfo, /*nonnull=*/1, complain);
8822 gcc_assert (instance != NULL_TREE);
8824 /* In C++17, "If the initializer expression is a prvalue and the
8825 cv-unqualified version of the source type is the same class as the class
8826 of the destination, the initializer expression is used to initialize the
8827 destination object." Handle that here to avoid doing overload
8828 resolution. */
8829 if (cxx_dialect >= cxx17
8830 && args && vec_safe_length (*args) == 1
8831 && name == complete_ctor_identifier)
8833 tree arg = (**args)[0];
8835 /* FIXME P0135 doesn't say how to handle direct initialization from a
8836 type with a suitable conversion operator. Let's handle it like
8837 copy-initialization, but allowing explict conversions. */
8838 tsubst_flags_t sub_complain = tf_warning;
8839 if (!is_dummy_object (instance))
8840 /* If we're using this to initialize a non-temporary object, don't
8841 require the destructor to be accessible. */
8842 sub_complain |= tf_no_cleanup;
8843 if (!reference_related_p (class_type, TREE_TYPE (arg)))
8844 arg = perform_implicit_conversion_flags (class_type, arg,
8845 sub_complain,
8846 flags);
8847 if ((TREE_CODE (arg) == TARGET_EXPR
8848 || TREE_CODE (arg) == CONSTRUCTOR)
8849 && (same_type_ignoring_top_level_qualifiers_p
8850 (class_type, TREE_TYPE (arg))))
8852 if (is_dummy_object (instance))
8853 return arg;
8854 if ((complain & tf_error)
8855 && (flags & LOOKUP_DELEGATING_CONS))
8856 check_self_delegation (arg);
8857 /* Avoid change of behavior on Wunused-var-2.C. */
8858 instance = mark_lvalue_use (instance);
8859 return build2 (INIT_EXPR, class_type, instance, arg);
8863 fns = lookup_fnfields (binfo, name, 1);
8865 /* When making a call to a constructor or destructor for a subobject
8866 that uses virtual base classes, pass down a pointer to a VTT for
8867 the subobject. */
8868 if ((name == base_ctor_identifier
8869 || name == base_dtor_identifier)
8870 && CLASSTYPE_VBASECLASSES (class_type))
8872 tree vtt;
8873 tree sub_vtt;
8875 /* If the current function is a complete object constructor
8876 or destructor, then we fetch the VTT directly.
8877 Otherwise, we look it up using the VTT we were given. */
8878 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
8879 vtt = decay_conversion (vtt, complain);
8880 if (vtt == error_mark_node)
8881 return error_mark_node;
8882 vtt = build_if_in_charge (vtt, current_vtt_parm);
8883 if (BINFO_SUBVTT_INDEX (binfo))
8884 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
8885 else
8886 sub_vtt = vtt;
8888 if (args == NULL)
8890 allocated = make_tree_vector ();
8891 args = &allocated;
8894 vec_safe_insert (*args, 0, sub_vtt);
8897 ret = build_new_method_call (instance, fns, args,
8898 TYPE_BINFO (BINFO_TYPE (binfo)),
8899 flags, /*fn=*/NULL,
8900 complain);
8902 if (allocated != NULL)
8903 release_tree_vector (allocated);
8905 if ((complain & tf_error)
8906 && (flags & LOOKUP_DELEGATING_CONS)
8907 && name == complete_ctor_identifier)
8908 check_self_delegation (ret);
8910 return ret;
8913 /* Return the NAME, as a C string. The NAME indicates a function that
8914 is a member of TYPE. *FREE_P is set to true if the caller must
8915 free the memory returned.
8917 Rather than go through all of this, we should simply set the names
8918 of constructors and destructors appropriately, and dispense with
8919 ctor_identifier, dtor_identifier, etc. */
8921 static char *
8922 name_as_c_string (tree name, tree type, bool *free_p)
8924 const char *pretty_name;
8926 /* Assume that we will not allocate memory. */
8927 *free_p = false;
8928 /* Constructors and destructors are special. */
8929 if (IDENTIFIER_CDTOR_P (name))
8931 pretty_name
8932 = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
8933 /* For a destructor, add the '~'. */
8934 if (IDENTIFIER_DTOR_P (name))
8936 pretty_name = concat ("~", pretty_name, NULL);
8937 /* Remember that we need to free the memory allocated. */
8938 *free_p = true;
8941 else if (IDENTIFIER_CONV_OP_P (name))
8943 pretty_name = concat ("operator ",
8944 type_as_string_translate (TREE_TYPE (name),
8945 TFF_PLAIN_IDENTIFIER),
8946 NULL);
8947 /* Remember that we need to free the memory allocated. */
8948 *free_p = true;
8950 else
8951 pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
8953 return CONST_CAST (char *, pretty_name);
8956 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
8957 be set, upon return, to the function called. ARGS may be NULL.
8958 This may change ARGS. */
8960 static tree
8961 build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
8962 tree conversion_path, int flags,
8963 tree *fn_p, tsubst_flags_t complain)
8965 struct z_candidate *candidates = 0, *cand;
8966 tree explicit_targs = NULL_TREE;
8967 tree basetype = NULL_TREE;
8968 tree access_binfo, binfo;
8969 tree optype;
8970 tree first_mem_arg = NULL_TREE;
8971 tree name;
8972 bool skip_first_for_error;
8973 vec<tree, va_gc> *user_args;
8974 tree call;
8975 tree fn;
8976 int template_only = 0;
8977 bool any_viable_p;
8978 tree orig_instance;
8979 tree orig_fns;
8980 vec<tree, va_gc> *orig_args = NULL;
8981 void *p;
8983 gcc_assert (instance != NULL_TREE);
8985 /* We don't know what function we're going to call, yet. */
8986 if (fn_p)
8987 *fn_p = NULL_TREE;
8989 if (error_operand_p (instance)
8990 || !fns || error_operand_p (fns))
8991 return error_mark_node;
8993 if (!BASELINK_P (fns))
8995 if (complain & tf_error)
8996 error ("call to non-function %qD", fns);
8997 return error_mark_node;
9000 orig_instance = instance;
9001 orig_fns = fns;
9003 /* Dismantle the baselink to collect all the information we need. */
9004 if (!conversion_path)
9005 conversion_path = BASELINK_BINFO (fns);
9006 access_binfo = BASELINK_ACCESS_BINFO (fns);
9007 binfo = BASELINK_BINFO (fns);
9008 optype = BASELINK_OPTYPE (fns);
9009 fns = BASELINK_FUNCTIONS (fns);
9010 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
9012 explicit_targs = TREE_OPERAND (fns, 1);
9013 fns = TREE_OPERAND (fns, 0);
9014 template_only = 1;
9016 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
9017 || TREE_CODE (fns) == TEMPLATE_DECL
9018 || TREE_CODE (fns) == OVERLOAD);
9019 fn = OVL_FIRST (fns);
9020 name = DECL_NAME (fn);
9022 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
9023 gcc_assert (CLASS_TYPE_P (basetype));
9025 if (processing_template_decl)
9027 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
9028 instance = build_non_dependent_expr (instance);
9029 if (args != NULL)
9030 make_args_non_dependent (*args);
9033 user_args = args == NULL ? NULL : *args;
9034 /* Under DR 147 A::A() is an invalid constructor call,
9035 not a functional cast. */
9036 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
9038 if (! (complain & tf_error))
9039 return error_mark_node;
9041 basetype = DECL_CONTEXT (fn);
9042 name = constructor_name (basetype);
9043 if (permerror (input_location,
9044 "cannot call constructor %<%T::%D%> directly",
9045 basetype, name))
9046 inform (input_location, "for a function-style cast, remove the "
9047 "redundant %<::%D%>", name);
9048 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
9049 complain);
9050 return call;
9053 /* Process the argument list. */
9054 if (args != NULL && *args != NULL)
9056 *args = resolve_args (*args, complain);
9057 if (*args == NULL)
9058 return error_mark_node;
9061 /* Consider the object argument to be used even if we end up selecting a
9062 static member function. */
9063 instance = mark_type_use (instance);
9066 /* Figure out whether to skip the first argument for the error
9067 message we will display to users if an error occurs. We don't
9068 want to display any compiler-generated arguments. The "this"
9069 pointer hasn't been added yet. However, we must remove the VTT
9070 pointer if this is a call to a base-class constructor or
9071 destructor. */
9072 skip_first_for_error = false;
9073 if (IDENTIFIER_CDTOR_P (name))
9075 /* Callers should explicitly indicate whether they want to ctor
9076 the complete object or just the part without virtual bases. */
9077 gcc_assert (name != ctor_identifier);
9079 /* Remove the VTT pointer, if present. */
9080 if ((name == base_ctor_identifier || name == base_dtor_identifier)
9081 && CLASSTYPE_VBASECLASSES (basetype))
9082 skip_first_for_error = true;
9084 /* It's OK to call destructors and constructors on cv-qualified
9085 objects. Therefore, convert the INSTANCE to the unqualified
9086 type, if necessary. */
9087 if (!same_type_p (basetype, TREE_TYPE (instance)))
9089 instance = build_this (instance);
9090 instance = build_nop (build_pointer_type (basetype), instance);
9091 instance = build_fold_indirect_ref (instance);
9094 else
9095 gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
9097 /* For the overload resolution we need to find the actual `this`
9098 that would be captured if the call turns out to be to a
9099 non-static member function. Do not actually capture it at this
9100 point. */
9101 if (DECL_CONSTRUCTOR_P (fn))
9102 /* Constructors don't use the enclosing 'this'. */
9103 first_mem_arg = instance;
9104 else
9105 first_mem_arg = maybe_resolve_dummy (instance, false);
9107 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9108 p = conversion_obstack_alloc (0);
9110 /* The number of arguments artificial parms in ARGS; we subtract one because
9111 there's no 'this' in ARGS. */
9112 unsigned skip = num_artificial_parms_for (fn) - 1;
9114 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
9115 initializer, not T({ }). */
9116 if (DECL_CONSTRUCTOR_P (fn)
9117 && vec_safe_length (user_args) > skip
9118 && DIRECT_LIST_INIT_P ((*user_args)[skip]))
9120 tree init_list = (*user_args)[skip];
9121 tree init = NULL_TREE;
9123 gcc_assert (user_args->length () == skip + 1
9124 && !(flags & LOOKUP_ONLYCONVERTING));
9126 /* If the initializer list has no elements and T is a class type with
9127 a default constructor, the object is value-initialized. Handle
9128 this here so we don't need to handle it wherever we use
9129 build_special_member_call. */
9130 if (CONSTRUCTOR_NELTS (init_list) == 0
9131 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
9132 /* For a user-provided default constructor, use the normal
9133 mechanisms so that protected access works. */
9134 && type_has_non_user_provided_default_constructor (basetype)
9135 && !processing_template_decl)
9136 init = build_value_init (basetype, complain);
9138 /* If BASETYPE is an aggregate, we need to do aggregate
9139 initialization. */
9140 else if (CP_AGGREGATE_TYPE_P (basetype))
9142 init = reshape_init (basetype, init_list, complain);
9143 init = digest_init (basetype, init, complain);
9146 if (init)
9148 if (is_dummy_object (instance))
9149 return get_target_expr_sfinae (init, complain);
9150 init = build2 (INIT_EXPR, TREE_TYPE (instance), instance, init);
9151 TREE_SIDE_EFFECTS (init) = true;
9152 return init;
9155 /* Otherwise go ahead with overload resolution. */
9156 add_list_candidates (fns, first_mem_arg, user_args,
9157 basetype, explicit_targs, template_only,
9158 conversion_path, access_binfo, flags,
9159 &candidates, complain);
9161 else
9162 add_candidates (fns, first_mem_arg, user_args, optype,
9163 explicit_targs, template_only, conversion_path,
9164 access_binfo, flags, &candidates, complain);
9166 any_viable_p = false;
9167 candidates = splice_viable (candidates, false, &any_viable_p);
9169 if (!any_viable_p)
9171 if (complain & tf_error)
9173 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
9174 cxx_incomplete_type_error (instance, basetype);
9175 else if (optype)
9176 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
9177 basetype, optype, build_tree_list_vec (user_args),
9178 TREE_TYPE (instance));
9179 else
9181 tree arglist = build_tree_list_vec (user_args);
9182 tree errname = name;
9183 bool twiddle = false;
9184 if (IDENTIFIER_CDTOR_P (errname))
9186 twiddle = IDENTIFIER_DTOR_P (errname);
9187 errname = constructor_name (basetype);
9189 if (explicit_targs)
9190 errname = lookup_template_function (errname, explicit_targs);
9191 if (skip_first_for_error)
9192 arglist = TREE_CHAIN (arglist);
9193 error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
9194 basetype, &"~"[!twiddle], errname, arglist,
9195 TREE_TYPE (instance));
9197 print_z_candidates (location_of (name), candidates);
9199 call = error_mark_node;
9201 else
9203 cand = tourney (candidates, complain);
9204 if (cand == 0)
9206 char *pretty_name;
9207 bool free_p;
9208 tree arglist;
9210 if (complain & tf_error)
9212 pretty_name = name_as_c_string (name, basetype, &free_p);
9213 arglist = build_tree_list_vec (user_args);
9214 if (skip_first_for_error)
9215 arglist = TREE_CHAIN (arglist);
9216 if (!any_strictly_viable (candidates))
9217 error ("no matching function for call to %<%s(%A)%>",
9218 pretty_name, arglist);
9219 else
9220 error ("call of overloaded %<%s(%A)%> is ambiguous",
9221 pretty_name, arglist);
9222 print_z_candidates (location_of (name), candidates);
9223 if (free_p)
9224 free (pretty_name);
9226 call = error_mark_node;
9228 else
9230 fn = cand->fn;
9231 call = NULL_TREE;
9233 if (!(flags & LOOKUP_NONVIRTUAL)
9234 && DECL_PURE_VIRTUAL_P (fn)
9235 && instance == current_class_ref
9236 && (complain & tf_warning))
9238 /* This is not an error, it is runtime undefined
9239 behavior. */
9240 if (!current_function_decl)
9241 warning (0, "pure virtual %q#D called from "
9242 "non-static data member initializer", fn);
9243 else if (DECL_CONSTRUCTOR_P (current_function_decl)
9244 || DECL_DESTRUCTOR_P (current_function_decl))
9245 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
9246 ? G_("pure virtual %q#D called from constructor")
9247 : G_("pure virtual %q#D called from destructor")),
9248 fn);
9251 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
9252 && !DECL_CONSTRUCTOR_P (fn)
9253 && is_dummy_object (instance))
9255 instance = maybe_resolve_dummy (instance, true);
9256 if (instance == error_mark_node)
9257 call = error_mark_node;
9258 else if (!is_dummy_object (instance))
9260 /* We captured 'this' in the current lambda now that
9261 we know we really need it. */
9262 cand->first_arg = instance;
9264 else if (any_dependent_bases_p ())
9265 /* We can't tell until instantiation time whether we can use
9266 *this as the implicit object argument. */;
9267 else
9269 if (complain & tf_error)
9270 error ("cannot call member function %qD without object",
9271 fn);
9272 call = error_mark_node;
9276 if (call != error_mark_node)
9278 /* Optimize away vtable lookup if we know that this
9279 function can't be overridden. We need to check if
9280 the context and the type where we found fn are the same,
9281 actually FN might be defined in a different class
9282 type because of a using-declaration. In this case, we
9283 do not want to perform a non-virtual call. */
9284 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
9285 && same_type_ignoring_top_level_qualifiers_p
9286 (DECL_CONTEXT (fn), BINFO_TYPE (binfo))
9287 && resolves_to_fixed_type_p (instance, 0))
9288 flags |= LOOKUP_NONVIRTUAL;
9289 if (explicit_targs)
9290 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
9291 /* Now we know what function is being called. */
9292 if (fn_p)
9293 *fn_p = fn;
9294 /* Build the actual CALL_EXPR. */
9295 call = build_over_call (cand, flags, complain);
9296 /* In an expression of the form `a->f()' where `f' turns
9297 out to be a static member function, `a' is
9298 none-the-less evaluated. */
9299 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
9300 && !is_dummy_object (instance)
9301 && TREE_SIDE_EFFECTS (instance))
9302 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
9303 instance, call);
9304 else if (call != error_mark_node
9305 && DECL_DESTRUCTOR_P (cand->fn)
9306 && !VOID_TYPE_P (TREE_TYPE (call)))
9307 /* An explicit call of the form "x->~X()" has type
9308 "void". However, on platforms where destructors
9309 return "this" (i.e., those where
9310 targetm.cxx.cdtor_returns_this is true), such calls
9311 will appear to have a return value of pointer type
9312 to the low-level call machinery. We do not want to
9313 change the low-level machinery, since we want to be
9314 able to optimize "delete f()" on such platforms as
9315 "operator delete(~X(f()))" (rather than generating
9316 "t = f(), ~X(t), operator delete (t)"). */
9317 call = build_nop (void_type_node, call);
9322 if (processing_template_decl && call != error_mark_node)
9324 bool cast_to_void = false;
9326 if (TREE_CODE (call) == COMPOUND_EXPR)
9327 call = TREE_OPERAND (call, 1);
9328 else if (TREE_CODE (call) == NOP_EXPR)
9330 cast_to_void = true;
9331 call = TREE_OPERAND (call, 0);
9333 if (INDIRECT_REF_P (call))
9334 call = TREE_OPERAND (call, 0);
9335 call = (build_min_non_dep_call_vec
9336 (call,
9337 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
9338 orig_instance, orig_fns, NULL_TREE),
9339 orig_args));
9340 SET_EXPR_LOCATION (call, input_location);
9341 call = convert_from_reference (call);
9342 if (cast_to_void)
9343 call = build_nop (void_type_node, call);
9346 /* Free all the conversions we allocated. */
9347 obstack_free (&conversion_obstack, p);
9349 if (orig_args != NULL)
9350 release_tree_vector (orig_args);
9352 return call;
9355 /* Wrapper for above. */
9357 tree
9358 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
9359 tree conversion_path, int flags,
9360 tree *fn_p, tsubst_flags_t complain)
9362 tree ret;
9363 bool subtime = timevar_cond_start (TV_OVERLOAD);
9364 ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
9365 fn_p, complain);
9366 timevar_cond_stop (TV_OVERLOAD, subtime);
9367 return ret;
9370 /* Returns true iff standard conversion sequence ICS1 is a proper
9371 subsequence of ICS2. */
9373 static bool
9374 is_subseq (conversion *ics1, conversion *ics2)
9376 /* We can assume that a conversion of the same code
9377 between the same types indicates a subsequence since we only get
9378 here if the types we are converting from are the same. */
9380 while (ics1->kind == ck_rvalue
9381 || ics1->kind == ck_lvalue)
9382 ics1 = next_conversion (ics1);
9384 while (1)
9386 while (ics2->kind == ck_rvalue
9387 || ics2->kind == ck_lvalue)
9388 ics2 = next_conversion (ics2);
9390 if (ics2->kind == ck_user
9391 || ics2->kind == ck_ambig
9392 || ics2->kind == ck_aggr
9393 || ics2->kind == ck_list
9394 || ics2->kind == ck_identity)
9395 /* At this point, ICS1 cannot be a proper subsequence of
9396 ICS2. We can get a USER_CONV when we are comparing the
9397 second standard conversion sequence of two user conversion
9398 sequences. */
9399 return false;
9401 ics2 = next_conversion (ics2);
9403 while (ics2->kind == ck_rvalue
9404 || ics2->kind == ck_lvalue)
9405 ics2 = next_conversion (ics2);
9407 if (ics2->kind == ics1->kind
9408 && same_type_p (ics2->type, ics1->type)
9409 && (ics1->kind == ck_identity
9410 || same_type_p (next_conversion (ics2)->type,
9411 next_conversion (ics1)->type)))
9412 return true;
9416 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
9417 be any _TYPE nodes. */
9419 bool
9420 is_properly_derived_from (tree derived, tree base)
9422 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
9423 return false;
9425 /* We only allow proper derivation here. The DERIVED_FROM_P macro
9426 considers every class derived from itself. */
9427 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
9428 && DERIVED_FROM_P (base, derived));
9431 /* We build the ICS for an implicit object parameter as a pointer
9432 conversion sequence. However, such a sequence should be compared
9433 as if it were a reference conversion sequence. If ICS is the
9434 implicit conversion sequence for an implicit object parameter,
9435 modify it accordingly. */
9437 static void
9438 maybe_handle_implicit_object (conversion **ics)
9440 if ((*ics)->this_p)
9442 /* [over.match.funcs]
9444 For non-static member functions, the type of the
9445 implicit object parameter is "reference to cv X"
9446 where X is the class of which the function is a
9447 member and cv is the cv-qualification on the member
9448 function declaration. */
9449 conversion *t = *ics;
9450 tree reference_type;
9452 /* The `this' parameter is a pointer to a class type. Make the
9453 implicit conversion talk about a reference to that same class
9454 type. */
9455 reference_type = TREE_TYPE (t->type);
9456 reference_type = build_reference_type (reference_type);
9458 if (t->kind == ck_qual)
9459 t = next_conversion (t);
9460 if (t->kind == ck_ptr)
9461 t = next_conversion (t);
9462 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
9463 t = direct_reference_binding (reference_type, t);
9464 t->this_p = 1;
9465 t->rvaluedness_matches_p = 0;
9466 *ics = t;
9470 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
9471 and return the initial reference binding conversion. Otherwise,
9472 leave *ICS unchanged and return NULL. */
9474 static conversion *
9475 maybe_handle_ref_bind (conversion **ics)
9477 if ((*ics)->kind == ck_ref_bind)
9479 conversion *old_ics = *ics;
9480 *ics = next_conversion (old_ics);
9481 (*ics)->user_conv_p = old_ics->user_conv_p;
9482 return old_ics;
9485 return NULL;
9488 /* Compare two implicit conversion sequences according to the rules set out in
9489 [over.ics.rank]. Return values:
9491 1: ics1 is better than ics2
9492 -1: ics2 is better than ics1
9493 0: ics1 and ics2 are indistinguishable */
9495 static int
9496 compare_ics (conversion *ics1, conversion *ics2)
9498 tree from_type1;
9499 tree from_type2;
9500 tree to_type1;
9501 tree to_type2;
9502 tree deref_from_type1 = NULL_TREE;
9503 tree deref_from_type2 = NULL_TREE;
9504 tree deref_to_type1 = NULL_TREE;
9505 tree deref_to_type2 = NULL_TREE;
9506 conversion_rank rank1, rank2;
9508 /* REF_BINDING is nonzero if the result of the conversion sequence
9509 is a reference type. In that case REF_CONV is the reference
9510 binding conversion. */
9511 conversion *ref_conv1;
9512 conversion *ref_conv2;
9514 /* Compare badness before stripping the reference conversion. */
9515 if (ics1->bad_p > ics2->bad_p)
9516 return -1;
9517 else if (ics1->bad_p < ics2->bad_p)
9518 return 1;
9520 /* Handle implicit object parameters. */
9521 maybe_handle_implicit_object (&ics1);
9522 maybe_handle_implicit_object (&ics2);
9524 /* Handle reference parameters. */
9525 ref_conv1 = maybe_handle_ref_bind (&ics1);
9526 ref_conv2 = maybe_handle_ref_bind (&ics2);
9528 /* List-initialization sequence L1 is a better conversion sequence than
9529 list-initialization sequence L2 if L1 converts to
9530 std::initializer_list<X> for some X and L2 does not. */
9531 if (ics1->kind == ck_list && ics2->kind != ck_list)
9532 return 1;
9533 if (ics2->kind == ck_list && ics1->kind != ck_list)
9534 return -1;
9536 /* [over.ics.rank]
9538 When comparing the basic forms of implicit conversion sequences (as
9539 defined in _over.best.ics_)
9541 --a standard conversion sequence (_over.ics.scs_) is a better
9542 conversion sequence than a user-defined conversion sequence
9543 or an ellipsis conversion sequence, and
9545 --a user-defined conversion sequence (_over.ics.user_) is a
9546 better conversion sequence than an ellipsis conversion sequence
9547 (_over.ics.ellipsis_). */
9548 /* Use BAD_CONVERSION_RANK because we already checked for a badness
9549 mismatch. If both ICS are bad, we try to make a decision based on
9550 what would have happened if they'd been good. This is not an
9551 extension, we'll still give an error when we build up the call; this
9552 just helps us give a more helpful error message. */
9553 rank1 = BAD_CONVERSION_RANK (ics1);
9554 rank2 = BAD_CONVERSION_RANK (ics2);
9556 if (rank1 > rank2)
9557 return -1;
9558 else if (rank1 < rank2)
9559 return 1;
9561 if (ics1->ellipsis_p)
9562 /* Both conversions are ellipsis conversions. */
9563 return 0;
9565 /* User-defined conversion sequence U1 is a better conversion sequence
9566 than another user-defined conversion sequence U2 if they contain the
9567 same user-defined conversion operator or constructor and if the sec-
9568 ond standard conversion sequence of U1 is better than the second
9569 standard conversion sequence of U2. */
9571 /* Handle list-conversion with the same code even though it isn't always
9572 ranked as a user-defined conversion and it doesn't have a second
9573 standard conversion sequence; it will still have the desired effect.
9574 Specifically, we need to do the reference binding comparison at the
9575 end of this function. */
9577 if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
9579 conversion *t1;
9580 conversion *t2;
9582 for (t1 = ics1; t1->kind != ck_user; t1 = next_conversion (t1))
9583 if (t1->kind == ck_ambig || t1->kind == ck_aggr
9584 || t1->kind == ck_list)
9585 break;
9586 for (t2 = ics2; t2->kind != ck_user; t2 = next_conversion (t2))
9587 if (t2->kind == ck_ambig || t2->kind == ck_aggr
9588 || t2->kind == ck_list)
9589 break;
9591 if (t1->kind != t2->kind)
9592 return 0;
9593 else if (t1->kind == ck_user)
9595 tree f1 = t1->cand ? t1->cand->fn : t1->type;
9596 tree f2 = t2->cand ? t2->cand->fn : t2->type;
9597 if (f1 != f2)
9598 return 0;
9600 else
9602 /* For ambiguous or aggregate conversions, use the target type as
9603 a proxy for the conversion function. */
9604 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
9605 return 0;
9608 /* We can just fall through here, after setting up
9609 FROM_TYPE1 and FROM_TYPE2. */
9610 from_type1 = t1->type;
9611 from_type2 = t2->type;
9613 else
9615 conversion *t1;
9616 conversion *t2;
9618 /* We're dealing with two standard conversion sequences.
9620 [over.ics.rank]
9622 Standard conversion sequence S1 is a better conversion
9623 sequence than standard conversion sequence S2 if
9625 --S1 is a proper subsequence of S2 (comparing the conversion
9626 sequences in the canonical form defined by _over.ics.scs_,
9627 excluding any Lvalue Transformation; the identity
9628 conversion sequence is considered to be a subsequence of
9629 any non-identity conversion sequence */
9631 t1 = ics1;
9632 while (t1->kind != ck_identity)
9633 t1 = next_conversion (t1);
9634 from_type1 = t1->type;
9636 t2 = ics2;
9637 while (t2->kind != ck_identity)
9638 t2 = next_conversion (t2);
9639 from_type2 = t2->type;
9642 /* One sequence can only be a subsequence of the other if they start with
9643 the same type. They can start with different types when comparing the
9644 second standard conversion sequence in two user-defined conversion
9645 sequences. */
9646 if (same_type_p (from_type1, from_type2))
9648 if (is_subseq (ics1, ics2))
9649 return 1;
9650 if (is_subseq (ics2, ics1))
9651 return -1;
9654 /* [over.ics.rank]
9656 Or, if not that,
9658 --the rank of S1 is better than the rank of S2 (by the rules
9659 defined below):
9661 Standard conversion sequences are ordered by their ranks: an Exact
9662 Match is a better conversion than a Promotion, which is a better
9663 conversion than a Conversion.
9665 Two conversion sequences with the same rank are indistinguishable
9666 unless one of the following rules applies:
9668 --A conversion that does not a convert a pointer, pointer to member,
9669 or std::nullptr_t to bool is better than one that does.
9671 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
9672 so that we do not have to check it explicitly. */
9673 if (ics1->rank < ics2->rank)
9674 return 1;
9675 else if (ics2->rank < ics1->rank)
9676 return -1;
9678 to_type1 = ics1->type;
9679 to_type2 = ics2->type;
9681 /* A conversion from scalar arithmetic type to complex is worse than a
9682 conversion between scalar arithmetic types. */
9683 if (same_type_p (from_type1, from_type2)
9684 && ARITHMETIC_TYPE_P (from_type1)
9685 && ARITHMETIC_TYPE_P (to_type1)
9686 && ARITHMETIC_TYPE_P (to_type2)
9687 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
9688 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
9690 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
9691 return -1;
9692 else
9693 return 1;
9696 if (TYPE_PTR_P (from_type1)
9697 && TYPE_PTR_P (from_type2)
9698 && TYPE_PTR_P (to_type1)
9699 && TYPE_PTR_P (to_type2))
9701 deref_from_type1 = TREE_TYPE (from_type1);
9702 deref_from_type2 = TREE_TYPE (from_type2);
9703 deref_to_type1 = TREE_TYPE (to_type1);
9704 deref_to_type2 = TREE_TYPE (to_type2);
9706 /* The rules for pointers to members A::* are just like the rules
9707 for pointers A*, except opposite: if B is derived from A then
9708 A::* converts to B::*, not vice versa. For that reason, we
9709 switch the from_ and to_ variables here. */
9710 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
9711 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
9712 || (TYPE_PTRMEMFUNC_P (from_type1)
9713 && TYPE_PTRMEMFUNC_P (from_type2)
9714 && TYPE_PTRMEMFUNC_P (to_type1)
9715 && TYPE_PTRMEMFUNC_P (to_type2)))
9717 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
9718 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
9719 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
9720 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
9723 if (deref_from_type1 != NULL_TREE
9724 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
9725 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
9727 /* This was one of the pointer or pointer-like conversions.
9729 [over.ics.rank]
9731 --If class B is derived directly or indirectly from class A,
9732 conversion of B* to A* is better than conversion of B* to
9733 void*, and conversion of A* to void* is better than
9734 conversion of B* to void*. */
9735 if (VOID_TYPE_P (deref_to_type1)
9736 && VOID_TYPE_P (deref_to_type2))
9738 if (is_properly_derived_from (deref_from_type1,
9739 deref_from_type2))
9740 return -1;
9741 else if (is_properly_derived_from (deref_from_type2,
9742 deref_from_type1))
9743 return 1;
9745 else if (VOID_TYPE_P (deref_to_type1)
9746 || VOID_TYPE_P (deref_to_type2))
9748 if (same_type_p (deref_from_type1, deref_from_type2))
9750 if (VOID_TYPE_P (deref_to_type2))
9752 if (is_properly_derived_from (deref_from_type1,
9753 deref_to_type1))
9754 return 1;
9756 /* We know that DEREF_TO_TYPE1 is `void' here. */
9757 else if (is_properly_derived_from (deref_from_type1,
9758 deref_to_type2))
9759 return -1;
9762 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
9763 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
9765 /* [over.ics.rank]
9767 --If class B is derived directly or indirectly from class A
9768 and class C is derived directly or indirectly from B,
9770 --conversion of C* to B* is better than conversion of C* to
9773 --conversion of B* to A* is better than conversion of C* to
9774 A* */
9775 if (same_type_p (deref_from_type1, deref_from_type2))
9777 if (is_properly_derived_from (deref_to_type1,
9778 deref_to_type2))
9779 return 1;
9780 else if (is_properly_derived_from (deref_to_type2,
9781 deref_to_type1))
9782 return -1;
9784 else if (same_type_p (deref_to_type1, deref_to_type2))
9786 if (is_properly_derived_from (deref_from_type2,
9787 deref_from_type1))
9788 return 1;
9789 else if (is_properly_derived_from (deref_from_type1,
9790 deref_from_type2))
9791 return -1;
9795 else if (CLASS_TYPE_P (non_reference (from_type1))
9796 && same_type_p (from_type1, from_type2))
9798 tree from = non_reference (from_type1);
9800 /* [over.ics.rank]
9802 --binding of an expression of type C to a reference of type
9803 B& is better than binding an expression of type C to a
9804 reference of type A&
9806 --conversion of C to B is better than conversion of C to A, */
9807 if (is_properly_derived_from (from, to_type1)
9808 && is_properly_derived_from (from, to_type2))
9810 if (is_properly_derived_from (to_type1, to_type2))
9811 return 1;
9812 else if (is_properly_derived_from (to_type2, to_type1))
9813 return -1;
9816 else if (CLASS_TYPE_P (non_reference (to_type1))
9817 && same_type_p (to_type1, to_type2))
9819 tree to = non_reference (to_type1);
9821 /* [over.ics.rank]
9823 --binding of an expression of type B to a reference of type
9824 A& is better than binding an expression of type C to a
9825 reference of type A&,
9827 --conversion of B to A is better than conversion of C to A */
9828 if (is_properly_derived_from (from_type1, to)
9829 && is_properly_derived_from (from_type2, to))
9831 if (is_properly_derived_from (from_type2, from_type1))
9832 return 1;
9833 else if (is_properly_derived_from (from_type1, from_type2))
9834 return -1;
9838 /* [over.ics.rank]
9840 --S1 and S2 differ only in their qualification conversion and yield
9841 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
9842 qualification signature of type T1 is a proper subset of the cv-
9843 qualification signature of type T2 */
9844 if (ics1->kind == ck_qual
9845 && ics2->kind == ck_qual
9846 && same_type_p (from_type1, from_type2))
9848 int result = comp_cv_qual_signature (to_type1, to_type2);
9849 if (result != 0)
9850 return result;
9853 /* [over.ics.rank]
9855 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
9856 to an implicit object parameter of a non-static member function
9857 declared without a ref-qualifier, and either S1 binds an lvalue
9858 reference to an lvalue and S2 binds an rvalue reference or S1 binds an
9859 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
9860 draft standard, 13.3.3.2)
9862 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
9863 types to which the references refer are the same type except for
9864 top-level cv-qualifiers, and the type to which the reference
9865 initialized by S2 refers is more cv-qualified than the type to
9866 which the reference initialized by S1 refers.
9868 DR 1328 [over.match.best]: the context is an initialization by
9869 conversion function for direct reference binding (13.3.1.6) of a
9870 reference to function type, the return type of F1 is the same kind of
9871 reference (i.e. lvalue or rvalue) as the reference being initialized,
9872 and the return type of F2 is not. */
9874 if (ref_conv1 && ref_conv2)
9876 if (!ref_conv1->this_p && !ref_conv2->this_p
9877 && (ref_conv1->rvaluedness_matches_p
9878 != ref_conv2->rvaluedness_matches_p)
9879 && (same_type_p (ref_conv1->type, ref_conv2->type)
9880 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
9881 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
9883 if (ref_conv1->bad_p
9884 && !same_type_p (TREE_TYPE (ref_conv1->type),
9885 TREE_TYPE (ref_conv2->type)))
9886 /* Don't prefer a bad conversion that drops cv-quals to a bad
9887 conversion with the wrong rvalueness. */
9888 return 0;
9889 return (ref_conv1->rvaluedness_matches_p
9890 - ref_conv2->rvaluedness_matches_p);
9893 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
9895 int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
9896 int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
9897 if (ref_conv1->bad_p)
9899 /* Prefer the one that drops fewer cv-quals. */
9900 tree ftype = next_conversion (ref_conv1)->type;
9901 int fquals = cp_type_quals (ftype);
9902 q1 ^= fquals;
9903 q2 ^= fquals;
9905 return comp_cv_qualification (q2, q1);
9909 /* Neither conversion sequence is better than the other. */
9910 return 0;
9913 /* The source type for this standard conversion sequence. */
9915 static tree
9916 source_type (conversion *t)
9918 for (;; t = next_conversion (t))
9920 if (t->kind == ck_user
9921 || t->kind == ck_ambig
9922 || t->kind == ck_identity)
9923 return t->type;
9925 gcc_unreachable ();
9928 /* Note a warning about preferring WINNER to LOSER. We do this by storing
9929 a pointer to LOSER and re-running joust to produce the warning if WINNER
9930 is actually used. */
9932 static void
9933 add_warning (struct z_candidate *winner, struct z_candidate *loser)
9935 candidate_warning *cw = (candidate_warning *)
9936 conversion_obstack_alloc (sizeof (candidate_warning));
9937 cw->loser = loser;
9938 cw->next = winner->warnings;
9939 winner->warnings = cw;
9942 /* Compare two candidates for overloading as described in
9943 [over.match.best]. Return values:
9945 1: cand1 is better than cand2
9946 -1: cand2 is better than cand1
9947 0: cand1 and cand2 are indistinguishable */
9949 static int
9950 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
9951 tsubst_flags_t complain)
9953 int winner = 0;
9954 int off1 = 0, off2 = 0;
9955 size_t i;
9956 size_t len;
9958 /* Candidates that involve bad conversions are always worse than those
9959 that don't. */
9960 if (cand1->viable > cand2->viable)
9961 return 1;
9962 if (cand1->viable < cand2->viable)
9963 return -1;
9965 /* If we have two pseudo-candidates for conversions to the same type,
9966 or two candidates for the same function, arbitrarily pick one. */
9967 if (cand1->fn == cand2->fn
9968 && (IS_TYPE_OR_DECL_P (cand1->fn)))
9969 return 1;
9971 /* Prefer a non-deleted function over an implicitly deleted move
9972 constructor or assignment operator. This differs slightly from the
9973 wording for issue 1402 (which says the move op is ignored by overload
9974 resolution), but this way produces better error messages. */
9975 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
9976 && TREE_CODE (cand2->fn) == FUNCTION_DECL
9977 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
9979 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
9980 && move_fn_p (cand1->fn))
9981 return -1;
9982 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
9983 && move_fn_p (cand2->fn))
9984 return 1;
9987 /* a viable function F1
9988 is defined to be a better function than another viable function F2 if
9989 for all arguments i, ICSi(F1) is not a worse conversion sequence than
9990 ICSi(F2), and then */
9992 /* for some argument j, ICSj(F1) is a better conversion sequence than
9993 ICSj(F2) */
9995 /* For comparing static and non-static member functions, we ignore
9996 the implicit object parameter of the non-static function. The
9997 standard says to pretend that the static function has an object
9998 parm, but that won't work with operator overloading. */
9999 len = cand1->num_convs;
10000 if (len != cand2->num_convs)
10002 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
10003 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
10005 if (DECL_CONSTRUCTOR_P (cand1->fn)
10006 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
10007 /* We're comparing a near-match list constructor and a near-match
10008 non-list constructor. Just treat them as unordered. */
10009 return 0;
10011 gcc_assert (static_1 != static_2);
10013 if (static_1)
10014 off2 = 1;
10015 else
10017 off1 = 1;
10018 --len;
10022 for (i = 0; i < len; ++i)
10024 conversion *t1 = cand1->convs[i + off1];
10025 conversion *t2 = cand2->convs[i + off2];
10026 int comp = compare_ics (t1, t2);
10028 if (comp != 0)
10030 if ((complain & tf_warning)
10031 && warn_sign_promo
10032 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
10033 == cr_std + cr_promotion)
10034 && t1->kind == ck_std
10035 && t2->kind == ck_std
10036 && TREE_CODE (t1->type) == INTEGER_TYPE
10037 && TREE_CODE (t2->type) == INTEGER_TYPE
10038 && (TYPE_PRECISION (t1->type)
10039 == TYPE_PRECISION (t2->type))
10040 && (TYPE_UNSIGNED (next_conversion (t1)->type)
10041 || (TREE_CODE (next_conversion (t1)->type)
10042 == ENUMERAL_TYPE)))
10044 tree type = next_conversion (t1)->type;
10045 tree type1, type2;
10046 struct z_candidate *w, *l;
10047 if (comp > 0)
10048 type1 = t1->type, type2 = t2->type,
10049 w = cand1, l = cand2;
10050 else
10051 type1 = t2->type, type2 = t1->type,
10052 w = cand2, l = cand1;
10054 if (warn)
10056 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
10057 type, type1, type2);
10058 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
10060 else
10061 add_warning (w, l);
10064 if (winner && comp != winner)
10066 winner = 0;
10067 goto tweak;
10069 winner = comp;
10073 /* warn about confusing overload resolution for user-defined conversions,
10074 either between a constructor and a conversion op, or between two
10075 conversion ops. */
10076 if ((complain & tf_warning)
10077 && winner && warn_conversion && cand1->second_conv
10078 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
10079 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
10081 struct z_candidate *w, *l;
10082 bool give_warning = false;
10084 if (winner == 1)
10085 w = cand1, l = cand2;
10086 else
10087 w = cand2, l = cand1;
10089 /* We don't want to complain about `X::operator T1 ()'
10090 beating `X::operator T2 () const', when T2 is a no less
10091 cv-qualified version of T1. */
10092 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
10093 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
10095 tree t = TREE_TYPE (TREE_TYPE (l->fn));
10096 tree f = TREE_TYPE (TREE_TYPE (w->fn));
10098 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
10100 t = TREE_TYPE (t);
10101 f = TREE_TYPE (f);
10103 if (!comp_ptr_ttypes (t, f))
10104 give_warning = true;
10106 else
10107 give_warning = true;
10109 if (!give_warning)
10110 /*NOP*/;
10111 else if (warn)
10113 tree source = source_type (w->convs[0]);
10114 if (! DECL_CONSTRUCTOR_P (w->fn))
10115 source = TREE_TYPE (source);
10116 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
10117 && warning (OPT_Wconversion, " for conversion from %qH to %qI",
10118 source, w->second_conv->type))
10120 inform (input_location, " because conversion sequence for the argument is better");
10123 else
10124 add_warning (w, l);
10127 if (winner)
10128 return winner;
10130 /* DR 495 moved this tiebreaker above the template ones. */
10131 /* or, if not that,
10132 the context is an initialization by user-defined conversion (see
10133 _dcl.init_ and _over.match.user_) and the standard conversion
10134 sequence from the return type of F1 to the destination type (i.e.,
10135 the type of the entity being initialized) is a better conversion
10136 sequence than the standard conversion sequence from the return type
10137 of F2 to the destination type. */
10139 if (cand1->second_conv)
10141 winner = compare_ics (cand1->second_conv, cand2->second_conv);
10142 if (winner)
10143 return winner;
10146 /* or, if not that,
10147 F1 is a non-template function and F2 is a template function
10148 specialization. */
10150 if (!cand1->template_decl && cand2->template_decl)
10151 return 1;
10152 else if (cand1->template_decl && !cand2->template_decl)
10153 return -1;
10155 /* or, if not that,
10156 F1 and F2 are template functions and the function template for F1 is
10157 more specialized than the template for F2 according to the partial
10158 ordering rules. */
10160 if (cand1->template_decl && cand2->template_decl)
10162 winner = more_specialized_fn
10163 (TI_TEMPLATE (cand1->template_decl),
10164 TI_TEMPLATE (cand2->template_decl),
10165 /* [temp.func.order]: The presence of unused ellipsis and default
10166 arguments has no effect on the partial ordering of function
10167 templates. add_function_candidate() will not have
10168 counted the "this" argument for constructors. */
10169 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
10170 if (winner)
10171 return winner;
10174 // C++ Concepts
10175 // or, if not that, F1 is more constrained than F2.
10176 if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn))
10178 winner = more_constrained (cand1->fn, cand2->fn);
10179 if (winner)
10180 return winner;
10183 /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
10184 if (deduction_guide_p (cand1->fn))
10186 gcc_assert (deduction_guide_p (cand2->fn));
10187 /* We distinguish between candidates from an explicit deduction guide and
10188 candidates built from a constructor based on DECL_ARTIFICIAL. */
10189 int art1 = DECL_ARTIFICIAL (cand1->fn);
10190 int art2 = DECL_ARTIFICIAL (cand2->fn);
10191 if (art1 != art2)
10192 return art2 - art1;
10194 if (art1)
10196 /* Prefer the special copy guide over a declared copy/move
10197 constructor. */
10198 if (copy_guide_p (cand1->fn))
10199 return 1;
10200 if (copy_guide_p (cand2->fn))
10201 return -1;
10203 /* Prefer a candidate generated from a non-template constructor. */
10204 int tg1 = template_guide_p (cand1->fn);
10205 int tg2 = template_guide_p (cand2->fn);
10206 if (tg1 != tg2)
10207 return tg2 - tg1;
10211 /* F1 is a member of a class D, F2 is a member of a base class B of D, and
10212 for all arguments the corresponding parameters of F1 and F2 have the same
10213 type (CWG 2273/2277). */
10214 if (DECL_P (cand1->fn) && DECL_CLASS_SCOPE_P (cand1->fn)
10215 && !DECL_CONV_FN_P (cand1->fn)
10216 && DECL_P (cand2->fn) && DECL_CLASS_SCOPE_P (cand2->fn)
10217 && !DECL_CONV_FN_P (cand2->fn))
10219 tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
10220 tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
10222 bool used1 = false;
10223 bool used2 = false;
10224 if (base1 == base2)
10225 /* No difference. */;
10226 else if (DERIVED_FROM_P (base1, base2))
10227 used1 = true;
10228 else if (DERIVED_FROM_P (base2, base1))
10229 used2 = true;
10231 if (int diff = used2 - used1)
10233 for (i = 0; i < len; ++i)
10235 conversion *t1 = cand1->convs[i + off1];
10236 conversion *t2 = cand2->convs[i + off2];
10237 if (!same_type_p (t1->type, t2->type))
10238 break;
10240 if (i == len)
10241 return diff;
10245 /* Check whether we can discard a builtin candidate, either because we
10246 have two identical ones or matching builtin and non-builtin candidates.
10248 (Pedantically in the latter case the builtin which matched the user
10249 function should not be added to the overload set, but we spot it here.
10251 [over.match.oper]
10252 ... the builtin candidates include ...
10253 - do not have the same parameter type list as any non-template
10254 non-member candidate. */
10256 if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
10258 for (i = 0; i < len; ++i)
10259 if (!same_type_p (cand1->convs[i]->type,
10260 cand2->convs[i]->type))
10261 break;
10262 if (i == cand1->num_convs)
10264 if (cand1->fn == cand2->fn)
10265 /* Two built-in candidates; arbitrarily pick one. */
10266 return 1;
10267 else if (identifier_p (cand1->fn))
10268 /* cand1 is built-in; prefer cand2. */
10269 return -1;
10270 else
10271 /* cand2 is built-in; prefer cand1. */
10272 return 1;
10276 /* For candidates of a multi-versioned function, make the version with
10277 the highest priority win. This version will be checked for dispatching
10278 first. If this version can be inlined into the caller, the front-end
10279 will simply make a direct call to this function. */
10281 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
10282 && DECL_FUNCTION_VERSIONED (cand1->fn)
10283 && TREE_CODE (cand2->fn) == FUNCTION_DECL
10284 && DECL_FUNCTION_VERSIONED (cand2->fn))
10286 tree f1 = TREE_TYPE (cand1->fn);
10287 tree f2 = TREE_TYPE (cand2->fn);
10288 tree p1 = TYPE_ARG_TYPES (f1);
10289 tree p2 = TYPE_ARG_TYPES (f2);
10291 /* Check if cand1->fn and cand2->fn are versions of the same function. It
10292 is possible that cand1->fn and cand2->fn are function versions but of
10293 different functions. Check types to see if they are versions of the same
10294 function. */
10295 if (compparms (p1, p2)
10296 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
10298 /* Always make the version with the higher priority, more
10299 specialized, win. */
10300 gcc_assert (targetm.compare_version_priority);
10301 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
10302 return 1;
10303 else
10304 return -1;
10308 /* If the two function declarations represent the same function (this can
10309 happen with declarations in multiple scopes and arg-dependent lookup),
10310 arbitrarily choose one. But first make sure the default args we're
10311 using match. */
10312 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
10313 && equal_functions (cand1->fn, cand2->fn))
10315 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
10316 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
10318 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
10320 for (i = 0; i < len; ++i)
10322 /* Don't crash if the fn is variadic. */
10323 if (!parms1)
10324 break;
10325 parms1 = TREE_CHAIN (parms1);
10326 parms2 = TREE_CHAIN (parms2);
10329 if (off1)
10330 parms1 = TREE_CHAIN (parms1);
10331 else if (off2)
10332 parms2 = TREE_CHAIN (parms2);
10334 for (; parms1; ++i)
10336 if (!cp_tree_equal (TREE_PURPOSE (parms1),
10337 TREE_PURPOSE (parms2)))
10339 if (warn)
10341 if (complain & tf_error)
10343 if (permerror (input_location,
10344 "default argument mismatch in "
10345 "overload resolution"))
10347 inform (DECL_SOURCE_LOCATION (cand1->fn),
10348 " candidate 1: %q#F", cand1->fn);
10349 inform (DECL_SOURCE_LOCATION (cand2->fn),
10350 " candidate 2: %q#F", cand2->fn);
10353 else
10354 return 0;
10356 else
10357 add_warning (cand1, cand2);
10358 break;
10360 parms1 = TREE_CHAIN (parms1);
10361 parms2 = TREE_CHAIN (parms2);
10364 return 1;
10367 tweak:
10369 /* Extension: If the worst conversion for one candidate is worse than the
10370 worst conversion for the other, take the first. */
10371 if (!pedantic && (complain & tf_warning_or_error))
10373 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
10374 struct z_candidate *w = 0, *l = 0;
10376 for (i = 0; i < len; ++i)
10378 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
10379 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
10380 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
10381 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
10383 if (rank1 < rank2)
10384 winner = 1, w = cand1, l = cand2;
10385 if (rank1 > rank2)
10386 winner = -1, w = cand2, l = cand1;
10387 if (winner)
10389 /* Don't choose a deleted function over ambiguity. */
10390 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
10391 return 0;
10392 if (warn)
10394 pedwarn (input_location, 0,
10395 "ISO C++ says that these are ambiguous, even "
10396 "though the worst conversion for the first is better than "
10397 "the worst conversion for the second:");
10398 print_z_candidate (input_location, _("candidate 1:"), w);
10399 print_z_candidate (input_location, _("candidate 2:"), l);
10401 else
10402 add_warning (w, l);
10403 return winner;
10407 gcc_assert (!winner);
10408 return 0;
10411 /* Given a list of candidates for overloading, find the best one, if any.
10412 This algorithm has a worst case of O(2n) (winner is last), and a best
10413 case of O(n/2) (totally ambiguous); much better than a sorting
10414 algorithm. */
10416 static struct z_candidate *
10417 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
10419 struct z_candidate *champ = candidates, *challenger;
10420 int fate;
10421 int champ_compared_to_predecessor = 0;
10423 /* Walk through the list once, comparing each current champ to the next
10424 candidate, knocking out a candidate or two with each comparison. */
10426 for (challenger = champ->next; challenger; )
10428 fate = joust (champ, challenger, 0, complain);
10429 if (fate == 1)
10430 challenger = challenger->next;
10431 else
10433 if (fate == 0)
10435 champ = challenger->next;
10436 if (champ == 0)
10437 return NULL;
10438 champ_compared_to_predecessor = 0;
10440 else
10442 champ = challenger;
10443 champ_compared_to_predecessor = 1;
10446 challenger = champ->next;
10450 /* Make sure the champ is better than all the candidates it hasn't yet
10451 been compared to. */
10453 for (challenger = candidates;
10454 challenger != champ
10455 && !(champ_compared_to_predecessor && challenger->next == champ);
10456 challenger = challenger->next)
10458 fate = joust (champ, challenger, 0, complain);
10459 if (fate != 1)
10460 return NULL;
10463 return champ;
10466 /* Returns nonzero if things of type FROM can be converted to TO. */
10468 bool
10469 can_convert (tree to, tree from, tsubst_flags_t complain)
10471 tree arg = NULL_TREE;
10472 /* implicit_conversion only considers user-defined conversions
10473 if it has an expression for the call argument list. */
10474 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
10475 arg = build1 (CAST_EXPR, from, NULL_TREE);
10476 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
10479 /* Returns nonzero if things of type FROM can be converted to TO with a
10480 standard conversion. */
10482 bool
10483 can_convert_standard (tree to, tree from, tsubst_flags_t complain)
10485 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
10488 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
10490 bool
10491 can_convert_arg (tree to, tree from, tree arg, int flags,
10492 tsubst_flags_t complain)
10494 conversion *t;
10495 void *p;
10496 bool ok_p;
10498 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10499 p = conversion_obstack_alloc (0);
10500 /* We want to discard any access checks done for this test,
10501 as we might not be in the appropriate access context and
10502 we'll do the check again when we actually perform the
10503 conversion. */
10504 push_deferring_access_checks (dk_deferred);
10506 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
10507 flags, complain);
10508 ok_p = (t && !t->bad_p);
10510 /* Discard the access checks now. */
10511 pop_deferring_access_checks ();
10512 /* Free all the conversions we allocated. */
10513 obstack_free (&conversion_obstack, p);
10515 return ok_p;
10518 /* Like can_convert_arg, but allows dubious conversions as well. */
10520 bool
10521 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
10522 tsubst_flags_t complain)
10524 conversion *t;
10525 void *p;
10527 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10528 p = conversion_obstack_alloc (0);
10529 /* Try to perform the conversion. */
10530 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
10531 flags, complain);
10532 /* Free all the conversions we allocated. */
10533 obstack_free (&conversion_obstack, p);
10535 return t != NULL;
10538 /* Convert EXPR to TYPE. Return the converted expression.
10540 Note that we allow bad conversions here because by the time we get to
10541 this point we are committed to doing the conversion. If we end up
10542 doing a bad conversion, convert_like will complain. */
10544 tree
10545 perform_implicit_conversion_flags (tree type, tree expr,
10546 tsubst_flags_t complain, int flags)
10548 conversion *conv;
10549 void *p;
10550 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
10552 if (error_operand_p (expr))
10553 return error_mark_node;
10555 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10556 p = conversion_obstack_alloc (0);
10558 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10559 /*c_cast_p=*/false,
10560 flags, complain);
10562 if (!conv)
10564 if (complain & tf_error)
10566 /* If expr has unknown type, then it is an overloaded function.
10567 Call instantiate_type to get good error messages. */
10568 if (TREE_TYPE (expr) == unknown_type_node)
10569 instantiate_type (type, expr, complain);
10570 else if (invalid_nonstatic_memfn_p (loc, expr, complain))
10571 /* We gave an error. */;
10572 else
10573 error_at (loc, "could not convert %qE from %qH to %qI", expr,
10574 TREE_TYPE (expr), type);
10576 expr = error_mark_node;
10578 else if (processing_template_decl && conv->kind != ck_identity)
10580 /* In a template, we are only concerned about determining the
10581 type of non-dependent expressions, so we do not have to
10582 perform the actual conversion. But for initializers, we
10583 need to be able to perform it at instantiation
10584 (or instantiate_non_dependent_expr) time. */
10585 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
10586 if (!(flags & LOOKUP_ONLYCONVERTING))
10587 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
10589 else
10590 expr = convert_like (conv, expr, complain);
10592 /* Free all the conversions we allocated. */
10593 obstack_free (&conversion_obstack, p);
10595 return expr;
10598 tree
10599 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
10601 return perform_implicit_conversion_flags (type, expr, complain,
10602 LOOKUP_IMPLICIT);
10605 /* Convert EXPR to TYPE (as a direct-initialization) if that is
10606 permitted. If the conversion is valid, the converted expression is
10607 returned. Otherwise, NULL_TREE is returned, except in the case
10608 that TYPE is a class type; in that case, an error is issued. If
10609 C_CAST_P is true, then this direct-initialization is taking
10610 place as part of a static_cast being attempted as part of a C-style
10611 cast. */
10613 tree
10614 perform_direct_initialization_if_possible (tree type,
10615 tree expr,
10616 bool c_cast_p,
10617 tsubst_flags_t complain)
10619 conversion *conv;
10620 void *p;
10622 if (type == error_mark_node || error_operand_p (expr))
10623 return error_mark_node;
10624 /* [dcl.init]
10626 If the destination type is a (possibly cv-qualified) class type:
10628 -- If the initialization is direct-initialization ...,
10629 constructors are considered. ... If no constructor applies, or
10630 the overload resolution is ambiguous, the initialization is
10631 ill-formed. */
10632 if (CLASS_TYPE_P (type))
10634 vec<tree, va_gc> *args = make_tree_vector_single (expr);
10635 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
10636 &args, type, LOOKUP_NORMAL, complain);
10637 release_tree_vector (args);
10638 return build_cplus_new (type, expr, complain);
10641 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10642 p = conversion_obstack_alloc (0);
10644 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10645 c_cast_p,
10646 LOOKUP_NORMAL, complain);
10647 if (!conv || conv->bad_p)
10648 expr = NULL_TREE;
10649 else
10650 expr = convert_like_real (conv, expr, NULL_TREE, 0,
10651 /*issue_conversion_warnings=*/false,
10652 c_cast_p,
10653 complain);
10655 /* Free all the conversions we allocated. */
10656 obstack_free (&conversion_obstack, p);
10658 return expr;
10661 /* When initializing a reference that lasts longer than a full-expression,
10662 this special rule applies:
10664 [class.temporary]
10666 The temporary to which the reference is bound or the temporary
10667 that is the complete object to which the reference is bound
10668 persists for the lifetime of the reference.
10670 The temporaries created during the evaluation of the expression
10671 initializing the reference, except the temporary to which the
10672 reference is bound, are destroyed at the end of the
10673 full-expression in which they are created.
10675 In that case, we store the converted expression into a new
10676 VAR_DECL in a new scope.
10678 However, we want to be careful not to create temporaries when
10679 they are not required. For example, given:
10681 struct B {};
10682 struct D : public B {};
10683 D f();
10684 const B& b = f();
10686 there is no need to copy the return value from "f"; we can just
10687 extend its lifetime. Similarly, given:
10689 struct S {};
10690 struct T { operator S(); };
10691 T t;
10692 const S& s = t;
10694 we can extend the lifetime of the return value of the conversion
10695 operator.
10697 The next several functions are involved in this lifetime extension. */
10699 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
10700 reference is being bound to a temporary. Create and return a new
10701 VAR_DECL with the indicated TYPE; this variable will store the value to
10702 which the reference is bound. */
10704 tree
10705 make_temporary_var_for_ref_to_temp (tree decl, tree type)
10707 tree var = create_temporary_var (type);
10709 /* Register the variable. */
10710 if (VAR_P (decl)
10711 && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
10713 /* Namespace-scope or local static; give it a mangled name. */
10714 /* FIXME share comdat with decl? */
10716 TREE_STATIC (var) = TREE_STATIC (decl);
10717 CP_DECL_THREAD_LOCAL_P (var) = CP_DECL_THREAD_LOCAL_P (decl);
10718 set_decl_tls_model (var, DECL_TLS_MODEL (decl));
10720 tree name = mangle_ref_init_variable (decl);
10721 DECL_NAME (var) = name;
10722 SET_DECL_ASSEMBLER_NAME (var, name);
10724 var = pushdecl (var);
10726 else
10727 /* Create a new cleanup level if necessary. */
10728 maybe_push_cleanup_level (type);
10730 return var;
10733 /* EXPR is the initializer for a variable DECL of reference or
10734 std::initializer_list type. Create, push and return a new VAR_DECL
10735 for the initializer so that it will live as long as DECL. Any
10736 cleanup for the new variable is returned through CLEANUP, and the
10737 code to initialize the new variable is returned through INITP. */
10739 static tree
10740 set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
10741 tree *initp)
10743 tree init;
10744 tree type;
10745 tree var;
10747 /* Create the temporary variable. */
10748 type = TREE_TYPE (expr);
10749 var = make_temporary_var_for_ref_to_temp (decl, type);
10750 layout_decl (var, 0);
10751 /* If the rvalue is the result of a function call it will be
10752 a TARGET_EXPR. If it is some other construct (such as a
10753 member access expression where the underlying object is
10754 itself the result of a function call), turn it into a
10755 TARGET_EXPR here. It is important that EXPR be a
10756 TARGET_EXPR below since otherwise the INIT_EXPR will
10757 attempt to make a bitwise copy of EXPR to initialize
10758 VAR. */
10759 if (TREE_CODE (expr) != TARGET_EXPR)
10760 expr = get_target_expr (expr);
10762 if (TREE_CODE (decl) == FIELD_DECL
10763 && extra_warnings && !TREE_NO_WARNING (decl))
10765 warning (OPT_Wextra, "a temporary bound to %qD only persists "
10766 "until the constructor exits", decl);
10767 TREE_NO_WARNING (decl) = true;
10770 /* Recursively extend temps in this initializer. */
10771 TARGET_EXPR_INITIAL (expr)
10772 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups);
10774 /* Any reference temp has a non-trivial initializer. */
10775 DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
10777 /* If the initializer is constant, put it in DECL_INITIAL so we get
10778 static initialization and use in constant expressions. */
10779 init = maybe_constant_init (expr);
10780 if (TREE_CONSTANT (init))
10782 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
10784 /* 5.19 says that a constant expression can include an
10785 lvalue-rvalue conversion applied to "a glvalue of literal type
10786 that refers to a non-volatile temporary object initialized
10787 with a constant expression". Rather than try to communicate
10788 that this VAR_DECL is a temporary, just mark it constexpr.
10790 Currently this is only useful for initializer_list temporaries,
10791 since reference vars can't appear in constant expressions. */
10792 DECL_DECLARED_CONSTEXPR_P (var) = true;
10793 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
10794 TREE_CONSTANT (var) = true;
10796 DECL_INITIAL (var) = init;
10797 init = NULL_TREE;
10799 else
10800 /* Create the INIT_EXPR that will initialize the temporary
10801 variable. */
10802 init = split_nonconstant_init (var, expr);
10803 if (at_function_scope_p ())
10805 add_decl_expr (var);
10807 if (TREE_STATIC (var))
10808 init = add_stmt_to_compound (init, register_dtor_fn (var));
10809 else
10811 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
10812 if (cleanup)
10813 vec_safe_push (*cleanups, cleanup);
10816 /* We must be careful to destroy the temporary only
10817 after its initialization has taken place. If the
10818 initialization throws an exception, then the
10819 destructor should not be run. We cannot simply
10820 transform INIT into something like:
10822 (INIT, ({ CLEANUP_STMT; }))
10824 because emit_local_var always treats the
10825 initializer as a full-expression. Thus, the
10826 destructor would run too early; it would run at the
10827 end of initializing the reference variable, rather
10828 than at the end of the block enclosing the
10829 reference variable.
10831 The solution is to pass back a cleanup expression
10832 which the caller is responsible for attaching to
10833 the statement tree. */
10835 else
10837 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
10838 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
10840 if (CP_DECL_THREAD_LOCAL_P (var))
10841 tls_aggregates = tree_cons (NULL_TREE, var,
10842 tls_aggregates);
10843 else
10844 static_aggregates = tree_cons (NULL_TREE, var,
10845 static_aggregates);
10847 else
10848 /* Check whether the dtor is callable. */
10849 cxx_maybe_build_cleanup (var, tf_warning_or_error);
10851 /* Avoid -Wunused-variable warning (c++/38958). */
10852 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
10853 && VAR_P (decl))
10854 TREE_USED (decl) = DECL_READ_P (decl) = true;
10856 *initp = init;
10857 return var;
10860 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
10861 initializing a variable of that TYPE. */
10863 tree
10864 initialize_reference (tree type, tree expr,
10865 int flags, tsubst_flags_t complain)
10867 conversion *conv;
10868 void *p;
10869 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
10871 if (type == error_mark_node || error_operand_p (expr))
10872 return error_mark_node;
10874 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10875 p = conversion_obstack_alloc (0);
10877 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
10878 flags, complain);
10879 if (!conv || conv->bad_p)
10881 if (complain & tf_error)
10883 if (conv)
10884 convert_like (conv, expr, complain);
10885 else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
10886 && !TYPE_REF_IS_RVALUE (type)
10887 && !lvalue_p (expr))
10888 error_at (loc, "invalid initialization of non-const reference of "
10889 "type %qH from an rvalue of type %qI",
10890 type, TREE_TYPE (expr));
10891 else
10892 error_at (loc, "invalid initialization of reference of type "
10893 "%qH from expression of type %qI", type,
10894 TREE_TYPE (expr));
10896 return error_mark_node;
10899 if (conv->kind == ck_ref_bind)
10900 /* Perform the conversion. */
10901 expr = convert_like (conv, expr, complain);
10902 else if (conv->kind == ck_ambig)
10903 /* We gave an error in build_user_type_conversion_1. */
10904 expr = error_mark_node;
10905 else
10906 gcc_unreachable ();
10908 /* Free all the conversions we allocated. */
10909 obstack_free (&conversion_obstack, p);
10911 return expr;
10914 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
10915 which is bound either to a reference or a std::initializer_list. */
10917 static tree
10918 extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups)
10920 tree sub = init;
10921 tree *p;
10922 STRIP_NOPS (sub);
10923 if (TREE_CODE (sub) == COMPOUND_EXPR)
10925 TREE_OPERAND (sub, 1)
10926 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups);
10927 return init;
10929 if (TREE_CODE (sub) != ADDR_EXPR)
10930 return init;
10931 /* Deal with binding to a subobject. */
10932 for (p = &TREE_OPERAND (sub, 0); TREE_CODE (*p) == COMPONENT_REF; )
10933 p = &TREE_OPERAND (*p, 0);
10934 if (TREE_CODE (*p) == TARGET_EXPR)
10936 tree subinit = NULL_TREE;
10937 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit);
10938 recompute_tree_invariant_for_addr_expr (sub);
10939 if (init != sub)
10940 init = fold_convert (TREE_TYPE (init), sub);
10941 if (subinit)
10942 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
10944 return init;
10947 /* INIT is part of the initializer for DECL. If there are any
10948 reference or initializer lists being initialized, extend their
10949 lifetime to match that of DECL. */
10951 tree
10952 extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
10954 tree type = TREE_TYPE (init);
10955 if (processing_template_decl)
10956 return init;
10957 if (TREE_CODE (type) == REFERENCE_TYPE)
10958 init = extend_ref_init_temps_1 (decl, init, cleanups);
10959 else
10961 tree ctor = init;
10962 if (TREE_CODE (ctor) == TARGET_EXPR)
10963 ctor = TARGET_EXPR_INITIAL (ctor);
10964 if (TREE_CODE (ctor) == CONSTRUCTOR)
10966 if (is_std_init_list (type))
10968 /* The temporary array underlying a std::initializer_list
10969 is handled like a reference temporary. */
10970 tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
10971 array = extend_ref_init_temps_1 (decl, array, cleanups);
10972 CONSTRUCTOR_ELT (ctor, 0)->value = array;
10974 else
10976 unsigned i;
10977 constructor_elt *p;
10978 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
10979 FOR_EACH_VEC_SAFE_ELT (elts, i, p)
10980 p->value = extend_ref_init_temps (decl, p->value, cleanups);
10982 recompute_constructor_flags (ctor);
10983 if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
10984 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10988 return init;
10991 /* Returns true iff an initializer for TYPE could contain temporaries that
10992 need to be extended because they are bound to references or
10993 std::initializer_list. */
10995 bool
10996 type_has_extended_temps (tree type)
10998 type = strip_array_types (type);
10999 if (TREE_CODE (type) == REFERENCE_TYPE)
11000 return true;
11001 if (CLASS_TYPE_P (type))
11003 if (is_std_init_list (type))
11004 return true;
11005 for (tree f = next_initializable_field (TYPE_FIELDS (type));
11006 f; f = next_initializable_field (DECL_CHAIN (f)))
11007 if (type_has_extended_temps (TREE_TYPE (f)))
11008 return true;
11010 return false;
11013 /* Returns true iff TYPE is some variant of std::initializer_list. */
11015 bool
11016 is_std_init_list (tree type)
11018 if (!TYPE_P (type))
11019 return false;
11020 if (cxx_dialect == cxx98)
11021 return false;
11022 /* Look through typedefs. */
11023 type = TYPE_MAIN_VARIANT (type);
11024 return (CLASS_TYPE_P (type)
11025 && CP_TYPE_CONTEXT (type) == std_node
11026 && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
11029 /* Returns true iff DECL is a list constructor: i.e. a constructor which
11030 will accept an argument list of a single std::initializer_list<T>. */
11032 bool
11033 is_list_ctor (tree decl)
11035 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
11036 tree arg;
11038 if (!args || args == void_list_node)
11039 return false;
11041 arg = non_reference (TREE_VALUE (args));
11042 if (!is_std_init_list (arg))
11043 return false;
11045 args = TREE_CHAIN (args);
11047 if (args && args != void_list_node && !TREE_PURPOSE (args))
11048 /* There are more non-defaulted parms. */
11049 return false;
11051 return true;
11054 #include "gt-cp-call.h"