1/* Functions related to invoking -*- C++ -*- methods and overloaded functions.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22
23/* High-level class interface. */
24
25#include "config.h"
26#include "system.h"
27#include "coretypes.h"
28#include "target.h"
29#include "cp-tree.h"
30#include "timevar.h"
31#include "stringpool.h"
32#include "cgraph.h"
33#include "stor-layout.h"
34#include "trans-mem.h"
35#include "flags.h"
36#include "toplev.h"
37#include "intl.h"
38#include "convert.h"
39#include "langhooks.h"
40#include "c-family/c-objc.h"
41#include "internal-fn.h"
42#include "stringpool.h"
43#include "attribs.h"
44
45/* The various kinds of conversion. */
46
47enum conversion_kind {
48 ck_identity,
49 ck_lvalue,
50 ck_fnptr,
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};
63
64/* The rank of the conversion. Order of the enumerals matters; better
65 conversions should come earlier in the list. */
66
67enum 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};
77
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. */
81
82struct conversion {
83 /* The kind of conversion represented by this step. */
84 conversion_kind kind;
85 /* The rank of this conversion. */
86 conversion_rank rank;
87 BOOL_BITFIELD user_conv_p : 1;
88 BOOL_BITFIELD ellipsis_p : 1;
89 BOOL_BITFIELD this_p : 1;
90 /* True if this conversion would be permitted with a bending of
91 language standards, e.g. disregarding pointer qualifiers or
92 converting integers to pointers. */
93 BOOL_BITFIELD bad_p : 1;
94 /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
95 temporary should be created to hold the result of the
96 conversion. */
97 BOOL_BITFIELD need_temporary_p : 1;
98 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
99 from a pointer-to-derived to pointer-to-base is being performed. */
100 BOOL_BITFIELD base_p : 1;
101 /* If KIND is ck_ref_bind, true when either an lvalue reference is
102 being bound to an lvalue expression or an rvalue reference is
103 being bound to an rvalue expression. If KIND is ck_rvalue,
104 true when we are treating an lvalue as an rvalue (12.8p33). If
105 KIND is ck_base, always false. */
106 BOOL_BITFIELD rvaluedness_matches_p: 1;
107 BOOL_BITFIELD check_narrowing: 1;
108 /* The type of the expression resulting from the conversion. */
109 tree type;
110 union {
111 /* The next conversion in the chain. Since the conversions are
112 arranged from outermost to innermost, the NEXT conversion will
113 actually be performed before this conversion. This variant is
114 used only when KIND is neither ck_identity, ck_ambig nor
115 ck_list. Please use the next_conversion function instead
116 of using this field directly. */
117 conversion *next;
118 /* The expression at the beginning of the conversion chain. This
119 variant is used only if KIND is ck_identity or ck_ambig. */
120 tree expr;
121 /* The array of conversions for an initializer_list, so this
122 variant is used only when KIN D is ck_list. */
123 conversion **list;
124 } u;
125 /* The function candidate corresponding to this conversion
126 sequence. This field is only used if KIND is ck_user. */
127 struct z_candidate *cand;
128};
129
130#define CONVERSION_RANK(NODE) \
131 ((NODE)->bad_p ? cr_bad \
132 : (NODE)->ellipsis_p ? cr_ellipsis \
133 : (NODE)->user_conv_p ? cr_user \
134 : (NODE)->rank)
135
136#define BAD_CONVERSION_RANK(NODE) \
137 ((NODE)->ellipsis_p ? cr_ellipsis \
138 : (NODE)->user_conv_p ? cr_user \
139 : (NODE)->rank)
140
141static struct obstack conversion_obstack;
142static bool conversion_obstack_initialized;
143struct rejection_reason;
144
145static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
146static int equal_functions (tree, tree);
147static int joust (struct z_candidate *, struct z_candidate *, bool,
148 tsubst_flags_t);
149static int compare_ics (conversion *, conversion *);
150static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
151#define convert_like(CONV, EXPR, COMPLAIN) \
152 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, \
153 /*issue_conversion_warnings=*/true, \
154 /*c_cast_p=*/false, (COMPLAIN))
155#define convert_like_with_context(CONV, EXPR, FN, ARGNO, COMPLAIN ) \
156 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), \
157 /*issue_conversion_warnings=*/true, \
158 /*c_cast_p=*/false, (COMPLAIN))
159static tree convert_like_real (conversion *, tree, tree, int, bool,
160 bool, tsubst_flags_t);
161static void op_error (location_t, enum tree_code, enum tree_code, tree,
162 tree, tree, bool);
163static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
164 tsubst_flags_t);
165static void print_z_candidate (location_t, const char *, struct z_candidate *);
166static void print_z_candidates (location_t, struct z_candidate *);
167static tree build_this (tree);
168static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
169static bool any_strictly_viable (struct z_candidate *);
170static struct z_candidate *add_template_candidate
171 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
172 tree, tree, tree, int, unification_kind_t, tsubst_flags_t);
173static struct z_candidate *add_template_candidate_real
174 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
175 tree, tree, tree, int, tree, unification_kind_t, tsubst_flags_t);
176static void add_builtin_candidates
177 (struct z_candidate **, enum tree_code, enum tree_code,
178 tree, tree *, int, tsubst_flags_t);
179static void add_builtin_candidate
180 (struct z_candidate **, enum tree_code, enum tree_code,
181 tree, tree, tree, tree *, tree *, int, tsubst_flags_t);
182static bool is_complete (tree);
183static void build_builtin_candidate
184 (struct z_candidate **, tree, tree, tree, tree *, tree *,
185 int, tsubst_flags_t);
186static struct z_candidate *add_conv_candidate
187 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, tree,
188 tree, tsubst_flags_t);
189static struct z_candidate *add_function_candidate
190 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
191 tree, int, tsubst_flags_t);
192static conversion *implicit_conversion (tree, tree, tree, bool, int,
193 tsubst_flags_t);
194static conversion *reference_binding (tree, tree, tree, bool, int,
195 tsubst_flags_t);
196static conversion *build_conv (conversion_kind, tree, conversion *);
197static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
198static conversion *next_conversion (conversion *);
199static bool is_subseq (conversion *, conversion *);
200static conversion *maybe_handle_ref_bind (conversion **);
201static void maybe_handle_implicit_object (conversion **);
202static struct z_candidate *add_candidate
203 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
204 conversion **, tree, tree, int, struct rejection_reason *, int);
205static tree source_type (conversion *);
206static void add_warning (struct z_candidate *, struct z_candidate *);
207static bool reference_compatible_p (tree, tree);
208static conversion *direct_reference_binding (tree, conversion *);
209static bool promoted_arithmetic_type_p (tree);
210static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
211static char *name_as_c_string (tree, tree, bool *);
212static tree prep_operand (tree);
213static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
214 bool, tree, tree, int, struct z_candidate **,
215 tsubst_flags_t);
216static conversion *merge_conversion_sequences (conversion *, conversion *);
217static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
218
219/* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
220 NAME can take many forms... */
221
222bool
223check_dtor_name (tree basetype, tree name)
224{
225 /* Just accept something we've already complained about. */
226 if (name == error_mark_node)
227 return true;
228
229 if (TREE_CODE (name) == TYPE_DECL)
230 name = TREE_TYPE (name);
231 else if (TYPE_P (name))
232 /* OK */;
233 else if (identifier_p (name))
234 {
235 if ((MAYBE_CLASS_TYPE_P (basetype)
236 || TREE_CODE (basetype) == ENUMERAL_TYPE)
237 && name == constructor_name (basetype))
238 return true;
239 else
240 name = get_type_value (name);
241 }
242 else
243 {
244 /* In the case of:
245
246 template <class T> struct S { ~S(); };
247 int i;
248 i.~S();
249
250 NAME will be a class template. */
251 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
252 return false;
253 }
254
255 if (!name || name == error_mark_node)
256 return false;
257 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
258}
259
260/* We want the address of a function or method. We avoid creating a
261 pointer-to-member function. */
262
263tree
264build_addr_func (tree function, tsubst_flags_t complain)
265{
266 tree type = TREE_TYPE (function);
267
268 /* We have to do these by hand to avoid real pointer to member
269 functions. */
270 if (TREE_CODE (type) == METHOD_TYPE)
271 {
272 if (TREE_CODE (function) == OFFSET_REF)
273 {
274 tree object = build_address (TREE_OPERAND (function, 0));
275 return get_member_function_from_ptrfunc (&object,
276 TREE_OPERAND (function, 1),
277 complain);
278 }
279 function = build_address (function);
280 }
281 else
282 function = decay_conversion (function, complain, /*reject_builtin=*/false);
283
284 return function;
285}
286
287/* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
288 POINTER_TYPE to those. Note, pointer to member function types
289 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
290 two variants. build_call_a is the primitive taking an array of
291 arguments, while build_call_n is a wrapper that handles varargs. */
292
293tree
294build_call_n (tree function, int n, ...)
295{
296 if (n == 0)
297 return build_call_a (function, 0, NULL);
298 else
299 {
300 tree *argarray = XALLOCAVEC (tree, n);
301 va_list ap;
302 int i;
303
304 va_start (ap, n);
305 for (i = 0; i < n; i++)
306 argarray[i] = va_arg (ap, tree);
307 va_end (ap);
308 return build_call_a (function, n, argarray);
309 }
310}
311
312/* Update various flags in cfun and the call itself based on what is being
313 called. Split out of build_call_a so that bot_manip can use it too. */
314
315void
316set_flags_from_callee (tree call)
317{
318 bool nothrow;
319 tree decl = get_callee_fndecl (call);
320
321 /* We check both the decl and the type; a function may be known not to
322 throw without being declared throw(). */
323 nothrow = decl && TREE_NOTHROW (decl);
324 if (CALL_EXPR_FN (call))
325 nothrow |= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (call))));
326 else if (internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW)
327 nothrow = true;
328
329 if (!nothrow && at_function_scope_p () && cfun && cp_function_chain)
330 cp_function_chain->can_throw = 1;
331
332 if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
333 current_function_returns_abnormally = 1;
334
335 TREE_NOTHROW (call) = nothrow;
336}
337
338tree
339build_call_a (tree function, int n, tree *argarray)
340{
341 tree decl;
342 tree result_type;
343 tree fntype;
344 int i;
345
346 function = build_addr_func (function, tf_warning_or_error);
347
348 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
349 fntype = TREE_TYPE (TREE_TYPE (function));
350 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
351 || TREE_CODE (fntype) == METHOD_TYPE);
352 result_type = TREE_TYPE (fntype);
353 /* An rvalue has no cv-qualifiers. */
354 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
355 result_type = cv_unqualified (result_type);
356
357 function = build_call_array_loc (input_location,
358 result_type, function, n, argarray);
359 set_flags_from_callee (function);
360
361 decl = get_callee_fndecl (function);
362
363 if (decl && !TREE_USED (decl))
364 {
365 /* We invoke build_call directly for several library
366 functions. These may have been declared normally if
367 we're building libgcc, so we can't just check
368 DECL_ARTIFICIAL. */
369 gcc_assert (DECL_ARTIFICIAL (decl)
370 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
371 "__", 2));
372 mark_used (decl);
373 }
374
375 require_complete_eh_spec_types (fntype, decl);
376
377 TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
378
379 /* Don't pass empty class objects by value. This is useful
380 for tags in STL, which are used to control overload resolution.
381 We don't need to handle other cases of copying empty classes. */
382 if (! decl || ! DECL_BUILT_IN (decl))
383 for (i = 0; i < n; i++)
384 {
385 tree arg = CALL_EXPR_ARG (function, i);
386 if (is_empty_class (TREE_TYPE (arg))
387 && ! TREE_ADDRESSABLE (TREE_TYPE (arg)))
388 {
389 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
390 arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
391 CALL_EXPR_ARG (function, i) = arg;
392 }
393 }
394
395 return function;
396}
397
398/* New overloading code. */
399
400struct z_candidate;
401
402struct candidate_warning {
403 z_candidate *loser;
404 candidate_warning *next;
405};
406
407/* Information for providing diagnostics about why overloading failed. */
408
409enum rejection_reason_code {
410 rr_none,
411 rr_arity,
412 rr_explicit_conversion,
413 rr_template_conversion,
414 rr_arg_conversion,
415 rr_bad_arg_conversion,
416 rr_template_unification,
417 rr_invalid_copy,
418 rr_inherited_ctor,
419 rr_constraint_failure
420};
421
422struct conversion_info {
423 /* The index of the argument, 0-based. */
424 int n_arg;
425 /* The actual argument or its type. */
426 tree from;
427 /* The type of the parameter. */
428 tree to_type;
429};
430
431struct rejection_reason {
432 enum rejection_reason_code code;
433 union {
434 /* Information about an arity mismatch. */
435 struct {
436 /* The expected number of arguments. */
437 int expected;
438 /* The actual number of arguments in the call. */
439 int actual;
440 /* Whether the call was a varargs call. */
441 bool call_varargs_p;
442 } arity;
443 /* Information about an argument conversion mismatch. */
444 struct conversion_info conversion;
445 /* Same, but for bad argument conversions. */
446 struct conversion_info bad_conversion;
447 /* Information about template unification failures. These are the
448 parameters passed to fn_type_unification. */
449 struct {
450 tree tmpl;
451 tree explicit_targs;
452 int num_targs;
453 const tree *args;
454 unsigned int nargs;
455 tree return_type;
456 unification_kind_t strict;
457 int flags;
458 } template_unification;
459 /* Information about template instantiation failures. These are the
460 parameters passed to instantiate_template. */
461 struct {
462 tree tmpl;
463 tree targs;
464 } template_instantiation;
465 } u;
466};
467
468struct z_candidate {
469 /* The FUNCTION_DECL that will be called if this candidate is
470 selected by overload resolution. */
471 tree fn;
472 /* If not NULL_TREE, the first argument to use when calling this
473 function. */
474 tree first_arg;
475 /* The rest of the arguments to use when calling this function. If
476 there are no further arguments this may be NULL or it may be an
477 empty vector. */
478 const vec<tree, va_gc> *args;
479 /* The implicit conversion sequences for each of the arguments to
480 FN. */
481 conversion **convs;
482 /* The number of implicit conversion sequences. */
483 size_t num_convs;
484 /* If FN is a user-defined conversion, the standard conversion
485 sequence from the type returned by FN to the desired destination
486 type. */
487 conversion *second_conv;
488 struct rejection_reason *reason;
489 /* If FN is a member function, the binfo indicating the path used to
490 qualify the name of FN at the call site. This path is used to
491 determine whether or not FN is accessible if it is selected by
492 overload resolution. The DECL_CONTEXT of FN will always be a
493 (possibly improper) base of this binfo. */
494 tree access_path;
495 /* If FN is a non-static member function, the binfo indicating the
496 subobject to which the `this' pointer should be converted if FN
497 is selected by overload resolution. The type pointed to by
498 the `this' pointer must correspond to the most derived class
499 indicated by the CONVERSION_PATH. */
500 tree conversion_path;
501 tree template_decl;
502 tree explicit_targs;
503 candidate_warning *warnings;
504 z_candidate *next;
505 int viable;
506
507 /* The flags active in add_candidate. */
508 int flags;
509};
510
511/* Returns true iff T is a null pointer constant in the sense of
512 [conv.ptr]. */
513
514bool
515null_ptr_cst_p (tree t)
516{
517 tree type = TREE_TYPE (t);
518
519 /* [conv.ptr]
520
521 A null pointer constant is an integral constant expression
522 (_expr.const_) rvalue of integer type that evaluates to zero or
523 an rvalue of type std::nullptr_t. */
524 if (NULLPTR_TYPE_P (type))
525 return true;
526
527 if (cxx_dialect >= cxx11)
528 {
529 /* Core issue 903 says only literal 0 is a null pointer constant. */
530 if (TREE_CODE (type) == INTEGER_TYPE
531 && !char_type_p (type)
532 && TREE_CODE (t) == INTEGER_CST
533 && integer_zerop (t)
534 && !TREE_OVERFLOW (t))
535 return true;
536 }
537 else if (CP_INTEGRAL_TYPE_P (type))
538 {
539 t = fold_non_dependent_expr (t);
540 STRIP_NOPS (t);
541 if (integer_zerop (t) && !TREE_OVERFLOW (t))
542 return true;
543 }
544
545 return false;
546}
547
548/* Returns true iff T is a null member pointer value (4.11). */
549
550bool
551null_member_pointer_value_p (tree t)
552{
553 tree type = TREE_TYPE (t);
554 if (!type)
555 return false;
556 else if (TYPE_PTRMEMFUNC_P (type))
557 return (TREE_CODE (t) == CONSTRUCTOR
558 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
559 else if (TYPE_PTRDATAMEM_P (type))
560 return integer_all_onesp (t);
561 else
562 return false;
563}
564
565/* Returns nonzero if PARMLIST consists of only default parms,
566 ellipsis, and/or undeduced parameter packs. */
567
568bool
569sufficient_parms_p (const_tree parmlist)
570{
571 for (; parmlist && parmlist != void_list_node;
572 parmlist = TREE_CHAIN (parmlist))
573 if (!TREE_PURPOSE (parmlist)
574 && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
575 return false;
576 return true;
577}
578
579/* Allocate N bytes of memory from the conversion obstack. The memory
580 is zeroed before being returned. */
581
582static void *
583conversion_obstack_alloc (size_t n)
584{
585 void *p;
586 if (!conversion_obstack_initialized)
587 {
588 gcc_obstack_init (&conversion_obstack);
589 conversion_obstack_initialized = true;
590 }
591 p = obstack_alloc (&conversion_obstack, n);
592 memset (p, 0, n);
593 return p;
594}
595
596/* Allocate rejection reasons. */
597
598static struct rejection_reason *
599alloc_rejection (enum rejection_reason_code code)
600{
601 struct rejection_reason *p;
602 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
603 p->code = code;
604 return p;
605}
606
607static struct rejection_reason *
608arity_rejection (tree first_arg, int expected, int actual)
609{
610 struct rejection_reason *r = alloc_rejection (rr_arity);
611 int adjust = first_arg != NULL_TREE;
612 r->u.arity.expected = expected - adjust;
613 r->u.arity.actual = actual - adjust;
614 return r;
615}
616
617static struct rejection_reason *
618arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
619{
620 struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
621 int adjust = first_arg != NULL_TREE;
622 r->u.conversion.n_arg = n_arg - adjust;
623 r->u.conversion.from = from;
624 r->u.conversion.to_type = to;
625 return r;
626}
627
628static struct rejection_reason *
629bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
630{
631 struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
632 int adjust = first_arg != NULL_TREE;
633 r->u.bad_conversion.n_arg = n_arg - adjust;
634 r->u.bad_conversion.from = from;
635 r->u.bad_conversion.to_type = to;
636 return r;
637}
638
639static struct rejection_reason *
640explicit_conversion_rejection (tree from, tree to)
641{
642 struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
643 r->u.conversion.n_arg = 0;
644 r->u.conversion.from = from;
645 r->u.conversion.to_type = to;
646 return r;
647}
648
649static struct rejection_reason *
650template_conversion_rejection (tree from, tree to)
651{
652 struct rejection_reason *r = alloc_rejection (rr_template_conversion);
653 r->u.conversion.n_arg = 0;
654 r->u.conversion.from = from;
655 r->u.conversion.to_type = to;
656 return r;
657}
658
659static struct rejection_reason *
660template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
661 const tree *args, unsigned int nargs,
662 tree return_type, unification_kind_t strict,
663 int flags)
664{
665 size_t args_n_bytes = sizeof (*args) * nargs;
666 tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
667 struct rejection_reason *r = alloc_rejection (rr_template_unification);
668 r->u.template_unification.tmpl = tmpl;
669 r->u.template_unification.explicit_targs = explicit_targs;
670 r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
671 /* Copy args to our own storage. */
672 memcpy (args1, args, args_n_bytes);
673 r->u.template_unification.args = args1;
674 r->u.template_unification.nargs = nargs;
675 r->u.template_unification.return_type = return_type;
676 r->u.template_unification.strict = strict;
677 r->u.template_unification.flags = flags;
678 return r;
679}
680
681static struct rejection_reason *
682template_unification_error_rejection (void)
683{
684 return alloc_rejection (rr_template_unification);
685}
686
687static struct rejection_reason *
688invalid_copy_with_fn_template_rejection (void)
689{
690 struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
691 return r;
692}
693
694static struct rejection_reason *
695inherited_ctor_rejection (void)
696{
697 struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
698 return r;
699}
700
701// Build a constraint failure record, saving information into the
702// template_instantiation field of the rejection. If FN is not a template
703// declaration, the TMPL member is the FN declaration and TARGS is empty.
704
705static struct rejection_reason *
706constraint_failure (tree fn)
707{
708 struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
709 if (tree ti = DECL_TEMPLATE_INFO (fn))
710 {
711 r->u.template_instantiation.tmpl = TI_TEMPLATE (ti);
712 r->u.template_instantiation.targs = TI_ARGS (ti);
713 }
714 else
715 {
716 r->u.template_instantiation.tmpl = fn;
717 r->u.template_instantiation.targs = NULL_TREE;
718 }
719 return r;
720}
721
722/* Dynamically allocate a conversion. */
723
724static conversion *
725alloc_conversion (conversion_kind kind)
726{
727 conversion *c;
728 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
729 c->kind = kind;
730 return c;
731}
732
733/* Make sure that all memory on the conversion obstack has been
734 freed. */
735
736void
737validate_conversion_obstack (void)
738{
739 if (conversion_obstack_initialized)
740 gcc_assert ((obstack_next_free (&conversion_obstack)
741 == obstack_base (&conversion_obstack)));
742}
743
744/* Dynamically allocate an array of N conversions. */
745
746static conversion **
747alloc_conversions (size_t n)
748{
749 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
750}
751
752static conversion *
753build_conv (conversion_kind code, tree type, conversion *from)
754{
755 conversion *t;
756 conversion_rank rank = CONVERSION_RANK (from);
757
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;
763
764 switch (code)
765 {
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;
773
774 case ck_qual:
775 case ck_fnptr:
776 if (rank < cr_exact)
777 rank = cr_exact;
778 break;
779
780 default:
781 break;
782 }
783 t->rank = rank;
784 t->user_conv_p = (code == ck_user || from->user_conv_p);
785 t->bad_p = from->bad_p;
786 t->base_p = false;
787 return t;
788}
789
790/* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
791 specialization of std::initializer_list<T>, if such a conversion is
792 possible. */
793
794static conversion *
795build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
796{
797 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
798 unsigned len = CONSTRUCTOR_NELTS (ctor);
799 conversion **subconvs = alloc_conversions (len);
800 conversion *t;
801 unsigned i;
802 tree val;
803
804 /* Within a list-initialization we can have more user-defined
805 conversions. */
806 flags &= ~LOOKUP_NO_CONVERSION;
807 /* But no narrowing conversions. */
808 flags |= LOOKUP_NO_NARROWING;
809
810 /* Can't make an array of these types. */
811 if (TREE_CODE (elttype) == REFERENCE_TYPE
812 || TREE_CODE (elttype) == FUNCTION_TYPE
813 || VOID_TYPE_P (elttype))
814 return NULL;
815
816 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
817 {
818 conversion *sub
819 = implicit_conversion (elttype, TREE_TYPE (val), val,
820 false, flags, complain);
821 if (sub == NULL)
822 return NULL;
823
824 subconvs[i] = sub;
825 }
826
827 t = alloc_conversion (ck_list);
828 t->type = type;
829 t->u.list = subconvs;
830 t->rank = cr_exact;
831
832 for (i = 0; i < len; ++i)
833 {
834 conversion *sub = subconvs[i];
835 if (sub->rank > t->rank)
836 t->rank = sub->rank;
837 if (sub->user_conv_p)
838 t->user_conv_p = true;
839 if (sub->bad_p)
840 t->bad_p = true;
841 }
842
843 return t;
844}
845
846/* Return the next conversion of the conversion chain (if applicable),
847 or NULL otherwise. Please use this function instead of directly
848 accessing fields of struct conversion. */
849
850static conversion *
851next_conversion (conversion *conv)
852{
853 if (conv == NULL
854 || conv->kind == ck_identity
855 || conv->kind == ck_ambig
856 || conv->kind == ck_list)
857 return NULL;
858 return conv->u.next;
859}
860
861/* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
862 is a valid aggregate initializer for array type ATYPE. */
863
864static bool
865can_convert_array (tree atype, tree ctor, int flags, tsubst_flags_t complain)
866{
867 unsigned i;
868 tree elttype = TREE_TYPE (atype);
869 for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
870 {
871 tree val = CONSTRUCTOR_ELT (ctor, i)->value;
872 bool ok;
873 if (TREE_CODE (elttype) == ARRAY_TYPE
874 && TREE_CODE (val) == CONSTRUCTOR)
875 ok = can_convert_array (elttype, val, flags, complain);
876 else
877 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
878 complain);
879 if (!ok)
880 return false;
881 }
882 return true;
883}
884
885/* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
886 aggregate class, if such a conversion is possible. */
887
888static conversion *
889build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
890{
891 unsigned HOST_WIDE_INT i = 0;
892 conversion *c;
893 tree field = next_initializable_field (TYPE_FIELDS (type));
894 tree empty_ctor = NULL_TREE;
895
896 /* We already called reshape_init in implicit_conversion. */
897
898 /* The conversions within the init-list aren't affected by the enclosing
899 context; they're always simple copy-initialization. */
900 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
901
902 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
903 {
904 tree ftype = TREE_TYPE (field);
905 tree val;
906 bool ok;
907
908 if (i < CONSTRUCTOR_NELTS (ctor))
909 val = CONSTRUCTOR_ELT (ctor, i)->value;
910 else if (DECL_INITIAL (field))
911 val = get_nsdmi (field, /*ctor*/false, complain);
912 else if (TREE_CODE (ftype) == REFERENCE_TYPE)
913 /* Value-initialization of reference is ill-formed. */
914 return NULL;
915 else
916 {
917 if (empty_ctor == NULL_TREE)
918 empty_ctor = build_constructor (init_list_type_node, NULL);
919 val = empty_ctor;
920 }
921 ++i;
922
923 if (TREE_CODE (ftype) == ARRAY_TYPE
924 && TREE_CODE (val) == CONSTRUCTOR)
925 ok = can_convert_array (ftype, val, flags, complain);
926 else
927 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
928 complain);
929
930 if (!ok)
931 return NULL;
932
933 if (TREE_CODE (type) == UNION_TYPE)
934 break;
935 }
936
937 if (i < CONSTRUCTOR_NELTS (ctor))
938 return NULL;
939
940 c = alloc_conversion (ck_aggr);
941 c->type = type;
942 c->rank = cr_exact;
943 c->user_conv_p = true;
944 c->check_narrowing = true;
945 c->u.next = NULL;
946 return c;
947}
948
949/* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
950 array type, if such a conversion is possible. */
951
952static conversion *
953build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
954{
955 conversion *c;
956 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
957 tree elttype = TREE_TYPE (type);
958 unsigned i;
959 tree val;
960 bool bad = false;
961 bool user = false;
962 enum conversion_rank rank = cr_exact;
963
964 /* We might need to propagate the size from the element to the array. */
965 complete_type (type);
966
967 if (TYPE_DOMAIN (type)
968 && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
969 {
970 unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
971 if (alen < len)
972 return NULL;
973 }
974
975 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
976
977 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
978 {
979 conversion *sub
980 = implicit_conversion (elttype, TREE_TYPE (val), val,
981 false, flags, complain);
982 if (sub == NULL)
983 return NULL;
984
985 if (sub->rank > rank)
986 rank = sub->rank;
987 if (sub->user_conv_p)
988 user = true;
989 if (sub->bad_p)
990 bad = true;
991 }
992
993 c = alloc_conversion (ck_aggr);
994 c->type = type;
995 c->rank = rank;
996 c->user_conv_p = user;
997 c->bad_p = bad;
998 c->u.next = NULL;
999 return c;
1000}
1001
1002/* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1003 complex type, if such a conversion is possible. */
1004
1005static conversion *
1006build_complex_conv (tree type, tree ctor, int flags,
1007 tsubst_flags_t complain)
1008{
1009 conversion *c;
1010 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1011 tree elttype = TREE_TYPE (type);
1012 unsigned i;
1013 tree val;
1014 bool bad = false;
1015 bool user = false;
1016 enum conversion_rank rank = cr_exact;
1017
1018 if (len != 2)
1019 return NULL;
1020
1021 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1022
1023 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
1024 {
1025 conversion *sub
1026 = implicit_conversion (elttype, TREE_TYPE (val), val,
1027 false, flags, complain);
1028 if (sub == NULL)
1029 return NULL;
1030
1031 if (sub->rank > rank)
1032 rank = sub->rank;
1033 if (sub->user_conv_p)
1034 user = true;
1035 if (sub->bad_p)
1036 bad = true;
1037 }
1038
1039 c = alloc_conversion (ck_aggr);
1040 c->type = type;
1041 c->rank = rank;
1042 c->user_conv_p = user;
1043 c->bad_p = bad;
1044 c->u.next = NULL;
1045 return c;
1046}
1047
1048/* Build a representation of the identity conversion from EXPR to
1049 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1050
1051static conversion *
1052build_identity_conv (tree type, tree expr)
1053{
1054 conversion *c;
1055
1056 c = alloc_conversion (ck_identity);
1057 c->type = type;
1058 c->u.expr = expr;
1059
1060 return c;
1061}
1062
1063/* Converting from EXPR to TYPE was ambiguous in the sense that there
1064 were multiple user-defined conversions to accomplish the job.
1065 Build a conversion that indicates that ambiguity. */
1066
1067static conversion *
1068build_ambiguous_conv (tree type, tree expr)
1069{
1070 conversion *c;
1071
1072 c = alloc_conversion (ck_ambig);
1073 c->type = type;
1074 c->u.expr = expr;
1075
1076 return c;
1077}
1078
1079tree
1080strip_top_quals (tree t)
1081{
1082 if (TREE_CODE (t) == ARRAY_TYPE)
1083 return t;
1084 return cp_build_qualified_type (t, 0);
1085}
1086
1087/* Returns the standard conversion path (see [conv]) from type FROM to type
1088 TO, if any. For proper handling of null pointer constants, you must
1089 also pass the expression EXPR to convert from. If C_CAST_P is true,
1090 this conversion is coming from a C-style cast. */
1091
1092static conversion *
1093standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1094 int flags, tsubst_flags_t complain)
1095{
1096 enum tree_code fcode, tcode;
1097 conversion *conv;
1098 bool fromref = false;
1099 tree qualified_to;
1100
1101 to = non_reference (to);
1102 if (TREE_CODE (from) == REFERENCE_TYPE)
1103 {
1104 fromref = true;
1105 from = TREE_TYPE (from);
1106 }
1107 qualified_to = to;
1108 to = strip_top_quals (to);
1109 from = strip_top_quals (from);
1110
1111 if (expr && type_unknown_p (expr))
1112 {
1113 if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1114 {
1115 tsubst_flags_t tflags = tf_conv;
1116 expr = instantiate_type (to, expr, tflags);
1117 if (expr == error_mark_node)
1118 return NULL;
1119 from = TREE_TYPE (expr);
1120 }
1121 else if (TREE_CODE (to) == BOOLEAN_TYPE)
1122 {
1123 /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1124 expr = resolve_nondeduced_context (expr, complain);
1125 from = TREE_TYPE (expr);
1126 }
1127 }
1128
1129 fcode = TREE_CODE (from);
1130 tcode = TREE_CODE (to);
1131
1132 conv = build_identity_conv (from, expr);
1133 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1134 {
1135 from = type_decays_to (from);
1136 fcode = TREE_CODE (from);
1137 conv = build_conv (ck_lvalue, from, conv);
1138 }
1139 /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1140 obvalue_p) seems odd, since it's already a prvalue, but that's how we
1141 express the copy constructor call required by copy-initialization. */
1142 else if (fromref || (expr && obvalue_p (expr)))
1143 {
1144 if (expr)
1145 {
1146 tree bitfield_type;
1147 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1148 if (bitfield_type)
1149 {
1150 from = strip_top_quals (bitfield_type);
1151 fcode = TREE_CODE (from);
1152 }
1153 }
1154 conv = build_conv (ck_rvalue, from, conv);
1155 if (flags & LOOKUP_PREFER_RVALUE)
1156 /* Tell convert_like_real to set LOOKUP_PREFER_RVALUE. */
1157 conv->rvaluedness_matches_p = true;
1158 }
1159
1160 /* Allow conversion between `__complex__' data types. */
1161 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1162 {
1163 /* The standard conversion sequence to convert FROM to TO is
1164 the standard conversion sequence to perform componentwise
1165 conversion. */
1166 conversion *part_conv = standard_conversion
1167 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1168 complain);
1169
1170 if (part_conv)
1171 {
1172 conv = build_conv (part_conv->kind, to, conv);
1173 conv->rank = part_conv->rank;
1174 }
1175 else
1176 conv = NULL;
1177
1178 return conv;
1179 }
1180
1181 if (same_type_p (from, to))
1182 {
1183 if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1184 conv->type = qualified_to;
1185 return conv;
1186 }
1187
1188 /* [conv.ptr]
1189 A null pointer constant can be converted to a pointer type; ... A
1190 null pointer constant of integral type can be converted to an
1191 rvalue of type std::nullptr_t. */
1192 if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1193 || NULLPTR_TYPE_P (to))
1194 && ((expr && null_ptr_cst_p (expr))
1195 || NULLPTR_TYPE_P (from)))
1196 conv = build_conv (ck_std, to, conv);
1197 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1198 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1199 {
1200 /* For backwards brain damage compatibility, allow interconversion of
1201 pointers and integers with a pedwarn. */
1202 conv = build_conv (ck_std, to, conv);
1203 conv->bad_p = true;
1204 }
1205 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1206 {
1207 /* For backwards brain damage compatibility, allow interconversion of
1208 enums and integers with a pedwarn. */
1209 conv = build_conv (ck_std, to, conv);
1210 conv->bad_p = true;
1211 }
1212 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1213 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1214 {
1215 tree to_pointee;
1216 tree from_pointee;
1217
1218 if (tcode == POINTER_TYPE)
1219 {
1220 to_pointee = TREE_TYPE (to);
1221 from_pointee = TREE_TYPE (from);
1222
1223 /* Since this is the target of a pointer, it can't have function
1224 qualifiers, so any TYPE_QUALS must be for attributes const or
1225 noreturn. Strip them. */
1226 if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1227 && TYPE_QUALS (to_pointee))
1228 to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1229 if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1230 && TYPE_QUALS (from_pointee))
1231 from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1232 }
1233 else
1234 {
1235 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1236 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1237 }
1238
1239 if (tcode == POINTER_TYPE
1240 && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1241 to_pointee))
1242 ;
1243 else if (VOID_TYPE_P (to_pointee)
1244 && !TYPE_PTRDATAMEM_P (from)
1245 && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1246 {
1247 tree nfrom = TREE_TYPE (from);
1248 /* Don't try to apply restrict to void. */
1249 int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1250 from_pointee = cp_build_qualified_type (void_type_node, quals);
1251 from = build_pointer_type (from_pointee);
1252 conv = build_conv (ck_ptr, from, conv);
1253 }
1254 else if (TYPE_PTRDATAMEM_P (from))
1255 {
1256 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1257 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1258
1259 if (same_type_p (fbase, tbase))
1260 /* No base conversion needed. */;
1261 else if (DERIVED_FROM_P (fbase, tbase)
1262 && (same_type_ignoring_top_level_qualifiers_p
1263 (from_pointee, to_pointee)))
1264 {
1265 from = build_ptrmem_type (tbase, from_pointee);
1266 conv = build_conv (ck_pmem, from, conv);
1267 }
1268 else
1269 return NULL;
1270 }
1271 else if (CLASS_TYPE_P (from_pointee)
1272 && CLASS_TYPE_P (to_pointee)
1273 /* [conv.ptr]
1274
1275 An rvalue of type "pointer to cv D," where D is a
1276 class type, can be converted to an rvalue of type
1277 "pointer to cv B," where B is a base class (clause
1278 _class.derived_) of D. If B is an inaccessible
1279 (clause _class.access_) or ambiguous
1280 (_class.member.lookup_) base class of D, a program
1281 that necessitates this conversion is ill-formed.
1282 Therefore, we use DERIVED_FROM_P, and do not check
1283 access or uniqueness. */
1284 && DERIVED_FROM_P (to_pointee, from_pointee))
1285 {
1286 from_pointee
1287 = cp_build_qualified_type (to_pointee,
1288 cp_type_quals (from_pointee));
1289 from = build_pointer_type (from_pointee);
1290 conv = build_conv (ck_ptr, from, conv);
1291 conv->base_p = true;
1292 }
1293
1294 if (same_type_p (from, to))
1295 /* OK */;
1296 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
1297 /* In a C-style cast, we ignore CV-qualification because we
1298 are allowed to perform a static_cast followed by a
1299 const_cast. */
1300 conv = build_conv (ck_qual, to, conv);
1301 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1302 conv = build_conv (ck_qual, to, conv);
1303 else if (expr && string_conv_p (to, expr, 0))
1304 /* converting from string constant to char *. */
1305 conv = build_conv (ck_qual, to, conv);
1306 else if (fnptr_conv_p (to, from))
1307 conv = build_conv (ck_fnptr, to, conv);
1308 /* Allow conversions among compatible ObjC pointer types (base
1309 conversions have been already handled above). */
1310 else if (c_dialect_objc ()
1311 && objc_compare_types (to, from, -4, NULL_TREE))
1312 conv = build_conv (ck_ptr, to, conv);
1313 else if (ptr_reasonably_similar (to_pointee, from_pointee))
1314 {
1315 conv = build_conv (ck_ptr, to, conv);
1316 conv->bad_p = true;
1317 }
1318 else
1319 return NULL;
1320
1321 from = to;
1322 }
1323 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1324 {
1325 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1326 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1327 tree fbase = class_of_this_parm (fromfn);
1328 tree tbase = class_of_this_parm (tofn);
1329
1330 if (!DERIVED_FROM_P (fbase, tbase))
1331 return NULL;
1332
1333 tree fstat = static_fn_type (fromfn);
1334 tree tstat = static_fn_type (tofn);
1335 if (same_type_p (tstat, fstat)
1336 || fnptr_conv_p (tstat, fstat))
1337 /* OK */;
1338 else
1339 return NULL;
1340
1341 if (!same_type_p (fbase, tbase))
1342 {
1343 from = build_memfn_type (fstat,
1344 tbase,
1345 cp_type_quals (tbase),
1346 type_memfn_rqual (tofn));
1347 from = build_ptrmemfunc_type (build_pointer_type (from));
1348 conv = build_conv (ck_pmem, from, conv);
1349 conv->base_p = true;
1350 }
1351 if (fnptr_conv_p (tstat, fstat))
1352 conv = build_conv (ck_fnptr, to, conv);
1353 }
1354 else if (tcode == BOOLEAN_TYPE)
1355 {
1356 /* [conv.bool]
1357
1358 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1359 to member type can be converted to a prvalue of type bool. ...
1360 For direct-initialization (8.5 [dcl.init]), a prvalue of type
1361 std::nullptr_t can be converted to a prvalue of type bool; */
1362 if (ARITHMETIC_TYPE_P (from)
1363 || UNSCOPED_ENUM_P (from)
1364 || fcode == POINTER_TYPE
1365 || TYPE_PTRMEM_P (from)
1366 || NULLPTR_TYPE_P (from))
1367 {
1368 conv = build_conv (ck_std, to, conv);
1369 if (fcode == POINTER_TYPE
1370 || TYPE_PTRDATAMEM_P (from)
1371 || (TYPE_PTRMEMFUNC_P (from)
1372 && conv->rank < cr_pbool)
1373 || NULLPTR_TYPE_P (from))
1374 conv->rank = cr_pbool;
1375 if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1376 conv->bad_p = true;
1377 return conv;
1378 }
1379
1380 return NULL;
1381 }
1382 /* We don't check for ENUMERAL_TYPE here because there are no standard
1383 conversions to enum type. */
1384 /* As an extension, allow conversion to complex type. */
1385 else if (ARITHMETIC_TYPE_P (to))
1386 {
1387 if (! (INTEGRAL_CODE_P (fcode)
1388 || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1389 || SCOPED_ENUM_P (from))
1390 return NULL;
1391 conv = build_conv (ck_std, to, conv);
1392
1393 /* Give this a better rank if it's a promotion. */
1394 if (same_type_p (to, type_promotes_to (from))
1395 && next_conversion (conv)->rank <= cr_promotion)
1396 conv->rank = cr_promotion;
1397 }
1398 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1399 && vector_types_convertible_p (from, to, false))
1400 return build_conv (ck_std, to, conv);
1401 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1402 && is_properly_derived_from (from, to))
1403 {
1404 if (conv->kind == ck_rvalue)
1405 conv = next_conversion (conv);
1406 conv = build_conv (ck_base, to, conv);
1407 /* The derived-to-base conversion indicates the initialization
1408 of a parameter with base type from an object of a derived
1409 type. A temporary object is created to hold the result of
1410 the conversion unless we're binding directly to a reference. */
1411 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1412 }
1413 else
1414 return NULL;
1415
1416 if (flags & LOOKUP_NO_NARROWING)
1417 conv->check_narrowing = true;
1418
1419 return conv;
1420}
1421
1422/* Returns nonzero if T1 is reference-related to T2. */
1423
1424bool
1425reference_related_p (tree t1, tree t2)
1426{
1427 if (t1 == error_mark_node || t2 == error_mark_node)
1428 return false;
1429
1430 t1 = TYPE_MAIN_VARIANT (t1);
1431 t2 = TYPE_MAIN_VARIANT (t2);
1432
1433 /* [dcl.init.ref]
1434
1435 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1436 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
1437 of T2. */
1438 return (same_type_p (t1, t2)
1439 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1440 && DERIVED_FROM_P (t1, t2)));
1441}
1442
1443/* Returns nonzero if T1 is reference-compatible with T2. */
1444
1445static bool
1446reference_compatible_p (tree t1, tree t2)
1447{
1448 /* [dcl.init.ref]
1449
1450 "cv1 T1" is reference compatible with "cv2 T2" if
1451 * T1 is reference-related to T2 or
1452 * T2 is "noexcept function" and T1 is "function", where the
1453 function types are otherwise the same,
1454 and cv1 is the same cv-qualification as, or greater cv-qualification
1455 than, cv2. */
1456 return ((reference_related_p (t1, t2)
1457 || fnptr_conv_p (t1, t2))
1458 && at_least_as_qualified_p (t1, t2));
1459}
1460
1461/* A reference of the indicated TYPE is being bound directly to the
1462 expression represented by the implicit conversion sequence CONV.
1463 Return a conversion sequence for this binding. */
1464
1465static conversion *
1466direct_reference_binding (tree type, conversion *conv)
1467{
1468 tree t;
1469
1470 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1471 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1472
1473 t = TREE_TYPE (type);
1474
1475 /* [over.ics.rank]
1476
1477 When a parameter of reference type binds directly
1478 (_dcl.init.ref_) to an argument expression, the implicit
1479 conversion sequence is the identity conversion, unless the
1480 argument expression has a type that is a derived class of the
1481 parameter type, in which case the implicit conversion sequence is
1482 a derived-to-base Conversion.
1483
1484 If the parameter binds directly to the result of applying a
1485 conversion function to the argument expression, the implicit
1486 conversion sequence is a user-defined conversion sequence
1487 (_over.ics.user_), with the second standard conversion sequence
1488 either an identity conversion or, if the conversion function
1489 returns an entity of a type that is a derived class of the
1490 parameter type, a derived-to-base conversion. */
1491 if (is_properly_derived_from (conv->type, t))
1492 {
1493 /* Represent the derived-to-base conversion. */
1494 conv = build_conv (ck_base, t, conv);
1495 /* We will actually be binding to the base-class subobject in
1496 the derived class, so we mark this conversion appropriately.
1497 That way, convert_like knows not to generate a temporary. */
1498 conv->need_temporary_p = false;
1499 }
1500 return build_conv (ck_ref_bind, type, conv);
1501}
1502
1503/* Returns the conversion path from type FROM to reference type TO for
1504 purposes of reference binding. For lvalue binding, either pass a
1505 reference type to FROM or an lvalue expression to EXPR. If the
1506 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1507 the conversion returned. If C_CAST_P is true, this
1508 conversion is coming from a C-style cast. */
1509
1510static conversion *
1511reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1512 tsubst_flags_t complain)
1513{
1514 conversion *conv = NULL;
1515 tree to = TREE_TYPE (rto);
1516 tree from = rfrom;
1517 tree tfrom;
1518 bool related_p;
1519 bool compatible_p;
1520 cp_lvalue_kind gl_kind;
1521 bool is_lvalue;
1522
1523 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1524 {
1525 expr = instantiate_type (to, expr, tf_none);
1526 if (expr == error_mark_node)
1527 return NULL;
1528 from = TREE_TYPE (expr);
1529 }
1530
1531 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1532 {
1533 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1534 /* DR 1288: Otherwise, if the initializer list has a single element
1535 of type E and ... [T's] referenced type is reference-related to E,
1536 the object or reference is initialized from that element... */
1537 if (CONSTRUCTOR_NELTS (expr) == 1)
1538 {
1539 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1540 if (error_operand_p (elt))
1541 return NULL;
1542 tree etype = TREE_TYPE (elt);
1543 if (reference_related_p (to, etype))
1544 {
1545 expr = elt;
1546 from = etype;
1547 goto skip;
1548 }
1549 }
1550 /* Otherwise, if T is a reference type, a prvalue temporary of the
1551 type referenced by T is copy-list-initialized or
1552 direct-list-initialized, depending on the kind of initialization
1553 for the reference, and the reference is bound to that temporary. */
1554 conv = implicit_conversion (to, from, expr, c_cast_p,
1555 flags|LOOKUP_NO_TEMP_BIND, complain);
1556 skip:;
1557 }
1558
1559 if (TREE_CODE (from) == REFERENCE_TYPE)
1560 {
1561 from = TREE_TYPE (from);
1562 if (!TYPE_REF_IS_RVALUE (rfrom)
1563 || TREE_CODE (from) == FUNCTION_TYPE)
1564 gl_kind = clk_ordinary;
1565 else
1566 gl_kind = clk_rvalueref;
1567 }
1568 else if (expr)
1569 gl_kind = lvalue_kind (expr);
1570 else if (CLASS_TYPE_P (from)
1571 || TREE_CODE (from) == ARRAY_TYPE)
1572 gl_kind = clk_class;
1573 else
1574 gl_kind = clk_none;
1575
1576 /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1577 if ((flags & LOOKUP_NO_TEMP_BIND)
1578 && (gl_kind & clk_class))
1579 gl_kind = clk_none;
1580
1581 /* Same mask as real_lvalue_p. */
1582 is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1583
1584 tfrom = from;
1585 if ((gl_kind & clk_bitfield) != 0)
1586 tfrom = unlowered_expr_type (expr);
1587
1588 /* Figure out whether or not the types are reference-related and
1589 reference compatible. We have to do this after stripping
1590 references from FROM. */
1591 related_p = reference_related_p (to, tfrom);
1592 /* If this is a C cast, first convert to an appropriately qualified
1593 type, so that we can later do a const_cast to the desired type. */
1594 if (related_p && c_cast_p
1595 && !at_least_as_qualified_p (to, tfrom))
1596 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1597 compatible_p = reference_compatible_p (to, tfrom);
1598
1599 /* Directly bind reference when target expression's type is compatible with
1600 the reference and expression is an lvalue. In DR391, the wording in
1601 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1602 const and rvalue references to rvalues of compatible class type.
1603 We should also do direct bindings for non-class xvalues. */
1604 if ((related_p || compatible_p) && gl_kind)
1605 {
1606 /* [dcl.init.ref]
1607
1608 If the initializer expression
1609
1610 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1611 is reference-compatible with "cv2 T2,"
1612
1613 the reference is bound directly to the initializer expression
1614 lvalue.
1615
1616 [...]
1617 If the initializer expression is an rvalue, with T2 a class type,
1618 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1619 is bound to the object represented by the rvalue or to a sub-object
1620 within that object. */
1621
1622 conv = build_identity_conv (tfrom, expr);
1623 conv = direct_reference_binding (rto, conv);
1624
1625 if (TREE_CODE (rfrom) == REFERENCE_TYPE)
1626 /* Handle rvalue reference to function properly. */
1627 conv->rvaluedness_matches_p
1628 = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1629 else
1630 conv->rvaluedness_matches_p
1631 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1632
1633 if ((gl_kind & clk_bitfield) != 0
1634 || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1635 /* For the purposes of overload resolution, we ignore the fact
1636 this expression is a bitfield or packed field. (In particular,
1637 [over.ics.ref] says specifically that a function with a
1638 non-const reference parameter is viable even if the
1639 argument is a bitfield.)
1640
1641 However, when we actually call the function we must create
1642 a temporary to which to bind the reference. If the
1643 reference is volatile, or isn't const, then we cannot make
1644 a temporary, so we just issue an error when the conversion
1645 actually occurs. */
1646 conv->need_temporary_p = true;
1647
1648 /* Don't allow binding of lvalues (other than function lvalues) to
1649 rvalue references. */
1650 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1651 && TREE_CODE (to) != FUNCTION_TYPE)
1652 conv->bad_p = true;
1653
1654 /* Nor the reverse. */
1655 if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
1656 && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
1657 || (flags & LOOKUP_NO_RVAL_BIND))
1658 && TREE_CODE (to) != FUNCTION_TYPE)
1659 conv->bad_p = true;
1660
1661 if (!compatible_p)
1662 conv->bad_p = true;
1663
1664 return conv;
1665 }
1666 /* [class.conv.fct] A conversion function is never used to convert a
1667 (possibly cv-qualified) object to the (possibly cv-qualified) same
1668 object type (or a reference to it), to a (possibly cv-qualified) base
1669 class of that type (or a reference to it).... */
1670 else if (CLASS_TYPE_P (from) && !related_p
1671 && !(flags & LOOKUP_NO_CONVERSION))
1672 {
1673 /* [dcl.init.ref]
1674
1675 If the initializer expression
1676
1677 -- has a class type (i.e., T2 is a class type) can be
1678 implicitly converted to an lvalue of type "cv3 T3," where
1679 "cv1 T1" is reference-compatible with "cv3 T3". (this
1680 conversion is selected by enumerating the applicable
1681 conversion functions (_over.match.ref_) and choosing the
1682 best one through overload resolution. (_over.match_).
1683
1684 the reference is bound to the lvalue result of the conversion
1685 in the second case. */
1686 z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
1687 complain);
1688 if (cand)
1689 return cand->second_conv;
1690 }
1691
1692 /* From this point on, we conceptually need temporaries, even if we
1693 elide them. Only the cases above are "direct bindings". */
1694 if (flags & LOOKUP_NO_TEMP_BIND)
1695 return NULL;
1696
1697 /* [over.ics.rank]
1698
1699 When a parameter of reference type is not bound directly to an
1700 argument expression, the conversion sequence is the one required
1701 to convert the argument expression to the underlying type of the
1702 reference according to _over.best.ics_. Conceptually, this
1703 conversion sequence corresponds to copy-initializing a temporary
1704 of the underlying type with the argument expression. Any
1705 difference in top-level cv-qualification is subsumed by the
1706 initialization itself and does not constitute a conversion. */
1707
1708 /* [dcl.init.ref]
1709
1710 Otherwise, the reference shall be an lvalue reference to a
1711 non-volatile const type, or the reference shall be an rvalue
1712 reference.
1713
1714 We try below to treat this as a bad conversion to improve diagnostics,
1715 but if TO is an incomplete class, we need to reject this conversion
1716 now to avoid unnecessary instantiation. */
1717 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto)
1718 && !COMPLETE_TYPE_P (to))
1719 return NULL;
1720
1721 /* We're generating a temporary now, but don't bind any more in the
1722 conversion (specifically, don't slice the temporary returned by a
1723 conversion operator). */
1724 flags |= LOOKUP_NO_TEMP_BIND;
1725
1726 /* Core issue 899: When [copy-]initializing a temporary to be bound
1727 to the first parameter of a copy constructor (12.8) called with
1728 a single argument in the context of direct-initialization,
1729 explicit conversion functions are also considered.
1730
1731 So don't set LOOKUP_ONLYCONVERTING in that case. */
1732 if (!(flags & LOOKUP_COPY_PARM))
1733 flags |= LOOKUP_ONLYCONVERTING;
1734
1735 if (!conv)
1736 conv = implicit_conversion (to, from, expr, c_cast_p,
1737 flags, complain);
1738 if (!conv)
1739 return NULL;
1740
1741 if (conv->user_conv_p)
1742 {
1743 /* If initializing the temporary used a conversion function,
1744 recalculate the second conversion sequence. */
1745 for (conversion *t = conv; t; t = next_conversion (t))
1746 if (t->kind == ck_user
1747 && DECL_CONV_FN_P (t->cand->fn))
1748 {
1749 tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
1750 int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
1751 conversion *new_second
1752 = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
1753 sflags, complain);
1754 if (!new_second)
1755 return NULL;
1756 return merge_conversion_sequences (t, new_second);
1757 }
1758 }
1759
1760 conv = build_conv (ck_ref_bind, rto, conv);
1761 /* This reference binding, unlike those above, requires the
1762 creation of a temporary. */
1763 conv->need_temporary_p = true;
1764 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1765
1766 /* [dcl.init.ref]
1767
1768 Otherwise, the reference shall be an lvalue reference to a
1769 non-volatile const type, or the reference shall be an rvalue
1770 reference. */
1771 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1772 conv->bad_p = true;
1773
1774 /* [dcl.init.ref]
1775
1776 Otherwise, a temporary of type "cv1 T1" is created and
1777 initialized from the initializer expression using the rules for a
1778 non-reference copy initialization. If T1 is reference-related to
1779 T2, cv1 must be the same cv-qualification as, or greater
1780 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1781 if (related_p && !at_least_as_qualified_p (to, from))
1782 conv->bad_p = true;
1783
1784 return conv;
1785}
1786
1787/* Returns the implicit conversion sequence (see [over.ics]) from type
1788 FROM to type TO. The optional expression EXPR may affect the
1789 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1790 true, this conversion is coming from a C-style cast. */
1791
1792static conversion *
1793implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1794 int flags, tsubst_flags_t complain)
1795{
1796 conversion *conv;
1797
1798 if (from == error_mark_node || to == error_mark_node
1799 || expr == error_mark_node)
1800 return NULL;
1801
1802 /* Other flags only apply to the primary function in overload
1803 resolution, or after we've chosen one. */
1804 flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
1805 |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE
1806 |LOOKUP_NO_NARROWING|LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL);
1807
1808 /* FIXME: actually we don't want warnings either, but we can't just
1809 have 'complain &= ~(tf_warning|tf_error)' because it would cause
1810 the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
1811 We really ought not to issue that warning until we've committed
1812 to that conversion. */
1813 complain &= ~tf_error;
1814
1815 /* Call reshape_init early to remove redundant braces. */
1816 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)
1817 && CLASS_TYPE_P (to)
1818 && COMPLETE_TYPE_P (complete_type (to))
1819 && !CLASSTYPE_NON_AGGREGATE (to))
1820 {
1821 expr = reshape_init (to, expr, complain);
1822 if (expr == error_mark_node)
1823 return NULL;
1824 from = TREE_TYPE (expr);
1825 }
1826
1827 if (TREE_CODE (to) == REFERENCE_TYPE)
1828 conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
1829 else
1830 conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
1831
1832 if (conv)
1833 return conv;
1834
1835 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1836 {
1837 if (is_std_init_list (to))
1838 return build_list_conv (to, expr, flags, complain);
1839
1840 /* As an extension, allow list-initialization of _Complex. */
1841 if (TREE_CODE (to) == COMPLEX_TYPE)
1842 {
1843 conv = build_complex_conv (to, expr, flags, complain);
1844 if (conv)
1845 return conv;
1846 }
1847
1848 /* Allow conversion from an initializer-list with one element to a
1849 scalar type. */
1850 if (SCALAR_TYPE_P (to))
1851 {
1852 int nelts = CONSTRUCTOR_NELTS (expr);
1853 tree elt;
1854
1855 if (nelts == 0)
1856 elt = build_value_init (to, tf_none);
1857 else if (nelts == 1)
1858 elt = CONSTRUCTOR_ELT (expr, 0)->value;
1859 else
1860 elt = error_mark_node;
1861
1862 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
1863 c_cast_p, flags, complain);
1864 if (conv)
1865 {
1866 conv->check_narrowing = true;
1867 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
1868 /* Too many levels of braces, i.e. '{{1}}'. */
1869 conv->bad_p = true;
1870 return conv;
1871 }
1872 }
1873 else if (TREE_CODE (to) == ARRAY_TYPE)
1874 return build_array_conv (to, expr, flags, complain);
1875 }
1876
1877 if (expr != NULL_TREE
1878 && (MAYBE_CLASS_TYPE_P (from)
1879 || MAYBE_CLASS_TYPE_P (to))
1880 && (flags & LOOKUP_NO_CONVERSION) == 0)
1881 {
1882 struct z_candidate *cand;
1883
1884 if (CLASS_TYPE_P (to)
1885 && BRACE_ENCLOSED_INITIALIZER_P (expr)
1886 && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
1887 return build_aggr_conv (to, expr, flags, complain);
1888
1889 cand = build_user_type_conversion_1 (to, expr, flags, complain);
1890 if (cand)
1891 {
1892 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
1893 && CONSTRUCTOR_NELTS (expr) == 1
1894 && !is_list_ctor (cand->fn))
1895 {
1896 /* "If C is not an initializer-list constructor and the
1897 initializer list has a single element of type cv U, where U is
1898 X or a class derived from X, the implicit conversion sequence
1899 has Exact Match rank if U is X, or Conversion rank if U is
1900 derived from X." */
1901 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1902 tree elttype = TREE_TYPE (elt);
1903 if (reference_related_p (to, elttype))
1904 return implicit_conversion (to, elttype, elt,
1905 c_cast_p, flags, complain);
1906 }
1907 conv = cand->second_conv;
1908 }
1909
1910 /* We used to try to bind a reference to a temporary here, but that
1911 is now handled after the recursive call to this function at the end
1912 of reference_binding. */
1913 return conv;
1914 }
1915
1916 return NULL;
1917}
1918
1919/* Add a new entry to the list of candidates. Used by the add_*_candidate
1920 functions. ARGS will not be changed until a single candidate is
1921 selected. */
1922
1923static struct z_candidate *
1924add_candidate (struct z_candidate **candidates,
1925 tree fn, tree first_arg, const vec<tree, va_gc> *args,
1926 size_t num_convs, conversion **convs,
1927 tree access_path, tree conversion_path,
1928 int viable, struct rejection_reason *reason,
1929 int flags)
1930{
1931 struct z_candidate *cand = (struct z_candidate *)
1932 conversion_obstack_alloc (sizeof (struct z_candidate));
1933
1934 cand->fn = fn;
1935 cand->first_arg = first_arg;
1936 cand->args = args;
1937 cand->convs = convs;
1938 cand->num_convs = num_convs;
1939 cand->access_path = access_path;
1940 cand->conversion_path = conversion_path;
1941 cand->viable = viable;
1942 cand->reason = reason;
1943 cand->next = *candidates;
1944 cand->flags = flags;
1945 *candidates = cand;
1946
1947 return cand;
1948}
1949
1950/* Return the number of remaining arguments in the parameter list
1951 beginning with ARG. */
1952
1953int
1954remaining_arguments (tree arg)
1955{
1956 int n;
1957
1958 for (n = 0; arg != NULL_TREE && arg != void_list_node;
1959 arg = TREE_CHAIN (arg))
1960 n++;
1961
1962 return n;
1963}
1964
1965/* Create an overload candidate for the function or method FN called
1966 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
1967 FLAGS is passed on to implicit_conversion.
1968
1969 This does not change ARGS.
1970
1971 CTYPE, if non-NULL, is the type we want to pretend this function
1972 comes from for purposes of overload resolution. */
1973
1974static struct z_candidate *
1975add_function_candidate (struct z_candidate **candidates,
1976 tree fn, tree ctype, tree first_arg,
1977 const vec<tree, va_gc> *args, tree access_path,
1978 tree conversion_path, int flags,
1979 tsubst_flags_t complain)
1980{
1981 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1982 int i, len;
1983 conversion **convs;
1984 tree parmnode;
1985 tree orig_first_arg = first_arg;
1986 int skip;
1987 int viable = 1;
1988 struct rejection_reason *reason = NULL;
1989
1990 /* At this point we should not see any functions which haven't been
1991 explicitly declared, except for friend functions which will have
1992 been found using argument dependent lookup. */
1993 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
1994
1995 /* The `this', `in_chrg' and VTT arguments to constructors are not
1996 considered in overload resolution. */
1997 if (DECL_CONSTRUCTOR_P (fn))
1998 {
1999 if (ctor_omit_inherited_parms (fn))
2000 /* Bring back parameters omitted from an inherited ctor. */
2001 parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2002 else
2003 parmlist = skip_artificial_parms_for (fn, parmlist);
2004 skip = num_artificial_parms_for (fn);
2005 if (skip > 0 && first_arg != NULL_TREE)
2006 {
2007 --skip;
2008 first_arg = NULL_TREE;
2009 }
2010 }
2011 else
2012 skip = 0;
2013
2014 len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2015 convs = alloc_conversions (len);
2016
2017 /* 13.3.2 - Viable functions [over.match.viable]
2018 First, to be a viable function, a candidate function shall have enough
2019 parameters to agree in number with the arguments in the list.
2020
2021 We need to check this first; otherwise, checking the ICSes might cause
2022 us to produce an ill-formed template instantiation. */
2023
2024 parmnode = parmlist;
2025 for (i = 0; i < len; ++i)
2026 {
2027 if (parmnode == NULL_TREE || parmnode == void_list_node)
2028 break;
2029 parmnode = TREE_CHAIN (parmnode);
2030 }
2031
2032 if ((i < len && parmnode)
2033 || !sufficient_parms_p (parmnode))
2034 {
2035 int remaining = remaining_arguments (parmnode);
2036 viable = 0;
2037 reason = arity_rejection (first_arg, i + remaining, len);
2038 }
2039
2040 /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2041 parameter of type "reference to cv C" (including such a constructor
2042 instantiated from a template) is excluded from the set of candidate
2043 functions when used to construct an object of type D with an argument list
2044 containing a single argument if C is reference-related to D. */
2045 if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2046 && flag_new_inheriting_ctors
2047 && DECL_INHERITED_CTOR (fn))
2048 {
2049 tree ptype = non_reference (TREE_VALUE (parmlist));
2050 tree dtype = DECL_CONTEXT (fn);
2051 tree btype = DECL_INHERITED_CTOR_BASE (fn);
2052 if (reference_related_p (ptype, dtype)
2053 && reference_related_p (btype, ptype))
2054 {
2055 viable = false;
2056 reason = inherited_ctor_rejection ();
2057 }
2058 }
2059
2060 /* Second, for a function to be viable, its constraints must be
2061 satisfied. */
2062 if (flag_concepts && viable
2063 && !constraints_satisfied_p (fn))
2064 {
2065 reason = constraint_failure (fn);
2066 viable = false;
2067 }
2068
2069 /* When looking for a function from a subobject from an implicit
2070 copy/move constructor/operator=, don't consider anything that takes (a
2071 reference to) an unrelated type. See c++/44909 and core 1092. */
2072 if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2073 {
2074 if (DECL_CONSTRUCTOR_P (fn))
2075 i = 1;
2076 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2077 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2078 i = 2;
2079 else
2080 i = 0;
2081 if (i && len == i)
2082 {
2083 parmnode = chain_index (i-1, parmlist);
2084 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2085 ctype))
2086 viable = 0;
2087 }
2088
2089 /* This only applies at the top level. */
2090 flags &= ~LOOKUP_DEFAULTED;
2091 }
2092
2093 if (! viable)
2094 goto out;
2095
2096 /* Third, for F to be a viable function, there shall exist for each
2097 argument an implicit conversion sequence that converts that argument
2098 to the corresponding parameter of F. */
2099
2100 parmnode = parmlist;
2101
2102 for (i = 0; i < len; ++i)
2103 {
2104 tree argtype, to_type;
2105 tree arg;
2106 conversion *t;
2107 int is_this;
2108
2109 if (parmnode == void_list_node)
2110 break;
2111
2112 if (i == 0 && first_arg != NULL_TREE)
2113 arg = first_arg;
2114 else
2115 arg = CONST_CAST_TREE (
2116 (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2117 argtype = lvalue_type (arg);
2118
2119 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2120 && ! DECL_CONSTRUCTOR_P (fn));
2121
2122 if (parmnode)
2123 {
2124 tree parmtype = TREE_VALUE (parmnode);
2125 int lflags = flags;
2126
2127 parmnode = TREE_CHAIN (parmnode);
2128
2129 /* The type of the implicit object parameter ('this') for
2130 overload resolution is not always the same as for the
2131 function itself; conversion functions are considered to
2132 be members of the class being converted, and functions
2133 introduced by a using-declaration are considered to be
2134 members of the class that uses them.
2135
2136 Since build_over_call ignores the ICS for the `this'
2137 parameter, we can just change the parm type. */
2138 if (ctype && is_this)
2139 {
2140 parmtype = cp_build_qualified_type
2141 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
2142 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2143 {
2144 /* If the function has a ref-qualifier, the implicit
2145 object parameter has reference type. */
2146 bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2147 parmtype = cp_build_reference_type (parmtype, rv);
2148 /* The special handling of 'this' conversions in compare_ics
2149 does not apply if there is a ref-qualifier. */
2150 is_this = false;
2151 }
2152 else
2153 {
2154 parmtype = build_pointer_type (parmtype);
2155 /* We don't use build_this here because we don't want to
2156 capture the object argument until we've chosen a
2157 non-static member function. */
2158 arg = build_address (arg);
2159 argtype = lvalue_type (arg);
2160 }
2161 }
2162
2163 /* Core issue 899: When [copy-]initializing a temporary to be bound
2164 to the first parameter of a copy constructor (12.8) called with
2165 a single argument in the context of direct-initialization,
2166 explicit conversion functions are also considered.
2167
2168 So set LOOKUP_COPY_PARM to let reference_binding know that
2169 it's being called in that context. We generalize the above
2170 to handle move constructors and template constructors as well;
2171 the standardese should soon be updated similarly. */
2172 if (ctype && i == 0 && (len-skip == 1)
2173 && DECL_CONSTRUCTOR_P (fn)
2174 && parmtype != error_mark_node
2175 && (same_type_ignoring_top_level_qualifiers_p
2176 (non_reference (parmtype), ctype)))
2177 {
2178 if (!(flags & LOOKUP_ONLYCONVERTING))
2179 lflags |= LOOKUP_COPY_PARM;
2180 /* We allow user-defined conversions within init-lists, but
2181 don't list-initialize the copy parm, as that would mean
2182 using two levels of braces for the same type. */
2183 if ((flags & LOOKUP_LIST_INIT_CTOR)
2184 && BRACE_ENCLOSED_INITIALIZER_P (arg))
2185 lflags |= LOOKUP_NO_CONVERSION;
2186 }
2187 else
2188 lflags |= LOOKUP_ONLYCONVERTING;
2189
2190 t = implicit_conversion (parmtype, argtype, arg,
2191 /*c_cast_p=*/false, lflags, complain);
2192 to_type = parmtype;
2193 }
2194 else
2195 {
2196 t = build_identity_conv (argtype, arg);
2197 t->ellipsis_p = true;
2198 to_type = argtype;
2199 }
2200
2201 if (t && is_this)
2202 t->this_p = true;
2203
2204 convs[i] = t;
2205 if (! t)
2206 {
2207 viable = 0;
2208 reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
2209 break;
2210 }
2211
2212 if (t->bad_p)
2213 {
2214 viable = -1;
2215 reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type);
2216 }
2217 }
2218
2219 out:
2220 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2221 access_path, conversion_path, viable, reason, flags);
2222}
2223
2224/* Create an overload candidate for the conversion function FN which will
2225 be invoked for expression OBJ, producing a pointer-to-function which
2226 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2227 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2228 passed on to implicit_conversion.
2229
2230 Actually, we don't really care about FN; we care about the type it
2231 converts to. There may be multiple conversion functions that will
2232 convert to that type, and we rely on build_user_type_conversion_1 to
2233 choose the best one; so when we create our candidate, we record the type
2234 instead of the function. */
2235
2236static struct z_candidate *
2237add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2238 const vec<tree, va_gc> *arglist,
2239 tree access_path, tree conversion_path,
2240 tsubst_flags_t complain)
2241{
2242 tree totype = TREE_TYPE (TREE_TYPE (fn));
2243 int i, len, viable, flags;
2244 tree parmlist, parmnode;
2245 conversion **convs;
2246 struct rejection_reason *reason;
2247
2248 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2249 parmlist = TREE_TYPE (parmlist);
2250 parmlist = TYPE_ARG_TYPES (parmlist);
2251
2252 len = vec_safe_length (arglist) + 1;
2253 convs = alloc_conversions (len);
2254 parmnode = parmlist;
2255 viable = 1;
2256 flags = LOOKUP_IMPLICIT;
2257 reason = NULL;
2258
2259 /* Don't bother looking up the same type twice. */
2260 if (*candidates && (*candidates)->fn == totype)
2261 return NULL;
2262
2263 for (i = 0; i < len; ++i)
2264 {
2265 tree arg, argtype, convert_type = NULL_TREE;
2266 conversion *t;
2267
2268 if (i == 0)
2269 arg = obj;
2270 else
2271 arg = (*arglist)[i - 1];
2272 argtype = lvalue_type (arg);
2273
2274 if (i == 0)
2275 {
2276 t = build_identity_conv (argtype, NULL_TREE);
2277 t = build_conv (ck_user, totype, t);
2278 /* Leave the 'cand' field null; we'll figure out the conversion in
2279 convert_like_real if this candidate is chosen. */
2280 convert_type = totype;
2281 }
2282 else if (parmnode == void_list_node)
2283 break;
2284 else if (parmnode)
2285 {
2286 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2287 /*c_cast_p=*/false, flags, complain);
2288 convert_type = TREE_VALUE (parmnode);
2289 }
2290 else
2291 {
2292 t = build_identity_conv (argtype, arg);
2293 t->ellipsis_p = true;
2294 convert_type = argtype;
2295 }
2296
2297 convs[i] = t;
2298 if (! t)
2299 break;
2300
2301 if (t->bad_p)
2302 {
2303 viable = -1;
2304 reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type);
2305 }
2306
2307 if (i == 0)
2308 continue;
2309
2310 if (parmnode)
2311 parmnode = TREE_CHAIN (parmnode);
2312 }
2313
2314 if (i < len
2315 || ! sufficient_parms_p (parmnode))
2316 {
2317 int remaining = remaining_arguments (parmnode);
2318 viable = 0;
2319 reason = arity_rejection (NULL_TREE, i + remaining, len);
2320 }
2321
2322 return add_candidate (candidates, totype, obj, arglist, len, convs,
2323 access_path, conversion_path, viable, reason, flags);
2324}
2325
2326static void
2327build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2328 tree type1, tree type2, tree *args, tree *argtypes,
2329 int flags, tsubst_flags_t complain)
2330{
2331 conversion *t;
2332 conversion **convs;
2333 size_t num_convs;
2334 int viable = 1, i;
2335 tree types[2];
2336 struct rejection_reason *reason = NULL;
2337
2338 types[0] = type1;
2339 types[1] = type2;
2340
2341 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
2342 convs = alloc_conversions (num_convs);
2343
2344 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2345 conversion ops are allowed. We handle that here by just checking for
2346 boolean_type_node because other operators don't ask for it. COND_EXPR
2347 also does contextual conversion to bool for the first operand, but we
2348 handle that in build_conditional_expr, and type1 here is operand 2. */
2349 if (type1 != boolean_type_node)
2350 flags |= LOOKUP_ONLYCONVERTING;
2351
2352 for (i = 0; i < 2; ++i)
2353 {
2354 if (! args[i])
2355 break;
2356
2357 t = implicit_conversion (types[i], argtypes[i], args[i],
2358 /*c_cast_p=*/false, flags, complain);
2359 if (! t)
2360 {
2361 viable = 0;
2362 /* We need something for printing the candidate. */
2363 t = build_identity_conv (types[i], NULL_TREE);
2364 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2365 types[i]);
2366 }
2367 else if (t->bad_p)
2368 {
2369 viable = 0;
2370 reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2371 types[i]);
2372 }
2373 convs[i] = t;
2374 }
2375
2376 /* For COND_EXPR we rearranged the arguments; undo that now. */
2377 if (args[2])
2378 {
2379 convs[2] = convs[1];
2380 convs[1] = convs[0];
2381 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2382 /*c_cast_p=*/false, flags,
2383 complain);
2384 if (t)
2385 convs[0] = t;
2386 else
2387 {
2388 viable = 0;
2389 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2390 boolean_type_node);
2391 }
2392 }
2393
2394 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2395 num_convs, convs,
2396 /*access_path=*/NULL_TREE,
2397 /*conversion_path=*/NULL_TREE,
2398 viable, reason, flags);
2399}
2400
2401static bool
2402is_complete (tree t)
2403{
2404 return COMPLETE_TYPE_P (complete_type (t));
2405}
2406
2407/* Returns nonzero if TYPE is a promoted arithmetic type. */
2408
2409static bool
2410promoted_arithmetic_type_p (tree type)
2411{
2412 /* [over.built]
2413
2414 In this section, the term promoted integral type is used to refer
2415 to those integral types which are preserved by integral promotion
2416 (including e.g. int and long but excluding e.g. char).
2417 Similarly, the term promoted arithmetic type refers to promoted
2418 integral types plus floating types. */
2419 return ((CP_INTEGRAL_TYPE_P (type)
2420 && same_type_p (type_promotes_to (type), type))
2421 || TREE_CODE (type) == REAL_TYPE);
2422}
2423
2424/* Create any builtin operator overload candidates for the operator in
2425 question given the converted operand types TYPE1 and TYPE2. The other
2426 args are passed through from add_builtin_candidates to
2427 build_builtin_candidate.
2428
2429 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2430 If CODE is requires candidates operands of the same type of the kind
2431 of which TYPE1 and TYPE2 are, we add both candidates
2432 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2433
2434static void
2435add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2436 enum tree_code code2, tree fnname, tree type1,
2437 tree type2, tree *args, tree *argtypes, int flags,
2438 tsubst_flags_t complain)
2439{
2440 switch (code)
2441 {
2442 case POSTINCREMENT_EXPR:
2443 case POSTDECREMENT_EXPR:
2444 args[1] = integer_zero_node;
2445 type2 = integer_type_node;
2446 break;
2447 default:
2448 break;
2449 }
2450
2451 switch (code)
2452 {
2453
2454/* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2455 and VQ is either volatile or empty, there exist candidate operator
2456 functions of the form
2457 VQ T& operator++(VQ T&);
2458 T operator++(VQ T&, int);
2459 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
2460 type other than bool, and VQ is either volatile or empty, there exist
2461 candidate operator functions of the form
2462 VQ T& operator--(VQ T&);
2463 T operator--(VQ T&, int);
2464 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
2465 complete object type, and VQ is either volatile or empty, there exist
2466 candidate operator functions of the form
2467 T*VQ& operator++(T*VQ&);
2468 T*VQ& operator--(T*VQ&);
2469 T* operator++(T*VQ&, int);
2470 T* operator--(T*VQ&, int); */
2471
2472 case POSTDECREMENT_EXPR:
2473 case PREDECREMENT_EXPR:
2474 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2475 return;
2476 /* FALLTHRU */
2477 case POSTINCREMENT_EXPR:
2478 case PREINCREMENT_EXPR:
2479 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2480 {
2481 type1 = build_reference_type (type1);
2482 break;
2483 }
2484 return;
2485
2486/* 7 For every cv-qualified or cv-unqualified object type T, there
2487 exist candidate operator functions of the form
2488
2489 T& operator*(T*);
2490
2491 8 For every function type T, there exist candidate operator functions of
2492 the form
2493 T& operator*(T*); */
2494
2495 case INDIRECT_REF:
2496 if (TYPE_PTR_P (type1)
2497 && (TYPE_PTROB_P (type1)
2498 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2499 break;
2500 return;
2501
2502/* 9 For every type T, there exist candidate operator functions of the form
2503 T* operator+(T*);
2504
2505 10For every promoted arithmetic type T, there exist candidate operator
2506 functions of the form
2507 T operator+(T);
2508 T operator-(T); */
2509
2510 case UNARY_PLUS_EXPR: /* unary + */
2511 if (TYPE_PTR_P (type1))
2512 break;
2513 /* FALLTHRU */
2514 case NEGATE_EXPR:
2515 if (ARITHMETIC_TYPE_P (type1))
2516 break;
2517 return;
2518
2519/* 11For every promoted integral type T, there exist candidate operator
2520 functions of the form
2521 T operator~(T); */
2522
2523 case BIT_NOT_EXPR:
2524 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2525 break;
2526 return;
2527
2528/* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2529 is the same type as C2 or is a derived class of C2, T is a complete
2530 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2531 there exist candidate operator functions of the form
2532 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2533 where CV12 is the union of CV1 and CV2. */
2534
2535 case MEMBER_REF:
2536 if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2537 {
2538 tree c1 = TREE_TYPE (type1);
2539 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2540
2541 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2542 && (TYPE_PTRMEMFUNC_P (type2)
2543 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2544 break;
2545 }
2546 return;
2547
2548/* 13For every pair of promoted arithmetic types L and R, there exist can-
2549 didate operator functions of the form
2550 LR operator*(L, R);
2551 LR operator/(L, R);
2552 LR operator+(L, R);
2553 LR operator-(L, R);
2554 bool operator<(L, R);
2555 bool operator>(L, R);
2556 bool operator<=(L, R);
2557 bool operator>=(L, R);
2558 bool operator==(L, R);
2559 bool operator!=(L, R);
2560 where LR is the result of the usual arithmetic conversions between
2561 types L and R.
2562
2563 14For every pair of types T and I, where T is a cv-qualified or cv-
2564 unqualified complete object type and I is a promoted integral type,
2565 there exist candidate operator functions of the form
2566 T* operator+(T*, I);
2567 T& operator[](T*, I);
2568 T* operator-(T*, I);
2569 T* operator+(I, T*);
2570 T& operator[](I, T*);
2571
2572 15For every T, where T is a pointer to complete object type, there exist
2573 candidate operator functions of the form112)
2574 ptrdiff_t operator-(T, T);
2575
2576 16For every pointer or enumeration type T, there exist candidate operator
2577 functions of the form
2578 bool operator<(T, T);
2579 bool operator>(T, T);
2580 bool operator<=(T, T);
2581 bool operator>=(T, T);
2582 bool operator==(T, T);
2583 bool operator!=(T, T);
2584
2585 17For every pointer to member type T, there exist candidate operator
2586 functions of the form
2587 bool operator==(T, T);
2588 bool operator!=(T, T); */
2589
2590 case MINUS_EXPR:
2591 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2592 break;
2593 if (TYPE_PTROB_P (type1)
2594 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2595 {
2596 type2 = ptrdiff_type_node;
2597 break;
2598 }
2599 /* FALLTHRU */
2600 case MULT_EXPR:
2601 case TRUNC_DIV_EXPR:
2602 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2603 break;
2604 return;
2605
2606 case EQ_EXPR:
2607 case NE_EXPR:
2608 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2609 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
2610 break;
2611 if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
2612 {
2613 type2 = type1;
2614 break;
2615 }
2616 if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
2617 {
2618 type1 = type2;
2619 break;
2620 }
2621 /* Fall through. */
2622 case LT_EXPR:
2623 case GT_EXPR:
2624 case LE_EXPR:
2625 case GE_EXPR:
2626 case MAX_EXPR:
2627 case MIN_EXPR:
2628 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2629 break;
2630 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2631 break;
2632 if (TREE_CODE (type1) == ENUMERAL_TYPE
2633 && TREE_CODE (type2) == ENUMERAL_TYPE)
2634 break;
2635 if (TYPE_PTR_P (type1)
2636 && null_ptr_cst_p (args[1]))
2637 {
2638 type2 = type1;
2639 break;
2640 }
2641 if (null_ptr_cst_p (args[0])
2642 && TYPE_PTR_P (type2))
2643 {
2644 type1 = type2;
2645 break;
2646 }
2647 return;
2648
2649 case PLUS_EXPR:
2650 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2651 break;
2652 /* FALLTHRU */
2653 case ARRAY_REF:
2654 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2655 {
2656 type1 = ptrdiff_type_node;
2657 break;
2658 }
2659 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2660 {
2661 type2 = ptrdiff_type_node;
2662 break;
2663 }
2664 return;
2665
2666/* 18For every pair of promoted integral types L and R, there exist candi-
2667 date operator functions of the form
2668 LR operator%(L, R);
2669 LR operator&(L, R);
2670 LR operator^(L, R);
2671 LR operator|(L, R);
2672 L operator<<(L, R);
2673 L operator>>(L, R);
2674 where LR is the result of the usual arithmetic conversions between
2675 types L and R. */
2676
2677 case TRUNC_MOD_EXPR:
2678 case BIT_AND_EXPR:
2679 case BIT_IOR_EXPR:
2680 case BIT_XOR_EXPR:
2681 case LSHIFT_EXPR:
2682 case RSHIFT_EXPR:
2683 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2684 break;
2685 return;
2686
2687/* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2688 type, VQ is either volatile or empty, and R is a promoted arithmetic
2689 type, there exist candidate operator functions of the form
2690 VQ L& operator=(VQ L&, R);
2691 VQ L& operator*=(VQ L&, R);
2692 VQ L& operator/=(VQ L&, R);
2693 VQ L& operator+=(VQ L&, R);
2694 VQ L& operator-=(VQ L&, R);
2695
2696 20For every pair T, VQ), where T is any type and VQ is either volatile
2697 or empty, there exist candidate operator functions of the form
2698 T*VQ& operator=(T*VQ&, T*);
2699
2700 21For every pair T, VQ), where T is a pointer to member type and VQ is
2701 either volatile or empty, there exist candidate operator functions of
2702 the form
2703 VQ T& operator=(VQ T&, T);
2704
2705 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2706 unqualified complete object type, VQ is either volatile or empty, and
2707 I is a promoted integral type, there exist candidate operator func-
2708 tions of the form
2709 T*VQ& operator+=(T*VQ&, I);
2710 T*VQ& operator-=(T*VQ&, I);
2711
2712 23For every triple L, VQ, R), where L is an integral or enumeration
2713 type, VQ is either volatile or empty, and R is a promoted integral
2714 type, there exist candidate operator functions of the form
2715
2716 VQ L& operator%=(VQ L&, R);
2717 VQ L& operator<<=(VQ L&, R);
2718 VQ L& operator>>=(VQ L&, R);
2719 VQ L& operator&=(VQ L&, R);
2720 VQ L& operator^=(VQ L&, R);
2721 VQ L& operator|=(VQ L&, R); */
2722
2723 case MODIFY_EXPR:
2724 switch (code2)
2725 {
2726 case PLUS_EXPR:
2727 case MINUS_EXPR:
2728 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2729 {
2730 type2 = ptrdiff_type_node;
2731 break;
2732 }
2733 /* FALLTHRU */
2734 case MULT_EXPR:
2735 case TRUNC_DIV_EXPR:
2736 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2737 break;
2738 return;
2739
2740 case TRUNC_MOD_EXPR:
2741 case BIT_AND_EXPR:
2742 case BIT_IOR_EXPR:
2743 case BIT_XOR_EXPR:
2744 case LSHIFT_EXPR:
2745 case RSHIFT_EXPR:
2746 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2747 break;
2748 return;
2749
2750 case NOP_EXPR:
2751 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2752 break;
2753 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2754 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2755 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2756 || ((TYPE_PTRMEMFUNC_P (type1)
2757 || TYPE_PTR_P (type1))
2758 && null_ptr_cst_p (args[1])))
2759 {
2760 type2 = type1;
2761 break;
2762 }
2763 return;
2764
2765 default:
2766 gcc_unreachable ();
2767 }
2768 type1 = build_reference_type (type1);
2769 break;
2770
2771 case COND_EXPR:
2772 /* [over.built]
2773
2774 For every pair of promoted arithmetic types L and R, there
2775 exist candidate operator functions of the form
2776
2777 LR operator?(bool, L, R);
2778
2779 where LR is the result of the usual arithmetic conversions
2780 between types L and R.
2781
2782 For every type T, where T is a pointer or pointer-to-member
2783 type, there exist candidate operator functions of the form T
2784 operator?(bool, T, T); */
2785
2786 if (promoted_arithmetic_type_p (type1)
2787 && promoted_arithmetic_type_p (type2))
2788 /* That's OK. */
2789 break;
2790
2791 /* Otherwise, the types should be pointers. */
2792 if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
2793 return;
2794
2795 /* We don't check that the two types are the same; the logic
2796 below will actually create two candidates; one in which both
2797 parameter types are TYPE1, and one in which both parameter
2798 types are TYPE2. */
2799 break;
2800
2801 case REALPART_EXPR:
2802 case IMAGPART_EXPR:
2803 if (ARITHMETIC_TYPE_P (type1))
2804 break;
2805 return;
2806
2807 default:
2808 gcc_unreachable ();
2809 }
2810
2811 /* Make sure we don't create builtin candidates with dependent types. */
2812 bool u1 = uses_template_parms (type1);
2813 bool u2 = type2 ? uses_template_parms (type2) : false;
2814 if (u1 || u2)
2815 {
2816 /* Try to recover if one of the types is non-dependent. But if
2817 there's only one type, there's nothing we can do. */
2818 if (!type2)
2819 return;
2820 /* And we lose if both are dependent. */
2821 if (u1 && u2)
2822 return;
2823 /* Or if they have different forms. */
2824 if (TREE_CODE (type1) != TREE_CODE (type2))
2825 return;
2826
2827 if (u1 && !u2)
2828 type1 = type2;
2829 else if (u2 && !u1)
2830 type2 = type1;
2831 }
2832
2833 /* If we're dealing with two pointer types or two enumeral types,
2834 we need candidates for both of them. */
2835 if (type2 && !same_type_p (type1, type2)
2836 && TREE_CODE (type1) == TREE_CODE (type2)
2837 && (TREE_CODE (type1) == REFERENCE_TYPE
2838 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2839 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2840 || TYPE_PTRMEMFUNC_P (type1)
2841 || MAYBE_CLASS_TYPE_P (type1)
2842 || TREE_CODE (type1) == ENUMERAL_TYPE))
2843 {
2844 if (TYPE_PTR_OR_PTRMEM_P (type1))
2845 {
2846 tree cptype = composite_pointer_type (type1, type2,
2847 error_mark_node,
2848 error_mark_node,
2849 CPO_CONVERSION,
2850 tf_none);
2851 if (cptype != error_mark_node)
2852 {
2853 build_builtin_candidate
2854 (candidates, fnname, cptype, cptype, args, argtypes,
2855 flags, complain);
2856 return;
2857 }
2858 }
2859
2860 build_builtin_candidate
2861 (candidates, fnname, type1, type1, args, argtypes, flags, complain);
2862 build_builtin_candidate
2863 (candidates, fnname, type2, type2, args, argtypes, flags, complain);
2864 return;
2865 }
2866
2867 build_builtin_candidate
2868 (candidates, fnname, type1, type2, args, argtypes, flags, complain);
2869}
2870
2871tree
2872type_decays_to (tree type)
2873{
2874 if (TREE_CODE (type) == ARRAY_TYPE)
2875 return build_pointer_type (TREE_TYPE (type));
2876 if (TREE_CODE (type) == FUNCTION_TYPE)
2877 return build_pointer_type (type);
2878 return type;
2879}
2880
2881/* There are three conditions of builtin candidates:
2882
2883 1) bool-taking candidates. These are the same regardless of the input.
2884 2) pointer-pair taking candidates. These are generated for each type
2885 one of the input types converts to.
2886 3) arithmetic candidates. According to the standard, we should generate
2887 all of these, but I'm trying not to...
2888
2889 Here we generate a superset of the possible candidates for this particular
2890 case. That is a subset of the full set the standard defines, plus some
2891 other cases which the standard disallows. add_builtin_candidate will
2892 filter out the invalid set. */
2893
2894static void
2895add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2896 enum tree_code code2, tree fnname, tree *args,
2897 int flags, tsubst_flags_t complain)
2898{
2899 int ref1, i;
2900 int enum_p = 0;
2901 tree type, argtypes[3], t;
2902 /* TYPES[i] is the set of possible builtin-operator parameter types
2903 we will consider for the Ith argument. */
2904 vec<tree, va_gc> *types[2];
2905 unsigned ix;
2906
2907 for (i = 0; i < 3; ++i)
2908 {
2909 if (args[i])
2910 argtypes[i] = unlowered_expr_type (args[i]);
2911 else
2912 argtypes[i] = NULL_TREE;
2913 }
2914
2915 switch (code)
2916 {
2917/* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2918 and VQ is either volatile or empty, there exist candidate operator
2919 functions of the form
2920 VQ T& operator++(VQ T&); */
2921
2922 case POSTINCREMENT_EXPR:
2923 case PREINCREMENT_EXPR:
2924 case POSTDECREMENT_EXPR:
2925 case PREDECREMENT_EXPR:
2926 case MODIFY_EXPR:
2927 ref1 = 1;
2928 break;
2929
2930/* 24There also exist candidate operator functions of the form
2931 bool operator!(bool);
2932 bool operator&&(bool, bool);
2933 bool operator||(bool, bool); */
2934
2935 case TRUTH_NOT_EXPR:
2936 build_builtin_candidate
2937 (candidates, fnname, boolean_type_node,
2938 NULL_TREE, args, argtypes, flags, complain);
2939 return;
2940
2941 case TRUTH_ORIF_EXPR:
2942 case TRUTH_ANDIF_EXPR:
2943 build_builtin_candidate
2944 (candidates, fnname, boolean_type_node,
2945 boolean_type_node, args, argtypes, flags, complain);
2946 return;
2947
2948 case ADDR_EXPR:
2949 case COMPOUND_EXPR:
2950 case COMPONENT_REF:
2951 return;
2952
2953 case COND_EXPR:
2954 case EQ_EXPR:
2955 case NE_EXPR:
2956 case LT_EXPR:
2957 case LE_EXPR:
2958 case GT_EXPR:
2959 case GE_EXPR:
2960 enum_p = 1;
2961 /* Fall through. */
2962
2963 default:
2964 ref1 = 0;
2965 }
2966
2967 types[0] = make_tree_vector ();
2968 types[1] = make_tree_vector ();
2969
2970 for (i = 0; i < 2; ++i)
2971 {
2972 if (! args[i])
2973 ;
2974 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
2975 {
2976 tree convs;
2977
2978 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2979 return;
2980
2981 convs = lookup_conversions (argtypes[i]);
2982
2983 if (code == COND_EXPR)
2984 {
2985 if (lvalue_p (args[i]))
2986 vec_safe_push (types[i], build_reference_type (argtypes[i]));
2987
2988 vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
2989 }
2990
2991 else if (! convs)
2992 return;
2993
2994 for (; convs; convs = TREE_CHAIN (convs))
2995 {
2996 type = TREE_TYPE (convs);
2997
2998 if (i == 0 && ref1
2999 && (TREE_CODE (type) != REFERENCE_TYPE
3000 || CP_TYPE_CONST_P (TREE_TYPE (type))))
3001 continue;
3002
3003 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
3004 vec_safe_push (types[i], type);
3005
3006 type = non_reference (type);
3007 if (i != 0 || ! ref1)
3008 {
3009 type = cv_unqualified (type_decays_to (type));
3010 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3011 vec_safe_push (types[i], type);
3012 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3013 type = type_promotes_to (type);
3014 }
3015
3016 if (! vec_member (type, types[i]))
3017 vec_safe_push (types[i], type);
3018 }
3019 }
3020 else
3021 {
3022 if (code == COND_EXPR && lvalue_p (args[i]))
3023 vec_safe_push (types[i], build_reference_type (argtypes[i]));
3024 type = non_reference (argtypes[i]);
3025 if (i != 0 || ! ref1)
3026 {
3027 type = cv_unqualified (type_decays_to (type));
3028 if (enum_p && UNSCOPED_ENUM_P (type))
3029 vec_safe_push (types[i], type);
3030 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3031 type = type_promotes_to (type);
3032 }
3033 vec_safe_push (types[i], type);
3034 }
3035 }
3036
3037 /* Run through the possible parameter types of both arguments,
3038 creating candidates with those parameter types. */
3039 FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3040 {
3041 unsigned jx;
3042 tree u;
3043
3044 if (!types[1]->is_empty ())
3045 FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3046 add_builtin_candidate
3047 (candidates, code, code2, fnname, t,
3048 u, args, argtypes, flags, complain);
3049 else
3050 add_builtin_candidate
3051 (candidates, code, code2, fnname, t,
3052 NULL_TREE, args, argtypes, flags, complain);
3053 }
3054
3055 release_tree_vector (types[0]);
3056 release_tree_vector (types[1]);
3057}
3058
3059
3060/* If TMPL can be successfully instantiated as indicated by
3061 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3062
3063 TMPL is the template. EXPLICIT_TARGS are any explicit template
3064 arguments. ARGLIST is the arguments provided at the call-site.
3065 This does not change ARGLIST. The RETURN_TYPE is the desired type
3066 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3067 as for add_function_candidate. If an OBJ is supplied, FLAGS and
3068 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
3069
3070static struct z_candidate*
3071add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3072 tree ctype, tree explicit_targs, tree first_arg,
3073 const vec<tree, va_gc> *arglist, tree return_type,
3074 tree access_path, tree conversion_path,
3075 int flags, tree obj, unification_kind_t strict,
3076 tsubst_flags_t complain)
3077{
3078 int ntparms = DECL_NTPARMS (tmpl);
3079 tree targs = make_tree_vec (ntparms);
3080 unsigned int len = vec_safe_length (arglist);
3081 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3082 unsigned int skip_without_in_chrg = 0;
3083 tree first_arg_without_in_chrg = first_arg;
3084 tree *args_without_in_chrg;
3085 unsigned int nargs_without_in_chrg;
3086 unsigned int ia, ix;
3087 tree arg;
3088 struct z_candidate *cand;
3089 tree fn;
3090 struct rejection_reason *reason = NULL;
3091 int errs;
3092
3093 /* We don't do deduction on the in-charge parameter, the VTT
3094 parameter or 'this'. */
3095 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
3096 {
3097 if (first_arg_without_in_chrg != NULL_TREE)
3098 first_arg_without_in_chrg = NULL_TREE;
3099 else if (return_type && strict == DEDUCE_CALL)
3100 /* We're deducing for a call to the result of a template conversion
3101 function, so the args don't contain 'this'; leave them alone. */;
3102 else
3103 ++skip_without_in_chrg;
3104 }
3105
3106 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3107 || DECL_BASE_CONSTRUCTOR_P (tmpl))
3108 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3109 {
3110 if (first_arg_without_in_chrg != NULL_TREE)
3111 first_arg_without_in_chrg = NULL_TREE;
3112 else
3113 ++skip_without_in_chrg;
3114 }
3115
3116 if (len < skip_without_in_chrg)
3117 return NULL;
3118
3119 if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3120 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3121 TREE_TYPE ((*arglist)[0])))
3122 {
3123 /* 12.8/6 says, "A declaration of a constructor for a class X is
3124 ill-formed if its first parameter is of type (optionally cv-qualified)
3125 X and either there are no other parameters or else all other
3126 parameters have default arguments. A member function template is never
3127 instantiated to produce such a constructor signature."
3128
3129 So if we're trying to copy an object of the containing class, don't
3130 consider a template constructor that has a first parameter type that
3131 is just a template parameter, as we would deduce a signature that we
3132 would then reject in the code below. */
3133 if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3134 {
3135 firstparm = TREE_VALUE (firstparm);
3136 if (PACK_EXPANSION_P (firstparm))
3137 firstparm = PACK_EXPANSION_PATTERN (firstparm);
3138 if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3139 {
3140 gcc_assert (!explicit_targs);
3141 reason = invalid_copy_with_fn_template_rejection ();
3142 goto fail;
3143 }
3144 }
3145 }
3146
3147 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3148 + (len - skip_without_in_chrg));
3149 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3150 ia = 0;
3151 if (first_arg_without_in_chrg != NULL_TREE)
3152 {
3153 args_without_in_chrg[ia] = first_arg_without_in_chrg;
3154 ++ia;
3155 }
3156 for (ix = skip_without_in_chrg;
3157 vec_safe_iterate (arglist, ix, &arg);
3158 ++ix)
3159 {
3160 args_without_in_chrg[ia] = arg;
3161 ++ia;
3162 }
3163 gcc_assert (ia == nargs_without_in_chrg);
3164
3165 errs = errorcount+sorrycount;
3166 fn = fn_type_unification (tmpl, explicit_targs, targs,
3167 args_without_in_chrg,
3168 nargs_without_in_chrg,
3169 return_type, strict, flags, false,
3170 complain & tf_decltype);
3171
3172 if (fn == error_mark_node)
3173 {
3174 /* Don't repeat unification later if it already resulted in errors. */
3175 if (errorcount+sorrycount == errs)
3176 reason = template_unification_rejection (tmpl, explicit_targs,
3177 targs, args_without_in_chrg,
3178 nargs_without_in_chrg,
3179 return_type, strict, flags);
3180 else
3181 reason = template_unification_error_rejection ();
3182 goto fail;
3183 }
3184
3185 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3186 {
3187 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3188 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3189 ctype))
3190 {
3191 /* We're trying to produce a constructor with a prohibited signature,
3192 as discussed above; handle here any cases we didn't catch then,
3193 such as X(X<T>). */
3194 reason = invalid_copy_with_fn_template_rejection ();
3195 goto fail;
3196 }
3197 }
3198
3199 if (obj != NULL_TREE)
3200 /* Aha, this is a conversion function. */
3201 cand = add_conv_candidate (candidates, fn, obj, arglist,
3202 access_path, conversion_path, complain);
3203 else
3204 cand = add_function_candidate (candidates, fn, ctype,
3205 first_arg, arglist, access_path,
3206 conversion_path, flags, complain);
3207 if (DECL_TI_TEMPLATE (fn) != tmpl)
3208 /* This situation can occur if a member template of a template
3209 class is specialized. Then, instantiate_template might return
3210 an instantiation of the specialization, in which case the
3211 DECL_TI_TEMPLATE field will point at the original
3212 specialization. For example:
3213
3214 template <class T> struct S { template <class U> void f(U);
3215 template <> void f(int) {}; };
3216 S<double> sd;
3217 sd.f(3);
3218
3219 Here, TMPL will be template <class U> S<double>::f(U).
3220 And, instantiate template will give us the specialization
3221 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3222 for this will point at template <class T> template <> S<T>::f(int),
3223 so that we can find the definition. For the purposes of
3224 overload resolution, however, we want the original TMPL. */
3225 cand->template_decl = build_template_info (tmpl, targs);
3226 else
3227 cand->template_decl = DECL_TEMPLATE_INFO (fn);
3228 cand->explicit_targs = explicit_targs;
3229
3230 return cand;
3231 fail:
3232 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
3233 access_path, conversion_path, 0, reason, flags);
3234}
3235
3236
3237static struct z_candidate *
3238add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3239 tree explicit_targs, tree first_arg,
3240 const vec<tree, va_gc> *arglist, tree return_type,
3241 tree access_path, tree conversion_path, int flags,
3242 unification_kind_t strict, tsubst_flags_t complain)
3243{
3244 return
3245 add_template_candidate_real (candidates, tmpl, ctype,
3246 explicit_targs, first_arg, arglist,
3247 return_type, access_path, conversion_path,
3248 flags, NULL_TREE, strict, complain);
3249}
3250
3251/* Create an overload candidate for the conversion function template TMPL,
3252 returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3253 pointer-to-function which will in turn be called with the argument list
3254 ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3255 passed on to implicit_conversion. */
3256
3257static struct z_candidate *
3258add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3259 tree obj,
3260 const vec<tree, va_gc> *arglist,
3261 tree return_type, tree access_path,
3262 tree conversion_path, tsubst_flags_t complain)
3263{
3264 /* Making this work broke PR 71117, so until the committee resolves core
3265 issue 2189, let's disable this candidate if there are any viable call
3266 operators. */
3267 if (any_strictly_viable (*candidates))
3268 return NULL;
3269
3270 return
3271 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3272 NULL_TREE, arglist, return_type, access_path,
3273 conversion_path, 0, obj, DEDUCE_CALL,
3274 complain);
3275}
3276
3277/* The CANDS are the set of candidates that were considered for
3278 overload resolution. Return the set of viable candidates, or CANDS
3279 if none are viable. If any of the candidates were viable, set
3280 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3281 considered viable only if it is strictly viable. */
3282
3283static struct z_candidate*
3284splice_viable (struct z_candidate *cands,
3285 bool strict_p,
3286 bool *any_viable_p)
3287{
3288 struct z_candidate *viable;
3289 struct z_candidate **last_viable;
3290 struct z_candidate **cand;
3291 bool found_strictly_viable = false;
3292
3293 /* Be strict inside templates, since build_over_call won't actually
3294 do the conversions to get pedwarns. */
3295 if (processing_template_decl)
3296 strict_p = true;
3297
3298 viable = NULL;
3299 last_viable = &viable;
3300 *any_viable_p = false;
3301
3302 cand = &cands;
3303 while (*cand)
3304 {
3305 struct z_candidate *c = *cand;
3306 if (!strict_p
3307 && (c->viable == 1 || TREE_CODE (c->fn) == TEMPLATE_DECL))
3308 {
3309 /* Be strict in the presence of a viable candidate. Also if
3310 there are template candidates, so that we get deduction errors
3311 for them instead of silently preferring a bad conversion. */
3312 strict_p = true;
3313 if (viable && !found_strictly_viable)
3314 {
3315 /* Put any spliced near matches back onto the main list so
3316 that we see them if there is no strict match. */
3317 *any_viable_p = false;
3318 *last_viable = cands;
3319 cands = viable;
3320 viable = NULL;
3321 last_viable = &viable;
3322 }
3323 }
3324
3325 if (strict_p ? c->viable == 1 : c->viable)
3326 {
3327 *last_viable = c;
3328 *cand = c->next;
3329 c->next = NULL;
3330 last_viable = &c->next;
3331 *any_viable_p = true;
3332 if (c->viable == 1)
3333 found_strictly_viable = true;
3334 }
3335 else
3336 cand = &c->next;
3337 }
3338
3339 return viable ? viable : cands;
3340}
3341
3342static bool
3343any_strictly_viable (struct z_candidate *cands)
3344{
3345 for (; cands; cands = cands->next)
3346 if (cands->viable == 1)
3347 return true;
3348 return false;
3349}
3350
3351/* OBJ is being used in an expression like "OBJ.f (...)". In other
3352 words, it is about to become the "this" pointer for a member
3353 function call. Take the address of the object. */
3354
3355static tree
3356build_this (tree obj)
3357{
3358 /* In a template, we are only concerned about the type of the
3359 expression, so we can take a shortcut. */
3360 if (processing_template_decl)
3361 return build_address (obj);
3362
3363 return cp_build_addr_expr (obj, tf_warning_or_error);
3364}
3365
3366/* Returns true iff functions are equivalent. Equivalent functions are
3367 not '==' only if one is a function-local extern function or if
3368 both are extern "C". */
3369
3370static inline int
3371equal_functions (tree fn1, tree fn2)
3372{
3373 if (TREE_CODE (fn1) != TREE_CODE (fn2))
3374 return 0;
3375 if (TREE_CODE (fn1) == TEMPLATE_DECL)
3376 return fn1 == fn2;
3377 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
3378 || DECL_EXTERN_C_FUNCTION_P (fn1))
3379 return decls_match (fn1, fn2);
3380 return fn1 == fn2;
3381}
3382
3383/* Print information about a candidate being rejected due to INFO. */
3384
3385static void
3386print_conversion_rejection (location_t loc, struct conversion_info *info)
3387{
3388 tree from = info->from;
3389 if (!TYPE_P (from))
3390 from = lvalue_type (from);
3391 if (info->n_arg == -1)
3392 {
3393 /* Conversion of implicit `this' argument failed. */
3394 if (!TYPE_P (info->from))
3395 /* A bad conversion for 'this' must be discarding cv-quals. */
3396 inform (loc, " passing %qT as %<this%> "
3397 "argument discards qualifiers",
3398 from);
3399 else
3400 inform (loc, " no known conversion for implicit "
3401 "%<this%> parameter from %qH to %qI",
3402 from, info->to_type);
3403 }
3404 else if (!TYPE_P (info->from))
3405 {
3406 if (info->n_arg >= 0)
3407 inform (loc, " conversion of argument %d would be ill-formed:",
3408 info->n_arg + 1);
3409 perform_implicit_conversion (info->to_type, info->from,
3410 tf_warning_or_error);
3411 }
3412 else if (info->n_arg == -2)
3413 /* Conversion of conversion function return value failed. */
3414 inform (loc, " no known conversion from %qH to %qI",
3415 from, info->to_type);
3416 else
3417 inform (loc, " no known conversion for argument %d from %qH to %qI",
3418 info->n_arg + 1, from, info->to_type);
3419}
3420
3421/* Print information about a candidate with WANT parameters and we found
3422 HAVE. */
3423
3424static void
3425print_arity_information (location_t loc, unsigned int have, unsigned int want)
3426{
3427 inform_n (loc, want,
3428 " candidate expects %d argument, %d provided",
3429 " candidate expects %d arguments, %d provided",
3430 want, have);
3431}
3432
3433/* Print information about one overload candidate CANDIDATE. MSGSTR
3434 is the text to print before the candidate itself.
3435
3436 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3437 to have been run through gettext by the caller. This wart makes
3438 life simpler in print_z_candidates and for the translators. */
3439
3440static void
3441print_z_candidate (location_t loc, const char *msgstr,
3442 struct z_candidate *candidate)
3443{
3444 const char *msg = (msgstr == NULL
3445 ? ""
3446 : ACONCAT ((msgstr, " ", NULL)));
3447 tree fn = candidate->fn;
3448 if (flag_new_inheriting_ctors)
3449 fn = strip_inheriting_ctors (fn);
3450 location_t cloc = location_of (fn);
3451
3452 if (identifier_p (fn))
3453 {
3454 cloc = loc;
3455 if (candidate->num_convs == 3)
3456 inform (cloc, "%s%<%D(%T, %T, %T)%> <built-in>", msg, fn,
3457 candidate->convs[0]->type,
3458 candidate->convs[1]->type,
3459 candidate->convs[2]->type);
3460 else if (candidate->num_convs == 2)
3461 inform (cloc, "%s%<%D(%T, %T)%> <built-in>", msg, fn,
3462 candidate->convs[0]->type,
3463 candidate->convs[1]->type);
3464 else
3465 inform (cloc, "%s%<%D(%T)%> <built-in>", msg, fn,
3466 candidate->convs[0]->type);
3467 }
3468 else if (TYPE_P (fn))
3469 inform (cloc, "%s%qT <conversion>", msg, fn);
3470 else if (candidate->viable == -1)
3471 inform (cloc, "%s%#qD <near match>", msg, fn);
3472 else if (DECL_DELETED_FN (fn))
3473 inform (cloc, "%s%#qD <deleted>", msg, fn);
3474 else
3475 inform (cloc, "%s%#qD", msg, fn);
3476 if (fn != candidate->fn)
3477 {
3478 cloc = location_of (candidate->fn);
3479 inform (cloc, " inherited here");
3480 }
3481 /* Give the user some information about why this candidate failed. */
3482 if (candidate->reason != NULL)
3483 {
3484 struct rejection_reason *r = candidate->reason;
3485
3486 switch (r->code)
3487 {
3488 case rr_arity:
3489 print_arity_information (cloc, r->u.arity.actual,
3490 r->u.arity.expected);
3491 break;
3492 case rr_arg_conversion:
3493 print_conversion_rejection (cloc, &r->u.conversion);
3494 break;
3495 case rr_bad_arg_conversion:
3496 print_conversion_rejection (cloc, &r->u.bad_conversion);
3497 break;
3498 case rr_explicit_conversion:
3499 inform (cloc, " return type %qT of explicit conversion function "
3500 "cannot be converted to %qT with a qualification "
3501 "conversion", r->u.conversion.from,
3502 r->u.conversion.to_type);
3503 break;
3504 case rr_template_conversion:
3505 inform (cloc, " conversion from return type %qT of template "
3506 "conversion function specialization to %qT is not an "
3507 "exact match", r->u.conversion.from,
3508 r->u.conversion.to_type);
3509 break;
3510 case rr_template_unification:
3511 /* We use template_unification_error_rejection if unification caused
3512 actual non-SFINAE errors, in which case we don't need to repeat
3513 them here. */
3514 if (r->u.template_unification.tmpl == NULL_TREE)
3515 {
3516 inform (cloc, " substitution of deduced template arguments "
3517 "resulted in errors seen above");
3518 break;
3519 }
3520 /* Re-run template unification with diagnostics. */
3521 inform (cloc, " template argument deduction/substitution failed:");
3522 fn_type_unification (r->u.template_unification.tmpl,
3523 r->u.template_unification.explicit_targs,
3524 (make_tree_vec
3525 (r->u.template_unification.num_targs)),
3526 r->u.template_unification.args,
3527 r->u.template_unification.nargs,
3528 r->u.template_unification.return_type,
3529 r->u.template_unification.strict,
3530 r->u.template_unification.flags,
3531 true, false);
3532 break;
3533 case rr_invalid_copy:
3534 inform (cloc,
3535 " a constructor taking a single argument of its own "
3536 "class type is invalid");
3537 break;
3538 case rr_constraint_failure:
3539 {
3540 tree tmpl = r->u.template_instantiation.tmpl;
3541 tree args = r->u.template_instantiation.targs;
3542 diagnose_constraints (cloc, tmpl, args);
3543 }
3544 break;
3545 case rr_inherited_ctor:
3546 inform (cloc, " an inherited constructor is not a candidate for "
3547 "initialization from an expression of the same or derived "
3548 "type");
3549 break;
3550 case rr_none:
3551 default:
3552 /* This candidate didn't have any issues or we failed to
3553 handle a particular code. Either way... */
3554 gcc_unreachable ();
3555 }
3556 }
3557}
3558
3559static void
3560print_z_candidates (location_t loc, struct z_candidate *candidates)
3561{
3562 struct z_candidate *cand1;
3563 struct z_candidate **cand2;
3564
3565 if (!candidates)
3566 return;
3567
3568 /* Remove non-viable deleted candidates. */
3569 cand1 = candidates;
3570 for (cand2 = &cand1; *cand2; )
3571 {
3572 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
3573 && !(*cand2)->viable
3574 && DECL_DELETED_FN ((*cand2)->fn))
3575 *cand2 = (*cand2)->next;
3576 else
3577 cand2 = &(*cand2)->next;
3578 }
3579 /* ...if there are any non-deleted ones. */
3580 if (cand1)
3581 candidates = cand1;
3582
3583 /* There may be duplicates in the set of candidates. We put off
3584 checking this condition as long as possible, since we have no way
3585 to eliminate duplicates from a set of functions in less than n^2
3586 time. Now we are about to emit an error message, so it is more
3587 permissible to go slowly. */
3588 for (cand1 = candidates; cand1; cand1 = cand1->next)
3589 {
3590 tree fn = cand1->fn;
3591 /* Skip builtin candidates and conversion functions. */
3592 if (!DECL_P (fn))
3593 continue;
3594 cand2 = &cand1->next;
3595 while (*cand2)
3596 {
3597 if (DECL_P ((*cand2)->fn)
3598 && equal_functions (fn, (*cand2)->fn))
3599 *cand2 = (*cand2)->next;
3600 else
3601 cand2 = &(*cand2)->next;
3602 }
3603 }
3604
3605 for (; candidates; candidates = candidates->next)
3606 print_z_candidate (loc, "candidate:", candidates);
3607}
3608
3609/* USER_SEQ is a user-defined conversion sequence, beginning with a
3610 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3611 the result of the conversion function to convert it to the final
3612 desired type. Merge the two sequences into a single sequence,
3613 and return the merged sequence. */
3614
3615static conversion *
3616merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
3617{
3618 conversion **t;
3619 bool bad = user_seq->bad_p;
3620
3621 gcc_assert (user_seq->kind == ck_user);
3622
3623 /* Find the end of the second conversion sequence. */
3624 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
3625 {
3626 /* The entire sequence is a user-conversion sequence. */
3627 (*t)->user_conv_p = true;
3628 if (bad)
3629 (*t)->bad_p = true;
3630 }
3631
3632 /* Replace the identity conversion with the user conversion
3633 sequence. */
3634 *t = user_seq;
3635
3636 return std_seq;
3637}
3638
3639/* Handle overload resolution for initializing an object of class type from
3640 an initializer list. First we look for a suitable constructor that
3641 takes a std::initializer_list; if we don't find one, we then look for a
3642 non-list constructor.
3643
3644 Parameters are as for add_candidates, except that the arguments are in
3645 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
3646 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3647
3648static void
3649add_list_candidates (tree fns, tree first_arg,
3650 const vec<tree, va_gc> *args, tree totype,
3651 tree explicit_targs, bool template_only,
3652 tree conversion_path, tree access_path,
3653 int flags,
3654 struct z_candidate **candidates,
3655 tsubst_flags_t complain)
3656{
3657 gcc_assert (*candidates == NULL);
3658
3659 /* We're looking for a ctor for list-initialization. */
3660 flags |= LOOKUP_LIST_INIT_CTOR;
3661 /* And we don't allow narrowing conversions. We also use this flag to
3662 avoid the copy constructor call for copy-list-initialization. */
3663 flags |= LOOKUP_NO_NARROWING;
3664
3665 unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
3666 tree init_list = (*args)[nart];
3667
3668 /* Always use the default constructor if the list is empty (DR 990). */
3669 if (CONSTRUCTOR_NELTS (init_list) == 0
3670 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
3671 ;
3672 /* If the class has a list ctor, try passing the list as a single
3673 argument first, but only consider list ctors. */
3674 else if (TYPE_HAS_LIST_CTOR (totype))
3675 {
3676 flags |= LOOKUP_LIST_ONLY;
3677 add_candidates (fns, first_arg, args, NULL_TREE,
3678 explicit_targs, template_only, conversion_path,
3679 access_path, flags, candidates, complain);
3680 if (any_strictly_viable (*candidates))
3681 return;
3682 }
3683
3684 /* Expand the CONSTRUCTOR into a new argument vec. */
3685 vec<tree, va_gc> *new_args;
3686 vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
3687 for (unsigned i = 0; i < nart; ++i)
3688 new_args->quick_push ((*args)[i]);
3689 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (init_list); ++i)
3690 new_args->quick_push (CONSTRUCTOR_ELT (init_list, i)->value);
3691
3692 /* We aren't looking for list-ctors anymore. */
3693 flags &= ~LOOKUP_LIST_ONLY;
3694 /* We allow more user-defined conversions within an init-list. */
3695 flags &= ~LOOKUP_NO_CONVERSION;
3696
3697 add_candidates (fns, first_arg, new_args, NULL_TREE,
3698 explicit_targs, template_only, conversion_path,
3699 access_path, flags, candidates, complain);
3700}
3701
3702/* Returns the best overload candidate to perform the requested
3703 conversion. This function is used for three the overloading situations
3704 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3705 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3706 per [dcl.init.ref], so we ignore temporary bindings. */
3707
3708static struct z_candidate *
3709build_user_type_conversion_1 (tree totype, tree expr, int flags,
3710 tsubst_flags_t complain)
3711{
3712 struct z_candidate *candidates, *cand;
3713 tree fromtype;
3714 tree ctors = NULL_TREE;
3715 tree conv_fns = NULL_TREE;
3716 conversion *conv = NULL;
3717 tree first_arg = NULL_TREE;
3718 vec<tree, va_gc> *args = NULL;
3719 bool any_viable_p;
3720 int convflags;
3721
3722 if (!expr)
3723 return NULL;
3724
3725 fromtype = TREE_TYPE (expr);
3726
3727 /* We represent conversion within a hierarchy using RVALUE_CONV and
3728 BASE_CONV, as specified by [over.best.ics]; these become plain
3729 constructor calls, as specified in [dcl.init]. */
3730 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
3731 || !DERIVED_FROM_P (totype, fromtype));
3732
3733 if (CLASS_TYPE_P (totype))
3734 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
3735 creating a garbage BASELINK; constructors can't be inherited. */
3736 ctors = get_class_binding (totype, complete_ctor_identifier);
3737
3738 /* FIXME P0135 doesn't say what to do in C++17 about list-initialization from
3739 a single element. For now, let's handle constructors as before and also
3740 consider conversion operators from the element. */
3741 if (cxx_dialect >= cxx17
3742 && BRACE_ENCLOSED_INITIALIZER_P (expr)
3743 && CONSTRUCTOR_NELTS (expr) == 1)
3744 fromtype = TREE_TYPE (CONSTRUCTOR_ELT (expr, 0)->value);
3745
3746 if (MAYBE_CLASS_TYPE_P (fromtype))
3747 {
3748 tree to_nonref = non_reference (totype);
3749 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
3750 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
3751 && DERIVED_FROM_P (to_nonref, fromtype)))
3752 {
3753 /* [class.conv.fct] A conversion function is never used to
3754 convert a (possibly cv-qualified) object to the (possibly
3755 cv-qualified) same object type (or a reference to it), to a
3756 (possibly cv-qualified) base class of that type (or a
3757 reference to it)... */
3758 }
3759 else
3760 conv_fns = lookup_conversions (fromtype);
3761 }
3762
3763 candidates = 0;
3764 flags |= LOOKUP_NO_CONVERSION;
3765 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3766 flags |= LOOKUP_NO_NARROWING;
3767
3768 /* It's OK to bind a temporary for converting constructor arguments, but
3769 not in converting the return value of a conversion operator. */
3770 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
3771 | (flags & LOOKUP_NO_NARROWING));
3772 flags &= ~LOOKUP_NO_TEMP_BIND;
3773
3774 if (ctors)
3775 {
3776 int ctorflags = flags;
3777
3778 first_arg = build_dummy_object (totype);
3779
3780 /* We should never try to call the abstract or base constructor
3781 from here. */
3782 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
3783 && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
3784
3785 args = make_tree_vector_single (expr);
3786 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3787 {
3788 /* List-initialization. */
3789 add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
3790 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
3791 ctorflags, &candidates, complain);
3792 }
3793 else
3794 {
3795 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
3796 TYPE_BINFO (totype), TYPE_BINFO (totype),
3797 ctorflags, &candidates, complain);
3798 }
3799
3800 for (cand = candidates; cand; cand = cand->next)
3801 {
3802 cand->second_conv = build_identity_conv (totype, NULL_TREE);
3803
3804 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
3805 set, then this is copy-initialization. In that case, "The
3806 result of the call is then used to direct-initialize the
3807 object that is the destination of the copy-initialization."
3808 [dcl.init]
3809
3810 We represent this in the conversion sequence with an
3811 rvalue conversion, which means a constructor call. */
3812 if (TREE_CODE (totype) != REFERENCE_TYPE
3813 && !(convflags & LOOKUP_NO_TEMP_BIND))
3814 cand->second_conv
3815 = build_conv (ck_rvalue, totype, cand->second_conv);
3816 }
3817 }
3818
3819 if (conv_fns)
3820 {
3821 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3822 /* FIXME see above about C++17. */
3823 first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
3824 else
3825 first_arg = expr;
3826 }
3827
3828 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
3829 {
3830 tree conversion_path = TREE_PURPOSE (conv_fns);
3831 struct z_candidate *old_candidates;
3832
3833 /* If we are called to convert to a reference type, we are trying to
3834 find a direct binding, so don't even consider temporaries. If
3835 we don't find a direct binding, the caller will try again to
3836 look for a temporary binding. */
3837 if (TREE_CODE (totype) == REFERENCE_TYPE)
3838 convflags |= LOOKUP_NO_TEMP_BIND;
3839
3840 old_candidates = candidates;
3841 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3842 NULL_TREE, false,
3843 conversion_path, TYPE_BINFO (fromtype),
3844 flags, &candidates, complain);
3845
3846 for (cand = candidates; cand != old_candidates; cand = cand->next)
3847 {
3848 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
3849 conversion *ics
3850 = implicit_conversion (totype,
3851 rettype,
3852 0,
3853 /*c_cast_p=*/false, convflags,
3854 complain);
3855
3856 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3857 copy-initialization. In that case, "The result of the
3858 call is then used to direct-initialize the object that is
3859 the destination of the copy-initialization." [dcl.init]
3860
3861 We represent this in the conversion sequence with an
3862 rvalue conversion, which means a constructor call. But
3863 don't add a second rvalue conversion if there's already
3864 one there. Which there really shouldn't be, but it's
3865 harmless since we'd add it here anyway. */
3866 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3867 && !(convflags & LOOKUP_NO_TEMP_BIND))
3868 ics = build_conv (ck_rvalue, totype, ics);
3869
3870 cand->second_conv = ics;
3871
3872 if (!ics)
3873 {
3874 cand->viable = 0;
3875 cand->reason = arg_conversion_rejection (NULL_TREE, -2,
3876 rettype, totype);
3877 }
3878 else if (DECL_NONCONVERTING_P (cand->fn)
3879 && ics->rank > cr_exact)
3880 {
3881 /* 13.3.1.5: For direct-initialization, those explicit
3882 conversion functions that are not hidden within S and
3883 yield type T or a type that can be converted to type T
3884 with a qualification conversion (4.4) are also candidate
3885 functions. */
3886 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
3887 I've raised this issue with the committee. --jason 9/2011 */
3888 cand->viable = -1;
3889 cand->reason = explicit_conversion_rejection (rettype, totype);
3890 }
3891 else if (cand->viable == 1 && ics->bad_p)
3892 {
3893 cand->viable = -1;
3894 cand->reason
3895 = bad_arg_conversion_rejection (NULL_TREE, -2,
3896 rettype, totype);
3897 }
3898 else if (primary_template_specialization_p (cand->fn)
3899 && ics->rank > cr_exact)
3900 {
3901 /* 13.3.3.1.2: If the user-defined conversion is specified by
3902 a specialization of a conversion function template, the
3903 second standard conversion sequence shall have exact match
3904 rank. */
3905 cand->viable = -1;
3906 cand->reason = template_conversion_rejection (rettype, totype);
3907 }
3908 }
3909 }
3910
3911 candidates = splice_viable (candidates, false, &any_viable_p);
3912 if (!any_viable_p)
3913 {
3914 if (args)
3915 release_tree_vector (args);
3916 return NULL;
3917 }
3918
3919 cand = tourney (candidates, complain);
3920 if (cand == 0)
3921 {
3922 if (complain & tf_error)
3923 {
3924 error ("conversion from %qH to %qI is ambiguous",
3925 fromtype, totype);
3926 print_z_candidates (location_of (expr), candidates);
3927 }
3928
3929 cand = candidates; /* any one will do */
3930 cand->second_conv = build_ambiguous_conv (totype, expr);
3931 cand->second_conv->user_conv_p = true;
3932 if (!any_strictly_viable (candidates))
3933 cand->second_conv->bad_p = true;
3934 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3935 ambiguous conversion is no worse than another user-defined
3936 conversion. */
3937
3938 return cand;
3939 }
3940
3941 tree convtype;
3942 if (!DECL_CONSTRUCTOR_P (cand->fn))
3943 convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
3944 else if (cand->second_conv->kind == ck_rvalue)
3945 /* DR 5: [in the first step of copy-initialization]...if the function
3946 is a constructor, the call initializes a temporary of the
3947 cv-unqualified version of the destination type. */
3948 convtype = cv_unqualified (totype);
3949 else
3950 convtype = totype;
3951 /* Build the user conversion sequence. */
3952 conv = build_conv
3953 (ck_user,
3954 convtype,
3955 build_identity_conv (TREE_TYPE (expr), expr));
3956 conv->cand = cand;
3957 if (cand->viable == -1)
3958 conv->bad_p = true;
3959
3960 /* Remember that this was a list-initialization. */
3961 if (flags & LOOKUP_NO_NARROWING)
3962 conv->check_narrowing = true;
3963
3964 /* Combine it with the second conversion sequence. */
3965 cand->second_conv = merge_conversion_sequences (conv,
3966 cand->second_conv);
3967
3968 return cand;
3969}
3970
3971/* Wrapper for above. */
3972
3973tree
3974build_user_type_conversion (tree totype, tree expr, int flags,
3975 tsubst_flags_t complain)
3976{
3977 struct z_candidate *cand;
3978 tree ret;
3979
3980 bool subtime = timevar_cond_start (TV_OVERLOAD);
3981 cand = build_user_type_conversion_1 (totype, expr, flags, complain);
3982
3983 if (cand)
3984 {
3985 if (cand->second_conv->kind == ck_ambig)
3986 ret = error_mark_node;
3987 else
3988 {
3989 expr = convert_like (cand->second_conv, expr, complain);
3990 ret = convert_from_reference (expr);
3991 }
3992 }
3993 else
3994 ret = NULL_TREE;
3995
3996 timevar_cond_stop (TV_OVERLOAD, subtime);
3997 return ret;
3998}
3999
4000/* Subroutine of convert_nontype_argument.
4001
4002 EXPR is an expression used in a context that requires a converted
4003 constant-expression, such as a template non-type parameter. Do any
4004 necessary conversions (that are permitted for converted
4005 constant-expressions) to convert it to the desired type.
4006
4007 If conversion is successful, returns the converted expression;
4008 otherwise, returns error_mark_node. */
4009
4010tree
4011build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
4012{
4013 conversion *conv;
4014 void *p;
4015 tree t;
4016 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
4017
4018 if (error_operand_p (expr))
4019 return error_mark_node;
4020
4021 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4022 p = conversion_obstack_alloc (0);
4023
4024 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4025 /*c_cast_p=*/false,
4026 LOOKUP_IMPLICIT, complain);
4027
4028 /* A converted constant expression of type T is an expression, implicitly
4029 converted to type T, where the converted expression is a constant
4030 expression and the implicit conversion sequence contains only
4031
4032 * user-defined conversions,
4033 * lvalue-to-rvalue conversions (7.1),
4034 * array-to-pointer conversions (7.2),
4035 * function-to-pointer conversions (7.3),
4036 * qualification conversions (7.5),
4037 * integral promotions (7.6),
4038 * integral conversions (7.8) other than narrowing conversions (11.6.4),
4039 * null pointer conversions (7.11) from std::nullptr_t,
4040 * null member pointer conversions (7.12) from std::nullptr_t, and
4041 * function pointer conversions (7.13),
4042
4043 and where the reference binding (if any) binds directly. */
4044
4045 for (conversion *c = conv;
4046 conv && c->kind != ck_identity;
4047 c = next_conversion (c))
4048 {
4049 switch (c->kind)
4050 {
4051 /* A conversion function is OK. If it isn't constexpr, we'll
4052 complain later that the argument isn't constant. */
4053 case ck_user:
4054 /* The lvalue-to-rvalue conversion is OK. */
4055 case ck_rvalue:
4056 /* Array-to-pointer and function-to-pointer. */
4057 case ck_lvalue:
4058 /* Function pointer conversions. */
4059 case ck_fnptr:
4060 /* Qualification conversions. */
4061 case ck_qual:
4062 break;
4063
4064 case ck_ref_bind:
4065 if (c->need_temporary_p)
4066 {
4067 if (complain & tf_error)
4068 error_at (loc, "initializing %qH with %qI in converted "
4069 "constant expression does not bind directly",
4070 type, next_conversion (c)->type);
4071 conv = NULL;
4072 }
4073 break;
4074
4075 case ck_base:
4076 case ck_pmem:
4077 case ck_ptr:
4078 case ck_std:
4079 t = next_conversion (c)->type;
4080 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
4081 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4082 /* Integral promotion or conversion. */
4083 break;
4084 if (NULLPTR_TYPE_P (t))
4085 /* Conversion from nullptr to pointer or pointer-to-member. */
4086 break;
4087
4088 if (complain & tf_error)
4089 error_at (loc, "conversion from %qH to %qI in a "
4090 "converted constant expression", t, type);
4091 /* fall through. */
4092
4093 default:
4094