PR c++/58636
[official-gcc.git] / gcc / cp / call.c
blobb16c6e421674090c8bd7b7c50aee30032748aa82
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* High-level class interface. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "tree.h"
30 #include "stor-layout.h"
31 #include "trans-mem.h"
32 #include "stringpool.h"
33 #include "cp-tree.h"
34 #include "flags.h"
35 #include "toplev.h"
36 #include "diagnostic-core.h"
37 #include "intl.h"
38 #include "target.h"
39 #include "convert.h"
40 #include "langhooks.h"
41 #include "c-family/c-objc.h"
42 #include "timevar.h"
43 #include "cgraph.h"
44 #include "wide-int.h"
46 /* The various kinds of conversion. */
48 typedef enum conversion_kind {
49 ck_identity,
50 ck_lvalue,
51 ck_qual,
52 ck_std,
53 ck_ptr,
54 ck_pmem,
55 ck_base,
56 ck_ref_bind,
57 ck_user,
58 ck_ambig,
59 ck_list,
60 ck_aggr,
61 ck_rvalue
62 } conversion_kind;
64 /* The rank of the conversion. Order of the enumerals matters; better
65 conversions should come earlier in the list. */
67 typedef enum conversion_rank {
68 cr_identity,
69 cr_exact,
70 cr_promotion,
71 cr_std,
72 cr_pbool,
73 cr_user,
74 cr_ellipsis,
75 cr_bad
76 } conversion_rank;
78 /* An implicit conversion sequence, in the sense of [over.best.ics].
79 The first conversion to be performed is at the end of the chain.
80 That conversion is always a cr_identity conversion. */
82 typedef struct conversion conversion;
83 struct conversion {
84 /* The kind of conversion represented by this step. */
85 conversion_kind kind;
86 /* The rank of this conversion. */
87 conversion_rank rank;
88 BOOL_BITFIELD user_conv_p : 1;
89 BOOL_BITFIELD ellipsis_p : 1;
90 BOOL_BITFIELD this_p : 1;
91 /* True if this conversion would be permitted with a bending of
92 language standards, e.g. disregarding pointer qualifiers or
93 converting integers to pointers. */
94 BOOL_BITFIELD bad_p : 1;
95 /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
96 temporary should be created to hold the result of the
97 conversion. */
98 BOOL_BITFIELD need_temporary_p : 1;
99 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
100 from a pointer-to-derived to pointer-to-base is being performed. */
101 BOOL_BITFIELD base_p : 1;
102 /* If KIND is ck_ref_bind, true when either an lvalue reference is
103 being bound to an lvalue expression or an rvalue reference is
104 being bound to an rvalue expression. If KIND is ck_rvalue,
105 true when we should treat an lvalue as an rvalue (12.8p33). If
106 KIND is ck_base, always false. */
107 BOOL_BITFIELD rvaluedness_matches_p: 1;
108 BOOL_BITFIELD check_narrowing: 1;
109 /* The type of the expression resulting from the conversion. */
110 tree type;
111 union {
112 /* The next conversion in the chain. Since the conversions are
113 arranged from outermost to innermost, the NEXT conversion will
114 actually be performed before this conversion. This variant is
115 used only when KIND is neither ck_identity, ck_ambig nor
116 ck_list. Please use the next_conversion function instead
117 of using this field directly. */
118 conversion *next;
119 /* The expression at the beginning of the conversion chain. This
120 variant is used only if KIND is ck_identity or ck_ambig. */
121 tree expr;
122 /* The array of conversions for an initializer_list, so this
123 variant is used only when KIN D is ck_list. */
124 conversion **list;
125 } u;
126 /* The function candidate corresponding to this conversion
127 sequence. This field is only used if KIND is ck_user. */
128 struct z_candidate *cand;
131 #define CONVERSION_RANK(NODE) \
132 ((NODE)->bad_p ? cr_bad \
133 : (NODE)->ellipsis_p ? cr_ellipsis \
134 : (NODE)->user_conv_p ? cr_user \
135 : (NODE)->rank)
137 #define BAD_CONVERSION_RANK(NODE) \
138 ((NODE)->ellipsis_p ? cr_ellipsis \
139 : (NODE)->user_conv_p ? cr_user \
140 : (NODE)->rank)
142 static struct obstack conversion_obstack;
143 static bool conversion_obstack_initialized;
144 struct rejection_reason;
146 static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
147 static int equal_functions (tree, tree);
148 static int joust (struct z_candidate *, struct z_candidate *, bool,
149 tsubst_flags_t);
150 static int compare_ics (conversion *, conversion *);
151 static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
152 static tree build_java_interface_fn_ref (tree, tree);
153 #define convert_like(CONV, EXPR, COMPLAIN) \
154 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \
155 /*issue_conversion_warnings=*/true, \
156 /*c_cast_p=*/false, (COMPLAIN))
157 #define convert_like_with_context(CONV, EXPR, FN, ARGNO, COMPLAIN ) \
158 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
159 /*issue_conversion_warnings=*/true, \
160 /*c_cast_p=*/false, (COMPLAIN))
161 static tree convert_like_real (conversion *, tree, tree, int, int, bool,
162 bool, tsubst_flags_t);
163 static void op_error (location_t, enum tree_code, enum tree_code, tree,
164 tree, tree, bool);
165 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
166 tsubst_flags_t);
167 static void print_z_candidate (location_t, const char *, struct z_candidate *);
168 static void print_z_candidates (location_t, struct z_candidate *);
169 static tree build_this (tree);
170 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
171 static bool any_strictly_viable (struct z_candidate *);
172 static struct z_candidate *add_template_candidate
173 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
174 tree, tree, tree, int, unification_kind_t, tsubst_flags_t);
175 static struct z_candidate *add_template_candidate_real
176 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
177 tree, tree, tree, int, tree, unification_kind_t, tsubst_flags_t);
178 static struct z_candidate *add_template_conv_candidate
179 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *,
180 tree, tree, tree, tsubst_flags_t);
181 static void add_builtin_candidates
182 (struct z_candidate **, enum tree_code, enum tree_code,
183 tree, tree *, int, tsubst_flags_t);
184 static void add_builtin_candidate
185 (struct z_candidate **, enum tree_code, enum tree_code,
186 tree, tree, tree, tree *, tree *, int, tsubst_flags_t);
187 static bool is_complete (tree);
188 static void build_builtin_candidate
189 (struct z_candidate **, tree, tree, tree, tree *, tree *,
190 int, tsubst_flags_t);
191 static struct z_candidate *add_conv_candidate
192 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
193 tree, tsubst_flags_t);
194 static struct z_candidate *add_function_candidate
195 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
196 tree, int, tsubst_flags_t);
197 static conversion *implicit_conversion (tree, tree, tree, bool, int,
198 tsubst_flags_t);
199 static conversion *standard_conversion (tree, tree, tree, bool, int);
200 static conversion *reference_binding (tree, tree, tree, bool, int,
201 tsubst_flags_t);
202 static conversion *build_conv (conversion_kind, tree, conversion *);
203 static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
204 static conversion *next_conversion (conversion *);
205 static bool is_subseq (conversion *, conversion *);
206 static conversion *maybe_handle_ref_bind (conversion **);
207 static void maybe_handle_implicit_object (conversion **);
208 static struct z_candidate *add_candidate
209 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
210 conversion **, tree, tree, int, struct rejection_reason *, int);
211 static tree source_type (conversion *);
212 static void add_warning (struct z_candidate *, struct z_candidate *);
213 static bool reference_compatible_p (tree, tree);
214 static conversion *direct_reference_binding (tree, conversion *);
215 static bool promoted_arithmetic_type_p (tree);
216 static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
217 static char *name_as_c_string (tree, tree, bool *);
218 static tree prep_operand (tree);
219 static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
220 bool, tree, tree, int, struct z_candidate **,
221 tsubst_flags_t);
222 static conversion *merge_conversion_sequences (conversion *, conversion *);
223 static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
225 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
226 NAME can take many forms... */
228 bool
229 check_dtor_name (tree basetype, tree name)
231 /* Just accept something we've already complained about. */
232 if (name == error_mark_node)
233 return true;
235 if (TREE_CODE (name) == TYPE_DECL)
236 name = TREE_TYPE (name);
237 else if (TYPE_P (name))
238 /* OK */;
239 else if (identifier_p (name))
241 if ((MAYBE_CLASS_TYPE_P (basetype)
242 && name == constructor_name (basetype))
243 || (TREE_CODE (basetype) == ENUMERAL_TYPE
244 && name == TYPE_IDENTIFIER (basetype)))
245 return true;
246 else
247 name = get_type_value (name);
249 else
251 /* In the case of:
253 template <class T> struct S { ~S(); };
254 int i;
255 i.~S();
257 NAME will be a class template. */
258 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
259 return false;
262 if (!name || name == error_mark_node)
263 return false;
264 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
267 /* We want the address of a function or method. We avoid creating a
268 pointer-to-member function. */
270 tree
271 build_addr_func (tree function, tsubst_flags_t complain)
273 tree type = TREE_TYPE (function);
275 /* We have to do these by hand to avoid real pointer to member
276 functions. */
277 if (TREE_CODE (type) == METHOD_TYPE)
279 if (TREE_CODE (function) == OFFSET_REF)
281 tree object = build_address (TREE_OPERAND (function, 0));
282 return get_member_function_from_ptrfunc (&object,
283 TREE_OPERAND (function, 1),
284 complain);
286 function = build_address (function);
288 else
289 function = decay_conversion (function, complain);
291 return function;
294 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
295 POINTER_TYPE to those. Note, pointer to member function types
296 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
297 two variants. build_call_a is the primitive taking an array of
298 arguments, while build_call_n is a wrapper that handles varargs. */
300 tree
301 build_call_n (tree function, int n, ...)
303 if (n == 0)
304 return build_call_a (function, 0, NULL);
305 else
307 tree *argarray = XALLOCAVEC (tree, n);
308 va_list ap;
309 int i;
311 va_start (ap, n);
312 for (i = 0; i < n; i++)
313 argarray[i] = va_arg (ap, tree);
314 va_end (ap);
315 return build_call_a (function, n, argarray);
319 /* Update various flags in cfun and the call itself based on what is being
320 called. Split out of build_call_a so that bot_manip can use it too. */
322 void
323 set_flags_from_callee (tree call)
325 int nothrow;
326 tree decl = get_callee_fndecl (call);
328 /* We check both the decl and the type; a function may be known not to
329 throw without being declared throw(). */
330 nothrow = ((decl && TREE_NOTHROW (decl))
331 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (call)))));
333 if (!nothrow && at_function_scope_p () && cfun && cp_function_chain)
334 cp_function_chain->can_throw = 1;
336 if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
337 current_function_returns_abnormally = 1;
339 TREE_NOTHROW (call) = nothrow;
342 tree
343 build_call_a (tree function, int n, tree *argarray)
345 tree decl;
346 tree result_type;
347 tree fntype;
348 int i;
350 function = build_addr_func (function, tf_warning_or_error);
352 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
353 fntype = TREE_TYPE (TREE_TYPE (function));
354 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
355 || TREE_CODE (fntype) == METHOD_TYPE);
356 result_type = TREE_TYPE (fntype);
357 /* An rvalue has no cv-qualifiers. */
358 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
359 result_type = cv_unqualified (result_type);
361 function = build_call_array_loc (input_location,
362 result_type, function, n, argarray);
363 set_flags_from_callee (function);
365 decl = get_callee_fndecl (function);
367 if (decl && !TREE_USED (decl))
369 /* We invoke build_call directly for several library
370 functions. These may have been declared normally if
371 we're building libgcc, so we can't just check
372 DECL_ARTIFICIAL. */
373 gcc_assert (DECL_ARTIFICIAL (decl)
374 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
375 "__", 2));
376 mark_used (decl);
379 if (decl && TREE_DEPRECATED (decl))
380 warn_deprecated_use (decl, NULL_TREE);
381 require_complete_eh_spec_types (fntype, decl);
383 TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
385 /* Don't pass empty class objects by value. This is useful
386 for tags in STL, which are used to control overload resolution.
387 We don't need to handle other cases of copying empty classes. */
388 if (! decl || ! DECL_BUILT_IN (decl))
389 for (i = 0; i < n; i++)
391 tree arg = CALL_EXPR_ARG (function, i);
392 if (is_empty_class (TREE_TYPE (arg))
393 && ! TREE_ADDRESSABLE (TREE_TYPE (arg)))
395 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
396 arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
397 CALL_EXPR_ARG (function, i) = arg;
401 return function;
404 /* Build something of the form ptr->method (args)
405 or object.method (args). This can also build
406 calls to constructors, and find friends.
408 Member functions always take their class variable
409 as a pointer.
411 INSTANCE is a class instance.
413 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
415 PARMS help to figure out what that NAME really refers to.
417 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
418 down to the real instance type to use for access checking. We need this
419 information to get protected accesses correct.
421 FLAGS is the logical disjunction of zero or more LOOKUP_
422 flags. See cp-tree.h for more info.
424 If this is all OK, calls build_function_call with the resolved
425 member function.
427 This function must also handle being called to perform
428 initialization, promotion/coercion of arguments, and
429 instantiation of default parameters.
431 Note that NAME may refer to an instance variable name. If
432 `operator()()' is defined for the type of that field, then we return
433 that result. */
435 /* New overloading code. */
437 typedef struct z_candidate z_candidate;
439 typedef struct candidate_warning candidate_warning;
440 struct candidate_warning {
441 z_candidate *loser;
442 candidate_warning *next;
445 /* Information for providing diagnostics about why overloading failed. */
447 enum rejection_reason_code {
448 rr_none,
449 rr_arity,
450 rr_explicit_conversion,
451 rr_template_conversion,
452 rr_arg_conversion,
453 rr_bad_arg_conversion,
454 rr_template_unification,
455 rr_invalid_copy
458 struct conversion_info {
459 /* The index of the argument, 0-based. */
460 int n_arg;
461 /* The actual argument or its type. */
462 tree from;
463 /* The type of the parameter. */
464 tree to_type;
467 struct rejection_reason {
468 enum rejection_reason_code code;
469 union {
470 /* Information about an arity mismatch. */
471 struct {
472 /* The expected number of arguments. */
473 int expected;
474 /* The actual number of arguments in the call. */
475 int actual;
476 /* Whether the call was a varargs call. */
477 bool call_varargs_p;
478 } arity;
479 /* Information about an argument conversion mismatch. */
480 struct conversion_info conversion;
481 /* Same, but for bad argument conversions. */
482 struct conversion_info bad_conversion;
483 /* Information about template unification failures. These are the
484 parameters passed to fn_type_unification. */
485 struct {
486 tree tmpl;
487 tree explicit_targs;
488 int num_targs;
489 const tree *args;
490 unsigned int nargs;
491 tree return_type;
492 unification_kind_t strict;
493 int flags;
494 } template_unification;
495 /* Information about template instantiation failures. These are the
496 parameters passed to instantiate_template. */
497 struct {
498 tree tmpl;
499 tree targs;
500 } template_instantiation;
501 } u;
504 struct z_candidate {
505 /* The FUNCTION_DECL that will be called if this candidate is
506 selected by overload resolution. */
507 tree fn;
508 /* If not NULL_TREE, the first argument to use when calling this
509 function. */
510 tree first_arg;
511 /* The rest of the arguments to use when calling this function. If
512 there are no further arguments this may be NULL or it may be an
513 empty vector. */
514 const vec<tree, va_gc> *args;
515 /* The implicit conversion sequences for each of the arguments to
516 FN. */
517 conversion **convs;
518 /* The number of implicit conversion sequences. */
519 size_t num_convs;
520 /* If FN is a user-defined conversion, the standard conversion
521 sequence from the type returned by FN to the desired destination
522 type. */
523 conversion *second_conv;
524 struct rejection_reason *reason;
525 /* If FN is a member function, the binfo indicating the path used to
526 qualify the name of FN at the call site. This path is used to
527 determine whether or not FN is accessible if it is selected by
528 overload resolution. The DECL_CONTEXT of FN will always be a
529 (possibly improper) base of this binfo. */
530 tree access_path;
531 /* If FN is a non-static member function, the binfo indicating the
532 subobject to which the `this' pointer should be converted if FN
533 is selected by overload resolution. The type pointed to by
534 the `this' pointer must correspond to the most derived class
535 indicated by the CONVERSION_PATH. */
536 tree conversion_path;
537 tree template_decl;
538 tree explicit_targs;
539 candidate_warning *warnings;
540 z_candidate *next;
541 int viable;
543 /* The flags active in add_candidate. */
544 int flags;
547 /* Returns true iff T is a null pointer constant in the sense of
548 [conv.ptr]. */
550 bool
551 null_ptr_cst_p (tree t)
553 /* [conv.ptr]
555 A null pointer constant is an integral constant expression
556 (_expr.const_) rvalue of integer type that evaluates to zero or
557 an rvalue of type std::nullptr_t. */
558 if (NULLPTR_TYPE_P (TREE_TYPE (t)))
559 return true;
560 if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)))
562 /* Core issue 903 says only literal 0 is a null pointer constant. */
563 if (cxx_dialect < cxx11)
564 t = maybe_constant_value (fold_non_dependent_expr_sfinae (t, tf_none));
565 STRIP_NOPS (t);
566 if (integer_zerop (t) && !TREE_OVERFLOW (t))
567 return true;
569 return false;
572 /* Returns true iff T is a null member pointer value (4.11). */
574 bool
575 null_member_pointer_value_p (tree t)
577 tree type = TREE_TYPE (t);
578 if (!type)
579 return false;
580 else if (TYPE_PTRMEMFUNC_P (type))
581 return (TREE_CODE (t) == CONSTRUCTOR
582 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
583 else if (TYPE_PTRDATAMEM_P (type))
584 return integer_all_onesp (t);
585 else
586 return false;
589 /* Returns nonzero if PARMLIST consists of only default parms,
590 ellipsis, and/or undeduced parameter packs. */
592 bool
593 sufficient_parms_p (const_tree parmlist)
595 for (; parmlist && parmlist != void_list_node;
596 parmlist = TREE_CHAIN (parmlist))
597 if (!TREE_PURPOSE (parmlist)
598 && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
599 return false;
600 return true;
603 /* Allocate N bytes of memory from the conversion obstack. The memory
604 is zeroed before being returned. */
606 static void *
607 conversion_obstack_alloc (size_t n)
609 void *p;
610 if (!conversion_obstack_initialized)
612 gcc_obstack_init (&conversion_obstack);
613 conversion_obstack_initialized = true;
615 p = obstack_alloc (&conversion_obstack, n);
616 memset (p, 0, n);
617 return p;
620 /* Allocate rejection reasons. */
622 static struct rejection_reason *
623 alloc_rejection (enum rejection_reason_code code)
625 struct rejection_reason *p;
626 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
627 p->code = code;
628 return p;
631 static struct rejection_reason *
632 arity_rejection (tree first_arg, int expected, int actual)
634 struct rejection_reason *r = alloc_rejection (rr_arity);
635 int adjust = first_arg != NULL_TREE;
636 r->u.arity.expected = expected - adjust;
637 r->u.arity.actual = actual - adjust;
638 return r;
641 static struct rejection_reason *
642 arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
644 struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
645 int adjust = first_arg != NULL_TREE;
646 r->u.conversion.n_arg = n_arg - adjust;
647 r->u.conversion.from = from;
648 r->u.conversion.to_type = to;
649 return r;
652 static struct rejection_reason *
653 bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
655 struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
656 int adjust = first_arg != NULL_TREE;
657 r->u.bad_conversion.n_arg = n_arg - adjust;
658 r->u.bad_conversion.from = from;
659 r->u.bad_conversion.to_type = to;
660 return r;
663 static struct rejection_reason *
664 explicit_conversion_rejection (tree from, tree to)
666 struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
667 r->u.conversion.n_arg = 0;
668 r->u.conversion.from = from;
669 r->u.conversion.to_type = to;
670 return r;
673 static struct rejection_reason *
674 template_conversion_rejection (tree from, tree to)
676 struct rejection_reason *r = alloc_rejection (rr_template_conversion);
677 r->u.conversion.n_arg = 0;
678 r->u.conversion.from = from;
679 r->u.conversion.to_type = to;
680 return r;
683 static struct rejection_reason *
684 template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
685 const tree *args, unsigned int nargs,
686 tree return_type, unification_kind_t strict,
687 int flags)
689 size_t args_n_bytes = sizeof (*args) * nargs;
690 tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
691 struct rejection_reason *r = alloc_rejection (rr_template_unification);
692 r->u.template_unification.tmpl = tmpl;
693 r->u.template_unification.explicit_targs = explicit_targs;
694 r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
695 /* Copy args to our own storage. */
696 memcpy (args1, args, args_n_bytes);
697 r->u.template_unification.args = args1;
698 r->u.template_unification.nargs = nargs;
699 r->u.template_unification.return_type = return_type;
700 r->u.template_unification.strict = strict;
701 r->u.template_unification.flags = flags;
702 return r;
705 static struct rejection_reason *
706 template_unification_error_rejection (void)
708 return alloc_rejection (rr_template_unification);
711 static struct rejection_reason *
712 invalid_copy_with_fn_template_rejection (void)
714 struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
715 return r;
718 /* Dynamically allocate a conversion. */
720 static conversion *
721 alloc_conversion (conversion_kind kind)
723 conversion *c;
724 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
725 c->kind = kind;
726 return c;
729 #ifdef ENABLE_CHECKING
731 /* Make sure that all memory on the conversion obstack has been
732 freed. */
734 void
735 validate_conversion_obstack (void)
737 if (conversion_obstack_initialized)
738 gcc_assert ((obstack_next_free (&conversion_obstack)
739 == obstack_base (&conversion_obstack)));
742 #endif /* ENABLE_CHECKING */
744 /* Dynamically allocate an array of N conversions. */
746 static conversion **
747 alloc_conversions (size_t n)
749 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
752 static conversion *
753 build_conv (conversion_kind code, tree type, conversion *from)
755 conversion *t;
756 conversion_rank rank = CONVERSION_RANK (from);
758 /* Note that the caller is responsible for filling in t->cand for
759 user-defined conversions. */
760 t = alloc_conversion (code);
761 t->type = type;
762 t->u.next = from;
764 switch (code)
766 case ck_ptr:
767 case ck_pmem:
768 case ck_base:
769 case ck_std:
770 if (rank < cr_std)
771 rank = cr_std;
772 break;
774 case ck_qual:
775 if (rank < cr_exact)
776 rank = cr_exact;
777 break;
779 default:
780 break;
782 t->rank = rank;
783 t->user_conv_p = (code == ck_user || from->user_conv_p);
784 t->bad_p = from->bad_p;
785 t->base_p = false;
786 return t;
789 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
790 specialization of std::initializer_list<T>, if such a conversion is
791 possible. */
793 static conversion *
794 build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
796 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
797 unsigned len = CONSTRUCTOR_NELTS (ctor);
798 conversion **subconvs = alloc_conversions (len);
799 conversion *t;
800 unsigned i;
801 tree val;
803 /* Within a list-initialization we can have more user-defined
804 conversions. */
805 flags &= ~LOOKUP_NO_CONVERSION;
806 /* But no narrowing conversions. */
807 flags |= LOOKUP_NO_NARROWING;
809 /* Can't make an array of these types. */
810 if (TREE_CODE (elttype) == REFERENCE_TYPE
811 || TREE_CODE (elttype) == FUNCTION_TYPE
812 || VOID_TYPE_P (elttype))
813 return NULL;
815 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
817 conversion *sub
818 = implicit_conversion (elttype, TREE_TYPE (val), val,
819 false, flags, complain);
820 if (sub == NULL)
821 return NULL;
823 subconvs[i] = sub;
826 t = alloc_conversion (ck_list);
827 t->type = type;
828 t->u.list = subconvs;
829 t->rank = cr_exact;
831 for (i = 0; i < len; ++i)
833 conversion *sub = subconvs[i];
834 if (sub->rank > t->rank)
835 t->rank = sub->rank;
836 if (sub->user_conv_p)
837 t->user_conv_p = true;
838 if (sub->bad_p)
839 t->bad_p = true;
842 return t;
845 /* Return the next conversion of the conversion chain (if applicable),
846 or NULL otherwise. Please use this function instead of directly
847 accessing fields of struct conversion. */
849 static conversion *
850 next_conversion (conversion *conv)
852 if (conv == NULL
853 || conv->kind == ck_identity
854 || conv->kind == ck_ambig
855 || conv->kind == ck_list)
856 return NULL;
857 return conv->u.next;
860 /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
861 is a valid aggregate initializer for array type ATYPE. */
863 static bool
864 can_convert_array (tree atype, tree ctor, int flags, tsubst_flags_t complain)
866 unsigned i;
867 tree elttype = TREE_TYPE (atype);
868 for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
870 tree val = CONSTRUCTOR_ELT (ctor, i)->value;
871 bool ok;
872 if (TREE_CODE (elttype) == ARRAY_TYPE
873 && TREE_CODE (val) == CONSTRUCTOR)
874 ok = can_convert_array (elttype, val, flags, complain);
875 else
876 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
877 complain);
878 if (!ok)
879 return false;
881 return true;
884 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
885 aggregate class, if such a conversion is possible. */
887 static conversion *
888 build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
890 unsigned HOST_WIDE_INT i = 0;
891 conversion *c;
892 tree field = next_initializable_field (TYPE_FIELDS (type));
893 tree empty_ctor = NULL_TREE;
895 ctor = reshape_init (type, ctor, tf_none);
896 if (ctor == error_mark_node)
897 return NULL;
899 /* The conversions within the init-list aren't affected by the enclosing
900 context; they're always simple copy-initialization. */
901 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
903 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
905 tree ftype = TREE_TYPE (field);
906 tree val;
907 bool ok;
909 if (i < CONSTRUCTOR_NELTS (ctor))
910 val = CONSTRUCTOR_ELT (ctor, i)->value;
911 else if (TREE_CODE (ftype) == REFERENCE_TYPE)
912 /* Value-initialization of reference is ill-formed. */
913 return NULL;
914 else
916 if (empty_ctor == NULL_TREE)
917 empty_ctor = build_constructor (init_list_type_node, NULL);
918 val = empty_ctor;
920 ++i;
922 if (TREE_CODE (ftype) == ARRAY_TYPE
923 && TREE_CODE (val) == CONSTRUCTOR)
924 ok = can_convert_array (ftype, val, flags, complain);
925 else
926 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
927 complain);
929 if (!ok)
930 return NULL;
932 if (TREE_CODE (type) == UNION_TYPE)
933 break;
936 if (i < CONSTRUCTOR_NELTS (ctor))
937 return NULL;
939 c = alloc_conversion (ck_aggr);
940 c->type = type;
941 c->rank = cr_exact;
942 c->user_conv_p = true;
943 c->check_narrowing = true;
944 c->u.next = NULL;
945 return c;
948 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
949 array type, if such a conversion is possible. */
951 static conversion *
952 build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
954 conversion *c;
955 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
956 tree elttype = TREE_TYPE (type);
957 unsigned i;
958 tree val;
959 bool bad = false;
960 bool user = false;
961 enum conversion_rank rank = cr_exact;
963 /* We might need to propagate the size from the element to the array. */
964 complete_type (type);
966 if (TYPE_DOMAIN (type)
967 && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
969 unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
970 if (alen < len)
971 return NULL;
974 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
976 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
978 conversion *sub
979 = implicit_conversion (elttype, TREE_TYPE (val), val,
980 false, flags, complain);
981 if (sub == NULL)
982 return NULL;
984 if (sub->rank > rank)
985 rank = sub->rank;
986 if (sub->user_conv_p)
987 user = true;
988 if (sub->bad_p)
989 bad = true;
992 c = alloc_conversion (ck_aggr);
993 c->type = type;
994 c->rank = rank;
995 c->user_conv_p = user;
996 c->bad_p = bad;
997 c->u.next = NULL;
998 return c;
1001 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1002 complex type, if such a conversion is possible. */
1004 static conversion *
1005 build_complex_conv (tree type, tree ctor, int flags,
1006 tsubst_flags_t complain)
1008 conversion *c;
1009 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1010 tree elttype = TREE_TYPE (type);
1011 unsigned i;
1012 tree val;
1013 bool bad = false;
1014 bool user = false;
1015 enum conversion_rank rank = cr_exact;
1017 if (len != 2)
1018 return NULL;
1020 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1022 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
1024 conversion *sub
1025 = implicit_conversion (elttype, TREE_TYPE (val), val,
1026 false, flags, complain);
1027 if (sub == NULL)
1028 return NULL;
1030 if (sub->rank > rank)
1031 rank = sub->rank;
1032 if (sub->user_conv_p)
1033 user = true;
1034 if (sub->bad_p)
1035 bad = true;
1038 c = alloc_conversion (ck_aggr);
1039 c->type = type;
1040 c->rank = rank;
1041 c->user_conv_p = user;
1042 c->bad_p = bad;
1043 c->u.next = NULL;
1044 return c;
1047 /* Build a representation of the identity conversion from EXPR to
1048 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1050 static conversion *
1051 build_identity_conv (tree type, tree expr)
1053 conversion *c;
1055 c = alloc_conversion (ck_identity);
1056 c->type = type;
1057 c->u.expr = expr;
1059 return c;
1062 /* Converting from EXPR to TYPE was ambiguous in the sense that there
1063 were multiple user-defined conversions to accomplish the job.
1064 Build a conversion that indicates that ambiguity. */
1066 static conversion *
1067 build_ambiguous_conv (tree type, tree expr)
1069 conversion *c;
1071 c = alloc_conversion (ck_ambig);
1072 c->type = type;
1073 c->u.expr = expr;
1075 return c;
1078 tree
1079 strip_top_quals (tree t)
1081 if (TREE_CODE (t) == ARRAY_TYPE)
1082 return t;
1083 return cp_build_qualified_type (t, 0);
1086 /* Returns the standard conversion path (see [conv]) from type FROM to type
1087 TO, if any. For proper handling of null pointer constants, you must
1088 also pass the expression EXPR to convert from. If C_CAST_P is true,
1089 this conversion is coming from a C-style cast. */
1091 static conversion *
1092 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1093 int flags)
1095 enum tree_code fcode, tcode;
1096 conversion *conv;
1097 bool fromref = false;
1098 tree qualified_to;
1100 to = non_reference (to);
1101 if (TREE_CODE (from) == REFERENCE_TYPE)
1103 fromref = true;
1104 from = TREE_TYPE (from);
1106 qualified_to = to;
1107 to = strip_top_quals (to);
1108 from = strip_top_quals (from);
1110 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1111 && expr && type_unknown_p (expr))
1113 tsubst_flags_t tflags = tf_conv;
1114 expr = instantiate_type (to, expr, tflags);
1115 if (expr == error_mark_node)
1116 return NULL;
1117 from = TREE_TYPE (expr);
1120 fcode = TREE_CODE (from);
1121 tcode = TREE_CODE (to);
1123 conv = build_identity_conv (from, expr);
1124 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1126 from = type_decays_to (from);
1127 fcode = TREE_CODE (from);
1128 conv = build_conv (ck_lvalue, from, conv);
1130 else if (fromref || (expr && lvalue_p (expr)))
1132 if (expr)
1134 tree bitfield_type;
1135 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1136 if (bitfield_type)
1138 from = strip_top_quals (bitfield_type);
1139 fcode = TREE_CODE (from);
1142 conv = build_conv (ck_rvalue, from, conv);
1143 if (flags & LOOKUP_PREFER_RVALUE)
1144 conv->rvaluedness_matches_p = true;
1147 /* Allow conversion between `__complex__' data types. */
1148 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1150 /* The standard conversion sequence to convert FROM to TO is
1151 the standard conversion sequence to perform componentwise
1152 conversion. */
1153 conversion *part_conv = standard_conversion
1154 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags);
1156 if (part_conv)
1158 conv = build_conv (part_conv->kind, to, conv);
1159 conv->rank = part_conv->rank;
1161 else
1162 conv = NULL;
1164 return conv;
1167 if (same_type_p (from, to))
1169 if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1170 conv->type = qualified_to;
1171 return conv;
1174 /* [conv.ptr]
1175 A null pointer constant can be converted to a pointer type; ... A
1176 null pointer constant of integral type can be converted to an
1177 rvalue of type std::nullptr_t. */
1178 if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1179 || NULLPTR_TYPE_P (to))
1180 && expr && null_ptr_cst_p (expr))
1181 conv = build_conv (ck_std, to, conv);
1182 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1183 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1185 /* For backwards brain damage compatibility, allow interconversion of
1186 pointers and integers with a pedwarn. */
1187 conv = build_conv (ck_std, to, conv);
1188 conv->bad_p = true;
1190 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1192 /* For backwards brain damage compatibility, allow interconversion of
1193 enums and integers with a pedwarn. */
1194 conv = build_conv (ck_std, to, conv);
1195 conv->bad_p = true;
1197 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1198 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1200 tree to_pointee;
1201 tree from_pointee;
1203 if (tcode == POINTER_TYPE
1204 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
1205 TREE_TYPE (to)))
1207 else if (VOID_TYPE_P (TREE_TYPE (to))
1208 && !TYPE_PTRDATAMEM_P (from)
1209 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
1211 tree nfrom = TREE_TYPE (from);
1212 /* Don't try to apply restrict to void. */
1213 int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1214 from = build_pointer_type
1215 (cp_build_qualified_type (void_type_node, quals));
1216 conv = build_conv (ck_ptr, from, conv);
1218 else if (TYPE_PTRDATAMEM_P (from))
1220 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1221 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1223 if (DERIVED_FROM_P (fbase, tbase)
1224 && (same_type_ignoring_top_level_qualifiers_p
1225 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
1226 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
1228 from = build_ptrmem_type (tbase,
1229 TYPE_PTRMEM_POINTED_TO_TYPE (from));
1230 conv = build_conv (ck_pmem, from, conv);
1232 else if (!same_type_p (fbase, tbase))
1233 return NULL;
1235 else if (CLASS_TYPE_P (TREE_TYPE (from))
1236 && CLASS_TYPE_P (TREE_TYPE (to))
1237 /* [conv.ptr]
1239 An rvalue of type "pointer to cv D," where D is a
1240 class type, can be converted to an rvalue of type
1241 "pointer to cv B," where B is a base class (clause
1242 _class.derived_) of D. If B is an inaccessible
1243 (clause _class.access_) or ambiguous
1244 (_class.member.lookup_) base class of D, a program
1245 that necessitates this conversion is ill-formed.
1246 Therefore, we use DERIVED_FROM_P, and do not check
1247 access or uniqueness. */
1248 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
1250 from =
1251 cp_build_qualified_type (TREE_TYPE (to),
1252 cp_type_quals (TREE_TYPE (from)));
1253 from = build_pointer_type (from);
1254 conv = build_conv (ck_ptr, from, conv);
1255 conv->base_p = true;
1258 if (tcode == POINTER_TYPE)
1260 to_pointee = TREE_TYPE (to);
1261 from_pointee = TREE_TYPE (from);
1263 else
1265 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1266 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1269 if (same_type_p (from, to))
1270 /* OK */;
1271 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
1272 /* In a C-style cast, we ignore CV-qualification because we
1273 are allowed to perform a static_cast followed by a
1274 const_cast. */
1275 conv = build_conv (ck_qual, to, conv);
1276 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1277 conv = build_conv (ck_qual, to, conv);
1278 else if (expr && string_conv_p (to, expr, 0))
1279 /* converting from string constant to char *. */
1280 conv = build_conv (ck_qual, to, conv);
1281 /* Allow conversions among compatible ObjC pointer types (base
1282 conversions have been already handled above). */
1283 else if (c_dialect_objc ()
1284 && objc_compare_types (to, from, -4, NULL_TREE))
1285 conv = build_conv (ck_ptr, to, conv);
1286 else if (ptr_reasonably_similar (to_pointee, from_pointee))
1288 conv = build_conv (ck_ptr, to, conv);
1289 conv->bad_p = true;
1291 else
1292 return NULL;
1294 from = to;
1296 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1298 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1299 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1300 tree fbase = class_of_this_parm (fromfn);
1301 tree tbase = class_of_this_parm (tofn);
1303 if (!DERIVED_FROM_P (fbase, tbase)
1304 || !same_type_p (static_fn_type (fromfn),
1305 static_fn_type (tofn)))
1306 return NULL;
1308 from = build_memfn_type (fromfn,
1309 tbase,
1310 cp_type_quals (tbase),
1311 type_memfn_rqual (tofn));
1312 from = build_ptrmemfunc_type (build_pointer_type (from));
1313 conv = build_conv (ck_pmem, from, conv);
1314 conv->base_p = true;
1316 else if (tcode == BOOLEAN_TYPE)
1318 /* [conv.bool]
1320 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1321 to member type can be converted to a prvalue of type bool. ...
1322 For direct-initialization (8.5 [dcl.init]), a prvalue of type
1323 std::nullptr_t can be converted to a prvalue of type bool; */
1324 if (ARITHMETIC_TYPE_P (from)
1325 || UNSCOPED_ENUM_P (from)
1326 || fcode == POINTER_TYPE
1327 || TYPE_PTRMEM_P (from)
1328 || NULLPTR_TYPE_P (from))
1330 conv = build_conv (ck_std, to, conv);
1331 if (fcode == POINTER_TYPE
1332 || TYPE_PTRDATAMEM_P (from)
1333 || (TYPE_PTRMEMFUNC_P (from)
1334 && conv->rank < cr_pbool)
1335 || NULLPTR_TYPE_P (from))
1336 conv->rank = cr_pbool;
1337 if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1338 conv->bad_p = true;
1339 return conv;
1342 return NULL;
1344 /* We don't check for ENUMERAL_TYPE here because there are no standard
1345 conversions to enum type. */
1346 /* As an extension, allow conversion to complex type. */
1347 else if (ARITHMETIC_TYPE_P (to))
1349 if (! (INTEGRAL_CODE_P (fcode)
1350 || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1351 || SCOPED_ENUM_P (from))
1352 return NULL;
1353 conv = build_conv (ck_std, to, conv);
1355 /* Give this a better rank if it's a promotion. */
1356 if (same_type_p (to, type_promotes_to (from))
1357 && next_conversion (conv)->rank <= cr_promotion)
1358 conv->rank = cr_promotion;
1360 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1361 && vector_types_convertible_p (from, to, false))
1362 return build_conv (ck_std, to, conv);
1363 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1364 && is_properly_derived_from (from, to))
1366 if (conv->kind == ck_rvalue)
1367 conv = next_conversion (conv);
1368 conv = build_conv (ck_base, to, conv);
1369 /* The derived-to-base conversion indicates the initialization
1370 of a parameter with base type from an object of a derived
1371 type. A temporary object is created to hold the result of
1372 the conversion unless we're binding directly to a reference. */
1373 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1375 else
1376 return NULL;
1378 if (flags & LOOKUP_NO_NARROWING)
1379 conv->check_narrowing = true;
1381 return conv;
1384 /* Returns nonzero if T1 is reference-related to T2. */
1386 bool
1387 reference_related_p (tree t1, tree t2)
1389 if (t1 == error_mark_node || t2 == error_mark_node)
1390 return false;
1392 t1 = TYPE_MAIN_VARIANT (t1);
1393 t2 = TYPE_MAIN_VARIANT (t2);
1395 /* [dcl.init.ref]
1397 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1398 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
1399 of T2. */
1400 return (same_type_p (t1, t2)
1401 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1402 && DERIVED_FROM_P (t1, t2)));
1405 /* Returns nonzero if T1 is reference-compatible with T2. */
1407 static bool
1408 reference_compatible_p (tree t1, tree t2)
1410 /* [dcl.init.ref]
1412 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
1413 reference-related to T2 and cv1 is the same cv-qualification as,
1414 or greater cv-qualification than, cv2. */
1415 return (reference_related_p (t1, t2)
1416 && at_least_as_qualified_p (t1, t2));
1419 /* A reference of the indicated TYPE is being bound directly to the
1420 expression represented by the implicit conversion sequence CONV.
1421 Return a conversion sequence for this binding. */
1423 static conversion *
1424 direct_reference_binding (tree type, conversion *conv)
1426 tree t;
1428 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1429 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1431 t = TREE_TYPE (type);
1433 /* [over.ics.rank]
1435 When a parameter of reference type binds directly
1436 (_dcl.init.ref_) to an argument expression, the implicit
1437 conversion sequence is the identity conversion, unless the
1438 argument expression has a type that is a derived class of the
1439 parameter type, in which case the implicit conversion sequence is
1440 a derived-to-base Conversion.
1442 If the parameter binds directly to the result of applying a
1443 conversion function to the argument expression, the implicit
1444 conversion sequence is a user-defined conversion sequence
1445 (_over.ics.user_), with the second standard conversion sequence
1446 either an identity conversion or, if the conversion function
1447 returns an entity of a type that is a derived class of the
1448 parameter type, a derived-to-base conversion. */
1449 if (!same_type_ignoring_top_level_qualifiers_p (t, conv->type))
1451 /* Represent the derived-to-base conversion. */
1452 conv = build_conv (ck_base, t, conv);
1453 /* We will actually be binding to the base-class subobject in
1454 the derived class, so we mark this conversion appropriately.
1455 That way, convert_like knows not to generate a temporary. */
1456 conv->need_temporary_p = false;
1458 return build_conv (ck_ref_bind, type, conv);
1461 /* Returns the conversion path from type FROM to reference type TO for
1462 purposes of reference binding. For lvalue binding, either pass a
1463 reference type to FROM or an lvalue expression to EXPR. If the
1464 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1465 the conversion returned. If C_CAST_P is true, this
1466 conversion is coming from a C-style cast. */
1468 static conversion *
1469 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1470 tsubst_flags_t complain)
1472 conversion *conv = NULL;
1473 tree to = TREE_TYPE (rto);
1474 tree from = rfrom;
1475 tree tfrom;
1476 bool related_p;
1477 bool compatible_p;
1478 cp_lvalue_kind gl_kind;
1479 bool is_lvalue;
1481 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1483 expr = instantiate_type (to, expr, tf_none);
1484 if (expr == error_mark_node)
1485 return NULL;
1486 from = TREE_TYPE (expr);
1489 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1491 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1492 /* DR 1288: Otherwise, if the initializer list has a single element
1493 of type E and ... [T's] referenced type is reference-related to E,
1494 the object or reference is initialized from that element... */
1495 if (CONSTRUCTOR_NELTS (expr) == 1)
1497 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1498 if (error_operand_p (elt))
1499 return NULL;
1500 tree etype = TREE_TYPE (elt);
1501 if (reference_related_p (to, etype))
1503 expr = elt;
1504 from = etype;
1505 goto skip;
1508 /* Otherwise, if T is a reference type, a prvalue temporary of the
1509 type referenced by T is copy-list-initialized or
1510 direct-list-initialized, depending on the kind of initialization
1511 for the reference, and the reference is bound to that temporary. */
1512 conv = implicit_conversion (to, from, expr, c_cast_p,
1513 flags|LOOKUP_NO_TEMP_BIND, complain);
1514 skip:;
1517 if (TREE_CODE (from) == REFERENCE_TYPE)
1519 from = TREE_TYPE (from);
1520 if (!TYPE_REF_IS_RVALUE (rfrom)
1521 || TREE_CODE (from) == FUNCTION_TYPE)
1522 gl_kind = clk_ordinary;
1523 else
1524 gl_kind = clk_rvalueref;
1526 else if (expr)
1528 gl_kind = lvalue_kind (expr);
1529 if (gl_kind & clk_class)
1530 /* A class prvalue is not a glvalue. */
1531 gl_kind = clk_none;
1533 else
1534 gl_kind = clk_none;
1535 is_lvalue = gl_kind && !(gl_kind & clk_rvalueref);
1537 tfrom = from;
1538 if ((gl_kind & clk_bitfield) != 0)
1539 tfrom = unlowered_expr_type (expr);
1541 /* Figure out whether or not the types are reference-related and
1542 reference compatible. We have do do this after stripping
1543 references from FROM. */
1544 related_p = reference_related_p (to, tfrom);
1545 /* If this is a C cast, first convert to an appropriately qualified
1546 type, so that we can later do a const_cast to the desired type. */
1547 if (related_p && c_cast_p
1548 && !at_least_as_qualified_p (to, tfrom))
1549 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1550 compatible_p = reference_compatible_p (to, tfrom);
1552 /* Directly bind reference when target expression's type is compatible with
1553 the reference and expression is an lvalue. In DR391, the wording in
1554 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1555 const and rvalue references to rvalues of compatible class type.
1556 We should also do direct bindings for non-class xvalues. */
1557 if (related_p
1558 && (gl_kind
1559 || (!(flags & LOOKUP_NO_TEMP_BIND)
1560 && (CLASS_TYPE_P (from)
1561 || TREE_CODE (from) == ARRAY_TYPE))))
1563 /* [dcl.init.ref]
1565 If the initializer expression
1567 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1568 is reference-compatible with "cv2 T2,"
1570 the reference is bound directly to the initializer expression
1571 lvalue.
1573 [...]
1574 If the initializer expression is an rvalue, with T2 a class type,
1575 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1576 is bound to the object represented by the rvalue or to a sub-object
1577 within that object. */
1579 conv = build_identity_conv (tfrom, expr);
1580 conv = direct_reference_binding (rto, conv);
1582 if (flags & LOOKUP_PREFER_RVALUE)
1583 /* The top-level caller requested that we pretend that the lvalue
1584 be treated as an rvalue. */
1585 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1586 else if (TREE_CODE (rfrom) == REFERENCE_TYPE)
1587 /* Handle rvalue reference to function properly. */
1588 conv->rvaluedness_matches_p
1589 = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1590 else
1591 conv->rvaluedness_matches_p
1592 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1594 if ((gl_kind & clk_bitfield) != 0
1595 || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1596 /* For the purposes of overload resolution, we ignore the fact
1597 this expression is a bitfield or packed field. (In particular,
1598 [over.ics.ref] says specifically that a function with a
1599 non-const reference parameter is viable even if the
1600 argument is a bitfield.)
1602 However, when we actually call the function we must create
1603 a temporary to which to bind the reference. If the
1604 reference is volatile, or isn't const, then we cannot make
1605 a temporary, so we just issue an error when the conversion
1606 actually occurs. */
1607 conv->need_temporary_p = true;
1609 /* Don't allow binding of lvalues (other than function lvalues) to
1610 rvalue references. */
1611 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1612 && TREE_CODE (to) != FUNCTION_TYPE
1613 && !(flags & LOOKUP_PREFER_RVALUE))
1614 conv->bad_p = true;
1616 /* Nor the reverse. */
1617 if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
1618 && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
1619 || (flags & LOOKUP_NO_RVAL_BIND))
1620 && TREE_CODE (to) != FUNCTION_TYPE)
1621 conv->bad_p = true;
1623 if (!compatible_p)
1624 conv->bad_p = true;
1626 return conv;
1628 /* [class.conv.fct] A conversion function is never used to convert a
1629 (possibly cv-qualified) object to the (possibly cv-qualified) same
1630 object type (or a reference to it), to a (possibly cv-qualified) base
1631 class of that type (or a reference to it).... */
1632 else if (CLASS_TYPE_P (from) && !related_p
1633 && !(flags & LOOKUP_NO_CONVERSION))
1635 /* [dcl.init.ref]
1637 If the initializer expression
1639 -- has a class type (i.e., T2 is a class type) can be
1640 implicitly converted to an lvalue of type "cv3 T3," where
1641 "cv1 T1" is reference-compatible with "cv3 T3". (this
1642 conversion is selected by enumerating the applicable
1643 conversion functions (_over.match.ref_) and choosing the
1644 best one through overload resolution. (_over.match_).
1646 the reference is bound to the lvalue result of the conversion
1647 in the second case. */
1648 z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
1649 complain);
1650 if (cand)
1651 return cand->second_conv;
1654 /* From this point on, we conceptually need temporaries, even if we
1655 elide them. Only the cases above are "direct bindings". */
1656 if (flags & LOOKUP_NO_TEMP_BIND)
1657 return NULL;
1659 /* [over.ics.rank]
1661 When a parameter of reference type is not bound directly to an
1662 argument expression, the conversion sequence is the one required
1663 to convert the argument expression to the underlying type of the
1664 reference according to _over.best.ics_. Conceptually, this
1665 conversion sequence corresponds to copy-initializing a temporary
1666 of the underlying type with the argument expression. Any
1667 difference in top-level cv-qualification is subsumed by the
1668 initialization itself and does not constitute a conversion. */
1670 /* We're generating a temporary now, but don't bind any more in the
1671 conversion (specifically, don't slice the temporary returned by a
1672 conversion operator). */
1673 flags |= LOOKUP_NO_TEMP_BIND;
1675 /* Core issue 899: When [copy-]initializing a temporary to be bound
1676 to the first parameter of a copy constructor (12.8) called with
1677 a single argument in the context of direct-initialization,
1678 explicit conversion functions are also considered.
1680 So don't set LOOKUP_ONLYCONVERTING in that case. */
1681 if (!(flags & LOOKUP_COPY_PARM))
1682 flags |= LOOKUP_ONLYCONVERTING;
1684 if (!conv)
1685 conv = implicit_conversion (to, from, expr, c_cast_p,
1686 flags, complain);
1687 if (!conv)
1688 return NULL;
1690 if (conv->user_conv_p)
1692 /* If initializing the temporary used a conversion function,
1693 recalculate the second conversion sequence. */
1694 for (conversion *t = conv; t; t = next_conversion (t))
1695 if (t->kind == ck_user
1696 && DECL_CONV_FN_P (t->cand->fn))
1698 tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
1699 int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
1700 conversion *new_second
1701 = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
1702 sflags, complain);
1703 if (!new_second)
1704 return NULL;
1705 return merge_conversion_sequences (t, new_second);
1709 conv = build_conv (ck_ref_bind, rto, conv);
1710 /* This reference binding, unlike those above, requires the
1711 creation of a temporary. */
1712 conv->need_temporary_p = true;
1713 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1715 /* [dcl.init.ref]
1717 Otherwise, the reference shall be an lvalue reference to a
1718 non-volatile const type, or the reference shall be an rvalue
1719 reference. */
1720 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1721 conv->bad_p = true;
1723 /* [dcl.init.ref]
1725 Otherwise, a temporary of type "cv1 T1" is created and
1726 initialized from the initializer expression using the rules for a
1727 non-reference copy initialization. If T1 is reference-related to
1728 T2, cv1 must be the same cv-qualification as, or greater
1729 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1730 if (related_p && !at_least_as_qualified_p (to, from))
1731 conv->bad_p = true;
1733 return conv;
1736 /* Returns the implicit conversion sequence (see [over.ics]) from type
1737 FROM to type TO. The optional expression EXPR may affect the
1738 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1739 true, this conversion is coming from a C-style cast. */
1741 static conversion *
1742 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1743 int flags, tsubst_flags_t complain)
1745 conversion *conv;
1747 if (from == error_mark_node || to == error_mark_node
1748 || expr == error_mark_node)
1749 return NULL;
1751 /* Other flags only apply to the primary function in overload
1752 resolution, or after we've chosen one. */
1753 flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
1754 |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE
1755 |LOOKUP_NO_NARROWING|LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL);
1757 /* FIXME: actually we don't want warnings either, but we can't just
1758 have 'complain &= ~(tf_warning|tf_error)' because it would cause
1759 the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
1760 We really ought not to issue that warning until we've committed
1761 to that conversion. */
1762 complain &= ~tf_error;
1764 if (TREE_CODE (to) == REFERENCE_TYPE)
1765 conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
1766 else
1767 conv = standard_conversion (to, from, expr, c_cast_p, flags);
1769 if (conv)
1770 return conv;
1772 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1774 if (is_std_init_list (to))
1775 return build_list_conv (to, expr, flags, complain);
1777 /* As an extension, allow list-initialization of _Complex. */
1778 if (TREE_CODE (to) == COMPLEX_TYPE)
1780 conv = build_complex_conv (to, expr, flags, complain);
1781 if (conv)
1782 return conv;
1785 /* Allow conversion from an initializer-list with one element to a
1786 scalar type. */
1787 if (SCALAR_TYPE_P (to))
1789 int nelts = CONSTRUCTOR_NELTS (expr);
1790 tree elt;
1792 if (nelts == 0)
1793 elt = build_value_init (to, tf_none);
1794 else if (nelts == 1)
1795 elt = CONSTRUCTOR_ELT (expr, 0)->value;
1796 else
1797 elt = error_mark_node;
1799 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
1800 c_cast_p, flags, complain);
1801 if (conv)
1803 conv->check_narrowing = true;
1804 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
1805 /* Too many levels of braces, i.e. '{{1}}'. */
1806 conv->bad_p = true;
1807 return conv;
1810 else if (TREE_CODE (to) == ARRAY_TYPE)
1811 return build_array_conv (to, expr, flags, complain);
1814 if (expr != NULL_TREE
1815 && (MAYBE_CLASS_TYPE_P (from)
1816 || MAYBE_CLASS_TYPE_P (to))
1817 && (flags & LOOKUP_NO_CONVERSION) == 0)
1819 struct z_candidate *cand;
1821 if (CLASS_TYPE_P (to)
1822 && BRACE_ENCLOSED_INITIALIZER_P (expr)
1823 && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
1824 return build_aggr_conv (to, expr, flags, complain);
1826 cand = build_user_type_conversion_1 (to, expr, flags, complain);
1827 if (cand)
1828 conv = cand->second_conv;
1830 /* We used to try to bind a reference to a temporary here, but that
1831 is now handled after the recursive call to this function at the end
1832 of reference_binding. */
1833 return conv;
1836 return NULL;
1839 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1840 functions. ARGS will not be changed until a single candidate is
1841 selected. */
1843 static struct z_candidate *
1844 add_candidate (struct z_candidate **candidates,
1845 tree fn, tree first_arg, const vec<tree, va_gc> *args,
1846 size_t num_convs, conversion **convs,
1847 tree access_path, tree conversion_path,
1848 int viable, struct rejection_reason *reason,
1849 int flags)
1851 struct z_candidate *cand = (struct z_candidate *)
1852 conversion_obstack_alloc (sizeof (struct z_candidate));
1854 cand->fn = fn;
1855 cand->first_arg = first_arg;
1856 cand->args = args;
1857 cand->convs = convs;
1858 cand->num_convs = num_convs;
1859 cand->access_path = access_path;
1860 cand->conversion_path = conversion_path;
1861 cand->viable = viable;
1862 cand->reason = reason;
1863 cand->next = *candidates;
1864 cand->flags = flags;
1865 *candidates = cand;
1867 return cand;
1870 /* Return the number of remaining arguments in the parameter list
1871 beginning with ARG. */
1873 static int
1874 remaining_arguments (tree arg)
1876 int n;
1878 for (n = 0; arg != NULL_TREE && arg != void_list_node;
1879 arg = TREE_CHAIN (arg))
1880 n++;
1882 return n;
1885 /* Create an overload candidate for the function or method FN called
1886 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
1887 FLAGS is passed on to implicit_conversion.
1889 This does not change ARGS.
1891 CTYPE, if non-NULL, is the type we want to pretend this function
1892 comes from for purposes of overload resolution. */
1894 static struct z_candidate *
1895 add_function_candidate (struct z_candidate **candidates,
1896 tree fn, tree ctype, tree first_arg,
1897 const vec<tree, va_gc> *args, tree access_path,
1898 tree conversion_path, int flags,
1899 tsubst_flags_t complain)
1901 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1902 int i, len;
1903 conversion **convs;
1904 tree parmnode;
1905 tree orig_first_arg = first_arg;
1906 int skip;
1907 int viable = 1;
1908 struct rejection_reason *reason = NULL;
1910 /* At this point we should not see any functions which haven't been
1911 explicitly declared, except for friend functions which will have
1912 been found using argument dependent lookup. */
1913 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
1915 /* The `this', `in_chrg' and VTT arguments to constructors are not
1916 considered in overload resolution. */
1917 if (DECL_CONSTRUCTOR_P (fn))
1919 parmlist = skip_artificial_parms_for (fn, parmlist);
1920 skip = num_artificial_parms_for (fn);
1921 if (skip > 0 && first_arg != NULL_TREE)
1923 --skip;
1924 first_arg = NULL_TREE;
1927 else
1928 skip = 0;
1930 len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
1931 convs = alloc_conversions (len);
1933 /* 13.3.2 - Viable functions [over.match.viable]
1934 First, to be a viable function, a candidate function shall have enough
1935 parameters to agree in number with the arguments in the list.
1937 We need to check this first; otherwise, checking the ICSes might cause
1938 us to produce an ill-formed template instantiation. */
1940 parmnode = parmlist;
1941 for (i = 0; i < len; ++i)
1943 if (parmnode == NULL_TREE || parmnode == void_list_node)
1944 break;
1945 parmnode = TREE_CHAIN (parmnode);
1948 if ((i < len && parmnode)
1949 || !sufficient_parms_p (parmnode))
1951 int remaining = remaining_arguments (parmnode);
1952 viable = 0;
1953 reason = arity_rejection (first_arg, i + remaining, len);
1955 /* When looking for a function from a subobject from an implicit
1956 copy/move constructor/operator=, don't consider anything that takes (a
1957 reference to) an unrelated type. See c++/44909 and core 1092. */
1958 else if (parmlist && (flags & LOOKUP_DEFAULTED))
1960 if (DECL_CONSTRUCTOR_P (fn))
1961 i = 1;
1962 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1963 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
1964 i = 2;
1965 else
1966 i = 0;
1967 if (i && len == i)
1969 parmnode = chain_index (i-1, parmlist);
1970 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
1971 ctype))
1972 viable = 0;
1975 /* This only applies at the top level. */
1976 flags &= ~LOOKUP_DEFAULTED;
1979 if (! viable)
1980 goto out;
1982 /* Second, for F to be a viable function, there shall exist for each
1983 argument an implicit conversion sequence that converts that argument
1984 to the corresponding parameter of F. */
1986 parmnode = parmlist;
1988 for (i = 0; i < len; ++i)
1990 tree argtype, to_type;
1991 tree arg;
1992 conversion *t;
1993 int is_this;
1995 if (parmnode == void_list_node)
1996 break;
1998 if (i == 0 && first_arg != NULL_TREE)
1999 arg = first_arg;
2000 else
2001 arg = CONST_CAST_TREE (
2002 (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2003 argtype = lvalue_type (arg);
2005 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2006 && ! DECL_CONSTRUCTOR_P (fn));
2008 if (parmnode)
2010 tree parmtype = TREE_VALUE (parmnode);
2011 int lflags = flags;
2013 parmnode = TREE_CHAIN (parmnode);
2015 /* The type of the implicit object parameter ('this') for
2016 overload resolution is not always the same as for the
2017 function itself; conversion functions are considered to
2018 be members of the class being converted, and functions
2019 introduced by a using-declaration are considered to be
2020 members of the class that uses them.
2022 Since build_over_call ignores the ICS for the `this'
2023 parameter, we can just change the parm type. */
2024 if (ctype && is_this)
2026 parmtype = cp_build_qualified_type
2027 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
2028 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2030 /* If the function has a ref-qualifier, the implicit
2031 object parameter has reference type. */
2032 bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2033 parmtype = cp_build_reference_type (parmtype, rv);
2034 /* The special handling of 'this' conversions in compare_ics
2035 does not apply if there is a ref-qualifier. */
2036 is_this = false;
2038 else
2040 parmtype = build_pointer_type (parmtype);
2041 arg = build_this (arg);
2042 argtype = lvalue_type (arg);
2046 /* Core issue 899: When [copy-]initializing a temporary to be bound
2047 to the first parameter of a copy constructor (12.8) called with
2048 a single argument in the context of direct-initialization,
2049 explicit conversion functions are also considered.
2051 So set LOOKUP_COPY_PARM to let reference_binding know that
2052 it's being called in that context. We generalize the above
2053 to handle move constructors and template constructors as well;
2054 the standardese should soon be updated similarly. */
2055 if (ctype && i == 0 && (len-skip == 1)
2056 && DECL_CONSTRUCTOR_P (fn)
2057 && parmtype != error_mark_node
2058 && (same_type_ignoring_top_level_qualifiers_p
2059 (non_reference (parmtype), ctype)))
2061 if (!(flags & LOOKUP_ONLYCONVERTING))
2062 lflags |= LOOKUP_COPY_PARM;
2063 /* We allow user-defined conversions within init-lists, but
2064 don't list-initialize the copy parm, as that would mean
2065 using two levels of braces for the same type. */
2066 if ((flags & LOOKUP_LIST_INIT_CTOR)
2067 && BRACE_ENCLOSED_INITIALIZER_P (arg))
2068 lflags |= LOOKUP_NO_CONVERSION;
2070 else
2071 lflags |= LOOKUP_ONLYCONVERTING;
2073 t = implicit_conversion (parmtype, argtype, arg,
2074 /*c_cast_p=*/false, lflags, complain);
2075 to_type = parmtype;
2077 else
2079 t = build_identity_conv (argtype, arg);
2080 t->ellipsis_p = true;
2081 to_type = argtype;
2084 if (t && is_this)
2085 t->this_p = true;
2087 convs[i] = t;
2088 if (! t)
2090 viable = 0;
2091 reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
2092 break;
2095 if (t->bad_p)
2097 viable = -1;
2098 reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type);
2102 out:
2103 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2104 access_path, conversion_path, viable, reason, flags);
2107 /* Create an overload candidate for the conversion function FN which will
2108 be invoked for expression OBJ, producing a pointer-to-function which
2109 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2110 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2111 passed on to implicit_conversion.
2113 Actually, we don't really care about FN; we care about the type it
2114 converts to. There may be multiple conversion functions that will
2115 convert to that type, and we rely on build_user_type_conversion_1 to
2116 choose the best one; so when we create our candidate, we record the type
2117 instead of the function. */
2119 static struct z_candidate *
2120 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2121 tree first_arg, const vec<tree, va_gc> *arglist,
2122 tree access_path, tree conversion_path,
2123 tsubst_flags_t complain)
2125 tree totype = TREE_TYPE (TREE_TYPE (fn));
2126 int i, len, viable, flags;
2127 tree parmlist, parmnode;
2128 conversion **convs;
2129 struct rejection_reason *reason;
2131 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2132 parmlist = TREE_TYPE (parmlist);
2133 parmlist = TYPE_ARG_TYPES (parmlist);
2135 len = vec_safe_length (arglist) + (first_arg != NULL_TREE ? 1 : 0) + 1;
2136 convs = alloc_conversions (len);
2137 parmnode = parmlist;
2138 viable = 1;
2139 flags = LOOKUP_IMPLICIT;
2140 reason = NULL;
2142 /* Don't bother looking up the same type twice. */
2143 if (*candidates && (*candidates)->fn == totype)
2144 return NULL;
2146 for (i = 0; i < len; ++i)
2148 tree arg, argtype, convert_type = NULL_TREE;
2149 conversion *t;
2151 if (i == 0)
2152 arg = obj;
2153 else if (i == 1 && first_arg != NULL_TREE)
2154 arg = first_arg;
2155 else
2156 arg = (*arglist)[i - (first_arg != NULL_TREE ? 1 : 0) - 1];
2157 argtype = lvalue_type (arg);
2159 if (i == 0)
2161 t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false,
2162 flags, complain);
2163 convert_type = totype;
2165 else if (parmnode == void_list_node)
2166 break;
2167 else if (parmnode)
2169 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2170 /*c_cast_p=*/false, flags, complain);
2171 convert_type = TREE_VALUE (parmnode);
2173 else
2175 t = build_identity_conv (argtype, arg);
2176 t->ellipsis_p = true;
2177 convert_type = argtype;
2180 convs[i] = t;
2181 if (! t)
2182 break;
2184 if (t->bad_p)
2186 viable = -1;
2187 reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type);
2190 if (i == 0)
2191 continue;
2193 if (parmnode)
2194 parmnode = TREE_CHAIN (parmnode);
2197 if (i < len
2198 || ! sufficient_parms_p (parmnode))
2200 int remaining = remaining_arguments (parmnode);
2201 viable = 0;
2202 reason = arity_rejection (NULL_TREE, i + remaining, len);
2205 return add_candidate (candidates, totype, first_arg, arglist, len, convs,
2206 access_path, conversion_path, viable, reason, flags);
2209 static void
2210 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2211 tree type1, tree type2, tree *args, tree *argtypes,
2212 int flags, tsubst_flags_t complain)
2214 conversion *t;
2215 conversion **convs;
2216 size_t num_convs;
2217 int viable = 1, i;
2218 tree types[2];
2219 struct rejection_reason *reason = NULL;
2221 types[0] = type1;
2222 types[1] = type2;
2224 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
2225 convs = alloc_conversions (num_convs);
2227 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2228 conversion ops are allowed. We handle that here by just checking for
2229 boolean_type_node because other operators don't ask for it. COND_EXPR
2230 also does contextual conversion to bool for the first operand, but we
2231 handle that in build_conditional_expr, and type1 here is operand 2. */
2232 if (type1 != boolean_type_node)
2233 flags |= LOOKUP_ONLYCONVERTING;
2235 for (i = 0; i < 2; ++i)
2237 if (! args[i])
2238 break;
2240 t = implicit_conversion (types[i], argtypes[i], args[i],
2241 /*c_cast_p=*/false, flags, complain);
2242 if (! t)
2244 viable = 0;
2245 /* We need something for printing the candidate. */
2246 t = build_identity_conv (types[i], NULL_TREE);
2247 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2248 types[i]);
2250 else if (t->bad_p)
2252 viable = 0;
2253 reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2254 types[i]);
2256 convs[i] = t;
2259 /* For COND_EXPR we rearranged the arguments; undo that now. */
2260 if (args[2])
2262 convs[2] = convs[1];
2263 convs[1] = convs[0];
2264 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2265 /*c_cast_p=*/false, flags,
2266 complain);
2267 if (t)
2268 convs[0] = t;
2269 else
2271 viable = 0;
2272 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2273 boolean_type_node);
2277 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2278 num_convs, convs,
2279 /*access_path=*/NULL_TREE,
2280 /*conversion_path=*/NULL_TREE,
2281 viable, reason, flags);
2284 static bool
2285 is_complete (tree t)
2287 return COMPLETE_TYPE_P (complete_type (t));
2290 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2292 static bool
2293 promoted_arithmetic_type_p (tree type)
2295 /* [over.built]
2297 In this section, the term promoted integral type is used to refer
2298 to those integral types which are preserved by integral promotion
2299 (including e.g. int and long but excluding e.g. char).
2300 Similarly, the term promoted arithmetic type refers to promoted
2301 integral types plus floating types. */
2302 return ((CP_INTEGRAL_TYPE_P (type)
2303 && same_type_p (type_promotes_to (type), type))
2304 || TREE_CODE (type) == REAL_TYPE);
2307 /* Create any builtin operator overload candidates for the operator in
2308 question given the converted operand types TYPE1 and TYPE2. The other
2309 args are passed through from add_builtin_candidates to
2310 build_builtin_candidate.
2312 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2313 If CODE is requires candidates operands of the same type of the kind
2314 of which TYPE1 and TYPE2 are, we add both candidates
2315 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2317 static void
2318 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2319 enum tree_code code2, tree fnname, tree type1,
2320 tree type2, tree *args, tree *argtypes, int flags,
2321 tsubst_flags_t complain)
2323 switch (code)
2325 case POSTINCREMENT_EXPR:
2326 case POSTDECREMENT_EXPR:
2327 args[1] = integer_zero_node;
2328 type2 = integer_type_node;
2329 break;
2330 default:
2331 break;
2334 switch (code)
2337 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2338 and VQ is either volatile or empty, there exist candidate operator
2339 functions of the form
2340 VQ T& operator++(VQ T&);
2341 T operator++(VQ T&, int);
2342 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
2343 type other than bool, and VQ is either volatile or empty, there exist
2344 candidate operator functions of the form
2345 VQ T& operator--(VQ T&);
2346 T operator--(VQ T&, int);
2347 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
2348 complete object type, and VQ is either volatile or empty, there exist
2349 candidate operator functions of the form
2350 T*VQ& operator++(T*VQ&);
2351 T*VQ& operator--(T*VQ&);
2352 T* operator++(T*VQ&, int);
2353 T* operator--(T*VQ&, int); */
2355 case POSTDECREMENT_EXPR:
2356 case PREDECREMENT_EXPR:
2357 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2358 return;
2359 case POSTINCREMENT_EXPR:
2360 case PREINCREMENT_EXPR:
2361 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2363 type1 = build_reference_type (type1);
2364 break;
2366 return;
2368 /* 7 For every cv-qualified or cv-unqualified object type T, there
2369 exist candidate operator functions of the form
2371 T& operator*(T*);
2373 8 For every function type T, there exist candidate operator functions of
2374 the form
2375 T& operator*(T*); */
2377 case INDIRECT_REF:
2378 if (TYPE_PTR_P (type1)
2379 && (TYPE_PTROB_P (type1)
2380 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2381 break;
2382 return;
2384 /* 9 For every type T, there exist candidate operator functions of the form
2385 T* operator+(T*);
2387 10For every promoted arithmetic type T, there exist candidate operator
2388 functions of the form
2389 T operator+(T);
2390 T operator-(T); */
2392 case UNARY_PLUS_EXPR: /* unary + */
2393 if (TYPE_PTR_P (type1))
2394 break;
2395 case NEGATE_EXPR:
2396 if (ARITHMETIC_TYPE_P (type1))
2397 break;
2398 return;
2400 /* 11For every promoted integral type T, there exist candidate operator
2401 functions of the form
2402 T operator~(T); */
2404 case BIT_NOT_EXPR:
2405 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2406 break;
2407 return;
2409 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2410 is the same type as C2 or is a derived class of C2, T is a complete
2411 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2412 there exist candidate operator functions of the form
2413 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2414 where CV12 is the union of CV1 and CV2. */
2416 case MEMBER_REF:
2417 if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2419 tree c1 = TREE_TYPE (type1);
2420 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2422 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2423 && (TYPE_PTRMEMFUNC_P (type2)
2424 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2425 break;
2427 return;
2429 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2430 didate operator functions of the form
2431 LR operator*(L, R);
2432 LR operator/(L, R);
2433 LR operator+(L, R);
2434 LR operator-(L, R);
2435 bool operator<(L, R);
2436 bool operator>(L, R);
2437 bool operator<=(L, R);
2438 bool operator>=(L, R);
2439 bool operator==(L, R);
2440 bool operator!=(L, R);
2441 where LR is the result of the usual arithmetic conversions between
2442 types L and R.
2444 14For every pair of types T and I, where T is a cv-qualified or cv-
2445 unqualified complete object type and I is a promoted integral type,
2446 there exist candidate operator functions of the form
2447 T* operator+(T*, I);
2448 T& operator[](T*, I);
2449 T* operator-(T*, I);
2450 T* operator+(I, T*);
2451 T& operator[](I, T*);
2453 15For every T, where T is a pointer to complete object type, there exist
2454 candidate operator functions of the form112)
2455 ptrdiff_t operator-(T, T);
2457 16For every pointer or enumeration type T, there exist candidate operator
2458 functions of the form
2459 bool operator<(T, T);
2460 bool operator>(T, T);
2461 bool operator<=(T, T);
2462 bool operator>=(T, T);
2463 bool operator==(T, T);
2464 bool operator!=(T, T);
2466 17For every pointer to member type T, there exist candidate operator
2467 functions of the form
2468 bool operator==(T, T);
2469 bool operator!=(T, T); */
2471 case MINUS_EXPR:
2472 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2473 break;
2474 if (TYPE_PTROB_P (type1)
2475 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2477 type2 = ptrdiff_type_node;
2478 break;
2480 case MULT_EXPR:
2481 case TRUNC_DIV_EXPR:
2482 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2483 break;
2484 return;
2486 case EQ_EXPR:
2487 case NE_EXPR:
2488 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2489 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
2490 break;
2491 if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
2493 type2 = type1;
2494 break;
2496 if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
2498 type1 = type2;
2499 break;
2501 /* Fall through. */
2502 case LT_EXPR:
2503 case GT_EXPR:
2504 case LE_EXPR:
2505 case GE_EXPR:
2506 case MAX_EXPR:
2507 case MIN_EXPR:
2508 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2509 break;
2510 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2511 break;
2512 if (TREE_CODE (type1) == ENUMERAL_TYPE
2513 && TREE_CODE (type2) == ENUMERAL_TYPE)
2514 break;
2515 if (TYPE_PTR_P (type1)
2516 && null_ptr_cst_p (args[1]))
2518 type2 = type1;
2519 break;
2521 if (null_ptr_cst_p (args[0])
2522 && TYPE_PTR_P (type2))
2524 type1 = type2;
2525 break;
2527 return;
2529 case PLUS_EXPR:
2530 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2531 break;
2532 case ARRAY_REF:
2533 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2535 type1 = ptrdiff_type_node;
2536 break;
2538 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2540 type2 = ptrdiff_type_node;
2541 break;
2543 return;
2545 /* 18For every pair of promoted integral types L and R, there exist candi-
2546 date operator functions of the form
2547 LR operator%(L, R);
2548 LR operator&(L, R);
2549 LR operator^(L, R);
2550 LR operator|(L, R);
2551 L operator<<(L, R);
2552 L operator>>(L, R);
2553 where LR is the result of the usual arithmetic conversions between
2554 types L and R. */
2556 case TRUNC_MOD_EXPR:
2557 case BIT_AND_EXPR:
2558 case BIT_IOR_EXPR:
2559 case BIT_XOR_EXPR:
2560 case LSHIFT_EXPR:
2561 case RSHIFT_EXPR:
2562 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2563 break;
2564 return;
2566 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2567 type, VQ is either volatile or empty, and R is a promoted arithmetic
2568 type, there exist candidate operator functions of the form
2569 VQ L& operator=(VQ L&, R);
2570 VQ L& operator*=(VQ L&, R);
2571 VQ L& operator/=(VQ L&, R);
2572 VQ L& operator+=(VQ L&, R);
2573 VQ L& operator-=(VQ L&, R);
2575 20For every pair T, VQ), where T is any type and VQ is either volatile
2576 or empty, there exist candidate operator functions of the form
2577 T*VQ& operator=(T*VQ&, T*);
2579 21For every pair T, VQ), where T is a pointer to member type and VQ is
2580 either volatile or empty, there exist candidate operator functions of
2581 the form
2582 VQ T& operator=(VQ T&, T);
2584 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2585 unqualified complete object type, VQ is either volatile or empty, and
2586 I is a promoted integral type, there exist candidate operator func-
2587 tions of the form
2588 T*VQ& operator+=(T*VQ&, I);
2589 T*VQ& operator-=(T*VQ&, I);
2591 23For every triple L, VQ, R), where L is an integral or enumeration
2592 type, VQ is either volatile or empty, and R is a promoted integral
2593 type, there exist candidate operator functions of the form
2595 VQ L& operator%=(VQ L&, R);
2596 VQ L& operator<<=(VQ L&, R);
2597 VQ L& operator>>=(VQ L&, R);
2598 VQ L& operator&=(VQ L&, R);
2599 VQ L& operator^=(VQ L&, R);
2600 VQ L& operator|=(VQ L&, R); */
2602 case MODIFY_EXPR:
2603 switch (code2)
2605 case PLUS_EXPR:
2606 case MINUS_EXPR:
2607 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2609 type2 = ptrdiff_type_node;
2610 break;
2612 case MULT_EXPR:
2613 case TRUNC_DIV_EXPR:
2614 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2615 break;
2616 return;
2618 case TRUNC_MOD_EXPR:
2619 case BIT_AND_EXPR:
2620 case BIT_IOR_EXPR:
2621 case BIT_XOR_EXPR:
2622 case LSHIFT_EXPR:
2623 case RSHIFT_EXPR:
2624 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2625 break;
2626 return;
2628 case NOP_EXPR:
2629 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2630 break;
2631 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2632 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2633 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2634 || ((TYPE_PTRMEMFUNC_P (type1)
2635 || TYPE_PTR_P (type1))
2636 && null_ptr_cst_p (args[1])))
2638 type2 = type1;
2639 break;
2641 return;
2643 default:
2644 gcc_unreachable ();
2646 type1 = build_reference_type (type1);
2647 break;
2649 case COND_EXPR:
2650 /* [over.built]
2652 For every pair of promoted arithmetic types L and R, there
2653 exist candidate operator functions of the form
2655 LR operator?(bool, L, R);
2657 where LR is the result of the usual arithmetic conversions
2658 between types L and R.
2660 For every type T, where T is a pointer or pointer-to-member
2661 type, there exist candidate operator functions of the form T
2662 operator?(bool, T, T); */
2664 if (promoted_arithmetic_type_p (type1)
2665 && promoted_arithmetic_type_p (type2))
2666 /* That's OK. */
2667 break;
2669 /* Otherwise, the types should be pointers. */
2670 if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
2671 return;
2673 /* We don't check that the two types are the same; the logic
2674 below will actually create two candidates; one in which both
2675 parameter types are TYPE1, and one in which both parameter
2676 types are TYPE2. */
2677 break;
2679 case REALPART_EXPR:
2680 case IMAGPART_EXPR:
2681 if (ARITHMETIC_TYPE_P (type1))
2682 break;
2683 return;
2685 default:
2686 gcc_unreachable ();
2689 /* Make sure we don't create builtin candidates with dependent types. */
2690 bool u1 = uses_template_parms (type1);
2691 bool u2 = type2 ? uses_template_parms (type2) : false;
2692 if (u1 || u2)
2694 /* Try to recover if one of the types is non-dependent. But if
2695 there's only one type, there's nothing we can do. */
2696 if (!type2)
2697 return;
2698 /* And we lose if both are dependent. */
2699 if (u1 && u2)
2700 return;
2701 /* Or if they have different forms. */
2702 if (TREE_CODE (type1) != TREE_CODE (type2))
2703 return;
2705 if (u1 && !u2)
2706 type1 = type2;
2707 else if (u2 && !u1)
2708 type2 = type1;
2711 /* If we're dealing with two pointer types or two enumeral types,
2712 we need candidates for both of them. */
2713 if (type2 && !same_type_p (type1, type2)
2714 && TREE_CODE (type1) == TREE_CODE (type2)
2715 && (TREE_CODE (type1) == REFERENCE_TYPE
2716 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2717 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2718 || TYPE_PTRMEMFUNC_P (type1)
2719 || MAYBE_CLASS_TYPE_P (type1)
2720 || TREE_CODE (type1) == ENUMERAL_TYPE))
2722 if (TYPE_PTR_OR_PTRMEM_P (type1))
2724 tree cptype = composite_pointer_type (type1, type2,
2725 error_mark_node,
2726 error_mark_node,
2727 CPO_CONVERSION,
2728 tf_none);
2729 if (cptype != error_mark_node)
2731 build_builtin_candidate
2732 (candidates, fnname, cptype, cptype, args, argtypes,
2733 flags, complain);
2734 return;
2738 build_builtin_candidate
2739 (candidates, fnname, type1, type1, args, argtypes, flags, complain);
2740 build_builtin_candidate
2741 (candidates, fnname, type2, type2, args, argtypes, flags, complain);
2742 return;
2745 build_builtin_candidate
2746 (candidates, fnname, type1, type2, args, argtypes, flags, complain);
2749 tree
2750 type_decays_to (tree type)
2752 if (TREE_CODE (type) == ARRAY_TYPE)
2753 return build_pointer_type (TREE_TYPE (type));
2754 if (TREE_CODE (type) == FUNCTION_TYPE)
2755 return build_pointer_type (type);
2756 return type;
2759 /* There are three conditions of builtin candidates:
2761 1) bool-taking candidates. These are the same regardless of the input.
2762 2) pointer-pair taking candidates. These are generated for each type
2763 one of the input types converts to.
2764 3) arithmetic candidates. According to the standard, we should generate
2765 all of these, but I'm trying not to...
2767 Here we generate a superset of the possible candidates for this particular
2768 case. That is a subset of the full set the standard defines, plus some
2769 other cases which the standard disallows. add_builtin_candidate will
2770 filter out the invalid set. */
2772 static void
2773 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2774 enum tree_code code2, tree fnname, tree *args,
2775 int flags, tsubst_flags_t complain)
2777 int ref1, i;
2778 int enum_p = 0;
2779 tree type, argtypes[3], t;
2780 /* TYPES[i] is the set of possible builtin-operator parameter types
2781 we will consider for the Ith argument. */
2782 vec<tree, va_gc> *types[2];
2783 unsigned ix;
2785 for (i = 0; i < 3; ++i)
2787 if (args[i])
2788 argtypes[i] = unlowered_expr_type (args[i]);
2789 else
2790 argtypes[i] = NULL_TREE;
2793 switch (code)
2795 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2796 and VQ is either volatile or empty, there exist candidate operator
2797 functions of the form
2798 VQ T& operator++(VQ T&); */
2800 case POSTINCREMENT_EXPR:
2801 case PREINCREMENT_EXPR:
2802 case POSTDECREMENT_EXPR:
2803 case PREDECREMENT_EXPR:
2804 case MODIFY_EXPR:
2805 ref1 = 1;
2806 break;
2808 /* 24There also exist candidate operator functions of the form
2809 bool operator!(bool);
2810 bool operator&&(bool, bool);
2811 bool operator||(bool, bool); */
2813 case TRUTH_NOT_EXPR:
2814 build_builtin_candidate
2815 (candidates, fnname, boolean_type_node,
2816 NULL_TREE, args, argtypes, flags, complain);
2817 return;
2819 case TRUTH_ORIF_EXPR:
2820 case TRUTH_ANDIF_EXPR:
2821 build_builtin_candidate
2822 (candidates, fnname, boolean_type_node,
2823 boolean_type_node, args, argtypes, flags, complain);
2824 return;
2826 case ADDR_EXPR:
2827 case COMPOUND_EXPR:
2828 case COMPONENT_REF:
2829 return;
2831 case COND_EXPR:
2832 case EQ_EXPR:
2833 case NE_EXPR:
2834 case LT_EXPR:
2835 case LE_EXPR:
2836 case GT_EXPR:
2837 case GE_EXPR:
2838 enum_p = 1;
2839 /* Fall through. */
2841 default:
2842 ref1 = 0;
2845 types[0] = make_tree_vector ();
2846 types[1] = make_tree_vector ();
2848 for (i = 0; i < 2; ++i)
2850 if (! args[i])
2852 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
2854 tree convs;
2856 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2857 return;
2859 convs = lookup_conversions (argtypes[i]);
2861 if (code == COND_EXPR)
2863 if (real_lvalue_p (args[i]))
2864 vec_safe_push (types[i], build_reference_type (argtypes[i]));
2866 vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
2869 else if (! convs)
2870 return;
2872 for (; convs; convs = TREE_CHAIN (convs))
2874 type = TREE_TYPE (convs);
2876 if (i == 0 && ref1
2877 && (TREE_CODE (type) != REFERENCE_TYPE
2878 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2879 continue;
2881 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2882 vec_safe_push (types[i], type);
2884 type = non_reference (type);
2885 if (i != 0 || ! ref1)
2887 type = cv_unqualified (type_decays_to (type));
2888 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2889 vec_safe_push (types[i], type);
2890 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2891 type = type_promotes_to (type);
2894 if (! vec_member (type, types[i]))
2895 vec_safe_push (types[i], type);
2898 else
2900 if (code == COND_EXPR && real_lvalue_p (args[i]))
2901 vec_safe_push (types[i], build_reference_type (argtypes[i]));
2902 type = non_reference (argtypes[i]);
2903 if (i != 0 || ! ref1)
2905 type = cv_unqualified (type_decays_to (type));
2906 if (enum_p && UNSCOPED_ENUM_P (type))
2907 vec_safe_push (types[i], type);
2908 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2909 type = type_promotes_to (type);
2911 vec_safe_push (types[i], type);
2915 /* Run through the possible parameter types of both arguments,
2916 creating candidates with those parameter types. */
2917 FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
2919 unsigned jx;
2920 tree u;
2922 if (!types[1]->is_empty ())
2923 FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
2924 add_builtin_candidate
2925 (candidates, code, code2, fnname, t,
2926 u, args, argtypes, flags, complain);
2927 else
2928 add_builtin_candidate
2929 (candidates, code, code2, fnname, t,
2930 NULL_TREE, args, argtypes, flags, complain);
2933 release_tree_vector (types[0]);
2934 release_tree_vector (types[1]);
2938 /* If TMPL can be successfully instantiated as indicated by
2939 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2941 TMPL is the template. EXPLICIT_TARGS are any explicit template
2942 arguments. ARGLIST is the arguments provided at the call-site.
2943 This does not change ARGLIST. The RETURN_TYPE is the desired type
2944 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
2945 as for add_function_candidate. If an OBJ is supplied, FLAGS and
2946 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
2948 static struct z_candidate*
2949 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2950 tree ctype, tree explicit_targs, tree first_arg,
2951 const vec<tree, va_gc> *arglist, tree return_type,
2952 tree access_path, tree conversion_path,
2953 int flags, tree obj, unification_kind_t strict,
2954 tsubst_flags_t complain)
2956 int ntparms = DECL_NTPARMS (tmpl);
2957 tree targs = make_tree_vec (ntparms);
2958 unsigned int len = vec_safe_length (arglist);
2959 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
2960 unsigned int skip_without_in_chrg = 0;
2961 tree first_arg_without_in_chrg = first_arg;
2962 tree *args_without_in_chrg;
2963 unsigned int nargs_without_in_chrg;
2964 unsigned int ia, ix;
2965 tree arg;
2966 struct z_candidate *cand;
2967 tree fn;
2968 struct rejection_reason *reason = NULL;
2969 int errs;
2971 /* We don't do deduction on the in-charge parameter, the VTT
2972 parameter or 'this'. */
2973 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2975 if (first_arg_without_in_chrg != NULL_TREE)
2976 first_arg_without_in_chrg = NULL_TREE;
2977 else
2978 ++skip_without_in_chrg;
2981 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2982 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2983 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
2985 if (first_arg_without_in_chrg != NULL_TREE)
2986 first_arg_without_in_chrg = NULL_TREE;
2987 else
2988 ++skip_without_in_chrg;
2991 if (len < skip_without_in_chrg)
2992 return NULL;
2994 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
2995 + (len - skip_without_in_chrg));
2996 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
2997 ia = 0;
2998 if (first_arg_without_in_chrg != NULL_TREE)
3000 args_without_in_chrg[ia] = first_arg_without_in_chrg;
3001 ++ia;
3003 for (ix = skip_without_in_chrg;
3004 vec_safe_iterate (arglist, ix, &arg);
3005 ++ix)
3007 args_without_in_chrg[ia] = arg;
3008 ++ia;
3010 gcc_assert (ia == nargs_without_in_chrg);
3012 errs = errorcount+sorrycount;
3013 fn = fn_type_unification (tmpl, explicit_targs, targs,
3014 args_without_in_chrg,
3015 nargs_without_in_chrg,
3016 return_type, strict, flags, false,
3017 complain & tf_decltype);
3019 if (fn == error_mark_node)
3021 /* Don't repeat unification later if it already resulted in errors. */
3022 if (errorcount+sorrycount == errs)
3023 reason = template_unification_rejection (tmpl, explicit_targs,
3024 targs, args_without_in_chrg,
3025 nargs_without_in_chrg,
3026 return_type, strict, flags);
3027 else
3028 reason = template_unification_error_rejection ();
3029 goto fail;
3032 /* In [class.copy]:
3034 A member function template is never instantiated to perform the
3035 copy of a class object to an object of its class type.
3037 It's a little unclear what this means; the standard explicitly
3038 does allow a template to be used to copy a class. For example,
3041 struct A {
3042 A(A&);
3043 template <class T> A(const T&);
3045 const A f ();
3046 void g () { A a (f ()); }
3048 the member template will be used to make the copy. The section
3049 quoted above appears in the paragraph that forbids constructors
3050 whose only parameter is (a possibly cv-qualified variant of) the
3051 class type, and a logical interpretation is that the intent was
3052 to forbid the instantiation of member templates which would then
3053 have that form. */
3054 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3056 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3057 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3058 ctype))
3060 reason = invalid_copy_with_fn_template_rejection ();
3061 goto fail;
3065 if (obj != NULL_TREE)
3066 /* Aha, this is a conversion function. */
3067 cand = add_conv_candidate (candidates, fn, obj, first_arg, arglist,
3068 access_path, conversion_path, complain);
3069 else
3070 cand = add_function_candidate (candidates, fn, ctype,
3071 first_arg, arglist, access_path,
3072 conversion_path, flags, complain);
3073 if (DECL_TI_TEMPLATE (fn) != tmpl)
3074 /* This situation can occur if a member template of a template
3075 class is specialized. Then, instantiate_template might return
3076 an instantiation of the specialization, in which case the
3077 DECL_TI_TEMPLATE field will point at the original
3078 specialization. For example:
3080 template <class T> struct S { template <class U> void f(U);
3081 template <> void f(int) {}; };
3082 S<double> sd;
3083 sd.f(3);
3085 Here, TMPL will be template <class U> S<double>::f(U).
3086 And, instantiate template will give us the specialization
3087 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3088 for this will point at template <class T> template <> S<T>::f(int),
3089 so that we can find the definition. For the purposes of
3090 overload resolution, however, we want the original TMPL. */
3091 cand->template_decl = build_template_info (tmpl, targs);
3092 else
3093 cand->template_decl = DECL_TEMPLATE_INFO (fn);
3094 cand->explicit_targs = explicit_targs;
3096 return cand;
3097 fail:
3098 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
3099 access_path, conversion_path, 0, reason, flags);
3103 static struct z_candidate *
3104 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3105 tree explicit_targs, tree first_arg,
3106 const vec<tree, va_gc> *arglist, tree return_type,
3107 tree access_path, tree conversion_path, int flags,
3108 unification_kind_t strict, tsubst_flags_t complain)
3110 return
3111 add_template_candidate_real (candidates, tmpl, ctype,
3112 explicit_targs, first_arg, arglist,
3113 return_type, access_path, conversion_path,
3114 flags, NULL_TREE, strict, complain);
3118 static struct z_candidate *
3119 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3120 tree obj, tree first_arg,
3121 const vec<tree, va_gc> *arglist,
3122 tree return_type, tree access_path,
3123 tree conversion_path, tsubst_flags_t complain)
3125 return
3126 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3127 first_arg, arglist, return_type, access_path,
3128 conversion_path, 0, obj, DEDUCE_CONV,
3129 complain);
3132 /* The CANDS are the set of candidates that were considered for
3133 overload resolution. Return the set of viable candidates, or CANDS
3134 if none are viable. If any of the candidates were viable, set
3135 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3136 considered viable only if it is strictly viable. */
3138 static struct z_candidate*
3139 splice_viable (struct z_candidate *cands,
3140 bool strict_p,
3141 bool *any_viable_p)
3143 struct z_candidate *viable;
3144 struct z_candidate **last_viable;
3145 struct z_candidate **cand;
3146 bool found_strictly_viable = false;
3148 /* Be strict inside templates, since build_over_call won't actually
3149 do the conversions to get pedwarns. */
3150 if (processing_template_decl)
3151 strict_p = true;
3153 viable = NULL;
3154 last_viable = &viable;
3155 *any_viable_p = false;
3157 cand = &cands;
3158 while (*cand)
3160 struct z_candidate *c = *cand;
3161 if (!strict_p
3162 && (c->viable == 1 || TREE_CODE (c->fn) == TEMPLATE_DECL))
3164 /* Be strict in the presence of a viable candidate. Also if
3165 there are template candidates, so that we get deduction errors
3166 for them instead of silently preferring a bad conversion. */
3167 strict_p = true;
3168 if (viable && !found_strictly_viable)
3170 /* Put any spliced near matches back onto the main list so
3171 that we see them if there is no strict match. */
3172 *any_viable_p = false;
3173 *last_viable = cands;
3174 cands = viable;
3175 viable = NULL;
3176 last_viable = &viable;
3180 if (strict_p ? c->viable == 1 : c->viable)
3182 *last_viable = c;
3183 *cand = c->next;
3184 c->next = NULL;
3185 last_viable = &c->next;
3186 *any_viable_p = true;
3187 if (c->viable == 1)
3188 found_strictly_viable = true;
3190 else
3191 cand = &c->next;
3194 return viable ? viable : cands;
3197 static bool
3198 any_strictly_viable (struct z_candidate *cands)
3200 for (; cands; cands = cands->next)
3201 if (cands->viable == 1)
3202 return true;
3203 return false;
3206 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3207 words, it is about to become the "this" pointer for a member
3208 function call. Take the address of the object. */
3210 static tree
3211 build_this (tree obj)
3213 /* In a template, we are only concerned about the type of the
3214 expression, so we can take a shortcut. */
3215 if (processing_template_decl)
3216 return build_address (obj);
3218 return cp_build_addr_expr (obj, tf_warning_or_error);
3221 /* Returns true iff functions are equivalent. Equivalent functions are
3222 not '==' only if one is a function-local extern function or if
3223 both are extern "C". */
3225 static inline int
3226 equal_functions (tree fn1, tree fn2)
3228 if (TREE_CODE (fn1) != TREE_CODE (fn2))
3229 return 0;
3230 if (TREE_CODE (fn1) == TEMPLATE_DECL)
3231 return fn1 == fn2;
3232 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
3233 || DECL_EXTERN_C_FUNCTION_P (fn1))
3234 return decls_match (fn1, fn2);
3235 return fn1 == fn2;
3238 /* Print information about a candidate being rejected due to INFO. */
3240 static void
3241 print_conversion_rejection (location_t loc, struct conversion_info *info)
3243 tree from = info->from;
3244 if (!TYPE_P (from))
3245 from = lvalue_type (from);
3246 if (info->n_arg == -1)
3248 /* Conversion of implicit `this' argument failed. */
3249 if (!TYPE_P (info->from))
3250 /* A bad conversion for 'this' must be discarding cv-quals. */
3251 inform (loc, " passing %qT as %<this%> "
3252 "argument discards qualifiers",
3253 from);
3254 else
3255 inform (loc, " no known conversion for implicit "
3256 "%<this%> parameter from %qT to %qT",
3257 from, info->to_type);
3259 else if (!TYPE_P (info->from))
3261 if (info->n_arg >= 0)
3262 inform (loc, " conversion of argument %d would be ill-formed:",
3263 info->n_arg + 1);
3264 perform_implicit_conversion (info->to_type, info->from,
3265 tf_warning_or_error);
3267 else if (info->n_arg == -2)
3268 /* Conversion of conversion function return value failed. */
3269 inform (loc, " no known conversion from %qT to %qT",
3270 from, info->to_type);
3271 else
3272 inform (loc, " no known conversion for argument %d from %qT to %qT",
3273 info->n_arg + 1, from, info->to_type);
3276 /* Print information about a candidate with WANT parameters and we found
3277 HAVE. */
3279 static void
3280 print_arity_information (location_t loc, unsigned int have, unsigned int want)
3282 inform_n (loc, want,
3283 " candidate expects %d argument, %d provided",
3284 " candidate expects %d arguments, %d provided",
3285 want, have);
3288 /* Print information about one overload candidate CANDIDATE. MSGSTR
3289 is the text to print before the candidate itself.
3291 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3292 to have been run through gettext by the caller. This wart makes
3293 life simpler in print_z_candidates and for the translators. */
3295 static void
3296 print_z_candidate (location_t loc, const char *msgstr,
3297 struct z_candidate *candidate)
3299 const char *msg = (msgstr == NULL
3300 ? ""
3301 : ACONCAT ((msgstr, " ", NULL)));
3302 location_t cloc = location_of (candidate->fn);
3304 if (identifier_p (candidate->fn))
3306 cloc = loc;
3307 if (candidate->num_convs == 3)
3308 inform (cloc, "%s%D(%T, %T, %T) <built-in>", msg, candidate->fn,
3309 candidate->convs[0]->type,
3310 candidate->convs[1]->type,
3311 candidate->convs[2]->type);
3312 else if (candidate->num_convs == 2)
3313 inform (cloc, "%s%D(%T, %T) <built-in>", msg, candidate->fn,
3314 candidate->convs[0]->type,
3315 candidate->convs[1]->type);
3316 else
3317 inform (cloc, "%s%D(%T) <built-in>", msg, candidate->fn,
3318 candidate->convs[0]->type);
3320 else if (TYPE_P (candidate->fn))
3321 inform (cloc, "%s%T <conversion>", msg, candidate->fn);
3322 else if (candidate->viable == -1)
3323 inform (cloc, "%s%#D <near match>", msg, candidate->fn);
3324 else if (DECL_DELETED_FN (candidate->fn))
3325 inform (cloc, "%s%#D <deleted>", msg, candidate->fn);
3326 else
3327 inform (cloc, "%s%#D", msg, candidate->fn);
3328 /* Give the user some information about why this candidate failed. */
3329 if (candidate->reason != NULL)
3331 struct rejection_reason *r = candidate->reason;
3333 switch (r->code)
3335 case rr_arity:
3336 print_arity_information (cloc, r->u.arity.actual,
3337 r->u.arity.expected);
3338 break;
3339 case rr_arg_conversion:
3340 print_conversion_rejection (cloc, &r->u.conversion);
3341 break;
3342 case rr_bad_arg_conversion:
3343 print_conversion_rejection (cloc, &r->u.bad_conversion);
3344 break;
3345 case rr_explicit_conversion:
3346 inform (cloc, " return type %qT of explicit conversion function "
3347 "cannot be converted to %qT with a qualification "
3348 "conversion", r->u.conversion.from,
3349 r->u.conversion.to_type);
3350 break;
3351 case rr_template_conversion:
3352 inform (cloc, " conversion from return type %qT of template "
3353 "conversion function specialization to %qT is not an "
3354 "exact match", r->u.conversion.from,
3355 r->u.conversion.to_type);
3356 break;
3357 case rr_template_unification:
3358 /* We use template_unification_error_rejection if unification caused
3359 actual non-SFINAE errors, in which case we don't need to repeat
3360 them here. */
3361 if (r->u.template_unification.tmpl == NULL_TREE)
3363 inform (cloc, " substitution of deduced template arguments "
3364 "resulted in errors seen above");
3365 break;
3367 /* Re-run template unification with diagnostics. */
3368 inform (cloc, " template argument deduction/substitution failed:");
3369 fn_type_unification (r->u.template_unification.tmpl,
3370 r->u.template_unification.explicit_targs,
3371 (make_tree_vec
3372 (r->u.template_unification.num_targs)),
3373 r->u.template_unification.args,
3374 r->u.template_unification.nargs,
3375 r->u.template_unification.return_type,
3376 r->u.template_unification.strict,
3377 r->u.template_unification.flags,
3378 true, false);
3379 break;
3380 case rr_invalid_copy:
3381 inform (cloc,
3382 " a constructor taking a single argument of its own "
3383 "class type is invalid");
3384 break;
3385 case rr_none:
3386 default:
3387 /* This candidate didn't have any issues or we failed to
3388 handle a particular code. Either way... */
3389 gcc_unreachable ();
3394 static void
3395 print_z_candidates (location_t loc, struct z_candidate *candidates)
3397 struct z_candidate *cand1;
3398 struct z_candidate **cand2;
3399 int n_candidates;
3401 if (!candidates)
3402 return;
3404 /* Remove non-viable deleted candidates. */
3405 cand1 = candidates;
3406 for (cand2 = &cand1; *cand2; )
3408 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
3409 && !(*cand2)->viable
3410 && DECL_DELETED_FN ((*cand2)->fn))
3411 *cand2 = (*cand2)->next;
3412 else
3413 cand2 = &(*cand2)->next;
3415 /* ...if there are any non-deleted ones. */
3416 if (cand1)
3417 candidates = cand1;
3419 /* There may be duplicates in the set of candidates. We put off
3420 checking this condition as long as possible, since we have no way
3421 to eliminate duplicates from a set of functions in less than n^2
3422 time. Now we are about to emit an error message, so it is more
3423 permissible to go slowly. */
3424 for (cand1 = candidates; cand1; cand1 = cand1->next)
3426 tree fn = cand1->fn;
3427 /* Skip builtin candidates and conversion functions. */
3428 if (!DECL_P (fn))
3429 continue;
3430 cand2 = &cand1->next;
3431 while (*cand2)
3433 if (DECL_P ((*cand2)->fn)
3434 && equal_functions (fn, (*cand2)->fn))
3435 *cand2 = (*cand2)->next;
3436 else
3437 cand2 = &(*cand2)->next;
3441 for (n_candidates = 0, cand1 = candidates; cand1; cand1 = cand1->next)
3442 n_candidates++;
3444 for (; candidates; candidates = candidates->next)
3445 print_z_candidate (loc, "candidate:", candidates);
3448 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3449 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3450 the result of the conversion function to convert it to the final
3451 desired type. Merge the two sequences into a single sequence,
3452 and return the merged sequence. */
3454 static conversion *
3455 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
3457 conversion **t;
3458 bool bad = user_seq->bad_p;
3460 gcc_assert (user_seq->kind == ck_user);
3462 /* Find the end of the second conversion sequence. */
3463 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
3465 /* The entire sequence is a user-conversion sequence. */
3466 (*t)->user_conv_p = true;
3467 if (bad)
3468 (*t)->bad_p = true;
3471 /* Replace the identity conversion with the user conversion
3472 sequence. */
3473 *t = user_seq;
3475 return std_seq;
3478 /* Handle overload resolution for initializing an object of class type from
3479 an initializer list. First we look for a suitable constructor that
3480 takes a std::initializer_list; if we don't find one, we then look for a
3481 non-list constructor.
3483 Parameters are as for add_candidates, except that the arguments are in
3484 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
3485 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3487 static void
3488 add_list_candidates (tree fns, tree first_arg,
3489 tree init_list, tree totype,
3490 tree explicit_targs, bool template_only,
3491 tree conversion_path, tree access_path,
3492 int flags,
3493 struct z_candidate **candidates,
3494 tsubst_flags_t complain)
3496 vec<tree, va_gc> *args;
3498 gcc_assert (*candidates == NULL);
3500 /* We're looking for a ctor for list-initialization. */
3501 flags |= LOOKUP_LIST_INIT_CTOR;
3502 /* And we don't allow narrowing conversions. We also use this flag to
3503 avoid the copy constructor call for copy-list-initialization. */
3504 flags |= LOOKUP_NO_NARROWING;
3506 /* Always use the default constructor if the list is empty (DR 990). */
3507 if (CONSTRUCTOR_NELTS (init_list) == 0
3508 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
3510 /* If the class has a list ctor, try passing the list as a single
3511 argument first, but only consider list ctors. */
3512 else if (TYPE_HAS_LIST_CTOR (totype))
3514 flags |= LOOKUP_LIST_ONLY;
3515 args = make_tree_vector_single (init_list);
3516 add_candidates (fns, first_arg, args, NULL_TREE,
3517 explicit_targs, template_only, conversion_path,
3518 access_path, flags, candidates, complain);
3519 if (any_strictly_viable (*candidates))
3520 return;
3523 args = ctor_to_vec (init_list);
3525 /* We aren't looking for list-ctors anymore. */
3526 flags &= ~LOOKUP_LIST_ONLY;
3527 /* We allow more user-defined conversions within an init-list. */
3528 flags &= ~LOOKUP_NO_CONVERSION;
3530 add_candidates (fns, first_arg, args, NULL_TREE,
3531 explicit_targs, template_only, conversion_path,
3532 access_path, flags, candidates, complain);
3535 /* Returns the best overload candidate to perform the requested
3536 conversion. This function is used for three the overloading situations
3537 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3538 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3539 per [dcl.init.ref], so we ignore temporary bindings. */
3541 static struct z_candidate *
3542 build_user_type_conversion_1 (tree totype, tree expr, int flags,
3543 tsubst_flags_t complain)
3545 struct z_candidate *candidates, *cand;
3546 tree fromtype;
3547 tree ctors = NULL_TREE;
3548 tree conv_fns = NULL_TREE;
3549 conversion *conv = NULL;
3550 tree first_arg = NULL_TREE;
3551 vec<tree, va_gc> *args = NULL;
3552 bool any_viable_p;
3553 int convflags;
3555 if (!expr)
3556 return NULL;
3558 fromtype = TREE_TYPE (expr);
3560 /* We represent conversion within a hierarchy using RVALUE_CONV and
3561 BASE_CONV, as specified by [over.best.ics]; these become plain
3562 constructor calls, as specified in [dcl.init]. */
3563 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
3564 || !DERIVED_FROM_P (totype, fromtype));
3566 if (MAYBE_CLASS_TYPE_P (totype))
3567 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
3568 creating a garbage BASELINK; constructors can't be inherited. */
3569 ctors = lookup_fnfields_slot (totype, complete_ctor_identifier);
3571 if (MAYBE_CLASS_TYPE_P (fromtype))
3573 tree to_nonref = non_reference (totype);
3574 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
3575 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
3576 && DERIVED_FROM_P (to_nonref, fromtype)))
3578 /* [class.conv.fct] A conversion function is never used to
3579 convert a (possibly cv-qualified) object to the (possibly
3580 cv-qualified) same object type (or a reference to it), to a
3581 (possibly cv-qualified) base class of that type (or a
3582 reference to it)... */
3584 else
3585 conv_fns = lookup_conversions (fromtype);
3588 candidates = 0;
3589 flags |= LOOKUP_NO_CONVERSION;
3590 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3591 flags |= LOOKUP_NO_NARROWING;
3593 /* It's OK to bind a temporary for converting constructor arguments, but
3594 not in converting the return value of a conversion operator. */
3595 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
3596 | (flags & LOOKUP_NO_NARROWING));
3597 flags &= ~LOOKUP_NO_TEMP_BIND;
3599 if (ctors)
3601 int ctorflags = flags;
3603 first_arg = build_dummy_object (totype);
3605 /* We should never try to call the abstract or base constructor
3606 from here. */
3607 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
3608 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)));
3610 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3612 /* List-initialization. */
3613 add_list_candidates (ctors, first_arg, expr, totype, NULL_TREE,
3614 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
3615 ctorflags, &candidates, complain);
3617 else
3619 args = make_tree_vector_single (expr);
3620 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
3621 TYPE_BINFO (totype), TYPE_BINFO (totype),
3622 ctorflags, &candidates, complain);
3625 for (cand = candidates; cand; cand = cand->next)
3627 cand->second_conv = build_identity_conv (totype, NULL_TREE);
3629 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
3630 set, then this is copy-initialization. In that case, "The
3631 result of the call is then used to direct-initialize the
3632 object that is the destination of the copy-initialization."
3633 [dcl.init]
3635 We represent this in the conversion sequence with an
3636 rvalue conversion, which means a constructor call. */
3637 if (TREE_CODE (totype) != REFERENCE_TYPE
3638 && !(convflags & LOOKUP_NO_TEMP_BIND))
3639 cand->second_conv
3640 = build_conv (ck_rvalue, totype, cand->second_conv);
3644 if (conv_fns)
3645 first_arg = expr;
3647 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
3649 tree conversion_path = TREE_PURPOSE (conv_fns);
3650 struct z_candidate *old_candidates;
3652 /* If we are called to convert to a reference type, we are trying to
3653 find a direct binding, so don't even consider temporaries. If
3654 we don't find a direct binding, the caller will try again to
3655 look for a temporary binding. */
3656 if (TREE_CODE (totype) == REFERENCE_TYPE)
3657 convflags |= LOOKUP_NO_TEMP_BIND;
3659 old_candidates = candidates;
3660 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3661 NULL_TREE, false,
3662 conversion_path, TYPE_BINFO (fromtype),
3663 flags, &candidates, complain);
3665 for (cand = candidates; cand != old_candidates; cand = cand->next)
3667 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
3668 conversion *ics
3669 = implicit_conversion (totype,
3670 rettype,
3672 /*c_cast_p=*/false, convflags,
3673 complain);
3675 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3676 copy-initialization. In that case, "The result of the
3677 call is then used to direct-initialize the object that is
3678 the destination of the copy-initialization." [dcl.init]
3680 We represent this in the conversion sequence with an
3681 rvalue conversion, which means a constructor call. But
3682 don't add a second rvalue conversion if there's already
3683 one there. Which there really shouldn't be, but it's
3684 harmless since we'd add it here anyway. */
3685 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3686 && !(convflags & LOOKUP_NO_TEMP_BIND))
3687 ics = build_conv (ck_rvalue, totype, ics);
3689 cand->second_conv = ics;
3691 if (!ics)
3693 cand->viable = 0;
3694 cand->reason = arg_conversion_rejection (NULL_TREE, -2,
3695 rettype, totype);
3697 else if (DECL_NONCONVERTING_P (cand->fn)
3698 && ics->rank > cr_exact)
3700 /* 13.3.1.5: For direct-initialization, those explicit
3701 conversion functions that are not hidden within S and
3702 yield type T or a type that can be converted to type T
3703 with a qualification conversion (4.4) are also candidate
3704 functions. */
3705 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
3706 I've raised this issue with the committee. --jason 9/2011 */
3707 cand->viable = -1;
3708 cand->reason = explicit_conversion_rejection (rettype, totype);
3710 else if (cand->viable == 1 && ics->bad_p)
3712 cand->viable = -1;
3713 cand->reason
3714 = bad_arg_conversion_rejection (NULL_TREE, -2,
3715 rettype, totype);
3717 else if (primary_template_instantiation_p (cand->fn)
3718 && ics->rank > cr_exact)
3720 /* 13.3.3.1.2: If the user-defined conversion is specified by
3721 a specialization of a conversion function template, the
3722 second standard conversion sequence shall have exact match
3723 rank. */
3724 cand->viable = -1;
3725 cand->reason = template_conversion_rejection (rettype, totype);
3730 candidates = splice_viable (candidates, false, &any_viable_p);
3731 if (!any_viable_p)
3733 if (args)
3734 release_tree_vector (args);
3735 return NULL;
3738 cand = tourney (candidates, complain);
3739 if (cand == 0)
3741 if (complain & tf_error)
3743 error ("conversion from %qT to %qT is ambiguous",
3744 fromtype, totype);
3745 print_z_candidates (location_of (expr), candidates);
3748 cand = candidates; /* any one will do */
3749 cand->second_conv = build_ambiguous_conv (totype, expr);
3750 cand->second_conv->user_conv_p = true;
3751 if (!any_strictly_viable (candidates))
3752 cand->second_conv->bad_p = true;
3753 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3754 ambiguous conversion is no worse than another user-defined
3755 conversion. */
3757 return cand;
3760 tree convtype;
3761 if (!DECL_CONSTRUCTOR_P (cand->fn))
3762 convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
3763 else if (cand->second_conv->kind == ck_rvalue)
3764 /* DR 5: [in the first step of copy-initialization]...if the function
3765 is a constructor, the call initializes a temporary of the
3766 cv-unqualified version of the destination type. */
3767 convtype = cv_unqualified (totype);
3768 else
3769 convtype = totype;
3770 /* Build the user conversion sequence. */
3771 conv = build_conv
3772 (ck_user,
3773 convtype,
3774 build_identity_conv (TREE_TYPE (expr), expr));
3775 conv->cand = cand;
3776 if (cand->viable == -1)
3777 conv->bad_p = true;
3779 /* Remember that this was a list-initialization. */
3780 if (flags & LOOKUP_NO_NARROWING)
3781 conv->check_narrowing = true;
3783 /* Combine it with the second conversion sequence. */
3784 cand->second_conv = merge_conversion_sequences (conv,
3785 cand->second_conv);
3787 return cand;
3790 /* Wrapper for above. */
3792 tree
3793 build_user_type_conversion (tree totype, tree expr, int flags,
3794 tsubst_flags_t complain)
3796 struct z_candidate *cand;
3797 tree ret;
3799 bool subtime = timevar_cond_start (TV_OVERLOAD);
3800 cand = build_user_type_conversion_1 (totype, expr, flags, complain);
3802 if (cand)
3804 if (cand->second_conv->kind == ck_ambig)
3805 ret = error_mark_node;
3806 else
3808 expr = convert_like (cand->second_conv, expr, complain);
3809 ret = convert_from_reference (expr);
3812 else
3813 ret = NULL_TREE;
3815 timevar_cond_stop (TV_OVERLOAD, subtime);
3816 return ret;
3819 /* Subroutine of convert_nontype_argument.
3821 EXPR is an argument for a template non-type parameter of integral or
3822 enumeration type. Do any necessary conversions (that are permitted for
3823 non-type arguments) to convert it to the parameter type.
3825 If conversion is successful, returns the converted expression;
3826 otherwise, returns error_mark_node. */
3828 tree
3829 build_integral_nontype_arg_conv (tree type, tree expr, tsubst_flags_t complain)
3831 conversion *conv;
3832 void *p;
3833 tree t;
3834 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
3836 if (error_operand_p (expr))
3837 return error_mark_node;
3839 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
3841 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3842 p = conversion_obstack_alloc (0);
3844 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
3845 /*c_cast_p=*/false,
3846 LOOKUP_IMPLICIT, complain);
3848 /* for a non-type template-parameter of integral or
3849 enumeration type, integral promotions (4.5) and integral
3850 conversions (4.7) are applied. */
3851 /* It should be sufficient to check the outermost conversion step, since
3852 there are no qualification conversions to integer type. */
3853 if (conv)
3854 switch (conv->kind)
3856 /* A conversion function is OK. If it isn't constexpr, we'll
3857 complain later that the argument isn't constant. */
3858 case ck_user:
3859 /* The lvalue-to-rvalue conversion is OK. */
3860 case ck_rvalue:
3861 case ck_identity:
3862 break;
3864 case ck_std:
3865 t = next_conversion (conv)->type;
3866 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t))
3867 break;
3869 if (complain & tf_error)
3870 error_at (loc, "conversion from %qT to %qT not considered for "
3871 "non-type template argument", t, type);
3872 /* and fall through. */
3874 default:
3875 conv = NULL;
3876 break;
3879 if (conv)
3880 expr = convert_like (conv, expr, complain);
3881 else
3882 expr = error_mark_node;
3884 /* Free all the conversions we allocated. */
3885 obstack_free (&conversion_obstack, p);
3887 return expr;
3890 /* Do any initial processing on the arguments to a function call. */
3892 static vec<tree, va_gc> *
3893 resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
3895 unsigned int ix;
3896 tree arg;
3898 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
3900 if (error_operand_p (arg))
3901 return NULL;
3902 else if (VOID_TYPE_P (TREE_TYPE (arg)))
3904 if (complain & tf_error)
3905 error ("invalid use of void expression");
3906 return NULL;
3908 else if (invalid_nonstatic_memfn_p (arg, complain))
3909 return NULL;
3911 return args;
3914 /* Perform overload resolution on FN, which is called with the ARGS.
3916 Return the candidate function selected by overload resolution, or
3917 NULL if the event that overload resolution failed. In the case
3918 that overload resolution fails, *CANDIDATES will be the set of
3919 candidates considered, and ANY_VIABLE_P will be set to true or
3920 false to indicate whether or not any of the candidates were
3921 viable.
3923 The ARGS should already have gone through RESOLVE_ARGS before this
3924 function is called. */
3926 static struct z_candidate *
3927 perform_overload_resolution (tree fn,
3928 const vec<tree, va_gc> *args,
3929 struct z_candidate **candidates,
3930 bool *any_viable_p, tsubst_flags_t complain)
3932 struct z_candidate *cand;
3933 tree explicit_targs;
3934 int template_only;
3936 bool subtime = timevar_cond_start (TV_OVERLOAD);
3938 explicit_targs = NULL_TREE;
3939 template_only = 0;
3941 *candidates = NULL;
3942 *any_viable_p = true;
3944 /* Check FN. */
3945 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
3946 || TREE_CODE (fn) == TEMPLATE_DECL
3947 || TREE_CODE (fn) == OVERLOAD
3948 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
3950 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3952 explicit_targs = TREE_OPERAND (fn, 1);
3953 fn = TREE_OPERAND (fn, 0);
3954 template_only = 1;
3957 /* Add the various candidate functions. */
3958 add_candidates (fn, NULL_TREE, args, NULL_TREE,
3959 explicit_targs, template_only,
3960 /*conversion_path=*/NULL_TREE,
3961 /*access_path=*/NULL_TREE,
3962 LOOKUP_NORMAL,
3963 candidates, complain);
3965 *candidates = splice_viable (*candidates, false, any_viable_p);
3966 if (*any_viable_p)
3967 cand = tourney (*candidates, complain);
3968 else
3969 cand = NULL;
3971 timevar_cond_stop (TV_OVERLOAD, subtime);
3972 return cand;
3975 /* Print an error message about being unable to build a call to FN with
3976 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
3977 be located; CANDIDATES is a possibly empty list of such
3978 functions. */
3980 static void
3981 print_error_for_call_failure (tree fn, vec<tree, va_gc> *args,
3982 struct z_candidate *candidates)
3984 tree name = DECL_NAME (OVL_CURRENT (fn));
3985 location_t loc = location_of (name);
3987 if (!any_strictly_viable (candidates))
3988 error_at (loc, "no matching function for call to %<%D(%A)%>",
3989 name, build_tree_list_vec (args));
3990 else
3991 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
3992 name, build_tree_list_vec (args));
3993 if (candidates)
3994 print_z_candidates (loc, candidates);
3997 /* Return an expression for a call to FN (a namespace-scope function,
3998 or a static member function) with the ARGS. This may change
3999 ARGS. */
4001 tree
4002 build_new_function_call (tree fn, vec<tree, va_gc> **args, bool koenig_p,
4003 tsubst_flags_t complain)
4005 struct z_candidate *candidates, *cand;
4006 bool any_viable_p;
4007 void *p;
4008 tree result;
4010 if (args != NULL && *args != NULL)
4012 *args = resolve_args (*args, complain);
4013 if (*args == NULL)
4014 return error_mark_node;
4017 if (flag_tm)
4018 tm_malloc_replacement (fn);
4020 /* If this function was found without using argument dependent
4021 lookup, then we want to ignore any undeclared friend
4022 functions. */
4023 if (!koenig_p)
4025 tree orig_fn = fn;
4027 fn = remove_hidden_names (fn);
4028 if (!fn)
4030 if (complain & tf_error)
4031 print_error_for_call_failure (orig_fn, *args, NULL);
4032 return error_mark_node;
4036 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4037 p = conversion_obstack_alloc (0);
4039 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
4040 complain);
4042 if (!cand)
4044 if (complain & tf_error)
4046 if (!any_viable_p && candidates && ! candidates->next
4047 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
4048 return cp_build_function_call_vec (candidates->fn, args, complain);
4049 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4050 fn = TREE_OPERAND (fn, 0);
4051 print_error_for_call_failure (fn, *args, candidates);
4053 result = error_mark_node;
4055 else
4057 int flags = LOOKUP_NORMAL;
4058 /* If fn is template_id_expr, the call has explicit template arguments
4059 (e.g. func<int>(5)), communicate this info to build_over_call
4060 through flags so that later we can use it to decide whether to warn
4061 about peculiar null pointer conversion. */
4062 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4063 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
4064 result = build_over_call (cand, flags, complain);
4067 /* Free all the conversions we allocated. */
4068 obstack_free (&conversion_obstack, p);
4070 return result;
4073 /* Build a call to a global operator new. FNNAME is the name of the
4074 operator (either "operator new" or "operator new[]") and ARGS are
4075 the arguments provided. This may change ARGS. *SIZE points to the
4076 total number of bytes required by the allocation, and is updated if
4077 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
4078 be used. If this function determines that no cookie should be
4079 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
4080 is not NULL_TREE, it is evaluated before calculating the final
4081 array size, and if it fails, the array size is replaced with
4082 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
4083 is non-NULL, it will be set, upon return, to the allocation
4084 function called. */
4086 tree
4087 build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
4088 tree *size, tree *cookie_size, tree size_check,
4089 tree *fn, tsubst_flags_t complain)
4091 tree original_size = *size;
4092 tree fns;
4093 struct z_candidate *candidates;
4094 struct z_candidate *cand;
4095 bool any_viable_p;
4097 if (fn)
4098 *fn = NULL_TREE;
4099 /* Set to (size_t)-1 if the size check fails. */
4100 if (size_check != NULL_TREE)
4102 tree errval = TYPE_MAX_VALUE (sizetype);
4103 if (cxx_dialect >= cxx11 && flag_exceptions)
4104 errval = throw_bad_array_new_length ();
4105 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4106 original_size, errval);
4108 vec_safe_insert (*args, 0, *size);
4109 *args = resolve_args (*args, complain);
4110 if (*args == NULL)
4111 return error_mark_node;
4113 /* Based on:
4115 [expr.new]
4117 If this lookup fails to find the name, or if the allocated type
4118 is not a class type, the allocation function's name is looked
4119 up in the global scope.
4121 we disregard block-scope declarations of "operator new". */
4122 fns = lookup_function_nonclass (fnname, *args, /*block_p=*/false);
4124 /* Figure out what function is being called. */
4125 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
4126 complain);
4128 /* If no suitable function could be found, issue an error message
4129 and give up. */
4130 if (!cand)
4132 if (complain & tf_error)
4133 print_error_for_call_failure (fns, *args, candidates);
4134 return error_mark_node;
4137 /* If a cookie is required, add some extra space. Whether
4138 or not a cookie is required cannot be determined until
4139 after we know which function was called. */
4140 if (*cookie_size)
4142 bool use_cookie = true;
4143 tree arg_types;
4145 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
4146 /* Skip the size_t parameter. */
4147 arg_types = TREE_CHAIN (arg_types);
4148 /* Check the remaining parameters (if any). */
4149 if (arg_types
4150 && TREE_CHAIN (arg_types) == void_list_node
4151 && same_type_p (TREE_VALUE (arg_types),
4152 ptr_type_node))
4153 use_cookie = false;
4154 /* If we need a cookie, adjust the number of bytes allocated. */
4155 if (use_cookie)
4157 /* Update the total size. */
4158 *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
4159 /* Set to (size_t)-1 if the size check fails. */
4160 gcc_assert (size_check != NULL_TREE);
4161 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4162 *size, TYPE_MAX_VALUE (sizetype));
4163 /* Update the argument list to reflect the adjusted size. */
4164 (**args)[0] = *size;
4166 else
4167 *cookie_size = NULL_TREE;
4170 /* Tell our caller which function we decided to call. */
4171 if (fn)
4172 *fn = cand->fn;
4174 /* Build the CALL_EXPR. */
4175 return build_over_call (cand, LOOKUP_NORMAL, complain);
4178 /* Build a new call to operator(). This may change ARGS. */
4180 static tree
4181 build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4183 struct z_candidate *candidates = 0, *cand;
4184 tree fns, convs, first_mem_arg = NULL_TREE;
4185 tree type = TREE_TYPE (obj);
4186 bool any_viable_p;
4187 tree result = NULL_TREE;
4188 void *p;
4190 if (error_operand_p (obj))
4191 return error_mark_node;
4193 obj = prep_operand (obj);
4195 if (TYPE_PTRMEMFUNC_P (type))
4197 if (complain & tf_error)
4198 /* It's no good looking for an overloaded operator() on a
4199 pointer-to-member-function. */
4200 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
4201 return error_mark_node;
4204 if (TYPE_BINFO (type))
4206 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
4207 if (fns == error_mark_node)
4208 return error_mark_node;
4210 else
4211 fns = NULL_TREE;
4213 if (args != NULL && *args != NULL)
4215 *args = resolve_args (*args, complain);
4216 if (*args == NULL)
4217 return error_mark_node;
4220 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4221 p = conversion_obstack_alloc (0);
4223 if (fns)
4225 first_mem_arg = obj;
4227 add_candidates (BASELINK_FUNCTIONS (fns),
4228 first_mem_arg, *args, NULL_TREE,
4229 NULL_TREE, false,
4230 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
4231 LOOKUP_NORMAL, &candidates, complain);
4234 convs = lookup_conversions (type);
4236 for (; convs; convs = TREE_CHAIN (convs))
4238 tree fns = TREE_VALUE (convs);
4239 tree totype = TREE_TYPE (convs);
4241 if (TYPE_PTRFN_P (totype)
4242 || TYPE_REFFN_P (totype)
4243 || (TREE_CODE (totype) == REFERENCE_TYPE
4244 && TYPE_PTRFN_P (TREE_TYPE (totype))))
4245 for (; fns; fns = OVL_NEXT (fns))
4247 tree fn = OVL_CURRENT (fns);
4249 if (DECL_NONCONVERTING_P (fn))
4250 continue;
4252 if (TREE_CODE (fn) == TEMPLATE_DECL)
4253 add_template_conv_candidate
4254 (&candidates, fn, obj, NULL_TREE, *args, totype,
4255 /*access_path=*/NULL_TREE,
4256 /*conversion_path=*/NULL_TREE, complain);
4257 else
4258 add_conv_candidate (&candidates, fn, obj, NULL_TREE,
4259 *args, /*conversion_path=*/NULL_TREE,
4260 /*access_path=*/NULL_TREE, complain);
4264 /* Be strict here because if we choose a bad conversion candidate, the
4265 errors we get won't mention the call context. */
4266 candidates = splice_viable (candidates, true, &any_viable_p);
4267 if (!any_viable_p)
4269 if (complain & tf_error)
4271 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
4272 build_tree_list_vec (*args));
4273 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4275 result = error_mark_node;
4277 else
4279 cand = tourney (candidates, complain);
4280 if (cand == 0)
4282 if (complain & tf_error)
4284 error ("call of %<(%T) (%A)%> is ambiguous",
4285 TREE_TYPE (obj), build_tree_list_vec (*args));
4286 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4288 result = error_mark_node;
4290 /* Since cand->fn will be a type, not a function, for a conversion
4291 function, we must be careful not to unconditionally look at
4292 DECL_NAME here. */
4293 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
4294 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
4295 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4296 else
4298 obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1,
4299 complain);
4300 obj = convert_from_reference (obj);
4301 result = cp_build_function_call_vec (obj, args, complain);
4305 /* Free all the conversions we allocated. */
4306 obstack_free (&conversion_obstack, p);
4308 return result;
4311 /* Wrapper for above. */
4313 tree
4314 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4316 tree ret;
4317 bool subtime = timevar_cond_start (TV_OVERLOAD);
4318 ret = build_op_call_1 (obj, args, complain);
4319 timevar_cond_stop (TV_OVERLOAD, subtime);
4320 return ret;
4323 /* Called by op_error to prepare format strings suitable for the error
4324 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4325 and a suffix (controlled by NTYPES). */
4327 static const char *
4328 op_error_string (const char *errmsg, int ntypes, bool match)
4330 const char *msg;
4332 const char *msgp = concat (match ? G_("ambiguous overload for ")
4333 : G_("no match for "), errmsg, NULL);
4335 if (ntypes == 3)
4336 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
4337 else if (ntypes == 2)
4338 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
4339 else
4340 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
4342 return msg;
4345 static void
4346 op_error (location_t loc, enum tree_code code, enum tree_code code2,
4347 tree arg1, tree arg2, tree arg3, bool match)
4349 const char *opname;
4351 if (code == MODIFY_EXPR)
4352 opname = assignment_operator_name_info[code2].name;
4353 else
4354 opname = operator_name_info[code].name;
4356 switch (code)
4358 case COND_EXPR:
4359 if (flag_diagnostics_show_caret)
4360 error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
4361 3, match),
4362 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4363 else
4364 error_at (loc, op_error_string (G_("ternary %<operator?:%> "
4365 "in %<%E ? %E : %E%>"), 3, match),
4366 arg1, arg2, arg3,
4367 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4368 break;
4370 case POSTINCREMENT_EXPR:
4371 case POSTDECREMENT_EXPR:
4372 if (flag_diagnostics_show_caret)
4373 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4374 opname, TREE_TYPE (arg1));
4375 else
4376 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4377 1, match),
4378 opname, arg1, opname, TREE_TYPE (arg1));
4379 break;
4381 case ARRAY_REF:
4382 if (flag_diagnostics_show_caret)
4383 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
4384 TREE_TYPE (arg1), TREE_TYPE (arg2));
4385 else
4386 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
4387 2, match),
4388 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
4389 break;
4391 case REALPART_EXPR:
4392 case IMAGPART_EXPR:
4393 if (flag_diagnostics_show_caret)
4394 error_at (loc, op_error_string (G_("%qs"), 1, match),
4395 opname, TREE_TYPE (arg1));
4396 else
4397 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
4398 opname, opname, arg1, TREE_TYPE (arg1));
4399 break;
4401 default:
4402 if (arg2)
4403 if (flag_diagnostics_show_caret)
4404 error_at (loc, op_error_string (G_("%<operator%s%>"), 2, match),
4405 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
4406 else
4407 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
4408 2, match),
4409 opname, arg1, opname, arg2,
4410 TREE_TYPE (arg1), TREE_TYPE (arg2));
4411 else
4412 if (flag_diagnostics_show_caret)
4413 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4414 opname, TREE_TYPE (arg1));
4415 else
4416 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
4417 1, match),
4418 opname, opname, arg1, TREE_TYPE (arg1));
4419 break;
4423 /* Return the implicit conversion sequence that could be used to
4424 convert E1 to E2 in [expr.cond]. */
4426 static conversion *
4427 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
4429 tree t1 = non_reference (TREE_TYPE (e1));
4430 tree t2 = non_reference (TREE_TYPE (e2));
4431 conversion *conv;
4432 bool good_base;
4434 /* [expr.cond]
4436 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
4437 implicitly converted (clause _conv_) to the type "lvalue reference to
4438 T2", subject to the constraint that in the conversion the
4439 reference must bind directly (_dcl.init.ref_) to an lvalue.
4441 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
4442 implicitly converted to the type "rvalue reference to T2", subject to
4443 the constraint that the reference must bind directly. */
4444 if (lvalue_or_rvalue_with_address_p (e2))
4446 tree rtype = cp_build_reference_type (t2, !real_lvalue_p (e2));
4447 conv = implicit_conversion (rtype,
4450 /*c_cast_p=*/false,
4451 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
4452 |LOOKUP_ONLYCONVERTING,
4453 complain);
4454 if (conv && !conv->bad_p)
4455 return conv;
4458 /* If E2 is a prvalue or if neither of the conversions above can be done
4459 and at least one of the operands has (possibly cv-qualified) class
4460 type: */
4461 if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
4462 return NULL;
4464 /* [expr.cond]
4466 If E1 and E2 have class type, and the underlying class types are
4467 the same or one is a base class of the other: E1 can be converted
4468 to match E2 if the class of T2 is the same type as, or a base
4469 class of, the class of T1, and the cv-qualification of T2 is the
4470 same cv-qualification as, or a greater cv-qualification than, the
4471 cv-qualification of T1. If the conversion is applied, E1 is
4472 changed to an rvalue of type T2 that still refers to the original
4473 source class object (or the appropriate subobject thereof). */
4474 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
4475 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
4477 if (good_base && at_least_as_qualified_p (t2, t1))
4479 conv = build_identity_conv (t1, e1);
4480 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
4481 TYPE_MAIN_VARIANT (t2)))
4482 conv = build_conv (ck_base, t2, conv);
4483 else
4484 conv = build_conv (ck_rvalue, t2, conv);
4485 return conv;
4487 else
4488 return NULL;
4490 else
4491 /* [expr.cond]
4493 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
4494 converted to the type that expression E2 would have if E2 were
4495 converted to an rvalue (or the type it has, if E2 is an rvalue). */
4496 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
4497 LOOKUP_IMPLICIT, complain);
4500 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
4501 arguments to the conditional expression. */
4503 static tree
4504 build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
4505 tsubst_flags_t complain)
4507 tree arg2_type;
4508 tree arg3_type;
4509 tree result = NULL_TREE;
4510 tree result_type = NULL_TREE;
4511 bool lvalue_p = true;
4512 struct z_candidate *candidates = 0;
4513 struct z_candidate *cand;
4514 void *p;
4515 tree orig_arg2, orig_arg3;
4517 /* As a G++ extension, the second argument to the conditional can be
4518 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
4519 c'.) If the second operand is omitted, make sure it is
4520 calculated only once. */
4521 if (!arg2)
4523 if (complain & tf_error)
4524 pedwarn (loc, OPT_Wpedantic,
4525 "ISO C++ forbids omitting the middle term of a ?: expression");
4527 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
4528 if (real_lvalue_p (arg1))
4529 arg2 = arg1 = stabilize_reference (arg1);
4530 else
4531 arg2 = arg1 = save_expr (arg1);
4534 /* If something has already gone wrong, just pass that fact up the
4535 tree. */
4536 if (error_operand_p (arg1)
4537 || error_operand_p (arg2)
4538 || error_operand_p (arg3))
4539 return error_mark_node;
4541 orig_arg2 = arg2;
4542 orig_arg3 = arg3;
4544 if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
4546 arg1 = force_rvalue (arg1, complain);
4547 arg2 = force_rvalue (arg2, complain);
4548 arg3 = force_rvalue (arg3, complain);
4550 /* force_rvalue can return error_mark on valid arguments. */
4551 if (error_operand_p (arg1)
4552 || error_operand_p (arg2)
4553 || error_operand_p (arg3))
4554 return error_mark_node;
4556 tree arg1_type = TREE_TYPE (arg1);
4557 arg2_type = TREE_TYPE (arg2);
4558 arg3_type = TREE_TYPE (arg3);
4560 if (TREE_CODE (arg2_type) != VECTOR_TYPE
4561 && TREE_CODE (arg3_type) != VECTOR_TYPE)
4563 /* Rely on the error messages of the scalar version. */
4564 tree scal = build_conditional_expr_1 (loc, integer_one_node,
4565 orig_arg2, orig_arg3, complain);
4566 if (scal == error_mark_node)
4567 return error_mark_node;
4568 tree stype = TREE_TYPE (scal);
4569 tree ctype = TREE_TYPE (arg1_type);
4570 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
4571 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
4573 if (complain & tf_error)
4574 error_at (loc, "inferred scalar type %qT is not an integer or "
4575 "floating point type of the same size as %qT", stype,
4576 COMPARISON_CLASS_P (arg1)
4577 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4578 : ctype);
4579 return error_mark_node;
4582 tree vtype = build_opaque_vector_type (stype,
4583 TYPE_VECTOR_SUBPARTS (arg1_type));
4584 /* We could pass complain & tf_warning to unsafe_conversion_p,
4585 but the warnings (like Wsign-conversion) have already been
4586 given by the scalar build_conditional_expr_1. We still check
4587 unsafe_conversion_p to forbid truncating long long -> float. */
4588 if (unsafe_conversion_p (loc, stype, arg2, false))
4590 if (complain & tf_error)
4591 error_at (loc, "conversion of scalar %qT to vector %qT "
4592 "involves truncation", arg2_type, vtype);
4593 return error_mark_node;
4595 if (unsafe_conversion_p (loc, stype, arg3, false))
4597 if (complain & tf_error)
4598 error_at (loc, "conversion of scalar %qT to vector %qT "
4599 "involves truncation", arg3_type, vtype);
4600 return error_mark_node;
4603 arg2 = cp_convert (stype, arg2, complain);
4604 arg2 = save_expr (arg2);
4605 arg2 = build_vector_from_val (vtype, arg2);
4606 arg2_type = vtype;
4607 arg3 = cp_convert (stype, arg3, complain);
4608 arg3 = save_expr (arg3);
4609 arg3 = build_vector_from_val (vtype, arg3);
4610 arg3_type = vtype;
4613 if ((TREE_CODE (arg2_type) == VECTOR_TYPE)
4614 != (TREE_CODE (arg3_type) == VECTOR_TYPE))
4616 enum stv_conv convert_flag =
4617 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
4618 complain & tf_error);
4620 switch (convert_flag)
4622 case stv_error:
4623 return error_mark_node;
4624 case stv_firstarg:
4626 arg2 = save_expr (arg2);
4627 arg2 = convert (TREE_TYPE (arg3_type), arg2);
4628 arg2 = build_vector_from_val (arg3_type, arg2);
4629 arg2_type = TREE_TYPE (arg2);
4630 break;
4632 case stv_secondarg:
4634 arg3 = save_expr (arg3);
4635 arg3 = convert (TREE_TYPE (arg2_type), arg3);
4636 arg3 = build_vector_from_val (arg2_type, arg3);
4637 arg3_type = TREE_TYPE (arg3);
4638 break;
4640 default:
4641 break;
4645 if (!same_type_p (arg2_type, arg3_type)
4646 || TYPE_VECTOR_SUBPARTS (arg1_type)
4647 != TYPE_VECTOR_SUBPARTS (arg2_type)
4648 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
4650 if (complain & tf_error)
4651 error_at (loc,
4652 "incompatible vector types in conditional expression: "
4653 "%qT, %qT and %qT", TREE_TYPE (arg1),
4654 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
4655 return error_mark_node;
4658 if (!COMPARISON_CLASS_P (arg1))
4659 arg1 = cp_build_binary_op (loc, NE_EXPR, arg1,
4660 build_zero_cst (arg1_type), complain);
4661 return fold_build3 (VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
4664 /* [expr.cond]
4666 The first expression is implicitly converted to bool (clause
4667 _conv_). */
4668 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
4669 LOOKUP_NORMAL);
4670 if (error_operand_p (arg1))
4671 return error_mark_node;
4673 /* [expr.cond]
4675 If either the second or the third operand has type (possibly
4676 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
4677 array-to-pointer (_conv.array_), and function-to-pointer
4678 (_conv.func_) standard conversions are performed on the second
4679 and third operands. */
4680 arg2_type = unlowered_expr_type (arg2);
4681 arg3_type = unlowered_expr_type (arg3);
4682 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
4684 /* Do the conversions. We don't these for `void' type arguments
4685 since it can't have any effect and since decay_conversion
4686 does not handle that case gracefully. */
4687 if (!VOID_TYPE_P (arg2_type))
4688 arg2 = decay_conversion (arg2, complain);
4689 if (!VOID_TYPE_P (arg3_type))
4690 arg3 = decay_conversion (arg3, complain);
4691 arg2_type = TREE_TYPE (arg2);
4692 arg3_type = TREE_TYPE (arg3);
4694 /* [expr.cond]
4696 One of the following shall hold:
4698 --The second or the third operand (but not both) is a
4699 throw-expression (_except.throw_); the result is of the
4700 type of the other and is an rvalue.
4702 --Both the second and the third operands have type void; the
4703 result is of type void and is an rvalue.
4705 We must avoid calling force_rvalue for expressions of type
4706 "void" because it will complain that their value is being
4707 used. */
4708 if (TREE_CODE (arg2) == THROW_EXPR
4709 && TREE_CODE (arg3) != THROW_EXPR)
4711 if (!VOID_TYPE_P (arg3_type))
4713 arg3 = force_rvalue (arg3, complain);
4714 if (arg3 == error_mark_node)
4715 return error_mark_node;
4717 arg3_type = TREE_TYPE (arg3);
4718 result_type = arg3_type;
4720 else if (TREE_CODE (arg2) != THROW_EXPR
4721 && TREE_CODE (arg3) == THROW_EXPR)
4723 if (!VOID_TYPE_P (arg2_type))
4725 arg2 = force_rvalue (arg2, complain);
4726 if (arg2 == error_mark_node)
4727 return error_mark_node;
4729 arg2_type = TREE_TYPE (arg2);
4730 result_type = arg2_type;
4732 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
4733 result_type = void_type_node;
4734 else
4736 if (complain & tf_error)
4738 if (VOID_TYPE_P (arg2_type))
4739 error_at (EXPR_LOC_OR_LOC (arg3, loc),
4740 "second operand to the conditional operator "
4741 "is of type %<void%>, but the third operand is "
4742 "neither a throw-expression nor of type %<void%>");
4743 else
4744 error_at (EXPR_LOC_OR_LOC (arg2, loc),
4745 "third operand to the conditional operator "
4746 "is of type %<void%>, but the second operand is "
4747 "neither a throw-expression nor of type %<void%>");
4749 return error_mark_node;
4752 lvalue_p = false;
4753 goto valid_operands;
4755 /* [expr.cond]
4757 Otherwise, if the second and third operand have different types,
4758 and either has (possibly cv-qualified) class type, or if both are
4759 glvalues of the same value category and the same type except for
4760 cv-qualification, an attempt is made to convert each of those operands
4761 to the type of the other. */
4762 else if (!same_type_p (arg2_type, arg3_type)
4763 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
4764 || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
4765 arg3_type)
4766 && lvalue_or_rvalue_with_address_p (arg2)
4767 && lvalue_or_rvalue_with_address_p (arg3)
4768 && real_lvalue_p (arg2) == real_lvalue_p (arg3))))
4770 conversion *conv2;
4771 conversion *conv3;
4772 bool converted = false;
4774 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4775 p = conversion_obstack_alloc (0);
4777 conv2 = conditional_conversion (arg2, arg3, complain);
4778 conv3 = conditional_conversion (arg3, arg2, complain);
4780 /* [expr.cond]
4782 If both can be converted, or one can be converted but the
4783 conversion is ambiguous, the program is ill-formed. If
4784 neither can be converted, the operands are left unchanged and
4785 further checking is performed as described below. If exactly
4786 one conversion is possible, that conversion is applied to the
4787 chosen operand and the converted operand is used in place of
4788 the original operand for the remainder of this section. */
4789 if ((conv2 && !conv2->bad_p
4790 && conv3 && !conv3->bad_p)
4791 || (conv2 && conv2->kind == ck_ambig)
4792 || (conv3 && conv3->kind == ck_ambig))
4794 if (complain & tf_error)
4796 error_at (loc, "operands to ?: have different types %qT and %qT",
4797 arg2_type, arg3_type);
4798 if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
4799 inform (loc, " and each type can be converted to the other");
4800 else if (conv2 && conv2->kind == ck_ambig)
4801 convert_like (conv2, arg2, complain);
4802 else
4803 convert_like (conv3, arg3, complain);
4805 result = error_mark_node;
4807 else if (conv2 && !conv2->bad_p)
4809 arg2 = convert_like (conv2, arg2, complain);
4810 arg2 = convert_from_reference (arg2);
4811 arg2_type = TREE_TYPE (arg2);
4812 /* Even if CONV2 is a valid conversion, the result of the
4813 conversion may be invalid. For example, if ARG3 has type
4814 "volatile X", and X does not have a copy constructor
4815 accepting a "volatile X&", then even if ARG2 can be
4816 converted to X, the conversion will fail. */
4817 if (error_operand_p (arg2))
4818 result = error_mark_node;
4819 converted = true;
4821 else if (conv3 && !conv3->bad_p)
4823 arg3 = convert_like (conv3, arg3, complain);
4824 arg3 = convert_from_reference (arg3);
4825 arg3_type = TREE_TYPE (arg3);
4826 if (error_operand_p (arg3))
4827 result = error_mark_node;
4828 converted = true;
4831 /* Free all the conversions we allocated. */
4832 obstack_free (&conversion_obstack, p);
4834 if (result)
4835 return result;
4837 /* If, after the conversion, both operands have class type,
4838 treat the cv-qualification of both operands as if it were the
4839 union of the cv-qualification of the operands.
4841 The standard is not clear about what to do in this
4842 circumstance. For example, if the first operand has type
4843 "const X" and the second operand has a user-defined
4844 conversion to "volatile X", what is the type of the second
4845 operand after this step? Making it be "const X" (matching
4846 the first operand) seems wrong, as that discards the
4847 qualification without actually performing a copy. Leaving it
4848 as "volatile X" seems wrong as that will result in the
4849 conditional expression failing altogether, even though,
4850 according to this step, the one operand could be converted to
4851 the type of the other. */
4852 if (converted
4853 && CLASS_TYPE_P (arg2_type)
4854 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
4855 arg2_type = arg3_type =
4856 cp_build_qualified_type (arg2_type,
4857 cp_type_quals (arg2_type)
4858 | cp_type_quals (arg3_type));
4861 /* [expr.cond]
4863 If the second and third operands are glvalues of the same value
4864 category and have the same type, the result is of that type and
4865 value category. */
4866 if (((real_lvalue_p (arg2) && real_lvalue_p (arg3))
4867 || (xvalue_p (arg2) && xvalue_p (arg3)))
4868 && same_type_p (arg2_type, arg3_type))
4870 result_type = arg2_type;
4871 arg2 = mark_lvalue_use (arg2);
4872 arg3 = mark_lvalue_use (arg3);
4873 goto valid_operands;
4876 /* [expr.cond]
4878 Otherwise, the result is an rvalue. If the second and third
4879 operand do not have the same type, and either has (possibly
4880 cv-qualified) class type, overload resolution is used to
4881 determine the conversions (if any) to be applied to the operands
4882 (_over.match.oper_, _over.built_). */
4883 lvalue_p = false;
4884 if (!same_type_p (arg2_type, arg3_type)
4885 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
4887 tree args[3];
4888 conversion *conv;
4889 bool any_viable_p;
4891 /* Rearrange the arguments so that add_builtin_candidate only has
4892 to know about two args. In build_builtin_candidate, the
4893 arguments are unscrambled. */
4894 args[0] = arg2;
4895 args[1] = arg3;
4896 args[2] = arg1;
4897 add_builtin_candidates (&candidates,
4898 COND_EXPR,
4899 NOP_EXPR,
4900 ansi_opname (COND_EXPR),
4901 args,
4902 LOOKUP_NORMAL, complain);
4904 /* [expr.cond]
4906 If the overload resolution fails, the program is
4907 ill-formed. */
4908 candidates = splice_viable (candidates, false, &any_viable_p);
4909 if (!any_viable_p)
4911 if (complain & tf_error)
4912 error_at (loc, "operands to ?: have different types %qT and %qT",
4913 arg2_type, arg3_type);
4914 return error_mark_node;
4916 cand = tourney (candidates, complain);
4917 if (!cand)
4919 if (complain & tf_error)
4921 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
4922 print_z_candidates (loc, candidates);
4924 return error_mark_node;
4927 /* [expr.cond]
4929 Otherwise, the conversions thus determined are applied, and
4930 the converted operands are used in place of the original
4931 operands for the remainder of this section. */
4932 conv = cand->convs[0];
4933 arg1 = convert_like (conv, arg1, complain);
4934 conv = cand->convs[1];
4935 arg2 = convert_like (conv, arg2, complain);
4936 arg2_type = TREE_TYPE (arg2);
4937 conv = cand->convs[2];
4938 arg3 = convert_like (conv, arg3, complain);
4939 arg3_type = TREE_TYPE (arg3);
4942 /* [expr.cond]
4944 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
4945 and function-to-pointer (_conv.func_) standard conversions are
4946 performed on the second and third operands.
4948 We need to force the lvalue-to-rvalue conversion here for class types,
4949 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
4950 that isn't wrapped with a TARGET_EXPR plays havoc with exception
4951 regions. */
4953 arg2 = force_rvalue (arg2, complain);
4954 if (!CLASS_TYPE_P (arg2_type))
4955 arg2_type = TREE_TYPE (arg2);
4957 arg3 = force_rvalue (arg3, complain);
4958 if (!CLASS_TYPE_P (arg3_type))
4959 arg3_type = TREE_TYPE (arg3);
4961 if (arg2 == error_mark_node || arg3 == error_mark_node)
4962 return error_mark_node;
4964 /* [expr.cond]
4966 After those conversions, one of the following shall hold:
4968 --The second and third operands have the same type; the result is of
4969 that type. */
4970 if (same_type_p (arg2_type, arg3_type))
4971 result_type = arg2_type;
4972 /* [expr.cond]
4974 --The second and third operands have arithmetic or enumeration
4975 type; the usual arithmetic conversions are performed to bring
4976 them to a common type, and the result is of that type. */
4977 else if ((ARITHMETIC_TYPE_P (arg2_type)
4978 || UNSCOPED_ENUM_P (arg2_type))
4979 && (ARITHMETIC_TYPE_P (arg3_type)
4980 || UNSCOPED_ENUM_P (arg3_type)))
4982 /* In this case, there is always a common type. */
4983 result_type = type_after_usual_arithmetic_conversions (arg2_type,
4984 arg3_type);
4985 if (complain & tf_warning)
4986 do_warn_double_promotion (result_type, arg2_type, arg3_type,
4987 "implicit conversion from %qT to %qT to "
4988 "match other result of conditional",
4989 loc);
4991 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
4992 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
4994 if (TREE_CODE (orig_arg2) == CONST_DECL
4995 && TREE_CODE (orig_arg3) == CONST_DECL
4996 && DECL_CONTEXT (orig_arg2) == DECL_CONTEXT (orig_arg3))
4997 /* Two enumerators from the same enumeration can have different
4998 types when the enumeration is still being defined. */;
4999 else if (complain & tf_warning)
5000 warning_at (loc, OPT_Wenum_compare, "enumeral mismatch in "
5001 "conditional expression: %qT vs %qT",
5002 arg2_type, arg3_type);
5004 else if (extra_warnings
5005 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
5006 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
5007 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
5008 && !same_type_p (arg2_type,
5009 type_promotes_to (arg3_type)))))
5011 if (complain & tf_warning)
5012 warning_at (loc, 0, "enumeral and non-enumeral type in "
5013 "conditional expression");
5016 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5017 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5019 /* [expr.cond]
5021 --The second and third operands have pointer type, or one has
5022 pointer type and the other is a null pointer constant; pointer
5023 conversions (_conv.ptr_) and qualification conversions
5024 (_conv.qual_) are performed to bring them to their composite
5025 pointer type (_expr.rel_). The result is of the composite
5026 pointer type.
5028 --The second and third operands have pointer to member type, or
5029 one has pointer to member type and the other is a null pointer
5030 constant; pointer to member conversions (_conv.mem_) and
5031 qualification conversions (_conv.qual_) are performed to bring
5032 them to a common type, whose cv-qualification shall match the
5033 cv-qualification of either the second or the third operand.
5034 The result is of the common type. */
5035 else if ((null_ptr_cst_p (arg2)
5036 && TYPE_PTR_OR_PTRMEM_P (arg3_type))
5037 || (null_ptr_cst_p (arg3)
5038 && TYPE_PTR_OR_PTRMEM_P (arg2_type))
5039 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
5040 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
5041 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
5043 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
5044 arg3, CPO_CONDITIONAL_EXPR,
5045 complain);
5046 if (result_type == error_mark_node)
5047 return error_mark_node;
5048 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5049 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5052 if (!result_type)
5054 if (complain & tf_error)
5055 error_at (loc, "operands to ?: have different types %qT and %qT",
5056 arg2_type, arg3_type);
5057 return error_mark_node;
5060 if (arg2 == error_mark_node || arg3 == error_mark_node)
5061 return error_mark_node;
5063 valid_operands:
5064 result = build3 (COND_EXPR, result_type, arg1, arg2, arg3);
5065 if (!cp_unevaluated_operand)
5066 /* Avoid folding within decltype (c++/42013) and noexcept. */
5067 result = fold_if_not_in_template (result);
5069 /* We can't use result_type below, as fold might have returned a
5070 throw_expr. */
5072 if (!lvalue_p)
5074 /* Expand both sides into the same slot, hopefully the target of
5075 the ?: expression. We used to check for TARGET_EXPRs here,
5076 but now we sometimes wrap them in NOP_EXPRs so the test would
5077 fail. */
5078 if (CLASS_TYPE_P (TREE_TYPE (result)))
5079 result = get_target_expr_sfinae (result, complain);
5080 /* If this expression is an rvalue, but might be mistaken for an
5081 lvalue, we must add a NON_LVALUE_EXPR. */
5082 result = rvalue (result);
5084 else
5085 result = force_paren_expr (result);
5087 return result;
5090 /* Wrapper for above. */
5092 tree
5093 build_conditional_expr (location_t loc, tree arg1, tree arg2, tree arg3,
5094 tsubst_flags_t complain)
5096 tree ret;
5097 bool subtime = timevar_cond_start (TV_OVERLOAD);
5098 ret = build_conditional_expr_1 (loc, arg1, arg2, arg3, complain);
5099 timevar_cond_stop (TV_OVERLOAD, subtime);
5100 return ret;
5103 /* OPERAND is an operand to an expression. Perform necessary steps
5104 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
5105 returned. */
5107 static tree
5108 prep_operand (tree operand)
5110 if (operand)
5112 if (CLASS_TYPE_P (TREE_TYPE (operand))
5113 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
5114 /* Make sure the template type is instantiated now. */
5115 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
5118 return operand;
5121 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5122 OVERLOAD) to the CANDIDATES, returning an updated list of
5123 CANDIDATES. The ARGS are the arguments provided to the call;
5124 if FIRST_ARG is non-null it is the implicit object argument,
5125 otherwise the first element of ARGS is used if needed. The
5126 EXPLICIT_TARGS are explicit template arguments provided.
5127 TEMPLATE_ONLY is true if only template functions should be
5128 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5129 add_function_candidate. */
5131 static void
5132 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
5133 tree return_type,
5134 tree explicit_targs, bool template_only,
5135 tree conversion_path, tree access_path,
5136 int flags,
5137 struct z_candidate **candidates,
5138 tsubst_flags_t complain)
5140 tree ctype;
5141 const vec<tree, va_gc> *non_static_args;
5142 bool check_list_ctor;
5143 bool check_converting;
5144 unification_kind_t strict;
5145 tree fn;
5147 if (!fns)
5148 return;
5150 /* Precalculate special handling of constructors and conversion ops. */
5151 fn = OVL_CURRENT (fns);
5152 if (DECL_CONV_FN_P (fn))
5154 check_list_ctor = false;
5155 check_converting = !!(flags & LOOKUP_ONLYCONVERTING);
5156 if (flags & LOOKUP_NO_CONVERSION)
5157 /* We're doing return_type(x). */
5158 strict = DEDUCE_CONV;
5159 else
5160 /* We're doing x.operator return_type(). */
5161 strict = DEDUCE_EXACT;
5162 /* [over.match.funcs] For conversion functions, the function
5163 is considered to be a member of the class of the implicit
5164 object argument for the purpose of defining the type of
5165 the implicit object parameter. */
5166 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
5168 else
5170 if (DECL_CONSTRUCTOR_P (fn))
5172 check_list_ctor = !!(flags & LOOKUP_LIST_ONLY);
5173 /* For list-initialization we consider explicit constructors
5174 and complain if one is chosen. */
5175 check_converting
5176 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
5177 == LOOKUP_ONLYCONVERTING);
5179 else
5181 check_list_ctor = false;
5182 check_converting = false;
5184 strict = DEDUCE_CALL;
5185 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
5188 if (first_arg)
5189 non_static_args = args;
5190 else
5191 /* Delay creating the implicit this parameter until it is needed. */
5192 non_static_args = NULL;
5194 for (; fns; fns = OVL_NEXT (fns))
5196 tree fn_first_arg;
5197 const vec<tree, va_gc> *fn_args;
5199 fn = OVL_CURRENT (fns);
5201 if (check_converting && DECL_NONCONVERTING_P (fn))
5202 continue;
5203 if (check_list_ctor && !is_list_ctor (fn))
5204 continue;
5206 /* Figure out which set of arguments to use. */
5207 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
5209 /* If this function is a non-static member and we didn't get an
5210 implicit object argument, move it out of args. */
5211 if (first_arg == NULL_TREE)
5213 unsigned int ix;
5214 tree arg;
5215 vec<tree, va_gc> *tempvec;
5216 vec_alloc (tempvec, args->length () - 1);
5217 for (ix = 1; args->iterate (ix, &arg); ++ix)
5218 tempvec->quick_push (arg);
5219 non_static_args = tempvec;
5220 first_arg = (*args)[0];
5223 fn_first_arg = first_arg;
5224 fn_args = non_static_args;
5226 else
5228 /* Otherwise, just use the list of arguments provided. */
5229 fn_first_arg = NULL_TREE;
5230 fn_args = args;
5233 if (TREE_CODE (fn) == TEMPLATE_DECL)
5234 add_template_candidate (candidates,
5236 ctype,
5237 explicit_targs,
5238 fn_first_arg,
5239 fn_args,
5240 return_type,
5241 access_path,
5242 conversion_path,
5243 flags,
5244 strict,
5245 complain);
5246 else if (!template_only)
5247 add_function_candidate (candidates,
5249 ctype,
5250 fn_first_arg,
5251 fn_args,
5252 access_path,
5253 conversion_path,
5254 flags,
5255 complain);
5259 static tree
5260 build_new_op_1 (location_t loc, enum tree_code code, int flags, tree arg1,
5261 tree arg2, tree arg3, tree *overload, tsubst_flags_t complain)
5263 struct z_candidate *candidates = 0, *cand;
5264 vec<tree, va_gc> *arglist;
5265 tree fnname;
5266 tree args[3];
5267 tree result = NULL_TREE;
5268 bool result_valid_p = false;
5269 enum tree_code code2 = NOP_EXPR;
5270 enum tree_code code_orig_arg1 = ERROR_MARK;
5271 enum tree_code code_orig_arg2 = ERROR_MARK;
5272 conversion *conv;
5273 void *p;
5274 bool strict_p;
5275 bool any_viable_p;
5277 if (error_operand_p (arg1)
5278 || error_operand_p (arg2)
5279 || error_operand_p (arg3))
5280 return error_mark_node;
5282 if (code == MODIFY_EXPR)
5284 code2 = TREE_CODE (arg3);
5285 arg3 = NULL_TREE;
5286 fnname = ansi_assopname (code2);
5288 else
5289 fnname = ansi_opname (code);
5291 arg1 = prep_operand (arg1);
5293 switch (code)
5295 case NEW_EXPR:
5296 case VEC_NEW_EXPR:
5297 case VEC_DELETE_EXPR:
5298 case DELETE_EXPR:
5299 /* Use build_op_new_call and build_op_delete_call instead. */
5300 gcc_unreachable ();
5302 case CALL_EXPR:
5303 /* Use build_op_call instead. */
5304 gcc_unreachable ();
5306 case TRUTH_ORIF_EXPR:
5307 case TRUTH_ANDIF_EXPR:
5308 case TRUTH_AND_EXPR:
5309 case TRUTH_OR_EXPR:
5310 /* These are saved for the sake of warn_logical_operator. */
5311 code_orig_arg1 = TREE_CODE (arg1);
5312 code_orig_arg2 = TREE_CODE (arg2);
5314 default:
5315 break;
5318 arg2 = prep_operand (arg2);
5319 arg3 = prep_operand (arg3);
5321 if (code == COND_EXPR)
5322 /* Use build_conditional_expr instead. */
5323 gcc_unreachable ();
5324 else if (! OVERLOAD_TYPE_P (TREE_TYPE (arg1))
5325 && (! arg2 || ! OVERLOAD_TYPE_P (TREE_TYPE (arg2))))
5326 goto builtin;
5328 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5329 arg2 = integer_zero_node;
5331 vec_alloc (arglist, 3);
5332 arglist->quick_push (arg1);
5333 if (arg2 != NULL_TREE)
5334 arglist->quick_push (arg2);
5335 if (arg3 != NULL_TREE)
5336 arglist->quick_push (arg3);
5338 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5339 p = conversion_obstack_alloc (0);
5341 /* Add namespace-scope operators to the list of functions to
5342 consider. */
5343 add_candidates (lookup_function_nonclass (fnname, arglist, /*block_p=*/true),
5344 NULL_TREE, arglist, NULL_TREE,
5345 NULL_TREE, false, NULL_TREE, NULL_TREE,
5346 flags, &candidates, complain);
5348 args[0] = arg1;
5349 args[1] = arg2;
5350 args[2] = NULL_TREE;
5352 /* Add class-member operators to the candidate set. */
5353 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
5355 tree fns;
5357 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
5358 if (fns == error_mark_node)
5360 result = error_mark_node;
5361 goto user_defined_result_ready;
5363 if (fns)
5364 add_candidates (BASELINK_FUNCTIONS (fns),
5365 NULL_TREE, arglist, NULL_TREE,
5366 NULL_TREE, false,
5367 BASELINK_BINFO (fns),
5368 BASELINK_ACCESS_BINFO (fns),
5369 flags, &candidates, complain);
5371 /* Per 13.3.1.2/3, 2nd bullet, if no operand has a class type, then
5372 only non-member functions that have type T1 or reference to
5373 cv-qualified-opt T1 for the first argument, if the first argument
5374 has an enumeration type, or T2 or reference to cv-qualified-opt
5375 T2 for the second argument, if the the second argument has an
5376 enumeration type. Filter out those that don't match. */
5377 else if (! arg2 || ! CLASS_TYPE_P (TREE_TYPE (arg2)))
5379 struct z_candidate **candp, **next;
5381 for (candp = &candidates; *candp; candp = next)
5383 tree parmlist, parmtype;
5384 int i, nargs = (arg2 ? 2 : 1);
5386 cand = *candp;
5387 next = &cand->next;
5389 parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5391 for (i = 0; i < nargs; ++i)
5393 parmtype = TREE_VALUE (parmlist);
5395 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
5396 parmtype = TREE_TYPE (parmtype);
5397 if (TREE_CODE (TREE_TYPE (args[i])) == ENUMERAL_TYPE
5398 && (same_type_ignoring_top_level_qualifiers_p
5399 (TREE_TYPE (args[i]), parmtype)))
5400 break;
5402 parmlist = TREE_CHAIN (parmlist);
5405 /* No argument has an appropriate type, so remove this
5406 candidate function from the list. */
5407 if (i == nargs)
5409 *candp = cand->next;
5410 next = candp;
5415 add_builtin_candidates (&candidates, code, code2, fnname, args,
5416 flags, complain);
5418 switch (code)
5420 case COMPOUND_EXPR:
5421 case ADDR_EXPR:
5422 /* For these, the built-in candidates set is empty
5423 [over.match.oper]/3. We don't want non-strict matches
5424 because exact matches are always possible with built-in
5425 operators. The built-in candidate set for COMPONENT_REF
5426 would be empty too, but since there are no such built-in
5427 operators, we accept non-strict matches for them. */
5428 strict_p = true;
5429 break;
5431 default:
5432 strict_p = false;
5433 break;
5436 candidates = splice_viable (candidates, strict_p, &any_viable_p);
5437 if (!any_viable_p)
5439 switch (code)
5441 case POSTINCREMENT_EXPR:
5442 case POSTDECREMENT_EXPR:
5443 /* Don't try anything fancy if we're not allowed to produce
5444 errors. */
5445 if (!(complain & tf_error))
5446 return error_mark_node;
5448 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
5449 distinguish between prefix and postfix ++ and
5450 operator++() was used for both, so we allow this with
5451 -fpermissive. */
5452 else
5454 const char *msg = (flag_permissive)
5455 ? G_("no %<%D(int)%> declared for postfix %qs,"
5456 " trying prefix operator instead")
5457 : G_("no %<%D(int)%> declared for postfix %qs");
5458 permerror (loc, msg, fnname, operator_name_info[code].name);
5461 if (!flag_permissive)
5462 return error_mark_node;
5464 if (code == POSTINCREMENT_EXPR)
5465 code = PREINCREMENT_EXPR;
5466 else
5467 code = PREDECREMENT_EXPR;
5468 result = build_new_op_1 (loc, code, flags, arg1, NULL_TREE,
5469 NULL_TREE, overload, complain);
5470 break;
5472 /* The caller will deal with these. */
5473 case ADDR_EXPR:
5474 case COMPOUND_EXPR:
5475 case COMPONENT_REF:
5476 result = NULL_TREE;
5477 result_valid_p = true;
5478 break;
5480 default:
5481 if (complain & tf_error)
5483 /* If one of the arguments of the operator represents
5484 an invalid use of member function pointer, try to report
5485 a meaningful error ... */
5486 if (invalid_nonstatic_memfn_p (arg1, tf_error)
5487 || invalid_nonstatic_memfn_p (arg2, tf_error)
5488 || invalid_nonstatic_memfn_p (arg3, tf_error))
5489 /* We displayed the error message. */;
5490 else
5492 /* ... Otherwise, report the more generic
5493 "no matching operator found" error */
5494 op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
5495 print_z_candidates (loc, candidates);
5498 result = error_mark_node;
5499 break;
5502 else
5504 cand = tourney (candidates, complain);
5505 if (cand == 0)
5507 if (complain & tf_error)
5509 op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
5510 print_z_candidates (loc, candidates);
5512 result = error_mark_node;
5514 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5516 if (overload)
5517 *overload = cand->fn;
5519 if (resolve_args (arglist, complain) == NULL)
5520 result = error_mark_node;
5521 else
5522 result = build_over_call (cand, LOOKUP_NORMAL, complain);
5524 else
5526 /* Give any warnings we noticed during overload resolution. */
5527 if (cand->warnings && (complain & tf_warning))
5529 struct candidate_warning *w;
5530 for (w = cand->warnings; w; w = w->next)
5531 joust (cand, w->loser, 1, complain);
5534 /* Check for comparison of different enum types. */
5535 switch (code)
5537 case GT_EXPR:
5538 case LT_EXPR:
5539 case GE_EXPR:
5540 case LE_EXPR:
5541 case EQ_EXPR:
5542 case NE_EXPR:
5543 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
5544 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
5545 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5546 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
5547 && (complain & tf_warning))
5549 warning (OPT_Wenum_compare,
5550 "comparison between %q#T and %q#T",
5551 TREE_TYPE (arg1), TREE_TYPE (arg2));
5553 break;
5554 default:
5555 break;
5558 /* We need to strip any leading REF_BIND so that bitfields
5559 don't cause errors. This should not remove any important
5560 conversions, because builtins don't apply to class
5561 objects directly. */
5562 conv = cand->convs[0];
5563 if (conv->kind == ck_ref_bind)
5564 conv = next_conversion (conv);
5565 arg1 = convert_like (conv, arg1, complain);
5567 if (arg2)
5569 conv = cand->convs[1];
5570 if (conv->kind == ck_ref_bind)
5571 conv = next_conversion (conv);
5572 else
5573 arg2 = decay_conversion (arg2, complain);
5575 /* We need to call warn_logical_operator before
5576 converting arg2 to a boolean_type, but after
5577 decaying an enumerator to its value. */
5578 if (complain & tf_warning)
5579 warn_logical_operator (loc, code, boolean_type_node,
5580 code_orig_arg1, arg1,
5581 code_orig_arg2, arg2);
5583 arg2 = convert_like (conv, arg2, complain);
5585 if (arg3)
5587 conv = cand->convs[2];
5588 if (conv->kind == ck_ref_bind)
5589 conv = next_conversion (conv);
5590 arg3 = convert_like (conv, arg3, complain);
5596 user_defined_result_ready:
5598 /* Free all the conversions we allocated. */
5599 obstack_free (&conversion_obstack, p);
5601 if (result || result_valid_p)
5602 return result;
5604 builtin:
5605 switch (code)
5607 case MODIFY_EXPR:
5608 return cp_build_modify_expr (arg1, code2, arg2, complain);
5610 case INDIRECT_REF:
5611 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
5613 case TRUTH_ANDIF_EXPR:
5614 case TRUTH_ORIF_EXPR:
5615 case TRUTH_AND_EXPR:
5616 case TRUTH_OR_EXPR:
5617 warn_logical_operator (loc, code, boolean_type_node,
5618 code_orig_arg1, arg1, code_orig_arg2, arg2);
5619 /* Fall through. */
5620 case PLUS_EXPR:
5621 case MINUS_EXPR:
5622 case MULT_EXPR:
5623 case TRUNC_DIV_EXPR:
5624 case GT_EXPR:
5625 case LT_EXPR:
5626 case GE_EXPR:
5627 case LE_EXPR:
5628 case EQ_EXPR:
5629 case NE_EXPR:
5630 case MAX_EXPR:
5631 case MIN_EXPR:
5632 case LSHIFT_EXPR:
5633 case RSHIFT_EXPR:
5634 case TRUNC_MOD_EXPR:
5635 case BIT_AND_EXPR:
5636 case BIT_IOR_EXPR:
5637 case BIT_XOR_EXPR:
5638 return cp_build_binary_op (loc, code, arg1, arg2, complain);
5640 case UNARY_PLUS_EXPR:
5641 case NEGATE_EXPR:
5642 case BIT_NOT_EXPR:
5643 case TRUTH_NOT_EXPR:
5644 case PREINCREMENT_EXPR:
5645 case POSTINCREMENT_EXPR:
5646 case PREDECREMENT_EXPR:
5647 case POSTDECREMENT_EXPR:
5648 case REALPART_EXPR:
5649 case IMAGPART_EXPR:
5650 case ABS_EXPR:
5651 return cp_build_unary_op (code, arg1, candidates != 0, complain);
5653 case ARRAY_REF:
5654 return cp_build_array_ref (input_location, arg1, arg2, complain);
5656 case MEMBER_REF:
5657 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_ARROW_STAR,
5658 complain),
5659 arg2, complain);
5661 /* The caller will deal with these. */
5662 case ADDR_EXPR:
5663 case COMPONENT_REF:
5664 case COMPOUND_EXPR:
5665 return NULL_TREE;
5667 default:
5668 gcc_unreachable ();
5670 return NULL_TREE;
5673 /* Wrapper for above. */
5675 tree
5676 build_new_op (location_t loc, enum tree_code code, int flags,
5677 tree arg1, tree arg2, tree arg3,
5678 tree *overload, tsubst_flags_t complain)
5680 tree ret;
5681 bool subtime = timevar_cond_start (TV_OVERLOAD);
5682 ret = build_new_op_1 (loc, code, flags, arg1, arg2, arg3,
5683 overload, complain);
5684 timevar_cond_stop (TV_OVERLOAD, subtime);
5685 return ret;
5688 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
5689 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
5691 static bool
5692 non_placement_deallocation_fn_p (tree t)
5694 /* A template instance is never a usual deallocation function,
5695 regardless of its signature. */
5696 if (TREE_CODE (t) == TEMPLATE_DECL
5697 || primary_template_instantiation_p (t))
5698 return false;
5700 /* If a class T has a member deallocation function named operator delete
5701 with exactly one parameter, then that function is a usual
5702 (non-placement) deallocation function. If class T does not declare
5703 such an operator delete but does declare a member deallocation
5704 function named operator delete with exactly two parameters, the second
5705 of which has type std::size_t (18.2), then this function is a usual
5706 deallocation function. */
5707 t = FUNCTION_ARG_CHAIN (t);
5708 if (t == void_list_node
5709 || (t && same_type_p (TREE_VALUE (t), size_type_node)
5710 && TREE_CHAIN (t) == void_list_node))
5711 return true;
5712 return false;
5715 /* Build a call to operator delete. This has to be handled very specially,
5716 because the restrictions on what signatures match are different from all
5717 other call instances. For a normal delete, only a delete taking (void *)
5718 or (void *, size_t) is accepted. For a placement delete, only an exact
5719 match with the placement new is accepted.
5721 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
5722 ADDR is the pointer to be deleted.
5723 SIZE is the size of the memory block to be deleted.
5724 GLOBAL_P is true if the delete-expression should not consider
5725 class-specific delete operators.
5726 PLACEMENT is the corresponding placement new call, or NULL_TREE.
5728 If this call to "operator delete" is being generated as part to
5729 deallocate memory allocated via a new-expression (as per [expr.new]
5730 which requires that if the initialization throws an exception then
5731 we call a deallocation function), then ALLOC_FN is the allocation
5732 function. */
5734 tree
5735 build_op_delete_call (enum tree_code code, tree addr, tree size,
5736 bool global_p, tree placement,
5737 tree alloc_fn, tsubst_flags_t complain)
5739 tree fn = NULL_TREE;
5740 tree fns, fnname, type, t;
5742 if (addr == error_mark_node)
5743 return error_mark_node;
5745 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
5747 fnname = ansi_opname (code);
5749 if (CLASS_TYPE_P (type)
5750 && COMPLETE_TYPE_P (complete_type (type))
5751 && !global_p)
5752 /* In [class.free]
5754 If the result of the lookup is ambiguous or inaccessible, or if
5755 the lookup selects a placement deallocation function, the
5756 program is ill-formed.
5758 Therefore, we ask lookup_fnfields to complain about ambiguity. */
5760 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
5761 if (fns == error_mark_node)
5762 return error_mark_node;
5764 else
5765 fns = NULL_TREE;
5767 if (fns == NULL_TREE)
5768 fns = lookup_name_nonclass (fnname);
5770 /* Strip const and volatile from addr. */
5771 addr = cp_convert (ptr_type_node, addr, complain);
5773 if (placement)
5775 /* "A declaration of a placement deallocation function matches the
5776 declaration of a placement allocation function if it has the same
5777 number of parameters and, after parameter transformations (8.3.5),
5778 all parameter types except the first are identical."
5780 So we build up the function type we want and ask instantiate_type
5781 to get it for us. */
5782 t = FUNCTION_ARG_CHAIN (alloc_fn);
5783 t = tree_cons (NULL_TREE, ptr_type_node, t);
5784 t = build_function_type (void_type_node, t);
5786 fn = instantiate_type (t, fns, tf_none);
5787 if (fn == error_mark_node)
5788 return NULL_TREE;
5790 if (BASELINK_P (fn))
5791 fn = BASELINK_FUNCTIONS (fn);
5793 /* "If the lookup finds the two-parameter form of a usual deallocation
5794 function (3.7.4.2) and that function, considered as a placement
5795 deallocation function, would have been selected as a match for the
5796 allocation function, the program is ill-formed." */
5797 if (non_placement_deallocation_fn_p (fn))
5799 /* But if the class has an operator delete (void *), then that is
5800 the usual deallocation function, so we shouldn't complain
5801 about using the operator delete (void *, size_t). */
5802 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
5803 t; t = OVL_NEXT (t))
5805 tree elt = OVL_CURRENT (t);
5806 if (non_placement_deallocation_fn_p (elt)
5807 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
5808 goto ok;
5810 if (complain & tf_error)
5812 permerror (0, "non-placement deallocation function %q+D", fn);
5813 permerror (input_location, "selected for placement delete");
5815 else
5816 return error_mark_node;
5817 ok:;
5820 else
5821 /* "Any non-placement deallocation function matches a non-placement
5822 allocation function. If the lookup finds a single matching
5823 deallocation function, that function will be called; otherwise, no
5824 deallocation function will be called." */
5825 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
5826 t; t = OVL_NEXT (t))
5828 tree elt = OVL_CURRENT (t);
5829 if (non_placement_deallocation_fn_p (elt))
5831 fn = elt;
5832 /* "If a class T has a member deallocation function named
5833 operator delete with exactly one parameter, then that
5834 function is a usual (non-placement) deallocation
5835 function. If class T does not declare such an operator
5836 delete but does declare a member deallocation function named
5837 operator delete with exactly two parameters, the second of
5838 which has type std::size_t (18.2), then this function is a
5839 usual deallocation function."
5841 So (void*) beats (void*, size_t). */
5842 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
5843 break;
5847 /* If we have a matching function, call it. */
5848 if (fn)
5850 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
5852 /* If the FN is a member function, make sure that it is
5853 accessible. */
5854 if (BASELINK_P (fns))
5855 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
5856 complain);
5858 /* Core issue 901: It's ok to new a type with deleted delete. */
5859 if (DECL_DELETED_FN (fn) && alloc_fn)
5860 return NULL_TREE;
5862 if (placement)
5864 /* The placement args might not be suitable for overload
5865 resolution at this point, so build the call directly. */
5866 int nargs = call_expr_nargs (placement);
5867 tree *argarray = XALLOCAVEC (tree, nargs);
5868 int i;
5869 argarray[0] = addr;
5870 for (i = 1; i < nargs; i++)
5871 argarray[i] = CALL_EXPR_ARG (placement, i);
5872 mark_used (fn);
5873 return build_cxx_call (fn, nargs, argarray, complain);
5875 else
5877 tree ret;
5878 vec<tree, va_gc> *args = make_tree_vector ();
5879 args->quick_push (addr);
5880 if (FUNCTION_ARG_CHAIN (fn) != void_list_node)
5881 args->quick_push (size);
5882 ret = cp_build_function_call_vec (fn, &args, complain);
5883 release_tree_vector (args);
5884 return ret;
5888 /* [expr.new]
5890 If no unambiguous matching deallocation function can be found,
5891 propagating the exception does not cause the object's memory to
5892 be freed. */
5893 if (alloc_fn)
5895 if ((complain & tf_warning)
5896 && !placement)
5897 warning (0, "no corresponding deallocation function for %qD",
5898 alloc_fn);
5899 return NULL_TREE;
5902 if (complain & tf_error)
5903 error ("no suitable %<operator %s%> for %qT",
5904 operator_name_info[(int)code].name, type);
5905 return error_mark_node;
5908 /* If the current scope isn't allowed to access DECL along
5909 BASETYPE_PATH, give an error. The most derived class in
5910 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
5911 the declaration to use in the error diagnostic. */
5913 bool
5914 enforce_access (tree basetype_path, tree decl, tree diag_decl,
5915 tsubst_flags_t complain)
5917 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
5919 if (!accessible_p (basetype_path, decl, true))
5921 if (complain & tf_error)
5923 if (TREE_PRIVATE (decl))
5924 error ("%q+#D is private", diag_decl);
5925 else if (TREE_PROTECTED (decl))
5926 error ("%q+#D is protected", diag_decl);
5927 else
5928 error ("%q+#D is inaccessible", diag_decl);
5929 error ("within this context");
5931 return false;
5934 return true;
5937 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
5938 bitwise or of LOOKUP_* values. If any errors are warnings are
5939 generated, set *DIAGNOSTIC_FN to "error" or "warning",
5940 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
5941 to NULL. */
5943 static tree
5944 build_temp (tree expr, tree type, int flags,
5945 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
5947 int savew, savee;
5948 vec<tree, va_gc> *args;
5950 savew = warningcount + werrorcount, savee = errorcount;
5951 args = make_tree_vector_single (expr);
5952 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
5953 &args, type, flags, complain);
5954 release_tree_vector (args);
5955 if (warningcount + werrorcount > savew)
5956 *diagnostic_kind = DK_WARNING;
5957 else if (errorcount > savee)
5958 *diagnostic_kind = DK_ERROR;
5959 else
5960 *diagnostic_kind = DK_UNSPECIFIED;
5961 return expr;
5964 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
5965 EXPR is implicitly converted to type TOTYPE.
5966 FN and ARGNUM are used for diagnostics. */
5968 static void
5969 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
5971 /* Issue warnings about peculiar, but valid, uses of NULL. */
5972 if (expr == null_node && TREE_CODE (totype) != BOOLEAN_TYPE
5973 && ARITHMETIC_TYPE_P (totype))
5975 source_location loc =
5976 expansion_point_location_if_in_system_header (input_location);
5978 if (fn)
5979 warning_at (loc, OPT_Wconversion_null,
5980 "passing NULL to non-pointer argument %P of %qD",
5981 argnum, fn);
5982 else
5983 warning_at (loc, OPT_Wconversion_null,
5984 "converting to non-pointer type %qT from NULL", totype);
5987 /* Issue warnings if "false" is converted to a NULL pointer */
5988 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
5989 && TYPE_PTR_P (totype))
5991 if (fn)
5992 warning_at (input_location, OPT_Wconversion_null,
5993 "converting %<false%> to pointer type for argument %P "
5994 "of %qD", argnum, fn);
5995 else
5996 warning_at (input_location, OPT_Wconversion_null,
5997 "converting %<false%> to pointer type %qT", totype);
6001 /* We gave a diagnostic during a conversion. If this was in the second
6002 standard conversion sequence of a user-defined conversion sequence, say
6003 which user-defined conversion. */
6005 static void
6006 maybe_print_user_conv_context (conversion *convs)
6008 if (convs->user_conv_p)
6009 for (conversion *t = convs; t; t = next_conversion (t))
6010 if (t->kind == ck_user)
6012 print_z_candidate (0, " after user-defined conversion:",
6013 t->cand);
6014 break;
6018 /* Perform the conversions in CONVS on the expression EXPR. FN and
6019 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
6020 indicates the `this' argument of a method. INNER is nonzero when
6021 being called to continue a conversion chain. It is negative when a
6022 reference binding will be applied, positive otherwise. If
6023 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
6024 conversions will be emitted if appropriate. If C_CAST_P is true,
6025 this conversion is coming from a C-style cast; in that case,
6026 conversions to inaccessible bases are permitted. */
6028 static tree
6029 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
6030 int inner, bool issue_conversion_warnings,
6031 bool c_cast_p, tsubst_flags_t complain)
6033 tree totype = convs->type;
6034 diagnostic_t diag_kind;
6035 int flags;
6036 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6038 if (convs->bad_p && !(complain & tf_error))
6039 return error_mark_node;
6041 if (convs->bad_p
6042 && convs->kind != ck_user
6043 && convs->kind != ck_list
6044 && convs->kind != ck_ambig
6045 && (convs->kind != ck_ref_bind
6046 || (convs->user_conv_p && next_conversion (convs)->bad_p))
6047 && (convs->kind != ck_rvalue
6048 || SCALAR_TYPE_P (totype))
6049 && convs->kind != ck_base)
6051 bool complained = false;
6052 conversion *t = convs;
6054 /* Give a helpful error if this is bad because of excess braces. */
6055 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6056 && SCALAR_TYPE_P (totype)
6057 && CONSTRUCTOR_NELTS (expr) > 0
6058 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
6060 complained = permerror (loc, "too many braces around initializer "
6061 "for %qT", totype);
6062 while (BRACE_ENCLOSED_INITIALIZER_P (expr)
6063 && CONSTRUCTOR_NELTS (expr) == 1)
6064 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6067 /* Give a helpful error if this is bad because a conversion to bool
6068 from std::nullptr_t requires direct-initialization. */
6069 if (NULLPTR_TYPE_P (TREE_TYPE (expr))
6070 && TREE_CODE (totype) == BOOLEAN_TYPE)
6071 complained = permerror (loc, "converting to %qT from %qT requires "
6072 "direct-initialization",
6073 totype, TREE_TYPE (expr));
6075 for (; t ; t = next_conversion (t))
6077 if (t->kind == ck_user && t->cand->reason)
6079 permerror (loc, "invalid user-defined conversion "
6080 "from %qT to %qT", TREE_TYPE (expr), totype);
6081 print_z_candidate (loc, "candidate is:", t->cand);
6082 expr = convert_like_real (t, expr, fn, argnum, 1,
6083 /*issue_conversion_warnings=*/false,
6084 /*c_cast_p=*/false,
6085 complain);
6086 if (convs->kind == ck_ref_bind)
6087 expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
6088 LOOKUP_NORMAL, NULL_TREE,
6089 complain);
6090 else
6091 expr = cp_convert (totype, expr, complain);
6092 if (fn)
6093 inform (DECL_SOURCE_LOCATION (fn),
6094 " initializing argument %P of %qD", argnum, fn);
6095 return expr;
6097 else if (t->kind == ck_user || !t->bad_p)
6099 expr = convert_like_real (t, expr, fn, argnum, 1,
6100 /*issue_conversion_warnings=*/false,
6101 /*c_cast_p=*/false,
6102 complain);
6103 break;
6105 else if (t->kind == ck_ambig)
6106 return convert_like_real (t, expr, fn, argnum, 1,
6107 /*issue_conversion_warnings=*/false,
6108 /*c_cast_p=*/false,
6109 complain);
6110 else if (t->kind == ck_identity)
6111 break;
6113 if (!complained)
6114 complained = permerror (loc, "invalid conversion from %qT to %qT",
6115 TREE_TYPE (expr), totype);
6116 if (complained && fn)
6117 inform (DECL_SOURCE_LOCATION (fn),
6118 " initializing argument %P of %qD", argnum, fn);
6120 return cp_convert (totype, expr, complain);
6123 if (issue_conversion_warnings && (complain & tf_warning))
6124 conversion_null_warnings (totype, expr, fn, argnum);
6126 switch (convs->kind)
6128 case ck_user:
6130 struct z_candidate *cand = convs->cand;
6131 tree convfn = cand->fn;
6132 unsigned i;
6134 /* When converting from an init list we consider explicit
6135 constructors, but actually trying to call one is an error. */
6136 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
6137 /* Unless this is for direct-list-initialization. */
6138 && !DIRECT_LIST_INIT_P (expr))
6140 if (!(complain & tf_error))
6141 return error_mark_node;
6142 error ("converting to %qT from initializer list would use "
6143 "explicit constructor %qD", totype, convfn);
6146 /* If we're initializing from {}, it's value-initialization. */
6147 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6148 && CONSTRUCTOR_NELTS (expr) == 0
6149 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
6151 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
6152 expr = build_value_init (totype, complain);
6153 expr = get_target_expr_sfinae (expr, complain);
6154 if (expr != error_mark_node)
6156 TARGET_EXPR_LIST_INIT_P (expr) = true;
6157 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
6159 return expr;
6162 expr = mark_rvalue_use (expr);
6164 /* Set user_conv_p on the argument conversions, so rvalue/base
6165 handling knows not to allow any more UDCs. */
6166 for (i = 0; i < cand->num_convs; ++i)
6167 cand->convs[i]->user_conv_p = true;
6169 expr = build_over_call (cand, LOOKUP_NORMAL, complain);
6171 /* If this is a constructor or a function returning an aggr type,
6172 we need to build up a TARGET_EXPR. */
6173 if (DECL_CONSTRUCTOR_P (convfn))
6175 expr = build_cplus_new (totype, expr, complain);
6177 /* Remember that this was list-initialization. */
6178 if (convs->check_narrowing && expr != error_mark_node)
6179 TARGET_EXPR_LIST_INIT_P (expr) = true;
6182 return expr;
6184 case ck_identity:
6185 expr = mark_rvalue_use (expr);
6186 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
6188 int nelts = CONSTRUCTOR_NELTS (expr);
6189 if (nelts == 0)
6190 expr = build_value_init (totype, complain);
6191 else if (nelts == 1)
6192 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6193 else
6194 gcc_unreachable ();
6197 if (type_unknown_p (expr))
6198 expr = instantiate_type (totype, expr, complain);
6199 /* Convert a constant to its underlying value, unless we are
6200 about to bind it to a reference, in which case we need to
6201 leave it as an lvalue. */
6202 if (inner >= 0)
6204 expr = decl_constant_value_safe (expr);
6205 if (expr == null_node && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
6206 /* If __null has been converted to an integer type, we do not
6207 want to warn about uses of EXPR as an integer, rather than
6208 as a pointer. */
6209 expr = build_int_cst (totype, 0);
6211 return expr;
6212 case ck_ambig:
6213 /* We leave bad_p off ck_ambig because overload resolution considers
6214 it valid, it just fails when we try to perform it. So we need to
6215 check complain here, too. */
6216 if (complain & tf_error)
6218 /* Call build_user_type_conversion again for the error. */
6219 build_user_type_conversion (totype, convs->u.expr, LOOKUP_NORMAL,
6220 complain);
6221 if (fn)
6222 inform (input_location, " initializing argument %P of %q+D",
6223 argnum, fn);
6225 return error_mark_node;
6227 case ck_list:
6229 /* Conversion to std::initializer_list<T>. */
6230 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
6231 tree new_ctor = build_constructor (init_list_type_node, NULL);
6232 unsigned len = CONSTRUCTOR_NELTS (expr);
6233 tree array, val, field;
6234 vec<constructor_elt, va_gc> *vec = NULL;
6235 unsigned ix;
6237 /* Convert all the elements. */
6238 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
6240 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
6241 1, false, false, complain);
6242 if (sub == error_mark_node)
6243 return sub;
6244 if (!BRACE_ENCLOSED_INITIALIZER_P (val))
6245 check_narrowing (TREE_TYPE (sub), val);
6246 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
6247 if (!TREE_CONSTANT (sub))
6248 TREE_CONSTANT (new_ctor) = false;
6250 /* Build up the array. */
6251 elttype = cp_build_qualified_type
6252 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
6253 array = build_array_of_n_type (elttype, len);
6254 array = finish_compound_literal (array, new_ctor, complain);
6255 /* Take the address explicitly rather than via decay_conversion
6256 to avoid the error about taking the address of a temporary. */
6257 array = cp_build_addr_expr (array, complain);
6258 array = cp_convert (build_pointer_type (elttype), array, complain);
6259 if (array == error_mark_node)
6260 return error_mark_node;
6262 /* Build up the initializer_list object. */
6263 totype = complete_type (totype);
6264 field = next_initializable_field (TYPE_FIELDS (totype));
6265 CONSTRUCTOR_APPEND_ELT (vec, field, array);
6266 field = next_initializable_field (DECL_CHAIN (field));
6267 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
6268 new_ctor = build_constructor (totype, vec);
6269 return get_target_expr_sfinae (new_ctor, complain);
6272 case ck_aggr:
6273 if (TREE_CODE (totype) == COMPLEX_TYPE)
6275 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
6276 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
6277 real = perform_implicit_conversion (TREE_TYPE (totype),
6278 real, complain);
6279 imag = perform_implicit_conversion (TREE_TYPE (totype),
6280 imag, complain);
6281 expr = build2 (COMPLEX_EXPR, totype, real, imag);
6282 return fold_if_not_in_template (expr);
6284 expr = reshape_init (totype, expr, complain);
6285 expr = get_target_expr_sfinae (digest_init (totype, expr, complain),
6286 complain);
6287 if (expr != error_mark_node)
6288 TARGET_EXPR_LIST_INIT_P (expr) = true;
6289 return expr;
6291 default:
6292 break;
6295 expr = convert_like_real (next_conversion (convs), expr, fn, argnum,
6296 convs->kind == ck_ref_bind ? -1 : 1,
6297 convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
6298 c_cast_p,
6299 complain);
6300 if (expr == error_mark_node)
6301 return error_mark_node;
6303 switch (convs->kind)
6305 case ck_rvalue:
6306 expr = decay_conversion (expr, complain);
6307 if (expr == error_mark_node)
6308 return error_mark_node;
6310 if (! MAYBE_CLASS_TYPE_P (totype))
6311 return expr;
6312 /* Else fall through. */
6313 case ck_base:
6314 if (convs->kind == ck_base && !convs->need_temporary_p)
6316 /* We are going to bind a reference directly to a base-class
6317 subobject of EXPR. */
6318 /* Build an expression for `*((base*) &expr)'. */
6319 expr = cp_build_addr_expr (expr, complain);
6320 expr = convert_to_base (expr, build_pointer_type (totype),
6321 !c_cast_p, /*nonnull=*/true, complain);
6322 expr = cp_build_indirect_ref (expr, RO_IMPLICIT_CONVERSION, complain);
6323 return expr;
6326 /* Copy-initialization where the cv-unqualified version of the source
6327 type is the same class as, or a derived class of, the class of the
6328 destination [is treated as direct-initialization]. [dcl.init] */
6329 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
6330 if (convs->user_conv_p)
6331 /* This conversion is being done in the context of a user-defined
6332 conversion (i.e. the second step of copy-initialization), so
6333 don't allow any more. */
6334 flags |= LOOKUP_NO_CONVERSION;
6335 if (convs->rvaluedness_matches_p)
6336 flags |= LOOKUP_PREFER_RVALUE;
6337 if (TREE_CODE (expr) == TARGET_EXPR
6338 && TARGET_EXPR_LIST_INIT_P (expr))
6339 /* Copy-list-initialization doesn't actually involve a copy. */
6340 return expr;
6341 expr = build_temp (expr, totype, flags, &diag_kind, complain);
6342 if (diag_kind && complain)
6344 maybe_print_user_conv_context (convs);
6345 if (fn)
6346 inform (DECL_SOURCE_LOCATION (fn),
6347 " initializing argument %P of %qD", argnum, fn);
6350 return build_cplus_new (totype, expr, complain);
6352 case ck_ref_bind:
6354 tree ref_type = totype;
6356 if (convs->bad_p && !next_conversion (convs)->bad_p)
6358 tree extype = TREE_TYPE (expr);
6359 if (TYPE_REF_IS_RVALUE (ref_type)
6360 && real_lvalue_p (expr))
6361 error_at (loc, "cannot bind %qT lvalue to %qT",
6362 extype, totype);
6363 else if (!TYPE_REF_IS_RVALUE (ref_type) && !real_lvalue_p (expr)
6364 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
6365 error_at (loc, "invalid initialization of non-const reference of "
6366 "type %qT from an rvalue of type %qT", totype, extype);
6367 else if (!reference_compatible_p (TREE_TYPE (totype), extype))
6368 error_at (loc, "binding %qT to reference of type %qT "
6369 "discards qualifiers", extype, totype);
6370 else
6371 gcc_unreachable ();
6372 maybe_print_user_conv_context (convs);
6373 if (fn)
6374 inform (input_location,
6375 " initializing argument %P of %q+D", argnum, fn);
6376 return error_mark_node;
6379 /* If necessary, create a temporary.
6381 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
6382 that need temporaries, even when their types are reference
6383 compatible with the type of reference being bound, so the
6384 upcoming call to cp_build_addr_expr doesn't fail. */
6385 if (convs->need_temporary_p
6386 || TREE_CODE (expr) == CONSTRUCTOR
6387 || TREE_CODE (expr) == VA_ARG_EXPR)
6389 /* Otherwise, a temporary of type "cv1 T1" is created and
6390 initialized from the initializer expression using the rules
6391 for a non-reference copy-initialization (8.5). */
6393 tree type = TREE_TYPE (ref_type);
6394 cp_lvalue_kind lvalue = real_lvalue_p (expr);
6396 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6397 (type, next_conversion (convs)->type));
6398 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
6399 && !TYPE_REF_IS_RVALUE (ref_type))
6401 /* If the reference is volatile or non-const, we
6402 cannot create a temporary. */
6403 if (lvalue & clk_bitfield)
6404 error_at (loc, "cannot bind bitfield %qE to %qT",
6405 expr, ref_type);
6406 else if (lvalue & clk_packed)
6407 error_at (loc, "cannot bind packed field %qE to %qT",
6408 expr, ref_type);
6409 else
6410 error_at (loc, "cannot bind rvalue %qE to %qT",
6411 expr, ref_type);
6412 return error_mark_node;
6414 /* If the source is a packed field, and we must use a copy
6415 constructor, then building the target expr will require
6416 binding the field to the reference parameter to the
6417 copy constructor, and we'll end up with an infinite
6418 loop. If we can use a bitwise copy, then we'll be
6419 OK. */
6420 if ((lvalue & clk_packed)
6421 && CLASS_TYPE_P (type)
6422 && type_has_nontrivial_copy_init (type))
6424 error_at (loc, "cannot bind packed field %qE to %qT",
6425 expr, ref_type);
6426 return error_mark_node;
6428 if (lvalue & clk_bitfield)
6430 expr = convert_bitfield_to_declared_type (expr);
6431 expr = fold_convert (type, expr);
6433 expr = build_target_expr_with_type (expr, type, complain);
6436 /* Take the address of the thing to which we will bind the
6437 reference. */
6438 expr = cp_build_addr_expr (expr, complain);
6439 if (expr == error_mark_node)
6440 return error_mark_node;
6442 /* Convert it to a pointer to the type referred to by the
6443 reference. This will adjust the pointer if a derived to
6444 base conversion is being performed. */
6445 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
6446 expr, complain);
6447 /* Convert the pointer to the desired reference type. */
6448 return build_nop (ref_type, expr);
6451 case ck_lvalue:
6452 return decay_conversion (expr, complain);
6454 case ck_qual:
6455 /* Warn about deprecated conversion if appropriate. */
6456 string_conv_p (totype, expr, 1);
6457 break;
6459 case ck_ptr:
6460 if (convs->base_p)
6461 expr = convert_to_base (expr, totype, !c_cast_p,
6462 /*nonnull=*/false, complain);
6463 return build_nop (totype, expr);
6465 case ck_pmem:
6466 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
6467 c_cast_p, complain);
6469 default:
6470 break;
6473 if (convs->check_narrowing)
6474 check_narrowing (totype, expr);
6476 if (issue_conversion_warnings)
6477 expr = cp_convert_and_check (totype, expr, complain);
6478 else
6479 expr = cp_convert (totype, expr, complain);
6481 return expr;
6484 /* ARG is being passed to a varargs function. Perform any conversions
6485 required. Return the converted value. */
6487 tree
6488 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
6490 tree arg_type;
6491 location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
6493 /* [expr.call]
6495 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6496 standard conversions are performed. */
6497 arg = decay_conversion (arg, complain);
6498 arg_type = TREE_TYPE (arg);
6499 /* [expr.call]
6501 If the argument has integral or enumeration type that is subject
6502 to the integral promotions (_conv.prom_), or a floating point
6503 type that is subject to the floating point promotion
6504 (_conv.fpprom_), the value of the argument is converted to the
6505 promoted type before the call. */
6506 if (TREE_CODE (arg_type) == REAL_TYPE
6507 && (TYPE_PRECISION (arg_type)
6508 < TYPE_PRECISION (double_type_node))
6509 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
6511 if ((complain & tf_warning)
6512 && warn_double_promotion && !c_inhibit_evaluation_warnings)
6513 warning_at (loc, OPT_Wdouble_promotion,
6514 "implicit conversion from %qT to %qT when passing "
6515 "argument to function",
6516 arg_type, double_type_node);
6517 arg = convert_to_real (double_type_node, arg);
6519 else if (NULLPTR_TYPE_P (arg_type))
6520 arg = null_pointer_node;
6521 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
6523 if (SCOPED_ENUM_P (arg_type))
6525 tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
6526 complain);
6527 prom = cp_perform_integral_promotions (prom, complain);
6528 if (abi_version_crosses (6)
6529 && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
6530 && (complain & tf_warning))
6531 warning_at (loc, OPT_Wabi, "scoped enum %qT passed through ... as "
6532 "%qT before -fabi-version=6, %qT after", arg_type,
6533 TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
6534 if (!abi_version_at_least (6))
6535 arg = prom;
6537 else
6538 arg = cp_perform_integral_promotions (arg, complain);
6541 arg = require_complete_type_sfinae (arg, complain);
6542 arg_type = TREE_TYPE (arg);
6544 if (arg != error_mark_node
6545 /* In a template (or ill-formed code), we can have an incomplete type
6546 even after require_complete_type_sfinae, in which case we don't know
6547 whether it has trivial copy or not. */
6548 && COMPLETE_TYPE_P (arg_type))
6550 /* Build up a real lvalue-to-rvalue conversion in case the
6551 copy constructor is trivial but not callable. */
6552 if (!cp_unevaluated_operand && CLASS_TYPE_P (arg_type))
6553 force_rvalue (arg, complain);
6555 /* [expr.call] 5.2.2/7:
6556 Passing a potentially-evaluated argument of class type (Clause 9)
6557 with a non-trivial copy constructor or a non-trivial destructor
6558 with no corresponding parameter is conditionally-supported, with
6559 implementation-defined semantics.
6561 We used to just warn here and do a bitwise copy, but now
6562 cp_expr_size will abort if we try to do that.
6564 If the call appears in the context of a sizeof expression,
6565 it is not potentially-evaluated. */
6566 if (cp_unevaluated_operand == 0
6567 && (type_has_nontrivial_copy_init (arg_type)
6568 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type)))
6570 if (complain & tf_error)
6571 error_at (loc, "cannot pass objects of non-trivially-copyable "
6572 "type %q#T through %<...%>", arg_type);
6573 return error_mark_node;
6577 return arg;
6580 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
6582 tree
6583 build_x_va_arg (source_location loc, tree expr, tree type)
6585 if (processing_template_decl)
6586 return build_min (VA_ARG_EXPR, type, expr);
6588 type = complete_type_or_else (type, NULL_TREE);
6590 if (expr == error_mark_node || !type)
6591 return error_mark_node;
6593 expr = mark_lvalue_use (expr);
6595 if (type_has_nontrivial_copy_init (type)
6596 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
6597 || TREE_CODE (type) == REFERENCE_TYPE)
6599 /* Remove reference types so we don't ICE later on. */
6600 tree type1 = non_reference (type);
6601 /* conditionally-supported behavior [expr.call] 5.2.2/7. */
6602 error ("cannot receive objects of non-trivially-copyable type %q#T "
6603 "through %<...%>; ", type);
6604 expr = convert (build_pointer_type (type1), null_node);
6605 expr = cp_build_indirect_ref (expr, RO_NULL, tf_warning_or_error);
6606 return expr;
6609 return build_va_arg (loc, expr, type);
6612 /* TYPE has been given to va_arg. Apply the default conversions which
6613 would have happened when passed via ellipsis. Return the promoted
6614 type, or the passed type if there is no change. */
6616 tree
6617 cxx_type_promotes_to (tree type)
6619 tree promote;
6621 /* Perform the array-to-pointer and function-to-pointer
6622 conversions. */
6623 type = type_decays_to (type);
6625 promote = type_promotes_to (type);
6626 if (same_type_p (type, promote))
6627 promote = type;
6629 return promote;
6632 /* ARG is a default argument expression being passed to a parameter of
6633 the indicated TYPE, which is a parameter to FN. PARMNUM is the
6634 zero-based argument number. Do any required conversions. Return
6635 the converted value. */
6637 static GTY(()) vec<tree, va_gc> *default_arg_context;
6638 void
6639 push_defarg_context (tree fn)
6640 { vec_safe_push (default_arg_context, fn); }
6642 void
6643 pop_defarg_context (void)
6644 { default_arg_context->pop (); }
6646 tree
6647 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
6648 tsubst_flags_t complain)
6650 int i;
6651 tree t;
6653 /* See through clones. */
6654 fn = DECL_ORIGIN (fn);
6656 /* Detect recursion. */
6657 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
6658 if (t == fn)
6660 if (complain & tf_error)
6661 error ("recursive evaluation of default argument for %q#D", fn);
6662 return error_mark_node;
6665 /* If the ARG is an unparsed default argument expression, the
6666 conversion cannot be performed. */
6667 if (TREE_CODE (arg) == DEFAULT_ARG)
6669 if (complain & tf_error)
6670 error ("call to %qD uses the default argument for parameter %P, which "
6671 "is not yet defined", fn, parmnum);
6672 return error_mark_node;
6675 push_defarg_context (fn);
6677 if (fn && DECL_TEMPLATE_INFO (fn))
6678 arg = tsubst_default_argument (fn, type, arg, complain);
6680 /* Due to:
6682 [dcl.fct.default]
6684 The names in the expression are bound, and the semantic
6685 constraints are checked, at the point where the default
6686 expressions appears.
6688 we must not perform access checks here. */
6689 push_deferring_access_checks (dk_no_check);
6690 /* We must make a copy of ARG, in case subsequent processing
6691 alters any part of it. */
6692 arg = break_out_target_exprs (arg);
6693 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
6694 ICR_DEFAULT_ARGUMENT, fn, parmnum,
6695 complain);
6696 arg = convert_for_arg_passing (type, arg, complain);
6697 pop_deferring_access_checks();
6699 pop_defarg_context ();
6701 return arg;
6704 /* Returns the type which will really be used for passing an argument of
6705 type TYPE. */
6707 tree
6708 type_passed_as (tree type)
6710 /* Pass classes with copy ctors by invisible reference. */
6711 if (TREE_ADDRESSABLE (type))
6713 type = build_reference_type (type);
6714 /* There are no other pointers to this temporary. */
6715 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
6717 else if (targetm.calls.promote_prototypes (type)
6718 && INTEGRAL_TYPE_P (type)
6719 && COMPLETE_TYPE_P (type)
6720 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
6721 type = integer_type_node;
6723 return type;
6726 /* Actually perform the appropriate conversion. */
6728 tree
6729 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
6731 tree bitfield_type;
6733 /* If VAL is a bitfield, then -- since it has already been converted
6734 to TYPE -- it cannot have a precision greater than TYPE.
6736 If it has a smaller precision, we must widen it here. For
6737 example, passing "int f:3;" to a function expecting an "int" will
6738 not result in any conversion before this point.
6740 If the precision is the same we must not risk widening. For
6741 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
6742 often have type "int", even though the C++ type for the field is
6743 "long long". If the value is being passed to a function
6744 expecting an "int", then no conversions will be required. But,
6745 if we call convert_bitfield_to_declared_type, the bitfield will
6746 be converted to "long long". */
6747 bitfield_type = is_bitfield_expr_with_lowered_type (val);
6748 if (bitfield_type
6749 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
6750 val = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type), val);
6752 if (val == error_mark_node)
6754 /* Pass classes with copy ctors by invisible reference. */
6755 else if (TREE_ADDRESSABLE (type))
6756 val = build1 (ADDR_EXPR, build_reference_type (type), val);
6757 else if (targetm.calls.promote_prototypes (type)
6758 && INTEGRAL_TYPE_P (type)
6759 && COMPLETE_TYPE_P (type)
6760 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
6761 val = cp_perform_integral_promotions (val, complain);
6762 if ((complain & tf_warning)
6763 && warn_suggest_attribute_format)
6765 tree rhstype = TREE_TYPE (val);
6766 const enum tree_code coder = TREE_CODE (rhstype);
6767 const enum tree_code codel = TREE_CODE (type);
6768 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6769 && coder == codel
6770 && check_missing_format_attribute (type, rhstype))
6771 warning (OPT_Wsuggest_attribute_format,
6772 "argument of function call might be a candidate for a format attribute");
6774 return val;
6777 /* Returns true iff FN is a function with magic varargs, i.e. ones for
6778 which no conversions at all should be done. This is true for some
6779 builtins which don't act like normal functions. */
6781 bool
6782 magic_varargs_p (tree fn)
6784 if (flag_cilkplus && is_cilkplus_reduce_builtin (fn) != BUILT_IN_NONE)
6785 return true;
6787 if (DECL_BUILT_IN (fn))
6788 switch (DECL_FUNCTION_CODE (fn))
6790 case BUILT_IN_CLASSIFY_TYPE:
6791 case BUILT_IN_CONSTANT_P:
6792 case BUILT_IN_NEXT_ARG:
6793 case BUILT_IN_VA_START:
6794 return true;
6796 default:;
6797 return lookup_attribute ("type generic",
6798 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
6801 return false;
6804 /* Returns the decl of the dispatcher function if FN is a function version. */
6806 tree
6807 get_function_version_dispatcher (tree fn)
6809 tree dispatcher_decl = NULL;
6811 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6812 && DECL_FUNCTION_VERSIONED (fn));
6814 gcc_assert (targetm.get_function_versions_dispatcher);
6815 dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
6817 if (dispatcher_decl == NULL)
6819 error_at (input_location, "use of multiversioned function "
6820 "without a default");
6821 return NULL;
6824 retrofit_lang_decl (dispatcher_decl);
6825 gcc_assert (dispatcher_decl != NULL);
6826 return dispatcher_decl;
6829 /* fn is a function version dispatcher that is marked used. Mark all the
6830 semantically identical function versions it will dispatch as used. */
6832 void
6833 mark_versions_used (tree fn)
6835 struct cgraph_node *node;
6836 struct cgraph_function_version_info *node_v;
6837 struct cgraph_function_version_info *it_v;
6839 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6841 node = cgraph_get_node (fn);
6842 if (node == NULL)
6843 return;
6845 gcc_assert (node->dispatcher_function);
6847 node_v = get_cgraph_node_version (node);
6848 if (node_v == NULL)
6849 return;
6851 /* All semantically identical versions are chained. Traverse and mark each
6852 one of them as used. */
6853 it_v = node_v->next;
6854 while (it_v != NULL)
6856 mark_used (it_v->this_node->decl);
6857 it_v = it_v->next;
6861 /* Subroutine of the various build_*_call functions. Overload resolution
6862 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
6863 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
6864 bitmask of various LOOKUP_* flags which apply to the call itself. */
6866 static tree
6867 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
6869 tree fn = cand->fn;
6870 const vec<tree, va_gc> *args = cand->args;
6871 tree first_arg = cand->first_arg;
6872 conversion **convs = cand->convs;
6873 conversion *conv;
6874 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
6875 int parmlen;
6876 tree val;
6877 int i = 0;
6878 int j = 0;
6879 unsigned int arg_index = 0;
6880 int is_method = 0;
6881 int nargs;
6882 tree *argarray;
6883 bool already_used = false;
6885 /* In a template, there is no need to perform all of the work that
6886 is normally done. We are only interested in the type of the call
6887 expression, i.e., the return type of the function. Any semantic
6888 errors will be deferred until the template is instantiated. */
6889 if (processing_template_decl)
6891 tree expr, addr;
6892 tree return_type;
6893 const tree *argarray;
6894 unsigned int nargs;
6896 return_type = TREE_TYPE (TREE_TYPE (fn));
6897 nargs = vec_safe_length (args);
6898 if (first_arg == NULL_TREE)
6899 argarray = args->address ();
6900 else
6902 tree *alcarray;
6903 unsigned int ix;
6904 tree arg;
6906 ++nargs;
6907 alcarray = XALLOCAVEC (tree, nargs);
6908 alcarray[0] = build_this (first_arg);
6909 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
6910 alcarray[ix + 1] = arg;
6911 argarray = alcarray;
6914 addr = build_addr_func (fn, complain);
6915 if (addr == error_mark_node)
6916 return error_mark_node;
6917 expr = build_call_array_loc (input_location, return_type,
6918 addr, nargs, argarray);
6919 if (TREE_THIS_VOLATILE (fn) && cfun)
6920 current_function_returns_abnormally = 1;
6921 return convert_from_reference (expr);
6924 /* Give any warnings we noticed during overload resolution. */
6925 if (cand->warnings && (complain & tf_warning))
6927 struct candidate_warning *w;
6928 for (w = cand->warnings; w; w = w->next)
6929 joust (cand, w->loser, 1, complain);
6932 /* Make =delete work with SFINAE. */
6933 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
6934 return error_mark_node;
6936 if (DECL_FUNCTION_MEMBER_P (fn))
6938 tree access_fn;
6939 /* If FN is a template function, two cases must be considered.
6940 For example:
6942 struct A {
6943 protected:
6944 template <class T> void f();
6946 template <class T> struct B {
6947 protected:
6948 void g();
6950 struct C : A, B<int> {
6951 using A::f; // #1
6952 using B<int>::g; // #2
6955 In case #1 where `A::f' is a member template, DECL_ACCESS is
6956 recorded in the primary template but not in its specialization.
6957 We check access of FN using its primary template.
6959 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
6960 because it is a member of class template B, DECL_ACCESS is
6961 recorded in the specialization `B<int>::g'. We cannot use its
6962 primary template because `B<T>::g' and `B<int>::g' may have
6963 different access. */
6964 if (DECL_TEMPLATE_INFO (fn)
6965 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
6966 access_fn = DECL_TI_TEMPLATE (fn);
6967 else
6968 access_fn = fn;
6969 if (!perform_or_defer_access_check (cand->access_path, access_fn,
6970 fn, complain))
6971 return error_mark_node;
6974 /* If we're checking for implicit delete, don't bother with argument
6975 conversions. */
6976 if (flags & LOOKUP_SPECULATIVE)
6978 if (DECL_DELETED_FN (fn))
6980 if (complain & tf_error)
6981 mark_used (fn);
6982 return error_mark_node;
6984 if (cand->viable == 1)
6985 return fn;
6986 else if (!(complain & tf_error))
6987 /* Reject bad conversions now. */
6988 return error_mark_node;
6989 /* else continue to get conversion error. */
6992 /* N3276 magic doesn't apply to nested calls. */
6993 int decltype_flag = (complain & tf_decltype);
6994 complain &= ~tf_decltype;
6996 /* Find maximum size of vector to hold converted arguments. */
6997 parmlen = list_length (parm);
6998 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
6999 if (parmlen > nargs)
7000 nargs = parmlen;
7001 argarray = XALLOCAVEC (tree, nargs);
7003 /* The implicit parameters to a constructor are not considered by overload
7004 resolution, and must be of the proper type. */
7005 if (DECL_CONSTRUCTOR_P (fn))
7007 tree object_arg;
7008 if (first_arg != NULL_TREE)
7010 object_arg = first_arg;
7011 first_arg = NULL_TREE;
7013 else
7015 object_arg = (*args)[arg_index];
7016 ++arg_index;
7018 argarray[j++] = build_this (object_arg);
7019 parm = TREE_CHAIN (parm);
7020 /* We should never try to call the abstract constructor. */
7021 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
7023 if (DECL_HAS_VTT_PARM_P (fn))
7025 argarray[j++] = (*args)[arg_index];
7026 ++arg_index;
7027 parm = TREE_CHAIN (parm);
7030 /* Bypass access control for 'this' parameter. */
7031 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7033 tree parmtype = TREE_VALUE (parm);
7034 tree arg = build_this (first_arg != NULL_TREE
7035 ? first_arg
7036 : (*args)[arg_index]);
7037 tree argtype = TREE_TYPE (arg);
7038 tree converted_arg;
7039 tree base_binfo;
7041 if (convs[i]->bad_p)
7043 if (complain & tf_error)
7045 if (permerror (input_location, "passing %qT as %<this%> "
7046 "argument discards qualifiers",
7047 TREE_TYPE (argtype)))
7048 inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
7050 else
7051 return error_mark_node;
7054 /* See if the function member or the whole class type is declared
7055 final and the call can be devirtualized. */
7056 if (DECL_FINAL_P (fn)
7057 || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
7058 flags |= LOOKUP_NONVIRTUAL;
7060 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
7061 X is called for an object that is not of type X, or of a type
7062 derived from X, the behavior is undefined.
7064 So we can assume that anything passed as 'this' is non-null, and
7065 optimize accordingly. */
7066 gcc_assert (TYPE_PTR_P (parmtype));
7067 /* Convert to the base in which the function was declared. */
7068 gcc_assert (cand->conversion_path != NULL_TREE);
7069 converted_arg = build_base_path (PLUS_EXPR,
7070 arg,
7071 cand->conversion_path,
7072 1, complain);
7073 /* Check that the base class is accessible. */
7074 if (!accessible_base_p (TREE_TYPE (argtype),
7075 BINFO_TYPE (cand->conversion_path), true))
7077 if (complain & tf_error)
7078 error ("%qT is not an accessible base of %qT",
7079 BINFO_TYPE (cand->conversion_path),
7080 TREE_TYPE (argtype));
7081 else
7082 return error_mark_node;
7084 /* If fn was found by a using declaration, the conversion path
7085 will be to the derived class, not the base declaring fn. We
7086 must convert from derived to base. */
7087 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
7088 TREE_TYPE (parmtype), ba_unique,
7089 NULL, complain);
7090 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
7091 base_binfo, 1, complain);
7093 argarray[j++] = converted_arg;
7094 parm = TREE_CHAIN (parm);
7095 if (first_arg != NULL_TREE)
7096 first_arg = NULL_TREE;
7097 else
7098 ++arg_index;
7099 ++i;
7100 is_method = 1;
7103 gcc_assert (first_arg == NULL_TREE);
7104 for (; arg_index < vec_safe_length (args) && parm;
7105 parm = TREE_CHAIN (parm), ++arg_index, ++i)
7107 tree type = TREE_VALUE (parm);
7108 tree arg = (*args)[arg_index];
7109 bool conversion_warning = true;
7111 conv = convs[i];
7113 /* If the argument is NULL and used to (implicitly) instantiate a
7114 template function (and bind one of the template arguments to
7115 the type of 'long int'), we don't want to warn about passing NULL
7116 to non-pointer argument.
7117 For example, if we have this template function:
7119 template<typename T> void func(T x) {}
7121 we want to warn (when -Wconversion is enabled) in this case:
7123 void foo() {
7124 func<int>(NULL);
7127 but not in this case:
7129 void foo() {
7130 func(NULL);
7133 if (arg == null_node
7134 && DECL_TEMPLATE_INFO (fn)
7135 && cand->template_decl
7136 && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
7137 conversion_warning = false;
7139 /* Warn about initializer_list deduction that isn't currently in the
7140 working draft. */
7141 if (cxx_dialect > cxx98
7142 && flag_deduce_init_list
7143 && cand->template_decl
7144 && is_std_init_list (non_reference (type))
7145 && BRACE_ENCLOSED_INITIALIZER_P (arg))
7147 tree tmpl = TI_TEMPLATE (cand->template_decl);
7148 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
7149 tree patparm = get_pattern_parm (realparm, tmpl);
7150 tree pattype = TREE_TYPE (patparm);
7151 if (PACK_EXPANSION_P (pattype))
7152 pattype = PACK_EXPANSION_PATTERN (pattype);
7153 pattype = non_reference (pattype);
7155 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
7156 && (cand->explicit_targs == NULL_TREE
7157 || (TREE_VEC_LENGTH (cand->explicit_targs)
7158 <= TEMPLATE_TYPE_IDX (pattype))))
7160 pedwarn (input_location, 0, "deducing %qT as %qT",
7161 non_reference (TREE_TYPE (patparm)),
7162 non_reference (type));
7163 pedwarn (input_location, 0, " in call to %q+D", cand->fn);
7164 pedwarn (input_location, 0,
7165 " (you can disable this with -fno-deduce-init-list)");
7168 val = convert_like_with_context (conv, arg, fn, i - is_method,
7169 conversion_warning
7170 ? complain
7171 : complain & (~tf_warning));
7173 val = convert_for_arg_passing (type, val, complain);
7175 if (val == error_mark_node)
7176 return error_mark_node;
7177 else
7178 argarray[j++] = val;
7181 /* Default arguments */
7182 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
7184 if (TREE_VALUE (parm) == error_mark_node)
7185 return error_mark_node;
7186 argarray[j++] = convert_default_arg (TREE_VALUE (parm),
7187 TREE_PURPOSE (parm),
7188 fn, i - is_method,
7189 complain);
7192 /* Ellipsis */
7193 for (; arg_index < vec_safe_length (args); ++arg_index)
7195 tree a = (*args)[arg_index];
7196 if (magic_varargs_p (fn))
7197 /* Do no conversions for magic varargs. */
7198 a = mark_type_use (a);
7199 else
7200 a = convert_arg_to_ellipsis (a, complain);
7201 argarray[j++] = a;
7204 gcc_assert (j <= nargs);
7205 nargs = j;
7207 check_function_arguments (TREE_TYPE (fn), nargs, argarray);
7209 /* Avoid actually calling copy constructors and copy assignment operators,
7210 if possible. */
7212 if (! flag_elide_constructors)
7213 /* Do things the hard way. */;
7214 else if (cand->num_convs == 1
7215 && (DECL_COPY_CONSTRUCTOR_P (fn)
7216 || DECL_MOVE_CONSTRUCTOR_P (fn)))
7218 tree targ;
7219 tree arg = argarray[num_artificial_parms_for (fn)];
7220 tree fa;
7221 bool trivial = trivial_fn_p (fn);
7223 /* Pull out the real argument, disregarding const-correctness. */
7224 targ = arg;
7225 while (CONVERT_EXPR_P (targ)
7226 || TREE_CODE (targ) == NON_LVALUE_EXPR)
7227 targ = TREE_OPERAND (targ, 0);
7228 if (TREE_CODE (targ) == ADDR_EXPR)
7230 targ = TREE_OPERAND (targ, 0);
7231 if (!same_type_ignoring_top_level_qualifiers_p
7232 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
7233 targ = NULL_TREE;
7235 else
7236 targ = NULL_TREE;
7238 if (targ)
7239 arg = targ;
7240 else
7241 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
7243 /* [class.copy]: the copy constructor is implicitly defined even if
7244 the implementation elided its use. */
7245 if (!trivial || DECL_DELETED_FN (fn))
7247 mark_used (fn);
7248 already_used = true;
7251 /* If we're creating a temp and we already have one, don't create a
7252 new one. If we're not creating a temp but we get one, use
7253 INIT_EXPR to collapse the temp into our target. Otherwise, if the
7254 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
7255 temp or an INIT_EXPR otherwise. */
7256 fa = argarray[0];
7257 if (is_dummy_object (fa))
7259 if (TREE_CODE (arg) == TARGET_EXPR)
7260 return arg;
7261 else if (trivial)
7262 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
7264 else if (TREE_CODE (arg) == TARGET_EXPR || trivial)
7266 tree to = stabilize_reference (cp_build_indirect_ref (fa, RO_NULL,
7267 complain));
7269 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
7270 return val;
7273 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
7274 && trivial_fn_p (fn)
7275 && !DECL_DELETED_FN (fn))
7277 tree to = stabilize_reference
7278 (cp_build_indirect_ref (argarray[0], RO_NULL, complain));
7279 tree type = TREE_TYPE (to);
7280 tree as_base = CLASSTYPE_AS_BASE (type);
7281 tree arg = argarray[1];
7283 if (is_really_empty_class (type))
7285 /* Avoid copying empty classes. */
7286 val = build2 (COMPOUND_EXPR, void_type_node, to, arg);
7287 TREE_NO_WARNING (val) = 1;
7288 val = build2 (COMPOUND_EXPR, type, val, to);
7289 TREE_NO_WARNING (val) = 1;
7291 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
7293 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
7294 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
7296 else
7298 /* We must only copy the non-tail padding parts. */
7299 tree arg0, arg2, t;
7300 tree array_type, alias_set;
7302 arg2 = TYPE_SIZE_UNIT (as_base);
7303 arg0 = cp_build_addr_expr (to, complain);
7305 array_type = build_array_type (char_type_node,
7306 build_index_type
7307 (size_binop (MINUS_EXPR,
7308 arg2, size_int (1))));
7309 alias_set = build_int_cst (build_pointer_type (type), 0);
7310 t = build2 (MODIFY_EXPR, void_type_node,
7311 build2 (MEM_REF, array_type, arg0, alias_set),
7312 build2 (MEM_REF, array_type, arg, alias_set));
7313 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
7314 TREE_NO_WARNING (val) = 1;
7317 return val;
7319 else if (DECL_DESTRUCTOR_P (fn)
7320 && trivial_fn_p (fn)
7321 && !DECL_DELETED_FN (fn))
7322 return fold_convert (void_type_node, argarray[0]);
7323 /* FIXME handle trivial default constructor, too. */
7325 /* For calls to a multi-versioned function, overload resolution
7326 returns the function with the highest target priority, that is,
7327 the version that will checked for dispatching first. If this
7328 version is inlinable, a direct call to this version can be made
7329 otherwise the call should go through the dispatcher. */
7331 if (DECL_FUNCTION_VERSIONED (fn)
7332 && (current_function_decl == NULL
7333 || !targetm.target_option.can_inline_p (current_function_decl, fn)))
7335 fn = get_function_version_dispatcher (fn);
7336 if (fn == NULL)
7337 return NULL;
7338 if (!already_used)
7339 mark_versions_used (fn);
7342 if (!already_used
7343 && !mark_used (fn))
7344 return error_mark_node;
7346 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
7347 /* Don't mess with virtual lookup in fold_non_dependent_expr; virtual
7348 functions can't be constexpr. */
7349 && !in_template_function ())
7351 tree t;
7352 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
7353 DECL_CONTEXT (fn),
7354 ba_any, NULL, complain);
7355 gcc_assert (binfo && binfo != error_mark_node);
7357 /* Warn about deprecated virtual functions now, since we're about
7358 to throw away the decl. */
7359 if (TREE_DEPRECATED (fn))
7360 warn_deprecated_use (fn, NULL_TREE);
7362 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
7363 complain);
7364 if (TREE_SIDE_EFFECTS (argarray[0]))
7365 argarray[0] = save_expr (argarray[0]);
7366 t = build_pointer_type (TREE_TYPE (fn));
7367 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
7368 fn = build_java_interface_fn_ref (fn, argarray[0]);
7369 else
7370 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
7371 TREE_TYPE (fn) = t;
7373 else
7375 fn = build_addr_func (fn, complain);
7376 if (fn == error_mark_node)
7377 return error_mark_node;
7380 tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
7381 if (TREE_CODE (call) == CALL_EXPR
7382 && (cand->flags & LOOKUP_LIST_INIT_CTOR))
7383 CALL_EXPR_LIST_INIT_P (call) = true;
7384 return call;
7387 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
7388 This function performs no overload resolution, conversion, or other
7389 high-level operations. */
7391 tree
7392 build_cxx_call (tree fn, int nargs, tree *argarray,
7393 tsubst_flags_t complain)
7395 tree fndecl;
7396 int optimize_sav;
7398 /* Remember roughly where this call is. */
7399 location_t loc = EXPR_LOC_OR_LOC (fn, input_location);
7400 fn = build_call_a (fn, nargs, argarray);
7401 SET_EXPR_LOCATION (fn, loc);
7403 fndecl = get_callee_fndecl (fn);
7405 /* Check that arguments to builtin functions match the expectations. */
7406 if (fndecl
7407 && DECL_BUILT_IN (fndecl)
7408 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7409 && !check_builtin_function_arguments (fndecl, nargs, argarray))
7410 return error_mark_node;
7412 /* If it is a built-in array notation function, then the return type of
7413 the function is the element type of the array passed in as array
7414 notation (i.e. the first parameter of the function). */
7415 if (flag_cilkplus && TREE_CODE (fn) == CALL_EXPR)
7417 enum built_in_function bif =
7418 is_cilkplus_reduce_builtin (CALL_EXPR_FN (fn));
7419 if (bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
7420 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
7421 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
7422 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
7423 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE
7424 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
7426 if (call_expr_nargs (fn) == 0)
7428 error_at (EXPR_LOCATION (fn), "Invalid builtin arguments");
7429 return error_mark_node;
7431 /* for bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO or
7432 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO or
7433 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO or
7434 BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO or
7435 BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND or
7436 BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
7437 The pre-defined return-type is the correct one. */
7438 tree array_ntn = CALL_EXPR_ARG (fn, 0);
7439 TREE_TYPE (fn) = TREE_TYPE (array_ntn);
7440 return fn;
7444 /* Some built-in function calls will be evaluated at compile-time in
7445 fold (). Set optimize to 1 when folding __builtin_constant_p inside
7446 a constexpr function so that fold_builtin_1 doesn't fold it to 0. */
7447 optimize_sav = optimize;
7448 if (!optimize && fndecl && DECL_IS_BUILTIN_CONSTANT_P (fndecl)
7449 && current_function_decl
7450 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
7451 optimize = 1;
7452 fn = fold_if_not_in_template (fn);
7453 optimize = optimize_sav;
7455 if (VOID_TYPE_P (TREE_TYPE (fn)))
7456 return fn;
7458 /* 5.2.2/11: If a function call is a prvalue of object type: if the
7459 function call is either the operand of a decltype-specifier or the
7460 right operand of a comma operator that is the operand of a
7461 decltype-specifier, a temporary object is not introduced for the
7462 prvalue. The type of the prvalue may be incomplete. */
7463 if (!(complain & tf_decltype))
7465 fn = require_complete_type_sfinae (fn, complain);
7466 if (fn == error_mark_node)
7467 return error_mark_node;
7469 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
7470 fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
7472 return convert_from_reference (fn);
7475 static GTY(()) tree java_iface_lookup_fn;
7477 /* Make an expression which yields the address of the Java interface
7478 method FN. This is achieved by generating a call to libjava's
7479 _Jv_LookupInterfaceMethodIdx(). */
7481 static tree
7482 build_java_interface_fn_ref (tree fn, tree instance)
7484 tree lookup_fn, method, idx;
7485 tree klass_ref, iface, iface_ref;
7486 int i;
7488 if (!java_iface_lookup_fn)
7490 tree ftype = build_function_type_list (ptr_type_node,
7491 ptr_type_node, ptr_type_node,
7492 java_int_type_node, NULL_TREE);
7493 java_iface_lookup_fn
7494 = add_builtin_function ("_Jv_LookupInterfaceMethodIdx", ftype,
7495 0, NOT_BUILT_IN, NULL, NULL_TREE);
7498 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
7499 This is the first entry in the vtable. */
7500 klass_ref = build_vtbl_ref (cp_build_indirect_ref (instance, RO_NULL,
7501 tf_warning_or_error),
7502 integer_zero_node);
7504 /* Get the java.lang.Class pointer for the interface being called. */
7505 iface = DECL_CONTEXT (fn);
7506 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
7507 if (!iface_ref || !VAR_P (iface_ref)
7508 || DECL_CONTEXT (iface_ref) != iface)
7510 error ("could not find class$ field in java interface type %qT",
7511 iface);
7512 return error_mark_node;
7514 iface_ref = build_address (iface_ref);
7515 iface_ref = convert (build_pointer_type (iface), iface_ref);
7517 /* Determine the itable index of FN. */
7518 i = 1;
7519 for (method = TYPE_METHODS (iface); method; method = DECL_CHAIN (method))
7521 if (!DECL_VIRTUAL_P (method))
7522 continue;
7523 if (fn == method)
7524 break;
7525 i++;
7527 idx = build_int_cst (NULL_TREE, i);
7529 lookup_fn = build1 (ADDR_EXPR,
7530 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
7531 java_iface_lookup_fn);
7532 return build_call_nary (ptr_type_node, lookup_fn,
7533 3, klass_ref, iface_ref, idx);
7536 /* Returns the value to use for the in-charge parameter when making a
7537 call to a function with the indicated NAME.
7539 FIXME:Can't we find a neater way to do this mapping? */
7541 tree
7542 in_charge_arg_for_name (tree name)
7544 if (name == base_ctor_identifier
7545 || name == base_dtor_identifier)
7546 return integer_zero_node;
7547 else if (name == complete_ctor_identifier)
7548 return integer_one_node;
7549 else if (name == complete_dtor_identifier)
7550 return integer_two_node;
7551 else if (name == deleting_dtor_identifier)
7552 return integer_three_node;
7554 /* This function should only be called with one of the names listed
7555 above. */
7556 gcc_unreachable ();
7557 return NULL_TREE;
7560 /* Build a call to a constructor, destructor, or an assignment
7561 operator for INSTANCE, an expression with class type. NAME
7562 indicates the special member function to call; *ARGS are the
7563 arguments. ARGS may be NULL. This may change ARGS. BINFO
7564 indicates the base of INSTANCE that is to be passed as the `this'
7565 parameter to the member function called.
7567 FLAGS are the LOOKUP_* flags to use when processing the call.
7569 If NAME indicates a complete object constructor, INSTANCE may be
7570 NULL_TREE. In this case, the caller will call build_cplus_new to
7571 store the newly constructed object into a VAR_DECL. */
7573 tree
7574 build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
7575 tree binfo, int flags, tsubst_flags_t complain)
7577 tree fns;
7578 /* The type of the subobject to be constructed or destroyed. */
7579 tree class_type;
7580 vec<tree, va_gc> *allocated = NULL;
7581 tree ret;
7583 gcc_assert (name == complete_ctor_identifier
7584 || name == base_ctor_identifier
7585 || name == complete_dtor_identifier
7586 || name == base_dtor_identifier
7587 || name == deleting_dtor_identifier
7588 || name == ansi_assopname (NOP_EXPR));
7589 if (TYPE_P (binfo))
7591 /* Resolve the name. */
7592 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
7593 return error_mark_node;
7595 binfo = TYPE_BINFO (binfo);
7598 gcc_assert (binfo != NULL_TREE);
7600 class_type = BINFO_TYPE (binfo);
7602 /* Handle the special case where INSTANCE is NULL_TREE. */
7603 if (name == complete_ctor_identifier && !instance)
7604 instance = build_dummy_object (class_type);
7605 else
7607 if (name == complete_dtor_identifier
7608 || name == base_dtor_identifier
7609 || name == deleting_dtor_identifier)
7610 gcc_assert (args == NULL || vec_safe_is_empty (*args));
7612 /* Convert to the base class, if necessary. */
7613 if (!same_type_ignoring_top_level_qualifiers_p
7614 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
7616 if (name != ansi_assopname (NOP_EXPR))
7617 /* For constructors and destructors, either the base is
7618 non-virtual, or it is virtual but we are doing the
7619 conversion from a constructor or destructor for the
7620 complete object. In either case, we can convert
7621 statically. */
7622 instance = convert_to_base_statically (instance, binfo);
7623 else
7624 /* However, for assignment operators, we must convert
7625 dynamically if the base is virtual. */
7626 instance = build_base_path (PLUS_EXPR, instance,
7627 binfo, /*nonnull=*/1, complain);
7631 gcc_assert (instance != NULL_TREE);
7633 fns = lookup_fnfields (binfo, name, 1);
7635 /* When making a call to a constructor or destructor for a subobject
7636 that uses virtual base classes, pass down a pointer to a VTT for
7637 the subobject. */
7638 if ((name == base_ctor_identifier
7639 || name == base_dtor_identifier)
7640 && CLASSTYPE_VBASECLASSES (class_type))
7642 tree vtt;
7643 tree sub_vtt;
7645 /* If the current function is a complete object constructor
7646 or destructor, then we fetch the VTT directly.
7647 Otherwise, we look it up using the VTT we were given. */
7648 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
7649 vtt = decay_conversion (vtt, complain);
7650 if (vtt == error_mark_node)
7651 return error_mark_node;
7652 vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
7653 build2 (EQ_EXPR, boolean_type_node,
7654 current_in_charge_parm, integer_zero_node),
7655 current_vtt_parm,
7656 vtt);
7657 if (BINFO_SUBVTT_INDEX (binfo))
7658 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
7659 else
7660 sub_vtt = vtt;
7662 if (args == NULL)
7664 allocated = make_tree_vector ();
7665 args = &allocated;
7668 vec_safe_insert (*args, 0, sub_vtt);
7671 ret = build_new_method_call (instance, fns, args,
7672 TYPE_BINFO (BINFO_TYPE (binfo)),
7673 flags, /*fn=*/NULL,
7674 complain);
7676 if (allocated != NULL)
7677 release_tree_vector (allocated);
7679 if ((complain & tf_error)
7680 && (flags & LOOKUP_DELEGATING_CONS)
7681 && name == complete_ctor_identifier
7682 && TREE_CODE (ret) == CALL_EXPR
7683 && (DECL_ABSTRACT_ORIGIN (TREE_OPERAND (CALL_EXPR_FN (ret), 0))
7684 == current_function_decl))
7685 error ("constructor delegates to itself");
7687 return ret;
7690 /* Return the NAME, as a C string. The NAME indicates a function that
7691 is a member of TYPE. *FREE_P is set to true if the caller must
7692 free the memory returned.
7694 Rather than go through all of this, we should simply set the names
7695 of constructors and destructors appropriately, and dispense with
7696 ctor_identifier, dtor_identifier, etc. */
7698 static char *
7699 name_as_c_string (tree name, tree type, bool *free_p)
7701 char *pretty_name;
7703 /* Assume that we will not allocate memory. */
7704 *free_p = false;
7705 /* Constructors and destructors are special. */
7706 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
7708 pretty_name
7709 = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type))));
7710 /* For a destructor, add the '~'. */
7711 if (name == complete_dtor_identifier
7712 || name == base_dtor_identifier
7713 || name == deleting_dtor_identifier)
7715 pretty_name = concat ("~", pretty_name, NULL);
7716 /* Remember that we need to free the memory allocated. */
7717 *free_p = true;
7720 else if (IDENTIFIER_TYPENAME_P (name))
7722 pretty_name = concat ("operator ",
7723 type_as_string_translate (TREE_TYPE (name),
7724 TFF_PLAIN_IDENTIFIER),
7725 NULL);
7726 /* Remember that we need to free the memory allocated. */
7727 *free_p = true;
7729 else
7730 pretty_name = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (name)));
7732 return pretty_name;
7735 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
7736 be set, upon return, to the function called. ARGS may be NULL.
7737 This may change ARGS. */
7739 static tree
7740 build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
7741 tree conversion_path, int flags,
7742 tree *fn_p, tsubst_flags_t complain)
7744 struct z_candidate *candidates = 0, *cand;
7745 tree explicit_targs = NULL_TREE;
7746 tree basetype = NULL_TREE;
7747 tree access_binfo, binfo;
7748 tree optype;
7749 tree first_mem_arg = NULL_TREE;
7750 tree name;
7751 bool skip_first_for_error;
7752 vec<tree, va_gc> *user_args;
7753 tree call;
7754 tree fn;
7755 int template_only = 0;
7756 bool any_viable_p;
7757 tree orig_instance;
7758 tree orig_fns;
7759 vec<tree, va_gc> *orig_args = NULL;
7760 void *p;
7762 gcc_assert (instance != NULL_TREE);
7764 /* We don't know what function we're going to call, yet. */
7765 if (fn_p)
7766 *fn_p = NULL_TREE;
7768 if (error_operand_p (instance)
7769 || !fns || error_operand_p (fns))
7770 return error_mark_node;
7772 if (!BASELINK_P (fns))
7774 if (complain & tf_error)
7775 error ("call to non-function %qD", fns);
7776 return error_mark_node;
7779 orig_instance = instance;
7780 orig_fns = fns;
7782 /* Dismantle the baselink to collect all the information we need. */
7783 if (!conversion_path)
7784 conversion_path = BASELINK_BINFO (fns);
7785 access_binfo = BASELINK_ACCESS_BINFO (fns);
7786 binfo = BASELINK_BINFO (fns);
7787 optype = BASELINK_OPTYPE (fns);
7788 fns = BASELINK_FUNCTIONS (fns);
7789 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7791 explicit_targs = TREE_OPERAND (fns, 1);
7792 fns = TREE_OPERAND (fns, 0);
7793 template_only = 1;
7795 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
7796 || TREE_CODE (fns) == TEMPLATE_DECL
7797 || TREE_CODE (fns) == OVERLOAD);
7798 fn = get_first_fn (fns);
7799 name = DECL_NAME (fn);
7801 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
7802 gcc_assert (CLASS_TYPE_P (basetype));
7804 if (processing_template_decl)
7806 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
7807 instance = build_non_dependent_expr (instance);
7808 if (args != NULL)
7809 make_args_non_dependent (*args);
7812 user_args = args == NULL ? NULL : *args;
7813 /* Under DR 147 A::A() is an invalid constructor call,
7814 not a functional cast. */
7815 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
7817 if (! (complain & tf_error))
7818 return error_mark_node;
7820 if (permerror (input_location,
7821 "cannot call constructor %<%T::%D%> directly",
7822 basetype, name))
7823 inform (input_location, "for a function-style cast, remove the "
7824 "redundant %<::%D%>", name);
7825 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
7826 complain);
7827 return call;
7830 /* Figure out whether to skip the first argument for the error
7831 message we will display to users if an error occurs. We don't
7832 want to display any compiler-generated arguments. The "this"
7833 pointer hasn't been added yet. However, we must remove the VTT
7834 pointer if this is a call to a base-class constructor or
7835 destructor. */
7836 skip_first_for_error = false;
7837 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
7839 /* Callers should explicitly indicate whether they want to construct
7840 the complete object or just the part without virtual bases. */
7841 gcc_assert (name != ctor_identifier);
7842 /* Similarly for destructors. */
7843 gcc_assert (name != dtor_identifier);
7844 /* Remove the VTT pointer, if present. */
7845 if ((name == base_ctor_identifier || name == base_dtor_identifier)
7846 && CLASSTYPE_VBASECLASSES (basetype))
7847 skip_first_for_error = true;
7850 /* Process the argument list. */
7851 if (args != NULL && *args != NULL)
7853 *args = resolve_args (*args, complain);
7854 if (*args == NULL)
7855 return error_mark_node;
7858 /* Consider the object argument to be used even if we end up selecting a
7859 static member function. */
7860 instance = mark_type_use (instance);
7862 /* It's OK to call destructors and constructors on cv-qualified objects.
7863 Therefore, convert the INSTANCE to the unqualified type, if
7864 necessary. */
7865 if (DECL_DESTRUCTOR_P (fn)
7866 || DECL_CONSTRUCTOR_P (fn))
7868 if (!same_type_p (basetype, TREE_TYPE (instance)))
7870 instance = build_this (instance);
7871 instance = build_nop (build_pointer_type (basetype), instance);
7872 instance = build_fold_indirect_ref (instance);
7875 if (DECL_DESTRUCTOR_P (fn))
7876 name = complete_dtor_identifier;
7878 /* For the overload resolution we need to find the actual `this`
7879 that would be captured if the call turns out to be to a
7880 non-static member function. Do not actually capture it at this
7881 point. */
7882 first_mem_arg = maybe_resolve_dummy (instance, false);
7884 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7885 p = conversion_obstack_alloc (0);
7887 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
7888 initializer, not T({ }). */
7889 if (DECL_CONSTRUCTOR_P (fn) && args != NULL && !vec_safe_is_empty (*args)
7890 && DIRECT_LIST_INIT_P ((**args)[0]))
7892 tree init_list = (**args)[0];
7893 tree init = NULL_TREE;
7895 gcc_assert ((*args)->length () == 1
7896 && !(flags & LOOKUP_ONLYCONVERTING));
7898 /* If the initializer list has no elements and T is a class type with
7899 a default constructor, the object is value-initialized. Handle
7900 this here so we don't need to handle it wherever we use
7901 build_special_member_call. */
7902 if (CONSTRUCTOR_NELTS (init_list) == 0
7903 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
7904 /* For a user-provided default constructor, use the normal
7905 mechanisms so that protected access works. */
7906 && !type_has_user_provided_default_constructor (basetype)
7907 && !processing_template_decl)
7908 init = build_value_init (basetype, complain);
7910 /* If BASETYPE is an aggregate, we need to do aggregate
7911 initialization. */
7912 else if (CP_AGGREGATE_TYPE_P (basetype))
7913 init = digest_init (basetype, init_list, complain);
7915 if (init)
7917 if (is_dummy_object (instance))
7918 return get_target_expr_sfinae (init, complain);
7919 init = build2 (INIT_EXPR, TREE_TYPE (instance), instance, init);
7920 TREE_SIDE_EFFECTS (init) = true;
7921 return init;
7924 /* Otherwise go ahead with overload resolution. */
7925 add_list_candidates (fns, first_mem_arg, init_list,
7926 basetype, explicit_targs, template_only,
7927 conversion_path, access_binfo, flags,
7928 &candidates, complain);
7930 else
7932 add_candidates (fns, first_mem_arg, user_args, optype,
7933 explicit_targs, template_only, conversion_path,
7934 access_binfo, flags, &candidates, complain);
7936 any_viable_p = false;
7937 candidates = splice_viable (candidates, false, &any_viable_p);
7939 if (!any_viable_p)
7941 if (complain & tf_error)
7943 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
7944 cxx_incomplete_type_error (instance, basetype);
7945 else if (optype)
7946 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
7947 basetype, optype, build_tree_list_vec (user_args),
7948 TREE_TYPE (instance));
7949 else
7951 char *pretty_name;
7952 bool free_p;
7953 tree arglist;
7955 pretty_name = name_as_c_string (name, basetype, &free_p);
7956 arglist = build_tree_list_vec (user_args);
7957 if (skip_first_for_error)
7958 arglist = TREE_CHAIN (arglist);
7959 error ("no matching function for call to %<%T::%s(%A)%#V%>",
7960 basetype, pretty_name, arglist,
7961 TREE_TYPE (instance));
7962 if (free_p)
7963 free (pretty_name);
7965 print_z_candidates (location_of (name), candidates);
7967 call = error_mark_node;
7969 else
7971 cand = tourney (candidates, complain);
7972 if (cand == 0)
7974 char *pretty_name;
7975 bool free_p;
7976 tree arglist;
7978 if (complain & tf_error)
7980 pretty_name = name_as_c_string (name, basetype, &free_p);
7981 arglist = build_tree_list_vec (user_args);
7982 if (skip_first_for_error)
7983 arglist = TREE_CHAIN (arglist);
7984 if (!any_strictly_viable (candidates))
7985 error ("no matching function for call to %<%s(%A)%>",
7986 pretty_name, arglist);
7987 else
7988 error ("call of overloaded %<%s(%A)%> is ambiguous",
7989 pretty_name, arglist);
7990 print_z_candidates (location_of (name), candidates);
7991 if (free_p)
7992 free (pretty_name);
7994 call = error_mark_node;
7996 else
7998 fn = cand->fn;
7999 call = NULL_TREE;
8001 if (!(flags & LOOKUP_NONVIRTUAL)
8002 && DECL_PURE_VIRTUAL_P (fn)
8003 && instance == current_class_ref
8004 && (complain & tf_warning))
8006 /* This is not an error, it is runtime undefined
8007 behavior. */
8008 if (!current_function_decl)
8009 warning (0, "pure virtual %q#D called from "
8010 "non-static data member initializer", fn);
8011 else if (DECL_CONSTRUCTOR_P (current_function_decl)
8012 || DECL_DESTRUCTOR_P (current_function_decl))
8013 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
8014 ? "pure virtual %q#D called from constructor"
8015 : "pure virtual %q#D called from destructor"),
8016 fn);
8019 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
8020 && !DECL_CONSTRUCTOR_P (fn)
8021 && is_dummy_object (instance))
8023 instance = maybe_resolve_dummy (instance, true);
8024 if (instance == error_mark_node)
8025 call = error_mark_node;
8026 else if (!is_dummy_object (instance))
8028 /* We captured 'this' in the current lambda now that
8029 we know we really need it. */
8030 cand->first_arg = instance;
8032 else
8034 if (complain & tf_error)
8035 error ("cannot call member function %qD without object",
8036 fn);
8037 call = error_mark_node;
8041 if (call != error_mark_node)
8043 /* Optimize away vtable lookup if we know that this
8044 function can't be overridden. We need to check if
8045 the context and the type where we found fn are the same,
8046 actually FN might be defined in a different class
8047 type because of a using-declaration. In this case, we
8048 do not want to perform a non-virtual call. */
8049 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
8050 && same_type_ignoring_top_level_qualifiers_p
8051 (DECL_CONTEXT (fn), BINFO_TYPE (binfo))
8052 && resolves_to_fixed_type_p (instance, 0))
8053 flags |= LOOKUP_NONVIRTUAL;
8054 if (explicit_targs)
8055 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
8056 /* Now we know what function is being called. */
8057 if (fn_p)
8058 *fn_p = fn;
8059 /* Build the actual CALL_EXPR. */
8060 call = build_over_call (cand, flags, complain);
8061 /* In an expression of the form `a->f()' where `f' turns
8062 out to be a static member function, `a' is
8063 none-the-less evaluated. */
8064 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
8065 && !is_dummy_object (instance)
8066 && TREE_SIDE_EFFECTS (instance))
8067 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
8068 instance, call);
8069 else if (call != error_mark_node
8070 && DECL_DESTRUCTOR_P (cand->fn)
8071 && !VOID_TYPE_P (TREE_TYPE (call)))
8072 /* An explicit call of the form "x->~X()" has type
8073 "void". However, on platforms where destructors
8074 return "this" (i.e., those where
8075 targetm.cxx.cdtor_returns_this is true), such calls
8076 will appear to have a return value of pointer type
8077 to the low-level call machinery. We do not want to
8078 change the low-level machinery, since we want to be
8079 able to optimize "delete f()" on such platforms as
8080 "operator delete(~X(f()))" (rather than generating
8081 "t = f(), ~X(t), operator delete (t)"). */
8082 call = build_nop (void_type_node, call);
8087 if (processing_template_decl && call != error_mark_node)
8089 bool cast_to_void = false;
8091 if (TREE_CODE (call) == COMPOUND_EXPR)
8092 call = TREE_OPERAND (call, 1);
8093 else if (TREE_CODE (call) == NOP_EXPR)
8095 cast_to_void = true;
8096 call = TREE_OPERAND (call, 0);
8098 if (INDIRECT_REF_P (call))
8099 call = TREE_OPERAND (call, 0);
8100 call = (build_min_non_dep_call_vec
8101 (call,
8102 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
8103 orig_instance, orig_fns, NULL_TREE),
8104 orig_args));
8105 SET_EXPR_LOCATION (call, input_location);
8106 call = convert_from_reference (call);
8107 if (cast_to_void)
8108 call = build_nop (void_type_node, call);
8111 /* Free all the conversions we allocated. */
8112 obstack_free (&conversion_obstack, p);
8114 if (orig_args != NULL)
8115 release_tree_vector (orig_args);
8117 return call;
8120 /* Wrapper for above. */
8122 tree
8123 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
8124 tree conversion_path, int flags,
8125 tree *fn_p, tsubst_flags_t complain)
8127 tree ret;
8128 bool subtime = timevar_cond_start (TV_OVERLOAD);
8129 ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
8130 fn_p, complain);
8131 timevar_cond_stop (TV_OVERLOAD, subtime);
8132 return ret;
8135 /* Returns true iff standard conversion sequence ICS1 is a proper
8136 subsequence of ICS2. */
8138 static bool
8139 is_subseq (conversion *ics1, conversion *ics2)
8141 /* We can assume that a conversion of the same code
8142 between the same types indicates a subsequence since we only get
8143 here if the types we are converting from are the same. */
8145 while (ics1->kind == ck_rvalue
8146 || ics1->kind == ck_lvalue)
8147 ics1 = next_conversion (ics1);
8149 while (1)
8151 while (ics2->kind == ck_rvalue
8152 || ics2->kind == ck_lvalue)
8153 ics2 = next_conversion (ics2);
8155 if (ics2->kind == ck_user
8156 || ics2->kind == ck_ambig
8157 || ics2->kind == ck_aggr
8158 || ics2->kind == ck_list
8159 || ics2->kind == ck_identity)
8160 /* At this point, ICS1 cannot be a proper subsequence of
8161 ICS2. We can get a USER_CONV when we are comparing the
8162 second standard conversion sequence of two user conversion
8163 sequences. */
8164 return false;
8166 ics2 = next_conversion (ics2);
8168 if (ics2->kind == ics1->kind
8169 && same_type_p (ics2->type, ics1->type)
8170 && same_type_p (next_conversion (ics2)->type,
8171 next_conversion (ics1)->type))
8172 return true;
8176 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
8177 be any _TYPE nodes. */
8179 bool
8180 is_properly_derived_from (tree derived, tree base)
8182 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
8183 return false;
8185 /* We only allow proper derivation here. The DERIVED_FROM_P macro
8186 considers every class derived from itself. */
8187 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
8188 && DERIVED_FROM_P (base, derived));
8191 /* We build the ICS for an implicit object parameter as a pointer
8192 conversion sequence. However, such a sequence should be compared
8193 as if it were a reference conversion sequence. If ICS is the
8194 implicit conversion sequence for an implicit object parameter,
8195 modify it accordingly. */
8197 static void
8198 maybe_handle_implicit_object (conversion **ics)
8200 if ((*ics)->this_p)
8202 /* [over.match.funcs]
8204 For non-static member functions, the type of the
8205 implicit object parameter is "reference to cv X"
8206 where X is the class of which the function is a
8207 member and cv is the cv-qualification on the member
8208 function declaration. */
8209 conversion *t = *ics;
8210 tree reference_type;
8212 /* The `this' parameter is a pointer to a class type. Make the
8213 implicit conversion talk about a reference to that same class
8214 type. */
8215 reference_type = TREE_TYPE (t->type);
8216 reference_type = build_reference_type (reference_type);
8218 if (t->kind == ck_qual)
8219 t = next_conversion (t);
8220 if (t->kind == ck_ptr)
8221 t = next_conversion (t);
8222 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
8223 t = direct_reference_binding (reference_type, t);
8224 t->this_p = 1;
8225 t->rvaluedness_matches_p = 0;
8226 *ics = t;
8230 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
8231 and return the initial reference binding conversion. Otherwise,
8232 leave *ICS unchanged and return NULL. */
8234 static conversion *
8235 maybe_handle_ref_bind (conversion **ics)
8237 if ((*ics)->kind == ck_ref_bind)
8239 conversion *old_ics = *ics;
8240 *ics = next_conversion (old_ics);
8241 (*ics)->user_conv_p = old_ics->user_conv_p;
8242 return old_ics;
8245 return NULL;
8248 /* Compare two implicit conversion sequences according to the rules set out in
8249 [over.ics.rank]. Return values:
8251 1: ics1 is better than ics2
8252 -1: ics2 is better than ics1
8253 0: ics1 and ics2 are indistinguishable */
8255 static int
8256 compare_ics (conversion *ics1, conversion *ics2)
8258 tree from_type1;
8259 tree from_type2;
8260 tree to_type1;
8261 tree to_type2;
8262 tree deref_from_type1 = NULL_TREE;
8263 tree deref_from_type2 = NULL_TREE;
8264 tree deref_to_type1 = NULL_TREE;
8265 tree deref_to_type2 = NULL_TREE;
8266 conversion_rank rank1, rank2;
8268 /* REF_BINDING is nonzero if the result of the conversion sequence
8269 is a reference type. In that case REF_CONV is the reference
8270 binding conversion. */
8271 conversion *ref_conv1;
8272 conversion *ref_conv2;
8274 /* Compare badness before stripping the reference conversion. */
8275 if (ics1->bad_p > ics2->bad_p)
8276 return -1;
8277 else if (ics1->bad_p < ics2->bad_p)
8278 return 1;
8280 /* Handle implicit object parameters. */
8281 maybe_handle_implicit_object (&ics1);
8282 maybe_handle_implicit_object (&ics2);
8284 /* Handle reference parameters. */
8285 ref_conv1 = maybe_handle_ref_bind (&ics1);
8286 ref_conv2 = maybe_handle_ref_bind (&ics2);
8288 /* List-initialization sequence L1 is a better conversion sequence than
8289 list-initialization sequence L2 if L1 converts to
8290 std::initializer_list<X> for some X and L2 does not. */
8291 if (ics1->kind == ck_list && ics2->kind != ck_list)
8292 return 1;
8293 if (ics2->kind == ck_list && ics1->kind != ck_list)
8294 return -1;
8296 /* [over.ics.rank]
8298 When comparing the basic forms of implicit conversion sequences (as
8299 defined in _over.best.ics_)
8301 --a standard conversion sequence (_over.ics.scs_) is a better
8302 conversion sequence than a user-defined conversion sequence
8303 or an ellipsis conversion sequence, and
8305 --a user-defined conversion sequence (_over.ics.user_) is a
8306 better conversion sequence than an ellipsis conversion sequence
8307 (_over.ics.ellipsis_). */
8308 /* Use BAD_CONVERSION_RANK because we already checked for a badness
8309 mismatch. If both ICS are bad, we try to make a decision based on
8310 what would have happened if they'd been good. This is not an
8311 extension, we'll still give an error when we build up the call; this
8312 just helps us give a more helpful error message. */
8313 rank1 = BAD_CONVERSION_RANK (ics1);
8314 rank2 = BAD_CONVERSION_RANK (ics2);
8316 if (rank1 > rank2)
8317 return -1;
8318 else if (rank1 < rank2)
8319 return 1;
8321 if (ics1->ellipsis_p)
8322 /* Both conversions are ellipsis conversions. */
8323 return 0;
8325 /* User-defined conversion sequence U1 is a better conversion sequence
8326 than another user-defined conversion sequence U2 if they contain the
8327 same user-defined conversion operator or constructor and if the sec-
8328 ond standard conversion sequence of U1 is better than the second
8329 standard conversion sequence of U2. */
8331 /* Handle list-conversion with the same code even though it isn't always
8332 ranked as a user-defined conversion and it doesn't have a second
8333 standard conversion sequence; it will still have the desired effect.
8334 Specifically, we need to do the reference binding comparison at the
8335 end of this function. */
8337 if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
8339 conversion *t1;
8340 conversion *t2;
8342 for (t1 = ics1; t1->kind != ck_user; t1 = next_conversion (t1))
8343 if (t1->kind == ck_ambig || t1->kind == ck_aggr
8344 || t1->kind == ck_list)
8345 break;
8346 for (t2 = ics2; t2->kind != ck_user; t2 = next_conversion (t2))
8347 if (t2->kind == ck_ambig || t2->kind == ck_aggr
8348 || t2->kind == ck_list)
8349 break;
8351 if (t1->kind != t2->kind)
8352 return 0;
8353 else if (t1->kind == ck_user)
8355 if (t1->cand->fn != t2->cand->fn)
8356 return 0;
8358 else
8360 /* For ambiguous or aggregate conversions, use the target type as
8361 a proxy for the conversion function. */
8362 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
8363 return 0;
8366 /* We can just fall through here, after setting up
8367 FROM_TYPE1 and FROM_TYPE2. */
8368 from_type1 = t1->type;
8369 from_type2 = t2->type;
8371 else
8373 conversion *t1;
8374 conversion *t2;
8376 /* We're dealing with two standard conversion sequences.
8378 [over.ics.rank]
8380 Standard conversion sequence S1 is a better conversion
8381 sequence than standard conversion sequence S2 if
8383 --S1 is a proper subsequence of S2 (comparing the conversion
8384 sequences in the canonical form defined by _over.ics.scs_,
8385 excluding any Lvalue Transformation; the identity
8386 conversion sequence is considered to be a subsequence of
8387 any non-identity conversion sequence */
8389 t1 = ics1;
8390 while (t1->kind != ck_identity)
8391 t1 = next_conversion (t1);
8392 from_type1 = t1->type;
8394 t2 = ics2;
8395 while (t2->kind != ck_identity)
8396 t2 = next_conversion (t2);
8397 from_type2 = t2->type;
8400 /* One sequence can only be a subsequence of the other if they start with
8401 the same type. They can start with different types when comparing the
8402 second standard conversion sequence in two user-defined conversion
8403 sequences. */
8404 if (same_type_p (from_type1, from_type2))
8406 if (is_subseq (ics1, ics2))
8407 return 1;
8408 if (is_subseq (ics2, ics1))
8409 return -1;
8412 /* [over.ics.rank]
8414 Or, if not that,
8416 --the rank of S1 is better than the rank of S2 (by the rules
8417 defined below):
8419 Standard conversion sequences are ordered by their ranks: an Exact
8420 Match is a better conversion than a Promotion, which is a better
8421 conversion than a Conversion.
8423 Two conversion sequences with the same rank are indistinguishable
8424 unless one of the following rules applies:
8426 --A conversion that does not a convert a pointer, pointer to member,
8427 or std::nullptr_t to bool is better than one that does.
8429 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
8430 so that we do not have to check it explicitly. */
8431 if (ics1->rank < ics2->rank)
8432 return 1;
8433 else if (ics2->rank < ics1->rank)
8434 return -1;
8436 to_type1 = ics1->type;
8437 to_type2 = ics2->type;
8439 /* A conversion from scalar arithmetic type to complex is worse than a
8440 conversion between scalar arithmetic types. */
8441 if (same_type_p (from_type1, from_type2)
8442 && ARITHMETIC_TYPE_P (from_type1)
8443 && ARITHMETIC_TYPE_P (to_type1)
8444 && ARITHMETIC_TYPE_P (to_type2)
8445 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
8446 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
8448 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
8449 return -1;
8450 else
8451 return 1;
8454 if (TYPE_PTR_P (from_type1)
8455 && TYPE_PTR_P (from_type2)
8456 && TYPE_PTR_P (to_type1)
8457 && TYPE_PTR_P (to_type2))
8459 deref_from_type1 = TREE_TYPE (from_type1);
8460 deref_from_type2 = TREE_TYPE (from_type2);
8461 deref_to_type1 = TREE_TYPE (to_type1);
8462 deref_to_type2 = TREE_TYPE (to_type2);
8464 /* The rules for pointers to members A::* are just like the rules
8465 for pointers A*, except opposite: if B is derived from A then
8466 A::* converts to B::*, not vice versa. For that reason, we
8467 switch the from_ and to_ variables here. */
8468 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
8469 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
8470 || (TYPE_PTRMEMFUNC_P (from_type1)
8471 && TYPE_PTRMEMFUNC_P (from_type2)
8472 && TYPE_PTRMEMFUNC_P (to_type1)
8473 && TYPE_PTRMEMFUNC_P (to_type2)))
8475 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
8476 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
8477 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
8478 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
8481 if (deref_from_type1 != NULL_TREE
8482 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
8483 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
8485 /* This was one of the pointer or pointer-like conversions.
8487 [over.ics.rank]
8489 --If class B is derived directly or indirectly from class A,
8490 conversion of B* to A* is better than conversion of B* to
8491 void*, and conversion of A* to void* is better than
8492 conversion of B* to void*. */
8493 if (VOID_TYPE_P (deref_to_type1)
8494 && VOID_TYPE_P (deref_to_type2))
8496 if (is_properly_derived_from (deref_from_type1,
8497 deref_from_type2))
8498 return -1;
8499 else if (is_properly_derived_from (deref_from_type2,
8500 deref_from_type1))
8501 return 1;
8503 else if (VOID_TYPE_P (deref_to_type1)
8504 || VOID_TYPE_P (deref_to_type2))
8506 if (same_type_p (deref_from_type1, deref_from_type2))
8508 if (VOID_TYPE_P (deref_to_type2))
8510 if (is_properly_derived_from (deref_from_type1,
8511 deref_to_type1))
8512 return 1;
8514 /* We know that DEREF_TO_TYPE1 is `void' here. */
8515 else if (is_properly_derived_from (deref_from_type1,
8516 deref_to_type2))
8517 return -1;
8520 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
8521 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
8523 /* [over.ics.rank]
8525 --If class B is derived directly or indirectly from class A
8526 and class C is derived directly or indirectly from B,
8528 --conversion of C* to B* is better than conversion of C* to
8531 --conversion of B* to A* is better than conversion of C* to
8532 A* */
8533 if (same_type_p (deref_from_type1, deref_from_type2))
8535 if (is_properly_derived_from (deref_to_type1,
8536 deref_to_type2))
8537 return 1;
8538 else if (is_properly_derived_from (deref_to_type2,
8539 deref_to_type1))
8540 return -1;
8542 else if (same_type_p (deref_to_type1, deref_to_type2))
8544 if (is_properly_derived_from (deref_from_type2,
8545 deref_from_type1))
8546 return 1;
8547 else if (is_properly_derived_from (deref_from_type1,
8548 deref_from_type2))
8549 return -1;
8553 else if (CLASS_TYPE_P (non_reference (from_type1))
8554 && same_type_p (from_type1, from_type2))
8556 tree from = non_reference (from_type1);
8558 /* [over.ics.rank]
8560 --binding of an expression of type C to a reference of type
8561 B& is better than binding an expression of type C to a
8562 reference of type A&
8564 --conversion of C to B is better than conversion of C to A, */
8565 if (is_properly_derived_from (from, to_type1)
8566 && is_properly_derived_from (from, to_type2))
8568 if (is_properly_derived_from (to_type1, to_type2))
8569 return 1;
8570 else if (is_properly_derived_from (to_type2, to_type1))
8571 return -1;
8574 else if (CLASS_TYPE_P (non_reference (to_type1))
8575 && same_type_p (to_type1, to_type2))
8577 tree to = non_reference (to_type1);
8579 /* [over.ics.rank]
8581 --binding of an expression of type B to a reference of type
8582 A& is better than binding an expression of type C to a
8583 reference of type A&,
8585 --conversion of B to A is better than conversion of C to A */
8586 if (is_properly_derived_from (from_type1, to)
8587 && is_properly_derived_from (from_type2, to))
8589 if (is_properly_derived_from (from_type2, from_type1))
8590 return 1;
8591 else if (is_properly_derived_from (from_type1, from_type2))
8592 return -1;
8596 /* [over.ics.rank]
8598 --S1 and S2 differ only in their qualification conversion and yield
8599 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
8600 qualification signature of type T1 is a proper subset of the cv-
8601 qualification signature of type T2 */
8602 if (ics1->kind == ck_qual
8603 && ics2->kind == ck_qual
8604 && same_type_p (from_type1, from_type2))
8606 int result = comp_cv_qual_signature (to_type1, to_type2);
8607 if (result != 0)
8608 return result;
8611 /* [over.ics.rank]
8613 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
8614 to an implicit object parameter of a non-static member function
8615 declared without a ref-qualifier, and either S1 binds an lvalue
8616 reference to an lvalue and S2 binds an rvalue reference or S1 binds an
8617 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
8618 draft standard, 13.3.3.2)
8620 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
8621 types to which the references refer are the same type except for
8622 top-level cv-qualifiers, and the type to which the reference
8623 initialized by S2 refers is more cv-qualified than the type to
8624 which the reference initialized by S1 refers.
8626 DR 1328 [over.match.best]: the context is an initialization by
8627 conversion function for direct reference binding (13.3.1.6) of a
8628 reference to function type, the return type of F1 is the same kind of
8629 reference (i.e. lvalue or rvalue) as the reference being initialized,
8630 and the return type of F2 is not. */
8632 if (ref_conv1 && ref_conv2)
8634 if (!ref_conv1->this_p && !ref_conv2->this_p
8635 && (ref_conv1->rvaluedness_matches_p
8636 != ref_conv2->rvaluedness_matches_p)
8637 && (same_type_p (ref_conv1->type, ref_conv2->type)
8638 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
8639 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
8641 if (ref_conv1->bad_p
8642 && !same_type_p (TREE_TYPE (ref_conv1->type),
8643 TREE_TYPE (ref_conv2->type)))
8644 /* Don't prefer a bad conversion that drops cv-quals to a bad
8645 conversion with the wrong rvalueness. */
8646 return 0;
8647 return (ref_conv1->rvaluedness_matches_p
8648 - ref_conv2->rvaluedness_matches_p);
8651 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
8653 int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
8654 int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
8655 if (ref_conv1->bad_p)
8657 /* Prefer the one that drops fewer cv-quals. */
8658 tree ftype = next_conversion (ref_conv1)->type;
8659 int fquals = cp_type_quals (ftype);
8660 q1 ^= fquals;
8661 q2 ^= fquals;
8663 return comp_cv_qualification (q2, q1);
8667 /* Neither conversion sequence is better than the other. */
8668 return 0;
8671 /* The source type for this standard conversion sequence. */
8673 static tree
8674 source_type (conversion *t)
8676 for (;; t = next_conversion (t))
8678 if (t->kind == ck_user
8679 || t->kind == ck_ambig
8680 || t->kind == ck_identity)
8681 return t->type;
8683 gcc_unreachable ();
8686 /* Note a warning about preferring WINNER to LOSER. We do this by storing
8687 a pointer to LOSER and re-running joust to produce the warning if WINNER
8688 is actually used. */
8690 static void
8691 add_warning (struct z_candidate *winner, struct z_candidate *loser)
8693 candidate_warning *cw = (candidate_warning *)
8694 conversion_obstack_alloc (sizeof (candidate_warning));
8695 cw->loser = loser;
8696 cw->next = winner->warnings;
8697 winner->warnings = cw;
8700 /* Compare two candidates for overloading as described in
8701 [over.match.best]. Return values:
8703 1: cand1 is better than cand2
8704 -1: cand2 is better than cand1
8705 0: cand1 and cand2 are indistinguishable */
8707 static int
8708 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
8709 tsubst_flags_t complain)
8711 int winner = 0;
8712 int off1 = 0, off2 = 0;
8713 size_t i;
8714 size_t len;
8716 /* Candidates that involve bad conversions are always worse than those
8717 that don't. */
8718 if (cand1->viable > cand2->viable)
8719 return 1;
8720 if (cand1->viable < cand2->viable)
8721 return -1;
8723 /* If we have two pseudo-candidates for conversions to the same type,
8724 or two candidates for the same function, arbitrarily pick one. */
8725 if (cand1->fn == cand2->fn
8726 && (IS_TYPE_OR_DECL_P (cand1->fn)))
8727 return 1;
8729 /* Prefer a non-deleted function over an implicitly deleted move
8730 constructor or assignment operator. This differs slightly from the
8731 wording for issue 1402 (which says the move op is ignored by overload
8732 resolution), but this way produces better error messages. */
8733 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
8734 && TREE_CODE (cand2->fn) == FUNCTION_DECL
8735 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
8737 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
8738 && move_fn_p (cand1->fn))
8739 return -1;
8740 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
8741 && move_fn_p (cand2->fn))
8742 return 1;
8745 /* a viable function F1
8746 is defined to be a better function than another viable function F2 if
8747 for all arguments i, ICSi(F1) is not a worse conversion sequence than
8748 ICSi(F2), and then */
8750 /* for some argument j, ICSj(F1) is a better conversion sequence than
8751 ICSj(F2) */
8753 /* For comparing static and non-static member functions, we ignore
8754 the implicit object parameter of the non-static function. The
8755 standard says to pretend that the static function has an object
8756 parm, but that won't work with operator overloading. */
8757 len = cand1->num_convs;
8758 if (len != cand2->num_convs)
8760 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
8761 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
8763 if (DECL_CONSTRUCTOR_P (cand1->fn)
8764 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
8765 /* We're comparing a near-match list constructor and a near-match
8766 non-list constructor. Just treat them as unordered. */
8767 return 0;
8769 gcc_assert (static_1 != static_2);
8771 if (static_1)
8772 off2 = 1;
8773 else
8775 off1 = 1;
8776 --len;
8780 for (i = 0; i < len; ++i)
8782 conversion *t1 = cand1->convs[i + off1];
8783 conversion *t2 = cand2->convs[i + off2];
8784 int comp = compare_ics (t1, t2);
8786 if (comp != 0)
8788 if ((complain & tf_warning)
8789 && warn_sign_promo
8790 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
8791 == cr_std + cr_promotion)
8792 && t1->kind == ck_std
8793 && t2->kind == ck_std
8794 && TREE_CODE (t1->type) == INTEGER_TYPE
8795 && TREE_CODE (t2->type) == INTEGER_TYPE
8796 && (TYPE_PRECISION (t1->type)
8797 == TYPE_PRECISION (t2->type))
8798 && (TYPE_UNSIGNED (next_conversion (t1)->type)
8799 || (TREE_CODE (next_conversion (t1)->type)
8800 == ENUMERAL_TYPE)))
8802 tree type = next_conversion (t1)->type;
8803 tree type1, type2;
8804 struct z_candidate *w, *l;
8805 if (comp > 0)
8806 type1 = t1->type, type2 = t2->type,
8807 w = cand1, l = cand2;
8808 else
8809 type1 = t2->type, type2 = t1->type,
8810 w = cand2, l = cand1;
8812 if (warn)
8814 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
8815 type, type1, type2);
8816 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
8818 else
8819 add_warning (w, l);
8822 if (winner && comp != winner)
8824 winner = 0;
8825 goto tweak;
8827 winner = comp;
8831 /* warn about confusing overload resolution for user-defined conversions,
8832 either between a constructor and a conversion op, or between two
8833 conversion ops. */
8834 if ((complain & tf_warning)
8835 && winner && warn_conversion && cand1->second_conv
8836 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
8837 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
8839 struct z_candidate *w, *l;
8840 bool give_warning = false;
8842 if (winner == 1)
8843 w = cand1, l = cand2;
8844 else
8845 w = cand2, l = cand1;
8847 /* We don't want to complain about `X::operator T1 ()'
8848 beating `X::operator T2 () const', when T2 is a no less
8849 cv-qualified version of T1. */
8850 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
8851 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
8853 tree t = TREE_TYPE (TREE_TYPE (l->fn));
8854 tree f = TREE_TYPE (TREE_TYPE (w->fn));
8856 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
8858 t = TREE_TYPE (t);
8859 f = TREE_TYPE (f);
8861 if (!comp_ptr_ttypes (t, f))
8862 give_warning = true;
8864 else
8865 give_warning = true;
8867 if (!give_warning)
8868 /*NOP*/;
8869 else if (warn)
8871 tree source = source_type (w->convs[0]);
8872 if (! DECL_CONSTRUCTOR_P (w->fn))
8873 source = TREE_TYPE (source);
8874 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
8875 && warning (OPT_Wconversion, " for conversion from %qT to %qT",
8876 source, w->second_conv->type))
8878 inform (input_location, " because conversion sequence for the argument is better");
8881 else
8882 add_warning (w, l);
8885 if (winner)
8886 return winner;
8888 /* DR 495 moved this tiebreaker above the template ones. */
8889 /* or, if not that,
8890 the context is an initialization by user-defined conversion (see
8891 _dcl.init_ and _over.match.user_) and the standard conversion
8892 sequence from the return type of F1 to the destination type (i.e.,
8893 the type of the entity being initialized) is a better conversion
8894 sequence than the standard conversion sequence from the return type
8895 of F2 to the destination type. */
8897 if (cand1->second_conv)
8899 winner = compare_ics (cand1->second_conv, cand2->second_conv);
8900 if (winner)
8901 return winner;
8904 /* or, if not that,
8905 F1 is a non-template function and F2 is a template function
8906 specialization. */
8908 if (!cand1->template_decl && cand2->template_decl)
8909 return 1;
8910 else if (cand1->template_decl && !cand2->template_decl)
8911 return -1;
8913 /* or, if not that,
8914 F1 and F2 are template functions and the function template for F1 is
8915 more specialized than the template for F2 according to the partial
8916 ordering rules. */
8918 if (cand1->template_decl && cand2->template_decl)
8920 winner = more_specialized_fn
8921 (TI_TEMPLATE (cand1->template_decl),
8922 TI_TEMPLATE (cand2->template_decl),
8923 /* [temp.func.order]: The presence of unused ellipsis and default
8924 arguments has no effect on the partial ordering of function
8925 templates. add_function_candidate() will not have
8926 counted the "this" argument for constructors. */
8927 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
8928 if (winner)
8929 return winner;
8932 /* Check whether we can discard a builtin candidate, either because we
8933 have two identical ones or matching builtin and non-builtin candidates.
8935 (Pedantically in the latter case the builtin which matched the user
8936 function should not be added to the overload set, but we spot it here.
8938 [over.match.oper]
8939 ... the builtin candidates include ...
8940 - do not have the same parameter type list as any non-template
8941 non-member candidate. */
8943 if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
8945 for (i = 0; i < len; ++i)
8946 if (!same_type_p (cand1->convs[i]->type,
8947 cand2->convs[i]->type))
8948 break;
8949 if (i == cand1->num_convs)
8951 if (cand1->fn == cand2->fn)
8952 /* Two built-in candidates; arbitrarily pick one. */
8953 return 1;
8954 else if (identifier_p (cand1->fn))
8955 /* cand1 is built-in; prefer cand2. */
8956 return -1;
8957 else
8958 /* cand2 is built-in; prefer cand1. */
8959 return 1;
8963 /* For candidates of a multi-versioned function, make the version with
8964 the highest priority win. This version will be checked for dispatching
8965 first. If this version can be inlined into the caller, the front-end
8966 will simply make a direct call to this function. */
8968 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
8969 && DECL_FUNCTION_VERSIONED (cand1->fn)
8970 && TREE_CODE (cand2->fn) == FUNCTION_DECL
8971 && DECL_FUNCTION_VERSIONED (cand2->fn))
8973 tree f1 = TREE_TYPE (cand1->fn);
8974 tree f2 = TREE_TYPE (cand2->fn);
8975 tree p1 = TYPE_ARG_TYPES (f1);
8976 tree p2 = TYPE_ARG_TYPES (f2);
8978 /* Check if cand1->fn and cand2->fn are versions of the same function. It
8979 is possible that cand1->fn and cand2->fn are function versions but of
8980 different functions. Check types to see if they are versions of the same
8981 function. */
8982 if (compparms (p1, p2)
8983 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8985 /* Always make the version with the higher priority, more
8986 specialized, win. */
8987 gcc_assert (targetm.compare_version_priority);
8988 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
8989 return 1;
8990 else
8991 return -1;
8995 /* If the two function declarations represent the same function (this can
8996 happen with declarations in multiple scopes and arg-dependent lookup),
8997 arbitrarily choose one. But first make sure the default args we're
8998 using match. */
8999 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
9000 && equal_functions (cand1->fn, cand2->fn))
9002 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
9003 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
9005 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
9007 for (i = 0; i < len; ++i)
9009 /* Don't crash if the fn is variadic. */
9010 if (!parms1)
9011 break;
9012 parms1 = TREE_CHAIN (parms1);
9013 parms2 = TREE_CHAIN (parms2);
9016 if (off1)
9017 parms1 = TREE_CHAIN (parms1);
9018 else if (off2)
9019 parms2 = TREE_CHAIN (parms2);
9021 for (; parms1; ++i)
9023 if (!cp_tree_equal (TREE_PURPOSE (parms1),
9024 TREE_PURPOSE (parms2)))
9026 if (warn)
9028 if (complain & tf_error)
9030 if (permerror (input_location,
9031 "default argument mismatch in "
9032 "overload resolution"))
9034 inform (input_location,
9035 " candidate 1: %q+#F", cand1->fn);
9036 inform (input_location,
9037 " candidate 2: %q+#F", cand2->fn);
9040 else
9041 return 0;
9043 else
9044 add_warning (cand1, cand2);
9045 break;
9047 parms1 = TREE_CHAIN (parms1);
9048 parms2 = TREE_CHAIN (parms2);
9051 return 1;
9054 tweak:
9056 /* Extension: If the worst conversion for one candidate is worse than the
9057 worst conversion for the other, take the first. */
9058 if (!pedantic && (complain & tf_warning_or_error))
9060 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
9061 struct z_candidate *w = 0, *l = 0;
9063 for (i = 0; i < len; ++i)
9065 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
9066 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
9067 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
9068 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
9070 if (rank1 < rank2)
9071 winner = 1, w = cand1, l = cand2;
9072 if (rank1 > rank2)
9073 winner = -1, w = cand2, l = cand1;
9074 if (winner)
9076 /* Don't choose a deleted function over ambiguity. */
9077 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
9078 return 0;
9079 if (warn)
9081 pedwarn (input_location, 0,
9082 "ISO C++ says that these are ambiguous, even "
9083 "though the worst conversion for the first is better than "
9084 "the worst conversion for the second:");
9085 print_z_candidate (input_location, _("candidate 1:"), w);
9086 print_z_candidate (input_location, _("candidate 2:"), l);
9088 else
9089 add_warning (w, l);
9090 return winner;
9094 gcc_assert (!winner);
9095 return 0;
9098 /* Given a list of candidates for overloading, find the best one, if any.
9099 This algorithm has a worst case of O(2n) (winner is last), and a best
9100 case of O(n/2) (totally ambiguous); much better than a sorting
9101 algorithm. */
9103 static struct z_candidate *
9104 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
9106 struct z_candidate *champ = candidates, *challenger;
9107 int fate;
9108 int champ_compared_to_predecessor = 0;
9110 /* Walk through the list once, comparing each current champ to the next
9111 candidate, knocking out a candidate or two with each comparison. */
9113 for (challenger = champ->next; challenger; )
9115 fate = joust (champ, challenger, 0, complain);
9116 if (fate == 1)
9117 challenger = challenger->next;
9118 else
9120 if (fate == 0)
9122 champ = challenger->next;
9123 if (champ == 0)
9124 return NULL;
9125 champ_compared_to_predecessor = 0;
9127 else
9129 champ = challenger;
9130 champ_compared_to_predecessor = 1;
9133 challenger = champ->next;
9137 /* Make sure the champ is better than all the candidates it hasn't yet
9138 been compared to. */
9140 for (challenger = candidates;
9141 challenger != champ
9142 && !(champ_compared_to_predecessor && challenger->next == champ);
9143 challenger = challenger->next)
9145 fate = joust (champ, challenger, 0, complain);
9146 if (fate != 1)
9147 return NULL;
9150 return champ;
9153 /* Returns nonzero if things of type FROM can be converted to TO. */
9155 bool
9156 can_convert (tree to, tree from, tsubst_flags_t complain)
9158 tree arg = NULL_TREE;
9159 /* implicit_conversion only considers user-defined conversions
9160 if it has an expression for the call argument list. */
9161 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
9162 arg = build1 (CAST_EXPR, from, NULL_TREE);
9163 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
9166 /* Returns nonzero if things of type FROM can be converted to TO with a
9167 standard conversion. */
9169 bool
9170 can_convert_standard (tree to, tree from, tsubst_flags_t complain)
9172 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
9175 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
9177 bool
9178 can_convert_arg (tree to, tree from, tree arg, int flags,
9179 tsubst_flags_t complain)
9181 conversion *t;
9182 void *p;
9183 bool ok_p;
9185 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9186 p = conversion_obstack_alloc (0);
9187 /* We want to discard any access checks done for this test,
9188 as we might not be in the appropriate access context and
9189 we'll do the check again when we actually perform the
9190 conversion. */
9191 push_deferring_access_checks (dk_deferred);
9193 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
9194 flags, complain);
9195 ok_p = (t && !t->bad_p);
9197 /* Discard the access checks now. */
9198 pop_deferring_access_checks ();
9199 /* Free all the conversions we allocated. */
9200 obstack_free (&conversion_obstack, p);
9202 return ok_p;
9205 /* Like can_convert_arg, but allows dubious conversions as well. */
9207 bool
9208 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
9209 tsubst_flags_t complain)
9211 conversion *t;
9212 void *p;
9214 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9215 p = conversion_obstack_alloc (0);
9216 /* Try to perform the conversion. */
9217 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
9218 flags, complain);
9219 /* Free all the conversions we allocated. */
9220 obstack_free (&conversion_obstack, p);
9222 return t != NULL;
9225 /* Convert EXPR to TYPE. Return the converted expression.
9227 Note that we allow bad conversions here because by the time we get to
9228 this point we are committed to doing the conversion. If we end up
9229 doing a bad conversion, convert_like will complain. */
9231 tree
9232 perform_implicit_conversion_flags (tree type, tree expr,
9233 tsubst_flags_t complain, int flags)
9235 conversion *conv;
9236 void *p;
9237 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
9239 if (error_operand_p (expr))
9240 return error_mark_node;
9242 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9243 p = conversion_obstack_alloc (0);
9245 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
9246 /*c_cast_p=*/false,
9247 flags, complain);
9249 if (!conv)
9251 if (complain & tf_error)
9253 /* If expr has unknown type, then it is an overloaded function.
9254 Call instantiate_type to get good error messages. */
9255 if (TREE_TYPE (expr) == unknown_type_node)
9256 instantiate_type (type, expr, complain);
9257 else if (invalid_nonstatic_memfn_p (expr, complain))
9258 /* We gave an error. */;
9259 else
9260 error_at (loc, "could not convert %qE from %qT to %qT", expr,
9261 TREE_TYPE (expr), type);
9263 expr = error_mark_node;
9265 else if (processing_template_decl && conv->kind != ck_identity)
9267 /* In a template, we are only concerned about determining the
9268 type of non-dependent expressions, so we do not have to
9269 perform the actual conversion. But for initializers, we
9270 need to be able to perform it at instantiation
9271 (or fold_non_dependent_expr) time. */
9272 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
9273 if (!(flags & LOOKUP_ONLYCONVERTING))
9274 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
9276 else
9277 expr = convert_like (conv, expr, complain);
9279 /* Free all the conversions we allocated. */
9280 obstack_free (&conversion_obstack, p);
9282 return expr;
9285 tree
9286 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
9288 return perform_implicit_conversion_flags (type, expr, complain,
9289 LOOKUP_IMPLICIT);
9292 /* Convert EXPR to TYPE (as a direct-initialization) if that is
9293 permitted. If the conversion is valid, the converted expression is
9294 returned. Otherwise, NULL_TREE is returned, except in the case
9295 that TYPE is a class type; in that case, an error is issued. If
9296 C_CAST_P is true, then this direct-initialization is taking
9297 place as part of a static_cast being attempted as part of a C-style
9298 cast. */
9300 tree
9301 perform_direct_initialization_if_possible (tree type,
9302 tree expr,
9303 bool c_cast_p,
9304 tsubst_flags_t complain)
9306 conversion *conv;
9307 void *p;
9309 if (type == error_mark_node || error_operand_p (expr))
9310 return error_mark_node;
9311 /* [dcl.init]
9313 If the destination type is a (possibly cv-qualified) class type:
9315 -- If the initialization is direct-initialization ...,
9316 constructors are considered. ... If no constructor applies, or
9317 the overload resolution is ambiguous, the initialization is
9318 ill-formed. */
9319 if (CLASS_TYPE_P (type))
9321 vec<tree, va_gc> *args = make_tree_vector_single (expr);
9322 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
9323 &args, type, LOOKUP_NORMAL, complain);
9324 release_tree_vector (args);
9325 return build_cplus_new (type, expr, complain);
9328 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9329 p = conversion_obstack_alloc (0);
9331 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
9332 c_cast_p,
9333 LOOKUP_NORMAL, complain);
9334 if (!conv || conv->bad_p)
9335 expr = NULL_TREE;
9336 else
9337 expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
9338 /*issue_conversion_warnings=*/false,
9339 c_cast_p,
9340 complain);
9342 /* Free all the conversions we allocated. */
9343 obstack_free (&conversion_obstack, p);
9345 return expr;
9348 /* When initializing a reference that lasts longer than a full-expression,
9349 this special rule applies:
9351 [class.temporary]
9353 The temporary to which the reference is bound or the temporary
9354 that is the complete object to which the reference is bound
9355 persists for the lifetime of the reference.
9357 The temporaries created during the evaluation of the expression
9358 initializing the reference, except the temporary to which the
9359 reference is bound, are destroyed at the end of the
9360 full-expression in which they are created.
9362 In that case, we store the converted expression into a new
9363 VAR_DECL in a new scope.
9365 However, we want to be careful not to create temporaries when
9366 they are not required. For example, given:
9368 struct B {};
9369 struct D : public B {};
9370 D f();
9371 const B& b = f();
9373 there is no need to copy the return value from "f"; we can just
9374 extend its lifetime. Similarly, given:
9376 struct S {};
9377 struct T { operator S(); };
9378 T t;
9379 const S& s = t;
9381 we can extend the lifetime of the return value of the conversion
9382 operator.
9384 The next several functions are involved in this lifetime extension. */
9386 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
9387 reference is being bound to a temporary. Create and return a new
9388 VAR_DECL with the indicated TYPE; this variable will store the value to
9389 which the reference is bound. */
9391 tree
9392 make_temporary_var_for_ref_to_temp (tree decl, tree type)
9394 tree var;
9396 /* Create the variable. */
9397 var = create_temporary_var (type);
9399 /* Register the variable. */
9400 if (VAR_P (decl)
9401 && (TREE_STATIC (decl) || DECL_THREAD_LOCAL_P (decl)))
9403 /* Namespace-scope or local static; give it a mangled name. */
9404 /* FIXME share comdat with decl? */
9405 tree name;
9407 TREE_STATIC (var) = TREE_STATIC (decl);
9408 set_decl_tls_model (var, DECL_TLS_MODEL (decl));
9409 name = mangle_ref_init_variable (decl);
9410 DECL_NAME (var) = name;
9411 SET_DECL_ASSEMBLER_NAME (var, name);
9412 var = pushdecl_top_level (var);
9414 else
9415 /* Create a new cleanup level if necessary. */
9416 maybe_push_cleanup_level (type);
9418 return var;
9421 /* EXPR is the initializer for a variable DECL of reference or
9422 std::initializer_list type. Create, push and return a new VAR_DECL
9423 for the initializer so that it will live as long as DECL. Any
9424 cleanup for the new variable is returned through CLEANUP, and the
9425 code to initialize the new variable is returned through INITP. */
9427 static tree
9428 set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
9429 tree *initp)
9431 tree init;
9432 tree type;
9433 tree var;
9435 /* Create the temporary variable. */
9436 type = TREE_TYPE (expr);
9437 var = make_temporary_var_for_ref_to_temp (decl, type);
9438 layout_decl (var, 0);
9439 /* If the rvalue is the result of a function call it will be
9440 a TARGET_EXPR. If it is some other construct (such as a
9441 member access expression where the underlying object is
9442 itself the result of a function call), turn it into a
9443 TARGET_EXPR here. It is important that EXPR be a
9444 TARGET_EXPR below since otherwise the INIT_EXPR will
9445 attempt to make a bitwise copy of EXPR to initialize
9446 VAR. */
9447 if (TREE_CODE (expr) != TARGET_EXPR)
9448 expr = get_target_expr (expr);
9450 if (TREE_CODE (decl) == FIELD_DECL
9451 && extra_warnings && !TREE_NO_WARNING (decl))
9453 warning (OPT_Wextra, "a temporary bound to %qD only persists "
9454 "until the constructor exits", decl);
9455 TREE_NO_WARNING (decl) = true;
9458 /* Recursively extend temps in this initializer. */
9459 TARGET_EXPR_INITIAL (expr)
9460 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups);
9462 /* Any reference temp has a non-trivial initializer. */
9463 DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
9465 /* If the initializer is constant, put it in DECL_INITIAL so we get
9466 static initialization and use in constant expressions. */
9467 init = maybe_constant_init (expr);
9468 if (TREE_CONSTANT (init))
9470 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
9472 /* 5.19 says that a constant expression can include an
9473 lvalue-rvalue conversion applied to "a glvalue of literal type
9474 that refers to a non-volatile temporary object initialized
9475 with a constant expression". Rather than try to communicate
9476 that this VAR_DECL is a temporary, just mark it constexpr.
9478 Currently this is only useful for initializer_list temporaries,
9479 since reference vars can't appear in constant expressions. */
9480 DECL_DECLARED_CONSTEXPR_P (var) = true;
9481 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
9482 TREE_CONSTANT (var) = true;
9484 DECL_INITIAL (var) = init;
9485 init = NULL_TREE;
9487 else
9488 /* Create the INIT_EXPR that will initialize the temporary
9489 variable. */
9490 init = build2 (INIT_EXPR, type, var, expr);
9491 if (at_function_scope_p ())
9493 add_decl_expr (var);
9495 if (TREE_STATIC (var))
9496 init = add_stmt_to_compound (init, register_dtor_fn (var));
9497 else
9499 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
9500 if (cleanup)
9501 vec_safe_push (*cleanups, cleanup);
9504 /* We must be careful to destroy the temporary only
9505 after its initialization has taken place. If the
9506 initialization throws an exception, then the
9507 destructor should not be run. We cannot simply
9508 transform INIT into something like:
9510 (INIT, ({ CLEANUP_STMT; }))
9512 because emit_local_var always treats the
9513 initializer as a full-expression. Thus, the
9514 destructor would run too early; it would run at the
9515 end of initializing the reference variable, rather
9516 than at the end of the block enclosing the
9517 reference variable.
9519 The solution is to pass back a cleanup expression
9520 which the caller is responsible for attaching to
9521 the statement tree. */
9523 else
9525 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
9526 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9528 if (DECL_THREAD_LOCAL_P (var))
9529 tls_aggregates = tree_cons (NULL_TREE, var,
9530 tls_aggregates);
9531 else
9532 static_aggregates = tree_cons (NULL_TREE, var,
9533 static_aggregates);
9535 else
9536 /* Check whether the dtor is callable. */
9537 cxx_maybe_build_cleanup (var, tf_warning_or_error);
9540 *initp = init;
9541 return var;
9544 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
9545 initializing a variable of that TYPE. */
9547 tree
9548 initialize_reference (tree type, tree expr,
9549 int flags, tsubst_flags_t complain)
9551 conversion *conv;
9552 void *p;
9553 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
9555 if (type == error_mark_node || error_operand_p (expr))
9556 return error_mark_node;
9558 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9559 p = conversion_obstack_alloc (0);
9561 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
9562 flags, complain);
9563 if (!conv || conv->bad_p)
9565 if (complain & tf_error)
9567 if (conv)
9568 convert_like (conv, expr, complain);
9569 else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
9570 && !TYPE_REF_IS_RVALUE (type)
9571 && !real_lvalue_p (expr))
9572 error_at (loc, "invalid initialization of non-const reference of "
9573 "type %qT from an rvalue of type %qT",
9574 type, TREE_TYPE (expr));
9575 else
9576 error_at (loc, "invalid initialization of reference of type "
9577 "%qT from expression of type %qT", type,
9578 TREE_TYPE (expr));
9580 return error_mark_node;
9583 if (conv->kind == ck_ref_bind)
9584 /* Perform the conversion. */
9585 expr = convert_like (conv, expr, complain);
9586 else if (conv->kind == ck_ambig)
9587 /* We gave an error in build_user_type_conversion_1. */
9588 expr = error_mark_node;
9589 else
9590 gcc_unreachable ();
9592 /* Free all the conversions we allocated. */
9593 obstack_free (&conversion_obstack, p);
9595 return expr;
9598 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
9599 which is bound either to a reference or a std::initializer_list. */
9601 static tree
9602 extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups)
9604 tree sub = init;
9605 tree *p;
9606 STRIP_NOPS (sub);
9607 if (TREE_CODE (sub) == COMPOUND_EXPR)
9609 TREE_OPERAND (sub, 1)
9610 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups);
9611 return init;
9613 if (TREE_CODE (sub) != ADDR_EXPR)
9614 return init;
9615 /* Deal with binding to a subobject. */
9616 for (p = &TREE_OPERAND (sub, 0); TREE_CODE (*p) == COMPONENT_REF; )
9617 p = &TREE_OPERAND (*p, 0);
9618 if (TREE_CODE (*p) == TARGET_EXPR)
9620 tree subinit = NULL_TREE;
9621 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit);
9622 if (subinit)
9623 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
9624 recompute_tree_invariant_for_addr_expr (sub);
9626 return init;
9629 /* INIT is part of the initializer for DECL. If there are any
9630 reference or initializer lists being initialized, extend their
9631 lifetime to match that of DECL. */
9633 tree
9634 extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
9636 tree type = TREE_TYPE (init);
9637 if (processing_template_decl)
9638 return init;
9639 if (TREE_CODE (type) == REFERENCE_TYPE)
9640 init = extend_ref_init_temps_1 (decl, init, cleanups);
9641 else if (is_std_init_list (type))
9643 /* The temporary array underlying a std::initializer_list
9644 is handled like a reference temporary. */
9645 tree ctor = init;
9646 if (TREE_CODE (ctor) == TARGET_EXPR)
9647 ctor = TARGET_EXPR_INITIAL (ctor);
9648 if (TREE_CODE (ctor) == CONSTRUCTOR)
9650 tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
9651 array = extend_ref_init_temps_1 (decl, array, cleanups);
9652 CONSTRUCTOR_ELT (ctor, 0)->value = array;
9655 else if (TREE_CODE (init) == CONSTRUCTOR)
9657 unsigned i;
9658 constructor_elt *p;
9659 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
9660 FOR_EACH_VEC_SAFE_ELT (elts, i, p)
9661 p->value = extend_ref_init_temps (decl, p->value, cleanups);
9664 return init;
9667 /* Returns true iff an initializer for TYPE could contain temporaries that
9668 need to be extended because they are bound to references or
9669 std::initializer_list. */
9671 bool
9672 type_has_extended_temps (tree type)
9674 type = strip_array_types (type);
9675 if (TREE_CODE (type) == REFERENCE_TYPE)
9676 return true;
9677 if (CLASS_TYPE_P (type))
9679 if (is_std_init_list (type))
9680 return true;
9681 for (tree f = next_initializable_field (TYPE_FIELDS (type));
9682 f; f = next_initializable_field (DECL_CHAIN (f)))
9683 if (type_has_extended_temps (TREE_TYPE (f)))
9684 return true;
9686 return false;
9689 /* Returns true iff TYPE is some variant of std::initializer_list. */
9691 bool
9692 is_std_init_list (tree type)
9694 /* Look through typedefs. */
9695 if (!TYPE_P (type))
9696 return false;
9697 if (cxx_dialect == cxx98)
9698 return false;
9699 type = TYPE_MAIN_VARIANT (type);
9700 return (CLASS_TYPE_P (type)
9701 && CP_TYPE_CONTEXT (type) == std_node
9702 && CLASSTYPE_TEMPLATE_INFO (type)
9703 && strcmp (TYPE_NAME_STRING (type), "initializer_list") == 0);
9706 /* Returns true iff DECL is a list constructor: i.e. a constructor which
9707 will accept an argument list of a single std::initializer_list<T>. */
9709 bool
9710 is_list_ctor (tree decl)
9712 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
9713 tree arg;
9715 if (!args || args == void_list_node)
9716 return false;
9718 arg = non_reference (TREE_VALUE (args));
9719 if (!is_std_init_list (arg))
9720 return false;
9722 args = TREE_CHAIN (args);
9724 if (args && args != void_list_node && !TREE_PURPOSE (args))
9725 /* There are more non-defaulted parms. */
9726 return false;
9728 return true;
9731 #include "gt-cp-call.h"