Daily bump.
[official-gcc.git] / gcc / cp / call.c
blobe002d0180df91598e184bc0636f38bbbfd9b7958
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* High-level class interface. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "tree.h"
30 #include "stor-layout.h"
31 #include "trans-mem.h"
32 #include "stringpool.h"
33 #include "cp-tree.h"
34 #include "flags.h"
35 #include "toplev.h"
36 #include "diagnostic-core.h"
37 #include "intl.h"
38 #include "target.h"
39 #include "convert.h"
40 #include "langhooks.h"
41 #include "c-family/c-objc.h"
42 #include "timevar.h"
43 #include "cgraph.h"
44 #include "wide-int.h"
46 /* The various kinds of conversion. */
48 typedef enum conversion_kind {
49 ck_identity,
50 ck_lvalue,
51 ck_qual,
52 ck_std,
53 ck_ptr,
54 ck_pmem,
55 ck_base,
56 ck_ref_bind,
57 ck_user,
58 ck_ambig,
59 ck_list,
60 ck_aggr,
61 ck_rvalue
62 } conversion_kind;
64 /* The rank of the conversion. Order of the enumerals matters; better
65 conversions should come earlier in the list. */
67 typedef enum conversion_rank {
68 cr_identity,
69 cr_exact,
70 cr_promotion,
71 cr_std,
72 cr_pbool,
73 cr_user,
74 cr_ellipsis,
75 cr_bad
76 } conversion_rank;
78 /* An implicit conversion sequence, in the sense of [over.best.ics].
79 The first conversion to be performed is at the end of the chain.
80 That conversion is always a cr_identity conversion. */
82 typedef struct conversion conversion;
83 struct conversion {
84 /* The kind of conversion represented by this step. */
85 conversion_kind kind;
86 /* The rank of this conversion. */
87 conversion_rank rank;
88 BOOL_BITFIELD user_conv_p : 1;
89 BOOL_BITFIELD ellipsis_p : 1;
90 BOOL_BITFIELD this_p : 1;
91 /* True if this conversion would be permitted with a bending of
92 language standards, e.g. disregarding pointer qualifiers or
93 converting integers to pointers. */
94 BOOL_BITFIELD bad_p : 1;
95 /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
96 temporary should be created to hold the result of the
97 conversion. */
98 BOOL_BITFIELD need_temporary_p : 1;
99 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
100 from a pointer-to-derived to pointer-to-base is being performed. */
101 BOOL_BITFIELD base_p : 1;
102 /* If KIND is ck_ref_bind, true when either an lvalue reference is
103 being bound to an lvalue expression or an rvalue reference is
104 being bound to an rvalue expression. If KIND is ck_rvalue,
105 true when we should treat an lvalue as an rvalue (12.8p33). If
106 KIND is ck_base, always false. */
107 BOOL_BITFIELD rvaluedness_matches_p: 1;
108 BOOL_BITFIELD check_narrowing: 1;
109 /* The type of the expression resulting from the conversion. */
110 tree type;
111 union {
112 /* The next conversion in the chain. Since the conversions are
113 arranged from outermost to innermost, the NEXT conversion will
114 actually be performed before this conversion. This variant is
115 used only when KIND is neither ck_identity, ck_ambig nor
116 ck_list. Please use the next_conversion function instead
117 of using this field directly. */
118 conversion *next;
119 /* The expression at the beginning of the conversion chain. This
120 variant is used only if KIND is ck_identity or ck_ambig. */
121 tree expr;
122 /* The array of conversions for an initializer_list, so this
123 variant is used only when KIN D is ck_list. */
124 conversion **list;
125 } u;
126 /* The function candidate corresponding to this conversion
127 sequence. This field is only used if KIND is ck_user. */
128 struct z_candidate *cand;
131 #define CONVERSION_RANK(NODE) \
132 ((NODE)->bad_p ? cr_bad \
133 : (NODE)->ellipsis_p ? cr_ellipsis \
134 : (NODE)->user_conv_p ? cr_user \
135 : (NODE)->rank)
137 #define BAD_CONVERSION_RANK(NODE) \
138 ((NODE)->ellipsis_p ? cr_ellipsis \
139 : (NODE)->user_conv_p ? cr_user \
140 : (NODE)->rank)
142 static struct obstack conversion_obstack;
143 static bool conversion_obstack_initialized;
144 struct rejection_reason;
146 static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
147 static int equal_functions (tree, tree);
148 static int joust (struct z_candidate *, struct z_candidate *, bool,
149 tsubst_flags_t);
150 static int compare_ics (conversion *, conversion *);
151 static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
152 static tree build_java_interface_fn_ref (tree, tree);
153 #define convert_like(CONV, EXPR, COMPLAIN) \
154 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \
155 /*issue_conversion_warnings=*/true, \
156 /*c_cast_p=*/false, (COMPLAIN))
157 #define convert_like_with_context(CONV, EXPR, FN, ARGNO, COMPLAIN ) \
158 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
159 /*issue_conversion_warnings=*/true, \
160 /*c_cast_p=*/false, (COMPLAIN))
161 static tree convert_like_real (conversion *, tree, tree, int, int, bool,
162 bool, tsubst_flags_t);
163 static void op_error (location_t, enum tree_code, enum tree_code, tree,
164 tree, tree, bool);
165 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
166 tsubst_flags_t);
167 static void print_z_candidate (location_t, const char *, struct z_candidate *);
168 static void print_z_candidates (location_t, struct z_candidate *);
169 static tree build_this (tree);
170 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
171 static bool any_strictly_viable (struct z_candidate *);
172 static struct z_candidate *add_template_candidate
173 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
174 tree, tree, tree, int, unification_kind_t, tsubst_flags_t);
175 static struct z_candidate *add_template_candidate_real
176 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
177 tree, tree, tree, int, tree, unification_kind_t, tsubst_flags_t);
178 static struct z_candidate *add_template_conv_candidate
179 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *,
180 tree, tree, tree, tsubst_flags_t);
181 static void add_builtin_candidates
182 (struct z_candidate **, enum tree_code, enum tree_code,
183 tree, tree *, int, tsubst_flags_t);
184 static void add_builtin_candidate
185 (struct z_candidate **, enum tree_code, enum tree_code,
186 tree, tree, tree, tree *, tree *, int, tsubst_flags_t);
187 static bool is_complete (tree);
188 static void build_builtin_candidate
189 (struct z_candidate **, tree, tree, tree, tree *, tree *,
190 int, tsubst_flags_t);
191 static struct z_candidate *add_conv_candidate
192 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
193 tree, tsubst_flags_t);
194 static struct z_candidate *add_function_candidate
195 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
196 tree, int, tsubst_flags_t);
197 static conversion *implicit_conversion (tree, tree, tree, bool, int,
198 tsubst_flags_t);
199 static conversion *standard_conversion (tree, tree, tree, bool, int);
200 static conversion *reference_binding (tree, tree, tree, bool, int,
201 tsubst_flags_t);
202 static conversion *build_conv (conversion_kind, tree, conversion *);
203 static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
204 static conversion *next_conversion (conversion *);
205 static bool is_subseq (conversion *, conversion *);
206 static conversion *maybe_handle_ref_bind (conversion **);
207 static void maybe_handle_implicit_object (conversion **);
208 static struct z_candidate *add_candidate
209 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
210 conversion **, tree, tree, int, struct rejection_reason *, int);
211 static tree source_type (conversion *);
212 static void add_warning (struct z_candidate *, struct z_candidate *);
213 static bool reference_compatible_p (tree, tree);
214 static conversion *direct_reference_binding (tree, conversion *);
215 static bool promoted_arithmetic_type_p (tree);
216 static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
217 static char *name_as_c_string (tree, tree, bool *);
218 static tree prep_operand (tree);
219 static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
220 bool, tree, tree, int, struct z_candidate **,
221 tsubst_flags_t);
222 static conversion *merge_conversion_sequences (conversion *, conversion *);
223 static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
225 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
226 NAME can take many forms... */
228 bool
229 check_dtor_name (tree basetype, tree name)
231 /* Just accept something we've already complained about. */
232 if (name == error_mark_node)
233 return true;
235 if (TREE_CODE (name) == TYPE_DECL)
236 name = TREE_TYPE (name);
237 else if (TYPE_P (name))
238 /* OK */;
239 else if (identifier_p (name))
241 if ((MAYBE_CLASS_TYPE_P (basetype)
242 && name == constructor_name (basetype))
243 || (TREE_CODE (basetype) == ENUMERAL_TYPE
244 && name == TYPE_IDENTIFIER (basetype)))
245 return true;
246 else
247 name = get_type_value (name);
249 else
251 /* In the case of:
253 template <class T> struct S { ~S(); };
254 int i;
255 i.~S();
257 NAME will be a class template. */
258 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
259 return false;
262 if (!name || name == error_mark_node)
263 return false;
264 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
267 /* We want the address of a function or method. We avoid creating a
268 pointer-to-member function. */
270 tree
271 build_addr_func (tree function, tsubst_flags_t complain)
273 tree type = TREE_TYPE (function);
275 /* We have to do these by hand to avoid real pointer to member
276 functions. */
277 if (TREE_CODE (type) == METHOD_TYPE)
279 if (TREE_CODE (function) == OFFSET_REF)
281 tree object = build_address (TREE_OPERAND (function, 0));
282 return get_member_function_from_ptrfunc (&object,
283 TREE_OPERAND (function, 1),
284 complain);
286 function = build_address (function);
288 else
289 function = decay_conversion (function, complain);
291 return function;
294 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
295 POINTER_TYPE to those. Note, pointer to member function types
296 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
297 two variants. build_call_a is the primitive taking an array of
298 arguments, while build_call_n is a wrapper that handles varargs. */
300 tree
301 build_call_n (tree function, int n, ...)
303 if (n == 0)
304 return build_call_a (function, 0, NULL);
305 else
307 tree *argarray = XALLOCAVEC (tree, n);
308 va_list ap;
309 int i;
311 va_start (ap, n);
312 for (i = 0; i < n; i++)
313 argarray[i] = va_arg (ap, tree);
314 va_end (ap);
315 return build_call_a (function, n, argarray);
319 /* Update various flags in cfun and the call itself based on what is being
320 called. Split out of build_call_a so that bot_manip can use it too. */
322 void
323 set_flags_from_callee (tree call)
325 int nothrow;
326 tree decl = get_callee_fndecl (call);
328 /* We check both the decl and the type; a function may be known not to
329 throw without being declared throw(). */
330 nothrow = ((decl && TREE_NOTHROW (decl))
331 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (call)))));
333 if (!nothrow && at_function_scope_p () && cfun && cp_function_chain)
334 cp_function_chain->can_throw = 1;
336 if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
337 current_function_returns_abnormally = 1;
339 TREE_NOTHROW (call) = nothrow;
342 tree
343 build_call_a (tree function, int n, tree *argarray)
345 tree decl;
346 tree result_type;
347 tree fntype;
348 int i;
350 function = build_addr_func (function, tf_warning_or_error);
352 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
353 fntype = TREE_TYPE (TREE_TYPE (function));
354 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
355 || TREE_CODE (fntype) == METHOD_TYPE);
356 result_type = TREE_TYPE (fntype);
357 /* An rvalue has no cv-qualifiers. */
358 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
359 result_type = cv_unqualified (result_type);
361 function = build_call_array_loc (input_location,
362 result_type, function, n, argarray);
363 set_flags_from_callee (function);
365 decl = get_callee_fndecl (function);
367 if (decl && !TREE_USED (decl))
369 /* We invoke build_call directly for several library
370 functions. These may have been declared normally if
371 we're building libgcc, so we can't just check
372 DECL_ARTIFICIAL. */
373 gcc_assert (DECL_ARTIFICIAL (decl)
374 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
375 "__", 2));
376 mark_used (decl);
379 if (decl && TREE_DEPRECATED (decl))
380 warn_deprecated_use (decl, NULL_TREE);
381 require_complete_eh_spec_types (fntype, decl);
383 TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
385 /* Don't pass empty class objects by value. This is useful
386 for tags in STL, which are used to control overload resolution.
387 We don't need to handle other cases of copying empty classes. */
388 if (! decl || ! DECL_BUILT_IN (decl))
389 for (i = 0; i < n; i++)
391 tree arg = CALL_EXPR_ARG (function, i);
392 if (is_empty_class (TREE_TYPE (arg))
393 && ! TREE_ADDRESSABLE (TREE_TYPE (arg)))
395 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
396 arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
397 CALL_EXPR_ARG (function, i) = arg;
401 return function;
404 /* Build something of the form ptr->method (args)
405 or object.method (args). This can also build
406 calls to constructors, and find friends.
408 Member functions always take their class variable
409 as a pointer.
411 INSTANCE is a class instance.
413 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
415 PARMS help to figure out what that NAME really refers to.
417 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
418 down to the real instance type to use for access checking. We need this
419 information to get protected accesses correct.
421 FLAGS is the logical disjunction of zero or more LOOKUP_
422 flags. See cp-tree.h for more info.
424 If this is all OK, calls build_function_call with the resolved
425 member function.
427 This function must also handle being called to perform
428 initialization, promotion/coercion of arguments, and
429 instantiation of default parameters.
431 Note that NAME may refer to an instance variable name. If
432 `operator()()' is defined for the type of that field, then we return
433 that result. */
435 /* New overloading code. */
437 typedef struct z_candidate z_candidate;
439 typedef struct candidate_warning candidate_warning;
440 struct candidate_warning {
441 z_candidate *loser;
442 candidate_warning *next;
445 /* Information for providing diagnostics about why overloading failed. */
447 enum rejection_reason_code {
448 rr_none,
449 rr_arity,
450 rr_explicit_conversion,
451 rr_template_conversion,
452 rr_arg_conversion,
453 rr_bad_arg_conversion,
454 rr_template_unification,
455 rr_invalid_copy
458 struct conversion_info {
459 /* The index of the argument, 0-based. */
460 int n_arg;
461 /* The actual argument or its type. */
462 tree from;
463 /* The type of the parameter. */
464 tree to_type;
467 struct rejection_reason {
468 enum rejection_reason_code code;
469 union {
470 /* Information about an arity mismatch. */
471 struct {
472 /* The expected number of arguments. */
473 int expected;
474 /* The actual number of arguments in the call. */
475 int actual;
476 /* Whether the call was a varargs call. */
477 bool call_varargs_p;
478 } arity;
479 /* Information about an argument conversion mismatch. */
480 struct conversion_info conversion;
481 /* Same, but for bad argument conversions. */
482 struct conversion_info bad_conversion;
483 /* Information about template unification failures. These are the
484 parameters passed to fn_type_unification. */
485 struct {
486 tree tmpl;
487 tree explicit_targs;
488 int num_targs;
489 const tree *args;
490 unsigned int nargs;
491 tree return_type;
492 unification_kind_t strict;
493 int flags;
494 } template_unification;
495 /* Information about template instantiation failures. These are the
496 parameters passed to instantiate_template. */
497 struct {
498 tree tmpl;
499 tree targs;
500 } template_instantiation;
501 } u;
504 struct z_candidate {
505 /* The FUNCTION_DECL that will be called if this candidate is
506 selected by overload resolution. */
507 tree fn;
508 /* If not NULL_TREE, the first argument to use when calling this
509 function. */
510 tree first_arg;
511 /* The rest of the arguments to use when calling this function. If
512 there are no further arguments this may be NULL or it may be an
513 empty vector. */
514 const vec<tree, va_gc> *args;
515 /* The implicit conversion sequences for each of the arguments to
516 FN. */
517 conversion **convs;
518 /* The number of implicit conversion sequences. */
519 size_t num_convs;
520 /* If FN is a user-defined conversion, the standard conversion
521 sequence from the type returned by FN to the desired destination
522 type. */
523 conversion *second_conv;
524 struct rejection_reason *reason;
525 /* If FN is a member function, the binfo indicating the path used to
526 qualify the name of FN at the call site. This path is used to
527 determine whether or not FN is accessible if it is selected by
528 overload resolution. The DECL_CONTEXT of FN will always be a
529 (possibly improper) base of this binfo. */
530 tree access_path;
531 /* If FN is a non-static member function, the binfo indicating the
532 subobject to which the `this' pointer should be converted if FN
533 is selected by overload resolution. The type pointed to by
534 the `this' pointer must correspond to the most derived class
535 indicated by the CONVERSION_PATH. */
536 tree conversion_path;
537 tree template_decl;
538 tree explicit_targs;
539 candidate_warning *warnings;
540 z_candidate *next;
541 int viable;
543 /* The flags active in add_candidate. */
544 int flags;
547 /* Returns true iff T is a null pointer constant in the sense of
548 [conv.ptr]. */
550 bool
551 null_ptr_cst_p (tree t)
553 /* [conv.ptr]
555 A null pointer constant is an integral constant expression
556 (_expr.const_) rvalue of integer type that evaluates to zero or
557 an rvalue of type std::nullptr_t. */
558 if (NULLPTR_TYPE_P (TREE_TYPE (t)))
559 return true;
560 if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)))
562 /* Core issue 903 says only literal 0 is a null pointer constant. */
563 if (cxx_dialect < cxx11)
564 t = maybe_constant_value (fold_non_dependent_expr_sfinae (t, tf_none));
565 STRIP_NOPS (t);
566 if (integer_zerop (t) && !TREE_OVERFLOW (t))
567 return true;
569 return false;
572 /* Returns true iff T is a null member pointer value (4.11). */
574 bool
575 null_member_pointer_value_p (tree t)
577 tree type = TREE_TYPE (t);
578 if (!type)
579 return false;
580 else if (TYPE_PTRMEMFUNC_P (type))
581 return (TREE_CODE (t) == CONSTRUCTOR
582 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
583 else if (TYPE_PTRDATAMEM_P (type))
584 return integer_all_onesp (t);
585 else
586 return false;
589 /* Returns nonzero if PARMLIST consists of only default parms,
590 ellipsis, and/or undeduced parameter packs. */
592 bool
593 sufficient_parms_p (const_tree parmlist)
595 for (; parmlist && parmlist != void_list_node;
596 parmlist = TREE_CHAIN (parmlist))
597 if (!TREE_PURPOSE (parmlist)
598 && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
599 return false;
600 return true;
603 /* Allocate N bytes of memory from the conversion obstack. The memory
604 is zeroed before being returned. */
606 static void *
607 conversion_obstack_alloc (size_t n)
609 void *p;
610 if (!conversion_obstack_initialized)
612 gcc_obstack_init (&conversion_obstack);
613 conversion_obstack_initialized = true;
615 p = obstack_alloc (&conversion_obstack, n);
616 memset (p, 0, n);
617 return p;
620 /* Allocate rejection reasons. */
622 static struct rejection_reason *
623 alloc_rejection (enum rejection_reason_code code)
625 struct rejection_reason *p;
626 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
627 p->code = code;
628 return p;
631 static struct rejection_reason *
632 arity_rejection (tree first_arg, int expected, int actual)
634 struct rejection_reason *r = alloc_rejection (rr_arity);
635 int adjust = first_arg != NULL_TREE;
636 r->u.arity.expected = expected - adjust;
637 r->u.arity.actual = actual - adjust;
638 return r;
641 static struct rejection_reason *
642 arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
644 struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
645 int adjust = first_arg != NULL_TREE;
646 r->u.conversion.n_arg = n_arg - adjust;
647 r->u.conversion.from = from;
648 r->u.conversion.to_type = to;
649 return r;
652 static struct rejection_reason *
653 bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
655 struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
656 int adjust = first_arg != NULL_TREE;
657 r->u.bad_conversion.n_arg = n_arg - adjust;
658 r->u.bad_conversion.from = from;
659 r->u.bad_conversion.to_type = to;
660 return r;
663 static struct rejection_reason *
664 explicit_conversion_rejection (tree from, tree to)
666 struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
667 r->u.conversion.n_arg = 0;
668 r->u.conversion.from = from;
669 r->u.conversion.to_type = to;
670 return r;
673 static struct rejection_reason *
674 template_conversion_rejection (tree from, tree to)
676 struct rejection_reason *r = alloc_rejection (rr_template_conversion);
677 r->u.conversion.n_arg = 0;
678 r->u.conversion.from = from;
679 r->u.conversion.to_type = to;
680 return r;
683 static struct rejection_reason *
684 template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
685 const tree *args, unsigned int nargs,
686 tree return_type, unification_kind_t strict,
687 int flags)
689 size_t args_n_bytes = sizeof (*args) * nargs;
690 tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
691 struct rejection_reason *r = alloc_rejection (rr_template_unification);
692 r->u.template_unification.tmpl = tmpl;
693 r->u.template_unification.explicit_targs = explicit_targs;
694 r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
695 /* Copy args to our own storage. */
696 memcpy (args1, args, args_n_bytes);
697 r->u.template_unification.args = args1;
698 r->u.template_unification.nargs = nargs;
699 r->u.template_unification.return_type = return_type;
700 r->u.template_unification.strict = strict;
701 r->u.template_unification.flags = flags;
702 return r;
705 static struct rejection_reason *
706 template_unification_error_rejection (void)
708 return alloc_rejection (rr_template_unification);
711 static struct rejection_reason *
712 invalid_copy_with_fn_template_rejection (void)
714 struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
715 return r;
718 /* Dynamically allocate a conversion. */
720 static conversion *
721 alloc_conversion (conversion_kind kind)
723 conversion *c;
724 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
725 c->kind = kind;
726 return c;
729 #ifdef ENABLE_CHECKING
731 /* Make sure that all memory on the conversion obstack has been
732 freed. */
734 void
735 validate_conversion_obstack (void)
737 if (conversion_obstack_initialized)
738 gcc_assert ((obstack_next_free (&conversion_obstack)
739 == obstack_base (&conversion_obstack)));
742 #endif /* ENABLE_CHECKING */
744 /* Dynamically allocate an array of N conversions. */
746 static conversion **
747 alloc_conversions (size_t n)
749 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
752 static conversion *
753 build_conv (conversion_kind code, tree type, conversion *from)
755 conversion *t;
756 conversion_rank rank = CONVERSION_RANK (from);
758 /* Note that the caller is responsible for filling in t->cand for
759 user-defined conversions. */
760 t = alloc_conversion (code);
761 t->type = type;
762 t->u.next = from;
764 switch (code)
766 case ck_ptr:
767 case ck_pmem:
768 case ck_base:
769 case ck_std:
770 if (rank < cr_std)
771 rank = cr_std;
772 break;
774 case ck_qual:
775 if (rank < cr_exact)
776 rank = cr_exact;
777 break;
779 default:
780 break;
782 t->rank = rank;
783 t->user_conv_p = (code == ck_user || from->user_conv_p);
784 t->bad_p = from->bad_p;
785 t->base_p = false;
786 return t;
789 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
790 specialization of std::initializer_list<T>, if such a conversion is
791 possible. */
793 static conversion *
794 build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
796 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
797 unsigned len = CONSTRUCTOR_NELTS (ctor);
798 conversion **subconvs = alloc_conversions (len);
799 conversion *t;
800 unsigned i;
801 tree val;
803 /* Within a list-initialization we can have more user-defined
804 conversions. */
805 flags &= ~LOOKUP_NO_CONVERSION;
806 /* But no narrowing conversions. */
807 flags |= LOOKUP_NO_NARROWING;
809 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
811 conversion *sub
812 = implicit_conversion (elttype, TREE_TYPE (val), val,
813 false, flags, complain);
814 if (sub == NULL)
815 return NULL;
817 subconvs[i] = sub;
820 t = alloc_conversion (ck_list);
821 t->type = type;
822 t->u.list = subconvs;
823 t->rank = cr_exact;
825 for (i = 0; i < len; ++i)
827 conversion *sub = subconvs[i];
828 if (sub->rank > t->rank)
829 t->rank = sub->rank;
830 if (sub->user_conv_p)
831 t->user_conv_p = true;
832 if (sub->bad_p)
833 t->bad_p = true;
836 return t;
839 /* Return the next conversion of the conversion chain (if applicable),
840 or NULL otherwise. Please use this function instead of directly
841 accessing fields of struct conversion. */
843 static conversion *
844 next_conversion (conversion *conv)
846 if (conv == NULL
847 || conv->kind == ck_identity
848 || conv->kind == ck_ambig
849 || conv->kind == ck_list)
850 return NULL;
851 return conv->u.next;
854 /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
855 is a valid aggregate initializer for array type ATYPE. */
857 static bool
858 can_convert_array (tree atype, tree ctor, int flags, tsubst_flags_t complain)
860 unsigned i;
861 tree elttype = TREE_TYPE (atype);
862 for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
864 tree val = CONSTRUCTOR_ELT (ctor, i)->value;
865 bool ok;
866 if (TREE_CODE (elttype) == ARRAY_TYPE
867 && TREE_CODE (val) == CONSTRUCTOR)
868 ok = can_convert_array (elttype, val, flags, complain);
869 else
870 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
871 complain);
872 if (!ok)
873 return false;
875 return true;
878 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
879 aggregate class, if such a conversion is possible. */
881 static conversion *
882 build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
884 unsigned HOST_WIDE_INT i = 0;
885 conversion *c;
886 tree field = next_initializable_field (TYPE_FIELDS (type));
887 tree empty_ctor = NULL_TREE;
889 ctor = reshape_init (type, ctor, tf_none);
890 if (ctor == error_mark_node)
891 return NULL;
893 /* The conversions within the init-list aren't affected by the enclosing
894 context; they're always simple copy-initialization. */
895 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
897 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
899 tree ftype = TREE_TYPE (field);
900 tree val;
901 bool ok;
903 if (i < CONSTRUCTOR_NELTS (ctor))
904 val = CONSTRUCTOR_ELT (ctor, i)->value;
905 else if (TREE_CODE (ftype) == REFERENCE_TYPE)
906 /* Value-initialization of reference is ill-formed. */
907 return NULL;
908 else
910 if (empty_ctor == NULL_TREE)
911 empty_ctor = build_constructor (init_list_type_node, NULL);
912 val = empty_ctor;
914 ++i;
916 if (TREE_CODE (ftype) == ARRAY_TYPE
917 && TREE_CODE (val) == CONSTRUCTOR)
918 ok = can_convert_array (ftype, val, flags, complain);
919 else
920 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
921 complain);
923 if (!ok)
924 return NULL;
926 if (TREE_CODE (type) == UNION_TYPE)
927 break;
930 if (i < CONSTRUCTOR_NELTS (ctor))
931 return NULL;
933 c = alloc_conversion (ck_aggr);
934 c->type = type;
935 c->rank = cr_exact;
936 c->user_conv_p = true;
937 c->check_narrowing = true;
938 c->u.next = NULL;
939 return c;
942 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
943 array type, if such a conversion is possible. */
945 static conversion *
946 build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
948 conversion *c;
949 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
950 tree elttype = TREE_TYPE (type);
951 unsigned i;
952 tree val;
953 bool bad = false;
954 bool user = false;
955 enum conversion_rank rank = cr_exact;
957 /* We might need to propagate the size from the element to the array. */
958 complete_type (type);
960 if (TYPE_DOMAIN (type)
961 && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
963 unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
964 if (alen < len)
965 return NULL;
968 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
970 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
972 conversion *sub
973 = implicit_conversion (elttype, TREE_TYPE (val), val,
974 false, flags, complain);
975 if (sub == NULL)
976 return NULL;
978 if (sub->rank > rank)
979 rank = sub->rank;
980 if (sub->user_conv_p)
981 user = true;
982 if (sub->bad_p)
983 bad = true;
986 c = alloc_conversion (ck_aggr);
987 c->type = type;
988 c->rank = rank;
989 c->user_conv_p = user;
990 c->bad_p = bad;
991 c->u.next = NULL;
992 return c;
995 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
996 complex type, if such a conversion is possible. */
998 static conversion *
999 build_complex_conv (tree type, tree ctor, int flags,
1000 tsubst_flags_t complain)
1002 conversion *c;
1003 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1004 tree elttype = TREE_TYPE (type);
1005 unsigned i;
1006 tree val;
1007 bool bad = false;
1008 bool user = false;
1009 enum conversion_rank rank = cr_exact;
1011 if (len != 2)
1012 return NULL;
1014 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1016 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
1018 conversion *sub
1019 = implicit_conversion (elttype, TREE_TYPE (val), val,
1020 false, flags, complain);
1021 if (sub == NULL)
1022 return NULL;
1024 if (sub->rank > rank)
1025 rank = sub->rank;
1026 if (sub->user_conv_p)
1027 user = true;
1028 if (sub->bad_p)
1029 bad = true;
1032 c = alloc_conversion (ck_aggr);
1033 c->type = type;
1034 c->rank = rank;
1035 c->user_conv_p = user;
1036 c->bad_p = bad;
1037 c->u.next = NULL;
1038 return c;
1041 /* Build a representation of the identity conversion from EXPR to
1042 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1044 static conversion *
1045 build_identity_conv (tree type, tree expr)
1047 conversion *c;
1049 c = alloc_conversion (ck_identity);
1050 c->type = type;
1051 c->u.expr = expr;
1053 return c;
1056 /* Converting from EXPR to TYPE was ambiguous in the sense that there
1057 were multiple user-defined conversions to accomplish the job.
1058 Build a conversion that indicates that ambiguity. */
1060 static conversion *
1061 build_ambiguous_conv (tree type, tree expr)
1063 conversion *c;
1065 c = alloc_conversion (ck_ambig);
1066 c->type = type;
1067 c->u.expr = expr;
1069 return c;
1072 tree
1073 strip_top_quals (tree t)
1075 if (TREE_CODE (t) == ARRAY_TYPE)
1076 return t;
1077 return cp_build_qualified_type (t, 0);
1080 /* Returns the standard conversion path (see [conv]) from type FROM to type
1081 TO, if any. For proper handling of null pointer constants, you must
1082 also pass the expression EXPR to convert from. If C_CAST_P is true,
1083 this conversion is coming from a C-style cast. */
1085 static conversion *
1086 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1087 int flags)
1089 enum tree_code fcode, tcode;
1090 conversion *conv;
1091 bool fromref = false;
1092 tree qualified_to;
1094 to = non_reference (to);
1095 if (TREE_CODE (from) == REFERENCE_TYPE)
1097 fromref = true;
1098 from = TREE_TYPE (from);
1100 qualified_to = to;
1101 to = strip_top_quals (to);
1102 from = strip_top_quals (from);
1104 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1105 && expr && type_unknown_p (expr))
1107 tsubst_flags_t tflags = tf_conv;
1108 expr = instantiate_type (to, expr, tflags);
1109 if (expr == error_mark_node)
1110 return NULL;
1111 from = TREE_TYPE (expr);
1114 fcode = TREE_CODE (from);
1115 tcode = TREE_CODE (to);
1117 conv = build_identity_conv (from, expr);
1118 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1120 from = type_decays_to (from);
1121 fcode = TREE_CODE (from);
1122 conv = build_conv (ck_lvalue, from, conv);
1124 else if (fromref || (expr && lvalue_p (expr)))
1126 if (expr)
1128 tree bitfield_type;
1129 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1130 if (bitfield_type)
1132 from = strip_top_quals (bitfield_type);
1133 fcode = TREE_CODE (from);
1136 conv = build_conv (ck_rvalue, from, conv);
1137 if (flags & LOOKUP_PREFER_RVALUE)
1138 conv->rvaluedness_matches_p = true;
1141 /* Allow conversion between `__complex__' data types. */
1142 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1144 /* The standard conversion sequence to convert FROM to TO is
1145 the standard conversion sequence to perform componentwise
1146 conversion. */
1147 conversion *part_conv = standard_conversion
1148 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags);
1150 if (part_conv)
1152 conv = build_conv (part_conv->kind, to, conv);
1153 conv->rank = part_conv->rank;
1155 else
1156 conv = NULL;
1158 return conv;
1161 if (same_type_p (from, to))
1163 if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1164 conv->type = qualified_to;
1165 return conv;
1168 /* [conv.ptr]
1169 A null pointer constant can be converted to a pointer type; ... A
1170 null pointer constant of integral type can be converted to an
1171 rvalue of type std::nullptr_t. */
1172 if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1173 || NULLPTR_TYPE_P (to))
1174 && expr && null_ptr_cst_p (expr))
1175 conv = build_conv (ck_std, to, conv);
1176 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1177 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1179 /* For backwards brain damage compatibility, allow interconversion of
1180 pointers and integers with a pedwarn. */
1181 conv = build_conv (ck_std, to, conv);
1182 conv->bad_p = true;
1184 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1186 /* For backwards brain damage compatibility, allow interconversion of
1187 enums and integers with a pedwarn. */
1188 conv = build_conv (ck_std, to, conv);
1189 conv->bad_p = true;
1191 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1192 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1194 tree to_pointee;
1195 tree from_pointee;
1197 if (tcode == POINTER_TYPE
1198 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
1199 TREE_TYPE (to)))
1201 else if (VOID_TYPE_P (TREE_TYPE (to))
1202 && !TYPE_PTRDATAMEM_P (from)
1203 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
1205 tree nfrom = TREE_TYPE (from);
1206 /* Don't try to apply restrict to void. */
1207 int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1208 from = build_pointer_type
1209 (cp_build_qualified_type (void_type_node, quals));
1210 conv = build_conv (ck_ptr, from, conv);
1212 else if (TYPE_PTRDATAMEM_P (from))
1214 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1215 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1217 if (DERIVED_FROM_P (fbase, tbase)
1218 && (same_type_ignoring_top_level_qualifiers_p
1219 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
1220 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
1222 from = build_ptrmem_type (tbase,
1223 TYPE_PTRMEM_POINTED_TO_TYPE (from));
1224 conv = build_conv (ck_pmem, from, conv);
1226 else if (!same_type_p (fbase, tbase))
1227 return NULL;
1229 else if (CLASS_TYPE_P (TREE_TYPE (from))
1230 && CLASS_TYPE_P (TREE_TYPE (to))
1231 /* [conv.ptr]
1233 An rvalue of type "pointer to cv D," where D is a
1234 class type, can be converted to an rvalue of type
1235 "pointer to cv B," where B is a base class (clause
1236 _class.derived_) of D. If B is an inaccessible
1237 (clause _class.access_) or ambiguous
1238 (_class.member.lookup_) base class of D, a program
1239 that necessitates this conversion is ill-formed.
1240 Therefore, we use DERIVED_FROM_P, and do not check
1241 access or uniqueness. */
1242 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
1244 from =
1245 cp_build_qualified_type (TREE_TYPE (to),
1246 cp_type_quals (TREE_TYPE (from)));
1247 from = build_pointer_type (from);
1248 conv = build_conv (ck_ptr, from, conv);
1249 conv->base_p = true;
1252 if (tcode == POINTER_TYPE)
1254 to_pointee = TREE_TYPE (to);
1255 from_pointee = TREE_TYPE (from);
1257 else
1259 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1260 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1263 if (same_type_p (from, to))
1264 /* OK */;
1265 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
1266 /* In a C-style cast, we ignore CV-qualification because we
1267 are allowed to perform a static_cast followed by a
1268 const_cast. */
1269 conv = build_conv (ck_qual, to, conv);
1270 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1271 conv = build_conv (ck_qual, to, conv);
1272 else if (expr && string_conv_p (to, expr, 0))
1273 /* converting from string constant to char *. */
1274 conv = build_conv (ck_qual, to, conv);
1275 /* Allow conversions among compatible ObjC pointer types (base
1276 conversions have been already handled above). */
1277 else if (c_dialect_objc ()
1278 && objc_compare_types (to, from, -4, NULL_TREE))
1279 conv = build_conv (ck_ptr, to, conv);
1280 else if (ptr_reasonably_similar (to_pointee, from_pointee))
1282 conv = build_conv (ck_ptr, to, conv);
1283 conv->bad_p = true;
1285 else
1286 return NULL;
1288 from = to;
1290 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1292 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1293 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1294 tree fbase = class_of_this_parm (fromfn);
1295 tree tbase = class_of_this_parm (tofn);
1297 if (!DERIVED_FROM_P (fbase, tbase)
1298 || !same_type_p (static_fn_type (fromfn),
1299 static_fn_type (tofn)))
1300 return NULL;
1302 from = build_memfn_type (fromfn,
1303 tbase,
1304 cp_type_quals (tbase),
1305 type_memfn_rqual (tofn));
1306 from = build_ptrmemfunc_type (build_pointer_type (from));
1307 conv = build_conv (ck_pmem, from, conv);
1308 conv->base_p = true;
1310 else if (tcode == BOOLEAN_TYPE)
1312 /* [conv.bool]
1314 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1315 to member type can be converted to a prvalue of type bool. ...
1316 For direct-initialization (8.5 [dcl.init]), a prvalue of type
1317 std::nullptr_t can be converted to a prvalue of type bool; */
1318 if (ARITHMETIC_TYPE_P (from)
1319 || UNSCOPED_ENUM_P (from)
1320 || fcode == POINTER_TYPE
1321 || TYPE_PTRMEM_P (from)
1322 || NULLPTR_TYPE_P (from))
1324 conv = build_conv (ck_std, to, conv);
1325 if (fcode == POINTER_TYPE
1326 || TYPE_PTRDATAMEM_P (from)
1327 || (TYPE_PTRMEMFUNC_P (from)
1328 && conv->rank < cr_pbool)
1329 || NULLPTR_TYPE_P (from))
1330 conv->rank = cr_pbool;
1331 if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1332 conv->bad_p = true;
1333 return conv;
1336 return NULL;
1338 /* We don't check for ENUMERAL_TYPE here because there are no standard
1339 conversions to enum type. */
1340 /* As an extension, allow conversion to complex type. */
1341 else if (ARITHMETIC_TYPE_P (to))
1343 if (! (INTEGRAL_CODE_P (fcode)
1344 || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1345 || SCOPED_ENUM_P (from))
1346 return NULL;
1347 conv = build_conv (ck_std, to, conv);
1349 /* Give this a better rank if it's a promotion. */
1350 if (same_type_p (to, type_promotes_to (from))
1351 && next_conversion (conv)->rank <= cr_promotion)
1352 conv->rank = cr_promotion;
1354 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1355 && vector_types_convertible_p (from, to, false))
1356 return build_conv (ck_std, to, conv);
1357 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1358 && is_properly_derived_from (from, to))
1360 if (conv->kind == ck_rvalue)
1361 conv = next_conversion (conv);
1362 conv = build_conv (ck_base, to, conv);
1363 /* The derived-to-base conversion indicates the initialization
1364 of a parameter with base type from an object of a derived
1365 type. A temporary object is created to hold the result of
1366 the conversion unless we're binding directly to a reference. */
1367 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1369 else
1370 return NULL;
1372 if (flags & LOOKUP_NO_NARROWING)
1373 conv->check_narrowing = true;
1375 return conv;
1378 /* Returns nonzero if T1 is reference-related to T2. */
1380 bool
1381 reference_related_p (tree t1, tree t2)
1383 if (t1 == error_mark_node || t2 == error_mark_node)
1384 return false;
1386 t1 = TYPE_MAIN_VARIANT (t1);
1387 t2 = TYPE_MAIN_VARIANT (t2);
1389 /* [dcl.init.ref]
1391 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1392 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
1393 of T2. */
1394 return (same_type_p (t1, t2)
1395 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1396 && DERIVED_FROM_P (t1, t2)));
1399 /* Returns nonzero if T1 is reference-compatible with T2. */
1401 static bool
1402 reference_compatible_p (tree t1, tree t2)
1404 /* [dcl.init.ref]
1406 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
1407 reference-related to T2 and cv1 is the same cv-qualification as,
1408 or greater cv-qualification than, cv2. */
1409 return (reference_related_p (t1, t2)
1410 && at_least_as_qualified_p (t1, t2));
1413 /* A reference of the indicated TYPE is being bound directly to the
1414 expression represented by the implicit conversion sequence CONV.
1415 Return a conversion sequence for this binding. */
1417 static conversion *
1418 direct_reference_binding (tree type, conversion *conv)
1420 tree t;
1422 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1423 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1425 t = TREE_TYPE (type);
1427 /* [over.ics.rank]
1429 When a parameter of reference type binds directly
1430 (_dcl.init.ref_) to an argument expression, the implicit
1431 conversion sequence is the identity conversion, unless the
1432 argument expression has a type that is a derived class of the
1433 parameter type, in which case the implicit conversion sequence is
1434 a derived-to-base Conversion.
1436 If the parameter binds directly to the result of applying a
1437 conversion function to the argument expression, the implicit
1438 conversion sequence is a user-defined conversion sequence
1439 (_over.ics.user_), with the second standard conversion sequence
1440 either an identity conversion or, if the conversion function
1441 returns an entity of a type that is a derived class of the
1442 parameter type, a derived-to-base conversion. */
1443 if (!same_type_ignoring_top_level_qualifiers_p (t, conv->type))
1445 /* Represent the derived-to-base conversion. */
1446 conv = build_conv (ck_base, t, conv);
1447 /* We will actually be binding to the base-class subobject in
1448 the derived class, so we mark this conversion appropriately.
1449 That way, convert_like knows not to generate a temporary. */
1450 conv->need_temporary_p = false;
1452 return build_conv (ck_ref_bind, type, conv);
1455 /* Returns the conversion path from type FROM to reference type TO for
1456 purposes of reference binding. For lvalue binding, either pass a
1457 reference type to FROM or an lvalue expression to EXPR. If the
1458 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1459 the conversion returned. If C_CAST_P is true, this
1460 conversion is coming from a C-style cast. */
1462 static conversion *
1463 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1464 tsubst_flags_t complain)
1466 conversion *conv = NULL;
1467 tree to = TREE_TYPE (rto);
1468 tree from = rfrom;
1469 tree tfrom;
1470 bool related_p;
1471 bool compatible_p;
1472 cp_lvalue_kind gl_kind;
1473 bool is_lvalue;
1475 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1477 expr = instantiate_type (to, expr, tf_none);
1478 if (expr == error_mark_node)
1479 return NULL;
1480 from = TREE_TYPE (expr);
1483 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1485 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1486 /* DR 1288: Otherwise, if the initializer list has a single element
1487 of type E and ... [T's] referenced type is reference-related to E,
1488 the object or reference is initialized from that element... */
1489 if (CONSTRUCTOR_NELTS (expr) == 1)
1491 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1492 if (error_operand_p (elt))
1493 return NULL;
1494 tree etype = TREE_TYPE (elt);
1495 if (reference_related_p (to, etype))
1497 expr = elt;
1498 from = etype;
1499 goto skip;
1502 /* Otherwise, if T is a reference type, a prvalue temporary of the
1503 type referenced by T is copy-list-initialized or
1504 direct-list-initialized, depending on the kind of initialization
1505 for the reference, and the reference is bound to that temporary. */
1506 conv = implicit_conversion (to, from, expr, c_cast_p,
1507 flags|LOOKUP_NO_TEMP_BIND, complain);
1508 skip:;
1511 if (TREE_CODE (from) == REFERENCE_TYPE)
1513 from = TREE_TYPE (from);
1514 if (!TYPE_REF_IS_RVALUE (rfrom)
1515 || TREE_CODE (from) == FUNCTION_TYPE)
1516 gl_kind = clk_ordinary;
1517 else
1518 gl_kind = clk_rvalueref;
1520 else if (expr)
1522 gl_kind = lvalue_kind (expr);
1523 if (gl_kind & clk_class)
1524 /* A class prvalue is not a glvalue. */
1525 gl_kind = clk_none;
1527 else
1528 gl_kind = clk_none;
1529 is_lvalue = gl_kind && !(gl_kind & clk_rvalueref);
1531 tfrom = from;
1532 if ((gl_kind & clk_bitfield) != 0)
1533 tfrom = unlowered_expr_type (expr);
1535 /* Figure out whether or not the types are reference-related and
1536 reference compatible. We have do do this after stripping
1537 references from FROM. */
1538 related_p = reference_related_p (to, tfrom);
1539 /* If this is a C cast, first convert to an appropriately qualified
1540 type, so that we can later do a const_cast to the desired type. */
1541 if (related_p && c_cast_p
1542 && !at_least_as_qualified_p (to, tfrom))
1543 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1544 compatible_p = reference_compatible_p (to, tfrom);
1546 /* Directly bind reference when target expression's type is compatible with
1547 the reference and expression is an lvalue. In DR391, the wording in
1548 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1549 const and rvalue references to rvalues of compatible class type.
1550 We should also do direct bindings for non-class xvalues. */
1551 if (related_p
1552 && (gl_kind
1553 || (!(flags & LOOKUP_NO_TEMP_BIND)
1554 && (CLASS_TYPE_P (from)
1555 || TREE_CODE (from) == ARRAY_TYPE))))
1557 /* [dcl.init.ref]
1559 If the initializer expression
1561 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1562 is reference-compatible with "cv2 T2,"
1564 the reference is bound directly to the initializer expression
1565 lvalue.
1567 [...]
1568 If the initializer expression is an rvalue, with T2 a class type,
1569 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1570 is bound to the object represented by the rvalue or to a sub-object
1571 within that object. */
1573 conv = build_identity_conv (tfrom, expr);
1574 conv = direct_reference_binding (rto, conv);
1576 if (flags & LOOKUP_PREFER_RVALUE)
1577 /* The top-level caller requested that we pretend that the lvalue
1578 be treated as an rvalue. */
1579 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1580 else if (TREE_CODE (rfrom) == REFERENCE_TYPE)
1581 /* Handle rvalue reference to function properly. */
1582 conv->rvaluedness_matches_p
1583 = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1584 else
1585 conv->rvaluedness_matches_p
1586 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1588 if ((gl_kind & clk_bitfield) != 0
1589 || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1590 /* For the purposes of overload resolution, we ignore the fact
1591 this expression is a bitfield or packed field. (In particular,
1592 [over.ics.ref] says specifically that a function with a
1593 non-const reference parameter is viable even if the
1594 argument is a bitfield.)
1596 However, when we actually call the function we must create
1597 a temporary to which to bind the reference. If the
1598 reference is volatile, or isn't const, then we cannot make
1599 a temporary, so we just issue an error when the conversion
1600 actually occurs. */
1601 conv->need_temporary_p = true;
1603 /* Don't allow binding of lvalues (other than function lvalues) to
1604 rvalue references. */
1605 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1606 && TREE_CODE (to) != FUNCTION_TYPE
1607 && !(flags & LOOKUP_PREFER_RVALUE))
1608 conv->bad_p = true;
1610 /* Nor the reverse. */
1611 if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
1612 && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
1613 || (flags & LOOKUP_NO_RVAL_BIND))
1614 && TREE_CODE (to) != FUNCTION_TYPE)
1615 conv->bad_p = true;
1617 if (!compatible_p)
1618 conv->bad_p = true;
1620 return conv;
1622 /* [class.conv.fct] A conversion function is never used to convert a
1623 (possibly cv-qualified) object to the (possibly cv-qualified) same
1624 object type (or a reference to it), to a (possibly cv-qualified) base
1625 class of that type (or a reference to it).... */
1626 else if (CLASS_TYPE_P (from) && !related_p
1627 && !(flags & LOOKUP_NO_CONVERSION))
1629 /* [dcl.init.ref]
1631 If the initializer expression
1633 -- has a class type (i.e., T2 is a class type) can be
1634 implicitly converted to an lvalue of type "cv3 T3," where
1635 "cv1 T1" is reference-compatible with "cv3 T3". (this
1636 conversion is selected by enumerating the applicable
1637 conversion functions (_over.match.ref_) and choosing the
1638 best one through overload resolution. (_over.match_).
1640 the reference is bound to the lvalue result of the conversion
1641 in the second case. */
1642 z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
1643 complain);
1644 if (cand)
1645 return cand->second_conv;
1648 /* From this point on, we conceptually need temporaries, even if we
1649 elide them. Only the cases above are "direct bindings". */
1650 if (flags & LOOKUP_NO_TEMP_BIND)
1651 return NULL;
1653 /* [over.ics.rank]
1655 When a parameter of reference type is not bound directly to an
1656 argument expression, the conversion sequence is the one required
1657 to convert the argument expression to the underlying type of the
1658 reference according to _over.best.ics_. Conceptually, this
1659 conversion sequence corresponds to copy-initializing a temporary
1660 of the underlying type with the argument expression. Any
1661 difference in top-level cv-qualification is subsumed by the
1662 initialization itself and does not constitute a conversion. */
1664 /* We're generating a temporary now, but don't bind any more in the
1665 conversion (specifically, don't slice the temporary returned by a
1666 conversion operator). */
1667 flags |= LOOKUP_NO_TEMP_BIND;
1669 /* Core issue 899: When [copy-]initializing a temporary to be bound
1670 to the first parameter of a copy constructor (12.8) called with
1671 a single argument in the context of direct-initialization,
1672 explicit conversion functions are also considered.
1674 So don't set LOOKUP_ONLYCONVERTING in that case. */
1675 if (!(flags & LOOKUP_COPY_PARM))
1676 flags |= LOOKUP_ONLYCONVERTING;
1678 if (!conv)
1679 conv = implicit_conversion (to, from, expr, c_cast_p,
1680 flags, complain);
1681 if (!conv)
1682 return NULL;
1684 if (conv->user_conv_p)
1686 /* If initializing the temporary used a conversion function,
1687 recalculate the second conversion sequence. */
1688 for (conversion *t = conv; t; t = next_conversion (t))
1689 if (t->kind == ck_user
1690 && DECL_CONV_FN_P (t->cand->fn))
1692 tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
1693 int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
1694 conversion *new_second
1695 = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
1696 sflags, complain);
1697 if (!new_second)
1698 return NULL;
1699 return merge_conversion_sequences (t, new_second);
1703 conv = build_conv (ck_ref_bind, rto, conv);
1704 /* This reference binding, unlike those above, requires the
1705 creation of a temporary. */
1706 conv->need_temporary_p = true;
1707 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1709 /* [dcl.init.ref]
1711 Otherwise, the reference shall be an lvalue reference to a
1712 non-volatile const type, or the reference shall be an rvalue
1713 reference. */
1714 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1715 conv->bad_p = true;
1717 /* [dcl.init.ref]
1719 Otherwise, a temporary of type "cv1 T1" is created and
1720 initialized from the initializer expression using the rules for a
1721 non-reference copy initialization. If T1 is reference-related to
1722 T2, cv1 must be the same cv-qualification as, or greater
1723 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1724 if (related_p && !at_least_as_qualified_p (to, from))
1725 conv->bad_p = true;
1727 return conv;
1730 /* Returns the implicit conversion sequence (see [over.ics]) from type
1731 FROM to type TO. The optional expression EXPR may affect the
1732 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1733 true, this conversion is coming from a C-style cast. */
1735 static conversion *
1736 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1737 int flags, tsubst_flags_t complain)
1739 conversion *conv;
1741 if (from == error_mark_node || to == error_mark_node
1742 || expr == error_mark_node)
1743 return NULL;
1745 /* Other flags only apply to the primary function in overload
1746 resolution, or after we've chosen one. */
1747 flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
1748 |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE
1749 |LOOKUP_NO_NARROWING|LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL);
1751 /* FIXME: actually we don't want warnings either, but we can't just
1752 have 'complain &= ~(tf_warning|tf_error)' because it would cause
1753 the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
1754 We really ought not to issue that warning until we've committed
1755 to that conversion. */
1756 complain &= ~tf_error;
1758 if (TREE_CODE (to) == REFERENCE_TYPE)
1759 conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
1760 else
1761 conv = standard_conversion (to, from, expr, c_cast_p, flags);
1763 if (conv)
1764 return conv;
1766 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1768 if (is_std_init_list (to))
1769 return build_list_conv (to, expr, flags, complain);
1771 /* As an extension, allow list-initialization of _Complex. */
1772 if (TREE_CODE (to) == COMPLEX_TYPE)
1774 conv = build_complex_conv (to, expr, flags, complain);
1775 if (conv)
1776 return conv;
1779 /* Allow conversion from an initializer-list with one element to a
1780 scalar type. */
1781 if (SCALAR_TYPE_P (to))
1783 int nelts = CONSTRUCTOR_NELTS (expr);
1784 tree elt;
1786 if (nelts == 0)
1787 elt = build_value_init (to, tf_none);
1788 else if (nelts == 1)
1789 elt = CONSTRUCTOR_ELT (expr, 0)->value;
1790 else
1791 elt = error_mark_node;
1793 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
1794 c_cast_p, flags, complain);
1795 if (conv)
1797 conv->check_narrowing = true;
1798 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
1799 /* Too many levels of braces, i.e. '{{1}}'. */
1800 conv->bad_p = true;
1801 return conv;
1804 else if (TREE_CODE (to) == ARRAY_TYPE)
1805 return build_array_conv (to, expr, flags, complain);
1808 if (expr != NULL_TREE
1809 && (MAYBE_CLASS_TYPE_P (from)
1810 || MAYBE_CLASS_TYPE_P (to))
1811 && (flags & LOOKUP_NO_CONVERSION) == 0)
1813 struct z_candidate *cand;
1815 if (CLASS_TYPE_P (to)
1816 && BRACE_ENCLOSED_INITIALIZER_P (expr)
1817 && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
1818 return build_aggr_conv (to, expr, flags, complain);
1820 cand = build_user_type_conversion_1 (to, expr, flags, complain);
1821 if (cand)
1822 conv = cand->second_conv;
1824 /* We used to try to bind a reference to a temporary here, but that
1825 is now handled after the recursive call to this function at the end
1826 of reference_binding. */
1827 return conv;
1830 return NULL;
1833 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1834 functions. ARGS will not be changed until a single candidate is
1835 selected. */
1837 static struct z_candidate *
1838 add_candidate (struct z_candidate **candidates,
1839 tree fn, tree first_arg, const vec<tree, va_gc> *args,
1840 size_t num_convs, conversion **convs,
1841 tree access_path, tree conversion_path,
1842 int viable, struct rejection_reason *reason,
1843 int flags)
1845 struct z_candidate *cand = (struct z_candidate *)
1846 conversion_obstack_alloc (sizeof (struct z_candidate));
1848 cand->fn = fn;
1849 cand->first_arg = first_arg;
1850 cand->args = args;
1851 cand->convs = convs;
1852 cand->num_convs = num_convs;
1853 cand->access_path = access_path;
1854 cand->conversion_path = conversion_path;
1855 cand->viable = viable;
1856 cand->reason = reason;
1857 cand->next = *candidates;
1858 cand->flags = flags;
1859 *candidates = cand;
1861 return cand;
1864 /* Return the number of remaining arguments in the parameter list
1865 beginning with ARG. */
1867 static int
1868 remaining_arguments (tree arg)
1870 int n;
1872 for (n = 0; arg != NULL_TREE && arg != void_list_node;
1873 arg = TREE_CHAIN (arg))
1874 n++;
1876 return n;
1879 /* Create an overload candidate for the function or method FN called
1880 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
1881 FLAGS is passed on to implicit_conversion.
1883 This does not change ARGS.
1885 CTYPE, if non-NULL, is the type we want to pretend this function
1886 comes from for purposes of overload resolution. */
1888 static struct z_candidate *
1889 add_function_candidate (struct z_candidate **candidates,
1890 tree fn, tree ctype, tree first_arg,
1891 const vec<tree, va_gc> *args, tree access_path,
1892 tree conversion_path, int flags,
1893 tsubst_flags_t complain)
1895 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1896 int i, len;
1897 conversion **convs;
1898 tree parmnode;
1899 tree orig_first_arg = first_arg;
1900 int skip;
1901 int viable = 1;
1902 struct rejection_reason *reason = NULL;
1904 /* At this point we should not see any functions which haven't been
1905 explicitly declared, except for friend functions which will have
1906 been found using argument dependent lookup. */
1907 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
1909 /* The `this', `in_chrg' and VTT arguments to constructors are not
1910 considered in overload resolution. */
1911 if (DECL_CONSTRUCTOR_P (fn))
1913 parmlist = skip_artificial_parms_for (fn, parmlist);
1914 skip = num_artificial_parms_for (fn);
1915 if (skip > 0 && first_arg != NULL_TREE)
1917 --skip;
1918 first_arg = NULL_TREE;
1921 else
1922 skip = 0;
1924 len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
1925 convs = alloc_conversions (len);
1927 /* 13.3.2 - Viable functions [over.match.viable]
1928 First, to be a viable function, a candidate function shall have enough
1929 parameters to agree in number with the arguments in the list.
1931 We need to check this first; otherwise, checking the ICSes might cause
1932 us to produce an ill-formed template instantiation. */
1934 parmnode = parmlist;
1935 for (i = 0; i < len; ++i)
1937 if (parmnode == NULL_TREE || parmnode == void_list_node)
1938 break;
1939 parmnode = TREE_CHAIN (parmnode);
1942 if ((i < len && parmnode)
1943 || !sufficient_parms_p (parmnode))
1945 int remaining = remaining_arguments (parmnode);
1946 viable = 0;
1947 reason = arity_rejection (first_arg, i + remaining, len);
1949 /* When looking for a function from a subobject from an implicit
1950 copy/move constructor/operator=, don't consider anything that takes (a
1951 reference to) an unrelated type. See c++/44909 and core 1092. */
1952 else if (parmlist && (flags & LOOKUP_DEFAULTED))
1954 if (DECL_CONSTRUCTOR_P (fn))
1955 i = 1;
1956 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1957 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
1958 i = 2;
1959 else
1960 i = 0;
1961 if (i && len == i)
1963 parmnode = chain_index (i-1, parmlist);
1964 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
1965 ctype))
1966 viable = 0;
1969 /* This only applies at the top level. */
1970 flags &= ~LOOKUP_DEFAULTED;
1973 if (! viable)
1974 goto out;
1976 /* Second, for F to be a viable function, there shall exist for each
1977 argument an implicit conversion sequence that converts that argument
1978 to the corresponding parameter of F. */
1980 parmnode = parmlist;
1982 for (i = 0; i < len; ++i)
1984 tree argtype, to_type;
1985 tree arg;
1986 conversion *t;
1987 int is_this;
1989 if (parmnode == void_list_node)
1990 break;
1992 if (i == 0 && first_arg != NULL_TREE)
1993 arg = first_arg;
1994 else
1995 arg = CONST_CAST_TREE (
1996 (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
1997 argtype = lvalue_type (arg);
1999 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2000 && ! DECL_CONSTRUCTOR_P (fn));
2002 if (parmnode)
2004 tree parmtype = TREE_VALUE (parmnode);
2005 int lflags = flags;
2007 parmnode = TREE_CHAIN (parmnode);
2009 /* The type of the implicit object parameter ('this') for
2010 overload resolution is not always the same as for the
2011 function itself; conversion functions are considered to
2012 be members of the class being converted, and functions
2013 introduced by a using-declaration are considered to be
2014 members of the class that uses them.
2016 Since build_over_call ignores the ICS for the `this'
2017 parameter, we can just change the parm type. */
2018 if (ctype && is_this)
2020 parmtype = cp_build_qualified_type
2021 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
2022 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2024 /* If the function has a ref-qualifier, the implicit
2025 object parameter has reference type. */
2026 bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2027 parmtype = cp_build_reference_type (parmtype, rv);
2028 /* The special handling of 'this' conversions in compare_ics
2029 does not apply if there is a ref-qualifier. */
2030 is_this = false;
2032 else
2034 parmtype = build_pointer_type (parmtype);
2035 arg = build_this (arg);
2036 argtype = lvalue_type (arg);
2040 /* Core issue 899: When [copy-]initializing a temporary to be bound
2041 to the first parameter of a copy constructor (12.8) called with
2042 a single argument in the context of direct-initialization,
2043 explicit conversion functions are also considered.
2045 So set LOOKUP_COPY_PARM to let reference_binding know that
2046 it's being called in that context. We generalize the above
2047 to handle move constructors and template constructors as well;
2048 the standardese should soon be updated similarly. */
2049 if (ctype && i == 0 && (len-skip == 1)
2050 && DECL_CONSTRUCTOR_P (fn)
2051 && parmtype != error_mark_node
2052 && (same_type_ignoring_top_level_qualifiers_p
2053 (non_reference (parmtype), ctype)))
2055 if (!(flags & LOOKUP_ONLYCONVERTING))
2056 lflags |= LOOKUP_COPY_PARM;
2057 /* We allow user-defined conversions within init-lists, but
2058 don't list-initialize the copy parm, as that would mean
2059 using two levels of braces for the same type. */
2060 if ((flags & LOOKUP_LIST_INIT_CTOR)
2061 && BRACE_ENCLOSED_INITIALIZER_P (arg))
2062 lflags |= LOOKUP_NO_CONVERSION;
2064 else
2065 lflags |= LOOKUP_ONLYCONVERTING;
2067 t = implicit_conversion (parmtype, argtype, arg,
2068 /*c_cast_p=*/false, lflags, complain);
2069 to_type = parmtype;
2071 else
2073 t = build_identity_conv (argtype, arg);
2074 t->ellipsis_p = true;
2075 to_type = argtype;
2078 if (t && is_this)
2079 t->this_p = true;
2081 convs[i] = t;
2082 if (! t)
2084 viable = 0;
2085 reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
2086 break;
2089 if (t->bad_p)
2091 viable = -1;
2092 reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type);
2096 out:
2097 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2098 access_path, conversion_path, viable, reason, flags);
2101 /* Create an overload candidate for the conversion function FN which will
2102 be invoked for expression OBJ, producing a pointer-to-function which
2103 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2104 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2105 passed on to implicit_conversion.
2107 Actually, we don't really care about FN; we care about the type it
2108 converts to. There may be multiple conversion functions that will
2109 convert to that type, and we rely on build_user_type_conversion_1 to
2110 choose the best one; so when we create our candidate, we record the type
2111 instead of the function. */
2113 static struct z_candidate *
2114 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2115 tree first_arg, const vec<tree, va_gc> *arglist,
2116 tree access_path, tree conversion_path,
2117 tsubst_flags_t complain)
2119 tree totype = TREE_TYPE (TREE_TYPE (fn));
2120 int i, len, viable, flags;
2121 tree parmlist, parmnode;
2122 conversion **convs;
2123 struct rejection_reason *reason;
2125 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2126 parmlist = TREE_TYPE (parmlist);
2127 parmlist = TYPE_ARG_TYPES (parmlist);
2129 len = vec_safe_length (arglist) + (first_arg != NULL_TREE ? 1 : 0) + 1;
2130 convs = alloc_conversions (len);
2131 parmnode = parmlist;
2132 viable = 1;
2133 flags = LOOKUP_IMPLICIT;
2134 reason = NULL;
2136 /* Don't bother looking up the same type twice. */
2137 if (*candidates && (*candidates)->fn == totype)
2138 return NULL;
2140 for (i = 0; i < len; ++i)
2142 tree arg, argtype, convert_type = NULL_TREE;
2143 conversion *t;
2145 if (i == 0)
2146 arg = obj;
2147 else if (i == 1 && first_arg != NULL_TREE)
2148 arg = first_arg;
2149 else
2150 arg = (*arglist)[i - (first_arg != NULL_TREE ? 1 : 0) - 1];
2151 argtype = lvalue_type (arg);
2153 if (i == 0)
2155 t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false,
2156 flags, complain);
2157 convert_type = totype;
2159 else if (parmnode == void_list_node)
2160 break;
2161 else if (parmnode)
2163 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2164 /*c_cast_p=*/false, flags, complain);
2165 convert_type = TREE_VALUE (parmnode);
2167 else
2169 t = build_identity_conv (argtype, arg);
2170 t->ellipsis_p = true;
2171 convert_type = argtype;
2174 convs[i] = t;
2175 if (! t)
2176 break;
2178 if (t->bad_p)
2180 viable = -1;
2181 reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type);
2184 if (i == 0)
2185 continue;
2187 if (parmnode)
2188 parmnode = TREE_CHAIN (parmnode);
2191 if (i < len
2192 || ! sufficient_parms_p (parmnode))
2194 int remaining = remaining_arguments (parmnode);
2195 viable = 0;
2196 reason = arity_rejection (NULL_TREE, i + remaining, len);
2199 return add_candidate (candidates, totype, first_arg, arglist, len, convs,
2200 access_path, conversion_path, viable, reason, flags);
2203 static void
2204 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2205 tree type1, tree type2, tree *args, tree *argtypes,
2206 int flags, tsubst_flags_t complain)
2208 conversion *t;
2209 conversion **convs;
2210 size_t num_convs;
2211 int viable = 1, i;
2212 tree types[2];
2213 struct rejection_reason *reason = NULL;
2215 types[0] = type1;
2216 types[1] = type2;
2218 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
2219 convs = alloc_conversions (num_convs);
2221 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2222 conversion ops are allowed. We handle that here by just checking for
2223 boolean_type_node because other operators don't ask for it. COND_EXPR
2224 also does contextual conversion to bool for the first operand, but we
2225 handle that in build_conditional_expr, and type1 here is operand 2. */
2226 if (type1 != boolean_type_node)
2227 flags |= LOOKUP_ONLYCONVERTING;
2229 for (i = 0; i < 2; ++i)
2231 if (! args[i])
2232 break;
2234 t = implicit_conversion (types[i], argtypes[i], args[i],
2235 /*c_cast_p=*/false, flags, complain);
2236 if (! t)
2238 viable = 0;
2239 /* We need something for printing the candidate. */
2240 t = build_identity_conv (types[i], NULL_TREE);
2241 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2242 types[i]);
2244 else if (t->bad_p)
2246 viable = 0;
2247 reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2248 types[i]);
2250 convs[i] = t;
2253 /* For COND_EXPR we rearranged the arguments; undo that now. */
2254 if (args[2])
2256 convs[2] = convs[1];
2257 convs[1] = convs[0];
2258 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2259 /*c_cast_p=*/false, flags,
2260 complain);
2261 if (t)
2262 convs[0] = t;
2263 else
2265 viable = 0;
2266 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2267 boolean_type_node);
2271 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2272 num_convs, convs,
2273 /*access_path=*/NULL_TREE,
2274 /*conversion_path=*/NULL_TREE,
2275 viable, reason, flags);
2278 static bool
2279 is_complete (tree t)
2281 return COMPLETE_TYPE_P (complete_type (t));
2284 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2286 static bool
2287 promoted_arithmetic_type_p (tree type)
2289 /* [over.built]
2291 In this section, the term promoted integral type is used to refer
2292 to those integral types which are preserved by integral promotion
2293 (including e.g. int and long but excluding e.g. char).
2294 Similarly, the term promoted arithmetic type refers to promoted
2295 integral types plus floating types. */
2296 return ((CP_INTEGRAL_TYPE_P (type)
2297 && same_type_p (type_promotes_to (type), type))
2298 || TREE_CODE (type) == REAL_TYPE);
2301 /* Create any builtin operator overload candidates for the operator in
2302 question given the converted operand types TYPE1 and TYPE2. The other
2303 args are passed through from add_builtin_candidates to
2304 build_builtin_candidate.
2306 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2307 If CODE is requires candidates operands of the same type of the kind
2308 of which TYPE1 and TYPE2 are, we add both candidates
2309 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2311 static void
2312 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2313 enum tree_code code2, tree fnname, tree type1,
2314 tree type2, tree *args, tree *argtypes, int flags,
2315 tsubst_flags_t complain)
2317 switch (code)
2319 case POSTINCREMENT_EXPR:
2320 case POSTDECREMENT_EXPR:
2321 args[1] = integer_zero_node;
2322 type2 = integer_type_node;
2323 break;
2324 default:
2325 break;
2328 switch (code)
2331 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2332 and VQ is either volatile or empty, there exist candidate operator
2333 functions of the form
2334 VQ T& operator++(VQ T&);
2335 T operator++(VQ T&, int);
2336 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
2337 type other than bool, and VQ is either volatile or empty, there exist
2338 candidate operator functions of the form
2339 VQ T& operator--(VQ T&);
2340 T operator--(VQ T&, int);
2341 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
2342 complete object type, and VQ is either volatile or empty, there exist
2343 candidate operator functions of the form
2344 T*VQ& operator++(T*VQ&);
2345 T*VQ& operator--(T*VQ&);
2346 T* operator++(T*VQ&, int);
2347 T* operator--(T*VQ&, int); */
2349 case POSTDECREMENT_EXPR:
2350 case PREDECREMENT_EXPR:
2351 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2352 return;
2353 case POSTINCREMENT_EXPR:
2354 case PREINCREMENT_EXPR:
2355 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2357 type1 = build_reference_type (type1);
2358 break;
2360 return;
2362 /* 7 For every cv-qualified or cv-unqualified object type T, there
2363 exist candidate operator functions of the form
2365 T& operator*(T*);
2367 8 For every function type T, there exist candidate operator functions of
2368 the form
2369 T& operator*(T*); */
2371 case INDIRECT_REF:
2372 if (TYPE_PTR_P (type1)
2373 && (TYPE_PTROB_P (type1)
2374 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2375 break;
2376 return;
2378 /* 9 For every type T, there exist candidate operator functions of the form
2379 T* operator+(T*);
2381 10For every promoted arithmetic type T, there exist candidate operator
2382 functions of the form
2383 T operator+(T);
2384 T operator-(T); */
2386 case UNARY_PLUS_EXPR: /* unary + */
2387 if (TYPE_PTR_P (type1))
2388 break;
2389 case NEGATE_EXPR:
2390 if (ARITHMETIC_TYPE_P (type1))
2391 break;
2392 return;
2394 /* 11For every promoted integral type T, there exist candidate operator
2395 functions of the form
2396 T operator~(T); */
2398 case BIT_NOT_EXPR:
2399 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2400 break;
2401 return;
2403 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2404 is the same type as C2 or is a derived class of C2, T is a complete
2405 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2406 there exist candidate operator functions of the form
2407 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2408 where CV12 is the union of CV1 and CV2. */
2410 case MEMBER_REF:
2411 if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2413 tree c1 = TREE_TYPE (type1);
2414 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2416 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2417 && (TYPE_PTRMEMFUNC_P (type2)
2418 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2419 break;
2421 return;
2423 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2424 didate operator functions of the form
2425 LR operator*(L, R);
2426 LR operator/(L, R);
2427 LR operator+(L, R);
2428 LR operator-(L, R);
2429 bool operator<(L, R);
2430 bool operator>(L, R);
2431 bool operator<=(L, R);
2432 bool operator>=(L, R);
2433 bool operator==(L, R);
2434 bool operator!=(L, R);
2435 where LR is the result of the usual arithmetic conversions between
2436 types L and R.
2438 14For every pair of types T and I, where T is a cv-qualified or cv-
2439 unqualified complete object type and I is a promoted integral type,
2440 there exist candidate operator functions of the form
2441 T* operator+(T*, I);
2442 T& operator[](T*, I);
2443 T* operator-(T*, I);
2444 T* operator+(I, T*);
2445 T& operator[](I, T*);
2447 15For every T, where T is a pointer to complete object type, there exist
2448 candidate operator functions of the form112)
2449 ptrdiff_t operator-(T, T);
2451 16For every pointer or enumeration type T, there exist candidate operator
2452 functions of the form
2453 bool operator<(T, T);
2454 bool operator>(T, T);
2455 bool operator<=(T, T);
2456 bool operator>=(T, T);
2457 bool operator==(T, T);
2458 bool operator!=(T, T);
2460 17For every pointer to member type T, there exist candidate operator
2461 functions of the form
2462 bool operator==(T, T);
2463 bool operator!=(T, T); */
2465 case MINUS_EXPR:
2466 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2467 break;
2468 if (TYPE_PTROB_P (type1)
2469 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2471 type2 = ptrdiff_type_node;
2472 break;
2474 case MULT_EXPR:
2475 case TRUNC_DIV_EXPR:
2476 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2477 break;
2478 return;
2480 case EQ_EXPR:
2481 case NE_EXPR:
2482 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2483 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
2484 break;
2485 if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
2487 type2 = type1;
2488 break;
2490 if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
2492 type1 = type2;
2493 break;
2495 /* Fall through. */
2496 case LT_EXPR:
2497 case GT_EXPR:
2498 case LE_EXPR:
2499 case GE_EXPR:
2500 case MAX_EXPR:
2501 case MIN_EXPR:
2502 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2503 break;
2504 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2505 break;
2506 if (TREE_CODE (type1) == ENUMERAL_TYPE
2507 && TREE_CODE (type2) == ENUMERAL_TYPE)
2508 break;
2509 if (TYPE_PTR_P (type1)
2510 && null_ptr_cst_p (args[1]))
2512 type2 = type1;
2513 break;
2515 if (null_ptr_cst_p (args[0])
2516 && TYPE_PTR_P (type2))
2518 type1 = type2;
2519 break;
2521 return;
2523 case PLUS_EXPR:
2524 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2525 break;
2526 case ARRAY_REF:
2527 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2529 type1 = ptrdiff_type_node;
2530 break;
2532 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2534 type2 = ptrdiff_type_node;
2535 break;
2537 return;
2539 /* 18For every pair of promoted integral types L and R, there exist candi-
2540 date operator functions of the form
2541 LR operator%(L, R);
2542 LR operator&(L, R);
2543 LR operator^(L, R);
2544 LR operator|(L, R);
2545 L operator<<(L, R);
2546 L operator>>(L, R);
2547 where LR is the result of the usual arithmetic conversions between
2548 types L and R. */
2550 case TRUNC_MOD_EXPR:
2551 case BIT_AND_EXPR:
2552 case BIT_IOR_EXPR:
2553 case BIT_XOR_EXPR:
2554 case LSHIFT_EXPR:
2555 case RSHIFT_EXPR:
2556 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2557 break;
2558 return;
2560 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2561 type, VQ is either volatile or empty, and R is a promoted arithmetic
2562 type, there exist candidate operator functions of the form
2563 VQ L& operator=(VQ L&, R);
2564 VQ L& operator*=(VQ L&, R);
2565 VQ L& operator/=(VQ L&, R);
2566 VQ L& operator+=(VQ L&, R);
2567 VQ L& operator-=(VQ L&, R);
2569 20For every pair T, VQ), where T is any type and VQ is either volatile
2570 or empty, there exist candidate operator functions of the form
2571 T*VQ& operator=(T*VQ&, T*);
2573 21For every pair T, VQ), where T is a pointer to member type and VQ is
2574 either volatile or empty, there exist candidate operator functions of
2575 the form
2576 VQ T& operator=(VQ T&, T);
2578 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2579 unqualified complete object type, VQ is either volatile or empty, and
2580 I is a promoted integral type, there exist candidate operator func-
2581 tions of the form
2582 T*VQ& operator+=(T*VQ&, I);
2583 T*VQ& operator-=(T*VQ&, I);
2585 23For every triple L, VQ, R), where L is an integral or enumeration
2586 type, VQ is either volatile or empty, and R is a promoted integral
2587 type, there exist candidate operator functions of the form
2589 VQ L& operator%=(VQ L&, R);
2590 VQ L& operator<<=(VQ L&, R);
2591 VQ L& operator>>=(VQ L&, R);
2592 VQ L& operator&=(VQ L&, R);
2593 VQ L& operator^=(VQ L&, R);
2594 VQ L& operator|=(VQ L&, R); */
2596 case MODIFY_EXPR:
2597 switch (code2)
2599 case PLUS_EXPR:
2600 case MINUS_EXPR:
2601 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2603 type2 = ptrdiff_type_node;
2604 break;
2606 case MULT_EXPR:
2607 case TRUNC_DIV_EXPR:
2608 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2609 break;
2610 return;
2612 case TRUNC_MOD_EXPR:
2613 case BIT_AND_EXPR:
2614 case BIT_IOR_EXPR:
2615 case BIT_XOR_EXPR:
2616 case LSHIFT_EXPR:
2617 case RSHIFT_EXPR:
2618 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2619 break;
2620 return;
2622 case NOP_EXPR:
2623 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2624 break;
2625 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2626 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2627 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2628 || ((TYPE_PTRMEMFUNC_P (type1)
2629 || TYPE_PTR_P (type1))
2630 && null_ptr_cst_p (args[1])))
2632 type2 = type1;
2633 break;
2635 return;
2637 default:
2638 gcc_unreachable ();
2640 type1 = build_reference_type (type1);
2641 break;
2643 case COND_EXPR:
2644 /* [over.built]
2646 For every pair of promoted arithmetic types L and R, there
2647 exist candidate operator functions of the form
2649 LR operator?(bool, L, R);
2651 where LR is the result of the usual arithmetic conversions
2652 between types L and R.
2654 For every type T, where T is a pointer or pointer-to-member
2655 type, there exist candidate operator functions of the form T
2656 operator?(bool, T, T); */
2658 if (promoted_arithmetic_type_p (type1)
2659 && promoted_arithmetic_type_p (type2))
2660 /* That's OK. */
2661 break;
2663 /* Otherwise, the types should be pointers. */
2664 if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
2665 return;
2667 /* We don't check that the two types are the same; the logic
2668 below will actually create two candidates; one in which both
2669 parameter types are TYPE1, and one in which both parameter
2670 types are TYPE2. */
2671 break;
2673 case REALPART_EXPR:
2674 case IMAGPART_EXPR:
2675 if (ARITHMETIC_TYPE_P (type1))
2676 break;
2677 return;
2679 default:
2680 gcc_unreachable ();
2683 /* Make sure we don't create builtin candidates with dependent types. */
2684 bool u1 = uses_template_parms (type1);
2685 bool u2 = type2 ? uses_template_parms (type2) : false;
2686 if (u1 || u2)
2688 /* Try to recover if one of the types is non-dependent. But if
2689 there's only one type, there's nothing we can do. */
2690 if (!type2)
2691 return;
2692 /* And we lose if both are dependent. */
2693 if (u1 && u2)
2694 return;
2695 /* Or if they have different forms. */
2696 if (TREE_CODE (type1) != TREE_CODE (type2))
2697 return;
2699 if (u1 && !u2)
2700 type1 = type2;
2701 else if (u2 && !u1)
2702 type2 = type1;
2705 /* If we're dealing with two pointer types or two enumeral types,
2706 we need candidates for both of them. */
2707 if (type2 && !same_type_p (type1, type2)
2708 && TREE_CODE (type1) == TREE_CODE (type2)
2709 && (TREE_CODE (type1) == REFERENCE_TYPE
2710 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2711 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2712 || TYPE_PTRMEMFUNC_P (type1)
2713 || MAYBE_CLASS_TYPE_P (type1)
2714 || TREE_CODE (type1) == ENUMERAL_TYPE))
2716 if (TYPE_PTR_OR_PTRMEM_P (type1))
2718 tree cptype = composite_pointer_type (type1, type2,
2719 error_mark_node,
2720 error_mark_node,
2721 CPO_CONVERSION,
2722 tf_none);
2723 if (cptype != error_mark_node)
2725 build_builtin_candidate
2726 (candidates, fnname, cptype, cptype, args, argtypes,
2727 flags, complain);
2728 return;
2732 build_builtin_candidate
2733 (candidates, fnname, type1, type1, args, argtypes, flags, complain);
2734 build_builtin_candidate
2735 (candidates, fnname, type2, type2, args, argtypes, flags, complain);
2736 return;
2739 build_builtin_candidate
2740 (candidates, fnname, type1, type2, args, argtypes, flags, complain);
2743 tree
2744 type_decays_to (tree type)
2746 if (TREE_CODE (type) == ARRAY_TYPE)
2747 return build_pointer_type (TREE_TYPE (type));
2748 if (TREE_CODE (type) == FUNCTION_TYPE)
2749 return build_pointer_type (type);
2750 return type;
2753 /* There are three conditions of builtin candidates:
2755 1) bool-taking candidates. These are the same regardless of the input.
2756 2) pointer-pair taking candidates. These are generated for each type
2757 one of the input types converts to.
2758 3) arithmetic candidates. According to the standard, we should generate
2759 all of these, but I'm trying not to...
2761 Here we generate a superset of the possible candidates for this particular
2762 case. That is a subset of the full set the standard defines, plus some
2763 other cases which the standard disallows. add_builtin_candidate will
2764 filter out the invalid set. */
2766 static void
2767 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2768 enum tree_code code2, tree fnname, tree *args,
2769 int flags, tsubst_flags_t complain)
2771 int ref1, i;
2772 int enum_p = 0;
2773 tree type, argtypes[3], t;
2774 /* TYPES[i] is the set of possible builtin-operator parameter types
2775 we will consider for the Ith argument. */
2776 vec<tree, va_gc> *types[2];
2777 unsigned ix;
2779 for (i = 0; i < 3; ++i)
2781 if (args[i])
2782 argtypes[i] = unlowered_expr_type (args[i]);
2783 else
2784 argtypes[i] = NULL_TREE;
2787 switch (code)
2789 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2790 and VQ is either volatile or empty, there exist candidate operator
2791 functions of the form
2792 VQ T& operator++(VQ T&); */
2794 case POSTINCREMENT_EXPR:
2795 case PREINCREMENT_EXPR:
2796 case POSTDECREMENT_EXPR:
2797 case PREDECREMENT_EXPR:
2798 case MODIFY_EXPR:
2799 ref1 = 1;
2800 break;
2802 /* 24There also exist candidate operator functions of the form
2803 bool operator!(bool);
2804 bool operator&&(bool, bool);
2805 bool operator||(bool, bool); */
2807 case TRUTH_NOT_EXPR:
2808 build_builtin_candidate
2809 (candidates, fnname, boolean_type_node,
2810 NULL_TREE, args, argtypes, flags, complain);
2811 return;
2813 case TRUTH_ORIF_EXPR:
2814 case TRUTH_ANDIF_EXPR:
2815 build_builtin_candidate
2816 (candidates, fnname, boolean_type_node,
2817 boolean_type_node, args, argtypes, flags, complain);
2818 return;
2820 case ADDR_EXPR:
2821 case COMPOUND_EXPR:
2822 case COMPONENT_REF:
2823 return;
2825 case COND_EXPR:
2826 case EQ_EXPR:
2827 case NE_EXPR:
2828 case LT_EXPR:
2829 case LE_EXPR:
2830 case GT_EXPR:
2831 case GE_EXPR:
2832 enum_p = 1;
2833 /* Fall through. */
2835 default:
2836 ref1 = 0;
2839 types[0] = make_tree_vector ();
2840 types[1] = make_tree_vector ();
2842 for (i = 0; i < 2; ++i)
2844 if (! args[i])
2846 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
2848 tree convs;
2850 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2851 return;
2853 convs = lookup_conversions (argtypes[i]);
2855 if (code == COND_EXPR)
2857 if (real_lvalue_p (args[i]))
2858 vec_safe_push (types[i], build_reference_type (argtypes[i]));
2860 vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
2863 else if (! convs)
2864 return;
2866 for (; convs; convs = TREE_CHAIN (convs))
2868 type = TREE_TYPE (convs);
2870 if (i == 0 && ref1
2871 && (TREE_CODE (type) != REFERENCE_TYPE
2872 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2873 continue;
2875 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2876 vec_safe_push (types[i], type);
2878 type = non_reference (type);
2879 if (i != 0 || ! ref1)
2881 type = cv_unqualified (type_decays_to (type));
2882 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2883 vec_safe_push (types[i], type);
2884 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2885 type = type_promotes_to (type);
2888 if (! vec_member (type, types[i]))
2889 vec_safe_push (types[i], type);
2892 else
2894 if (code == COND_EXPR && real_lvalue_p (args[i]))
2895 vec_safe_push (types[i], build_reference_type (argtypes[i]));
2896 type = non_reference (argtypes[i]);
2897 if (i != 0 || ! ref1)
2899 type = cv_unqualified (type_decays_to (type));
2900 if (enum_p && UNSCOPED_ENUM_P (type))
2901 vec_safe_push (types[i], type);
2902 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2903 type = type_promotes_to (type);
2905 vec_safe_push (types[i], type);
2909 /* Run through the possible parameter types of both arguments,
2910 creating candidates with those parameter types. */
2911 FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
2913 unsigned jx;
2914 tree u;
2916 if (!types[1]->is_empty ())
2917 FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
2918 add_builtin_candidate
2919 (candidates, code, code2, fnname, t,
2920 u, args, argtypes, flags, complain);
2921 else
2922 add_builtin_candidate
2923 (candidates, code, code2, fnname, t,
2924 NULL_TREE, args, argtypes, flags, complain);
2927 release_tree_vector (types[0]);
2928 release_tree_vector (types[1]);
2932 /* If TMPL can be successfully instantiated as indicated by
2933 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2935 TMPL is the template. EXPLICIT_TARGS are any explicit template
2936 arguments. ARGLIST is the arguments provided at the call-site.
2937 This does not change ARGLIST. The RETURN_TYPE is the desired type
2938 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
2939 as for add_function_candidate. If an OBJ is supplied, FLAGS and
2940 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
2942 static struct z_candidate*
2943 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2944 tree ctype, tree explicit_targs, tree first_arg,
2945 const vec<tree, va_gc> *arglist, tree return_type,
2946 tree access_path, tree conversion_path,
2947 int flags, tree obj, unification_kind_t strict,
2948 tsubst_flags_t complain)
2950 int ntparms = DECL_NTPARMS (tmpl);
2951 tree targs = make_tree_vec (ntparms);
2952 unsigned int len = vec_safe_length (arglist);
2953 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
2954 unsigned int skip_without_in_chrg = 0;
2955 tree first_arg_without_in_chrg = first_arg;
2956 tree *args_without_in_chrg;
2957 unsigned int nargs_without_in_chrg;
2958 unsigned int ia, ix;
2959 tree arg;
2960 struct z_candidate *cand;
2961 tree fn;
2962 struct rejection_reason *reason = NULL;
2963 int errs;
2965 /* We don't do deduction on the in-charge parameter, the VTT
2966 parameter or 'this'. */
2967 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2969 if (first_arg_without_in_chrg != NULL_TREE)
2970 first_arg_without_in_chrg = NULL_TREE;
2971 else
2972 ++skip_without_in_chrg;
2975 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2976 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2977 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
2979 if (first_arg_without_in_chrg != NULL_TREE)
2980 first_arg_without_in_chrg = NULL_TREE;
2981 else
2982 ++skip_without_in_chrg;
2985 if (len < skip_without_in_chrg)
2986 return NULL;
2988 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
2989 + (len - skip_without_in_chrg));
2990 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
2991 ia = 0;
2992 if (first_arg_without_in_chrg != NULL_TREE)
2994 args_without_in_chrg[ia] = first_arg_without_in_chrg;
2995 ++ia;
2997 for (ix = skip_without_in_chrg;
2998 vec_safe_iterate (arglist, ix, &arg);
2999 ++ix)
3001 args_without_in_chrg[ia] = arg;
3002 ++ia;
3004 gcc_assert (ia == nargs_without_in_chrg);
3006 errs = errorcount+sorrycount;
3007 fn = fn_type_unification (tmpl, explicit_targs, targs,
3008 args_without_in_chrg,
3009 nargs_without_in_chrg,
3010 return_type, strict, flags, false,
3011 complain & tf_decltype);
3013 if (fn == error_mark_node)
3015 /* Don't repeat unification later if it already resulted in errors. */
3016 if (errorcount+sorrycount == errs)
3017 reason = template_unification_rejection (tmpl, explicit_targs,
3018 targs, args_without_in_chrg,
3019 nargs_without_in_chrg,
3020 return_type, strict, flags);
3021 else
3022 reason = template_unification_error_rejection ();
3023 goto fail;
3026 /* In [class.copy]:
3028 A member function template is never instantiated to perform the
3029 copy of a class object to an object of its class type.
3031 It's a little unclear what this means; the standard explicitly
3032 does allow a template to be used to copy a class. For example,
3035 struct A {
3036 A(A&);
3037 template <class T> A(const T&);
3039 const A f ();
3040 void g () { A a (f ()); }
3042 the member template will be used to make the copy. The section
3043 quoted above appears in the paragraph that forbids constructors
3044 whose only parameter is (a possibly cv-qualified variant of) the
3045 class type, and a logical interpretation is that the intent was
3046 to forbid the instantiation of member templates which would then
3047 have that form. */
3048 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3050 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3051 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3052 ctype))
3054 reason = invalid_copy_with_fn_template_rejection ();
3055 goto fail;
3059 if (obj != NULL_TREE)
3060 /* Aha, this is a conversion function. */
3061 cand = add_conv_candidate (candidates, fn, obj, first_arg, arglist,
3062 access_path, conversion_path, complain);
3063 else
3064 cand = add_function_candidate (candidates, fn, ctype,
3065 first_arg, arglist, access_path,
3066 conversion_path, flags, complain);
3067 if (DECL_TI_TEMPLATE (fn) != tmpl)
3068 /* This situation can occur if a member template of a template
3069 class is specialized. Then, instantiate_template might return
3070 an instantiation of the specialization, in which case the
3071 DECL_TI_TEMPLATE field will point at the original
3072 specialization. For example:
3074 template <class T> struct S { template <class U> void f(U);
3075 template <> void f(int) {}; };
3076 S<double> sd;
3077 sd.f(3);
3079 Here, TMPL will be template <class U> S<double>::f(U).
3080 And, instantiate template will give us the specialization
3081 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3082 for this will point at template <class T> template <> S<T>::f(int),
3083 so that we can find the definition. For the purposes of
3084 overload resolution, however, we want the original TMPL. */
3085 cand->template_decl = build_template_info (tmpl, targs);
3086 else
3087 cand->template_decl = DECL_TEMPLATE_INFO (fn);
3088 cand->explicit_targs = explicit_targs;
3090 return cand;
3091 fail:
3092 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
3093 access_path, conversion_path, 0, reason, flags);
3097 static struct z_candidate *
3098 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3099 tree explicit_targs, tree first_arg,
3100 const vec<tree, va_gc> *arglist, tree return_type,
3101 tree access_path, tree conversion_path, int flags,
3102 unification_kind_t strict, tsubst_flags_t complain)
3104 return
3105 add_template_candidate_real (candidates, tmpl, ctype,
3106 explicit_targs, first_arg, arglist,
3107 return_type, access_path, conversion_path,
3108 flags, NULL_TREE, strict, complain);
3112 static struct z_candidate *
3113 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3114 tree obj, tree first_arg,
3115 const vec<tree, va_gc> *arglist,
3116 tree return_type, tree access_path,
3117 tree conversion_path, tsubst_flags_t complain)
3119 return
3120 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3121 first_arg, arglist, return_type, access_path,
3122 conversion_path, 0, obj, DEDUCE_CONV,
3123 complain);
3126 /* The CANDS are the set of candidates that were considered for
3127 overload resolution. Return the set of viable candidates, or CANDS
3128 if none are viable. If any of the candidates were viable, set
3129 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3130 considered viable only if it is strictly viable. */
3132 static struct z_candidate*
3133 splice_viable (struct z_candidate *cands,
3134 bool strict_p,
3135 bool *any_viable_p)
3137 struct z_candidate *viable;
3138 struct z_candidate **last_viable;
3139 struct z_candidate **cand;
3140 bool found_strictly_viable = false;
3142 /* Be strict inside templates, since build_over_call won't actually
3143 do the conversions to get pedwarns. */
3144 if (processing_template_decl)
3145 strict_p = true;
3147 viable = NULL;
3148 last_viable = &viable;
3149 *any_viable_p = false;
3151 cand = &cands;
3152 while (*cand)
3154 struct z_candidate *c = *cand;
3155 if (!strict_p
3156 && (c->viable == 1 || TREE_CODE (c->fn) == TEMPLATE_DECL))
3158 /* Be strict in the presence of a viable candidate. Also if
3159 there are template candidates, so that we get deduction errors
3160 for them instead of silently preferring a bad conversion. */
3161 strict_p = true;
3162 if (viable && !found_strictly_viable)
3164 /* Put any spliced near matches back onto the main list so
3165 that we see them if there is no strict match. */
3166 *any_viable_p = false;
3167 *last_viable = cands;
3168 cands = viable;
3169 viable = NULL;
3170 last_viable = &viable;
3174 if (strict_p ? c->viable == 1 : c->viable)
3176 *last_viable = c;
3177 *cand = c->next;
3178 c->next = NULL;
3179 last_viable = &c->next;
3180 *any_viable_p = true;
3181 if (c->viable == 1)
3182 found_strictly_viable = true;
3184 else
3185 cand = &c->next;
3188 return viable ? viable : cands;
3191 static bool
3192 any_strictly_viable (struct z_candidate *cands)
3194 for (; cands; cands = cands->next)
3195 if (cands->viable == 1)
3196 return true;
3197 return false;
3200 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3201 words, it is about to become the "this" pointer for a member
3202 function call. Take the address of the object. */
3204 static tree
3205 build_this (tree obj)
3207 /* In a template, we are only concerned about the type of the
3208 expression, so we can take a shortcut. */
3209 if (processing_template_decl)
3210 return build_address (obj);
3212 return cp_build_addr_expr (obj, tf_warning_or_error);
3215 /* Returns true iff functions are equivalent. Equivalent functions are
3216 not '==' only if one is a function-local extern function or if
3217 both are extern "C". */
3219 static inline int
3220 equal_functions (tree fn1, tree fn2)
3222 if (TREE_CODE (fn1) != TREE_CODE (fn2))
3223 return 0;
3224 if (TREE_CODE (fn1) == TEMPLATE_DECL)
3225 return fn1 == fn2;
3226 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
3227 || DECL_EXTERN_C_FUNCTION_P (fn1))
3228 return decls_match (fn1, fn2);
3229 return fn1 == fn2;
3232 /* Print information about a candidate being rejected due to INFO. */
3234 static void
3235 print_conversion_rejection (location_t loc, struct conversion_info *info)
3237 tree from = info->from;
3238 if (!TYPE_P (from))
3239 from = lvalue_type (from);
3240 if (info->n_arg == -1)
3242 /* Conversion of implicit `this' argument failed. */
3243 if (!TYPE_P (info->from))
3244 /* A bad conversion for 'this' must be discarding cv-quals. */
3245 inform (loc, " passing %qT as %<this%> "
3246 "argument discards qualifiers",
3247 from);
3248 else
3249 inform (loc, " no known conversion for implicit "
3250 "%<this%> parameter from %qT to %qT",
3251 from, info->to_type);
3253 else if (!TYPE_P (info->from))
3255 if (info->n_arg >= 0)
3256 inform (loc, " conversion of argument %d would be ill-formed:",
3257 info->n_arg + 1);
3258 perform_implicit_conversion (info->to_type, info->from,
3259 tf_warning_or_error);
3261 else if (info->n_arg == -2)
3262 /* Conversion of conversion function return value failed. */
3263 inform (loc, " no known conversion from %qT to %qT",
3264 from, info->to_type);
3265 else
3266 inform (loc, " no known conversion for argument %d from %qT to %qT",
3267 info->n_arg + 1, from, info->to_type);
3270 /* Print information about a candidate with WANT parameters and we found
3271 HAVE. */
3273 static void
3274 print_arity_information (location_t loc, unsigned int have, unsigned int want)
3276 inform_n (loc, want,
3277 " candidate expects %d argument, %d provided",
3278 " candidate expects %d arguments, %d provided",
3279 want, have);
3282 /* Print information about one overload candidate CANDIDATE. MSGSTR
3283 is the text to print before the candidate itself.
3285 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3286 to have been run through gettext by the caller. This wart makes
3287 life simpler in print_z_candidates and for the translators. */
3289 static void
3290 print_z_candidate (location_t loc, const char *msgstr,
3291 struct z_candidate *candidate)
3293 const char *msg = (msgstr == NULL
3294 ? ""
3295 : ACONCAT ((msgstr, " ", NULL)));
3296 location_t cloc = location_of (candidate->fn);
3298 if (identifier_p (candidate->fn))
3300 cloc = loc;
3301 if (candidate->num_convs == 3)
3302 inform (cloc, "%s%D(%T, %T, %T) <built-in>", msg, candidate->fn,
3303 candidate->convs[0]->type,
3304 candidate->convs[1]->type,
3305 candidate->convs[2]->type);
3306 else if (candidate->num_convs == 2)
3307 inform (cloc, "%s%D(%T, %T) <built-in>", msg, candidate->fn,
3308 candidate->convs[0]->type,
3309 candidate->convs[1]->type);
3310 else
3311 inform (cloc, "%s%D(%T) <built-in>", msg, candidate->fn,
3312 candidate->convs[0]->type);
3314 else if (TYPE_P (candidate->fn))
3315 inform (cloc, "%s%T <conversion>", msg, candidate->fn);
3316 else if (candidate->viable == -1)
3317 inform (cloc, "%s%#D <near match>", msg, candidate->fn);
3318 else if (DECL_DELETED_FN (candidate->fn))
3319 inform (cloc, "%s%#D <deleted>", msg, candidate->fn);
3320 else
3321 inform (cloc, "%s%#D", msg, candidate->fn);
3322 /* Give the user some information about why this candidate failed. */
3323 if (candidate->reason != NULL)
3325 struct rejection_reason *r = candidate->reason;
3327 switch (r->code)
3329 case rr_arity:
3330 print_arity_information (cloc, r->u.arity.actual,
3331 r->u.arity.expected);
3332 break;
3333 case rr_arg_conversion:
3334 print_conversion_rejection (cloc, &r->u.conversion);
3335 break;
3336 case rr_bad_arg_conversion:
3337 print_conversion_rejection (cloc, &r->u.bad_conversion);
3338 break;
3339 case rr_explicit_conversion:
3340 inform (cloc, " return type %qT of explicit conversion function "
3341 "cannot be converted to %qT with a qualification "
3342 "conversion", r->u.conversion.from,
3343 r->u.conversion.to_type);
3344 break;
3345 case rr_template_conversion:
3346 inform (cloc, " conversion from return type %qT of template "
3347 "conversion function specialization to %qT is not an "
3348 "exact match", r->u.conversion.from,
3349 r->u.conversion.to_type);
3350 break;
3351 case rr_template_unification:
3352 /* We use template_unification_error_rejection if unification caused
3353 actual non-SFINAE errors, in which case we don't need to repeat
3354 them here. */
3355 if (r->u.template_unification.tmpl == NULL_TREE)
3357 inform (cloc, " substitution of deduced template arguments "
3358 "resulted in errors seen above");
3359 break;
3361 /* Re-run template unification with diagnostics. */
3362 inform (cloc, " template argument deduction/substitution failed:");
3363 fn_type_unification (r->u.template_unification.tmpl,
3364 r->u.template_unification.explicit_targs,
3365 (make_tree_vec
3366 (r->u.template_unification.num_targs)),
3367 r->u.template_unification.args,
3368 r->u.template_unification.nargs,
3369 r->u.template_unification.return_type,
3370 r->u.template_unification.strict,
3371 r->u.template_unification.flags,
3372 true, false);
3373 break;
3374 case rr_invalid_copy:
3375 inform (cloc,
3376 " a constructor taking a single argument of its own "
3377 "class type is invalid");
3378 break;
3379 case rr_none:
3380 default:
3381 /* This candidate didn't have any issues or we failed to
3382 handle a particular code. Either way... */
3383 gcc_unreachable ();
3388 static void
3389 print_z_candidates (location_t loc, struct z_candidate *candidates)
3391 struct z_candidate *cand1;
3392 struct z_candidate **cand2;
3393 int n_candidates;
3395 if (!candidates)
3396 return;
3398 /* Remove non-viable deleted candidates. */
3399 cand1 = candidates;
3400 for (cand2 = &cand1; *cand2; )
3402 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
3403 && !(*cand2)->viable
3404 && DECL_DELETED_FN ((*cand2)->fn))
3405 *cand2 = (*cand2)->next;
3406 else
3407 cand2 = &(*cand2)->next;
3409 /* ...if there are any non-deleted ones. */
3410 if (cand1)
3411 candidates = cand1;
3413 /* There may be duplicates in the set of candidates. We put off
3414 checking this condition as long as possible, since we have no way
3415 to eliminate duplicates from a set of functions in less than n^2
3416 time. Now we are about to emit an error message, so it is more
3417 permissible to go slowly. */
3418 for (cand1 = candidates; cand1; cand1 = cand1->next)
3420 tree fn = cand1->fn;
3421 /* Skip builtin candidates and conversion functions. */
3422 if (!DECL_P (fn))
3423 continue;
3424 cand2 = &cand1->next;
3425 while (*cand2)
3427 if (DECL_P ((*cand2)->fn)
3428 && equal_functions (fn, (*cand2)->fn))
3429 *cand2 = (*cand2)->next;
3430 else
3431 cand2 = &(*cand2)->next;
3435 for (n_candidates = 0, cand1 = candidates; cand1; cand1 = cand1->next)
3436 n_candidates++;
3438 for (; candidates; candidates = candidates->next)
3439 print_z_candidate (loc, "candidate:", candidates);
3442 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3443 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3444 the result of the conversion function to convert it to the final
3445 desired type. Merge the two sequences into a single sequence,
3446 and return the merged sequence. */
3448 static conversion *
3449 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
3451 conversion **t;
3452 bool bad = user_seq->bad_p;
3454 gcc_assert (user_seq->kind == ck_user);
3456 /* Find the end of the second conversion sequence. */
3457 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
3459 /* The entire sequence is a user-conversion sequence. */
3460 (*t)->user_conv_p = true;
3461 if (bad)
3462 (*t)->bad_p = true;
3465 /* Replace the identity conversion with the user conversion
3466 sequence. */
3467 *t = user_seq;
3469 return std_seq;
3472 /* Handle overload resolution for initializing an object of class type from
3473 an initializer list. First we look for a suitable constructor that
3474 takes a std::initializer_list; if we don't find one, we then look for a
3475 non-list constructor.
3477 Parameters are as for add_candidates, except that the arguments are in
3478 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
3479 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3481 static void
3482 add_list_candidates (tree fns, tree first_arg,
3483 tree init_list, tree totype,
3484 tree explicit_targs, bool template_only,
3485 tree conversion_path, tree access_path,
3486 int flags,
3487 struct z_candidate **candidates,
3488 tsubst_flags_t complain)
3490 vec<tree, va_gc> *args;
3492 gcc_assert (*candidates == NULL);
3494 /* We're looking for a ctor for list-initialization. */
3495 flags |= LOOKUP_LIST_INIT_CTOR;
3496 /* And we don't allow narrowing conversions. We also use this flag to
3497 avoid the copy constructor call for copy-list-initialization. */
3498 flags |= LOOKUP_NO_NARROWING;
3500 /* Always use the default constructor if the list is empty (DR 990). */
3501 if (CONSTRUCTOR_NELTS (init_list) == 0
3502 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
3504 /* If the class has a list ctor, try passing the list as a single
3505 argument first, but only consider list ctors. */
3506 else if (TYPE_HAS_LIST_CTOR (totype))
3508 flags |= LOOKUP_LIST_ONLY;
3509 args = make_tree_vector_single (init_list);
3510 add_candidates (fns, first_arg, args, NULL_TREE,
3511 explicit_targs, template_only, conversion_path,
3512 access_path, flags, candidates, complain);
3513 if (any_strictly_viable (*candidates))
3514 return;
3517 args = ctor_to_vec (init_list);
3519 /* We aren't looking for list-ctors anymore. */
3520 flags &= ~LOOKUP_LIST_ONLY;
3521 /* We allow more user-defined conversions within an init-list. */
3522 flags &= ~LOOKUP_NO_CONVERSION;
3524 add_candidates (fns, first_arg, args, NULL_TREE,
3525 explicit_targs, template_only, conversion_path,
3526 access_path, flags, candidates, complain);
3529 /* Returns the best overload candidate to perform the requested
3530 conversion. This function is used for three the overloading situations
3531 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3532 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3533 per [dcl.init.ref], so we ignore temporary bindings. */
3535 static struct z_candidate *
3536 build_user_type_conversion_1 (tree totype, tree expr, int flags,
3537 tsubst_flags_t complain)
3539 struct z_candidate *candidates, *cand;
3540 tree fromtype;
3541 tree ctors = NULL_TREE;
3542 tree conv_fns = NULL_TREE;
3543 conversion *conv = NULL;
3544 tree first_arg = NULL_TREE;
3545 vec<tree, va_gc> *args = NULL;
3546 bool any_viable_p;
3547 int convflags;
3549 if (!expr)
3550 return NULL;
3552 fromtype = TREE_TYPE (expr);
3554 /* We represent conversion within a hierarchy using RVALUE_CONV and
3555 BASE_CONV, as specified by [over.best.ics]; these become plain
3556 constructor calls, as specified in [dcl.init]. */
3557 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
3558 || !DERIVED_FROM_P (totype, fromtype));
3560 if (MAYBE_CLASS_TYPE_P (totype))
3561 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
3562 creating a garbage BASELINK; constructors can't be inherited. */
3563 ctors = lookup_fnfields_slot (totype, complete_ctor_identifier);
3565 if (MAYBE_CLASS_TYPE_P (fromtype))
3567 tree to_nonref = non_reference (totype);
3568 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
3569 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
3570 && DERIVED_FROM_P (to_nonref, fromtype)))
3572 /* [class.conv.fct] A conversion function is never used to
3573 convert a (possibly cv-qualified) object to the (possibly
3574 cv-qualified) same object type (or a reference to it), to a
3575 (possibly cv-qualified) base class of that type (or a
3576 reference to it)... */
3578 else
3579 conv_fns = lookup_conversions (fromtype);
3582 candidates = 0;
3583 flags |= LOOKUP_NO_CONVERSION;
3584 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3585 flags |= LOOKUP_NO_NARROWING;
3587 /* It's OK to bind a temporary for converting constructor arguments, but
3588 not in converting the return value of a conversion operator. */
3589 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION);
3590 flags &= ~LOOKUP_NO_TEMP_BIND;
3592 if (ctors)
3594 int ctorflags = flags;
3596 first_arg = build_dummy_object (totype);
3598 /* We should never try to call the abstract or base constructor
3599 from here. */
3600 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
3601 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)));
3603 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3605 /* List-initialization. */
3606 add_list_candidates (ctors, first_arg, expr, totype, NULL_TREE,
3607 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
3608 ctorflags, &candidates, complain);
3610 else
3612 args = make_tree_vector_single (expr);
3613 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
3614 TYPE_BINFO (totype), TYPE_BINFO (totype),
3615 ctorflags, &candidates, complain);
3618 for (cand = candidates; cand; cand = cand->next)
3620 cand->second_conv = build_identity_conv (totype, NULL_TREE);
3622 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
3623 set, then this is copy-initialization. In that case, "The
3624 result of the call is then used to direct-initialize the
3625 object that is the destination of the copy-initialization."
3626 [dcl.init]
3628 We represent this in the conversion sequence with an
3629 rvalue conversion, which means a constructor call. */
3630 if (TREE_CODE (totype) != REFERENCE_TYPE
3631 && !(convflags & LOOKUP_NO_TEMP_BIND))
3632 cand->second_conv
3633 = build_conv (ck_rvalue, totype, cand->second_conv);
3637 if (conv_fns)
3638 first_arg = expr;
3640 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
3642 tree conversion_path = TREE_PURPOSE (conv_fns);
3643 struct z_candidate *old_candidates;
3645 /* If we are called to convert to a reference type, we are trying to
3646 find a direct binding, so don't even consider temporaries. If
3647 we don't find a direct binding, the caller will try again to
3648 look for a temporary binding. */
3649 if (TREE_CODE (totype) == REFERENCE_TYPE)
3650 convflags |= LOOKUP_NO_TEMP_BIND;
3652 old_candidates = candidates;
3653 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3654 NULL_TREE, false,
3655 conversion_path, TYPE_BINFO (fromtype),
3656 flags, &candidates, complain);
3658 for (cand = candidates; cand != old_candidates; cand = cand->next)
3660 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
3661 conversion *ics
3662 = implicit_conversion (totype,
3663 rettype,
3665 /*c_cast_p=*/false, convflags,
3666 complain);
3668 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3669 copy-initialization. In that case, "The result of the
3670 call is then used to direct-initialize the object that is
3671 the destination of the copy-initialization." [dcl.init]
3673 We represent this in the conversion sequence with an
3674 rvalue conversion, which means a constructor call. But
3675 don't add a second rvalue conversion if there's already
3676 one there. Which there really shouldn't be, but it's
3677 harmless since we'd add it here anyway. */
3678 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3679 && !(convflags & LOOKUP_NO_TEMP_BIND))
3680 ics = build_conv (ck_rvalue, totype, ics);
3682 cand->second_conv = ics;
3684 if (!ics)
3686 cand->viable = 0;
3687 cand->reason = arg_conversion_rejection (NULL_TREE, -2,
3688 rettype, totype);
3690 else if (DECL_NONCONVERTING_P (cand->fn)
3691 && ics->rank > cr_exact)
3693 /* 13.3.1.5: For direct-initialization, those explicit
3694 conversion functions that are not hidden within S and
3695 yield type T or a type that can be converted to type T
3696 with a qualification conversion (4.4) are also candidate
3697 functions. */
3698 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
3699 I've raised this issue with the committee. --jason 9/2011 */
3700 cand->viable = -1;
3701 cand->reason = explicit_conversion_rejection (rettype, totype);
3703 else if (cand->viable == 1 && ics->bad_p)
3705 cand->viable = -1;
3706 cand->reason
3707 = bad_arg_conversion_rejection (NULL_TREE, -2,
3708 rettype, totype);
3710 else if (primary_template_instantiation_p (cand->fn)
3711 && ics->rank > cr_exact)
3713 /* 13.3.3.1.2: If the user-defined conversion is specified by
3714 a specialization of a conversion function template, the
3715 second standard conversion sequence shall have exact match
3716 rank. */
3717 cand->viable = -1;
3718 cand->reason = template_conversion_rejection (rettype, totype);
3723 candidates = splice_viable (candidates, false, &any_viable_p);
3724 if (!any_viable_p)
3726 if (args)
3727 release_tree_vector (args);
3728 return NULL;
3731 cand = tourney (candidates, complain);
3732 if (cand == 0)
3734 if (complain & tf_error)
3736 error ("conversion from %qT to %qT is ambiguous",
3737 fromtype, totype);
3738 print_z_candidates (location_of (expr), candidates);
3741 cand = candidates; /* any one will do */
3742 cand->second_conv = build_ambiguous_conv (totype, expr);
3743 cand->second_conv->user_conv_p = true;
3744 if (!any_strictly_viable (candidates))
3745 cand->second_conv->bad_p = true;
3746 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3747 ambiguous conversion is no worse than another user-defined
3748 conversion. */
3750 return cand;
3753 tree convtype;
3754 if (!DECL_CONSTRUCTOR_P (cand->fn))
3755 convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
3756 else if (cand->second_conv->kind == ck_rvalue)
3757 /* DR 5: [in the first step of copy-initialization]...if the function
3758 is a constructor, the call initializes a temporary of the
3759 cv-unqualified version of the destination type. */
3760 convtype = cv_unqualified (totype);
3761 else
3762 convtype = totype;
3763 /* Build the user conversion sequence. */
3764 conv = build_conv
3765 (ck_user,
3766 convtype,
3767 build_identity_conv (TREE_TYPE (expr), expr));
3768 conv->cand = cand;
3769 if (cand->viable == -1)
3770 conv->bad_p = true;
3772 /* Remember that this was a list-initialization. */
3773 if (flags & LOOKUP_NO_NARROWING)
3774 conv->check_narrowing = true;
3776 /* Combine it with the second conversion sequence. */
3777 cand->second_conv = merge_conversion_sequences (conv,
3778 cand->second_conv);
3780 return cand;
3783 /* Wrapper for above. */
3785 tree
3786 build_user_type_conversion (tree totype, tree expr, int flags,
3787 tsubst_flags_t complain)
3789 struct z_candidate *cand;
3790 tree ret;
3792 bool subtime = timevar_cond_start (TV_OVERLOAD);
3793 cand = build_user_type_conversion_1 (totype, expr, flags, complain);
3795 if (cand)
3797 if (cand->second_conv->kind == ck_ambig)
3798 ret = error_mark_node;
3799 else
3801 expr = convert_like (cand->second_conv, expr, complain);
3802 ret = convert_from_reference (expr);
3805 else
3806 ret = NULL_TREE;
3808 timevar_cond_stop (TV_OVERLOAD, subtime);
3809 return ret;
3812 /* Subroutine of convert_nontype_argument.
3814 EXPR is an argument for a template non-type parameter of integral or
3815 enumeration type. Do any necessary conversions (that are permitted for
3816 non-type arguments) to convert it to the parameter type.
3818 If conversion is successful, returns the converted expression;
3819 otherwise, returns error_mark_node. */
3821 tree
3822 build_integral_nontype_arg_conv (tree type, tree expr, tsubst_flags_t complain)
3824 conversion *conv;
3825 void *p;
3826 tree t;
3827 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
3829 if (error_operand_p (expr))
3830 return error_mark_node;
3832 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
3834 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3835 p = conversion_obstack_alloc (0);
3837 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
3838 /*c_cast_p=*/false,
3839 LOOKUP_IMPLICIT, complain);
3841 /* for a non-type template-parameter of integral or
3842 enumeration type, integral promotions (4.5) and integral
3843 conversions (4.7) are applied. */
3844 /* It should be sufficient to check the outermost conversion step, since
3845 there are no qualification conversions to integer type. */
3846 if (conv)
3847 switch (conv->kind)
3849 /* A conversion function is OK. If it isn't constexpr, we'll
3850 complain later that the argument isn't constant. */
3851 case ck_user:
3852 /* The lvalue-to-rvalue conversion is OK. */
3853 case ck_rvalue:
3854 case ck_identity:
3855 break;
3857 case ck_std:
3858 t = next_conversion (conv)->type;
3859 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t))
3860 break;
3862 if (complain & tf_error)
3863 error_at (loc, "conversion from %qT to %qT not considered for "
3864 "non-type template argument", t, type);
3865 /* and fall through. */
3867 default:
3868 conv = NULL;
3869 break;
3872 if (conv)
3873 expr = convert_like (conv, expr, complain);
3874 else
3875 expr = error_mark_node;
3877 /* Free all the conversions we allocated. */
3878 obstack_free (&conversion_obstack, p);
3880 return expr;
3883 /* Do any initial processing on the arguments to a function call. */
3885 static vec<tree, va_gc> *
3886 resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
3888 unsigned int ix;
3889 tree arg;
3891 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
3893 if (error_operand_p (arg))
3894 return NULL;
3895 else if (VOID_TYPE_P (TREE_TYPE (arg)))
3897 if (complain & tf_error)
3898 error ("invalid use of void expression");
3899 return NULL;
3901 else if (invalid_nonstatic_memfn_p (arg, complain))
3902 return NULL;
3904 return args;
3907 /* Perform overload resolution on FN, which is called with the ARGS.
3909 Return the candidate function selected by overload resolution, or
3910 NULL if the event that overload resolution failed. In the case
3911 that overload resolution fails, *CANDIDATES will be the set of
3912 candidates considered, and ANY_VIABLE_P will be set to true or
3913 false to indicate whether or not any of the candidates were
3914 viable.
3916 The ARGS should already have gone through RESOLVE_ARGS before this
3917 function is called. */
3919 static struct z_candidate *
3920 perform_overload_resolution (tree fn,
3921 const vec<tree, va_gc> *args,
3922 struct z_candidate **candidates,
3923 bool *any_viable_p, tsubst_flags_t complain)
3925 struct z_candidate *cand;
3926 tree explicit_targs;
3927 int template_only;
3929 bool subtime = timevar_cond_start (TV_OVERLOAD);
3931 explicit_targs = NULL_TREE;
3932 template_only = 0;
3934 *candidates = NULL;
3935 *any_viable_p = true;
3937 /* Check FN. */
3938 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
3939 || TREE_CODE (fn) == TEMPLATE_DECL
3940 || TREE_CODE (fn) == OVERLOAD
3941 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
3943 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3945 explicit_targs = TREE_OPERAND (fn, 1);
3946 fn = TREE_OPERAND (fn, 0);
3947 template_only = 1;
3950 /* Add the various candidate functions. */
3951 add_candidates (fn, NULL_TREE, args, NULL_TREE,
3952 explicit_targs, template_only,
3953 /*conversion_path=*/NULL_TREE,
3954 /*access_path=*/NULL_TREE,
3955 LOOKUP_NORMAL,
3956 candidates, complain);
3958 *candidates = splice_viable (*candidates, false, any_viable_p);
3959 if (*any_viable_p)
3960 cand = tourney (*candidates, complain);
3961 else
3962 cand = NULL;
3964 timevar_cond_stop (TV_OVERLOAD, subtime);
3965 return cand;
3968 /* Print an error message about being unable to build a call to FN with
3969 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
3970 be located; CANDIDATES is a possibly empty list of such
3971 functions. */
3973 static void
3974 print_error_for_call_failure (tree fn, vec<tree, va_gc> *args,
3975 struct z_candidate *candidates)
3977 tree name = DECL_NAME (OVL_CURRENT (fn));
3978 location_t loc = location_of (name);
3980 if (!any_strictly_viable (candidates))
3981 error_at (loc, "no matching function for call to %<%D(%A)%>",
3982 name, build_tree_list_vec (args));
3983 else
3984 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
3985 name, build_tree_list_vec (args));
3986 if (candidates)
3987 print_z_candidates (loc, candidates);
3990 /* Return an expression for a call to FN (a namespace-scope function,
3991 or a static member function) with the ARGS. This may change
3992 ARGS. */
3994 tree
3995 build_new_function_call (tree fn, vec<tree, va_gc> **args, bool koenig_p,
3996 tsubst_flags_t complain)
3998 struct z_candidate *candidates, *cand;
3999 bool any_viable_p;
4000 void *p;
4001 tree result;
4003 if (args != NULL && *args != NULL)
4005 *args = resolve_args (*args, complain);
4006 if (*args == NULL)
4007 return error_mark_node;
4010 if (flag_tm)
4011 tm_malloc_replacement (fn);
4013 /* If this function was found without using argument dependent
4014 lookup, then we want to ignore any undeclared friend
4015 functions. */
4016 if (!koenig_p)
4018 tree orig_fn = fn;
4020 fn = remove_hidden_names (fn);
4021 if (!fn)
4023 if (complain & tf_error)
4024 print_error_for_call_failure (orig_fn, *args, NULL);
4025 return error_mark_node;
4029 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4030 p = conversion_obstack_alloc (0);
4032 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
4033 complain);
4035 if (!cand)
4037 if (complain & tf_error)
4039 if (!any_viable_p && candidates && ! candidates->next
4040 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
4041 return cp_build_function_call_vec (candidates->fn, args, complain);
4042 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4043 fn = TREE_OPERAND (fn, 0);
4044 print_error_for_call_failure (fn, *args, candidates);
4046 result = error_mark_node;
4048 else
4050 int flags = LOOKUP_NORMAL;
4051 /* If fn is template_id_expr, the call has explicit template arguments
4052 (e.g. func<int>(5)), communicate this info to build_over_call
4053 through flags so that later we can use it to decide whether to warn
4054 about peculiar null pointer conversion. */
4055 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4056 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
4057 result = build_over_call (cand, flags, complain);
4060 /* Free all the conversions we allocated. */
4061 obstack_free (&conversion_obstack, p);
4063 return result;
4066 /* Build a call to a global operator new. FNNAME is the name of the
4067 operator (either "operator new" or "operator new[]") and ARGS are
4068 the arguments provided. This may change ARGS. *SIZE points to the
4069 total number of bytes required by the allocation, and is updated if
4070 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
4071 be used. If this function determines that no cookie should be
4072 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
4073 is not NULL_TREE, it is evaluated before calculating the final
4074 array size, and if it fails, the array size is replaced with
4075 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
4076 is non-NULL, it will be set, upon return, to the allocation
4077 function called. */
4079 tree
4080 build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
4081 tree *size, tree *cookie_size, tree size_check,
4082 tree *fn, tsubst_flags_t complain)
4084 tree original_size = *size;
4085 tree fns;
4086 struct z_candidate *candidates;
4087 struct z_candidate *cand;
4088 bool any_viable_p;
4090 if (fn)
4091 *fn = NULL_TREE;
4092 /* Set to (size_t)-1 if the size check fails. */
4093 if (size_check != NULL_TREE)
4095 tree errval = TYPE_MAX_VALUE (sizetype);
4096 if (cxx_dialect >= cxx11 && flag_exceptions)
4097 errval = throw_bad_array_new_length ();
4098 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4099 original_size, errval);
4101 vec_safe_insert (*args, 0, *size);
4102 *args = resolve_args (*args, complain);
4103 if (*args == NULL)
4104 return error_mark_node;
4106 /* Based on:
4108 [expr.new]
4110 If this lookup fails to find the name, or if the allocated type
4111 is not a class type, the allocation function's name is looked
4112 up in the global scope.
4114 we disregard block-scope declarations of "operator new". */
4115 fns = lookup_function_nonclass (fnname, *args, /*block_p=*/false);
4117 /* Figure out what function is being called. */
4118 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
4119 complain);
4121 /* If no suitable function could be found, issue an error message
4122 and give up. */
4123 if (!cand)
4125 if (complain & tf_error)
4126 print_error_for_call_failure (fns, *args, candidates);
4127 return error_mark_node;
4130 /* If a cookie is required, add some extra space. Whether
4131 or not a cookie is required cannot be determined until
4132 after we know which function was called. */
4133 if (*cookie_size)
4135 bool use_cookie = true;
4136 tree arg_types;
4138 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
4139 /* Skip the size_t parameter. */
4140 arg_types = TREE_CHAIN (arg_types);
4141 /* Check the remaining parameters (if any). */
4142 if (arg_types
4143 && TREE_CHAIN (arg_types) == void_list_node
4144 && same_type_p (TREE_VALUE (arg_types),
4145 ptr_type_node))
4146 use_cookie = false;
4147 /* If we need a cookie, adjust the number of bytes allocated. */
4148 if (use_cookie)
4150 /* Update the total size. */
4151 *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
4152 /* Set to (size_t)-1 if the size check fails. */
4153 gcc_assert (size_check != NULL_TREE);
4154 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4155 *size, TYPE_MAX_VALUE (sizetype));
4156 /* Update the argument list to reflect the adjusted size. */
4157 (**args)[0] = *size;
4159 else
4160 *cookie_size = NULL_TREE;
4163 /* Tell our caller which function we decided to call. */
4164 if (fn)
4165 *fn = cand->fn;
4167 /* Build the CALL_EXPR. */
4168 return build_over_call (cand, LOOKUP_NORMAL, complain);
4171 /* Build a new call to operator(). This may change ARGS. */
4173 static tree
4174 build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4176 struct z_candidate *candidates = 0, *cand;
4177 tree fns, convs, first_mem_arg = NULL_TREE;
4178 tree type = TREE_TYPE (obj);
4179 bool any_viable_p;
4180 tree result = NULL_TREE;
4181 void *p;
4183 if (error_operand_p (obj))
4184 return error_mark_node;
4186 obj = prep_operand (obj);
4188 if (TYPE_PTRMEMFUNC_P (type))
4190 if (complain & tf_error)
4191 /* It's no good looking for an overloaded operator() on a
4192 pointer-to-member-function. */
4193 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
4194 return error_mark_node;
4197 if (TYPE_BINFO (type))
4199 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
4200 if (fns == error_mark_node)
4201 return error_mark_node;
4203 else
4204 fns = NULL_TREE;
4206 if (args != NULL && *args != NULL)
4208 *args = resolve_args (*args, complain);
4209 if (*args == NULL)
4210 return error_mark_node;
4213 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4214 p = conversion_obstack_alloc (0);
4216 if (fns)
4218 first_mem_arg = obj;
4220 add_candidates (BASELINK_FUNCTIONS (fns),
4221 first_mem_arg, *args, NULL_TREE,
4222 NULL_TREE, false,
4223 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
4224 LOOKUP_NORMAL, &candidates, complain);
4227 convs = lookup_conversions (type);
4229 for (; convs; convs = TREE_CHAIN (convs))
4231 tree fns = TREE_VALUE (convs);
4232 tree totype = TREE_TYPE (convs);
4234 if (TYPE_PTRFN_P (totype)
4235 || TYPE_REFFN_P (totype)
4236 || (TREE_CODE (totype) == REFERENCE_TYPE
4237 && TYPE_PTRFN_P (TREE_TYPE (totype))))
4238 for (; fns; fns = OVL_NEXT (fns))
4240 tree fn = OVL_CURRENT (fns);
4242 if (DECL_NONCONVERTING_P (fn))
4243 continue;
4245 if (TREE_CODE (fn) == TEMPLATE_DECL)
4246 add_template_conv_candidate
4247 (&candidates, fn, obj, NULL_TREE, *args, totype,
4248 /*access_path=*/NULL_TREE,
4249 /*conversion_path=*/NULL_TREE, complain);
4250 else
4251 add_conv_candidate (&candidates, fn, obj, NULL_TREE,
4252 *args, /*conversion_path=*/NULL_TREE,
4253 /*access_path=*/NULL_TREE, complain);
4257 /* Be strict here because if we choose a bad conversion candidate, the
4258 errors we get won't mention the call context. */
4259 candidates = splice_viable (candidates, true, &any_viable_p);
4260 if (!any_viable_p)
4262 if (complain & tf_error)
4264 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
4265 build_tree_list_vec (*args));
4266 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4268 result = error_mark_node;
4270 else
4272 cand = tourney (candidates, complain);
4273 if (cand == 0)
4275 if (complain & tf_error)
4277 error ("call of %<(%T) (%A)%> is ambiguous",
4278 TREE_TYPE (obj), build_tree_list_vec (*args));
4279 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4281 result = error_mark_node;
4283 /* Since cand->fn will be a type, not a function, for a conversion
4284 function, we must be careful not to unconditionally look at
4285 DECL_NAME here. */
4286 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
4287 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
4288 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4289 else
4291 obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1,
4292 complain);
4293 obj = convert_from_reference (obj);
4294 result = cp_build_function_call_vec (obj, args, complain);
4298 /* Free all the conversions we allocated. */
4299 obstack_free (&conversion_obstack, p);
4301 return result;
4304 /* Wrapper for above. */
4306 tree
4307 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4309 tree ret;
4310 bool subtime = timevar_cond_start (TV_OVERLOAD);
4311 ret = build_op_call_1 (obj, args, complain);
4312 timevar_cond_stop (TV_OVERLOAD, subtime);
4313 return ret;
4316 /* Called by op_error to prepare format strings suitable for the error
4317 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4318 and a suffix (controlled by NTYPES). */
4320 static const char *
4321 op_error_string (const char *errmsg, int ntypes, bool match)
4323 const char *msg;
4325 const char *msgp = concat (match ? G_("ambiguous overload for ")
4326 : G_("no match for "), errmsg, NULL);
4328 if (ntypes == 3)
4329 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
4330 else if (ntypes == 2)
4331 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
4332 else
4333 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
4335 return msg;
4338 static void
4339 op_error (location_t loc, enum tree_code code, enum tree_code code2,
4340 tree arg1, tree arg2, tree arg3, bool match)
4342 const char *opname;
4344 if (code == MODIFY_EXPR)
4345 opname = assignment_operator_name_info[code2].name;
4346 else
4347 opname = operator_name_info[code].name;
4349 switch (code)
4351 case COND_EXPR:
4352 if (flag_diagnostics_show_caret)
4353 error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
4354 3, match),
4355 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4356 else
4357 error_at (loc, op_error_string (G_("ternary %<operator?:%> "
4358 "in %<%E ? %E : %E%>"), 3, match),
4359 arg1, arg2, arg3,
4360 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4361 break;
4363 case POSTINCREMENT_EXPR:
4364 case POSTDECREMENT_EXPR:
4365 if (flag_diagnostics_show_caret)
4366 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4367 opname, TREE_TYPE (arg1));
4368 else
4369 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4370 1, match),
4371 opname, arg1, opname, TREE_TYPE (arg1));
4372 break;
4374 case ARRAY_REF:
4375 if (flag_diagnostics_show_caret)
4376 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
4377 TREE_TYPE (arg1), TREE_TYPE (arg2));
4378 else
4379 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
4380 2, match),
4381 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
4382 break;
4384 case REALPART_EXPR:
4385 case IMAGPART_EXPR:
4386 if (flag_diagnostics_show_caret)
4387 error_at (loc, op_error_string (G_("%qs"), 1, match),
4388 opname, TREE_TYPE (arg1));
4389 else
4390 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
4391 opname, opname, arg1, TREE_TYPE (arg1));
4392 break;
4394 default:
4395 if (arg2)
4396 if (flag_diagnostics_show_caret)
4397 error_at (loc, op_error_string (G_("%<operator%s%>"), 2, match),
4398 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
4399 else
4400 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
4401 2, match),
4402 opname, arg1, opname, arg2,
4403 TREE_TYPE (arg1), TREE_TYPE (arg2));
4404 else
4405 if (flag_diagnostics_show_caret)
4406 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4407 opname, TREE_TYPE (arg1));
4408 else
4409 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
4410 1, match),
4411 opname, opname, arg1, TREE_TYPE (arg1));
4412 break;
4416 /* Return the implicit conversion sequence that could be used to
4417 convert E1 to E2 in [expr.cond]. */
4419 static conversion *
4420 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
4422 tree t1 = non_reference (TREE_TYPE (e1));
4423 tree t2 = non_reference (TREE_TYPE (e2));
4424 conversion *conv;
4425 bool good_base;
4427 /* [expr.cond]
4429 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
4430 implicitly converted (clause _conv_) to the type "lvalue reference to
4431 T2", subject to the constraint that in the conversion the
4432 reference must bind directly (_dcl.init.ref_) to an lvalue.
4434 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
4435 implicitly converted to the type "rvalue reference to T2", subject to
4436 the constraint that the reference must bind directly. */
4437 if (lvalue_or_rvalue_with_address_p (e2))
4439 tree rtype = cp_build_reference_type (t2, !real_lvalue_p (e2));
4440 conv = implicit_conversion (rtype,
4443 /*c_cast_p=*/false,
4444 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
4445 |LOOKUP_ONLYCONVERTING,
4446 complain);
4447 if (conv && !conv->bad_p)
4448 return conv;
4451 /* If E2 is a prvalue or if neither of the conversions above can be done
4452 and at least one of the operands has (possibly cv-qualified) class
4453 type: */
4454 if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
4455 return NULL;
4457 /* [expr.cond]
4459 If E1 and E2 have class type, and the underlying class types are
4460 the same or one is a base class of the other: E1 can be converted
4461 to match E2 if the class of T2 is the same type as, or a base
4462 class of, the class of T1, and the cv-qualification of T2 is the
4463 same cv-qualification as, or a greater cv-qualification than, the
4464 cv-qualification of T1. If the conversion is applied, E1 is
4465 changed to an rvalue of type T2 that still refers to the original
4466 source class object (or the appropriate subobject thereof). */
4467 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
4468 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
4470 if (good_base && at_least_as_qualified_p (t2, t1))
4472 conv = build_identity_conv (t1, e1);
4473 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
4474 TYPE_MAIN_VARIANT (t2)))
4475 conv = build_conv (ck_base, t2, conv);
4476 else
4477 conv = build_conv (ck_rvalue, t2, conv);
4478 return conv;
4480 else
4481 return NULL;
4483 else
4484 /* [expr.cond]
4486 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
4487 converted to the type that expression E2 would have if E2 were
4488 converted to an rvalue (or the type it has, if E2 is an rvalue). */
4489 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
4490 LOOKUP_IMPLICIT, complain);
4493 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
4494 arguments to the conditional expression. */
4496 static tree
4497 build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
4498 tsubst_flags_t complain)
4500 tree arg2_type;
4501 tree arg3_type;
4502 tree result = NULL_TREE;
4503 tree result_type = NULL_TREE;
4504 bool lvalue_p = true;
4505 struct z_candidate *candidates = 0;
4506 struct z_candidate *cand;
4507 void *p;
4508 tree orig_arg2, orig_arg3;
4510 /* As a G++ extension, the second argument to the conditional can be
4511 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
4512 c'.) If the second operand is omitted, make sure it is
4513 calculated only once. */
4514 if (!arg2)
4516 if (complain & tf_error)
4517 pedwarn (loc, OPT_Wpedantic,
4518 "ISO C++ forbids omitting the middle term of a ?: expression");
4520 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
4521 if (real_lvalue_p (arg1))
4522 arg2 = arg1 = stabilize_reference (arg1);
4523 else
4524 arg2 = arg1 = save_expr (arg1);
4527 /* If something has already gone wrong, just pass that fact up the
4528 tree. */
4529 if (error_operand_p (arg1)
4530 || error_operand_p (arg2)
4531 || error_operand_p (arg3))
4532 return error_mark_node;
4534 orig_arg2 = arg2;
4535 orig_arg3 = arg3;
4537 if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
4539 arg1 = force_rvalue (arg1, complain);
4540 arg2 = force_rvalue (arg2, complain);
4541 arg3 = force_rvalue (arg3, complain);
4543 /* force_rvalue can return error_mark on valid arguments. */
4544 if (error_operand_p (arg1)
4545 || error_operand_p (arg2)
4546 || error_operand_p (arg3))
4547 return error_mark_node;
4549 tree arg1_type = TREE_TYPE (arg1);
4550 arg2_type = TREE_TYPE (arg2);
4551 arg3_type = TREE_TYPE (arg3);
4553 if (TREE_CODE (arg2_type) != VECTOR_TYPE
4554 && TREE_CODE (arg3_type) != VECTOR_TYPE)
4556 /* Rely on the error messages of the scalar version. */
4557 tree scal = build_conditional_expr_1 (loc, integer_one_node,
4558 orig_arg2, orig_arg3, complain);
4559 if (scal == error_mark_node)
4560 return error_mark_node;
4561 tree stype = TREE_TYPE (scal);
4562 tree ctype = TREE_TYPE (arg1_type);
4563 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
4564 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
4566 if (complain & tf_error)
4567 error_at (loc, "inferred scalar type %qT is not an integer or "
4568 "floating point type of the same size as %qT", stype,
4569 COMPARISON_CLASS_P (arg1)
4570 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4571 : ctype);
4572 return error_mark_node;
4575 tree vtype = build_opaque_vector_type (stype,
4576 TYPE_VECTOR_SUBPARTS (arg1_type));
4577 /* We could pass complain & tf_warning to unsafe_conversion_p,
4578 but the warnings (like Wsign-conversion) have already been
4579 given by the scalar build_conditional_expr_1. We still check
4580 unsafe_conversion_p to forbid truncating long long -> float. */
4581 if (unsafe_conversion_p (loc, stype, arg2, false))
4583 if (complain & tf_error)
4584 error_at (loc, "conversion of scalar %qT to vector %qT "
4585 "involves truncation", arg2_type, vtype);
4586 return error_mark_node;
4588 if (unsafe_conversion_p (loc, stype, arg3, false))
4590 if (complain & tf_error)
4591 error_at (loc, "conversion of scalar %qT to vector %qT "
4592 "involves truncation", arg3_type, vtype);
4593 return error_mark_node;
4596 arg2 = cp_convert (stype, arg2, complain);
4597 arg2 = save_expr (arg2);
4598 arg2 = build_vector_from_val (vtype, arg2);
4599 arg2_type = vtype;
4600 arg3 = cp_convert (stype, arg3, complain);
4601 arg3 = save_expr (arg3);
4602 arg3 = build_vector_from_val (vtype, arg3);
4603 arg3_type = vtype;
4606 if ((TREE_CODE (arg2_type) == VECTOR_TYPE)
4607 != (TREE_CODE (arg3_type) == VECTOR_TYPE))
4609 enum stv_conv convert_flag =
4610 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
4611 complain & tf_error);
4613 switch (convert_flag)
4615 case stv_error:
4616 return error_mark_node;
4617 case stv_firstarg:
4619 arg2 = save_expr (arg2);
4620 arg2 = convert (TREE_TYPE (arg3_type), arg2);
4621 arg2 = build_vector_from_val (arg3_type, arg2);
4622 arg2_type = TREE_TYPE (arg2);
4623 break;
4625 case stv_secondarg:
4627 arg3 = save_expr (arg3);
4628 arg3 = convert (TREE_TYPE (arg2_type), arg3);
4629 arg3 = build_vector_from_val (arg2_type, arg3);
4630 arg3_type = TREE_TYPE (arg3);
4631 break;
4633 default:
4634 break;
4638 if (!same_type_p (arg2_type, arg3_type)
4639 || TYPE_VECTOR_SUBPARTS (arg1_type)
4640 != TYPE_VECTOR_SUBPARTS (arg2_type)
4641 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
4643 if (complain & tf_error)
4644 error_at (loc,
4645 "incompatible vector types in conditional expression: "
4646 "%qT, %qT and %qT", TREE_TYPE (arg1),
4647 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
4648 return error_mark_node;
4651 if (!COMPARISON_CLASS_P (arg1))
4652 arg1 = cp_build_binary_op (loc, NE_EXPR, arg1,
4653 build_zero_cst (arg1_type), complain);
4654 return fold_build3 (VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
4657 /* [expr.cond]
4659 The first expression is implicitly converted to bool (clause
4660 _conv_). */
4661 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
4662 LOOKUP_NORMAL);
4663 if (error_operand_p (arg1))
4664 return error_mark_node;
4666 /* [expr.cond]
4668 If either the second or the third operand has type (possibly
4669 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
4670 array-to-pointer (_conv.array_), and function-to-pointer
4671 (_conv.func_) standard conversions are performed on the second
4672 and third operands. */
4673 arg2_type = unlowered_expr_type (arg2);
4674 arg3_type = unlowered_expr_type (arg3);
4675 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
4677 /* Do the conversions. We don't these for `void' type arguments
4678 since it can't have any effect and since decay_conversion
4679 does not handle that case gracefully. */
4680 if (!VOID_TYPE_P (arg2_type))
4681 arg2 = decay_conversion (arg2, complain);
4682 if (!VOID_TYPE_P (arg3_type))
4683 arg3 = decay_conversion (arg3, complain);
4684 arg2_type = TREE_TYPE (arg2);
4685 arg3_type = TREE_TYPE (arg3);
4687 /* [expr.cond]
4689 One of the following shall hold:
4691 --The second or the third operand (but not both) is a
4692 throw-expression (_except.throw_); the result is of the
4693 type of the other and is an rvalue.
4695 --Both the second and the third operands have type void; the
4696 result is of type void and is an rvalue.
4698 We must avoid calling force_rvalue for expressions of type
4699 "void" because it will complain that their value is being
4700 used. */
4701 if (TREE_CODE (arg2) == THROW_EXPR
4702 && TREE_CODE (arg3) != THROW_EXPR)
4704 if (!VOID_TYPE_P (arg3_type))
4706 arg3 = force_rvalue (arg3, complain);
4707 if (arg3 == error_mark_node)
4708 return error_mark_node;
4710 arg3_type = TREE_TYPE (arg3);
4711 result_type = arg3_type;
4713 else if (TREE_CODE (arg2) != THROW_EXPR
4714 && TREE_CODE (arg3) == THROW_EXPR)
4716 if (!VOID_TYPE_P (arg2_type))
4718 arg2 = force_rvalue (arg2, complain);
4719 if (arg2 == error_mark_node)
4720 return error_mark_node;
4722 arg2_type = TREE_TYPE (arg2);
4723 result_type = arg2_type;
4725 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
4726 result_type = void_type_node;
4727 else
4729 if (complain & tf_error)
4731 if (VOID_TYPE_P (arg2_type))
4732 error_at (EXPR_LOC_OR_LOC (arg3, loc),
4733 "second operand to the conditional operator "
4734 "is of type %<void%>, but the third operand is "
4735 "neither a throw-expression nor of type %<void%>");
4736 else
4737 error_at (EXPR_LOC_OR_LOC (arg2, loc),
4738 "third operand to the conditional operator "
4739 "is of type %<void%>, but the second operand is "
4740 "neither a throw-expression nor of type %<void%>");
4742 return error_mark_node;
4745 lvalue_p = false;
4746 goto valid_operands;
4748 /* [expr.cond]
4750 Otherwise, if the second and third operand have different types,
4751 and either has (possibly cv-qualified) class type, or if both are
4752 glvalues of the same value category and the same type except for
4753 cv-qualification, an attempt is made to convert each of those operands
4754 to the type of the other. */
4755 else if (!same_type_p (arg2_type, arg3_type)
4756 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
4757 || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
4758 arg3_type)
4759 && lvalue_or_rvalue_with_address_p (arg2)
4760 && lvalue_or_rvalue_with_address_p (arg3)
4761 && real_lvalue_p (arg2) == real_lvalue_p (arg3))))
4763 conversion *conv2;
4764 conversion *conv3;
4766 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4767 p = conversion_obstack_alloc (0);
4769 conv2 = conditional_conversion (arg2, arg3, complain);
4770 conv3 = conditional_conversion (arg3, arg2, complain);
4772 /* [expr.cond]
4774 If both can be converted, or one can be converted but the
4775 conversion is ambiguous, the program is ill-formed. If
4776 neither can be converted, the operands are left unchanged and
4777 further checking is performed as described below. If exactly
4778 one conversion is possible, that conversion is applied to the
4779 chosen operand and the converted operand is used in place of
4780 the original operand for the remainder of this section. */
4781 if ((conv2 && !conv2->bad_p
4782 && conv3 && !conv3->bad_p)
4783 || (conv2 && conv2->kind == ck_ambig)
4784 || (conv3 && conv3->kind == ck_ambig))
4786 if (complain & tf_error)
4788 error_at (loc, "operands to ?: have different types %qT and %qT",
4789 arg2_type, arg3_type);
4790 if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
4791 inform (loc, " and each type can be converted to the other");
4792 else if (conv2 && conv2->kind == ck_ambig)
4793 convert_like (conv2, arg2, complain);
4794 else
4795 convert_like (conv3, arg3, complain);
4797 result = error_mark_node;
4799 else if (conv2 && !conv2->bad_p)
4801 arg2 = convert_like (conv2, arg2, complain);
4802 arg2 = convert_from_reference (arg2);
4803 arg2_type = TREE_TYPE (arg2);
4804 /* Even if CONV2 is a valid conversion, the result of the
4805 conversion may be invalid. For example, if ARG3 has type
4806 "volatile X", and X does not have a copy constructor
4807 accepting a "volatile X&", then even if ARG2 can be
4808 converted to X, the conversion will fail. */
4809 if (error_operand_p (arg2))
4810 result = error_mark_node;
4812 else if (conv3 && !conv3->bad_p)
4814 arg3 = convert_like (conv3, arg3, complain);
4815 arg3 = convert_from_reference (arg3);
4816 arg3_type = TREE_TYPE (arg3);
4817 if (error_operand_p (arg3))
4818 result = error_mark_node;
4821 /* Free all the conversions we allocated. */
4822 obstack_free (&conversion_obstack, p);
4824 if (result)
4825 return result;
4827 /* If, after the conversion, both operands have class type,
4828 treat the cv-qualification of both operands as if it were the
4829 union of the cv-qualification of the operands.
4831 The standard is not clear about what to do in this
4832 circumstance. For example, if the first operand has type
4833 "const X" and the second operand has a user-defined
4834 conversion to "volatile X", what is the type of the second
4835 operand after this step? Making it be "const X" (matching
4836 the first operand) seems wrong, as that discards the
4837 qualification without actually performing a copy. Leaving it
4838 as "volatile X" seems wrong as that will result in the
4839 conditional expression failing altogether, even though,
4840 according to this step, the one operand could be converted to
4841 the type of the other. */
4842 if (((conv2 && !conv2->bad_p)
4843 || (conv3 && !conv3->bad_p))
4844 && CLASS_TYPE_P (arg2_type)
4845 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
4846 arg2_type = arg3_type =
4847 cp_build_qualified_type (arg2_type,
4848 cp_type_quals (arg2_type)
4849 | cp_type_quals (arg3_type));
4852 /* [expr.cond]
4854 If the second and third operands are glvalues of the same value
4855 category and have the same type, the result is of that type and
4856 value category. */
4857 if (((real_lvalue_p (arg2) && real_lvalue_p (arg3))
4858 || (xvalue_p (arg2) && xvalue_p (arg3)))
4859 && same_type_p (arg2_type, arg3_type))
4861 result_type = arg2_type;
4862 arg2 = mark_lvalue_use (arg2);
4863 arg3 = mark_lvalue_use (arg3);
4864 goto valid_operands;
4867 /* [expr.cond]
4869 Otherwise, the result is an rvalue. If the second and third
4870 operand do not have the same type, and either has (possibly
4871 cv-qualified) class type, overload resolution is used to
4872 determine the conversions (if any) to be applied to the operands
4873 (_over.match.oper_, _over.built_). */
4874 lvalue_p = false;
4875 if (!same_type_p (arg2_type, arg3_type)
4876 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
4878 tree args[3];
4879 conversion *conv;
4880 bool any_viable_p;
4882 /* Rearrange the arguments so that add_builtin_candidate only has
4883 to know about two args. In build_builtin_candidate, the
4884 arguments are unscrambled. */
4885 args[0] = arg2;
4886 args[1] = arg3;
4887 args[2] = arg1;
4888 add_builtin_candidates (&candidates,
4889 COND_EXPR,
4890 NOP_EXPR,
4891 ansi_opname (COND_EXPR),
4892 args,
4893 LOOKUP_NORMAL, complain);
4895 /* [expr.cond]
4897 If the overload resolution fails, the program is
4898 ill-formed. */
4899 candidates = splice_viable (candidates, false, &any_viable_p);
4900 if (!any_viable_p)
4902 if (complain & tf_error)
4903 error_at (loc, "operands to ?: have different types %qT and %qT",
4904 arg2_type, arg3_type);
4905 return error_mark_node;
4907 cand = tourney (candidates, complain);
4908 if (!cand)
4910 if (complain & tf_error)
4912 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
4913 print_z_candidates (loc, candidates);
4915 return error_mark_node;
4918 /* [expr.cond]
4920 Otherwise, the conversions thus determined are applied, and
4921 the converted operands are used in place of the original
4922 operands for the remainder of this section. */
4923 conv = cand->convs[0];
4924 arg1 = convert_like (conv, arg1, complain);
4925 conv = cand->convs[1];
4926 arg2 = convert_like (conv, arg2, complain);
4927 arg2_type = TREE_TYPE (arg2);
4928 conv = cand->convs[2];
4929 arg3 = convert_like (conv, arg3, complain);
4930 arg3_type = TREE_TYPE (arg3);
4933 /* [expr.cond]
4935 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
4936 and function-to-pointer (_conv.func_) standard conversions are
4937 performed on the second and third operands.
4939 We need to force the lvalue-to-rvalue conversion here for class types,
4940 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
4941 that isn't wrapped with a TARGET_EXPR plays havoc with exception
4942 regions. */
4944 arg2 = force_rvalue (arg2, complain);
4945 if (!CLASS_TYPE_P (arg2_type))
4946 arg2_type = TREE_TYPE (arg2);
4948 arg3 = force_rvalue (arg3, complain);
4949 if (!CLASS_TYPE_P (arg3_type))
4950 arg3_type = TREE_TYPE (arg3);
4952 if (arg2 == error_mark_node || arg3 == error_mark_node)
4953 return error_mark_node;
4955 /* [expr.cond]
4957 After those conversions, one of the following shall hold:
4959 --The second and third operands have the same type; the result is of
4960 that type. */
4961 if (same_type_p (arg2_type, arg3_type))
4962 result_type = arg2_type;
4963 /* [expr.cond]
4965 --The second and third operands have arithmetic or enumeration
4966 type; the usual arithmetic conversions are performed to bring
4967 them to a common type, and the result is of that type. */
4968 else if ((ARITHMETIC_TYPE_P (arg2_type)
4969 || UNSCOPED_ENUM_P (arg2_type))
4970 && (ARITHMETIC_TYPE_P (arg3_type)
4971 || UNSCOPED_ENUM_P (arg3_type)))
4973 /* In this case, there is always a common type. */
4974 result_type = type_after_usual_arithmetic_conversions (arg2_type,
4975 arg3_type);
4976 if (complain & tf_warning)
4977 do_warn_double_promotion (result_type, arg2_type, arg3_type,
4978 "implicit conversion from %qT to %qT to "
4979 "match other result of conditional",
4980 loc);
4982 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
4983 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
4985 if (TREE_CODE (orig_arg2) == CONST_DECL
4986 && TREE_CODE (orig_arg3) == CONST_DECL
4987 && DECL_CONTEXT (orig_arg2) == DECL_CONTEXT (orig_arg3))
4988 /* Two enumerators from the same enumeration can have different
4989 types when the enumeration is still being defined. */;
4990 else if (complain & tf_warning)
4991 warning_at (loc, OPT_Wenum_compare, "enumeral mismatch in "
4992 "conditional expression: %qT vs %qT",
4993 arg2_type, arg3_type);
4995 else if (extra_warnings
4996 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
4997 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
4998 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
4999 && !same_type_p (arg2_type,
5000 type_promotes_to (arg3_type)))))
5002 if (complain & tf_warning)
5003 warning_at (loc, 0, "enumeral and non-enumeral type in "
5004 "conditional expression");
5007 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5008 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5010 /* [expr.cond]
5012 --The second and third operands have pointer type, or one has
5013 pointer type and the other is a null pointer constant; pointer
5014 conversions (_conv.ptr_) and qualification conversions
5015 (_conv.qual_) are performed to bring them to their composite
5016 pointer type (_expr.rel_). The result is of the composite
5017 pointer type.
5019 --The second and third operands have pointer to member type, or
5020 one has pointer to member type and the other is a null pointer
5021 constant; pointer to member conversions (_conv.mem_) and
5022 qualification conversions (_conv.qual_) are performed to bring
5023 them to a common type, whose cv-qualification shall match the
5024 cv-qualification of either the second or the third operand.
5025 The result is of the common type. */
5026 else if ((null_ptr_cst_p (arg2)
5027 && TYPE_PTR_OR_PTRMEM_P (arg3_type))
5028 || (null_ptr_cst_p (arg3)
5029 && TYPE_PTR_OR_PTRMEM_P (arg2_type))
5030 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
5031 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
5032 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
5034 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
5035 arg3, CPO_CONDITIONAL_EXPR,
5036 complain);
5037 if (result_type == error_mark_node)
5038 return error_mark_node;
5039 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5040 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5043 if (!result_type)
5045 if (complain & tf_error)
5046 error_at (loc, "operands to ?: have different types %qT and %qT",
5047 arg2_type, arg3_type);
5048 return error_mark_node;
5051 if (arg2 == error_mark_node || arg3 == error_mark_node)
5052 return error_mark_node;
5054 valid_operands:
5055 result = build3 (COND_EXPR, result_type, arg1, arg2, arg3);
5056 if (!cp_unevaluated_operand)
5057 /* Avoid folding within decltype (c++/42013) and noexcept. */
5058 result = fold_if_not_in_template (result);
5060 /* We can't use result_type below, as fold might have returned a
5061 throw_expr. */
5063 if (!lvalue_p)
5065 /* Expand both sides into the same slot, hopefully the target of
5066 the ?: expression. We used to check for TARGET_EXPRs here,
5067 but now we sometimes wrap them in NOP_EXPRs so the test would
5068 fail. */
5069 if (CLASS_TYPE_P (TREE_TYPE (result)))
5070 result = get_target_expr_sfinae (result, complain);
5071 /* If this expression is an rvalue, but might be mistaken for an
5072 lvalue, we must add a NON_LVALUE_EXPR. */
5073 result = rvalue (result);
5075 else
5076 result = force_paren_expr (result);
5078 return result;
5081 /* Wrapper for above. */
5083 tree
5084 build_conditional_expr (location_t loc, tree arg1, tree arg2, tree arg3,
5085 tsubst_flags_t complain)
5087 tree ret;
5088 bool subtime = timevar_cond_start (TV_OVERLOAD);
5089 ret = build_conditional_expr_1 (loc, arg1, arg2, arg3, complain);
5090 timevar_cond_stop (TV_OVERLOAD, subtime);
5091 return ret;
5094 /* OPERAND is an operand to an expression. Perform necessary steps
5095 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
5096 returned. */
5098 static tree
5099 prep_operand (tree operand)
5101 if (operand)
5103 if (CLASS_TYPE_P (TREE_TYPE (operand))
5104 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
5105 /* Make sure the template type is instantiated now. */
5106 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
5109 return operand;
5112 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5113 OVERLOAD) to the CANDIDATES, returning an updated list of
5114 CANDIDATES. The ARGS are the arguments provided to the call;
5115 if FIRST_ARG is non-null it is the implicit object argument,
5116 otherwise the first element of ARGS is used if needed. The
5117 EXPLICIT_TARGS are explicit template arguments provided.
5118 TEMPLATE_ONLY is true if only template functions should be
5119 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5120 add_function_candidate. */
5122 static void
5123 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
5124 tree return_type,
5125 tree explicit_targs, bool template_only,
5126 tree conversion_path, tree access_path,
5127 int flags,
5128 struct z_candidate **candidates,
5129 tsubst_flags_t complain)
5131 tree ctype;
5132 const vec<tree, va_gc> *non_static_args;
5133 bool check_list_ctor;
5134 bool check_converting;
5135 unification_kind_t strict;
5136 tree fn;
5138 if (!fns)
5139 return;
5141 /* Precalculate special handling of constructors and conversion ops. */
5142 fn = OVL_CURRENT (fns);
5143 if (DECL_CONV_FN_P (fn))
5145 check_list_ctor = false;
5146 check_converting = !!(flags & LOOKUP_ONLYCONVERTING);
5147 if (flags & LOOKUP_NO_CONVERSION)
5148 /* We're doing return_type(x). */
5149 strict = DEDUCE_CONV;
5150 else
5151 /* We're doing x.operator return_type(). */
5152 strict = DEDUCE_EXACT;
5153 /* [over.match.funcs] For conversion functions, the function
5154 is considered to be a member of the class of the implicit
5155 object argument for the purpose of defining the type of
5156 the implicit object parameter. */
5157 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
5159 else
5161 if (DECL_CONSTRUCTOR_P (fn))
5163 check_list_ctor = !!(flags & LOOKUP_LIST_ONLY);
5164 /* For list-initialization we consider explicit constructors
5165 and complain if one is chosen. */
5166 check_converting
5167 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
5168 == LOOKUP_ONLYCONVERTING);
5170 else
5172 check_list_ctor = false;
5173 check_converting = false;
5175 strict = DEDUCE_CALL;
5176 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
5179 if (first_arg)
5180 non_static_args = args;
5181 else
5182 /* Delay creating the implicit this parameter until it is needed. */
5183 non_static_args = NULL;
5185 for (; fns; fns = OVL_NEXT (fns))
5187 tree fn_first_arg;
5188 const vec<tree, va_gc> *fn_args;
5190 fn = OVL_CURRENT (fns);
5192 if (check_converting && DECL_NONCONVERTING_P (fn))
5193 continue;
5194 if (check_list_ctor && !is_list_ctor (fn))
5195 continue;
5197 /* Figure out which set of arguments to use. */
5198 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
5200 /* If this function is a non-static member and we didn't get an
5201 implicit object argument, move it out of args. */
5202 if (first_arg == NULL_TREE)
5204 unsigned int ix;
5205 tree arg;
5206 vec<tree, va_gc> *tempvec;
5207 vec_alloc (tempvec, args->length () - 1);
5208 for (ix = 1; args->iterate (ix, &arg); ++ix)
5209 tempvec->quick_push (arg);
5210 non_static_args = tempvec;
5211 first_arg = (*args)[0];
5214 fn_first_arg = first_arg;
5215 fn_args = non_static_args;
5217 else
5219 /* Otherwise, just use the list of arguments provided. */
5220 fn_first_arg = NULL_TREE;
5221 fn_args = args;
5224 if (TREE_CODE (fn) == TEMPLATE_DECL)
5225 add_template_candidate (candidates,
5227 ctype,
5228 explicit_targs,
5229 fn_first_arg,
5230 fn_args,
5231 return_type,
5232 access_path,
5233 conversion_path,
5234 flags,
5235 strict,
5236 complain);
5237 else if (!template_only)
5238 add_function_candidate (candidates,
5240 ctype,
5241 fn_first_arg,
5242 fn_args,
5243 access_path,
5244 conversion_path,
5245 flags,
5246 complain);
5250 static tree
5251 build_new_op_1 (location_t loc, enum tree_code code, int flags, tree arg1,
5252 tree arg2, tree arg3, tree *overload, tsubst_flags_t complain)
5254 struct z_candidate *candidates = 0, *cand;
5255 vec<tree, va_gc> *arglist;
5256 tree fnname;
5257 tree args[3];
5258 tree result = NULL_TREE;
5259 bool result_valid_p = false;
5260 enum tree_code code2 = NOP_EXPR;
5261 enum tree_code code_orig_arg1 = ERROR_MARK;
5262 enum tree_code code_orig_arg2 = ERROR_MARK;
5263 conversion *conv;
5264 void *p;
5265 bool strict_p;
5266 bool any_viable_p;
5268 if (error_operand_p (arg1)
5269 || error_operand_p (arg2)
5270 || error_operand_p (arg3))
5271 return error_mark_node;
5273 if (code == MODIFY_EXPR)
5275 code2 = TREE_CODE (arg3);
5276 arg3 = NULL_TREE;
5277 fnname = ansi_assopname (code2);
5279 else
5280 fnname = ansi_opname (code);
5282 arg1 = prep_operand (arg1);
5284 switch (code)
5286 case NEW_EXPR:
5287 case VEC_NEW_EXPR:
5288 case VEC_DELETE_EXPR:
5289 case DELETE_EXPR:
5290 /* Use build_op_new_call and build_op_delete_call instead. */
5291 gcc_unreachable ();
5293 case CALL_EXPR:
5294 /* Use build_op_call instead. */
5295 gcc_unreachable ();
5297 case TRUTH_ORIF_EXPR:
5298 case TRUTH_ANDIF_EXPR:
5299 case TRUTH_AND_EXPR:
5300 case TRUTH_OR_EXPR:
5301 /* These are saved for the sake of warn_logical_operator. */
5302 code_orig_arg1 = TREE_CODE (arg1);
5303 code_orig_arg2 = TREE_CODE (arg2);
5305 default:
5306 break;
5309 arg2 = prep_operand (arg2);
5310 arg3 = prep_operand (arg3);
5312 if (code == COND_EXPR)
5313 /* Use build_conditional_expr instead. */
5314 gcc_unreachable ();
5315 else if (! OVERLOAD_TYPE_P (TREE_TYPE (arg1))
5316 && (! arg2 || ! OVERLOAD_TYPE_P (TREE_TYPE (arg2))))
5317 goto builtin;
5319 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5320 arg2 = integer_zero_node;
5322 vec_alloc (arglist, 3);
5323 arglist->quick_push (arg1);
5324 if (arg2 != NULL_TREE)
5325 arglist->quick_push (arg2);
5326 if (arg3 != NULL_TREE)
5327 arglist->quick_push (arg3);
5329 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5330 p = conversion_obstack_alloc (0);
5332 /* Add namespace-scope operators to the list of functions to
5333 consider. */
5334 add_candidates (lookup_function_nonclass (fnname, arglist, /*block_p=*/true),
5335 NULL_TREE, arglist, NULL_TREE,
5336 NULL_TREE, false, NULL_TREE, NULL_TREE,
5337 flags, &candidates, complain);
5339 args[0] = arg1;
5340 args[1] = arg2;
5341 args[2] = NULL_TREE;
5343 /* Add class-member operators to the candidate set. */
5344 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
5346 tree fns;
5348 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
5349 if (fns == error_mark_node)
5351 result = error_mark_node;
5352 goto user_defined_result_ready;
5354 if (fns)
5355 add_candidates (BASELINK_FUNCTIONS (fns),
5356 NULL_TREE, arglist, NULL_TREE,
5357 NULL_TREE, false,
5358 BASELINK_BINFO (fns),
5359 BASELINK_ACCESS_BINFO (fns),
5360 flags, &candidates, complain);
5362 /* Per 13.3.1.2/3, 2nd bullet, if no operand has a class type, then
5363 only non-member functions that have type T1 or reference to
5364 cv-qualified-opt T1 for the first argument, if the first argument
5365 has an enumeration type, or T2 or reference to cv-qualified-opt
5366 T2 for the second argument, if the the second argument has an
5367 enumeration type. Filter out those that don't match. */
5368 else if (! arg2 || ! CLASS_TYPE_P (TREE_TYPE (arg2)))
5370 struct z_candidate **candp, **next;
5372 for (candp = &candidates; *candp; candp = next)
5374 tree parmlist, parmtype;
5375 int i, nargs = (arg2 ? 2 : 1);
5377 cand = *candp;
5378 next = &cand->next;
5380 parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5382 for (i = 0; i < nargs; ++i)
5384 parmtype = TREE_VALUE (parmlist);
5386 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
5387 parmtype = TREE_TYPE (parmtype);
5388 if (TREE_CODE (TREE_TYPE (args[i])) == ENUMERAL_TYPE
5389 && (same_type_ignoring_top_level_qualifiers_p
5390 (TREE_TYPE (args[i]), parmtype)))
5391 break;
5393 parmlist = TREE_CHAIN (parmlist);
5396 /* No argument has an appropriate type, so remove this
5397 candidate function from the list. */
5398 if (i == nargs)
5400 *candp = cand->next;
5401 next = candp;
5406 add_builtin_candidates (&candidates, code, code2, fnname, args,
5407 flags, complain);
5409 switch (code)
5411 case COMPOUND_EXPR:
5412 case ADDR_EXPR:
5413 /* For these, the built-in candidates set is empty
5414 [over.match.oper]/3. We don't want non-strict matches
5415 because exact matches are always possible with built-in
5416 operators. The built-in candidate set for COMPONENT_REF
5417 would be empty too, but since there are no such built-in
5418 operators, we accept non-strict matches for them. */
5419 strict_p = true;
5420 break;
5422 default:
5423 strict_p = false;
5424 break;
5427 candidates = splice_viable (candidates, strict_p, &any_viable_p);
5428 if (!any_viable_p)
5430 switch (code)
5432 case POSTINCREMENT_EXPR:
5433 case POSTDECREMENT_EXPR:
5434 /* Don't try anything fancy if we're not allowed to produce
5435 errors. */
5436 if (!(complain & tf_error))
5437 return error_mark_node;
5439 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
5440 distinguish between prefix and postfix ++ and
5441 operator++() was used for both, so we allow this with
5442 -fpermissive. */
5443 else
5445 const char *msg = (flag_permissive)
5446 ? G_("no %<%D(int)%> declared for postfix %qs,"
5447 " trying prefix operator instead")
5448 : G_("no %<%D(int)%> declared for postfix %qs");
5449 permerror (loc, msg, fnname, operator_name_info[code].name);
5452 if (!flag_permissive)
5453 return error_mark_node;
5455 if (code == POSTINCREMENT_EXPR)
5456 code = PREINCREMENT_EXPR;
5457 else
5458 code = PREDECREMENT_EXPR;
5459 result = build_new_op_1 (loc, code, flags, arg1, NULL_TREE,
5460 NULL_TREE, overload, complain);
5461 break;
5463 /* The caller will deal with these. */
5464 case ADDR_EXPR:
5465 case COMPOUND_EXPR:
5466 case COMPONENT_REF:
5467 result = NULL_TREE;
5468 result_valid_p = true;
5469 break;
5471 default:
5472 if (complain & tf_error)
5474 /* If one of the arguments of the operator represents
5475 an invalid use of member function pointer, try to report
5476 a meaningful error ... */
5477 if (invalid_nonstatic_memfn_p (arg1, tf_error)
5478 || invalid_nonstatic_memfn_p (arg2, tf_error)
5479 || invalid_nonstatic_memfn_p (arg3, tf_error))
5480 /* We displayed the error message. */;
5481 else
5483 /* ... Otherwise, report the more generic
5484 "no matching operator found" error */
5485 op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
5486 print_z_candidates (loc, candidates);
5489 result = error_mark_node;
5490 break;
5493 else
5495 cand = tourney (candidates, complain);
5496 if (cand == 0)
5498 if (complain & tf_error)
5500 op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
5501 print_z_candidates (loc, candidates);
5503 result = error_mark_node;
5505 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5507 if (overload)
5508 *overload = cand->fn;
5510 if (resolve_args (arglist, complain) == NULL)
5511 result = error_mark_node;
5512 else
5513 result = build_over_call (cand, LOOKUP_NORMAL, complain);
5515 else
5517 /* Give any warnings we noticed during overload resolution. */
5518 if (cand->warnings && (complain & tf_warning))
5520 struct candidate_warning *w;
5521 for (w = cand->warnings; w; w = w->next)
5522 joust (cand, w->loser, 1, complain);
5525 /* Check for comparison of different enum types. */
5526 switch (code)
5528 case GT_EXPR:
5529 case LT_EXPR:
5530 case GE_EXPR:
5531 case LE_EXPR:
5532 case EQ_EXPR:
5533 case NE_EXPR:
5534 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
5535 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
5536 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5537 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
5538 && (complain & tf_warning))
5540 warning (OPT_Wenum_compare,
5541 "comparison between %q#T and %q#T",
5542 TREE_TYPE (arg1), TREE_TYPE (arg2));
5544 break;
5545 default:
5546 break;
5549 /* We need to strip any leading REF_BIND so that bitfields
5550 don't cause errors. This should not remove any important
5551 conversions, because builtins don't apply to class
5552 objects directly. */
5553 conv = cand->convs[0];
5554 if (conv->kind == ck_ref_bind)
5555 conv = next_conversion (conv);
5556 arg1 = convert_like (conv, arg1, complain);
5558 if (arg2)
5560 conv = cand->convs[1];
5561 if (conv->kind == ck_ref_bind)
5562 conv = next_conversion (conv);
5563 else
5564 arg2 = decay_conversion (arg2, complain);
5566 /* We need to call warn_logical_operator before
5567 converting arg2 to a boolean_type, but after
5568 decaying an enumerator to its value. */
5569 if (complain & tf_warning)
5570 warn_logical_operator (loc, code, boolean_type_node,
5571 code_orig_arg1, arg1,
5572 code_orig_arg2, arg2);
5574 arg2 = convert_like (conv, arg2, complain);
5576 if (arg3)
5578 conv = cand->convs[2];
5579 if (conv->kind == ck_ref_bind)
5580 conv = next_conversion (conv);
5581 arg3 = convert_like (conv, arg3, complain);
5587 user_defined_result_ready:
5589 /* Free all the conversions we allocated. */
5590 obstack_free (&conversion_obstack, p);
5592 if (result || result_valid_p)
5593 return result;
5595 builtin:
5596 switch (code)
5598 case MODIFY_EXPR:
5599 return cp_build_modify_expr (arg1, code2, arg2, complain);
5601 case INDIRECT_REF:
5602 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
5604 case TRUTH_ANDIF_EXPR:
5605 case TRUTH_ORIF_EXPR:
5606 case TRUTH_AND_EXPR:
5607 case TRUTH_OR_EXPR:
5608 warn_logical_operator (loc, code, boolean_type_node,
5609 code_orig_arg1, arg1, code_orig_arg2, arg2);
5610 /* Fall through. */
5611 case PLUS_EXPR:
5612 case MINUS_EXPR:
5613 case MULT_EXPR:
5614 case TRUNC_DIV_EXPR:
5615 case GT_EXPR:
5616 case LT_EXPR:
5617 case GE_EXPR:
5618 case LE_EXPR:
5619 case EQ_EXPR:
5620 case NE_EXPR:
5621 case MAX_EXPR:
5622 case MIN_EXPR:
5623 case LSHIFT_EXPR:
5624 case RSHIFT_EXPR:
5625 case TRUNC_MOD_EXPR:
5626 case BIT_AND_EXPR:
5627 case BIT_IOR_EXPR:
5628 case BIT_XOR_EXPR:
5629 return cp_build_binary_op (loc, code, arg1, arg2, complain);
5631 case UNARY_PLUS_EXPR:
5632 case NEGATE_EXPR:
5633 case BIT_NOT_EXPR:
5634 case TRUTH_NOT_EXPR:
5635 case PREINCREMENT_EXPR:
5636 case POSTINCREMENT_EXPR:
5637 case PREDECREMENT_EXPR:
5638 case POSTDECREMENT_EXPR:
5639 case REALPART_EXPR:
5640 case IMAGPART_EXPR:
5641 case ABS_EXPR:
5642 return cp_build_unary_op (code, arg1, candidates != 0, complain);
5644 case ARRAY_REF:
5645 return cp_build_array_ref (input_location, arg1, arg2, complain);
5647 case MEMBER_REF:
5648 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_ARROW_STAR,
5649 complain),
5650 arg2, complain);
5652 /* The caller will deal with these. */
5653 case ADDR_EXPR:
5654 case COMPONENT_REF:
5655 case COMPOUND_EXPR:
5656 return NULL_TREE;
5658 default:
5659 gcc_unreachable ();
5661 return NULL_TREE;
5664 /* Wrapper for above. */
5666 tree
5667 build_new_op (location_t loc, enum tree_code code, int flags,
5668 tree arg1, tree arg2, tree arg3,
5669 tree *overload, tsubst_flags_t complain)
5671 tree ret;
5672 bool subtime = timevar_cond_start (TV_OVERLOAD);
5673 ret = build_new_op_1 (loc, code, flags, arg1, arg2, arg3,
5674 overload, complain);
5675 timevar_cond_stop (TV_OVERLOAD, subtime);
5676 return ret;
5679 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
5680 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
5682 static bool
5683 non_placement_deallocation_fn_p (tree t)
5685 /* A template instance is never a usual deallocation function,
5686 regardless of its signature. */
5687 if (TREE_CODE (t) == TEMPLATE_DECL
5688 || primary_template_instantiation_p (t))
5689 return false;
5691 /* If a class T has a member deallocation function named operator delete
5692 with exactly one parameter, then that function is a usual
5693 (non-placement) deallocation function. If class T does not declare
5694 such an operator delete but does declare a member deallocation
5695 function named operator delete with exactly two parameters, the second
5696 of which has type std::size_t (18.2), then this function is a usual
5697 deallocation function. */
5698 t = FUNCTION_ARG_CHAIN (t);
5699 if (t == void_list_node
5700 || (t && same_type_p (TREE_VALUE (t), size_type_node)
5701 && TREE_CHAIN (t) == void_list_node))
5702 return true;
5703 return false;
5706 /* Build a call to operator delete. This has to be handled very specially,
5707 because the restrictions on what signatures match are different from all
5708 other call instances. For a normal delete, only a delete taking (void *)
5709 or (void *, size_t) is accepted. For a placement delete, only an exact
5710 match with the placement new is accepted.
5712 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
5713 ADDR is the pointer to be deleted.
5714 SIZE is the size of the memory block to be deleted.
5715 GLOBAL_P is true if the delete-expression should not consider
5716 class-specific delete operators.
5717 PLACEMENT is the corresponding placement new call, or NULL_TREE.
5719 If this call to "operator delete" is being generated as part to
5720 deallocate memory allocated via a new-expression (as per [expr.new]
5721 which requires that if the initialization throws an exception then
5722 we call a deallocation function), then ALLOC_FN is the allocation
5723 function. */
5725 tree
5726 build_op_delete_call (enum tree_code code, tree addr, tree size,
5727 bool global_p, tree placement,
5728 tree alloc_fn, tsubst_flags_t complain)
5730 tree fn = NULL_TREE;
5731 tree fns, fnname, type, t;
5733 if (addr == error_mark_node)
5734 return error_mark_node;
5736 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
5738 fnname = ansi_opname (code);
5740 if (CLASS_TYPE_P (type)
5741 && COMPLETE_TYPE_P (complete_type (type))
5742 && !global_p)
5743 /* In [class.free]
5745 If the result of the lookup is ambiguous or inaccessible, or if
5746 the lookup selects a placement deallocation function, the
5747 program is ill-formed.
5749 Therefore, we ask lookup_fnfields to complain about ambiguity. */
5751 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
5752 if (fns == error_mark_node)
5753 return error_mark_node;
5755 else
5756 fns = NULL_TREE;
5758 if (fns == NULL_TREE)
5759 fns = lookup_name_nonclass (fnname);
5761 /* Strip const and volatile from addr. */
5762 addr = cp_convert (ptr_type_node, addr, complain);
5764 if (placement)
5766 /* "A declaration of a placement deallocation function matches the
5767 declaration of a placement allocation function if it has the same
5768 number of parameters and, after parameter transformations (8.3.5),
5769 all parameter types except the first are identical."
5771 So we build up the function type we want and ask instantiate_type
5772 to get it for us. */
5773 t = FUNCTION_ARG_CHAIN (alloc_fn);
5774 t = tree_cons (NULL_TREE, ptr_type_node, t);
5775 t = build_function_type (void_type_node, t);
5777 fn = instantiate_type (t, fns, tf_none);
5778 if (fn == error_mark_node)
5779 return NULL_TREE;
5781 if (BASELINK_P (fn))
5782 fn = BASELINK_FUNCTIONS (fn);
5784 /* "If the lookup finds the two-parameter form of a usual deallocation
5785 function (3.7.4.2) and that function, considered as a placement
5786 deallocation function, would have been selected as a match for the
5787 allocation function, the program is ill-formed." */
5788 if (non_placement_deallocation_fn_p (fn))
5790 /* But if the class has an operator delete (void *), then that is
5791 the usual deallocation function, so we shouldn't complain
5792 about using the operator delete (void *, size_t). */
5793 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
5794 t; t = OVL_NEXT (t))
5796 tree elt = OVL_CURRENT (t);
5797 if (non_placement_deallocation_fn_p (elt)
5798 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
5799 goto ok;
5801 if (complain & tf_error)
5803 permerror (0, "non-placement deallocation function %q+D", fn);
5804 permerror (input_location, "selected for placement delete");
5806 else
5807 return error_mark_node;
5808 ok:;
5811 else
5812 /* "Any non-placement deallocation function matches a non-placement
5813 allocation function. If the lookup finds a single matching
5814 deallocation function, that function will be called; otherwise, no
5815 deallocation function will be called." */
5816 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
5817 t; t = OVL_NEXT (t))
5819 tree elt = OVL_CURRENT (t);
5820 if (non_placement_deallocation_fn_p (elt))
5822 fn = elt;
5823 /* "If a class T has a member deallocation function named
5824 operator delete with exactly one parameter, then that
5825 function is a usual (non-placement) deallocation
5826 function. If class T does not declare such an operator
5827 delete but does declare a member deallocation function named
5828 operator delete with exactly two parameters, the second of
5829 which has type std::size_t (18.2), then this function is a
5830 usual deallocation function."
5832 So (void*) beats (void*, size_t). */
5833 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
5834 break;
5838 /* If we have a matching function, call it. */
5839 if (fn)
5841 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
5843 /* If the FN is a member function, make sure that it is
5844 accessible. */
5845 if (BASELINK_P (fns))
5846 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
5847 complain);
5849 /* Core issue 901: It's ok to new a type with deleted delete. */
5850 if (DECL_DELETED_FN (fn) && alloc_fn)
5851 return NULL_TREE;
5853 if (placement)
5855 /* The placement args might not be suitable for overload
5856 resolution at this point, so build the call directly. */
5857 int nargs = call_expr_nargs (placement);
5858 tree *argarray = XALLOCAVEC (tree, nargs);
5859 int i;
5860 argarray[0] = addr;
5861 for (i = 1; i < nargs; i++)
5862 argarray[i] = CALL_EXPR_ARG (placement, i);
5863 mark_used (fn);
5864 return build_cxx_call (fn, nargs, argarray, complain);
5866 else
5868 tree ret;
5869 vec<tree, va_gc> *args = make_tree_vector ();
5870 args->quick_push (addr);
5871 if (FUNCTION_ARG_CHAIN (fn) != void_list_node)
5872 args->quick_push (size);
5873 ret = cp_build_function_call_vec (fn, &args, complain);
5874 release_tree_vector (args);
5875 return ret;
5879 /* [expr.new]
5881 If no unambiguous matching deallocation function can be found,
5882 propagating the exception does not cause the object's memory to
5883 be freed. */
5884 if (alloc_fn)
5886 if ((complain & tf_warning)
5887 && !placement)
5888 warning (0, "no corresponding deallocation function for %qD",
5889 alloc_fn);
5890 return NULL_TREE;
5893 if (complain & tf_error)
5894 error ("no suitable %<operator %s%> for %qT",
5895 operator_name_info[(int)code].name, type);
5896 return error_mark_node;
5899 /* If the current scope isn't allowed to access DECL along
5900 BASETYPE_PATH, give an error. The most derived class in
5901 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
5902 the declaration to use in the error diagnostic. */
5904 bool
5905 enforce_access (tree basetype_path, tree decl, tree diag_decl,
5906 tsubst_flags_t complain)
5908 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
5910 if (!accessible_p (basetype_path, decl, true))
5912 if (complain & tf_error)
5914 if (TREE_PRIVATE (decl))
5915 error ("%q+#D is private", diag_decl);
5916 else if (TREE_PROTECTED (decl))
5917 error ("%q+#D is protected", diag_decl);
5918 else
5919 error ("%q+#D is inaccessible", diag_decl);
5920 error ("within this context");
5922 return false;
5925 return true;
5928 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
5929 bitwise or of LOOKUP_* values. If any errors are warnings are
5930 generated, set *DIAGNOSTIC_FN to "error" or "warning",
5931 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
5932 to NULL. */
5934 static tree
5935 build_temp (tree expr, tree type, int flags,
5936 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
5938 int savew, savee;
5939 vec<tree, va_gc> *args;
5941 savew = warningcount + werrorcount, savee = errorcount;
5942 args = make_tree_vector_single (expr);
5943 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
5944 &args, type, flags, complain);
5945 release_tree_vector (args);
5946 if (warningcount + werrorcount > savew)
5947 *diagnostic_kind = DK_WARNING;
5948 else if (errorcount > savee)
5949 *diagnostic_kind = DK_ERROR;
5950 else
5951 *diagnostic_kind = DK_UNSPECIFIED;
5952 return expr;
5955 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
5956 EXPR is implicitly converted to type TOTYPE.
5957 FN and ARGNUM are used for diagnostics. */
5959 static void
5960 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
5962 /* Issue warnings about peculiar, but valid, uses of NULL. */
5963 if (expr == null_node && TREE_CODE (totype) != BOOLEAN_TYPE
5964 && ARITHMETIC_TYPE_P (totype))
5966 source_location loc =
5967 expansion_point_location_if_in_system_header (input_location);
5969 if (fn)
5970 warning_at (loc, OPT_Wconversion_null,
5971 "passing NULL to non-pointer argument %P of %qD",
5972 argnum, fn);
5973 else
5974 warning_at (loc, OPT_Wconversion_null,
5975 "converting to non-pointer type %qT from NULL", totype);
5978 /* Issue warnings if "false" is converted to a NULL pointer */
5979 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
5980 && TYPE_PTR_P (totype))
5982 if (fn)
5983 warning_at (input_location, OPT_Wconversion_null,
5984 "converting %<false%> to pointer type for argument %P "
5985 "of %qD", argnum, fn);
5986 else
5987 warning_at (input_location, OPT_Wconversion_null,
5988 "converting %<false%> to pointer type %qT", totype);
5992 /* We gave a diagnostic during a conversion. If this was in the second
5993 standard conversion sequence of a user-defined conversion sequence, say
5994 which user-defined conversion. */
5996 static void
5997 maybe_print_user_conv_context (conversion *convs)
5999 if (convs->user_conv_p)
6000 for (conversion *t = convs; t; t = next_conversion (t))
6001 if (t->kind == ck_user)
6003 print_z_candidate (0, " after user-defined conversion:",
6004 t->cand);
6005 break;
6009 /* Perform the conversions in CONVS on the expression EXPR. FN and
6010 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
6011 indicates the `this' argument of a method. INNER is nonzero when
6012 being called to continue a conversion chain. It is negative when a
6013 reference binding will be applied, positive otherwise. If
6014 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
6015 conversions will be emitted if appropriate. If C_CAST_P is true,
6016 this conversion is coming from a C-style cast; in that case,
6017 conversions to inaccessible bases are permitted. */
6019 static tree
6020 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
6021 int inner, bool issue_conversion_warnings,
6022 bool c_cast_p, tsubst_flags_t complain)
6024 tree totype = convs->type;
6025 diagnostic_t diag_kind;
6026 int flags;
6027 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6029 if (convs->bad_p && !(complain & tf_error))
6030 return error_mark_node;
6032 if (convs->bad_p
6033 && convs->kind != ck_user
6034 && convs->kind != ck_list
6035 && convs->kind != ck_ambig
6036 && (convs->kind != ck_ref_bind
6037 || (convs->user_conv_p && next_conversion (convs)->bad_p))
6038 && (convs->kind != ck_rvalue
6039 || SCALAR_TYPE_P (totype))
6040 && convs->kind != ck_base)
6042 bool complained = false;
6043 conversion *t = convs;
6045 /* Give a helpful error if this is bad because of excess braces. */
6046 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6047 && SCALAR_TYPE_P (totype)
6048 && CONSTRUCTOR_NELTS (expr) > 0
6049 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
6051 complained = permerror (loc, "too many braces around initializer "
6052 "for %qT", totype);
6053 while (BRACE_ENCLOSED_INITIALIZER_P (expr)
6054 && CONSTRUCTOR_NELTS (expr) == 1)
6055 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6058 /* Give a helpful error if this is bad because a conversion to bool
6059 from std::nullptr_t requires direct-initialization. */
6060 if (NULLPTR_TYPE_P (TREE_TYPE (expr))
6061 && TREE_CODE (totype) == BOOLEAN_TYPE)
6062 complained = permerror (loc, "converting to %qT from %qT requires "
6063 "direct-initialization",
6064 totype, TREE_TYPE (expr));
6066 for (; t ; t = next_conversion (t))
6068 if (t->kind == ck_user && t->cand->reason)
6070 permerror (loc, "invalid user-defined conversion "
6071 "from %qT to %qT", TREE_TYPE (expr), totype);
6072 print_z_candidate (loc, "candidate is:", t->cand);
6073 expr = convert_like_real (t, expr, fn, argnum, 1,
6074 /*issue_conversion_warnings=*/false,
6075 /*c_cast_p=*/false,
6076 complain);
6077 if (convs->kind == ck_ref_bind)
6078 expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
6079 LOOKUP_NORMAL, NULL_TREE,
6080 complain);
6081 else
6082 expr = cp_convert (totype, expr, complain);
6083 if (fn)
6084 inform (DECL_SOURCE_LOCATION (fn),
6085 " initializing argument %P of %qD", argnum, fn);
6086 return expr;
6088 else if (t->kind == ck_user || !t->bad_p)
6090 expr = convert_like_real (t, expr, fn, argnum, 1,
6091 /*issue_conversion_warnings=*/false,
6092 /*c_cast_p=*/false,
6093 complain);
6094 break;
6096 else if (t->kind == ck_ambig)
6097 return convert_like_real (t, expr, fn, argnum, 1,
6098 /*issue_conversion_warnings=*/false,
6099 /*c_cast_p=*/false,
6100 complain);
6101 else if (t->kind == ck_identity)
6102 break;
6104 if (!complained)
6105 complained = permerror (loc, "invalid conversion from %qT to %qT",
6106 TREE_TYPE (expr), totype);
6107 if (complained && fn)
6108 inform (DECL_SOURCE_LOCATION (fn),
6109 " initializing argument %P of %qD", argnum, fn);
6111 return cp_convert (totype, expr, complain);
6114 if (issue_conversion_warnings && (complain & tf_warning))
6115 conversion_null_warnings (totype, expr, fn, argnum);
6117 switch (convs->kind)
6119 case ck_user:
6121 struct z_candidate *cand = convs->cand;
6122 tree convfn = cand->fn;
6123 unsigned i;
6125 /* When converting from an init list we consider explicit
6126 constructors, but actually trying to call one is an error. */
6127 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
6128 /* Unless this is for direct-list-initialization. */
6129 && !DIRECT_LIST_INIT_P (expr))
6131 if (!(complain & tf_error))
6132 return error_mark_node;
6133 error ("converting to %qT from initializer list would use "
6134 "explicit constructor %qD", totype, convfn);
6137 /* If we're initializing from {}, it's value-initialization. */
6138 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6139 && CONSTRUCTOR_NELTS (expr) == 0
6140 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
6142 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
6143 expr = build_value_init (totype, complain);
6144 expr = get_target_expr_sfinae (expr, complain);
6145 if (expr != error_mark_node)
6147 TARGET_EXPR_LIST_INIT_P (expr) = true;
6148 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
6150 return expr;
6153 expr = mark_rvalue_use (expr);
6155 /* Set user_conv_p on the argument conversions, so rvalue/base
6156 handling knows not to allow any more UDCs. */
6157 for (i = 0; i < cand->num_convs; ++i)
6158 cand->convs[i]->user_conv_p = true;
6160 expr = build_over_call (cand, LOOKUP_NORMAL, complain);
6162 /* If this is a constructor or a function returning an aggr type,
6163 we need to build up a TARGET_EXPR. */
6164 if (DECL_CONSTRUCTOR_P (convfn))
6166 expr = build_cplus_new (totype, expr, complain);
6168 /* Remember that this was list-initialization. */
6169 if (convs->check_narrowing && expr != error_mark_node)
6170 TARGET_EXPR_LIST_INIT_P (expr) = true;
6173 return expr;
6175 case ck_identity:
6176 expr = mark_rvalue_use (expr);
6177 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
6179 int nelts = CONSTRUCTOR_NELTS (expr);
6180 if (nelts == 0)
6181 expr = build_value_init (totype, complain);
6182 else if (nelts == 1)
6183 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6184 else
6185 gcc_unreachable ();
6188 if (type_unknown_p (expr))
6189 expr = instantiate_type (totype, expr, complain);
6190 /* Convert a constant to its underlying value, unless we are
6191 about to bind it to a reference, in which case we need to
6192 leave it as an lvalue. */
6193 if (inner >= 0)
6195 expr = decl_constant_value_safe (expr);
6196 if (expr == null_node && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
6197 /* If __null has been converted to an integer type, we do not
6198 want to warn about uses of EXPR as an integer, rather than
6199 as a pointer. */
6200 expr = build_int_cst (totype, 0);
6202 return expr;
6203 case ck_ambig:
6204 /* We leave bad_p off ck_ambig because overload resolution considers
6205 it valid, it just fails when we try to perform it. So we need to
6206 check complain here, too. */
6207 if (complain & tf_error)
6209 /* Call build_user_type_conversion again for the error. */
6210 build_user_type_conversion (totype, convs->u.expr, LOOKUP_NORMAL,
6211 complain);
6212 if (fn)
6213 inform (input_location, " initializing argument %P of %q+D",
6214 argnum, fn);
6216 return error_mark_node;
6218 case ck_list:
6220 /* Conversion to std::initializer_list<T>. */
6221 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
6222 tree new_ctor = build_constructor (init_list_type_node, NULL);
6223 unsigned len = CONSTRUCTOR_NELTS (expr);
6224 tree array, val, field;
6225 vec<constructor_elt, va_gc> *vec = NULL;
6226 unsigned ix;
6228 /* Convert all the elements. */
6229 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
6231 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
6232 1, false, false, complain);
6233 if (sub == error_mark_node)
6234 return sub;
6235 if (!BRACE_ENCLOSED_INITIALIZER_P (val))
6236 check_narrowing (TREE_TYPE (sub), val);
6237 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
6238 if (!TREE_CONSTANT (sub))
6239 TREE_CONSTANT (new_ctor) = false;
6241 /* Build up the array. */
6242 elttype = cp_build_qualified_type
6243 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
6244 array = build_array_of_n_type (elttype, len);
6245 array = finish_compound_literal (array, new_ctor, complain);
6246 /* Take the address explicitly rather than via decay_conversion
6247 to avoid the error about taking the address of a temporary. */
6248 array = cp_build_addr_expr (array, complain);
6249 array = cp_convert (build_pointer_type (elttype), array, complain);
6250 if (array == error_mark_node)
6251 return error_mark_node;
6253 /* Build up the initializer_list object. */
6254 totype = complete_type (totype);
6255 field = next_initializable_field (TYPE_FIELDS (totype));
6256 CONSTRUCTOR_APPEND_ELT (vec, field, array);
6257 field = next_initializable_field (DECL_CHAIN (field));
6258 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
6259 new_ctor = build_constructor (totype, vec);
6260 return get_target_expr_sfinae (new_ctor, complain);
6263 case ck_aggr:
6264 if (TREE_CODE (totype) == COMPLEX_TYPE)
6266 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
6267 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
6268 real = perform_implicit_conversion (TREE_TYPE (totype),
6269 real, complain);
6270 imag = perform_implicit_conversion (TREE_TYPE (totype),
6271 imag, complain);
6272 expr = build2 (COMPLEX_EXPR, totype, real, imag);
6273 return fold_if_not_in_template (expr);
6275 expr = reshape_init (totype, expr, complain);
6276 expr = get_target_expr_sfinae (digest_init (totype, expr, complain),
6277 complain);
6278 if (expr != error_mark_node)
6279 TARGET_EXPR_LIST_INIT_P (expr) = true;
6280 return expr;
6282 default:
6283 break;
6286 expr = convert_like_real (next_conversion (convs), expr, fn, argnum,
6287 convs->kind == ck_ref_bind ? -1 : 1,
6288 convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
6289 c_cast_p,
6290 complain);
6291 if (expr == error_mark_node)
6292 return error_mark_node;
6294 switch (convs->kind)
6296 case ck_rvalue:
6297 expr = decay_conversion (expr, complain);
6298 if (expr == error_mark_node)
6299 return error_mark_node;
6301 if (! MAYBE_CLASS_TYPE_P (totype))
6302 return expr;
6303 /* Else fall through. */
6304 case ck_base:
6305 if (convs->kind == ck_base && !convs->need_temporary_p)
6307 /* We are going to bind a reference directly to a base-class
6308 subobject of EXPR. */
6309 /* Build an expression for `*((base*) &expr)'. */
6310 expr = cp_build_addr_expr (expr, complain);
6311 expr = convert_to_base (expr, build_pointer_type (totype),
6312 !c_cast_p, /*nonnull=*/true, complain);
6313 expr = cp_build_indirect_ref (expr, RO_IMPLICIT_CONVERSION, complain);
6314 return expr;
6317 /* Copy-initialization where the cv-unqualified version of the source
6318 type is the same class as, or a derived class of, the class of the
6319 destination [is treated as direct-initialization]. [dcl.init] */
6320 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
6321 if (convs->user_conv_p)
6322 /* This conversion is being done in the context of a user-defined
6323 conversion (i.e. the second step of copy-initialization), so
6324 don't allow any more. */
6325 flags |= LOOKUP_NO_CONVERSION;
6326 if (convs->rvaluedness_matches_p)
6327 flags |= LOOKUP_PREFER_RVALUE;
6328 if (TREE_CODE (expr) == TARGET_EXPR
6329 && TARGET_EXPR_LIST_INIT_P (expr))
6330 /* Copy-list-initialization doesn't actually involve a copy. */
6331 return expr;
6332 expr = build_temp (expr, totype, flags, &diag_kind, complain);
6333 if (diag_kind && complain)
6335 maybe_print_user_conv_context (convs);
6336 if (fn)
6337 inform (DECL_SOURCE_LOCATION (fn),
6338 " initializing argument %P of %qD", argnum, fn);
6341 return build_cplus_new (totype, expr, complain);
6343 case ck_ref_bind:
6345 tree ref_type = totype;
6347 if (convs->bad_p && !next_conversion (convs)->bad_p)
6349 tree extype = TREE_TYPE (expr);
6350 if (TYPE_REF_IS_RVALUE (ref_type)
6351 && real_lvalue_p (expr))
6352 error_at (loc, "cannot bind %qT lvalue to %qT",
6353 extype, totype);
6354 else if (!TYPE_REF_IS_RVALUE (ref_type) && !real_lvalue_p (expr)
6355 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
6356 error_at (loc, "invalid initialization of non-const reference of "
6357 "type %qT from an rvalue of type %qT", totype, extype);
6358 else if (!reference_compatible_p (TREE_TYPE (totype), extype))
6359 error_at (loc, "binding %qT to reference of type %qT "
6360 "discards qualifiers", extype, totype);
6361 else
6362 gcc_unreachable ();
6363 maybe_print_user_conv_context (convs);
6364 if (fn)
6365 inform (input_location,
6366 " initializing argument %P of %q+D", argnum, fn);
6367 return error_mark_node;
6370 /* If necessary, create a temporary.
6372 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
6373 that need temporaries, even when their types are reference
6374 compatible with the type of reference being bound, so the
6375 upcoming call to cp_build_addr_expr doesn't fail. */
6376 if (convs->need_temporary_p
6377 || TREE_CODE (expr) == CONSTRUCTOR
6378 || TREE_CODE (expr) == VA_ARG_EXPR)
6380 /* Otherwise, a temporary of type "cv1 T1" is created and
6381 initialized from the initializer expression using the rules
6382 for a non-reference copy-initialization (8.5). */
6384 tree type = TREE_TYPE (ref_type);
6385 cp_lvalue_kind lvalue = real_lvalue_p (expr);
6387 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6388 (type, next_conversion (convs)->type));
6389 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
6390 && !TYPE_REF_IS_RVALUE (ref_type))
6392 /* If the reference is volatile or non-const, we
6393 cannot create a temporary. */
6394 if (lvalue & clk_bitfield)
6395 error_at (loc, "cannot bind bitfield %qE to %qT",
6396 expr, ref_type);
6397 else if (lvalue & clk_packed)
6398 error_at (loc, "cannot bind packed field %qE to %qT",
6399 expr, ref_type);
6400 else
6401 error_at (loc, "cannot bind rvalue %qE to %qT",
6402 expr, ref_type);
6403 return error_mark_node;
6405 /* If the source is a packed field, and we must use a copy
6406 constructor, then building the target expr will require
6407 binding the field to the reference parameter to the
6408 copy constructor, and we'll end up with an infinite
6409 loop. If we can use a bitwise copy, then we'll be
6410 OK. */
6411 if ((lvalue & clk_packed)
6412 && CLASS_TYPE_P (type)
6413 && type_has_nontrivial_copy_init (type))
6415 error_at (loc, "cannot bind packed field %qE to %qT",
6416 expr, ref_type);
6417 return error_mark_node;
6419 if (lvalue & clk_bitfield)
6421 expr = convert_bitfield_to_declared_type (expr);
6422 expr = fold_convert (type, expr);
6424 expr = build_target_expr_with_type (expr, type, complain);
6427 /* Take the address of the thing to which we will bind the
6428 reference. */
6429 expr = cp_build_addr_expr (expr, complain);
6430 if (expr == error_mark_node)
6431 return error_mark_node;
6433 /* Convert it to a pointer to the type referred to by the
6434 reference. This will adjust the pointer if a derived to
6435 base conversion is being performed. */
6436 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
6437 expr, complain);
6438 /* Convert the pointer to the desired reference type. */
6439 return build_nop (ref_type, expr);
6442 case ck_lvalue:
6443 return decay_conversion (expr, complain);
6445 case ck_qual:
6446 /* Warn about deprecated conversion if appropriate. */
6447 string_conv_p (totype, expr, 1);
6448 break;
6450 case ck_ptr:
6451 if (convs->base_p)
6452 expr = convert_to_base (expr, totype, !c_cast_p,
6453 /*nonnull=*/false, complain);
6454 return build_nop (totype, expr);
6456 case ck_pmem:
6457 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
6458 c_cast_p, complain);
6460 default:
6461 break;
6464 if (convs->check_narrowing)
6465 check_narrowing (totype, expr);
6467 if (issue_conversion_warnings)
6468 expr = cp_convert_and_check (totype, expr, complain);
6469 else
6470 expr = cp_convert (totype, expr, complain);
6472 return expr;
6475 /* ARG is being passed to a varargs function. Perform any conversions
6476 required. Return the converted value. */
6478 tree
6479 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
6481 tree arg_type;
6482 location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
6484 /* [expr.call]
6486 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6487 standard conversions are performed. */
6488 arg = decay_conversion (arg, complain);
6489 arg_type = TREE_TYPE (arg);
6490 /* [expr.call]
6492 If the argument has integral or enumeration type that is subject
6493 to the integral promotions (_conv.prom_), or a floating point
6494 type that is subject to the floating point promotion
6495 (_conv.fpprom_), the value of the argument is converted to the
6496 promoted type before the call. */
6497 if (TREE_CODE (arg_type) == REAL_TYPE
6498 && (TYPE_PRECISION (arg_type)
6499 < TYPE_PRECISION (double_type_node))
6500 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
6502 if ((complain & tf_warning)
6503 && warn_double_promotion && !c_inhibit_evaluation_warnings)
6504 warning_at (loc, OPT_Wdouble_promotion,
6505 "implicit conversion from %qT to %qT when passing "
6506 "argument to function",
6507 arg_type, double_type_node);
6508 arg = convert_to_real (double_type_node, arg);
6510 else if (NULLPTR_TYPE_P (arg_type))
6511 arg = null_pointer_node;
6512 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
6514 if (SCOPED_ENUM_P (arg_type))
6516 tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
6517 complain);
6518 prom = cp_perform_integral_promotions (prom, complain);
6519 if (abi_version_crosses (6)
6520 && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
6521 && (complain & tf_warning))
6522 warning_at (loc, OPT_Wabi, "scoped enum %qT passed through ... as "
6523 "%qT before -fabi-version=6, %qT after", arg_type,
6524 TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
6525 if (!abi_version_at_least (6))
6526 arg = prom;
6528 else
6529 arg = cp_perform_integral_promotions (arg, complain);
6532 arg = require_complete_type_sfinae (arg, complain);
6533 arg_type = TREE_TYPE (arg);
6535 if (arg != error_mark_node
6536 /* In a template (or ill-formed code), we can have an incomplete type
6537 even after require_complete_type_sfinae, in which case we don't know
6538 whether it has trivial copy or not. */
6539 && COMPLETE_TYPE_P (arg_type))
6541 /* Build up a real lvalue-to-rvalue conversion in case the
6542 copy constructor is trivial but not callable. */
6543 if (!cp_unevaluated_operand && CLASS_TYPE_P (arg_type))
6544 force_rvalue (arg, complain);
6546 /* [expr.call] 5.2.2/7:
6547 Passing a potentially-evaluated argument of class type (Clause 9)
6548 with a non-trivial copy constructor or a non-trivial destructor
6549 with no corresponding parameter is conditionally-supported, with
6550 implementation-defined semantics.
6552 We used to just warn here and do a bitwise copy, but now
6553 cp_expr_size will abort if we try to do that.
6555 If the call appears in the context of a sizeof expression,
6556 it is not potentially-evaluated. */
6557 if (cp_unevaluated_operand == 0
6558 && (type_has_nontrivial_copy_init (arg_type)
6559 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type)))
6561 if (complain & tf_error)
6562 error_at (loc, "cannot pass objects of non-trivially-copyable "
6563 "type %q#T through %<...%>", arg_type);
6564 return error_mark_node;
6568 return arg;
6571 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
6573 tree
6574 build_x_va_arg (source_location loc, tree expr, tree type)
6576 if (processing_template_decl)
6577 return build_min (VA_ARG_EXPR, type, expr);
6579 type = complete_type_or_else (type, NULL_TREE);
6581 if (expr == error_mark_node || !type)
6582 return error_mark_node;
6584 expr = mark_lvalue_use (expr);
6586 if (type_has_nontrivial_copy_init (type)
6587 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
6588 || TREE_CODE (type) == REFERENCE_TYPE)
6590 /* Remove reference types so we don't ICE later on. */
6591 tree type1 = non_reference (type);
6592 /* conditionally-supported behavior [expr.call] 5.2.2/7. */
6593 error ("cannot receive objects of non-trivially-copyable type %q#T "
6594 "through %<...%>; ", type);
6595 expr = convert (build_pointer_type (type1), null_node);
6596 expr = cp_build_indirect_ref (expr, RO_NULL, tf_warning_or_error);
6597 return expr;
6600 return build_va_arg (loc, expr, type);
6603 /* TYPE has been given to va_arg. Apply the default conversions which
6604 would have happened when passed via ellipsis. Return the promoted
6605 type, or the passed type if there is no change. */
6607 tree
6608 cxx_type_promotes_to (tree type)
6610 tree promote;
6612 /* Perform the array-to-pointer and function-to-pointer
6613 conversions. */
6614 type = type_decays_to (type);
6616 promote = type_promotes_to (type);
6617 if (same_type_p (type, promote))
6618 promote = type;
6620 return promote;
6623 /* ARG is a default argument expression being passed to a parameter of
6624 the indicated TYPE, which is a parameter to FN. PARMNUM is the
6625 zero-based argument number. Do any required conversions. Return
6626 the converted value. */
6628 static GTY(()) vec<tree, va_gc> *default_arg_context;
6629 void
6630 push_defarg_context (tree fn)
6631 { vec_safe_push (default_arg_context, fn); }
6633 void
6634 pop_defarg_context (void)
6635 { default_arg_context->pop (); }
6637 tree
6638 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
6639 tsubst_flags_t complain)
6641 int i;
6642 tree t;
6644 /* See through clones. */
6645 fn = DECL_ORIGIN (fn);
6647 /* Detect recursion. */
6648 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
6649 if (t == fn)
6651 if (complain & tf_error)
6652 error ("recursive evaluation of default argument for %q#D", fn);
6653 return error_mark_node;
6656 /* If the ARG is an unparsed default argument expression, the
6657 conversion cannot be performed. */
6658 if (TREE_CODE (arg) == DEFAULT_ARG)
6660 if (complain & tf_error)
6661 error ("call to %qD uses the default argument for parameter %P, which "
6662 "is not yet defined", fn, parmnum);
6663 return error_mark_node;
6666 push_defarg_context (fn);
6668 if (fn && DECL_TEMPLATE_INFO (fn))
6669 arg = tsubst_default_argument (fn, type, arg, complain);
6671 /* Due to:
6673 [dcl.fct.default]
6675 The names in the expression are bound, and the semantic
6676 constraints are checked, at the point where the default
6677 expressions appears.
6679 we must not perform access checks here. */
6680 push_deferring_access_checks (dk_no_check);
6681 /* We must make a copy of ARG, in case subsequent processing
6682 alters any part of it. */
6683 arg = break_out_target_exprs (arg);
6684 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
6685 ICR_DEFAULT_ARGUMENT, fn, parmnum,
6686 complain);
6687 arg = convert_for_arg_passing (type, arg, complain);
6688 pop_deferring_access_checks();
6690 pop_defarg_context ();
6692 return arg;
6695 /* Returns the type which will really be used for passing an argument of
6696 type TYPE. */
6698 tree
6699 type_passed_as (tree type)
6701 /* Pass classes with copy ctors by invisible reference. */
6702 if (TREE_ADDRESSABLE (type))
6704 type = build_reference_type (type);
6705 /* There are no other pointers to this temporary. */
6706 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
6708 else if (targetm.calls.promote_prototypes (type)
6709 && INTEGRAL_TYPE_P (type)
6710 && COMPLETE_TYPE_P (type)
6711 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
6712 type = integer_type_node;
6714 return type;
6717 /* Actually perform the appropriate conversion. */
6719 tree
6720 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
6722 tree bitfield_type;
6724 /* If VAL is a bitfield, then -- since it has already been converted
6725 to TYPE -- it cannot have a precision greater than TYPE.
6727 If it has a smaller precision, we must widen it here. For
6728 example, passing "int f:3;" to a function expecting an "int" will
6729 not result in any conversion before this point.
6731 If the precision is the same we must not risk widening. For
6732 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
6733 often have type "int", even though the C++ type for the field is
6734 "long long". If the value is being passed to a function
6735 expecting an "int", then no conversions will be required. But,
6736 if we call convert_bitfield_to_declared_type, the bitfield will
6737 be converted to "long long". */
6738 bitfield_type = is_bitfield_expr_with_lowered_type (val);
6739 if (bitfield_type
6740 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
6741 val = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type), val);
6743 if (val == error_mark_node)
6745 /* Pass classes with copy ctors by invisible reference. */
6746 else if (TREE_ADDRESSABLE (type))
6747 val = build1 (ADDR_EXPR, build_reference_type (type), val);
6748 else if (targetm.calls.promote_prototypes (type)
6749 && INTEGRAL_TYPE_P (type)
6750 && COMPLETE_TYPE_P (type)
6751 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
6752 val = cp_perform_integral_promotions (val, complain);
6753 if ((complain & tf_warning)
6754 && warn_suggest_attribute_format)
6756 tree rhstype = TREE_TYPE (val);
6757 const enum tree_code coder = TREE_CODE (rhstype);
6758 const enum tree_code codel = TREE_CODE (type);
6759 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6760 && coder == codel
6761 && check_missing_format_attribute (type, rhstype))
6762 warning (OPT_Wsuggest_attribute_format,
6763 "argument of function call might be a candidate for a format attribute");
6765 return val;
6768 /* Returns true iff FN is a function with magic varargs, i.e. ones for
6769 which no conversions at all should be done. This is true for some
6770 builtins which don't act like normal functions. */
6772 bool
6773 magic_varargs_p (tree fn)
6775 if (flag_cilkplus && is_cilkplus_reduce_builtin (fn) != BUILT_IN_NONE)
6776 return true;
6778 if (DECL_BUILT_IN (fn))
6779 switch (DECL_FUNCTION_CODE (fn))
6781 case BUILT_IN_CLASSIFY_TYPE:
6782 case BUILT_IN_CONSTANT_P:
6783 case BUILT_IN_NEXT_ARG:
6784 case BUILT_IN_VA_START:
6785 return true;
6787 default:;
6788 return lookup_attribute ("type generic",
6789 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
6792 return false;
6795 /* Returns the decl of the dispatcher function if FN is a function version. */
6797 tree
6798 get_function_version_dispatcher (tree fn)
6800 tree dispatcher_decl = NULL;
6802 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6803 && DECL_FUNCTION_VERSIONED (fn));
6805 gcc_assert (targetm.get_function_versions_dispatcher);
6806 dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
6808 if (dispatcher_decl == NULL)
6810 error_at (input_location, "use of multiversioned function "
6811 "without a default");
6812 return NULL;
6815 retrofit_lang_decl (dispatcher_decl);
6816 gcc_assert (dispatcher_decl != NULL);
6817 return dispatcher_decl;
6820 /* fn is a function version dispatcher that is marked used. Mark all the
6821 semantically identical function versions it will dispatch as used. */
6823 void
6824 mark_versions_used (tree fn)
6826 struct cgraph_node *node;
6827 struct cgraph_function_version_info *node_v;
6828 struct cgraph_function_version_info *it_v;
6830 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6832 node = cgraph_get_node (fn);
6833 if (node == NULL)
6834 return;
6836 gcc_assert (node->dispatcher_function);
6838 node_v = get_cgraph_node_version (node);
6839 if (node_v == NULL)
6840 return;
6842 /* All semantically identical versions are chained. Traverse and mark each
6843 one of them as used. */
6844 it_v = node_v->next;
6845 while (it_v != NULL)
6847 mark_used (it_v->this_node->decl);
6848 it_v = it_v->next;
6852 /* Subroutine of the various build_*_call functions. Overload resolution
6853 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
6854 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
6855 bitmask of various LOOKUP_* flags which apply to the call itself. */
6857 static tree
6858 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
6860 tree fn = cand->fn;
6861 const vec<tree, va_gc> *args = cand->args;
6862 tree first_arg = cand->first_arg;
6863 conversion **convs = cand->convs;
6864 conversion *conv;
6865 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
6866 int parmlen;
6867 tree val;
6868 int i = 0;
6869 int j = 0;
6870 unsigned int arg_index = 0;
6871 int is_method = 0;
6872 int nargs;
6873 tree *argarray;
6874 bool already_used = false;
6876 /* In a template, there is no need to perform all of the work that
6877 is normally done. We are only interested in the type of the call
6878 expression, i.e., the return type of the function. Any semantic
6879 errors will be deferred until the template is instantiated. */
6880 if (processing_template_decl)
6882 tree expr, addr;
6883 tree return_type;
6884 const tree *argarray;
6885 unsigned int nargs;
6887 return_type = TREE_TYPE (TREE_TYPE (fn));
6888 nargs = vec_safe_length (args);
6889 if (first_arg == NULL_TREE)
6890 argarray = args->address ();
6891 else
6893 tree *alcarray;
6894 unsigned int ix;
6895 tree arg;
6897 ++nargs;
6898 alcarray = XALLOCAVEC (tree, nargs);
6899 alcarray[0] = build_this (first_arg);
6900 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
6901 alcarray[ix + 1] = arg;
6902 argarray = alcarray;
6905 addr = build_addr_func (fn, complain);
6906 if (addr == error_mark_node)
6907 return error_mark_node;
6908 expr = build_call_array_loc (input_location, return_type,
6909 addr, nargs, argarray);
6910 if (TREE_THIS_VOLATILE (fn) && cfun)
6911 current_function_returns_abnormally = 1;
6912 return convert_from_reference (expr);
6915 /* Give any warnings we noticed during overload resolution. */
6916 if (cand->warnings && (complain & tf_warning))
6918 struct candidate_warning *w;
6919 for (w = cand->warnings; w; w = w->next)
6920 joust (cand, w->loser, 1, complain);
6923 /* Make =delete work with SFINAE. */
6924 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
6925 return error_mark_node;
6927 if (DECL_FUNCTION_MEMBER_P (fn))
6929 tree access_fn;
6930 /* If FN is a template function, two cases must be considered.
6931 For example:
6933 struct A {
6934 protected:
6935 template <class T> void f();
6937 template <class T> struct B {
6938 protected:
6939 void g();
6941 struct C : A, B<int> {
6942 using A::f; // #1
6943 using B<int>::g; // #2
6946 In case #1 where `A::f' is a member template, DECL_ACCESS is
6947 recorded in the primary template but not in its specialization.
6948 We check access of FN using its primary template.
6950 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
6951 because it is a member of class template B, DECL_ACCESS is
6952 recorded in the specialization `B<int>::g'. We cannot use its
6953 primary template because `B<T>::g' and `B<int>::g' may have
6954 different access. */
6955 if (DECL_TEMPLATE_INFO (fn)
6956 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
6957 access_fn = DECL_TI_TEMPLATE (fn);
6958 else
6959 access_fn = fn;
6960 if (!perform_or_defer_access_check (cand->access_path, access_fn,
6961 fn, complain))
6962 return error_mark_node;
6965 /* If we're checking for implicit delete, don't bother with argument
6966 conversions. */
6967 if (flags & LOOKUP_SPECULATIVE)
6969 if (DECL_DELETED_FN (fn))
6971 if (complain & tf_error)
6972 mark_used (fn);
6973 return error_mark_node;
6975 if (cand->viable == 1)
6976 return fn;
6977 else if (!(complain & tf_error))
6978 /* Reject bad conversions now. */
6979 return error_mark_node;
6980 /* else continue to get conversion error. */
6983 /* N3276 magic doesn't apply to nested calls. */
6984 int decltype_flag = (complain & tf_decltype);
6985 complain &= ~tf_decltype;
6987 /* Find maximum size of vector to hold converted arguments. */
6988 parmlen = list_length (parm);
6989 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
6990 if (parmlen > nargs)
6991 nargs = parmlen;
6992 argarray = XALLOCAVEC (tree, nargs);
6994 /* The implicit parameters to a constructor are not considered by overload
6995 resolution, and must be of the proper type. */
6996 if (DECL_CONSTRUCTOR_P (fn))
6998 tree object_arg;
6999 if (first_arg != NULL_TREE)
7001 object_arg = first_arg;
7002 first_arg = NULL_TREE;
7004 else
7006 object_arg = (*args)[arg_index];
7007 ++arg_index;
7009 argarray[j++] = build_this (object_arg);
7010 parm = TREE_CHAIN (parm);
7011 /* We should never try to call the abstract constructor. */
7012 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
7014 if (DECL_HAS_VTT_PARM_P (fn))
7016 argarray[j++] = (*args)[arg_index];
7017 ++arg_index;
7018 parm = TREE_CHAIN (parm);
7021 /* Bypass access control for 'this' parameter. */
7022 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7024 tree parmtype = TREE_VALUE (parm);
7025 tree arg = build_this (first_arg != NULL_TREE
7026 ? first_arg
7027 : (*args)[arg_index]);
7028 tree argtype = TREE_TYPE (arg);
7029 tree converted_arg;
7030 tree base_binfo;
7032 if (convs[i]->bad_p)
7034 if (complain & tf_error)
7036 if (permerror (input_location, "passing %qT as %<this%> "
7037 "argument discards qualifiers",
7038 TREE_TYPE (argtype)))
7039 inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
7041 else
7042 return error_mark_node;
7045 /* See if the function member or the whole class type is declared
7046 final and the call can be devirtualized. */
7047 if (DECL_FINAL_P (fn)
7048 || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
7049 flags |= LOOKUP_NONVIRTUAL;
7051 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
7052 X is called for an object that is not of type X, or of a type
7053 derived from X, the behavior is undefined.
7055 So we can assume that anything passed as 'this' is non-null, and
7056 optimize accordingly. */
7057 gcc_assert (TYPE_PTR_P (parmtype));
7058 /* Convert to the base in which the function was declared. */
7059 gcc_assert (cand->conversion_path != NULL_TREE);
7060 converted_arg = build_base_path (PLUS_EXPR,
7061 arg,
7062 cand->conversion_path,
7063 1, complain);
7064 /* Check that the base class is accessible. */
7065 if (!accessible_base_p (TREE_TYPE (argtype),
7066 BINFO_TYPE (cand->conversion_path), true))
7068 if (complain & tf_error)
7069 error ("%qT is not an accessible base of %qT",
7070 BINFO_TYPE (cand->conversion_path),
7071 TREE_TYPE (argtype));
7072 else
7073 return error_mark_node;
7075 /* If fn was found by a using declaration, the conversion path
7076 will be to the derived class, not the base declaring fn. We
7077 must convert from derived to base. */
7078 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
7079 TREE_TYPE (parmtype), ba_unique,
7080 NULL, complain);
7081 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
7082 base_binfo, 1, complain);
7084 argarray[j++] = converted_arg;
7085 parm = TREE_CHAIN (parm);
7086 if (first_arg != NULL_TREE)
7087 first_arg = NULL_TREE;
7088 else
7089 ++arg_index;
7090 ++i;
7091 is_method = 1;
7094 gcc_assert (first_arg == NULL_TREE);
7095 for (; arg_index < vec_safe_length (args) && parm;
7096 parm = TREE_CHAIN (parm), ++arg_index, ++i)
7098 tree type = TREE_VALUE (parm);
7099 tree arg = (*args)[arg_index];
7100 bool conversion_warning = true;
7102 conv = convs[i];
7104 /* If the argument is NULL and used to (implicitly) instantiate a
7105 template function (and bind one of the template arguments to
7106 the type of 'long int'), we don't want to warn about passing NULL
7107 to non-pointer argument.
7108 For example, if we have this template function:
7110 template<typename T> void func(T x) {}
7112 we want to warn (when -Wconversion is enabled) in this case:
7114 void foo() {
7115 func<int>(NULL);
7118 but not in this case:
7120 void foo() {
7121 func(NULL);
7124 if (arg == null_node
7125 && DECL_TEMPLATE_INFO (fn)
7126 && cand->template_decl
7127 && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
7128 conversion_warning = false;
7130 /* Warn about initializer_list deduction that isn't currently in the
7131 working draft. */
7132 if (cxx_dialect > cxx98
7133 && flag_deduce_init_list
7134 && cand->template_decl
7135 && is_std_init_list (non_reference (type))
7136 && BRACE_ENCLOSED_INITIALIZER_P (arg))
7138 tree tmpl = TI_TEMPLATE (cand->template_decl);
7139 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
7140 tree patparm = get_pattern_parm (realparm, tmpl);
7141 tree pattype = TREE_TYPE (patparm);
7142 if (PACK_EXPANSION_P (pattype))
7143 pattype = PACK_EXPANSION_PATTERN (pattype);
7144 pattype = non_reference (pattype);
7146 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
7147 && (cand->explicit_targs == NULL_TREE
7148 || (TREE_VEC_LENGTH (cand->explicit_targs)
7149 <= TEMPLATE_TYPE_IDX (pattype))))
7151 pedwarn (input_location, 0, "deducing %qT as %qT",
7152 non_reference (TREE_TYPE (patparm)),
7153 non_reference (type));
7154 pedwarn (input_location, 0, " in call to %q+D", cand->fn);
7155 pedwarn (input_location, 0,
7156 " (you can disable this with -fno-deduce-init-list)");
7159 val = convert_like_with_context (conv, arg, fn, i - is_method,
7160 conversion_warning
7161 ? complain
7162 : complain & (~tf_warning));
7164 val = convert_for_arg_passing (type, val, complain);
7166 if (val == error_mark_node)
7167 return error_mark_node;
7168 else
7169 argarray[j++] = val;
7172 /* Default arguments */
7173 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
7175 if (TREE_VALUE (parm) == error_mark_node)
7176 return error_mark_node;
7177 argarray[j++] = convert_default_arg (TREE_VALUE (parm),
7178 TREE_PURPOSE (parm),
7179 fn, i - is_method,
7180 complain);
7183 /* Ellipsis */
7184 for (; arg_index < vec_safe_length (args); ++arg_index)
7186 tree a = (*args)[arg_index];
7187 if (magic_varargs_p (fn))
7188 /* Do no conversions for magic varargs. */
7189 a = mark_type_use (a);
7190 else
7191 a = convert_arg_to_ellipsis (a, complain);
7192 argarray[j++] = a;
7195 gcc_assert (j <= nargs);
7196 nargs = j;
7198 check_function_arguments (TREE_TYPE (fn), nargs, argarray);
7200 /* Avoid actually calling copy constructors and copy assignment operators,
7201 if possible. */
7203 if (! flag_elide_constructors)
7204 /* Do things the hard way. */;
7205 else if (cand->num_convs == 1
7206 && (DECL_COPY_CONSTRUCTOR_P (fn)
7207 || DECL_MOVE_CONSTRUCTOR_P (fn)))
7209 tree targ;
7210 tree arg = argarray[num_artificial_parms_for (fn)];
7211 tree fa;
7212 bool trivial = trivial_fn_p (fn);
7214 /* Pull out the real argument, disregarding const-correctness. */
7215 targ = arg;
7216 while (CONVERT_EXPR_P (targ)
7217 || TREE_CODE (targ) == NON_LVALUE_EXPR)
7218 targ = TREE_OPERAND (targ, 0);
7219 if (TREE_CODE (targ) == ADDR_EXPR)
7221 targ = TREE_OPERAND (targ, 0);
7222 if (!same_type_ignoring_top_level_qualifiers_p
7223 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
7224 targ = NULL_TREE;
7226 else
7227 targ = NULL_TREE;
7229 if (targ)
7230 arg = targ;
7231 else
7232 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
7234 /* [class.copy]: the copy constructor is implicitly defined even if
7235 the implementation elided its use. */
7236 if (!trivial || DECL_DELETED_FN (fn))
7238 mark_used (fn);
7239 already_used = true;
7242 /* If we're creating a temp and we already have one, don't create a
7243 new one. If we're not creating a temp but we get one, use
7244 INIT_EXPR to collapse the temp into our target. Otherwise, if the
7245 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
7246 temp or an INIT_EXPR otherwise. */
7247 fa = argarray[0];
7248 if (is_dummy_object (fa))
7250 if (TREE_CODE (arg) == TARGET_EXPR)
7251 return arg;
7252 else if (trivial)
7253 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
7255 else if (TREE_CODE (arg) == TARGET_EXPR || trivial)
7257 tree to = stabilize_reference (cp_build_indirect_ref (fa, RO_NULL,
7258 complain));
7260 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
7261 return val;
7264 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
7265 && trivial_fn_p (fn)
7266 && !DECL_DELETED_FN (fn))
7268 tree to = stabilize_reference
7269 (cp_build_indirect_ref (argarray[0], RO_NULL, complain));
7270 tree type = TREE_TYPE (to);
7271 tree as_base = CLASSTYPE_AS_BASE (type);
7272 tree arg = argarray[1];
7274 if (is_really_empty_class (type))
7276 /* Avoid copying empty classes. */
7277 val = build2 (COMPOUND_EXPR, void_type_node, to, arg);
7278 TREE_NO_WARNING (val) = 1;
7279 val = build2 (COMPOUND_EXPR, type, val, to);
7280 TREE_NO_WARNING (val) = 1;
7282 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
7284 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
7285 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
7287 else
7289 /* We must only copy the non-tail padding parts. */
7290 tree arg0, arg2, t;
7291 tree array_type, alias_set;
7293 arg2 = TYPE_SIZE_UNIT (as_base);
7294 arg0 = cp_build_addr_expr (to, complain);
7296 array_type = build_array_type (char_type_node,
7297 build_index_type
7298 (size_binop (MINUS_EXPR,
7299 arg2, size_int (1))));
7300 alias_set = build_int_cst (build_pointer_type (type), 0);
7301 t = build2 (MODIFY_EXPR, void_type_node,
7302 build2 (MEM_REF, array_type, arg0, alias_set),
7303 build2 (MEM_REF, array_type, arg, alias_set));
7304 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
7305 TREE_NO_WARNING (val) = 1;
7308 return val;
7310 else if (DECL_DESTRUCTOR_P (fn)
7311 && trivial_fn_p (fn)
7312 && !DECL_DELETED_FN (fn))
7313 return fold_convert (void_type_node, argarray[0]);
7314 /* FIXME handle trivial default constructor, too. */
7316 /* For calls to a multi-versioned function, overload resolution
7317 returns the function with the highest target priority, that is,
7318 the version that will checked for dispatching first. If this
7319 version is inlinable, a direct call to this version can be made
7320 otherwise the call should go through the dispatcher. */
7322 if (DECL_FUNCTION_VERSIONED (fn)
7323 && (current_function_decl == NULL
7324 || !targetm.target_option.can_inline_p (current_function_decl, fn)))
7326 fn = get_function_version_dispatcher (fn);
7327 if (fn == NULL)
7328 return NULL;
7329 if (!already_used)
7330 mark_versions_used (fn);
7333 if (!already_used
7334 && !mark_used (fn))
7335 return error_mark_node;
7337 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
7338 /* Don't mess with virtual lookup in fold_non_dependent_expr; virtual
7339 functions can't be constexpr. */
7340 && !in_template_function ())
7342 tree t;
7343 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
7344 DECL_CONTEXT (fn),
7345 ba_any, NULL, complain);
7346 gcc_assert (binfo && binfo != error_mark_node);
7348 /* Warn about deprecated virtual functions now, since we're about
7349 to throw away the decl. */
7350 if (TREE_DEPRECATED (fn))
7351 warn_deprecated_use (fn, NULL_TREE);
7353 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
7354 complain);
7355 if (TREE_SIDE_EFFECTS (argarray[0]))
7356 argarray[0] = save_expr (argarray[0]);
7357 t = build_pointer_type (TREE_TYPE (fn));
7358 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
7359 fn = build_java_interface_fn_ref (fn, argarray[0]);
7360 else
7361 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
7362 TREE_TYPE (fn) = t;
7364 else
7366 fn = build_addr_func (fn, complain);
7367 if (fn == error_mark_node)
7368 return error_mark_node;
7371 tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
7372 if (TREE_CODE (call) == CALL_EXPR
7373 && (cand->flags & LOOKUP_LIST_INIT_CTOR))
7374 CALL_EXPR_LIST_INIT_P (call) = true;
7375 return call;
7378 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
7379 This function performs no overload resolution, conversion, or other
7380 high-level operations. */
7382 tree
7383 build_cxx_call (tree fn, int nargs, tree *argarray,
7384 tsubst_flags_t complain)
7386 tree fndecl;
7387 int optimize_sav;
7389 /* Remember roughly where this call is. */
7390 location_t loc = EXPR_LOC_OR_LOC (fn, input_location);
7391 fn = build_call_a (fn, nargs, argarray);
7392 SET_EXPR_LOCATION (fn, loc);
7394 fndecl = get_callee_fndecl (fn);
7396 /* Check that arguments to builtin functions match the expectations. */
7397 if (fndecl
7398 && DECL_BUILT_IN (fndecl)
7399 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7400 && !check_builtin_function_arguments (fndecl, nargs, argarray))
7401 return error_mark_node;
7403 /* If it is a built-in array notation function, then the return type of
7404 the function is the element type of the array passed in as array
7405 notation (i.e. the first parameter of the function). */
7406 if (flag_cilkplus && TREE_CODE (fn) == CALL_EXPR)
7408 enum built_in_function bif =
7409 is_cilkplus_reduce_builtin (CALL_EXPR_FN (fn));
7410 if (bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
7411 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
7412 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
7413 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
7414 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE
7415 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
7417 if (call_expr_nargs (fn) == 0)
7419 error_at (EXPR_LOCATION (fn), "Invalid builtin arguments");
7420 return error_mark_node;
7422 /* for bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO or
7423 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO or
7424 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO or
7425 BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO or
7426 BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND or
7427 BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
7428 The pre-defined return-type is the correct one. */
7429 tree array_ntn = CALL_EXPR_ARG (fn, 0);
7430 TREE_TYPE (fn) = TREE_TYPE (array_ntn);
7431 return fn;
7435 /* Some built-in function calls will be evaluated at compile-time in
7436 fold (). Set optimize to 1 when folding __builtin_constant_p inside
7437 a constexpr function so that fold_builtin_1 doesn't fold it to 0. */
7438 optimize_sav = optimize;
7439 if (!optimize && fndecl && DECL_IS_BUILTIN_CONSTANT_P (fndecl)
7440 && current_function_decl
7441 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
7442 optimize = 1;
7443 fn = fold_if_not_in_template (fn);
7444 optimize = optimize_sav;
7446 if (VOID_TYPE_P (TREE_TYPE (fn)))
7447 return fn;
7449 /* 5.2.2/11: If a function call is a prvalue of object type: if the
7450 function call is either the operand of a decltype-specifier or the
7451 right operand of a comma operator that is the operand of a
7452 decltype-specifier, a temporary object is not introduced for the
7453 prvalue. The type of the prvalue may be incomplete. */
7454 if (!(complain & tf_decltype))
7456 fn = require_complete_type_sfinae (fn, complain);
7457 if (fn == error_mark_node)
7458 return error_mark_node;
7460 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
7461 fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
7463 return convert_from_reference (fn);
7466 static GTY(()) tree java_iface_lookup_fn;
7468 /* Make an expression which yields the address of the Java interface
7469 method FN. This is achieved by generating a call to libjava's
7470 _Jv_LookupInterfaceMethodIdx(). */
7472 static tree
7473 build_java_interface_fn_ref (tree fn, tree instance)
7475 tree lookup_fn, method, idx;
7476 tree klass_ref, iface, iface_ref;
7477 int i;
7479 if (!java_iface_lookup_fn)
7481 tree ftype = build_function_type_list (ptr_type_node,
7482 ptr_type_node, ptr_type_node,
7483 java_int_type_node, NULL_TREE);
7484 java_iface_lookup_fn
7485 = add_builtin_function ("_Jv_LookupInterfaceMethodIdx", ftype,
7486 0, NOT_BUILT_IN, NULL, NULL_TREE);
7489 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
7490 This is the first entry in the vtable. */
7491 klass_ref = build_vtbl_ref (cp_build_indirect_ref (instance, RO_NULL,
7492 tf_warning_or_error),
7493 integer_zero_node);
7495 /* Get the java.lang.Class pointer for the interface being called. */
7496 iface = DECL_CONTEXT (fn);
7497 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
7498 if (!iface_ref || !VAR_P (iface_ref)
7499 || DECL_CONTEXT (iface_ref) != iface)
7501 error ("could not find class$ field in java interface type %qT",
7502 iface);
7503 return error_mark_node;
7505 iface_ref = build_address (iface_ref);
7506 iface_ref = convert (build_pointer_type (iface), iface_ref);
7508 /* Determine the itable index of FN. */
7509 i = 1;
7510 for (method = TYPE_METHODS (iface); method; method = DECL_CHAIN (method))
7512 if (!DECL_VIRTUAL_P (method))
7513 continue;
7514 if (fn == method)
7515 break;
7516 i++;
7518 idx = build_int_cst (NULL_TREE, i);
7520 lookup_fn = build1 (ADDR_EXPR,
7521 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
7522 java_iface_lookup_fn);
7523 return build_call_nary (ptr_type_node, lookup_fn,
7524 3, klass_ref, iface_ref, idx);
7527 /* Returns the value to use for the in-charge parameter when making a
7528 call to a function with the indicated NAME.
7530 FIXME:Can't we find a neater way to do this mapping? */
7532 tree
7533 in_charge_arg_for_name (tree name)
7535 if (name == base_ctor_identifier
7536 || name == base_dtor_identifier)
7537 return integer_zero_node;
7538 else if (name == complete_ctor_identifier)
7539 return integer_one_node;
7540 else if (name == complete_dtor_identifier)
7541 return integer_two_node;
7542 else if (name == deleting_dtor_identifier)
7543 return integer_three_node;
7545 /* This function should only be called with one of the names listed
7546 above. */
7547 gcc_unreachable ();
7548 return NULL_TREE;
7551 /* Build a call to a constructor, destructor, or an assignment
7552 operator for INSTANCE, an expression with class type. NAME
7553 indicates the special member function to call; *ARGS are the
7554 arguments. ARGS may be NULL. This may change ARGS. BINFO
7555 indicates the base of INSTANCE that is to be passed as the `this'
7556 parameter to the member function called.
7558 FLAGS are the LOOKUP_* flags to use when processing the call.
7560 If NAME indicates a complete object constructor, INSTANCE may be
7561 NULL_TREE. In this case, the caller will call build_cplus_new to
7562 store the newly constructed object into a VAR_DECL. */
7564 tree
7565 build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
7566 tree binfo, int flags, tsubst_flags_t complain)
7568 tree fns;
7569 /* The type of the subobject to be constructed or destroyed. */
7570 tree class_type;
7571 vec<tree, va_gc> *allocated = NULL;
7572 tree ret;
7574 gcc_assert (name == complete_ctor_identifier
7575 || name == base_ctor_identifier
7576 || name == complete_dtor_identifier
7577 || name == base_dtor_identifier
7578 || name == deleting_dtor_identifier
7579 || name == ansi_assopname (NOP_EXPR));
7580 if (TYPE_P (binfo))
7582 /* Resolve the name. */
7583 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
7584 return error_mark_node;
7586 binfo = TYPE_BINFO (binfo);
7589 gcc_assert (binfo != NULL_TREE);
7591 class_type = BINFO_TYPE (binfo);
7593 /* Handle the special case where INSTANCE is NULL_TREE. */
7594 if (name == complete_ctor_identifier && !instance)
7595 instance = build_dummy_object (class_type);
7596 else
7598 if (name == complete_dtor_identifier
7599 || name == base_dtor_identifier
7600 || name == deleting_dtor_identifier)
7601 gcc_assert (args == NULL || vec_safe_is_empty (*args));
7603 /* Convert to the base class, if necessary. */
7604 if (!same_type_ignoring_top_level_qualifiers_p
7605 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
7607 if (name != ansi_assopname (NOP_EXPR))
7608 /* For constructors and destructors, either the base is
7609 non-virtual, or it is virtual but we are doing the
7610 conversion from a constructor or destructor for the
7611 complete object. In either case, we can convert
7612 statically. */
7613 instance = convert_to_base_statically (instance, binfo);
7614 else
7615 /* However, for assignment operators, we must convert
7616 dynamically if the base is virtual. */
7617 instance = build_base_path (PLUS_EXPR, instance,
7618 binfo, /*nonnull=*/1, complain);
7622 gcc_assert (instance != NULL_TREE);
7624 fns = lookup_fnfields (binfo, name, 1);
7626 /* When making a call to a constructor or destructor for a subobject
7627 that uses virtual base classes, pass down a pointer to a VTT for
7628 the subobject. */
7629 if ((name == base_ctor_identifier
7630 || name == base_dtor_identifier)
7631 && CLASSTYPE_VBASECLASSES (class_type))
7633 tree vtt;
7634 tree sub_vtt;
7636 /* If the current function is a complete object constructor
7637 or destructor, then we fetch the VTT directly.
7638 Otherwise, we look it up using the VTT we were given. */
7639 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
7640 vtt = decay_conversion (vtt, complain);
7641 if (vtt == error_mark_node)
7642 return error_mark_node;
7643 vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
7644 build2 (EQ_EXPR, boolean_type_node,
7645 current_in_charge_parm, integer_zero_node),
7646 current_vtt_parm,
7647 vtt);
7648 if (BINFO_SUBVTT_INDEX (binfo))
7649 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
7650 else
7651 sub_vtt = vtt;
7653 if (args == NULL)
7655 allocated = make_tree_vector ();
7656 args = &allocated;
7659 vec_safe_insert (*args, 0, sub_vtt);
7662 ret = build_new_method_call (instance, fns, args,
7663 TYPE_BINFO (BINFO_TYPE (binfo)),
7664 flags, /*fn=*/NULL,
7665 complain);
7667 if (allocated != NULL)
7668 release_tree_vector (allocated);
7670 if ((complain & tf_error)
7671 && (flags & LOOKUP_DELEGATING_CONS)
7672 && name == complete_ctor_identifier
7673 && TREE_CODE (ret) == CALL_EXPR
7674 && (DECL_ABSTRACT_ORIGIN (TREE_OPERAND (CALL_EXPR_FN (ret), 0))
7675 == current_function_decl))
7676 error ("constructor delegates to itself");
7678 return ret;
7681 /* Return the NAME, as a C string. The NAME indicates a function that
7682 is a member of TYPE. *FREE_P is set to true if the caller must
7683 free the memory returned.
7685 Rather than go through all of this, we should simply set the names
7686 of constructors and destructors appropriately, and dispense with
7687 ctor_identifier, dtor_identifier, etc. */
7689 static char *
7690 name_as_c_string (tree name, tree type, bool *free_p)
7692 char *pretty_name;
7694 /* Assume that we will not allocate memory. */
7695 *free_p = false;
7696 /* Constructors and destructors are special. */
7697 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
7699 pretty_name
7700 = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type))));
7701 /* For a destructor, add the '~'. */
7702 if (name == complete_dtor_identifier
7703 || name == base_dtor_identifier
7704 || name == deleting_dtor_identifier)
7706 pretty_name = concat ("~", pretty_name, NULL);
7707 /* Remember that we need to free the memory allocated. */
7708 *free_p = true;
7711 else if (IDENTIFIER_TYPENAME_P (name))
7713 pretty_name = concat ("operator ",
7714 type_as_string_translate (TREE_TYPE (name),
7715 TFF_PLAIN_IDENTIFIER),
7716 NULL);
7717 /* Remember that we need to free the memory allocated. */
7718 *free_p = true;
7720 else
7721 pretty_name = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (name)));
7723 return pretty_name;
7726 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
7727 be set, upon return, to the function called. ARGS may be NULL.
7728 This may change ARGS. */
7730 static tree
7731 build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
7732 tree conversion_path, int flags,
7733 tree *fn_p, tsubst_flags_t complain)
7735 struct z_candidate *candidates = 0, *cand;
7736 tree explicit_targs = NULL_TREE;
7737 tree basetype = NULL_TREE;
7738 tree access_binfo, binfo;
7739 tree optype;
7740 tree first_mem_arg = NULL_TREE;
7741 tree name;
7742 bool skip_first_for_error;
7743 vec<tree, va_gc> *user_args;
7744 tree call;
7745 tree fn;
7746 int template_only = 0;
7747 bool any_viable_p;
7748 tree orig_instance;
7749 tree orig_fns;
7750 vec<tree, va_gc> *orig_args = NULL;
7751 void *p;
7753 gcc_assert (instance != NULL_TREE);
7755 /* We don't know what function we're going to call, yet. */
7756 if (fn_p)
7757 *fn_p = NULL_TREE;
7759 if (error_operand_p (instance)
7760 || !fns || error_operand_p (fns))
7761 return error_mark_node;
7763 if (!BASELINK_P (fns))
7765 if (complain & tf_error)
7766 error ("call to non-function %qD", fns);
7767 return error_mark_node;
7770 orig_instance = instance;
7771 orig_fns = fns;
7773 /* Dismantle the baselink to collect all the information we need. */
7774 if (!conversion_path)
7775 conversion_path = BASELINK_BINFO (fns);
7776 access_binfo = BASELINK_ACCESS_BINFO (fns);
7777 binfo = BASELINK_BINFO (fns);
7778 optype = BASELINK_OPTYPE (fns);
7779 fns = BASELINK_FUNCTIONS (fns);
7780 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7782 explicit_targs = TREE_OPERAND (fns, 1);
7783 fns = TREE_OPERAND (fns, 0);
7784 template_only = 1;
7786 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
7787 || TREE_CODE (fns) == TEMPLATE_DECL
7788 || TREE_CODE (fns) == OVERLOAD);
7789 fn = get_first_fn (fns);
7790 name = DECL_NAME (fn);
7792 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
7793 gcc_assert (CLASS_TYPE_P (basetype));
7795 if (processing_template_decl)
7797 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
7798 instance = build_non_dependent_expr (instance);
7799 if (args != NULL)
7800 make_args_non_dependent (*args);
7803 user_args = args == NULL ? NULL : *args;
7804 /* Under DR 147 A::A() is an invalid constructor call,
7805 not a functional cast. */
7806 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
7808 if (! (complain & tf_error))
7809 return error_mark_node;
7811 if (permerror (input_location,
7812 "cannot call constructor %<%T::%D%> directly",
7813 basetype, name))
7814 inform (input_location, "for a function-style cast, remove the "
7815 "redundant %<::%D%>", name);
7816 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
7817 complain);
7818 return call;
7821 /* Figure out whether to skip the first argument for the error
7822 message we will display to users if an error occurs. We don't
7823 want to display any compiler-generated arguments. The "this"
7824 pointer hasn't been added yet. However, we must remove the VTT
7825 pointer if this is a call to a base-class constructor or
7826 destructor. */
7827 skip_first_for_error = false;
7828 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
7830 /* Callers should explicitly indicate whether they want to construct
7831 the complete object or just the part without virtual bases. */
7832 gcc_assert (name != ctor_identifier);
7833 /* Similarly for destructors. */
7834 gcc_assert (name != dtor_identifier);
7835 /* Remove the VTT pointer, if present. */
7836 if ((name == base_ctor_identifier || name == base_dtor_identifier)
7837 && CLASSTYPE_VBASECLASSES (basetype))
7838 skip_first_for_error = true;
7841 /* Process the argument list. */
7842 if (args != NULL && *args != NULL)
7844 *args = resolve_args (*args, complain);
7845 if (*args == NULL)
7846 return error_mark_node;
7849 /* Consider the object argument to be used even if we end up selecting a
7850 static member function. */
7851 instance = mark_type_use (instance);
7853 /* It's OK to call destructors and constructors on cv-qualified objects.
7854 Therefore, convert the INSTANCE to the unqualified type, if
7855 necessary. */
7856 if (DECL_DESTRUCTOR_P (fn)
7857 || DECL_CONSTRUCTOR_P (fn))
7859 if (!same_type_p (basetype, TREE_TYPE (instance)))
7861 instance = build_this (instance);
7862 instance = build_nop (build_pointer_type (basetype), instance);
7863 instance = build_fold_indirect_ref (instance);
7866 if (DECL_DESTRUCTOR_P (fn))
7867 name = complete_dtor_identifier;
7869 /* For the overload resolution we need to find the actual `this`
7870 that would be captured if the call turns out to be to a
7871 non-static member function. Do not actually capture it at this
7872 point. */
7873 first_mem_arg = maybe_resolve_dummy (instance, false);
7875 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7876 p = conversion_obstack_alloc (0);
7878 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
7879 initializer, not T({ }). */
7880 if (DECL_CONSTRUCTOR_P (fn) && args != NULL && !vec_safe_is_empty (*args)
7881 && DIRECT_LIST_INIT_P ((**args)[0]))
7883 tree init_list = (**args)[0];
7884 tree init = NULL_TREE;
7886 gcc_assert ((*args)->length () == 1
7887 && !(flags & LOOKUP_ONLYCONVERTING));
7889 /* If the initializer list has no elements and T is a class type with
7890 a default constructor, the object is value-initialized. Handle
7891 this here so we don't need to handle it wherever we use
7892 build_special_member_call. */
7893 if (CONSTRUCTOR_NELTS (init_list) == 0
7894 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
7895 /* For a user-provided default constructor, use the normal
7896 mechanisms so that protected access works. */
7897 && !type_has_user_provided_default_constructor (basetype)
7898 && !processing_template_decl)
7899 init = build_value_init (basetype, complain);
7901 /* If BASETYPE is an aggregate, we need to do aggregate
7902 initialization. */
7903 else if (CP_AGGREGATE_TYPE_P (basetype))
7904 init = digest_init (basetype, init_list, complain);
7906 if (init)
7908 if (is_dummy_object (instance))
7909 return get_target_expr_sfinae (init, complain);
7910 init = build2 (INIT_EXPR, TREE_TYPE (instance), instance, init);
7911 TREE_SIDE_EFFECTS (init) = true;
7912 return init;
7915 /* Otherwise go ahead with overload resolution. */
7916 add_list_candidates (fns, first_mem_arg, init_list,
7917 basetype, explicit_targs, template_only,
7918 conversion_path, access_binfo, flags,
7919 &candidates, complain);
7921 else
7923 add_candidates (fns, first_mem_arg, user_args, optype,
7924 explicit_targs, template_only, conversion_path,
7925 access_binfo, flags, &candidates, complain);
7927 any_viable_p = false;
7928 candidates = splice_viable (candidates, false, &any_viable_p);
7930 if (!any_viable_p)
7932 if (complain & tf_error)
7934 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
7935 cxx_incomplete_type_error (instance, basetype);
7936 else if (optype)
7937 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
7938 basetype, optype, build_tree_list_vec (user_args),
7939 TREE_TYPE (instance));
7940 else
7942 char *pretty_name;
7943 bool free_p;
7944 tree arglist;
7946 pretty_name = name_as_c_string (name, basetype, &free_p);
7947 arglist = build_tree_list_vec (user_args);
7948 if (skip_first_for_error)
7949 arglist = TREE_CHAIN (arglist);
7950 error ("no matching function for call to %<%T::%s(%A)%#V%>",
7951 basetype, pretty_name, arglist,
7952 TREE_TYPE (instance));
7953 if (free_p)
7954 free (pretty_name);
7956 print_z_candidates (location_of (name), candidates);
7958 call = error_mark_node;
7960 else
7962 cand = tourney (candidates, complain);
7963 if (cand == 0)
7965 char *pretty_name;
7966 bool free_p;
7967 tree arglist;
7969 if (complain & tf_error)
7971 pretty_name = name_as_c_string (name, basetype, &free_p);
7972 arglist = build_tree_list_vec (user_args);
7973 if (skip_first_for_error)
7974 arglist = TREE_CHAIN (arglist);
7975 if (!any_strictly_viable (candidates))
7976 error ("no matching function for call to %<%s(%A)%>",
7977 pretty_name, arglist);
7978 else
7979 error ("call of overloaded %<%s(%A)%> is ambiguous",
7980 pretty_name, arglist);
7981 print_z_candidates (location_of (name), candidates);
7982 if (free_p)
7983 free (pretty_name);
7985 call = error_mark_node;
7987 else
7989 fn = cand->fn;
7990 call = NULL_TREE;
7992 if (!(flags & LOOKUP_NONVIRTUAL)
7993 && DECL_PURE_VIRTUAL_P (fn)
7994 && instance == current_class_ref
7995 && (complain & tf_warning))
7997 /* This is not an error, it is runtime undefined
7998 behavior. */
7999 if (!current_function_decl)
8000 warning (0, "pure virtual %q#D called from "
8001 "non-static data member initializer", fn);
8002 else if (DECL_CONSTRUCTOR_P (current_function_decl)
8003 || DECL_DESTRUCTOR_P (current_function_decl))
8004 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
8005 ? "pure virtual %q#D called from constructor"
8006 : "pure virtual %q#D called from destructor"),
8007 fn);
8010 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
8011 && !DECL_CONSTRUCTOR_P (fn)
8012 && is_dummy_object (instance))
8014 instance = maybe_resolve_dummy (instance, true);
8015 if (instance == error_mark_node)
8016 call = error_mark_node;
8017 else if (!is_dummy_object (instance))
8019 /* We captured 'this' in the current lambda now that
8020 we know we really need it. */
8021 cand->first_arg = instance;
8023 else
8025 if (complain & tf_error)
8026 error ("cannot call member function %qD without object",
8027 fn);
8028 call = error_mark_node;
8032 if (call != error_mark_node)
8034 /* Optimize away vtable lookup if we know that this
8035 function can't be overridden. We need to check if
8036 the context and the type where we found fn are the same,
8037 actually FN might be defined in a different class
8038 type because of a using-declaration. In this case, we
8039 do not want to perform a non-virtual call. */
8040 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
8041 && same_type_ignoring_top_level_qualifiers_p
8042 (DECL_CONTEXT (fn), BINFO_TYPE (binfo))
8043 && resolves_to_fixed_type_p (instance, 0))
8044 flags |= LOOKUP_NONVIRTUAL;
8045 if (explicit_targs)
8046 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
8047 /* Now we know what function is being called. */
8048 if (fn_p)
8049 *fn_p = fn;
8050 /* Build the actual CALL_EXPR. */
8051 call = build_over_call (cand, flags, complain);
8052 /* In an expression of the form `a->f()' where `f' turns
8053 out to be a static member function, `a' is
8054 none-the-less evaluated. */
8055 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
8056 && !is_dummy_object (instance)
8057 && TREE_SIDE_EFFECTS (instance))
8058 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
8059 instance, call);
8060 else if (call != error_mark_node
8061 && DECL_DESTRUCTOR_P (cand->fn)
8062 && !VOID_TYPE_P (TREE_TYPE (call)))
8063 /* An explicit call of the form "x->~X()" has type
8064 "void". However, on platforms where destructors
8065 return "this" (i.e., those where
8066 targetm.cxx.cdtor_returns_this is true), such calls
8067 will appear to have a return value of pointer type
8068 to the low-level call machinery. We do not want to
8069 change the low-level machinery, since we want to be
8070 able to optimize "delete f()" on such platforms as
8071 "operator delete(~X(f()))" (rather than generating
8072 "t = f(), ~X(t), operator delete (t)"). */
8073 call = build_nop (void_type_node, call);
8078 if (processing_template_decl && call != error_mark_node)
8080 bool cast_to_void = false;
8082 if (TREE_CODE (call) == COMPOUND_EXPR)
8083 call = TREE_OPERAND (call, 1);
8084 else if (TREE_CODE (call) == NOP_EXPR)
8086 cast_to_void = true;
8087 call = TREE_OPERAND (call, 0);
8089 if (INDIRECT_REF_P (call))
8090 call = TREE_OPERAND (call, 0);
8091 call = (build_min_non_dep_call_vec
8092 (call,
8093 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
8094 orig_instance, orig_fns, NULL_TREE),
8095 orig_args));
8096 SET_EXPR_LOCATION (call, input_location);
8097 call = convert_from_reference (call);
8098 if (cast_to_void)
8099 call = build_nop (void_type_node, call);
8102 /* Free all the conversions we allocated. */
8103 obstack_free (&conversion_obstack, p);
8105 if (orig_args != NULL)
8106 release_tree_vector (orig_args);
8108 return call;
8111 /* Wrapper for above. */
8113 tree
8114 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
8115 tree conversion_path, int flags,
8116 tree *fn_p, tsubst_flags_t complain)
8118 tree ret;
8119 bool subtime = timevar_cond_start (TV_OVERLOAD);
8120 ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
8121 fn_p, complain);
8122 timevar_cond_stop (TV_OVERLOAD, subtime);
8123 return ret;
8126 /* Returns true iff standard conversion sequence ICS1 is a proper
8127 subsequence of ICS2. */
8129 static bool
8130 is_subseq (conversion *ics1, conversion *ics2)
8132 /* We can assume that a conversion of the same code
8133 between the same types indicates a subsequence since we only get
8134 here if the types we are converting from are the same. */
8136 while (ics1->kind == ck_rvalue
8137 || ics1->kind == ck_lvalue)
8138 ics1 = next_conversion (ics1);
8140 while (1)
8142 while (ics2->kind == ck_rvalue
8143 || ics2->kind == ck_lvalue)
8144 ics2 = next_conversion (ics2);
8146 if (ics2->kind == ck_user
8147 || ics2->kind == ck_ambig
8148 || ics2->kind == ck_aggr
8149 || ics2->kind == ck_list
8150 || ics2->kind == ck_identity)
8151 /* At this point, ICS1 cannot be a proper subsequence of
8152 ICS2. We can get a USER_CONV when we are comparing the
8153 second standard conversion sequence of two user conversion
8154 sequences. */
8155 return false;
8157 ics2 = next_conversion (ics2);
8159 if (ics2->kind == ics1->kind
8160 && same_type_p (ics2->type, ics1->type)
8161 && same_type_p (next_conversion (ics2)->type,
8162 next_conversion (ics1)->type))
8163 return true;
8167 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
8168 be any _TYPE nodes. */
8170 bool
8171 is_properly_derived_from (tree derived, tree base)
8173 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
8174 return false;
8176 /* We only allow proper derivation here. The DERIVED_FROM_P macro
8177 considers every class derived from itself. */
8178 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
8179 && DERIVED_FROM_P (base, derived));
8182 /* We build the ICS for an implicit object parameter as a pointer
8183 conversion sequence. However, such a sequence should be compared
8184 as if it were a reference conversion sequence. If ICS is the
8185 implicit conversion sequence for an implicit object parameter,
8186 modify it accordingly. */
8188 static void
8189 maybe_handle_implicit_object (conversion **ics)
8191 if ((*ics)->this_p)
8193 /* [over.match.funcs]
8195 For non-static member functions, the type of the
8196 implicit object parameter is "reference to cv X"
8197 where X is the class of which the function is a
8198 member and cv is the cv-qualification on the member
8199 function declaration. */
8200 conversion *t = *ics;
8201 tree reference_type;
8203 /* The `this' parameter is a pointer to a class type. Make the
8204 implicit conversion talk about a reference to that same class
8205 type. */
8206 reference_type = TREE_TYPE (t->type);
8207 reference_type = build_reference_type (reference_type);
8209 if (t->kind == ck_qual)
8210 t = next_conversion (t);
8211 if (t->kind == ck_ptr)
8212 t = next_conversion (t);
8213 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
8214 t = direct_reference_binding (reference_type, t);
8215 t->this_p = 1;
8216 t->rvaluedness_matches_p = 0;
8217 *ics = t;
8221 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
8222 and return the initial reference binding conversion. Otherwise,
8223 leave *ICS unchanged and return NULL. */
8225 static conversion *
8226 maybe_handle_ref_bind (conversion **ics)
8228 if ((*ics)->kind == ck_ref_bind)
8230 conversion *old_ics = *ics;
8231 *ics = next_conversion (old_ics);
8232 (*ics)->user_conv_p = old_ics->user_conv_p;
8233 return old_ics;
8236 return NULL;
8239 /* Compare two implicit conversion sequences according to the rules set out in
8240 [over.ics.rank]. Return values:
8242 1: ics1 is better than ics2
8243 -1: ics2 is better than ics1
8244 0: ics1 and ics2 are indistinguishable */
8246 static int
8247 compare_ics (conversion *ics1, conversion *ics2)
8249 tree from_type1;
8250 tree from_type2;
8251 tree to_type1;
8252 tree to_type2;
8253 tree deref_from_type1 = NULL_TREE;
8254 tree deref_from_type2 = NULL_TREE;
8255 tree deref_to_type1 = NULL_TREE;
8256 tree deref_to_type2 = NULL_TREE;
8257 conversion_rank rank1, rank2;
8259 /* REF_BINDING is nonzero if the result of the conversion sequence
8260 is a reference type. In that case REF_CONV is the reference
8261 binding conversion. */
8262 conversion *ref_conv1;
8263 conversion *ref_conv2;
8265 /* Compare badness before stripping the reference conversion. */
8266 if (ics1->bad_p > ics2->bad_p)
8267 return -1;
8268 else if (ics1->bad_p < ics2->bad_p)
8269 return 1;
8271 /* Handle implicit object parameters. */
8272 maybe_handle_implicit_object (&ics1);
8273 maybe_handle_implicit_object (&ics2);
8275 /* Handle reference parameters. */
8276 ref_conv1 = maybe_handle_ref_bind (&ics1);
8277 ref_conv2 = maybe_handle_ref_bind (&ics2);
8279 /* List-initialization sequence L1 is a better conversion sequence than
8280 list-initialization sequence L2 if L1 converts to
8281 std::initializer_list<X> for some X and L2 does not. */
8282 if (ics1->kind == ck_list && ics2->kind != ck_list)
8283 return 1;
8284 if (ics2->kind == ck_list && ics1->kind != ck_list)
8285 return -1;
8287 /* [over.ics.rank]
8289 When comparing the basic forms of implicit conversion sequences (as
8290 defined in _over.best.ics_)
8292 --a standard conversion sequence (_over.ics.scs_) is a better
8293 conversion sequence than a user-defined conversion sequence
8294 or an ellipsis conversion sequence, and
8296 --a user-defined conversion sequence (_over.ics.user_) is a
8297 better conversion sequence than an ellipsis conversion sequence
8298 (_over.ics.ellipsis_). */
8299 /* Use BAD_CONVERSION_RANK because we already checked for a badness
8300 mismatch. If both ICS are bad, we try to make a decision based on
8301 what would have happened if they'd been good. This is not an
8302 extension, we'll still give an error when we build up the call; this
8303 just helps us give a more helpful error message. */
8304 rank1 = BAD_CONVERSION_RANK (ics1);
8305 rank2 = BAD_CONVERSION_RANK (ics2);
8307 if (rank1 > rank2)
8308 return -1;
8309 else if (rank1 < rank2)
8310 return 1;
8312 if (ics1->ellipsis_p)
8313 /* Both conversions are ellipsis conversions. */
8314 return 0;
8316 /* User-defined conversion sequence U1 is a better conversion sequence
8317 than another user-defined conversion sequence U2 if they contain the
8318 same user-defined conversion operator or constructor and if the sec-
8319 ond standard conversion sequence of U1 is better than the second
8320 standard conversion sequence of U2. */
8322 /* Handle list-conversion with the same code even though it isn't always
8323 ranked as a user-defined conversion and it doesn't have a second
8324 standard conversion sequence; it will still have the desired effect.
8325 Specifically, we need to do the reference binding comparison at the
8326 end of this function. */
8328 if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
8330 conversion *t1;
8331 conversion *t2;
8333 for (t1 = ics1; t1->kind != ck_user; t1 = next_conversion (t1))
8334 if (t1->kind == ck_ambig || t1->kind == ck_aggr
8335 || t1->kind == ck_list)
8336 break;
8337 for (t2 = ics2; t2->kind != ck_user; t2 = next_conversion (t2))
8338 if (t2->kind == ck_ambig || t2->kind == ck_aggr
8339 || t2->kind == ck_list)
8340 break;
8342 if (t1->kind != t2->kind)
8343 return 0;
8344 else if (t1->kind == ck_user)
8346 if (t1->cand->fn != t2->cand->fn)
8347 return 0;
8349 else
8351 /* For ambiguous or aggregate conversions, use the target type as
8352 a proxy for the conversion function. */
8353 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
8354 return 0;
8357 /* We can just fall through here, after setting up
8358 FROM_TYPE1 and FROM_TYPE2. */
8359 from_type1 = t1->type;
8360 from_type2 = t2->type;
8362 else
8364 conversion *t1;
8365 conversion *t2;
8367 /* We're dealing with two standard conversion sequences.
8369 [over.ics.rank]
8371 Standard conversion sequence S1 is a better conversion
8372 sequence than standard conversion sequence S2 if
8374 --S1 is a proper subsequence of S2 (comparing the conversion
8375 sequences in the canonical form defined by _over.ics.scs_,
8376 excluding any Lvalue Transformation; the identity
8377 conversion sequence is considered to be a subsequence of
8378 any non-identity conversion sequence */
8380 t1 = ics1;
8381 while (t1->kind != ck_identity)
8382 t1 = next_conversion (t1);
8383 from_type1 = t1->type;
8385 t2 = ics2;
8386 while (t2->kind != ck_identity)
8387 t2 = next_conversion (t2);
8388 from_type2 = t2->type;
8391 /* One sequence can only be a subsequence of the other if they start with
8392 the same type. They can start with different types when comparing the
8393 second standard conversion sequence in two user-defined conversion
8394 sequences. */
8395 if (same_type_p (from_type1, from_type2))
8397 if (is_subseq (ics1, ics2))
8398 return 1;
8399 if (is_subseq (ics2, ics1))
8400 return -1;
8403 /* [over.ics.rank]
8405 Or, if not that,
8407 --the rank of S1 is better than the rank of S2 (by the rules
8408 defined below):
8410 Standard conversion sequences are ordered by their ranks: an Exact
8411 Match is a better conversion than a Promotion, which is a better
8412 conversion than a Conversion.
8414 Two conversion sequences with the same rank are indistinguishable
8415 unless one of the following rules applies:
8417 --A conversion that does not a convert a pointer, pointer to member,
8418 or std::nullptr_t to bool is better than one that does.
8420 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
8421 so that we do not have to check it explicitly. */
8422 if (ics1->rank < ics2->rank)
8423 return 1;
8424 else if (ics2->rank < ics1->rank)
8425 return -1;
8427 to_type1 = ics1->type;
8428 to_type2 = ics2->type;
8430 /* A conversion from scalar arithmetic type to complex is worse than a
8431 conversion between scalar arithmetic types. */
8432 if (same_type_p (from_type1, from_type2)
8433 && ARITHMETIC_TYPE_P (from_type1)
8434 && ARITHMETIC_TYPE_P (to_type1)
8435 && ARITHMETIC_TYPE_P (to_type2)
8436 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
8437 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
8439 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
8440 return -1;
8441 else
8442 return 1;
8445 if (TYPE_PTR_P (from_type1)
8446 && TYPE_PTR_P (from_type2)
8447 && TYPE_PTR_P (to_type1)
8448 && TYPE_PTR_P (to_type2))
8450 deref_from_type1 = TREE_TYPE (from_type1);
8451 deref_from_type2 = TREE_TYPE (from_type2);
8452 deref_to_type1 = TREE_TYPE (to_type1);
8453 deref_to_type2 = TREE_TYPE (to_type2);
8455 /* The rules for pointers to members A::* are just like the rules
8456 for pointers A*, except opposite: if B is derived from A then
8457 A::* converts to B::*, not vice versa. For that reason, we
8458 switch the from_ and to_ variables here. */
8459 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
8460 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
8461 || (TYPE_PTRMEMFUNC_P (from_type1)
8462 && TYPE_PTRMEMFUNC_P (from_type2)
8463 && TYPE_PTRMEMFUNC_P (to_type1)
8464 && TYPE_PTRMEMFUNC_P (to_type2)))
8466 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
8467 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
8468 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
8469 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
8472 if (deref_from_type1 != NULL_TREE
8473 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
8474 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
8476 /* This was one of the pointer or pointer-like conversions.
8478 [over.ics.rank]
8480 --If class B is derived directly or indirectly from class A,
8481 conversion of B* to A* is better than conversion of B* to
8482 void*, and conversion of A* to void* is better than
8483 conversion of B* to void*. */
8484 if (VOID_TYPE_P (deref_to_type1)
8485 && VOID_TYPE_P (deref_to_type2))
8487 if (is_properly_derived_from (deref_from_type1,
8488 deref_from_type2))
8489 return -1;
8490 else if (is_properly_derived_from (deref_from_type2,
8491 deref_from_type1))
8492 return 1;
8494 else if (VOID_TYPE_P (deref_to_type1)
8495 || VOID_TYPE_P (deref_to_type2))
8497 if (same_type_p (deref_from_type1, deref_from_type2))
8499 if (VOID_TYPE_P (deref_to_type2))
8501 if (is_properly_derived_from (deref_from_type1,
8502 deref_to_type1))
8503 return 1;
8505 /* We know that DEREF_TO_TYPE1 is `void' here. */
8506 else if (is_properly_derived_from (deref_from_type1,
8507 deref_to_type2))
8508 return -1;
8511 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
8512 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
8514 /* [over.ics.rank]
8516 --If class B is derived directly or indirectly from class A
8517 and class C is derived directly or indirectly from B,
8519 --conversion of C* to B* is better than conversion of C* to
8522 --conversion of B* to A* is better than conversion of C* to
8523 A* */
8524 if (same_type_p (deref_from_type1, deref_from_type2))
8526 if (is_properly_derived_from (deref_to_type1,
8527 deref_to_type2))
8528 return 1;
8529 else if (is_properly_derived_from (deref_to_type2,
8530 deref_to_type1))
8531 return -1;
8533 else if (same_type_p (deref_to_type1, deref_to_type2))
8535 if (is_properly_derived_from (deref_from_type2,
8536 deref_from_type1))
8537 return 1;
8538 else if (is_properly_derived_from (deref_from_type1,
8539 deref_from_type2))
8540 return -1;
8544 else if (CLASS_TYPE_P (non_reference (from_type1))
8545 && same_type_p (from_type1, from_type2))
8547 tree from = non_reference (from_type1);
8549 /* [over.ics.rank]
8551 --binding of an expression of type C to a reference of type
8552 B& is better than binding an expression of type C to a
8553 reference of type A&
8555 --conversion of C to B is better than conversion of C to A, */
8556 if (is_properly_derived_from (from, to_type1)
8557 && is_properly_derived_from (from, to_type2))
8559 if (is_properly_derived_from (to_type1, to_type2))
8560 return 1;
8561 else if (is_properly_derived_from (to_type2, to_type1))
8562 return -1;
8565 else if (CLASS_TYPE_P (non_reference (to_type1))
8566 && same_type_p (to_type1, to_type2))
8568 tree to = non_reference (to_type1);
8570 /* [over.ics.rank]
8572 --binding of an expression of type B to a reference of type
8573 A& is better than binding an expression of type C to a
8574 reference of type A&,
8576 --conversion of B to A is better than conversion of C to A */
8577 if (is_properly_derived_from (from_type1, to)
8578 && is_properly_derived_from (from_type2, to))
8580 if (is_properly_derived_from (from_type2, from_type1))
8581 return 1;
8582 else if (is_properly_derived_from (from_type1, from_type2))
8583 return -1;
8587 /* [over.ics.rank]
8589 --S1 and S2 differ only in their qualification conversion and yield
8590 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
8591 qualification signature of type T1 is a proper subset of the cv-
8592 qualification signature of type T2 */
8593 if (ics1->kind == ck_qual
8594 && ics2->kind == ck_qual
8595 && same_type_p (from_type1, from_type2))
8597 int result = comp_cv_qual_signature (to_type1, to_type2);
8598 if (result != 0)
8599 return result;
8602 /* [over.ics.rank]
8604 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
8605 to an implicit object parameter of a non-static member function
8606 declared without a ref-qualifier, and either S1 binds an lvalue
8607 reference to an lvalue and S2 binds an rvalue reference or S1 binds an
8608 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
8609 draft standard, 13.3.3.2)
8611 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
8612 types to which the references refer are the same type except for
8613 top-level cv-qualifiers, and the type to which the reference
8614 initialized by S2 refers is more cv-qualified than the type to
8615 which the reference initialized by S1 refers.
8617 DR 1328 [over.match.best]: the context is an initialization by
8618 conversion function for direct reference binding (13.3.1.6) of a
8619 reference to function type, the return type of F1 is the same kind of
8620 reference (i.e. lvalue or rvalue) as the reference being initialized,
8621 and the return type of F2 is not. */
8623 if (ref_conv1 && ref_conv2)
8625 if (!ref_conv1->this_p && !ref_conv2->this_p
8626 && (ref_conv1->rvaluedness_matches_p
8627 != ref_conv2->rvaluedness_matches_p)
8628 && (same_type_p (ref_conv1->type, ref_conv2->type)
8629 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
8630 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
8632 if (ref_conv1->bad_p
8633 && !same_type_p (TREE_TYPE (ref_conv1->type),
8634 TREE_TYPE (ref_conv2->type)))
8635 /* Don't prefer a bad conversion that drops cv-quals to a bad
8636 conversion with the wrong rvalueness. */
8637 return 0;
8638 return (ref_conv1->rvaluedness_matches_p
8639 - ref_conv2->rvaluedness_matches_p);
8642 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
8644 int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
8645 int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
8646 if (ref_conv1->bad_p)
8648 /* Prefer the one that drops fewer cv-quals. */
8649 tree ftype = next_conversion (ref_conv1)->type;
8650 int fquals = cp_type_quals (ftype);
8651 q1 ^= fquals;
8652 q2 ^= fquals;
8654 return comp_cv_qualification (q2, q1);
8658 /* Neither conversion sequence is better than the other. */
8659 return 0;
8662 /* The source type for this standard conversion sequence. */
8664 static tree
8665 source_type (conversion *t)
8667 for (;; t = next_conversion (t))
8669 if (t->kind == ck_user
8670 || t->kind == ck_ambig
8671 || t->kind == ck_identity)
8672 return t->type;
8674 gcc_unreachable ();
8677 /* Note a warning about preferring WINNER to LOSER. We do this by storing
8678 a pointer to LOSER and re-running joust to produce the warning if WINNER
8679 is actually used. */
8681 static void
8682 add_warning (struct z_candidate *winner, struct z_candidate *loser)
8684 candidate_warning *cw = (candidate_warning *)
8685 conversion_obstack_alloc (sizeof (candidate_warning));
8686 cw->loser = loser;
8687 cw->next = winner->warnings;
8688 winner->warnings = cw;
8691 /* Compare two candidates for overloading as described in
8692 [over.match.best]. Return values:
8694 1: cand1 is better than cand2
8695 -1: cand2 is better than cand1
8696 0: cand1 and cand2 are indistinguishable */
8698 static int
8699 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
8700 tsubst_flags_t complain)
8702 int winner = 0;
8703 int off1 = 0, off2 = 0;
8704 size_t i;
8705 size_t len;
8707 /* Candidates that involve bad conversions are always worse than those
8708 that don't. */
8709 if (cand1->viable > cand2->viable)
8710 return 1;
8711 if (cand1->viable < cand2->viable)
8712 return -1;
8714 /* If we have two pseudo-candidates for conversions to the same type,
8715 or two candidates for the same function, arbitrarily pick one. */
8716 if (cand1->fn == cand2->fn
8717 && (IS_TYPE_OR_DECL_P (cand1->fn)))
8718 return 1;
8720 /* Prefer a non-deleted function over an implicitly deleted move
8721 constructor or assignment operator. This differs slightly from the
8722 wording for issue 1402 (which says the move op is ignored by overload
8723 resolution), but this way produces better error messages. */
8724 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
8725 && TREE_CODE (cand2->fn) == FUNCTION_DECL
8726 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
8728 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
8729 && move_fn_p (cand1->fn))
8730 return -1;
8731 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
8732 && move_fn_p (cand2->fn))
8733 return 1;
8736 /* a viable function F1
8737 is defined to be a better function than another viable function F2 if
8738 for all arguments i, ICSi(F1) is not a worse conversion sequence than
8739 ICSi(F2), and then */
8741 /* for some argument j, ICSj(F1) is a better conversion sequence than
8742 ICSj(F2) */
8744 /* For comparing static and non-static member functions, we ignore
8745 the implicit object parameter of the non-static function. The
8746 standard says to pretend that the static function has an object
8747 parm, but that won't work with operator overloading. */
8748 len = cand1->num_convs;
8749 if (len != cand2->num_convs)
8751 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
8752 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
8754 if (DECL_CONSTRUCTOR_P (cand1->fn)
8755 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
8756 /* We're comparing a near-match list constructor and a near-match
8757 non-list constructor. Just treat them as unordered. */
8758 return 0;
8760 gcc_assert (static_1 != static_2);
8762 if (static_1)
8763 off2 = 1;
8764 else
8766 off1 = 1;
8767 --len;
8771 for (i = 0; i < len; ++i)
8773 conversion *t1 = cand1->convs[i + off1];
8774 conversion *t2 = cand2->convs[i + off2];
8775 int comp = compare_ics (t1, t2);
8777 if (comp != 0)
8779 if ((complain & tf_warning)
8780 && warn_sign_promo
8781 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
8782 == cr_std + cr_promotion)
8783 && t1->kind == ck_std
8784 && t2->kind == ck_std
8785 && TREE_CODE (t1->type) == INTEGER_TYPE
8786 && TREE_CODE (t2->type) == INTEGER_TYPE
8787 && (TYPE_PRECISION (t1->type)
8788 == TYPE_PRECISION (t2->type))
8789 && (TYPE_UNSIGNED (next_conversion (t1)->type)
8790 || (TREE_CODE (next_conversion (t1)->type)
8791 == ENUMERAL_TYPE)))
8793 tree type = next_conversion (t1)->type;
8794 tree type1, type2;
8795 struct z_candidate *w, *l;
8796 if (comp > 0)
8797 type1 = t1->type, type2 = t2->type,
8798 w = cand1, l = cand2;
8799 else
8800 type1 = t2->type, type2 = t1->type,
8801 w = cand2, l = cand1;
8803 if (warn)
8805 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
8806 type, type1, type2);
8807 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
8809 else
8810 add_warning (w, l);
8813 if (winner && comp != winner)
8815 winner = 0;
8816 goto tweak;
8818 winner = comp;
8822 /* warn about confusing overload resolution for user-defined conversions,
8823 either between a constructor and a conversion op, or between two
8824 conversion ops. */
8825 if ((complain & tf_warning)
8826 && winner && warn_conversion && cand1->second_conv
8827 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
8828 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
8830 struct z_candidate *w, *l;
8831 bool give_warning = false;
8833 if (winner == 1)
8834 w = cand1, l = cand2;
8835 else
8836 w = cand2, l = cand1;
8838 /* We don't want to complain about `X::operator T1 ()'
8839 beating `X::operator T2 () const', when T2 is a no less
8840 cv-qualified version of T1. */
8841 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
8842 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
8844 tree t = TREE_TYPE (TREE_TYPE (l->fn));
8845 tree f = TREE_TYPE (TREE_TYPE (w->fn));
8847 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
8849 t = TREE_TYPE (t);
8850 f = TREE_TYPE (f);
8852 if (!comp_ptr_ttypes (t, f))
8853 give_warning = true;
8855 else
8856 give_warning = true;
8858 if (!give_warning)
8859 /*NOP*/;
8860 else if (warn)
8862 tree source = source_type (w->convs[0]);
8863 if (! DECL_CONSTRUCTOR_P (w->fn))
8864 source = TREE_TYPE (source);
8865 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
8866 && warning (OPT_Wconversion, " for conversion from %qT to %qT",
8867 source, w->second_conv->type))
8869 inform (input_location, " because conversion sequence for the argument is better");
8872 else
8873 add_warning (w, l);
8876 if (winner)
8877 return winner;
8879 /* DR 495 moved this tiebreaker above the template ones. */
8880 /* or, if not that,
8881 the context is an initialization by user-defined conversion (see
8882 _dcl.init_ and _over.match.user_) and the standard conversion
8883 sequence from the return type of F1 to the destination type (i.e.,
8884 the type of the entity being initialized) is a better conversion
8885 sequence than the standard conversion sequence from the return type
8886 of F2 to the destination type. */
8888 if (cand1->second_conv)
8890 winner = compare_ics (cand1->second_conv, cand2->second_conv);
8891 if (winner)
8892 return winner;
8895 /* or, if not that,
8896 F1 is a non-template function and F2 is a template function
8897 specialization. */
8899 if (!cand1->template_decl && cand2->template_decl)
8900 return 1;
8901 else if (cand1->template_decl && !cand2->template_decl)
8902 return -1;
8904 /* or, if not that,
8905 F1 and F2 are template functions and the function template for F1 is
8906 more specialized than the template for F2 according to the partial
8907 ordering rules. */
8909 if (cand1->template_decl && cand2->template_decl)
8911 winner = more_specialized_fn
8912 (TI_TEMPLATE (cand1->template_decl),
8913 TI_TEMPLATE (cand2->template_decl),
8914 /* [temp.func.order]: The presence of unused ellipsis and default
8915 arguments has no effect on the partial ordering of function
8916 templates. add_function_candidate() will not have
8917 counted the "this" argument for constructors. */
8918 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
8919 if (winner)
8920 return winner;
8923 /* Check whether we can discard a builtin candidate, either because we
8924 have two identical ones or matching builtin and non-builtin candidates.
8926 (Pedantically in the latter case the builtin which matched the user
8927 function should not be added to the overload set, but we spot it here.
8929 [over.match.oper]
8930 ... the builtin candidates include ...
8931 - do not have the same parameter type list as any non-template
8932 non-member candidate. */
8934 if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
8936 for (i = 0; i < len; ++i)
8937 if (!same_type_p (cand1->convs[i]->type,
8938 cand2->convs[i]->type))
8939 break;
8940 if (i == cand1->num_convs)
8942 if (cand1->fn == cand2->fn)
8943 /* Two built-in candidates; arbitrarily pick one. */
8944 return 1;
8945 else if (identifier_p (cand1->fn))
8946 /* cand1 is built-in; prefer cand2. */
8947 return -1;
8948 else
8949 /* cand2 is built-in; prefer cand1. */
8950 return 1;
8954 /* For candidates of a multi-versioned function, make the version with
8955 the highest priority win. This version will be checked for dispatching
8956 first. If this version can be inlined into the caller, the front-end
8957 will simply make a direct call to this function. */
8959 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
8960 && DECL_FUNCTION_VERSIONED (cand1->fn)
8961 && TREE_CODE (cand2->fn) == FUNCTION_DECL
8962 && DECL_FUNCTION_VERSIONED (cand2->fn))
8964 tree f1 = TREE_TYPE (cand1->fn);
8965 tree f2 = TREE_TYPE (cand2->fn);
8966 tree p1 = TYPE_ARG_TYPES (f1);
8967 tree p2 = TYPE_ARG_TYPES (f2);
8969 /* Check if cand1->fn and cand2->fn are versions of the same function. It
8970 is possible that cand1->fn and cand2->fn are function versions but of
8971 different functions. Check types to see if they are versions of the same
8972 function. */
8973 if (compparms (p1, p2)
8974 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8976 /* Always make the version with the higher priority, more
8977 specialized, win. */
8978 gcc_assert (targetm.compare_version_priority);
8979 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
8980 return 1;
8981 else
8982 return -1;
8986 /* If the two function declarations represent the same function (this can
8987 happen with declarations in multiple scopes and arg-dependent lookup),
8988 arbitrarily choose one. But first make sure the default args we're
8989 using match. */
8990 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
8991 && equal_functions (cand1->fn, cand2->fn))
8993 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
8994 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
8996 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
8998 for (i = 0; i < len; ++i)
9000 /* Don't crash if the fn is variadic. */
9001 if (!parms1)
9002 break;
9003 parms1 = TREE_CHAIN (parms1);
9004 parms2 = TREE_CHAIN (parms2);
9007 if (off1)
9008 parms1 = TREE_CHAIN (parms1);
9009 else if (off2)
9010 parms2 = TREE_CHAIN (parms2);
9012 for (; parms1; ++i)
9014 if (!cp_tree_equal (TREE_PURPOSE (parms1),
9015 TREE_PURPOSE (parms2)))
9017 if (warn)
9019 if (complain & tf_error)
9021 if (permerror (input_location,
9022 "default argument mismatch in "
9023 "overload resolution"))
9025 inform (input_location,
9026 " candidate 1: %q+#F", cand1->fn);
9027 inform (input_location,
9028 " candidate 2: %q+#F", cand2->fn);
9031 else
9032 return 0;
9034 else
9035 add_warning (cand1, cand2);
9036 break;
9038 parms1 = TREE_CHAIN (parms1);
9039 parms2 = TREE_CHAIN (parms2);
9042 return 1;
9045 tweak:
9047 /* Extension: If the worst conversion for one candidate is worse than the
9048 worst conversion for the other, take the first. */
9049 if (!pedantic && (complain & tf_warning_or_error))
9051 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
9052 struct z_candidate *w = 0, *l = 0;
9054 for (i = 0; i < len; ++i)
9056 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
9057 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
9058 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
9059 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
9061 if (rank1 < rank2)
9062 winner = 1, w = cand1, l = cand2;
9063 if (rank1 > rank2)
9064 winner = -1, w = cand2, l = cand1;
9065 if (winner)
9067 /* Don't choose a deleted function over ambiguity. */
9068 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
9069 return 0;
9070 if (warn)
9072 pedwarn (input_location, 0,
9073 "ISO C++ says that these are ambiguous, even "
9074 "though the worst conversion for the first is better than "
9075 "the worst conversion for the second:");
9076 print_z_candidate (input_location, _("candidate 1:"), w);
9077 print_z_candidate (input_location, _("candidate 2:"), l);
9079 else
9080 add_warning (w, l);
9081 return winner;
9085 gcc_assert (!winner);
9086 return 0;
9089 /* Given a list of candidates for overloading, find the best one, if any.
9090 This algorithm has a worst case of O(2n) (winner is last), and a best
9091 case of O(n/2) (totally ambiguous); much better than a sorting
9092 algorithm. */
9094 static struct z_candidate *
9095 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
9097 struct z_candidate *champ = candidates, *challenger;
9098 int fate;
9099 int champ_compared_to_predecessor = 0;
9101 /* Walk through the list once, comparing each current champ to the next
9102 candidate, knocking out a candidate or two with each comparison. */
9104 for (challenger = champ->next; challenger; )
9106 fate = joust (champ, challenger, 0, complain);
9107 if (fate == 1)
9108 challenger = challenger->next;
9109 else
9111 if (fate == 0)
9113 champ = challenger->next;
9114 if (champ == 0)
9115 return NULL;
9116 champ_compared_to_predecessor = 0;
9118 else
9120 champ = challenger;
9121 champ_compared_to_predecessor = 1;
9124 challenger = champ->next;
9128 /* Make sure the champ is better than all the candidates it hasn't yet
9129 been compared to. */
9131 for (challenger = candidates;
9132 challenger != champ
9133 && !(champ_compared_to_predecessor && challenger->next == champ);
9134 challenger = challenger->next)
9136 fate = joust (champ, challenger, 0, complain);
9137 if (fate != 1)
9138 return NULL;
9141 return champ;
9144 /* Returns nonzero if things of type FROM can be converted to TO. */
9146 bool
9147 can_convert (tree to, tree from, tsubst_flags_t complain)
9149 tree arg = NULL_TREE;
9150 /* implicit_conversion only considers user-defined conversions
9151 if it has an expression for the call argument list. */
9152 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
9153 arg = build1 (CAST_EXPR, from, NULL_TREE);
9154 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
9157 /* Returns nonzero if things of type FROM can be converted to TO with a
9158 standard conversion. */
9160 bool
9161 can_convert_standard (tree to, tree from, tsubst_flags_t complain)
9163 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
9166 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
9168 bool
9169 can_convert_arg (tree to, tree from, tree arg, int flags,
9170 tsubst_flags_t complain)
9172 conversion *t;
9173 void *p;
9174 bool ok_p;
9176 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9177 p = conversion_obstack_alloc (0);
9178 /* We want to discard any access checks done for this test,
9179 as we might not be in the appropriate access context and
9180 we'll do the check again when we actually perform the
9181 conversion. */
9182 push_deferring_access_checks (dk_deferred);
9184 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
9185 flags, complain);
9186 ok_p = (t && !t->bad_p);
9188 /* Discard the access checks now. */
9189 pop_deferring_access_checks ();
9190 /* Free all the conversions we allocated. */
9191 obstack_free (&conversion_obstack, p);
9193 return ok_p;
9196 /* Like can_convert_arg, but allows dubious conversions as well. */
9198 bool
9199 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
9200 tsubst_flags_t complain)
9202 conversion *t;
9203 void *p;
9205 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9206 p = conversion_obstack_alloc (0);
9207 /* Try to perform the conversion. */
9208 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
9209 flags, complain);
9210 /* Free all the conversions we allocated. */
9211 obstack_free (&conversion_obstack, p);
9213 return t != NULL;
9216 /* Convert EXPR to TYPE. Return the converted expression.
9218 Note that we allow bad conversions here because by the time we get to
9219 this point we are committed to doing the conversion. If we end up
9220 doing a bad conversion, convert_like will complain. */
9222 tree
9223 perform_implicit_conversion_flags (tree type, tree expr,
9224 tsubst_flags_t complain, int flags)
9226 conversion *conv;
9227 void *p;
9228 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
9230 if (error_operand_p (expr))
9231 return error_mark_node;
9233 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9234 p = conversion_obstack_alloc (0);
9236 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
9237 /*c_cast_p=*/false,
9238 flags, complain);
9240 if (!conv)
9242 if (complain & tf_error)
9244 /* If expr has unknown type, then it is an overloaded function.
9245 Call instantiate_type to get good error messages. */
9246 if (TREE_TYPE (expr) == unknown_type_node)
9247 instantiate_type (type, expr, complain);
9248 else if (invalid_nonstatic_memfn_p (expr, complain))
9249 /* We gave an error. */;
9250 else
9251 error_at (loc, "could not convert %qE from %qT to %qT", expr,
9252 TREE_TYPE (expr), type);
9254 expr = error_mark_node;
9256 else if (processing_template_decl && conv->kind != ck_identity)
9258 /* In a template, we are only concerned about determining the
9259 type of non-dependent expressions, so we do not have to
9260 perform the actual conversion. But for initializers, we
9261 need to be able to perform it at instantiation
9262 (or fold_non_dependent_expr) time. */
9263 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
9264 if (!(flags & LOOKUP_ONLYCONVERTING))
9265 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
9267 else
9268 expr = convert_like (conv, expr, complain);
9270 /* Free all the conversions we allocated. */
9271 obstack_free (&conversion_obstack, p);
9273 return expr;
9276 tree
9277 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
9279 return perform_implicit_conversion_flags (type, expr, complain,
9280 LOOKUP_IMPLICIT);
9283 /* Convert EXPR to TYPE (as a direct-initialization) if that is
9284 permitted. If the conversion is valid, the converted expression is
9285 returned. Otherwise, NULL_TREE is returned, except in the case
9286 that TYPE is a class type; in that case, an error is issued. If
9287 C_CAST_P is true, then this direct-initialization is taking
9288 place as part of a static_cast being attempted as part of a C-style
9289 cast. */
9291 tree
9292 perform_direct_initialization_if_possible (tree type,
9293 tree expr,
9294 bool c_cast_p,
9295 tsubst_flags_t complain)
9297 conversion *conv;
9298 void *p;
9300 if (type == error_mark_node || error_operand_p (expr))
9301 return error_mark_node;
9302 /* [dcl.init]
9304 If the destination type is a (possibly cv-qualified) class type:
9306 -- If the initialization is direct-initialization ...,
9307 constructors are considered. ... If no constructor applies, or
9308 the overload resolution is ambiguous, the initialization is
9309 ill-formed. */
9310 if (CLASS_TYPE_P (type))
9312 vec<tree, va_gc> *args = make_tree_vector_single (expr);
9313 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
9314 &args, type, LOOKUP_NORMAL, complain);
9315 release_tree_vector (args);
9316 return build_cplus_new (type, expr, complain);
9319 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9320 p = conversion_obstack_alloc (0);
9322 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
9323 c_cast_p,
9324 LOOKUP_NORMAL, complain);
9325 if (!conv || conv->bad_p)
9326 expr = NULL_TREE;
9327 else
9328 expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
9329 /*issue_conversion_warnings=*/false,
9330 c_cast_p,
9331 complain);
9333 /* Free all the conversions we allocated. */
9334 obstack_free (&conversion_obstack, p);
9336 return expr;
9339 /* When initializing a reference that lasts longer than a full-expression,
9340 this special rule applies:
9342 [class.temporary]
9344 The temporary to which the reference is bound or the temporary
9345 that is the complete object to which the reference is bound
9346 persists for the lifetime of the reference.
9348 The temporaries created during the evaluation of the expression
9349 initializing the reference, except the temporary to which the
9350 reference is bound, are destroyed at the end of the
9351 full-expression in which they are created.
9353 In that case, we store the converted expression into a new
9354 VAR_DECL in a new scope.
9356 However, we want to be careful not to create temporaries when
9357 they are not required. For example, given:
9359 struct B {};
9360 struct D : public B {};
9361 D f();
9362 const B& b = f();
9364 there is no need to copy the return value from "f"; we can just
9365 extend its lifetime. Similarly, given:
9367 struct S {};
9368 struct T { operator S(); };
9369 T t;
9370 const S& s = t;
9372 we can extend the lifetime of the return value of the conversion
9373 operator.
9375 The next several functions are involved in this lifetime extension. */
9377 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
9378 reference is being bound to a temporary. Create and return a new
9379 VAR_DECL with the indicated TYPE; this variable will store the value to
9380 which the reference is bound. */
9382 tree
9383 make_temporary_var_for_ref_to_temp (tree decl, tree type)
9385 tree var;
9387 /* Create the variable. */
9388 var = create_temporary_var (type);
9390 /* Register the variable. */
9391 if (VAR_P (decl)
9392 && (TREE_STATIC (decl) || DECL_THREAD_LOCAL_P (decl)))
9394 /* Namespace-scope or local static; give it a mangled name. */
9395 /* FIXME share comdat with decl? */
9396 tree name;
9398 TREE_STATIC (var) = TREE_STATIC (decl);
9399 set_decl_tls_model (var, DECL_TLS_MODEL (decl));
9400 name = mangle_ref_init_variable (decl);
9401 DECL_NAME (var) = name;
9402 SET_DECL_ASSEMBLER_NAME (var, name);
9403 var = pushdecl_top_level (var);
9405 else
9406 /* Create a new cleanup level if necessary. */
9407 maybe_push_cleanup_level (type);
9409 return var;
9412 /* EXPR is the initializer for a variable DECL of reference or
9413 std::initializer_list type. Create, push and return a new VAR_DECL
9414 for the initializer so that it will live as long as DECL. Any
9415 cleanup for the new variable is returned through CLEANUP, and the
9416 code to initialize the new variable is returned through INITP. */
9418 static tree
9419 set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
9420 tree *initp)
9422 tree init;
9423 tree type;
9424 tree var;
9426 /* Create the temporary variable. */
9427 type = TREE_TYPE (expr);
9428 var = make_temporary_var_for_ref_to_temp (decl, type);
9429 layout_decl (var, 0);
9430 /* If the rvalue is the result of a function call it will be
9431 a TARGET_EXPR. If it is some other construct (such as a
9432 member access expression where the underlying object is
9433 itself the result of a function call), turn it into a
9434 TARGET_EXPR here. It is important that EXPR be a
9435 TARGET_EXPR below since otherwise the INIT_EXPR will
9436 attempt to make a bitwise copy of EXPR to initialize
9437 VAR. */
9438 if (TREE_CODE (expr) != TARGET_EXPR)
9439 expr = get_target_expr (expr);
9441 if (TREE_CODE (decl) == FIELD_DECL
9442 && extra_warnings && !TREE_NO_WARNING (decl))
9444 warning (OPT_Wextra, "a temporary bound to %qD only persists "
9445 "until the constructor exits", decl);
9446 TREE_NO_WARNING (decl) = true;
9449 /* Recursively extend temps in this initializer. */
9450 TARGET_EXPR_INITIAL (expr)
9451 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups);
9453 /* Any reference temp has a non-trivial initializer. */
9454 DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
9456 /* If the initializer is constant, put it in DECL_INITIAL so we get
9457 static initialization and use in constant expressions. */
9458 init = maybe_constant_init (expr);
9459 if (TREE_CONSTANT (init))
9461 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
9463 /* 5.19 says that a constant expression can include an
9464 lvalue-rvalue conversion applied to "a glvalue of literal type
9465 that refers to a non-volatile temporary object initialized
9466 with a constant expression". Rather than try to communicate
9467 that this VAR_DECL is a temporary, just mark it constexpr.
9469 Currently this is only useful for initializer_list temporaries,
9470 since reference vars can't appear in constant expressions. */
9471 DECL_DECLARED_CONSTEXPR_P (var) = true;
9472 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
9473 TREE_CONSTANT (var) = true;
9475 DECL_INITIAL (var) = init;
9476 init = NULL_TREE;
9478 else
9479 /* Create the INIT_EXPR that will initialize the temporary
9480 variable. */
9481 init = build2 (INIT_EXPR, type, var, expr);
9482 if (at_function_scope_p ())
9484 add_decl_expr (var);
9486 if (TREE_STATIC (var))
9487 init = add_stmt_to_compound (init, register_dtor_fn (var));
9488 else
9490 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
9491 if (cleanup)
9492 vec_safe_push (*cleanups, cleanup);
9495 /* We must be careful to destroy the temporary only
9496 after its initialization has taken place. If the
9497 initialization throws an exception, then the
9498 destructor should not be run. We cannot simply
9499 transform INIT into something like:
9501 (INIT, ({ CLEANUP_STMT; }))
9503 because emit_local_var always treats the
9504 initializer as a full-expression. Thus, the
9505 destructor would run too early; it would run at the
9506 end of initializing the reference variable, rather
9507 than at the end of the block enclosing the
9508 reference variable.
9510 The solution is to pass back a cleanup expression
9511 which the caller is responsible for attaching to
9512 the statement tree. */
9514 else
9516 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
9517 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9519 if (DECL_THREAD_LOCAL_P (var))
9520 tls_aggregates = tree_cons (NULL_TREE, var,
9521 tls_aggregates);
9522 else
9523 static_aggregates = tree_cons (NULL_TREE, var,
9524 static_aggregates);
9526 else
9527 /* Check whether the dtor is callable. */
9528 cxx_maybe_build_cleanup (var, tf_warning_or_error);
9531 *initp = init;
9532 return var;
9535 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
9536 initializing a variable of that TYPE. */
9538 tree
9539 initialize_reference (tree type, tree expr,
9540 int flags, tsubst_flags_t complain)
9542 conversion *conv;
9543 void *p;
9544 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
9546 if (type == error_mark_node || error_operand_p (expr))
9547 return error_mark_node;
9549 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9550 p = conversion_obstack_alloc (0);
9552 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
9553 flags, complain);
9554 if (!conv || conv->bad_p)
9556 if (complain & tf_error)
9558 if (conv)
9559 convert_like (conv, expr, complain);
9560 else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
9561 && !TYPE_REF_IS_RVALUE (type)
9562 && !real_lvalue_p (expr))
9563 error_at (loc, "invalid initialization of non-const reference of "
9564 "type %qT from an rvalue of type %qT",
9565 type, TREE_TYPE (expr));
9566 else
9567 error_at (loc, "invalid initialization of reference of type "
9568 "%qT from expression of type %qT", type,
9569 TREE_TYPE (expr));
9571 return error_mark_node;
9574 if (conv->kind == ck_ref_bind)
9575 /* Perform the conversion. */
9576 expr = convert_like (conv, expr, complain);
9577 else if (conv->kind == ck_ambig)
9578 /* We gave an error in build_user_type_conversion_1. */
9579 expr = error_mark_node;
9580 else
9581 gcc_unreachable ();
9583 /* Free all the conversions we allocated. */
9584 obstack_free (&conversion_obstack, p);
9586 return expr;
9589 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
9590 which is bound either to a reference or a std::initializer_list. */
9592 static tree
9593 extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups)
9595 tree sub = init;
9596 tree *p;
9597 STRIP_NOPS (sub);
9598 if (TREE_CODE (sub) == COMPOUND_EXPR)
9600 TREE_OPERAND (sub, 1)
9601 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups);
9602 return init;
9604 if (TREE_CODE (sub) != ADDR_EXPR)
9605 return init;
9606 /* Deal with binding to a subobject. */
9607 for (p = &TREE_OPERAND (sub, 0); TREE_CODE (*p) == COMPONENT_REF; )
9608 p = &TREE_OPERAND (*p, 0);
9609 if (TREE_CODE (*p) == TARGET_EXPR)
9611 tree subinit = NULL_TREE;
9612 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit);
9613 if (subinit)
9614 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
9615 recompute_tree_invariant_for_addr_expr (sub);
9617 return init;
9620 /* INIT is part of the initializer for DECL. If there are any
9621 reference or initializer lists being initialized, extend their
9622 lifetime to match that of DECL. */
9624 tree
9625 extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
9627 tree type = TREE_TYPE (init);
9628 if (processing_template_decl)
9629 return init;
9630 if (TREE_CODE (type) == REFERENCE_TYPE)
9631 init = extend_ref_init_temps_1 (decl, init, cleanups);
9632 else if (is_std_init_list (type))
9634 /* The temporary array underlying a std::initializer_list
9635 is handled like a reference temporary. */
9636 tree ctor = init;
9637 if (TREE_CODE (ctor) == TARGET_EXPR)
9638 ctor = TARGET_EXPR_INITIAL (ctor);
9639 if (TREE_CODE (ctor) == CONSTRUCTOR)
9641 tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
9642 array = extend_ref_init_temps_1 (decl, array, cleanups);
9643 CONSTRUCTOR_ELT (ctor, 0)->value = array;
9646 else if (TREE_CODE (init) == CONSTRUCTOR)
9648 unsigned i;
9649 constructor_elt *p;
9650 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
9651 FOR_EACH_VEC_SAFE_ELT (elts, i, p)
9652 p->value = extend_ref_init_temps (decl, p->value, cleanups);
9655 return init;
9658 /* Returns true iff an initializer for TYPE could contain temporaries that
9659 need to be extended because they are bound to references or
9660 std::initializer_list. */
9662 bool
9663 type_has_extended_temps (tree type)
9665 type = strip_array_types (type);
9666 if (TREE_CODE (type) == REFERENCE_TYPE)
9667 return true;
9668 if (CLASS_TYPE_P (type))
9670 if (is_std_init_list (type))
9671 return true;
9672 for (tree f = next_initializable_field (TYPE_FIELDS (type));
9673 f; f = next_initializable_field (DECL_CHAIN (f)))
9674 if (type_has_extended_temps (TREE_TYPE (f)))
9675 return true;
9677 return false;
9680 /* Returns true iff TYPE is some variant of std::initializer_list. */
9682 bool
9683 is_std_init_list (tree type)
9685 /* Look through typedefs. */
9686 if (!TYPE_P (type))
9687 return false;
9688 if (cxx_dialect == cxx98)
9689 return false;
9690 type = TYPE_MAIN_VARIANT (type);
9691 return (CLASS_TYPE_P (type)
9692 && CP_TYPE_CONTEXT (type) == std_node
9693 && CLASSTYPE_TEMPLATE_INFO (type)
9694 && strcmp (TYPE_NAME_STRING (type), "initializer_list") == 0);
9697 /* Returns true iff DECL is a list constructor: i.e. a constructor which
9698 will accept an argument list of a single std::initializer_list<T>. */
9700 bool
9701 is_list_ctor (tree decl)
9703 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
9704 tree arg;
9706 if (!args || args == void_list_node)
9707 return false;
9709 arg = non_reference (TREE_VALUE (args));
9710 if (!is_std_init_list (arg))
9711 return false;
9713 args = TREE_CHAIN (args);
9715 if (args && args != void_list_node && !TREE_PURPOSE (args))
9716 /* There are more non-defaulted parms. */
9717 return false;
9719 return true;
9722 #include "gt-cp-call.h"