RISC-V: Add support for riscv-*-*.
[official-gcc.git] / gcc / cp / call.c
blob209c1fd2f0e810b56ad7875471fff21977bcb7be
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"
45 /* The various kinds of conversion. */
47 enum conversion_kind {
48 ck_identity,
49 ck_lvalue,
50 ck_fnptr,
51 ck_qual,
52 ck_std,
53 ck_ptr,
54 ck_pmem,
55 ck_base,
56 ck_ref_bind,
57 ck_user,
58 ck_ambig,
59 ck_list,
60 ck_aggr,
61 ck_rvalue
64 /* The rank of the conversion. Order of the enumerals matters; better
65 conversions should come earlier in the list. */
67 enum conversion_rank {
68 cr_identity,
69 cr_exact,
70 cr_promotion,
71 cr_std,
72 cr_pbool,
73 cr_user,
74 cr_ellipsis,
75 cr_bad
78 /* An implicit conversion sequence, in the sense of [over.best.ics].
79 The first conversion to be performed is at the end of the chain.
80 That conversion is always a cr_identity conversion. */
82 struct conversion {
83 /* The kind of conversion represented by this step. */
84 conversion_kind kind;
85 /* The rank of this conversion. */
86 conversion_rank rank;
87 BOOL_BITFIELD user_conv_p : 1;
88 BOOL_BITFIELD ellipsis_p : 1;
89 BOOL_BITFIELD this_p : 1;
90 /* True if this conversion would be permitted with a bending of
91 language standards, e.g. disregarding pointer qualifiers or
92 converting integers to pointers. */
93 BOOL_BITFIELD bad_p : 1;
94 /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
95 temporary should be created to hold the result of the
96 conversion. If KIND is ck_ambig, true if the context is
97 copy-initialization. */
98 BOOL_BITFIELD need_temporary_p : 1;
99 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
100 from a pointer-to-derived to pointer-to-base is being performed. */
101 BOOL_BITFIELD base_p : 1;
102 /* If KIND is ck_ref_bind, true when either an lvalue reference is
103 being bound to an lvalue expression or an rvalue reference is
104 being bound to an rvalue expression. If KIND is ck_rvalue,
105 true when we are treating an lvalue as an rvalue (12.8p33). If
106 KIND is ck_base, always false. If ck_identity, we will be
107 binding a reference directly or decaying to a pointer. */
108 BOOL_BITFIELD rvaluedness_matches_p: 1;
109 BOOL_BITFIELD check_narrowing: 1;
110 /* The type of the expression resulting from the conversion. */
111 tree type;
112 union {
113 /* The next conversion in the chain. Since the conversions are
114 arranged from outermost to innermost, the NEXT conversion will
115 actually be performed before this conversion. This variant is
116 used only when KIND is neither ck_identity, ck_ambig nor
117 ck_list. Please use the next_conversion function instead
118 of using this field directly. */
119 conversion *next;
120 /* The expression at the beginning of the conversion chain. This
121 variant is used only if KIND is ck_identity or ck_ambig. */
122 tree expr;
123 /* The array of conversions for an initializer_list, so this
124 variant is used only when KIN D is ck_list. */
125 conversion **list;
126 } u;
127 /* The function candidate corresponding to this conversion
128 sequence. This field is only used if KIND is ck_user. */
129 struct z_candidate *cand;
132 #define CONVERSION_RANK(NODE) \
133 ((NODE)->bad_p ? cr_bad \
134 : (NODE)->ellipsis_p ? cr_ellipsis \
135 : (NODE)->user_conv_p ? cr_user \
136 : (NODE)->rank)
138 #define BAD_CONVERSION_RANK(NODE) \
139 ((NODE)->ellipsis_p ? cr_ellipsis \
140 : (NODE)->user_conv_p ? cr_user \
141 : (NODE)->rank)
143 static struct obstack conversion_obstack;
144 static bool conversion_obstack_initialized;
145 struct rejection_reason;
147 static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
148 static int equal_functions (tree, tree);
149 static int joust (struct z_candidate *, struct z_candidate *, bool,
150 tsubst_flags_t);
151 static int compare_ics (conversion *, conversion *);
152 static void maybe_warn_class_memaccess (location_t, tree,
153 const vec<tree, va_gc> *);
154 static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
155 #define convert_like(CONV, EXPR, COMPLAIN) \
156 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, \
157 /*issue_conversion_warnings=*/true, \
158 /*c_cast_p=*/false, (COMPLAIN))
159 #define convert_like_with_context(CONV, EXPR, FN, ARGNO, COMPLAIN ) \
160 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), \
161 /*issue_conversion_warnings=*/true, \
162 /*c_cast_p=*/false, (COMPLAIN))
163 static tree convert_like_real (conversion *, tree, tree, int, bool,
164 bool, tsubst_flags_t);
165 static void op_error (location_t, enum tree_code, enum tree_code, tree,
166 tree, tree, bool);
167 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
168 tsubst_flags_t);
169 static void print_z_candidate (location_t, const char *, struct z_candidate *);
170 static void print_z_candidates (location_t, struct z_candidate *);
171 static tree build_this (tree);
172 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
173 static bool any_strictly_viable (struct z_candidate *);
174 static struct z_candidate *add_template_candidate
175 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
176 tree, tree, tree, int, unification_kind_t, tsubst_flags_t);
177 static struct z_candidate *add_template_candidate_real
178 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
179 tree, tree, tree, int, tree, unification_kind_t, tsubst_flags_t);
180 static void add_builtin_candidates
181 (struct z_candidate **, enum tree_code, enum tree_code,
182 tree, tree *, int, tsubst_flags_t);
183 static void add_builtin_candidate
184 (struct z_candidate **, enum tree_code, enum tree_code,
185 tree, tree, tree, tree *, tree *, int, tsubst_flags_t);
186 static bool is_complete (tree);
187 static void build_builtin_candidate
188 (struct z_candidate **, tree, tree, tree, tree *, tree *,
189 int, tsubst_flags_t);
190 static struct z_candidate *add_conv_candidate
191 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, tree,
192 tree, tsubst_flags_t);
193 static struct z_candidate *add_function_candidate
194 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
195 tree, int, conversion**, tsubst_flags_t);
196 static conversion *implicit_conversion (tree, tree, tree, bool, int,
197 tsubst_flags_t);
198 static conversion *reference_binding (tree, tree, tree, bool, int,
199 tsubst_flags_t);
200 static conversion *build_conv (conversion_kind, tree, conversion *);
201 static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
202 static conversion *next_conversion (conversion *);
203 static bool is_subseq (conversion *, conversion *);
204 static conversion *maybe_handle_ref_bind (conversion **);
205 static void maybe_handle_implicit_object (conversion **);
206 static struct z_candidate *add_candidate
207 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
208 conversion **, tree, tree, int, struct rejection_reason *, int);
209 static tree source_type (conversion *);
210 static void add_warning (struct z_candidate *, struct z_candidate *);
211 static bool reference_compatible_p (tree, tree);
212 static conversion *direct_reference_binding (tree, conversion *);
213 static bool promoted_arithmetic_type_p (tree);
214 static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
215 static char *name_as_c_string (tree, tree, bool *);
216 static tree prep_operand (tree);
217 static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
218 bool, tree, tree, int, struct z_candidate **,
219 tsubst_flags_t);
220 static conversion *merge_conversion_sequences (conversion *, conversion *);
221 static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
223 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
224 NAME can take many forms... */
226 bool
227 check_dtor_name (tree basetype, tree name)
229 /* Just accept something we've already complained about. */
230 if (name == error_mark_node)
231 return true;
233 if (TREE_CODE (name) == TYPE_DECL)
234 name = TREE_TYPE (name);
235 else if (TYPE_P (name))
236 /* OK */;
237 else if (identifier_p (name))
239 if ((MAYBE_CLASS_TYPE_P (basetype)
240 || TREE_CODE (basetype) == ENUMERAL_TYPE)
241 && name == constructor_name (basetype))
242 return true;
243 else
244 name = get_type_value (name);
246 else
248 /* In the case of:
250 template <class T> struct S { ~S(); };
251 int i;
252 i.~S();
254 NAME will be a class template. */
255 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
256 return false;
259 if (!name || name == error_mark_node)
260 return false;
261 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
264 /* We want the address of a function or method. We avoid creating a
265 pointer-to-member function. */
267 tree
268 build_addr_func (tree function, tsubst_flags_t complain)
270 tree type = TREE_TYPE (function);
272 /* We have to do these by hand to avoid real pointer to member
273 functions. */
274 if (TREE_CODE (type) == METHOD_TYPE)
276 if (TREE_CODE (function) == OFFSET_REF)
278 tree object = build_address (TREE_OPERAND (function, 0));
279 return get_member_function_from_ptrfunc (&object,
280 TREE_OPERAND (function, 1),
281 complain);
283 function = build_address (function);
285 else
286 function = decay_conversion (function, complain, /*reject_builtin=*/false);
288 return function;
291 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
292 POINTER_TYPE to those. Note, pointer to member function types
293 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
294 two variants. build_call_a is the primitive taking an array of
295 arguments, while build_call_n is a wrapper that handles varargs. */
297 tree
298 build_call_n (tree function, int n, ...)
300 if (n == 0)
301 return build_call_a (function, 0, NULL);
302 else
304 tree *argarray = XALLOCAVEC (tree, n);
305 va_list ap;
306 int i;
308 va_start (ap, n);
309 for (i = 0; i < n; i++)
310 argarray[i] = va_arg (ap, tree);
311 va_end (ap);
312 return build_call_a (function, n, argarray);
316 /* Update various flags in cfun and the call itself based on what is being
317 called. Split out of build_call_a so that bot_manip can use it too. */
319 void
320 set_flags_from_callee (tree call)
322 /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
323 tree decl = cp_get_callee_fndecl_nofold (call);
325 /* We check both the decl and the type; a function may be known not to
326 throw without being declared throw(). */
327 bool nothrow = decl && TREE_NOTHROW (decl);
328 tree callee = cp_get_callee (call);
329 if (callee)
330 nothrow |= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (callee)));
331 else if (TREE_CODE (call) == CALL_EXPR
332 && internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW)
333 nothrow = true;
335 if (!nothrow && at_function_scope_p () && cfun && cp_function_chain)
336 cp_function_chain->can_throw = 1;
338 if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
339 current_function_returns_abnormally = 1;
341 TREE_NOTHROW (call) = nothrow;
344 tree
345 build_call_a (tree function, int n, tree *argarray)
347 tree decl;
348 tree result_type;
349 tree fntype;
350 int i;
352 function = build_addr_func (function, tf_warning_or_error);
354 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
355 fntype = TREE_TYPE (TREE_TYPE (function));
356 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
357 || TREE_CODE (fntype) == METHOD_TYPE);
358 result_type = TREE_TYPE (fntype);
359 /* An rvalue has no cv-qualifiers. */
360 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
361 result_type = cv_unqualified (result_type);
363 function = build_call_array_loc (input_location,
364 result_type, function, n, argarray);
365 set_flags_from_callee (function);
367 decl = get_callee_fndecl (function);
369 if (decl && !TREE_USED (decl))
371 /* We invoke build_call directly for several library
372 functions. These may have been declared normally if
373 we're building libgcc, so we can't just check
374 DECL_ARTIFICIAL. */
375 gcc_assert (DECL_ARTIFICIAL (decl)
376 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
377 "__", 2));
378 mark_used (decl);
381 require_complete_eh_spec_types (fntype, decl);
383 TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
385 /* Don't pass empty class objects by value. This is useful
386 for tags in STL, which are used to control overload resolution.
387 We don't need to handle other cases of copying empty classes. */
388 if (! decl || ! DECL_BUILT_IN (decl))
389 for (i = 0; i < n; i++)
391 tree arg = CALL_EXPR_ARG (function, i);
392 if (is_empty_class (TREE_TYPE (arg))
393 && ! TREE_ADDRESSABLE (TREE_TYPE (arg)))
395 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
396 arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
397 CALL_EXPR_ARG (function, i) = arg;
401 return function;
404 /* New overloading code. */
406 struct z_candidate;
408 struct candidate_warning {
409 z_candidate *loser;
410 candidate_warning *next;
413 /* Information for providing diagnostics about why overloading failed. */
415 enum rejection_reason_code {
416 rr_none,
417 rr_arity,
418 rr_explicit_conversion,
419 rr_template_conversion,
420 rr_arg_conversion,
421 rr_bad_arg_conversion,
422 rr_template_unification,
423 rr_invalid_copy,
424 rr_inherited_ctor,
425 rr_constraint_failure
428 struct conversion_info {
429 /* The index of the argument, 0-based. */
430 int n_arg;
431 /* The actual argument or its type. */
432 tree from;
433 /* The type of the parameter. */
434 tree to_type;
437 struct rejection_reason {
438 enum rejection_reason_code code;
439 union {
440 /* Information about an arity mismatch. */
441 struct {
442 /* The expected number of arguments. */
443 int expected;
444 /* The actual number of arguments in the call. */
445 int actual;
446 /* Whether the call was a varargs call. */
447 bool call_varargs_p;
448 } arity;
449 /* Information about an argument conversion mismatch. */
450 struct conversion_info conversion;
451 /* Same, but for bad argument conversions. */
452 struct conversion_info bad_conversion;
453 /* Information about template unification failures. These are the
454 parameters passed to fn_type_unification. */
455 struct {
456 tree tmpl;
457 tree explicit_targs;
458 int num_targs;
459 const tree *args;
460 unsigned int nargs;
461 tree return_type;
462 unification_kind_t strict;
463 int flags;
464 } template_unification;
465 /* Information about template instantiation failures. These are the
466 parameters passed to instantiate_template. */
467 struct {
468 tree tmpl;
469 tree targs;
470 } template_instantiation;
471 } u;
474 struct z_candidate {
475 /* The FUNCTION_DECL that will be called if this candidate is
476 selected by overload resolution. */
477 tree fn;
478 /* If not NULL_TREE, the first argument to use when calling this
479 function. */
480 tree first_arg;
481 /* The rest of the arguments to use when calling this function. If
482 there are no further arguments this may be NULL or it may be an
483 empty vector. */
484 const vec<tree, va_gc> *args;
485 /* The implicit conversion sequences for each of the arguments to
486 FN. */
487 conversion **convs;
488 /* The number of implicit conversion sequences. */
489 size_t num_convs;
490 /* If FN is a user-defined conversion, the standard conversion
491 sequence from the type returned by FN to the desired destination
492 type. */
493 conversion *second_conv;
494 struct rejection_reason *reason;
495 /* If FN is a member function, the binfo indicating the path used to
496 qualify the name of FN at the call site. This path is used to
497 determine whether or not FN is accessible if it is selected by
498 overload resolution. The DECL_CONTEXT of FN will always be a
499 (possibly improper) base of this binfo. */
500 tree access_path;
501 /* If FN is a non-static member function, the binfo indicating the
502 subobject to which the `this' pointer should be converted if FN
503 is selected by overload resolution. The type pointed to by
504 the `this' pointer must correspond to the most derived class
505 indicated by the CONVERSION_PATH. */
506 tree conversion_path;
507 tree template_decl;
508 tree explicit_targs;
509 candidate_warning *warnings;
510 z_candidate *next;
511 int viable;
513 /* The flags active in add_candidate. */
514 int flags;
517 /* Returns true iff T is a null pointer constant in the sense of
518 [conv.ptr]. */
520 bool
521 null_ptr_cst_p (tree t)
523 tree type = TREE_TYPE (t);
525 /* [conv.ptr]
527 A null pointer constant is an integral constant expression
528 (_expr.const_) rvalue of integer type that evaluates to zero or
529 an rvalue of type std::nullptr_t. */
530 if (NULLPTR_TYPE_P (type))
531 return true;
533 if (cxx_dialect >= cxx11)
535 STRIP_ANY_LOCATION_WRAPPER (t);
537 /* Core issue 903 says only literal 0 is a null pointer constant. */
538 if (TREE_CODE (type) == INTEGER_TYPE
539 && !char_type_p (type)
540 && TREE_CODE (t) == INTEGER_CST
541 && integer_zerop (t)
542 && !TREE_OVERFLOW (t))
543 return true;
545 else if (CP_INTEGRAL_TYPE_P (type))
547 t = fold_non_dependent_expr (t, tf_none);
548 STRIP_NOPS (t);
549 if (integer_zerop (t) && !TREE_OVERFLOW (t))
550 return true;
553 return false;
556 /* Returns true iff T is a null member pointer value (4.11). */
558 bool
559 null_member_pointer_value_p (tree t)
561 tree type = TREE_TYPE (t);
562 if (!type)
563 return false;
564 else if (TYPE_PTRMEMFUNC_P (type))
565 return (TREE_CODE (t) == CONSTRUCTOR
566 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
567 else if (TYPE_PTRDATAMEM_P (type))
568 return integer_all_onesp (t);
569 else
570 return false;
573 /* Returns nonzero if PARMLIST consists of only default parms,
574 ellipsis, and/or undeduced parameter packs. */
576 bool
577 sufficient_parms_p (const_tree parmlist)
579 for (; parmlist && parmlist != void_list_node;
580 parmlist = TREE_CHAIN (parmlist))
581 if (!TREE_PURPOSE (parmlist)
582 && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
583 return false;
584 return true;
587 /* Allocate N bytes of memory from the conversion obstack. The memory
588 is zeroed before being returned. */
590 static void *
591 conversion_obstack_alloc (size_t n)
593 void *p;
594 if (!conversion_obstack_initialized)
596 gcc_obstack_init (&conversion_obstack);
597 conversion_obstack_initialized = true;
599 p = obstack_alloc (&conversion_obstack, n);
600 memset (p, 0, n);
601 return p;
604 /* Allocate rejection reasons. */
606 static struct rejection_reason *
607 alloc_rejection (enum rejection_reason_code code)
609 struct rejection_reason *p;
610 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
611 p->code = code;
612 return p;
615 static struct rejection_reason *
616 arity_rejection (tree first_arg, int expected, int actual)
618 struct rejection_reason *r = alloc_rejection (rr_arity);
619 int adjust = first_arg != NULL_TREE;
620 r->u.arity.expected = expected - adjust;
621 r->u.arity.actual = actual - adjust;
622 return r;
625 static struct rejection_reason *
626 arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
628 struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
629 int adjust = first_arg != NULL_TREE;
630 r->u.conversion.n_arg = n_arg - adjust;
631 r->u.conversion.from = from;
632 r->u.conversion.to_type = to;
633 return r;
636 static struct rejection_reason *
637 bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
639 struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
640 int adjust = first_arg != NULL_TREE;
641 r->u.bad_conversion.n_arg = n_arg - adjust;
642 r->u.bad_conversion.from = from;
643 r->u.bad_conversion.to_type = to;
644 return r;
647 static struct rejection_reason *
648 explicit_conversion_rejection (tree from, tree to)
650 struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
651 r->u.conversion.n_arg = 0;
652 r->u.conversion.from = from;
653 r->u.conversion.to_type = to;
654 return r;
657 static struct rejection_reason *
658 template_conversion_rejection (tree from, tree to)
660 struct rejection_reason *r = alloc_rejection (rr_template_conversion);
661 r->u.conversion.n_arg = 0;
662 r->u.conversion.from = from;
663 r->u.conversion.to_type = to;
664 return r;
667 static struct rejection_reason *
668 template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
669 const tree *args, unsigned int nargs,
670 tree return_type, unification_kind_t strict,
671 int flags)
673 size_t args_n_bytes = sizeof (*args) * nargs;
674 tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
675 struct rejection_reason *r = alloc_rejection (rr_template_unification);
676 r->u.template_unification.tmpl = tmpl;
677 r->u.template_unification.explicit_targs = explicit_targs;
678 r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
679 /* Copy args to our own storage. */
680 memcpy (args1, args, args_n_bytes);
681 r->u.template_unification.args = args1;
682 r->u.template_unification.nargs = nargs;
683 r->u.template_unification.return_type = return_type;
684 r->u.template_unification.strict = strict;
685 r->u.template_unification.flags = flags;
686 return r;
689 static struct rejection_reason *
690 template_unification_error_rejection (void)
692 return alloc_rejection (rr_template_unification);
695 static struct rejection_reason *
696 invalid_copy_with_fn_template_rejection (void)
698 struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
699 return r;
702 static struct rejection_reason *
703 inherited_ctor_rejection (void)
705 struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
706 return r;
709 // Build a constraint failure record, saving information into the
710 // template_instantiation field of the rejection. If FN is not a template
711 // declaration, the TMPL member is the FN declaration and TARGS is empty.
713 static struct rejection_reason *
714 constraint_failure (tree fn)
716 struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
717 if (tree ti = DECL_TEMPLATE_INFO (fn))
719 r->u.template_instantiation.tmpl = TI_TEMPLATE (ti);
720 r->u.template_instantiation.targs = TI_ARGS (ti);
722 else
724 r->u.template_instantiation.tmpl = fn;
725 r->u.template_instantiation.targs = NULL_TREE;
727 return r;
730 /* Dynamically allocate a conversion. */
732 static conversion *
733 alloc_conversion (conversion_kind kind)
735 conversion *c;
736 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
737 c->kind = kind;
738 return c;
741 /* Make sure that all memory on the conversion obstack has been
742 freed. */
744 void
745 validate_conversion_obstack (void)
747 if (conversion_obstack_initialized)
748 gcc_assert ((obstack_next_free (&conversion_obstack)
749 == obstack_base (&conversion_obstack)));
752 /* Dynamically allocate an array of N conversions. */
754 static conversion **
755 alloc_conversions (size_t n)
757 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
760 static conversion *
761 build_conv (conversion_kind code, tree type, conversion *from)
763 conversion *t;
764 conversion_rank rank = CONVERSION_RANK (from);
766 /* Note that the caller is responsible for filling in t->cand for
767 user-defined conversions. */
768 t = alloc_conversion (code);
769 t->type = type;
770 t->u.next = from;
772 switch (code)
774 case ck_ptr:
775 case ck_pmem:
776 case ck_base:
777 case ck_std:
778 if (rank < cr_std)
779 rank = cr_std;
780 break;
782 case ck_qual:
783 case ck_fnptr:
784 if (rank < cr_exact)
785 rank = cr_exact;
786 break;
788 default:
789 break;
791 t->rank = rank;
792 t->user_conv_p = (code == ck_user || from->user_conv_p);
793 t->bad_p = from->bad_p;
794 t->base_p = false;
795 return t;
798 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
799 specialization of std::initializer_list<T>, if such a conversion is
800 possible. */
802 static conversion *
803 build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
805 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
806 unsigned len = CONSTRUCTOR_NELTS (ctor);
807 conversion **subconvs = alloc_conversions (len);
808 conversion *t;
809 unsigned i;
810 tree val;
812 /* Within a list-initialization we can have more user-defined
813 conversions. */
814 flags &= ~LOOKUP_NO_CONVERSION;
815 /* But no narrowing conversions. */
816 flags |= LOOKUP_NO_NARROWING;
818 /* Can't make an array of these types. */
819 if (TYPE_REF_P (elttype)
820 || TREE_CODE (elttype) == FUNCTION_TYPE
821 || VOID_TYPE_P (elttype))
822 return NULL;
824 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
826 conversion *sub
827 = implicit_conversion (elttype, TREE_TYPE (val), val,
828 false, flags, complain);
829 if (sub == NULL)
830 return NULL;
832 subconvs[i] = sub;
835 t = alloc_conversion (ck_list);
836 t->type = type;
837 t->u.list = subconvs;
838 t->rank = cr_exact;
840 for (i = 0; i < len; ++i)
842 conversion *sub = subconvs[i];
843 if (sub->rank > t->rank)
844 t->rank = sub->rank;
845 if (sub->user_conv_p)
846 t->user_conv_p = true;
847 if (sub->bad_p)
848 t->bad_p = true;
851 return t;
854 /* Return the next conversion of the conversion chain (if applicable),
855 or NULL otherwise. Please use this function instead of directly
856 accessing fields of struct conversion. */
858 static conversion *
859 next_conversion (conversion *conv)
861 if (conv == NULL
862 || conv->kind == ck_identity
863 || conv->kind == ck_ambig
864 || conv->kind == ck_list)
865 return NULL;
866 return conv->u.next;
869 /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
870 is a valid aggregate initializer for array type ATYPE. */
872 static bool
873 can_convert_array (tree atype, tree ctor, int flags, tsubst_flags_t complain)
875 unsigned i;
876 tree elttype = TREE_TYPE (atype);
877 for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
879 tree val = CONSTRUCTOR_ELT (ctor, i)->value;
880 bool ok;
881 if (TREE_CODE (elttype) == ARRAY_TYPE
882 && TREE_CODE (val) == CONSTRUCTOR)
883 ok = can_convert_array (elttype, val, flags, complain);
884 else
885 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
886 complain);
887 if (!ok)
888 return false;
890 return true;
893 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
894 aggregate class, if such a conversion is possible. */
896 static conversion *
897 build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
899 unsigned HOST_WIDE_INT i = 0;
900 conversion *c;
901 tree field = next_initializable_field (TYPE_FIELDS (type));
902 tree empty_ctor = NULL_TREE;
904 /* We already called reshape_init in implicit_conversion. */
906 /* The conversions within the init-list aren't affected by the enclosing
907 context; they're always simple copy-initialization. */
908 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
910 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
912 tree ftype = TREE_TYPE (field);
913 tree val;
914 bool ok;
916 if (i < CONSTRUCTOR_NELTS (ctor))
917 val = CONSTRUCTOR_ELT (ctor, i)->value;
918 else if (DECL_INITIAL (field))
919 val = get_nsdmi (field, /*ctor*/false, complain);
920 else if (TYPE_REF_P (ftype))
921 /* Value-initialization of reference is ill-formed. */
922 return NULL;
923 else
925 if (empty_ctor == NULL_TREE)
926 empty_ctor = build_constructor (init_list_type_node, NULL);
927 val = empty_ctor;
929 ++i;
931 if (TREE_CODE (ftype) == ARRAY_TYPE
932 && TREE_CODE (val) == CONSTRUCTOR)
933 ok = can_convert_array (ftype, val, flags, complain);
934 else
935 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
936 complain);
938 if (!ok)
939 return NULL;
941 if (TREE_CODE (type) == UNION_TYPE)
942 break;
945 if (i < CONSTRUCTOR_NELTS (ctor))
946 return NULL;
948 c = alloc_conversion (ck_aggr);
949 c->type = type;
950 c->rank = cr_exact;
951 c->user_conv_p = true;
952 c->check_narrowing = true;
953 c->u.next = NULL;
954 return c;
957 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
958 array type, if such a conversion is possible. */
960 static conversion *
961 build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
963 conversion *c;
964 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
965 tree elttype = TREE_TYPE (type);
966 unsigned i;
967 tree val;
968 bool bad = false;
969 bool user = false;
970 enum conversion_rank rank = cr_exact;
972 /* We might need to propagate the size from the element to the array. */
973 complete_type (type);
975 if (TYPE_DOMAIN (type)
976 && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
978 unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
979 if (alen < len)
980 return NULL;
983 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
985 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
987 conversion *sub
988 = implicit_conversion (elttype, TREE_TYPE (val), val,
989 false, flags, complain);
990 if (sub == NULL)
991 return NULL;
993 if (sub->rank > rank)
994 rank = sub->rank;
995 if (sub->user_conv_p)
996 user = true;
997 if (sub->bad_p)
998 bad = true;
1001 c = alloc_conversion (ck_aggr);
1002 c->type = type;
1003 c->rank = rank;
1004 c->user_conv_p = user;
1005 c->bad_p = bad;
1006 c->u.next = NULL;
1007 return c;
1010 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1011 complex type, if such a conversion is possible. */
1013 static conversion *
1014 build_complex_conv (tree type, tree ctor, int flags,
1015 tsubst_flags_t complain)
1017 conversion *c;
1018 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1019 tree elttype = TREE_TYPE (type);
1020 unsigned i;
1021 tree val;
1022 bool bad = false;
1023 bool user = false;
1024 enum conversion_rank rank = cr_exact;
1026 if (len != 2)
1027 return NULL;
1029 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1031 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
1033 conversion *sub
1034 = implicit_conversion (elttype, TREE_TYPE (val), val,
1035 false, flags, complain);
1036 if (sub == NULL)
1037 return NULL;
1039 if (sub->rank > rank)
1040 rank = sub->rank;
1041 if (sub->user_conv_p)
1042 user = true;
1043 if (sub->bad_p)
1044 bad = true;
1047 c = alloc_conversion (ck_aggr);
1048 c->type = type;
1049 c->rank = rank;
1050 c->user_conv_p = user;
1051 c->bad_p = bad;
1052 c->u.next = NULL;
1053 return c;
1056 /* Build a representation of the identity conversion from EXPR to
1057 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1059 static conversion *
1060 build_identity_conv (tree type, tree expr)
1062 conversion *c;
1064 c = alloc_conversion (ck_identity);
1065 c->type = type;
1066 c->u.expr = expr;
1068 return c;
1071 /* Converting from EXPR to TYPE was ambiguous in the sense that there
1072 were multiple user-defined conversions to accomplish the job.
1073 Build a conversion that indicates that ambiguity. */
1075 static conversion *
1076 build_ambiguous_conv (tree type, tree expr)
1078 conversion *c;
1080 c = alloc_conversion (ck_ambig);
1081 c->type = type;
1082 c->u.expr = expr;
1084 return c;
1087 tree
1088 strip_top_quals (tree t)
1090 if (TREE_CODE (t) == ARRAY_TYPE)
1091 return t;
1092 return cp_build_qualified_type (t, 0);
1095 /* Returns the standard conversion path (see [conv]) from type FROM to type
1096 TO, if any. For proper handling of null pointer constants, you must
1097 also pass the expression EXPR to convert from. If C_CAST_P is true,
1098 this conversion is coming from a C-style cast. */
1100 static conversion *
1101 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1102 int flags, tsubst_flags_t complain)
1104 enum tree_code fcode, tcode;
1105 conversion *conv;
1106 bool fromref = false;
1107 tree qualified_to;
1109 to = non_reference (to);
1110 if (TYPE_REF_P (from))
1112 fromref = true;
1113 from = TREE_TYPE (from);
1115 qualified_to = to;
1116 to = strip_top_quals (to);
1117 from = strip_top_quals (from);
1119 if (expr && type_unknown_p (expr))
1121 if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1123 tsubst_flags_t tflags = tf_conv;
1124 expr = instantiate_type (to, expr, tflags);
1125 if (expr == error_mark_node)
1126 return NULL;
1127 from = TREE_TYPE (expr);
1129 else if (TREE_CODE (to) == BOOLEAN_TYPE)
1131 /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1132 expr = resolve_nondeduced_context (expr, complain);
1133 from = TREE_TYPE (expr);
1137 fcode = TREE_CODE (from);
1138 tcode = TREE_CODE (to);
1140 conv = build_identity_conv (from, expr);
1141 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1143 from = type_decays_to (from);
1144 fcode = TREE_CODE (from);
1145 /* Tell convert_like_real that we're using the address. */
1146 conv->rvaluedness_matches_p = true;
1147 conv = build_conv (ck_lvalue, from, conv);
1149 /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1150 obvalue_p) seems odd, since it's already a prvalue, but that's how we
1151 express the copy constructor call required by copy-initialization. */
1152 else if (fromref || (expr && obvalue_p (expr)))
1154 if (expr)
1156 tree bitfield_type;
1157 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1158 if (bitfield_type)
1160 from = strip_top_quals (bitfield_type);
1161 fcode = TREE_CODE (from);
1164 conv = build_conv (ck_rvalue, from, conv);
1165 if (flags & LOOKUP_PREFER_RVALUE)
1166 /* Tell convert_like_real to set LOOKUP_PREFER_RVALUE. */
1167 conv->rvaluedness_matches_p = true;
1170 /* Allow conversion between `__complex__' data types. */
1171 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1173 /* The standard conversion sequence to convert FROM to TO is
1174 the standard conversion sequence to perform componentwise
1175 conversion. */
1176 conversion *part_conv = standard_conversion
1177 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1178 complain);
1180 if (part_conv)
1182 conv = build_conv (part_conv->kind, to, conv);
1183 conv->rank = part_conv->rank;
1185 else
1186 conv = NULL;
1188 return conv;
1191 if (same_type_p (from, to))
1193 if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1194 conv->type = qualified_to;
1195 return conv;
1198 /* [conv.ptr]
1199 A null pointer constant can be converted to a pointer type; ... A
1200 null pointer constant of integral type can be converted to an
1201 rvalue of type std::nullptr_t. */
1202 if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1203 || NULLPTR_TYPE_P (to))
1204 && ((expr && null_ptr_cst_p (expr))
1205 || NULLPTR_TYPE_P (from)))
1206 conv = build_conv (ck_std, to, conv);
1207 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1208 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1210 /* For backwards brain damage compatibility, allow interconversion of
1211 pointers and integers with a pedwarn. */
1212 conv = build_conv (ck_std, to, conv);
1213 conv->bad_p = true;
1215 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1217 /* For backwards brain damage compatibility, allow interconversion of
1218 enums and integers with a pedwarn. */
1219 conv = build_conv (ck_std, to, conv);
1220 conv->bad_p = true;
1222 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1223 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1225 tree to_pointee;
1226 tree from_pointee;
1228 if (tcode == POINTER_TYPE)
1230 to_pointee = TREE_TYPE (to);
1231 from_pointee = TREE_TYPE (from);
1233 /* Since this is the target of a pointer, it can't have function
1234 qualifiers, so any TYPE_QUALS must be for attributes const or
1235 noreturn. Strip them. */
1236 if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1237 && TYPE_QUALS (to_pointee))
1238 to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1239 if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1240 && TYPE_QUALS (from_pointee))
1241 from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1243 else
1245 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1246 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1249 if (tcode == POINTER_TYPE
1250 && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1251 to_pointee))
1253 else if (VOID_TYPE_P (to_pointee)
1254 && !TYPE_PTRDATAMEM_P (from)
1255 && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1257 tree nfrom = TREE_TYPE (from);
1258 /* Don't try to apply restrict to void. */
1259 int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1260 from_pointee = cp_build_qualified_type (void_type_node, quals);
1261 from = build_pointer_type (from_pointee);
1262 conv = build_conv (ck_ptr, from, conv);
1264 else if (TYPE_PTRDATAMEM_P (from))
1266 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1267 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1269 if (same_type_p (fbase, tbase))
1270 /* No base conversion needed. */;
1271 else if (DERIVED_FROM_P (fbase, tbase)
1272 && (same_type_ignoring_top_level_qualifiers_p
1273 (from_pointee, to_pointee)))
1275 from = build_ptrmem_type (tbase, from_pointee);
1276 conv = build_conv (ck_pmem, from, conv);
1278 else
1279 return NULL;
1281 else if (CLASS_TYPE_P (from_pointee)
1282 && CLASS_TYPE_P (to_pointee)
1283 /* [conv.ptr]
1285 An rvalue of type "pointer to cv D," where D is a
1286 class type, can be converted to an rvalue of type
1287 "pointer to cv B," where B is a base class (clause
1288 _class.derived_) of D. If B is an inaccessible
1289 (clause _class.access_) or ambiguous
1290 (_class.member.lookup_) base class of D, a program
1291 that necessitates this conversion is ill-formed.
1292 Therefore, we use DERIVED_FROM_P, and do not check
1293 access or uniqueness. */
1294 && DERIVED_FROM_P (to_pointee, from_pointee))
1296 from_pointee
1297 = cp_build_qualified_type (to_pointee,
1298 cp_type_quals (from_pointee));
1299 from = build_pointer_type (from_pointee);
1300 conv = build_conv (ck_ptr, from, conv);
1301 conv->base_p = true;
1304 if (same_type_p (from, to))
1305 /* OK */;
1306 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
1307 /* In a C-style cast, we ignore CV-qualification because we
1308 are allowed to perform a static_cast followed by a
1309 const_cast. */
1310 conv = build_conv (ck_qual, to, conv);
1311 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1312 conv = build_conv (ck_qual, to, conv);
1313 else if (expr && string_conv_p (to, expr, 0))
1314 /* converting from string constant to char *. */
1315 conv = build_conv (ck_qual, to, conv);
1316 else if (fnptr_conv_p (to, from))
1317 conv = build_conv (ck_fnptr, to, conv);
1318 /* Allow conversions among compatible ObjC pointer types (base
1319 conversions have been already handled above). */
1320 else if (c_dialect_objc ()
1321 && objc_compare_types (to, from, -4, NULL_TREE))
1322 conv = build_conv (ck_ptr, to, conv);
1323 else if (ptr_reasonably_similar (to_pointee, from_pointee))
1325 conv = build_conv (ck_ptr, to, conv);
1326 conv->bad_p = true;
1328 else
1329 return NULL;
1331 from = to;
1333 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1335 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1336 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1337 tree fbase = class_of_this_parm (fromfn);
1338 tree tbase = class_of_this_parm (tofn);
1340 if (!DERIVED_FROM_P (fbase, tbase))
1341 return NULL;
1343 tree fstat = static_fn_type (fromfn);
1344 tree tstat = static_fn_type (tofn);
1345 if (same_type_p (tstat, fstat)
1346 || fnptr_conv_p (tstat, fstat))
1347 /* OK */;
1348 else
1349 return NULL;
1351 if (!same_type_p (fbase, tbase))
1353 from = build_memfn_type (fstat,
1354 tbase,
1355 cp_type_quals (tbase),
1356 type_memfn_rqual (tofn));
1357 from = build_ptrmemfunc_type (build_pointer_type (from));
1358 conv = build_conv (ck_pmem, from, conv);
1359 conv->base_p = true;
1361 if (fnptr_conv_p (tstat, fstat))
1362 conv = build_conv (ck_fnptr, to, conv);
1364 else if (tcode == BOOLEAN_TYPE)
1366 /* [conv.bool]
1368 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1369 to member type can be converted to a prvalue of type bool. ...
1370 For direct-initialization (8.5 [dcl.init]), a prvalue of type
1371 std::nullptr_t can be converted to a prvalue of type bool; */
1372 if (ARITHMETIC_TYPE_P (from)
1373 || UNSCOPED_ENUM_P (from)
1374 || fcode == POINTER_TYPE
1375 || TYPE_PTRMEM_P (from)
1376 || NULLPTR_TYPE_P (from))
1378 conv = build_conv (ck_std, to, conv);
1379 if (fcode == POINTER_TYPE
1380 || TYPE_PTRDATAMEM_P (from)
1381 || (TYPE_PTRMEMFUNC_P (from)
1382 && conv->rank < cr_pbool)
1383 || NULLPTR_TYPE_P (from))
1384 conv->rank = cr_pbool;
1385 if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1386 conv->bad_p = true;
1387 return conv;
1390 return NULL;
1392 /* We don't check for ENUMERAL_TYPE here because there are no standard
1393 conversions to enum type. */
1394 /* As an extension, allow conversion to complex type. */
1395 else if (ARITHMETIC_TYPE_P (to))
1397 if (! (INTEGRAL_CODE_P (fcode)
1398 || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1399 || SCOPED_ENUM_P (from))
1400 return NULL;
1401 conv = build_conv (ck_std, to, conv);
1403 /* Give this a better rank if it's a promotion. */
1404 if (same_type_p (to, type_promotes_to (from))
1405 && next_conversion (conv)->rank <= cr_promotion)
1406 conv->rank = cr_promotion;
1408 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1409 && vector_types_convertible_p (from, to, false))
1410 return build_conv (ck_std, to, conv);
1411 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1412 && is_properly_derived_from (from, to))
1414 if (conv->kind == ck_rvalue)
1415 conv = next_conversion (conv);
1416 conv = build_conv (ck_base, to, conv);
1417 /* The derived-to-base conversion indicates the initialization
1418 of a parameter with base type from an object of a derived
1419 type. A temporary object is created to hold the result of
1420 the conversion unless we're binding directly to a reference. */
1421 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1423 else
1424 return NULL;
1426 if (flags & LOOKUP_NO_NARROWING)
1427 conv->check_narrowing = true;
1429 return conv;
1432 /* Returns nonzero if T1 is reference-related to T2. */
1434 bool
1435 reference_related_p (tree t1, tree t2)
1437 if (t1 == error_mark_node || t2 == error_mark_node)
1438 return false;
1440 t1 = TYPE_MAIN_VARIANT (t1);
1441 t2 = TYPE_MAIN_VARIANT (t2);
1443 /* [dcl.init.ref]
1445 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1446 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
1447 of T2. */
1448 return (same_type_p (t1, t2)
1449 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1450 && DERIVED_FROM_P (t1, t2)));
1453 /* Returns nonzero if T1 is reference-compatible with T2. */
1455 static bool
1456 reference_compatible_p (tree t1, tree t2)
1458 /* [dcl.init.ref]
1460 "cv1 T1" is reference compatible with "cv2 T2" if
1461 * T1 is reference-related to T2 or
1462 * T2 is "noexcept function" and T1 is "function", where the
1463 function types are otherwise the same,
1464 and cv1 is the same cv-qualification as, or greater cv-qualification
1465 than, cv2. */
1466 return ((reference_related_p (t1, t2)
1467 || fnptr_conv_p (t1, t2))
1468 && at_least_as_qualified_p (t1, t2));
1471 /* A reference of the indicated TYPE is being bound directly to the
1472 expression represented by the implicit conversion sequence CONV.
1473 Return a conversion sequence for this binding. */
1475 static conversion *
1476 direct_reference_binding (tree type, conversion *conv)
1478 tree t;
1480 gcc_assert (TYPE_REF_P (type));
1481 gcc_assert (!TYPE_REF_P (conv->type));
1483 t = TREE_TYPE (type);
1485 if (conv->kind == ck_identity)
1486 /* Mark the identity conv as to not decay to rvalue. */
1487 conv->rvaluedness_matches_p = true;
1489 /* [over.ics.rank]
1491 When a parameter of reference type binds directly
1492 (_dcl.init.ref_) to an argument expression, the implicit
1493 conversion sequence is the identity conversion, unless the
1494 argument expression has a type that is a derived class of the
1495 parameter type, in which case the implicit conversion sequence is
1496 a derived-to-base Conversion.
1498 If the parameter binds directly to the result of applying a
1499 conversion function to the argument expression, the implicit
1500 conversion sequence is a user-defined conversion sequence
1501 (_over.ics.user_), with the second standard conversion sequence
1502 either an identity conversion or, if the conversion function
1503 returns an entity of a type that is a derived class of the
1504 parameter type, a derived-to-base conversion. */
1505 if (is_properly_derived_from (conv->type, t))
1507 /* Represent the derived-to-base conversion. */
1508 conv = build_conv (ck_base, t, conv);
1509 /* We will actually be binding to the base-class subobject in
1510 the derived class, so we mark this conversion appropriately.
1511 That way, convert_like knows not to generate a temporary. */
1512 conv->need_temporary_p = false;
1515 return build_conv (ck_ref_bind, type, conv);
1518 /* Returns the conversion path from type FROM to reference type TO for
1519 purposes of reference binding. For lvalue binding, either pass a
1520 reference type to FROM or an lvalue expression to EXPR. If the
1521 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1522 the conversion returned. If C_CAST_P is true, this
1523 conversion is coming from a C-style cast. */
1525 static conversion *
1526 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1527 tsubst_flags_t complain)
1529 conversion *conv = NULL;
1530 tree to = TREE_TYPE (rto);
1531 tree from = rfrom;
1532 tree tfrom;
1533 bool related_p;
1534 bool compatible_p;
1535 cp_lvalue_kind gl_kind;
1536 bool is_lvalue;
1538 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1540 expr = instantiate_type (to, expr, tf_none);
1541 if (expr == error_mark_node)
1542 return NULL;
1543 from = TREE_TYPE (expr);
1546 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1548 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1549 /* DR 1288: Otherwise, if the initializer list has a single element
1550 of type E and ... [T's] referenced type is reference-related to E,
1551 the object or reference is initialized from that element... */
1552 if (CONSTRUCTOR_NELTS (expr) == 1)
1554 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1555 if (error_operand_p (elt))
1556 return NULL;
1557 tree etype = TREE_TYPE (elt);
1558 if (reference_related_p (to, etype))
1560 expr = elt;
1561 from = etype;
1562 goto skip;
1565 /* Otherwise, if T is a reference type, a prvalue temporary of the type
1566 referenced by T is copy-list-initialized, and the reference is bound
1567 to that temporary. */
1568 CONSTRUCTOR_IS_DIRECT_INIT (expr) = false;
1569 skip:;
1572 if (TYPE_REF_P (from))
1574 from = TREE_TYPE (from);
1575 if (!TYPE_REF_IS_RVALUE (rfrom)
1576 || TREE_CODE (from) == FUNCTION_TYPE)
1577 gl_kind = clk_ordinary;
1578 else
1579 gl_kind = clk_rvalueref;
1581 else if (expr)
1582 gl_kind = lvalue_kind (expr);
1583 else if (CLASS_TYPE_P (from)
1584 || TREE_CODE (from) == ARRAY_TYPE)
1585 gl_kind = clk_class;
1586 else
1587 gl_kind = clk_none;
1589 /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1590 if ((flags & LOOKUP_NO_TEMP_BIND)
1591 && (gl_kind & clk_class))
1592 gl_kind = clk_none;
1594 /* Same mask as real_lvalue_p. */
1595 is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1597 tfrom = from;
1598 if ((gl_kind & clk_bitfield) != 0)
1599 tfrom = unlowered_expr_type (expr);
1601 /* Figure out whether or not the types are reference-related and
1602 reference compatible. We have to do this after stripping
1603 references from FROM. */
1604 related_p = reference_related_p (to, tfrom);
1605 /* If this is a C cast, first convert to an appropriately qualified
1606 type, so that we can later do a const_cast to the desired type. */
1607 if (related_p && c_cast_p
1608 && !at_least_as_qualified_p (to, tfrom))
1609 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1610 compatible_p = reference_compatible_p (to, tfrom);
1612 /* Directly bind reference when target expression's type is compatible with
1613 the reference and expression is an lvalue. In DR391, the wording in
1614 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1615 const and rvalue references to rvalues of compatible class type.
1616 We should also do direct bindings for non-class xvalues. */
1617 if ((related_p || compatible_p) && gl_kind)
1619 /* [dcl.init.ref]
1621 If the initializer expression
1623 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1624 is reference-compatible with "cv2 T2,"
1626 the reference is bound directly to the initializer expression
1627 lvalue.
1629 [...]
1630 If the initializer expression is an rvalue, with T2 a class type,
1631 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1632 is bound to the object represented by the rvalue or to a sub-object
1633 within that object. */
1635 conv = build_identity_conv (tfrom, expr);
1636 conv = direct_reference_binding (rto, conv);
1638 if (TYPE_REF_P (rfrom))
1639 /* Handle rvalue reference to function properly. */
1640 conv->rvaluedness_matches_p
1641 = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1642 else
1643 conv->rvaluedness_matches_p
1644 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1646 if ((gl_kind & clk_bitfield) != 0
1647 || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1648 /* For the purposes of overload resolution, we ignore the fact
1649 this expression is a bitfield or packed field. (In particular,
1650 [over.ics.ref] says specifically that a function with a
1651 non-const reference parameter is viable even if the
1652 argument is a bitfield.)
1654 However, when we actually call the function we must create
1655 a temporary to which to bind the reference. If the
1656 reference is volatile, or isn't const, then we cannot make
1657 a temporary, so we just issue an error when the conversion
1658 actually occurs. */
1659 conv->need_temporary_p = true;
1661 /* Don't allow binding of lvalues (other than function lvalues) to
1662 rvalue references. */
1663 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1664 && TREE_CODE (to) != FUNCTION_TYPE)
1665 conv->bad_p = true;
1667 /* Nor the reverse. */
1668 if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
1669 && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
1670 || (flags & LOOKUP_NO_RVAL_BIND))
1671 && TREE_CODE (to) != FUNCTION_TYPE)
1672 conv->bad_p = true;
1674 if (!compatible_p)
1675 conv->bad_p = true;
1677 return conv;
1679 /* [class.conv.fct] A conversion function is never used to convert a
1680 (possibly cv-qualified) object to the (possibly cv-qualified) same
1681 object type (or a reference to it), to a (possibly cv-qualified) base
1682 class of that type (or a reference to it).... */
1683 else if (CLASS_TYPE_P (from) && !related_p
1684 && !(flags & LOOKUP_NO_CONVERSION))
1686 /* [dcl.init.ref]
1688 If the initializer expression
1690 -- has a class type (i.e., T2 is a class type) can be
1691 implicitly converted to an lvalue of type "cv3 T3," where
1692 "cv1 T1" is reference-compatible with "cv3 T3". (this
1693 conversion is selected by enumerating the applicable
1694 conversion functions (_over.match.ref_) and choosing the
1695 best one through overload resolution. (_over.match_).
1697 the reference is bound to the lvalue result of the conversion
1698 in the second case. */
1699 z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
1700 complain);
1701 if (cand)
1702 return cand->second_conv;
1705 /* From this point on, we conceptually need temporaries, even if we
1706 elide them. Only the cases above are "direct bindings". */
1707 if (flags & LOOKUP_NO_TEMP_BIND)
1708 return NULL;
1710 /* [over.ics.rank]
1712 When a parameter of reference type is not bound directly to an
1713 argument expression, the conversion sequence is the one required
1714 to convert the argument expression to the underlying type of the
1715 reference according to _over.best.ics_. Conceptually, this
1716 conversion sequence corresponds to copy-initializing a temporary
1717 of the underlying type with the argument expression. Any
1718 difference in top-level cv-qualification is subsumed by the
1719 initialization itself and does not constitute a conversion. */
1721 /* [dcl.init.ref]
1723 Otherwise, the reference shall be an lvalue reference to a
1724 non-volatile const type, or the reference shall be an rvalue
1725 reference.
1727 We try below to treat this as a bad conversion to improve diagnostics,
1728 but if TO is an incomplete class, we need to reject this conversion
1729 now to avoid unnecessary instantiation. */
1730 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto)
1731 && !COMPLETE_TYPE_P (to))
1732 return NULL;
1734 /* We're generating a temporary now, but don't bind any more in the
1735 conversion (specifically, don't slice the temporary returned by a
1736 conversion operator). */
1737 flags |= LOOKUP_NO_TEMP_BIND;
1739 /* Core issue 899: When [copy-]initializing a temporary to be bound
1740 to the first parameter of a copy constructor (12.8) called with
1741 a single argument in the context of direct-initialization,
1742 explicit conversion functions are also considered.
1744 So don't set LOOKUP_ONLYCONVERTING in that case. */
1745 if (!(flags & LOOKUP_COPY_PARM))
1746 flags |= LOOKUP_ONLYCONVERTING;
1748 if (!conv)
1749 conv = implicit_conversion (to, from, expr, c_cast_p,
1750 flags, complain);
1751 if (!conv)
1752 return NULL;
1754 if (conv->user_conv_p)
1756 /* If initializing the temporary used a conversion function,
1757 recalculate the second conversion sequence. */
1758 for (conversion *t = conv; t; t = next_conversion (t))
1759 if (t->kind == ck_user
1760 && DECL_CONV_FN_P (t->cand->fn))
1762 tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
1763 int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
1764 conversion *new_second
1765 = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
1766 sflags, complain);
1767 if (!new_second)
1768 return NULL;
1769 return merge_conversion_sequences (t, new_second);
1773 conv = build_conv (ck_ref_bind, rto, conv);
1774 /* This reference binding, unlike those above, requires the
1775 creation of a temporary. */
1776 conv->need_temporary_p = true;
1777 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1779 /* [dcl.init.ref]
1781 Otherwise, the reference shall be an lvalue reference to a
1782 non-volatile const type, or the reference shall be an rvalue
1783 reference. */
1784 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1785 conv->bad_p = true;
1787 /* [dcl.init.ref]
1789 Otherwise, a temporary of type "cv1 T1" is created and
1790 initialized from the initializer expression using the rules for a
1791 non-reference copy initialization. If T1 is reference-related to
1792 T2, cv1 must be the same cv-qualification as, or greater
1793 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1794 if (related_p && !at_least_as_qualified_p (to, from))
1795 conv->bad_p = true;
1797 return conv;
1800 /* Returns the implicit conversion sequence (see [over.ics]) from type
1801 FROM to type TO. The optional expression EXPR may affect the
1802 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1803 true, this conversion is coming from a C-style cast. */
1805 static conversion *
1806 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1807 int flags, tsubst_flags_t complain)
1809 conversion *conv;
1811 if (from == error_mark_node || to == error_mark_node
1812 || expr == error_mark_node)
1813 return NULL;
1815 /* Other flags only apply to the primary function in overload
1816 resolution, or after we've chosen one. */
1817 flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
1818 |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE
1819 |LOOKUP_NO_NARROWING|LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL);
1821 /* FIXME: actually we don't want warnings either, but we can't just
1822 have 'complain &= ~(tf_warning|tf_error)' because it would cause
1823 the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
1824 We really ought not to issue that warning until we've committed
1825 to that conversion. */
1826 complain &= ~tf_error;
1828 /* Call reshape_init early to remove redundant braces. */
1829 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)
1830 && CLASS_TYPE_P (to)
1831 && COMPLETE_TYPE_P (complete_type (to))
1832 && !CLASSTYPE_NON_AGGREGATE (to))
1834 expr = reshape_init (to, expr, complain);
1835 if (expr == error_mark_node)
1836 return NULL;
1837 from = TREE_TYPE (expr);
1840 if (TYPE_REF_P (to))
1841 conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
1842 else
1843 conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
1845 if (conv)
1846 return conv;
1848 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1850 if (is_std_init_list (to))
1851 return build_list_conv (to, expr, flags, complain);
1853 /* As an extension, allow list-initialization of _Complex. */
1854 if (TREE_CODE (to) == COMPLEX_TYPE)
1856 conv = build_complex_conv (to, expr, flags, complain);
1857 if (conv)
1858 return conv;
1861 /* Allow conversion from an initializer-list with one element to a
1862 scalar type. */
1863 if (SCALAR_TYPE_P (to))
1865 int nelts = CONSTRUCTOR_NELTS (expr);
1866 tree elt;
1868 if (nelts == 0)
1869 elt = build_value_init (to, tf_none);
1870 else if (nelts == 1)
1871 elt = CONSTRUCTOR_ELT (expr, 0)->value;
1872 else
1873 elt = error_mark_node;
1875 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
1876 c_cast_p, flags, complain);
1877 if (conv)
1879 conv->check_narrowing = true;
1880 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
1881 /* Too many levels of braces, i.e. '{{1}}'. */
1882 conv->bad_p = true;
1883 return conv;
1886 else if (TREE_CODE (to) == ARRAY_TYPE)
1887 return build_array_conv (to, expr, flags, complain);
1890 if (expr != NULL_TREE
1891 && (MAYBE_CLASS_TYPE_P (from)
1892 || MAYBE_CLASS_TYPE_P (to))
1893 && (flags & LOOKUP_NO_CONVERSION) == 0)
1895 struct z_candidate *cand;
1897 if (CLASS_TYPE_P (to)
1898 && BRACE_ENCLOSED_INITIALIZER_P (expr)
1899 && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
1900 return build_aggr_conv (to, expr, flags, complain);
1902 cand = build_user_type_conversion_1 (to, expr, flags, complain);
1903 if (cand)
1905 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
1906 && CONSTRUCTOR_NELTS (expr) == 1
1907 && !is_list_ctor (cand->fn))
1909 /* "If C is not an initializer-list constructor and the
1910 initializer list has a single element of type cv U, where U is
1911 X or a class derived from X, the implicit conversion sequence
1912 has Exact Match rank if U is X, or Conversion rank if U is
1913 derived from X." */
1914 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1915 tree elttype = TREE_TYPE (elt);
1916 if (reference_related_p (to, elttype))
1917 return implicit_conversion (to, elttype, elt,
1918 c_cast_p, flags, complain);
1920 conv = cand->second_conv;
1923 /* We used to try to bind a reference to a temporary here, but that
1924 is now handled after the recursive call to this function at the end
1925 of reference_binding. */
1926 return conv;
1929 return NULL;
1932 /* Like implicit_conversion, but return NULL if the conversion is bad.
1934 This is not static so that check_non_deducible_conversion can call it within
1935 add_template_candidate_real as part of overload resolution; it should not be
1936 called outside of overload resolution. */
1938 conversion *
1939 good_conversion (tree to, tree from, tree expr,
1940 int flags, tsubst_flags_t complain)
1942 conversion *c = implicit_conversion (to, from, expr, /*cast*/false,
1943 flags, complain);
1944 if (c && c->bad_p)
1945 c = NULL;
1946 return c;
1949 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1950 functions. ARGS will not be changed until a single candidate is
1951 selected. */
1953 static struct z_candidate *
1954 add_candidate (struct z_candidate **candidates,
1955 tree fn, tree first_arg, const vec<tree, va_gc> *args,
1956 size_t num_convs, conversion **convs,
1957 tree access_path, tree conversion_path,
1958 int viable, struct rejection_reason *reason,
1959 int flags)
1961 struct z_candidate *cand = (struct z_candidate *)
1962 conversion_obstack_alloc (sizeof (struct z_candidate));
1964 cand->fn = fn;
1965 cand->first_arg = first_arg;
1966 cand->args = args;
1967 cand->convs = convs;
1968 cand->num_convs = num_convs;
1969 cand->access_path = access_path;
1970 cand->conversion_path = conversion_path;
1971 cand->viable = viable;
1972 cand->reason = reason;
1973 cand->next = *candidates;
1974 cand->flags = flags;
1975 *candidates = cand;
1977 return cand;
1980 /* Return the number of remaining arguments in the parameter list
1981 beginning with ARG. */
1984 remaining_arguments (tree arg)
1986 int n;
1988 for (n = 0; arg != NULL_TREE && arg != void_list_node;
1989 arg = TREE_CHAIN (arg))
1990 n++;
1992 return n;
1995 /* [over.match.copy]: When initializing a temporary object (12.2) to be bound
1996 to the first parameter of a constructor where the parameter is of type
1997 "reference to possibly cv-qualified T" and the constructor is called with a
1998 single argument in the context of direct-initialization of an object of type
1999 "cv2 T", explicit conversion functions are also considered.
2001 So set LOOKUP_COPY_PARM to let reference_binding know that
2002 it's being called in that context. */
2005 conv_flags (int i, int nargs, tree fn, tree arg, int flags)
2007 int lflags = flags;
2008 tree t;
2009 if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn)
2010 && (t = FUNCTION_FIRST_USER_PARMTYPE (fn))
2011 && (same_type_ignoring_top_level_qualifiers_p
2012 (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn))))
2014 if (!(flags & LOOKUP_ONLYCONVERTING))
2015 lflags |= LOOKUP_COPY_PARM;
2016 if ((flags & LOOKUP_LIST_INIT_CTOR)
2017 && BRACE_ENCLOSED_INITIALIZER_P (arg))
2018 lflags |= LOOKUP_NO_CONVERSION;
2020 else
2021 lflags |= LOOKUP_ONLYCONVERTING;
2023 return lflags;
2026 /* Create an overload candidate for the function or method FN called
2027 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
2028 FLAGS is passed on to implicit_conversion.
2030 This does not change ARGS.
2032 CTYPE, if non-NULL, is the type we want to pretend this function
2033 comes from for purposes of overload resolution. */
2035 static struct z_candidate *
2036 add_function_candidate (struct z_candidate **candidates,
2037 tree fn, tree ctype, tree first_arg,
2038 const vec<tree, va_gc> *args, tree access_path,
2039 tree conversion_path, int flags,
2040 conversion **convs,
2041 tsubst_flags_t complain)
2043 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2044 int i, len;
2045 tree parmnode;
2046 tree orig_first_arg = first_arg;
2047 int skip;
2048 int viable = 1;
2049 struct rejection_reason *reason = NULL;
2051 /* At this point we should not see any functions which haven't been
2052 explicitly declared, except for friend functions which will have
2053 been found using argument dependent lookup. */
2054 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
2056 /* The `this', `in_chrg' and VTT arguments to constructors are not
2057 considered in overload resolution. */
2058 if (DECL_CONSTRUCTOR_P (fn))
2060 if (ctor_omit_inherited_parms (fn))
2061 /* Bring back parameters omitted from an inherited ctor. */
2062 parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2063 else
2064 parmlist = skip_artificial_parms_for (fn, parmlist);
2065 skip = num_artificial_parms_for (fn);
2066 if (skip > 0 && first_arg != NULL_TREE)
2068 --skip;
2069 first_arg = NULL_TREE;
2072 else
2073 skip = 0;
2075 len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2076 if (!convs)
2077 convs = alloc_conversions (len);
2079 /* 13.3.2 - Viable functions [over.match.viable]
2080 First, to be a viable function, a candidate function shall have enough
2081 parameters to agree in number with the arguments in the list.
2083 We need to check this first; otherwise, checking the ICSes might cause
2084 us to produce an ill-formed template instantiation. */
2086 parmnode = parmlist;
2087 for (i = 0; i < len; ++i)
2089 if (parmnode == NULL_TREE || parmnode == void_list_node)
2090 break;
2091 parmnode = TREE_CHAIN (parmnode);
2094 if ((i < len && parmnode)
2095 || !sufficient_parms_p (parmnode))
2097 int remaining = remaining_arguments (parmnode);
2098 viable = 0;
2099 reason = arity_rejection (first_arg, i + remaining, len);
2102 /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2103 parameter of type "reference to cv C" (including such a constructor
2104 instantiated from a template) is excluded from the set of candidate
2105 functions when used to construct an object of type D with an argument list
2106 containing a single argument if C is reference-related to D. */
2107 if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2108 && flag_new_inheriting_ctors
2109 && DECL_INHERITED_CTOR (fn))
2111 tree ptype = non_reference (TREE_VALUE (parmlist));
2112 tree dtype = DECL_CONTEXT (fn);
2113 tree btype = DECL_INHERITED_CTOR_BASE (fn);
2114 if (reference_related_p (ptype, dtype)
2115 && reference_related_p (btype, ptype))
2117 viable = false;
2118 reason = inherited_ctor_rejection ();
2122 /* Second, for a function to be viable, its constraints must be
2123 satisfied. */
2124 if (flag_concepts && viable
2125 && !constraints_satisfied_p (fn))
2127 reason = constraint_failure (fn);
2128 viable = false;
2131 /* When looking for a function from a subobject from an implicit
2132 copy/move constructor/operator=, don't consider anything that takes (a
2133 reference to) an unrelated type. See c++/44909 and core 1092. */
2134 if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2136 if (DECL_CONSTRUCTOR_P (fn))
2137 i = 1;
2138 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2139 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2140 i = 2;
2141 else
2142 i = 0;
2143 if (i && len == i)
2145 parmnode = chain_index (i-1, parmlist);
2146 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2147 ctype))
2148 viable = 0;
2151 /* This only applies at the top level. */
2152 flags &= ~LOOKUP_DEFAULTED;
2155 if (! viable)
2156 goto out;
2158 /* Third, for F to be a viable function, there shall exist for each
2159 argument an implicit conversion sequence that converts that argument
2160 to the corresponding parameter of F. */
2162 parmnode = parmlist;
2164 for (i = 0; i < len; ++i)
2166 tree argtype, to_type;
2167 tree arg;
2168 conversion *t;
2169 int is_this;
2171 if (parmnode == void_list_node)
2172 break;
2174 if (convs[i])
2176 /* Already set during deduction. */
2177 parmnode = TREE_CHAIN (parmnode);
2178 continue;
2181 if (i == 0 && first_arg != NULL_TREE)
2182 arg = first_arg;
2183 else
2184 arg = CONST_CAST_TREE (
2185 (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2186 argtype = lvalue_type (arg);
2188 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2189 && ! DECL_CONSTRUCTOR_P (fn));
2191 if (parmnode)
2193 tree parmtype = TREE_VALUE (parmnode);
2195 parmnode = TREE_CHAIN (parmnode);
2197 /* The type of the implicit object parameter ('this') for
2198 overload resolution is not always the same as for the
2199 function itself; conversion functions are considered to
2200 be members of the class being converted, and functions
2201 introduced by a using-declaration are considered to be
2202 members of the class that uses them.
2204 Since build_over_call ignores the ICS for the `this'
2205 parameter, we can just change the parm type. */
2206 if (ctype && is_this)
2208 parmtype = cp_build_qualified_type
2209 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
2210 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2212 /* If the function has a ref-qualifier, the implicit
2213 object parameter has reference type. */
2214 bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2215 parmtype = cp_build_reference_type (parmtype, rv);
2216 /* The special handling of 'this' conversions in compare_ics
2217 does not apply if there is a ref-qualifier. */
2218 is_this = false;
2220 else
2222 parmtype = build_pointer_type (parmtype);
2223 /* We don't use build_this here because we don't want to
2224 capture the object argument until we've chosen a
2225 non-static member function. */
2226 arg = build_address (arg);
2227 argtype = lvalue_type (arg);
2231 int lflags = conv_flags (i, len-skip, fn, arg, flags);
2233 t = implicit_conversion (parmtype, argtype, arg,
2234 /*c_cast_p=*/false, lflags, complain);
2235 to_type = parmtype;
2237 else
2239 t = build_identity_conv (argtype, arg);
2240 t->ellipsis_p = true;
2241 to_type = argtype;
2244 if (t && is_this)
2245 t->this_p = true;
2247 convs[i] = t;
2248 if (! t)
2250 viable = 0;
2251 reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
2252 break;
2255 if (t->bad_p)
2257 viable = -1;
2258 reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type);
2262 out:
2263 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2264 access_path, conversion_path, viable, reason, flags);
2267 /* Create an overload candidate for the conversion function FN which will
2268 be invoked for expression OBJ, producing a pointer-to-function which
2269 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2270 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2271 passed on to implicit_conversion.
2273 Actually, we don't really care about FN; we care about the type it
2274 converts to. There may be multiple conversion functions that will
2275 convert to that type, and we rely on build_user_type_conversion_1 to
2276 choose the best one; so when we create our candidate, we record the type
2277 instead of the function. */
2279 static struct z_candidate *
2280 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2281 const vec<tree, va_gc> *arglist,
2282 tree access_path, tree conversion_path,
2283 tsubst_flags_t complain)
2285 tree totype = TREE_TYPE (TREE_TYPE (fn));
2286 int i, len, viable, flags;
2287 tree parmlist, parmnode;
2288 conversion **convs;
2289 struct rejection_reason *reason;
2291 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2292 parmlist = TREE_TYPE (parmlist);
2293 parmlist = TYPE_ARG_TYPES (parmlist);
2295 len = vec_safe_length (arglist) + 1;
2296 convs = alloc_conversions (len);
2297 parmnode = parmlist;
2298 viable = 1;
2299 flags = LOOKUP_IMPLICIT;
2300 reason = NULL;
2302 /* Don't bother looking up the same type twice. */
2303 if (*candidates && (*candidates)->fn == totype)
2304 return NULL;
2306 for (i = 0; i < len; ++i)
2308 tree arg, argtype, convert_type = NULL_TREE;
2309 conversion *t;
2311 if (i == 0)
2312 arg = obj;
2313 else
2314 arg = (*arglist)[i - 1];
2315 argtype = lvalue_type (arg);
2317 if (i == 0)
2319 t = build_identity_conv (argtype, NULL_TREE);
2320 t = build_conv (ck_user, totype, t);
2321 /* Leave the 'cand' field null; we'll figure out the conversion in
2322 convert_like_real if this candidate is chosen. */
2323 convert_type = totype;
2325 else if (parmnode == void_list_node)
2326 break;
2327 else if (parmnode)
2329 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2330 /*c_cast_p=*/false, flags, complain);
2331 convert_type = TREE_VALUE (parmnode);
2333 else
2335 t = build_identity_conv (argtype, arg);
2336 t->ellipsis_p = true;
2337 convert_type = argtype;
2340 convs[i] = t;
2341 if (! t)
2342 break;
2344 if (t->bad_p)
2346 viable = -1;
2347 reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type);
2350 if (i == 0)
2351 continue;
2353 if (parmnode)
2354 parmnode = TREE_CHAIN (parmnode);
2357 if (i < len
2358 || ! sufficient_parms_p (parmnode))
2360 int remaining = remaining_arguments (parmnode);
2361 viable = 0;
2362 reason = arity_rejection (NULL_TREE, i + remaining, len);
2365 return add_candidate (candidates, totype, obj, arglist, len, convs,
2366 access_path, conversion_path, viable, reason, flags);
2369 static void
2370 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2371 tree type1, tree type2, tree *args, tree *argtypes,
2372 int flags, tsubst_flags_t complain)
2374 conversion *t;
2375 conversion **convs;
2376 size_t num_convs;
2377 int viable = 1, i;
2378 tree types[2];
2379 struct rejection_reason *reason = NULL;
2381 types[0] = type1;
2382 types[1] = type2;
2384 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
2385 convs = alloc_conversions (num_convs);
2387 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2388 conversion ops are allowed. We handle that here by just checking for
2389 boolean_type_node because other operators don't ask for it. COND_EXPR
2390 also does contextual conversion to bool for the first operand, but we
2391 handle that in build_conditional_expr, and type1 here is operand 2. */
2392 if (type1 != boolean_type_node)
2393 flags |= LOOKUP_ONLYCONVERTING;
2395 for (i = 0; i < 2; ++i)
2397 if (! args[i])
2398 break;
2400 t = implicit_conversion (types[i], argtypes[i], args[i],
2401 /*c_cast_p=*/false, flags, complain);
2402 if (! t)
2404 viable = 0;
2405 /* We need something for printing the candidate. */
2406 t = build_identity_conv (types[i], NULL_TREE);
2407 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2408 types[i]);
2410 else if (t->bad_p)
2412 viable = 0;
2413 reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2414 types[i]);
2416 convs[i] = t;
2419 /* For COND_EXPR we rearranged the arguments; undo that now. */
2420 if (args[2])
2422 convs[2] = convs[1];
2423 convs[1] = convs[0];
2424 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2425 /*c_cast_p=*/false, flags,
2426 complain);
2427 if (t)
2428 convs[0] = t;
2429 else
2431 viable = 0;
2432 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2433 boolean_type_node);
2437 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2438 num_convs, convs,
2439 /*access_path=*/NULL_TREE,
2440 /*conversion_path=*/NULL_TREE,
2441 viable, reason, flags);
2444 static bool
2445 is_complete (tree t)
2447 return COMPLETE_TYPE_P (complete_type (t));
2450 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2452 static bool
2453 promoted_arithmetic_type_p (tree type)
2455 /* [over.built]
2457 In this section, the term promoted integral type is used to refer
2458 to those integral types which are preserved by integral promotion
2459 (including e.g. int and long but excluding e.g. char).
2460 Similarly, the term promoted arithmetic type refers to promoted
2461 integral types plus floating types. */
2462 return ((CP_INTEGRAL_TYPE_P (type)
2463 && same_type_p (type_promotes_to (type), type))
2464 || TREE_CODE (type) == REAL_TYPE);
2467 /* Create any builtin operator overload candidates for the operator in
2468 question given the converted operand types TYPE1 and TYPE2. The other
2469 args are passed through from add_builtin_candidates to
2470 build_builtin_candidate.
2472 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2473 If CODE is requires candidates operands of the same type of the kind
2474 of which TYPE1 and TYPE2 are, we add both candidates
2475 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2477 static void
2478 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2479 enum tree_code code2, tree fnname, tree type1,
2480 tree type2, tree *args, tree *argtypes, int flags,
2481 tsubst_flags_t complain)
2483 switch (code)
2485 case POSTINCREMENT_EXPR:
2486 case POSTDECREMENT_EXPR:
2487 args[1] = integer_zero_node;
2488 type2 = integer_type_node;
2489 break;
2490 default:
2491 break;
2494 switch (code)
2497 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2498 and VQ is either volatile or empty, there exist candidate operator
2499 functions of the form
2500 VQ T& operator++(VQ T&);
2501 T operator++(VQ T&, int);
2502 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
2503 type other than bool, and VQ is either volatile or empty, there exist
2504 candidate operator functions of the form
2505 VQ T& operator--(VQ T&);
2506 T operator--(VQ T&, int);
2507 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
2508 complete object type, and VQ is either volatile or empty, there exist
2509 candidate operator functions of the form
2510 T*VQ& operator++(T*VQ&);
2511 T*VQ& operator--(T*VQ&);
2512 T* operator++(T*VQ&, int);
2513 T* operator--(T*VQ&, int); */
2515 case POSTDECREMENT_EXPR:
2516 case PREDECREMENT_EXPR:
2517 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2518 return;
2519 /* FALLTHRU */
2520 case POSTINCREMENT_EXPR:
2521 case PREINCREMENT_EXPR:
2522 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2524 type1 = build_reference_type (type1);
2525 break;
2527 return;
2529 /* 7 For every cv-qualified or cv-unqualified object type T, there
2530 exist candidate operator functions of the form
2532 T& operator*(T*);
2534 8 For every function type T, there exist candidate operator functions of
2535 the form
2536 T& operator*(T*); */
2538 case INDIRECT_REF:
2539 if (TYPE_PTR_P (type1)
2540 && (TYPE_PTROB_P (type1)
2541 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2542 break;
2543 return;
2545 /* 9 For every type T, there exist candidate operator functions of the form
2546 T* operator+(T*);
2548 10For every promoted arithmetic type T, there exist candidate operator
2549 functions of the form
2550 T operator+(T);
2551 T operator-(T); */
2553 case UNARY_PLUS_EXPR: /* unary + */
2554 if (TYPE_PTR_P (type1))
2555 break;
2556 /* FALLTHRU */
2557 case NEGATE_EXPR:
2558 if (ARITHMETIC_TYPE_P (type1))
2559 break;
2560 return;
2562 /* 11For every promoted integral type T, there exist candidate operator
2563 functions of the form
2564 T operator~(T); */
2566 case BIT_NOT_EXPR:
2567 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2568 break;
2569 return;
2571 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2572 is the same type as C2 or is a derived class of C2, T is a complete
2573 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2574 there exist candidate operator functions of the form
2575 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2576 where CV12 is the union of CV1 and CV2. */
2578 case MEMBER_REF:
2579 if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2581 tree c1 = TREE_TYPE (type1);
2582 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2584 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2585 && (TYPE_PTRMEMFUNC_P (type2)
2586 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2587 break;
2589 return;
2591 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2592 didate operator functions of the form
2593 LR operator*(L, R);
2594 LR operator/(L, R);
2595 LR operator+(L, R);
2596 LR operator-(L, R);
2597 bool operator<(L, R);
2598 bool operator>(L, R);
2599 bool operator<=(L, R);
2600 bool operator>=(L, R);
2601 bool operator==(L, R);
2602 bool operator!=(L, R);
2603 where LR is the result of the usual arithmetic conversions between
2604 types L and R.
2606 14For every pair of types T and I, where T is a cv-qualified or cv-
2607 unqualified complete object type and I is a promoted integral type,
2608 there exist candidate operator functions of the form
2609 T* operator+(T*, I);
2610 T& operator[](T*, I);
2611 T* operator-(T*, I);
2612 T* operator+(I, T*);
2613 T& operator[](I, T*);
2615 15For every T, where T is a pointer to complete object type, there exist
2616 candidate operator functions of the form112)
2617 ptrdiff_t operator-(T, T);
2619 16For every pointer or enumeration type T, there exist candidate operator
2620 functions of the form
2621 bool operator<(T, T);
2622 bool operator>(T, T);
2623 bool operator<=(T, T);
2624 bool operator>=(T, T);
2625 bool operator==(T, T);
2626 bool operator!=(T, T);
2628 17For every pointer to member type T, there exist candidate operator
2629 functions of the form
2630 bool operator==(T, T);
2631 bool operator!=(T, T); */
2633 case MINUS_EXPR:
2634 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2635 break;
2636 if (TYPE_PTROB_P (type1)
2637 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2639 type2 = ptrdiff_type_node;
2640 break;
2642 /* FALLTHRU */
2643 case MULT_EXPR:
2644 case TRUNC_DIV_EXPR:
2645 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2646 break;
2647 return;
2649 case EQ_EXPR:
2650 case NE_EXPR:
2651 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2652 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
2653 break;
2654 if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
2656 type2 = type1;
2657 break;
2659 if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
2661 type1 = type2;
2662 break;
2664 /* Fall through. */
2665 case LT_EXPR:
2666 case GT_EXPR:
2667 case LE_EXPR:
2668 case GE_EXPR:
2669 case MAX_EXPR:
2670 case MIN_EXPR:
2671 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2672 break;
2673 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2674 break;
2675 if (TREE_CODE (type1) == ENUMERAL_TYPE
2676 && TREE_CODE (type2) == ENUMERAL_TYPE)
2677 break;
2678 if (TYPE_PTR_P (type1)
2679 && null_ptr_cst_p (args[1]))
2681 type2 = type1;
2682 break;
2684 if (null_ptr_cst_p (args[0])
2685 && TYPE_PTR_P (type2))
2687 type1 = type2;
2688 break;
2690 return;
2692 case PLUS_EXPR:
2693 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2694 break;
2695 /* FALLTHRU */
2696 case ARRAY_REF:
2697 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2699 type1 = ptrdiff_type_node;
2700 break;
2702 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2704 type2 = ptrdiff_type_node;
2705 break;
2707 return;
2709 /* 18For every pair of promoted integral types L and R, there exist candi-
2710 date operator functions of the form
2711 LR operator%(L, R);
2712 LR operator&(L, R);
2713 LR operator^(L, R);
2714 LR operator|(L, R);
2715 L operator<<(L, R);
2716 L operator>>(L, R);
2717 where LR is the result of the usual arithmetic conversions between
2718 types L and R. */
2720 case TRUNC_MOD_EXPR:
2721 case BIT_AND_EXPR:
2722 case BIT_IOR_EXPR:
2723 case BIT_XOR_EXPR:
2724 case LSHIFT_EXPR:
2725 case RSHIFT_EXPR:
2726 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2727 break;
2728 return;
2730 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2731 type, VQ is either volatile or empty, and R is a promoted arithmetic
2732 type, there exist candidate operator functions of the form
2733 VQ L& operator=(VQ L&, R);
2734 VQ L& operator*=(VQ L&, R);
2735 VQ L& operator/=(VQ L&, R);
2736 VQ L& operator+=(VQ L&, R);
2737 VQ L& operator-=(VQ L&, R);
2739 20For every pair T, VQ), where T is any type and VQ is either volatile
2740 or empty, there exist candidate operator functions of the form
2741 T*VQ& operator=(T*VQ&, T*);
2743 21For every pair T, VQ), where T is a pointer to member type and VQ is
2744 either volatile or empty, there exist candidate operator functions of
2745 the form
2746 VQ T& operator=(VQ T&, T);
2748 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2749 unqualified complete object type, VQ is either volatile or empty, and
2750 I is a promoted integral type, there exist candidate operator func-
2751 tions of the form
2752 T*VQ& operator+=(T*VQ&, I);
2753 T*VQ& operator-=(T*VQ&, I);
2755 23For every triple L, VQ, R), where L is an integral or enumeration
2756 type, VQ is either volatile or empty, and R is a promoted integral
2757 type, there exist candidate operator functions of the form
2759 VQ L& operator%=(VQ L&, R);
2760 VQ L& operator<<=(VQ L&, R);
2761 VQ L& operator>>=(VQ L&, R);
2762 VQ L& operator&=(VQ L&, R);
2763 VQ L& operator^=(VQ L&, R);
2764 VQ L& operator|=(VQ L&, R); */
2766 case MODIFY_EXPR:
2767 switch (code2)
2769 case PLUS_EXPR:
2770 case MINUS_EXPR:
2771 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2773 type2 = ptrdiff_type_node;
2774 break;
2776 /* FALLTHRU */
2777 case MULT_EXPR:
2778 case TRUNC_DIV_EXPR:
2779 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2780 break;
2781 return;
2783 case TRUNC_MOD_EXPR:
2784 case BIT_AND_EXPR:
2785 case BIT_IOR_EXPR:
2786 case BIT_XOR_EXPR:
2787 case LSHIFT_EXPR:
2788 case RSHIFT_EXPR:
2789 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2790 break;
2791 return;
2793 case NOP_EXPR:
2794 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2795 break;
2796 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2797 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2798 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2799 || ((TYPE_PTRMEMFUNC_P (type1)
2800 || TYPE_PTR_P (type1))
2801 && null_ptr_cst_p (args[1])))
2803 type2 = type1;
2804 break;
2806 return;
2808 default:
2809 gcc_unreachable ();
2811 type1 = build_reference_type (type1);
2812 break;
2814 case COND_EXPR:
2815 /* [over.built]
2817 For every pair of promoted arithmetic types L and R, there
2818 exist candidate operator functions of the form
2820 LR operator?(bool, L, R);
2822 where LR is the result of the usual arithmetic conversions
2823 between types L and R.
2825 For every type T, where T is a pointer or pointer-to-member
2826 type, there exist candidate operator functions of the form T
2827 operator?(bool, T, T); */
2829 if (promoted_arithmetic_type_p (type1)
2830 && promoted_arithmetic_type_p (type2))
2831 /* That's OK. */
2832 break;
2834 /* Otherwise, the types should be pointers. */
2835 if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
2836 return;
2838 /* We don't check that the two types are the same; the logic
2839 below will actually create two candidates; one in which both
2840 parameter types are TYPE1, and one in which both parameter
2841 types are TYPE2. */
2842 break;
2844 case REALPART_EXPR:
2845 case IMAGPART_EXPR:
2846 if (ARITHMETIC_TYPE_P (type1))
2847 break;
2848 return;
2850 default:
2851 gcc_unreachable ();
2854 /* Make sure we don't create builtin candidates with dependent types. */
2855 bool u1 = uses_template_parms (type1);
2856 bool u2 = type2 ? uses_template_parms (type2) : false;
2857 if (u1 || u2)
2859 /* Try to recover if one of the types is non-dependent. But if
2860 there's only one type, there's nothing we can do. */
2861 if (!type2)
2862 return;
2863 /* And we lose if both are dependent. */
2864 if (u1 && u2)
2865 return;
2866 /* Or if they have different forms. */
2867 if (TREE_CODE (type1) != TREE_CODE (type2))
2868 return;
2870 if (u1 && !u2)
2871 type1 = type2;
2872 else if (u2 && !u1)
2873 type2 = type1;
2876 /* If we're dealing with two pointer types or two enumeral types,
2877 we need candidates for both of them. */
2878 if (type2 && !same_type_p (type1, type2)
2879 && TREE_CODE (type1) == TREE_CODE (type2)
2880 && (TYPE_REF_P (type1)
2881 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2882 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2883 || TYPE_PTRMEMFUNC_P (type1)
2884 || MAYBE_CLASS_TYPE_P (type1)
2885 || TREE_CODE (type1) == ENUMERAL_TYPE))
2887 if (TYPE_PTR_OR_PTRMEM_P (type1))
2889 tree cptype = composite_pointer_type (type1, type2,
2890 error_mark_node,
2891 error_mark_node,
2892 CPO_CONVERSION,
2893 tf_none);
2894 if (cptype != error_mark_node)
2896 build_builtin_candidate
2897 (candidates, fnname, cptype, cptype, args, argtypes,
2898 flags, complain);
2899 return;
2903 build_builtin_candidate
2904 (candidates, fnname, type1, type1, args, argtypes, flags, complain);
2905 build_builtin_candidate
2906 (candidates, fnname, type2, type2, args, argtypes, flags, complain);
2907 return;
2910 build_builtin_candidate
2911 (candidates, fnname, type1, type2, args, argtypes, flags, complain);
2914 tree
2915 type_decays_to (tree type)
2917 if (TREE_CODE (type) == ARRAY_TYPE)
2918 return build_pointer_type (TREE_TYPE (type));
2919 if (TREE_CODE (type) == FUNCTION_TYPE)
2920 return build_pointer_type (type);
2921 return type;
2924 /* There are three conditions of builtin candidates:
2926 1) bool-taking candidates. These are the same regardless of the input.
2927 2) pointer-pair taking candidates. These are generated for each type
2928 one of the input types converts to.
2929 3) arithmetic candidates. According to the standard, we should generate
2930 all of these, but I'm trying not to...
2932 Here we generate a superset of the possible candidates for this particular
2933 case. That is a subset of the full set the standard defines, plus some
2934 other cases which the standard disallows. add_builtin_candidate will
2935 filter out the invalid set. */
2937 static void
2938 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2939 enum tree_code code2, tree fnname, tree *args,
2940 int flags, tsubst_flags_t complain)
2942 int ref1, i;
2943 int enum_p = 0;
2944 tree type, argtypes[3], t;
2945 /* TYPES[i] is the set of possible builtin-operator parameter types
2946 we will consider for the Ith argument. */
2947 vec<tree, va_gc> *types[2];
2948 unsigned ix;
2950 for (i = 0; i < 3; ++i)
2952 if (args[i])
2953 argtypes[i] = unlowered_expr_type (args[i]);
2954 else
2955 argtypes[i] = NULL_TREE;
2958 switch (code)
2960 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2961 and VQ is either volatile or empty, there exist candidate operator
2962 functions of the form
2963 VQ T& operator++(VQ T&); */
2965 case POSTINCREMENT_EXPR:
2966 case PREINCREMENT_EXPR:
2967 case POSTDECREMENT_EXPR:
2968 case PREDECREMENT_EXPR:
2969 case MODIFY_EXPR:
2970 ref1 = 1;
2971 break;
2973 /* 24There also exist candidate operator functions of the form
2974 bool operator!(bool);
2975 bool operator&&(bool, bool);
2976 bool operator||(bool, bool); */
2978 case TRUTH_NOT_EXPR:
2979 build_builtin_candidate
2980 (candidates, fnname, boolean_type_node,
2981 NULL_TREE, args, argtypes, flags, complain);
2982 return;
2984 case TRUTH_ORIF_EXPR:
2985 case TRUTH_ANDIF_EXPR:
2986 build_builtin_candidate
2987 (candidates, fnname, boolean_type_node,
2988 boolean_type_node, args, argtypes, flags, complain);
2989 return;
2991 case ADDR_EXPR:
2992 case COMPOUND_EXPR:
2993 case COMPONENT_REF:
2994 return;
2996 case COND_EXPR:
2997 case EQ_EXPR:
2998 case NE_EXPR:
2999 case LT_EXPR:
3000 case LE_EXPR:
3001 case GT_EXPR:
3002 case GE_EXPR:
3003 enum_p = 1;
3004 /* Fall through. */
3006 default:
3007 ref1 = 0;
3010 types[0] = make_tree_vector ();
3011 types[1] = make_tree_vector ();
3013 for (i = 0; i < 2; ++i)
3015 if (! args[i])
3017 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
3019 tree convs;
3021 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
3022 return;
3024 convs = lookup_conversions (argtypes[i]);
3026 if (code == COND_EXPR)
3028 if (lvalue_p (args[i]))
3029 vec_safe_push (types[i], build_reference_type (argtypes[i]));
3031 vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
3034 else if (! convs)
3035 return;
3037 for (; convs; convs = TREE_CHAIN (convs))
3039 type = TREE_TYPE (convs);
3041 if (i == 0 && ref1
3042 && (!TYPE_REF_P (type)
3043 || CP_TYPE_CONST_P (TREE_TYPE (type))))
3044 continue;
3046 if (code == COND_EXPR && TYPE_REF_P (type))
3047 vec_safe_push (types[i], type);
3049 type = non_reference (type);
3050 if (i != 0 || ! ref1)
3052 type = cv_unqualified (type_decays_to (type));
3053 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3054 vec_safe_push (types[i], type);
3055 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3056 type = type_promotes_to (type);
3059 if (! vec_member (type, types[i]))
3060 vec_safe_push (types[i], type);
3063 else
3065 if (code == COND_EXPR && lvalue_p (args[i]))
3066 vec_safe_push (types[i], build_reference_type (argtypes[i]));
3067 type = non_reference (argtypes[i]);
3068 if (i != 0 || ! ref1)
3070 type = cv_unqualified (type_decays_to (type));
3071 if (enum_p && UNSCOPED_ENUM_P (type))
3072 vec_safe_push (types[i], type);
3073 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3074 type = type_promotes_to (type);
3076 vec_safe_push (types[i], type);
3080 /* Run through the possible parameter types of both arguments,
3081 creating candidates with those parameter types. */
3082 FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3084 unsigned jx;
3085 tree u;
3087 if (!types[1]->is_empty ())
3088 FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3089 add_builtin_candidate
3090 (candidates, code, code2, fnname, t,
3091 u, args, argtypes, flags, complain);
3092 else
3093 add_builtin_candidate
3094 (candidates, code, code2, fnname, t,
3095 NULL_TREE, args, argtypes, flags, complain);
3098 release_tree_vector (types[0]);
3099 release_tree_vector (types[1]);
3103 /* If TMPL can be successfully instantiated as indicated by
3104 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3106 TMPL is the template. EXPLICIT_TARGS are any explicit template
3107 arguments. ARGLIST is the arguments provided at the call-site.
3108 This does not change ARGLIST. The RETURN_TYPE is the desired type
3109 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3110 as for add_function_candidate. If an OBJ is supplied, FLAGS and
3111 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
3113 static struct z_candidate*
3114 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3115 tree ctype, tree explicit_targs, tree first_arg,
3116 const vec<tree, va_gc> *arglist, tree return_type,
3117 tree access_path, tree conversion_path,
3118 int flags, tree obj, unification_kind_t strict,
3119 tsubst_flags_t complain)
3121 int ntparms = DECL_NTPARMS (tmpl);
3122 tree targs = make_tree_vec (ntparms);
3123 unsigned int len = vec_safe_length (arglist);
3124 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3125 unsigned int skip_without_in_chrg = 0;
3126 tree first_arg_without_in_chrg = first_arg;
3127 tree *args_without_in_chrg;
3128 unsigned int nargs_without_in_chrg;
3129 unsigned int ia, ix;
3130 tree arg;
3131 struct z_candidate *cand;
3132 tree fn;
3133 struct rejection_reason *reason = NULL;
3134 int errs;
3135 conversion **convs = NULL;
3137 /* We don't do deduction on the in-charge parameter, the VTT
3138 parameter or 'this'. */
3139 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
3141 if (first_arg_without_in_chrg != NULL_TREE)
3142 first_arg_without_in_chrg = NULL_TREE;
3143 else if (return_type && strict == DEDUCE_CALL)
3144 /* We're deducing for a call to the result of a template conversion
3145 function, so the args don't contain 'this'; leave them alone. */;
3146 else
3147 ++skip_without_in_chrg;
3150 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3151 || DECL_BASE_CONSTRUCTOR_P (tmpl))
3152 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3154 if (first_arg_without_in_chrg != NULL_TREE)
3155 first_arg_without_in_chrg = NULL_TREE;
3156 else
3157 ++skip_without_in_chrg;
3160 if (len < skip_without_in_chrg)
3161 return NULL;
3163 if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3164 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3165 TREE_TYPE ((*arglist)[0])))
3167 /* 12.8/6 says, "A declaration of a constructor for a class X is
3168 ill-formed if its first parameter is of type (optionally cv-qualified)
3169 X and either there are no other parameters or else all other
3170 parameters have default arguments. A member function template is never
3171 instantiated to produce such a constructor signature."
3173 So if we're trying to copy an object of the containing class, don't
3174 consider a template constructor that has a first parameter type that
3175 is just a template parameter, as we would deduce a signature that we
3176 would then reject in the code below. */
3177 if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3179 firstparm = TREE_VALUE (firstparm);
3180 if (PACK_EXPANSION_P (firstparm))
3181 firstparm = PACK_EXPANSION_PATTERN (firstparm);
3182 if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3184 gcc_assert (!explicit_targs);
3185 reason = invalid_copy_with_fn_template_rejection ();
3186 goto fail;
3191 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3192 + (len - skip_without_in_chrg));
3193 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3194 ia = 0;
3195 if (first_arg_without_in_chrg != NULL_TREE)
3197 args_without_in_chrg[ia] = first_arg_without_in_chrg;
3198 ++ia;
3200 for (ix = skip_without_in_chrg;
3201 vec_safe_iterate (arglist, ix, &arg);
3202 ++ix)
3204 args_without_in_chrg[ia] = arg;
3205 ++ia;
3207 gcc_assert (ia == nargs_without_in_chrg);
3209 errs = errorcount+sorrycount;
3210 if (!obj)
3211 convs = alloc_conversions (nargs);
3212 fn = fn_type_unification (tmpl, explicit_targs, targs,
3213 args_without_in_chrg,
3214 nargs_without_in_chrg,
3215 return_type, strict, flags, convs,
3216 false, complain & tf_decltype);
3218 if (fn == error_mark_node)
3220 /* Don't repeat unification later if it already resulted in errors. */
3221 if (errorcount+sorrycount == errs)
3222 reason = template_unification_rejection (tmpl, explicit_targs,
3223 targs, args_without_in_chrg,
3224 nargs_without_in_chrg,
3225 return_type, strict, flags);
3226 else
3227 reason = template_unification_error_rejection ();
3228 goto fail;
3231 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3233 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3234 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3235 ctype))
3237 /* We're trying to produce a constructor with a prohibited signature,
3238 as discussed above; handle here any cases we didn't catch then,
3239 such as X(X<T>). */
3240 reason = invalid_copy_with_fn_template_rejection ();
3241 goto fail;
3245 if (obj != NULL_TREE)
3246 /* Aha, this is a conversion function. */
3247 cand = add_conv_candidate (candidates, fn, obj, arglist,
3248 access_path, conversion_path, complain);
3249 else
3250 cand = add_function_candidate (candidates, fn, ctype,
3251 first_arg, arglist, access_path,
3252 conversion_path, flags, convs, complain);
3253 if (DECL_TI_TEMPLATE (fn) != tmpl)
3254 /* This situation can occur if a member template of a template
3255 class is specialized. Then, instantiate_template might return
3256 an instantiation of the specialization, in which case the
3257 DECL_TI_TEMPLATE field will point at the original
3258 specialization. For example:
3260 template <class T> struct S { template <class U> void f(U);
3261 template <> void f(int) {}; };
3262 S<double> sd;
3263 sd.f(3);
3265 Here, TMPL will be template <class U> S<double>::f(U).
3266 And, instantiate template will give us the specialization
3267 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3268 for this will point at template <class T> template <> S<T>::f(int),
3269 so that we can find the definition. For the purposes of
3270 overload resolution, however, we want the original TMPL. */
3271 cand->template_decl = build_template_info (tmpl, targs);
3272 else
3273 cand->template_decl = DECL_TEMPLATE_INFO (fn);
3274 cand->explicit_targs = explicit_targs;
3276 return cand;
3277 fail:
3278 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
3279 access_path, conversion_path, 0, reason, flags);
3283 static struct z_candidate *
3284 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3285 tree explicit_targs, tree first_arg,
3286 const vec<tree, va_gc> *arglist, tree return_type,
3287 tree access_path, tree conversion_path, int flags,
3288 unification_kind_t strict, tsubst_flags_t complain)
3290 return
3291 add_template_candidate_real (candidates, tmpl, ctype,
3292 explicit_targs, first_arg, arglist,
3293 return_type, access_path, conversion_path,
3294 flags, NULL_TREE, strict, complain);
3297 /* Create an overload candidate for the conversion function template TMPL,
3298 returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3299 pointer-to-function which will in turn be called with the argument list
3300 ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3301 passed on to implicit_conversion. */
3303 static struct z_candidate *
3304 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3305 tree obj,
3306 const vec<tree, va_gc> *arglist,
3307 tree return_type, tree access_path,
3308 tree conversion_path, tsubst_flags_t complain)
3310 /* Making this work broke PR 71117 and 85118, so until the committee resolves
3311 core issue 2189, let's disable this candidate if there are any call
3312 operators. */
3313 if (*candidates)
3314 return NULL;
3316 return
3317 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3318 NULL_TREE, arglist, return_type, access_path,
3319 conversion_path, 0, obj, DEDUCE_CALL,
3320 complain);
3323 /* The CANDS are the set of candidates that were considered for
3324 overload resolution. Return the set of viable candidates, or CANDS
3325 if none are viable. If any of the candidates were viable, set
3326 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3327 considered viable only if it is strictly viable. */
3329 static struct z_candidate*
3330 splice_viable (struct z_candidate *cands,
3331 bool strict_p,
3332 bool *any_viable_p)
3334 struct z_candidate *viable;
3335 struct z_candidate **last_viable;
3336 struct z_candidate **cand;
3337 bool found_strictly_viable = false;
3339 /* Be strict inside templates, since build_over_call won't actually
3340 do the conversions to get pedwarns. */
3341 if (processing_template_decl)
3342 strict_p = true;
3344 viable = NULL;
3345 last_viable = &viable;
3346 *any_viable_p = false;
3348 cand = &cands;
3349 while (*cand)
3351 struct z_candidate *c = *cand;
3352 if (!strict_p
3353 && (c->viable == 1 || TREE_CODE (c->fn) == TEMPLATE_DECL))
3355 /* Be strict in the presence of a viable candidate. Also if
3356 there are template candidates, so that we get deduction errors
3357 for them instead of silently preferring a bad conversion. */
3358 strict_p = true;
3359 if (viable && !found_strictly_viable)
3361 /* Put any spliced near matches back onto the main list so
3362 that we see them if there is no strict match. */
3363 *any_viable_p = false;
3364 *last_viable = cands;
3365 cands = viable;
3366 viable = NULL;
3367 last_viable = &viable;
3371 if (strict_p ? c->viable == 1 : c->viable)
3373 *last_viable = c;
3374 *cand = c->next;
3375 c->next = NULL;
3376 last_viable = &c->next;
3377 *any_viable_p = true;
3378 if (c->viable == 1)
3379 found_strictly_viable = true;
3381 else
3382 cand = &c->next;
3385 return viable ? viable : cands;
3388 static bool
3389 any_strictly_viable (struct z_candidate *cands)
3391 for (; cands; cands = cands->next)
3392 if (cands->viable == 1)
3393 return true;
3394 return false;
3397 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3398 words, it is about to become the "this" pointer for a member
3399 function call. Take the address of the object. */
3401 static tree
3402 build_this (tree obj)
3404 /* In a template, we are only concerned about the type of the
3405 expression, so we can take a shortcut. */
3406 if (processing_template_decl)
3407 return build_address (obj);
3409 return cp_build_addr_expr (obj, tf_warning_or_error);
3412 /* Returns true iff functions are equivalent. Equivalent functions are
3413 not '==' only if one is a function-local extern function or if
3414 both are extern "C". */
3416 static inline int
3417 equal_functions (tree fn1, tree fn2)
3419 if (TREE_CODE (fn1) != TREE_CODE (fn2))
3420 return 0;
3421 if (TREE_CODE (fn1) == TEMPLATE_DECL)
3422 return fn1 == fn2;
3423 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
3424 || DECL_EXTERN_C_FUNCTION_P (fn1))
3425 return decls_match (fn1, fn2);
3426 return fn1 == fn2;
3429 /* Print information about a candidate being rejected due to INFO. */
3431 static void
3432 print_conversion_rejection (location_t loc, struct conversion_info *info)
3434 tree from = info->from;
3435 if (!TYPE_P (from))
3436 from = lvalue_type (from);
3437 if (info->n_arg == -1)
3439 /* Conversion of implicit `this' argument failed. */
3440 if (!TYPE_P (info->from))
3441 /* A bad conversion for 'this' must be discarding cv-quals. */
3442 inform (loc, " passing %qT as %<this%> "
3443 "argument discards qualifiers",
3444 from);
3445 else
3446 inform (loc, " no known conversion for implicit "
3447 "%<this%> parameter from %qH to %qI",
3448 from, info->to_type);
3450 else if (!TYPE_P (info->from))
3452 if (info->n_arg >= 0)
3453 inform (loc, " conversion of argument %d would be ill-formed:",
3454 info->n_arg + 1);
3455 perform_implicit_conversion (info->to_type, info->from,
3456 tf_warning_or_error);
3458 else if (info->n_arg == -2)
3459 /* Conversion of conversion function return value failed. */
3460 inform (loc, " no known conversion from %qH to %qI",
3461 from, info->to_type);
3462 else
3463 inform (loc, " no known conversion for argument %d from %qH to %qI",
3464 info->n_arg + 1, from, info->to_type);
3467 /* Print information about a candidate with WANT parameters and we found
3468 HAVE. */
3470 static void
3471 print_arity_information (location_t loc, unsigned int have, unsigned int want)
3473 inform_n (loc, want,
3474 " candidate expects %d argument, %d provided",
3475 " candidate expects %d arguments, %d provided",
3476 want, have);
3479 /* Print information about one overload candidate CANDIDATE. MSGSTR
3480 is the text to print before the candidate itself.
3482 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3483 to have been run through gettext by the caller. This wart makes
3484 life simpler in print_z_candidates and for the translators. */
3486 static void
3487 print_z_candidate (location_t loc, const char *msgstr,
3488 struct z_candidate *candidate)
3490 const char *msg = (msgstr == NULL
3491 ? ""
3492 : ACONCAT ((msgstr, " ", NULL)));
3493 tree fn = candidate->fn;
3494 if (flag_new_inheriting_ctors)
3495 fn = strip_inheriting_ctors (fn);
3496 location_t cloc = location_of (fn);
3498 if (identifier_p (fn))
3500 cloc = loc;
3501 if (candidate->num_convs == 3)
3502 inform (cloc, "%s%<%D(%T, %T, %T)%> <built-in>", msg, fn,
3503 candidate->convs[0]->type,
3504 candidate->convs[1]->type,
3505 candidate->convs[2]->type);
3506 else if (candidate->num_convs == 2)
3507 inform (cloc, "%s%<%D(%T, %T)%> <built-in>", msg, fn,
3508 candidate->convs[0]->type,
3509 candidate->convs[1]->type);
3510 else
3511 inform (cloc, "%s%<%D(%T)%> <built-in>", msg, fn,
3512 candidate->convs[0]->type);
3514 else if (TYPE_P (fn))
3515 inform (cloc, "%s%qT <conversion>", msg, fn);
3516 else if (candidate->viable == -1)
3517 inform (cloc, "%s%#qD <near match>", msg, fn);
3518 else if (DECL_DELETED_FN (fn))
3519 inform (cloc, "%s%#qD <deleted>", msg, fn);
3520 else
3521 inform (cloc, "%s%#qD", msg, fn);
3522 if (fn != candidate->fn)
3524 cloc = location_of (candidate->fn);
3525 inform (cloc, " inherited here");
3527 /* Give the user some information about why this candidate failed. */
3528 if (candidate->reason != NULL)
3530 struct rejection_reason *r = candidate->reason;
3532 switch (r->code)
3534 case rr_arity:
3535 print_arity_information (cloc, r->u.arity.actual,
3536 r->u.arity.expected);
3537 break;
3538 case rr_arg_conversion:
3539 print_conversion_rejection (cloc, &r->u.conversion);
3540 break;
3541 case rr_bad_arg_conversion:
3542 print_conversion_rejection (cloc, &r->u.bad_conversion);
3543 break;
3544 case rr_explicit_conversion:
3545 inform (cloc, " return type %qT of explicit conversion function "
3546 "cannot be converted to %qT with a qualification "
3547 "conversion", r->u.conversion.from,
3548 r->u.conversion.to_type);
3549 break;
3550 case rr_template_conversion:
3551 inform (cloc, " conversion from return type %qT of template "
3552 "conversion function specialization to %qT is not an "
3553 "exact match", r->u.conversion.from,
3554 r->u.conversion.to_type);
3555 break;
3556 case rr_template_unification:
3557 /* We use template_unification_error_rejection if unification caused
3558 actual non-SFINAE errors, in which case we don't need to repeat
3559 them here. */
3560 if (r->u.template_unification.tmpl == NULL_TREE)
3562 inform (cloc, " substitution of deduced template arguments "
3563 "resulted in errors seen above");
3564 break;
3566 /* Re-run template unification with diagnostics. */
3567 inform (cloc, " template argument deduction/substitution failed:");
3568 fn_type_unification (r->u.template_unification.tmpl,
3569 r->u.template_unification.explicit_targs,
3570 (make_tree_vec
3571 (r->u.template_unification.num_targs)),
3572 r->u.template_unification.args,
3573 r->u.template_unification.nargs,
3574 r->u.template_unification.return_type,
3575 r->u.template_unification.strict,
3576 r->u.template_unification.flags,
3577 NULL, true, false);
3578 break;
3579 case rr_invalid_copy:
3580 inform (cloc,
3581 " a constructor taking a single argument of its own "
3582 "class type is invalid");
3583 break;
3584 case rr_constraint_failure:
3586 tree tmpl = r->u.template_instantiation.tmpl;
3587 tree args = r->u.template_instantiation.targs;
3588 diagnose_constraints (cloc, tmpl, args);
3590 break;
3591 case rr_inherited_ctor:
3592 inform (cloc, " an inherited constructor is not a candidate for "
3593 "initialization from an expression of the same or derived "
3594 "type");
3595 break;
3596 case rr_none:
3597 default:
3598 /* This candidate didn't have any issues or we failed to
3599 handle a particular code. Either way... */
3600 gcc_unreachable ();
3605 static void
3606 print_z_candidates (location_t loc, struct z_candidate *candidates)
3608 struct z_candidate *cand1;
3609 struct z_candidate **cand2;
3611 if (!candidates)
3612 return;
3614 /* Remove non-viable deleted candidates. */
3615 cand1 = candidates;
3616 for (cand2 = &cand1; *cand2; )
3618 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
3619 && !(*cand2)->viable
3620 && DECL_DELETED_FN ((*cand2)->fn))
3621 *cand2 = (*cand2)->next;
3622 else
3623 cand2 = &(*cand2)->next;
3625 /* ...if there are any non-deleted ones. */
3626 if (cand1)
3627 candidates = cand1;
3629 /* There may be duplicates in the set of candidates. We put off
3630 checking this condition as long as possible, since we have no way
3631 to eliminate duplicates from a set of functions in less than n^2
3632 time. Now we are about to emit an error message, so it is more
3633 permissible to go slowly. */
3634 for (cand1 = candidates; cand1; cand1 = cand1->next)
3636 tree fn = cand1->fn;
3637 /* Skip builtin candidates and conversion functions. */
3638 if (!DECL_P (fn))
3639 continue;
3640 cand2 = &cand1->next;
3641 while (*cand2)
3643 if (DECL_P ((*cand2)->fn)
3644 && equal_functions (fn, (*cand2)->fn))
3645 *cand2 = (*cand2)->next;
3646 else
3647 cand2 = &(*cand2)->next;
3651 for (; candidates; candidates = candidates->next)
3652 print_z_candidate (loc, "candidate:", candidates);
3655 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3656 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3657 the result of the conversion function to convert it to the final
3658 desired type. Merge the two sequences into a single sequence,
3659 and return the merged sequence. */
3661 static conversion *
3662 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
3664 conversion **t;
3665 bool bad = user_seq->bad_p;
3667 gcc_assert (user_seq->kind == ck_user);
3669 /* Find the end of the second conversion sequence. */
3670 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
3672 /* The entire sequence is a user-conversion sequence. */
3673 (*t)->user_conv_p = true;
3674 if (bad)
3675 (*t)->bad_p = true;
3678 if ((*t)->rvaluedness_matches_p)
3679 /* We're binding a reference directly to the result of the conversion.
3680 build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
3681 type, but we want it back. */
3682 user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn));
3684 /* Replace the identity conversion with the user conversion
3685 sequence. */
3686 *t = user_seq;
3688 return std_seq;
3691 /* Handle overload resolution for initializing an object of class type from
3692 an initializer list. First we look for a suitable constructor that
3693 takes a std::initializer_list; if we don't find one, we then look for a
3694 non-list constructor.
3696 Parameters are as for add_candidates, except that the arguments are in
3697 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
3698 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3700 static void
3701 add_list_candidates (tree fns, tree first_arg,
3702 const vec<tree, va_gc> *args, tree totype,
3703 tree explicit_targs, bool template_only,
3704 tree conversion_path, tree access_path,
3705 int flags,
3706 struct z_candidate **candidates,
3707 tsubst_flags_t complain)
3709 gcc_assert (*candidates == NULL);
3711 /* We're looking for a ctor for list-initialization. */
3712 flags |= LOOKUP_LIST_INIT_CTOR;
3713 /* And we don't allow narrowing conversions. We also use this flag to
3714 avoid the copy constructor call for copy-list-initialization. */
3715 flags |= LOOKUP_NO_NARROWING;
3717 unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
3718 tree init_list = (*args)[nart];
3720 /* Always use the default constructor if the list is empty (DR 990). */
3721 if (CONSTRUCTOR_NELTS (init_list) == 0
3722 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
3724 /* If the class has a list ctor, try passing the list as a single
3725 argument first, but only consider list ctors. */
3726 else if (TYPE_HAS_LIST_CTOR (totype))
3728 flags |= LOOKUP_LIST_ONLY;
3729 add_candidates (fns, first_arg, args, NULL_TREE,
3730 explicit_targs, template_only, conversion_path,
3731 access_path, flags, candidates, complain);
3732 if (any_strictly_viable (*candidates))
3733 return;
3736 /* Expand the CONSTRUCTOR into a new argument vec. */
3737 vec<tree, va_gc> *new_args;
3738 vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
3739 for (unsigned i = 0; i < nart; ++i)
3740 new_args->quick_push ((*args)[i]);
3741 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (init_list); ++i)
3742 new_args->quick_push (CONSTRUCTOR_ELT (init_list, i)->value);
3744 /* We aren't looking for list-ctors anymore. */
3745 flags &= ~LOOKUP_LIST_ONLY;
3746 /* We allow more user-defined conversions within an init-list. */
3747 flags &= ~LOOKUP_NO_CONVERSION;
3749 add_candidates (fns, first_arg, new_args, NULL_TREE,
3750 explicit_targs, template_only, conversion_path,
3751 access_path, flags, candidates, complain);
3754 /* Returns the best overload candidate to perform the requested
3755 conversion. This function is used for three the overloading situations
3756 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3757 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3758 per [dcl.init.ref], so we ignore temporary bindings. */
3760 static struct z_candidate *
3761 build_user_type_conversion_1 (tree totype, tree expr, int flags,
3762 tsubst_flags_t complain)
3764 struct z_candidate *candidates, *cand;
3765 tree fromtype;
3766 tree ctors = NULL_TREE;
3767 tree conv_fns = NULL_TREE;
3768 conversion *conv = NULL;
3769 tree first_arg = NULL_TREE;
3770 vec<tree, va_gc> *args = NULL;
3771 bool any_viable_p;
3772 int convflags;
3774 if (!expr)
3775 return NULL;
3777 fromtype = TREE_TYPE (expr);
3779 /* We represent conversion within a hierarchy using RVALUE_CONV and
3780 BASE_CONV, as specified by [over.best.ics]; these become plain
3781 constructor calls, as specified in [dcl.init]. */
3782 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
3783 || !DERIVED_FROM_P (totype, fromtype));
3785 if (CLASS_TYPE_P (totype))
3786 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
3787 creating a garbage BASELINK; constructors can't be inherited. */
3788 ctors = get_class_binding (totype, complete_ctor_identifier);
3790 if (MAYBE_CLASS_TYPE_P (fromtype))
3792 tree to_nonref = non_reference (totype);
3793 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
3794 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
3795 && DERIVED_FROM_P (to_nonref, fromtype)))
3797 /* [class.conv.fct] A conversion function is never used to
3798 convert a (possibly cv-qualified) object to the (possibly
3799 cv-qualified) same object type (or a reference to it), to a
3800 (possibly cv-qualified) base class of that type (or a
3801 reference to it)... */
3803 else
3804 conv_fns = lookup_conversions (fromtype);
3807 candidates = 0;
3808 flags |= LOOKUP_NO_CONVERSION;
3809 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3810 flags |= LOOKUP_NO_NARROWING;
3812 /* It's OK to bind a temporary for converting constructor arguments, but
3813 not in converting the return value of a conversion operator. */
3814 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
3815 | (flags & LOOKUP_NO_NARROWING));
3816 flags &= ~LOOKUP_NO_TEMP_BIND;
3818 if (ctors)
3820 int ctorflags = flags;
3822 first_arg = build_dummy_object (totype);
3824 /* We should never try to call the abstract or base constructor
3825 from here. */
3826 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
3827 && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
3829 args = make_tree_vector_single (expr);
3830 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3832 /* List-initialization. */
3833 add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
3834 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
3835 ctorflags, &candidates, complain);
3837 else
3839 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
3840 TYPE_BINFO (totype), TYPE_BINFO (totype),
3841 ctorflags, &candidates, complain);
3844 for (cand = candidates; cand; cand = cand->next)
3846 cand->second_conv = build_identity_conv (totype, NULL_TREE);
3848 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
3849 set, then this is copy-initialization. In that case, "The
3850 result of the call is then used to direct-initialize the
3851 object that is the destination of the copy-initialization."
3852 [dcl.init]
3854 We represent this in the conversion sequence with an
3855 rvalue conversion, which means a constructor call. */
3856 if (!TYPE_REF_P (totype)
3857 && !(convflags & LOOKUP_NO_TEMP_BIND))
3858 cand->second_conv
3859 = build_conv (ck_rvalue, totype, cand->second_conv);
3863 if (conv_fns)
3865 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3866 first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
3867 else
3868 first_arg = expr;
3871 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
3873 tree conversion_path = TREE_PURPOSE (conv_fns);
3874 struct z_candidate *old_candidates;
3876 /* If we are called to convert to a reference type, we are trying to
3877 find a direct binding, so don't even consider temporaries. If
3878 we don't find a direct binding, the caller will try again to
3879 look for a temporary binding. */
3880 if (TYPE_REF_P (totype))
3881 convflags |= LOOKUP_NO_TEMP_BIND;
3883 old_candidates = candidates;
3884 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3885 NULL_TREE, false,
3886 conversion_path, TYPE_BINFO (fromtype),
3887 flags, &candidates, complain);
3889 for (cand = candidates; cand != old_candidates; cand = cand->next)
3891 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
3892 conversion *ics
3893 = implicit_conversion (totype,
3894 rettype,
3896 /*c_cast_p=*/false, convflags,
3897 complain);
3899 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3900 copy-initialization. In that case, "The result of the
3901 call is then used to direct-initialize the object that is
3902 the destination of the copy-initialization." [dcl.init]
3904 We represent this in the conversion sequence with an
3905 rvalue conversion, which means a constructor call. But
3906 don't add a second rvalue conversion if there's already
3907 one there. Which there really shouldn't be, but it's
3908 harmless since we'd add it here anyway. */
3909 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3910 && !(convflags & LOOKUP_NO_TEMP_BIND))
3911 ics = build_conv (ck_rvalue, totype, ics);
3913 cand->second_conv = ics;
3915 if (!ics)
3917 cand->viable = 0;
3918 cand->reason = arg_conversion_rejection (NULL_TREE, -2,
3919 rettype, totype);
3921 else if (DECL_NONCONVERTING_P (cand->fn)
3922 && ics->rank > cr_exact)
3924 /* 13.3.1.5: For direct-initialization, those explicit
3925 conversion functions that are not hidden within S and
3926 yield type T or a type that can be converted to type T
3927 with a qualification conversion (4.4) are also candidate
3928 functions. */
3929 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
3930 I've raised this issue with the committee. --jason 9/2011 */
3931 cand->viable = -1;
3932 cand->reason = explicit_conversion_rejection (rettype, totype);
3934 else if (cand->viable == 1 && ics->bad_p)
3936 cand->viable = -1;
3937 cand->reason
3938 = bad_arg_conversion_rejection (NULL_TREE, -2,
3939 rettype, totype);
3941 else if (primary_template_specialization_p (cand->fn)
3942 && ics->rank > cr_exact)
3944 /* 13.3.3.1.2: If the user-defined conversion is specified by
3945 a specialization of a conversion function template, the
3946 second standard conversion sequence shall have exact match
3947 rank. */
3948 cand->viable = -1;
3949 cand->reason = template_conversion_rejection (rettype, totype);
3954 candidates = splice_viable (candidates, false, &any_viable_p);
3955 if (!any_viable_p)
3957 if (args)
3958 release_tree_vector (args);
3959 return NULL;
3962 cand = tourney (candidates, complain);
3963 if (cand == 0)
3965 if (complain & tf_error)
3967 error ("conversion from %qH to %qI is ambiguous",
3968 fromtype, totype);
3969 print_z_candidates (location_of (expr), candidates);
3972 cand = candidates; /* any one will do */
3973 cand->second_conv = build_ambiguous_conv (totype, expr);
3974 cand->second_conv->user_conv_p = true;
3975 if (!any_strictly_viable (candidates))
3976 cand->second_conv->bad_p = true;
3977 if (flags & LOOKUP_ONLYCONVERTING)
3978 cand->second_conv->need_temporary_p = true;
3979 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3980 ambiguous conversion is no worse than another user-defined
3981 conversion. */
3983 return cand;
3986 tree convtype;
3987 if (!DECL_CONSTRUCTOR_P (cand->fn))
3988 convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
3989 else if (cand->second_conv->kind == ck_rvalue)
3990 /* DR 5: [in the first step of copy-initialization]...if the function
3991 is a constructor, the call initializes a temporary of the
3992 cv-unqualified version of the destination type. */
3993 convtype = cv_unqualified (totype);
3994 else
3995 convtype = totype;
3996 /* Build the user conversion sequence. */
3997 conv = build_conv
3998 (ck_user,
3999 convtype,
4000 build_identity_conv (TREE_TYPE (expr), expr));
4001 conv->cand = cand;
4002 if (cand->viable == -1)
4003 conv->bad_p = true;
4005 /* Remember that this was a list-initialization. */
4006 if (flags & LOOKUP_NO_NARROWING)
4007 conv->check_narrowing = true;
4009 /* Combine it with the second conversion sequence. */
4010 cand->second_conv = merge_conversion_sequences (conv,
4011 cand->second_conv);
4013 return cand;
4016 /* Wrapper for above. */
4018 tree
4019 build_user_type_conversion (tree totype, tree expr, int flags,
4020 tsubst_flags_t complain)
4022 struct z_candidate *cand;
4023 tree ret;
4025 bool subtime = timevar_cond_start (TV_OVERLOAD);
4026 cand = build_user_type_conversion_1 (totype, expr, flags, complain);
4028 if (cand)
4030 if (cand->second_conv->kind == ck_ambig)
4031 ret = error_mark_node;
4032 else
4034 expr = convert_like (cand->second_conv, expr, complain);
4035 ret = convert_from_reference (expr);
4038 else
4039 ret = NULL_TREE;
4041 timevar_cond_stop (TV_OVERLOAD, subtime);
4042 return ret;
4045 /* Subroutine of convert_nontype_argument.
4047 EXPR is an expression used in a context that requires a converted
4048 constant-expression, such as a template non-type parameter. Do any
4049 necessary conversions (that are permitted for converted
4050 constant-expressions) to convert it to the desired type.
4052 If conversion is successful, returns the converted expression;
4053 otherwise, returns error_mark_node. */
4055 tree
4056 build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
4058 conversion *conv;
4059 void *p;
4060 tree t;
4061 location_t loc = cp_expr_loc_or_loc (expr, input_location);
4063 if (error_operand_p (expr))
4064 return error_mark_node;
4066 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4067 p = conversion_obstack_alloc (0);
4069 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4070 /*c_cast_p=*/false,
4071 LOOKUP_IMPLICIT, complain);
4073 /* A converted constant expression of type T is an expression, implicitly
4074 converted to type T, where the converted expression is a constant
4075 expression and the implicit conversion sequence contains only
4077 * user-defined conversions,
4078 * lvalue-to-rvalue conversions (7.1),
4079 * array-to-pointer conversions (7.2),
4080 * function-to-pointer conversions (7.3),
4081 * qualification conversions (7.5),
4082 * integral promotions (7.6),
4083 * integral conversions (7.8) other than narrowing conversions (11.6.4),
4084 * null pointer conversions (7.11) from std::nullptr_t,
4085 * null member pointer conversions (7.12) from std::nullptr_t, and
4086 * function pointer conversions (7.13),
4088 and where the reference binding (if any) binds directly. */
4090 for (conversion *c = conv;
4091 conv && c->kind != ck_identity;
4092 c = next_conversion (c))
4094 switch (c->kind)
4096 /* A conversion function is OK. If it isn't constexpr, we'll
4097 complain later that the argument isn't constant. */
4098 case ck_user:
4099 /* The lvalue-to-rvalue conversion is OK. */
4100 case ck_rvalue:
4101 /* Array-to-pointer and function-to-pointer. */
4102 case ck_lvalue:
4103 /* Function pointer conversions. */
4104 case ck_fnptr:
4105 /* Qualification conversions. */
4106 case ck_qual:
4107 break;
4109 case ck_ref_bind:
4110 if (c->need_temporary_p)
4112 if (complain & tf_error)
4113 error_at (loc, "initializing %qH with %qI in converted "
4114 "constant expression does not bind directly",
4115 type, next_conversion (c)->type);
4116 conv = NULL;
4118 break;
4120 case ck_base:
4121 case ck_pmem:
4122 case ck_ptr:
4123 case ck_std:
4124 t = next_conversion (c)->type;
4125 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
4126 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4127 /* Integral promotion or conversion. */
4128 break;
4129 if (NULLPTR_TYPE_P (t))
4130 /* Conversion from nullptr to pointer or pointer-to-member. */
4131 break;
4133 if (complain & tf_error)
4134 error_at (loc, "conversion from %qH to %qI in a "
4135 "converted constant expression", t, type);
4136 /* fall through. */
4138 default:
4139 conv = NULL;
4140 break;
4144 /* Avoid confusing convert_nontype_argument by introducing
4145 a redundant conversion to the same reference type. */
4146 if (conv && conv->kind == ck_ref_bind
4147 && REFERENCE_REF_P (expr))
4149 tree ref = TREE_OPERAND (expr, 0);
4150 if (same_type_p (type, TREE_TYPE (ref)))
4151 return ref;
4154 if (conv)
4155 expr = convert_like (conv, expr, complain);
4156 else
4157 expr = error_mark_node;
4159 /* Free all the conversions we allocated. */
4160 obstack_free (&conversion_obstack, p);
4162 return expr;
4165 /* Do any initial processing on the arguments to a function call. */
4167 static vec<tree, va_gc> *
4168 resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
4170 unsigned int ix;
4171 tree arg;
4173 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
4175 if (error_operand_p (arg))
4176 return NULL;
4177 else if (VOID_TYPE_P (TREE_TYPE (arg)))
4179 if (complain & tf_error)
4180 error ("invalid use of void expression");
4181 return NULL;
4183 else if (invalid_nonstatic_memfn_p (arg->exp.locus, arg, complain))
4184 return NULL;
4186 return args;
4189 /* Perform overload resolution on FN, which is called with the ARGS.
4191 Return the candidate function selected by overload resolution, or
4192 NULL if the event that overload resolution failed. In the case
4193 that overload resolution fails, *CANDIDATES will be the set of
4194 candidates considered, and ANY_VIABLE_P will be set to true or
4195 false to indicate whether or not any of the candidates were
4196 viable.
4198 The ARGS should already have gone through RESOLVE_ARGS before this
4199 function is called. */
4201 static struct z_candidate *
4202 perform_overload_resolution (tree fn,
4203 const vec<tree, va_gc> *args,
4204 struct z_candidate **candidates,
4205 bool *any_viable_p, tsubst_flags_t complain)
4207 struct z_candidate *cand;
4208 tree explicit_targs;
4209 int template_only;
4211 bool subtime = timevar_cond_start (TV_OVERLOAD);
4213 explicit_targs = NULL_TREE;
4214 template_only = 0;
4216 *candidates = NULL;
4217 *any_viable_p = true;
4219 /* Check FN. */
4220 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
4221 || TREE_CODE (fn) == TEMPLATE_DECL
4222 || TREE_CODE (fn) == OVERLOAD
4223 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
4225 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4227 explicit_targs = TREE_OPERAND (fn, 1);
4228 fn = TREE_OPERAND (fn, 0);
4229 template_only = 1;
4232 /* Add the various candidate functions. */
4233 add_candidates (fn, NULL_TREE, args, NULL_TREE,
4234 explicit_targs, template_only,
4235 /*conversion_path=*/NULL_TREE,
4236 /*access_path=*/NULL_TREE,
4237 LOOKUP_NORMAL,
4238 candidates, complain);
4240 *candidates = splice_viable (*candidates, false, any_viable_p);
4241 if (*any_viable_p)
4242 cand = tourney (*candidates, complain);
4243 else
4244 cand = NULL;
4246 timevar_cond_stop (TV_OVERLOAD, subtime);
4247 return cand;
4250 /* Print an error message about being unable to build a call to FN with
4251 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
4252 be located; CANDIDATES is a possibly empty list of such
4253 functions. */
4255 static void
4256 print_error_for_call_failure (tree fn, vec<tree, va_gc> *args,
4257 struct z_candidate *candidates)
4259 tree targs = NULL_TREE;
4260 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4262 targs = TREE_OPERAND (fn, 1);
4263 fn = TREE_OPERAND (fn, 0);
4265 tree name = OVL_NAME (fn);
4266 location_t loc = location_of (name);
4267 if (targs)
4268 name = lookup_template_function (name, targs);
4270 if (!any_strictly_viable (candidates))
4271 error_at (loc, "no matching function for call to %<%D(%A)%>",
4272 name, build_tree_list_vec (args));
4273 else
4274 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
4275 name, build_tree_list_vec (args));
4276 if (candidates)
4277 print_z_candidates (loc, candidates);
4280 /* Return an expression for a call to FN (a namespace-scope function,
4281 or a static member function) with the ARGS. This may change
4282 ARGS. */
4284 tree
4285 build_new_function_call (tree fn, vec<tree, va_gc> **args,
4286 tsubst_flags_t complain)
4288 struct z_candidate *candidates, *cand;
4289 bool any_viable_p;
4290 void *p;
4291 tree result;
4293 if (args != NULL && *args != NULL)
4295 *args = resolve_args (*args, complain);
4296 if (*args == NULL)
4297 return error_mark_node;
4300 if (flag_tm)
4301 tm_malloc_replacement (fn);
4303 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4304 p = conversion_obstack_alloc (0);
4306 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
4307 complain);
4309 if (!cand)
4311 if (complain & tf_error)
4313 // If there is a single (non-viable) function candidate,
4314 // let the error be diagnosed by cp_build_function_call_vec.
4315 if (!any_viable_p && candidates && ! candidates->next
4316 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
4317 return cp_build_function_call_vec (candidates->fn, args, complain);
4319 // Otherwise, emit notes for non-viable candidates.
4320 print_error_for_call_failure (fn, *args, candidates);
4322 result = error_mark_node;
4324 else
4326 int flags = LOOKUP_NORMAL;
4327 /* If fn is template_id_expr, the call has explicit template arguments
4328 (e.g. func<int>(5)), communicate this info to build_over_call
4329 through flags so that later we can use it to decide whether to warn
4330 about peculiar null pointer conversion. */
4331 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4333 /* If overload resolution selects a specialization of a
4334 function concept for non-dependent template arguments,
4335 the expression is true if the constraints are satisfied
4336 and false otherwise.
4338 NOTE: This is an extension of Concepts Lite TS that
4339 allows constraints to be used in expressions. */
4340 if (flag_concepts && !processing_template_decl)
4342 tree tmpl = DECL_TI_TEMPLATE (cand->fn);
4343 tree targs = DECL_TI_ARGS (cand->fn);
4344 tree decl = DECL_TEMPLATE_RESULT (tmpl);
4345 if (DECL_DECLARED_CONCEPT_P (decl))
4346 return evaluate_function_concept (decl, targs);
4349 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
4352 result = build_over_call (cand, flags, complain);
4355 /* Free all the conversions we allocated. */
4356 obstack_free (&conversion_obstack, p);
4358 return result;
4361 /* Build a call to a global operator new. FNNAME is the name of the
4362 operator (either "operator new" or "operator new[]") and ARGS are
4363 the arguments provided. This may change ARGS. *SIZE points to the
4364 total number of bytes required by the allocation, and is updated if
4365 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
4366 be used. If this function determines that no cookie should be
4367 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
4368 is not NULL_TREE, it is evaluated before calculating the final
4369 array size, and if it fails, the array size is replaced with
4370 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
4371 is non-NULL, it will be set, upon return, to the allocation
4372 function called. */
4374 tree
4375 build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
4376 tree *size, tree *cookie_size,
4377 tree align_arg, tree size_check,
4378 tree *fn, tsubst_flags_t complain)
4380 tree original_size = *size;
4381 tree fns;
4382 struct z_candidate *candidates;
4383 struct z_candidate *cand = NULL;
4384 bool any_viable_p;
4386 if (fn)
4387 *fn = NULL_TREE;
4388 /* Set to (size_t)-1 if the size check fails. */
4389 if (size_check != NULL_TREE)
4391 tree errval = TYPE_MAX_VALUE (sizetype);
4392 if (cxx_dialect >= cxx11 && flag_exceptions)
4393 errval = throw_bad_array_new_length ();
4394 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4395 original_size, errval);
4397 vec_safe_insert (*args, 0, *size);
4398 *args = resolve_args (*args, complain);
4399 if (*args == NULL)
4400 return error_mark_node;
4402 /* Based on:
4404 [expr.new]
4406 If this lookup fails to find the name, or if the allocated type
4407 is not a class type, the allocation function's name is looked
4408 up in the global scope.
4410 we disregard block-scope declarations of "operator new". */
4411 fns = lookup_name_real (fnname, 0, 1, /*block_p=*/false, 0, 0);
4412 fns = lookup_arg_dependent (fnname, fns, *args);
4414 if (align_arg)
4416 vec<tree, va_gc>* align_args
4417 = vec_copy_and_insert (*args, align_arg, 1);
4418 cand = perform_overload_resolution (fns, align_args, &candidates,
4419 &any_viable_p, tf_none);
4420 if (cand)
4421 *args = align_args;
4422 /* If no aligned allocation function matches, try again without the
4423 alignment. */
4426 /* Figure out what function is being called. */
4427 if (!cand)
4428 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
4429 complain);
4431 /* If no suitable function could be found, issue an error message
4432 and give up. */
4433 if (!cand)
4435 if (complain & tf_error)
4436 print_error_for_call_failure (fns, *args, candidates);
4437 return error_mark_node;
4440 /* If a cookie is required, add some extra space. Whether
4441 or not a cookie is required cannot be determined until
4442 after we know which function was called. */
4443 if (*cookie_size)
4445 bool use_cookie = true;
4446 tree arg_types;
4448 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
4449 /* Skip the size_t parameter. */
4450 arg_types = TREE_CHAIN (arg_types);
4451 /* Check the remaining parameters (if any). */
4452 if (arg_types
4453 && TREE_CHAIN (arg_types) == void_list_node
4454 && same_type_p (TREE_VALUE (arg_types),
4455 ptr_type_node))
4456 use_cookie = false;
4457 /* If we need a cookie, adjust the number of bytes allocated. */
4458 if (use_cookie)
4460 /* Update the total size. */
4461 *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
4462 if (size_check)
4464 /* Set to (size_t)-1 if the size check fails. */
4465 gcc_assert (size_check != NULL_TREE);
4466 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4467 *size, TYPE_MAX_VALUE (sizetype));
4469 /* Update the argument list to reflect the adjusted size. */
4470 (**args)[0] = *size;
4472 else
4473 *cookie_size = NULL_TREE;
4476 /* Tell our caller which function we decided to call. */
4477 if (fn)
4478 *fn = cand->fn;
4480 /* Build the CALL_EXPR. */
4481 return build_over_call (cand, LOOKUP_NORMAL, complain);
4484 /* Build a new call to operator(). This may change ARGS. */
4486 static tree
4487 build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4489 struct z_candidate *candidates = 0, *cand;
4490 tree fns, convs, first_mem_arg = NULL_TREE;
4491 bool any_viable_p;
4492 tree result = NULL_TREE;
4493 void *p;
4495 obj = mark_lvalue_use (obj);
4497 if (error_operand_p (obj))
4498 return error_mark_node;
4500 tree type = TREE_TYPE (obj);
4502 obj = prep_operand (obj);
4504 if (TYPE_PTRMEMFUNC_P (type))
4506 if (complain & tf_error)
4507 /* It's no good looking for an overloaded operator() on a
4508 pointer-to-member-function. */
4509 error ("pointer-to-member function %qE cannot be called without "
4510 "an object; consider using %<.*%> or %<->*%>", obj);
4511 return error_mark_node;
4514 if (TYPE_BINFO (type))
4516 fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1);
4517 if (fns == error_mark_node)
4518 return error_mark_node;
4520 else
4521 fns = NULL_TREE;
4523 if (args != NULL && *args != NULL)
4525 *args = resolve_args (*args, complain);
4526 if (*args == NULL)
4527 return error_mark_node;
4530 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4531 p = conversion_obstack_alloc (0);
4533 if (fns)
4535 first_mem_arg = obj;
4537 add_candidates (BASELINK_FUNCTIONS (fns),
4538 first_mem_arg, *args, NULL_TREE,
4539 NULL_TREE, false,
4540 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
4541 LOOKUP_NORMAL, &candidates, complain);
4544 convs = lookup_conversions (type);
4546 for (; convs; convs = TREE_CHAIN (convs))
4548 tree totype = TREE_TYPE (convs);
4550 if (TYPE_PTRFN_P (totype)
4551 || TYPE_REFFN_P (totype)
4552 || (TYPE_REF_P (totype)
4553 && TYPE_PTRFN_P (TREE_TYPE (totype))))
4554 for (ovl_iterator iter (TREE_VALUE (convs)); iter; ++iter)
4556 tree fn = *iter;
4558 if (DECL_NONCONVERTING_P (fn))
4559 continue;
4561 if (TREE_CODE (fn) == TEMPLATE_DECL)
4562 add_template_conv_candidate
4563 (&candidates, fn, obj, *args, totype,
4564 /*access_path=*/NULL_TREE,
4565 /*conversion_path=*/NULL_TREE, complain);
4566 else
4567 add_conv_candidate (&candidates, fn, obj,
4568 *args, /*conversion_path=*/NULL_TREE,
4569 /*access_path=*/NULL_TREE, complain);
4573 /* Be strict here because if we choose a bad conversion candidate, the
4574 errors we get won't mention the call context. */
4575 candidates = splice_viable (candidates, true, &any_viable_p);
4576 if (!any_viable_p)
4578 if (complain & tf_error)
4580 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
4581 build_tree_list_vec (*args));
4582 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4584 result = error_mark_node;
4586 else
4588 cand = tourney (candidates, complain);
4589 if (cand == 0)
4591 if (complain & tf_error)
4593 error ("call of %<(%T) (%A)%> is ambiguous",
4594 TREE_TYPE (obj), build_tree_list_vec (*args));
4595 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4597 result = error_mark_node;
4599 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
4600 && DECL_OVERLOADED_OPERATOR_P (cand->fn)
4601 && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
4602 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4603 else
4605 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
4606 obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
4607 -1, complain);
4608 else
4610 gcc_checking_assert (TYPE_P (cand->fn));
4611 obj = convert_like (cand->convs[0], obj, complain);
4613 obj = convert_from_reference (obj);
4614 result = cp_build_function_call_vec (obj, args, complain);
4618 /* Free all the conversions we allocated. */
4619 obstack_free (&conversion_obstack, p);
4621 return result;
4624 /* Wrapper for above. */
4626 tree
4627 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4629 tree ret;
4630 bool subtime = timevar_cond_start (TV_OVERLOAD);
4631 ret = build_op_call_1 (obj, args, complain);
4632 timevar_cond_stop (TV_OVERLOAD, subtime);
4633 return ret;
4636 /* Called by op_error to prepare format strings suitable for the error
4637 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4638 and a suffix (controlled by NTYPES). */
4640 static const char *
4641 op_error_string (const char *errmsg, int ntypes, bool match)
4643 const char *msg;
4645 const char *msgp = concat (match ? G_("ambiguous overload for ")
4646 : G_("no match for "), errmsg, NULL);
4648 if (ntypes == 3)
4649 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
4650 else if (ntypes == 2)
4651 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
4652 else
4653 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
4655 return msg;
4658 static void
4659 op_error (location_t loc, enum tree_code code, enum tree_code code2,
4660 tree arg1, tree arg2, tree arg3, bool match)
4662 bool assop = code == MODIFY_EXPR;
4663 const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
4665 switch (code)
4667 case COND_EXPR:
4668 if (flag_diagnostics_show_caret)
4669 error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
4670 3, match),
4671 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4672 else
4673 error_at (loc, op_error_string (G_("ternary %<operator?:%> "
4674 "in %<%E ? %E : %E%>"), 3, match),
4675 arg1, arg2, arg3,
4676 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4677 break;
4679 case POSTINCREMENT_EXPR:
4680 case POSTDECREMENT_EXPR:
4681 if (flag_diagnostics_show_caret)
4682 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4683 opname, TREE_TYPE (arg1));
4684 else
4685 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4686 1, match),
4687 opname, arg1, opname, TREE_TYPE (arg1));
4688 break;
4690 case ARRAY_REF:
4691 if (flag_diagnostics_show_caret)
4692 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
4693 TREE_TYPE (arg1), TREE_TYPE (arg2));
4694 else
4695 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
4696 2, match),
4697 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
4698 break;
4700 case REALPART_EXPR:
4701 case IMAGPART_EXPR:
4702 if (flag_diagnostics_show_caret)
4703 error_at (loc, op_error_string (G_("%qs"), 1, match),
4704 opname, TREE_TYPE (arg1));
4705 else
4706 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
4707 opname, opname, arg1, TREE_TYPE (arg1));
4708 break;
4710 default:
4711 if (arg2)
4712 if (flag_diagnostics_show_caret)
4713 error_at (loc, op_error_string (G_("%<operator%s%>"), 2, match),
4714 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
4715 else
4716 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
4717 2, match),
4718 opname, arg1, opname, arg2,
4719 TREE_TYPE (arg1), TREE_TYPE (arg2));
4720 else
4721 if (flag_diagnostics_show_caret)
4722 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4723 opname, TREE_TYPE (arg1));
4724 else
4725 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
4726 1, match),
4727 opname, opname, arg1, TREE_TYPE (arg1));
4728 break;
4732 /* Return the implicit conversion sequence that could be used to
4733 convert E1 to E2 in [expr.cond]. */
4735 static conversion *
4736 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
4738 tree t1 = non_reference (TREE_TYPE (e1));
4739 tree t2 = non_reference (TREE_TYPE (e2));
4740 conversion *conv;
4741 bool good_base;
4743 /* [expr.cond]
4745 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
4746 implicitly converted (clause _conv_) to the type "lvalue reference to
4747 T2", subject to the constraint that in the conversion the
4748 reference must bind directly (_dcl.init.ref_) to an lvalue.
4750 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
4751 implicitly converted to the type "rvalue reference to T2", subject to
4752 the constraint that the reference must bind directly. */
4753 if (glvalue_p (e2))
4755 tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
4756 conv = implicit_conversion (rtype,
4759 /*c_cast_p=*/false,
4760 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
4761 |LOOKUP_ONLYCONVERTING,
4762 complain);
4763 if (conv && !conv->bad_p)
4764 return conv;
4767 /* If E2 is a prvalue or if neither of the conversions above can be done
4768 and at least one of the operands has (possibly cv-qualified) class
4769 type: */
4770 if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
4771 return NULL;
4773 /* [expr.cond]
4775 If E1 and E2 have class type, and the underlying class types are
4776 the same or one is a base class of the other: E1 can be converted
4777 to match E2 if the class of T2 is the same type as, or a base
4778 class of, the class of T1, and the cv-qualification of T2 is the
4779 same cv-qualification as, or a greater cv-qualification than, the
4780 cv-qualification of T1. If the conversion is applied, E1 is
4781 changed to an rvalue of type T2 that still refers to the original
4782 source class object (or the appropriate subobject thereof). */
4783 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
4784 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
4786 if (good_base && at_least_as_qualified_p (t2, t1))
4788 conv = build_identity_conv (t1, e1);
4789 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
4790 TYPE_MAIN_VARIANT (t2)))
4791 conv = build_conv (ck_base, t2, conv);
4792 else
4793 conv = build_conv (ck_rvalue, t2, conv);
4794 return conv;
4796 else
4797 return NULL;
4799 else
4800 /* [expr.cond]
4802 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
4803 converted to the type that expression E2 would have if E2 were
4804 converted to an rvalue (or the type it has, if E2 is an rvalue). */
4805 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
4806 LOOKUP_IMPLICIT, complain);
4809 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
4810 arguments to the conditional expression. */
4812 static tree
4813 build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
4814 tsubst_flags_t complain)
4816 tree arg2_type;
4817 tree arg3_type;
4818 tree result = NULL_TREE;
4819 tree result_type = NULL_TREE;
4820 bool is_glvalue = true;
4821 struct z_candidate *candidates = 0;
4822 struct z_candidate *cand;
4823 void *p;
4824 tree orig_arg2, orig_arg3;
4826 /* As a G++ extension, the second argument to the conditional can be
4827 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
4828 c'.) If the second operand is omitted, make sure it is
4829 calculated only once. */
4830 if (!arg2)
4832 if (complain & tf_error)
4833 pedwarn (loc, OPT_Wpedantic,
4834 "ISO C++ forbids omitting the middle term of a ?: expression");
4836 if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
4837 warn_for_omitted_condop (loc, arg1);
4839 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
4840 if (lvalue_p (arg1))
4841 arg2 = arg1 = cp_stabilize_reference (arg1);
4842 else
4843 arg2 = arg1 = cp_save_expr (arg1);
4846 /* If something has already gone wrong, just pass that fact up the
4847 tree. */
4848 if (error_operand_p (arg1)
4849 || error_operand_p (arg2)
4850 || error_operand_p (arg3))
4851 return error_mark_node;
4853 orig_arg2 = arg2;
4854 orig_arg3 = arg3;
4856 if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
4858 tree arg1_type = TREE_TYPE (arg1);
4860 /* If arg1 is another cond_expr choosing between -1 and 0,
4861 then we can use its comparison. It may help to avoid
4862 additional comparison, produce more accurate diagnostics
4863 and enables folding. */
4864 if (TREE_CODE (arg1) == VEC_COND_EXPR
4865 && integer_minus_onep (TREE_OPERAND (arg1, 1))
4866 && integer_zerop (TREE_OPERAND (arg1, 2)))
4867 arg1 = TREE_OPERAND (arg1, 0);
4869 arg1 = force_rvalue (arg1, complain);
4870 arg2 = force_rvalue (arg2, complain);
4871 arg3 = force_rvalue (arg3, complain);
4873 /* force_rvalue can return error_mark on valid arguments. */
4874 if (error_operand_p (arg1)
4875 || error_operand_p (arg2)
4876 || error_operand_p (arg3))
4877 return error_mark_node;
4879 arg2_type = TREE_TYPE (arg2);
4880 arg3_type = TREE_TYPE (arg3);
4882 if (!VECTOR_TYPE_P (arg2_type)
4883 && !VECTOR_TYPE_P (arg3_type))
4885 /* Rely on the error messages of the scalar version. */
4886 tree scal = build_conditional_expr_1 (loc, integer_one_node,
4887 orig_arg2, orig_arg3, complain);
4888 if (scal == error_mark_node)
4889 return error_mark_node;
4890 tree stype = TREE_TYPE (scal);
4891 tree ctype = TREE_TYPE (arg1_type);
4892 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
4893 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
4895 if (complain & tf_error)
4896 error_at (loc, "inferred scalar type %qT is not an integer or "
4897 "floating point type of the same size as %qT", stype,
4898 COMPARISON_CLASS_P (arg1)
4899 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4900 : ctype);
4901 return error_mark_node;
4904 tree vtype = build_opaque_vector_type (stype,
4905 TYPE_VECTOR_SUBPARTS (arg1_type));
4906 /* We could pass complain & tf_warning to unsafe_conversion_p,
4907 but the warnings (like Wsign-conversion) have already been
4908 given by the scalar build_conditional_expr_1. We still check
4909 unsafe_conversion_p to forbid truncating long long -> float. */
4910 if (unsafe_conversion_p (loc, stype, arg2, NULL_TREE, false))
4912 if (complain & tf_error)
4913 error_at (loc, "conversion of scalar %qH to vector %qI "
4914 "involves truncation", arg2_type, vtype);
4915 return error_mark_node;
4917 if (unsafe_conversion_p (loc, stype, arg3, NULL_TREE, false))
4919 if (complain & tf_error)
4920 error_at (loc, "conversion of scalar %qH to vector %qI "
4921 "involves truncation", arg3_type, vtype);
4922 return error_mark_node;
4925 arg2 = cp_convert (stype, arg2, complain);
4926 arg2 = save_expr (arg2);
4927 arg2 = build_vector_from_val (vtype, arg2);
4928 arg2_type = vtype;
4929 arg3 = cp_convert (stype, arg3, complain);
4930 arg3 = save_expr (arg3);
4931 arg3 = build_vector_from_val (vtype, arg3);
4932 arg3_type = vtype;
4935 if (VECTOR_TYPE_P (arg2_type) != VECTOR_TYPE_P (arg3_type))
4937 enum stv_conv convert_flag =
4938 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
4939 complain & tf_error);
4941 switch (convert_flag)
4943 case stv_error:
4944 return error_mark_node;
4945 case stv_firstarg:
4947 arg2 = save_expr (arg2);
4948 arg2 = convert (TREE_TYPE (arg3_type), arg2);
4949 arg2 = build_vector_from_val (arg3_type, arg2);
4950 arg2_type = TREE_TYPE (arg2);
4951 break;
4953 case stv_secondarg:
4955 arg3 = save_expr (arg3);
4956 arg3 = convert (TREE_TYPE (arg2_type), arg3);
4957 arg3 = build_vector_from_val (arg2_type, arg3);
4958 arg3_type = TREE_TYPE (arg3);
4959 break;
4961 default:
4962 break;
4966 if (!same_type_p (arg2_type, arg3_type)
4967 || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type),
4968 TYPE_VECTOR_SUBPARTS (arg2_type))
4969 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
4971 if (complain & tf_error)
4972 error_at (loc,
4973 "incompatible vector types in conditional expression: "
4974 "%qT, %qT and %qT", TREE_TYPE (arg1),
4975 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
4976 return error_mark_node;
4979 if (!COMPARISON_CLASS_P (arg1))
4981 tree cmp_type = build_same_sized_truth_vector_type (arg1_type);
4982 arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
4984 return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
4987 /* [expr.cond]
4989 The first expression is implicitly converted to bool (clause
4990 _conv_). */
4991 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
4992 LOOKUP_NORMAL);
4993 if (error_operand_p (arg1))
4994 return error_mark_node;
4996 /* [expr.cond]
4998 If either the second or the third operand has type (possibly
4999 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
5000 array-to-pointer (_conv.array_), and function-to-pointer
5001 (_conv.func_) standard conversions are performed on the second
5002 and third operands. */
5003 arg2_type = unlowered_expr_type (arg2);
5004 arg3_type = unlowered_expr_type (arg3);
5005 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
5007 /* [expr.cond]
5009 One of the following shall hold:
5011 --The second or the third operand (but not both) is a
5012 throw-expression (_except.throw_); the result is of the type
5013 and value category of the other.
5015 --Both the second and the third operands have type void; the
5016 result is of type void and is a prvalue. */
5017 if (TREE_CODE (arg2) == THROW_EXPR
5018 && TREE_CODE (arg3) != THROW_EXPR)
5020 result_type = arg3_type;
5021 is_glvalue = glvalue_p (arg3);
5023 else if (TREE_CODE (arg2) != THROW_EXPR
5024 && TREE_CODE (arg3) == THROW_EXPR)
5026 result_type = arg2_type;
5027 is_glvalue = glvalue_p (arg2);
5029 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
5031 result_type = void_type_node;
5032 is_glvalue = false;
5034 else
5036 if (complain & tf_error)
5038 if (VOID_TYPE_P (arg2_type))
5039 error_at (cp_expr_loc_or_loc (arg3, loc),
5040 "second operand to the conditional operator "
5041 "is of type %<void%>, but the third operand is "
5042 "neither a throw-expression nor of type %<void%>");
5043 else
5044 error_at (cp_expr_loc_or_loc (arg2, loc),
5045 "third operand to the conditional operator "
5046 "is of type %<void%>, but the second operand is "
5047 "neither a throw-expression nor of type %<void%>");
5049 return error_mark_node;
5052 goto valid_operands;
5054 /* [expr.cond]
5056 Otherwise, if the second and third operand have different types,
5057 and either has (possibly cv-qualified) class type, or if both are
5058 glvalues of the same value category and the same type except for
5059 cv-qualification, an attempt is made to convert each of those operands
5060 to the type of the other. */
5061 else if (!same_type_p (arg2_type, arg3_type)
5062 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
5063 || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
5064 arg3_type)
5065 && glvalue_p (arg2) && glvalue_p (arg3)
5066 && lvalue_p (arg2) == lvalue_p (arg3))))
5068 conversion *conv2;
5069 conversion *conv3;
5070 bool converted = false;
5072 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5073 p = conversion_obstack_alloc (0);
5075 conv2 = conditional_conversion (arg2, arg3, complain);
5076 conv3 = conditional_conversion (arg3, arg2, complain);
5078 /* [expr.cond]
5080 If both can be converted, or one can be converted but the
5081 conversion is ambiguous, the program is ill-formed. If
5082 neither can be converted, the operands are left unchanged and
5083 further checking is performed as described below. If exactly
5084 one conversion is possible, that conversion is applied to the
5085 chosen operand and the converted operand is used in place of
5086 the original operand for the remainder of this section. */
5087 if ((conv2 && !conv2->bad_p
5088 && conv3 && !conv3->bad_p)
5089 || (conv2 && conv2->kind == ck_ambig)
5090 || (conv3 && conv3->kind == ck_ambig))
5092 if (complain & tf_error)
5094 error_at (loc, "operands to ?: have different types %qT and %qT",
5095 arg2_type, arg3_type);
5096 if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
5097 inform (loc, " and each type can be converted to the other");
5098 else if (conv2 && conv2->kind == ck_ambig)
5099 convert_like (conv2, arg2, complain);
5100 else
5101 convert_like (conv3, arg3, complain);
5103 result = error_mark_node;
5105 else if (conv2 && !conv2->bad_p)
5107 arg2 = convert_like (conv2, arg2, complain);
5108 arg2 = convert_from_reference (arg2);
5109 arg2_type = TREE_TYPE (arg2);
5110 /* Even if CONV2 is a valid conversion, the result of the
5111 conversion may be invalid. For example, if ARG3 has type
5112 "volatile X", and X does not have a copy constructor
5113 accepting a "volatile X&", then even if ARG2 can be
5114 converted to X, the conversion will fail. */
5115 if (error_operand_p (arg2))
5116 result = error_mark_node;
5117 converted = true;
5119 else if (conv3 && !conv3->bad_p)
5121 arg3 = convert_like (conv3, arg3, complain);
5122 arg3 = convert_from_reference (arg3);
5123 arg3_type = TREE_TYPE (arg3);
5124 if (error_operand_p (arg3))
5125 result = error_mark_node;
5126 converted = true;
5129 /* Free all the conversions we allocated. */
5130 obstack_free (&conversion_obstack, p);
5132 if (result)
5133 return result;
5135 /* If, after the conversion, both operands have class type,
5136 treat the cv-qualification of both operands as if it were the
5137 union of the cv-qualification of the operands.
5139 The standard is not clear about what to do in this
5140 circumstance. For example, if the first operand has type
5141 "const X" and the second operand has a user-defined
5142 conversion to "volatile X", what is the type of the second
5143 operand after this step? Making it be "const X" (matching
5144 the first operand) seems wrong, as that discards the
5145 qualification without actually performing a copy. Leaving it
5146 as "volatile X" seems wrong as that will result in the
5147 conditional expression failing altogether, even though,
5148 according to this step, the one operand could be converted to
5149 the type of the other. */
5150 if (converted
5151 && CLASS_TYPE_P (arg2_type)
5152 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
5153 arg2_type = arg3_type =
5154 cp_build_qualified_type (arg2_type,
5155 cp_type_quals (arg2_type)
5156 | cp_type_quals (arg3_type));
5159 /* [expr.cond]
5161 If the second and third operands are glvalues of the same value
5162 category and have the same type, the result is of that type and
5163 value category. */
5164 if (((lvalue_p (arg2) && lvalue_p (arg3))
5165 || (xvalue_p (arg2) && xvalue_p (arg3)))
5166 && same_type_p (arg2_type, arg3_type))
5168 result_type = arg2_type;
5169 arg2 = mark_lvalue_use (arg2);
5170 arg3 = mark_lvalue_use (arg3);
5171 goto valid_operands;
5174 /* [expr.cond]
5176 Otherwise, the result is an rvalue. If the second and third
5177 operand do not have the same type, and either has (possibly
5178 cv-qualified) class type, overload resolution is used to
5179 determine the conversions (if any) to be applied to the operands
5180 (_over.match.oper_, _over.built_). */
5181 is_glvalue = false;
5182 if (!same_type_p (arg2_type, arg3_type)
5183 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
5185 tree args[3];
5186 conversion *conv;
5187 bool any_viable_p;
5189 /* Rearrange the arguments so that add_builtin_candidate only has
5190 to know about two args. In build_builtin_candidate, the
5191 arguments are unscrambled. */
5192 args[0] = arg2;
5193 args[1] = arg3;
5194 args[2] = arg1;
5195 add_builtin_candidates (&candidates,
5196 COND_EXPR,
5197 NOP_EXPR,
5198 ovl_op_identifier (false, COND_EXPR),
5199 args,
5200 LOOKUP_NORMAL, complain);
5202 /* [expr.cond]
5204 If the overload resolution fails, the program is
5205 ill-formed. */
5206 candidates = splice_viable (candidates, false, &any_viable_p);
5207 if (!any_viable_p)
5209 if (complain & tf_error)
5210 error_at (loc, "operands to ?: have different types %qT and %qT",
5211 arg2_type, arg3_type);
5212 return error_mark_node;
5214 cand = tourney (candidates, complain);
5215 if (!cand)
5217 if (complain & tf_error)
5219 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
5220 print_z_candidates (loc, candidates);
5222 return error_mark_node;
5225 /* [expr.cond]
5227 Otherwise, the conversions thus determined are applied, and
5228 the converted operands are used in place of the original
5229 operands for the remainder of this section. */
5230 conv = cand->convs[0];
5231 arg1 = convert_like (conv, arg1, complain);
5232 conv = cand->convs[1];
5233 arg2 = convert_like (conv, arg2, complain);
5234 arg2_type = TREE_TYPE (arg2);
5235 conv = cand->convs[2];
5236 arg3 = convert_like (conv, arg3, complain);
5237 arg3_type = TREE_TYPE (arg3);
5240 /* [expr.cond]
5242 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
5243 and function-to-pointer (_conv.func_) standard conversions are
5244 performed on the second and third operands.
5246 We need to force the lvalue-to-rvalue conversion here for class types,
5247 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
5248 that isn't wrapped with a TARGET_EXPR plays havoc with exception
5249 regions. */
5251 arg2 = force_rvalue (arg2, complain);
5252 if (!CLASS_TYPE_P (arg2_type))
5253 arg2_type = TREE_TYPE (arg2);
5255 arg3 = force_rvalue (arg3, complain);
5256 if (!CLASS_TYPE_P (arg3_type))
5257 arg3_type = TREE_TYPE (arg3);
5259 if (arg2 == error_mark_node || arg3 == error_mark_node)
5260 return error_mark_node;
5262 /* [expr.cond]
5264 After those conversions, one of the following shall hold:
5266 --The second and third operands have the same type; the result is of
5267 that type. */
5268 if (same_type_p (arg2_type, arg3_type))
5269 result_type = arg2_type;
5270 /* [expr.cond]
5272 --The second and third operands have arithmetic or enumeration
5273 type; the usual arithmetic conversions are performed to bring
5274 them to a common type, and the result is of that type. */
5275 else if ((ARITHMETIC_TYPE_P (arg2_type)
5276 || UNSCOPED_ENUM_P (arg2_type))
5277 && (ARITHMETIC_TYPE_P (arg3_type)
5278 || UNSCOPED_ENUM_P (arg3_type)))
5280 /* In this case, there is always a common type. */
5281 result_type = type_after_usual_arithmetic_conversions (arg2_type,
5282 arg3_type);
5283 if (complain & tf_warning)
5284 do_warn_double_promotion (result_type, arg2_type, arg3_type,
5285 "implicit conversion from %qH to %qI to "
5286 "match other result of conditional",
5287 loc);
5289 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
5290 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
5292 if (TREE_CODE (orig_arg2) == CONST_DECL
5293 && TREE_CODE (orig_arg3) == CONST_DECL
5294 && DECL_CONTEXT (orig_arg2) == DECL_CONTEXT (orig_arg3))
5295 /* Two enumerators from the same enumeration can have different
5296 types when the enumeration is still being defined. */;
5297 else if (complain & tf_warning)
5298 warning_at (loc, OPT_Wenum_compare, "enumeral mismatch in "
5299 "conditional expression: %qT vs %qT",
5300 arg2_type, arg3_type);
5302 else if (extra_warnings
5303 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
5304 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
5305 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
5306 && !same_type_p (arg2_type,
5307 type_promotes_to (arg3_type)))))
5309 if (complain & tf_warning)
5310 warning_at (loc, OPT_Wextra, "enumeral and non-enumeral type in "
5311 "conditional expression");
5314 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5315 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5317 /* [expr.cond]
5319 --The second and third operands have pointer type, or one has
5320 pointer type and the other is a null pointer constant; pointer
5321 conversions (_conv.ptr_) and qualification conversions
5322 (_conv.qual_) are performed to bring them to their composite
5323 pointer type (_expr.rel_). The result is of the composite
5324 pointer type.
5326 --The second and third operands have pointer to member type, or
5327 one has pointer to member type and the other is a null pointer
5328 constant; pointer to member conversions (_conv.mem_) and
5329 qualification conversions (_conv.qual_) are performed to bring
5330 them to a common type, whose cv-qualification shall match the
5331 cv-qualification of either the second or the third operand.
5332 The result is of the common type. */
5333 else if ((null_ptr_cst_p (arg2)
5334 && TYPE_PTR_OR_PTRMEM_P (arg3_type))
5335 || (null_ptr_cst_p (arg3)
5336 && TYPE_PTR_OR_PTRMEM_P (arg2_type))
5337 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
5338 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
5339 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
5341 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
5342 arg3, CPO_CONDITIONAL_EXPR,
5343 complain);
5344 if (result_type == error_mark_node)
5345 return error_mark_node;
5346 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5347 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5350 if (!result_type)
5352 if (complain & tf_error)
5353 error_at (loc, "operands to ?: have different types %qT and %qT",
5354 arg2_type, arg3_type);
5355 return error_mark_node;
5358 if (arg2 == error_mark_node || arg3 == error_mark_node)
5359 return error_mark_node;
5361 valid_operands:
5362 if (processing_template_decl && is_glvalue)
5364 /* Let lvalue_kind know this was a glvalue. */
5365 tree arg = (result_type == arg2_type ? arg2 : arg3);
5366 result_type = cp_build_reference_type (result_type, xvalue_p (arg));
5369 result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
5371 /* If the ARG2 and ARG3 are the same and don't have side-effects,
5372 warn here, because the COND_EXPR will be turned into ARG2. */
5373 if (warn_duplicated_branches
5374 && (complain & tf_warning)
5375 && (arg2 == arg3 || operand_equal_p (arg2, arg3, 0)))
5376 warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
5377 "this condition has identical branches");
5379 /* We can't use result_type below, as fold might have returned a
5380 throw_expr. */
5382 if (!is_glvalue)
5384 /* Expand both sides into the same slot, hopefully the target of
5385 the ?: expression. We used to check for TARGET_EXPRs here,
5386 but now we sometimes wrap them in NOP_EXPRs so the test would
5387 fail. */
5388 if (CLASS_TYPE_P (TREE_TYPE (result)))
5389 result = get_target_expr_sfinae (result, complain);
5390 /* If this expression is an rvalue, but might be mistaken for an
5391 lvalue, we must add a NON_LVALUE_EXPR. */
5392 result = rvalue (result);
5394 else
5395 result = force_paren_expr (result);
5397 return result;
5400 /* Wrapper for above. */
5402 tree
5403 build_conditional_expr (location_t loc, tree arg1, tree arg2, tree arg3,
5404 tsubst_flags_t complain)
5406 tree ret;
5407 bool subtime = timevar_cond_start (TV_OVERLOAD);
5408 ret = build_conditional_expr_1 (loc, arg1, arg2, arg3, complain);
5409 timevar_cond_stop (TV_OVERLOAD, subtime);
5410 return ret;
5413 /* OPERAND is an operand to an expression. Perform necessary steps
5414 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
5415 returned. */
5417 static tree
5418 prep_operand (tree operand)
5420 if (operand)
5422 if (CLASS_TYPE_P (TREE_TYPE (operand))
5423 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
5424 /* Make sure the template type is instantiated now. */
5425 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
5428 return operand;
5431 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5432 OVERLOAD) to the CANDIDATES, returning an updated list of
5433 CANDIDATES. The ARGS are the arguments provided to the call;
5434 if FIRST_ARG is non-null it is the implicit object argument,
5435 otherwise the first element of ARGS is used if needed. The
5436 EXPLICIT_TARGS are explicit template arguments provided.
5437 TEMPLATE_ONLY is true if only template functions should be
5438 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5439 add_function_candidate. */
5441 static void
5442 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
5443 tree return_type,
5444 tree explicit_targs, bool template_only,
5445 tree conversion_path, tree access_path,
5446 int flags,
5447 struct z_candidate **candidates,
5448 tsubst_flags_t complain)
5450 tree ctype;
5451 const vec<tree, va_gc> *non_static_args;
5452 bool check_list_ctor = false;
5453 bool check_converting = false;
5454 unification_kind_t strict;
5456 if (!fns)
5457 return;
5459 /* Precalculate special handling of constructors and conversion ops. */
5460 tree fn = OVL_FIRST (fns);
5461 if (DECL_CONV_FN_P (fn))
5463 check_list_ctor = false;
5464 check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
5465 if (flags & LOOKUP_NO_CONVERSION)
5466 /* We're doing return_type(x). */
5467 strict = DEDUCE_CONV;
5468 else
5469 /* We're doing x.operator return_type(). */
5470 strict = DEDUCE_EXACT;
5471 /* [over.match.funcs] For conversion functions, the function
5472 is considered to be a member of the class of the implicit
5473 object argument for the purpose of defining the type of
5474 the implicit object parameter. */
5475 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
5477 else
5479 if (DECL_CONSTRUCTOR_P (fn))
5481 check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
5482 /* For list-initialization we consider explicit constructors
5483 and complain if one is chosen. */
5484 check_converting
5485 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
5486 == LOOKUP_ONLYCONVERTING);
5488 strict = DEDUCE_CALL;
5489 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
5492 if (first_arg)
5493 non_static_args = args;
5494 else
5495 /* Delay creating the implicit this parameter until it is needed. */
5496 non_static_args = NULL;
5498 for (lkp_iterator iter (fns); iter; ++iter)
5500 fn = *iter;
5502 if (check_converting && DECL_NONCONVERTING_P (fn))
5503 continue;
5504 if (check_list_ctor && !is_list_ctor (fn))
5505 continue;
5507 tree fn_first_arg = NULL_TREE;
5508 const vec<tree, va_gc> *fn_args = args;
5510 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
5512 /* Figure out where the object arg comes from. If this
5513 function is a non-static member and we didn't get an
5514 implicit object argument, move it out of args. */
5515 if (first_arg == NULL_TREE)
5517 unsigned int ix;
5518 tree arg;
5519 vec<tree, va_gc> *tempvec;
5520 vec_alloc (tempvec, args->length () - 1);
5521 for (ix = 1; args->iterate (ix, &arg); ++ix)
5522 tempvec->quick_push (arg);
5523 non_static_args = tempvec;
5524 first_arg = (*args)[0];
5527 fn_first_arg = first_arg;
5528 fn_args = non_static_args;
5531 if (TREE_CODE (fn) == TEMPLATE_DECL)
5532 add_template_candidate (candidates,
5534 ctype,
5535 explicit_targs,
5536 fn_first_arg,
5537 fn_args,
5538 return_type,
5539 access_path,
5540 conversion_path,
5541 flags,
5542 strict,
5543 complain);
5544 else if (!template_only)
5545 add_function_candidate (candidates,
5547 ctype,
5548 fn_first_arg,
5549 fn_args,
5550 access_path,
5551 conversion_path,
5552 flags,
5553 NULL,
5554 complain);
5558 /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
5559 -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
5561 static int
5562 op_is_ordered (tree_code code)
5564 switch (code)
5566 // 5. b @= a
5567 case MODIFY_EXPR:
5568 return (flag_strong_eval_order > 1 ? -1 : 0);
5570 // 6. a[b]
5571 case ARRAY_REF:
5572 return (flag_strong_eval_order > 1 ? 1 : 0);
5574 // 1. a.b
5575 // Not overloadable (yet).
5576 // 2. a->b
5577 // Only one argument.
5578 // 3. a->*b
5579 case MEMBER_REF:
5580 // 7. a << b
5581 case LSHIFT_EXPR:
5582 // 8. a >> b
5583 case RSHIFT_EXPR:
5584 return (flag_strong_eval_order ? 1 : 0);
5586 default:
5587 return 0;
5591 static tree
5592 build_new_op_1 (location_t loc, enum tree_code code, int flags, tree arg1,
5593 tree arg2, tree arg3, tree *overload, tsubst_flags_t complain)
5595 struct z_candidate *candidates = 0, *cand;
5596 vec<tree, va_gc> *arglist;
5597 tree args[3];
5598 tree result = NULL_TREE;
5599 bool result_valid_p = false;
5600 enum tree_code code2 = NOP_EXPR;
5601 enum tree_code code_orig_arg1 = ERROR_MARK;
5602 enum tree_code code_orig_arg2 = ERROR_MARK;
5603 conversion *conv;
5604 void *p;
5605 bool strict_p;
5606 bool any_viable_p;
5608 if (error_operand_p (arg1)
5609 || error_operand_p (arg2)
5610 || error_operand_p (arg3))
5611 return error_mark_node;
5613 bool ismodop = code == MODIFY_EXPR;
5614 if (ismodop)
5616 code2 = TREE_CODE (arg3);
5617 arg3 = NULL_TREE;
5619 tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
5621 arg1 = prep_operand (arg1);
5623 bool memonly = false;
5624 switch (code)
5626 case NEW_EXPR:
5627 case VEC_NEW_EXPR:
5628 case VEC_DELETE_EXPR:
5629 case DELETE_EXPR:
5630 /* Use build_op_new_call and build_op_delete_call instead. */
5631 gcc_unreachable ();
5633 case CALL_EXPR:
5634 /* Use build_op_call instead. */
5635 gcc_unreachable ();
5637 case TRUTH_ORIF_EXPR:
5638 case TRUTH_ANDIF_EXPR:
5639 case TRUTH_AND_EXPR:
5640 case TRUTH_OR_EXPR:
5641 /* These are saved for the sake of warn_logical_operator. */
5642 code_orig_arg1 = TREE_CODE (arg1);
5643 code_orig_arg2 = TREE_CODE (arg2);
5644 break;
5645 case GT_EXPR:
5646 case LT_EXPR:
5647 case GE_EXPR:
5648 case LE_EXPR:
5649 case EQ_EXPR:
5650 case NE_EXPR:
5651 /* These are saved for the sake of maybe_warn_bool_compare. */
5652 code_orig_arg1 = TREE_CODE (TREE_TYPE (arg1));
5653 code_orig_arg2 = TREE_CODE (TREE_TYPE (arg2));
5654 break;
5656 /* =, ->, [], () must be non-static member functions. */
5657 case MODIFY_EXPR:
5658 if (code2 != NOP_EXPR)
5659 break;
5660 /* FALLTHRU */
5661 case COMPONENT_REF:
5662 case ARRAY_REF:
5663 memonly = true;
5664 break;
5666 default:
5667 break;
5670 arg2 = prep_operand (arg2);
5671 arg3 = prep_operand (arg3);
5673 if (code == COND_EXPR)
5674 /* Use build_conditional_expr instead. */
5675 gcc_unreachable ();
5676 else if (! OVERLOAD_TYPE_P (TREE_TYPE (arg1))
5677 && (! arg2 || ! OVERLOAD_TYPE_P (TREE_TYPE (arg2))))
5678 goto builtin;
5680 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5681 arg2 = integer_zero_node;
5683 vec_alloc (arglist, 3);
5684 arglist->quick_push (arg1);
5685 if (arg2 != NULL_TREE)
5686 arglist->quick_push (arg2);
5687 if (arg3 != NULL_TREE)
5688 arglist->quick_push (arg3);
5690 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5691 p = conversion_obstack_alloc (0);
5693 /* Add namespace-scope operators to the list of functions to
5694 consider. */
5695 if (!memonly)
5697 tree fns = lookup_name_real (fnname, 0, 1, /*block_p=*/true, 0, 0);
5698 fns = lookup_arg_dependent (fnname, fns, arglist);
5699 add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
5700 NULL_TREE, false, NULL_TREE, NULL_TREE,
5701 flags, &candidates, complain);
5704 args[0] = arg1;
5705 args[1] = arg2;
5706 args[2] = NULL_TREE;
5708 /* Add class-member operators to the candidate set. */
5709 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
5711 tree fns;
5713 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
5714 if (fns == error_mark_node)
5716 result = error_mark_node;
5717 goto user_defined_result_ready;
5719 if (fns)
5720 add_candidates (BASELINK_FUNCTIONS (fns),
5721 NULL_TREE, arglist, NULL_TREE,
5722 NULL_TREE, false,
5723 BASELINK_BINFO (fns),
5724 BASELINK_ACCESS_BINFO (fns),
5725 flags, &candidates, complain);
5727 /* Per 13.3.1.2/3, 2nd bullet, if no operand has a class type, then
5728 only non-member functions that have type T1 or reference to
5729 cv-qualified-opt T1 for the first argument, if the first argument
5730 has an enumeration type, or T2 or reference to cv-qualified-opt
5731 T2 for the second argument, if the second argument has an
5732 enumeration type. Filter out those that don't match. */
5733 else if (! arg2 || ! CLASS_TYPE_P (TREE_TYPE (arg2)))
5735 struct z_candidate **candp, **next;
5737 for (candp = &candidates; *candp; candp = next)
5739 tree parmlist, parmtype;
5740 int i, nargs = (arg2 ? 2 : 1);
5742 cand = *candp;
5743 next = &cand->next;
5745 parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5747 for (i = 0; i < nargs; ++i)
5749 parmtype = TREE_VALUE (parmlist);
5751 if (TYPE_REF_P (parmtype))
5752 parmtype = TREE_TYPE (parmtype);
5753 if (TREE_CODE (TREE_TYPE (args[i])) == ENUMERAL_TYPE
5754 && (same_type_ignoring_top_level_qualifiers_p
5755 (TREE_TYPE (args[i]), parmtype)))
5756 break;
5758 parmlist = TREE_CHAIN (parmlist);
5761 /* No argument has an appropriate type, so remove this
5762 candidate function from the list. */
5763 if (i == nargs)
5765 *candp = cand->next;
5766 next = candp;
5771 add_builtin_candidates (&candidates, code, code2, fnname, args,
5772 flags, complain);
5774 switch (code)
5776 case COMPOUND_EXPR:
5777 case ADDR_EXPR:
5778 /* For these, the built-in candidates set is empty
5779 [over.match.oper]/3. We don't want non-strict matches
5780 because exact matches are always possible with built-in
5781 operators. The built-in candidate set for COMPONENT_REF
5782 would be empty too, but since there are no such built-in
5783 operators, we accept non-strict matches for them. */
5784 strict_p = true;
5785 break;
5787 default:
5788 strict_p = false;
5789 break;
5792 candidates = splice_viable (candidates, strict_p, &any_viable_p);
5793 if (!any_viable_p)
5795 switch (code)
5797 case POSTINCREMENT_EXPR:
5798 case POSTDECREMENT_EXPR:
5799 /* Don't try anything fancy if we're not allowed to produce
5800 errors. */
5801 if (!(complain & tf_error))
5802 return error_mark_node;
5804 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
5805 distinguish between prefix and postfix ++ and
5806 operator++() was used for both, so we allow this with
5807 -fpermissive. */
5808 else
5810 const char *msg = (flag_permissive)
5811 ? G_("no %<%D(int)%> declared for postfix %qs,"
5812 " trying prefix operator instead")
5813 : G_("no %<%D(int)%> declared for postfix %qs");
5814 permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
5817 if (!flag_permissive)
5818 return error_mark_node;
5820 if (code == POSTINCREMENT_EXPR)
5821 code = PREINCREMENT_EXPR;
5822 else
5823 code = PREDECREMENT_EXPR;
5824 result = build_new_op_1 (loc, code, flags, arg1, NULL_TREE,
5825 NULL_TREE, overload, complain);
5826 break;
5828 /* The caller will deal with these. */
5829 case ADDR_EXPR:
5830 case COMPOUND_EXPR:
5831 case COMPONENT_REF:
5832 result = NULL_TREE;
5833 result_valid_p = true;
5834 break;
5836 default:
5837 if (complain & tf_error)
5839 /* If one of the arguments of the operator represents
5840 an invalid use of member function pointer, try to report
5841 a meaningful error ... */
5842 if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
5843 || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
5844 || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
5845 /* We displayed the error message. */;
5846 else
5848 /* ... Otherwise, report the more generic
5849 "no matching operator found" error */
5850 op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
5851 print_z_candidates (loc, candidates);
5854 result = error_mark_node;
5855 break;
5858 else
5860 cand = tourney (candidates, complain);
5861 if (cand == 0)
5863 if (complain & tf_error)
5865 op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
5866 print_z_candidates (loc, candidates);
5868 result = error_mark_node;
5870 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5872 if (overload)
5873 *overload = cand->fn;
5875 if (resolve_args (arglist, complain) == NULL)
5876 result = error_mark_node;
5877 else
5878 result = build_over_call (cand, LOOKUP_NORMAL, complain);
5880 if (trivial_fn_p (cand->fn))
5881 /* There won't be a CALL_EXPR. */;
5882 else if (result && result != error_mark_node)
5884 tree call = extract_call_expr (result);
5885 CALL_EXPR_OPERATOR_SYNTAX (call) = true;
5887 if (processing_template_decl && DECL_HIDDEN_FRIEND_P (cand->fn))
5888 /* This prevents build_new_function_call from discarding this
5889 function during instantiation of the enclosing template. */
5890 KOENIG_LOOKUP_P (call) = 1;
5892 /* Specify evaluation order as per P0145R2. */
5893 CALL_EXPR_ORDERED_ARGS (call) = false;
5894 switch (op_is_ordered (code))
5896 case -1:
5897 CALL_EXPR_REVERSE_ARGS (call) = true;
5898 break;
5900 case 1:
5901 CALL_EXPR_ORDERED_ARGS (call) = true;
5902 break;
5904 default:
5905 break;
5909 else
5911 /* Give any warnings we noticed during overload resolution. */
5912 if (cand->warnings && (complain & tf_warning))
5914 struct candidate_warning *w;
5915 for (w = cand->warnings; w; w = w->next)
5916 joust (cand, w->loser, 1, complain);
5919 /* Check for comparison of different enum types. */
5920 switch (code)
5922 case GT_EXPR:
5923 case LT_EXPR:
5924 case GE_EXPR:
5925 case LE_EXPR:
5926 case EQ_EXPR:
5927 case NE_EXPR:
5928 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
5929 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
5930 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5931 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
5932 && (complain & tf_warning))
5934 warning (OPT_Wenum_compare,
5935 "comparison between %q#T and %q#T",
5936 TREE_TYPE (arg1), TREE_TYPE (arg2));
5938 break;
5939 default:
5940 break;
5943 /* We need to strip any leading REF_BIND so that bitfields
5944 don't cause errors. This should not remove any important
5945 conversions, because builtins don't apply to class
5946 objects directly. */
5947 conv = cand->convs[0];
5948 if (conv->kind == ck_ref_bind)
5949 conv = next_conversion (conv);
5950 arg1 = convert_like (conv, arg1, complain);
5952 if (arg2)
5954 conv = cand->convs[1];
5955 if (conv->kind == ck_ref_bind)
5956 conv = next_conversion (conv);
5957 else
5958 arg2 = decay_conversion (arg2, complain);
5960 /* We need to call warn_logical_operator before
5961 converting arg2 to a boolean_type, but after
5962 decaying an enumerator to its value. */
5963 if (complain & tf_warning)
5964 warn_logical_operator (loc, code, boolean_type_node,
5965 code_orig_arg1, arg1,
5966 code_orig_arg2, arg2);
5968 arg2 = convert_like (conv, arg2, complain);
5970 if (arg3)
5972 conv = cand->convs[2];
5973 if (conv->kind == ck_ref_bind)
5974 conv = next_conversion (conv);
5975 arg3 = convert_like (conv, arg3, complain);
5981 user_defined_result_ready:
5983 /* Free all the conversions we allocated. */
5984 obstack_free (&conversion_obstack, p);
5986 if (result || result_valid_p)
5987 return result;
5989 builtin:
5990 switch (code)
5992 case MODIFY_EXPR:
5993 return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
5995 case INDIRECT_REF:
5996 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
5998 case TRUTH_ANDIF_EXPR:
5999 case TRUTH_ORIF_EXPR:
6000 case TRUTH_AND_EXPR:
6001 case TRUTH_OR_EXPR:
6002 if (complain & tf_warning)
6003 warn_logical_operator (loc, code, boolean_type_node,
6004 code_orig_arg1, arg1,
6005 code_orig_arg2, arg2);
6006 /* Fall through. */
6007 case GT_EXPR:
6008 case LT_EXPR:
6009 case GE_EXPR:
6010 case LE_EXPR:
6011 case EQ_EXPR:
6012 case NE_EXPR:
6013 if ((complain & tf_warning)
6014 && ((code_orig_arg1 == BOOLEAN_TYPE)
6015 ^ (code_orig_arg2 == BOOLEAN_TYPE)))
6016 maybe_warn_bool_compare (loc, code, arg1, arg2);
6017 if (complain & tf_warning && warn_tautological_compare)
6018 warn_tautological_cmp (loc, code, arg1, arg2);
6019 /* Fall through. */
6020 case PLUS_EXPR:
6021 case MINUS_EXPR:
6022 case MULT_EXPR:
6023 case TRUNC_DIV_EXPR:
6024 case MAX_EXPR:
6025 case MIN_EXPR:
6026 case LSHIFT_EXPR:
6027 case RSHIFT_EXPR:
6028 case TRUNC_MOD_EXPR:
6029 case BIT_AND_EXPR:
6030 case BIT_IOR_EXPR:
6031 case BIT_XOR_EXPR:
6032 return cp_build_binary_op (loc, code, arg1, arg2, complain);
6034 case UNARY_PLUS_EXPR:
6035 case NEGATE_EXPR:
6036 case BIT_NOT_EXPR:
6037 case TRUTH_NOT_EXPR:
6038 case PREINCREMENT_EXPR:
6039 case POSTINCREMENT_EXPR:
6040 case PREDECREMENT_EXPR:
6041 case POSTDECREMENT_EXPR:
6042 case REALPART_EXPR:
6043 case IMAGPART_EXPR:
6044 case ABS_EXPR:
6045 return cp_build_unary_op (code, arg1, candidates != 0, complain);
6047 case ARRAY_REF:
6048 return cp_build_array_ref (input_location, arg1, arg2, complain);
6050 case MEMBER_REF:
6051 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_ARROW_STAR,
6052 complain),
6053 arg2, complain);
6055 /* The caller will deal with these. */
6056 case ADDR_EXPR:
6057 case COMPONENT_REF:
6058 case COMPOUND_EXPR:
6059 return NULL_TREE;
6061 default:
6062 gcc_unreachable ();
6064 return NULL_TREE;
6067 /* Wrapper for above. */
6069 tree
6070 build_new_op (location_t loc, enum tree_code code, int flags,
6071 tree arg1, tree arg2, tree arg3,
6072 tree *overload, tsubst_flags_t complain)
6074 tree ret;
6075 bool subtime = timevar_cond_start (TV_OVERLOAD);
6076 ret = build_new_op_1 (loc, code, flags, arg1, arg2, arg3,
6077 overload, complain);
6078 timevar_cond_stop (TV_OVERLOAD, subtime);
6079 return ret;
6082 /* CALL was returned by some call-building function; extract the actual
6083 CALL_EXPR from any bits that have been tacked on, e.g. by
6084 convert_from_reference. */
6086 tree
6087 extract_call_expr (tree call)
6089 while (TREE_CODE (call) == COMPOUND_EXPR)
6090 call = TREE_OPERAND (call, 1);
6091 if (REFERENCE_REF_P (call))
6092 call = TREE_OPERAND (call, 0);
6093 if (TREE_CODE (call) == TARGET_EXPR)
6094 call = TARGET_EXPR_INITIAL (call);
6095 gcc_assert (TREE_CODE (call) == CALL_EXPR
6096 || TREE_CODE (call) == AGGR_INIT_EXPR
6097 || call == error_mark_node);
6098 return call;
6101 /* Returns true if FN has two parameters, of which the second has type
6102 size_t. */
6104 static bool
6105 second_parm_is_size_t (tree fn)
6107 tree t = FUNCTION_ARG_CHAIN (fn);
6108 if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
6109 return false;
6110 t = TREE_CHAIN (t);
6111 if (t == void_list_node)
6112 return true;
6113 if (aligned_new_threshold && t
6114 && same_type_p (TREE_VALUE (t), align_type_node)
6115 && TREE_CHAIN (t) == void_list_node)
6116 return true;
6117 return false;
6120 /* True if T, an allocation function, has std::align_val_t as its second
6121 argument. */
6123 bool
6124 aligned_allocation_fn_p (tree t)
6126 if (!aligned_new_threshold)
6127 return false;
6129 tree a = FUNCTION_ARG_CHAIN (t);
6130 return (a && same_type_p (TREE_VALUE (a), align_type_node));
6133 /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
6134 function (3.7.4.2 [basic.stc.dynamic.deallocation]) with a parameter of
6135 std::align_val_t. */
6137 static bool
6138 aligned_deallocation_fn_p (tree t)
6140 if (!aligned_new_threshold)
6141 return false;
6143 /* A template instance is never a usual deallocation function,
6144 regardless of its signature. */
6145 if (TREE_CODE (t) == TEMPLATE_DECL
6146 || primary_template_specialization_p (t))
6147 return false;
6149 tree a = FUNCTION_ARG_CHAIN (t);
6150 if (same_type_p (TREE_VALUE (a), align_type_node)
6151 && TREE_CHAIN (a) == void_list_node)
6152 return true;
6153 if (!same_type_p (TREE_VALUE (a), size_type_node))
6154 return false;
6155 a = TREE_CHAIN (a);
6156 if (a && same_type_p (TREE_VALUE (a), align_type_node)
6157 && TREE_CHAIN (a) == void_list_node)
6158 return true;
6159 return false;
6162 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
6163 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
6165 bool
6166 usual_deallocation_fn_p (tree t)
6168 /* A template instance is never a usual deallocation function,
6169 regardless of its signature. */
6170 if (TREE_CODE (t) == TEMPLATE_DECL
6171 || primary_template_specialization_p (t))
6172 return false;
6174 /* If a class T has a member deallocation function named operator delete
6175 with exactly one parameter, then that function is a usual
6176 (non-placement) deallocation function. If class T does not declare
6177 such an operator delete but does declare a member deallocation
6178 function named operator delete with exactly two parameters, the second
6179 of which has type std::size_t (18.2), then this function is a usual
6180 deallocation function. */
6181 bool global = DECL_NAMESPACE_SCOPE_P (t);
6182 tree chain = FUNCTION_ARG_CHAIN (t);
6183 if (!chain)
6184 return false;
6185 if (chain == void_list_node
6186 || ((!global || flag_sized_deallocation)
6187 && second_parm_is_size_t (t)))
6188 return true;
6189 if (aligned_deallocation_fn_p (t))
6190 return true;
6191 return false;
6194 /* Build a call to operator delete. This has to be handled very specially,
6195 because the restrictions on what signatures match are different from all
6196 other call instances. For a normal delete, only a delete taking (void *)
6197 or (void *, size_t) is accepted. For a placement delete, only an exact
6198 match with the placement new is accepted.
6200 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
6201 ADDR is the pointer to be deleted.
6202 SIZE is the size of the memory block to be deleted.
6203 GLOBAL_P is true if the delete-expression should not consider
6204 class-specific delete operators.
6205 PLACEMENT is the corresponding placement new call, or NULL_TREE.
6207 If this call to "operator delete" is being generated as part to
6208 deallocate memory allocated via a new-expression (as per [expr.new]
6209 which requires that if the initialization throws an exception then
6210 we call a deallocation function), then ALLOC_FN is the allocation
6211 function. */
6213 tree
6214 build_op_delete_call (enum tree_code code, tree addr, tree size,
6215 bool global_p, tree placement,
6216 tree alloc_fn, tsubst_flags_t complain)
6218 tree fn = NULL_TREE;
6219 tree fns, fnname, type, t;
6221 if (addr == error_mark_node)
6222 return error_mark_node;
6224 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
6226 fnname = ovl_op_identifier (false, code);
6228 if (CLASS_TYPE_P (type)
6229 && COMPLETE_TYPE_P (complete_type (type))
6230 && !global_p)
6231 /* In [class.free]
6233 If the result of the lookup is ambiguous or inaccessible, or if
6234 the lookup selects a placement deallocation function, the
6235 program is ill-formed.
6237 Therefore, we ask lookup_fnfields to complain about ambiguity. */
6239 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
6240 if (fns == error_mark_node)
6241 return error_mark_node;
6243 else
6244 fns = NULL_TREE;
6246 if (fns == NULL_TREE)
6247 fns = lookup_name_nonclass (fnname);
6249 /* Strip const and volatile from addr. */
6250 addr = cp_convert (ptr_type_node, addr, complain);
6252 if (placement)
6254 /* "A declaration of a placement deallocation function matches the
6255 declaration of a placement allocation function if it has the same
6256 number of parameters and, after parameter transformations (8.3.5),
6257 all parameter types except the first are identical."
6259 So we build up the function type we want and ask instantiate_type
6260 to get it for us. */
6261 t = FUNCTION_ARG_CHAIN (alloc_fn);
6262 t = tree_cons (NULL_TREE, ptr_type_node, t);
6263 t = build_function_type (void_type_node, t);
6265 fn = instantiate_type (t, fns, tf_none);
6266 if (fn == error_mark_node)
6267 return NULL_TREE;
6269 fn = MAYBE_BASELINK_FUNCTIONS (fn);
6271 /* "If the lookup finds the two-parameter form of a usual deallocation
6272 function (3.7.4.2) and that function, considered as a placement
6273 deallocation function, would have been selected as a match for the
6274 allocation function, the program is ill-formed." */
6275 if (second_parm_is_size_t (fn))
6277 const char *const msg1
6278 = G_("exception cleanup for this placement new selects "
6279 "non-placement operator delete");
6280 const char *const msg2
6281 = G_("%qD is a usual (non-placement) deallocation "
6282 "function in C++14 (or with -fsized-deallocation)");
6284 /* But if the class has an operator delete (void *), then that is
6285 the usual deallocation function, so we shouldn't complain
6286 about using the operator delete (void *, size_t). */
6287 if (DECL_CLASS_SCOPE_P (fn))
6288 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns));
6289 iter; ++iter)
6291 tree elt = *iter;
6292 if (usual_deallocation_fn_p (elt)
6293 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
6294 goto ok;
6296 /* Before C++14 a two-parameter global deallocation function is
6297 always a placement deallocation function, but warn if
6298 -Wc++14-compat. */
6299 else if (!flag_sized_deallocation)
6301 if ((complain & tf_warning)
6302 && warning (OPT_Wc__14_compat, msg1))
6303 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6304 goto ok;
6307 if (complain & tf_warning_or_error)
6309 if (permerror (input_location, msg1))
6311 /* Only mention C++14 for namespace-scope delete. */
6312 if (DECL_NAMESPACE_SCOPE_P (fn))
6313 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6314 else
6315 inform (DECL_SOURCE_LOCATION (fn),
6316 "%qD is a usual (non-placement) deallocation "
6317 "function", fn);
6320 else
6321 return error_mark_node;
6322 ok:;
6325 else
6326 /* "Any non-placement deallocation function matches a non-placement
6327 allocation function. If the lookup finds a single matching
6328 deallocation function, that function will be called; otherwise, no
6329 deallocation function will be called." */
6330 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
6332 tree elt = *iter;
6333 if (usual_deallocation_fn_p (elt))
6335 if (!fn)
6337 fn = elt;
6338 continue;
6341 /* -- If the type has new-extended alignment, a function with a
6342 parameter of type std::align_val_t is preferred; otherwise a
6343 function without such a parameter is preferred. If exactly one
6344 preferred function is found, that function is selected and the
6345 selection process terminates. If more than one preferred
6346 function is found, all non-preferred functions are eliminated
6347 from further consideration. */
6348 if (aligned_new_threshold)
6350 bool want_align = type_has_new_extended_alignment (type);
6351 bool fn_align = aligned_deallocation_fn_p (fn);
6352 bool elt_align = aligned_deallocation_fn_p (elt);
6354 if (elt_align != fn_align)
6356 if (want_align == elt_align)
6357 fn = elt;
6358 continue;
6362 /* -- If the deallocation functions have class scope, the one
6363 without a parameter of type std::size_t is selected. */
6364 bool want_size;
6365 if (DECL_CLASS_SCOPE_P (fn))
6366 want_size = false;
6368 /* -- If the type is complete and if, for the second alternative
6369 (delete array) only, the operand is a pointer to a class type
6370 with a non-trivial destructor or a (possibly multi-dimensional)
6371 array thereof, the function with a parameter of type std::size_t
6372 is selected.
6374 -- Otherwise, it is unspecified whether a deallocation function
6375 with a parameter of type std::size_t is selected. */
6376 else
6378 want_size = COMPLETE_TYPE_P (type);
6379 if (code == VEC_DELETE_EXPR
6380 && !TYPE_VEC_NEW_USES_COOKIE (type))
6381 /* We need a cookie to determine the array size. */
6382 want_size = false;
6384 bool fn_size = second_parm_is_size_t (fn);
6385 bool elt_size = second_parm_is_size_t (elt);
6386 gcc_assert (fn_size != elt_size);
6387 if (want_size == elt_size)
6388 fn = elt;
6392 /* If we have a matching function, call it. */
6393 if (fn)
6395 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6397 /* If the FN is a member function, make sure that it is
6398 accessible. */
6399 if (BASELINK_P (fns))
6400 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
6401 complain);
6403 /* Core issue 901: It's ok to new a type with deleted delete. */
6404 if (DECL_DELETED_FN (fn) && alloc_fn)
6405 return NULL_TREE;
6407 if (placement)
6409 /* The placement args might not be suitable for overload
6410 resolution at this point, so build the call directly. */
6411 int nargs = call_expr_nargs (placement);
6412 tree *argarray = XALLOCAVEC (tree, nargs);
6413 int i;
6414 argarray[0] = addr;
6415 for (i = 1; i < nargs; i++)
6416 argarray[i] = CALL_EXPR_ARG (placement, i);
6417 if (!mark_used (fn, complain) && !(complain & tf_error))
6418 return error_mark_node;
6419 return build_cxx_call (fn, nargs, argarray, complain);
6421 else
6423 tree ret;
6424 vec<tree, va_gc> *args = make_tree_vector ();
6425 args->quick_push (addr);
6426 if (second_parm_is_size_t (fn))
6427 args->quick_push (size);
6428 if (aligned_deallocation_fn_p (fn))
6430 tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
6431 args->quick_push (al);
6433 ret = cp_build_function_call_vec (fn, &args, complain);
6434 release_tree_vector (args);
6435 return ret;
6439 /* [expr.new]
6441 If no unambiguous matching deallocation function can be found,
6442 propagating the exception does not cause the object's memory to
6443 be freed. */
6444 if (alloc_fn)
6446 if ((complain & tf_warning)
6447 && !placement)
6448 warning (0, "no corresponding deallocation function for %qD",
6449 alloc_fn);
6450 return NULL_TREE;
6453 if (complain & tf_error)
6454 error ("no suitable %<operator %s%> for %qT",
6455 OVL_OP_INFO (false, code)->name, type);
6456 return error_mark_node;
6459 /* If the current scope isn't allowed to access DECL along
6460 BASETYPE_PATH, give an error. The most derived class in
6461 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
6462 the declaration to use in the error diagnostic. */
6464 bool
6465 enforce_access (tree basetype_path, tree decl, tree diag_decl,
6466 tsubst_flags_t complain, access_failure_info *afi)
6468 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
6470 if (flag_new_inheriting_ctors
6471 && DECL_INHERITED_CTOR (decl))
6473 /* 7.3.3/18: The additional constructors are accessible if they would be
6474 accessible when used to construct an object of the corresponding base
6475 class. */
6476 decl = strip_inheriting_ctors (decl);
6477 basetype_path = lookup_base (basetype_path, DECL_CONTEXT (decl),
6478 ba_any, NULL, complain);
6481 if (!accessible_p (basetype_path, decl, true))
6483 if (complain & tf_error)
6485 if (flag_new_inheriting_ctors)
6486 diag_decl = strip_inheriting_ctors (diag_decl);
6487 if (TREE_PRIVATE (decl))
6489 error ("%q#D is private within this context", diag_decl);
6490 inform (DECL_SOURCE_LOCATION (diag_decl),
6491 "declared private here");
6492 if (afi)
6493 afi->record_access_failure (basetype_path, diag_decl);
6495 else if (TREE_PROTECTED (decl))
6497 error ("%q#D is protected within this context", diag_decl);
6498 inform (DECL_SOURCE_LOCATION (diag_decl),
6499 "declared protected here");
6500 if (afi)
6501 afi->record_access_failure (basetype_path, diag_decl);
6503 else
6505 error ("%q#D is inaccessible within this context", diag_decl);
6506 inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
6507 if (afi)
6508 afi->record_access_failure (basetype_path, diag_decl);
6511 return false;
6514 return true;
6517 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
6518 bitwise or of LOOKUP_* values. If any errors are warnings are
6519 generated, set *DIAGNOSTIC_FN to "error" or "warning",
6520 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
6521 to NULL. */
6523 static tree
6524 build_temp (tree expr, tree type, int flags,
6525 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
6527 int savew, savee;
6528 vec<tree, va_gc> *args;
6530 *diagnostic_kind = DK_UNSPECIFIED;
6532 /* If the source is a packed field, calling the copy constructor will require
6533 binding the field to the reference parameter to the copy constructor, and
6534 we'll end up with an infinite loop. If we can use a bitwise copy, then
6535 do that now. */
6536 if ((lvalue_kind (expr) & clk_packed)
6537 && CLASS_TYPE_P (TREE_TYPE (expr))
6538 && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
6539 return get_target_expr_sfinae (expr, complain);
6541 savew = warningcount + werrorcount, savee = errorcount;
6542 args = make_tree_vector_single (expr);
6543 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6544 &args, type, flags, complain);
6545 release_tree_vector (args);
6546 if (warningcount + werrorcount > savew)
6547 *diagnostic_kind = DK_WARNING;
6548 else if (errorcount > savee)
6549 *diagnostic_kind = DK_ERROR;
6550 return expr;
6553 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
6554 Also handle a subset of zero as null warnings.
6555 EXPR is implicitly converted to type TOTYPE.
6556 FN and ARGNUM are used for diagnostics. */
6558 static void
6559 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
6561 /* Issue warnings about peculiar, but valid, uses of NULL. */
6562 if (null_node_p (expr) && TREE_CODE (totype) != BOOLEAN_TYPE
6563 && ARITHMETIC_TYPE_P (totype))
6565 source_location loc =
6566 expansion_point_location_if_in_system_header (input_location);
6568 if (fn)
6569 warning_at (loc, OPT_Wconversion_null,
6570 "passing NULL to non-pointer argument %P of %qD",
6571 argnum, fn);
6572 else
6573 warning_at (loc, OPT_Wconversion_null,
6574 "converting to non-pointer type %qT from NULL", totype);
6577 /* Issue warnings if "false" is converted to a NULL pointer */
6578 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
6579 && TYPE_PTR_P (totype))
6581 if (fn)
6582 warning_at (input_location, OPT_Wconversion_null,
6583 "converting %<false%> to pointer type for argument %P "
6584 "of %qD", argnum, fn);
6585 else
6586 warning_at (input_location, OPT_Wconversion_null,
6587 "converting %<false%> to pointer type %qT", totype);
6589 /* Handle zero as null pointer warnings for cases other
6590 than EQ_EXPR and NE_EXPR */
6591 else if (null_ptr_cst_p (expr) &&
6592 (TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype)))
6594 source_location loc =
6595 expansion_point_location_if_in_system_header (input_location);
6596 maybe_warn_zero_as_null_pointer_constant (expr, loc);
6600 /* We gave a diagnostic during a conversion. If this was in the second
6601 standard conversion sequence of a user-defined conversion sequence, say
6602 which user-defined conversion. */
6604 static void
6605 maybe_print_user_conv_context (conversion *convs)
6607 if (convs->user_conv_p)
6608 for (conversion *t = convs; t; t = next_conversion (t))
6609 if (t->kind == ck_user)
6611 print_z_candidate (0, " after user-defined conversion:",
6612 t->cand);
6613 break;
6617 /* Locate the parameter with the given index within FNDECL.
6618 ARGNUM is zero based, -1 indicates the `this' argument of a method.
6619 Return the location of the FNDECL itself if there are problems. */
6621 location_t
6622 get_fndecl_argument_location (tree fndecl, int argnum)
6624 int i;
6625 tree param;
6627 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6628 for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
6629 i < argnum && param;
6630 i++, param = TREE_CHAIN (param))
6633 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6634 return the location of FNDECL. */
6635 if (param == NULL)
6636 return DECL_SOURCE_LOCATION (fndecl);
6638 return DECL_SOURCE_LOCATION (param);
6641 /* Perform the conversions in CONVS on the expression EXPR. FN and
6642 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
6643 indicates the `this' argument of a method. INNER is nonzero when
6644 being called to continue a conversion chain. It is negative when a
6645 reference binding will be applied, positive otherwise. If
6646 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
6647 conversions will be emitted if appropriate. If C_CAST_P is true,
6648 this conversion is coming from a C-style cast; in that case,
6649 conversions to inaccessible bases are permitted. */
6651 static tree
6652 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
6653 bool issue_conversion_warnings,
6654 bool c_cast_p, tsubst_flags_t complain)
6656 tree totype = convs->type;
6657 diagnostic_t diag_kind;
6658 int flags;
6659 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6661 if (convs->bad_p && !(complain & tf_error))
6662 return error_mark_node;
6664 if (convs->bad_p
6665 && convs->kind != ck_user
6666 && convs->kind != ck_list
6667 && convs->kind != ck_ambig
6668 && (convs->kind != ck_ref_bind
6669 || (convs->user_conv_p && next_conversion (convs)->bad_p))
6670 && (convs->kind != ck_rvalue
6671 || SCALAR_TYPE_P (totype))
6672 && convs->kind != ck_base)
6674 bool complained = false;
6675 conversion *t = convs;
6677 /* Give a helpful error if this is bad because of excess braces. */
6678 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6679 && SCALAR_TYPE_P (totype)
6680 && CONSTRUCTOR_NELTS (expr) > 0
6681 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
6683 complained = permerror (loc, "too many braces around initializer "
6684 "for %qT", totype);
6685 while (BRACE_ENCLOSED_INITIALIZER_P (expr)
6686 && CONSTRUCTOR_NELTS (expr) == 1)
6687 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6690 /* Give a helpful error if this is bad because a conversion to bool
6691 from std::nullptr_t requires direct-initialization. */
6692 if (NULLPTR_TYPE_P (TREE_TYPE (expr))
6693 && TREE_CODE (totype) == BOOLEAN_TYPE)
6694 complained = permerror (loc, "converting to %qH from %qI requires "
6695 "direct-initialization",
6696 totype, TREE_TYPE (expr));
6698 for (; t ; t = next_conversion (t))
6700 if (t->kind == ck_user && t->cand->reason)
6702 complained = permerror (loc, "invalid user-defined conversion "
6703 "from %qH to %qI", TREE_TYPE (expr),
6704 totype);
6705 if (complained)
6706 print_z_candidate (loc, "candidate is:", t->cand);
6707 expr = convert_like_real (t, expr, fn, argnum,
6708 /*issue_conversion_warnings=*/false,
6709 /*c_cast_p=*/false,
6710 complain);
6711 if (convs->kind == ck_ref_bind)
6712 expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
6713 LOOKUP_NORMAL, NULL_TREE,
6714 complain);
6715 else
6716 expr = cp_convert (totype, expr, complain);
6717 if (complained && fn)
6718 inform (DECL_SOURCE_LOCATION (fn),
6719 " initializing argument %P of %qD", argnum, fn);
6720 return expr;
6722 else if (t->kind == ck_user || !t->bad_p)
6724 expr = convert_like_real (t, expr, fn, argnum,
6725 /*issue_conversion_warnings=*/false,
6726 /*c_cast_p=*/false,
6727 complain);
6728 break;
6730 else if (t->kind == ck_ambig)
6731 return convert_like_real (t, expr, fn, argnum,
6732 /*issue_conversion_warnings=*/false,
6733 /*c_cast_p=*/false,
6734 complain);
6735 else if (t->kind == ck_identity)
6736 break;
6738 if (!complained)
6739 complained = permerror (loc, "invalid conversion from %qH to %qI",
6740 TREE_TYPE (expr), totype);
6741 if (complained && fn)
6742 inform (get_fndecl_argument_location (fn, argnum),
6743 " initializing argument %P of %qD", argnum, fn);
6745 return cp_convert (totype, expr, complain);
6748 if (issue_conversion_warnings && (complain & tf_warning))
6749 conversion_null_warnings (totype, expr, fn, argnum);
6751 switch (convs->kind)
6753 case ck_user:
6755 struct z_candidate *cand = convs->cand;
6757 if (cand == NULL)
6758 /* We chose the surrogate function from add_conv_candidate, now we
6759 actually need to build the conversion. */
6760 cand = build_user_type_conversion_1 (totype, expr,
6761 LOOKUP_NO_CONVERSION, complain);
6763 tree convfn = cand->fn;
6765 /* When converting from an init list we consider explicit
6766 constructors, but actually trying to call one is an error. */
6767 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
6768 && BRACE_ENCLOSED_INITIALIZER_P (expr)
6769 /* Unless this is for direct-list-initialization. */
6770 && !CONSTRUCTOR_IS_DIRECT_INIT (expr)
6771 /* And in C++98 a default constructor can't be explicit. */
6772 && cxx_dialect >= cxx11)
6774 if (!(complain & tf_error))
6775 return error_mark_node;
6776 location_t loc = location_of (expr);
6777 if (CONSTRUCTOR_NELTS (expr) == 0
6778 && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
6780 if (pedwarn (loc, 0, "converting to %qT from initializer list "
6781 "would use explicit constructor %qD",
6782 totype, convfn))
6783 inform (loc, "in C++11 and above a default constructor "
6784 "can be explicit");
6786 else
6787 error ("converting to %qT from initializer list would use "
6788 "explicit constructor %qD", totype, convfn);
6791 /* If we're initializing from {}, it's value-initialization. */
6792 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6793 && CONSTRUCTOR_NELTS (expr) == 0
6794 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
6796 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
6797 if (abstract_virtuals_error_sfinae (NULL_TREE, totype, complain))
6798 return error_mark_node;
6799 expr = build_value_init (totype, complain);
6800 expr = get_target_expr_sfinae (expr, complain);
6801 if (expr != error_mark_node)
6803 TARGET_EXPR_LIST_INIT_P (expr) = true;
6804 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
6806 return expr;
6809 expr = mark_rvalue_use (expr);
6811 /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
6812 any more UDCs. */
6813 expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
6814 complain);
6816 /* If this is a constructor or a function returning an aggr type,
6817 we need to build up a TARGET_EXPR. */
6818 if (DECL_CONSTRUCTOR_P (convfn))
6820 expr = build_cplus_new (totype, expr, complain);
6822 /* Remember that this was list-initialization. */
6823 if (convs->check_narrowing && expr != error_mark_node)
6824 TARGET_EXPR_LIST_INIT_P (expr) = true;
6827 return expr;
6829 case ck_identity:
6830 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
6832 int nelts = CONSTRUCTOR_NELTS (expr);
6833 if (nelts == 0)
6834 expr = build_value_init (totype, complain);
6835 else if (nelts == 1)
6836 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6837 else
6838 gcc_unreachable ();
6840 expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p,
6841 /*read_p=*/true, UNKNOWN_LOCATION,
6842 /*reject_builtin=*/true);
6844 if (type_unknown_p (expr))
6845 expr = instantiate_type (totype, expr, complain);
6846 if (expr == null_node
6847 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
6848 /* If __null has been converted to an integer type, we do not want to
6849 continue to warn about uses of EXPR as an integer, rather than as a
6850 pointer. */
6851 expr = build_int_cst (totype, 0);
6852 return expr;
6853 case ck_ambig:
6854 /* We leave bad_p off ck_ambig because overload resolution considers
6855 it valid, it just fails when we try to perform it. So we need to
6856 check complain here, too. */
6857 if (complain & tf_error)
6859 /* Call build_user_type_conversion again for the error. */
6860 int flags = (convs->need_temporary_p
6861 ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
6862 build_user_type_conversion (totype, convs->u.expr, flags, complain);
6863 gcc_assert (seen_error ());
6864 if (fn)
6865 inform (DECL_SOURCE_LOCATION (fn),
6866 " initializing argument %P of %qD", argnum, fn);
6868 return error_mark_node;
6870 case ck_list:
6872 /* Conversion to std::initializer_list<T>. */
6873 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
6874 tree new_ctor = build_constructor (init_list_type_node, NULL);
6875 unsigned len = CONSTRUCTOR_NELTS (expr);
6876 tree array, val, field;
6877 vec<constructor_elt, va_gc> *vec = NULL;
6878 unsigned ix;
6880 /* Convert all the elements. */
6881 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
6883 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
6884 false, false, complain);
6885 if (sub == error_mark_node)
6886 return sub;
6887 if (!BRACE_ENCLOSED_INITIALIZER_P (val)
6888 && !check_narrowing (TREE_TYPE (sub), val, complain))
6889 return error_mark_node;
6890 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
6891 if (!TREE_CONSTANT (sub))
6892 TREE_CONSTANT (new_ctor) = false;
6894 /* Build up the array. */
6895 elttype = cp_build_qualified_type
6896 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
6897 array = build_array_of_n_type (elttype, len);
6898 array = finish_compound_literal (array, new_ctor, complain);
6899 /* Take the address explicitly rather than via decay_conversion
6900 to avoid the error about taking the address of a temporary. */
6901 array = cp_build_addr_expr (array, complain);
6902 array = cp_convert (build_pointer_type (elttype), array, complain);
6903 if (array == error_mark_node)
6904 return error_mark_node;
6906 /* Build up the initializer_list object. Note: fail gracefully
6907 if the object cannot be completed because, for example, no
6908 definition is provided (c++/80956). */
6909 totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain);
6910 if (!totype)
6911 return error_mark_node;
6912 field = next_initializable_field (TYPE_FIELDS (totype));
6913 CONSTRUCTOR_APPEND_ELT (vec, field, array);
6914 field = next_initializable_field (DECL_CHAIN (field));
6915 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
6916 new_ctor = build_constructor (totype, vec);
6917 return get_target_expr_sfinae (new_ctor, complain);
6920 case ck_aggr:
6921 if (TREE_CODE (totype) == COMPLEX_TYPE)
6923 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
6924 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
6925 real = perform_implicit_conversion (TREE_TYPE (totype),
6926 real, complain);
6927 imag = perform_implicit_conversion (TREE_TYPE (totype),
6928 imag, complain);
6929 expr = build2 (COMPLEX_EXPR, totype, real, imag);
6930 return expr;
6932 expr = reshape_init (totype, expr, complain);
6933 expr = get_target_expr_sfinae (digest_init (totype, expr, complain),
6934 complain);
6935 if (expr != error_mark_node)
6936 TARGET_EXPR_LIST_INIT_P (expr) = true;
6937 return expr;
6939 default:
6940 break;
6943 expr = convert_like_real (next_conversion (convs), expr, fn, argnum,
6944 convs->kind == ck_ref_bind
6945 ? issue_conversion_warnings : false,
6946 c_cast_p, complain);
6947 if (expr == error_mark_node)
6948 return error_mark_node;
6950 switch (convs->kind)
6952 case ck_rvalue:
6953 expr = decay_conversion (expr, complain);
6954 if (expr == error_mark_node)
6956 if (complain & tf_error)
6958 maybe_print_user_conv_context (convs);
6959 if (fn)
6960 inform (DECL_SOURCE_LOCATION (fn),
6961 " initializing argument %P of %qD", argnum, fn);
6963 return error_mark_node;
6966 if (! MAYBE_CLASS_TYPE_P (totype))
6967 return expr;
6969 /* Don't introduce copies when passing arguments along to the inherited
6970 constructor. */
6971 if (current_function_decl
6972 && flag_new_inheriting_ctors
6973 && DECL_INHERITED_CTOR (current_function_decl))
6974 return expr;
6976 if (TREE_CODE (expr) == TARGET_EXPR
6977 && TARGET_EXPR_LIST_INIT_P (expr))
6978 /* Copy-list-initialization doesn't actually involve a copy. */
6979 return expr;
6981 /* Fall through. */
6982 case ck_base:
6983 if (convs->kind == ck_base && !convs->need_temporary_p)
6985 /* We are going to bind a reference directly to a base-class
6986 subobject of EXPR. */
6987 /* Build an expression for `*((base*) &expr)'. */
6988 expr = convert_to_base (expr, totype,
6989 !c_cast_p, /*nonnull=*/true, complain);
6990 return expr;
6993 /* Copy-initialization where the cv-unqualified version of the source
6994 type is the same class as, or a derived class of, the class of the
6995 destination [is treated as direct-initialization]. [dcl.init] */
6996 flags = LOOKUP_NORMAL;
6997 if (convs->user_conv_p)
6998 /* This conversion is being done in the context of a user-defined
6999 conversion (i.e. the second step of copy-initialization), so
7000 don't allow any more. */
7001 flags |= LOOKUP_NO_CONVERSION;
7002 else
7003 flags |= LOOKUP_ONLYCONVERTING;
7004 if (convs->rvaluedness_matches_p)
7005 /* standard_conversion got LOOKUP_PREFER_RVALUE. */
7006 flags |= LOOKUP_PREFER_RVALUE;
7007 expr = build_temp (expr, totype, flags, &diag_kind, complain);
7008 if (diag_kind && complain)
7010 maybe_print_user_conv_context (convs);
7011 if (fn)
7012 inform (DECL_SOURCE_LOCATION (fn),
7013 " initializing argument %P of %qD", argnum, fn);
7016 return build_cplus_new (totype, expr, complain);
7018 case ck_ref_bind:
7020 tree ref_type = totype;
7022 if (convs->bad_p && !next_conversion (convs)->bad_p)
7024 tree extype = TREE_TYPE (expr);
7025 if (TYPE_REF_IS_RVALUE (ref_type)
7026 && lvalue_p (expr))
7027 error_at (loc, "cannot bind rvalue reference of type %qH to "
7028 "lvalue of type %qI", totype, extype);
7029 else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
7030 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
7031 error_at (loc, "cannot bind non-const lvalue reference of "
7032 "type %qH to an rvalue of type %qI", totype, extype);
7033 else if (!reference_compatible_p (TREE_TYPE (totype), extype))
7034 error_at (loc, "binding reference of type %qH to %qI "
7035 "discards qualifiers", totype, extype);
7036 else
7037 gcc_unreachable ();
7038 maybe_print_user_conv_context (convs);
7039 if (fn)
7040 inform (DECL_SOURCE_LOCATION (fn),
7041 " initializing argument %P of %qD", argnum, fn);
7042 return error_mark_node;
7045 /* If necessary, create a temporary.
7047 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
7048 that need temporaries, even when their types are reference
7049 compatible with the type of reference being bound, so the
7050 upcoming call to cp_build_addr_expr doesn't fail. */
7051 if (convs->need_temporary_p
7052 || TREE_CODE (expr) == CONSTRUCTOR
7053 || TREE_CODE (expr) == VA_ARG_EXPR)
7055 /* Otherwise, a temporary of type "cv1 T1" is created and
7056 initialized from the initializer expression using the rules
7057 for a non-reference copy-initialization (8.5). */
7059 tree type = TREE_TYPE (ref_type);
7060 cp_lvalue_kind lvalue = lvalue_kind (expr);
7062 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7063 (type, next_conversion (convs)->type));
7064 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
7065 && !TYPE_REF_IS_RVALUE (ref_type))
7067 /* If the reference is volatile or non-const, we
7068 cannot create a temporary. */
7069 if (lvalue & clk_bitfield)
7070 error_at (loc, "cannot bind bitfield %qE to %qT",
7071 expr, ref_type);
7072 else if (lvalue & clk_packed)
7073 error_at (loc, "cannot bind packed field %qE to %qT",
7074 expr, ref_type);
7075 else
7076 error_at (loc, "cannot bind rvalue %qE to %qT",
7077 expr, ref_type);
7078 return error_mark_node;
7080 /* If the source is a packed field, and we must use a copy
7081 constructor, then building the target expr will require
7082 binding the field to the reference parameter to the
7083 copy constructor, and we'll end up with an infinite
7084 loop. If we can use a bitwise copy, then we'll be
7085 OK. */
7086 if ((lvalue & clk_packed)
7087 && CLASS_TYPE_P (type)
7088 && type_has_nontrivial_copy_init (type))
7090 error_at (loc, "cannot bind packed field %qE to %qT",
7091 expr, ref_type);
7092 return error_mark_node;
7094 if (lvalue & clk_bitfield)
7096 expr = convert_bitfield_to_declared_type (expr);
7097 expr = fold_convert (type, expr);
7099 expr = build_target_expr_with_type (expr, type, complain);
7102 /* Take the address of the thing to which we will bind the
7103 reference. */
7104 expr = cp_build_addr_expr (expr, complain);
7105 if (expr == error_mark_node)
7106 return error_mark_node;
7108 /* Convert it to a pointer to the type referred to by the
7109 reference. This will adjust the pointer if a derived to
7110 base conversion is being performed. */
7111 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
7112 expr, complain);
7113 /* Convert the pointer to the desired reference type. */
7114 return build_nop (ref_type, expr);
7117 case ck_lvalue:
7118 return decay_conversion (expr, complain);
7120 case ck_fnptr:
7121 /* ??? Should the address of a transaction-safe pointer point to the TM
7122 clone, and this conversion look up the primary function? */
7123 return build_nop (totype, expr);
7125 case ck_qual:
7126 /* Warn about deprecated conversion if appropriate. */
7127 string_conv_p (totype, expr, 1);
7128 break;
7130 case ck_ptr:
7131 if (convs->base_p)
7132 expr = convert_to_base (expr, totype, !c_cast_p,
7133 /*nonnull=*/false, complain);
7134 return build_nop (totype, expr);
7136 case ck_pmem:
7137 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
7138 c_cast_p, complain);
7140 default:
7141 break;
7144 if (convs->check_narrowing
7145 && !check_narrowing (totype, expr, complain))
7146 return error_mark_node;
7148 warning_sentinel w (warn_zero_as_null_pointer_constant);
7149 if (issue_conversion_warnings)
7150 expr = cp_convert_and_check (totype, expr, complain);
7151 else
7152 expr = cp_convert (totype, expr, complain);
7154 return expr;
7157 /* ARG is being passed to a varargs function. Perform any conversions
7158 required. Return the converted value. */
7160 tree
7161 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
7163 tree arg_type;
7164 location_t loc = cp_expr_loc_or_loc (arg, input_location);
7166 /* [expr.call]
7168 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7169 standard conversions are performed. */
7170 arg = decay_conversion (arg, complain);
7171 arg_type = TREE_TYPE (arg);
7172 /* [expr.call]
7174 If the argument has integral or enumeration type that is subject
7175 to the integral promotions (_conv.prom_), or a floating point
7176 type that is subject to the floating point promotion
7177 (_conv.fpprom_), the value of the argument is converted to the
7178 promoted type before the call. */
7179 if (TREE_CODE (arg_type) == REAL_TYPE
7180 && (TYPE_PRECISION (arg_type)
7181 < TYPE_PRECISION (double_type_node))
7182 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
7184 if ((complain & tf_warning)
7185 && warn_double_promotion && !c_inhibit_evaluation_warnings)
7186 warning_at (loc, OPT_Wdouble_promotion,
7187 "implicit conversion from %qH to %qI when passing "
7188 "argument to function",
7189 arg_type, double_type_node);
7190 arg = convert_to_real_nofold (double_type_node, arg);
7192 else if (NULLPTR_TYPE_P (arg_type))
7193 arg = null_pointer_node;
7194 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
7196 if (SCOPED_ENUM_P (arg_type))
7198 tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
7199 complain);
7200 prom = cp_perform_integral_promotions (prom, complain);
7201 if (abi_version_crosses (6)
7202 && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
7203 && (complain & tf_warning))
7204 warning_at (loc, OPT_Wabi, "scoped enum %qT passed through ... as "
7205 "%qT before -fabi-version=6, %qT after", arg_type,
7206 TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
7207 if (!abi_version_at_least (6))
7208 arg = prom;
7210 else
7211 arg = cp_perform_integral_promotions (arg, complain);
7214 arg = require_complete_type_sfinae (arg, complain);
7215 arg_type = TREE_TYPE (arg);
7217 if (arg != error_mark_node
7218 /* In a template (or ill-formed code), we can have an incomplete type
7219 even after require_complete_type_sfinae, in which case we don't know
7220 whether it has trivial copy or not. */
7221 && COMPLETE_TYPE_P (arg_type)
7222 && !cp_unevaluated_operand)
7224 /* [expr.call] 5.2.2/7:
7225 Passing a potentially-evaluated argument of class type (Clause 9)
7226 with a non-trivial copy constructor or a non-trivial destructor
7227 with no corresponding parameter is conditionally-supported, with
7228 implementation-defined semantics.
7230 We support it as pass-by-invisible-reference, just like a normal
7231 value parameter.
7233 If the call appears in the context of a sizeof expression,
7234 it is not potentially-evaluated. */
7235 if (type_has_nontrivial_copy_init (arg_type)
7236 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
7238 arg = force_rvalue (arg, complain);
7239 if (complain & tf_warning)
7240 warning (OPT_Wconditionally_supported,
7241 "passing objects of non-trivially-copyable "
7242 "type %q#T through %<...%> is conditionally supported",
7243 arg_type);
7244 return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
7246 /* Build up a real lvalue-to-rvalue conversion in case the
7247 copy constructor is trivial but not callable. */
7248 else if (CLASS_TYPE_P (arg_type))
7249 force_rvalue (arg, complain);
7253 return arg;
7256 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
7258 tree
7259 build_x_va_arg (source_location loc, tree expr, tree type)
7261 if (processing_template_decl)
7263 tree r = build_min (VA_ARG_EXPR, type, expr);
7264 SET_EXPR_LOCATION (r, loc);
7265 return r;
7268 type = complete_type_or_else (type, NULL_TREE);
7270 if (expr == error_mark_node || !type)
7271 return error_mark_node;
7273 expr = mark_lvalue_use (expr);
7275 if (TYPE_REF_P (type))
7277 error ("cannot receive reference type %qT through %<...%>", type);
7278 return error_mark_node;
7281 if (type_has_nontrivial_copy_init (type)
7282 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7284 /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
7285 it as pass by invisible reference. */
7286 warning_at (loc, OPT_Wconditionally_supported,
7287 "receiving objects of non-trivially-copyable type %q#T "
7288 "through %<...%> is conditionally-supported", type);
7290 tree ref = cp_build_reference_type (type, false);
7291 expr = build_va_arg (loc, expr, ref);
7292 return convert_from_reference (expr);
7295 tree ret = build_va_arg (loc, expr, type);
7296 if (CLASS_TYPE_P (type))
7297 /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
7298 know how to handle it. */
7299 ret = get_target_expr (ret);
7300 return ret;
7303 /* TYPE has been given to va_arg. Apply the default conversions which
7304 would have happened when passed via ellipsis. Return the promoted
7305 type, or the passed type if there is no change. */
7307 tree
7308 cxx_type_promotes_to (tree type)
7310 tree promote;
7312 /* Perform the array-to-pointer and function-to-pointer
7313 conversions. */
7314 type = type_decays_to (type);
7316 promote = type_promotes_to (type);
7317 if (same_type_p (type, promote))
7318 promote = type;
7320 return promote;
7323 /* ARG is a default argument expression being passed to a parameter of
7324 the indicated TYPE, which is a parameter to FN. PARMNUM is the
7325 zero-based argument number. Do any required conversions. Return
7326 the converted value. */
7328 static GTY(()) vec<tree, va_gc> *default_arg_context;
7329 void
7330 push_defarg_context (tree fn)
7331 { vec_safe_push (default_arg_context, fn); }
7333 void
7334 pop_defarg_context (void)
7335 { default_arg_context->pop (); }
7337 tree
7338 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
7339 tsubst_flags_t complain)
7341 int i;
7342 tree t;
7344 /* See through clones. */
7345 fn = DECL_ORIGIN (fn);
7346 /* And inheriting ctors. */
7347 if (flag_new_inheriting_ctors)
7348 fn = strip_inheriting_ctors (fn);
7350 /* Detect recursion. */
7351 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
7352 if (t == fn)
7354 if (complain & tf_error)
7355 error ("recursive evaluation of default argument for %q#D", fn);
7356 return error_mark_node;
7359 /* If the ARG is an unparsed default argument expression, the
7360 conversion cannot be performed. */
7361 if (TREE_CODE (arg) == DEFAULT_ARG)
7363 if (complain & tf_error)
7364 error ("call to %qD uses the default argument for parameter %P, which "
7365 "is not yet defined", fn, parmnum);
7366 return error_mark_node;
7369 push_defarg_context (fn);
7371 if (fn && DECL_TEMPLATE_INFO (fn))
7372 arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
7374 /* Due to:
7376 [dcl.fct.default]
7378 The names in the expression are bound, and the semantic
7379 constraints are checked, at the point where the default
7380 expressions appears.
7382 we must not perform access checks here. */
7383 push_deferring_access_checks (dk_no_check);
7384 /* We must make a copy of ARG, in case subsequent processing
7385 alters any part of it. */
7386 arg = break_out_target_exprs (arg, /*clear location*/true);
7388 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
7389 ICR_DEFAULT_ARGUMENT, fn, parmnum,
7390 complain);
7391 arg = convert_for_arg_passing (type, arg, complain);
7392 pop_deferring_access_checks();
7394 pop_defarg_context ();
7396 return arg;
7399 /* Returns the type which will really be used for passing an argument of
7400 type TYPE. */
7402 tree
7403 type_passed_as (tree type)
7405 /* Pass classes with copy ctors by invisible reference. */
7406 if (TREE_ADDRESSABLE (type))
7408 type = build_reference_type (type);
7409 /* There are no other pointers to this temporary. */
7410 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
7412 else if (targetm.calls.promote_prototypes (NULL_TREE)
7413 && INTEGRAL_TYPE_P (type)
7414 && COMPLETE_TYPE_P (type)
7415 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
7416 type = integer_type_node;
7418 return type;
7421 /* Actually perform the appropriate conversion. */
7423 tree
7424 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
7426 tree bitfield_type;
7428 /* If VAL is a bitfield, then -- since it has already been converted
7429 to TYPE -- it cannot have a precision greater than TYPE.
7431 If it has a smaller precision, we must widen it here. For
7432 example, passing "int f:3;" to a function expecting an "int" will
7433 not result in any conversion before this point.
7435 If the precision is the same we must not risk widening. For
7436 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
7437 often have type "int", even though the C++ type for the field is
7438 "long long". If the value is being passed to a function
7439 expecting an "int", then no conversions will be required. But,
7440 if we call convert_bitfield_to_declared_type, the bitfield will
7441 be converted to "long long". */
7442 bitfield_type = is_bitfield_expr_with_lowered_type (val);
7443 if (bitfield_type
7444 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
7445 val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
7447 if (val == error_mark_node)
7449 /* Pass classes with copy ctors by invisible reference. */
7450 else if (TREE_ADDRESSABLE (type))
7451 val = build1 (ADDR_EXPR, build_reference_type (type), val);
7452 else if (targetm.calls.promote_prototypes (NULL_TREE)
7453 && INTEGRAL_TYPE_P (type)
7454 && COMPLETE_TYPE_P (type)
7455 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
7456 val = cp_perform_integral_promotions (val, complain);
7457 if (complain & tf_warning)
7459 if (warn_suggest_attribute_format)
7461 tree rhstype = TREE_TYPE (val);
7462 const enum tree_code coder = TREE_CODE (rhstype);
7463 const enum tree_code codel = TREE_CODE (type);
7464 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7465 && coder == codel
7466 && check_missing_format_attribute (type, rhstype))
7467 warning (OPT_Wsuggest_attribute_format,
7468 "argument of function call might be a candidate "
7469 "for a format attribute");
7471 maybe_warn_parm_abi (type, cp_expr_loc_or_loc (val, input_location));
7473 return val;
7476 /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
7477 which just decay_conversion or no conversions at all should be done.
7478 This is true for some builtins which don't act like normal functions.
7479 Return 2 if no conversions at all should be done, 1 if just
7480 decay_conversion. Return 3 for special treatment of the 3rd argument
7481 for __builtin_*_overflow_p. */
7484 magic_varargs_p (tree fn)
7486 if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
7487 switch (DECL_FUNCTION_CODE (fn))
7489 case BUILT_IN_CLASSIFY_TYPE:
7490 case BUILT_IN_CONSTANT_P:
7491 case BUILT_IN_NEXT_ARG:
7492 case BUILT_IN_VA_START:
7493 return 1;
7495 case BUILT_IN_ADD_OVERFLOW_P:
7496 case BUILT_IN_SUB_OVERFLOW_P:
7497 case BUILT_IN_MUL_OVERFLOW_P:
7498 return 3;
7500 default:;
7501 return lookup_attribute ("type generic",
7502 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
7505 return 0;
7508 /* Returns the decl of the dispatcher function if FN is a function version. */
7510 tree
7511 get_function_version_dispatcher (tree fn)
7513 tree dispatcher_decl = NULL;
7515 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7516 && DECL_FUNCTION_VERSIONED (fn));
7518 gcc_assert (targetm.get_function_versions_dispatcher);
7519 dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
7521 if (dispatcher_decl == NULL)
7523 error_at (input_location, "use of multiversioned function "
7524 "without a default");
7525 return NULL;
7528 retrofit_lang_decl (dispatcher_decl);
7529 gcc_assert (dispatcher_decl != NULL);
7530 return dispatcher_decl;
7533 /* fn is a function version dispatcher that is marked used. Mark all the
7534 semantically identical function versions it will dispatch as used. */
7536 void
7537 mark_versions_used (tree fn)
7539 struct cgraph_node *node;
7540 struct cgraph_function_version_info *node_v;
7541 struct cgraph_function_version_info *it_v;
7543 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7545 node = cgraph_node::get (fn);
7546 if (node == NULL)
7547 return;
7549 gcc_assert (node->dispatcher_function);
7551 node_v = node->function_version ();
7552 if (node_v == NULL)
7553 return;
7555 /* All semantically identical versions are chained. Traverse and mark each
7556 one of them as used. */
7557 it_v = node_v->next;
7558 while (it_v != NULL)
7560 mark_used (it_v->this_node->decl);
7561 it_v = it_v->next;
7565 /* Build a call to "the copy constructor" for the type of A, even if it
7566 wouldn't be selected by normal overload resolution. Used for
7567 diagnostics. */
7569 static tree
7570 call_copy_ctor (tree a, tsubst_flags_t complain)
7572 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
7573 tree binfo = TYPE_BINFO (ctype);
7574 tree copy = get_copy_ctor (ctype, complain);
7575 copy = build_baselink (binfo, binfo, copy, NULL_TREE);
7576 tree ob = build_dummy_object (ctype);
7577 vec<tree, va_gc>* args = make_tree_vector_single (a);
7578 tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
7579 LOOKUP_NORMAL, NULL, complain);
7580 release_tree_vector (args);
7581 return r;
7584 /* Return true iff T refers to a base field. */
7586 static bool
7587 is_base_field_ref (tree t)
7589 STRIP_NOPS (t);
7590 if (TREE_CODE (t) == ADDR_EXPR)
7591 t = TREE_OPERAND (t, 0);
7592 if (TREE_CODE (t) == COMPONENT_REF)
7593 t = TREE_OPERAND (t, 1);
7594 if (TREE_CODE (t) == FIELD_DECL)
7595 return DECL_FIELD_IS_BASE (t);
7596 return false;
7599 /* We can't elide a copy from a function returning by value to a base
7600 subobject, as the callee might clobber tail padding. Return true iff this
7601 could be that case. */
7603 static bool
7604 unsafe_copy_elision_p (tree target, tree exp)
7606 /* Copy elision only happens with a TARGET_EXPR. */
7607 if (TREE_CODE (exp) != TARGET_EXPR)
7608 return false;
7609 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7610 /* It's safe to elide the copy for a class with no tail padding. */
7611 if (tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
7612 return false;
7613 /* It's safe to elide the copy if we aren't initializing a base object. */
7614 if (!is_base_field_ref (target))
7615 return false;
7616 tree init = TARGET_EXPR_INITIAL (exp);
7617 /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
7618 while (TREE_CODE (init) == COMPOUND_EXPR)
7619 init = TREE_OPERAND (init, 1);
7620 if (TREE_CODE (init) == COND_EXPR)
7622 /* We'll end up copying from each of the arms of the COND_EXPR directly
7623 into the target, so look at them. */
7624 if (tree op = TREE_OPERAND (init, 1))
7625 if (unsafe_copy_elision_p (target, op))
7626 return true;
7627 return unsafe_copy_elision_p (target, TREE_OPERAND (init, 2));
7629 return (TREE_CODE (init) == AGGR_INIT_EXPR
7630 && !AGGR_INIT_VIA_CTOR_P (init));
7633 /* True iff C is a conversion that binds a reference to a prvalue. */
7635 static bool
7636 conv_binds_ref_to_prvalue (conversion *c)
7638 if (c->kind != ck_ref_bind)
7639 return false;
7640 if (c->need_temporary_p)
7641 return true;
7643 c = next_conversion (c);
7645 if (c->kind == ck_rvalue)
7646 return true;
7647 if (c->kind == ck_user && !TYPE_REF_P (c->type))
7648 return true;
7649 if (c->kind == ck_identity && c->u.expr
7650 && TREE_CODE (c->u.expr) == TARGET_EXPR)
7651 return true;
7653 return false;
7656 /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
7657 class type or a pointer to class type. */
7659 tree
7660 build_trivial_dtor_call (tree instance)
7662 gcc_assert (!is_dummy_object (instance));
7664 if (!flag_lifetime_dse)
7666 no_clobber:
7667 return fold_convert (void_type_node, instance);
7670 if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
7672 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7673 goto no_clobber;
7674 instance = cp_build_fold_indirect_ref (instance);
7677 /* A trivial destructor should still clobber the object. */
7678 tree clobber = build_clobber (TREE_TYPE (instance));
7679 return build2 (MODIFY_EXPR, void_type_node,
7680 instance, clobber);
7683 /* Subroutine of the various build_*_call functions. Overload resolution
7684 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
7685 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
7686 bitmask of various LOOKUP_* flags which apply to the call itself. */
7688 static tree
7689 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
7691 tree fn = cand->fn;
7692 const vec<tree, va_gc> *args = cand->args;
7693 tree first_arg = cand->first_arg;
7694 conversion **convs = cand->convs;
7695 conversion *conv;
7696 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
7697 int parmlen;
7698 tree val;
7699 int i = 0;
7700 int j = 0;
7701 unsigned int arg_index = 0;
7702 int is_method = 0;
7703 int nargs;
7704 tree *argarray;
7705 bool already_used = false;
7707 /* In a template, there is no need to perform all of the work that
7708 is normally done. We are only interested in the type of the call
7709 expression, i.e., the return type of the function. Any semantic
7710 errors will be deferred until the template is instantiated. */
7711 if (processing_template_decl)
7713 tree expr, addr;
7714 tree return_type;
7715 const tree *argarray;
7716 unsigned int nargs;
7718 if (undeduced_auto_decl (fn))
7719 mark_used (fn, complain);
7720 else
7721 /* Otherwise set TREE_USED for the benefit of -Wunused-function.
7722 See PR80598. */
7723 TREE_USED (fn) = 1;
7725 return_type = TREE_TYPE (TREE_TYPE (fn));
7726 nargs = vec_safe_length (args);
7727 if (first_arg == NULL_TREE)
7728 argarray = args->address ();
7729 else
7731 tree *alcarray;
7732 unsigned int ix;
7733 tree arg;
7735 ++nargs;
7736 alcarray = XALLOCAVEC (tree, nargs);
7737 alcarray[0] = build_this (first_arg);
7738 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
7739 alcarray[ix + 1] = arg;
7740 argarray = alcarray;
7743 addr = build_addr_func (fn, complain);
7744 if (addr == error_mark_node)
7745 return error_mark_node;
7746 expr = build_call_array_loc (input_location, return_type,
7747 addr, nargs, argarray);
7748 if (TREE_THIS_VOLATILE (fn) && cfun)
7749 current_function_returns_abnormally = 1;
7750 return convert_from_reference (expr);
7753 /* Give any warnings we noticed during overload resolution. */
7754 if (cand->warnings && (complain & tf_warning))
7756 struct candidate_warning *w;
7757 for (w = cand->warnings; w; w = w->next)
7758 joust (cand, w->loser, 1, complain);
7761 /* Core issue 2327: P0135 doesn't say how to handle the case where the
7762 argument to the copy constructor ends up being a prvalue after
7763 conversion. Let's do the normal processing, but pretend we aren't
7764 actually using the copy constructor. */
7765 bool force_elide = false;
7766 if (cxx_dialect >= cxx17
7767 && cand->num_convs == 1
7768 && DECL_COMPLETE_CONSTRUCTOR_P (fn)
7769 && (DECL_COPY_CONSTRUCTOR_P (fn)
7770 || DECL_MOVE_CONSTRUCTOR_P (fn))
7771 && conv_binds_ref_to_prvalue (convs[0]))
7773 force_elide = true;
7774 goto not_really_used;
7777 /* OK, we're actually calling this inherited constructor; set its deletedness
7778 appropriately. We can get away with doing this here because calling is
7779 the only way to refer to a constructor. */
7780 if (DECL_INHERITED_CTOR (fn))
7781 deduce_inheriting_ctor (fn);
7783 /* Make =delete work with SFINAE. */
7784 if (DECL_DELETED_FN (fn))
7786 if (complain & tf_error)
7787 mark_used (fn);
7788 return error_mark_node;
7791 if (DECL_FUNCTION_MEMBER_P (fn))
7793 tree access_fn;
7794 /* If FN is a template function, two cases must be considered.
7795 For example:
7797 struct A {
7798 protected:
7799 template <class T> void f();
7801 template <class T> struct B {
7802 protected:
7803 void g();
7805 struct C : A, B<int> {
7806 using A::f; // #1
7807 using B<int>::g; // #2
7810 In case #1 where `A::f' is a member template, DECL_ACCESS is
7811 recorded in the primary template but not in its specialization.
7812 We check access of FN using its primary template.
7814 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
7815 because it is a member of class template B, DECL_ACCESS is
7816 recorded in the specialization `B<int>::g'. We cannot use its
7817 primary template because `B<T>::g' and `B<int>::g' may have
7818 different access. */
7819 if (DECL_TEMPLATE_INFO (fn)
7820 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
7821 access_fn = DECL_TI_TEMPLATE (fn);
7822 else
7823 access_fn = fn;
7824 if (!perform_or_defer_access_check (cand->access_path, access_fn,
7825 fn, complain))
7826 return error_mark_node;
7829 /* If we're checking for implicit delete, don't bother with argument
7830 conversions. */
7831 if (flags & LOOKUP_SPECULATIVE)
7833 if (cand->viable == 1)
7834 return fn;
7835 else if (!(complain & tf_error))
7836 /* Reject bad conversions now. */
7837 return error_mark_node;
7838 /* else continue to get conversion error. */
7841 not_really_used:
7843 /* N3276 magic doesn't apply to nested calls. */
7844 tsubst_flags_t decltype_flag = (complain & tf_decltype);
7845 complain &= ~tf_decltype;
7846 /* No-Cleanup doesn't apply to nested calls either. */
7847 tsubst_flags_t no_cleanup_complain = complain;
7848 complain &= ~tf_no_cleanup;
7850 /* Find maximum size of vector to hold converted arguments. */
7851 parmlen = list_length (parm);
7852 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
7853 if (parmlen > nargs)
7854 nargs = parmlen;
7855 argarray = XALLOCAVEC (tree, nargs);
7857 /* The implicit parameters to a constructor are not considered by overload
7858 resolution, and must be of the proper type. */
7859 if (DECL_CONSTRUCTOR_P (fn))
7861 tree object_arg;
7862 if (first_arg != NULL_TREE)
7864 object_arg = first_arg;
7865 first_arg = NULL_TREE;
7867 else
7869 object_arg = (*args)[arg_index];
7870 ++arg_index;
7872 argarray[j++] = build_this (object_arg);
7873 parm = TREE_CHAIN (parm);
7874 /* We should never try to call the abstract constructor. */
7875 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
7877 if (DECL_HAS_VTT_PARM_P (fn))
7879 argarray[j++] = (*args)[arg_index];
7880 ++arg_index;
7881 parm = TREE_CHAIN (parm);
7884 if (flags & LOOKUP_PREFER_RVALUE)
7886 /* The implicit move specified in 15.8.3/3 fails "...if the type of
7887 the first parameter of the selected constructor is not an rvalue
7888 reference to the object’s type (possibly cv-qualified)...." */
7889 gcc_assert (!(complain & tf_error));
7890 tree ptype = convs[0]->type;
7891 if (!TYPE_REF_P (ptype)
7892 || !TYPE_REF_IS_RVALUE (ptype)
7893 || CONVERSION_RANK (convs[0]) > cr_exact)
7894 return error_mark_node;
7897 /* Bypass access control for 'this' parameter. */
7898 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7900 tree parmtype = TREE_VALUE (parm);
7901 tree arg = build_this (first_arg != NULL_TREE
7902 ? first_arg
7903 : (*args)[arg_index]);
7904 tree argtype = TREE_TYPE (arg);
7905 tree converted_arg;
7906 tree base_binfo;
7908 if (arg == error_mark_node)
7909 return error_mark_node;
7911 if (convs[i]->bad_p)
7913 if (complain & tf_error)
7915 if (permerror (input_location, "passing %qT as %<this%> "
7916 "argument discards qualifiers",
7917 TREE_TYPE (argtype)))
7918 inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
7920 else
7921 return error_mark_node;
7924 /* See if the function member or the whole class type is declared
7925 final and the call can be devirtualized. */
7926 if (DECL_FINAL_P (fn)
7927 || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
7928 flags |= LOOKUP_NONVIRTUAL;
7930 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
7931 X is called for an object that is not of type X, or of a type
7932 derived from X, the behavior is undefined.
7934 So we can assume that anything passed as 'this' is non-null, and
7935 optimize accordingly. */
7936 gcc_assert (TYPE_PTR_P (parmtype));
7937 /* Convert to the base in which the function was declared. */
7938 gcc_assert (cand->conversion_path != NULL_TREE);
7939 converted_arg = build_base_path (PLUS_EXPR,
7940 arg,
7941 cand->conversion_path,
7942 1, complain);
7943 /* Check that the base class is accessible. */
7944 if (!accessible_base_p (TREE_TYPE (argtype),
7945 BINFO_TYPE (cand->conversion_path), true))
7947 if (complain & tf_error)
7948 error ("%qT is not an accessible base of %qT",
7949 BINFO_TYPE (cand->conversion_path),
7950 TREE_TYPE (argtype));
7951 else
7952 return error_mark_node;
7954 /* If fn was found by a using declaration, the conversion path
7955 will be to the derived class, not the base declaring fn. We
7956 must convert from derived to base. */
7957 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
7958 TREE_TYPE (parmtype), ba_unique,
7959 NULL, complain);
7960 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
7961 base_binfo, 1, complain);
7963 argarray[j++] = converted_arg;
7964 parm = TREE_CHAIN (parm);
7965 if (first_arg != NULL_TREE)
7966 first_arg = NULL_TREE;
7967 else
7968 ++arg_index;
7969 ++i;
7970 is_method = 1;
7973 gcc_assert (first_arg == NULL_TREE);
7974 for (; arg_index < vec_safe_length (args) && parm;
7975 parm = TREE_CHAIN (parm), ++arg_index, ++i)
7977 tree type = TREE_VALUE (parm);
7978 tree arg = (*args)[arg_index];
7979 bool conversion_warning = true;
7981 conv = convs[i];
7983 /* If the argument is NULL and used to (implicitly) instantiate a
7984 template function (and bind one of the template arguments to
7985 the type of 'long int'), we don't want to warn about passing NULL
7986 to non-pointer argument.
7987 For example, if we have this template function:
7989 template<typename T> void func(T x) {}
7991 we want to warn (when -Wconversion is enabled) in this case:
7993 void foo() {
7994 func<int>(NULL);
7997 but not in this case:
7999 void foo() {
8000 func(NULL);
8003 if (null_node_p (arg)
8004 && DECL_TEMPLATE_INFO (fn)
8005 && cand->template_decl
8006 && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
8007 conversion_warning = false;
8009 /* Warn about initializer_list deduction that isn't currently in the
8010 working draft. */
8011 if (cxx_dialect > cxx98
8012 && flag_deduce_init_list
8013 && cand->template_decl
8014 && is_std_init_list (non_reference (type))
8015 && BRACE_ENCLOSED_INITIALIZER_P (arg))
8017 tree tmpl = TI_TEMPLATE (cand->template_decl);
8018 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
8019 tree patparm = get_pattern_parm (realparm, tmpl);
8020 tree pattype = TREE_TYPE (patparm);
8021 if (PACK_EXPANSION_P (pattype))
8022 pattype = PACK_EXPANSION_PATTERN (pattype);
8023 pattype = non_reference (pattype);
8025 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
8026 && (cand->explicit_targs == NULL_TREE
8027 || (TREE_VEC_LENGTH (cand->explicit_targs)
8028 <= TEMPLATE_TYPE_IDX (pattype))))
8030 pedwarn (input_location, 0, "deducing %qT as %qT",
8031 non_reference (TREE_TYPE (patparm)),
8032 non_reference (type));
8033 pedwarn (DECL_SOURCE_LOCATION (cand->fn), 0,
8034 " in call to %qD", cand->fn);
8035 pedwarn (input_location, 0,
8036 " (you can disable this with -fno-deduce-init-list)");
8040 /* Set user_conv_p on the argument conversions, so rvalue/base handling
8041 knows not to allow any more UDCs. This needs to happen after we
8042 process cand->warnings. */
8043 if (flags & LOOKUP_NO_CONVERSION)
8044 conv->user_conv_p = true;
8046 tsubst_flags_t arg_complain = complain;
8047 if (!conversion_warning)
8048 arg_complain &= ~tf_warning;
8050 val = convert_like_with_context (conv, arg, fn, i - is_method,
8051 arg_complain);
8052 val = convert_for_arg_passing (type, val, arg_complain);
8054 if (val == error_mark_node)
8055 return error_mark_node;
8056 else
8057 argarray[j++] = val;
8060 /* Default arguments */
8061 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
8063 if (TREE_VALUE (parm) == error_mark_node)
8064 return error_mark_node;
8065 val = convert_default_arg (TREE_VALUE (parm),
8066 TREE_PURPOSE (parm),
8067 fn, i - is_method,
8068 complain);
8069 if (val == error_mark_node)
8070 return error_mark_node;
8071 argarray[j++] = val;
8074 /* Ellipsis */
8075 int magic = magic_varargs_p (fn);
8076 for (; arg_index < vec_safe_length (args); ++arg_index)
8078 tree a = (*args)[arg_index];
8079 if ((magic == 3 && arg_index == 2) || magic == 2)
8081 /* Do no conversions for certain magic varargs. */
8082 a = mark_type_use (a);
8083 if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
8084 return error_mark_node;
8086 else if (magic != 0)
8087 /* For other magic varargs only do decay_conversion. */
8088 a = decay_conversion (a, complain);
8089 else if (DECL_CONSTRUCTOR_P (fn)
8090 && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
8091 TREE_TYPE (a)))
8093 /* Avoid infinite recursion trying to call A(...). */
8094 if (complain & tf_error)
8095 /* Try to call the actual copy constructor for a good error. */
8096 call_copy_ctor (a, complain);
8097 return error_mark_node;
8099 else
8100 a = convert_arg_to_ellipsis (a, complain);
8101 if (a == error_mark_node)
8102 return error_mark_node;
8103 argarray[j++] = a;
8106 gcc_assert (j <= nargs);
8107 nargs = j;
8109 /* Avoid to do argument-transformation, if warnings for format, and for
8110 nonnull are disabled. Just in case that at least one of them is active
8111 the check_function_arguments function might warn about something. */
8113 bool warned_p = false;
8114 if (warn_nonnull
8115 || warn_format
8116 || warn_suggest_attribute_format
8117 || warn_restrict)
8119 tree *fargs = (!nargs ? argarray
8120 : (tree *) alloca (nargs * sizeof (tree)));
8121 for (j = 0; j < nargs; j++)
8123 /* For -Wformat undo the implicit passing by hidden reference
8124 done by convert_arg_to_ellipsis. */
8125 if (TREE_CODE (argarray[j]) == ADDR_EXPR
8126 && TYPE_REF_P (TREE_TYPE (argarray[j])))
8127 fargs[j] = TREE_OPERAND (argarray[j], 0);
8128 else
8129 fargs[j] = maybe_constant_value (argarray[j]);
8132 warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
8133 nargs, fargs, NULL);
8136 if (DECL_INHERITED_CTOR (fn))
8138 /* Check for passing ellipsis arguments to an inherited constructor. We
8139 could handle this by open-coding the inherited constructor rather than
8140 defining it, but let's not bother now. */
8141 if (!cp_unevaluated_operand
8142 && cand->num_convs
8143 && cand->convs[cand->num_convs-1]->ellipsis_p)
8145 if (complain & tf_error)
8147 sorry ("passing arguments to ellipsis of inherited constructor "
8148 "%qD", cand->fn);
8149 inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
8151 return error_mark_node;
8154 /* A base constructor inheriting from a virtual base doesn't get the
8155 inherited arguments, just this and __vtt. */
8156 if (ctor_omit_inherited_parms (fn))
8157 nargs = 2;
8160 /* Avoid actually calling copy constructors and copy assignment operators,
8161 if possible. */
8163 if (! flag_elide_constructors && !force_elide)
8164 /* Do things the hard way. */;
8165 else if (cand->num_convs == 1
8166 && (DECL_COPY_CONSTRUCTOR_P (fn)
8167 || DECL_MOVE_CONSTRUCTOR_P (fn))
8168 /* It's unsafe to elide the constructor when handling
8169 a noexcept-expression, it may evaluate to the wrong
8170 value (c++/53025). */
8171 && (force_elide || cp_noexcept_operand == 0))
8173 tree targ;
8174 tree arg = argarray[num_artificial_parms_for (fn)];
8175 tree fa;
8176 bool trivial = trivial_fn_p (fn);
8178 /* Pull out the real argument, disregarding const-correctness. */
8179 targ = arg;
8180 /* Strip the reference binding for the constructor parameter. */
8181 if (CONVERT_EXPR_P (targ)
8182 && TYPE_REF_P (TREE_TYPE (targ)))
8183 targ = TREE_OPERAND (targ, 0);
8184 /* But don't strip any other reference bindings; binding a temporary to a
8185 reference prevents copy elision. */
8186 while ((CONVERT_EXPR_P (targ)
8187 && !TYPE_REF_P (TREE_TYPE (targ)))
8188 || TREE_CODE (targ) == NON_LVALUE_EXPR)
8189 targ = TREE_OPERAND (targ, 0);
8190 if (TREE_CODE (targ) == ADDR_EXPR)
8192 targ = TREE_OPERAND (targ, 0);
8193 if (!same_type_ignoring_top_level_qualifiers_p
8194 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
8195 targ = NULL_TREE;
8197 else
8198 targ = NULL_TREE;
8200 if (targ)
8201 arg = targ;
8202 else
8203 arg = cp_build_fold_indirect_ref (arg);
8205 /* In C++17 we shouldn't be copying a TARGET_EXPR except into a base
8206 subobject. */
8207 if (CHECKING_P && cxx_dialect >= cxx17)
8208 gcc_assert (TREE_CODE (arg) != TARGET_EXPR
8209 || force_elide
8210 /* It's from binding the ref parm to a packed field. */
8211 || convs[0]->need_temporary_p
8212 || seen_error ()
8213 /* See unsafe_copy_elision_p. */
8214 || DECL_BASE_CONSTRUCTOR_P (fn));
8216 fa = argarray[0];
8217 bool unsafe = unsafe_copy_elision_p (fa, arg);
8218 bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe);
8220 /* [class.copy]: the copy constructor is implicitly defined even if the
8221 implementation elided its use. But don't warn about deprecation when
8222 eliding a temporary, as then no copy is actually performed. */
8223 warning_sentinel s (warn_deprecated_copy, eliding_temp);
8224 if (force_elide)
8225 /* The language says this isn't called. */;
8226 else if (!trivial)
8228 if (!mark_used (fn, complain) && !(complain & tf_error))
8229 return error_mark_node;
8230 already_used = true;
8232 else
8233 cp_warn_deprecated_use (fn, complain);
8235 /* If we're creating a temp and we already have one, don't create a
8236 new one. If we're not creating a temp but we get one, use
8237 INIT_EXPR to collapse the temp into our target. Otherwise, if the
8238 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
8239 temp or an INIT_EXPR otherwise. */
8240 if (is_dummy_object (fa))
8242 if (TREE_CODE (arg) == TARGET_EXPR)
8243 return arg;
8244 else if (trivial)
8245 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
8247 else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
8248 && !unsafe)
8250 tree to = cp_stabilize_reference (cp_build_fold_indirect_ref (fa));
8252 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
8253 return val;
8256 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
8257 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
8258 && trivial_fn_p (fn))
8260 tree to = cp_stabilize_reference
8261 (cp_build_fold_indirect_ref (argarray[0]));
8262 tree type = TREE_TYPE (to);
8263 tree as_base = CLASSTYPE_AS_BASE (type);
8264 tree arg = argarray[1];
8265 location_t loc = cp_expr_loc_or_loc (arg, input_location);
8267 if (is_really_empty_class (type))
8269 /* Avoid copying empty classes. */
8270 val = build2 (COMPOUND_EXPR, type, arg, to);
8271 TREE_NO_WARNING (val) = 1;
8273 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
8275 if (is_std_init_list (type)
8276 && conv_binds_ref_to_prvalue (convs[1]))
8277 warning_at (loc, OPT_Winit_list_lifetime,
8278 "assignment from temporary initializer_list does not "
8279 "extend the lifetime of the underlying array");
8280 arg = cp_build_fold_indirect_ref (arg);
8281 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
8283 else
8285 /* We must only copy the non-tail padding parts. */
8286 tree arg0, arg2, t;
8287 tree array_type, alias_set;
8289 arg2 = TYPE_SIZE_UNIT (as_base);
8290 arg0 = cp_build_addr_expr (to, complain);
8292 array_type = build_array_type (unsigned_char_type_node,
8293 build_index_type
8294 (size_binop (MINUS_EXPR,
8295 arg2, size_int (1))));
8296 alias_set = build_int_cst (build_pointer_type (type), 0);
8297 t = build2 (MODIFY_EXPR, void_type_node,
8298 build2 (MEM_REF, array_type, arg0, alias_set),
8299 build2 (MEM_REF, array_type, arg, alias_set));
8300 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
8301 TREE_NO_WARNING (val) = 1;
8304 cp_warn_deprecated_use (fn, complain);
8306 return val;
8308 else if (trivial_fn_p (fn))
8310 if (DECL_DESTRUCTOR_P (fn))
8311 return build_trivial_dtor_call (argarray[0]);
8312 else if (default_ctor_p (fn))
8314 if (is_dummy_object (argarray[0]))
8315 return force_target_expr (DECL_CONTEXT (fn), void_node,
8316 no_cleanup_complain);
8317 else
8318 return cp_build_fold_indirect_ref (argarray[0]);
8322 gcc_assert (!force_elide);
8324 if (!already_used
8325 && !mark_used (fn, complain))
8326 return error_mark_node;
8328 /* Warn if the built-in writes to an object of a non-trivial type. */
8329 if (warn_class_memaccess
8330 && vec_safe_length (args) >= 2
8331 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8332 maybe_warn_class_memaccess (input_location, fn, args);
8334 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
8335 /* Don't mess with virtual lookup in instantiate_non_dependent_expr;
8336 virtual functions can't be constexpr. */
8337 && !in_template_function ())
8339 tree t;
8340 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
8341 DECL_CONTEXT (fn),
8342 ba_any, NULL, complain);
8343 gcc_assert (binfo && binfo != error_mark_node);
8345 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
8346 complain);
8347 if (TREE_SIDE_EFFECTS (argarray[0]))
8348 argarray[0] = save_expr (argarray[0]);
8349 t = build_pointer_type (TREE_TYPE (fn));
8350 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
8351 TREE_TYPE (fn) = t;
8353 else
8355 fn = build_addr_func (fn, complain);
8356 if (fn == error_mark_node)
8357 return error_mark_node;
8360 tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
8361 if (call == error_mark_node)
8362 return call;
8363 if (cand->flags & LOOKUP_LIST_INIT_CTOR)
8365 tree c = extract_call_expr (call);
8366 /* build_new_op_1 will clear this when appropriate. */
8367 CALL_EXPR_ORDERED_ARGS (c) = true;
8369 if (warned_p)
8371 tree c = extract_call_expr (call);
8372 if (TREE_CODE (c) == CALL_EXPR)
8373 TREE_NO_WARNING (c) = 1;
8375 return call;
8378 namespace
8381 /* Return the DECL of the first non-static subobject of class TYPE
8382 that satisfies the predicate PRED or null if none can be found. */
8384 template <class Predicate>
8385 tree
8386 first_non_static_field (tree type, Predicate pred)
8388 if (!type || !CLASS_TYPE_P (type))
8389 return NULL_TREE;
8391 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8393 if (TREE_CODE (field) != FIELD_DECL)
8394 continue;
8395 if (TREE_STATIC (field))
8396 continue;
8397 if (pred (field))
8398 return field;
8401 int i = 0;
8403 for (tree base_binfo, binfo = TYPE_BINFO (type);
8404 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8406 tree base = TREE_TYPE (base_binfo);
8407 if (pred (base))
8408 return base;
8409 if (tree field = first_non_static_field (base, pred))
8410 return field;
8413 return NULL_TREE;
8416 struct NonPublicField
8418 bool operator() (const_tree t)
8420 return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t));
8424 /* Return the DECL of the first non-public subobject of class TYPE
8425 or null if none can be found. */
8427 static inline tree
8428 first_non_public_field (tree type)
8430 return first_non_static_field (type, NonPublicField ());
8433 struct NonTrivialField
8435 bool operator() (const_tree t)
8437 return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t);
8441 /* Return the DECL of the first non-trivial subobject of class TYPE
8442 or null if none can be found. */
8444 static inline tree
8445 first_non_trivial_field (tree type)
8447 return first_non_static_field (type, NonTrivialField ());
8450 } /* unnamed namespace */
8452 /* Return true if all copy and move assignment operator overloads for
8453 class TYPE are trivial and at least one of them is not deleted and,
8454 when ACCESS is set, accessible. Return false otherwise. Set
8455 HASASSIGN to true when the TYPE has a (not necessarily trivial)
8456 copy or move assignment. */
8458 static bool
8459 has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
8461 tree fns = get_class_binding (type, assign_op_identifier);
8462 bool all_trivial = true;
8464 /* Iterate over overloads of the assignment operator, checking
8465 accessible copy assignments for triviality. */
8467 for (ovl_iterator oi (fns); oi; ++oi)
8469 tree f = *oi;
8471 /* Skip operators that aren't copy assignments. */
8472 if (!copy_fn_p (f))
8473 continue;
8475 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
8476 || accessible_p (TYPE_BINFO (type), f, true));
8478 /* Skip template assignment operators and deleted functions. */
8479 if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
8480 continue;
8482 if (accessible)
8483 *hasassign = true;
8485 if (!accessible || !trivial_fn_p (f))
8486 all_trivial = false;
8488 /* Break early when both properties have been determined. */
8489 if (*hasassign && !all_trivial)
8490 break;
8493 /* Return true if they're all trivial and one of the expressions
8494 TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
8495 tree ref = cp_build_reference_type (type, false);
8496 return (all_trivial
8497 && (is_trivially_xible (MODIFY_EXPR, type, type)
8498 || is_trivially_xible (MODIFY_EXPR, type, ref)));
8501 /* Return true if all copy and move ctor overloads for class TYPE are
8502 trivial and at least one of them is not deleted and, when ACCESS is
8503 set, accessible. Return false otherwise. Set each element of HASCTOR[]
8504 to true when the TYPE has a (not necessarily trivial) default and copy
8505 (or move) ctor, respectively. */
8507 static bool
8508 has_trivial_copy_p (tree type, bool access, bool hasctor[2])
8510 tree fns = get_class_binding (type, complete_ctor_identifier);
8511 bool all_trivial = true;
8513 for (ovl_iterator oi (fns); oi; ++oi)
8515 tree f = *oi;
8517 /* Skip template constructors. */
8518 if (TREE_CODE (f) != FUNCTION_DECL)
8519 continue;
8521 bool cpy_or_move_ctor_p = copy_fn_p (f);
8523 /* Skip ctors other than default, copy, and move. */
8524 if (!cpy_or_move_ctor_p && !default_ctor_p (f))
8525 continue;
8527 if (DECL_DELETED_FN (f))
8528 continue;
8530 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
8531 || accessible_p (TYPE_BINFO (type), f, true));
8533 if (accessible)
8534 hasctor[cpy_or_move_ctor_p] = true;
8536 if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
8537 all_trivial = false;
8539 /* Break early when both properties have been determined. */
8540 if (hasctor[0] && hasctor[1] && !all_trivial)
8541 break;
8544 return all_trivial;
8547 /* Issue a warning on a call to the built-in function FNDECL if it is
8548 a raw memory write whose destination is not an object of (something
8549 like) trivial or standard layout type with a non-deleted assignment
8550 and copy ctor. Detects const correctness violations, corrupting
8551 references, virtual table pointers, and bypassing non-trivial
8552 assignments. */
8554 static void
8555 maybe_warn_class_memaccess (location_t loc, tree fndecl,
8556 const vec<tree, va_gc> *args)
8558 /* Except for bcopy where it's second, the destination pointer is
8559 the first argument for all functions handled here. Compute
8560 the index of the destination and source arguments. */
8561 unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
8562 unsigned srcidx = !dstidx;
8564 tree dest = (*args)[dstidx];
8565 if (!TREE_TYPE (dest) || !INDIRECT_TYPE_P (TREE_TYPE (dest)))
8566 return;
8568 tree srctype = NULL_TREE;
8570 /* Determine the type of the pointed-to object and whether it's
8571 a complete class type. */
8572 tree desttype = TREE_TYPE (TREE_TYPE (dest));
8574 if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
8575 return;
8577 /* Check to see if the raw memory call is made by a non-static member
8578 function with THIS as the destination argument for the destination
8579 type. If so, and if the class has no non-trivial bases or members,
8580 be more permissive. */
8581 if (current_function_decl
8582 && DECL_NONSTATIC_MEMBER_FUNCTION_P (current_function_decl)
8583 && is_this_parameter (tree_strip_nop_conversions (dest)))
8585 tree ctx = DECL_CONTEXT (current_function_decl);
8586 bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
8587 tree binfo = TYPE_BINFO (ctx);
8589 /* FIXME: The following if statement is overly permissive (see
8590 bug 84851). Remove it in GCC 9. */
8591 if (special
8592 && !BINFO_VTABLE (binfo)
8593 && !BINFO_N_BASE_BINFOS (binfo)
8594 && (DECL_CONSTRUCTOR_P (current_function_decl)
8595 || DECL_DESTRUCTOR_P (current_function_decl)))
8596 return;
8598 if (special
8599 && !BINFO_VTABLE (binfo)
8600 && !first_non_trivial_field (desttype))
8601 return;
8604 /* True if the class is trivial. */
8605 bool trivial = trivial_type_p (desttype);
8607 /* Set to true if DESTYPE has an accessible copy assignment. */
8608 bool hasassign = false;
8609 /* True if all of the class' overloaded copy assignment operators
8610 are all trivial (and not deleted) and at least one of them is
8611 accessible. */
8612 bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
8614 /* Set to true if DESTTYPE has an accessible default and copy ctor,
8615 respectively. */
8616 bool hasctors[2] = { false, false };
8618 /* True if all of the class' overloaded copy constructors are all
8619 trivial (and not deleted) and at least one of them is accessible. */
8620 bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
8622 /* Set FLD to the first private/protected member of the class. */
8623 tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
8625 /* The warning format string. */
8626 const char *warnfmt = NULL;
8627 /* A suggested alternative to offer instead of the raw memory call.
8628 Empty string when none can be come up with. */
8629 const char *suggest = "";
8630 bool warned = false;
8632 switch (DECL_FUNCTION_CODE (fndecl))
8634 case BUILT_IN_MEMSET:
8635 if (!integer_zerop (maybe_constant_value ((*args)[1])))
8637 /* Diagnose setting non-copy-assignable or non-trivial types,
8638 or types with a private member, to (potentially) non-zero
8639 bytes. Since the value of the bytes being written is unknown,
8640 suggest using assignment instead (if one exists). Also warn
8641 for writes into objects for which zero-initialization doesn't
8642 mean all bits clear (pointer-to-member data, where null is all
8643 bits set). Since the value being written is (most likely)
8644 non-zero, simply suggest assignment (but not copy assignment). */
8645 suggest = "; use assignment instead";
8646 if (!trivassign)
8647 warnfmt = G_("%qD writing to an object of type %#qT with "
8648 "no trivial copy-assignment");
8649 else if (!trivial)
8650 warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
8651 else if (fld)
8653 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
8654 warned = warning_at (loc, OPT_Wclass_memaccess,
8655 "%qD writing to an object of type %#qT with "
8656 "%qs member %qD",
8657 fndecl, desttype, access, fld);
8659 else if (!zero_init_p (desttype))
8660 warnfmt = G_("%qD writing to an object of type %#qT containing "
8661 "a pointer to data member%s");
8663 break;
8665 /* Fall through. */
8667 case BUILT_IN_BZERO:
8668 /* Similarly to the above, diagnose clearing non-trivial or non-
8669 standard layout objects, or objects of types with no assignmenmt.
8670 Since the value being written is known to be zero, suggest either
8671 copy assignment, copy ctor, or default ctor as an alternative,
8672 depending on what's available. */
8674 if (hasassign && hasctors[0])
8675 suggest = G_("; use assignment or value-initialization instead");
8676 else if (hasassign)
8677 suggest = G_("; use assignment instead");
8678 else if (hasctors[0])
8679 suggest = G_("; use value-initialization instead");
8681 if (!trivassign)
8682 warnfmt = G_("%qD clearing an object of type %#qT with "
8683 "no trivial copy-assignment%s");
8684 else if (!trivial)
8685 warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
8686 else if (!zero_init_p (desttype))
8687 warnfmt = G_("%qD clearing an object of type %#qT containing "
8688 "a pointer-to-member%s");
8689 break;
8691 case BUILT_IN_BCOPY:
8692 case BUILT_IN_MEMCPY:
8693 case BUILT_IN_MEMMOVE:
8694 case BUILT_IN_MEMPCPY:
8695 /* Determine the type of the source object. */
8696 srctype = TREE_TYPE ((*args)[srcidx]);
8697 if (!srctype || !INDIRECT_TYPE_P (srctype))
8698 srctype = void_type_node;
8699 else
8700 srctype = TREE_TYPE (srctype);
8702 /* Since it's impossible to determine wheter the byte copy is
8703 being used in place of assignment to an existing object or
8704 as a substitute for initialization, assume it's the former.
8705 Determine the best alternative to use instead depending on
8706 what's not deleted. */
8707 if (hasassign && hasctors[1])
8708 suggest = G_("; use copy-assignment or copy-initialization instead");
8709 else if (hasassign)
8710 suggest = G_("; use copy-assignment instead");
8711 else if (hasctors[1])
8712 suggest = G_("; use copy-initialization instead");
8714 if (!trivassign)
8715 warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
8716 "copy-assignment%s");
8717 else if (!trivially_copyable_p (desttype))
8718 warnfmt = G_("%qD writing to an object of non-trivially copyable "
8719 "type %#qT%s");
8720 else if (!trivcopy)
8721 warnfmt = G_("%qD writing to an object with a deleted copy constructor");
8723 else if (!trivial
8724 && !VOID_TYPE_P (srctype)
8725 && !char_type_p (TYPE_MAIN_VARIANT (srctype))
8726 && !same_type_ignoring_top_level_qualifiers_p (desttype,
8727 srctype))
8729 /* Warn when copying into a non-trivial object from an object
8730 of a different type other than void or char. */
8731 warned = warning_at (loc, OPT_Wclass_memaccess,
8732 "%qD copying an object of non-trivial type "
8733 "%#qT from an array of %#qT",
8734 fndecl, desttype, srctype);
8736 else if (fld
8737 && !VOID_TYPE_P (srctype)
8738 && !char_type_p (TYPE_MAIN_VARIANT (srctype))
8739 && !same_type_ignoring_top_level_qualifiers_p (desttype,
8740 srctype))
8742 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
8743 warned = warning_at (loc, OPT_Wclass_memaccess,
8744 "%qD copying an object of type %#qT with "
8745 "%qs member %qD from an array of %#qT; use "
8746 "assignment or copy-initialization instead",
8747 fndecl, desttype, access, fld, srctype);
8749 else if (!trivial && vec_safe_length (args) > 2)
8751 tree sz = maybe_constant_value ((*args)[2]);
8752 if (!tree_fits_uhwi_p (sz))
8753 break;
8755 /* Finally, warn on partial copies. */
8756 unsigned HOST_WIDE_INT typesize
8757 = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
8758 if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
8759 warned = warning_at (loc, OPT_Wclass_memaccess,
8760 (typesize - partial > 1
8761 ? G_("%qD writing to an object of "
8762 "a non-trivial type %#qT leaves %wu "
8763 "bytes unchanged")
8764 : G_("%qD writing to an object of "
8765 "a non-trivial type %#qT leaves %wu "
8766 "byte unchanged")),
8767 fndecl, desttype, typesize - partial);
8769 break;
8771 case BUILT_IN_REALLOC:
8773 if (!trivially_copyable_p (desttype))
8774 warnfmt = G_("%qD moving an object of non-trivially copyable type "
8775 "%#qT; use %<new%> and %<delete%> instead");
8776 else if (!trivcopy)
8777 warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
8778 "constructor; use %<new%> and %<delete%> instead");
8779 else if (!get_dtor (desttype, tf_none))
8780 warnfmt = G_("%qD moving an object of type %#qT with deleted "
8781 "destructor");
8782 else if (!trivial)
8784 tree sz = maybe_constant_value ((*args)[1]);
8785 if (TREE_CODE (sz) == INTEGER_CST
8786 && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype)))
8787 /* Finally, warn on reallocation into insufficient space. */
8788 warned = warning_at (loc, OPT_Wclass_memaccess,
8789 "%qD moving an object of non-trivial type "
8790 "%#qT and size %E into a region of size %E",
8791 fndecl, desttype, TYPE_SIZE_UNIT (desttype),
8792 sz);
8794 break;
8796 default:
8797 return;
8800 if (warnfmt)
8802 if (suggest)
8803 warned = warning_at (loc, OPT_Wclass_memaccess,
8804 warnfmt, fndecl, desttype, suggest);
8805 else
8806 warned = warning_at (loc, OPT_Wclass_memaccess,
8807 warnfmt, fndecl, desttype);
8810 if (warned)
8811 inform (location_of (desttype), "%#qT declared here", desttype);
8814 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
8815 This function performs no overload resolution, conversion, or other
8816 high-level operations. */
8818 tree
8819 build_cxx_call (tree fn, int nargs, tree *argarray,
8820 tsubst_flags_t complain)
8822 tree fndecl;
8824 /* Remember roughly where this call is. */
8825 location_t loc = cp_expr_loc_or_loc (fn, input_location);
8826 fn = build_call_a (fn, nargs, argarray);
8827 SET_EXPR_LOCATION (fn, loc);
8829 fndecl = get_callee_fndecl (fn);
8831 /* Check that arguments to builtin functions match the expectations. */
8832 if (fndecl
8833 && !processing_template_decl
8834 && DECL_BUILT_IN (fndecl)
8835 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
8837 int i;
8839 /* We need to take care that values to BUILT_IN_NORMAL
8840 are reduced. */
8841 for (i = 0; i < nargs; i++)
8842 argarray[i] = maybe_constant_value (argarray[i]);
8844 if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
8845 nargs, argarray))
8846 return error_mark_node;
8849 if (VOID_TYPE_P (TREE_TYPE (fn)))
8850 return fn;
8852 /* 5.2.2/11: If a function call is a prvalue of object type: if the
8853 function call is either the operand of a decltype-specifier or the
8854 right operand of a comma operator that is the operand of a
8855 decltype-specifier, a temporary object is not introduced for the
8856 prvalue. The type of the prvalue may be incomplete. */
8857 if (!(complain & tf_decltype))
8859 fn = require_complete_type_sfinae (fn, complain);
8860 if (fn == error_mark_node)
8861 return error_mark_node;
8863 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
8865 fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
8866 maybe_warn_parm_abi (TREE_TYPE (fn), loc);
8869 return convert_from_reference (fn);
8872 /* Returns the value to use for the in-charge parameter when making a
8873 call to a function with the indicated NAME.
8875 FIXME:Can't we find a neater way to do this mapping? */
8877 tree
8878 in_charge_arg_for_name (tree name)
8880 if (IDENTIFIER_CTOR_P (name))
8882 if (name == complete_ctor_identifier)
8883 return integer_one_node;
8884 gcc_checking_assert (name == base_ctor_identifier);
8886 else
8888 if (name == complete_dtor_identifier)
8889 return integer_two_node;
8890 else if (name == deleting_dtor_identifier)
8891 return integer_three_node;
8892 gcc_checking_assert (name == base_dtor_identifier);
8895 return integer_zero_node;
8898 /* We've built up a constructor call RET. Complain if it delegates to the
8899 constructor we're currently compiling. */
8901 static void
8902 check_self_delegation (tree ret)
8904 if (TREE_CODE (ret) == TARGET_EXPR)
8905 ret = TARGET_EXPR_INITIAL (ret);
8906 tree fn = cp_get_callee_fndecl_nofold (ret);
8907 if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
8908 error ("constructor delegates to itself");
8911 /* Build a call to a constructor, destructor, or an assignment
8912 operator for INSTANCE, an expression with class type. NAME
8913 indicates the special member function to call; *ARGS are the
8914 arguments. ARGS may be NULL. This may change ARGS. BINFO
8915 indicates the base of INSTANCE that is to be passed as the `this'
8916 parameter to the member function called.
8918 FLAGS are the LOOKUP_* flags to use when processing the call.
8920 If NAME indicates a complete object constructor, INSTANCE may be
8921 NULL_TREE. In this case, the caller will call build_cplus_new to
8922 store the newly constructed object into a VAR_DECL. */
8924 tree
8925 build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
8926 tree binfo, int flags, tsubst_flags_t complain)
8928 tree fns;
8929 /* The type of the subobject to be constructed or destroyed. */
8930 tree class_type;
8931 vec<tree, va_gc> *allocated = NULL;
8932 tree ret;
8934 gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
8936 if (error_operand_p (instance))
8937 return error_mark_node;
8939 if (IDENTIFIER_DTOR_P (name))
8941 gcc_assert (args == NULL || vec_safe_is_empty (*args));
8942 if (!type_build_dtor_call (TREE_TYPE (instance)))
8943 /* Shortcut to avoid lazy destructor declaration. */
8944 return build_trivial_dtor_call (instance);
8947 if (TYPE_P (binfo))
8949 /* Resolve the name. */
8950 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
8951 return error_mark_node;
8953 binfo = TYPE_BINFO (binfo);
8956 gcc_assert (binfo != NULL_TREE);
8958 class_type = BINFO_TYPE (binfo);
8960 /* Handle the special case where INSTANCE is NULL_TREE. */
8961 if (name == complete_ctor_identifier && !instance)
8962 instance = build_dummy_object (class_type);
8963 else
8965 /* Convert to the base class, if necessary. */
8966 if (!same_type_ignoring_top_level_qualifiers_p
8967 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
8969 if (IDENTIFIER_CDTOR_P (name))
8970 /* For constructors and destructors, either the base is
8971 non-virtual, or it is virtual but we are doing the
8972 conversion from a constructor or destructor for the
8973 complete object. In either case, we can convert
8974 statically. */
8975 instance = convert_to_base_statically (instance, binfo);
8976 else
8978 /* However, for assignment operators, we must convert
8979 dynamically if the base is virtual. */
8980 gcc_checking_assert (name == assign_op_identifier);
8981 instance = build_base_path (PLUS_EXPR, instance,
8982 binfo, /*nonnull=*/1, complain);
8987 gcc_assert (instance != NULL_TREE);
8989 /* In C++17, "If the initializer expression is a prvalue and the
8990 cv-unqualified version of the source type is the same class as the class
8991 of the destination, the initializer expression is used to initialize the
8992 destination object." Handle that here to avoid doing overload
8993 resolution. */
8994 if (cxx_dialect >= cxx17
8995 && args && vec_safe_length (*args) == 1
8996 && name == complete_ctor_identifier)
8998 tree arg = (**args)[0];
9000 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
9001 && !TYPE_HAS_LIST_CTOR (class_type)
9002 && CONSTRUCTOR_NELTS (arg) == 1)
9003 arg = CONSTRUCTOR_ELT (arg, 0)->value;
9005 if ((TREE_CODE (arg) == TARGET_EXPR
9006 || TREE_CODE (arg) == CONSTRUCTOR)
9007 && (same_type_ignoring_top_level_qualifiers_p
9008 (class_type, TREE_TYPE (arg))))
9010 if (is_dummy_object (instance))
9011 return arg;
9012 else if (TREE_CODE (arg) == TARGET_EXPR)
9013 TARGET_EXPR_DIRECT_INIT_P (arg) = true;
9015 if ((complain & tf_error)
9016 && (flags & LOOKUP_DELEGATING_CONS))
9017 check_self_delegation (arg);
9018 /* Avoid change of behavior on Wunused-var-2.C. */
9019 instance = mark_lvalue_use (instance);
9020 return build2 (INIT_EXPR, class_type, instance, arg);
9024 fns = lookup_fnfields (binfo, name, 1);
9026 /* When making a call to a constructor or destructor for a subobject
9027 that uses virtual base classes, pass down a pointer to a VTT for
9028 the subobject. */
9029 if ((name == base_ctor_identifier
9030 || name == base_dtor_identifier)
9031 && CLASSTYPE_VBASECLASSES (class_type))
9033 tree vtt;
9034 tree sub_vtt;
9036 /* If the current function is a complete object constructor
9037 or destructor, then we fetch the VTT directly.
9038 Otherwise, we look it up using the VTT we were given. */
9039 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
9040 vtt = decay_conversion (vtt, complain);
9041 if (vtt == error_mark_node)
9042 return error_mark_node;
9043 vtt = build_if_in_charge (vtt, current_vtt_parm);
9044 if (BINFO_SUBVTT_INDEX (binfo))
9045 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
9046 else
9047 sub_vtt = vtt;
9049 if (args == NULL)
9051 allocated = make_tree_vector ();
9052 args = &allocated;
9055 vec_safe_insert (*args, 0, sub_vtt);
9058 ret = build_new_method_call (instance, fns, args,
9059 TYPE_BINFO (BINFO_TYPE (binfo)),
9060 flags, /*fn=*/NULL,
9061 complain);
9063 if (allocated != NULL)
9064 release_tree_vector (allocated);
9066 if ((complain & tf_error)
9067 && (flags & LOOKUP_DELEGATING_CONS)
9068 && name == complete_ctor_identifier)
9069 check_self_delegation (ret);
9071 return ret;
9074 /* Return the NAME, as a C string. The NAME indicates a function that
9075 is a member of TYPE. *FREE_P is set to true if the caller must
9076 free the memory returned.
9078 Rather than go through all of this, we should simply set the names
9079 of constructors and destructors appropriately, and dispense with
9080 ctor_identifier, dtor_identifier, etc. */
9082 static char *
9083 name_as_c_string (tree name, tree type, bool *free_p)
9085 const char *pretty_name;
9087 /* Assume that we will not allocate memory. */
9088 *free_p = false;
9089 /* Constructors and destructors are special. */
9090 if (IDENTIFIER_CDTOR_P (name))
9092 pretty_name
9093 = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
9094 /* For a destructor, add the '~'. */
9095 if (IDENTIFIER_DTOR_P (name))
9097 pretty_name = concat ("~", pretty_name, NULL);
9098 /* Remember that we need to free the memory allocated. */
9099 *free_p = true;
9102 else if (IDENTIFIER_CONV_OP_P (name))
9104 pretty_name = concat ("operator ",
9105 type_as_string_translate (TREE_TYPE (name),
9106 TFF_PLAIN_IDENTIFIER),
9107 NULL);
9108 /* Remember that we need to free the memory allocated. */
9109 *free_p = true;
9111 else
9112 pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
9114 return CONST_CAST (char *, pretty_name);
9117 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
9118 be set, upon return, to the function called. ARGS may be NULL.
9119 This may change ARGS. */
9121 static tree
9122 build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
9123 tree conversion_path, int flags,
9124 tree *fn_p, tsubst_flags_t complain)
9126 struct z_candidate *candidates = 0, *cand;
9127 tree explicit_targs = NULL_TREE;
9128 tree basetype = NULL_TREE;
9129 tree access_binfo, binfo;
9130 tree optype;
9131 tree first_mem_arg = NULL_TREE;
9132 tree name;
9133 bool skip_first_for_error;
9134 vec<tree, va_gc> *user_args;
9135 tree call;
9136 tree fn;
9137 int template_only = 0;
9138 bool any_viable_p;
9139 tree orig_instance;
9140 tree orig_fns;
9141 vec<tree, va_gc> *orig_args = NULL;
9142 void *p;
9144 gcc_assert (instance != NULL_TREE);
9146 /* We don't know what function we're going to call, yet. */
9147 if (fn_p)
9148 *fn_p = NULL_TREE;
9150 if (error_operand_p (instance)
9151 || !fns || error_operand_p (fns))
9152 return error_mark_node;
9154 if (!BASELINK_P (fns))
9156 if (complain & tf_error)
9157 error ("call to non-function %qD", fns);
9158 return error_mark_node;
9161 orig_instance = instance;
9162 orig_fns = fns;
9164 /* Dismantle the baselink to collect all the information we need. */
9165 if (!conversion_path)
9166 conversion_path = BASELINK_BINFO (fns);
9167 access_binfo = BASELINK_ACCESS_BINFO (fns);
9168 binfo = BASELINK_BINFO (fns);
9169 optype = BASELINK_OPTYPE (fns);
9170 fns = BASELINK_FUNCTIONS (fns);
9171 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
9173 explicit_targs = TREE_OPERAND (fns, 1);
9174 fns = TREE_OPERAND (fns, 0);
9175 template_only = 1;
9177 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
9178 || TREE_CODE (fns) == TEMPLATE_DECL
9179 || TREE_CODE (fns) == OVERLOAD);
9180 fn = OVL_FIRST (fns);
9181 name = DECL_NAME (fn);
9183 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
9184 gcc_assert (CLASS_TYPE_P (basetype));
9186 user_args = args == NULL ? NULL : *args;
9187 /* Under DR 147 A::A() is an invalid constructor call,
9188 not a functional cast. */
9189 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
9191 if (! (complain & tf_error))
9192 return error_mark_node;
9194 basetype = DECL_CONTEXT (fn);
9195 name = constructor_name (basetype);
9196 if (permerror (input_location,
9197 "cannot call constructor %<%T::%D%> directly",
9198 basetype, name))
9199 inform (input_location, "for a function-style cast, remove the "
9200 "redundant %<::%D%>", name);
9201 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
9202 complain);
9203 return call;
9206 if (processing_template_decl)
9208 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
9209 instance = build_non_dependent_expr (instance);
9210 if (args != NULL)
9211 make_args_non_dependent (*args);
9214 /* Process the argument list. */
9215 if (args != NULL && *args != NULL)
9217 *args = resolve_args (*args, complain);
9218 if (*args == NULL)
9219 return error_mark_node;
9220 user_args = *args;
9223 /* Consider the object argument to be used even if we end up selecting a
9224 static member function. */
9225 instance = mark_type_use (instance);
9227 /* Figure out whether to skip the first argument for the error
9228 message we will display to users if an error occurs. We don't
9229 want to display any compiler-generated arguments. The "this"
9230 pointer hasn't been added yet. However, we must remove the VTT
9231 pointer if this is a call to a base-class constructor or
9232 destructor. */
9233 skip_first_for_error = false;
9234 if (IDENTIFIER_CDTOR_P (name))
9236 /* Callers should explicitly indicate whether they want to ctor
9237 the complete object or just the part without virtual bases. */
9238 gcc_assert (name != ctor_identifier);
9240 /* Remove the VTT pointer, if present. */
9241 if ((name == base_ctor_identifier || name == base_dtor_identifier)
9242 && CLASSTYPE_VBASECLASSES (basetype))
9243 skip_first_for_error = true;
9245 /* It's OK to call destructors and constructors on cv-qualified
9246 objects. Therefore, convert the INSTANCE to the unqualified
9247 type, if necessary. */
9248 if (!same_type_p (basetype, TREE_TYPE (instance)))
9250 instance = build_this (instance);
9251 instance = build_nop (build_pointer_type (basetype), instance);
9252 instance = build_fold_indirect_ref (instance);
9255 else
9256 gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
9258 /* For the overload resolution we need to find the actual `this`
9259 that would be captured if the call turns out to be to a
9260 non-static member function. Do not actually capture it at this
9261 point. */
9262 if (DECL_CONSTRUCTOR_P (fn))
9263 /* Constructors don't use the enclosing 'this'. */
9264 first_mem_arg = instance;
9265 else
9266 first_mem_arg = maybe_resolve_dummy (instance, false);
9268 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9269 p = conversion_obstack_alloc (0);
9271 /* The number of arguments artificial parms in ARGS; we subtract one because
9272 there's no 'this' in ARGS. */
9273 unsigned skip = num_artificial_parms_for (fn) - 1;
9275 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
9276 initializer, not T({ }). */
9277 if (DECL_CONSTRUCTOR_P (fn)
9278 && vec_safe_length (user_args) > skip
9279 && DIRECT_LIST_INIT_P ((*user_args)[skip]))
9281 tree init_list = (*user_args)[skip];
9282 tree init = NULL_TREE;
9284 gcc_assert (user_args->length () == skip + 1
9285 && !(flags & LOOKUP_ONLYCONVERTING));
9287 /* If the initializer list has no elements and T is a class type with
9288 a default constructor, the object is value-initialized. Handle
9289 this here so we don't need to handle it wherever we use
9290 build_special_member_call. */
9291 if (CONSTRUCTOR_NELTS (init_list) == 0
9292 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
9293 /* For a user-provided default constructor, use the normal
9294 mechanisms so that protected access works. */
9295 && type_has_non_user_provided_default_constructor (basetype)
9296 && !processing_template_decl)
9297 init = build_value_init (basetype, complain);
9299 /* If BASETYPE is an aggregate, we need to do aggregate
9300 initialization. */
9301 else if (CP_AGGREGATE_TYPE_P (basetype))
9303 init = reshape_init (basetype, init_list, complain);
9304 init = digest_init (basetype, init, complain);
9307 if (init)
9309 if (is_dummy_object (instance))
9310 return get_target_expr_sfinae (init, complain);
9311 init = build2 (INIT_EXPR, TREE_TYPE (instance), instance, init);
9312 TREE_SIDE_EFFECTS (init) = true;
9313 return init;
9316 /* Otherwise go ahead with overload resolution. */
9317 add_list_candidates (fns, first_mem_arg, user_args,
9318 basetype, explicit_targs, template_only,
9319 conversion_path, access_binfo, flags,
9320 &candidates, complain);
9322 else
9323 add_candidates (fns, first_mem_arg, user_args, optype,
9324 explicit_targs, template_only, conversion_path,
9325 access_binfo, flags, &candidates, complain);
9327 any_viable_p = false;
9328 candidates = splice_viable (candidates, false, &any_viable_p);
9330 if (!any_viable_p)
9332 if (complain & tf_error)
9334 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
9335 cxx_incomplete_type_error (instance, basetype);
9336 else if (optype)
9337 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
9338 basetype, optype, build_tree_list_vec (user_args),
9339 TREE_TYPE (instance));
9340 else
9342 tree arglist = build_tree_list_vec (user_args);
9343 tree errname = name;
9344 bool twiddle = false;
9345 if (IDENTIFIER_CDTOR_P (errname))
9347 twiddle = IDENTIFIER_DTOR_P (errname);
9348 errname = constructor_name (basetype);
9350 if (explicit_targs)
9351 errname = lookup_template_function (errname, explicit_targs);
9352 if (skip_first_for_error)
9353 arglist = TREE_CHAIN (arglist);
9354 error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
9355 basetype, &"~"[!twiddle], errname, arglist,
9356 TREE_TYPE (instance));
9358 print_z_candidates (location_of (name), candidates);
9360 call = error_mark_node;
9362 else
9364 cand = tourney (candidates, complain);
9365 if (cand == 0)
9367 char *pretty_name;
9368 bool free_p;
9369 tree arglist;
9371 if (complain & tf_error)
9373 pretty_name = name_as_c_string (name, basetype, &free_p);
9374 arglist = build_tree_list_vec (user_args);
9375 if (skip_first_for_error)
9376 arglist = TREE_CHAIN (arglist);
9377 if (!any_strictly_viable (candidates))
9378 error ("no matching function for call to %<%s(%A)%>",
9379 pretty_name, arglist);
9380 else
9381 error ("call of overloaded %<%s(%A)%> is ambiguous",
9382 pretty_name, arglist);
9383 print_z_candidates (location_of (name), candidates);
9384 if (free_p)
9385 free (pretty_name);
9387 call = error_mark_node;
9389 else
9391 fn = cand->fn;
9392 call = NULL_TREE;
9394 if (!(flags & LOOKUP_NONVIRTUAL)
9395 && DECL_PURE_VIRTUAL_P (fn)
9396 && instance == current_class_ref
9397 && (complain & tf_warning))
9399 /* This is not an error, it is runtime undefined
9400 behavior. */
9401 if (!current_function_decl)
9402 warning (0, "pure virtual %q#D called from "
9403 "non-static data member initializer", fn);
9404 else if (DECL_CONSTRUCTOR_P (current_function_decl)
9405 || DECL_DESTRUCTOR_P (current_function_decl))
9406 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
9407 ? G_("pure virtual %q#D called from constructor")
9408 : G_("pure virtual %q#D called from destructor")),
9409 fn);
9412 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
9413 && !DECL_CONSTRUCTOR_P (fn)
9414 && is_dummy_object (instance))
9416 instance = maybe_resolve_dummy (instance, true);
9417 if (instance == error_mark_node)
9418 call = error_mark_node;
9419 else if (!is_dummy_object (instance))
9421 /* We captured 'this' in the current lambda now that
9422 we know we really need it. */
9423 cand->first_arg = instance;
9425 else if (any_dependent_bases_p ())
9426 /* We can't tell until instantiation time whether we can use
9427 *this as the implicit object argument. */;
9428 else
9430 if (complain & tf_error)
9431 error ("cannot call member function %qD without object",
9432 fn);
9433 call = error_mark_node;
9437 if (call != error_mark_node)
9439 /* Optimize away vtable lookup if we know that this
9440 function can't be overridden. We need to check if
9441 the context and the type where we found fn are the same,
9442 actually FN might be defined in a different class
9443 type because of a using-declaration. In this case, we
9444 do not want to perform a non-virtual call. */
9445 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
9446 && same_type_ignoring_top_level_qualifiers_p
9447 (DECL_CONTEXT (fn), BINFO_TYPE (binfo))
9448 && resolves_to_fixed_type_p (instance, 0))
9449 flags |= LOOKUP_NONVIRTUAL;
9450 if (explicit_targs)
9451 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
9452 /* Now we know what function is being called. */
9453 if (fn_p)
9454 *fn_p = fn;
9455 /* Build the actual CALL_EXPR. */
9456 call = build_over_call (cand, flags, complain);
9457 /* In an expression of the form `a->f()' where `f' turns
9458 out to be a static member function, `a' is
9459 none-the-less evaluated. */
9460 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
9461 && !is_dummy_object (instance)
9462 && TREE_SIDE_EFFECTS (instance))
9464 /* But avoid the implicit lvalue-rvalue conversion when 'a'
9465 is volatile. */
9466 tree a = instance;
9467 if (TREE_THIS_VOLATILE (a))
9468 a = build_this (a);
9469 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), a, call);
9471 else if (call != error_mark_node
9472 && DECL_DESTRUCTOR_P (cand->fn)
9473 && !VOID_TYPE_P (TREE_TYPE (call)))
9474 /* An explicit call of the form "x->~X()" has type
9475 "void". However, on platforms where destructors
9476 return "this" (i.e., those where
9477 targetm.cxx.cdtor_returns_this is true), such calls
9478 will appear to have a return value of pointer type
9479 to the low-level call machinery. We do not want to
9480 change the low-level machinery, since we want to be
9481 able to optimize "delete f()" on such platforms as
9482 "operator delete(~X(f()))" (rather than generating
9483 "t = f(), ~X(t), operator delete (t)"). */
9484 call = build_nop (void_type_node, call);
9489 if (processing_template_decl && call != error_mark_node)
9491 bool cast_to_void = false;
9493 if (TREE_CODE (call) == COMPOUND_EXPR)
9494 call = TREE_OPERAND (call, 1);
9495 else if (TREE_CODE (call) == NOP_EXPR)
9497 cast_to_void = true;
9498 call = TREE_OPERAND (call, 0);
9500 if (INDIRECT_REF_P (call))
9501 call = TREE_OPERAND (call, 0);
9502 call = (build_min_non_dep_call_vec
9503 (call,
9504 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
9505 orig_instance, orig_fns, NULL_TREE),
9506 orig_args));
9507 SET_EXPR_LOCATION (call, input_location);
9508 call = convert_from_reference (call);
9509 if (cast_to_void)
9510 call = build_nop (void_type_node, call);
9513 /* Free all the conversions we allocated. */
9514 obstack_free (&conversion_obstack, p);
9516 if (orig_args != NULL)
9517 release_tree_vector (orig_args);
9519 return call;
9522 /* Wrapper for above. */
9524 tree
9525 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
9526 tree conversion_path, int flags,
9527 tree *fn_p, tsubst_flags_t complain)
9529 tree ret;
9530 bool subtime = timevar_cond_start (TV_OVERLOAD);
9531 ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
9532 fn_p, complain);
9533 timevar_cond_stop (TV_OVERLOAD, subtime);
9534 return ret;
9537 /* Returns true iff standard conversion sequence ICS1 is a proper
9538 subsequence of ICS2. */
9540 static bool
9541 is_subseq (conversion *ics1, conversion *ics2)
9543 /* We can assume that a conversion of the same code
9544 between the same types indicates a subsequence since we only get
9545 here if the types we are converting from are the same. */
9547 while (ics1->kind == ck_rvalue
9548 || ics1->kind == ck_lvalue)
9549 ics1 = next_conversion (ics1);
9551 while (1)
9553 while (ics2->kind == ck_rvalue
9554 || ics2->kind == ck_lvalue)
9555 ics2 = next_conversion (ics2);
9557 if (ics2->kind == ck_user
9558 || ics2->kind == ck_ambig
9559 || ics2->kind == ck_aggr
9560 || ics2->kind == ck_list
9561 || ics2->kind == ck_identity)
9562 /* At this point, ICS1 cannot be a proper subsequence of
9563 ICS2. We can get a USER_CONV when we are comparing the
9564 second standard conversion sequence of two user conversion
9565 sequences. */
9566 return false;
9568 ics2 = next_conversion (ics2);
9570 while (ics2->kind == ck_rvalue
9571 || ics2->kind == ck_lvalue)
9572 ics2 = next_conversion (ics2);
9574 if (ics2->kind == ics1->kind
9575 && same_type_p (ics2->type, ics1->type)
9576 && (ics1->kind == ck_identity
9577 || same_type_p (next_conversion (ics2)->type,
9578 next_conversion (ics1)->type)))
9579 return true;
9583 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
9584 be any _TYPE nodes. */
9586 bool
9587 is_properly_derived_from (tree derived, tree base)
9589 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
9590 return false;
9592 /* We only allow proper derivation here. The DERIVED_FROM_P macro
9593 considers every class derived from itself. */
9594 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
9595 && DERIVED_FROM_P (base, derived));
9598 /* We build the ICS for an implicit object parameter as a pointer
9599 conversion sequence. However, such a sequence should be compared
9600 as if it were a reference conversion sequence. If ICS is the
9601 implicit conversion sequence for an implicit object parameter,
9602 modify it accordingly. */
9604 static void
9605 maybe_handle_implicit_object (conversion **ics)
9607 if ((*ics)->this_p)
9609 /* [over.match.funcs]
9611 For non-static member functions, the type of the
9612 implicit object parameter is "reference to cv X"
9613 where X is the class of which the function is a
9614 member and cv is the cv-qualification on the member
9615 function declaration. */
9616 conversion *t = *ics;
9617 tree reference_type;
9619 /* The `this' parameter is a pointer to a class type. Make the
9620 implicit conversion talk about a reference to that same class
9621 type. */
9622 reference_type = TREE_TYPE (t->type);
9623 reference_type = build_reference_type (reference_type);
9625 if (t->kind == ck_qual)
9626 t = next_conversion (t);
9627 if (t->kind == ck_ptr)
9628 t = next_conversion (t);
9629 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
9630 t = direct_reference_binding (reference_type, t);
9631 t->this_p = 1;
9632 t->rvaluedness_matches_p = 0;
9633 *ics = t;
9637 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
9638 and return the initial reference binding conversion. Otherwise,
9639 leave *ICS unchanged and return NULL. */
9641 static conversion *
9642 maybe_handle_ref_bind (conversion **ics)
9644 if ((*ics)->kind == ck_ref_bind)
9646 conversion *old_ics = *ics;
9647 *ics = next_conversion (old_ics);
9648 (*ics)->user_conv_p = old_ics->user_conv_p;
9649 return old_ics;
9652 return NULL;
9655 /* Compare two implicit conversion sequences according to the rules set out in
9656 [over.ics.rank]. Return values:
9658 1: ics1 is better than ics2
9659 -1: ics2 is better than ics1
9660 0: ics1 and ics2 are indistinguishable */
9662 static int
9663 compare_ics (conversion *ics1, conversion *ics2)
9665 tree from_type1;
9666 tree from_type2;
9667 tree to_type1;
9668 tree to_type2;
9669 tree deref_from_type1 = NULL_TREE;
9670 tree deref_from_type2 = NULL_TREE;
9671 tree deref_to_type1 = NULL_TREE;
9672 tree deref_to_type2 = NULL_TREE;
9673 conversion_rank rank1, rank2;
9675 /* REF_BINDING is nonzero if the result of the conversion sequence
9676 is a reference type. In that case REF_CONV is the reference
9677 binding conversion. */
9678 conversion *ref_conv1;
9679 conversion *ref_conv2;
9681 /* Compare badness before stripping the reference conversion. */
9682 if (ics1->bad_p > ics2->bad_p)
9683 return -1;
9684 else if (ics1->bad_p < ics2->bad_p)
9685 return 1;
9687 /* Handle implicit object parameters. */
9688 maybe_handle_implicit_object (&ics1);
9689 maybe_handle_implicit_object (&ics2);
9691 /* Handle reference parameters. */
9692 ref_conv1 = maybe_handle_ref_bind (&ics1);
9693 ref_conv2 = maybe_handle_ref_bind (&ics2);
9695 /* List-initialization sequence L1 is a better conversion sequence than
9696 list-initialization sequence L2 if L1 converts to
9697 std::initializer_list<X> for some X and L2 does not. */
9698 if (ics1->kind == ck_list && ics2->kind != ck_list)
9699 return 1;
9700 if (ics2->kind == ck_list && ics1->kind != ck_list)
9701 return -1;
9703 /* [over.ics.rank]
9705 When comparing the basic forms of implicit conversion sequences (as
9706 defined in _over.best.ics_)
9708 --a standard conversion sequence (_over.ics.scs_) is a better
9709 conversion sequence than a user-defined conversion sequence
9710 or an ellipsis conversion sequence, and
9712 --a user-defined conversion sequence (_over.ics.user_) is a
9713 better conversion sequence than an ellipsis conversion sequence
9714 (_over.ics.ellipsis_). */
9715 /* Use BAD_CONVERSION_RANK because we already checked for a badness
9716 mismatch. If both ICS are bad, we try to make a decision based on
9717 what would have happened if they'd been good. This is not an
9718 extension, we'll still give an error when we build up the call; this
9719 just helps us give a more helpful error message. */
9720 rank1 = BAD_CONVERSION_RANK (ics1);
9721 rank2 = BAD_CONVERSION_RANK (ics2);
9723 if (rank1 > rank2)
9724 return -1;
9725 else if (rank1 < rank2)
9726 return 1;
9728 if (ics1->ellipsis_p)
9729 /* Both conversions are ellipsis conversions. */
9730 return 0;
9732 /* User-defined conversion sequence U1 is a better conversion sequence
9733 than another user-defined conversion sequence U2 if they contain the
9734 same user-defined conversion operator or constructor and if the sec-
9735 ond standard conversion sequence of U1 is better than the second
9736 standard conversion sequence of U2. */
9738 /* Handle list-conversion with the same code even though it isn't always
9739 ranked as a user-defined conversion and it doesn't have a second
9740 standard conversion sequence; it will still have the desired effect.
9741 Specifically, we need to do the reference binding comparison at the
9742 end of this function. */
9744 if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
9746 conversion *t1;
9747 conversion *t2;
9749 for (t1 = ics1; t1->kind != ck_user; t1 = next_conversion (t1))
9750 if (t1->kind == ck_ambig || t1->kind == ck_aggr
9751 || t1->kind == ck_list)
9752 break;
9753 for (t2 = ics2; t2->kind != ck_user; t2 = next_conversion (t2))
9754 if (t2->kind == ck_ambig || t2->kind == ck_aggr
9755 || t2->kind == ck_list)
9756 break;
9758 if (t1->kind != t2->kind)
9759 return 0;
9760 else if (t1->kind == ck_user)
9762 tree f1 = t1->cand ? t1->cand->fn : t1->type;
9763 tree f2 = t2->cand ? t2->cand->fn : t2->type;
9764 if (f1 != f2)
9765 return 0;
9767 else
9769 /* For ambiguous or aggregate conversions, use the target type as
9770 a proxy for the conversion function. */
9771 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
9772 return 0;
9775 /* We can just fall through here, after setting up
9776 FROM_TYPE1 and FROM_TYPE2. */
9777 from_type1 = t1->type;
9778 from_type2 = t2->type;
9780 else
9782 conversion *t1;
9783 conversion *t2;
9785 /* We're dealing with two standard conversion sequences.
9787 [over.ics.rank]
9789 Standard conversion sequence S1 is a better conversion
9790 sequence than standard conversion sequence S2 if
9792 --S1 is a proper subsequence of S2 (comparing the conversion
9793 sequences in the canonical form defined by _over.ics.scs_,
9794 excluding any Lvalue Transformation; the identity
9795 conversion sequence is considered to be a subsequence of
9796 any non-identity conversion sequence */
9798 t1 = ics1;
9799 while (t1->kind != ck_identity)
9800 t1 = next_conversion (t1);
9801 from_type1 = t1->type;
9803 t2 = ics2;
9804 while (t2->kind != ck_identity)
9805 t2 = next_conversion (t2);
9806 from_type2 = t2->type;
9809 /* One sequence can only be a subsequence of the other if they start with
9810 the same type. They can start with different types when comparing the
9811 second standard conversion sequence in two user-defined conversion
9812 sequences. */
9813 if (same_type_p (from_type1, from_type2))
9815 if (is_subseq (ics1, ics2))
9816 return 1;
9817 if (is_subseq (ics2, ics1))
9818 return -1;
9821 /* [over.ics.rank]
9823 Or, if not that,
9825 --the rank of S1 is better than the rank of S2 (by the rules
9826 defined below):
9828 Standard conversion sequences are ordered by their ranks: an Exact
9829 Match is a better conversion than a Promotion, which is a better
9830 conversion than a Conversion.
9832 Two conversion sequences with the same rank are indistinguishable
9833 unless one of the following rules applies:
9835 --A conversion that does not a convert a pointer, pointer to member,
9836 or std::nullptr_t to bool is better than one that does.
9838 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
9839 so that we do not have to check it explicitly. */
9840 if (ics1->rank < ics2->rank)
9841 return 1;
9842 else if (ics2->rank < ics1->rank)
9843 return -1;
9845 to_type1 = ics1->type;
9846 to_type2 = ics2->type;
9848 /* A conversion from scalar arithmetic type to complex is worse than a
9849 conversion between scalar arithmetic types. */
9850 if (same_type_p (from_type1, from_type2)
9851 && ARITHMETIC_TYPE_P (from_type1)
9852 && ARITHMETIC_TYPE_P (to_type1)
9853 && ARITHMETIC_TYPE_P (to_type2)
9854 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
9855 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
9857 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
9858 return -1;
9859 else
9860 return 1;
9863 if (TYPE_PTR_P (from_type1)
9864 && TYPE_PTR_P (from_type2)
9865 && TYPE_PTR_P (to_type1)
9866 && TYPE_PTR_P (to_type2))
9868 deref_from_type1 = TREE_TYPE (from_type1);
9869 deref_from_type2 = TREE_TYPE (from_type2);
9870 deref_to_type1 = TREE_TYPE (to_type1);
9871 deref_to_type2 = TREE_TYPE (to_type2);
9873 /* The rules for pointers to members A::* are just like the rules
9874 for pointers A*, except opposite: if B is derived from A then
9875 A::* converts to B::*, not vice versa. For that reason, we
9876 switch the from_ and to_ variables here. */
9877 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
9878 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
9879 || (TYPE_PTRMEMFUNC_P (from_type1)
9880 && TYPE_PTRMEMFUNC_P (from_type2)
9881 && TYPE_PTRMEMFUNC_P (to_type1)
9882 && TYPE_PTRMEMFUNC_P (to_type2)))
9884 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
9885 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
9886 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
9887 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
9890 if (deref_from_type1 != NULL_TREE
9891 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
9892 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
9894 /* This was one of the pointer or pointer-like conversions.
9896 [over.ics.rank]
9898 --If class B is derived directly or indirectly from class A,
9899 conversion of B* to A* is better than conversion of B* to
9900 void*, and conversion of A* to void* is better than
9901 conversion of B* to void*. */
9902 if (VOID_TYPE_P (deref_to_type1)
9903 && VOID_TYPE_P (deref_to_type2))
9905 if (is_properly_derived_from (deref_from_type1,
9906 deref_from_type2))
9907 return -1;
9908 else if (is_properly_derived_from (deref_from_type2,
9909 deref_from_type1))
9910 return 1;
9912 else if (VOID_TYPE_P (deref_to_type1)
9913 || VOID_TYPE_P (deref_to_type2))
9915 if (same_type_p (deref_from_type1, deref_from_type2))
9917 if (VOID_TYPE_P (deref_to_type2))
9919 if (is_properly_derived_from (deref_from_type1,
9920 deref_to_type1))
9921 return 1;
9923 /* We know that DEREF_TO_TYPE1 is `void' here. */
9924 else if (is_properly_derived_from (deref_from_type1,
9925 deref_to_type2))
9926 return -1;
9929 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
9930 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
9932 /* [over.ics.rank]
9934 --If class B is derived directly or indirectly from class A
9935 and class C is derived directly or indirectly from B,
9937 --conversion of C* to B* is better than conversion of C* to
9940 --conversion of B* to A* is better than conversion of C* to
9941 A* */
9942 if (same_type_p (deref_from_type1, deref_from_type2))
9944 if (is_properly_derived_from (deref_to_type1,
9945 deref_to_type2))
9946 return 1;
9947 else if (is_properly_derived_from (deref_to_type2,
9948 deref_to_type1))
9949 return -1;
9951 else if (same_type_p (deref_to_type1, deref_to_type2))
9953 if (is_properly_derived_from (deref_from_type2,
9954 deref_from_type1))
9955 return 1;
9956 else if (is_properly_derived_from (deref_from_type1,
9957 deref_from_type2))
9958 return -1;
9962 else if (CLASS_TYPE_P (non_reference (from_type1))
9963 && same_type_p (from_type1, from_type2))
9965 tree from = non_reference (from_type1);
9967 /* [over.ics.rank]
9969 --binding of an expression of type C to a reference of type
9970 B& is better than binding an expression of type C to a
9971 reference of type A&
9973 --conversion of C to B is better than conversion of C to A, */
9974 if (is_properly_derived_from (from, to_type1)
9975 && is_properly_derived_from (from, to_type2))
9977 if (is_properly_derived_from (to_type1, to_type2))
9978 return 1;
9979 else if (is_properly_derived_from (to_type2, to_type1))
9980 return -1;
9983 else if (CLASS_TYPE_P (non_reference (to_type1))
9984 && same_type_p (to_type1, to_type2))
9986 tree to = non_reference (to_type1);
9988 /* [over.ics.rank]
9990 --binding of an expression of type B to a reference of type
9991 A& is better than binding an expression of type C to a
9992 reference of type A&,
9994 --conversion of B to A is better than conversion of C to A */
9995 if (is_properly_derived_from (from_type1, to)
9996 && is_properly_derived_from (from_type2, to))
9998 if (is_properly_derived_from (from_type2, from_type1))
9999 return 1;
10000 else if (is_properly_derived_from (from_type1, from_type2))
10001 return -1;
10005 /* [over.ics.rank]
10007 --S1 and S2 differ only in their qualification conversion and yield
10008 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
10009 qualification signature of type T1 is a proper subset of the cv-
10010 qualification signature of type T2 */
10011 if (ics1->kind == ck_qual
10012 && ics2->kind == ck_qual
10013 && same_type_p (from_type1, from_type2))
10015 int result = comp_cv_qual_signature (to_type1, to_type2);
10016 if (result != 0)
10017 return result;
10020 /* [over.ics.rank]
10022 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
10023 to an implicit object parameter of a non-static member function
10024 declared without a ref-qualifier, and either S1 binds an lvalue
10025 reference to an lvalue and S2 binds an rvalue reference or S1 binds an
10026 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
10027 draft standard, 13.3.3.2)
10029 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
10030 types to which the references refer are the same type except for
10031 top-level cv-qualifiers, and the type to which the reference
10032 initialized by S2 refers is more cv-qualified than the type to
10033 which the reference initialized by S1 refers.
10035 DR 1328 [over.match.best]: the context is an initialization by
10036 conversion function for direct reference binding (13.3.1.6) of a
10037 reference to function type, the return type of F1 is the same kind of
10038 reference (i.e. lvalue or rvalue) as the reference being initialized,
10039 and the return type of F2 is not. */
10041 if (ref_conv1 && ref_conv2)
10043 if (!ref_conv1->this_p && !ref_conv2->this_p
10044 && (ref_conv1->rvaluedness_matches_p
10045 != ref_conv2->rvaluedness_matches_p)
10046 && (same_type_p (ref_conv1->type, ref_conv2->type)
10047 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
10048 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
10050 if (ref_conv1->bad_p
10051 && !same_type_p (TREE_TYPE (ref_conv1->type),
10052 TREE_TYPE (ref_conv2->type)))
10053 /* Don't prefer a bad conversion that drops cv-quals to a bad
10054 conversion with the wrong rvalueness. */
10055 return 0;
10056 return (ref_conv1->rvaluedness_matches_p
10057 - ref_conv2->rvaluedness_matches_p);
10060 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
10062 int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
10063 int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
10064 if (ref_conv1->bad_p)
10066 /* Prefer the one that drops fewer cv-quals. */
10067 tree ftype = next_conversion (ref_conv1)->type;
10068 int fquals = cp_type_quals (ftype);
10069 q1 ^= fquals;
10070 q2 ^= fquals;
10072 return comp_cv_qualification (q2, q1);
10076 /* Neither conversion sequence is better than the other. */
10077 return 0;
10080 /* The source type for this standard conversion sequence. */
10082 static tree
10083 source_type (conversion *t)
10085 for (;; t = next_conversion (t))
10087 if (t->kind == ck_user
10088 || t->kind == ck_ambig
10089 || t->kind == ck_identity)
10090 return t->type;
10092 gcc_unreachable ();
10095 /* Note a warning about preferring WINNER to LOSER. We do this by storing
10096 a pointer to LOSER and re-running joust to produce the warning if WINNER
10097 is actually used. */
10099 static void
10100 add_warning (struct z_candidate *winner, struct z_candidate *loser)
10102 candidate_warning *cw = (candidate_warning *)
10103 conversion_obstack_alloc (sizeof (candidate_warning));
10104 cw->loser = loser;
10105 cw->next = winner->warnings;
10106 winner->warnings = cw;
10109 /* Compare two candidates for overloading as described in
10110 [over.match.best]. Return values:
10112 1: cand1 is better than cand2
10113 -1: cand2 is better than cand1
10114 0: cand1 and cand2 are indistinguishable */
10116 static int
10117 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
10118 tsubst_flags_t complain)
10120 int winner = 0;
10121 int off1 = 0, off2 = 0;
10122 size_t i;
10123 size_t len;
10125 /* Candidates that involve bad conversions are always worse than those
10126 that don't. */
10127 if (cand1->viable > cand2->viable)
10128 return 1;
10129 if (cand1->viable < cand2->viable)
10130 return -1;
10132 /* If we have two pseudo-candidates for conversions to the same type,
10133 or two candidates for the same function, arbitrarily pick one. */
10134 if (cand1->fn == cand2->fn
10135 && (IS_TYPE_OR_DECL_P (cand1->fn)))
10136 return 1;
10138 /* Prefer a non-deleted function over an implicitly deleted move
10139 constructor or assignment operator. This differs slightly from the
10140 wording for issue 1402 (which says the move op is ignored by overload
10141 resolution), but this way produces better error messages. */
10142 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
10143 && TREE_CODE (cand2->fn) == FUNCTION_DECL
10144 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
10146 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
10147 && move_fn_p (cand1->fn))
10148 return -1;
10149 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
10150 && move_fn_p (cand2->fn))
10151 return 1;
10154 /* a viable function F1
10155 is defined to be a better function than another viable function F2 if
10156 for all arguments i, ICSi(F1) is not a worse conversion sequence than
10157 ICSi(F2), and then */
10159 /* for some argument j, ICSj(F1) is a better conversion sequence than
10160 ICSj(F2) */
10162 /* For comparing static and non-static member functions, we ignore
10163 the implicit object parameter of the non-static function. The
10164 standard says to pretend that the static function has an object
10165 parm, but that won't work with operator overloading. */
10166 len = cand1->num_convs;
10167 if (len != cand2->num_convs)
10169 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
10170 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
10172 if (DECL_CONSTRUCTOR_P (cand1->fn)
10173 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
10174 /* We're comparing a near-match list constructor and a near-match
10175 non-list constructor. Just treat them as unordered. */
10176 return 0;
10178 gcc_assert (static_1 != static_2);
10180 if (static_1)
10181 off2 = 1;
10182 else
10184 off1 = 1;
10185 --len;
10189 for (i = 0; i < len; ++i)
10191 conversion *t1 = cand1->convs[i + off1];
10192 conversion *t2 = cand2->convs[i + off2];
10193 int comp = compare_ics (t1, t2);
10195 if (comp != 0)
10197 if ((complain & tf_warning)
10198 && warn_sign_promo
10199 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
10200 == cr_std + cr_promotion)
10201 && t1->kind == ck_std
10202 && t2->kind == ck_std
10203 && TREE_CODE (t1->type) == INTEGER_TYPE
10204 && TREE_CODE (t2->type) == INTEGER_TYPE
10205 && (TYPE_PRECISION (t1->type)
10206 == TYPE_PRECISION (t2->type))
10207 && (TYPE_UNSIGNED (next_conversion (t1)->type)
10208 || (TREE_CODE (next_conversion (t1)->type)
10209 == ENUMERAL_TYPE)))
10211 tree type = next_conversion (t1)->type;
10212 tree type1, type2;
10213 struct z_candidate *w, *l;
10214 if (comp > 0)
10215 type1 = t1->type, type2 = t2->type,
10216 w = cand1, l = cand2;
10217 else
10218 type1 = t2->type, type2 = t1->type,
10219 w = cand2, l = cand1;
10221 if (warn)
10223 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
10224 type, type1, type2);
10225 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
10227 else
10228 add_warning (w, l);
10231 if (winner && comp != winner)
10233 winner = 0;
10234 goto tweak;
10236 winner = comp;
10240 /* warn about confusing overload resolution for user-defined conversions,
10241 either between a constructor and a conversion op, or between two
10242 conversion ops. */
10243 if ((complain & tf_warning)
10244 && winner && warn_conversion && cand1->second_conv
10245 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
10246 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
10248 struct z_candidate *w, *l;
10249 bool give_warning = false;
10251 if (winner == 1)
10252 w = cand1, l = cand2;
10253 else
10254 w = cand2, l = cand1;
10256 /* We don't want to complain about `X::operator T1 ()'
10257 beating `X::operator T2 () const', when T2 is a no less
10258 cv-qualified version of T1. */
10259 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
10260 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
10262 tree t = TREE_TYPE (TREE_TYPE (l->fn));
10263 tree f = TREE_TYPE (TREE_TYPE (w->fn));
10265 if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t))
10267 t = TREE_TYPE (t);
10268 f = TREE_TYPE (f);
10270 if (!comp_ptr_ttypes (t, f))
10271 give_warning = true;
10273 else
10274 give_warning = true;
10276 if (!give_warning)
10277 /*NOP*/;
10278 else if (warn)
10280 tree source = source_type (w->convs[0]);
10281 if (INDIRECT_TYPE_P (source))
10282 source = TREE_TYPE (source);
10283 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
10284 && warning (OPT_Wconversion, " for conversion from %qH to %qI",
10285 source, w->second_conv->type))
10287 inform (input_location, " because conversion sequence for the argument is better");
10290 else
10291 add_warning (w, l);
10294 if (winner)
10295 return winner;
10297 /* DR 495 moved this tiebreaker above the template ones. */
10298 /* or, if not that,
10299 the context is an initialization by user-defined conversion (see
10300 _dcl.init_ and _over.match.user_) and the standard conversion
10301 sequence from the return type of F1 to the destination type (i.e.,
10302 the type of the entity being initialized) is a better conversion
10303 sequence than the standard conversion sequence from the return type
10304 of F2 to the destination type. */
10306 if (cand1->second_conv)
10308 winner = compare_ics (cand1->second_conv, cand2->second_conv);
10309 if (winner)
10310 return winner;
10313 /* or, if not that,
10314 F1 is a non-template function and F2 is a template function
10315 specialization. */
10317 if (!cand1->template_decl && cand2->template_decl)
10318 return 1;
10319 else if (cand1->template_decl && !cand2->template_decl)
10320 return -1;
10322 /* or, if not that,
10323 F1 and F2 are template functions and the function template for F1 is
10324 more specialized than the template for F2 according to the partial
10325 ordering rules. */
10327 if (cand1->template_decl && cand2->template_decl)
10329 winner = more_specialized_fn
10330 (TI_TEMPLATE (cand1->template_decl),
10331 TI_TEMPLATE (cand2->template_decl),
10332 /* [temp.func.order]: The presence of unused ellipsis and default
10333 arguments has no effect on the partial ordering of function
10334 templates. add_function_candidate() will not have
10335 counted the "this" argument for constructors. */
10336 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
10337 if (winner)
10338 return winner;
10341 // C++ Concepts
10342 // or, if not that, F1 is more constrained than F2.
10343 if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn))
10345 winner = more_constrained (cand1->fn, cand2->fn);
10346 if (winner)
10347 return winner;
10350 /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
10351 if (deduction_guide_p (cand1->fn))
10353 gcc_assert (deduction_guide_p (cand2->fn));
10354 /* We distinguish between candidates from an explicit deduction guide and
10355 candidates built from a constructor based on DECL_ARTIFICIAL. */
10356 int art1 = DECL_ARTIFICIAL (cand1->fn);
10357 int art2 = DECL_ARTIFICIAL (cand2->fn);
10358 if (art1 != art2)
10359 return art2 - art1;
10361 if (art1)
10363 /* Prefer the special copy guide over a declared copy/move
10364 constructor. */
10365 if (copy_guide_p (cand1->fn))
10366 return 1;
10367 if (copy_guide_p (cand2->fn))
10368 return -1;
10370 /* Prefer a candidate generated from a non-template constructor. */
10371 int tg1 = template_guide_p (cand1->fn);
10372 int tg2 = template_guide_p (cand2->fn);
10373 if (tg1 != tg2)
10374 return tg2 - tg1;
10378 /* F1 is a member of a class D, F2 is a member of a base class B of D, and
10379 for all arguments the corresponding parameters of F1 and F2 have the same
10380 type (CWG 2273/2277). */
10381 if (DECL_P (cand1->fn) && DECL_CLASS_SCOPE_P (cand1->fn)
10382 && !DECL_CONV_FN_P (cand1->fn)
10383 && DECL_P (cand2->fn) && DECL_CLASS_SCOPE_P (cand2->fn)
10384 && !DECL_CONV_FN_P (cand2->fn))
10386 tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
10387 tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
10389 bool used1 = false;
10390 bool used2 = false;
10391 if (base1 == base2)
10392 /* No difference. */;
10393 else if (DERIVED_FROM_P (base1, base2))
10394 used1 = true;
10395 else if (DERIVED_FROM_P (base2, base1))
10396 used2 = true;
10398 if (int diff = used2 - used1)
10400 for (i = 0; i < len; ++i)
10402 conversion *t1 = cand1->convs[i + off1];
10403 conversion *t2 = cand2->convs[i + off2];
10404 if (!same_type_p (t1->type, t2->type))
10405 break;
10407 if (i == len)
10408 return diff;
10412 /* Check whether we can discard a builtin candidate, either because we
10413 have two identical ones or matching builtin and non-builtin candidates.
10415 (Pedantically in the latter case the builtin which matched the user
10416 function should not be added to the overload set, but we spot it here.
10418 [over.match.oper]
10419 ... the builtin candidates include ...
10420 - do not have the same parameter type list as any non-template
10421 non-member candidate. */
10423 if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
10425 for (i = 0; i < len; ++i)
10426 if (!same_type_p (cand1->convs[i]->type,
10427 cand2->convs[i]->type))
10428 break;
10429 if (i == cand1->num_convs)
10431 if (cand1->fn == cand2->fn)
10432 /* Two built-in candidates; arbitrarily pick one. */
10433 return 1;
10434 else if (identifier_p (cand1->fn))
10435 /* cand1 is built-in; prefer cand2. */
10436 return -1;
10437 else
10438 /* cand2 is built-in; prefer cand1. */
10439 return 1;
10443 /* For candidates of a multi-versioned function, make the version with
10444 the highest priority win. This version will be checked for dispatching
10445 first. If this version can be inlined into the caller, the front-end
10446 will simply make a direct call to this function. */
10448 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
10449 && DECL_FUNCTION_VERSIONED (cand1->fn)
10450 && TREE_CODE (cand2->fn) == FUNCTION_DECL
10451 && DECL_FUNCTION_VERSIONED (cand2->fn))
10453 tree f1 = TREE_TYPE (cand1->fn);
10454 tree f2 = TREE_TYPE (cand2->fn);
10455 tree p1 = TYPE_ARG_TYPES (f1);
10456 tree p2 = TYPE_ARG_TYPES (f2);
10458 /* Check if cand1->fn and cand2->fn are versions of the same function. It
10459 is possible that cand1->fn and cand2->fn are function versions but of
10460 different functions. Check types to see if they are versions of the same
10461 function. */
10462 if (compparms (p1, p2)
10463 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
10465 /* Always make the version with the higher priority, more
10466 specialized, win. */
10467 gcc_assert (targetm.compare_version_priority);
10468 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
10469 return 1;
10470 else
10471 return -1;
10475 /* If the two function declarations represent the same function (this can
10476 happen with declarations in multiple scopes and arg-dependent lookup),
10477 arbitrarily choose one. But first make sure the default args we're
10478 using match. */
10479 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
10480 && equal_functions (cand1->fn, cand2->fn))
10482 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
10483 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
10485 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
10487 for (i = 0; i < len; ++i)
10489 /* Don't crash if the fn is variadic. */
10490 if (!parms1)
10491 break;
10492 parms1 = TREE_CHAIN (parms1);
10493 parms2 = TREE_CHAIN (parms2);
10496 if (off1)
10497 parms1 = TREE_CHAIN (parms1);
10498 else if (off2)
10499 parms2 = TREE_CHAIN (parms2);
10501 for (; parms1; ++i)
10503 if (!cp_tree_equal (TREE_PURPOSE (parms1),
10504 TREE_PURPOSE (parms2)))
10506 if (warn)
10508 if (complain & tf_error)
10510 if (permerror (input_location,
10511 "default argument mismatch in "
10512 "overload resolution"))
10514 inform (DECL_SOURCE_LOCATION (cand1->fn),
10515 " candidate 1: %q#F", cand1->fn);
10516 inform (DECL_SOURCE_LOCATION (cand2->fn),
10517 " candidate 2: %q#F", cand2->fn);
10520 else
10521 return 0;
10523 else
10524 add_warning (cand1, cand2);
10525 break;
10527 parms1 = TREE_CHAIN (parms1);
10528 parms2 = TREE_CHAIN (parms2);
10531 return 1;
10534 tweak:
10536 /* Extension: If the worst conversion for one candidate is worse than the
10537 worst conversion for the other, take the first. */
10538 if (!pedantic && (complain & tf_warning_or_error))
10540 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
10541 struct z_candidate *w = 0, *l = 0;
10543 for (i = 0; i < len; ++i)
10545 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
10546 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
10547 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
10548 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
10550 if (rank1 < rank2)
10551 winner = 1, w = cand1, l = cand2;
10552 if (rank1 > rank2)
10553 winner = -1, w = cand2, l = cand1;
10554 if (winner)
10556 /* Don't choose a deleted function over ambiguity. */
10557 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
10558 return 0;
10559 if (warn)
10561 pedwarn (input_location, 0,
10562 "ISO C++ says that these are ambiguous, even "
10563 "though the worst conversion for the first is better than "
10564 "the worst conversion for the second:");
10565 print_z_candidate (input_location, _("candidate 1:"), w);
10566 print_z_candidate (input_location, _("candidate 2:"), l);
10568 else
10569 add_warning (w, l);
10570 return winner;
10574 gcc_assert (!winner);
10575 return 0;
10578 /* Given a list of candidates for overloading, find the best one, if any.
10579 This algorithm has a worst case of O(2n) (winner is last), and a best
10580 case of O(n/2) (totally ambiguous); much better than a sorting
10581 algorithm. */
10583 static struct z_candidate *
10584 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
10586 struct z_candidate *champ = candidates, *challenger;
10587 int fate;
10588 int champ_compared_to_predecessor = 0;
10590 /* Walk through the list once, comparing each current champ to the next
10591 candidate, knocking out a candidate or two with each comparison. */
10593 for (challenger = champ->next; challenger; )
10595 fate = joust (champ, challenger, 0, complain);
10596 if (fate == 1)
10597 challenger = challenger->next;
10598 else
10600 if (fate == 0)
10602 champ = challenger->next;
10603 if (champ == 0)
10604 return NULL;
10605 champ_compared_to_predecessor = 0;
10607 else
10609 champ = challenger;
10610 champ_compared_to_predecessor = 1;
10613 challenger = champ->next;
10617 /* Make sure the champ is better than all the candidates it hasn't yet
10618 been compared to. */
10620 for (challenger = candidates;
10621 challenger != champ
10622 && !(champ_compared_to_predecessor && challenger->next == champ);
10623 challenger = challenger->next)
10625 fate = joust (champ, challenger, 0, complain);
10626 if (fate != 1)
10627 return NULL;
10630 return champ;
10633 /* Returns nonzero if things of type FROM can be converted to TO. */
10635 bool
10636 can_convert (tree to, tree from, tsubst_flags_t complain)
10638 tree arg = NULL_TREE;
10639 /* implicit_conversion only considers user-defined conversions
10640 if it has an expression for the call argument list. */
10641 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
10642 arg = build1 (CAST_EXPR, from, NULL_TREE);
10643 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
10646 /* Returns nonzero if things of type FROM can be converted to TO with a
10647 standard conversion. */
10649 bool
10650 can_convert_standard (tree to, tree from, tsubst_flags_t complain)
10652 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
10655 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
10657 bool
10658 can_convert_arg (tree to, tree from, tree arg, int flags,
10659 tsubst_flags_t complain)
10661 conversion *t;
10662 void *p;
10663 bool ok_p;
10665 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10666 p = conversion_obstack_alloc (0);
10667 /* We want to discard any access checks done for this test,
10668 as we might not be in the appropriate access context and
10669 we'll do the check again when we actually perform the
10670 conversion. */
10671 push_deferring_access_checks (dk_deferred);
10673 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
10674 flags, complain);
10675 ok_p = (t && !t->bad_p);
10677 /* Discard the access checks now. */
10678 pop_deferring_access_checks ();
10679 /* Free all the conversions we allocated. */
10680 obstack_free (&conversion_obstack, p);
10682 return ok_p;
10685 /* Like can_convert_arg, but allows dubious conversions as well. */
10687 bool
10688 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
10689 tsubst_flags_t complain)
10691 conversion *t;
10692 void *p;
10694 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10695 p = conversion_obstack_alloc (0);
10696 /* Try to perform the conversion. */
10697 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
10698 flags, complain);
10699 /* Free all the conversions we allocated. */
10700 obstack_free (&conversion_obstack, p);
10702 return t != NULL;
10705 /* Convert EXPR to TYPE. Return the converted expression.
10707 Note that we allow bad conversions here because by the time we get to
10708 this point we are committed to doing the conversion. If we end up
10709 doing a bad conversion, convert_like will complain. */
10711 tree
10712 perform_implicit_conversion_flags (tree type, tree expr,
10713 tsubst_flags_t complain, int flags)
10715 conversion *conv;
10716 void *p;
10717 location_t loc = cp_expr_loc_or_loc (expr, input_location);
10719 if (TYPE_REF_P (type))
10720 expr = mark_lvalue_use (expr);
10721 else
10722 expr = mark_rvalue_use (expr);
10724 if (error_operand_p (expr))
10725 return error_mark_node;
10727 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10728 p = conversion_obstack_alloc (0);
10730 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10731 /*c_cast_p=*/false,
10732 flags, complain);
10734 if (!conv)
10736 if (complain & tf_error)
10738 /* If expr has unknown type, then it is an overloaded function.
10739 Call instantiate_type to get good error messages. */
10740 if (TREE_TYPE (expr) == unknown_type_node)
10741 instantiate_type (type, expr, complain);
10742 else if (invalid_nonstatic_memfn_p (loc, expr, complain))
10743 /* We gave an error. */;
10744 else
10745 error_at (loc, "could not convert %qE from %qH to %qI", expr,
10746 TREE_TYPE (expr), type);
10748 expr = error_mark_node;
10750 else if (processing_template_decl && conv->kind != ck_identity)
10752 /* In a template, we are only concerned about determining the
10753 type of non-dependent expressions, so we do not have to
10754 perform the actual conversion. But for initializers, we
10755 need to be able to perform it at instantiation
10756 (or instantiate_non_dependent_expr) time. */
10757 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
10758 if (!(flags & LOOKUP_ONLYCONVERTING))
10759 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
10761 else
10762 expr = convert_like (conv, expr, complain);
10764 /* Free all the conversions we allocated. */
10765 obstack_free (&conversion_obstack, p);
10767 return expr;
10770 tree
10771 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
10773 return perform_implicit_conversion_flags (type, expr, complain,
10774 LOOKUP_IMPLICIT);
10777 /* Convert EXPR to TYPE (as a direct-initialization) if that is
10778 permitted. If the conversion is valid, the converted expression is
10779 returned. Otherwise, NULL_TREE is returned, except in the case
10780 that TYPE is a class type; in that case, an error is issued. If
10781 C_CAST_P is true, then this direct-initialization is taking
10782 place as part of a static_cast being attempted as part of a C-style
10783 cast. */
10785 tree
10786 perform_direct_initialization_if_possible (tree type,
10787 tree expr,
10788 bool c_cast_p,
10789 tsubst_flags_t complain)
10791 conversion *conv;
10792 void *p;
10794 if (type == error_mark_node || error_operand_p (expr))
10795 return error_mark_node;
10796 /* [dcl.init]
10798 If the destination type is a (possibly cv-qualified) class type:
10800 -- If the initialization is direct-initialization ...,
10801 constructors are considered. ... If no constructor applies, or
10802 the overload resolution is ambiguous, the initialization is
10803 ill-formed. */
10804 if (CLASS_TYPE_P (type))
10806 vec<tree, va_gc> *args = make_tree_vector_single (expr);
10807 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
10808 &args, type, LOOKUP_NORMAL, complain);
10809 release_tree_vector (args);
10810 return build_cplus_new (type, expr, complain);
10813 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10814 p = conversion_obstack_alloc (0);
10816 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10817 c_cast_p,
10818 LOOKUP_NORMAL, complain);
10819 if (!conv || conv->bad_p)
10820 expr = NULL_TREE;
10821 else if (processing_template_decl && conv->kind != ck_identity)
10823 /* In a template, we are only concerned about determining the
10824 type of non-dependent expressions, so we do not have to
10825 perform the actual conversion. But for initializers, we
10826 need to be able to perform it at instantiation
10827 (or instantiate_non_dependent_expr) time. */
10828 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
10829 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
10831 else
10832 expr = convert_like_real (conv, expr, NULL_TREE, 0,
10833 /*issue_conversion_warnings=*/false,
10834 c_cast_p,
10835 complain);
10837 /* Free all the conversions we allocated. */
10838 obstack_free (&conversion_obstack, p);
10840 return expr;
10843 /* When initializing a reference that lasts longer than a full-expression,
10844 this special rule applies:
10846 [class.temporary]
10848 The temporary to which the reference is bound or the temporary
10849 that is the complete object to which the reference is bound
10850 persists for the lifetime of the reference.
10852 The temporaries created during the evaluation of the expression
10853 initializing the reference, except the temporary to which the
10854 reference is bound, are destroyed at the end of the
10855 full-expression in which they are created.
10857 In that case, we store the converted expression into a new
10858 VAR_DECL in a new scope.
10860 However, we want to be careful not to create temporaries when
10861 they are not required. For example, given:
10863 struct B {};
10864 struct D : public B {};
10865 D f();
10866 const B& b = f();
10868 there is no need to copy the return value from "f"; we can just
10869 extend its lifetime. Similarly, given:
10871 struct S {};
10872 struct T { operator S(); };
10873 T t;
10874 const S& s = t;
10876 we can extend the lifetime of the return value of the conversion
10877 operator.
10879 The next several functions are involved in this lifetime extension. */
10881 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
10882 reference is being bound to a temporary. Create and return a new
10883 VAR_DECL with the indicated TYPE; this variable will store the value to
10884 which the reference is bound. */
10886 tree
10887 make_temporary_var_for_ref_to_temp (tree decl, tree type)
10889 tree var = create_temporary_var (type);
10891 /* Register the variable. */
10892 if (VAR_P (decl)
10893 && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
10895 /* Namespace-scope or local static; give it a mangled name. */
10896 /* FIXME share comdat with decl? */
10898 TREE_STATIC (var) = TREE_STATIC (decl);
10899 CP_DECL_THREAD_LOCAL_P (var) = CP_DECL_THREAD_LOCAL_P (decl);
10900 set_decl_tls_model (var, DECL_TLS_MODEL (decl));
10902 tree name = mangle_ref_init_variable (decl);
10903 DECL_NAME (var) = name;
10904 SET_DECL_ASSEMBLER_NAME (var, name);
10906 var = pushdecl (var);
10908 else
10909 /* Create a new cleanup level if necessary. */
10910 maybe_push_cleanup_level (type);
10912 return var;
10915 /* EXPR is the initializer for a variable DECL of reference or
10916 std::initializer_list type. Create, push and return a new VAR_DECL
10917 for the initializer so that it will live as long as DECL. Any
10918 cleanup for the new variable is returned through CLEANUP, and the
10919 code to initialize the new variable is returned through INITP. */
10921 static tree
10922 set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
10923 tree *initp)
10925 tree init;
10926 tree type;
10927 tree var;
10929 /* Create the temporary variable. */
10930 type = TREE_TYPE (expr);
10931 var = make_temporary_var_for_ref_to_temp (decl, type);
10932 layout_decl (var, 0);
10933 /* If the rvalue is the result of a function call it will be
10934 a TARGET_EXPR. If it is some other construct (such as a
10935 member access expression where the underlying object is
10936 itself the result of a function call), turn it into a
10937 TARGET_EXPR here. It is important that EXPR be a
10938 TARGET_EXPR below since otherwise the INIT_EXPR will
10939 attempt to make a bitwise copy of EXPR to initialize
10940 VAR. */
10941 if (TREE_CODE (expr) != TARGET_EXPR)
10942 expr = get_target_expr (expr);
10944 if (TREE_CODE (decl) == FIELD_DECL
10945 && extra_warnings && !TREE_NO_WARNING (decl))
10947 warning (OPT_Wextra, "a temporary bound to %qD only persists "
10948 "until the constructor exits", decl);
10949 TREE_NO_WARNING (decl) = true;
10952 /* Recursively extend temps in this initializer. */
10953 TARGET_EXPR_INITIAL (expr)
10954 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups);
10956 /* Any reference temp has a non-trivial initializer. */
10957 DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
10959 /* If the initializer is constant, put it in DECL_INITIAL so we get
10960 static initialization and use in constant expressions. */
10961 init = maybe_constant_init (expr);
10962 /* As in store_init_value. */
10963 init = cp_fully_fold (init);
10964 if (TREE_CONSTANT (init))
10966 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
10968 /* 5.19 says that a constant expression can include an
10969 lvalue-rvalue conversion applied to "a glvalue of literal type
10970 that refers to a non-volatile temporary object initialized
10971 with a constant expression". Rather than try to communicate
10972 that this VAR_DECL is a temporary, just mark it constexpr. */
10973 DECL_DECLARED_CONSTEXPR_P (var) = true;
10974 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
10975 TREE_CONSTANT (var) = true;
10976 TREE_READONLY (var) = true;
10978 DECL_INITIAL (var) = init;
10979 init = NULL_TREE;
10981 else
10982 /* Create the INIT_EXPR that will initialize the temporary
10983 variable. */
10984 init = split_nonconstant_init (var, expr);
10985 if (at_function_scope_p ())
10987 add_decl_expr (var);
10989 if (TREE_STATIC (var))
10990 init = add_stmt_to_compound (init, register_dtor_fn (var));
10991 else
10993 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
10994 if (cleanup)
10995 vec_safe_push (*cleanups, cleanup);
10998 /* We must be careful to destroy the temporary only
10999 after its initialization has taken place. If the
11000 initialization throws an exception, then the
11001 destructor should not be run. We cannot simply
11002 transform INIT into something like:
11004 (INIT, ({ CLEANUP_STMT; }))
11006 because emit_local_var always treats the
11007 initializer as a full-expression. Thus, the
11008 destructor would run too early; it would run at the
11009 end of initializing the reference variable, rather
11010 than at the end of the block enclosing the
11011 reference variable.
11013 The solution is to pass back a cleanup expression
11014 which the caller is responsible for attaching to
11015 the statement tree. */
11017 else
11019 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
11020 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
11022 if (CP_DECL_THREAD_LOCAL_P (var))
11023 tls_aggregates = tree_cons (NULL_TREE, var,
11024 tls_aggregates);
11025 else
11026 static_aggregates = tree_cons (NULL_TREE, var,
11027 static_aggregates);
11029 else
11030 /* Check whether the dtor is callable. */
11031 cxx_maybe_build_cleanup (var, tf_warning_or_error);
11033 /* Avoid -Wunused-variable warning (c++/38958). */
11034 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
11035 && VAR_P (decl))
11036 TREE_USED (decl) = DECL_READ_P (decl) = true;
11038 *initp = init;
11039 return var;
11042 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
11043 initializing a variable of that TYPE. */
11045 tree
11046 initialize_reference (tree type, tree expr,
11047 int flags, tsubst_flags_t complain)
11049 conversion *conv;
11050 void *p;
11051 location_t loc = cp_expr_loc_or_loc (expr, input_location);
11053 if (type == error_mark_node || error_operand_p (expr))
11054 return error_mark_node;
11056 /* Get the high-water mark for the CONVERSION_OBSTACK. */
11057 p = conversion_obstack_alloc (0);
11059 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
11060 flags, complain);
11061 if (!conv || conv->bad_p)
11063 if (complain & tf_error)
11065 if (conv)
11066 convert_like (conv, expr, complain);
11067 else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
11068 && !TYPE_REF_IS_RVALUE (type)
11069 && !lvalue_p (expr))
11070 error_at (loc, "invalid initialization of non-const reference of "
11071 "type %qH from an rvalue of type %qI",
11072 type, TREE_TYPE (expr));
11073 else
11074 error_at (loc, "invalid initialization of reference of type "
11075 "%qH from expression of type %qI", type,
11076 TREE_TYPE (expr));
11078 return error_mark_node;
11081 if (conv->kind == ck_ref_bind)
11082 /* Perform the conversion. */
11083 expr = convert_like (conv, expr, complain);
11084 else if (conv->kind == ck_ambig)
11085 /* We gave an error in build_user_type_conversion_1. */
11086 expr = error_mark_node;
11087 else
11088 gcc_unreachable ();
11090 /* Free all the conversions we allocated. */
11091 obstack_free (&conversion_obstack, p);
11093 return expr;
11096 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
11097 which is bound either to a reference or a std::initializer_list. */
11099 static tree
11100 extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups)
11102 tree sub = init;
11103 tree *p;
11104 STRIP_NOPS (sub);
11105 if (TREE_CODE (sub) == COMPOUND_EXPR)
11107 TREE_OPERAND (sub, 1)
11108 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups);
11109 return init;
11111 if (TREE_CODE (sub) != ADDR_EXPR)
11112 return init;
11113 /* Deal with binding to a subobject. */
11114 for (p = &TREE_OPERAND (sub, 0);
11115 (TREE_CODE (*p) == COMPONENT_REF
11116 || TREE_CODE (*p) == ARRAY_REF); )
11117 p = &TREE_OPERAND (*p, 0);
11118 if (TREE_CODE (*p) == TARGET_EXPR)
11120 tree subinit = NULL_TREE;
11121 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit);
11122 recompute_tree_invariant_for_addr_expr (sub);
11123 if (init != sub)
11124 init = fold_convert (TREE_TYPE (init), sub);
11125 if (subinit)
11126 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
11128 return init;
11131 /* INIT is part of the initializer for DECL. If there are any
11132 reference or initializer lists being initialized, extend their
11133 lifetime to match that of DECL. */
11135 tree
11136 extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
11138 tree type = TREE_TYPE (init);
11139 if (processing_template_decl)
11140 return init;
11141 if (TYPE_REF_P (type))
11142 init = extend_ref_init_temps_1 (decl, init, cleanups);
11143 else
11145 tree ctor = init;
11146 if (TREE_CODE (ctor) == TARGET_EXPR)
11147 ctor = TARGET_EXPR_INITIAL (ctor);
11148 if (TREE_CODE (ctor) == CONSTRUCTOR)
11150 if (is_std_init_list (type))
11152 /* The temporary array underlying a std::initializer_list
11153 is handled like a reference temporary. */
11154 tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
11155 array = extend_ref_init_temps_1 (decl, array, cleanups);
11156 CONSTRUCTOR_ELT (ctor, 0)->value = array;
11158 else
11160 unsigned i;
11161 constructor_elt *p;
11162 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
11163 FOR_EACH_VEC_SAFE_ELT (elts, i, p)
11164 p->value = extend_ref_init_temps (decl, p->value, cleanups);
11166 recompute_constructor_flags (ctor);
11167 if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
11168 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
11172 return init;
11175 /* Returns true iff an initializer for TYPE could contain temporaries that
11176 need to be extended because they are bound to references or
11177 std::initializer_list. */
11179 bool
11180 type_has_extended_temps (tree type)
11182 type = strip_array_types (type);
11183 if (TYPE_REF_P (type))
11184 return true;
11185 if (CLASS_TYPE_P (type))
11187 if (is_std_init_list (type))
11188 return true;
11189 for (tree f = next_initializable_field (TYPE_FIELDS (type));
11190 f; f = next_initializable_field (DECL_CHAIN (f)))
11191 if (type_has_extended_temps (TREE_TYPE (f)))
11192 return true;
11194 return false;
11197 /* Returns true iff TYPE is some variant of std::initializer_list. */
11199 bool
11200 is_std_init_list (tree type)
11202 if (!TYPE_P (type))
11203 return false;
11204 if (cxx_dialect == cxx98)
11205 return false;
11206 /* Look through typedefs. */
11207 type = TYPE_MAIN_VARIANT (type);
11208 return (CLASS_TYPE_P (type)
11209 && CP_TYPE_CONTEXT (type) == std_node
11210 && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
11213 /* Returns true iff DECL is a list constructor: i.e. a constructor which
11214 will accept an argument list of a single std::initializer_list<T>. */
11216 bool
11217 is_list_ctor (tree decl)
11219 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
11220 tree arg;
11222 if (!args || args == void_list_node)
11223 return false;
11225 arg = non_reference (TREE_VALUE (args));
11226 if (!is_std_init_list (arg))
11227 return false;
11229 args = TREE_CHAIN (args);
11231 if (args && args != void_list_node && !TREE_PURPOSE (args))
11232 /* There are more non-defaulted parms. */
11233 return false;
11235 return true;
11238 #include "gt-cp-call.h"