c-common.c (handle_tls_model_attribute): Use set_decl_tls_model.
[official-gcc.git] / gcc / cp / call.c
blob1d4c4f99e2f3d066796aef45a1a6abe2c5fe592b
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);
2029 else
2031 parmtype = build_pointer_type (parmtype);
2032 arg = build_this (arg);
2033 argtype = lvalue_type (arg);
2037 /* Core issue 899: When [copy-]initializing a temporary to be bound
2038 to the first parameter of a copy constructor (12.8) called with
2039 a single argument in the context of direct-initialization,
2040 explicit conversion functions are also considered.
2042 So set LOOKUP_COPY_PARM to let reference_binding know that
2043 it's being called in that context. We generalize the above
2044 to handle move constructors and template constructors as well;
2045 the standardese should soon be updated similarly. */
2046 if (ctype && i == 0 && (len-skip == 1)
2047 && DECL_CONSTRUCTOR_P (fn)
2048 && parmtype != error_mark_node
2049 && (same_type_ignoring_top_level_qualifiers_p
2050 (non_reference (parmtype), ctype)))
2052 if (!(flags & LOOKUP_ONLYCONVERTING))
2053 lflags |= LOOKUP_COPY_PARM;
2054 /* We allow user-defined conversions within init-lists, but
2055 don't list-initialize the copy parm, as that would mean
2056 using two levels of braces for the same type. */
2057 if ((flags & LOOKUP_LIST_INIT_CTOR)
2058 && BRACE_ENCLOSED_INITIALIZER_P (arg))
2059 lflags |= LOOKUP_NO_CONVERSION;
2061 else
2062 lflags |= LOOKUP_ONLYCONVERTING;
2064 t = implicit_conversion (parmtype, argtype, arg,
2065 /*c_cast_p=*/false, lflags, complain);
2066 to_type = parmtype;
2068 else
2070 t = build_identity_conv (argtype, arg);
2071 t->ellipsis_p = true;
2072 to_type = argtype;
2075 if (t && is_this)
2076 t->this_p = true;
2078 convs[i] = t;
2079 if (! t)
2081 viable = 0;
2082 reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
2083 break;
2086 if (t->bad_p)
2088 viable = -1;
2089 reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type);
2093 out:
2094 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2095 access_path, conversion_path, viable, reason, flags);
2098 /* Create an overload candidate for the conversion function FN which will
2099 be invoked for expression OBJ, producing a pointer-to-function which
2100 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2101 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2102 passed on to implicit_conversion.
2104 Actually, we don't really care about FN; we care about the type it
2105 converts to. There may be multiple conversion functions that will
2106 convert to that type, and we rely on build_user_type_conversion_1 to
2107 choose the best one; so when we create our candidate, we record the type
2108 instead of the function. */
2110 static struct z_candidate *
2111 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2112 tree first_arg, const vec<tree, va_gc> *arglist,
2113 tree access_path, tree conversion_path,
2114 tsubst_flags_t complain)
2116 tree totype = TREE_TYPE (TREE_TYPE (fn));
2117 int i, len, viable, flags;
2118 tree parmlist, parmnode;
2119 conversion **convs;
2120 struct rejection_reason *reason;
2122 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2123 parmlist = TREE_TYPE (parmlist);
2124 parmlist = TYPE_ARG_TYPES (parmlist);
2126 len = vec_safe_length (arglist) + (first_arg != NULL_TREE ? 1 : 0) + 1;
2127 convs = alloc_conversions (len);
2128 parmnode = parmlist;
2129 viable = 1;
2130 flags = LOOKUP_IMPLICIT;
2131 reason = NULL;
2133 /* Don't bother looking up the same type twice. */
2134 if (*candidates && (*candidates)->fn == totype)
2135 return NULL;
2137 for (i = 0; i < len; ++i)
2139 tree arg, argtype, convert_type = NULL_TREE;
2140 conversion *t;
2142 if (i == 0)
2143 arg = obj;
2144 else if (i == 1 && first_arg != NULL_TREE)
2145 arg = first_arg;
2146 else
2147 arg = (*arglist)[i - (first_arg != NULL_TREE ? 1 : 0) - 1];
2148 argtype = lvalue_type (arg);
2150 if (i == 0)
2152 t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false,
2153 flags, complain);
2154 convert_type = totype;
2156 else if (parmnode == void_list_node)
2157 break;
2158 else if (parmnode)
2160 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2161 /*c_cast_p=*/false, flags, complain);
2162 convert_type = TREE_VALUE (parmnode);
2164 else
2166 t = build_identity_conv (argtype, arg);
2167 t->ellipsis_p = true;
2168 convert_type = argtype;
2171 convs[i] = t;
2172 if (! t)
2173 break;
2175 if (t->bad_p)
2177 viable = -1;
2178 reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type);
2181 if (i == 0)
2182 continue;
2184 if (parmnode)
2185 parmnode = TREE_CHAIN (parmnode);
2188 if (i < len
2189 || ! sufficient_parms_p (parmnode))
2191 int remaining = remaining_arguments (parmnode);
2192 viable = 0;
2193 reason = arity_rejection (NULL_TREE, i + remaining, len);
2196 return add_candidate (candidates, totype, first_arg, arglist, len, convs,
2197 access_path, conversion_path, viable, reason, flags);
2200 static void
2201 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2202 tree type1, tree type2, tree *args, tree *argtypes,
2203 int flags, tsubst_flags_t complain)
2205 conversion *t;
2206 conversion **convs;
2207 size_t num_convs;
2208 int viable = 1, i;
2209 tree types[2];
2210 struct rejection_reason *reason = NULL;
2212 types[0] = type1;
2213 types[1] = type2;
2215 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
2216 convs = alloc_conversions (num_convs);
2218 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2219 conversion ops are allowed. We handle that here by just checking for
2220 boolean_type_node because other operators don't ask for it. COND_EXPR
2221 also does contextual conversion to bool for the first operand, but we
2222 handle that in build_conditional_expr, and type1 here is operand 2. */
2223 if (type1 != boolean_type_node)
2224 flags |= LOOKUP_ONLYCONVERTING;
2226 for (i = 0; i < 2; ++i)
2228 if (! args[i])
2229 break;
2231 t = implicit_conversion (types[i], argtypes[i], args[i],
2232 /*c_cast_p=*/false, flags, complain);
2233 if (! t)
2235 viable = 0;
2236 /* We need something for printing the candidate. */
2237 t = build_identity_conv (types[i], NULL_TREE);
2238 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2239 types[i]);
2241 else if (t->bad_p)
2243 viable = 0;
2244 reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2245 types[i]);
2247 convs[i] = t;
2250 /* For COND_EXPR we rearranged the arguments; undo that now. */
2251 if (args[2])
2253 convs[2] = convs[1];
2254 convs[1] = convs[0];
2255 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2256 /*c_cast_p=*/false, flags,
2257 complain);
2258 if (t)
2259 convs[0] = t;
2260 else
2262 viable = 0;
2263 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2264 boolean_type_node);
2268 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2269 num_convs, convs,
2270 /*access_path=*/NULL_TREE,
2271 /*conversion_path=*/NULL_TREE,
2272 viable, reason, flags);
2275 static bool
2276 is_complete (tree t)
2278 return COMPLETE_TYPE_P (complete_type (t));
2281 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2283 static bool
2284 promoted_arithmetic_type_p (tree type)
2286 /* [over.built]
2288 In this section, the term promoted integral type is used to refer
2289 to those integral types which are preserved by integral promotion
2290 (including e.g. int and long but excluding e.g. char).
2291 Similarly, the term promoted arithmetic type refers to promoted
2292 integral types plus floating types. */
2293 return ((CP_INTEGRAL_TYPE_P (type)
2294 && same_type_p (type_promotes_to (type), type))
2295 || TREE_CODE (type) == REAL_TYPE);
2298 /* Create any builtin operator overload candidates for the operator in
2299 question given the converted operand types TYPE1 and TYPE2. The other
2300 args are passed through from add_builtin_candidates to
2301 build_builtin_candidate.
2303 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2304 If CODE is requires candidates operands of the same type of the kind
2305 of which TYPE1 and TYPE2 are, we add both candidates
2306 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2308 static void
2309 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2310 enum tree_code code2, tree fnname, tree type1,
2311 tree type2, tree *args, tree *argtypes, int flags,
2312 tsubst_flags_t complain)
2314 switch (code)
2316 case POSTINCREMENT_EXPR:
2317 case POSTDECREMENT_EXPR:
2318 args[1] = integer_zero_node;
2319 type2 = integer_type_node;
2320 break;
2321 default:
2322 break;
2325 switch (code)
2328 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2329 and VQ is either volatile or empty, there exist candidate operator
2330 functions of the form
2331 VQ T& operator++(VQ T&);
2332 T operator++(VQ T&, int);
2333 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
2334 type other than bool, and VQ is either volatile or empty, there exist
2335 candidate operator functions of the form
2336 VQ T& operator--(VQ T&);
2337 T operator--(VQ T&, int);
2338 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
2339 complete object type, and VQ is either volatile or empty, there exist
2340 candidate operator functions of the form
2341 T*VQ& operator++(T*VQ&);
2342 T*VQ& operator--(T*VQ&);
2343 T* operator++(T*VQ&, int);
2344 T* operator--(T*VQ&, int); */
2346 case POSTDECREMENT_EXPR:
2347 case PREDECREMENT_EXPR:
2348 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2349 return;
2350 case POSTINCREMENT_EXPR:
2351 case PREINCREMENT_EXPR:
2352 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2354 type1 = build_reference_type (type1);
2355 break;
2357 return;
2359 /* 7 For every cv-qualified or cv-unqualified object type T, there
2360 exist candidate operator functions of the form
2362 T& operator*(T*);
2364 8 For every function type T, there exist candidate operator functions of
2365 the form
2366 T& operator*(T*); */
2368 case INDIRECT_REF:
2369 if (TYPE_PTR_P (type1)
2370 && (TYPE_PTROB_P (type1)
2371 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2372 break;
2373 return;
2375 /* 9 For every type T, there exist candidate operator functions of the form
2376 T* operator+(T*);
2378 10For every promoted arithmetic type T, there exist candidate operator
2379 functions of the form
2380 T operator+(T);
2381 T operator-(T); */
2383 case UNARY_PLUS_EXPR: /* unary + */
2384 if (TYPE_PTR_P (type1))
2385 break;
2386 case NEGATE_EXPR:
2387 if (ARITHMETIC_TYPE_P (type1))
2388 break;
2389 return;
2391 /* 11For every promoted integral type T, there exist candidate operator
2392 functions of the form
2393 T operator~(T); */
2395 case BIT_NOT_EXPR:
2396 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2397 break;
2398 return;
2400 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2401 is the same type as C2 or is a derived class of C2, T is a complete
2402 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2403 there exist candidate operator functions of the form
2404 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2405 where CV12 is the union of CV1 and CV2. */
2407 case MEMBER_REF:
2408 if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2410 tree c1 = TREE_TYPE (type1);
2411 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2413 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2414 && (TYPE_PTRMEMFUNC_P (type2)
2415 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2416 break;
2418 return;
2420 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2421 didate operator functions of the form
2422 LR operator*(L, R);
2423 LR operator/(L, R);
2424 LR operator+(L, R);
2425 LR operator-(L, R);
2426 bool operator<(L, R);
2427 bool operator>(L, R);
2428 bool operator<=(L, R);
2429 bool operator>=(L, R);
2430 bool operator==(L, R);
2431 bool operator!=(L, R);
2432 where LR is the result of the usual arithmetic conversions between
2433 types L and R.
2435 14For every pair of types T and I, where T is a cv-qualified or cv-
2436 unqualified complete object type and I is a promoted integral type,
2437 there exist candidate operator functions of the form
2438 T* operator+(T*, I);
2439 T& operator[](T*, I);
2440 T* operator-(T*, I);
2441 T* operator+(I, T*);
2442 T& operator[](I, T*);
2444 15For every T, where T is a pointer to complete object type, there exist
2445 candidate operator functions of the form112)
2446 ptrdiff_t operator-(T, T);
2448 16For every pointer or enumeration type T, there exist candidate operator
2449 functions of the form
2450 bool operator<(T, T);
2451 bool operator>(T, T);
2452 bool operator<=(T, T);
2453 bool operator>=(T, T);
2454 bool operator==(T, T);
2455 bool operator!=(T, T);
2457 17For every pointer to member type T, there exist candidate operator
2458 functions of the form
2459 bool operator==(T, T);
2460 bool operator!=(T, T); */
2462 case MINUS_EXPR:
2463 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2464 break;
2465 if (TYPE_PTROB_P (type1)
2466 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2468 type2 = ptrdiff_type_node;
2469 break;
2471 case MULT_EXPR:
2472 case TRUNC_DIV_EXPR:
2473 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2474 break;
2475 return;
2477 case EQ_EXPR:
2478 case NE_EXPR:
2479 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2480 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
2481 break;
2482 if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
2484 type2 = type1;
2485 break;
2487 if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
2489 type1 = type2;
2490 break;
2492 /* Fall through. */
2493 case LT_EXPR:
2494 case GT_EXPR:
2495 case LE_EXPR:
2496 case GE_EXPR:
2497 case MAX_EXPR:
2498 case MIN_EXPR:
2499 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2500 break;
2501 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2502 break;
2503 if (TREE_CODE (type1) == ENUMERAL_TYPE
2504 && TREE_CODE (type2) == ENUMERAL_TYPE)
2505 break;
2506 if (TYPE_PTR_P (type1)
2507 && null_ptr_cst_p (args[1]))
2509 type2 = type1;
2510 break;
2512 if (null_ptr_cst_p (args[0])
2513 && TYPE_PTR_P (type2))
2515 type1 = type2;
2516 break;
2518 return;
2520 case PLUS_EXPR:
2521 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2522 break;
2523 case ARRAY_REF:
2524 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2526 type1 = ptrdiff_type_node;
2527 break;
2529 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2531 type2 = ptrdiff_type_node;
2532 break;
2534 return;
2536 /* 18For every pair of promoted integral types L and R, there exist candi-
2537 date operator functions of the form
2538 LR operator%(L, R);
2539 LR operator&(L, R);
2540 LR operator^(L, R);
2541 LR operator|(L, R);
2542 L operator<<(L, R);
2543 L operator>>(L, R);
2544 where LR is the result of the usual arithmetic conversions between
2545 types L and R. */
2547 case TRUNC_MOD_EXPR:
2548 case BIT_AND_EXPR:
2549 case BIT_IOR_EXPR:
2550 case BIT_XOR_EXPR:
2551 case LSHIFT_EXPR:
2552 case RSHIFT_EXPR:
2553 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2554 break;
2555 return;
2557 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2558 type, VQ is either volatile or empty, and R is a promoted arithmetic
2559 type, there exist candidate operator functions of the form
2560 VQ L& operator=(VQ L&, R);
2561 VQ L& operator*=(VQ L&, R);
2562 VQ L& operator/=(VQ L&, R);
2563 VQ L& operator+=(VQ L&, R);
2564 VQ L& operator-=(VQ L&, R);
2566 20For every pair T, VQ), where T is any type and VQ is either volatile
2567 or empty, there exist candidate operator functions of the form
2568 T*VQ& operator=(T*VQ&, T*);
2570 21For every pair T, VQ), where T is a pointer to member type and VQ is
2571 either volatile or empty, there exist candidate operator functions of
2572 the form
2573 VQ T& operator=(VQ T&, T);
2575 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2576 unqualified complete object type, VQ is either volatile or empty, and
2577 I is a promoted integral type, there exist candidate operator func-
2578 tions of the form
2579 T*VQ& operator+=(T*VQ&, I);
2580 T*VQ& operator-=(T*VQ&, I);
2582 23For every triple L, VQ, R), where L is an integral or enumeration
2583 type, VQ is either volatile or empty, and R is a promoted integral
2584 type, there exist candidate operator functions of the form
2586 VQ L& operator%=(VQ L&, R);
2587 VQ L& operator<<=(VQ L&, R);
2588 VQ L& operator>>=(VQ L&, R);
2589 VQ L& operator&=(VQ L&, R);
2590 VQ L& operator^=(VQ L&, R);
2591 VQ L& operator|=(VQ L&, R); */
2593 case MODIFY_EXPR:
2594 switch (code2)
2596 case PLUS_EXPR:
2597 case MINUS_EXPR:
2598 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2600 type2 = ptrdiff_type_node;
2601 break;
2603 case MULT_EXPR:
2604 case TRUNC_DIV_EXPR:
2605 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2606 break;
2607 return;
2609 case TRUNC_MOD_EXPR:
2610 case BIT_AND_EXPR:
2611 case BIT_IOR_EXPR:
2612 case BIT_XOR_EXPR:
2613 case LSHIFT_EXPR:
2614 case RSHIFT_EXPR:
2615 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2616 break;
2617 return;
2619 case NOP_EXPR:
2620 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2621 break;
2622 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2623 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2624 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2625 || ((TYPE_PTRMEMFUNC_P (type1)
2626 || TYPE_PTR_P (type1))
2627 && null_ptr_cst_p (args[1])))
2629 type2 = type1;
2630 break;
2632 return;
2634 default:
2635 gcc_unreachable ();
2637 type1 = build_reference_type (type1);
2638 break;
2640 case COND_EXPR:
2641 /* [over.built]
2643 For every pair of promoted arithmetic types L and R, there
2644 exist candidate operator functions of the form
2646 LR operator?(bool, L, R);
2648 where LR is the result of the usual arithmetic conversions
2649 between types L and R.
2651 For every type T, where T is a pointer or pointer-to-member
2652 type, there exist candidate operator functions of the form T
2653 operator?(bool, T, T); */
2655 if (promoted_arithmetic_type_p (type1)
2656 && promoted_arithmetic_type_p (type2))
2657 /* That's OK. */
2658 break;
2660 /* Otherwise, the types should be pointers. */
2661 if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
2662 return;
2664 /* We don't check that the two types are the same; the logic
2665 below will actually create two candidates; one in which both
2666 parameter types are TYPE1, and one in which both parameter
2667 types are TYPE2. */
2668 break;
2670 case REALPART_EXPR:
2671 case IMAGPART_EXPR:
2672 if (ARITHMETIC_TYPE_P (type1))
2673 break;
2674 return;
2676 default:
2677 gcc_unreachable ();
2680 /* Make sure we don't create builtin candidates with dependent types. */
2681 bool u1 = uses_template_parms (type1);
2682 bool u2 = type2 ? uses_template_parms (type2) : false;
2683 if (u1 || u2)
2685 /* Try to recover if one of the types is non-dependent. But if
2686 there's only one type, there's nothing we can do. */
2687 if (!type2)
2688 return;
2689 /* And we lose if both are dependent. */
2690 if (u1 && u2)
2691 return;
2692 /* Or if they have different forms. */
2693 if (TREE_CODE (type1) != TREE_CODE (type2))
2694 return;
2696 if (u1 && !u2)
2697 type1 = type2;
2698 else if (u2 && !u1)
2699 type2 = type1;
2702 /* If we're dealing with two pointer types or two enumeral types,
2703 we need candidates for both of them. */
2704 if (type2 && !same_type_p (type1, type2)
2705 && TREE_CODE (type1) == TREE_CODE (type2)
2706 && (TREE_CODE (type1) == REFERENCE_TYPE
2707 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2708 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2709 || TYPE_PTRMEMFUNC_P (type1)
2710 || MAYBE_CLASS_TYPE_P (type1)
2711 || TREE_CODE (type1) == ENUMERAL_TYPE))
2713 if (TYPE_PTR_OR_PTRMEM_P (type1))
2715 tree cptype = composite_pointer_type (type1, type2,
2716 error_mark_node,
2717 error_mark_node,
2718 CPO_CONVERSION,
2719 tf_none);
2720 if (cptype != error_mark_node)
2722 build_builtin_candidate
2723 (candidates, fnname, cptype, cptype, args, argtypes,
2724 flags, complain);
2725 return;
2729 build_builtin_candidate
2730 (candidates, fnname, type1, type1, args, argtypes, flags, complain);
2731 build_builtin_candidate
2732 (candidates, fnname, type2, type2, args, argtypes, flags, complain);
2733 return;
2736 build_builtin_candidate
2737 (candidates, fnname, type1, type2, args, argtypes, flags, complain);
2740 tree
2741 type_decays_to (tree type)
2743 if (TREE_CODE (type) == ARRAY_TYPE)
2744 return build_pointer_type (TREE_TYPE (type));
2745 if (TREE_CODE (type) == FUNCTION_TYPE)
2746 return build_pointer_type (type);
2747 return type;
2750 /* There are three conditions of builtin candidates:
2752 1) bool-taking candidates. These are the same regardless of the input.
2753 2) pointer-pair taking candidates. These are generated for each type
2754 one of the input types converts to.
2755 3) arithmetic candidates. According to the standard, we should generate
2756 all of these, but I'm trying not to...
2758 Here we generate a superset of the possible candidates for this particular
2759 case. That is a subset of the full set the standard defines, plus some
2760 other cases which the standard disallows. add_builtin_candidate will
2761 filter out the invalid set. */
2763 static void
2764 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2765 enum tree_code code2, tree fnname, tree *args,
2766 int flags, tsubst_flags_t complain)
2768 int ref1, i;
2769 int enum_p = 0;
2770 tree type, argtypes[3], t;
2771 /* TYPES[i] is the set of possible builtin-operator parameter types
2772 we will consider for the Ith argument. */
2773 vec<tree, va_gc> *types[2];
2774 unsigned ix;
2776 for (i = 0; i < 3; ++i)
2778 if (args[i])
2779 argtypes[i] = unlowered_expr_type (args[i]);
2780 else
2781 argtypes[i] = NULL_TREE;
2784 switch (code)
2786 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2787 and VQ is either volatile or empty, there exist candidate operator
2788 functions of the form
2789 VQ T& operator++(VQ T&); */
2791 case POSTINCREMENT_EXPR:
2792 case PREINCREMENT_EXPR:
2793 case POSTDECREMENT_EXPR:
2794 case PREDECREMENT_EXPR:
2795 case MODIFY_EXPR:
2796 ref1 = 1;
2797 break;
2799 /* 24There also exist candidate operator functions of the form
2800 bool operator!(bool);
2801 bool operator&&(bool, bool);
2802 bool operator||(bool, bool); */
2804 case TRUTH_NOT_EXPR:
2805 build_builtin_candidate
2806 (candidates, fnname, boolean_type_node,
2807 NULL_TREE, args, argtypes, flags, complain);
2808 return;
2810 case TRUTH_ORIF_EXPR:
2811 case TRUTH_ANDIF_EXPR:
2812 build_builtin_candidate
2813 (candidates, fnname, boolean_type_node,
2814 boolean_type_node, args, argtypes, flags, complain);
2815 return;
2817 case ADDR_EXPR:
2818 case COMPOUND_EXPR:
2819 case COMPONENT_REF:
2820 return;
2822 case COND_EXPR:
2823 case EQ_EXPR:
2824 case NE_EXPR:
2825 case LT_EXPR:
2826 case LE_EXPR:
2827 case GT_EXPR:
2828 case GE_EXPR:
2829 enum_p = 1;
2830 /* Fall through. */
2832 default:
2833 ref1 = 0;
2836 types[0] = make_tree_vector ();
2837 types[1] = make_tree_vector ();
2839 for (i = 0; i < 2; ++i)
2841 if (! args[i])
2843 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
2845 tree convs;
2847 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2848 return;
2850 convs = lookup_conversions (argtypes[i]);
2852 if (code == COND_EXPR)
2854 if (real_lvalue_p (args[i]))
2855 vec_safe_push (types[i], build_reference_type (argtypes[i]));
2857 vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
2860 else if (! convs)
2861 return;
2863 for (; convs; convs = TREE_CHAIN (convs))
2865 type = TREE_TYPE (convs);
2867 if (i == 0 && ref1
2868 && (TREE_CODE (type) != REFERENCE_TYPE
2869 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2870 continue;
2872 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2873 vec_safe_push (types[i], type);
2875 type = non_reference (type);
2876 if (i != 0 || ! ref1)
2878 type = cv_unqualified (type_decays_to (type));
2879 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2880 vec_safe_push (types[i], type);
2881 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2882 type = type_promotes_to (type);
2885 if (! vec_member (type, types[i]))
2886 vec_safe_push (types[i], type);
2889 else
2891 if (code == COND_EXPR && real_lvalue_p (args[i]))
2892 vec_safe_push (types[i], build_reference_type (argtypes[i]));
2893 type = non_reference (argtypes[i]);
2894 if (i != 0 || ! ref1)
2896 type = cv_unqualified (type_decays_to (type));
2897 if (enum_p && UNSCOPED_ENUM_P (type))
2898 vec_safe_push (types[i], type);
2899 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2900 type = type_promotes_to (type);
2902 vec_safe_push (types[i], type);
2906 /* Run through the possible parameter types of both arguments,
2907 creating candidates with those parameter types. */
2908 FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
2910 unsigned jx;
2911 tree u;
2913 if (!types[1]->is_empty ())
2914 FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
2915 add_builtin_candidate
2916 (candidates, code, code2, fnname, t,
2917 u, args, argtypes, flags, complain);
2918 else
2919 add_builtin_candidate
2920 (candidates, code, code2, fnname, t,
2921 NULL_TREE, args, argtypes, flags, complain);
2924 release_tree_vector (types[0]);
2925 release_tree_vector (types[1]);
2929 /* If TMPL can be successfully instantiated as indicated by
2930 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2932 TMPL is the template. EXPLICIT_TARGS are any explicit template
2933 arguments. ARGLIST is the arguments provided at the call-site.
2934 This does not change ARGLIST. The RETURN_TYPE is the desired type
2935 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
2936 as for add_function_candidate. If an OBJ is supplied, FLAGS and
2937 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
2939 static struct z_candidate*
2940 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2941 tree ctype, tree explicit_targs, tree first_arg,
2942 const vec<tree, va_gc> *arglist, tree return_type,
2943 tree access_path, tree conversion_path,
2944 int flags, tree obj, unification_kind_t strict,
2945 tsubst_flags_t complain)
2947 int ntparms = DECL_NTPARMS (tmpl);
2948 tree targs = make_tree_vec (ntparms);
2949 unsigned int len = vec_safe_length (arglist);
2950 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
2951 unsigned int skip_without_in_chrg = 0;
2952 tree first_arg_without_in_chrg = first_arg;
2953 tree *args_without_in_chrg;
2954 unsigned int nargs_without_in_chrg;
2955 unsigned int ia, ix;
2956 tree arg;
2957 struct z_candidate *cand;
2958 tree fn;
2959 struct rejection_reason *reason = NULL;
2960 int errs;
2962 /* We don't do deduction on the in-charge parameter, the VTT
2963 parameter or 'this'. */
2964 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2966 if (first_arg_without_in_chrg != NULL_TREE)
2967 first_arg_without_in_chrg = NULL_TREE;
2968 else
2969 ++skip_without_in_chrg;
2972 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2973 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2974 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
2976 if (first_arg_without_in_chrg != NULL_TREE)
2977 first_arg_without_in_chrg = NULL_TREE;
2978 else
2979 ++skip_without_in_chrg;
2982 if (len < skip_without_in_chrg)
2983 return NULL;
2985 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
2986 + (len - skip_without_in_chrg));
2987 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
2988 ia = 0;
2989 if (first_arg_without_in_chrg != NULL_TREE)
2991 args_without_in_chrg[ia] = first_arg_without_in_chrg;
2992 ++ia;
2994 for (ix = skip_without_in_chrg;
2995 vec_safe_iterate (arglist, ix, &arg);
2996 ++ix)
2998 args_without_in_chrg[ia] = arg;
2999 ++ia;
3001 gcc_assert (ia == nargs_without_in_chrg);
3003 errs = errorcount+sorrycount;
3004 fn = fn_type_unification (tmpl, explicit_targs, targs,
3005 args_without_in_chrg,
3006 nargs_without_in_chrg,
3007 return_type, strict, flags, false,
3008 complain & tf_decltype);
3010 if (fn == error_mark_node)
3012 /* Don't repeat unification later if it already resulted in errors. */
3013 if (errorcount+sorrycount == errs)
3014 reason = template_unification_rejection (tmpl, explicit_targs,
3015 targs, args_without_in_chrg,
3016 nargs_without_in_chrg,
3017 return_type, strict, flags);
3018 else
3019 reason = template_unification_error_rejection ();
3020 goto fail;
3023 /* In [class.copy]:
3025 A member function template is never instantiated to perform the
3026 copy of a class object to an object of its class type.
3028 It's a little unclear what this means; the standard explicitly
3029 does allow a template to be used to copy a class. For example,
3032 struct A {
3033 A(A&);
3034 template <class T> A(const T&);
3036 const A f ();
3037 void g () { A a (f ()); }
3039 the member template will be used to make the copy. The section
3040 quoted above appears in the paragraph that forbids constructors
3041 whose only parameter is (a possibly cv-qualified variant of) the
3042 class type, and a logical interpretation is that the intent was
3043 to forbid the instantiation of member templates which would then
3044 have that form. */
3045 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3047 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3048 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3049 ctype))
3051 reason = invalid_copy_with_fn_template_rejection ();
3052 goto fail;
3056 if (obj != NULL_TREE)
3057 /* Aha, this is a conversion function. */
3058 cand = add_conv_candidate (candidates, fn, obj, first_arg, arglist,
3059 access_path, conversion_path, complain);
3060 else
3061 cand = add_function_candidate (candidates, fn, ctype,
3062 first_arg, arglist, access_path,
3063 conversion_path, flags, complain);
3064 if (DECL_TI_TEMPLATE (fn) != tmpl)
3065 /* This situation can occur if a member template of a template
3066 class is specialized. Then, instantiate_template might return
3067 an instantiation of the specialization, in which case the
3068 DECL_TI_TEMPLATE field will point at the original
3069 specialization. For example:
3071 template <class T> struct S { template <class U> void f(U);
3072 template <> void f(int) {}; };
3073 S<double> sd;
3074 sd.f(3);
3076 Here, TMPL will be template <class U> S<double>::f(U).
3077 And, instantiate template will give us the specialization
3078 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3079 for this will point at template <class T> template <> S<T>::f(int),
3080 so that we can find the definition. For the purposes of
3081 overload resolution, however, we want the original TMPL. */
3082 cand->template_decl = build_template_info (tmpl, targs);
3083 else
3084 cand->template_decl = DECL_TEMPLATE_INFO (fn);
3085 cand->explicit_targs = explicit_targs;
3087 return cand;
3088 fail:
3089 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
3090 access_path, conversion_path, 0, reason, flags);
3094 static struct z_candidate *
3095 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3096 tree explicit_targs, tree first_arg,
3097 const vec<tree, va_gc> *arglist, tree return_type,
3098 tree access_path, tree conversion_path, int flags,
3099 unification_kind_t strict, tsubst_flags_t complain)
3101 return
3102 add_template_candidate_real (candidates, tmpl, ctype,
3103 explicit_targs, first_arg, arglist,
3104 return_type, access_path, conversion_path,
3105 flags, NULL_TREE, strict, complain);
3109 static struct z_candidate *
3110 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3111 tree obj, tree first_arg,
3112 const vec<tree, va_gc> *arglist,
3113 tree return_type, tree access_path,
3114 tree conversion_path, tsubst_flags_t complain)
3116 return
3117 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3118 first_arg, arglist, return_type, access_path,
3119 conversion_path, 0, obj, DEDUCE_CONV,
3120 complain);
3123 /* The CANDS are the set of candidates that were considered for
3124 overload resolution. Return the set of viable candidates, or CANDS
3125 if none are viable. If any of the candidates were viable, set
3126 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3127 considered viable only if it is strictly viable. */
3129 static struct z_candidate*
3130 splice_viable (struct z_candidate *cands,
3131 bool strict_p,
3132 bool *any_viable_p)
3134 struct z_candidate *viable;
3135 struct z_candidate **last_viable;
3136 struct z_candidate **cand;
3137 bool found_strictly_viable = false;
3139 /* Be strict inside templates, since build_over_call won't actually
3140 do the conversions to get pedwarns. */
3141 if (processing_template_decl)
3142 strict_p = true;
3144 viable = NULL;
3145 last_viable = &viable;
3146 *any_viable_p = false;
3148 cand = &cands;
3149 while (*cand)
3151 struct z_candidate *c = *cand;
3152 if (!strict_p
3153 && (c->viable == 1 || TREE_CODE (c->fn) == TEMPLATE_DECL))
3155 /* Be strict in the presence of a viable candidate. Also if
3156 there are template candidates, so that we get deduction errors
3157 for them instead of silently preferring a bad conversion. */
3158 strict_p = true;
3159 if (viable && !found_strictly_viable)
3161 /* Put any spliced near matches back onto the main list so
3162 that we see them if there is no strict match. */
3163 *any_viable_p = false;
3164 *last_viable = cands;
3165 cands = viable;
3166 viable = NULL;
3167 last_viable = &viable;
3171 if (strict_p ? c->viable == 1 : c->viable)
3173 *last_viable = c;
3174 *cand = c->next;
3175 c->next = NULL;
3176 last_viable = &c->next;
3177 *any_viable_p = true;
3178 if (c->viable == 1)
3179 found_strictly_viable = true;
3181 else
3182 cand = &c->next;
3185 return viable ? viable : cands;
3188 static bool
3189 any_strictly_viable (struct z_candidate *cands)
3191 for (; cands; cands = cands->next)
3192 if (cands->viable == 1)
3193 return true;
3194 return false;
3197 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3198 words, it is about to become the "this" pointer for a member
3199 function call. Take the address of the object. */
3201 static tree
3202 build_this (tree obj)
3204 /* In a template, we are only concerned about the type of the
3205 expression, so we can take a shortcut. */
3206 if (processing_template_decl)
3207 return build_address (obj);
3209 return cp_build_addr_expr (obj, tf_warning_or_error);
3212 /* Returns true iff functions are equivalent. Equivalent functions are
3213 not '==' only if one is a function-local extern function or if
3214 both are extern "C". */
3216 static inline int
3217 equal_functions (tree fn1, tree fn2)
3219 if (TREE_CODE (fn1) != TREE_CODE (fn2))
3220 return 0;
3221 if (TREE_CODE (fn1) == TEMPLATE_DECL)
3222 return fn1 == fn2;
3223 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
3224 || DECL_EXTERN_C_FUNCTION_P (fn1))
3225 return decls_match (fn1, fn2);
3226 return fn1 == fn2;
3229 /* Print information about a candidate being rejected due to INFO. */
3231 static void
3232 print_conversion_rejection (location_t loc, struct conversion_info *info)
3234 tree from = info->from;
3235 if (!TYPE_P (from))
3236 from = lvalue_type (from);
3237 if (info->n_arg == -1)
3239 /* Conversion of implicit `this' argument failed. */
3240 if (!TYPE_P (info->from))
3241 /* A bad conversion for 'this' must be discarding cv-quals. */
3242 inform (loc, " passing %qT as %<this%> "
3243 "argument discards qualifiers",
3244 from);
3245 else
3246 inform (loc, " no known conversion for implicit "
3247 "%<this%> parameter from %qT to %qT",
3248 from, info->to_type);
3250 else if (!TYPE_P (info->from))
3252 if (info->n_arg >= 0)
3253 inform (loc, " conversion of argument %d would be ill-formed:",
3254 info->n_arg + 1);
3255 perform_implicit_conversion (info->to_type, info->from,
3256 tf_warning_or_error);
3258 else if (info->n_arg == -2)
3259 /* Conversion of conversion function return value failed. */
3260 inform (loc, " no known conversion from %qT to %qT",
3261 from, info->to_type);
3262 else
3263 inform (loc, " no known conversion for argument %d from %qT to %qT",
3264 info->n_arg + 1, from, info->to_type);
3267 /* Print information about a candidate with WANT parameters and we found
3268 HAVE. */
3270 static void
3271 print_arity_information (location_t loc, unsigned int have, unsigned int want)
3273 inform_n (loc, want,
3274 " candidate expects %d argument, %d provided",
3275 " candidate expects %d arguments, %d provided",
3276 want, have);
3279 /* Print information about one overload candidate CANDIDATE. MSGSTR
3280 is the text to print before the candidate itself.
3282 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3283 to have been run through gettext by the caller. This wart makes
3284 life simpler in print_z_candidates and for the translators. */
3286 static void
3287 print_z_candidate (location_t loc, const char *msgstr,
3288 struct z_candidate *candidate)
3290 const char *msg = (msgstr == NULL
3291 ? ""
3292 : ACONCAT ((msgstr, " ", NULL)));
3293 location_t cloc = location_of (candidate->fn);
3295 if (identifier_p (candidate->fn))
3297 cloc = loc;
3298 if (candidate->num_convs == 3)
3299 inform (cloc, "%s%D(%T, %T, %T) <built-in>", msg, candidate->fn,
3300 candidate->convs[0]->type,
3301 candidate->convs[1]->type,
3302 candidate->convs[2]->type);
3303 else if (candidate->num_convs == 2)
3304 inform (cloc, "%s%D(%T, %T) <built-in>", msg, candidate->fn,
3305 candidate->convs[0]->type,
3306 candidate->convs[1]->type);
3307 else
3308 inform (cloc, "%s%D(%T) <built-in>", msg, candidate->fn,
3309 candidate->convs[0]->type);
3311 else if (TYPE_P (candidate->fn))
3312 inform (cloc, "%s%T <conversion>", msg, candidate->fn);
3313 else if (candidate->viable == -1)
3314 inform (cloc, "%s%#D <near match>", msg, candidate->fn);
3315 else if (DECL_DELETED_FN (candidate->fn))
3316 inform (cloc, "%s%#D <deleted>", msg, candidate->fn);
3317 else
3318 inform (cloc, "%s%#D", msg, candidate->fn);
3319 /* Give the user some information about why this candidate failed. */
3320 if (candidate->reason != NULL)
3322 struct rejection_reason *r = candidate->reason;
3324 switch (r->code)
3326 case rr_arity:
3327 print_arity_information (cloc, r->u.arity.actual,
3328 r->u.arity.expected);
3329 break;
3330 case rr_arg_conversion:
3331 print_conversion_rejection (cloc, &r->u.conversion);
3332 break;
3333 case rr_bad_arg_conversion:
3334 print_conversion_rejection (cloc, &r->u.bad_conversion);
3335 break;
3336 case rr_explicit_conversion:
3337 inform (cloc, " return type %qT of explicit conversion function "
3338 "cannot be converted to %qT with a qualification "
3339 "conversion", r->u.conversion.from,
3340 r->u.conversion.to_type);
3341 break;
3342 case rr_template_conversion:
3343 inform (cloc, " conversion from return type %qT of template "
3344 "conversion function specialization to %qT is not an "
3345 "exact match", r->u.conversion.from,
3346 r->u.conversion.to_type);
3347 break;
3348 case rr_template_unification:
3349 /* We use template_unification_error_rejection if unification caused
3350 actual non-SFINAE errors, in which case we don't need to repeat
3351 them here. */
3352 if (r->u.template_unification.tmpl == NULL_TREE)
3354 inform (cloc, " substitution of deduced template arguments "
3355 "resulted in errors seen above");
3356 break;
3358 /* Re-run template unification with diagnostics. */
3359 inform (cloc, " template argument deduction/substitution failed:");
3360 fn_type_unification (r->u.template_unification.tmpl,
3361 r->u.template_unification.explicit_targs,
3362 (make_tree_vec
3363 (r->u.template_unification.num_targs)),
3364 r->u.template_unification.args,
3365 r->u.template_unification.nargs,
3366 r->u.template_unification.return_type,
3367 r->u.template_unification.strict,
3368 r->u.template_unification.flags,
3369 true, false);
3370 break;
3371 case rr_invalid_copy:
3372 inform (cloc,
3373 " a constructor taking a single argument of its own "
3374 "class type is invalid");
3375 break;
3376 case rr_none:
3377 default:
3378 /* This candidate didn't have any issues or we failed to
3379 handle a particular code. Either way... */
3380 gcc_unreachable ();
3385 static void
3386 print_z_candidates (location_t loc, struct z_candidate *candidates)
3388 struct z_candidate *cand1;
3389 struct z_candidate **cand2;
3390 int n_candidates;
3392 if (!candidates)
3393 return;
3395 /* Remove non-viable deleted candidates. */
3396 cand1 = candidates;
3397 for (cand2 = &cand1; *cand2; )
3399 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
3400 && !(*cand2)->viable
3401 && DECL_DELETED_FN ((*cand2)->fn))
3402 *cand2 = (*cand2)->next;
3403 else
3404 cand2 = &(*cand2)->next;
3406 /* ...if there are any non-deleted ones. */
3407 if (cand1)
3408 candidates = cand1;
3410 /* There may be duplicates in the set of candidates. We put off
3411 checking this condition as long as possible, since we have no way
3412 to eliminate duplicates from a set of functions in less than n^2
3413 time. Now we are about to emit an error message, so it is more
3414 permissible to go slowly. */
3415 for (cand1 = candidates; cand1; cand1 = cand1->next)
3417 tree fn = cand1->fn;
3418 /* Skip builtin candidates and conversion functions. */
3419 if (!DECL_P (fn))
3420 continue;
3421 cand2 = &cand1->next;
3422 while (*cand2)
3424 if (DECL_P ((*cand2)->fn)
3425 && equal_functions (fn, (*cand2)->fn))
3426 *cand2 = (*cand2)->next;
3427 else
3428 cand2 = &(*cand2)->next;
3432 for (n_candidates = 0, cand1 = candidates; cand1; cand1 = cand1->next)
3433 n_candidates++;
3435 for (; candidates; candidates = candidates->next)
3436 print_z_candidate (loc, "candidate:", candidates);
3439 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3440 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3441 the result of the conversion function to convert it to the final
3442 desired type. Merge the two sequences into a single sequence,
3443 and return the merged sequence. */
3445 static conversion *
3446 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
3448 conversion **t;
3449 bool bad = user_seq->bad_p;
3451 gcc_assert (user_seq->kind == ck_user);
3453 /* Find the end of the second conversion sequence. */
3454 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
3456 /* The entire sequence is a user-conversion sequence. */
3457 (*t)->user_conv_p = true;
3458 if (bad)
3459 (*t)->bad_p = true;
3462 /* Replace the identity conversion with the user conversion
3463 sequence. */
3464 *t = user_seq;
3466 return std_seq;
3469 /* Handle overload resolution for initializing an object of class type from
3470 an initializer list. First we look for a suitable constructor that
3471 takes a std::initializer_list; if we don't find one, we then look for a
3472 non-list constructor.
3474 Parameters are as for add_candidates, except that the arguments are in
3475 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
3476 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3478 static void
3479 add_list_candidates (tree fns, tree first_arg,
3480 tree init_list, tree totype,
3481 tree explicit_targs, bool template_only,
3482 tree conversion_path, tree access_path,
3483 int flags,
3484 struct z_candidate **candidates,
3485 tsubst_flags_t complain)
3487 vec<tree, va_gc> *args;
3489 gcc_assert (*candidates == NULL);
3491 /* We're looking for a ctor for list-initialization. */
3492 flags |= LOOKUP_LIST_INIT_CTOR;
3493 /* And we don't allow narrowing conversions. We also use this flag to
3494 avoid the copy constructor call for copy-list-initialization. */
3495 flags |= LOOKUP_NO_NARROWING;
3497 /* Always use the default constructor if the list is empty (DR 990). */
3498 if (CONSTRUCTOR_NELTS (init_list) == 0
3499 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
3501 /* If the class has a list ctor, try passing the list as a single
3502 argument first, but only consider list ctors. */
3503 else if (TYPE_HAS_LIST_CTOR (totype))
3505 flags |= LOOKUP_LIST_ONLY;
3506 args = make_tree_vector_single (init_list);
3507 add_candidates (fns, first_arg, args, NULL_TREE,
3508 explicit_targs, template_only, conversion_path,
3509 access_path, flags, candidates, complain);
3510 if (any_strictly_viable (*candidates))
3511 return;
3514 args = ctor_to_vec (init_list);
3516 /* We aren't looking for list-ctors anymore. */
3517 flags &= ~LOOKUP_LIST_ONLY;
3518 /* We allow more user-defined conversions within an init-list. */
3519 flags &= ~LOOKUP_NO_CONVERSION;
3521 add_candidates (fns, first_arg, args, NULL_TREE,
3522 explicit_targs, template_only, conversion_path,
3523 access_path, flags, candidates, complain);
3526 /* Returns the best overload candidate to perform the requested
3527 conversion. This function is used for three the overloading situations
3528 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3529 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3530 per [dcl.init.ref], so we ignore temporary bindings. */
3532 static struct z_candidate *
3533 build_user_type_conversion_1 (tree totype, tree expr, int flags,
3534 tsubst_flags_t complain)
3536 struct z_candidate *candidates, *cand;
3537 tree fromtype;
3538 tree ctors = NULL_TREE;
3539 tree conv_fns = NULL_TREE;
3540 conversion *conv = NULL;
3541 tree first_arg = NULL_TREE;
3542 vec<tree, va_gc> *args = NULL;
3543 bool any_viable_p;
3544 int convflags;
3546 if (!expr)
3547 return NULL;
3549 fromtype = TREE_TYPE (expr);
3551 /* We represent conversion within a hierarchy using RVALUE_CONV and
3552 BASE_CONV, as specified by [over.best.ics]; these become plain
3553 constructor calls, as specified in [dcl.init]. */
3554 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
3555 || !DERIVED_FROM_P (totype, fromtype));
3557 if (MAYBE_CLASS_TYPE_P (totype))
3558 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
3559 creating a garbage BASELINK; constructors can't be inherited. */
3560 ctors = lookup_fnfields_slot (totype, complete_ctor_identifier);
3562 if (MAYBE_CLASS_TYPE_P (fromtype))
3564 tree to_nonref = non_reference (totype);
3565 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
3566 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
3567 && DERIVED_FROM_P (to_nonref, fromtype)))
3569 /* [class.conv.fct] A conversion function is never used to
3570 convert a (possibly cv-qualified) object to the (possibly
3571 cv-qualified) same object type (or a reference to it), to a
3572 (possibly cv-qualified) base class of that type (or a
3573 reference to it)... */
3575 else
3576 conv_fns = lookup_conversions (fromtype);
3579 candidates = 0;
3580 flags |= LOOKUP_NO_CONVERSION;
3581 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3582 flags |= LOOKUP_NO_NARROWING;
3584 /* It's OK to bind a temporary for converting constructor arguments, but
3585 not in converting the return value of a conversion operator. */
3586 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION);
3587 flags &= ~LOOKUP_NO_TEMP_BIND;
3589 if (ctors)
3591 int ctorflags = flags;
3593 first_arg = build_dummy_object (totype);
3595 /* We should never try to call the abstract or base constructor
3596 from here. */
3597 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
3598 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)));
3600 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3602 /* List-initialization. */
3603 add_list_candidates (ctors, first_arg, expr, totype, NULL_TREE,
3604 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
3605 ctorflags, &candidates, complain);
3607 else
3609 args = make_tree_vector_single (expr);
3610 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
3611 TYPE_BINFO (totype), TYPE_BINFO (totype),
3612 ctorflags, &candidates, complain);
3615 for (cand = candidates; cand; cand = cand->next)
3617 cand->second_conv = build_identity_conv (totype, NULL_TREE);
3619 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
3620 set, then this is copy-initialization. In that case, "The
3621 result of the call is then used to direct-initialize the
3622 object that is the destination of the copy-initialization."
3623 [dcl.init]
3625 We represent this in the conversion sequence with an
3626 rvalue conversion, which means a constructor call. */
3627 if (TREE_CODE (totype) != REFERENCE_TYPE
3628 && !(convflags & LOOKUP_NO_TEMP_BIND))
3629 cand->second_conv
3630 = build_conv (ck_rvalue, totype, cand->second_conv);
3634 if (conv_fns)
3635 first_arg = expr;
3637 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
3639 tree conversion_path = TREE_PURPOSE (conv_fns);
3640 struct z_candidate *old_candidates;
3642 /* If we are called to convert to a reference type, we are trying to
3643 find a direct binding, so don't even consider temporaries. If
3644 we don't find a direct binding, the caller will try again to
3645 look for a temporary binding. */
3646 if (TREE_CODE (totype) == REFERENCE_TYPE)
3647 convflags |= LOOKUP_NO_TEMP_BIND;
3649 old_candidates = candidates;
3650 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3651 NULL_TREE, false,
3652 conversion_path, TYPE_BINFO (fromtype),
3653 flags, &candidates, complain);
3655 for (cand = candidates; cand != old_candidates; cand = cand->next)
3657 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
3658 conversion *ics
3659 = implicit_conversion (totype,
3660 rettype,
3662 /*c_cast_p=*/false, convflags,
3663 complain);
3665 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3666 copy-initialization. In that case, "The result of the
3667 call is then used to direct-initialize the object that is
3668 the destination of the copy-initialization." [dcl.init]
3670 We represent this in the conversion sequence with an
3671 rvalue conversion, which means a constructor call. But
3672 don't add a second rvalue conversion if there's already
3673 one there. Which there really shouldn't be, but it's
3674 harmless since we'd add it here anyway. */
3675 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3676 && !(convflags & LOOKUP_NO_TEMP_BIND))
3677 ics = build_conv (ck_rvalue, totype, ics);
3679 cand->second_conv = ics;
3681 if (!ics)
3683 cand->viable = 0;
3684 cand->reason = arg_conversion_rejection (NULL_TREE, -2,
3685 rettype, totype);
3687 else if (DECL_NONCONVERTING_P (cand->fn)
3688 && ics->rank > cr_exact)
3690 /* 13.3.1.5: For direct-initialization, those explicit
3691 conversion functions that are not hidden within S and
3692 yield type T or a type that can be converted to type T
3693 with a qualification conversion (4.4) are also candidate
3694 functions. */
3695 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
3696 I've raised this issue with the committee. --jason 9/2011 */
3697 cand->viable = -1;
3698 cand->reason = explicit_conversion_rejection (rettype, totype);
3700 else if (cand->viable == 1 && ics->bad_p)
3702 cand->viable = -1;
3703 cand->reason
3704 = bad_arg_conversion_rejection (NULL_TREE, -2,
3705 rettype, totype);
3707 else if (primary_template_instantiation_p (cand->fn)
3708 && ics->rank > cr_exact)
3710 /* 13.3.3.1.2: If the user-defined conversion is specified by
3711 a specialization of a conversion function template, the
3712 second standard conversion sequence shall have exact match
3713 rank. */
3714 cand->viable = -1;
3715 cand->reason = template_conversion_rejection (rettype, totype);
3720 candidates = splice_viable (candidates, false, &any_viable_p);
3721 if (!any_viable_p)
3723 if (args)
3724 release_tree_vector (args);
3725 return NULL;
3728 cand = tourney (candidates, complain);
3729 if (cand == 0)
3731 if (complain & tf_error)
3733 error ("conversion from %qT to %qT is ambiguous",
3734 fromtype, totype);
3735 print_z_candidates (location_of (expr), candidates);
3738 cand = candidates; /* any one will do */
3739 cand->second_conv = build_ambiguous_conv (totype, expr);
3740 cand->second_conv->user_conv_p = true;
3741 if (!any_strictly_viable (candidates))
3742 cand->second_conv->bad_p = true;
3743 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3744 ambiguous conversion is no worse than another user-defined
3745 conversion. */
3747 return cand;
3750 tree convtype;
3751 if (!DECL_CONSTRUCTOR_P (cand->fn))
3752 convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
3753 else if (cand->second_conv->kind == ck_rvalue)
3754 /* DR 5: [in the first step of copy-initialization]...if the function
3755 is a constructor, the call initializes a temporary of the
3756 cv-unqualified version of the destination type. */
3757 convtype = cv_unqualified (totype);
3758 else
3759 convtype = totype;
3760 /* Build the user conversion sequence. */
3761 conv = build_conv
3762 (ck_user,
3763 convtype,
3764 build_identity_conv (TREE_TYPE (expr), expr));
3765 conv->cand = cand;
3766 if (cand->viable == -1)
3767 conv->bad_p = true;
3769 /* Remember that this was a list-initialization. */
3770 if (flags & LOOKUP_NO_NARROWING)
3771 conv->check_narrowing = true;
3773 /* Combine it with the second conversion sequence. */
3774 cand->second_conv = merge_conversion_sequences (conv,
3775 cand->second_conv);
3777 return cand;
3780 /* Wrapper for above. */
3782 tree
3783 build_user_type_conversion (tree totype, tree expr, int flags,
3784 tsubst_flags_t complain)
3786 struct z_candidate *cand;
3787 tree ret;
3789 bool subtime = timevar_cond_start (TV_OVERLOAD);
3790 cand = build_user_type_conversion_1 (totype, expr, flags, complain);
3792 if (cand)
3794 if (cand->second_conv->kind == ck_ambig)
3795 ret = error_mark_node;
3796 else
3798 expr = convert_like (cand->second_conv, expr, complain);
3799 ret = convert_from_reference (expr);
3802 else
3803 ret = NULL_TREE;
3805 timevar_cond_stop (TV_OVERLOAD, subtime);
3806 return ret;
3809 /* Subroutine of convert_nontype_argument.
3811 EXPR is an argument for a template non-type parameter of integral or
3812 enumeration type. Do any necessary conversions (that are permitted for
3813 non-type arguments) to convert it to the parameter type.
3815 If conversion is successful, returns the converted expression;
3816 otherwise, returns error_mark_node. */
3818 tree
3819 build_integral_nontype_arg_conv (tree type, tree expr, tsubst_flags_t complain)
3821 conversion *conv;
3822 void *p;
3823 tree t;
3824 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
3826 if (error_operand_p (expr))
3827 return error_mark_node;
3829 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
3831 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3832 p = conversion_obstack_alloc (0);
3834 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
3835 /*c_cast_p=*/false,
3836 LOOKUP_IMPLICIT, complain);
3838 /* for a non-type template-parameter of integral or
3839 enumeration type, integral promotions (4.5) and integral
3840 conversions (4.7) are applied. */
3841 /* It should be sufficient to check the outermost conversion step, since
3842 there are no qualification conversions to integer type. */
3843 if (conv)
3844 switch (conv->kind)
3846 /* A conversion function is OK. If it isn't constexpr, we'll
3847 complain later that the argument isn't constant. */
3848 case ck_user:
3849 /* The lvalue-to-rvalue conversion is OK. */
3850 case ck_rvalue:
3851 case ck_identity:
3852 break;
3854 case ck_std:
3855 t = next_conversion (conv)->type;
3856 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t))
3857 break;
3859 if (complain & tf_error)
3860 error_at (loc, "conversion from %qT to %qT not considered for "
3861 "non-type template argument", t, type);
3862 /* and fall through. */
3864 default:
3865 conv = NULL;
3866 break;
3869 if (conv)
3870 expr = convert_like (conv, expr, complain);
3871 else
3872 expr = error_mark_node;
3874 /* Free all the conversions we allocated. */
3875 obstack_free (&conversion_obstack, p);
3877 return expr;
3880 /* Do any initial processing on the arguments to a function call. */
3882 static vec<tree, va_gc> *
3883 resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
3885 unsigned int ix;
3886 tree arg;
3888 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
3890 if (error_operand_p (arg))
3891 return NULL;
3892 else if (VOID_TYPE_P (TREE_TYPE (arg)))
3894 if (complain & tf_error)
3895 error ("invalid use of void expression");
3896 return NULL;
3898 else if (invalid_nonstatic_memfn_p (arg, complain))
3899 return NULL;
3901 return args;
3904 /* Perform overload resolution on FN, which is called with the ARGS.
3906 Return the candidate function selected by overload resolution, or
3907 NULL if the event that overload resolution failed. In the case
3908 that overload resolution fails, *CANDIDATES will be the set of
3909 candidates considered, and ANY_VIABLE_P will be set to true or
3910 false to indicate whether or not any of the candidates were
3911 viable.
3913 The ARGS should already have gone through RESOLVE_ARGS before this
3914 function is called. */
3916 static struct z_candidate *
3917 perform_overload_resolution (tree fn,
3918 const vec<tree, va_gc> *args,
3919 struct z_candidate **candidates,
3920 bool *any_viable_p, tsubst_flags_t complain)
3922 struct z_candidate *cand;
3923 tree explicit_targs;
3924 int template_only;
3926 bool subtime = timevar_cond_start (TV_OVERLOAD);
3928 explicit_targs = NULL_TREE;
3929 template_only = 0;
3931 *candidates = NULL;
3932 *any_viable_p = true;
3934 /* Check FN. */
3935 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
3936 || TREE_CODE (fn) == TEMPLATE_DECL
3937 || TREE_CODE (fn) == OVERLOAD
3938 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
3940 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3942 explicit_targs = TREE_OPERAND (fn, 1);
3943 fn = TREE_OPERAND (fn, 0);
3944 template_only = 1;
3947 /* Add the various candidate functions. */
3948 add_candidates (fn, NULL_TREE, args, NULL_TREE,
3949 explicit_targs, template_only,
3950 /*conversion_path=*/NULL_TREE,
3951 /*access_path=*/NULL_TREE,
3952 LOOKUP_NORMAL,
3953 candidates, complain);
3955 *candidates = splice_viable (*candidates, false, any_viable_p);
3956 if (*any_viable_p)
3957 cand = tourney (*candidates, complain);
3958 else
3959 cand = NULL;
3961 timevar_cond_stop (TV_OVERLOAD, subtime);
3962 return cand;
3965 /* Print an error message about being unable to build a call to FN with
3966 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
3967 be located; CANDIDATES is a possibly empty list of such
3968 functions. */
3970 static void
3971 print_error_for_call_failure (tree fn, vec<tree, va_gc> *args,
3972 struct z_candidate *candidates)
3974 tree name = DECL_NAME (OVL_CURRENT (fn));
3975 location_t loc = location_of (name);
3977 if (!any_strictly_viable (candidates))
3978 error_at (loc, "no matching function for call to %<%D(%A)%>",
3979 name, build_tree_list_vec (args));
3980 else
3981 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
3982 name, build_tree_list_vec (args));
3983 if (candidates)
3984 print_z_candidates (loc, candidates);
3987 /* Return an expression for a call to FN (a namespace-scope function,
3988 or a static member function) with the ARGS. This may change
3989 ARGS. */
3991 tree
3992 build_new_function_call (tree fn, vec<tree, va_gc> **args, bool koenig_p,
3993 tsubst_flags_t complain)
3995 struct z_candidate *candidates, *cand;
3996 bool any_viable_p;
3997 void *p;
3998 tree result;
4000 if (args != NULL && *args != NULL)
4002 *args = resolve_args (*args, complain);
4003 if (*args == NULL)
4004 return error_mark_node;
4007 if (flag_tm)
4008 tm_malloc_replacement (fn);
4010 /* If this function was found without using argument dependent
4011 lookup, then we want to ignore any undeclared friend
4012 functions. */
4013 if (!koenig_p)
4015 tree orig_fn = fn;
4017 fn = remove_hidden_names (fn);
4018 if (!fn)
4020 if (complain & tf_error)
4021 print_error_for_call_failure (orig_fn, *args, NULL);
4022 return error_mark_node;
4026 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4027 p = conversion_obstack_alloc (0);
4029 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
4030 complain);
4032 if (!cand)
4034 if (complain & tf_error)
4036 if (!any_viable_p && candidates && ! candidates->next
4037 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
4038 return cp_build_function_call_vec (candidates->fn, args, complain);
4039 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4040 fn = TREE_OPERAND (fn, 0);
4041 print_error_for_call_failure (fn, *args, candidates);
4043 result = error_mark_node;
4045 else
4047 int flags = LOOKUP_NORMAL;
4048 /* If fn is template_id_expr, the call has explicit template arguments
4049 (e.g. func<int>(5)), communicate this info to build_over_call
4050 through flags so that later we can use it to decide whether to warn
4051 about peculiar null pointer conversion. */
4052 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4053 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
4054 result = build_over_call (cand, flags, complain);
4057 /* Free all the conversions we allocated. */
4058 obstack_free (&conversion_obstack, p);
4060 return result;
4063 /* Build a call to a global operator new. FNNAME is the name of the
4064 operator (either "operator new" or "operator new[]") and ARGS are
4065 the arguments provided. This may change ARGS. *SIZE points to the
4066 total number of bytes required by the allocation, and is updated if
4067 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
4068 be used. If this function determines that no cookie should be
4069 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
4070 is not NULL_TREE, it is evaluated before calculating the final
4071 array size, and if it fails, the array size is replaced with
4072 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
4073 is non-NULL, it will be set, upon return, to the allocation
4074 function called. */
4076 tree
4077 build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
4078 tree *size, tree *cookie_size, tree size_check,
4079 tree *fn, tsubst_flags_t complain)
4081 tree original_size = *size;
4082 tree fns;
4083 struct z_candidate *candidates;
4084 struct z_candidate *cand;
4085 bool any_viable_p;
4087 if (fn)
4088 *fn = NULL_TREE;
4089 /* Set to (size_t)-1 if the size check fails. */
4090 if (size_check != NULL_TREE)
4092 tree errval = TYPE_MAX_VALUE (sizetype);
4093 if (cxx_dialect >= cxx11 && flag_exceptions)
4094 errval = throw_bad_array_new_length ();
4095 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4096 original_size, errval);
4098 vec_safe_insert (*args, 0, *size);
4099 *args = resolve_args (*args, complain);
4100 if (*args == NULL)
4101 return error_mark_node;
4103 /* Based on:
4105 [expr.new]
4107 If this lookup fails to find the name, or if the allocated type
4108 is not a class type, the allocation function's name is looked
4109 up in the global scope.
4111 we disregard block-scope declarations of "operator new". */
4112 fns = lookup_function_nonclass (fnname, *args, /*block_p=*/false);
4114 /* Figure out what function is being called. */
4115 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
4116 complain);
4118 /* If no suitable function could be found, issue an error message
4119 and give up. */
4120 if (!cand)
4122 if (complain & tf_error)
4123 print_error_for_call_failure (fns, *args, candidates);
4124 return error_mark_node;
4127 /* If a cookie is required, add some extra space. Whether
4128 or not a cookie is required cannot be determined until
4129 after we know which function was called. */
4130 if (*cookie_size)
4132 bool use_cookie = true;
4133 tree arg_types;
4135 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
4136 /* Skip the size_t parameter. */
4137 arg_types = TREE_CHAIN (arg_types);
4138 /* Check the remaining parameters (if any). */
4139 if (arg_types
4140 && TREE_CHAIN (arg_types) == void_list_node
4141 && same_type_p (TREE_VALUE (arg_types),
4142 ptr_type_node))
4143 use_cookie = false;
4144 /* If we need a cookie, adjust the number of bytes allocated. */
4145 if (use_cookie)
4147 /* Update the total size. */
4148 *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
4149 /* Set to (size_t)-1 if the size check fails. */
4150 gcc_assert (size_check != NULL_TREE);
4151 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4152 *size, TYPE_MAX_VALUE (sizetype));
4153 /* Update the argument list to reflect the adjusted size. */
4154 (**args)[0] = *size;
4156 else
4157 *cookie_size = NULL_TREE;
4160 /* Tell our caller which function we decided to call. */
4161 if (fn)
4162 *fn = cand->fn;
4164 /* Build the CALL_EXPR. */
4165 return build_over_call (cand, LOOKUP_NORMAL, complain);
4168 /* Build a new call to operator(). This may change ARGS. */
4170 static tree
4171 build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4173 struct z_candidate *candidates = 0, *cand;
4174 tree fns, convs, first_mem_arg = NULL_TREE;
4175 tree type = TREE_TYPE (obj);
4176 bool any_viable_p;
4177 tree result = NULL_TREE;
4178 void *p;
4180 if (error_operand_p (obj))
4181 return error_mark_node;
4183 obj = prep_operand (obj);
4185 if (TYPE_PTRMEMFUNC_P (type))
4187 if (complain & tf_error)
4188 /* It's no good looking for an overloaded operator() on a
4189 pointer-to-member-function. */
4190 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
4191 return error_mark_node;
4194 if (TYPE_BINFO (type))
4196 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
4197 if (fns == error_mark_node)
4198 return error_mark_node;
4200 else
4201 fns = NULL_TREE;
4203 if (args != NULL && *args != NULL)
4205 *args = resolve_args (*args, complain);
4206 if (*args == NULL)
4207 return error_mark_node;
4210 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4211 p = conversion_obstack_alloc (0);
4213 if (fns)
4215 first_mem_arg = obj;
4217 add_candidates (BASELINK_FUNCTIONS (fns),
4218 first_mem_arg, *args, NULL_TREE,
4219 NULL_TREE, false,
4220 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
4221 LOOKUP_NORMAL, &candidates, complain);
4224 convs = lookup_conversions (type);
4226 for (; convs; convs = TREE_CHAIN (convs))
4228 tree fns = TREE_VALUE (convs);
4229 tree totype = TREE_TYPE (convs);
4231 if (TYPE_PTRFN_P (totype)
4232 || TYPE_REFFN_P (totype)
4233 || (TREE_CODE (totype) == REFERENCE_TYPE
4234 && TYPE_PTRFN_P (TREE_TYPE (totype))))
4235 for (; fns; fns = OVL_NEXT (fns))
4237 tree fn = OVL_CURRENT (fns);
4239 if (DECL_NONCONVERTING_P (fn))
4240 continue;
4242 if (TREE_CODE (fn) == TEMPLATE_DECL)
4243 add_template_conv_candidate
4244 (&candidates, fn, obj, NULL_TREE, *args, totype,
4245 /*access_path=*/NULL_TREE,
4246 /*conversion_path=*/NULL_TREE, complain);
4247 else
4248 add_conv_candidate (&candidates, fn, obj, NULL_TREE,
4249 *args, /*conversion_path=*/NULL_TREE,
4250 /*access_path=*/NULL_TREE, complain);
4254 /* Be strict here because if we choose a bad conversion candidate, the
4255 errors we get won't mention the call context. */
4256 candidates = splice_viable (candidates, true, &any_viable_p);
4257 if (!any_viable_p)
4259 if (complain & tf_error)
4261 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
4262 build_tree_list_vec (*args));
4263 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4265 result = error_mark_node;
4267 else
4269 cand = tourney (candidates, complain);
4270 if (cand == 0)
4272 if (complain & tf_error)
4274 error ("call of %<(%T) (%A)%> is ambiguous",
4275 TREE_TYPE (obj), build_tree_list_vec (*args));
4276 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4278 result = error_mark_node;
4280 /* Since cand->fn will be a type, not a function, for a conversion
4281 function, we must be careful not to unconditionally look at
4282 DECL_NAME here. */
4283 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
4284 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
4285 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4286 else
4288 obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1,
4289 complain);
4290 obj = convert_from_reference (obj);
4291 result = cp_build_function_call_vec (obj, args, complain);
4295 /* Free all the conversions we allocated. */
4296 obstack_free (&conversion_obstack, p);
4298 return result;
4301 /* Wrapper for above. */
4303 tree
4304 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4306 tree ret;
4307 bool subtime = timevar_cond_start (TV_OVERLOAD);
4308 ret = build_op_call_1 (obj, args, complain);
4309 timevar_cond_stop (TV_OVERLOAD, subtime);
4310 return ret;
4313 /* Called by op_error to prepare format strings suitable for the error
4314 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4315 and a suffix (controlled by NTYPES). */
4317 static const char *
4318 op_error_string (const char *errmsg, int ntypes, bool match)
4320 const char *msg;
4322 const char *msgp = concat (match ? G_("ambiguous overload for ")
4323 : G_("no match for "), errmsg, NULL);
4325 if (ntypes == 3)
4326 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
4327 else if (ntypes == 2)
4328 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
4329 else
4330 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
4332 return msg;
4335 static void
4336 op_error (location_t loc, enum tree_code code, enum tree_code code2,
4337 tree arg1, tree arg2, tree arg3, bool match)
4339 const char *opname;
4341 if (code == MODIFY_EXPR)
4342 opname = assignment_operator_name_info[code2].name;
4343 else
4344 opname = operator_name_info[code].name;
4346 switch (code)
4348 case COND_EXPR:
4349 if (flag_diagnostics_show_caret)
4350 error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
4351 3, match),
4352 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4353 else
4354 error_at (loc, op_error_string (G_("ternary %<operator?:%> "
4355 "in %<%E ? %E : %E%>"), 3, match),
4356 arg1, arg2, arg3,
4357 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4358 break;
4360 case POSTINCREMENT_EXPR:
4361 case POSTDECREMENT_EXPR:
4362 if (flag_diagnostics_show_caret)
4363 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4364 opname, TREE_TYPE (arg1));
4365 else
4366 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4367 1, match),
4368 opname, arg1, opname, TREE_TYPE (arg1));
4369 break;
4371 case ARRAY_REF:
4372 if (flag_diagnostics_show_caret)
4373 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
4374 TREE_TYPE (arg1), TREE_TYPE (arg2));
4375 else
4376 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
4377 2, match),
4378 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
4379 break;
4381 case REALPART_EXPR:
4382 case IMAGPART_EXPR:
4383 if (flag_diagnostics_show_caret)
4384 error_at (loc, op_error_string (G_("%qs"), 1, match),
4385 opname, TREE_TYPE (arg1));
4386 else
4387 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
4388 opname, opname, arg1, TREE_TYPE (arg1));
4389 break;
4391 default:
4392 if (arg2)
4393 if (flag_diagnostics_show_caret)
4394 error_at (loc, op_error_string (G_("%<operator%s%>"), 2, match),
4395 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
4396 else
4397 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
4398 2, match),
4399 opname, arg1, opname, arg2,
4400 TREE_TYPE (arg1), TREE_TYPE (arg2));
4401 else
4402 if (flag_diagnostics_show_caret)
4403 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4404 opname, TREE_TYPE (arg1));
4405 else
4406 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
4407 1, match),
4408 opname, opname, arg1, TREE_TYPE (arg1));
4409 break;
4413 /* Return the implicit conversion sequence that could be used to
4414 convert E1 to E2 in [expr.cond]. */
4416 static conversion *
4417 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
4419 tree t1 = non_reference (TREE_TYPE (e1));
4420 tree t2 = non_reference (TREE_TYPE (e2));
4421 conversion *conv;
4422 bool good_base;
4424 /* [expr.cond]
4426 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
4427 implicitly converted (clause _conv_) to the type "lvalue reference to
4428 T2", subject to the constraint that in the conversion the
4429 reference must bind directly (_dcl.init.ref_) to an lvalue.
4431 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
4432 implicitly converted to the type "rvalue reference to T2", subject to
4433 the constraint that the reference must bind directly. */
4434 if (lvalue_or_rvalue_with_address_p (e2))
4436 tree rtype = cp_build_reference_type (t2, !real_lvalue_p (e2));
4437 conv = implicit_conversion (rtype,
4440 /*c_cast_p=*/false,
4441 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
4442 |LOOKUP_ONLYCONVERTING,
4443 complain);
4444 if (conv && !conv->bad_p)
4445 return conv;
4448 /* If E2 is a prvalue or if neither of the conversions above can be done
4449 and at least one of the operands has (possibly cv-qualified) class
4450 type: */
4451 if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
4452 return NULL;
4454 /* [expr.cond]
4456 If E1 and E2 have class type, and the underlying class types are
4457 the same or one is a base class of the other: E1 can be converted
4458 to match E2 if the class of T2 is the same type as, or a base
4459 class of, the class of T1, and the cv-qualification of T2 is the
4460 same cv-qualification as, or a greater cv-qualification than, the
4461 cv-qualification of T1. If the conversion is applied, E1 is
4462 changed to an rvalue of type T2 that still refers to the original
4463 source class object (or the appropriate subobject thereof). */
4464 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
4465 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
4467 if (good_base && at_least_as_qualified_p (t2, t1))
4469 conv = build_identity_conv (t1, e1);
4470 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
4471 TYPE_MAIN_VARIANT (t2)))
4472 conv = build_conv (ck_base, t2, conv);
4473 else
4474 conv = build_conv (ck_rvalue, t2, conv);
4475 return conv;
4477 else
4478 return NULL;
4480 else
4481 /* [expr.cond]
4483 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
4484 converted to the type that expression E2 would have if E2 were
4485 converted to an rvalue (or the type it has, if E2 is an rvalue). */
4486 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
4487 LOOKUP_IMPLICIT, complain);
4490 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
4491 arguments to the conditional expression. */
4493 static tree
4494 build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
4495 tsubst_flags_t complain)
4497 tree arg2_type;
4498 tree arg3_type;
4499 tree result = NULL_TREE;
4500 tree result_type = NULL_TREE;
4501 bool lvalue_p = true;
4502 struct z_candidate *candidates = 0;
4503 struct z_candidate *cand;
4504 void *p;
4505 tree orig_arg2, orig_arg3;
4507 /* As a G++ extension, the second argument to the conditional can be
4508 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
4509 c'.) If the second operand is omitted, make sure it is
4510 calculated only once. */
4511 if (!arg2)
4513 if (complain & tf_error)
4514 pedwarn (loc, OPT_Wpedantic,
4515 "ISO C++ forbids omitting the middle term of a ?: expression");
4517 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
4518 if (real_lvalue_p (arg1))
4519 arg2 = arg1 = stabilize_reference (arg1);
4520 else
4521 arg2 = arg1 = save_expr (arg1);
4524 /* If something has already gone wrong, just pass that fact up the
4525 tree. */
4526 if (error_operand_p (arg1)
4527 || error_operand_p (arg2)
4528 || error_operand_p (arg3))
4529 return error_mark_node;
4531 orig_arg2 = arg2;
4532 orig_arg3 = arg3;
4534 if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
4536 arg1 = force_rvalue (arg1, complain);
4537 arg2 = force_rvalue (arg2, complain);
4538 arg3 = force_rvalue (arg3, complain);
4540 /* force_rvalue can return error_mark on valid arguments. */
4541 if (error_operand_p (arg1)
4542 || error_operand_p (arg2)
4543 || error_operand_p (arg3))
4544 return error_mark_node;
4546 tree arg1_type = TREE_TYPE (arg1);
4547 arg2_type = TREE_TYPE (arg2);
4548 arg3_type = TREE_TYPE (arg3);
4550 if (TREE_CODE (arg2_type) != VECTOR_TYPE
4551 && TREE_CODE (arg3_type) != VECTOR_TYPE)
4553 /* Rely on the error messages of the scalar version. */
4554 tree scal = build_conditional_expr_1 (loc, integer_one_node,
4555 orig_arg2, orig_arg3, complain);
4556 if (scal == error_mark_node)
4557 return error_mark_node;
4558 tree stype = TREE_TYPE (scal);
4559 tree ctype = TREE_TYPE (arg1_type);
4560 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
4561 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
4563 if (complain & tf_error)
4564 error_at (loc, "inferred scalar type %qT is not an integer or "
4565 "floating point type of the same size as %qT", stype,
4566 COMPARISON_CLASS_P (arg1)
4567 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4568 : ctype);
4569 return error_mark_node;
4572 tree vtype = build_opaque_vector_type (stype,
4573 TYPE_VECTOR_SUBPARTS (arg1_type));
4574 /* We could pass complain & tf_warning to unsafe_conversion_p,
4575 but the warnings (like Wsign-conversion) have already been
4576 given by the scalar build_conditional_expr_1. We still check
4577 unsafe_conversion_p to forbid truncating long long -> float. */
4578 if (unsafe_conversion_p (loc, stype, arg2, false))
4580 if (complain & tf_error)
4581 error_at (loc, "conversion of scalar %qT to vector %qT "
4582 "involves truncation", arg2_type, vtype);
4583 return error_mark_node;
4585 if (unsafe_conversion_p (loc, stype, arg3, false))
4587 if (complain & tf_error)
4588 error_at (loc, "conversion of scalar %qT to vector %qT "
4589 "involves truncation", arg3_type, vtype);
4590 return error_mark_node;
4593 arg2 = cp_convert (stype, arg2, complain);
4594 arg2 = save_expr (arg2);
4595 arg2 = build_vector_from_val (vtype, arg2);
4596 arg2_type = vtype;
4597 arg3 = cp_convert (stype, arg3, complain);
4598 arg3 = save_expr (arg3);
4599 arg3 = build_vector_from_val (vtype, arg3);
4600 arg3_type = vtype;
4603 if ((TREE_CODE (arg2_type) == VECTOR_TYPE)
4604 != (TREE_CODE (arg3_type) == VECTOR_TYPE))
4606 enum stv_conv convert_flag =
4607 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
4608 complain & tf_error);
4610 switch (convert_flag)
4612 case stv_error:
4613 return error_mark_node;
4614 case stv_firstarg:
4616 arg2 = save_expr (arg2);
4617 arg2 = convert (TREE_TYPE (arg3_type), arg2);
4618 arg2 = build_vector_from_val (arg3_type, arg2);
4619 arg2_type = TREE_TYPE (arg2);
4620 break;
4622 case stv_secondarg:
4624 arg3 = save_expr (arg3);
4625 arg3 = convert (TREE_TYPE (arg2_type), arg3);
4626 arg3 = build_vector_from_val (arg2_type, arg3);
4627 arg3_type = TREE_TYPE (arg3);
4628 break;
4630 default:
4631 break;
4635 if (!same_type_p (arg2_type, arg3_type)
4636 || TYPE_VECTOR_SUBPARTS (arg1_type)
4637 != TYPE_VECTOR_SUBPARTS (arg2_type)
4638 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
4640 if (complain & tf_error)
4641 error_at (loc,
4642 "incompatible vector types in conditional expression: "
4643 "%qT, %qT and %qT", TREE_TYPE (arg1),
4644 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
4645 return error_mark_node;
4648 if (!COMPARISON_CLASS_P (arg1))
4649 arg1 = cp_build_binary_op (loc, NE_EXPR, arg1,
4650 build_zero_cst (arg1_type), complain);
4651 return fold_build3 (VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
4654 /* [expr.cond]
4656 The first expression is implicitly converted to bool (clause
4657 _conv_). */
4658 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
4659 LOOKUP_NORMAL);
4660 if (error_operand_p (arg1))
4661 return error_mark_node;
4663 /* [expr.cond]
4665 If either the second or the third operand has type (possibly
4666 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
4667 array-to-pointer (_conv.array_), and function-to-pointer
4668 (_conv.func_) standard conversions are performed on the second
4669 and third operands. */
4670 arg2_type = unlowered_expr_type (arg2);
4671 arg3_type = unlowered_expr_type (arg3);
4672 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
4674 /* Do the conversions. We don't these for `void' type arguments
4675 since it can't have any effect and since decay_conversion
4676 does not handle that case gracefully. */
4677 if (!VOID_TYPE_P (arg2_type))
4678 arg2 = decay_conversion (arg2, complain);
4679 if (!VOID_TYPE_P (arg3_type))
4680 arg3 = decay_conversion (arg3, complain);
4681 arg2_type = TREE_TYPE (arg2);
4682 arg3_type = TREE_TYPE (arg3);
4684 /* [expr.cond]
4686 One of the following shall hold:
4688 --The second or the third operand (but not both) is a
4689 throw-expression (_except.throw_); the result is of the
4690 type of the other and is an rvalue.
4692 --Both the second and the third operands have type void; the
4693 result is of type void and is an rvalue.
4695 We must avoid calling force_rvalue for expressions of type
4696 "void" because it will complain that their value is being
4697 used. */
4698 if (TREE_CODE (arg2) == THROW_EXPR
4699 && TREE_CODE (arg3) != THROW_EXPR)
4701 if (!VOID_TYPE_P (arg3_type))
4703 arg3 = force_rvalue (arg3, complain);
4704 if (arg3 == error_mark_node)
4705 return error_mark_node;
4707 arg3_type = TREE_TYPE (arg3);
4708 result_type = arg3_type;
4710 else if (TREE_CODE (arg2) != THROW_EXPR
4711 && TREE_CODE (arg3) == THROW_EXPR)
4713 if (!VOID_TYPE_P (arg2_type))
4715 arg2 = force_rvalue (arg2, complain);
4716 if (arg2 == error_mark_node)
4717 return error_mark_node;
4719 arg2_type = TREE_TYPE (arg2);
4720 result_type = arg2_type;
4722 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
4723 result_type = void_type_node;
4724 else
4726 if (complain & tf_error)
4728 if (VOID_TYPE_P (arg2_type))
4729 error_at (EXPR_LOC_OR_LOC (arg3, loc),
4730 "second operand to the conditional operator "
4731 "is of type %<void%>, but the third operand is "
4732 "neither a throw-expression nor of type %<void%>");
4733 else
4734 error_at (EXPR_LOC_OR_LOC (arg2, loc),
4735 "third operand to the conditional operator "
4736 "is of type %<void%>, but the second operand is "
4737 "neither a throw-expression nor of type %<void%>");
4739 return error_mark_node;
4742 lvalue_p = false;
4743 goto valid_operands;
4745 /* [expr.cond]
4747 Otherwise, if the second and third operand have different types,
4748 and either has (possibly cv-qualified) class type, or if both are
4749 glvalues of the same value category and the same type except for
4750 cv-qualification, an attempt is made to convert each of those operands
4751 to the type of the other. */
4752 else if (!same_type_p (arg2_type, arg3_type)
4753 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
4754 || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
4755 arg3_type)
4756 && lvalue_or_rvalue_with_address_p (arg2)
4757 && lvalue_or_rvalue_with_address_p (arg3)
4758 && real_lvalue_p (arg2) == real_lvalue_p (arg3))))
4760 conversion *conv2;
4761 conversion *conv3;
4763 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4764 p = conversion_obstack_alloc (0);
4766 conv2 = conditional_conversion (arg2, arg3, complain);
4767 conv3 = conditional_conversion (arg3, arg2, complain);
4769 /* [expr.cond]
4771 If both can be converted, or one can be converted but the
4772 conversion is ambiguous, the program is ill-formed. If
4773 neither can be converted, the operands are left unchanged and
4774 further checking is performed as described below. If exactly
4775 one conversion is possible, that conversion is applied to the
4776 chosen operand and the converted operand is used in place of
4777 the original operand for the remainder of this section. */
4778 if ((conv2 && !conv2->bad_p
4779 && conv3 && !conv3->bad_p)
4780 || (conv2 && conv2->kind == ck_ambig)
4781 || (conv3 && conv3->kind == ck_ambig))
4783 if (complain & tf_error)
4785 error_at (loc, "operands to ?: have different types %qT and %qT",
4786 arg2_type, arg3_type);
4787 if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
4788 inform (loc, " and each type can be converted to the other");
4789 else if (conv2 && conv2->kind == ck_ambig)
4790 convert_like (conv2, arg2, complain);
4791 else
4792 convert_like (conv3, arg3, complain);
4794 result = error_mark_node;
4796 else if (conv2 && !conv2->bad_p)
4798 arg2 = convert_like (conv2, arg2, complain);
4799 arg2 = convert_from_reference (arg2);
4800 arg2_type = TREE_TYPE (arg2);
4801 /* Even if CONV2 is a valid conversion, the result of the
4802 conversion may be invalid. For example, if ARG3 has type
4803 "volatile X", and X does not have a copy constructor
4804 accepting a "volatile X&", then even if ARG2 can be
4805 converted to X, the conversion will fail. */
4806 if (error_operand_p (arg2))
4807 result = error_mark_node;
4809 else if (conv3 && !conv3->bad_p)
4811 arg3 = convert_like (conv3, arg3, complain);
4812 arg3 = convert_from_reference (arg3);
4813 arg3_type = TREE_TYPE (arg3);
4814 if (error_operand_p (arg3))
4815 result = error_mark_node;
4818 /* Free all the conversions we allocated. */
4819 obstack_free (&conversion_obstack, p);
4821 if (result)
4822 return result;
4824 /* If, after the conversion, both operands have class type,
4825 treat the cv-qualification of both operands as if it were the
4826 union of the cv-qualification of the operands.
4828 The standard is not clear about what to do in this
4829 circumstance. For example, if the first operand has type
4830 "const X" and the second operand has a user-defined
4831 conversion to "volatile X", what is the type of the second
4832 operand after this step? Making it be "const X" (matching
4833 the first operand) seems wrong, as that discards the
4834 qualification without actually performing a copy. Leaving it
4835 as "volatile X" seems wrong as that will result in the
4836 conditional expression failing altogether, even though,
4837 according to this step, the one operand could be converted to
4838 the type of the other. */
4839 if (((conv2 && !conv2->bad_p)
4840 || (conv3 && !conv3->bad_p))
4841 && CLASS_TYPE_P (arg2_type)
4842 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
4843 arg2_type = arg3_type =
4844 cp_build_qualified_type (arg2_type,
4845 cp_type_quals (arg2_type)
4846 | cp_type_quals (arg3_type));
4849 /* [expr.cond]
4851 If the second and third operands are glvalues of the same value
4852 category and have the same type, the result is of that type and
4853 value category. */
4854 if (((real_lvalue_p (arg2) && real_lvalue_p (arg3))
4855 || (xvalue_p (arg2) && xvalue_p (arg3)))
4856 && same_type_p (arg2_type, arg3_type))
4858 result_type = arg2_type;
4859 arg2 = mark_lvalue_use (arg2);
4860 arg3 = mark_lvalue_use (arg3);
4861 goto valid_operands;
4864 /* [expr.cond]
4866 Otherwise, the result is an rvalue. If the second and third
4867 operand do not have the same type, and either has (possibly
4868 cv-qualified) class type, overload resolution is used to
4869 determine the conversions (if any) to be applied to the operands
4870 (_over.match.oper_, _over.built_). */
4871 lvalue_p = false;
4872 if (!same_type_p (arg2_type, arg3_type)
4873 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
4875 tree args[3];
4876 conversion *conv;
4877 bool any_viable_p;
4879 /* Rearrange the arguments so that add_builtin_candidate only has
4880 to know about two args. In build_builtin_candidate, the
4881 arguments are unscrambled. */
4882 args[0] = arg2;
4883 args[1] = arg3;
4884 args[2] = arg1;
4885 add_builtin_candidates (&candidates,
4886 COND_EXPR,
4887 NOP_EXPR,
4888 ansi_opname (COND_EXPR),
4889 args,
4890 LOOKUP_NORMAL, complain);
4892 /* [expr.cond]
4894 If the overload resolution fails, the program is
4895 ill-formed. */
4896 candidates = splice_viable (candidates, false, &any_viable_p);
4897 if (!any_viable_p)
4899 if (complain & tf_error)
4900 error_at (loc, "operands to ?: have different types %qT and %qT",
4901 arg2_type, arg3_type);
4902 return error_mark_node;
4904 cand = tourney (candidates, complain);
4905 if (!cand)
4907 if (complain & tf_error)
4909 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
4910 print_z_candidates (loc, candidates);
4912 return error_mark_node;
4915 /* [expr.cond]
4917 Otherwise, the conversions thus determined are applied, and
4918 the converted operands are used in place of the original
4919 operands for the remainder of this section. */
4920 conv = cand->convs[0];
4921 arg1 = convert_like (conv, arg1, complain);
4922 conv = cand->convs[1];
4923 arg2 = convert_like (conv, arg2, complain);
4924 arg2_type = TREE_TYPE (arg2);
4925 conv = cand->convs[2];
4926 arg3 = convert_like (conv, arg3, complain);
4927 arg3_type = TREE_TYPE (arg3);
4930 /* [expr.cond]
4932 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
4933 and function-to-pointer (_conv.func_) standard conversions are
4934 performed on the second and third operands.
4936 We need to force the lvalue-to-rvalue conversion here for class types,
4937 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
4938 that isn't wrapped with a TARGET_EXPR plays havoc with exception
4939 regions. */
4941 arg2 = force_rvalue (arg2, complain);
4942 if (!CLASS_TYPE_P (arg2_type))
4943 arg2_type = TREE_TYPE (arg2);
4945 arg3 = force_rvalue (arg3, complain);
4946 if (!CLASS_TYPE_P (arg3_type))
4947 arg3_type = TREE_TYPE (arg3);
4949 if (arg2 == error_mark_node || arg3 == error_mark_node)
4950 return error_mark_node;
4952 /* [expr.cond]
4954 After those conversions, one of the following shall hold:
4956 --The second and third operands have the same type; the result is of
4957 that type. */
4958 if (same_type_p (arg2_type, arg3_type))
4959 result_type = arg2_type;
4960 /* [expr.cond]
4962 --The second and third operands have arithmetic or enumeration
4963 type; the usual arithmetic conversions are performed to bring
4964 them to a common type, and the result is of that type. */
4965 else if ((ARITHMETIC_TYPE_P (arg2_type)
4966 || UNSCOPED_ENUM_P (arg2_type))
4967 && (ARITHMETIC_TYPE_P (arg3_type)
4968 || UNSCOPED_ENUM_P (arg3_type)))
4970 /* In this case, there is always a common type. */
4971 result_type = type_after_usual_arithmetic_conversions (arg2_type,
4972 arg3_type);
4973 if (complain & tf_warning)
4974 do_warn_double_promotion (result_type, arg2_type, arg3_type,
4975 "implicit conversion from %qT to %qT to "
4976 "match other result of conditional",
4977 loc);
4979 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
4980 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
4982 if (TREE_CODE (orig_arg2) == CONST_DECL
4983 && TREE_CODE (orig_arg3) == CONST_DECL
4984 && DECL_CONTEXT (orig_arg2) == DECL_CONTEXT (orig_arg3))
4985 /* Two enumerators from the same enumeration can have different
4986 types when the enumeration is still being defined. */;
4987 else if (complain & tf_warning)
4988 warning_at (loc, OPT_Wenum_compare, "enumeral mismatch in "
4989 "conditional expression: %qT vs %qT",
4990 arg2_type, arg3_type);
4992 else if (extra_warnings
4993 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
4994 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
4995 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
4996 && !same_type_p (arg2_type,
4997 type_promotes_to (arg3_type)))))
4999 if (complain & tf_warning)
5000 warning_at (loc, 0, "enumeral and non-enumeral type in "
5001 "conditional expression");
5004 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5005 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5007 /* [expr.cond]
5009 --The second and third operands have pointer type, or one has
5010 pointer type and the other is a null pointer constant; pointer
5011 conversions (_conv.ptr_) and qualification conversions
5012 (_conv.qual_) are performed to bring them to their composite
5013 pointer type (_expr.rel_). The result is of the composite
5014 pointer type.
5016 --The second and third operands have pointer to member type, or
5017 one has pointer to member type and the other is a null pointer
5018 constant; pointer to member conversions (_conv.mem_) and
5019 qualification conversions (_conv.qual_) are performed to bring
5020 them to a common type, whose cv-qualification shall match the
5021 cv-qualification of either the second or the third operand.
5022 The result is of the common type. */
5023 else if ((null_ptr_cst_p (arg2)
5024 && TYPE_PTR_OR_PTRMEM_P (arg3_type))
5025 || (null_ptr_cst_p (arg3)
5026 && TYPE_PTR_OR_PTRMEM_P (arg2_type))
5027 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
5028 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
5029 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
5031 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
5032 arg3, CPO_CONDITIONAL_EXPR,
5033 complain);
5034 if (result_type == error_mark_node)
5035 return error_mark_node;
5036 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5037 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5040 if (!result_type)
5042 if (complain & tf_error)
5043 error_at (loc, "operands to ?: have different types %qT and %qT",
5044 arg2_type, arg3_type);
5045 return error_mark_node;
5048 if (arg2 == error_mark_node || arg3 == error_mark_node)
5049 return error_mark_node;
5051 valid_operands:
5052 result = build3 (COND_EXPR, result_type, arg1, arg2, arg3);
5053 if (!cp_unevaluated_operand)
5054 /* Avoid folding within decltype (c++/42013) and noexcept. */
5055 result = fold_if_not_in_template (result);
5057 /* We can't use result_type below, as fold might have returned a
5058 throw_expr. */
5060 if (!lvalue_p)
5062 /* Expand both sides into the same slot, hopefully the target of
5063 the ?: expression. We used to check for TARGET_EXPRs here,
5064 but now we sometimes wrap them in NOP_EXPRs so the test would
5065 fail. */
5066 if (CLASS_TYPE_P (TREE_TYPE (result)))
5067 result = get_target_expr_sfinae (result, complain);
5068 /* If this expression is an rvalue, but might be mistaken for an
5069 lvalue, we must add a NON_LVALUE_EXPR. */
5070 result = rvalue (result);
5072 else
5073 result = force_paren_expr (result);
5075 return result;
5078 /* Wrapper for above. */
5080 tree
5081 build_conditional_expr (location_t loc, tree arg1, tree arg2, tree arg3,
5082 tsubst_flags_t complain)
5084 tree ret;
5085 bool subtime = timevar_cond_start (TV_OVERLOAD);
5086 ret = build_conditional_expr_1 (loc, arg1, arg2, arg3, complain);
5087 timevar_cond_stop (TV_OVERLOAD, subtime);
5088 return ret;
5091 /* OPERAND is an operand to an expression. Perform necessary steps
5092 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
5093 returned. */
5095 static tree
5096 prep_operand (tree operand)
5098 if (operand)
5100 if (CLASS_TYPE_P (TREE_TYPE (operand))
5101 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
5102 /* Make sure the template type is instantiated now. */
5103 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
5106 return operand;
5109 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5110 OVERLOAD) to the CANDIDATES, returning an updated list of
5111 CANDIDATES. The ARGS are the arguments provided to the call;
5112 if FIRST_ARG is non-null it is the implicit object argument,
5113 otherwise the first element of ARGS is used if needed. The
5114 EXPLICIT_TARGS are explicit template arguments provided.
5115 TEMPLATE_ONLY is true if only template functions should be
5116 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5117 add_function_candidate. */
5119 static void
5120 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
5121 tree return_type,
5122 tree explicit_targs, bool template_only,
5123 tree conversion_path, tree access_path,
5124 int flags,
5125 struct z_candidate **candidates,
5126 tsubst_flags_t complain)
5128 tree ctype;
5129 const vec<tree, va_gc> *non_static_args;
5130 bool check_list_ctor;
5131 bool check_converting;
5132 unification_kind_t strict;
5133 tree fn;
5135 if (!fns)
5136 return;
5138 /* Precalculate special handling of constructors and conversion ops. */
5139 fn = OVL_CURRENT (fns);
5140 if (DECL_CONV_FN_P (fn))
5142 check_list_ctor = false;
5143 check_converting = !!(flags & LOOKUP_ONLYCONVERTING);
5144 if (flags & LOOKUP_NO_CONVERSION)
5145 /* We're doing return_type(x). */
5146 strict = DEDUCE_CONV;
5147 else
5148 /* We're doing x.operator return_type(). */
5149 strict = DEDUCE_EXACT;
5150 /* [over.match.funcs] For conversion functions, the function
5151 is considered to be a member of the class of the implicit
5152 object argument for the purpose of defining the type of
5153 the implicit object parameter. */
5154 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
5156 else
5158 if (DECL_CONSTRUCTOR_P (fn))
5160 check_list_ctor = !!(flags & LOOKUP_LIST_ONLY);
5161 /* For list-initialization we consider explicit constructors
5162 and complain if one is chosen. */
5163 check_converting
5164 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
5165 == LOOKUP_ONLYCONVERTING);
5167 else
5169 check_list_ctor = false;
5170 check_converting = false;
5172 strict = DEDUCE_CALL;
5173 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
5176 if (first_arg)
5177 non_static_args = args;
5178 else
5179 /* Delay creating the implicit this parameter until it is needed. */
5180 non_static_args = NULL;
5182 for (; fns; fns = OVL_NEXT (fns))
5184 tree fn_first_arg;
5185 const vec<tree, va_gc> *fn_args;
5187 fn = OVL_CURRENT (fns);
5189 if (check_converting && DECL_NONCONVERTING_P (fn))
5190 continue;
5191 if (check_list_ctor && !is_list_ctor (fn))
5192 continue;
5194 /* Figure out which set of arguments to use. */
5195 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
5197 /* If this function is a non-static member and we didn't get an
5198 implicit object argument, move it out of args. */
5199 if (first_arg == NULL_TREE)
5201 unsigned int ix;
5202 tree arg;
5203 vec<tree, va_gc> *tempvec;
5204 vec_alloc (tempvec, args->length () - 1);
5205 for (ix = 1; args->iterate (ix, &arg); ++ix)
5206 tempvec->quick_push (arg);
5207 non_static_args = tempvec;
5208 first_arg = (*args)[0];
5211 fn_first_arg = first_arg;
5212 fn_args = non_static_args;
5214 else
5216 /* Otherwise, just use the list of arguments provided. */
5217 fn_first_arg = NULL_TREE;
5218 fn_args = args;
5221 if (TREE_CODE (fn) == TEMPLATE_DECL)
5222 add_template_candidate (candidates,
5224 ctype,
5225 explicit_targs,
5226 fn_first_arg,
5227 fn_args,
5228 return_type,
5229 access_path,
5230 conversion_path,
5231 flags,
5232 strict,
5233 complain);
5234 else if (!template_only)
5235 add_function_candidate (candidates,
5237 ctype,
5238 fn_first_arg,
5239 fn_args,
5240 access_path,
5241 conversion_path,
5242 flags,
5243 complain);
5247 static tree
5248 build_new_op_1 (location_t loc, enum tree_code code, int flags, tree arg1,
5249 tree arg2, tree arg3, tree *overload, tsubst_flags_t complain)
5251 struct z_candidate *candidates = 0, *cand;
5252 vec<tree, va_gc> *arglist;
5253 tree fnname;
5254 tree args[3];
5255 tree result = NULL_TREE;
5256 bool result_valid_p = false;
5257 enum tree_code code2 = NOP_EXPR;
5258 enum tree_code code_orig_arg1 = ERROR_MARK;
5259 enum tree_code code_orig_arg2 = ERROR_MARK;
5260 conversion *conv;
5261 void *p;
5262 bool strict_p;
5263 bool any_viable_p;
5265 if (error_operand_p (arg1)
5266 || error_operand_p (arg2)
5267 || error_operand_p (arg3))
5268 return error_mark_node;
5270 if (code == MODIFY_EXPR)
5272 code2 = TREE_CODE (arg3);
5273 arg3 = NULL_TREE;
5274 fnname = ansi_assopname (code2);
5276 else
5277 fnname = ansi_opname (code);
5279 arg1 = prep_operand (arg1);
5281 switch (code)
5283 case NEW_EXPR:
5284 case VEC_NEW_EXPR:
5285 case VEC_DELETE_EXPR:
5286 case DELETE_EXPR:
5287 /* Use build_op_new_call and build_op_delete_call instead. */
5288 gcc_unreachable ();
5290 case CALL_EXPR:
5291 /* Use build_op_call instead. */
5292 gcc_unreachable ();
5294 case TRUTH_ORIF_EXPR:
5295 case TRUTH_ANDIF_EXPR:
5296 case TRUTH_AND_EXPR:
5297 case TRUTH_OR_EXPR:
5298 /* These are saved for the sake of warn_logical_operator. */
5299 code_orig_arg1 = TREE_CODE (arg1);
5300 code_orig_arg2 = TREE_CODE (arg2);
5302 default:
5303 break;
5306 arg2 = prep_operand (arg2);
5307 arg3 = prep_operand (arg3);
5309 if (code == COND_EXPR)
5310 /* Use build_conditional_expr instead. */
5311 gcc_unreachable ();
5312 else if (! OVERLOAD_TYPE_P (TREE_TYPE (arg1))
5313 && (! arg2 || ! OVERLOAD_TYPE_P (TREE_TYPE (arg2))))
5314 goto builtin;
5316 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5317 arg2 = integer_zero_node;
5319 vec_alloc (arglist, 3);
5320 arglist->quick_push (arg1);
5321 if (arg2 != NULL_TREE)
5322 arglist->quick_push (arg2);
5323 if (arg3 != NULL_TREE)
5324 arglist->quick_push (arg3);
5326 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5327 p = conversion_obstack_alloc (0);
5329 /* Add namespace-scope operators to the list of functions to
5330 consider. */
5331 add_candidates (lookup_function_nonclass (fnname, arglist, /*block_p=*/true),
5332 NULL_TREE, arglist, NULL_TREE,
5333 NULL_TREE, false, NULL_TREE, NULL_TREE,
5334 flags, &candidates, complain);
5336 args[0] = arg1;
5337 args[1] = arg2;
5338 args[2] = NULL_TREE;
5340 /* Add class-member operators to the candidate set. */
5341 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
5343 tree fns;
5345 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
5346 if (fns == error_mark_node)
5348 result = error_mark_node;
5349 goto user_defined_result_ready;
5351 if (fns)
5352 add_candidates (BASELINK_FUNCTIONS (fns),
5353 NULL_TREE, arglist, NULL_TREE,
5354 NULL_TREE, false,
5355 BASELINK_BINFO (fns),
5356 BASELINK_ACCESS_BINFO (fns),
5357 flags, &candidates, complain);
5359 /* Per 13.3.1.2/3, 2nd bullet, if no operand has a class type, then
5360 only non-member functions that have type T1 or reference to
5361 cv-qualified-opt T1 for the first argument, if the first argument
5362 has an enumeration type, or T2 or reference to cv-qualified-opt
5363 T2 for the second argument, if the the second argument has an
5364 enumeration type. Filter out those that don't match. */
5365 else if (! arg2 || ! CLASS_TYPE_P (TREE_TYPE (arg2)))
5367 struct z_candidate **candp, **next;
5369 for (candp = &candidates; *candp; candp = next)
5371 tree parmlist, parmtype;
5372 int i, nargs = (arg2 ? 2 : 1);
5374 cand = *candp;
5375 next = &cand->next;
5377 parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5379 for (i = 0; i < nargs; ++i)
5381 parmtype = TREE_VALUE (parmlist);
5383 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
5384 parmtype = TREE_TYPE (parmtype);
5385 if (TREE_CODE (TREE_TYPE (args[i])) == ENUMERAL_TYPE
5386 && (same_type_ignoring_top_level_qualifiers_p
5387 (TREE_TYPE (args[i]), parmtype)))
5388 break;
5390 parmlist = TREE_CHAIN (parmlist);
5393 /* No argument has an appropriate type, so remove this
5394 candidate function from the list. */
5395 if (i == nargs)
5397 *candp = cand->next;
5398 next = candp;
5403 add_builtin_candidates (&candidates, code, code2, fnname, args,
5404 flags, complain);
5406 switch (code)
5408 case COMPOUND_EXPR:
5409 case ADDR_EXPR:
5410 /* For these, the built-in candidates set is empty
5411 [over.match.oper]/3. We don't want non-strict matches
5412 because exact matches are always possible with built-in
5413 operators. The built-in candidate set for COMPONENT_REF
5414 would be empty too, but since there are no such built-in
5415 operators, we accept non-strict matches for them. */
5416 strict_p = true;
5417 break;
5419 default:
5420 strict_p = false;
5421 break;
5424 candidates = splice_viable (candidates, strict_p, &any_viable_p);
5425 if (!any_viable_p)
5427 switch (code)
5429 case POSTINCREMENT_EXPR:
5430 case POSTDECREMENT_EXPR:
5431 /* Don't try anything fancy if we're not allowed to produce
5432 errors. */
5433 if (!(complain & tf_error))
5434 return error_mark_node;
5436 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
5437 distinguish between prefix and postfix ++ and
5438 operator++() was used for both, so we allow this with
5439 -fpermissive. */
5440 else
5442 const char *msg = (flag_permissive)
5443 ? G_("no %<%D(int)%> declared for postfix %qs,"
5444 " trying prefix operator instead")
5445 : G_("no %<%D(int)%> declared for postfix %qs");
5446 permerror (loc, msg, fnname, operator_name_info[code].name);
5449 if (!flag_permissive)
5450 return error_mark_node;
5452 if (code == POSTINCREMENT_EXPR)
5453 code = PREINCREMENT_EXPR;
5454 else
5455 code = PREDECREMENT_EXPR;
5456 result = build_new_op_1 (loc, code, flags, arg1, NULL_TREE,
5457 NULL_TREE, overload, complain);
5458 break;
5460 /* The caller will deal with these. */
5461 case ADDR_EXPR:
5462 case COMPOUND_EXPR:
5463 case COMPONENT_REF:
5464 result = NULL_TREE;
5465 result_valid_p = true;
5466 break;
5468 default:
5469 if (complain & tf_error)
5471 /* If one of the arguments of the operator represents
5472 an invalid use of member function pointer, try to report
5473 a meaningful error ... */
5474 if (invalid_nonstatic_memfn_p (arg1, tf_error)
5475 || invalid_nonstatic_memfn_p (arg2, tf_error)
5476 || invalid_nonstatic_memfn_p (arg3, tf_error))
5477 /* We displayed the error message. */;
5478 else
5480 /* ... Otherwise, report the more generic
5481 "no matching operator found" error */
5482 op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
5483 print_z_candidates (loc, candidates);
5486 result = error_mark_node;
5487 break;
5490 else
5492 cand = tourney (candidates, complain);
5493 if (cand == 0)
5495 if (complain & tf_error)
5497 op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
5498 print_z_candidates (loc, candidates);
5500 result = error_mark_node;
5502 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5504 if (overload)
5505 *overload = cand->fn;
5507 if (resolve_args (arglist, complain) == NULL)
5508 result = error_mark_node;
5509 else
5510 result = build_over_call (cand, LOOKUP_NORMAL, complain);
5512 else
5514 /* Give any warnings we noticed during overload resolution. */
5515 if (cand->warnings && (complain & tf_warning))
5517 struct candidate_warning *w;
5518 for (w = cand->warnings; w; w = w->next)
5519 joust (cand, w->loser, 1, complain);
5522 /* Check for comparison of different enum types. */
5523 switch (code)
5525 case GT_EXPR:
5526 case LT_EXPR:
5527 case GE_EXPR:
5528 case LE_EXPR:
5529 case EQ_EXPR:
5530 case NE_EXPR:
5531 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
5532 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
5533 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5534 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
5535 && (complain & tf_warning))
5537 warning (OPT_Wenum_compare,
5538 "comparison between %q#T and %q#T",
5539 TREE_TYPE (arg1), TREE_TYPE (arg2));
5541 break;
5542 default:
5543 break;
5546 /* We need to strip any leading REF_BIND so that bitfields
5547 don't cause errors. This should not remove any important
5548 conversions, because builtins don't apply to class
5549 objects directly. */
5550 conv = cand->convs[0];
5551 if (conv->kind == ck_ref_bind)
5552 conv = next_conversion (conv);
5553 arg1 = convert_like (conv, arg1, complain);
5555 if (arg2)
5557 conv = cand->convs[1];
5558 if (conv->kind == ck_ref_bind)
5559 conv = next_conversion (conv);
5560 else
5561 arg2 = decay_conversion (arg2, complain);
5563 /* We need to call warn_logical_operator before
5564 converting arg2 to a boolean_type, but after
5565 decaying an enumerator to its value. */
5566 if (complain & tf_warning)
5567 warn_logical_operator (loc, code, boolean_type_node,
5568 code_orig_arg1, arg1,
5569 code_orig_arg2, arg2);
5571 arg2 = convert_like (conv, arg2, complain);
5573 if (arg3)
5575 conv = cand->convs[2];
5576 if (conv->kind == ck_ref_bind)
5577 conv = next_conversion (conv);
5578 arg3 = convert_like (conv, arg3, complain);
5584 user_defined_result_ready:
5586 /* Free all the conversions we allocated. */
5587 obstack_free (&conversion_obstack, p);
5589 if (result || result_valid_p)
5590 return result;
5592 builtin:
5593 switch (code)
5595 case MODIFY_EXPR:
5596 return cp_build_modify_expr (arg1, code2, arg2, complain);
5598 case INDIRECT_REF:
5599 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
5601 case TRUTH_ANDIF_EXPR:
5602 case TRUTH_ORIF_EXPR:
5603 case TRUTH_AND_EXPR:
5604 case TRUTH_OR_EXPR:
5605 warn_logical_operator (loc, code, boolean_type_node,
5606 code_orig_arg1, arg1, code_orig_arg2, arg2);
5607 /* Fall through. */
5608 case PLUS_EXPR:
5609 case MINUS_EXPR:
5610 case MULT_EXPR:
5611 case TRUNC_DIV_EXPR:
5612 case GT_EXPR:
5613 case LT_EXPR:
5614 case GE_EXPR:
5615 case LE_EXPR:
5616 case EQ_EXPR:
5617 case NE_EXPR:
5618 case MAX_EXPR:
5619 case MIN_EXPR:
5620 case LSHIFT_EXPR:
5621 case RSHIFT_EXPR:
5622 case TRUNC_MOD_EXPR:
5623 case BIT_AND_EXPR:
5624 case BIT_IOR_EXPR:
5625 case BIT_XOR_EXPR:
5626 return cp_build_binary_op (loc, code, arg1, arg2, complain);
5628 case UNARY_PLUS_EXPR:
5629 case NEGATE_EXPR:
5630 case BIT_NOT_EXPR:
5631 case TRUTH_NOT_EXPR:
5632 case PREINCREMENT_EXPR:
5633 case POSTINCREMENT_EXPR:
5634 case PREDECREMENT_EXPR:
5635 case POSTDECREMENT_EXPR:
5636 case REALPART_EXPR:
5637 case IMAGPART_EXPR:
5638 case ABS_EXPR:
5639 return cp_build_unary_op (code, arg1, candidates != 0, complain);
5641 case ARRAY_REF:
5642 return cp_build_array_ref (input_location, arg1, arg2, complain);
5644 case MEMBER_REF:
5645 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_ARROW_STAR,
5646 complain),
5647 arg2, complain);
5649 /* The caller will deal with these. */
5650 case ADDR_EXPR:
5651 case COMPONENT_REF:
5652 case COMPOUND_EXPR:
5653 return NULL_TREE;
5655 default:
5656 gcc_unreachable ();
5658 return NULL_TREE;
5661 /* Wrapper for above. */
5663 tree
5664 build_new_op (location_t loc, enum tree_code code, int flags,
5665 tree arg1, tree arg2, tree arg3,
5666 tree *overload, tsubst_flags_t complain)
5668 tree ret;
5669 bool subtime = timevar_cond_start (TV_OVERLOAD);
5670 ret = build_new_op_1 (loc, code, flags, arg1, arg2, arg3,
5671 overload, complain);
5672 timevar_cond_stop (TV_OVERLOAD, subtime);
5673 return ret;
5676 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
5677 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
5679 static bool
5680 non_placement_deallocation_fn_p (tree t)
5682 /* A template instance is never a usual deallocation function,
5683 regardless of its signature. */
5684 if (TREE_CODE (t) == TEMPLATE_DECL
5685 || primary_template_instantiation_p (t))
5686 return false;
5688 /* If a class T has a member deallocation function named operator delete
5689 with exactly one parameter, then that function is a usual
5690 (non-placement) deallocation function. If class T does not declare
5691 such an operator delete but does declare a member deallocation
5692 function named operator delete with exactly two parameters, the second
5693 of which has type std::size_t (18.2), then this function is a usual
5694 deallocation function. */
5695 t = FUNCTION_ARG_CHAIN (t);
5696 if (t == void_list_node
5697 || (t && same_type_p (TREE_VALUE (t), size_type_node)
5698 && TREE_CHAIN (t) == void_list_node))
5699 return true;
5700 return false;
5703 /* Build a call to operator delete. This has to be handled very specially,
5704 because the restrictions on what signatures match are different from all
5705 other call instances. For a normal delete, only a delete taking (void *)
5706 or (void *, size_t) is accepted. For a placement delete, only an exact
5707 match with the placement new is accepted.
5709 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
5710 ADDR is the pointer to be deleted.
5711 SIZE is the size of the memory block to be deleted.
5712 GLOBAL_P is true if the delete-expression should not consider
5713 class-specific delete operators.
5714 PLACEMENT is the corresponding placement new call, or NULL_TREE.
5716 If this call to "operator delete" is being generated as part to
5717 deallocate memory allocated via a new-expression (as per [expr.new]
5718 which requires that if the initialization throws an exception then
5719 we call a deallocation function), then ALLOC_FN is the allocation
5720 function. */
5722 tree
5723 build_op_delete_call (enum tree_code code, tree addr, tree size,
5724 bool global_p, tree placement,
5725 tree alloc_fn, tsubst_flags_t complain)
5727 tree fn = NULL_TREE;
5728 tree fns, fnname, type, t;
5730 if (addr == error_mark_node)
5731 return error_mark_node;
5733 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
5735 fnname = ansi_opname (code);
5737 if (CLASS_TYPE_P (type)
5738 && COMPLETE_TYPE_P (complete_type (type))
5739 && !global_p)
5740 /* In [class.free]
5742 If the result of the lookup is ambiguous or inaccessible, or if
5743 the lookup selects a placement deallocation function, the
5744 program is ill-formed.
5746 Therefore, we ask lookup_fnfields to complain about ambiguity. */
5748 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
5749 if (fns == error_mark_node)
5750 return error_mark_node;
5752 else
5753 fns = NULL_TREE;
5755 if (fns == NULL_TREE)
5756 fns = lookup_name_nonclass (fnname);
5758 /* Strip const and volatile from addr. */
5759 addr = cp_convert (ptr_type_node, addr, complain);
5761 if (placement)
5763 /* "A declaration of a placement deallocation function matches the
5764 declaration of a placement allocation function if it has the same
5765 number of parameters and, after parameter transformations (8.3.5),
5766 all parameter types except the first are identical."
5768 So we build up the function type we want and ask instantiate_type
5769 to get it for us. */
5770 t = FUNCTION_ARG_CHAIN (alloc_fn);
5771 t = tree_cons (NULL_TREE, ptr_type_node, t);
5772 t = build_function_type (void_type_node, t);
5774 fn = instantiate_type (t, fns, tf_none);
5775 if (fn == error_mark_node)
5776 return NULL_TREE;
5778 if (BASELINK_P (fn))
5779 fn = BASELINK_FUNCTIONS (fn);
5781 /* "If the lookup finds the two-parameter form of a usual deallocation
5782 function (3.7.4.2) and that function, considered as a placement
5783 deallocation function, would have been selected as a match for the
5784 allocation function, the program is ill-formed." */
5785 if (non_placement_deallocation_fn_p (fn))
5787 /* But if the class has an operator delete (void *), then that is
5788 the usual deallocation function, so we shouldn't complain
5789 about using the operator delete (void *, size_t). */
5790 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
5791 t; t = OVL_NEXT (t))
5793 tree elt = OVL_CURRENT (t);
5794 if (non_placement_deallocation_fn_p (elt)
5795 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
5796 goto ok;
5798 if (complain & tf_error)
5800 permerror (0, "non-placement deallocation function %q+D", fn);
5801 permerror (input_location, "selected for placement delete");
5803 else
5804 return error_mark_node;
5805 ok:;
5808 else
5809 /* "Any non-placement deallocation function matches a non-placement
5810 allocation function. If the lookup finds a single matching
5811 deallocation function, that function will be called; otherwise, no
5812 deallocation function will be called." */
5813 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
5814 t; t = OVL_NEXT (t))
5816 tree elt = OVL_CURRENT (t);
5817 if (non_placement_deallocation_fn_p (elt))
5819 fn = elt;
5820 /* "If a class T has a member deallocation function named
5821 operator delete with exactly one parameter, then that
5822 function is a usual (non-placement) deallocation
5823 function. If class T does not declare such an operator
5824 delete but does declare a member deallocation function named
5825 operator delete with exactly two parameters, the second of
5826 which has type std::size_t (18.2), then this function is a
5827 usual deallocation function."
5829 So (void*) beats (void*, size_t). */
5830 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
5831 break;
5835 /* If we have a matching function, call it. */
5836 if (fn)
5838 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
5840 /* If the FN is a member function, make sure that it is
5841 accessible. */
5842 if (BASELINK_P (fns))
5843 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
5844 complain);
5846 /* Core issue 901: It's ok to new a type with deleted delete. */
5847 if (DECL_DELETED_FN (fn) && alloc_fn)
5848 return NULL_TREE;
5850 if (placement)
5852 /* The placement args might not be suitable for overload
5853 resolution at this point, so build the call directly. */
5854 int nargs = call_expr_nargs (placement);
5855 tree *argarray = XALLOCAVEC (tree, nargs);
5856 int i;
5857 argarray[0] = addr;
5858 for (i = 1; i < nargs; i++)
5859 argarray[i] = CALL_EXPR_ARG (placement, i);
5860 mark_used (fn);
5861 return build_cxx_call (fn, nargs, argarray, complain);
5863 else
5865 tree ret;
5866 vec<tree, va_gc> *args = make_tree_vector ();
5867 args->quick_push (addr);
5868 if (FUNCTION_ARG_CHAIN (fn) != void_list_node)
5869 args->quick_push (size);
5870 ret = cp_build_function_call_vec (fn, &args, complain);
5871 release_tree_vector (args);
5872 return ret;
5876 /* [expr.new]
5878 If no unambiguous matching deallocation function can be found,
5879 propagating the exception does not cause the object's memory to
5880 be freed. */
5881 if (alloc_fn)
5883 if ((complain & tf_warning)
5884 && !placement)
5885 warning (0, "no corresponding deallocation function for %qD",
5886 alloc_fn);
5887 return NULL_TREE;
5890 if (complain & tf_error)
5891 error ("no suitable %<operator %s%> for %qT",
5892 operator_name_info[(int)code].name, type);
5893 return error_mark_node;
5896 /* If the current scope isn't allowed to access DECL along
5897 BASETYPE_PATH, give an error. The most derived class in
5898 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
5899 the declaration to use in the error diagnostic. */
5901 bool
5902 enforce_access (tree basetype_path, tree decl, tree diag_decl,
5903 tsubst_flags_t complain)
5905 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
5907 if (!accessible_p (basetype_path, decl, true))
5909 if (complain & tf_error)
5911 if (TREE_PRIVATE (decl))
5912 error ("%q+#D is private", diag_decl);
5913 else if (TREE_PROTECTED (decl))
5914 error ("%q+#D is protected", diag_decl);
5915 else
5916 error ("%q+#D is inaccessible", diag_decl);
5917 error ("within this context");
5919 return false;
5922 return true;
5925 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
5926 bitwise or of LOOKUP_* values. If any errors are warnings are
5927 generated, set *DIAGNOSTIC_FN to "error" or "warning",
5928 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
5929 to NULL. */
5931 static tree
5932 build_temp (tree expr, tree type, int flags,
5933 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
5935 int savew, savee;
5936 vec<tree, va_gc> *args;
5938 savew = warningcount + werrorcount, savee = errorcount;
5939 args = make_tree_vector_single (expr);
5940 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
5941 &args, type, flags, complain);
5942 release_tree_vector (args);
5943 if (warningcount + werrorcount > savew)
5944 *diagnostic_kind = DK_WARNING;
5945 else if (errorcount > savee)
5946 *diagnostic_kind = DK_ERROR;
5947 else
5948 *diagnostic_kind = DK_UNSPECIFIED;
5949 return expr;
5952 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
5953 EXPR is implicitly converted to type TOTYPE.
5954 FN and ARGNUM are used for diagnostics. */
5956 static void
5957 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
5959 /* Issue warnings about peculiar, but valid, uses of NULL. */
5960 if (expr == null_node && TREE_CODE (totype) != BOOLEAN_TYPE
5961 && ARITHMETIC_TYPE_P (totype))
5963 source_location loc =
5964 expansion_point_location_if_in_system_header (input_location);
5966 if (fn)
5967 warning_at (loc, OPT_Wconversion_null,
5968 "passing NULL to non-pointer argument %P of %qD",
5969 argnum, fn);
5970 else
5971 warning_at (loc, OPT_Wconversion_null,
5972 "converting to non-pointer type %qT from NULL", totype);
5975 /* Issue warnings if "false" is converted to a NULL pointer */
5976 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
5977 && TYPE_PTR_P (totype))
5979 if (fn)
5980 warning_at (input_location, OPT_Wconversion_null,
5981 "converting %<false%> to pointer type for argument %P "
5982 "of %qD", argnum, fn);
5983 else
5984 warning_at (input_location, OPT_Wconversion_null,
5985 "converting %<false%> to pointer type %qT", totype);
5989 /* We gave a diagnostic during a conversion. If this was in the second
5990 standard conversion sequence of a user-defined conversion sequence, say
5991 which user-defined conversion. */
5993 static void
5994 maybe_print_user_conv_context (conversion *convs)
5996 if (convs->user_conv_p)
5997 for (conversion *t = convs; t; t = next_conversion (t))
5998 if (t->kind == ck_user)
6000 print_z_candidate (0, " after user-defined conversion:",
6001 t->cand);
6002 break;
6006 /* Perform the conversions in CONVS on the expression EXPR. FN and
6007 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
6008 indicates the `this' argument of a method. INNER is nonzero when
6009 being called to continue a conversion chain. It is negative when a
6010 reference binding will be applied, positive otherwise. If
6011 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
6012 conversions will be emitted if appropriate. If C_CAST_P is true,
6013 this conversion is coming from a C-style cast; in that case,
6014 conversions to inaccessible bases are permitted. */
6016 static tree
6017 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
6018 int inner, bool issue_conversion_warnings,
6019 bool c_cast_p, tsubst_flags_t complain)
6021 tree totype = convs->type;
6022 diagnostic_t diag_kind;
6023 int flags;
6024 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6026 if (convs->bad_p && !(complain & tf_error))
6027 return error_mark_node;
6029 if (convs->bad_p
6030 && convs->kind != ck_user
6031 && convs->kind != ck_list
6032 && convs->kind != ck_ambig
6033 && (convs->kind != ck_ref_bind
6034 || (convs->user_conv_p && next_conversion (convs)->bad_p))
6035 && (convs->kind != ck_rvalue
6036 || SCALAR_TYPE_P (totype))
6037 && convs->kind != ck_base)
6039 bool complained = false;
6040 conversion *t = convs;
6042 /* Give a helpful error if this is bad because of excess braces. */
6043 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6044 && SCALAR_TYPE_P (totype)
6045 && CONSTRUCTOR_NELTS (expr) > 0
6046 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
6048 complained = permerror (loc, "too many braces around initializer "
6049 "for %qT", totype);
6050 while (BRACE_ENCLOSED_INITIALIZER_P (expr)
6051 && CONSTRUCTOR_NELTS (expr) == 1)
6052 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6055 /* Give a helpful error if this is bad because a conversion to bool
6056 from std::nullptr_t requires direct-initialization. */
6057 if (NULLPTR_TYPE_P (TREE_TYPE (expr))
6058 && TREE_CODE (totype) == BOOLEAN_TYPE)
6059 complained = permerror (loc, "converting to %qT from %qT requires "
6060 "direct-initialization",
6061 totype, TREE_TYPE (expr));
6063 for (; t ; t = next_conversion (t))
6065 if (t->kind == ck_user && t->cand->reason)
6067 permerror (loc, "invalid user-defined conversion "
6068 "from %qT to %qT", TREE_TYPE (expr), totype);
6069 print_z_candidate (loc, "candidate is:", t->cand);
6070 expr = convert_like_real (t, expr, fn, argnum, 1,
6071 /*issue_conversion_warnings=*/false,
6072 /*c_cast_p=*/false,
6073 complain);
6074 if (convs->kind == ck_ref_bind)
6075 expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
6076 LOOKUP_NORMAL, NULL_TREE,
6077 complain);
6078 else
6079 expr = cp_convert (totype, expr, complain);
6080 if (fn)
6081 inform (DECL_SOURCE_LOCATION (fn),
6082 " initializing argument %P of %qD", argnum, fn);
6083 return expr;
6085 else if (t->kind == ck_user || !t->bad_p)
6087 expr = convert_like_real (t, expr, fn, argnum, 1,
6088 /*issue_conversion_warnings=*/false,
6089 /*c_cast_p=*/false,
6090 complain);
6091 break;
6093 else if (t->kind == ck_ambig)
6094 return convert_like_real (t, expr, fn, argnum, 1,
6095 /*issue_conversion_warnings=*/false,
6096 /*c_cast_p=*/false,
6097 complain);
6098 else if (t->kind == ck_identity)
6099 break;
6101 if (!complained)
6102 complained = permerror (loc, "invalid conversion from %qT to %qT",
6103 TREE_TYPE (expr), totype);
6104 if (complained && fn)
6105 inform (DECL_SOURCE_LOCATION (fn),
6106 " initializing argument %P of %qD", argnum, fn);
6108 return cp_convert (totype, expr, complain);
6111 if (issue_conversion_warnings && (complain & tf_warning))
6112 conversion_null_warnings (totype, expr, fn, argnum);
6114 switch (convs->kind)
6116 case ck_user:
6118 struct z_candidate *cand = convs->cand;
6119 tree convfn = cand->fn;
6120 unsigned i;
6122 /* When converting from an init list we consider explicit
6123 constructors, but actually trying to call one is an error. */
6124 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
6125 /* Unless this is for direct-list-initialization. */
6126 && !DIRECT_LIST_INIT_P (expr))
6128 if (!(complain & tf_error))
6129 return error_mark_node;
6130 error ("converting to %qT from initializer list would use "
6131 "explicit constructor %qD", totype, convfn);
6134 /* If we're initializing from {}, it's value-initialization. */
6135 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6136 && CONSTRUCTOR_NELTS (expr) == 0
6137 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
6139 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
6140 expr = build_value_init (totype, complain);
6141 expr = get_target_expr_sfinae (expr, complain);
6142 if (expr != error_mark_node)
6144 TARGET_EXPR_LIST_INIT_P (expr) = true;
6145 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
6147 return expr;
6150 expr = mark_rvalue_use (expr);
6152 /* Set user_conv_p on the argument conversions, so rvalue/base
6153 handling knows not to allow any more UDCs. */
6154 for (i = 0; i < cand->num_convs; ++i)
6155 cand->convs[i]->user_conv_p = true;
6157 expr = build_over_call (cand, LOOKUP_NORMAL, complain);
6159 /* If this is a constructor or a function returning an aggr type,
6160 we need to build up a TARGET_EXPR. */
6161 if (DECL_CONSTRUCTOR_P (convfn))
6163 expr = build_cplus_new (totype, expr, complain);
6165 /* Remember that this was list-initialization. */
6166 if (convs->check_narrowing && expr != error_mark_node)
6167 TARGET_EXPR_LIST_INIT_P (expr) = true;
6170 return expr;
6172 case ck_identity:
6173 expr = mark_rvalue_use (expr);
6174 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
6176 int nelts = CONSTRUCTOR_NELTS (expr);
6177 if (nelts == 0)
6178 expr = build_value_init (totype, complain);
6179 else if (nelts == 1)
6180 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6181 else
6182 gcc_unreachable ();
6185 if (type_unknown_p (expr))
6186 expr = instantiate_type (totype, expr, complain);
6187 /* Convert a constant to its underlying value, unless we are
6188 about to bind it to a reference, in which case we need to
6189 leave it as an lvalue. */
6190 if (inner >= 0)
6192 expr = decl_constant_value_safe (expr);
6193 if (expr == null_node && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
6194 /* If __null has been converted to an integer type, we do not
6195 want to warn about uses of EXPR as an integer, rather than
6196 as a pointer. */
6197 expr = build_int_cst (totype, 0);
6199 return expr;
6200 case ck_ambig:
6201 /* We leave bad_p off ck_ambig because overload resolution considers
6202 it valid, it just fails when we try to perform it. So we need to
6203 check complain here, too. */
6204 if (complain & tf_error)
6206 /* Call build_user_type_conversion again for the error. */
6207 build_user_type_conversion (totype, convs->u.expr, LOOKUP_NORMAL,
6208 complain);
6209 if (fn)
6210 inform (input_location, " initializing argument %P of %q+D",
6211 argnum, fn);
6213 return error_mark_node;
6215 case ck_list:
6217 /* Conversion to std::initializer_list<T>. */
6218 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
6219 tree new_ctor = build_constructor (init_list_type_node, NULL);
6220 unsigned len = CONSTRUCTOR_NELTS (expr);
6221 tree array, val, field;
6222 vec<constructor_elt, va_gc> *vec = NULL;
6223 unsigned ix;
6225 /* Convert all the elements. */
6226 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
6228 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
6229 1, false, false, complain);
6230 if (sub == error_mark_node)
6231 return sub;
6232 if (!BRACE_ENCLOSED_INITIALIZER_P (val))
6233 check_narrowing (TREE_TYPE (sub), val);
6234 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
6235 if (!TREE_CONSTANT (sub))
6236 TREE_CONSTANT (new_ctor) = false;
6238 /* Build up the array. */
6239 elttype = cp_build_qualified_type
6240 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
6241 array = build_array_of_n_type (elttype, len);
6242 array = finish_compound_literal (array, new_ctor, complain);
6243 /* Take the address explicitly rather than via decay_conversion
6244 to avoid the error about taking the address of a temporary. */
6245 array = cp_build_addr_expr (array, complain);
6246 array = cp_convert (build_pointer_type (elttype), array, complain);
6247 if (array == error_mark_node)
6248 return error_mark_node;
6250 /* Build up the initializer_list object. */
6251 totype = complete_type (totype);
6252 field = next_initializable_field (TYPE_FIELDS (totype));
6253 CONSTRUCTOR_APPEND_ELT (vec, field, array);
6254 field = next_initializable_field (DECL_CHAIN (field));
6255 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
6256 new_ctor = build_constructor (totype, vec);
6257 return get_target_expr_sfinae (new_ctor, complain);
6260 case ck_aggr:
6261 if (TREE_CODE (totype) == COMPLEX_TYPE)
6263 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
6264 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
6265 real = perform_implicit_conversion (TREE_TYPE (totype),
6266 real, complain);
6267 imag = perform_implicit_conversion (TREE_TYPE (totype),
6268 imag, complain);
6269 expr = build2 (COMPLEX_EXPR, totype, real, imag);
6270 return fold_if_not_in_template (expr);
6272 expr = reshape_init (totype, expr, complain);
6273 expr = get_target_expr_sfinae (digest_init (totype, expr, complain),
6274 complain);
6275 if (expr != error_mark_node)
6276 TARGET_EXPR_LIST_INIT_P (expr) = true;
6277 return expr;
6279 default:
6280 break;
6283 expr = convert_like_real (next_conversion (convs), expr, fn, argnum,
6284 convs->kind == ck_ref_bind ? -1 : 1,
6285 convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
6286 c_cast_p,
6287 complain);
6288 if (expr == error_mark_node)
6289 return error_mark_node;
6291 switch (convs->kind)
6293 case ck_rvalue:
6294 expr = decay_conversion (expr, complain);
6295 if (expr == error_mark_node)
6296 return error_mark_node;
6298 if (! MAYBE_CLASS_TYPE_P (totype))
6299 return expr;
6300 /* Else fall through. */
6301 case ck_base:
6302 if (convs->kind == ck_base && !convs->need_temporary_p)
6304 /* We are going to bind a reference directly to a base-class
6305 subobject of EXPR. */
6306 /* Build an expression for `*((base*) &expr)'. */
6307 expr = cp_build_addr_expr (expr, complain);
6308 expr = convert_to_base (expr, build_pointer_type (totype),
6309 !c_cast_p, /*nonnull=*/true, complain);
6310 expr = cp_build_indirect_ref (expr, RO_IMPLICIT_CONVERSION, complain);
6311 return expr;
6314 /* Copy-initialization where the cv-unqualified version of the source
6315 type is the same class as, or a derived class of, the class of the
6316 destination [is treated as direct-initialization]. [dcl.init] */
6317 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
6318 if (convs->user_conv_p)
6319 /* This conversion is being done in the context of a user-defined
6320 conversion (i.e. the second step of copy-initialization), so
6321 don't allow any more. */
6322 flags |= LOOKUP_NO_CONVERSION;
6323 if (convs->rvaluedness_matches_p)
6324 flags |= LOOKUP_PREFER_RVALUE;
6325 if (TREE_CODE (expr) == TARGET_EXPR
6326 && TARGET_EXPR_LIST_INIT_P (expr))
6327 /* Copy-list-initialization doesn't actually involve a copy. */
6328 return expr;
6329 expr = build_temp (expr, totype, flags, &diag_kind, complain);
6330 if (diag_kind && complain)
6332 maybe_print_user_conv_context (convs);
6333 if (fn)
6334 inform (DECL_SOURCE_LOCATION (fn),
6335 " initializing argument %P of %qD", argnum, fn);
6338 return build_cplus_new (totype, expr, complain);
6340 case ck_ref_bind:
6342 tree ref_type = totype;
6344 if (convs->bad_p && !next_conversion (convs)->bad_p)
6346 tree extype = TREE_TYPE (expr);
6347 if (TYPE_REF_IS_RVALUE (ref_type)
6348 && real_lvalue_p (expr))
6349 error_at (loc, "cannot bind %qT lvalue to %qT",
6350 extype, totype);
6351 else if (!TYPE_REF_IS_RVALUE (ref_type) && !real_lvalue_p (expr)
6352 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
6353 error_at (loc, "invalid initialization of non-const reference of "
6354 "type %qT from an rvalue of type %qT", totype, extype);
6355 else if (!reference_compatible_p (TREE_TYPE (totype), extype))
6356 error_at (loc, "binding %qT to reference of type %qT "
6357 "discards qualifiers", extype, totype);
6358 else
6359 gcc_unreachable ();
6360 maybe_print_user_conv_context (convs);
6361 if (fn)
6362 inform (input_location,
6363 " initializing argument %P of %q+D", argnum, fn);
6364 return error_mark_node;
6367 /* If necessary, create a temporary.
6369 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
6370 that need temporaries, even when their types are reference
6371 compatible with the type of reference being bound, so the
6372 upcoming call to cp_build_addr_expr doesn't fail. */
6373 if (convs->need_temporary_p
6374 || TREE_CODE (expr) == CONSTRUCTOR
6375 || TREE_CODE (expr) == VA_ARG_EXPR)
6377 /* Otherwise, a temporary of type "cv1 T1" is created and
6378 initialized from the initializer expression using the rules
6379 for a non-reference copy-initialization (8.5). */
6381 tree type = TREE_TYPE (ref_type);
6382 cp_lvalue_kind lvalue = real_lvalue_p (expr);
6384 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6385 (type, next_conversion (convs)->type));
6386 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
6387 && !TYPE_REF_IS_RVALUE (ref_type))
6389 /* If the reference is volatile or non-const, we
6390 cannot create a temporary. */
6391 if (lvalue & clk_bitfield)
6392 error_at (loc, "cannot bind bitfield %qE to %qT",
6393 expr, ref_type);
6394 else if (lvalue & clk_packed)
6395 error_at (loc, "cannot bind packed field %qE to %qT",
6396 expr, ref_type);
6397 else
6398 error_at (loc, "cannot bind rvalue %qE to %qT",
6399 expr, ref_type);
6400 return error_mark_node;
6402 /* If the source is a packed field, and we must use a copy
6403 constructor, then building the target expr will require
6404 binding the field to the reference parameter to the
6405 copy constructor, and we'll end up with an infinite
6406 loop. If we can use a bitwise copy, then we'll be
6407 OK. */
6408 if ((lvalue & clk_packed)
6409 && CLASS_TYPE_P (type)
6410 && type_has_nontrivial_copy_init (type))
6412 error_at (loc, "cannot bind packed field %qE to %qT",
6413 expr, ref_type);
6414 return error_mark_node;
6416 if (lvalue & clk_bitfield)
6418 expr = convert_bitfield_to_declared_type (expr);
6419 expr = fold_convert (type, expr);
6421 expr = build_target_expr_with_type (expr, type, complain);
6424 /* Take the address of the thing to which we will bind the
6425 reference. */
6426 expr = cp_build_addr_expr (expr, complain);
6427 if (expr == error_mark_node)
6428 return error_mark_node;
6430 /* Convert it to a pointer to the type referred to by the
6431 reference. This will adjust the pointer if a derived to
6432 base conversion is being performed. */
6433 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
6434 expr, complain);
6435 /* Convert the pointer to the desired reference type. */
6436 return build_nop (ref_type, expr);
6439 case ck_lvalue:
6440 return decay_conversion (expr, complain);
6442 case ck_qual:
6443 /* Warn about deprecated conversion if appropriate. */
6444 string_conv_p (totype, expr, 1);
6445 break;
6447 case ck_ptr:
6448 if (convs->base_p)
6449 expr = convert_to_base (expr, totype, !c_cast_p,
6450 /*nonnull=*/false, complain);
6451 return build_nop (totype, expr);
6453 case ck_pmem:
6454 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
6455 c_cast_p, complain);
6457 default:
6458 break;
6461 if (convs->check_narrowing)
6462 check_narrowing (totype, expr);
6464 if (issue_conversion_warnings)
6465 expr = cp_convert_and_check (totype, expr, complain);
6466 else
6467 expr = cp_convert (totype, expr, complain);
6469 return expr;
6472 /* ARG is being passed to a varargs function. Perform any conversions
6473 required. Return the converted value. */
6475 tree
6476 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
6478 tree arg_type;
6479 location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
6481 /* [expr.call]
6483 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6484 standard conversions are performed. */
6485 arg = decay_conversion (arg, complain);
6486 arg_type = TREE_TYPE (arg);
6487 /* [expr.call]
6489 If the argument has integral or enumeration type that is subject
6490 to the integral promotions (_conv.prom_), or a floating point
6491 type that is subject to the floating point promotion
6492 (_conv.fpprom_), the value of the argument is converted to the
6493 promoted type before the call. */
6494 if (TREE_CODE (arg_type) == REAL_TYPE
6495 && (TYPE_PRECISION (arg_type)
6496 < TYPE_PRECISION (double_type_node))
6497 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
6499 if ((complain & tf_warning)
6500 && warn_double_promotion && !c_inhibit_evaluation_warnings)
6501 warning_at (loc, OPT_Wdouble_promotion,
6502 "implicit conversion from %qT to %qT when passing "
6503 "argument to function",
6504 arg_type, double_type_node);
6505 arg = convert_to_real (double_type_node, arg);
6507 else if (NULLPTR_TYPE_P (arg_type))
6508 arg = null_pointer_node;
6509 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
6511 if (SCOPED_ENUM_P (arg_type))
6513 tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
6514 complain);
6515 prom = cp_perform_integral_promotions (prom, complain);
6516 if (abi_version_crosses (6)
6517 && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
6518 && (complain & tf_warning))
6519 warning_at (loc, OPT_Wabi, "scoped enum %qT passed through ... as "
6520 "%qT before -fabi-version=6, %qT after", arg_type,
6521 TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
6522 if (!abi_version_at_least (6))
6523 arg = prom;
6525 else
6526 arg = cp_perform_integral_promotions (arg, complain);
6529 arg = require_complete_type_sfinae (arg, complain);
6530 arg_type = TREE_TYPE (arg);
6532 if (arg != error_mark_node
6533 /* In a template (or ill-formed code), we can have an incomplete type
6534 even after require_complete_type_sfinae, in which case we don't know
6535 whether it has trivial copy or not. */
6536 && COMPLETE_TYPE_P (arg_type))
6538 /* Build up a real lvalue-to-rvalue conversion in case the
6539 copy constructor is trivial but not callable. */
6540 if (!cp_unevaluated_operand && CLASS_TYPE_P (arg_type))
6541 force_rvalue (arg, complain);
6543 /* [expr.call] 5.2.2/7:
6544 Passing a potentially-evaluated argument of class type (Clause 9)
6545 with a non-trivial copy constructor or a non-trivial destructor
6546 with no corresponding parameter is conditionally-supported, with
6547 implementation-defined semantics.
6549 We used to just warn here and do a bitwise copy, but now
6550 cp_expr_size will abort if we try to do that.
6552 If the call appears in the context of a sizeof expression,
6553 it is not potentially-evaluated. */
6554 if (cp_unevaluated_operand == 0
6555 && (type_has_nontrivial_copy_init (arg_type)
6556 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type)))
6558 if (complain & tf_error)
6559 error_at (loc, "cannot pass objects of non-trivially-copyable "
6560 "type %q#T through %<...%>", arg_type);
6561 return error_mark_node;
6565 return arg;
6568 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
6570 tree
6571 build_x_va_arg (source_location loc, tree expr, tree type)
6573 if (processing_template_decl)
6574 return build_min (VA_ARG_EXPR, type, expr);
6576 type = complete_type_or_else (type, NULL_TREE);
6578 if (expr == error_mark_node || !type)
6579 return error_mark_node;
6581 expr = mark_lvalue_use (expr);
6583 if (type_has_nontrivial_copy_init (type)
6584 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
6585 || TREE_CODE (type) == REFERENCE_TYPE)
6587 /* Remove reference types so we don't ICE later on. */
6588 tree type1 = non_reference (type);
6589 /* conditionally-supported behavior [expr.call] 5.2.2/7. */
6590 error ("cannot receive objects of non-trivially-copyable type %q#T "
6591 "through %<...%>; ", type);
6592 expr = convert (build_pointer_type (type1), null_node);
6593 expr = cp_build_indirect_ref (expr, RO_NULL, tf_warning_or_error);
6594 return expr;
6597 return build_va_arg (loc, expr, type);
6600 /* TYPE has been given to va_arg. Apply the default conversions which
6601 would have happened when passed via ellipsis. Return the promoted
6602 type, or the passed type if there is no change. */
6604 tree
6605 cxx_type_promotes_to (tree type)
6607 tree promote;
6609 /* Perform the array-to-pointer and function-to-pointer
6610 conversions. */
6611 type = type_decays_to (type);
6613 promote = type_promotes_to (type);
6614 if (same_type_p (type, promote))
6615 promote = type;
6617 return promote;
6620 /* ARG is a default argument expression being passed to a parameter of
6621 the indicated TYPE, which is a parameter to FN. PARMNUM is the
6622 zero-based argument number. Do any required conversions. Return
6623 the converted value. */
6625 static GTY(()) vec<tree, va_gc> *default_arg_context;
6626 void
6627 push_defarg_context (tree fn)
6628 { vec_safe_push (default_arg_context, fn); }
6630 void
6631 pop_defarg_context (void)
6632 { default_arg_context->pop (); }
6634 tree
6635 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
6636 tsubst_flags_t complain)
6638 int i;
6639 tree t;
6641 /* See through clones. */
6642 fn = DECL_ORIGIN (fn);
6644 /* Detect recursion. */
6645 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
6646 if (t == fn)
6648 if (complain & tf_error)
6649 error ("recursive evaluation of default argument for %q#D", fn);
6650 return error_mark_node;
6653 /* If the ARG is an unparsed default argument expression, the
6654 conversion cannot be performed. */
6655 if (TREE_CODE (arg) == DEFAULT_ARG)
6657 if (complain & tf_error)
6658 error ("call to %qD uses the default argument for parameter %P, which "
6659 "is not yet defined", fn, parmnum);
6660 return error_mark_node;
6663 push_defarg_context (fn);
6665 if (fn && DECL_TEMPLATE_INFO (fn))
6666 arg = tsubst_default_argument (fn, type, arg, complain);
6668 /* Due to:
6670 [dcl.fct.default]
6672 The names in the expression are bound, and the semantic
6673 constraints are checked, at the point where the default
6674 expressions appears.
6676 we must not perform access checks here. */
6677 push_deferring_access_checks (dk_no_check);
6678 /* We must make a copy of ARG, in case subsequent processing
6679 alters any part of it. */
6680 arg = break_out_target_exprs (arg);
6681 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
6682 ICR_DEFAULT_ARGUMENT, fn, parmnum,
6683 complain);
6684 arg = convert_for_arg_passing (type, arg, complain);
6685 pop_deferring_access_checks();
6687 pop_defarg_context ();
6689 return arg;
6692 /* Returns the type which will really be used for passing an argument of
6693 type TYPE. */
6695 tree
6696 type_passed_as (tree type)
6698 /* Pass classes with copy ctors by invisible reference. */
6699 if (TREE_ADDRESSABLE (type))
6701 type = build_reference_type (type);
6702 /* There are no other pointers to this temporary. */
6703 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
6705 else if (targetm.calls.promote_prototypes (type)
6706 && INTEGRAL_TYPE_P (type)
6707 && COMPLETE_TYPE_P (type)
6708 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
6709 type = integer_type_node;
6711 return type;
6714 /* Actually perform the appropriate conversion. */
6716 tree
6717 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
6719 tree bitfield_type;
6721 /* If VAL is a bitfield, then -- since it has already been converted
6722 to TYPE -- it cannot have a precision greater than TYPE.
6724 If it has a smaller precision, we must widen it here. For
6725 example, passing "int f:3;" to a function expecting an "int" will
6726 not result in any conversion before this point.
6728 If the precision is the same we must not risk widening. For
6729 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
6730 often have type "int", even though the C++ type for the field is
6731 "long long". If the value is being passed to a function
6732 expecting an "int", then no conversions will be required. But,
6733 if we call convert_bitfield_to_declared_type, the bitfield will
6734 be converted to "long long". */
6735 bitfield_type = is_bitfield_expr_with_lowered_type (val);
6736 if (bitfield_type
6737 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
6738 val = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type), val);
6740 if (val == error_mark_node)
6742 /* Pass classes with copy ctors by invisible reference. */
6743 else if (TREE_ADDRESSABLE (type))
6744 val = build1 (ADDR_EXPR, build_reference_type (type), val);
6745 else if (targetm.calls.promote_prototypes (type)
6746 && INTEGRAL_TYPE_P (type)
6747 && COMPLETE_TYPE_P (type)
6748 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
6749 val = cp_perform_integral_promotions (val, complain);
6750 if ((complain & tf_warning)
6751 && warn_suggest_attribute_format)
6753 tree rhstype = TREE_TYPE (val);
6754 const enum tree_code coder = TREE_CODE (rhstype);
6755 const enum tree_code codel = TREE_CODE (type);
6756 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6757 && coder == codel
6758 && check_missing_format_attribute (type, rhstype))
6759 warning (OPT_Wsuggest_attribute_format,
6760 "argument of function call might be a candidate for a format attribute");
6762 return val;
6765 /* Returns true iff FN is a function with magic varargs, i.e. ones for
6766 which no conversions at all should be done. This is true for some
6767 builtins which don't act like normal functions. */
6769 bool
6770 magic_varargs_p (tree fn)
6772 if (flag_cilkplus && is_cilkplus_reduce_builtin (fn) != BUILT_IN_NONE)
6773 return true;
6775 if (DECL_BUILT_IN (fn))
6776 switch (DECL_FUNCTION_CODE (fn))
6778 case BUILT_IN_CLASSIFY_TYPE:
6779 case BUILT_IN_CONSTANT_P:
6780 case BUILT_IN_NEXT_ARG:
6781 case BUILT_IN_VA_START:
6782 return true;
6784 default:;
6785 return lookup_attribute ("type generic",
6786 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
6789 return false;
6792 /* Returns the decl of the dispatcher function if FN is a function version. */
6794 tree
6795 get_function_version_dispatcher (tree fn)
6797 tree dispatcher_decl = NULL;
6799 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6800 && DECL_FUNCTION_VERSIONED (fn));
6802 gcc_assert (targetm.get_function_versions_dispatcher);
6803 dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
6805 if (dispatcher_decl == NULL)
6807 error_at (input_location, "use of multiversioned function "
6808 "without a default");
6809 return NULL;
6812 retrofit_lang_decl (dispatcher_decl);
6813 gcc_assert (dispatcher_decl != NULL);
6814 return dispatcher_decl;
6817 /* fn is a function version dispatcher that is marked used. Mark all the
6818 semantically identical function versions it will dispatch as used. */
6820 void
6821 mark_versions_used (tree fn)
6823 struct cgraph_node *node;
6824 struct cgraph_function_version_info *node_v;
6825 struct cgraph_function_version_info *it_v;
6827 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6829 node = cgraph_get_node (fn);
6830 if (node == NULL)
6831 return;
6833 gcc_assert (node->dispatcher_function);
6835 node_v = get_cgraph_node_version (node);
6836 if (node_v == NULL)
6837 return;
6839 /* All semantically identical versions are chained. Traverse and mark each
6840 one of them as used. */
6841 it_v = node_v->next;
6842 while (it_v != NULL)
6844 mark_used (it_v->this_node->decl);
6845 it_v = it_v->next;
6849 /* Subroutine of the various build_*_call functions. Overload resolution
6850 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
6851 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
6852 bitmask of various LOOKUP_* flags which apply to the call itself. */
6854 static tree
6855 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
6857 tree fn = cand->fn;
6858 const vec<tree, va_gc> *args = cand->args;
6859 tree first_arg = cand->first_arg;
6860 conversion **convs = cand->convs;
6861 conversion *conv;
6862 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
6863 int parmlen;
6864 tree val;
6865 int i = 0;
6866 int j = 0;
6867 unsigned int arg_index = 0;
6868 int is_method = 0;
6869 int nargs;
6870 tree *argarray;
6871 bool already_used = false;
6873 /* In a template, there is no need to perform all of the work that
6874 is normally done. We are only interested in the type of the call
6875 expression, i.e., the return type of the function. Any semantic
6876 errors will be deferred until the template is instantiated. */
6877 if (processing_template_decl)
6879 tree expr, addr;
6880 tree return_type;
6881 const tree *argarray;
6882 unsigned int nargs;
6884 return_type = TREE_TYPE (TREE_TYPE (fn));
6885 nargs = vec_safe_length (args);
6886 if (first_arg == NULL_TREE)
6887 argarray = args->address ();
6888 else
6890 tree *alcarray;
6891 unsigned int ix;
6892 tree arg;
6894 ++nargs;
6895 alcarray = XALLOCAVEC (tree, nargs);
6896 alcarray[0] = first_arg;
6897 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
6898 alcarray[ix + 1] = arg;
6899 argarray = alcarray;
6902 addr = build_addr_func (fn, complain);
6903 if (addr == error_mark_node)
6904 return error_mark_node;
6905 expr = build_call_array_loc (input_location, return_type,
6906 addr, nargs, argarray);
6907 if (TREE_THIS_VOLATILE (fn) && cfun)
6908 current_function_returns_abnormally = 1;
6909 return convert_from_reference (expr);
6912 /* Give any warnings we noticed during overload resolution. */
6913 if (cand->warnings && (complain & tf_warning))
6915 struct candidate_warning *w;
6916 for (w = cand->warnings; w; w = w->next)
6917 joust (cand, w->loser, 1, complain);
6920 /* Make =delete work with SFINAE. */
6921 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
6922 return error_mark_node;
6924 if (DECL_FUNCTION_MEMBER_P (fn))
6926 tree access_fn;
6927 /* If FN is a template function, two cases must be considered.
6928 For example:
6930 struct A {
6931 protected:
6932 template <class T> void f();
6934 template <class T> struct B {
6935 protected:
6936 void g();
6938 struct C : A, B<int> {
6939 using A::f; // #1
6940 using B<int>::g; // #2
6943 In case #1 where `A::f' is a member template, DECL_ACCESS is
6944 recorded in the primary template but not in its specialization.
6945 We check access of FN using its primary template.
6947 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
6948 because it is a member of class template B, DECL_ACCESS is
6949 recorded in the specialization `B<int>::g'. We cannot use its
6950 primary template because `B<T>::g' and `B<int>::g' may have
6951 different access. */
6952 if (DECL_TEMPLATE_INFO (fn)
6953 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
6954 access_fn = DECL_TI_TEMPLATE (fn);
6955 else
6956 access_fn = fn;
6957 if (!perform_or_defer_access_check (cand->access_path, access_fn,
6958 fn, complain))
6959 return error_mark_node;
6962 /* If we're checking for implicit delete, don't bother with argument
6963 conversions. */
6964 if (flags & LOOKUP_SPECULATIVE)
6966 if (DECL_DELETED_FN (fn))
6968 if (complain & tf_error)
6969 mark_used (fn);
6970 return error_mark_node;
6972 if (cand->viable == 1)
6973 return fn;
6974 else if (!(complain & tf_error))
6975 /* Reject bad conversions now. */
6976 return error_mark_node;
6977 /* else continue to get conversion error. */
6980 /* N3276 magic doesn't apply to nested calls. */
6981 int decltype_flag = (complain & tf_decltype);
6982 complain &= ~tf_decltype;
6984 /* Find maximum size of vector to hold converted arguments. */
6985 parmlen = list_length (parm);
6986 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
6987 if (parmlen > nargs)
6988 nargs = parmlen;
6989 argarray = XALLOCAVEC (tree, nargs);
6991 /* The implicit parameters to a constructor are not considered by overload
6992 resolution, and must be of the proper type. */
6993 if (DECL_CONSTRUCTOR_P (fn))
6995 tree object_arg;
6996 if (first_arg != NULL_TREE)
6998 object_arg = first_arg;
6999 first_arg = NULL_TREE;
7001 else
7003 object_arg = (*args)[arg_index];
7004 ++arg_index;
7006 argarray[j++] = build_this (object_arg);
7007 parm = TREE_CHAIN (parm);
7008 /* We should never try to call the abstract constructor. */
7009 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
7011 if (DECL_HAS_VTT_PARM_P (fn))
7013 argarray[j++] = (*args)[arg_index];
7014 ++arg_index;
7015 parm = TREE_CHAIN (parm);
7018 /* Bypass access control for 'this' parameter. */
7019 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7021 tree parmtype = TREE_VALUE (parm);
7022 tree arg = build_this (first_arg != NULL_TREE
7023 ? first_arg
7024 : (*args)[arg_index]);
7025 tree argtype = TREE_TYPE (arg);
7026 tree converted_arg;
7027 tree base_binfo;
7029 if (convs[i]->bad_p)
7031 if (complain & tf_error)
7033 if (permerror (input_location, "passing %qT as %<this%> "
7034 "argument discards qualifiers",
7035 TREE_TYPE (argtype)))
7036 inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
7038 else
7039 return error_mark_node;
7042 /* See if the function member or the whole class type is declared
7043 final and the call can be devirtualized. */
7044 if (DECL_FINAL_P (fn)
7045 || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
7046 flags |= LOOKUP_NONVIRTUAL;
7048 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
7049 X is called for an object that is not of type X, or of a type
7050 derived from X, the behavior is undefined.
7052 So we can assume that anything passed as 'this' is non-null, and
7053 optimize accordingly. */
7054 gcc_assert (TYPE_PTR_P (parmtype));
7055 /* Convert to the base in which the function was declared. */
7056 gcc_assert (cand->conversion_path != NULL_TREE);
7057 converted_arg = build_base_path (PLUS_EXPR,
7058 arg,
7059 cand->conversion_path,
7060 1, complain);
7061 /* Check that the base class is accessible. */
7062 if (!accessible_base_p (TREE_TYPE (argtype),
7063 BINFO_TYPE (cand->conversion_path), true))
7065 if (complain & tf_error)
7066 error ("%qT is not an accessible base of %qT",
7067 BINFO_TYPE (cand->conversion_path),
7068 TREE_TYPE (argtype));
7069 else
7070 return error_mark_node;
7072 /* If fn was found by a using declaration, the conversion path
7073 will be to the derived class, not the base declaring fn. We
7074 must convert from derived to base. */
7075 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
7076 TREE_TYPE (parmtype), ba_unique,
7077 NULL, complain);
7078 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
7079 base_binfo, 1, complain);
7081 argarray[j++] = converted_arg;
7082 parm = TREE_CHAIN (parm);
7083 if (first_arg != NULL_TREE)
7084 first_arg = NULL_TREE;
7085 else
7086 ++arg_index;
7087 ++i;
7088 is_method = 1;
7091 gcc_assert (first_arg == NULL_TREE);
7092 for (; arg_index < vec_safe_length (args) && parm;
7093 parm = TREE_CHAIN (parm), ++arg_index, ++i)
7095 tree type = TREE_VALUE (parm);
7096 tree arg = (*args)[arg_index];
7097 bool conversion_warning = true;
7099 conv = convs[i];
7101 /* If the argument is NULL and used to (implicitly) instantiate a
7102 template function (and bind one of the template arguments to
7103 the type of 'long int'), we don't want to warn about passing NULL
7104 to non-pointer argument.
7105 For example, if we have this template function:
7107 template<typename T> void func(T x) {}
7109 we want to warn (when -Wconversion is enabled) in this case:
7111 void foo() {
7112 func<int>(NULL);
7115 but not in this case:
7117 void foo() {
7118 func(NULL);
7121 if (arg == null_node
7122 && DECL_TEMPLATE_INFO (fn)
7123 && cand->template_decl
7124 && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
7125 conversion_warning = false;
7127 /* Warn about initializer_list deduction that isn't currently in the
7128 working draft. */
7129 if (cxx_dialect > cxx98
7130 && flag_deduce_init_list
7131 && cand->template_decl
7132 && is_std_init_list (non_reference (type))
7133 && BRACE_ENCLOSED_INITIALIZER_P (arg))
7135 tree tmpl = TI_TEMPLATE (cand->template_decl);
7136 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
7137 tree patparm = get_pattern_parm (realparm, tmpl);
7138 tree pattype = TREE_TYPE (patparm);
7139 if (PACK_EXPANSION_P (pattype))
7140 pattype = PACK_EXPANSION_PATTERN (pattype);
7141 pattype = non_reference (pattype);
7143 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
7144 && (cand->explicit_targs == NULL_TREE
7145 || (TREE_VEC_LENGTH (cand->explicit_targs)
7146 <= TEMPLATE_TYPE_IDX (pattype))))
7148 pedwarn (input_location, 0, "deducing %qT as %qT",
7149 non_reference (TREE_TYPE (patparm)),
7150 non_reference (type));
7151 pedwarn (input_location, 0, " in call to %q+D", cand->fn);
7152 pedwarn (input_location, 0,
7153 " (you can disable this with -fno-deduce-init-list)");
7156 val = convert_like_with_context (conv, arg, fn, i - is_method,
7157 conversion_warning
7158 ? complain
7159 : complain & (~tf_warning));
7161 val = convert_for_arg_passing (type, val, complain);
7163 if (val == error_mark_node)
7164 return error_mark_node;
7165 else
7166 argarray[j++] = val;
7169 /* Default arguments */
7170 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
7172 if (TREE_VALUE (parm) == error_mark_node)
7173 return error_mark_node;
7174 argarray[j++] = convert_default_arg (TREE_VALUE (parm),
7175 TREE_PURPOSE (parm),
7176 fn, i - is_method,
7177 complain);
7180 /* Ellipsis */
7181 for (; arg_index < vec_safe_length (args); ++arg_index)
7183 tree a = (*args)[arg_index];
7184 if (magic_varargs_p (fn))
7185 /* Do no conversions for magic varargs. */
7186 a = mark_type_use (a);
7187 else
7188 a = convert_arg_to_ellipsis (a, complain);
7189 argarray[j++] = a;
7192 gcc_assert (j <= nargs);
7193 nargs = j;
7195 check_function_arguments (TREE_TYPE (fn), nargs, argarray);
7197 /* Avoid actually calling copy constructors and copy assignment operators,
7198 if possible. */
7200 if (! flag_elide_constructors)
7201 /* Do things the hard way. */;
7202 else if (cand->num_convs == 1
7203 && (DECL_COPY_CONSTRUCTOR_P (fn)
7204 || DECL_MOVE_CONSTRUCTOR_P (fn)))
7206 tree targ;
7207 tree arg = argarray[num_artificial_parms_for (fn)];
7208 tree fa;
7209 bool trivial = trivial_fn_p (fn);
7211 /* Pull out the real argument, disregarding const-correctness. */
7212 targ = arg;
7213 while (CONVERT_EXPR_P (targ)
7214 || TREE_CODE (targ) == NON_LVALUE_EXPR)
7215 targ = TREE_OPERAND (targ, 0);
7216 if (TREE_CODE (targ) == ADDR_EXPR)
7218 targ = TREE_OPERAND (targ, 0);
7219 if (!same_type_ignoring_top_level_qualifiers_p
7220 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
7221 targ = NULL_TREE;
7223 else
7224 targ = NULL_TREE;
7226 if (targ)
7227 arg = targ;
7228 else
7229 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
7231 /* [class.copy]: the copy constructor is implicitly defined even if
7232 the implementation elided its use. */
7233 if (!trivial || DECL_DELETED_FN (fn))
7235 mark_used (fn);
7236 already_used = true;
7239 /* If we're creating a temp and we already have one, don't create a
7240 new one. If we're not creating a temp but we get one, use
7241 INIT_EXPR to collapse the temp into our target. Otherwise, if the
7242 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
7243 temp or an INIT_EXPR otherwise. */
7244 fa = argarray[0];
7245 if (is_dummy_object (fa))
7247 if (TREE_CODE (arg) == TARGET_EXPR)
7248 return arg;
7249 else if (trivial)
7250 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
7252 else if (TREE_CODE (arg) == TARGET_EXPR || trivial)
7254 tree to = stabilize_reference (cp_build_indirect_ref (fa, RO_NULL,
7255 complain));
7257 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
7258 return val;
7261 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
7262 && trivial_fn_p (fn)
7263 && !DECL_DELETED_FN (fn))
7265 tree to = stabilize_reference
7266 (cp_build_indirect_ref (argarray[0], RO_NULL, complain));
7267 tree type = TREE_TYPE (to);
7268 tree as_base = CLASSTYPE_AS_BASE (type);
7269 tree arg = argarray[1];
7271 if (is_really_empty_class (type))
7273 /* Avoid copying empty classes. */
7274 val = build2 (COMPOUND_EXPR, void_type_node, to, arg);
7275 TREE_NO_WARNING (val) = 1;
7276 val = build2 (COMPOUND_EXPR, type, val, to);
7277 TREE_NO_WARNING (val) = 1;
7279 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
7281 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
7282 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
7284 else
7286 /* We must only copy the non-tail padding parts. */
7287 tree arg0, arg2, t;
7288 tree array_type, alias_set;
7290 arg2 = TYPE_SIZE_UNIT (as_base);
7291 arg0 = cp_build_addr_expr (to, complain);
7293 array_type = build_array_type (char_type_node,
7294 build_index_type
7295 (size_binop (MINUS_EXPR,
7296 arg2, size_int (1))));
7297 alias_set = build_int_cst (build_pointer_type (type), 0);
7298 t = build2 (MODIFY_EXPR, void_type_node,
7299 build2 (MEM_REF, array_type, arg0, alias_set),
7300 build2 (MEM_REF, array_type, arg, alias_set));
7301 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
7302 TREE_NO_WARNING (val) = 1;
7305 return val;
7307 else if (DECL_DESTRUCTOR_P (fn)
7308 && trivial_fn_p (fn)
7309 && !DECL_DELETED_FN (fn))
7310 return fold_convert (void_type_node, argarray[0]);
7311 /* FIXME handle trivial default constructor, too. */
7313 /* For calls to a multi-versioned function, overload resolution
7314 returns the function with the highest target priority, that is,
7315 the version that will checked for dispatching first. If this
7316 version is inlinable, a direct call to this version can be made
7317 otherwise the call should go through the dispatcher. */
7319 if (DECL_FUNCTION_VERSIONED (fn)
7320 && (current_function_decl == NULL
7321 || !targetm.target_option.can_inline_p (current_function_decl, fn)))
7323 fn = get_function_version_dispatcher (fn);
7324 if (fn == NULL)
7325 return NULL;
7326 if (!already_used)
7327 mark_versions_used (fn);
7330 if (!already_used
7331 && !mark_used (fn))
7332 return error_mark_node;
7334 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
7335 /* Don't mess with virtual lookup in fold_non_dependent_expr; virtual
7336 functions can't be constexpr. */
7337 && !in_template_function ())
7339 tree t;
7340 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
7341 DECL_CONTEXT (fn),
7342 ba_any, NULL, complain);
7343 gcc_assert (binfo && binfo != error_mark_node);
7345 /* Warn about deprecated virtual functions now, since we're about
7346 to throw away the decl. */
7347 if (TREE_DEPRECATED (fn))
7348 warn_deprecated_use (fn, NULL_TREE);
7350 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
7351 complain);
7352 if (TREE_SIDE_EFFECTS (argarray[0]))
7353 argarray[0] = save_expr (argarray[0]);
7354 t = build_pointer_type (TREE_TYPE (fn));
7355 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
7356 fn = build_java_interface_fn_ref (fn, argarray[0]);
7357 else
7358 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
7359 TREE_TYPE (fn) = t;
7361 else
7363 fn = build_addr_func (fn, complain);
7364 if (fn == error_mark_node)
7365 return error_mark_node;
7368 tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
7369 if (TREE_CODE (call) == CALL_EXPR
7370 && (cand->flags & LOOKUP_LIST_INIT_CTOR))
7371 CALL_EXPR_LIST_INIT_P (call) = true;
7372 return call;
7375 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
7376 This function performs no overload resolution, conversion, or other
7377 high-level operations. */
7379 tree
7380 build_cxx_call (tree fn, int nargs, tree *argarray,
7381 tsubst_flags_t complain)
7383 tree fndecl;
7384 int optimize_sav;
7386 /* Remember roughly where this call is. */
7387 location_t loc = EXPR_LOC_OR_LOC (fn, input_location);
7388 fn = build_call_a (fn, nargs, argarray);
7389 SET_EXPR_LOCATION (fn, loc);
7391 fndecl = get_callee_fndecl (fn);
7393 /* Check that arguments to builtin functions match the expectations. */
7394 if (fndecl
7395 && DECL_BUILT_IN (fndecl)
7396 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7397 && !check_builtin_function_arguments (fndecl, nargs, argarray))
7398 return error_mark_node;
7400 /* If it is a built-in array notation function, then the return type of
7401 the function is the element type of the array passed in as array
7402 notation (i.e. the first parameter of the function). */
7403 if (flag_cilkplus && TREE_CODE (fn) == CALL_EXPR)
7405 enum built_in_function bif =
7406 is_cilkplus_reduce_builtin (CALL_EXPR_FN (fn));
7407 if (bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
7408 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
7409 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
7410 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
7411 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE
7412 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
7414 /* for bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO or
7415 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO or
7416 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO or
7417 BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO or
7418 BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND or
7419 BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
7420 The pre-defined return-type is the correct one. */
7421 tree array_ntn = CALL_EXPR_ARG (fn, 0);
7422 TREE_TYPE (fn) = TREE_TYPE (array_ntn);
7423 return fn;
7427 /* Some built-in function calls will be evaluated at compile-time in
7428 fold (). Set optimize to 1 when folding __builtin_constant_p inside
7429 a constexpr function so that fold_builtin_1 doesn't fold it to 0. */
7430 optimize_sav = optimize;
7431 if (!optimize && fndecl && DECL_IS_BUILTIN_CONSTANT_P (fndecl)
7432 && current_function_decl
7433 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
7434 optimize = 1;
7435 fn = fold_if_not_in_template (fn);
7436 optimize = optimize_sav;
7438 if (VOID_TYPE_P (TREE_TYPE (fn)))
7439 return fn;
7441 /* 5.2.2/11: If a function call is a prvalue of object type: if the
7442 function call is either the operand of a decltype-specifier or the
7443 right operand of a comma operator that is the operand of a
7444 decltype-specifier, a temporary object is not introduced for the
7445 prvalue. The type of the prvalue may be incomplete. */
7446 if (!(complain & tf_decltype))
7448 fn = require_complete_type_sfinae (fn, complain);
7449 if (fn == error_mark_node)
7450 return error_mark_node;
7452 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
7453 fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
7455 return convert_from_reference (fn);
7458 static GTY(()) tree java_iface_lookup_fn;
7460 /* Make an expression which yields the address of the Java interface
7461 method FN. This is achieved by generating a call to libjava's
7462 _Jv_LookupInterfaceMethodIdx(). */
7464 static tree
7465 build_java_interface_fn_ref (tree fn, tree instance)
7467 tree lookup_fn, method, idx;
7468 tree klass_ref, iface, iface_ref;
7469 int i;
7471 if (!java_iface_lookup_fn)
7473 tree ftype = build_function_type_list (ptr_type_node,
7474 ptr_type_node, ptr_type_node,
7475 java_int_type_node, NULL_TREE);
7476 java_iface_lookup_fn
7477 = add_builtin_function ("_Jv_LookupInterfaceMethodIdx", ftype,
7478 0, NOT_BUILT_IN, NULL, NULL_TREE);
7481 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
7482 This is the first entry in the vtable. */
7483 klass_ref = build_vtbl_ref (cp_build_indirect_ref (instance, RO_NULL,
7484 tf_warning_or_error),
7485 integer_zero_node);
7487 /* Get the java.lang.Class pointer for the interface being called. */
7488 iface = DECL_CONTEXT (fn);
7489 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
7490 if (!iface_ref || !VAR_P (iface_ref)
7491 || DECL_CONTEXT (iface_ref) != iface)
7493 error ("could not find class$ field in java interface type %qT",
7494 iface);
7495 return error_mark_node;
7497 iface_ref = build_address (iface_ref);
7498 iface_ref = convert (build_pointer_type (iface), iface_ref);
7500 /* Determine the itable index of FN. */
7501 i = 1;
7502 for (method = TYPE_METHODS (iface); method; method = DECL_CHAIN (method))
7504 if (!DECL_VIRTUAL_P (method))
7505 continue;
7506 if (fn == method)
7507 break;
7508 i++;
7510 idx = build_int_cst (NULL_TREE, i);
7512 lookup_fn = build1 (ADDR_EXPR,
7513 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
7514 java_iface_lookup_fn);
7515 return build_call_nary (ptr_type_node, lookup_fn,
7516 3, klass_ref, iface_ref, idx);
7519 /* Returns the value to use for the in-charge parameter when making a
7520 call to a function with the indicated NAME.
7522 FIXME:Can't we find a neater way to do this mapping? */
7524 tree
7525 in_charge_arg_for_name (tree name)
7527 if (name == base_ctor_identifier
7528 || name == base_dtor_identifier)
7529 return integer_zero_node;
7530 else if (name == complete_ctor_identifier)
7531 return integer_one_node;
7532 else if (name == complete_dtor_identifier)
7533 return integer_two_node;
7534 else if (name == deleting_dtor_identifier)
7535 return integer_three_node;
7537 /* This function should only be called with one of the names listed
7538 above. */
7539 gcc_unreachable ();
7540 return NULL_TREE;
7543 /* Build a call to a constructor, destructor, or an assignment
7544 operator for INSTANCE, an expression with class type. NAME
7545 indicates the special member function to call; *ARGS are the
7546 arguments. ARGS may be NULL. This may change ARGS. BINFO
7547 indicates the base of INSTANCE that is to be passed as the `this'
7548 parameter to the member function called.
7550 FLAGS are the LOOKUP_* flags to use when processing the call.
7552 If NAME indicates a complete object constructor, INSTANCE may be
7553 NULL_TREE. In this case, the caller will call build_cplus_new to
7554 store the newly constructed object into a VAR_DECL. */
7556 tree
7557 build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
7558 tree binfo, int flags, tsubst_flags_t complain)
7560 tree fns;
7561 /* The type of the subobject to be constructed or destroyed. */
7562 tree class_type;
7563 vec<tree, va_gc> *allocated = NULL;
7564 tree ret;
7566 gcc_assert (name == complete_ctor_identifier
7567 || name == base_ctor_identifier
7568 || name == complete_dtor_identifier
7569 || name == base_dtor_identifier
7570 || name == deleting_dtor_identifier
7571 || name == ansi_assopname (NOP_EXPR));
7572 if (TYPE_P (binfo))
7574 /* Resolve the name. */
7575 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
7576 return error_mark_node;
7578 binfo = TYPE_BINFO (binfo);
7581 gcc_assert (binfo != NULL_TREE);
7583 class_type = BINFO_TYPE (binfo);
7585 /* Handle the special case where INSTANCE is NULL_TREE. */
7586 if (name == complete_ctor_identifier && !instance)
7587 instance = build_dummy_object (class_type);
7588 else
7590 if (name == complete_dtor_identifier
7591 || name == base_dtor_identifier
7592 || name == deleting_dtor_identifier)
7593 gcc_assert (args == NULL || vec_safe_is_empty (*args));
7595 /* Convert to the base class, if necessary. */
7596 if (!same_type_ignoring_top_level_qualifiers_p
7597 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
7599 if (name != ansi_assopname (NOP_EXPR))
7600 /* For constructors and destructors, either the base is
7601 non-virtual, or it is virtual but we are doing the
7602 conversion from a constructor or destructor for the
7603 complete object. In either case, we can convert
7604 statically. */
7605 instance = convert_to_base_statically (instance, binfo);
7606 else
7607 /* However, for assignment operators, we must convert
7608 dynamically if the base is virtual. */
7609 instance = build_base_path (PLUS_EXPR, instance,
7610 binfo, /*nonnull=*/1, complain);
7614 gcc_assert (instance != NULL_TREE);
7616 fns = lookup_fnfields (binfo, name, 1);
7618 /* When making a call to a constructor or destructor for a subobject
7619 that uses virtual base classes, pass down a pointer to a VTT for
7620 the subobject. */
7621 if ((name == base_ctor_identifier
7622 || name == base_dtor_identifier)
7623 && CLASSTYPE_VBASECLASSES (class_type))
7625 tree vtt;
7626 tree sub_vtt;
7628 /* If the current function is a complete object constructor
7629 or destructor, then we fetch the VTT directly.
7630 Otherwise, we look it up using the VTT we were given. */
7631 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
7632 vtt = decay_conversion (vtt, complain);
7633 if (vtt == error_mark_node)
7634 return error_mark_node;
7635 vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
7636 build2 (EQ_EXPR, boolean_type_node,
7637 current_in_charge_parm, integer_zero_node),
7638 current_vtt_parm,
7639 vtt);
7640 if (BINFO_SUBVTT_INDEX (binfo))
7641 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
7642 else
7643 sub_vtt = vtt;
7645 if (args == NULL)
7647 allocated = make_tree_vector ();
7648 args = &allocated;
7651 vec_safe_insert (*args, 0, sub_vtt);
7654 ret = build_new_method_call (instance, fns, args,
7655 TYPE_BINFO (BINFO_TYPE (binfo)),
7656 flags, /*fn=*/NULL,
7657 complain);
7659 if (allocated != NULL)
7660 release_tree_vector (allocated);
7662 if ((complain & tf_error)
7663 && (flags & LOOKUP_DELEGATING_CONS)
7664 && name == complete_ctor_identifier
7665 && TREE_CODE (ret) == CALL_EXPR
7666 && (DECL_ABSTRACT_ORIGIN (TREE_OPERAND (CALL_EXPR_FN (ret), 0))
7667 == current_function_decl))
7668 error ("constructor delegates to itself");
7670 return ret;
7673 /* Return the NAME, as a C string. The NAME indicates a function that
7674 is a member of TYPE. *FREE_P is set to true if the caller must
7675 free the memory returned.
7677 Rather than go through all of this, we should simply set the names
7678 of constructors and destructors appropriately, and dispense with
7679 ctor_identifier, dtor_identifier, etc. */
7681 static char *
7682 name_as_c_string (tree name, tree type, bool *free_p)
7684 char *pretty_name;
7686 /* Assume that we will not allocate memory. */
7687 *free_p = false;
7688 /* Constructors and destructors are special. */
7689 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
7691 pretty_name
7692 = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type))));
7693 /* For a destructor, add the '~'. */
7694 if (name == complete_dtor_identifier
7695 || name == base_dtor_identifier
7696 || name == deleting_dtor_identifier)
7698 pretty_name = concat ("~", pretty_name, NULL);
7699 /* Remember that we need to free the memory allocated. */
7700 *free_p = true;
7703 else if (IDENTIFIER_TYPENAME_P (name))
7705 pretty_name = concat ("operator ",
7706 type_as_string_translate (TREE_TYPE (name),
7707 TFF_PLAIN_IDENTIFIER),
7708 NULL);
7709 /* Remember that we need to free the memory allocated. */
7710 *free_p = true;
7712 else
7713 pretty_name = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (name)));
7715 return pretty_name;
7718 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
7719 be set, upon return, to the function called. ARGS may be NULL.
7720 This may change ARGS. */
7722 static tree
7723 build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
7724 tree conversion_path, int flags,
7725 tree *fn_p, tsubst_flags_t complain)
7727 struct z_candidate *candidates = 0, *cand;
7728 tree explicit_targs = NULL_TREE;
7729 tree basetype = NULL_TREE;
7730 tree access_binfo, binfo;
7731 tree optype;
7732 tree first_mem_arg = NULL_TREE;
7733 tree name;
7734 bool skip_first_for_error;
7735 vec<tree, va_gc> *user_args;
7736 tree call;
7737 tree fn;
7738 int template_only = 0;
7739 bool any_viable_p;
7740 tree orig_instance;
7741 tree orig_fns;
7742 vec<tree, va_gc> *orig_args = NULL;
7743 void *p;
7745 gcc_assert (instance != NULL_TREE);
7747 /* We don't know what function we're going to call, yet. */
7748 if (fn_p)
7749 *fn_p = NULL_TREE;
7751 if (error_operand_p (instance)
7752 || !fns || error_operand_p (fns))
7753 return error_mark_node;
7755 if (!BASELINK_P (fns))
7757 if (complain & tf_error)
7758 error ("call to non-function %qD", fns);
7759 return error_mark_node;
7762 orig_instance = instance;
7763 orig_fns = fns;
7765 /* Dismantle the baselink to collect all the information we need. */
7766 if (!conversion_path)
7767 conversion_path = BASELINK_BINFO (fns);
7768 access_binfo = BASELINK_ACCESS_BINFO (fns);
7769 binfo = BASELINK_BINFO (fns);
7770 optype = BASELINK_OPTYPE (fns);
7771 fns = BASELINK_FUNCTIONS (fns);
7772 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7774 explicit_targs = TREE_OPERAND (fns, 1);
7775 fns = TREE_OPERAND (fns, 0);
7776 template_only = 1;
7778 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
7779 || TREE_CODE (fns) == TEMPLATE_DECL
7780 || TREE_CODE (fns) == OVERLOAD);
7781 fn = get_first_fn (fns);
7782 name = DECL_NAME (fn);
7784 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
7785 gcc_assert (CLASS_TYPE_P (basetype));
7787 if (processing_template_decl)
7789 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
7790 instance = build_non_dependent_expr (instance);
7791 if (args != NULL)
7792 make_args_non_dependent (*args);
7795 user_args = args == NULL ? NULL : *args;
7796 /* Under DR 147 A::A() is an invalid constructor call,
7797 not a functional cast. */
7798 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
7800 if (! (complain & tf_error))
7801 return error_mark_node;
7803 if (permerror (input_location,
7804 "cannot call constructor %<%T::%D%> directly",
7805 basetype, name))
7806 inform (input_location, "for a function-style cast, remove the "
7807 "redundant %<::%D%>", name);
7808 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
7809 complain);
7810 return call;
7813 /* Figure out whether to skip the first argument for the error
7814 message we will display to users if an error occurs. We don't
7815 want to display any compiler-generated arguments. The "this"
7816 pointer hasn't been added yet. However, we must remove the VTT
7817 pointer if this is a call to a base-class constructor or
7818 destructor. */
7819 skip_first_for_error = false;
7820 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
7822 /* Callers should explicitly indicate whether they want to construct
7823 the complete object or just the part without virtual bases. */
7824 gcc_assert (name != ctor_identifier);
7825 /* Similarly for destructors. */
7826 gcc_assert (name != dtor_identifier);
7827 /* Remove the VTT pointer, if present. */
7828 if ((name == base_ctor_identifier || name == base_dtor_identifier)
7829 && CLASSTYPE_VBASECLASSES (basetype))
7830 skip_first_for_error = true;
7833 /* Process the argument list. */
7834 if (args != NULL && *args != NULL)
7836 *args = resolve_args (*args, complain);
7837 if (*args == NULL)
7838 return error_mark_node;
7841 /* Consider the object argument to be used even if we end up selecting a
7842 static member function. */
7843 instance = mark_type_use (instance);
7845 /* It's OK to call destructors and constructors on cv-qualified objects.
7846 Therefore, convert the INSTANCE to the unqualified type, if
7847 necessary. */
7848 if (DECL_DESTRUCTOR_P (fn)
7849 || DECL_CONSTRUCTOR_P (fn))
7851 if (!same_type_p (basetype, TREE_TYPE (instance)))
7853 instance = build_this (instance);
7854 instance = build_nop (build_pointer_type (basetype), instance);
7855 instance = build_fold_indirect_ref (instance);
7858 if (DECL_DESTRUCTOR_P (fn))
7859 name = complete_dtor_identifier;
7861 /* For the overload resolution we need to find the actual `this`
7862 that would be captured if the call turns out to be to a
7863 non-static member function. Do not actually capture it at this
7864 point. */
7865 first_mem_arg = maybe_resolve_dummy (instance, false);
7867 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7868 p = conversion_obstack_alloc (0);
7870 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
7871 initializer, not T({ }). */
7872 if (DECL_CONSTRUCTOR_P (fn) && args != NULL && !vec_safe_is_empty (*args)
7873 && DIRECT_LIST_INIT_P ((**args)[0]))
7875 tree init_list = (**args)[0];
7876 tree init = NULL_TREE;
7878 gcc_assert ((*args)->length () == 1
7879 && !(flags & LOOKUP_ONLYCONVERTING));
7881 /* If the initializer list has no elements and T is a class type with
7882 a default constructor, the object is value-initialized. Handle
7883 this here so we don't need to handle it wherever we use
7884 build_special_member_call. */
7885 if (CONSTRUCTOR_NELTS (init_list) == 0
7886 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
7887 /* For a user-provided default constructor, use the normal
7888 mechanisms so that protected access works. */
7889 && !type_has_user_provided_default_constructor (basetype)
7890 && !processing_template_decl)
7891 init = build_value_init (basetype, complain);
7893 /* If BASETYPE is an aggregate, we need to do aggregate
7894 initialization. */
7895 else if (CP_AGGREGATE_TYPE_P (basetype))
7896 init = digest_init (basetype, init_list, complain);
7898 if (init)
7900 if (is_dummy_object (instance))
7901 return get_target_expr_sfinae (init, complain);
7902 init = build2 (INIT_EXPR, TREE_TYPE (instance), instance, init);
7903 TREE_SIDE_EFFECTS (init) = true;
7904 return init;
7907 /* Otherwise go ahead with overload resolution. */
7908 add_list_candidates (fns, first_mem_arg, init_list,
7909 basetype, explicit_targs, template_only,
7910 conversion_path, access_binfo, flags,
7911 &candidates, complain);
7913 else
7915 add_candidates (fns, first_mem_arg, user_args, optype,
7916 explicit_targs, template_only, conversion_path,
7917 access_binfo, flags, &candidates, complain);
7919 any_viable_p = false;
7920 candidates = splice_viable (candidates, false, &any_viable_p);
7922 if (!any_viable_p)
7924 if (complain & tf_error)
7926 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
7927 cxx_incomplete_type_error (instance, basetype);
7928 else if (optype)
7929 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
7930 basetype, optype, build_tree_list_vec (user_args),
7931 TREE_TYPE (instance));
7932 else
7934 char *pretty_name;
7935 bool free_p;
7936 tree arglist;
7938 pretty_name = name_as_c_string (name, basetype, &free_p);
7939 arglist = build_tree_list_vec (user_args);
7940 if (skip_first_for_error)
7941 arglist = TREE_CHAIN (arglist);
7942 error ("no matching function for call to %<%T::%s(%A)%#V%>",
7943 basetype, pretty_name, arglist,
7944 TREE_TYPE (instance));
7945 if (free_p)
7946 free (pretty_name);
7948 print_z_candidates (location_of (name), candidates);
7950 call = error_mark_node;
7952 else
7954 cand = tourney (candidates, complain);
7955 if (cand == 0)
7957 char *pretty_name;
7958 bool free_p;
7959 tree arglist;
7961 if (complain & tf_error)
7963 pretty_name = name_as_c_string (name, basetype, &free_p);
7964 arglist = build_tree_list_vec (user_args);
7965 if (skip_first_for_error)
7966 arglist = TREE_CHAIN (arglist);
7967 if (!any_strictly_viable (candidates))
7968 error ("no matching function for call to %<%s(%A)%>",
7969 pretty_name, arglist);
7970 else
7971 error ("call of overloaded %<%s(%A)%> is ambiguous",
7972 pretty_name, arglist);
7973 print_z_candidates (location_of (name), candidates);
7974 if (free_p)
7975 free (pretty_name);
7977 call = error_mark_node;
7979 else
7981 fn = cand->fn;
7982 call = NULL_TREE;
7984 if (!(flags & LOOKUP_NONVIRTUAL)
7985 && DECL_PURE_VIRTUAL_P (fn)
7986 && instance == current_class_ref
7987 && (complain & tf_warning))
7989 /* This is not an error, it is runtime undefined
7990 behavior. */
7991 if (!current_function_decl)
7992 warning (0, "pure virtual %q#D called from "
7993 "non-static data member initializer", fn);
7994 else if (DECL_CONSTRUCTOR_P (current_function_decl)
7995 || DECL_DESTRUCTOR_P (current_function_decl))
7996 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
7997 ? "pure virtual %q#D called from constructor"
7998 : "pure virtual %q#D called from destructor"),
7999 fn);
8002 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
8003 && !DECL_CONSTRUCTOR_P (fn)
8004 && is_dummy_object (instance))
8006 instance = maybe_resolve_dummy (instance, true);
8007 if (instance == error_mark_node)
8008 call = error_mark_node;
8009 else if (!is_dummy_object (instance))
8011 /* We captured 'this' in the current lambda now that
8012 we know we really need it. */
8013 cand->first_arg = instance;
8015 else
8017 if (complain & tf_error)
8018 error ("cannot call member function %qD without object",
8019 fn);
8020 call = error_mark_node;
8024 if (call != error_mark_node)
8026 /* Optimize away vtable lookup if we know that this
8027 function can't be overridden. We need to check if
8028 the context and the type where we found fn are the same,
8029 actually FN might be defined in a different class
8030 type because of a using-declaration. In this case, we
8031 do not want to perform a non-virtual call. */
8032 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
8033 && same_type_ignoring_top_level_qualifiers_p
8034 (DECL_CONTEXT (fn), BINFO_TYPE (binfo))
8035 && resolves_to_fixed_type_p (instance, 0))
8036 flags |= LOOKUP_NONVIRTUAL;
8037 if (explicit_targs)
8038 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
8039 /* Now we know what function is being called. */
8040 if (fn_p)
8041 *fn_p = fn;
8042 /* Build the actual CALL_EXPR. */
8043 call = build_over_call (cand, flags, complain);
8044 /* In an expression of the form `a->f()' where `f' turns
8045 out to be a static member function, `a' is
8046 none-the-less evaluated. */
8047 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
8048 && !is_dummy_object (instance)
8049 && TREE_SIDE_EFFECTS (instance))
8050 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
8051 instance, call);
8052 else if (call != error_mark_node
8053 && DECL_DESTRUCTOR_P (cand->fn)
8054 && !VOID_TYPE_P (TREE_TYPE (call)))
8055 /* An explicit call of the form "x->~X()" has type
8056 "void". However, on platforms where destructors
8057 return "this" (i.e., those where
8058 targetm.cxx.cdtor_returns_this is true), such calls
8059 will appear to have a return value of pointer type
8060 to the low-level call machinery. We do not want to
8061 change the low-level machinery, since we want to be
8062 able to optimize "delete f()" on such platforms as
8063 "operator delete(~X(f()))" (rather than generating
8064 "t = f(), ~X(t), operator delete (t)"). */
8065 call = build_nop (void_type_node, call);
8070 if (processing_template_decl && call != error_mark_node)
8072 bool cast_to_void = false;
8074 if (TREE_CODE (call) == COMPOUND_EXPR)
8075 call = TREE_OPERAND (call, 1);
8076 else if (TREE_CODE (call) == NOP_EXPR)
8078 cast_to_void = true;
8079 call = TREE_OPERAND (call, 0);
8081 if (INDIRECT_REF_P (call))
8082 call = TREE_OPERAND (call, 0);
8083 call = (build_min_non_dep_call_vec
8084 (call,
8085 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
8086 orig_instance, orig_fns, NULL_TREE),
8087 orig_args));
8088 SET_EXPR_LOCATION (call, input_location);
8089 call = convert_from_reference (call);
8090 if (cast_to_void)
8091 call = build_nop (void_type_node, call);
8094 /* Free all the conversions we allocated. */
8095 obstack_free (&conversion_obstack, p);
8097 if (orig_args != NULL)
8098 release_tree_vector (orig_args);
8100 return call;
8103 /* Wrapper for above. */
8105 tree
8106 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
8107 tree conversion_path, int flags,
8108 tree *fn_p, tsubst_flags_t complain)
8110 tree ret;
8111 bool subtime = timevar_cond_start (TV_OVERLOAD);
8112 ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
8113 fn_p, complain);
8114 timevar_cond_stop (TV_OVERLOAD, subtime);
8115 return ret;
8118 /* Returns true iff standard conversion sequence ICS1 is a proper
8119 subsequence of ICS2. */
8121 static bool
8122 is_subseq (conversion *ics1, conversion *ics2)
8124 /* We can assume that a conversion of the same code
8125 between the same types indicates a subsequence since we only get
8126 here if the types we are converting from are the same. */
8128 while (ics1->kind == ck_rvalue
8129 || ics1->kind == ck_lvalue)
8130 ics1 = next_conversion (ics1);
8132 while (1)
8134 while (ics2->kind == ck_rvalue
8135 || ics2->kind == ck_lvalue)
8136 ics2 = next_conversion (ics2);
8138 if (ics2->kind == ck_user
8139 || ics2->kind == ck_ambig
8140 || ics2->kind == ck_aggr
8141 || ics2->kind == ck_list
8142 || ics2->kind == ck_identity)
8143 /* At this point, ICS1 cannot be a proper subsequence of
8144 ICS2. We can get a USER_CONV when we are comparing the
8145 second standard conversion sequence of two user conversion
8146 sequences. */
8147 return false;
8149 ics2 = next_conversion (ics2);
8151 if (ics2->kind == ics1->kind
8152 && same_type_p (ics2->type, ics1->type)
8153 && same_type_p (next_conversion (ics2)->type,
8154 next_conversion (ics1)->type))
8155 return true;
8159 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
8160 be any _TYPE nodes. */
8162 bool
8163 is_properly_derived_from (tree derived, tree base)
8165 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
8166 return false;
8168 /* We only allow proper derivation here. The DERIVED_FROM_P macro
8169 considers every class derived from itself. */
8170 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
8171 && DERIVED_FROM_P (base, derived));
8174 /* We build the ICS for an implicit object parameter as a pointer
8175 conversion sequence. However, such a sequence should be compared
8176 as if it were a reference conversion sequence. If ICS is the
8177 implicit conversion sequence for an implicit object parameter,
8178 modify it accordingly. */
8180 static void
8181 maybe_handle_implicit_object (conversion **ics)
8183 if ((*ics)->this_p)
8185 /* [over.match.funcs]
8187 For non-static member functions, the type of the
8188 implicit object parameter is "reference to cv X"
8189 where X is the class of which the function is a
8190 member and cv is the cv-qualification on the member
8191 function declaration. */
8192 conversion *t = *ics;
8193 tree reference_type;
8195 /* The `this' parameter is a pointer to a class type. Make the
8196 implicit conversion talk about a reference to that same class
8197 type. */
8198 reference_type = TREE_TYPE (t->type);
8199 reference_type = build_reference_type (reference_type);
8201 if (t->kind == ck_qual)
8202 t = next_conversion (t);
8203 if (t->kind == ck_ptr)
8204 t = next_conversion (t);
8205 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
8206 t = direct_reference_binding (reference_type, t);
8207 t->this_p = 1;
8208 t->rvaluedness_matches_p = 0;
8209 *ics = t;
8213 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
8214 and return the initial reference binding conversion. Otherwise,
8215 leave *ICS unchanged and return NULL. */
8217 static conversion *
8218 maybe_handle_ref_bind (conversion **ics)
8220 if ((*ics)->kind == ck_ref_bind)
8222 conversion *old_ics = *ics;
8223 *ics = next_conversion (old_ics);
8224 (*ics)->user_conv_p = old_ics->user_conv_p;
8225 return old_ics;
8228 return NULL;
8231 /* Compare two implicit conversion sequences according to the rules set out in
8232 [over.ics.rank]. Return values:
8234 1: ics1 is better than ics2
8235 -1: ics2 is better than ics1
8236 0: ics1 and ics2 are indistinguishable */
8238 static int
8239 compare_ics (conversion *ics1, conversion *ics2)
8241 tree from_type1;
8242 tree from_type2;
8243 tree to_type1;
8244 tree to_type2;
8245 tree deref_from_type1 = NULL_TREE;
8246 tree deref_from_type2 = NULL_TREE;
8247 tree deref_to_type1 = NULL_TREE;
8248 tree deref_to_type2 = NULL_TREE;
8249 conversion_rank rank1, rank2;
8251 /* REF_BINDING is nonzero if the result of the conversion sequence
8252 is a reference type. In that case REF_CONV is the reference
8253 binding conversion. */
8254 conversion *ref_conv1;
8255 conversion *ref_conv2;
8257 /* Compare badness before stripping the reference conversion. */
8258 if (ics1->bad_p > ics2->bad_p)
8259 return -1;
8260 else if (ics1->bad_p < ics2->bad_p)
8261 return 1;
8263 /* Handle implicit object parameters. */
8264 maybe_handle_implicit_object (&ics1);
8265 maybe_handle_implicit_object (&ics2);
8267 /* Handle reference parameters. */
8268 ref_conv1 = maybe_handle_ref_bind (&ics1);
8269 ref_conv2 = maybe_handle_ref_bind (&ics2);
8271 /* List-initialization sequence L1 is a better conversion sequence than
8272 list-initialization sequence L2 if L1 converts to
8273 std::initializer_list<X> for some X and L2 does not. */
8274 if (ics1->kind == ck_list && ics2->kind != ck_list)
8275 return 1;
8276 if (ics2->kind == ck_list && ics1->kind != ck_list)
8277 return -1;
8279 /* [over.ics.rank]
8281 When comparing the basic forms of implicit conversion sequences (as
8282 defined in _over.best.ics_)
8284 --a standard conversion sequence (_over.ics.scs_) is a better
8285 conversion sequence than a user-defined conversion sequence
8286 or an ellipsis conversion sequence, and
8288 --a user-defined conversion sequence (_over.ics.user_) is a
8289 better conversion sequence than an ellipsis conversion sequence
8290 (_over.ics.ellipsis_). */
8291 /* Use BAD_CONVERSION_RANK because we already checked for a badness
8292 mismatch. If both ICS are bad, we try to make a decision based on
8293 what would have happened if they'd been good. This is not an
8294 extension, we'll still give an error when we build up the call; this
8295 just helps us give a more helpful error message. */
8296 rank1 = BAD_CONVERSION_RANK (ics1);
8297 rank2 = BAD_CONVERSION_RANK (ics2);
8299 if (rank1 > rank2)
8300 return -1;
8301 else if (rank1 < rank2)
8302 return 1;
8304 if (ics1->ellipsis_p)
8305 /* Both conversions are ellipsis conversions. */
8306 return 0;
8308 /* User-defined conversion sequence U1 is a better conversion sequence
8309 than another user-defined conversion sequence U2 if they contain the
8310 same user-defined conversion operator or constructor and if the sec-
8311 ond standard conversion sequence of U1 is better than the second
8312 standard conversion sequence of U2. */
8314 /* Handle list-conversion with the same code even though it isn't always
8315 ranked as a user-defined conversion and it doesn't have a second
8316 standard conversion sequence; it will still have the desired effect.
8317 Specifically, we need to do the reference binding comparison at the
8318 end of this function. */
8320 if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
8322 conversion *t1;
8323 conversion *t2;
8325 for (t1 = ics1; t1->kind != ck_user; t1 = next_conversion (t1))
8326 if (t1->kind == ck_ambig || t1->kind == ck_aggr
8327 || t1->kind == ck_list)
8328 break;
8329 for (t2 = ics2; t2->kind != ck_user; t2 = next_conversion (t2))
8330 if (t2->kind == ck_ambig || t2->kind == ck_aggr
8331 || t2->kind == ck_list)
8332 break;
8334 if (t1->kind != t2->kind)
8335 return 0;
8336 else if (t1->kind == ck_user)
8338 if (t1->cand->fn != t2->cand->fn)
8339 return 0;
8341 else
8343 /* For ambiguous or aggregate conversions, use the target type as
8344 a proxy for the conversion function. */
8345 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
8346 return 0;
8349 /* We can just fall through here, after setting up
8350 FROM_TYPE1 and FROM_TYPE2. */
8351 from_type1 = t1->type;
8352 from_type2 = t2->type;
8354 else
8356 conversion *t1;
8357 conversion *t2;
8359 /* We're dealing with two standard conversion sequences.
8361 [over.ics.rank]
8363 Standard conversion sequence S1 is a better conversion
8364 sequence than standard conversion sequence S2 if
8366 --S1 is a proper subsequence of S2 (comparing the conversion
8367 sequences in the canonical form defined by _over.ics.scs_,
8368 excluding any Lvalue Transformation; the identity
8369 conversion sequence is considered to be a subsequence of
8370 any non-identity conversion sequence */
8372 t1 = ics1;
8373 while (t1->kind != ck_identity)
8374 t1 = next_conversion (t1);
8375 from_type1 = t1->type;
8377 t2 = ics2;
8378 while (t2->kind != ck_identity)
8379 t2 = next_conversion (t2);
8380 from_type2 = t2->type;
8383 /* One sequence can only be a subsequence of the other if they start with
8384 the same type. They can start with different types when comparing the
8385 second standard conversion sequence in two user-defined conversion
8386 sequences. */
8387 if (same_type_p (from_type1, from_type2))
8389 if (is_subseq (ics1, ics2))
8390 return 1;
8391 if (is_subseq (ics2, ics1))
8392 return -1;
8395 /* [over.ics.rank]
8397 Or, if not that,
8399 --the rank of S1 is better than the rank of S2 (by the rules
8400 defined below):
8402 Standard conversion sequences are ordered by their ranks: an Exact
8403 Match is a better conversion than a Promotion, which is a better
8404 conversion than a Conversion.
8406 Two conversion sequences with the same rank are indistinguishable
8407 unless one of the following rules applies:
8409 --A conversion that does not a convert a pointer, pointer to member,
8410 or std::nullptr_t to bool is better than one that does.
8412 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
8413 so that we do not have to check it explicitly. */
8414 if (ics1->rank < ics2->rank)
8415 return 1;
8416 else if (ics2->rank < ics1->rank)
8417 return -1;
8419 to_type1 = ics1->type;
8420 to_type2 = ics2->type;
8422 /* A conversion from scalar arithmetic type to complex is worse than a
8423 conversion between scalar arithmetic types. */
8424 if (same_type_p (from_type1, from_type2)
8425 && ARITHMETIC_TYPE_P (from_type1)
8426 && ARITHMETIC_TYPE_P (to_type1)
8427 && ARITHMETIC_TYPE_P (to_type2)
8428 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
8429 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
8431 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
8432 return -1;
8433 else
8434 return 1;
8437 if (TYPE_PTR_P (from_type1)
8438 && TYPE_PTR_P (from_type2)
8439 && TYPE_PTR_P (to_type1)
8440 && TYPE_PTR_P (to_type2))
8442 deref_from_type1 = TREE_TYPE (from_type1);
8443 deref_from_type2 = TREE_TYPE (from_type2);
8444 deref_to_type1 = TREE_TYPE (to_type1);
8445 deref_to_type2 = TREE_TYPE (to_type2);
8447 /* The rules for pointers to members A::* are just like the rules
8448 for pointers A*, except opposite: if B is derived from A then
8449 A::* converts to B::*, not vice versa. For that reason, we
8450 switch the from_ and to_ variables here. */
8451 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
8452 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
8453 || (TYPE_PTRMEMFUNC_P (from_type1)
8454 && TYPE_PTRMEMFUNC_P (from_type2)
8455 && TYPE_PTRMEMFUNC_P (to_type1)
8456 && TYPE_PTRMEMFUNC_P (to_type2)))
8458 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
8459 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
8460 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
8461 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
8464 if (deref_from_type1 != NULL_TREE
8465 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
8466 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
8468 /* This was one of the pointer or pointer-like conversions.
8470 [over.ics.rank]
8472 --If class B is derived directly or indirectly from class A,
8473 conversion of B* to A* is better than conversion of B* to
8474 void*, and conversion of A* to void* is better than
8475 conversion of B* to void*. */
8476 if (VOID_TYPE_P (deref_to_type1)
8477 && VOID_TYPE_P (deref_to_type2))
8479 if (is_properly_derived_from (deref_from_type1,
8480 deref_from_type2))
8481 return -1;
8482 else if (is_properly_derived_from (deref_from_type2,
8483 deref_from_type1))
8484 return 1;
8486 else if (VOID_TYPE_P (deref_to_type1)
8487 || VOID_TYPE_P (deref_to_type2))
8489 if (same_type_p (deref_from_type1, deref_from_type2))
8491 if (VOID_TYPE_P (deref_to_type2))
8493 if (is_properly_derived_from (deref_from_type1,
8494 deref_to_type1))
8495 return 1;
8497 /* We know that DEREF_TO_TYPE1 is `void' here. */
8498 else if (is_properly_derived_from (deref_from_type1,
8499 deref_to_type2))
8500 return -1;
8503 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
8504 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
8506 /* [over.ics.rank]
8508 --If class B is derived directly or indirectly from class A
8509 and class C is derived directly or indirectly from B,
8511 --conversion of C* to B* is better than conversion of C* to
8514 --conversion of B* to A* is better than conversion of C* to
8515 A* */
8516 if (same_type_p (deref_from_type1, deref_from_type2))
8518 if (is_properly_derived_from (deref_to_type1,
8519 deref_to_type2))
8520 return 1;
8521 else if (is_properly_derived_from (deref_to_type2,
8522 deref_to_type1))
8523 return -1;
8525 else if (same_type_p (deref_to_type1, deref_to_type2))
8527 if (is_properly_derived_from (deref_from_type2,
8528 deref_from_type1))
8529 return 1;
8530 else if (is_properly_derived_from (deref_from_type1,
8531 deref_from_type2))
8532 return -1;
8536 else if (CLASS_TYPE_P (non_reference (from_type1))
8537 && same_type_p (from_type1, from_type2))
8539 tree from = non_reference (from_type1);
8541 /* [over.ics.rank]
8543 --binding of an expression of type C to a reference of type
8544 B& is better than binding an expression of type C to a
8545 reference of type A&
8547 --conversion of C to B is better than conversion of C to A, */
8548 if (is_properly_derived_from (from, to_type1)
8549 && is_properly_derived_from (from, to_type2))
8551 if (is_properly_derived_from (to_type1, to_type2))
8552 return 1;
8553 else if (is_properly_derived_from (to_type2, to_type1))
8554 return -1;
8557 else if (CLASS_TYPE_P (non_reference (to_type1))
8558 && same_type_p (to_type1, to_type2))
8560 tree to = non_reference (to_type1);
8562 /* [over.ics.rank]
8564 --binding of an expression of type B to a reference of type
8565 A& is better than binding an expression of type C to a
8566 reference of type A&,
8568 --conversion of B to A is better than conversion of C to A */
8569 if (is_properly_derived_from (from_type1, to)
8570 && is_properly_derived_from (from_type2, to))
8572 if (is_properly_derived_from (from_type2, from_type1))
8573 return 1;
8574 else if (is_properly_derived_from (from_type1, from_type2))
8575 return -1;
8579 /* [over.ics.rank]
8581 --S1 and S2 differ only in their qualification conversion and yield
8582 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
8583 qualification signature of type T1 is a proper subset of the cv-
8584 qualification signature of type T2 */
8585 if (ics1->kind == ck_qual
8586 && ics2->kind == ck_qual
8587 && same_type_p (from_type1, from_type2))
8589 int result = comp_cv_qual_signature (to_type1, to_type2);
8590 if (result != 0)
8591 return result;
8594 /* [over.ics.rank]
8596 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
8597 to an implicit object parameter, and either S1 binds an lvalue reference
8598 to an lvalue and S2 binds an rvalue reference or S1 binds an rvalue
8599 reference to an rvalue and S2 binds an lvalue reference
8600 (C++0x draft standard, 13.3.3.2)
8602 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
8603 types to which the references refer are the same type except for
8604 top-level cv-qualifiers, and the type to which the reference
8605 initialized by S2 refers is more cv-qualified than the type to
8606 which the reference initialized by S1 refers.
8608 DR 1328 [over.match.best]: the context is an initialization by
8609 conversion function for direct reference binding (13.3.1.6) of a
8610 reference to function type, the return type of F1 is the same kind of
8611 reference (i.e. lvalue or rvalue) as the reference being initialized,
8612 and the return type of F2 is not. */
8614 if (ref_conv1 && ref_conv2)
8616 if (!ref_conv1->this_p && !ref_conv2->this_p
8617 && (ref_conv1->rvaluedness_matches_p
8618 != ref_conv2->rvaluedness_matches_p)
8619 && (same_type_p (ref_conv1->type, ref_conv2->type)
8620 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
8621 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
8623 if (ref_conv1->bad_p
8624 && !same_type_p (TREE_TYPE (ref_conv1->type),
8625 TREE_TYPE (ref_conv2->type)))
8626 /* Don't prefer a bad conversion that drops cv-quals to a bad
8627 conversion with the wrong rvalueness. */
8628 return 0;
8629 return (ref_conv1->rvaluedness_matches_p
8630 - ref_conv2->rvaluedness_matches_p);
8633 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
8635 int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
8636 int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
8637 if (ref_conv1->bad_p)
8639 /* Prefer the one that drops fewer cv-quals. */
8640 tree ftype = next_conversion (ref_conv1)->type;
8641 int fquals = cp_type_quals (ftype);
8642 q1 ^= fquals;
8643 q2 ^= fquals;
8645 return comp_cv_qualification (q2, q1);
8649 /* Neither conversion sequence is better than the other. */
8650 return 0;
8653 /* The source type for this standard conversion sequence. */
8655 static tree
8656 source_type (conversion *t)
8658 for (;; t = next_conversion (t))
8660 if (t->kind == ck_user
8661 || t->kind == ck_ambig
8662 || t->kind == ck_identity)
8663 return t->type;
8665 gcc_unreachable ();
8668 /* Note a warning about preferring WINNER to LOSER. We do this by storing
8669 a pointer to LOSER and re-running joust to produce the warning if WINNER
8670 is actually used. */
8672 static void
8673 add_warning (struct z_candidate *winner, struct z_candidate *loser)
8675 candidate_warning *cw = (candidate_warning *)
8676 conversion_obstack_alloc (sizeof (candidate_warning));
8677 cw->loser = loser;
8678 cw->next = winner->warnings;
8679 winner->warnings = cw;
8682 /* Compare two candidates for overloading as described in
8683 [over.match.best]. Return values:
8685 1: cand1 is better than cand2
8686 -1: cand2 is better than cand1
8687 0: cand1 and cand2 are indistinguishable */
8689 static int
8690 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
8691 tsubst_flags_t complain)
8693 int winner = 0;
8694 int off1 = 0, off2 = 0;
8695 size_t i;
8696 size_t len;
8698 /* Candidates that involve bad conversions are always worse than those
8699 that don't. */
8700 if (cand1->viable > cand2->viable)
8701 return 1;
8702 if (cand1->viable < cand2->viable)
8703 return -1;
8705 /* If we have two pseudo-candidates for conversions to the same type,
8706 or two candidates for the same function, arbitrarily pick one. */
8707 if (cand1->fn == cand2->fn
8708 && (IS_TYPE_OR_DECL_P (cand1->fn)))
8709 return 1;
8711 /* Prefer a non-deleted function over an implicitly deleted move
8712 constructor or assignment operator. This differs slightly from the
8713 wording for issue 1402 (which says the move op is ignored by overload
8714 resolution), but this way produces better error messages. */
8715 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
8716 && TREE_CODE (cand2->fn) == FUNCTION_DECL
8717 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
8719 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
8720 && move_fn_p (cand1->fn))
8721 return -1;
8722 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
8723 && move_fn_p (cand2->fn))
8724 return 1;
8727 /* a viable function F1
8728 is defined to be a better function than another viable function F2 if
8729 for all arguments i, ICSi(F1) is not a worse conversion sequence than
8730 ICSi(F2), and then */
8732 /* for some argument j, ICSj(F1) is a better conversion sequence than
8733 ICSj(F2) */
8735 /* For comparing static and non-static member functions, we ignore
8736 the implicit object parameter of the non-static function. The
8737 standard says to pretend that the static function has an object
8738 parm, but that won't work with operator overloading. */
8739 len = cand1->num_convs;
8740 if (len != cand2->num_convs)
8742 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
8743 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
8745 if (DECL_CONSTRUCTOR_P (cand1->fn)
8746 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
8747 /* We're comparing a near-match list constructor and a near-match
8748 non-list constructor. Just treat them as unordered. */
8749 return 0;
8751 gcc_assert (static_1 != static_2);
8753 if (static_1)
8754 off2 = 1;
8755 else
8757 off1 = 1;
8758 --len;
8762 for (i = 0; i < len; ++i)
8764 conversion *t1 = cand1->convs[i + off1];
8765 conversion *t2 = cand2->convs[i + off2];
8766 int comp = compare_ics (t1, t2);
8768 if (comp != 0)
8770 if ((complain & tf_warning)
8771 && warn_sign_promo
8772 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
8773 == cr_std + cr_promotion)
8774 && t1->kind == ck_std
8775 && t2->kind == ck_std
8776 && TREE_CODE (t1->type) == INTEGER_TYPE
8777 && TREE_CODE (t2->type) == INTEGER_TYPE
8778 && (TYPE_PRECISION (t1->type)
8779 == TYPE_PRECISION (t2->type))
8780 && (TYPE_UNSIGNED (next_conversion (t1)->type)
8781 || (TREE_CODE (next_conversion (t1)->type)
8782 == ENUMERAL_TYPE)))
8784 tree type = next_conversion (t1)->type;
8785 tree type1, type2;
8786 struct z_candidate *w, *l;
8787 if (comp > 0)
8788 type1 = t1->type, type2 = t2->type,
8789 w = cand1, l = cand2;
8790 else
8791 type1 = t2->type, type2 = t1->type,
8792 w = cand2, l = cand1;
8794 if (warn)
8796 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
8797 type, type1, type2);
8798 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
8800 else
8801 add_warning (w, l);
8804 if (winner && comp != winner)
8806 winner = 0;
8807 goto tweak;
8809 winner = comp;
8813 /* warn about confusing overload resolution for user-defined conversions,
8814 either between a constructor and a conversion op, or between two
8815 conversion ops. */
8816 if ((complain & tf_warning)
8817 && winner && warn_conversion && cand1->second_conv
8818 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
8819 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
8821 struct z_candidate *w, *l;
8822 bool give_warning = false;
8824 if (winner == 1)
8825 w = cand1, l = cand2;
8826 else
8827 w = cand2, l = cand1;
8829 /* We don't want to complain about `X::operator T1 ()'
8830 beating `X::operator T2 () const', when T2 is a no less
8831 cv-qualified version of T1. */
8832 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
8833 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
8835 tree t = TREE_TYPE (TREE_TYPE (l->fn));
8836 tree f = TREE_TYPE (TREE_TYPE (w->fn));
8838 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
8840 t = TREE_TYPE (t);
8841 f = TREE_TYPE (f);
8843 if (!comp_ptr_ttypes (t, f))
8844 give_warning = true;
8846 else
8847 give_warning = true;
8849 if (!give_warning)
8850 /*NOP*/;
8851 else if (warn)
8853 tree source = source_type (w->convs[0]);
8854 if (! DECL_CONSTRUCTOR_P (w->fn))
8855 source = TREE_TYPE (source);
8856 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
8857 && warning (OPT_Wconversion, " for conversion from %qT to %qT",
8858 source, w->second_conv->type))
8860 inform (input_location, " because conversion sequence for the argument is better");
8863 else
8864 add_warning (w, l);
8867 if (winner)
8868 return winner;
8870 /* DR 495 moved this tiebreaker above the template ones. */
8871 /* or, if not that,
8872 the context is an initialization by user-defined conversion (see
8873 _dcl.init_ and _over.match.user_) and the standard conversion
8874 sequence from the return type of F1 to the destination type (i.e.,
8875 the type of the entity being initialized) is a better conversion
8876 sequence than the standard conversion sequence from the return type
8877 of F2 to the destination type. */
8879 if (cand1->second_conv)
8881 winner = compare_ics (cand1->second_conv, cand2->second_conv);
8882 if (winner)
8883 return winner;
8886 /* or, if not that,
8887 F1 is a non-template function and F2 is a template function
8888 specialization. */
8890 if (!cand1->template_decl && cand2->template_decl)
8891 return 1;
8892 else if (cand1->template_decl && !cand2->template_decl)
8893 return -1;
8895 /* or, if not that,
8896 F1 and F2 are template functions and the function template for F1 is
8897 more specialized than the template for F2 according to the partial
8898 ordering rules. */
8900 if (cand1->template_decl && cand2->template_decl)
8902 winner = more_specialized_fn
8903 (TI_TEMPLATE (cand1->template_decl),
8904 TI_TEMPLATE (cand2->template_decl),
8905 /* [temp.func.order]: The presence of unused ellipsis and default
8906 arguments has no effect on the partial ordering of function
8907 templates. add_function_candidate() will not have
8908 counted the "this" argument for constructors. */
8909 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
8910 if (winner)
8911 return winner;
8914 /* Check whether we can discard a builtin candidate, either because we
8915 have two identical ones or matching builtin and non-builtin candidates.
8917 (Pedantically in the latter case the builtin which matched the user
8918 function should not be added to the overload set, but we spot it here.
8920 [over.match.oper]
8921 ... the builtin candidates include ...
8922 - do not have the same parameter type list as any non-template
8923 non-member candidate. */
8925 if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
8927 for (i = 0; i < len; ++i)
8928 if (!same_type_p (cand1->convs[i]->type,
8929 cand2->convs[i]->type))
8930 break;
8931 if (i == cand1->num_convs)
8933 if (cand1->fn == cand2->fn)
8934 /* Two built-in candidates; arbitrarily pick one. */
8935 return 1;
8936 else if (identifier_p (cand1->fn))
8937 /* cand1 is built-in; prefer cand2. */
8938 return -1;
8939 else
8940 /* cand2 is built-in; prefer cand1. */
8941 return 1;
8945 /* For candidates of a multi-versioned function, make the version with
8946 the highest priority win. This version will be checked for dispatching
8947 first. If this version can be inlined into the caller, the front-end
8948 will simply make a direct call to this function. */
8950 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
8951 && DECL_FUNCTION_VERSIONED (cand1->fn)
8952 && TREE_CODE (cand2->fn) == FUNCTION_DECL
8953 && DECL_FUNCTION_VERSIONED (cand2->fn))
8955 tree f1 = TREE_TYPE (cand1->fn);
8956 tree f2 = TREE_TYPE (cand2->fn);
8957 tree p1 = TYPE_ARG_TYPES (f1);
8958 tree p2 = TYPE_ARG_TYPES (f2);
8960 /* Check if cand1->fn and cand2->fn are versions of the same function. It
8961 is possible that cand1->fn and cand2->fn are function versions but of
8962 different functions. Check types to see if they are versions of the same
8963 function. */
8964 if (compparms (p1, p2)
8965 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8967 /* Always make the version with the higher priority, more
8968 specialized, win. */
8969 gcc_assert (targetm.compare_version_priority);
8970 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
8971 return 1;
8972 else
8973 return -1;
8977 /* If the two function declarations represent the same function (this can
8978 happen with declarations in multiple scopes and arg-dependent lookup),
8979 arbitrarily choose one. But first make sure the default args we're
8980 using match. */
8981 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
8982 && equal_functions (cand1->fn, cand2->fn))
8984 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
8985 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
8987 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
8989 for (i = 0; i < len; ++i)
8991 /* Don't crash if the fn is variadic. */
8992 if (!parms1)
8993 break;
8994 parms1 = TREE_CHAIN (parms1);
8995 parms2 = TREE_CHAIN (parms2);
8998 if (off1)
8999 parms1 = TREE_CHAIN (parms1);
9000 else if (off2)
9001 parms2 = TREE_CHAIN (parms2);
9003 for (; parms1; ++i)
9005 if (!cp_tree_equal (TREE_PURPOSE (parms1),
9006 TREE_PURPOSE (parms2)))
9008 if (warn)
9010 if (complain & tf_error)
9012 if (permerror (input_location,
9013 "default argument mismatch in "
9014 "overload resolution"))
9016 inform (input_location,
9017 " candidate 1: %q+#F", cand1->fn);
9018 inform (input_location,
9019 " candidate 2: %q+#F", cand2->fn);
9022 else
9023 return 0;
9025 else
9026 add_warning (cand1, cand2);
9027 break;
9029 parms1 = TREE_CHAIN (parms1);
9030 parms2 = TREE_CHAIN (parms2);
9033 return 1;
9036 tweak:
9038 /* Extension: If the worst conversion for one candidate is worse than the
9039 worst conversion for the other, take the first. */
9040 if (!pedantic && (complain & tf_warning_or_error))
9042 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
9043 struct z_candidate *w = 0, *l = 0;
9045 for (i = 0; i < len; ++i)
9047 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
9048 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
9049 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
9050 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
9052 if (rank1 < rank2)
9053 winner = 1, w = cand1, l = cand2;
9054 if (rank1 > rank2)
9055 winner = -1, w = cand2, l = cand1;
9056 if (winner)
9058 /* Don't choose a deleted function over ambiguity. */
9059 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
9060 return 0;
9061 if (warn)
9063 pedwarn (input_location, 0,
9064 "ISO C++ says that these are ambiguous, even "
9065 "though the worst conversion for the first is better than "
9066 "the worst conversion for the second:");
9067 print_z_candidate (input_location, _("candidate 1:"), w);
9068 print_z_candidate (input_location, _("candidate 2:"), l);
9070 else
9071 add_warning (w, l);
9072 return winner;
9076 gcc_assert (!winner);
9077 return 0;
9080 /* Given a list of candidates for overloading, find the best one, if any.
9081 This algorithm has a worst case of O(2n) (winner is last), and a best
9082 case of O(n/2) (totally ambiguous); much better than a sorting
9083 algorithm. */
9085 static struct z_candidate *
9086 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
9088 struct z_candidate *champ = candidates, *challenger;
9089 int fate;
9090 int champ_compared_to_predecessor = 0;
9092 /* Walk through the list once, comparing each current champ to the next
9093 candidate, knocking out a candidate or two with each comparison. */
9095 for (challenger = champ->next; challenger; )
9097 fate = joust (champ, challenger, 0, complain);
9098 if (fate == 1)
9099 challenger = challenger->next;
9100 else
9102 if (fate == 0)
9104 champ = challenger->next;
9105 if (champ == 0)
9106 return NULL;
9107 champ_compared_to_predecessor = 0;
9109 else
9111 champ = challenger;
9112 champ_compared_to_predecessor = 1;
9115 challenger = champ->next;
9119 /* Make sure the champ is better than all the candidates it hasn't yet
9120 been compared to. */
9122 for (challenger = candidates;
9123 challenger != champ
9124 && !(champ_compared_to_predecessor && challenger->next == champ);
9125 challenger = challenger->next)
9127 fate = joust (champ, challenger, 0, complain);
9128 if (fate != 1)
9129 return NULL;
9132 return champ;
9135 /* Returns nonzero if things of type FROM can be converted to TO. */
9137 bool
9138 can_convert (tree to, tree from, tsubst_flags_t complain)
9140 tree arg = NULL_TREE;
9141 /* implicit_conversion only considers user-defined conversions
9142 if it has an expression for the call argument list. */
9143 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
9144 arg = build1 (CAST_EXPR, from, NULL_TREE);
9145 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
9148 /* Returns nonzero if things of type FROM can be converted to TO with a
9149 standard conversion. */
9151 bool
9152 can_convert_standard (tree to, tree from, tsubst_flags_t complain)
9154 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
9157 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
9159 bool
9160 can_convert_arg (tree to, tree from, tree arg, int flags,
9161 tsubst_flags_t complain)
9163 conversion *t;
9164 void *p;
9165 bool ok_p;
9167 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9168 p = conversion_obstack_alloc (0);
9169 /* We want to discard any access checks done for this test,
9170 as we might not be in the appropriate access context and
9171 we'll do the check again when we actually perform the
9172 conversion. */
9173 push_deferring_access_checks (dk_deferred);
9175 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
9176 flags, complain);
9177 ok_p = (t && !t->bad_p);
9179 /* Discard the access checks now. */
9180 pop_deferring_access_checks ();
9181 /* Free all the conversions we allocated. */
9182 obstack_free (&conversion_obstack, p);
9184 return ok_p;
9187 /* Like can_convert_arg, but allows dubious conversions as well. */
9189 bool
9190 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
9191 tsubst_flags_t complain)
9193 conversion *t;
9194 void *p;
9196 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9197 p = conversion_obstack_alloc (0);
9198 /* Try to perform the conversion. */
9199 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
9200 flags, complain);
9201 /* Free all the conversions we allocated. */
9202 obstack_free (&conversion_obstack, p);
9204 return t != NULL;
9207 /* Convert EXPR to TYPE. Return the converted expression.
9209 Note that we allow bad conversions here because by the time we get to
9210 this point we are committed to doing the conversion. If we end up
9211 doing a bad conversion, convert_like will complain. */
9213 tree
9214 perform_implicit_conversion_flags (tree type, tree expr,
9215 tsubst_flags_t complain, int flags)
9217 conversion *conv;
9218 void *p;
9219 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
9221 if (error_operand_p (expr))
9222 return error_mark_node;
9224 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9225 p = conversion_obstack_alloc (0);
9227 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
9228 /*c_cast_p=*/false,
9229 flags, complain);
9231 if (!conv)
9233 if (complain & tf_error)
9235 /* If expr has unknown type, then it is an overloaded function.
9236 Call instantiate_type to get good error messages. */
9237 if (TREE_TYPE (expr) == unknown_type_node)
9238 instantiate_type (type, expr, complain);
9239 else if (invalid_nonstatic_memfn_p (expr, complain))
9240 /* We gave an error. */;
9241 else
9242 error_at (loc, "could not convert %qE from %qT to %qT", expr,
9243 TREE_TYPE (expr), type);
9245 expr = error_mark_node;
9247 else if (processing_template_decl && conv->kind != ck_identity)
9249 /* In a template, we are only concerned about determining the
9250 type of non-dependent expressions, so we do not have to
9251 perform the actual conversion. But for initializers, we
9252 need to be able to perform it at instantiation
9253 (or fold_non_dependent_expr) time. */
9254 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
9255 if (!(flags & LOOKUP_ONLYCONVERTING))
9256 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
9258 else
9259 expr = convert_like (conv, expr, complain);
9261 /* Free all the conversions we allocated. */
9262 obstack_free (&conversion_obstack, p);
9264 return expr;
9267 tree
9268 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
9270 return perform_implicit_conversion_flags (type, expr, complain,
9271 LOOKUP_IMPLICIT);
9274 /* Convert EXPR to TYPE (as a direct-initialization) if that is
9275 permitted. If the conversion is valid, the converted expression is
9276 returned. Otherwise, NULL_TREE is returned, except in the case
9277 that TYPE is a class type; in that case, an error is issued. If
9278 C_CAST_P is true, then this direct-initialization is taking
9279 place as part of a static_cast being attempted as part of a C-style
9280 cast. */
9282 tree
9283 perform_direct_initialization_if_possible (tree type,
9284 tree expr,
9285 bool c_cast_p,
9286 tsubst_flags_t complain)
9288 conversion *conv;
9289 void *p;
9291 if (type == error_mark_node || error_operand_p (expr))
9292 return error_mark_node;
9293 /* [dcl.init]
9295 If the destination type is a (possibly cv-qualified) class type:
9297 -- If the initialization is direct-initialization ...,
9298 constructors are considered. ... If no constructor applies, or
9299 the overload resolution is ambiguous, the initialization is
9300 ill-formed. */
9301 if (CLASS_TYPE_P (type))
9303 vec<tree, va_gc> *args = make_tree_vector_single (expr);
9304 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
9305 &args, type, LOOKUP_NORMAL, complain);
9306 release_tree_vector (args);
9307 return build_cplus_new (type, expr, complain);
9310 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9311 p = conversion_obstack_alloc (0);
9313 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
9314 c_cast_p,
9315 LOOKUP_NORMAL, complain);
9316 if (!conv || conv->bad_p)
9317 expr = NULL_TREE;
9318 else
9319 expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
9320 /*issue_conversion_warnings=*/false,
9321 c_cast_p,
9322 complain);
9324 /* Free all the conversions we allocated. */
9325 obstack_free (&conversion_obstack, p);
9327 return expr;
9330 /* When initializing a reference that lasts longer than a full-expression,
9331 this special rule applies:
9333 [class.temporary]
9335 The temporary to which the reference is bound or the temporary
9336 that is the complete object to which the reference is bound
9337 persists for the lifetime of the reference.
9339 The temporaries created during the evaluation of the expression
9340 initializing the reference, except the temporary to which the
9341 reference is bound, are destroyed at the end of the
9342 full-expression in which they are created.
9344 In that case, we store the converted expression into a new
9345 VAR_DECL in a new scope.
9347 However, we want to be careful not to create temporaries when
9348 they are not required. For example, given:
9350 struct B {};
9351 struct D : public B {};
9352 D f();
9353 const B& b = f();
9355 there is no need to copy the return value from "f"; we can just
9356 extend its lifetime. Similarly, given:
9358 struct S {};
9359 struct T { operator S(); };
9360 T t;
9361 const S& s = t;
9363 we can extend the lifetime of the return value of the conversion
9364 operator.
9366 The next several functions are involved in this lifetime extension. */
9368 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
9369 reference is being bound to a temporary. Create and return a new
9370 VAR_DECL with the indicated TYPE; this variable will store the value to
9371 which the reference is bound. */
9373 tree
9374 make_temporary_var_for_ref_to_temp (tree decl, tree type)
9376 tree var;
9378 /* Create the variable. */
9379 var = create_temporary_var (type);
9381 /* Register the variable. */
9382 if (VAR_P (decl)
9383 && (TREE_STATIC (decl) || DECL_THREAD_LOCAL_P (decl)))
9385 /* Namespace-scope or local static; give it a mangled name. */
9386 /* FIXME share comdat with decl? */
9387 tree name;
9389 TREE_STATIC (var) = TREE_STATIC (decl);
9390 set_decl_tls_model (var, DECL_TLS_MODEL (decl));
9391 name = mangle_ref_init_variable (decl);
9392 DECL_NAME (var) = name;
9393 SET_DECL_ASSEMBLER_NAME (var, name);
9394 var = pushdecl_top_level (var);
9396 else
9397 /* Create a new cleanup level if necessary. */
9398 maybe_push_cleanup_level (type);
9400 return var;
9403 /* EXPR is the initializer for a variable DECL of reference or
9404 std::initializer_list type. Create, push and return a new VAR_DECL
9405 for the initializer so that it will live as long as DECL. Any
9406 cleanup for the new variable is returned through CLEANUP, and the
9407 code to initialize the new variable is returned through INITP. */
9409 static tree
9410 set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
9411 tree *initp)
9413 tree init;
9414 tree type;
9415 tree var;
9417 /* Create the temporary variable. */
9418 type = TREE_TYPE (expr);
9419 var = make_temporary_var_for_ref_to_temp (decl, type);
9420 layout_decl (var, 0);
9421 /* If the rvalue is the result of a function call it will be
9422 a TARGET_EXPR. If it is some other construct (such as a
9423 member access expression where the underlying object is
9424 itself the result of a function call), turn it into a
9425 TARGET_EXPR here. It is important that EXPR be a
9426 TARGET_EXPR below since otherwise the INIT_EXPR will
9427 attempt to make a bitwise copy of EXPR to initialize
9428 VAR. */
9429 if (TREE_CODE (expr) != TARGET_EXPR)
9430 expr = get_target_expr (expr);
9432 if (TREE_CODE (decl) == FIELD_DECL
9433 && extra_warnings && !TREE_NO_WARNING (decl))
9435 warning (OPT_Wextra, "a temporary bound to %qD only persists "
9436 "until the constructor exits", decl);
9437 TREE_NO_WARNING (decl) = true;
9440 /* Recursively extend temps in this initializer. */
9441 TARGET_EXPR_INITIAL (expr)
9442 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups);
9444 /* Any reference temp has a non-trivial initializer. */
9445 DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
9447 /* If the initializer is constant, put it in DECL_INITIAL so we get
9448 static initialization and use in constant expressions. */
9449 init = maybe_constant_init (expr);
9450 if (TREE_CONSTANT (init))
9452 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
9454 /* 5.19 says that a constant expression can include an
9455 lvalue-rvalue conversion applied to "a glvalue of literal type
9456 that refers to a non-volatile temporary object initialized
9457 with a constant expression". Rather than try to communicate
9458 that this VAR_DECL is a temporary, just mark it constexpr.
9460 Currently this is only useful for initializer_list temporaries,
9461 since reference vars can't appear in constant expressions. */
9462 DECL_DECLARED_CONSTEXPR_P (var) = true;
9463 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
9464 TREE_CONSTANT (var) = true;
9466 DECL_INITIAL (var) = init;
9467 init = NULL_TREE;
9469 else
9470 /* Create the INIT_EXPR that will initialize the temporary
9471 variable. */
9472 init = build2 (INIT_EXPR, type, var, expr);
9473 if (at_function_scope_p ())
9475 add_decl_expr (var);
9477 if (TREE_STATIC (var))
9478 init = add_stmt_to_compound (init, register_dtor_fn (var));
9479 else
9481 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
9482 if (cleanup)
9483 vec_safe_push (*cleanups, cleanup);
9486 /* We must be careful to destroy the temporary only
9487 after its initialization has taken place. If the
9488 initialization throws an exception, then the
9489 destructor should not be run. We cannot simply
9490 transform INIT into something like:
9492 (INIT, ({ CLEANUP_STMT; }))
9494 because emit_local_var always treats the
9495 initializer as a full-expression. Thus, the
9496 destructor would run too early; it would run at the
9497 end of initializing the reference variable, rather
9498 than at the end of the block enclosing the
9499 reference variable.
9501 The solution is to pass back a cleanup expression
9502 which the caller is responsible for attaching to
9503 the statement tree. */
9505 else
9507 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
9508 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9510 if (DECL_THREAD_LOCAL_P (var))
9511 tls_aggregates = tree_cons (NULL_TREE, var,
9512 tls_aggregates);
9513 else
9514 static_aggregates = tree_cons (NULL_TREE, var,
9515 static_aggregates);
9517 else
9518 /* Check whether the dtor is callable. */
9519 cxx_maybe_build_cleanup (var, tf_warning_or_error);
9522 *initp = init;
9523 return var;
9526 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
9527 initializing a variable of that TYPE. */
9529 tree
9530 initialize_reference (tree type, tree expr,
9531 int flags, tsubst_flags_t complain)
9533 conversion *conv;
9534 void *p;
9535 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
9537 if (type == error_mark_node || error_operand_p (expr))
9538 return error_mark_node;
9540 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9541 p = conversion_obstack_alloc (0);
9543 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
9544 flags, complain);
9545 if (!conv || conv->bad_p)
9547 if (complain & tf_error)
9549 if (conv)
9550 convert_like (conv, expr, complain);
9551 else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
9552 && !TYPE_REF_IS_RVALUE (type)
9553 && !real_lvalue_p (expr))
9554 error_at (loc, "invalid initialization of non-const reference of "
9555 "type %qT from an rvalue of type %qT",
9556 type, TREE_TYPE (expr));
9557 else
9558 error_at (loc, "invalid initialization of reference of type "
9559 "%qT from expression of type %qT", type,
9560 TREE_TYPE (expr));
9562 return error_mark_node;
9565 if (conv->kind == ck_ref_bind)
9566 /* Perform the conversion. */
9567 expr = convert_like (conv, expr, complain);
9568 else if (conv->kind == ck_ambig)
9569 /* We gave an error in build_user_type_conversion_1. */
9570 expr = error_mark_node;
9571 else
9572 gcc_unreachable ();
9574 /* Free all the conversions we allocated. */
9575 obstack_free (&conversion_obstack, p);
9577 return expr;
9580 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
9581 which is bound either to a reference or a std::initializer_list. */
9583 static tree
9584 extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups)
9586 tree sub = init;
9587 tree *p;
9588 STRIP_NOPS (sub);
9589 if (TREE_CODE (sub) == COMPOUND_EXPR)
9591 TREE_OPERAND (sub, 1)
9592 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups);
9593 return init;
9595 if (TREE_CODE (sub) != ADDR_EXPR)
9596 return init;
9597 /* Deal with binding to a subobject. */
9598 for (p = &TREE_OPERAND (sub, 0); TREE_CODE (*p) == COMPONENT_REF; )
9599 p = &TREE_OPERAND (*p, 0);
9600 if (TREE_CODE (*p) == TARGET_EXPR)
9602 tree subinit = NULL_TREE;
9603 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit);
9604 if (subinit)
9605 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
9606 recompute_tree_invariant_for_addr_expr (sub);
9608 return init;
9611 /* INIT is part of the initializer for DECL. If there are any
9612 reference or initializer lists being initialized, extend their
9613 lifetime to match that of DECL. */
9615 tree
9616 extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
9618 tree type = TREE_TYPE (init);
9619 if (processing_template_decl)
9620 return init;
9621 if (TREE_CODE (type) == REFERENCE_TYPE)
9622 init = extend_ref_init_temps_1 (decl, init, cleanups);
9623 else if (is_std_init_list (type))
9625 /* The temporary array underlying a std::initializer_list
9626 is handled like a reference temporary. */
9627 tree ctor = init;
9628 if (TREE_CODE (ctor) == TARGET_EXPR)
9629 ctor = TARGET_EXPR_INITIAL (ctor);
9630 if (TREE_CODE (ctor) == CONSTRUCTOR)
9632 tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
9633 array = extend_ref_init_temps_1 (decl, array, cleanups);
9634 CONSTRUCTOR_ELT (ctor, 0)->value = array;
9637 else if (TREE_CODE (init) == CONSTRUCTOR)
9639 unsigned i;
9640 constructor_elt *p;
9641 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
9642 FOR_EACH_VEC_SAFE_ELT (elts, i, p)
9643 p->value = extend_ref_init_temps (decl, p->value, cleanups);
9646 return init;
9649 /* Returns true iff an initializer for TYPE could contain temporaries that
9650 need to be extended because they are bound to references or
9651 std::initializer_list. */
9653 bool
9654 type_has_extended_temps (tree type)
9656 type = strip_array_types (type);
9657 if (TREE_CODE (type) == REFERENCE_TYPE)
9658 return true;
9659 if (CLASS_TYPE_P (type))
9661 if (is_std_init_list (type))
9662 return true;
9663 for (tree f = next_initializable_field (TYPE_FIELDS (type));
9664 f; f = next_initializable_field (DECL_CHAIN (f)))
9665 if (type_has_extended_temps (TREE_TYPE (f)))
9666 return true;
9668 return false;
9671 /* Returns true iff TYPE is some variant of std::initializer_list. */
9673 bool
9674 is_std_init_list (tree type)
9676 /* Look through typedefs. */
9677 if (!TYPE_P (type))
9678 return false;
9679 if (cxx_dialect == cxx98)
9680 return false;
9681 type = TYPE_MAIN_VARIANT (type);
9682 return (CLASS_TYPE_P (type)
9683 && CP_TYPE_CONTEXT (type) == std_node
9684 && CLASSTYPE_TEMPLATE_INFO (type)
9685 && strcmp (TYPE_NAME_STRING (type), "initializer_list") == 0);
9688 /* Returns true iff DECL is a list constructor: i.e. a constructor which
9689 will accept an argument list of a single std::initializer_list<T>. */
9691 bool
9692 is_list_ctor (tree decl)
9694 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
9695 tree arg;
9697 if (!args || args == void_list_node)
9698 return false;
9700 arg = non_reference (TREE_VALUE (args));
9701 if (!is_std_init_list (arg))
9702 return false;
9704 args = TREE_CHAIN (args);
9706 if (args && args != void_list_node && !TREE_PURPOSE (args))
9707 /* There are more non-defaulted parms. */
9708 return false;
9710 return true;
9713 #include "gt-cp-call.h"