Merge from trunk.
[official-gcc.git] / gcc / cp / call.c
blobdb6af39174aa0c328593403bedcc87425c750ff2
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987-2013 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 "cp-tree.h"
31 #include "flags.h"
32 #include "toplev.h"
33 #include "diagnostic-core.h"
34 #include "intl.h"
35 #include "target.h"
36 #include "convert.h"
37 #include "langhooks.h"
38 #include "c-family/c-objc.h"
39 #include "timevar.h"
40 #include "cgraph.h"
42 /* The various kinds of conversion. */
44 typedef enum conversion_kind {
45 ck_identity,
46 ck_lvalue,
47 ck_qual,
48 ck_std,
49 ck_ptr,
50 ck_pmem,
51 ck_base,
52 ck_ref_bind,
53 ck_user,
54 ck_ambig,
55 ck_list,
56 ck_aggr,
57 ck_rvalue
58 } conversion_kind;
60 /* The rank of the conversion. Order of the enumerals matters; better
61 conversions should come earlier in the list. */
63 typedef enum conversion_rank {
64 cr_identity,
65 cr_exact,
66 cr_promotion,
67 cr_std,
68 cr_pbool,
69 cr_user,
70 cr_ellipsis,
71 cr_bad
72 } conversion_rank;
74 /* An implicit conversion sequence, in the sense of [over.best.ics].
75 The first conversion to be performed is at the end of the chain.
76 That conversion is always a cr_identity conversion. */
78 typedef struct conversion conversion;
79 struct conversion {
80 /* The kind of conversion represented by this step. */
81 conversion_kind kind;
82 /* The rank of this conversion. */
83 conversion_rank rank;
84 BOOL_BITFIELD user_conv_p : 1;
85 BOOL_BITFIELD ellipsis_p : 1;
86 BOOL_BITFIELD this_p : 1;
87 /* True if this conversion would be permitted with a bending of
88 language standards, e.g. disregarding pointer qualifiers or
89 converting integers to pointers. */
90 BOOL_BITFIELD bad_p : 1;
91 /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
92 temporary should be created to hold the result of the
93 conversion. */
94 BOOL_BITFIELD need_temporary_p : 1;
95 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
96 from a pointer-to-derived to pointer-to-base is being performed. */
97 BOOL_BITFIELD base_p : 1;
98 /* If KIND is ck_ref_bind, true when either an lvalue reference is
99 being bound to an lvalue expression or an rvalue reference is
100 being bound to an rvalue expression. If KIND is ck_rvalue,
101 true when we should treat an lvalue as an rvalue (12.8p33). If
102 KIND is ck_base, always false. */
103 BOOL_BITFIELD rvaluedness_matches_p: 1;
104 BOOL_BITFIELD check_narrowing: 1;
105 /* The type of the expression resulting from the conversion. */
106 tree type;
107 union {
108 /* The next conversion in the chain. Since the conversions are
109 arranged from outermost to innermost, the NEXT conversion will
110 actually be performed before this conversion. This variant is
111 used only when KIND is neither ck_identity, ck_ambig nor
112 ck_list. Please use the next_conversion function instead
113 of using this field directly. */
114 conversion *next;
115 /* The expression at the beginning of the conversion chain. This
116 variant is used only if KIND is ck_identity or ck_ambig. */
117 tree expr;
118 /* The array of conversions for an initializer_list, so this
119 variant is used only when KIN D is ck_list. */
120 conversion **list;
121 } u;
122 /* The function candidate corresponding to this conversion
123 sequence. This field is only used if KIND is ck_user. */
124 struct z_candidate *cand;
127 #define CONVERSION_RANK(NODE) \
128 ((NODE)->bad_p ? cr_bad \
129 : (NODE)->ellipsis_p ? cr_ellipsis \
130 : (NODE)->user_conv_p ? cr_user \
131 : (NODE)->rank)
133 #define BAD_CONVERSION_RANK(NODE) \
134 ((NODE)->ellipsis_p ? cr_ellipsis \
135 : (NODE)->user_conv_p ? cr_user \
136 : (NODE)->rank)
138 static struct obstack conversion_obstack;
139 static bool conversion_obstack_initialized;
140 struct rejection_reason;
142 static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
143 static int equal_functions (tree, tree);
144 static int joust (struct z_candidate *, struct z_candidate *, bool,
145 tsubst_flags_t);
146 static int compare_ics (conversion *, conversion *);
147 static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
148 static tree build_java_interface_fn_ref (tree, tree);
149 #define convert_like(CONV, EXPR, COMPLAIN) \
150 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \
151 /*issue_conversion_warnings=*/true, \
152 /*c_cast_p=*/false, (COMPLAIN))
153 #define convert_like_with_context(CONV, EXPR, FN, ARGNO, COMPLAIN ) \
154 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
155 /*issue_conversion_warnings=*/true, \
156 /*c_cast_p=*/false, (COMPLAIN))
157 static tree convert_like_real (conversion *, tree, tree, int, int, bool,
158 bool, tsubst_flags_t);
159 static void op_error (location_t, enum tree_code, enum tree_code, tree,
160 tree, tree, bool);
161 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
162 tsubst_flags_t);
163 static void print_z_candidate (location_t, const char *, struct z_candidate *);
164 static void print_z_candidates (location_t, struct z_candidate *);
165 static tree build_this (tree);
166 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
167 static bool any_strictly_viable (struct z_candidate *);
168 static struct z_candidate *add_template_candidate
169 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
170 tree, tree, tree, int, unification_kind_t, tsubst_flags_t);
171 static struct z_candidate *add_template_candidate_real
172 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
173 tree, tree, tree, int, tree, unification_kind_t, tsubst_flags_t);
174 static struct z_candidate *add_template_conv_candidate
175 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *,
176 tree, tree, tree, tsubst_flags_t);
177 static void add_builtin_candidates
178 (struct z_candidate **, enum tree_code, enum tree_code,
179 tree, tree *, int, tsubst_flags_t);
180 static void add_builtin_candidate
181 (struct z_candidate **, enum tree_code, enum tree_code,
182 tree, tree, tree, tree *, tree *, int, tsubst_flags_t);
183 static bool is_complete (tree);
184 static void build_builtin_candidate
185 (struct z_candidate **, tree, tree, tree, tree *, tree *,
186 int, tsubst_flags_t);
187 static struct z_candidate *add_conv_candidate
188 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
189 tree, tsubst_flags_t);
190 static struct z_candidate *add_function_candidate
191 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
192 tree, int, tsubst_flags_t);
193 static conversion *implicit_conversion (tree, tree, tree, bool, int,
194 tsubst_flags_t);
195 static conversion *standard_conversion (tree, tree, tree, bool, int);
196 static conversion *reference_binding (tree, tree, tree, bool, int,
197 tsubst_flags_t);
198 static conversion *build_conv (conversion_kind, tree, conversion *);
199 static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
200 static conversion *next_conversion (conversion *);
201 static bool is_subseq (conversion *, conversion *);
202 static conversion *maybe_handle_ref_bind (conversion **);
203 static void maybe_handle_implicit_object (conversion **);
204 static struct z_candidate *add_candidate
205 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
206 conversion **, tree, tree, int, struct rejection_reason *);
207 static tree source_type (conversion *);
208 static void add_warning (struct z_candidate *, struct z_candidate *);
209 static bool reference_compatible_p (tree, tree);
210 static conversion *direct_reference_binding (tree, conversion *);
211 static bool promoted_arithmetic_type_p (tree);
212 static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
213 static char *name_as_c_string (tree, tree, bool *);
214 static tree prep_operand (tree);
215 static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
216 bool, tree, tree, int, struct z_candidate **,
217 tsubst_flags_t);
218 static conversion *merge_conversion_sequences (conversion *, conversion *);
219 static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
221 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
222 NAME can take many forms... */
224 bool
225 check_dtor_name (tree basetype, tree name)
227 /* Just accept something we've already complained about. */
228 if (name == error_mark_node)
229 return true;
231 if (TREE_CODE (name) == TYPE_DECL)
232 name = TREE_TYPE (name);
233 else if (TYPE_P (name))
234 /* OK */;
235 else if (identifier_p (name))
237 if ((MAYBE_CLASS_TYPE_P (basetype)
238 && name == constructor_name (basetype))
239 || (TREE_CODE (basetype) == ENUMERAL_TYPE
240 && name == TYPE_IDENTIFIER (basetype)))
241 return true;
242 else
243 name = get_type_value (name);
245 else
247 /* In the case of:
249 template <class T> struct S { ~S(); };
250 int i;
251 i.~S();
253 NAME will be a class template. */
254 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
255 return false;
258 if (!name || name == error_mark_node)
259 return false;
260 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
263 /* We want the address of a function or method. We avoid creating a
264 pointer-to-member function. */
266 tree
267 build_addr_func (tree function, tsubst_flags_t complain)
269 tree type = TREE_TYPE (function);
271 /* We have to do these by hand to avoid real pointer to member
272 functions. */
273 if (TREE_CODE (type) == METHOD_TYPE)
275 if (TREE_CODE (function) == OFFSET_REF)
277 tree object = build_address (TREE_OPERAND (function, 0));
278 return get_member_function_from_ptrfunc (&object,
279 TREE_OPERAND (function, 1),
280 complain);
282 function = build_address (function);
284 else
285 function = decay_conversion (function, complain);
287 return function;
290 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
291 POINTER_TYPE to those. Note, pointer to member function types
292 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
293 two variants. build_call_a is the primitive taking an array of
294 arguments, while build_call_n is a wrapper that handles varargs. */
296 tree
297 build_call_n (tree function, int n, ...)
299 if (n == 0)
300 return build_call_a (function, 0, NULL);
301 else
303 tree *argarray = XALLOCAVEC (tree, n);
304 va_list ap;
305 int i;
307 va_start (ap, n);
308 for (i = 0; i < n; i++)
309 argarray[i] = va_arg (ap, tree);
310 va_end (ap);
311 return build_call_a (function, n, argarray);
315 /* Update various flags in cfun and the call itself based on what is being
316 called. Split out of build_call_a so that bot_manip can use it too. */
318 void
319 set_flags_from_callee (tree call)
321 int nothrow;
322 tree decl = get_callee_fndecl (call);
324 /* We check both the decl and the type; a function may be known not to
325 throw without being declared throw(). */
326 nothrow = ((decl && TREE_NOTHROW (decl))
327 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (call)))));
329 if (!nothrow && at_function_scope_p () && cfun && cp_function_chain)
330 cp_function_chain->can_throw = 1;
332 if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
333 current_function_returns_abnormally = 1;
335 TREE_NOTHROW (call) = nothrow;
338 tree
339 build_call_a (tree function, int n, tree *argarray)
341 tree decl;
342 tree result_type;
343 tree fntype;
344 int i;
346 function = build_addr_func (function, tf_warning_or_error);
348 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
349 fntype = TREE_TYPE (TREE_TYPE (function));
350 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
351 || TREE_CODE (fntype) == METHOD_TYPE);
352 result_type = TREE_TYPE (fntype);
353 /* An rvalue has no cv-qualifiers. */
354 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
355 result_type = cv_unqualified (result_type);
357 function = build_call_array_loc (input_location,
358 result_type, function, n, argarray);
359 set_flags_from_callee (function);
361 decl = get_callee_fndecl (function);
363 if (decl && !TREE_USED (decl))
365 /* We invoke build_call directly for several library
366 functions. These may have been declared normally if
367 we're building libgcc, so we can't just check
368 DECL_ARTIFICIAL. */
369 gcc_assert (DECL_ARTIFICIAL (decl)
370 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
371 "__", 2));
372 mark_used (decl);
375 if (decl && TREE_DEPRECATED (decl))
376 warn_deprecated_use (decl, NULL_TREE);
377 require_complete_eh_spec_types (fntype, decl);
379 TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
381 /* Don't pass empty class objects by value. This is useful
382 for tags in STL, which are used to control overload resolution.
383 We don't need to handle other cases of copying empty classes. */
384 if (! decl || ! DECL_BUILT_IN (decl))
385 for (i = 0; i < n; i++)
387 tree arg = CALL_EXPR_ARG (function, i);
388 if (is_empty_class (TREE_TYPE (arg))
389 && ! TREE_ADDRESSABLE (TREE_TYPE (arg)))
391 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
392 arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
393 CALL_EXPR_ARG (function, i) = arg;
397 return function;
400 /* Build something of the form ptr->method (args)
401 or object.method (args). This can also build
402 calls to constructors, and find friends.
404 Member functions always take their class variable
405 as a pointer.
407 INSTANCE is a class instance.
409 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
411 PARMS help to figure out what that NAME really refers to.
413 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
414 down to the real instance type to use for access checking. We need this
415 information to get protected accesses correct.
417 FLAGS is the logical disjunction of zero or more LOOKUP_
418 flags. See cp-tree.h for more info.
420 If this is all OK, calls build_function_call with the resolved
421 member function.
423 This function must also handle being called to perform
424 initialization, promotion/coercion of arguments, and
425 instantiation of default parameters.
427 Note that NAME may refer to an instance variable name. If
428 `operator()()' is defined for the type of that field, then we return
429 that result. */
431 /* New overloading code. */
433 typedef struct z_candidate z_candidate;
435 typedef struct candidate_warning candidate_warning;
436 struct candidate_warning {
437 z_candidate *loser;
438 candidate_warning *next;
441 /* Information for providing diagnostics about why overloading failed. */
443 enum rejection_reason_code {
444 rr_none,
445 rr_arity,
446 rr_explicit_conversion,
447 rr_template_conversion,
448 rr_arg_conversion,
449 rr_bad_arg_conversion,
450 rr_template_unification,
451 rr_invalid_copy,
452 rr_constraint_failure
455 struct conversion_info {
456 /* The index of the argument, 0-based. */
457 int n_arg;
458 /* The type of the actual argument. */
459 tree from_type;
460 /* The type of the formal argument. */
461 tree to_type;
464 struct rejection_reason {
465 enum rejection_reason_code code;
466 union {
467 /* Information about an arity mismatch. */
468 struct {
469 /* The expected number of arguments. */
470 int expected;
471 /* The actual number of arguments in the call. */
472 int actual;
473 /* Whether the call was a varargs call. */
474 bool call_varargs_p;
475 } arity;
476 /* Information about an argument conversion mismatch. */
477 struct conversion_info conversion;
478 /* Same, but for bad argument conversions. */
479 struct conversion_info bad_conversion;
480 /* Information about template unification failures. These are the
481 parameters passed to fn_type_unification. */
482 struct {
483 tree tmpl;
484 tree explicit_targs;
485 int num_targs;
486 const tree *args;
487 unsigned int nargs;
488 tree return_type;
489 unification_kind_t strict;
490 int flags;
491 } template_unification;
492 /* Information about template instantiation failures. These are the
493 parameters passed to instantiate_template. */
494 struct {
495 tree tmpl;
496 tree targs;
497 } template_instantiation;
498 } u;
501 struct z_candidate {
502 /* The FUNCTION_DECL that will be called if this candidate is
503 selected by overload resolution. */
504 tree fn;
505 /* If not NULL_TREE, the first argument to use when calling this
506 function. */
507 tree first_arg;
508 /* The rest of the arguments to use when calling this function. If
509 there are no further arguments this may be NULL or it may be an
510 empty vector. */
511 const vec<tree, va_gc> *args;
512 /* The implicit conversion sequences for each of the arguments to
513 FN. */
514 conversion **convs;
515 /* The number of implicit conversion sequences. */
516 size_t num_convs;
517 /* If FN is a user-defined conversion, the standard conversion
518 sequence from the type returned by FN to the desired destination
519 type. */
520 conversion *second_conv;
521 int viable;
522 struct rejection_reason *reason;
523 /* If FN is a member function, the binfo indicating the path used to
524 qualify the name of FN at the call site. This path is used to
525 determine whether or not FN is accessible if it is selected by
526 overload resolution. The DECL_CONTEXT of FN will always be a
527 (possibly improper) base of this binfo. */
528 tree access_path;
529 /* If FN is a non-static member function, the binfo indicating the
530 subobject to which the `this' pointer should be converted if FN
531 is selected by overload resolution. The type pointed to by
532 the `this' pointer must correspond to the most derived class
533 indicated by the CONVERSION_PATH. */
534 tree conversion_path;
535 tree template_decl;
536 tree explicit_targs;
537 candidate_warning *warnings;
538 z_candidate *next;
541 /* Returns true iff T is a null pointer constant in the sense of
542 [conv.ptr]. */
544 bool
545 null_ptr_cst_p (tree t)
547 /* [conv.ptr]
549 A null pointer constant is an integral constant expression
550 (_expr.const_) rvalue of integer type that evaluates to zero or
551 an rvalue of type std::nullptr_t. */
552 if (NULLPTR_TYPE_P (TREE_TYPE (t)))
553 return true;
554 if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)))
556 /* Core issue 903 says only literal 0 is a null pointer constant. */
557 if (cxx_dialect < cxx11)
558 t = maybe_constant_value (fold_non_dependent_expr_sfinae (t, tf_none));
559 STRIP_NOPS (t);
560 if (integer_zerop (t) && !TREE_OVERFLOW (t))
561 return true;
563 return false;
566 /* Returns true iff T is a null member pointer value (4.11). */
568 bool
569 null_member_pointer_value_p (tree t)
571 tree type = TREE_TYPE (t);
572 if (!type)
573 return false;
574 else if (TYPE_PTRMEMFUNC_P (type))
575 return (TREE_CODE (t) == CONSTRUCTOR
576 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
577 else if (TYPE_PTRDATAMEM_P (type))
578 return integer_all_onesp (t);
579 else
580 return false;
583 /* Returns nonzero if PARMLIST consists of only default parms,
584 ellipsis, and/or undeduced parameter packs. */
586 bool
587 sufficient_parms_p (const_tree parmlist)
589 for (; parmlist && parmlist != void_list_node;
590 parmlist = TREE_CHAIN (parmlist))
591 if (!TREE_PURPOSE (parmlist)
592 && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
593 return false;
594 return true;
597 /* Allocate N bytes of memory from the conversion obstack. The memory
598 is zeroed before being returned. */
600 static void *
601 conversion_obstack_alloc (size_t n)
603 void *p;
604 if (!conversion_obstack_initialized)
606 gcc_obstack_init (&conversion_obstack);
607 conversion_obstack_initialized = true;
609 p = obstack_alloc (&conversion_obstack, n);
610 memset (p, 0, n);
611 return p;
614 /* Allocate rejection reasons. */
616 static struct rejection_reason *
617 alloc_rejection (enum rejection_reason_code code)
619 struct rejection_reason *p;
620 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
621 p->code = code;
622 return p;
625 static struct rejection_reason *
626 arity_rejection (tree first_arg, int expected, int actual)
628 struct rejection_reason *r = alloc_rejection (rr_arity);
629 int adjust = first_arg != NULL_TREE;
630 r->u.arity.expected = expected - adjust;
631 r->u.arity.actual = actual - adjust;
632 return r;
635 static struct rejection_reason *
636 arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
638 struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
639 int adjust = first_arg != NULL_TREE;
640 r->u.conversion.n_arg = n_arg - adjust;
641 r->u.conversion.from_type = from;
642 r->u.conversion.to_type = to;
643 return r;
646 static struct rejection_reason *
647 bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
649 struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
650 int adjust = first_arg != NULL_TREE;
651 r->u.bad_conversion.n_arg = n_arg - adjust;
652 r->u.bad_conversion.from_type = from;
653 r->u.bad_conversion.to_type = to;
654 return r;
657 static struct rejection_reason *
658 explicit_conversion_rejection (tree from, tree to)
660 struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
661 r->u.conversion.n_arg = 0;
662 r->u.conversion.from_type = from;
663 r->u.conversion.to_type = to;
664 return r;
667 static struct rejection_reason *
668 template_conversion_rejection (tree from, tree to)
670 struct rejection_reason *r = alloc_rejection (rr_template_conversion);
671 r->u.conversion.n_arg = 0;
672 r->u.conversion.from_type = from;
673 r->u.conversion.to_type = to;
674 return r;
677 static struct rejection_reason *
678 template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
679 const tree *args, unsigned int nargs,
680 tree return_type, unification_kind_t strict,
681 int flags)
683 size_t args_n_bytes = sizeof (*args) * nargs;
684 tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
685 struct rejection_reason *r = alloc_rejection (rr_template_unification);
686 r->u.template_unification.tmpl = tmpl;
687 r->u.template_unification.explicit_targs = explicit_targs;
688 r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
689 /* Copy args to our own storage. */
690 memcpy (args1, args, args_n_bytes);
691 r->u.template_unification.args = args1;
692 r->u.template_unification.nargs = nargs;
693 r->u.template_unification.return_type = return_type;
694 r->u.template_unification.strict = strict;
695 r->u.template_unification.flags = flags;
696 return r;
699 static struct rejection_reason *
700 template_unification_error_rejection (void)
702 return alloc_rejection (rr_template_unification);
705 static struct rejection_reason *
706 invalid_copy_with_fn_template_rejection (void)
708 struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
709 return r;
712 static struct rejection_reason *
713 template_constraint_failure (tree tmpl, tree targs)
715 struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
716 r->u.template_instantiation.tmpl = tmpl;
717 r->u.template_instantiation.targs = targs;
718 return r;
721 /* Dynamically allocate a conversion. */
723 static conversion *
724 alloc_conversion (conversion_kind kind)
726 conversion *c;
727 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
728 c->kind = kind;
729 return c;
732 #ifdef ENABLE_CHECKING
734 /* Make sure that all memory on the conversion obstack has been
735 freed. */
737 void
738 validate_conversion_obstack (void)
740 if (conversion_obstack_initialized)
741 gcc_assert ((obstack_next_free (&conversion_obstack)
742 == obstack_base (&conversion_obstack)));
745 #endif /* ENABLE_CHECKING */
747 /* Dynamically allocate an array of N conversions. */
749 static conversion **
750 alloc_conversions (size_t n)
752 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
755 static conversion *
756 build_conv (conversion_kind code, tree type, conversion *from)
758 conversion *t;
759 conversion_rank rank = CONVERSION_RANK (from);
761 /* Note that the caller is responsible for filling in t->cand for
762 user-defined conversions. */
763 t = alloc_conversion (code);
764 t->type = type;
765 t->u.next = from;
767 switch (code)
769 case ck_ptr:
770 case ck_pmem:
771 case ck_base:
772 case ck_std:
773 if (rank < cr_std)
774 rank = cr_std;
775 break;
777 case ck_qual:
778 if (rank < cr_exact)
779 rank = cr_exact;
780 break;
782 default:
783 break;
785 t->rank = rank;
786 t->user_conv_p = (code == ck_user || from->user_conv_p);
787 t->bad_p = from->bad_p;
788 t->base_p = false;
789 return t;
792 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
793 specialization of std::initializer_list<T>, if such a conversion is
794 possible. */
796 static conversion *
797 build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
799 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
800 unsigned len = CONSTRUCTOR_NELTS (ctor);
801 conversion **subconvs = alloc_conversions (len);
802 conversion *t;
803 unsigned i;
804 tree val;
806 /* Within a list-initialization we can have more user-defined
807 conversions. */
808 flags &= ~LOOKUP_NO_CONVERSION;
809 /* But no narrowing conversions. */
810 flags |= LOOKUP_NO_NARROWING;
812 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
814 conversion *sub
815 = implicit_conversion (elttype, TREE_TYPE (val), val,
816 false, flags, complain);
817 if (sub == NULL)
818 return NULL;
820 subconvs[i] = sub;
823 t = alloc_conversion (ck_list);
824 t->type = type;
825 t->u.list = subconvs;
826 t->rank = cr_exact;
828 for (i = 0; i < len; ++i)
830 conversion *sub = subconvs[i];
831 if (sub->rank > t->rank)
832 t->rank = sub->rank;
833 if (sub->user_conv_p)
834 t->user_conv_p = true;
835 if (sub->bad_p)
836 t->bad_p = true;
839 return t;
842 /* Return the next conversion of the conversion chain (if applicable),
843 or NULL otherwise. Please use this function instead of directly
844 accessing fields of struct conversion. */
846 static conversion *
847 next_conversion (conversion *conv)
849 if (conv == NULL
850 || conv->kind == ck_identity
851 || conv->kind == ck_ambig
852 || conv->kind == ck_list)
853 return NULL;
854 return conv->u.next;
857 /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
858 is a valid aggregate initializer for array type ATYPE. */
860 static bool
861 can_convert_array (tree atype, tree ctor, int flags, tsubst_flags_t complain)
863 unsigned i;
864 tree elttype = TREE_TYPE (atype);
865 for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
867 tree val = CONSTRUCTOR_ELT (ctor, i)->value;
868 bool ok;
869 if (TREE_CODE (elttype) == ARRAY_TYPE
870 && TREE_CODE (val) == CONSTRUCTOR)
871 ok = can_convert_array (elttype, val, flags, complain);
872 else
873 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
874 complain);
875 if (!ok)
876 return false;
878 return true;
881 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
882 aggregate class, if such a conversion is possible. */
884 static conversion *
885 build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
887 unsigned HOST_WIDE_INT i = 0;
888 conversion *c;
889 tree field = next_initializable_field (TYPE_FIELDS (type));
890 tree empty_ctor = NULL_TREE;
892 ctor = reshape_init (type, ctor, tf_none);
893 if (ctor == error_mark_node)
894 return NULL;
896 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
898 tree ftype = TREE_TYPE (field);
899 tree val;
900 bool ok;
902 if (i < CONSTRUCTOR_NELTS (ctor))
903 val = CONSTRUCTOR_ELT (ctor, i)->value;
904 else
906 if (empty_ctor == NULL_TREE)
907 empty_ctor = build_constructor (init_list_type_node, NULL);
908 val = empty_ctor;
910 ++i;
912 if (TREE_CODE (ftype) == ARRAY_TYPE
913 && TREE_CODE (val) == CONSTRUCTOR)
914 ok = can_convert_array (ftype, val, flags, complain);
915 else
916 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
917 complain);
919 if (!ok)
920 return NULL;
922 if (TREE_CODE (type) == UNION_TYPE)
923 break;
926 if (i < CONSTRUCTOR_NELTS (ctor))
927 return NULL;
929 c = alloc_conversion (ck_aggr);
930 c->type = type;
931 c->rank = cr_exact;
932 c->user_conv_p = true;
933 c->u.next = NULL;
934 return c;
937 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
938 array type, if such a conversion is possible. */
940 static conversion *
941 build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
943 conversion *c;
944 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
945 tree elttype = TREE_TYPE (type);
946 unsigned i;
947 tree val;
948 bool bad = false;
949 bool user = false;
950 enum conversion_rank rank = cr_exact;
952 if (TYPE_DOMAIN (type))
954 unsigned HOST_WIDE_INT alen = tree_low_cst (array_type_nelts_top (type), 1);
955 if (alen < len)
956 return NULL;
959 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
961 conversion *sub
962 = implicit_conversion (elttype, TREE_TYPE (val), val,
963 false, flags, complain);
964 if (sub == NULL)
965 return NULL;
967 if (sub->rank > rank)
968 rank = sub->rank;
969 if (sub->user_conv_p)
970 user = true;
971 if (sub->bad_p)
972 bad = true;
975 c = alloc_conversion (ck_aggr);
976 c->type = type;
977 c->rank = rank;
978 c->user_conv_p = user;
979 c->bad_p = bad;
980 c->u.next = NULL;
981 return c;
984 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
985 complex type, if such a conversion is possible. */
987 static conversion *
988 build_complex_conv (tree type, tree ctor, int flags,
989 tsubst_flags_t complain)
991 conversion *c;
992 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
993 tree elttype = TREE_TYPE (type);
994 unsigned i;
995 tree val;
996 bool bad = false;
997 bool user = false;
998 enum conversion_rank rank = cr_exact;
1000 if (len != 2)
1001 return NULL;
1003 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
1005 conversion *sub
1006 = implicit_conversion (elttype, TREE_TYPE (val), val,
1007 false, flags, complain);
1008 if (sub == NULL)
1009 return NULL;
1011 if (sub->rank > rank)
1012 rank = sub->rank;
1013 if (sub->user_conv_p)
1014 user = true;
1015 if (sub->bad_p)
1016 bad = true;
1019 c = alloc_conversion (ck_aggr);
1020 c->type = type;
1021 c->rank = rank;
1022 c->user_conv_p = user;
1023 c->bad_p = bad;
1024 c->u.next = NULL;
1025 return c;
1028 /* Build a representation of the identity conversion from EXPR to
1029 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1031 static conversion *
1032 build_identity_conv (tree type, tree expr)
1034 conversion *c;
1036 c = alloc_conversion (ck_identity);
1037 c->type = type;
1038 c->u.expr = expr;
1040 return c;
1043 /* Converting from EXPR to TYPE was ambiguous in the sense that there
1044 were multiple user-defined conversions to accomplish the job.
1045 Build a conversion that indicates that ambiguity. */
1047 static conversion *
1048 build_ambiguous_conv (tree type, tree expr)
1050 conversion *c;
1052 c = alloc_conversion (ck_ambig);
1053 c->type = type;
1054 c->u.expr = expr;
1056 return c;
1059 tree
1060 strip_top_quals (tree t)
1062 if (TREE_CODE (t) == ARRAY_TYPE)
1063 return t;
1064 return cp_build_qualified_type (t, 0);
1067 /* Returns the standard conversion path (see [conv]) from type FROM to type
1068 TO, if any. For proper handling of null pointer constants, you must
1069 also pass the expression EXPR to convert from. If C_CAST_P is true,
1070 this conversion is coming from a C-style cast. */
1072 static conversion *
1073 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1074 int flags)
1076 enum tree_code fcode, tcode;
1077 conversion *conv;
1078 bool fromref = false;
1079 tree qualified_to;
1081 to = non_reference (to);
1082 if (TREE_CODE (from) == REFERENCE_TYPE)
1084 fromref = true;
1085 from = TREE_TYPE (from);
1087 qualified_to = to;
1088 to = strip_top_quals (to);
1089 from = strip_top_quals (from);
1091 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1092 && expr && type_unknown_p (expr))
1094 tsubst_flags_t tflags = tf_conv;
1095 expr = instantiate_type (to, expr, tflags);
1096 if (expr == error_mark_node)
1097 return NULL;
1098 from = TREE_TYPE (expr);
1101 fcode = TREE_CODE (from);
1102 tcode = TREE_CODE (to);
1104 conv = build_identity_conv (from, expr);
1105 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1107 from = type_decays_to (from);
1108 fcode = TREE_CODE (from);
1109 conv = build_conv (ck_lvalue, from, conv);
1111 else if (fromref || (expr && lvalue_p (expr)))
1113 if (expr)
1115 tree bitfield_type;
1116 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1117 if (bitfield_type)
1119 from = strip_top_quals (bitfield_type);
1120 fcode = TREE_CODE (from);
1123 conv = build_conv (ck_rvalue, from, conv);
1124 if (flags & LOOKUP_PREFER_RVALUE)
1125 conv->rvaluedness_matches_p = true;
1128 /* Allow conversion between `__complex__' data types. */
1129 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1131 /* The standard conversion sequence to convert FROM to TO is
1132 the standard conversion sequence to perform componentwise
1133 conversion. */
1134 conversion *part_conv = standard_conversion
1135 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags);
1137 if (part_conv)
1139 conv = build_conv (part_conv->kind, to, conv);
1140 conv->rank = part_conv->rank;
1142 else
1143 conv = NULL;
1145 return conv;
1148 if (same_type_p (from, to))
1150 if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1151 conv->type = qualified_to;
1152 return conv;
1155 /* [conv.ptr]
1156 A null pointer constant can be converted to a pointer type; ... A
1157 null pointer constant of integral type can be converted to an
1158 rvalue of type std::nullptr_t. */
1159 if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1160 || NULLPTR_TYPE_P (to))
1161 && expr && null_ptr_cst_p (expr))
1162 conv = build_conv (ck_std, to, conv);
1163 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1164 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1166 /* For backwards brain damage compatibility, allow interconversion of
1167 pointers and integers with a pedwarn. */
1168 conv = build_conv (ck_std, to, conv);
1169 conv->bad_p = true;
1171 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1173 /* For backwards brain damage compatibility, allow interconversion of
1174 enums and integers with a pedwarn. */
1175 conv = build_conv (ck_std, to, conv);
1176 conv->bad_p = true;
1178 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1179 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1181 tree to_pointee;
1182 tree from_pointee;
1184 if (tcode == POINTER_TYPE
1185 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
1186 TREE_TYPE (to)))
1188 else if (VOID_TYPE_P (TREE_TYPE (to))
1189 && !TYPE_PTRDATAMEM_P (from)
1190 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
1192 tree nfrom = TREE_TYPE (from);
1193 from = build_pointer_type
1194 (cp_build_qualified_type (void_type_node,
1195 cp_type_quals (nfrom)));
1196 conv = build_conv (ck_ptr, from, conv);
1198 else if (TYPE_PTRDATAMEM_P (from))
1200 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1201 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1203 if (DERIVED_FROM_P (fbase, tbase)
1204 && (same_type_ignoring_top_level_qualifiers_p
1205 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
1206 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
1208 from = build_ptrmem_type (tbase,
1209 TYPE_PTRMEM_POINTED_TO_TYPE (from));
1210 conv = build_conv (ck_pmem, from, conv);
1212 else if (!same_type_p (fbase, tbase))
1213 return NULL;
1215 else if (CLASS_TYPE_P (TREE_TYPE (from))
1216 && CLASS_TYPE_P (TREE_TYPE (to))
1217 /* [conv.ptr]
1219 An rvalue of type "pointer to cv D," where D is a
1220 class type, can be converted to an rvalue of type
1221 "pointer to cv B," where B is a base class (clause
1222 _class.derived_) of D. If B is an inaccessible
1223 (clause _class.access_) or ambiguous
1224 (_class.member.lookup_) base class of D, a program
1225 that necessitates this conversion is ill-formed.
1226 Therefore, we use DERIVED_FROM_P, and do not check
1227 access or uniqueness. */
1228 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
1230 from =
1231 cp_build_qualified_type (TREE_TYPE (to),
1232 cp_type_quals (TREE_TYPE (from)));
1233 from = build_pointer_type (from);
1234 conv = build_conv (ck_ptr, from, conv);
1235 conv->base_p = true;
1238 if (tcode == POINTER_TYPE)
1240 to_pointee = TREE_TYPE (to);
1241 from_pointee = TREE_TYPE (from);
1243 else
1245 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1246 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1249 if (same_type_p (from, to))
1250 /* OK */;
1251 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
1252 /* In a C-style cast, we ignore CV-qualification because we
1253 are allowed to perform a static_cast followed by a
1254 const_cast. */
1255 conv = build_conv (ck_qual, to, conv);
1256 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1257 conv = build_conv (ck_qual, to, conv);
1258 else if (expr && string_conv_p (to, expr, 0))
1259 /* converting from string constant to char *. */
1260 conv = build_conv (ck_qual, to, conv);
1261 /* Allow conversions among compatible ObjC pointer types (base
1262 conversions have been already handled above). */
1263 else if (c_dialect_objc ()
1264 && objc_compare_types (to, from, -4, NULL_TREE))
1265 conv = build_conv (ck_ptr, to, conv);
1266 else if (ptr_reasonably_similar (to_pointee, from_pointee))
1268 conv = build_conv (ck_ptr, to, conv);
1269 conv->bad_p = true;
1271 else
1272 return NULL;
1274 from = to;
1276 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1278 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1279 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1280 tree fbase = class_of_this_parm (fromfn);
1281 tree tbase = class_of_this_parm (tofn);
1283 if (!DERIVED_FROM_P (fbase, tbase)
1284 || !same_type_p (static_fn_type (fromfn),
1285 static_fn_type (tofn)))
1286 return NULL;
1288 from = build_memfn_type (fromfn,
1289 tbase,
1290 cp_type_quals (tbase),
1291 type_memfn_rqual (tofn));
1292 from = build_ptrmemfunc_type (build_pointer_type (from));
1293 conv = build_conv (ck_pmem, from, conv);
1294 conv->base_p = true;
1296 else if (tcode == BOOLEAN_TYPE)
1298 /* [conv.bool]
1300 An rvalue of arithmetic, unscoped enumeration, pointer, or
1301 pointer to member type can be converted to an rvalue of type
1302 bool. ... An rvalue of type std::nullptr_t can be converted
1303 to an rvalue of type bool; */
1304 if (ARITHMETIC_TYPE_P (from)
1305 || UNSCOPED_ENUM_P (from)
1306 || fcode == POINTER_TYPE
1307 || TYPE_PTRMEM_P (from)
1308 || NULLPTR_TYPE_P (from))
1310 conv = build_conv (ck_std, to, conv);
1311 if (fcode == POINTER_TYPE
1312 || TYPE_PTRDATAMEM_P (from)
1313 || (TYPE_PTRMEMFUNC_P (from)
1314 && conv->rank < cr_pbool)
1315 || NULLPTR_TYPE_P (from))
1316 conv->rank = cr_pbool;
1317 return conv;
1320 return NULL;
1322 /* We don't check for ENUMERAL_TYPE here because there are no standard
1323 conversions to enum type. */
1324 /* As an extension, allow conversion to complex type. */
1325 else if (ARITHMETIC_TYPE_P (to))
1327 if (! (INTEGRAL_CODE_P (fcode)
1328 || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1329 || SCOPED_ENUM_P (from))
1330 return NULL;
1331 conv = build_conv (ck_std, to, conv);
1333 /* Give this a better rank if it's a promotion. */
1334 if (same_type_p (to, type_promotes_to (from))
1335 && next_conversion (conv)->rank <= cr_promotion)
1336 conv->rank = cr_promotion;
1338 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1339 && vector_types_convertible_p (from, to, false))
1340 return build_conv (ck_std, to, conv);
1341 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1342 && is_properly_derived_from (from, to))
1344 if (conv->kind == ck_rvalue)
1345 conv = next_conversion (conv);
1346 conv = build_conv (ck_base, to, conv);
1347 /* The derived-to-base conversion indicates the initialization
1348 of a parameter with base type from an object of a derived
1349 type. A temporary object is created to hold the result of
1350 the conversion unless we're binding directly to a reference. */
1351 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1353 else
1354 return NULL;
1356 if (flags & LOOKUP_NO_NARROWING)
1357 conv->check_narrowing = true;
1359 return conv;
1362 /* Returns nonzero if T1 is reference-related to T2. */
1364 bool
1365 reference_related_p (tree t1, tree t2)
1367 if (t1 == error_mark_node || t2 == error_mark_node)
1368 return false;
1370 t1 = TYPE_MAIN_VARIANT (t1);
1371 t2 = TYPE_MAIN_VARIANT (t2);
1373 /* [dcl.init.ref]
1375 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1376 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
1377 of T2. */
1378 return (same_type_p (t1, t2)
1379 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1380 && DERIVED_FROM_P (t1, t2)));
1383 /* Returns nonzero if T1 is reference-compatible with T2. */
1385 static bool
1386 reference_compatible_p (tree t1, tree t2)
1388 /* [dcl.init.ref]
1390 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
1391 reference-related to T2 and cv1 is the same cv-qualification as,
1392 or greater cv-qualification than, cv2. */
1393 return (reference_related_p (t1, t2)
1394 && at_least_as_qualified_p (t1, t2));
1397 /* A reference of the indicated TYPE is being bound directly to the
1398 expression represented by the implicit conversion sequence CONV.
1399 Return a conversion sequence for this binding. */
1401 static conversion *
1402 direct_reference_binding (tree type, conversion *conv)
1404 tree t;
1406 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1407 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1409 t = TREE_TYPE (type);
1411 /* [over.ics.rank]
1413 When a parameter of reference type binds directly
1414 (_dcl.init.ref_) to an argument expression, the implicit
1415 conversion sequence is the identity conversion, unless the
1416 argument expression has a type that is a derived class of the
1417 parameter type, in which case the implicit conversion sequence is
1418 a derived-to-base Conversion.
1420 If the parameter binds directly to the result of applying a
1421 conversion function to the argument expression, the implicit
1422 conversion sequence is a user-defined conversion sequence
1423 (_over.ics.user_), with the second standard conversion sequence
1424 either an identity conversion or, if the conversion function
1425 returns an entity of a type that is a derived class of the
1426 parameter type, a derived-to-base conversion. */
1427 if (!same_type_ignoring_top_level_qualifiers_p (t, conv->type))
1429 /* Represent the derived-to-base conversion. */
1430 conv = build_conv (ck_base, t, conv);
1431 /* We will actually be binding to the base-class subobject in
1432 the derived class, so we mark this conversion appropriately.
1433 That way, convert_like knows not to generate a temporary. */
1434 conv->need_temporary_p = false;
1436 return build_conv (ck_ref_bind, type, conv);
1439 /* Returns the conversion path from type FROM to reference type TO for
1440 purposes of reference binding. For lvalue binding, either pass a
1441 reference type to FROM or an lvalue expression to EXPR. If the
1442 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1443 the conversion returned. If C_CAST_P is true, this
1444 conversion is coming from a C-style cast. */
1446 static conversion *
1447 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1448 tsubst_flags_t complain)
1450 conversion *conv = NULL;
1451 tree to = TREE_TYPE (rto);
1452 tree from = rfrom;
1453 tree tfrom;
1454 bool related_p;
1455 bool compatible_p;
1456 cp_lvalue_kind gl_kind;
1457 bool is_lvalue;
1459 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1461 expr = instantiate_type (to, expr, tf_none);
1462 if (expr == error_mark_node)
1463 return NULL;
1464 from = TREE_TYPE (expr);
1467 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1469 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1470 conv = implicit_conversion (to, from, expr, c_cast_p,
1471 flags, complain);
1472 if (!CLASS_TYPE_P (to)
1473 && CONSTRUCTOR_NELTS (expr) == 1)
1475 expr = CONSTRUCTOR_ELT (expr, 0)->value;
1476 if (error_operand_p (expr))
1477 return NULL;
1478 from = TREE_TYPE (expr);
1482 if (TREE_CODE (from) == REFERENCE_TYPE)
1484 from = TREE_TYPE (from);
1485 if (!TYPE_REF_IS_RVALUE (rfrom)
1486 || TREE_CODE (from) == FUNCTION_TYPE)
1487 gl_kind = clk_ordinary;
1488 else
1489 gl_kind = clk_rvalueref;
1491 else if (expr)
1493 gl_kind = lvalue_kind (expr);
1494 if (gl_kind & clk_class)
1495 /* A class prvalue is not a glvalue. */
1496 gl_kind = clk_none;
1498 else
1499 gl_kind = clk_none;
1500 is_lvalue = gl_kind && !(gl_kind & clk_rvalueref);
1502 tfrom = from;
1503 if ((gl_kind & clk_bitfield) != 0)
1504 tfrom = unlowered_expr_type (expr);
1506 /* Figure out whether or not the types are reference-related and
1507 reference compatible. We have do do this after stripping
1508 references from FROM. */
1509 related_p = reference_related_p (to, tfrom);
1510 /* If this is a C cast, first convert to an appropriately qualified
1511 type, so that we can later do a const_cast to the desired type. */
1512 if (related_p && c_cast_p
1513 && !at_least_as_qualified_p (to, tfrom))
1514 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1515 compatible_p = reference_compatible_p (to, tfrom);
1517 /* Directly bind reference when target expression's type is compatible with
1518 the reference and expression is an lvalue. In DR391, the wording in
1519 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1520 const and rvalue references to rvalues of compatible class type.
1521 We should also do direct bindings for non-class xvalues. */
1522 if (compatible_p
1523 && (is_lvalue
1524 || (((CP_TYPE_CONST_NON_VOLATILE_P (to)
1525 && !(flags & LOOKUP_NO_RVAL_BIND))
1526 || TYPE_REF_IS_RVALUE (rto))
1527 && (gl_kind
1528 || (!(flags & LOOKUP_NO_TEMP_BIND)
1529 && (CLASS_TYPE_P (from)
1530 || TREE_CODE (from) == ARRAY_TYPE))))))
1532 /* [dcl.init.ref]
1534 If the initializer expression
1536 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1537 is reference-compatible with "cv2 T2,"
1539 the reference is bound directly to the initializer expression
1540 lvalue.
1542 [...]
1543 If the initializer expression is an rvalue, with T2 a class type,
1544 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1545 is bound to the object represented by the rvalue or to a sub-object
1546 within that object. */
1548 conv = build_identity_conv (tfrom, expr);
1549 conv = direct_reference_binding (rto, conv);
1551 if (flags & LOOKUP_PREFER_RVALUE)
1552 /* The top-level caller requested that we pretend that the lvalue
1553 be treated as an rvalue. */
1554 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1555 else if (TREE_CODE (rfrom) == REFERENCE_TYPE)
1556 /* Handle rvalue reference to function properly. */
1557 conv->rvaluedness_matches_p
1558 = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1559 else
1560 conv->rvaluedness_matches_p
1561 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1563 if ((gl_kind & clk_bitfield) != 0
1564 || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1565 /* For the purposes of overload resolution, we ignore the fact
1566 this expression is a bitfield or packed field. (In particular,
1567 [over.ics.ref] says specifically that a function with a
1568 non-const reference parameter is viable even if the
1569 argument is a bitfield.)
1571 However, when we actually call the function we must create
1572 a temporary to which to bind the reference. If the
1573 reference is volatile, or isn't const, then we cannot make
1574 a temporary, so we just issue an error when the conversion
1575 actually occurs. */
1576 conv->need_temporary_p = true;
1578 /* Don't allow binding of lvalues (other than function lvalues) to
1579 rvalue references. */
1580 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1581 && TREE_CODE (to) != FUNCTION_TYPE
1582 && !(flags & LOOKUP_PREFER_RVALUE))
1583 conv->bad_p = true;
1585 return conv;
1587 /* [class.conv.fct] A conversion function is never used to convert a
1588 (possibly cv-qualified) object to the (possibly cv-qualified) same
1589 object type (or a reference to it), to a (possibly cv-qualified) base
1590 class of that type (or a reference to it).... */
1591 else if (CLASS_TYPE_P (from) && !related_p
1592 && !(flags & LOOKUP_NO_CONVERSION))
1594 /* [dcl.init.ref]
1596 If the initializer expression
1598 -- has a class type (i.e., T2 is a class type) can be
1599 implicitly converted to an lvalue of type "cv3 T3," where
1600 "cv1 T1" is reference-compatible with "cv3 T3". (this
1601 conversion is selected by enumerating the applicable
1602 conversion functions (_over.match.ref_) and choosing the
1603 best one through overload resolution. (_over.match_).
1605 the reference is bound to the lvalue result of the conversion
1606 in the second case. */
1607 z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
1608 complain);
1609 if (cand)
1610 return cand->second_conv;
1613 /* From this point on, we conceptually need temporaries, even if we
1614 elide them. Only the cases above are "direct bindings". */
1615 if (flags & LOOKUP_NO_TEMP_BIND)
1616 return NULL;
1618 /* [over.ics.rank]
1620 When a parameter of reference type is not bound directly to an
1621 argument expression, the conversion sequence is the one required
1622 to convert the argument expression to the underlying type of the
1623 reference according to _over.best.ics_. Conceptually, this
1624 conversion sequence corresponds to copy-initializing a temporary
1625 of the underlying type with the argument expression. Any
1626 difference in top-level cv-qualification is subsumed by the
1627 initialization itself and does not constitute a conversion. */
1629 /* [dcl.init.ref]
1631 Otherwise, the reference shall be to a non-volatile const type.
1633 Under C++0x, [8.5.3/5 dcl.init.ref] it may also be an rvalue reference */
1634 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1635 return NULL;
1637 /* [dcl.init.ref]
1639 Otherwise, a temporary of type "cv1 T1" is created and
1640 initialized from the initializer expression using the rules for a
1641 non-reference copy initialization. If T1 is reference-related to
1642 T2, cv1 must be the same cv-qualification as, or greater
1643 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1644 if (related_p && !at_least_as_qualified_p (to, from))
1645 return NULL;
1647 /* We're generating a temporary now, but don't bind any more in the
1648 conversion (specifically, don't slice the temporary returned by a
1649 conversion operator). */
1650 flags |= LOOKUP_NO_TEMP_BIND;
1652 /* Core issue 899: When [copy-]initializing a temporary to be bound
1653 to the first parameter of a copy constructor (12.8) called with
1654 a single argument in the context of direct-initialization,
1655 explicit conversion functions are also considered.
1657 So don't set LOOKUP_ONLYCONVERTING in that case. */
1658 if (!(flags & LOOKUP_COPY_PARM))
1659 flags |= LOOKUP_ONLYCONVERTING;
1661 if (!conv)
1662 conv = implicit_conversion (to, from, expr, c_cast_p,
1663 flags, complain);
1664 if (!conv)
1665 return NULL;
1667 conv = build_conv (ck_ref_bind, rto, conv);
1668 /* This reference binding, unlike those above, requires the
1669 creation of a temporary. */
1670 conv->need_temporary_p = true;
1671 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1673 return conv;
1676 /* Returns the implicit conversion sequence (see [over.ics]) from type
1677 FROM to type TO. The optional expression EXPR may affect the
1678 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1679 true, this conversion is coming from a C-style cast. */
1681 static conversion *
1682 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1683 int flags, tsubst_flags_t complain)
1685 conversion *conv;
1687 if (from == error_mark_node || to == error_mark_node
1688 || expr == error_mark_node)
1689 return NULL;
1691 /* Other flags only apply to the primary function in overload
1692 resolution, or after we've chosen one. */
1693 flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
1694 |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE
1695 |LOOKUP_NO_NARROWING|LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL);
1697 /* FIXME: actually we don't want warnings either, but we can't just
1698 have 'complain &= ~(tf_warning|tf_error)' because it would cause
1699 the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
1700 We really ought not to issue that warning until we've committed
1701 to that conversion. */
1702 complain &= ~tf_error;
1704 if (TREE_CODE (to) == REFERENCE_TYPE)
1705 conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
1706 else
1707 conv = standard_conversion (to, from, expr, c_cast_p, flags);
1709 if (conv)
1710 return conv;
1712 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1714 if (is_std_init_list (to))
1715 return build_list_conv (to, expr, flags, complain);
1717 /* As an extension, allow list-initialization of _Complex. */
1718 if (TREE_CODE (to) == COMPLEX_TYPE)
1720 conv = build_complex_conv (to, expr, flags, complain);
1721 if (conv)
1722 return conv;
1725 /* Allow conversion from an initializer-list with one element to a
1726 scalar type. */
1727 if (SCALAR_TYPE_P (to))
1729 int nelts = CONSTRUCTOR_NELTS (expr);
1730 tree elt;
1732 if (nelts == 0)
1733 elt = build_value_init (to, tf_none);
1734 else if (nelts == 1)
1735 elt = CONSTRUCTOR_ELT (expr, 0)->value;
1736 else
1737 elt = error_mark_node;
1739 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
1740 c_cast_p, flags, complain);
1741 if (conv)
1743 conv->check_narrowing = true;
1744 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
1745 /* Too many levels of braces, i.e. '{{1}}'. */
1746 conv->bad_p = true;
1747 return conv;
1750 else if (TREE_CODE (to) == ARRAY_TYPE)
1751 return build_array_conv (to, expr, flags, complain);
1754 if (expr != NULL_TREE
1755 && (MAYBE_CLASS_TYPE_P (from)
1756 || MAYBE_CLASS_TYPE_P (to))
1757 && (flags & LOOKUP_NO_CONVERSION) == 0)
1759 struct z_candidate *cand;
1761 if (CLASS_TYPE_P (to)
1762 && BRACE_ENCLOSED_INITIALIZER_P (expr)
1763 && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
1764 return build_aggr_conv (to, expr, flags, complain);
1766 cand = build_user_type_conversion_1 (to, expr, flags, complain);
1767 if (cand)
1768 conv = cand->second_conv;
1770 /* We used to try to bind a reference to a temporary here, but that
1771 is now handled after the recursive call to this function at the end
1772 of reference_binding. */
1773 return conv;
1776 return NULL;
1779 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1780 functions. ARGS will not be changed until a single candidate is
1781 selected. */
1783 static struct z_candidate *
1784 add_candidate (struct z_candidate **candidates,
1785 tree fn, tree first_arg, const vec<tree, va_gc> *args,
1786 size_t num_convs, conversion **convs,
1787 tree access_path, tree conversion_path,
1788 int viable, struct rejection_reason *reason)
1790 struct z_candidate *cand = (struct z_candidate *)
1791 conversion_obstack_alloc (sizeof (struct z_candidate));
1793 cand->fn = fn;
1794 cand->first_arg = first_arg;
1795 cand->args = args;
1796 cand->convs = convs;
1797 cand->num_convs = num_convs;
1798 cand->access_path = access_path;
1799 cand->conversion_path = conversion_path;
1800 cand->viable = viable;
1801 cand->reason = reason;
1802 cand->next = *candidates;
1803 *candidates = cand;
1805 return cand;
1808 /* Return the number of remaining arguments in the parameter list
1809 beginning with ARG. */
1811 static int
1812 remaining_arguments (tree arg)
1814 int n;
1816 for (n = 0; arg != NULL_TREE && arg != void_list_node;
1817 arg = TREE_CHAIN (arg))
1818 n++;
1820 return n;
1823 // Returns true if FN is a non-template member function or non-template
1824 // friend function. Both kinds of declaration can be constrained.
1825 static inline bool
1826 is_constrainable_non_template_fn (tree fn)
1828 if (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (fn))
1829 return true;
1831 return DECL_FUNCTION_MEMBER_P (fn) &&
1832 DECL_TEMPLATE_INFO (fn) &&
1833 !DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn));
1836 /* Create an overload candidate for the function or method FN called
1837 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
1838 FLAGS is passed on to implicit_conversion.
1840 This does not change ARGS.
1842 CTYPE, if non-NULL, is the type we want to pretend this function
1843 comes from for purposes of overload resolution. */
1845 static struct z_candidate *
1846 add_function_candidate (struct z_candidate **candidates,
1847 tree fn, tree ctype, tree first_arg,
1848 const vec<tree, va_gc> *args, tree access_path,
1849 tree conversion_path, int flags,
1850 tsubst_flags_t complain)
1852 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1853 int i, len;
1854 conversion **convs;
1855 tree parmnode;
1856 tree orig_first_arg = first_arg;
1857 int skip;
1858 int viable = 1;
1859 struct rejection_reason *reason = NULL;
1861 /* At this point we should not see any functions which haven't been
1862 explicitly declared, except for friend functions which will have
1863 been found using argument dependent lookup. */
1864 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
1866 /* The `this', `in_chrg' and VTT arguments to constructors are not
1867 considered in overload resolution. */
1868 if (DECL_CONSTRUCTOR_P (fn))
1870 parmlist = skip_artificial_parms_for (fn, parmlist);
1871 skip = num_artificial_parms_for (fn);
1872 if (skip > 0 && first_arg != NULL_TREE)
1874 --skip;
1875 first_arg = NULL_TREE;
1878 else
1879 skip = 0;
1881 len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
1882 convs = alloc_conversions (len);
1884 // Viable functions
1886 // Functions whose constraints are not satisfied are non-viable.
1888 // For function templates, constraints are checked as part of template
1889 // argument deduction. A failure there means that the template is
1890 // already added as a non-viable candidate. For non-template member
1891 // functions, however, the declaration declaration has already been
1892 // synthesized, but its constraints have not actually been checked.
1893 // We should do that now.
1895 // TODO: Consider checking constrained non-template members during
1896 // class template instantiation and setting a flag indicating whether
1897 // or not the declaration is viable. This could be set as a flag in
1898 // TEMPLATE_INFO (there should be a bunch of unused bits there).
1899 if (is_constrainable_non_template_fn (fn))
1901 tree tmpl = DECL_TI_TEMPLATE (fn);
1902 tree args = DECL_TI_ARGS (fn);
1903 if (!check_template_constraints (tmpl, args))
1905 reason = template_constraint_failure (tmpl, args);
1906 viable = false;
1907 goto out;
1911 /* 13.3.2 - Viable functions [over.match.viable]
1912 First, to be a viable function, a candidate function shall have enough
1913 parameters to agree in number with the arguments in the list.
1915 We need to check this first; otherwise, checking the ICSes might cause
1916 us to produce an ill-formed template instantiation. */
1918 parmnode = parmlist;
1919 for (i = 0; i < len; ++i)
1921 if (parmnode == NULL_TREE || parmnode == void_list_node)
1922 break;
1923 parmnode = TREE_CHAIN (parmnode);
1926 if ((i < len && parmnode)
1927 || !sufficient_parms_p (parmnode))
1929 int remaining = remaining_arguments (parmnode);
1930 viable = 0;
1931 reason = arity_rejection (first_arg, i + remaining, len);
1933 /* When looking for a function from a subobject from an implicit
1934 copy/move constructor/operator=, don't consider anything that takes (a
1935 reference to) an unrelated type. See c++/44909 and core 1092. */
1936 else if (parmlist && (flags & LOOKUP_DEFAULTED))
1938 if (DECL_CONSTRUCTOR_P (fn))
1939 i = 1;
1940 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1941 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
1942 i = 2;
1943 else
1944 i = 0;
1945 if (i && len == i)
1947 parmnode = chain_index (i-1, parmlist);
1948 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
1949 ctype))
1950 viable = 0;
1953 /* This only applies at the top level. */
1954 flags &= ~LOOKUP_DEFAULTED;
1957 if (! viable)
1958 goto out;
1960 /* Second, for F to be a viable function, there shall exist for each
1961 argument an implicit conversion sequence that converts that argument
1962 to the corresponding parameter of F. */
1964 parmnode = parmlist;
1966 for (i = 0; i < len; ++i)
1968 tree argtype, to_type;
1969 tree arg;
1970 conversion *t;
1971 int is_this;
1973 if (parmnode == void_list_node)
1974 break;
1976 if (i == 0 && first_arg != NULL_TREE)
1977 arg = first_arg;
1978 else
1979 arg = CONST_CAST_TREE (
1980 (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
1981 argtype = lvalue_type (arg);
1983 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1984 && ! DECL_CONSTRUCTOR_P (fn));
1986 if (parmnode)
1988 tree parmtype = TREE_VALUE (parmnode);
1989 int lflags = flags;
1991 parmnode = TREE_CHAIN (parmnode);
1993 /* The type of the implicit object parameter ('this') for
1994 overload resolution is not always the same as for the
1995 function itself; conversion functions are considered to
1996 be members of the class being converted, and functions
1997 introduced by a using-declaration are considered to be
1998 members of the class that uses them.
2000 Since build_over_call ignores the ICS for the `this'
2001 parameter, we can just change the parm type. */
2002 if (ctype && is_this)
2004 parmtype = cp_build_qualified_type
2005 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
2006 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2008 /* If the function has a ref-qualifier, the implicit
2009 object parameter has reference type. */
2010 bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2011 parmtype = cp_build_reference_type (parmtype, rv);
2013 else
2015 parmtype = build_pointer_type (parmtype);
2016 arg = build_this (arg);
2017 argtype = lvalue_type (arg);
2021 /* Core issue 899: When [copy-]initializing a temporary to be bound
2022 to the first parameter of a copy constructor (12.8) called with
2023 a single argument in the context of direct-initialization,
2024 explicit conversion functions are also considered.
2026 So set LOOKUP_COPY_PARM to let reference_binding know that
2027 it's being called in that context. We generalize the above
2028 to handle move constructors and template constructors as well;
2029 the standardese should soon be updated similarly. */
2030 if (ctype && i == 0 && (len-skip == 1)
2031 && DECL_CONSTRUCTOR_P (fn)
2032 && parmtype != error_mark_node
2033 && (same_type_ignoring_top_level_qualifiers_p
2034 (non_reference (parmtype), ctype)))
2036 if (!(flags & LOOKUP_ONLYCONVERTING))
2037 lflags |= LOOKUP_COPY_PARM;
2038 /* We allow user-defined conversions within init-lists, but
2039 don't list-initialize the copy parm, as that would mean
2040 using two levels of braces for the same type. */
2041 if ((flags & LOOKUP_LIST_INIT_CTOR)
2042 && BRACE_ENCLOSED_INITIALIZER_P (arg))
2043 lflags |= LOOKUP_NO_CONVERSION;
2045 else
2046 lflags |= LOOKUP_ONLYCONVERTING;
2048 t = implicit_conversion (parmtype, argtype, arg,
2049 /*c_cast_p=*/false, lflags, complain);
2050 to_type = parmtype;
2052 else
2054 t = build_identity_conv (argtype, arg);
2055 t->ellipsis_p = true;
2056 to_type = argtype;
2059 if (t && is_this)
2060 t->this_p = true;
2062 convs[i] = t;
2063 if (! t)
2065 viable = 0;
2066 reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
2067 break;
2070 if (t->bad_p)
2072 viable = -1;
2073 reason = bad_arg_conversion_rejection (first_arg, i, argtype, to_type);
2077 out:
2078 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2079 access_path, conversion_path, viable, reason);
2082 /* Create an overload candidate for the conversion function FN which will
2083 be invoked for expression OBJ, producing a pointer-to-function which
2084 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2085 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2086 passed on to implicit_conversion.
2088 Actually, we don't really care about FN; we care about the type it
2089 converts to. There may be multiple conversion functions that will
2090 convert to that type, and we rely on build_user_type_conversion_1 to
2091 choose the best one; so when we create our candidate, we record the type
2092 instead of the function. */
2094 static struct z_candidate *
2095 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2096 tree first_arg, const vec<tree, va_gc> *arglist,
2097 tree access_path, tree conversion_path,
2098 tsubst_flags_t complain)
2100 tree totype = TREE_TYPE (TREE_TYPE (fn));
2101 int i, len, viable, flags;
2102 tree parmlist, parmnode;
2103 conversion **convs;
2104 struct rejection_reason *reason;
2106 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2107 parmlist = TREE_TYPE (parmlist);
2108 parmlist = TYPE_ARG_TYPES (parmlist);
2110 len = vec_safe_length (arglist) + (first_arg != NULL_TREE ? 1 : 0) + 1;
2111 convs = alloc_conversions (len);
2112 parmnode = parmlist;
2113 viable = 1;
2114 flags = LOOKUP_IMPLICIT;
2115 reason = NULL;
2117 /* Don't bother looking up the same type twice. */
2118 if (*candidates && (*candidates)->fn == totype)
2119 return NULL;
2121 for (i = 0; i < len; ++i)
2123 tree arg, argtype, convert_type = NULL_TREE;
2124 conversion *t;
2126 if (i == 0)
2127 arg = obj;
2128 else if (i == 1 && first_arg != NULL_TREE)
2129 arg = first_arg;
2130 else
2131 arg = (*arglist)[i - (first_arg != NULL_TREE ? 1 : 0) - 1];
2132 argtype = lvalue_type (arg);
2134 if (i == 0)
2136 t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false,
2137 flags, complain);
2138 convert_type = totype;
2140 else if (parmnode == void_list_node)
2141 break;
2142 else if (parmnode)
2144 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2145 /*c_cast_p=*/false, flags, complain);
2146 convert_type = TREE_VALUE (parmnode);
2148 else
2150 t = build_identity_conv (argtype, arg);
2151 t->ellipsis_p = true;
2152 convert_type = argtype;
2155 convs[i] = t;
2156 if (! t)
2157 break;
2159 if (t->bad_p)
2161 viable = -1;
2162 reason = bad_arg_conversion_rejection (NULL_TREE, i, argtype, convert_type);
2165 if (i == 0)
2166 continue;
2168 if (parmnode)
2169 parmnode = TREE_CHAIN (parmnode);
2172 if (i < len
2173 || ! sufficient_parms_p (parmnode))
2175 int remaining = remaining_arguments (parmnode);
2176 viable = 0;
2177 reason = arity_rejection (NULL_TREE, i + remaining, len);
2180 return add_candidate (candidates, totype, first_arg, arglist, len, convs,
2181 access_path, conversion_path, viable, reason);
2184 static void
2185 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2186 tree type1, tree type2, tree *args, tree *argtypes,
2187 int flags, tsubst_flags_t complain)
2189 conversion *t;
2190 conversion **convs;
2191 size_t num_convs;
2192 int viable = 1, i;
2193 tree types[2];
2194 struct rejection_reason *reason = NULL;
2196 types[0] = type1;
2197 types[1] = type2;
2199 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
2200 convs = alloc_conversions (num_convs);
2202 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2203 conversion ops are allowed. We handle that here by just checking for
2204 boolean_type_node because other operators don't ask for it. COND_EXPR
2205 also does contextual conversion to bool for the first operand, but we
2206 handle that in build_conditional_expr, and type1 here is operand 2. */
2207 if (type1 != boolean_type_node)
2208 flags |= LOOKUP_ONLYCONVERTING;
2210 for (i = 0; i < 2; ++i)
2212 if (! args[i])
2213 break;
2215 t = implicit_conversion (types[i], argtypes[i], args[i],
2216 /*c_cast_p=*/false, flags, complain);
2217 if (! t)
2219 viable = 0;
2220 /* We need something for printing the candidate. */
2221 t = build_identity_conv (types[i], NULL_TREE);
2222 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2223 types[i]);
2225 else if (t->bad_p)
2227 viable = 0;
2228 reason = bad_arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2229 types[i]);
2231 convs[i] = t;
2234 /* For COND_EXPR we rearranged the arguments; undo that now. */
2235 if (args[2])
2237 convs[2] = convs[1];
2238 convs[1] = convs[0];
2239 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2240 /*c_cast_p=*/false, flags,
2241 complain);
2242 if (t)
2243 convs[0] = t;
2244 else
2246 viable = 0;
2247 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2248 boolean_type_node);
2252 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2253 num_convs, convs,
2254 /*access_path=*/NULL_TREE,
2255 /*conversion_path=*/NULL_TREE,
2256 viable, reason);
2259 static bool
2260 is_complete (tree t)
2262 return COMPLETE_TYPE_P (complete_type (t));
2265 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2267 static bool
2268 promoted_arithmetic_type_p (tree type)
2270 /* [over.built]
2272 In this section, the term promoted integral type is used to refer
2273 to those integral types which are preserved by integral promotion
2274 (including e.g. int and long but excluding e.g. char).
2275 Similarly, the term promoted arithmetic type refers to promoted
2276 integral types plus floating types. */
2277 return ((CP_INTEGRAL_TYPE_P (type)
2278 && same_type_p (type_promotes_to (type), type))
2279 || TREE_CODE (type) == REAL_TYPE);
2282 /* Create any builtin operator overload candidates for the operator in
2283 question given the converted operand types TYPE1 and TYPE2. The other
2284 args are passed through from add_builtin_candidates to
2285 build_builtin_candidate.
2287 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2288 If CODE is requires candidates operands of the same type of the kind
2289 of which TYPE1 and TYPE2 are, we add both candidates
2290 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2292 static void
2293 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2294 enum tree_code code2, tree fnname, tree type1,
2295 tree type2, tree *args, tree *argtypes, int flags,
2296 tsubst_flags_t complain)
2298 switch (code)
2300 case POSTINCREMENT_EXPR:
2301 case POSTDECREMENT_EXPR:
2302 args[1] = integer_zero_node;
2303 type2 = integer_type_node;
2304 break;
2305 default:
2306 break;
2309 switch (code)
2312 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2313 and VQ is either volatile or empty, there exist candidate operator
2314 functions of the form
2315 VQ T& operator++(VQ T&);
2316 T operator++(VQ T&, int);
2317 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
2318 type other than bool, and VQ is either volatile or empty, there exist
2319 candidate operator functions of the form
2320 VQ T& operator--(VQ T&);
2321 T operator--(VQ T&, int);
2322 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
2323 complete object type, and VQ is either volatile or empty, there exist
2324 candidate operator functions of the form
2325 T*VQ& operator++(T*VQ&);
2326 T*VQ& operator--(T*VQ&);
2327 T* operator++(T*VQ&, int);
2328 T* operator--(T*VQ&, int); */
2330 case POSTDECREMENT_EXPR:
2331 case PREDECREMENT_EXPR:
2332 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2333 return;
2334 case POSTINCREMENT_EXPR:
2335 case PREINCREMENT_EXPR:
2336 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2338 type1 = build_reference_type (type1);
2339 break;
2341 return;
2343 /* 7 For every cv-qualified or cv-unqualified object type T, there
2344 exist candidate operator functions of the form
2346 T& operator*(T*);
2348 8 For every function type T, there exist candidate operator functions of
2349 the form
2350 T& operator*(T*); */
2352 case INDIRECT_REF:
2353 if (TYPE_PTR_P (type1)
2354 && !uses_template_parms (TREE_TYPE (type1))
2355 && (TYPE_PTROB_P (type1)
2356 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2357 break;
2358 return;
2360 /* 9 For every type T, there exist candidate operator functions of the form
2361 T* operator+(T*);
2363 10For every promoted arithmetic type T, there exist candidate operator
2364 functions of the form
2365 T operator+(T);
2366 T operator-(T); */
2368 case UNARY_PLUS_EXPR: /* unary + */
2369 if (TYPE_PTR_P (type1))
2370 break;
2371 case NEGATE_EXPR:
2372 if (ARITHMETIC_TYPE_P (type1))
2373 break;
2374 return;
2376 /* 11For every promoted integral type T, there exist candidate operator
2377 functions of the form
2378 T operator~(T); */
2380 case BIT_NOT_EXPR:
2381 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2382 break;
2383 return;
2385 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2386 is the same type as C2 or is a derived class of C2, T is a complete
2387 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2388 there exist candidate operator functions of the form
2389 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2390 where CV12 is the union of CV1 and CV2. */
2392 case MEMBER_REF:
2393 if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2395 tree c1 = TREE_TYPE (type1);
2396 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2398 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2399 && (TYPE_PTRMEMFUNC_P (type2)
2400 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2401 break;
2403 return;
2405 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2406 didate operator functions of the form
2407 LR operator*(L, R);
2408 LR operator/(L, R);
2409 LR operator+(L, R);
2410 LR operator-(L, R);
2411 bool operator<(L, R);
2412 bool operator>(L, R);
2413 bool operator<=(L, R);
2414 bool operator>=(L, R);
2415 bool operator==(L, R);
2416 bool operator!=(L, R);
2417 where LR is the result of the usual arithmetic conversions between
2418 types L and R.
2420 14For every pair of types T and I, where T is a cv-qualified or cv-
2421 unqualified complete object type and I is a promoted integral type,
2422 there exist candidate operator functions of the form
2423 T* operator+(T*, I);
2424 T& operator[](T*, I);
2425 T* operator-(T*, I);
2426 T* operator+(I, T*);
2427 T& operator[](I, T*);
2429 15For every T, where T is a pointer to complete object type, there exist
2430 candidate operator functions of the form112)
2431 ptrdiff_t operator-(T, T);
2433 16For every pointer or enumeration type T, there exist candidate operator
2434 functions of the form
2435 bool operator<(T, T);
2436 bool operator>(T, T);
2437 bool operator<=(T, T);
2438 bool operator>=(T, T);
2439 bool operator==(T, T);
2440 bool operator!=(T, T);
2442 17For every pointer to member type T, there exist candidate operator
2443 functions of the form
2444 bool operator==(T, T);
2445 bool operator!=(T, T); */
2447 case MINUS_EXPR:
2448 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2449 break;
2450 if (TYPE_PTROB_P (type1)
2451 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2453 type2 = ptrdiff_type_node;
2454 break;
2456 case MULT_EXPR:
2457 case TRUNC_DIV_EXPR:
2458 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2459 break;
2460 return;
2462 case EQ_EXPR:
2463 case NE_EXPR:
2464 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2465 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
2466 break;
2467 if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
2469 type2 = type1;
2470 break;
2472 if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
2474 type1 = type2;
2475 break;
2477 /* Fall through. */
2478 case LT_EXPR:
2479 case GT_EXPR:
2480 case LE_EXPR:
2481 case GE_EXPR:
2482 case MAX_EXPR:
2483 case MIN_EXPR:
2484 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2485 break;
2486 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2487 break;
2488 if (TREE_CODE (type1) == ENUMERAL_TYPE
2489 && TREE_CODE (type2) == ENUMERAL_TYPE)
2490 break;
2491 if (TYPE_PTR_P (type1)
2492 && null_ptr_cst_p (args[1])
2493 && !uses_template_parms (type1))
2495 type2 = type1;
2496 break;
2498 if (null_ptr_cst_p (args[0])
2499 && TYPE_PTR_P (type2)
2500 && !uses_template_parms (type2))
2502 type1 = type2;
2503 break;
2505 return;
2507 case PLUS_EXPR:
2508 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2509 break;
2510 case ARRAY_REF:
2511 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2513 type1 = ptrdiff_type_node;
2514 break;
2516 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2518 type2 = ptrdiff_type_node;
2519 break;
2521 return;
2523 /* 18For every pair of promoted integral types L and R, there exist candi-
2524 date operator functions of the form
2525 LR operator%(L, R);
2526 LR operator&(L, R);
2527 LR operator^(L, R);
2528 LR operator|(L, R);
2529 L operator<<(L, R);
2530 L operator>>(L, R);
2531 where LR is the result of the usual arithmetic conversions between
2532 types L and R. */
2534 case TRUNC_MOD_EXPR:
2535 case BIT_AND_EXPR:
2536 case BIT_IOR_EXPR:
2537 case BIT_XOR_EXPR:
2538 case LSHIFT_EXPR:
2539 case RSHIFT_EXPR:
2540 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2541 break;
2542 return;
2544 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2545 type, VQ is either volatile or empty, and R is a promoted arithmetic
2546 type, there exist candidate operator functions of the form
2547 VQ L& operator=(VQ L&, R);
2548 VQ L& operator*=(VQ L&, R);
2549 VQ L& operator/=(VQ L&, R);
2550 VQ L& operator+=(VQ L&, R);
2551 VQ L& operator-=(VQ L&, R);
2553 20For every pair T, VQ), where T is any type and VQ is either volatile
2554 or empty, there exist candidate operator functions of the form
2555 T*VQ& operator=(T*VQ&, T*);
2557 21For every pair T, VQ), where T is a pointer to member type and VQ is
2558 either volatile or empty, there exist candidate operator functions of
2559 the form
2560 VQ T& operator=(VQ T&, T);
2562 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2563 unqualified complete object type, VQ is either volatile or empty, and
2564 I is a promoted integral type, there exist candidate operator func-
2565 tions of the form
2566 T*VQ& operator+=(T*VQ&, I);
2567 T*VQ& operator-=(T*VQ&, I);
2569 23For every triple L, VQ, R), where L is an integral or enumeration
2570 type, VQ is either volatile or empty, and R is a promoted integral
2571 type, there exist candidate operator functions of the form
2573 VQ L& operator%=(VQ L&, R);
2574 VQ L& operator<<=(VQ L&, R);
2575 VQ L& operator>>=(VQ L&, R);
2576 VQ L& operator&=(VQ L&, R);
2577 VQ L& operator^=(VQ L&, R);
2578 VQ L& operator|=(VQ L&, R); */
2580 case MODIFY_EXPR:
2581 switch (code2)
2583 case PLUS_EXPR:
2584 case MINUS_EXPR:
2585 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2587 type2 = ptrdiff_type_node;
2588 break;
2590 case MULT_EXPR:
2591 case TRUNC_DIV_EXPR:
2592 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2593 break;
2594 return;
2596 case TRUNC_MOD_EXPR:
2597 case BIT_AND_EXPR:
2598 case BIT_IOR_EXPR:
2599 case BIT_XOR_EXPR:
2600 case LSHIFT_EXPR:
2601 case RSHIFT_EXPR:
2602 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2603 break;
2604 return;
2606 case NOP_EXPR:
2607 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2608 break;
2609 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2610 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2611 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2612 || ((TYPE_PTRMEMFUNC_P (type1)
2613 || TYPE_PTR_P (type1))
2614 && null_ptr_cst_p (args[1])))
2616 type2 = type1;
2617 break;
2619 return;
2621 default:
2622 gcc_unreachable ();
2624 type1 = build_reference_type (type1);
2625 break;
2627 case COND_EXPR:
2628 /* [over.built]
2630 For every pair of promoted arithmetic types L and R, there
2631 exist candidate operator functions of the form
2633 LR operator?(bool, L, R);
2635 where LR is the result of the usual arithmetic conversions
2636 between types L and R.
2638 For every type T, where T is a pointer or pointer-to-member
2639 type, there exist candidate operator functions of the form T
2640 operator?(bool, T, T); */
2642 if (promoted_arithmetic_type_p (type1)
2643 && promoted_arithmetic_type_p (type2))
2644 /* That's OK. */
2645 break;
2647 /* Otherwise, the types should be pointers. */
2648 if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
2649 return;
2651 /* We don't check that the two types are the same; the logic
2652 below will actually create two candidates; one in which both
2653 parameter types are TYPE1, and one in which both parameter
2654 types are TYPE2. */
2655 break;
2657 case REALPART_EXPR:
2658 case IMAGPART_EXPR:
2659 if (ARITHMETIC_TYPE_P (type1))
2660 break;
2661 return;
2663 default:
2664 gcc_unreachable ();
2667 /* If we're dealing with two pointer types or two enumeral types,
2668 we need candidates for both of them. */
2669 if (type2 && !same_type_p (type1, type2)
2670 && TREE_CODE (type1) == TREE_CODE (type2)
2671 && (TREE_CODE (type1) == REFERENCE_TYPE
2672 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2673 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2674 || TYPE_PTRMEMFUNC_P (type1)
2675 || MAYBE_CLASS_TYPE_P (type1)
2676 || TREE_CODE (type1) == ENUMERAL_TYPE))
2678 if (TYPE_PTR_OR_PTRMEM_P (type1))
2680 tree cptype = composite_pointer_type (type1, type2,
2681 error_mark_node,
2682 error_mark_node,
2683 CPO_CONVERSION,
2684 tf_none);
2685 if (cptype != error_mark_node)
2687 build_builtin_candidate
2688 (candidates, fnname, cptype, cptype, args, argtypes,
2689 flags, complain);
2690 return;
2694 build_builtin_candidate
2695 (candidates, fnname, type1, type1, args, argtypes, flags, complain);
2696 build_builtin_candidate
2697 (candidates, fnname, type2, type2, args, argtypes, flags, complain);
2698 return;
2701 build_builtin_candidate
2702 (candidates, fnname, type1, type2, args, argtypes, flags, complain);
2705 tree
2706 type_decays_to (tree type)
2708 if (TREE_CODE (type) == ARRAY_TYPE)
2709 return build_pointer_type (TREE_TYPE (type));
2710 if (TREE_CODE (type) == FUNCTION_TYPE)
2711 return build_pointer_type (type);
2712 return type;
2715 /* There are three conditions of builtin candidates:
2717 1) bool-taking candidates. These are the same regardless of the input.
2718 2) pointer-pair taking candidates. These are generated for each type
2719 one of the input types converts to.
2720 3) arithmetic candidates. According to the standard, we should generate
2721 all of these, but I'm trying not to...
2723 Here we generate a superset of the possible candidates for this particular
2724 case. That is a subset of the full set the standard defines, plus some
2725 other cases which the standard disallows. add_builtin_candidate will
2726 filter out the invalid set. */
2728 static void
2729 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2730 enum tree_code code2, tree fnname, tree *args,
2731 int flags, tsubst_flags_t complain)
2733 int ref1, i;
2734 int enum_p = 0;
2735 tree type, argtypes[3], t;
2736 /* TYPES[i] is the set of possible builtin-operator parameter types
2737 we will consider for the Ith argument. */
2738 vec<tree, va_gc> *types[2];
2739 unsigned ix;
2741 for (i = 0; i < 3; ++i)
2743 if (args[i])
2744 argtypes[i] = unlowered_expr_type (args[i]);
2745 else
2746 argtypes[i] = NULL_TREE;
2749 switch (code)
2751 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2752 and VQ is either volatile or empty, there exist candidate operator
2753 functions of the form
2754 VQ T& operator++(VQ T&); */
2756 case POSTINCREMENT_EXPR:
2757 case PREINCREMENT_EXPR:
2758 case POSTDECREMENT_EXPR:
2759 case PREDECREMENT_EXPR:
2760 case MODIFY_EXPR:
2761 ref1 = 1;
2762 break;
2764 /* 24There also exist candidate operator functions of the form
2765 bool operator!(bool);
2766 bool operator&&(bool, bool);
2767 bool operator||(bool, bool); */
2769 case TRUTH_NOT_EXPR:
2770 build_builtin_candidate
2771 (candidates, fnname, boolean_type_node,
2772 NULL_TREE, args, argtypes, flags, complain);
2773 return;
2775 case TRUTH_ORIF_EXPR:
2776 case TRUTH_ANDIF_EXPR:
2777 build_builtin_candidate
2778 (candidates, fnname, boolean_type_node,
2779 boolean_type_node, args, argtypes, flags, complain);
2780 return;
2782 case ADDR_EXPR:
2783 case COMPOUND_EXPR:
2784 case COMPONENT_REF:
2785 return;
2787 case COND_EXPR:
2788 case EQ_EXPR:
2789 case NE_EXPR:
2790 case LT_EXPR:
2791 case LE_EXPR:
2792 case GT_EXPR:
2793 case GE_EXPR:
2794 enum_p = 1;
2795 /* Fall through. */
2797 default:
2798 ref1 = 0;
2801 types[0] = make_tree_vector ();
2802 types[1] = make_tree_vector ();
2804 for (i = 0; i < 2; ++i)
2806 if (! args[i])
2808 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
2810 tree convs;
2812 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2813 return;
2815 convs = lookup_conversions (argtypes[i]);
2817 if (code == COND_EXPR)
2819 if (real_lvalue_p (args[i]))
2820 vec_safe_push (types[i], build_reference_type (argtypes[i]));
2822 vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
2825 else if (! convs)
2826 return;
2828 for (; convs; convs = TREE_CHAIN (convs))
2830 type = TREE_TYPE (convs);
2832 if (i == 0 && ref1
2833 && (TREE_CODE (type) != REFERENCE_TYPE
2834 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2835 continue;
2837 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2838 vec_safe_push (types[i], type);
2840 type = non_reference (type);
2841 if (i != 0 || ! ref1)
2843 type = cv_unqualified (type_decays_to (type));
2844 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2845 vec_safe_push (types[i], type);
2846 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2847 type = type_promotes_to (type);
2850 if (! vec_member (type, types[i]))
2851 vec_safe_push (types[i], type);
2854 else
2856 if (code == COND_EXPR && real_lvalue_p (args[i]))
2857 vec_safe_push (types[i], build_reference_type (argtypes[i]));
2858 type = non_reference (argtypes[i]);
2859 if (i != 0 || ! ref1)
2861 type = cv_unqualified (type_decays_to (type));
2862 if (enum_p && UNSCOPED_ENUM_P (type))
2863 vec_safe_push (types[i], type);
2864 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2865 type = type_promotes_to (type);
2867 vec_safe_push (types[i], type);
2871 /* Run through the possible parameter types of both arguments,
2872 creating candidates with those parameter types. */
2873 FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
2875 unsigned jx;
2876 tree u;
2878 if (!types[1]->is_empty ())
2879 FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
2880 add_builtin_candidate
2881 (candidates, code, code2, fnname, t,
2882 u, args, argtypes, flags, complain);
2883 else
2884 add_builtin_candidate
2885 (candidates, code, code2, fnname, t,
2886 NULL_TREE, args, argtypes, flags, complain);
2889 release_tree_vector (types[0]);
2890 release_tree_vector (types[1]);
2894 /* If TMPL can be successfully instantiated as indicated by
2895 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2897 TMPL is the template. EXPLICIT_TARGS are any explicit template
2898 arguments. ARGLIST is the arguments provided at the call-site.
2899 This does not change ARGLIST. The RETURN_TYPE is the desired type
2900 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
2901 as for add_function_candidate. If an OBJ is supplied, FLAGS and
2902 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
2904 static struct z_candidate*
2905 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2906 tree ctype, tree explicit_targs, tree first_arg,
2907 const vec<tree, va_gc> *arglist, tree return_type,
2908 tree access_path, tree conversion_path,
2909 int flags, tree obj, unification_kind_t strict,
2910 tsubst_flags_t complain)
2912 int ntparms = DECL_NTPARMS (tmpl);
2913 tree targs = make_tree_vec (ntparms);
2914 unsigned int len = vec_safe_length (arglist);
2915 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
2916 unsigned int skip_without_in_chrg = 0;
2917 tree first_arg_without_in_chrg = first_arg;
2918 tree *args_without_in_chrg;
2919 unsigned int nargs_without_in_chrg;
2920 unsigned int ia, ix;
2921 tree arg;
2922 struct z_candidate *cand;
2923 tree fn;
2924 struct rejection_reason *reason = NULL;
2925 int errs;
2927 /* We don't do deduction on the in-charge parameter, the VTT
2928 parameter or 'this'. */
2929 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2931 if (first_arg_without_in_chrg != NULL_TREE)
2932 first_arg_without_in_chrg = NULL_TREE;
2933 else
2934 ++skip_without_in_chrg;
2937 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2938 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2939 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
2941 if (first_arg_without_in_chrg != NULL_TREE)
2942 first_arg_without_in_chrg = NULL_TREE;
2943 else
2944 ++skip_without_in_chrg;
2947 if (len < skip_without_in_chrg)
2948 return NULL;
2950 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
2951 + (len - skip_without_in_chrg));
2952 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
2953 ia = 0;
2954 if (first_arg_without_in_chrg != NULL_TREE)
2956 args_without_in_chrg[ia] = first_arg_without_in_chrg;
2957 ++ia;
2959 for (ix = skip_without_in_chrg;
2960 vec_safe_iterate (arglist, ix, &arg);
2961 ++ix)
2963 args_without_in_chrg[ia] = arg;
2964 ++ia;
2966 gcc_assert (ia == nargs_without_in_chrg);
2968 errs = errorcount+sorrycount;
2969 fn = fn_type_unification (tmpl, explicit_targs, targs,
2970 args_without_in_chrg,
2971 nargs_without_in_chrg,
2972 return_type, strict, flags, false,
2973 complain & tf_decltype);
2975 if (fn == error_mark_node)
2977 if (errorcount+sorrycount == errs)
2978 /* Don't repeat unification later if it already resulted in errors. */
2979 reason = template_unification_rejection (tmpl, explicit_targs,
2980 targs, args_without_in_chrg,
2981 nargs_without_in_chrg,
2982 return_type, strict, flags);
2983 else
2984 reason = template_unification_error_rejection ();
2985 goto fail;
2988 /* In [class.copy]:
2990 A member function template is never instantiated to perform the
2991 copy of a class object to an object of its class type.
2993 It's a little unclear what this means; the standard explicitly
2994 does allow a template to be used to copy a class. For example,
2997 struct A {
2998 A(A&);
2999 template <class T> A(const T&);
3001 const A f ();
3002 void g () { A a (f ()); }
3004 the member template will be used to make the copy. The section
3005 quoted above appears in the paragraph that forbids constructors
3006 whose only parameter is (a possibly cv-qualified variant of) the
3007 class type, and a logical interpretation is that the intent was
3008 to forbid the instantiation of member templates which would then
3009 have that form. */
3010 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3012 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3013 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3014 ctype))
3016 reason = invalid_copy_with_fn_template_rejection ();
3017 goto fail;
3021 if (obj != NULL_TREE)
3022 /* Aha, this is a conversion function. */
3023 cand = add_conv_candidate (candidates, fn, obj, first_arg, arglist,
3024 access_path, conversion_path, complain);
3025 else
3026 cand = add_function_candidate (candidates, fn, ctype,
3027 first_arg, arglist, access_path,
3028 conversion_path, flags, complain);
3029 if (DECL_TI_TEMPLATE (fn) != tmpl)
3031 /* This situation can occur if a member template of a template
3032 class is specialized. Then, instantiate_template might return
3033 an instantiation of the specialization, in which case the
3034 DECL_TI_TEMPLATE field will point at the original
3035 specialization. For example:
3037 template <class T> struct S { template <class U> void f(U);
3038 template <> void f(int) {}; };
3039 S<double> sd;
3040 sd.f(3);
3042 Here, TMPL will be template <class U> S<double>::f(U).
3043 And, instantiate template will give us the specialization
3044 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3045 for this will point at template <class T> template <> S<T>::f(int),
3046 so that we can find the definition. For the purposes of
3047 overload resolution, however, we want the original TMPL. */
3048 cand->template_decl = build_template_info (tmpl, targs);
3050 else
3051 cand->template_decl = DECL_TEMPLATE_INFO (fn);
3052 cand->explicit_targs = explicit_targs;
3054 return cand;
3055 fail:
3056 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
3057 access_path, conversion_path, 0, reason);
3061 static struct z_candidate *
3062 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3063 tree explicit_targs, tree first_arg,
3064 const vec<tree, va_gc> *arglist, tree return_type,
3065 tree access_path, tree conversion_path, int flags,
3066 unification_kind_t strict, tsubst_flags_t complain)
3068 return
3069 add_template_candidate_real (candidates, tmpl, ctype,
3070 explicit_targs, first_arg, arglist,
3071 return_type, access_path, conversion_path,
3072 flags, NULL_TREE, strict, complain);
3076 static struct z_candidate *
3077 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3078 tree obj, tree first_arg,
3079 const vec<tree, va_gc> *arglist,
3080 tree return_type, tree access_path,
3081 tree conversion_path, tsubst_flags_t complain)
3083 return
3084 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3085 first_arg, arglist, return_type, access_path,
3086 conversion_path, 0, obj, DEDUCE_CONV,
3087 complain);
3090 /* The CANDS are the set of candidates that were considered for
3091 overload resolution. Return the set of viable candidates, or CANDS
3092 if none are viable. If any of the candidates were viable, set
3093 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3094 considered viable only if it is strictly viable. */
3096 static struct z_candidate*
3097 splice_viable (struct z_candidate *cands,
3098 bool strict_p,
3099 bool *any_viable_p)
3101 struct z_candidate *viable;
3102 struct z_candidate **last_viable;
3103 struct z_candidate **cand;
3105 /* Be strict inside templates, since build_over_call won't actually
3106 do the conversions to get pedwarns. */
3107 if (processing_template_decl)
3108 strict_p = true;
3110 viable = NULL;
3111 last_viable = &viable;
3112 *any_viable_p = false;
3114 cand = &cands;
3115 while (*cand)
3117 struct z_candidate *c = *cand;
3118 if (strict_p ? c->viable == 1 : c->viable)
3120 *last_viable = c;
3121 *cand = c->next;
3122 c->next = NULL;
3123 last_viable = &c->next;
3124 *any_viable_p = true;
3126 else
3127 cand = &c->next;
3130 return viable ? viable : cands;
3133 static bool
3134 any_strictly_viable (struct z_candidate *cands)
3136 for (; cands; cands = cands->next)
3137 if (cands->viable == 1)
3138 return true;
3139 return false;
3142 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3143 words, it is about to become the "this" pointer for a member
3144 function call. Take the address of the object. */
3146 static tree
3147 build_this (tree obj)
3149 /* In a template, we are only concerned about the type of the
3150 expression, so we can take a shortcut. */
3151 if (processing_template_decl)
3152 return build_address (obj);
3154 return cp_build_addr_expr (obj, tf_warning_or_error);
3157 /* Returns true iff functions are equivalent. Equivalent functions are
3158 not '==' only if one is a function-local extern function or if
3159 both are extern "C". */
3161 static inline int
3162 equal_functions (tree fn1, tree fn2)
3164 if (TREE_CODE (fn1) != TREE_CODE (fn2))
3165 return 0;
3166 if (TREE_CODE (fn1) == TEMPLATE_DECL)
3167 return fn1 == fn2;
3168 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
3169 || DECL_EXTERN_C_FUNCTION_P (fn1))
3170 return decls_match (fn1, fn2);
3171 return fn1 == fn2;
3174 /* Print information about a candidate being rejected due to INFO. */
3176 static void
3177 print_conversion_rejection (location_t loc, struct conversion_info *info)
3179 if (info->n_arg == -1)
3180 /* Conversion of implicit `this' argument failed. */
3181 inform (loc, " no known conversion for implicit "
3182 "%<this%> parameter from %qT to %qT",
3183 info->from_type, info->to_type);
3184 else
3185 inform (loc, " no known conversion for argument %d from %qT to %qT",
3186 info->n_arg+1, info->from_type, info->to_type);
3189 /* Print information about a candidate with WANT parameters and we found
3190 HAVE. */
3192 static void
3193 print_arity_information (location_t loc, unsigned int have, unsigned int want)
3195 inform_n (loc, want,
3196 " candidate expects %d argument, %d provided",
3197 " candidate expects %d arguments, %d provided",
3198 want, have);
3201 /* Print information about one overload candidate CANDIDATE. MSGSTR
3202 is the text to print before the candidate itself.
3204 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3205 to have been run through gettext by the caller. This wart makes
3206 life simpler in print_z_candidates and for the translators. */
3208 static void
3209 print_z_candidate (location_t loc, const char *msgstr,
3210 struct z_candidate *candidate)
3212 const char *msg = (msgstr == NULL
3213 ? ""
3214 : ACONCAT ((msgstr, " ", NULL)));
3215 location_t cloc = location_of (candidate->fn);
3217 if (identifier_p (candidate->fn))
3219 cloc = loc;
3220 if (candidate->num_convs == 3)
3221 inform (cloc, "%s%D(%T, %T, %T) <built-in>", msg, candidate->fn,
3222 candidate->convs[0]->type,
3223 candidate->convs[1]->type,
3224 candidate->convs[2]->type);
3225 else if (candidate->num_convs == 2)
3226 inform (cloc, "%s%D(%T, %T) <built-in>", msg, candidate->fn,
3227 candidate->convs[0]->type,
3228 candidate->convs[1]->type);
3229 else
3230 inform (cloc, "%s%D(%T) <built-in>", msg, candidate->fn,
3231 candidate->convs[0]->type);
3233 else if (TYPE_P (candidate->fn))
3234 inform (cloc, "%s%T <conversion>", msg, candidate->fn);
3235 else if (candidate->viable == -1)
3236 inform (cloc, "%s%#D <near match>", msg, candidate->fn);
3237 else if (DECL_DELETED_FN (STRIP_TEMPLATE (candidate->fn)))
3238 inform (cloc, "%s%#D <deleted>", msg, candidate->fn);
3239 else
3240 inform (cloc, "%s%#D", msg, candidate->fn);
3241 /* Give the user some information about why this candidate failed. */
3242 if (candidate->reason != NULL)
3244 struct rejection_reason *r = candidate->reason;
3246 switch (r->code)
3248 case rr_arity:
3249 print_arity_information (cloc, r->u.arity.actual,
3250 r->u.arity.expected);
3251 break;
3252 case rr_arg_conversion:
3253 print_conversion_rejection (cloc, &r->u.conversion);
3254 break;
3255 case rr_bad_arg_conversion:
3256 print_conversion_rejection (cloc, &r->u.bad_conversion);
3257 break;
3258 case rr_explicit_conversion:
3259 inform (cloc, " return type %qT of explicit conversion function "
3260 "cannot be converted to %qT with a qualification "
3261 "conversion", r->u.conversion.from_type,
3262 r->u.conversion.to_type);
3263 break;
3264 case rr_template_conversion:
3265 inform (cloc, " conversion from return type %qT of template "
3266 "conversion function specialization to %qT is not an "
3267 "exact match", r->u.conversion.from_type,
3268 r->u.conversion.to_type);
3269 break;
3270 case rr_template_unification:
3271 /* We use template_unification_error_rejection if unification caused
3272 actual non-SFINAE errors, in which case we don't need to repeat
3273 them here. */
3274 if (r->u.template_unification.tmpl == NULL_TREE)
3276 inform (cloc, " substitution of deduced template arguments "
3277 "resulted in errors seen above");
3278 break;
3280 /* Re-run template unification with diagnostics. */
3281 inform (cloc, " template argument deduction/substitution failed:");
3282 fn_type_unification (r->u.template_unification.tmpl,
3283 r->u.template_unification.explicit_targs,
3284 (make_tree_vec
3285 (r->u.template_unification.num_targs)),
3286 r->u.template_unification.args,
3287 r->u.template_unification.nargs,
3288 r->u.template_unification.return_type,
3289 r->u.template_unification.strict,
3290 r->u.template_unification.flags,
3291 true, false);
3292 break;
3293 case rr_invalid_copy:
3294 inform (cloc,
3295 " a constructor taking a single argument of its own "
3296 "class type is invalid");
3297 break;
3298 case rr_constraint_failure:
3300 tree tmpl = r->u.template_instantiation.tmpl;
3301 tree args = r->u.template_instantiation.targs;
3302 diagnose_constraints (cloc, tmpl, args);
3304 break;
3305 case rr_none:
3306 default:
3307 /* This candidate didn't have any issues or we failed to
3308 handle a particular code. Either way... */
3309 gcc_unreachable ();
3314 static void
3315 print_z_candidates (location_t loc, struct z_candidate *candidates)
3317 struct z_candidate *cand1;
3318 struct z_candidate **cand2;
3319 int n_candidates;
3321 if (!candidates)
3322 return;
3324 /* Remove non-viable deleted candidates. */
3325 cand1 = candidates;
3326 for (cand2 = &cand1; *cand2; )
3328 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
3329 && !(*cand2)->viable
3330 && DECL_DELETED_FN ((*cand2)->fn))
3331 *cand2 = (*cand2)->next;
3332 else
3333 cand2 = &(*cand2)->next;
3335 /* ...if there are any non-deleted ones. */
3336 if (cand1)
3337 candidates = cand1;
3339 /* There may be duplicates in the set of candidates. We put off
3340 checking this condition as long as possible, since we have no way
3341 to eliminate duplicates from a set of functions in less than n^2
3342 time. Now we are about to emit an error message, so it is more
3343 permissible to go slowly. */
3344 for (cand1 = candidates; cand1; cand1 = cand1->next)
3346 tree fn = cand1->fn;
3347 /* Skip builtin candidates and conversion functions. */
3348 if (!DECL_P (fn))
3349 continue;
3350 cand2 = &cand1->next;
3351 while (*cand2)
3353 if (DECL_P ((*cand2)->fn)
3354 && equal_functions (fn, (*cand2)->fn))
3355 *cand2 = (*cand2)->next;
3356 else
3357 cand2 = &(*cand2)->next;
3361 for (n_candidates = 0, cand1 = candidates; cand1; cand1 = cand1->next)
3362 n_candidates++;
3364 inform_n (loc, n_candidates, "candidate is:", "candidates are:");
3365 for (; candidates; candidates = candidates->next)
3366 print_z_candidate (loc, NULL, candidates);
3369 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3370 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3371 the result of the conversion function to convert it to the final
3372 desired type. Merge the two sequences into a single sequence,
3373 and return the merged sequence. */
3375 static conversion *
3376 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
3378 conversion **t;
3379 bool bad = user_seq->bad_p;
3381 gcc_assert (user_seq->kind == ck_user);
3383 /* Find the end of the second conversion sequence. */
3384 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
3386 /* The entire sequence is a user-conversion sequence. */
3387 (*t)->user_conv_p = true;
3388 if (bad)
3389 (*t)->bad_p = true;
3392 /* Replace the identity conversion with the user conversion
3393 sequence. */
3394 *t = user_seq;
3396 return std_seq;
3399 /* Handle overload resolution for initializing an object of class type from
3400 an initializer list. First we look for a suitable constructor that
3401 takes a std::initializer_list; if we don't find one, we then look for a
3402 non-list constructor.
3404 Parameters are as for add_candidates, except that the arguments are in
3405 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
3406 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3408 static void
3409 add_list_candidates (tree fns, tree first_arg,
3410 tree init_list, tree totype,
3411 tree explicit_targs, bool template_only,
3412 tree conversion_path, tree access_path,
3413 int flags,
3414 struct z_candidate **candidates,
3415 tsubst_flags_t complain)
3417 vec<tree, va_gc> *args;
3419 gcc_assert (*candidates == NULL);
3421 /* We're looking for a ctor for list-initialization. */
3422 flags |= LOOKUP_LIST_INIT_CTOR;
3423 /* And we don't allow narrowing conversions. We also use this flag to
3424 avoid the copy constructor call for copy-list-initialization. */
3425 flags |= LOOKUP_NO_NARROWING;
3427 /* Always use the default constructor if the list is empty (DR 990). */
3428 if (CONSTRUCTOR_NELTS (init_list) == 0
3429 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
3431 /* If the class has a list ctor, try passing the list as a single
3432 argument first, but only consider list ctors. */
3433 else if (TYPE_HAS_LIST_CTOR (totype))
3435 flags |= LOOKUP_LIST_ONLY;
3436 args = make_tree_vector_single (init_list);
3437 add_candidates (fns, first_arg, args, NULL_TREE,
3438 explicit_targs, template_only, conversion_path,
3439 access_path, flags, candidates, complain);
3440 if (any_strictly_viable (*candidates))
3441 return;
3444 args = ctor_to_vec (init_list);
3446 /* We aren't looking for list-ctors anymore. */
3447 flags &= ~LOOKUP_LIST_ONLY;
3448 /* We allow more user-defined conversions within an init-list. */
3449 flags &= ~LOOKUP_NO_CONVERSION;
3451 add_candidates (fns, first_arg, args, NULL_TREE,
3452 explicit_targs, template_only, conversion_path,
3453 access_path, flags, candidates, complain);
3456 /* Returns the best overload candidate to perform the requested
3457 conversion. This function is used for three the overloading situations
3458 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3459 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3460 per [dcl.init.ref], so we ignore temporary bindings. */
3462 static struct z_candidate *
3463 build_user_type_conversion_1 (tree totype, tree expr, int flags,
3464 tsubst_flags_t complain)
3466 struct z_candidate *candidates, *cand;
3467 tree fromtype;
3468 tree ctors = NULL_TREE;
3469 tree conv_fns = NULL_TREE;
3470 conversion *conv = NULL;
3471 tree first_arg = NULL_TREE;
3472 vec<tree, va_gc> *args = NULL;
3473 bool any_viable_p;
3474 int convflags;
3476 if (!expr)
3477 return NULL;
3479 fromtype = TREE_TYPE (expr);
3481 /* We represent conversion within a hierarchy using RVALUE_CONV and
3482 BASE_CONV, as specified by [over.best.ics]; these become plain
3483 constructor calls, as specified in [dcl.init]. */
3484 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
3485 || !DERIVED_FROM_P (totype, fromtype));
3487 if (MAYBE_CLASS_TYPE_P (totype))
3488 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
3489 creating a garbage BASELINK; constructors can't be inherited. */
3490 ctors = lookup_fnfields_slot (totype, complete_ctor_identifier);
3492 if (MAYBE_CLASS_TYPE_P (fromtype))
3494 tree to_nonref = non_reference (totype);
3495 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
3496 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
3497 && DERIVED_FROM_P (to_nonref, fromtype)))
3499 /* [class.conv.fct] A conversion function is never used to
3500 convert a (possibly cv-qualified) object to the (possibly
3501 cv-qualified) same object type (or a reference to it), to a
3502 (possibly cv-qualified) base class of that type (or a
3503 reference to it)... */
3505 else
3506 conv_fns = lookup_conversions (fromtype);
3509 candidates = 0;
3510 flags |= LOOKUP_NO_CONVERSION;
3511 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3512 flags |= LOOKUP_NO_NARROWING;
3514 /* It's OK to bind a temporary for converting constructor arguments, but
3515 not in converting the return value of a conversion operator. */
3516 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION);
3517 flags &= ~LOOKUP_NO_TEMP_BIND;
3519 if (ctors)
3521 int ctorflags = flags;
3523 first_arg = build_int_cst (build_pointer_type (totype), 0);
3524 first_arg = build_fold_indirect_ref (first_arg);
3526 /* We should never try to call the abstract or base constructor
3527 from here. */
3528 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
3529 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)));
3531 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3533 /* List-initialization. */
3534 add_list_candidates (ctors, first_arg, expr, totype, NULL_TREE,
3535 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
3536 ctorflags, &candidates, complain);
3538 else
3540 args = make_tree_vector_single (expr);
3541 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
3542 TYPE_BINFO (totype), TYPE_BINFO (totype),
3543 ctorflags, &candidates, complain);
3546 for (cand = candidates; cand; cand = cand->next)
3548 cand->second_conv = build_identity_conv (totype, NULL_TREE);
3550 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
3551 set, then this is copy-initialization. In that case, "The
3552 result of the call is then used to direct-initialize the
3553 object that is the destination of the copy-initialization."
3554 [dcl.init]
3556 We represent this in the conversion sequence with an
3557 rvalue conversion, which means a constructor call. */
3558 if (TREE_CODE (totype) != REFERENCE_TYPE
3559 && !(convflags & LOOKUP_NO_TEMP_BIND))
3560 cand->second_conv
3561 = build_conv (ck_rvalue, totype, cand->second_conv);
3565 if (conv_fns)
3566 first_arg = expr;
3568 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
3570 tree conversion_path = TREE_PURPOSE (conv_fns);
3571 struct z_candidate *old_candidates;
3573 /* If we are called to convert to a reference type, we are trying to
3574 find a direct binding, so don't even consider temporaries. If
3575 we don't find a direct binding, the caller will try again to
3576 look for a temporary binding. */
3577 if (TREE_CODE (totype) == REFERENCE_TYPE)
3578 convflags |= LOOKUP_NO_TEMP_BIND;
3580 old_candidates = candidates;
3581 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3582 NULL_TREE, false,
3583 conversion_path, TYPE_BINFO (fromtype),
3584 flags, &candidates, complain);
3586 for (cand = candidates; cand != old_candidates; cand = cand->next)
3588 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
3589 conversion *ics
3590 = implicit_conversion (totype,
3591 rettype,
3593 /*c_cast_p=*/false, convflags,
3594 complain);
3596 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3597 copy-initialization. In that case, "The result of the
3598 call is then used to direct-initialize the object that is
3599 the destination of the copy-initialization." [dcl.init]
3601 We represent this in the conversion sequence with an
3602 rvalue conversion, which means a constructor call. But
3603 don't add a second rvalue conversion if there's already
3604 one there. Which there really shouldn't be, but it's
3605 harmless since we'd add it here anyway. */
3606 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3607 && !(convflags & LOOKUP_NO_TEMP_BIND))
3608 ics = build_conv (ck_rvalue, totype, ics);
3610 cand->second_conv = ics;
3612 if (!ics)
3614 cand->viable = 0;
3615 cand->reason = arg_conversion_rejection (NULL_TREE, -1,
3616 rettype, totype);
3618 else if (DECL_NONCONVERTING_P (cand->fn)
3619 && ics->rank > cr_exact)
3621 /* 13.3.1.5: For direct-initialization, those explicit
3622 conversion functions that are not hidden within S and
3623 yield type T or a type that can be converted to type T
3624 with a qualification conversion (4.4) are also candidate
3625 functions. */
3626 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
3627 I've raised this issue with the committee. --jason 9/2011 */
3628 cand->viable = -1;
3629 cand->reason = explicit_conversion_rejection (rettype, totype);
3631 else if (cand->viable == 1 && ics->bad_p)
3633 cand->viable = -1;
3634 cand->reason
3635 = bad_arg_conversion_rejection (NULL_TREE, -1,
3636 rettype, totype);
3638 else if (primary_template_instantiation_p (cand->fn)
3639 && ics->rank > cr_exact)
3641 /* 13.3.3.1.2: If the user-defined conversion is specified by
3642 a specialization of a conversion function template, the
3643 second standard conversion sequence shall have exact match
3644 rank. */
3645 cand->viable = -1;
3646 cand->reason = template_conversion_rejection (rettype, totype);
3651 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3652 if (!any_viable_p)
3654 if (args)
3655 release_tree_vector (args);
3656 return NULL;
3659 cand = tourney (candidates, complain);
3660 if (cand == 0)
3662 if (complain & tf_error)
3664 error ("conversion from %qT to %qT is ambiguous",
3665 fromtype, totype);
3666 print_z_candidates (location_of (expr), candidates);
3669 cand = candidates; /* any one will do */
3670 cand->second_conv = build_ambiguous_conv (totype, expr);
3671 cand->second_conv->user_conv_p = true;
3672 if (!any_strictly_viable (candidates))
3673 cand->second_conv->bad_p = true;
3674 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3675 ambiguous conversion is no worse than another user-defined
3676 conversion. */
3678 return cand;
3681 /* Build the user conversion sequence. */
3682 conv = build_conv
3683 (ck_user,
3684 (DECL_CONSTRUCTOR_P (cand->fn)
3685 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
3686 build_identity_conv (TREE_TYPE (expr), expr));
3687 conv->cand = cand;
3688 if (cand->viable == -1)
3689 conv->bad_p = true;
3691 /* Remember that this was a list-initialization. */
3692 if (flags & LOOKUP_NO_NARROWING)
3693 conv->check_narrowing = true;
3695 /* Combine it with the second conversion sequence. */
3696 cand->second_conv = merge_conversion_sequences (conv,
3697 cand->second_conv);
3699 return cand;
3702 /* Wrapper for above. */
3704 tree
3705 build_user_type_conversion (tree totype, tree expr, int flags,
3706 tsubst_flags_t complain)
3708 struct z_candidate *cand;
3709 tree ret;
3711 bool subtime = timevar_cond_start (TV_OVERLOAD);
3712 cand = build_user_type_conversion_1 (totype, expr, flags, complain);
3714 if (cand)
3716 if (cand->second_conv->kind == ck_ambig)
3717 ret = error_mark_node;
3718 else
3720 expr = convert_like (cand->second_conv, expr, complain);
3721 ret = convert_from_reference (expr);
3724 else
3725 ret = NULL_TREE;
3727 timevar_cond_stop (TV_OVERLOAD, subtime);
3728 return ret;
3731 /* Subroutine of convert_nontype_argument.
3733 EXPR is an argument for a template non-type parameter of integral or
3734 enumeration type. Do any necessary conversions (that are permitted for
3735 non-type arguments) to convert it to the parameter type.
3737 If conversion is successful, returns the converted expression;
3738 otherwise, returns error_mark_node. */
3740 tree
3741 build_integral_nontype_arg_conv (tree type, tree expr, tsubst_flags_t complain)
3743 conversion *conv;
3744 void *p;
3745 tree t;
3746 location_t loc = EXPR_LOC_OR_HERE (expr);
3748 if (error_operand_p (expr))
3749 return error_mark_node;
3751 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
3753 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3754 p = conversion_obstack_alloc (0);
3756 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
3757 /*c_cast_p=*/false,
3758 LOOKUP_IMPLICIT, complain);
3760 /* for a non-type template-parameter of integral or
3761 enumeration type, integral promotions (4.5) and integral
3762 conversions (4.7) are applied. */
3763 /* It should be sufficient to check the outermost conversion step, since
3764 there are no qualification conversions to integer type. */
3765 if (conv)
3766 switch (conv->kind)
3768 /* A conversion function is OK. If it isn't constexpr, we'll
3769 complain later that the argument isn't constant. */
3770 case ck_user:
3771 /* The lvalue-to-rvalue conversion is OK. */
3772 case ck_rvalue:
3773 case ck_identity:
3774 break;
3776 case ck_std:
3777 t = next_conversion (conv)->type;
3778 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t))
3779 break;
3781 if (complain & tf_error)
3782 error_at (loc, "conversion from %qT to %qT not considered for "
3783 "non-type template argument", t, type);
3784 /* and fall through. */
3786 default:
3787 conv = NULL;
3788 break;
3791 if (conv)
3792 expr = convert_like (conv, expr, complain);
3793 else
3794 expr = error_mark_node;
3796 /* Free all the conversions we allocated. */
3797 obstack_free (&conversion_obstack, p);
3799 return expr;
3802 /* Do any initial processing on the arguments to a function call. */
3804 static vec<tree, va_gc> *
3805 resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
3807 unsigned int ix;
3808 tree arg;
3810 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
3812 if (error_operand_p (arg))
3813 return NULL;
3814 else if (VOID_TYPE_P (TREE_TYPE (arg)))
3816 if (complain & tf_error)
3817 error ("invalid use of void expression");
3818 return NULL;
3820 else if (invalid_nonstatic_memfn_p (arg, complain))
3821 return NULL;
3823 return args;
3826 /* Perform overload resolution on FN, which is called with the ARGS.
3828 Return the candidate function selected by overload resolution, or
3829 NULL if the event that overload resolution failed. In the case
3830 that overload resolution fails, *CANDIDATES will be the set of
3831 candidates considered, and ANY_VIABLE_P will be set to true or
3832 false to indicate whether or not any of the candidates were
3833 viable.
3835 The ARGS should already have gone through RESOLVE_ARGS before this
3836 function is called. */
3838 static struct z_candidate *
3839 perform_overload_resolution (tree fn,
3840 const vec<tree, va_gc> *args,
3841 struct z_candidate **candidates,
3842 bool *any_viable_p, tsubst_flags_t complain)
3844 struct z_candidate *cand;
3845 tree explicit_targs;
3846 int template_only;
3848 bool subtime = timevar_cond_start (TV_OVERLOAD);
3850 explicit_targs = NULL_TREE;
3851 template_only = 0;
3853 *candidates = NULL;
3854 *any_viable_p = true;
3856 /* Check FN. */
3857 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
3858 || TREE_CODE (fn) == TEMPLATE_DECL
3859 || TREE_CODE (fn) == OVERLOAD
3860 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
3862 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3864 explicit_targs = TREE_OPERAND (fn, 1);
3865 fn = TREE_OPERAND (fn, 0);
3866 template_only = 1;
3869 /* Add the various candidate functions. */
3870 add_candidates (fn, NULL_TREE, args, NULL_TREE,
3871 explicit_targs, template_only,
3872 /*conversion_path=*/NULL_TREE,
3873 /*access_path=*/NULL_TREE,
3874 LOOKUP_NORMAL,
3875 candidates, complain);
3877 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
3878 if (*any_viable_p)
3879 cand = tourney (*candidates, complain);
3880 else
3881 cand = NULL;
3883 timevar_cond_stop (TV_OVERLOAD, subtime);
3884 return cand;
3887 /* Print an error message about being unable to build a call to FN with
3888 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
3889 be located; CANDIDATES is a possibly empty list of such
3890 functions. */
3892 static void
3893 print_error_for_call_failure (tree fn, vec<tree, va_gc> *args, bool any_viable_p,
3894 struct z_candidate *candidates)
3896 tree name = DECL_NAME (OVL_CURRENT (fn));
3897 location_t loc = location_of (name);
3899 if (!any_viable_p)
3900 error_at (loc, "no matching function for call to %<%D(%A)%>",
3901 name, build_tree_list_vec (args));
3902 else
3903 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
3904 name, build_tree_list_vec (args));
3905 if (candidates)
3906 print_z_candidates (loc, candidates);
3909 /* Return an expression for a call to FN (a namespace-scope function,
3910 or a static member function) with the ARGS. This may change
3911 ARGS. */
3913 tree
3914 build_new_function_call (tree fn, vec<tree, va_gc> **args, bool koenig_p,
3915 tsubst_flags_t complain)
3917 struct z_candidate *candidates, *cand;
3918 bool any_viable_p;
3919 void *p;
3920 tree result;
3922 if (args != NULL && *args != NULL)
3924 *args = resolve_args (*args, complain);
3925 if (*args == NULL)
3926 return error_mark_node;
3929 if (flag_tm)
3930 tm_malloc_replacement (fn);
3932 /* If this function was found without using argument dependent
3933 lookup, then we want to ignore any undeclared friend
3934 functions. */
3935 if (!koenig_p)
3937 tree orig_fn = fn;
3939 fn = remove_hidden_names (fn);
3940 if (!fn)
3942 if (complain & tf_error)
3943 print_error_for_call_failure (orig_fn, *args, false, NULL);
3944 return error_mark_node;
3948 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3949 p = conversion_obstack_alloc (0);
3951 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
3952 complain);
3954 if (!cand)
3956 if (complain & tf_error)
3958 if (!any_viable_p && candidates && ! candidates->next
3959 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
3960 return cp_build_function_call_vec (candidates->fn, args, complain);
3961 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3962 fn = TREE_OPERAND (fn, 0);
3963 print_error_for_call_failure (fn, *args, any_viable_p, candidates);
3965 result = error_mark_node;
3967 else
3969 int flags = LOOKUP_NORMAL;
3970 /* If fn is template_id_expr, the call has explicit template arguments
3971 (e.g. func<int>(5)), communicate this info to build_over_call
3972 through flags so that later we can use it to decide whether to warn
3973 about peculiar null pointer conversion. */
3974 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3975 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
3976 result = build_over_call (cand, flags, complain);
3979 /* Free all the conversions we allocated. */
3980 obstack_free (&conversion_obstack, p);
3982 return result;
3985 /* Build a call to a global operator new. FNNAME is the name of the
3986 operator (either "operator new" or "operator new[]") and ARGS are
3987 the arguments provided. This may change ARGS. *SIZE points to the
3988 total number of bytes required by the allocation, and is updated if
3989 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
3990 be used. If this function determines that no cookie should be
3991 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
3992 is not NULL_TREE, it is evaluated before calculating the final
3993 array size, and if it fails, the array size is replaced with
3994 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
3995 is non-NULL, it will be set, upon return, to the allocation
3996 function called. */
3998 tree
3999 build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
4000 tree *size, tree *cookie_size, tree size_check,
4001 tree *fn, tsubst_flags_t complain)
4003 tree original_size = *size;
4004 tree fns;
4005 struct z_candidate *candidates;
4006 struct z_candidate *cand;
4007 bool any_viable_p;
4009 if (fn)
4010 *fn = NULL_TREE;
4011 /* Set to (size_t)-1 if the size check fails. */
4012 if (size_check != NULL_TREE)
4014 tree errval = TYPE_MAX_VALUE (sizetype);
4015 if (cxx_dialect >= cxx11)
4016 errval = throw_bad_array_new_length ();
4017 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4018 original_size, errval);
4020 vec_safe_insert (*args, 0, *size);
4021 *args = resolve_args (*args, complain);
4022 if (*args == NULL)
4023 return error_mark_node;
4025 /* Based on:
4027 [expr.new]
4029 If this lookup fails to find the name, or if the allocated type
4030 is not a class type, the allocation function's name is looked
4031 up in the global scope.
4033 we disregard block-scope declarations of "operator new". */
4034 fns = lookup_function_nonclass (fnname, *args, /*block_p=*/false);
4036 /* Figure out what function is being called. */
4037 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
4038 complain);
4040 /* If no suitable function could be found, issue an error message
4041 and give up. */
4042 if (!cand)
4044 if (complain & tf_error)
4045 print_error_for_call_failure (fns, *args, any_viable_p, candidates);
4046 return error_mark_node;
4049 /* If a cookie is required, add some extra space. Whether
4050 or not a cookie is required cannot be determined until
4051 after we know which function was called. */
4052 if (*cookie_size)
4054 bool use_cookie = true;
4055 if (!abi_version_at_least (2))
4057 /* In G++ 3.2, the check was implemented incorrectly; it
4058 looked at the placement expression, rather than the
4059 type of the function. */
4060 if ((*args)->length () == 2
4061 && same_type_p (TREE_TYPE ((**args)[1]), ptr_type_node))
4062 use_cookie = false;
4064 else
4066 tree arg_types;
4068 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
4069 /* Skip the size_t parameter. */
4070 arg_types = TREE_CHAIN (arg_types);
4071 /* Check the remaining parameters (if any). */
4072 if (arg_types
4073 && TREE_CHAIN (arg_types) == void_list_node
4074 && same_type_p (TREE_VALUE (arg_types),
4075 ptr_type_node))
4076 use_cookie = false;
4078 /* If we need a cookie, adjust the number of bytes allocated. */
4079 if (use_cookie)
4081 /* Update the total size. */
4082 *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
4083 /* Set to (size_t)-1 if the size check fails. */
4084 gcc_assert (size_check != NULL_TREE);
4085 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4086 *size, TYPE_MAX_VALUE (sizetype));
4087 /* Update the argument list to reflect the adjusted size. */
4088 (**args)[0] = *size;
4090 else
4091 *cookie_size = NULL_TREE;
4094 /* Tell our caller which function we decided to call. */
4095 if (fn)
4096 *fn = cand->fn;
4098 /* Build the CALL_EXPR. */
4099 return build_over_call (cand, LOOKUP_NORMAL, complain);
4102 /* Build a new call to operator(). This may change ARGS. */
4104 static tree
4105 build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4107 struct z_candidate *candidates = 0, *cand;
4108 tree fns, convs, first_mem_arg = NULL_TREE;
4109 tree type = TREE_TYPE (obj);
4110 bool any_viable_p;
4111 tree result = NULL_TREE;
4112 void *p;
4114 if (error_operand_p (obj))
4115 return error_mark_node;
4117 obj = prep_operand (obj);
4119 if (TYPE_PTRMEMFUNC_P (type))
4121 if (complain & tf_error)
4122 /* It's no good looking for an overloaded operator() on a
4123 pointer-to-member-function. */
4124 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
4125 return error_mark_node;
4128 if (TYPE_BINFO (type))
4130 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
4131 if (fns == error_mark_node)
4132 return error_mark_node;
4134 else
4135 fns = NULL_TREE;
4137 if (args != NULL && *args != NULL)
4139 *args = resolve_args (*args, complain);
4140 if (*args == NULL)
4141 return error_mark_node;
4144 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4145 p = conversion_obstack_alloc (0);
4147 if (fns)
4149 first_mem_arg = obj;
4151 add_candidates (BASELINK_FUNCTIONS (fns),
4152 first_mem_arg, *args, NULL_TREE,
4153 NULL_TREE, false,
4154 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
4155 LOOKUP_NORMAL, &candidates, complain);
4158 convs = lookup_conversions (type);
4160 for (; convs; convs = TREE_CHAIN (convs))
4162 tree fns = TREE_VALUE (convs);
4163 tree totype = TREE_TYPE (convs);
4165 if (TYPE_PTRFN_P (totype)
4166 || TYPE_REFFN_P (totype)
4167 || (TREE_CODE (totype) == REFERENCE_TYPE
4168 && TYPE_PTRFN_P (TREE_TYPE (totype))))
4169 for (; fns; fns = OVL_NEXT (fns))
4171 tree fn = OVL_CURRENT (fns);
4173 if (DECL_NONCONVERTING_P (fn))
4174 continue;
4176 if (TREE_CODE (fn) == TEMPLATE_DECL)
4177 add_template_conv_candidate
4178 (&candidates, fn, obj, NULL_TREE, *args, totype,
4179 /*access_path=*/NULL_TREE,
4180 /*conversion_path=*/NULL_TREE, complain);
4181 else
4182 add_conv_candidate (&candidates, fn, obj, NULL_TREE,
4183 *args, /*conversion_path=*/NULL_TREE,
4184 /*access_path=*/NULL_TREE, complain);
4188 candidates = splice_viable (candidates, pedantic, &any_viable_p);
4189 if (!any_viable_p)
4191 if (complain & tf_error)
4193 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
4194 build_tree_list_vec (*args));
4195 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4197 result = error_mark_node;
4199 else
4201 cand = tourney (candidates, complain);
4202 if (cand == 0)
4204 if (complain & tf_error)
4206 error ("call of %<(%T) (%A)%> is ambiguous",
4207 TREE_TYPE (obj), build_tree_list_vec (*args));
4208 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4210 result = error_mark_node;
4212 /* Since cand->fn will be a type, not a function, for a conversion
4213 function, we must be careful not to unconditionally look at
4214 DECL_NAME here. */
4215 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
4216 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
4217 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4218 else
4220 obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1,
4221 complain);
4222 obj = convert_from_reference (obj);
4223 result = cp_build_function_call_vec (obj, args, complain);
4227 /* Free all the conversions we allocated. */
4228 obstack_free (&conversion_obstack, p);
4230 return result;
4233 /* Wrapper for above. */
4235 tree
4236 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4238 tree ret;
4239 bool subtime = timevar_cond_start (TV_OVERLOAD);
4240 ret = build_op_call_1 (obj, args, complain);
4241 timevar_cond_stop (TV_OVERLOAD, subtime);
4242 return ret;
4245 /* Called by op_error to prepare format strings suitable for the error
4246 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4247 and a suffix (controlled by NTYPES). */
4249 static const char *
4250 op_error_string (const char *errmsg, int ntypes, bool match)
4252 const char *msg;
4254 const char *msgp = concat (match ? G_("ambiguous overload for ")
4255 : G_("no match for "), errmsg, NULL);
4257 if (ntypes == 3)
4258 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
4259 else if (ntypes == 2)
4260 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
4261 else
4262 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
4264 return msg;
4267 static void
4268 op_error (location_t loc, enum tree_code code, enum tree_code code2,
4269 tree arg1, tree arg2, tree arg3, bool match)
4271 const char *opname;
4273 if (code == MODIFY_EXPR)
4274 opname = assignment_operator_name_info[code2].name;
4275 else
4276 opname = operator_name_info[code].name;
4278 switch (code)
4280 case COND_EXPR:
4281 if (flag_diagnostics_show_caret)
4282 error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
4283 3, match),
4284 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4285 else
4286 error_at (loc, op_error_string (G_("ternary %<operator?:%> "
4287 "in %<%E ? %E : %E%>"), 3, match),
4288 arg1, arg2, arg3,
4289 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4290 break;
4292 case POSTINCREMENT_EXPR:
4293 case POSTDECREMENT_EXPR:
4294 if (flag_diagnostics_show_caret)
4295 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4296 opname, TREE_TYPE (arg1));
4297 else
4298 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4299 1, match),
4300 opname, arg1, opname, TREE_TYPE (arg1));
4301 break;
4303 case ARRAY_REF:
4304 if (flag_diagnostics_show_caret)
4305 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
4306 TREE_TYPE (arg1), TREE_TYPE (arg2));
4307 else
4308 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
4309 2, match),
4310 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
4311 break;
4313 case REALPART_EXPR:
4314 case IMAGPART_EXPR:
4315 if (flag_diagnostics_show_caret)
4316 error_at (loc, op_error_string (G_("%qs"), 1, match),
4317 opname, TREE_TYPE (arg1));
4318 else
4319 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
4320 opname, opname, arg1, TREE_TYPE (arg1));
4321 break;
4323 default:
4324 if (arg2)
4325 if (flag_diagnostics_show_caret)
4326 error_at (loc, op_error_string (G_("%<operator%s%>"), 2, match),
4327 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
4328 else
4329 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
4330 2, match),
4331 opname, arg1, opname, arg2,
4332 TREE_TYPE (arg1), TREE_TYPE (arg2));
4333 else
4334 if (flag_diagnostics_show_caret)
4335 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4336 opname, TREE_TYPE (arg1));
4337 else
4338 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
4339 1, match),
4340 opname, opname, arg1, TREE_TYPE (arg1));
4341 break;
4345 /* Return the implicit conversion sequence that could be used to
4346 convert E1 to E2 in [expr.cond]. */
4348 static conversion *
4349 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
4351 tree t1 = non_reference (TREE_TYPE (e1));
4352 tree t2 = non_reference (TREE_TYPE (e2));
4353 conversion *conv;
4354 bool good_base;
4356 /* [expr.cond]
4358 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
4359 implicitly converted (clause _conv_) to the type "lvalue reference to
4360 T2", subject to the constraint that in the conversion the
4361 reference must bind directly (_dcl.init.ref_) to an lvalue. */
4362 if (real_lvalue_p (e2))
4364 conv = implicit_conversion (build_reference_type (t2),
4367 /*c_cast_p=*/false,
4368 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
4369 |LOOKUP_ONLYCONVERTING,
4370 complain);
4371 if (conv)
4372 return conv;
4375 /* [expr.cond]
4377 If E1 and E2 have class type, and the underlying class types are
4378 the same or one is a base class of the other: E1 can be converted
4379 to match E2 if the class of T2 is the same type as, or a base
4380 class of, the class of T1, and the cv-qualification of T2 is the
4381 same cv-qualification as, or a greater cv-qualification than, the
4382 cv-qualification of T1. If the conversion is applied, E1 is
4383 changed to an rvalue of type T2 that still refers to the original
4384 source class object (or the appropriate subobject thereof). */
4385 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
4386 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
4388 if (good_base && at_least_as_qualified_p (t2, t1))
4390 conv = build_identity_conv (t1, e1);
4391 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
4392 TYPE_MAIN_VARIANT (t2)))
4393 conv = build_conv (ck_base, t2, conv);
4394 else
4395 conv = build_conv (ck_rvalue, t2, conv);
4396 return conv;
4398 else
4399 return NULL;
4401 else
4402 /* [expr.cond]
4404 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
4405 converted to the type that expression E2 would have if E2 were
4406 converted to an rvalue (or the type it has, if E2 is an rvalue). */
4407 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
4408 LOOKUP_IMPLICIT, complain);
4411 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
4412 arguments to the conditional expression. */
4414 static tree
4415 build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
4416 tsubst_flags_t complain)
4418 tree arg2_type;
4419 tree arg3_type;
4420 tree result = NULL_TREE;
4421 tree result_type = NULL_TREE;
4422 bool lvalue_p = true;
4423 struct z_candidate *candidates = 0;
4424 struct z_candidate *cand;
4425 void *p;
4426 tree orig_arg2, orig_arg3;
4428 /* As a G++ extension, the second argument to the conditional can be
4429 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
4430 c'.) If the second operand is omitted, make sure it is
4431 calculated only once. */
4432 if (!arg2)
4434 if (complain & tf_error)
4435 pedwarn (loc, OPT_Wpedantic,
4436 "ISO C++ forbids omitting the middle term of a ?: expression");
4438 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
4439 if (real_lvalue_p (arg1))
4440 arg2 = arg1 = stabilize_reference (arg1);
4441 else
4442 arg2 = arg1 = save_expr (arg1);
4445 /* If something has already gone wrong, just pass that fact up the
4446 tree. */
4447 if (error_operand_p (arg1)
4448 || error_operand_p (arg2)
4449 || error_operand_p (arg3))
4450 return error_mark_node;
4452 orig_arg2 = arg2;
4453 orig_arg3 = arg3;
4455 if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
4457 arg1 = force_rvalue (arg1, complain);
4458 arg2 = force_rvalue (arg2, complain);
4459 arg3 = force_rvalue (arg3, complain);
4461 tree arg1_type = TREE_TYPE (arg1);
4462 arg2_type = TREE_TYPE (arg2);
4463 arg3_type = TREE_TYPE (arg3);
4465 if (TREE_CODE (arg2_type) != VECTOR_TYPE
4466 && TREE_CODE (arg3_type) != VECTOR_TYPE)
4468 /* Rely on the error messages of the scalar version. */
4469 tree scal = build_conditional_expr_1 (loc, integer_one_node,
4470 orig_arg2, orig_arg3, complain);
4471 if (scal == error_mark_node)
4472 return error_mark_node;
4473 tree stype = TREE_TYPE (scal);
4474 tree ctype = TREE_TYPE (arg1_type);
4475 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
4476 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
4478 if (complain & tf_error)
4479 error_at (loc, "inferred scalar type %qT is not an integer or "
4480 "floating point type of the same size as %qT", stype,
4481 COMPARISON_CLASS_P (arg1)
4482 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4483 : ctype);
4484 return error_mark_node;
4487 tree vtype = build_opaque_vector_type (stype,
4488 TYPE_VECTOR_SUBPARTS (arg1_type));
4489 /* We could pass complain & tf_warning to unsafe_conversion_p,
4490 but the warnings (like Wsign-conversion) have already been
4491 given by the scalar build_conditional_expr_1. We still check
4492 unsafe_conversion_p to forbid truncating long long -> float. */
4493 if (unsafe_conversion_p (stype, arg2, false))
4495 if (complain & tf_error)
4496 error_at (loc, "conversion of scalar %qT to vector %qT "
4497 "involves truncation", arg2_type, vtype);
4498 return error_mark_node;
4500 if (unsafe_conversion_p (stype, arg3, false))
4502 if (complain & tf_error)
4503 error_at (loc, "conversion of scalar %qT to vector %qT "
4504 "involves truncation", arg3_type, vtype);
4505 return error_mark_node;
4508 arg2 = cp_convert (stype, arg2, complain);
4509 arg2 = save_expr (arg2);
4510 arg2 = build_vector_from_val (vtype, arg2);
4511 arg2_type = vtype;
4512 arg3 = cp_convert (stype, arg3, complain);
4513 arg3 = save_expr (arg3);
4514 arg3 = build_vector_from_val (vtype, arg3);
4515 arg3_type = vtype;
4518 if ((TREE_CODE (arg2_type) == VECTOR_TYPE)
4519 != (TREE_CODE (arg3_type) == VECTOR_TYPE))
4521 enum stv_conv convert_flag =
4522 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
4523 complain & tf_error);
4525 switch (convert_flag)
4527 case stv_error:
4528 return error_mark_node;
4529 case stv_firstarg:
4531 arg2 = save_expr (arg2);
4532 arg2 = convert (TREE_TYPE (arg3_type), arg2);
4533 arg2 = build_vector_from_val (arg3_type, arg2);
4534 arg2_type = TREE_TYPE (arg2);
4535 break;
4537 case stv_secondarg:
4539 arg3 = save_expr (arg3);
4540 arg3 = convert (TREE_TYPE (arg2_type), arg3);
4541 arg3 = build_vector_from_val (arg2_type, arg3);
4542 arg3_type = TREE_TYPE (arg3);
4543 break;
4545 default:
4546 break;
4550 if (!same_type_p (arg2_type, arg3_type)
4551 || TYPE_VECTOR_SUBPARTS (arg1_type)
4552 != TYPE_VECTOR_SUBPARTS (arg2_type)
4553 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
4555 if (complain & tf_error)
4556 error_at (loc,
4557 "incompatible vector types in conditional expression: "
4558 "%qT, %qT and %qT", TREE_TYPE (arg1),
4559 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
4560 return error_mark_node;
4563 if (!COMPARISON_CLASS_P (arg1))
4564 arg1 = cp_build_binary_op (loc, NE_EXPR, arg1,
4565 build_zero_cst (arg1_type), complain);
4566 return fold_build3 (VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
4569 /* [expr.cond]
4571 The first expression is implicitly converted to bool (clause
4572 _conv_). */
4573 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
4574 LOOKUP_NORMAL);
4575 if (error_operand_p (arg1))
4576 return error_mark_node;
4578 /* [expr.cond]
4580 If either the second or the third operand has type (possibly
4581 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
4582 array-to-pointer (_conv.array_), and function-to-pointer
4583 (_conv.func_) standard conversions are performed on the second
4584 and third operands. */
4585 arg2_type = unlowered_expr_type (arg2);
4586 arg3_type = unlowered_expr_type (arg3);
4587 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
4589 /* Do the conversions. We don't these for `void' type arguments
4590 since it can't have any effect and since decay_conversion
4591 does not handle that case gracefully. */
4592 if (!VOID_TYPE_P (arg2_type))
4593 arg2 = decay_conversion (arg2, complain);
4594 if (!VOID_TYPE_P (arg3_type))
4595 arg3 = decay_conversion (arg3, complain);
4596 arg2_type = TREE_TYPE (arg2);
4597 arg3_type = TREE_TYPE (arg3);
4599 /* [expr.cond]
4601 One of the following shall hold:
4603 --The second or the third operand (but not both) is a
4604 throw-expression (_except.throw_); the result is of the
4605 type of the other and is an rvalue.
4607 --Both the second and the third operands have type void; the
4608 result is of type void and is an rvalue.
4610 We must avoid calling force_rvalue for expressions of type
4611 "void" because it will complain that their value is being
4612 used. */
4613 if (TREE_CODE (arg2) == THROW_EXPR
4614 && TREE_CODE (arg3) != THROW_EXPR)
4616 if (!VOID_TYPE_P (arg3_type))
4618 arg3 = force_rvalue (arg3, complain);
4619 if (arg3 == error_mark_node)
4620 return error_mark_node;
4622 arg3_type = TREE_TYPE (arg3);
4623 result_type = arg3_type;
4625 else if (TREE_CODE (arg2) != THROW_EXPR
4626 && TREE_CODE (arg3) == THROW_EXPR)
4628 if (!VOID_TYPE_P (arg2_type))
4630 arg2 = force_rvalue (arg2, complain);
4631 if (arg2 == error_mark_node)
4632 return error_mark_node;
4634 arg2_type = TREE_TYPE (arg2);
4635 result_type = arg2_type;
4637 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
4638 result_type = void_type_node;
4639 else
4641 if (complain & tf_error)
4643 if (VOID_TYPE_P (arg2_type))
4644 error_at (EXPR_LOC_OR_LOC (arg3, loc),
4645 "second operand to the conditional operator "
4646 "is of type %<void%>, but the third operand is "
4647 "neither a throw-expression nor of type %<void%>");
4648 else
4649 error_at (EXPR_LOC_OR_LOC (arg2, loc),
4650 "third operand to the conditional operator "
4651 "is of type %<void%>, but the second operand is "
4652 "neither a throw-expression nor of type %<void%>");
4654 return error_mark_node;
4657 lvalue_p = false;
4658 goto valid_operands;
4660 /* [expr.cond]
4662 Otherwise, if the second and third operand have different types,
4663 and either has (possibly cv-qualified) class type, an attempt is
4664 made to convert each of those operands to the type of the other. */
4665 else if (!same_type_p (arg2_type, arg3_type)
4666 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
4668 conversion *conv2;
4669 conversion *conv3;
4671 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4672 p = conversion_obstack_alloc (0);
4674 conv2 = conditional_conversion (arg2, arg3, complain);
4675 conv3 = conditional_conversion (arg3, arg2, complain);
4677 /* [expr.cond]
4679 If both can be converted, or one can be converted but the
4680 conversion is ambiguous, the program is ill-formed. If
4681 neither can be converted, the operands are left unchanged and
4682 further checking is performed as described below. If exactly
4683 one conversion is possible, that conversion is applied to the
4684 chosen operand and the converted operand is used in place of
4685 the original operand for the remainder of this section. */
4686 if ((conv2 && !conv2->bad_p
4687 && conv3 && !conv3->bad_p)
4688 || (conv2 && conv2->kind == ck_ambig)
4689 || (conv3 && conv3->kind == ck_ambig))
4691 if (complain & tf_error)
4692 error_at (loc, "operands to ?: have different types %qT and %qT",
4693 arg2_type, arg3_type);
4694 result = error_mark_node;
4696 else if (conv2 && (!conv2->bad_p || !conv3))
4698 arg2 = convert_like (conv2, arg2, complain);
4699 arg2 = convert_from_reference (arg2);
4700 arg2_type = TREE_TYPE (arg2);
4701 /* Even if CONV2 is a valid conversion, the result of the
4702 conversion may be invalid. For example, if ARG3 has type
4703 "volatile X", and X does not have a copy constructor
4704 accepting a "volatile X&", then even if ARG2 can be
4705 converted to X, the conversion will fail. */
4706 if (error_operand_p (arg2))
4707 result = error_mark_node;
4709 else if (conv3 && (!conv3->bad_p || !conv2))
4711 arg3 = convert_like (conv3, arg3, complain);
4712 arg3 = convert_from_reference (arg3);
4713 arg3_type = TREE_TYPE (arg3);
4714 if (error_operand_p (arg3))
4715 result = error_mark_node;
4718 /* Free all the conversions we allocated. */
4719 obstack_free (&conversion_obstack, p);
4721 if (result)
4722 return result;
4724 /* If, after the conversion, both operands have class type,
4725 treat the cv-qualification of both operands as if it were the
4726 union of the cv-qualification of the operands.
4728 The standard is not clear about what to do in this
4729 circumstance. For example, if the first operand has type
4730 "const X" and the second operand has a user-defined
4731 conversion to "volatile X", what is the type of the second
4732 operand after this step? Making it be "const X" (matching
4733 the first operand) seems wrong, as that discards the
4734 qualification without actually performing a copy. Leaving it
4735 as "volatile X" seems wrong as that will result in the
4736 conditional expression failing altogether, even though,
4737 according to this step, the one operand could be converted to
4738 the type of the other. */
4739 if ((conv2 || conv3)
4740 && CLASS_TYPE_P (arg2_type)
4741 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
4742 arg2_type = arg3_type =
4743 cp_build_qualified_type (arg2_type,
4744 cp_type_quals (arg2_type)
4745 | cp_type_quals (arg3_type));
4748 /* [expr.cond]
4750 If the second and third operands are glvalues of the same value
4751 category and have the same type, the result is of that type and
4752 value category. */
4753 if (((real_lvalue_p (arg2) && real_lvalue_p (arg3))
4754 || (xvalue_p (arg2) && xvalue_p (arg3)))
4755 && same_type_p (arg2_type, arg3_type))
4757 result_type = arg2_type;
4758 arg2 = mark_lvalue_use (arg2);
4759 arg3 = mark_lvalue_use (arg3);
4760 goto valid_operands;
4763 /* [expr.cond]
4765 Otherwise, the result is an rvalue. If the second and third
4766 operand do not have the same type, and either has (possibly
4767 cv-qualified) class type, overload resolution is used to
4768 determine the conversions (if any) to be applied to the operands
4769 (_over.match.oper_, _over.built_). */
4770 lvalue_p = false;
4771 if (!same_type_p (arg2_type, arg3_type)
4772 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
4774 tree args[3];
4775 conversion *conv;
4776 bool any_viable_p;
4778 /* Rearrange the arguments so that add_builtin_candidate only has
4779 to know about two args. In build_builtin_candidate, the
4780 arguments are unscrambled. */
4781 args[0] = arg2;
4782 args[1] = arg3;
4783 args[2] = arg1;
4784 add_builtin_candidates (&candidates,
4785 COND_EXPR,
4786 NOP_EXPR,
4787 ansi_opname (COND_EXPR),
4788 args,
4789 LOOKUP_NORMAL, complain);
4791 /* [expr.cond]
4793 If the overload resolution fails, the program is
4794 ill-formed. */
4795 candidates = splice_viable (candidates, pedantic, &any_viable_p);
4796 if (!any_viable_p)
4798 if (complain & tf_error)
4800 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
4801 print_z_candidates (loc, candidates);
4803 return error_mark_node;
4805 cand = tourney (candidates, complain);
4806 if (!cand)
4808 if (complain & tf_error)
4810 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
4811 print_z_candidates (loc, candidates);
4813 return error_mark_node;
4816 /* [expr.cond]
4818 Otherwise, the conversions thus determined are applied, and
4819 the converted operands are used in place of the original
4820 operands for the remainder of this section. */
4821 conv = cand->convs[0];
4822 arg1 = convert_like (conv, arg1, complain);
4823 conv = cand->convs[1];
4824 arg2 = convert_like (conv, arg2, complain);
4825 arg2_type = TREE_TYPE (arg2);
4826 conv = cand->convs[2];
4827 arg3 = convert_like (conv, arg3, complain);
4828 arg3_type = TREE_TYPE (arg3);
4831 /* [expr.cond]
4833 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
4834 and function-to-pointer (_conv.func_) standard conversions are
4835 performed on the second and third operands.
4837 We need to force the lvalue-to-rvalue conversion here for class types,
4838 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
4839 that isn't wrapped with a TARGET_EXPR plays havoc with exception
4840 regions. */
4842 arg2 = force_rvalue (arg2, complain);
4843 if (!CLASS_TYPE_P (arg2_type))
4844 arg2_type = TREE_TYPE (arg2);
4846 arg3 = force_rvalue (arg3, complain);
4847 if (!CLASS_TYPE_P (arg3_type))
4848 arg3_type = TREE_TYPE (arg3);
4850 if (arg2 == error_mark_node || arg3 == error_mark_node)
4851 return error_mark_node;
4853 /* [expr.cond]
4855 After those conversions, one of the following shall hold:
4857 --The second and third operands have the same type; the result is of
4858 that type. */
4859 if (same_type_p (arg2_type, arg3_type))
4860 result_type = arg2_type;
4861 /* [expr.cond]
4863 --The second and third operands have arithmetic or enumeration
4864 type; the usual arithmetic conversions are performed to bring
4865 them to a common type, and the result is of that type. */
4866 else if ((ARITHMETIC_TYPE_P (arg2_type)
4867 || UNSCOPED_ENUM_P (arg2_type))
4868 && (ARITHMETIC_TYPE_P (arg3_type)
4869 || UNSCOPED_ENUM_P (arg3_type)))
4871 /* In this case, there is always a common type. */
4872 result_type = type_after_usual_arithmetic_conversions (arg2_type,
4873 arg3_type);
4874 if (complain & tf_warning)
4875 do_warn_double_promotion (result_type, arg2_type, arg3_type,
4876 "implicit conversion from %qT to %qT to "
4877 "match other result of conditional",
4878 loc);
4880 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
4881 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
4883 if (TREE_CODE (orig_arg2) == CONST_DECL
4884 && TREE_CODE (orig_arg3) == CONST_DECL
4885 && DECL_CONTEXT (orig_arg2) == DECL_CONTEXT (orig_arg3))
4886 /* Two enumerators from the same enumeration can have different
4887 types when the enumeration is still being defined. */;
4888 else if (complain & tf_warning)
4889 warning_at (loc, OPT_Wenum_compare, "enumeral mismatch in "
4890 "conditional expression: %qT vs %qT",
4891 arg2_type, arg3_type);
4893 else if (extra_warnings
4894 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
4895 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
4896 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
4897 && !same_type_p (arg2_type,
4898 type_promotes_to (arg3_type)))))
4900 if (complain & tf_warning)
4901 warning_at (loc, 0, "enumeral and non-enumeral type in "
4902 "conditional expression");
4905 arg2 = perform_implicit_conversion (result_type, arg2, complain);
4906 arg3 = perform_implicit_conversion (result_type, arg3, complain);
4908 /* [expr.cond]
4910 --The second and third operands have pointer type, or one has
4911 pointer type and the other is a null pointer constant; pointer
4912 conversions (_conv.ptr_) and qualification conversions
4913 (_conv.qual_) are performed to bring them to their composite
4914 pointer type (_expr.rel_). The result is of the composite
4915 pointer type.
4917 --The second and third operands have pointer to member type, or
4918 one has pointer to member type and the other is a null pointer
4919 constant; pointer to member conversions (_conv.mem_) and
4920 qualification conversions (_conv.qual_) are performed to bring
4921 them to a common type, whose cv-qualification shall match the
4922 cv-qualification of either the second or the third operand.
4923 The result is of the common type. */
4924 else if ((null_ptr_cst_p (arg2)
4925 && TYPE_PTR_OR_PTRMEM_P (arg3_type))
4926 || (null_ptr_cst_p (arg3)
4927 && TYPE_PTR_OR_PTRMEM_P (arg2_type))
4928 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
4929 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
4930 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
4932 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
4933 arg3, CPO_CONDITIONAL_EXPR,
4934 complain);
4935 if (result_type == error_mark_node)
4936 return error_mark_node;
4937 arg2 = perform_implicit_conversion (result_type, arg2, complain);
4938 arg3 = perform_implicit_conversion (result_type, arg3, complain);
4941 if (!result_type)
4943 if (complain & tf_error)
4944 error_at (loc, "operands to ?: have different types %qT and %qT",
4945 arg2_type, arg3_type);
4946 return error_mark_node;
4949 if (arg2 == error_mark_node || arg3 == error_mark_node)
4950 return error_mark_node;
4952 valid_operands:
4953 result = build3 (COND_EXPR, result_type, arg1, arg2, arg3);
4954 if (!cp_unevaluated_operand)
4955 /* Avoid folding within decltype (c++/42013) and noexcept. */
4956 result = fold_if_not_in_template (result);
4958 /* We can't use result_type below, as fold might have returned a
4959 throw_expr. */
4961 if (!lvalue_p)
4963 /* Expand both sides into the same slot, hopefully the target of
4964 the ?: expression. We used to check for TARGET_EXPRs here,
4965 but now we sometimes wrap them in NOP_EXPRs so the test would
4966 fail. */
4967 if (CLASS_TYPE_P (TREE_TYPE (result)))
4968 result = get_target_expr_sfinae (result, complain);
4969 /* If this expression is an rvalue, but might be mistaken for an
4970 lvalue, we must add a NON_LVALUE_EXPR. */
4971 result = rvalue (result);
4973 else
4974 result = force_paren_expr (result);
4976 return result;
4979 /* Wrapper for above. */
4981 tree
4982 build_conditional_expr (location_t loc, tree arg1, tree arg2, tree arg3,
4983 tsubst_flags_t complain)
4985 tree ret;
4986 bool subtime = timevar_cond_start (TV_OVERLOAD);
4987 ret = build_conditional_expr_1 (loc, arg1, arg2, arg3, complain);
4988 timevar_cond_stop (TV_OVERLOAD, subtime);
4989 return ret;
4992 /* OPERAND is an operand to an expression. Perform necessary steps
4993 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
4994 returned. */
4996 static tree
4997 prep_operand (tree operand)
4999 if (operand)
5001 if (CLASS_TYPE_P (TREE_TYPE (operand))
5002 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
5003 /* Make sure the template type is instantiated now. */
5004 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
5007 return operand;
5010 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5011 OVERLOAD) to the CANDIDATES, returning an updated list of
5012 CANDIDATES. The ARGS are the arguments provided to the call;
5013 if FIRST_ARG is non-null it is the implicit object argument,
5014 otherwise the first element of ARGS is used if needed. The
5015 EXPLICIT_TARGS are explicit template arguments provided.
5016 TEMPLATE_ONLY is true if only template functions should be
5017 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5018 add_function_candidate. */
5020 static void
5021 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
5022 tree return_type,
5023 tree explicit_targs, bool template_only,
5024 tree conversion_path, tree access_path,
5025 int flags,
5026 struct z_candidate **candidates,
5027 tsubst_flags_t complain)
5029 tree ctype;
5030 const vec<tree, va_gc> *non_static_args;
5031 bool check_list_ctor;
5032 bool check_converting;
5033 unification_kind_t strict;
5034 tree fn;
5036 if (!fns)
5037 return;
5039 /* Precalculate special handling of constructors and conversion ops. */
5040 fn = OVL_CURRENT (fns);
5041 if (DECL_CONV_FN_P (fn))
5043 check_list_ctor = false;
5044 check_converting = !!(flags & LOOKUP_ONLYCONVERTING);
5045 if (flags & LOOKUP_NO_CONVERSION)
5046 /* We're doing return_type(x). */
5047 strict = DEDUCE_CONV;
5048 else
5049 /* We're doing x.operator return_type(). */
5050 strict = DEDUCE_EXACT;
5051 /* [over.match.funcs] For conversion functions, the function
5052 is considered to be a member of the class of the implicit
5053 object argument for the purpose of defining the type of
5054 the implicit object parameter. */
5055 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
5057 else
5059 if (DECL_CONSTRUCTOR_P (fn))
5061 check_list_ctor = !!(flags & LOOKUP_LIST_ONLY);
5062 /* For list-initialization we consider explicit constructors
5063 and complain if one is chosen. */
5064 check_converting
5065 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
5066 == LOOKUP_ONLYCONVERTING);
5068 else
5070 check_list_ctor = false;
5071 check_converting = false;
5073 strict = DEDUCE_CALL;
5074 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
5077 if (first_arg)
5078 non_static_args = args;
5079 else
5080 /* Delay creating the implicit this parameter until it is needed. */
5081 non_static_args = NULL;
5083 for (; fns; fns = OVL_NEXT (fns))
5085 tree fn_first_arg;
5086 const vec<tree, va_gc> *fn_args;
5088 fn = OVL_CURRENT (fns);
5090 if (check_converting && DECL_NONCONVERTING_P (fn))
5091 continue;
5092 if (check_list_ctor && !is_list_ctor (fn))
5093 continue;
5095 /* Figure out which set of arguments to use. */
5096 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
5098 /* If this function is a non-static member and we didn't get an
5099 implicit object argument, move it out of args. */
5100 if (first_arg == NULL_TREE)
5102 unsigned int ix;
5103 tree arg;
5104 vec<tree, va_gc> *tempvec;
5105 vec_alloc (tempvec, args->length () - 1);
5106 for (ix = 1; args->iterate (ix, &arg); ++ix)
5107 tempvec->quick_push (arg);
5108 non_static_args = tempvec;
5109 first_arg = (*args)[0];
5112 fn_first_arg = first_arg;
5113 fn_args = non_static_args;
5115 else
5117 /* Otherwise, just use the list of arguments provided. */
5118 fn_first_arg = NULL_TREE;
5119 fn_args = args;
5122 if (TREE_CODE (fn) == TEMPLATE_DECL)
5123 add_template_candidate (candidates,
5125 ctype,
5126 explicit_targs,
5127 fn_first_arg,
5128 fn_args,
5129 return_type,
5130 access_path,
5131 conversion_path,
5132 flags,
5133 strict,
5134 complain);
5135 else if (!template_only)
5136 add_function_candidate (candidates,
5138 ctype,
5139 fn_first_arg,
5140 fn_args,
5141 access_path,
5142 conversion_path,
5143 flags,
5144 complain);
5148 static tree
5149 build_new_op_1 (location_t loc, enum tree_code code, int flags, tree arg1,
5150 tree arg2, tree arg3, tree *overload, tsubst_flags_t complain)
5152 struct z_candidate *candidates = 0, *cand;
5153 vec<tree, va_gc> *arglist;
5154 tree fnname;
5155 tree args[3];
5156 tree result = NULL_TREE;
5157 bool result_valid_p = false;
5158 enum tree_code code2 = NOP_EXPR;
5159 enum tree_code code_orig_arg1 = ERROR_MARK;
5160 enum tree_code code_orig_arg2 = ERROR_MARK;
5161 conversion *conv;
5162 void *p;
5163 bool strict_p;
5164 bool any_viable_p;
5166 if (error_operand_p (arg1)
5167 || error_operand_p (arg2)
5168 || error_operand_p (arg3))
5169 return error_mark_node;
5171 if (code == MODIFY_EXPR)
5173 code2 = TREE_CODE (arg3);
5174 arg3 = NULL_TREE;
5175 fnname = ansi_assopname (code2);
5177 else
5178 fnname = ansi_opname (code);
5180 arg1 = prep_operand (arg1);
5182 switch (code)
5184 case NEW_EXPR:
5185 case VEC_NEW_EXPR:
5186 case VEC_DELETE_EXPR:
5187 case DELETE_EXPR:
5188 /* Use build_op_new_call and build_op_delete_call instead. */
5189 gcc_unreachable ();
5191 case CALL_EXPR:
5192 /* Use build_op_call instead. */
5193 gcc_unreachable ();
5195 case TRUTH_ORIF_EXPR:
5196 case TRUTH_ANDIF_EXPR:
5197 case TRUTH_AND_EXPR:
5198 case TRUTH_OR_EXPR:
5199 /* These are saved for the sake of warn_logical_operator. */
5200 code_orig_arg1 = TREE_CODE (arg1);
5201 code_orig_arg2 = TREE_CODE (arg2);
5203 default:
5204 break;
5207 arg2 = prep_operand (arg2);
5208 arg3 = prep_operand (arg3);
5210 if (code == COND_EXPR)
5211 /* Use build_conditional_expr instead. */
5212 gcc_unreachable ();
5213 else if (! OVERLOAD_TYPE_P (TREE_TYPE (arg1))
5214 && (! arg2 || ! OVERLOAD_TYPE_P (TREE_TYPE (arg2))))
5215 goto builtin;
5217 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5218 arg2 = integer_zero_node;
5220 vec_alloc (arglist, 3);
5221 arglist->quick_push (arg1);
5222 if (arg2 != NULL_TREE)
5223 arglist->quick_push (arg2);
5224 if (arg3 != NULL_TREE)
5225 arglist->quick_push (arg3);
5227 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5228 p = conversion_obstack_alloc (0);
5230 /* Add namespace-scope operators to the list of functions to
5231 consider. */
5232 add_candidates (lookup_function_nonclass (fnname, arglist, /*block_p=*/true),
5233 NULL_TREE, arglist, NULL_TREE,
5234 NULL_TREE, false, NULL_TREE, NULL_TREE,
5235 flags, &candidates, complain);
5237 args[0] = arg1;
5238 args[1] = arg2;
5239 args[2] = NULL_TREE;
5241 /* Add class-member operators to the candidate set. */
5242 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
5244 tree fns;
5246 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
5247 if (fns == error_mark_node)
5249 result = error_mark_node;
5250 goto user_defined_result_ready;
5252 if (fns)
5253 add_candidates (BASELINK_FUNCTIONS (fns),
5254 NULL_TREE, arglist, NULL_TREE,
5255 NULL_TREE, false,
5256 BASELINK_BINFO (fns),
5257 BASELINK_ACCESS_BINFO (fns),
5258 flags, &candidates, complain);
5260 /* Per 13.3.1.2/3, 2nd bullet, if no operand has a class type, then
5261 only non-member functions that have type T1 or reference to
5262 cv-qualified-opt T1 for the first argument, if the first argument
5263 has an enumeration type, or T2 or reference to cv-qualified-opt
5264 T2 for the second argument, if the the second argument has an
5265 enumeration type. Filter out those that don't match. */
5266 else if (! arg2 || ! CLASS_TYPE_P (TREE_TYPE (arg2)))
5268 struct z_candidate **candp, **next;
5270 for (candp = &candidates; *candp; candp = next)
5272 tree parmlist, parmtype;
5273 int i, nargs = (arg2 ? 2 : 1);
5275 cand = *candp;
5276 next = &cand->next;
5278 parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5280 for (i = 0; i < nargs; ++i)
5282 parmtype = TREE_VALUE (parmlist);
5284 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
5285 parmtype = TREE_TYPE (parmtype);
5286 if (TREE_CODE (TREE_TYPE (args[i])) == ENUMERAL_TYPE
5287 && (same_type_ignoring_top_level_qualifiers_p
5288 (TREE_TYPE (args[i]), parmtype)))
5289 break;
5291 parmlist = TREE_CHAIN (parmlist);
5294 /* No argument has an appropriate type, so remove this
5295 candidate function from the list. */
5296 if (i == nargs)
5298 *candp = cand->next;
5299 next = candp;
5304 add_builtin_candidates (&candidates, code, code2, fnname, args,
5305 flags, complain);
5307 switch (code)
5309 case COMPOUND_EXPR:
5310 case ADDR_EXPR:
5311 /* For these, the built-in candidates set is empty
5312 [over.match.oper]/3. We don't want non-strict matches
5313 because exact matches are always possible with built-in
5314 operators. The built-in candidate set for COMPONENT_REF
5315 would be empty too, but since there are no such built-in
5316 operators, we accept non-strict matches for them. */
5317 strict_p = true;
5318 break;
5320 default:
5321 strict_p = pedantic;
5322 break;
5325 candidates = splice_viable (candidates, strict_p, &any_viable_p);
5326 if (!any_viable_p)
5328 switch (code)
5330 case POSTINCREMENT_EXPR:
5331 case POSTDECREMENT_EXPR:
5332 /* Don't try anything fancy if we're not allowed to produce
5333 errors. */
5334 if (!(complain & tf_error))
5335 return error_mark_node;
5337 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
5338 distinguish between prefix and postfix ++ and
5339 operator++() was used for both, so we allow this with
5340 -fpermissive. */
5341 else
5343 const char *msg = (flag_permissive)
5344 ? G_("no %<%D(int)%> declared for postfix %qs,"
5345 " trying prefix operator instead")
5346 : G_("no %<%D(int)%> declared for postfix %qs");
5347 permerror (loc, msg, fnname, operator_name_info[code].name);
5350 if (!flag_permissive)
5351 return error_mark_node;
5353 if (code == POSTINCREMENT_EXPR)
5354 code = PREINCREMENT_EXPR;
5355 else
5356 code = PREDECREMENT_EXPR;
5357 result = build_new_op_1 (loc, code, flags, arg1, NULL_TREE,
5358 NULL_TREE, overload, complain);
5359 break;
5361 /* The caller will deal with these. */
5362 case ADDR_EXPR:
5363 case COMPOUND_EXPR:
5364 case COMPONENT_REF:
5365 result = NULL_TREE;
5366 result_valid_p = true;
5367 break;
5369 default:
5370 if (complain & tf_error)
5372 /* If one of the arguments of the operator represents
5373 an invalid use of member function pointer, try to report
5374 a meaningful error ... */
5375 if (invalid_nonstatic_memfn_p (arg1, tf_error)
5376 || invalid_nonstatic_memfn_p (arg2, tf_error)
5377 || invalid_nonstatic_memfn_p (arg3, tf_error))
5378 /* We displayed the error message. */;
5379 else
5381 /* ... Otherwise, report the more generic
5382 "no matching operator found" error */
5383 op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
5384 print_z_candidates (loc, candidates);
5387 result = error_mark_node;
5388 break;
5391 else
5393 cand = tourney (candidates, complain);
5394 if (cand == 0)
5396 if (complain & tf_error)
5398 op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
5399 print_z_candidates (loc, candidates);
5401 result = error_mark_node;
5403 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5405 if (overload)
5406 *overload = cand->fn;
5408 if (resolve_args (arglist, complain) == NULL)
5409 result = error_mark_node;
5410 else
5411 result = build_over_call (cand, LOOKUP_NORMAL, complain);
5413 else
5415 /* Give any warnings we noticed during overload resolution. */
5416 if (cand->warnings && (complain & tf_warning))
5418 struct candidate_warning *w;
5419 for (w = cand->warnings; w; w = w->next)
5420 joust (cand, w->loser, 1, complain);
5423 /* Check for comparison of different enum types. */
5424 switch (code)
5426 case GT_EXPR:
5427 case LT_EXPR:
5428 case GE_EXPR:
5429 case LE_EXPR:
5430 case EQ_EXPR:
5431 case NE_EXPR:
5432 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
5433 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
5434 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5435 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
5436 && (complain & tf_warning))
5438 warning (OPT_Wenum_compare,
5439 "comparison between %q#T and %q#T",
5440 TREE_TYPE (arg1), TREE_TYPE (arg2));
5442 break;
5443 default:
5444 break;
5447 /* We need to strip any leading REF_BIND so that bitfields
5448 don't cause errors. This should not remove any important
5449 conversions, because builtins don't apply to class
5450 objects directly. */
5451 conv = cand->convs[0];
5452 if (conv->kind == ck_ref_bind)
5453 conv = next_conversion (conv);
5454 arg1 = convert_like (conv, arg1, complain);
5456 if (arg2)
5458 conv = cand->convs[1];
5459 if (conv->kind == ck_ref_bind)
5460 conv = next_conversion (conv);
5461 else
5462 arg2 = decay_conversion (arg2, complain);
5464 /* We need to call warn_logical_operator before
5465 converting arg2 to a boolean_type, but after
5466 decaying an enumerator to its value. */
5467 if (complain & tf_warning)
5468 warn_logical_operator (loc, code, boolean_type_node,
5469 code_orig_arg1, arg1,
5470 code_orig_arg2, arg2);
5472 arg2 = convert_like (conv, arg2, complain);
5474 if (arg3)
5476 conv = cand->convs[2];
5477 if (conv->kind == ck_ref_bind)
5478 conv = next_conversion (conv);
5479 arg3 = convert_like (conv, arg3, complain);
5485 user_defined_result_ready:
5487 /* Free all the conversions we allocated. */
5488 obstack_free (&conversion_obstack, p);
5490 if (result || result_valid_p)
5491 return result;
5493 builtin:
5494 switch (code)
5496 case MODIFY_EXPR:
5497 return cp_build_modify_expr (arg1, code2, arg2, complain);
5499 case INDIRECT_REF:
5500 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
5502 case TRUTH_ANDIF_EXPR:
5503 case TRUTH_ORIF_EXPR:
5504 case TRUTH_AND_EXPR:
5505 case TRUTH_OR_EXPR:
5506 warn_logical_operator (loc, code, boolean_type_node,
5507 code_orig_arg1, arg1, code_orig_arg2, arg2);
5508 /* Fall through. */
5509 case PLUS_EXPR:
5510 case MINUS_EXPR:
5511 case MULT_EXPR:
5512 case TRUNC_DIV_EXPR:
5513 case GT_EXPR:
5514 case LT_EXPR:
5515 case GE_EXPR:
5516 case LE_EXPR:
5517 case EQ_EXPR:
5518 case NE_EXPR:
5519 case MAX_EXPR:
5520 case MIN_EXPR:
5521 case LSHIFT_EXPR:
5522 case RSHIFT_EXPR:
5523 case TRUNC_MOD_EXPR:
5524 case BIT_AND_EXPR:
5525 case BIT_IOR_EXPR:
5526 case BIT_XOR_EXPR:
5527 return cp_build_binary_op (loc, code, arg1, arg2, complain);
5529 case UNARY_PLUS_EXPR:
5530 case NEGATE_EXPR:
5531 case BIT_NOT_EXPR:
5532 case TRUTH_NOT_EXPR:
5533 case PREINCREMENT_EXPR:
5534 case POSTINCREMENT_EXPR:
5535 case PREDECREMENT_EXPR:
5536 case POSTDECREMENT_EXPR:
5537 case REALPART_EXPR:
5538 case IMAGPART_EXPR:
5539 case ABS_EXPR:
5540 return cp_build_unary_op (code, arg1, candidates != 0, complain);
5542 case ARRAY_REF:
5543 return cp_build_array_ref (input_location, arg1, arg2, complain);
5545 case MEMBER_REF:
5546 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_ARROW_STAR,
5547 complain),
5548 arg2, complain);
5550 /* The caller will deal with these. */
5551 case ADDR_EXPR:
5552 case COMPONENT_REF:
5553 case COMPOUND_EXPR:
5554 return NULL_TREE;
5556 default:
5557 gcc_unreachable ();
5559 return NULL_TREE;
5562 /* Wrapper for above. */
5564 tree
5565 build_new_op (location_t loc, enum tree_code code, int flags,
5566 tree arg1, tree arg2, tree arg3,
5567 tree *overload, tsubst_flags_t complain)
5569 tree ret;
5570 bool subtime = timevar_cond_start (TV_OVERLOAD);
5571 ret = build_new_op_1 (loc, code, flags, arg1, arg2, arg3,
5572 overload, complain);
5573 timevar_cond_stop (TV_OVERLOAD, subtime);
5574 return ret;
5577 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
5578 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
5580 static bool
5581 non_placement_deallocation_fn_p (tree t)
5583 /* A template instance is never a usual deallocation function,
5584 regardless of its signature. */
5585 if (TREE_CODE (t) == TEMPLATE_DECL
5586 || primary_template_instantiation_p (t))
5587 return false;
5589 /* If a class T has a member deallocation function named operator delete
5590 with exactly one parameter, then that function is a usual
5591 (non-placement) deallocation function. If class T does not declare
5592 such an operator delete but does declare a member deallocation
5593 function named operator delete with exactly two parameters, the second
5594 of which has type std::size_t (18.2), then this function is a usual
5595 deallocation function. */
5596 t = FUNCTION_ARG_CHAIN (t);
5597 if (t == void_list_node
5598 || (t && same_type_p (TREE_VALUE (t), size_type_node)
5599 && TREE_CHAIN (t) == void_list_node))
5600 return true;
5601 return false;
5604 /* Build a call to operator delete. This has to be handled very specially,
5605 because the restrictions on what signatures match are different from all
5606 other call instances. For a normal delete, only a delete taking (void *)
5607 or (void *, size_t) is accepted. For a placement delete, only an exact
5608 match with the placement new is accepted.
5610 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
5611 ADDR is the pointer to be deleted.
5612 SIZE is the size of the memory block to be deleted.
5613 GLOBAL_P is true if the delete-expression should not consider
5614 class-specific delete operators.
5615 PLACEMENT is the corresponding placement new call, or NULL_TREE.
5617 If this call to "operator delete" is being generated as part to
5618 deallocate memory allocated via a new-expression (as per [expr.new]
5619 which requires that if the initialization throws an exception then
5620 we call a deallocation function), then ALLOC_FN is the allocation
5621 function. */
5623 tree
5624 build_op_delete_call (enum tree_code code, tree addr, tree size,
5625 bool global_p, tree placement,
5626 tree alloc_fn, tsubst_flags_t complain)
5628 tree fn = NULL_TREE;
5629 tree fns, fnname, type, t;
5631 if (addr == error_mark_node)
5632 return error_mark_node;
5634 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
5636 fnname = ansi_opname (code);
5638 if (CLASS_TYPE_P (type)
5639 && COMPLETE_TYPE_P (complete_type (type))
5640 && !global_p)
5641 /* In [class.free]
5643 If the result of the lookup is ambiguous or inaccessible, or if
5644 the lookup selects a placement deallocation function, the
5645 program is ill-formed.
5647 Therefore, we ask lookup_fnfields to complain about ambiguity. */
5649 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
5650 if (fns == error_mark_node)
5651 return error_mark_node;
5653 else
5654 fns = NULL_TREE;
5656 if (fns == NULL_TREE)
5657 fns = lookup_name_nonclass (fnname);
5659 /* Strip const and volatile from addr. */
5660 addr = cp_convert (ptr_type_node, addr, complain);
5662 if (placement)
5664 /* "A declaration of a placement deallocation function matches the
5665 declaration of a placement allocation function if it has the same
5666 number of parameters and, after parameter transformations (8.3.5),
5667 all parameter types except the first are identical."
5669 So we build up the function type we want and ask instantiate_type
5670 to get it for us. */
5671 t = FUNCTION_ARG_CHAIN (alloc_fn);
5672 t = tree_cons (NULL_TREE, ptr_type_node, t);
5673 t = build_function_type (void_type_node, t);
5675 fn = instantiate_type (t, fns, tf_none);
5676 if (fn == error_mark_node)
5677 return NULL_TREE;
5679 if (BASELINK_P (fn))
5680 fn = BASELINK_FUNCTIONS (fn);
5682 /* "If the lookup finds the two-parameter form of a usual deallocation
5683 function (3.7.4.2) and that function, considered as a placement
5684 deallocation function, would have been selected as a match for the
5685 allocation function, the program is ill-formed." */
5686 if (non_placement_deallocation_fn_p (fn))
5688 /* But if the class has an operator delete (void *), then that is
5689 the usual deallocation function, so we shouldn't complain
5690 about using the operator delete (void *, size_t). */
5691 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
5692 t; t = OVL_NEXT (t))
5694 tree elt = OVL_CURRENT (t);
5695 if (non_placement_deallocation_fn_p (elt)
5696 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
5697 goto ok;
5699 if (complain & tf_error)
5701 permerror (0, "non-placement deallocation function %q+D", fn);
5702 permerror (input_location, "selected for placement delete");
5704 else
5705 return error_mark_node;
5706 ok:;
5709 else
5710 /* "Any non-placement deallocation function matches a non-placement
5711 allocation function. If the lookup finds a single matching
5712 deallocation function, that function will be called; otherwise, no
5713 deallocation function will be called." */
5714 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
5715 t; t = OVL_NEXT (t))
5717 tree elt = OVL_CURRENT (t);
5718 if (non_placement_deallocation_fn_p (elt))
5720 fn = elt;
5721 /* "If a class T has a member deallocation function named
5722 operator delete with exactly one parameter, then that
5723 function is a usual (non-placement) deallocation
5724 function. If class T does not declare such an operator
5725 delete but does declare a member deallocation function named
5726 operator delete with exactly two parameters, the second of
5727 which has type std::size_t (18.2), then this function is a
5728 usual deallocation function."
5730 So (void*) beats (void*, size_t). */
5731 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
5732 break;
5736 /* If we have a matching function, call it. */
5737 if (fn)
5739 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
5741 /* If the FN is a member function, make sure that it is
5742 accessible. */
5743 if (BASELINK_P (fns))
5744 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
5745 complain);
5747 /* Core issue 901: It's ok to new a type with deleted delete. */
5748 if (DECL_DELETED_FN (fn) && alloc_fn)
5749 return NULL_TREE;
5751 if (placement)
5753 /* The placement args might not be suitable for overload
5754 resolution at this point, so build the call directly. */
5755 int nargs = call_expr_nargs (placement);
5756 tree *argarray = XALLOCAVEC (tree, nargs);
5757 int i;
5758 argarray[0] = addr;
5759 for (i = 1; i < nargs; i++)
5760 argarray[i] = CALL_EXPR_ARG (placement, i);
5761 mark_used (fn);
5762 return build_cxx_call (fn, nargs, argarray, complain);
5764 else
5766 tree ret;
5767 vec<tree, va_gc> *args;
5768 vec_alloc (args, 2);
5769 args->quick_push (addr);
5770 if (FUNCTION_ARG_CHAIN (fn) != void_list_node)
5771 args->quick_push (size);
5772 ret = cp_build_function_call_vec (fn, &args, complain);
5773 vec_free (args);
5774 return ret;
5778 /* [expr.new]
5780 If no unambiguous matching deallocation function can be found,
5781 propagating the exception does not cause the object's memory to
5782 be freed. */
5783 if (alloc_fn)
5785 if ((complain & tf_warning)
5786 && !placement)
5787 warning (0, "no corresponding deallocation function for %qD",
5788 alloc_fn);
5789 return NULL_TREE;
5792 if (complain & tf_error)
5793 error ("no suitable %<operator %s%> for %qT",
5794 operator_name_info[(int)code].name, type);
5795 return error_mark_node;
5798 /* If the current scope isn't allowed to access DECL along
5799 BASETYPE_PATH, give an error. The most derived class in
5800 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
5801 the declaration to use in the error diagnostic. */
5803 bool
5804 enforce_access (tree basetype_path, tree decl, tree diag_decl,
5805 tsubst_flags_t complain)
5807 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
5809 if (!accessible_p (basetype_path, decl, true))
5811 if (complain & tf_error)
5813 if (TREE_PRIVATE (decl))
5814 error ("%q+#D is private", diag_decl);
5815 else if (TREE_PROTECTED (decl))
5816 error ("%q+#D is protected", diag_decl);
5817 else
5818 error ("%q+#D is inaccessible", diag_decl);
5819 error ("within this context");
5821 return false;
5824 return true;
5827 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
5828 bitwise or of LOOKUP_* values. If any errors are warnings are
5829 generated, set *DIAGNOSTIC_FN to "error" or "warning",
5830 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
5831 to NULL. */
5833 static tree
5834 build_temp (tree expr, tree type, int flags,
5835 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
5837 int savew, savee;
5838 vec<tree, va_gc> *args;
5840 savew = warningcount + werrorcount, savee = errorcount;
5841 args = make_tree_vector_single (expr);
5842 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
5843 &args, type, flags, complain);
5844 release_tree_vector (args);
5845 if (warningcount + werrorcount > savew)
5846 *diagnostic_kind = DK_WARNING;
5847 else if (errorcount > savee)
5848 *diagnostic_kind = DK_ERROR;
5849 else
5850 *diagnostic_kind = DK_UNSPECIFIED;
5851 return expr;
5854 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
5855 EXPR is implicitly converted to type TOTYPE.
5856 FN and ARGNUM are used for diagnostics. */
5858 static void
5859 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
5861 /* Issue warnings about peculiar, but valid, uses of NULL. */
5862 if (expr == null_node && TREE_CODE (totype) != BOOLEAN_TYPE
5863 && ARITHMETIC_TYPE_P (totype))
5865 source_location loc =
5866 expansion_point_location_if_in_system_header (input_location);
5868 if (fn)
5869 warning_at (loc, OPT_Wconversion_null,
5870 "passing NULL to non-pointer argument %P of %qD",
5871 argnum, fn);
5872 else
5873 warning_at (loc, OPT_Wconversion_null,
5874 "converting to non-pointer type %qT from NULL", totype);
5877 /* Issue warnings if "false" is converted to a NULL pointer */
5878 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
5879 && TYPE_PTR_P (totype))
5881 if (fn)
5882 warning_at (input_location, OPT_Wconversion_null,
5883 "converting %<false%> to pointer type for argument %P "
5884 "of %qD", argnum, fn);
5885 else
5886 warning_at (input_location, OPT_Wconversion_null,
5887 "converting %<false%> to pointer type %qT", totype);
5891 /* Perform the conversions in CONVS on the expression EXPR. FN and
5892 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
5893 indicates the `this' argument of a method. INNER is nonzero when
5894 being called to continue a conversion chain. It is negative when a
5895 reference binding will be applied, positive otherwise. If
5896 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
5897 conversions will be emitted if appropriate. If C_CAST_P is true,
5898 this conversion is coming from a C-style cast; in that case,
5899 conversions to inaccessible bases are permitted. */
5901 static tree
5902 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
5903 int inner, bool issue_conversion_warnings,
5904 bool c_cast_p, tsubst_flags_t complain)
5906 tree totype = convs->type;
5907 diagnostic_t diag_kind;
5908 int flags;
5909 location_t loc = EXPR_LOC_OR_HERE (expr);
5911 if (convs->bad_p && !(complain & tf_error))
5912 return error_mark_node;
5914 if (convs->bad_p
5915 && convs->kind != ck_user
5916 && convs->kind != ck_list
5917 && convs->kind != ck_ambig
5918 && (convs->kind != ck_ref_bind
5919 || convs->user_conv_p)
5920 && convs->kind != ck_rvalue
5921 && convs->kind != ck_base)
5923 conversion *t = convs;
5925 /* Give a helpful error if this is bad because of excess braces. */
5926 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5927 && SCALAR_TYPE_P (totype)
5928 && CONSTRUCTOR_NELTS (expr) > 0
5929 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
5930 permerror (loc, "too many braces around initializer for %qT", totype);
5932 for (; t ; t = next_conversion (t))
5934 if (t->kind == ck_user && t->cand->reason)
5936 permerror (loc, "invalid user-defined conversion "
5937 "from %qT to %qT", TREE_TYPE (expr), totype);
5938 print_z_candidate (loc, "candidate is:", t->cand);
5939 expr = convert_like_real (t, expr, fn, argnum, 1,
5940 /*issue_conversion_warnings=*/false,
5941 /*c_cast_p=*/false,
5942 complain);
5943 if (convs->kind == ck_ref_bind)
5944 return convert_to_reference (totype, expr, CONV_IMPLICIT,
5945 LOOKUP_NORMAL, NULL_TREE,
5946 complain);
5947 else
5948 return cp_convert (totype, expr, complain);
5950 else if (t->kind == ck_user || !t->bad_p)
5952 expr = convert_like_real (t, expr, fn, argnum, 1,
5953 /*issue_conversion_warnings=*/false,
5954 /*c_cast_p=*/false,
5955 complain);
5956 break;
5958 else if (t->kind == ck_ambig)
5959 return convert_like_real (t, expr, fn, argnum, 1,
5960 /*issue_conversion_warnings=*/false,
5961 /*c_cast_p=*/false,
5962 complain);
5963 else if (t->kind == ck_identity)
5964 break;
5966 if (permerror (loc, "invalid conversion from %qT to %qT",
5967 TREE_TYPE (expr), totype)
5968 && fn)
5969 inform (DECL_SOURCE_LOCATION (fn),
5970 "initializing argument %P of %qD", argnum, fn);
5972 return cp_convert (totype, expr, complain);
5975 if (issue_conversion_warnings && (complain & tf_warning))
5976 conversion_null_warnings (totype, expr, fn, argnum);
5978 switch (convs->kind)
5980 case ck_user:
5982 struct z_candidate *cand = convs->cand;
5983 tree convfn = cand->fn;
5984 unsigned i;
5986 /* When converting from an init list we consider explicit
5987 constructors, but actually trying to call one is an error. */
5988 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
5989 /* Unless this is for direct-list-initialization. */
5990 && !(BRACE_ENCLOSED_INITIALIZER_P (expr)
5991 && CONSTRUCTOR_IS_DIRECT_INIT (expr)))
5993 error ("converting to %qT from initializer list would use "
5994 "explicit constructor %qD", totype, convfn);
5997 /* If we're initializing from {}, it's value-initialization. */
5998 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5999 && CONSTRUCTOR_NELTS (expr) == 0
6000 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
6002 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
6003 expr = build_value_init (totype, complain);
6004 expr = get_target_expr_sfinae (expr, complain);
6005 if (expr != error_mark_node)
6007 TARGET_EXPR_LIST_INIT_P (expr) = true;
6008 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
6010 return expr;
6013 expr = mark_rvalue_use (expr);
6015 /* Set user_conv_p on the argument conversions, so rvalue/base
6016 handling knows not to allow any more UDCs. */
6017 for (i = 0; i < cand->num_convs; ++i)
6018 cand->convs[i]->user_conv_p = true;
6020 expr = build_over_call (cand, LOOKUP_NORMAL, complain);
6022 /* If this is a constructor or a function returning an aggr type,
6023 we need to build up a TARGET_EXPR. */
6024 if (DECL_CONSTRUCTOR_P (convfn))
6026 expr = build_cplus_new (totype, expr, complain);
6028 /* Remember that this was list-initialization. */
6029 if (convs->check_narrowing && expr != error_mark_node)
6030 TARGET_EXPR_LIST_INIT_P (expr) = true;
6033 return expr;
6035 case ck_identity:
6036 expr = mark_rvalue_use (expr);
6037 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
6039 int nelts = CONSTRUCTOR_NELTS (expr);
6040 if (nelts == 0)
6041 expr = build_value_init (totype, complain);
6042 else if (nelts == 1)
6043 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6044 else
6045 gcc_unreachable ();
6048 if (type_unknown_p (expr))
6049 expr = instantiate_type (totype, expr, complain);
6050 /* Convert a constant to its underlying value, unless we are
6051 about to bind it to a reference, in which case we need to
6052 leave it as an lvalue. */
6053 if (inner >= 0)
6055 expr = decl_constant_value_safe (expr);
6056 if (expr == null_node && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
6057 /* If __null has been converted to an integer type, we do not
6058 want to warn about uses of EXPR as an integer, rather than
6059 as a pointer. */
6060 expr = build_int_cst (totype, 0);
6062 return expr;
6063 case ck_ambig:
6064 /* We leave bad_p off ck_ambig because overload resolution considers
6065 it valid, it just fails when we try to perform it. So we need to
6066 check complain here, too. */
6067 if (complain & tf_error)
6069 /* Call build_user_type_conversion again for the error. */
6070 build_user_type_conversion (totype, convs->u.expr, LOOKUP_NORMAL,
6071 complain);
6072 if (fn)
6073 inform (input_location, "initializing argument %P of %q+D",
6074 argnum, fn);
6076 return error_mark_node;
6078 case ck_list:
6080 /* Conversion to std::initializer_list<T>. */
6081 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
6082 tree new_ctor = build_constructor (init_list_type_node, NULL);
6083 unsigned len = CONSTRUCTOR_NELTS (expr);
6084 tree array, val, field;
6085 vec<constructor_elt, va_gc> *vec = NULL;
6086 unsigned ix;
6088 /* Convert all the elements. */
6089 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
6091 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
6092 1, false, false, complain);
6093 if (sub == error_mark_node)
6094 return sub;
6095 if (!BRACE_ENCLOSED_INITIALIZER_P (val))
6096 check_narrowing (TREE_TYPE (sub), val);
6097 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
6098 if (!TREE_CONSTANT (sub))
6099 TREE_CONSTANT (new_ctor) = false;
6101 /* Build up the array. */
6102 elttype = cp_build_qualified_type
6103 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
6104 array = build_array_of_n_type (elttype, len);
6105 array = finish_compound_literal (array, new_ctor, complain);
6106 /* Take the address explicitly rather than via decay_conversion
6107 to avoid the error about taking the address of a temporary. */
6108 array = cp_build_addr_expr (array, complain);
6109 array = cp_convert (build_pointer_type (elttype), array, complain);
6111 /* Build up the initializer_list object. */
6112 totype = complete_type (totype);
6113 field = next_initializable_field (TYPE_FIELDS (totype));
6114 CONSTRUCTOR_APPEND_ELT (vec, field, array);
6115 field = next_initializable_field (DECL_CHAIN (field));
6116 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
6117 new_ctor = build_constructor (totype, vec);
6118 return get_target_expr_sfinae (new_ctor, complain);
6121 case ck_aggr:
6122 if (TREE_CODE (totype) == COMPLEX_TYPE)
6124 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
6125 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
6126 real = perform_implicit_conversion (TREE_TYPE (totype),
6127 real, complain);
6128 imag = perform_implicit_conversion (TREE_TYPE (totype),
6129 imag, complain);
6130 expr = build2 (COMPLEX_EXPR, totype, real, imag);
6131 return fold_if_not_in_template (expr);
6133 expr = reshape_init (totype, expr, complain);
6134 return get_target_expr_sfinae (digest_init (totype, expr, complain),
6135 complain);
6137 default:
6138 break;
6141 expr = convert_like_real (next_conversion (convs), expr, fn, argnum,
6142 convs->kind == ck_ref_bind ? -1 : 1,
6143 convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
6144 c_cast_p,
6145 complain);
6146 if (expr == error_mark_node)
6147 return error_mark_node;
6149 switch (convs->kind)
6151 case ck_rvalue:
6152 expr = decay_conversion (expr, complain);
6153 if (expr == error_mark_node)
6154 return error_mark_node;
6156 if (! MAYBE_CLASS_TYPE_P (totype))
6157 return expr;
6158 /* Else fall through. */
6159 case ck_base:
6160 if (convs->kind == ck_base && !convs->need_temporary_p)
6162 /* We are going to bind a reference directly to a base-class
6163 subobject of EXPR. */
6164 /* Build an expression for `*((base*) &expr)'. */
6165 expr = cp_build_addr_expr (expr, complain);
6166 expr = convert_to_base (expr, build_pointer_type (totype),
6167 !c_cast_p, /*nonnull=*/true, complain);
6168 expr = cp_build_indirect_ref (expr, RO_IMPLICIT_CONVERSION, complain);
6169 return expr;
6172 /* Copy-initialization where the cv-unqualified version of the source
6173 type is the same class as, or a derived class of, the class of the
6174 destination [is treated as direct-initialization]. [dcl.init] */
6175 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
6176 if (convs->user_conv_p)
6177 /* This conversion is being done in the context of a user-defined
6178 conversion (i.e. the second step of copy-initialization), so
6179 don't allow any more. */
6180 flags |= LOOKUP_NO_CONVERSION;
6181 if (convs->rvaluedness_matches_p)
6182 flags |= LOOKUP_PREFER_RVALUE;
6183 if (TREE_CODE (expr) == TARGET_EXPR
6184 && TARGET_EXPR_LIST_INIT_P (expr))
6185 /* Copy-list-initialization doesn't actually involve a copy. */
6186 return expr;
6187 expr = build_temp (expr, totype, flags, &diag_kind, complain);
6188 if (diag_kind && fn && complain)
6189 emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (fn), 0,
6190 " initializing argument %P of %qD", argnum, fn);
6191 return build_cplus_new (totype, expr, complain);
6193 case ck_ref_bind:
6195 tree ref_type = totype;
6197 if (convs->bad_p && !next_conversion (convs)->bad_p)
6199 gcc_assert (TYPE_REF_IS_RVALUE (ref_type)
6200 && real_lvalue_p (expr));
6202 error_at (loc, "cannot bind %qT lvalue to %qT",
6203 TREE_TYPE (expr), totype);
6204 if (fn)
6205 inform (input_location,
6206 "initializing argument %P of %q+D", argnum, fn);
6207 return error_mark_node;
6210 /* If necessary, create a temporary.
6212 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
6213 that need temporaries, even when their types are reference
6214 compatible with the type of reference being bound, so the
6215 upcoming call to cp_build_addr_expr doesn't fail. */
6216 if (convs->need_temporary_p
6217 || TREE_CODE (expr) == CONSTRUCTOR
6218 || TREE_CODE (expr) == VA_ARG_EXPR)
6220 /* Otherwise, a temporary of type "cv1 T1" is created and
6221 initialized from the initializer expression using the rules
6222 for a non-reference copy-initialization (8.5). */
6224 tree type = TREE_TYPE (ref_type);
6225 cp_lvalue_kind lvalue = real_lvalue_p (expr);
6227 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6228 (type, next_conversion (convs)->type));
6229 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
6230 && !TYPE_REF_IS_RVALUE (ref_type))
6232 /* If the reference is volatile or non-const, we
6233 cannot create a temporary. */
6234 if (lvalue & clk_bitfield)
6235 error_at (loc, "cannot bind bitfield %qE to %qT",
6236 expr, ref_type);
6237 else if (lvalue & clk_packed)
6238 error_at (loc, "cannot bind packed field %qE to %qT",
6239 expr, ref_type);
6240 else
6241 error_at (loc, "cannot bind rvalue %qE to %qT",
6242 expr, ref_type);
6243 return error_mark_node;
6245 /* If the source is a packed field, and we must use a copy
6246 constructor, then building the target expr will require
6247 binding the field to the reference parameter to the
6248 copy constructor, and we'll end up with an infinite
6249 loop. If we can use a bitwise copy, then we'll be
6250 OK. */
6251 if ((lvalue & clk_packed)
6252 && CLASS_TYPE_P (type)
6253 && type_has_nontrivial_copy_init (type))
6255 error_at (loc, "cannot bind packed field %qE to %qT",
6256 expr, ref_type);
6257 return error_mark_node;
6259 if (lvalue & clk_bitfield)
6261 expr = convert_bitfield_to_declared_type (expr);
6262 expr = fold_convert (type, expr);
6264 expr = build_target_expr_with_type (expr, type, complain);
6267 /* Take the address of the thing to which we will bind the
6268 reference. */
6269 expr = cp_build_addr_expr (expr, complain);
6270 if (expr == error_mark_node)
6271 return error_mark_node;
6273 /* Convert it to a pointer to the type referred to by the
6274 reference. This will adjust the pointer if a derived to
6275 base conversion is being performed. */
6276 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
6277 expr, complain);
6278 /* Convert the pointer to the desired reference type. */
6279 return build_nop (ref_type, expr);
6282 case ck_lvalue:
6283 return decay_conversion (expr, complain);
6285 case ck_qual:
6286 /* Warn about deprecated conversion if appropriate. */
6287 string_conv_p (totype, expr, 1);
6288 break;
6290 case ck_ptr:
6291 if (convs->base_p)
6292 expr = convert_to_base (expr, totype, !c_cast_p,
6293 /*nonnull=*/false, complain);
6294 return build_nop (totype, expr);
6296 case ck_pmem:
6297 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
6298 c_cast_p, complain);
6300 default:
6301 break;
6304 if (convs->check_narrowing)
6305 check_narrowing (totype, expr);
6307 if (issue_conversion_warnings)
6308 expr = cp_convert_and_check (totype, expr, complain);
6309 else
6310 expr = cp_convert (totype, expr, complain);
6312 return expr;
6315 /* ARG is being passed to a varargs function. Perform any conversions
6316 required. Return the converted value. */
6318 tree
6319 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
6321 tree arg_type;
6322 location_t loc = EXPR_LOC_OR_HERE (arg);
6324 /* [expr.call]
6326 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6327 standard conversions are performed. */
6328 arg = decay_conversion (arg, complain);
6329 arg_type = TREE_TYPE (arg);
6330 /* [expr.call]
6332 If the argument has integral or enumeration type that is subject
6333 to the integral promotions (_conv.prom_), or a floating point
6334 type that is subject to the floating point promotion
6335 (_conv.fpprom_), the value of the argument is converted to the
6336 promoted type before the call. */
6337 if (TREE_CODE (arg_type) == REAL_TYPE
6338 && (TYPE_PRECISION (arg_type)
6339 < TYPE_PRECISION (double_type_node))
6340 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
6342 if ((complain & tf_warning)
6343 && warn_double_promotion && !c_inhibit_evaluation_warnings)
6344 warning_at (loc, OPT_Wdouble_promotion,
6345 "implicit conversion from %qT to %qT when passing "
6346 "argument to function",
6347 arg_type, double_type_node);
6348 arg = convert_to_real (double_type_node, arg);
6350 else if (NULLPTR_TYPE_P (arg_type))
6351 arg = null_pointer_node;
6352 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
6354 if (SCOPED_ENUM_P (arg_type) && !abi_version_at_least (6))
6356 if (complain & tf_warning)
6357 warning_at (loc, OPT_Wabi, "scoped enum %qT will not promote to an "
6358 "integral type in a future version of GCC", arg_type);
6359 arg = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg, complain);
6361 arg = cp_perform_integral_promotions (arg, complain);
6364 arg = require_complete_type_sfinae (arg, complain);
6365 arg_type = TREE_TYPE (arg);
6367 if (arg != error_mark_node
6368 /* In a template (or ill-formed code), we can have an incomplete type
6369 even after require_complete_type_sfinae, in which case we don't know
6370 whether it has trivial copy or not. */
6371 && COMPLETE_TYPE_P (arg_type))
6373 /* Build up a real lvalue-to-rvalue conversion in case the
6374 copy constructor is trivial but not callable. */
6375 if (!cp_unevaluated_operand && CLASS_TYPE_P (arg_type))
6376 force_rvalue (arg, complain);
6378 /* [expr.call] 5.2.2/7:
6379 Passing a potentially-evaluated argument of class type (Clause 9)
6380 with a non-trivial copy constructor or a non-trivial destructor
6381 with no corresponding parameter is conditionally-supported, with
6382 implementation-defined semantics.
6384 We used to just warn here and do a bitwise copy, but now
6385 cp_expr_size will abort if we try to do that.
6387 If the call appears in the context of a sizeof expression,
6388 it is not potentially-evaluated. */
6389 if (cp_unevaluated_operand == 0
6390 && (type_has_nontrivial_copy_init (arg_type)
6391 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type)))
6393 if (complain & tf_error)
6394 error_at (loc, "cannot pass objects of non-trivially-copyable "
6395 "type %q#T through %<...%>", arg_type);
6396 else
6397 return error_mark_node;
6401 return arg;
6404 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
6406 tree
6407 build_x_va_arg (source_location loc, tree expr, tree type)
6409 if (processing_template_decl)
6410 return build_min (VA_ARG_EXPR, type, expr);
6412 type = complete_type_or_else (type, NULL_TREE);
6414 if (expr == error_mark_node || !type)
6415 return error_mark_node;
6417 expr = mark_lvalue_use (expr);
6419 if (type_has_nontrivial_copy_init (type)
6420 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
6421 || TREE_CODE (type) == REFERENCE_TYPE)
6423 /* Remove reference types so we don't ICE later on. */
6424 tree type1 = non_reference (type);
6425 /* conditionally-supported behavior [expr.call] 5.2.2/7. */
6426 error ("cannot receive objects of non-trivially-copyable type %q#T "
6427 "through %<...%>; ", type);
6428 expr = convert (build_pointer_type (type1), null_node);
6429 expr = cp_build_indirect_ref (expr, RO_NULL, tf_warning_or_error);
6430 return expr;
6433 return build_va_arg (loc, expr, type);
6436 /* TYPE has been given to va_arg. Apply the default conversions which
6437 would have happened when passed via ellipsis. Return the promoted
6438 type, or the passed type if there is no change. */
6440 tree
6441 cxx_type_promotes_to (tree type)
6443 tree promote;
6445 /* Perform the array-to-pointer and function-to-pointer
6446 conversions. */
6447 type = type_decays_to (type);
6449 promote = type_promotes_to (type);
6450 if (same_type_p (type, promote))
6451 promote = type;
6453 return promote;
6456 /* ARG is a default argument expression being passed to a parameter of
6457 the indicated TYPE, which is a parameter to FN. PARMNUM is the
6458 zero-based argument number. Do any required conversions. Return
6459 the converted value. */
6461 static GTY(()) vec<tree, va_gc> *default_arg_context;
6462 void
6463 push_defarg_context (tree fn)
6464 { vec_safe_push (default_arg_context, fn); }
6466 void
6467 pop_defarg_context (void)
6468 { default_arg_context->pop (); }
6470 tree
6471 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
6472 tsubst_flags_t complain)
6474 int i;
6475 tree t;
6477 /* See through clones. */
6478 fn = DECL_ORIGIN (fn);
6480 /* Detect recursion. */
6481 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
6482 if (t == fn)
6484 if (complain & tf_error)
6485 error ("recursive evaluation of default argument for %q#D", fn);
6486 return error_mark_node;
6489 /* If the ARG is an unparsed default argument expression, the
6490 conversion cannot be performed. */
6491 if (TREE_CODE (arg) == DEFAULT_ARG)
6493 if (complain & tf_error)
6494 error ("call to %qD uses the default argument for parameter %P, which "
6495 "is not yet defined", fn, parmnum);
6496 return error_mark_node;
6499 push_defarg_context (fn);
6501 if (fn && DECL_TEMPLATE_INFO (fn))
6502 arg = tsubst_default_argument (fn, type, arg, complain);
6504 /* Due to:
6506 [dcl.fct.default]
6508 The names in the expression are bound, and the semantic
6509 constraints are checked, at the point where the default
6510 expressions appears.
6512 we must not perform access checks here. */
6513 push_deferring_access_checks (dk_no_check);
6514 /* We must make a copy of ARG, in case subsequent processing
6515 alters any part of it. */
6516 arg = break_out_target_exprs (arg);
6517 if (TREE_CODE (arg) == CONSTRUCTOR)
6519 arg = digest_init (type, arg, complain);
6520 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
6521 ICR_DEFAULT_ARGUMENT, fn, parmnum,
6522 complain);
6524 else
6526 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
6527 ICR_DEFAULT_ARGUMENT, fn, parmnum,
6528 complain);
6529 arg = convert_for_arg_passing (type, arg, complain);
6531 pop_deferring_access_checks();
6533 pop_defarg_context ();
6535 return arg;
6538 /* Returns the type which will really be used for passing an argument of
6539 type TYPE. */
6541 tree
6542 type_passed_as (tree type)
6544 /* Pass classes with copy ctors by invisible reference. */
6545 if (TREE_ADDRESSABLE (type))
6547 type = build_reference_type (type);
6548 /* There are no other pointers to this temporary. */
6549 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
6551 else if (targetm.calls.promote_prototypes (type)
6552 && INTEGRAL_TYPE_P (type)
6553 && COMPLETE_TYPE_P (type)
6554 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
6555 TYPE_SIZE (integer_type_node)))
6556 type = integer_type_node;
6558 return type;
6561 /* Actually perform the appropriate conversion. */
6563 tree
6564 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
6566 tree bitfield_type;
6568 /* If VAL is a bitfield, then -- since it has already been converted
6569 to TYPE -- it cannot have a precision greater than TYPE.
6571 If it has a smaller precision, we must widen it here. For
6572 example, passing "int f:3;" to a function expecting an "int" will
6573 not result in any conversion before this point.
6575 If the precision is the same we must not risk widening. For
6576 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
6577 often have type "int", even though the C++ type for the field is
6578 "long long". If the value is being passed to a function
6579 expecting an "int", then no conversions will be required. But,
6580 if we call convert_bitfield_to_declared_type, the bitfield will
6581 be converted to "long long". */
6582 bitfield_type = is_bitfield_expr_with_lowered_type (val);
6583 if (bitfield_type
6584 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
6585 val = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type), val);
6587 if (val == error_mark_node)
6589 /* Pass classes with copy ctors by invisible reference. */
6590 else if (TREE_ADDRESSABLE (type))
6591 val = build1 (ADDR_EXPR, build_reference_type (type), val);
6592 else if (targetm.calls.promote_prototypes (type)
6593 && INTEGRAL_TYPE_P (type)
6594 && COMPLETE_TYPE_P (type)
6595 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
6596 TYPE_SIZE (integer_type_node)))
6597 val = cp_perform_integral_promotions (val, complain);
6598 if ((complain & tf_warning)
6599 && warn_suggest_attribute_format)
6601 tree rhstype = TREE_TYPE (val);
6602 const enum tree_code coder = TREE_CODE (rhstype);
6603 const enum tree_code codel = TREE_CODE (type);
6604 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6605 && coder == codel
6606 && check_missing_format_attribute (type, rhstype))
6607 warning (OPT_Wsuggest_attribute_format,
6608 "argument of function call might be a candidate for a format attribute");
6610 return val;
6613 /* Returns true iff FN is a function with magic varargs, i.e. ones for
6614 which no conversions at all should be done. This is true for some
6615 builtins which don't act like normal functions. */
6617 bool
6618 magic_varargs_p (tree fn)
6620 if (flag_enable_cilkplus && is_cilkplus_reduce_builtin (fn) != BUILT_IN_NONE)
6621 return true;
6623 if (DECL_BUILT_IN (fn))
6624 switch (DECL_FUNCTION_CODE (fn))
6626 case BUILT_IN_CLASSIFY_TYPE:
6627 case BUILT_IN_CONSTANT_P:
6628 case BUILT_IN_NEXT_ARG:
6629 case BUILT_IN_VA_START:
6630 return true;
6632 default:;
6633 return lookup_attribute ("type generic",
6634 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
6637 return false;
6640 /* Returns the decl of the dispatcher function if FN is a function version. */
6642 tree
6643 get_function_version_dispatcher (tree fn)
6645 tree dispatcher_decl = NULL;
6647 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6648 && DECL_FUNCTION_VERSIONED (fn));
6650 gcc_assert (targetm.get_function_versions_dispatcher);
6651 dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
6653 if (dispatcher_decl == NULL)
6655 error_at (input_location, "use of multiversioned function "
6656 "without a default");
6657 return NULL;
6660 retrofit_lang_decl (dispatcher_decl);
6661 gcc_assert (dispatcher_decl != NULL);
6662 return dispatcher_decl;
6665 /* fn is a function version dispatcher that is marked used. Mark all the
6666 semantically identical function versions it will dispatch as used. */
6668 void
6669 mark_versions_used (tree fn)
6671 struct cgraph_node *node;
6672 struct cgraph_function_version_info *node_v;
6673 struct cgraph_function_version_info *it_v;
6675 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6677 node = cgraph_get_node (fn);
6678 if (node == NULL)
6679 return;
6681 gcc_assert (node->dispatcher_function);
6683 node_v = get_cgraph_node_version (node);
6684 if (node_v == NULL)
6685 return;
6687 /* All semantically identical versions are chained. Traverse and mark each
6688 one of them as used. */
6689 it_v = node_v->next;
6690 while (it_v != NULL)
6692 mark_used (it_v->this_node->decl);
6693 it_v = it_v->next;
6697 /* Subroutine of the various build_*_call functions. Overload resolution
6698 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
6699 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
6700 bitmask of various LOOKUP_* flags which apply to the call itself. */
6702 static tree
6703 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
6705 tree fn = cand->fn;
6706 const vec<tree, va_gc> *args = cand->args;
6707 tree first_arg = cand->first_arg;
6708 conversion **convs = cand->convs;
6709 conversion *conv;
6710 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
6711 int parmlen;
6712 tree val;
6713 int i = 0;
6714 int j = 0;
6715 unsigned int arg_index = 0;
6716 int is_method = 0;
6717 int nargs;
6718 tree *argarray;
6719 bool already_used = false;
6721 /* In a template, there is no need to perform all of the work that
6722 is normally done. We are only interested in the type of the call
6723 expression, i.e., the return type of the function. Any semantic
6724 errors will be deferred until the template is instantiated. */
6725 if (processing_template_decl)
6727 tree expr, addr;
6728 tree return_type;
6729 const tree *argarray;
6730 unsigned int nargs;
6732 return_type = TREE_TYPE (TREE_TYPE (fn));
6733 nargs = vec_safe_length (args);
6734 if (first_arg == NULL_TREE)
6735 argarray = args->address ();
6736 else
6738 tree *alcarray;
6739 unsigned int ix;
6740 tree arg;
6742 ++nargs;
6743 alcarray = XALLOCAVEC (tree, nargs);
6744 alcarray[0] = first_arg;
6745 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
6746 alcarray[ix + 1] = arg;
6747 argarray = alcarray;
6750 addr = build_addr_func (fn, complain);
6751 if (addr == error_mark_node)
6752 return error_mark_node;
6753 expr = build_call_array_loc (input_location, return_type,
6754 addr, nargs, argarray);
6755 if (TREE_THIS_VOLATILE (fn) && cfun)
6756 current_function_returns_abnormally = 1;
6757 return convert_from_reference (expr);
6760 /* Give any warnings we noticed during overload resolution. */
6761 if (cand->warnings && (complain & tf_warning))
6763 struct candidate_warning *w;
6764 for (w = cand->warnings; w; w = w->next)
6765 joust (cand, w->loser, 1, complain);
6768 /* Make =delete work with SFINAE. */
6769 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
6770 return error_mark_node;
6772 if (DECL_FUNCTION_MEMBER_P (fn))
6774 tree access_fn;
6775 /* If FN is a template function, two cases must be considered.
6776 For example:
6778 struct A {
6779 protected:
6780 template <class T> void f();
6782 template <class T> struct B {
6783 protected:
6784 void g();
6786 struct C : A, B<int> {
6787 using A::f; // #1
6788 using B<int>::g; // #2
6791 In case #1 where `A::f' is a member template, DECL_ACCESS is
6792 recorded in the primary template but not in its specialization.
6793 We check access of FN using its primary template.
6795 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
6796 because it is a member of class template B, DECL_ACCESS is
6797 recorded in the specialization `B<int>::g'. We cannot use its
6798 primary template because `B<T>::g' and `B<int>::g' may have
6799 different access. */
6800 if (DECL_TEMPLATE_INFO (fn)
6801 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
6802 access_fn = DECL_TI_TEMPLATE (fn);
6803 else
6804 access_fn = fn;
6805 if (!perform_or_defer_access_check (cand->access_path, access_fn,
6806 fn, complain))
6807 return error_mark_node;
6810 /* If we're checking for implicit delete, don't bother with argument
6811 conversions. */
6812 if (flags & LOOKUP_SPECULATIVE)
6814 if (DECL_DELETED_FN (fn))
6816 if (complain & tf_error)
6817 mark_used (fn);
6818 return error_mark_node;
6820 if (cand->viable == 1)
6821 return fn;
6822 else if (!(complain & tf_error))
6823 /* Reject bad conversions now. */
6824 return error_mark_node;
6825 /* else continue to get conversion error. */
6828 /* N3276 magic doesn't apply to nested calls. */
6829 int decltype_flag = (complain & tf_decltype);
6830 complain &= ~tf_decltype;
6832 /* Find maximum size of vector to hold converted arguments. */
6833 parmlen = list_length (parm);
6834 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
6835 if (parmlen > nargs)
6836 nargs = parmlen;
6837 argarray = XALLOCAVEC (tree, nargs);
6839 /* The implicit parameters to a constructor are not considered by overload
6840 resolution, and must be of the proper type. */
6841 if (DECL_CONSTRUCTOR_P (fn))
6843 tree object_arg;
6844 if (first_arg != NULL_TREE)
6846 object_arg = first_arg;
6847 first_arg = NULL_TREE;
6849 else
6851 object_arg = (*args)[arg_index];
6852 ++arg_index;
6854 argarray[j++] = build_this (object_arg);
6855 parm = TREE_CHAIN (parm);
6856 /* We should never try to call the abstract constructor. */
6857 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
6859 if (DECL_HAS_VTT_PARM_P (fn))
6861 argarray[j++] = (*args)[arg_index];
6862 ++arg_index;
6863 parm = TREE_CHAIN (parm);
6866 /* Bypass access control for 'this' parameter. */
6867 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6869 tree parmtype = TREE_VALUE (parm);
6870 tree arg = build_this (first_arg != NULL_TREE
6871 ? first_arg
6872 : (*args)[arg_index]);
6873 tree argtype = TREE_TYPE (arg);
6874 tree converted_arg;
6875 tree base_binfo;
6877 if (convs[i]->bad_p)
6879 if (complain & tf_error)
6880 permerror (input_location, "passing %qT as %<this%> argument of %q#D discards qualifiers",
6881 TREE_TYPE (argtype), fn);
6882 else
6883 return error_mark_node;
6886 /* See if the function member or the whole class type is declared
6887 final and the call can be devirtualized. */
6888 if (DECL_FINAL_P (fn)
6889 || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
6890 flags |= LOOKUP_NONVIRTUAL;
6892 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
6893 X is called for an object that is not of type X, or of a type
6894 derived from X, the behavior is undefined.
6896 So we can assume that anything passed as 'this' is non-null, and
6897 optimize accordingly. */
6898 gcc_assert (TYPE_PTR_P (parmtype));
6899 /* Convert to the base in which the function was declared. */
6900 gcc_assert (cand->conversion_path != NULL_TREE);
6901 converted_arg = build_base_path (PLUS_EXPR,
6902 arg,
6903 cand->conversion_path,
6904 1, complain);
6905 /* Check that the base class is accessible. */
6906 if (!accessible_base_p (TREE_TYPE (argtype),
6907 BINFO_TYPE (cand->conversion_path), true))
6909 if (complain & tf_error)
6910 error ("%qT is not an accessible base of %qT",
6911 BINFO_TYPE (cand->conversion_path),
6912 TREE_TYPE (argtype));
6913 else
6914 return error_mark_node;
6916 /* If fn was found by a using declaration, the conversion path
6917 will be to the derived class, not the base declaring fn. We
6918 must convert from derived to base. */
6919 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
6920 TREE_TYPE (parmtype), ba_unique,
6921 NULL, complain);
6922 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
6923 base_binfo, 1, complain);
6925 argarray[j++] = converted_arg;
6926 parm = TREE_CHAIN (parm);
6927 if (first_arg != NULL_TREE)
6928 first_arg = NULL_TREE;
6929 else
6930 ++arg_index;
6931 ++i;
6932 is_method = 1;
6935 gcc_assert (first_arg == NULL_TREE);
6936 for (; arg_index < vec_safe_length (args) && parm;
6937 parm = TREE_CHAIN (parm), ++arg_index, ++i)
6939 tree type = TREE_VALUE (parm);
6940 tree arg = (*args)[arg_index];
6941 bool conversion_warning = true;
6943 conv = convs[i];
6945 /* If the argument is NULL and used to (implicitly) instantiate a
6946 template function (and bind one of the template arguments to
6947 the type of 'long int'), we don't want to warn about passing NULL
6948 to non-pointer argument.
6949 For example, if we have this template function:
6951 template<typename T> void func(T x) {}
6953 we want to warn (when -Wconversion is enabled) in this case:
6955 void foo() {
6956 func<int>(NULL);
6959 but not in this case:
6961 void foo() {
6962 func(NULL);
6965 if (arg == null_node
6966 && DECL_TEMPLATE_INFO (fn)
6967 && cand->template_decl
6968 && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
6969 conversion_warning = false;
6971 /* Warn about initializer_list deduction that isn't currently in the
6972 working draft. */
6973 if (cxx_dialect > cxx98
6974 && flag_deduce_init_list
6975 && cand->template_decl
6976 && is_std_init_list (non_reference (type))
6977 && BRACE_ENCLOSED_INITIALIZER_P (arg))
6979 tree tmpl = TI_TEMPLATE (cand->template_decl);
6980 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
6981 tree patparm = get_pattern_parm (realparm, tmpl);
6982 tree pattype = TREE_TYPE (patparm);
6983 if (PACK_EXPANSION_P (pattype))
6984 pattype = PACK_EXPANSION_PATTERN (pattype);
6985 pattype = non_reference (pattype);
6987 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
6988 && (cand->explicit_targs == NULL_TREE
6989 || (TREE_VEC_LENGTH (cand->explicit_targs)
6990 <= TEMPLATE_TYPE_IDX (pattype))))
6992 pedwarn (input_location, 0, "deducing %qT as %qT",
6993 non_reference (TREE_TYPE (patparm)),
6994 non_reference (type));
6995 pedwarn (input_location, 0, " in call to %q+D", cand->fn);
6996 pedwarn (input_location, 0,
6997 " (you can disable this with -fno-deduce-init-list)");
7000 val = convert_like_with_context (conv, arg, fn, i - is_method,
7001 conversion_warning
7002 ? complain
7003 : complain & (~tf_warning));
7005 val = convert_for_arg_passing (type, val, complain);
7007 if (val == error_mark_node)
7008 return error_mark_node;
7009 else
7010 argarray[j++] = val;
7013 /* Default arguments */
7014 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
7016 if (TREE_VALUE (parm) == error_mark_node)
7017 return error_mark_node;
7018 argarray[j++] = convert_default_arg (TREE_VALUE (parm),
7019 TREE_PURPOSE (parm),
7020 fn, i - is_method,
7021 complain);
7024 /* Ellipsis */
7025 for (; arg_index < vec_safe_length (args); ++arg_index)
7027 tree a = (*args)[arg_index];
7028 if (magic_varargs_p (fn))
7029 /* Do no conversions for magic varargs. */
7030 a = mark_type_use (a);
7031 else
7032 a = convert_arg_to_ellipsis (a, complain);
7033 argarray[j++] = a;
7036 gcc_assert (j <= nargs);
7037 nargs = j;
7039 check_function_arguments (TREE_TYPE (fn), nargs, argarray);
7041 /* Avoid actually calling copy constructors and copy assignment operators,
7042 if possible. */
7044 if (! flag_elide_constructors)
7045 /* Do things the hard way. */;
7046 else if (cand->num_convs == 1
7047 && (DECL_COPY_CONSTRUCTOR_P (fn)
7048 || DECL_MOVE_CONSTRUCTOR_P (fn)))
7050 tree targ;
7051 tree arg = argarray[num_artificial_parms_for (fn)];
7052 tree fa;
7053 bool trivial = trivial_fn_p (fn);
7055 /* Pull out the real argument, disregarding const-correctness. */
7056 targ = arg;
7057 while (CONVERT_EXPR_P (targ)
7058 || TREE_CODE (targ) == NON_LVALUE_EXPR)
7059 targ = TREE_OPERAND (targ, 0);
7060 if (TREE_CODE (targ) == ADDR_EXPR)
7062 targ = TREE_OPERAND (targ, 0);
7063 if (!same_type_ignoring_top_level_qualifiers_p
7064 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
7065 targ = NULL_TREE;
7067 else
7068 targ = NULL_TREE;
7070 if (targ)
7071 arg = targ;
7072 else
7073 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
7075 /* [class.copy]: the copy constructor is implicitly defined even if
7076 the implementation elided its use. */
7077 if (!trivial || DECL_DELETED_FN (fn))
7079 mark_used (fn);
7080 already_used = true;
7083 /* If we're creating a temp and we already have one, don't create a
7084 new one. If we're not creating a temp but we get one, use
7085 INIT_EXPR to collapse the temp into our target. Otherwise, if the
7086 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
7087 temp or an INIT_EXPR otherwise. */
7088 fa = argarray[0];
7089 if (integer_zerop (fa))
7091 if (TREE_CODE (arg) == TARGET_EXPR)
7092 return arg;
7093 else if (trivial)
7094 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
7096 else if (TREE_CODE (arg) == TARGET_EXPR || trivial)
7098 tree to = stabilize_reference (cp_build_indirect_ref (fa, RO_NULL,
7099 complain));
7101 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
7102 return val;
7105 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
7106 && trivial_fn_p (fn)
7107 && !DECL_DELETED_FN (fn))
7109 tree to = stabilize_reference
7110 (cp_build_indirect_ref (argarray[0], RO_NULL, complain));
7111 tree type = TREE_TYPE (to);
7112 tree as_base = CLASSTYPE_AS_BASE (type);
7113 tree arg = argarray[1];
7115 if (is_really_empty_class (type))
7117 /* Avoid copying empty classes. */
7118 val = build2 (COMPOUND_EXPR, void_type_node, to, arg);
7119 TREE_NO_WARNING (val) = 1;
7120 val = build2 (COMPOUND_EXPR, type, val, to);
7121 TREE_NO_WARNING (val) = 1;
7123 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
7125 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
7126 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
7128 else
7130 /* We must only copy the non-tail padding parts. */
7131 tree arg0, arg2, t;
7132 tree array_type, alias_set;
7134 arg2 = TYPE_SIZE_UNIT (as_base);
7135 arg0 = cp_build_addr_expr (to, complain);
7137 array_type = build_array_type (char_type_node,
7138 build_index_type
7139 (size_binop (MINUS_EXPR,
7140 arg2, size_int (1))));
7141 alias_set = build_int_cst (build_pointer_type (type), 0);
7142 t = build2 (MODIFY_EXPR, void_type_node,
7143 build2 (MEM_REF, array_type, arg0, alias_set),
7144 build2 (MEM_REF, array_type, arg, alias_set));
7145 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
7146 TREE_NO_WARNING (val) = 1;
7149 return val;
7151 else if (DECL_DESTRUCTOR_P (fn)
7152 && trivial_fn_p (fn)
7153 && !DECL_DELETED_FN (fn))
7154 return fold_convert (void_type_node, argarray[0]);
7155 /* FIXME handle trivial default constructor, too. */
7157 /* For calls to a multi-versioned function, overload resolution
7158 returns the function with the highest target priority, that is,
7159 the version that will checked for dispatching first. If this
7160 version is inlinable, a direct call to this version can be made
7161 otherwise the call should go through the dispatcher. */
7163 if (DECL_FUNCTION_VERSIONED (fn)
7164 && (current_function_decl == NULL
7165 || !targetm.target_option.can_inline_p (current_function_decl, fn)))
7167 fn = get_function_version_dispatcher (fn);
7168 if (fn == NULL)
7169 return NULL;
7170 if (!already_used)
7171 mark_versions_used (fn);
7174 if (!already_used
7175 && !mark_used (fn))
7176 return error_mark_node;
7178 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
7179 /* Don't mess with virtual lookup in fold_non_dependent_expr; virtual
7180 functions can't be constexpr. */
7181 && !in_template_function ())
7183 tree t;
7184 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
7185 DECL_CONTEXT (fn),
7186 ba_any, NULL, complain);
7187 gcc_assert (binfo && binfo != error_mark_node);
7189 /* Warn about deprecated virtual functions now, since we're about
7190 to throw away the decl. */
7191 if (TREE_DEPRECATED (fn))
7192 warn_deprecated_use (fn, NULL_TREE);
7194 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
7195 complain);
7196 if (TREE_SIDE_EFFECTS (argarray[0]))
7197 argarray[0] = save_expr (argarray[0]);
7198 t = build_pointer_type (TREE_TYPE (fn));
7199 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
7200 fn = build_java_interface_fn_ref (fn, argarray[0]);
7201 else
7202 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
7203 TREE_TYPE (fn) = t;
7205 else
7207 fn = build_addr_func (fn, complain);
7208 if (fn == error_mark_node)
7209 return error_mark_node;
7212 return build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
7215 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
7216 This function performs no overload resolution, conversion, or other
7217 high-level operations. */
7219 tree
7220 build_cxx_call (tree fn, int nargs, tree *argarray,
7221 tsubst_flags_t complain)
7223 tree fndecl;
7224 int optimize_sav;
7226 /* Remember roughly where this call is. */
7227 location_t loc = EXPR_LOC_OR_HERE (fn);
7228 fn = build_call_a (fn, nargs, argarray);
7229 SET_EXPR_LOCATION (fn, loc);
7231 fndecl = get_callee_fndecl (fn);
7233 /* Check that arguments to builtin functions match the expectations. */
7234 if (fndecl
7235 && DECL_BUILT_IN (fndecl)
7236 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7237 && !check_builtin_function_arguments (fndecl, nargs, argarray))
7238 return error_mark_node;
7240 /* If it is a built-in array notation function, then the return type of
7241 the function is the element type of the array passed in as array
7242 notation (i.e. the first parameter of the function). */
7243 if (flag_enable_cilkplus && TREE_CODE (fn) == CALL_EXPR)
7245 enum built_in_function bif =
7246 is_cilkplus_reduce_builtin (CALL_EXPR_FN (fn));
7247 if (bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
7248 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
7249 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
7250 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
7251 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE
7252 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
7254 /* for bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO or
7255 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO or
7256 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO or
7257 BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO or
7258 BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND or
7259 BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
7260 The pre-defined return-type is the correct one. */
7261 tree array_ntn = CALL_EXPR_ARG (fn, 0);
7262 TREE_TYPE (fn) = TREE_TYPE (array_ntn);
7263 return fn;
7267 /* Some built-in function calls will be evaluated at compile-time in
7268 fold (). Set optimize to 1 when folding __builtin_constant_p inside
7269 a constexpr function so that fold_builtin_1 doesn't fold it to 0. */
7270 optimize_sav = optimize;
7271 if (!optimize && fndecl && DECL_IS_BUILTIN_CONSTANT_P (fndecl)
7272 && current_function_decl
7273 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
7274 optimize = 1;
7275 fn = fold_if_not_in_template (fn);
7276 optimize = optimize_sav;
7278 if (VOID_TYPE_P (TREE_TYPE (fn)))
7279 return fn;
7281 /* 5.2.2/11: If a function call is a prvalue of object type: if the
7282 function call is either the operand of a decltype-specifier or the
7283 right operand of a comma operator that is the operand of a
7284 decltype-specifier, a temporary object is not introduced for the
7285 prvalue. The type of the prvalue may be incomplete. */
7286 if (!(complain & tf_decltype))
7288 fn = require_complete_type_sfinae (fn, complain);
7289 if (fn == error_mark_node)
7290 return error_mark_node;
7292 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
7293 fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
7295 return convert_from_reference (fn);
7298 static GTY(()) tree java_iface_lookup_fn;
7300 /* Make an expression which yields the address of the Java interface
7301 method FN. This is achieved by generating a call to libjava's
7302 _Jv_LookupInterfaceMethodIdx(). */
7304 static tree
7305 build_java_interface_fn_ref (tree fn, tree instance)
7307 tree lookup_fn, method, idx;
7308 tree klass_ref, iface, iface_ref;
7309 int i;
7311 if (!java_iface_lookup_fn)
7313 tree ftype = build_function_type_list (ptr_type_node,
7314 ptr_type_node, ptr_type_node,
7315 java_int_type_node, NULL_TREE);
7316 java_iface_lookup_fn
7317 = add_builtin_function ("_Jv_LookupInterfaceMethodIdx", ftype,
7318 0, NOT_BUILT_IN, NULL, NULL_TREE);
7321 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
7322 This is the first entry in the vtable. */
7323 klass_ref = build_vtbl_ref (cp_build_indirect_ref (instance, RO_NULL,
7324 tf_warning_or_error),
7325 integer_zero_node);
7327 /* Get the java.lang.Class pointer for the interface being called. */
7328 iface = DECL_CONTEXT (fn);
7329 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
7330 if (!iface_ref || !VAR_P (iface_ref)
7331 || DECL_CONTEXT (iface_ref) != iface)
7333 error ("could not find class$ field in java interface type %qT",
7334 iface);
7335 return error_mark_node;
7337 iface_ref = build_address (iface_ref);
7338 iface_ref = convert (build_pointer_type (iface), iface_ref);
7340 /* Determine the itable index of FN. */
7341 i = 1;
7342 for (method = TYPE_METHODS (iface); method; method = DECL_CHAIN (method))
7344 if (!DECL_VIRTUAL_P (method))
7345 continue;
7346 if (fn == method)
7347 break;
7348 i++;
7350 idx = build_int_cst (NULL_TREE, i);
7352 lookup_fn = build1 (ADDR_EXPR,
7353 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
7354 java_iface_lookup_fn);
7355 return build_call_nary (ptr_type_node, lookup_fn,
7356 3, klass_ref, iface_ref, idx);
7359 /* Returns the value to use for the in-charge parameter when making a
7360 call to a function with the indicated NAME.
7362 FIXME:Can't we find a neater way to do this mapping? */
7364 tree
7365 in_charge_arg_for_name (tree name)
7367 if (name == base_ctor_identifier
7368 || name == base_dtor_identifier)
7369 return integer_zero_node;
7370 else if (name == complete_ctor_identifier)
7371 return integer_one_node;
7372 else if (name == complete_dtor_identifier)
7373 return integer_two_node;
7374 else if (name == deleting_dtor_identifier)
7375 return integer_three_node;
7377 /* This function should only be called with one of the names listed
7378 above. */
7379 gcc_unreachable ();
7380 return NULL_TREE;
7383 /* Build a call to a constructor, destructor, or an assignment
7384 operator for INSTANCE, an expression with class type. NAME
7385 indicates the special member function to call; *ARGS are the
7386 arguments. ARGS may be NULL. This may change ARGS. BINFO
7387 indicates the base of INSTANCE that is to be passed as the `this'
7388 parameter to the member function called.
7390 FLAGS are the LOOKUP_* flags to use when processing the call.
7392 If NAME indicates a complete object constructor, INSTANCE may be
7393 NULL_TREE. In this case, the caller will call build_cplus_new to
7394 store the newly constructed object into a VAR_DECL. */
7396 tree
7397 build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
7398 tree binfo, int flags, tsubst_flags_t complain)
7400 tree fns;
7401 /* The type of the subobject to be constructed or destroyed. */
7402 tree class_type;
7403 vec<tree, va_gc> *allocated = NULL;
7404 tree ret;
7406 gcc_assert (name == complete_ctor_identifier
7407 || name == base_ctor_identifier
7408 || name == complete_dtor_identifier
7409 || name == base_dtor_identifier
7410 || name == deleting_dtor_identifier
7411 || name == ansi_assopname (NOP_EXPR));
7412 if (TYPE_P (binfo))
7414 /* Resolve the name. */
7415 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
7416 return error_mark_node;
7418 binfo = TYPE_BINFO (binfo);
7421 gcc_assert (binfo != NULL_TREE);
7423 class_type = BINFO_TYPE (binfo);
7425 /* Handle the special case where INSTANCE is NULL_TREE. */
7426 if (name == complete_ctor_identifier && !instance)
7428 instance = build_int_cst (build_pointer_type (class_type), 0);
7429 instance = build1 (INDIRECT_REF, class_type, instance);
7431 else
7433 if (name == complete_dtor_identifier
7434 || name == base_dtor_identifier
7435 || name == deleting_dtor_identifier)
7436 gcc_assert (args == NULL || vec_safe_is_empty (*args));
7438 /* Convert to the base class, if necessary. */
7439 if (!same_type_ignoring_top_level_qualifiers_p
7440 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
7442 if (name != ansi_assopname (NOP_EXPR))
7443 /* For constructors and destructors, either the base is
7444 non-virtual, or it is virtual but we are doing the
7445 conversion from a constructor or destructor for the
7446 complete object. In either case, we can convert
7447 statically. */
7448 instance = convert_to_base_statically (instance, binfo);
7449 else
7450 /* However, for assignment operators, we must convert
7451 dynamically if the base is virtual. */
7452 instance = build_base_path (PLUS_EXPR, instance,
7453 binfo, /*nonnull=*/1, complain);
7457 gcc_assert (instance != NULL_TREE);
7459 fns = lookup_fnfields (binfo, name, 1);
7461 /* When making a call to a constructor or destructor for a subobject
7462 that uses virtual base classes, pass down a pointer to a VTT for
7463 the subobject. */
7464 if ((name == base_ctor_identifier
7465 || name == base_dtor_identifier)
7466 && CLASSTYPE_VBASECLASSES (class_type))
7468 tree vtt;
7469 tree sub_vtt;
7471 /* If the current function is a complete object constructor
7472 or destructor, then we fetch the VTT directly.
7473 Otherwise, we look it up using the VTT we were given. */
7474 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
7475 vtt = decay_conversion (vtt, complain);
7476 if (vtt == error_mark_node)
7477 return error_mark_node;
7478 vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
7479 build2 (EQ_EXPR, boolean_type_node,
7480 current_in_charge_parm, integer_zero_node),
7481 current_vtt_parm,
7482 vtt);
7483 if (BINFO_SUBVTT_INDEX (binfo))
7484 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
7485 else
7486 sub_vtt = vtt;
7488 if (args == NULL)
7490 allocated = make_tree_vector ();
7491 args = &allocated;
7494 vec_safe_insert (*args, 0, sub_vtt);
7497 ret = build_new_method_call (instance, fns, args,
7498 TYPE_BINFO (BINFO_TYPE (binfo)),
7499 flags, /*fn=*/NULL,
7500 complain);
7502 if (allocated != NULL)
7503 release_tree_vector (allocated);
7505 if ((complain & tf_error)
7506 && (flags & LOOKUP_DELEGATING_CONS)
7507 && name == complete_ctor_identifier
7508 && TREE_CODE (ret) == CALL_EXPR
7509 && (DECL_ABSTRACT_ORIGIN (TREE_OPERAND (CALL_EXPR_FN (ret), 0))
7510 == current_function_decl))
7511 error ("constructor delegates to itself");
7513 return ret;
7516 /* Return the NAME, as a C string. The NAME indicates a function that
7517 is a member of TYPE. *FREE_P is set to true if the caller must
7518 free the memory returned.
7520 Rather than go through all of this, we should simply set the names
7521 of constructors and destructors appropriately, and dispense with
7522 ctor_identifier, dtor_identifier, etc. */
7524 static char *
7525 name_as_c_string (tree name, tree type, bool *free_p)
7527 char *pretty_name;
7529 /* Assume that we will not allocate memory. */
7530 *free_p = false;
7531 /* Constructors and destructors are special. */
7532 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
7534 pretty_name
7535 = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type))));
7536 /* For a destructor, add the '~'. */
7537 if (name == complete_dtor_identifier
7538 || name == base_dtor_identifier
7539 || name == deleting_dtor_identifier)
7541 pretty_name = concat ("~", pretty_name, NULL);
7542 /* Remember that we need to free the memory allocated. */
7543 *free_p = true;
7546 else if (IDENTIFIER_TYPENAME_P (name))
7548 pretty_name = concat ("operator ",
7549 type_as_string_translate (TREE_TYPE (name),
7550 TFF_PLAIN_IDENTIFIER),
7551 NULL);
7552 /* Remember that we need to free the memory allocated. */
7553 *free_p = true;
7555 else
7556 pretty_name = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (name)));
7558 return pretty_name;
7561 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
7562 be set, upon return, to the function called. ARGS may be NULL.
7563 This may change ARGS. */
7565 static tree
7566 build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
7567 tree conversion_path, int flags,
7568 tree *fn_p, tsubst_flags_t complain)
7570 struct z_candidate *candidates = 0, *cand;
7571 tree explicit_targs = NULL_TREE;
7572 tree basetype = NULL_TREE;
7573 tree access_binfo;
7574 tree optype;
7575 tree first_mem_arg = NULL_TREE;
7576 tree name;
7577 bool skip_first_for_error;
7578 vec<tree, va_gc> *user_args;
7579 tree call;
7580 tree fn;
7581 int template_only = 0;
7582 bool any_viable_p;
7583 tree orig_instance;
7584 tree orig_fns;
7585 vec<tree, va_gc> *orig_args = NULL;
7586 void *p;
7588 gcc_assert (instance != NULL_TREE);
7590 /* We don't know what function we're going to call, yet. */
7591 if (fn_p)
7592 *fn_p = NULL_TREE;
7594 if (error_operand_p (instance)
7595 || !fns || error_operand_p (fns))
7596 return error_mark_node;
7598 if (!BASELINK_P (fns))
7600 if (complain & tf_error)
7601 error ("call to non-function %qD", fns);
7602 return error_mark_node;
7605 orig_instance = instance;
7606 orig_fns = fns;
7608 /* Dismantle the baselink to collect all the information we need. */
7609 if (!conversion_path)
7610 conversion_path = BASELINK_BINFO (fns);
7611 access_binfo = BASELINK_ACCESS_BINFO (fns);
7612 optype = BASELINK_OPTYPE (fns);
7613 fns = BASELINK_FUNCTIONS (fns);
7614 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7616 explicit_targs = TREE_OPERAND (fns, 1);
7617 fns = TREE_OPERAND (fns, 0);
7618 template_only = 1;
7620 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
7621 || TREE_CODE (fns) == TEMPLATE_DECL
7622 || TREE_CODE (fns) == OVERLOAD);
7623 fn = get_first_fn (fns);
7624 name = DECL_NAME (fn);
7626 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
7627 gcc_assert (CLASS_TYPE_P (basetype));
7629 if (processing_template_decl)
7631 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
7632 instance = build_non_dependent_expr (instance);
7633 if (args != NULL)
7634 make_args_non_dependent (*args);
7637 user_args = args == NULL ? NULL : *args;
7638 /* Under DR 147 A::A() is an invalid constructor call,
7639 not a functional cast. */
7640 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
7642 if (! (complain & tf_error))
7643 return error_mark_node;
7645 if (permerror (input_location,
7646 "cannot call constructor %<%T::%D%> directly",
7647 basetype, name))
7648 inform (input_location, "for a function-style cast, remove the "
7649 "redundant %<::%D%>", name);
7650 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
7651 complain);
7652 return call;
7655 /* Figure out whether to skip the first argument for the error
7656 message we will display to users if an error occurs. We don't
7657 want to display any compiler-generated arguments. The "this"
7658 pointer hasn't been added yet. However, we must remove the VTT
7659 pointer if this is a call to a base-class constructor or
7660 destructor. */
7661 skip_first_for_error = false;
7662 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
7664 /* Callers should explicitly indicate whether they want to construct
7665 the complete object or just the part without virtual bases. */
7666 gcc_assert (name != ctor_identifier);
7667 /* Similarly for destructors. */
7668 gcc_assert (name != dtor_identifier);
7669 /* Remove the VTT pointer, if present. */
7670 if ((name == base_ctor_identifier || name == base_dtor_identifier)
7671 && CLASSTYPE_VBASECLASSES (basetype))
7672 skip_first_for_error = true;
7675 /* Process the argument list. */
7676 if (args != NULL && *args != NULL)
7678 *args = resolve_args (*args, complain);
7679 if (*args == NULL)
7680 return error_mark_node;
7683 /* Consider the object argument to be used even if we end up selecting a
7684 static member function. */
7685 instance = mark_type_use (instance);
7687 /* It's OK to call destructors and constructors on cv-qualified objects.
7688 Therefore, convert the INSTANCE to the unqualified type, if
7689 necessary. */
7690 if (DECL_DESTRUCTOR_P (fn)
7691 || DECL_CONSTRUCTOR_P (fn))
7693 if (!same_type_p (basetype, TREE_TYPE (instance)))
7695 instance = build_this (instance);
7696 instance = build_nop (build_pointer_type (basetype), instance);
7697 instance = build_fold_indirect_ref (instance);
7700 if (DECL_DESTRUCTOR_P (fn))
7701 name = complete_dtor_identifier;
7703 first_mem_arg = instance;
7705 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7706 p = conversion_obstack_alloc (0);
7708 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
7709 initializer, not T({ }). */
7710 if (DECL_CONSTRUCTOR_P (fn) && args != NULL && !vec_safe_is_empty (*args)
7711 && BRACE_ENCLOSED_INITIALIZER_P ((**args)[0])
7712 && CONSTRUCTOR_IS_DIRECT_INIT ((**args)[0]))
7714 tree init_list = (**args)[0];
7715 tree init = NULL_TREE;
7717 gcc_assert ((*args)->length () == 1
7718 && !(flags & LOOKUP_ONLYCONVERTING));
7720 /* If the initializer list has no elements and T is a class type with
7721 a default constructor, the object is value-initialized. Handle
7722 this here so we don't need to handle it wherever we use
7723 build_special_member_call. */
7724 if (CONSTRUCTOR_NELTS (init_list) == 0
7725 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
7726 /* For a user-provided default constructor, use the normal
7727 mechanisms so that protected access works. */
7728 && !type_has_user_provided_default_constructor (basetype)
7729 && !processing_template_decl)
7730 init = build_value_init (basetype, complain);
7732 /* If BASETYPE is an aggregate, we need to do aggregate
7733 initialization. */
7734 else if (CP_AGGREGATE_TYPE_P (basetype))
7735 init = digest_init (basetype, init_list, complain);
7737 if (init)
7739 if (INDIRECT_REF_P (instance)
7740 && integer_zerop (TREE_OPERAND (instance, 0)))
7741 return get_target_expr_sfinae (init, complain);
7742 init = build2 (INIT_EXPR, TREE_TYPE (instance), instance, init);
7743 TREE_SIDE_EFFECTS (init) = true;
7744 return init;
7747 /* Otherwise go ahead with overload resolution. */
7748 add_list_candidates (fns, first_mem_arg, init_list,
7749 basetype, explicit_targs, template_only,
7750 conversion_path, access_binfo, flags,
7751 &candidates, complain);
7753 else
7755 add_candidates (fns, first_mem_arg, user_args, optype,
7756 explicit_targs, template_only, conversion_path,
7757 access_binfo, flags, &candidates, complain);
7759 any_viable_p = false;
7760 candidates = splice_viable (candidates, pedantic, &any_viable_p);
7762 if (!any_viable_p)
7764 if (complain & tf_error)
7766 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
7767 cxx_incomplete_type_error (instance, basetype);
7768 else if (optype)
7769 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
7770 basetype, optype, build_tree_list_vec (user_args),
7771 TREE_TYPE (instance));
7772 else
7774 char *pretty_name;
7775 bool free_p;
7776 tree arglist;
7778 pretty_name = name_as_c_string (name, basetype, &free_p);
7779 arglist = build_tree_list_vec (user_args);
7780 if (skip_first_for_error)
7781 arglist = TREE_CHAIN (arglist);
7782 error ("no matching function for call to %<%T::%s(%A)%#V%>",
7783 basetype, pretty_name, arglist,
7784 TREE_TYPE (instance));
7785 if (free_p)
7786 free (pretty_name);
7788 print_z_candidates (location_of (name), candidates);
7790 call = error_mark_node;
7792 else
7794 cand = tourney (candidates, complain);
7795 if (cand == 0)
7797 char *pretty_name;
7798 bool free_p;
7799 tree arglist;
7801 if (complain & tf_error)
7803 pretty_name = name_as_c_string (name, basetype, &free_p);
7804 arglist = build_tree_list_vec (user_args);
7805 if (skip_first_for_error)
7806 arglist = TREE_CHAIN (arglist);
7807 error ("call of overloaded %<%s(%A)%> is ambiguous", pretty_name,
7808 arglist);
7809 print_z_candidates (location_of (name), candidates);
7810 if (free_p)
7811 free (pretty_name);
7813 call = error_mark_node;
7815 else
7817 fn = cand->fn;
7818 call = NULL_TREE;
7820 if (!(flags & LOOKUP_NONVIRTUAL)
7821 && DECL_PURE_VIRTUAL_P (fn)
7822 && instance == current_class_ref
7823 && (DECL_CONSTRUCTOR_P (current_function_decl)
7824 || DECL_DESTRUCTOR_P (current_function_decl))
7825 && (complain & tf_warning))
7826 /* This is not an error, it is runtime undefined
7827 behavior. */
7828 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl) ?
7829 "pure virtual %q#D called from constructor"
7830 : "pure virtual %q#D called from destructor"),
7831 fn);
7833 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
7834 && is_dummy_object (instance))
7836 instance = maybe_resolve_dummy (instance);
7837 if (instance == error_mark_node)
7838 call = error_mark_node;
7839 else if (!is_dummy_object (instance))
7841 /* We captured 'this' in the current lambda now that
7842 we know we really need it. */
7843 cand->first_arg = instance;
7845 else
7847 if (complain & tf_error)
7848 error ("cannot call member function %qD without object",
7849 fn);
7850 call = error_mark_node;
7854 if (call != error_mark_node)
7856 /* Optimize away vtable lookup if we know that this
7857 function can't be overridden. We need to check if
7858 the context and the instance type are the same,
7859 actually FN might be defined in a different class
7860 type because of a using-declaration. In this case, we
7861 do not want to perform a non-virtual call. */
7862 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
7863 && same_type_ignoring_top_level_qualifiers_p
7864 (DECL_CONTEXT (fn), TREE_TYPE (instance))
7865 && resolves_to_fixed_type_p (instance, 0))
7866 flags |= LOOKUP_NONVIRTUAL;
7867 if (explicit_targs)
7868 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
7869 /* Now we know what function is being called. */
7870 if (fn_p)
7871 *fn_p = fn;
7872 /* Build the actual CALL_EXPR. */
7873 call = build_over_call (cand, flags, complain);
7874 /* In an expression of the form `a->f()' where `f' turns
7875 out to be a static member function, `a' is
7876 none-the-less evaluated. */
7877 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
7878 && !is_dummy_object (instance)
7879 && TREE_SIDE_EFFECTS (instance))
7880 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
7881 instance, call);
7882 else if (call != error_mark_node
7883 && DECL_DESTRUCTOR_P (cand->fn)
7884 && !VOID_TYPE_P (TREE_TYPE (call)))
7885 /* An explicit call of the form "x->~X()" has type
7886 "void". However, on platforms where destructors
7887 return "this" (i.e., those where
7888 targetm.cxx.cdtor_returns_this is true), such calls
7889 will appear to have a return value of pointer type
7890 to the low-level call machinery. We do not want to
7891 change the low-level machinery, since we want to be
7892 able to optimize "delete f()" on such platforms as
7893 "operator delete(~X(f()))" (rather than generating
7894 "t = f(), ~X(t), operator delete (t)"). */
7895 call = build_nop (void_type_node, call);
7900 if (processing_template_decl && call != error_mark_node)
7902 bool cast_to_void = false;
7904 if (TREE_CODE (call) == COMPOUND_EXPR)
7905 call = TREE_OPERAND (call, 1);
7906 else if (TREE_CODE (call) == NOP_EXPR)
7908 cast_to_void = true;
7909 call = TREE_OPERAND (call, 0);
7911 if (INDIRECT_REF_P (call))
7912 call = TREE_OPERAND (call, 0);
7913 call = (build_min_non_dep_call_vec
7914 (call,
7915 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
7916 orig_instance, orig_fns, NULL_TREE),
7917 orig_args));
7918 SET_EXPR_LOCATION (call, input_location);
7919 call = convert_from_reference (call);
7920 if (cast_to_void)
7921 call = build_nop (void_type_node, call);
7924 /* Free all the conversions we allocated. */
7925 obstack_free (&conversion_obstack, p);
7927 if (orig_args != NULL)
7928 release_tree_vector (orig_args);
7930 return call;
7933 /* Wrapper for above. */
7935 tree
7936 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
7937 tree conversion_path, int flags,
7938 tree *fn_p, tsubst_flags_t complain)
7940 tree ret;
7941 bool subtime = timevar_cond_start (TV_OVERLOAD);
7942 ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
7943 fn_p, complain);
7944 timevar_cond_stop (TV_OVERLOAD, subtime);
7945 return ret;
7948 /* Returns true iff standard conversion sequence ICS1 is a proper
7949 subsequence of ICS2. */
7951 static bool
7952 is_subseq (conversion *ics1, conversion *ics2)
7954 /* We can assume that a conversion of the same code
7955 between the same types indicates a subsequence since we only get
7956 here if the types we are converting from are the same. */
7958 while (ics1->kind == ck_rvalue
7959 || ics1->kind == ck_lvalue)
7960 ics1 = next_conversion (ics1);
7962 while (1)
7964 while (ics2->kind == ck_rvalue
7965 || ics2->kind == ck_lvalue)
7966 ics2 = next_conversion (ics2);
7968 if (ics2->kind == ck_user
7969 || ics2->kind == ck_ambig
7970 || ics2->kind == ck_aggr
7971 || ics2->kind == ck_list
7972 || ics2->kind == ck_identity)
7973 /* At this point, ICS1 cannot be a proper subsequence of
7974 ICS2. We can get a USER_CONV when we are comparing the
7975 second standard conversion sequence of two user conversion
7976 sequences. */
7977 return false;
7979 ics2 = next_conversion (ics2);
7981 if (ics2->kind == ics1->kind
7982 && same_type_p (ics2->type, ics1->type)
7983 && same_type_p (next_conversion (ics2)->type,
7984 next_conversion (ics1)->type))
7985 return true;
7989 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
7990 be any _TYPE nodes. */
7992 bool
7993 is_properly_derived_from (tree derived, tree base)
7995 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
7996 return false;
7998 /* We only allow proper derivation here. The DERIVED_FROM_P macro
7999 considers every class derived from itself. */
8000 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
8001 && DERIVED_FROM_P (base, derived));
8004 /* We build the ICS for an implicit object parameter as a pointer
8005 conversion sequence. However, such a sequence should be compared
8006 as if it were a reference conversion sequence. If ICS is the
8007 implicit conversion sequence for an implicit object parameter,
8008 modify it accordingly. */
8010 static void
8011 maybe_handle_implicit_object (conversion **ics)
8013 if ((*ics)->this_p)
8015 /* [over.match.funcs]
8017 For non-static member functions, the type of the
8018 implicit object parameter is "reference to cv X"
8019 where X is the class of which the function is a
8020 member and cv is the cv-qualification on the member
8021 function declaration. */
8022 conversion *t = *ics;
8023 tree reference_type;
8025 /* The `this' parameter is a pointer to a class type. Make the
8026 implicit conversion talk about a reference to that same class
8027 type. */
8028 reference_type = TREE_TYPE (t->type);
8029 reference_type = build_reference_type (reference_type);
8031 if (t->kind == ck_qual)
8032 t = next_conversion (t);
8033 if (t->kind == ck_ptr)
8034 t = next_conversion (t);
8035 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
8036 t = direct_reference_binding (reference_type, t);
8037 t->this_p = 1;
8038 t->rvaluedness_matches_p = 0;
8039 *ics = t;
8043 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
8044 and return the initial reference binding conversion. Otherwise,
8045 leave *ICS unchanged and return NULL. */
8047 static conversion *
8048 maybe_handle_ref_bind (conversion **ics)
8050 if ((*ics)->kind == ck_ref_bind)
8052 conversion *old_ics = *ics;
8053 *ics = next_conversion (old_ics);
8054 (*ics)->user_conv_p = old_ics->user_conv_p;
8055 return old_ics;
8058 return NULL;
8061 /* Compare two implicit conversion sequences according to the rules set out in
8062 [over.ics.rank]. Return values:
8064 1: ics1 is better than ics2
8065 -1: ics2 is better than ics1
8066 0: ics1 and ics2 are indistinguishable */
8068 static int
8069 compare_ics (conversion *ics1, conversion *ics2)
8071 tree from_type1;
8072 tree from_type2;
8073 tree to_type1;
8074 tree to_type2;
8075 tree deref_from_type1 = NULL_TREE;
8076 tree deref_from_type2 = NULL_TREE;
8077 tree deref_to_type1 = NULL_TREE;
8078 tree deref_to_type2 = NULL_TREE;
8079 conversion_rank rank1, rank2;
8081 /* REF_BINDING is nonzero if the result of the conversion sequence
8082 is a reference type. In that case REF_CONV is the reference
8083 binding conversion. */
8084 conversion *ref_conv1;
8085 conversion *ref_conv2;
8087 /* Handle implicit object parameters. */
8088 maybe_handle_implicit_object (&ics1);
8089 maybe_handle_implicit_object (&ics2);
8091 /* Handle reference parameters. */
8092 ref_conv1 = maybe_handle_ref_bind (&ics1);
8093 ref_conv2 = maybe_handle_ref_bind (&ics2);
8095 /* List-initialization sequence L1 is a better conversion sequence than
8096 list-initialization sequence L2 if L1 converts to
8097 std::initializer_list<X> for some X and L2 does not. */
8098 if (ics1->kind == ck_list && ics2->kind != ck_list)
8099 return 1;
8100 if (ics2->kind == ck_list && ics1->kind != ck_list)
8101 return -1;
8103 /* [over.ics.rank]
8105 When comparing the basic forms of implicit conversion sequences (as
8106 defined in _over.best.ics_)
8108 --a standard conversion sequence (_over.ics.scs_) is a better
8109 conversion sequence than a user-defined conversion sequence
8110 or an ellipsis conversion sequence, and
8112 --a user-defined conversion sequence (_over.ics.user_) is a
8113 better conversion sequence than an ellipsis conversion sequence
8114 (_over.ics.ellipsis_). */
8115 rank1 = CONVERSION_RANK (ics1);
8116 rank2 = CONVERSION_RANK (ics2);
8118 if (rank1 > rank2)
8119 return -1;
8120 else if (rank1 < rank2)
8121 return 1;
8123 if (rank1 == cr_bad)
8125 /* Both ICS are bad. We try to make a decision based on what would
8126 have happened if they'd been good. This is not an extension,
8127 we'll still give an error when we build up the call; this just
8128 helps us give a more helpful error message. */
8129 rank1 = BAD_CONVERSION_RANK (ics1);
8130 rank2 = BAD_CONVERSION_RANK (ics2);
8132 if (rank1 > rank2)
8133 return -1;
8134 else if (rank1 < rank2)
8135 return 1;
8137 /* We couldn't make up our minds; try to figure it out below. */
8140 if (ics1->ellipsis_p)
8141 /* Both conversions are ellipsis conversions. */
8142 return 0;
8144 /* User-defined conversion sequence U1 is a better conversion sequence
8145 than another user-defined conversion sequence U2 if they contain the
8146 same user-defined conversion operator or constructor and if the sec-
8147 ond standard conversion sequence of U1 is better than the second
8148 standard conversion sequence of U2. */
8150 /* Handle list-conversion with the same code even though it isn't always
8151 ranked as a user-defined conversion and it doesn't have a second
8152 standard conversion sequence; it will still have the desired effect.
8153 Specifically, we need to do the reference binding comparison at the
8154 end of this function. */
8156 if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
8158 conversion *t1;
8159 conversion *t2;
8161 for (t1 = ics1; t1->kind != ck_user; t1 = next_conversion (t1))
8162 if (t1->kind == ck_ambig || t1->kind == ck_aggr
8163 || t1->kind == ck_list)
8164 break;
8165 for (t2 = ics2; t2->kind != ck_user; t2 = next_conversion (t2))
8166 if (t2->kind == ck_ambig || t2->kind == ck_aggr
8167 || t2->kind == ck_list)
8168 break;
8170 if (t1->kind != t2->kind)
8171 return 0;
8172 else if (t1->kind == ck_user)
8174 if (t1->cand->fn != t2->cand->fn)
8175 return 0;
8177 else
8179 /* For ambiguous or aggregate conversions, use the target type as
8180 a proxy for the conversion function. */
8181 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
8182 return 0;
8185 /* We can just fall through here, after setting up
8186 FROM_TYPE1 and FROM_TYPE2. */
8187 from_type1 = t1->type;
8188 from_type2 = t2->type;
8190 else
8192 conversion *t1;
8193 conversion *t2;
8195 /* We're dealing with two standard conversion sequences.
8197 [over.ics.rank]
8199 Standard conversion sequence S1 is a better conversion
8200 sequence than standard conversion sequence S2 if
8202 --S1 is a proper subsequence of S2 (comparing the conversion
8203 sequences in the canonical form defined by _over.ics.scs_,
8204 excluding any Lvalue Transformation; the identity
8205 conversion sequence is considered to be a subsequence of
8206 any non-identity conversion sequence */
8208 t1 = ics1;
8209 while (t1->kind != ck_identity)
8210 t1 = next_conversion (t1);
8211 from_type1 = t1->type;
8213 t2 = ics2;
8214 while (t2->kind != ck_identity)
8215 t2 = next_conversion (t2);
8216 from_type2 = t2->type;
8219 /* One sequence can only be a subsequence of the other if they start with
8220 the same type. They can start with different types when comparing the
8221 second standard conversion sequence in two user-defined conversion
8222 sequences. */
8223 if (same_type_p (from_type1, from_type2))
8225 if (is_subseq (ics1, ics2))
8226 return 1;
8227 if (is_subseq (ics2, ics1))
8228 return -1;
8231 /* [over.ics.rank]
8233 Or, if not that,
8235 --the rank of S1 is better than the rank of S2 (by the rules
8236 defined below):
8238 Standard conversion sequences are ordered by their ranks: an Exact
8239 Match is a better conversion than a Promotion, which is a better
8240 conversion than a Conversion.
8242 Two conversion sequences with the same rank are indistinguishable
8243 unless one of the following rules applies:
8245 --A conversion that does not a convert a pointer, pointer to member,
8246 or std::nullptr_t to bool is better than one that does.
8248 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
8249 so that we do not have to check it explicitly. */
8250 if (ics1->rank < ics2->rank)
8251 return 1;
8252 else if (ics2->rank < ics1->rank)
8253 return -1;
8255 to_type1 = ics1->type;
8256 to_type2 = ics2->type;
8258 /* A conversion from scalar arithmetic type to complex is worse than a
8259 conversion between scalar arithmetic types. */
8260 if (same_type_p (from_type1, from_type2)
8261 && ARITHMETIC_TYPE_P (from_type1)
8262 && ARITHMETIC_TYPE_P (to_type1)
8263 && ARITHMETIC_TYPE_P (to_type2)
8264 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
8265 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
8267 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
8268 return -1;
8269 else
8270 return 1;
8273 if (TYPE_PTR_P (from_type1)
8274 && TYPE_PTR_P (from_type2)
8275 && TYPE_PTR_P (to_type1)
8276 && TYPE_PTR_P (to_type2))
8278 deref_from_type1 = TREE_TYPE (from_type1);
8279 deref_from_type2 = TREE_TYPE (from_type2);
8280 deref_to_type1 = TREE_TYPE (to_type1);
8281 deref_to_type2 = TREE_TYPE (to_type2);
8283 /* The rules for pointers to members A::* are just like the rules
8284 for pointers A*, except opposite: if B is derived from A then
8285 A::* converts to B::*, not vice versa. For that reason, we
8286 switch the from_ and to_ variables here. */
8287 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
8288 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
8289 || (TYPE_PTRMEMFUNC_P (from_type1)
8290 && TYPE_PTRMEMFUNC_P (from_type2)
8291 && TYPE_PTRMEMFUNC_P (to_type1)
8292 && TYPE_PTRMEMFUNC_P (to_type2)))
8294 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
8295 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
8296 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
8297 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
8300 if (deref_from_type1 != NULL_TREE
8301 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
8302 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
8304 /* This was one of the pointer or pointer-like conversions.
8306 [over.ics.rank]
8308 --If class B is derived directly or indirectly from class A,
8309 conversion of B* to A* is better than conversion of B* to
8310 void*, and conversion of A* to void* is better than
8311 conversion of B* to void*. */
8312 if (VOID_TYPE_P (deref_to_type1)
8313 && VOID_TYPE_P (deref_to_type2))
8315 if (is_properly_derived_from (deref_from_type1,
8316 deref_from_type2))
8317 return -1;
8318 else if (is_properly_derived_from (deref_from_type2,
8319 deref_from_type1))
8320 return 1;
8322 else if (VOID_TYPE_P (deref_to_type1)
8323 || VOID_TYPE_P (deref_to_type2))
8325 if (same_type_p (deref_from_type1, deref_from_type2))
8327 if (VOID_TYPE_P (deref_to_type2))
8329 if (is_properly_derived_from (deref_from_type1,
8330 deref_to_type1))
8331 return 1;
8333 /* We know that DEREF_TO_TYPE1 is `void' here. */
8334 else if (is_properly_derived_from (deref_from_type1,
8335 deref_to_type2))
8336 return -1;
8339 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
8340 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
8342 /* [over.ics.rank]
8344 --If class B is derived directly or indirectly from class A
8345 and class C is derived directly or indirectly from B,
8347 --conversion of C* to B* is better than conversion of C* to
8350 --conversion of B* to A* is better than conversion of C* to
8351 A* */
8352 if (same_type_p (deref_from_type1, deref_from_type2))
8354 if (is_properly_derived_from (deref_to_type1,
8355 deref_to_type2))
8356 return 1;
8357 else if (is_properly_derived_from (deref_to_type2,
8358 deref_to_type1))
8359 return -1;
8361 else if (same_type_p (deref_to_type1, deref_to_type2))
8363 if (is_properly_derived_from (deref_from_type2,
8364 deref_from_type1))
8365 return 1;
8366 else if (is_properly_derived_from (deref_from_type1,
8367 deref_from_type2))
8368 return -1;
8372 else if (CLASS_TYPE_P (non_reference (from_type1))
8373 && same_type_p (from_type1, from_type2))
8375 tree from = non_reference (from_type1);
8377 /* [over.ics.rank]
8379 --binding of an expression of type C to a reference of type
8380 B& is better than binding an expression of type C to a
8381 reference of type A&
8383 --conversion of C to B is better than conversion of C to A, */
8384 if (is_properly_derived_from (from, to_type1)
8385 && is_properly_derived_from (from, to_type2))
8387 if (is_properly_derived_from (to_type1, to_type2))
8388 return 1;
8389 else if (is_properly_derived_from (to_type2, to_type1))
8390 return -1;
8393 else if (CLASS_TYPE_P (non_reference (to_type1))
8394 && same_type_p (to_type1, to_type2))
8396 tree to = non_reference (to_type1);
8398 /* [over.ics.rank]
8400 --binding of an expression of type B to a reference of type
8401 A& is better than binding an expression of type C to a
8402 reference of type A&,
8404 --conversion of B to A is better than conversion of C to A */
8405 if (is_properly_derived_from (from_type1, to)
8406 && is_properly_derived_from (from_type2, to))
8408 if (is_properly_derived_from (from_type2, from_type1))
8409 return 1;
8410 else if (is_properly_derived_from (from_type1, from_type2))
8411 return -1;
8415 /* [over.ics.rank]
8417 --S1 and S2 differ only in their qualification conversion and yield
8418 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
8419 qualification signature of type T1 is a proper subset of the cv-
8420 qualification signature of type T2 */
8421 if (ics1->kind == ck_qual
8422 && ics2->kind == ck_qual
8423 && same_type_p (from_type1, from_type2))
8425 int result = comp_cv_qual_signature (to_type1, to_type2);
8426 if (result != 0)
8427 return result;
8430 /* [over.ics.rank]
8432 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
8433 to an implicit object parameter, and either S1 binds an lvalue reference
8434 to an lvalue and S2 binds an rvalue reference or S1 binds an rvalue
8435 reference to an rvalue and S2 binds an lvalue reference
8436 (C++0x draft standard, 13.3.3.2)
8438 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
8439 types to which the references refer are the same type except for
8440 top-level cv-qualifiers, and the type to which the reference
8441 initialized by S2 refers is more cv-qualified than the type to
8442 which the reference initialized by S1 refers.
8444 DR 1328 [over.match.best]: the context is an initialization by
8445 conversion function for direct reference binding (13.3.1.6) of a
8446 reference to function type, the return type of F1 is the same kind of
8447 reference (i.e. lvalue or rvalue) as the reference being initialized,
8448 and the return type of F2 is not. */
8450 if (ref_conv1 && ref_conv2)
8452 if (!ref_conv1->this_p && !ref_conv2->this_p
8453 && (ref_conv1->rvaluedness_matches_p
8454 != ref_conv2->rvaluedness_matches_p)
8455 && (same_type_p (ref_conv1->type, ref_conv2->type)
8456 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
8457 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
8459 return (ref_conv1->rvaluedness_matches_p
8460 - ref_conv2->rvaluedness_matches_p);
8463 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
8464 return comp_cv_qualification (TREE_TYPE (ref_conv2->type),
8465 TREE_TYPE (ref_conv1->type));
8468 /* Neither conversion sequence is better than the other. */
8469 return 0;
8472 /* The source type for this standard conversion sequence. */
8474 static tree
8475 source_type (conversion *t)
8477 for (;; t = next_conversion (t))
8479 if (t->kind == ck_user
8480 || t->kind == ck_ambig
8481 || t->kind == ck_identity)
8482 return t->type;
8484 gcc_unreachable ();
8487 /* Note a warning about preferring WINNER to LOSER. We do this by storing
8488 a pointer to LOSER and re-running joust to produce the warning if WINNER
8489 is actually used. */
8491 static void
8492 add_warning (struct z_candidate *winner, struct z_candidate *loser)
8494 candidate_warning *cw = (candidate_warning *)
8495 conversion_obstack_alloc (sizeof (candidate_warning));
8496 cw->loser = loser;
8497 cw->next = winner->warnings;
8498 winner->warnings = cw;
8501 // Returns the template declaration associated with the candidate
8502 // function. For actual templates, this is directly associated
8503 // with the candidate. For temploids, we return the template
8504 // associated with the specialization.
8505 static inline tree
8506 template_decl_for_candidate (struct z_candidate *cand)
8508 tree r = cand->template_decl;
8509 tree d = cand->fn;
8510 if (!r && DECL_P (d) && DECL_USE_TEMPLATE (d))
8511 r = DECL_TI_TEMPLATE (d);
8512 if (r && TREE_CODE (r) == TEMPLATE_INFO)
8513 r = TI_TEMPLATE (r);
8514 return r;
8517 /* Compare two candidates for overloading as described in
8518 [over.match.best]. Return values:
8520 1: cand1 is better than cand2
8521 -1: cand2 is better than cand1
8522 0: cand1 and cand2 are indistinguishable */
8524 static int
8525 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
8526 tsubst_flags_t complain)
8528 int winner = 0;
8529 int off1 = 0, off2 = 0;
8530 size_t i;
8531 size_t len;
8533 // Get the actual template decls associated with the candidates.
8534 tree tmpl1 = template_decl_for_candidate (cand1);
8535 tree tmpl2 = template_decl_for_candidate (cand2);
8537 /* Candidates that involve bad conversions are always worse than those
8538 that don't. */
8539 if (cand1->viable > cand2->viable)
8540 return 1;
8541 if (cand1->viable < cand2->viable)
8542 return -1;
8544 /* If we have two pseudo-candidates for conversions to the same type,
8545 or two candidates for the same function, arbitrarily pick one. */
8546 if (cand1->fn == cand2->fn
8547 && (IS_TYPE_OR_DECL_P (cand1->fn)))
8548 return 1;
8550 /* Prefer a non-deleted function over an implicitly deleted move
8551 constructor or assignment operator. This differs slightly from the
8552 wording for issue 1402 (which says the move op is ignored by overload
8553 resolution), but this way produces better error messages. */
8554 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
8555 && TREE_CODE (cand2->fn) == FUNCTION_DECL
8556 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
8558 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
8559 && move_fn_p (cand1->fn))
8560 return -1;
8561 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
8562 && move_fn_p (cand2->fn))
8563 return 1;
8566 /* a viable function F1
8567 is defined to be a better function than another viable function F2 if
8568 for all arguments i, ICSi(F1) is not a worse conversion sequence than
8569 ICSi(F2), and then */
8571 /* for some argument j, ICSj(F1) is a better conversion sequence than
8572 ICSj(F2) */
8574 /* For comparing static and non-static member functions, we ignore
8575 the implicit object parameter of the non-static function. The
8576 standard says to pretend that the static function has an object
8577 parm, but that won't work with operator overloading. */
8578 len = cand1->num_convs;
8579 if (len != cand2->num_convs)
8581 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
8582 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
8584 if (DECL_CONSTRUCTOR_P (cand1->fn)
8585 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
8586 /* We're comparing a near-match list constructor and a near-match
8587 non-list constructor. Just treat them as unordered. */
8588 return 0;
8590 gcc_assert (static_1 != static_2);
8592 if (static_1)
8593 off2 = 1;
8594 else
8596 off1 = 1;
8597 --len;
8601 for (i = 0; i < len; ++i)
8603 conversion *t1 = cand1->convs[i + off1];
8604 conversion *t2 = cand2->convs[i + off2];
8605 int comp = compare_ics (t1, t2);
8607 if (comp != 0)
8609 if ((complain & tf_warning)
8610 && warn_sign_promo
8611 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
8612 == cr_std + cr_promotion)
8613 && t1->kind == ck_std
8614 && t2->kind == ck_std
8615 && TREE_CODE (t1->type) == INTEGER_TYPE
8616 && TREE_CODE (t2->type) == INTEGER_TYPE
8617 && (TYPE_PRECISION (t1->type)
8618 == TYPE_PRECISION (t2->type))
8619 && (TYPE_UNSIGNED (next_conversion (t1)->type)
8620 || (TREE_CODE (next_conversion (t1)->type)
8621 == ENUMERAL_TYPE)))
8623 tree type = next_conversion (t1)->type;
8624 tree type1, type2;
8625 struct z_candidate *w, *l;
8626 if (comp > 0)
8627 type1 = t1->type, type2 = t2->type,
8628 w = cand1, l = cand2;
8629 else
8630 type1 = t2->type, type2 = t1->type,
8631 w = cand2, l = cand1;
8633 if (warn)
8635 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
8636 type, type1, type2);
8637 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
8639 else
8640 add_warning (w, l);
8643 if (winner && comp != winner)
8645 winner = 0;
8646 goto tweak;
8648 winner = comp;
8652 /* warn about confusing overload resolution for user-defined conversions,
8653 either between a constructor and a conversion op, or between two
8654 conversion ops. */
8655 if ((complain & tf_warning)
8656 && winner && warn_conversion && cand1->second_conv
8657 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
8658 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
8660 struct z_candidate *w, *l;
8661 bool give_warning = false;
8663 if (winner == 1)
8664 w = cand1, l = cand2;
8665 else
8666 w = cand2, l = cand1;
8668 /* We don't want to complain about `X::operator T1 ()'
8669 beating `X::operator T2 () const', when T2 is a no less
8670 cv-qualified version of T1. */
8671 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
8672 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
8674 tree t = TREE_TYPE (TREE_TYPE (l->fn));
8675 tree f = TREE_TYPE (TREE_TYPE (w->fn));
8677 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
8679 t = TREE_TYPE (t);
8680 f = TREE_TYPE (f);
8682 if (!comp_ptr_ttypes (t, f))
8683 give_warning = true;
8685 else
8686 give_warning = true;
8688 if (!give_warning)
8689 /*NOP*/;
8690 else if (warn)
8692 tree source = source_type (w->convs[0]);
8693 if (! DECL_CONSTRUCTOR_P (w->fn))
8694 source = TREE_TYPE (source);
8695 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
8696 && warning (OPT_Wconversion, " for conversion from %qT to %qT",
8697 source, w->second_conv->type))
8699 inform (input_location, " because conversion sequence for the argument is better");
8702 else
8703 add_warning (w, l);
8706 if (winner)
8707 return winner;
8709 /* DR 495 moved this tiebreaker above the template ones. */
8710 /* or, if not that,
8711 the context is an initialization by user-defined conversion (see
8712 _dcl.init_ and _over.match.user_) and the standard conversion
8713 sequence from the return type of F1 to the destination type (i.e.,
8714 the type of the entity being initialized) is a better conversion
8715 sequence than the standard conversion sequence from the return type
8716 of F2 to the destination type. */
8718 if (cand1->second_conv)
8720 winner = compare_ics (cand1->second_conv, cand2->second_conv);
8721 if (winner)
8722 return winner;
8725 /* or, if not that,
8726 F1 is a non-template function and F2 is a template function
8727 specialization. */
8729 if (!cand1->template_decl && cand2->template_decl)
8730 return 1;
8731 else if (cand1->template_decl && !cand2->template_decl)
8732 return -1;
8734 /* or, if not that,
8735 F1 and F2 are template functions and the function template for F1 is
8736 more specialized than the template for F2 according to the partial
8737 ordering rules. */
8739 if (tmpl1 && tmpl2)
8741 /* [temp.func.order]: The presence of unused ellipsis and default
8742 arguments has no effect on the partial ordering of function
8743 templates. add_function_candidate() will not have
8744 counted the "this" argument for constructors. */
8745 int nparms = cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn);
8746 winner = more_specialized_fn (tmpl1, tmpl2, nparms);
8747 if (winner)
8748 return winner;
8751 /* Check whether we can discard a builtin candidate, either because we
8752 have two identical ones or matching builtin and non-builtin candidates.
8754 (Pedantically in the latter case the builtin which matched the user
8755 function should not be added to the overload set, but we spot it here.
8757 [over.match.oper]
8758 ... the builtin candidates include ...
8759 - do not have the same parameter type list as any non-template
8760 non-member candidate. */
8762 if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
8764 for (i = 0; i < len; ++i)
8765 if (!same_type_p (cand1->convs[i]->type,
8766 cand2->convs[i]->type))
8767 break;
8768 if (i == cand1->num_convs)
8770 if (cand1->fn == cand2->fn)
8771 /* Two built-in candidates; arbitrarily pick one. */
8772 return 1;
8773 else if (identifier_p (cand1->fn))
8774 /* cand1 is built-in; prefer cand2. */
8775 return -1;
8776 else
8777 /* cand2 is built-in; prefer cand1. */
8778 return 1;
8782 /* For candidates of a multi-versioned function, make the version with
8783 the highest priority win. This version will be checked for dispatching
8784 first. If this version can be inlined into the caller, the front-end
8785 will simply make a direct call to this function. */
8787 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
8788 && DECL_FUNCTION_VERSIONED (cand1->fn)
8789 && TREE_CODE (cand2->fn) == FUNCTION_DECL
8790 && DECL_FUNCTION_VERSIONED (cand2->fn))
8792 tree f1 = TREE_TYPE (cand1->fn);
8793 tree f2 = TREE_TYPE (cand2->fn);
8794 tree p1 = TYPE_ARG_TYPES (f1);
8795 tree p2 = TYPE_ARG_TYPES (f2);
8797 /* Check if cand1->fn and cand2->fn are versions of the same function. It
8798 is possible that cand1->fn and cand2->fn are function versions but of
8799 different functions. Check types to see if they are versions of the same
8800 function. */
8801 if (compparms (p1, p2)
8802 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8804 /* Always make the version with the higher priority, more
8805 specialized, win. */
8806 gcc_assert (targetm.compare_version_priority);
8807 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
8808 return 1;
8809 else
8810 return -1;
8814 /* If the two function declarations represent the same function (this can
8815 happen with declarations in multiple scopes and arg-dependent lookup),
8816 arbitrarily choose one. But first make sure the default args we're
8817 using match. */
8818 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
8819 && equal_functions (cand1->fn, cand2->fn))
8821 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
8822 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
8824 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
8826 for (i = 0; i < len; ++i)
8828 /* Don't crash if the fn is variadic. */
8829 if (!parms1)
8830 break;
8831 parms1 = TREE_CHAIN (parms1);
8832 parms2 = TREE_CHAIN (parms2);
8835 if (off1)
8836 parms1 = TREE_CHAIN (parms1);
8837 else if (off2)
8838 parms2 = TREE_CHAIN (parms2);
8840 for (; parms1; ++i)
8842 if (!cp_tree_equal (TREE_PURPOSE (parms1),
8843 TREE_PURPOSE (parms2)))
8845 if (warn)
8847 if (complain & tf_error)
8849 if (permerror (input_location,
8850 "default argument mismatch in "
8851 "overload resolution"))
8853 inform (input_location,
8854 " candidate 1: %q+#F", cand1->fn);
8855 inform (input_location,
8856 " candidate 2: %q+#F", cand2->fn);
8859 else
8860 return 0;
8862 else
8863 add_warning (cand1, cand2);
8864 break;
8866 parms1 = TREE_CHAIN (parms1);
8867 parms2 = TREE_CHAIN (parms2);
8870 return 1;
8873 tweak:
8875 /* Extension: If the worst conversion for one candidate is worse than the
8876 worst conversion for the other, take the first. */
8877 if (!pedantic && (complain & tf_warning_or_error))
8879 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
8880 struct z_candidate *w = 0, *l = 0;
8882 for (i = 0; i < len; ++i)
8884 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
8885 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
8886 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
8887 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
8889 if (rank1 < rank2)
8890 winner = 1, w = cand1, l = cand2;
8891 if (rank1 > rank2)
8892 winner = -1, w = cand2, l = cand1;
8893 if (winner)
8895 /* Don't choose a deleted function over ambiguity. */
8896 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
8897 return 0;
8898 if (warn)
8900 pedwarn (input_location, 0,
8901 "ISO C++ says that these are ambiguous, even "
8902 "though the worst conversion for the first is better than "
8903 "the worst conversion for the second:");
8904 print_z_candidate (input_location, _("candidate 1:"), w);
8905 print_z_candidate (input_location, _("candidate 2:"), l);
8907 else
8908 add_warning (w, l);
8909 return winner;
8913 gcc_assert (!winner);
8914 return 0;
8917 /* Given a list of candidates for overloading, find the best one, if any.
8918 This algorithm has a worst case of O(2n) (winner is last), and a best
8919 case of O(n/2) (totally ambiguous); much better than a sorting
8920 algorithm. */
8922 static struct z_candidate *
8923 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
8925 struct z_candidate *champ = candidates, *challenger;
8926 int fate;
8927 int champ_compared_to_predecessor = 0;
8929 /* Walk through the list once, comparing each current champ to the next
8930 candidate, knocking out a candidate or two with each comparison. */
8932 for (challenger = champ->next; challenger; )
8934 fate = joust (champ, challenger, 0, complain);
8935 if (fate == 1)
8936 challenger = challenger->next;
8937 else
8939 if (fate == 0)
8941 champ = challenger->next;
8942 if (champ == 0)
8943 return NULL;
8944 champ_compared_to_predecessor = 0;
8946 else
8948 champ = challenger;
8949 champ_compared_to_predecessor = 1;
8952 challenger = champ->next;
8956 /* Make sure the champ is better than all the candidates it hasn't yet
8957 been compared to. */
8959 for (challenger = candidates;
8960 challenger != champ
8961 && !(champ_compared_to_predecessor && challenger->next == champ);
8962 challenger = challenger->next)
8964 fate = joust (champ, challenger, 0, complain);
8965 if (fate != 1)
8966 return NULL;
8969 return champ;
8973 // Returns true if things of type FROM can be implicitly converted to TO.
8974 bool
8975 can_convert (tree to, tree from, tsubst_flags_t complain)
8977 tree arg = NULL_TREE;
8978 /* implicit_conversion only considers user-defined conversions
8979 if it has an expression for the call argument list. */
8980 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
8981 arg = build1 (CAST_EXPR, from, NULL_TREE);
8982 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
8985 /* Returns nonzero if things of type FROM can be converted to TO with a
8986 standard conversion. */
8988 bool
8989 can_convert_standard (tree to, tree from, tsubst_flags_t complain)
8991 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
8994 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
8996 bool
8997 can_convert_arg (tree to, tree from, tree arg, int flags,
8998 tsubst_flags_t complain)
9000 conversion *t;
9001 void *p;
9002 bool ok_p;
9004 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9005 p = conversion_obstack_alloc (0);
9006 /* We want to discard any access checks done for this test,
9007 as we might not be in the appropriate access context and
9008 we'll do the check again when we actually perform the
9009 conversion. */
9010 push_deferring_access_checks (dk_deferred);
9012 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
9013 flags, complain);
9014 ok_p = (t && !t->bad_p);
9016 /* Discard the access checks now. */
9017 pop_deferring_access_checks ();
9018 /* Free all the conversions we allocated. */
9019 obstack_free (&conversion_obstack, p);
9021 return ok_p;
9024 /* Like can_convert_arg, but allows dubious conversions as well. */
9026 bool
9027 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
9028 tsubst_flags_t complain)
9030 conversion *t;
9031 void *p;
9033 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9034 p = conversion_obstack_alloc (0);
9035 /* Try to perform the conversion. */
9036 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
9037 flags, complain);
9038 /* Free all the conversions we allocated. */
9039 obstack_free (&conversion_obstack, p);
9041 return t != NULL;
9044 /* Convert EXPR to TYPE. Return the converted expression.
9046 Note that we allow bad conversions here because by the time we get to
9047 this point we are committed to doing the conversion. If we end up
9048 doing a bad conversion, convert_like will complain. */
9050 tree
9051 perform_implicit_conversion_flags (tree type, tree expr,
9052 tsubst_flags_t complain, int flags)
9054 conversion *conv;
9055 void *p;
9056 location_t loc = EXPR_LOC_OR_HERE (expr);
9058 if (error_operand_p (expr))
9059 return error_mark_node;
9061 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9062 p = conversion_obstack_alloc (0);
9064 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
9065 /*c_cast_p=*/false,
9066 flags, complain);
9068 if (!conv)
9070 if (complain & tf_error)
9072 /* If expr has unknown type, then it is an overloaded function.
9073 Call instantiate_type to get good error messages. */
9074 if (TREE_TYPE (expr) == unknown_type_node)
9075 instantiate_type (type, expr, complain);
9076 else if (invalid_nonstatic_memfn_p (expr, complain))
9077 /* We gave an error. */;
9078 else
9079 error_at (loc, "could not convert %qE from %qT to %qT", expr,
9080 TREE_TYPE (expr), type);
9082 expr = error_mark_node;
9084 else if (processing_template_decl && conv->kind != ck_identity)
9086 /* In a template, we are only concerned about determining the
9087 type of non-dependent expressions, so we do not have to
9088 perform the actual conversion. But for initializers, we
9089 need to be able to perform it at instantiation
9090 (or fold_non_dependent_expr) time. */
9091 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
9092 if (!(flags & LOOKUP_ONLYCONVERTING))
9093 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
9095 else
9096 expr = convert_like (conv, expr, complain);
9098 /* Free all the conversions we allocated. */
9099 obstack_free (&conversion_obstack, p);
9101 return expr;
9104 tree
9105 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
9107 return perform_implicit_conversion_flags (type, expr, complain,
9108 LOOKUP_IMPLICIT);
9111 /* Convert EXPR to TYPE (as a direct-initialization) if that is
9112 permitted. If the conversion is valid, the converted expression is
9113 returned. Otherwise, NULL_TREE is returned, except in the case
9114 that TYPE is a class type; in that case, an error is issued. If
9115 C_CAST_P is true, then this direct-initialization is taking
9116 place as part of a static_cast being attempted as part of a C-style
9117 cast. */
9119 tree
9120 perform_direct_initialization_if_possible (tree type,
9121 tree expr,
9122 bool c_cast_p,
9123 tsubst_flags_t complain)
9125 conversion *conv;
9126 void *p;
9128 if (type == error_mark_node || error_operand_p (expr))
9129 return error_mark_node;
9130 /* [dcl.init]
9132 If the destination type is a (possibly cv-qualified) class type:
9134 -- If the initialization is direct-initialization ...,
9135 constructors are considered. ... If no constructor applies, or
9136 the overload resolution is ambiguous, the initialization is
9137 ill-formed. */
9138 if (CLASS_TYPE_P (type))
9140 vec<tree, va_gc> *args = make_tree_vector_single (expr);
9141 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
9142 &args, type, LOOKUP_NORMAL, complain);
9143 release_tree_vector (args);
9144 return build_cplus_new (type, expr, complain);
9147 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9148 p = conversion_obstack_alloc (0);
9150 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
9151 c_cast_p,
9152 LOOKUP_NORMAL, complain);
9153 if (!conv || conv->bad_p)
9154 expr = NULL_TREE;
9155 else
9156 expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
9157 /*issue_conversion_warnings=*/false,
9158 c_cast_p,
9159 complain);
9161 /* Free all the conversions we allocated. */
9162 obstack_free (&conversion_obstack, p);
9164 return expr;
9167 /* When initializing a reference that lasts longer than a full-expression,
9168 this special rule applies:
9170 [class.temporary]
9172 The temporary to which the reference is bound or the temporary
9173 that is the complete object to which the reference is bound
9174 persists for the lifetime of the reference.
9176 The temporaries created during the evaluation of the expression
9177 initializing the reference, except the temporary to which the
9178 reference is bound, are destroyed at the end of the
9179 full-expression in which they are created.
9181 In that case, we store the converted expression into a new
9182 VAR_DECL in a new scope.
9184 However, we want to be careful not to create temporaries when
9185 they are not required. For example, given:
9187 struct B {};
9188 struct D : public B {};
9189 D f();
9190 const B& b = f();
9192 there is no need to copy the return value from "f"; we can just
9193 extend its lifetime. Similarly, given:
9195 struct S {};
9196 struct T { operator S(); };
9197 T t;
9198 const S& s = t;
9200 we can extend the lifetime of the return value of the conversion
9201 operator.
9203 The next several functions are involved in this lifetime extension. */
9205 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
9206 reference is being bound to a temporary. Create and return a new
9207 VAR_DECL with the indicated TYPE; this variable will store the value to
9208 which the reference is bound. */
9210 tree
9211 make_temporary_var_for_ref_to_temp (tree decl, tree type)
9213 tree var;
9215 /* Create the variable. */
9216 var = create_temporary_var (type);
9218 /* Register the variable. */
9219 if (VAR_P (decl)
9220 && (TREE_STATIC (decl) || DECL_THREAD_LOCAL_P (decl)))
9222 /* Namespace-scope or local static; give it a mangled name. */
9223 /* FIXME share comdat with decl? */
9224 tree name;
9226 TREE_STATIC (var) = TREE_STATIC (decl);
9227 DECL_TLS_MODEL (var) = DECL_TLS_MODEL (decl);
9228 name = mangle_ref_init_variable (decl);
9229 DECL_NAME (var) = name;
9230 SET_DECL_ASSEMBLER_NAME (var, name);
9231 var = pushdecl_top_level (var);
9233 else
9234 /* Create a new cleanup level if necessary. */
9235 maybe_push_cleanup_level (type);
9237 return var;
9240 /* EXPR is the initializer for a variable DECL of reference or
9241 std::initializer_list type. Create, push and return a new VAR_DECL
9242 for the initializer so that it will live as long as DECL. Any
9243 cleanup for the new variable is returned through CLEANUP, and the
9244 code to initialize the new variable is returned through INITP. */
9246 static tree
9247 set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
9248 tree *initp)
9250 tree init;
9251 tree type;
9252 tree var;
9254 /* Create the temporary variable. */
9255 type = TREE_TYPE (expr);
9256 var = make_temporary_var_for_ref_to_temp (decl, type);
9257 layout_decl (var, 0);
9258 /* If the rvalue is the result of a function call it will be
9259 a TARGET_EXPR. If it is some other construct (such as a
9260 member access expression where the underlying object is
9261 itself the result of a function call), turn it into a
9262 TARGET_EXPR here. It is important that EXPR be a
9263 TARGET_EXPR below since otherwise the INIT_EXPR will
9264 attempt to make a bitwise copy of EXPR to initialize
9265 VAR. */
9266 if (TREE_CODE (expr) != TARGET_EXPR)
9267 expr = get_target_expr (expr);
9269 if (TREE_CODE (decl) == FIELD_DECL
9270 && extra_warnings && !TREE_NO_WARNING (decl))
9272 warning (OPT_Wextra, "a temporary bound to %qD only persists "
9273 "until the constructor exits", decl);
9274 TREE_NO_WARNING (decl) = true;
9277 /* Recursively extend temps in this initializer. */
9278 TARGET_EXPR_INITIAL (expr)
9279 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups);
9281 /* Any reference temp has a non-trivial initializer. */
9282 DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
9284 /* If the initializer is constant, put it in DECL_INITIAL so we get
9285 static initialization and use in constant expressions. */
9286 init = maybe_constant_init (expr);
9287 if (TREE_CONSTANT (init))
9289 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
9291 /* 5.19 says that a constant expression can include an
9292 lvalue-rvalue conversion applied to "a glvalue of literal type
9293 that refers to a non-volatile temporary object initialized
9294 with a constant expression". Rather than try to communicate
9295 that this VAR_DECL is a temporary, just mark it constexpr.
9297 Currently this is only useful for initializer_list temporaries,
9298 since reference vars can't appear in constant expressions. */
9299 DECL_DECLARED_CONSTEXPR_P (var) = true;
9300 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
9301 TREE_CONSTANT (var) = true;
9303 DECL_INITIAL (var) = init;
9304 init = NULL_TREE;
9306 else
9307 /* Create the INIT_EXPR that will initialize the temporary
9308 variable. */
9309 init = build2 (INIT_EXPR, type, var, expr);
9310 if (at_function_scope_p ())
9312 add_decl_expr (var);
9314 if (TREE_STATIC (var))
9315 init = add_stmt_to_compound (init, register_dtor_fn (var));
9316 else
9318 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
9319 if (cleanup)
9320 vec_safe_push (*cleanups, cleanup);
9323 /* We must be careful to destroy the temporary only
9324 after its initialization has taken place. If the
9325 initialization throws an exception, then the
9326 destructor should not be run. We cannot simply
9327 transform INIT into something like:
9329 (INIT, ({ CLEANUP_STMT; }))
9331 because emit_local_var always treats the
9332 initializer as a full-expression. Thus, the
9333 destructor would run too early; it would run at the
9334 end of initializing the reference variable, rather
9335 than at the end of the block enclosing the
9336 reference variable.
9338 The solution is to pass back a cleanup expression
9339 which the caller is responsible for attaching to
9340 the statement tree. */
9342 else
9344 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
9345 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9347 if (DECL_THREAD_LOCAL_P (var))
9348 tls_aggregates = tree_cons (NULL_TREE, var,
9349 tls_aggregates);
9350 else
9351 static_aggregates = tree_cons (NULL_TREE, var,
9352 static_aggregates);
9354 else
9355 /* Check whether the dtor is callable. */
9356 cxx_maybe_build_cleanup (var, tf_warning_or_error);
9359 *initp = init;
9360 return var;
9363 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
9364 initializing a variable of that TYPE. */
9366 tree
9367 initialize_reference (tree type, tree expr,
9368 int flags, tsubst_flags_t complain)
9370 conversion *conv;
9371 void *p;
9372 location_t loc = EXPR_LOC_OR_HERE (expr);
9374 if (type == error_mark_node || error_operand_p (expr))
9375 return error_mark_node;
9377 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9378 p = conversion_obstack_alloc (0);
9380 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
9381 flags, complain);
9382 if (!conv || conv->bad_p)
9384 if (complain & tf_error)
9386 if (conv)
9387 convert_like (conv, expr, complain);
9388 else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
9389 && !TYPE_REF_IS_RVALUE (type)
9390 && !real_lvalue_p (expr))
9391 error_at (loc, "invalid initialization of non-const reference of "
9392 "type %qT from an rvalue of type %qT",
9393 type, TREE_TYPE (expr));
9394 else
9395 error_at (loc, "invalid initialization of reference of type "
9396 "%qT from expression of type %qT", type,
9397 TREE_TYPE (expr));
9399 return error_mark_node;
9402 if (conv->kind == ck_ref_bind)
9403 /* Perform the conversion. */
9404 expr = convert_like (conv, expr, complain);
9405 else if (conv->kind == ck_ambig)
9406 /* We gave an error in build_user_type_conversion_1. */
9407 expr = error_mark_node;
9408 else
9409 gcc_unreachable ();
9411 /* Free all the conversions we allocated. */
9412 obstack_free (&conversion_obstack, p);
9414 return expr;
9417 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
9418 which is bound either to a reference or a std::initializer_list. */
9420 static tree
9421 extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups)
9423 tree sub = init;
9424 tree *p;
9425 STRIP_NOPS (sub);
9426 if (TREE_CODE (sub) == COMPOUND_EXPR)
9428 TREE_OPERAND (sub, 1)
9429 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups);
9430 return init;
9432 if (TREE_CODE (sub) != ADDR_EXPR)
9433 return init;
9434 /* Deal with binding to a subobject. */
9435 for (p = &TREE_OPERAND (sub, 0); TREE_CODE (*p) == COMPONENT_REF; )
9436 p = &TREE_OPERAND (*p, 0);
9437 if (TREE_CODE (*p) == TARGET_EXPR)
9439 tree subinit = NULL_TREE;
9440 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit);
9441 if (subinit)
9442 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
9443 recompute_tree_invariant_for_addr_expr (sub);
9445 return init;
9448 /* INIT is part of the initializer for DECL. If there are any
9449 reference or initializer lists being initialized, extend their
9450 lifetime to match that of DECL. */
9452 tree
9453 extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
9455 tree type = TREE_TYPE (init);
9456 if (processing_template_decl)
9457 return init;
9458 if (TREE_CODE (type) == REFERENCE_TYPE)
9459 init = extend_ref_init_temps_1 (decl, init, cleanups);
9460 else if (is_std_init_list (type))
9462 /* The temporary array underlying a std::initializer_list
9463 is handled like a reference temporary. */
9464 tree ctor = init;
9465 if (TREE_CODE (ctor) == TARGET_EXPR)
9466 ctor = TARGET_EXPR_INITIAL (ctor);
9467 if (TREE_CODE (ctor) == CONSTRUCTOR)
9469 tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
9470 array = extend_ref_init_temps_1 (decl, array, cleanups);
9471 CONSTRUCTOR_ELT (ctor, 0)->value = array;
9474 else if (TREE_CODE (init) == CONSTRUCTOR)
9476 unsigned i;
9477 constructor_elt *p;
9478 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
9479 FOR_EACH_VEC_SAFE_ELT (elts, i, p)
9480 p->value = extend_ref_init_temps (decl, p->value, cleanups);
9483 return init;
9486 /* Returns true iff an initializer for TYPE could contain temporaries that
9487 need to be extended because they are bound to references or
9488 std::initializer_list. */
9490 bool
9491 type_has_extended_temps (tree type)
9493 type = strip_array_types (type);
9494 if (TREE_CODE (type) == REFERENCE_TYPE)
9495 return true;
9496 if (CLASS_TYPE_P (type))
9498 if (is_std_init_list (type))
9499 return true;
9500 for (tree f = next_initializable_field (TYPE_FIELDS (type));
9501 f; f = next_initializable_field (DECL_CHAIN (f)))
9502 if (type_has_extended_temps (TREE_TYPE (f)))
9503 return true;
9505 return false;
9508 /* Returns true iff TYPE is some variant of std::initializer_list. */
9510 bool
9511 is_std_init_list (tree type)
9513 /* Look through typedefs. */
9514 if (!TYPE_P (type))
9515 return false;
9516 if (cxx_dialect == cxx98)
9517 return false;
9518 type = TYPE_MAIN_VARIANT (type);
9519 return (CLASS_TYPE_P (type)
9520 && CP_TYPE_CONTEXT (type) == std_node
9521 && strcmp (TYPE_NAME_STRING (type), "initializer_list") == 0);
9524 /* Returns true iff DECL is a list constructor: i.e. a constructor which
9525 will accept an argument list of a single std::initializer_list<T>. */
9527 bool
9528 is_list_ctor (tree decl)
9530 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
9531 tree arg;
9533 if (!args || args == void_list_node)
9534 return false;
9536 arg = non_reference (TREE_VALUE (args));
9537 if (!is_std_init_list (arg))
9538 return false;
9540 args = TREE_CHAIN (args);
9542 if (args && args != void_list_node && !TREE_PURPOSE (args))
9543 /* There are more non-defaulted parms. */
9544 return false;
9546 return true;
9549 #include "gt-cp-call.h"