Remove outermost loop parameter.
[official-gcc/graphite-test-results.git] / gcc / cp / call.c
blobb9f1c7fd4ec7ba829917806d6a3ee6d8e2aa9455
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"
42 /* The various kinds of conversion. */
44 typedef enum conversion_kind {
45 ck_identity,
46 ck_lvalue,
47 ck_qual,
48 ck_std,
49 ck_ptr,
50 ck_pmem,
51 ck_base,
52 ck_ref_bind,
53 ck_user,
54 ck_ambig,
55 ck_list,
56 ck_aggr,
57 ck_rvalue
58 } conversion_kind;
60 /* The rank of the conversion. Order of the enumerals matters; better
61 conversions should come earlier in the list. */
63 typedef enum conversion_rank {
64 cr_identity,
65 cr_exact,
66 cr_promotion,
67 cr_std,
68 cr_pbool,
69 cr_user,
70 cr_ellipsis,
71 cr_bad
72 } conversion_rank;
74 /* An implicit conversion sequence, in the sense of [over.best.ics].
75 The first conversion to be performed is at the end of the chain.
76 That conversion is always a cr_identity conversion. */
78 typedef struct conversion conversion;
79 struct conversion {
80 /* The kind of conversion represented by this step. */
81 conversion_kind kind;
82 /* The rank of this conversion. */
83 conversion_rank rank;
84 BOOL_BITFIELD user_conv_p : 1;
85 BOOL_BITFIELD ellipsis_p : 1;
86 BOOL_BITFIELD this_p : 1;
87 BOOL_BITFIELD bad_p : 1;
88 /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
89 temporary should be created to hold the result of the
90 conversion. */
91 BOOL_BITFIELD need_temporary_p : 1;
92 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
93 from a pointer-to-derived to pointer-to-base is being performed. */
94 BOOL_BITFIELD base_p : 1;
95 /* If KIND is ck_ref_bind, true when either an lvalue reference is
96 being bound to an lvalue expression or an rvalue reference is
97 being bound to an rvalue expression. */
98 BOOL_BITFIELD rvaluedness_matches_p: 1;
99 BOOL_BITFIELD check_narrowing: 1;
100 /* The type of the expression resulting from the conversion. */
101 tree type;
102 union {
103 /* The next conversion in the chain. Since the conversions are
104 arranged from outermost to innermost, the NEXT conversion will
105 actually be performed before this conversion. This variant is
106 used only when KIND is neither ck_identity nor ck_ambig. */
107 conversion *next;
108 /* The expression at the beginning of the conversion chain. This
109 variant is used only if KIND is ck_identity or ck_ambig. */
110 tree expr;
111 /* The array of conversions for an initializer_list. */
112 conversion **list;
113 } u;
114 /* The function candidate corresponding to this conversion
115 sequence. This field is only used if KIND is ck_user. */
116 struct z_candidate *cand;
119 #define CONVERSION_RANK(NODE) \
120 ((NODE)->bad_p ? cr_bad \
121 : (NODE)->ellipsis_p ? cr_ellipsis \
122 : (NODE)->user_conv_p ? cr_user \
123 : (NODE)->rank)
125 static struct obstack conversion_obstack;
126 static bool conversion_obstack_initialized;
128 static struct z_candidate * tourney (struct z_candidate *);
129 static int equal_functions (tree, tree);
130 static int joust (struct z_candidate *, struct z_candidate *, bool);
131 static int compare_ics (conversion *, conversion *);
132 static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
133 static tree build_java_interface_fn_ref (tree, tree);
134 #define convert_like(CONV, EXPR, COMPLAIN) \
135 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \
136 /*issue_conversion_warnings=*/true, \
137 /*c_cast_p=*/false, (COMPLAIN))
138 #define convert_like_with_context(CONV, EXPR, FN, ARGNO, COMPLAIN ) \
139 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
140 /*issue_conversion_warnings=*/true, \
141 /*c_cast_p=*/false, (COMPLAIN))
142 static tree convert_like_real (conversion *, tree, tree, int, int, bool,
143 bool, tsubst_flags_t);
144 static void op_error (enum tree_code, enum tree_code, tree, tree,
145 tree, bool);
146 static VEC(tree,gc) *resolve_args (VEC(tree,gc) *);
147 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
148 static void print_z_candidate (const char *, struct z_candidate *);
149 static void print_z_candidates (struct z_candidate *);
150 static tree build_this (tree);
151 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
152 static bool any_strictly_viable (struct z_candidate *);
153 static struct z_candidate *add_template_candidate
154 (struct z_candidate **, tree, tree, tree, tree, const VEC(tree,gc) *,
155 tree, tree, tree, int, unification_kind_t);
156 static struct z_candidate *add_template_candidate_real
157 (struct z_candidate **, tree, tree, tree, tree, const VEC(tree,gc) *,
158 tree, tree, tree, int, tree, unification_kind_t);
159 static struct z_candidate *add_template_conv_candidate
160 (struct z_candidate **, tree, tree, tree, const VEC(tree,gc) *, tree,
161 tree, tree);
162 static void add_builtin_candidates
163 (struct z_candidate **, enum tree_code, enum tree_code,
164 tree, tree *, int);
165 static void add_builtin_candidate
166 (struct z_candidate **, enum tree_code, enum tree_code,
167 tree, tree, tree, tree *, tree *, int);
168 static bool is_complete (tree);
169 static void build_builtin_candidate
170 (struct z_candidate **, tree, tree, tree, tree *, tree *,
171 int);
172 static struct z_candidate *add_conv_candidate
173 (struct z_candidate **, tree, tree, tree, const VEC(tree,gc) *, tree,
174 tree);
175 static struct z_candidate *add_function_candidate
176 (struct z_candidate **, tree, tree, tree, const VEC(tree,gc) *, tree,
177 tree, int);
178 static conversion *implicit_conversion (tree, tree, tree, bool, int);
179 static conversion *standard_conversion (tree, tree, tree, bool, int);
180 static conversion *reference_binding (tree, tree, tree, bool, int);
181 static conversion *build_conv (conversion_kind, tree, conversion *);
182 static conversion *build_list_conv (tree, tree, int);
183 static bool is_subseq (conversion *, conversion *);
184 static conversion *maybe_handle_ref_bind (conversion **);
185 static void maybe_handle_implicit_object (conversion **);
186 static struct z_candidate *add_candidate
187 (struct z_candidate **, tree, tree, const VEC(tree,gc) *, size_t,
188 conversion **, tree, tree, int);
189 static tree source_type (conversion *);
190 static void add_warning (struct z_candidate *, struct z_candidate *);
191 static bool reference_compatible_p (tree, tree);
192 static conversion *convert_class_to_reference (tree, tree, tree, int);
193 static conversion *direct_reference_binding (tree, conversion *);
194 static bool promoted_arithmetic_type_p (tree);
195 static conversion *conditional_conversion (tree, tree);
196 static char *name_as_c_string (tree, tree, bool *);
197 static tree prep_operand (tree);
198 static void add_candidates (tree, tree, const VEC(tree,gc) *, tree, tree, bool,
199 tree, tree, int, struct z_candidate **);
200 static conversion *merge_conversion_sequences (conversion *, conversion *);
201 static bool magic_varargs_p (tree);
202 static tree build_temp (tree, tree, int, diagnostic_t *);
204 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
205 NAME can take many forms... */
207 bool
208 check_dtor_name (tree basetype, tree name)
210 /* Just accept something we've already complained about. */
211 if (name == error_mark_node)
212 return true;
214 if (TREE_CODE (name) == TYPE_DECL)
215 name = TREE_TYPE (name);
216 else if (TYPE_P (name))
217 /* OK */;
218 else if (TREE_CODE (name) == IDENTIFIER_NODE)
220 if ((MAYBE_CLASS_TYPE_P (basetype)
221 && name == constructor_name (basetype))
222 || (TREE_CODE (basetype) == ENUMERAL_TYPE
223 && name == TYPE_IDENTIFIER (basetype)))
224 return true;
225 else
226 name = get_type_value (name);
228 else
230 /* In the case of:
232 template <class T> struct S { ~S(); };
233 int i;
234 i.~S();
236 NAME will be a class template. */
237 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
238 return false;
241 if (!name || name == error_mark_node)
242 return false;
243 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
246 /* We want the address of a function or method. We avoid creating a
247 pointer-to-member function. */
249 tree
250 build_addr_func (tree function)
252 tree type = TREE_TYPE (function);
254 /* We have to do these by hand to avoid real pointer to member
255 functions. */
256 if (TREE_CODE (type) == METHOD_TYPE)
258 if (TREE_CODE (function) == OFFSET_REF)
260 tree object = build_address (TREE_OPERAND (function, 0));
261 return get_member_function_from_ptrfunc (&object,
262 TREE_OPERAND (function, 1));
264 function = build_address (function);
266 else
267 function = decay_conversion (function);
269 return function;
272 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
273 POINTER_TYPE to those. Note, pointer to member function types
274 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
275 two variants. build_call_a is the primitive taking an array of
276 arguments, while build_call_n is a wrapper that handles varargs. */
278 tree
279 build_call_n (tree function, int n, ...)
281 if (n == 0)
282 return build_call_a (function, 0, NULL);
283 else
285 tree *argarray = (tree *) alloca (n * sizeof (tree));
286 va_list ap;
287 int i;
289 va_start (ap, n);
290 for (i = 0; i < n; i++)
291 argarray[i] = va_arg (ap, tree);
292 va_end (ap);
293 return build_call_a (function, n, argarray);
297 tree
298 build_call_a (tree function, int n, tree *argarray)
300 int is_constructor = 0;
301 int nothrow;
302 tree decl;
303 tree result_type;
304 tree fntype;
305 int i;
307 function = build_addr_func (function);
309 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
310 fntype = TREE_TYPE (TREE_TYPE (function));
311 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
312 || TREE_CODE (fntype) == METHOD_TYPE);
313 result_type = TREE_TYPE (fntype);
314 /* An rvalue has no cv-qualifiers. */
315 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
316 result_type = cv_unqualified (result_type);
318 if (TREE_CODE (function) == ADDR_EXPR
319 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
321 decl = TREE_OPERAND (function, 0);
322 if (!TREE_USED (decl))
324 /* We invoke build_call directly for several library
325 functions. These may have been declared normally if
326 we're building libgcc, so we can't just check
327 DECL_ARTIFICIAL. */
328 gcc_assert (DECL_ARTIFICIAL (decl)
329 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
330 "__", 2));
331 mark_used (decl);
334 else
335 decl = NULL_TREE;
337 /* We check both the decl and the type; a function may be known not to
338 throw without being declared throw(). */
339 nothrow = ((decl && TREE_NOTHROW (decl))
340 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
342 if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
343 current_function_returns_abnormally = 1;
345 if (decl && TREE_DEPRECATED (decl))
346 warn_deprecated_use (decl, NULL_TREE);
347 require_complete_eh_spec_types (fntype, decl);
349 if (decl && DECL_CONSTRUCTOR_P (decl))
350 is_constructor = 1;
352 /* Don't pass empty class objects by value. This is useful
353 for tags in STL, which are used to control overload resolution.
354 We don't need to handle other cases of copying empty classes. */
355 if (! decl || ! DECL_BUILT_IN (decl))
356 for (i = 0; i < n; i++)
357 if (is_empty_class (TREE_TYPE (argarray[i]))
358 && ! TREE_ADDRESSABLE (TREE_TYPE (argarray[i])))
360 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (argarray[i]));
361 argarray[i] = build2 (COMPOUND_EXPR, TREE_TYPE (t),
362 argarray[i], t);
365 function = build_call_array_loc (input_location,
366 result_type, function, n, argarray);
367 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
368 TREE_NOTHROW (function) = nothrow;
370 return function;
373 /* Build something of the form ptr->method (args)
374 or object.method (args). This can also build
375 calls to constructors, and find friends.
377 Member functions always take their class variable
378 as a pointer.
380 INSTANCE is a class instance.
382 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
384 PARMS help to figure out what that NAME really refers to.
386 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
387 down to the real instance type to use for access checking. We need this
388 information to get protected accesses correct.
390 FLAGS is the logical disjunction of zero or more LOOKUP_
391 flags. See cp-tree.h for more info.
393 If this is all OK, calls build_function_call with the resolved
394 member function.
396 This function must also handle being called to perform
397 initialization, promotion/coercion of arguments, and
398 instantiation of default parameters.
400 Note that NAME may refer to an instance variable name. If
401 `operator()()' is defined for the type of that field, then we return
402 that result. */
404 /* New overloading code. */
406 typedef struct z_candidate z_candidate;
408 typedef struct candidate_warning candidate_warning;
409 struct candidate_warning {
410 z_candidate *loser;
411 candidate_warning *next;
414 struct z_candidate {
415 /* The FUNCTION_DECL that will be called if this candidate is
416 selected by overload resolution. */
417 tree fn;
418 /* If not NULL_TREE, the first argument to use when calling this
419 function. */
420 tree first_arg;
421 /* The rest of the arguments to use when calling this function. If
422 there are no further arguments this may be NULL or it may be an
423 empty vector. */
424 const VEC(tree,gc) *args;
425 /* The implicit conversion sequences for each of the arguments to
426 FN. */
427 conversion **convs;
428 /* The number of implicit conversion sequences. */
429 size_t num_convs;
430 /* If FN is a user-defined conversion, the standard conversion
431 sequence from the type returned by FN to the desired destination
432 type. */
433 conversion *second_conv;
434 int viable;
435 /* If FN is a member function, the binfo indicating the path used to
436 qualify the name of FN at the call site. This path is used to
437 determine whether or not FN is accessible if it is selected by
438 overload resolution. The DECL_CONTEXT of FN will always be a
439 (possibly improper) base of this binfo. */
440 tree access_path;
441 /* If FN is a non-static member function, the binfo indicating the
442 subobject to which the `this' pointer should be converted if FN
443 is selected by overload resolution. The type pointed to the by
444 the `this' pointer must correspond to the most derived class
445 indicated by the CONVERSION_PATH. */
446 tree conversion_path;
447 tree template_decl;
448 tree explicit_targs;
449 candidate_warning *warnings;
450 z_candidate *next;
453 /* Returns true iff T is a null pointer constant in the sense of
454 [conv.ptr]. */
456 bool
457 null_ptr_cst_p (tree t)
459 /* [conv.ptr]
461 A null pointer constant is an integral constant expression
462 (_expr.const_) rvalue of integer type that evaluates to zero or
463 an rvalue of type std::nullptr_t. */
464 t = integral_constant_value (t);
465 if (t == null_node
466 || NULLPTR_TYPE_P (TREE_TYPE (t)))
467 return true;
468 if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t))
470 STRIP_NOPS (t);
471 if (!TREE_OVERFLOW (t))
472 return true;
474 return false;
477 /* Returns nonzero if PARMLIST consists of only default parms and/or
478 ellipsis. */
480 bool
481 sufficient_parms_p (const_tree parmlist)
483 for (; parmlist && parmlist != void_list_node;
484 parmlist = TREE_CHAIN (parmlist))
485 if (!TREE_PURPOSE (parmlist))
486 return false;
487 return true;
490 /* Allocate N bytes of memory from the conversion obstack. The memory
491 is zeroed before being returned. */
493 static void *
494 conversion_obstack_alloc (size_t n)
496 void *p;
497 if (!conversion_obstack_initialized)
499 gcc_obstack_init (&conversion_obstack);
500 conversion_obstack_initialized = true;
502 p = obstack_alloc (&conversion_obstack, n);
503 memset (p, 0, n);
504 return p;
507 /* Dynamically allocate a conversion. */
509 static conversion *
510 alloc_conversion (conversion_kind kind)
512 conversion *c;
513 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
514 c->kind = kind;
515 return c;
518 #ifdef ENABLE_CHECKING
520 /* Make sure that all memory on the conversion obstack has been
521 freed. */
523 void
524 validate_conversion_obstack (void)
526 if (conversion_obstack_initialized)
527 gcc_assert ((obstack_next_free (&conversion_obstack)
528 == obstack_base (&conversion_obstack)));
531 #endif /* ENABLE_CHECKING */
533 /* Dynamically allocate an array of N conversions. */
535 static conversion **
536 alloc_conversions (size_t n)
538 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
541 static conversion *
542 build_conv (conversion_kind code, tree type, conversion *from)
544 conversion *t;
545 conversion_rank rank = CONVERSION_RANK (from);
547 /* Note that the caller is responsible for filling in t->cand for
548 user-defined conversions. */
549 t = alloc_conversion (code);
550 t->type = type;
551 t->u.next = from;
553 switch (code)
555 case ck_ptr:
556 case ck_pmem:
557 case ck_base:
558 case ck_std:
559 if (rank < cr_std)
560 rank = cr_std;
561 break;
563 case ck_qual:
564 if (rank < cr_exact)
565 rank = cr_exact;
566 break;
568 default:
569 break;
571 t->rank = rank;
572 t->user_conv_p = (code == ck_user || from->user_conv_p);
573 t->bad_p = from->bad_p;
574 t->base_p = false;
575 return t;
578 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
579 specialization of std::initializer_list<T>, if such a conversion is
580 possible. */
582 static conversion *
583 build_list_conv (tree type, tree ctor, int flags)
585 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
586 unsigned len = CONSTRUCTOR_NELTS (ctor);
587 conversion **subconvs = alloc_conversions (len);
588 conversion *t;
589 unsigned i;
590 tree val;
592 /* Within a list-initialization we can have more user-defined
593 conversions. */
594 flags &= ~LOOKUP_NO_CONVERSION;
595 /* But no narrowing conversions. */
596 flags |= LOOKUP_NO_NARROWING;
598 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
600 conversion *sub
601 = implicit_conversion (elttype, TREE_TYPE (val), val,
602 false, flags);
603 if (sub == NULL)
604 return NULL;
606 subconvs[i] = sub;
609 t = alloc_conversion (ck_list);
610 t->type = type;
611 t->u.list = subconvs;
612 t->rank = cr_exact;
614 for (i = 0; i < len; ++i)
616 conversion *sub = subconvs[i];
617 if (sub->rank > t->rank)
618 t->rank = sub->rank;
619 if (sub->user_conv_p)
620 t->user_conv_p = true;
621 if (sub->bad_p)
622 t->bad_p = true;
625 return t;
628 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
629 aggregate class, if such a conversion is possible. */
631 static conversion *
632 build_aggr_conv (tree type, tree ctor, int flags)
634 unsigned HOST_WIDE_INT i = 0;
635 conversion *c;
636 tree field = next_initializable_field (TYPE_FIELDS (type));
637 tree empty_ctor = NULL_TREE;
639 for (; field; field = next_initializable_field (TREE_CHAIN (field)))
641 if (i < CONSTRUCTOR_NELTS (ctor))
643 constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i);
644 if (!can_convert_arg (TREE_TYPE (field), TREE_TYPE (ce->value),
645 ce->value, flags))
646 return NULL;
647 ++i;
648 if (TREE_CODE (type) == UNION_TYPE)
649 break;
651 else
653 if (empty_ctor == NULL_TREE)
654 empty_ctor = build_constructor (init_list_type_node, NULL);
655 if (!can_convert_arg (TREE_TYPE (field), TREE_TYPE (empty_ctor),
656 empty_ctor, flags))
657 return NULL;
661 if (i < CONSTRUCTOR_NELTS (ctor))
662 return NULL;
664 c = alloc_conversion (ck_aggr);
665 c->type = type;
666 c->rank = cr_exact;
667 c->user_conv_p = true;
668 c->u.next = NULL;
669 return c;
672 /* Build a representation of the identity conversion from EXPR to
673 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
675 static conversion *
676 build_identity_conv (tree type, tree expr)
678 conversion *c;
680 c = alloc_conversion (ck_identity);
681 c->type = type;
682 c->u.expr = expr;
684 return c;
687 /* Converting from EXPR to TYPE was ambiguous in the sense that there
688 were multiple user-defined conversions to accomplish the job.
689 Build a conversion that indicates that ambiguity. */
691 static conversion *
692 build_ambiguous_conv (tree type, tree expr)
694 conversion *c;
696 c = alloc_conversion (ck_ambig);
697 c->type = type;
698 c->u.expr = expr;
700 return c;
703 tree
704 strip_top_quals (tree t)
706 if (TREE_CODE (t) == ARRAY_TYPE)
707 return t;
708 return cp_build_qualified_type (t, 0);
711 /* Returns the standard conversion path (see [conv]) from type FROM to type
712 TO, if any. For proper handling of null pointer constants, you must
713 also pass the expression EXPR to convert from. If C_CAST_P is true,
714 this conversion is coming from a C-style cast. */
716 static conversion *
717 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
718 int flags)
720 enum tree_code fcode, tcode;
721 conversion *conv;
722 bool fromref = false;
724 to = non_reference (to);
725 if (TREE_CODE (from) == REFERENCE_TYPE)
727 fromref = true;
728 from = TREE_TYPE (from);
730 to = strip_top_quals (to);
731 from = strip_top_quals (from);
733 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
734 && expr && type_unknown_p (expr))
736 tsubst_flags_t tflags = tf_conv;
737 if (!(flags & LOOKUP_PROTECT))
738 tflags |= tf_no_access_control;
739 expr = instantiate_type (to, expr, tflags);
740 if (expr == error_mark_node)
741 return NULL;
742 from = TREE_TYPE (expr);
745 fcode = TREE_CODE (from);
746 tcode = TREE_CODE (to);
748 conv = build_identity_conv (from, expr);
749 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
751 from = type_decays_to (from);
752 fcode = TREE_CODE (from);
753 conv = build_conv (ck_lvalue, from, conv);
755 else if (fromref || (expr && lvalue_p (expr)))
757 if (expr)
759 tree bitfield_type;
760 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
761 if (bitfield_type)
763 from = strip_top_quals (bitfield_type);
764 fcode = TREE_CODE (from);
767 conv = build_conv (ck_rvalue, from, conv);
770 /* Allow conversion between `__complex__' data types. */
771 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
773 /* The standard conversion sequence to convert FROM to TO is
774 the standard conversion sequence to perform componentwise
775 conversion. */
776 conversion *part_conv = standard_conversion
777 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags);
779 if (part_conv)
781 conv = build_conv (part_conv->kind, to, conv);
782 conv->rank = part_conv->rank;
784 else
785 conv = NULL;
787 return conv;
790 if (same_type_p (from, to))
791 return conv;
793 /* [conv.ptr]
794 A null pointer constant can be converted to a pointer type; ... A
795 null pointer constant of integral type can be converted to an
796 rvalue of type std::nullptr_t. */
797 if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to)
798 || NULLPTR_TYPE_P (to))
799 && expr && null_ptr_cst_p (expr))
800 conv = build_conv (ck_std, to, conv);
801 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
802 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
804 /* For backwards brain damage compatibility, allow interconversion of
805 pointers and integers with a pedwarn. */
806 conv = build_conv (ck_std, to, conv);
807 conv->bad_p = true;
809 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
811 /* For backwards brain damage compatibility, allow interconversion of
812 enums and integers with a pedwarn. */
813 conv = build_conv (ck_std, to, conv);
814 conv->bad_p = true;
816 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
817 || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
819 tree to_pointee;
820 tree from_pointee;
822 if (tcode == POINTER_TYPE
823 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
824 TREE_TYPE (to)))
826 else if (VOID_TYPE_P (TREE_TYPE (to))
827 && !TYPE_PTRMEM_P (from)
828 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
830 from = build_pointer_type
831 (cp_build_qualified_type (void_type_node,
832 cp_type_quals (TREE_TYPE (from))));
833 conv = build_conv (ck_ptr, from, conv);
835 else if (TYPE_PTRMEM_P (from))
837 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
838 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
840 if (DERIVED_FROM_P (fbase, tbase)
841 && (same_type_ignoring_top_level_qualifiers_p
842 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
843 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
845 from = build_ptrmem_type (tbase,
846 TYPE_PTRMEM_POINTED_TO_TYPE (from));
847 conv = build_conv (ck_pmem, from, conv);
849 else if (!same_type_p (fbase, tbase))
850 return NULL;
852 else if (CLASS_TYPE_P (TREE_TYPE (from))
853 && CLASS_TYPE_P (TREE_TYPE (to))
854 /* [conv.ptr]
856 An rvalue of type "pointer to cv D," where D is a
857 class type, can be converted to an rvalue of type
858 "pointer to cv B," where B is a base class (clause
859 _class.derived_) of D. If B is an inaccessible
860 (clause _class.access_) or ambiguous
861 (_class.member.lookup_) base class of D, a program
862 that necessitates this conversion is ill-formed.
863 Therefore, we use DERIVED_FROM_P, and do not check
864 access or uniqueness. */
865 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
867 from =
868 cp_build_qualified_type (TREE_TYPE (to),
869 cp_type_quals (TREE_TYPE (from)));
870 from = build_pointer_type (from);
871 conv = build_conv (ck_ptr, from, conv);
872 conv->base_p = true;
875 if (tcode == POINTER_TYPE)
877 to_pointee = TREE_TYPE (to);
878 from_pointee = TREE_TYPE (from);
880 else
882 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
883 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
886 if (same_type_p (from, to))
887 /* OK */;
888 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
889 /* In a C-style cast, we ignore CV-qualification because we
890 are allowed to perform a static_cast followed by a
891 const_cast. */
892 conv = build_conv (ck_qual, to, conv);
893 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
894 conv = build_conv (ck_qual, to, conv);
895 else if (expr && string_conv_p (to, expr, 0))
896 /* converting from string constant to char *. */
897 conv = build_conv (ck_qual, to, conv);
898 else if (ptr_reasonably_similar (to_pointee, from_pointee))
900 conv = build_conv (ck_ptr, to, conv);
901 conv->bad_p = true;
903 else
904 return NULL;
906 from = to;
908 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
910 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
911 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
912 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
913 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
915 if (!DERIVED_FROM_P (fbase, tbase)
916 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
917 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
918 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
919 || cp_type_quals (fbase) != cp_type_quals (tbase))
920 return NULL;
922 from = build_memfn_type (fromfn, tbase, cp_type_quals (tbase));
923 from = build_ptrmemfunc_type (build_pointer_type (from));
924 conv = build_conv (ck_pmem, from, conv);
925 conv->base_p = true;
927 else if (tcode == BOOLEAN_TYPE)
929 /* [conv.bool]
931 An rvalue of arithmetic, unscoped enumeration, pointer, or
932 pointer to member type can be converted to an rvalue of type
933 bool. ... An rvalue of type std::nullptr_t can be converted
934 to an rvalue of type bool; */
935 if (ARITHMETIC_TYPE_P (from)
936 || UNSCOPED_ENUM_P (from)
937 || fcode == POINTER_TYPE
938 || TYPE_PTR_TO_MEMBER_P (from)
939 || NULLPTR_TYPE_P (from))
941 conv = build_conv (ck_std, to, conv);
942 if (fcode == POINTER_TYPE
943 || TYPE_PTRMEM_P (from)
944 || (TYPE_PTRMEMFUNC_P (from)
945 && conv->rank < cr_pbool)
946 || NULLPTR_TYPE_P (from))
947 conv->rank = cr_pbool;
948 return conv;
951 return NULL;
953 /* We don't check for ENUMERAL_TYPE here because there are no standard
954 conversions to enum type. */
955 /* As an extension, allow conversion to complex type. */
956 else if (ARITHMETIC_TYPE_P (to))
958 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE)
959 || SCOPED_ENUM_P (from))
960 return NULL;
961 conv = build_conv (ck_std, to, conv);
963 /* Give this a better rank if it's a promotion. */
964 if (same_type_p (to, type_promotes_to (from))
965 && conv->u.next->rank <= cr_promotion)
966 conv->rank = cr_promotion;
968 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
969 && vector_types_convertible_p (from, to, false))
970 return build_conv (ck_std, to, conv);
971 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
972 && is_properly_derived_from (from, to))
974 if (conv->kind == ck_rvalue)
975 conv = conv->u.next;
976 conv = build_conv (ck_base, to, conv);
977 /* The derived-to-base conversion indicates the initialization
978 of a parameter with base type from an object of a derived
979 type. A temporary object is created to hold the result of
980 the conversion unless we're binding directly to a reference. */
981 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
983 else
984 return NULL;
986 if (flags & LOOKUP_NO_NARROWING)
987 conv->check_narrowing = true;
989 return conv;
992 /* Returns nonzero if T1 is reference-related to T2. */
994 bool
995 reference_related_p (tree t1, tree t2)
997 t1 = TYPE_MAIN_VARIANT (t1);
998 t2 = TYPE_MAIN_VARIANT (t2);
1000 /* [dcl.init.ref]
1002 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1003 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
1004 of T2. */
1005 return (same_type_p (t1, t2)
1006 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1007 && DERIVED_FROM_P (t1, t2)));
1010 /* Returns nonzero if T1 is reference-compatible with T2. */
1012 static bool
1013 reference_compatible_p (tree t1, tree t2)
1015 /* [dcl.init.ref]
1017 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
1018 reference-related to T2 and cv1 is the same cv-qualification as,
1019 or greater cv-qualification than, cv2. */
1020 return (reference_related_p (t1, t2)
1021 && at_least_as_qualified_p (t1, t2));
1024 /* Determine whether or not the EXPR (of class type S) can be
1025 converted to T as in [over.match.ref]. */
1027 static conversion *
1028 convert_class_to_reference (tree reference_type, tree s, tree expr, int flags)
1030 tree conversions;
1031 tree first_arg;
1032 conversion *conv;
1033 tree t;
1034 struct z_candidate *candidates;
1035 struct z_candidate *cand;
1036 bool any_viable_p;
1038 conversions = lookup_conversions (s, /*lookup_template_convs_p=*/true);
1039 if (!conversions)
1040 return NULL;
1042 /* [over.match.ref]
1044 Assuming that "cv1 T" is the underlying type of the reference
1045 being initialized, and "cv S" is the type of the initializer
1046 expression, with S a class type, the candidate functions are
1047 selected as follows:
1049 --The conversion functions of S and its base classes are
1050 considered. Those that are not hidden within S and yield type
1051 "reference to cv2 T2", where "cv1 T" is reference-compatible
1052 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
1054 The argument list has one argument, which is the initializer
1055 expression. */
1057 candidates = 0;
1059 /* Conceptually, we should take the address of EXPR and put it in
1060 the argument list. Unfortunately, however, that can result in
1061 error messages, which we should not issue now because we are just
1062 trying to find a conversion operator. Therefore, we use NULL,
1063 cast to the appropriate type. */
1064 first_arg = build_int_cst (build_pointer_type (s), 0);
1066 t = TREE_TYPE (reference_type);
1068 /* We're performing a user-defined conversion to a desired type, so set
1069 this for the benefit of add_candidates. */
1070 flags |= LOOKUP_NO_CONVERSION;
1072 for (; conversions; conversions = TREE_CHAIN (conversions))
1074 tree fns = TREE_VALUE (conversions);
1075 tree binfo = TREE_PURPOSE (conversions);
1076 struct z_candidate *old_candidates = candidates;;
1078 add_candidates (fns, first_arg, NULL, reference_type,
1079 NULL_TREE, false,
1080 binfo, TYPE_BINFO (s),
1081 flags, &candidates);
1083 for (cand = candidates; cand != old_candidates; cand = cand->next)
1085 /* Now, see if the conversion function really returns
1086 an lvalue of the appropriate type. From the
1087 point of view of unification, simply returning an
1088 rvalue of the right type is good enough. */
1089 tree f = cand->fn;
1090 tree t2 = TREE_TYPE (TREE_TYPE (f));
1091 if (TREE_CODE (t2) != REFERENCE_TYPE
1092 || !reference_compatible_p (t, TREE_TYPE (t2)))
1094 cand->viable = 0;
1096 else
1098 conversion *identity_conv;
1099 /* Build a standard conversion sequence indicating the
1100 binding from the reference type returned by the
1101 function to the desired REFERENCE_TYPE. */
1102 identity_conv
1103 = build_identity_conv (TREE_TYPE (TREE_TYPE
1104 (TREE_TYPE (cand->fn))),
1105 NULL_TREE);
1106 cand->second_conv
1107 = (direct_reference_binding
1108 (reference_type, identity_conv));
1109 cand->second_conv->rvaluedness_matches_p
1110 = TYPE_REF_IS_RVALUE (TREE_TYPE (TREE_TYPE (cand->fn)))
1111 == TYPE_REF_IS_RVALUE (reference_type);
1112 cand->second_conv->bad_p |= cand->convs[0]->bad_p;
1114 /* Don't allow binding of lvalues to rvalue references. */
1115 if (TYPE_REF_IS_RVALUE (reference_type)
1116 && !TYPE_REF_IS_RVALUE (TREE_TYPE (TREE_TYPE (cand->fn))))
1117 cand->second_conv->bad_p = true;
1122 candidates = splice_viable (candidates, pedantic, &any_viable_p);
1123 /* If none of the conversion functions worked out, let our caller
1124 know. */
1125 if (!any_viable_p)
1126 return NULL;
1128 cand = tourney (candidates);
1129 if (!cand)
1130 return NULL;
1132 /* Now that we know that this is the function we're going to use fix
1133 the dummy first argument. */
1134 gcc_assert (cand->first_arg == NULL_TREE
1135 || integer_zerop (cand->first_arg));
1136 cand->first_arg = build_this (expr);
1138 /* Build a user-defined conversion sequence representing the
1139 conversion. */
1140 conv = build_conv (ck_user,
1141 TREE_TYPE (TREE_TYPE (cand->fn)),
1142 build_identity_conv (TREE_TYPE (expr), expr));
1143 conv->cand = cand;
1145 if (cand->viable == -1)
1146 conv->bad_p = true;
1148 /* Merge it with the standard conversion sequence from the
1149 conversion function's return type to the desired type. */
1150 cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
1152 return cand->second_conv;
1155 /* A reference of the indicated TYPE is being bound directly to the
1156 expression represented by the implicit conversion sequence CONV.
1157 Return a conversion sequence for this binding. */
1159 static conversion *
1160 direct_reference_binding (tree type, conversion *conv)
1162 tree t;
1164 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1165 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1167 t = TREE_TYPE (type);
1169 /* [over.ics.rank]
1171 When a parameter of reference type binds directly
1172 (_dcl.init.ref_) to an argument expression, the implicit
1173 conversion sequence is the identity conversion, unless the
1174 argument expression has a type that is a derived class of the
1175 parameter type, in which case the implicit conversion sequence is
1176 a derived-to-base Conversion.
1178 If the parameter binds directly to the result of applying a
1179 conversion function to the argument expression, the implicit
1180 conversion sequence is a user-defined conversion sequence
1181 (_over.ics.user_), with the second standard conversion sequence
1182 either an identity conversion or, if the conversion function
1183 returns an entity of a type that is a derived class of the
1184 parameter type, a derived-to-base conversion. */
1185 if (!same_type_ignoring_top_level_qualifiers_p (t, conv->type))
1187 /* Represent the derived-to-base conversion. */
1188 conv = build_conv (ck_base, t, conv);
1189 /* We will actually be binding to the base-class subobject in
1190 the derived class, so we mark this conversion appropriately.
1191 That way, convert_like knows not to generate a temporary. */
1192 conv->need_temporary_p = false;
1194 return build_conv (ck_ref_bind, type, conv);
1197 /* Returns the conversion path from type FROM to reference type TO for
1198 purposes of reference binding. For lvalue binding, either pass a
1199 reference type to FROM or an lvalue expression to EXPR. If the
1200 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1201 the conversion returned. If C_CAST_P is true, this
1202 conversion is coming from a C-style cast. */
1204 static conversion *
1205 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags)
1207 conversion *conv = NULL;
1208 tree to = TREE_TYPE (rto);
1209 tree from = rfrom;
1210 tree tfrom;
1211 bool related_p;
1212 bool compatible_p;
1213 cp_lvalue_kind is_lvalue = clk_none;
1215 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1217 expr = instantiate_type (to, expr, tf_none);
1218 if (expr == error_mark_node)
1219 return NULL;
1220 from = TREE_TYPE (expr);
1223 if (TREE_CODE (from) == REFERENCE_TYPE)
1225 /* Anything with reference type is an lvalue. */
1226 is_lvalue = clk_ordinary;
1227 from = TREE_TYPE (from);
1230 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1232 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1233 conv = implicit_conversion (to, from, expr, c_cast_p,
1234 flags);
1235 if (!CLASS_TYPE_P (to)
1236 && CONSTRUCTOR_NELTS (expr) == 1)
1238 expr = CONSTRUCTOR_ELT (expr, 0)->value;
1239 if (error_operand_p (expr))
1240 return NULL;
1241 from = TREE_TYPE (expr);
1245 if (is_lvalue == clk_none && expr)
1246 is_lvalue = real_lvalue_p (expr);
1248 tfrom = from;
1249 if ((is_lvalue & clk_bitfield) != 0)
1250 tfrom = unlowered_expr_type (expr);
1252 /* Figure out whether or not the types are reference-related and
1253 reference compatible. We have do do this after stripping
1254 references from FROM. */
1255 related_p = reference_related_p (to, tfrom);
1256 /* If this is a C cast, first convert to an appropriately qualified
1257 type, so that we can later do a const_cast to the desired type. */
1258 if (related_p && c_cast_p
1259 && !at_least_as_qualified_p (to, tfrom))
1260 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1261 compatible_p = reference_compatible_p (to, tfrom);
1263 /* Directly bind reference when target expression's type is compatible with
1264 the reference and expression is an lvalue. In DR391, the wording in
1265 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1266 const and rvalue references to rvalues of compatible class type.
1267 We should also do direct bindings for non-class "rvalues" derived from
1268 rvalue references. */
1269 if (compatible_p
1270 && (is_lvalue
1271 || (((CP_TYPE_CONST_NON_VOLATILE_P (to)
1272 && !(flags & LOOKUP_NO_TEMP_BIND))
1273 || TYPE_REF_IS_RVALUE (rto))
1274 && (CLASS_TYPE_P (from) || (expr && lvalue_p (expr))))))
1276 /* [dcl.init.ref]
1278 If the initializer expression
1280 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1281 is reference-compatible with "cv2 T2,"
1283 the reference is bound directly to the initializer expression
1284 lvalue.
1286 [...]
1287 If the initializer expression is an rvalue, with T2 a class type,
1288 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1289 is bound to the object represented by the rvalue or to a sub-object
1290 within that object. */
1292 conv = build_identity_conv (tfrom, expr);
1293 conv = direct_reference_binding (rto, conv);
1295 if (flags & LOOKUP_PREFER_RVALUE)
1296 /* The top-level caller requested that we pretend that the lvalue
1297 be treated as an rvalue. */
1298 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1299 else
1300 conv->rvaluedness_matches_p
1301 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1303 if ((is_lvalue & clk_bitfield) != 0
1304 || ((is_lvalue & clk_packed) != 0 && !TYPE_PACKED (to)))
1305 /* For the purposes of overload resolution, we ignore the fact
1306 this expression is a bitfield or packed field. (In particular,
1307 [over.ics.ref] says specifically that a function with a
1308 non-const reference parameter is viable even if the
1309 argument is a bitfield.)
1311 However, when we actually call the function we must create
1312 a temporary to which to bind the reference. If the
1313 reference is volatile, or isn't const, then we cannot make
1314 a temporary, so we just issue an error when the conversion
1315 actually occurs. */
1316 conv->need_temporary_p = true;
1318 /* Don't allow binding of lvalues to rvalue references. */
1319 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1320 && !(flags & LOOKUP_PREFER_RVALUE))
1321 conv->bad_p = true;
1323 return conv;
1325 /* [class.conv.fct] A conversion function is never used to convert a
1326 (possibly cv-qualified) object to the (possibly cv-qualified) same
1327 object type (or a reference to it), to a (possibly cv-qualified) base
1328 class of that type (or a reference to it).... */
1329 else if (CLASS_TYPE_P (from) && !related_p
1330 && !(flags & LOOKUP_NO_CONVERSION))
1332 /* [dcl.init.ref]
1334 If the initializer expression
1336 -- has a class type (i.e., T2 is a class type) can be
1337 implicitly converted to an lvalue of type "cv3 T3," where
1338 "cv1 T1" is reference-compatible with "cv3 T3". (this
1339 conversion is selected by enumerating the applicable
1340 conversion functions (_over.match.ref_) and choosing the
1341 best one through overload resolution. (_over.match_).
1343 the reference is bound to the lvalue result of the conversion
1344 in the second case. */
1345 conv = convert_class_to_reference (rto, from, expr, flags);
1346 if (conv)
1347 return conv;
1350 /* From this point on, we conceptually need temporaries, even if we
1351 elide them. Only the cases above are "direct bindings". */
1352 if (flags & LOOKUP_NO_TEMP_BIND)
1353 return NULL;
1355 /* [over.ics.rank]
1357 When a parameter of reference type is not bound directly to an
1358 argument expression, the conversion sequence is the one required
1359 to convert the argument expression to the underlying type of the
1360 reference according to _over.best.ics_. Conceptually, this
1361 conversion sequence corresponds to copy-initializing a temporary
1362 of the underlying type with the argument expression. Any
1363 difference in top-level cv-qualification is subsumed by the
1364 initialization itself and does not constitute a conversion. */
1366 /* [dcl.init.ref]
1368 Otherwise, the reference shall be to a non-volatile const type.
1370 Under C++0x, [8.5.3/5 dcl.init.ref] it may also be an rvalue reference */
1371 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1372 return NULL;
1374 /* [dcl.init.ref]
1376 Otherwise, a temporary of type "cv1 T1" is created and
1377 initialized from the initializer expression using the rules for a
1378 non-reference copy initialization. If T1 is reference-related to
1379 T2, cv1 must be the same cv-qualification as, or greater
1380 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1381 if (related_p && !at_least_as_qualified_p (to, from))
1382 return NULL;
1384 /* We're generating a temporary now, but don't bind any more in the
1385 conversion (specifically, don't slice the temporary returned by a
1386 conversion operator). */
1387 flags |= LOOKUP_NO_TEMP_BIND;
1389 /* Temporaries are copy-initialized, except for this hack to allow
1390 explicit conversion ops to the copy ctor. See also
1391 add_function_candidate. */
1392 if (!(flags & LOOKUP_COPY_PARM))
1393 flags |= LOOKUP_ONLYCONVERTING;
1395 if (!conv)
1396 conv = implicit_conversion (to, from, expr, c_cast_p,
1397 flags);
1398 if (!conv)
1399 return NULL;
1401 conv = build_conv (ck_ref_bind, rto, conv);
1402 /* This reference binding, unlike those above, requires the
1403 creation of a temporary. */
1404 conv->need_temporary_p = true;
1405 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1407 return conv;
1410 /* Returns the implicit conversion sequence (see [over.ics]) from type
1411 FROM to type TO. The optional expression EXPR may affect the
1412 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1413 true, this conversion is coming from a C-style cast. */
1415 static conversion *
1416 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1417 int flags)
1419 conversion *conv;
1421 if (from == error_mark_node || to == error_mark_node
1422 || expr == error_mark_node)
1423 return NULL;
1425 if (TREE_CODE (to) == REFERENCE_TYPE)
1426 conv = reference_binding (to, from, expr, c_cast_p, flags);
1427 else
1428 conv = standard_conversion (to, from, expr, c_cast_p, flags);
1430 if (conv)
1431 return conv;
1433 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1435 if (is_std_init_list (to))
1436 return build_list_conv (to, expr, flags);
1438 /* Allow conversion from an initializer-list with one element to a
1439 scalar type. */
1440 if (SCALAR_TYPE_P (to))
1442 int nelts = CONSTRUCTOR_NELTS (expr);
1443 tree elt;
1445 if (nelts == 0)
1446 elt = integer_zero_node;
1447 else if (nelts == 1)
1448 elt = CONSTRUCTOR_ELT (expr, 0)->value;
1449 else
1450 elt = error_mark_node;
1452 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
1453 c_cast_p, flags);
1454 if (conv)
1456 conv->check_narrowing = true;
1457 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
1458 /* Too many levels of braces, i.e. '{{1}}'. */
1459 conv->bad_p = true;
1460 return conv;
1465 if (expr != NULL_TREE
1466 && (MAYBE_CLASS_TYPE_P (from)
1467 || MAYBE_CLASS_TYPE_P (to))
1468 && (flags & LOOKUP_NO_CONVERSION) == 0)
1470 struct z_candidate *cand;
1471 int convflags = (flags & (LOOKUP_NO_TEMP_BIND|LOOKUP_ONLYCONVERTING
1472 |LOOKUP_NO_NARROWING));
1474 if (CLASS_TYPE_P (to)
1475 && !CLASSTYPE_NON_AGGREGATE (complete_type (to))
1476 && BRACE_ENCLOSED_INITIALIZER_P (expr))
1477 return build_aggr_conv (to, expr, flags);
1479 cand = build_user_type_conversion_1 (to, expr, convflags);
1480 if (cand)
1481 conv = cand->second_conv;
1483 /* We used to try to bind a reference to a temporary here, but that
1484 is now handled after the recursive call to this function at the end
1485 of reference_binding. */
1486 return conv;
1489 return NULL;
1492 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1493 functions. ARGS will not be changed until a single candidate is
1494 selected. */
1496 static struct z_candidate *
1497 add_candidate (struct z_candidate **candidates,
1498 tree fn, tree first_arg, const VEC(tree,gc) *args,
1499 size_t num_convs, conversion **convs,
1500 tree access_path, tree conversion_path,
1501 int viable)
1503 struct z_candidate *cand = (struct z_candidate *)
1504 conversion_obstack_alloc (sizeof (struct z_candidate));
1506 cand->fn = fn;
1507 cand->first_arg = first_arg;
1508 cand->args = args;
1509 cand->convs = convs;
1510 cand->num_convs = num_convs;
1511 cand->access_path = access_path;
1512 cand->conversion_path = conversion_path;
1513 cand->viable = viable;
1514 cand->next = *candidates;
1515 *candidates = cand;
1517 return cand;
1520 /* Create an overload candidate for the function or method FN called
1521 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
1522 FLAGS is passed on to implicit_conversion.
1524 This does not change ARGS.
1526 CTYPE, if non-NULL, is the type we want to pretend this function
1527 comes from for purposes of overload resolution. */
1529 static struct z_candidate *
1530 add_function_candidate (struct z_candidate **candidates,
1531 tree fn, tree ctype, tree first_arg,
1532 const VEC(tree,gc) *args, tree access_path,
1533 tree conversion_path, int flags)
1535 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1536 int i, len;
1537 conversion **convs;
1538 tree parmnode;
1539 tree orig_first_arg = first_arg;
1540 int skip;
1541 int viable = 1;
1543 /* At this point we should not see any functions which haven't been
1544 explicitly declared, except for friend functions which will have
1545 been found using argument dependent lookup. */
1546 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
1548 /* The `this', `in_chrg' and VTT arguments to constructors are not
1549 considered in overload resolution. */
1550 if (DECL_CONSTRUCTOR_P (fn))
1552 parmlist = skip_artificial_parms_for (fn, parmlist);
1553 skip = num_artificial_parms_for (fn);
1554 if (skip > 0 && first_arg != NULL_TREE)
1556 --skip;
1557 first_arg = NULL_TREE;
1560 else
1561 skip = 0;
1563 len = VEC_length (tree, args) - skip + (first_arg != NULL_TREE ? 1 : 0);
1564 convs = alloc_conversions (len);
1566 /* 13.3.2 - Viable functions [over.match.viable]
1567 First, to be a viable function, a candidate function shall have enough
1568 parameters to agree in number with the arguments in the list.
1570 We need to check this first; otherwise, checking the ICSes might cause
1571 us to produce an ill-formed template instantiation. */
1573 parmnode = parmlist;
1574 for (i = 0; i < len; ++i)
1576 if (parmnode == NULL_TREE || parmnode == void_list_node)
1577 break;
1578 parmnode = TREE_CHAIN (parmnode);
1581 if (i < len && parmnode)
1582 viable = 0;
1584 /* Make sure there are default args for the rest of the parms. */
1585 else if (!sufficient_parms_p (parmnode))
1586 viable = 0;
1588 if (! viable)
1589 goto out;
1591 /* Second, for F to be a viable function, there shall exist for each
1592 argument an implicit conversion sequence that converts that argument
1593 to the corresponding parameter of F. */
1595 parmnode = parmlist;
1597 for (i = 0; i < len; ++i)
1599 tree arg, argtype;
1600 conversion *t;
1601 int is_this;
1603 if (parmnode == void_list_node)
1604 break;
1606 if (i == 0 && first_arg != NULL_TREE)
1607 arg = first_arg;
1608 else
1609 arg = VEC_index (tree, args,
1610 i + skip - (first_arg != NULL_TREE ? 1 : 0));
1611 argtype = lvalue_type (arg);
1613 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1614 && ! DECL_CONSTRUCTOR_P (fn));
1616 if (parmnode)
1618 tree parmtype = TREE_VALUE (parmnode);
1619 int lflags = flags;
1621 /* The type of the implicit object parameter ('this') for
1622 overload resolution is not always the same as for the
1623 function itself; conversion functions are considered to
1624 be members of the class being converted, and functions
1625 introduced by a using-declaration are considered to be
1626 members of the class that uses them.
1628 Since build_over_call ignores the ICS for the `this'
1629 parameter, we can just change the parm type. */
1630 if (ctype && is_this)
1632 parmtype = cp_build_qualified_type
1633 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
1634 parmtype = build_pointer_type (parmtype);
1637 if (ctype && i == 0 && DECL_COPY_CONSTRUCTOR_P (fn)
1638 && (len-skip == 1))
1640 /* Hack: Direct-initialize copy parm (i.e. suppress
1641 LOOKUP_ONLYCONVERTING) to make explicit conversion ops
1642 work. See also reference_binding. */
1643 lflags |= LOOKUP_COPY_PARM;
1644 if (flags & LOOKUP_NO_COPY_CTOR_CONVERSION)
1645 lflags |= LOOKUP_NO_CONVERSION;
1647 else
1648 lflags |= LOOKUP_ONLYCONVERTING;
1650 t = implicit_conversion (parmtype, argtype, arg,
1651 /*c_cast_p=*/false, lflags);
1653 else
1655 t = build_identity_conv (argtype, arg);
1656 t->ellipsis_p = true;
1659 if (t && is_this)
1660 t->this_p = true;
1662 convs[i] = t;
1663 if (! t)
1665 viable = 0;
1666 break;
1669 if (t->bad_p)
1670 viable = -1;
1672 if (parmnode)
1673 parmnode = TREE_CHAIN (parmnode);
1676 out:
1677 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
1678 access_path, conversion_path, viable);
1681 /* Create an overload candidate for the conversion function FN which will
1682 be invoked for expression OBJ, producing a pointer-to-function which
1683 will in turn be called with the argument list FIRST_ARG/ARGLIST,
1684 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
1685 passed on to implicit_conversion.
1687 Actually, we don't really care about FN; we care about the type it
1688 converts to. There may be multiple conversion functions that will
1689 convert to that type, and we rely on build_user_type_conversion_1 to
1690 choose the best one; so when we create our candidate, we record the type
1691 instead of the function. */
1693 static struct z_candidate *
1694 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
1695 tree first_arg, const VEC(tree,gc) *arglist,
1696 tree access_path, tree conversion_path)
1698 tree totype = TREE_TYPE (TREE_TYPE (fn));
1699 int i, len, viable, flags;
1700 tree parmlist, parmnode;
1701 conversion **convs;
1703 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1704 parmlist = TREE_TYPE (parmlist);
1705 parmlist = TYPE_ARG_TYPES (parmlist);
1707 len = VEC_length (tree, arglist) + (first_arg != NULL_TREE ? 1 : 0) + 1;
1708 convs = alloc_conversions (len);
1709 parmnode = parmlist;
1710 viable = 1;
1711 flags = LOOKUP_IMPLICIT;
1713 /* Don't bother looking up the same type twice. */
1714 if (*candidates && (*candidates)->fn == totype)
1715 return NULL;
1717 for (i = 0; i < len; ++i)
1719 tree arg, argtype;
1720 conversion *t;
1722 if (i == 0)
1723 arg = obj;
1724 else if (i == 1 && first_arg != NULL_TREE)
1725 arg = first_arg;
1726 else
1727 arg = VEC_index (tree, arglist,
1728 i - (first_arg != NULL_TREE ? 1 : 0) - 1);
1729 argtype = lvalue_type (arg);
1731 if (i == 0)
1732 t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false,
1733 flags);
1734 else if (parmnode == void_list_node)
1735 break;
1736 else if (parmnode)
1737 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
1738 /*c_cast_p=*/false, flags);
1739 else
1741 t = build_identity_conv (argtype, arg);
1742 t->ellipsis_p = true;
1745 convs[i] = t;
1746 if (! t)
1747 break;
1749 if (t->bad_p)
1750 viable = -1;
1752 if (i == 0)
1753 continue;
1755 if (parmnode)
1756 parmnode = TREE_CHAIN (parmnode);
1759 if (i < len)
1760 viable = 0;
1762 if (!sufficient_parms_p (parmnode))
1763 viable = 0;
1765 return add_candidate (candidates, totype, first_arg, arglist, len, convs,
1766 access_path, conversion_path, viable);
1769 static void
1770 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
1771 tree type1, tree type2, tree *args, tree *argtypes,
1772 int flags)
1774 conversion *t;
1775 conversion **convs;
1776 size_t num_convs;
1777 int viable = 1, i;
1778 tree types[2];
1780 types[0] = type1;
1781 types[1] = type2;
1783 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
1784 convs = alloc_conversions (num_convs);
1786 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
1787 conversion ops are allowed. We handle that here by just checking for
1788 boolean_type_node because other operators don't ask for it. COND_EXPR
1789 also does contextual conversion to bool for the first operand, but we
1790 handle that in build_conditional_expr, and type1 here is operand 2. */
1791 if (type1 != boolean_type_node)
1792 flags |= LOOKUP_ONLYCONVERTING;
1794 for (i = 0; i < 2; ++i)
1796 if (! args[i])
1797 break;
1799 t = implicit_conversion (types[i], argtypes[i], args[i],
1800 /*c_cast_p=*/false, flags);
1801 if (! t)
1803 viable = 0;
1804 /* We need something for printing the candidate. */
1805 t = build_identity_conv (types[i], NULL_TREE);
1807 else if (t->bad_p)
1808 viable = 0;
1809 convs[i] = t;
1812 /* For COND_EXPR we rearranged the arguments; undo that now. */
1813 if (args[2])
1815 convs[2] = convs[1];
1816 convs[1] = convs[0];
1817 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
1818 /*c_cast_p=*/false, flags);
1819 if (t)
1820 convs[0] = t;
1821 else
1822 viable = 0;
1825 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
1826 num_convs, convs,
1827 /*access_path=*/NULL_TREE,
1828 /*conversion_path=*/NULL_TREE,
1829 viable);
1832 static bool
1833 is_complete (tree t)
1835 return COMPLETE_TYPE_P (complete_type (t));
1838 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1840 static bool
1841 promoted_arithmetic_type_p (tree type)
1843 /* [over.built]
1845 In this section, the term promoted integral type is used to refer
1846 to those integral types which are preserved by integral promotion
1847 (including e.g. int and long but excluding e.g. char).
1848 Similarly, the term promoted arithmetic type refers to promoted
1849 integral types plus floating types. */
1850 return ((CP_INTEGRAL_TYPE_P (type)
1851 && same_type_p (type_promotes_to (type), type))
1852 || TREE_CODE (type) == REAL_TYPE);
1855 /* Create any builtin operator overload candidates for the operator in
1856 question given the converted operand types TYPE1 and TYPE2. The other
1857 args are passed through from add_builtin_candidates to
1858 build_builtin_candidate.
1860 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1861 If CODE is requires candidates operands of the same type of the kind
1862 of which TYPE1 and TYPE2 are, we add both candidates
1863 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1865 static void
1866 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
1867 enum tree_code code2, tree fnname, tree type1,
1868 tree type2, tree *args, tree *argtypes, int flags)
1870 switch (code)
1872 case POSTINCREMENT_EXPR:
1873 case POSTDECREMENT_EXPR:
1874 args[1] = integer_zero_node;
1875 type2 = integer_type_node;
1876 break;
1877 default:
1878 break;
1881 switch (code)
1884 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1885 and VQ is either volatile or empty, there exist candidate operator
1886 functions of the form
1887 VQ T& operator++(VQ T&);
1888 T operator++(VQ T&, int);
1889 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1890 type other than bool, and VQ is either volatile or empty, there exist
1891 candidate operator functions of the form
1892 VQ T& operator--(VQ T&);
1893 T operator--(VQ T&, int);
1894 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1895 complete object type, and VQ is either volatile or empty, there exist
1896 candidate operator functions of the form
1897 T*VQ& operator++(T*VQ&);
1898 T*VQ& operator--(T*VQ&);
1899 T* operator++(T*VQ&, int);
1900 T* operator--(T*VQ&, int); */
1902 case POSTDECREMENT_EXPR:
1903 case PREDECREMENT_EXPR:
1904 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1905 return;
1906 case POSTINCREMENT_EXPR:
1907 case PREINCREMENT_EXPR:
1908 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1910 type1 = build_reference_type (type1);
1911 break;
1913 return;
1915 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1916 exist candidate operator functions of the form
1918 T& operator*(T*);
1920 8 For every function type T, there exist candidate operator functions of
1921 the form
1922 T& operator*(T*); */
1924 case INDIRECT_REF:
1925 if (TREE_CODE (type1) == POINTER_TYPE
1926 && (TYPE_PTROB_P (type1)
1927 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1928 break;
1929 return;
1931 /* 9 For every type T, there exist candidate operator functions of the form
1932 T* operator+(T*);
1934 10For every promoted arithmetic type T, there exist candidate operator
1935 functions of the form
1936 T operator+(T);
1937 T operator-(T); */
1939 case UNARY_PLUS_EXPR: /* unary + */
1940 if (TREE_CODE (type1) == POINTER_TYPE)
1941 break;
1942 case NEGATE_EXPR:
1943 if (ARITHMETIC_TYPE_P (type1))
1944 break;
1945 return;
1947 /* 11For every promoted integral type T, there exist candidate operator
1948 functions of the form
1949 T operator~(T); */
1951 case BIT_NOT_EXPR:
1952 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
1953 break;
1954 return;
1956 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1957 is the same type as C2 or is a derived class of C2, T is a complete
1958 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1959 there exist candidate operator functions of the form
1960 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1961 where CV12 is the union of CV1 and CV2. */
1963 case MEMBER_REF:
1964 if (TREE_CODE (type1) == POINTER_TYPE
1965 && TYPE_PTR_TO_MEMBER_P (type2))
1967 tree c1 = TREE_TYPE (type1);
1968 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
1970 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
1971 && (TYPE_PTRMEMFUNC_P (type2)
1972 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
1973 break;
1975 return;
1977 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1978 didate operator functions of the form
1979 LR operator*(L, R);
1980 LR operator/(L, R);
1981 LR operator+(L, R);
1982 LR operator-(L, R);
1983 bool operator<(L, R);
1984 bool operator>(L, R);
1985 bool operator<=(L, R);
1986 bool operator>=(L, R);
1987 bool operator==(L, R);
1988 bool operator!=(L, R);
1989 where LR is the result of the usual arithmetic conversions between
1990 types L and R.
1992 14For every pair of types T and I, where T is a cv-qualified or cv-
1993 unqualified complete object type and I is a promoted integral type,
1994 there exist candidate operator functions of the form
1995 T* operator+(T*, I);
1996 T& operator[](T*, I);
1997 T* operator-(T*, I);
1998 T* operator+(I, T*);
1999 T& operator[](I, T*);
2001 15For every T, where T is a pointer to complete object type, there exist
2002 candidate operator functions of the form112)
2003 ptrdiff_t operator-(T, T);
2005 16For every pointer or enumeration type T, there exist candidate operator
2006 functions of the form
2007 bool operator<(T, T);
2008 bool operator>(T, T);
2009 bool operator<=(T, T);
2010 bool operator>=(T, T);
2011 bool operator==(T, T);
2012 bool operator!=(T, T);
2014 17For every pointer to member type T, there exist candidate operator
2015 functions of the form
2016 bool operator==(T, T);
2017 bool operator!=(T, T); */
2019 case MINUS_EXPR:
2020 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2021 break;
2022 if (TYPE_PTROB_P (type1)
2023 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2025 type2 = ptrdiff_type_node;
2026 break;
2028 case MULT_EXPR:
2029 case TRUNC_DIV_EXPR:
2030 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2031 break;
2032 return;
2034 case EQ_EXPR:
2035 case NE_EXPR:
2036 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2037 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
2038 break;
2039 if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
2041 type2 = type1;
2042 break;
2044 if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
2046 type1 = type2;
2047 break;
2049 /* Fall through. */
2050 case LT_EXPR:
2051 case GT_EXPR:
2052 case LE_EXPR:
2053 case GE_EXPR:
2054 case MAX_EXPR:
2055 case MIN_EXPR:
2056 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2057 break;
2058 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2059 break;
2060 if (TREE_CODE (type1) == ENUMERAL_TYPE
2061 && TREE_CODE (type2) == ENUMERAL_TYPE)
2062 break;
2063 if (TYPE_PTR_P (type1)
2064 && null_ptr_cst_p (args[1])
2065 && !uses_template_parms (type1))
2067 type2 = type1;
2068 break;
2070 if (null_ptr_cst_p (args[0])
2071 && TYPE_PTR_P (type2)
2072 && !uses_template_parms (type2))
2074 type1 = type2;
2075 break;
2077 return;
2079 case PLUS_EXPR:
2080 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2081 break;
2082 case ARRAY_REF:
2083 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2085 type1 = ptrdiff_type_node;
2086 break;
2088 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2090 type2 = ptrdiff_type_node;
2091 break;
2093 return;
2095 /* 18For every pair of promoted integral types L and R, there exist candi-
2096 date operator functions of the form
2097 LR operator%(L, R);
2098 LR operator&(L, R);
2099 LR operator^(L, R);
2100 LR operator|(L, R);
2101 L operator<<(L, R);
2102 L operator>>(L, R);
2103 where LR is the result of the usual arithmetic conversions between
2104 types L and R. */
2106 case TRUNC_MOD_EXPR:
2107 case BIT_AND_EXPR:
2108 case BIT_IOR_EXPR:
2109 case BIT_XOR_EXPR:
2110 case LSHIFT_EXPR:
2111 case RSHIFT_EXPR:
2112 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2113 break;
2114 return;
2116 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2117 type, VQ is either volatile or empty, and R is a promoted arithmetic
2118 type, there exist candidate operator functions of the form
2119 VQ L& operator=(VQ L&, R);
2120 VQ L& operator*=(VQ L&, R);
2121 VQ L& operator/=(VQ L&, R);
2122 VQ L& operator+=(VQ L&, R);
2123 VQ L& operator-=(VQ L&, R);
2125 20For every pair T, VQ), where T is any type and VQ is either volatile
2126 or empty, there exist candidate operator functions of the form
2127 T*VQ& operator=(T*VQ&, T*);
2129 21For every pair T, VQ), where T is a pointer to member type and VQ is
2130 either volatile or empty, there exist candidate operator functions of
2131 the form
2132 VQ T& operator=(VQ T&, T);
2134 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2135 unqualified complete object type, VQ is either volatile or empty, and
2136 I is a promoted integral type, there exist candidate operator func-
2137 tions of the form
2138 T*VQ& operator+=(T*VQ&, I);
2139 T*VQ& operator-=(T*VQ&, I);
2141 23For every triple L, VQ, R), where L is an integral or enumeration
2142 type, VQ is either volatile or empty, and R is a promoted integral
2143 type, there exist candidate operator functions of the form
2145 VQ L& operator%=(VQ L&, R);
2146 VQ L& operator<<=(VQ L&, R);
2147 VQ L& operator>>=(VQ L&, R);
2148 VQ L& operator&=(VQ L&, R);
2149 VQ L& operator^=(VQ L&, R);
2150 VQ L& operator|=(VQ L&, R); */
2152 case MODIFY_EXPR:
2153 switch (code2)
2155 case PLUS_EXPR:
2156 case MINUS_EXPR:
2157 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2159 type2 = ptrdiff_type_node;
2160 break;
2162 case MULT_EXPR:
2163 case TRUNC_DIV_EXPR:
2164 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2165 break;
2166 return;
2168 case TRUNC_MOD_EXPR:
2169 case BIT_AND_EXPR:
2170 case BIT_IOR_EXPR:
2171 case BIT_XOR_EXPR:
2172 case LSHIFT_EXPR:
2173 case RSHIFT_EXPR:
2174 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2175 break;
2176 return;
2178 case NOP_EXPR:
2179 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2180 break;
2181 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2182 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2183 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
2184 || ((TYPE_PTRMEMFUNC_P (type1)
2185 || TREE_CODE (type1) == POINTER_TYPE)
2186 && null_ptr_cst_p (args[1])))
2188 type2 = type1;
2189 break;
2191 return;
2193 default:
2194 gcc_unreachable ();
2196 type1 = build_reference_type (type1);
2197 break;
2199 case COND_EXPR:
2200 /* [over.built]
2202 For every pair of promoted arithmetic types L and R, there
2203 exist candidate operator functions of the form
2205 LR operator?(bool, L, R);
2207 where LR is the result of the usual arithmetic conversions
2208 between types L and R.
2210 For every type T, where T is a pointer or pointer-to-member
2211 type, there exist candidate operator functions of the form T
2212 operator?(bool, T, T); */
2214 if (promoted_arithmetic_type_p (type1)
2215 && promoted_arithmetic_type_p (type2))
2216 /* That's OK. */
2217 break;
2219 /* Otherwise, the types should be pointers. */
2220 if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
2221 || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
2222 return;
2224 /* We don't check that the two types are the same; the logic
2225 below will actually create two candidates; one in which both
2226 parameter types are TYPE1, and one in which both parameter
2227 types are TYPE2. */
2228 break;
2230 default:
2231 gcc_unreachable ();
2234 /* If we're dealing with two pointer types or two enumeral types,
2235 we need candidates for both of them. */
2236 if (type2 && !same_type_p (type1, type2)
2237 && TREE_CODE (type1) == TREE_CODE (type2)
2238 && (TREE_CODE (type1) == REFERENCE_TYPE
2239 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2240 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
2241 || TYPE_PTRMEMFUNC_P (type1)
2242 || MAYBE_CLASS_TYPE_P (type1)
2243 || TREE_CODE (type1) == ENUMERAL_TYPE))
2245 build_builtin_candidate
2246 (candidates, fnname, type1, type1, args, argtypes, flags);
2247 build_builtin_candidate
2248 (candidates, fnname, type2, type2, args, argtypes, flags);
2249 return;
2252 build_builtin_candidate
2253 (candidates, fnname, type1, type2, args, argtypes, flags);
2256 tree
2257 type_decays_to (tree type)
2259 if (TREE_CODE (type) == ARRAY_TYPE)
2260 return build_pointer_type (TREE_TYPE (type));
2261 if (TREE_CODE (type) == FUNCTION_TYPE)
2262 return build_pointer_type (type);
2263 if (!MAYBE_CLASS_TYPE_P (type))
2264 type = cv_unqualified (type);
2265 return type;
2268 /* There are three conditions of builtin candidates:
2270 1) bool-taking candidates. These are the same regardless of the input.
2271 2) pointer-pair taking candidates. These are generated for each type
2272 one of the input types converts to.
2273 3) arithmetic candidates. According to the standard, we should generate
2274 all of these, but I'm trying not to...
2276 Here we generate a superset of the possible candidates for this particular
2277 case. That is a subset of the full set the standard defines, plus some
2278 other cases which the standard disallows. add_builtin_candidate will
2279 filter out the invalid set. */
2281 static void
2282 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2283 enum tree_code code2, tree fnname, tree *args,
2284 int flags)
2286 int ref1, i;
2287 int enum_p = 0;
2288 tree type, argtypes[3];
2289 /* TYPES[i] is the set of possible builtin-operator parameter types
2290 we will consider for the Ith argument. These are represented as
2291 a TREE_LIST; the TREE_VALUE of each node is the potential
2292 parameter type. */
2293 tree types[2];
2295 for (i = 0; i < 3; ++i)
2297 if (args[i])
2298 argtypes[i] = unlowered_expr_type (args[i]);
2299 else
2300 argtypes[i] = NULL_TREE;
2303 switch (code)
2305 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2306 and VQ is either volatile or empty, there exist candidate operator
2307 functions of the form
2308 VQ T& operator++(VQ T&); */
2310 case POSTINCREMENT_EXPR:
2311 case PREINCREMENT_EXPR:
2312 case POSTDECREMENT_EXPR:
2313 case PREDECREMENT_EXPR:
2314 case MODIFY_EXPR:
2315 ref1 = 1;
2316 break;
2318 /* 24There also exist candidate operator functions of the form
2319 bool operator!(bool);
2320 bool operator&&(bool, bool);
2321 bool operator||(bool, bool); */
2323 case TRUTH_NOT_EXPR:
2324 build_builtin_candidate
2325 (candidates, fnname, boolean_type_node,
2326 NULL_TREE, args, argtypes, flags);
2327 return;
2329 case TRUTH_ORIF_EXPR:
2330 case TRUTH_ANDIF_EXPR:
2331 build_builtin_candidate
2332 (candidates, fnname, boolean_type_node,
2333 boolean_type_node, args, argtypes, flags);
2334 return;
2336 case ADDR_EXPR:
2337 case COMPOUND_EXPR:
2338 case COMPONENT_REF:
2339 return;
2341 case COND_EXPR:
2342 case EQ_EXPR:
2343 case NE_EXPR:
2344 case LT_EXPR:
2345 case LE_EXPR:
2346 case GT_EXPR:
2347 case GE_EXPR:
2348 enum_p = 1;
2349 /* Fall through. */
2351 default:
2352 ref1 = 0;
2355 types[0] = types[1] = NULL_TREE;
2357 for (i = 0; i < 2; ++i)
2359 if (! args[i])
2361 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
2363 tree convs;
2365 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2366 return;
2368 convs = lookup_conversions (argtypes[i],
2369 /*lookup_template_convs_p=*/false);
2371 if (code == COND_EXPR)
2373 if (real_lvalue_p (args[i]))
2374 types[i] = tree_cons
2375 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2377 types[i] = tree_cons
2378 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2381 else if (! convs)
2382 return;
2384 for (; convs; convs = TREE_CHAIN (convs))
2386 type = TREE_TYPE (convs);
2388 if (i == 0 && ref1
2389 && (TREE_CODE (type) != REFERENCE_TYPE
2390 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2391 continue;
2393 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2394 types[i] = tree_cons (NULL_TREE, type, types[i]);
2396 type = non_reference (type);
2397 if (i != 0 || ! ref1)
2399 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2400 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2401 types[i] = tree_cons (NULL_TREE, type, types[i]);
2402 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2403 type = type_promotes_to (type);
2406 if (! value_member (type, types[i]))
2407 types[i] = tree_cons (NULL_TREE, type, types[i]);
2410 else
2412 if (code == COND_EXPR && real_lvalue_p (args[i]))
2413 types[i] = tree_cons
2414 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2415 type = non_reference (argtypes[i]);
2416 if (i != 0 || ! ref1)
2418 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2419 if (enum_p && UNSCOPED_ENUM_P (type))
2420 types[i] = tree_cons (NULL_TREE, type, types[i]);
2421 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2422 type = type_promotes_to (type);
2424 types[i] = tree_cons (NULL_TREE, type, types[i]);
2428 /* Run through the possible parameter types of both arguments,
2429 creating candidates with those parameter types. */
2430 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2432 if (types[1])
2433 for (type = types[1]; type; type = TREE_CHAIN (type))
2434 add_builtin_candidate
2435 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2436 TREE_VALUE (type), args, argtypes, flags);
2437 else
2438 add_builtin_candidate
2439 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2440 NULL_TREE, args, argtypes, flags);
2445 /* If TMPL can be successfully instantiated as indicated by
2446 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2448 TMPL is the template. EXPLICIT_TARGS are any explicit template
2449 arguments. ARGLIST is the arguments provided at the call-site.
2450 This does not change ARGLIST. The RETURN_TYPE is the desired type
2451 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
2452 as for add_function_candidate. If an OBJ is supplied, FLAGS and
2453 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
2455 static struct z_candidate*
2456 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2457 tree ctype, tree explicit_targs, tree first_arg,
2458 const VEC(tree,gc) *arglist, tree return_type,
2459 tree access_path, tree conversion_path,
2460 int flags, tree obj, unification_kind_t strict)
2462 int ntparms = DECL_NTPARMS (tmpl);
2463 tree targs = make_tree_vec (ntparms);
2464 unsigned int len = VEC_length (tree, arglist);
2465 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
2466 unsigned int skip_without_in_chrg = 0;
2467 tree first_arg_without_in_chrg = first_arg;
2468 tree *args_without_in_chrg;
2469 unsigned int nargs_without_in_chrg;
2470 unsigned int ia, ix;
2471 tree arg;
2472 struct z_candidate *cand;
2473 int i;
2474 tree fn;
2476 /* We don't do deduction on the in-charge parameter, the VTT
2477 parameter or 'this'. */
2478 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2480 if (first_arg_without_in_chrg != NULL_TREE)
2481 first_arg_without_in_chrg = NULL_TREE;
2482 else
2483 ++skip_without_in_chrg;
2486 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2487 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2488 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
2490 if (first_arg_without_in_chrg != NULL_TREE)
2491 first_arg_without_in_chrg = NULL_TREE;
2492 else
2493 ++skip_without_in_chrg;
2496 if (len < skip_without_in_chrg)
2497 return NULL;
2499 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
2500 + (len - skip_without_in_chrg));
2501 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
2502 ia = 0;
2503 if (first_arg_without_in_chrg != NULL_TREE)
2505 args_without_in_chrg[ia] = first_arg_without_in_chrg;
2506 ++ia;
2508 for (ix = skip_without_in_chrg;
2509 VEC_iterate (tree, arglist, ix, arg);
2510 ++ix)
2512 args_without_in_chrg[ia] = arg;
2513 ++ia;
2515 gcc_assert (ia == nargs_without_in_chrg);
2517 i = fn_type_unification (tmpl, explicit_targs, targs,
2518 args_without_in_chrg,
2519 nargs_without_in_chrg,
2520 return_type, strict, flags);
2522 if (i != 0)
2523 goto fail;
2525 fn = instantiate_template (tmpl, targs, tf_none);
2526 if (fn == error_mark_node)
2527 goto fail;
2529 /* In [class.copy]:
2531 A member function template is never instantiated to perform the
2532 copy of a class object to an object of its class type.
2534 It's a little unclear what this means; the standard explicitly
2535 does allow a template to be used to copy a class. For example,
2538 struct A {
2539 A(A&);
2540 template <class T> A(const T&);
2542 const A f ();
2543 void g () { A a (f ()); }
2545 the member template will be used to make the copy. The section
2546 quoted above appears in the paragraph that forbids constructors
2547 whose only parameter is (a possibly cv-qualified variant of) the
2548 class type, and a logical interpretation is that the intent was
2549 to forbid the instantiation of member templates which would then
2550 have that form. */
2551 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
2553 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2554 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2555 ctype))
2556 goto fail;
2559 if (obj != NULL_TREE)
2560 /* Aha, this is a conversion function. */
2561 cand = add_conv_candidate (candidates, fn, obj, first_arg, arglist,
2562 access_path, conversion_path);
2563 else
2564 cand = add_function_candidate (candidates, fn, ctype,
2565 first_arg, arglist, access_path,
2566 conversion_path, flags);
2567 if (DECL_TI_TEMPLATE (fn) != tmpl)
2568 /* This situation can occur if a member template of a template
2569 class is specialized. Then, instantiate_template might return
2570 an instantiation of the specialization, in which case the
2571 DECL_TI_TEMPLATE field will point at the original
2572 specialization. For example:
2574 template <class T> struct S { template <class U> void f(U);
2575 template <> void f(int) {}; };
2576 S<double> sd;
2577 sd.f(3);
2579 Here, TMPL will be template <class U> S<double>::f(U).
2580 And, instantiate template will give us the specialization
2581 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2582 for this will point at template <class T> template <> S<T>::f(int),
2583 so that we can find the definition. For the purposes of
2584 overload resolution, however, we want the original TMPL. */
2585 cand->template_decl = build_template_info (tmpl, targs);
2586 else
2587 cand->template_decl = DECL_TEMPLATE_INFO (fn);
2588 cand->explicit_targs = explicit_targs;
2590 return cand;
2591 fail:
2592 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
2593 access_path, conversion_path, 0);
2597 static struct z_candidate *
2598 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2599 tree explicit_targs, tree first_arg,
2600 const VEC(tree,gc) *arglist, tree return_type,
2601 tree access_path, tree conversion_path, int flags,
2602 unification_kind_t strict)
2604 return
2605 add_template_candidate_real (candidates, tmpl, ctype,
2606 explicit_targs, first_arg, arglist,
2607 return_type, access_path, conversion_path,
2608 flags, NULL_TREE, strict);
2612 static struct z_candidate *
2613 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2614 tree obj, tree first_arg,
2615 const VEC(tree,gc) *arglist,
2616 tree return_type, tree access_path,
2617 tree conversion_path)
2619 return
2620 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2621 first_arg, arglist, return_type, access_path,
2622 conversion_path, 0, obj, DEDUCE_CONV);
2625 /* The CANDS are the set of candidates that were considered for
2626 overload resolution. Return the set of viable candidates, or CANDS
2627 if none are viable. If any of the candidates were viable, set
2628 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
2629 considered viable only if it is strictly viable. */
2631 static struct z_candidate*
2632 splice_viable (struct z_candidate *cands,
2633 bool strict_p,
2634 bool *any_viable_p)
2636 struct z_candidate *viable;
2637 struct z_candidate **last_viable;
2638 struct z_candidate **cand;
2640 viable = NULL;
2641 last_viable = &viable;
2642 *any_viable_p = false;
2644 cand = &cands;
2645 while (*cand)
2647 struct z_candidate *c = *cand;
2648 if (strict_p ? c->viable == 1 : c->viable)
2650 *last_viable = c;
2651 *cand = c->next;
2652 c->next = NULL;
2653 last_viable = &c->next;
2654 *any_viable_p = true;
2656 else
2657 cand = &c->next;
2660 return viable ? viable : cands;
2663 static bool
2664 any_strictly_viable (struct z_candidate *cands)
2666 for (; cands; cands = cands->next)
2667 if (cands->viable == 1)
2668 return true;
2669 return false;
2672 /* OBJ is being used in an expression like "OBJ.f (...)". In other
2673 words, it is about to become the "this" pointer for a member
2674 function call. Take the address of the object. */
2676 static tree
2677 build_this (tree obj)
2679 /* In a template, we are only concerned about the type of the
2680 expression, so we can take a shortcut. */
2681 if (processing_template_decl)
2682 return build_address (obj);
2684 return cp_build_unary_op (ADDR_EXPR, obj, 0, tf_warning_or_error);
2687 /* Returns true iff functions are equivalent. Equivalent functions are
2688 not '==' only if one is a function-local extern function or if
2689 both are extern "C". */
2691 static inline int
2692 equal_functions (tree fn1, tree fn2)
2694 if (TREE_CODE (fn1) != TREE_CODE (fn2))
2695 return 0;
2696 if (TREE_CODE (fn1) == TEMPLATE_DECL)
2697 return fn1 == fn2;
2698 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2699 || DECL_EXTERN_C_FUNCTION_P (fn1))
2700 return decls_match (fn1, fn2);
2701 return fn1 == fn2;
2704 /* Print information about one overload candidate CANDIDATE. MSGSTR
2705 is the text to print before the candidate itself.
2707 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2708 to have been run through gettext by the caller. This wart makes
2709 life simpler in print_z_candidates and for the translators. */
2711 static void
2712 print_z_candidate (const char *msgstr, struct z_candidate *candidate)
2714 if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2716 if (candidate->num_convs == 3)
2717 inform (input_location, "%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
2718 candidate->convs[0]->type,
2719 candidate->convs[1]->type,
2720 candidate->convs[2]->type);
2721 else if (candidate->num_convs == 2)
2722 inform (input_location, "%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
2723 candidate->convs[0]->type,
2724 candidate->convs[1]->type);
2725 else
2726 inform (input_location, "%s %D(%T) <built-in>", msgstr, candidate->fn,
2727 candidate->convs[0]->type);
2729 else if (TYPE_P (candidate->fn))
2730 inform (input_location, "%s %T <conversion>", msgstr, candidate->fn);
2731 else if (candidate->viable == -1)
2732 inform (input_location, "%s %+#D <near match>", msgstr, candidate->fn);
2733 else if (DECL_DELETED_FN (STRIP_TEMPLATE (candidate->fn)))
2734 inform (input_location, "%s %+#D <deleted>", msgstr, candidate->fn);
2735 else
2736 inform (input_location, "%s %+#D", msgstr, candidate->fn);
2739 static void
2740 print_z_candidates (struct z_candidate *candidates)
2742 const char *str;
2743 struct z_candidate *cand1;
2744 struct z_candidate **cand2;
2745 char *spaces;
2747 if (!candidates)
2748 return;
2750 /* Remove deleted candidates. */
2751 cand1 = candidates;
2752 for (cand2 = &cand1; *cand2; )
2754 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2755 && DECL_DELETED_FN ((*cand2)->fn))
2756 *cand2 = (*cand2)->next;
2757 else
2758 cand2 = &(*cand2)->next;
2760 /* ...if there are any non-deleted ones. */
2761 if (cand1)
2762 candidates = cand1;
2764 /* There may be duplicates in the set of candidates. We put off
2765 checking this condition as long as possible, since we have no way
2766 to eliminate duplicates from a set of functions in less than n^2
2767 time. Now we are about to emit an error message, so it is more
2768 permissible to go slowly. */
2769 for (cand1 = candidates; cand1; cand1 = cand1->next)
2771 tree fn = cand1->fn;
2772 /* Skip builtin candidates and conversion functions. */
2773 if (!DECL_P (fn))
2774 continue;
2775 cand2 = &cand1->next;
2776 while (*cand2)
2778 if (DECL_P ((*cand2)->fn)
2779 && equal_functions (fn, (*cand2)->fn))
2780 *cand2 = (*cand2)->next;
2781 else
2782 cand2 = &(*cand2)->next;
2786 str = candidates->next ? _("candidates are:") : _("candidate is:");
2787 spaces = NULL;
2788 for (; candidates; candidates = candidates->next)
2790 print_z_candidate (spaces ? spaces : str, candidates);
2791 spaces = spaces ? spaces : get_spaces (str);
2793 free (spaces);
2796 /* USER_SEQ is a user-defined conversion sequence, beginning with a
2797 USER_CONV. STD_SEQ is the standard conversion sequence applied to
2798 the result of the conversion function to convert it to the final
2799 desired type. Merge the two sequences into a single sequence,
2800 and return the merged sequence. */
2802 static conversion *
2803 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
2805 conversion **t;
2807 gcc_assert (user_seq->kind == ck_user);
2809 /* Find the end of the second conversion sequence. */
2810 t = &(std_seq);
2811 while ((*t)->kind != ck_identity)
2812 t = &((*t)->u.next);
2814 /* Replace the identity conversion with the user conversion
2815 sequence. */
2816 *t = user_seq;
2818 /* The entire sequence is a user-conversion sequence. */
2819 std_seq->user_conv_p = true;
2821 return std_seq;
2824 /* Handle overload resolution for initializing an object of class type from
2825 an initializer list. First we look for a suitable constructor that
2826 takes a std::initializer_list; if we don't find one, we then look for a
2827 non-list constructor.
2829 Parameters are as for add_candidates, except that the arguments are in
2830 the form of a CONSTRUCTOR (the initializer list) rather than a VEC, and
2831 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
2833 static void
2834 add_list_candidates (tree fns, tree first_arg,
2835 tree init_list, tree totype,
2836 tree explicit_targs, bool template_only,
2837 tree conversion_path, tree access_path,
2838 int flags,
2839 struct z_candidate **candidates)
2841 VEC(tree,gc) *args;
2843 gcc_assert (*candidates == NULL);
2845 /* For list-initialization we consider explicit constructors, but
2846 give an error if one is selected. */
2847 flags &= ~LOOKUP_ONLYCONVERTING;
2848 /* And we don't allow narrowing conversions. We also use this flag to
2849 avoid the copy constructor call for copy-list-initialization. */
2850 flags |= LOOKUP_NO_NARROWING;
2852 /* Always use the default constructor if the list is empty (DR 990). */
2853 if (CONSTRUCTOR_NELTS (init_list) == 0
2854 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
2856 /* If the class has a list ctor, try passing the list as a single
2857 argument first, but only consider list ctors. */
2858 else if (TYPE_HAS_LIST_CTOR (totype))
2860 flags |= LOOKUP_LIST_ONLY;
2861 args = make_tree_vector_single (init_list);
2862 add_candidates (fns, first_arg, args, NULL_TREE,
2863 explicit_targs, template_only, conversion_path,
2864 access_path, flags, candidates);
2865 if (any_strictly_viable (*candidates))
2866 return;
2869 args = ctor_to_vec (init_list);
2871 /* We aren't looking for list-ctors anymore. */
2872 flags &= ~LOOKUP_LIST_ONLY;
2873 /* We allow more user-defined conversions within an init-list. */
2874 flags &= ~LOOKUP_NO_CONVERSION;
2875 /* But not for the copy ctor. */
2876 flags |= LOOKUP_NO_COPY_CTOR_CONVERSION;
2878 add_candidates (fns, first_arg, args, NULL_TREE,
2879 explicit_targs, template_only, conversion_path,
2880 access_path, flags, candidates);
2883 /* Returns the best overload candidate to perform the requested
2884 conversion. This function is used for three the overloading situations
2885 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2886 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2887 per [dcl.init.ref], so we ignore temporary bindings. */
2889 static struct z_candidate *
2890 build_user_type_conversion_1 (tree totype, tree expr, int flags)
2892 struct z_candidate *candidates, *cand;
2893 tree fromtype = TREE_TYPE (expr);
2894 tree ctors = NULL_TREE;
2895 tree conv_fns = NULL_TREE;
2896 conversion *conv = NULL;
2897 tree first_arg = NULL_TREE;
2898 VEC(tree,gc) *args = NULL;
2899 bool any_viable_p;
2900 int convflags;
2902 /* We represent conversion within a hierarchy using RVALUE_CONV and
2903 BASE_CONV, as specified by [over.best.ics]; these become plain
2904 constructor calls, as specified in [dcl.init]. */
2905 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
2906 || !DERIVED_FROM_P (totype, fromtype));
2908 if (MAYBE_CLASS_TYPE_P (totype))
2909 ctors = lookup_fnfields (totype, complete_ctor_identifier, 0);
2911 if (MAYBE_CLASS_TYPE_P (fromtype))
2913 tree to_nonref = non_reference (totype);
2914 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
2915 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
2916 && DERIVED_FROM_P (to_nonref, fromtype)))
2918 /* [class.conv.fct] A conversion function is never used to
2919 convert a (possibly cv-qualified) object to the (possibly
2920 cv-qualified) same object type (or a reference to it), to a
2921 (possibly cv-qualified) base class of that type (or a
2922 reference to it)... */
2924 else
2925 conv_fns = lookup_conversions (fromtype,
2926 /*lookup_template_convs_p=*/true);
2929 candidates = 0;
2930 flags |= LOOKUP_NO_CONVERSION;
2931 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
2932 flags |= LOOKUP_NO_NARROWING;
2934 /* It's OK to bind a temporary for converting constructor arguments, but
2935 not in converting the return value of a conversion operator. */
2936 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION);
2937 flags &= ~LOOKUP_NO_TEMP_BIND;
2939 if (ctors)
2941 int ctorflags = flags;
2942 ctors = BASELINK_FUNCTIONS (ctors);
2944 first_arg = build_int_cst (build_pointer_type (totype), 0);
2946 /* We should never try to call the abstract or base constructor
2947 from here. */
2948 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2949 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)));
2951 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
2953 /* List-initialization. */
2954 add_list_candidates (ctors, first_arg, expr, totype, NULL_TREE,
2955 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
2956 ctorflags, &candidates);
2958 else
2960 args = make_tree_vector_single (expr);
2961 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
2962 TYPE_BINFO (totype), TYPE_BINFO (totype),
2963 ctorflags, &candidates);
2966 for (cand = candidates; cand; cand = cand->next)
2968 cand->second_conv = build_identity_conv (totype, NULL_TREE);
2970 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
2971 set, then this is copy-initialization. In that case, "The
2972 result of the call is then used to direct-initialize the
2973 object that is the destination of the copy-initialization."
2974 [dcl.init]
2976 We represent this in the conversion sequence with an
2977 rvalue conversion, which means a constructor call. */
2978 if (TREE_CODE (totype) != REFERENCE_TYPE
2979 && !(convflags & LOOKUP_NO_TEMP_BIND))
2980 cand->second_conv
2981 = build_conv (ck_rvalue, totype, cand->second_conv);
2985 if (conv_fns)
2986 first_arg = build_this (expr);
2988 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
2990 tree conversion_path = TREE_PURPOSE (conv_fns);
2991 struct z_candidate *old_candidates;
2993 /* If we are called to convert to a reference type, we are trying to
2994 find an lvalue binding, so don't even consider temporaries. If
2995 we don't find an lvalue binding, the caller will try again to
2996 look for a temporary binding. */
2997 if (TREE_CODE (totype) == REFERENCE_TYPE)
2998 convflags |= LOOKUP_NO_TEMP_BIND;
3000 old_candidates = candidates;
3001 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3002 NULL_TREE, false,
3003 conversion_path, TYPE_BINFO (fromtype),
3004 flags, &candidates);
3006 for (cand = candidates; cand != old_candidates; cand = cand->next)
3008 conversion *ics
3009 = implicit_conversion (totype,
3010 TREE_TYPE (TREE_TYPE (cand->fn)),
3012 /*c_cast_p=*/false, convflags);
3014 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3015 copy-initialization. In that case, "The result of the
3016 call is then used to direct-initialize the object that is
3017 the destination of the copy-initialization." [dcl.init]
3019 We represent this in the conversion sequence with an
3020 rvalue conversion, which means a constructor call. But
3021 don't add a second rvalue conversion if there's already
3022 one there. Which there really shouldn't be, but it's
3023 harmless since we'd add it here anyway. */
3024 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3025 && !(convflags & LOOKUP_NO_TEMP_BIND))
3026 ics = build_conv (ck_rvalue, totype, ics);
3028 cand->second_conv = ics;
3030 if (!ics)
3031 cand->viable = 0;
3032 else if (cand->viable == 1 && ics->bad_p)
3033 cand->viable = -1;
3037 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3038 if (!any_viable_p)
3039 return NULL;
3041 cand = tourney (candidates);
3042 if (cand == 0)
3044 if (flags & LOOKUP_COMPLAIN)
3046 error ("conversion from %qT to %qT is ambiguous",
3047 fromtype, totype);
3048 print_z_candidates (candidates);
3051 cand = candidates; /* any one will do */
3052 cand->second_conv = build_ambiguous_conv (totype, expr);
3053 cand->second_conv->user_conv_p = true;
3054 if (!any_strictly_viable (candidates))
3055 cand->second_conv->bad_p = true;
3056 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3057 ambiguous conversion is no worse than another user-defined
3058 conversion. */
3060 return cand;
3063 /* Build the user conversion sequence. */
3064 conv = build_conv
3065 (ck_user,
3066 (DECL_CONSTRUCTOR_P (cand->fn)
3067 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
3068 build_identity_conv (TREE_TYPE (expr), expr));
3069 conv->cand = cand;
3071 /* Remember that this was a list-initialization. */
3072 if (flags & LOOKUP_NO_NARROWING)
3073 conv->check_narrowing = true;
3075 /* Combine it with the second conversion sequence. */
3076 cand->second_conv = merge_conversion_sequences (conv,
3077 cand->second_conv);
3079 if (cand->viable == -1)
3080 cand->second_conv->bad_p = true;
3082 return cand;
3085 tree
3086 build_user_type_conversion (tree totype, tree expr, int flags)
3088 struct z_candidate *cand
3089 = build_user_type_conversion_1 (totype, expr, flags);
3091 if (cand)
3093 if (cand->second_conv->kind == ck_ambig)
3094 return error_mark_node;
3095 expr = convert_like (cand->second_conv, expr, tf_warning_or_error);
3096 return convert_from_reference (expr);
3098 return NULL_TREE;
3101 /* Do any initial processing on the arguments to a function call. */
3103 static VEC(tree,gc) *
3104 resolve_args (VEC(tree,gc) *args)
3106 unsigned int ix;
3107 tree arg;
3109 for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
3111 if (error_operand_p (arg))
3112 return NULL;
3113 else if (VOID_TYPE_P (TREE_TYPE (arg)))
3115 error ("invalid use of void expression");
3116 return NULL;
3118 else if (invalid_nonstatic_memfn_p (arg, tf_warning_or_error))
3119 return NULL;
3121 return args;
3124 /* Perform overload resolution on FN, which is called with the ARGS.
3126 Return the candidate function selected by overload resolution, or
3127 NULL if the event that overload resolution failed. In the case
3128 that overload resolution fails, *CANDIDATES will be the set of
3129 candidates considered, and ANY_VIABLE_P will be set to true or
3130 false to indicate whether or not any of the candidates were
3131 viable.
3133 The ARGS should already have gone through RESOLVE_ARGS before this
3134 function is called. */
3136 static struct z_candidate *
3137 perform_overload_resolution (tree fn,
3138 const VEC(tree,gc) *args,
3139 struct z_candidate **candidates,
3140 bool *any_viable_p)
3142 struct z_candidate *cand;
3143 tree explicit_targs = NULL_TREE;
3144 int template_only = 0;
3146 *candidates = NULL;
3147 *any_viable_p = true;
3149 /* Check FN. */
3150 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
3151 || TREE_CODE (fn) == TEMPLATE_DECL
3152 || TREE_CODE (fn) == OVERLOAD
3153 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
3155 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3157 explicit_targs = TREE_OPERAND (fn, 1);
3158 fn = TREE_OPERAND (fn, 0);
3159 template_only = 1;
3162 /* Add the various candidate functions. */
3163 add_candidates (fn, NULL_TREE, args, NULL_TREE,
3164 explicit_targs, template_only,
3165 /*conversion_path=*/NULL_TREE,
3166 /*access_path=*/NULL_TREE,
3167 LOOKUP_NORMAL,
3168 candidates);
3170 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
3171 if (!*any_viable_p)
3172 return NULL;
3174 cand = tourney (*candidates);
3175 return cand;
3178 /* Return an expression for a call to FN (a namespace-scope function,
3179 or a static member function) with the ARGS. This may change
3180 ARGS. */
3182 tree
3183 build_new_function_call (tree fn, VEC(tree,gc) **args, bool koenig_p,
3184 tsubst_flags_t complain)
3186 struct z_candidate *candidates, *cand;
3187 bool any_viable_p;
3188 void *p;
3189 tree result;
3191 if (args != NULL && *args != NULL)
3193 *args = resolve_args (*args);
3194 if (*args == NULL)
3195 return error_mark_node;
3198 /* If this function was found without using argument dependent
3199 lookup, then we want to ignore any undeclared friend
3200 functions. */
3201 if (!koenig_p)
3203 tree orig_fn = fn;
3205 fn = remove_hidden_names (fn);
3206 if (!fn)
3208 if (complain & tf_error)
3209 error ("no matching function for call to %<%D(%A)%>",
3210 DECL_NAME (OVL_CURRENT (orig_fn)),
3211 build_tree_list_vec (*args));
3212 return error_mark_node;
3216 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3217 p = conversion_obstack_alloc (0);
3219 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p);
3221 if (!cand)
3223 if (complain & tf_error)
3225 if (!any_viable_p && candidates && ! candidates->next
3226 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
3227 return cp_build_function_call_vec (candidates->fn, args, complain);
3228 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3229 fn = TREE_OPERAND (fn, 0);
3230 if (!any_viable_p)
3231 error ("no matching function for call to %<%D(%A)%>",
3232 DECL_NAME (OVL_CURRENT (fn)), build_tree_list_vec (*args));
3233 else
3234 error ("call of overloaded %<%D(%A)%> is ambiguous",
3235 DECL_NAME (OVL_CURRENT (fn)), build_tree_list_vec (*args));
3236 if (candidates)
3237 print_z_candidates (candidates);
3239 result = error_mark_node;
3241 else
3242 result = build_over_call (cand, LOOKUP_NORMAL, complain);
3244 /* Free all the conversions we allocated. */
3245 obstack_free (&conversion_obstack, p);
3247 return result;
3250 /* Build a call to a global operator new. FNNAME is the name of the
3251 operator (either "operator new" or "operator new[]") and ARGS are
3252 the arguments provided. This may change ARGS. *SIZE points to the
3253 total number of bytes required by the allocation, and is updated if
3254 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
3255 be used. If this function determines that no cookie should be
3256 used, after all, *COOKIE_SIZE is set to NULL_TREE. If FN is
3257 non-NULL, it will be set, upon return, to the allocation function
3258 called. */
3260 tree
3261 build_operator_new_call (tree fnname, VEC(tree,gc) **args,
3262 tree *size, tree *cookie_size,
3263 tree *fn)
3265 tree fns;
3266 struct z_candidate *candidates;
3267 struct z_candidate *cand;
3268 bool any_viable_p;
3270 if (fn)
3271 *fn = NULL_TREE;
3272 VEC_safe_insert (tree, gc, *args, 0, *size);
3273 *args = resolve_args (*args);
3274 if (*args == NULL)
3275 return error_mark_node;
3277 /* Based on:
3279 [expr.new]
3281 If this lookup fails to find the name, or if the allocated type
3282 is not a class type, the allocation function's name is looked
3283 up in the global scope.
3285 we disregard block-scope declarations of "operator new". */
3286 fns = lookup_function_nonclass (fnname, *args, /*block_p=*/false);
3288 /* Figure out what function is being called. */
3289 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p);
3291 /* If no suitable function could be found, issue an error message
3292 and give up. */
3293 if (!cand)
3295 if (!any_viable_p)
3296 error ("no matching function for call to %<%D(%A)%>",
3297 DECL_NAME (OVL_CURRENT (fns)), build_tree_list_vec (*args));
3298 else
3299 error ("call of overloaded %<%D(%A)%> is ambiguous",
3300 DECL_NAME (OVL_CURRENT (fns)), build_tree_list_vec (*args));
3301 if (candidates)
3302 print_z_candidates (candidates);
3303 return error_mark_node;
3306 /* If a cookie is required, add some extra space. Whether
3307 or not a cookie is required cannot be determined until
3308 after we know which function was called. */
3309 if (*cookie_size)
3311 bool use_cookie = true;
3312 if (!abi_version_at_least (2))
3314 /* In G++ 3.2, the check was implemented incorrectly; it
3315 looked at the placement expression, rather than the
3316 type of the function. */
3317 if (VEC_length (tree, *args) == 2
3318 && same_type_p (TREE_TYPE (VEC_index (tree, *args, 1)),
3319 ptr_type_node))
3320 use_cookie = false;
3322 else
3324 tree arg_types;
3326 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
3327 /* Skip the size_t parameter. */
3328 arg_types = TREE_CHAIN (arg_types);
3329 /* Check the remaining parameters (if any). */
3330 if (arg_types
3331 && TREE_CHAIN (arg_types) == void_list_node
3332 && same_type_p (TREE_VALUE (arg_types),
3333 ptr_type_node))
3334 use_cookie = false;
3336 /* If we need a cookie, adjust the number of bytes allocated. */
3337 if (use_cookie)
3339 /* Update the total size. */
3340 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
3341 /* Update the argument list to reflect the adjusted size. */
3342 VEC_replace (tree, *args, 0, *size);
3344 else
3345 *cookie_size = NULL_TREE;
3348 /* Tell our caller which function we decided to call. */
3349 if (fn)
3350 *fn = cand->fn;
3352 /* Build the CALL_EXPR. */
3353 return build_over_call (cand, LOOKUP_NORMAL, tf_warning_or_error);
3356 /* Build a new call to operator(). This may change ARGS. */
3358 tree
3359 build_op_call (tree obj, VEC(tree,gc) **args, tsubst_flags_t complain)
3361 struct z_candidate *candidates = 0, *cand;
3362 tree fns, convs, first_mem_arg = NULL_TREE;
3363 tree type = TREE_TYPE (obj);
3364 bool any_viable_p;
3365 tree result = NULL_TREE;
3366 void *p;
3368 if (error_operand_p (obj))
3369 return error_mark_node;
3371 obj = prep_operand (obj);
3373 if (TYPE_PTRMEMFUNC_P (type))
3375 if (complain & tf_error)
3376 /* It's no good looking for an overloaded operator() on a
3377 pointer-to-member-function. */
3378 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
3379 return error_mark_node;
3382 if (TYPE_BINFO (type))
3384 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
3385 if (fns == error_mark_node)
3386 return error_mark_node;
3388 else
3389 fns = NULL_TREE;
3391 if (args != NULL && *args != NULL)
3393 *args = resolve_args (*args);
3394 if (*args == NULL)
3395 return error_mark_node;
3398 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3399 p = conversion_obstack_alloc (0);
3401 if (fns)
3403 first_mem_arg = build_this (obj);
3405 add_candidates (BASELINK_FUNCTIONS (fns),
3406 first_mem_arg, *args, NULL_TREE,
3407 NULL_TREE, false,
3408 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
3409 LOOKUP_NORMAL, &candidates);
3412 convs = lookup_conversions (type, /*lookup_template_convs_p=*/true);
3414 for (; convs; convs = TREE_CHAIN (convs))
3416 tree fns = TREE_VALUE (convs);
3417 tree totype = TREE_TYPE (convs);
3419 if ((TREE_CODE (totype) == POINTER_TYPE
3420 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
3421 || (TREE_CODE (totype) == REFERENCE_TYPE
3422 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
3423 || (TREE_CODE (totype) == REFERENCE_TYPE
3424 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
3425 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
3426 for (; fns; fns = OVL_NEXT (fns))
3428 tree fn = OVL_CURRENT (fns);
3430 if (DECL_NONCONVERTING_P (fn))
3431 continue;
3433 if (TREE_CODE (fn) == TEMPLATE_DECL)
3434 add_template_conv_candidate
3435 (&candidates, fn, obj, NULL_TREE, *args, totype,
3436 /*access_path=*/NULL_TREE,
3437 /*conversion_path=*/NULL_TREE);
3438 else
3439 add_conv_candidate (&candidates, fn, obj, NULL_TREE,
3440 *args, /*conversion_path=*/NULL_TREE,
3441 /*access_path=*/NULL_TREE);
3445 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3446 if (!any_viable_p)
3448 if (complain & tf_error)
3450 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
3451 build_tree_list_vec (*args));
3452 print_z_candidates (candidates);
3454 result = error_mark_node;
3456 else
3458 cand = tourney (candidates);
3459 if (cand == 0)
3461 if (complain & tf_error)
3463 error ("call of %<(%T) (%A)%> is ambiguous",
3464 TREE_TYPE (obj), build_tree_list_vec (*args));
3465 print_z_candidates (candidates);
3467 result = error_mark_node;
3469 /* Since cand->fn will be a type, not a function, for a conversion
3470 function, we must be careful not to unconditionally look at
3471 DECL_NAME here. */
3472 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
3473 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
3474 result = build_over_call (cand, LOOKUP_NORMAL, complain);
3475 else
3477 obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1,
3478 complain);
3479 obj = convert_from_reference (obj);
3480 result = cp_build_function_call_vec (obj, args, complain);
3484 /* Free all the conversions we allocated. */
3485 obstack_free (&conversion_obstack, p);
3487 return result;
3490 static void
3491 op_error (enum tree_code code, enum tree_code code2,
3492 tree arg1, tree arg2, tree arg3, bool match)
3494 const char *opname;
3496 if (code == MODIFY_EXPR)
3497 opname = assignment_operator_name_info[code2].name;
3498 else
3499 opname = operator_name_info[code].name;
3501 switch (code)
3503 case COND_EXPR:
3504 if (match)
3505 error ("ambiguous overload for ternary %<operator?:%> "
3506 "in %<%E ? %E : %E%>", arg1, arg2, arg3);
3507 else
3508 error ("no match for ternary %<operator?:%> "
3509 "in %<%E ? %E : %E%>", arg1, arg2, arg3);
3510 break;
3512 case POSTINCREMENT_EXPR:
3513 case POSTDECREMENT_EXPR:
3514 if (match)
3515 error ("ambiguous overload for %<operator%s%> in %<%E%s%>",
3516 opname, arg1, opname);
3517 else
3518 error ("no match for %<operator%s%> in %<%E%s%>",
3519 opname, arg1, opname);
3520 break;
3522 case ARRAY_REF:
3523 if (match)
3524 error ("ambiguous overload for %<operator[]%> in %<%E[%E]%>",
3525 arg1, arg2);
3526 else
3527 error ("no match for %<operator[]%> in %<%E[%E]%>",
3528 arg1, arg2);
3529 break;
3531 case REALPART_EXPR:
3532 case IMAGPART_EXPR:
3533 if (match)
3534 error ("ambiguous overload for %qs in %<%s %E%>",
3535 opname, opname, arg1);
3536 else
3537 error ("no match for %qs in %<%s %E%>",
3538 opname, opname, arg1);
3539 break;
3541 default:
3542 if (arg2)
3543 if (match)
3544 error ("ambiguous overload for %<operator%s%> in %<%E %s %E%>",
3545 opname, arg1, opname, arg2);
3546 else
3547 error ("no match for %<operator%s%> in %<%E %s %E%>",
3548 opname, arg1, opname, arg2);
3549 else
3550 if (match)
3551 error ("ambiguous overload for %<operator%s%> in %<%s%E%>",
3552 opname, opname, arg1);
3553 else
3554 error ("no match for %<operator%s%> in %<%s%E%>",
3555 opname, opname, arg1);
3556 break;
3560 /* Return the implicit conversion sequence that could be used to
3561 convert E1 to E2 in [expr.cond]. */
3563 static conversion *
3564 conditional_conversion (tree e1, tree e2)
3566 tree t1 = non_reference (TREE_TYPE (e1));
3567 tree t2 = non_reference (TREE_TYPE (e2));
3568 conversion *conv;
3569 bool good_base;
3571 /* [expr.cond]
3573 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
3574 implicitly converted (clause _conv_) to the type "reference to
3575 T2", subject to the constraint that in the conversion the
3576 reference must bind directly (_dcl.init.ref_) to E1. */
3577 if (real_lvalue_p (e2))
3579 conv = implicit_conversion (build_reference_type (t2),
3582 /*c_cast_p=*/false,
3583 LOOKUP_NO_TEMP_BIND|LOOKUP_ONLYCONVERTING);
3584 if (conv)
3585 return conv;
3588 /* [expr.cond]
3590 If E1 and E2 have class type, and the underlying class types are
3591 the same or one is a base class of the other: E1 can be converted
3592 to match E2 if the class of T2 is the same type as, or a base
3593 class of, the class of T1, and the cv-qualification of T2 is the
3594 same cv-qualification as, or a greater cv-qualification than, the
3595 cv-qualification of T1. If the conversion is applied, E1 is
3596 changed to an rvalue of type T2 that still refers to the original
3597 source class object (or the appropriate subobject thereof). */
3598 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
3599 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
3601 if (good_base && at_least_as_qualified_p (t2, t1))
3603 conv = build_identity_conv (t1, e1);
3604 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
3605 TYPE_MAIN_VARIANT (t2)))
3606 conv = build_conv (ck_base, t2, conv);
3607 else
3608 conv = build_conv (ck_rvalue, t2, conv);
3609 return conv;
3611 else
3612 return NULL;
3614 else
3615 /* [expr.cond]
3617 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
3618 converted to the type that expression E2 would have if E2 were
3619 converted to an rvalue (or the type it has, if E2 is an rvalue). */
3620 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
3621 LOOKUP_IMPLICIT);
3624 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
3625 arguments to the conditional expression. */
3627 tree
3628 build_conditional_expr (tree arg1, tree arg2, tree arg3,
3629 tsubst_flags_t complain)
3631 tree arg2_type;
3632 tree arg3_type;
3633 tree result = NULL_TREE;
3634 tree result_save;
3635 tree result_type = NULL_TREE;
3636 bool lvalue_p = true;
3637 struct z_candidate *candidates = 0;
3638 struct z_candidate *cand;
3639 void *p;
3641 /* As a G++ extension, the second argument to the conditional can be
3642 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
3643 c'.) If the second operand is omitted, make sure it is
3644 calculated only once. */
3645 if (!arg2)
3647 if (complain & tf_error)
3648 pedwarn (input_location, OPT_pedantic,
3649 "ISO C++ forbids omitting the middle term of a ?: expression");
3651 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
3652 if (real_lvalue_p (arg1))
3653 arg2 = arg1 = stabilize_reference (arg1);
3654 else
3655 arg2 = arg1 = save_expr (arg1);
3658 /* [expr.cond]
3660 The first expression is implicitly converted to bool (clause
3661 _conv_). */
3662 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
3663 LOOKUP_NORMAL);
3665 /* If something has already gone wrong, just pass that fact up the
3666 tree. */
3667 if (error_operand_p (arg1)
3668 || error_operand_p (arg2)
3669 || error_operand_p (arg3))
3670 return error_mark_node;
3672 /* [expr.cond]
3674 If either the second or the third operand has type (possibly
3675 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3676 array-to-pointer (_conv.array_), and function-to-pointer
3677 (_conv.func_) standard conversions are performed on the second
3678 and third operands. */
3679 arg2_type = unlowered_expr_type (arg2);
3680 arg3_type = unlowered_expr_type (arg3);
3681 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3683 /* Do the conversions. We don't these for `void' type arguments
3684 since it can't have any effect and since decay_conversion
3685 does not handle that case gracefully. */
3686 if (!VOID_TYPE_P (arg2_type))
3687 arg2 = decay_conversion (arg2);
3688 if (!VOID_TYPE_P (arg3_type))
3689 arg3 = decay_conversion (arg3);
3690 arg2_type = TREE_TYPE (arg2);
3691 arg3_type = TREE_TYPE (arg3);
3693 /* [expr.cond]
3695 One of the following shall hold:
3697 --The second or the third operand (but not both) is a
3698 throw-expression (_except.throw_); the result is of the
3699 type of the other and is an rvalue.
3701 --Both the second and the third operands have type void; the
3702 result is of type void and is an rvalue.
3704 We must avoid calling force_rvalue for expressions of type
3705 "void" because it will complain that their value is being
3706 used. */
3707 if (TREE_CODE (arg2) == THROW_EXPR
3708 && TREE_CODE (arg3) != THROW_EXPR)
3710 if (!VOID_TYPE_P (arg3_type))
3711 arg3 = force_rvalue (arg3);
3712 arg3_type = TREE_TYPE (arg3);
3713 result_type = arg3_type;
3715 else if (TREE_CODE (arg2) != THROW_EXPR
3716 && TREE_CODE (arg3) == THROW_EXPR)
3718 if (!VOID_TYPE_P (arg2_type))
3719 arg2 = force_rvalue (arg2);
3720 arg2_type = TREE_TYPE (arg2);
3721 result_type = arg2_type;
3723 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3724 result_type = void_type_node;
3725 else
3727 if (complain & tf_error)
3729 if (VOID_TYPE_P (arg2_type))
3730 error ("second operand to the conditional operator "
3731 "is of type %<void%>, "
3732 "but the third operand is neither a throw-expression "
3733 "nor of type %<void%>");
3734 else
3735 error ("third operand to the conditional operator "
3736 "is of type %<void%>, "
3737 "but the second operand is neither a throw-expression "
3738 "nor of type %<void%>");
3740 return error_mark_node;
3743 lvalue_p = false;
3744 goto valid_operands;
3746 /* [expr.cond]
3748 Otherwise, if the second and third operand have different types,
3749 and either has (possibly cv-qualified) class type, an attempt is
3750 made to convert each of those operands to the type of the other. */
3751 else if (!same_type_p (arg2_type, arg3_type)
3752 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3754 conversion *conv2;
3755 conversion *conv3;
3757 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3758 p = conversion_obstack_alloc (0);
3760 conv2 = conditional_conversion (arg2, arg3);
3761 conv3 = conditional_conversion (arg3, arg2);
3763 /* [expr.cond]
3765 If both can be converted, or one can be converted but the
3766 conversion is ambiguous, the program is ill-formed. If
3767 neither can be converted, the operands are left unchanged and
3768 further checking is performed as described below. If exactly
3769 one conversion is possible, that conversion is applied to the
3770 chosen operand and the converted operand is used in place of
3771 the original operand for the remainder of this section. */
3772 if ((conv2 && !conv2->bad_p
3773 && conv3 && !conv3->bad_p)
3774 || (conv2 && conv2->kind == ck_ambig)
3775 || (conv3 && conv3->kind == ck_ambig))
3777 error ("operands to ?: have different types %qT and %qT",
3778 arg2_type, arg3_type);
3779 result = error_mark_node;
3781 else if (conv2 && (!conv2->bad_p || !conv3))
3783 arg2 = convert_like (conv2, arg2, complain);
3784 arg2 = convert_from_reference (arg2);
3785 arg2_type = TREE_TYPE (arg2);
3786 /* Even if CONV2 is a valid conversion, the result of the
3787 conversion may be invalid. For example, if ARG3 has type
3788 "volatile X", and X does not have a copy constructor
3789 accepting a "volatile X&", then even if ARG2 can be
3790 converted to X, the conversion will fail. */
3791 if (error_operand_p (arg2))
3792 result = error_mark_node;
3794 else if (conv3 && (!conv3->bad_p || !conv2))
3796 arg3 = convert_like (conv3, arg3, complain);
3797 arg3 = convert_from_reference (arg3);
3798 arg3_type = TREE_TYPE (arg3);
3799 if (error_operand_p (arg3))
3800 result = error_mark_node;
3803 /* Free all the conversions we allocated. */
3804 obstack_free (&conversion_obstack, p);
3806 if (result)
3807 return result;
3809 /* If, after the conversion, both operands have class type,
3810 treat the cv-qualification of both operands as if it were the
3811 union of the cv-qualification of the operands.
3813 The standard is not clear about what to do in this
3814 circumstance. For example, if the first operand has type
3815 "const X" and the second operand has a user-defined
3816 conversion to "volatile X", what is the type of the second
3817 operand after this step? Making it be "const X" (matching
3818 the first operand) seems wrong, as that discards the
3819 qualification without actually performing a copy. Leaving it
3820 as "volatile X" seems wrong as that will result in the
3821 conditional expression failing altogether, even though,
3822 according to this step, the one operand could be converted to
3823 the type of the other. */
3824 if ((conv2 || conv3)
3825 && CLASS_TYPE_P (arg2_type)
3826 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
3827 arg2_type = arg3_type =
3828 cp_build_qualified_type (arg2_type,
3829 cp_type_quals (arg2_type)
3830 | cp_type_quals (arg3_type));
3833 /* [expr.cond]
3835 If the second and third operands are lvalues and have the same
3836 type, the result is of that type and is an lvalue. */
3837 if (real_lvalue_p (arg2)
3838 && real_lvalue_p (arg3)
3839 && same_type_p (arg2_type, arg3_type))
3841 result_type = arg2_type;
3842 goto valid_operands;
3845 /* [expr.cond]
3847 Otherwise, the result is an rvalue. If the second and third
3848 operand do not have the same type, and either has (possibly
3849 cv-qualified) class type, overload resolution is used to
3850 determine the conversions (if any) to be applied to the operands
3851 (_over.match.oper_, _over.built_). */
3852 lvalue_p = false;
3853 if (!same_type_p (arg2_type, arg3_type)
3854 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3856 tree args[3];
3857 conversion *conv;
3858 bool any_viable_p;
3860 /* Rearrange the arguments so that add_builtin_candidate only has
3861 to know about two args. In build_builtin_candidate, the
3862 arguments are unscrambled. */
3863 args[0] = arg2;
3864 args[1] = arg3;
3865 args[2] = arg1;
3866 add_builtin_candidates (&candidates,
3867 COND_EXPR,
3868 NOP_EXPR,
3869 ansi_opname (COND_EXPR),
3870 args,
3871 LOOKUP_NORMAL);
3873 /* [expr.cond]
3875 If the overload resolution fails, the program is
3876 ill-formed. */
3877 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3878 if (!any_viable_p)
3880 if (complain & tf_error)
3882 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
3883 print_z_candidates (candidates);
3885 return error_mark_node;
3887 cand = tourney (candidates);
3888 if (!cand)
3890 if (complain & tf_error)
3892 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
3893 print_z_candidates (candidates);
3895 return error_mark_node;
3898 /* [expr.cond]
3900 Otherwise, the conversions thus determined are applied, and
3901 the converted operands are used in place of the original
3902 operands for the remainder of this section. */
3903 conv = cand->convs[0];
3904 arg1 = convert_like (conv, arg1, complain);
3905 conv = cand->convs[1];
3906 arg2 = convert_like (conv, arg2, complain);
3907 arg2_type = TREE_TYPE (arg2);
3908 conv = cand->convs[2];
3909 arg3 = convert_like (conv, arg3, complain);
3910 arg3_type = TREE_TYPE (arg3);
3913 /* [expr.cond]
3915 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3916 and function-to-pointer (_conv.func_) standard conversions are
3917 performed on the second and third operands.
3919 We need to force the lvalue-to-rvalue conversion here for class types,
3920 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3921 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3922 regions. */
3924 arg2 = force_rvalue (arg2);
3925 if (!CLASS_TYPE_P (arg2_type))
3926 arg2_type = TREE_TYPE (arg2);
3928 arg3 = force_rvalue (arg3);
3929 if (!CLASS_TYPE_P (arg3_type))
3930 arg3_type = TREE_TYPE (arg3);
3932 if (arg2 == error_mark_node || arg3 == error_mark_node)
3933 return error_mark_node;
3935 /* [expr.cond]
3937 After those conversions, one of the following shall hold:
3939 --The second and third operands have the same type; the result is of
3940 that type. */
3941 if (same_type_p (arg2_type, arg3_type))
3942 result_type = arg2_type;
3943 /* [expr.cond]
3945 --The second and third operands have arithmetic or enumeration
3946 type; the usual arithmetic conversions are performed to bring
3947 them to a common type, and the result is of that type. */
3948 else if ((ARITHMETIC_TYPE_P (arg2_type)
3949 || UNSCOPED_ENUM_P (arg2_type))
3950 && (ARITHMETIC_TYPE_P (arg3_type)
3951 || UNSCOPED_ENUM_P (arg3_type)))
3953 /* In this case, there is always a common type. */
3954 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3955 arg3_type);
3957 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3958 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3960 if (complain & tf_warning)
3961 warning (0,
3962 "enumeral mismatch in conditional expression: %qT vs %qT",
3963 arg2_type, arg3_type);
3965 else if (extra_warnings
3966 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3967 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3968 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3969 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3971 if (complain & tf_warning)
3972 warning (0,
3973 "enumeral and non-enumeral type in conditional expression");
3976 arg2 = perform_implicit_conversion (result_type, arg2, complain);
3977 arg3 = perform_implicit_conversion (result_type, arg3, complain);
3979 /* [expr.cond]
3981 --The second and third operands have pointer type, or one has
3982 pointer type and the other is a null pointer constant; pointer
3983 conversions (_conv.ptr_) and qualification conversions
3984 (_conv.qual_) are performed to bring them to their composite
3985 pointer type (_expr.rel_). The result is of the composite
3986 pointer type.
3988 --The second and third operands have pointer to member type, or
3989 one has pointer to member type and the other is a null pointer
3990 constant; pointer to member conversions (_conv.mem_) and
3991 qualification conversions (_conv.qual_) are performed to bring
3992 them to a common type, whose cv-qualification shall match the
3993 cv-qualification of either the second or the third operand.
3994 The result is of the common type. */
3995 else if ((null_ptr_cst_p (arg2)
3996 && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
3997 || (null_ptr_cst_p (arg3)
3998 && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
3999 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
4000 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
4001 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
4003 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
4004 arg3, CPO_CONDITIONAL_EXPR,
4005 complain);
4006 if (result_type == error_mark_node)
4007 return error_mark_node;
4008 arg2 = perform_implicit_conversion (result_type, arg2, complain);
4009 arg3 = perform_implicit_conversion (result_type, arg3, complain);
4012 if (!result_type)
4014 if (complain & tf_error)
4015 error ("operands to ?: have different types %qT and %qT",
4016 arg2_type, arg3_type);
4017 return error_mark_node;
4020 valid_operands:
4021 result_save = build3 (COND_EXPR, result_type, arg1, arg2, arg3);
4022 result = fold_if_not_in_template (result_save);
4024 if (cp_unevaluated_operand && TREE_CODE (result) == CALL_EXPR)
4025 /* Avoid folding to a CALL_EXPR within decltype (c++/42013). */
4026 result = result_save;
4028 /* We can't use result_type below, as fold might have returned a
4029 throw_expr. */
4031 if (!lvalue_p)
4033 /* Expand both sides into the same slot, hopefully the target of
4034 the ?: expression. We used to check for TARGET_EXPRs here,
4035 but now we sometimes wrap them in NOP_EXPRs so the test would
4036 fail. */
4037 if (CLASS_TYPE_P (TREE_TYPE (result)))
4038 result = get_target_expr (result);
4039 /* If this expression is an rvalue, but might be mistaken for an
4040 lvalue, we must add a NON_LVALUE_EXPR. */
4041 result = rvalue (result);
4044 return result;
4047 /* OPERAND is an operand to an expression. Perform necessary steps
4048 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
4049 returned. */
4051 static tree
4052 prep_operand (tree operand)
4054 if (operand)
4056 if (CLASS_TYPE_P (TREE_TYPE (operand))
4057 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
4058 /* Make sure the template type is instantiated now. */
4059 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
4062 return operand;
4065 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
4066 OVERLOAD) to the CANDIDATES, returning an updated list of
4067 CANDIDATES. The ARGS are the arguments provided to the call;
4068 if FIRST_ARG is non-null it is the implicit object argument,
4069 otherwise the first element of ARGS is used if needed. The
4070 EXPLICIT_TARGS are explicit template arguments provided.
4071 TEMPLATE_ONLY is true if only template functions should be
4072 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
4073 add_function_candidate. */
4075 static void
4076 add_candidates (tree fns, tree first_arg, const VEC(tree,gc) *args,
4077 tree return_type,
4078 tree explicit_targs, bool template_only,
4079 tree conversion_path, tree access_path,
4080 int flags,
4081 struct z_candidate **candidates)
4083 tree ctype;
4084 const VEC(tree,gc) *non_static_args;
4085 bool check_list_ctor;
4086 bool check_converting;
4087 unification_kind_t strict;
4088 tree fn;
4090 if (!fns)
4091 return;
4093 /* Precalculate special handling of constructors and conversion ops. */
4094 fn = OVL_CURRENT (fns);
4095 if (DECL_CONV_FN_P (fn))
4097 check_list_ctor = false;
4098 check_converting = !!(flags & LOOKUP_ONLYCONVERTING);
4099 if (flags & LOOKUP_NO_CONVERSION)
4100 /* We're doing return_type(x). */
4101 strict = DEDUCE_CONV;
4102 else
4103 /* We're doing x.operator return_type(). */
4104 strict = DEDUCE_EXACT;
4105 /* [over.match.funcs] For conversion functions, the function
4106 is considered to be a member of the class of the implicit
4107 object argument for the purpose of defining the type of
4108 the implicit object parameter. */
4109 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (first_arg)));
4111 else
4113 if (DECL_CONSTRUCTOR_P (fn))
4115 check_list_ctor = !!(flags & LOOKUP_LIST_ONLY);
4116 check_converting = !!(flags & LOOKUP_ONLYCONVERTING);
4118 else
4120 check_list_ctor = false;
4121 check_converting = false;
4123 strict = DEDUCE_CALL;
4124 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
4127 if (first_arg)
4128 non_static_args = args;
4129 else
4130 /* Delay creating the implicit this parameter until it is needed. */
4131 non_static_args = NULL;
4133 for (; fns; fns = OVL_NEXT (fns))
4135 tree fn_first_arg;
4136 const VEC(tree,gc) *fn_args;
4138 fn = OVL_CURRENT (fns);
4140 if (check_converting && DECL_NONCONVERTING_P (fn))
4141 continue;
4142 if (check_list_ctor && !is_list_ctor (fn))
4143 continue;
4145 /* Figure out which set of arguments to use. */
4146 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4148 /* If this function is a non-static member and we didn't get an
4149 implicit object argument, move it out of args. */
4150 if (first_arg == NULL_TREE)
4152 unsigned int ix;
4153 tree arg;
4154 VEC(tree,gc) *tempvec
4155 = VEC_alloc (tree, gc, VEC_length (tree, args) - 1);
4156 for (ix = 1; VEC_iterate (tree, args, ix, arg); ++ix)
4157 VEC_quick_push (tree, tempvec, arg);
4158 non_static_args = tempvec;
4159 first_arg = build_this (VEC_index (tree, args, 0));
4162 fn_first_arg = first_arg;
4163 fn_args = non_static_args;
4165 else
4167 /* Otherwise, just use the list of arguments provided. */
4168 fn_first_arg = NULL_TREE;
4169 fn_args = args;
4172 if (TREE_CODE (fn) == TEMPLATE_DECL)
4173 add_template_candidate (candidates,
4175 ctype,
4176 explicit_targs,
4177 fn_first_arg,
4178 fn_args,
4179 return_type,
4180 access_path,
4181 conversion_path,
4182 flags,
4183 strict);
4184 else if (!template_only)
4185 add_function_candidate (candidates,
4187 ctype,
4188 fn_first_arg,
4189 fn_args,
4190 access_path,
4191 conversion_path,
4192 flags);
4196 /* Even unsigned enum types promote to signed int. We don't want to
4197 issue -Wsign-compare warnings for this case. Here ORIG_ARG is the
4198 original argument and ARG is the argument after any conversions
4199 have been applied. We set TREE_NO_WARNING if we have added a cast
4200 from an unsigned enum type to a signed integer type. */
4202 static void
4203 avoid_sign_compare_warnings (tree orig_arg, tree arg)
4205 if (orig_arg != NULL_TREE
4206 && arg != NULL_TREE
4207 && orig_arg != arg
4208 && TREE_CODE (TREE_TYPE (orig_arg)) == ENUMERAL_TYPE
4209 && TYPE_UNSIGNED (TREE_TYPE (orig_arg))
4210 && INTEGRAL_TYPE_P (TREE_TYPE (arg))
4211 && !TYPE_UNSIGNED (TREE_TYPE (arg)))
4212 TREE_NO_WARNING (arg) = 1;
4215 tree
4216 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
4217 bool *overloaded_p, tsubst_flags_t complain)
4219 tree orig_arg1 = arg1;
4220 tree orig_arg2 = arg2;
4221 tree orig_arg3 = arg3;
4222 struct z_candidate *candidates = 0, *cand;
4223 VEC(tree,gc) *arglist;
4224 tree fnname;
4225 tree args[3];
4226 tree result = NULL_TREE;
4227 bool result_valid_p = false;
4228 enum tree_code code2 = NOP_EXPR;
4229 enum tree_code code_orig_arg1 = ERROR_MARK;
4230 enum tree_code code_orig_arg2 = ERROR_MARK;
4231 conversion *conv;
4232 void *p;
4233 bool strict_p;
4234 bool any_viable_p;
4236 if (error_operand_p (arg1)
4237 || error_operand_p (arg2)
4238 || error_operand_p (arg3))
4239 return error_mark_node;
4241 if (code == MODIFY_EXPR)
4243 code2 = TREE_CODE (arg3);
4244 arg3 = NULL_TREE;
4245 fnname = ansi_assopname (code2);
4247 else
4248 fnname = ansi_opname (code);
4250 arg1 = prep_operand (arg1);
4252 switch (code)
4254 case NEW_EXPR:
4255 case VEC_NEW_EXPR:
4256 case VEC_DELETE_EXPR:
4257 case DELETE_EXPR:
4258 /* Use build_op_new_call and build_op_delete_call instead. */
4259 gcc_unreachable ();
4261 case CALL_EXPR:
4262 /* Use build_op_call instead. */
4263 gcc_unreachable ();
4265 case TRUTH_ORIF_EXPR:
4266 case TRUTH_ANDIF_EXPR:
4267 case TRUTH_AND_EXPR:
4268 case TRUTH_OR_EXPR:
4269 /* These are saved for the sake of warn_logical_operator. */
4270 code_orig_arg1 = TREE_CODE (arg1);
4271 code_orig_arg2 = TREE_CODE (arg2);
4273 default:
4274 break;
4277 arg2 = prep_operand (arg2);
4278 arg3 = prep_operand (arg3);
4280 if (code == COND_EXPR)
4281 /* Use build_conditional_expr instead. */
4282 gcc_unreachable ();
4283 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
4284 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
4285 goto builtin;
4287 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
4288 arg2 = integer_zero_node;
4290 arglist = VEC_alloc (tree, gc, 3);
4291 VEC_quick_push (tree, arglist, arg1);
4292 if (arg2 != NULL_TREE)
4293 VEC_quick_push (tree, arglist, arg2);
4294 if (arg3 != NULL_TREE)
4295 VEC_quick_push (tree, arglist, arg3);
4297 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4298 p = conversion_obstack_alloc (0);
4300 /* Add namespace-scope operators to the list of functions to
4301 consider. */
4302 add_candidates (lookup_function_nonclass (fnname, arglist, /*block_p=*/true),
4303 NULL_TREE, arglist, NULL_TREE,
4304 NULL_TREE, false, NULL_TREE, NULL_TREE,
4305 flags, &candidates);
4306 /* Add class-member operators to the candidate set. */
4307 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
4309 tree fns;
4311 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
4312 if (fns == error_mark_node)
4314 result = error_mark_node;
4315 goto user_defined_result_ready;
4317 if (fns)
4318 add_candidates (BASELINK_FUNCTIONS (fns),
4319 NULL_TREE, arglist, NULL_TREE,
4320 NULL_TREE, false,
4321 BASELINK_BINFO (fns),
4322 BASELINK_ACCESS_BINFO (fns),
4323 flags, &candidates);
4326 args[0] = arg1;
4327 args[1] = arg2;
4328 args[2] = NULL_TREE;
4330 add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
4332 switch (code)
4334 case COMPOUND_EXPR:
4335 case ADDR_EXPR:
4336 /* For these, the built-in candidates set is empty
4337 [over.match.oper]/3. We don't want non-strict matches
4338 because exact matches are always possible with built-in
4339 operators. The built-in candidate set for COMPONENT_REF
4340 would be empty too, but since there are no such built-in
4341 operators, we accept non-strict matches for them. */
4342 strict_p = true;
4343 break;
4345 default:
4346 strict_p = pedantic;
4347 break;
4350 candidates = splice_viable (candidates, strict_p, &any_viable_p);
4351 if (!any_viable_p)
4353 switch (code)
4355 case POSTINCREMENT_EXPR:
4356 case POSTDECREMENT_EXPR:
4357 /* Don't try anything fancy if we're not allowed to produce
4358 errors. */
4359 if (!(complain & tf_error))
4360 return error_mark_node;
4362 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
4363 distinguish between prefix and postfix ++ and
4364 operator++() was used for both, so we allow this with
4365 -fpermissive. */
4366 if (flags & LOOKUP_COMPLAIN)
4368 const char *msg = (flag_permissive)
4369 ? G_("no %<%D(int)%> declared for postfix %qs,"
4370 " trying prefix operator instead")
4371 : G_("no %<%D(int)%> declared for postfix %qs");
4372 permerror (input_location, msg, fnname,
4373 operator_name_info[code].name);
4376 if (!flag_permissive)
4377 return error_mark_node;
4379 if (code == POSTINCREMENT_EXPR)
4380 code = PREINCREMENT_EXPR;
4381 else
4382 code = PREDECREMENT_EXPR;
4383 result = build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE,
4384 overloaded_p, complain);
4385 break;
4387 /* The caller will deal with these. */
4388 case ADDR_EXPR:
4389 case COMPOUND_EXPR:
4390 case COMPONENT_REF:
4391 result = NULL_TREE;
4392 result_valid_p = true;
4393 break;
4395 default:
4396 if ((flags & LOOKUP_COMPLAIN) && (complain & tf_error))
4398 /* If one of the arguments of the operator represents
4399 an invalid use of member function pointer, try to report
4400 a meaningful error ... */
4401 if (invalid_nonstatic_memfn_p (arg1, tf_error)
4402 || invalid_nonstatic_memfn_p (arg2, tf_error)
4403 || invalid_nonstatic_memfn_p (arg3, tf_error))
4404 /* We displayed the error message. */;
4405 else
4407 /* ... Otherwise, report the more generic
4408 "no matching operator found" error */
4409 op_error (code, code2, arg1, arg2, arg3, FALSE);
4410 print_z_candidates (candidates);
4413 result = error_mark_node;
4414 break;
4417 else
4419 cand = tourney (candidates);
4420 if (cand == 0)
4422 if ((flags & LOOKUP_COMPLAIN) && (complain & tf_error))
4424 op_error (code, code2, arg1, arg2, arg3, TRUE);
4425 print_z_candidates (candidates);
4427 result = error_mark_node;
4429 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
4431 if (overloaded_p)
4432 *overloaded_p = true;
4434 if (resolve_args (arglist) == NULL)
4435 result = error_mark_node;
4436 else
4437 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4439 else
4441 /* Give any warnings we noticed during overload resolution. */
4442 if (cand->warnings && (complain & tf_warning))
4444 struct candidate_warning *w;
4445 for (w = cand->warnings; w; w = w->next)
4446 joust (cand, w->loser, 1);
4449 /* Check for comparison of different enum types. */
4450 switch (code)
4452 case GT_EXPR:
4453 case LT_EXPR:
4454 case GE_EXPR:
4455 case LE_EXPR:
4456 case EQ_EXPR:
4457 case NE_EXPR:
4458 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
4459 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
4460 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
4461 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
4462 && (complain & tf_warning))
4464 warning (OPT_Wenum_compare,
4465 "comparison between %q#T and %q#T",
4466 TREE_TYPE (arg1), TREE_TYPE (arg2));
4468 break;
4469 default:
4470 break;
4473 /* We need to strip any leading REF_BIND so that bitfields
4474 don't cause errors. This should not remove any important
4475 conversions, because builtins don't apply to class
4476 objects directly. */
4477 conv = cand->convs[0];
4478 if (conv->kind == ck_ref_bind)
4479 conv = conv->u.next;
4480 arg1 = convert_like (conv, arg1, complain);
4482 if (arg2)
4484 /* We need to call warn_logical_operator before
4485 converting arg2 to a boolean_type. */
4486 if (complain & tf_warning)
4487 warn_logical_operator (input_location, code, boolean_type_node,
4488 code_orig_arg1, arg1,
4489 code_orig_arg2, arg2);
4491 conv = cand->convs[1];
4492 if (conv->kind == ck_ref_bind)
4493 conv = conv->u.next;
4494 arg2 = convert_like (conv, arg2, complain);
4496 if (arg3)
4498 conv = cand->convs[2];
4499 if (conv->kind == ck_ref_bind)
4500 conv = conv->u.next;
4501 arg3 = convert_like (conv, arg3, complain);
4507 user_defined_result_ready:
4509 /* Free all the conversions we allocated. */
4510 obstack_free (&conversion_obstack, p);
4512 if (result || result_valid_p)
4513 return result;
4515 builtin:
4516 avoid_sign_compare_warnings (orig_arg1, arg1);
4517 avoid_sign_compare_warnings (orig_arg2, arg2);
4518 avoid_sign_compare_warnings (orig_arg3, arg3);
4520 switch (code)
4522 case MODIFY_EXPR:
4523 return cp_build_modify_expr (arg1, code2, arg2, complain);
4525 case INDIRECT_REF:
4526 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
4528 case TRUTH_ANDIF_EXPR:
4529 case TRUTH_ORIF_EXPR:
4530 case TRUTH_AND_EXPR:
4531 case TRUTH_OR_EXPR:
4532 warn_logical_operator (input_location, code, boolean_type_node,
4533 code_orig_arg1, arg1, code_orig_arg2, arg2);
4534 /* Fall through. */
4535 case PLUS_EXPR:
4536 case MINUS_EXPR:
4537 case MULT_EXPR:
4538 case TRUNC_DIV_EXPR:
4539 case GT_EXPR:
4540 case LT_EXPR:
4541 case GE_EXPR:
4542 case LE_EXPR:
4543 case EQ_EXPR:
4544 case NE_EXPR:
4545 case MAX_EXPR:
4546 case MIN_EXPR:
4547 case LSHIFT_EXPR:
4548 case RSHIFT_EXPR:
4549 case TRUNC_MOD_EXPR:
4550 case BIT_AND_EXPR:
4551 case BIT_IOR_EXPR:
4552 case BIT_XOR_EXPR:
4553 return cp_build_binary_op (input_location, code, arg1, arg2, complain);
4555 case UNARY_PLUS_EXPR:
4556 case NEGATE_EXPR:
4557 case BIT_NOT_EXPR:
4558 case TRUTH_NOT_EXPR:
4559 case PREINCREMENT_EXPR:
4560 case POSTINCREMENT_EXPR:
4561 case PREDECREMENT_EXPR:
4562 case POSTDECREMENT_EXPR:
4563 case REALPART_EXPR:
4564 case IMAGPART_EXPR:
4565 return cp_build_unary_op (code, arg1, candidates != 0, complain);
4567 case ARRAY_REF:
4568 return cp_build_array_ref (input_location, arg1, arg2, complain);
4570 case MEMBER_REF:
4571 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_NULL,
4572 complain),
4573 arg2);
4575 /* The caller will deal with these. */
4576 case ADDR_EXPR:
4577 case COMPONENT_REF:
4578 case COMPOUND_EXPR:
4579 return NULL_TREE;
4581 default:
4582 gcc_unreachable ();
4584 return NULL_TREE;
4587 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
4588 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
4590 static bool
4591 non_placement_deallocation_fn_p (tree t)
4593 /* A template instance is never a usual deallocation function,
4594 regardless of its signature. */
4595 if (TREE_CODE (t) == TEMPLATE_DECL
4596 || primary_template_instantiation_p (t))
4597 return false;
4599 /* If a class T has a member deallocation function named operator delete
4600 with exactly one parameter, then that function is a usual
4601 (non-placement) deallocation function. If class T does not declare
4602 such an operator delete but does declare a member deallocation
4603 function named operator delete with exactly two parameters, the second
4604 of which has type std::size_t (18.2), then this function is a usual
4605 deallocation function. */
4606 t = FUNCTION_ARG_CHAIN (t);
4607 if (t == void_list_node
4608 || (t && same_type_p (TREE_VALUE (t), size_type_node)
4609 && TREE_CHAIN (t) == void_list_node))
4610 return true;
4611 return false;
4614 /* Build a call to operator delete. This has to be handled very specially,
4615 because the restrictions on what signatures match are different from all
4616 other call instances. For a normal delete, only a delete taking (void *)
4617 or (void *, size_t) is accepted. For a placement delete, only an exact
4618 match with the placement new is accepted.
4620 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
4621 ADDR is the pointer to be deleted.
4622 SIZE is the size of the memory block to be deleted.
4623 GLOBAL_P is true if the delete-expression should not consider
4624 class-specific delete operators.
4625 PLACEMENT is the corresponding placement new call, or NULL_TREE.
4627 If this call to "operator delete" is being generated as part to
4628 deallocate memory allocated via a new-expression (as per [expr.new]
4629 which requires that if the initialization throws an exception then
4630 we call a deallocation function), then ALLOC_FN is the allocation
4631 function. */
4633 tree
4634 build_op_delete_call (enum tree_code code, tree addr, tree size,
4635 bool global_p, tree placement,
4636 tree alloc_fn)
4638 tree fn = NULL_TREE;
4639 tree fns, fnname, type, t;
4641 if (addr == error_mark_node)
4642 return error_mark_node;
4644 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
4646 fnname = ansi_opname (code);
4648 if (CLASS_TYPE_P (type)
4649 && COMPLETE_TYPE_P (complete_type (type))
4650 && !global_p)
4651 /* In [class.free]
4653 If the result of the lookup is ambiguous or inaccessible, or if
4654 the lookup selects a placement deallocation function, the
4655 program is ill-formed.
4657 Therefore, we ask lookup_fnfields to complain about ambiguity. */
4659 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
4660 if (fns == error_mark_node)
4661 return error_mark_node;
4663 else
4664 fns = NULL_TREE;
4666 if (fns == NULL_TREE)
4667 fns = lookup_name_nonclass (fnname);
4669 /* Strip const and volatile from addr. */
4670 addr = cp_convert (ptr_type_node, addr);
4672 if (placement)
4674 /* "A declaration of a placement deallocation function matches the
4675 declaration of a placement allocation function if it has the same
4676 number of parameters and, after parameter transformations (8.3.5),
4677 all parameter types except the first are identical."
4679 So we build up the function type we want and ask instantiate_type
4680 to get it for us. */
4681 t = FUNCTION_ARG_CHAIN (alloc_fn);
4682 t = tree_cons (NULL_TREE, ptr_type_node, t);
4683 t = build_function_type (void_type_node, t);
4685 fn = instantiate_type (t, fns, tf_none);
4686 if (fn == error_mark_node)
4687 return NULL_TREE;
4689 if (BASELINK_P (fn))
4690 fn = BASELINK_FUNCTIONS (fn);
4692 /* "If the lookup finds the two-parameter form of a usual deallocation
4693 function (3.7.4.2) and that function, considered as a placement
4694 deallocation function, would have been selected as a match for the
4695 allocation function, the program is ill-formed." */
4696 if (non_placement_deallocation_fn_p (fn))
4698 /* But if the class has an operator delete (void *), then that is
4699 the usual deallocation function, so we shouldn't complain
4700 about using the operator delete (void *, size_t). */
4701 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
4702 t; t = OVL_NEXT (t))
4704 tree elt = OVL_CURRENT (t);
4705 if (non_placement_deallocation_fn_p (elt)
4706 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
4707 goto ok;
4709 permerror (0, "non-placement deallocation function %q+D", fn);
4710 permerror (input_location, "selected for placement delete");
4711 ok:;
4714 else
4715 /* "Any non-placement deallocation function matches a non-placement
4716 allocation function. If the lookup finds a single matching
4717 deallocation function, that function will be called; otherwise, no
4718 deallocation function will be called." */
4719 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
4720 t; t = OVL_NEXT (t))
4722 tree elt = OVL_CURRENT (t);
4723 if (non_placement_deallocation_fn_p (elt))
4725 fn = elt;
4726 /* "If a class T has a member deallocation function named
4727 operator delete with exactly one parameter, then that
4728 function is a usual (non-placement) deallocation
4729 function. If class T does not declare such an operator
4730 delete but does declare a member deallocation function named
4731 operator delete with exactly two parameters, the second of
4732 which has type std::size_t (18.2), then this function is a
4733 usual deallocation function."
4735 So (void*) beats (void*, size_t). */
4736 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4737 break;
4741 /* If we have a matching function, call it. */
4742 if (fn)
4744 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
4746 /* If the FN is a member function, make sure that it is
4747 accessible. */
4748 if (BASELINK_P (fns))
4749 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn);
4751 /* Core issue 901: It's ok to new a type with deleted delete. */
4752 if (DECL_DELETED_FN (fn) && alloc_fn)
4753 return NULL_TREE;
4755 if (placement)
4757 /* The placement args might not be suitable for overload
4758 resolution at this point, so build the call directly. */
4759 int nargs = call_expr_nargs (placement);
4760 tree *argarray = (tree *) alloca (nargs * sizeof (tree));
4761 int i;
4762 argarray[0] = addr;
4763 for (i = 1; i < nargs; i++)
4764 argarray[i] = CALL_EXPR_ARG (placement, i);
4765 mark_used (fn);
4766 return build_cxx_call (fn, nargs, argarray);
4768 else
4770 tree ret;
4771 VEC(tree,gc) *args = VEC_alloc (tree, gc, 2);
4772 VEC_quick_push (tree, args, addr);
4773 if (FUNCTION_ARG_CHAIN (fn) != void_list_node)
4774 VEC_quick_push (tree, args, size);
4775 ret = cp_build_function_call_vec (fn, &args, tf_warning_or_error);
4776 VEC_free (tree, gc, args);
4777 return ret;
4781 /* [expr.new]
4783 If no unambiguous matching deallocation function can be found,
4784 propagating the exception does not cause the object's memory to
4785 be freed. */
4786 if (alloc_fn)
4788 if (!placement)
4789 warning (0, "no corresponding deallocation function for %qD",
4790 alloc_fn);
4791 return NULL_TREE;
4794 error ("no suitable %<operator %s%> for %qT",
4795 operator_name_info[(int)code].name, type);
4796 return error_mark_node;
4799 /* If the current scope isn't allowed to access DECL along
4800 BASETYPE_PATH, give an error. The most derived class in
4801 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
4802 the declaration to use in the error diagnostic. */
4804 bool
4805 enforce_access (tree basetype_path, tree decl, tree diag_decl)
4807 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
4809 if (!accessible_p (basetype_path, decl, true))
4811 if (TREE_PRIVATE (decl))
4812 error ("%q+#D is private", diag_decl);
4813 else if (TREE_PROTECTED (decl))
4814 error ("%q+#D is protected", diag_decl);
4815 else
4816 error ("%q+#D is inaccessible", diag_decl);
4817 error ("within this context");
4818 return false;
4821 return true;
4824 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
4825 bitwise or of LOOKUP_* values. If any errors are warnings are
4826 generated, set *DIAGNOSTIC_FN to "error" or "warning",
4827 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
4828 to NULL. */
4830 static tree
4831 build_temp (tree expr, tree type, int flags,
4832 diagnostic_t *diagnostic_kind)
4834 int savew, savee;
4835 VEC(tree,gc) *args;
4837 savew = warningcount, savee = errorcount;
4838 args = make_tree_vector_single (expr);
4839 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4840 &args, type, flags, tf_warning_or_error);
4841 release_tree_vector (args);
4842 if (warningcount > savew)
4843 *diagnostic_kind = DK_WARNING;
4844 else if (errorcount > savee)
4845 *diagnostic_kind = DK_ERROR;
4846 else
4847 *diagnostic_kind = DK_UNSPECIFIED;
4848 return expr;
4851 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
4852 EXPR is implicitly converted to type TOTYPE.
4853 FN and ARGNUM are used for diagnostics. */
4855 static void
4856 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
4858 tree t = non_reference (totype);
4860 /* Issue warnings about peculiar, but valid, uses of NULL. */
4861 if (expr == null_node && TREE_CODE (t) != BOOLEAN_TYPE && ARITHMETIC_TYPE_P (t))
4863 if (fn)
4864 warning_at (input_location, OPT_Wconversion_null,
4865 "passing NULL to non-pointer argument %P of %qD",
4866 argnum, fn);
4867 else
4868 warning_at (input_location, OPT_Wconversion_null,
4869 "converting to non-pointer type %qT from NULL", t);
4872 /* Issue warnings if "false" is converted to a NULL pointer */
4873 else if (expr == boolean_false_node && fn && POINTER_TYPE_P (t))
4874 warning_at (input_location, OPT_Wconversion_null,
4875 "converting %<false%> to pointer type for argument %P of %qD",
4876 argnum, fn);
4879 /* Perform the conversions in CONVS on the expression EXPR. FN and
4880 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
4881 indicates the `this' argument of a method. INNER is nonzero when
4882 being called to continue a conversion chain. It is negative when a
4883 reference binding will be applied, positive otherwise. If
4884 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
4885 conversions will be emitted if appropriate. If C_CAST_P is true,
4886 this conversion is coming from a C-style cast; in that case,
4887 conversions to inaccessible bases are permitted. */
4889 static tree
4890 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
4891 int inner, bool issue_conversion_warnings,
4892 bool c_cast_p, tsubst_flags_t complain)
4894 tree totype = convs->type;
4895 diagnostic_t diag_kind;
4896 int flags;
4898 if (convs->bad_p
4899 && convs->kind != ck_user
4900 && convs->kind != ck_list
4901 && convs->kind != ck_ambig
4902 && convs->kind != ck_ref_bind
4903 && convs->kind != ck_rvalue
4904 && convs->kind != ck_base)
4906 conversion *t = convs;
4908 /* Give a helpful error if this is bad because of excess braces. */
4909 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
4910 && SCALAR_TYPE_P (totype)
4911 && CONSTRUCTOR_NELTS (expr) > 0
4912 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
4913 permerror (input_location, "too many braces around initializer for %qT", totype);
4915 for (; t; t = convs->u.next)
4917 if (t->kind == ck_user || !t->bad_p)
4919 expr = convert_like_real (t, expr, fn, argnum, 1,
4920 /*issue_conversion_warnings=*/false,
4921 /*c_cast_p=*/false,
4922 complain);
4923 break;
4925 else if (t->kind == ck_ambig)
4926 return convert_like_real (t, expr, fn, argnum, 1,
4927 /*issue_conversion_warnings=*/false,
4928 /*c_cast_p=*/false,
4929 complain);
4930 else if (t->kind == ck_identity)
4931 break;
4933 if (complain & tf_error)
4935 permerror (input_location, "invalid conversion from %qT to %qT", TREE_TYPE (expr), totype);
4936 if (fn)
4937 permerror (input_location, " initializing argument %P of %qD", argnum, fn);
4939 else
4940 return error_mark_node;
4942 return cp_convert (totype, expr);
4945 if (issue_conversion_warnings && (complain & tf_warning))
4946 conversion_null_warnings (totype, expr, fn, argnum);
4948 switch (convs->kind)
4950 case ck_user:
4952 struct z_candidate *cand = convs->cand;
4953 tree convfn = cand->fn;
4954 unsigned i;
4956 expr = mark_rvalue_use (expr);
4958 /* When converting from an init list we consider explicit
4959 constructors, but actually trying to call one is an error. */
4960 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn))
4962 if (complain & tf_error)
4963 error ("converting to %qT from initializer list would use "
4964 "explicit constructor %qD", totype, convfn);
4965 else
4966 return error_mark_node;
4969 /* Set user_conv_p on the argument conversions, so rvalue/base
4970 handling knows not to allow any more UDCs. */
4971 for (i = 0; i < cand->num_convs; ++i)
4972 cand->convs[i]->user_conv_p = true;
4974 expr = build_over_call (cand, LOOKUP_NORMAL, complain);
4976 /* If this is a constructor or a function returning an aggr type,
4977 we need to build up a TARGET_EXPR. */
4978 if (DECL_CONSTRUCTOR_P (convfn))
4980 expr = build_cplus_new (totype, expr);
4982 /* Remember that this was list-initialization. */
4983 if (convs->check_narrowing)
4984 TARGET_EXPR_LIST_INIT_P (expr) = true;
4987 return expr;
4989 case ck_identity:
4990 expr = mark_rvalue_use (expr);
4991 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4993 int nelts = CONSTRUCTOR_NELTS (expr);
4994 if (nelts == 0)
4995 expr = integer_zero_node;
4996 else if (nelts == 1)
4997 expr = CONSTRUCTOR_ELT (expr, 0)->value;
4998 else
4999 gcc_unreachable ();
5002 if (type_unknown_p (expr))
5003 expr = instantiate_type (totype, expr, complain);
5004 /* Convert a constant to its underlying value, unless we are
5005 about to bind it to a reference, in which case we need to
5006 leave it as an lvalue. */
5007 if (inner >= 0)
5009 expr = decl_constant_value (expr);
5010 if (expr == null_node && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
5011 /* If __null has been converted to an integer type, we do not
5012 want to warn about uses of EXPR as an integer, rather than
5013 as a pointer. */
5014 expr = build_int_cst (totype, 0);
5016 return expr;
5017 case ck_ambig:
5018 if (!(complain & tf_error))
5019 return error_mark_node;
5020 /* Call build_user_type_conversion again for the error. */
5021 return build_user_type_conversion
5022 (totype, convs->u.expr, LOOKUP_NORMAL);
5024 case ck_list:
5026 /* Conversion to std::initializer_list<T>. */
5027 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
5028 tree new_ctor = build_constructor (init_list_type_node, NULL);
5029 unsigned len = CONSTRUCTOR_NELTS (expr);
5030 tree array, val;
5031 VEC(tree,gc) *parms;
5032 unsigned ix;
5034 /* Convert all the elements. */
5035 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
5037 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
5038 1, false, false, complain);
5039 if (sub == error_mark_node)
5040 return sub;
5041 check_narrowing (TREE_TYPE (sub), val);
5042 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
5044 /* Build up the array. */
5045 elttype = cp_build_qualified_type
5046 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
5047 array = build_array_of_n_type (elttype, len);
5048 array = finish_compound_literal (array, new_ctor);
5050 parms = make_tree_vector ();
5051 VEC_safe_push (tree, gc, parms, decay_conversion (array));
5052 VEC_safe_push (tree, gc, parms, size_int (len));
5053 /* Call the private constructor. */
5054 push_deferring_access_checks (dk_no_check);
5055 new_ctor = build_special_member_call
5056 (NULL_TREE, complete_ctor_identifier, &parms, totype, 0, complain);
5057 release_tree_vector (parms);
5058 pop_deferring_access_checks ();
5059 return build_cplus_new (totype, new_ctor);
5062 case ck_aggr:
5063 return get_target_expr (digest_init (totype, expr));
5065 default:
5066 break;
5069 expr = convert_like_real (convs->u.next, expr, fn, argnum,
5070 convs->kind == ck_ref_bind ? -1 : 1,
5071 convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
5072 c_cast_p,
5073 complain);
5074 if (expr == error_mark_node)
5075 return error_mark_node;
5077 switch (convs->kind)
5079 case ck_rvalue:
5080 expr = decay_conversion (expr);
5081 if (! MAYBE_CLASS_TYPE_P (totype))
5082 return expr;
5083 /* Else fall through. */
5084 case ck_base:
5085 if (convs->kind == ck_base && !convs->need_temporary_p)
5087 /* We are going to bind a reference directly to a base-class
5088 subobject of EXPR. */
5089 /* Build an expression for `*((base*) &expr)'. */
5090 expr = cp_build_unary_op (ADDR_EXPR, expr, 0, complain);
5091 expr = convert_to_base (expr, build_pointer_type (totype),
5092 !c_cast_p, /*nonnull=*/true, complain);
5093 expr = cp_build_indirect_ref (expr, RO_IMPLICIT_CONVERSION, complain);
5094 return expr;
5097 /* Copy-initialization where the cv-unqualified version of the source
5098 type is the same class as, or a derived class of, the class of the
5099 destination [is treated as direct-initialization]. [dcl.init] */
5100 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
5101 if (convs->user_conv_p)
5102 /* This conversion is being done in the context of a user-defined
5103 conversion (i.e. the second step of copy-initialization), so
5104 don't allow any more. */
5105 flags |= LOOKUP_NO_CONVERSION;
5106 expr = build_temp (expr, totype, flags, &diag_kind);
5107 if (diag_kind && fn)
5109 if ((complain & tf_error))
5110 emit_diagnostic (diag_kind, input_location, 0,
5111 " initializing argument %P of %qD", argnum, fn);
5112 else if (diag_kind == DK_ERROR)
5113 return error_mark_node;
5115 return build_cplus_new (totype, expr);
5117 case ck_ref_bind:
5119 tree ref_type = totype;
5121 if (convs->bad_p && TYPE_REF_IS_RVALUE (ref_type)
5122 && real_lvalue_p (expr))
5124 if (complain & tf_error)
5126 error ("cannot bind %qT lvalue to %qT",
5127 TREE_TYPE (expr), totype);
5128 if (fn)
5129 error (" initializing argument %P of %q+D", argnum, fn);
5131 return error_mark_node;
5134 /* If necessary, create a temporary.
5136 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
5137 that need temporaries, even when their types are reference
5138 compatible with the type of reference being bound, so the
5139 upcoming call to cp_build_unary_op (ADDR_EXPR, expr, ...)
5140 doesn't fail. */
5141 if (convs->need_temporary_p
5142 || TREE_CODE (expr) == CONSTRUCTOR
5143 || TREE_CODE (expr) == VA_ARG_EXPR)
5145 tree type = convs->u.next->type;
5146 cp_lvalue_kind lvalue = real_lvalue_p (expr);
5148 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type))
5149 && !TYPE_REF_IS_RVALUE (ref_type))
5151 if (complain & tf_error)
5153 /* If the reference is volatile or non-const, we
5154 cannot create a temporary. */
5155 if (lvalue & clk_bitfield)
5156 error ("cannot bind bitfield %qE to %qT",
5157 expr, ref_type);
5158 else if (lvalue & clk_packed)
5159 error ("cannot bind packed field %qE to %qT",
5160 expr, ref_type);
5161 else
5162 error ("cannot bind rvalue %qE to %qT", expr, ref_type);
5164 return error_mark_node;
5166 /* If the source is a packed field, and we must use a copy
5167 constructor, then building the target expr will require
5168 binding the field to the reference parameter to the
5169 copy constructor, and we'll end up with an infinite
5170 loop. If we can use a bitwise copy, then we'll be
5171 OK. */
5172 if ((lvalue & clk_packed)
5173 && CLASS_TYPE_P (type)
5174 && !TYPE_HAS_TRIVIAL_INIT_REF (type))
5176 if (complain & tf_error)
5177 error ("cannot bind packed field %qE to %qT",
5178 expr, ref_type);
5179 return error_mark_node;
5181 if (lvalue & clk_bitfield)
5183 expr = convert_bitfield_to_declared_type (expr);
5184 expr = fold_convert (type, expr);
5186 expr = build_target_expr_with_type (expr, type);
5189 /* Take the address of the thing to which we will bind the
5190 reference. */
5191 expr = cp_build_unary_op (ADDR_EXPR, expr, 1, complain);
5192 if (expr == error_mark_node)
5193 return error_mark_node;
5195 /* Convert it to a pointer to the type referred to by the
5196 reference. This will adjust the pointer if a derived to
5197 base conversion is being performed. */
5198 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
5199 expr);
5200 /* Convert the pointer to the desired reference type. */
5201 return build_nop (ref_type, expr);
5204 case ck_lvalue:
5205 return decay_conversion (expr);
5207 case ck_qual:
5208 /* Warn about deprecated conversion if appropriate. */
5209 string_conv_p (totype, expr, 1);
5210 break;
5212 case ck_ptr:
5213 if (convs->base_p)
5214 expr = convert_to_base (expr, totype, !c_cast_p,
5215 /*nonnull=*/false, complain);
5216 return build_nop (totype, expr);
5218 case ck_pmem:
5219 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
5220 c_cast_p);
5222 default:
5223 break;
5226 if (convs->check_narrowing)
5227 check_narrowing (totype, expr);
5229 if (issue_conversion_warnings && (complain & tf_warning))
5230 expr = convert_and_check (totype, expr);
5231 else
5232 expr = convert (totype, expr);
5234 return expr;
5237 /* ARG is being passed to a varargs function. Perform any conversions
5238 required. Return the converted value. */
5240 tree
5241 convert_arg_to_ellipsis (tree arg)
5243 /* [expr.call]
5245 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
5246 standard conversions are performed. */
5247 arg = decay_conversion (arg);
5248 /* [expr.call]
5250 If the argument has integral or enumeration type that is subject
5251 to the integral promotions (_conv.prom_), or a floating point
5252 type that is subject to the floating point promotion
5253 (_conv.fpprom_), the value of the argument is converted to the
5254 promoted type before the call. */
5255 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
5256 && (TYPE_PRECISION (TREE_TYPE (arg))
5257 < TYPE_PRECISION (double_type_node))
5258 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (arg))))
5259 arg = convert_to_real (double_type_node, arg);
5260 else if (NULLPTR_TYPE_P (TREE_TYPE (arg)))
5261 arg = null_pointer_node;
5262 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
5263 arg = perform_integral_promotions (arg);
5265 arg = require_complete_type (arg);
5267 if (arg != error_mark_node
5268 && (type_has_nontrivial_copy_init (TREE_TYPE (arg))
5269 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (arg))))
5271 /* [expr.call] 5.2.2/7:
5272 Passing a potentially-evaluated argument of class type (Clause 9)
5273 with a non-trivial copy constructor or a non-trivial destructor
5274 with no corresponding parameter is conditionally-supported, with
5275 implementation-defined semantics.
5277 We used to just warn here and do a bitwise copy, but now
5278 cp_expr_size will abort if we try to do that.
5280 If the call appears in the context of a sizeof expression,
5281 it is not potentially-evaluated. */
5282 if (cp_unevaluated_operand == 0)
5283 error ("cannot pass objects of non-trivially-copyable "
5284 "type %q#T through %<...%>", TREE_TYPE (arg));
5287 return arg;
5290 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
5292 tree
5293 build_x_va_arg (tree expr, tree type)
5295 if (processing_template_decl)
5296 return build_min (VA_ARG_EXPR, type, expr);
5298 type = complete_type_or_else (type, NULL_TREE);
5300 if (expr == error_mark_node || !type)
5301 return error_mark_node;
5303 expr = mark_lvalue_use (expr);
5305 if (type_has_nontrivial_copy_init (type)
5306 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5307 || TREE_CODE (type) == REFERENCE_TYPE)
5309 /* Remove reference types so we don't ICE later on. */
5310 tree type1 = non_reference (type);
5311 /* conditionally-supported behavior [expr.call] 5.2.2/7. */
5312 error ("cannot receive objects of non-trivially-copyable type %q#T "
5313 "through %<...%>; ", type);
5314 expr = convert (build_pointer_type (type1), null_node);
5315 expr = cp_build_indirect_ref (expr, RO_NULL, tf_warning_or_error);
5316 return expr;
5319 return build_va_arg (input_location, expr, type);
5322 /* TYPE has been given to va_arg. Apply the default conversions which
5323 would have happened when passed via ellipsis. Return the promoted
5324 type, or the passed type if there is no change. */
5326 tree
5327 cxx_type_promotes_to (tree type)
5329 tree promote;
5331 /* Perform the array-to-pointer and function-to-pointer
5332 conversions. */
5333 type = type_decays_to (type);
5335 promote = type_promotes_to (type);
5336 if (same_type_p (type, promote))
5337 promote = type;
5339 return promote;
5342 /* ARG is a default argument expression being passed to a parameter of
5343 the indicated TYPE, which is a parameter to FN. Do any required
5344 conversions. Return the converted value. */
5346 static GTY(()) VEC(tree,gc) *default_arg_context;
5348 tree
5349 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
5351 int i;
5352 tree t;
5354 /* If the ARG is an unparsed default argument expression, the
5355 conversion cannot be performed. */
5356 if (TREE_CODE (arg) == DEFAULT_ARG)
5358 error ("the default argument for parameter %d of %qD has "
5359 "not yet been parsed",
5360 parmnum, fn);
5361 return error_mark_node;
5364 /* Detect recursion. */
5365 for (i = 0; VEC_iterate (tree, default_arg_context, i, t); ++i)
5366 if (t == fn)
5368 error ("recursive evaluation of default argument for %q#D", fn);
5369 return error_mark_node;
5371 VEC_safe_push (tree, gc, default_arg_context, fn);
5373 if (fn && DECL_TEMPLATE_INFO (fn))
5374 arg = tsubst_default_argument (fn, type, arg);
5376 /* Due to:
5378 [dcl.fct.default]
5380 The names in the expression are bound, and the semantic
5381 constraints are checked, at the point where the default
5382 expressions appears.
5384 we must not perform access checks here. */
5385 push_deferring_access_checks (dk_no_check);
5386 arg = break_out_target_exprs (arg);
5387 if (TREE_CODE (arg) == CONSTRUCTOR)
5389 arg = digest_init (type, arg);
5390 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
5391 "default argument", fn, parmnum,
5392 tf_warning_or_error);
5394 else
5396 /* We must make a copy of ARG, in case subsequent processing
5397 alters any part of it. For example, during gimplification a
5398 cast of the form (T) &X::f (where "f" is a member function)
5399 will lead to replacing the PTRMEM_CST for &X::f with a
5400 VAR_DECL. We can avoid the copy for constants, since they
5401 are never modified in place. */
5402 if (!CONSTANT_CLASS_P (arg))
5403 arg = unshare_expr (arg);
5404 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
5405 "default argument", fn, parmnum,
5406 tf_warning_or_error);
5407 arg = convert_for_arg_passing (type, arg);
5409 pop_deferring_access_checks();
5411 VEC_pop (tree, default_arg_context);
5413 return arg;
5416 /* Returns the type which will really be used for passing an argument of
5417 type TYPE. */
5419 tree
5420 type_passed_as (tree type)
5422 /* Pass classes with copy ctors by invisible reference. */
5423 if (TREE_ADDRESSABLE (type))
5425 type = build_reference_type (type);
5426 /* There are no other pointers to this temporary. */
5427 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
5429 else if (targetm.calls.promote_prototypes (type)
5430 && INTEGRAL_TYPE_P (type)
5431 && COMPLETE_TYPE_P (type)
5432 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
5433 TYPE_SIZE (integer_type_node)))
5434 type = integer_type_node;
5436 return type;
5439 /* Actually perform the appropriate conversion. */
5441 tree
5442 convert_for_arg_passing (tree type, tree val)
5444 tree bitfield_type;
5446 /* If VAL is a bitfield, then -- since it has already been converted
5447 to TYPE -- it cannot have a precision greater than TYPE.
5449 If it has a smaller precision, we must widen it here. For
5450 example, passing "int f:3;" to a function expecting an "int" will
5451 not result in any conversion before this point.
5453 If the precision is the same we must not risk widening. For
5454 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
5455 often have type "int", even though the C++ type for the field is
5456 "long long". If the value is being passed to a function
5457 expecting an "int", then no conversions will be required. But,
5458 if we call convert_bitfield_to_declared_type, the bitfield will
5459 be converted to "long long". */
5460 bitfield_type = is_bitfield_expr_with_lowered_type (val);
5461 if (bitfield_type
5462 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
5463 val = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type), val);
5465 if (val == error_mark_node)
5467 /* Pass classes with copy ctors by invisible reference. */
5468 else if (TREE_ADDRESSABLE (type))
5469 val = build1 (ADDR_EXPR, build_reference_type (type), val);
5470 else if (targetm.calls.promote_prototypes (type)
5471 && INTEGRAL_TYPE_P (type)
5472 && COMPLETE_TYPE_P (type)
5473 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
5474 TYPE_SIZE (integer_type_node)))
5475 val = perform_integral_promotions (val);
5476 if (warn_missing_format_attribute)
5478 tree rhstype = TREE_TYPE (val);
5479 const enum tree_code coder = TREE_CODE (rhstype);
5480 const enum tree_code codel = TREE_CODE (type);
5481 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5482 && coder == codel
5483 && check_missing_format_attribute (type, rhstype))
5484 warning (OPT_Wmissing_format_attribute,
5485 "argument of function call might be a candidate for a format attribute");
5487 return val;
5490 /* Returns true iff FN is a function with magic varargs, i.e. ones for
5491 which no conversions at all should be done. This is true for some
5492 builtins which don't act like normal functions. */
5494 static bool
5495 magic_varargs_p (tree fn)
5497 if (DECL_BUILT_IN (fn))
5498 switch (DECL_FUNCTION_CODE (fn))
5500 case BUILT_IN_CLASSIFY_TYPE:
5501 case BUILT_IN_CONSTANT_P:
5502 case BUILT_IN_NEXT_ARG:
5503 case BUILT_IN_VA_START:
5504 return true;
5506 default:;
5507 return lookup_attribute ("type generic",
5508 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
5511 return false;
5514 /* Subroutine of the various build_*_call functions. Overload resolution
5515 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
5516 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
5517 bitmask of various LOOKUP_* flags which apply to the call itself. */
5519 static tree
5520 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
5522 tree fn = cand->fn;
5523 const VEC(tree,gc) *args = cand->args;
5524 tree first_arg = cand->first_arg;
5525 conversion **convs = cand->convs;
5526 conversion *conv;
5527 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
5528 int parmlen;
5529 tree val;
5530 int i = 0;
5531 int j = 0;
5532 unsigned int arg_index = 0;
5533 int is_method = 0;
5534 int nargs;
5535 tree *argarray;
5536 bool already_used = false;
5538 /* In a template, there is no need to perform all of the work that
5539 is normally done. We are only interested in the type of the call
5540 expression, i.e., the return type of the function. Any semantic
5541 errors will be deferred until the template is instantiated. */
5542 if (processing_template_decl)
5544 tree expr;
5545 tree return_type;
5546 const tree *argarray;
5547 unsigned int nargs;
5549 return_type = TREE_TYPE (TREE_TYPE (fn));
5550 nargs = VEC_length (tree, args);
5551 if (first_arg == NULL_TREE)
5552 argarray = VEC_address (tree, CONST_CAST (VEC(tree,gc) *, args));
5553 else
5555 tree *alcarray;
5556 unsigned int ix;
5557 tree arg;
5559 ++nargs;
5560 alcarray = XALLOCAVEC (tree, nargs);
5561 alcarray[0] = first_arg;
5562 for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
5563 alcarray[ix + 1] = arg;
5564 argarray = alcarray;
5566 expr = build_call_array_loc (input_location,
5567 return_type, build_addr_func (fn), nargs,
5568 argarray);
5569 if (TREE_THIS_VOLATILE (fn) && cfun)
5570 current_function_returns_abnormally = 1;
5571 if (!VOID_TYPE_P (return_type))
5572 require_complete_type (return_type);
5573 return convert_from_reference (expr);
5576 /* Give any warnings we noticed during overload resolution. */
5577 if (cand->warnings)
5579 struct candidate_warning *w;
5580 for (w = cand->warnings; w; w = w->next)
5581 joust (cand, w->loser, 1);
5584 /* Make =delete work with SFINAE. */
5585 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
5586 return error_mark_node;
5588 if (DECL_FUNCTION_MEMBER_P (fn))
5590 /* If FN is a template function, two cases must be considered.
5591 For example:
5593 struct A {
5594 protected:
5595 template <class T> void f();
5597 template <class T> struct B {
5598 protected:
5599 void g();
5601 struct C : A, B<int> {
5602 using A::f; // #1
5603 using B<int>::g; // #2
5606 In case #1 where `A::f' is a member template, DECL_ACCESS is
5607 recorded in the primary template but not in its specialization.
5608 We check access of FN using its primary template.
5610 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
5611 because it is a member of class template B, DECL_ACCESS is
5612 recorded in the specialization `B<int>::g'. We cannot use its
5613 primary template because `B<T>::g' and `B<int>::g' may have
5614 different access. */
5615 if (DECL_TEMPLATE_INFO (fn)
5616 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
5617 perform_or_defer_access_check (cand->access_path,
5618 DECL_TI_TEMPLATE (fn), fn);
5619 else
5620 perform_or_defer_access_check (cand->access_path, fn, fn);
5623 /* Find maximum size of vector to hold converted arguments. */
5624 parmlen = list_length (parm);
5625 nargs = VEC_length (tree, args) + (first_arg != NULL_TREE ? 1 : 0);
5626 if (parmlen > nargs)
5627 nargs = parmlen;
5628 argarray = (tree *) alloca (nargs * sizeof (tree));
5630 /* The implicit parameters to a constructor are not considered by overload
5631 resolution, and must be of the proper type. */
5632 if (DECL_CONSTRUCTOR_P (fn))
5634 if (first_arg != NULL_TREE)
5636 argarray[j++] = first_arg;
5637 first_arg = NULL_TREE;
5639 else
5641 argarray[j++] = VEC_index (tree, args, arg_index);
5642 ++arg_index;
5644 parm = TREE_CHAIN (parm);
5645 /* We should never try to call the abstract constructor. */
5646 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
5648 if (DECL_HAS_VTT_PARM_P (fn))
5650 argarray[j++] = VEC_index (tree, args, arg_index);
5651 ++arg_index;
5652 parm = TREE_CHAIN (parm);
5655 /* Bypass access control for 'this' parameter. */
5656 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5658 tree parmtype = TREE_VALUE (parm);
5659 tree arg = (first_arg != NULL_TREE
5660 ? first_arg
5661 : VEC_index (tree, args, arg_index));
5662 tree argtype = TREE_TYPE (arg);
5663 tree converted_arg;
5664 tree base_binfo;
5666 if (convs[i]->bad_p)
5668 if (complain & tf_error)
5669 permerror (input_location, "passing %qT as %<this%> argument of %q#D discards qualifiers",
5670 TREE_TYPE (argtype), fn);
5671 else
5672 return error_mark_node;
5675 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
5676 X is called for an object that is not of type X, or of a type
5677 derived from X, the behavior is undefined.
5679 So we can assume that anything passed as 'this' is non-null, and
5680 optimize accordingly. */
5681 gcc_assert (TREE_CODE (parmtype) == POINTER_TYPE);
5682 /* Convert to the base in which the function was declared. */
5683 gcc_assert (cand->conversion_path != NULL_TREE);
5684 converted_arg = build_base_path (PLUS_EXPR,
5685 arg,
5686 cand->conversion_path,
5688 /* Check that the base class is accessible. */
5689 if (!accessible_base_p (TREE_TYPE (argtype),
5690 BINFO_TYPE (cand->conversion_path), true))
5691 error ("%qT is not an accessible base of %qT",
5692 BINFO_TYPE (cand->conversion_path),
5693 TREE_TYPE (argtype));
5694 /* If fn was found by a using declaration, the conversion path
5695 will be to the derived class, not the base declaring fn. We
5696 must convert from derived to base. */
5697 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
5698 TREE_TYPE (parmtype), ba_unique, NULL);
5699 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
5700 base_binfo, 1);
5702 argarray[j++] = converted_arg;
5703 parm = TREE_CHAIN (parm);
5704 if (first_arg != NULL_TREE)
5705 first_arg = NULL_TREE;
5706 else
5707 ++arg_index;
5708 ++i;
5709 is_method = 1;
5712 gcc_assert (first_arg == NULL_TREE);
5713 for (; arg_index < VEC_length (tree, args) && parm;
5714 parm = TREE_CHAIN (parm), ++arg_index, ++i)
5716 tree type = TREE_VALUE (parm);
5717 tree arg = VEC_index (tree, args, arg_index);
5719 conv = convs[i];
5721 /* Don't make a copy here if build_call is going to. */
5722 if (conv->kind == ck_rvalue
5723 && COMPLETE_TYPE_P (complete_type (type))
5724 && !TREE_ADDRESSABLE (type))
5725 conv = conv->u.next;
5727 /* Warn about initializer_list deduction that isn't currently in the
5728 working draft. */
5729 if (cxx_dialect > cxx98
5730 && flag_deduce_init_list
5731 && cand->template_decl
5732 && is_std_init_list (non_reference (type))
5733 && BRACE_ENCLOSED_INITIALIZER_P (arg))
5735 tree tmpl = TI_TEMPLATE (cand->template_decl);
5736 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
5737 tree patparm = get_pattern_parm (realparm, tmpl);
5738 tree pattype = TREE_TYPE (patparm);
5739 if (PACK_EXPANSION_P (pattype))
5740 pattype = PACK_EXPANSION_PATTERN (pattype);
5741 pattype = non_reference (pattype);
5743 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
5744 && (cand->explicit_targs == NULL_TREE
5745 || (TREE_VEC_LENGTH (cand->explicit_targs)
5746 <= TEMPLATE_TYPE_IDX (pattype))))
5748 pedwarn (input_location, 0, "deducing %qT as %qT",
5749 non_reference (TREE_TYPE (patparm)),
5750 non_reference (type));
5751 pedwarn (input_location, 0, " in call to %q+D", cand->fn);
5752 pedwarn (input_location, 0,
5753 " (you can disable this with -fno-deduce-init-list)");
5757 val = convert_like_with_context (conv, arg, fn, i-is_method, complain);
5759 val = convert_for_arg_passing (type, val);
5760 if (val == error_mark_node)
5761 return error_mark_node;
5762 else
5763 argarray[j++] = val;
5766 /* Default arguments */
5767 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
5768 argarray[j++] = convert_default_arg (TREE_VALUE (parm),
5769 TREE_PURPOSE (parm),
5770 fn, i - is_method);
5771 /* Ellipsis */
5772 for (; arg_index < VEC_length (tree, args); ++arg_index)
5774 tree a = VEC_index (tree, args, arg_index);
5775 if (magic_varargs_p (fn))
5776 /* Do no conversions for magic varargs. */
5777 a = mark_type_use (a);
5778 else
5779 a = convert_arg_to_ellipsis (a);
5780 argarray[j++] = a;
5783 gcc_assert (j <= nargs);
5784 nargs = j;
5786 check_function_arguments (TYPE_ATTRIBUTES (TREE_TYPE (fn)),
5787 nargs, argarray, TYPE_ARG_TYPES (TREE_TYPE (fn)));
5789 /* Avoid actually calling copy constructors and copy assignment operators,
5790 if possible. */
5792 if (! flag_elide_constructors)
5793 /* Do things the hard way. */;
5794 else if (cand->num_convs == 1
5795 && (DECL_COPY_CONSTRUCTOR_P (fn)
5796 || DECL_MOVE_CONSTRUCTOR_P (fn)))
5798 tree targ;
5799 tree arg = argarray[num_artificial_parms_for (fn)];
5800 tree fa;
5802 /* Pull out the real argument, disregarding const-correctness. */
5803 targ = arg;
5804 while (CONVERT_EXPR_P (targ)
5805 || TREE_CODE (targ) == NON_LVALUE_EXPR)
5806 targ = TREE_OPERAND (targ, 0);
5807 if (TREE_CODE (targ) == ADDR_EXPR)
5809 targ = TREE_OPERAND (targ, 0);
5810 if (!same_type_ignoring_top_level_qualifiers_p
5811 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
5812 targ = NULL_TREE;
5814 else
5815 targ = NULL_TREE;
5817 if (targ)
5818 arg = targ;
5819 else
5820 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
5822 if (TREE_CODE (arg) == TARGET_EXPR
5823 && TARGET_EXPR_LIST_INIT_P (arg))
5825 /* Copy-list-initialization doesn't require the copy constructor
5826 to be defined. */
5828 /* [class.copy]: the copy constructor is implicitly defined even if
5829 the implementation elided its use. */
5830 else if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn))
5831 || move_fn_p (fn))
5833 mark_used (fn);
5834 already_used = true;
5837 /* If we're creating a temp and we already have one, don't create a
5838 new one. If we're not creating a temp but we get one, use
5839 INIT_EXPR to collapse the temp into our target. Otherwise, if the
5840 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
5841 temp or an INIT_EXPR otherwise. */
5842 fa = (cand->first_arg != NULL_TREE
5843 ? cand->first_arg
5844 : VEC_index (tree, args, 0));
5845 if (integer_zerop (fa))
5847 if (TREE_CODE (arg) == TARGET_EXPR)
5848 return arg;
5849 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn))
5850 && !move_fn_p (fn))
5851 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
5853 else if (TREE_CODE (arg) == TARGET_EXPR
5854 || (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn))
5855 && !move_fn_p (fn)))
5857 tree to = stabilize_reference (cp_build_indirect_ref (fa, RO_NULL,
5858 complain));
5860 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
5861 return val;
5864 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
5865 && copy_fn_p (fn)
5866 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
5868 tree to = stabilize_reference
5869 (cp_build_indirect_ref (argarray[0], RO_NULL, complain));
5870 tree type = TREE_TYPE (to);
5871 tree as_base = CLASSTYPE_AS_BASE (type);
5872 tree arg = argarray[1];
5874 if (is_really_empty_class (type))
5876 /* Avoid copying empty classes. */
5877 val = build2 (COMPOUND_EXPR, void_type_node, to, arg);
5878 TREE_NO_WARNING (val) = 1;
5879 val = build2 (COMPOUND_EXPR, type, val, to);
5880 TREE_NO_WARNING (val) = 1;
5882 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
5884 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
5885 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
5887 else
5889 /* We must only copy the non-tail padding parts.
5890 Use __builtin_memcpy for the bitwise copy.
5891 FIXME fix 22488 so we can go back to using MODIFY_EXPR
5892 instead of an explicit call to memcpy. */
5894 tree arg0, arg1, arg2, t;
5895 tree test = NULL_TREE;
5897 arg2 = TYPE_SIZE_UNIT (as_base);
5898 arg1 = arg;
5899 arg0 = cp_build_unary_op (ADDR_EXPR, to, 0, complain);
5901 if (!can_trust_pointer_alignment ())
5903 /* If we can't be sure about pointer alignment, a call
5904 to __builtin_memcpy is expanded as a call to memcpy, which
5905 is invalid with identical args. Otherwise it is
5906 expanded as a block move, which should be safe. */
5907 arg0 = save_expr (arg0);
5908 arg1 = save_expr (arg1);
5909 test = build2 (EQ_EXPR, boolean_type_node, arg0, arg1);
5911 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
5912 t = build_call_n (t, 3, arg0, arg1, arg2);
5914 t = convert (TREE_TYPE (arg0), t);
5915 if (test)
5916 t = build3 (COND_EXPR, TREE_TYPE (t), test, arg0, t);
5917 val = cp_build_indirect_ref (t, RO_NULL, complain);
5918 TREE_NO_WARNING (val) = 1;
5921 return val;
5924 if (!already_used)
5925 mark_used (fn);
5927 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
5929 tree t;
5930 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
5931 DECL_CONTEXT (fn),
5932 ba_any, NULL);
5933 gcc_assert (binfo && binfo != error_mark_node);
5935 /* Warn about deprecated virtual functions now, since we're about
5936 to throw away the decl. */
5937 if (TREE_DEPRECATED (fn))
5938 warn_deprecated_use (fn, NULL_TREE);
5940 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1);
5941 if (TREE_SIDE_EFFECTS (argarray[0]))
5942 argarray[0] = save_expr (argarray[0]);
5943 t = build_pointer_type (TREE_TYPE (fn));
5944 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
5945 fn = build_java_interface_fn_ref (fn, argarray[0]);
5946 else
5947 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
5948 TREE_TYPE (fn) = t;
5950 else
5951 fn = build_addr_func (fn);
5953 return build_cxx_call (fn, nargs, argarray);
5956 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
5957 This function performs no overload resolution, conversion, or other
5958 high-level operations. */
5960 tree
5961 build_cxx_call (tree fn, int nargs, tree *argarray)
5963 tree fndecl;
5965 fn = build_call_a (fn, nargs, argarray);
5967 /* If this call might throw an exception, note that fact. */
5968 fndecl = get_callee_fndecl (fn);
5969 if ((!fndecl || !TREE_NOTHROW (fndecl))
5970 && at_function_scope_p ()
5971 && cfun)
5972 cp_function_chain->can_throw = 1;
5974 /* Check that arguments to builtin functions match the expectations. */
5975 if (fndecl
5976 && DECL_BUILT_IN (fndecl)
5977 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
5978 && !check_builtin_function_arguments (fndecl, nargs, argarray))
5979 return error_mark_node;
5981 /* Some built-in function calls will be evaluated at compile-time in
5982 fold (). */
5983 fn = fold_if_not_in_template (fn);
5985 if (VOID_TYPE_P (TREE_TYPE (fn)))
5986 return fn;
5988 fn = require_complete_type (fn);
5989 if (fn == error_mark_node)
5990 return error_mark_node;
5992 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
5993 fn = build_cplus_new (TREE_TYPE (fn), fn);
5994 return convert_from_reference (fn);
5997 static GTY(()) tree java_iface_lookup_fn;
5999 /* Make an expression which yields the address of the Java interface
6000 method FN. This is achieved by generating a call to libjava's
6001 _Jv_LookupInterfaceMethodIdx(). */
6003 static tree
6004 build_java_interface_fn_ref (tree fn, tree instance)
6006 tree lookup_fn, method, idx;
6007 tree klass_ref, iface, iface_ref;
6008 int i;
6010 if (!java_iface_lookup_fn)
6012 tree endlink = build_void_list_node ();
6013 tree t = tree_cons (NULL_TREE, ptr_type_node,
6014 tree_cons (NULL_TREE, ptr_type_node,
6015 tree_cons (NULL_TREE, java_int_type_node,
6016 endlink)));
6017 java_iface_lookup_fn
6018 = add_builtin_function ("_Jv_LookupInterfaceMethodIdx",
6019 build_function_type (ptr_type_node, t),
6020 0, NOT_BUILT_IN, NULL, NULL_TREE);
6023 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
6024 This is the first entry in the vtable. */
6025 klass_ref = build_vtbl_ref (cp_build_indirect_ref (instance, RO_NULL,
6026 tf_warning_or_error),
6027 integer_zero_node);
6029 /* Get the java.lang.Class pointer for the interface being called. */
6030 iface = DECL_CONTEXT (fn);
6031 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
6032 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
6033 || DECL_CONTEXT (iface_ref) != iface)
6035 error ("could not find class$ field in java interface type %qT",
6036 iface);
6037 return error_mark_node;
6039 iface_ref = build_address (iface_ref);
6040 iface_ref = convert (build_pointer_type (iface), iface_ref);
6042 /* Determine the itable index of FN. */
6043 i = 1;
6044 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
6046 if (!DECL_VIRTUAL_P (method))
6047 continue;
6048 if (fn == method)
6049 break;
6050 i++;
6052 idx = build_int_cst (NULL_TREE, i);
6054 lookup_fn = build1 (ADDR_EXPR,
6055 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
6056 java_iface_lookup_fn);
6057 return build_call_nary (ptr_type_node, lookup_fn,
6058 3, klass_ref, iface_ref, idx);
6061 /* Returns the value to use for the in-charge parameter when making a
6062 call to a function with the indicated NAME.
6064 FIXME:Can't we find a neater way to do this mapping? */
6066 tree
6067 in_charge_arg_for_name (tree name)
6069 if (name == base_ctor_identifier
6070 || name == base_dtor_identifier)
6071 return integer_zero_node;
6072 else if (name == complete_ctor_identifier)
6073 return integer_one_node;
6074 else if (name == complete_dtor_identifier)
6075 return integer_two_node;
6076 else if (name == deleting_dtor_identifier)
6077 return integer_three_node;
6079 /* This function should only be called with one of the names listed
6080 above. */
6081 gcc_unreachable ();
6082 return NULL_TREE;
6085 /* Build a call to a constructor, destructor, or an assignment
6086 operator for INSTANCE, an expression with class type. NAME
6087 indicates the special member function to call; *ARGS are the
6088 arguments. ARGS may be NULL. This may change ARGS. BINFO
6089 indicates the base of INSTANCE that is to be passed as the `this'
6090 parameter to the member function called.
6092 FLAGS are the LOOKUP_* flags to use when processing the call.
6094 If NAME indicates a complete object constructor, INSTANCE may be
6095 NULL_TREE. In this case, the caller will call build_cplus_new to
6096 store the newly constructed object into a VAR_DECL. */
6098 tree
6099 build_special_member_call (tree instance, tree name, VEC(tree,gc) **args,
6100 tree binfo, int flags, tsubst_flags_t complain)
6102 tree fns;
6103 /* The type of the subobject to be constructed or destroyed. */
6104 tree class_type;
6105 VEC(tree,gc) *allocated = NULL;
6106 tree ret;
6108 gcc_assert (name == complete_ctor_identifier
6109 || name == base_ctor_identifier
6110 || name == complete_dtor_identifier
6111 || name == base_dtor_identifier
6112 || name == deleting_dtor_identifier
6113 || name == ansi_assopname (NOP_EXPR));
6114 if (TYPE_P (binfo))
6116 /* Resolve the name. */
6117 if (!complete_type_or_else (binfo, NULL_TREE))
6118 return error_mark_node;
6120 binfo = TYPE_BINFO (binfo);
6123 gcc_assert (binfo != NULL_TREE);
6125 class_type = BINFO_TYPE (binfo);
6127 /* Handle the special case where INSTANCE is NULL_TREE. */
6128 if (name == complete_ctor_identifier && !instance)
6130 instance = build_int_cst (build_pointer_type (class_type), 0);
6131 instance = build1 (INDIRECT_REF, class_type, instance);
6133 else
6135 if (name == complete_dtor_identifier
6136 || name == base_dtor_identifier
6137 || name == deleting_dtor_identifier)
6138 gcc_assert (args == NULL || VEC_empty (tree, *args));
6140 /* Convert to the base class, if necessary. */
6141 if (!same_type_ignoring_top_level_qualifiers_p
6142 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
6144 if (name != ansi_assopname (NOP_EXPR))
6145 /* For constructors and destructors, either the base is
6146 non-virtual, or it is virtual but we are doing the
6147 conversion from a constructor or destructor for the
6148 complete object. In either case, we can convert
6149 statically. */
6150 instance = convert_to_base_statically (instance, binfo);
6151 else
6152 /* However, for assignment operators, we must convert
6153 dynamically if the base is virtual. */
6154 instance = build_base_path (PLUS_EXPR, instance,
6155 binfo, /*nonnull=*/1);
6159 gcc_assert (instance != NULL_TREE);
6161 fns = lookup_fnfields (binfo, name, 1);
6163 /* When making a call to a constructor or destructor for a subobject
6164 that uses virtual base classes, pass down a pointer to a VTT for
6165 the subobject. */
6166 if ((name == base_ctor_identifier
6167 || name == base_dtor_identifier)
6168 && CLASSTYPE_VBASECLASSES (class_type))
6170 tree vtt;
6171 tree sub_vtt;
6173 /* If the current function is a complete object constructor
6174 or destructor, then we fetch the VTT directly.
6175 Otherwise, we look it up using the VTT we were given. */
6176 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
6177 vtt = decay_conversion (vtt);
6178 vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
6179 build2 (EQ_EXPR, boolean_type_node,
6180 current_in_charge_parm, integer_zero_node),
6181 current_vtt_parm,
6182 vtt);
6183 gcc_assert (BINFO_SUBVTT_INDEX (binfo));
6184 sub_vtt = build2 (POINTER_PLUS_EXPR, TREE_TYPE (vtt), vtt,
6185 BINFO_SUBVTT_INDEX (binfo));
6187 if (args == NULL)
6189 allocated = make_tree_vector ();
6190 args = &allocated;
6193 VEC_safe_insert (tree, gc, *args, 0, sub_vtt);
6196 ret = build_new_method_call (instance, fns, args,
6197 TYPE_BINFO (BINFO_TYPE (binfo)),
6198 flags, /*fn=*/NULL,
6199 complain);
6201 if (allocated != NULL)
6202 release_tree_vector (allocated);
6204 return ret;
6207 /* Return the NAME, as a C string. The NAME indicates a function that
6208 is a member of TYPE. *FREE_P is set to true if the caller must
6209 free the memory returned.
6211 Rather than go through all of this, we should simply set the names
6212 of constructors and destructors appropriately, and dispense with
6213 ctor_identifier, dtor_identifier, etc. */
6215 static char *
6216 name_as_c_string (tree name, tree type, bool *free_p)
6218 char *pretty_name;
6220 /* Assume that we will not allocate memory. */
6221 *free_p = false;
6222 /* Constructors and destructors are special. */
6223 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
6225 pretty_name
6226 = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type))));
6227 /* For a destructor, add the '~'. */
6228 if (name == complete_dtor_identifier
6229 || name == base_dtor_identifier
6230 || name == deleting_dtor_identifier)
6232 pretty_name = concat ("~", pretty_name, NULL);
6233 /* Remember that we need to free the memory allocated. */
6234 *free_p = true;
6237 else if (IDENTIFIER_TYPENAME_P (name))
6239 pretty_name = concat ("operator ",
6240 type_as_string_translate (TREE_TYPE (name),
6241 TFF_PLAIN_IDENTIFIER),
6242 NULL);
6243 /* Remember that we need to free the memory allocated. */
6244 *free_p = true;
6246 else
6247 pretty_name = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (name)));
6249 return pretty_name;
6252 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
6253 be set, upon return, to the function called. ARGS may be NULL.
6254 This may change ARGS. */
6256 tree
6257 build_new_method_call (tree instance, tree fns, VEC(tree,gc) **args,
6258 tree conversion_path, int flags,
6259 tree *fn_p, tsubst_flags_t complain)
6261 struct z_candidate *candidates = 0, *cand;
6262 tree explicit_targs = NULL_TREE;
6263 tree basetype = NULL_TREE;
6264 tree access_binfo;
6265 tree optype;
6266 tree first_mem_arg = NULL_TREE;
6267 tree instance_ptr;
6268 tree name;
6269 bool skip_first_for_error;
6270 VEC(tree,gc) *user_args;
6271 tree call;
6272 tree fn;
6273 int template_only = 0;
6274 bool any_viable_p;
6275 tree orig_instance;
6276 tree orig_fns;
6277 VEC(tree,gc) *orig_args = NULL;
6278 void *p;
6280 gcc_assert (instance != NULL_TREE);
6282 /* We don't know what function we're going to call, yet. */
6283 if (fn_p)
6284 *fn_p = NULL_TREE;
6286 if (error_operand_p (instance)
6287 || !fns || error_operand_p (fns))
6288 return error_mark_node;
6290 if (!BASELINK_P (fns))
6292 if (complain & tf_error)
6293 error ("call to non-function %qD", fns);
6294 return error_mark_node;
6297 orig_instance = instance;
6298 orig_fns = fns;
6300 /* Dismantle the baselink to collect all the information we need. */
6301 if (!conversion_path)
6302 conversion_path = BASELINK_BINFO (fns);
6303 access_binfo = BASELINK_ACCESS_BINFO (fns);
6304 optype = BASELINK_OPTYPE (fns);
6305 fns = BASELINK_FUNCTIONS (fns);
6306 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
6308 explicit_targs = TREE_OPERAND (fns, 1);
6309 fns = TREE_OPERAND (fns, 0);
6310 template_only = 1;
6312 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
6313 || TREE_CODE (fns) == TEMPLATE_DECL
6314 || TREE_CODE (fns) == OVERLOAD);
6315 fn = get_first_fn (fns);
6316 name = DECL_NAME (fn);
6318 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
6319 gcc_assert (CLASS_TYPE_P (basetype));
6321 if (processing_template_decl)
6323 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
6324 instance = build_non_dependent_expr (instance);
6325 if (args != NULL)
6326 make_args_non_dependent (*args);
6329 user_args = args == NULL ? NULL : *args;
6330 /* Under DR 147 A::A() is an invalid constructor call,
6331 not a functional cast. */
6332 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
6334 if (! (complain & tf_error))
6335 return error_mark_node;
6337 permerror (input_location,
6338 "cannot call constructor %<%T::%D%> directly",
6339 basetype, name);
6340 permerror (input_location, " for a function-style cast, remove the "
6341 "redundant %<::%D%>", name);
6342 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
6343 complain);
6344 return call;
6347 /* Figure out whether to skip the first argument for the error
6348 message we will display to users if an error occurs. We don't
6349 want to display any compiler-generated arguments. The "this"
6350 pointer hasn't been added yet. However, we must remove the VTT
6351 pointer if this is a call to a base-class constructor or
6352 destructor. */
6353 skip_first_for_error = false;
6354 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
6356 /* Callers should explicitly indicate whether they want to construct
6357 the complete object or just the part without virtual bases. */
6358 gcc_assert (name != ctor_identifier);
6359 /* Similarly for destructors. */
6360 gcc_assert (name != dtor_identifier);
6361 /* Remove the VTT pointer, if present. */
6362 if ((name == base_ctor_identifier || name == base_dtor_identifier)
6363 && CLASSTYPE_VBASECLASSES (basetype))
6364 skip_first_for_error = true;
6367 /* Process the argument list. */
6368 if (args != NULL && *args != NULL)
6370 *args = resolve_args (*args);
6371 if (*args == NULL)
6372 return error_mark_node;
6375 instance_ptr = build_this (instance);
6377 /* It's OK to call destructors and constructors on cv-qualified objects.
6378 Therefore, convert the INSTANCE_PTR to the unqualified type, if
6379 necessary. */
6380 if (DECL_DESTRUCTOR_P (fn)
6381 || DECL_CONSTRUCTOR_P (fn))
6383 tree type = build_pointer_type (basetype);
6384 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
6385 instance_ptr = build_nop (type, instance_ptr);
6387 if (DECL_DESTRUCTOR_P (fn))
6388 name = complete_dtor_identifier;
6390 first_mem_arg = instance_ptr;
6392 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6393 p = conversion_obstack_alloc (0);
6395 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
6396 initializer, not T({ }). */
6397 if (DECL_CONSTRUCTOR_P (fn) && args != NULL && !VEC_empty (tree, *args)
6398 && BRACE_ENCLOSED_INITIALIZER_P (VEC_index (tree, *args, 0))
6399 && CONSTRUCTOR_IS_DIRECT_INIT (VEC_index (tree, *args, 0)))
6401 gcc_assert (VEC_length (tree, *args) == 1
6402 && !(flags & LOOKUP_ONLYCONVERTING));
6404 add_list_candidates (fns, first_mem_arg, VEC_index (tree, *args, 0),
6405 basetype, explicit_targs, template_only,
6406 conversion_path, access_binfo, flags, &candidates);
6408 else
6410 add_candidates (fns, first_mem_arg, user_args, optype,
6411 explicit_targs, template_only, conversion_path,
6412 access_binfo, flags, &candidates);
6414 any_viable_p = false;
6415 candidates = splice_viable (candidates, pedantic, &any_viable_p);
6417 if (!any_viable_p)
6419 if (complain & tf_error)
6421 if (!COMPLETE_TYPE_P (basetype))
6422 cxx_incomplete_type_error (instance_ptr, basetype);
6423 else if (optype)
6424 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
6425 basetype, optype, build_tree_list_vec (user_args),
6426 TREE_TYPE (TREE_TYPE (instance_ptr)));
6427 else
6429 char *pretty_name;
6430 bool free_p;
6431 tree arglist;
6433 pretty_name = name_as_c_string (name, basetype, &free_p);
6434 arglist = build_tree_list_vec (user_args);
6435 if (skip_first_for_error)
6436 arglist = TREE_CHAIN (arglist);
6437 error ("no matching function for call to %<%T::%s(%A)%#V%>",
6438 basetype, pretty_name, arglist,
6439 TREE_TYPE (TREE_TYPE (instance_ptr)));
6440 if (free_p)
6441 free (pretty_name);
6443 print_z_candidates (candidates);
6445 call = error_mark_node;
6447 else
6449 cand = tourney (candidates);
6450 if (cand == 0)
6452 char *pretty_name;
6453 bool free_p;
6454 tree arglist;
6456 if (complain & tf_error)
6458 pretty_name = name_as_c_string (name, basetype, &free_p);
6459 arglist = build_tree_list_vec (user_args);
6460 if (skip_first_for_error)
6461 arglist = TREE_CHAIN (arglist);
6462 error ("call of overloaded %<%s(%A)%> is ambiguous", pretty_name,
6463 arglist);
6464 print_z_candidates (candidates);
6465 if (free_p)
6466 free (pretty_name);
6468 call = error_mark_node;
6470 else
6472 fn = cand->fn;
6474 if (!(flags & LOOKUP_NONVIRTUAL)
6475 && DECL_PURE_VIRTUAL_P (fn)
6476 && instance == current_class_ref
6477 && (DECL_CONSTRUCTOR_P (current_function_decl)
6478 || DECL_DESTRUCTOR_P (current_function_decl))
6479 && (complain & tf_warning))
6480 /* This is not an error, it is runtime undefined
6481 behavior. */
6482 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl) ?
6483 "pure virtual %q#D called from constructor"
6484 : "pure virtual %q#D called from destructor"),
6485 fn);
6487 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
6488 && is_dummy_object (instance_ptr))
6490 if (complain & tf_error)
6491 error ("cannot call member function %qD without object",
6492 fn);
6493 call = error_mark_node;
6495 else
6497 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
6498 && resolves_to_fixed_type_p (instance, 0))
6499 flags |= LOOKUP_NONVIRTUAL;
6500 /* Now we know what function is being called. */
6501 if (fn_p)
6502 *fn_p = fn;
6503 /* Build the actual CALL_EXPR. */
6504 call = build_over_call (cand, flags, complain);
6505 /* In an expression of the form `a->f()' where `f' turns
6506 out to be a static member function, `a' is
6507 none-the-less evaluated. */
6508 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
6509 && !is_dummy_object (instance_ptr)
6510 && TREE_SIDE_EFFECTS (instance_ptr))
6511 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
6512 instance_ptr, call);
6513 else if (call != error_mark_node
6514 && DECL_DESTRUCTOR_P (cand->fn)
6515 && !VOID_TYPE_P (TREE_TYPE (call)))
6516 /* An explicit call of the form "x->~X()" has type
6517 "void". However, on platforms where destructors
6518 return "this" (i.e., those where
6519 targetm.cxx.cdtor_returns_this is true), such calls
6520 will appear to have a return value of pointer type
6521 to the low-level call machinery. We do not want to
6522 change the low-level machinery, since we want to be
6523 able to optimize "delete f()" on such platforms as
6524 "operator delete(~X(f()))" (rather than generating
6525 "t = f(), ~X(t), operator delete (t)"). */
6526 call = build_nop (void_type_node, call);
6531 if (processing_template_decl && call != error_mark_node)
6533 bool cast_to_void = false;
6535 if (TREE_CODE (call) == COMPOUND_EXPR)
6536 call = TREE_OPERAND (call, 1);
6537 else if (TREE_CODE (call) == NOP_EXPR)
6539 cast_to_void = true;
6540 call = TREE_OPERAND (call, 0);
6542 if (TREE_CODE (call) == INDIRECT_REF)
6543 call = TREE_OPERAND (call, 0);
6544 call = (build_min_non_dep_call_vec
6545 (call,
6546 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
6547 orig_instance, orig_fns, NULL_TREE),
6548 orig_args));
6549 call = convert_from_reference (call);
6550 if (cast_to_void)
6551 call = build_nop (void_type_node, call);
6554 /* Free all the conversions we allocated. */
6555 obstack_free (&conversion_obstack, p);
6557 if (orig_args != NULL)
6558 release_tree_vector (orig_args);
6560 return call;
6563 /* Returns true iff standard conversion sequence ICS1 is a proper
6564 subsequence of ICS2. */
6566 static bool
6567 is_subseq (conversion *ics1, conversion *ics2)
6569 /* We can assume that a conversion of the same code
6570 between the same types indicates a subsequence since we only get
6571 here if the types we are converting from are the same. */
6573 while (ics1->kind == ck_rvalue
6574 || ics1->kind == ck_lvalue)
6575 ics1 = ics1->u.next;
6577 while (1)
6579 while (ics2->kind == ck_rvalue
6580 || ics2->kind == ck_lvalue)
6581 ics2 = ics2->u.next;
6583 if (ics2->kind == ck_user
6584 || ics2->kind == ck_ambig
6585 || ics2->kind == ck_identity)
6586 /* At this point, ICS1 cannot be a proper subsequence of
6587 ICS2. We can get a USER_CONV when we are comparing the
6588 second standard conversion sequence of two user conversion
6589 sequences. */
6590 return false;
6592 ics2 = ics2->u.next;
6594 if (ics2->kind == ics1->kind
6595 && same_type_p (ics2->type, ics1->type)
6596 && same_type_p (ics2->u.next->type,
6597 ics1->u.next->type))
6598 return true;
6602 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
6603 be any _TYPE nodes. */
6605 bool
6606 is_properly_derived_from (tree derived, tree base)
6608 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
6609 return false;
6611 /* We only allow proper derivation here. The DERIVED_FROM_P macro
6612 considers every class derived from itself. */
6613 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
6614 && DERIVED_FROM_P (base, derived));
6617 /* We build the ICS for an implicit object parameter as a pointer
6618 conversion sequence. However, such a sequence should be compared
6619 as if it were a reference conversion sequence. If ICS is the
6620 implicit conversion sequence for an implicit object parameter,
6621 modify it accordingly. */
6623 static void
6624 maybe_handle_implicit_object (conversion **ics)
6626 if ((*ics)->this_p)
6628 /* [over.match.funcs]
6630 For non-static member functions, the type of the
6631 implicit object parameter is "reference to cv X"
6632 where X is the class of which the function is a
6633 member and cv is the cv-qualification on the member
6634 function declaration. */
6635 conversion *t = *ics;
6636 tree reference_type;
6638 /* The `this' parameter is a pointer to a class type. Make the
6639 implicit conversion talk about a reference to that same class
6640 type. */
6641 reference_type = TREE_TYPE (t->type);
6642 reference_type = build_reference_type (reference_type);
6644 if (t->kind == ck_qual)
6645 t = t->u.next;
6646 if (t->kind == ck_ptr)
6647 t = t->u.next;
6648 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
6649 t = direct_reference_binding (reference_type, t);
6650 t->this_p = 1;
6651 t->rvaluedness_matches_p = 0;
6652 *ics = t;
6656 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
6657 and return the initial reference binding conversion. Otherwise,
6658 leave *ICS unchanged and return NULL. */
6660 static conversion *
6661 maybe_handle_ref_bind (conversion **ics)
6663 if ((*ics)->kind == ck_ref_bind)
6665 conversion *old_ics = *ics;
6666 *ics = old_ics->u.next;
6667 (*ics)->user_conv_p = old_ics->user_conv_p;
6668 return old_ics;
6671 return NULL;
6674 /* Compare two implicit conversion sequences according to the rules set out in
6675 [over.ics.rank]. Return values:
6677 1: ics1 is better than ics2
6678 -1: ics2 is better than ics1
6679 0: ics1 and ics2 are indistinguishable */
6681 static int
6682 compare_ics (conversion *ics1, conversion *ics2)
6684 tree from_type1;
6685 tree from_type2;
6686 tree to_type1;
6687 tree to_type2;
6688 tree deref_from_type1 = NULL_TREE;
6689 tree deref_from_type2 = NULL_TREE;
6690 tree deref_to_type1 = NULL_TREE;
6691 tree deref_to_type2 = NULL_TREE;
6692 conversion_rank rank1, rank2;
6694 /* REF_BINDING is nonzero if the result of the conversion sequence
6695 is a reference type. In that case REF_CONV is the reference
6696 binding conversion. */
6697 conversion *ref_conv1;
6698 conversion *ref_conv2;
6700 /* Handle implicit object parameters. */
6701 maybe_handle_implicit_object (&ics1);
6702 maybe_handle_implicit_object (&ics2);
6704 /* Handle reference parameters. */
6705 ref_conv1 = maybe_handle_ref_bind (&ics1);
6706 ref_conv2 = maybe_handle_ref_bind (&ics2);
6708 /* List-initialization sequence L1 is a better conversion sequence than
6709 list-initialization sequence L2 if L1 converts to
6710 std::initializer_list<X> for some X and L2 does not. */
6711 if (ics1->kind == ck_list && ics2->kind != ck_list)
6712 return 1;
6713 if (ics2->kind == ck_list && ics1->kind != ck_list)
6714 return -1;
6716 /* [over.ics.rank]
6718 When comparing the basic forms of implicit conversion sequences (as
6719 defined in _over.best.ics_)
6721 --a standard conversion sequence (_over.ics.scs_) is a better
6722 conversion sequence than a user-defined conversion sequence
6723 or an ellipsis conversion sequence, and
6725 --a user-defined conversion sequence (_over.ics.user_) is a
6726 better conversion sequence than an ellipsis conversion sequence
6727 (_over.ics.ellipsis_). */
6728 rank1 = CONVERSION_RANK (ics1);
6729 rank2 = CONVERSION_RANK (ics2);
6731 if (rank1 > rank2)
6732 return -1;
6733 else if (rank1 < rank2)
6734 return 1;
6736 if (rank1 == cr_bad)
6738 /* XXX Isn't this an extension? */
6739 /* Both ICS are bad. We try to make a decision based on what
6740 would have happened if they'd been good. */
6741 if (ics1->user_conv_p > ics2->user_conv_p
6742 || ics1->rank > ics2->rank)
6743 return -1;
6744 else if (ics1->user_conv_p < ics2->user_conv_p
6745 || ics1->rank < ics2->rank)
6746 return 1;
6748 /* We couldn't make up our minds; try to figure it out below. */
6751 if (ics1->ellipsis_p || ics1->kind == ck_list)
6752 /* Both conversions are ellipsis conversions or both are building a
6753 std::initializer_list. */
6754 return 0;
6756 /* User-defined conversion sequence U1 is a better conversion sequence
6757 than another user-defined conversion sequence U2 if they contain the
6758 same user-defined conversion operator or constructor and if the sec-
6759 ond standard conversion sequence of U1 is better than the second
6760 standard conversion sequence of U2. */
6762 if (ics1->user_conv_p)
6764 conversion *t1;
6765 conversion *t2;
6767 for (t1 = ics1; t1->kind != ck_user; t1 = t1->u.next)
6768 if (t1->kind == ck_ambig || t1->kind == ck_aggr)
6769 return 0;
6770 for (t2 = ics2; t2->kind != ck_user; t2 = t2->u.next)
6771 if (t2->kind == ck_ambig || t2->kind == ck_aggr)
6772 return 0;
6774 if (t1->cand->fn != t2->cand->fn)
6775 return 0;
6777 /* We can just fall through here, after setting up
6778 FROM_TYPE1 and FROM_TYPE2. */
6779 from_type1 = t1->type;
6780 from_type2 = t2->type;
6782 else
6784 conversion *t1;
6785 conversion *t2;
6787 /* We're dealing with two standard conversion sequences.
6789 [over.ics.rank]
6791 Standard conversion sequence S1 is a better conversion
6792 sequence than standard conversion sequence S2 if
6794 --S1 is a proper subsequence of S2 (comparing the conversion
6795 sequences in the canonical form defined by _over.ics.scs_,
6796 excluding any Lvalue Transformation; the identity
6797 conversion sequence is considered to be a subsequence of
6798 any non-identity conversion sequence */
6800 t1 = ics1;
6801 while (t1->kind != ck_identity)
6802 t1 = t1->u.next;
6803 from_type1 = t1->type;
6805 t2 = ics2;
6806 while (t2->kind != ck_identity)
6807 t2 = t2->u.next;
6808 from_type2 = t2->type;
6811 /* One sequence can only be a subsequence of the other if they start with
6812 the same type. They can start with different types when comparing the
6813 second standard conversion sequence in two user-defined conversion
6814 sequences. */
6815 if (same_type_p (from_type1, from_type2))
6817 if (is_subseq (ics1, ics2))
6818 return 1;
6819 if (is_subseq (ics2, ics1))
6820 return -1;
6823 /* [over.ics.rank]
6825 Or, if not that,
6827 --the rank of S1 is better than the rank of S2 (by the rules
6828 defined below):
6830 Standard conversion sequences are ordered by their ranks: an Exact
6831 Match is a better conversion than a Promotion, which is a better
6832 conversion than a Conversion.
6834 Two conversion sequences with the same rank are indistinguishable
6835 unless one of the following rules applies:
6837 --A conversion that does not a convert a pointer, pointer to member,
6838 or std::nullptr_t to bool is better than one that does.
6840 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
6841 so that we do not have to check it explicitly. */
6842 if (ics1->rank < ics2->rank)
6843 return 1;
6844 else if (ics2->rank < ics1->rank)
6845 return -1;
6847 to_type1 = ics1->type;
6848 to_type2 = ics2->type;
6850 /* A conversion from scalar arithmetic type to complex is worse than a
6851 conversion between scalar arithmetic types. */
6852 if (same_type_p (from_type1, from_type2)
6853 && ARITHMETIC_TYPE_P (from_type1)
6854 && ARITHMETIC_TYPE_P (to_type1)
6855 && ARITHMETIC_TYPE_P (to_type2)
6856 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
6857 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
6859 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
6860 return -1;
6861 else
6862 return 1;
6865 if (TYPE_PTR_P (from_type1)
6866 && TYPE_PTR_P (from_type2)
6867 && TYPE_PTR_P (to_type1)
6868 && TYPE_PTR_P (to_type2))
6870 deref_from_type1 = TREE_TYPE (from_type1);
6871 deref_from_type2 = TREE_TYPE (from_type2);
6872 deref_to_type1 = TREE_TYPE (to_type1);
6873 deref_to_type2 = TREE_TYPE (to_type2);
6875 /* The rules for pointers to members A::* are just like the rules
6876 for pointers A*, except opposite: if B is derived from A then
6877 A::* converts to B::*, not vice versa. For that reason, we
6878 switch the from_ and to_ variables here. */
6879 else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
6880 && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
6881 || (TYPE_PTRMEMFUNC_P (from_type1)
6882 && TYPE_PTRMEMFUNC_P (from_type2)
6883 && TYPE_PTRMEMFUNC_P (to_type1)
6884 && TYPE_PTRMEMFUNC_P (to_type2)))
6886 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
6887 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
6888 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
6889 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
6892 if (deref_from_type1 != NULL_TREE
6893 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
6894 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
6896 /* This was one of the pointer or pointer-like conversions.
6898 [over.ics.rank]
6900 --If class B is derived directly or indirectly from class A,
6901 conversion of B* to A* is better than conversion of B* to
6902 void*, and conversion of A* to void* is better than
6903 conversion of B* to void*. */
6904 if (TREE_CODE (deref_to_type1) == VOID_TYPE
6905 && TREE_CODE (deref_to_type2) == VOID_TYPE)
6907 if (is_properly_derived_from (deref_from_type1,
6908 deref_from_type2))
6909 return -1;
6910 else if (is_properly_derived_from (deref_from_type2,
6911 deref_from_type1))
6912 return 1;
6914 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
6915 || TREE_CODE (deref_to_type2) == VOID_TYPE)
6917 if (same_type_p (deref_from_type1, deref_from_type2))
6919 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
6921 if (is_properly_derived_from (deref_from_type1,
6922 deref_to_type1))
6923 return 1;
6925 /* We know that DEREF_TO_TYPE1 is `void' here. */
6926 else if (is_properly_derived_from (deref_from_type1,
6927 deref_to_type2))
6928 return -1;
6931 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
6932 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
6934 /* [over.ics.rank]
6936 --If class B is derived directly or indirectly from class A
6937 and class C is derived directly or indirectly from B,
6939 --conversion of C* to B* is better than conversion of C* to
6942 --conversion of B* to A* is better than conversion of C* to
6943 A* */
6944 if (same_type_p (deref_from_type1, deref_from_type2))
6946 if (is_properly_derived_from (deref_to_type1,
6947 deref_to_type2))
6948 return 1;
6949 else if (is_properly_derived_from (deref_to_type2,
6950 deref_to_type1))
6951 return -1;
6953 else if (same_type_p (deref_to_type1, deref_to_type2))
6955 if (is_properly_derived_from (deref_from_type2,
6956 deref_from_type1))
6957 return 1;
6958 else if (is_properly_derived_from (deref_from_type1,
6959 deref_from_type2))
6960 return -1;
6964 else if (CLASS_TYPE_P (non_reference (from_type1))
6965 && same_type_p (from_type1, from_type2))
6967 tree from = non_reference (from_type1);
6969 /* [over.ics.rank]
6971 --binding of an expression of type C to a reference of type
6972 B& is better than binding an expression of type C to a
6973 reference of type A&
6975 --conversion of C to B is better than conversion of C to A, */
6976 if (is_properly_derived_from (from, to_type1)
6977 && is_properly_derived_from (from, to_type2))
6979 if (is_properly_derived_from (to_type1, to_type2))
6980 return 1;
6981 else if (is_properly_derived_from (to_type2, to_type1))
6982 return -1;
6985 else if (CLASS_TYPE_P (non_reference (to_type1))
6986 && same_type_p (to_type1, to_type2))
6988 tree to = non_reference (to_type1);
6990 /* [over.ics.rank]
6992 --binding of an expression of type B to a reference of type
6993 A& is better than binding an expression of type C to a
6994 reference of type A&,
6996 --conversion of B to A is better than conversion of C to A */
6997 if (is_properly_derived_from (from_type1, to)
6998 && is_properly_derived_from (from_type2, to))
7000 if (is_properly_derived_from (from_type2, from_type1))
7001 return 1;
7002 else if (is_properly_derived_from (from_type1, from_type2))
7003 return -1;
7007 /* [over.ics.rank]
7009 --S1 and S2 differ only in their qualification conversion and yield
7010 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
7011 qualification signature of type T1 is a proper subset of the cv-
7012 qualification signature of type T2 */
7013 if (ics1->kind == ck_qual
7014 && ics2->kind == ck_qual
7015 && same_type_p (from_type1, from_type2))
7017 int result = comp_cv_qual_signature (to_type1, to_type2);
7018 if (result != 0)
7019 return result;
7022 /* [over.ics.rank]
7024 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
7025 to an implicit object parameter, and either S1 binds an lvalue reference
7026 to an lvalue and S2 binds an rvalue reference or S1 binds an rvalue
7027 reference to an rvalue and S2 binds an lvalue reference
7028 (C++0x draft standard, 13.3.3.2)
7030 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
7031 types to which the references refer are the same type except for
7032 top-level cv-qualifiers, and the type to which the reference
7033 initialized by S2 refers is more cv-qualified than the type to
7034 which the reference initialized by S1 refers */
7036 if (ref_conv1 && ref_conv2)
7038 if (!ref_conv1->this_p && !ref_conv2->this_p
7039 && (TYPE_REF_IS_RVALUE (ref_conv1->type)
7040 != TYPE_REF_IS_RVALUE (ref_conv2->type)))
7042 if (ref_conv1->rvaluedness_matches_p)
7043 return 1;
7044 if (ref_conv2->rvaluedness_matches_p)
7045 return -1;
7048 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
7049 return comp_cv_qualification (TREE_TYPE (ref_conv2->type),
7050 TREE_TYPE (ref_conv1->type));
7053 /* Neither conversion sequence is better than the other. */
7054 return 0;
7057 /* The source type for this standard conversion sequence. */
7059 static tree
7060 source_type (conversion *t)
7062 for (;; t = t->u.next)
7064 if (t->kind == ck_user
7065 || t->kind == ck_ambig
7066 || t->kind == ck_identity)
7067 return t->type;
7069 gcc_unreachable ();
7072 /* Note a warning about preferring WINNER to LOSER. We do this by storing
7073 a pointer to LOSER and re-running joust to produce the warning if WINNER
7074 is actually used. */
7076 static void
7077 add_warning (struct z_candidate *winner, struct z_candidate *loser)
7079 candidate_warning *cw = (candidate_warning *)
7080 conversion_obstack_alloc (sizeof (candidate_warning));
7081 cw->loser = loser;
7082 cw->next = winner->warnings;
7083 winner->warnings = cw;
7086 /* Compare two candidates for overloading as described in
7087 [over.match.best]. Return values:
7089 1: cand1 is better than cand2
7090 -1: cand2 is better than cand1
7091 0: cand1 and cand2 are indistinguishable */
7093 static int
7094 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
7096 int winner = 0;
7097 int off1 = 0, off2 = 0;
7098 size_t i;
7099 size_t len;
7101 /* Candidates that involve bad conversions are always worse than those
7102 that don't. */
7103 if (cand1->viable > cand2->viable)
7104 return 1;
7105 if (cand1->viable < cand2->viable)
7106 return -1;
7108 /* If we have two pseudo-candidates for conversions to the same type,
7109 or two candidates for the same function, arbitrarily pick one. */
7110 if (cand1->fn == cand2->fn
7111 && (IS_TYPE_OR_DECL_P (cand1->fn)))
7112 return 1;
7114 /* a viable function F1
7115 is defined to be a better function than another viable function F2 if
7116 for all arguments i, ICSi(F1) is not a worse conversion sequence than
7117 ICSi(F2), and then */
7119 /* for some argument j, ICSj(F1) is a better conversion sequence than
7120 ICSj(F2) */
7122 /* For comparing static and non-static member functions, we ignore
7123 the implicit object parameter of the non-static function. The
7124 standard says to pretend that the static function has an object
7125 parm, but that won't work with operator overloading. */
7126 len = cand1->num_convs;
7127 if (len != cand2->num_convs)
7129 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
7130 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
7132 gcc_assert (static_1 != static_2);
7134 if (static_1)
7135 off2 = 1;
7136 else
7138 off1 = 1;
7139 --len;
7143 for (i = 0; i < len; ++i)
7145 conversion *t1 = cand1->convs[i + off1];
7146 conversion *t2 = cand2->convs[i + off2];
7147 int comp = compare_ics (t1, t2);
7149 if (comp != 0)
7151 if (warn_sign_promo
7152 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
7153 == cr_std + cr_promotion)
7154 && t1->kind == ck_std
7155 && t2->kind == ck_std
7156 && TREE_CODE (t1->type) == INTEGER_TYPE
7157 && TREE_CODE (t2->type) == INTEGER_TYPE
7158 && (TYPE_PRECISION (t1->type)
7159 == TYPE_PRECISION (t2->type))
7160 && (TYPE_UNSIGNED (t1->u.next->type)
7161 || (TREE_CODE (t1->u.next->type)
7162 == ENUMERAL_TYPE)))
7164 tree type = t1->u.next->type;
7165 tree type1, type2;
7166 struct z_candidate *w, *l;
7167 if (comp > 0)
7168 type1 = t1->type, type2 = t2->type,
7169 w = cand1, l = cand2;
7170 else
7171 type1 = t2->type, type2 = t1->type,
7172 w = cand2, l = cand1;
7174 if (warn)
7176 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
7177 type, type1, type2);
7178 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
7180 else
7181 add_warning (w, l);
7184 if (winner && comp != winner)
7186 winner = 0;
7187 goto tweak;
7189 winner = comp;
7193 /* warn about confusing overload resolution for user-defined conversions,
7194 either between a constructor and a conversion op, or between two
7195 conversion ops. */
7196 if (winner && warn_conversion && cand1->second_conv
7197 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
7198 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
7200 struct z_candidate *w, *l;
7201 bool give_warning = false;
7203 if (winner == 1)
7204 w = cand1, l = cand2;
7205 else
7206 w = cand2, l = cand1;
7208 /* We don't want to complain about `X::operator T1 ()'
7209 beating `X::operator T2 () const', when T2 is a no less
7210 cv-qualified version of T1. */
7211 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
7212 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
7214 tree t = TREE_TYPE (TREE_TYPE (l->fn));
7215 tree f = TREE_TYPE (TREE_TYPE (w->fn));
7217 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
7219 t = TREE_TYPE (t);
7220 f = TREE_TYPE (f);
7222 if (!comp_ptr_ttypes (t, f))
7223 give_warning = true;
7225 else
7226 give_warning = true;
7228 if (!give_warning)
7229 /*NOP*/;
7230 else if (warn)
7232 tree source = source_type (w->convs[0]);
7233 if (! DECL_CONSTRUCTOR_P (w->fn))
7234 source = TREE_TYPE (source);
7235 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
7236 && warning (OPT_Wconversion, " for conversion from %qT to %qT",
7237 source, w->second_conv->type))
7239 inform (input_location, " because conversion sequence for the argument is better");
7242 else
7243 add_warning (w, l);
7246 if (winner)
7247 return winner;
7249 /* or, if not that,
7250 F1 is a non-template function and F2 is a template function
7251 specialization. */
7253 if (!cand1->template_decl && cand2->template_decl)
7254 return 1;
7255 else if (cand1->template_decl && !cand2->template_decl)
7256 return -1;
7258 /* or, if not that,
7259 F1 and F2 are template functions and the function template for F1 is
7260 more specialized than the template for F2 according to the partial
7261 ordering rules. */
7263 if (cand1->template_decl && cand2->template_decl)
7265 winner = more_specialized_fn
7266 (TI_TEMPLATE (cand1->template_decl),
7267 TI_TEMPLATE (cand2->template_decl),
7268 /* [temp.func.order]: The presence of unused ellipsis and default
7269 arguments has no effect on the partial ordering of function
7270 templates. add_function_candidate() will not have
7271 counted the "this" argument for constructors. */
7272 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
7273 if (winner)
7274 return winner;
7277 /* or, if not that,
7278 the context is an initialization by user-defined conversion (see
7279 _dcl.init_ and _over.match.user_) and the standard conversion
7280 sequence from the return type of F1 to the destination type (i.e.,
7281 the type of the entity being initialized) is a better conversion
7282 sequence than the standard conversion sequence from the return type
7283 of F2 to the destination type. */
7285 if (cand1->second_conv)
7287 winner = compare_ics (cand1->second_conv, cand2->second_conv);
7288 if (winner)
7289 return winner;
7292 /* Check whether we can discard a builtin candidate, either because we
7293 have two identical ones or matching builtin and non-builtin candidates.
7295 (Pedantically in the latter case the builtin which matched the user
7296 function should not be added to the overload set, but we spot it here.
7298 [over.match.oper]
7299 ... the builtin candidates include ...
7300 - do not have the same parameter type list as any non-template
7301 non-member candidate. */
7303 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
7304 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
7306 for (i = 0; i < len; ++i)
7307 if (!same_type_p (cand1->convs[i]->type,
7308 cand2->convs[i]->type))
7309 break;
7310 if (i == cand1->num_convs)
7312 if (cand1->fn == cand2->fn)
7313 /* Two built-in candidates; arbitrarily pick one. */
7314 return 1;
7315 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
7316 /* cand1 is built-in; prefer cand2. */
7317 return -1;
7318 else
7319 /* cand2 is built-in; prefer cand1. */
7320 return 1;
7324 /* If the two function declarations represent the same function (this can
7325 happen with declarations in multiple scopes and arg-dependent lookup),
7326 arbitrarily choose one. But first make sure the default args we're
7327 using match. */
7328 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
7329 && equal_functions (cand1->fn, cand2->fn))
7331 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
7332 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
7334 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
7336 for (i = 0; i < len; ++i)
7338 /* Don't crash if the fn is variadic. */
7339 if (!parms1)
7340 break;
7341 parms1 = TREE_CHAIN (parms1);
7342 parms2 = TREE_CHAIN (parms2);
7345 if (off1)
7346 parms1 = TREE_CHAIN (parms1);
7347 else if (off2)
7348 parms2 = TREE_CHAIN (parms2);
7350 for (; parms1; ++i)
7352 if (!cp_tree_equal (TREE_PURPOSE (parms1),
7353 TREE_PURPOSE (parms2)))
7355 if (warn)
7357 permerror (input_location, "default argument mismatch in "
7358 "overload resolution");
7359 inform (input_location,
7360 " candidate 1: %q+#F", cand1->fn);
7361 inform (input_location,
7362 " candidate 2: %q+#F", cand2->fn);
7364 else
7365 add_warning (cand1, cand2);
7366 break;
7368 parms1 = TREE_CHAIN (parms1);
7369 parms2 = TREE_CHAIN (parms2);
7372 return 1;
7375 tweak:
7377 /* Extension: If the worst conversion for one candidate is worse than the
7378 worst conversion for the other, take the first. */
7379 if (!pedantic)
7381 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
7382 struct z_candidate *w = 0, *l = 0;
7384 for (i = 0; i < len; ++i)
7386 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
7387 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
7388 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
7389 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
7391 if (rank1 < rank2)
7392 winner = 1, w = cand1, l = cand2;
7393 if (rank1 > rank2)
7394 winner = -1, w = cand2, l = cand1;
7395 if (winner)
7397 if (warn)
7399 pedwarn (input_location, 0,
7400 "ISO C++ says that these are ambiguous, even "
7401 "though the worst conversion for the first is better than "
7402 "the worst conversion for the second:");
7403 print_z_candidate (_("candidate 1:"), w);
7404 print_z_candidate (_("candidate 2:"), l);
7406 else
7407 add_warning (w, l);
7408 return winner;
7412 gcc_assert (!winner);
7413 return 0;
7416 /* Given a list of candidates for overloading, find the best one, if any.
7417 This algorithm has a worst case of O(2n) (winner is last), and a best
7418 case of O(n/2) (totally ambiguous); much better than a sorting
7419 algorithm. */
7421 static struct z_candidate *
7422 tourney (struct z_candidate *candidates)
7424 struct z_candidate *champ = candidates, *challenger;
7425 int fate;
7426 int champ_compared_to_predecessor = 0;
7428 /* Walk through the list once, comparing each current champ to the next
7429 candidate, knocking out a candidate or two with each comparison. */
7431 for (challenger = champ->next; challenger; )
7433 fate = joust (champ, challenger, 0);
7434 if (fate == 1)
7435 challenger = challenger->next;
7436 else
7438 if (fate == 0)
7440 champ = challenger->next;
7441 if (champ == 0)
7442 return NULL;
7443 champ_compared_to_predecessor = 0;
7445 else
7447 champ = challenger;
7448 champ_compared_to_predecessor = 1;
7451 challenger = champ->next;
7455 /* Make sure the champ is better than all the candidates it hasn't yet
7456 been compared to. */
7458 for (challenger = candidates;
7459 challenger != champ
7460 && !(champ_compared_to_predecessor && challenger->next == champ);
7461 challenger = challenger->next)
7463 fate = joust (champ, challenger, 0);
7464 if (fate != 1)
7465 return NULL;
7468 return champ;
7471 /* Returns nonzero if things of type FROM can be converted to TO. */
7473 bool
7474 can_convert (tree to, tree from)
7476 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT);
7479 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
7481 bool
7482 can_convert_arg (tree to, tree from, tree arg, int flags)
7484 conversion *t;
7485 void *p;
7486 bool ok_p;
7488 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7489 p = conversion_obstack_alloc (0);
7491 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
7492 flags);
7493 ok_p = (t && !t->bad_p);
7495 /* Free all the conversions we allocated. */
7496 obstack_free (&conversion_obstack, p);
7498 return ok_p;
7501 /* Like can_convert_arg, but allows dubious conversions as well. */
7503 bool
7504 can_convert_arg_bad (tree to, tree from, tree arg, int flags)
7506 conversion *t;
7507 void *p;
7509 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7510 p = conversion_obstack_alloc (0);
7511 /* Try to perform the conversion. */
7512 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
7513 flags);
7514 /* Free all the conversions we allocated. */
7515 obstack_free (&conversion_obstack, p);
7517 return t != NULL;
7520 /* Convert EXPR to TYPE. Return the converted expression.
7522 Note that we allow bad conversions here because by the time we get to
7523 this point we are committed to doing the conversion. If we end up
7524 doing a bad conversion, convert_like will complain. */
7526 tree
7527 perform_implicit_conversion_flags (tree type, tree expr, tsubst_flags_t complain, int flags)
7529 conversion *conv;
7530 void *p;
7532 if (error_operand_p (expr))
7533 return error_mark_node;
7535 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7536 p = conversion_obstack_alloc (0);
7538 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
7539 /*c_cast_p=*/false,
7540 flags);
7542 if (!conv)
7544 if (complain & tf_error)
7546 /* If expr has unknown type, then it is an overloaded function.
7547 Call instantiate_type to get good error messages. */
7548 if (TREE_TYPE (expr) == unknown_type_node)
7549 instantiate_type (type, expr, complain);
7550 else if (invalid_nonstatic_memfn_p (expr, complain))
7551 /* We gave an error. */;
7552 else
7553 error ("could not convert %qE to %qT", expr, type);
7555 expr = error_mark_node;
7557 else if (processing_template_decl)
7559 /* In a template, we are only concerned about determining the
7560 type of non-dependent expressions, so we do not have to
7561 perform the actual conversion. */
7562 if (TREE_TYPE (expr) != type)
7563 expr = build_nop (type, expr);
7565 else
7566 expr = convert_like (conv, expr, complain);
7568 /* Free all the conversions we allocated. */
7569 obstack_free (&conversion_obstack, p);
7571 return expr;
7574 tree
7575 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
7577 return perform_implicit_conversion_flags (type, expr, complain, LOOKUP_IMPLICIT);
7580 /* Convert EXPR to TYPE (as a direct-initialization) if that is
7581 permitted. If the conversion is valid, the converted expression is
7582 returned. Otherwise, NULL_TREE is returned, except in the case
7583 that TYPE is a class type; in that case, an error is issued. If
7584 C_CAST_P is true, then this direction initialization is taking
7585 place as part of a static_cast being attempted as part of a C-style
7586 cast. */
7588 tree
7589 perform_direct_initialization_if_possible (tree type,
7590 tree expr,
7591 bool c_cast_p,
7592 tsubst_flags_t complain)
7594 conversion *conv;
7595 void *p;
7597 if (type == error_mark_node || error_operand_p (expr))
7598 return error_mark_node;
7599 /* [dcl.init]
7601 If the destination type is a (possibly cv-qualified) class type:
7603 -- If the initialization is direct-initialization ...,
7604 constructors are considered. ... If no constructor applies, or
7605 the overload resolution is ambiguous, the initialization is
7606 ill-formed. */
7607 if (CLASS_TYPE_P (type))
7609 VEC(tree,gc) *args = make_tree_vector_single (expr);
7610 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
7611 &args, type, LOOKUP_NORMAL, complain);
7612 release_tree_vector (args);
7613 return build_cplus_new (type, expr);
7616 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7617 p = conversion_obstack_alloc (0);
7619 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
7620 c_cast_p,
7621 LOOKUP_NORMAL);
7622 if (!conv || conv->bad_p)
7623 expr = NULL_TREE;
7624 else
7625 expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
7626 /*issue_conversion_warnings=*/false,
7627 c_cast_p,
7628 complain);
7630 /* Free all the conversions we allocated. */
7631 obstack_free (&conversion_obstack, p);
7633 return expr;
7636 /* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference
7637 is being bound to a temporary. Create and return a new VAR_DECL
7638 with the indicated TYPE; this variable will store the value to
7639 which the reference is bound. */
7641 tree
7642 make_temporary_var_for_ref_to_temp (tree decl, tree type)
7644 tree var;
7646 /* Create the variable. */
7647 var = create_temporary_var (type);
7649 /* Register the variable. */
7650 if (TREE_STATIC (decl))
7652 /* Namespace-scope or local static; give it a mangled name. */
7653 tree name;
7655 TREE_STATIC (var) = 1;
7656 name = mangle_ref_init_variable (decl);
7657 DECL_NAME (var) = name;
7658 SET_DECL_ASSEMBLER_NAME (var, name);
7659 var = pushdecl_top_level (var);
7661 else
7662 /* Create a new cleanup level if necessary. */
7663 maybe_push_cleanup_level (type);
7665 return var;
7668 /* EXPR is the initializer for a variable DECL of reference or
7669 std::initializer_list type. Create, push and return a new VAR_DECL
7670 for the initializer so that it will live as long as DECL. Any
7671 cleanup for the new variable is returned through CLEANUP, and the
7672 code to initialize the new variable is returned through INITP. */
7674 tree
7675 set_up_extended_ref_temp (tree decl, tree expr, tree *cleanup, tree *initp)
7677 tree init;
7678 tree type;
7679 tree var;
7681 /* Create the temporary variable. */
7682 type = TREE_TYPE (expr);
7683 var = make_temporary_var_for_ref_to_temp (decl, type);
7684 layout_decl (var, 0);
7685 /* If the rvalue is the result of a function call it will be
7686 a TARGET_EXPR. If it is some other construct (such as a
7687 member access expression where the underlying object is
7688 itself the result of a function call), turn it into a
7689 TARGET_EXPR here. It is important that EXPR be a
7690 TARGET_EXPR below since otherwise the INIT_EXPR will
7691 attempt to make a bitwise copy of EXPR to initialize
7692 VAR. */
7693 if (TREE_CODE (expr) != TARGET_EXPR)
7694 expr = get_target_expr (expr);
7695 /* Create the INIT_EXPR that will initialize the temporary
7696 variable. */
7697 init = build2 (INIT_EXPR, type, var, expr);
7698 if (at_function_scope_p ())
7700 add_decl_expr (var);
7702 if (TREE_STATIC (var))
7703 init = add_stmt_to_compound (init, register_dtor_fn (var));
7704 else
7705 *cleanup = cxx_maybe_build_cleanup (var);
7707 /* We must be careful to destroy the temporary only
7708 after its initialization has taken place. If the
7709 initialization throws an exception, then the
7710 destructor should not be run. We cannot simply
7711 transform INIT into something like:
7713 (INIT, ({ CLEANUP_STMT; }))
7715 because emit_local_var always treats the
7716 initializer as a full-expression. Thus, the
7717 destructor would run too early; it would run at the
7718 end of initializing the reference variable, rather
7719 than at the end of the block enclosing the
7720 reference variable.
7722 The solution is to pass back a cleanup expression
7723 which the caller is responsible for attaching to
7724 the statement tree. */
7726 else
7728 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
7729 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7730 static_aggregates = tree_cons (NULL_TREE, var,
7731 static_aggregates);
7734 *initp = init;
7735 return var;
7738 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
7739 initializing a variable of that TYPE. If DECL is non-NULL, it is
7740 the VAR_DECL being initialized with the EXPR. (In that case, the
7741 type of DECL will be TYPE.) If DECL is non-NULL, then CLEANUP must
7742 also be non-NULL, and with *CLEANUP initialized to NULL. Upon
7743 return, if *CLEANUP is no longer NULL, it will be an expression
7744 that should be pushed as a cleanup after the returned expression
7745 is used to initialize DECL.
7747 Return the converted expression. */
7749 tree
7750 initialize_reference (tree type, tree expr, tree decl, tree *cleanup,
7751 tsubst_flags_t complain)
7753 conversion *conv;
7754 void *p;
7756 if (type == error_mark_node || error_operand_p (expr))
7757 return error_mark_node;
7759 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7760 p = conversion_obstack_alloc (0);
7762 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
7763 LOOKUP_NORMAL);
7764 if (!conv || conv->bad_p)
7766 if (complain & tf_error)
7768 if (!CP_TYPE_CONST_P (TREE_TYPE (type))
7769 && !TYPE_REF_IS_RVALUE (type)
7770 && !real_lvalue_p (expr))
7771 error ("invalid initialization of non-const reference of "
7772 "type %qT from an rvalue of type %qT",
7773 type, TREE_TYPE (expr));
7774 else
7775 error ("invalid initialization of reference of type "
7776 "%qT from expression of type %qT", type,
7777 TREE_TYPE (expr));
7779 return error_mark_node;
7782 /* If DECL is non-NULL, then this special rule applies:
7784 [class.temporary]
7786 The temporary to which the reference is bound or the temporary
7787 that is the complete object to which the reference is bound
7788 persists for the lifetime of the reference.
7790 The temporaries created during the evaluation of the expression
7791 initializing the reference, except the temporary to which the
7792 reference is bound, are destroyed at the end of the
7793 full-expression in which they are created.
7795 In that case, we store the converted expression into a new
7796 VAR_DECL in a new scope.
7798 However, we want to be careful not to create temporaries when
7799 they are not required. For example, given:
7801 struct B {};
7802 struct D : public B {};
7803 D f();
7804 const B& b = f();
7806 there is no need to copy the return value from "f"; we can just
7807 extend its lifetime. Similarly, given:
7809 struct S {};
7810 struct T { operator S(); };
7811 T t;
7812 const S& s = t;
7814 we can extend the lifetime of the return value of the conversion
7815 operator. */
7816 gcc_assert (conv->kind == ck_ref_bind);
7817 if (decl)
7819 tree var;
7820 tree base_conv_type;
7822 /* Skip over the REF_BIND. */
7823 conv = conv->u.next;
7824 /* If the next conversion is a BASE_CONV, skip that too -- but
7825 remember that the conversion was required. */
7826 if (conv->kind == ck_base)
7828 base_conv_type = conv->type;
7829 conv = conv->u.next;
7831 else
7832 base_conv_type = NULL_TREE;
7833 /* Perform the remainder of the conversion. */
7834 expr = convert_like_real (conv, expr,
7835 /*fn=*/NULL_TREE, /*argnum=*/0,
7836 /*inner=*/-1,
7837 /*issue_conversion_warnings=*/true,
7838 /*c_cast_p=*/false,
7839 tf_warning_or_error);
7840 if (error_operand_p (expr))
7841 expr = error_mark_node;
7842 else
7844 if (!lvalue_or_rvalue_with_address_p (expr))
7846 tree init;
7847 var = set_up_extended_ref_temp (decl, expr, cleanup, &init);
7848 /* Use its address to initialize the reference variable. */
7849 expr = build_address (var);
7850 if (base_conv_type)
7851 expr = convert_to_base (expr,
7852 build_pointer_type (base_conv_type),
7853 /*check_access=*/true,
7854 /*nonnull=*/true, complain);
7855 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
7857 else
7858 /* Take the address of EXPR. */
7859 expr = cp_build_unary_op (ADDR_EXPR, expr, 0, tf_warning_or_error);
7860 /* If a BASE_CONV was required, perform it now. */
7861 if (base_conv_type)
7862 expr = (perform_implicit_conversion
7863 (build_pointer_type (base_conv_type), expr,
7864 tf_warning_or_error));
7865 expr = build_nop (type, expr);
7868 else
7869 /* Perform the conversion. */
7870 expr = convert_like (conv, expr, tf_warning_or_error);
7872 /* Free all the conversions we allocated. */
7873 obstack_free (&conversion_obstack, p);
7875 return expr;
7878 /* Returns true iff TYPE is some variant of std::initializer_list. */
7880 bool
7881 is_std_init_list (tree type)
7883 return (CLASS_TYPE_P (type)
7884 && CP_TYPE_CONTEXT (type) == std_node
7885 && strcmp (TYPE_NAME_STRING (type), "initializer_list") == 0);
7888 /* Returns true iff DECL is a list constructor: i.e. a constructor which
7889 will accept an argument list of a single std::initializer_list<T>. */
7891 bool
7892 is_list_ctor (tree decl)
7894 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
7895 tree arg;
7897 if (!args || args == void_list_node)
7898 return false;
7900 arg = non_reference (TREE_VALUE (args));
7901 if (!is_std_init_list (arg))
7902 return false;
7904 args = TREE_CHAIN (args);
7906 if (args && args != void_list_node && !TREE_PURPOSE (args))
7907 /* There are more non-defaulted parms. */
7908 return false;
7910 return true;
7913 #include "gt-cp-call.h"