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