ChangeLog entry:
[official-gcc.git] / gcc / cp / call.c
blobe072891f9271c2c69d3d9f211ccbaed2e5afa235
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 2010, 2011, 2012
5 Free Software Foundation, Inc.
6 Contributed by Michael Tiemann (tiemann@cygnus.com) and
7 modified by Brendan Kehoe (brendan@cygnus.com).
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3, or (at your option)
14 any later version.
16 GCC is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* High-level class interface. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "cp-tree.h"
34 #include "output.h"
35 #include "flags.h"
36 #include "toplev.h"
37 #include "diagnostic-core.h"
38 #include "intl.h"
39 #include "target.h"
40 #include "convert.h"
41 #include "langhooks.h"
42 #include "c-family/c-objc.h"
43 #include "timevar.h"
45 /* The various kinds of conversion. */
47 typedef enum conversion_kind {
48 ck_identity,
49 ck_lvalue,
50 ck_qual,
51 ck_std,
52 ck_ptr,
53 ck_pmem,
54 ck_base,
55 ck_ref_bind,
56 ck_user,
57 ck_ambig,
58 ck_list,
59 ck_aggr,
60 ck_rvalue
61 } conversion_kind;
63 /* The rank of the conversion. Order of the enumerals matters; better
64 conversions should come earlier in the list. */
66 typedef enum conversion_rank {
67 cr_identity,
68 cr_exact,
69 cr_promotion,
70 cr_std,
71 cr_pbool,
72 cr_user,
73 cr_ellipsis,
74 cr_bad
75 } conversion_rank;
77 /* An implicit conversion sequence, in the sense of [over.best.ics].
78 The first conversion to be performed is at the end of the chain.
79 That conversion is always a cr_identity conversion. */
81 typedef struct conversion conversion;
82 struct conversion {
83 /* The kind of conversion represented by this step. */
84 conversion_kind kind;
85 /* The rank of this conversion. */
86 conversion_rank rank;
87 BOOL_BITFIELD user_conv_p : 1;
88 BOOL_BITFIELD ellipsis_p : 1;
89 BOOL_BITFIELD this_p : 1;
90 /* True if this conversion would be permitted with a bending of
91 language standards, e.g. disregarding pointer qualifiers or
92 converting integers to pointers. */
93 BOOL_BITFIELD bad_p : 1;
94 /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
95 temporary should be created to hold the result of the
96 conversion. */
97 BOOL_BITFIELD need_temporary_p : 1;
98 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
99 from a pointer-to-derived to pointer-to-base is being performed. */
100 BOOL_BITFIELD base_p : 1;
101 /* If KIND is ck_ref_bind, true when either an lvalue reference is
102 being bound to an lvalue expression or an rvalue reference is
103 being bound to an rvalue expression. If KIND is ck_rvalue,
104 true when we should treat an lvalue as an rvalue (12.8p33). If
105 KIND is ck_base, always false. */
106 BOOL_BITFIELD rvaluedness_matches_p: 1;
107 BOOL_BITFIELD check_narrowing: 1;
108 /* The type of the expression resulting from the conversion. */
109 tree type;
110 union {
111 /* The next conversion in the chain. Since the conversions are
112 arranged from outermost to innermost, the NEXT conversion will
113 actually be performed before this conversion. This variant is
114 used only when KIND is neither ck_identity, ck_ambig nor
115 ck_list. Please use the next_conversion function instead
116 of using this field directly. */
117 conversion *next;
118 /* The expression at the beginning of the conversion chain. This
119 variant is used only if KIND is ck_identity or ck_ambig. */
120 tree expr;
121 /* The array of conversions for an initializer_list, so this
122 variant is used only when KIN D is ck_list. */
123 conversion **list;
124 } u;
125 /* The function candidate corresponding to this conversion
126 sequence. This field is only used if KIND is ck_user. */
127 struct z_candidate *cand;
130 #define CONVERSION_RANK(NODE) \
131 ((NODE)->bad_p ? cr_bad \
132 : (NODE)->ellipsis_p ? cr_ellipsis \
133 : (NODE)->user_conv_p ? cr_user \
134 : (NODE)->rank)
136 #define BAD_CONVERSION_RANK(NODE) \
137 ((NODE)->ellipsis_p ? cr_ellipsis \
138 : (NODE)->user_conv_p ? cr_user \
139 : (NODE)->rank)
141 static struct obstack conversion_obstack;
142 static bool conversion_obstack_initialized;
143 struct rejection_reason;
145 static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
146 static int equal_functions (tree, tree);
147 static int joust (struct z_candidate *, struct z_candidate *, bool,
148 tsubst_flags_t);
149 static int compare_ics (conversion *, conversion *);
150 static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
151 static tree build_java_interface_fn_ref (tree, tree);
152 #define convert_like(CONV, EXPR, COMPLAIN) \
153 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \
154 /*issue_conversion_warnings=*/true, \
155 /*c_cast_p=*/false, (COMPLAIN))
156 #define convert_like_with_context(CONV, EXPR, FN, ARGNO, COMPLAIN ) \
157 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
158 /*issue_conversion_warnings=*/true, \
159 /*c_cast_p=*/false, (COMPLAIN))
160 static tree convert_like_real (conversion *, tree, tree, int, int, bool,
161 bool, tsubst_flags_t);
162 static void op_error (enum tree_code, enum tree_code, tree, tree,
163 tree, bool);
164 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
165 tsubst_flags_t);
166 static void print_z_candidate (const char *, struct z_candidate *);
167 static void print_z_candidates (location_t, struct z_candidate *);
168 static tree build_this (tree);
169 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
170 static bool any_strictly_viable (struct z_candidate *);
171 static struct z_candidate *add_template_candidate
172 (struct z_candidate **, tree, tree, tree, tree, const VEC(tree,gc) *,
173 tree, tree, tree, int, unification_kind_t, tsubst_flags_t);
174 static struct z_candidate *add_template_candidate_real
175 (struct z_candidate **, tree, tree, tree, tree, const VEC(tree,gc) *,
176 tree, tree, tree, int, tree, unification_kind_t, tsubst_flags_t);
177 static struct z_candidate *add_template_conv_candidate
178 (struct z_candidate **, tree, tree, tree, const VEC(tree,gc) *, tree,
179 tree, tree, 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, tree, const VEC(tree,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,gc) *, tree,
195 tree, int, tsubst_flags_t);
196 static conversion *implicit_conversion (tree, tree, tree, bool, int,
197 tsubst_flags_t);
198 static conversion *standard_conversion (tree, tree, tree, bool, int);
199 static conversion *reference_binding (tree, tree, tree, bool, int,
200 tsubst_flags_t);
201 static conversion *build_conv (conversion_kind, tree, conversion *);
202 static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
203 static conversion *next_conversion (conversion *);
204 static bool is_subseq (conversion *, conversion *);
205 static conversion *maybe_handle_ref_bind (conversion **);
206 static void maybe_handle_implicit_object (conversion **);
207 static struct z_candidate *add_candidate
208 (struct z_candidate **, tree, tree, const VEC(tree,gc) *, size_t,
209 conversion **, tree, tree, int, struct rejection_reason *);
210 static tree source_type (conversion *);
211 static void add_warning (struct z_candidate *, struct z_candidate *);
212 static bool reference_compatible_p (tree, tree);
213 static conversion *direct_reference_binding (tree, conversion *);
214 static bool promoted_arithmetic_type_p (tree);
215 static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
216 static char *name_as_c_string (tree, tree, bool *);
217 static tree prep_operand (tree);
218 static void add_candidates (tree, tree, const VEC(tree,gc) *, tree, tree, bool,
219 tree, tree, int, struct z_candidate **,
220 tsubst_flags_t);
221 static conversion *merge_conversion_sequences (conversion *, conversion *);
222 static bool magic_varargs_p (tree);
223 static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
225 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
226 NAME can take many forms... */
228 bool
229 check_dtor_name (tree basetype, tree name)
231 /* Just accept something we've already complained about. */
232 if (name == error_mark_node)
233 return true;
235 if (TREE_CODE (name) == TYPE_DECL)
236 name = TREE_TYPE (name);
237 else if (TYPE_P (name))
238 /* OK */;
239 else if (TREE_CODE (name) == IDENTIFIER_NODE)
241 if ((MAYBE_CLASS_TYPE_P (basetype)
242 && name == constructor_name (basetype))
243 || (TREE_CODE (basetype) == ENUMERAL_TYPE
244 && name == TYPE_IDENTIFIER (basetype)))
245 return true;
246 else
247 name = get_type_value (name);
249 else
251 /* In the case of:
253 template <class T> struct S { ~S(); };
254 int i;
255 i.~S();
257 NAME will be a class template. */
258 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
259 return false;
262 if (!name || name == error_mark_node)
263 return false;
264 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
267 /* We want the address of a function or method. We avoid creating a
268 pointer-to-member function. */
270 tree
271 build_addr_func (tree function, tsubst_flags_t complain)
273 tree type = TREE_TYPE (function);
275 /* We have to do these by hand to avoid real pointer to member
276 functions. */
277 if (TREE_CODE (type) == METHOD_TYPE)
279 if (TREE_CODE (function) == OFFSET_REF)
281 tree object = build_address (TREE_OPERAND (function, 0));
282 return get_member_function_from_ptrfunc (&object,
283 TREE_OPERAND (function, 1),
284 complain);
286 function = build_address (function);
288 else
289 function = decay_conversion (function, complain);
291 return function;
294 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
295 POINTER_TYPE to those. Note, pointer to member function types
296 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
297 two variants. build_call_a is the primitive taking an array of
298 arguments, while build_call_n is a wrapper that handles varargs. */
300 tree
301 build_call_n (tree function, int n, ...)
303 if (n == 0)
304 return build_call_a (function, 0, NULL);
305 else
307 tree *argarray = XALLOCAVEC (tree, n);
308 va_list ap;
309 int i;
311 va_start (ap, n);
312 for (i = 0; i < n; i++)
313 argarray[i] = va_arg (ap, tree);
314 va_end (ap);
315 return build_call_a (function, n, argarray);
319 /* Update various flags in cfun and the call itself based on what is being
320 called. Split out of build_call_a so that bot_manip can use it too. */
322 void
323 set_flags_from_callee (tree call)
325 int nothrow;
326 tree decl = get_callee_fndecl (call);
328 /* We check both the decl and the type; a function may be known not to
329 throw without being declared throw(). */
330 nothrow = ((decl && TREE_NOTHROW (decl))
331 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (call)))));
333 if (!nothrow && at_function_scope_p () && cfun && cp_function_chain)
334 cp_function_chain->can_throw = 1;
336 if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
337 current_function_returns_abnormally = 1;
339 TREE_NOTHROW (call) = nothrow;
342 tree
343 build_call_a (tree function, int n, tree *argarray)
345 tree decl;
346 tree result_type;
347 tree fntype;
348 int i;
350 function = build_addr_func (function, tf_warning_or_error);
352 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
353 fntype = TREE_TYPE (TREE_TYPE (function));
354 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
355 || TREE_CODE (fntype) == METHOD_TYPE);
356 result_type = TREE_TYPE (fntype);
357 /* An rvalue has no cv-qualifiers. */
358 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
359 result_type = cv_unqualified (result_type);
361 function = build_call_array_loc (input_location,
362 result_type, function, n, argarray);
363 set_flags_from_callee (function);
365 decl = get_callee_fndecl (function);
367 if (decl && !TREE_USED (decl))
369 /* We invoke build_call directly for several library
370 functions. These may have been declared normally if
371 we're building libgcc, so we can't just check
372 DECL_ARTIFICIAL. */
373 gcc_assert (DECL_ARTIFICIAL (decl)
374 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
375 "__", 2));
376 mark_used (decl);
379 if (decl && TREE_DEPRECATED (decl))
380 warn_deprecated_use (decl, NULL_TREE);
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 /* Build something of the form ptr->method (args)
405 or object.method (args). This can also build
406 calls to constructors, and find friends.
408 Member functions always take their class variable
409 as a pointer.
411 INSTANCE is a class instance.
413 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
415 PARMS help to figure out what that NAME really refers to.
417 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
418 down to the real instance type to use for access checking. We need this
419 information to get protected accesses correct.
421 FLAGS is the logical disjunction of zero or more LOOKUP_
422 flags. See cp-tree.h for more info.
424 If this is all OK, calls build_function_call with the resolved
425 member function.
427 This function must also handle being called to perform
428 initialization, promotion/coercion of arguments, and
429 instantiation of default parameters.
431 Note that NAME may refer to an instance variable name. If
432 `operator()()' is defined for the type of that field, then we return
433 that result. */
435 /* New overloading code. */
437 typedef struct z_candidate z_candidate;
439 typedef struct candidate_warning candidate_warning;
440 struct candidate_warning {
441 z_candidate *loser;
442 candidate_warning *next;
445 /* Information for providing diagnostics about why overloading failed. */
447 enum rejection_reason_code {
448 rr_none,
449 rr_arity,
450 rr_explicit_conversion,
451 rr_template_conversion,
452 rr_arg_conversion,
453 rr_bad_arg_conversion,
454 rr_template_unification,
455 rr_template_instantiation,
456 rr_invalid_copy
459 struct conversion_info {
460 /* The index of the argument, 0-based. */
461 int n_arg;
462 /* The type of the actual argument. */
463 tree from_type;
464 /* The type of the formal argument. */
465 tree to_type;
468 struct rejection_reason {
469 enum rejection_reason_code code;
470 union {
471 /* Information about an arity mismatch. */
472 struct {
473 /* The expected number of arguments. */
474 int expected;
475 /* The actual number of arguments in the call. */
476 int actual;
477 /* Whether the call was a varargs call. */
478 bool call_varargs_p;
479 } arity;
480 /* Information about an argument conversion mismatch. */
481 struct conversion_info conversion;
482 /* Same, but for bad argument conversions. */
483 struct conversion_info bad_conversion;
484 /* Information about template unification failures. These are the
485 parameters passed to fn_type_unification. */
486 struct {
487 tree tmpl;
488 tree explicit_targs;
489 tree targs;
490 const tree *args;
491 unsigned int nargs;
492 tree return_type;
493 unification_kind_t strict;
494 int flags;
495 } template_unification;
496 /* Information about template instantiation failures. These are the
497 parameters passed to instantiate_template. */
498 struct {
499 tree tmpl;
500 tree targs;
501 } template_instantiation;
502 } u;
505 struct z_candidate {
506 /* The FUNCTION_DECL that will be called if this candidate is
507 selected by overload resolution. */
508 tree fn;
509 /* If not NULL_TREE, the first argument to use when calling this
510 function. */
511 tree first_arg;
512 /* The rest of the arguments to use when calling this function. If
513 there are no further arguments this may be NULL or it may be an
514 empty vector. */
515 const VEC(tree,gc) *args;
516 /* The implicit conversion sequences for each of the arguments to
517 FN. */
518 conversion **convs;
519 /* The number of implicit conversion sequences. */
520 size_t num_convs;
521 /* If FN is a user-defined conversion, the standard conversion
522 sequence from the type returned by FN to the desired destination
523 type. */
524 conversion *second_conv;
525 int viable;
526 struct rejection_reason *reason;
527 /* If FN is a member function, the binfo indicating the path used to
528 qualify the name of FN at the call site. This path is used to
529 determine whether or not FN is accessible if it is selected by
530 overload resolution. The DECL_CONTEXT of FN will always be a
531 (possibly improper) base of this binfo. */
532 tree access_path;
533 /* If FN is a non-static member function, the binfo indicating the
534 subobject to which the `this' pointer should be converted if FN
535 is selected by overload resolution. The type pointed to by
536 the `this' pointer must correspond to the most derived class
537 indicated by the CONVERSION_PATH. */
538 tree conversion_path;
539 tree template_decl;
540 tree explicit_targs;
541 candidate_warning *warnings;
542 z_candidate *next;
545 /* Returns true iff T is a null pointer constant in the sense of
546 [conv.ptr]. */
548 bool
549 null_ptr_cst_p (tree t)
551 /* [conv.ptr]
553 A null pointer constant is an integral constant expression
554 (_expr.const_) rvalue of integer type that evaluates to zero or
555 an rvalue of type std::nullptr_t. */
556 if (NULLPTR_TYPE_P (TREE_TYPE (t)))
557 return true;
558 if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)))
560 /* Core issue 903 says only literal 0 is a null pointer constant. */
561 if (cxx_dialect < cxx0x)
562 t = integral_constant_value (t);
563 STRIP_NOPS (t);
564 if (integer_zerop (t) && !TREE_OVERFLOW (t))
565 return true;
567 return false;
570 /* Returns true iff T is a null member pointer value (4.11). */
572 bool
573 null_member_pointer_value_p (tree t)
575 tree type = TREE_TYPE (t);
576 if (!type)
577 return false;
578 else if (TYPE_PTRMEMFUNC_P (type))
579 return (TREE_CODE (t) == CONSTRUCTOR
580 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
581 else if (TYPE_PTRMEM_P (type))
582 return integer_all_onesp (t);
583 else
584 return false;
587 /* Returns nonzero if PARMLIST consists of only default parms,
588 ellipsis, and/or undeduced parameter packs. */
590 bool
591 sufficient_parms_p (const_tree parmlist)
593 for (; parmlist && parmlist != void_list_node;
594 parmlist = TREE_CHAIN (parmlist))
595 if (!TREE_PURPOSE (parmlist)
596 && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
597 return false;
598 return true;
601 /* Allocate N bytes of memory from the conversion obstack. The memory
602 is zeroed before being returned. */
604 static void *
605 conversion_obstack_alloc (size_t n)
607 void *p;
608 if (!conversion_obstack_initialized)
610 gcc_obstack_init (&conversion_obstack);
611 conversion_obstack_initialized = true;
613 p = obstack_alloc (&conversion_obstack, n);
614 memset (p, 0, n);
615 return p;
618 /* Allocate rejection reasons. */
620 static struct rejection_reason *
621 alloc_rejection (enum rejection_reason_code code)
623 struct rejection_reason *p;
624 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
625 p->code = code;
626 return p;
629 static struct rejection_reason *
630 arity_rejection (tree first_arg, int expected, int actual)
632 struct rejection_reason *r = alloc_rejection (rr_arity);
633 int adjust = first_arg != NULL_TREE;
634 r->u.arity.expected = expected - adjust;
635 r->u.arity.actual = actual - adjust;
636 return r;
639 static struct rejection_reason *
640 arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
642 struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
643 int adjust = first_arg != NULL_TREE;
644 r->u.conversion.n_arg = n_arg - adjust;
645 r->u.conversion.from_type = from;
646 r->u.conversion.to_type = to;
647 return r;
650 static struct rejection_reason *
651 bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
653 struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
654 int adjust = first_arg != NULL_TREE;
655 r->u.bad_conversion.n_arg = n_arg - adjust;
656 r->u.bad_conversion.from_type = from;
657 r->u.bad_conversion.to_type = to;
658 return r;
661 static struct rejection_reason *
662 explicit_conversion_rejection (tree from, tree to)
664 struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
665 r->u.conversion.n_arg = 0;
666 r->u.conversion.from_type = from;
667 r->u.conversion.to_type = to;
668 return r;
671 static struct rejection_reason *
672 template_conversion_rejection (tree from, tree to)
674 struct rejection_reason *r = alloc_rejection (rr_template_conversion);
675 r->u.conversion.n_arg = 0;
676 r->u.conversion.from_type = from;
677 r->u.conversion.to_type = to;
678 return r;
681 static struct rejection_reason *
682 template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
683 const tree *args, unsigned int nargs,
684 tree return_type, unification_kind_t strict,
685 int flags)
687 size_t args_n_bytes = sizeof (*args) * nargs;
688 tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
689 struct rejection_reason *r = alloc_rejection (rr_template_unification);
690 r->u.template_unification.tmpl = tmpl;
691 r->u.template_unification.explicit_targs = explicit_targs;
692 r->u.template_unification.targs = targs;
693 /* Copy args to our own storage. */
694 memcpy (args1, args, args_n_bytes);
695 r->u.template_unification.args = args1;
696 r->u.template_unification.nargs = nargs;
697 r->u.template_unification.return_type = return_type;
698 r->u.template_unification.strict = strict;
699 r->u.template_unification.flags = flags;
700 return r;
703 static struct rejection_reason *
704 template_unification_error_rejection (void)
706 return alloc_rejection (rr_template_unification);
709 static struct rejection_reason *
710 template_instantiation_rejection (tree tmpl, tree targs)
712 struct rejection_reason *r = alloc_rejection (rr_template_instantiation);
713 r->u.template_instantiation.tmpl = tmpl;
714 r->u.template_instantiation.targs = targs;
715 return r;
718 static struct rejection_reason *
719 invalid_copy_with_fn_template_rejection (void)
721 struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
722 return r;
725 /* Dynamically allocate a conversion. */
727 static conversion *
728 alloc_conversion (conversion_kind kind)
730 conversion *c;
731 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
732 c->kind = kind;
733 return c;
736 #ifdef ENABLE_CHECKING
738 /* Make sure that all memory on the conversion obstack has been
739 freed. */
741 void
742 validate_conversion_obstack (void)
744 if (conversion_obstack_initialized)
745 gcc_assert ((obstack_next_free (&conversion_obstack)
746 == obstack_base (&conversion_obstack)));
749 #endif /* ENABLE_CHECKING */
751 /* Dynamically allocate an array of N conversions. */
753 static conversion **
754 alloc_conversions (size_t n)
756 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
759 static conversion *
760 build_conv (conversion_kind code, tree type, conversion *from)
762 conversion *t;
763 conversion_rank rank = CONVERSION_RANK (from);
765 /* Note that the caller is responsible for filling in t->cand for
766 user-defined conversions. */
767 t = alloc_conversion (code);
768 t->type = type;
769 t->u.next = from;
771 switch (code)
773 case ck_ptr:
774 case ck_pmem:
775 case ck_base:
776 case ck_std:
777 if (rank < cr_std)
778 rank = cr_std;
779 break;
781 case ck_qual:
782 if (rank < cr_exact)
783 rank = cr_exact;
784 break;
786 default:
787 break;
789 t->rank = rank;
790 t->user_conv_p = (code == ck_user || from->user_conv_p);
791 t->bad_p = from->bad_p;
792 t->base_p = false;
793 return t;
796 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
797 specialization of std::initializer_list<T>, if such a conversion is
798 possible. */
800 static conversion *
801 build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
803 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
804 unsigned len = CONSTRUCTOR_NELTS (ctor);
805 conversion **subconvs = alloc_conversions (len);
806 conversion *t;
807 unsigned i;
808 tree val;
810 /* Within a list-initialization we can have more user-defined
811 conversions. */
812 flags &= ~LOOKUP_NO_CONVERSION;
813 /* But no narrowing conversions. */
814 flags |= LOOKUP_NO_NARROWING;
816 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
818 conversion *sub
819 = implicit_conversion (elttype, TREE_TYPE (val), val,
820 false, flags, complain);
821 if (sub == NULL)
822 return NULL;
824 subconvs[i] = sub;
827 t = alloc_conversion (ck_list);
828 t->type = type;
829 t->u.list = subconvs;
830 t->rank = cr_exact;
832 for (i = 0; i < len; ++i)
834 conversion *sub = subconvs[i];
835 if (sub->rank > t->rank)
836 t->rank = sub->rank;
837 if (sub->user_conv_p)
838 t->user_conv_p = true;
839 if (sub->bad_p)
840 t->bad_p = true;
843 return t;
846 /* Return the next conversion of the conversion chain (if applicable),
847 or NULL otherwise. Please use this function instead of directly
848 accessing fields of struct conversion. */
850 static conversion *
851 next_conversion (conversion *conv)
853 if (conv == NULL
854 || conv->kind == ck_identity
855 || conv->kind == ck_ambig
856 || conv->kind == ck_list)
857 return NULL;
858 return conv->u.next;
861 /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
862 is a valid aggregate initializer for array type ATYPE. */
864 static bool
865 can_convert_array (tree atype, tree ctor, int flags, tsubst_flags_t complain)
867 unsigned i;
868 tree elttype = TREE_TYPE (atype);
869 for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
871 tree val = CONSTRUCTOR_ELT (ctor, i)->value;
872 bool ok;
873 if (TREE_CODE (elttype) == ARRAY_TYPE
874 && TREE_CODE (val) == CONSTRUCTOR)
875 ok = can_convert_array (elttype, val, flags, complain);
876 else
877 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
878 complain);
879 if (!ok)
880 return false;
882 return true;
885 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
886 aggregate class, if such a conversion is possible. */
888 static conversion *
889 build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
891 unsigned HOST_WIDE_INT i = 0;
892 conversion *c;
893 tree field = next_initializable_field (TYPE_FIELDS (type));
894 tree empty_ctor = NULL_TREE;
896 ctor = reshape_init (type, ctor, tf_none);
897 if (ctor == error_mark_node)
898 return NULL;
900 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
902 tree ftype = TREE_TYPE (field);
903 tree val;
904 bool ok;
906 if (i < CONSTRUCTOR_NELTS (ctor))
907 val = CONSTRUCTOR_ELT (ctor, i)->value;
908 else
910 if (empty_ctor == NULL_TREE)
911 empty_ctor = build_constructor (init_list_type_node, NULL);
912 val = empty_ctor;
914 ++i;
916 if (TREE_CODE (ftype) == ARRAY_TYPE
917 && TREE_CODE (val) == CONSTRUCTOR)
918 ok = can_convert_array (ftype, val, flags, complain);
919 else
920 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
921 complain);
923 if (!ok)
924 return NULL;
926 if (TREE_CODE (type) == UNION_TYPE)
927 break;
930 if (i < CONSTRUCTOR_NELTS (ctor))
931 return NULL;
933 c = alloc_conversion (ck_aggr);
934 c->type = type;
935 c->rank = cr_exact;
936 c->user_conv_p = true;
937 c->u.next = NULL;
938 return c;
941 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
942 array type, if such a conversion is possible. */
944 static conversion *
945 build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
947 conversion *c;
948 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
949 tree elttype = TREE_TYPE (type);
950 unsigned i;
951 tree val;
952 bool bad = false;
953 bool user = false;
954 enum conversion_rank rank = cr_exact;
956 if (TYPE_DOMAIN (type))
958 unsigned HOST_WIDE_INT alen = tree_low_cst (array_type_nelts_top (type), 1);
959 if (alen < len)
960 return NULL;
963 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
965 conversion *sub
966 = implicit_conversion (elttype, TREE_TYPE (val), val,
967 false, flags, complain);
968 if (sub == NULL)
969 return NULL;
971 if (sub->rank > rank)
972 rank = sub->rank;
973 if (sub->user_conv_p)
974 user = true;
975 if (sub->bad_p)
976 bad = true;
979 c = alloc_conversion (ck_aggr);
980 c->type = type;
981 c->rank = rank;
982 c->user_conv_p = user;
983 c->bad_p = bad;
984 c->u.next = NULL;
985 return c;
988 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
989 complex type, if such a conversion is possible. */
991 static conversion *
992 build_complex_conv (tree type, tree ctor, int flags,
993 tsubst_flags_t complain)
995 conversion *c;
996 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
997 tree elttype = TREE_TYPE (type);
998 unsigned i;
999 tree val;
1000 bool bad = false;
1001 bool user = false;
1002 enum conversion_rank rank = cr_exact;
1004 if (len != 2)
1005 return NULL;
1007 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
1009 conversion *sub
1010 = implicit_conversion (elttype, TREE_TYPE (val), val,
1011 false, flags, complain);
1012 if (sub == NULL)
1013 return NULL;
1015 if (sub->rank > rank)
1016 rank = sub->rank;
1017 if (sub->user_conv_p)
1018 user = true;
1019 if (sub->bad_p)
1020 bad = true;
1023 c = alloc_conversion (ck_aggr);
1024 c->type = type;
1025 c->rank = rank;
1026 c->user_conv_p = user;
1027 c->bad_p = bad;
1028 c->u.next = NULL;
1029 return c;
1032 /* Build a representation of the identity conversion from EXPR to
1033 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1035 static conversion *
1036 build_identity_conv (tree type, tree expr)
1038 conversion *c;
1040 c = alloc_conversion (ck_identity);
1041 c->type = type;
1042 c->u.expr = expr;
1044 return c;
1047 /* Converting from EXPR to TYPE was ambiguous in the sense that there
1048 were multiple user-defined conversions to accomplish the job.
1049 Build a conversion that indicates that ambiguity. */
1051 static conversion *
1052 build_ambiguous_conv (tree type, tree expr)
1054 conversion *c;
1056 c = alloc_conversion (ck_ambig);
1057 c->type = type;
1058 c->u.expr = expr;
1060 return c;
1063 tree
1064 strip_top_quals (tree t)
1066 if (TREE_CODE (t) == ARRAY_TYPE)
1067 return t;
1068 return cp_build_qualified_type (t, 0);
1071 /* Returns the standard conversion path (see [conv]) from type FROM to type
1072 TO, if any. For proper handling of null pointer constants, you must
1073 also pass the expression EXPR to convert from. If C_CAST_P is true,
1074 this conversion is coming from a C-style cast. */
1076 static conversion *
1077 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1078 int flags)
1080 enum tree_code fcode, tcode;
1081 conversion *conv;
1082 bool fromref = false;
1083 tree qualified_to;
1085 to = non_reference (to);
1086 if (TREE_CODE (from) == REFERENCE_TYPE)
1088 fromref = true;
1089 from = TREE_TYPE (from);
1091 qualified_to = to;
1092 to = strip_top_quals (to);
1093 from = strip_top_quals (from);
1095 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1096 && expr && type_unknown_p (expr))
1098 tsubst_flags_t tflags = tf_conv;
1099 if (!(flags & LOOKUP_PROTECT))
1100 tflags |= tf_no_access_control;
1101 expr = instantiate_type (to, expr, tflags);
1102 if (expr == error_mark_node)
1103 return NULL;
1104 from = TREE_TYPE (expr);
1107 fcode = TREE_CODE (from);
1108 tcode = TREE_CODE (to);
1110 conv = build_identity_conv (from, expr);
1111 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1113 from = type_decays_to (from);
1114 fcode = TREE_CODE (from);
1115 conv = build_conv (ck_lvalue, from, conv);
1117 else if (fromref || (expr && lvalue_p (expr)))
1119 if (expr)
1121 tree bitfield_type;
1122 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1123 if (bitfield_type)
1125 from = strip_top_quals (bitfield_type);
1126 fcode = TREE_CODE (from);
1129 conv = build_conv (ck_rvalue, from, conv);
1130 if (flags & LOOKUP_PREFER_RVALUE)
1131 conv->rvaluedness_matches_p = true;
1134 /* Allow conversion between `__complex__' data types. */
1135 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1137 /* The standard conversion sequence to convert FROM to TO is
1138 the standard conversion sequence to perform componentwise
1139 conversion. */
1140 conversion *part_conv = standard_conversion
1141 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags);
1143 if (part_conv)
1145 conv = build_conv (part_conv->kind, to, conv);
1146 conv->rank = part_conv->rank;
1148 else
1149 conv = NULL;
1151 return conv;
1154 if (same_type_p (from, to))
1156 if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1157 conv->type = qualified_to;
1158 return conv;
1161 /* [conv.ptr]
1162 A null pointer constant can be converted to a pointer type; ... A
1163 null pointer constant of integral type can be converted to an
1164 rvalue of type std::nullptr_t. */
1165 if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to)
1166 || NULLPTR_TYPE_P (to))
1167 && expr && null_ptr_cst_p (expr))
1168 conv = build_conv (ck_std, to, conv);
1169 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1170 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1172 /* For backwards brain damage compatibility, allow interconversion of
1173 pointers and integers with a pedwarn. */
1174 conv = build_conv (ck_std, to, conv);
1175 conv->bad_p = true;
1177 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1179 /* For backwards brain damage compatibility, allow interconversion of
1180 enums and integers with a pedwarn. */
1181 conv = build_conv (ck_std, to, conv);
1182 conv->bad_p = true;
1184 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1185 || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
1187 tree to_pointee;
1188 tree from_pointee;
1190 if (tcode == POINTER_TYPE
1191 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
1192 TREE_TYPE (to)))
1194 else if (VOID_TYPE_P (TREE_TYPE (to))
1195 && !TYPE_PTRMEM_P (from)
1196 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
1198 tree nfrom = TREE_TYPE (from);
1199 from = build_pointer_type
1200 (cp_build_qualified_type (void_type_node,
1201 cp_type_quals (nfrom)));
1202 conv = build_conv (ck_ptr, from, conv);
1204 else if (TYPE_PTRMEM_P (from))
1206 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1207 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1209 if (DERIVED_FROM_P (fbase, tbase)
1210 && (same_type_ignoring_top_level_qualifiers_p
1211 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
1212 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
1214 from = build_ptrmem_type (tbase,
1215 TYPE_PTRMEM_POINTED_TO_TYPE (from));
1216 conv = build_conv (ck_pmem, from, conv);
1218 else if (!same_type_p (fbase, tbase))
1219 return NULL;
1221 else if (CLASS_TYPE_P (TREE_TYPE (from))
1222 && CLASS_TYPE_P (TREE_TYPE (to))
1223 /* [conv.ptr]
1225 An rvalue of type "pointer to cv D," where D is a
1226 class type, can be converted to an rvalue of type
1227 "pointer to cv B," where B is a base class (clause
1228 _class.derived_) of D. If B is an inaccessible
1229 (clause _class.access_) or ambiguous
1230 (_class.member.lookup_) base class of D, a program
1231 that necessitates this conversion is ill-formed.
1232 Therefore, we use DERIVED_FROM_P, and do not check
1233 access or uniqueness. */
1234 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
1236 from =
1237 cp_build_qualified_type (TREE_TYPE (to),
1238 cp_type_quals (TREE_TYPE (from)));
1239 from = build_pointer_type (from);
1240 conv = build_conv (ck_ptr, from, conv);
1241 conv->base_p = true;
1244 if (tcode == POINTER_TYPE)
1246 to_pointee = TREE_TYPE (to);
1247 from_pointee = TREE_TYPE (from);
1249 else
1251 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1252 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1255 if (same_type_p (from, to))
1256 /* OK */;
1257 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
1258 /* In a C-style cast, we ignore CV-qualification because we
1259 are allowed to perform a static_cast followed by a
1260 const_cast. */
1261 conv = build_conv (ck_qual, to, conv);
1262 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1263 conv = build_conv (ck_qual, to, conv);
1264 else if (expr && string_conv_p (to, expr, 0))
1265 /* converting from string constant to char *. */
1266 conv = build_conv (ck_qual, to, conv);
1267 /* Allow conversions among compatible ObjC pointer types (base
1268 conversions have been already handled above). */
1269 else if (c_dialect_objc ()
1270 && objc_compare_types (to, from, -4, NULL_TREE))
1271 conv = build_conv (ck_ptr, to, conv);
1272 else if (ptr_reasonably_similar (to_pointee, from_pointee))
1274 conv = build_conv (ck_ptr, to, conv);
1275 conv->bad_p = true;
1277 else
1278 return NULL;
1280 from = to;
1282 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1284 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1285 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1286 tree fbase = class_of_this_parm (fromfn);
1287 tree tbase = class_of_this_parm (tofn);
1289 if (!DERIVED_FROM_P (fbase, tbase)
1290 || !same_type_p (static_fn_type (fromfn),
1291 static_fn_type (tofn)))
1292 return NULL;
1294 from = build_memfn_type (fromfn, tbase, cp_type_quals (tbase));
1295 from = build_ptrmemfunc_type (build_pointer_type (from));
1296 conv = build_conv (ck_pmem, from, conv);
1297 conv->base_p = true;
1299 else if (tcode == BOOLEAN_TYPE)
1301 /* [conv.bool]
1303 An rvalue of arithmetic, unscoped enumeration, pointer, or
1304 pointer to member type can be converted to an rvalue of type
1305 bool. ... An rvalue of type std::nullptr_t can be converted
1306 to an rvalue of type bool; */
1307 if (ARITHMETIC_TYPE_P (from)
1308 || UNSCOPED_ENUM_P (from)
1309 || fcode == POINTER_TYPE
1310 || TYPE_PTR_TO_MEMBER_P (from)
1311 || NULLPTR_TYPE_P (from))
1313 conv = build_conv (ck_std, to, conv);
1314 if (fcode == POINTER_TYPE
1315 || TYPE_PTRMEM_P (from)
1316 || (TYPE_PTRMEMFUNC_P (from)
1317 && conv->rank < cr_pbool)
1318 || NULLPTR_TYPE_P (from))
1319 conv->rank = cr_pbool;
1320 return conv;
1323 return NULL;
1325 /* We don't check for ENUMERAL_TYPE here because there are no standard
1326 conversions to enum type. */
1327 /* As an extension, allow conversion to complex type. */
1328 else if (ARITHMETIC_TYPE_P (to))
1330 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE)
1331 || SCOPED_ENUM_P (from))
1332 return NULL;
1333 conv = build_conv (ck_std, to, conv);
1335 /* Give this a better rank if it's a promotion. */
1336 if (same_type_p (to, type_promotes_to (from))
1337 && next_conversion (conv)->rank <= cr_promotion)
1338 conv->rank = cr_promotion;
1340 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1341 && vector_types_convertible_p (from, to, false))
1342 return build_conv (ck_std, to, conv);
1343 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1344 && is_properly_derived_from (from, to))
1346 if (conv->kind == ck_rvalue)
1347 conv = next_conversion (conv);
1348 conv = build_conv (ck_base, to, conv);
1349 /* The derived-to-base conversion indicates the initialization
1350 of a parameter with base type from an object of a derived
1351 type. A temporary object is created to hold the result of
1352 the conversion unless we're binding directly to a reference. */
1353 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1355 else
1356 return NULL;
1358 if (flags & LOOKUP_NO_NARROWING)
1359 conv->check_narrowing = true;
1361 return conv;
1364 /* Returns nonzero if T1 is reference-related to T2. */
1366 bool
1367 reference_related_p (tree t1, tree t2)
1369 if (t1 == error_mark_node || t2 == error_mark_node)
1370 return false;
1372 t1 = TYPE_MAIN_VARIANT (t1);
1373 t2 = TYPE_MAIN_VARIANT (t2);
1375 /* [dcl.init.ref]
1377 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1378 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
1379 of T2. */
1380 return (same_type_p (t1, t2)
1381 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1382 && DERIVED_FROM_P (t1, t2)));
1385 /* Returns nonzero if T1 is reference-compatible with T2. */
1387 static bool
1388 reference_compatible_p (tree t1, tree t2)
1390 /* [dcl.init.ref]
1392 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
1393 reference-related to T2 and cv1 is the same cv-qualification as,
1394 or greater cv-qualification than, cv2. */
1395 return (reference_related_p (t1, t2)
1396 && at_least_as_qualified_p (t1, t2));
1399 /* A reference of the indicated TYPE is being bound directly to the
1400 expression represented by the implicit conversion sequence CONV.
1401 Return a conversion sequence for this binding. */
1403 static conversion *
1404 direct_reference_binding (tree type, conversion *conv)
1406 tree t;
1408 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1409 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1411 t = TREE_TYPE (type);
1413 /* [over.ics.rank]
1415 When a parameter of reference type binds directly
1416 (_dcl.init.ref_) to an argument expression, the implicit
1417 conversion sequence is the identity conversion, unless the
1418 argument expression has a type that is a derived class of the
1419 parameter type, in which case the implicit conversion sequence is
1420 a derived-to-base Conversion.
1422 If the parameter binds directly to the result of applying a
1423 conversion function to the argument expression, the implicit
1424 conversion sequence is a user-defined conversion sequence
1425 (_over.ics.user_), with the second standard conversion sequence
1426 either an identity conversion or, if the conversion function
1427 returns an entity of a type that is a derived class of the
1428 parameter type, a derived-to-base conversion. */
1429 if (!same_type_ignoring_top_level_qualifiers_p (t, conv->type))
1431 /* Represent the derived-to-base conversion. */
1432 conv = build_conv (ck_base, t, conv);
1433 /* We will actually be binding to the base-class subobject in
1434 the derived class, so we mark this conversion appropriately.
1435 That way, convert_like knows not to generate a temporary. */
1436 conv->need_temporary_p = false;
1438 return build_conv (ck_ref_bind, type, conv);
1441 /* Returns the conversion path from type FROM to reference type TO for
1442 purposes of reference binding. For lvalue binding, either pass a
1443 reference type to FROM or an lvalue expression to EXPR. If the
1444 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1445 the conversion returned. If C_CAST_P is true, this
1446 conversion is coming from a C-style cast. */
1448 static conversion *
1449 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1450 tsubst_flags_t complain)
1452 conversion *conv = NULL;
1453 tree to = TREE_TYPE (rto);
1454 tree from = rfrom;
1455 tree tfrom;
1456 bool related_p;
1457 bool compatible_p;
1458 cp_lvalue_kind gl_kind;
1459 bool is_lvalue;
1461 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1463 expr = instantiate_type (to, expr, tf_none);
1464 if (expr == error_mark_node)
1465 return NULL;
1466 from = TREE_TYPE (expr);
1469 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1471 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1472 conv = implicit_conversion (to, from, expr, c_cast_p,
1473 flags, complain);
1474 if (!CLASS_TYPE_P (to)
1475 && CONSTRUCTOR_NELTS (expr) == 1)
1477 expr = CONSTRUCTOR_ELT (expr, 0)->value;
1478 if (error_operand_p (expr))
1479 return NULL;
1480 from = TREE_TYPE (expr);
1484 if (TREE_CODE (from) == REFERENCE_TYPE)
1486 from = TREE_TYPE (from);
1487 if (!TYPE_REF_IS_RVALUE (rfrom)
1488 || TREE_CODE (from) == FUNCTION_TYPE)
1489 gl_kind = clk_ordinary;
1490 else
1491 gl_kind = clk_rvalueref;
1493 else if (expr)
1495 gl_kind = lvalue_kind (expr);
1496 if (gl_kind & clk_class)
1497 /* A class prvalue is not a glvalue. */
1498 gl_kind = clk_none;
1500 else
1501 gl_kind = clk_none;
1502 is_lvalue = gl_kind && !(gl_kind & clk_rvalueref);
1504 tfrom = from;
1505 if ((gl_kind & clk_bitfield) != 0)
1506 tfrom = unlowered_expr_type (expr);
1508 /* Figure out whether or not the types are reference-related and
1509 reference compatible. We have do do this after stripping
1510 references from FROM. */
1511 related_p = reference_related_p (to, tfrom);
1512 /* If this is a C cast, first convert to an appropriately qualified
1513 type, so that we can later do a const_cast to the desired type. */
1514 if (related_p && c_cast_p
1515 && !at_least_as_qualified_p (to, tfrom))
1516 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1517 compatible_p = reference_compatible_p (to, tfrom);
1519 /* Directly bind reference when target expression's type is compatible with
1520 the reference and expression is an lvalue. In DR391, the wording in
1521 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1522 const and rvalue references to rvalues of compatible class type.
1523 We should also do direct bindings for non-class xvalues. */
1524 if (compatible_p
1525 && (is_lvalue
1526 || (((CP_TYPE_CONST_NON_VOLATILE_P (to)
1527 && !(flags & LOOKUP_NO_RVAL_BIND))
1528 || TYPE_REF_IS_RVALUE (rto))
1529 && (gl_kind
1530 || (!(flags & LOOKUP_NO_TEMP_BIND)
1531 && (CLASS_TYPE_P (from)
1532 || TREE_CODE (from) == ARRAY_TYPE))))))
1534 /* [dcl.init.ref]
1536 If the initializer expression
1538 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1539 is reference-compatible with "cv2 T2,"
1541 the reference is bound directly to the initializer expression
1542 lvalue.
1544 [...]
1545 If the initializer expression is an rvalue, with T2 a class type,
1546 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1547 is bound to the object represented by the rvalue or to a sub-object
1548 within that object. */
1550 conv = build_identity_conv (tfrom, expr);
1551 conv = direct_reference_binding (rto, conv);
1553 if (flags & LOOKUP_PREFER_RVALUE)
1554 /* The top-level caller requested that we pretend that the lvalue
1555 be treated as an rvalue. */
1556 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1557 else if (TREE_CODE (rfrom) == REFERENCE_TYPE)
1558 /* Handle rvalue reference to function properly. */
1559 conv->rvaluedness_matches_p
1560 = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1561 else
1562 conv->rvaluedness_matches_p
1563 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1565 if ((gl_kind & clk_bitfield) != 0
1566 || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1567 /* For the purposes of overload resolution, we ignore the fact
1568 this expression is a bitfield or packed field. (In particular,
1569 [over.ics.ref] says specifically that a function with a
1570 non-const reference parameter is viable even if the
1571 argument is a bitfield.)
1573 However, when we actually call the function we must create
1574 a temporary to which to bind the reference. If the
1575 reference is volatile, or isn't const, then we cannot make
1576 a temporary, so we just issue an error when the conversion
1577 actually occurs. */
1578 conv->need_temporary_p = true;
1580 /* Don't allow binding of lvalues (other than function lvalues) to
1581 rvalue references. */
1582 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1583 && TREE_CODE (to) != FUNCTION_TYPE
1584 && !(flags & LOOKUP_PREFER_RVALUE))
1585 conv->bad_p = true;
1587 return conv;
1589 /* [class.conv.fct] A conversion function is never used to convert a
1590 (possibly cv-qualified) object to the (possibly cv-qualified) same
1591 object type (or a reference to it), to a (possibly cv-qualified) base
1592 class of that type (or a reference to it).... */
1593 else if (CLASS_TYPE_P (from) && !related_p
1594 && !(flags & LOOKUP_NO_CONVERSION))
1596 /* [dcl.init.ref]
1598 If the initializer expression
1600 -- has a class type (i.e., T2 is a class type) can be
1601 implicitly converted to an lvalue of type "cv3 T3," where
1602 "cv1 T1" is reference-compatible with "cv3 T3". (this
1603 conversion is selected by enumerating the applicable
1604 conversion functions (_over.match.ref_) and choosing the
1605 best one through overload resolution. (_over.match_).
1607 the reference is bound to the lvalue result of the conversion
1608 in the second case. */
1609 z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
1610 complain);
1611 if (cand)
1612 return cand->second_conv;
1615 /* From this point on, we conceptually need temporaries, even if we
1616 elide them. Only the cases above are "direct bindings". */
1617 if (flags & LOOKUP_NO_TEMP_BIND)
1618 return NULL;
1620 /* [over.ics.rank]
1622 When a parameter of reference type is not bound directly to an
1623 argument expression, the conversion sequence is the one required
1624 to convert the argument expression to the underlying type of the
1625 reference according to _over.best.ics_. Conceptually, this
1626 conversion sequence corresponds to copy-initializing a temporary
1627 of the underlying type with the argument expression. Any
1628 difference in top-level cv-qualification is subsumed by the
1629 initialization itself and does not constitute a conversion. */
1631 /* [dcl.init.ref]
1633 Otherwise, the reference shall be to a non-volatile const type.
1635 Under C++0x, [8.5.3/5 dcl.init.ref] it may also be an rvalue reference */
1636 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1637 return NULL;
1639 /* [dcl.init.ref]
1641 Otherwise, a temporary of type "cv1 T1" is created and
1642 initialized from the initializer expression using the rules for a
1643 non-reference copy initialization. If T1 is reference-related to
1644 T2, cv1 must be the same cv-qualification as, or greater
1645 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1646 if (related_p && !at_least_as_qualified_p (to, from))
1647 return NULL;
1649 /* We're generating a temporary now, but don't bind any more in the
1650 conversion (specifically, don't slice the temporary returned by a
1651 conversion operator). */
1652 flags |= LOOKUP_NO_TEMP_BIND;
1654 /* Core issue 899: When [copy-]initializing a temporary to be bound
1655 to the first parameter of a copy constructor (12.8) called with
1656 a single argument in the context of direct-initialization,
1657 explicit conversion functions are also considered.
1659 So don't set LOOKUP_ONLYCONVERTING in that case. */
1660 if (!(flags & LOOKUP_COPY_PARM))
1661 flags |= LOOKUP_ONLYCONVERTING;
1663 if (!conv)
1664 conv = implicit_conversion (to, from, expr, c_cast_p,
1665 flags, complain);
1666 if (!conv)
1667 return NULL;
1669 conv = build_conv (ck_ref_bind, rto, conv);
1670 /* This reference binding, unlike those above, requires the
1671 creation of a temporary. */
1672 conv->need_temporary_p = true;
1673 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1675 return conv;
1678 /* Returns the implicit conversion sequence (see [over.ics]) from type
1679 FROM to type TO. The optional expression EXPR may affect the
1680 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1681 true, this conversion is coming from a C-style cast. */
1683 static conversion *
1684 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1685 int flags, tsubst_flags_t complain)
1687 conversion *conv;
1689 if (from == error_mark_node || to == error_mark_node
1690 || expr == error_mark_node)
1691 return NULL;
1693 /* Other flags only apply to the primary function in overload
1694 resolution, or after we've chosen one. */
1695 flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
1696 |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE
1697 |LOOKUP_NO_NARROWING|LOOKUP_PROTECT);
1699 if (TREE_CODE (to) == REFERENCE_TYPE)
1700 conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
1701 else
1702 conv = standard_conversion (to, from, expr, c_cast_p, flags);
1704 if (conv)
1705 return conv;
1707 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1709 if (is_std_init_list (to))
1710 return build_list_conv (to, expr, flags, complain);
1712 /* As an extension, allow list-initialization of _Complex. */
1713 if (TREE_CODE (to) == COMPLEX_TYPE)
1715 conv = build_complex_conv (to, expr, flags, complain);
1716 if (conv)
1717 return conv;
1720 /* Allow conversion from an initializer-list with one element to a
1721 scalar type. */
1722 if (SCALAR_TYPE_P (to))
1724 int nelts = CONSTRUCTOR_NELTS (expr);
1725 tree elt;
1727 if (nelts == 0)
1728 elt = build_value_init (to, tf_none);
1729 else if (nelts == 1)
1730 elt = CONSTRUCTOR_ELT (expr, 0)->value;
1731 else
1732 elt = error_mark_node;
1734 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
1735 c_cast_p, flags, complain);
1736 if (conv)
1738 conv->check_narrowing = true;
1739 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
1740 /* Too many levels of braces, i.e. '{{1}}'. */
1741 conv->bad_p = true;
1742 return conv;
1745 else if (TREE_CODE (to) == ARRAY_TYPE)
1746 return build_array_conv (to, expr, flags, complain);
1749 if (expr != NULL_TREE
1750 && (MAYBE_CLASS_TYPE_P (from)
1751 || MAYBE_CLASS_TYPE_P (to))
1752 && (flags & LOOKUP_NO_CONVERSION) == 0)
1754 struct z_candidate *cand;
1756 if (CLASS_TYPE_P (to)
1757 && BRACE_ENCLOSED_INITIALIZER_P (expr)
1758 && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
1759 return build_aggr_conv (to, expr, flags, complain);
1761 cand = build_user_type_conversion_1 (to, expr, flags, complain);
1762 if (cand)
1763 conv = cand->second_conv;
1765 /* We used to try to bind a reference to a temporary here, but that
1766 is now handled after the recursive call to this function at the end
1767 of reference_binding. */
1768 return conv;
1771 return NULL;
1774 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1775 functions. ARGS will not be changed until a single candidate is
1776 selected. */
1778 static struct z_candidate *
1779 add_candidate (struct z_candidate **candidates,
1780 tree fn, tree first_arg, const VEC(tree,gc) *args,
1781 size_t num_convs, conversion **convs,
1782 tree access_path, tree conversion_path,
1783 int viable, struct rejection_reason *reason)
1785 struct z_candidate *cand = (struct z_candidate *)
1786 conversion_obstack_alloc (sizeof (struct z_candidate));
1788 cand->fn = fn;
1789 cand->first_arg = first_arg;
1790 cand->args = args;
1791 cand->convs = convs;
1792 cand->num_convs = num_convs;
1793 cand->access_path = access_path;
1794 cand->conversion_path = conversion_path;
1795 cand->viable = viable;
1796 cand->reason = reason;
1797 cand->next = *candidates;
1798 *candidates = cand;
1800 return cand;
1803 /* Return the number of remaining arguments in the parameter list
1804 beginning with ARG. */
1806 static int
1807 remaining_arguments (tree arg)
1809 int n;
1811 for (n = 0; arg != NULL_TREE && arg != void_list_node;
1812 arg = TREE_CHAIN (arg))
1813 n++;
1815 return n;
1818 /* Create an overload candidate for the function or method FN called
1819 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
1820 FLAGS is passed on to implicit_conversion.
1822 This does not change ARGS.
1824 CTYPE, if non-NULL, is the type we want to pretend this function
1825 comes from for purposes of overload resolution. */
1827 static struct z_candidate *
1828 add_function_candidate (struct z_candidate **candidates,
1829 tree fn, tree ctype, tree first_arg,
1830 const VEC(tree,gc) *args, tree access_path,
1831 tree conversion_path, int flags,
1832 tsubst_flags_t complain)
1834 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1835 int i, len;
1836 conversion **convs;
1837 tree parmnode;
1838 tree orig_first_arg = first_arg;
1839 int skip;
1840 int viable = 1;
1841 struct rejection_reason *reason = NULL;
1843 /* At this point we should not see any functions which haven't been
1844 explicitly declared, except for friend functions which will have
1845 been found using argument dependent lookup. */
1846 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
1848 /* The `this', `in_chrg' and VTT arguments to constructors are not
1849 considered in overload resolution. */
1850 if (DECL_CONSTRUCTOR_P (fn))
1852 parmlist = skip_artificial_parms_for (fn, parmlist);
1853 skip = num_artificial_parms_for (fn);
1854 if (skip > 0 && first_arg != NULL_TREE)
1856 --skip;
1857 first_arg = NULL_TREE;
1860 else
1861 skip = 0;
1863 len = VEC_length (tree, args) - skip + (first_arg != NULL_TREE ? 1 : 0);
1864 convs = alloc_conversions (len);
1866 /* 13.3.2 - Viable functions [over.match.viable]
1867 First, to be a viable function, a candidate function shall have enough
1868 parameters to agree in number with the arguments in the list.
1870 We need to check this first; otherwise, checking the ICSes might cause
1871 us to produce an ill-formed template instantiation. */
1873 parmnode = parmlist;
1874 for (i = 0; i < len; ++i)
1876 if (parmnode == NULL_TREE || parmnode == void_list_node)
1877 break;
1878 parmnode = TREE_CHAIN (parmnode);
1881 if ((i < len && parmnode)
1882 || !sufficient_parms_p (parmnode))
1884 int remaining = remaining_arguments (parmnode);
1885 viable = 0;
1886 reason = arity_rejection (first_arg, i + remaining, len);
1888 /* When looking for a function from a subobject from an implicit
1889 copy/move constructor/operator=, don't consider anything that takes (a
1890 reference to) an unrelated type. See c++/44909 and core 1092. */
1891 else if (parmlist && (flags & LOOKUP_DEFAULTED))
1893 if (DECL_CONSTRUCTOR_P (fn))
1894 i = 1;
1895 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1896 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
1897 i = 2;
1898 else
1899 i = 0;
1900 if (i && len == i)
1902 parmnode = chain_index (i-1, parmlist);
1903 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
1904 ctype))
1905 viable = 0;
1908 /* This only applies at the top level. */
1909 flags &= ~LOOKUP_DEFAULTED;
1912 if (! viable)
1913 goto out;
1915 /* Second, for F to be a viable function, there shall exist for each
1916 argument an implicit conversion sequence that converts that argument
1917 to the corresponding parameter of F. */
1919 parmnode = parmlist;
1921 for (i = 0; i < len; ++i)
1923 tree arg, argtype, to_type;
1924 conversion *t;
1925 int is_this;
1927 if (parmnode == void_list_node)
1928 break;
1930 if (i == 0 && first_arg != NULL_TREE)
1931 arg = first_arg;
1932 else
1933 arg = VEC_index (tree, args,
1934 i + skip - (first_arg != NULL_TREE ? 1 : 0));
1935 argtype = lvalue_type (arg);
1937 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1938 && ! DECL_CONSTRUCTOR_P (fn));
1940 if (parmnode)
1942 tree parmtype = TREE_VALUE (parmnode);
1943 int lflags = flags;
1945 parmnode = TREE_CHAIN (parmnode);
1947 /* The type of the implicit object parameter ('this') for
1948 overload resolution is not always the same as for the
1949 function itself; conversion functions are considered to
1950 be members of the class being converted, and functions
1951 introduced by a using-declaration are considered to be
1952 members of the class that uses them.
1954 Since build_over_call ignores the ICS for the `this'
1955 parameter, we can just change the parm type. */
1956 if (ctype && is_this)
1958 parmtype = cp_build_qualified_type
1959 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
1960 parmtype = build_pointer_type (parmtype);
1963 /* Core issue 899: When [copy-]initializing a temporary to be bound
1964 to the first parameter of a copy constructor (12.8) called with
1965 a single argument in the context of direct-initialization,
1966 explicit conversion functions are also considered.
1968 So set LOOKUP_COPY_PARM to let reference_binding know that
1969 it's being called in that context. We generalize the above
1970 to handle move constructors and template constructors as well;
1971 the standardese should soon be updated similarly. */
1972 if (ctype && i == 0 && (len-skip == 1)
1973 && DECL_CONSTRUCTOR_P (fn)
1974 && parmtype != error_mark_node
1975 && (same_type_ignoring_top_level_qualifiers_p
1976 (non_reference (parmtype), ctype)))
1978 if (!(flags & LOOKUP_ONLYCONVERTING))
1979 lflags |= LOOKUP_COPY_PARM;
1980 /* We allow user-defined conversions within init-lists, but
1981 don't list-initialize the copy parm, as that would mean
1982 using two levels of braces for the same type. */
1983 if ((flags & LOOKUP_LIST_INIT_CTOR)
1984 && BRACE_ENCLOSED_INITIALIZER_P (arg))
1985 lflags |= LOOKUP_NO_CONVERSION;
1987 else
1988 lflags |= LOOKUP_ONLYCONVERTING;
1990 t = implicit_conversion (parmtype, argtype, arg,
1991 /*c_cast_p=*/false, lflags, complain);
1992 to_type = parmtype;
1994 else
1996 t = build_identity_conv (argtype, arg);
1997 t->ellipsis_p = true;
1998 to_type = argtype;
2001 if (t && is_this)
2002 t->this_p = true;
2004 convs[i] = t;
2005 if (! t)
2007 viable = 0;
2008 reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
2009 break;
2012 if (t->bad_p)
2014 viable = -1;
2015 reason = bad_arg_conversion_rejection (first_arg, i, argtype, to_type);
2019 out:
2020 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2021 access_path, conversion_path, viable, reason);
2024 /* Create an overload candidate for the conversion function FN which will
2025 be invoked for expression OBJ, producing a pointer-to-function which
2026 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2027 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2028 passed on to implicit_conversion.
2030 Actually, we don't really care about FN; we care about the type it
2031 converts to. There may be multiple conversion functions that will
2032 convert to that type, and we rely on build_user_type_conversion_1 to
2033 choose the best one; so when we create our candidate, we record the type
2034 instead of the function. */
2036 static struct z_candidate *
2037 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2038 tree first_arg, const VEC(tree,gc) *arglist,
2039 tree access_path, tree conversion_path,
2040 tsubst_flags_t complain)
2042 tree totype = TREE_TYPE (TREE_TYPE (fn));
2043 int i, len, viable, flags;
2044 tree parmlist, parmnode;
2045 conversion **convs;
2046 struct rejection_reason *reason;
2048 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2049 parmlist = TREE_TYPE (parmlist);
2050 parmlist = TYPE_ARG_TYPES (parmlist);
2052 len = VEC_length (tree, arglist) + (first_arg != NULL_TREE ? 1 : 0) + 1;
2053 convs = alloc_conversions (len);
2054 parmnode = parmlist;
2055 viable = 1;
2056 flags = LOOKUP_IMPLICIT;
2057 reason = NULL;
2059 /* Don't bother looking up the same type twice. */
2060 if (*candidates && (*candidates)->fn == totype)
2061 return NULL;
2063 for (i = 0; i < len; ++i)
2065 tree arg, argtype, convert_type = NULL_TREE;
2066 conversion *t;
2068 if (i == 0)
2069 arg = obj;
2070 else if (i == 1 && first_arg != NULL_TREE)
2071 arg = first_arg;
2072 else
2073 arg = VEC_index (tree, arglist,
2074 i - (first_arg != NULL_TREE ? 1 : 0) - 1);
2075 argtype = lvalue_type (arg);
2077 if (i == 0)
2079 t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false,
2080 flags, complain);
2081 convert_type = totype;
2083 else if (parmnode == void_list_node)
2084 break;
2085 else if (parmnode)
2087 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2088 /*c_cast_p=*/false, flags, complain);
2089 convert_type = TREE_VALUE (parmnode);
2091 else
2093 t = build_identity_conv (argtype, arg);
2094 t->ellipsis_p = true;
2095 convert_type = argtype;
2098 convs[i] = t;
2099 if (! t)
2100 break;
2102 if (t->bad_p)
2104 viable = -1;
2105 reason = bad_arg_conversion_rejection (NULL_TREE, i, argtype, convert_type);
2108 if (i == 0)
2109 continue;
2111 if (parmnode)
2112 parmnode = TREE_CHAIN (parmnode);
2115 if (i < len
2116 || ! sufficient_parms_p (parmnode))
2118 int remaining = remaining_arguments (parmnode);
2119 viable = 0;
2120 reason = arity_rejection (NULL_TREE, i + remaining, len);
2123 return add_candidate (candidates, totype, first_arg, arglist, len, convs,
2124 access_path, conversion_path, viable, reason);
2127 static void
2128 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2129 tree type1, tree type2, tree *args, tree *argtypes,
2130 int flags, tsubst_flags_t complain)
2132 conversion *t;
2133 conversion **convs;
2134 size_t num_convs;
2135 int viable = 1, i;
2136 tree types[2];
2137 struct rejection_reason *reason = NULL;
2139 types[0] = type1;
2140 types[1] = type2;
2142 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
2143 convs = alloc_conversions (num_convs);
2145 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2146 conversion ops are allowed. We handle that here by just checking for
2147 boolean_type_node because other operators don't ask for it. COND_EXPR
2148 also does contextual conversion to bool for the first operand, but we
2149 handle that in build_conditional_expr, and type1 here is operand 2. */
2150 if (type1 != boolean_type_node)
2151 flags |= LOOKUP_ONLYCONVERTING;
2153 for (i = 0; i < 2; ++i)
2155 if (! args[i])
2156 break;
2158 t = implicit_conversion (types[i], argtypes[i], args[i],
2159 /*c_cast_p=*/false, flags, complain);
2160 if (! t)
2162 viable = 0;
2163 /* We need something for printing the candidate. */
2164 t = build_identity_conv (types[i], NULL_TREE);
2165 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2166 types[i]);
2168 else if (t->bad_p)
2170 viable = 0;
2171 reason = bad_arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2172 types[i]);
2174 convs[i] = t;
2177 /* For COND_EXPR we rearranged the arguments; undo that now. */
2178 if (args[2])
2180 convs[2] = convs[1];
2181 convs[1] = convs[0];
2182 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2183 /*c_cast_p=*/false, flags,
2184 complain);
2185 if (t)
2186 convs[0] = t;
2187 else
2189 viable = 0;
2190 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2191 boolean_type_node);
2195 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2196 num_convs, convs,
2197 /*access_path=*/NULL_TREE,
2198 /*conversion_path=*/NULL_TREE,
2199 viable, reason);
2202 static bool
2203 is_complete (tree t)
2205 return COMPLETE_TYPE_P (complete_type (t));
2208 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2210 static bool
2211 promoted_arithmetic_type_p (tree type)
2213 /* [over.built]
2215 In this section, the term promoted integral type is used to refer
2216 to those integral types which are preserved by integral promotion
2217 (including e.g. int and long but excluding e.g. char).
2218 Similarly, the term promoted arithmetic type refers to promoted
2219 integral types plus floating types. */
2220 return ((CP_INTEGRAL_TYPE_P (type)
2221 && same_type_p (type_promotes_to (type), type))
2222 || TREE_CODE (type) == REAL_TYPE);
2225 /* Create any builtin operator overload candidates for the operator in
2226 question given the converted operand types TYPE1 and TYPE2. The other
2227 args are passed through from add_builtin_candidates to
2228 build_builtin_candidate.
2230 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2231 If CODE is requires candidates operands of the same type of the kind
2232 of which TYPE1 and TYPE2 are, we add both candidates
2233 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2235 static void
2236 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2237 enum tree_code code2, tree fnname, tree type1,
2238 tree type2, tree *args, tree *argtypes, int flags,
2239 tsubst_flags_t complain)
2241 switch (code)
2243 case POSTINCREMENT_EXPR:
2244 case POSTDECREMENT_EXPR:
2245 args[1] = integer_zero_node;
2246 type2 = integer_type_node;
2247 break;
2248 default:
2249 break;
2252 switch (code)
2255 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2256 and VQ is either volatile or empty, there exist candidate operator
2257 functions of the form
2258 VQ T& operator++(VQ T&);
2259 T operator++(VQ T&, int);
2260 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
2261 type other than bool, and VQ is either volatile or empty, there exist
2262 candidate operator functions of the form
2263 VQ T& operator--(VQ T&);
2264 T operator--(VQ T&, int);
2265 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
2266 complete object type, and VQ is either volatile or empty, there exist
2267 candidate operator functions of the form
2268 T*VQ& operator++(T*VQ&);
2269 T*VQ& operator--(T*VQ&);
2270 T* operator++(T*VQ&, int);
2271 T* operator--(T*VQ&, int); */
2273 case POSTDECREMENT_EXPR:
2274 case PREDECREMENT_EXPR:
2275 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2276 return;
2277 case POSTINCREMENT_EXPR:
2278 case PREINCREMENT_EXPR:
2279 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2281 type1 = build_reference_type (type1);
2282 break;
2284 return;
2286 /* 7 For every cv-qualified or cv-unqualified object type T, there
2287 exist candidate operator functions of the form
2289 T& operator*(T*);
2291 8 For every function type T, there exist candidate operator functions of
2292 the form
2293 T& operator*(T*); */
2295 case INDIRECT_REF:
2296 if (TREE_CODE (type1) == POINTER_TYPE
2297 && !uses_template_parms (TREE_TYPE (type1))
2298 && (TYPE_PTROB_P (type1)
2299 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2300 break;
2301 return;
2303 /* 9 For every type T, there exist candidate operator functions of the form
2304 T* operator+(T*);
2306 10For every promoted arithmetic type T, there exist candidate operator
2307 functions of the form
2308 T operator+(T);
2309 T operator-(T); */
2311 case UNARY_PLUS_EXPR: /* unary + */
2312 if (TREE_CODE (type1) == POINTER_TYPE)
2313 break;
2314 case NEGATE_EXPR:
2315 if (ARITHMETIC_TYPE_P (type1))
2316 break;
2317 return;
2319 /* 11For every promoted integral type T, there exist candidate operator
2320 functions of the form
2321 T operator~(T); */
2323 case BIT_NOT_EXPR:
2324 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2325 break;
2326 return;
2328 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2329 is the same type as C2 or is a derived class of C2, T is a complete
2330 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2331 there exist candidate operator functions of the form
2332 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2333 where CV12 is the union of CV1 and CV2. */
2335 case MEMBER_REF:
2336 if (TREE_CODE (type1) == POINTER_TYPE
2337 && TYPE_PTR_TO_MEMBER_P (type2))
2339 tree c1 = TREE_TYPE (type1);
2340 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2342 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2343 && (TYPE_PTRMEMFUNC_P (type2)
2344 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2345 break;
2347 return;
2349 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2350 didate operator functions of the form
2351 LR operator*(L, R);
2352 LR operator/(L, R);
2353 LR operator+(L, R);
2354 LR operator-(L, R);
2355 bool operator<(L, R);
2356 bool operator>(L, R);
2357 bool operator<=(L, R);
2358 bool operator>=(L, R);
2359 bool operator==(L, R);
2360 bool operator!=(L, R);
2361 where LR is the result of the usual arithmetic conversions between
2362 types L and R.
2364 14For every pair of types T and I, where T is a cv-qualified or cv-
2365 unqualified complete object type and I is a promoted integral type,
2366 there exist candidate operator functions of the form
2367 T* operator+(T*, I);
2368 T& operator[](T*, I);
2369 T* operator-(T*, I);
2370 T* operator+(I, T*);
2371 T& operator[](I, T*);
2373 15For every T, where T is a pointer to complete object type, there exist
2374 candidate operator functions of the form112)
2375 ptrdiff_t operator-(T, T);
2377 16For every pointer or enumeration type T, there exist candidate operator
2378 functions of the form
2379 bool operator<(T, T);
2380 bool operator>(T, T);
2381 bool operator<=(T, T);
2382 bool operator>=(T, T);
2383 bool operator==(T, T);
2384 bool operator!=(T, T);
2386 17For every pointer to member type T, there exist candidate operator
2387 functions of the form
2388 bool operator==(T, T);
2389 bool operator!=(T, T); */
2391 case MINUS_EXPR:
2392 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2393 break;
2394 if (TYPE_PTROB_P (type1)
2395 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2397 type2 = ptrdiff_type_node;
2398 break;
2400 case MULT_EXPR:
2401 case TRUNC_DIV_EXPR:
2402 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2403 break;
2404 return;
2406 case EQ_EXPR:
2407 case NE_EXPR:
2408 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2409 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
2410 break;
2411 if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
2413 type2 = type1;
2414 break;
2416 if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
2418 type1 = type2;
2419 break;
2421 /* Fall through. */
2422 case LT_EXPR:
2423 case GT_EXPR:
2424 case LE_EXPR:
2425 case GE_EXPR:
2426 case MAX_EXPR:
2427 case MIN_EXPR:
2428 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2429 break;
2430 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2431 break;
2432 if (TREE_CODE (type1) == ENUMERAL_TYPE
2433 && TREE_CODE (type2) == ENUMERAL_TYPE)
2434 break;
2435 if (TYPE_PTR_P (type1)
2436 && null_ptr_cst_p (args[1])
2437 && !uses_template_parms (type1))
2439 type2 = type1;
2440 break;
2442 if (null_ptr_cst_p (args[0])
2443 && TYPE_PTR_P (type2)
2444 && !uses_template_parms (type2))
2446 type1 = type2;
2447 break;
2449 return;
2451 case PLUS_EXPR:
2452 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2453 break;
2454 case ARRAY_REF:
2455 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2457 type1 = ptrdiff_type_node;
2458 break;
2460 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2462 type2 = ptrdiff_type_node;
2463 break;
2465 return;
2467 /* 18For every pair of promoted integral types L and R, there exist candi-
2468 date operator functions of the form
2469 LR operator%(L, R);
2470 LR operator&(L, R);
2471 LR operator^(L, R);
2472 LR operator|(L, R);
2473 L operator<<(L, R);
2474 L operator>>(L, R);
2475 where LR is the result of the usual arithmetic conversions between
2476 types L and R. */
2478 case TRUNC_MOD_EXPR:
2479 case BIT_AND_EXPR:
2480 case BIT_IOR_EXPR:
2481 case BIT_XOR_EXPR:
2482 case LSHIFT_EXPR:
2483 case RSHIFT_EXPR:
2484 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2485 break;
2486 return;
2488 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2489 type, VQ is either volatile or empty, and R is a promoted arithmetic
2490 type, there exist candidate operator functions of the form
2491 VQ L& operator=(VQ L&, R);
2492 VQ L& operator*=(VQ L&, R);
2493 VQ L& operator/=(VQ L&, R);
2494 VQ L& operator+=(VQ L&, R);
2495 VQ L& operator-=(VQ L&, R);
2497 20For every pair T, VQ), where T is any type and VQ is either volatile
2498 or empty, there exist candidate operator functions of the form
2499 T*VQ& operator=(T*VQ&, T*);
2501 21For every pair T, VQ), where T is a pointer to member type and VQ is
2502 either volatile or empty, there exist candidate operator functions of
2503 the form
2504 VQ T& operator=(VQ T&, T);
2506 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2507 unqualified complete object type, VQ is either volatile or empty, and
2508 I is a promoted integral type, there exist candidate operator func-
2509 tions of the form
2510 T*VQ& operator+=(T*VQ&, I);
2511 T*VQ& operator-=(T*VQ&, I);
2513 23For every triple L, VQ, R), where L is an integral or enumeration
2514 type, VQ is either volatile or empty, and R is a promoted integral
2515 type, there exist candidate operator functions of the form
2517 VQ L& operator%=(VQ L&, R);
2518 VQ L& operator<<=(VQ L&, R);
2519 VQ L& operator>>=(VQ L&, R);
2520 VQ L& operator&=(VQ L&, R);
2521 VQ L& operator^=(VQ L&, R);
2522 VQ L& operator|=(VQ L&, R); */
2524 case MODIFY_EXPR:
2525 switch (code2)
2527 case PLUS_EXPR:
2528 case MINUS_EXPR:
2529 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2531 type2 = ptrdiff_type_node;
2532 break;
2534 case MULT_EXPR:
2535 case TRUNC_DIV_EXPR:
2536 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2537 break;
2538 return;
2540 case TRUNC_MOD_EXPR:
2541 case BIT_AND_EXPR:
2542 case BIT_IOR_EXPR:
2543 case BIT_XOR_EXPR:
2544 case LSHIFT_EXPR:
2545 case RSHIFT_EXPR:
2546 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2547 break;
2548 return;
2550 case NOP_EXPR:
2551 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2552 break;
2553 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2554 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2555 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
2556 || ((TYPE_PTRMEMFUNC_P (type1)
2557 || TREE_CODE (type1) == POINTER_TYPE)
2558 && null_ptr_cst_p (args[1])))
2560 type2 = type1;
2561 break;
2563 return;
2565 default:
2566 gcc_unreachable ();
2568 type1 = build_reference_type (type1);
2569 break;
2571 case COND_EXPR:
2572 /* [over.built]
2574 For every pair of promoted arithmetic types L and R, there
2575 exist candidate operator functions of the form
2577 LR operator?(bool, L, R);
2579 where LR is the result of the usual arithmetic conversions
2580 between types L and R.
2582 For every type T, where T is a pointer or pointer-to-member
2583 type, there exist candidate operator functions of the form T
2584 operator?(bool, T, T); */
2586 if (promoted_arithmetic_type_p (type1)
2587 && promoted_arithmetic_type_p (type2))
2588 /* That's OK. */
2589 break;
2591 /* Otherwise, the types should be pointers. */
2592 if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
2593 || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
2594 return;
2596 /* We don't check that the two types are the same; the logic
2597 below will actually create two candidates; one in which both
2598 parameter types are TYPE1, and one in which both parameter
2599 types are TYPE2. */
2600 break;
2602 case REALPART_EXPR:
2603 case IMAGPART_EXPR:
2604 if (ARITHMETIC_TYPE_P (type1))
2605 break;
2606 return;
2608 default:
2609 gcc_unreachable ();
2612 /* If we're dealing with two pointer types or two enumeral types,
2613 we need candidates for both of them. */
2614 if (type2 && !same_type_p (type1, type2)
2615 && TREE_CODE (type1) == TREE_CODE (type2)
2616 && (TREE_CODE (type1) == REFERENCE_TYPE
2617 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2618 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
2619 || TYPE_PTRMEMFUNC_P (type1)
2620 || MAYBE_CLASS_TYPE_P (type1)
2621 || TREE_CODE (type1) == ENUMERAL_TYPE))
2623 if (TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
2625 tree cptype = composite_pointer_type (type1, type2,
2626 error_mark_node,
2627 error_mark_node,
2628 CPO_CONVERSION,
2629 tf_none);
2630 if (cptype != error_mark_node)
2632 build_builtin_candidate
2633 (candidates, fnname, cptype, cptype, args, argtypes,
2634 flags, complain);
2635 return;
2639 build_builtin_candidate
2640 (candidates, fnname, type1, type1, args, argtypes, flags, complain);
2641 build_builtin_candidate
2642 (candidates, fnname, type2, type2, args, argtypes, flags, complain);
2643 return;
2646 build_builtin_candidate
2647 (candidates, fnname, type1, type2, args, argtypes, flags, complain);
2650 tree
2651 type_decays_to (tree type)
2653 if (TREE_CODE (type) == ARRAY_TYPE)
2654 return build_pointer_type (TREE_TYPE (type));
2655 if (TREE_CODE (type) == FUNCTION_TYPE)
2656 return build_pointer_type (type);
2657 return type;
2660 /* There are three conditions of builtin candidates:
2662 1) bool-taking candidates. These are the same regardless of the input.
2663 2) pointer-pair taking candidates. These are generated for each type
2664 one of the input types converts to.
2665 3) arithmetic candidates. According to the standard, we should generate
2666 all of these, but I'm trying not to...
2668 Here we generate a superset of the possible candidates for this particular
2669 case. That is a subset of the full set the standard defines, plus some
2670 other cases which the standard disallows. add_builtin_candidate will
2671 filter out the invalid set. */
2673 static void
2674 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2675 enum tree_code code2, tree fnname, tree *args,
2676 int flags, tsubst_flags_t complain)
2678 int ref1, i;
2679 int enum_p = 0;
2680 tree type, argtypes[3], t;
2681 /* TYPES[i] is the set of possible builtin-operator parameter types
2682 we will consider for the Ith argument. */
2683 VEC(tree,gc) *types[2];
2684 unsigned ix;
2686 for (i = 0; i < 3; ++i)
2688 if (args[i])
2689 argtypes[i] = unlowered_expr_type (args[i]);
2690 else
2691 argtypes[i] = NULL_TREE;
2694 switch (code)
2696 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2697 and VQ is either volatile or empty, there exist candidate operator
2698 functions of the form
2699 VQ T& operator++(VQ T&); */
2701 case POSTINCREMENT_EXPR:
2702 case PREINCREMENT_EXPR:
2703 case POSTDECREMENT_EXPR:
2704 case PREDECREMENT_EXPR:
2705 case MODIFY_EXPR:
2706 ref1 = 1;
2707 break;
2709 /* 24There also exist candidate operator functions of the form
2710 bool operator!(bool);
2711 bool operator&&(bool, bool);
2712 bool operator||(bool, bool); */
2714 case TRUTH_NOT_EXPR:
2715 build_builtin_candidate
2716 (candidates, fnname, boolean_type_node,
2717 NULL_TREE, args, argtypes, flags, complain);
2718 return;
2720 case TRUTH_ORIF_EXPR:
2721 case TRUTH_ANDIF_EXPR:
2722 build_builtin_candidate
2723 (candidates, fnname, boolean_type_node,
2724 boolean_type_node, args, argtypes, flags, complain);
2725 return;
2727 case ADDR_EXPR:
2728 case COMPOUND_EXPR:
2729 case COMPONENT_REF:
2730 return;
2732 case COND_EXPR:
2733 case EQ_EXPR:
2734 case NE_EXPR:
2735 case LT_EXPR:
2736 case LE_EXPR:
2737 case GT_EXPR:
2738 case GE_EXPR:
2739 enum_p = 1;
2740 /* Fall through. */
2742 default:
2743 ref1 = 0;
2746 types[0] = make_tree_vector ();
2747 types[1] = make_tree_vector ();
2749 for (i = 0; i < 2; ++i)
2751 if (! args[i])
2753 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
2755 tree convs;
2757 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2758 return;
2760 convs = lookup_conversions (argtypes[i]);
2762 if (code == COND_EXPR)
2764 if (real_lvalue_p (args[i]))
2765 VEC_safe_push (tree, gc, types[i],
2766 build_reference_type (argtypes[i]));
2768 VEC_safe_push (tree, gc, types[i],
2769 TYPE_MAIN_VARIANT (argtypes[i]));
2772 else if (! convs)
2773 return;
2775 for (; convs; convs = TREE_CHAIN (convs))
2777 type = TREE_TYPE (convs);
2779 if (i == 0 && ref1
2780 && (TREE_CODE (type) != REFERENCE_TYPE
2781 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2782 continue;
2784 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2785 VEC_safe_push (tree, gc, types[i], type);
2787 type = non_reference (type);
2788 if (i != 0 || ! ref1)
2790 type = cv_unqualified (type_decays_to (type));
2791 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2792 VEC_safe_push (tree, gc, types[i], type);
2793 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2794 type = type_promotes_to (type);
2797 if (! vec_member (type, types[i]))
2798 VEC_safe_push (tree, gc, types[i], type);
2801 else
2803 if (code == COND_EXPR && real_lvalue_p (args[i]))
2804 VEC_safe_push (tree, gc, types[i],
2805 build_reference_type (argtypes[i]));
2806 type = non_reference (argtypes[i]);
2807 if (i != 0 || ! ref1)
2809 type = cv_unqualified (type_decays_to (type));
2810 if (enum_p && UNSCOPED_ENUM_P (type))
2811 VEC_safe_push (tree, gc, types[i], type);
2812 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2813 type = type_promotes_to (type);
2815 VEC_safe_push (tree, gc, types[i], type);
2819 /* Run through the possible parameter types of both arguments,
2820 creating candidates with those parameter types. */
2821 FOR_EACH_VEC_ELT_REVERSE (tree, types[0], ix, t)
2823 unsigned jx;
2824 tree u;
2826 if (!VEC_empty (tree, types[1]))
2827 FOR_EACH_VEC_ELT_REVERSE (tree, types[1], jx, u)
2828 add_builtin_candidate
2829 (candidates, code, code2, fnname, t,
2830 u, args, argtypes, flags, complain);
2831 else
2832 add_builtin_candidate
2833 (candidates, code, code2, fnname, t,
2834 NULL_TREE, args, argtypes, flags, complain);
2837 release_tree_vector (types[0]);
2838 release_tree_vector (types[1]);
2842 /* If TMPL can be successfully instantiated as indicated by
2843 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2845 TMPL is the template. EXPLICIT_TARGS are any explicit template
2846 arguments. ARGLIST is the arguments provided at the call-site.
2847 This does not change ARGLIST. The RETURN_TYPE is the desired type
2848 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
2849 as for add_function_candidate. If an OBJ is supplied, FLAGS and
2850 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
2852 static struct z_candidate*
2853 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2854 tree ctype, tree explicit_targs, tree first_arg,
2855 const VEC(tree,gc) *arglist, tree return_type,
2856 tree access_path, tree conversion_path,
2857 int flags, tree obj, unification_kind_t strict,
2858 tsubst_flags_t complain)
2860 int ntparms = DECL_NTPARMS (tmpl);
2861 tree targs = make_tree_vec (ntparms);
2862 unsigned int len = VEC_length (tree, arglist);
2863 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
2864 unsigned int skip_without_in_chrg = 0;
2865 tree first_arg_without_in_chrg = first_arg;
2866 tree *args_without_in_chrg;
2867 unsigned int nargs_without_in_chrg;
2868 unsigned int ia, ix;
2869 tree arg;
2870 struct z_candidate *cand;
2871 int i;
2872 tree fn;
2873 struct rejection_reason *reason = NULL;
2874 int errs;
2876 /* We don't do deduction on the in-charge parameter, the VTT
2877 parameter or 'this'. */
2878 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2880 if (first_arg_without_in_chrg != NULL_TREE)
2881 first_arg_without_in_chrg = NULL_TREE;
2882 else
2883 ++skip_without_in_chrg;
2886 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2887 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2888 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
2890 if (first_arg_without_in_chrg != NULL_TREE)
2891 first_arg_without_in_chrg = NULL_TREE;
2892 else
2893 ++skip_without_in_chrg;
2896 if (len < skip_without_in_chrg)
2897 return NULL;
2899 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
2900 + (len - skip_without_in_chrg));
2901 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
2902 ia = 0;
2903 if (first_arg_without_in_chrg != NULL_TREE)
2905 args_without_in_chrg[ia] = first_arg_without_in_chrg;
2906 ++ia;
2908 for (ix = skip_without_in_chrg;
2909 VEC_iterate (tree, arglist, ix, arg);
2910 ++ix)
2912 args_without_in_chrg[ia] = arg;
2913 ++ia;
2915 gcc_assert (ia == nargs_without_in_chrg);
2917 errs = errorcount+sorrycount;
2918 i = fn_type_unification (tmpl, explicit_targs, targs,
2919 args_without_in_chrg,
2920 nargs_without_in_chrg,
2921 return_type, strict, flags, false);
2923 if (i != 0)
2925 /* Don't repeat unification later if it already resulted in errors. */
2926 if (errorcount+sorrycount == errs)
2927 reason = template_unification_rejection (tmpl, explicit_targs,
2928 targs, args_without_in_chrg,
2929 nargs_without_in_chrg,
2930 return_type, strict, flags);
2931 else
2932 reason = template_unification_error_rejection ();
2933 goto fail;
2936 fn = instantiate_template (tmpl, targs, tf_none);
2937 if (fn == error_mark_node)
2939 reason = template_instantiation_rejection (tmpl, targs);
2940 goto fail;
2943 /* In [class.copy]:
2945 A member function template is never instantiated to perform the
2946 copy of a class object to an object of its class type.
2948 It's a little unclear what this means; the standard explicitly
2949 does allow a template to be used to copy a class. For example,
2952 struct A {
2953 A(A&);
2954 template <class T> A(const T&);
2956 const A f ();
2957 void g () { A a (f ()); }
2959 the member template will be used to make the copy. The section
2960 quoted above appears in the paragraph that forbids constructors
2961 whose only parameter is (a possibly cv-qualified variant of) the
2962 class type, and a logical interpretation is that the intent was
2963 to forbid the instantiation of member templates which would then
2964 have that form. */
2965 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
2967 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2968 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2969 ctype))
2971 reason = invalid_copy_with_fn_template_rejection ();
2972 goto fail;
2976 if (obj != NULL_TREE)
2977 /* Aha, this is a conversion function. */
2978 cand = add_conv_candidate (candidates, fn, obj, first_arg, arglist,
2979 access_path, conversion_path, complain);
2980 else
2981 cand = add_function_candidate (candidates, fn, ctype,
2982 first_arg, arglist, access_path,
2983 conversion_path, flags, complain);
2984 if (DECL_TI_TEMPLATE (fn) != tmpl)
2985 /* This situation can occur if a member template of a template
2986 class is specialized. Then, instantiate_template might return
2987 an instantiation of the specialization, in which case the
2988 DECL_TI_TEMPLATE field will point at the original
2989 specialization. For example:
2991 template <class T> struct S { template <class U> void f(U);
2992 template <> void f(int) {}; };
2993 S<double> sd;
2994 sd.f(3);
2996 Here, TMPL will be template <class U> S<double>::f(U).
2997 And, instantiate template will give us the specialization
2998 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2999 for this will point at template <class T> template <> S<T>::f(int),
3000 so that we can find the definition. For the purposes of
3001 overload resolution, however, we want the original TMPL. */
3002 cand->template_decl = build_template_info (tmpl, targs);
3003 else
3004 cand->template_decl = DECL_TEMPLATE_INFO (fn);
3005 cand->explicit_targs = explicit_targs;
3007 return cand;
3008 fail:
3009 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
3010 access_path, conversion_path, 0, reason);
3014 static struct z_candidate *
3015 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3016 tree explicit_targs, tree first_arg,
3017 const VEC(tree,gc) *arglist, tree return_type,
3018 tree access_path, tree conversion_path, int flags,
3019 unification_kind_t strict, tsubst_flags_t complain)
3021 return
3022 add_template_candidate_real (candidates, tmpl, ctype,
3023 explicit_targs, first_arg, arglist,
3024 return_type, access_path, conversion_path,
3025 flags, NULL_TREE, strict, complain);
3029 static struct z_candidate *
3030 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3031 tree obj, tree first_arg,
3032 const VEC(tree,gc) *arglist,
3033 tree return_type, tree access_path,
3034 tree conversion_path, tsubst_flags_t complain)
3036 return
3037 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3038 first_arg, arglist, return_type, access_path,
3039 conversion_path, 0, obj, DEDUCE_CONV,
3040 complain);
3043 /* The CANDS are the set of candidates that were considered for
3044 overload resolution. Return the set of viable candidates, or CANDS
3045 if none are viable. If any of the candidates were viable, set
3046 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3047 considered viable only if it is strictly viable. */
3049 static struct z_candidate*
3050 splice_viable (struct z_candidate *cands,
3051 bool strict_p,
3052 bool *any_viable_p)
3054 struct z_candidate *viable;
3055 struct z_candidate **last_viable;
3056 struct z_candidate **cand;
3058 /* Be strict inside templates, since build_over_call won't actually
3059 do the conversions to get pedwarns. */
3060 if (processing_template_decl)
3061 strict_p = true;
3063 viable = NULL;
3064 last_viable = &viable;
3065 *any_viable_p = false;
3067 cand = &cands;
3068 while (*cand)
3070 struct z_candidate *c = *cand;
3071 if (strict_p ? c->viable == 1 : c->viable)
3073 *last_viable = c;
3074 *cand = c->next;
3075 c->next = NULL;
3076 last_viable = &c->next;
3077 *any_viable_p = true;
3079 else
3080 cand = &c->next;
3083 return viable ? viable : cands;
3086 static bool
3087 any_strictly_viable (struct z_candidate *cands)
3089 for (; cands; cands = cands->next)
3090 if (cands->viable == 1)
3091 return true;
3092 return false;
3095 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3096 words, it is about to become the "this" pointer for a member
3097 function call. Take the address of the object. */
3099 static tree
3100 build_this (tree obj)
3102 /* In a template, we are only concerned about the type of the
3103 expression, so we can take a shortcut. */
3104 if (processing_template_decl)
3105 return build_address (obj);
3107 return cp_build_addr_expr (obj, tf_warning_or_error);
3110 /* Returns true iff functions are equivalent. Equivalent functions are
3111 not '==' only if one is a function-local extern function or if
3112 both are extern "C". */
3114 static inline int
3115 equal_functions (tree fn1, tree fn2)
3117 if (TREE_CODE (fn1) != TREE_CODE (fn2))
3118 return 0;
3119 if (TREE_CODE (fn1) == TEMPLATE_DECL)
3120 return fn1 == fn2;
3121 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
3122 || DECL_EXTERN_C_FUNCTION_P (fn1))
3123 return decls_match (fn1, fn2);
3124 return fn1 == fn2;
3127 /* Print information about a candidate being rejected due to INFO. */
3129 static void
3130 print_conversion_rejection (location_t loc, struct conversion_info *info)
3132 if (info->n_arg == -1)
3133 /* Conversion of implicit `this' argument failed. */
3134 inform (loc, " no known conversion for implicit "
3135 "%<this%> parameter from %qT to %qT",
3136 info->from_type, info->to_type);
3137 else
3138 inform (loc, " no known conversion for argument %d from %qT to %qT",
3139 info->n_arg+1, info->from_type, info->to_type);
3142 /* Print information about a candidate with WANT parameters and we found
3143 HAVE. */
3145 static void
3146 print_arity_information (location_t loc, unsigned int have, unsigned int want)
3148 inform_n (loc, want,
3149 " candidate expects %d argument, %d provided",
3150 " candidate expects %d arguments, %d provided",
3151 want, have);
3154 /* Print information about one overload candidate CANDIDATE. MSGSTR
3155 is the text to print before the candidate itself.
3157 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3158 to have been run through gettext by the caller. This wart makes
3159 life simpler in print_z_candidates and for the translators. */
3161 static void
3162 print_z_candidate (const char *msgstr, struct z_candidate *candidate)
3164 const char *msg = (msgstr == NULL
3165 ? ""
3166 : ACONCAT ((msgstr, " ", NULL)));
3167 location_t loc = location_of (candidate->fn);
3169 if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
3171 if (candidate->num_convs == 3)
3172 inform (input_location, "%s%D(%T, %T, %T) <built-in>", msg, candidate->fn,
3173 candidate->convs[0]->type,
3174 candidate->convs[1]->type,
3175 candidate->convs[2]->type);
3176 else if (candidate->num_convs == 2)
3177 inform (input_location, "%s%D(%T, %T) <built-in>", msg, candidate->fn,
3178 candidate->convs[0]->type,
3179 candidate->convs[1]->type);
3180 else
3181 inform (input_location, "%s%D(%T) <built-in>", msg, candidate->fn,
3182 candidate->convs[0]->type);
3184 else if (TYPE_P (candidate->fn))
3185 inform (input_location, "%s%T <conversion>", msg, candidate->fn);
3186 else if (candidate->viable == -1)
3187 inform (loc, "%s%#D <near match>", msg, candidate->fn);
3188 else if (DECL_DELETED_FN (STRIP_TEMPLATE (candidate->fn)))
3189 inform (loc, "%s%#D <deleted>", msg, candidate->fn);
3190 else
3191 inform (loc, "%s%#D", msg, candidate->fn);
3192 /* Give the user some information about why this candidate failed. */
3193 if (candidate->reason != NULL)
3195 struct rejection_reason *r = candidate->reason;
3197 switch (r->code)
3199 case rr_arity:
3200 print_arity_information (loc, r->u.arity.actual,
3201 r->u.arity.expected);
3202 break;
3203 case rr_arg_conversion:
3204 print_conversion_rejection (loc, &r->u.conversion);
3205 break;
3206 case rr_bad_arg_conversion:
3207 print_conversion_rejection (loc, &r->u.bad_conversion);
3208 break;
3209 case rr_explicit_conversion:
3210 inform (loc, " return type %qT of explicit conversion function "
3211 "cannot be converted to %qT with a qualification "
3212 "conversion", r->u.conversion.from_type,
3213 r->u.conversion.to_type);
3214 break;
3215 case rr_template_conversion:
3216 inform (loc, " conversion from return type %qT of template "
3217 "conversion function specialization to %qT is not an "
3218 "exact match", r->u.conversion.from_type,
3219 r->u.conversion.to_type);
3220 break;
3221 case rr_template_unification:
3222 /* We use template_unification_error_rejection if unification caused
3223 actual non-SFINAE errors, in which case we don't need to repeat
3224 them here. */
3225 if (r->u.template_unification.tmpl == NULL_TREE)
3227 inform (loc, " substitution of deduced template arguments "
3228 "resulted in errors seen above");
3229 break;
3231 /* Re-run template unification with diagnostics. */
3232 inform (loc, " template argument deduction/substitution failed:");
3233 fn_type_unification (r->u.template_unification.tmpl,
3234 r->u.template_unification.explicit_targs,
3235 r->u.template_unification.targs,
3236 r->u.template_unification.args,
3237 r->u.template_unification.nargs,
3238 r->u.template_unification.return_type,
3239 r->u.template_unification.strict,
3240 r->u.template_unification.flags,
3241 true);
3242 break;
3243 case rr_template_instantiation:
3244 /* Re-run template instantiation with diagnostics. */
3245 instantiate_template (r->u.template_instantiation.tmpl,
3246 r->u.template_instantiation.targs,
3247 tf_warning_or_error);
3248 break;
3249 case rr_invalid_copy:
3250 inform (loc,
3251 " a constructor taking a single argument of its own "
3252 "class type is invalid");
3253 break;
3254 case rr_none:
3255 default:
3256 /* This candidate didn't have any issues or we failed to
3257 handle a particular code. Either way... */
3258 gcc_unreachable ();
3263 static void
3264 print_z_candidates (location_t loc, struct z_candidate *candidates)
3266 struct z_candidate *cand1;
3267 struct z_candidate **cand2;
3268 int n_candidates;
3270 if (!candidates)
3271 return;
3273 /* Remove non-viable deleted candidates. */
3274 cand1 = candidates;
3275 for (cand2 = &cand1; *cand2; )
3277 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
3278 && !(*cand2)->viable
3279 && DECL_DELETED_FN ((*cand2)->fn))
3280 *cand2 = (*cand2)->next;
3281 else
3282 cand2 = &(*cand2)->next;
3284 /* ...if there are any non-deleted ones. */
3285 if (cand1)
3286 candidates = cand1;
3288 /* There may be duplicates in the set of candidates. We put off
3289 checking this condition as long as possible, since we have no way
3290 to eliminate duplicates from a set of functions in less than n^2
3291 time. Now we are about to emit an error message, so it is more
3292 permissible to go slowly. */
3293 for (cand1 = candidates; cand1; cand1 = cand1->next)
3295 tree fn = cand1->fn;
3296 /* Skip builtin candidates and conversion functions. */
3297 if (!DECL_P (fn))
3298 continue;
3299 cand2 = &cand1->next;
3300 while (*cand2)
3302 if (DECL_P ((*cand2)->fn)
3303 && equal_functions (fn, (*cand2)->fn))
3304 *cand2 = (*cand2)->next;
3305 else
3306 cand2 = &(*cand2)->next;
3310 for (n_candidates = 0, cand1 = candidates; cand1; cand1 = cand1->next)
3311 n_candidates++;
3313 inform_n (loc, n_candidates, "candidate is:", "candidates are:");
3314 for (; candidates; candidates = candidates->next)
3315 print_z_candidate (NULL, candidates);
3318 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3319 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3320 the result of the conversion function to convert it to the final
3321 desired type. Merge the two sequences into a single sequence,
3322 and return the merged sequence. */
3324 static conversion *
3325 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
3327 conversion **t;
3328 bool bad = user_seq->bad_p;
3330 gcc_assert (user_seq->kind == ck_user);
3332 /* Find the end of the second conversion sequence. */
3333 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
3335 /* The entire sequence is a user-conversion sequence. */
3336 (*t)->user_conv_p = true;
3337 if (bad)
3338 (*t)->bad_p = true;
3341 /* Replace the identity conversion with the user conversion
3342 sequence. */
3343 *t = user_seq;
3345 return std_seq;
3348 /* Handle overload resolution for initializing an object of class type from
3349 an initializer list. First we look for a suitable constructor that
3350 takes a std::initializer_list; if we don't find one, we then look for a
3351 non-list constructor.
3353 Parameters are as for add_candidates, except that the arguments are in
3354 the form of a CONSTRUCTOR (the initializer list) rather than a VEC, and
3355 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3357 static void
3358 add_list_candidates (tree fns, tree first_arg,
3359 tree init_list, tree totype,
3360 tree explicit_targs, bool template_only,
3361 tree conversion_path, tree access_path,
3362 int flags,
3363 struct z_candidate **candidates,
3364 tsubst_flags_t complain)
3366 VEC(tree,gc) *args;
3368 gcc_assert (*candidates == NULL);
3370 /* We're looking for a ctor for list-initialization. */
3371 flags |= LOOKUP_LIST_INIT_CTOR;
3372 /* And we don't allow narrowing conversions. We also use this flag to
3373 avoid the copy constructor call for copy-list-initialization. */
3374 flags |= LOOKUP_NO_NARROWING;
3376 /* Always use the default constructor if the list is empty (DR 990). */
3377 if (CONSTRUCTOR_NELTS (init_list) == 0
3378 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
3380 /* If the class has a list ctor, try passing the list as a single
3381 argument first, but only consider list ctors. */
3382 else if (TYPE_HAS_LIST_CTOR (totype))
3384 flags |= LOOKUP_LIST_ONLY;
3385 args = make_tree_vector_single (init_list);
3386 add_candidates (fns, first_arg, args, NULL_TREE,
3387 explicit_targs, template_only, conversion_path,
3388 access_path, flags, candidates, complain);
3389 if (any_strictly_viable (*candidates))
3390 return;
3393 args = ctor_to_vec (init_list);
3395 /* We aren't looking for list-ctors anymore. */
3396 flags &= ~LOOKUP_LIST_ONLY;
3397 /* We allow more user-defined conversions within an init-list. */
3398 flags &= ~LOOKUP_NO_CONVERSION;
3400 add_candidates (fns, first_arg, args, NULL_TREE,
3401 explicit_targs, template_only, conversion_path,
3402 access_path, flags, candidates, complain);
3405 /* Returns the best overload candidate to perform the requested
3406 conversion. This function is used for three the overloading situations
3407 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3408 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3409 per [dcl.init.ref], so we ignore temporary bindings. */
3411 static struct z_candidate *
3412 build_user_type_conversion_1 (tree totype, tree expr, int flags,
3413 tsubst_flags_t complain)
3415 struct z_candidate *candidates, *cand;
3416 tree fromtype;
3417 tree ctors = NULL_TREE;
3418 tree conv_fns = NULL_TREE;
3419 conversion *conv = NULL;
3420 tree first_arg = NULL_TREE;
3421 VEC(tree,gc) *args = NULL;
3422 bool any_viable_p;
3423 int convflags;
3425 if (!expr)
3426 return NULL;
3428 fromtype = TREE_TYPE (expr);
3430 /* We represent conversion within a hierarchy using RVALUE_CONV and
3431 BASE_CONV, as specified by [over.best.ics]; these become plain
3432 constructor calls, as specified in [dcl.init]. */
3433 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
3434 || !DERIVED_FROM_P (totype, fromtype));
3436 if (MAYBE_CLASS_TYPE_P (totype))
3437 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
3438 creating a garbage BASELINK; constructors can't be inherited. */
3439 ctors = lookup_fnfields_slot (totype, complete_ctor_identifier);
3441 if (MAYBE_CLASS_TYPE_P (fromtype))
3443 tree to_nonref = non_reference (totype);
3444 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
3445 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
3446 && DERIVED_FROM_P (to_nonref, fromtype)))
3448 /* [class.conv.fct] A conversion function is never used to
3449 convert a (possibly cv-qualified) object to the (possibly
3450 cv-qualified) same object type (or a reference to it), to a
3451 (possibly cv-qualified) base class of that type (or a
3452 reference to it)... */
3454 else
3455 conv_fns = lookup_conversions (fromtype);
3458 candidates = 0;
3459 flags |= LOOKUP_NO_CONVERSION;
3460 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3461 flags |= LOOKUP_NO_NARROWING;
3463 /* It's OK to bind a temporary for converting constructor arguments, but
3464 not in converting the return value of a conversion operator. */
3465 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION);
3466 flags &= ~LOOKUP_NO_TEMP_BIND;
3468 if (ctors)
3470 int ctorflags = flags;
3472 first_arg = build_int_cst (build_pointer_type (totype), 0);
3474 /* We should never try to call the abstract or base constructor
3475 from here. */
3476 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
3477 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)));
3479 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3481 /* List-initialization. */
3482 add_list_candidates (ctors, first_arg, expr, totype, NULL_TREE,
3483 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
3484 ctorflags, &candidates, complain);
3486 else
3488 args = make_tree_vector_single (expr);
3489 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
3490 TYPE_BINFO (totype), TYPE_BINFO (totype),
3491 ctorflags, &candidates, complain);
3494 for (cand = candidates; cand; cand = cand->next)
3496 cand->second_conv = build_identity_conv (totype, NULL_TREE);
3498 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
3499 set, then this is copy-initialization. In that case, "The
3500 result of the call is then used to direct-initialize the
3501 object that is the destination of the copy-initialization."
3502 [dcl.init]
3504 We represent this in the conversion sequence with an
3505 rvalue conversion, which means a constructor call. */
3506 if (TREE_CODE (totype) != REFERENCE_TYPE
3507 && !(convflags & LOOKUP_NO_TEMP_BIND))
3508 cand->second_conv
3509 = build_conv (ck_rvalue, totype, cand->second_conv);
3513 if (conv_fns)
3514 first_arg = build_this (expr);
3516 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
3518 tree conversion_path = TREE_PURPOSE (conv_fns);
3519 struct z_candidate *old_candidates;
3521 /* If we are called to convert to a reference type, we are trying to
3522 find a direct binding, so don't even consider temporaries. If
3523 we don't find a direct binding, the caller will try again to
3524 look for a temporary binding. */
3525 if (TREE_CODE (totype) == REFERENCE_TYPE)
3526 convflags |= LOOKUP_NO_TEMP_BIND;
3528 old_candidates = candidates;
3529 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3530 NULL_TREE, false,
3531 conversion_path, TYPE_BINFO (fromtype),
3532 flags, &candidates, complain);
3534 for (cand = candidates; cand != old_candidates; cand = cand->next)
3536 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
3537 conversion *ics
3538 = implicit_conversion (totype,
3539 rettype,
3541 /*c_cast_p=*/false, convflags,
3542 complain);
3544 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3545 copy-initialization. In that case, "The result of the
3546 call is then used to direct-initialize the object that is
3547 the destination of the copy-initialization." [dcl.init]
3549 We represent this in the conversion sequence with an
3550 rvalue conversion, which means a constructor call. But
3551 don't add a second rvalue conversion if there's already
3552 one there. Which there really shouldn't be, but it's
3553 harmless since we'd add it here anyway. */
3554 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3555 && !(convflags & LOOKUP_NO_TEMP_BIND))
3556 ics = build_conv (ck_rvalue, totype, ics);
3558 cand->second_conv = ics;
3560 if (!ics)
3562 cand->viable = 0;
3563 cand->reason = arg_conversion_rejection (NULL_TREE, -1,
3564 rettype, totype);
3566 else if (DECL_NONCONVERTING_P (cand->fn)
3567 && ics->rank > cr_exact)
3569 /* 13.3.1.5: For direct-initialization, those explicit
3570 conversion functions that are not hidden within S and
3571 yield type T or a type that can be converted to type T
3572 with a qualification conversion (4.4) are also candidate
3573 functions. */
3574 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
3575 I've raised this issue with the committee. --jason 9/2011 */
3576 cand->viable = -1;
3577 cand->reason = explicit_conversion_rejection (rettype, totype);
3579 else if (cand->viable == 1 && ics->bad_p)
3581 cand->viable = -1;
3582 cand->reason
3583 = bad_arg_conversion_rejection (NULL_TREE, -1,
3584 rettype, totype);
3586 else if (primary_template_instantiation_p (cand->fn)
3587 && ics->rank > cr_exact)
3589 /* 13.3.3.1.2: If the user-defined conversion is specified by
3590 a specialization of a conversion function template, the
3591 second standard conversion sequence shall have exact match
3592 rank. */
3593 cand->viable = -1;
3594 cand->reason = template_conversion_rejection (rettype, totype);
3599 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3600 if (!any_viable_p)
3602 if (args)
3603 release_tree_vector (args);
3604 return NULL;
3607 cand = tourney (candidates, complain);
3608 if (cand == 0)
3610 if ((flags & LOOKUP_COMPLAIN)
3611 && (complain & tf_error))
3613 error ("conversion from %qT to %qT is ambiguous",
3614 fromtype, totype);
3615 print_z_candidates (location_of (expr), candidates);
3618 cand = candidates; /* any one will do */
3619 cand->second_conv = build_ambiguous_conv (totype, expr);
3620 cand->second_conv->user_conv_p = true;
3621 if (!any_strictly_viable (candidates))
3622 cand->second_conv->bad_p = true;
3623 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3624 ambiguous conversion is no worse than another user-defined
3625 conversion. */
3627 return cand;
3630 /* Build the user conversion sequence. */
3631 conv = build_conv
3632 (ck_user,
3633 (DECL_CONSTRUCTOR_P (cand->fn)
3634 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
3635 build_identity_conv (TREE_TYPE (expr), expr));
3636 conv->cand = cand;
3637 if (cand->viable == -1)
3638 conv->bad_p = true;
3640 /* Remember that this was a list-initialization. */
3641 if (flags & LOOKUP_NO_NARROWING)
3642 conv->check_narrowing = true;
3644 /* Combine it with the second conversion sequence. */
3645 cand->second_conv = merge_conversion_sequences (conv,
3646 cand->second_conv);
3648 return cand;
3651 /* Wrapper for above. */
3653 tree
3654 build_user_type_conversion (tree totype, tree expr, int flags,
3655 tsubst_flags_t complain)
3657 struct z_candidate *cand;
3658 tree ret;
3660 bool subtime = timevar_cond_start (TV_OVERLOAD);
3661 cand = build_user_type_conversion_1 (totype, expr, flags, complain);
3663 if (cand)
3665 if (cand->second_conv->kind == ck_ambig)
3666 ret = error_mark_node;
3667 else
3669 expr = convert_like (cand->second_conv, expr, complain);
3670 ret = convert_from_reference (expr);
3673 else
3674 ret = NULL_TREE;
3676 timevar_cond_stop (TV_OVERLOAD, subtime);
3677 return ret;
3680 /* Subroutine of convert_nontype_argument.
3682 EXPR is an argument for a template non-type parameter of integral or
3683 enumeration type. Do any necessary conversions (that are permitted for
3684 non-type arguments) to convert it to the parameter type.
3686 If conversion is successful, returns the converted expression;
3687 otherwise, returns error_mark_node. */
3689 tree
3690 build_integral_nontype_arg_conv (tree type, tree expr, tsubst_flags_t complain)
3692 conversion *conv;
3693 void *p;
3694 tree t;
3696 if (error_operand_p (expr))
3697 return error_mark_node;
3699 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
3701 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3702 p = conversion_obstack_alloc (0);
3704 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
3705 /*c_cast_p=*/false,
3706 LOOKUP_IMPLICIT, complain);
3708 /* for a non-type template-parameter of integral or
3709 enumeration type, integral promotions (4.5) and integral
3710 conversions (4.7) are applied. */
3711 /* It should be sufficient to check the outermost conversion step, since
3712 there are no qualification conversions to integer type. */
3713 if (conv)
3714 switch (conv->kind)
3716 /* A conversion function is OK. If it isn't constexpr, we'll
3717 complain later that the argument isn't constant. */
3718 case ck_user:
3719 /* The lvalue-to-rvalue conversion is OK. */
3720 case ck_rvalue:
3721 case ck_identity:
3722 break;
3724 case ck_std:
3725 t = next_conversion (conv)->type;
3726 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t))
3727 break;
3729 if (complain & tf_error)
3730 error ("conversion from %qT to %qT not considered for "
3731 "non-type template argument", t, type);
3732 /* and fall through. */
3734 default:
3735 conv = NULL;
3736 break;
3739 if (conv)
3740 expr = convert_like (conv, expr, complain);
3741 else
3742 expr = error_mark_node;
3744 /* Free all the conversions we allocated. */
3745 obstack_free (&conversion_obstack, p);
3747 return expr;
3750 /* Do any initial processing on the arguments to a function call. */
3752 static VEC(tree,gc) *
3753 resolve_args (VEC(tree,gc) *args, tsubst_flags_t complain)
3755 unsigned int ix;
3756 tree arg;
3758 FOR_EACH_VEC_ELT (tree, args, ix, arg)
3760 if (error_operand_p (arg))
3761 return NULL;
3762 else if (VOID_TYPE_P (TREE_TYPE (arg)))
3764 if (complain & tf_error)
3765 error ("invalid use of void expression");
3766 return NULL;
3768 else if (invalid_nonstatic_memfn_p (arg, complain))
3769 return NULL;
3771 return args;
3774 /* Perform overload resolution on FN, which is called with the ARGS.
3776 Return the candidate function selected by overload resolution, or
3777 NULL if the event that overload resolution failed. In the case
3778 that overload resolution fails, *CANDIDATES will be the set of
3779 candidates considered, and ANY_VIABLE_P will be set to true or
3780 false to indicate whether or not any of the candidates were
3781 viable.
3783 The ARGS should already have gone through RESOLVE_ARGS before this
3784 function is called. */
3786 static struct z_candidate *
3787 perform_overload_resolution (tree fn,
3788 const VEC(tree,gc) *args,
3789 struct z_candidate **candidates,
3790 bool *any_viable_p, tsubst_flags_t complain)
3792 struct z_candidate *cand;
3793 tree explicit_targs;
3794 int template_only;
3796 bool subtime = timevar_cond_start (TV_OVERLOAD);
3798 explicit_targs = NULL_TREE;
3799 template_only = 0;
3801 *candidates = NULL;
3802 *any_viable_p = true;
3804 /* Check FN. */
3805 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
3806 || TREE_CODE (fn) == TEMPLATE_DECL
3807 || TREE_CODE (fn) == OVERLOAD
3808 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
3810 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3812 explicit_targs = TREE_OPERAND (fn, 1);
3813 fn = TREE_OPERAND (fn, 0);
3814 template_only = 1;
3817 /* Add the various candidate functions. */
3818 add_candidates (fn, NULL_TREE, args, NULL_TREE,
3819 explicit_targs, template_only,
3820 /*conversion_path=*/NULL_TREE,
3821 /*access_path=*/NULL_TREE,
3822 LOOKUP_NORMAL,
3823 candidates, complain);
3825 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
3826 if (*any_viable_p)
3827 cand = tourney (*candidates, complain);
3828 else
3829 cand = NULL;
3831 timevar_cond_stop (TV_OVERLOAD, subtime);
3832 return cand;
3835 /* Print an error message about being unable to build a call to FN with
3836 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
3837 be located; CANDIDATES is a possibly empty list of such
3838 functions. */
3840 static void
3841 print_error_for_call_failure (tree fn, VEC(tree,gc) *args, bool any_viable_p,
3842 struct z_candidate *candidates)
3844 tree name = DECL_NAME (OVL_CURRENT (fn));
3845 location_t loc = location_of (name);
3847 if (!any_viable_p)
3848 error_at (loc, "no matching function for call to %<%D(%A)%>",
3849 name, build_tree_list_vec (args));
3850 else
3851 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
3852 name, build_tree_list_vec (args));
3853 if (candidates)
3854 print_z_candidates (loc, candidates);
3857 /* Return an expression for a call to FN (a namespace-scope function,
3858 or a static member function) with the ARGS. This may change
3859 ARGS. */
3861 tree
3862 build_new_function_call (tree fn, VEC(tree,gc) **args, bool koenig_p,
3863 tsubst_flags_t complain)
3865 struct z_candidate *candidates, *cand;
3866 bool any_viable_p;
3867 void *p;
3868 tree result;
3870 if (args != NULL && *args != NULL)
3872 *args = resolve_args (*args, complain);
3873 if (*args == NULL)
3874 return error_mark_node;
3877 if (flag_tm)
3878 tm_malloc_replacement (fn);
3880 /* If this function was found without using argument dependent
3881 lookup, then we want to ignore any undeclared friend
3882 functions. */
3883 if (!koenig_p)
3885 tree orig_fn = fn;
3887 fn = remove_hidden_names (fn);
3888 if (!fn)
3890 if (complain & tf_error)
3891 print_error_for_call_failure (orig_fn, *args, false, NULL);
3892 return error_mark_node;
3896 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3897 p = conversion_obstack_alloc (0);
3899 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
3900 complain);
3902 if (!cand)
3904 if (complain & tf_error)
3906 if (!any_viable_p && candidates && ! candidates->next
3907 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
3908 return cp_build_function_call_vec (candidates->fn, args, complain);
3909 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3910 fn = TREE_OPERAND (fn, 0);
3911 print_error_for_call_failure (fn, *args, any_viable_p, candidates);
3913 result = error_mark_node;
3915 else
3917 int flags = LOOKUP_NORMAL;
3918 /* If fn is template_id_expr, the call has explicit template arguments
3919 (e.g. func<int>(5)), communicate this info to build_over_call
3920 through flags so that later we can use it to decide whether to warn
3921 about peculiar null pointer conversion. */
3922 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3923 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
3924 result = build_over_call (cand, flags, complain);
3927 /* Free all the conversions we allocated. */
3928 obstack_free (&conversion_obstack, p);
3930 return result;
3933 /* Build a call to a global operator new. FNNAME is the name of the
3934 operator (either "operator new" or "operator new[]") and ARGS are
3935 the arguments provided. This may change ARGS. *SIZE points to the
3936 total number of bytes required by the allocation, and is updated if
3937 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
3938 be used. If this function determines that no cookie should be
3939 used, after all, *COOKIE_SIZE is set to NULL_TREE. If FN is
3940 non-NULL, it will be set, upon return, to the allocation function
3941 called. */
3943 tree
3944 build_operator_new_call (tree fnname, VEC(tree,gc) **args,
3945 tree *size, tree *cookie_size,
3946 tree *fn, tsubst_flags_t complain)
3948 tree fns;
3949 struct z_candidate *candidates;
3950 struct z_candidate *cand;
3951 bool any_viable_p;
3953 if (fn)
3954 *fn = NULL_TREE;
3955 VEC_safe_insert (tree, gc, *args, 0, *size);
3956 *args = resolve_args (*args, complain);
3957 if (*args == NULL)
3958 return error_mark_node;
3960 /* Based on:
3962 [expr.new]
3964 If this lookup fails to find the name, or if the allocated type
3965 is not a class type, the allocation function's name is looked
3966 up in the global scope.
3968 we disregard block-scope declarations of "operator new". */
3969 fns = lookup_function_nonclass (fnname, *args, /*block_p=*/false);
3971 /* Figure out what function is being called. */
3972 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
3973 complain);
3975 /* If no suitable function could be found, issue an error message
3976 and give up. */
3977 if (!cand)
3979 if (complain & tf_error)
3980 print_error_for_call_failure (fns, *args, any_viable_p, candidates);
3981 return error_mark_node;
3984 /* If a cookie is required, add some extra space. Whether
3985 or not a cookie is required cannot be determined until
3986 after we know which function was called. */
3987 if (*cookie_size)
3989 bool use_cookie = true;
3990 if (!abi_version_at_least (2))
3992 /* In G++ 3.2, the check was implemented incorrectly; it
3993 looked at the placement expression, rather than the
3994 type of the function. */
3995 if (VEC_length (tree, *args) == 2
3996 && same_type_p (TREE_TYPE (VEC_index (tree, *args, 1)),
3997 ptr_type_node))
3998 use_cookie = false;
4000 else
4002 tree arg_types;
4004 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
4005 /* Skip the size_t parameter. */
4006 arg_types = TREE_CHAIN (arg_types);
4007 /* Check the remaining parameters (if any). */
4008 if (arg_types
4009 && TREE_CHAIN (arg_types) == void_list_node
4010 && same_type_p (TREE_VALUE (arg_types),
4011 ptr_type_node))
4012 use_cookie = false;
4014 /* If we need a cookie, adjust the number of bytes allocated. */
4015 if (use_cookie)
4017 /* Update the total size. */
4018 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
4019 /* Update the argument list to reflect the adjusted size. */
4020 VEC_replace (tree, *args, 0, *size);
4022 else
4023 *cookie_size = NULL_TREE;
4026 /* Tell our caller which function we decided to call. */
4027 if (fn)
4028 *fn = cand->fn;
4030 /* Build the CALL_EXPR. */
4031 return build_over_call (cand, LOOKUP_NORMAL, complain);
4034 /* Build a new call to operator(). This may change ARGS. */
4036 static tree
4037 build_op_call_1 (tree obj, VEC(tree,gc) **args, tsubst_flags_t complain)
4039 struct z_candidate *candidates = 0, *cand;
4040 tree fns, convs, first_mem_arg = NULL_TREE;
4041 tree type = TREE_TYPE (obj);
4042 bool any_viable_p;
4043 tree result = NULL_TREE;
4044 void *p;
4046 if (error_operand_p (obj))
4047 return error_mark_node;
4049 obj = prep_operand (obj);
4051 if (TYPE_PTRMEMFUNC_P (type))
4053 if (complain & tf_error)
4054 /* It's no good looking for an overloaded operator() on a
4055 pointer-to-member-function. */
4056 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
4057 return error_mark_node;
4060 if (TYPE_BINFO (type))
4062 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
4063 if (fns == error_mark_node)
4064 return error_mark_node;
4066 else
4067 fns = NULL_TREE;
4069 if (args != NULL && *args != NULL)
4071 *args = resolve_args (*args, complain);
4072 if (*args == NULL)
4073 return error_mark_node;
4076 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4077 p = conversion_obstack_alloc (0);
4079 if (fns)
4081 first_mem_arg = build_this (obj);
4083 add_candidates (BASELINK_FUNCTIONS (fns),
4084 first_mem_arg, *args, NULL_TREE,
4085 NULL_TREE, false,
4086 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
4087 LOOKUP_NORMAL, &candidates, complain);
4090 convs = lookup_conversions (type);
4092 for (; convs; convs = TREE_CHAIN (convs))
4094 tree fns = TREE_VALUE (convs);
4095 tree totype = TREE_TYPE (convs);
4097 if ((TREE_CODE (totype) == POINTER_TYPE
4098 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
4099 || (TREE_CODE (totype) == REFERENCE_TYPE
4100 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
4101 || (TREE_CODE (totype) == REFERENCE_TYPE
4102 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
4103 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
4104 for (; fns; fns = OVL_NEXT (fns))
4106 tree fn = OVL_CURRENT (fns);
4108 if (DECL_NONCONVERTING_P (fn))
4109 continue;
4111 if (TREE_CODE (fn) == TEMPLATE_DECL)
4112 add_template_conv_candidate
4113 (&candidates, fn, obj, NULL_TREE, *args, totype,
4114 /*access_path=*/NULL_TREE,
4115 /*conversion_path=*/NULL_TREE, complain);
4116 else
4117 add_conv_candidate (&candidates, fn, obj, NULL_TREE,
4118 *args, /*conversion_path=*/NULL_TREE,
4119 /*access_path=*/NULL_TREE, complain);
4123 candidates = splice_viable (candidates, pedantic, &any_viable_p);
4124 if (!any_viable_p)
4126 if (complain & tf_error)
4128 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
4129 build_tree_list_vec (*args));
4130 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4132 result = error_mark_node;
4134 else
4136 cand = tourney (candidates, complain);
4137 if (cand == 0)
4139 if (complain & tf_error)
4141 error ("call of %<(%T) (%A)%> is ambiguous",
4142 TREE_TYPE (obj), build_tree_list_vec (*args));
4143 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4145 result = error_mark_node;
4147 /* Since cand->fn will be a type, not a function, for a conversion
4148 function, we must be careful not to unconditionally look at
4149 DECL_NAME here. */
4150 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
4151 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
4152 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4153 else
4155 obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1,
4156 complain);
4157 obj = convert_from_reference (obj);
4158 result = cp_build_function_call_vec (obj, args, complain);
4162 /* Free all the conversions we allocated. */
4163 obstack_free (&conversion_obstack, p);
4165 return result;
4168 /* Wrapper for above. */
4170 tree
4171 build_op_call (tree obj, VEC(tree,gc) **args, tsubst_flags_t complain)
4173 tree ret;
4174 bool subtime = timevar_cond_start (TV_OVERLOAD);
4175 ret = build_op_call_1 (obj, args, complain);
4176 timevar_cond_stop (TV_OVERLOAD, subtime);
4177 return ret;
4180 /* Called by op_error to prepare format strings suitable for the error
4181 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4182 and a suffix (controlled by NTYPES). */
4184 static const char *
4185 op_error_string (const char *errmsg, int ntypes, bool match)
4187 const char *msg;
4189 const char *msgp = concat (match ? G_("ambiguous overload for ")
4190 : G_("no match for "), errmsg, NULL);
4192 if (ntypes == 3)
4193 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
4194 else if (ntypes == 2)
4195 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
4196 else
4197 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
4199 return msg;
4202 static void
4203 op_error (enum tree_code code, enum tree_code code2,
4204 tree arg1, tree arg2, tree arg3, bool match)
4206 const char *opname;
4208 if (code == MODIFY_EXPR)
4209 opname = assignment_operator_name_info[code2].name;
4210 else
4211 opname = operator_name_info[code].name;
4213 switch (code)
4215 case COND_EXPR:
4216 if (flag_diagnostics_show_caret)
4217 error (op_error_string (G_("ternary %<operator?:%>"), 3, match),
4218 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4219 else
4220 error (op_error_string (G_("ternary %<operator?:%> "
4221 "in %<%E ? %E : %E%>"), 3, match),
4222 arg1, arg2, arg3,
4223 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4224 break;
4226 case POSTINCREMENT_EXPR:
4227 case POSTDECREMENT_EXPR:
4228 if (flag_diagnostics_show_caret)
4229 error (op_error_string (G_("%<operator%s%>"), 1, match),
4230 opname, TREE_TYPE (arg1));
4231 else
4232 error (op_error_string (G_("%<operator%s%> in %<%E%s%>"), 1, match),
4233 opname, arg1, opname, TREE_TYPE (arg1));
4234 break;
4236 case ARRAY_REF:
4237 if (flag_diagnostics_show_caret)
4238 error (op_error_string (G_("%<operator[]%>"), 2, match),
4239 TREE_TYPE (arg1), TREE_TYPE (arg2));
4240 else
4241 error (op_error_string (G_("%<operator[]%> in %<%E[%E]%>"), 2, match),
4242 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
4243 break;
4245 case REALPART_EXPR:
4246 case IMAGPART_EXPR:
4247 if (flag_diagnostics_show_caret)
4248 error (op_error_string (G_("%qs"), 1, match),
4249 opname, TREE_TYPE (arg1));
4250 else
4251 error (op_error_string (G_("%qs in %<%s %E%>"), 1, match),
4252 opname, opname, arg1, TREE_TYPE (arg1));
4253 break;
4255 default:
4256 if (arg2)
4257 if (flag_diagnostics_show_caret)
4258 error (op_error_string (G_("%<operator%s%>"), 2, match),
4259 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
4260 else
4261 error (op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
4262 2, match),
4263 opname, arg1, opname, arg2,
4264 TREE_TYPE (arg1), TREE_TYPE (arg2));
4265 else
4266 if (flag_diagnostics_show_caret)
4267 error (op_error_string (G_("%<operator%s%>"), 1, match),
4268 opname, TREE_TYPE (arg1));
4269 else
4270 error (op_error_string (G_("%<operator%s%> in %<%s%E%>"),
4271 1, match),
4272 opname, opname, arg1, TREE_TYPE (arg1));
4273 break;
4277 /* Return the implicit conversion sequence that could be used to
4278 convert E1 to E2 in [expr.cond]. */
4280 static conversion *
4281 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
4283 tree t1 = non_reference (TREE_TYPE (e1));
4284 tree t2 = non_reference (TREE_TYPE (e2));
4285 conversion *conv;
4286 bool good_base;
4288 /* [expr.cond]
4290 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
4291 implicitly converted (clause _conv_) to the type "lvalue reference to
4292 T2", subject to the constraint that in the conversion the
4293 reference must bind directly (_dcl.init.ref_) to an lvalue. */
4294 if (real_lvalue_p (e2))
4296 conv = implicit_conversion (build_reference_type (t2),
4299 /*c_cast_p=*/false,
4300 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
4301 |LOOKUP_ONLYCONVERTING,
4302 complain);
4303 if (conv)
4304 return conv;
4307 /* [expr.cond]
4309 If E1 and E2 have class type, and the underlying class types are
4310 the same or one is a base class of the other: E1 can be converted
4311 to match E2 if the class of T2 is the same type as, or a base
4312 class of, the class of T1, and the cv-qualification of T2 is the
4313 same cv-qualification as, or a greater cv-qualification than, the
4314 cv-qualification of T1. If the conversion is applied, E1 is
4315 changed to an rvalue of type T2 that still refers to the original
4316 source class object (or the appropriate subobject thereof). */
4317 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
4318 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
4320 if (good_base && at_least_as_qualified_p (t2, t1))
4322 conv = build_identity_conv (t1, e1);
4323 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
4324 TYPE_MAIN_VARIANT (t2)))
4325 conv = build_conv (ck_base, t2, conv);
4326 else
4327 conv = build_conv (ck_rvalue, t2, conv);
4328 return conv;
4330 else
4331 return NULL;
4333 else
4334 /* [expr.cond]
4336 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
4337 converted to the type that expression E2 would have if E2 were
4338 converted to an rvalue (or the type it has, if E2 is an rvalue). */
4339 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
4340 LOOKUP_IMPLICIT, complain);
4343 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
4344 arguments to the conditional expression. */
4346 static tree
4347 build_conditional_expr_1 (tree arg1, tree arg2, tree arg3,
4348 tsubst_flags_t complain)
4350 tree arg2_type;
4351 tree arg3_type;
4352 tree result = NULL_TREE;
4353 tree result_type = NULL_TREE;
4354 bool lvalue_p = true;
4355 struct z_candidate *candidates = 0;
4356 struct z_candidate *cand;
4357 void *p;
4359 /* As a G++ extension, the second argument to the conditional can be
4360 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
4361 c'.) If the second operand is omitted, make sure it is
4362 calculated only once. */
4363 if (!arg2)
4365 if (complain & tf_error)
4366 pedwarn (input_location, OPT_Wpedantic,
4367 "ISO C++ forbids omitting the middle term of a ?: expression");
4369 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
4370 if (real_lvalue_p (arg1))
4371 arg2 = arg1 = stabilize_reference (arg1);
4372 else
4373 arg2 = arg1 = save_expr (arg1);
4376 /* [expr.cond]
4378 The first expression is implicitly converted to bool (clause
4379 _conv_). */
4380 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
4381 LOOKUP_NORMAL);
4383 /* If something has already gone wrong, just pass that fact up the
4384 tree. */
4385 if (error_operand_p (arg1)
4386 || error_operand_p (arg2)
4387 || error_operand_p (arg3))
4388 return error_mark_node;
4390 /* [expr.cond]
4392 If either the second or the third operand has type (possibly
4393 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
4394 array-to-pointer (_conv.array_), and function-to-pointer
4395 (_conv.func_) standard conversions are performed on the second
4396 and third operands. */
4397 arg2_type = unlowered_expr_type (arg2);
4398 arg3_type = unlowered_expr_type (arg3);
4399 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
4401 /* Do the conversions. We don't these for `void' type arguments
4402 since it can't have any effect and since decay_conversion
4403 does not handle that case gracefully. */
4404 if (!VOID_TYPE_P (arg2_type))
4405 arg2 = decay_conversion (arg2, complain);
4406 if (!VOID_TYPE_P (arg3_type))
4407 arg3 = decay_conversion (arg3, complain);
4408 arg2_type = TREE_TYPE (arg2);
4409 arg3_type = TREE_TYPE (arg3);
4411 /* [expr.cond]
4413 One of the following shall hold:
4415 --The second or the third operand (but not both) is a
4416 throw-expression (_except.throw_); the result is of the
4417 type of the other and is an rvalue.
4419 --Both the second and the third operands have type void; the
4420 result is of type void and is an rvalue.
4422 We must avoid calling force_rvalue for expressions of type
4423 "void" because it will complain that their value is being
4424 used. */
4425 if (TREE_CODE (arg2) == THROW_EXPR
4426 && TREE_CODE (arg3) != THROW_EXPR)
4428 if (!VOID_TYPE_P (arg3_type))
4430 arg3 = force_rvalue (arg3, complain);
4431 if (arg3 == error_mark_node)
4432 return error_mark_node;
4434 arg3_type = TREE_TYPE (arg3);
4435 result_type = arg3_type;
4437 else if (TREE_CODE (arg2) != THROW_EXPR
4438 && TREE_CODE (arg3) == THROW_EXPR)
4440 if (!VOID_TYPE_P (arg2_type))
4442 arg2 = force_rvalue (arg2, complain);
4443 if (arg2 == error_mark_node)
4444 return error_mark_node;
4446 arg2_type = TREE_TYPE (arg2);
4447 result_type = arg2_type;
4449 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
4450 result_type = void_type_node;
4451 else
4453 if (complain & tf_error)
4455 if (VOID_TYPE_P (arg2_type))
4456 error ("second operand to the conditional operator "
4457 "is of type %<void%>, "
4458 "but the third operand is neither a throw-expression "
4459 "nor of type %<void%>");
4460 else
4461 error ("third operand to the conditional operator "
4462 "is of type %<void%>, "
4463 "but the second operand is neither a throw-expression "
4464 "nor of type %<void%>");
4466 return error_mark_node;
4469 lvalue_p = false;
4470 goto valid_operands;
4472 /* [expr.cond]
4474 Otherwise, if the second and third operand have different types,
4475 and either has (possibly cv-qualified) class type, an attempt is
4476 made to convert each of those operands to the type of the other. */
4477 else if (!same_type_p (arg2_type, arg3_type)
4478 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
4480 conversion *conv2;
4481 conversion *conv3;
4483 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4484 p = conversion_obstack_alloc (0);
4486 conv2 = conditional_conversion (arg2, arg3, complain);
4487 conv3 = conditional_conversion (arg3, arg2, complain);
4489 /* [expr.cond]
4491 If both can be converted, or one can be converted but the
4492 conversion is ambiguous, the program is ill-formed. If
4493 neither can be converted, the operands are left unchanged and
4494 further checking is performed as described below. If exactly
4495 one conversion is possible, that conversion is applied to the
4496 chosen operand and the converted operand is used in place of
4497 the original operand for the remainder of this section. */
4498 if ((conv2 && !conv2->bad_p
4499 && conv3 && !conv3->bad_p)
4500 || (conv2 && conv2->kind == ck_ambig)
4501 || (conv3 && conv3->kind == ck_ambig))
4503 error ("operands to ?: have different types %qT and %qT",
4504 arg2_type, arg3_type);
4505 result = error_mark_node;
4507 else if (conv2 && (!conv2->bad_p || !conv3))
4509 arg2 = convert_like (conv2, arg2, complain);
4510 arg2 = convert_from_reference (arg2);
4511 arg2_type = TREE_TYPE (arg2);
4512 /* Even if CONV2 is a valid conversion, the result of the
4513 conversion may be invalid. For example, if ARG3 has type
4514 "volatile X", and X does not have a copy constructor
4515 accepting a "volatile X&", then even if ARG2 can be
4516 converted to X, the conversion will fail. */
4517 if (error_operand_p (arg2))
4518 result = error_mark_node;
4520 else if (conv3 && (!conv3->bad_p || !conv2))
4522 arg3 = convert_like (conv3, arg3, complain);
4523 arg3 = convert_from_reference (arg3);
4524 arg3_type = TREE_TYPE (arg3);
4525 if (error_operand_p (arg3))
4526 result = error_mark_node;
4529 /* Free all the conversions we allocated. */
4530 obstack_free (&conversion_obstack, p);
4532 if (result)
4533 return result;
4535 /* If, after the conversion, both operands have class type,
4536 treat the cv-qualification of both operands as if it were the
4537 union of the cv-qualification of the operands.
4539 The standard is not clear about what to do in this
4540 circumstance. For example, if the first operand has type
4541 "const X" and the second operand has a user-defined
4542 conversion to "volatile X", what is the type of the second
4543 operand after this step? Making it be "const X" (matching
4544 the first operand) seems wrong, as that discards the
4545 qualification without actually performing a copy. Leaving it
4546 as "volatile X" seems wrong as that will result in the
4547 conditional expression failing altogether, even though,
4548 according to this step, the one operand could be converted to
4549 the type of the other. */
4550 if ((conv2 || conv3)
4551 && CLASS_TYPE_P (arg2_type)
4552 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
4553 arg2_type = arg3_type =
4554 cp_build_qualified_type (arg2_type,
4555 cp_type_quals (arg2_type)
4556 | cp_type_quals (arg3_type));
4559 /* [expr.cond]
4561 If the second and third operands are lvalues and have the same
4562 type, the result is of that type and is an lvalue. */
4563 if (real_lvalue_p (arg2)
4564 && real_lvalue_p (arg3)
4565 && same_type_p (arg2_type, arg3_type))
4567 result_type = arg2_type;
4568 arg2 = mark_lvalue_use (arg2);
4569 arg3 = mark_lvalue_use (arg3);
4570 goto valid_operands;
4573 /* [expr.cond]
4575 Otherwise, the result is an rvalue. If the second and third
4576 operand do not have the same type, and either has (possibly
4577 cv-qualified) class type, overload resolution is used to
4578 determine the conversions (if any) to be applied to the operands
4579 (_over.match.oper_, _over.built_). */
4580 lvalue_p = false;
4581 if (!same_type_p (arg2_type, arg3_type)
4582 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
4584 tree args[3];
4585 conversion *conv;
4586 bool any_viable_p;
4588 /* Rearrange the arguments so that add_builtin_candidate only has
4589 to know about two args. In build_builtin_candidate, the
4590 arguments are unscrambled. */
4591 args[0] = arg2;
4592 args[1] = arg3;
4593 args[2] = arg1;
4594 add_builtin_candidates (&candidates,
4595 COND_EXPR,
4596 NOP_EXPR,
4597 ansi_opname (COND_EXPR),
4598 args,
4599 LOOKUP_NORMAL, complain);
4601 /* [expr.cond]
4603 If the overload resolution fails, the program is
4604 ill-formed. */
4605 candidates = splice_viable (candidates, pedantic, &any_viable_p);
4606 if (!any_viable_p)
4608 if (complain & tf_error)
4610 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
4611 print_z_candidates (location_of (arg1), candidates);
4613 return error_mark_node;
4615 cand = tourney (candidates, complain);
4616 if (!cand)
4618 if (complain & tf_error)
4620 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
4621 print_z_candidates (location_of (arg1), candidates);
4623 return error_mark_node;
4626 /* [expr.cond]
4628 Otherwise, the conversions thus determined are applied, and
4629 the converted operands are used in place of the original
4630 operands for the remainder of this section. */
4631 conv = cand->convs[0];
4632 arg1 = convert_like (conv, arg1, complain);
4633 conv = cand->convs[1];
4634 arg2 = convert_like (conv, arg2, complain);
4635 arg2_type = TREE_TYPE (arg2);
4636 conv = cand->convs[2];
4637 arg3 = convert_like (conv, arg3, complain);
4638 arg3_type = TREE_TYPE (arg3);
4641 /* [expr.cond]
4643 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
4644 and function-to-pointer (_conv.func_) standard conversions are
4645 performed on the second and third operands.
4647 We need to force the lvalue-to-rvalue conversion here for class types,
4648 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
4649 that isn't wrapped with a TARGET_EXPR plays havoc with exception
4650 regions. */
4652 arg2 = force_rvalue (arg2, complain);
4653 if (!CLASS_TYPE_P (arg2_type))
4654 arg2_type = TREE_TYPE (arg2);
4656 arg3 = force_rvalue (arg3, complain);
4657 if (!CLASS_TYPE_P (arg3_type))
4658 arg3_type = TREE_TYPE (arg3);
4660 if (arg2 == error_mark_node || arg3 == error_mark_node)
4661 return error_mark_node;
4663 /* [expr.cond]
4665 After those conversions, one of the following shall hold:
4667 --The second and third operands have the same type; the result is of
4668 that type. */
4669 if (same_type_p (arg2_type, arg3_type))
4670 result_type = arg2_type;
4671 /* [expr.cond]
4673 --The second and third operands have arithmetic or enumeration
4674 type; the usual arithmetic conversions are performed to bring
4675 them to a common type, and the result is of that type. */
4676 else if ((ARITHMETIC_TYPE_P (arg2_type)
4677 || UNSCOPED_ENUM_P (arg2_type))
4678 && (ARITHMETIC_TYPE_P (arg3_type)
4679 || UNSCOPED_ENUM_P (arg3_type)))
4681 /* In this case, there is always a common type. */
4682 result_type = type_after_usual_arithmetic_conversions (arg2_type,
4683 arg3_type);
4684 do_warn_double_promotion (result_type, arg2_type, arg3_type,
4685 "implicit conversion from %qT to %qT to "
4686 "match other result of conditional",
4687 input_location);
4689 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
4690 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
4692 if (complain & tf_warning)
4693 warning (0,
4694 "enumeral mismatch in conditional expression: %qT vs %qT",
4695 arg2_type, arg3_type);
4697 else if (extra_warnings
4698 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
4699 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
4700 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
4701 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
4703 if (complain & tf_warning)
4704 warning (0,
4705 "enumeral and non-enumeral type in conditional expression");
4708 arg2 = perform_implicit_conversion (result_type, arg2, complain);
4709 arg3 = perform_implicit_conversion (result_type, arg3, complain);
4711 /* [expr.cond]
4713 --The second and third operands have pointer type, or one has
4714 pointer type and the other is a null pointer constant; pointer
4715 conversions (_conv.ptr_) and qualification conversions
4716 (_conv.qual_) are performed to bring them to their composite
4717 pointer type (_expr.rel_). The result is of the composite
4718 pointer type.
4720 --The second and third operands have pointer to member type, or
4721 one has pointer to member type and the other is a null pointer
4722 constant; pointer to member conversions (_conv.mem_) and
4723 qualification conversions (_conv.qual_) are performed to bring
4724 them to a common type, whose cv-qualification shall match the
4725 cv-qualification of either the second or the third operand.
4726 The result is of the common type. */
4727 else if ((null_ptr_cst_p (arg2)
4728 && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
4729 || (null_ptr_cst_p (arg3)
4730 && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
4731 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
4732 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
4733 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
4735 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
4736 arg3, CPO_CONDITIONAL_EXPR,
4737 complain);
4738 if (result_type == error_mark_node)
4739 return error_mark_node;
4740 arg2 = perform_implicit_conversion (result_type, arg2, complain);
4741 arg3 = perform_implicit_conversion (result_type, arg3, complain);
4744 if (!result_type)
4746 if (complain & tf_error)
4747 error ("operands to ?: have different types %qT and %qT",
4748 arg2_type, arg3_type);
4749 return error_mark_node;
4752 valid_operands:
4753 result = build3 (COND_EXPR, result_type, arg1, arg2, arg3);
4754 if (!cp_unevaluated_operand)
4755 /* Avoid folding within decltype (c++/42013) and noexcept. */
4756 result = fold_if_not_in_template (result);
4758 /* We can't use result_type below, as fold might have returned a
4759 throw_expr. */
4761 if (!lvalue_p)
4763 /* Expand both sides into the same slot, hopefully the target of
4764 the ?: expression. We used to check for TARGET_EXPRs here,
4765 but now we sometimes wrap them in NOP_EXPRs so the test would
4766 fail. */
4767 if (CLASS_TYPE_P (TREE_TYPE (result)))
4768 result = get_target_expr (result);
4769 /* If this expression is an rvalue, but might be mistaken for an
4770 lvalue, we must add a NON_LVALUE_EXPR. */
4771 result = rvalue (result);
4774 return result;
4777 /* Wrapper for above. */
4779 tree
4780 build_conditional_expr (tree arg1, tree arg2, tree arg3,
4781 tsubst_flags_t complain)
4783 tree ret;
4784 bool subtime = timevar_cond_start (TV_OVERLOAD);
4785 ret = build_conditional_expr_1 (arg1, arg2, arg3, complain);
4786 timevar_cond_stop (TV_OVERLOAD, subtime);
4787 return ret;
4790 /* OPERAND is an operand to an expression. Perform necessary steps
4791 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
4792 returned. */
4794 static tree
4795 prep_operand (tree operand)
4797 if (operand)
4799 if (CLASS_TYPE_P (TREE_TYPE (operand))
4800 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
4801 /* Make sure the template type is instantiated now. */
4802 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
4805 return operand;
4808 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
4809 OVERLOAD) to the CANDIDATES, returning an updated list of
4810 CANDIDATES. The ARGS are the arguments provided to the call;
4811 if FIRST_ARG is non-null it is the implicit object argument,
4812 otherwise the first element of ARGS is used if needed. The
4813 EXPLICIT_TARGS are explicit template arguments provided.
4814 TEMPLATE_ONLY is true if only template functions should be
4815 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
4816 add_function_candidate. */
4818 static void
4819 add_candidates (tree fns, tree first_arg, const VEC(tree,gc) *args,
4820 tree return_type,
4821 tree explicit_targs, bool template_only,
4822 tree conversion_path, tree access_path,
4823 int flags,
4824 struct z_candidate **candidates,
4825 tsubst_flags_t complain)
4827 tree ctype;
4828 const VEC(tree,gc) *non_static_args;
4829 bool check_list_ctor;
4830 bool check_converting;
4831 unification_kind_t strict;
4832 tree fn;
4834 if (!fns)
4835 return;
4837 /* Precalculate special handling of constructors and conversion ops. */
4838 fn = OVL_CURRENT (fns);
4839 if (DECL_CONV_FN_P (fn))
4841 check_list_ctor = false;
4842 check_converting = !!(flags & LOOKUP_ONLYCONVERTING);
4843 if (flags & LOOKUP_NO_CONVERSION)
4844 /* We're doing return_type(x). */
4845 strict = DEDUCE_CONV;
4846 else
4847 /* We're doing x.operator return_type(). */
4848 strict = DEDUCE_EXACT;
4849 /* [over.match.funcs] For conversion functions, the function
4850 is considered to be a member of the class of the implicit
4851 object argument for the purpose of defining the type of
4852 the implicit object parameter. */
4853 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (first_arg)));
4855 else
4857 if (DECL_CONSTRUCTOR_P (fn))
4859 check_list_ctor = !!(flags & LOOKUP_LIST_ONLY);
4860 /* For list-initialization we consider explicit constructors
4861 and complain if one is chosen. */
4862 check_converting
4863 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
4864 == LOOKUP_ONLYCONVERTING);
4866 else
4868 check_list_ctor = false;
4869 check_converting = false;
4871 strict = DEDUCE_CALL;
4872 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
4875 if (first_arg)
4876 non_static_args = args;
4877 else
4878 /* Delay creating the implicit this parameter until it is needed. */
4879 non_static_args = NULL;
4881 for (; fns; fns = OVL_NEXT (fns))
4883 tree fn_first_arg;
4884 const VEC(tree,gc) *fn_args;
4886 fn = OVL_CURRENT (fns);
4888 if (check_converting && DECL_NONCONVERTING_P (fn))
4889 continue;
4890 if (check_list_ctor && !is_list_ctor (fn))
4891 continue;
4893 /* Figure out which set of arguments to use. */
4894 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4896 /* If this function is a non-static member and we didn't get an
4897 implicit object argument, move it out of args. */
4898 if (first_arg == NULL_TREE)
4900 unsigned int ix;
4901 tree arg;
4902 VEC(tree,gc) *tempvec
4903 = VEC_alloc (tree, gc, VEC_length (tree, args) - 1);
4904 for (ix = 1; VEC_iterate (tree, args, ix, arg); ++ix)
4905 VEC_quick_push (tree, tempvec, arg);
4906 non_static_args = tempvec;
4907 first_arg = build_this (VEC_index (tree, args, 0));
4910 fn_first_arg = first_arg;
4911 fn_args = non_static_args;
4913 else
4915 /* Otherwise, just use the list of arguments provided. */
4916 fn_first_arg = NULL_TREE;
4917 fn_args = args;
4920 if (TREE_CODE (fn) == TEMPLATE_DECL)
4921 add_template_candidate (candidates,
4923 ctype,
4924 explicit_targs,
4925 fn_first_arg,
4926 fn_args,
4927 return_type,
4928 access_path,
4929 conversion_path,
4930 flags,
4931 strict,
4932 complain);
4933 else if (!template_only)
4934 add_function_candidate (candidates,
4936 ctype,
4937 fn_first_arg,
4938 fn_args,
4939 access_path,
4940 conversion_path,
4941 flags,
4942 complain);
4946 static tree
4947 build_new_op_1 (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
4948 tree *overload, tsubst_flags_t complain)
4950 struct z_candidate *candidates = 0, *cand;
4951 VEC(tree,gc) *arglist;
4952 tree fnname;
4953 tree args[3];
4954 tree result = NULL_TREE;
4955 bool result_valid_p = false;
4956 enum tree_code code2 = NOP_EXPR;
4957 enum tree_code code_orig_arg1 = ERROR_MARK;
4958 enum tree_code code_orig_arg2 = ERROR_MARK;
4959 conversion *conv;
4960 void *p;
4961 bool strict_p;
4962 bool any_viable_p;
4964 if (error_operand_p (arg1)
4965 || error_operand_p (arg2)
4966 || error_operand_p (arg3))
4967 return error_mark_node;
4969 if (code == MODIFY_EXPR)
4971 code2 = TREE_CODE (arg3);
4972 arg3 = NULL_TREE;
4973 fnname = ansi_assopname (code2);
4975 else
4976 fnname = ansi_opname (code);
4978 arg1 = prep_operand (arg1);
4980 switch (code)
4982 case NEW_EXPR:
4983 case VEC_NEW_EXPR:
4984 case VEC_DELETE_EXPR:
4985 case DELETE_EXPR:
4986 /* Use build_op_new_call and build_op_delete_call instead. */
4987 gcc_unreachable ();
4989 case CALL_EXPR:
4990 /* Use build_op_call instead. */
4991 gcc_unreachable ();
4993 case TRUTH_ORIF_EXPR:
4994 case TRUTH_ANDIF_EXPR:
4995 case TRUTH_AND_EXPR:
4996 case TRUTH_OR_EXPR:
4997 /* These are saved for the sake of warn_logical_operator. */
4998 code_orig_arg1 = TREE_CODE (arg1);
4999 code_orig_arg2 = TREE_CODE (arg2);
5001 default:
5002 break;
5005 arg2 = prep_operand (arg2);
5006 arg3 = prep_operand (arg3);
5008 if (code == COND_EXPR)
5009 /* Use build_conditional_expr instead. */
5010 gcc_unreachable ();
5011 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
5012 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
5013 goto builtin;
5015 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5016 arg2 = integer_zero_node;
5018 arglist = VEC_alloc (tree, gc, 3);
5019 VEC_quick_push (tree, arglist, arg1);
5020 if (arg2 != NULL_TREE)
5021 VEC_quick_push (tree, arglist, arg2);
5022 if (arg3 != NULL_TREE)
5023 VEC_quick_push (tree, arglist, arg3);
5025 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5026 p = conversion_obstack_alloc (0);
5028 /* Add namespace-scope operators to the list of functions to
5029 consider. */
5030 add_candidates (lookup_function_nonclass (fnname, arglist, /*block_p=*/true),
5031 NULL_TREE, arglist, NULL_TREE,
5032 NULL_TREE, false, NULL_TREE, NULL_TREE,
5033 flags, &candidates, complain);
5034 /* Add class-member operators to the candidate set. */
5035 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
5037 tree fns;
5039 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
5040 if (fns == error_mark_node)
5042 result = error_mark_node;
5043 goto user_defined_result_ready;
5045 if (fns)
5046 add_candidates (BASELINK_FUNCTIONS (fns),
5047 NULL_TREE, arglist, NULL_TREE,
5048 NULL_TREE, false,
5049 BASELINK_BINFO (fns),
5050 BASELINK_ACCESS_BINFO (fns),
5051 flags, &candidates, complain);
5054 args[0] = arg1;
5055 args[1] = arg2;
5056 args[2] = NULL_TREE;
5058 add_builtin_candidates (&candidates, code, code2, fnname, args,
5059 flags, complain);
5061 switch (code)
5063 case COMPOUND_EXPR:
5064 case ADDR_EXPR:
5065 /* For these, the built-in candidates set is empty
5066 [over.match.oper]/3. We don't want non-strict matches
5067 because exact matches are always possible with built-in
5068 operators. The built-in candidate set for COMPONENT_REF
5069 would be empty too, but since there are no such built-in
5070 operators, we accept non-strict matches for them. */
5071 strict_p = true;
5072 break;
5074 default:
5075 strict_p = pedantic;
5076 break;
5079 candidates = splice_viable (candidates, strict_p, &any_viable_p);
5080 if (!any_viable_p)
5082 switch (code)
5084 case POSTINCREMENT_EXPR:
5085 case POSTDECREMENT_EXPR:
5086 /* Don't try anything fancy if we're not allowed to produce
5087 errors. */
5088 if (!(complain & tf_error))
5089 return error_mark_node;
5091 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
5092 distinguish between prefix and postfix ++ and
5093 operator++() was used for both, so we allow this with
5094 -fpermissive. */
5095 if (flags & LOOKUP_COMPLAIN)
5097 const char *msg = (flag_permissive)
5098 ? G_("no %<%D(int)%> declared for postfix %qs,"
5099 " trying prefix operator instead")
5100 : G_("no %<%D(int)%> declared for postfix %qs");
5101 permerror (input_location, msg, fnname,
5102 operator_name_info[code].name);
5105 if (!flag_permissive)
5106 return error_mark_node;
5108 if (code == POSTINCREMENT_EXPR)
5109 code = PREINCREMENT_EXPR;
5110 else
5111 code = PREDECREMENT_EXPR;
5112 result = build_new_op_1 (code, flags, arg1, NULL_TREE, NULL_TREE,
5113 overload, complain);
5114 break;
5116 /* The caller will deal with these. */
5117 case ADDR_EXPR:
5118 case COMPOUND_EXPR:
5119 case COMPONENT_REF:
5120 result = NULL_TREE;
5121 result_valid_p = true;
5122 break;
5124 default:
5125 if ((flags & LOOKUP_COMPLAIN) && (complain & tf_error))
5127 /* If one of the arguments of the operator represents
5128 an invalid use of member function pointer, try to report
5129 a meaningful error ... */
5130 if (invalid_nonstatic_memfn_p (arg1, tf_error)
5131 || invalid_nonstatic_memfn_p (arg2, tf_error)
5132 || invalid_nonstatic_memfn_p (arg3, tf_error))
5133 /* We displayed the error message. */;
5134 else
5136 /* ... Otherwise, report the more generic
5137 "no matching operator found" error */
5138 op_error (code, code2, arg1, arg2, arg3, FALSE);
5139 print_z_candidates (input_location, candidates);
5142 result = error_mark_node;
5143 break;
5146 else
5148 cand = tourney (candidates, complain);
5149 if (cand == 0)
5151 if ((flags & LOOKUP_COMPLAIN) && (complain & tf_error))
5153 op_error (code, code2, arg1, arg2, arg3, TRUE);
5154 print_z_candidates (input_location, candidates);
5156 result = error_mark_node;
5158 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5160 if (overload)
5161 *overload = cand->fn;
5163 if (resolve_args (arglist, complain) == NULL)
5164 result = error_mark_node;
5165 else
5166 result = build_over_call (cand, LOOKUP_NORMAL, complain);
5168 else
5170 /* Give any warnings we noticed during overload resolution. */
5171 if (cand->warnings && (complain & tf_warning))
5173 struct candidate_warning *w;
5174 for (w = cand->warnings; w; w = w->next)
5175 joust (cand, w->loser, 1, complain);
5178 /* Check for comparison of different enum types. */
5179 switch (code)
5181 case GT_EXPR:
5182 case LT_EXPR:
5183 case GE_EXPR:
5184 case LE_EXPR:
5185 case EQ_EXPR:
5186 case NE_EXPR:
5187 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
5188 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
5189 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5190 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
5191 && (complain & tf_warning))
5193 warning (OPT_Wenum_compare,
5194 "comparison between %q#T and %q#T",
5195 TREE_TYPE (arg1), TREE_TYPE (arg2));
5197 break;
5198 default:
5199 break;
5202 /* We need to strip any leading REF_BIND so that bitfields
5203 don't cause errors. This should not remove any important
5204 conversions, because builtins don't apply to class
5205 objects directly. */
5206 conv = cand->convs[0];
5207 if (conv->kind == ck_ref_bind)
5208 conv = next_conversion (conv);
5209 arg1 = convert_like (conv, arg1, complain);
5211 if (arg2)
5213 /* We need to call warn_logical_operator before
5214 converting arg2 to a boolean_type. */
5215 if (complain & tf_warning)
5216 warn_logical_operator (input_location, code, boolean_type_node,
5217 code_orig_arg1, arg1,
5218 code_orig_arg2, arg2);
5220 conv = cand->convs[1];
5221 if (conv->kind == ck_ref_bind)
5222 conv = next_conversion (conv);
5223 arg2 = convert_like (conv, arg2, complain);
5225 if (arg3)
5227 conv = cand->convs[2];
5228 if (conv->kind == ck_ref_bind)
5229 conv = next_conversion (conv);
5230 arg3 = convert_like (conv, arg3, complain);
5236 user_defined_result_ready:
5238 /* Free all the conversions we allocated. */
5239 obstack_free (&conversion_obstack, p);
5241 if (result || result_valid_p)
5242 return result;
5244 builtin:
5245 switch (code)
5247 case MODIFY_EXPR:
5248 return cp_build_modify_expr (arg1, code2, arg2, complain);
5250 case INDIRECT_REF:
5251 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
5253 case TRUTH_ANDIF_EXPR:
5254 case TRUTH_ORIF_EXPR:
5255 case TRUTH_AND_EXPR:
5256 case TRUTH_OR_EXPR:
5257 warn_logical_operator (input_location, code, boolean_type_node,
5258 code_orig_arg1, arg1, code_orig_arg2, arg2);
5259 /* Fall through. */
5260 case PLUS_EXPR:
5261 case MINUS_EXPR:
5262 case MULT_EXPR:
5263 case TRUNC_DIV_EXPR:
5264 case GT_EXPR:
5265 case LT_EXPR:
5266 case GE_EXPR:
5267 case LE_EXPR:
5268 case EQ_EXPR:
5269 case NE_EXPR:
5270 case MAX_EXPR:
5271 case MIN_EXPR:
5272 case LSHIFT_EXPR:
5273 case RSHIFT_EXPR:
5274 case TRUNC_MOD_EXPR:
5275 case BIT_AND_EXPR:
5276 case BIT_IOR_EXPR:
5277 case BIT_XOR_EXPR:
5278 return cp_build_binary_op (input_location, code, arg1, arg2, complain);
5280 case UNARY_PLUS_EXPR:
5281 case NEGATE_EXPR:
5282 case BIT_NOT_EXPR:
5283 case TRUTH_NOT_EXPR:
5284 case PREINCREMENT_EXPR:
5285 case POSTINCREMENT_EXPR:
5286 case PREDECREMENT_EXPR:
5287 case POSTDECREMENT_EXPR:
5288 case REALPART_EXPR:
5289 case IMAGPART_EXPR:
5290 case ABS_EXPR:
5291 return cp_build_unary_op (code, arg1, candidates != 0, complain);
5293 case ARRAY_REF:
5294 return cp_build_array_ref (input_location, arg1, arg2, complain);
5296 case MEMBER_REF:
5297 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_NULL,
5298 complain),
5299 arg2, complain);
5301 /* The caller will deal with these. */
5302 case ADDR_EXPR:
5303 case COMPONENT_REF:
5304 case COMPOUND_EXPR:
5305 return NULL_TREE;
5307 default:
5308 gcc_unreachable ();
5310 return NULL_TREE;
5313 /* Wrapper for above. */
5315 tree
5316 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
5317 tree *overload, tsubst_flags_t complain)
5319 tree ret;
5320 bool subtime = timevar_cond_start (TV_OVERLOAD);
5321 ret = build_new_op_1 (code, flags, arg1, arg2, arg3, overload, complain);
5322 timevar_cond_stop (TV_OVERLOAD, subtime);
5323 return ret;
5326 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
5327 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
5329 static bool
5330 non_placement_deallocation_fn_p (tree t)
5332 /* A template instance is never a usual deallocation function,
5333 regardless of its signature. */
5334 if (TREE_CODE (t) == TEMPLATE_DECL
5335 || primary_template_instantiation_p (t))
5336 return false;
5338 /* If a class T has a member deallocation function named operator delete
5339 with exactly one parameter, then that function is a usual
5340 (non-placement) deallocation function. If class T does not declare
5341 such an operator delete but does declare a member deallocation
5342 function named operator delete with exactly two parameters, the second
5343 of which has type std::size_t (18.2), then this function is a usual
5344 deallocation function. */
5345 t = FUNCTION_ARG_CHAIN (t);
5346 if (t == void_list_node
5347 || (t && same_type_p (TREE_VALUE (t), size_type_node)
5348 && TREE_CHAIN (t) == void_list_node))
5349 return true;
5350 return false;
5353 /* Build a call to operator delete. This has to be handled very specially,
5354 because the restrictions on what signatures match are different from all
5355 other call instances. For a normal delete, only a delete taking (void *)
5356 or (void *, size_t) is accepted. For a placement delete, only an exact
5357 match with the placement new is accepted.
5359 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
5360 ADDR is the pointer to be deleted.
5361 SIZE is the size of the memory block to be deleted.
5362 GLOBAL_P is true if the delete-expression should not consider
5363 class-specific delete operators.
5364 PLACEMENT is the corresponding placement new call, or NULL_TREE.
5366 If this call to "operator delete" is being generated as part to
5367 deallocate memory allocated via a new-expression (as per [expr.new]
5368 which requires that if the initialization throws an exception then
5369 we call a deallocation function), then ALLOC_FN is the allocation
5370 function. */
5372 tree
5373 build_op_delete_call (enum tree_code code, tree addr, tree size,
5374 bool global_p, tree placement,
5375 tree alloc_fn)
5377 tree fn = NULL_TREE;
5378 tree fns, fnname, type, t;
5380 if (addr == error_mark_node)
5381 return error_mark_node;
5383 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
5385 fnname = ansi_opname (code);
5387 if (CLASS_TYPE_P (type)
5388 && COMPLETE_TYPE_P (complete_type (type))
5389 && !global_p)
5390 /* In [class.free]
5392 If the result of the lookup is ambiguous or inaccessible, or if
5393 the lookup selects a placement deallocation function, the
5394 program is ill-formed.
5396 Therefore, we ask lookup_fnfields to complain about ambiguity. */
5398 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
5399 if (fns == error_mark_node)
5400 return error_mark_node;
5402 else
5403 fns = NULL_TREE;
5405 if (fns == NULL_TREE)
5406 fns = lookup_name_nonclass (fnname);
5408 /* Strip const and volatile from addr. */
5409 addr = cp_convert (ptr_type_node, addr);
5411 if (placement)
5413 /* "A declaration of a placement deallocation function matches the
5414 declaration of a placement allocation function if it has the same
5415 number of parameters and, after parameter transformations (8.3.5),
5416 all parameter types except the first are identical."
5418 So we build up the function type we want and ask instantiate_type
5419 to get it for us. */
5420 t = FUNCTION_ARG_CHAIN (alloc_fn);
5421 t = tree_cons (NULL_TREE, ptr_type_node, t);
5422 t = build_function_type (void_type_node, t);
5424 fn = instantiate_type (t, fns, tf_none);
5425 if (fn == error_mark_node)
5426 return NULL_TREE;
5428 if (BASELINK_P (fn))
5429 fn = BASELINK_FUNCTIONS (fn);
5431 /* "If the lookup finds the two-parameter form of a usual deallocation
5432 function (3.7.4.2) and that function, considered as a placement
5433 deallocation function, would have been selected as a match for the
5434 allocation function, the program is ill-formed." */
5435 if (non_placement_deallocation_fn_p (fn))
5437 /* But if the class has an operator delete (void *), then that is
5438 the usual deallocation function, so we shouldn't complain
5439 about using the operator delete (void *, size_t). */
5440 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
5441 t; t = OVL_NEXT (t))
5443 tree elt = OVL_CURRENT (t);
5444 if (non_placement_deallocation_fn_p (elt)
5445 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
5446 goto ok;
5448 permerror (0, "non-placement deallocation function %q+D", fn);
5449 permerror (input_location, "selected for placement delete");
5450 ok:;
5453 else
5454 /* "Any non-placement deallocation function matches a non-placement
5455 allocation function. If the lookup finds a single matching
5456 deallocation function, that function will be called; otherwise, no
5457 deallocation function will be called." */
5458 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
5459 t; t = OVL_NEXT (t))
5461 tree elt = OVL_CURRENT (t);
5462 if (non_placement_deallocation_fn_p (elt))
5464 fn = elt;
5465 /* "If a class T has a member deallocation function named
5466 operator delete with exactly one parameter, then that
5467 function is a usual (non-placement) deallocation
5468 function. If class T does not declare such an operator
5469 delete but does declare a member deallocation function named
5470 operator delete with exactly two parameters, the second of
5471 which has type std::size_t (18.2), then this function is a
5472 usual deallocation function."
5474 So (void*) beats (void*, size_t). */
5475 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
5476 break;
5480 /* If we have a matching function, call it. */
5481 if (fn)
5483 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
5485 /* If the FN is a member function, make sure that it is
5486 accessible. */
5487 if (BASELINK_P (fns))
5488 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn);
5490 /* Core issue 901: It's ok to new a type with deleted delete. */
5491 if (DECL_DELETED_FN (fn) && alloc_fn)
5492 return NULL_TREE;
5494 if (placement)
5496 /* The placement args might not be suitable for overload
5497 resolution at this point, so build the call directly. */
5498 int nargs = call_expr_nargs (placement);
5499 tree *argarray = XALLOCAVEC (tree, nargs);
5500 int i;
5501 argarray[0] = addr;
5502 for (i = 1; i < nargs; i++)
5503 argarray[i] = CALL_EXPR_ARG (placement, i);
5504 mark_used (fn);
5505 return build_cxx_call (fn, nargs, argarray);
5507 else
5509 tree ret;
5510 VEC(tree,gc) *args = VEC_alloc (tree, gc, 2);
5511 VEC_quick_push (tree, args, addr);
5512 if (FUNCTION_ARG_CHAIN (fn) != void_list_node)
5513 VEC_quick_push (tree, args, size);
5514 ret = cp_build_function_call_vec (fn, &args, tf_warning_or_error);
5515 VEC_free (tree, gc, args);
5516 return ret;
5520 /* [expr.new]
5522 If no unambiguous matching deallocation function can be found,
5523 propagating the exception does not cause the object's memory to
5524 be freed. */
5525 if (alloc_fn)
5527 if (!placement)
5528 warning (0, "no corresponding deallocation function for %qD",
5529 alloc_fn);
5530 return NULL_TREE;
5533 error ("no suitable %<operator %s%> for %qT",
5534 operator_name_info[(int)code].name, type);
5535 return error_mark_node;
5538 /* If the current scope isn't allowed to access DECL along
5539 BASETYPE_PATH, give an error. The most derived class in
5540 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
5541 the declaration to use in the error diagnostic. */
5543 bool
5544 enforce_access (tree basetype_path, tree decl, tree diag_decl)
5546 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
5548 if (!accessible_p (basetype_path, decl, true))
5550 if (TREE_PRIVATE (decl))
5551 error ("%q+#D is private", diag_decl);
5552 else if (TREE_PROTECTED (decl))
5553 error ("%q+#D is protected", diag_decl);
5554 else
5555 error ("%q+#D is inaccessible", diag_decl);
5556 error ("within this context");
5557 return false;
5560 return true;
5563 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
5564 bitwise or of LOOKUP_* values. If any errors are warnings are
5565 generated, set *DIAGNOSTIC_FN to "error" or "warning",
5566 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
5567 to NULL. */
5569 static tree
5570 build_temp (tree expr, tree type, int flags,
5571 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
5573 int savew, savee;
5574 VEC(tree,gc) *args;
5576 savew = warningcount, savee = errorcount;
5577 args = make_tree_vector_single (expr);
5578 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
5579 &args, type, flags, complain);
5580 release_tree_vector (args);
5581 if (warningcount > savew)
5582 *diagnostic_kind = DK_WARNING;
5583 else if (errorcount > savee)
5584 *diagnostic_kind = DK_ERROR;
5585 else
5586 *diagnostic_kind = DK_UNSPECIFIED;
5587 return expr;
5590 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
5591 EXPR is implicitly converted to type TOTYPE.
5592 FN and ARGNUM are used for diagnostics. */
5594 static void
5595 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
5597 /* Issue warnings about peculiar, but valid, uses of NULL. */
5598 if (expr == null_node && TREE_CODE (totype) != BOOLEAN_TYPE
5599 && ARITHMETIC_TYPE_P (totype))
5601 source_location loc =
5602 expansion_point_location_if_in_system_header (input_location);
5604 if (fn)
5605 warning_at (loc, OPT_Wconversion_null,
5606 "passing NULL to non-pointer argument %P of %qD",
5607 argnum, fn);
5608 else
5609 warning_at (loc, OPT_Wconversion_null,
5610 "converting to non-pointer type %qT from NULL", totype);
5613 /* Issue warnings if "false" is converted to a NULL pointer */
5614 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
5615 && TYPE_PTR_P (totype))
5617 if (fn)
5618 warning_at (input_location, OPT_Wconversion_null,
5619 "converting %<false%> to pointer type for argument %P "
5620 "of %qD", argnum, fn);
5621 else
5622 warning_at (input_location, OPT_Wconversion_null,
5623 "converting %<false%> to pointer type %qT", totype);
5627 /* Perform the conversions in CONVS on the expression EXPR. FN and
5628 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
5629 indicates the `this' argument of a method. INNER is nonzero when
5630 being called to continue a conversion chain. It is negative when a
5631 reference binding will be applied, positive otherwise. If
5632 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
5633 conversions will be emitted if appropriate. If C_CAST_P is true,
5634 this conversion is coming from a C-style cast; in that case,
5635 conversions to inaccessible bases are permitted. */
5637 static tree
5638 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
5639 int inner, bool issue_conversion_warnings,
5640 bool c_cast_p, tsubst_flags_t complain)
5642 tree totype = convs->type;
5643 diagnostic_t diag_kind;
5644 int flags;
5646 if (convs->bad_p && !(complain & tf_error))
5647 return error_mark_node;
5649 if (convs->bad_p
5650 && convs->kind != ck_user
5651 && convs->kind != ck_list
5652 && convs->kind != ck_ambig
5653 && (convs->kind != ck_ref_bind
5654 || convs->user_conv_p)
5655 && convs->kind != ck_rvalue
5656 && convs->kind != ck_base)
5658 conversion *t = convs;
5660 /* Give a helpful error if this is bad because of excess braces. */
5661 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5662 && SCALAR_TYPE_P (totype)
5663 && CONSTRUCTOR_NELTS (expr) > 0
5664 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
5665 permerror (input_location, "too many braces around initializer for %qT", totype);
5667 for (; t ; t = next_conversion (t))
5669 if (t->kind == ck_user && t->cand->reason)
5671 permerror (input_location, "invalid user-defined conversion "
5672 "from %qT to %qT", TREE_TYPE (expr), totype);
5673 print_z_candidate ("candidate is:", t->cand);
5674 expr = convert_like_real (t, expr, fn, argnum, 1,
5675 /*issue_conversion_warnings=*/false,
5676 /*c_cast_p=*/false,
5677 complain);
5678 if (convs->kind == ck_ref_bind)
5679 return convert_to_reference (totype, expr, CONV_IMPLICIT,
5680 LOOKUP_NORMAL, NULL_TREE);
5681 else
5682 return cp_convert (totype, expr);
5684 else if (t->kind == ck_user || !t->bad_p)
5686 expr = convert_like_real (t, expr, fn, argnum, 1,
5687 /*issue_conversion_warnings=*/false,
5688 /*c_cast_p=*/false,
5689 complain);
5690 break;
5692 else if (t->kind == ck_ambig)
5693 return convert_like_real (t, expr, fn, argnum, 1,
5694 /*issue_conversion_warnings=*/false,
5695 /*c_cast_p=*/false,
5696 complain);
5697 else if (t->kind == ck_identity)
5698 break;
5701 permerror (input_location, "invalid conversion from %qT to %qT",
5702 TREE_TYPE (expr), totype);
5703 if (fn)
5704 permerror (DECL_SOURCE_LOCATION (fn),
5705 " initializing argument %P of %qD", argnum, fn);
5707 return cp_convert (totype, expr);
5710 if (issue_conversion_warnings && (complain & tf_warning))
5711 conversion_null_warnings (totype, expr, fn, argnum);
5713 switch (convs->kind)
5715 case ck_user:
5717 struct z_candidate *cand = convs->cand;
5718 tree convfn = cand->fn;
5719 unsigned i;
5721 /* If we're initializing from {}, it's value-initialization. */
5722 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5723 && CONSTRUCTOR_NELTS (expr) == 0
5724 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
5726 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
5727 expr = build_value_init (totype, complain);
5728 expr = get_target_expr_sfinae (expr, complain);
5729 if (expr != error_mark_node)
5731 TARGET_EXPR_LIST_INIT_P (expr) = true;
5732 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
5734 return expr;
5737 expr = mark_rvalue_use (expr);
5739 /* When converting from an init list we consider explicit
5740 constructors, but actually trying to call one is an error. */
5741 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
5742 /* Unless this is for direct-list-initialization. */
5743 && !(BRACE_ENCLOSED_INITIALIZER_P (expr)
5744 && CONSTRUCTOR_IS_DIRECT_INIT (expr))
5745 /* Unless we're calling it for value-initialization from an
5746 empty list, since that is handled separately in 8.5.4. */
5747 && cand->num_convs > 0)
5749 error ("converting to %qT from initializer list would use "
5750 "explicit constructor %qD", totype, convfn);
5753 /* Set user_conv_p on the argument conversions, so rvalue/base
5754 handling knows not to allow any more UDCs. */
5755 for (i = 0; i < cand->num_convs; ++i)
5756 cand->convs[i]->user_conv_p = true;
5758 expr = build_over_call (cand, LOOKUP_NORMAL, complain);
5760 /* If this is a constructor or a function returning an aggr type,
5761 we need to build up a TARGET_EXPR. */
5762 if (DECL_CONSTRUCTOR_P (convfn))
5764 expr = build_cplus_new (totype, expr, complain);
5766 /* Remember that this was list-initialization. */
5767 if (convs->check_narrowing && expr != error_mark_node)
5768 TARGET_EXPR_LIST_INIT_P (expr) = true;
5771 return expr;
5773 case ck_identity:
5774 expr = mark_rvalue_use (expr);
5775 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
5777 int nelts = CONSTRUCTOR_NELTS (expr);
5778 if (nelts == 0)
5779 expr = build_value_init (totype, complain);
5780 else if (nelts == 1)
5781 expr = CONSTRUCTOR_ELT (expr, 0)->value;
5782 else
5783 gcc_unreachable ();
5786 if (type_unknown_p (expr))
5787 expr = instantiate_type (totype, expr, complain);
5788 /* Convert a constant to its underlying value, unless we are
5789 about to bind it to a reference, in which case we need to
5790 leave it as an lvalue. */
5791 if (inner >= 0)
5793 expr = decl_constant_value_safe (expr);
5794 if (expr == null_node && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
5795 /* If __null has been converted to an integer type, we do not
5796 want to warn about uses of EXPR as an integer, rather than
5797 as a pointer. */
5798 expr = build_int_cst (totype, 0);
5800 return expr;
5801 case ck_ambig:
5802 /* We leave bad_p off ck_ambig because overload resolution considers
5803 it valid, it just fails when we try to perform it. So we need to
5804 check complain here, too. */
5805 if (complain & tf_error)
5807 /* Call build_user_type_conversion again for the error. */
5808 build_user_type_conversion (totype, convs->u.expr, LOOKUP_NORMAL,
5809 complain);
5810 if (fn)
5811 error (" initializing argument %P of %q+D", argnum, fn);
5813 return error_mark_node;
5815 case ck_list:
5817 /* Conversion to std::initializer_list<T>. */
5818 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
5819 tree new_ctor = build_constructor (init_list_type_node, NULL);
5820 unsigned len = CONSTRUCTOR_NELTS (expr);
5821 tree array, val, field;
5822 VEC(constructor_elt,gc) *vec = NULL;
5823 unsigned ix;
5825 /* Convert all the elements. */
5826 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
5828 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
5829 1, false, false, complain);
5830 if (sub == error_mark_node)
5831 return sub;
5832 if (!BRACE_ENCLOSED_INITIALIZER_P (val))
5833 check_narrowing (TREE_TYPE (sub), val);
5834 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
5835 if (!TREE_CONSTANT (sub))
5836 TREE_CONSTANT (new_ctor) = false;
5838 /* Build up the array. */
5839 elttype = cp_build_qualified_type
5840 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
5841 array = build_array_of_n_type (elttype, len);
5842 array = finish_compound_literal (array, new_ctor, complain);
5844 /* Build up the initializer_list object. */
5845 totype = complete_type (totype);
5846 field = next_initializable_field (TYPE_FIELDS (totype));
5847 CONSTRUCTOR_APPEND_ELT (vec, field, decay_conversion (array, complain));
5848 field = next_initializable_field (DECL_CHAIN (field));
5849 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
5850 new_ctor = build_constructor (totype, vec);
5851 return get_target_expr (new_ctor);
5854 case ck_aggr:
5855 if (TREE_CODE (totype) == COMPLEX_TYPE)
5857 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
5858 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
5859 real = perform_implicit_conversion (TREE_TYPE (totype),
5860 real, complain);
5861 imag = perform_implicit_conversion (TREE_TYPE (totype),
5862 imag, complain);
5863 expr = build2 (COMPLEX_EXPR, totype, real, imag);
5864 return fold_if_not_in_template (expr);
5866 expr = reshape_init (totype, expr, complain);
5867 return get_target_expr (digest_init (totype, expr, complain));
5869 default:
5870 break;
5873 expr = convert_like_real (next_conversion (convs), expr, fn, argnum,
5874 convs->kind == ck_ref_bind ? -1 : 1,
5875 convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
5876 c_cast_p,
5877 complain);
5878 if (expr == error_mark_node)
5879 return error_mark_node;
5881 switch (convs->kind)
5883 case ck_rvalue:
5884 expr = decay_conversion (expr, complain);
5885 if (expr == error_mark_node)
5886 return error_mark_node;
5888 if (! MAYBE_CLASS_TYPE_P (totype))
5889 return expr;
5890 /* Else fall through. */
5891 case ck_base:
5892 if (convs->kind == ck_base && !convs->need_temporary_p)
5894 /* We are going to bind a reference directly to a base-class
5895 subobject of EXPR. */
5896 /* Build an expression for `*((base*) &expr)'. */
5897 expr = cp_build_addr_expr (expr, complain);
5898 expr = convert_to_base (expr, build_pointer_type (totype),
5899 !c_cast_p, /*nonnull=*/true, complain);
5900 expr = cp_build_indirect_ref (expr, RO_IMPLICIT_CONVERSION, complain);
5901 return expr;
5904 /* Copy-initialization where the cv-unqualified version of the source
5905 type is the same class as, or a derived class of, the class of the
5906 destination [is treated as direct-initialization]. [dcl.init] */
5907 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
5908 if (convs->user_conv_p)
5909 /* This conversion is being done in the context of a user-defined
5910 conversion (i.e. the second step of copy-initialization), so
5911 don't allow any more. */
5912 flags |= LOOKUP_NO_CONVERSION;
5913 if (convs->rvaluedness_matches_p)
5914 flags |= LOOKUP_PREFER_RVALUE;
5915 if (TREE_CODE (expr) == TARGET_EXPR
5916 && TARGET_EXPR_LIST_INIT_P (expr))
5917 /* Copy-list-initialization doesn't actually involve a copy. */
5918 return expr;
5919 expr = build_temp (expr, totype, flags, &diag_kind, complain);
5920 if (diag_kind && fn && complain)
5921 emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (fn), 0,
5922 " initializing argument %P of %qD", argnum, fn);
5923 return build_cplus_new (totype, expr, complain);
5925 case ck_ref_bind:
5927 tree ref_type = totype;
5929 if (convs->bad_p && !next_conversion (convs)->bad_p)
5931 gcc_assert (TYPE_REF_IS_RVALUE (ref_type)
5932 && real_lvalue_p (expr));
5934 error ("cannot bind %qT lvalue to %qT",
5935 TREE_TYPE (expr), totype);
5936 if (fn)
5937 error (" initializing argument %P of %q+D", argnum, fn);
5938 return error_mark_node;
5941 /* If necessary, create a temporary.
5943 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
5944 that need temporaries, even when their types are reference
5945 compatible with the type of reference being bound, so the
5946 upcoming call to cp_build_addr_expr doesn't fail. */
5947 if (convs->need_temporary_p
5948 || TREE_CODE (expr) == CONSTRUCTOR
5949 || TREE_CODE (expr) == VA_ARG_EXPR)
5951 /* Otherwise, a temporary of type "cv1 T1" is created and
5952 initialized from the initializer expression using the rules
5953 for a non-reference copy-initialization (8.5). */
5955 tree type = TREE_TYPE (ref_type);
5956 cp_lvalue_kind lvalue = real_lvalue_p (expr);
5958 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5959 (type, next_conversion (convs)->type));
5960 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
5961 && !TYPE_REF_IS_RVALUE (ref_type))
5963 /* If the reference is volatile or non-const, we
5964 cannot create a temporary. */
5965 if (lvalue & clk_bitfield)
5966 error ("cannot bind bitfield %qE to %qT",
5967 expr, ref_type);
5968 else if (lvalue & clk_packed)
5969 error ("cannot bind packed field %qE to %qT",
5970 expr, ref_type);
5971 else
5972 error ("cannot bind rvalue %qE to %qT", expr, ref_type);
5973 return error_mark_node;
5975 /* If the source is a packed field, and we must use a copy
5976 constructor, then building the target expr will require
5977 binding the field to the reference parameter to the
5978 copy constructor, and we'll end up with an infinite
5979 loop. If we can use a bitwise copy, then we'll be
5980 OK. */
5981 if ((lvalue & clk_packed)
5982 && CLASS_TYPE_P (type)
5983 && type_has_nontrivial_copy_init (type))
5985 error ("cannot bind packed field %qE to %qT",
5986 expr, ref_type);
5987 return error_mark_node;
5989 if (lvalue & clk_bitfield)
5991 expr = convert_bitfield_to_declared_type (expr);
5992 expr = fold_convert (type, expr);
5994 expr = build_target_expr_with_type (expr, type, complain);
5997 /* Take the address of the thing to which we will bind the
5998 reference. */
5999 expr = cp_build_addr_expr (expr, complain);
6000 if (expr == error_mark_node)
6001 return error_mark_node;
6003 /* Convert it to a pointer to the type referred to by the
6004 reference. This will adjust the pointer if a derived to
6005 base conversion is being performed. */
6006 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
6007 expr);
6008 /* Convert the pointer to the desired reference type. */
6009 return build_nop (ref_type, expr);
6012 case ck_lvalue:
6013 return decay_conversion (expr, complain);
6015 case ck_qual:
6016 /* Warn about deprecated conversion if appropriate. */
6017 string_conv_p (totype, expr, 1);
6018 break;
6020 case ck_ptr:
6021 if (convs->base_p)
6022 expr = convert_to_base (expr, totype, !c_cast_p,
6023 /*nonnull=*/false, complain);
6024 return build_nop (totype, expr);
6026 case ck_pmem:
6027 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
6028 c_cast_p, complain);
6030 default:
6031 break;
6034 if (convs->check_narrowing)
6035 check_narrowing (totype, expr);
6037 if (issue_conversion_warnings && (complain & tf_warning))
6038 expr = convert_and_check (totype, expr);
6039 else
6040 expr = convert (totype, expr);
6042 return expr;
6045 /* ARG is being passed to a varargs function. Perform any conversions
6046 required. Return the converted value. */
6048 tree
6049 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
6051 tree arg_type;
6053 /* [expr.call]
6055 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6056 standard conversions are performed. */
6057 arg = decay_conversion (arg, complain);
6058 arg_type = TREE_TYPE (arg);
6059 /* [expr.call]
6061 If the argument has integral or enumeration type that is subject
6062 to the integral promotions (_conv.prom_), or a floating point
6063 type that is subject to the floating point promotion
6064 (_conv.fpprom_), the value of the argument is converted to the
6065 promoted type before the call. */
6066 if (TREE_CODE (arg_type) == REAL_TYPE
6067 && (TYPE_PRECISION (arg_type)
6068 < TYPE_PRECISION (double_type_node))
6069 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
6071 if ((complain & tf_warning)
6072 && warn_double_promotion && !c_inhibit_evaluation_warnings)
6073 warning (OPT_Wdouble_promotion,
6074 "implicit conversion from %qT to %qT when passing "
6075 "argument to function",
6076 arg_type, double_type_node);
6077 arg = convert_to_real (double_type_node, arg);
6079 else if (NULLPTR_TYPE_P (arg_type))
6080 arg = null_pointer_node;
6081 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
6083 if (SCOPED_ENUM_P (arg_type) && !abi_version_at_least (6))
6085 if (complain & tf_warning)
6086 warning (OPT_Wabi, "scoped enum %qT will not promote to an "
6087 "integral type in a future version of GCC", arg_type);
6088 arg = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg);
6090 arg = perform_integral_promotions (arg);
6093 arg = require_complete_type (arg);
6094 arg_type = TREE_TYPE (arg);
6096 if (arg != error_mark_node
6097 /* In a template (or ill-formed code), we can have an incomplete type
6098 even after require_complete_type, in which case we don't know
6099 whether it has trivial copy or not. */
6100 && COMPLETE_TYPE_P (arg_type))
6102 /* Build up a real lvalue-to-rvalue conversion in case the
6103 copy constructor is trivial but not callable. */
6104 if (!cp_unevaluated_operand && CLASS_TYPE_P (arg_type))
6105 force_rvalue (arg, complain);
6107 /* [expr.call] 5.2.2/7:
6108 Passing a potentially-evaluated argument of class type (Clause 9)
6109 with a non-trivial copy constructor or a non-trivial destructor
6110 with no corresponding parameter is conditionally-supported, with
6111 implementation-defined semantics.
6113 We used to just warn here and do a bitwise copy, but now
6114 cp_expr_size will abort if we try to do that.
6116 If the call appears in the context of a sizeof expression,
6117 it is not potentially-evaluated. */
6118 if (cp_unevaluated_operand == 0
6119 && (type_has_nontrivial_copy_init (arg_type)
6120 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type)))
6122 if (complain & tf_error)
6123 error ("cannot pass objects of non-trivially-copyable "
6124 "type %q#T through %<...%>", arg_type);
6125 else
6126 return error_mark_node;
6130 return arg;
6133 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
6135 tree
6136 build_x_va_arg (source_location loc, tree expr, tree type)
6138 if (processing_template_decl)
6139 return build_min (VA_ARG_EXPR, type, expr);
6141 type = complete_type_or_else (type, NULL_TREE);
6143 if (expr == error_mark_node || !type)
6144 return error_mark_node;
6146 expr = mark_lvalue_use (expr);
6148 if (type_has_nontrivial_copy_init (type)
6149 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
6150 || TREE_CODE (type) == REFERENCE_TYPE)
6152 /* Remove reference types so we don't ICE later on. */
6153 tree type1 = non_reference (type);
6154 /* conditionally-supported behavior [expr.call] 5.2.2/7. */
6155 error ("cannot receive objects of non-trivially-copyable type %q#T "
6156 "through %<...%>; ", type);
6157 expr = convert (build_pointer_type (type1), null_node);
6158 expr = cp_build_indirect_ref (expr, RO_NULL, tf_warning_or_error);
6159 return expr;
6162 return build_va_arg (loc, expr, type);
6165 /* TYPE has been given to va_arg. Apply the default conversions which
6166 would have happened when passed via ellipsis. Return the promoted
6167 type, or the passed type if there is no change. */
6169 tree
6170 cxx_type_promotes_to (tree type)
6172 tree promote;
6174 /* Perform the array-to-pointer and function-to-pointer
6175 conversions. */
6176 type = type_decays_to (type);
6178 promote = type_promotes_to (type);
6179 if (same_type_p (type, promote))
6180 promote = type;
6182 return promote;
6185 /* ARG is a default argument expression being passed to a parameter of
6186 the indicated TYPE, which is a parameter to FN. PARMNUM is the
6187 zero-based argument number. Do any required conversions. Return
6188 the converted value. */
6190 static GTY(()) VEC(tree,gc) *default_arg_context;
6191 void
6192 push_defarg_context (tree fn)
6193 { VEC_safe_push (tree, gc, default_arg_context, fn); }
6194 void
6195 pop_defarg_context (void)
6196 { VEC_pop (tree, default_arg_context); }
6198 tree
6199 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
6200 tsubst_flags_t complain)
6202 int i;
6203 tree t;
6205 /* See through clones. */
6206 fn = DECL_ORIGIN (fn);
6208 /* Detect recursion. */
6209 FOR_EACH_VEC_ELT (tree, default_arg_context, i, t)
6210 if (t == fn)
6212 if (complain & tf_error)
6213 error ("recursive evaluation of default argument for %q#D", fn);
6214 return error_mark_node;
6217 /* If the ARG is an unparsed default argument expression, the
6218 conversion cannot be performed. */
6219 if (TREE_CODE (arg) == DEFAULT_ARG)
6221 if (complain & tf_error)
6222 error ("call to %qD uses the default argument for parameter %P, which "
6223 "is not yet defined", fn, parmnum);
6224 return error_mark_node;
6227 push_defarg_context (fn);
6229 if (fn && DECL_TEMPLATE_INFO (fn))
6230 arg = tsubst_default_argument (fn, type, arg);
6232 /* Due to:
6234 [dcl.fct.default]
6236 The names in the expression are bound, and the semantic
6237 constraints are checked, at the point where the default
6238 expressions appears.
6240 we must not perform access checks here. */
6241 push_deferring_access_checks (dk_no_check);
6242 /* We must make a copy of ARG, in case subsequent processing
6243 alters any part of it. */
6244 arg = break_out_target_exprs (arg);
6245 if (TREE_CODE (arg) == CONSTRUCTOR)
6247 arg = digest_init (type, arg, complain);
6248 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
6249 ICR_DEFAULT_ARGUMENT, fn, parmnum,
6250 complain);
6252 else
6254 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
6255 ICR_DEFAULT_ARGUMENT, fn, parmnum,
6256 complain);
6257 arg = convert_for_arg_passing (type, arg, complain);
6259 pop_deferring_access_checks();
6261 pop_defarg_context ();
6263 return arg;
6266 /* Returns the type which will really be used for passing an argument of
6267 type TYPE. */
6269 tree
6270 type_passed_as (tree type)
6272 /* Pass classes with copy ctors by invisible reference. */
6273 if (TREE_ADDRESSABLE (type))
6275 type = build_reference_type (type);
6276 /* There are no other pointers to this temporary. */
6277 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
6279 else if (targetm.calls.promote_prototypes (type)
6280 && INTEGRAL_TYPE_P (type)
6281 && COMPLETE_TYPE_P (type)
6282 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
6283 TYPE_SIZE (integer_type_node)))
6284 type = integer_type_node;
6286 return type;
6289 /* Actually perform the appropriate conversion. */
6291 tree
6292 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
6294 tree bitfield_type;
6296 /* If VAL is a bitfield, then -- since it has already been converted
6297 to TYPE -- it cannot have a precision greater than TYPE.
6299 If it has a smaller precision, we must widen it here. For
6300 example, passing "int f:3;" to a function expecting an "int" will
6301 not result in any conversion before this point.
6303 If the precision is the same we must not risk widening. For
6304 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
6305 often have type "int", even though the C++ type for the field is
6306 "long long". If the value is being passed to a function
6307 expecting an "int", then no conversions will be required. But,
6308 if we call convert_bitfield_to_declared_type, the bitfield will
6309 be converted to "long long". */
6310 bitfield_type = is_bitfield_expr_with_lowered_type (val);
6311 if (bitfield_type
6312 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
6313 val = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type), val);
6315 if (val == error_mark_node)
6317 /* Pass classes with copy ctors by invisible reference. */
6318 else if (TREE_ADDRESSABLE (type))
6319 val = build1 (ADDR_EXPR, build_reference_type (type), val);
6320 else if (targetm.calls.promote_prototypes (type)
6321 && INTEGRAL_TYPE_P (type)
6322 && COMPLETE_TYPE_P (type)
6323 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
6324 TYPE_SIZE (integer_type_node)))
6325 val = perform_integral_promotions (val);
6326 if ((complain & tf_warning)
6327 && warn_suggest_attribute_format)
6329 tree rhstype = TREE_TYPE (val);
6330 const enum tree_code coder = TREE_CODE (rhstype);
6331 const enum tree_code codel = TREE_CODE (type);
6332 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6333 && coder == codel
6334 && check_missing_format_attribute (type, rhstype))
6335 warning (OPT_Wsuggest_attribute_format,
6336 "argument of function call might be a candidate for a format attribute");
6338 return val;
6341 /* Returns true iff FN is a function with magic varargs, i.e. ones for
6342 which no conversions at all should be done. This is true for some
6343 builtins which don't act like normal functions. */
6345 static bool
6346 magic_varargs_p (tree fn)
6348 if (DECL_BUILT_IN (fn))
6349 switch (DECL_FUNCTION_CODE (fn))
6351 case BUILT_IN_CLASSIFY_TYPE:
6352 case BUILT_IN_CONSTANT_P:
6353 case BUILT_IN_NEXT_ARG:
6354 case BUILT_IN_VA_START:
6355 return true;
6357 default:;
6358 return lookup_attribute ("type generic",
6359 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
6362 return false;
6365 /* Subroutine of the various build_*_call functions. Overload resolution
6366 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
6367 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
6368 bitmask of various LOOKUP_* flags which apply to the call itself. */
6370 static tree
6371 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
6373 tree fn = cand->fn;
6374 const VEC(tree,gc) *args = cand->args;
6375 tree first_arg = cand->first_arg;
6376 conversion **convs = cand->convs;
6377 conversion *conv;
6378 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
6379 int parmlen;
6380 tree val;
6381 int i = 0;
6382 int j = 0;
6383 unsigned int arg_index = 0;
6384 int is_method = 0;
6385 int nargs;
6386 tree *argarray;
6387 bool already_used = false;
6389 /* In a template, there is no need to perform all of the work that
6390 is normally done. We are only interested in the type of the call
6391 expression, i.e., the return type of the function. Any semantic
6392 errors will be deferred until the template is instantiated. */
6393 if (processing_template_decl)
6395 tree expr, addr;
6396 tree return_type;
6397 const tree *argarray;
6398 unsigned int nargs;
6400 return_type = TREE_TYPE (TREE_TYPE (fn));
6401 nargs = VEC_length (tree, args);
6402 if (first_arg == NULL_TREE)
6403 argarray = VEC_address (tree, CONST_CAST (VEC(tree,gc) *, args));
6404 else
6406 tree *alcarray;
6407 unsigned int ix;
6408 tree arg;
6410 ++nargs;
6411 alcarray = XALLOCAVEC (tree, nargs);
6412 alcarray[0] = first_arg;
6413 FOR_EACH_VEC_ELT (tree, args, ix, arg)
6414 alcarray[ix + 1] = arg;
6415 argarray = alcarray;
6418 addr = build_addr_func (fn, complain);
6419 if (addr == error_mark_node)
6420 return error_mark_node;
6421 expr = build_call_array_loc (input_location, return_type,
6422 addr, nargs, argarray);
6423 if (TREE_THIS_VOLATILE (fn) && cfun)
6424 current_function_returns_abnormally = 1;
6425 return convert_from_reference (expr);
6428 /* Give any warnings we noticed during overload resolution. */
6429 if (cand->warnings && (complain & tf_warning))
6431 struct candidate_warning *w;
6432 for (w = cand->warnings; w; w = w->next)
6433 joust (cand, w->loser, 1, complain);
6436 /* Make =delete work with SFINAE. */
6437 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
6438 return error_mark_node;
6440 if (DECL_FUNCTION_MEMBER_P (fn))
6442 tree access_fn;
6443 /* If FN is a template function, two cases must be considered.
6444 For example:
6446 struct A {
6447 protected:
6448 template <class T> void f();
6450 template <class T> struct B {
6451 protected:
6452 void g();
6454 struct C : A, B<int> {
6455 using A::f; // #1
6456 using B<int>::g; // #2
6459 In case #1 where `A::f' is a member template, DECL_ACCESS is
6460 recorded in the primary template but not in its specialization.
6461 We check access of FN using its primary template.
6463 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
6464 because it is a member of class template B, DECL_ACCESS is
6465 recorded in the specialization `B<int>::g'. We cannot use its
6466 primary template because `B<T>::g' and `B<int>::g' may have
6467 different access. */
6468 if (DECL_TEMPLATE_INFO (fn)
6469 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
6470 access_fn = DECL_TI_TEMPLATE (fn);
6471 else
6472 access_fn = fn;
6473 if (flags & LOOKUP_SPECULATIVE)
6475 if (!speculative_access_check (cand->access_path, access_fn, fn,
6476 !!(flags & LOOKUP_COMPLAIN)))
6477 return error_mark_node;
6479 else
6480 perform_or_defer_access_check (cand->access_path, access_fn, fn);
6483 /* If we're checking for implicit delete, don't bother with argument
6484 conversions. */
6485 if (flags & LOOKUP_SPECULATIVE)
6487 if (DECL_DELETED_FN (fn))
6489 if (flags & LOOKUP_COMPLAIN)
6490 mark_used (fn);
6491 return error_mark_node;
6493 if (cand->viable == 1)
6494 return fn;
6495 else if (!(flags & LOOKUP_COMPLAIN))
6496 /* Reject bad conversions now. */
6497 return error_mark_node;
6498 /* else continue to get conversion error. */
6501 /* Find maximum size of vector to hold converted arguments. */
6502 parmlen = list_length (parm);
6503 nargs = VEC_length (tree, args) + (first_arg != NULL_TREE ? 1 : 0);
6504 if (parmlen > nargs)
6505 nargs = parmlen;
6506 argarray = XALLOCAVEC (tree, nargs);
6508 /* The implicit parameters to a constructor are not considered by overload
6509 resolution, and must be of the proper type. */
6510 if (DECL_CONSTRUCTOR_P (fn))
6512 if (first_arg != NULL_TREE)
6514 argarray[j++] = first_arg;
6515 first_arg = NULL_TREE;
6517 else
6519 argarray[j++] = VEC_index (tree, args, arg_index);
6520 ++arg_index;
6522 parm = TREE_CHAIN (parm);
6523 /* We should never try to call the abstract constructor. */
6524 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
6526 if (DECL_HAS_VTT_PARM_P (fn))
6528 argarray[j++] = VEC_index (tree, args, arg_index);
6529 ++arg_index;
6530 parm = TREE_CHAIN (parm);
6533 /* Bypass access control for 'this' parameter. */
6534 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6536 tree parmtype = TREE_VALUE (parm);
6537 tree arg = (first_arg != NULL_TREE
6538 ? first_arg
6539 : VEC_index (tree, args, arg_index));
6540 tree argtype = TREE_TYPE (arg);
6541 tree converted_arg;
6542 tree base_binfo;
6544 if (convs[i]->bad_p)
6546 if (complain & tf_error)
6547 permerror (input_location, "passing %qT as %<this%> argument of %q#D discards qualifiers",
6548 TREE_TYPE (argtype), fn);
6549 else
6550 return error_mark_node;
6553 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
6554 X is called for an object that is not of type X, or of a type
6555 derived from X, the behavior is undefined.
6557 So we can assume that anything passed as 'this' is non-null, and
6558 optimize accordingly. */
6559 gcc_assert (TREE_CODE (parmtype) == POINTER_TYPE);
6560 /* Convert to the base in which the function was declared. */
6561 gcc_assert (cand->conversion_path != NULL_TREE);
6562 converted_arg = build_base_path (PLUS_EXPR,
6563 arg,
6564 cand->conversion_path,
6565 1, complain);
6566 /* Check that the base class is accessible. */
6567 if (!accessible_base_p (TREE_TYPE (argtype),
6568 BINFO_TYPE (cand->conversion_path), true))
6569 error ("%qT is not an accessible base of %qT",
6570 BINFO_TYPE (cand->conversion_path),
6571 TREE_TYPE (argtype));
6572 /* If fn was found by a using declaration, the conversion path
6573 will be to the derived class, not the base declaring fn. We
6574 must convert from derived to base. */
6575 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
6576 TREE_TYPE (parmtype), ba_unique, NULL);
6577 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
6578 base_binfo, 1, complain);
6580 argarray[j++] = converted_arg;
6581 parm = TREE_CHAIN (parm);
6582 if (first_arg != NULL_TREE)
6583 first_arg = NULL_TREE;
6584 else
6585 ++arg_index;
6586 ++i;
6587 is_method = 1;
6590 gcc_assert (first_arg == NULL_TREE);
6591 for (; arg_index < VEC_length (tree, args) && parm;
6592 parm = TREE_CHAIN (parm), ++arg_index, ++i)
6594 tree type = TREE_VALUE (parm);
6595 tree arg = VEC_index (tree, args, arg_index);
6596 bool conversion_warning = true;
6598 conv = convs[i];
6600 /* If the argument is NULL and used to (implicitly) instantiate a
6601 template function (and bind one of the template arguments to
6602 the type of 'long int'), we don't want to warn about passing NULL
6603 to non-pointer argument.
6604 For example, if we have this template function:
6606 template<typename T> void func(T x) {}
6608 we want to warn (when -Wconversion is enabled) in this case:
6610 void foo() {
6611 func<int>(NULL);
6614 but not in this case:
6616 void foo() {
6617 func(NULL);
6620 if (arg == null_node
6621 && DECL_TEMPLATE_INFO (fn)
6622 && cand->template_decl
6623 && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
6624 conversion_warning = false;
6626 /* Warn about initializer_list deduction that isn't currently in the
6627 working draft. */
6628 if (cxx_dialect > cxx98
6629 && flag_deduce_init_list
6630 && cand->template_decl
6631 && is_std_init_list (non_reference (type))
6632 && BRACE_ENCLOSED_INITIALIZER_P (arg))
6634 tree tmpl = TI_TEMPLATE (cand->template_decl);
6635 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
6636 tree patparm = get_pattern_parm (realparm, tmpl);
6637 tree pattype = TREE_TYPE (patparm);
6638 if (PACK_EXPANSION_P (pattype))
6639 pattype = PACK_EXPANSION_PATTERN (pattype);
6640 pattype = non_reference (pattype);
6642 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
6643 && (cand->explicit_targs == NULL_TREE
6644 || (TREE_VEC_LENGTH (cand->explicit_targs)
6645 <= TEMPLATE_TYPE_IDX (pattype))))
6647 pedwarn (input_location, 0, "deducing %qT as %qT",
6648 non_reference (TREE_TYPE (patparm)),
6649 non_reference (type));
6650 pedwarn (input_location, 0, " in call to %q+D", cand->fn);
6651 pedwarn (input_location, 0,
6652 " (you can disable this with -fno-deduce-init-list)");
6656 val = convert_like_with_context (conv, arg, fn, i-is_method,
6657 conversion_warning
6658 ? complain
6659 : complain & (~tf_warning));
6661 val = convert_for_arg_passing (type, val, complain);
6662 if (val == error_mark_node)
6663 return error_mark_node;
6664 else
6665 argarray[j++] = val;
6668 /* Default arguments */
6669 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
6671 if (TREE_VALUE (parm) == error_mark_node)
6672 return error_mark_node;
6673 argarray[j++] = convert_default_arg (TREE_VALUE (parm),
6674 TREE_PURPOSE (parm),
6675 fn, i - is_method,
6676 complain);
6679 /* Ellipsis */
6680 for (; arg_index < VEC_length (tree, args); ++arg_index)
6682 tree a = VEC_index (tree, args, arg_index);
6683 if (magic_varargs_p (fn))
6684 /* Do no conversions for magic varargs. */
6685 a = mark_type_use (a);
6686 else
6687 a = convert_arg_to_ellipsis (a, complain);
6688 argarray[j++] = a;
6691 gcc_assert (j <= nargs);
6692 nargs = j;
6694 check_function_arguments (TREE_TYPE (fn), nargs, argarray);
6696 /* Avoid actually calling copy constructors and copy assignment operators,
6697 if possible. */
6699 if (! flag_elide_constructors)
6700 /* Do things the hard way. */;
6701 else if (cand->num_convs == 1
6702 && (DECL_COPY_CONSTRUCTOR_P (fn)
6703 || DECL_MOVE_CONSTRUCTOR_P (fn)))
6705 tree targ;
6706 tree arg = argarray[num_artificial_parms_for (fn)];
6707 tree fa;
6708 bool trivial = trivial_fn_p (fn);
6710 /* Pull out the real argument, disregarding const-correctness. */
6711 targ = arg;
6712 while (CONVERT_EXPR_P (targ)
6713 || TREE_CODE (targ) == NON_LVALUE_EXPR)
6714 targ = TREE_OPERAND (targ, 0);
6715 if (TREE_CODE (targ) == ADDR_EXPR)
6717 targ = TREE_OPERAND (targ, 0);
6718 if (!same_type_ignoring_top_level_qualifiers_p
6719 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
6720 targ = NULL_TREE;
6722 else
6723 targ = NULL_TREE;
6725 if (targ)
6726 arg = targ;
6727 else
6728 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
6730 /* [class.copy]: the copy constructor is implicitly defined even if
6731 the implementation elided its use. */
6732 if (!trivial || DECL_DELETED_FN (fn))
6734 mark_used (fn);
6735 already_used = true;
6738 /* If we're creating a temp and we already have one, don't create a
6739 new one. If we're not creating a temp but we get one, use
6740 INIT_EXPR to collapse the temp into our target. Otherwise, if the
6741 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
6742 temp or an INIT_EXPR otherwise. */
6743 fa = argarray[0];
6744 if (integer_zerop (fa))
6746 if (TREE_CODE (arg) == TARGET_EXPR)
6747 return arg;
6748 else if (trivial)
6749 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
6751 else if (TREE_CODE (arg) == TARGET_EXPR || trivial)
6753 tree to = stabilize_reference (cp_build_indirect_ref (fa, RO_NULL,
6754 complain));
6756 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
6757 return val;
6760 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
6761 && trivial_fn_p (fn)
6762 && !DECL_DELETED_FN (fn))
6764 tree to = stabilize_reference
6765 (cp_build_indirect_ref (argarray[0], RO_NULL, complain));
6766 tree type = TREE_TYPE (to);
6767 tree as_base = CLASSTYPE_AS_BASE (type);
6768 tree arg = argarray[1];
6770 if (is_really_empty_class (type))
6772 /* Avoid copying empty classes. */
6773 val = build2 (COMPOUND_EXPR, void_type_node, to, arg);
6774 TREE_NO_WARNING (val) = 1;
6775 val = build2 (COMPOUND_EXPR, type, val, to);
6776 TREE_NO_WARNING (val) = 1;
6778 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
6780 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
6781 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
6783 else
6785 /* We must only copy the non-tail padding parts. */
6786 tree arg0, arg2, t;
6787 tree array_type, alias_set;
6789 arg2 = TYPE_SIZE_UNIT (as_base);
6790 arg0 = cp_build_addr_expr (to, complain);
6792 array_type = build_array_type (char_type_node,
6793 build_index_type
6794 (size_binop (MINUS_EXPR,
6795 arg2, size_int (1))));
6796 alias_set = build_int_cst (build_pointer_type (type), 0);
6797 t = build2 (MODIFY_EXPR, void_type_node,
6798 build2 (MEM_REF, array_type, arg0, alias_set),
6799 build2 (MEM_REF, array_type, arg, alias_set));
6800 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
6801 TREE_NO_WARNING (val) = 1;
6804 return val;
6806 else if (DECL_DESTRUCTOR_P (fn)
6807 && trivial_fn_p (fn)
6808 && !DECL_DELETED_FN (fn))
6809 return fold_convert (void_type_node, argarray[0]);
6810 /* FIXME handle trivial default constructor, too. */
6812 if (!already_used)
6813 mark_used (fn);
6815 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
6817 tree t;
6818 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
6819 DECL_CONTEXT (fn),
6820 ba_any, NULL);
6821 gcc_assert (binfo && binfo != error_mark_node);
6823 /* Warn about deprecated virtual functions now, since we're about
6824 to throw away the decl. */
6825 if (TREE_DEPRECATED (fn))
6826 warn_deprecated_use (fn, NULL_TREE);
6828 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
6829 complain);
6830 if (TREE_SIDE_EFFECTS (argarray[0]))
6831 argarray[0] = save_expr (argarray[0]);
6832 t = build_pointer_type (TREE_TYPE (fn));
6833 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
6834 fn = build_java_interface_fn_ref (fn, argarray[0]);
6835 else
6836 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
6837 TREE_TYPE (fn) = t;
6839 else
6841 fn = build_addr_func (fn, complain);
6842 if (fn == error_mark_node)
6843 return error_mark_node;
6846 return build_cxx_call (fn, nargs, argarray);
6849 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
6850 This function performs no overload resolution, conversion, or other
6851 high-level operations. */
6853 tree
6854 build_cxx_call (tree fn, int nargs, tree *argarray)
6856 tree fndecl;
6858 /* Remember roughly where this call is. */
6859 location_t loc = EXPR_LOC_OR_HERE (fn);
6860 fn = build_call_a (fn, nargs, argarray);
6861 SET_EXPR_LOCATION (fn, loc);
6863 fndecl = get_callee_fndecl (fn);
6865 /* Check that arguments to builtin functions match the expectations. */
6866 if (fndecl
6867 && DECL_BUILT_IN (fndecl)
6868 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6869 && !check_builtin_function_arguments (fndecl, nargs, argarray))
6870 return error_mark_node;
6872 /* Some built-in function calls will be evaluated at compile-time in
6873 fold (). */
6874 fn = fold_if_not_in_template (fn);
6876 if (VOID_TYPE_P (TREE_TYPE (fn)))
6877 return fn;
6879 fn = require_complete_type (fn);
6880 if (fn == error_mark_node)
6881 return error_mark_node;
6883 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
6884 fn = build_cplus_new (TREE_TYPE (fn), fn, tf_warning_or_error);
6885 return convert_from_reference (fn);
6888 static GTY(()) tree java_iface_lookup_fn;
6890 /* Make an expression which yields the address of the Java interface
6891 method FN. This is achieved by generating a call to libjava's
6892 _Jv_LookupInterfaceMethodIdx(). */
6894 static tree
6895 build_java_interface_fn_ref (tree fn, tree instance)
6897 tree lookup_fn, method, idx;
6898 tree klass_ref, iface, iface_ref;
6899 int i;
6901 if (!java_iface_lookup_fn)
6903 tree ftype = build_function_type_list (ptr_type_node,
6904 ptr_type_node, ptr_type_node,
6905 java_int_type_node, NULL_TREE);
6906 java_iface_lookup_fn
6907 = add_builtin_function ("_Jv_LookupInterfaceMethodIdx", ftype,
6908 0, NOT_BUILT_IN, NULL, NULL_TREE);
6911 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
6912 This is the first entry in the vtable. */
6913 klass_ref = build_vtbl_ref (cp_build_indirect_ref (instance, RO_NULL,
6914 tf_warning_or_error),
6915 integer_zero_node);
6917 /* Get the java.lang.Class pointer for the interface being called. */
6918 iface = DECL_CONTEXT (fn);
6919 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
6920 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
6921 || DECL_CONTEXT (iface_ref) != iface)
6923 error ("could not find class$ field in java interface type %qT",
6924 iface);
6925 return error_mark_node;
6927 iface_ref = build_address (iface_ref);
6928 iface_ref = convert (build_pointer_type (iface), iface_ref);
6930 /* Determine the itable index of FN. */
6931 i = 1;
6932 for (method = TYPE_METHODS (iface); method; method = DECL_CHAIN (method))
6934 if (!DECL_VIRTUAL_P (method))
6935 continue;
6936 if (fn == method)
6937 break;
6938 i++;
6940 idx = build_int_cst (NULL_TREE, i);
6942 lookup_fn = build1 (ADDR_EXPR,
6943 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
6944 java_iface_lookup_fn);
6945 return build_call_nary (ptr_type_node, lookup_fn,
6946 3, klass_ref, iface_ref, idx);
6949 /* Returns the value to use for the in-charge parameter when making a
6950 call to a function with the indicated NAME.
6952 FIXME:Can't we find a neater way to do this mapping? */
6954 tree
6955 in_charge_arg_for_name (tree name)
6957 if (name == base_ctor_identifier
6958 || name == base_dtor_identifier)
6959 return integer_zero_node;
6960 else if (name == complete_ctor_identifier)
6961 return integer_one_node;
6962 else if (name == complete_dtor_identifier)
6963 return integer_two_node;
6964 else if (name == deleting_dtor_identifier)
6965 return integer_three_node;
6967 /* This function should only be called with one of the names listed
6968 above. */
6969 gcc_unreachable ();
6970 return NULL_TREE;
6973 /* Build a call to a constructor, destructor, or an assignment
6974 operator for INSTANCE, an expression with class type. NAME
6975 indicates the special member function to call; *ARGS are the
6976 arguments. ARGS may be NULL. This may change ARGS. BINFO
6977 indicates the base of INSTANCE that is to be passed as the `this'
6978 parameter to the member function called.
6980 FLAGS are the LOOKUP_* flags to use when processing the call.
6982 If NAME indicates a complete object constructor, INSTANCE may be
6983 NULL_TREE. In this case, the caller will call build_cplus_new to
6984 store the newly constructed object into a VAR_DECL. */
6986 tree
6987 build_special_member_call (tree instance, tree name, VEC(tree,gc) **args,
6988 tree binfo, int flags, tsubst_flags_t complain)
6990 tree fns;
6991 /* The type of the subobject to be constructed or destroyed. */
6992 tree class_type;
6993 VEC(tree,gc) *allocated = NULL;
6994 tree ret;
6996 gcc_assert (name == complete_ctor_identifier
6997 || name == base_ctor_identifier
6998 || name == complete_dtor_identifier
6999 || name == base_dtor_identifier
7000 || name == deleting_dtor_identifier
7001 || name == ansi_assopname (NOP_EXPR));
7002 if (TYPE_P (binfo))
7004 /* Resolve the name. */
7005 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
7006 return error_mark_node;
7008 binfo = TYPE_BINFO (binfo);
7011 gcc_assert (binfo != NULL_TREE);
7013 class_type = BINFO_TYPE (binfo);
7015 /* Handle the special case where INSTANCE is NULL_TREE. */
7016 if (name == complete_ctor_identifier && !instance)
7018 instance = build_int_cst (build_pointer_type (class_type), 0);
7019 instance = build1 (INDIRECT_REF, class_type, instance);
7021 else
7023 if (name == complete_dtor_identifier
7024 || name == base_dtor_identifier
7025 || name == deleting_dtor_identifier)
7026 gcc_assert (args == NULL || VEC_empty (tree, *args));
7028 /* Convert to the base class, if necessary. */
7029 if (!same_type_ignoring_top_level_qualifiers_p
7030 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
7032 if (name != ansi_assopname (NOP_EXPR))
7033 /* For constructors and destructors, either the base is
7034 non-virtual, or it is virtual but we are doing the
7035 conversion from a constructor or destructor for the
7036 complete object. In either case, we can convert
7037 statically. */
7038 instance = convert_to_base_statically (instance, binfo);
7039 else
7040 /* However, for assignment operators, we must convert
7041 dynamically if the base is virtual. */
7042 instance = build_base_path (PLUS_EXPR, instance,
7043 binfo, /*nonnull=*/1, complain);
7047 gcc_assert (instance != NULL_TREE);
7049 fns = lookup_fnfields (binfo, name, 1);
7051 /* When making a call to a constructor or destructor for a subobject
7052 that uses virtual base classes, pass down a pointer to a VTT for
7053 the subobject. */
7054 if ((name == base_ctor_identifier
7055 || name == base_dtor_identifier)
7056 && CLASSTYPE_VBASECLASSES (class_type))
7058 tree vtt;
7059 tree sub_vtt;
7061 /* If the current function is a complete object constructor
7062 or destructor, then we fetch the VTT directly.
7063 Otherwise, we look it up using the VTT we were given. */
7064 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
7065 vtt = decay_conversion (vtt, complain);
7066 if (vtt == error_mark_node)
7067 return error_mark_node;
7068 vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
7069 build2 (EQ_EXPR, boolean_type_node,
7070 current_in_charge_parm, integer_zero_node),
7071 current_vtt_parm,
7072 vtt);
7073 if (BINFO_SUBVTT_INDEX (binfo))
7074 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
7075 else
7076 sub_vtt = vtt;
7078 if (args == NULL)
7080 allocated = make_tree_vector ();
7081 args = &allocated;
7084 VEC_safe_insert (tree, gc, *args, 0, sub_vtt);
7087 ret = build_new_method_call (instance, fns, args,
7088 TYPE_BINFO (BINFO_TYPE (binfo)),
7089 flags, /*fn=*/NULL,
7090 complain);
7092 if (allocated != NULL)
7093 release_tree_vector (allocated);
7095 return ret;
7098 /* Return the NAME, as a C string. The NAME indicates a function that
7099 is a member of TYPE. *FREE_P is set to true if the caller must
7100 free the memory returned.
7102 Rather than go through all of this, we should simply set the names
7103 of constructors and destructors appropriately, and dispense with
7104 ctor_identifier, dtor_identifier, etc. */
7106 static char *
7107 name_as_c_string (tree name, tree type, bool *free_p)
7109 char *pretty_name;
7111 /* Assume that we will not allocate memory. */
7112 *free_p = false;
7113 /* Constructors and destructors are special. */
7114 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
7116 pretty_name
7117 = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type))));
7118 /* For a destructor, add the '~'. */
7119 if (name == complete_dtor_identifier
7120 || name == base_dtor_identifier
7121 || name == deleting_dtor_identifier)
7123 pretty_name = concat ("~", pretty_name, NULL);
7124 /* Remember that we need to free the memory allocated. */
7125 *free_p = true;
7128 else if (IDENTIFIER_TYPENAME_P (name))
7130 pretty_name = concat ("operator ",
7131 type_as_string_translate (TREE_TYPE (name),
7132 TFF_PLAIN_IDENTIFIER),
7133 NULL);
7134 /* Remember that we need to free the memory allocated. */
7135 *free_p = true;
7137 else
7138 pretty_name = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (name)));
7140 return pretty_name;
7143 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
7144 be set, upon return, to the function called. ARGS may be NULL.
7145 This may change ARGS. */
7147 static tree
7148 build_new_method_call_1 (tree instance, tree fns, VEC(tree,gc) **args,
7149 tree conversion_path, int flags,
7150 tree *fn_p, tsubst_flags_t complain)
7152 struct z_candidate *candidates = 0, *cand;
7153 tree explicit_targs = NULL_TREE;
7154 tree basetype = NULL_TREE;
7155 tree access_binfo;
7156 tree optype;
7157 tree first_mem_arg = NULL_TREE;
7158 tree instance_ptr;
7159 tree name;
7160 bool skip_first_for_error;
7161 VEC(tree,gc) *user_args;
7162 tree call;
7163 tree fn;
7164 int template_only = 0;
7165 bool any_viable_p;
7166 tree orig_instance;
7167 tree orig_fns;
7168 VEC(tree,gc) *orig_args = NULL;
7169 void *p;
7171 gcc_assert (instance != NULL_TREE);
7173 /* We don't know what function we're going to call, yet. */
7174 if (fn_p)
7175 *fn_p = NULL_TREE;
7177 if (error_operand_p (instance)
7178 || !fns || error_operand_p (fns))
7179 return error_mark_node;
7181 if (!BASELINK_P (fns))
7183 if (complain & tf_error)
7184 error ("call to non-function %qD", fns);
7185 return error_mark_node;
7188 orig_instance = instance;
7189 orig_fns = fns;
7191 /* Dismantle the baselink to collect all the information we need. */
7192 if (!conversion_path)
7193 conversion_path = BASELINK_BINFO (fns);
7194 access_binfo = BASELINK_ACCESS_BINFO (fns);
7195 optype = BASELINK_OPTYPE (fns);
7196 fns = BASELINK_FUNCTIONS (fns);
7197 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7199 explicit_targs = TREE_OPERAND (fns, 1);
7200 fns = TREE_OPERAND (fns, 0);
7201 template_only = 1;
7203 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
7204 || TREE_CODE (fns) == TEMPLATE_DECL
7205 || TREE_CODE (fns) == OVERLOAD);
7206 fn = get_first_fn (fns);
7207 name = DECL_NAME (fn);
7209 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
7210 gcc_assert (CLASS_TYPE_P (basetype));
7212 if (processing_template_decl)
7214 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
7215 instance = build_non_dependent_expr (instance);
7216 if (args != NULL)
7217 make_args_non_dependent (*args);
7220 user_args = args == NULL ? NULL : *args;
7221 /* Under DR 147 A::A() is an invalid constructor call,
7222 not a functional cast. */
7223 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
7225 if (! (complain & tf_error))
7226 return error_mark_node;
7228 permerror (input_location,
7229 "cannot call constructor %<%T::%D%> directly",
7230 basetype, name);
7231 permerror (input_location, " for a function-style cast, remove the "
7232 "redundant %<::%D%>", name);
7233 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
7234 complain);
7235 return call;
7238 /* Figure out whether to skip the first argument for the error
7239 message we will display to users if an error occurs. We don't
7240 want to display any compiler-generated arguments. The "this"
7241 pointer hasn't been added yet. However, we must remove the VTT
7242 pointer if this is a call to a base-class constructor or
7243 destructor. */
7244 skip_first_for_error = false;
7245 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
7247 /* Callers should explicitly indicate whether they want to construct
7248 the complete object or just the part without virtual bases. */
7249 gcc_assert (name != ctor_identifier);
7250 /* Similarly for destructors. */
7251 gcc_assert (name != dtor_identifier);
7252 /* Remove the VTT pointer, if present. */
7253 if ((name == base_ctor_identifier || name == base_dtor_identifier)
7254 && CLASSTYPE_VBASECLASSES (basetype))
7255 skip_first_for_error = true;
7258 /* Process the argument list. */
7259 if (args != NULL && *args != NULL)
7261 *args = resolve_args (*args, complain);
7262 if (*args == NULL)
7263 return error_mark_node;
7266 instance_ptr = build_this (instance);
7268 /* It's OK to call destructors and constructors on cv-qualified objects.
7269 Therefore, convert the INSTANCE_PTR to the unqualified type, if
7270 necessary. */
7271 if (DECL_DESTRUCTOR_P (fn)
7272 || DECL_CONSTRUCTOR_P (fn))
7274 tree type = build_pointer_type (basetype);
7275 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
7276 instance_ptr = build_nop (type, instance_ptr);
7278 if (DECL_DESTRUCTOR_P (fn))
7279 name = complete_dtor_identifier;
7281 first_mem_arg = instance_ptr;
7283 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7284 p = conversion_obstack_alloc (0);
7286 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
7287 initializer, not T({ }). */
7288 if (DECL_CONSTRUCTOR_P (fn) && args != NULL && !VEC_empty (tree, *args)
7289 && BRACE_ENCLOSED_INITIALIZER_P (VEC_index (tree, *args, 0))
7290 && CONSTRUCTOR_IS_DIRECT_INIT (VEC_index (tree, *args, 0)))
7292 tree init_list = VEC_index (tree, *args, 0);
7293 tree init = NULL_TREE;
7295 gcc_assert (VEC_length (tree, *args) == 1
7296 && !(flags & LOOKUP_ONLYCONVERTING));
7298 /* If the initializer list has no elements and T is a class type with
7299 a default constructor, the object is value-initialized. Handle
7300 this here so we don't need to handle it wherever we use
7301 build_special_member_call. */
7302 if (CONSTRUCTOR_NELTS (init_list) == 0
7303 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
7304 && !processing_template_decl)
7305 init = build_value_init (basetype, complain);
7307 /* If BASETYPE is an aggregate, we need to do aggregate
7308 initialization. */
7309 else if (CP_AGGREGATE_TYPE_P (basetype))
7310 init = digest_init (basetype, init_list, complain);
7312 if (init)
7314 tree ob;
7315 if (integer_zerop (instance_ptr))
7316 return get_target_expr_sfinae (init, complain);
7317 ob = build_fold_indirect_ref (instance_ptr);
7318 init = build2 (INIT_EXPR, TREE_TYPE (ob), ob, init);
7319 TREE_SIDE_EFFECTS (init) = true;
7320 return init;
7323 /* Otherwise go ahead with overload resolution. */
7324 add_list_candidates (fns, first_mem_arg, init_list,
7325 basetype, explicit_targs, template_only,
7326 conversion_path, access_binfo, flags,
7327 &candidates, complain);
7329 else
7331 add_candidates (fns, first_mem_arg, user_args, optype,
7332 explicit_targs, template_only, conversion_path,
7333 access_binfo, flags, &candidates, complain);
7335 any_viable_p = false;
7336 candidates = splice_viable (candidates, pedantic, &any_viable_p);
7338 if (!any_viable_p)
7340 if (complain & tf_error)
7342 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
7343 cxx_incomplete_type_error (instance_ptr, basetype);
7344 else if (optype)
7345 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
7346 basetype, optype, build_tree_list_vec (user_args),
7347 TREE_TYPE (TREE_TYPE (instance_ptr)));
7348 else
7350 char *pretty_name;
7351 bool free_p;
7352 tree arglist;
7354 pretty_name = name_as_c_string (name, basetype, &free_p);
7355 arglist = build_tree_list_vec (user_args);
7356 if (skip_first_for_error)
7357 arglist = TREE_CHAIN (arglist);
7358 error ("no matching function for call to %<%T::%s(%A)%#V%>",
7359 basetype, pretty_name, arglist,
7360 TREE_TYPE (TREE_TYPE (instance_ptr)));
7361 if (free_p)
7362 free (pretty_name);
7364 print_z_candidates (location_of (name), candidates);
7366 call = error_mark_node;
7368 else
7370 cand = tourney (candidates, complain);
7371 if (cand == 0)
7373 char *pretty_name;
7374 bool free_p;
7375 tree arglist;
7377 if (complain & tf_error)
7379 pretty_name = name_as_c_string (name, basetype, &free_p);
7380 arglist = build_tree_list_vec (user_args);
7381 if (skip_first_for_error)
7382 arglist = TREE_CHAIN (arglist);
7383 error ("call of overloaded %<%s(%A)%> is ambiguous", pretty_name,
7384 arglist);
7385 print_z_candidates (location_of (name), candidates);
7386 if (free_p)
7387 free (pretty_name);
7389 call = error_mark_node;
7391 else
7393 fn = cand->fn;
7395 if (!(flags & LOOKUP_NONVIRTUAL)
7396 && DECL_PURE_VIRTUAL_P (fn)
7397 && instance == current_class_ref
7398 && (DECL_CONSTRUCTOR_P (current_function_decl)
7399 || DECL_DESTRUCTOR_P (current_function_decl))
7400 && (complain & tf_warning))
7401 /* This is not an error, it is runtime undefined
7402 behavior. */
7403 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl) ?
7404 "pure virtual %q#D called from constructor"
7405 : "pure virtual %q#D called from destructor"),
7406 fn);
7408 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
7409 && is_dummy_object (instance_ptr))
7411 if (complain & tf_error)
7412 error ("cannot call member function %qD without object",
7413 fn);
7414 call = error_mark_node;
7416 else
7418 /* Optimize away vtable lookup if we know that this function
7419 can't be overridden. */
7420 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
7421 && (resolves_to_fixed_type_p (instance, 0)
7422 || DECL_FINAL_P (fn) || CLASSTYPE_FINAL (basetype)))
7423 flags |= LOOKUP_NONVIRTUAL;
7424 if (explicit_targs)
7425 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
7426 /* Now we know what function is being called. */
7427 if (fn_p)
7428 *fn_p = fn;
7429 /* Build the actual CALL_EXPR. */
7430 call = build_over_call (cand, flags, complain);
7431 /* In an expression of the form `a->f()' where `f' turns
7432 out to be a static member function, `a' is
7433 none-the-less evaluated. */
7434 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
7435 && !is_dummy_object (instance_ptr)
7436 && TREE_SIDE_EFFECTS (instance_ptr))
7437 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
7438 instance_ptr, call);
7439 else if (call != error_mark_node
7440 && DECL_DESTRUCTOR_P (cand->fn)
7441 && !VOID_TYPE_P (TREE_TYPE (call)))
7442 /* An explicit call of the form "x->~X()" has type
7443 "void". However, on platforms where destructors
7444 return "this" (i.e., those where
7445 targetm.cxx.cdtor_returns_this is true), such calls
7446 will appear to have a return value of pointer type
7447 to the low-level call machinery. We do not want to
7448 change the low-level machinery, since we want to be
7449 able to optimize "delete f()" on such platforms as
7450 "operator delete(~X(f()))" (rather than generating
7451 "t = f(), ~X(t), operator delete (t)"). */
7452 call = build_nop (void_type_node, call);
7457 if (processing_template_decl && call != error_mark_node)
7459 bool cast_to_void = false;
7461 if (TREE_CODE (call) == COMPOUND_EXPR)
7462 call = TREE_OPERAND (call, 1);
7463 else if (TREE_CODE (call) == NOP_EXPR)
7465 cast_to_void = true;
7466 call = TREE_OPERAND (call, 0);
7468 if (TREE_CODE (call) == INDIRECT_REF)
7469 call = TREE_OPERAND (call, 0);
7470 call = (build_min_non_dep_call_vec
7471 (call,
7472 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
7473 orig_instance, orig_fns, NULL_TREE),
7474 orig_args));
7475 call = convert_from_reference (call);
7476 if (cast_to_void)
7477 call = build_nop (void_type_node, call);
7480 /* Free all the conversions we allocated. */
7481 obstack_free (&conversion_obstack, p);
7483 if (orig_args != NULL)
7484 release_tree_vector (orig_args);
7486 return call;
7489 /* Wrapper for above. */
7491 tree
7492 build_new_method_call (tree instance, tree fns, VEC(tree,gc) **args,
7493 tree conversion_path, int flags,
7494 tree *fn_p, tsubst_flags_t complain)
7496 tree ret;
7497 bool subtime = timevar_cond_start (TV_OVERLOAD);
7498 ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
7499 fn_p, complain);
7500 timevar_cond_stop (TV_OVERLOAD, subtime);
7501 return ret;
7504 /* Returns true iff standard conversion sequence ICS1 is a proper
7505 subsequence of ICS2. */
7507 static bool
7508 is_subseq (conversion *ics1, conversion *ics2)
7510 /* We can assume that a conversion of the same code
7511 between the same types indicates a subsequence since we only get
7512 here if the types we are converting from are the same. */
7514 while (ics1->kind == ck_rvalue
7515 || ics1->kind == ck_lvalue)
7516 ics1 = next_conversion (ics1);
7518 while (1)
7520 while (ics2->kind == ck_rvalue
7521 || ics2->kind == ck_lvalue)
7522 ics2 = next_conversion (ics2);
7524 if (ics2->kind == ck_user
7525 || ics2->kind == ck_ambig
7526 || ics2->kind == ck_aggr
7527 || ics2->kind == ck_list
7528 || ics2->kind == ck_identity)
7529 /* At this point, ICS1 cannot be a proper subsequence of
7530 ICS2. We can get a USER_CONV when we are comparing the
7531 second standard conversion sequence of two user conversion
7532 sequences. */
7533 return false;
7535 ics2 = next_conversion (ics2);
7537 if (ics2->kind == ics1->kind
7538 && same_type_p (ics2->type, ics1->type)
7539 && same_type_p (next_conversion (ics2)->type,
7540 next_conversion (ics1)->type))
7541 return true;
7545 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
7546 be any _TYPE nodes. */
7548 bool
7549 is_properly_derived_from (tree derived, tree base)
7551 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
7552 return false;
7554 /* We only allow proper derivation here. The DERIVED_FROM_P macro
7555 considers every class derived from itself. */
7556 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
7557 && DERIVED_FROM_P (base, derived));
7560 /* We build the ICS for an implicit object parameter as a pointer
7561 conversion sequence. However, such a sequence should be compared
7562 as if it were a reference conversion sequence. If ICS is the
7563 implicit conversion sequence for an implicit object parameter,
7564 modify it accordingly. */
7566 static void
7567 maybe_handle_implicit_object (conversion **ics)
7569 if ((*ics)->this_p)
7571 /* [over.match.funcs]
7573 For non-static member functions, the type of the
7574 implicit object parameter is "reference to cv X"
7575 where X is the class of which the function is a
7576 member and cv is the cv-qualification on the member
7577 function declaration. */
7578 conversion *t = *ics;
7579 tree reference_type;
7581 /* The `this' parameter is a pointer to a class type. Make the
7582 implicit conversion talk about a reference to that same class
7583 type. */
7584 reference_type = TREE_TYPE (t->type);
7585 reference_type = build_reference_type (reference_type);
7587 if (t->kind == ck_qual)
7588 t = next_conversion (t);
7589 if (t->kind == ck_ptr)
7590 t = next_conversion (t);
7591 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
7592 t = direct_reference_binding (reference_type, t);
7593 t->this_p = 1;
7594 t->rvaluedness_matches_p = 0;
7595 *ics = t;
7599 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
7600 and return the initial reference binding conversion. Otherwise,
7601 leave *ICS unchanged and return NULL. */
7603 static conversion *
7604 maybe_handle_ref_bind (conversion **ics)
7606 if ((*ics)->kind == ck_ref_bind)
7608 conversion *old_ics = *ics;
7609 *ics = next_conversion (old_ics);
7610 (*ics)->user_conv_p = old_ics->user_conv_p;
7611 return old_ics;
7614 return NULL;
7617 /* Compare two implicit conversion sequences according to the rules set out in
7618 [over.ics.rank]. Return values:
7620 1: ics1 is better than ics2
7621 -1: ics2 is better than ics1
7622 0: ics1 and ics2 are indistinguishable */
7624 static int
7625 compare_ics (conversion *ics1, conversion *ics2)
7627 tree from_type1;
7628 tree from_type2;
7629 tree to_type1;
7630 tree to_type2;
7631 tree deref_from_type1 = NULL_TREE;
7632 tree deref_from_type2 = NULL_TREE;
7633 tree deref_to_type1 = NULL_TREE;
7634 tree deref_to_type2 = NULL_TREE;
7635 conversion_rank rank1, rank2;
7637 /* REF_BINDING is nonzero if the result of the conversion sequence
7638 is a reference type. In that case REF_CONV is the reference
7639 binding conversion. */
7640 conversion *ref_conv1;
7641 conversion *ref_conv2;
7643 /* Handle implicit object parameters. */
7644 maybe_handle_implicit_object (&ics1);
7645 maybe_handle_implicit_object (&ics2);
7647 /* Handle reference parameters. */
7648 ref_conv1 = maybe_handle_ref_bind (&ics1);
7649 ref_conv2 = maybe_handle_ref_bind (&ics2);
7651 /* List-initialization sequence L1 is a better conversion sequence than
7652 list-initialization sequence L2 if L1 converts to
7653 std::initializer_list<X> for some X and L2 does not. */
7654 if (ics1->kind == ck_list && ics2->kind != ck_list)
7655 return 1;
7656 if (ics2->kind == ck_list && ics1->kind != ck_list)
7657 return -1;
7659 /* [over.ics.rank]
7661 When comparing the basic forms of implicit conversion sequences (as
7662 defined in _over.best.ics_)
7664 --a standard conversion sequence (_over.ics.scs_) is a better
7665 conversion sequence than a user-defined conversion sequence
7666 or an ellipsis conversion sequence, and
7668 --a user-defined conversion sequence (_over.ics.user_) is a
7669 better conversion sequence than an ellipsis conversion sequence
7670 (_over.ics.ellipsis_). */
7671 rank1 = CONVERSION_RANK (ics1);
7672 rank2 = CONVERSION_RANK (ics2);
7674 if (rank1 > rank2)
7675 return -1;
7676 else if (rank1 < rank2)
7677 return 1;
7679 if (rank1 == cr_bad)
7681 /* Both ICS are bad. We try to make a decision based on what would
7682 have happened if they'd been good. This is not an extension,
7683 we'll still give an error when we build up the call; this just
7684 helps us give a more helpful error message. */
7685 rank1 = BAD_CONVERSION_RANK (ics1);
7686 rank2 = BAD_CONVERSION_RANK (ics2);
7688 if (rank1 > rank2)
7689 return -1;
7690 else if (rank1 < rank2)
7691 return 1;
7693 /* We couldn't make up our minds; try to figure it out below. */
7696 if (ics1->ellipsis_p)
7697 /* Both conversions are ellipsis conversions. */
7698 return 0;
7700 /* User-defined conversion sequence U1 is a better conversion sequence
7701 than another user-defined conversion sequence U2 if they contain the
7702 same user-defined conversion operator or constructor and if the sec-
7703 ond standard conversion sequence of U1 is better than the second
7704 standard conversion sequence of U2. */
7706 /* Handle list-conversion with the same code even though it isn't always
7707 ranked as a user-defined conversion and it doesn't have a second
7708 standard conversion sequence; it will still have the desired effect.
7709 Specifically, we need to do the reference binding comparison at the
7710 end of this function. */
7712 if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
7714 conversion *t1;
7715 conversion *t2;
7717 for (t1 = ics1; t1->kind != ck_user; t1 = next_conversion (t1))
7718 if (t1->kind == ck_ambig || t1->kind == ck_aggr
7719 || t1->kind == ck_list)
7720 break;
7721 for (t2 = ics2; t2->kind != ck_user; t2 = next_conversion (t2))
7722 if (t2->kind == ck_ambig || t2->kind == ck_aggr
7723 || t2->kind == ck_list)
7724 break;
7726 if (t1->kind != t2->kind)
7727 return 0;
7728 else if (t1->kind == ck_user)
7730 if (t1->cand->fn != t2->cand->fn)
7731 return 0;
7733 else
7735 /* For ambiguous or aggregate conversions, use the target type as
7736 a proxy for the conversion function. */
7737 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
7738 return 0;
7741 /* We can just fall through here, after setting up
7742 FROM_TYPE1 and FROM_TYPE2. */
7743 from_type1 = t1->type;
7744 from_type2 = t2->type;
7746 else
7748 conversion *t1;
7749 conversion *t2;
7751 /* We're dealing with two standard conversion sequences.
7753 [over.ics.rank]
7755 Standard conversion sequence S1 is a better conversion
7756 sequence than standard conversion sequence S2 if
7758 --S1 is a proper subsequence of S2 (comparing the conversion
7759 sequences in the canonical form defined by _over.ics.scs_,
7760 excluding any Lvalue Transformation; the identity
7761 conversion sequence is considered to be a subsequence of
7762 any non-identity conversion sequence */
7764 t1 = ics1;
7765 while (t1->kind != ck_identity)
7766 t1 = next_conversion (t1);
7767 from_type1 = t1->type;
7769 t2 = ics2;
7770 while (t2->kind != ck_identity)
7771 t2 = next_conversion (t2);
7772 from_type2 = t2->type;
7775 /* One sequence can only be a subsequence of the other if they start with
7776 the same type. They can start with different types when comparing the
7777 second standard conversion sequence in two user-defined conversion
7778 sequences. */
7779 if (same_type_p (from_type1, from_type2))
7781 if (is_subseq (ics1, ics2))
7782 return 1;
7783 if (is_subseq (ics2, ics1))
7784 return -1;
7787 /* [over.ics.rank]
7789 Or, if not that,
7791 --the rank of S1 is better than the rank of S2 (by the rules
7792 defined below):
7794 Standard conversion sequences are ordered by their ranks: an Exact
7795 Match is a better conversion than a Promotion, which is a better
7796 conversion than a Conversion.
7798 Two conversion sequences with the same rank are indistinguishable
7799 unless one of the following rules applies:
7801 --A conversion that does not a convert a pointer, pointer to member,
7802 or std::nullptr_t to bool is better than one that does.
7804 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
7805 so that we do not have to check it explicitly. */
7806 if (ics1->rank < ics2->rank)
7807 return 1;
7808 else if (ics2->rank < ics1->rank)
7809 return -1;
7811 to_type1 = ics1->type;
7812 to_type2 = ics2->type;
7814 /* A conversion from scalar arithmetic type to complex is worse than a
7815 conversion between scalar arithmetic types. */
7816 if (same_type_p (from_type1, from_type2)
7817 && ARITHMETIC_TYPE_P (from_type1)
7818 && ARITHMETIC_TYPE_P (to_type1)
7819 && ARITHMETIC_TYPE_P (to_type2)
7820 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
7821 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
7823 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
7824 return -1;
7825 else
7826 return 1;
7829 if (TYPE_PTR_P (from_type1)
7830 && TYPE_PTR_P (from_type2)
7831 && TYPE_PTR_P (to_type1)
7832 && TYPE_PTR_P (to_type2))
7834 deref_from_type1 = TREE_TYPE (from_type1);
7835 deref_from_type2 = TREE_TYPE (from_type2);
7836 deref_to_type1 = TREE_TYPE (to_type1);
7837 deref_to_type2 = TREE_TYPE (to_type2);
7839 /* The rules for pointers to members A::* are just like the rules
7840 for pointers A*, except opposite: if B is derived from A then
7841 A::* converts to B::*, not vice versa. For that reason, we
7842 switch the from_ and to_ variables here. */
7843 else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
7844 && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
7845 || (TYPE_PTRMEMFUNC_P (from_type1)
7846 && TYPE_PTRMEMFUNC_P (from_type2)
7847 && TYPE_PTRMEMFUNC_P (to_type1)
7848 && TYPE_PTRMEMFUNC_P (to_type2)))
7850 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
7851 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
7852 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
7853 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
7856 if (deref_from_type1 != NULL_TREE
7857 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
7858 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
7860 /* This was one of the pointer or pointer-like conversions.
7862 [over.ics.rank]
7864 --If class B is derived directly or indirectly from class A,
7865 conversion of B* to A* is better than conversion of B* to
7866 void*, and conversion of A* to void* is better than
7867 conversion of B* to void*. */
7868 if (TREE_CODE (deref_to_type1) == VOID_TYPE
7869 && TREE_CODE (deref_to_type2) == VOID_TYPE)
7871 if (is_properly_derived_from (deref_from_type1,
7872 deref_from_type2))
7873 return -1;
7874 else if (is_properly_derived_from (deref_from_type2,
7875 deref_from_type1))
7876 return 1;
7878 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
7879 || TREE_CODE (deref_to_type2) == VOID_TYPE)
7881 if (same_type_p (deref_from_type1, deref_from_type2))
7883 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
7885 if (is_properly_derived_from (deref_from_type1,
7886 deref_to_type1))
7887 return 1;
7889 /* We know that DEREF_TO_TYPE1 is `void' here. */
7890 else if (is_properly_derived_from (deref_from_type1,
7891 deref_to_type2))
7892 return -1;
7895 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
7896 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
7898 /* [over.ics.rank]
7900 --If class B is derived directly or indirectly from class A
7901 and class C is derived directly or indirectly from B,
7903 --conversion of C* to B* is better than conversion of C* to
7906 --conversion of B* to A* is better than conversion of C* to
7907 A* */
7908 if (same_type_p (deref_from_type1, deref_from_type2))
7910 if (is_properly_derived_from (deref_to_type1,
7911 deref_to_type2))
7912 return 1;
7913 else if (is_properly_derived_from (deref_to_type2,
7914 deref_to_type1))
7915 return -1;
7917 else if (same_type_p (deref_to_type1, deref_to_type2))
7919 if (is_properly_derived_from (deref_from_type2,
7920 deref_from_type1))
7921 return 1;
7922 else if (is_properly_derived_from (deref_from_type1,
7923 deref_from_type2))
7924 return -1;
7928 else if (CLASS_TYPE_P (non_reference (from_type1))
7929 && same_type_p (from_type1, from_type2))
7931 tree from = non_reference (from_type1);
7933 /* [over.ics.rank]
7935 --binding of an expression of type C to a reference of type
7936 B& is better than binding an expression of type C to a
7937 reference of type A&
7939 --conversion of C to B is better than conversion of C to A, */
7940 if (is_properly_derived_from (from, to_type1)
7941 && is_properly_derived_from (from, to_type2))
7943 if (is_properly_derived_from (to_type1, to_type2))
7944 return 1;
7945 else if (is_properly_derived_from (to_type2, to_type1))
7946 return -1;
7949 else if (CLASS_TYPE_P (non_reference (to_type1))
7950 && same_type_p (to_type1, to_type2))
7952 tree to = non_reference (to_type1);
7954 /* [over.ics.rank]
7956 --binding of an expression of type B to a reference of type
7957 A& is better than binding an expression of type C to a
7958 reference of type A&,
7960 --conversion of B to A is better than conversion of C to A */
7961 if (is_properly_derived_from (from_type1, to)
7962 && is_properly_derived_from (from_type2, to))
7964 if (is_properly_derived_from (from_type2, from_type1))
7965 return 1;
7966 else if (is_properly_derived_from (from_type1, from_type2))
7967 return -1;
7971 /* [over.ics.rank]
7973 --S1 and S2 differ only in their qualification conversion and yield
7974 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
7975 qualification signature of type T1 is a proper subset of the cv-
7976 qualification signature of type T2 */
7977 if (ics1->kind == ck_qual
7978 && ics2->kind == ck_qual
7979 && same_type_p (from_type1, from_type2))
7981 int result = comp_cv_qual_signature (to_type1, to_type2);
7982 if (result != 0)
7983 return result;
7986 /* [over.ics.rank]
7988 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
7989 to an implicit object parameter, and either S1 binds an lvalue reference
7990 to an lvalue and S2 binds an rvalue reference or S1 binds an rvalue
7991 reference to an rvalue and S2 binds an lvalue reference
7992 (C++0x draft standard, 13.3.3.2)
7994 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
7995 types to which the references refer are the same type except for
7996 top-level cv-qualifiers, and the type to which the reference
7997 initialized by S2 refers is more cv-qualified than the type to
7998 which the reference initialized by S1 refers.
8000 DR 1328 [over.match.best]: the context is an initialization by
8001 conversion function for direct reference binding (13.3.1.6) of a
8002 reference to function type, the return type of F1 is the same kind of
8003 reference (i.e. lvalue or rvalue) as the reference being initialized,
8004 and the return type of F2 is not. */
8006 if (ref_conv1 && ref_conv2)
8008 if (!ref_conv1->this_p && !ref_conv2->this_p
8009 && (ref_conv1->rvaluedness_matches_p
8010 != ref_conv2->rvaluedness_matches_p)
8011 && (same_type_p (ref_conv1->type, ref_conv2->type)
8012 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
8013 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
8015 return (ref_conv1->rvaluedness_matches_p
8016 - ref_conv2->rvaluedness_matches_p);
8019 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
8020 return comp_cv_qualification (TREE_TYPE (ref_conv2->type),
8021 TREE_TYPE (ref_conv1->type));
8024 /* Neither conversion sequence is better than the other. */
8025 return 0;
8028 /* The source type for this standard conversion sequence. */
8030 static tree
8031 source_type (conversion *t)
8033 for (;; t = next_conversion (t))
8035 if (t->kind == ck_user
8036 || t->kind == ck_ambig
8037 || t->kind == ck_identity)
8038 return t->type;
8040 gcc_unreachable ();
8043 /* Note a warning about preferring WINNER to LOSER. We do this by storing
8044 a pointer to LOSER and re-running joust to produce the warning if WINNER
8045 is actually used. */
8047 static void
8048 add_warning (struct z_candidate *winner, struct z_candidate *loser)
8050 candidate_warning *cw = (candidate_warning *)
8051 conversion_obstack_alloc (sizeof (candidate_warning));
8052 cw->loser = loser;
8053 cw->next = winner->warnings;
8054 winner->warnings = cw;
8057 /* Compare two candidates for overloading as described in
8058 [over.match.best]. Return values:
8060 1: cand1 is better than cand2
8061 -1: cand2 is better than cand1
8062 0: cand1 and cand2 are indistinguishable */
8064 static int
8065 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
8066 tsubst_flags_t complain)
8068 int winner = 0;
8069 int off1 = 0, off2 = 0;
8070 size_t i;
8071 size_t len;
8073 /* Candidates that involve bad conversions are always worse than those
8074 that don't. */
8075 if (cand1->viable > cand2->viable)
8076 return 1;
8077 if (cand1->viable < cand2->viable)
8078 return -1;
8080 /* If we have two pseudo-candidates for conversions to the same type,
8081 or two candidates for the same function, arbitrarily pick one. */
8082 if (cand1->fn == cand2->fn
8083 && (IS_TYPE_OR_DECL_P (cand1->fn)))
8084 return 1;
8086 /* a viable function F1
8087 is defined to be a better function than another viable function F2 if
8088 for all arguments i, ICSi(F1) is not a worse conversion sequence than
8089 ICSi(F2), and then */
8091 /* for some argument j, ICSj(F1) is a better conversion sequence than
8092 ICSj(F2) */
8094 /* For comparing static and non-static member functions, we ignore
8095 the implicit object parameter of the non-static function. The
8096 standard says to pretend that the static function has an object
8097 parm, but that won't work with operator overloading. */
8098 len = cand1->num_convs;
8099 if (len != cand2->num_convs)
8101 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
8102 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
8104 if (DECL_CONSTRUCTOR_P (cand1->fn)
8105 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
8106 /* We're comparing a near-match list constructor and a near-match
8107 non-list constructor. Just treat them as unordered. */
8108 return 0;
8110 gcc_assert (static_1 != static_2);
8112 if (static_1)
8113 off2 = 1;
8114 else
8116 off1 = 1;
8117 --len;
8121 for (i = 0; i < len; ++i)
8123 conversion *t1 = cand1->convs[i + off1];
8124 conversion *t2 = cand2->convs[i + off2];
8125 int comp = compare_ics (t1, t2);
8127 if (comp != 0)
8129 if ((complain & tf_warning)
8130 && warn_sign_promo
8131 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
8132 == cr_std + cr_promotion)
8133 && t1->kind == ck_std
8134 && t2->kind == ck_std
8135 && TREE_CODE (t1->type) == INTEGER_TYPE
8136 && TREE_CODE (t2->type) == INTEGER_TYPE
8137 && (TYPE_PRECISION (t1->type)
8138 == TYPE_PRECISION (t2->type))
8139 && (TYPE_UNSIGNED (next_conversion (t1)->type)
8140 || (TREE_CODE (next_conversion (t1)->type)
8141 == ENUMERAL_TYPE)))
8143 tree type = next_conversion (t1)->type;
8144 tree type1, type2;
8145 struct z_candidate *w, *l;
8146 if (comp > 0)
8147 type1 = t1->type, type2 = t2->type,
8148 w = cand1, l = cand2;
8149 else
8150 type1 = t2->type, type2 = t1->type,
8151 w = cand2, l = cand1;
8153 if (warn)
8155 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
8156 type, type1, type2);
8157 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
8159 else
8160 add_warning (w, l);
8163 if (winner && comp != winner)
8165 winner = 0;
8166 goto tweak;
8168 winner = comp;
8172 /* warn about confusing overload resolution for user-defined conversions,
8173 either between a constructor and a conversion op, or between two
8174 conversion ops. */
8175 if ((complain & tf_warning)
8176 && winner && warn_conversion && cand1->second_conv
8177 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
8178 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
8180 struct z_candidate *w, *l;
8181 bool give_warning = false;
8183 if (winner == 1)
8184 w = cand1, l = cand2;
8185 else
8186 w = cand2, l = cand1;
8188 /* We don't want to complain about `X::operator T1 ()'
8189 beating `X::operator T2 () const', when T2 is a no less
8190 cv-qualified version of T1. */
8191 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
8192 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
8194 tree t = TREE_TYPE (TREE_TYPE (l->fn));
8195 tree f = TREE_TYPE (TREE_TYPE (w->fn));
8197 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
8199 t = TREE_TYPE (t);
8200 f = TREE_TYPE (f);
8202 if (!comp_ptr_ttypes (t, f))
8203 give_warning = true;
8205 else
8206 give_warning = true;
8208 if (!give_warning)
8209 /*NOP*/;
8210 else if (warn)
8212 tree source = source_type (w->convs[0]);
8213 if (! DECL_CONSTRUCTOR_P (w->fn))
8214 source = TREE_TYPE (source);
8215 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
8216 && warning (OPT_Wconversion, " for conversion from %qT to %qT",
8217 source, w->second_conv->type))
8219 inform (input_location, " because conversion sequence for the argument is better");
8222 else
8223 add_warning (w, l);
8226 if (winner)
8227 return winner;
8229 /* DR 495 moved this tiebreaker above the template ones. */
8230 /* or, if not that,
8231 the context is an initialization by user-defined conversion (see
8232 _dcl.init_ and _over.match.user_) and the standard conversion
8233 sequence from the return type of F1 to the destination type (i.e.,
8234 the type of the entity being initialized) is a better conversion
8235 sequence than the standard conversion sequence from the return type
8236 of F2 to the destination type. */
8238 if (cand1->second_conv)
8240 winner = compare_ics (cand1->second_conv, cand2->second_conv);
8241 if (winner)
8242 return winner;
8245 /* or, if not that,
8246 F1 is a non-template function and F2 is a template function
8247 specialization. */
8249 if (!cand1->template_decl && cand2->template_decl)
8250 return 1;
8251 else if (cand1->template_decl && !cand2->template_decl)
8252 return -1;
8254 /* or, if not that,
8255 F1 and F2 are template functions and the function template for F1 is
8256 more specialized than the template for F2 according to the partial
8257 ordering rules. */
8259 if (cand1->template_decl && cand2->template_decl)
8261 winner = more_specialized_fn
8262 (TI_TEMPLATE (cand1->template_decl),
8263 TI_TEMPLATE (cand2->template_decl),
8264 /* [temp.func.order]: The presence of unused ellipsis and default
8265 arguments has no effect on the partial ordering of function
8266 templates. add_function_candidate() will not have
8267 counted the "this" argument for constructors. */
8268 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
8269 if (winner)
8270 return winner;
8273 /* Check whether we can discard a builtin candidate, either because we
8274 have two identical ones or matching builtin and non-builtin candidates.
8276 (Pedantically in the latter case the builtin which matched the user
8277 function should not be added to the overload set, but we spot it here.
8279 [over.match.oper]
8280 ... the builtin candidates include ...
8281 - do not have the same parameter type list as any non-template
8282 non-member candidate. */
8284 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
8285 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
8287 for (i = 0; i < len; ++i)
8288 if (!same_type_p (cand1->convs[i]->type,
8289 cand2->convs[i]->type))
8290 break;
8291 if (i == cand1->num_convs)
8293 if (cand1->fn == cand2->fn)
8294 /* Two built-in candidates; arbitrarily pick one. */
8295 return 1;
8296 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
8297 /* cand1 is built-in; prefer cand2. */
8298 return -1;
8299 else
8300 /* cand2 is built-in; prefer cand1. */
8301 return 1;
8305 /* If the two function declarations represent the same function (this can
8306 happen with declarations in multiple scopes and arg-dependent lookup),
8307 arbitrarily choose one. But first make sure the default args we're
8308 using match. */
8309 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
8310 && equal_functions (cand1->fn, cand2->fn))
8312 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
8313 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
8315 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
8317 for (i = 0; i < len; ++i)
8319 /* Don't crash if the fn is variadic. */
8320 if (!parms1)
8321 break;
8322 parms1 = TREE_CHAIN (parms1);
8323 parms2 = TREE_CHAIN (parms2);
8326 if (off1)
8327 parms1 = TREE_CHAIN (parms1);
8328 else if (off2)
8329 parms2 = TREE_CHAIN (parms2);
8331 for (; parms1; ++i)
8333 if (!cp_tree_equal (TREE_PURPOSE (parms1),
8334 TREE_PURPOSE (parms2)))
8336 if (warn)
8338 if (complain & tf_error)
8340 permerror (input_location,
8341 "default argument mismatch in "
8342 "overload resolution");
8343 inform (input_location,
8344 " candidate 1: %q+#F", cand1->fn);
8345 inform (input_location,
8346 " candidate 2: %q+#F", cand2->fn);
8348 else
8349 return 0;
8351 else
8352 add_warning (cand1, cand2);
8353 break;
8355 parms1 = TREE_CHAIN (parms1);
8356 parms2 = TREE_CHAIN (parms2);
8359 return 1;
8362 tweak:
8364 /* Extension: If the worst conversion for one candidate is worse than the
8365 worst conversion for the other, take the first. */
8366 if (!pedantic && (complain & tf_warning_or_error))
8368 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
8369 struct z_candidate *w = 0, *l = 0;
8371 for (i = 0; i < len; ++i)
8373 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
8374 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
8375 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
8376 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
8378 if (rank1 < rank2)
8379 winner = 1, w = cand1, l = cand2;
8380 if (rank1 > rank2)
8381 winner = -1, w = cand2, l = cand1;
8382 if (winner)
8384 /* Don't choose a deleted function over ambiguity. */
8385 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
8386 return 0;
8387 if (warn)
8389 pedwarn (input_location, 0,
8390 "ISO C++ says that these are ambiguous, even "
8391 "though the worst conversion for the first is better than "
8392 "the worst conversion for the second:");
8393 print_z_candidate (_("candidate 1:"), w);
8394 print_z_candidate (_("candidate 2:"), l);
8396 else
8397 add_warning (w, l);
8398 return winner;
8402 gcc_assert (!winner);
8403 return 0;
8406 /* Given a list of candidates for overloading, find the best one, if any.
8407 This algorithm has a worst case of O(2n) (winner is last), and a best
8408 case of O(n/2) (totally ambiguous); much better than a sorting
8409 algorithm. */
8411 static struct z_candidate *
8412 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
8414 struct z_candidate *champ = candidates, *challenger;
8415 int fate;
8416 int champ_compared_to_predecessor = 0;
8418 /* Walk through the list once, comparing each current champ to the next
8419 candidate, knocking out a candidate or two with each comparison. */
8421 for (challenger = champ->next; challenger; )
8423 fate = joust (champ, challenger, 0, complain);
8424 if (fate == 1)
8425 challenger = challenger->next;
8426 else
8428 if (fate == 0)
8430 champ = challenger->next;
8431 if (champ == 0)
8432 return NULL;
8433 champ_compared_to_predecessor = 0;
8435 else
8437 champ = challenger;
8438 champ_compared_to_predecessor = 1;
8441 challenger = champ->next;
8445 /* Make sure the champ is better than all the candidates it hasn't yet
8446 been compared to. */
8448 for (challenger = candidates;
8449 challenger != champ
8450 && !(champ_compared_to_predecessor && challenger->next == champ);
8451 challenger = challenger->next)
8453 fate = joust (champ, challenger, 0, complain);
8454 if (fate != 1)
8455 return NULL;
8458 return champ;
8461 /* Returns nonzero if things of type FROM can be converted to TO. */
8463 bool
8464 can_convert (tree to, tree from, tsubst_flags_t complain)
8466 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
8469 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
8471 bool
8472 can_convert_arg (tree to, tree from, tree arg, int flags,
8473 tsubst_flags_t complain)
8475 conversion *t;
8476 void *p;
8477 bool ok_p;
8479 /* Get the high-water mark for the CONVERSION_OBSTACK. */
8480 p = conversion_obstack_alloc (0);
8482 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
8483 flags, complain);
8484 ok_p = (t && !t->bad_p);
8486 /* Free all the conversions we allocated. */
8487 obstack_free (&conversion_obstack, p);
8489 return ok_p;
8492 /* Like can_convert_arg, but allows dubious conversions as well. */
8494 bool
8495 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
8496 tsubst_flags_t complain)
8498 conversion *t;
8499 void *p;
8501 /* Get the high-water mark for the CONVERSION_OBSTACK. */
8502 p = conversion_obstack_alloc (0);
8503 /* Try to perform the conversion. */
8504 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
8505 flags, complain);
8506 /* Free all the conversions we allocated. */
8507 obstack_free (&conversion_obstack, p);
8509 return t != NULL;
8512 /* Convert EXPR to TYPE. Return the converted expression.
8514 Note that we allow bad conversions here because by the time we get to
8515 this point we are committed to doing the conversion. If we end up
8516 doing a bad conversion, convert_like will complain. */
8518 tree
8519 perform_implicit_conversion_flags (tree type, tree expr,
8520 tsubst_flags_t complain, int flags)
8522 conversion *conv;
8523 void *p;
8525 if (error_operand_p (expr))
8526 return error_mark_node;
8528 /* Get the high-water mark for the CONVERSION_OBSTACK. */
8529 p = conversion_obstack_alloc (0);
8531 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
8532 /*c_cast_p=*/false,
8533 flags, complain);
8535 if (!conv)
8537 if (complain & tf_error)
8539 /* If expr has unknown type, then it is an overloaded function.
8540 Call instantiate_type to get good error messages. */
8541 if (TREE_TYPE (expr) == unknown_type_node)
8542 instantiate_type (type, expr, complain);
8543 else if (invalid_nonstatic_memfn_p (expr, complain))
8544 /* We gave an error. */;
8545 else
8546 error ("could not convert %qE from %qT to %qT", expr,
8547 TREE_TYPE (expr), type);
8549 expr = error_mark_node;
8551 else if (processing_template_decl && conv->kind != ck_identity)
8553 /* In a template, we are only concerned about determining the
8554 type of non-dependent expressions, so we do not have to
8555 perform the actual conversion. But for initializers, we
8556 need to be able to perform it at instantiation
8557 (or fold_non_dependent_expr) time. */
8558 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
8559 if (!(flags & LOOKUP_ONLYCONVERTING))
8560 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
8562 else
8563 expr = convert_like (conv, expr, complain);
8565 /* Free all the conversions we allocated. */
8566 obstack_free (&conversion_obstack, p);
8568 return expr;
8571 tree
8572 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
8574 return perform_implicit_conversion_flags (type, expr, complain,
8575 LOOKUP_IMPLICIT);
8578 /* Convert EXPR to TYPE (as a direct-initialization) if that is
8579 permitted. If the conversion is valid, the converted expression is
8580 returned. Otherwise, NULL_TREE is returned, except in the case
8581 that TYPE is a class type; in that case, an error is issued. If
8582 C_CAST_P is true, then this direct-initialization is taking
8583 place as part of a static_cast being attempted as part of a C-style
8584 cast. */
8586 tree
8587 perform_direct_initialization_if_possible (tree type,
8588 tree expr,
8589 bool c_cast_p,
8590 tsubst_flags_t complain)
8592 conversion *conv;
8593 void *p;
8595 if (type == error_mark_node || error_operand_p (expr))
8596 return error_mark_node;
8597 /* [dcl.init]
8599 If the destination type is a (possibly cv-qualified) class type:
8601 -- If the initialization is direct-initialization ...,
8602 constructors are considered. ... If no constructor applies, or
8603 the overload resolution is ambiguous, the initialization is
8604 ill-formed. */
8605 if (CLASS_TYPE_P (type))
8607 VEC(tree,gc) *args = make_tree_vector_single (expr);
8608 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8609 &args, type, LOOKUP_NORMAL, complain);
8610 release_tree_vector (args);
8611 return build_cplus_new (type, expr, complain);
8614 /* Get the high-water mark for the CONVERSION_OBSTACK. */
8615 p = conversion_obstack_alloc (0);
8617 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
8618 c_cast_p,
8619 LOOKUP_NORMAL, complain);
8620 if (!conv || conv->bad_p)
8621 expr = NULL_TREE;
8622 else
8623 expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
8624 /*issue_conversion_warnings=*/false,
8625 c_cast_p,
8626 complain);
8628 /* Free all the conversions we allocated. */
8629 obstack_free (&conversion_obstack, p);
8631 return expr;
8634 /* When initializing a reference that lasts longer than a full-expression,
8635 this special rule applies:
8637 [class.temporary]
8639 The temporary to which the reference is bound or the temporary
8640 that is the complete object to which the reference is bound
8641 persists for the lifetime of the reference.
8643 The temporaries created during the evaluation of the expression
8644 initializing the reference, except the temporary to which the
8645 reference is bound, are destroyed at the end of the
8646 full-expression in which they are created.
8648 In that case, we store the converted expression into a new
8649 VAR_DECL in a new scope.
8651 However, we want to be careful not to create temporaries when
8652 they are not required. For example, given:
8654 struct B {};
8655 struct D : public B {};
8656 D f();
8657 const B& b = f();
8659 there is no need to copy the return value from "f"; we can just
8660 extend its lifetime. Similarly, given:
8662 struct S {};
8663 struct T { operator S(); };
8664 T t;
8665 const S& s = t;
8667 we can extend the lifetime of the return value of the conversion
8668 operator.
8670 The next several functions are involved in this lifetime extension. */
8672 /* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference
8673 is being bound to a temporary. Create and return a new VAR_DECL
8674 with the indicated TYPE; this variable will store the value to
8675 which the reference is bound. */
8677 tree
8678 make_temporary_var_for_ref_to_temp (tree decl, tree type)
8680 tree var;
8682 /* Create the variable. */
8683 var = create_temporary_var (type);
8685 /* Register the variable. */
8686 if (TREE_STATIC (decl))
8688 /* Namespace-scope or local static; give it a mangled name. */
8689 /* FIXME share comdat with decl? */
8690 tree name;
8692 TREE_STATIC (var) = 1;
8693 name = mangle_ref_init_variable (decl);
8694 DECL_NAME (var) = name;
8695 SET_DECL_ASSEMBLER_NAME (var, name);
8696 var = pushdecl_top_level (var);
8698 else
8699 /* Create a new cleanup level if necessary. */
8700 maybe_push_cleanup_level (type);
8702 return var;
8705 /* EXPR is the initializer for a variable DECL of reference or
8706 std::initializer_list type. Create, push and return a new VAR_DECL
8707 for the initializer so that it will live as long as DECL. Any
8708 cleanup for the new variable is returned through CLEANUP, and the
8709 code to initialize the new variable is returned through INITP. */
8711 static tree
8712 set_up_extended_ref_temp (tree decl, tree expr, VEC(tree,gc) **cleanups,
8713 tree *initp)
8715 tree init;
8716 tree type;
8717 tree var;
8719 /* Create the temporary variable. */
8720 type = TREE_TYPE (expr);
8721 var = make_temporary_var_for_ref_to_temp (decl, type);
8722 layout_decl (var, 0);
8723 /* If the rvalue is the result of a function call it will be
8724 a TARGET_EXPR. If it is some other construct (such as a
8725 member access expression where the underlying object is
8726 itself the result of a function call), turn it into a
8727 TARGET_EXPR here. It is important that EXPR be a
8728 TARGET_EXPR below since otherwise the INIT_EXPR will
8729 attempt to make a bitwise copy of EXPR to initialize
8730 VAR. */
8731 if (TREE_CODE (expr) != TARGET_EXPR)
8732 expr = get_target_expr (expr);
8734 if (TREE_CODE (decl) == FIELD_DECL
8735 && extra_warnings && !TREE_NO_WARNING (decl))
8737 warning (OPT_Wextra, "a temporary bound to %qD only persists "
8738 "until the constructor exits", decl);
8739 TREE_NO_WARNING (decl) = true;
8742 /* Recursively extend temps in this initializer. */
8743 TARGET_EXPR_INITIAL (expr)
8744 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups);
8746 /* If the initializer is constant, put it in DECL_INITIAL so we get
8747 static initialization and use in constant expressions. */
8748 init = maybe_constant_init (expr);
8749 if (TREE_CONSTANT (init))
8751 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
8753 /* 5.19 says that a constant expression can include an
8754 lvalue-rvalue conversion applied to "a glvalue of literal type
8755 that refers to a non-volatile temporary object initialized
8756 with a constant expression". Rather than try to communicate
8757 that this VAR_DECL is a temporary, just mark it constexpr.
8759 Currently this is only useful for initializer_list temporaries,
8760 since reference vars can't appear in constant expressions. */
8761 DECL_DECLARED_CONSTEXPR_P (var) = true;
8762 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
8763 TREE_CONSTANT (var) = true;
8765 DECL_INITIAL (var) = init;
8766 init = NULL_TREE;
8768 else
8769 /* Create the INIT_EXPR that will initialize the temporary
8770 variable. */
8771 init = build2 (INIT_EXPR, type, var, expr);
8772 if (at_function_scope_p ())
8774 add_decl_expr (var);
8776 if (TREE_STATIC (var))
8777 init = add_stmt_to_compound (init, register_dtor_fn (var));
8778 else
8780 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
8781 if (cleanup)
8782 VEC_safe_push (tree, gc, *cleanups, cleanup);
8785 /* We must be careful to destroy the temporary only
8786 after its initialization has taken place. If the
8787 initialization throws an exception, then the
8788 destructor should not be run. We cannot simply
8789 transform INIT into something like:
8791 (INIT, ({ CLEANUP_STMT; }))
8793 because emit_local_var always treats the
8794 initializer as a full-expression. Thus, the
8795 destructor would run too early; it would run at the
8796 end of initializing the reference variable, rather
8797 than at the end of the block enclosing the
8798 reference variable.
8800 The solution is to pass back a cleanup expression
8801 which the caller is responsible for attaching to
8802 the statement tree. */
8804 else
8806 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
8807 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
8808 static_aggregates = tree_cons (NULL_TREE, var,
8809 static_aggregates);
8812 *initp = init;
8813 return var;
8816 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
8817 initializing a variable of that TYPE. */
8819 tree
8820 initialize_reference (tree type, tree expr,
8821 int flags, tsubst_flags_t complain)
8823 conversion *conv;
8824 void *p;
8826 if (type == error_mark_node || error_operand_p (expr))
8827 return error_mark_node;
8829 /* Get the high-water mark for the CONVERSION_OBSTACK. */
8830 p = conversion_obstack_alloc (0);
8832 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
8833 flags, complain);
8834 if (!conv || conv->bad_p)
8836 if (complain & tf_error)
8838 if (conv)
8839 convert_like (conv, expr, complain);
8840 else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
8841 && !TYPE_REF_IS_RVALUE (type)
8842 && !real_lvalue_p (expr))
8843 error ("invalid initialization of non-const reference of "
8844 "type %qT from an rvalue of type %qT",
8845 type, TREE_TYPE (expr));
8846 else
8847 error ("invalid initialization of reference of type "
8848 "%qT from expression of type %qT", type,
8849 TREE_TYPE (expr));
8851 return error_mark_node;
8854 gcc_assert (conv->kind == ck_ref_bind);
8856 /* Perform the conversion. */
8857 expr = convert_like (conv, expr, complain);
8859 /* Free all the conversions we allocated. */
8860 obstack_free (&conversion_obstack, p);
8862 return expr;
8865 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
8866 which is bound either to a reference or a std::initializer_list. */
8868 static tree
8869 extend_ref_init_temps_1 (tree decl, tree init, VEC(tree,gc) **cleanups)
8871 tree sub = init;
8872 tree *p;
8873 STRIP_NOPS (sub);
8874 if (TREE_CODE (sub) != ADDR_EXPR)
8875 return init;
8876 /* Deal with binding to a subobject. */
8877 for (p = &TREE_OPERAND (sub, 0); TREE_CODE (*p) == COMPONENT_REF; )
8878 p = &TREE_OPERAND (*p, 0);
8879 if (TREE_CODE (*p) == TARGET_EXPR)
8881 tree subinit = NULL_TREE;
8882 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit);
8883 if (subinit)
8884 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
8886 return init;
8889 /* INIT is part of the initializer for DECL. If there are any
8890 reference or initializer lists being initialized, extend their
8891 lifetime to match that of DECL. */
8893 tree
8894 extend_ref_init_temps (tree decl, tree init, VEC(tree,gc) **cleanups)
8896 tree type = TREE_TYPE (init);
8897 if (processing_template_decl)
8898 return init;
8899 if (TREE_CODE (type) == REFERENCE_TYPE)
8900 init = extend_ref_init_temps_1 (decl, init, cleanups);
8901 else if (is_std_init_list (type))
8903 /* The temporary array underlying a std::initializer_list
8904 is handled like a reference temporary. */
8905 tree ctor = init;
8906 if (TREE_CODE (ctor) == TARGET_EXPR)
8907 ctor = TARGET_EXPR_INITIAL (ctor);
8908 if (TREE_CODE (ctor) == CONSTRUCTOR)
8910 tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
8911 array = extend_ref_init_temps_1 (decl, array, cleanups);
8912 CONSTRUCTOR_ELT (ctor, 0)->value = array;
8915 else if (TREE_CODE (init) == CONSTRUCTOR)
8917 unsigned i;
8918 constructor_elt *p;
8919 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
8920 FOR_EACH_VEC_ELT (constructor_elt, elts, i, p)
8921 p->value = extend_ref_init_temps (decl, p->value, cleanups);
8924 return init;
8927 /* Returns true iff TYPE is some variant of std::initializer_list. */
8929 bool
8930 is_std_init_list (tree type)
8932 /* Look through typedefs. */
8933 if (!TYPE_P (type))
8934 return false;
8935 type = TYPE_MAIN_VARIANT (type);
8936 return (CLASS_TYPE_P (type)
8937 && CP_TYPE_CONTEXT (type) == std_node
8938 && strcmp (TYPE_NAME_STRING (type), "initializer_list") == 0);
8941 /* Returns true iff DECL is a list constructor: i.e. a constructor which
8942 will accept an argument list of a single std::initializer_list<T>. */
8944 bool
8945 is_list_ctor (tree decl)
8947 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
8948 tree arg;
8950 if (!args || args == void_list_node)
8951 return false;
8953 arg = non_reference (TREE_VALUE (args));
8954 if (!is_std_init_list (arg))
8955 return false;
8957 args = TREE_CHAIN (args);
8959 if (args && args != void_list_node && !TREE_PURPOSE (args))
8960 /* There are more non-defaulted parms. */
8961 return false;
8963 return true;
8966 #include "gt-cp-call.h"