Merge from trunk
[official-gcc.git] / gcc / cp / call.c
blobb459b49e5eb8861bb49d0ad37bd0c4bacf5cc3de
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* High-level class interface. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "tree.h"
30 #include "stor-layout.h"
31 #include "trans-mem.h"
32 #include "stringpool.h"
33 #include "cp-tree.h"
34 #include "flags.h"
35 #include "toplev.h"
36 #include "diagnostic-core.h"
37 #include "intl.h"
38 #include "target.h"
39 #include "convert.h"
40 #include "langhooks.h"
41 #include "c-family/c-objc.h"
42 #include "timevar.h"
43 #include "cgraph.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 (location_t, enum tree_code, enum tree_code, tree,
163 tree, 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 (location_t, 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, va_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, va_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, va_gc> *,
179 tree, 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, va_gc> *, tree,
192 tree, tsubst_flags_t);
193 static struct z_candidate *add_function_candidate
194 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
195 tree, int, 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, va_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, va_gc> *, tree, tree,
219 bool, tree, tree, int, struct z_candidate **,
220 tsubst_flags_t);
221 static conversion *merge_conversion_sequences (conversion *, conversion *);
222 static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
224 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
225 NAME can take many forms... */
227 bool
228 check_dtor_name (tree basetype, tree name)
230 /* Just accept something we've already complained about. */
231 if (name == error_mark_node)
232 return true;
234 if (TREE_CODE (name) == TYPE_DECL)
235 name = TREE_TYPE (name);
236 else if (TYPE_P (name))
237 /* OK */;
238 else if (identifier_p (name))
240 if ((MAYBE_CLASS_TYPE_P (basetype)
241 && name == constructor_name (basetype))
242 || (TREE_CODE (basetype) == ENUMERAL_TYPE
243 && name == TYPE_IDENTIFIER (basetype)))
244 return true;
245 else
246 name = get_type_value (name);
248 else
250 /* In the case of:
252 template <class T> struct S { ~S(); };
253 int i;
254 i.~S();
256 NAME will be a class template. */
257 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
258 return false;
261 if (!name || name == error_mark_node)
262 return false;
263 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
266 /* We want the address of a function or method. We avoid creating a
267 pointer-to-member function. */
269 tree
270 build_addr_func (tree function, tsubst_flags_t complain)
272 tree type = TREE_TYPE (function);
274 /* We have to do these by hand to avoid real pointer to member
275 functions. */
276 if (TREE_CODE (type) == METHOD_TYPE)
278 if (TREE_CODE (function) == OFFSET_REF)
280 tree object = build_address (TREE_OPERAND (function, 0));
281 return get_member_function_from_ptrfunc (&object,
282 TREE_OPERAND (function, 1),
283 complain);
285 function = build_address (function);
287 else
288 function = decay_conversion (function, complain);
290 return function;
293 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
294 POINTER_TYPE to those. Note, pointer to member function types
295 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
296 two variants. build_call_a is the primitive taking an array of
297 arguments, while build_call_n is a wrapper that handles varargs. */
299 tree
300 build_call_n (tree function, int n, ...)
302 if (n == 0)
303 return build_call_a (function, 0, NULL);
304 else
306 tree *argarray = XALLOCAVEC (tree, n);
307 va_list ap;
308 int i;
310 va_start (ap, n);
311 for (i = 0; i < n; i++)
312 argarray[i] = va_arg (ap, tree);
313 va_end (ap);
314 return build_call_a (function, n, argarray);
318 /* Update various flags in cfun and the call itself based on what is being
319 called. Split out of build_call_a so that bot_manip can use it too. */
321 void
322 set_flags_from_callee (tree call)
324 int nothrow;
325 tree decl = get_callee_fndecl (call);
327 /* We check both the decl and the type; a function may be known not to
328 throw without being declared throw(). */
329 nothrow = ((decl && TREE_NOTHROW (decl))
330 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (call)))));
332 if (!nothrow && at_function_scope_p () && cfun && cp_function_chain)
333 cp_function_chain->can_throw = 1;
335 if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
336 current_function_returns_abnormally = 1;
338 TREE_NOTHROW (call) = nothrow;
341 tree
342 build_call_a (tree function, int n, tree *argarray)
344 tree decl;
345 tree result_type;
346 tree fntype;
347 int i;
349 function = build_addr_func (function, tf_warning_or_error);
351 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
352 fntype = TREE_TYPE (TREE_TYPE (function));
353 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
354 || TREE_CODE (fntype) == METHOD_TYPE);
355 result_type = TREE_TYPE (fntype);
356 /* An rvalue has no cv-qualifiers. */
357 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
358 result_type = cv_unqualified (result_type);
360 function = build_call_array_loc (input_location,
361 result_type, function, n, argarray);
362 set_flags_from_callee (function);
364 decl = get_callee_fndecl (function);
366 if (decl && !TREE_USED (decl))
368 /* We invoke build_call directly for several library
369 functions. These may have been declared normally if
370 we're building libgcc, so we can't just check
371 DECL_ARTIFICIAL. */
372 gcc_assert (DECL_ARTIFICIAL (decl)
373 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
374 "__", 2));
375 mark_used (decl);
378 if (decl && TREE_DEPRECATED (decl))
379 warn_deprecated_use (decl, NULL_TREE);
380 require_complete_eh_spec_types (fntype, decl);
382 TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
384 /* Don't pass empty class objects by value. This is useful
385 for tags in STL, which are used to control overload resolution.
386 We don't need to handle other cases of copying empty classes. */
387 if (! decl || ! DECL_BUILT_IN (decl))
388 for (i = 0; i < n; i++)
390 tree arg = CALL_EXPR_ARG (function, i);
391 if (is_empty_class (TREE_TYPE (arg))
392 && ! TREE_ADDRESSABLE (TREE_TYPE (arg)))
394 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
395 arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
396 CALL_EXPR_ARG (function, i) = arg;
400 return function;
403 /* Build something of the form ptr->method (args)
404 or object.method (args). This can also build
405 calls to constructors, and find friends.
407 Member functions always take their class variable
408 as a pointer.
410 INSTANCE is a class instance.
412 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
414 PARMS help to figure out what that NAME really refers to.
416 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
417 down to the real instance type to use for access checking. We need this
418 information to get protected accesses correct.
420 FLAGS is the logical disjunction of zero or more LOOKUP_
421 flags. See cp-tree.h for more info.
423 If this is all OK, calls build_function_call with the resolved
424 member function.
426 This function must also handle being called to perform
427 initialization, promotion/coercion of arguments, and
428 instantiation of default parameters.
430 Note that NAME may refer to an instance variable name. If
431 `operator()()' is defined for the type of that field, then we return
432 that result. */
434 /* New overloading code. */
436 typedef struct z_candidate z_candidate;
438 typedef struct candidate_warning candidate_warning;
439 struct candidate_warning {
440 z_candidate *loser;
441 candidate_warning *next;
444 /* Information for providing diagnostics about why overloading failed. */
446 enum rejection_reason_code {
447 rr_none,
448 rr_arity,
449 rr_explicit_conversion,
450 rr_template_conversion,
451 rr_arg_conversion,
452 rr_bad_arg_conversion,
453 rr_template_unification,
454 rr_invalid_copy,
455 rr_constraint_failure
458 struct conversion_info {
459 /* The index of the argument, 0-based. */
460 int n_arg;
461 /* The type of the actual argument. */
462 tree from_type;
463 /* The type of the formal argument. */
464 tree to_type;
467 struct rejection_reason {
468 enum rejection_reason_code code;
469 union {
470 /* Information about an arity mismatch. */
471 struct {
472 /* The expected number of arguments. */
473 int expected;
474 /* The actual number of arguments in the call. */
475 int actual;
476 /* Whether the call was a varargs call. */
477 bool call_varargs_p;
478 } arity;
479 /* Information about an argument conversion mismatch. */
480 struct conversion_info conversion;
481 /* Same, but for bad argument conversions. */
482 struct conversion_info bad_conversion;
483 /* Information about template unification failures. These are the
484 parameters passed to fn_type_unification. */
485 struct {
486 tree tmpl;
487 tree explicit_targs;
488 int num_targs;
489 const tree *args;
490 unsigned int nargs;
491 tree return_type;
492 unification_kind_t strict;
493 int flags;
494 } template_unification;
495 /* Information about template instantiation failures. These are the
496 parameters passed to instantiate_template. */
497 struct {
498 tree tmpl;
499 tree targs;
500 } template_instantiation;
501 } u;
504 struct z_candidate {
505 /* The FUNCTION_DECL that will be called if this candidate is
506 selected by overload resolution. */
507 tree fn;
508 /* If not NULL_TREE, the first argument to use when calling this
509 function. */
510 tree first_arg;
511 /* The rest of the arguments to use when calling this function. If
512 there are no further arguments this may be NULL or it may be an
513 empty vector. */
514 const vec<tree, va_gc> *args;
515 /* The implicit conversion sequences for each of the arguments to
516 FN. */
517 conversion **convs;
518 /* The number of implicit conversion sequences. */
519 size_t num_convs;
520 /* If FN is a user-defined conversion, the standard conversion
521 sequence from the type returned by FN to the desired destination
522 type. */
523 conversion *second_conv;
524 int viable;
525 struct rejection_reason *reason;
526 /* If FN is a member function, the binfo indicating the path used to
527 qualify the name of FN at the call site. This path is used to
528 determine whether or not FN is accessible if it is selected by
529 overload resolution. The DECL_CONTEXT of FN will always be a
530 (possibly improper) base of this binfo. */
531 tree access_path;
532 /* If FN is a non-static member function, the binfo indicating the
533 subobject to which the `this' pointer should be converted if FN
534 is selected by overload resolution. The type pointed to by
535 the `this' pointer must correspond to the most derived class
536 indicated by the CONVERSION_PATH. */
537 tree conversion_path;
538 tree template_decl;
539 tree explicit_targs;
540 candidate_warning *warnings;
541 z_candidate *next;
544 /* Returns true iff T is a null pointer constant in the sense of
545 [conv.ptr]. */
547 bool
548 null_ptr_cst_p (tree t)
550 /* [conv.ptr]
552 A null pointer constant is an integral constant expression
553 (_expr.const_) rvalue of integer type that evaluates to zero or
554 an rvalue of type std::nullptr_t. */
555 if (NULLPTR_TYPE_P (TREE_TYPE (t)))
556 return true;
557 if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)))
559 /* Core issue 903 says only literal 0 is a null pointer constant. */
560 if (cxx_dialect < cxx11)
561 t = maybe_constant_value (fold_non_dependent_expr_sfinae (t, tf_none));
562 STRIP_NOPS (t);
563 if (integer_zerop (t) && !TREE_OVERFLOW (t))
564 return true;
566 return false;
569 /* Returns true iff T is a null member pointer value (4.11). */
571 bool
572 null_member_pointer_value_p (tree t)
574 tree type = TREE_TYPE (t);
575 if (!type)
576 return false;
577 else if (TYPE_PTRMEMFUNC_P (type))
578 return (TREE_CODE (t) == CONSTRUCTOR
579 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
580 else if (TYPE_PTRDATAMEM_P (type))
581 return integer_all_onesp (t);
582 else
583 return false;
586 /* Returns nonzero if PARMLIST consists of only default parms,
587 ellipsis, and/or undeduced parameter packs. */
589 bool
590 sufficient_parms_p (const_tree parmlist)
592 for (; parmlist && parmlist != void_list_node;
593 parmlist = TREE_CHAIN (parmlist))
594 if (!TREE_PURPOSE (parmlist)
595 && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
596 return false;
597 return true;
600 /* Allocate N bytes of memory from the conversion obstack. The memory
601 is zeroed before being returned. */
603 static void *
604 conversion_obstack_alloc (size_t n)
606 void *p;
607 if (!conversion_obstack_initialized)
609 gcc_obstack_init (&conversion_obstack);
610 conversion_obstack_initialized = true;
612 p = obstack_alloc (&conversion_obstack, n);
613 memset (p, 0, n);
614 return p;
617 /* Allocate rejection reasons. */
619 static struct rejection_reason *
620 alloc_rejection (enum rejection_reason_code code)
622 struct rejection_reason *p;
623 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
624 p->code = code;
625 return p;
628 static struct rejection_reason *
629 arity_rejection (tree first_arg, int expected, int actual)
631 struct rejection_reason *r = alloc_rejection (rr_arity);
632 int adjust = first_arg != NULL_TREE;
633 r->u.arity.expected = expected - adjust;
634 r->u.arity.actual = actual - adjust;
635 return r;
638 static struct rejection_reason *
639 arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
641 struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
642 int adjust = first_arg != NULL_TREE;
643 r->u.conversion.n_arg = n_arg - adjust;
644 r->u.conversion.from_type = from;
645 r->u.conversion.to_type = to;
646 return r;
649 static struct rejection_reason *
650 bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
652 struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
653 int adjust = first_arg != NULL_TREE;
654 r->u.bad_conversion.n_arg = n_arg - adjust;
655 r->u.bad_conversion.from_type = from;
656 r->u.bad_conversion.to_type = to;
657 return r;
660 static struct rejection_reason *
661 explicit_conversion_rejection (tree from, tree to)
663 struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
664 r->u.conversion.n_arg = 0;
665 r->u.conversion.from_type = from;
666 r->u.conversion.to_type = to;
667 return r;
670 static struct rejection_reason *
671 template_conversion_rejection (tree from, tree to)
673 struct rejection_reason *r = alloc_rejection (rr_template_conversion);
674 r->u.conversion.n_arg = 0;
675 r->u.conversion.from_type = from;
676 r->u.conversion.to_type = to;
677 return r;
680 static struct rejection_reason *
681 template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
682 const tree *args, unsigned int nargs,
683 tree return_type, unification_kind_t strict,
684 int flags)
686 size_t args_n_bytes = sizeof (*args) * nargs;
687 tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
688 struct rejection_reason *r = alloc_rejection (rr_template_unification);
689 r->u.template_unification.tmpl = tmpl;
690 r->u.template_unification.explicit_targs = explicit_targs;
691 r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
692 /* Copy args to our own storage. */
693 memcpy (args1, args, args_n_bytes);
694 r->u.template_unification.args = args1;
695 r->u.template_unification.nargs = nargs;
696 r->u.template_unification.return_type = return_type;
697 r->u.template_unification.strict = strict;
698 r->u.template_unification.flags = flags;
699 return r;
702 static struct rejection_reason *
703 template_unification_error_rejection (void)
705 return alloc_rejection (rr_template_unification);
708 static struct rejection_reason *
709 invalid_copy_with_fn_template_rejection (void)
711 struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
712 return r;
715 static struct rejection_reason *
716 template_constraint_failure (tree tmpl, tree targs)
718 struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
719 r->u.template_instantiation.tmpl = tmpl;
720 r->u.template_instantiation.targs = targs;
721 return r;
724 /* Dynamically allocate a conversion. */
726 static conversion *
727 alloc_conversion (conversion_kind kind)
729 conversion *c;
730 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
731 c->kind = kind;
732 return c;
735 #ifdef ENABLE_CHECKING
737 /* Make sure that all memory on the conversion obstack has been
738 freed. */
740 void
741 validate_conversion_obstack (void)
743 if (conversion_obstack_initialized)
744 gcc_assert ((obstack_next_free (&conversion_obstack)
745 == obstack_base (&conversion_obstack)));
748 #endif /* ENABLE_CHECKING */
750 /* Dynamically allocate an array of N conversions. */
752 static conversion **
753 alloc_conversions (size_t n)
755 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
758 static conversion *
759 build_conv (conversion_kind code, tree type, conversion *from)
761 conversion *t;
762 conversion_rank rank = CONVERSION_RANK (from);
764 /* Note that the caller is responsible for filling in t->cand for
765 user-defined conversions. */
766 t = alloc_conversion (code);
767 t->type = type;
768 t->u.next = from;
770 switch (code)
772 case ck_ptr:
773 case ck_pmem:
774 case ck_base:
775 case ck_std:
776 if (rank < cr_std)
777 rank = cr_std;
778 break;
780 case ck_qual:
781 if (rank < cr_exact)
782 rank = cr_exact;
783 break;
785 default:
786 break;
788 t->rank = rank;
789 t->user_conv_p = (code == ck_user || from->user_conv_p);
790 t->bad_p = from->bad_p;
791 t->base_p = false;
792 return t;
795 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
796 specialization of std::initializer_list<T>, if such a conversion is
797 possible. */
799 static conversion *
800 build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
802 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
803 unsigned len = CONSTRUCTOR_NELTS (ctor);
804 conversion **subconvs = alloc_conversions (len);
805 conversion *t;
806 unsigned i;
807 tree val;
809 /* Within a list-initialization we can have more user-defined
810 conversions. */
811 flags &= ~LOOKUP_NO_CONVERSION;
812 /* But no narrowing conversions. */
813 flags |= LOOKUP_NO_NARROWING;
815 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
817 conversion *sub
818 = implicit_conversion (elttype, TREE_TYPE (val), val,
819 false, flags, complain);
820 if (sub == NULL)
821 return NULL;
823 subconvs[i] = sub;
826 t = alloc_conversion (ck_list);
827 t->type = type;
828 t->u.list = subconvs;
829 t->rank = cr_exact;
831 for (i = 0; i < len; ++i)
833 conversion *sub = subconvs[i];
834 if (sub->rank > t->rank)
835 t->rank = sub->rank;
836 if (sub->user_conv_p)
837 t->user_conv_p = true;
838 if (sub->bad_p)
839 t->bad_p = true;
842 return t;
845 /* Return the next conversion of the conversion chain (if applicable),
846 or NULL otherwise. Please use this function instead of directly
847 accessing fields of struct conversion. */
849 static conversion *
850 next_conversion (conversion *conv)
852 if (conv == NULL
853 || conv->kind == ck_identity
854 || conv->kind == ck_ambig
855 || conv->kind == ck_list)
856 return NULL;
857 return conv->u.next;
860 /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
861 is a valid aggregate initializer for array type ATYPE. */
863 static bool
864 can_convert_array (tree atype, tree ctor, int flags, tsubst_flags_t complain)
866 unsigned i;
867 tree elttype = TREE_TYPE (atype);
868 for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
870 tree val = CONSTRUCTOR_ELT (ctor, i)->value;
871 bool ok;
872 if (TREE_CODE (elttype) == ARRAY_TYPE
873 && TREE_CODE (val) == CONSTRUCTOR)
874 ok = can_convert_array (elttype, val, flags, complain);
875 else
876 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
877 complain);
878 if (!ok)
879 return false;
881 return true;
884 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
885 aggregate class, if such a conversion is possible. */
887 static conversion *
888 build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
890 unsigned HOST_WIDE_INT i = 0;
891 conversion *c;
892 tree field = next_initializable_field (TYPE_FIELDS (type));
893 tree empty_ctor = NULL_TREE;
895 ctor = reshape_init (type, ctor, tf_none);
896 if (ctor == error_mark_node)
897 return NULL;
899 flags |= LOOKUP_NO_NARROWING;
901 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
903 tree ftype = TREE_TYPE (field);
904 tree val;
905 bool ok;
907 if (i < CONSTRUCTOR_NELTS (ctor))
908 val = CONSTRUCTOR_ELT (ctor, i)->value;
909 else if (TREE_CODE (ftype) == REFERENCE_TYPE)
910 /* Value-initialization of reference is ill-formed. */
911 return NULL;
912 else
914 if (empty_ctor == NULL_TREE)
915 empty_ctor = build_constructor (init_list_type_node, NULL);
916 val = empty_ctor;
918 ++i;
920 if (TREE_CODE (ftype) == ARRAY_TYPE
921 && TREE_CODE (val) == CONSTRUCTOR)
922 ok = can_convert_array (ftype, val, flags, complain);
923 else
924 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
925 complain);
927 if (!ok)
928 return NULL;
930 if (TREE_CODE (type) == UNION_TYPE)
931 break;
934 if (i < CONSTRUCTOR_NELTS (ctor))
935 return NULL;
937 c = alloc_conversion (ck_aggr);
938 c->type = type;
939 c->rank = cr_exact;
940 c->user_conv_p = true;
941 c->check_narrowing = true;
942 c->u.next = NULL;
943 return c;
946 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
947 array type, if such a conversion is possible. */
949 static conversion *
950 build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
952 conversion *c;
953 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
954 tree elttype = TREE_TYPE (type);
955 unsigned i;
956 tree val;
957 bool bad = false;
958 bool user = false;
959 enum conversion_rank rank = cr_exact;
961 if (TYPE_DOMAIN (type)
962 && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
964 unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
965 if (alen < len)
966 return NULL;
969 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
971 conversion *sub
972 = implicit_conversion (elttype, TREE_TYPE (val), val,
973 false, flags, complain);
974 if (sub == NULL)
975 return NULL;
977 if (sub->rank > rank)
978 rank = sub->rank;
979 if (sub->user_conv_p)
980 user = true;
981 if (sub->bad_p)
982 bad = true;
985 c = alloc_conversion (ck_aggr);
986 c->type = type;
987 c->rank = rank;
988 c->user_conv_p = user;
989 c->bad_p = bad;
990 c->u.next = NULL;
991 return c;
994 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
995 complex type, if such a conversion is possible. */
997 static conversion *
998 build_complex_conv (tree type, tree ctor, int flags,
999 tsubst_flags_t complain)
1001 conversion *c;
1002 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1003 tree elttype = TREE_TYPE (type);
1004 unsigned i;
1005 tree val;
1006 bool bad = false;
1007 bool user = false;
1008 enum conversion_rank rank = cr_exact;
1010 if (len != 2)
1011 return NULL;
1013 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
1015 conversion *sub
1016 = implicit_conversion (elttype, TREE_TYPE (val), val,
1017 false, flags, complain);
1018 if (sub == NULL)
1019 return NULL;
1021 if (sub->rank > rank)
1022 rank = sub->rank;
1023 if (sub->user_conv_p)
1024 user = true;
1025 if (sub->bad_p)
1026 bad = true;
1029 c = alloc_conversion (ck_aggr);
1030 c->type = type;
1031 c->rank = rank;
1032 c->user_conv_p = user;
1033 c->bad_p = bad;
1034 c->u.next = NULL;
1035 return c;
1038 /* Build a representation of the identity conversion from EXPR to
1039 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1041 static conversion *
1042 build_identity_conv (tree type, tree expr)
1044 conversion *c;
1046 c = alloc_conversion (ck_identity);
1047 c->type = type;
1048 c->u.expr = expr;
1050 return c;
1053 /* Converting from EXPR to TYPE was ambiguous in the sense that there
1054 were multiple user-defined conversions to accomplish the job.
1055 Build a conversion that indicates that ambiguity. */
1057 static conversion *
1058 build_ambiguous_conv (tree type, tree expr)
1060 conversion *c;
1062 c = alloc_conversion (ck_ambig);
1063 c->type = type;
1064 c->u.expr = expr;
1066 return c;
1069 tree
1070 strip_top_quals (tree t)
1072 if (TREE_CODE (t) == ARRAY_TYPE)
1073 return t;
1074 return cp_build_qualified_type (t, 0);
1077 /* Returns the standard conversion path (see [conv]) from type FROM to type
1078 TO, if any. For proper handling of null pointer constants, you must
1079 also pass the expression EXPR to convert from. If C_CAST_P is true,
1080 this conversion is coming from a C-style cast. */
1082 static conversion *
1083 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1084 int flags)
1086 enum tree_code fcode, tcode;
1087 conversion *conv;
1088 bool fromref = false;
1089 tree qualified_to;
1091 to = non_reference (to);
1092 if (TREE_CODE (from) == REFERENCE_TYPE)
1094 fromref = true;
1095 from = TREE_TYPE (from);
1097 qualified_to = to;
1098 to = strip_top_quals (to);
1099 from = strip_top_quals (from);
1101 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1102 && expr && type_unknown_p (expr))
1104 tsubst_flags_t tflags = tf_conv;
1105 expr = instantiate_type (to, expr, tflags);
1106 if (expr == error_mark_node)
1107 return NULL;
1108 from = TREE_TYPE (expr);
1111 fcode = TREE_CODE (from);
1112 tcode = TREE_CODE (to);
1114 conv = build_identity_conv (from, expr);
1115 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1117 from = type_decays_to (from);
1118 fcode = TREE_CODE (from);
1119 conv = build_conv (ck_lvalue, from, conv);
1121 else if (fromref || (expr && lvalue_p (expr)))
1123 if (expr)
1125 tree bitfield_type;
1126 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1127 if (bitfield_type)
1129 from = strip_top_quals (bitfield_type);
1130 fcode = TREE_CODE (from);
1133 conv = build_conv (ck_rvalue, from, conv);
1134 if (flags & LOOKUP_PREFER_RVALUE)
1135 conv->rvaluedness_matches_p = true;
1138 /* Allow conversion between `__complex__' data types. */
1139 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1141 /* The standard conversion sequence to convert FROM to TO is
1142 the standard conversion sequence to perform componentwise
1143 conversion. */
1144 conversion *part_conv = standard_conversion
1145 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags);
1147 if (part_conv)
1149 conv = build_conv (part_conv->kind, to, conv);
1150 conv->rank = part_conv->rank;
1152 else
1153 conv = NULL;
1155 return conv;
1158 if (same_type_p (from, to))
1160 if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1161 conv->type = qualified_to;
1162 return conv;
1165 /* [conv.ptr]
1166 A null pointer constant can be converted to a pointer type; ... A
1167 null pointer constant of integral type can be converted to an
1168 rvalue of type std::nullptr_t. */
1169 if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1170 || NULLPTR_TYPE_P (to))
1171 && expr && null_ptr_cst_p (expr))
1172 conv = build_conv (ck_std, to, conv);
1173 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1174 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1176 /* For backwards brain damage compatibility, allow interconversion of
1177 pointers and integers with a pedwarn. */
1178 conv = build_conv (ck_std, to, conv);
1179 conv->bad_p = true;
1181 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1183 /* For backwards brain damage compatibility, allow interconversion of
1184 enums and integers with a pedwarn. */
1185 conv = build_conv (ck_std, to, conv);
1186 conv->bad_p = true;
1188 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1189 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1191 tree to_pointee;
1192 tree from_pointee;
1194 if (tcode == POINTER_TYPE
1195 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
1196 TREE_TYPE (to)))
1198 else if (VOID_TYPE_P (TREE_TYPE (to))
1199 && !TYPE_PTRDATAMEM_P (from)
1200 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
1202 tree nfrom = TREE_TYPE (from);
1203 from = build_pointer_type
1204 (cp_build_qualified_type (void_type_node,
1205 cp_type_quals (nfrom)));
1206 conv = build_conv (ck_ptr, from, conv);
1208 else if (TYPE_PTRDATAMEM_P (from))
1210 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1211 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1213 if (DERIVED_FROM_P (fbase, tbase)
1214 && (same_type_ignoring_top_level_qualifiers_p
1215 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
1216 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
1218 from = build_ptrmem_type (tbase,
1219 TYPE_PTRMEM_POINTED_TO_TYPE (from));
1220 conv = build_conv (ck_pmem, from, conv);
1222 else if (!same_type_p (fbase, tbase))
1223 return NULL;
1225 else if (CLASS_TYPE_P (TREE_TYPE (from))
1226 && CLASS_TYPE_P (TREE_TYPE (to))
1227 /* [conv.ptr]
1229 An rvalue of type "pointer to cv D," where D is a
1230 class type, can be converted to an rvalue of type
1231 "pointer to cv B," where B is a base class (clause
1232 _class.derived_) of D. If B is an inaccessible
1233 (clause _class.access_) or ambiguous
1234 (_class.member.lookup_) base class of D, a program
1235 that necessitates this conversion is ill-formed.
1236 Therefore, we use DERIVED_FROM_P, and do not check
1237 access or uniqueness. */
1238 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
1240 from =
1241 cp_build_qualified_type (TREE_TYPE (to),
1242 cp_type_quals (TREE_TYPE (from)));
1243 from = build_pointer_type (from);
1244 conv = build_conv (ck_ptr, from, conv);
1245 conv->base_p = true;
1248 if (tcode == POINTER_TYPE)
1250 to_pointee = TREE_TYPE (to);
1251 from_pointee = TREE_TYPE (from);
1253 else
1255 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1256 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1259 if (same_type_p (from, to))
1260 /* OK */;
1261 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
1262 /* In a C-style cast, we ignore CV-qualification because we
1263 are allowed to perform a static_cast followed by a
1264 const_cast. */
1265 conv = build_conv (ck_qual, to, conv);
1266 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1267 conv = build_conv (ck_qual, to, conv);
1268 else if (expr && string_conv_p (to, expr, 0))
1269 /* converting from string constant to char *. */
1270 conv = build_conv (ck_qual, to, conv);
1271 /* Allow conversions among compatible ObjC pointer types (base
1272 conversions have been already handled above). */
1273 else if (c_dialect_objc ()
1274 && objc_compare_types (to, from, -4, NULL_TREE))
1275 conv = build_conv (ck_ptr, to, conv);
1276 else if (ptr_reasonably_similar (to_pointee, from_pointee))
1278 conv = build_conv (ck_ptr, to, conv);
1279 conv->bad_p = true;
1281 else
1282 return NULL;
1284 from = to;
1286 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1288 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1289 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1290 tree fbase = class_of_this_parm (fromfn);
1291 tree tbase = class_of_this_parm (tofn);
1293 if (!DERIVED_FROM_P (fbase, tbase)
1294 || !same_type_p (static_fn_type (fromfn),
1295 static_fn_type (tofn)))
1296 return NULL;
1298 from = build_memfn_type (fromfn,
1299 tbase,
1300 cp_type_quals (tbase),
1301 type_memfn_rqual (tofn));
1302 from = build_ptrmemfunc_type (build_pointer_type (from));
1303 conv = build_conv (ck_pmem, from, conv);
1304 conv->base_p = true;
1306 else if (tcode == BOOLEAN_TYPE)
1308 /* [conv.bool]
1310 An rvalue of arithmetic, unscoped enumeration, pointer, or
1311 pointer to member type can be converted to an rvalue of type
1312 bool. ... An rvalue of type std::nullptr_t can be converted
1313 to an rvalue of type bool; */
1314 if (ARITHMETIC_TYPE_P (from)
1315 || UNSCOPED_ENUM_P (from)
1316 || fcode == POINTER_TYPE
1317 || TYPE_PTRMEM_P (from)
1318 || NULLPTR_TYPE_P (from))
1320 conv = build_conv (ck_std, to, conv);
1321 if (fcode == POINTER_TYPE
1322 || TYPE_PTRDATAMEM_P (from)
1323 || (TYPE_PTRMEMFUNC_P (from)
1324 && conv->rank < cr_pbool)
1325 || NULLPTR_TYPE_P (from))
1326 conv->rank = cr_pbool;
1327 return conv;
1330 return NULL;
1332 /* We don't check for ENUMERAL_TYPE here because there are no standard
1333 conversions to enum type. */
1334 /* As an extension, allow conversion to complex type. */
1335 else if (ARITHMETIC_TYPE_P (to))
1337 if (! (INTEGRAL_CODE_P (fcode)
1338 || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1339 || SCOPED_ENUM_P (from))
1340 return NULL;
1341 conv = build_conv (ck_std, to, conv);
1343 /* Give this a better rank if it's a promotion. */
1344 if (same_type_p (to, type_promotes_to (from))
1345 && next_conversion (conv)->rank <= cr_promotion)
1346 conv->rank = cr_promotion;
1348 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1349 && vector_types_convertible_p (from, to, false))
1350 return build_conv (ck_std, to, conv);
1351 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1352 && is_properly_derived_from (from, to))
1354 if (conv->kind == ck_rvalue)
1355 conv = next_conversion (conv);
1356 conv = build_conv (ck_base, to, conv);
1357 /* The derived-to-base conversion indicates the initialization
1358 of a parameter with base type from an object of a derived
1359 type. A temporary object is created to hold the result of
1360 the conversion unless we're binding directly to a reference. */
1361 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1363 else
1364 return NULL;
1366 if (flags & LOOKUP_NO_NARROWING)
1367 conv->check_narrowing = true;
1369 return conv;
1372 /* Returns nonzero if T1 is reference-related to T2. */
1374 bool
1375 reference_related_p (tree t1, tree t2)
1377 if (t1 == error_mark_node || t2 == error_mark_node)
1378 return false;
1380 t1 = TYPE_MAIN_VARIANT (t1);
1381 t2 = TYPE_MAIN_VARIANT (t2);
1383 /* [dcl.init.ref]
1385 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1386 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
1387 of T2. */
1388 return (same_type_p (t1, t2)
1389 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1390 && DERIVED_FROM_P (t1, t2)));
1393 /* Returns nonzero if T1 is reference-compatible with T2. */
1395 static bool
1396 reference_compatible_p (tree t1, tree t2)
1398 /* [dcl.init.ref]
1400 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
1401 reference-related to T2 and cv1 is the same cv-qualification as,
1402 or greater cv-qualification than, cv2. */
1403 return (reference_related_p (t1, t2)
1404 && at_least_as_qualified_p (t1, t2));
1407 /* A reference of the indicated TYPE is being bound directly to the
1408 expression represented by the implicit conversion sequence CONV.
1409 Return a conversion sequence for this binding. */
1411 static conversion *
1412 direct_reference_binding (tree type, conversion *conv)
1414 tree t;
1416 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1417 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1419 t = TREE_TYPE (type);
1421 /* [over.ics.rank]
1423 When a parameter of reference type binds directly
1424 (_dcl.init.ref_) to an argument expression, the implicit
1425 conversion sequence is the identity conversion, unless the
1426 argument expression has a type that is a derived class of the
1427 parameter type, in which case the implicit conversion sequence is
1428 a derived-to-base Conversion.
1430 If the parameter binds directly to the result of applying a
1431 conversion function to the argument expression, the implicit
1432 conversion sequence is a user-defined conversion sequence
1433 (_over.ics.user_), with the second standard conversion sequence
1434 either an identity conversion or, if the conversion function
1435 returns an entity of a type that is a derived class of the
1436 parameter type, a derived-to-base conversion. */
1437 if (!same_type_ignoring_top_level_qualifiers_p (t, conv->type))
1439 /* Represent the derived-to-base conversion. */
1440 conv = build_conv (ck_base, t, conv);
1441 /* We will actually be binding to the base-class subobject in
1442 the derived class, so we mark this conversion appropriately.
1443 That way, convert_like knows not to generate a temporary. */
1444 conv->need_temporary_p = false;
1446 return build_conv (ck_ref_bind, type, conv);
1449 /* Returns the conversion path from type FROM to reference type TO for
1450 purposes of reference binding. For lvalue binding, either pass a
1451 reference type to FROM or an lvalue expression to EXPR. If the
1452 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1453 the conversion returned. If C_CAST_P is true, this
1454 conversion is coming from a C-style cast. */
1456 static conversion *
1457 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1458 tsubst_flags_t complain)
1460 conversion *conv = NULL;
1461 tree to = TREE_TYPE (rto);
1462 tree from = rfrom;
1463 tree tfrom;
1464 bool related_p;
1465 bool compatible_p;
1466 cp_lvalue_kind gl_kind;
1467 bool is_lvalue;
1469 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1471 expr = instantiate_type (to, expr, tf_none);
1472 if (expr == error_mark_node)
1473 return NULL;
1474 from = TREE_TYPE (expr);
1477 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1479 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1480 /* DR 1288: Otherwise, if the initializer list has a single element
1481 of type E and ... [T's] referenced type is reference-related to E,
1482 the object or reference is initialized from that element... */
1483 if (CONSTRUCTOR_NELTS (expr) == 1)
1485 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1486 if (error_operand_p (elt))
1487 return NULL;
1488 tree etype = TREE_TYPE (elt);
1489 if (reference_related_p (to, etype))
1491 expr = elt;
1492 from = etype;
1493 goto skip;
1496 /* Otherwise, if T is a reference type, a prvalue temporary of the
1497 type referenced by T is copy-list-initialized or
1498 direct-list-initialized, depending on the kind of initialization
1499 for the reference, and the reference is bound to that temporary. */
1500 conv = implicit_conversion (to, from, expr, c_cast_p,
1501 flags|LOOKUP_NO_TEMP_BIND, complain);
1502 skip:;
1505 if (TREE_CODE (from) == REFERENCE_TYPE)
1507 from = TREE_TYPE (from);
1508 if (!TYPE_REF_IS_RVALUE (rfrom)
1509 || TREE_CODE (from) == FUNCTION_TYPE)
1510 gl_kind = clk_ordinary;
1511 else
1512 gl_kind = clk_rvalueref;
1514 else if (expr)
1516 gl_kind = lvalue_kind (expr);
1517 if (gl_kind & clk_class)
1518 /* A class prvalue is not a glvalue. */
1519 gl_kind = clk_none;
1521 else
1522 gl_kind = clk_none;
1523 is_lvalue = gl_kind && !(gl_kind & clk_rvalueref);
1525 tfrom = from;
1526 if ((gl_kind & clk_bitfield) != 0)
1527 tfrom = unlowered_expr_type (expr);
1529 /* Figure out whether or not the types are reference-related and
1530 reference compatible. We have do do this after stripping
1531 references from FROM. */
1532 related_p = reference_related_p (to, tfrom);
1533 /* If this is a C cast, first convert to an appropriately qualified
1534 type, so that we can later do a const_cast to the desired type. */
1535 if (related_p && c_cast_p
1536 && !at_least_as_qualified_p (to, tfrom))
1537 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1538 compatible_p = reference_compatible_p (to, tfrom);
1540 /* Directly bind reference when target expression's type is compatible with
1541 the reference and expression is an lvalue. In DR391, the wording in
1542 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1543 const and rvalue references to rvalues of compatible class type.
1544 We should also do direct bindings for non-class xvalues. */
1545 if (compatible_p
1546 && (is_lvalue
1547 || (((CP_TYPE_CONST_NON_VOLATILE_P (to)
1548 && !(flags & LOOKUP_NO_RVAL_BIND))
1549 || TYPE_REF_IS_RVALUE (rto))
1550 && (gl_kind
1551 || (!(flags & LOOKUP_NO_TEMP_BIND)
1552 && (CLASS_TYPE_P (from)
1553 || TREE_CODE (from) == ARRAY_TYPE))))))
1555 /* [dcl.init.ref]
1557 If the initializer expression
1559 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1560 is reference-compatible with "cv2 T2,"
1562 the reference is bound directly to the initializer expression
1563 lvalue.
1565 [...]
1566 If the initializer expression is an rvalue, with T2 a class type,
1567 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1568 is bound to the object represented by the rvalue or to a sub-object
1569 within that object. */
1571 conv = build_identity_conv (tfrom, expr);
1572 conv = direct_reference_binding (rto, conv);
1574 if (flags & LOOKUP_PREFER_RVALUE)
1575 /* The top-level caller requested that we pretend that the lvalue
1576 be treated as an rvalue. */
1577 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1578 else if (TREE_CODE (rfrom) == REFERENCE_TYPE)
1579 /* Handle rvalue reference to function properly. */
1580 conv->rvaluedness_matches_p
1581 = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1582 else
1583 conv->rvaluedness_matches_p
1584 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1586 if ((gl_kind & clk_bitfield) != 0
1587 || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1588 /* For the purposes of overload resolution, we ignore the fact
1589 this expression is a bitfield or packed field. (In particular,
1590 [over.ics.ref] says specifically that a function with a
1591 non-const reference parameter is viable even if the
1592 argument is a bitfield.)
1594 However, when we actually call the function we must create
1595 a temporary to which to bind the reference. If the
1596 reference is volatile, or isn't const, then we cannot make
1597 a temporary, so we just issue an error when the conversion
1598 actually occurs. */
1599 conv->need_temporary_p = true;
1601 /* Don't allow binding of lvalues (other than function lvalues) to
1602 rvalue references. */
1603 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1604 && TREE_CODE (to) != FUNCTION_TYPE
1605 && !(flags & LOOKUP_PREFER_RVALUE))
1606 conv->bad_p = true;
1608 return conv;
1610 /* [class.conv.fct] A conversion function is never used to convert a
1611 (possibly cv-qualified) object to the (possibly cv-qualified) same
1612 object type (or a reference to it), to a (possibly cv-qualified) base
1613 class of that type (or a reference to it).... */
1614 else if (CLASS_TYPE_P (from) && !related_p
1615 && !(flags & LOOKUP_NO_CONVERSION))
1617 /* [dcl.init.ref]
1619 If the initializer expression
1621 -- has a class type (i.e., T2 is a class type) can be
1622 implicitly converted to an lvalue of type "cv3 T3," where
1623 "cv1 T1" is reference-compatible with "cv3 T3". (this
1624 conversion is selected by enumerating the applicable
1625 conversion functions (_over.match.ref_) and choosing the
1626 best one through overload resolution. (_over.match_).
1628 the reference is bound to the lvalue result of the conversion
1629 in the second case. */
1630 z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
1631 complain);
1632 if (cand)
1633 return cand->second_conv;
1636 /* From this point on, we conceptually need temporaries, even if we
1637 elide them. Only the cases above are "direct bindings". */
1638 if (flags & LOOKUP_NO_TEMP_BIND)
1639 return NULL;
1641 /* [over.ics.rank]
1643 When a parameter of reference type is not bound directly to an
1644 argument expression, the conversion sequence is the one required
1645 to convert the argument expression to the underlying type of the
1646 reference according to _over.best.ics_. Conceptually, this
1647 conversion sequence corresponds to copy-initializing a temporary
1648 of the underlying type with the argument expression. Any
1649 difference in top-level cv-qualification is subsumed by the
1650 initialization itself and does not constitute a conversion. */
1652 /* [dcl.init.ref]
1654 Otherwise, the reference shall be an lvalue reference to a
1655 non-volatile const type, or the reference shall be an rvalue
1656 reference. */
1657 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1658 return NULL;
1660 /* [dcl.init.ref]
1662 Otherwise, a temporary of type "cv1 T1" is created and
1663 initialized from the initializer expression using the rules for a
1664 non-reference copy initialization. If T1 is reference-related to
1665 T2, cv1 must be the same cv-qualification as, or greater
1666 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1667 if (related_p && !at_least_as_qualified_p (to, from))
1668 return NULL;
1670 /* We're generating a temporary now, but don't bind any more in the
1671 conversion (specifically, don't slice the temporary returned by a
1672 conversion operator). */
1673 flags |= LOOKUP_NO_TEMP_BIND;
1675 /* Core issue 899: When [copy-]initializing a temporary to be bound
1676 to the first parameter of a copy constructor (12.8) called with
1677 a single argument in the context of direct-initialization,
1678 explicit conversion functions are also considered.
1680 So don't set LOOKUP_ONLYCONVERTING in that case. */
1681 if (!(flags & LOOKUP_COPY_PARM))
1682 flags |= LOOKUP_ONLYCONVERTING;
1684 if (!conv)
1685 conv = implicit_conversion (to, from, expr, c_cast_p,
1686 flags, complain);
1687 if (!conv)
1688 return NULL;
1690 conv = build_conv (ck_ref_bind, rto, conv);
1691 /* This reference binding, unlike those above, requires the
1692 creation of a temporary. */
1693 conv->need_temporary_p = true;
1694 if (TYPE_REF_IS_RVALUE (rto))
1696 conv->rvaluedness_matches_p = 1;
1697 /* In the second case, if the reference is an rvalue reference and
1698 the second standard conversion sequence of the user-defined
1699 conversion sequence includes an lvalue-to-rvalue conversion, the
1700 program is ill-formed. */
1701 if (conv->user_conv_p && next_conversion (conv)->kind == ck_rvalue)
1702 conv->bad_p = 1;
1705 return conv;
1708 /* Returns the implicit conversion sequence (see [over.ics]) from type
1709 FROM to type TO. The optional expression EXPR may affect the
1710 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1711 true, this conversion is coming from a C-style cast. */
1713 static conversion *
1714 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1715 int flags, tsubst_flags_t complain)
1717 conversion *conv;
1719 if (from == error_mark_node || to == error_mark_node
1720 || expr == error_mark_node)
1721 return NULL;
1723 /* Other flags only apply to the primary function in overload
1724 resolution, or after we've chosen one. */
1725 flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
1726 |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE
1727 |LOOKUP_NO_NARROWING|LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL);
1729 /* FIXME: actually we don't want warnings either, but we can't just
1730 have 'complain &= ~(tf_warning|tf_error)' because it would cause
1731 the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
1732 We really ought not to issue that warning until we've committed
1733 to that conversion. */
1734 complain &= ~tf_error;
1736 if (TREE_CODE (to) == REFERENCE_TYPE)
1737 conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
1738 else
1739 conv = standard_conversion (to, from, expr, c_cast_p, flags);
1741 if (conv)
1742 return conv;
1744 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1746 if (is_std_init_list (to))
1747 return build_list_conv (to, expr, flags, complain);
1749 /* As an extension, allow list-initialization of _Complex. */
1750 if (TREE_CODE (to) == COMPLEX_TYPE)
1752 conv = build_complex_conv (to, expr, flags, complain);
1753 if (conv)
1754 return conv;
1757 /* Allow conversion from an initializer-list with one element to a
1758 scalar type. */
1759 if (SCALAR_TYPE_P (to))
1761 int nelts = CONSTRUCTOR_NELTS (expr);
1762 tree elt;
1764 if (nelts == 0)
1765 elt = build_value_init (to, tf_none);
1766 else if (nelts == 1)
1767 elt = CONSTRUCTOR_ELT (expr, 0)->value;
1768 else
1769 elt = error_mark_node;
1771 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
1772 c_cast_p, flags, complain);
1773 if (conv)
1775 conv->check_narrowing = true;
1776 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
1777 /* Too many levels of braces, i.e. '{{1}}'. */
1778 conv->bad_p = true;
1779 return conv;
1782 else if (TREE_CODE (to) == ARRAY_TYPE)
1783 return build_array_conv (to, expr, flags, complain);
1786 if (expr != NULL_TREE
1787 && (MAYBE_CLASS_TYPE_P (from)
1788 || MAYBE_CLASS_TYPE_P (to))
1789 && (flags & LOOKUP_NO_CONVERSION) == 0)
1791 struct z_candidate *cand;
1793 if (CLASS_TYPE_P (to)
1794 && BRACE_ENCLOSED_INITIALIZER_P (expr)
1795 && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
1796 return build_aggr_conv (to, expr, flags, complain);
1798 cand = build_user_type_conversion_1 (to, expr, flags, complain);
1799 if (cand)
1800 conv = cand->second_conv;
1802 /* We used to try to bind a reference to a temporary here, but that
1803 is now handled after the recursive call to this function at the end
1804 of reference_binding. */
1805 return conv;
1808 return NULL;
1811 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1812 functions. ARGS will not be changed until a single candidate is
1813 selected. */
1815 static struct z_candidate *
1816 add_candidate (struct z_candidate **candidates,
1817 tree fn, tree first_arg, const vec<tree, va_gc> *args,
1818 size_t num_convs, conversion **convs,
1819 tree access_path, tree conversion_path,
1820 int viable, struct rejection_reason *reason)
1822 struct z_candidate *cand = (struct z_candidate *)
1823 conversion_obstack_alloc (sizeof (struct z_candidate));
1825 cand->fn = fn;
1826 cand->first_arg = first_arg;
1827 cand->args = args;
1828 cand->convs = convs;
1829 cand->num_convs = num_convs;
1830 cand->access_path = access_path;
1831 cand->conversion_path = conversion_path;
1832 cand->viable = viable;
1833 cand->reason = reason;
1834 cand->next = *candidates;
1835 *candidates = cand;
1837 return cand;
1840 /* Return the number of remaining arguments in the parameter list
1841 beginning with ARG. */
1843 static int
1844 remaining_arguments (tree arg)
1846 int n;
1848 for (n = 0; arg != NULL_TREE && arg != void_list_node;
1849 arg = TREE_CHAIN (arg))
1850 n++;
1852 return n;
1855 // Returns true if FN is a non-template member function or non-template
1856 // friend function. Both kinds of declaration can be constrained.
1857 static inline bool
1858 is_constrainable_non_template_fn (tree fn)
1860 if (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (fn))
1861 return true;
1863 return DECL_FUNCTION_MEMBER_P (fn) &&
1864 DECL_TEMPLATE_INFO (fn) &&
1865 !DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn));
1868 /* Create an overload candidate for the function or method FN called
1869 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
1870 FLAGS is passed on to implicit_conversion.
1872 This does not change ARGS.
1874 CTYPE, if non-NULL, is the type we want to pretend this function
1875 comes from for purposes of overload resolution. */
1877 static struct z_candidate *
1878 add_function_candidate (struct z_candidate **candidates,
1879 tree fn, tree ctype, tree first_arg,
1880 const vec<tree, va_gc> *args, tree access_path,
1881 tree conversion_path, int flags,
1882 tsubst_flags_t complain)
1884 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1885 int i, len;
1886 conversion **convs;
1887 tree parmnode;
1888 tree orig_first_arg = first_arg;
1889 int skip;
1890 int viable = 1;
1891 struct rejection_reason *reason = NULL;
1893 /* At this point we should not see any functions which haven't been
1894 explicitly declared, except for friend functions which will have
1895 been found using argument dependent lookup. */
1896 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
1898 /* The `this', `in_chrg' and VTT arguments to constructors are not
1899 considered in overload resolution. */
1900 if (DECL_CONSTRUCTOR_P (fn))
1902 parmlist = skip_artificial_parms_for (fn, parmlist);
1903 skip = num_artificial_parms_for (fn);
1904 if (skip > 0 && first_arg != NULL_TREE)
1906 --skip;
1907 first_arg = NULL_TREE;
1910 else
1911 skip = 0;
1913 len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
1914 convs = alloc_conversions (len);
1916 // Viable functions
1918 // Functions whose constraints are not satisfied are non-viable.
1920 // For function templates, constraints are checked as part of template
1921 // argument deduction. A failure there means that the template is
1922 // already added as a non-viable candidate. For non-template member
1923 // functions, however, the declaration declaration has already been
1924 // synthesized, but its constraints have not actually been checked.
1925 // We should do that now.
1927 // TODO: Consider checking constrained non-template members during
1928 // class template instantiation and setting a flag indicating whether
1929 // or not the declaration is viable. This could be set as a flag in
1930 // TEMPLATE_INFO (there should be a bunch of unused bits there).
1931 if (is_constrainable_non_template_fn (fn))
1933 tree tmpl = DECL_TI_TEMPLATE (fn);
1934 tree args = DECL_TI_ARGS (fn);
1935 if (!check_template_constraints (tmpl, args))
1937 reason = template_constraint_failure (tmpl, args);
1938 viable = false;
1939 goto out;
1943 /* 13.3.2 - Viable functions [over.match.viable]
1944 First, to be a viable function, a candidate function shall have enough
1945 parameters to agree in number with the arguments in the list.
1947 We need to check this first; otherwise, checking the ICSes might cause
1948 us to produce an ill-formed template instantiation. */
1950 parmnode = parmlist;
1951 for (i = 0; i < len; ++i)
1953 if (parmnode == NULL_TREE || parmnode == void_list_node)
1954 break;
1955 parmnode = TREE_CHAIN (parmnode);
1958 if ((i < len && parmnode)
1959 || !sufficient_parms_p (parmnode))
1961 int remaining = remaining_arguments (parmnode);
1962 viable = 0;
1963 reason = arity_rejection (first_arg, i + remaining, len);
1965 /* When looking for a function from a subobject from an implicit
1966 copy/move constructor/operator=, don't consider anything that takes (a
1967 reference to) an unrelated type. See c++/44909 and core 1092. */
1968 else if (parmlist && (flags & LOOKUP_DEFAULTED))
1970 if (DECL_CONSTRUCTOR_P (fn))
1971 i = 1;
1972 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1973 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
1974 i = 2;
1975 else
1976 i = 0;
1977 if (i && len == i)
1979 parmnode = chain_index (i-1, parmlist);
1980 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
1981 ctype))
1982 viable = 0;
1985 /* This only applies at the top level. */
1986 flags &= ~LOOKUP_DEFAULTED;
1989 if (! viable)
1990 goto out;
1992 /* Second, for F to be a viable function, there shall exist for each
1993 argument an implicit conversion sequence that converts that argument
1994 to the corresponding parameter of F. */
1996 parmnode = parmlist;
1998 for (i = 0; i < len; ++i)
2000 tree argtype, to_type;
2001 tree arg;
2002 conversion *t;
2003 int is_this;
2005 if (parmnode == void_list_node)
2006 break;
2008 if (i == 0 && first_arg != NULL_TREE)
2009 arg = first_arg;
2010 else
2011 arg = CONST_CAST_TREE (
2012 (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2013 argtype = lvalue_type (arg);
2015 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2016 && ! DECL_CONSTRUCTOR_P (fn));
2018 if (parmnode)
2020 tree parmtype = TREE_VALUE (parmnode);
2021 int lflags = flags;
2023 parmnode = TREE_CHAIN (parmnode);
2025 /* The type of the implicit object parameter ('this') for
2026 overload resolution is not always the same as for the
2027 function itself; conversion functions are considered to
2028 be members of the class being converted, and functions
2029 introduced by a using-declaration are considered to be
2030 members of the class that uses them.
2032 Since build_over_call ignores the ICS for the `this'
2033 parameter, we can just change the parm type. */
2034 if (ctype && is_this)
2036 parmtype = cp_build_qualified_type
2037 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
2038 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2040 /* If the function has a ref-qualifier, the implicit
2041 object parameter has reference type. */
2042 bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2043 parmtype = cp_build_reference_type (parmtype, rv);
2045 else
2047 parmtype = build_pointer_type (parmtype);
2048 arg = build_this (arg);
2049 argtype = lvalue_type (arg);
2053 /* Core issue 899: When [copy-]initializing a temporary to be bound
2054 to the first parameter of a copy constructor (12.8) called with
2055 a single argument in the context of direct-initialization,
2056 explicit conversion functions are also considered.
2058 So set LOOKUP_COPY_PARM to let reference_binding know that
2059 it's being called in that context. We generalize the above
2060 to handle move constructors and template constructors as well;
2061 the standardese should soon be updated similarly. */
2062 if (ctype && i == 0 && (len-skip == 1)
2063 && DECL_CONSTRUCTOR_P (fn)
2064 && parmtype != error_mark_node
2065 && (same_type_ignoring_top_level_qualifiers_p
2066 (non_reference (parmtype), ctype)))
2068 if (!(flags & LOOKUP_ONLYCONVERTING))
2069 lflags |= LOOKUP_COPY_PARM;
2070 /* We allow user-defined conversions within init-lists, but
2071 don't list-initialize the copy parm, as that would mean
2072 using two levels of braces for the same type. */
2073 if ((flags & LOOKUP_LIST_INIT_CTOR)
2074 && BRACE_ENCLOSED_INITIALIZER_P (arg))
2075 lflags |= LOOKUP_NO_CONVERSION;
2077 else
2078 lflags |= LOOKUP_ONLYCONVERTING;
2080 t = implicit_conversion (parmtype, argtype, arg,
2081 /*c_cast_p=*/false, lflags, complain);
2082 to_type = parmtype;
2084 else
2086 t = build_identity_conv (argtype, arg);
2087 t->ellipsis_p = true;
2088 to_type = argtype;
2091 if (t && is_this)
2092 t->this_p = true;
2094 convs[i] = t;
2095 if (! t)
2097 viable = 0;
2098 reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
2099 break;
2102 if (t->bad_p)
2104 viable = -1;
2105 reason = bad_arg_conversion_rejection (first_arg, i, argtype, to_type);
2109 out:
2110 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2111 access_path, conversion_path, viable, reason);
2114 /* Create an overload candidate for the conversion function FN which will
2115 be invoked for expression OBJ, producing a pointer-to-function which
2116 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2117 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2118 passed on to implicit_conversion.
2120 Actually, we don't really care about FN; we care about the type it
2121 converts to. There may be multiple conversion functions that will
2122 convert to that type, and we rely on build_user_type_conversion_1 to
2123 choose the best one; so when we create our candidate, we record the type
2124 instead of the function. */
2126 static struct z_candidate *
2127 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2128 tree first_arg, const vec<tree, va_gc> *arglist,
2129 tree access_path, tree conversion_path,
2130 tsubst_flags_t complain)
2132 tree totype = TREE_TYPE (TREE_TYPE (fn));
2133 int i, len, viable, flags;
2134 tree parmlist, parmnode;
2135 conversion **convs;
2136 struct rejection_reason *reason;
2138 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2139 parmlist = TREE_TYPE (parmlist);
2140 parmlist = TYPE_ARG_TYPES (parmlist);
2142 len = vec_safe_length (arglist) + (first_arg != NULL_TREE ? 1 : 0) + 1;
2143 convs = alloc_conversions (len);
2144 parmnode = parmlist;
2145 viable = 1;
2146 flags = LOOKUP_IMPLICIT;
2147 reason = NULL;
2149 /* Don't bother looking up the same type twice. */
2150 if (*candidates && (*candidates)->fn == totype)
2151 return NULL;
2153 for (i = 0; i < len; ++i)
2155 tree arg, argtype, convert_type = NULL_TREE;
2156 conversion *t;
2158 if (i == 0)
2159 arg = obj;
2160 else if (i == 1 && first_arg != NULL_TREE)
2161 arg = first_arg;
2162 else
2163 arg = (*arglist)[i - (first_arg != NULL_TREE ? 1 : 0) - 1];
2164 argtype = lvalue_type (arg);
2166 if (i == 0)
2168 t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false,
2169 flags, complain);
2170 convert_type = totype;
2172 else if (parmnode == void_list_node)
2173 break;
2174 else if (parmnode)
2176 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2177 /*c_cast_p=*/false, flags, complain);
2178 convert_type = TREE_VALUE (parmnode);
2180 else
2182 t = build_identity_conv (argtype, arg);
2183 t->ellipsis_p = true;
2184 convert_type = argtype;
2187 convs[i] = t;
2188 if (! t)
2189 break;
2191 if (t->bad_p)
2193 viable = -1;
2194 reason = bad_arg_conversion_rejection (NULL_TREE, i, argtype, convert_type);
2197 if (i == 0)
2198 continue;
2200 if (parmnode)
2201 parmnode = TREE_CHAIN (parmnode);
2204 if (i < len
2205 || ! sufficient_parms_p (parmnode))
2207 int remaining = remaining_arguments (parmnode);
2208 viable = 0;
2209 reason = arity_rejection (NULL_TREE, i + remaining, len);
2212 return add_candidate (candidates, totype, first_arg, arglist, len, convs,
2213 access_path, conversion_path, viable, reason);
2216 static void
2217 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2218 tree type1, tree type2, tree *args, tree *argtypes,
2219 int flags, tsubst_flags_t complain)
2221 conversion *t;
2222 conversion **convs;
2223 size_t num_convs;
2224 int viable = 1, i;
2225 tree types[2];
2226 struct rejection_reason *reason = NULL;
2228 types[0] = type1;
2229 types[1] = type2;
2231 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
2232 convs = alloc_conversions (num_convs);
2234 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2235 conversion ops are allowed. We handle that here by just checking for
2236 boolean_type_node because other operators don't ask for it. COND_EXPR
2237 also does contextual conversion to bool for the first operand, but we
2238 handle that in build_conditional_expr, and type1 here is operand 2. */
2239 if (type1 != boolean_type_node)
2240 flags |= LOOKUP_ONLYCONVERTING;
2242 for (i = 0; i < 2; ++i)
2244 if (! args[i])
2245 break;
2247 t = implicit_conversion (types[i], argtypes[i], args[i],
2248 /*c_cast_p=*/false, flags, complain);
2249 if (! t)
2251 viable = 0;
2252 /* We need something for printing the candidate. */
2253 t = build_identity_conv (types[i], NULL_TREE);
2254 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2255 types[i]);
2257 else if (t->bad_p)
2259 viable = 0;
2260 reason = bad_arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2261 types[i]);
2263 convs[i] = t;
2266 /* For COND_EXPR we rearranged the arguments; undo that now. */
2267 if (args[2])
2269 convs[2] = convs[1];
2270 convs[1] = convs[0];
2271 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2272 /*c_cast_p=*/false, flags,
2273 complain);
2274 if (t)
2275 convs[0] = t;
2276 else
2278 viable = 0;
2279 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2280 boolean_type_node);
2284 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2285 num_convs, convs,
2286 /*access_path=*/NULL_TREE,
2287 /*conversion_path=*/NULL_TREE,
2288 viable, reason);
2291 static bool
2292 is_complete (tree t)
2294 return COMPLETE_TYPE_P (complete_type (t));
2297 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2299 static bool
2300 promoted_arithmetic_type_p (tree type)
2302 /* [over.built]
2304 In this section, the term promoted integral type is used to refer
2305 to those integral types which are preserved by integral promotion
2306 (including e.g. int and long but excluding e.g. char).
2307 Similarly, the term promoted arithmetic type refers to promoted
2308 integral types plus floating types. */
2309 return ((CP_INTEGRAL_TYPE_P (type)
2310 && same_type_p (type_promotes_to (type), type))
2311 || TREE_CODE (type) == REAL_TYPE);
2314 /* Create any builtin operator overload candidates for the operator in
2315 question given the converted operand types TYPE1 and TYPE2. The other
2316 args are passed through from add_builtin_candidates to
2317 build_builtin_candidate.
2319 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2320 If CODE is requires candidates operands of the same type of the kind
2321 of which TYPE1 and TYPE2 are, we add both candidates
2322 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2324 static void
2325 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2326 enum tree_code code2, tree fnname, tree type1,
2327 tree type2, tree *args, tree *argtypes, int flags,
2328 tsubst_flags_t complain)
2330 switch (code)
2332 case POSTINCREMENT_EXPR:
2333 case POSTDECREMENT_EXPR:
2334 args[1] = integer_zero_node;
2335 type2 = integer_type_node;
2336 break;
2337 default:
2338 break;
2341 switch (code)
2344 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2345 and VQ is either volatile or empty, there exist candidate operator
2346 functions of the form
2347 VQ T& operator++(VQ T&);
2348 T operator++(VQ T&, int);
2349 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
2350 type other than bool, and VQ is either volatile or empty, there exist
2351 candidate operator functions of the form
2352 VQ T& operator--(VQ T&);
2353 T operator--(VQ T&, int);
2354 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
2355 complete object type, and VQ is either volatile or empty, there exist
2356 candidate operator functions of the form
2357 T*VQ& operator++(T*VQ&);
2358 T*VQ& operator--(T*VQ&);
2359 T* operator++(T*VQ&, int);
2360 T* operator--(T*VQ&, int); */
2362 case POSTDECREMENT_EXPR:
2363 case PREDECREMENT_EXPR:
2364 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2365 return;
2366 case POSTINCREMENT_EXPR:
2367 case PREINCREMENT_EXPR:
2368 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2370 type1 = build_reference_type (type1);
2371 break;
2373 return;
2375 /* 7 For every cv-qualified or cv-unqualified object type T, there
2376 exist candidate operator functions of the form
2378 T& operator*(T*);
2380 8 For every function type T, there exist candidate operator functions of
2381 the form
2382 T& operator*(T*); */
2384 case INDIRECT_REF:
2385 if (TYPE_PTR_P (type1)
2386 && (TYPE_PTROB_P (type1)
2387 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2388 break;
2389 return;
2391 /* 9 For every type T, there exist candidate operator functions of the form
2392 T* operator+(T*);
2394 10For every promoted arithmetic type T, there exist candidate operator
2395 functions of the form
2396 T operator+(T);
2397 T operator-(T); */
2399 case UNARY_PLUS_EXPR: /* unary + */
2400 if (TYPE_PTR_P (type1))
2401 break;
2402 case NEGATE_EXPR:
2403 if (ARITHMETIC_TYPE_P (type1))
2404 break;
2405 return;
2407 /* 11For every promoted integral type T, there exist candidate operator
2408 functions of the form
2409 T operator~(T); */
2411 case BIT_NOT_EXPR:
2412 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2413 break;
2414 return;
2416 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2417 is the same type as C2 or is a derived class of C2, T is a complete
2418 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2419 there exist candidate operator functions of the form
2420 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2421 where CV12 is the union of CV1 and CV2. */
2423 case MEMBER_REF:
2424 if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2426 tree c1 = TREE_TYPE (type1);
2427 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2429 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2430 && (TYPE_PTRMEMFUNC_P (type2)
2431 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2432 break;
2434 return;
2436 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2437 didate operator functions of the form
2438 LR operator*(L, R);
2439 LR operator/(L, R);
2440 LR operator+(L, R);
2441 LR operator-(L, R);
2442 bool operator<(L, R);
2443 bool operator>(L, R);
2444 bool operator<=(L, R);
2445 bool operator>=(L, R);
2446 bool operator==(L, R);
2447 bool operator!=(L, R);
2448 where LR is the result of the usual arithmetic conversions between
2449 types L and R.
2451 14For every pair of types T and I, where T is a cv-qualified or cv-
2452 unqualified complete object type and I is a promoted integral type,
2453 there exist candidate operator functions of the form
2454 T* operator+(T*, I);
2455 T& operator[](T*, I);
2456 T* operator-(T*, I);
2457 T* operator+(I, T*);
2458 T& operator[](I, T*);
2460 15For every T, where T is a pointer to complete object type, there exist
2461 candidate operator functions of the form112)
2462 ptrdiff_t operator-(T, T);
2464 16For every pointer or enumeration type T, there exist candidate operator
2465 functions of the form
2466 bool operator<(T, T);
2467 bool operator>(T, T);
2468 bool operator<=(T, T);
2469 bool operator>=(T, T);
2470 bool operator==(T, T);
2471 bool operator!=(T, T);
2473 17For every pointer to member type T, there exist candidate operator
2474 functions of the form
2475 bool operator==(T, T);
2476 bool operator!=(T, T); */
2478 case MINUS_EXPR:
2479 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2480 break;
2481 if (TYPE_PTROB_P (type1)
2482 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2484 type2 = ptrdiff_type_node;
2485 break;
2487 case MULT_EXPR:
2488 case TRUNC_DIV_EXPR:
2489 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2490 break;
2491 return;
2493 case EQ_EXPR:
2494 case NE_EXPR:
2495 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2496 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
2497 break;
2498 if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
2500 type2 = type1;
2501 break;
2503 if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
2505 type1 = type2;
2506 break;
2508 /* Fall through. */
2509 case LT_EXPR:
2510 case GT_EXPR:
2511 case LE_EXPR:
2512 case GE_EXPR:
2513 case MAX_EXPR:
2514 case MIN_EXPR:
2515 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2516 break;
2517 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2518 break;
2519 if (TREE_CODE (type1) == ENUMERAL_TYPE
2520 && TREE_CODE (type2) == ENUMERAL_TYPE)
2521 break;
2522 if (TYPE_PTR_P (type1)
2523 && null_ptr_cst_p (args[1]))
2525 type2 = type1;
2526 break;
2528 if (null_ptr_cst_p (args[0])
2529 && TYPE_PTR_P (type2))
2531 type1 = type2;
2532 break;
2534 return;
2536 case PLUS_EXPR:
2537 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2538 break;
2539 case ARRAY_REF:
2540 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2542 type1 = ptrdiff_type_node;
2543 break;
2545 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2547 type2 = ptrdiff_type_node;
2548 break;
2550 return;
2552 /* 18For every pair of promoted integral types L and R, there exist candi-
2553 date operator functions of the form
2554 LR operator%(L, R);
2555 LR operator&(L, R);
2556 LR operator^(L, R);
2557 LR operator|(L, R);
2558 L operator<<(L, R);
2559 L operator>>(L, R);
2560 where LR is the result of the usual arithmetic conversions between
2561 types L and R. */
2563 case TRUNC_MOD_EXPR:
2564 case BIT_AND_EXPR:
2565 case BIT_IOR_EXPR:
2566 case BIT_XOR_EXPR:
2567 case LSHIFT_EXPR:
2568 case RSHIFT_EXPR:
2569 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2570 break;
2571 return;
2573 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2574 type, VQ is either volatile or empty, and R is a promoted arithmetic
2575 type, there exist candidate operator functions of the form
2576 VQ L& operator=(VQ L&, R);
2577 VQ L& operator*=(VQ L&, R);
2578 VQ L& operator/=(VQ L&, R);
2579 VQ L& operator+=(VQ L&, R);
2580 VQ L& operator-=(VQ L&, R);
2582 20For every pair T, VQ), where T is any type and VQ is either volatile
2583 or empty, there exist candidate operator functions of the form
2584 T*VQ& operator=(T*VQ&, T*);
2586 21For every pair T, VQ), where T is a pointer to member type and VQ is
2587 either volatile or empty, there exist candidate operator functions of
2588 the form
2589 VQ T& operator=(VQ T&, T);
2591 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2592 unqualified complete object type, VQ is either volatile or empty, and
2593 I is a promoted integral type, there exist candidate operator func-
2594 tions of the form
2595 T*VQ& operator+=(T*VQ&, I);
2596 T*VQ& operator-=(T*VQ&, I);
2598 23For every triple L, VQ, R), where L is an integral or enumeration
2599 type, VQ is either volatile or empty, and R is a promoted integral
2600 type, there exist candidate operator functions of the form
2602 VQ L& operator%=(VQ L&, R);
2603 VQ L& operator<<=(VQ L&, R);
2604 VQ L& operator>>=(VQ L&, R);
2605 VQ L& operator&=(VQ L&, R);
2606 VQ L& operator^=(VQ L&, R);
2607 VQ L& operator|=(VQ L&, R); */
2609 case MODIFY_EXPR:
2610 switch (code2)
2612 case PLUS_EXPR:
2613 case MINUS_EXPR:
2614 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2616 type2 = ptrdiff_type_node;
2617 break;
2619 case MULT_EXPR:
2620 case TRUNC_DIV_EXPR:
2621 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2622 break;
2623 return;
2625 case TRUNC_MOD_EXPR:
2626 case BIT_AND_EXPR:
2627 case BIT_IOR_EXPR:
2628 case BIT_XOR_EXPR:
2629 case LSHIFT_EXPR:
2630 case RSHIFT_EXPR:
2631 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2632 break;
2633 return;
2635 case NOP_EXPR:
2636 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2637 break;
2638 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2639 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2640 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2641 || ((TYPE_PTRMEMFUNC_P (type1)
2642 || TYPE_PTR_P (type1))
2643 && null_ptr_cst_p (args[1])))
2645 type2 = type1;
2646 break;
2648 return;
2650 default:
2651 gcc_unreachable ();
2653 type1 = build_reference_type (type1);
2654 break;
2656 case COND_EXPR:
2657 /* [over.built]
2659 For every pair of promoted arithmetic types L and R, there
2660 exist candidate operator functions of the form
2662 LR operator?(bool, L, R);
2664 where LR is the result of the usual arithmetic conversions
2665 between types L and R.
2667 For every type T, where T is a pointer or pointer-to-member
2668 type, there exist candidate operator functions of the form T
2669 operator?(bool, T, T); */
2671 if (promoted_arithmetic_type_p (type1)
2672 && promoted_arithmetic_type_p (type2))
2673 /* That's OK. */
2674 break;
2676 /* Otherwise, the types should be pointers. */
2677 if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
2678 return;
2680 /* We don't check that the two types are the same; the logic
2681 below will actually create two candidates; one in which both
2682 parameter types are TYPE1, and one in which both parameter
2683 types are TYPE2. */
2684 break;
2686 case REALPART_EXPR:
2687 case IMAGPART_EXPR:
2688 if (ARITHMETIC_TYPE_P (type1))
2689 break;
2690 return;
2692 default:
2693 gcc_unreachable ();
2696 /* Make sure we don't create builtin candidates with dependent types. */
2697 bool u1 = uses_template_parms (type1);
2698 bool u2 = type2 ? uses_template_parms (type2) : false;
2699 if (u1 || u2)
2701 /* Try to recover if one of the types is non-dependent. But if
2702 there's only one type, there's nothing we can do. */
2703 if (!type2)
2704 return;
2705 /* And we lose if both are dependent. */
2706 if (u1 && u2)
2707 return;
2708 /* Or if they have different forms. */
2709 if (TREE_CODE (type1) != TREE_CODE (type2))
2710 return;
2712 if (u1 && !u2)
2713 type1 = type2;
2714 else if (u2 && !u1)
2715 type2 = type1;
2718 /* If we're dealing with two pointer types or two enumeral types,
2719 we need candidates for both of them. */
2720 if (type2 && !same_type_p (type1, type2)
2721 && TREE_CODE (type1) == TREE_CODE (type2)
2722 && (TREE_CODE (type1) == REFERENCE_TYPE
2723 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2724 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2725 || TYPE_PTRMEMFUNC_P (type1)
2726 || MAYBE_CLASS_TYPE_P (type1)
2727 || TREE_CODE (type1) == ENUMERAL_TYPE))
2729 if (TYPE_PTR_OR_PTRMEM_P (type1))
2731 tree cptype = composite_pointer_type (type1, type2,
2732 error_mark_node,
2733 error_mark_node,
2734 CPO_CONVERSION,
2735 tf_none);
2736 if (cptype != error_mark_node)
2738 build_builtin_candidate
2739 (candidates, fnname, cptype, cptype, args, argtypes,
2740 flags, complain);
2741 return;
2745 build_builtin_candidate
2746 (candidates, fnname, type1, type1, args, argtypes, flags, complain);
2747 build_builtin_candidate
2748 (candidates, fnname, type2, type2, args, argtypes, flags, complain);
2749 return;
2752 build_builtin_candidate
2753 (candidates, fnname, type1, type2, args, argtypes, flags, complain);
2756 tree
2757 type_decays_to (tree type)
2759 if (TREE_CODE (type) == ARRAY_TYPE)
2760 return build_pointer_type (TREE_TYPE (type));
2761 if (TREE_CODE (type) == FUNCTION_TYPE)
2762 return build_pointer_type (type);
2763 return type;
2766 /* There are three conditions of builtin candidates:
2768 1) bool-taking candidates. These are the same regardless of the input.
2769 2) pointer-pair taking candidates. These are generated for each type
2770 one of the input types converts to.
2771 3) arithmetic candidates. According to the standard, we should generate
2772 all of these, but I'm trying not to...
2774 Here we generate a superset of the possible candidates for this particular
2775 case. That is a subset of the full set the standard defines, plus some
2776 other cases which the standard disallows. add_builtin_candidate will
2777 filter out the invalid set. */
2779 static void
2780 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2781 enum tree_code code2, tree fnname, tree *args,
2782 int flags, tsubst_flags_t complain)
2784 int ref1, i;
2785 int enum_p = 0;
2786 tree type, argtypes[3], t;
2787 /* TYPES[i] is the set of possible builtin-operator parameter types
2788 we will consider for the Ith argument. */
2789 vec<tree, va_gc> *types[2];
2790 unsigned ix;
2792 for (i = 0; i < 3; ++i)
2794 if (args[i])
2795 argtypes[i] = unlowered_expr_type (args[i]);
2796 else
2797 argtypes[i] = NULL_TREE;
2800 switch (code)
2802 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2803 and VQ is either volatile or empty, there exist candidate operator
2804 functions of the form
2805 VQ T& operator++(VQ T&); */
2807 case POSTINCREMENT_EXPR:
2808 case PREINCREMENT_EXPR:
2809 case POSTDECREMENT_EXPR:
2810 case PREDECREMENT_EXPR:
2811 case MODIFY_EXPR:
2812 ref1 = 1;
2813 break;
2815 /* 24There also exist candidate operator functions of the form
2816 bool operator!(bool);
2817 bool operator&&(bool, bool);
2818 bool operator||(bool, bool); */
2820 case TRUTH_NOT_EXPR:
2821 build_builtin_candidate
2822 (candidates, fnname, boolean_type_node,
2823 NULL_TREE, args, argtypes, flags, complain);
2824 return;
2826 case TRUTH_ORIF_EXPR:
2827 case TRUTH_ANDIF_EXPR:
2828 build_builtin_candidate
2829 (candidates, fnname, boolean_type_node,
2830 boolean_type_node, args, argtypes, flags, complain);
2831 return;
2833 case ADDR_EXPR:
2834 case COMPOUND_EXPR:
2835 case COMPONENT_REF:
2836 return;
2838 case COND_EXPR:
2839 case EQ_EXPR:
2840 case NE_EXPR:
2841 case LT_EXPR:
2842 case LE_EXPR:
2843 case GT_EXPR:
2844 case GE_EXPR:
2845 enum_p = 1;
2846 /* Fall through. */
2848 default:
2849 ref1 = 0;
2852 types[0] = make_tree_vector ();
2853 types[1] = make_tree_vector ();
2855 for (i = 0; i < 2; ++i)
2857 if (! args[i])
2859 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
2861 tree convs;
2863 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2864 return;
2866 convs = lookup_conversions (argtypes[i]);
2868 if (code == COND_EXPR)
2870 if (real_lvalue_p (args[i]))
2871 vec_safe_push (types[i], build_reference_type (argtypes[i]));
2873 vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
2876 else if (! convs)
2877 return;
2879 for (; convs; convs = TREE_CHAIN (convs))
2881 type = TREE_TYPE (convs);
2883 if (i == 0 && ref1
2884 && (TREE_CODE (type) != REFERENCE_TYPE
2885 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2886 continue;
2888 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2889 vec_safe_push (types[i], type);
2891 type = non_reference (type);
2892 if (i != 0 || ! ref1)
2894 type = cv_unqualified (type_decays_to (type));
2895 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2896 vec_safe_push (types[i], type);
2897 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2898 type = type_promotes_to (type);
2901 if (! vec_member (type, types[i]))
2902 vec_safe_push (types[i], type);
2905 else
2907 if (code == COND_EXPR && real_lvalue_p (args[i]))
2908 vec_safe_push (types[i], build_reference_type (argtypes[i]));
2909 type = non_reference (argtypes[i]);
2910 if (i != 0 || ! ref1)
2912 type = cv_unqualified (type_decays_to (type));
2913 if (enum_p && UNSCOPED_ENUM_P (type))
2914 vec_safe_push (types[i], type);
2915 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2916 type = type_promotes_to (type);
2918 vec_safe_push (types[i], type);
2922 /* Run through the possible parameter types of both arguments,
2923 creating candidates with those parameter types. */
2924 FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
2926 unsigned jx;
2927 tree u;
2929 if (!types[1]->is_empty ())
2930 FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
2931 add_builtin_candidate
2932 (candidates, code, code2, fnname, t,
2933 u, args, argtypes, flags, complain);
2934 else
2935 add_builtin_candidate
2936 (candidates, code, code2, fnname, t,
2937 NULL_TREE, args, argtypes, flags, complain);
2940 release_tree_vector (types[0]);
2941 release_tree_vector (types[1]);
2945 /* If TMPL can be successfully instantiated as indicated by
2946 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2948 TMPL is the template. EXPLICIT_TARGS are any explicit template
2949 arguments. ARGLIST is the arguments provided at the call-site.
2950 This does not change ARGLIST. The RETURN_TYPE is the desired type
2951 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
2952 as for add_function_candidate. If an OBJ is supplied, FLAGS and
2953 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
2955 static struct z_candidate*
2956 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2957 tree ctype, tree explicit_targs, tree first_arg,
2958 const vec<tree, va_gc> *arglist, tree return_type,
2959 tree access_path, tree conversion_path,
2960 int flags, tree obj, unification_kind_t strict,
2961 tsubst_flags_t complain)
2963 int ntparms = DECL_NTPARMS (tmpl);
2964 tree targs = make_tree_vec (ntparms);
2965 unsigned int len = vec_safe_length (arglist);
2966 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
2967 unsigned int skip_without_in_chrg = 0;
2968 tree first_arg_without_in_chrg = first_arg;
2969 tree *args_without_in_chrg;
2970 unsigned int nargs_without_in_chrg;
2971 unsigned int ia, ix;
2972 tree arg;
2973 struct z_candidate *cand;
2974 tree fn;
2975 struct rejection_reason *reason = NULL;
2976 int errs;
2978 /* We don't do deduction on the in-charge parameter, the VTT
2979 parameter or 'this'. */
2980 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2982 if (first_arg_without_in_chrg != NULL_TREE)
2983 first_arg_without_in_chrg = NULL_TREE;
2984 else
2985 ++skip_without_in_chrg;
2988 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2989 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2990 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
2992 if (first_arg_without_in_chrg != NULL_TREE)
2993 first_arg_without_in_chrg = NULL_TREE;
2994 else
2995 ++skip_without_in_chrg;
2998 if (len < skip_without_in_chrg)
2999 return NULL;
3001 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3002 + (len - skip_without_in_chrg));
3003 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3004 ia = 0;
3005 if (first_arg_without_in_chrg != NULL_TREE)
3007 args_without_in_chrg[ia] = first_arg_without_in_chrg;
3008 ++ia;
3010 for (ix = skip_without_in_chrg;
3011 vec_safe_iterate (arglist, ix, &arg);
3012 ++ix)
3014 args_without_in_chrg[ia] = arg;
3015 ++ia;
3017 gcc_assert (ia == nargs_without_in_chrg);
3019 errs = errorcount+sorrycount;
3020 fn = fn_type_unification (tmpl, explicit_targs, targs,
3021 args_without_in_chrg,
3022 nargs_without_in_chrg,
3023 return_type, strict, flags, false,
3024 complain & tf_decltype);
3026 if (fn == error_mark_node)
3028 if (errorcount+sorrycount == errs)
3029 /* Don't repeat unification later if it already resulted in errors. */
3030 reason = template_unification_rejection (tmpl, explicit_targs,
3031 targs, args_without_in_chrg,
3032 nargs_without_in_chrg,
3033 return_type, strict, flags);
3034 else
3035 reason = template_unification_error_rejection ();
3036 goto fail;
3039 /* In [class.copy]:
3041 A member function template is never instantiated to perform the
3042 copy of a class object to an object of its class type.
3044 It's a little unclear what this means; the standard explicitly
3045 does allow a template to be used to copy a class. For example,
3048 struct A {
3049 A(A&);
3050 template <class T> A(const T&);
3052 const A f ();
3053 void g () { A a (f ()); }
3055 the member template will be used to make the copy. The section
3056 quoted above appears in the paragraph that forbids constructors
3057 whose only parameter is (a possibly cv-qualified variant of) the
3058 class type, and a logical interpretation is that the intent was
3059 to forbid the instantiation of member templates which would then
3060 have that form. */
3061 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3063 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3064 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3065 ctype))
3067 reason = invalid_copy_with_fn_template_rejection ();
3068 goto fail;
3072 if (obj != NULL_TREE)
3073 /* Aha, this is a conversion function. */
3074 cand = add_conv_candidate (candidates, fn, obj, first_arg, arglist,
3075 access_path, conversion_path, complain);
3076 else
3077 cand = add_function_candidate (candidates, fn, ctype,
3078 first_arg, arglist, access_path,
3079 conversion_path, flags, complain);
3080 if (DECL_TI_TEMPLATE (fn) != tmpl)
3082 /* This situation can occur if a member template of a template
3083 class is specialized. Then, instantiate_template might return
3084 an instantiation of the specialization, in which case the
3085 DECL_TI_TEMPLATE field will point at the original
3086 specialization. For example:
3088 template <class T> struct S { template <class U> void f(U);
3089 template <> void f(int) {}; };
3090 S<double> sd;
3091 sd.f(3);
3093 Here, TMPL will be template <class U> S<double>::f(U).
3094 And, instantiate template will give us the specialization
3095 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3096 for this will point at template <class T> template <> S<T>::f(int),
3097 so that we can find the definition. For the purposes of
3098 overload resolution, however, we want the original TMPL. */
3099 cand->template_decl = build_template_info (tmpl, targs);
3101 else
3102 cand->template_decl = DECL_TEMPLATE_INFO (fn);
3103 cand->explicit_targs = explicit_targs;
3105 return cand;
3106 fail:
3107 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
3108 access_path, conversion_path, 0, reason);
3112 static struct z_candidate *
3113 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3114 tree explicit_targs, tree first_arg,
3115 const vec<tree, va_gc> *arglist, tree return_type,
3116 tree access_path, tree conversion_path, int flags,
3117 unification_kind_t strict, tsubst_flags_t complain)
3119 return
3120 add_template_candidate_real (candidates, tmpl, ctype,
3121 explicit_targs, first_arg, arglist,
3122 return_type, access_path, conversion_path,
3123 flags, NULL_TREE, strict, complain);
3127 static struct z_candidate *
3128 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3129 tree obj, tree first_arg,
3130 const vec<tree, va_gc> *arglist,
3131 tree return_type, tree access_path,
3132 tree conversion_path, tsubst_flags_t complain)
3134 return
3135 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3136 first_arg, arglist, return_type, access_path,
3137 conversion_path, 0, obj, DEDUCE_CONV,
3138 complain);
3141 /* The CANDS are the set of candidates that were considered for
3142 overload resolution. Return the set of viable candidates, or CANDS
3143 if none are viable. If any of the candidates were viable, set
3144 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3145 considered viable only if it is strictly viable. */
3147 static struct z_candidate*
3148 splice_viable (struct z_candidate *cands,
3149 bool strict_p,
3150 bool *any_viable_p)
3152 struct z_candidate *viable;
3153 struct z_candidate **last_viable;
3154 struct z_candidate **cand;
3156 /* Be strict inside templates, since build_over_call won't actually
3157 do the conversions to get pedwarns. */
3158 if (processing_template_decl)
3159 strict_p = true;
3161 viable = NULL;
3162 last_viable = &viable;
3163 *any_viable_p = false;
3165 cand = &cands;
3166 while (*cand)
3168 struct z_candidate *c = *cand;
3169 if (strict_p ? c->viable == 1 : c->viable)
3171 *last_viable = c;
3172 *cand = c->next;
3173 c->next = NULL;
3174 last_viable = &c->next;
3175 *any_viable_p = true;
3177 else
3178 cand = &c->next;
3181 return viable ? viable : cands;
3184 static bool
3185 any_strictly_viable (struct z_candidate *cands)
3187 for (; cands; cands = cands->next)
3188 if (cands->viable == 1)
3189 return true;
3190 return false;
3193 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3194 words, it is about to become the "this" pointer for a member
3195 function call. Take the address of the object. */
3197 static tree
3198 build_this (tree obj)
3200 /* In a template, we are only concerned about the type of the
3201 expression, so we can take a shortcut. */
3202 if (processing_template_decl)
3203 return build_address (obj);
3205 return cp_build_addr_expr (obj, tf_warning_or_error);
3208 /* Returns true iff functions are equivalent. Equivalent functions are
3209 not '==' only if one is a function-local extern function or if
3210 both are extern "C". */
3212 static inline int
3213 equal_functions (tree fn1, tree fn2)
3215 if (TREE_CODE (fn1) != TREE_CODE (fn2))
3216 return 0;
3217 if (TREE_CODE (fn1) == TEMPLATE_DECL)
3218 return fn1 == fn2;
3219 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
3220 || DECL_EXTERN_C_FUNCTION_P (fn1))
3221 return decls_match (fn1, fn2);
3222 return fn1 == fn2;
3225 /* Print information about a candidate being rejected due to INFO. */
3227 static void
3228 print_conversion_rejection (location_t loc, struct conversion_info *info)
3230 if (info->n_arg == -1)
3231 /* Conversion of implicit `this' argument failed. */
3232 inform (loc, " no known conversion for implicit "
3233 "%<this%> parameter from %qT to %qT",
3234 info->from_type, info->to_type);
3235 else if (info->n_arg == -2)
3236 /* Conversion of conversion function return value failed. */
3237 inform (loc, " no known conversion from %qT to %qT",
3238 info->from_type, info->to_type);
3239 else
3240 inform (loc, " no known conversion for argument %d from %qT to %qT",
3241 info->n_arg+1, info->from_type, info->to_type);
3244 /* Print information about a candidate with WANT parameters and we found
3245 HAVE. */
3247 static void
3248 print_arity_information (location_t loc, unsigned int have, unsigned int want)
3250 inform_n (loc, want,
3251 " candidate expects %d argument, %d provided",
3252 " candidate expects %d arguments, %d provided",
3253 want, have);
3256 /* Print information about one overload candidate CANDIDATE. MSGSTR
3257 is the text to print before the candidate itself.
3259 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3260 to have been run through gettext by the caller. This wart makes
3261 life simpler in print_z_candidates and for the translators. */
3263 static void
3264 print_z_candidate (location_t loc, const char *msgstr,
3265 struct z_candidate *candidate)
3267 const char *msg = (msgstr == NULL
3268 ? ""
3269 : ACONCAT ((msgstr, " ", NULL)));
3270 location_t cloc = location_of (candidate->fn);
3272 if (identifier_p (candidate->fn))
3274 cloc = loc;
3275 if (candidate->num_convs == 3)
3276 inform (cloc, "%s%D(%T, %T, %T) <built-in>", msg, candidate->fn,
3277 candidate->convs[0]->type,
3278 candidate->convs[1]->type,
3279 candidate->convs[2]->type);
3280 else if (candidate->num_convs == 2)
3281 inform (cloc, "%s%D(%T, %T) <built-in>", msg, candidate->fn,
3282 candidate->convs[0]->type,
3283 candidate->convs[1]->type);
3284 else
3285 inform (cloc, "%s%D(%T) <built-in>", msg, candidate->fn,
3286 candidate->convs[0]->type);
3288 else if (TYPE_P (candidate->fn))
3289 inform (cloc, "%s%T <conversion>", msg, candidate->fn);
3290 else if (candidate->viable == -1)
3291 inform (cloc, "%s%#D <near match>", msg, candidate->fn);
3292 else if (DECL_DELETED_FN (candidate->fn))
3293 inform (cloc, "%s%#D <deleted>", msg, candidate->fn);
3294 else
3295 inform (cloc, "%s%#D", msg, candidate->fn);
3296 /* Give the user some information about why this candidate failed. */
3297 if (candidate->reason != NULL)
3299 struct rejection_reason *r = candidate->reason;
3301 switch (r->code)
3303 case rr_arity:
3304 print_arity_information (cloc, r->u.arity.actual,
3305 r->u.arity.expected);
3306 break;
3307 case rr_arg_conversion:
3308 print_conversion_rejection (cloc, &r->u.conversion);
3309 break;
3310 case rr_bad_arg_conversion:
3311 print_conversion_rejection (cloc, &r->u.bad_conversion);
3312 break;
3313 case rr_explicit_conversion:
3314 inform (cloc, " return type %qT of explicit conversion function "
3315 "cannot be converted to %qT with a qualification "
3316 "conversion", r->u.conversion.from_type,
3317 r->u.conversion.to_type);
3318 break;
3319 case rr_template_conversion:
3320 inform (cloc, " conversion from return type %qT of template "
3321 "conversion function specialization to %qT is not an "
3322 "exact match", r->u.conversion.from_type,
3323 r->u.conversion.to_type);
3324 break;
3325 case rr_template_unification:
3326 /* We use template_unification_error_rejection if unification caused
3327 actual non-SFINAE errors, in which case we don't need to repeat
3328 them here. */
3329 if (r->u.template_unification.tmpl == NULL_TREE)
3331 inform (cloc, " substitution of deduced template arguments "
3332 "resulted in errors seen above");
3333 break;
3335 /* Re-run template unification with diagnostics. */
3336 inform (cloc, " template argument deduction/substitution failed:");
3337 fn_type_unification (r->u.template_unification.tmpl,
3338 r->u.template_unification.explicit_targs,
3339 (make_tree_vec
3340 (r->u.template_unification.num_targs)),
3341 r->u.template_unification.args,
3342 r->u.template_unification.nargs,
3343 r->u.template_unification.return_type,
3344 r->u.template_unification.strict,
3345 r->u.template_unification.flags,
3346 true, false);
3347 break;
3348 case rr_invalid_copy:
3349 inform (cloc,
3350 " a constructor taking a single argument of its own "
3351 "class type is invalid");
3352 break;
3353 case rr_constraint_failure:
3355 tree tmpl = r->u.template_instantiation.tmpl;
3356 tree args = r->u.template_instantiation.targs;
3357 diagnose_constraints (cloc, tmpl, args);
3359 break;
3360 case rr_none:
3361 default:
3362 /* This candidate didn't have any issues or we failed to
3363 handle a particular code. Either way... */
3364 gcc_unreachable ();
3369 static void
3370 print_z_candidates (location_t loc, struct z_candidate *candidates)
3372 struct z_candidate *cand1;
3373 struct z_candidate **cand2;
3374 int n_candidates;
3376 if (!candidates)
3377 return;
3379 /* Remove non-viable deleted candidates. */
3380 cand1 = candidates;
3381 for (cand2 = &cand1; *cand2; )
3383 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
3384 && !(*cand2)->viable
3385 && DECL_DELETED_FN ((*cand2)->fn))
3386 *cand2 = (*cand2)->next;
3387 else
3388 cand2 = &(*cand2)->next;
3390 /* ...if there are any non-deleted ones. */
3391 if (cand1)
3392 candidates = cand1;
3394 /* There may be duplicates in the set of candidates. We put off
3395 checking this condition as long as possible, since we have no way
3396 to eliminate duplicates from a set of functions in less than n^2
3397 time. Now we are about to emit an error message, so it is more
3398 permissible to go slowly. */
3399 for (cand1 = candidates; cand1; cand1 = cand1->next)
3401 tree fn = cand1->fn;
3402 /* Skip builtin candidates and conversion functions. */
3403 if (!DECL_P (fn))
3404 continue;
3405 cand2 = &cand1->next;
3406 while (*cand2)
3408 if (DECL_P ((*cand2)->fn)
3409 && equal_functions (fn, (*cand2)->fn))
3410 *cand2 = (*cand2)->next;
3411 else
3412 cand2 = &(*cand2)->next;
3416 for (n_candidates = 0, cand1 = candidates; cand1; cand1 = cand1->next)
3417 n_candidates++;
3419 inform_n (loc, n_candidates, "candidate is:", "candidates are:");
3420 for (; candidates; candidates = candidates->next)
3421 print_z_candidate (loc, NULL, candidates);
3424 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3425 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3426 the result of the conversion function to convert it to the final
3427 desired type. Merge the two sequences into a single sequence,
3428 and return the merged sequence. */
3430 static conversion *
3431 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
3433 conversion **t;
3434 bool bad = user_seq->bad_p;
3436 gcc_assert (user_seq->kind == ck_user);
3438 /* Find the end of the second conversion sequence. */
3439 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
3441 /* The entire sequence is a user-conversion sequence. */
3442 (*t)->user_conv_p = true;
3443 if (bad)
3444 (*t)->bad_p = true;
3447 /* Replace the identity conversion with the user conversion
3448 sequence. */
3449 *t = user_seq;
3451 return std_seq;
3454 /* Handle overload resolution for initializing an object of class type from
3455 an initializer list. First we look for a suitable constructor that
3456 takes a std::initializer_list; if we don't find one, we then look for a
3457 non-list constructor.
3459 Parameters are as for add_candidates, except that the arguments are in
3460 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
3461 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3463 static void
3464 add_list_candidates (tree fns, tree first_arg,
3465 tree init_list, tree totype,
3466 tree explicit_targs, bool template_only,
3467 tree conversion_path, tree access_path,
3468 int flags,
3469 struct z_candidate **candidates,
3470 tsubst_flags_t complain)
3472 vec<tree, va_gc> *args;
3474 gcc_assert (*candidates == NULL);
3476 /* We're looking for a ctor for list-initialization. */
3477 flags |= LOOKUP_LIST_INIT_CTOR;
3478 /* And we don't allow narrowing conversions. We also use this flag to
3479 avoid the copy constructor call for copy-list-initialization. */
3480 flags |= LOOKUP_NO_NARROWING;
3482 /* Always use the default constructor if the list is empty (DR 990). */
3483 if (CONSTRUCTOR_NELTS (init_list) == 0
3484 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
3486 /* If the class has a list ctor, try passing the list as a single
3487 argument first, but only consider list ctors. */
3488 else if (TYPE_HAS_LIST_CTOR (totype))
3490 flags |= LOOKUP_LIST_ONLY;
3491 args = make_tree_vector_single (init_list);
3492 add_candidates (fns, first_arg, args, NULL_TREE,
3493 explicit_targs, template_only, conversion_path,
3494 access_path, flags, candidates, complain);
3495 if (any_strictly_viable (*candidates))
3496 return;
3499 args = ctor_to_vec (init_list);
3501 /* We aren't looking for list-ctors anymore. */
3502 flags &= ~LOOKUP_LIST_ONLY;
3503 /* We allow more user-defined conversions within an init-list. */
3504 flags &= ~LOOKUP_NO_CONVERSION;
3506 add_candidates (fns, first_arg, args, NULL_TREE,
3507 explicit_targs, template_only, conversion_path,
3508 access_path, flags, candidates, complain);
3511 /* Returns the best overload candidate to perform the requested
3512 conversion. This function is used for three the overloading situations
3513 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3514 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3515 per [dcl.init.ref], so we ignore temporary bindings. */
3517 static struct z_candidate *
3518 build_user_type_conversion_1 (tree totype, tree expr, int flags,
3519 tsubst_flags_t complain)
3521 struct z_candidate *candidates, *cand;
3522 tree fromtype;
3523 tree ctors = NULL_TREE;
3524 tree conv_fns = NULL_TREE;
3525 conversion *conv = NULL;
3526 tree first_arg = NULL_TREE;
3527 vec<tree, va_gc> *args = NULL;
3528 bool any_viable_p;
3529 int convflags;
3531 if (!expr)
3532 return NULL;
3534 fromtype = TREE_TYPE (expr);
3536 /* We represent conversion within a hierarchy using RVALUE_CONV and
3537 BASE_CONV, as specified by [over.best.ics]; these become plain
3538 constructor calls, as specified in [dcl.init]. */
3539 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
3540 || !DERIVED_FROM_P (totype, fromtype));
3542 if (MAYBE_CLASS_TYPE_P (totype))
3543 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
3544 creating a garbage BASELINK; constructors can't be inherited. */
3545 ctors = lookup_fnfields_slot (totype, complete_ctor_identifier);
3547 if (MAYBE_CLASS_TYPE_P (fromtype))
3549 tree to_nonref = non_reference (totype);
3550 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
3551 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
3552 && DERIVED_FROM_P (to_nonref, fromtype)))
3554 /* [class.conv.fct] A conversion function is never used to
3555 convert a (possibly cv-qualified) object to the (possibly
3556 cv-qualified) same object type (or a reference to it), to a
3557 (possibly cv-qualified) base class of that type (or a
3558 reference to it)... */
3560 else
3561 conv_fns = lookup_conversions (fromtype);
3564 candidates = 0;
3565 flags |= LOOKUP_NO_CONVERSION;
3566 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3567 flags |= LOOKUP_NO_NARROWING;
3569 /* It's OK to bind a temporary for converting constructor arguments, but
3570 not in converting the return value of a conversion operator. */
3571 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION);
3572 flags &= ~LOOKUP_NO_TEMP_BIND;
3574 if (ctors)
3576 int ctorflags = flags;
3578 first_arg = build_int_cst (build_pointer_type (totype), 0);
3579 first_arg = build_fold_indirect_ref (first_arg);
3581 /* We should never try to call the abstract or base constructor
3582 from here. */
3583 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
3584 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)));
3586 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3588 /* List-initialization. */
3589 add_list_candidates (ctors, first_arg, expr, totype, NULL_TREE,
3590 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
3591 ctorflags, &candidates, complain);
3593 else
3595 args = make_tree_vector_single (expr);
3596 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
3597 TYPE_BINFO (totype), TYPE_BINFO (totype),
3598 ctorflags, &candidates, complain);
3601 for (cand = candidates; cand; cand = cand->next)
3603 cand->second_conv = build_identity_conv (totype, NULL_TREE);
3605 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
3606 set, then this is copy-initialization. In that case, "The
3607 result of the call is then used to direct-initialize the
3608 object that is the destination of the copy-initialization."
3609 [dcl.init]
3611 We represent this in the conversion sequence with an
3612 rvalue conversion, which means a constructor call. */
3613 if (TREE_CODE (totype) != REFERENCE_TYPE
3614 && !(convflags & LOOKUP_NO_TEMP_BIND))
3615 cand->second_conv
3616 = build_conv (ck_rvalue, totype, cand->second_conv);
3620 if (conv_fns)
3621 first_arg = expr;
3623 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
3625 tree conversion_path = TREE_PURPOSE (conv_fns);
3626 struct z_candidate *old_candidates;
3628 /* If we are called to convert to a reference type, we are trying to
3629 find a direct binding, so don't even consider temporaries. If
3630 we don't find a direct binding, the caller will try again to
3631 look for a temporary binding. */
3632 if (TREE_CODE (totype) == REFERENCE_TYPE)
3633 convflags |= LOOKUP_NO_TEMP_BIND;
3635 old_candidates = candidates;
3636 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3637 NULL_TREE, false,
3638 conversion_path, TYPE_BINFO (fromtype),
3639 flags, &candidates, complain);
3641 for (cand = candidates; cand != old_candidates; cand = cand->next)
3643 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
3644 conversion *ics
3645 = implicit_conversion (totype,
3646 rettype,
3648 /*c_cast_p=*/false, convflags,
3649 complain);
3651 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3652 copy-initialization. In that case, "The result of the
3653 call is then used to direct-initialize the object that is
3654 the destination of the copy-initialization." [dcl.init]
3656 We represent this in the conversion sequence with an
3657 rvalue conversion, which means a constructor call. But
3658 don't add a second rvalue conversion if there's already
3659 one there. Which there really shouldn't be, but it's
3660 harmless since we'd add it here anyway. */
3661 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3662 && !(convflags & LOOKUP_NO_TEMP_BIND))
3663 ics = build_conv (ck_rvalue, totype, ics);
3665 cand->second_conv = ics;
3667 if (!ics)
3669 cand->viable = 0;
3670 cand->reason = arg_conversion_rejection (NULL_TREE, -2,
3671 rettype, totype);
3673 else if (DECL_NONCONVERTING_P (cand->fn)
3674 && ics->rank > cr_exact)
3676 /* 13.3.1.5: For direct-initialization, those explicit
3677 conversion functions that are not hidden within S and
3678 yield type T or a type that can be converted to type T
3679 with a qualification conversion (4.4) are also candidate
3680 functions. */
3681 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
3682 I've raised this issue with the committee. --jason 9/2011 */
3683 cand->viable = -1;
3684 cand->reason = explicit_conversion_rejection (rettype, totype);
3686 else if (cand->viable == 1 && ics->bad_p)
3688 cand->viable = -1;
3689 cand->reason
3690 = bad_arg_conversion_rejection (NULL_TREE, -2,
3691 rettype, totype);
3693 else if (primary_template_instantiation_p (cand->fn)
3694 && ics->rank > cr_exact)
3696 /* 13.3.3.1.2: If the user-defined conversion is specified by
3697 a specialization of a conversion function template, the
3698 second standard conversion sequence shall have exact match
3699 rank. */
3700 cand->viable = -1;
3701 cand->reason = template_conversion_rejection (rettype, totype);
3706 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3707 if (!any_viable_p)
3709 if (args)
3710 release_tree_vector (args);
3711 return NULL;
3714 cand = tourney (candidates, complain);
3715 if (cand == 0)
3717 if (complain & tf_error)
3719 error ("conversion from %qT to %qT is ambiguous",
3720 fromtype, totype);
3721 print_z_candidates (location_of (expr), candidates);
3724 cand = candidates; /* any one will do */
3725 cand->second_conv = build_ambiguous_conv (totype, expr);
3726 cand->second_conv->user_conv_p = true;
3727 if (!any_strictly_viable (candidates))
3728 cand->second_conv->bad_p = true;
3729 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3730 ambiguous conversion is no worse than another user-defined
3731 conversion. */
3733 return cand;
3736 /* Build the user conversion sequence. */
3737 conv = build_conv
3738 (ck_user,
3739 (DECL_CONSTRUCTOR_P (cand->fn)
3740 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
3741 build_identity_conv (TREE_TYPE (expr), expr));
3742 conv->cand = cand;
3743 if (cand->viable == -1)
3744 conv->bad_p = true;
3746 /* Remember that this was a list-initialization. */
3747 if (flags & LOOKUP_NO_NARROWING)
3748 conv->check_narrowing = true;
3750 /* Combine it with the second conversion sequence. */
3751 cand->second_conv = merge_conversion_sequences (conv,
3752 cand->second_conv);
3754 return cand;
3757 /* Wrapper for above. */
3759 tree
3760 build_user_type_conversion (tree totype, tree expr, int flags,
3761 tsubst_flags_t complain)
3763 struct z_candidate *cand;
3764 tree ret;
3766 bool subtime = timevar_cond_start (TV_OVERLOAD);
3767 cand = build_user_type_conversion_1 (totype, expr, flags, complain);
3769 if (cand)
3771 if (cand->second_conv->kind == ck_ambig)
3772 ret = error_mark_node;
3773 else
3775 expr = convert_like (cand->second_conv, expr, complain);
3776 ret = convert_from_reference (expr);
3779 else
3780 ret = NULL_TREE;
3782 timevar_cond_stop (TV_OVERLOAD, subtime);
3783 return ret;
3786 /* Subroutine of convert_nontype_argument.
3788 EXPR is an argument for a template non-type parameter of integral or
3789 enumeration type. Do any necessary conversions (that are permitted for
3790 non-type arguments) to convert it to the parameter type.
3792 If conversion is successful, returns the converted expression;
3793 otherwise, returns error_mark_node. */
3795 tree
3796 build_integral_nontype_arg_conv (tree type, tree expr, tsubst_flags_t complain)
3798 conversion *conv;
3799 void *p;
3800 tree t;
3801 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
3803 if (error_operand_p (expr))
3804 return error_mark_node;
3806 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
3808 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3809 p = conversion_obstack_alloc (0);
3811 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
3812 /*c_cast_p=*/false,
3813 LOOKUP_IMPLICIT, complain);
3815 /* for a non-type template-parameter of integral or
3816 enumeration type, integral promotions (4.5) and integral
3817 conversions (4.7) are applied. */
3818 /* It should be sufficient to check the outermost conversion step, since
3819 there are no qualification conversions to integer type. */
3820 if (conv)
3821 switch (conv->kind)
3823 /* A conversion function is OK. If it isn't constexpr, we'll
3824 complain later that the argument isn't constant. */
3825 case ck_user:
3826 /* The lvalue-to-rvalue conversion is OK. */
3827 case ck_rvalue:
3828 case ck_identity:
3829 break;
3831 case ck_std:
3832 t = next_conversion (conv)->type;
3833 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t))
3834 break;
3836 if (complain & tf_error)
3837 error_at (loc, "conversion from %qT to %qT not considered for "
3838 "non-type template argument", t, type);
3839 /* and fall through. */
3841 default:
3842 conv = NULL;
3843 break;
3846 if (conv)
3847 expr = convert_like (conv, expr, complain);
3848 else
3849 expr = error_mark_node;
3851 /* Free all the conversions we allocated. */
3852 obstack_free (&conversion_obstack, p);
3854 return expr;
3857 /* Do any initial processing on the arguments to a function call. */
3859 static vec<tree, va_gc> *
3860 resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
3862 unsigned int ix;
3863 tree arg;
3865 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
3867 if (error_operand_p (arg))
3868 return NULL;
3869 else if (VOID_TYPE_P (TREE_TYPE (arg)))
3871 if (complain & tf_error)
3872 error ("invalid use of void expression");
3873 return NULL;
3875 else if (invalid_nonstatic_memfn_p (arg, complain))
3876 return NULL;
3878 return args;
3881 /* Perform overload resolution on FN, which is called with the ARGS.
3883 Return the candidate function selected by overload resolution, or
3884 NULL if the event that overload resolution failed. In the case
3885 that overload resolution fails, *CANDIDATES will be the set of
3886 candidates considered, and ANY_VIABLE_P will be set to true or
3887 false to indicate whether or not any of the candidates were
3888 viable.
3890 The ARGS should already have gone through RESOLVE_ARGS before this
3891 function is called. */
3893 static struct z_candidate *
3894 perform_overload_resolution (tree fn,
3895 const vec<tree, va_gc> *args,
3896 struct z_candidate **candidates,
3897 bool *any_viable_p, tsubst_flags_t complain)
3899 struct z_candidate *cand;
3900 tree explicit_targs;
3901 int template_only;
3903 bool subtime = timevar_cond_start (TV_OVERLOAD);
3905 explicit_targs = NULL_TREE;
3906 template_only = 0;
3908 *candidates = NULL;
3909 *any_viable_p = true;
3911 /* Check FN. */
3912 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
3913 || TREE_CODE (fn) == TEMPLATE_DECL
3914 || TREE_CODE (fn) == OVERLOAD
3915 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
3917 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3919 explicit_targs = TREE_OPERAND (fn, 1);
3920 fn = TREE_OPERAND (fn, 0);
3921 template_only = 1;
3924 /* Add the various candidate functions. */
3925 add_candidates (fn, NULL_TREE, args, NULL_TREE,
3926 explicit_targs, template_only,
3927 /*conversion_path=*/NULL_TREE,
3928 /*access_path=*/NULL_TREE,
3929 LOOKUP_NORMAL,
3930 candidates, complain);
3932 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
3933 if (*any_viable_p)
3934 cand = tourney (*candidates, complain);
3935 else
3936 cand = NULL;
3938 timevar_cond_stop (TV_OVERLOAD, subtime);
3939 return cand;
3942 /* Print an error message about being unable to build a call to FN with
3943 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
3944 be located; CANDIDATES is a possibly empty list of such
3945 functions. */
3947 static void
3948 print_error_for_call_failure (tree fn, vec<tree, va_gc> *args, bool any_viable_p,
3949 struct z_candidate *candidates)
3951 tree name = DECL_NAME (OVL_CURRENT (fn));
3952 location_t loc = location_of (name);
3954 if (!any_viable_p)
3955 error_at (loc, "no matching function for call to %<%D(%A)%>",
3956 name, build_tree_list_vec (args));
3957 else
3958 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
3959 name, build_tree_list_vec (args));
3960 if (candidates)
3961 print_z_candidates (loc, candidates);
3964 /* Return an expression for a call to FN (a namespace-scope function,
3965 or a static member function) with the ARGS. This may change
3966 ARGS. */
3968 tree
3969 build_new_function_call (tree fn, vec<tree, va_gc> **args, bool koenig_p,
3970 tsubst_flags_t complain)
3972 struct z_candidate *candidates, *cand;
3973 bool any_viable_p;
3974 void *p;
3975 tree result;
3977 if (args != NULL && *args != NULL)
3979 *args = resolve_args (*args, complain);
3980 if (*args == NULL)
3981 return error_mark_node;
3984 if (flag_tm)
3985 tm_malloc_replacement (fn);
3987 /* If this function was found without using argument dependent
3988 lookup, then we want to ignore any undeclared friend
3989 functions. */
3990 if (!koenig_p)
3992 tree orig_fn = fn;
3994 fn = remove_hidden_names (fn);
3995 if (!fn)
3997 if (complain & tf_error)
3998 print_error_for_call_failure (orig_fn, *args, false, NULL);
3999 return error_mark_node;
4003 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4004 p = conversion_obstack_alloc (0);
4006 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
4007 complain);
4009 if (!cand)
4011 if (complain & tf_error)
4013 if (!any_viable_p && candidates && ! candidates->next
4014 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
4015 return cp_build_function_call_vec (candidates->fn, args, complain);
4016 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4017 fn = TREE_OPERAND (fn, 0);
4018 print_error_for_call_failure (fn, *args, any_viable_p, candidates);
4020 result = error_mark_node;
4022 else
4024 int flags = LOOKUP_NORMAL;
4025 /* If fn is template_id_expr, the call has explicit template arguments
4026 (e.g. func<int>(5)), communicate this info to build_over_call
4027 through flags so that later we can use it to decide whether to warn
4028 about peculiar null pointer conversion. */
4029 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4030 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
4031 result = build_over_call (cand, flags, complain);
4034 /* Free all the conversions we allocated. */
4035 obstack_free (&conversion_obstack, p);
4037 return result;
4040 /* Build a call to a global operator new. FNNAME is the name of the
4041 operator (either "operator new" or "operator new[]") and ARGS are
4042 the arguments provided. This may change ARGS. *SIZE points to the
4043 total number of bytes required by the allocation, and is updated if
4044 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
4045 be used. If this function determines that no cookie should be
4046 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
4047 is not NULL_TREE, it is evaluated before calculating the final
4048 array size, and if it fails, the array size is replaced with
4049 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
4050 is non-NULL, it will be set, upon return, to the allocation
4051 function called. */
4053 tree
4054 build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
4055 tree *size, tree *cookie_size, tree size_check,
4056 tree *fn, tsubst_flags_t complain)
4058 tree original_size = *size;
4059 tree fns;
4060 struct z_candidate *candidates;
4061 struct z_candidate *cand;
4062 bool any_viable_p;
4064 if (fn)
4065 *fn = NULL_TREE;
4066 /* Set to (size_t)-1 if the size check fails. */
4067 if (size_check != NULL_TREE)
4069 tree errval = TYPE_MAX_VALUE (sizetype);
4070 if (cxx_dialect >= cxx11 && flag_exceptions)
4071 errval = throw_bad_array_new_length ();
4072 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4073 original_size, errval);
4075 vec_safe_insert (*args, 0, *size);
4076 *args = resolve_args (*args, complain);
4077 if (*args == NULL)
4078 return error_mark_node;
4080 /* Based on:
4082 [expr.new]
4084 If this lookup fails to find the name, or if the allocated type
4085 is not a class type, the allocation function's name is looked
4086 up in the global scope.
4088 we disregard block-scope declarations of "operator new". */
4089 fns = lookup_function_nonclass (fnname, *args, /*block_p=*/false);
4091 /* Figure out what function is being called. */
4092 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
4093 complain);
4095 /* If no suitable function could be found, issue an error message
4096 and give up. */
4097 if (!cand)
4099 if (complain & tf_error)
4100 print_error_for_call_failure (fns, *args, any_viable_p, candidates);
4101 return error_mark_node;
4104 /* If a cookie is required, add some extra space. Whether
4105 or not a cookie is required cannot be determined until
4106 after we know which function was called. */
4107 if (*cookie_size)
4109 bool use_cookie = true;
4110 if (!abi_version_at_least (2))
4112 /* In G++ 3.2, the check was implemented incorrectly; it
4113 looked at the placement expression, rather than the
4114 type of the function. */
4115 if ((*args)->length () == 2
4116 && same_type_p (TREE_TYPE ((**args)[1]), ptr_type_node))
4117 use_cookie = false;
4119 else
4121 tree arg_types;
4123 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
4124 /* Skip the size_t parameter. */
4125 arg_types = TREE_CHAIN (arg_types);
4126 /* Check the remaining parameters (if any). */
4127 if (arg_types
4128 && TREE_CHAIN (arg_types) == void_list_node
4129 && same_type_p (TREE_VALUE (arg_types),
4130 ptr_type_node))
4131 use_cookie = false;
4133 /* If we need a cookie, adjust the number of bytes allocated. */
4134 if (use_cookie)
4136 /* Update the total size. */
4137 *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
4138 /* Set to (size_t)-1 if the size check fails. */
4139 gcc_assert (size_check != NULL_TREE);
4140 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4141 *size, TYPE_MAX_VALUE (sizetype));
4142 /* Update the argument list to reflect the adjusted size. */
4143 (**args)[0] = *size;
4145 else
4146 *cookie_size = NULL_TREE;
4149 /* Tell our caller which function we decided to call. */
4150 if (fn)
4151 *fn = cand->fn;
4153 /* Build the CALL_EXPR. */
4154 return build_over_call (cand, LOOKUP_NORMAL, complain);
4157 /* Build a new call to operator(). This may change ARGS. */
4159 static tree
4160 build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4162 struct z_candidate *candidates = 0, *cand;
4163 tree fns, convs, first_mem_arg = NULL_TREE;
4164 tree type = TREE_TYPE (obj);
4165 bool any_viable_p;
4166 tree result = NULL_TREE;
4167 void *p;
4169 if (error_operand_p (obj))
4170 return error_mark_node;
4172 obj = prep_operand (obj);
4174 if (TYPE_PTRMEMFUNC_P (type))
4176 if (complain & tf_error)
4177 /* It's no good looking for an overloaded operator() on a
4178 pointer-to-member-function. */
4179 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
4180 return error_mark_node;
4183 if (TYPE_BINFO (type))
4185 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
4186 if (fns == error_mark_node)
4187 return error_mark_node;
4189 else
4190 fns = NULL_TREE;
4192 if (args != NULL && *args != NULL)
4194 *args = resolve_args (*args, complain);
4195 if (*args == NULL)
4196 return error_mark_node;
4199 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4200 p = conversion_obstack_alloc (0);
4202 if (fns)
4204 first_mem_arg = obj;
4206 add_candidates (BASELINK_FUNCTIONS (fns),
4207 first_mem_arg, *args, NULL_TREE,
4208 NULL_TREE, false,
4209 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
4210 LOOKUP_NORMAL, &candidates, complain);
4213 convs = lookup_conversions (type);
4215 for (; convs; convs = TREE_CHAIN (convs))
4217 tree fns = TREE_VALUE (convs);
4218 tree totype = TREE_TYPE (convs);
4220 if (TYPE_PTRFN_P (totype)
4221 || TYPE_REFFN_P (totype)
4222 || (TREE_CODE (totype) == REFERENCE_TYPE
4223 && TYPE_PTRFN_P (TREE_TYPE (totype))))
4224 for (; fns; fns = OVL_NEXT (fns))
4226 tree fn = OVL_CURRENT (fns);
4228 if (DECL_NONCONVERTING_P (fn))
4229 continue;
4231 if (TREE_CODE (fn) == TEMPLATE_DECL)
4232 add_template_conv_candidate
4233 (&candidates, fn, obj, NULL_TREE, *args, totype,
4234 /*access_path=*/NULL_TREE,
4235 /*conversion_path=*/NULL_TREE, complain);
4236 else
4237 add_conv_candidate (&candidates, fn, obj, NULL_TREE,
4238 *args, /*conversion_path=*/NULL_TREE,
4239 /*access_path=*/NULL_TREE, complain);
4243 candidates = splice_viable (candidates, pedantic, &any_viable_p);
4244 if (!any_viable_p)
4246 if (complain & tf_error)
4248 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
4249 build_tree_list_vec (*args));
4250 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4252 result = error_mark_node;
4254 else
4256 cand = tourney (candidates, complain);
4257 if (cand == 0)
4259 if (complain & tf_error)
4261 error ("call of %<(%T) (%A)%> is ambiguous",
4262 TREE_TYPE (obj), build_tree_list_vec (*args));
4263 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4265 result = error_mark_node;
4267 /* Since cand->fn will be a type, not a function, for a conversion
4268 function, we must be careful not to unconditionally look at
4269 DECL_NAME here. */
4270 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
4271 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
4272 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4273 else
4275 obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1,
4276 complain);
4277 obj = convert_from_reference (obj);
4278 result = cp_build_function_call_vec (obj, args, complain);
4282 /* Free all the conversions we allocated. */
4283 obstack_free (&conversion_obstack, p);
4285 return result;
4288 /* Wrapper for above. */
4290 tree
4291 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4293 tree ret;
4294 bool subtime = timevar_cond_start (TV_OVERLOAD);
4295 ret = build_op_call_1 (obj, args, complain);
4296 timevar_cond_stop (TV_OVERLOAD, subtime);
4297 return ret;
4300 /* Called by op_error to prepare format strings suitable for the error
4301 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4302 and a suffix (controlled by NTYPES). */
4304 static const char *
4305 op_error_string (const char *errmsg, int ntypes, bool match)
4307 const char *msg;
4309 const char *msgp = concat (match ? G_("ambiguous overload for ")
4310 : G_("no match for "), errmsg, NULL);
4312 if (ntypes == 3)
4313 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
4314 else if (ntypes == 2)
4315 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
4316 else
4317 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
4319 return msg;
4322 static void
4323 op_error (location_t loc, enum tree_code code, enum tree_code code2,
4324 tree arg1, tree arg2, tree arg3, bool match)
4326 const char *opname;
4328 if (code == MODIFY_EXPR)
4329 opname = assignment_operator_name_info[code2].name;
4330 else
4331 opname = operator_name_info[code].name;
4333 switch (code)
4335 case COND_EXPR:
4336 if (flag_diagnostics_show_caret)
4337 error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
4338 3, match),
4339 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4340 else
4341 error_at (loc, op_error_string (G_("ternary %<operator?:%> "
4342 "in %<%E ? %E : %E%>"), 3, match),
4343 arg1, arg2, arg3,
4344 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4345 break;
4347 case POSTINCREMENT_EXPR:
4348 case POSTDECREMENT_EXPR:
4349 if (flag_diagnostics_show_caret)
4350 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4351 opname, TREE_TYPE (arg1));
4352 else
4353 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4354 1, match),
4355 opname, arg1, opname, TREE_TYPE (arg1));
4356 break;
4358 case ARRAY_REF:
4359 if (flag_diagnostics_show_caret)
4360 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
4361 TREE_TYPE (arg1), TREE_TYPE (arg2));
4362 else
4363 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
4364 2, match),
4365 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
4366 break;
4368 case REALPART_EXPR:
4369 case IMAGPART_EXPR:
4370 if (flag_diagnostics_show_caret)
4371 error_at (loc, op_error_string (G_("%qs"), 1, match),
4372 opname, TREE_TYPE (arg1));
4373 else
4374 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
4375 opname, opname, arg1, TREE_TYPE (arg1));
4376 break;
4378 default:
4379 if (arg2)
4380 if (flag_diagnostics_show_caret)
4381 error_at (loc, op_error_string (G_("%<operator%s%>"), 2, match),
4382 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
4383 else
4384 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
4385 2, match),
4386 opname, arg1, opname, arg2,
4387 TREE_TYPE (arg1), TREE_TYPE (arg2));
4388 else
4389 if (flag_diagnostics_show_caret)
4390 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4391 opname, TREE_TYPE (arg1));
4392 else
4393 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
4394 1, match),
4395 opname, opname, arg1, TREE_TYPE (arg1));
4396 break;
4400 /* Return the implicit conversion sequence that could be used to
4401 convert E1 to E2 in [expr.cond]. */
4403 static conversion *
4404 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
4406 tree t1 = non_reference (TREE_TYPE (e1));
4407 tree t2 = non_reference (TREE_TYPE (e2));
4408 conversion *conv;
4409 bool good_base;
4411 /* [expr.cond]
4413 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
4414 implicitly converted (clause _conv_) to the type "lvalue reference to
4415 T2", subject to the constraint that in the conversion the
4416 reference must bind directly (_dcl.init.ref_) to an lvalue. */
4417 if (real_lvalue_p (e2))
4419 conv = implicit_conversion (build_reference_type (t2),
4422 /*c_cast_p=*/false,
4423 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
4424 |LOOKUP_ONLYCONVERTING,
4425 complain);
4426 if (conv)
4427 return conv;
4430 /* [expr.cond]
4432 If E1 and E2 have class type, and the underlying class types are
4433 the same or one is a base class of the other: E1 can be converted
4434 to match E2 if the class of T2 is the same type as, or a base
4435 class of, the class of T1, and the cv-qualification of T2 is the
4436 same cv-qualification as, or a greater cv-qualification than, the
4437 cv-qualification of T1. If the conversion is applied, E1 is
4438 changed to an rvalue of type T2 that still refers to the original
4439 source class object (or the appropriate subobject thereof). */
4440 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
4441 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
4443 if (good_base && at_least_as_qualified_p (t2, t1))
4445 conv = build_identity_conv (t1, e1);
4446 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
4447 TYPE_MAIN_VARIANT (t2)))
4448 conv = build_conv (ck_base, t2, conv);
4449 else
4450 conv = build_conv (ck_rvalue, t2, conv);
4451 return conv;
4453 else
4454 return NULL;
4456 else
4457 /* [expr.cond]
4459 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
4460 converted to the type that expression E2 would have if E2 were
4461 converted to an rvalue (or the type it has, if E2 is an rvalue). */
4462 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
4463 LOOKUP_IMPLICIT, complain);
4466 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
4467 arguments to the conditional expression. */
4469 static tree
4470 build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
4471 tsubst_flags_t complain)
4473 tree arg2_type;
4474 tree arg3_type;
4475 tree result = NULL_TREE;
4476 tree result_type = NULL_TREE;
4477 bool lvalue_p = true;
4478 struct z_candidate *candidates = 0;
4479 struct z_candidate *cand;
4480 void *p;
4481 tree orig_arg2, orig_arg3;
4483 /* As a G++ extension, the second argument to the conditional can be
4484 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
4485 c'.) If the second operand is omitted, make sure it is
4486 calculated only once. */
4487 if (!arg2)
4489 if (complain & tf_error)
4490 pedwarn (loc, OPT_Wpedantic,
4491 "ISO C++ forbids omitting the middle term of a ?: expression");
4493 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
4494 if (real_lvalue_p (arg1))
4495 arg2 = arg1 = stabilize_reference (arg1);
4496 else
4497 arg2 = arg1 = save_expr (arg1);
4500 /* If something has already gone wrong, just pass that fact up the
4501 tree. */
4502 if (error_operand_p (arg1)
4503 || error_operand_p (arg2)
4504 || error_operand_p (arg3))
4505 return error_mark_node;
4507 orig_arg2 = arg2;
4508 orig_arg3 = arg3;
4510 if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
4512 arg1 = force_rvalue (arg1, complain);
4513 arg2 = force_rvalue (arg2, complain);
4514 arg3 = force_rvalue (arg3, complain);
4516 /* force_rvalue can return error_mark on valid arguments. */
4517 if (error_operand_p (arg1)
4518 || error_operand_p (arg2)
4519 || error_operand_p (arg3))
4520 return error_mark_node;
4522 tree arg1_type = TREE_TYPE (arg1);
4523 arg2_type = TREE_TYPE (arg2);
4524 arg3_type = TREE_TYPE (arg3);
4526 if (TREE_CODE (arg2_type) != VECTOR_TYPE
4527 && TREE_CODE (arg3_type) != VECTOR_TYPE)
4529 /* Rely on the error messages of the scalar version. */
4530 tree scal = build_conditional_expr_1 (loc, integer_one_node,
4531 orig_arg2, orig_arg3, complain);
4532 if (scal == error_mark_node)
4533 return error_mark_node;
4534 tree stype = TREE_TYPE (scal);
4535 tree ctype = TREE_TYPE (arg1_type);
4536 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
4537 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
4539 if (complain & tf_error)
4540 error_at (loc, "inferred scalar type %qT is not an integer or "
4541 "floating point type of the same size as %qT", stype,
4542 COMPARISON_CLASS_P (arg1)
4543 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4544 : ctype);
4545 return error_mark_node;
4548 tree vtype = build_opaque_vector_type (stype,
4549 TYPE_VECTOR_SUBPARTS (arg1_type));
4550 /* We could pass complain & tf_warning to unsafe_conversion_p,
4551 but the warnings (like Wsign-conversion) have already been
4552 given by the scalar build_conditional_expr_1. We still check
4553 unsafe_conversion_p to forbid truncating long long -> float. */
4554 if (unsafe_conversion_p (loc, stype, arg2, false))
4556 if (complain & tf_error)
4557 error_at (loc, "conversion of scalar %qT to vector %qT "
4558 "involves truncation", arg2_type, vtype);
4559 return error_mark_node;
4561 if (unsafe_conversion_p (loc, stype, arg3, false))
4563 if (complain & tf_error)
4564 error_at (loc, "conversion of scalar %qT to vector %qT "
4565 "involves truncation", arg3_type, vtype);
4566 return error_mark_node;
4569 arg2 = cp_convert (stype, arg2, complain);
4570 arg2 = save_expr (arg2);
4571 arg2 = build_vector_from_val (vtype, arg2);
4572 arg2_type = vtype;
4573 arg3 = cp_convert (stype, arg3, complain);
4574 arg3 = save_expr (arg3);
4575 arg3 = build_vector_from_val (vtype, arg3);
4576 arg3_type = vtype;
4579 if ((TREE_CODE (arg2_type) == VECTOR_TYPE)
4580 != (TREE_CODE (arg3_type) == VECTOR_TYPE))
4582 enum stv_conv convert_flag =
4583 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
4584 complain & tf_error);
4586 switch (convert_flag)
4588 case stv_error:
4589 return error_mark_node;
4590 case stv_firstarg:
4592 arg2 = save_expr (arg2);
4593 arg2 = convert (TREE_TYPE (arg3_type), arg2);
4594 arg2 = build_vector_from_val (arg3_type, arg2);
4595 arg2_type = TREE_TYPE (arg2);
4596 break;
4598 case stv_secondarg:
4600 arg3 = save_expr (arg3);
4601 arg3 = convert (TREE_TYPE (arg2_type), arg3);
4602 arg3 = build_vector_from_val (arg2_type, arg3);
4603 arg3_type = TREE_TYPE (arg3);
4604 break;
4606 default:
4607 break;
4611 if (!same_type_p (arg2_type, arg3_type)
4612 || TYPE_VECTOR_SUBPARTS (arg1_type)
4613 != TYPE_VECTOR_SUBPARTS (arg2_type)
4614 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
4616 if (complain & tf_error)
4617 error_at (loc,
4618 "incompatible vector types in conditional expression: "
4619 "%qT, %qT and %qT", TREE_TYPE (arg1),
4620 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
4621 return error_mark_node;
4624 if (!COMPARISON_CLASS_P (arg1))
4625 arg1 = cp_build_binary_op (loc, NE_EXPR, arg1,
4626 build_zero_cst (arg1_type), complain);
4627 return fold_build3 (VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
4630 /* [expr.cond]
4632 The first expression is implicitly converted to bool (clause
4633 _conv_). */
4634 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
4635 LOOKUP_NORMAL);
4636 if (error_operand_p (arg1))
4637 return error_mark_node;
4639 /* [expr.cond]
4641 If either the second or the third operand has type (possibly
4642 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
4643 array-to-pointer (_conv.array_), and function-to-pointer
4644 (_conv.func_) standard conversions are performed on the second
4645 and third operands. */
4646 arg2_type = unlowered_expr_type (arg2);
4647 arg3_type = unlowered_expr_type (arg3);
4648 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
4650 /* Do the conversions. We don't these for `void' type arguments
4651 since it can't have any effect and since decay_conversion
4652 does not handle that case gracefully. */
4653 if (!VOID_TYPE_P (arg2_type))
4654 arg2 = decay_conversion (arg2, complain);
4655 if (!VOID_TYPE_P (arg3_type))
4656 arg3 = decay_conversion (arg3, complain);
4657 arg2_type = TREE_TYPE (arg2);
4658 arg3_type = TREE_TYPE (arg3);
4660 /* [expr.cond]
4662 One of the following shall hold:
4664 --The second or the third operand (but not both) is a
4665 throw-expression (_except.throw_); the result is of the
4666 type of the other and is an rvalue.
4668 --Both the second and the third operands have type void; the
4669 result is of type void and is an rvalue.
4671 We must avoid calling force_rvalue for expressions of type
4672 "void" because it will complain that their value is being
4673 used. */
4674 if (TREE_CODE (arg2) == THROW_EXPR
4675 && TREE_CODE (arg3) != THROW_EXPR)
4677 if (!VOID_TYPE_P (arg3_type))
4679 arg3 = force_rvalue (arg3, complain);
4680 if (arg3 == error_mark_node)
4681 return error_mark_node;
4683 arg3_type = TREE_TYPE (arg3);
4684 result_type = arg3_type;
4686 else if (TREE_CODE (arg2) != THROW_EXPR
4687 && TREE_CODE (arg3) == THROW_EXPR)
4689 if (!VOID_TYPE_P (arg2_type))
4691 arg2 = force_rvalue (arg2, complain);
4692 if (arg2 == error_mark_node)
4693 return error_mark_node;
4695 arg2_type = TREE_TYPE (arg2);
4696 result_type = arg2_type;
4698 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
4699 result_type = void_type_node;
4700 else
4702 if (complain & tf_error)
4704 if (VOID_TYPE_P (arg2_type))
4705 error_at (EXPR_LOC_OR_LOC (arg3, loc),
4706 "second operand to the conditional operator "
4707 "is of type %<void%>, but the third operand is "
4708 "neither a throw-expression nor of type %<void%>");
4709 else
4710 error_at (EXPR_LOC_OR_LOC (arg2, loc),
4711 "third operand to the conditional operator "
4712 "is of type %<void%>, but the second operand is "
4713 "neither a throw-expression nor of type %<void%>");
4715 return error_mark_node;
4718 lvalue_p = false;
4719 goto valid_operands;
4721 /* [expr.cond]
4723 Otherwise, if the second and third operand have different types,
4724 and either has (possibly cv-qualified) class type, an attempt is
4725 made to convert each of those operands to the type of the other. */
4726 else if (!same_type_p (arg2_type, arg3_type)
4727 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
4729 conversion *conv2;
4730 conversion *conv3;
4732 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4733 p = conversion_obstack_alloc (0);
4735 conv2 = conditional_conversion (arg2, arg3, complain);
4736 conv3 = conditional_conversion (arg3, arg2, complain);
4738 /* [expr.cond]
4740 If both can be converted, or one can be converted but the
4741 conversion is ambiguous, the program is ill-formed. If
4742 neither can be converted, the operands are left unchanged and
4743 further checking is performed as described below. If exactly
4744 one conversion is possible, that conversion is applied to the
4745 chosen operand and the converted operand is used in place of
4746 the original operand for the remainder of this section. */
4747 if ((conv2 && !conv2->bad_p
4748 && conv3 && !conv3->bad_p)
4749 || (conv2 && conv2->kind == ck_ambig)
4750 || (conv3 && conv3->kind == ck_ambig))
4752 if (complain & tf_error)
4753 error_at (loc, "operands to ?: have different types %qT and %qT",
4754 arg2_type, arg3_type);
4755 result = error_mark_node;
4757 else if (conv2 && (!conv2->bad_p || !conv3))
4759 arg2 = convert_like (conv2, arg2, complain);
4760 arg2 = convert_from_reference (arg2);
4761 arg2_type = TREE_TYPE (arg2);
4762 /* Even if CONV2 is a valid conversion, the result of the
4763 conversion may be invalid. For example, if ARG3 has type
4764 "volatile X", and X does not have a copy constructor
4765 accepting a "volatile X&", then even if ARG2 can be
4766 converted to X, the conversion will fail. */
4767 if (error_operand_p (arg2))
4768 result = error_mark_node;
4770 else if (conv3 && (!conv3->bad_p || !conv2))
4772 arg3 = convert_like (conv3, arg3, complain);
4773 arg3 = convert_from_reference (arg3);
4774 arg3_type = TREE_TYPE (arg3);
4775 if (error_operand_p (arg3))
4776 result = error_mark_node;
4779 /* Free all the conversions we allocated. */
4780 obstack_free (&conversion_obstack, p);
4782 if (result)
4783 return result;
4785 /* If, after the conversion, both operands have class type,
4786 treat the cv-qualification of both operands as if it were the
4787 union of the cv-qualification of the operands.
4789 The standard is not clear about what to do in this
4790 circumstance. For example, if the first operand has type
4791 "const X" and the second operand has a user-defined
4792 conversion to "volatile X", what is the type of the second
4793 operand after this step? Making it be "const X" (matching
4794 the first operand) seems wrong, as that discards the
4795 qualification without actually performing a copy. Leaving it
4796 as "volatile X" seems wrong as that will result in the
4797 conditional expression failing altogether, even though,
4798 according to this step, the one operand could be converted to
4799 the type of the other. */
4800 if ((conv2 || conv3)
4801 && CLASS_TYPE_P (arg2_type)
4802 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
4803 arg2_type = arg3_type =
4804 cp_build_qualified_type (arg2_type,
4805 cp_type_quals (arg2_type)
4806 | cp_type_quals (arg3_type));
4809 /* [expr.cond]
4811 If the second and third operands are glvalues of the same value
4812 category and have the same type, the result is of that type and
4813 value category. */
4814 if (((real_lvalue_p (arg2) && real_lvalue_p (arg3))
4815 || (xvalue_p (arg2) && xvalue_p (arg3)))
4816 && same_type_p (arg2_type, arg3_type))
4818 result_type = arg2_type;
4819 arg2 = mark_lvalue_use (arg2);
4820 arg3 = mark_lvalue_use (arg3);
4821 goto valid_operands;
4824 /* [expr.cond]
4826 Otherwise, the result is an rvalue. If the second and third
4827 operand do not have the same type, and either has (possibly
4828 cv-qualified) class type, overload resolution is used to
4829 determine the conversions (if any) to be applied to the operands
4830 (_over.match.oper_, _over.built_). */
4831 lvalue_p = false;
4832 if (!same_type_p (arg2_type, arg3_type)
4833 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
4835 tree args[3];
4836 conversion *conv;
4837 bool any_viable_p;
4839 /* Rearrange the arguments so that add_builtin_candidate only has
4840 to know about two args. In build_builtin_candidate, the
4841 arguments are unscrambled. */
4842 args[0] = arg2;
4843 args[1] = arg3;
4844 args[2] = arg1;
4845 add_builtin_candidates (&candidates,
4846 COND_EXPR,
4847 NOP_EXPR,
4848 ansi_opname (COND_EXPR),
4849 args,
4850 LOOKUP_NORMAL, complain);
4852 /* [expr.cond]
4854 If the overload resolution fails, the program is
4855 ill-formed. */
4856 candidates = splice_viable (candidates, pedantic, &any_viable_p);
4857 if (!any_viable_p)
4859 if (complain & tf_error)
4861 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
4862 print_z_candidates (loc, candidates);
4864 return error_mark_node;
4866 cand = tourney (candidates, complain);
4867 if (!cand)
4869 if (complain & tf_error)
4871 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
4872 print_z_candidates (loc, candidates);
4874 return error_mark_node;
4877 /* [expr.cond]
4879 Otherwise, the conversions thus determined are applied, and
4880 the converted operands are used in place of the original
4881 operands for the remainder of this section. */
4882 conv = cand->convs[0];
4883 arg1 = convert_like (conv, arg1, complain);
4884 conv = cand->convs[1];
4885 arg2 = convert_like (conv, arg2, complain);
4886 arg2_type = TREE_TYPE (arg2);
4887 conv = cand->convs[2];
4888 arg3 = convert_like (conv, arg3, complain);
4889 arg3_type = TREE_TYPE (arg3);
4892 /* [expr.cond]
4894 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
4895 and function-to-pointer (_conv.func_) standard conversions are
4896 performed on the second and third operands.
4898 We need to force the lvalue-to-rvalue conversion here for class types,
4899 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
4900 that isn't wrapped with a TARGET_EXPR plays havoc with exception
4901 regions. */
4903 arg2 = force_rvalue (arg2, complain);
4904 if (!CLASS_TYPE_P (arg2_type))
4905 arg2_type = TREE_TYPE (arg2);
4907 arg3 = force_rvalue (arg3, complain);
4908 if (!CLASS_TYPE_P (arg3_type))
4909 arg3_type = TREE_TYPE (arg3);
4911 if (arg2 == error_mark_node || arg3 == error_mark_node)
4912 return error_mark_node;
4914 /* [expr.cond]
4916 After those conversions, one of the following shall hold:
4918 --The second and third operands have the same type; the result is of
4919 that type. */
4920 if (same_type_p (arg2_type, arg3_type))
4921 result_type = arg2_type;
4922 /* [expr.cond]
4924 --The second and third operands have arithmetic or enumeration
4925 type; the usual arithmetic conversions are performed to bring
4926 them to a common type, and the result is of that type. */
4927 else if ((ARITHMETIC_TYPE_P (arg2_type)
4928 || UNSCOPED_ENUM_P (arg2_type))
4929 && (ARITHMETIC_TYPE_P (arg3_type)
4930 || UNSCOPED_ENUM_P (arg3_type)))
4932 /* In this case, there is always a common type. */
4933 result_type = type_after_usual_arithmetic_conversions (arg2_type,
4934 arg3_type);
4935 if (complain & tf_warning)
4936 do_warn_double_promotion (result_type, arg2_type, arg3_type,
4937 "implicit conversion from %qT to %qT to "
4938 "match other result of conditional",
4939 loc);
4941 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
4942 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
4944 if (TREE_CODE (orig_arg2) == CONST_DECL
4945 && TREE_CODE (orig_arg3) == CONST_DECL
4946 && DECL_CONTEXT (orig_arg2) == DECL_CONTEXT (orig_arg3))
4947 /* Two enumerators from the same enumeration can have different
4948 types when the enumeration is still being defined. */;
4949 else if (complain & tf_warning)
4950 warning_at (loc, OPT_Wenum_compare, "enumeral mismatch in "
4951 "conditional expression: %qT vs %qT",
4952 arg2_type, arg3_type);
4954 else if (extra_warnings
4955 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
4956 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
4957 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
4958 && !same_type_p (arg2_type,
4959 type_promotes_to (arg3_type)))))
4961 if (complain & tf_warning)
4962 warning_at (loc, 0, "enumeral and non-enumeral type in "
4963 "conditional expression");
4966 arg2 = perform_implicit_conversion (result_type, arg2, complain);
4967 arg3 = perform_implicit_conversion (result_type, arg3, complain);
4969 /* [expr.cond]
4971 --The second and third operands have pointer type, or one has
4972 pointer type and the other is a null pointer constant; pointer
4973 conversions (_conv.ptr_) and qualification conversions
4974 (_conv.qual_) are performed to bring them to their composite
4975 pointer type (_expr.rel_). The result is of the composite
4976 pointer type.
4978 --The second and third operands have pointer to member type, or
4979 one has pointer to member type and the other is a null pointer
4980 constant; pointer to member conversions (_conv.mem_) and
4981 qualification conversions (_conv.qual_) are performed to bring
4982 them to a common type, whose cv-qualification shall match the
4983 cv-qualification of either the second or the third operand.
4984 The result is of the common type. */
4985 else if ((null_ptr_cst_p (arg2)
4986 && TYPE_PTR_OR_PTRMEM_P (arg3_type))
4987 || (null_ptr_cst_p (arg3)
4988 && TYPE_PTR_OR_PTRMEM_P (arg2_type))
4989 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
4990 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
4991 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
4993 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
4994 arg3, CPO_CONDITIONAL_EXPR,
4995 complain);
4996 if (result_type == error_mark_node)
4997 return error_mark_node;
4998 arg2 = perform_implicit_conversion (result_type, arg2, complain);
4999 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5002 if (!result_type)
5004 if (complain & tf_error)
5005 error_at (loc, "operands to ?: have different types %qT and %qT",
5006 arg2_type, arg3_type);
5007 return error_mark_node;
5010 if (arg2 == error_mark_node || arg3 == error_mark_node)
5011 return error_mark_node;
5013 valid_operands:
5014 result = build3 (COND_EXPR, result_type, arg1, arg2, arg3);
5015 if (!cp_unevaluated_operand)
5016 /* Avoid folding within decltype (c++/42013) and noexcept. */
5017 result = fold_if_not_in_template (result);
5019 /* We can't use result_type below, as fold might have returned a
5020 throw_expr. */
5022 if (!lvalue_p)
5024 /* Expand both sides into the same slot, hopefully the target of
5025 the ?: expression. We used to check for TARGET_EXPRs here,
5026 but now we sometimes wrap them in NOP_EXPRs so the test would
5027 fail. */
5028 if (CLASS_TYPE_P (TREE_TYPE (result)))
5029 result = get_target_expr_sfinae (result, complain);
5030 /* If this expression is an rvalue, but might be mistaken for an
5031 lvalue, we must add a NON_LVALUE_EXPR. */
5032 result = rvalue (result);
5034 else
5035 result = force_paren_expr (result);
5037 return result;
5040 /* Wrapper for above. */
5042 tree
5043 build_conditional_expr (location_t loc, tree arg1, tree arg2, tree arg3,
5044 tsubst_flags_t complain)
5046 tree ret;
5047 bool subtime = timevar_cond_start (TV_OVERLOAD);
5048 ret = build_conditional_expr_1 (loc, arg1, arg2, arg3, complain);
5049 timevar_cond_stop (TV_OVERLOAD, subtime);
5050 return ret;
5053 /* OPERAND is an operand to an expression. Perform necessary steps
5054 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
5055 returned. */
5057 static tree
5058 prep_operand (tree operand)
5060 if (operand)
5062 if (CLASS_TYPE_P (TREE_TYPE (operand))
5063 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
5064 /* Make sure the template type is instantiated now. */
5065 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
5068 return operand;
5071 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5072 OVERLOAD) to the CANDIDATES, returning an updated list of
5073 CANDIDATES. The ARGS are the arguments provided to the call;
5074 if FIRST_ARG is non-null it is the implicit object argument,
5075 otherwise the first element of ARGS is used if needed. The
5076 EXPLICIT_TARGS are explicit template arguments provided.
5077 TEMPLATE_ONLY is true if only template functions should be
5078 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5079 add_function_candidate. */
5081 static void
5082 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
5083 tree return_type,
5084 tree explicit_targs, bool template_only,
5085 tree conversion_path, tree access_path,
5086 int flags,
5087 struct z_candidate **candidates,
5088 tsubst_flags_t complain)
5090 tree ctype;
5091 const vec<tree, va_gc> *non_static_args;
5092 bool check_list_ctor;
5093 bool check_converting;
5094 unification_kind_t strict;
5095 tree fn;
5097 if (!fns)
5098 return;
5100 /* Precalculate special handling of constructors and conversion ops. */
5101 fn = OVL_CURRENT (fns);
5102 if (DECL_CONV_FN_P (fn))
5104 check_list_ctor = false;
5105 check_converting = !!(flags & LOOKUP_ONLYCONVERTING);
5106 if (flags & LOOKUP_NO_CONVERSION)
5107 /* We're doing return_type(x). */
5108 strict = DEDUCE_CONV;
5109 else
5110 /* We're doing x.operator return_type(). */
5111 strict = DEDUCE_EXACT;
5112 /* [over.match.funcs] For conversion functions, the function
5113 is considered to be a member of the class of the implicit
5114 object argument for the purpose of defining the type of
5115 the implicit object parameter. */
5116 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
5118 else
5120 if (DECL_CONSTRUCTOR_P (fn))
5122 check_list_ctor = !!(flags & LOOKUP_LIST_ONLY);
5123 /* For list-initialization we consider explicit constructors
5124 and complain if one is chosen. */
5125 check_converting
5126 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
5127 == LOOKUP_ONLYCONVERTING);
5129 else
5131 check_list_ctor = false;
5132 check_converting = false;
5134 strict = DEDUCE_CALL;
5135 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
5138 if (first_arg)
5139 non_static_args = args;
5140 else
5141 /* Delay creating the implicit this parameter until it is needed. */
5142 non_static_args = NULL;
5144 for (; fns; fns = OVL_NEXT (fns))
5146 tree fn_first_arg;
5147 const vec<tree, va_gc> *fn_args;
5149 fn = OVL_CURRENT (fns);
5151 if (check_converting && DECL_NONCONVERTING_P (fn))
5152 continue;
5153 if (check_list_ctor && !is_list_ctor (fn))
5154 continue;
5156 /* Figure out which set of arguments to use. */
5157 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
5159 /* If this function is a non-static member and we didn't get an
5160 implicit object argument, move it out of args. */
5161 if (first_arg == NULL_TREE)
5163 unsigned int ix;
5164 tree arg;
5165 vec<tree, va_gc> *tempvec;
5166 vec_alloc (tempvec, args->length () - 1);
5167 for (ix = 1; args->iterate (ix, &arg); ++ix)
5168 tempvec->quick_push (arg);
5169 non_static_args = tempvec;
5170 first_arg = (*args)[0];
5173 fn_first_arg = first_arg;
5174 fn_args = non_static_args;
5176 else
5178 /* Otherwise, just use the list of arguments provided. */
5179 fn_first_arg = NULL_TREE;
5180 fn_args = args;
5183 if (TREE_CODE (fn) == TEMPLATE_DECL)
5184 add_template_candidate (candidates,
5186 ctype,
5187 explicit_targs,
5188 fn_first_arg,
5189 fn_args,
5190 return_type,
5191 access_path,
5192 conversion_path,
5193 flags,
5194 strict,
5195 complain);
5196 else if (!template_only)
5197 add_function_candidate (candidates,
5199 ctype,
5200 fn_first_arg,
5201 fn_args,
5202 access_path,
5203 conversion_path,
5204 flags,
5205 complain);
5209 static tree
5210 build_new_op_1 (location_t loc, enum tree_code code, int flags, tree arg1,
5211 tree arg2, tree arg3, tree *overload, tsubst_flags_t complain)
5213 struct z_candidate *candidates = 0, *cand;
5214 vec<tree, va_gc> *arglist;
5215 tree fnname;
5216 tree args[3];
5217 tree result = NULL_TREE;
5218 bool result_valid_p = false;
5219 enum tree_code code2 = NOP_EXPR;
5220 enum tree_code code_orig_arg1 = ERROR_MARK;
5221 enum tree_code code_orig_arg2 = ERROR_MARK;
5222 conversion *conv;
5223 void *p;
5224 bool strict_p;
5225 bool any_viable_p;
5227 if (error_operand_p (arg1)
5228 || error_operand_p (arg2)
5229 || error_operand_p (arg3))
5230 return error_mark_node;
5232 if (code == MODIFY_EXPR)
5234 code2 = TREE_CODE (arg3);
5235 arg3 = NULL_TREE;
5236 fnname = ansi_assopname (code2);
5238 else
5239 fnname = ansi_opname (code);
5241 arg1 = prep_operand (arg1);
5243 switch (code)
5245 case NEW_EXPR:
5246 case VEC_NEW_EXPR:
5247 case VEC_DELETE_EXPR:
5248 case DELETE_EXPR:
5249 /* Use build_op_new_call and build_op_delete_call instead. */
5250 gcc_unreachable ();
5252 case CALL_EXPR:
5253 /* Use build_op_call instead. */
5254 gcc_unreachable ();
5256 case TRUTH_ORIF_EXPR:
5257 case TRUTH_ANDIF_EXPR:
5258 case TRUTH_AND_EXPR:
5259 case TRUTH_OR_EXPR:
5260 /* These are saved for the sake of warn_logical_operator. */
5261 code_orig_arg1 = TREE_CODE (arg1);
5262 code_orig_arg2 = TREE_CODE (arg2);
5264 default:
5265 break;
5268 arg2 = prep_operand (arg2);
5269 arg3 = prep_operand (arg3);
5271 if (code == COND_EXPR)
5272 /* Use build_conditional_expr instead. */
5273 gcc_unreachable ();
5274 else if (! OVERLOAD_TYPE_P (TREE_TYPE (arg1))
5275 && (! arg2 || ! OVERLOAD_TYPE_P (TREE_TYPE (arg2))))
5276 goto builtin;
5278 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5279 arg2 = integer_zero_node;
5281 vec_alloc (arglist, 3);
5282 arglist->quick_push (arg1);
5283 if (arg2 != NULL_TREE)
5284 arglist->quick_push (arg2);
5285 if (arg3 != NULL_TREE)
5286 arglist->quick_push (arg3);
5288 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5289 p = conversion_obstack_alloc (0);
5291 /* Add namespace-scope operators to the list of functions to
5292 consider. */
5293 add_candidates (lookup_function_nonclass (fnname, arglist, /*block_p=*/true),
5294 NULL_TREE, arglist, NULL_TREE,
5295 NULL_TREE, false, NULL_TREE, NULL_TREE,
5296 flags, &candidates, complain);
5298 args[0] = arg1;
5299 args[1] = arg2;
5300 args[2] = NULL_TREE;
5302 /* Add class-member operators to the candidate set. */
5303 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
5305 tree fns;
5307 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
5308 if (fns == error_mark_node)
5310 result = error_mark_node;
5311 goto user_defined_result_ready;
5313 if (fns)
5314 add_candidates (BASELINK_FUNCTIONS (fns),
5315 NULL_TREE, arglist, NULL_TREE,
5316 NULL_TREE, false,
5317 BASELINK_BINFO (fns),
5318 BASELINK_ACCESS_BINFO (fns),
5319 flags, &candidates, complain);
5321 /* Per 13.3.1.2/3, 2nd bullet, if no operand has a class type, then
5322 only non-member functions that have type T1 or reference to
5323 cv-qualified-opt T1 for the first argument, if the first argument
5324 has an enumeration type, or T2 or reference to cv-qualified-opt
5325 T2 for the second argument, if the the second argument has an
5326 enumeration type. Filter out those that don't match. */
5327 else if (! arg2 || ! CLASS_TYPE_P (TREE_TYPE (arg2)))
5329 struct z_candidate **candp, **next;
5331 for (candp = &candidates; *candp; candp = next)
5333 tree parmlist, parmtype;
5334 int i, nargs = (arg2 ? 2 : 1);
5336 cand = *candp;
5337 next = &cand->next;
5339 parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5341 for (i = 0; i < nargs; ++i)
5343 parmtype = TREE_VALUE (parmlist);
5345 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
5346 parmtype = TREE_TYPE (parmtype);
5347 if (TREE_CODE (TREE_TYPE (args[i])) == ENUMERAL_TYPE
5348 && (same_type_ignoring_top_level_qualifiers_p
5349 (TREE_TYPE (args[i]), parmtype)))
5350 break;
5352 parmlist = TREE_CHAIN (parmlist);
5355 /* No argument has an appropriate type, so remove this
5356 candidate function from the list. */
5357 if (i == nargs)
5359 *candp = cand->next;
5360 next = candp;
5365 add_builtin_candidates (&candidates, code, code2, fnname, args,
5366 flags, complain);
5368 switch (code)
5370 case COMPOUND_EXPR:
5371 case ADDR_EXPR:
5372 /* For these, the built-in candidates set is empty
5373 [over.match.oper]/3. We don't want non-strict matches
5374 because exact matches are always possible with built-in
5375 operators. The built-in candidate set for COMPONENT_REF
5376 would be empty too, but since there are no such built-in
5377 operators, we accept non-strict matches for them. */
5378 strict_p = true;
5379 break;
5381 default:
5382 strict_p = pedantic;
5383 break;
5386 candidates = splice_viable (candidates, strict_p, &any_viable_p);
5387 if (!any_viable_p)
5389 switch (code)
5391 case POSTINCREMENT_EXPR:
5392 case POSTDECREMENT_EXPR:
5393 /* Don't try anything fancy if we're not allowed to produce
5394 errors. */
5395 if (!(complain & tf_error))
5396 return error_mark_node;
5398 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
5399 distinguish between prefix and postfix ++ and
5400 operator++() was used for both, so we allow this with
5401 -fpermissive. */
5402 else
5404 const char *msg = (flag_permissive)
5405 ? G_("no %<%D(int)%> declared for postfix %qs,"
5406 " trying prefix operator instead")
5407 : G_("no %<%D(int)%> declared for postfix %qs");
5408 permerror (loc, msg, fnname, operator_name_info[code].name);
5411 if (!flag_permissive)
5412 return error_mark_node;
5414 if (code == POSTINCREMENT_EXPR)
5415 code = PREINCREMENT_EXPR;
5416 else
5417 code = PREDECREMENT_EXPR;
5418 result = build_new_op_1 (loc, code, flags, arg1, NULL_TREE,
5419 NULL_TREE, overload, complain);
5420 break;
5422 /* The caller will deal with these. */
5423 case ADDR_EXPR:
5424 case COMPOUND_EXPR:
5425 case COMPONENT_REF:
5426 result = NULL_TREE;
5427 result_valid_p = true;
5428 break;
5430 default:
5431 if (complain & tf_error)
5433 /* If one of the arguments of the operator represents
5434 an invalid use of member function pointer, try to report
5435 a meaningful error ... */
5436 if (invalid_nonstatic_memfn_p (arg1, tf_error)
5437 || invalid_nonstatic_memfn_p (arg2, tf_error)
5438 || invalid_nonstatic_memfn_p (arg3, tf_error))
5439 /* We displayed the error message. */;
5440 else
5442 /* ... Otherwise, report the more generic
5443 "no matching operator found" error */
5444 op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
5445 print_z_candidates (loc, candidates);
5448 result = error_mark_node;
5449 break;
5452 else
5454 cand = tourney (candidates, complain);
5455 if (cand == 0)
5457 if (complain & tf_error)
5459 op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
5460 print_z_candidates (loc, candidates);
5462 result = error_mark_node;
5464 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5466 if (overload)
5467 *overload = cand->fn;
5469 if (resolve_args (arglist, complain) == NULL)
5470 result = error_mark_node;
5471 else
5472 result = build_over_call (cand, LOOKUP_NORMAL, complain);
5474 else
5476 /* Give any warnings we noticed during overload resolution. */
5477 if (cand->warnings && (complain & tf_warning))
5479 struct candidate_warning *w;
5480 for (w = cand->warnings; w; w = w->next)
5481 joust (cand, w->loser, 1, complain);
5484 /* Check for comparison of different enum types. */
5485 switch (code)
5487 case GT_EXPR:
5488 case LT_EXPR:
5489 case GE_EXPR:
5490 case LE_EXPR:
5491 case EQ_EXPR:
5492 case NE_EXPR:
5493 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
5494 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
5495 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5496 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
5497 && (complain & tf_warning))
5499 warning (OPT_Wenum_compare,
5500 "comparison between %q#T and %q#T",
5501 TREE_TYPE (arg1), TREE_TYPE (arg2));
5503 break;
5504 default:
5505 break;
5508 /* We need to strip any leading REF_BIND so that bitfields
5509 don't cause errors. This should not remove any important
5510 conversions, because builtins don't apply to class
5511 objects directly. */
5512 conv = cand->convs[0];
5513 if (conv->kind == ck_ref_bind)
5514 conv = next_conversion (conv);
5515 arg1 = convert_like (conv, arg1, complain);
5517 if (arg2)
5519 conv = cand->convs[1];
5520 if (conv->kind == ck_ref_bind)
5521 conv = next_conversion (conv);
5522 else
5523 arg2 = decay_conversion (arg2, complain);
5525 /* We need to call warn_logical_operator before
5526 converting arg2 to a boolean_type, but after
5527 decaying an enumerator to its value. */
5528 if (complain & tf_warning)
5529 warn_logical_operator (loc, code, boolean_type_node,
5530 code_orig_arg1, arg1,
5531 code_orig_arg2, arg2);
5533 arg2 = convert_like (conv, arg2, complain);
5535 if (arg3)
5537 conv = cand->convs[2];
5538 if (conv->kind == ck_ref_bind)
5539 conv = next_conversion (conv);
5540 arg3 = convert_like (conv, arg3, complain);
5546 user_defined_result_ready:
5548 /* Free all the conversions we allocated. */
5549 obstack_free (&conversion_obstack, p);
5551 if (result || result_valid_p)
5552 return result;
5554 builtin:
5555 switch (code)
5557 case MODIFY_EXPR:
5558 return cp_build_modify_expr (arg1, code2, arg2, complain);
5560 case INDIRECT_REF:
5561 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
5563 case TRUTH_ANDIF_EXPR:
5564 case TRUTH_ORIF_EXPR:
5565 case TRUTH_AND_EXPR:
5566 case TRUTH_OR_EXPR:
5567 warn_logical_operator (loc, code, boolean_type_node,
5568 code_orig_arg1, arg1, code_orig_arg2, arg2);
5569 /* Fall through. */
5570 case PLUS_EXPR:
5571 case MINUS_EXPR:
5572 case MULT_EXPR:
5573 case TRUNC_DIV_EXPR:
5574 case GT_EXPR:
5575 case LT_EXPR:
5576 case GE_EXPR:
5577 case LE_EXPR:
5578 case EQ_EXPR:
5579 case NE_EXPR:
5580 case MAX_EXPR:
5581 case MIN_EXPR:
5582 case LSHIFT_EXPR:
5583 case RSHIFT_EXPR:
5584 case TRUNC_MOD_EXPR:
5585 case BIT_AND_EXPR:
5586 case BIT_IOR_EXPR:
5587 case BIT_XOR_EXPR:
5588 return cp_build_binary_op (loc, code, arg1, arg2, complain);
5590 case UNARY_PLUS_EXPR:
5591 case NEGATE_EXPR:
5592 case BIT_NOT_EXPR:
5593 case TRUTH_NOT_EXPR:
5594 case PREINCREMENT_EXPR:
5595 case POSTINCREMENT_EXPR:
5596 case PREDECREMENT_EXPR:
5597 case POSTDECREMENT_EXPR:
5598 case REALPART_EXPR:
5599 case IMAGPART_EXPR:
5600 case ABS_EXPR:
5601 return cp_build_unary_op (code, arg1, candidates != 0, complain);
5603 case ARRAY_REF:
5604 return cp_build_array_ref (input_location, arg1, arg2, complain);
5606 case MEMBER_REF:
5607 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_ARROW_STAR,
5608 complain),
5609 arg2, complain);
5611 /* The caller will deal with these. */
5612 case ADDR_EXPR:
5613 case COMPONENT_REF:
5614 case COMPOUND_EXPR:
5615 return NULL_TREE;
5617 default:
5618 gcc_unreachable ();
5620 return NULL_TREE;
5623 /* Wrapper for above. */
5625 tree
5626 build_new_op (location_t loc, enum tree_code code, int flags,
5627 tree arg1, tree arg2, tree arg3,
5628 tree *overload, tsubst_flags_t complain)
5630 tree ret;
5631 bool subtime = timevar_cond_start (TV_OVERLOAD);
5632 ret = build_new_op_1 (loc, code, flags, arg1, arg2, arg3,
5633 overload, complain);
5634 timevar_cond_stop (TV_OVERLOAD, subtime);
5635 return ret;
5638 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
5639 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
5641 static bool
5642 non_placement_deallocation_fn_p (tree t)
5644 /* A template instance is never a usual deallocation function,
5645 regardless of its signature. */
5646 if (TREE_CODE (t) == TEMPLATE_DECL
5647 || primary_template_instantiation_p (t))
5648 return false;
5650 /* If a class T has a member deallocation function named operator delete
5651 with exactly one parameter, then that function is a usual
5652 (non-placement) deallocation function. If class T does not declare
5653 such an operator delete but does declare a member deallocation
5654 function named operator delete with exactly two parameters, the second
5655 of which has type std::size_t (18.2), then this function is a usual
5656 deallocation function. */
5657 t = FUNCTION_ARG_CHAIN (t);
5658 if (t == void_list_node
5659 || (t && same_type_p (TREE_VALUE (t), size_type_node)
5660 && TREE_CHAIN (t) == void_list_node))
5661 return true;
5662 return false;
5665 /* Build a call to operator delete. This has to be handled very specially,
5666 because the restrictions on what signatures match are different from all
5667 other call instances. For a normal delete, only a delete taking (void *)
5668 or (void *, size_t) is accepted. For a placement delete, only an exact
5669 match with the placement new is accepted.
5671 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
5672 ADDR is the pointer to be deleted.
5673 SIZE is the size of the memory block to be deleted.
5674 GLOBAL_P is true if the delete-expression should not consider
5675 class-specific delete operators.
5676 PLACEMENT is the corresponding placement new call, or NULL_TREE.
5678 If this call to "operator delete" is being generated as part to
5679 deallocate memory allocated via a new-expression (as per [expr.new]
5680 which requires that if the initialization throws an exception then
5681 we call a deallocation function), then ALLOC_FN is the allocation
5682 function. */
5684 tree
5685 build_op_delete_call (enum tree_code code, tree addr, tree size,
5686 bool global_p, tree placement,
5687 tree alloc_fn, tsubst_flags_t complain)
5689 tree fn = NULL_TREE;
5690 tree fns, fnname, type, t;
5692 if (addr == error_mark_node)
5693 return error_mark_node;
5695 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
5697 fnname = ansi_opname (code);
5699 if (CLASS_TYPE_P (type)
5700 && COMPLETE_TYPE_P (complete_type (type))
5701 && !global_p)
5702 /* In [class.free]
5704 If the result of the lookup is ambiguous or inaccessible, or if
5705 the lookup selects a placement deallocation function, the
5706 program is ill-formed.
5708 Therefore, we ask lookup_fnfields to complain about ambiguity. */
5710 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
5711 if (fns == error_mark_node)
5712 return error_mark_node;
5714 else
5715 fns = NULL_TREE;
5717 if (fns == NULL_TREE)
5718 fns = lookup_name_nonclass (fnname);
5720 /* Strip const and volatile from addr. */
5721 addr = cp_convert (ptr_type_node, addr, complain);
5723 if (placement)
5725 /* "A declaration of a placement deallocation function matches the
5726 declaration of a placement allocation function if it has the same
5727 number of parameters and, after parameter transformations (8.3.5),
5728 all parameter types except the first are identical."
5730 So we build up the function type we want and ask instantiate_type
5731 to get it for us. */
5732 t = FUNCTION_ARG_CHAIN (alloc_fn);
5733 t = tree_cons (NULL_TREE, ptr_type_node, t);
5734 t = build_function_type (void_type_node, t);
5736 fn = instantiate_type (t, fns, tf_none);
5737 if (fn == error_mark_node)
5738 return NULL_TREE;
5740 if (BASELINK_P (fn))
5741 fn = BASELINK_FUNCTIONS (fn);
5743 /* "If the lookup finds the two-parameter form of a usual deallocation
5744 function (3.7.4.2) and that function, considered as a placement
5745 deallocation function, would have been selected as a match for the
5746 allocation function, the program is ill-formed." */
5747 if (non_placement_deallocation_fn_p (fn))
5749 /* But if the class has an operator delete (void *), then that is
5750 the usual deallocation function, so we shouldn't complain
5751 about using the operator delete (void *, size_t). */
5752 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
5753 t; t = OVL_NEXT (t))
5755 tree elt = OVL_CURRENT (t);
5756 if (non_placement_deallocation_fn_p (elt)
5757 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
5758 goto ok;
5760 if (complain & tf_error)
5762 permerror (0, "non-placement deallocation function %q+D", fn);
5763 permerror (input_location, "selected for placement delete");
5765 else
5766 return error_mark_node;
5767 ok:;
5770 else
5771 /* "Any non-placement deallocation function matches a non-placement
5772 allocation function. If the lookup finds a single matching
5773 deallocation function, that function will be called; otherwise, no
5774 deallocation function will be called." */
5775 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
5776 t; t = OVL_NEXT (t))
5778 tree elt = OVL_CURRENT (t);
5779 if (non_placement_deallocation_fn_p (elt))
5781 fn = elt;
5782 /* "If a class T has a member deallocation function named
5783 operator delete with exactly one parameter, then that
5784 function is a usual (non-placement) deallocation
5785 function. If class T does not declare such an operator
5786 delete but does declare a member deallocation function named
5787 operator delete with exactly two parameters, the second of
5788 which has type std::size_t (18.2), then this function is a
5789 usual deallocation function."
5791 So (void*) beats (void*, size_t). */
5792 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
5793 break;
5797 /* If we have a matching function, call it. */
5798 if (fn)
5800 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
5802 /* If the FN is a member function, make sure that it is
5803 accessible. */
5804 if (BASELINK_P (fns))
5805 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
5806 complain);
5808 /* Core issue 901: It's ok to new a type with deleted delete. */
5809 if (DECL_DELETED_FN (fn) && alloc_fn)
5810 return NULL_TREE;
5812 if (placement)
5814 /* The placement args might not be suitable for overload
5815 resolution at this point, so build the call directly. */
5816 int nargs = call_expr_nargs (placement);
5817 tree *argarray = XALLOCAVEC (tree, nargs);
5818 int i;
5819 argarray[0] = addr;
5820 for (i = 1; i < nargs; i++)
5821 argarray[i] = CALL_EXPR_ARG (placement, i);
5822 mark_used (fn);
5823 return build_cxx_call (fn, nargs, argarray, complain);
5825 else
5827 tree ret;
5828 vec<tree, va_gc> *args = make_tree_vector ();
5829 args->quick_push (addr);
5830 if (FUNCTION_ARG_CHAIN (fn) != void_list_node)
5831 args->quick_push (size);
5832 ret = cp_build_function_call_vec (fn, &args, complain);
5833 release_tree_vector (args);
5834 return ret;
5838 /* [expr.new]
5840 If no unambiguous matching deallocation function can be found,
5841 propagating the exception does not cause the object's memory to
5842 be freed. */
5843 if (alloc_fn)
5845 if ((complain & tf_warning)
5846 && !placement)
5847 warning (0, "no corresponding deallocation function for %qD",
5848 alloc_fn);
5849 return NULL_TREE;
5852 if (complain & tf_error)
5853 error ("no suitable %<operator %s%> for %qT",
5854 operator_name_info[(int)code].name, type);
5855 return error_mark_node;
5858 /* If the current scope isn't allowed to access DECL along
5859 BASETYPE_PATH, give an error. The most derived class in
5860 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
5861 the declaration to use in the error diagnostic. */
5863 bool
5864 enforce_access (tree basetype_path, tree decl, tree diag_decl,
5865 tsubst_flags_t complain)
5867 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
5869 if (!accessible_p (basetype_path, decl, true))
5871 if (complain & tf_error)
5873 if (TREE_PRIVATE (decl))
5874 error ("%q+#D is private", diag_decl);
5875 else if (TREE_PROTECTED (decl))
5876 error ("%q+#D is protected", diag_decl);
5877 else
5878 error ("%q+#D is inaccessible", diag_decl);
5879 error ("within this context");
5881 return false;
5884 return true;
5887 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
5888 bitwise or of LOOKUP_* values. If any errors are warnings are
5889 generated, set *DIAGNOSTIC_FN to "error" or "warning",
5890 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
5891 to NULL. */
5893 static tree
5894 build_temp (tree expr, tree type, int flags,
5895 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
5897 int savew, savee;
5898 vec<tree, va_gc> *args;
5900 savew = warningcount + werrorcount, savee = errorcount;
5901 args = make_tree_vector_single (expr);
5902 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
5903 &args, type, flags, complain);
5904 release_tree_vector (args);
5905 if (warningcount + werrorcount > savew)
5906 *diagnostic_kind = DK_WARNING;
5907 else if (errorcount > savee)
5908 *diagnostic_kind = DK_ERROR;
5909 else
5910 *diagnostic_kind = DK_UNSPECIFIED;
5911 return expr;
5914 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
5915 EXPR is implicitly converted to type TOTYPE.
5916 FN and ARGNUM are used for diagnostics. */
5918 static void
5919 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
5921 /* Issue warnings about peculiar, but valid, uses of NULL. */
5922 if (expr == null_node && TREE_CODE (totype) != BOOLEAN_TYPE
5923 && ARITHMETIC_TYPE_P (totype))
5925 source_location loc =
5926 expansion_point_location_if_in_system_header (input_location);
5928 if (fn)
5929 warning_at (loc, OPT_Wconversion_null,
5930 "passing NULL to non-pointer argument %P of %qD",
5931 argnum, fn);
5932 else
5933 warning_at (loc, OPT_Wconversion_null,
5934 "converting to non-pointer type %qT from NULL", totype);
5937 /* Issue warnings if "false" is converted to a NULL pointer */
5938 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
5939 && TYPE_PTR_P (totype))
5941 if (fn)
5942 warning_at (input_location, OPT_Wconversion_null,
5943 "converting %<false%> to pointer type for argument %P "
5944 "of %qD", argnum, fn);
5945 else
5946 warning_at (input_location, OPT_Wconversion_null,
5947 "converting %<false%> to pointer type %qT", totype);
5951 /* Perform the conversions in CONVS on the expression EXPR. FN and
5952 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
5953 indicates the `this' argument of a method. INNER is nonzero when
5954 being called to continue a conversion chain. It is negative when a
5955 reference binding will be applied, positive otherwise. If
5956 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
5957 conversions will be emitted if appropriate. If C_CAST_P is true,
5958 this conversion is coming from a C-style cast; in that case,
5959 conversions to inaccessible bases are permitted. */
5961 static tree
5962 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
5963 int inner, bool issue_conversion_warnings,
5964 bool c_cast_p, tsubst_flags_t complain)
5966 tree totype = convs->type;
5967 diagnostic_t diag_kind;
5968 int flags;
5969 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
5971 if (convs->bad_p && !(complain & tf_error))
5972 return error_mark_node;
5974 if (convs->bad_p
5975 && convs->kind != ck_user
5976 && convs->kind != ck_list
5977 && convs->kind != ck_ambig
5978 && (convs->kind != ck_ref_bind
5979 || (convs->user_conv_p && next_conversion (convs)->bad_p))
5980 && (convs->kind != ck_rvalue
5981 || SCALAR_TYPE_P (totype))
5982 && convs->kind != ck_base)
5984 bool complained = false;
5985 conversion *t = convs;
5987 /* Give a helpful error if this is bad because of excess braces. */
5988 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5989 && SCALAR_TYPE_P (totype)
5990 && CONSTRUCTOR_NELTS (expr) > 0
5991 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
5993 complained = permerror (loc, "too many braces around initializer "
5994 "for %qT", totype);
5995 while (BRACE_ENCLOSED_INITIALIZER_P (expr)
5996 && CONSTRUCTOR_NELTS (expr) == 1)
5997 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6000 for (; t ; t = next_conversion (t))
6002 if (t->kind == ck_user && t->cand->reason)
6004 permerror (loc, "invalid user-defined conversion "
6005 "from %qT to %qT", TREE_TYPE (expr), totype);
6006 print_z_candidate (loc, "candidate is:", t->cand);
6007 expr = convert_like_real (t, expr, fn, argnum, 1,
6008 /*issue_conversion_warnings=*/false,
6009 /*c_cast_p=*/false,
6010 complain);
6011 if (convs->kind == ck_ref_bind)
6012 return convert_to_reference (totype, expr, CONV_IMPLICIT,
6013 LOOKUP_NORMAL, NULL_TREE,
6014 complain);
6015 else
6016 return cp_convert (totype, expr, complain);
6018 else if (t->kind == ck_user || !t->bad_p)
6020 expr = convert_like_real (t, expr, fn, argnum, 1,
6021 /*issue_conversion_warnings=*/false,
6022 /*c_cast_p=*/false,
6023 complain);
6024 break;
6026 else if (t->kind == ck_ambig)
6027 return convert_like_real (t, expr, fn, argnum, 1,
6028 /*issue_conversion_warnings=*/false,
6029 /*c_cast_p=*/false,
6030 complain);
6031 else if (t->kind == ck_identity)
6032 break;
6034 if (!complained)
6035 complained = permerror (loc, "invalid conversion from %qT to %qT",
6036 TREE_TYPE (expr), totype);
6037 if (complained && fn)
6038 inform (DECL_SOURCE_LOCATION (fn),
6039 "initializing argument %P of %qD", argnum, fn);
6041 return cp_convert (totype, expr, complain);
6044 if (issue_conversion_warnings && (complain & tf_warning))
6045 conversion_null_warnings (totype, expr, fn, argnum);
6047 switch (convs->kind)
6049 case ck_user:
6051 struct z_candidate *cand = convs->cand;
6052 tree convfn = cand->fn;
6053 unsigned i;
6055 /* When converting from an init list we consider explicit
6056 constructors, but actually trying to call one is an error. */
6057 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
6058 /* Unless this is for direct-list-initialization. */
6059 && !(BRACE_ENCLOSED_INITIALIZER_P (expr)
6060 && CONSTRUCTOR_IS_DIRECT_INIT (expr)))
6062 if (!(complain & tf_error))
6063 return error_mark_node;
6064 error ("converting to %qT from initializer list would use "
6065 "explicit constructor %qD", totype, convfn);
6068 /* If we're initializing from {}, it's value-initialization. */
6069 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6070 && CONSTRUCTOR_NELTS (expr) == 0
6071 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
6073 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
6074 expr = build_value_init (totype, complain);
6075 expr = get_target_expr_sfinae (expr, complain);
6076 if (expr != error_mark_node)
6078 TARGET_EXPR_LIST_INIT_P (expr) = true;
6079 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
6081 return expr;
6084 expr = mark_rvalue_use (expr);
6086 /* Set user_conv_p on the argument conversions, so rvalue/base
6087 handling knows not to allow any more UDCs. */
6088 for (i = 0; i < cand->num_convs; ++i)
6089 cand->convs[i]->user_conv_p = true;
6091 expr = build_over_call (cand, LOOKUP_NORMAL, complain);
6093 /* If this is a constructor or a function returning an aggr type,
6094 we need to build up a TARGET_EXPR. */
6095 if (DECL_CONSTRUCTOR_P (convfn))
6097 expr = build_cplus_new (totype, expr, complain);
6099 /* Remember that this was list-initialization. */
6100 if (convs->check_narrowing && expr != error_mark_node)
6101 TARGET_EXPR_LIST_INIT_P (expr) = true;
6104 return expr;
6106 case ck_identity:
6107 expr = mark_rvalue_use (expr);
6108 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
6110 int nelts = CONSTRUCTOR_NELTS (expr);
6111 if (nelts == 0)
6112 expr = build_value_init (totype, complain);
6113 else if (nelts == 1)
6114 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6115 else
6116 gcc_unreachable ();
6119 if (type_unknown_p (expr))
6120 expr = instantiate_type (totype, expr, complain);
6121 /* Convert a constant to its underlying value, unless we are
6122 about to bind it to a reference, in which case we need to
6123 leave it as an lvalue. */
6124 if (inner >= 0)
6126 expr = decl_constant_value_safe (expr);
6127 if (expr == null_node && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
6128 /* If __null has been converted to an integer type, we do not
6129 want to warn about uses of EXPR as an integer, rather than
6130 as a pointer. */
6131 expr = build_int_cst (totype, 0);
6133 return expr;
6134 case ck_ambig:
6135 /* We leave bad_p off ck_ambig because overload resolution considers
6136 it valid, it just fails when we try to perform it. So we need to
6137 check complain here, too. */
6138 if (complain & tf_error)
6140 /* Call build_user_type_conversion again for the error. */
6141 build_user_type_conversion (totype, convs->u.expr, LOOKUP_NORMAL,
6142 complain);
6143 if (fn)
6144 inform (input_location, "initializing argument %P of %q+D",
6145 argnum, fn);
6147 return error_mark_node;
6149 case ck_list:
6151 /* Conversion to std::initializer_list<T>. */
6152 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
6153 tree new_ctor = build_constructor (init_list_type_node, NULL);
6154 unsigned len = CONSTRUCTOR_NELTS (expr);
6155 tree array, val, field;
6156 vec<constructor_elt, va_gc> *vec = NULL;
6157 unsigned ix;
6159 /* Convert all the elements. */
6160 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
6162 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
6163 1, false, false, complain);
6164 if (sub == error_mark_node)
6165 return sub;
6166 if (!BRACE_ENCLOSED_INITIALIZER_P (val))
6167 check_narrowing (TREE_TYPE (sub), val);
6168 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
6169 if (!TREE_CONSTANT (sub))
6170 TREE_CONSTANT (new_ctor) = false;
6172 /* Build up the array. */
6173 elttype = cp_build_qualified_type
6174 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
6175 array = build_array_of_n_type (elttype, len);
6176 array = finish_compound_literal (array, new_ctor, complain);
6177 /* Take the address explicitly rather than via decay_conversion
6178 to avoid the error about taking the address of a temporary. */
6179 array = cp_build_addr_expr (array, complain);
6180 array = cp_convert (build_pointer_type (elttype), array, complain);
6181 if (array == error_mark_node)
6182 return error_mark_node;
6184 /* Build up the initializer_list object. */
6185 totype = complete_type (totype);
6186 field = next_initializable_field (TYPE_FIELDS (totype));
6187 CONSTRUCTOR_APPEND_ELT (vec, field, array);
6188 field = next_initializable_field (DECL_CHAIN (field));
6189 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
6190 new_ctor = build_constructor (totype, vec);
6191 return get_target_expr_sfinae (new_ctor, complain);
6194 case ck_aggr:
6195 if (TREE_CODE (totype) == COMPLEX_TYPE)
6197 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
6198 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
6199 real = perform_implicit_conversion (TREE_TYPE (totype),
6200 real, complain);
6201 imag = perform_implicit_conversion (TREE_TYPE (totype),
6202 imag, complain);
6203 expr = build2 (COMPLEX_EXPR, totype, real, imag);
6204 return fold_if_not_in_template (expr);
6206 expr = reshape_init (totype, expr, complain);
6207 expr = get_target_expr_sfinae (digest_init (totype, expr, complain),
6208 complain);
6209 if (expr != error_mark_node)
6210 TARGET_EXPR_LIST_INIT_P (expr) = true;
6211 return expr;
6213 default:
6214 break;
6217 expr = convert_like_real (next_conversion (convs), expr, fn, argnum,
6218 convs->kind == ck_ref_bind ? -1 : 1,
6219 convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
6220 c_cast_p,
6221 complain);
6222 if (expr == error_mark_node)
6223 return error_mark_node;
6225 switch (convs->kind)
6227 case ck_rvalue:
6228 expr = decay_conversion (expr, complain);
6229 if (expr == error_mark_node)
6230 return error_mark_node;
6232 if (! MAYBE_CLASS_TYPE_P (totype))
6233 return expr;
6234 /* Else fall through. */
6235 case ck_base:
6236 if (convs->kind == ck_base && !convs->need_temporary_p)
6238 /* We are going to bind a reference directly to a base-class
6239 subobject of EXPR. */
6240 /* Build an expression for `*((base*) &expr)'. */
6241 expr = cp_build_addr_expr (expr, complain);
6242 expr = convert_to_base (expr, build_pointer_type (totype),
6243 !c_cast_p, /*nonnull=*/true, complain);
6244 expr = cp_build_indirect_ref (expr, RO_IMPLICIT_CONVERSION, complain);
6245 return expr;
6248 /* Copy-initialization where the cv-unqualified version of the source
6249 type is the same class as, or a derived class of, the class of the
6250 destination [is treated as direct-initialization]. [dcl.init] */
6251 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
6252 if (convs->user_conv_p)
6253 /* This conversion is being done in the context of a user-defined
6254 conversion (i.e. the second step of copy-initialization), so
6255 don't allow any more. */
6256 flags |= LOOKUP_NO_CONVERSION;
6257 if (convs->rvaluedness_matches_p)
6258 flags |= LOOKUP_PREFER_RVALUE;
6259 if (TREE_CODE (expr) == TARGET_EXPR
6260 && TARGET_EXPR_LIST_INIT_P (expr))
6261 /* Copy-list-initialization doesn't actually involve a copy. */
6262 return expr;
6263 expr = build_temp (expr, totype, flags, &diag_kind, complain);
6264 if (diag_kind && fn && complain)
6265 emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (fn), 0,
6266 " initializing argument %P of %qD", argnum, fn);
6267 return build_cplus_new (totype, expr, complain);
6269 case ck_ref_bind:
6271 tree ref_type = totype;
6273 if (convs->bad_p && !next_conversion (convs)->bad_p)
6275 gcc_assert (TYPE_REF_IS_RVALUE (ref_type)
6276 && (real_lvalue_p (expr)
6277 || next_conversion(convs)->kind == ck_rvalue));
6279 error_at (loc, "cannot bind %qT lvalue to %qT",
6280 TREE_TYPE (expr), totype);
6281 if (fn)
6282 inform (input_location,
6283 "initializing argument %P of %q+D", argnum, fn);
6284 return error_mark_node;
6287 /* If necessary, create a temporary.
6289 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
6290 that need temporaries, even when their types are reference
6291 compatible with the type of reference being bound, so the
6292 upcoming call to cp_build_addr_expr doesn't fail. */
6293 if (convs->need_temporary_p
6294 || TREE_CODE (expr) == CONSTRUCTOR
6295 || TREE_CODE (expr) == VA_ARG_EXPR)
6297 /* Otherwise, a temporary of type "cv1 T1" is created and
6298 initialized from the initializer expression using the rules
6299 for a non-reference copy-initialization (8.5). */
6301 tree type = TREE_TYPE (ref_type);
6302 cp_lvalue_kind lvalue = real_lvalue_p (expr);
6304 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6305 (type, next_conversion (convs)->type));
6306 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
6307 && !TYPE_REF_IS_RVALUE (ref_type))
6309 /* If the reference is volatile or non-const, we
6310 cannot create a temporary. */
6311 if (lvalue & clk_bitfield)
6312 error_at (loc, "cannot bind bitfield %qE to %qT",
6313 expr, ref_type);
6314 else if (lvalue & clk_packed)
6315 error_at (loc, "cannot bind packed field %qE to %qT",
6316 expr, ref_type);
6317 else
6318 error_at (loc, "cannot bind rvalue %qE to %qT",
6319 expr, ref_type);
6320 return error_mark_node;
6322 /* If the source is a packed field, and we must use a copy
6323 constructor, then building the target expr will require
6324 binding the field to the reference parameter to the
6325 copy constructor, and we'll end up with an infinite
6326 loop. If we can use a bitwise copy, then we'll be
6327 OK. */
6328 if ((lvalue & clk_packed)
6329 && CLASS_TYPE_P (type)
6330 && type_has_nontrivial_copy_init (type))
6332 error_at (loc, "cannot bind packed field %qE to %qT",
6333 expr, ref_type);
6334 return error_mark_node;
6336 if (lvalue & clk_bitfield)
6338 expr = convert_bitfield_to_declared_type (expr);
6339 expr = fold_convert (type, expr);
6341 expr = build_target_expr_with_type (expr, type, complain);
6344 /* Take the address of the thing to which we will bind the
6345 reference. */
6346 expr = cp_build_addr_expr (expr, complain);
6347 if (expr == error_mark_node)
6348 return error_mark_node;
6350 /* Convert it to a pointer to the type referred to by the
6351 reference. This will adjust the pointer if a derived to
6352 base conversion is being performed. */
6353 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
6354 expr, complain);
6355 /* Convert the pointer to the desired reference type. */
6356 return build_nop (ref_type, expr);
6359 case ck_lvalue:
6360 return decay_conversion (expr, complain);
6362 case ck_qual:
6363 /* Warn about deprecated conversion if appropriate. */
6364 string_conv_p (totype, expr, 1);
6365 break;
6367 case ck_ptr:
6368 if (convs->base_p)
6369 expr = convert_to_base (expr, totype, !c_cast_p,
6370 /*nonnull=*/false, complain);
6371 return build_nop (totype, expr);
6373 case ck_pmem:
6374 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
6375 c_cast_p, complain);
6377 default:
6378 break;
6381 if (convs->check_narrowing)
6382 check_narrowing (totype, expr);
6384 if (issue_conversion_warnings)
6385 expr = cp_convert_and_check (totype, expr, complain);
6386 else
6387 expr = cp_convert (totype, expr, complain);
6389 return expr;
6392 /* ARG is being passed to a varargs function. Perform any conversions
6393 required. Return the converted value. */
6395 tree
6396 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
6398 tree arg_type;
6399 location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
6401 /* [expr.call]
6403 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6404 standard conversions are performed. */
6405 arg = decay_conversion (arg, complain);
6406 arg_type = TREE_TYPE (arg);
6407 /* [expr.call]
6409 If the argument has integral or enumeration type that is subject
6410 to the integral promotions (_conv.prom_), or a floating point
6411 type that is subject to the floating point promotion
6412 (_conv.fpprom_), the value of the argument is converted to the
6413 promoted type before the call. */
6414 if (TREE_CODE (arg_type) == REAL_TYPE
6415 && (TYPE_PRECISION (arg_type)
6416 < TYPE_PRECISION (double_type_node))
6417 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
6419 if ((complain & tf_warning)
6420 && warn_double_promotion && !c_inhibit_evaluation_warnings)
6421 warning_at (loc, OPT_Wdouble_promotion,
6422 "implicit conversion from %qT to %qT when passing "
6423 "argument to function",
6424 arg_type, double_type_node);
6425 arg = convert_to_real (double_type_node, arg);
6427 else if (NULLPTR_TYPE_P (arg_type))
6428 arg = null_pointer_node;
6429 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
6431 if (SCOPED_ENUM_P (arg_type) && !abi_version_at_least (6))
6433 if (complain & tf_warning)
6434 warning_at (loc, OPT_Wabi, "scoped enum %qT will not promote to an "
6435 "integral type in a future version of GCC", arg_type);
6436 arg = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg, complain);
6438 arg = cp_perform_integral_promotions (arg, complain);
6441 arg = require_complete_type_sfinae (arg, complain);
6442 arg_type = TREE_TYPE (arg);
6444 if (arg != error_mark_node
6445 /* In a template (or ill-formed code), we can have an incomplete type
6446 even after require_complete_type_sfinae, in which case we don't know
6447 whether it has trivial copy or not. */
6448 && COMPLETE_TYPE_P (arg_type))
6450 /* Build up a real lvalue-to-rvalue conversion in case the
6451 copy constructor is trivial but not callable. */
6452 if (!cp_unevaluated_operand && CLASS_TYPE_P (arg_type))
6453 force_rvalue (arg, complain);
6455 /* [expr.call] 5.2.2/7:
6456 Passing a potentially-evaluated argument of class type (Clause 9)
6457 with a non-trivial copy constructor or a non-trivial destructor
6458 with no corresponding parameter is conditionally-supported, with
6459 implementation-defined semantics.
6461 We used to just warn here and do a bitwise copy, but now
6462 cp_expr_size will abort if we try to do that.
6464 If the call appears in the context of a sizeof expression,
6465 it is not potentially-evaluated. */
6466 if (cp_unevaluated_operand == 0
6467 && (type_has_nontrivial_copy_init (arg_type)
6468 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type)))
6470 if (complain & tf_error)
6471 error_at (loc, "cannot pass objects of non-trivially-copyable "
6472 "type %q#T through %<...%>", arg_type);
6473 return error_mark_node;
6477 return arg;
6480 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
6482 tree
6483 build_x_va_arg (source_location loc, tree expr, tree type)
6485 if (processing_template_decl)
6486 return build_min (VA_ARG_EXPR, type, expr);
6488 type = complete_type_or_else (type, NULL_TREE);
6490 if (expr == error_mark_node || !type)
6491 return error_mark_node;
6493 expr = mark_lvalue_use (expr);
6495 if (type_has_nontrivial_copy_init (type)
6496 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
6497 || TREE_CODE (type) == REFERENCE_TYPE)
6499 /* Remove reference types so we don't ICE later on. */
6500 tree type1 = non_reference (type);
6501 /* conditionally-supported behavior [expr.call] 5.2.2/7. */
6502 error ("cannot receive objects of non-trivially-copyable type %q#T "
6503 "through %<...%>; ", type);
6504 expr = convert (build_pointer_type (type1), null_node);
6505 expr = cp_build_indirect_ref (expr, RO_NULL, tf_warning_or_error);
6506 return expr;
6509 return build_va_arg (loc, expr, type);
6512 /* TYPE has been given to va_arg. Apply the default conversions which
6513 would have happened when passed via ellipsis. Return the promoted
6514 type, or the passed type if there is no change. */
6516 tree
6517 cxx_type_promotes_to (tree type)
6519 tree promote;
6521 /* Perform the array-to-pointer and function-to-pointer
6522 conversions. */
6523 type = type_decays_to (type);
6525 promote = type_promotes_to (type);
6526 if (same_type_p (type, promote))
6527 promote = type;
6529 return promote;
6532 /* ARG is a default argument expression being passed to a parameter of
6533 the indicated TYPE, which is a parameter to FN. PARMNUM is the
6534 zero-based argument number. Do any required conversions. Return
6535 the converted value. */
6537 static GTY(()) vec<tree, va_gc> *default_arg_context;
6538 void
6539 push_defarg_context (tree fn)
6540 { vec_safe_push (default_arg_context, fn); }
6542 void
6543 pop_defarg_context (void)
6544 { default_arg_context->pop (); }
6546 tree
6547 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
6548 tsubst_flags_t complain)
6550 int i;
6551 tree t;
6553 /* See through clones. */
6554 fn = DECL_ORIGIN (fn);
6556 /* Detect recursion. */
6557 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
6558 if (t == fn)
6560 if (complain & tf_error)
6561 error ("recursive evaluation of default argument for %q#D", fn);
6562 return error_mark_node;
6565 /* If the ARG is an unparsed default argument expression, the
6566 conversion cannot be performed. */
6567 if (TREE_CODE (arg) == DEFAULT_ARG)
6569 if (complain & tf_error)
6570 error ("call to %qD uses the default argument for parameter %P, which "
6571 "is not yet defined", fn, parmnum);
6572 return error_mark_node;
6575 push_defarg_context (fn);
6577 if (fn && DECL_TEMPLATE_INFO (fn))
6578 arg = tsubst_default_argument (fn, type, arg, complain);
6580 /* Due to:
6582 [dcl.fct.default]
6584 The names in the expression are bound, and the semantic
6585 constraints are checked, at the point where the default
6586 expressions appears.
6588 we must not perform access checks here. */
6589 push_deferring_access_checks (dk_no_check);
6590 /* We must make a copy of ARG, in case subsequent processing
6591 alters any part of it. */
6592 arg = break_out_target_exprs (arg);
6593 if (TREE_CODE (arg) == CONSTRUCTOR)
6595 arg = digest_init (type, arg, complain);
6596 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
6597 ICR_DEFAULT_ARGUMENT, fn, parmnum,
6598 complain);
6600 else
6602 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
6603 ICR_DEFAULT_ARGUMENT, fn, parmnum,
6604 complain);
6605 arg = convert_for_arg_passing (type, arg, complain);
6607 pop_deferring_access_checks();
6609 pop_defarg_context ();
6611 return arg;
6614 /* Returns the type which will really be used for passing an argument of
6615 type TYPE. */
6617 tree
6618 type_passed_as (tree type)
6620 /* Pass classes with copy ctors by invisible reference. */
6621 if (TREE_ADDRESSABLE (type))
6623 type = build_reference_type (type);
6624 /* There are no other pointers to this temporary. */
6625 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
6627 else if (targetm.calls.promote_prototypes (type)
6628 && INTEGRAL_TYPE_P (type)
6629 && COMPLETE_TYPE_P (type)
6630 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
6631 TYPE_SIZE (integer_type_node)))
6632 type = integer_type_node;
6634 return type;
6637 /* Actually perform the appropriate conversion. */
6639 tree
6640 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
6642 tree bitfield_type;
6644 /* If VAL is a bitfield, then -- since it has already been converted
6645 to TYPE -- it cannot have a precision greater than TYPE.
6647 If it has a smaller precision, we must widen it here. For
6648 example, passing "int f:3;" to a function expecting an "int" will
6649 not result in any conversion before this point.
6651 If the precision is the same we must not risk widening. For
6652 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
6653 often have type "int", even though the C++ type for the field is
6654 "long long". If the value is being passed to a function
6655 expecting an "int", then no conversions will be required. But,
6656 if we call convert_bitfield_to_declared_type, the bitfield will
6657 be converted to "long long". */
6658 bitfield_type = is_bitfield_expr_with_lowered_type (val);
6659 if (bitfield_type
6660 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
6661 val = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type), val);
6663 if (val == error_mark_node)
6665 /* Pass classes with copy ctors by invisible reference. */
6666 else if (TREE_ADDRESSABLE (type))
6667 val = build1 (ADDR_EXPR, build_reference_type (type), val);
6668 else if (targetm.calls.promote_prototypes (type)
6669 && INTEGRAL_TYPE_P (type)
6670 && COMPLETE_TYPE_P (type)
6671 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
6672 TYPE_SIZE (integer_type_node)))
6673 val = cp_perform_integral_promotions (val, complain);
6674 if ((complain & tf_warning)
6675 && warn_suggest_attribute_format)
6677 tree rhstype = TREE_TYPE (val);
6678 const enum tree_code coder = TREE_CODE (rhstype);
6679 const enum tree_code codel = TREE_CODE (type);
6680 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6681 && coder == codel
6682 && check_missing_format_attribute (type, rhstype))
6683 warning (OPT_Wsuggest_attribute_format,
6684 "argument of function call might be a candidate for a format attribute");
6686 return val;
6689 /* Returns true iff FN is a function with magic varargs, i.e. ones for
6690 which no conversions at all should be done. This is true for some
6691 builtins which don't act like normal functions. */
6693 bool
6694 magic_varargs_p (tree fn)
6696 if (flag_cilkplus && is_cilkplus_reduce_builtin (fn) != BUILT_IN_NONE)
6697 return true;
6699 if (DECL_BUILT_IN (fn))
6700 switch (DECL_FUNCTION_CODE (fn))
6702 case BUILT_IN_CLASSIFY_TYPE:
6703 case BUILT_IN_CONSTANT_P:
6704 case BUILT_IN_NEXT_ARG:
6705 case BUILT_IN_VA_START:
6706 return true;
6708 default:;
6709 return lookup_attribute ("type generic",
6710 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
6713 return false;
6716 /* Returns the decl of the dispatcher function if FN is a function version. */
6718 tree
6719 get_function_version_dispatcher (tree fn)
6721 tree dispatcher_decl = NULL;
6723 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6724 && DECL_FUNCTION_VERSIONED (fn));
6726 gcc_assert (targetm.get_function_versions_dispatcher);
6727 dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
6729 if (dispatcher_decl == NULL)
6731 error_at (input_location, "use of multiversioned function "
6732 "without a default");
6733 return NULL;
6736 retrofit_lang_decl (dispatcher_decl);
6737 gcc_assert (dispatcher_decl != NULL);
6738 return dispatcher_decl;
6741 /* fn is a function version dispatcher that is marked used. Mark all the
6742 semantically identical function versions it will dispatch as used. */
6744 void
6745 mark_versions_used (tree fn)
6747 struct cgraph_node *node;
6748 struct cgraph_function_version_info *node_v;
6749 struct cgraph_function_version_info *it_v;
6751 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6753 node = cgraph_get_node (fn);
6754 if (node == NULL)
6755 return;
6757 gcc_assert (node->dispatcher_function);
6759 node_v = get_cgraph_node_version (node);
6760 if (node_v == NULL)
6761 return;
6763 /* All semantically identical versions are chained. Traverse and mark each
6764 one of them as used. */
6765 it_v = node_v->next;
6766 while (it_v != NULL)
6768 mark_used (it_v->this_node->decl);
6769 it_v = it_v->next;
6773 /* Subroutine of the various build_*_call functions. Overload resolution
6774 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
6775 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
6776 bitmask of various LOOKUP_* flags which apply to the call itself. */
6778 static tree
6779 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
6781 tree fn = cand->fn;
6782 const vec<tree, va_gc> *args = cand->args;
6783 tree first_arg = cand->first_arg;
6784 conversion **convs = cand->convs;
6785 conversion *conv;
6786 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
6787 int parmlen;
6788 tree val;
6789 int i = 0;
6790 int j = 0;
6791 unsigned int arg_index = 0;
6792 int is_method = 0;
6793 int nargs;
6794 tree *argarray;
6795 bool already_used = false;
6797 /* In a template, there is no need to perform all of the work that
6798 is normally done. We are only interested in the type of the call
6799 expression, i.e., the return type of the function. Any semantic
6800 errors will be deferred until the template is instantiated. */
6801 if (processing_template_decl)
6803 tree expr, addr;
6804 tree return_type;
6805 const tree *argarray;
6806 unsigned int nargs;
6808 return_type = TREE_TYPE (TREE_TYPE (fn));
6809 nargs = vec_safe_length (args);
6810 if (first_arg == NULL_TREE)
6811 argarray = args->address ();
6812 else
6814 tree *alcarray;
6815 unsigned int ix;
6816 tree arg;
6818 ++nargs;
6819 alcarray = XALLOCAVEC (tree, nargs);
6820 alcarray[0] = first_arg;
6821 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
6822 alcarray[ix + 1] = arg;
6823 argarray = alcarray;
6826 addr = build_addr_func (fn, complain);
6827 if (addr == error_mark_node)
6828 return error_mark_node;
6829 expr = build_call_array_loc (input_location, return_type,
6830 addr, nargs, argarray);
6831 if (TREE_THIS_VOLATILE (fn) && cfun)
6832 current_function_returns_abnormally = 1;
6833 return convert_from_reference (expr);
6836 /* Give any warnings we noticed during overload resolution. */
6837 if (cand->warnings && (complain & tf_warning))
6839 struct candidate_warning *w;
6840 for (w = cand->warnings; w; w = w->next)
6841 joust (cand, w->loser, 1, complain);
6844 /* Make =delete work with SFINAE. */
6845 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
6846 return error_mark_node;
6848 if (DECL_FUNCTION_MEMBER_P (fn))
6850 tree access_fn;
6851 /* If FN is a template function, two cases must be considered.
6852 For example:
6854 struct A {
6855 protected:
6856 template <class T> void f();
6858 template <class T> struct B {
6859 protected:
6860 void g();
6862 struct C : A, B<int> {
6863 using A::f; // #1
6864 using B<int>::g; // #2
6867 In case #1 where `A::f' is a member template, DECL_ACCESS is
6868 recorded in the primary template but not in its specialization.
6869 We check access of FN using its primary template.
6871 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
6872 because it is a member of class template B, DECL_ACCESS is
6873 recorded in the specialization `B<int>::g'. We cannot use its
6874 primary template because `B<T>::g' and `B<int>::g' may have
6875 different access. */
6876 if (DECL_TEMPLATE_INFO (fn)
6877 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
6878 access_fn = DECL_TI_TEMPLATE (fn);
6879 else
6880 access_fn = fn;
6881 if (!perform_or_defer_access_check (cand->access_path, access_fn,
6882 fn, complain))
6883 return error_mark_node;
6886 /* If we're checking for implicit delete, don't bother with argument
6887 conversions. */
6888 if (flags & LOOKUP_SPECULATIVE)
6890 if (DECL_DELETED_FN (fn))
6892 if (complain & tf_error)
6893 mark_used (fn);
6894 return error_mark_node;
6896 if (cand->viable == 1)
6897 return fn;
6898 else if (!(complain & tf_error))
6899 /* Reject bad conversions now. */
6900 return error_mark_node;
6901 /* else continue to get conversion error. */
6904 /* N3276 magic doesn't apply to nested calls. */
6905 int decltype_flag = (complain & tf_decltype);
6906 complain &= ~tf_decltype;
6908 /* Find maximum size of vector to hold converted arguments. */
6909 parmlen = list_length (parm);
6910 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
6911 if (parmlen > nargs)
6912 nargs = parmlen;
6913 argarray = XALLOCAVEC (tree, nargs);
6915 /* The implicit parameters to a constructor are not considered by overload
6916 resolution, and must be of the proper type. */
6917 if (DECL_CONSTRUCTOR_P (fn))
6919 tree object_arg;
6920 if (first_arg != NULL_TREE)
6922 object_arg = first_arg;
6923 first_arg = NULL_TREE;
6925 else
6927 object_arg = (*args)[arg_index];
6928 ++arg_index;
6930 argarray[j++] = build_this (object_arg);
6931 parm = TREE_CHAIN (parm);
6932 /* We should never try to call the abstract constructor. */
6933 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
6935 if (DECL_HAS_VTT_PARM_P (fn))
6937 argarray[j++] = (*args)[arg_index];
6938 ++arg_index;
6939 parm = TREE_CHAIN (parm);
6942 /* Bypass access control for 'this' parameter. */
6943 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6945 tree parmtype = TREE_VALUE (parm);
6946 tree arg = build_this (first_arg != NULL_TREE
6947 ? first_arg
6948 : (*args)[arg_index]);
6949 tree argtype = TREE_TYPE (arg);
6950 tree converted_arg;
6951 tree base_binfo;
6953 if (convs[i]->bad_p)
6955 if (complain & tf_error)
6956 permerror (input_location, "passing %qT as %<this%> argument of %q#D discards qualifiers",
6957 TREE_TYPE (argtype), fn);
6958 else
6959 return error_mark_node;
6962 /* See if the function member or the whole class type is declared
6963 final and the call can be devirtualized. */
6964 if (DECL_FINAL_P (fn)
6965 || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
6966 flags |= LOOKUP_NONVIRTUAL;
6968 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
6969 X is called for an object that is not of type X, or of a type
6970 derived from X, the behavior is undefined.
6972 So we can assume that anything passed as 'this' is non-null, and
6973 optimize accordingly. */
6974 gcc_assert (TYPE_PTR_P (parmtype));
6975 /* Convert to the base in which the function was declared. */
6976 gcc_assert (cand->conversion_path != NULL_TREE);
6977 converted_arg = build_base_path (PLUS_EXPR,
6978 arg,
6979 cand->conversion_path,
6980 1, complain);
6981 /* Check that the base class is accessible. */
6982 if (!accessible_base_p (TREE_TYPE (argtype),
6983 BINFO_TYPE (cand->conversion_path), true))
6985 if (complain & tf_error)
6986 error ("%qT is not an accessible base of %qT",
6987 BINFO_TYPE (cand->conversion_path),
6988 TREE_TYPE (argtype));
6989 else
6990 return error_mark_node;
6992 /* If fn was found by a using declaration, the conversion path
6993 will be to the derived class, not the base declaring fn. We
6994 must convert from derived to base. */
6995 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
6996 TREE_TYPE (parmtype), ba_unique,
6997 NULL, complain);
6998 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
6999 base_binfo, 1, complain);
7001 argarray[j++] = converted_arg;
7002 parm = TREE_CHAIN (parm);
7003 if (first_arg != NULL_TREE)
7004 first_arg = NULL_TREE;
7005 else
7006 ++arg_index;
7007 ++i;
7008 is_method = 1;
7011 gcc_assert (first_arg == NULL_TREE);
7012 for (; arg_index < vec_safe_length (args) && parm;
7013 parm = TREE_CHAIN (parm), ++arg_index, ++i)
7015 tree type = TREE_VALUE (parm);
7016 tree arg = (*args)[arg_index];
7017 bool conversion_warning = true;
7019 conv = convs[i];
7021 /* If the argument is NULL and used to (implicitly) instantiate a
7022 template function (and bind one of the template arguments to
7023 the type of 'long int'), we don't want to warn about passing NULL
7024 to non-pointer argument.
7025 For example, if we have this template function:
7027 template<typename T> void func(T x) {}
7029 we want to warn (when -Wconversion is enabled) in this case:
7031 void foo() {
7032 func<int>(NULL);
7035 but not in this case:
7037 void foo() {
7038 func(NULL);
7041 if (arg == null_node
7042 && DECL_TEMPLATE_INFO (fn)
7043 && cand->template_decl
7044 && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
7045 conversion_warning = false;
7047 /* Warn about initializer_list deduction that isn't currently in the
7048 working draft. */
7049 if (cxx_dialect > cxx98
7050 && flag_deduce_init_list
7051 && cand->template_decl
7052 && is_std_init_list (non_reference (type))
7053 && BRACE_ENCLOSED_INITIALIZER_P (arg))
7055 tree tmpl = TI_TEMPLATE (cand->template_decl);
7056 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
7057 tree patparm = get_pattern_parm (realparm, tmpl);
7058 tree pattype = TREE_TYPE (patparm);
7059 if (PACK_EXPANSION_P (pattype))
7060 pattype = PACK_EXPANSION_PATTERN (pattype);
7061 pattype = non_reference (pattype);
7063 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
7064 && (cand->explicit_targs == NULL_TREE
7065 || (TREE_VEC_LENGTH (cand->explicit_targs)
7066 <= TEMPLATE_TYPE_IDX (pattype))))
7068 pedwarn (input_location, 0, "deducing %qT as %qT",
7069 non_reference (TREE_TYPE (patparm)),
7070 non_reference (type));
7071 pedwarn (input_location, 0, " in call to %q+D", cand->fn);
7072 pedwarn (input_location, 0,
7073 " (you can disable this with -fno-deduce-init-list)");
7076 val = convert_like_with_context (conv, arg, fn, i - is_method,
7077 conversion_warning
7078 ? complain
7079 : complain & (~tf_warning));
7081 val = convert_for_arg_passing (type, val, complain);
7083 if (val == error_mark_node)
7084 return error_mark_node;
7085 else
7086 argarray[j++] = val;
7089 /* Default arguments */
7090 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
7092 if (TREE_VALUE (parm) == error_mark_node)
7093 return error_mark_node;
7094 argarray[j++] = convert_default_arg (TREE_VALUE (parm),
7095 TREE_PURPOSE (parm),
7096 fn, i - is_method,
7097 complain);
7100 /* Ellipsis */
7101 for (; arg_index < vec_safe_length (args); ++arg_index)
7103 tree a = (*args)[arg_index];
7104 if (magic_varargs_p (fn))
7105 /* Do no conversions for magic varargs. */
7106 a = mark_type_use (a);
7107 else
7108 a = convert_arg_to_ellipsis (a, complain);
7109 argarray[j++] = a;
7112 gcc_assert (j <= nargs);
7113 nargs = j;
7115 check_function_arguments (TREE_TYPE (fn), nargs, argarray);
7117 /* Avoid actually calling copy constructors and copy assignment operators,
7118 if possible. */
7120 if (! flag_elide_constructors)
7121 /* Do things the hard way. */;
7122 else if (cand->num_convs == 1
7123 && (DECL_COPY_CONSTRUCTOR_P (fn)
7124 || DECL_MOVE_CONSTRUCTOR_P (fn)))
7126 tree targ;
7127 tree arg = argarray[num_artificial_parms_for (fn)];
7128 tree fa;
7129 bool trivial = trivial_fn_p (fn);
7131 /* Pull out the real argument, disregarding const-correctness. */
7132 targ = arg;
7133 while (CONVERT_EXPR_P (targ)
7134 || TREE_CODE (targ) == NON_LVALUE_EXPR)
7135 targ = TREE_OPERAND (targ, 0);
7136 if (TREE_CODE (targ) == ADDR_EXPR)
7138 targ = TREE_OPERAND (targ, 0);
7139 if (!same_type_ignoring_top_level_qualifiers_p
7140 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
7141 targ = NULL_TREE;
7143 else
7144 targ = NULL_TREE;
7146 if (targ)
7147 arg = targ;
7148 else
7149 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
7151 /* [class.copy]: the copy constructor is implicitly defined even if
7152 the implementation elided its use. */
7153 if (!trivial || DECL_DELETED_FN (fn))
7155 mark_used (fn);
7156 already_used = true;
7159 /* If we're creating a temp and we already have one, don't create a
7160 new one. If we're not creating a temp but we get one, use
7161 INIT_EXPR to collapse the temp into our target. Otherwise, if the
7162 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
7163 temp or an INIT_EXPR otherwise. */
7164 fa = argarray[0];
7165 if (integer_zerop (fa))
7167 if (TREE_CODE (arg) == TARGET_EXPR)
7168 return arg;
7169 else if (trivial)
7170 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
7172 else if (TREE_CODE (arg) == TARGET_EXPR || trivial)
7174 tree to = stabilize_reference (cp_build_indirect_ref (fa, RO_NULL,
7175 complain));
7177 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
7178 return val;
7181 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
7182 && trivial_fn_p (fn)
7183 && !DECL_DELETED_FN (fn))
7185 tree to = stabilize_reference
7186 (cp_build_indirect_ref (argarray[0], RO_NULL, complain));
7187 tree type = TREE_TYPE (to);
7188 tree as_base = CLASSTYPE_AS_BASE (type);
7189 tree arg = argarray[1];
7191 if (is_really_empty_class (type))
7193 /* Avoid copying empty classes. */
7194 val = build2 (COMPOUND_EXPR, void_type_node, to, arg);
7195 TREE_NO_WARNING (val) = 1;
7196 val = build2 (COMPOUND_EXPR, type, val, to);
7197 TREE_NO_WARNING (val) = 1;
7199 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
7201 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
7202 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
7204 else
7206 /* We must only copy the non-tail padding parts. */
7207 tree arg0, arg2, t;
7208 tree array_type, alias_set;
7210 arg2 = TYPE_SIZE_UNIT (as_base);
7211 arg0 = cp_build_addr_expr (to, complain);
7213 array_type = build_array_type (char_type_node,
7214 build_index_type
7215 (size_binop (MINUS_EXPR,
7216 arg2, size_int (1))));
7217 alias_set = build_int_cst (build_pointer_type (type), 0);
7218 t = build2 (MODIFY_EXPR, void_type_node,
7219 build2 (MEM_REF, array_type, arg0, alias_set),
7220 build2 (MEM_REF, array_type, arg, alias_set));
7221 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
7222 TREE_NO_WARNING (val) = 1;
7225 return val;
7227 else if (DECL_DESTRUCTOR_P (fn)
7228 && trivial_fn_p (fn)
7229 && !DECL_DELETED_FN (fn))
7230 return fold_convert (void_type_node, argarray[0]);
7231 /* FIXME handle trivial default constructor, too. */
7233 /* For calls to a multi-versioned function, overload resolution
7234 returns the function with the highest target priority, that is,
7235 the version that will checked for dispatching first. If this
7236 version is inlinable, a direct call to this version can be made
7237 otherwise the call should go through the dispatcher. */
7239 if (DECL_FUNCTION_VERSIONED (fn)
7240 && (current_function_decl == NULL
7241 || !targetm.target_option.can_inline_p (current_function_decl, fn)))
7243 fn = get_function_version_dispatcher (fn);
7244 if (fn == NULL)
7245 return NULL;
7246 if (!already_used)
7247 mark_versions_used (fn);
7250 if (!already_used
7251 && !mark_used (fn))
7252 return error_mark_node;
7254 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
7255 /* Don't mess with virtual lookup in fold_non_dependent_expr; virtual
7256 functions can't be constexpr. */
7257 && !in_template_function ())
7259 tree t;
7260 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
7261 DECL_CONTEXT (fn),
7262 ba_any, NULL, complain);
7263 gcc_assert (binfo && binfo != error_mark_node);
7265 /* Warn about deprecated virtual functions now, since we're about
7266 to throw away the decl. */
7267 if (TREE_DEPRECATED (fn))
7268 warn_deprecated_use (fn, NULL_TREE);
7270 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
7271 complain);
7272 if (TREE_SIDE_EFFECTS (argarray[0]))
7273 argarray[0] = save_expr (argarray[0]);
7274 t = build_pointer_type (TREE_TYPE (fn));
7275 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
7276 fn = build_java_interface_fn_ref (fn, argarray[0]);
7277 else
7278 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
7279 TREE_TYPE (fn) = t;
7281 else
7283 fn = build_addr_func (fn, complain);
7284 if (fn == error_mark_node)
7285 return error_mark_node;
7288 return build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
7291 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
7292 This function performs no overload resolution, conversion, or other
7293 high-level operations. */
7295 tree
7296 build_cxx_call (tree fn, int nargs, tree *argarray,
7297 tsubst_flags_t complain)
7299 tree fndecl;
7300 int optimize_sav;
7302 /* Remember roughly where this call is. */
7303 location_t loc = EXPR_LOC_OR_LOC (fn, input_location);
7304 fn = build_call_a (fn, nargs, argarray);
7305 SET_EXPR_LOCATION (fn, loc);
7307 fndecl = get_callee_fndecl (fn);
7309 /* Check that arguments to builtin functions match the expectations. */
7310 if (fndecl
7311 && DECL_BUILT_IN (fndecl)
7312 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7313 && !check_builtin_function_arguments (fndecl, nargs, argarray))
7314 return error_mark_node;
7316 /* If it is a built-in array notation function, then the return type of
7317 the function is the element type of the array passed in as array
7318 notation (i.e. the first parameter of the function). */
7319 if (flag_cilkplus && TREE_CODE (fn) == CALL_EXPR)
7321 enum built_in_function bif =
7322 is_cilkplus_reduce_builtin (CALL_EXPR_FN (fn));
7323 if (bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
7324 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
7325 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
7326 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
7327 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE
7328 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
7330 /* for bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO or
7331 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO or
7332 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO or
7333 BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO or
7334 BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND or
7335 BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
7336 The pre-defined return-type is the correct one. */
7337 tree array_ntn = CALL_EXPR_ARG (fn, 0);
7338 TREE_TYPE (fn) = TREE_TYPE (array_ntn);
7339 return fn;
7343 /* Some built-in function calls will be evaluated at compile-time in
7344 fold (). Set optimize to 1 when folding __builtin_constant_p inside
7345 a constexpr function so that fold_builtin_1 doesn't fold it to 0. */
7346 optimize_sav = optimize;
7347 if (!optimize && fndecl && DECL_IS_BUILTIN_CONSTANT_P (fndecl)
7348 && current_function_decl
7349 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
7350 optimize = 1;
7351 fn = fold_if_not_in_template (fn);
7352 optimize = optimize_sav;
7354 if (VOID_TYPE_P (TREE_TYPE (fn)))
7355 return fn;
7357 /* 5.2.2/11: If a function call is a prvalue of object type: if the
7358 function call is either the operand of a decltype-specifier or the
7359 right operand of a comma operator that is the operand of a
7360 decltype-specifier, a temporary object is not introduced for the
7361 prvalue. The type of the prvalue may be incomplete. */
7362 if (!(complain & tf_decltype))
7364 fn = require_complete_type_sfinae (fn, complain);
7365 if (fn == error_mark_node)
7366 return error_mark_node;
7368 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
7369 fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
7371 return convert_from_reference (fn);
7374 static GTY(()) tree java_iface_lookup_fn;
7376 /* Make an expression which yields the address of the Java interface
7377 method FN. This is achieved by generating a call to libjava's
7378 _Jv_LookupInterfaceMethodIdx(). */
7380 static tree
7381 build_java_interface_fn_ref (tree fn, tree instance)
7383 tree lookup_fn, method, idx;
7384 tree klass_ref, iface, iface_ref;
7385 int i;
7387 if (!java_iface_lookup_fn)
7389 tree ftype = build_function_type_list (ptr_type_node,
7390 ptr_type_node, ptr_type_node,
7391 java_int_type_node, NULL_TREE);
7392 java_iface_lookup_fn
7393 = add_builtin_function ("_Jv_LookupInterfaceMethodIdx", ftype,
7394 0, NOT_BUILT_IN, NULL, NULL_TREE);
7397 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
7398 This is the first entry in the vtable. */
7399 klass_ref = build_vtbl_ref (cp_build_indirect_ref (instance, RO_NULL,
7400 tf_warning_or_error),
7401 integer_zero_node);
7403 /* Get the java.lang.Class pointer for the interface being called. */
7404 iface = DECL_CONTEXT (fn);
7405 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
7406 if (!iface_ref || !VAR_P (iface_ref)
7407 || DECL_CONTEXT (iface_ref) != iface)
7409 error ("could not find class$ field in java interface type %qT",
7410 iface);
7411 return error_mark_node;
7413 iface_ref = build_address (iface_ref);
7414 iface_ref = convert (build_pointer_type (iface), iface_ref);
7416 /* Determine the itable index of FN. */
7417 i = 1;
7418 for (method = TYPE_METHODS (iface); method; method = DECL_CHAIN (method))
7420 if (!DECL_VIRTUAL_P (method))
7421 continue;
7422 if (fn == method)
7423 break;
7424 i++;
7426 idx = build_int_cst (NULL_TREE, i);
7428 lookup_fn = build1 (ADDR_EXPR,
7429 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
7430 java_iface_lookup_fn);
7431 return build_call_nary (ptr_type_node, lookup_fn,
7432 3, klass_ref, iface_ref, idx);
7435 /* Returns the value to use for the in-charge parameter when making a
7436 call to a function with the indicated NAME.
7438 FIXME:Can't we find a neater way to do this mapping? */
7440 tree
7441 in_charge_arg_for_name (tree name)
7443 if (name == base_ctor_identifier
7444 || name == base_dtor_identifier)
7445 return integer_zero_node;
7446 else if (name == complete_ctor_identifier)
7447 return integer_one_node;
7448 else if (name == complete_dtor_identifier)
7449 return integer_two_node;
7450 else if (name == deleting_dtor_identifier)
7451 return integer_three_node;
7453 /* This function should only be called with one of the names listed
7454 above. */
7455 gcc_unreachable ();
7456 return NULL_TREE;
7459 /* Build a call to a constructor, destructor, or an assignment
7460 operator for INSTANCE, an expression with class type. NAME
7461 indicates the special member function to call; *ARGS are the
7462 arguments. ARGS may be NULL. This may change ARGS. BINFO
7463 indicates the base of INSTANCE that is to be passed as the `this'
7464 parameter to the member function called.
7466 FLAGS are the LOOKUP_* flags to use when processing the call.
7468 If NAME indicates a complete object constructor, INSTANCE may be
7469 NULL_TREE. In this case, the caller will call build_cplus_new to
7470 store the newly constructed object into a VAR_DECL. */
7472 tree
7473 build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
7474 tree binfo, int flags, tsubst_flags_t complain)
7476 tree fns;
7477 /* The type of the subobject to be constructed or destroyed. */
7478 tree class_type;
7479 vec<tree, va_gc> *allocated = NULL;
7480 tree ret;
7482 gcc_assert (name == complete_ctor_identifier
7483 || name == base_ctor_identifier
7484 || name == complete_dtor_identifier
7485 || name == base_dtor_identifier
7486 || name == deleting_dtor_identifier
7487 || name == ansi_assopname (NOP_EXPR));
7488 if (TYPE_P (binfo))
7490 /* Resolve the name. */
7491 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
7492 return error_mark_node;
7494 binfo = TYPE_BINFO (binfo);
7497 gcc_assert (binfo != NULL_TREE);
7499 class_type = BINFO_TYPE (binfo);
7501 /* Handle the special case where INSTANCE is NULL_TREE. */
7502 if (name == complete_ctor_identifier && !instance)
7504 instance = build_int_cst (build_pointer_type (class_type), 0);
7505 instance = build1 (INDIRECT_REF, class_type, instance);
7507 else
7509 if (name == complete_dtor_identifier
7510 || name == base_dtor_identifier
7511 || name == deleting_dtor_identifier)
7512 gcc_assert (args == NULL || vec_safe_is_empty (*args));
7514 /* Convert to the base class, if necessary. */
7515 if (!same_type_ignoring_top_level_qualifiers_p
7516 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
7518 if (name != ansi_assopname (NOP_EXPR))
7519 /* For constructors and destructors, either the base is
7520 non-virtual, or it is virtual but we are doing the
7521 conversion from a constructor or destructor for the
7522 complete object. In either case, we can convert
7523 statically. */
7524 instance = convert_to_base_statically (instance, binfo);
7525 else
7526 /* However, for assignment operators, we must convert
7527 dynamically if the base is virtual. */
7528 instance = build_base_path (PLUS_EXPR, instance,
7529 binfo, /*nonnull=*/1, complain);
7533 gcc_assert (instance != NULL_TREE);
7535 fns = lookup_fnfields (binfo, name, 1);
7537 /* When making a call to a constructor or destructor for a subobject
7538 that uses virtual base classes, pass down a pointer to a VTT for
7539 the subobject. */
7540 if ((name == base_ctor_identifier
7541 || name == base_dtor_identifier)
7542 && CLASSTYPE_VBASECLASSES (class_type))
7544 tree vtt;
7545 tree sub_vtt;
7547 /* If the current function is a complete object constructor
7548 or destructor, then we fetch the VTT directly.
7549 Otherwise, we look it up using the VTT we were given. */
7550 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
7551 vtt = decay_conversion (vtt, complain);
7552 if (vtt == error_mark_node)
7553 return error_mark_node;
7554 vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
7555 build2 (EQ_EXPR, boolean_type_node,
7556 current_in_charge_parm, integer_zero_node),
7557 current_vtt_parm,
7558 vtt);
7559 if (BINFO_SUBVTT_INDEX (binfo))
7560 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
7561 else
7562 sub_vtt = vtt;
7564 if (args == NULL)
7566 allocated = make_tree_vector ();
7567 args = &allocated;
7570 vec_safe_insert (*args, 0, sub_vtt);
7573 ret = build_new_method_call (instance, fns, args,
7574 TYPE_BINFO (BINFO_TYPE (binfo)),
7575 flags, /*fn=*/NULL,
7576 complain);
7578 if (allocated != NULL)
7579 release_tree_vector (allocated);
7581 if ((complain & tf_error)
7582 && (flags & LOOKUP_DELEGATING_CONS)
7583 && name == complete_ctor_identifier
7584 && TREE_CODE (ret) == CALL_EXPR
7585 && (DECL_ABSTRACT_ORIGIN (TREE_OPERAND (CALL_EXPR_FN (ret), 0))
7586 == current_function_decl))
7587 error ("constructor delegates to itself");
7589 return ret;
7592 /* Return the NAME, as a C string. The NAME indicates a function that
7593 is a member of TYPE. *FREE_P is set to true if the caller must
7594 free the memory returned.
7596 Rather than go through all of this, we should simply set the names
7597 of constructors and destructors appropriately, and dispense with
7598 ctor_identifier, dtor_identifier, etc. */
7600 static char *
7601 name_as_c_string (tree name, tree type, bool *free_p)
7603 char *pretty_name;
7605 /* Assume that we will not allocate memory. */
7606 *free_p = false;
7607 /* Constructors and destructors are special. */
7608 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
7610 pretty_name
7611 = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type))));
7612 /* For a destructor, add the '~'. */
7613 if (name == complete_dtor_identifier
7614 || name == base_dtor_identifier
7615 || name == deleting_dtor_identifier)
7617 pretty_name = concat ("~", pretty_name, NULL);
7618 /* Remember that we need to free the memory allocated. */
7619 *free_p = true;
7622 else if (IDENTIFIER_TYPENAME_P (name))
7624 pretty_name = concat ("operator ",
7625 type_as_string_translate (TREE_TYPE (name),
7626 TFF_PLAIN_IDENTIFIER),
7627 NULL);
7628 /* Remember that we need to free the memory allocated. */
7629 *free_p = true;
7631 else
7632 pretty_name = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (name)));
7634 return pretty_name;
7637 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
7638 be set, upon return, to the function called. ARGS may be NULL.
7639 This may change ARGS. */
7641 static tree
7642 build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
7643 tree conversion_path, int flags,
7644 tree *fn_p, tsubst_flags_t complain)
7646 struct z_candidate *candidates = 0, *cand;
7647 tree explicit_targs = NULL_TREE;
7648 tree basetype = NULL_TREE;
7649 tree access_binfo, binfo;
7650 tree optype;
7651 tree first_mem_arg = NULL_TREE;
7652 tree name;
7653 bool skip_first_for_error;
7654 vec<tree, va_gc> *user_args;
7655 tree call;
7656 tree fn;
7657 int template_only = 0;
7658 bool any_viable_p;
7659 tree orig_instance;
7660 tree orig_fns;
7661 vec<tree, va_gc> *orig_args = NULL;
7662 void *p;
7664 gcc_assert (instance != NULL_TREE);
7666 /* We don't know what function we're going to call, yet. */
7667 if (fn_p)
7668 *fn_p = NULL_TREE;
7670 if (error_operand_p (instance)
7671 || !fns || error_operand_p (fns))
7672 return error_mark_node;
7674 if (!BASELINK_P (fns))
7676 if (complain & tf_error)
7677 error ("call to non-function %qD", fns);
7678 return error_mark_node;
7681 orig_instance = instance;
7682 orig_fns = fns;
7684 /* Dismantle the baselink to collect all the information we need. */
7685 if (!conversion_path)
7686 conversion_path = BASELINK_BINFO (fns);
7687 access_binfo = BASELINK_ACCESS_BINFO (fns);
7688 binfo = BASELINK_BINFO (fns);
7689 optype = BASELINK_OPTYPE (fns);
7690 fns = BASELINK_FUNCTIONS (fns);
7691 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7693 explicit_targs = TREE_OPERAND (fns, 1);
7694 fns = TREE_OPERAND (fns, 0);
7695 template_only = 1;
7697 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
7698 || TREE_CODE (fns) == TEMPLATE_DECL
7699 || TREE_CODE (fns) == OVERLOAD);
7700 fn = get_first_fn (fns);
7701 name = DECL_NAME (fn);
7703 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
7704 gcc_assert (CLASS_TYPE_P (basetype));
7706 if (processing_template_decl)
7708 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
7709 instance = build_non_dependent_expr (instance);
7710 if (args != NULL)
7711 make_args_non_dependent (*args);
7714 user_args = args == NULL ? NULL : *args;
7715 /* Under DR 147 A::A() is an invalid constructor call,
7716 not a functional cast. */
7717 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
7719 if (! (complain & tf_error))
7720 return error_mark_node;
7722 if (permerror (input_location,
7723 "cannot call constructor %<%T::%D%> directly",
7724 basetype, name))
7725 inform (input_location, "for a function-style cast, remove the "
7726 "redundant %<::%D%>", name);
7727 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
7728 complain);
7729 return call;
7732 /* Figure out whether to skip the first argument for the error
7733 message we will display to users if an error occurs. We don't
7734 want to display any compiler-generated arguments. The "this"
7735 pointer hasn't been added yet. However, we must remove the VTT
7736 pointer if this is a call to a base-class constructor or
7737 destructor. */
7738 skip_first_for_error = false;
7739 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
7741 /* Callers should explicitly indicate whether they want to construct
7742 the complete object or just the part without virtual bases. */
7743 gcc_assert (name != ctor_identifier);
7744 /* Similarly for destructors. */
7745 gcc_assert (name != dtor_identifier);
7746 /* Remove the VTT pointer, if present. */
7747 if ((name == base_ctor_identifier || name == base_dtor_identifier)
7748 && CLASSTYPE_VBASECLASSES (basetype))
7749 skip_first_for_error = true;
7752 /* Process the argument list. */
7753 if (args != NULL && *args != NULL)
7755 *args = resolve_args (*args, complain);
7756 if (*args == NULL)
7757 return error_mark_node;
7760 /* Consider the object argument to be used even if we end up selecting a
7761 static member function. */
7762 instance = mark_type_use (instance);
7764 /* It's OK to call destructors and constructors on cv-qualified objects.
7765 Therefore, convert the INSTANCE to the unqualified type, if
7766 necessary. */
7767 if (DECL_DESTRUCTOR_P (fn)
7768 || DECL_CONSTRUCTOR_P (fn))
7770 if (!same_type_p (basetype, TREE_TYPE (instance)))
7772 instance = build_this (instance);
7773 instance = build_nop (build_pointer_type (basetype), instance);
7774 instance = build_fold_indirect_ref (instance);
7777 if (DECL_DESTRUCTOR_P (fn))
7778 name = complete_dtor_identifier;
7780 first_mem_arg = instance;
7782 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7783 p = conversion_obstack_alloc (0);
7785 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
7786 initializer, not T({ }). */
7787 if (DECL_CONSTRUCTOR_P (fn) && args != NULL && !vec_safe_is_empty (*args)
7788 && BRACE_ENCLOSED_INITIALIZER_P ((**args)[0])
7789 && CONSTRUCTOR_IS_DIRECT_INIT ((**args)[0]))
7791 tree init_list = (**args)[0];
7792 tree init = NULL_TREE;
7794 gcc_assert ((*args)->length () == 1
7795 && !(flags & LOOKUP_ONLYCONVERTING));
7797 /* If the initializer list has no elements and T is a class type with
7798 a default constructor, the object is value-initialized. Handle
7799 this here so we don't need to handle it wherever we use
7800 build_special_member_call. */
7801 if (CONSTRUCTOR_NELTS (init_list) == 0
7802 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
7803 /* For a user-provided default constructor, use the normal
7804 mechanisms so that protected access works. */
7805 && !type_has_user_provided_default_constructor (basetype)
7806 && !processing_template_decl)
7807 init = build_value_init (basetype, complain);
7809 /* If BASETYPE is an aggregate, we need to do aggregate
7810 initialization. */
7811 else if (CP_AGGREGATE_TYPE_P (basetype))
7812 init = digest_init (basetype, init_list, complain);
7814 if (init)
7816 if (INDIRECT_REF_P (instance)
7817 && integer_zerop (TREE_OPERAND (instance, 0)))
7818 return get_target_expr_sfinae (init, complain);
7819 init = build2 (INIT_EXPR, TREE_TYPE (instance), instance, init);
7820 TREE_SIDE_EFFECTS (init) = true;
7821 return init;
7824 /* Otherwise go ahead with overload resolution. */
7825 add_list_candidates (fns, first_mem_arg, init_list,
7826 basetype, explicit_targs, template_only,
7827 conversion_path, access_binfo, flags,
7828 &candidates, complain);
7830 else
7832 add_candidates (fns, first_mem_arg, user_args, optype,
7833 explicit_targs, template_only, conversion_path,
7834 access_binfo, flags, &candidates, complain);
7836 any_viable_p = false;
7837 candidates = splice_viable (candidates, pedantic, &any_viable_p);
7839 if (!any_viable_p)
7841 if (complain & tf_error)
7843 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
7844 cxx_incomplete_type_error (instance, basetype);
7845 else if (optype)
7846 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
7847 basetype, optype, build_tree_list_vec (user_args),
7848 TREE_TYPE (instance));
7849 else
7851 char *pretty_name;
7852 bool free_p;
7853 tree arglist;
7855 pretty_name = name_as_c_string (name, basetype, &free_p);
7856 arglist = build_tree_list_vec (user_args);
7857 if (skip_first_for_error)
7858 arglist = TREE_CHAIN (arglist);
7859 error ("no matching function for call to %<%T::%s(%A)%#V%>",
7860 basetype, pretty_name, arglist,
7861 TREE_TYPE (instance));
7862 if (free_p)
7863 free (pretty_name);
7865 print_z_candidates (location_of (name), candidates);
7867 call = error_mark_node;
7869 else
7871 cand = tourney (candidates, complain);
7872 if (cand == 0)
7874 char *pretty_name;
7875 bool free_p;
7876 tree arglist;
7878 if (complain & tf_error)
7880 pretty_name = name_as_c_string (name, basetype, &free_p);
7881 arglist = build_tree_list_vec (user_args);
7882 if (skip_first_for_error)
7883 arglist = TREE_CHAIN (arglist);
7884 error ("call of overloaded %<%s(%A)%> is ambiguous", pretty_name,
7885 arglist);
7886 print_z_candidates (location_of (name), candidates);
7887 if (free_p)
7888 free (pretty_name);
7890 call = error_mark_node;
7892 else
7894 fn = cand->fn;
7895 call = NULL_TREE;
7897 if (!(flags & LOOKUP_NONVIRTUAL)
7898 && DECL_PURE_VIRTUAL_P (fn)
7899 && instance == current_class_ref
7900 && (DECL_CONSTRUCTOR_P (current_function_decl)
7901 || DECL_DESTRUCTOR_P (current_function_decl))
7902 && (complain & tf_warning))
7903 /* This is not an error, it is runtime undefined
7904 behavior. */
7905 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl) ?
7906 "pure virtual %q#D called from constructor"
7907 : "pure virtual %q#D called from destructor"),
7908 fn);
7910 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
7911 && is_dummy_object (instance))
7913 instance = maybe_resolve_dummy (instance);
7914 if (instance == error_mark_node)
7915 call = error_mark_node;
7916 else if (!is_dummy_object (instance))
7918 /* We captured 'this' in the current lambda now that
7919 we know we really need it. */
7920 cand->first_arg = instance;
7922 else
7924 if (complain & tf_error)
7925 error ("cannot call member function %qD without object",
7926 fn);
7927 call = error_mark_node;
7931 if (call != error_mark_node)
7933 /* Optimize away vtable lookup if we know that this
7934 function can't be overridden. We need to check if
7935 the context and the type where we found fn are the same,
7936 actually FN might be defined in a different class
7937 type because of a using-declaration. In this case, we
7938 do not want to perform a non-virtual call. */
7939 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
7940 && same_type_ignoring_top_level_qualifiers_p
7941 (DECL_CONTEXT (fn), BINFO_TYPE (binfo))
7942 && resolves_to_fixed_type_p (instance, 0))
7943 flags |= LOOKUP_NONVIRTUAL;
7944 if (explicit_targs)
7945 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
7946 /* Now we know what function is being called. */
7947 if (fn_p)
7948 *fn_p = fn;
7949 /* Build the actual CALL_EXPR. */
7950 call = build_over_call (cand, flags, complain);
7951 /* In an expression of the form `a->f()' where `f' turns
7952 out to be a static member function, `a' is
7953 none-the-less evaluated. */
7954 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
7955 && !is_dummy_object (instance)
7956 && TREE_SIDE_EFFECTS (instance))
7957 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
7958 instance, call);
7959 else if (call != error_mark_node
7960 && DECL_DESTRUCTOR_P (cand->fn)
7961 && !VOID_TYPE_P (TREE_TYPE (call)))
7962 /* An explicit call of the form "x->~X()" has type
7963 "void". However, on platforms where destructors
7964 return "this" (i.e., those where
7965 targetm.cxx.cdtor_returns_this is true), such calls
7966 will appear to have a return value of pointer type
7967 to the low-level call machinery. We do not want to
7968 change the low-level machinery, since we want to be
7969 able to optimize "delete f()" on such platforms as
7970 "operator delete(~X(f()))" (rather than generating
7971 "t = f(), ~X(t), operator delete (t)"). */
7972 call = build_nop (void_type_node, call);
7977 if (processing_template_decl && call != error_mark_node)
7979 bool cast_to_void = false;
7981 if (TREE_CODE (call) == COMPOUND_EXPR)
7982 call = TREE_OPERAND (call, 1);
7983 else if (TREE_CODE (call) == NOP_EXPR)
7985 cast_to_void = true;
7986 call = TREE_OPERAND (call, 0);
7988 if (INDIRECT_REF_P (call))
7989 call = TREE_OPERAND (call, 0);
7990 call = (build_min_non_dep_call_vec
7991 (call,
7992 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
7993 orig_instance, orig_fns, NULL_TREE),
7994 orig_args));
7995 SET_EXPR_LOCATION (call, input_location);
7996 call = convert_from_reference (call);
7997 if (cast_to_void)
7998 call = build_nop (void_type_node, call);
8001 /* Free all the conversions we allocated. */
8002 obstack_free (&conversion_obstack, p);
8004 if (orig_args != NULL)
8005 release_tree_vector (orig_args);
8007 return call;
8010 /* Wrapper for above. */
8012 tree
8013 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
8014 tree conversion_path, int flags,
8015 tree *fn_p, tsubst_flags_t complain)
8017 tree ret;
8018 bool subtime = timevar_cond_start (TV_OVERLOAD);
8019 ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
8020 fn_p, complain);
8021 timevar_cond_stop (TV_OVERLOAD, subtime);
8022 return ret;
8025 /* Returns true iff standard conversion sequence ICS1 is a proper
8026 subsequence of ICS2. */
8028 static bool
8029 is_subseq (conversion *ics1, conversion *ics2)
8031 /* We can assume that a conversion of the same code
8032 between the same types indicates a subsequence since we only get
8033 here if the types we are converting from are the same. */
8035 while (ics1->kind == ck_rvalue
8036 || ics1->kind == ck_lvalue)
8037 ics1 = next_conversion (ics1);
8039 while (1)
8041 while (ics2->kind == ck_rvalue
8042 || ics2->kind == ck_lvalue)
8043 ics2 = next_conversion (ics2);
8045 if (ics2->kind == ck_user
8046 || ics2->kind == ck_ambig
8047 || ics2->kind == ck_aggr
8048 || ics2->kind == ck_list
8049 || ics2->kind == ck_identity)
8050 /* At this point, ICS1 cannot be a proper subsequence of
8051 ICS2. We can get a USER_CONV when we are comparing the
8052 second standard conversion sequence of two user conversion
8053 sequences. */
8054 return false;
8056 ics2 = next_conversion (ics2);
8058 if (ics2->kind == ics1->kind
8059 && same_type_p (ics2->type, ics1->type)
8060 && same_type_p (next_conversion (ics2)->type,
8061 next_conversion (ics1)->type))
8062 return true;
8066 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
8067 be any _TYPE nodes. */
8069 bool
8070 is_properly_derived_from (tree derived, tree base)
8072 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
8073 return false;
8075 /* We only allow proper derivation here. The DERIVED_FROM_P macro
8076 considers every class derived from itself. */
8077 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
8078 && DERIVED_FROM_P (base, derived));
8081 /* We build the ICS for an implicit object parameter as a pointer
8082 conversion sequence. However, such a sequence should be compared
8083 as if it were a reference conversion sequence. If ICS is the
8084 implicit conversion sequence for an implicit object parameter,
8085 modify it accordingly. */
8087 static void
8088 maybe_handle_implicit_object (conversion **ics)
8090 if ((*ics)->this_p)
8092 /* [over.match.funcs]
8094 For non-static member functions, the type of the
8095 implicit object parameter is "reference to cv X"
8096 where X is the class of which the function is a
8097 member and cv is the cv-qualification on the member
8098 function declaration. */
8099 conversion *t = *ics;
8100 tree reference_type;
8102 /* The `this' parameter is a pointer to a class type. Make the
8103 implicit conversion talk about a reference to that same class
8104 type. */
8105 reference_type = TREE_TYPE (t->type);
8106 reference_type = build_reference_type (reference_type);
8108 if (t->kind == ck_qual)
8109 t = next_conversion (t);
8110 if (t->kind == ck_ptr)
8111 t = next_conversion (t);
8112 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
8113 t = direct_reference_binding (reference_type, t);
8114 t->this_p = 1;
8115 t->rvaluedness_matches_p = 0;
8116 *ics = t;
8120 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
8121 and return the initial reference binding conversion. Otherwise,
8122 leave *ICS unchanged and return NULL. */
8124 static conversion *
8125 maybe_handle_ref_bind (conversion **ics)
8127 if ((*ics)->kind == ck_ref_bind)
8129 conversion *old_ics = *ics;
8130 *ics = next_conversion (old_ics);
8131 (*ics)->user_conv_p = old_ics->user_conv_p;
8132 return old_ics;
8135 return NULL;
8138 /* Compare two implicit conversion sequences according to the rules set out in
8139 [over.ics.rank]. Return values:
8141 1: ics1 is better than ics2
8142 -1: ics2 is better than ics1
8143 0: ics1 and ics2 are indistinguishable */
8145 static int
8146 compare_ics (conversion *ics1, conversion *ics2)
8148 tree from_type1;
8149 tree from_type2;
8150 tree to_type1;
8151 tree to_type2;
8152 tree deref_from_type1 = NULL_TREE;
8153 tree deref_from_type2 = NULL_TREE;
8154 tree deref_to_type1 = NULL_TREE;
8155 tree deref_to_type2 = NULL_TREE;
8156 conversion_rank rank1, rank2;
8158 /* REF_BINDING is nonzero if the result of the conversion sequence
8159 is a reference type. In that case REF_CONV is the reference
8160 binding conversion. */
8161 conversion *ref_conv1;
8162 conversion *ref_conv2;
8164 /* Handle implicit object parameters. */
8165 maybe_handle_implicit_object (&ics1);
8166 maybe_handle_implicit_object (&ics2);
8168 /* Handle reference parameters. */
8169 ref_conv1 = maybe_handle_ref_bind (&ics1);
8170 ref_conv2 = maybe_handle_ref_bind (&ics2);
8172 /* List-initialization sequence L1 is a better conversion sequence than
8173 list-initialization sequence L2 if L1 converts to
8174 std::initializer_list<X> for some X and L2 does not. */
8175 if (ics1->kind == ck_list && ics2->kind != ck_list)
8176 return 1;
8177 if (ics2->kind == ck_list && ics1->kind != ck_list)
8178 return -1;
8180 /* [over.ics.rank]
8182 When comparing the basic forms of implicit conversion sequences (as
8183 defined in _over.best.ics_)
8185 --a standard conversion sequence (_over.ics.scs_) is a better
8186 conversion sequence than a user-defined conversion sequence
8187 or an ellipsis conversion sequence, and
8189 --a user-defined conversion sequence (_over.ics.user_) is a
8190 better conversion sequence than an ellipsis conversion sequence
8191 (_over.ics.ellipsis_). */
8192 rank1 = CONVERSION_RANK (ics1);
8193 rank2 = CONVERSION_RANK (ics2);
8195 if (rank1 > rank2)
8196 return -1;
8197 else if (rank1 < rank2)
8198 return 1;
8200 if (rank1 == cr_bad)
8202 /* Both ICS are bad. We try to make a decision based on what would
8203 have happened if they'd been good. This is not an extension,
8204 we'll still give an error when we build up the call; this just
8205 helps us give a more helpful error message. */
8206 rank1 = BAD_CONVERSION_RANK (ics1);
8207 rank2 = BAD_CONVERSION_RANK (ics2);
8209 if (rank1 > rank2)
8210 return -1;
8211 else if (rank1 < rank2)
8212 return 1;
8214 /* We couldn't make up our minds; try to figure it out below. */
8217 if (ics1->ellipsis_p)
8218 /* Both conversions are ellipsis conversions. */
8219 return 0;
8221 /* User-defined conversion sequence U1 is a better conversion sequence
8222 than another user-defined conversion sequence U2 if they contain the
8223 same user-defined conversion operator or constructor and if the sec-
8224 ond standard conversion sequence of U1 is better than the second
8225 standard conversion sequence of U2. */
8227 /* Handle list-conversion with the same code even though it isn't always
8228 ranked as a user-defined conversion and it doesn't have a second
8229 standard conversion sequence; it will still have the desired effect.
8230 Specifically, we need to do the reference binding comparison at the
8231 end of this function. */
8233 if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
8235 conversion *t1;
8236 conversion *t2;
8238 for (t1 = ics1; t1->kind != ck_user; t1 = next_conversion (t1))
8239 if (t1->kind == ck_ambig || t1->kind == ck_aggr
8240 || t1->kind == ck_list)
8241 break;
8242 for (t2 = ics2; t2->kind != ck_user; t2 = next_conversion (t2))
8243 if (t2->kind == ck_ambig || t2->kind == ck_aggr
8244 || t2->kind == ck_list)
8245 break;
8247 if (t1->kind != t2->kind)
8248 return 0;
8249 else if (t1->kind == ck_user)
8251 if (t1->cand->fn != t2->cand->fn)
8252 return 0;
8254 else
8256 /* For ambiguous or aggregate conversions, use the target type as
8257 a proxy for the conversion function. */
8258 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
8259 return 0;
8262 /* We can just fall through here, after setting up
8263 FROM_TYPE1 and FROM_TYPE2. */
8264 from_type1 = t1->type;
8265 from_type2 = t2->type;
8267 else
8269 conversion *t1;
8270 conversion *t2;
8272 /* We're dealing with two standard conversion sequences.
8274 [over.ics.rank]
8276 Standard conversion sequence S1 is a better conversion
8277 sequence than standard conversion sequence S2 if
8279 --S1 is a proper subsequence of S2 (comparing the conversion
8280 sequences in the canonical form defined by _over.ics.scs_,
8281 excluding any Lvalue Transformation; the identity
8282 conversion sequence is considered to be a subsequence of
8283 any non-identity conversion sequence */
8285 t1 = ics1;
8286 while (t1->kind != ck_identity)
8287 t1 = next_conversion (t1);
8288 from_type1 = t1->type;
8290 t2 = ics2;
8291 while (t2->kind != ck_identity)
8292 t2 = next_conversion (t2);
8293 from_type2 = t2->type;
8296 /* One sequence can only be a subsequence of the other if they start with
8297 the same type. They can start with different types when comparing the
8298 second standard conversion sequence in two user-defined conversion
8299 sequences. */
8300 if (same_type_p (from_type1, from_type2))
8302 if (is_subseq (ics1, ics2))
8303 return 1;
8304 if (is_subseq (ics2, ics1))
8305 return -1;
8308 /* [over.ics.rank]
8310 Or, if not that,
8312 --the rank of S1 is better than the rank of S2 (by the rules
8313 defined below):
8315 Standard conversion sequences are ordered by their ranks: an Exact
8316 Match is a better conversion than a Promotion, which is a better
8317 conversion than a Conversion.
8319 Two conversion sequences with the same rank are indistinguishable
8320 unless one of the following rules applies:
8322 --A conversion that does not a convert a pointer, pointer to member,
8323 or std::nullptr_t to bool is better than one that does.
8325 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
8326 so that we do not have to check it explicitly. */
8327 if (ics1->rank < ics2->rank)
8328 return 1;
8329 else if (ics2->rank < ics1->rank)
8330 return -1;
8332 to_type1 = ics1->type;
8333 to_type2 = ics2->type;
8335 /* A conversion from scalar arithmetic type to complex is worse than a
8336 conversion between scalar arithmetic types. */
8337 if (same_type_p (from_type1, from_type2)
8338 && ARITHMETIC_TYPE_P (from_type1)
8339 && ARITHMETIC_TYPE_P (to_type1)
8340 && ARITHMETIC_TYPE_P (to_type2)
8341 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
8342 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
8344 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
8345 return -1;
8346 else
8347 return 1;
8350 if (TYPE_PTR_P (from_type1)
8351 && TYPE_PTR_P (from_type2)
8352 && TYPE_PTR_P (to_type1)
8353 && TYPE_PTR_P (to_type2))
8355 deref_from_type1 = TREE_TYPE (from_type1);
8356 deref_from_type2 = TREE_TYPE (from_type2);
8357 deref_to_type1 = TREE_TYPE (to_type1);
8358 deref_to_type2 = TREE_TYPE (to_type2);
8360 /* The rules for pointers to members A::* are just like the rules
8361 for pointers A*, except opposite: if B is derived from A then
8362 A::* converts to B::*, not vice versa. For that reason, we
8363 switch the from_ and to_ variables here. */
8364 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
8365 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
8366 || (TYPE_PTRMEMFUNC_P (from_type1)
8367 && TYPE_PTRMEMFUNC_P (from_type2)
8368 && TYPE_PTRMEMFUNC_P (to_type1)
8369 && TYPE_PTRMEMFUNC_P (to_type2)))
8371 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
8372 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
8373 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
8374 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
8377 if (deref_from_type1 != NULL_TREE
8378 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
8379 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
8381 /* This was one of the pointer or pointer-like conversions.
8383 [over.ics.rank]
8385 --If class B is derived directly or indirectly from class A,
8386 conversion of B* to A* is better than conversion of B* to
8387 void*, and conversion of A* to void* is better than
8388 conversion of B* to void*. */
8389 if (VOID_TYPE_P (deref_to_type1)
8390 && VOID_TYPE_P (deref_to_type2))
8392 if (is_properly_derived_from (deref_from_type1,
8393 deref_from_type2))
8394 return -1;
8395 else if (is_properly_derived_from (deref_from_type2,
8396 deref_from_type1))
8397 return 1;
8399 else if (VOID_TYPE_P (deref_to_type1)
8400 || VOID_TYPE_P (deref_to_type2))
8402 if (same_type_p (deref_from_type1, deref_from_type2))
8404 if (VOID_TYPE_P (deref_to_type2))
8406 if (is_properly_derived_from (deref_from_type1,
8407 deref_to_type1))
8408 return 1;
8410 /* We know that DEREF_TO_TYPE1 is `void' here. */
8411 else if (is_properly_derived_from (deref_from_type1,
8412 deref_to_type2))
8413 return -1;
8416 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
8417 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
8419 /* [over.ics.rank]
8421 --If class B is derived directly or indirectly from class A
8422 and class C is derived directly or indirectly from B,
8424 --conversion of C* to B* is better than conversion of C* to
8427 --conversion of B* to A* is better than conversion of C* to
8428 A* */
8429 if (same_type_p (deref_from_type1, deref_from_type2))
8431 if (is_properly_derived_from (deref_to_type1,
8432 deref_to_type2))
8433 return 1;
8434 else if (is_properly_derived_from (deref_to_type2,
8435 deref_to_type1))
8436 return -1;
8438 else if (same_type_p (deref_to_type1, deref_to_type2))
8440 if (is_properly_derived_from (deref_from_type2,
8441 deref_from_type1))
8442 return 1;
8443 else if (is_properly_derived_from (deref_from_type1,
8444 deref_from_type2))
8445 return -1;
8449 else if (CLASS_TYPE_P (non_reference (from_type1))
8450 && same_type_p (from_type1, from_type2))
8452 tree from = non_reference (from_type1);
8454 /* [over.ics.rank]
8456 --binding of an expression of type C to a reference of type
8457 B& is better than binding an expression of type C to a
8458 reference of type A&
8460 --conversion of C to B is better than conversion of C to A, */
8461 if (is_properly_derived_from (from, to_type1)
8462 && is_properly_derived_from (from, to_type2))
8464 if (is_properly_derived_from (to_type1, to_type2))
8465 return 1;
8466 else if (is_properly_derived_from (to_type2, to_type1))
8467 return -1;
8470 else if (CLASS_TYPE_P (non_reference (to_type1))
8471 && same_type_p (to_type1, to_type2))
8473 tree to = non_reference (to_type1);
8475 /* [over.ics.rank]
8477 --binding of an expression of type B to a reference of type
8478 A& is better than binding an expression of type C to a
8479 reference of type A&,
8481 --conversion of B to A is better than conversion of C to A */
8482 if (is_properly_derived_from (from_type1, to)
8483 && is_properly_derived_from (from_type2, to))
8485 if (is_properly_derived_from (from_type2, from_type1))
8486 return 1;
8487 else if (is_properly_derived_from (from_type1, from_type2))
8488 return -1;
8492 /* [over.ics.rank]
8494 --S1 and S2 differ only in their qualification conversion and yield
8495 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
8496 qualification signature of type T1 is a proper subset of the cv-
8497 qualification signature of type T2 */
8498 if (ics1->kind == ck_qual
8499 && ics2->kind == ck_qual
8500 && same_type_p (from_type1, from_type2))
8502 int result = comp_cv_qual_signature (to_type1, to_type2);
8503 if (result != 0)
8504 return result;
8507 /* [over.ics.rank]
8509 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
8510 to an implicit object parameter, and either S1 binds an lvalue reference
8511 to an lvalue and S2 binds an rvalue reference or S1 binds an rvalue
8512 reference to an rvalue and S2 binds an lvalue reference
8513 (C++0x draft standard, 13.3.3.2)
8515 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
8516 types to which the references refer are the same type except for
8517 top-level cv-qualifiers, and the type to which the reference
8518 initialized by S2 refers is more cv-qualified than the type to
8519 which the reference initialized by S1 refers.
8521 DR 1328 [over.match.best]: the context is an initialization by
8522 conversion function for direct reference binding (13.3.1.6) of a
8523 reference to function type, the return type of F1 is the same kind of
8524 reference (i.e. lvalue or rvalue) as the reference being initialized,
8525 and the return type of F2 is not. */
8527 if (ref_conv1 && ref_conv2)
8529 if (!ref_conv1->this_p && !ref_conv2->this_p
8530 && (ref_conv1->rvaluedness_matches_p
8531 != ref_conv2->rvaluedness_matches_p)
8532 && (same_type_p (ref_conv1->type, ref_conv2->type)
8533 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
8534 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
8536 return (ref_conv1->rvaluedness_matches_p
8537 - ref_conv2->rvaluedness_matches_p);
8540 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
8541 return comp_cv_qualification (TREE_TYPE (ref_conv2->type),
8542 TREE_TYPE (ref_conv1->type));
8545 /* Neither conversion sequence is better than the other. */
8546 return 0;
8549 /* The source type for this standard conversion sequence. */
8551 static tree
8552 source_type (conversion *t)
8554 for (;; t = next_conversion (t))
8556 if (t->kind == ck_user
8557 || t->kind == ck_ambig
8558 || t->kind == ck_identity)
8559 return t->type;
8561 gcc_unreachable ();
8564 /* Note a warning about preferring WINNER to LOSER. We do this by storing
8565 a pointer to LOSER and re-running joust to produce the warning if WINNER
8566 is actually used. */
8568 static void
8569 add_warning (struct z_candidate *winner, struct z_candidate *loser)
8571 candidate_warning *cw = (candidate_warning *)
8572 conversion_obstack_alloc (sizeof (candidate_warning));
8573 cw->loser = loser;
8574 cw->next = winner->warnings;
8575 winner->warnings = cw;
8578 // Returns the template declaration associated with the candidate
8579 // function. For actual templates, this is directly associated
8580 // with the candidate. For temploids, we return the template
8581 // associated with the specialization.
8582 static inline tree
8583 template_decl_for_candidate (struct z_candidate *cand)
8585 tree r = cand->template_decl;
8586 tree d = cand->fn;
8587 if (!r && DECL_P (d) && DECL_USE_TEMPLATE (d))
8588 r = DECL_TI_TEMPLATE (d);
8589 if (r && TREE_CODE (r) == TEMPLATE_INFO)
8590 r = TI_TEMPLATE (r);
8591 return r;
8594 /* Compare two candidates for overloading as described in
8595 [over.match.best]. Return values:
8597 1: cand1 is better than cand2
8598 -1: cand2 is better than cand1
8599 0: cand1 and cand2 are indistinguishable */
8601 static int
8602 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
8603 tsubst_flags_t complain)
8605 int winner = 0;
8606 int off1 = 0, off2 = 0;
8607 size_t i;
8608 size_t len;
8610 // Get the actual template decls associated with the candidates.
8611 tree tmpl1 = template_decl_for_candidate (cand1);
8612 tree tmpl2 = template_decl_for_candidate (cand2);
8614 /* Candidates that involve bad conversions are always worse than those
8615 that don't. */
8616 if (cand1->viable > cand2->viable)
8617 return 1;
8618 if (cand1->viable < cand2->viable)
8619 return -1;
8621 /* If we have two pseudo-candidates for conversions to the same type,
8622 or two candidates for the same function, arbitrarily pick one. */
8623 if (cand1->fn == cand2->fn
8624 && (IS_TYPE_OR_DECL_P (cand1->fn)))
8625 return 1;
8627 /* Prefer a non-deleted function over an implicitly deleted move
8628 constructor or assignment operator. This differs slightly from the
8629 wording for issue 1402 (which says the move op is ignored by overload
8630 resolution), but this way produces better error messages. */
8631 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
8632 && TREE_CODE (cand2->fn) == FUNCTION_DECL
8633 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
8635 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
8636 && move_fn_p (cand1->fn))
8637 return -1;
8638 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
8639 && move_fn_p (cand2->fn))
8640 return 1;
8643 /* a viable function F1
8644 is defined to be a better function than another viable function F2 if
8645 for all arguments i, ICSi(F1) is not a worse conversion sequence than
8646 ICSi(F2), and then */
8648 /* for some argument j, ICSj(F1) is a better conversion sequence than
8649 ICSj(F2) */
8651 /* For comparing static and non-static member functions, we ignore
8652 the implicit object parameter of the non-static function. The
8653 standard says to pretend that the static function has an object
8654 parm, but that won't work with operator overloading. */
8655 len = cand1->num_convs;
8656 if (len != cand2->num_convs)
8658 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
8659 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
8661 if (DECL_CONSTRUCTOR_P (cand1->fn)
8662 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
8663 /* We're comparing a near-match list constructor and a near-match
8664 non-list constructor. Just treat them as unordered. */
8665 return 0;
8667 gcc_assert (static_1 != static_2);
8669 if (static_1)
8670 off2 = 1;
8671 else
8673 off1 = 1;
8674 --len;
8678 for (i = 0; i < len; ++i)
8680 conversion *t1 = cand1->convs[i + off1];
8681 conversion *t2 = cand2->convs[i + off2];
8682 int comp = compare_ics (t1, t2);
8684 if (comp != 0)
8686 if ((complain & tf_warning)
8687 && warn_sign_promo
8688 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
8689 == cr_std + cr_promotion)
8690 && t1->kind == ck_std
8691 && t2->kind == ck_std
8692 && TREE_CODE (t1->type) == INTEGER_TYPE
8693 && TREE_CODE (t2->type) == INTEGER_TYPE
8694 && (TYPE_PRECISION (t1->type)
8695 == TYPE_PRECISION (t2->type))
8696 && (TYPE_UNSIGNED (next_conversion (t1)->type)
8697 || (TREE_CODE (next_conversion (t1)->type)
8698 == ENUMERAL_TYPE)))
8700 tree type = next_conversion (t1)->type;
8701 tree type1, type2;
8702 struct z_candidate *w, *l;
8703 if (comp > 0)
8704 type1 = t1->type, type2 = t2->type,
8705 w = cand1, l = cand2;
8706 else
8707 type1 = t2->type, type2 = t1->type,
8708 w = cand2, l = cand1;
8710 if (warn)
8712 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
8713 type, type1, type2);
8714 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
8716 else
8717 add_warning (w, l);
8720 if (winner && comp != winner)
8722 winner = 0;
8723 goto tweak;
8725 winner = comp;
8729 /* warn about confusing overload resolution for user-defined conversions,
8730 either between a constructor and a conversion op, or between two
8731 conversion ops. */
8732 if ((complain & tf_warning)
8733 && winner && warn_conversion && cand1->second_conv
8734 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
8735 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
8737 struct z_candidate *w, *l;
8738 bool give_warning = false;
8740 if (winner == 1)
8741 w = cand1, l = cand2;
8742 else
8743 w = cand2, l = cand1;
8745 /* We don't want to complain about `X::operator T1 ()'
8746 beating `X::operator T2 () const', when T2 is a no less
8747 cv-qualified version of T1. */
8748 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
8749 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
8751 tree t = TREE_TYPE (TREE_TYPE (l->fn));
8752 tree f = TREE_TYPE (TREE_TYPE (w->fn));
8754 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
8756 t = TREE_TYPE (t);
8757 f = TREE_TYPE (f);
8759 if (!comp_ptr_ttypes (t, f))
8760 give_warning = true;
8762 else
8763 give_warning = true;
8765 if (!give_warning)
8766 /*NOP*/;
8767 else if (warn)
8769 tree source = source_type (w->convs[0]);
8770 if (! DECL_CONSTRUCTOR_P (w->fn))
8771 source = TREE_TYPE (source);
8772 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
8773 && warning (OPT_Wconversion, " for conversion from %qT to %qT",
8774 source, w->second_conv->type))
8776 inform (input_location, " because conversion sequence for the argument is better");
8779 else
8780 add_warning (w, l);
8783 if (winner)
8784 return winner;
8786 /* DR 495 moved this tiebreaker above the template ones. */
8787 /* or, if not that,
8788 the context is an initialization by user-defined conversion (see
8789 _dcl.init_ and _over.match.user_) and the standard conversion
8790 sequence from the return type of F1 to the destination type (i.e.,
8791 the type of the entity being initialized) is a better conversion
8792 sequence than the standard conversion sequence from the return type
8793 of F2 to the destination type. */
8795 if (cand1->second_conv)
8797 winner = compare_ics (cand1->second_conv, cand2->second_conv);
8798 if (winner)
8799 return winner;
8802 /* or, if not that,
8803 F1 is a non-template function and F2 is a template function
8804 specialization. */
8806 if (!cand1->template_decl && cand2->template_decl)
8807 return 1;
8808 else if (cand1->template_decl && !cand2->template_decl)
8809 return -1;
8811 /* or, if not that,
8812 F1 and F2 are template functions and the function template for F1 is
8813 more specialized than the template for F2 according to the partial
8814 ordering rules. */
8816 if (tmpl1 && tmpl2)
8818 /* [temp.func.order]: The presence of unused ellipsis and default
8819 arguments has no effect on the partial ordering of function
8820 templates. add_function_candidate() will not have
8821 counted the "this" argument for constructors. */
8822 int nparms = cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn);
8823 winner = more_specialized_fn (tmpl1, tmpl2, nparms);
8824 if (winner)
8825 return winner;
8828 /* Check whether we can discard a builtin candidate, either because we
8829 have two identical ones or matching builtin and non-builtin candidates.
8831 (Pedantically in the latter case the builtin which matched the user
8832 function should not be added to the overload set, but we spot it here.
8834 [over.match.oper]
8835 ... the builtin candidates include ...
8836 - do not have the same parameter type list as any non-template
8837 non-member candidate. */
8839 if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
8841 for (i = 0; i < len; ++i)
8842 if (!same_type_p (cand1->convs[i]->type,
8843 cand2->convs[i]->type))
8844 break;
8845 if (i == cand1->num_convs)
8847 if (cand1->fn == cand2->fn)
8848 /* Two built-in candidates; arbitrarily pick one. */
8849 return 1;
8850 else if (identifier_p (cand1->fn))
8851 /* cand1 is built-in; prefer cand2. */
8852 return -1;
8853 else
8854 /* cand2 is built-in; prefer cand1. */
8855 return 1;
8859 /* For candidates of a multi-versioned function, make the version with
8860 the highest priority win. This version will be checked for dispatching
8861 first. If this version can be inlined into the caller, the front-end
8862 will simply make a direct call to this function. */
8864 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
8865 && DECL_FUNCTION_VERSIONED (cand1->fn)
8866 && TREE_CODE (cand2->fn) == FUNCTION_DECL
8867 && DECL_FUNCTION_VERSIONED (cand2->fn))
8869 tree f1 = TREE_TYPE (cand1->fn);
8870 tree f2 = TREE_TYPE (cand2->fn);
8871 tree p1 = TYPE_ARG_TYPES (f1);
8872 tree p2 = TYPE_ARG_TYPES (f2);
8874 /* Check if cand1->fn and cand2->fn are versions of the same function. It
8875 is possible that cand1->fn and cand2->fn are function versions but of
8876 different functions. Check types to see if they are versions of the same
8877 function. */
8878 if (compparms (p1, p2)
8879 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8881 /* Always make the version with the higher priority, more
8882 specialized, win. */
8883 gcc_assert (targetm.compare_version_priority);
8884 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
8885 return 1;
8886 else
8887 return -1;
8891 /* If the two function declarations represent the same function (this can
8892 happen with declarations in multiple scopes and arg-dependent lookup),
8893 arbitrarily choose one. But first make sure the default args we're
8894 using match. */
8895 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
8896 && equal_functions (cand1->fn, cand2->fn))
8898 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
8899 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
8901 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
8903 for (i = 0; i < len; ++i)
8905 /* Don't crash if the fn is variadic. */
8906 if (!parms1)
8907 break;
8908 parms1 = TREE_CHAIN (parms1);
8909 parms2 = TREE_CHAIN (parms2);
8912 if (off1)
8913 parms1 = TREE_CHAIN (parms1);
8914 else if (off2)
8915 parms2 = TREE_CHAIN (parms2);
8917 for (; parms1; ++i)
8919 if (!cp_tree_equal (TREE_PURPOSE (parms1),
8920 TREE_PURPOSE (parms2)))
8922 if (warn)
8924 if (complain & tf_error)
8926 if (permerror (input_location,
8927 "default argument mismatch in "
8928 "overload resolution"))
8930 inform (input_location,
8931 " candidate 1: %q+#F", cand1->fn);
8932 inform (input_location,
8933 " candidate 2: %q+#F", cand2->fn);
8936 else
8937 return 0;
8939 else
8940 add_warning (cand1, cand2);
8941 break;
8943 parms1 = TREE_CHAIN (parms1);
8944 parms2 = TREE_CHAIN (parms2);
8947 return 1;
8950 tweak:
8952 /* Extension: If the worst conversion for one candidate is worse than the
8953 worst conversion for the other, take the first. */
8954 if (!pedantic && (complain & tf_warning_or_error))
8956 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
8957 struct z_candidate *w = 0, *l = 0;
8959 for (i = 0; i < len; ++i)
8961 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
8962 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
8963 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
8964 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
8966 if (rank1 < rank2)
8967 winner = 1, w = cand1, l = cand2;
8968 if (rank1 > rank2)
8969 winner = -1, w = cand2, l = cand1;
8970 if (winner)
8972 /* Don't choose a deleted function over ambiguity. */
8973 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
8974 return 0;
8975 if (warn)
8977 pedwarn (input_location, 0,
8978 "ISO C++ says that these are ambiguous, even "
8979 "though the worst conversion for the first is better than "
8980 "the worst conversion for the second:");
8981 print_z_candidate (input_location, _("candidate 1:"), w);
8982 print_z_candidate (input_location, _("candidate 2:"), l);
8984 else
8985 add_warning (w, l);
8986 return winner;
8990 gcc_assert (!winner);
8991 return 0;
8994 /* Given a list of candidates for overloading, find the best one, if any.
8995 This algorithm has a worst case of O(2n) (winner is last), and a best
8996 case of O(n/2) (totally ambiguous); much better than a sorting
8997 algorithm. */
8999 static struct z_candidate *
9000 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
9002 struct z_candidate *champ = candidates, *challenger;
9003 int fate;
9004 int champ_compared_to_predecessor = 0;
9006 /* Walk through the list once, comparing each current champ to the next
9007 candidate, knocking out a candidate or two with each comparison. */
9009 for (challenger = champ->next; challenger; )
9011 fate = joust (champ, challenger, 0, complain);
9012 if (fate == 1)
9013 challenger = challenger->next;
9014 else
9016 if (fate == 0)
9018 champ = challenger->next;
9019 if (champ == 0)
9020 return NULL;
9021 champ_compared_to_predecessor = 0;
9023 else
9025 champ = challenger;
9026 champ_compared_to_predecessor = 1;
9029 challenger = champ->next;
9033 /* Make sure the champ is better than all the candidates it hasn't yet
9034 been compared to. */
9036 for (challenger = candidates;
9037 challenger != champ
9038 && !(champ_compared_to_predecessor && challenger->next == champ);
9039 challenger = challenger->next)
9041 fate = joust (champ, challenger, 0, complain);
9042 if (fate != 1)
9043 return NULL;
9046 return champ;
9050 // Returns true if things of type FROM can be implicitly converted to TO.
9051 bool
9052 can_convert (tree to, tree from, tsubst_flags_t complain)
9054 tree arg = NULL_TREE;
9055 /* implicit_conversion only considers user-defined conversions
9056 if it has an expression for the call argument list. */
9057 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
9058 arg = build1 (CAST_EXPR, from, NULL_TREE);
9059 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
9062 /* Returns nonzero if things of type FROM can be converted to TO with a
9063 standard conversion. */
9065 bool
9066 can_convert_standard (tree to, tree from, tsubst_flags_t complain)
9068 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
9071 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
9073 bool
9074 can_convert_arg (tree to, tree from, tree arg, int flags,
9075 tsubst_flags_t complain)
9077 conversion *t;
9078 void *p;
9079 bool ok_p;
9081 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9082 p = conversion_obstack_alloc (0);
9083 /* We want to discard any access checks done for this test,
9084 as we might not be in the appropriate access context and
9085 we'll do the check again when we actually perform the
9086 conversion. */
9087 push_deferring_access_checks (dk_deferred);
9089 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
9090 flags, complain);
9091 ok_p = (t && !t->bad_p);
9093 /* Discard the access checks now. */
9094 pop_deferring_access_checks ();
9095 /* Free all the conversions we allocated. */
9096 obstack_free (&conversion_obstack, p);
9098 return ok_p;
9101 /* Like can_convert_arg, but allows dubious conversions as well. */
9103 bool
9104 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
9105 tsubst_flags_t complain)
9107 conversion *t;
9108 void *p;
9110 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9111 p = conversion_obstack_alloc (0);
9112 /* Try to perform the conversion. */
9113 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
9114 flags, complain);
9115 /* Free all the conversions we allocated. */
9116 obstack_free (&conversion_obstack, p);
9118 return t != NULL;
9121 /* Convert EXPR to TYPE. Return the converted expression.
9123 Note that we allow bad conversions here because by the time we get to
9124 this point we are committed to doing the conversion. If we end up
9125 doing a bad conversion, convert_like will complain. */
9127 tree
9128 perform_implicit_conversion_flags (tree type, tree expr,
9129 tsubst_flags_t complain, int flags)
9131 conversion *conv;
9132 void *p;
9133 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
9135 if (error_operand_p (expr))
9136 return error_mark_node;
9138 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9139 p = conversion_obstack_alloc (0);
9141 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
9142 /*c_cast_p=*/false,
9143 flags, complain);
9145 if (!conv)
9147 if (complain & tf_error)
9149 /* If expr has unknown type, then it is an overloaded function.
9150 Call instantiate_type to get good error messages. */
9151 if (TREE_TYPE (expr) == unknown_type_node)
9152 instantiate_type (type, expr, complain);
9153 else if (invalid_nonstatic_memfn_p (expr, complain))
9154 /* We gave an error. */;
9155 else
9156 error_at (loc, "could not convert %qE from %qT to %qT", expr,
9157 TREE_TYPE (expr), type);
9159 expr = error_mark_node;
9161 else if (processing_template_decl && conv->kind != ck_identity)
9163 /* In a template, we are only concerned about determining the
9164 type of non-dependent expressions, so we do not have to
9165 perform the actual conversion. But for initializers, we
9166 need to be able to perform it at instantiation
9167 (or fold_non_dependent_expr) time. */
9168 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
9169 if (!(flags & LOOKUP_ONLYCONVERTING))
9170 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
9172 else
9173 expr = convert_like (conv, expr, complain);
9175 /* Free all the conversions we allocated. */
9176 obstack_free (&conversion_obstack, p);
9178 return expr;
9181 tree
9182 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
9184 return perform_implicit_conversion_flags (type, expr, complain,
9185 LOOKUP_IMPLICIT);
9188 /* Convert EXPR to TYPE (as a direct-initialization) if that is
9189 permitted. If the conversion is valid, the converted expression is
9190 returned. Otherwise, NULL_TREE is returned, except in the case
9191 that TYPE is a class type; in that case, an error is issued. If
9192 C_CAST_P is true, then this direct-initialization is taking
9193 place as part of a static_cast being attempted as part of a C-style
9194 cast. */
9196 tree
9197 perform_direct_initialization_if_possible (tree type,
9198 tree expr,
9199 bool c_cast_p,
9200 tsubst_flags_t complain)
9202 conversion *conv;
9203 void *p;
9205 if (type == error_mark_node || error_operand_p (expr))
9206 return error_mark_node;
9207 /* [dcl.init]
9209 If the destination type is a (possibly cv-qualified) class type:
9211 -- If the initialization is direct-initialization ...,
9212 constructors are considered. ... If no constructor applies, or
9213 the overload resolution is ambiguous, the initialization is
9214 ill-formed. */
9215 if (CLASS_TYPE_P (type))
9217 vec<tree, va_gc> *args = make_tree_vector_single (expr);
9218 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
9219 &args, type, LOOKUP_NORMAL, complain);
9220 release_tree_vector (args);
9221 return build_cplus_new (type, expr, complain);
9224 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9225 p = conversion_obstack_alloc (0);
9227 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
9228 c_cast_p,
9229 LOOKUP_NORMAL, complain);
9230 if (!conv || conv->bad_p)
9231 expr = NULL_TREE;
9232 else
9233 expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
9234 /*issue_conversion_warnings=*/false,
9235 c_cast_p,
9236 complain);
9238 /* Free all the conversions we allocated. */
9239 obstack_free (&conversion_obstack, p);
9241 return expr;
9244 /* When initializing a reference that lasts longer than a full-expression,
9245 this special rule applies:
9247 [class.temporary]
9249 The temporary to which the reference is bound or the temporary
9250 that is the complete object to which the reference is bound
9251 persists for the lifetime of the reference.
9253 The temporaries created during the evaluation of the expression
9254 initializing the reference, except the temporary to which the
9255 reference is bound, are destroyed at the end of the
9256 full-expression in which they are created.
9258 In that case, we store the converted expression into a new
9259 VAR_DECL in a new scope.
9261 However, we want to be careful not to create temporaries when
9262 they are not required. For example, given:
9264 struct B {};
9265 struct D : public B {};
9266 D f();
9267 const B& b = f();
9269 there is no need to copy the return value from "f"; we can just
9270 extend its lifetime. Similarly, given:
9272 struct S {};
9273 struct T { operator S(); };
9274 T t;
9275 const S& s = t;
9277 we can extend the lifetime of the return value of the conversion
9278 operator.
9280 The next several functions are involved in this lifetime extension. */
9282 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
9283 reference is being bound to a temporary. Create and return a new
9284 VAR_DECL with the indicated TYPE; this variable will store the value to
9285 which the reference is bound. */
9287 tree
9288 make_temporary_var_for_ref_to_temp (tree decl, tree type)
9290 tree var;
9292 /* Create the variable. */
9293 var = create_temporary_var (type);
9295 /* Register the variable. */
9296 if (VAR_P (decl)
9297 && (TREE_STATIC (decl) || DECL_THREAD_LOCAL_P (decl)))
9299 /* Namespace-scope or local static; give it a mangled name. */
9300 /* FIXME share comdat with decl? */
9301 tree name;
9303 TREE_STATIC (var) = TREE_STATIC (decl);
9304 DECL_TLS_MODEL (var) = DECL_TLS_MODEL (decl);
9305 name = mangle_ref_init_variable (decl);
9306 DECL_NAME (var) = name;
9307 SET_DECL_ASSEMBLER_NAME (var, name);
9308 var = pushdecl_top_level (var);
9310 else
9311 /* Create a new cleanup level if necessary. */
9312 maybe_push_cleanup_level (type);
9314 return var;
9317 /* EXPR is the initializer for a variable DECL of reference or
9318 std::initializer_list type. Create, push and return a new VAR_DECL
9319 for the initializer so that it will live as long as DECL. Any
9320 cleanup for the new variable is returned through CLEANUP, and the
9321 code to initialize the new variable is returned through INITP. */
9323 static tree
9324 set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
9325 tree *initp)
9327 tree init;
9328 tree type;
9329 tree var;
9331 /* Create the temporary variable. */
9332 type = TREE_TYPE (expr);
9333 var = make_temporary_var_for_ref_to_temp (decl, type);
9334 layout_decl (var, 0);
9335 /* If the rvalue is the result of a function call it will be
9336 a TARGET_EXPR. If it is some other construct (such as a
9337 member access expression where the underlying object is
9338 itself the result of a function call), turn it into a
9339 TARGET_EXPR here. It is important that EXPR be a
9340 TARGET_EXPR below since otherwise the INIT_EXPR will
9341 attempt to make a bitwise copy of EXPR to initialize
9342 VAR. */
9343 if (TREE_CODE (expr) != TARGET_EXPR)
9344 expr = get_target_expr (expr);
9346 if (TREE_CODE (decl) == FIELD_DECL
9347 && extra_warnings && !TREE_NO_WARNING (decl))
9349 warning (OPT_Wextra, "a temporary bound to %qD only persists "
9350 "until the constructor exits", decl);
9351 TREE_NO_WARNING (decl) = true;
9354 /* Recursively extend temps in this initializer. */
9355 TARGET_EXPR_INITIAL (expr)
9356 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups);
9358 /* Any reference temp has a non-trivial initializer. */
9359 DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
9361 /* If the initializer is constant, put it in DECL_INITIAL so we get
9362 static initialization and use in constant expressions. */
9363 init = maybe_constant_init (expr);
9364 if (TREE_CONSTANT (init))
9366 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
9368 /* 5.19 says that a constant expression can include an
9369 lvalue-rvalue conversion applied to "a glvalue of literal type
9370 that refers to a non-volatile temporary object initialized
9371 with a constant expression". Rather than try to communicate
9372 that this VAR_DECL is a temporary, just mark it constexpr.
9374 Currently this is only useful for initializer_list temporaries,
9375 since reference vars can't appear in constant expressions. */
9376 DECL_DECLARED_CONSTEXPR_P (var) = true;
9377 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
9378 TREE_CONSTANT (var) = true;
9380 DECL_INITIAL (var) = init;
9381 init = NULL_TREE;
9383 else
9384 /* Create the INIT_EXPR that will initialize the temporary
9385 variable. */
9386 init = build2 (INIT_EXPR, type, var, expr);
9387 if (at_function_scope_p ())
9389 add_decl_expr (var);
9391 if (TREE_STATIC (var))
9392 init = add_stmt_to_compound (init, register_dtor_fn (var));
9393 else
9395 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
9396 if (cleanup)
9397 vec_safe_push (*cleanups, cleanup);
9400 /* We must be careful to destroy the temporary only
9401 after its initialization has taken place. If the
9402 initialization throws an exception, then the
9403 destructor should not be run. We cannot simply
9404 transform INIT into something like:
9406 (INIT, ({ CLEANUP_STMT; }))
9408 because emit_local_var always treats the
9409 initializer as a full-expression. Thus, the
9410 destructor would run too early; it would run at the
9411 end of initializing the reference variable, rather
9412 than at the end of the block enclosing the
9413 reference variable.
9415 The solution is to pass back a cleanup expression
9416 which the caller is responsible for attaching to
9417 the statement tree. */
9419 else
9421 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
9422 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9424 if (DECL_THREAD_LOCAL_P (var))
9425 tls_aggregates = tree_cons (NULL_TREE, var,
9426 tls_aggregates);
9427 else
9428 static_aggregates = tree_cons (NULL_TREE, var,
9429 static_aggregates);
9431 else
9432 /* Check whether the dtor is callable. */
9433 cxx_maybe_build_cleanup (var, tf_warning_or_error);
9436 *initp = init;
9437 return var;
9440 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
9441 initializing a variable of that TYPE. */
9443 tree
9444 initialize_reference (tree type, tree expr,
9445 int flags, tsubst_flags_t complain)
9447 conversion *conv;
9448 void *p;
9449 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
9451 if (type == error_mark_node || error_operand_p (expr))
9452 return error_mark_node;
9454 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9455 p = conversion_obstack_alloc (0);
9457 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
9458 flags, complain);
9459 if (!conv || conv->bad_p)
9461 if (complain & tf_error)
9463 if (conv)
9464 convert_like (conv, expr, complain);
9465 else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
9466 && !TYPE_REF_IS_RVALUE (type)
9467 && !real_lvalue_p (expr))
9468 error_at (loc, "invalid initialization of non-const reference of "
9469 "type %qT from an rvalue of type %qT",
9470 type, TREE_TYPE (expr));
9471 else
9472 error_at (loc, "invalid initialization of reference of type "
9473 "%qT from expression of type %qT", type,
9474 TREE_TYPE (expr));
9476 return error_mark_node;
9479 if (conv->kind == ck_ref_bind)
9480 /* Perform the conversion. */
9481 expr = convert_like (conv, expr, complain);
9482 else if (conv->kind == ck_ambig)
9483 /* We gave an error in build_user_type_conversion_1. */
9484 expr = error_mark_node;
9485 else
9486 gcc_unreachable ();
9488 /* Free all the conversions we allocated. */
9489 obstack_free (&conversion_obstack, p);
9491 return expr;
9494 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
9495 which is bound either to a reference or a std::initializer_list. */
9497 static tree
9498 extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups)
9500 tree sub = init;
9501 tree *p;
9502 STRIP_NOPS (sub);
9503 if (TREE_CODE (sub) == COMPOUND_EXPR)
9505 TREE_OPERAND (sub, 1)
9506 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups);
9507 return init;
9509 if (TREE_CODE (sub) != ADDR_EXPR)
9510 return init;
9511 /* Deal with binding to a subobject. */
9512 for (p = &TREE_OPERAND (sub, 0); TREE_CODE (*p) == COMPONENT_REF; )
9513 p = &TREE_OPERAND (*p, 0);
9514 if (TREE_CODE (*p) == TARGET_EXPR)
9516 tree subinit = NULL_TREE;
9517 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit);
9518 if (subinit)
9519 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
9520 recompute_tree_invariant_for_addr_expr (sub);
9522 return init;
9525 /* INIT is part of the initializer for DECL. If there are any
9526 reference or initializer lists being initialized, extend their
9527 lifetime to match that of DECL. */
9529 tree
9530 extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
9532 tree type = TREE_TYPE (init);
9533 if (processing_template_decl)
9534 return init;
9535 if (TREE_CODE (type) == REFERENCE_TYPE)
9536 init = extend_ref_init_temps_1 (decl, init, cleanups);
9537 else if (is_std_init_list (type))
9539 /* The temporary array underlying a std::initializer_list
9540 is handled like a reference temporary. */
9541 tree ctor = init;
9542 if (TREE_CODE (ctor) == TARGET_EXPR)
9543 ctor = TARGET_EXPR_INITIAL (ctor);
9544 if (TREE_CODE (ctor) == CONSTRUCTOR)
9546 tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
9547 array = extend_ref_init_temps_1 (decl, array, cleanups);
9548 CONSTRUCTOR_ELT (ctor, 0)->value = array;
9551 else if (TREE_CODE (init) == CONSTRUCTOR)
9553 unsigned i;
9554 constructor_elt *p;
9555 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
9556 FOR_EACH_VEC_SAFE_ELT (elts, i, p)
9557 p->value = extend_ref_init_temps (decl, p->value, cleanups);
9560 return init;
9563 /* Returns true iff an initializer for TYPE could contain temporaries that
9564 need to be extended because they are bound to references or
9565 std::initializer_list. */
9567 bool
9568 type_has_extended_temps (tree type)
9570 type = strip_array_types (type);
9571 if (TREE_CODE (type) == REFERENCE_TYPE)
9572 return true;
9573 if (CLASS_TYPE_P (type))
9575 if (is_std_init_list (type))
9576 return true;
9577 for (tree f = next_initializable_field (TYPE_FIELDS (type));
9578 f; f = next_initializable_field (DECL_CHAIN (f)))
9579 if (type_has_extended_temps (TREE_TYPE (f)))
9580 return true;
9582 return false;
9585 /* Returns true iff TYPE is some variant of std::initializer_list. */
9587 bool
9588 is_std_init_list (tree type)
9590 /* Look through typedefs. */
9591 if (!TYPE_P (type))
9592 return false;
9593 if (cxx_dialect == cxx98)
9594 return false;
9595 type = TYPE_MAIN_VARIANT (type);
9596 return (CLASS_TYPE_P (type)
9597 && CP_TYPE_CONTEXT (type) == std_node
9598 && strcmp (TYPE_NAME_STRING (type), "initializer_list") == 0);
9601 /* Returns true iff DECL is a list constructor: i.e. a constructor which
9602 will accept an argument list of a single std::initializer_list<T>. */
9604 bool
9605 is_list_ctor (tree decl)
9607 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
9608 tree arg;
9610 if (!args || args == void_list_node)
9611 return false;
9613 arg = non_reference (TREE_VALUE (args));
9614 if (!is_std_init_list (arg))
9615 return false;
9617 args = TREE_CHAIN (args);
9619 if (args && args != void_list_node && !TREE_PURPOSE (args))
9620 /* There are more non-defaulted parms. */
9621 return false;
9623 return true;
9626 #include "gt-cp-call.h"