/
[official-gcc.git] / gcc / cp / call.c
blob7d602b94d22c1bbb6e2ebe85d8120b5567a7e6cc
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com) and
6 modified by Brendan Kehoe (brendan@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
25 /* High-level class interface. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "output.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"
43 /* The various kinds of conversion. */
45 typedef enum conversion_kind {
46 ck_identity,
47 ck_lvalue,
48 ck_qual,
49 ck_std,
50 ck_ptr,
51 ck_pmem,
52 ck_base,
53 ck_ref_bind,
54 ck_user,
55 ck_ambig,
56 ck_list,
57 ck_aggr,
58 ck_rvalue
59 } conversion_kind;
61 /* The rank of the conversion. Order of the enumerals matters; better
62 conversions should come earlier in the list. */
64 typedef enum conversion_rank {
65 cr_identity,
66 cr_exact,
67 cr_promotion,
68 cr_std,
69 cr_pbool,
70 cr_user,
71 cr_ellipsis,
72 cr_bad
73 } conversion_rank;
75 /* An implicit conversion sequence, in the sense of [over.best.ics].
76 The first conversion to be performed is at the end of the chain.
77 That conversion is always a cr_identity conversion. */
79 typedef struct conversion conversion;
80 struct conversion {
81 /* The kind of conversion represented by this step. */
82 conversion_kind kind;
83 /* The rank of this conversion. */
84 conversion_rank rank;
85 BOOL_BITFIELD user_conv_p : 1;
86 BOOL_BITFIELD ellipsis_p : 1;
87 BOOL_BITFIELD this_p : 1;
88 /* True if this conversion would be permitted with a bending of
89 language standards, e.g. disregarding pointer qualifiers or
90 converting integers to pointers. */
91 BOOL_BITFIELD bad_p : 1;
92 /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
93 temporary should be created to hold the result of the
94 conversion. */
95 BOOL_BITFIELD need_temporary_p : 1;
96 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
97 from a pointer-to-derived to pointer-to-base is being performed. */
98 BOOL_BITFIELD base_p : 1;
99 /* If KIND is ck_ref_bind, true when either an lvalue reference is
100 being bound to an lvalue expression or an rvalue reference is
101 being bound to an rvalue expression. */
102 BOOL_BITFIELD rvaluedness_matches_p: 1;
103 BOOL_BITFIELD check_narrowing: 1;
104 /* The type of the expression resulting from the conversion. */
105 tree type;
106 union {
107 /* The next conversion in the chain. Since the conversions are
108 arranged from outermost to innermost, the NEXT conversion will
109 actually be performed before this conversion. This variant is
110 used only when KIND is neither ck_identity nor ck_ambig. */
111 conversion *next;
112 /* The expression at the beginning of the conversion chain. This
113 variant is used only if KIND is ck_identity or ck_ambig. */
114 tree expr;
115 /* The array of conversions for an initializer_list. */
116 conversion **list;
117 } u;
118 /* The function candidate corresponding to this conversion
119 sequence. This field is only used if KIND is ck_user. */
120 struct z_candidate *cand;
123 #define CONVERSION_RANK(NODE) \
124 ((NODE)->bad_p ? cr_bad \
125 : (NODE)->ellipsis_p ? cr_ellipsis \
126 : (NODE)->user_conv_p ? cr_user \
127 : (NODE)->rank)
129 #define BAD_CONVERSION_RANK(NODE) \
130 ((NODE)->ellipsis_p ? cr_ellipsis \
131 : (NODE)->user_conv_p ? cr_user \
132 : (NODE)->rank)
134 static struct obstack conversion_obstack;
135 static bool conversion_obstack_initialized;
136 struct rejection_reason;
138 static struct z_candidate * tourney (struct z_candidate *);
139 static int equal_functions (tree, tree);
140 static int joust (struct z_candidate *, struct z_candidate *, bool);
141 static int compare_ics (conversion *, conversion *);
142 static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
143 static tree build_java_interface_fn_ref (tree, tree);
144 #define convert_like(CONV, EXPR, COMPLAIN) \
145 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \
146 /*issue_conversion_warnings=*/true, \
147 /*c_cast_p=*/false, (COMPLAIN))
148 #define convert_like_with_context(CONV, EXPR, FN, ARGNO, COMPLAIN ) \
149 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
150 /*issue_conversion_warnings=*/true, \
151 /*c_cast_p=*/false, (COMPLAIN))
152 static tree convert_like_real (conversion *, tree, tree, int, int, bool,
153 bool, tsubst_flags_t);
154 static void op_error (enum tree_code, enum tree_code, tree, tree,
155 tree, bool);
156 static VEC(tree,gc) *resolve_args (VEC(tree,gc) *);
157 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
158 static void print_z_candidate (const char *, struct z_candidate *);
159 static void print_z_candidates (location_t, struct z_candidate *);
160 static tree build_this (tree);
161 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
162 static bool any_strictly_viable (struct z_candidate *);
163 static struct z_candidate *add_template_candidate
164 (struct z_candidate **, tree, tree, tree, tree, const VEC(tree,gc) *,
165 tree, tree, tree, int, unification_kind_t);
166 static struct z_candidate *add_template_candidate_real
167 (struct z_candidate **, tree, tree, tree, tree, const VEC(tree,gc) *,
168 tree, tree, tree, int, tree, unification_kind_t);
169 static struct z_candidate *add_template_conv_candidate
170 (struct z_candidate **, tree, tree, tree, const VEC(tree,gc) *, tree,
171 tree, tree);
172 static void add_builtin_candidates
173 (struct z_candidate **, enum tree_code, enum tree_code,
174 tree, tree *, int);
175 static void add_builtin_candidate
176 (struct z_candidate **, enum tree_code, enum tree_code,
177 tree, tree, tree, tree *, tree *, int);
178 static bool is_complete (tree);
179 static void build_builtin_candidate
180 (struct z_candidate **, tree, tree, tree, tree *, tree *,
181 int);
182 static struct z_candidate *add_conv_candidate
183 (struct z_candidate **, tree, tree, tree, const VEC(tree,gc) *, tree,
184 tree);
185 static struct z_candidate *add_function_candidate
186 (struct z_candidate **, tree, tree, tree, const VEC(tree,gc) *, tree,
187 tree, int);
188 static conversion *implicit_conversion (tree, tree, tree, bool, int);
189 static conversion *standard_conversion (tree, tree, tree, bool, int);
190 static conversion *reference_binding (tree, tree, tree, bool, int);
191 static conversion *build_conv (conversion_kind, tree, conversion *);
192 static conversion *build_list_conv (tree, tree, int);
193 static bool is_subseq (conversion *, conversion *);
194 static conversion *maybe_handle_ref_bind (conversion **);
195 static void maybe_handle_implicit_object (conversion **);
196 static struct z_candidate *add_candidate
197 (struct z_candidate **, tree, tree, const VEC(tree,gc) *, size_t,
198 conversion **, tree, tree, int, struct rejection_reason *);
199 static tree source_type (conversion *);
200 static void add_warning (struct z_candidate *, struct z_candidate *);
201 static bool reference_compatible_p (tree, tree);
202 static conversion *convert_class_to_reference (tree, tree, tree, int);
203 static conversion *direct_reference_binding (tree, conversion *);
204 static bool promoted_arithmetic_type_p (tree);
205 static conversion *conditional_conversion (tree, tree);
206 static char *name_as_c_string (tree, tree, bool *);
207 static tree prep_operand (tree);
208 static void add_candidates (tree, tree, const VEC(tree,gc) *, tree, tree, bool,
209 tree, tree, int, struct z_candidate **);
210 static conversion *merge_conversion_sequences (conversion *, conversion *);
211 static bool magic_varargs_p (tree);
212 static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
214 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
215 NAME can take many forms... */
217 bool
218 check_dtor_name (tree basetype, tree name)
220 /* Just accept something we've already complained about. */
221 if (name == error_mark_node)
222 return true;
224 if (TREE_CODE (name) == TYPE_DECL)
225 name = TREE_TYPE (name);
226 else if (TYPE_P (name))
227 /* OK */;
228 else if (TREE_CODE (name) == IDENTIFIER_NODE)
230 if ((MAYBE_CLASS_TYPE_P (basetype)
231 && name == constructor_name (basetype))
232 || (TREE_CODE (basetype) == ENUMERAL_TYPE
233 && name == TYPE_IDENTIFIER (basetype)))
234 return true;
235 else
236 name = get_type_value (name);
238 else
240 /* In the case of:
242 template <class T> struct S { ~S(); };
243 int i;
244 i.~S();
246 NAME will be a class template. */
247 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
248 return false;
251 if (!name || name == error_mark_node)
252 return false;
253 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
256 /* We want the address of a function or method. We avoid creating a
257 pointer-to-member function. */
259 tree
260 build_addr_func (tree function)
262 tree type = TREE_TYPE (function);
264 /* We have to do these by hand to avoid real pointer to member
265 functions. */
266 if (TREE_CODE (type) == METHOD_TYPE)
268 if (TREE_CODE (function) == OFFSET_REF)
270 tree object = build_address (TREE_OPERAND (function, 0));
271 return get_member_function_from_ptrfunc (&object,
272 TREE_OPERAND (function, 1));
274 function = build_address (function);
276 else
277 function = decay_conversion (function);
279 return function;
282 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
283 POINTER_TYPE to those. Note, pointer to member function types
284 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
285 two variants. build_call_a is the primitive taking an array of
286 arguments, while build_call_n is a wrapper that handles varargs. */
288 tree
289 build_call_n (tree function, int n, ...)
291 if (n == 0)
292 return build_call_a (function, 0, NULL);
293 else
295 tree *argarray = XALLOCAVEC (tree, n);
296 va_list ap;
297 int i;
299 va_start (ap, n);
300 for (i = 0; i < n; i++)
301 argarray[i] = va_arg (ap, tree);
302 va_end (ap);
303 return build_call_a (function, n, argarray);
307 tree
308 build_call_a (tree function, int n, tree *argarray)
310 int is_constructor = 0;
311 int nothrow;
312 tree decl;
313 tree result_type;
314 tree fntype;
315 int i;
317 function = build_addr_func (function);
319 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
320 fntype = TREE_TYPE (TREE_TYPE (function));
321 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
322 || TREE_CODE (fntype) == METHOD_TYPE);
323 result_type = TREE_TYPE (fntype);
324 /* An rvalue has no cv-qualifiers. */
325 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
326 result_type = cv_unqualified (result_type);
328 if (TREE_CODE (function) == ADDR_EXPR
329 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
331 decl = TREE_OPERAND (function, 0);
332 if (!TREE_USED (decl))
334 /* We invoke build_call directly for several library
335 functions. These may have been declared normally if
336 we're building libgcc, so we can't just check
337 DECL_ARTIFICIAL. */
338 gcc_assert (DECL_ARTIFICIAL (decl)
339 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
340 "__", 2));
341 mark_used (decl);
344 else
345 decl = NULL_TREE;
347 /* We check both the decl and the type; a function may be known not to
348 throw without being declared throw(). */
349 nothrow = ((decl && TREE_NOTHROW (decl))
350 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
352 if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
353 current_function_returns_abnormally = 1;
355 if (decl && TREE_DEPRECATED (decl))
356 warn_deprecated_use (decl, NULL_TREE);
357 require_complete_eh_spec_types (fntype, decl);
359 if (decl && DECL_CONSTRUCTOR_P (decl))
360 is_constructor = 1;
362 /* Don't pass empty class objects by value. This is useful
363 for tags in STL, which are used to control overload resolution.
364 We don't need to handle other cases of copying empty classes. */
365 if (! decl || ! DECL_BUILT_IN (decl))
366 for (i = 0; i < n; i++)
367 if (is_empty_class (TREE_TYPE (argarray[i]))
368 && ! TREE_ADDRESSABLE (TREE_TYPE (argarray[i])))
370 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (argarray[i]));
371 argarray[i] = build2 (COMPOUND_EXPR, TREE_TYPE (t),
372 argarray[i], t);
375 function = build_call_array_loc (input_location,
376 result_type, function, n, argarray);
377 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
378 TREE_NOTHROW (function) = nothrow;
380 return function;
383 /* Build something of the form ptr->method (args)
384 or object.method (args). This can also build
385 calls to constructors, and find friends.
387 Member functions always take their class variable
388 as a pointer.
390 INSTANCE is a class instance.
392 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
394 PARMS help to figure out what that NAME really refers to.
396 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
397 down to the real instance type to use for access checking. We need this
398 information to get protected accesses correct.
400 FLAGS is the logical disjunction of zero or more LOOKUP_
401 flags. See cp-tree.h for more info.
403 If this is all OK, calls build_function_call with the resolved
404 member function.
406 This function must also handle being called to perform
407 initialization, promotion/coercion of arguments, and
408 instantiation of default parameters.
410 Note that NAME may refer to an instance variable name. If
411 `operator()()' is defined for the type of that field, then we return
412 that result. */
414 /* New overloading code. */
416 typedef struct z_candidate z_candidate;
418 typedef struct candidate_warning candidate_warning;
419 struct candidate_warning {
420 z_candidate *loser;
421 candidate_warning *next;
424 /* Information for providing diagnostics about why overloading failed. */
426 enum rejection_reason_code {
427 rr_none,
428 rr_arity,
429 rr_arg_conversion,
430 rr_bad_arg_conversion
433 struct conversion_info {
434 /* The index of the argument, 0-based. */
435 int n_arg;
436 /* The type of the actual argument. */
437 tree from_type;
438 /* The type of the formal argument. */
439 tree to_type;
442 struct rejection_reason {
443 enum rejection_reason_code code;
444 union {
445 /* Information about an arity mismatch. */
446 struct {
447 /* The expected number of arguments. */
448 int expected;
449 /* The actual number of arguments in the call. */
450 int actual;
451 /* Whether the call was a varargs call. */
452 bool call_varargs_p;
453 } arity;
454 /* Information about an argument conversion mismatch. */
455 struct conversion_info conversion;
456 /* Same, but for bad argument conversions. */
457 struct conversion_info bad_conversion;
458 } u;
461 struct z_candidate {
462 /* The FUNCTION_DECL that will be called if this candidate is
463 selected by overload resolution. */
464 tree fn;
465 /* If not NULL_TREE, the first argument to use when calling this
466 function. */
467 tree first_arg;
468 /* The rest of the arguments to use when calling this function. If
469 there are no further arguments this may be NULL or it may be an
470 empty vector. */
471 const VEC(tree,gc) *args;
472 /* The implicit conversion sequences for each of the arguments to
473 FN. */
474 conversion **convs;
475 /* The number of implicit conversion sequences. */
476 size_t num_convs;
477 /* If FN is a user-defined conversion, the standard conversion
478 sequence from the type returned by FN to the desired destination
479 type. */
480 conversion *second_conv;
481 int viable;
482 struct rejection_reason *reason;
483 /* If FN is a member function, the binfo indicating the path used to
484 qualify the name of FN at the call site. This path is used to
485 determine whether or not FN is accessible if it is selected by
486 overload resolution. The DECL_CONTEXT of FN will always be a
487 (possibly improper) base of this binfo. */
488 tree access_path;
489 /* If FN is a non-static member function, the binfo indicating the
490 subobject to which the `this' pointer should be converted if FN
491 is selected by overload resolution. The type pointed to the by
492 the `this' pointer must correspond to the most derived class
493 indicated by the CONVERSION_PATH. */
494 tree conversion_path;
495 tree template_decl;
496 tree explicit_targs;
497 candidate_warning *warnings;
498 z_candidate *next;
501 /* Returns true iff T is a null pointer constant in the sense of
502 [conv.ptr]. */
504 bool
505 null_ptr_cst_p (tree t)
507 /* [conv.ptr]
509 A null pointer constant is an integral constant expression
510 (_expr.const_) rvalue of integer type that evaluates to zero or
511 an rvalue of type std::nullptr_t. */
512 if (NULLPTR_TYPE_P (TREE_TYPE (t)))
513 return true;
514 if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)))
516 if (cxx_dialect >= cxx0x)
518 t = fold_non_dependent_expr (t);
519 t = maybe_constant_value (t);
520 if (TREE_CONSTANT (t) && integer_zerop (t))
521 return true;
523 else
525 t = integral_constant_value (t);
526 STRIP_NOPS (t);
527 if (integer_zerop (t) && !TREE_OVERFLOW (t))
528 return true;
531 return false;
534 /* Returns nonzero if PARMLIST consists of only default parms and/or
535 ellipsis. */
537 bool
538 sufficient_parms_p (const_tree parmlist)
540 for (; parmlist && parmlist != void_list_node;
541 parmlist = TREE_CHAIN (parmlist))
542 if (!TREE_PURPOSE (parmlist))
543 return false;
544 return true;
547 /* Allocate N bytes of memory from the conversion obstack. The memory
548 is zeroed before being returned. */
550 static void *
551 conversion_obstack_alloc (size_t n)
553 void *p;
554 if (!conversion_obstack_initialized)
556 gcc_obstack_init (&conversion_obstack);
557 conversion_obstack_initialized = true;
559 p = obstack_alloc (&conversion_obstack, n);
560 memset (p, 0, n);
561 return p;
564 /* Allocate rejection reasons. */
566 static struct rejection_reason *
567 alloc_rejection (enum rejection_reason_code code)
569 struct rejection_reason *p;
570 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
571 p->code = code;
572 return p;
575 static struct rejection_reason *
576 arity_rejection (tree first_arg, int expected, int actual)
578 struct rejection_reason *r = alloc_rejection (rr_arity);
579 int adjust = first_arg != NULL_TREE;
580 r->u.arity.expected = expected - adjust;
581 r->u.arity.actual = actual - adjust;
582 return r;
585 static struct rejection_reason *
586 arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
588 struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
589 int adjust = first_arg != NULL_TREE;
590 r->u.conversion.n_arg = n_arg - adjust;
591 r->u.conversion.from_type = from;
592 r->u.conversion.to_type = to;
593 return r;
596 static struct rejection_reason *
597 bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
599 struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
600 int adjust = first_arg != NULL_TREE;
601 r->u.bad_conversion.n_arg = n_arg - adjust;
602 r->u.bad_conversion.from_type = from;
603 r->u.bad_conversion.to_type = to;
604 return r;
607 /* Dynamically allocate a conversion. */
609 static conversion *
610 alloc_conversion (conversion_kind kind)
612 conversion *c;
613 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
614 c->kind = kind;
615 return c;
618 #ifdef ENABLE_CHECKING
620 /* Make sure that all memory on the conversion obstack has been
621 freed. */
623 void
624 validate_conversion_obstack (void)
626 if (conversion_obstack_initialized)
627 gcc_assert ((obstack_next_free (&conversion_obstack)
628 == obstack_base (&conversion_obstack)));
631 #endif /* ENABLE_CHECKING */
633 /* Dynamically allocate an array of N conversions. */
635 static conversion **
636 alloc_conversions (size_t n)
638 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
641 static conversion *
642 build_conv (conversion_kind code, tree type, conversion *from)
644 conversion *t;
645 conversion_rank rank = CONVERSION_RANK (from);
647 /* Note that the caller is responsible for filling in t->cand for
648 user-defined conversions. */
649 t = alloc_conversion (code);
650 t->type = type;
651 t->u.next = from;
653 switch (code)
655 case ck_ptr:
656 case ck_pmem:
657 case ck_base:
658 case ck_std:
659 if (rank < cr_std)
660 rank = cr_std;
661 break;
663 case ck_qual:
664 if (rank < cr_exact)
665 rank = cr_exact;
666 break;
668 default:
669 break;
671 t->rank = rank;
672 t->user_conv_p = (code == ck_user || from->user_conv_p);
673 t->bad_p = from->bad_p;
674 t->base_p = false;
675 return t;
678 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
679 specialization of std::initializer_list<T>, if such a conversion is
680 possible. */
682 static conversion *
683 build_list_conv (tree type, tree ctor, int flags)
685 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
686 unsigned len = CONSTRUCTOR_NELTS (ctor);
687 conversion **subconvs = alloc_conversions (len);
688 conversion *t;
689 unsigned i;
690 tree val;
692 /* Within a list-initialization we can have more user-defined
693 conversions. */
694 flags &= ~LOOKUP_NO_CONVERSION;
695 /* But no narrowing conversions. */
696 flags |= LOOKUP_NO_NARROWING;
698 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
700 conversion *sub
701 = implicit_conversion (elttype, TREE_TYPE (val), val,
702 false, flags);
703 if (sub == NULL)
704 return NULL;
706 subconvs[i] = sub;
709 t = alloc_conversion (ck_list);
710 t->type = type;
711 t->u.list = subconvs;
712 t->rank = cr_exact;
714 for (i = 0; i < len; ++i)
716 conversion *sub = subconvs[i];
717 if (sub->rank > t->rank)
718 t->rank = sub->rank;
719 if (sub->user_conv_p)
720 t->user_conv_p = true;
721 if (sub->bad_p)
722 t->bad_p = true;
725 return t;
728 /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
729 is a valid aggregate initializer for array type ATYPE. */
731 static bool
732 can_convert_array (tree atype, tree ctor, int flags)
734 unsigned i;
735 tree elttype = TREE_TYPE (atype);
736 for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
738 tree val = CONSTRUCTOR_ELT (ctor, i)->value;
739 bool ok;
740 if (TREE_CODE (elttype) == ARRAY_TYPE
741 && TREE_CODE (val) == CONSTRUCTOR)
742 ok = can_convert_array (elttype, val, flags);
743 else
744 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags);
745 if (!ok)
746 return false;
748 return true;
751 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
752 aggregate class, if such a conversion is possible. */
754 static conversion *
755 build_aggr_conv (tree type, tree ctor, int flags)
757 unsigned HOST_WIDE_INT i = 0;
758 conversion *c;
759 tree field = next_initializable_field (TYPE_FIELDS (type));
760 tree empty_ctor = NULL_TREE;
762 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
764 tree ftype = TREE_TYPE (field);
765 tree val;
766 bool ok;
768 if (i < CONSTRUCTOR_NELTS (ctor))
769 val = CONSTRUCTOR_ELT (ctor, i)->value;
770 else
772 if (empty_ctor == NULL_TREE)
773 empty_ctor = build_constructor (init_list_type_node, NULL);
774 val = empty_ctor;
776 ++i;
778 if (TREE_CODE (ftype) == ARRAY_TYPE
779 && TREE_CODE (val) == CONSTRUCTOR)
780 ok = can_convert_array (ftype, val, flags);
781 else
782 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags);
784 if (!ok)
785 return NULL;
787 if (TREE_CODE (type) == UNION_TYPE)
788 break;
791 if (i < CONSTRUCTOR_NELTS (ctor))
792 return NULL;
794 c = alloc_conversion (ck_aggr);
795 c->type = type;
796 c->rank = cr_exact;
797 c->user_conv_p = true;
798 c->u.next = NULL;
799 return c;
802 /* Build a representation of the identity conversion from EXPR to
803 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
805 static conversion *
806 build_identity_conv (tree type, tree expr)
808 conversion *c;
810 c = alloc_conversion (ck_identity);
811 c->type = type;
812 c->u.expr = expr;
814 return c;
817 /* Converting from EXPR to TYPE was ambiguous in the sense that there
818 were multiple user-defined conversions to accomplish the job.
819 Build a conversion that indicates that ambiguity. */
821 static conversion *
822 build_ambiguous_conv (tree type, tree expr)
824 conversion *c;
826 c = alloc_conversion (ck_ambig);
827 c->type = type;
828 c->u.expr = expr;
830 return c;
833 tree
834 strip_top_quals (tree t)
836 if (TREE_CODE (t) == ARRAY_TYPE)
837 return t;
838 return cp_build_qualified_type (t, 0);
841 /* Returns the standard conversion path (see [conv]) from type FROM to type
842 TO, if any. For proper handling of null pointer constants, you must
843 also pass the expression EXPR to convert from. If C_CAST_P is true,
844 this conversion is coming from a C-style cast. */
846 static conversion *
847 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
848 int flags)
850 enum tree_code fcode, tcode;
851 conversion *conv;
852 bool fromref = false;
854 to = non_reference (to);
855 if (TREE_CODE (from) == REFERENCE_TYPE)
857 fromref = true;
858 from = TREE_TYPE (from);
860 to = strip_top_quals (to);
861 from = strip_top_quals (from);
863 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
864 && expr && type_unknown_p (expr))
866 tsubst_flags_t tflags = tf_conv;
867 if (!(flags & LOOKUP_PROTECT))
868 tflags |= tf_no_access_control;
869 expr = instantiate_type (to, expr, tflags);
870 if (expr == error_mark_node)
871 return NULL;
872 from = TREE_TYPE (expr);
875 fcode = TREE_CODE (from);
876 tcode = TREE_CODE (to);
878 conv = build_identity_conv (from, expr);
879 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
881 from = type_decays_to (from);
882 fcode = TREE_CODE (from);
883 conv = build_conv (ck_lvalue, from, conv);
885 else if (fromref || (expr && lvalue_p (expr)))
887 if (expr)
889 tree bitfield_type;
890 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
891 if (bitfield_type)
893 from = strip_top_quals (bitfield_type);
894 fcode = TREE_CODE (from);
897 conv = build_conv (ck_rvalue, from, conv);
900 /* Allow conversion between `__complex__' data types. */
901 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
903 /* The standard conversion sequence to convert FROM to TO is
904 the standard conversion sequence to perform componentwise
905 conversion. */
906 conversion *part_conv = standard_conversion
907 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags);
909 if (part_conv)
911 conv = build_conv (part_conv->kind, to, conv);
912 conv->rank = part_conv->rank;
914 else
915 conv = NULL;
917 return conv;
920 if (same_type_p (from, to))
921 return conv;
923 /* [conv.ptr]
924 A null pointer constant can be converted to a pointer type; ... A
925 null pointer constant of integral type can be converted to an
926 rvalue of type std::nullptr_t. */
927 if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to)
928 || NULLPTR_TYPE_P (to))
929 && expr && null_ptr_cst_p (expr))
930 conv = build_conv (ck_std, to, conv);
931 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
932 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
934 /* For backwards brain damage compatibility, allow interconversion of
935 pointers and integers with a pedwarn. */
936 conv = build_conv (ck_std, to, conv);
937 conv->bad_p = true;
939 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
941 /* For backwards brain damage compatibility, allow interconversion of
942 enums and integers with a pedwarn. */
943 conv = build_conv (ck_std, to, conv);
944 conv->bad_p = true;
946 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
947 || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
949 tree to_pointee;
950 tree from_pointee;
952 if (tcode == POINTER_TYPE
953 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
954 TREE_TYPE (to)))
956 else if (VOID_TYPE_P (TREE_TYPE (to))
957 && !TYPE_PTRMEM_P (from)
958 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
960 tree nfrom = TREE_TYPE (from);
961 from = build_pointer_type
962 (cp_build_qualified_type (void_type_node,
963 cp_type_quals (nfrom)));
964 conv = build_conv (ck_ptr, from, conv);
966 else if (TYPE_PTRMEM_P (from))
968 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
969 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
971 if (DERIVED_FROM_P (fbase, tbase)
972 && (same_type_ignoring_top_level_qualifiers_p
973 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
974 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
976 from = build_ptrmem_type (tbase,
977 TYPE_PTRMEM_POINTED_TO_TYPE (from));
978 conv = build_conv (ck_pmem, from, conv);
980 else if (!same_type_p (fbase, tbase))
981 return NULL;
983 else if (CLASS_TYPE_P (TREE_TYPE (from))
984 && CLASS_TYPE_P (TREE_TYPE (to))
985 /* [conv.ptr]
987 An rvalue of type "pointer to cv D," where D is a
988 class type, can be converted to an rvalue of type
989 "pointer to cv B," where B is a base class (clause
990 _class.derived_) of D. If B is an inaccessible
991 (clause _class.access_) or ambiguous
992 (_class.member.lookup_) base class of D, a program
993 that necessitates this conversion is ill-formed.
994 Therefore, we use DERIVED_FROM_P, and do not check
995 access or uniqueness. */
996 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
998 from =
999 cp_build_qualified_type (TREE_TYPE (to),
1000 cp_type_quals (TREE_TYPE (from)));
1001 from = build_pointer_type (from);
1002 conv = build_conv (ck_ptr, from, conv);
1003 conv->base_p = true;
1006 if (tcode == POINTER_TYPE)
1008 to_pointee = TREE_TYPE (to);
1009 from_pointee = TREE_TYPE (from);
1011 else
1013 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1014 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1017 if (same_type_p (from, to))
1018 /* OK */;
1019 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
1020 /* In a C-style cast, we ignore CV-qualification because we
1021 are allowed to perform a static_cast followed by a
1022 const_cast. */
1023 conv = build_conv (ck_qual, to, conv);
1024 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1025 conv = build_conv (ck_qual, to, conv);
1026 else if (expr && string_conv_p (to, expr, 0))
1027 /* converting from string constant to char *. */
1028 conv = build_conv (ck_qual, to, conv);
1029 /* Allow conversions among compatible ObjC pointer types (base
1030 conversions have been already handled above). */
1031 else if (c_dialect_objc ()
1032 && objc_compare_types (to, from, -4, NULL_TREE))
1033 conv = build_conv (ck_ptr, to, conv);
1034 else if (ptr_reasonably_similar (to_pointee, from_pointee))
1036 conv = build_conv (ck_ptr, to, conv);
1037 conv->bad_p = true;
1039 else
1040 return NULL;
1042 from = to;
1044 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1046 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1047 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1048 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
1049 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
1051 if (!DERIVED_FROM_P (fbase, tbase)
1052 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
1053 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
1054 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
1055 || cp_type_quals (fbase) != cp_type_quals (tbase))
1056 return NULL;
1058 from = build_memfn_type (fromfn, tbase, cp_type_quals (tbase));
1059 from = build_ptrmemfunc_type (build_pointer_type (from));
1060 conv = build_conv (ck_pmem, from, conv);
1061 conv->base_p = true;
1063 else if (tcode == BOOLEAN_TYPE)
1065 /* [conv.bool]
1067 An rvalue of arithmetic, unscoped enumeration, pointer, or
1068 pointer to member type can be converted to an rvalue of type
1069 bool. ... An rvalue of type std::nullptr_t can be converted
1070 to an rvalue of type bool; */
1071 if (ARITHMETIC_TYPE_P (from)
1072 || UNSCOPED_ENUM_P (from)
1073 || fcode == POINTER_TYPE
1074 || TYPE_PTR_TO_MEMBER_P (from)
1075 || NULLPTR_TYPE_P (from))
1077 conv = build_conv (ck_std, to, conv);
1078 if (fcode == POINTER_TYPE
1079 || TYPE_PTRMEM_P (from)
1080 || (TYPE_PTRMEMFUNC_P (from)
1081 && conv->rank < cr_pbool)
1082 || NULLPTR_TYPE_P (from))
1083 conv->rank = cr_pbool;
1084 return conv;
1087 return NULL;
1089 /* We don't check for ENUMERAL_TYPE here because there are no standard
1090 conversions to enum type. */
1091 /* As an extension, allow conversion to complex type. */
1092 else if (ARITHMETIC_TYPE_P (to))
1094 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE)
1095 || SCOPED_ENUM_P (from))
1096 return NULL;
1097 conv = build_conv (ck_std, to, conv);
1099 /* Give this a better rank if it's a promotion. */
1100 if (same_type_p (to, type_promotes_to (from))
1101 && conv->u.next->rank <= cr_promotion)
1102 conv->rank = cr_promotion;
1104 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1105 && vector_types_convertible_p (from, to, false))
1106 return build_conv (ck_std, to, conv);
1107 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1108 && is_properly_derived_from (from, to))
1110 if (conv->kind == ck_rvalue)
1111 conv = conv->u.next;
1112 conv = build_conv (ck_base, to, conv);
1113 /* The derived-to-base conversion indicates the initialization
1114 of a parameter with base type from an object of a derived
1115 type. A temporary object is created to hold the result of
1116 the conversion unless we're binding directly to a reference. */
1117 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1119 else
1120 return NULL;
1122 if (flags & LOOKUP_NO_NARROWING)
1123 conv->check_narrowing = true;
1125 return conv;
1128 /* Returns nonzero if T1 is reference-related to T2. */
1130 bool
1131 reference_related_p (tree t1, tree t2)
1133 if (t1 == error_mark_node || t2 == error_mark_node)
1134 return false;
1136 t1 = TYPE_MAIN_VARIANT (t1);
1137 t2 = TYPE_MAIN_VARIANT (t2);
1139 /* [dcl.init.ref]
1141 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1142 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
1143 of T2. */
1144 return (same_type_p (t1, t2)
1145 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1146 && DERIVED_FROM_P (t1, t2)));
1149 /* Returns nonzero if T1 is reference-compatible with T2. */
1151 static bool
1152 reference_compatible_p (tree t1, tree t2)
1154 /* [dcl.init.ref]
1156 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
1157 reference-related to T2 and cv1 is the same cv-qualification as,
1158 or greater cv-qualification than, cv2. */
1159 return (reference_related_p (t1, t2)
1160 && at_least_as_qualified_p (t1, t2));
1163 /* Determine whether or not the EXPR (of class type S) can be
1164 converted to T as in [over.match.ref]. */
1166 static conversion *
1167 convert_class_to_reference (tree reference_type, tree s, tree expr, int flags)
1169 tree conversions;
1170 tree first_arg;
1171 conversion *conv;
1172 tree t;
1173 struct z_candidate *candidates;
1174 struct z_candidate *cand;
1175 bool any_viable_p;
1177 if (!expr)
1178 return NULL;
1180 conversions = lookup_conversions (s);
1181 if (!conversions)
1182 return NULL;
1184 /* [over.match.ref]
1186 Assuming that "cv1 T" is the underlying type of the reference
1187 being initialized, and "cv S" is the type of the initializer
1188 expression, with S a class type, the candidate functions are
1189 selected as follows:
1191 --The conversion functions of S and its base classes are
1192 considered. Those that are not hidden within S and yield type
1193 "reference to cv2 T2", where "cv1 T" is reference-compatible
1194 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
1196 The argument list has one argument, which is the initializer
1197 expression. */
1199 candidates = 0;
1201 /* Conceptually, we should take the address of EXPR and put it in
1202 the argument list. Unfortunately, however, that can result in
1203 error messages, which we should not issue now because we are just
1204 trying to find a conversion operator. Therefore, we use NULL,
1205 cast to the appropriate type. */
1206 first_arg = build_int_cst (build_pointer_type (s), 0);
1208 t = TREE_TYPE (reference_type);
1210 /* We're performing a user-defined conversion to a desired type, so set
1211 this for the benefit of add_candidates. */
1212 flags |= LOOKUP_NO_CONVERSION;
1214 for (; conversions; conversions = TREE_CHAIN (conversions))
1216 tree fns = TREE_VALUE (conversions);
1217 tree binfo = TREE_PURPOSE (conversions);
1218 struct z_candidate *old_candidates = candidates;;
1220 add_candidates (fns, first_arg, NULL, reference_type,
1221 NULL_TREE, false,
1222 binfo, TYPE_BINFO (s),
1223 flags, &candidates);
1225 for (cand = candidates; cand != old_candidates; cand = cand->next)
1227 /* Now, see if the conversion function really returns
1228 an lvalue of the appropriate type. From the
1229 point of view of unification, simply returning an
1230 rvalue of the right type is good enough. */
1231 tree f = cand->fn;
1232 tree t2 = TREE_TYPE (TREE_TYPE (f));
1233 if (TREE_CODE (t2) != REFERENCE_TYPE
1234 || !reference_compatible_p (t, TREE_TYPE (t2)))
1236 /* No need to set cand->reason here; this is most likely
1237 an ambiguous match. If it's not, either this candidate
1238 will win, or we will have identified a reason for it
1239 losing already. */
1240 cand->viable = 0;
1242 else
1244 conversion *identity_conv;
1245 /* Build a standard conversion sequence indicating the
1246 binding from the reference type returned by the
1247 function to the desired REFERENCE_TYPE. */
1248 identity_conv
1249 = build_identity_conv (TREE_TYPE (TREE_TYPE
1250 (TREE_TYPE (cand->fn))),
1251 NULL_TREE);
1252 cand->second_conv
1253 = (direct_reference_binding
1254 (reference_type, identity_conv));
1255 cand->second_conv->rvaluedness_matches_p
1256 = TYPE_REF_IS_RVALUE (TREE_TYPE (TREE_TYPE (cand->fn)))
1257 == TYPE_REF_IS_RVALUE (reference_type);
1258 cand->second_conv->bad_p |= cand->convs[0]->bad_p;
1260 /* Don't allow binding of lvalues to rvalue references. */
1261 if (TYPE_REF_IS_RVALUE (reference_type)
1262 && !TYPE_REF_IS_RVALUE (TREE_TYPE (TREE_TYPE (cand->fn))))
1263 cand->second_conv->bad_p = true;
1268 candidates = splice_viable (candidates, pedantic, &any_viable_p);
1269 /* If none of the conversion functions worked out, let our caller
1270 know. */
1271 if (!any_viable_p)
1272 return NULL;
1274 cand = tourney (candidates);
1275 if (!cand)
1276 return NULL;
1278 /* Now that we know that this is the function we're going to use fix
1279 the dummy first argument. */
1280 gcc_assert (cand->first_arg == NULL_TREE
1281 || integer_zerop (cand->first_arg));
1282 cand->first_arg = build_this (expr);
1284 /* Build a user-defined conversion sequence representing the
1285 conversion. */
1286 conv = build_conv (ck_user,
1287 TREE_TYPE (TREE_TYPE (cand->fn)),
1288 build_identity_conv (TREE_TYPE (expr), expr));
1289 conv->cand = cand;
1291 if (cand->viable == -1)
1292 conv->bad_p = true;
1294 /* Merge it with the standard conversion sequence from the
1295 conversion function's return type to the desired type. */
1296 cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
1298 return cand->second_conv;
1301 /* A reference of the indicated TYPE is being bound directly to the
1302 expression represented by the implicit conversion sequence CONV.
1303 Return a conversion sequence for this binding. */
1305 static conversion *
1306 direct_reference_binding (tree type, conversion *conv)
1308 tree t;
1310 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1311 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1313 t = TREE_TYPE (type);
1315 /* [over.ics.rank]
1317 When a parameter of reference type binds directly
1318 (_dcl.init.ref_) to an argument expression, the implicit
1319 conversion sequence is the identity conversion, unless the
1320 argument expression has a type that is a derived class of the
1321 parameter type, in which case the implicit conversion sequence is
1322 a derived-to-base Conversion.
1324 If the parameter binds directly to the result of applying a
1325 conversion function to the argument expression, the implicit
1326 conversion sequence is a user-defined conversion sequence
1327 (_over.ics.user_), with the second standard conversion sequence
1328 either an identity conversion or, if the conversion function
1329 returns an entity of a type that is a derived class of the
1330 parameter type, a derived-to-base conversion. */
1331 if (!same_type_ignoring_top_level_qualifiers_p (t, conv->type))
1333 /* Represent the derived-to-base conversion. */
1334 conv = build_conv (ck_base, t, conv);
1335 /* We will actually be binding to the base-class subobject in
1336 the derived class, so we mark this conversion appropriately.
1337 That way, convert_like knows not to generate a temporary. */
1338 conv->need_temporary_p = false;
1340 return build_conv (ck_ref_bind, type, conv);
1343 /* Returns the conversion path from type FROM to reference type TO for
1344 purposes of reference binding. For lvalue binding, either pass a
1345 reference type to FROM or an lvalue expression to EXPR. If the
1346 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1347 the conversion returned. If C_CAST_P is true, this
1348 conversion is coming from a C-style cast. */
1350 static conversion *
1351 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags)
1353 conversion *conv = NULL;
1354 tree to = TREE_TYPE (rto);
1355 tree from = rfrom;
1356 tree tfrom;
1357 bool related_p;
1358 bool compatible_p;
1359 cp_lvalue_kind is_lvalue = clk_none;
1361 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1363 expr = instantiate_type (to, expr, tf_none);
1364 if (expr == error_mark_node)
1365 return NULL;
1366 from = TREE_TYPE (expr);
1369 if (TREE_CODE (from) == REFERENCE_TYPE)
1371 /* Anything with reference type is an lvalue. */
1372 is_lvalue = clk_ordinary;
1373 from = TREE_TYPE (from);
1376 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1378 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1379 conv = implicit_conversion (to, from, expr, c_cast_p,
1380 flags);
1381 if (!CLASS_TYPE_P (to)
1382 && CONSTRUCTOR_NELTS (expr) == 1)
1384 expr = CONSTRUCTOR_ELT (expr, 0)->value;
1385 if (error_operand_p (expr))
1386 return NULL;
1387 from = TREE_TYPE (expr);
1391 if (is_lvalue == clk_none && expr)
1392 is_lvalue = real_lvalue_p (expr);
1394 tfrom = from;
1395 if ((is_lvalue & clk_bitfield) != 0)
1396 tfrom = unlowered_expr_type (expr);
1398 /* Figure out whether or not the types are reference-related and
1399 reference compatible. We have do do this after stripping
1400 references from FROM. */
1401 related_p = reference_related_p (to, tfrom);
1402 /* If this is a C cast, first convert to an appropriately qualified
1403 type, so that we can later do a const_cast to the desired type. */
1404 if (related_p && c_cast_p
1405 && !at_least_as_qualified_p (to, tfrom))
1406 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1407 compatible_p = reference_compatible_p (to, tfrom);
1409 /* Directly bind reference when target expression's type is compatible with
1410 the reference and expression is an lvalue. In DR391, the wording in
1411 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1412 const and rvalue references to rvalues of compatible class type.
1413 We should also do direct bindings for non-class "rvalues" derived from
1414 rvalue references. */
1415 if (compatible_p
1416 && (is_lvalue
1417 || (((CP_TYPE_CONST_NON_VOLATILE_P (to)
1418 && !(flags & LOOKUP_NO_TEMP_BIND))
1419 || TYPE_REF_IS_RVALUE (rto))
1420 && (CLASS_TYPE_P (from) || (expr && lvalue_p (expr))))))
1422 /* [dcl.init.ref]
1424 If the initializer expression
1426 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1427 is reference-compatible with "cv2 T2,"
1429 the reference is bound directly to the initializer expression
1430 lvalue.
1432 [...]
1433 If the initializer expression is an rvalue, with T2 a class type,
1434 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1435 is bound to the object represented by the rvalue or to a sub-object
1436 within that object. */
1438 conv = build_identity_conv (tfrom, expr);
1439 conv = direct_reference_binding (rto, conv);
1441 if (flags & LOOKUP_PREFER_RVALUE)
1442 /* The top-level caller requested that we pretend that the lvalue
1443 be treated as an rvalue. */
1444 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1445 else
1446 conv->rvaluedness_matches_p
1447 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1449 if ((is_lvalue & clk_bitfield) != 0
1450 || ((is_lvalue & clk_packed) != 0 && !TYPE_PACKED (to)))
1451 /* For the purposes of overload resolution, we ignore the fact
1452 this expression is a bitfield or packed field. (In particular,
1453 [over.ics.ref] says specifically that a function with a
1454 non-const reference parameter is viable even if the
1455 argument is a bitfield.)
1457 However, when we actually call the function we must create
1458 a temporary to which to bind the reference. If the
1459 reference is volatile, or isn't const, then we cannot make
1460 a temporary, so we just issue an error when the conversion
1461 actually occurs. */
1462 conv->need_temporary_p = true;
1464 /* Don't allow binding of lvalues to rvalue references. */
1465 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1466 && !(flags & LOOKUP_PREFER_RVALUE))
1467 conv->bad_p = true;
1469 return conv;
1471 /* [class.conv.fct] A conversion function is never used to convert a
1472 (possibly cv-qualified) object to the (possibly cv-qualified) same
1473 object type (or a reference to it), to a (possibly cv-qualified) base
1474 class of that type (or a reference to it).... */
1475 else if (CLASS_TYPE_P (from) && !related_p
1476 && !(flags & LOOKUP_NO_CONVERSION))
1478 /* [dcl.init.ref]
1480 If the initializer expression
1482 -- has a class type (i.e., T2 is a class type) can be
1483 implicitly converted to an lvalue of type "cv3 T3," where
1484 "cv1 T1" is reference-compatible with "cv3 T3". (this
1485 conversion is selected by enumerating the applicable
1486 conversion functions (_over.match.ref_) and choosing the
1487 best one through overload resolution. (_over.match_).
1489 the reference is bound to the lvalue result of the conversion
1490 in the second case. */
1491 conv = convert_class_to_reference (rto, from, expr, flags);
1492 if (conv)
1493 return conv;
1496 /* From this point on, we conceptually need temporaries, even if we
1497 elide them. Only the cases above are "direct bindings". */
1498 if (flags & LOOKUP_NO_TEMP_BIND)
1499 return NULL;
1501 /* [over.ics.rank]
1503 When a parameter of reference type is not bound directly to an
1504 argument expression, the conversion sequence is the one required
1505 to convert the argument expression to the underlying type of the
1506 reference according to _over.best.ics_. Conceptually, this
1507 conversion sequence corresponds to copy-initializing a temporary
1508 of the underlying type with the argument expression. Any
1509 difference in top-level cv-qualification is subsumed by the
1510 initialization itself and does not constitute a conversion. */
1512 /* [dcl.init.ref]
1514 Otherwise, the reference shall be to a non-volatile const type.
1516 Under C++0x, [8.5.3/5 dcl.init.ref] it may also be an rvalue reference */
1517 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1518 return NULL;
1520 /* [dcl.init.ref]
1522 Otherwise, a temporary of type "cv1 T1" is created and
1523 initialized from the initializer expression using the rules for a
1524 non-reference copy initialization. If T1 is reference-related to
1525 T2, cv1 must be the same cv-qualification as, or greater
1526 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1527 if (related_p && !at_least_as_qualified_p (to, from))
1528 return NULL;
1530 /* We're generating a temporary now, but don't bind any more in the
1531 conversion (specifically, don't slice the temporary returned by a
1532 conversion operator). */
1533 flags |= LOOKUP_NO_TEMP_BIND;
1535 /* Core issue 899: When [copy-]initializing a temporary to be bound
1536 to the first parameter of a copy constructor (12.8) called with
1537 a single argument in the context of direct-initialization,
1538 explicit conversion functions are also considered.
1540 So don't set LOOKUP_ONLYCONVERTING in that case. */
1541 if (!(flags & LOOKUP_COPY_PARM))
1542 flags |= LOOKUP_ONLYCONVERTING;
1544 if (!conv)
1545 conv = implicit_conversion (to, from, expr, c_cast_p,
1546 flags);
1547 if (!conv)
1548 return NULL;
1550 conv = build_conv (ck_ref_bind, rto, conv);
1551 /* This reference binding, unlike those above, requires the
1552 creation of a temporary. */
1553 conv->need_temporary_p = true;
1554 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1556 return conv;
1559 /* Returns the implicit conversion sequence (see [over.ics]) from type
1560 FROM to type TO. The optional expression EXPR may affect the
1561 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1562 true, this conversion is coming from a C-style cast. */
1564 static conversion *
1565 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1566 int flags)
1568 conversion *conv;
1570 if (from == error_mark_node || to == error_mark_node
1571 || expr == error_mark_node)
1572 return NULL;
1574 if (TREE_CODE (to) == REFERENCE_TYPE)
1575 conv = reference_binding (to, from, expr, c_cast_p, flags);
1576 else
1577 conv = standard_conversion (to, from, expr, c_cast_p, flags);
1579 if (conv)
1580 return conv;
1582 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1584 if (is_std_init_list (to))
1585 return build_list_conv (to, expr, flags);
1587 /* Allow conversion from an initializer-list with one element to a
1588 scalar type. */
1589 if (SCALAR_TYPE_P (to))
1591 int nelts = CONSTRUCTOR_NELTS (expr);
1592 tree elt;
1594 if (nelts == 0)
1595 elt = build_value_init (to, tf_none);
1596 else if (nelts == 1)
1597 elt = CONSTRUCTOR_ELT (expr, 0)->value;
1598 else
1599 elt = error_mark_node;
1601 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
1602 c_cast_p, flags);
1603 if (conv)
1605 conv->check_narrowing = true;
1606 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
1607 /* Too many levels of braces, i.e. '{{1}}'. */
1608 conv->bad_p = true;
1609 return conv;
1614 if (expr != NULL_TREE
1615 && (MAYBE_CLASS_TYPE_P (from)
1616 || MAYBE_CLASS_TYPE_P (to))
1617 && (flags & LOOKUP_NO_CONVERSION) == 0)
1619 struct z_candidate *cand;
1620 int convflags = (flags & (LOOKUP_NO_TEMP_BIND|LOOKUP_ONLYCONVERTING
1621 |LOOKUP_NO_NARROWING));
1623 if (CLASS_TYPE_P (to)
1624 && !CLASSTYPE_NON_AGGREGATE (complete_type (to))
1625 && BRACE_ENCLOSED_INITIALIZER_P (expr))
1626 return build_aggr_conv (to, expr, flags);
1628 cand = build_user_type_conversion_1 (to, expr, convflags);
1629 if (cand)
1630 conv = cand->second_conv;
1632 /* We used to try to bind a reference to a temporary here, but that
1633 is now handled after the recursive call to this function at the end
1634 of reference_binding. */
1635 return conv;
1638 return NULL;
1641 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1642 functions. ARGS will not be changed until a single candidate is
1643 selected. */
1645 static struct z_candidate *
1646 add_candidate (struct z_candidate **candidates,
1647 tree fn, tree first_arg, const VEC(tree,gc) *args,
1648 size_t num_convs, conversion **convs,
1649 tree access_path, tree conversion_path,
1650 int viable, struct rejection_reason *reason)
1652 struct z_candidate *cand = (struct z_candidate *)
1653 conversion_obstack_alloc (sizeof (struct z_candidate));
1655 cand->fn = fn;
1656 cand->first_arg = first_arg;
1657 cand->args = args;
1658 cand->convs = convs;
1659 cand->num_convs = num_convs;
1660 cand->access_path = access_path;
1661 cand->conversion_path = conversion_path;
1662 cand->viable = viable;
1663 cand->reason = reason;
1664 cand->next = *candidates;
1665 *candidates = cand;
1667 return cand;
1670 /* Return the number of remaining arguments in the parameter list
1671 beginning with ARG. */
1673 static int
1674 remaining_arguments (tree arg)
1676 int n;
1678 for (n = 0; arg != NULL_TREE && arg != void_list_node;
1679 arg = TREE_CHAIN (arg))
1680 n++;
1682 return n;
1685 /* Create an overload candidate for the function or method FN called
1686 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
1687 FLAGS is passed on to implicit_conversion.
1689 This does not change ARGS.
1691 CTYPE, if non-NULL, is the type we want to pretend this function
1692 comes from for purposes of overload resolution. */
1694 static struct z_candidate *
1695 add_function_candidate (struct z_candidate **candidates,
1696 tree fn, tree ctype, tree first_arg,
1697 const VEC(tree,gc) *args, tree access_path,
1698 tree conversion_path, int flags)
1700 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1701 int i, len;
1702 conversion **convs;
1703 tree parmnode;
1704 tree orig_first_arg = first_arg;
1705 int skip;
1706 int viable = 1;
1707 struct rejection_reason *reason = NULL;
1709 /* At this point we should not see any functions which haven't been
1710 explicitly declared, except for friend functions which will have
1711 been found using argument dependent lookup. */
1712 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
1714 /* The `this', `in_chrg' and VTT arguments to constructors are not
1715 considered in overload resolution. */
1716 if (DECL_CONSTRUCTOR_P (fn))
1718 parmlist = skip_artificial_parms_for (fn, parmlist);
1719 skip = num_artificial_parms_for (fn);
1720 if (skip > 0 && first_arg != NULL_TREE)
1722 --skip;
1723 first_arg = NULL_TREE;
1726 else
1727 skip = 0;
1729 len = VEC_length (tree, args) - skip + (first_arg != NULL_TREE ? 1 : 0);
1730 convs = alloc_conversions (len);
1732 /* 13.3.2 - Viable functions [over.match.viable]
1733 First, to be a viable function, a candidate function shall have enough
1734 parameters to agree in number with the arguments in the list.
1736 We need to check this first; otherwise, checking the ICSes might cause
1737 us to produce an ill-formed template instantiation. */
1739 parmnode = parmlist;
1740 for (i = 0; i < len; ++i)
1742 if (parmnode == NULL_TREE || parmnode == void_list_node)
1743 break;
1744 parmnode = TREE_CHAIN (parmnode);
1747 if ((i < len && parmnode)
1748 || !sufficient_parms_p (parmnode))
1750 int remaining = remaining_arguments (parmnode);
1751 viable = 0;
1752 reason = arity_rejection (first_arg, i + remaining, len);
1754 /* When looking for a function from a subobject from an implicit
1755 copy/move constructor/operator=, don't consider anything that takes (a
1756 reference to) an unrelated type. See c++/44909 and core 1092. */
1757 else if (parmlist && (flags & LOOKUP_DEFAULTED))
1759 if (DECL_CONSTRUCTOR_P (fn))
1760 i = 1;
1761 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1762 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
1763 i = 2;
1764 else
1765 i = 0;
1766 if (i && len == i)
1768 parmnode = chain_index (i-1, parmlist);
1769 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
1770 ctype))
1771 viable = 0;
1774 /* This only applies at the top level. */
1775 flags &= ~LOOKUP_DEFAULTED;
1778 if (! viable)
1779 goto out;
1781 /* Second, for F to be a viable function, there shall exist for each
1782 argument an implicit conversion sequence that converts that argument
1783 to the corresponding parameter of F. */
1785 parmnode = parmlist;
1787 for (i = 0; i < len; ++i)
1789 tree arg, argtype, to_type;
1790 conversion *t;
1791 int is_this;
1793 if (parmnode == void_list_node)
1794 break;
1796 if (i == 0 && first_arg != NULL_TREE)
1797 arg = first_arg;
1798 else
1799 arg = VEC_index (tree, args,
1800 i + skip - (first_arg != NULL_TREE ? 1 : 0));
1801 argtype = lvalue_type (arg);
1803 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1804 && ! DECL_CONSTRUCTOR_P (fn));
1806 if (parmnode)
1808 tree parmtype = TREE_VALUE (parmnode);
1809 int lflags = flags;
1811 parmnode = TREE_CHAIN (parmnode);
1813 /* The type of the implicit object parameter ('this') for
1814 overload resolution is not always the same as for the
1815 function itself; conversion functions are considered to
1816 be members of the class being converted, and functions
1817 introduced by a using-declaration are considered to be
1818 members of the class that uses them.
1820 Since build_over_call ignores the ICS for the `this'
1821 parameter, we can just change the parm type. */
1822 if (ctype && is_this)
1824 parmtype = cp_build_qualified_type
1825 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
1826 parmtype = build_pointer_type (parmtype);
1829 /* Core issue 899: When [copy-]initializing a temporary to be bound
1830 to the first parameter of a copy constructor (12.8) called with
1831 a single argument in the context of direct-initialization,
1832 explicit conversion functions are also considered.
1834 So set LOOKUP_COPY_PARM to let reference_binding know that
1835 it's being called in that context. We generalize the above
1836 to handle move constructors and template constructors as well;
1837 the standardese should soon be updated similarly. */
1838 if (ctype && i == 0 && (len-skip == 1)
1839 && !(flags & LOOKUP_ONLYCONVERTING)
1840 && DECL_CONSTRUCTOR_P (fn)
1841 && parmtype != error_mark_node
1842 && (same_type_ignoring_top_level_qualifiers_p
1843 (non_reference (parmtype), ctype)))
1845 lflags |= LOOKUP_COPY_PARM;
1846 /* We allow user-defined conversions within init-lists, but
1847 not for the copy constructor. */
1848 if (flags & LOOKUP_NO_COPY_CTOR_CONVERSION)
1849 lflags |= LOOKUP_NO_CONVERSION;
1851 else
1852 lflags |= LOOKUP_ONLYCONVERTING;
1854 t = implicit_conversion (parmtype, argtype, arg,
1855 /*c_cast_p=*/false, lflags);
1856 to_type = parmtype;
1858 else
1860 t = build_identity_conv (argtype, arg);
1861 t->ellipsis_p = true;
1862 to_type = argtype;
1865 if (t && is_this)
1866 t->this_p = true;
1868 convs[i] = t;
1869 if (! t)
1871 viable = 0;
1872 reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
1873 break;
1876 if (t->bad_p)
1878 viable = -1;
1879 reason = bad_arg_conversion_rejection (first_arg, i, argtype, to_type);
1883 out:
1884 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
1885 access_path, conversion_path, viable, reason);
1888 /* Create an overload candidate for the conversion function FN which will
1889 be invoked for expression OBJ, producing a pointer-to-function which
1890 will in turn be called with the argument list FIRST_ARG/ARGLIST,
1891 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
1892 passed on to implicit_conversion.
1894 Actually, we don't really care about FN; we care about the type it
1895 converts to. There may be multiple conversion functions that will
1896 convert to that type, and we rely on build_user_type_conversion_1 to
1897 choose the best one; so when we create our candidate, we record the type
1898 instead of the function. */
1900 static struct z_candidate *
1901 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
1902 tree first_arg, const VEC(tree,gc) *arglist,
1903 tree access_path, tree conversion_path)
1905 tree totype = TREE_TYPE (TREE_TYPE (fn));
1906 int i, len, viable, flags;
1907 tree parmlist, parmnode;
1908 conversion **convs;
1909 struct rejection_reason *reason;
1911 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1912 parmlist = TREE_TYPE (parmlist);
1913 parmlist = TYPE_ARG_TYPES (parmlist);
1915 len = VEC_length (tree, arglist) + (first_arg != NULL_TREE ? 1 : 0) + 1;
1916 convs = alloc_conversions (len);
1917 parmnode = parmlist;
1918 viable = 1;
1919 flags = LOOKUP_IMPLICIT;
1920 reason = NULL;
1922 /* Don't bother looking up the same type twice. */
1923 if (*candidates && (*candidates)->fn == totype)
1924 return NULL;
1926 for (i = 0; i < len; ++i)
1928 tree arg, argtype, convert_type = NULL_TREE;
1929 conversion *t;
1931 if (i == 0)
1932 arg = obj;
1933 else if (i == 1 && first_arg != NULL_TREE)
1934 arg = first_arg;
1935 else
1936 arg = VEC_index (tree, arglist,
1937 i - (first_arg != NULL_TREE ? 1 : 0) - 1);
1938 argtype = lvalue_type (arg);
1940 if (i == 0)
1942 t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false,
1943 flags);
1944 convert_type = totype;
1946 else if (parmnode == void_list_node)
1947 break;
1948 else if (parmnode)
1950 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
1951 /*c_cast_p=*/false, flags);
1952 convert_type = TREE_VALUE (parmnode);
1954 else
1956 t = build_identity_conv (argtype, arg);
1957 t->ellipsis_p = true;
1958 convert_type = argtype;
1961 convs[i] = t;
1962 if (! t)
1963 break;
1965 if (t->bad_p)
1967 viable = -1;
1968 reason = bad_arg_conversion_rejection (NULL_TREE, i, argtype, convert_type);
1971 if (i == 0)
1972 continue;
1974 if (parmnode)
1975 parmnode = TREE_CHAIN (parmnode);
1978 if (i < len
1979 || ! sufficient_parms_p (parmnode))
1981 int remaining = remaining_arguments (parmnode);
1982 viable = 0;
1983 reason = arity_rejection (NULL_TREE, i + remaining, len);
1986 return add_candidate (candidates, totype, first_arg, arglist, len, convs,
1987 access_path, conversion_path, viable, reason);
1990 static void
1991 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
1992 tree type1, tree type2, tree *args, tree *argtypes,
1993 int flags)
1995 conversion *t;
1996 conversion **convs;
1997 size_t num_convs;
1998 int viable = 1, i;
1999 tree types[2];
2000 struct rejection_reason *reason = NULL;
2002 types[0] = type1;
2003 types[1] = type2;
2005 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
2006 convs = alloc_conversions (num_convs);
2008 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2009 conversion ops are allowed. We handle that here by just checking for
2010 boolean_type_node because other operators don't ask for it. COND_EXPR
2011 also does contextual conversion to bool for the first operand, but we
2012 handle that in build_conditional_expr, and type1 here is operand 2. */
2013 if (type1 != boolean_type_node)
2014 flags |= LOOKUP_ONLYCONVERTING;
2016 for (i = 0; i < 2; ++i)
2018 if (! args[i])
2019 break;
2021 t = implicit_conversion (types[i], argtypes[i], args[i],
2022 /*c_cast_p=*/false, flags);
2023 if (! t)
2025 viable = 0;
2026 /* We need something for printing the candidate. */
2027 t = build_identity_conv (types[i], NULL_TREE);
2028 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i], types[i]);
2030 else if (t->bad_p)
2032 viable = 0;
2033 reason = bad_arg_conversion_rejection (NULL_TREE, i, argtypes[i], types[i]);
2035 convs[i] = t;
2038 /* For COND_EXPR we rearranged the arguments; undo that now. */
2039 if (args[2])
2041 convs[2] = convs[1];
2042 convs[1] = convs[0];
2043 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2044 /*c_cast_p=*/false, flags);
2045 if (t)
2046 convs[0] = t;
2047 else
2049 viable = 0;
2050 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2051 boolean_type_node);
2055 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2056 num_convs, convs,
2057 /*access_path=*/NULL_TREE,
2058 /*conversion_path=*/NULL_TREE,
2059 viable, reason);
2062 static bool
2063 is_complete (tree t)
2065 return COMPLETE_TYPE_P (complete_type (t));
2068 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2070 static bool
2071 promoted_arithmetic_type_p (tree type)
2073 /* [over.built]
2075 In this section, the term promoted integral type is used to refer
2076 to those integral types which are preserved by integral promotion
2077 (including e.g. int and long but excluding e.g. char).
2078 Similarly, the term promoted arithmetic type refers to promoted
2079 integral types plus floating types. */
2080 return ((CP_INTEGRAL_TYPE_P (type)
2081 && same_type_p (type_promotes_to (type), type))
2082 || TREE_CODE (type) == REAL_TYPE);
2085 /* Create any builtin operator overload candidates for the operator in
2086 question given the converted operand types TYPE1 and TYPE2. The other
2087 args are passed through from add_builtin_candidates to
2088 build_builtin_candidate.
2090 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2091 If CODE is requires candidates operands of the same type of the kind
2092 of which TYPE1 and TYPE2 are, we add both candidates
2093 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2095 static void
2096 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2097 enum tree_code code2, tree fnname, tree type1,
2098 tree type2, tree *args, tree *argtypes, int flags)
2100 switch (code)
2102 case POSTINCREMENT_EXPR:
2103 case POSTDECREMENT_EXPR:
2104 args[1] = integer_zero_node;
2105 type2 = integer_type_node;
2106 break;
2107 default:
2108 break;
2111 switch (code)
2114 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2115 and VQ is either volatile or empty, there exist candidate operator
2116 functions of the form
2117 VQ T& operator++(VQ T&);
2118 T operator++(VQ T&, int);
2119 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
2120 type other than bool, and VQ is either volatile or empty, there exist
2121 candidate operator functions of the form
2122 VQ T& operator--(VQ T&);
2123 T operator--(VQ T&, int);
2124 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
2125 complete object type, and VQ is either volatile or empty, there exist
2126 candidate operator functions of the form
2127 T*VQ& operator++(T*VQ&);
2128 T*VQ& operator--(T*VQ&);
2129 T* operator++(T*VQ&, int);
2130 T* operator--(T*VQ&, int); */
2132 case POSTDECREMENT_EXPR:
2133 case PREDECREMENT_EXPR:
2134 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2135 return;
2136 case POSTINCREMENT_EXPR:
2137 case PREINCREMENT_EXPR:
2138 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2140 type1 = build_reference_type (type1);
2141 break;
2143 return;
2145 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
2146 exist candidate operator functions of the form
2148 T& operator*(T*);
2150 8 For every function type T, there exist candidate operator functions of
2151 the form
2152 T& operator*(T*); */
2154 case INDIRECT_REF:
2155 if (TREE_CODE (type1) == POINTER_TYPE
2156 && is_complete (TREE_TYPE (type1))
2157 && (TYPE_PTROB_P (type1)
2158 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2159 break;
2160 return;
2162 /* 9 For every type T, there exist candidate operator functions of the form
2163 T* operator+(T*);
2165 10For every promoted arithmetic type T, there exist candidate operator
2166 functions of the form
2167 T operator+(T);
2168 T operator-(T); */
2170 case UNARY_PLUS_EXPR: /* unary + */
2171 if (TREE_CODE (type1) == POINTER_TYPE)
2172 break;
2173 case NEGATE_EXPR:
2174 if (ARITHMETIC_TYPE_P (type1))
2175 break;
2176 return;
2178 /* 11For every promoted integral type T, there exist candidate operator
2179 functions of the form
2180 T operator~(T); */
2182 case BIT_NOT_EXPR:
2183 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2184 break;
2185 return;
2187 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2188 is the same type as C2 or is a derived class of C2, T is a complete
2189 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2190 there exist candidate operator functions of the form
2191 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2192 where CV12 is the union of CV1 and CV2. */
2194 case MEMBER_REF:
2195 if (TREE_CODE (type1) == POINTER_TYPE
2196 && TYPE_PTR_TO_MEMBER_P (type2))
2198 tree c1 = TREE_TYPE (type1);
2199 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2201 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2202 && (TYPE_PTRMEMFUNC_P (type2)
2203 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2204 break;
2206 return;
2208 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2209 didate operator functions of the form
2210 LR operator*(L, R);
2211 LR operator/(L, R);
2212 LR operator+(L, R);
2213 LR operator-(L, R);
2214 bool operator<(L, R);
2215 bool operator>(L, R);
2216 bool operator<=(L, R);
2217 bool operator>=(L, R);
2218 bool operator==(L, R);
2219 bool operator!=(L, R);
2220 where LR is the result of the usual arithmetic conversions between
2221 types L and R.
2223 14For every pair of types T and I, where T is a cv-qualified or cv-
2224 unqualified complete object type and I is a promoted integral type,
2225 there exist candidate operator functions of the form
2226 T* operator+(T*, I);
2227 T& operator[](T*, I);
2228 T* operator-(T*, I);
2229 T* operator+(I, T*);
2230 T& operator[](I, T*);
2232 15For every T, where T is a pointer to complete object type, there exist
2233 candidate operator functions of the form112)
2234 ptrdiff_t operator-(T, T);
2236 16For every pointer or enumeration type T, there exist candidate operator
2237 functions of the form
2238 bool operator<(T, T);
2239 bool operator>(T, T);
2240 bool operator<=(T, T);
2241 bool operator>=(T, T);
2242 bool operator==(T, T);
2243 bool operator!=(T, T);
2245 17For every pointer to member type T, there exist candidate operator
2246 functions of the form
2247 bool operator==(T, T);
2248 bool operator!=(T, T); */
2250 case MINUS_EXPR:
2251 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2252 break;
2253 if (TYPE_PTROB_P (type1)
2254 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2256 type2 = ptrdiff_type_node;
2257 break;
2259 case MULT_EXPR:
2260 case TRUNC_DIV_EXPR:
2261 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2262 break;
2263 return;
2265 case EQ_EXPR:
2266 case NE_EXPR:
2267 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2268 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
2269 break;
2270 if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
2272 type2 = type1;
2273 break;
2275 if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
2277 type1 = type2;
2278 break;
2280 /* Fall through. */
2281 case LT_EXPR:
2282 case GT_EXPR:
2283 case LE_EXPR:
2284 case GE_EXPR:
2285 case MAX_EXPR:
2286 case MIN_EXPR:
2287 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2288 break;
2289 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2290 break;
2291 if (TREE_CODE (type1) == ENUMERAL_TYPE
2292 && TREE_CODE (type2) == ENUMERAL_TYPE)
2293 break;
2294 if (TYPE_PTR_P (type1)
2295 && null_ptr_cst_p (args[1])
2296 && !uses_template_parms (type1))
2298 type2 = type1;
2299 break;
2301 if (null_ptr_cst_p (args[0])
2302 && TYPE_PTR_P (type2)
2303 && !uses_template_parms (type2))
2305 type1 = type2;
2306 break;
2308 return;
2310 case PLUS_EXPR:
2311 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2312 break;
2313 case ARRAY_REF:
2314 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2316 type1 = ptrdiff_type_node;
2317 break;
2319 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2321 type2 = ptrdiff_type_node;
2322 break;
2324 return;
2326 /* 18For every pair of promoted integral types L and R, there exist candi-
2327 date operator functions of the form
2328 LR operator%(L, R);
2329 LR operator&(L, R);
2330 LR operator^(L, R);
2331 LR operator|(L, R);
2332 L operator<<(L, R);
2333 L operator>>(L, R);
2334 where LR is the result of the usual arithmetic conversions between
2335 types L and R. */
2337 case TRUNC_MOD_EXPR:
2338 case BIT_AND_EXPR:
2339 case BIT_IOR_EXPR:
2340 case BIT_XOR_EXPR:
2341 case LSHIFT_EXPR:
2342 case RSHIFT_EXPR:
2343 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2344 break;
2345 return;
2347 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2348 type, VQ is either volatile or empty, and R is a promoted arithmetic
2349 type, there exist candidate operator functions of the form
2350 VQ L& operator=(VQ L&, R);
2351 VQ L& operator*=(VQ L&, R);
2352 VQ L& operator/=(VQ L&, R);
2353 VQ L& operator+=(VQ L&, R);
2354 VQ L& operator-=(VQ L&, R);
2356 20For every pair T, VQ), where T is any type and VQ is either volatile
2357 or empty, there exist candidate operator functions of the form
2358 T*VQ& operator=(T*VQ&, T*);
2360 21For every pair T, VQ), where T is a pointer to member type and VQ is
2361 either volatile or empty, there exist candidate operator functions of
2362 the form
2363 VQ T& operator=(VQ T&, T);
2365 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2366 unqualified complete object type, VQ is either volatile or empty, and
2367 I is a promoted integral type, there exist candidate operator func-
2368 tions of the form
2369 T*VQ& operator+=(T*VQ&, I);
2370 T*VQ& operator-=(T*VQ&, I);
2372 23For every triple L, VQ, R), where L is an integral or enumeration
2373 type, VQ is either volatile or empty, and R is a promoted integral
2374 type, there exist candidate operator functions of the form
2376 VQ L& operator%=(VQ L&, R);
2377 VQ L& operator<<=(VQ L&, R);
2378 VQ L& operator>>=(VQ L&, R);
2379 VQ L& operator&=(VQ L&, R);
2380 VQ L& operator^=(VQ L&, R);
2381 VQ L& operator|=(VQ L&, R); */
2383 case MODIFY_EXPR:
2384 switch (code2)
2386 case PLUS_EXPR:
2387 case MINUS_EXPR:
2388 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2390 type2 = ptrdiff_type_node;
2391 break;
2393 case MULT_EXPR:
2394 case TRUNC_DIV_EXPR:
2395 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2396 break;
2397 return;
2399 case TRUNC_MOD_EXPR:
2400 case BIT_AND_EXPR:
2401 case BIT_IOR_EXPR:
2402 case BIT_XOR_EXPR:
2403 case LSHIFT_EXPR:
2404 case RSHIFT_EXPR:
2405 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2406 break;
2407 return;
2409 case NOP_EXPR:
2410 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2411 break;
2412 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2413 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2414 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
2415 || ((TYPE_PTRMEMFUNC_P (type1)
2416 || TREE_CODE (type1) == POINTER_TYPE)
2417 && null_ptr_cst_p (args[1])))
2419 type2 = type1;
2420 break;
2422 return;
2424 default:
2425 gcc_unreachable ();
2427 type1 = build_reference_type (type1);
2428 break;
2430 case COND_EXPR:
2431 /* [over.built]
2433 For every pair of promoted arithmetic types L and R, there
2434 exist candidate operator functions of the form
2436 LR operator?(bool, L, R);
2438 where LR is the result of the usual arithmetic conversions
2439 between types L and R.
2441 For every type T, where T is a pointer or pointer-to-member
2442 type, there exist candidate operator functions of the form T
2443 operator?(bool, T, T); */
2445 if (promoted_arithmetic_type_p (type1)
2446 && promoted_arithmetic_type_p (type2))
2447 /* That's OK. */
2448 break;
2450 /* Otherwise, the types should be pointers. */
2451 if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
2452 || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
2453 return;
2455 /* We don't check that the two types are the same; the logic
2456 below will actually create two candidates; one in which both
2457 parameter types are TYPE1, and one in which both parameter
2458 types are TYPE2. */
2459 break;
2461 default:
2462 gcc_unreachable ();
2465 /* If we're dealing with two pointer types or two enumeral types,
2466 we need candidates for both of them. */
2467 if (type2 && !same_type_p (type1, type2)
2468 && TREE_CODE (type1) == TREE_CODE (type2)
2469 && (TREE_CODE (type1) == REFERENCE_TYPE
2470 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2471 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
2472 || TYPE_PTRMEMFUNC_P (type1)
2473 || MAYBE_CLASS_TYPE_P (type1)
2474 || TREE_CODE (type1) == ENUMERAL_TYPE))
2476 build_builtin_candidate
2477 (candidates, fnname, type1, type1, args, argtypes, flags);
2478 build_builtin_candidate
2479 (candidates, fnname, type2, type2, args, argtypes, flags);
2480 return;
2483 build_builtin_candidate
2484 (candidates, fnname, type1, type2, args, argtypes, flags);
2487 tree
2488 type_decays_to (tree type)
2490 if (TREE_CODE (type) == ARRAY_TYPE)
2491 return build_pointer_type (TREE_TYPE (type));
2492 if (TREE_CODE (type) == FUNCTION_TYPE)
2493 return build_pointer_type (type);
2494 if (!MAYBE_CLASS_TYPE_P (type))
2495 type = cv_unqualified (type);
2496 return type;
2499 /* There are three conditions of builtin candidates:
2501 1) bool-taking candidates. These are the same regardless of the input.
2502 2) pointer-pair taking candidates. These are generated for each type
2503 one of the input types converts to.
2504 3) arithmetic candidates. According to the standard, we should generate
2505 all of these, but I'm trying not to...
2507 Here we generate a superset of the possible candidates for this particular
2508 case. That is a subset of the full set the standard defines, plus some
2509 other cases which the standard disallows. add_builtin_candidate will
2510 filter out the invalid set. */
2512 static void
2513 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2514 enum tree_code code2, tree fnname, tree *args,
2515 int flags)
2517 int ref1, i;
2518 int enum_p = 0;
2519 tree type, argtypes[3], t;
2520 /* TYPES[i] is the set of possible builtin-operator parameter types
2521 we will consider for the Ith argument. */
2522 VEC(tree,gc) *types[2];
2523 unsigned ix;
2525 for (i = 0; i < 3; ++i)
2527 if (args[i])
2528 argtypes[i] = unlowered_expr_type (args[i]);
2529 else
2530 argtypes[i] = NULL_TREE;
2533 switch (code)
2535 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2536 and VQ is either volatile or empty, there exist candidate operator
2537 functions of the form
2538 VQ T& operator++(VQ T&); */
2540 case POSTINCREMENT_EXPR:
2541 case PREINCREMENT_EXPR:
2542 case POSTDECREMENT_EXPR:
2543 case PREDECREMENT_EXPR:
2544 case MODIFY_EXPR:
2545 ref1 = 1;
2546 break;
2548 /* 24There also exist candidate operator functions of the form
2549 bool operator!(bool);
2550 bool operator&&(bool, bool);
2551 bool operator||(bool, bool); */
2553 case TRUTH_NOT_EXPR:
2554 build_builtin_candidate
2555 (candidates, fnname, boolean_type_node,
2556 NULL_TREE, args, argtypes, flags);
2557 return;
2559 case TRUTH_ORIF_EXPR:
2560 case TRUTH_ANDIF_EXPR:
2561 build_builtin_candidate
2562 (candidates, fnname, boolean_type_node,
2563 boolean_type_node, args, argtypes, flags);
2564 return;
2566 case ADDR_EXPR:
2567 case COMPOUND_EXPR:
2568 case COMPONENT_REF:
2569 return;
2571 case COND_EXPR:
2572 case EQ_EXPR:
2573 case NE_EXPR:
2574 case LT_EXPR:
2575 case LE_EXPR:
2576 case GT_EXPR:
2577 case GE_EXPR:
2578 enum_p = 1;
2579 /* Fall through. */
2581 default:
2582 ref1 = 0;
2585 types[0] = make_tree_vector ();
2586 types[1] = make_tree_vector ();
2588 for (i = 0; i < 2; ++i)
2590 if (! args[i])
2592 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
2594 tree convs;
2596 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2597 return;
2599 convs = lookup_conversions (argtypes[i]);
2601 if (code == COND_EXPR)
2603 if (real_lvalue_p (args[i]))
2604 VEC_safe_push (tree, gc, types[i],
2605 build_reference_type (argtypes[i]));
2607 VEC_safe_push (tree, gc, types[i],
2608 TYPE_MAIN_VARIANT (argtypes[i]));
2611 else if (! convs)
2612 return;
2614 for (; convs; convs = TREE_CHAIN (convs))
2616 type = TREE_TYPE (convs);
2618 if (i == 0 && ref1
2619 && (TREE_CODE (type) != REFERENCE_TYPE
2620 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2621 continue;
2623 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2624 VEC_safe_push (tree, gc, types[i], type);
2626 type = non_reference (type);
2627 if (i != 0 || ! ref1)
2629 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2630 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2631 VEC_safe_push (tree, gc, types[i], type);
2632 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2633 type = type_promotes_to (type);
2636 if (! vec_member (type, types[i]))
2637 VEC_safe_push (tree, gc, types[i], type);
2640 else
2642 if (code == COND_EXPR && real_lvalue_p (args[i]))
2643 VEC_safe_push (tree, gc, types[i],
2644 build_reference_type (argtypes[i]));
2645 type = non_reference (argtypes[i]);
2646 if (i != 0 || ! ref1)
2648 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2649 if (enum_p && UNSCOPED_ENUM_P (type))
2650 VEC_safe_push (tree, gc, types[i], type);
2651 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2652 type = type_promotes_to (type);
2654 VEC_safe_push (tree, gc, types[i], type);
2658 /* Run through the possible parameter types of both arguments,
2659 creating candidates with those parameter types. */
2660 FOR_EACH_VEC_ELT_REVERSE (tree, types[0], ix, t)
2662 unsigned jx;
2663 tree u;
2665 if (!VEC_empty (tree, types[1]))
2666 FOR_EACH_VEC_ELT_REVERSE (tree, types[1], jx, u)
2667 add_builtin_candidate
2668 (candidates, code, code2, fnname, t,
2669 u, args, argtypes, flags);
2670 else
2671 add_builtin_candidate
2672 (candidates, code, code2, fnname, t,
2673 NULL_TREE, args, argtypes, flags);
2676 release_tree_vector (types[0]);
2677 release_tree_vector (types[1]);
2681 /* If TMPL can be successfully instantiated as indicated by
2682 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2684 TMPL is the template. EXPLICIT_TARGS are any explicit template
2685 arguments. ARGLIST is the arguments provided at the call-site.
2686 This does not change ARGLIST. The RETURN_TYPE is the desired type
2687 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
2688 as for add_function_candidate. If an OBJ is supplied, FLAGS and
2689 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
2691 static struct z_candidate*
2692 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2693 tree ctype, tree explicit_targs, tree first_arg,
2694 const VEC(tree,gc) *arglist, tree return_type,
2695 tree access_path, tree conversion_path,
2696 int flags, tree obj, unification_kind_t strict)
2698 int ntparms = DECL_NTPARMS (tmpl);
2699 tree targs = make_tree_vec (ntparms);
2700 unsigned int len = VEC_length (tree, arglist);
2701 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
2702 unsigned int skip_without_in_chrg = 0;
2703 tree first_arg_without_in_chrg = first_arg;
2704 tree *args_without_in_chrg;
2705 unsigned int nargs_without_in_chrg;
2706 unsigned int ia, ix;
2707 tree arg;
2708 struct z_candidate *cand;
2709 int i;
2710 tree fn;
2711 struct rejection_reason *reason = NULL;
2713 /* We don't do deduction on the in-charge parameter, the VTT
2714 parameter or 'this'. */
2715 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2717 if (first_arg_without_in_chrg != NULL_TREE)
2718 first_arg_without_in_chrg = NULL_TREE;
2719 else
2720 ++skip_without_in_chrg;
2723 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2724 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2725 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
2727 if (first_arg_without_in_chrg != NULL_TREE)
2728 first_arg_without_in_chrg = NULL_TREE;
2729 else
2730 ++skip_without_in_chrg;
2733 if (len < skip_without_in_chrg)
2734 return NULL;
2736 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
2737 + (len - skip_without_in_chrg));
2738 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
2739 ia = 0;
2740 if (first_arg_without_in_chrg != NULL_TREE)
2742 args_without_in_chrg[ia] = first_arg_without_in_chrg;
2743 ++ia;
2745 for (ix = skip_without_in_chrg;
2746 VEC_iterate (tree, arglist, ix, arg);
2747 ++ix)
2749 args_without_in_chrg[ia] = arg;
2750 ++ia;
2752 gcc_assert (ia == nargs_without_in_chrg);
2754 i = fn_type_unification (tmpl, explicit_targs, targs,
2755 args_without_in_chrg,
2756 nargs_without_in_chrg,
2757 return_type, strict, flags);
2759 if (i != 0)
2760 goto fail;
2762 fn = instantiate_template (tmpl, targs, tf_none);
2763 if (fn == error_mark_node)
2764 goto fail;
2766 /* In [class.copy]:
2768 A member function template is never instantiated to perform the
2769 copy of a class object to an object of its class type.
2771 It's a little unclear what this means; the standard explicitly
2772 does allow a template to be used to copy a class. For example,
2775 struct A {
2776 A(A&);
2777 template <class T> A(const T&);
2779 const A f ();
2780 void g () { A a (f ()); }
2782 the member template will be used to make the copy. The section
2783 quoted above appears in the paragraph that forbids constructors
2784 whose only parameter is (a possibly cv-qualified variant of) the
2785 class type, and a logical interpretation is that the intent was
2786 to forbid the instantiation of member templates which would then
2787 have that form. */
2788 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
2790 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2791 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2792 ctype))
2793 goto fail;
2796 if (obj != NULL_TREE)
2797 /* Aha, this is a conversion function. */
2798 cand = add_conv_candidate (candidates, fn, obj, first_arg, arglist,
2799 access_path, conversion_path);
2800 else
2801 cand = add_function_candidate (candidates, fn, ctype,
2802 first_arg, arglist, access_path,
2803 conversion_path, flags);
2804 if (DECL_TI_TEMPLATE (fn) != tmpl)
2805 /* This situation can occur if a member template of a template
2806 class is specialized. Then, instantiate_template might return
2807 an instantiation of the specialization, in which case the
2808 DECL_TI_TEMPLATE field will point at the original
2809 specialization. For example:
2811 template <class T> struct S { template <class U> void f(U);
2812 template <> void f(int) {}; };
2813 S<double> sd;
2814 sd.f(3);
2816 Here, TMPL will be template <class U> S<double>::f(U).
2817 And, instantiate template will give us the specialization
2818 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2819 for this will point at template <class T> template <> S<T>::f(int),
2820 so that we can find the definition. For the purposes of
2821 overload resolution, however, we want the original TMPL. */
2822 cand->template_decl = build_template_info (tmpl, targs);
2823 else
2824 cand->template_decl = DECL_TEMPLATE_INFO (fn);
2825 cand->explicit_targs = explicit_targs;
2827 return cand;
2828 fail:
2829 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
2830 access_path, conversion_path, 0, reason);
2834 static struct z_candidate *
2835 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2836 tree explicit_targs, tree first_arg,
2837 const VEC(tree,gc) *arglist, tree return_type,
2838 tree access_path, tree conversion_path, int flags,
2839 unification_kind_t strict)
2841 return
2842 add_template_candidate_real (candidates, tmpl, ctype,
2843 explicit_targs, first_arg, arglist,
2844 return_type, access_path, conversion_path,
2845 flags, NULL_TREE, strict);
2849 static struct z_candidate *
2850 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2851 tree obj, tree first_arg,
2852 const VEC(tree,gc) *arglist,
2853 tree return_type, tree access_path,
2854 tree conversion_path)
2856 return
2857 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2858 first_arg, arglist, return_type, access_path,
2859 conversion_path, 0, obj, DEDUCE_CONV);
2862 /* The CANDS are the set of candidates that were considered for
2863 overload resolution. Return the set of viable candidates, or CANDS
2864 if none are viable. If any of the candidates were viable, set
2865 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
2866 considered viable only if it is strictly viable. */
2868 static struct z_candidate*
2869 splice_viable (struct z_candidate *cands,
2870 bool strict_p,
2871 bool *any_viable_p)
2873 struct z_candidate *viable;
2874 struct z_candidate **last_viable;
2875 struct z_candidate **cand;
2877 viable = NULL;
2878 last_viable = &viable;
2879 *any_viable_p = false;
2881 cand = &cands;
2882 while (*cand)
2884 struct z_candidate *c = *cand;
2885 if (strict_p ? c->viable == 1 : c->viable)
2887 *last_viable = c;
2888 *cand = c->next;
2889 c->next = NULL;
2890 last_viable = &c->next;
2891 *any_viable_p = true;
2893 else
2894 cand = &c->next;
2897 return viable ? viable : cands;
2900 static bool
2901 any_strictly_viable (struct z_candidate *cands)
2903 for (; cands; cands = cands->next)
2904 if (cands->viable == 1)
2905 return true;
2906 return false;
2909 /* OBJ is being used in an expression like "OBJ.f (...)". In other
2910 words, it is about to become the "this" pointer for a member
2911 function call. Take the address of the object. */
2913 static tree
2914 build_this (tree obj)
2916 /* In a template, we are only concerned about the type of the
2917 expression, so we can take a shortcut. */
2918 if (processing_template_decl)
2919 return build_address (obj);
2921 return cp_build_addr_expr (obj, tf_warning_or_error);
2924 /* Returns true iff functions are equivalent. Equivalent functions are
2925 not '==' only if one is a function-local extern function or if
2926 both are extern "C". */
2928 static inline int
2929 equal_functions (tree fn1, tree fn2)
2931 if (TREE_CODE (fn1) != TREE_CODE (fn2))
2932 return 0;
2933 if (TREE_CODE (fn1) == TEMPLATE_DECL)
2934 return fn1 == fn2;
2935 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2936 || DECL_EXTERN_C_FUNCTION_P (fn1))
2937 return decls_match (fn1, fn2);
2938 return fn1 == fn2;
2941 /* Print information about a candidate being rejected due to INFO. */
2943 static void
2944 print_conversion_rejection (location_t loc, struct conversion_info *info)
2946 if (info->n_arg == -1)
2947 /* Conversion of implicit `this' argument failed. */
2948 inform (loc, " no known conversion for implicit "
2949 "%<this%> parameter from %qT to %qT",
2950 info->from_type, info->to_type);
2951 else
2952 inform (loc, " no known conversion for argument %d from %qT to %qT",
2953 info->n_arg+1, info->from_type, info->to_type);
2956 /* Print information about one overload candidate CANDIDATE. MSGSTR
2957 is the text to print before the candidate itself.
2959 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2960 to have been run through gettext by the caller. This wart makes
2961 life simpler in print_z_candidates and for the translators. */
2963 static void
2964 print_z_candidate (const char *msgstr, struct z_candidate *candidate)
2966 const char *msg = (msgstr == NULL
2967 ? ""
2968 : ACONCAT ((msgstr, " ", NULL)));
2969 location_t loc = location_of (candidate->fn);
2971 if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2973 if (candidate->num_convs == 3)
2974 inform (input_location, "%s%D(%T, %T, %T) <built-in>", msg, candidate->fn,
2975 candidate->convs[0]->type,
2976 candidate->convs[1]->type,
2977 candidate->convs[2]->type);
2978 else if (candidate->num_convs == 2)
2979 inform (input_location, "%s%D(%T, %T) <built-in>", msg, candidate->fn,
2980 candidate->convs[0]->type,
2981 candidate->convs[1]->type);
2982 else
2983 inform (input_location, "%s%D(%T) <built-in>", msg, candidate->fn,
2984 candidate->convs[0]->type);
2986 else if (TYPE_P (candidate->fn))
2987 inform (input_location, "%s%T <conversion>", msg, candidate->fn);
2988 else if (candidate->viable == -1)
2989 inform (loc, "%s%#D <near match>", msg, candidate->fn);
2990 else if (DECL_DELETED_FN (STRIP_TEMPLATE (candidate->fn)))
2991 inform (loc, "%s%#D <deleted>", msg, candidate->fn);
2992 else
2993 inform (loc, "%s%#D", msg, candidate->fn);
2994 /* Give the user some information about why this candidate failed. */
2995 if (candidate->reason != NULL)
2997 struct rejection_reason *r = candidate->reason;
2999 switch (r->code)
3001 case rr_arity:
3002 inform_n (loc, r->u.arity.expected,
3003 " candidate expects %d argument, %d provided",
3004 " candidate expects %d arguments, %d provided",
3005 r->u.arity.expected, r->u.arity.actual);
3006 break;
3007 case rr_arg_conversion:
3008 print_conversion_rejection (loc, &r->u.conversion);
3009 break;
3010 case rr_bad_arg_conversion:
3011 print_conversion_rejection (loc, &r->u.bad_conversion);
3012 break;
3013 case rr_none:
3014 default:
3015 /* This candidate didn't have any issues or we failed to
3016 handle a particular code. Either way... */
3017 gcc_unreachable ();
3022 static void
3023 print_z_candidates (location_t loc, struct z_candidate *candidates)
3025 struct z_candidate *cand1;
3026 struct z_candidate **cand2;
3027 int n_candidates;
3029 if (!candidates)
3030 return;
3032 /* Remove non-viable deleted candidates. */
3033 cand1 = candidates;
3034 for (cand2 = &cand1; *cand2; )
3036 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
3037 && !(*cand2)->viable
3038 && DECL_DELETED_FN ((*cand2)->fn))
3039 *cand2 = (*cand2)->next;
3040 else
3041 cand2 = &(*cand2)->next;
3043 /* ...if there are any non-deleted ones. */
3044 if (cand1)
3045 candidates = cand1;
3047 /* There may be duplicates in the set of candidates. We put off
3048 checking this condition as long as possible, since we have no way
3049 to eliminate duplicates from a set of functions in less than n^2
3050 time. Now we are about to emit an error message, so it is more
3051 permissible to go slowly. */
3052 for (cand1 = candidates; cand1; cand1 = cand1->next)
3054 tree fn = cand1->fn;
3055 /* Skip builtin candidates and conversion functions. */
3056 if (!DECL_P (fn))
3057 continue;
3058 cand2 = &cand1->next;
3059 while (*cand2)
3061 if (DECL_P ((*cand2)->fn)
3062 && equal_functions (fn, (*cand2)->fn))
3063 *cand2 = (*cand2)->next;
3064 else
3065 cand2 = &(*cand2)->next;
3069 for (n_candidates = 0, cand1 = candidates; cand1; cand1 = cand1->next)
3070 n_candidates++;
3072 inform_n (loc, n_candidates, "candidate is:", "candidates are:");
3073 for (; candidates; candidates = candidates->next)
3074 print_z_candidate (NULL, candidates);
3077 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3078 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3079 the result of the conversion function to convert it to the final
3080 desired type. Merge the two sequences into a single sequence,
3081 and return the merged sequence. */
3083 static conversion *
3084 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
3086 conversion **t;
3088 gcc_assert (user_seq->kind == ck_user);
3090 /* Find the end of the second conversion sequence. */
3091 t = &(std_seq);
3092 while ((*t)->kind != ck_identity)
3093 t = &((*t)->u.next);
3095 /* Replace the identity conversion with the user conversion
3096 sequence. */
3097 *t = user_seq;
3099 /* The entire sequence is a user-conversion sequence. */
3100 std_seq->user_conv_p = true;
3102 return std_seq;
3105 /* Handle overload resolution for initializing an object of class type from
3106 an initializer list. First we look for a suitable constructor that
3107 takes a std::initializer_list; if we don't find one, we then look for a
3108 non-list constructor.
3110 Parameters are as for add_candidates, except that the arguments are in
3111 the form of a CONSTRUCTOR (the initializer list) rather than a VEC, and
3112 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3114 static void
3115 add_list_candidates (tree fns, tree first_arg,
3116 tree init_list, tree totype,
3117 tree explicit_targs, bool template_only,
3118 tree conversion_path, tree access_path,
3119 int flags,
3120 struct z_candidate **candidates)
3122 VEC(tree,gc) *args;
3124 gcc_assert (*candidates == NULL);
3126 /* For list-initialization we consider explicit constructors, but
3127 give an error if one is selected. */
3128 flags &= ~LOOKUP_ONLYCONVERTING;
3129 /* And we don't allow narrowing conversions. We also use this flag to
3130 avoid the copy constructor call for copy-list-initialization. */
3131 flags |= LOOKUP_NO_NARROWING;
3133 /* Always use the default constructor if the list is empty (DR 990). */
3134 if (CONSTRUCTOR_NELTS (init_list) == 0
3135 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
3137 /* If the class has a list ctor, try passing the list as a single
3138 argument first, but only consider list ctors. */
3139 else if (TYPE_HAS_LIST_CTOR (totype))
3141 flags |= LOOKUP_LIST_ONLY;
3142 args = make_tree_vector_single (init_list);
3143 add_candidates (fns, first_arg, args, NULL_TREE,
3144 explicit_targs, template_only, conversion_path,
3145 access_path, flags, candidates);
3146 if (any_strictly_viable (*candidates))
3147 return;
3150 args = ctor_to_vec (init_list);
3152 /* We aren't looking for list-ctors anymore. */
3153 flags &= ~LOOKUP_LIST_ONLY;
3154 /* We allow more user-defined conversions within an init-list. */
3155 flags &= ~LOOKUP_NO_CONVERSION;
3156 /* But not for the copy ctor. */
3157 flags |= LOOKUP_NO_COPY_CTOR_CONVERSION;
3159 add_candidates (fns, first_arg, args, NULL_TREE,
3160 explicit_targs, template_only, conversion_path,
3161 access_path, flags, candidates);
3164 /* Returns the best overload candidate to perform the requested
3165 conversion. This function is used for three the overloading situations
3166 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3167 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
3168 per [dcl.init.ref], so we ignore temporary bindings. */
3170 static struct z_candidate *
3171 build_user_type_conversion_1 (tree totype, tree expr, int flags)
3173 struct z_candidate *candidates, *cand;
3174 tree fromtype = TREE_TYPE (expr);
3175 tree ctors = NULL_TREE;
3176 tree conv_fns = NULL_TREE;
3177 conversion *conv = NULL;
3178 tree first_arg = NULL_TREE;
3179 VEC(tree,gc) *args = NULL;
3180 bool any_viable_p;
3181 int convflags;
3183 /* We represent conversion within a hierarchy using RVALUE_CONV and
3184 BASE_CONV, as specified by [over.best.ics]; these become plain
3185 constructor calls, as specified in [dcl.init]. */
3186 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
3187 || !DERIVED_FROM_P (totype, fromtype));
3189 if (MAYBE_CLASS_TYPE_P (totype))
3190 ctors = lookup_fnfields (totype, complete_ctor_identifier, 0);
3192 if (MAYBE_CLASS_TYPE_P (fromtype))
3194 tree to_nonref = non_reference (totype);
3195 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
3196 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
3197 && DERIVED_FROM_P (to_nonref, fromtype)))
3199 /* [class.conv.fct] A conversion function is never used to
3200 convert a (possibly cv-qualified) object to the (possibly
3201 cv-qualified) same object type (or a reference to it), to a
3202 (possibly cv-qualified) base class of that type (or a
3203 reference to it)... */
3205 else
3206 conv_fns = lookup_conversions (fromtype);
3209 candidates = 0;
3210 flags |= LOOKUP_NO_CONVERSION;
3211 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3212 flags |= LOOKUP_NO_NARROWING;
3214 /* It's OK to bind a temporary for converting constructor arguments, but
3215 not in converting the return value of a conversion operator. */
3216 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION);
3217 flags &= ~LOOKUP_NO_TEMP_BIND;
3219 if (ctors)
3221 int ctorflags = flags;
3222 ctors = BASELINK_FUNCTIONS (ctors);
3224 first_arg = build_int_cst (build_pointer_type (totype), 0);
3226 /* We should never try to call the abstract or base constructor
3227 from here. */
3228 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
3229 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)));
3231 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3233 /* List-initialization. */
3234 add_list_candidates (ctors, first_arg, expr, totype, NULL_TREE,
3235 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
3236 ctorflags, &candidates);
3238 else
3240 args = make_tree_vector_single (expr);
3241 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
3242 TYPE_BINFO (totype), TYPE_BINFO (totype),
3243 ctorflags, &candidates);
3246 for (cand = candidates; cand; cand = cand->next)
3248 cand->second_conv = build_identity_conv (totype, NULL_TREE);
3250 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
3251 set, then this is copy-initialization. In that case, "The
3252 result of the call is then used to direct-initialize the
3253 object that is the destination of the copy-initialization."
3254 [dcl.init]
3256 We represent this in the conversion sequence with an
3257 rvalue conversion, which means a constructor call. */
3258 if (TREE_CODE (totype) != REFERENCE_TYPE
3259 && !(convflags & LOOKUP_NO_TEMP_BIND))
3260 cand->second_conv
3261 = build_conv (ck_rvalue, totype, cand->second_conv);
3265 if (conv_fns)
3266 first_arg = build_this (expr);
3268 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
3270 tree conversion_path = TREE_PURPOSE (conv_fns);
3271 struct z_candidate *old_candidates;
3273 /* If we are called to convert to a reference type, we are trying to
3274 find an lvalue binding, so don't even consider temporaries. If
3275 we don't find an lvalue binding, the caller will try again to
3276 look for a temporary binding. */
3277 if (TREE_CODE (totype) == REFERENCE_TYPE)
3278 convflags |= LOOKUP_NO_TEMP_BIND;
3280 old_candidates = candidates;
3281 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3282 NULL_TREE, false,
3283 conversion_path, TYPE_BINFO (fromtype),
3284 flags, &candidates);
3286 for (cand = candidates; cand != old_candidates; cand = cand->next)
3288 conversion *ics
3289 = implicit_conversion (totype,
3290 TREE_TYPE (TREE_TYPE (cand->fn)),
3292 /*c_cast_p=*/false, convflags);
3294 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3295 copy-initialization. In that case, "The result of the
3296 call is then used to direct-initialize the object that is
3297 the destination of the copy-initialization." [dcl.init]
3299 We represent this in the conversion sequence with an
3300 rvalue conversion, which means a constructor call. But
3301 don't add a second rvalue conversion if there's already
3302 one there. Which there really shouldn't be, but it's
3303 harmless since we'd add it here anyway. */
3304 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3305 && !(convflags & LOOKUP_NO_TEMP_BIND))
3306 ics = build_conv (ck_rvalue, totype, ics);
3308 cand->second_conv = ics;
3310 if (!ics)
3312 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
3313 cand->viable = 0;
3314 cand->reason = arg_conversion_rejection (NULL_TREE, -1,
3315 rettype, totype);
3317 else if (cand->viable == 1 && ics->bad_p)
3319 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
3320 cand->viable = -1;
3321 cand->reason
3322 = bad_arg_conversion_rejection (NULL_TREE, -1,
3323 rettype, totype);
3328 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3329 if (!any_viable_p)
3330 return NULL;
3332 cand = tourney (candidates);
3333 if (cand == 0)
3335 if (flags & LOOKUP_COMPLAIN)
3337 error ("conversion from %qT to %qT is ambiguous",
3338 fromtype, totype);
3339 print_z_candidates (location_of (expr), candidates);
3342 cand = candidates; /* any one will do */
3343 cand->second_conv = build_ambiguous_conv (totype, expr);
3344 cand->second_conv->user_conv_p = true;
3345 if (!any_strictly_viable (candidates))
3346 cand->second_conv->bad_p = true;
3347 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3348 ambiguous conversion is no worse than another user-defined
3349 conversion. */
3351 return cand;
3354 /* Build the user conversion sequence. */
3355 conv = build_conv
3356 (ck_user,
3357 (DECL_CONSTRUCTOR_P (cand->fn)
3358 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
3359 build_identity_conv (TREE_TYPE (expr), expr));
3360 conv->cand = cand;
3362 /* Remember that this was a list-initialization. */
3363 if (flags & LOOKUP_NO_NARROWING)
3364 conv->check_narrowing = true;
3366 /* Combine it with the second conversion sequence. */
3367 cand->second_conv = merge_conversion_sequences (conv,
3368 cand->second_conv);
3370 if (cand->viable == -1)
3371 cand->second_conv->bad_p = true;
3373 return cand;
3376 tree
3377 build_user_type_conversion (tree totype, tree expr, int flags)
3379 struct z_candidate *cand
3380 = build_user_type_conversion_1 (totype, expr, flags);
3382 if (cand)
3384 if (cand->second_conv->kind == ck_ambig)
3385 return error_mark_node;
3386 expr = convert_like (cand->second_conv, expr, tf_warning_or_error);
3387 return convert_from_reference (expr);
3389 return NULL_TREE;
3392 /* Subroutine of convert_nontype_argument.
3394 EXPR is an argument for a template non-type parameter of integral or
3395 enumeration type. Do any necessary conversions (that are permitted for
3396 non-type arguments) to convert it to the parameter type.
3398 If conversion is successful, returns the converted expression;
3399 otherwise, returns error_mark_node. */
3401 tree
3402 build_integral_nontype_arg_conv (tree type, tree expr, tsubst_flags_t complain)
3404 conversion *conv;
3405 void *p;
3406 tree t;
3408 if (error_operand_p (expr))
3409 return error_mark_node;
3411 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
3413 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3414 p = conversion_obstack_alloc (0);
3416 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
3417 /*c_cast_p=*/false,
3418 LOOKUP_IMPLICIT);
3420 /* for a non-type template-parameter of integral or
3421 enumeration type, integral promotions (4.5) and integral
3422 conversions (4.7) are applied. */
3423 /* It should be sufficient to check the outermost conversion step, since
3424 there are no qualification conversions to integer type. */
3425 if (conv)
3426 switch (conv->kind)
3428 /* A conversion function is OK. If it isn't constexpr, we'll
3429 complain later that the argument isn't constant. */
3430 case ck_user:
3431 /* The lvalue-to-rvalue conversion is OK. */
3432 case ck_rvalue:
3433 case ck_identity:
3434 break;
3436 case ck_std:
3437 t = conv->u.next->type;
3438 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t))
3439 break;
3441 if (complain & tf_error)
3442 error ("conversion from %qT to %qT not considered for "
3443 "non-type template argument", t, type);
3444 /* and fall through. */
3446 default:
3447 conv = NULL;
3448 break;
3451 if (conv)
3452 expr = convert_like (conv, expr, complain);
3453 else
3454 expr = error_mark_node;
3456 /* Free all the conversions we allocated. */
3457 obstack_free (&conversion_obstack, p);
3459 return expr;
3462 /* Do any initial processing on the arguments to a function call. */
3464 static VEC(tree,gc) *
3465 resolve_args (VEC(tree,gc) *args)
3467 unsigned int ix;
3468 tree arg;
3470 FOR_EACH_VEC_ELT (tree, args, ix, arg)
3472 if (error_operand_p (arg))
3473 return NULL;
3474 else if (VOID_TYPE_P (TREE_TYPE (arg)))
3476 error ("invalid use of void expression");
3477 return NULL;
3479 else if (invalid_nonstatic_memfn_p (arg, tf_warning_or_error))
3480 return NULL;
3482 return args;
3485 /* Perform overload resolution on FN, which is called with the ARGS.
3487 Return the candidate function selected by overload resolution, or
3488 NULL if the event that overload resolution failed. In the case
3489 that overload resolution fails, *CANDIDATES will be the set of
3490 candidates considered, and ANY_VIABLE_P will be set to true or
3491 false to indicate whether or not any of the candidates were
3492 viable.
3494 The ARGS should already have gone through RESOLVE_ARGS before this
3495 function is called. */
3497 static struct z_candidate *
3498 perform_overload_resolution (tree fn,
3499 const VEC(tree,gc) *args,
3500 struct z_candidate **candidates,
3501 bool *any_viable_p)
3503 struct z_candidate *cand;
3504 tree explicit_targs = NULL_TREE;
3505 int template_only = 0;
3507 *candidates = NULL;
3508 *any_viable_p = true;
3510 /* Check FN. */
3511 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
3512 || TREE_CODE (fn) == TEMPLATE_DECL
3513 || TREE_CODE (fn) == OVERLOAD
3514 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
3516 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3518 explicit_targs = TREE_OPERAND (fn, 1);
3519 fn = TREE_OPERAND (fn, 0);
3520 template_only = 1;
3523 /* Add the various candidate functions. */
3524 add_candidates (fn, NULL_TREE, args, NULL_TREE,
3525 explicit_targs, template_only,
3526 /*conversion_path=*/NULL_TREE,
3527 /*access_path=*/NULL_TREE,
3528 LOOKUP_NORMAL,
3529 candidates);
3531 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
3532 if (!*any_viable_p)
3533 return NULL;
3535 cand = tourney (*candidates);
3536 return cand;
3539 /* Print an error message about being unable to build a call to FN with
3540 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
3541 be located; CANDIDATES is a possibly empty list of such
3542 functions. */
3544 static void
3545 print_error_for_call_failure (tree fn, VEC(tree,gc) *args, bool any_viable_p,
3546 struct z_candidate *candidates)
3548 tree name = DECL_NAME (OVL_CURRENT (fn));
3549 location_t loc = location_of (name);
3551 if (!any_viable_p)
3552 error_at (loc, "no matching function for call to %<%D(%A)%>",
3553 name, build_tree_list_vec (args));
3554 else
3555 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
3556 name, build_tree_list_vec (args));
3557 if (candidates)
3558 print_z_candidates (loc, candidates);
3561 /* Return an expression for a call to FN (a namespace-scope function,
3562 or a static member function) with the ARGS. This may change
3563 ARGS. */
3565 tree
3566 build_new_function_call (tree fn, VEC(tree,gc) **args, bool koenig_p,
3567 tsubst_flags_t complain)
3569 struct z_candidate *candidates, *cand;
3570 bool any_viable_p;
3571 void *p;
3572 tree result;
3574 if (args != NULL && *args != NULL)
3576 *args = resolve_args (*args);
3577 if (*args == NULL)
3578 return error_mark_node;
3581 /* If this function was found without using argument dependent
3582 lookup, then we want to ignore any undeclared friend
3583 functions. */
3584 if (!koenig_p)
3586 tree orig_fn = fn;
3588 fn = remove_hidden_names (fn);
3589 if (!fn)
3591 if (complain & tf_error)
3592 print_error_for_call_failure (orig_fn, *args, false, NULL);
3593 return error_mark_node;
3597 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3598 p = conversion_obstack_alloc (0);
3600 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p);
3602 if (!cand)
3604 if (complain & tf_error)
3606 if (!any_viable_p && candidates && ! candidates->next
3607 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
3608 return cp_build_function_call_vec (candidates->fn, args, complain);
3609 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3610 fn = TREE_OPERAND (fn, 0);
3611 print_error_for_call_failure (fn, *args, any_viable_p, candidates);
3613 result = error_mark_node;
3615 else
3616 result = build_over_call (cand, LOOKUP_NORMAL, complain);
3618 /* Free all the conversions we allocated. */
3619 obstack_free (&conversion_obstack, p);
3621 return result;
3624 /* Build a call to a global operator new. FNNAME is the name of the
3625 operator (either "operator new" or "operator new[]") and ARGS are
3626 the arguments provided. This may change ARGS. *SIZE points to the
3627 total number of bytes required by the allocation, and is updated if
3628 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
3629 be used. If this function determines that no cookie should be
3630 used, after all, *COOKIE_SIZE is set to NULL_TREE. If FN is
3631 non-NULL, it will be set, upon return, to the allocation function
3632 called. */
3634 tree
3635 build_operator_new_call (tree fnname, VEC(tree,gc) **args,
3636 tree *size, tree *cookie_size,
3637 tree *fn)
3639 tree fns;
3640 struct z_candidate *candidates;
3641 struct z_candidate *cand;
3642 bool any_viable_p;
3644 if (fn)
3645 *fn = NULL_TREE;
3646 VEC_safe_insert (tree, gc, *args, 0, *size);
3647 *args = resolve_args (*args);
3648 if (*args == NULL)
3649 return error_mark_node;
3651 /* Based on:
3653 [expr.new]
3655 If this lookup fails to find the name, or if the allocated type
3656 is not a class type, the allocation function's name is looked
3657 up in the global scope.
3659 we disregard block-scope declarations of "operator new". */
3660 fns = lookup_function_nonclass (fnname, *args, /*block_p=*/false);
3662 /* Figure out what function is being called. */
3663 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p);
3665 /* If no suitable function could be found, issue an error message
3666 and give up. */
3667 if (!cand)
3669 print_error_for_call_failure (fns, *args, any_viable_p, candidates);
3670 return error_mark_node;
3673 /* If a cookie is required, add some extra space. Whether
3674 or not a cookie is required cannot be determined until
3675 after we know which function was called. */
3676 if (*cookie_size)
3678 bool use_cookie = true;
3679 if (!abi_version_at_least (2))
3681 /* In G++ 3.2, the check was implemented incorrectly; it
3682 looked at the placement expression, rather than the
3683 type of the function. */
3684 if (VEC_length (tree, *args) == 2
3685 && same_type_p (TREE_TYPE (VEC_index (tree, *args, 1)),
3686 ptr_type_node))
3687 use_cookie = false;
3689 else
3691 tree arg_types;
3693 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
3694 /* Skip the size_t parameter. */
3695 arg_types = TREE_CHAIN (arg_types);
3696 /* Check the remaining parameters (if any). */
3697 if (arg_types
3698 && TREE_CHAIN (arg_types) == void_list_node
3699 && same_type_p (TREE_VALUE (arg_types),
3700 ptr_type_node))
3701 use_cookie = false;
3703 /* If we need a cookie, adjust the number of bytes allocated. */
3704 if (use_cookie)
3706 /* Update the total size. */
3707 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
3708 /* Update the argument list to reflect the adjusted size. */
3709 VEC_replace (tree, *args, 0, *size);
3711 else
3712 *cookie_size = NULL_TREE;
3715 /* Tell our caller which function we decided to call. */
3716 if (fn)
3717 *fn = cand->fn;
3719 /* Build the CALL_EXPR. */
3720 return build_over_call (cand, LOOKUP_NORMAL, tf_warning_or_error);
3723 /* Build a new call to operator(). This may change ARGS. */
3725 tree
3726 build_op_call (tree obj, VEC(tree,gc) **args, tsubst_flags_t complain)
3728 struct z_candidate *candidates = 0, *cand;
3729 tree fns, convs, first_mem_arg = NULL_TREE;
3730 tree type = TREE_TYPE (obj);
3731 bool any_viable_p;
3732 tree result = NULL_TREE;
3733 void *p;
3735 if (error_operand_p (obj))
3736 return error_mark_node;
3738 obj = prep_operand (obj);
3740 if (TYPE_PTRMEMFUNC_P (type))
3742 if (complain & tf_error)
3743 /* It's no good looking for an overloaded operator() on a
3744 pointer-to-member-function. */
3745 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
3746 return error_mark_node;
3749 if (TYPE_BINFO (type))
3751 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
3752 if (fns == error_mark_node)
3753 return error_mark_node;
3755 else
3756 fns = NULL_TREE;
3758 if (args != NULL && *args != NULL)
3760 *args = resolve_args (*args);
3761 if (*args == NULL)
3762 return error_mark_node;
3765 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3766 p = conversion_obstack_alloc (0);
3768 if (fns)
3770 first_mem_arg = build_this (obj);
3772 add_candidates (BASELINK_FUNCTIONS (fns),
3773 first_mem_arg, *args, NULL_TREE,
3774 NULL_TREE, false,
3775 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
3776 LOOKUP_NORMAL, &candidates);
3779 convs = lookup_conversions (type);
3781 for (; convs; convs = TREE_CHAIN (convs))
3783 tree fns = TREE_VALUE (convs);
3784 tree totype = TREE_TYPE (convs);
3786 if ((TREE_CODE (totype) == POINTER_TYPE
3787 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
3788 || (TREE_CODE (totype) == REFERENCE_TYPE
3789 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
3790 || (TREE_CODE (totype) == REFERENCE_TYPE
3791 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
3792 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
3793 for (; fns; fns = OVL_NEXT (fns))
3795 tree fn = OVL_CURRENT (fns);
3797 if (DECL_NONCONVERTING_P (fn))
3798 continue;
3800 if (TREE_CODE (fn) == TEMPLATE_DECL)
3801 add_template_conv_candidate
3802 (&candidates, fn, obj, NULL_TREE, *args, totype,
3803 /*access_path=*/NULL_TREE,
3804 /*conversion_path=*/NULL_TREE);
3805 else
3806 add_conv_candidate (&candidates, fn, obj, NULL_TREE,
3807 *args, /*conversion_path=*/NULL_TREE,
3808 /*access_path=*/NULL_TREE);
3812 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3813 if (!any_viable_p)
3815 if (complain & tf_error)
3817 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
3818 build_tree_list_vec (*args));
3819 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
3821 result = error_mark_node;
3823 else
3825 cand = tourney (candidates);
3826 if (cand == 0)
3828 if (complain & tf_error)
3830 error ("call of %<(%T) (%A)%> is ambiguous",
3831 TREE_TYPE (obj), build_tree_list_vec (*args));
3832 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
3834 result = error_mark_node;
3836 /* Since cand->fn will be a type, not a function, for a conversion
3837 function, we must be careful not to unconditionally look at
3838 DECL_NAME here. */
3839 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
3840 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
3841 result = build_over_call (cand, LOOKUP_NORMAL, complain);
3842 else
3844 obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1,
3845 complain);
3846 obj = convert_from_reference (obj);
3847 result = cp_build_function_call_vec (obj, args, complain);
3851 /* Free all the conversions we allocated. */
3852 obstack_free (&conversion_obstack, p);
3854 return result;
3857 static void
3858 op_error (enum tree_code code, enum tree_code code2,
3859 tree arg1, tree arg2, tree arg3, bool match)
3861 const char *opname;
3863 if (code == MODIFY_EXPR)
3864 opname = assignment_operator_name_info[code2].name;
3865 else
3866 opname = operator_name_info[code].name;
3868 switch (code)
3870 case COND_EXPR:
3871 if (match)
3872 error ("ambiguous overload for ternary %<operator?:%> "
3873 "in %<%E ? %E : %E%>", arg1, arg2, arg3);
3874 else
3875 error ("no match for ternary %<operator?:%> "
3876 "in %<%E ? %E : %E%>", arg1, arg2, arg3);
3877 break;
3879 case POSTINCREMENT_EXPR:
3880 case POSTDECREMENT_EXPR:
3881 if (match)
3882 error ("ambiguous overload for %<operator%s%> in %<%E%s%>",
3883 opname, arg1, opname);
3884 else
3885 error ("no match for %<operator%s%> in %<%E%s%>",
3886 opname, arg1, opname);
3887 break;
3889 case ARRAY_REF:
3890 if (match)
3891 error ("ambiguous overload for %<operator[]%> in %<%E[%E]%>",
3892 arg1, arg2);
3893 else
3894 error ("no match for %<operator[]%> in %<%E[%E]%>",
3895 arg1, arg2);
3896 break;
3898 case REALPART_EXPR:
3899 case IMAGPART_EXPR:
3900 if (match)
3901 error ("ambiguous overload for %qs in %<%s %E%>",
3902 opname, opname, arg1);
3903 else
3904 error ("no match for %qs in %<%s %E%>",
3905 opname, opname, arg1);
3906 break;
3908 default:
3909 if (arg2)
3910 if (match)
3911 error ("ambiguous overload for %<operator%s%> in %<%E %s %E%>",
3912 opname, arg1, opname, arg2);
3913 else
3914 error ("no match for %<operator%s%> in %<%E %s %E%>",
3915 opname, arg1, opname, arg2);
3916 else
3917 if (match)
3918 error ("ambiguous overload for %<operator%s%> in %<%s%E%>",
3919 opname, opname, arg1);
3920 else
3921 error ("no match for %<operator%s%> in %<%s%E%>",
3922 opname, opname, arg1);
3923 break;
3927 /* Return the implicit conversion sequence that could be used to
3928 convert E1 to E2 in [expr.cond]. */
3930 static conversion *
3931 conditional_conversion (tree e1, tree e2)
3933 tree t1 = non_reference (TREE_TYPE (e1));
3934 tree t2 = non_reference (TREE_TYPE (e2));
3935 conversion *conv;
3936 bool good_base;
3938 /* [expr.cond]
3940 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
3941 implicitly converted (clause _conv_) to the type "reference to
3942 T2", subject to the constraint that in the conversion the
3943 reference must bind directly (_dcl.init.ref_) to E1. */
3944 if (real_lvalue_p (e2))
3946 conv = implicit_conversion (build_reference_type (t2),
3949 /*c_cast_p=*/false,
3950 LOOKUP_NO_TEMP_BIND|LOOKUP_ONLYCONVERTING);
3951 if (conv)
3952 return conv;
3955 /* [expr.cond]
3957 If E1 and E2 have class type, and the underlying class types are
3958 the same or one is a base class of the other: E1 can be converted
3959 to match E2 if the class of T2 is the same type as, or a base
3960 class of, the class of T1, and the cv-qualification of T2 is the
3961 same cv-qualification as, or a greater cv-qualification than, the
3962 cv-qualification of T1. If the conversion is applied, E1 is
3963 changed to an rvalue of type T2 that still refers to the original
3964 source class object (or the appropriate subobject thereof). */
3965 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
3966 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
3968 if (good_base && at_least_as_qualified_p (t2, t1))
3970 conv = build_identity_conv (t1, e1);
3971 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
3972 TYPE_MAIN_VARIANT (t2)))
3973 conv = build_conv (ck_base, t2, conv);
3974 else
3975 conv = build_conv (ck_rvalue, t2, conv);
3976 return conv;
3978 else
3979 return NULL;
3981 else
3982 /* [expr.cond]
3984 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
3985 converted to the type that expression E2 would have if E2 were
3986 converted to an rvalue (or the type it has, if E2 is an rvalue). */
3987 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
3988 LOOKUP_IMPLICIT);
3991 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
3992 arguments to the conditional expression. */
3994 tree
3995 build_conditional_expr (tree arg1, tree arg2, tree arg3,
3996 tsubst_flags_t complain)
3998 tree arg2_type;
3999 tree arg3_type;
4000 tree result = NULL_TREE;
4001 tree result_type = NULL_TREE;
4002 bool lvalue_p = true;
4003 struct z_candidate *candidates = 0;
4004 struct z_candidate *cand;
4005 void *p;
4007 /* As a G++ extension, the second argument to the conditional can be
4008 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
4009 c'.) If the second operand is omitted, make sure it is
4010 calculated only once. */
4011 if (!arg2)
4013 if (complain & tf_error)
4014 pedwarn (input_location, OPT_pedantic,
4015 "ISO C++ forbids omitting the middle term of a ?: expression");
4017 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
4018 if (real_lvalue_p (arg1))
4019 arg2 = arg1 = stabilize_reference (arg1);
4020 else
4021 arg2 = arg1 = save_expr (arg1);
4024 /* [expr.cond]
4026 The first expression is implicitly converted to bool (clause
4027 _conv_). */
4028 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
4029 LOOKUP_NORMAL);
4031 /* If something has already gone wrong, just pass that fact up the
4032 tree. */
4033 if (error_operand_p (arg1)
4034 || error_operand_p (arg2)
4035 || error_operand_p (arg3))
4036 return error_mark_node;
4038 /* [expr.cond]
4040 If either the second or the third operand has type (possibly
4041 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
4042 array-to-pointer (_conv.array_), and function-to-pointer
4043 (_conv.func_) standard conversions are performed on the second
4044 and third operands. */
4045 arg2_type = unlowered_expr_type (arg2);
4046 arg3_type = unlowered_expr_type (arg3);
4047 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
4049 /* Do the conversions. We don't these for `void' type arguments
4050 since it can't have any effect and since decay_conversion
4051 does not handle that case gracefully. */
4052 if (!VOID_TYPE_P (arg2_type))
4053 arg2 = decay_conversion (arg2);
4054 if (!VOID_TYPE_P (arg3_type))
4055 arg3 = decay_conversion (arg3);
4056 arg2_type = TREE_TYPE (arg2);
4057 arg3_type = TREE_TYPE (arg3);
4059 /* [expr.cond]
4061 One of the following shall hold:
4063 --The second or the third operand (but not both) is a
4064 throw-expression (_except.throw_); the result is of the
4065 type of the other and is an rvalue.
4067 --Both the second and the third operands have type void; the
4068 result is of type void and is an rvalue.
4070 We must avoid calling force_rvalue for expressions of type
4071 "void" because it will complain that their value is being
4072 used. */
4073 if (TREE_CODE (arg2) == THROW_EXPR
4074 && TREE_CODE (arg3) != THROW_EXPR)
4076 if (!VOID_TYPE_P (arg3_type))
4077 arg3 = force_rvalue (arg3);
4078 arg3_type = TREE_TYPE (arg3);
4079 result_type = arg3_type;
4081 else if (TREE_CODE (arg2) != THROW_EXPR
4082 && TREE_CODE (arg3) == THROW_EXPR)
4084 if (!VOID_TYPE_P (arg2_type))
4085 arg2 = force_rvalue (arg2);
4086 arg2_type = TREE_TYPE (arg2);
4087 result_type = arg2_type;
4089 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
4090 result_type = void_type_node;
4091 else
4093 if (complain & tf_error)
4095 if (VOID_TYPE_P (arg2_type))
4096 error ("second operand to the conditional operator "
4097 "is of type %<void%>, "
4098 "but the third operand is neither a throw-expression "
4099 "nor of type %<void%>");
4100 else
4101 error ("third operand to the conditional operator "
4102 "is of type %<void%>, "
4103 "but the second operand is neither a throw-expression "
4104 "nor of type %<void%>");
4106 return error_mark_node;
4109 lvalue_p = false;
4110 goto valid_operands;
4112 /* [expr.cond]
4114 Otherwise, if the second and third operand have different types,
4115 and either has (possibly cv-qualified) class type, an attempt is
4116 made to convert each of those operands to the type of the other. */
4117 else if (!same_type_p (arg2_type, arg3_type)
4118 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
4120 conversion *conv2;
4121 conversion *conv3;
4123 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4124 p = conversion_obstack_alloc (0);
4126 conv2 = conditional_conversion (arg2, arg3);
4127 conv3 = conditional_conversion (arg3, arg2);
4129 /* [expr.cond]
4131 If both can be converted, or one can be converted but the
4132 conversion is ambiguous, the program is ill-formed. If
4133 neither can be converted, the operands are left unchanged and
4134 further checking is performed as described below. If exactly
4135 one conversion is possible, that conversion is applied to the
4136 chosen operand and the converted operand is used in place of
4137 the original operand for the remainder of this section. */
4138 if ((conv2 && !conv2->bad_p
4139 && conv3 && !conv3->bad_p)
4140 || (conv2 && conv2->kind == ck_ambig)
4141 || (conv3 && conv3->kind == ck_ambig))
4143 error ("operands to ?: have different types %qT and %qT",
4144 arg2_type, arg3_type);
4145 result = error_mark_node;
4147 else if (conv2 && (!conv2->bad_p || !conv3))
4149 arg2 = convert_like (conv2, arg2, complain);
4150 arg2 = convert_from_reference (arg2);
4151 arg2_type = TREE_TYPE (arg2);
4152 /* Even if CONV2 is a valid conversion, the result of the
4153 conversion may be invalid. For example, if ARG3 has type
4154 "volatile X", and X does not have a copy constructor
4155 accepting a "volatile X&", then even if ARG2 can be
4156 converted to X, the conversion will fail. */
4157 if (error_operand_p (arg2))
4158 result = error_mark_node;
4160 else if (conv3 && (!conv3->bad_p || !conv2))
4162 arg3 = convert_like (conv3, arg3, complain);
4163 arg3 = convert_from_reference (arg3);
4164 arg3_type = TREE_TYPE (arg3);
4165 if (error_operand_p (arg3))
4166 result = error_mark_node;
4169 /* Free all the conversions we allocated. */
4170 obstack_free (&conversion_obstack, p);
4172 if (result)
4173 return result;
4175 /* If, after the conversion, both operands have class type,
4176 treat the cv-qualification of both operands as if it were the
4177 union of the cv-qualification of the operands.
4179 The standard is not clear about what to do in this
4180 circumstance. For example, if the first operand has type
4181 "const X" and the second operand has a user-defined
4182 conversion to "volatile X", what is the type of the second
4183 operand after this step? Making it be "const X" (matching
4184 the first operand) seems wrong, as that discards the
4185 qualification without actually performing a copy. Leaving it
4186 as "volatile X" seems wrong as that will result in the
4187 conditional expression failing altogether, even though,
4188 according to this step, the one operand could be converted to
4189 the type of the other. */
4190 if ((conv2 || conv3)
4191 && CLASS_TYPE_P (arg2_type)
4192 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
4193 arg2_type = arg3_type =
4194 cp_build_qualified_type (arg2_type,
4195 cp_type_quals (arg2_type)
4196 | cp_type_quals (arg3_type));
4199 /* [expr.cond]
4201 If the second and third operands are lvalues and have the same
4202 type, the result is of that type and is an lvalue. */
4203 if (real_lvalue_p (arg2)
4204 && real_lvalue_p (arg3)
4205 && same_type_p (arg2_type, arg3_type))
4207 result_type = arg2_type;
4208 arg2 = mark_lvalue_use (arg2);
4209 arg3 = mark_lvalue_use (arg3);
4210 goto valid_operands;
4213 /* [expr.cond]
4215 Otherwise, the result is an rvalue. If the second and third
4216 operand do not have the same type, and either has (possibly
4217 cv-qualified) class type, overload resolution is used to
4218 determine the conversions (if any) to be applied to the operands
4219 (_over.match.oper_, _over.built_). */
4220 lvalue_p = false;
4221 if (!same_type_p (arg2_type, arg3_type)
4222 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
4224 tree args[3];
4225 conversion *conv;
4226 bool any_viable_p;
4228 /* Rearrange the arguments so that add_builtin_candidate only has
4229 to know about two args. In build_builtin_candidate, the
4230 arguments are unscrambled. */
4231 args[0] = arg2;
4232 args[1] = arg3;
4233 args[2] = arg1;
4234 add_builtin_candidates (&candidates,
4235 COND_EXPR,
4236 NOP_EXPR,
4237 ansi_opname (COND_EXPR),
4238 args,
4239 LOOKUP_NORMAL);
4241 /* [expr.cond]
4243 If the overload resolution fails, the program is
4244 ill-formed. */
4245 candidates = splice_viable (candidates, pedantic, &any_viable_p);
4246 if (!any_viable_p)
4248 if (complain & tf_error)
4250 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
4251 print_z_candidates (location_of (arg1), candidates);
4253 return error_mark_node;
4255 cand = tourney (candidates);
4256 if (!cand)
4258 if (complain & tf_error)
4260 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
4261 print_z_candidates (location_of (arg1), candidates);
4263 return error_mark_node;
4266 /* [expr.cond]
4268 Otherwise, the conversions thus determined are applied, and
4269 the converted operands are used in place of the original
4270 operands for the remainder of this section. */
4271 conv = cand->convs[0];
4272 arg1 = convert_like (conv, arg1, complain);
4273 conv = cand->convs[1];
4274 arg2 = convert_like (conv, arg2, complain);
4275 arg2_type = TREE_TYPE (arg2);
4276 conv = cand->convs[2];
4277 arg3 = convert_like (conv, arg3, complain);
4278 arg3_type = TREE_TYPE (arg3);
4281 /* [expr.cond]
4283 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
4284 and function-to-pointer (_conv.func_) standard conversions are
4285 performed on the second and third operands.
4287 We need to force the lvalue-to-rvalue conversion here for class types,
4288 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
4289 that isn't wrapped with a TARGET_EXPR plays havoc with exception
4290 regions. */
4292 arg2 = force_rvalue (arg2);
4293 if (!CLASS_TYPE_P (arg2_type))
4294 arg2_type = TREE_TYPE (arg2);
4296 arg3 = force_rvalue (arg3);
4297 if (!CLASS_TYPE_P (arg3_type))
4298 arg3_type = TREE_TYPE (arg3);
4300 if (arg2 == error_mark_node || arg3 == error_mark_node)
4301 return error_mark_node;
4303 /* [expr.cond]
4305 After those conversions, one of the following shall hold:
4307 --The second and third operands have the same type; the result is of
4308 that type. */
4309 if (same_type_p (arg2_type, arg3_type))
4310 result_type = arg2_type;
4311 /* [expr.cond]
4313 --The second and third operands have arithmetic or enumeration
4314 type; the usual arithmetic conversions are performed to bring
4315 them to a common type, and the result is of that type. */
4316 else if ((ARITHMETIC_TYPE_P (arg2_type)
4317 || UNSCOPED_ENUM_P (arg2_type))
4318 && (ARITHMETIC_TYPE_P (arg3_type)
4319 || UNSCOPED_ENUM_P (arg3_type)))
4321 /* In this case, there is always a common type. */
4322 result_type = type_after_usual_arithmetic_conversions (arg2_type,
4323 arg3_type);
4324 do_warn_double_promotion (result_type, arg2_type, arg3_type,
4325 "implicit conversion from %qT to %qT to "
4326 "match other result of conditional",
4327 input_location);
4329 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
4330 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
4332 if (complain & tf_warning)
4333 warning (0,
4334 "enumeral mismatch in conditional expression: %qT vs %qT",
4335 arg2_type, arg3_type);
4337 else if (extra_warnings
4338 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
4339 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
4340 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
4341 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
4343 if (complain & tf_warning)
4344 warning (0,
4345 "enumeral and non-enumeral type in conditional expression");
4348 arg2 = perform_implicit_conversion (result_type, arg2, complain);
4349 arg3 = perform_implicit_conversion (result_type, arg3, complain);
4351 /* [expr.cond]
4353 --The second and third operands have pointer type, or one has
4354 pointer type and the other is a null pointer constant; pointer
4355 conversions (_conv.ptr_) and qualification conversions
4356 (_conv.qual_) are performed to bring them to their composite
4357 pointer type (_expr.rel_). The result is of the composite
4358 pointer type.
4360 --The second and third operands have pointer to member type, or
4361 one has pointer to member type and the other is a null pointer
4362 constant; pointer to member conversions (_conv.mem_) and
4363 qualification conversions (_conv.qual_) are performed to bring
4364 them to a common type, whose cv-qualification shall match the
4365 cv-qualification of either the second or the third operand.
4366 The result is of the common type. */
4367 else if ((null_ptr_cst_p (arg2)
4368 && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
4369 || (null_ptr_cst_p (arg3)
4370 && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
4371 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
4372 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
4373 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
4375 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
4376 arg3, CPO_CONDITIONAL_EXPR,
4377 complain);
4378 if (result_type == error_mark_node)
4379 return error_mark_node;
4380 arg2 = perform_implicit_conversion (result_type, arg2, complain);
4381 arg3 = perform_implicit_conversion (result_type, arg3, complain);
4384 if (!result_type)
4386 if (complain & tf_error)
4387 error ("operands to ?: have different types %qT and %qT",
4388 arg2_type, arg3_type);
4389 return error_mark_node;
4392 valid_operands:
4393 result = build3 (COND_EXPR, result_type, arg1, arg2, arg3);
4394 if (!cp_unevaluated_operand)
4395 /* Avoid folding within decltype (c++/42013) and noexcept. */
4396 result = fold_if_not_in_template (result);
4398 /* We can't use result_type below, as fold might have returned a
4399 throw_expr. */
4401 if (!lvalue_p)
4403 /* Expand both sides into the same slot, hopefully the target of
4404 the ?: expression. We used to check for TARGET_EXPRs here,
4405 but now we sometimes wrap them in NOP_EXPRs so the test would
4406 fail. */
4407 if (CLASS_TYPE_P (TREE_TYPE (result)))
4408 result = get_target_expr (result);
4409 /* If this expression is an rvalue, but might be mistaken for an
4410 lvalue, we must add a NON_LVALUE_EXPR. */
4411 result = rvalue (result);
4414 return result;
4417 /* OPERAND is an operand to an expression. Perform necessary steps
4418 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
4419 returned. */
4421 static tree
4422 prep_operand (tree operand)
4424 if (operand)
4426 if (CLASS_TYPE_P (TREE_TYPE (operand))
4427 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
4428 /* Make sure the template type is instantiated now. */
4429 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
4432 return operand;
4435 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
4436 OVERLOAD) to the CANDIDATES, returning an updated list of
4437 CANDIDATES. The ARGS are the arguments provided to the call;
4438 if FIRST_ARG is non-null it is the implicit object argument,
4439 otherwise the first element of ARGS is used if needed. The
4440 EXPLICIT_TARGS are explicit template arguments provided.
4441 TEMPLATE_ONLY is true if only template functions should be
4442 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
4443 add_function_candidate. */
4445 static void
4446 add_candidates (tree fns, tree first_arg, const VEC(tree,gc) *args,
4447 tree return_type,
4448 tree explicit_targs, bool template_only,
4449 tree conversion_path, tree access_path,
4450 int flags,
4451 struct z_candidate **candidates)
4453 tree ctype;
4454 const VEC(tree,gc) *non_static_args;
4455 bool check_list_ctor;
4456 bool check_converting;
4457 unification_kind_t strict;
4458 tree fn;
4460 if (!fns)
4461 return;
4463 /* Precalculate special handling of constructors and conversion ops. */
4464 fn = OVL_CURRENT (fns);
4465 if (DECL_CONV_FN_P (fn))
4467 check_list_ctor = false;
4468 check_converting = !!(flags & LOOKUP_ONLYCONVERTING);
4469 if (flags & LOOKUP_NO_CONVERSION)
4470 /* We're doing return_type(x). */
4471 strict = DEDUCE_CONV;
4472 else
4473 /* We're doing x.operator return_type(). */
4474 strict = DEDUCE_EXACT;
4475 /* [over.match.funcs] For conversion functions, the function
4476 is considered to be a member of the class of the implicit
4477 object argument for the purpose of defining the type of
4478 the implicit object parameter. */
4479 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (first_arg)));
4481 else
4483 if (DECL_CONSTRUCTOR_P (fn))
4485 check_list_ctor = !!(flags & LOOKUP_LIST_ONLY);
4486 check_converting = !!(flags & LOOKUP_ONLYCONVERTING);
4488 else
4490 check_list_ctor = false;
4491 check_converting = false;
4493 strict = DEDUCE_CALL;
4494 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
4497 if (first_arg)
4498 non_static_args = args;
4499 else
4500 /* Delay creating the implicit this parameter until it is needed. */
4501 non_static_args = NULL;
4503 for (; fns; fns = OVL_NEXT (fns))
4505 tree fn_first_arg;
4506 const VEC(tree,gc) *fn_args;
4508 fn = OVL_CURRENT (fns);
4510 if (check_converting && DECL_NONCONVERTING_P (fn))
4511 continue;
4512 if (check_list_ctor && !is_list_ctor (fn))
4513 continue;
4515 /* Figure out which set of arguments to use. */
4516 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4518 /* If this function is a non-static member and we didn't get an
4519 implicit object argument, move it out of args. */
4520 if (first_arg == NULL_TREE)
4522 unsigned int ix;
4523 tree arg;
4524 VEC(tree,gc) *tempvec
4525 = VEC_alloc (tree, gc, VEC_length (tree, args) - 1);
4526 for (ix = 1; VEC_iterate (tree, args, ix, arg); ++ix)
4527 VEC_quick_push (tree, tempvec, arg);
4528 non_static_args = tempvec;
4529 first_arg = build_this (VEC_index (tree, args, 0));
4532 fn_first_arg = first_arg;
4533 fn_args = non_static_args;
4535 else
4537 /* Otherwise, just use the list of arguments provided. */
4538 fn_first_arg = NULL_TREE;
4539 fn_args = args;
4542 if (TREE_CODE (fn) == TEMPLATE_DECL)
4543 add_template_candidate (candidates,
4545 ctype,
4546 explicit_targs,
4547 fn_first_arg,
4548 fn_args,
4549 return_type,
4550 access_path,
4551 conversion_path,
4552 flags,
4553 strict);
4554 else if (!template_only)
4555 add_function_candidate (candidates,
4557 ctype,
4558 fn_first_arg,
4559 fn_args,
4560 access_path,
4561 conversion_path,
4562 flags);
4566 /* Even unsigned enum types promote to signed int. We don't want to
4567 issue -Wsign-compare warnings for this case. Here ORIG_ARG is the
4568 original argument and ARG is the argument after any conversions
4569 have been applied. We set TREE_NO_WARNING if we have added a cast
4570 from an unsigned enum type to a signed integer type. */
4572 static void
4573 avoid_sign_compare_warnings (tree orig_arg, tree arg)
4575 if (orig_arg != NULL_TREE
4576 && arg != NULL_TREE
4577 && orig_arg != arg
4578 && TREE_CODE (TREE_TYPE (orig_arg)) == ENUMERAL_TYPE
4579 && TYPE_UNSIGNED (TREE_TYPE (orig_arg))
4580 && INTEGRAL_TYPE_P (TREE_TYPE (arg))
4581 && !TYPE_UNSIGNED (TREE_TYPE (arg)))
4582 TREE_NO_WARNING (arg) = 1;
4585 tree
4586 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
4587 bool *overloaded_p, tsubst_flags_t complain)
4589 tree orig_arg1 = arg1;
4590 tree orig_arg2 = arg2;
4591 tree orig_arg3 = arg3;
4592 struct z_candidate *candidates = 0, *cand;
4593 VEC(tree,gc) *arglist;
4594 tree fnname;
4595 tree args[3];
4596 tree result = NULL_TREE;
4597 bool result_valid_p = false;
4598 enum tree_code code2 = NOP_EXPR;
4599 enum tree_code code_orig_arg1 = ERROR_MARK;
4600 enum tree_code code_orig_arg2 = ERROR_MARK;
4601 conversion *conv;
4602 void *p;
4603 bool strict_p;
4604 bool any_viable_p;
4606 if (error_operand_p (arg1)
4607 || error_operand_p (arg2)
4608 || error_operand_p (arg3))
4609 return error_mark_node;
4611 if (code == MODIFY_EXPR)
4613 code2 = TREE_CODE (arg3);
4614 arg3 = NULL_TREE;
4615 fnname = ansi_assopname (code2);
4617 else
4618 fnname = ansi_opname (code);
4620 arg1 = prep_operand (arg1);
4622 switch (code)
4624 case NEW_EXPR:
4625 case VEC_NEW_EXPR:
4626 case VEC_DELETE_EXPR:
4627 case DELETE_EXPR:
4628 /* Use build_op_new_call and build_op_delete_call instead. */
4629 gcc_unreachable ();
4631 case CALL_EXPR:
4632 /* Use build_op_call instead. */
4633 gcc_unreachable ();
4635 case TRUTH_ORIF_EXPR:
4636 case TRUTH_ANDIF_EXPR:
4637 case TRUTH_AND_EXPR:
4638 case TRUTH_OR_EXPR:
4639 /* These are saved for the sake of warn_logical_operator. */
4640 code_orig_arg1 = TREE_CODE (arg1);
4641 code_orig_arg2 = TREE_CODE (arg2);
4643 default:
4644 break;
4647 arg2 = prep_operand (arg2);
4648 arg3 = prep_operand (arg3);
4650 if (code == COND_EXPR)
4651 /* Use build_conditional_expr instead. */
4652 gcc_unreachable ();
4653 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
4654 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
4655 goto builtin;
4657 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
4658 arg2 = integer_zero_node;
4660 arglist = VEC_alloc (tree, gc, 3);
4661 VEC_quick_push (tree, arglist, arg1);
4662 if (arg2 != NULL_TREE)
4663 VEC_quick_push (tree, arglist, arg2);
4664 if (arg3 != NULL_TREE)
4665 VEC_quick_push (tree, arglist, arg3);
4667 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4668 p = conversion_obstack_alloc (0);
4670 /* Add namespace-scope operators to the list of functions to
4671 consider. */
4672 add_candidates (lookup_function_nonclass (fnname, arglist, /*block_p=*/true),
4673 NULL_TREE, arglist, NULL_TREE,
4674 NULL_TREE, false, NULL_TREE, NULL_TREE,
4675 flags, &candidates);
4676 /* Add class-member operators to the candidate set. */
4677 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
4679 tree fns;
4681 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
4682 if (fns == error_mark_node)
4684 result = error_mark_node;
4685 goto user_defined_result_ready;
4687 if (fns)
4688 add_candidates (BASELINK_FUNCTIONS (fns),
4689 NULL_TREE, arglist, NULL_TREE,
4690 NULL_TREE, false,
4691 BASELINK_BINFO (fns),
4692 BASELINK_ACCESS_BINFO (fns),
4693 flags, &candidates);
4696 args[0] = arg1;
4697 args[1] = arg2;
4698 args[2] = NULL_TREE;
4700 add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
4702 switch (code)
4704 case COMPOUND_EXPR:
4705 case ADDR_EXPR:
4706 /* For these, the built-in candidates set is empty
4707 [over.match.oper]/3. We don't want non-strict matches
4708 because exact matches are always possible with built-in
4709 operators. The built-in candidate set for COMPONENT_REF
4710 would be empty too, but since there are no such built-in
4711 operators, we accept non-strict matches for them. */
4712 strict_p = true;
4713 break;
4715 default:
4716 strict_p = pedantic;
4717 break;
4720 candidates = splice_viable (candidates, strict_p, &any_viable_p);
4721 if (!any_viable_p)
4723 switch (code)
4725 case POSTINCREMENT_EXPR:
4726 case POSTDECREMENT_EXPR:
4727 /* Don't try anything fancy if we're not allowed to produce
4728 errors. */
4729 if (!(complain & tf_error))
4730 return error_mark_node;
4732 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
4733 distinguish between prefix and postfix ++ and
4734 operator++() was used for both, so we allow this with
4735 -fpermissive. */
4736 if (flags & LOOKUP_COMPLAIN)
4738 const char *msg = (flag_permissive)
4739 ? G_("no %<%D(int)%> declared for postfix %qs,"
4740 " trying prefix operator instead")
4741 : G_("no %<%D(int)%> declared for postfix %qs");
4742 permerror (input_location, msg, fnname,
4743 operator_name_info[code].name);
4746 if (!flag_permissive)
4747 return error_mark_node;
4749 if (code == POSTINCREMENT_EXPR)
4750 code = PREINCREMENT_EXPR;
4751 else
4752 code = PREDECREMENT_EXPR;
4753 result = build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE,
4754 overloaded_p, complain);
4755 break;
4757 /* The caller will deal with these. */
4758 case ADDR_EXPR:
4759 case COMPOUND_EXPR:
4760 case COMPONENT_REF:
4761 result = NULL_TREE;
4762 result_valid_p = true;
4763 break;
4765 default:
4766 if ((flags & LOOKUP_COMPLAIN) && (complain & tf_error))
4768 /* If one of the arguments of the operator represents
4769 an invalid use of member function pointer, try to report
4770 a meaningful error ... */
4771 if (invalid_nonstatic_memfn_p (arg1, tf_error)
4772 || invalid_nonstatic_memfn_p (arg2, tf_error)
4773 || invalid_nonstatic_memfn_p (arg3, tf_error))
4774 /* We displayed the error message. */;
4775 else
4777 /* ... Otherwise, report the more generic
4778 "no matching operator found" error */
4779 op_error (code, code2, arg1, arg2, arg3, FALSE);
4780 print_z_candidates (input_location, candidates);
4783 result = error_mark_node;
4784 break;
4787 else
4789 cand = tourney (candidates);
4790 if (cand == 0)
4792 if ((flags & LOOKUP_COMPLAIN) && (complain & tf_error))
4794 op_error (code, code2, arg1, arg2, arg3, TRUE);
4795 print_z_candidates (input_location, candidates);
4797 result = error_mark_node;
4799 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
4801 if (overloaded_p)
4802 *overloaded_p = true;
4804 if (resolve_args (arglist) == NULL)
4805 result = error_mark_node;
4806 else
4807 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4809 else
4811 /* Give any warnings we noticed during overload resolution. */
4812 if (cand->warnings && (complain & tf_warning))
4814 struct candidate_warning *w;
4815 for (w = cand->warnings; w; w = w->next)
4816 joust (cand, w->loser, 1);
4819 /* Check for comparison of different enum types. */
4820 switch (code)
4822 case GT_EXPR:
4823 case LT_EXPR:
4824 case GE_EXPR:
4825 case LE_EXPR:
4826 case EQ_EXPR:
4827 case NE_EXPR:
4828 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
4829 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
4830 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
4831 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
4832 && (complain & tf_warning))
4834 warning (OPT_Wenum_compare,
4835 "comparison between %q#T and %q#T",
4836 TREE_TYPE (arg1), TREE_TYPE (arg2));
4838 break;
4839 default:
4840 break;
4843 /* We need to strip any leading REF_BIND so that bitfields
4844 don't cause errors. This should not remove any important
4845 conversions, because builtins don't apply to class
4846 objects directly. */
4847 conv = cand->convs[0];
4848 if (conv->kind == ck_ref_bind)
4849 conv = conv->u.next;
4850 arg1 = convert_like (conv, arg1, complain);
4852 if (arg2)
4854 /* We need to call warn_logical_operator before
4855 converting arg2 to a boolean_type. */
4856 if (complain & tf_warning)
4857 warn_logical_operator (input_location, code, boolean_type_node,
4858 code_orig_arg1, arg1,
4859 code_orig_arg2, arg2);
4861 conv = cand->convs[1];
4862 if (conv->kind == ck_ref_bind)
4863 conv = conv->u.next;
4864 arg2 = convert_like (conv, arg2, complain);
4866 if (arg3)
4868 conv = cand->convs[2];
4869 if (conv->kind == ck_ref_bind)
4870 conv = conv->u.next;
4871 arg3 = convert_like (conv, arg3, complain);
4877 user_defined_result_ready:
4879 /* Free all the conversions we allocated. */
4880 obstack_free (&conversion_obstack, p);
4882 if (result || result_valid_p)
4883 return result;
4885 builtin:
4886 avoid_sign_compare_warnings (orig_arg1, arg1);
4887 avoid_sign_compare_warnings (orig_arg2, arg2);
4888 avoid_sign_compare_warnings (orig_arg3, arg3);
4890 switch (code)
4892 case MODIFY_EXPR:
4893 return cp_build_modify_expr (arg1, code2, arg2, complain);
4895 case INDIRECT_REF:
4896 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
4898 case TRUTH_ANDIF_EXPR:
4899 case TRUTH_ORIF_EXPR:
4900 case TRUTH_AND_EXPR:
4901 case TRUTH_OR_EXPR:
4902 warn_logical_operator (input_location, code, boolean_type_node,
4903 code_orig_arg1, arg1, code_orig_arg2, arg2);
4904 /* Fall through. */
4905 case PLUS_EXPR:
4906 case MINUS_EXPR:
4907 case MULT_EXPR:
4908 case TRUNC_DIV_EXPR:
4909 case GT_EXPR:
4910 case LT_EXPR:
4911 case GE_EXPR:
4912 case LE_EXPR:
4913 case EQ_EXPR:
4914 case NE_EXPR:
4915 case MAX_EXPR:
4916 case MIN_EXPR:
4917 case LSHIFT_EXPR:
4918 case RSHIFT_EXPR:
4919 case TRUNC_MOD_EXPR:
4920 case BIT_AND_EXPR:
4921 case BIT_IOR_EXPR:
4922 case BIT_XOR_EXPR:
4923 return cp_build_binary_op (input_location, code, arg1, arg2, complain);
4925 case UNARY_PLUS_EXPR:
4926 case NEGATE_EXPR:
4927 case BIT_NOT_EXPR:
4928 case TRUTH_NOT_EXPR:
4929 case PREINCREMENT_EXPR:
4930 case POSTINCREMENT_EXPR:
4931 case PREDECREMENT_EXPR:
4932 case POSTDECREMENT_EXPR:
4933 case REALPART_EXPR:
4934 case IMAGPART_EXPR:
4935 return cp_build_unary_op (code, arg1, candidates != 0, complain);
4937 case ARRAY_REF:
4938 return cp_build_array_ref (input_location, arg1, arg2, complain);
4940 case MEMBER_REF:
4941 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_NULL,
4942 complain),
4943 arg2);
4945 /* The caller will deal with these. */
4946 case ADDR_EXPR:
4947 case COMPONENT_REF:
4948 case COMPOUND_EXPR:
4949 return NULL_TREE;
4951 default:
4952 gcc_unreachable ();
4954 return NULL_TREE;
4957 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
4958 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
4960 static bool
4961 non_placement_deallocation_fn_p (tree t)
4963 /* A template instance is never a usual deallocation function,
4964 regardless of its signature. */
4965 if (TREE_CODE (t) == TEMPLATE_DECL
4966 || primary_template_instantiation_p (t))
4967 return false;
4969 /* If a class T has a member deallocation function named operator delete
4970 with exactly one parameter, then that function is a usual
4971 (non-placement) deallocation function. If class T does not declare
4972 such an operator delete but does declare a member deallocation
4973 function named operator delete with exactly two parameters, the second
4974 of which has type std::size_t (18.2), then this function is a usual
4975 deallocation function. */
4976 t = FUNCTION_ARG_CHAIN (t);
4977 if (t == void_list_node
4978 || (t && same_type_p (TREE_VALUE (t), size_type_node)
4979 && TREE_CHAIN (t) == void_list_node))
4980 return true;
4981 return false;
4984 /* Build a call to operator delete. This has to be handled very specially,
4985 because the restrictions on what signatures match are different from all
4986 other call instances. For a normal delete, only a delete taking (void *)
4987 or (void *, size_t) is accepted. For a placement delete, only an exact
4988 match with the placement new is accepted.
4990 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
4991 ADDR is the pointer to be deleted.
4992 SIZE is the size of the memory block to be deleted.
4993 GLOBAL_P is true if the delete-expression should not consider
4994 class-specific delete operators.
4995 PLACEMENT is the corresponding placement new call, or NULL_TREE.
4997 If this call to "operator delete" is being generated as part to
4998 deallocate memory allocated via a new-expression (as per [expr.new]
4999 which requires that if the initialization throws an exception then
5000 we call a deallocation function), then ALLOC_FN is the allocation
5001 function. */
5003 tree
5004 build_op_delete_call (enum tree_code code, tree addr, tree size,
5005 bool global_p, tree placement,
5006 tree alloc_fn)
5008 tree fn = NULL_TREE;
5009 tree fns, fnname, type, t;
5011 if (addr == error_mark_node)
5012 return error_mark_node;
5014 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
5016 fnname = ansi_opname (code);
5018 if (CLASS_TYPE_P (type)
5019 && COMPLETE_TYPE_P (complete_type (type))
5020 && !global_p)
5021 /* In [class.free]
5023 If the result of the lookup is ambiguous or inaccessible, or if
5024 the lookup selects a placement deallocation function, the
5025 program is ill-formed.
5027 Therefore, we ask lookup_fnfields to complain about ambiguity. */
5029 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
5030 if (fns == error_mark_node)
5031 return error_mark_node;
5033 else
5034 fns = NULL_TREE;
5036 if (fns == NULL_TREE)
5037 fns = lookup_name_nonclass (fnname);
5039 /* Strip const and volatile from addr. */
5040 addr = cp_convert (ptr_type_node, addr);
5042 if (placement)
5044 /* "A declaration of a placement deallocation function matches the
5045 declaration of a placement allocation function if it has the same
5046 number of parameters and, after parameter transformations (8.3.5),
5047 all parameter types except the first are identical."
5049 So we build up the function type we want and ask instantiate_type
5050 to get it for us. */
5051 t = FUNCTION_ARG_CHAIN (alloc_fn);
5052 t = tree_cons (NULL_TREE, ptr_type_node, t);
5053 t = build_function_type (void_type_node, t);
5055 fn = instantiate_type (t, fns, tf_none);
5056 if (fn == error_mark_node)
5057 return NULL_TREE;
5059 if (BASELINK_P (fn))
5060 fn = BASELINK_FUNCTIONS (fn);
5062 /* "If the lookup finds the two-parameter form of a usual deallocation
5063 function (3.7.4.2) and that function, considered as a placement
5064 deallocation function, would have been selected as a match for the
5065 allocation function, the program is ill-formed." */
5066 if (non_placement_deallocation_fn_p (fn))
5068 /* But if the class has an operator delete (void *), then that is
5069 the usual deallocation function, so we shouldn't complain
5070 about using the operator delete (void *, size_t). */
5071 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
5072 t; t = OVL_NEXT (t))
5074 tree elt = OVL_CURRENT (t);
5075 if (non_placement_deallocation_fn_p (elt)
5076 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
5077 goto ok;
5079 permerror (0, "non-placement deallocation function %q+D", fn);
5080 permerror (input_location, "selected for placement delete");
5081 ok:;
5084 else
5085 /* "Any non-placement deallocation function matches a non-placement
5086 allocation function. If the lookup finds a single matching
5087 deallocation function, that function will be called; otherwise, no
5088 deallocation function will be called." */
5089 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
5090 t; t = OVL_NEXT (t))
5092 tree elt = OVL_CURRENT (t);
5093 if (non_placement_deallocation_fn_p (elt))
5095 fn = elt;
5096 /* "If a class T has a member deallocation function named
5097 operator delete with exactly one parameter, then that
5098 function is a usual (non-placement) deallocation
5099 function. If class T does not declare such an operator
5100 delete but does declare a member deallocation function named
5101 operator delete with exactly two parameters, the second of
5102 which has type std::size_t (18.2), then this function is a
5103 usual deallocation function."
5105 So (void*) beats (void*, size_t). */
5106 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
5107 break;
5111 /* If we have a matching function, call it. */
5112 if (fn)
5114 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
5116 /* If the FN is a member function, make sure that it is
5117 accessible. */
5118 if (BASELINK_P (fns))
5119 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn);
5121 /* Core issue 901: It's ok to new a type with deleted delete. */
5122 if (DECL_DELETED_FN (fn) && alloc_fn)
5123 return NULL_TREE;
5125 if (placement)
5127 /* The placement args might not be suitable for overload
5128 resolution at this point, so build the call directly. */
5129 int nargs = call_expr_nargs (placement);
5130 tree *argarray = XALLOCAVEC (tree, nargs);
5131 int i;
5132 argarray[0] = addr;
5133 for (i = 1; i < nargs; i++)
5134 argarray[i] = CALL_EXPR_ARG (placement, i);
5135 mark_used (fn);
5136 return build_cxx_call (fn, nargs, argarray);
5138 else
5140 tree ret;
5141 VEC(tree,gc) *args = VEC_alloc (tree, gc, 2);
5142 VEC_quick_push (tree, args, addr);
5143 if (FUNCTION_ARG_CHAIN (fn) != void_list_node)
5144 VEC_quick_push (tree, args, size);
5145 ret = cp_build_function_call_vec (fn, &args, tf_warning_or_error);
5146 VEC_free (tree, gc, args);
5147 return ret;
5151 /* [expr.new]
5153 If no unambiguous matching deallocation function can be found,
5154 propagating the exception does not cause the object's memory to
5155 be freed. */
5156 if (alloc_fn)
5158 if (!placement)
5159 warning (0, "no corresponding deallocation function for %qD",
5160 alloc_fn);
5161 return NULL_TREE;
5164 error ("no suitable %<operator %s%> for %qT",
5165 operator_name_info[(int)code].name, type);
5166 return error_mark_node;
5169 /* If the current scope isn't allowed to access DECL along
5170 BASETYPE_PATH, give an error. The most derived class in
5171 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
5172 the declaration to use in the error diagnostic. */
5174 bool
5175 enforce_access (tree basetype_path, tree decl, tree diag_decl)
5177 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
5179 if (!accessible_p (basetype_path, decl, true))
5181 if (TREE_PRIVATE (decl))
5182 error ("%q+#D is private", diag_decl);
5183 else if (TREE_PROTECTED (decl))
5184 error ("%q+#D is protected", diag_decl);
5185 else
5186 error ("%q+#D is inaccessible", diag_decl);
5187 error ("within this context");
5188 return false;
5191 return true;
5194 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
5195 bitwise or of LOOKUP_* values. If any errors are warnings are
5196 generated, set *DIAGNOSTIC_FN to "error" or "warning",
5197 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
5198 to NULL. */
5200 static tree
5201 build_temp (tree expr, tree type, int flags,
5202 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
5204 int savew, savee;
5205 VEC(tree,gc) *args;
5207 savew = warningcount, savee = errorcount;
5208 args = make_tree_vector_single (expr);
5209 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
5210 &args, type, flags, complain);
5211 release_tree_vector (args);
5212 if (warningcount > savew)
5213 *diagnostic_kind = DK_WARNING;
5214 else if (errorcount > savee)
5215 *diagnostic_kind = DK_ERROR;
5216 else
5217 *diagnostic_kind = DK_UNSPECIFIED;
5218 return expr;
5221 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
5222 EXPR is implicitly converted to type TOTYPE.
5223 FN and ARGNUM are used for diagnostics. */
5225 static void
5226 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
5228 tree t = non_reference (totype);
5230 /* Issue warnings about peculiar, but valid, uses of NULL. */
5231 if (expr == null_node && TREE_CODE (t) != BOOLEAN_TYPE && ARITHMETIC_TYPE_P (t))
5233 if (fn)
5234 warning_at (input_location, OPT_Wconversion_null,
5235 "passing NULL to non-pointer argument %P of %qD",
5236 argnum, fn);
5237 else
5238 warning_at (input_location, OPT_Wconversion_null,
5239 "converting to non-pointer type %qT from NULL", t);
5242 /* Issue warnings if "false" is converted to a NULL pointer */
5243 else if (expr == boolean_false_node && fn && POINTER_TYPE_P (t))
5244 warning_at (input_location, OPT_Wconversion_null,
5245 "converting %<false%> to pointer type for argument %P of %qD",
5246 argnum, fn);
5249 /* Perform the conversions in CONVS on the expression EXPR. FN and
5250 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
5251 indicates the `this' argument of a method. INNER is nonzero when
5252 being called to continue a conversion chain. It is negative when a
5253 reference binding will be applied, positive otherwise. If
5254 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
5255 conversions will be emitted if appropriate. If C_CAST_P is true,
5256 this conversion is coming from a C-style cast; in that case,
5257 conversions to inaccessible bases are permitted. */
5259 static tree
5260 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
5261 int inner, bool issue_conversion_warnings,
5262 bool c_cast_p, tsubst_flags_t complain)
5264 tree totype = convs->type;
5265 diagnostic_t diag_kind;
5266 int flags;
5268 if (convs->bad_p
5269 && convs->kind != ck_user
5270 && convs->kind != ck_list
5271 && convs->kind != ck_ambig
5272 && convs->kind != ck_ref_bind
5273 && convs->kind != ck_rvalue
5274 && convs->kind != ck_base)
5276 conversion *t = convs;
5278 /* Give a helpful error if this is bad because of excess braces. */
5279 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5280 && SCALAR_TYPE_P (totype)
5281 && CONSTRUCTOR_NELTS (expr) > 0
5282 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
5283 permerror (input_location, "too many braces around initializer for %qT", totype);
5285 for (; t; t = convs->u.next)
5287 if (t->kind == ck_user || !t->bad_p)
5289 expr = convert_like_real (t, expr, fn, argnum, 1,
5290 /*issue_conversion_warnings=*/false,
5291 /*c_cast_p=*/false,
5292 complain);
5293 break;
5295 else if (t->kind == ck_ambig)
5296 return convert_like_real (t, expr, fn, argnum, 1,
5297 /*issue_conversion_warnings=*/false,
5298 /*c_cast_p=*/false,
5299 complain);
5300 else if (t->kind == ck_identity)
5301 break;
5303 if (complain & tf_error)
5305 permerror (input_location, "invalid conversion from %qT to %qT", TREE_TYPE (expr), totype);
5306 if (fn)
5307 permerror (DECL_SOURCE_LOCATION (fn),
5308 " initializing argument %P of %qD", argnum, fn);
5310 else
5311 return error_mark_node;
5313 return cp_convert (totype, expr);
5316 if (issue_conversion_warnings && (complain & tf_warning))
5317 conversion_null_warnings (totype, expr, fn, argnum);
5319 switch (convs->kind)
5321 case ck_user:
5323 struct z_candidate *cand = convs->cand;
5324 tree convfn = cand->fn;
5325 unsigned i;
5327 expr = mark_rvalue_use (expr);
5329 /* When converting from an init list we consider explicit
5330 constructors, but actually trying to call one is an error. */
5331 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
5332 /* Unless we're calling it for value-initialization from an
5333 empty list, since that is handled separately in 8.5.4. */
5334 && cand->num_convs > 0)
5336 if (complain & tf_error)
5337 error ("converting to %qT from initializer list would use "
5338 "explicit constructor %qD", totype, convfn);
5339 else
5340 return error_mark_node;
5343 /* Set user_conv_p on the argument conversions, so rvalue/base
5344 handling knows not to allow any more UDCs. */
5345 for (i = 0; i < cand->num_convs; ++i)
5346 cand->convs[i]->user_conv_p = true;
5348 expr = build_over_call (cand, LOOKUP_NORMAL, complain);
5350 /* If this is a constructor or a function returning an aggr type,
5351 we need to build up a TARGET_EXPR. */
5352 if (DECL_CONSTRUCTOR_P (convfn))
5354 expr = build_cplus_new (totype, expr);
5356 /* Remember that this was list-initialization. */
5357 if (convs->check_narrowing)
5358 TARGET_EXPR_LIST_INIT_P (expr) = true;
5361 return expr;
5363 case ck_identity:
5364 expr = mark_rvalue_use (expr);
5365 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
5367 int nelts = CONSTRUCTOR_NELTS (expr);
5368 if (nelts == 0)
5369 expr = build_value_init (totype, tf_warning_or_error);
5370 else if (nelts == 1)
5371 expr = CONSTRUCTOR_ELT (expr, 0)->value;
5372 else
5373 gcc_unreachable ();
5376 if (type_unknown_p (expr))
5377 expr = instantiate_type (totype, expr, complain);
5378 /* Convert a constant to its underlying value, unless we are
5379 about to bind it to a reference, in which case we need to
5380 leave it as an lvalue. */
5381 if (inner >= 0)
5383 expr = decl_constant_value (expr);
5384 if (expr == null_node && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
5385 /* If __null has been converted to an integer type, we do not
5386 want to warn about uses of EXPR as an integer, rather than
5387 as a pointer. */
5388 expr = build_int_cst (totype, 0);
5390 return expr;
5391 case ck_ambig:
5392 if (complain & tf_error)
5394 /* Call build_user_type_conversion again for the error. */
5395 build_user_type_conversion (totype, convs->u.expr, LOOKUP_NORMAL);
5396 if (fn)
5397 error (" initializing argument %P of %q+D", argnum, fn);
5399 return error_mark_node;
5401 case ck_list:
5403 /* Conversion to std::initializer_list<T>. */
5404 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
5405 tree new_ctor = build_constructor (init_list_type_node, NULL);
5406 unsigned len = CONSTRUCTOR_NELTS (expr);
5407 tree array, val;
5408 VEC(tree,gc) *parms;
5409 unsigned ix;
5411 /* Convert all the elements. */
5412 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
5414 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
5415 1, false, false, complain);
5416 if (sub == error_mark_node)
5417 return sub;
5418 if (!BRACE_ENCLOSED_INITIALIZER_P (val))
5419 check_narrowing (TREE_TYPE (sub), val);
5420 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
5422 /* Build up the array. */
5423 elttype = cp_build_qualified_type
5424 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
5425 array = build_array_of_n_type (elttype, len);
5426 array = finish_compound_literal (array, new_ctor);
5428 parms = make_tree_vector ();
5429 VEC_safe_push (tree, gc, parms, decay_conversion (array));
5430 VEC_safe_push (tree, gc, parms, size_int (len));
5431 /* Call the private constructor. */
5432 push_deferring_access_checks (dk_no_check);
5433 new_ctor = build_special_member_call
5434 (NULL_TREE, complete_ctor_identifier, &parms, totype, 0, complain);
5435 release_tree_vector (parms);
5436 pop_deferring_access_checks ();
5437 return build_cplus_new (totype, new_ctor);
5440 case ck_aggr:
5441 return get_target_expr (digest_init (totype, expr));
5443 default:
5444 break;
5447 expr = convert_like_real (convs->u.next, expr, fn, argnum,
5448 convs->kind == ck_ref_bind ? -1 : 1,
5449 convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
5450 c_cast_p,
5451 complain);
5452 if (expr == error_mark_node)
5453 return error_mark_node;
5455 switch (convs->kind)
5457 case ck_rvalue:
5458 expr = decay_conversion (expr);
5459 if (! MAYBE_CLASS_TYPE_P (totype))
5460 return expr;
5461 /* Else fall through. */
5462 case ck_base:
5463 if (convs->kind == ck_base && !convs->need_temporary_p)
5465 /* We are going to bind a reference directly to a base-class
5466 subobject of EXPR. */
5467 /* Build an expression for `*((base*) &expr)'. */
5468 expr = cp_build_addr_expr (expr, complain);
5469 expr = convert_to_base (expr, build_pointer_type (totype),
5470 !c_cast_p, /*nonnull=*/true, complain);
5471 expr = cp_build_indirect_ref (expr, RO_IMPLICIT_CONVERSION, complain);
5472 return expr;
5475 /* Copy-initialization where the cv-unqualified version of the source
5476 type is the same class as, or a derived class of, the class of the
5477 destination [is treated as direct-initialization]. [dcl.init] */
5478 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
5479 if (convs->user_conv_p)
5480 /* This conversion is being done in the context of a user-defined
5481 conversion (i.e. the second step of copy-initialization), so
5482 don't allow any more. */
5483 flags |= LOOKUP_NO_CONVERSION;
5484 if (TREE_CODE (expr) == TARGET_EXPR
5485 && TARGET_EXPR_LIST_INIT_P (expr))
5486 /* Copy-list-initialization doesn't actually involve a copy. */
5487 return expr;
5488 expr = build_temp (expr, totype, flags, &diag_kind, complain);
5489 if (diag_kind && fn)
5491 if ((complain & tf_error))
5492 emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (fn), 0,
5493 " initializing argument %P of %qD", argnum, fn);
5494 else if (diag_kind == DK_ERROR)
5495 return error_mark_node;
5497 return build_cplus_new (totype, expr);
5499 case ck_ref_bind:
5501 tree ref_type = totype;
5503 if (convs->bad_p && TYPE_REF_IS_RVALUE (ref_type)
5504 && real_lvalue_p (expr))
5506 if (complain & tf_error)
5508 error ("cannot bind %qT lvalue to %qT",
5509 TREE_TYPE (expr), totype);
5510 if (fn)
5511 error (" initializing argument %P of %q+D", argnum, fn);
5513 return error_mark_node;
5516 /* If necessary, create a temporary.
5518 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
5519 that need temporaries, even when their types are reference
5520 compatible with the type of reference being bound, so the
5521 upcoming call to cp_build_addr_expr doesn't fail. */
5522 if (convs->need_temporary_p
5523 || TREE_CODE (expr) == CONSTRUCTOR
5524 || TREE_CODE (expr) == VA_ARG_EXPR)
5526 /* Otherwise, a temporary of type "cv1 T1" is created and
5527 initialized from the initializer expression using the rules
5528 for a non-reference copy-initialization (8.5). */
5530 tree type = TREE_TYPE (ref_type);
5531 cp_lvalue_kind lvalue = real_lvalue_p (expr);
5533 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5534 (type, convs->u.next->type));
5535 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
5536 && !TYPE_REF_IS_RVALUE (ref_type))
5538 if (complain & tf_error)
5540 /* If the reference is volatile or non-const, we
5541 cannot create a temporary. */
5542 if (lvalue & clk_bitfield)
5543 error ("cannot bind bitfield %qE to %qT",
5544 expr, ref_type);
5545 else if (lvalue & clk_packed)
5546 error ("cannot bind packed field %qE to %qT",
5547 expr, ref_type);
5548 else
5549 error ("cannot bind rvalue %qE to %qT", expr, ref_type);
5551 return error_mark_node;
5553 /* If the source is a packed field, and we must use a copy
5554 constructor, then building the target expr will require
5555 binding the field to the reference parameter to the
5556 copy constructor, and we'll end up with an infinite
5557 loop. If we can use a bitwise copy, then we'll be
5558 OK. */
5559 if ((lvalue & clk_packed)
5560 && CLASS_TYPE_P (type)
5561 && type_has_nontrivial_copy_init (type))
5563 if (complain & tf_error)
5564 error ("cannot bind packed field %qE to %qT",
5565 expr, ref_type);
5566 return error_mark_node;
5568 if (lvalue & clk_bitfield)
5570 expr = convert_bitfield_to_declared_type (expr);
5571 expr = fold_convert (type, expr);
5573 expr = build_target_expr_with_type (expr, type);
5576 /* Take the address of the thing to which we will bind the
5577 reference. */
5578 expr = cp_build_addr_expr (expr, complain);
5579 if (expr == error_mark_node)
5580 return error_mark_node;
5582 /* Convert it to a pointer to the type referred to by the
5583 reference. This will adjust the pointer if a derived to
5584 base conversion is being performed. */
5585 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
5586 expr);
5587 /* Convert the pointer to the desired reference type. */
5588 return build_nop (ref_type, expr);
5591 case ck_lvalue:
5592 return decay_conversion (expr);
5594 case ck_qual:
5595 /* Warn about deprecated conversion if appropriate. */
5596 string_conv_p (totype, expr, 1);
5597 break;
5599 case ck_ptr:
5600 if (convs->base_p)
5601 expr = convert_to_base (expr, totype, !c_cast_p,
5602 /*nonnull=*/false, complain);
5603 return build_nop (totype, expr);
5605 case ck_pmem:
5606 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
5607 c_cast_p, complain);
5609 default:
5610 break;
5613 if (convs->check_narrowing)
5614 check_narrowing (totype, expr);
5616 if (issue_conversion_warnings && (complain & tf_warning))
5617 expr = convert_and_check (totype, expr);
5618 else
5619 expr = convert (totype, expr);
5621 return expr;
5624 /* ARG is being passed to a varargs function. Perform any conversions
5625 required. Return the converted value. */
5627 tree
5628 convert_arg_to_ellipsis (tree arg)
5630 tree arg_type;
5632 /* [expr.call]
5634 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
5635 standard conversions are performed. */
5636 arg = decay_conversion (arg);
5637 arg_type = TREE_TYPE (arg);
5638 /* [expr.call]
5640 If the argument has integral or enumeration type that is subject
5641 to the integral promotions (_conv.prom_), or a floating point
5642 type that is subject to the floating point promotion
5643 (_conv.fpprom_), the value of the argument is converted to the
5644 promoted type before the call. */
5645 if (TREE_CODE (arg_type) == REAL_TYPE
5646 && (TYPE_PRECISION (arg_type)
5647 < TYPE_PRECISION (double_type_node))
5648 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
5650 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5651 warning (OPT_Wdouble_promotion,
5652 "implicit conversion from %qT to %qT when passing "
5653 "argument to function",
5654 arg_type, double_type_node);
5655 arg = convert_to_real (double_type_node, arg);
5657 else if (NULLPTR_TYPE_P (arg_type))
5658 arg = null_pointer_node;
5659 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
5660 arg = perform_integral_promotions (arg);
5662 arg = require_complete_type (arg);
5663 arg_type = TREE_TYPE (arg);
5665 if (arg != error_mark_node
5666 && (type_has_nontrivial_copy_init (arg_type)
5667 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type)))
5669 /* [expr.call] 5.2.2/7:
5670 Passing a potentially-evaluated argument of class type (Clause 9)
5671 with a non-trivial copy constructor or a non-trivial destructor
5672 with no corresponding parameter is conditionally-supported, with
5673 implementation-defined semantics.
5675 We used to just warn here and do a bitwise copy, but now
5676 cp_expr_size will abort if we try to do that.
5678 If the call appears in the context of a sizeof expression,
5679 it is not potentially-evaluated. */
5680 if (cp_unevaluated_operand == 0)
5681 error ("cannot pass objects of non-trivially-copyable "
5682 "type %q#T through %<...%>", arg_type);
5685 return arg;
5688 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
5690 tree
5691 build_x_va_arg (tree expr, tree type)
5693 if (processing_template_decl)
5694 return build_min (VA_ARG_EXPR, type, expr);
5696 type = complete_type_or_else (type, NULL_TREE);
5698 if (expr == error_mark_node || !type)
5699 return error_mark_node;
5701 expr = mark_lvalue_use (expr);
5703 if (type_has_nontrivial_copy_init (type)
5704 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5705 || TREE_CODE (type) == REFERENCE_TYPE)
5707 /* Remove reference types so we don't ICE later on. */
5708 tree type1 = non_reference (type);
5709 /* conditionally-supported behavior [expr.call] 5.2.2/7. */
5710 error ("cannot receive objects of non-trivially-copyable type %q#T "
5711 "through %<...%>; ", type);
5712 expr = convert (build_pointer_type (type1), null_node);
5713 expr = cp_build_indirect_ref (expr, RO_NULL, tf_warning_or_error);
5714 return expr;
5717 return build_va_arg (input_location, expr, type);
5720 /* TYPE has been given to va_arg. Apply the default conversions which
5721 would have happened when passed via ellipsis. Return the promoted
5722 type, or the passed type if there is no change. */
5724 tree
5725 cxx_type_promotes_to (tree type)
5727 tree promote;
5729 /* Perform the array-to-pointer and function-to-pointer
5730 conversions. */
5731 type = type_decays_to (type);
5733 promote = type_promotes_to (type);
5734 if (same_type_p (type, promote))
5735 promote = type;
5737 return promote;
5740 /* ARG is a default argument expression being passed to a parameter of
5741 the indicated TYPE, which is a parameter to FN. Do any required
5742 conversions. Return the converted value. */
5744 static GTY(()) VEC(tree,gc) *default_arg_context;
5746 tree
5747 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
5749 int i;
5750 tree t;
5752 /* If the ARG is an unparsed default argument expression, the
5753 conversion cannot be performed. */
5754 if (TREE_CODE (arg) == DEFAULT_ARG)
5756 error ("the default argument for parameter %d of %qD has "
5757 "not yet been parsed",
5758 parmnum, fn);
5759 return error_mark_node;
5762 /* Detect recursion. */
5763 FOR_EACH_VEC_ELT (tree, default_arg_context, i, t)
5764 if (t == fn)
5766 error ("recursive evaluation of default argument for %q#D", fn);
5767 return error_mark_node;
5769 VEC_safe_push (tree, gc, default_arg_context, fn);
5771 if (fn && DECL_TEMPLATE_INFO (fn))
5772 arg = tsubst_default_argument (fn, type, arg);
5774 /* Due to:
5776 [dcl.fct.default]
5778 The names in the expression are bound, and the semantic
5779 constraints are checked, at the point where the default
5780 expressions appears.
5782 we must not perform access checks here. */
5783 push_deferring_access_checks (dk_no_check);
5784 arg = break_out_target_exprs (arg);
5785 if (TREE_CODE (arg) == CONSTRUCTOR)
5787 arg = digest_init (type, arg);
5788 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
5789 ICR_DEFAULT_ARGUMENT, fn, parmnum,
5790 tf_warning_or_error);
5792 else
5794 /* We must make a copy of ARG, in case subsequent processing
5795 alters any part of it. For example, during gimplification a
5796 cast of the form (T) &X::f (where "f" is a member function)
5797 will lead to replacing the PTRMEM_CST for &X::f with a
5798 VAR_DECL. We can avoid the copy for constants, since they
5799 are never modified in place. */
5800 if (!CONSTANT_CLASS_P (arg))
5801 arg = unshare_expr (arg);
5802 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
5803 ICR_DEFAULT_ARGUMENT, fn, parmnum,
5804 tf_warning_or_error);
5805 arg = convert_for_arg_passing (type, arg);
5807 pop_deferring_access_checks();
5809 VEC_pop (tree, default_arg_context);
5811 return arg;
5814 /* Returns the type which will really be used for passing an argument of
5815 type TYPE. */
5817 tree
5818 type_passed_as (tree type)
5820 /* Pass classes with copy ctors by invisible reference. */
5821 if (TREE_ADDRESSABLE (type))
5823 type = build_reference_type (type);
5824 /* There are no other pointers to this temporary. */
5825 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
5827 else if (targetm.calls.promote_prototypes (type)
5828 && INTEGRAL_TYPE_P (type)
5829 && COMPLETE_TYPE_P (type)
5830 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
5831 TYPE_SIZE (integer_type_node)))
5832 type = integer_type_node;
5834 return type;
5837 /* Actually perform the appropriate conversion. */
5839 tree
5840 convert_for_arg_passing (tree type, tree val)
5842 tree bitfield_type;
5844 /* If VAL is a bitfield, then -- since it has already been converted
5845 to TYPE -- it cannot have a precision greater than TYPE.
5847 If it has a smaller precision, we must widen it here. For
5848 example, passing "int f:3;" to a function expecting an "int" will
5849 not result in any conversion before this point.
5851 If the precision is the same we must not risk widening. For
5852 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
5853 often have type "int", even though the C++ type for the field is
5854 "long long". If the value is being passed to a function
5855 expecting an "int", then no conversions will be required. But,
5856 if we call convert_bitfield_to_declared_type, the bitfield will
5857 be converted to "long long". */
5858 bitfield_type = is_bitfield_expr_with_lowered_type (val);
5859 if (bitfield_type
5860 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
5861 val = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type), val);
5863 if (val == error_mark_node)
5865 /* Pass classes with copy ctors by invisible reference. */
5866 else if (TREE_ADDRESSABLE (type))
5867 val = build1 (ADDR_EXPR, build_reference_type (type), val);
5868 else if (targetm.calls.promote_prototypes (type)
5869 && INTEGRAL_TYPE_P (type)
5870 && COMPLETE_TYPE_P (type)
5871 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
5872 TYPE_SIZE (integer_type_node)))
5873 val = perform_integral_promotions (val);
5874 if (warn_missing_format_attribute)
5876 tree rhstype = TREE_TYPE (val);
5877 const enum tree_code coder = TREE_CODE (rhstype);
5878 const enum tree_code codel = TREE_CODE (type);
5879 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5880 && coder == codel
5881 && check_missing_format_attribute (type, rhstype))
5882 warning (OPT_Wmissing_format_attribute,
5883 "argument of function call might be a candidate for a format attribute");
5885 return val;
5888 /* Returns true iff FN is a function with magic varargs, i.e. ones for
5889 which no conversions at all should be done. This is true for some
5890 builtins which don't act like normal functions. */
5892 static bool
5893 magic_varargs_p (tree fn)
5895 if (DECL_BUILT_IN (fn))
5896 switch (DECL_FUNCTION_CODE (fn))
5898 case BUILT_IN_CLASSIFY_TYPE:
5899 case BUILT_IN_CONSTANT_P:
5900 case BUILT_IN_NEXT_ARG:
5901 case BUILT_IN_VA_START:
5902 return true;
5904 default:;
5905 return lookup_attribute ("type generic",
5906 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
5909 return false;
5912 /* Subroutine of the various build_*_call functions. Overload resolution
5913 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
5914 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
5915 bitmask of various LOOKUP_* flags which apply to the call itself. */
5917 static tree
5918 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
5920 tree fn = cand->fn;
5921 const VEC(tree,gc) *args = cand->args;
5922 tree first_arg = cand->first_arg;
5923 conversion **convs = cand->convs;
5924 conversion *conv;
5925 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
5926 int parmlen;
5927 tree val;
5928 int i = 0;
5929 int j = 0;
5930 unsigned int arg_index = 0;
5931 int is_method = 0;
5932 int nargs;
5933 tree *argarray;
5934 bool already_used = false;
5936 /* In a template, there is no need to perform all of the work that
5937 is normally done. We are only interested in the type of the call
5938 expression, i.e., the return type of the function. Any semantic
5939 errors will be deferred until the template is instantiated. */
5940 if (processing_template_decl)
5942 tree expr;
5943 tree return_type;
5944 const tree *argarray;
5945 unsigned int nargs;
5947 return_type = TREE_TYPE (TREE_TYPE (fn));
5948 nargs = VEC_length (tree, args);
5949 if (first_arg == NULL_TREE)
5950 argarray = VEC_address (tree, CONST_CAST (VEC(tree,gc) *, args));
5951 else
5953 tree *alcarray;
5954 unsigned int ix;
5955 tree arg;
5957 ++nargs;
5958 alcarray = XALLOCAVEC (tree, nargs);
5959 alcarray[0] = first_arg;
5960 FOR_EACH_VEC_ELT (tree, args, ix, arg)
5961 alcarray[ix + 1] = arg;
5962 argarray = alcarray;
5964 expr = build_call_array_loc (input_location,
5965 return_type, build_addr_func (fn), nargs,
5966 argarray);
5967 if (TREE_THIS_VOLATILE (fn) && cfun)
5968 current_function_returns_abnormally = 1;
5969 if (!VOID_TYPE_P (return_type))
5970 require_complete_type_sfinae (return_type, complain);
5971 return convert_from_reference (expr);
5974 /* Give any warnings we noticed during overload resolution. */
5975 if (cand->warnings && (complain & tf_warning))
5977 struct candidate_warning *w;
5978 for (w = cand->warnings; w; w = w->next)
5979 joust (cand, w->loser, 1);
5982 /* Make =delete work with SFINAE. */
5983 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
5984 return error_mark_node;
5986 if (DECL_FUNCTION_MEMBER_P (fn))
5988 tree access_fn;
5989 /* If FN is a template function, two cases must be considered.
5990 For example:
5992 struct A {
5993 protected:
5994 template <class T> void f();
5996 template <class T> struct B {
5997 protected:
5998 void g();
6000 struct C : A, B<int> {
6001 using A::f; // #1
6002 using B<int>::g; // #2
6005 In case #1 where `A::f' is a member template, DECL_ACCESS is
6006 recorded in the primary template but not in its specialization.
6007 We check access of FN using its primary template.
6009 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
6010 because it is a member of class template B, DECL_ACCESS is
6011 recorded in the specialization `B<int>::g'. We cannot use its
6012 primary template because `B<T>::g' and `B<int>::g' may have
6013 different access. */
6014 if (DECL_TEMPLATE_INFO (fn)
6015 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
6016 access_fn = DECL_TI_TEMPLATE (fn);
6017 else
6018 access_fn = fn;
6019 if (flags & LOOKUP_SPECULATIVE)
6021 if (!speculative_access_check (cand->access_path, access_fn, fn,
6022 !!(flags & LOOKUP_COMPLAIN)))
6023 return error_mark_node;
6025 else
6026 perform_or_defer_access_check (cand->access_path, access_fn, fn);
6029 /* If we're checking for implicit delete, don't bother with argument
6030 conversions. */
6031 if (flags & LOOKUP_SPECULATIVE)
6033 if (DECL_DELETED_FN (fn))
6035 if (flags & LOOKUP_COMPLAIN)
6036 mark_used (fn);
6037 return error_mark_node;
6039 if (cand->viable == 1)
6040 return fn;
6041 else if (!(flags & LOOKUP_COMPLAIN))
6042 /* Reject bad conversions now. */
6043 return error_mark_node;
6044 /* else continue to get conversion error. */
6047 /* Find maximum size of vector to hold converted arguments. */
6048 parmlen = list_length (parm);
6049 nargs = VEC_length (tree, args) + (first_arg != NULL_TREE ? 1 : 0);
6050 if (parmlen > nargs)
6051 nargs = parmlen;
6052 argarray = XALLOCAVEC (tree, nargs);
6054 /* The implicit parameters to a constructor are not considered by overload
6055 resolution, and must be of the proper type. */
6056 if (DECL_CONSTRUCTOR_P (fn))
6058 if (first_arg != NULL_TREE)
6060 argarray[j++] = first_arg;
6061 first_arg = NULL_TREE;
6063 else
6065 argarray[j++] = VEC_index (tree, args, arg_index);
6066 ++arg_index;
6068 parm = TREE_CHAIN (parm);
6069 /* We should never try to call the abstract constructor. */
6070 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
6072 if (DECL_HAS_VTT_PARM_P (fn))
6074 argarray[j++] = VEC_index (tree, args, arg_index);
6075 ++arg_index;
6076 parm = TREE_CHAIN (parm);
6079 /* Bypass access control for 'this' parameter. */
6080 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6082 tree parmtype = TREE_VALUE (parm);
6083 tree arg = (first_arg != NULL_TREE
6084 ? first_arg
6085 : VEC_index (tree, args, arg_index));
6086 tree argtype = TREE_TYPE (arg);
6087 tree converted_arg;
6088 tree base_binfo;
6090 if (convs[i]->bad_p)
6092 if (complain & tf_error)
6093 permerror (input_location, "passing %qT as %<this%> argument of %q#D discards qualifiers",
6094 TREE_TYPE (argtype), fn);
6095 else
6096 return error_mark_node;
6099 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
6100 X is called for an object that is not of type X, or of a type
6101 derived from X, the behavior is undefined.
6103 So we can assume that anything passed as 'this' is non-null, and
6104 optimize accordingly. */
6105 gcc_assert (TREE_CODE (parmtype) == POINTER_TYPE);
6106 /* Convert to the base in which the function was declared. */
6107 gcc_assert (cand->conversion_path != NULL_TREE);
6108 converted_arg = build_base_path (PLUS_EXPR,
6109 arg,
6110 cand->conversion_path,
6112 /* Check that the base class is accessible. */
6113 if (!accessible_base_p (TREE_TYPE (argtype),
6114 BINFO_TYPE (cand->conversion_path), true))
6115 error ("%qT is not an accessible base of %qT",
6116 BINFO_TYPE (cand->conversion_path),
6117 TREE_TYPE (argtype));
6118 /* If fn was found by a using declaration, the conversion path
6119 will be to the derived class, not the base declaring fn. We
6120 must convert from derived to base. */
6121 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
6122 TREE_TYPE (parmtype), ba_unique, NULL);
6123 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
6124 base_binfo, 1);
6126 argarray[j++] = converted_arg;
6127 parm = TREE_CHAIN (parm);
6128 if (first_arg != NULL_TREE)
6129 first_arg = NULL_TREE;
6130 else
6131 ++arg_index;
6132 ++i;
6133 is_method = 1;
6136 gcc_assert (first_arg == NULL_TREE);
6137 for (; arg_index < VEC_length (tree, args) && parm;
6138 parm = TREE_CHAIN (parm), ++arg_index, ++i)
6140 tree type = TREE_VALUE (parm);
6141 tree arg = VEC_index (tree, args, arg_index);
6143 conv = convs[i];
6145 /* Don't make a copy here if build_call is going to. */
6146 if (conv->kind == ck_rvalue
6147 && COMPLETE_TYPE_P (complete_type (type))
6148 && !TREE_ADDRESSABLE (type))
6149 conv = conv->u.next;
6151 /* Warn about initializer_list deduction that isn't currently in the
6152 working draft. */
6153 if (cxx_dialect > cxx98
6154 && flag_deduce_init_list
6155 && cand->template_decl
6156 && is_std_init_list (non_reference (type))
6157 && BRACE_ENCLOSED_INITIALIZER_P (arg))
6159 tree tmpl = TI_TEMPLATE (cand->template_decl);
6160 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
6161 tree patparm = get_pattern_parm (realparm, tmpl);
6162 tree pattype = TREE_TYPE (patparm);
6163 if (PACK_EXPANSION_P (pattype))
6164 pattype = PACK_EXPANSION_PATTERN (pattype);
6165 pattype = non_reference (pattype);
6167 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
6168 && (cand->explicit_targs == NULL_TREE
6169 || (TREE_VEC_LENGTH (cand->explicit_targs)
6170 <= TEMPLATE_TYPE_IDX (pattype))))
6172 pedwarn (input_location, 0, "deducing %qT as %qT",
6173 non_reference (TREE_TYPE (patparm)),
6174 non_reference (type));
6175 pedwarn (input_location, 0, " in call to %q+D", cand->fn);
6176 pedwarn (input_location, 0,
6177 " (you can disable this with -fno-deduce-init-list)");
6181 val = convert_like_with_context (conv, arg, fn, i-is_method, complain);
6183 val = convert_for_arg_passing (type, val);
6184 if (val == error_mark_node)
6185 return error_mark_node;
6186 else
6187 argarray[j++] = val;
6190 /* Default arguments */
6191 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
6192 argarray[j++] = convert_default_arg (TREE_VALUE (parm),
6193 TREE_PURPOSE (parm),
6194 fn, i - is_method);
6195 /* Ellipsis */
6196 for (; arg_index < VEC_length (tree, args); ++arg_index)
6198 tree a = VEC_index (tree, args, arg_index);
6199 if (magic_varargs_p (fn))
6200 /* Do no conversions for magic varargs. */
6201 a = mark_type_use (a);
6202 else
6203 a = convert_arg_to_ellipsis (a);
6204 argarray[j++] = a;
6207 gcc_assert (j <= nargs);
6208 nargs = j;
6210 check_function_arguments (TYPE_ATTRIBUTES (TREE_TYPE (fn)),
6211 nargs, argarray, TYPE_ARG_TYPES (TREE_TYPE (fn)));
6213 /* Avoid actually calling copy constructors and copy assignment operators,
6214 if possible. */
6216 if (! flag_elide_constructors)
6217 /* Do things the hard way. */;
6218 else if (cand->num_convs == 1
6219 && (DECL_COPY_CONSTRUCTOR_P (fn)
6220 || DECL_MOVE_CONSTRUCTOR_P (fn)))
6222 tree targ;
6223 tree arg = argarray[num_artificial_parms_for (fn)];
6224 tree fa;
6225 bool trivial = trivial_fn_p (fn);
6227 /* Pull out the real argument, disregarding const-correctness. */
6228 targ = arg;
6229 while (CONVERT_EXPR_P (targ)
6230 || TREE_CODE (targ) == NON_LVALUE_EXPR)
6231 targ = TREE_OPERAND (targ, 0);
6232 if (TREE_CODE (targ) == ADDR_EXPR)
6234 targ = TREE_OPERAND (targ, 0);
6235 if (!same_type_ignoring_top_level_qualifiers_p
6236 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
6237 targ = NULL_TREE;
6239 else
6240 targ = NULL_TREE;
6242 if (targ)
6243 arg = targ;
6244 else
6245 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
6247 /* [class.copy]: the copy constructor is implicitly defined even if
6248 the implementation elided its use. */
6249 if (!trivial || DECL_DELETED_FN (fn))
6251 mark_used (fn);
6252 already_used = true;
6255 /* If we're creating a temp and we already have one, don't create a
6256 new one. If we're not creating a temp but we get one, use
6257 INIT_EXPR to collapse the temp into our target. Otherwise, if the
6258 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
6259 temp or an INIT_EXPR otherwise. */
6260 fa = argarray[0];
6261 if (integer_zerop (fa))
6263 if (TREE_CODE (arg) == TARGET_EXPR)
6264 return arg;
6265 else if (trivial)
6266 return force_target_expr (DECL_CONTEXT (fn), arg);
6268 else if (TREE_CODE (arg) == TARGET_EXPR || trivial)
6270 tree to = stabilize_reference (cp_build_indirect_ref (fa, RO_NULL,
6271 complain));
6273 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
6274 return val;
6277 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
6278 && trivial_fn_p (fn)
6279 && !DECL_DELETED_FN (fn))
6281 tree to = stabilize_reference
6282 (cp_build_indirect_ref (argarray[0], RO_NULL, complain));
6283 tree type = TREE_TYPE (to);
6284 tree as_base = CLASSTYPE_AS_BASE (type);
6285 tree arg = argarray[1];
6287 if (is_really_empty_class (type))
6289 /* Avoid copying empty classes. */
6290 val = build2 (COMPOUND_EXPR, void_type_node, to, arg);
6291 TREE_NO_WARNING (val) = 1;
6292 val = build2 (COMPOUND_EXPR, type, val, to);
6293 TREE_NO_WARNING (val) = 1;
6295 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
6297 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
6298 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
6300 else
6302 /* We must only copy the non-tail padding parts.
6303 Use __builtin_memcpy for the bitwise copy.
6304 FIXME fix 22488 so we can go back to using MODIFY_EXPR
6305 instead of an explicit call to memcpy. */
6307 tree arg0, arg1, arg2, t;
6308 tree test = NULL_TREE;
6310 arg2 = TYPE_SIZE_UNIT (as_base);
6311 arg1 = arg;
6312 arg0 = cp_build_addr_expr (to, complain);
6314 if (!can_trust_pointer_alignment ())
6316 /* If we can't be sure about pointer alignment, a call
6317 to __builtin_memcpy is expanded as a call to memcpy, which
6318 is invalid with identical args. Otherwise it is
6319 expanded as a block move, which should be safe. */
6320 arg0 = save_expr (arg0);
6321 arg1 = save_expr (arg1);
6322 test = build2 (EQ_EXPR, boolean_type_node, arg0, arg1);
6324 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
6325 t = build_call_n (t, 3, arg0, arg1, arg2);
6327 t = convert (TREE_TYPE (arg0), t);
6328 if (test)
6329 t = build3 (COND_EXPR, TREE_TYPE (t), test, arg0, t);
6330 val = cp_build_indirect_ref (t, RO_NULL, complain);
6331 TREE_NO_WARNING (val) = 1;
6334 return val;
6336 /* FIXME handle trivial default constructor and destructor, too. */
6338 if (!already_used)
6339 mark_used (fn);
6341 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
6343 tree t;
6344 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
6345 DECL_CONTEXT (fn),
6346 ba_any, NULL);
6347 gcc_assert (binfo && binfo != error_mark_node);
6349 /* Warn about deprecated virtual functions now, since we're about
6350 to throw away the decl. */
6351 if (TREE_DEPRECATED (fn))
6352 warn_deprecated_use (fn, NULL_TREE);
6354 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1);
6355 if (TREE_SIDE_EFFECTS (argarray[0]))
6356 argarray[0] = save_expr (argarray[0]);
6357 t = build_pointer_type (TREE_TYPE (fn));
6358 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
6359 fn = build_java_interface_fn_ref (fn, argarray[0]);
6360 else
6361 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
6362 TREE_TYPE (fn) = t;
6364 else
6365 fn = build_addr_func (fn);
6367 return build_cxx_call (fn, nargs, argarray);
6370 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
6371 This function performs no overload resolution, conversion, or other
6372 high-level operations. */
6374 tree
6375 build_cxx_call (tree fn, int nargs, tree *argarray)
6377 tree fndecl;
6379 fn = build_call_a (fn, nargs, argarray);
6381 /* If this call might throw an exception, note that fact. */
6382 fndecl = get_callee_fndecl (fn);
6383 if ((!fndecl || !TREE_NOTHROW (fndecl))
6384 && at_function_scope_p ()
6385 && cfun
6386 && cp_function_chain)
6387 cp_function_chain->can_throw = 1;
6389 /* Check that arguments to builtin functions match the expectations. */
6390 if (fndecl
6391 && DECL_BUILT_IN (fndecl)
6392 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6393 && !check_builtin_function_arguments (fndecl, nargs, argarray))
6394 return error_mark_node;
6396 /* Some built-in function calls will be evaluated at compile-time in
6397 fold (). */
6398 fn = fold_if_not_in_template (fn);
6400 if (VOID_TYPE_P (TREE_TYPE (fn)))
6401 return fn;
6403 fn = require_complete_type (fn);
6404 if (fn == error_mark_node)
6405 return error_mark_node;
6407 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
6408 fn = build_cplus_new (TREE_TYPE (fn), fn);
6409 return convert_from_reference (fn);
6412 static GTY(()) tree java_iface_lookup_fn;
6414 /* Make an expression which yields the address of the Java interface
6415 method FN. This is achieved by generating a call to libjava's
6416 _Jv_LookupInterfaceMethodIdx(). */
6418 static tree
6419 build_java_interface_fn_ref (tree fn, tree instance)
6421 tree lookup_fn, method, idx;
6422 tree klass_ref, iface, iface_ref;
6423 int i;
6425 if (!java_iface_lookup_fn)
6427 tree ftype = build_function_type_list (ptr_type_node,
6428 ptr_type_node, ptr_type_node,
6429 java_int_type_node, NULL_TREE);
6430 java_iface_lookup_fn
6431 = add_builtin_function ("_Jv_LookupInterfaceMethodIdx", ftype,
6432 0, NOT_BUILT_IN, NULL, NULL_TREE);
6435 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
6436 This is the first entry in the vtable. */
6437 klass_ref = build_vtbl_ref (cp_build_indirect_ref (instance, RO_NULL,
6438 tf_warning_or_error),
6439 integer_zero_node);
6441 /* Get the java.lang.Class pointer for the interface being called. */
6442 iface = DECL_CONTEXT (fn);
6443 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
6444 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
6445 || DECL_CONTEXT (iface_ref) != iface)
6447 error ("could not find class$ field in java interface type %qT",
6448 iface);
6449 return error_mark_node;
6451 iface_ref = build_address (iface_ref);
6452 iface_ref = convert (build_pointer_type (iface), iface_ref);
6454 /* Determine the itable index of FN. */
6455 i = 1;
6456 for (method = TYPE_METHODS (iface); method; method = DECL_CHAIN (method))
6458 if (!DECL_VIRTUAL_P (method))
6459 continue;
6460 if (fn == method)
6461 break;
6462 i++;
6464 idx = build_int_cst (NULL_TREE, i);
6466 lookup_fn = build1 (ADDR_EXPR,
6467 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
6468 java_iface_lookup_fn);
6469 return build_call_nary (ptr_type_node, lookup_fn,
6470 3, klass_ref, iface_ref, idx);
6473 /* Returns the value to use for the in-charge parameter when making a
6474 call to a function with the indicated NAME.
6476 FIXME:Can't we find a neater way to do this mapping? */
6478 tree
6479 in_charge_arg_for_name (tree name)
6481 if (name == base_ctor_identifier
6482 || name == base_dtor_identifier)
6483 return integer_zero_node;
6484 else if (name == complete_ctor_identifier)
6485 return integer_one_node;
6486 else if (name == complete_dtor_identifier)
6487 return integer_two_node;
6488 else if (name == deleting_dtor_identifier)
6489 return integer_three_node;
6491 /* This function should only be called with one of the names listed
6492 above. */
6493 gcc_unreachable ();
6494 return NULL_TREE;
6497 /* Build a call to a constructor, destructor, or an assignment
6498 operator for INSTANCE, an expression with class type. NAME
6499 indicates the special member function to call; *ARGS are the
6500 arguments. ARGS may be NULL. This may change ARGS. BINFO
6501 indicates the base of INSTANCE that is to be passed as the `this'
6502 parameter to the member function called.
6504 FLAGS are the LOOKUP_* flags to use when processing the call.
6506 If NAME indicates a complete object constructor, INSTANCE may be
6507 NULL_TREE. In this case, the caller will call build_cplus_new to
6508 store the newly constructed object into a VAR_DECL. */
6510 tree
6511 build_special_member_call (tree instance, tree name, VEC(tree,gc) **args,
6512 tree binfo, int flags, tsubst_flags_t complain)
6514 tree fns;
6515 /* The type of the subobject to be constructed or destroyed. */
6516 tree class_type;
6517 VEC(tree,gc) *allocated = NULL;
6518 tree ret;
6520 gcc_assert (name == complete_ctor_identifier
6521 || name == base_ctor_identifier
6522 || name == complete_dtor_identifier
6523 || name == base_dtor_identifier
6524 || name == deleting_dtor_identifier
6525 || name == ansi_assopname (NOP_EXPR));
6526 if (TYPE_P (binfo))
6528 /* Resolve the name. */
6529 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
6530 return error_mark_node;
6532 binfo = TYPE_BINFO (binfo);
6535 gcc_assert (binfo != NULL_TREE);
6537 class_type = BINFO_TYPE (binfo);
6539 /* Handle the special case where INSTANCE is NULL_TREE. */
6540 if (name == complete_ctor_identifier && !instance)
6542 instance = build_int_cst (build_pointer_type (class_type), 0);
6543 instance = build1 (INDIRECT_REF, class_type, instance);
6545 else
6547 if (name == complete_dtor_identifier
6548 || name == base_dtor_identifier
6549 || name == deleting_dtor_identifier)
6550 gcc_assert (args == NULL || VEC_empty (tree, *args));
6552 /* Convert to the base class, if necessary. */
6553 if (!same_type_ignoring_top_level_qualifiers_p
6554 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
6556 if (name != ansi_assopname (NOP_EXPR))
6557 /* For constructors and destructors, either the base is
6558 non-virtual, or it is virtual but we are doing the
6559 conversion from a constructor or destructor for the
6560 complete object. In either case, we can convert
6561 statically. */
6562 instance = convert_to_base_statically (instance, binfo);
6563 else
6564 /* However, for assignment operators, we must convert
6565 dynamically if the base is virtual. */
6566 instance = build_base_path (PLUS_EXPR, instance,
6567 binfo, /*nonnull=*/1);
6571 gcc_assert (instance != NULL_TREE);
6573 fns = lookup_fnfields (binfo, name, 1);
6575 /* When making a call to a constructor or destructor for a subobject
6576 that uses virtual base classes, pass down a pointer to a VTT for
6577 the subobject. */
6578 if ((name == base_ctor_identifier
6579 || name == base_dtor_identifier)
6580 && CLASSTYPE_VBASECLASSES (class_type))
6582 tree vtt;
6583 tree sub_vtt;
6585 /* If the current function is a complete object constructor
6586 or destructor, then we fetch the VTT directly.
6587 Otherwise, we look it up using the VTT we were given. */
6588 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
6589 vtt = decay_conversion (vtt);
6590 vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
6591 build2 (EQ_EXPR, boolean_type_node,
6592 current_in_charge_parm, integer_zero_node),
6593 current_vtt_parm,
6594 vtt);
6595 gcc_assert (BINFO_SUBVTT_INDEX (binfo));
6596 sub_vtt = build2 (POINTER_PLUS_EXPR, TREE_TYPE (vtt), vtt,
6597 BINFO_SUBVTT_INDEX (binfo));
6599 if (args == NULL)
6601 allocated = make_tree_vector ();
6602 args = &allocated;
6605 VEC_safe_insert (tree, gc, *args, 0, sub_vtt);
6608 ret = build_new_method_call (instance, fns, args,
6609 TYPE_BINFO (BINFO_TYPE (binfo)),
6610 flags, /*fn=*/NULL,
6611 complain);
6613 if (allocated != NULL)
6614 release_tree_vector (allocated);
6616 return ret;
6619 /* Return the NAME, as a C string. The NAME indicates a function that
6620 is a member of TYPE. *FREE_P is set to true if the caller must
6621 free the memory returned.
6623 Rather than go through all of this, we should simply set the names
6624 of constructors and destructors appropriately, and dispense with
6625 ctor_identifier, dtor_identifier, etc. */
6627 static char *
6628 name_as_c_string (tree name, tree type, bool *free_p)
6630 char *pretty_name;
6632 /* Assume that we will not allocate memory. */
6633 *free_p = false;
6634 /* Constructors and destructors are special. */
6635 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
6637 pretty_name
6638 = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type))));
6639 /* For a destructor, add the '~'. */
6640 if (name == complete_dtor_identifier
6641 || name == base_dtor_identifier
6642 || name == deleting_dtor_identifier)
6644 pretty_name = concat ("~", pretty_name, NULL);
6645 /* Remember that we need to free the memory allocated. */
6646 *free_p = true;
6649 else if (IDENTIFIER_TYPENAME_P (name))
6651 pretty_name = concat ("operator ",
6652 type_as_string_translate (TREE_TYPE (name),
6653 TFF_PLAIN_IDENTIFIER),
6654 NULL);
6655 /* Remember that we need to free the memory allocated. */
6656 *free_p = true;
6658 else
6659 pretty_name = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (name)));
6661 return pretty_name;
6664 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
6665 be set, upon return, to the function called. ARGS may be NULL.
6666 This may change ARGS. */
6668 tree
6669 build_new_method_call (tree instance, tree fns, VEC(tree,gc) **args,
6670 tree conversion_path, int flags,
6671 tree *fn_p, tsubst_flags_t complain)
6673 struct z_candidate *candidates = 0, *cand;
6674 tree explicit_targs = NULL_TREE;
6675 tree basetype = NULL_TREE;
6676 tree access_binfo;
6677 tree optype;
6678 tree first_mem_arg = NULL_TREE;
6679 tree instance_ptr;
6680 tree name;
6681 bool skip_first_for_error;
6682 VEC(tree,gc) *user_args;
6683 tree call;
6684 tree fn;
6685 int template_only = 0;
6686 bool any_viable_p;
6687 tree orig_instance;
6688 tree orig_fns;
6689 VEC(tree,gc) *orig_args = NULL;
6690 void *p;
6692 gcc_assert (instance != NULL_TREE);
6694 /* We don't know what function we're going to call, yet. */
6695 if (fn_p)
6696 *fn_p = NULL_TREE;
6698 if (error_operand_p (instance)
6699 || !fns || error_operand_p (fns))
6700 return error_mark_node;
6702 if (!BASELINK_P (fns))
6704 if (complain & tf_error)
6705 error ("call to non-function %qD", fns);
6706 return error_mark_node;
6709 orig_instance = instance;
6710 orig_fns = fns;
6712 /* Dismantle the baselink to collect all the information we need. */
6713 if (!conversion_path)
6714 conversion_path = BASELINK_BINFO (fns);
6715 access_binfo = BASELINK_ACCESS_BINFO (fns);
6716 optype = BASELINK_OPTYPE (fns);
6717 fns = BASELINK_FUNCTIONS (fns);
6718 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
6720 explicit_targs = TREE_OPERAND (fns, 1);
6721 fns = TREE_OPERAND (fns, 0);
6722 template_only = 1;
6724 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
6725 || TREE_CODE (fns) == TEMPLATE_DECL
6726 || TREE_CODE (fns) == OVERLOAD);
6727 fn = get_first_fn (fns);
6728 name = DECL_NAME (fn);
6730 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
6731 gcc_assert (CLASS_TYPE_P (basetype));
6733 if (processing_template_decl)
6735 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
6736 instance = build_non_dependent_expr (instance);
6737 if (args != NULL)
6738 make_args_non_dependent (*args);
6741 user_args = args == NULL ? NULL : *args;
6742 /* Under DR 147 A::A() is an invalid constructor call,
6743 not a functional cast. */
6744 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
6746 if (! (complain & tf_error))
6747 return error_mark_node;
6749 permerror (input_location,
6750 "cannot call constructor %<%T::%D%> directly",
6751 basetype, name);
6752 permerror (input_location, " for a function-style cast, remove the "
6753 "redundant %<::%D%>", name);
6754 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
6755 complain);
6756 return call;
6759 /* Figure out whether to skip the first argument for the error
6760 message we will display to users if an error occurs. We don't
6761 want to display any compiler-generated arguments. The "this"
6762 pointer hasn't been added yet. However, we must remove the VTT
6763 pointer if this is a call to a base-class constructor or
6764 destructor. */
6765 skip_first_for_error = false;
6766 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
6768 /* Callers should explicitly indicate whether they want to construct
6769 the complete object or just the part without virtual bases. */
6770 gcc_assert (name != ctor_identifier);
6771 /* Similarly for destructors. */
6772 gcc_assert (name != dtor_identifier);
6773 /* Remove the VTT pointer, if present. */
6774 if ((name == base_ctor_identifier || name == base_dtor_identifier)
6775 && CLASSTYPE_VBASECLASSES (basetype))
6776 skip_first_for_error = true;
6779 /* Process the argument list. */
6780 if (args != NULL && *args != NULL)
6782 *args = resolve_args (*args);
6783 if (*args == NULL)
6784 return error_mark_node;
6787 instance_ptr = build_this (instance);
6789 /* It's OK to call destructors and constructors on cv-qualified objects.
6790 Therefore, convert the INSTANCE_PTR to the unqualified type, if
6791 necessary. */
6792 if (DECL_DESTRUCTOR_P (fn)
6793 || DECL_CONSTRUCTOR_P (fn))
6795 tree type = build_pointer_type (basetype);
6796 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
6797 instance_ptr = build_nop (type, instance_ptr);
6799 if (DECL_DESTRUCTOR_P (fn))
6800 name = complete_dtor_identifier;
6802 first_mem_arg = instance_ptr;
6804 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6805 p = conversion_obstack_alloc (0);
6807 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
6808 initializer, not T({ }). */
6809 if (DECL_CONSTRUCTOR_P (fn) && args != NULL && !VEC_empty (tree, *args)
6810 && BRACE_ENCLOSED_INITIALIZER_P (VEC_index (tree, *args, 0))
6811 && CONSTRUCTOR_IS_DIRECT_INIT (VEC_index (tree, *args, 0)))
6813 gcc_assert (VEC_length (tree, *args) == 1
6814 && !(flags & LOOKUP_ONLYCONVERTING));
6816 add_list_candidates (fns, first_mem_arg, VEC_index (tree, *args, 0),
6817 basetype, explicit_targs, template_only,
6818 conversion_path, access_binfo, flags, &candidates);
6820 else
6822 add_candidates (fns, first_mem_arg, user_args, optype,
6823 explicit_targs, template_only, conversion_path,
6824 access_binfo, flags, &candidates);
6826 any_viable_p = false;
6827 candidates = splice_viable (candidates, pedantic, &any_viable_p);
6829 if (!any_viable_p)
6831 if (complain & tf_error)
6833 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
6834 cxx_incomplete_type_error (instance_ptr, basetype);
6835 else if (optype)
6836 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
6837 basetype, optype, build_tree_list_vec (user_args),
6838 TREE_TYPE (TREE_TYPE (instance_ptr)));
6839 else
6841 char *pretty_name;
6842 bool free_p;
6843 tree arglist;
6845 pretty_name = name_as_c_string (name, basetype, &free_p);
6846 arglist = build_tree_list_vec (user_args);
6847 if (skip_first_for_error)
6848 arglist = TREE_CHAIN (arglist);
6849 error ("no matching function for call to %<%T::%s(%A)%#V%>",
6850 basetype, pretty_name, arglist,
6851 TREE_TYPE (TREE_TYPE (instance_ptr)));
6852 if (free_p)
6853 free (pretty_name);
6855 print_z_candidates (location_of (name), candidates);
6857 call = error_mark_node;
6859 else
6861 cand = tourney (candidates);
6862 if (cand == 0)
6864 char *pretty_name;
6865 bool free_p;
6866 tree arglist;
6868 if (complain & tf_error)
6870 pretty_name = name_as_c_string (name, basetype, &free_p);
6871 arglist = build_tree_list_vec (user_args);
6872 if (skip_first_for_error)
6873 arglist = TREE_CHAIN (arglist);
6874 error ("call of overloaded %<%s(%A)%> is ambiguous", pretty_name,
6875 arglist);
6876 print_z_candidates (location_of (name), candidates);
6877 if (free_p)
6878 free (pretty_name);
6880 call = error_mark_node;
6882 else
6884 fn = cand->fn;
6886 if (!(flags & LOOKUP_NONVIRTUAL)
6887 && DECL_PURE_VIRTUAL_P (fn)
6888 && instance == current_class_ref
6889 && (DECL_CONSTRUCTOR_P (current_function_decl)
6890 || DECL_DESTRUCTOR_P (current_function_decl))
6891 && (complain & tf_warning))
6892 /* This is not an error, it is runtime undefined
6893 behavior. */
6894 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl) ?
6895 "pure virtual %q#D called from constructor"
6896 : "pure virtual %q#D called from destructor"),
6897 fn);
6899 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
6900 && is_dummy_object (instance_ptr))
6902 if (complain & tf_error)
6903 error ("cannot call member function %qD without object",
6904 fn);
6905 call = error_mark_node;
6907 else
6909 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
6910 && resolves_to_fixed_type_p (instance, 0))
6911 flags |= LOOKUP_NONVIRTUAL;
6912 /* Now we know what function is being called. */
6913 if (fn_p)
6914 *fn_p = fn;
6915 /* Build the actual CALL_EXPR. */
6916 call = build_over_call (cand, flags, complain);
6917 /* In an expression of the form `a->f()' where `f' turns
6918 out to be a static member function, `a' is
6919 none-the-less evaluated. */
6920 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
6921 && !is_dummy_object (instance_ptr)
6922 && TREE_SIDE_EFFECTS (instance_ptr))
6923 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
6924 instance_ptr, call);
6925 else if (call != error_mark_node
6926 && DECL_DESTRUCTOR_P (cand->fn)
6927 && !VOID_TYPE_P (TREE_TYPE (call)))
6928 /* An explicit call of the form "x->~X()" has type
6929 "void". However, on platforms where destructors
6930 return "this" (i.e., those where
6931 targetm.cxx.cdtor_returns_this is true), such calls
6932 will appear to have a return value of pointer type
6933 to the low-level call machinery. We do not want to
6934 change the low-level machinery, since we want to be
6935 able to optimize "delete f()" on such platforms as
6936 "operator delete(~X(f()))" (rather than generating
6937 "t = f(), ~X(t), operator delete (t)"). */
6938 call = build_nop (void_type_node, call);
6943 if (processing_template_decl && call != error_mark_node)
6945 bool cast_to_void = false;
6947 if (TREE_CODE (call) == COMPOUND_EXPR)
6948 call = TREE_OPERAND (call, 1);
6949 else if (TREE_CODE (call) == NOP_EXPR)
6951 cast_to_void = true;
6952 call = TREE_OPERAND (call, 0);
6954 if (TREE_CODE (call) == INDIRECT_REF)
6955 call = TREE_OPERAND (call, 0);
6956 call = (build_min_non_dep_call_vec
6957 (call,
6958 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
6959 orig_instance, orig_fns, NULL_TREE),
6960 orig_args));
6961 call = convert_from_reference (call);
6962 if (cast_to_void)
6963 call = build_nop (void_type_node, call);
6966 /* Free all the conversions we allocated. */
6967 obstack_free (&conversion_obstack, p);
6969 if (orig_args != NULL)
6970 release_tree_vector (orig_args);
6972 return call;
6975 /* Returns true iff standard conversion sequence ICS1 is a proper
6976 subsequence of ICS2. */
6978 static bool
6979 is_subseq (conversion *ics1, conversion *ics2)
6981 /* We can assume that a conversion of the same code
6982 between the same types indicates a subsequence since we only get
6983 here if the types we are converting from are the same. */
6985 while (ics1->kind == ck_rvalue
6986 || ics1->kind == ck_lvalue)
6987 ics1 = ics1->u.next;
6989 while (1)
6991 while (ics2->kind == ck_rvalue
6992 || ics2->kind == ck_lvalue)
6993 ics2 = ics2->u.next;
6995 if (ics2->kind == ck_user
6996 || ics2->kind == ck_ambig
6997 || ics2->kind == ck_aggr
6998 || ics2->kind == ck_list
6999 || ics2->kind == ck_identity)
7000 /* At this point, ICS1 cannot be a proper subsequence of
7001 ICS2. We can get a USER_CONV when we are comparing the
7002 second standard conversion sequence of two user conversion
7003 sequences. */
7004 return false;
7006 ics2 = ics2->u.next;
7008 if (ics2->kind == ics1->kind
7009 && same_type_p (ics2->type, ics1->type)
7010 && same_type_p (ics2->u.next->type,
7011 ics1->u.next->type))
7012 return true;
7016 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
7017 be any _TYPE nodes. */
7019 bool
7020 is_properly_derived_from (tree derived, tree base)
7022 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
7023 return false;
7025 /* We only allow proper derivation here. The DERIVED_FROM_P macro
7026 considers every class derived from itself. */
7027 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
7028 && DERIVED_FROM_P (base, derived));
7031 /* We build the ICS for an implicit object parameter as a pointer
7032 conversion sequence. However, such a sequence should be compared
7033 as if it were a reference conversion sequence. If ICS is the
7034 implicit conversion sequence for an implicit object parameter,
7035 modify it accordingly. */
7037 static void
7038 maybe_handle_implicit_object (conversion **ics)
7040 if ((*ics)->this_p)
7042 /* [over.match.funcs]
7044 For non-static member functions, the type of the
7045 implicit object parameter is "reference to cv X"
7046 where X is the class of which the function is a
7047 member and cv is the cv-qualification on the member
7048 function declaration. */
7049 conversion *t = *ics;
7050 tree reference_type;
7052 /* The `this' parameter is a pointer to a class type. Make the
7053 implicit conversion talk about a reference to that same class
7054 type. */
7055 reference_type = TREE_TYPE (t->type);
7056 reference_type = build_reference_type (reference_type);
7058 if (t->kind == ck_qual)
7059 t = t->u.next;
7060 if (t->kind == ck_ptr)
7061 t = t->u.next;
7062 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
7063 t = direct_reference_binding (reference_type, t);
7064 t->this_p = 1;
7065 t->rvaluedness_matches_p = 0;
7066 *ics = t;
7070 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
7071 and return the initial reference binding conversion. Otherwise,
7072 leave *ICS unchanged and return NULL. */
7074 static conversion *
7075 maybe_handle_ref_bind (conversion **ics)
7077 if ((*ics)->kind == ck_ref_bind)
7079 conversion *old_ics = *ics;
7080 *ics = old_ics->u.next;
7081 (*ics)->user_conv_p = old_ics->user_conv_p;
7082 return old_ics;
7085 return NULL;
7088 /* Compare two implicit conversion sequences according to the rules set out in
7089 [over.ics.rank]. Return values:
7091 1: ics1 is better than ics2
7092 -1: ics2 is better than ics1
7093 0: ics1 and ics2 are indistinguishable */
7095 static int
7096 compare_ics (conversion *ics1, conversion *ics2)
7098 tree from_type1;
7099 tree from_type2;
7100 tree to_type1;
7101 tree to_type2;
7102 tree deref_from_type1 = NULL_TREE;
7103 tree deref_from_type2 = NULL_TREE;
7104 tree deref_to_type1 = NULL_TREE;
7105 tree deref_to_type2 = NULL_TREE;
7106 conversion_rank rank1, rank2;
7108 /* REF_BINDING is nonzero if the result of the conversion sequence
7109 is a reference type. In that case REF_CONV is the reference
7110 binding conversion. */
7111 conversion *ref_conv1;
7112 conversion *ref_conv2;
7114 /* Handle implicit object parameters. */
7115 maybe_handle_implicit_object (&ics1);
7116 maybe_handle_implicit_object (&ics2);
7118 /* Handle reference parameters. */
7119 ref_conv1 = maybe_handle_ref_bind (&ics1);
7120 ref_conv2 = maybe_handle_ref_bind (&ics2);
7122 /* List-initialization sequence L1 is a better conversion sequence than
7123 list-initialization sequence L2 if L1 converts to
7124 std::initializer_list<X> for some X and L2 does not. */
7125 if (ics1->kind == ck_list && ics2->kind != ck_list)
7126 return 1;
7127 if (ics2->kind == ck_list && ics1->kind != ck_list)
7128 return -1;
7130 /* [over.ics.rank]
7132 When comparing the basic forms of implicit conversion sequences (as
7133 defined in _over.best.ics_)
7135 --a standard conversion sequence (_over.ics.scs_) is a better
7136 conversion sequence than a user-defined conversion sequence
7137 or an ellipsis conversion sequence, and
7139 --a user-defined conversion sequence (_over.ics.user_) is a
7140 better conversion sequence than an ellipsis conversion sequence
7141 (_over.ics.ellipsis_). */
7142 rank1 = CONVERSION_RANK (ics1);
7143 rank2 = CONVERSION_RANK (ics2);
7145 if (rank1 > rank2)
7146 return -1;
7147 else if (rank1 < rank2)
7148 return 1;
7150 if (rank1 == cr_bad)
7152 /* Both ICS are bad. We try to make a decision based on what would
7153 have happened if they'd been good. This is not an extension,
7154 we'll still give an error when we build up the call; this just
7155 helps us give a more helpful error message. */
7156 rank1 = BAD_CONVERSION_RANK (ics1);
7157 rank2 = BAD_CONVERSION_RANK (ics2);
7159 if (rank1 > rank2)
7160 return -1;
7161 else if (rank1 < rank2)
7162 return 1;
7164 /* We couldn't make up our minds; try to figure it out below. */
7167 if (ics1->ellipsis_p)
7168 /* Both conversions are ellipsis conversions. */
7169 return 0;
7171 /* User-defined conversion sequence U1 is a better conversion sequence
7172 than another user-defined conversion sequence U2 if they contain the
7173 same user-defined conversion operator or constructor and if the sec-
7174 ond standard conversion sequence of U1 is better than the second
7175 standard conversion sequence of U2. */
7177 /* Handle list-conversion with the same code even though it isn't always
7178 ranked as a user-defined conversion and it doesn't have a second
7179 standard conversion sequence; it will still have the desired effect.
7180 Specifically, we need to do the reference binding comparison at the
7181 end of this function. */
7183 if (ics1->user_conv_p || ics1->kind == ck_list)
7185 conversion *t1;
7186 conversion *t2;
7188 for (t1 = ics1; t1->kind != ck_user; t1 = t1->u.next)
7189 if (t1->kind == ck_ambig || t1->kind == ck_aggr
7190 || t1->kind == ck_list)
7191 break;
7192 for (t2 = ics2; t2->kind != ck_user; t2 = t2->u.next)
7193 if (t2->kind == ck_ambig || t2->kind == ck_aggr
7194 || t2->kind == ck_list)
7195 break;
7197 if (t1->kind != t2->kind)
7198 return 0;
7199 else if (t1->kind == ck_user)
7201 if (t1->cand->fn != t2->cand->fn)
7202 return 0;
7204 else
7206 /* For ambiguous or aggregate conversions, use the target type as
7207 a proxy for the conversion function. */
7208 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
7209 return 0;
7212 /* We can just fall through here, after setting up
7213 FROM_TYPE1 and FROM_TYPE2. */
7214 from_type1 = t1->type;
7215 from_type2 = t2->type;
7217 else
7219 conversion *t1;
7220 conversion *t2;
7222 /* We're dealing with two standard conversion sequences.
7224 [over.ics.rank]
7226 Standard conversion sequence S1 is a better conversion
7227 sequence than standard conversion sequence S2 if
7229 --S1 is a proper subsequence of S2 (comparing the conversion
7230 sequences in the canonical form defined by _over.ics.scs_,
7231 excluding any Lvalue Transformation; the identity
7232 conversion sequence is considered to be a subsequence of
7233 any non-identity conversion sequence */
7235 t1 = ics1;
7236 while (t1->kind != ck_identity)
7237 t1 = t1->u.next;
7238 from_type1 = t1->type;
7240 t2 = ics2;
7241 while (t2->kind != ck_identity)
7242 t2 = t2->u.next;
7243 from_type2 = t2->type;
7246 /* One sequence can only be a subsequence of the other if they start with
7247 the same type. They can start with different types when comparing the
7248 second standard conversion sequence in two user-defined conversion
7249 sequences. */
7250 if (same_type_p (from_type1, from_type2))
7252 if (is_subseq (ics1, ics2))
7253 return 1;
7254 if (is_subseq (ics2, ics1))
7255 return -1;
7258 /* [over.ics.rank]
7260 Or, if not that,
7262 --the rank of S1 is better than the rank of S2 (by the rules
7263 defined below):
7265 Standard conversion sequences are ordered by their ranks: an Exact
7266 Match is a better conversion than a Promotion, which is a better
7267 conversion than a Conversion.
7269 Two conversion sequences with the same rank are indistinguishable
7270 unless one of the following rules applies:
7272 --A conversion that does not a convert a pointer, pointer to member,
7273 or std::nullptr_t to bool is better than one that does.
7275 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
7276 so that we do not have to check it explicitly. */
7277 if (ics1->rank < ics2->rank)
7278 return 1;
7279 else if (ics2->rank < ics1->rank)
7280 return -1;
7282 to_type1 = ics1->type;
7283 to_type2 = ics2->type;
7285 /* A conversion from scalar arithmetic type to complex is worse than a
7286 conversion between scalar arithmetic types. */
7287 if (same_type_p (from_type1, from_type2)
7288 && ARITHMETIC_TYPE_P (from_type1)
7289 && ARITHMETIC_TYPE_P (to_type1)
7290 && ARITHMETIC_TYPE_P (to_type2)
7291 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
7292 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
7294 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
7295 return -1;
7296 else
7297 return 1;
7300 if (TYPE_PTR_P (from_type1)
7301 && TYPE_PTR_P (from_type2)
7302 && TYPE_PTR_P (to_type1)
7303 && TYPE_PTR_P (to_type2))
7305 deref_from_type1 = TREE_TYPE (from_type1);
7306 deref_from_type2 = TREE_TYPE (from_type2);
7307 deref_to_type1 = TREE_TYPE (to_type1);
7308 deref_to_type2 = TREE_TYPE (to_type2);
7310 /* The rules for pointers to members A::* are just like the rules
7311 for pointers A*, except opposite: if B is derived from A then
7312 A::* converts to B::*, not vice versa. For that reason, we
7313 switch the from_ and to_ variables here. */
7314 else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
7315 && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
7316 || (TYPE_PTRMEMFUNC_P (from_type1)
7317 && TYPE_PTRMEMFUNC_P (from_type2)
7318 && TYPE_PTRMEMFUNC_P (to_type1)
7319 && TYPE_PTRMEMFUNC_P (to_type2)))
7321 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
7322 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
7323 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
7324 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
7327 if (deref_from_type1 != NULL_TREE
7328 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
7329 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
7331 /* This was one of the pointer or pointer-like conversions.
7333 [over.ics.rank]
7335 --If class B is derived directly or indirectly from class A,
7336 conversion of B* to A* is better than conversion of B* to
7337 void*, and conversion of A* to void* is better than
7338 conversion of B* to void*. */
7339 if (TREE_CODE (deref_to_type1) == VOID_TYPE
7340 && TREE_CODE (deref_to_type2) == VOID_TYPE)
7342 if (is_properly_derived_from (deref_from_type1,
7343 deref_from_type2))
7344 return -1;
7345 else if (is_properly_derived_from (deref_from_type2,
7346 deref_from_type1))
7347 return 1;
7349 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
7350 || TREE_CODE (deref_to_type2) == VOID_TYPE)
7352 if (same_type_p (deref_from_type1, deref_from_type2))
7354 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
7356 if (is_properly_derived_from (deref_from_type1,
7357 deref_to_type1))
7358 return 1;
7360 /* We know that DEREF_TO_TYPE1 is `void' here. */
7361 else if (is_properly_derived_from (deref_from_type1,
7362 deref_to_type2))
7363 return -1;
7366 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
7367 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
7369 /* [over.ics.rank]
7371 --If class B is derived directly or indirectly from class A
7372 and class C is derived directly or indirectly from B,
7374 --conversion of C* to B* is better than conversion of C* to
7377 --conversion of B* to A* is better than conversion of C* to
7378 A* */
7379 if (same_type_p (deref_from_type1, deref_from_type2))
7381 if (is_properly_derived_from (deref_to_type1,
7382 deref_to_type2))
7383 return 1;
7384 else if (is_properly_derived_from (deref_to_type2,
7385 deref_to_type1))
7386 return -1;
7388 else if (same_type_p (deref_to_type1, deref_to_type2))
7390 if (is_properly_derived_from (deref_from_type2,
7391 deref_from_type1))
7392 return 1;
7393 else if (is_properly_derived_from (deref_from_type1,
7394 deref_from_type2))
7395 return -1;
7399 else if (CLASS_TYPE_P (non_reference (from_type1))
7400 && same_type_p (from_type1, from_type2))
7402 tree from = non_reference (from_type1);
7404 /* [over.ics.rank]
7406 --binding of an expression of type C to a reference of type
7407 B& is better than binding an expression of type C to a
7408 reference of type A&
7410 --conversion of C to B is better than conversion of C to A, */
7411 if (is_properly_derived_from (from, to_type1)
7412 && is_properly_derived_from (from, to_type2))
7414 if (is_properly_derived_from (to_type1, to_type2))
7415 return 1;
7416 else if (is_properly_derived_from (to_type2, to_type1))
7417 return -1;
7420 else if (CLASS_TYPE_P (non_reference (to_type1))
7421 && same_type_p (to_type1, to_type2))
7423 tree to = non_reference (to_type1);
7425 /* [over.ics.rank]
7427 --binding of an expression of type B to a reference of type
7428 A& is better than binding an expression of type C to a
7429 reference of type A&,
7431 --conversion of B to A is better than conversion of C to A */
7432 if (is_properly_derived_from (from_type1, to)
7433 && is_properly_derived_from (from_type2, to))
7435 if (is_properly_derived_from (from_type2, from_type1))
7436 return 1;
7437 else if (is_properly_derived_from (from_type1, from_type2))
7438 return -1;
7442 /* [over.ics.rank]
7444 --S1 and S2 differ only in their qualification conversion and yield
7445 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
7446 qualification signature of type T1 is a proper subset of the cv-
7447 qualification signature of type T2 */
7448 if (ics1->kind == ck_qual
7449 && ics2->kind == ck_qual
7450 && same_type_p (from_type1, from_type2))
7452 int result = comp_cv_qual_signature (to_type1, to_type2);
7453 if (result != 0)
7454 return result;
7457 /* [over.ics.rank]
7459 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
7460 to an implicit object parameter, and either S1 binds an lvalue reference
7461 to an lvalue and S2 binds an rvalue reference or S1 binds an rvalue
7462 reference to an rvalue and S2 binds an lvalue reference
7463 (C++0x draft standard, 13.3.3.2)
7465 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
7466 types to which the references refer are the same type except for
7467 top-level cv-qualifiers, and the type to which the reference
7468 initialized by S2 refers is more cv-qualified than the type to
7469 which the reference initialized by S1 refers */
7471 if (ref_conv1 && ref_conv2)
7473 if (!ref_conv1->this_p && !ref_conv2->this_p
7474 && (TYPE_REF_IS_RVALUE (ref_conv1->type)
7475 != TYPE_REF_IS_RVALUE (ref_conv2->type)))
7477 if (ref_conv1->rvaluedness_matches_p)
7478 return 1;
7479 if (ref_conv2->rvaluedness_matches_p)
7480 return -1;
7483 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
7484 return comp_cv_qualification (TREE_TYPE (ref_conv2->type),
7485 TREE_TYPE (ref_conv1->type));
7488 /* Neither conversion sequence is better than the other. */
7489 return 0;
7492 /* The source type for this standard conversion sequence. */
7494 static tree
7495 source_type (conversion *t)
7497 for (;; t = t->u.next)
7499 if (t->kind == ck_user
7500 || t->kind == ck_ambig
7501 || t->kind == ck_identity)
7502 return t->type;
7504 gcc_unreachable ();
7507 /* Note a warning about preferring WINNER to LOSER. We do this by storing
7508 a pointer to LOSER and re-running joust to produce the warning if WINNER
7509 is actually used. */
7511 static void
7512 add_warning (struct z_candidate *winner, struct z_candidate *loser)
7514 candidate_warning *cw = (candidate_warning *)
7515 conversion_obstack_alloc (sizeof (candidate_warning));
7516 cw->loser = loser;
7517 cw->next = winner->warnings;
7518 winner->warnings = cw;
7521 /* Compare two candidates for overloading as described in
7522 [over.match.best]. Return values:
7524 1: cand1 is better than cand2
7525 -1: cand2 is better than cand1
7526 0: cand1 and cand2 are indistinguishable */
7528 static int
7529 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
7531 int winner = 0;
7532 int off1 = 0, off2 = 0;
7533 size_t i;
7534 size_t len;
7536 /* Candidates that involve bad conversions are always worse than those
7537 that don't. */
7538 if (cand1->viable > cand2->viable)
7539 return 1;
7540 if (cand1->viable < cand2->viable)
7541 return -1;
7543 /* If we have two pseudo-candidates for conversions to the same type,
7544 or two candidates for the same function, arbitrarily pick one. */
7545 if (cand1->fn == cand2->fn
7546 && (IS_TYPE_OR_DECL_P (cand1->fn)))
7547 return 1;
7549 /* a viable function F1
7550 is defined to be a better function than another viable function F2 if
7551 for all arguments i, ICSi(F1) is not a worse conversion sequence than
7552 ICSi(F2), and then */
7554 /* for some argument j, ICSj(F1) is a better conversion sequence than
7555 ICSj(F2) */
7557 /* For comparing static and non-static member functions, we ignore
7558 the implicit object parameter of the non-static function. The
7559 standard says to pretend that the static function has an object
7560 parm, but that won't work with operator overloading. */
7561 len = cand1->num_convs;
7562 if (len != cand2->num_convs)
7564 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
7565 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
7567 gcc_assert (static_1 != static_2);
7569 if (static_1)
7570 off2 = 1;
7571 else
7573 off1 = 1;
7574 --len;
7578 for (i = 0; i < len; ++i)
7580 conversion *t1 = cand1->convs[i + off1];
7581 conversion *t2 = cand2->convs[i + off2];
7582 int comp = compare_ics (t1, t2);
7584 if (comp != 0)
7586 if (warn_sign_promo
7587 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
7588 == cr_std + cr_promotion)
7589 && t1->kind == ck_std
7590 && t2->kind == ck_std
7591 && TREE_CODE (t1->type) == INTEGER_TYPE
7592 && TREE_CODE (t2->type) == INTEGER_TYPE
7593 && (TYPE_PRECISION (t1->type)
7594 == TYPE_PRECISION (t2->type))
7595 && (TYPE_UNSIGNED (t1->u.next->type)
7596 || (TREE_CODE (t1->u.next->type)
7597 == ENUMERAL_TYPE)))
7599 tree type = t1->u.next->type;
7600 tree type1, type2;
7601 struct z_candidate *w, *l;
7602 if (comp > 0)
7603 type1 = t1->type, type2 = t2->type,
7604 w = cand1, l = cand2;
7605 else
7606 type1 = t2->type, type2 = t1->type,
7607 w = cand2, l = cand1;
7609 if (warn)
7611 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
7612 type, type1, type2);
7613 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
7615 else
7616 add_warning (w, l);
7619 if (winner && comp != winner)
7621 winner = 0;
7622 goto tweak;
7624 winner = comp;
7628 /* warn about confusing overload resolution for user-defined conversions,
7629 either between a constructor and a conversion op, or between two
7630 conversion ops. */
7631 if (winner && warn_conversion && cand1->second_conv
7632 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
7633 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
7635 struct z_candidate *w, *l;
7636 bool give_warning = false;
7638 if (winner == 1)
7639 w = cand1, l = cand2;
7640 else
7641 w = cand2, l = cand1;
7643 /* We don't want to complain about `X::operator T1 ()'
7644 beating `X::operator T2 () const', when T2 is a no less
7645 cv-qualified version of T1. */
7646 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
7647 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
7649 tree t = TREE_TYPE (TREE_TYPE (l->fn));
7650 tree f = TREE_TYPE (TREE_TYPE (w->fn));
7652 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
7654 t = TREE_TYPE (t);
7655 f = TREE_TYPE (f);
7657 if (!comp_ptr_ttypes (t, f))
7658 give_warning = true;
7660 else
7661 give_warning = true;
7663 if (!give_warning)
7664 /*NOP*/;
7665 else if (warn)
7667 tree source = source_type (w->convs[0]);
7668 if (! DECL_CONSTRUCTOR_P (w->fn))
7669 source = TREE_TYPE (source);
7670 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
7671 && warning (OPT_Wconversion, " for conversion from %qT to %qT",
7672 source, w->second_conv->type))
7674 inform (input_location, " because conversion sequence for the argument is better");
7677 else
7678 add_warning (w, l);
7681 if (winner)
7682 return winner;
7684 /* or, if not that,
7685 F1 is a non-template function and F2 is a template function
7686 specialization. */
7688 if (!cand1->template_decl && cand2->template_decl)
7689 return 1;
7690 else if (cand1->template_decl && !cand2->template_decl)
7691 return -1;
7693 /* or, if not that,
7694 F1 and F2 are template functions and the function template for F1 is
7695 more specialized than the template for F2 according to the partial
7696 ordering rules. */
7698 if (cand1->template_decl && cand2->template_decl)
7700 winner = more_specialized_fn
7701 (TI_TEMPLATE (cand1->template_decl),
7702 TI_TEMPLATE (cand2->template_decl),
7703 /* [temp.func.order]: The presence of unused ellipsis and default
7704 arguments has no effect on the partial ordering of function
7705 templates. add_function_candidate() will not have
7706 counted the "this" argument for constructors. */
7707 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
7708 if (winner)
7709 return winner;
7712 /* or, if not that,
7713 the context is an initialization by user-defined conversion (see
7714 _dcl.init_ and _over.match.user_) and the standard conversion
7715 sequence from the return type of F1 to the destination type (i.e.,
7716 the type of the entity being initialized) is a better conversion
7717 sequence than the standard conversion sequence from the return type
7718 of F2 to the destination type. */
7720 if (cand1->second_conv)
7722 winner = compare_ics (cand1->second_conv, cand2->second_conv);
7723 if (winner)
7724 return winner;
7727 /* Check whether we can discard a builtin candidate, either because we
7728 have two identical ones or matching builtin and non-builtin candidates.
7730 (Pedantically in the latter case the builtin which matched the user
7731 function should not be added to the overload set, but we spot it here.
7733 [over.match.oper]
7734 ... the builtin candidates include ...
7735 - do not have the same parameter type list as any non-template
7736 non-member candidate. */
7738 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
7739 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
7741 for (i = 0; i < len; ++i)
7742 if (!same_type_p (cand1->convs[i]->type,
7743 cand2->convs[i]->type))
7744 break;
7745 if (i == cand1->num_convs)
7747 if (cand1->fn == cand2->fn)
7748 /* Two built-in candidates; arbitrarily pick one. */
7749 return 1;
7750 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
7751 /* cand1 is built-in; prefer cand2. */
7752 return -1;
7753 else
7754 /* cand2 is built-in; prefer cand1. */
7755 return 1;
7759 /* If the two function declarations represent the same function (this can
7760 happen with declarations in multiple scopes and arg-dependent lookup),
7761 arbitrarily choose one. But first make sure the default args we're
7762 using match. */
7763 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
7764 && equal_functions (cand1->fn, cand2->fn))
7766 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
7767 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
7769 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
7771 for (i = 0; i < len; ++i)
7773 /* Don't crash if the fn is variadic. */
7774 if (!parms1)
7775 break;
7776 parms1 = TREE_CHAIN (parms1);
7777 parms2 = TREE_CHAIN (parms2);
7780 if (off1)
7781 parms1 = TREE_CHAIN (parms1);
7782 else if (off2)
7783 parms2 = TREE_CHAIN (parms2);
7785 for (; parms1; ++i)
7787 if (!cp_tree_equal (TREE_PURPOSE (parms1),
7788 TREE_PURPOSE (parms2)))
7790 if (warn)
7792 permerror (input_location, "default argument mismatch in "
7793 "overload resolution");
7794 inform (input_location,
7795 " candidate 1: %q+#F", cand1->fn);
7796 inform (input_location,
7797 " candidate 2: %q+#F", cand2->fn);
7799 else
7800 add_warning (cand1, cand2);
7801 break;
7803 parms1 = TREE_CHAIN (parms1);
7804 parms2 = TREE_CHAIN (parms2);
7807 return 1;
7810 tweak:
7812 /* Extension: If the worst conversion for one candidate is worse than the
7813 worst conversion for the other, take the first. */
7814 if (!pedantic)
7816 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
7817 struct z_candidate *w = 0, *l = 0;
7819 for (i = 0; i < len; ++i)
7821 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
7822 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
7823 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
7824 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
7826 if (rank1 < rank2)
7827 winner = 1, w = cand1, l = cand2;
7828 if (rank1 > rank2)
7829 winner = -1, w = cand2, l = cand1;
7830 if (winner)
7832 /* Don't choose a deleted function over ambiguity. */
7833 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
7834 return 0;
7835 if (warn)
7837 pedwarn (input_location, 0,
7838 "ISO C++ says that these are ambiguous, even "
7839 "though the worst conversion for the first is better than "
7840 "the worst conversion for the second:");
7841 print_z_candidate (_("candidate 1:"), w);
7842 print_z_candidate (_("candidate 2:"), l);
7844 else
7845 add_warning (w, l);
7846 return winner;
7850 gcc_assert (!winner);
7851 return 0;
7854 /* Given a list of candidates for overloading, find the best one, if any.
7855 This algorithm has a worst case of O(2n) (winner is last), and a best
7856 case of O(n/2) (totally ambiguous); much better than a sorting
7857 algorithm. */
7859 static struct z_candidate *
7860 tourney (struct z_candidate *candidates)
7862 struct z_candidate *champ = candidates, *challenger;
7863 int fate;
7864 int champ_compared_to_predecessor = 0;
7866 /* Walk through the list once, comparing each current champ to the next
7867 candidate, knocking out a candidate or two with each comparison. */
7869 for (challenger = champ->next; challenger; )
7871 fate = joust (champ, challenger, 0);
7872 if (fate == 1)
7873 challenger = challenger->next;
7874 else
7876 if (fate == 0)
7878 champ = challenger->next;
7879 if (champ == 0)
7880 return NULL;
7881 champ_compared_to_predecessor = 0;
7883 else
7885 champ = challenger;
7886 champ_compared_to_predecessor = 1;
7889 challenger = champ->next;
7893 /* Make sure the champ is better than all the candidates it hasn't yet
7894 been compared to. */
7896 for (challenger = candidates;
7897 challenger != champ
7898 && !(champ_compared_to_predecessor && challenger->next == champ);
7899 challenger = challenger->next)
7901 fate = joust (champ, challenger, 0);
7902 if (fate != 1)
7903 return NULL;
7906 return champ;
7909 /* Returns nonzero if things of type FROM can be converted to TO. */
7911 bool
7912 can_convert (tree to, tree from)
7914 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT);
7917 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
7919 bool
7920 can_convert_arg (tree to, tree from, tree arg, int flags)
7922 conversion *t;
7923 void *p;
7924 bool ok_p;
7926 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7927 p = conversion_obstack_alloc (0);
7929 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
7930 flags);
7931 ok_p = (t && !t->bad_p);
7933 /* Free all the conversions we allocated. */
7934 obstack_free (&conversion_obstack, p);
7936 return ok_p;
7939 /* Like can_convert_arg, but allows dubious conversions as well. */
7941 bool
7942 can_convert_arg_bad (tree to, tree from, tree arg, int flags)
7944 conversion *t;
7945 void *p;
7947 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7948 p = conversion_obstack_alloc (0);
7949 /* Try to perform the conversion. */
7950 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
7951 flags);
7952 /* Free all the conversions we allocated. */
7953 obstack_free (&conversion_obstack, p);
7955 return t != NULL;
7958 /* Convert EXPR to TYPE. Return the converted expression.
7960 Note that we allow bad conversions here because by the time we get to
7961 this point we are committed to doing the conversion. If we end up
7962 doing a bad conversion, convert_like will complain. */
7964 tree
7965 perform_implicit_conversion_flags (tree type, tree expr, tsubst_flags_t complain, int flags)
7967 conversion *conv;
7968 void *p;
7970 if (error_operand_p (expr))
7971 return error_mark_node;
7973 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7974 p = conversion_obstack_alloc (0);
7976 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
7977 /*c_cast_p=*/false,
7978 flags);
7980 if (!conv)
7982 if (complain & tf_error)
7984 /* If expr has unknown type, then it is an overloaded function.
7985 Call instantiate_type to get good error messages. */
7986 if (TREE_TYPE (expr) == unknown_type_node)
7987 instantiate_type (type, expr, complain);
7988 else if (invalid_nonstatic_memfn_p (expr, complain))
7989 /* We gave an error. */;
7990 else
7991 error ("could not convert %qE to %qT", expr, type);
7993 expr = error_mark_node;
7995 else if (processing_template_decl)
7997 /* In a template, we are only concerned about determining the
7998 type of non-dependent expressions, so we do not have to
7999 perform the actual conversion. */
8000 if (TREE_TYPE (expr) != type)
8001 expr = build_nop (type, expr);
8003 else
8004 expr = convert_like (conv, expr, complain);
8006 /* Free all the conversions we allocated. */
8007 obstack_free (&conversion_obstack, p);
8009 return expr;
8012 tree
8013 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
8015 return perform_implicit_conversion_flags (type, expr, complain, LOOKUP_IMPLICIT);
8018 /* Convert EXPR to TYPE (as a direct-initialization) if that is
8019 permitted. If the conversion is valid, the converted expression is
8020 returned. Otherwise, NULL_TREE is returned, except in the case
8021 that TYPE is a class type; in that case, an error is issued. If
8022 C_CAST_P is true, then this direction initialization is taking
8023 place as part of a static_cast being attempted as part of a C-style
8024 cast. */
8026 tree
8027 perform_direct_initialization_if_possible (tree type,
8028 tree expr,
8029 bool c_cast_p,
8030 tsubst_flags_t complain)
8032 conversion *conv;
8033 void *p;
8035 if (type == error_mark_node || error_operand_p (expr))
8036 return error_mark_node;
8037 /* [dcl.init]
8039 If the destination type is a (possibly cv-qualified) class type:
8041 -- If the initialization is direct-initialization ...,
8042 constructors are considered. ... If no constructor applies, or
8043 the overload resolution is ambiguous, the initialization is
8044 ill-formed. */
8045 if (CLASS_TYPE_P (type))
8047 VEC(tree,gc) *args = make_tree_vector_single (expr);
8048 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8049 &args, type, LOOKUP_NORMAL, complain);
8050 release_tree_vector (args);
8051 return build_cplus_new (type, expr);
8054 /* Get the high-water mark for the CONVERSION_OBSTACK. */
8055 p = conversion_obstack_alloc (0);
8057 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
8058 c_cast_p,
8059 LOOKUP_NORMAL);
8060 if (!conv || conv->bad_p)
8061 expr = NULL_TREE;
8062 else
8063 expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
8064 /*issue_conversion_warnings=*/false,
8065 c_cast_p,
8066 complain);
8068 /* Free all the conversions we allocated. */
8069 obstack_free (&conversion_obstack, p);
8071 return expr;
8074 /* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference
8075 is being bound to a temporary. Create and return a new VAR_DECL
8076 with the indicated TYPE; this variable will store the value to
8077 which the reference is bound. */
8079 tree
8080 make_temporary_var_for_ref_to_temp (tree decl, tree type)
8082 tree var;
8084 /* Create the variable. */
8085 var = create_temporary_var (type);
8087 /* Register the variable. */
8088 if (TREE_STATIC (decl))
8090 /* Namespace-scope or local static; give it a mangled name. */
8091 tree name;
8093 TREE_STATIC (var) = 1;
8094 name = mangle_ref_init_variable (decl);
8095 DECL_NAME (var) = name;
8096 SET_DECL_ASSEMBLER_NAME (var, name);
8097 var = pushdecl_top_level (var);
8099 else
8100 /* Create a new cleanup level if necessary. */
8101 maybe_push_cleanup_level (type);
8103 return var;
8106 /* EXPR is the initializer for a variable DECL of reference or
8107 std::initializer_list type. Create, push and return a new VAR_DECL
8108 for the initializer so that it will live as long as DECL. Any
8109 cleanup for the new variable is returned through CLEANUP, and the
8110 code to initialize the new variable is returned through INITP. */
8112 tree
8113 set_up_extended_ref_temp (tree decl, tree expr, tree *cleanup, tree *initp)
8115 tree init;
8116 tree type;
8117 tree var;
8119 /* Create the temporary variable. */
8120 type = TREE_TYPE (expr);
8121 var = make_temporary_var_for_ref_to_temp (decl, type);
8122 layout_decl (var, 0);
8123 /* If the rvalue is the result of a function call it will be
8124 a TARGET_EXPR. If it is some other construct (such as a
8125 member access expression where the underlying object is
8126 itself the result of a function call), turn it into a
8127 TARGET_EXPR here. It is important that EXPR be a
8128 TARGET_EXPR below since otherwise the INIT_EXPR will
8129 attempt to make a bitwise copy of EXPR to initialize
8130 VAR. */
8131 if (TREE_CODE (expr) != TARGET_EXPR)
8132 expr = get_target_expr (expr);
8134 /* If the initializer is constant, put it in DECL_INITIAL so we get
8135 static initialization and use in constant expressions. */
8136 init = maybe_constant_init (expr);
8137 if (TREE_CONSTANT (init))
8139 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
8141 /* 5.19 says that a constant expression can include an
8142 lvalue-rvalue conversion applied to "a glvalue of literal type
8143 that refers to a non-volatile temporary object initialized
8144 with a constant expression". Rather than try to communicate
8145 that this VAR_DECL is a temporary, just mark it constexpr.
8147 Currently this is only useful for initializer_list temporaries,
8148 since reference vars can't appear in constant expressions. */
8149 DECL_DECLARED_CONSTEXPR_P (var) = true;
8150 TREE_CONSTANT (var) = true;
8152 DECL_INITIAL (var) = init;
8153 init = NULL_TREE;
8155 else
8156 /* Create the INIT_EXPR that will initialize the temporary
8157 variable. */
8158 init = build2 (INIT_EXPR, type, var, expr);
8159 if (at_function_scope_p ())
8161 add_decl_expr (var);
8163 if (TREE_STATIC (var))
8164 init = add_stmt_to_compound (init, register_dtor_fn (var));
8165 else
8166 *cleanup = cxx_maybe_build_cleanup (var);
8168 /* We must be careful to destroy the temporary only
8169 after its initialization has taken place. If the
8170 initialization throws an exception, then the
8171 destructor should not be run. We cannot simply
8172 transform INIT into something like:
8174 (INIT, ({ CLEANUP_STMT; }))
8176 because emit_local_var always treats the
8177 initializer as a full-expression. Thus, the
8178 destructor would run too early; it would run at the
8179 end of initializing the reference variable, rather
8180 than at the end of the block enclosing the
8181 reference variable.
8183 The solution is to pass back a cleanup expression
8184 which the caller is responsible for attaching to
8185 the statement tree. */
8187 else
8189 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
8190 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
8191 static_aggregates = tree_cons (NULL_TREE, var,
8192 static_aggregates);
8195 *initp = init;
8196 return var;
8199 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
8200 initializing a variable of that TYPE. If DECL is non-NULL, it is
8201 the VAR_DECL being initialized with the EXPR. (In that case, the
8202 type of DECL will be TYPE.) If DECL is non-NULL, then CLEANUP must
8203 also be non-NULL, and with *CLEANUP initialized to NULL. Upon
8204 return, if *CLEANUP is no longer NULL, it will be an expression
8205 that should be pushed as a cleanup after the returned expression
8206 is used to initialize DECL.
8208 Return the converted expression. */
8210 tree
8211 initialize_reference (tree type, tree expr, tree decl, tree *cleanup,
8212 tsubst_flags_t complain)
8214 conversion *conv;
8215 void *p;
8217 if (type == error_mark_node || error_operand_p (expr))
8218 return error_mark_node;
8220 /* Get the high-water mark for the CONVERSION_OBSTACK. */
8221 p = conversion_obstack_alloc (0);
8223 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
8224 LOOKUP_NORMAL);
8225 if (!conv || conv->bad_p)
8227 if (complain & tf_error)
8229 if (!CP_TYPE_CONST_P (TREE_TYPE (type))
8230 && !TYPE_REF_IS_RVALUE (type)
8231 && !real_lvalue_p (expr))
8232 error ("invalid initialization of non-const reference of "
8233 "type %qT from an rvalue of type %qT",
8234 type, TREE_TYPE (expr));
8235 else
8236 error ("invalid initialization of reference of type "
8237 "%qT from expression of type %qT", type,
8238 TREE_TYPE (expr));
8240 return error_mark_node;
8243 /* If DECL is non-NULL, then this special rule applies:
8245 [class.temporary]
8247 The temporary to which the reference is bound or the temporary
8248 that is the complete object to which the reference is bound
8249 persists for the lifetime of the reference.
8251 The temporaries created during the evaluation of the expression
8252 initializing the reference, except the temporary to which the
8253 reference is bound, are destroyed at the end of the
8254 full-expression in which they are created.
8256 In that case, we store the converted expression into a new
8257 VAR_DECL in a new scope.
8259 However, we want to be careful not to create temporaries when
8260 they are not required. For example, given:
8262 struct B {};
8263 struct D : public B {};
8264 D f();
8265 const B& b = f();
8267 there is no need to copy the return value from "f"; we can just
8268 extend its lifetime. Similarly, given:
8270 struct S {};
8271 struct T { operator S(); };
8272 T t;
8273 const S& s = t;
8275 we can extend the lifetime of the return value of the conversion
8276 operator. */
8277 gcc_assert (conv->kind == ck_ref_bind);
8278 if (decl)
8280 tree var;
8281 tree base_conv_type;
8283 /* Skip over the REF_BIND. */
8284 conv = conv->u.next;
8285 /* If the next conversion is a BASE_CONV, skip that too -- but
8286 remember that the conversion was required. */
8287 if (conv->kind == ck_base)
8289 base_conv_type = conv->type;
8290 conv = conv->u.next;
8292 else
8293 base_conv_type = NULL_TREE;
8294 /* Perform the remainder of the conversion. */
8295 expr = convert_like_real (conv, expr,
8296 /*fn=*/NULL_TREE, /*argnum=*/0,
8297 /*inner=*/-1,
8298 /*issue_conversion_warnings=*/true,
8299 /*c_cast_p=*/false,
8300 tf_warning_or_error);
8301 if (error_operand_p (expr))
8302 expr = error_mark_node;
8303 else
8305 if (!lvalue_or_rvalue_with_address_p (expr))
8307 tree init;
8308 var = set_up_extended_ref_temp (decl, expr, cleanup, &init);
8309 /* Use its address to initialize the reference variable. */
8310 expr = build_address (var);
8311 if (base_conv_type)
8312 expr = convert_to_base (expr,
8313 build_pointer_type (base_conv_type),
8314 /*check_access=*/true,
8315 /*nonnull=*/true, complain);
8316 if (init)
8317 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
8319 else
8320 /* Take the address of EXPR. */
8321 expr = cp_build_addr_expr (expr, tf_warning_or_error);
8322 /* If a BASE_CONV was required, perform it now. */
8323 if (base_conv_type)
8324 expr = (perform_implicit_conversion
8325 (build_pointer_type (base_conv_type), expr,
8326 tf_warning_or_error));
8327 expr = build_nop (type, expr);
8330 else
8331 /* Perform the conversion. */
8332 expr = convert_like (conv, expr, tf_warning_or_error);
8334 /* Free all the conversions we allocated. */
8335 obstack_free (&conversion_obstack, p);
8337 return expr;
8340 /* Returns true iff TYPE is some variant of std::initializer_list. */
8342 bool
8343 is_std_init_list (tree type)
8345 /* Look through typedefs. */
8346 if (!TYPE_P (type))
8347 return false;
8348 type = TYPE_MAIN_VARIANT (type);
8349 return (CLASS_TYPE_P (type)
8350 && CP_TYPE_CONTEXT (type) == std_node
8351 && strcmp (TYPE_NAME_STRING (type), "initializer_list") == 0);
8354 /* Returns true iff DECL is a list constructor: i.e. a constructor which
8355 will accept an argument list of a single std::initializer_list<T>. */
8357 bool
8358 is_list_ctor (tree decl)
8360 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
8361 tree arg;
8363 if (!args || args == void_list_node)
8364 return false;
8366 arg = non_reference (TREE_VALUE (args));
8367 if (!is_std_init_list (arg))
8368 return false;
8370 args = TREE_CHAIN (args);
8372 if (args && args != void_list_node && !TREE_PURPOSE (args))
8373 /* There are more non-defaulted parms. */
8374 return false;
8376 return true;
8379 #include "gt-cp-call.h"