]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/call.c
d899d45ec6dbc1c48a5dd1a9d28126efe9288c6b
[thirdparty/gcc.git] / gcc / cp / call.c
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
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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
47 enum 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
67 enum conversion_rank {
68 cr_identity,
69 cr_exact,
70 cr_promotion,
71 cr_std,
72 cr_pbool,
73 cr_user,
74 cr_ellipsis,
75 cr_bad
76 };
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
82 struct 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
141 static struct obstack conversion_obstack;
142 static bool conversion_obstack_initialized;
143 struct rejection_reason;
144
145 static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
146 static int equal_functions (tree, tree);
147 static int joust (struct z_candidate *, struct z_candidate *, bool,
148 tsubst_flags_t);
149 static int compare_ics (conversion *, conversion *);
150 static 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))
159 static tree convert_like_real (conversion *, tree, tree, int, bool,
160 bool, tsubst_flags_t);
161 static void op_error (location_t, enum tree_code, enum tree_code, tree,
162 tree, tree, bool);
163 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
164 tsubst_flags_t);
165 static void print_z_candidate (location_t, const char *, struct z_candidate *);
166 static void print_z_candidates (location_t, struct z_candidate *);
167 static tree build_this (tree);
168 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
169 static bool any_strictly_viable (struct z_candidate *);
170 static 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);
173 static 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);
176 static void add_builtin_candidates
177 (struct z_candidate **, enum tree_code, enum tree_code,
178 tree, tree *, int, tsubst_flags_t);
179 static void add_builtin_candidate
180 (struct z_candidate **, enum tree_code, enum tree_code,
181 tree, tree, tree, tree *, tree *, int, tsubst_flags_t);
182 static bool is_complete (tree);
183 static void build_builtin_candidate
184 (struct z_candidate **, tree, tree, tree, tree *, tree *,
185 int, tsubst_flags_t);
186 static struct z_candidate *add_conv_candidate
187 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, tree,
188 tree, tsubst_flags_t);
189 static 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);
192 static conversion *implicit_conversion (tree, tree, tree, bool, int,
193 tsubst_flags_t);
194 static conversion *reference_binding (tree, tree, tree, bool, int,
195 tsubst_flags_t);
196 static conversion *build_conv (conversion_kind, tree, conversion *);
197 static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
198 static conversion *next_conversion (conversion *);
199 static bool is_subseq (conversion *, conversion *);
200 static conversion *maybe_handle_ref_bind (conversion **);
201 static void maybe_handle_implicit_object (conversion **);
202 static 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);
205 static tree source_type (conversion *);
206 static void add_warning (struct z_candidate *, struct z_candidate *);
207 static bool reference_compatible_p (tree, tree);
208 static conversion *direct_reference_binding (tree, conversion *);
209 static bool promoted_arithmetic_type_p (tree);
210 static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
211 static char *name_as_c_string (tree, tree, bool *);
212 static tree prep_operand (tree);
213 static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
214 bool, tree, tree, int, struct z_candidate **,
215 tsubst_flags_t);
216 static conversion *merge_conversion_sequences (conversion *, conversion *);
217 static 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
222 bool
223 check_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
263 tree
264 build_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
293 tree
294 build_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
315 void
316 set_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
338 tree
339 build_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 if (current_function_decl && decl
380 && flag_new_inheriting_ctors
381 && DECL_INHERITED_CTOR (current_function_decl)
382 && (DECL_INHERITED_CTOR (current_function_decl)
383 == DECL_CLONED_FUNCTION (decl)))
384 /* Pass arguments directly to the inherited constructor. */
385 CALL_FROM_THUNK_P (function) = true;
386
387 /* Don't pass empty class objects by value. This is useful
388 for tags in STL, which are used to control overload resolution.
389 We don't need to handle other cases of copying empty classes. */
390 else if (! decl || ! DECL_BUILT_IN (decl))
391 for (i = 0; i < n; i++)
392 {
393 tree arg = CALL_EXPR_ARG (function, i);
394 if (is_empty_class (TREE_TYPE (arg))
395 && ! TREE_ADDRESSABLE (TREE_TYPE (arg)))
396 {
397 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
398 arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
399 CALL_EXPR_ARG (function, i) = arg;
400 }
401 }
402
403 return function;
404 }
405
406 /* New overloading code. */
407
408 struct z_candidate;
409
410 struct candidate_warning {
411 z_candidate *loser;
412 candidate_warning *next;
413 };
414
415 /* Information for providing diagnostics about why overloading failed. */
416
417 enum rejection_reason_code {
418 rr_none,
419 rr_arity,
420 rr_explicit_conversion,
421 rr_template_conversion,
422 rr_arg_conversion,
423 rr_bad_arg_conversion,
424 rr_template_unification,
425 rr_invalid_copy,
426 rr_inherited_ctor,
427 rr_constraint_failure
428 };
429
430 struct conversion_info {
431 /* The index of the argument, 0-based. */
432 int n_arg;
433 /* The actual argument or its type. */
434 tree from;
435 /* The type of the parameter. */
436 tree to_type;
437 };
438
439 struct rejection_reason {
440 enum rejection_reason_code code;
441 union {
442 /* Information about an arity mismatch. */
443 struct {
444 /* The expected number of arguments. */
445 int expected;
446 /* The actual number of arguments in the call. */
447 int actual;
448 /* Whether the call was a varargs call. */
449 bool call_varargs_p;
450 } arity;
451 /* Information about an argument conversion mismatch. */
452 struct conversion_info conversion;
453 /* Same, but for bad argument conversions. */
454 struct conversion_info bad_conversion;
455 /* Information about template unification failures. These are the
456 parameters passed to fn_type_unification. */
457 struct {
458 tree tmpl;
459 tree explicit_targs;
460 int num_targs;
461 const tree *args;
462 unsigned int nargs;
463 tree return_type;
464 unification_kind_t strict;
465 int flags;
466 } template_unification;
467 /* Information about template instantiation failures. These are the
468 parameters passed to instantiate_template. */
469 struct {
470 tree tmpl;
471 tree targs;
472 } template_instantiation;
473 } u;
474 };
475
476 struct z_candidate {
477 /* The FUNCTION_DECL that will be called if this candidate is
478 selected by overload resolution. */
479 tree fn;
480 /* If not NULL_TREE, the first argument to use when calling this
481 function. */
482 tree first_arg;
483 /* The rest of the arguments to use when calling this function. If
484 there are no further arguments this may be NULL or it may be an
485 empty vector. */
486 const vec<tree, va_gc> *args;
487 /* The implicit conversion sequences for each of the arguments to
488 FN. */
489 conversion **convs;
490 /* The number of implicit conversion sequences. */
491 size_t num_convs;
492 /* If FN is a user-defined conversion, the standard conversion
493 sequence from the type returned by FN to the desired destination
494 type. */
495 conversion *second_conv;
496 struct rejection_reason *reason;
497 /* If FN is a member function, the binfo indicating the path used to
498 qualify the name of FN at the call site. This path is used to
499 determine whether or not FN is accessible if it is selected by
500 overload resolution. The DECL_CONTEXT of FN will always be a
501 (possibly improper) base of this binfo. */
502 tree access_path;
503 /* If FN is a non-static member function, the binfo indicating the
504 subobject to which the `this' pointer should be converted if FN
505 is selected by overload resolution. The type pointed to by
506 the `this' pointer must correspond to the most derived class
507 indicated by the CONVERSION_PATH. */
508 tree conversion_path;
509 tree template_decl;
510 tree explicit_targs;
511 candidate_warning *warnings;
512 z_candidate *next;
513 int viable;
514
515 /* The flags active in add_candidate. */
516 int flags;
517 };
518
519 /* Returns true iff T is a null pointer constant in the sense of
520 [conv.ptr]. */
521
522 bool
523 null_ptr_cst_p (tree t)
524 {
525 tree type = TREE_TYPE (t);
526
527 /* [conv.ptr]
528
529 A null pointer constant is an integral constant expression
530 (_expr.const_) rvalue of integer type that evaluates to zero or
531 an rvalue of type std::nullptr_t. */
532 if (NULLPTR_TYPE_P (type))
533 return true;
534
535 if (cxx_dialect >= cxx11)
536 {
537 /* Core issue 903 says only literal 0 is a null pointer constant. */
538 if (TREE_CODE (type) == INTEGER_TYPE
539 && !char_type_p (type)
540 && TREE_CODE (t) == INTEGER_CST
541 && integer_zerop (t)
542 && !TREE_OVERFLOW (t))
543 return true;
544 }
545 else if (CP_INTEGRAL_TYPE_P (type))
546 {
547 t = fold_non_dependent_expr (t);
548 STRIP_NOPS (t);
549 if (integer_zerop (t) && !TREE_OVERFLOW (t))
550 return true;
551 }
552
553 return false;
554 }
555
556 /* Returns true iff T is a null member pointer value (4.11). */
557
558 bool
559 null_member_pointer_value_p (tree t)
560 {
561 tree type = TREE_TYPE (t);
562 if (!type)
563 return false;
564 else if (TYPE_PTRMEMFUNC_P (type))
565 return (TREE_CODE (t) == CONSTRUCTOR
566 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
567 else if (TYPE_PTRDATAMEM_P (type))
568 return integer_all_onesp (t);
569 else
570 return false;
571 }
572
573 /* Returns nonzero if PARMLIST consists of only default parms,
574 ellipsis, and/or undeduced parameter packs. */
575
576 bool
577 sufficient_parms_p (const_tree parmlist)
578 {
579 for (; parmlist && parmlist != void_list_node;
580 parmlist = TREE_CHAIN (parmlist))
581 if (!TREE_PURPOSE (parmlist)
582 && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
583 return false;
584 return true;
585 }
586
587 /* Allocate N bytes of memory from the conversion obstack. The memory
588 is zeroed before being returned. */
589
590 static void *
591 conversion_obstack_alloc (size_t n)
592 {
593 void *p;
594 if (!conversion_obstack_initialized)
595 {
596 gcc_obstack_init (&conversion_obstack);
597 conversion_obstack_initialized = true;
598 }
599 p = obstack_alloc (&conversion_obstack, n);
600 memset (p, 0, n);
601 return p;
602 }
603
604 /* Allocate rejection reasons. */
605
606 static struct rejection_reason *
607 alloc_rejection (enum rejection_reason_code code)
608 {
609 struct rejection_reason *p;
610 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
611 p->code = code;
612 return p;
613 }
614
615 static struct rejection_reason *
616 arity_rejection (tree first_arg, int expected, int actual)
617 {
618 struct rejection_reason *r = alloc_rejection (rr_arity);
619 int adjust = first_arg != NULL_TREE;
620 r->u.arity.expected = expected - adjust;
621 r->u.arity.actual = actual - adjust;
622 return r;
623 }
624
625 static struct rejection_reason *
626 arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
627 {
628 struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
629 int adjust = first_arg != NULL_TREE;
630 r->u.conversion.n_arg = n_arg - adjust;
631 r->u.conversion.from = from;
632 r->u.conversion.to_type = to;
633 return r;
634 }
635
636 static struct rejection_reason *
637 bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
638 {
639 struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
640 int adjust = first_arg != NULL_TREE;
641 r->u.bad_conversion.n_arg = n_arg - adjust;
642 r->u.bad_conversion.from = from;
643 r->u.bad_conversion.to_type = to;
644 return r;
645 }
646
647 static struct rejection_reason *
648 explicit_conversion_rejection (tree from, tree to)
649 {
650 struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
651 r->u.conversion.n_arg = 0;
652 r->u.conversion.from = from;
653 r->u.conversion.to_type = to;
654 return r;
655 }
656
657 static struct rejection_reason *
658 template_conversion_rejection (tree from, tree to)
659 {
660 struct rejection_reason *r = alloc_rejection (rr_template_conversion);
661 r->u.conversion.n_arg = 0;
662 r->u.conversion.from = from;
663 r->u.conversion.to_type = to;
664 return r;
665 }
666
667 static struct rejection_reason *
668 template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
669 const tree *args, unsigned int nargs,
670 tree return_type, unification_kind_t strict,
671 int flags)
672 {
673 size_t args_n_bytes = sizeof (*args) * nargs;
674 tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
675 struct rejection_reason *r = alloc_rejection (rr_template_unification);
676 r->u.template_unification.tmpl = tmpl;
677 r->u.template_unification.explicit_targs = explicit_targs;
678 r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
679 /* Copy args to our own storage. */
680 memcpy (args1, args, args_n_bytes);
681 r->u.template_unification.args = args1;
682 r->u.template_unification.nargs = nargs;
683 r->u.template_unification.return_type = return_type;
684 r->u.template_unification.strict = strict;
685 r->u.template_unification.flags = flags;
686 return r;
687 }
688
689 static struct rejection_reason *
690 template_unification_error_rejection (void)
691 {
692 return alloc_rejection (rr_template_unification);
693 }
694
695 static struct rejection_reason *
696 invalid_copy_with_fn_template_rejection (void)
697 {
698 struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
699 return r;
700 }
701
702 static struct rejection_reason *
703 inherited_ctor_rejection (void)
704 {
705 struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
706 return r;
707 }
708
709 // Build a constraint failure record, saving information into the
710 // template_instantiation field of the rejection. If FN is not a template
711 // declaration, the TMPL member is the FN declaration and TARGS is empty.
712
713 static struct rejection_reason *
714 constraint_failure (tree fn)
715 {
716 struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
717 if (tree ti = DECL_TEMPLATE_INFO (fn))
718 {
719 r->u.template_instantiation.tmpl = TI_TEMPLATE (ti);
720 r->u.template_instantiation.targs = TI_ARGS (ti);
721 }
722 else
723 {
724 r->u.template_instantiation.tmpl = fn;
725 r->u.template_instantiation.targs = NULL_TREE;
726 }
727 return r;
728 }
729
730 /* Dynamically allocate a conversion. */
731
732 static conversion *
733 alloc_conversion (conversion_kind kind)
734 {
735 conversion *c;
736 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
737 c->kind = kind;
738 return c;
739 }
740
741 /* Make sure that all memory on the conversion obstack has been
742 freed. */
743
744 void
745 validate_conversion_obstack (void)
746 {
747 if (conversion_obstack_initialized)
748 gcc_assert ((obstack_next_free (&conversion_obstack)
749 == obstack_base (&conversion_obstack)));
750 }
751
752 /* Dynamically allocate an array of N conversions. */
753
754 static conversion **
755 alloc_conversions (size_t n)
756 {
757 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
758 }
759
760 static conversion *
761 build_conv (conversion_kind code, tree type, conversion *from)
762 {
763 conversion *t;
764 conversion_rank rank = CONVERSION_RANK (from);
765
766 /* Note that the caller is responsible for filling in t->cand for
767 user-defined conversions. */
768 t = alloc_conversion (code);
769 t->type = type;
770 t->u.next = from;
771
772 switch (code)
773 {
774 case ck_ptr:
775 case ck_pmem:
776 case ck_base:
777 case ck_std:
778 if (rank < cr_std)
779 rank = cr_std;
780 break;
781
782 case ck_qual:
783 case ck_fnptr:
784 if (rank < cr_exact)
785 rank = cr_exact;
786 break;
787
788 default:
789 break;
790 }
791 t->rank = rank;
792 t->user_conv_p = (code == ck_user || from->user_conv_p);
793 t->bad_p = from->bad_p;
794 t->base_p = false;
795 return t;
796 }
797
798 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
799 specialization of std::initializer_list<T>, if such a conversion is
800 possible. */
801
802 static conversion *
803 build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
804 {
805 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
806 unsigned len = CONSTRUCTOR_NELTS (ctor);
807 conversion **subconvs = alloc_conversions (len);
808 conversion *t;
809 unsigned i;
810 tree val;
811
812 /* Within a list-initialization we can have more user-defined
813 conversions. */
814 flags &= ~LOOKUP_NO_CONVERSION;
815 /* But no narrowing conversions. */
816 flags |= LOOKUP_NO_NARROWING;
817
818 /* Can't make an array of these types. */
819 if (TREE_CODE (elttype) == REFERENCE_TYPE
820 || TREE_CODE (elttype) == FUNCTION_TYPE
821 || VOID_TYPE_P (elttype))
822 return NULL;
823
824 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
825 {
826 conversion *sub
827 = implicit_conversion (elttype, TREE_TYPE (val), val,
828 false, flags, complain);
829 if (sub == NULL)
830 return NULL;
831
832 subconvs[i] = sub;
833 }
834
835 t = alloc_conversion (ck_list);
836 t->type = type;
837 t->u.list = subconvs;
838 t->rank = cr_exact;
839
840 for (i = 0; i < len; ++i)
841 {
842 conversion *sub = subconvs[i];
843 if (sub->rank > t->rank)
844 t->rank = sub->rank;
845 if (sub->user_conv_p)
846 t->user_conv_p = true;
847 if (sub->bad_p)
848 t->bad_p = true;
849 }
850
851 return t;
852 }
853
854 /* Return the next conversion of the conversion chain (if applicable),
855 or NULL otherwise. Please use this function instead of directly
856 accessing fields of struct conversion. */
857
858 static conversion *
859 next_conversion (conversion *conv)
860 {
861 if (conv == NULL
862 || conv->kind == ck_identity
863 || conv->kind == ck_ambig
864 || conv->kind == ck_list)
865 return NULL;
866 return conv->u.next;
867 }
868
869 /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
870 is a valid aggregate initializer for array type ATYPE. */
871
872 static bool
873 can_convert_array (tree atype, tree ctor, int flags, tsubst_flags_t complain)
874 {
875 unsigned i;
876 tree elttype = TREE_TYPE (atype);
877 for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
878 {
879 tree val = CONSTRUCTOR_ELT (ctor, i)->value;
880 bool ok;
881 if (TREE_CODE (elttype) == ARRAY_TYPE
882 && TREE_CODE (val) == CONSTRUCTOR)
883 ok = can_convert_array (elttype, val, flags, complain);
884 else
885 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
886 complain);
887 if (!ok)
888 return false;
889 }
890 return true;
891 }
892
893 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
894 aggregate class, if such a conversion is possible. */
895
896 static conversion *
897 build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
898 {
899 unsigned HOST_WIDE_INT i = 0;
900 conversion *c;
901 tree field = next_initializable_field (TYPE_FIELDS (type));
902 tree empty_ctor = NULL_TREE;
903
904 /* We already called reshape_init in implicit_conversion. */
905
906 /* The conversions within the init-list aren't affected by the enclosing
907 context; they're always simple copy-initialization. */
908 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
909
910 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
911 {
912 tree ftype = TREE_TYPE (field);
913 tree val;
914 bool ok;
915
916 if (i < CONSTRUCTOR_NELTS (ctor))
917 val = CONSTRUCTOR_ELT (ctor, i)->value;
918 else if (DECL_INITIAL (field))
919 val = get_nsdmi (field, /*ctor*/false, complain);
920 else if (TREE_CODE (ftype) == REFERENCE_TYPE)
921 /* Value-initialization of reference is ill-formed. */
922 return NULL;
923 else
924 {
925 if (empty_ctor == NULL_TREE)
926 empty_ctor = build_constructor (init_list_type_node, NULL);
927 val = empty_ctor;
928 }
929 ++i;
930
931 if (TREE_CODE (ftype) == ARRAY_TYPE
932 && TREE_CODE (val) == CONSTRUCTOR)
933 ok = can_convert_array (ftype, val, flags, complain);
934 else
935 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
936 complain);
937
938 if (!ok)
939 return NULL;
940
941 if (TREE_CODE (type) == UNION_TYPE)
942 break;
943 }
944
945 if (i < CONSTRUCTOR_NELTS (ctor))
946 return NULL;
947
948 c = alloc_conversion (ck_aggr);
949 c->type = type;
950 c->rank = cr_exact;
951 c->user_conv_p = true;
952 c->check_narrowing = true;
953 c->u.next = NULL;
954 return c;
955 }
956
957 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
958 array type, if such a conversion is possible. */
959
960 static conversion *
961 build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
962 {
963 conversion *c;
964 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
965 tree elttype = TREE_TYPE (type);
966 unsigned i;
967 tree val;
968 bool bad = false;
969 bool user = false;
970 enum conversion_rank rank = cr_exact;
971
972 /* We might need to propagate the size from the element to the array. */
973 complete_type (type);
974
975 if (TYPE_DOMAIN (type)
976 && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
977 {
978 unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
979 if (alen < len)
980 return NULL;
981 }
982
983 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
984
985 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
986 {
987 conversion *sub
988 = implicit_conversion (elttype, TREE_TYPE (val), val,
989 false, flags, complain);
990 if (sub == NULL)
991 return NULL;
992
993 if (sub->rank > rank)
994 rank = sub->rank;
995 if (sub->user_conv_p)
996 user = true;
997 if (sub->bad_p)
998 bad = true;
999 }
1000
1001 c = alloc_conversion (ck_aggr);
1002 c->type = type;
1003 c->rank = rank;
1004 c->user_conv_p = user;
1005 c->bad_p = bad;
1006 c->u.next = NULL;
1007 return c;
1008 }
1009
1010 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1011 complex type, if such a conversion is possible. */
1012
1013 static conversion *
1014 build_complex_conv (tree type, tree ctor, int flags,
1015 tsubst_flags_t complain)
1016 {
1017 conversion *c;
1018 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1019 tree elttype = TREE_TYPE (type);
1020 unsigned i;
1021 tree val;
1022 bool bad = false;
1023 bool user = false;
1024 enum conversion_rank rank = cr_exact;
1025
1026 if (len != 2)
1027 return NULL;
1028
1029 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1030
1031 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
1032 {
1033 conversion *sub
1034 = implicit_conversion (elttype, TREE_TYPE (val), val,
1035 false, flags, complain);
1036 if (sub == NULL)
1037 return NULL;
1038
1039 if (sub->rank > rank)
1040 rank = sub->rank;
1041 if (sub->user_conv_p)
1042 user = true;
1043 if (sub->bad_p)
1044 bad = true;
1045 }
1046
1047 c = alloc_conversion (ck_aggr);
1048 c->type = type;
1049 c->rank = rank;
1050 c->user_conv_p = user;
1051 c->bad_p = bad;
1052 c->u.next = NULL;
1053 return c;
1054 }
1055
1056 /* Build a representation of the identity conversion from EXPR to
1057 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1058
1059 static conversion *
1060 build_identity_conv (tree type, tree expr)
1061 {
1062 conversion *c;
1063
1064 c = alloc_conversion (ck_identity);
1065 c->type = type;
1066 c->u.expr = expr;
1067
1068 return c;
1069 }
1070
1071 /* Converting from EXPR to TYPE was ambiguous in the sense that there
1072 were multiple user-defined conversions to accomplish the job.
1073 Build a conversion that indicates that ambiguity. */
1074
1075 static conversion *
1076 build_ambiguous_conv (tree type, tree expr)
1077 {
1078 conversion *c;
1079
1080 c = alloc_conversion (ck_ambig);
1081 c->type = type;
1082 c->u.expr = expr;
1083
1084 return c;
1085 }
1086
1087 tree
1088 strip_top_quals (tree t)
1089 {
1090 if (TREE_CODE (t) == ARRAY_TYPE)
1091 return t;
1092 return cp_build_qualified_type (t, 0);
1093 }
1094
1095 /* Returns the standard conversion path (see [conv]) from type FROM to type
1096 TO, if any. For proper handling of null pointer constants, you must
1097 also pass the expression EXPR to convert from. If C_CAST_P is true,
1098 this conversion is coming from a C-style cast. */
1099
1100 static conversion *
1101 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1102 int flags, tsubst_flags_t complain)
1103 {
1104 enum tree_code fcode, tcode;
1105 conversion *conv;
1106 bool fromref = false;
1107 tree qualified_to;
1108
1109 to = non_reference (to);
1110 if (TREE_CODE (from) == REFERENCE_TYPE)
1111 {
1112 fromref = true;
1113 from = TREE_TYPE (from);
1114 }
1115 qualified_to = to;
1116 to = strip_top_quals (to);
1117 from = strip_top_quals (from);
1118
1119 if (expr && type_unknown_p (expr))
1120 {
1121 if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1122 {
1123 tsubst_flags_t tflags = tf_conv;
1124 expr = instantiate_type (to, expr, tflags);
1125 if (expr == error_mark_node)
1126 return NULL;
1127 from = TREE_TYPE (expr);
1128 }
1129 else if (TREE_CODE (to) == BOOLEAN_TYPE)
1130 {
1131 /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1132 expr = resolve_nondeduced_context (expr, complain);
1133 from = TREE_TYPE (expr);
1134 }
1135 }
1136
1137 fcode = TREE_CODE (from);
1138 tcode = TREE_CODE (to);
1139
1140 conv = build_identity_conv (from, expr);
1141 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1142 {
1143 from = type_decays_to (from);
1144 fcode = TREE_CODE (from);
1145 conv = build_conv (ck_lvalue, from, conv);
1146 }
1147 /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1148 obvalue_p) seems odd, since it's already a prvalue, but that's how we
1149 express the copy constructor call required by copy-initialization. */
1150 else if (fromref || (expr && obvalue_p (expr)))
1151 {
1152 if (expr)
1153 {
1154 tree bitfield_type;
1155 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1156 if (bitfield_type)
1157 {
1158 from = strip_top_quals (bitfield_type);
1159 fcode = TREE_CODE (from);
1160 }
1161 }
1162 conv = build_conv (ck_rvalue, from, conv);
1163 if (flags & LOOKUP_PREFER_RVALUE)
1164 /* Tell convert_like_real to set LOOKUP_PREFER_RVALUE. */
1165 conv->rvaluedness_matches_p = true;
1166 }
1167
1168 /* Allow conversion between `__complex__' data types. */
1169 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1170 {
1171 /* The standard conversion sequence to convert FROM to TO is
1172 the standard conversion sequence to perform componentwise
1173 conversion. */
1174 conversion *part_conv = standard_conversion
1175 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1176 complain);
1177
1178 if (part_conv)
1179 {
1180 conv = build_conv (part_conv->kind, to, conv);
1181 conv->rank = part_conv->rank;
1182 }
1183 else
1184 conv = NULL;
1185
1186 return conv;
1187 }
1188
1189 if (same_type_p (from, to))
1190 {
1191 if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1192 conv->type = qualified_to;
1193 return conv;
1194 }
1195
1196 /* [conv.ptr]
1197 A null pointer constant can be converted to a pointer type; ... A
1198 null pointer constant of integral type can be converted to an
1199 rvalue of type std::nullptr_t. */
1200 if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1201 || NULLPTR_TYPE_P (to))
1202 && ((expr && null_ptr_cst_p (expr))
1203 || NULLPTR_TYPE_P (from)))
1204 conv = build_conv (ck_std, to, conv);
1205 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1206 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1207 {
1208 /* For backwards brain damage compatibility, allow interconversion of
1209 pointers and integers with a pedwarn. */
1210 conv = build_conv (ck_std, to, conv);
1211 conv->bad_p = true;
1212 }
1213 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1214 {
1215 /* For backwards brain damage compatibility, allow interconversion of
1216 enums and integers with a pedwarn. */
1217 conv = build_conv (ck_std, to, conv);
1218 conv->bad_p = true;
1219 }
1220 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1221 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1222 {
1223 tree to_pointee;
1224 tree from_pointee;
1225
1226 if (tcode == POINTER_TYPE)
1227 {
1228 to_pointee = TREE_TYPE (to);
1229 from_pointee = TREE_TYPE (from);
1230
1231 /* Since this is the target of a pointer, it can't have function
1232 qualifiers, so any TYPE_QUALS must be for attributes const or
1233 noreturn. Strip them. */
1234 if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1235 && TYPE_QUALS (to_pointee))
1236 to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1237 if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1238 && TYPE_QUALS (from_pointee))
1239 from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1240 }
1241 else
1242 {
1243 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1244 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1245 }
1246
1247 if (tcode == POINTER_TYPE
1248 && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1249 to_pointee))
1250 ;
1251 else if (VOID_TYPE_P (to_pointee)
1252 && !TYPE_PTRDATAMEM_P (from)
1253 && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1254 {
1255 tree nfrom = TREE_TYPE (from);
1256 /* Don't try to apply restrict to void. */
1257 int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1258 from_pointee = cp_build_qualified_type (void_type_node, quals);
1259 from = build_pointer_type (from_pointee);
1260 conv = build_conv (ck_ptr, from, conv);
1261 }
1262 else if (TYPE_PTRDATAMEM_P (from))
1263 {
1264 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1265 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1266
1267 if (same_type_p (fbase, tbase))
1268 /* No base conversion needed. */;
1269 else if (DERIVED_FROM_P (fbase, tbase)
1270 && (same_type_ignoring_top_level_qualifiers_p
1271 (from_pointee, to_pointee)))
1272 {
1273 from = build_ptrmem_type (tbase, from_pointee);
1274 conv = build_conv (ck_pmem, from, conv);
1275 }
1276 else
1277 return NULL;
1278 }
1279 else if (CLASS_TYPE_P (from_pointee)
1280 && CLASS_TYPE_P (to_pointee)
1281 /* [conv.ptr]
1282
1283 An rvalue of type "pointer to cv D," where D is a
1284 class type, can be converted to an rvalue of type
1285 "pointer to cv B," where B is a base class (clause
1286 _class.derived_) of D. If B is an inaccessible
1287 (clause _class.access_) or ambiguous
1288 (_class.member.lookup_) base class of D, a program
1289 that necessitates this conversion is ill-formed.
1290 Therefore, we use DERIVED_FROM_P, and do not check
1291 access or uniqueness. */
1292 && DERIVED_FROM_P (to_pointee, from_pointee))
1293 {
1294 from_pointee
1295 = cp_build_qualified_type (to_pointee,
1296 cp_type_quals (from_pointee));
1297 from = build_pointer_type (from_pointee);
1298 conv = build_conv (ck_ptr, from, conv);
1299 conv->base_p = true;
1300 }
1301
1302 if (same_type_p (from, to))
1303 /* OK */;
1304 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
1305 /* In a C-style cast, we ignore CV-qualification because we
1306 are allowed to perform a static_cast followed by a
1307 const_cast. */
1308 conv = build_conv (ck_qual, to, conv);
1309 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1310 conv = build_conv (ck_qual, to, conv);
1311 else if (expr && string_conv_p (to, expr, 0))
1312 /* converting from string constant to char *. */
1313 conv = build_conv (ck_qual, to, conv);
1314 else if (fnptr_conv_p (to, from))
1315 conv = build_conv (ck_fnptr, to, conv);
1316 /* Allow conversions among compatible ObjC pointer types (base
1317 conversions have been already handled above). */
1318 else if (c_dialect_objc ()
1319 && objc_compare_types (to, from, -4, NULL_TREE))
1320 conv = build_conv (ck_ptr, to, conv);
1321 else if (ptr_reasonably_similar (to_pointee, from_pointee))
1322 {
1323 conv = build_conv (ck_ptr, to, conv);
1324 conv->bad_p = true;
1325 }
1326 else
1327 return NULL;
1328
1329 from = to;
1330 }
1331 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1332 {
1333 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1334 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1335 tree fbase = class_of_this_parm (fromfn);
1336 tree tbase = class_of_this_parm (tofn);
1337
1338 if (!DERIVED_FROM_P (fbase, tbase))
1339 return NULL;
1340
1341 tree fstat = static_fn_type (fromfn);
1342 tree tstat = static_fn_type (tofn);
1343 if (same_type_p (tstat, fstat)
1344 || fnptr_conv_p (tstat, fstat))
1345 /* OK */;
1346 else
1347 return NULL;
1348
1349 if (!same_type_p (fbase, tbase))
1350 {
1351 from = build_memfn_type (fstat,
1352 tbase,
1353 cp_type_quals (tbase),
1354 type_memfn_rqual (tofn));
1355 from = build_ptrmemfunc_type (build_pointer_type (from));
1356 conv = build_conv (ck_pmem, from, conv);
1357 conv->base_p = true;
1358 }
1359 if (fnptr_conv_p (tstat, fstat))
1360 conv = build_conv (ck_fnptr, to, conv);
1361 }
1362 else if (tcode == BOOLEAN_TYPE)
1363 {
1364 /* [conv.bool]
1365
1366 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1367 to member type can be converted to a prvalue of type bool. ...
1368 For direct-initialization (8.5 [dcl.init]), a prvalue of type
1369 std::nullptr_t can be converted to a prvalue of type bool; */
1370 if (ARITHMETIC_TYPE_P (from)
1371 || UNSCOPED_ENUM_P (from)
1372 || fcode == POINTER_TYPE
1373 || TYPE_PTRMEM_P (from)
1374 || NULLPTR_TYPE_P (from))
1375 {
1376 conv = build_conv (ck_std, to, conv);
1377 if (fcode == POINTER_TYPE
1378 || TYPE_PTRDATAMEM_P (from)
1379 || (TYPE_PTRMEMFUNC_P (from)
1380 && conv->rank < cr_pbool)
1381 || NULLPTR_TYPE_P (from))
1382 conv->rank = cr_pbool;
1383 if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1384 conv->bad_p = true;
1385 return conv;
1386 }
1387
1388 return NULL;
1389 }
1390 /* We don't check for ENUMERAL_TYPE here because there are no standard
1391 conversions to enum type. */
1392 /* As an extension, allow conversion to complex type. */
1393 else if (ARITHMETIC_TYPE_P (to))
1394 {
1395 if (! (INTEGRAL_CODE_P (fcode)
1396 || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1397 || SCOPED_ENUM_P (from))
1398 return NULL;
1399 conv = build_conv (ck_std, to, conv);
1400
1401 /* Give this a better rank if it's a promotion. */
1402 if (same_type_p (to, type_promotes_to (from))
1403 && next_conversion (conv)->rank <= cr_promotion)
1404 conv->rank = cr_promotion;
1405 }
1406 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1407 && vector_types_convertible_p (from, to, false))
1408 return build_conv (ck_std, to, conv);
1409 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1410 && is_properly_derived_from (from, to))
1411 {
1412 if (conv->kind == ck_rvalue)
1413 conv = next_conversion (conv);
1414 conv = build_conv (ck_base, to, conv);
1415 /* The derived-to-base conversion indicates the initialization
1416 of a parameter with base type from an object of a derived
1417 type. A temporary object is created to hold the result of
1418 the conversion unless we're binding directly to a reference. */
1419 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1420 }
1421 else
1422 return NULL;
1423
1424 if (flags & LOOKUP_NO_NARROWING)
1425 conv->check_narrowing = true;
1426
1427 return conv;
1428 }
1429
1430 /* Returns nonzero if T1 is reference-related to T2. */
1431
1432 bool
1433 reference_related_p (tree t1, tree t2)
1434 {
1435 if (t1 == error_mark_node || t2 == error_mark_node)
1436 return false;
1437
1438 t1 = TYPE_MAIN_VARIANT (t1);
1439 t2 = TYPE_MAIN_VARIANT (t2);
1440
1441 /* [dcl.init.ref]
1442
1443 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1444 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
1445 of T2. */
1446 return (same_type_p (t1, t2)
1447 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1448 && DERIVED_FROM_P (t1, t2)));
1449 }
1450
1451 /* Returns nonzero if T1 is reference-compatible with T2. */
1452
1453 static bool
1454 reference_compatible_p (tree t1, tree t2)
1455 {
1456 /* [dcl.init.ref]
1457
1458 "cv1 T1" is reference compatible with "cv2 T2" if
1459 * T1 is reference-related to T2 or
1460 * T2 is "noexcept function" and T1 is "function", where the
1461 function types are otherwise the same,
1462 and cv1 is the same cv-qualification as, or greater cv-qualification
1463 than, cv2. */
1464 return ((reference_related_p (t1, t2)
1465 || fnptr_conv_p (t1, t2))
1466 && at_least_as_qualified_p (t1, t2));
1467 }
1468
1469 /* A reference of the indicated TYPE is being bound directly to the
1470 expression represented by the implicit conversion sequence CONV.
1471 Return a conversion sequence for this binding. */
1472
1473 static conversion *
1474 direct_reference_binding (tree type, conversion *conv)
1475 {
1476 tree t;
1477
1478 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1479 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1480
1481 t = TREE_TYPE (type);
1482
1483 /* [over.ics.rank]
1484
1485 When a parameter of reference type binds directly
1486 (_dcl.init.ref_) to an argument expression, the implicit
1487 conversion sequence is the identity conversion, unless the
1488 argument expression has a type that is a derived class of the
1489 parameter type, in which case the implicit conversion sequence is
1490 a derived-to-base Conversion.
1491
1492 If the parameter binds directly to the result of applying a
1493 conversion function to the argument expression, the implicit
1494 conversion sequence is a user-defined conversion sequence
1495 (_over.ics.user_), with the second standard conversion sequence
1496 either an identity conversion or, if the conversion function
1497 returns an entity of a type that is a derived class of the
1498 parameter type, a derived-to-base conversion. */
1499 if (is_properly_derived_from (conv->type, t))
1500 {
1501 /* Represent the derived-to-base conversion. */
1502 conv = build_conv (ck_base, t, conv);
1503 /* We will actually be binding to the base-class subobject in
1504 the derived class, so we mark this conversion appropriately.
1505 That way, convert_like knows not to generate a temporary. */
1506 conv->need_temporary_p = false;
1507 }
1508 return build_conv (ck_ref_bind, type, conv);
1509 }
1510
1511 /* Returns the conversion path from type FROM to reference type TO for
1512 purposes of reference binding. For lvalue binding, either pass a
1513 reference type to FROM or an lvalue expression to EXPR. If the
1514 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1515 the conversion returned. If C_CAST_P is true, this
1516 conversion is coming from a C-style cast. */
1517
1518 static conversion *
1519 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1520 tsubst_flags_t complain)
1521 {
1522 conversion *conv = NULL;
1523 tree to = TREE_TYPE (rto);
1524 tree from = rfrom;
1525 tree tfrom;
1526 bool related_p;
1527 bool compatible_p;
1528 cp_lvalue_kind gl_kind;
1529 bool is_lvalue;
1530
1531 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1532 {
1533 expr = instantiate_type (to, expr, tf_none);
1534 if (expr == error_mark_node)
1535 return NULL;
1536 from = TREE_TYPE (expr);
1537 }
1538
1539 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1540 {
1541 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1542 /* DR 1288: Otherwise, if the initializer list has a single element
1543 of type E and ... [T's] referenced type is reference-related to E,
1544 the object or reference is initialized from that element... */
1545 if (CONSTRUCTOR_NELTS (expr) == 1)
1546 {
1547 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1548 if (error_operand_p (elt))
1549 return NULL;
1550 tree etype = TREE_TYPE (elt);
1551 if (reference_related_p (to, etype))
1552 {
1553 expr = elt;
1554 from = etype;
1555 goto skip;
1556 }
1557 }
1558 /* Otherwise, if T is a reference type, a prvalue temporary of the
1559 type referenced by T is copy-list-initialized or
1560 direct-list-initialized, depending on the kind of initialization
1561 for the reference, and the reference is bound to that temporary. */
1562 conv = implicit_conversion (to, from, expr, c_cast_p,
1563 flags|LOOKUP_NO_TEMP_BIND, complain);
1564 skip:;
1565 }
1566
1567 if (TREE_CODE (from) == REFERENCE_TYPE)
1568 {
1569 from = TREE_TYPE (from);
1570 if (!TYPE_REF_IS_RVALUE (rfrom)
1571 || TREE_CODE (from) == FUNCTION_TYPE)
1572 gl_kind = clk_ordinary;
1573 else
1574 gl_kind = clk_rvalueref;
1575 }
1576 else if (expr)
1577 gl_kind = lvalue_kind (expr);
1578 else if (CLASS_TYPE_P (from)
1579 || TREE_CODE (from) == ARRAY_TYPE)
1580 gl_kind = clk_class;
1581 else
1582 gl_kind = clk_none;
1583
1584 /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1585 if ((flags & LOOKUP_NO_TEMP_BIND)
1586 && (gl_kind & clk_class))
1587 gl_kind = clk_none;
1588
1589 /* Same mask as real_lvalue_p. */
1590 is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1591
1592 tfrom = from;
1593 if ((gl_kind & clk_bitfield) != 0)
1594 tfrom = unlowered_expr_type (expr);
1595
1596 /* Figure out whether or not the types are reference-related and
1597 reference compatible. We have to do this after stripping
1598 references from FROM. */
1599 related_p = reference_related_p (to, tfrom);
1600 /* If this is a C cast, first convert to an appropriately qualified
1601 type, so that we can later do a const_cast to the desired type. */
1602 if (related_p && c_cast_p
1603 && !at_least_as_qualified_p (to, tfrom))
1604 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1605 compatible_p = reference_compatible_p (to, tfrom);
1606
1607 /* Directly bind reference when target expression's type is compatible with
1608 the reference and expression is an lvalue. In DR391, the wording in
1609 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1610 const and rvalue references to rvalues of compatible class type.
1611 We should also do direct bindings for non-class xvalues. */
1612 if ((related_p || compatible_p) && gl_kind)
1613 {
1614 /* [dcl.init.ref]
1615
1616 If the initializer expression
1617
1618 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1619 is reference-compatible with "cv2 T2,"
1620
1621 the reference is bound directly to the initializer expression
1622 lvalue.
1623
1624 [...]
1625 If the initializer expression is an rvalue, with T2 a class type,
1626 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1627 is bound to the object represented by the rvalue or to a sub-object
1628 within that object. */
1629
1630 conv = build_identity_conv (tfrom, expr);
1631 conv = direct_reference_binding (rto, conv);
1632
1633 if (TREE_CODE (rfrom) == REFERENCE_TYPE)
1634 /* Handle rvalue reference to function properly. */
1635 conv->rvaluedness_matches_p
1636 = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1637 else
1638 conv->rvaluedness_matches_p
1639 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1640
1641 if ((gl_kind & clk_bitfield) != 0
1642 || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1643 /* For the purposes of overload resolution, we ignore the fact
1644 this expression is a bitfield or packed field. (In particular,
1645 [over.ics.ref] says specifically that a function with a
1646 non-const reference parameter is viable even if the
1647 argument is a bitfield.)
1648
1649 However, when we actually call the function we must create
1650 a temporary to which to bind the reference. If the
1651 reference is volatile, or isn't const, then we cannot make
1652 a temporary, so we just issue an error when the conversion
1653 actually occurs. */
1654 conv->need_temporary_p = true;
1655
1656 /* Don't allow binding of lvalues (other than function lvalues) to
1657 rvalue references. */
1658 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1659 && TREE_CODE (to) != FUNCTION_TYPE)
1660 conv->bad_p = true;
1661
1662 /* Nor the reverse. */
1663 if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
1664 && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
1665 || (flags & LOOKUP_NO_RVAL_BIND))
1666 && TREE_CODE (to) != FUNCTION_TYPE)
1667 conv->bad_p = true;
1668
1669 if (!compatible_p)
1670 conv->bad_p = true;
1671
1672 return conv;
1673 }
1674 /* [class.conv.fct] A conversion function is never used to convert a
1675 (possibly cv-qualified) object to the (possibly cv-qualified) same
1676 object type (or a reference to it), to a (possibly cv-qualified) base
1677 class of that type (or a reference to it).... */
1678 else if (CLASS_TYPE_P (from) && !related_p
1679 && !(flags & LOOKUP_NO_CONVERSION))
1680 {
1681 /* [dcl.init.ref]
1682
1683 If the initializer expression
1684
1685 -- has a class type (i.e., T2 is a class type) can be
1686 implicitly converted to an lvalue of type "cv3 T3," where
1687 "cv1 T1" is reference-compatible with "cv3 T3". (this
1688 conversion is selected by enumerating the applicable
1689 conversion functions (_over.match.ref_) and choosing the
1690 best one through overload resolution. (_over.match_).
1691
1692 the reference is bound to the lvalue result of the conversion
1693 in the second case. */
1694 z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
1695 complain);
1696 if (cand)
1697 return cand->second_conv;
1698 }
1699
1700 /* From this point on, we conceptually need temporaries, even if we
1701 elide them. Only the cases above are "direct bindings". */
1702 if (flags & LOOKUP_NO_TEMP_BIND)
1703 return NULL;
1704
1705 /* [over.ics.rank]
1706
1707 When a parameter of reference type is not bound directly to an
1708 argument expression, the conversion sequence is the one required
1709 to convert the argument expression to the underlying type of the
1710 reference according to _over.best.ics_. Conceptually, this
1711 conversion sequence corresponds to copy-initializing a temporary
1712 of the underlying type with the argument expression. Any
1713 difference in top-level cv-qualification is subsumed by the
1714 initialization itself and does not constitute a conversion. */
1715
1716 /* [dcl.init.ref]
1717
1718 Otherwise, the reference shall be an lvalue reference to a
1719 non-volatile const type, or the reference shall be an rvalue
1720 reference.
1721
1722 We try below to treat this as a bad conversion to improve diagnostics,
1723 but if TO is an incomplete class, we need to reject this conversion
1724 now to avoid unnecessary instantiation. */
1725 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto)
1726 && !COMPLETE_TYPE_P (to))
1727 return NULL;
1728
1729 /* We're generating a temporary now, but don't bind any more in the
1730 conversion (specifically, don't slice the temporary returned by a
1731 conversion operator). */
1732 flags |= LOOKUP_NO_TEMP_BIND;
1733
1734 /* Core issue 899: When [copy-]initializing a temporary to be bound
1735 to the first parameter of a copy constructor (12.8) called with
1736 a single argument in the context of direct-initialization,
1737 explicit conversion functions are also considered.
1738
1739 So don't set LOOKUP_ONLYCONVERTING in that case. */
1740 if (!(flags & LOOKUP_COPY_PARM))
1741 flags |= LOOKUP_ONLYCONVERTING;
1742
1743 if (!conv)
1744 conv = implicit_conversion (to, from, expr, c_cast_p,
1745 flags, complain);
1746 if (!conv)
1747 return NULL;
1748
1749 if (conv->user_conv_p)
1750 {
1751 /* If initializing the temporary used a conversion function,
1752 recalculate the second conversion sequence. */
1753 for (conversion *t = conv; t; t = next_conversion (t))
1754 if (t->kind == ck_user
1755 && DECL_CONV_FN_P (t->cand->fn))
1756 {
1757 tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
1758 int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
1759 conversion *new_second
1760 = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
1761 sflags, complain);
1762 if (!new_second)
1763 return NULL;
1764 return merge_conversion_sequences (t, new_second);
1765 }
1766 }
1767
1768 conv = build_conv (ck_ref_bind, rto, conv);
1769 /* This reference binding, unlike those above, requires the
1770 creation of a temporary. */
1771 conv->need_temporary_p = true;
1772 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1773
1774 /* [dcl.init.ref]
1775
1776 Otherwise, the reference shall be an lvalue reference to a
1777 non-volatile const type, or the reference shall be an rvalue
1778 reference. */
1779 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1780 conv->bad_p = true;
1781
1782 /* [dcl.init.ref]
1783
1784 Otherwise, a temporary of type "cv1 T1" is created and
1785 initialized from the initializer expression using the rules for a
1786 non-reference copy initialization. If T1 is reference-related to
1787 T2, cv1 must be the same cv-qualification as, or greater
1788 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1789 if (related_p && !at_least_as_qualified_p (to, from))
1790 conv->bad_p = true;
1791
1792 return conv;
1793 }
1794
1795 /* Returns the implicit conversion sequence (see [over.ics]) from type
1796 FROM to type TO. The optional expression EXPR may affect the
1797 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1798 true, this conversion is coming from a C-style cast. */
1799
1800 static conversion *
1801 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1802 int flags, tsubst_flags_t complain)
1803 {
1804 conversion *conv;
1805
1806 if (from == error_mark_node || to == error_mark_node
1807 || expr == error_mark_node)
1808 return NULL;
1809
1810 /* Other flags only apply to the primary function in overload
1811 resolution, or after we've chosen one. */
1812 flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
1813 |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE
1814 |LOOKUP_NO_NARROWING|LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL);
1815
1816 /* FIXME: actually we don't want warnings either, but we can't just
1817 have 'complain &= ~(tf_warning|tf_error)' because it would cause
1818 the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
1819 We really ought not to issue that warning until we've committed
1820 to that conversion. */
1821 complain &= ~tf_error;
1822
1823 /* Call reshape_init early to remove redundant braces. */
1824 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)
1825 && CLASS_TYPE_P (to)
1826 && COMPLETE_TYPE_P (complete_type (to))
1827 && !CLASSTYPE_NON_AGGREGATE (to))
1828 {
1829 expr = reshape_init (to, expr, complain);
1830 if (expr == error_mark_node)
1831 return NULL;
1832 from = TREE_TYPE (expr);
1833 }
1834
1835 if (TREE_CODE (to) == REFERENCE_TYPE)
1836 conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
1837 else
1838 conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
1839
1840 if (conv)
1841 return conv;
1842
1843 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1844 {
1845 if (is_std_init_list (to))
1846 return build_list_conv (to, expr, flags, complain);
1847
1848 /* As an extension, allow list-initialization of _Complex. */
1849 if (TREE_CODE (to) == COMPLEX_TYPE)
1850 {
1851 conv = build_complex_conv (to, expr, flags, complain);
1852 if (conv)
1853 return conv;
1854 }
1855
1856 /* Allow conversion from an initializer-list with one element to a
1857 scalar type. */
1858 if (SCALAR_TYPE_P (to))
1859 {
1860 int nelts = CONSTRUCTOR_NELTS (expr);
1861 tree elt;
1862
1863 if (nelts == 0)
1864 elt = build_value_init (to, tf_none);
1865 else if (nelts == 1)
1866 elt = CONSTRUCTOR_ELT (expr, 0)->value;
1867 else
1868 elt = error_mark_node;
1869
1870 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
1871 c_cast_p, flags, complain);
1872 if (conv)
1873 {
1874 conv->check_narrowing = true;
1875 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
1876 /* Too many levels of braces, i.e. '{{1}}'. */
1877 conv->bad_p = true;
1878 return conv;
1879 }
1880 }
1881 else if (TREE_CODE (to) == ARRAY_TYPE)
1882 return build_array_conv (to, expr, flags, complain);
1883 }
1884
1885 if (expr != NULL_TREE
1886 && (MAYBE_CLASS_TYPE_P (from)
1887 || MAYBE_CLASS_TYPE_P (to))
1888 && (flags & LOOKUP_NO_CONVERSION) == 0)
1889 {
1890 struct z_candidate *cand;
1891
1892 if (CLASS_TYPE_P (to)
1893 && BRACE_ENCLOSED_INITIALIZER_P (expr)
1894 && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
1895 return build_aggr_conv (to, expr, flags, complain);
1896
1897 cand = build_user_type_conversion_1 (to, expr, flags, complain);
1898 if (cand)
1899 {
1900 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
1901 && CONSTRUCTOR_NELTS (expr) == 1
1902 && !is_list_ctor (cand->fn))
1903 {
1904 /* "If C is not an initializer-list constructor and the
1905 initializer list has a single element of type cv U, where U is
1906 X or a class derived from X, the implicit conversion sequence
1907 has Exact Match rank if U is X, or Conversion rank if U is
1908 derived from X." */
1909 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1910 tree elttype = TREE_TYPE (elt);
1911 if (reference_related_p (to, elttype))
1912 return implicit_conversion (to, elttype, elt,
1913 c_cast_p, flags, complain);
1914 }
1915 conv = cand->second_conv;
1916 }
1917
1918 /* We used to try to bind a reference to a temporary here, but that
1919 is now handled after the recursive call to this function at the end
1920 of reference_binding. */
1921 return conv;
1922 }
1923
1924 return NULL;
1925 }
1926
1927 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1928 functions. ARGS will not be changed until a single candidate is
1929 selected. */
1930
1931 static struct z_candidate *
1932 add_candidate (struct z_candidate **candidates,
1933 tree fn, tree first_arg, const vec<tree, va_gc> *args,
1934 size_t num_convs, conversion **convs,
1935 tree access_path, tree conversion_path,
1936 int viable, struct rejection_reason *reason,
1937 int flags)
1938 {
1939 struct z_candidate *cand = (struct z_candidate *)
1940 conversion_obstack_alloc (sizeof (struct z_candidate));
1941
1942 cand->fn = fn;
1943 cand->first_arg = first_arg;
1944 cand->args = args;
1945 cand->convs = convs;
1946 cand->num_convs = num_convs;
1947 cand->access_path = access_path;
1948 cand->conversion_path = conversion_path;
1949 cand->viable = viable;
1950 cand->reason = reason;
1951 cand->next = *candidates;
1952 cand->flags = flags;
1953 *candidates = cand;
1954
1955 return cand;
1956 }
1957
1958 /* Return the number of remaining arguments in the parameter list
1959 beginning with ARG. */
1960
1961 int
1962 remaining_arguments (tree arg)
1963 {
1964 int n;
1965
1966 for (n = 0; arg != NULL_TREE && arg != void_list_node;
1967 arg = TREE_CHAIN (arg))
1968 n++;
1969
1970 return n;
1971 }
1972
1973 /* Create an overload candidate for the function or method FN called
1974 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
1975 FLAGS is passed on to implicit_conversion.
1976
1977 This does not change ARGS.
1978
1979 CTYPE, if non-NULL, is the type we want to pretend this function
1980 comes from for purposes of overload resolution. */
1981
1982 static struct z_candidate *
1983 add_function_candidate (struct z_candidate **candidates,
1984 tree fn, tree ctype, tree first_arg,
1985 const vec<tree, va_gc> *args, tree access_path,
1986 tree conversion_path, int flags,
1987 tsubst_flags_t complain)
1988 {
1989 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1990 int i, len;
1991 conversion **convs;
1992 tree parmnode;
1993 tree orig_first_arg = first_arg;
1994 int skip;
1995 int viable = 1;
1996 struct rejection_reason *reason = NULL;
1997
1998 /* At this point we should not see any functions which haven't been
1999 explicitly declared, except for friend functions which will have
2000 been found using argument dependent lookup. */
2001 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
2002
2003 /* The `this', `in_chrg' and VTT arguments to constructors are not
2004 considered in overload resolution. */
2005 if (DECL_CONSTRUCTOR_P (fn))
2006 {
2007 if (ctor_omit_inherited_parms (fn))
2008 /* Bring back parameters omitted from an inherited ctor. */
2009 parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2010 else
2011 parmlist = skip_artificial_parms_for (fn, parmlist);
2012 skip = num_artificial_parms_for (fn);
2013 if (skip > 0 && first_arg != NULL_TREE)
2014 {
2015 --skip;
2016 first_arg = NULL_TREE;
2017 }
2018 }
2019 else
2020 skip = 0;
2021
2022 len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2023 convs = alloc_conversions (len);
2024
2025 /* 13.3.2 - Viable functions [over.match.viable]
2026 First, to be a viable function, a candidate function shall have enough
2027 parameters to agree in number with the arguments in the list.
2028
2029 We need to check this first; otherwise, checking the ICSes might cause
2030 us to produce an ill-formed template instantiation. */
2031
2032 parmnode = parmlist;
2033 for (i = 0; i < len; ++i)
2034 {
2035 if (parmnode == NULL_TREE || parmnode == void_list_node)
2036 break;
2037 parmnode = TREE_CHAIN (parmnode);
2038 }
2039
2040 if ((i < len && parmnode)
2041 || !sufficient_parms_p (parmnode))
2042 {
2043 int remaining = remaining_arguments (parmnode);
2044 viable = 0;
2045 reason = arity_rejection (first_arg, i + remaining, len);
2046 }
2047
2048 /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2049 parameter of type "reference to cv C" (including such a constructor
2050 instantiated from a template) is excluded from the set of candidate
2051 functions when used to construct an object of type D with an argument list
2052 containing a single argument if C is reference-related to D. */
2053 if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2054 && flag_new_inheriting_ctors
2055 && DECL_INHERITED_CTOR (fn))
2056 {
2057 tree ptype = non_reference (TREE_VALUE (parmlist));
2058 tree dtype = DECL_CONTEXT (fn);
2059 tree btype = DECL_INHERITED_CTOR_BASE (fn);
2060 if (reference_related_p (ptype, dtype)
2061 && reference_related_p (btype, ptype))
2062 {
2063 viable = false;
2064 reason = inherited_ctor_rejection ();
2065 }
2066 }
2067
2068 /* Second, for a function to be viable, its constraints must be
2069 satisfied. */
2070 if (flag_concepts && viable
2071 && !constraints_satisfied_p (fn))
2072 {
2073 reason = constraint_failure (fn);
2074 viable = false;
2075 }
2076
2077 /* When looking for a function from a subobject from an implicit
2078 copy/move constructor/operator=, don't consider anything that takes (a
2079 reference to) an unrelated type. See c++/44909 and core 1092. */
2080 if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2081 {
2082 if (DECL_CONSTRUCTOR_P (fn))
2083 i = 1;
2084 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2085 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2086 i = 2;
2087 else
2088 i = 0;
2089 if (i && len == i)
2090 {
2091 parmnode = chain_index (i-1, parmlist);
2092 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2093 ctype))
2094 viable = 0;
2095 }
2096
2097 /* This only applies at the top level. */
2098 flags &= ~LOOKUP_DEFAULTED;
2099 }
2100
2101 if (! viable)
2102 goto out;
2103
2104 /* Third, for F to be a viable function, there shall exist for each
2105 argument an implicit conversion sequence that converts that argument
2106 to the corresponding parameter of F. */
2107
2108 parmnode = parmlist;
2109
2110 for (i = 0; i < len; ++i)
2111 {
2112 tree argtype, to_type;
2113 tree arg;
2114 conversion *t;
2115 int is_this;
2116
2117 if (parmnode == void_list_node)
2118 break;
2119
2120 if (i == 0 && first_arg != NULL_TREE)
2121 arg = first_arg;
2122 else
2123 arg = CONST_CAST_TREE (
2124 (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2125 argtype = lvalue_type (arg);
2126
2127 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2128 && ! DECL_CONSTRUCTOR_P (fn));
2129
2130 if (parmnode)
2131 {
2132 tree parmtype = TREE_VALUE (parmnode);
2133 int lflags = flags;
2134
2135 parmnode = TREE_CHAIN (parmnode);
2136
2137 /* The type of the implicit object parameter ('this') for
2138 overload resolution is not always the same as for the
2139 function itself; conversion functions are considered to
2140 be members of the class being converted, and functions
2141 introduced by a using-declaration are considered to be
2142 members of the class that uses them.
2143
2144 Since build_over_call ignores the ICS for the `this'
2145 parameter, we can just change the parm type. */
2146 if (ctype && is_this)
2147 {
2148 parmtype = cp_build_qualified_type
2149 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
2150 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2151 {
2152 /* If the function has a ref-qualifier, the implicit
2153 object parameter has reference type. */
2154 bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2155 parmtype = cp_build_reference_type (parmtype, rv);
2156 /* The special handling of 'this' conversions in compare_ics
2157 does not apply if there is a ref-qualifier. */
2158 is_this = false;
2159 }
2160 else
2161 {
2162 parmtype = build_pointer_type (parmtype);
2163 /* We don't use build_this here because we don't want to
2164 capture the object argument until we've chosen a
2165 non-static member function. */
2166 arg = build_address (arg);
2167 argtype = lvalue_type (arg);
2168 }
2169 }
2170
2171 /* Core issue 899: When [copy-]initializing a temporary to be bound
2172 to the first parameter of a copy constructor (12.8) called with
2173 a single argument in the context of direct-initialization,
2174 explicit conversion functions are also considered.
2175
2176 So set LOOKUP_COPY_PARM to let reference_binding know that
2177 it's being called in that context. We generalize the above
2178 to handle move constructors and template constructors as well;
2179 the standardese should soon be updated similarly. */
2180 if (ctype && i == 0 && (len-skip == 1)
2181 && DECL_CONSTRUCTOR_P (fn)
2182 && parmtype != error_mark_node
2183 && (same_type_ignoring_top_level_qualifiers_p
2184 (non_reference (parmtype), ctype)))
2185 {
2186 if (!(flags & LOOKUP_ONLYCONVERTING))
2187 lflags |= LOOKUP_COPY_PARM;
2188 /* We allow user-defined conversions within init-lists, but
2189 don't list-initialize the copy parm, as that would mean
2190 using two levels of braces for the same type. */
2191 if ((flags & LOOKUP_LIST_INIT_CTOR)
2192 && BRACE_ENCLOSED_INITIALIZER_P (arg))
2193 lflags |= LOOKUP_NO_CONVERSION;
2194 }
2195 else
2196 lflags |= LOOKUP_ONLYCONVERTING;
2197
2198 t = implicit_conversion (parmtype, argtype, arg,
2199 /*c_cast_p=*/false, lflags, complain);
2200 to_type = parmtype;
2201 }
2202 else
2203 {
2204 t = build_identity_conv (argtype, arg);
2205 t->ellipsis_p = true;
2206 to_type = argtype;
2207 }
2208
2209 if (t && is_this)
2210 t->this_p = true;
2211
2212 convs[i] = t;
2213 if (! t)
2214 {
2215 viable = 0;
2216 reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
2217 break;
2218 }
2219
2220 if (t->bad_p)
2221 {
2222 viable = -1;
2223 reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type);
2224 }
2225 }
2226
2227 out:
2228 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2229 access_path, conversion_path, viable, reason, flags);
2230 }
2231
2232 /* Create an overload candidate for the conversion function FN which will
2233 be invoked for expression OBJ, producing a pointer-to-function which
2234 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2235 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2236 passed on to implicit_conversion.
2237
2238 Actually, we don't really care about FN; we care about the type it
2239 converts to. There may be multiple conversion functions that will
2240 convert to that type, and we rely on build_user_type_conversion_1 to
2241 choose the best one; so when we create our candidate, we record the type
2242 instead of the function. */
2243
2244 static struct z_candidate *
2245 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2246 const vec<tree, va_gc> *arglist,
2247 tree access_path, tree conversion_path,
2248 tsubst_flags_t complain)
2249 {
2250 tree totype = TREE_TYPE (TREE_TYPE (fn));
2251 int i, len, viable, flags;
2252 tree parmlist, parmnode;
2253 conversion **convs;
2254 struct rejection_reason *reason;
2255
2256 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2257 parmlist = TREE_TYPE (parmlist);
2258 parmlist = TYPE_ARG_TYPES (parmlist);
2259
2260 len = vec_safe_length (arglist) + 1;
2261 convs = alloc_conversions (len);
2262 parmnode = parmlist;
2263 viable = 1;
2264 flags = LOOKUP_IMPLICIT;
2265 reason = NULL;
2266
2267 /* Don't bother looking up the same type twice. */
2268 if (*candidates && (*candidates)->fn == totype)
2269 return NULL;
2270
2271 for (i = 0; i < len; ++i)
2272 {
2273 tree arg, argtype, convert_type = NULL_TREE;
2274 conversion *t;
2275
2276 if (i == 0)
2277 arg = obj;
2278 else
2279 arg = (*arglist)[i - 1];
2280 argtype = lvalue_type (arg);
2281
2282 if (i == 0)
2283 {
2284 t = build_identity_conv (argtype, NULL_TREE);
2285 t = build_conv (ck_user, totype, t);
2286 /* Leave the 'cand' field null; we'll figure out the conversion in
2287 convert_like_real if this candidate is chosen. */
2288 convert_type = totype;
2289 }
2290 else if (parmnode == void_list_node)
2291 break;
2292 else if (parmnode)
2293 {
2294 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2295 /*c_cast_p=*/false, flags, complain);
2296 convert_type = TREE_VALUE (parmnode);
2297 }
2298 else
2299 {
2300 t = build_identity_conv (argtype, arg);
2301 t->ellipsis_p = true;
2302 convert_type = argtype;
2303 }
2304
2305 convs[i] = t;
2306 if (! t)
2307 break;
2308
2309 if (t->bad_p)
2310 {
2311 viable = -1;
2312 reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type);
2313 }
2314
2315 if (i == 0)
2316 continue;
2317
2318 if (parmnode)
2319 parmnode = TREE_CHAIN (parmnode);
2320 }
2321
2322 if (i < len
2323 || ! sufficient_parms_p (parmnode))
2324 {
2325 int remaining = remaining_arguments (parmnode);
2326 viable = 0;
2327 reason = arity_rejection (NULL_TREE, i + remaining, len);
2328 }
2329
2330 return add_candidate (candidates, totype, obj, arglist, len, convs,
2331 access_path, conversion_path, viable, reason, flags);
2332 }
2333
2334 static void
2335 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2336 tree type1, tree type2, tree *args, tree *argtypes,
2337 int flags, tsubst_flags_t complain)
2338 {
2339 conversion *t;
2340 conversion **convs;
2341 size_t num_convs;
2342 int viable = 1, i;
2343 tree types[2];
2344 struct rejection_reason *reason = NULL;
2345
2346 types[0] = type1;
2347 types[1] = type2;
2348
2349 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
2350 convs = alloc_conversions (num_convs);
2351
2352 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2353 conversion ops are allowed. We handle that here by just checking for
2354 boolean_type_node because other operators don't ask for it. COND_EXPR
2355 also does contextual conversion to bool for the first operand, but we
2356 handle that in build_conditional_expr, and type1 here is operand 2. */
2357 if (type1 != boolean_type_node)
2358 flags |= LOOKUP_ONLYCONVERTING;
2359
2360 for (i = 0; i < 2; ++i)
2361 {
2362 if (! args[i])
2363 break;
2364
2365 t = implicit_conversion (types[i], argtypes[i], args[i],
2366 /*c_cast_p=*/false, flags, complain);
2367 if (! t)
2368 {
2369 viable = 0;
2370 /* We need something for printing the candidate. */
2371 t = build_identity_conv (types[i], NULL_TREE);
2372 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2373 types[i]);
2374 }
2375 else if (t->bad_p)
2376 {
2377 viable = 0;
2378 reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2379 types[i]);
2380 }
2381 convs[i] = t;
2382 }
2383
2384 /* For COND_EXPR we rearranged the arguments; undo that now. */
2385 if (args[2])
2386 {
2387 convs[2] = convs[1];
2388 convs[1] = convs[0];
2389 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2390 /*c_cast_p=*/false, flags,
2391 complain);
2392 if (t)
2393 convs[0] = t;
2394 else
2395 {
2396 viable = 0;
2397 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2398 boolean_type_node);
2399 }
2400 }
2401
2402 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2403 num_convs, convs,
2404 /*access_path=*/NULL_TREE,
2405 /*conversion_path=*/NULL_TREE,
2406 viable, reason, flags);
2407 }
2408
2409 static bool
2410 is_complete (tree t)
2411 {
2412 return COMPLETE_TYPE_P (complete_type (t));
2413 }
2414
2415 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2416
2417 static bool
2418 promoted_arithmetic_type_p (tree type)
2419 {
2420 /* [over.built]
2421
2422 In this section, the term promoted integral type is used to refer
2423 to those integral types which are preserved by integral promotion
2424 (including e.g. int and long but excluding e.g. char).
2425 Similarly, the term promoted arithmetic type refers to promoted
2426 integral types plus floating types. */
2427 return ((CP_INTEGRAL_TYPE_P (type)
2428 && same_type_p (type_promotes_to (type), type))
2429 || TREE_CODE (type) == REAL_TYPE);
2430 }
2431
2432 /* Create any builtin operator overload candidates for the operator in
2433 question given the converted operand types TYPE1 and TYPE2. The other
2434 args are passed through from add_builtin_candidates to
2435 build_builtin_candidate.
2436
2437 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2438 If CODE is requires candidates operands of the same type of the kind
2439 of which TYPE1 and TYPE2 are, we add both candidates
2440 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2441
2442 static void
2443 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2444 enum tree_code code2, tree fnname, tree type1,
2445 tree type2, tree *args, tree *argtypes, int flags,
2446 tsubst_flags_t complain)
2447 {
2448 switch (code)
2449 {
2450 case POSTINCREMENT_EXPR:
2451 case POSTDECREMENT_EXPR:
2452 args[1] = integer_zero_node;
2453 type2 = integer_type_node;
2454 break;
2455 default:
2456 break;
2457 }
2458
2459 switch (code)
2460 {
2461
2462 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2463 and VQ is either volatile or empty, there exist candidate operator
2464 functions of the form
2465 VQ T& operator++(VQ T&);
2466 T operator++(VQ T&, int);
2467 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
2468 type other than bool, and VQ is either volatile or empty, there exist
2469 candidate operator functions of the form
2470 VQ T& operator--(VQ T&);
2471 T operator--(VQ T&, int);
2472 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
2473 complete object type, and VQ is either volatile or empty, there exist
2474 candidate operator functions of the form
2475 T*VQ& operator++(T*VQ&);
2476 T*VQ& operator--(T*VQ&);
2477 T* operator++(T*VQ&, int);
2478 T* operator--(T*VQ&, int); */
2479
2480 case POSTDECREMENT_EXPR:
2481 case PREDECREMENT_EXPR:
2482 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2483 return;
2484 /* FALLTHRU */
2485 case POSTINCREMENT_EXPR:
2486 case PREINCREMENT_EXPR:
2487 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2488 {
2489 type1 = build_reference_type (type1);
2490 break;
2491 }
2492 return;
2493
2494 /* 7 For every cv-qualified or cv-unqualified object type T, there
2495 exist candidate operator functions of the form
2496
2497 T& operator*(T*);
2498
2499 8 For every function type T, there exist candidate operator functions of
2500 the form
2501 T& operator*(T*); */
2502
2503 case INDIRECT_REF:
2504 if (TYPE_PTR_P (type1)
2505 && (TYPE_PTROB_P (type1)
2506 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2507 break;
2508 return;
2509
2510 /* 9 For every type T, there exist candidate operator functions of the form
2511 T* operator+(T*);
2512
2513 10For every promoted arithmetic type T, there exist candidate operator
2514 functions of the form
2515 T operator+(T);
2516 T operator-(T); */
2517
2518 case UNARY_PLUS_EXPR: /* unary + */
2519 if (TYPE_PTR_P (type1))
2520 break;
2521 /* FALLTHRU */
2522 case NEGATE_EXPR:
2523 if (ARITHMETIC_TYPE_P (type1))
2524 break;
2525 return;
2526
2527 /* 11For every promoted integral type T, there exist candidate operator
2528 functions of the form
2529 T operator~(T); */
2530
2531 case BIT_NOT_EXPR:
2532 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2533 break;
2534 return;
2535
2536 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2537 is the same type as C2 or is a derived class of C2, T is a complete
2538 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2539 there exist candidate operator functions of the form
2540 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2541 where CV12 is the union of CV1 and CV2. */
2542
2543 case MEMBER_REF:
2544 if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2545 {
2546 tree c1 = TREE_TYPE (type1);
2547 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2548
2549 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2550 && (TYPE_PTRMEMFUNC_P (type2)
2551 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2552 break;
2553 }
2554 return;
2555
2556 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2557 didate operator functions of the form
2558 LR operator*(L, R);
2559 LR operator/(L, R);
2560 LR operator+(L, R);
2561 LR operator-(L, R);
2562 bool operator<(L, R);
2563 bool operator>(L, R);
2564 bool operator<=(L, R);
2565 bool operator>=(L, R);
2566 bool operator==(L, R);
2567 bool operator!=(L, R);
2568 where LR is the result of the usual arithmetic conversions between
2569 types L and R.
2570
2571 14For every pair of types T and I, where T is a cv-qualified or cv-
2572 unqualified complete object type and I is a promoted integral type,
2573 there exist candidate operator functions of the form
2574 T* operator+(T*, I);
2575 T& operator[](T*, I);
2576 T* operator-(T*, I);
2577 T* operator+(I, T*);
2578 T& operator[](I, T*);
2579
2580 15For every T, where T is a pointer to complete object type, there exist
2581 candidate operator functions of the form112)
2582 ptrdiff_t operator-(T, T);
2583
2584 16For every pointer or enumeration type T, there exist candidate operator
2585 functions of the form
2586 bool operator<(T, T);
2587 bool operator>(T, T);
2588 bool operator<=(T, T);
2589 bool operator>=(T, T);
2590 bool operator==(T, T);
2591 bool operator!=(T, T);
2592
2593 17For every pointer to member type T, there exist candidate operator
2594 functions of the form
2595 bool operator==(T, T);
2596 bool operator!=(T, T); */
2597
2598 case MINUS_EXPR:
2599 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2600 break;
2601 if (TYPE_PTROB_P (type1)
2602 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2603 {
2604 type2 = ptrdiff_type_node;
2605 break;
2606 }
2607 /* FALLTHRU */
2608 case MULT_EXPR:
2609 case TRUNC_DIV_EXPR:
2610 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2611 break;
2612 return;
2613
2614 case EQ_EXPR:
2615 case NE_EXPR:
2616 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2617 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
2618 break;
2619 if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
2620 {
2621 type2 = type1;
2622 break;
2623 }
2624 if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
2625 {
2626 type1 = type2;
2627 break;
2628 }
2629 /* Fall through. */
2630 case LT_EXPR:
2631 case GT_EXPR:
2632 case LE_EXPR:
2633 case GE_EXPR:
2634 case MAX_EXPR:
2635 case MIN_EXPR:
2636 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2637 break;
2638 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2639 break;
2640 if (TREE_CODE (type1) == ENUMERAL_TYPE
2641 && TREE_CODE (type2) == ENUMERAL_TYPE)
2642 break;
2643 if (TYPE_PTR_P (type1)
2644 && null_ptr_cst_p (args[1]))
2645 {
2646 type2 = type1;
2647 break;
2648 }
2649 if (null_ptr_cst_p (args[0])
2650 && TYPE_PTR_P (type2))
2651 {
2652 type1 = type2;
2653 break;
2654 }
2655 return;
2656
2657 case PLUS_EXPR:
2658 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2659 break;
2660 /* FALLTHRU */
2661 case ARRAY_REF:
2662 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2663 {
2664 type1 = ptrdiff_type_node;
2665 break;
2666 }
2667 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2668 {
2669 type2 = ptrdiff_type_node;
2670 break;
2671 }
2672 return;
2673
2674 /* 18For every pair of promoted integral types L and R, there exist candi-
2675 date operator functions of the form
2676 LR operator%(L, R);
2677 LR operator&(L, R);
2678 LR operator^(L, R);
2679 LR operator|(L, R);
2680 L operator<<(L, R);
2681 L operator>>(L, R);
2682 where LR is the result of the usual arithmetic conversions between
2683 types L and R. */
2684
2685 case TRUNC_MOD_EXPR:
2686 case BIT_AND_EXPR:
2687 case BIT_IOR_EXPR:
2688 case BIT_XOR_EXPR:
2689 case LSHIFT_EXPR:
2690 case RSHIFT_EXPR:
2691 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2692 break;
2693 return;
2694
2695 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2696 type, VQ is either volatile or empty, and R is a promoted arithmetic
2697 type, there exist candidate operator functions of the form
2698 VQ L& operator=(VQ L&, R);
2699 VQ L& operator*=(VQ L&, R);
2700 VQ L& operator/=(VQ L&, R);
2701 VQ L& operator+=(VQ L&, R);
2702 VQ L& operator-=(VQ L&, R);
2703
2704 20For every pair T, VQ), where T is any type and VQ is either volatile
2705 or empty, there exist candidate operator functions of the form
2706 T*VQ& operator=(T*VQ&, T*);
2707
2708 21For every pair T, VQ), where T is a pointer to member type and VQ is
2709 either volatile or empty, there exist candidate operator functions of
2710 the form
2711 VQ T& operator=(VQ T&, T);
2712
2713 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2714 unqualified complete object type, VQ is either volatile or empty, and
2715 I is a promoted integral type, there exist candidate operator func-
2716 tions of the form
2717 T*VQ& operator+=(T*VQ&, I);
2718 T*VQ& operator-=(T*VQ&, I);
2719
2720 23For every triple L, VQ, R), where L is an integral or enumeration
2721 type, VQ is either volatile or empty, and R is a promoted integral
2722 type, there exist candidate operator functions of the form
2723
2724 VQ L& operator%=(VQ L&, R);
2725 VQ L& operator<<=(VQ L&, R);
2726 VQ L& operator>>=(VQ L&, R);
2727 VQ L& operator&=(VQ L&, R);
2728 VQ L& operator^=(VQ L&, R);
2729 VQ L& operator|=(VQ L&, R); */
2730
2731 case MODIFY_EXPR:
2732 switch (code2)
2733 {
2734 case PLUS_EXPR:
2735 case MINUS_EXPR:
2736 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2737 {
2738 type2 = ptrdiff_type_node;
2739 break;
2740 }
2741 /* FALLTHRU */
2742 case MULT_EXPR:
2743 case TRUNC_DIV_EXPR:
2744 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2745 break;
2746 return;
2747
2748 case TRUNC_MOD_EXPR:
2749 case BIT_AND_EXPR:
2750 case BIT_IOR_EXPR:
2751 case BIT_XOR_EXPR:
2752 case LSHIFT_EXPR:
2753 case RSHIFT_EXPR:
2754 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2755 break;
2756 return;
2757
2758 case NOP_EXPR:
2759 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2760 break;
2761 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2762 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2763 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2764 || ((TYPE_PTRMEMFUNC_P (type1)
2765 || TYPE_PTR_P (type1))
2766 && null_ptr_cst_p (args[1])))
2767 {
2768 type2 = type1;
2769 break;
2770 }
2771 return;
2772
2773 default:
2774 gcc_unreachable ();
2775 }
2776 type1 = build_reference_type (type1);
2777 break;
2778
2779 case COND_EXPR:
2780 /* [over.built]
2781
2782 For every pair of promoted arithmetic types L and R, there
2783 exist candidate operator functions of the form
2784
2785 LR operator?(bool, L, R);
2786
2787 where LR is the result of the usual arithmetic conversions
2788 between types L and R.
2789
2790 For every type T, where T is a pointer or pointer-to-member
2791 type, there exist candidate operator functions of the form T
2792 operator?(bool, T, T); */
2793
2794 if (promoted_arithmetic_type_p (type1)
2795 && promoted_arithmetic_type_p (type2))
2796 /* That's OK. */
2797 break;
2798
2799 /* Otherwise, the types should be pointers. */
2800 if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
2801 return;
2802
2803 /* We don't check that the two types are the same; the logic
2804 below will actually create two candidates; one in which both
2805 parameter types are TYPE1, and one in which both parameter
2806 types are TYPE2. */
2807 break;
2808
2809 case REALPART_EXPR:
2810 case IMAGPART_EXPR:
2811 if (ARITHMETIC_TYPE_P (type1))
2812 break;
2813 return;
2814
2815 default:
2816 gcc_unreachable ();
2817 }
2818
2819 /* Make sure we don't create builtin candidates with dependent types. */
2820 bool u1 = uses_template_parms (type1);
2821 bool u2 = type2 ? uses_template_parms (type2) : false;
2822 if (u1 || u2)
2823 {
2824 /* Try to recover if one of the types is non-dependent. But if
2825 there's only one type, there's nothing we can do. */
2826 if (!type2)
2827 return;
2828 /* And we lose if both are dependent. */
2829 if (u1 && u2)
2830 return;
2831 /* Or if they have different forms. */
2832 if (TREE_CODE (type1) != TREE_CODE (type2))
2833 return;
2834
2835 if (u1 && !u2)
2836 type1 = type2;
2837 else if (u2 && !u1)
2838 type2 = type1;
2839 }
2840
2841 /* If we're dealing with two pointer types or two enumeral types,
2842 we need candidates for both of them. */
2843 if (type2 && !same_type_p (type1, type2)
2844 && TREE_CODE (type1) == TREE_CODE (type2)
2845 && (TREE_CODE (type1) == REFERENCE_TYPE
2846 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2847 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2848 || TYPE_PTRMEMFUNC_P (type1)
2849 || MAYBE_CLASS_TYPE_P (type1)
2850 || TREE_CODE (type1) == ENUMERAL_TYPE))
2851 {
2852 if (TYPE_PTR_OR_PTRMEM_P (type1))
2853 {
2854 tree cptype = composite_pointer_type (type1, type2,
2855 error_mark_node,
2856 error_mark_node,
2857 CPO_CONVERSION,
2858 tf_none);
2859 if (cptype != error_mark_node)
2860 {
2861 build_builtin_candidate
2862 (candidates, fnname, cptype, cptype, args, argtypes,
2863 flags, complain);
2864 return;
2865 }
2866 }
2867
2868 build_builtin_candidate
2869 (candidates, fnname, type1, type1, args, argtypes, flags, complain);
2870 build_builtin_candidate
2871 (candidates, fnname, type2, type2, args, argtypes, flags, complain);
2872 return;
2873 }
2874
2875 build_builtin_candidate
2876 (candidates, fnname, type1, type2, args, argtypes, flags, complain);
2877 }
2878
2879 tree
2880 type_decays_to (tree type)
2881 {
2882 if (TREE_CODE (type) == ARRAY_TYPE)
2883 return build_pointer_type (TREE_TYPE (type));
2884 if (TREE_CODE (type) == FUNCTION_TYPE)
2885 return build_pointer_type (type);
2886 return type;
2887 }
2888
2889 /* There are three conditions of builtin candidates:
2890
2891 1) bool-taking candidates. These are the same regardless of the input.
2892 2) pointer-pair taking candidates. These are generated for each type
2893 one of the input types converts to.
2894 3) arithmetic candidates. According to the standard, we should generate
2895 all of these, but I'm trying not to...
2896
2897 Here we generate a superset of the possible candidates for this particular
2898 case. That is a subset of the full set the standard defines, plus some
2899 other cases which the standard disallows. add_builtin_candidate will
2900 filter out the invalid set. */
2901
2902 static void
2903 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2904 enum tree_code code2, tree fnname, tree *args,
2905 int flags, tsubst_flags_t complain)
2906 {
2907 int ref1, i;
2908 int enum_p = 0;
2909 tree type, argtypes[3], t;
2910 /* TYPES[i] is the set of possible builtin-operator parameter types
2911 we will consider for the Ith argument. */
2912 vec<tree, va_gc> *types[2];
2913 unsigned ix;
2914
2915 for (i = 0; i < 3; ++i)
2916 {
2917 if (args[i])
2918 argtypes[i] = unlowered_expr_type (args[i]);
2919 else
2920 argtypes[i] = NULL_TREE;
2921 }
2922
2923 switch (code)
2924 {
2925 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2926 and VQ is either volatile or empty, there exist candidate operator
2927 functions of the form
2928 VQ T& operator++(VQ T&); */
2929
2930 case POSTINCREMENT_EXPR:
2931 case PREINCREMENT_EXPR:
2932 case POSTDECREMENT_EXPR:
2933 case PREDECREMENT_EXPR:
2934 case MODIFY_EXPR:
2935 ref1 = 1;
2936 break;
2937
2938 /* 24There also exist candidate operator functions of the form
2939 bool operator!(bool);
2940 bool operator&&(bool, bool);
2941 bool operator||(bool, bool); */
2942
2943 case TRUTH_NOT_EXPR:
2944 build_builtin_candidate
2945 (candidates, fnname, boolean_type_node,
2946 NULL_TREE, args, argtypes, flags, complain);
2947 return;
2948
2949 case TRUTH_ORIF_EXPR:
2950 case TRUTH_ANDIF_EXPR:
2951 build_builtin_candidate
2952 (candidates, fnname, boolean_type_node,
2953 boolean_type_node, args, argtypes, flags, complain);
2954 return;
2955
2956 case ADDR_EXPR:
2957 case COMPOUND_EXPR:
2958 case COMPONENT_REF:
2959 return;
2960
2961 case COND_EXPR:
2962 case EQ_EXPR:
2963 case NE_EXPR:
2964 case LT_EXPR:
2965 case LE_EXPR:
2966 case GT_EXPR:
2967 case GE_EXPR:
2968 enum_p = 1;
2969 /* Fall through. */
2970
2971 default:
2972 ref1 = 0;
2973 }
2974
2975 types[0] = make_tree_vector ();
2976 types[1] = make_tree_vector ();
2977
2978 for (i = 0; i < 2; ++i)
2979 {
2980 if (! args[i])
2981 ;
2982 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
2983 {
2984 tree convs;
2985
2986 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2987 return;
2988
2989 convs = lookup_conversions (argtypes[i]);
2990
2991 if (code == COND_EXPR)
2992 {
2993 if (lvalue_p (args[i]))
2994 vec_safe_push (types[i], build_reference_type (argtypes[i]));
2995
2996 vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
2997 }
2998
2999 else if (! convs)
3000 return;
3001
3002 for (; convs; convs = TREE_CHAIN (convs))
3003 {
3004 type = TREE_TYPE (convs);
3005
3006 if (i == 0 && ref1
3007 && (TREE_CODE (type) != REFERENCE_TYPE
3008 || CP_TYPE_CONST_P (TREE_TYPE (type))))
3009 continue;
3010
3011 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
3012 vec_safe_push (types[i], type);
3013
3014 type = non_reference (type);
3015 if (i != 0 || ! ref1)
3016 {
3017 type = cv_unqualified (type_decays_to (type));
3018 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3019 vec_safe_push (types[i], type);
3020 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3021 type = type_promotes_to (type);
3022 }
3023
3024 if (! vec_member (type, types[i]))
3025 vec_safe_push (types[i], type);
3026 }
3027 }
3028 else
3029 {
3030 if (code == COND_EXPR && lvalue_p (args[i]))
3031 vec_safe_push (types[i], build_reference_type (argtypes[i]));
3032 type = non_reference (argtypes[i]);
3033 if (i != 0 || ! ref1)
3034 {
3035 type = cv_unqualified (type_decays_to (type));
3036 if (enum_p && UNSCOPED_ENUM_P (type))
3037 vec_safe_push (types[i], type);
3038 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3039 type = type_promotes_to (type);
3040 }
3041 vec_safe_push (types[i], type);
3042 }
3043 }
3044
3045 /* Run through the possible parameter types of both arguments,
3046 creating candidates with those parameter types. */
3047 FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3048 {
3049 unsigned jx;
3050 tree u;
3051
3052 if (!types[1]->is_empty ())
3053 FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3054 add_builtin_candidate
3055 (candidates, code, code2, fnname, t,
3056 u, args, argtypes, flags, complain);
3057 else
3058 add_builtin_candidate
3059 (candidates, code, code2, fnname, t,
3060 NULL_TREE, args, argtypes, flags, complain);
3061 }
3062
3063 release_tree_vector (types[0]);
3064 release_tree_vector (types[1]);
3065 }
3066
3067
3068 /* If TMPL can be successfully instantiated as indicated by
3069 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3070
3071 TMPL is the template. EXPLICIT_TARGS are any explicit template
3072 arguments. ARGLIST is the arguments provided at the call-site.
3073 This does not change ARGLIST. The RETURN_TYPE is the desired type
3074 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3075 as for add_function_candidate. If an OBJ is supplied, FLAGS and
3076 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
3077
3078 static struct z_candidate*
3079 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3080 tree ctype, tree explicit_targs, tree first_arg,
3081 const vec<tree, va_gc> *arglist, tree return_type,
3082 tree access_path, tree conversion_path,
3083 int flags, tree obj, unification_kind_t strict,
3084 tsubst_flags_t complain)
3085 {
3086 int ntparms = DECL_NTPARMS (tmpl);
3087 tree targs = make_tree_vec (ntparms);
3088 unsigned int len = vec_safe_length (arglist);
3089 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3090 unsigned int skip_without_in_chrg = 0;
3091 tree first_arg_without_in_chrg = first_arg;
3092 tree *args_without_in_chrg;
3093 unsigned int nargs_without_in_chrg;
3094 unsigned int ia, ix;
3095 tree arg;
3096 struct z_candidate *cand;
3097 tree fn;
3098 struct rejection_reason *reason = NULL;
3099 int errs;
3100
3101 /* We don't do deduction on the in-charge parameter, the VTT
3102 parameter or 'this'. */
3103 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
3104 {
3105 if (first_arg_without_in_chrg != NULL_TREE)
3106 first_arg_without_in_chrg = NULL_TREE;
3107 else if (return_type && strict == DEDUCE_CALL)
3108 /* We're deducing for a call to the result of a template conversion
3109 function, so the args don't contain 'this'; leave them alone. */;
3110 else
3111 ++skip_without_in_chrg;
3112 }
3113
3114 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3115 || DECL_BASE_CONSTRUCTOR_P (tmpl))
3116 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3117 {
3118 if (first_arg_without_in_chrg != NULL_TREE)
3119 first_arg_without_in_chrg = NULL_TREE;
3120 else
3121 ++skip_without_in_chrg;
3122 }
3123
3124 if (len < skip_without_in_chrg)
3125 return NULL;
3126
3127 if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3128 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3129 TREE_TYPE ((*arglist)[0])))
3130 {
3131 /* 12.8/6 says, "A declaration of a constructor for a class X is
3132 ill-formed if its first parameter is of type (optionally cv-qualified)
3133 X and either there are no other parameters or else all other
3134 parameters have default arguments. A member function template is never
3135 instantiated to produce such a constructor signature."
3136
3137 So if we're trying to copy an object of the containing class, don't
3138 consider a template constructor that has a first parameter type that
3139 is just a template parameter, as we would deduce a signature that we
3140 would then reject in the code below. */
3141 if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3142 {
3143 firstparm = TREE_VALUE (firstparm);
3144 if (PACK_EXPANSION_P (firstparm))
3145 firstparm = PACK_EXPANSION_PATTERN (firstparm);
3146 if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3147 {
3148 gcc_assert (!explicit_targs);
3149 reason = invalid_copy_with_fn_template_rejection ();
3150 goto fail;
3151 }
3152 }
3153 }
3154
3155 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3156 + (len - skip_without_in_chrg));
3157 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3158 ia = 0;
3159 if (first_arg_without_in_chrg != NULL_TREE)
3160 {
3161 args_without_in_chrg[ia] = first_arg_without_in_chrg;
3162 ++ia;
3163 }
3164 for (ix = skip_without_in_chrg;
3165 vec_safe_iterate (arglist, ix, &arg);
3166 ++ix)
3167 {
3168 args_without_in_chrg[ia] = arg;
3169 ++ia;
3170 }
3171 gcc_assert (ia == nargs_without_in_chrg);
3172
3173 errs = errorcount+sorrycount;
3174 fn = fn_type_unification (tmpl, explicit_targs, targs,
3175 args_without_in_chrg,
3176 nargs_without_in_chrg,
3177 return_type, strict, flags, false,
3178 complain & tf_decltype);
3179
3180 if (fn == error_mark_node)
3181 {
3182 /* Don't repeat unification later if it already resulted in errors. */
3183 if (errorcount+sorrycount == errs)
3184 reason = template_unification_rejection (tmpl, explicit_targs,
3185 targs, args_without_in_chrg,
3186 nargs_without_in_chrg,
3187 return_type, strict, flags);
3188 else
3189 reason = template_unification_error_rejection ();
3190 goto fail;
3191 }
3192
3193 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3194 {
3195 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3196 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3197 ctype))
3198 {
3199 /* We're trying to produce a constructor with a prohibited signature,
3200 as discussed above; handle here any cases we didn't catch then,
3201 such as X(X<T>). */
3202 reason = invalid_copy_with_fn_template_rejection ();
3203 goto fail;
3204 }
3205 }
3206
3207 if (obj != NULL_TREE)
3208 /* Aha, this is a conversion function. */
3209 cand = add_conv_candidate (candidates, fn, obj, arglist,
3210 access_path, conversion_path, complain);
3211 else
3212 cand = add_function_candidate (candidates, fn, ctype,
3213 first_arg, arglist, access_path,
3214 conversion_path, flags, complain);
3215 if (DECL_TI_TEMPLATE (fn) != tmpl)
3216 /* This situation can occur if a member template of a template
3217 class is specialized. Then, instantiate_template might return
3218 an instantiation of the specialization, in which case the
3219 DECL_TI_TEMPLATE field will point at the original
3220 specialization. For example:
3221
3222 template <class T> struct S { template <class U> void f(U);
3223 template <> void f(int) {}; };
3224 S<double> sd;
3225 sd.f(3);
3226
3227 Here, TMPL will be template <class U> S<double>::f(U).
3228 And, instantiate template will give us the specialization
3229 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3230 for this will point at template <class T> template <> S<T>::f(int),
3231 so that we can find the definition. For the purposes of
3232 overload resolution, however, we want the original TMPL. */
3233 cand->template_decl = build_template_info (tmpl, targs);
3234 else
3235 cand->template_decl = DECL_TEMPLATE_INFO (fn);
3236 cand->explicit_targs = explicit_targs;
3237
3238 return cand;
3239 fail:
3240 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
3241 access_path, conversion_path, 0, reason, flags);
3242 }
3243
3244
3245 static struct z_candidate *
3246 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3247 tree explicit_targs, tree first_arg,
3248 const vec<tree, va_gc> *arglist, tree return_type,
3249 tree access_path, tree conversion_path, int flags,
3250 unification_kind_t strict, tsubst_flags_t complain)
3251 {
3252 return
3253 add_template_candidate_real (candidates, tmpl, ctype,
3254 explicit_targs, first_arg, arglist,
3255 return_type, access_path, conversion_path,
3256 flags, NULL_TREE, strict, complain);
3257 }
3258
3259 /* Create an overload candidate for the conversion function template TMPL,
3260 returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3261 pointer-to-function which will in turn be called with the argument list
3262 ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3263 passed on to implicit_conversion. */
3264
3265 static struct z_candidate *
3266 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3267 tree obj,
3268 const vec<tree, va_gc> *arglist,
3269 tree return_type, tree access_path,
3270 tree conversion_path, tsubst_flags_t complain)
3271 {
3272 /* Making this work broke PR 71117, so until the committee resolves core
3273 issue 2189, let's disable this candidate if there are any viable call
3274 operators. */
3275 if (any_strictly_viable (*candidates))
3276 return NULL;
3277
3278 return
3279 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3280 NULL_TREE, arglist, return_type, access_path,
3281 conversion_path, 0, obj, DEDUCE_CALL,
3282 complain);
3283 }
3284
3285 /* The CANDS are the set of candidates that were considered for
3286 overload resolution. Return the set of viable candidates, or CANDS
3287 if none are viable. If any of the candidates were viable, set
3288 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3289 considered viable only if it is strictly viable. */
3290
3291 static struct z_candidate*
3292 splice_viable (struct z_candidate *cands,
3293 bool strict_p,
3294 bool *any_viable_p)
3295 {
3296 struct z_candidate *viable;
3297 struct z_candidate **last_viable;
3298 struct z_candidate **cand;
3299 bool found_strictly_viable = false;
3300
3301 /* Be strict inside templates, since build_over_call won't actually
3302 do the conversions to get pedwarns. */
3303 if (processing_template_decl)
3304 strict_p = true;
3305
3306 viable = NULL;
3307 last_viable = &viable;
3308 *any_viable_p = false;
3309
3310 cand = &cands;
3311 while (*cand)
3312 {
3313 struct z_candidate *c = *cand;
3314 if (!strict_p
3315 && (c->viable == 1 || TREE_CODE (c->fn) == TEMPLATE_DECL))
3316 {
3317 /* Be strict in the presence of a viable candidate. Also if
3318 there are template candidates, so that we get deduction errors
3319 for them instead of silently preferring a bad conversion. */
3320 strict_p = true;
3321 if (viable && !found_strictly_viable)
3322 {
3323 /* Put any spliced near matches back onto the main list so
3324 that we see them if there is no strict match. */
3325 *any_viable_p = false;
3326 *last_viable = cands;
3327 cands = viable;
3328 viable = NULL;
3329 last_viable = &viable;
3330 }
3331 }
3332
3333 if (strict_p ? c->viable == 1 : c->viable)
3334 {
3335 *last_viable = c;
3336 *cand = c->next;
3337 c->next = NULL;
3338 last_viable = &c->next;
3339 *any_viable_p = true;
3340 if (c->viable == 1)
3341 found_strictly_viable = true;
3342 }
3343 else
3344 cand = &c->next;
3345 }
3346
3347 return viable ? viable : cands;
3348 }
3349
3350 static bool
3351 any_strictly_viable (struct z_candidate *cands)
3352 {
3353 for (; cands; cands = cands->next)
3354 if (cands->viable == 1)
3355 return true;
3356 return false;
3357 }
3358
3359 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3360 words, it is about to become the "this" pointer for a member
3361 function call. Take the address of the object. */
3362
3363 static tree
3364 build_this (tree obj)
3365 {
3366 /* In a template, we are only concerned about the type of the
3367 expression, so we can take a shortcut. */
3368 if (processing_nonlambda_template ())
3369 return build_address (obj);
3370
3371 return cp_build_addr_expr (obj, tf_warning_or_error);
3372 }
3373
3374 /* Returns true iff functions are equivalent. Equivalent functions are
3375 not '==' only if one is a function-local extern function or if
3376 both are extern "C". */
3377
3378 static inline int
3379 equal_functions (tree fn1, tree fn2)
3380 {
3381 if (TREE_CODE (fn1) != TREE_CODE (fn2))
3382 return 0;
3383 if (TREE_CODE (fn1) == TEMPLATE_DECL)
3384 return fn1 == fn2;
3385 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
3386 || DECL_EXTERN_C_FUNCTION_P (fn1))
3387 return decls_match (fn1, fn2);
3388 return fn1 == fn2;
3389 }
3390
3391 /* Print information about a candidate being rejected due to INFO. */
3392
3393 static void
3394 print_conversion_rejection (location_t loc, struct conversion_info *info)
3395 {
3396 tree from = info->from;
3397 if (!TYPE_P (from))
3398 from = lvalue_type (from);
3399 if (info->n_arg == -1)
3400 {
3401 /* Conversion of implicit `this' argument failed. */
3402 if (!TYPE_P (info->from))
3403 /* A bad conversion for 'this' must be discarding cv-quals. */
3404 inform (loc, " passing %qT as %<this%> "
3405 "argument discards qualifiers",
3406 from);
3407 else
3408 inform (loc, " no known conversion for implicit "
3409 "%<this%> parameter from %qH to %qI",
3410 from, info->to_type);
3411 }
3412 else if (!TYPE_P (info->from))
3413 {
3414 if (info->n_arg >= 0)
3415 inform (loc, " conversion of argument %d would be ill-formed:",
3416 info->n_arg + 1);
3417 perform_implicit_conversion (info->to_type, info->from,
3418 tf_warning_or_error);
3419 }
3420 else if (info->n_arg == -2)
3421 /* Conversion of conversion function return value failed. */
3422 inform (loc, " no known conversion from %qH to %qI",
3423 from, info->to_type);
3424 else
3425 inform (loc, " no known conversion for argument %d from %qH to %qI",
3426 info->n_arg + 1, from, info->to_type);
3427 }
3428
3429 /* Print information about a candidate with WANT parameters and we found
3430 HAVE. */
3431
3432 static void
3433 print_arity_information (location_t loc, unsigned int have, unsigned int want)
3434 {
3435 inform_n (loc, want,
3436 " candidate expects %d argument, %d provided",
3437 " candidate expects %d arguments, %d provided",
3438 want, have);
3439 }
3440
3441 /* Print information about one overload candidate CANDIDATE. MSGSTR
3442 is the text to print before the candidate itself.
3443
3444 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3445 to have been run through gettext by the caller. This wart makes
3446 life simpler in print_z_candidates and for the translators. */
3447
3448 static void
3449 print_z_candidate (location_t loc, const char *msgstr,
3450 struct z_candidate *candidate)
3451 {
3452 const char *msg = (msgstr == NULL
3453 ? ""
3454 : ACONCAT ((msgstr, " ", NULL)));
3455 tree fn = candidate->fn;
3456 if (flag_new_inheriting_ctors)
3457 fn = strip_inheriting_ctors (fn);
3458 location_t cloc = location_of (fn);
3459
3460 if (identifier_p (fn))
3461 {
3462 cloc = loc;
3463 if (candidate->num_convs == 3)
3464 inform (cloc, "%s%<%D(%T, %T, %T)%> <built-in>", msg, fn,
3465 candidate->convs[0]->type,
3466 candidate->convs[1]->type,
3467 candidate->convs[2]->type);
3468 else if (candidate->num_convs == 2)
3469 inform (cloc, "%s%<%D(%T, %T)%> <built-in>", msg, fn,
3470 candidate->convs[0]->type,
3471 candidate->convs[1]->type);
3472 else
3473 inform (cloc, "%s%<%D(%T)%> <built-in>", msg, fn,
3474 candidate->convs[0]->type);
3475 }
3476 else if (TYPE_P (fn))
3477 inform (cloc, "%s%qT <conversion>", msg, fn);
3478 else if (candidate->viable == -1)
3479 inform (cloc, "%s%#qD <near match>", msg, fn);
3480 else if (DECL_DELETED_FN (fn))
3481 inform (cloc, "%s%#qD <deleted>", msg, fn);
3482 else
3483 inform (cloc, "%s%#qD", msg, fn);
3484 if (fn != candidate->fn)
3485 {
3486 cloc = location_of (candidate->fn);
3487 inform (cloc, " inherited here");
3488 }
3489 /* Give the user some information about why this candidate failed. */
3490 if (candidate->reason != NULL)
3491 {
3492 struct rejection_reason *r = candidate->reason;
3493
3494 switch (r->code)
3495 {
3496 case rr_arity:
3497 print_arity_information (cloc, r->u.arity.actual,
3498 r->u.arity.expected);
3499 break;
3500 case rr_arg_conversion:
3501 print_conversion_rejection (cloc, &r->u.conversion);
3502 break;
3503 case rr_bad_arg_conversion:
3504 print_conversion_rejection (cloc, &r->u.bad_conversion);
3505 break;
3506 case rr_explicit_conversion:
3507 inform (cloc, " return type %qT of explicit conversion function "
3508 "cannot be converted to %qT with a qualification "
3509 "conversion", r->u.conversion.from,
3510 r->u.conversion.to_type);
3511 break;
3512 case rr_template_conversion:
3513 inform (cloc, " conversion from return type %qT of template "
3514 "conversion function specialization to %qT is not an "
3515 "exact match", r->u.conversion.from,
3516 r->u.conversion.to_type);
3517 break;
3518 case rr_template_unification:
3519 /* We use template_unification_error_rejection if unification caused
3520 actual non-SFINAE errors, in which case we don't need to repeat
3521 them here. */
3522 if (r->u.template_unification.tmpl == NULL_TREE)
3523 {
3524 inform (cloc, " substitution of deduced template arguments "
3525 "resulted in errors seen above");
3526 break;
3527 }
3528 /* Re-run template unification with diagnostics. */
3529 inform (cloc, " template argument deduction/substitution failed:");
3530 fn_type_unification (r->u.template_unification.tmpl,
3531 r->u.template_unification.explicit_targs,
3532 (make_tree_vec
3533 (r->u.template_unification.num_targs)),
3534 r->u.template_unification.args,
3535 r->u.template_unification.nargs,
3536 r->u.template_unification.return_type,
3537 r->u.template_unification.strict,
3538 r->u.template_unification.flags,
3539 true, false);
3540 break;
3541 case rr_invalid_copy:
3542 inform (cloc,
3543 " a constructor taking a single argument of its own "
3544 "class type is invalid");
3545 break;
3546 case rr_constraint_failure:
3547 {
3548 tree tmpl = r->u.template_instantiation.tmpl;
3549 tree args = r->u.template_instantiation.targs;
3550 diagnose_constraints (cloc, tmpl, args);
3551 }
3552 break;
3553 case rr_inherited_ctor:
3554 inform (cloc, " an inherited constructor is not a candidate for "
3555 "initialization from an expression of the same or derived "
3556 "type");
3557 break;
3558 case rr_none:
3559 default:
3560 /* This candidate didn't have any issues or we failed to
3561 handle a particular code. Either way... */
3562 gcc_unreachable ();
3563 }
3564 }
3565 }
3566
3567 static void
3568 print_z_candidates (location_t loc, struct z_candidate *candidates)
3569 {
3570 struct z_candidate *cand1;
3571 struct z_candidate **cand2;
3572
3573 if (!candidates)
3574 return;
3575
3576 /* Remove non-viable deleted candidates. */
3577 cand1 = candidates;
3578 for (cand2 = &cand1; *cand2; )
3579 {
3580 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
3581 && !(*cand2)->viable
3582 && DECL_DELETED_FN ((*cand2)->fn))
3583 *cand2 = (*cand2)->next;
3584 else
3585 cand2 = &(*cand2)->next;
3586 }
3587 /* ...if there are any non-deleted ones. */
3588 if (cand1)
3589 candidates = cand1;
3590
3591 /* There may be duplicates in the set of candidates. We put off
3592 checking this condition as long as possible, since we have no way
3593 to eliminate duplicates from a set of functions in less than n^2
3594 time. Now we are about to emit an error message, so it is more
3595 permissible to go slowly. */
3596 for (cand1 = candidates; cand1; cand1 = cand1->next)
3597 {
3598 tree fn = cand1->fn;
3599 /* Skip builtin candidates and conversion functions. */
3600 if (!DECL_P (fn))
3601 continue;
3602 cand2 = &cand1->next;
3603 while (*cand2)
3604 {
3605 if (DECL_P ((*cand2)->fn)
3606 && equal_functions (fn, (*cand2)->fn))
3607 *cand2 = (*cand2)->next;
3608 else
3609 cand2 = &(*cand2)->next;
3610 }
3611 }
3612
3613 for (; candidates; candidates = candidates->next)
3614 print_z_candidate (loc, "candidate:", candidates);
3615 }
3616
3617 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3618 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3619 the result of the conversion function to convert it to the final
3620 desired type. Merge the two sequences into a single sequence,
3621 and return the merged sequence. */
3622
3623 static conversion *
3624 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
3625 {
3626 conversion **t;
3627 bool bad = user_seq->bad_p;
3628
3629 gcc_assert (user_seq->kind == ck_user);
3630
3631 /* Find the end of the second conversion sequence. */
3632 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
3633 {
3634 /* The entire sequence is a user-conversion sequence. */
3635 (*t)->user_conv_p = true;
3636 if (bad)
3637 (*t)->bad_p = true;
3638 }
3639
3640 /* Replace the identity conversion with the user conversion
3641 sequence. */
3642 *t = user_seq;
3643
3644 return std_seq;
3645 }
3646
3647 /* Handle overload resolution for initializing an object of class type from
3648 an initializer list. First we look for a suitable constructor that
3649 takes a std::initializer_list; if we don't find one, we then look for a
3650 non-list constructor.
3651
3652 Parameters are as for add_candidates, except that the arguments are in
3653 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
3654 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3655
3656 static void
3657 add_list_candidates (tree fns, tree first_arg,
3658 const vec<tree, va_gc> *args, tree totype,
3659 tree explicit_targs, bool template_only,
3660 tree conversion_path, tree access_path,
3661 int flags,
3662 struct z_candidate **candidates,
3663 tsubst_flags_t complain)
3664 {
3665 gcc_assert (*candidates == NULL);
3666
3667 /* We're looking for a ctor for list-initialization. */
3668 flags |= LOOKUP_LIST_INIT_CTOR;
3669 /* And we don't allow narrowing conversions. We also use this flag to
3670 avoid the copy constructor call for copy-list-initialization. */
3671 flags |= LOOKUP_NO_NARROWING;
3672
3673 unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
3674 tree init_list = (*args)[nart];
3675
3676 /* Always use the default constructor if the list is empty (DR 990). */
3677 if (CONSTRUCTOR_NELTS (init_list) == 0
3678 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
3679 ;
3680 /* If the class has a list ctor, try passing the list as a single
3681 argument first, but only consider list ctors. */
3682 else if (TYPE_HAS_LIST_CTOR (totype))
3683 {
3684 flags |= LOOKUP_LIST_ONLY;
3685 add_candidates (fns, first_arg, args, NULL_TREE,
3686 explicit_targs, template_only, conversion_path,
3687 access_path, flags, candidates, complain);
3688 if (any_strictly_viable (*candidates))
3689 return;
3690 }
3691
3692 /* Expand the CONSTRUCTOR into a new argument vec. */
3693 vec<tree, va_gc> *new_args;
3694 vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
3695 for (unsigned i = 0; i < nart; ++i)
3696 new_args->quick_push ((*args)[i]);
3697 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (init_list); ++i)
3698 new_args->quick_push (CONSTRUCTOR_ELT (init_list, i)->value);
3699
3700 /* We aren't looking for list-ctors anymore. */
3701 flags &= ~LOOKUP_LIST_ONLY;
3702 /* We allow more user-defined conversions within an init-list. */
3703 flags &= ~LOOKUP_NO_CONVERSION;
3704
3705 add_candidates (fns, first_arg, new_args, NULL_TREE,
3706 explicit_targs, template_only, conversion_path,
3707 access_path, flags, candidates, complain);
3708 }
3709
3710 /* Returns the best overload candidate to perform the requested
3711 conversion. This function is used for three the overloading situations
3712 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3713 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3714 per [dcl.init.ref], so we ignore temporary bindings. */
3715
3716 static struct z_candidate *
3717 build_user_type_conversion_1 (tree totype, tree expr, int flags,
3718 tsubst_flags_t complain)
3719 {
3720 struct z_candidate *candidates, *cand;
3721 tree fromtype;
3722 tree ctors = NULL_TREE;
3723 tree conv_fns = NULL_TREE;
3724 conversion *conv = NULL;
3725 tree first_arg = NULL_TREE;
3726 vec<tree, va_gc> *args = NULL;
3727 bool any_viable_p;
3728 int convflags;
3729
3730 if (!expr)
3731 return NULL;
3732
3733 fromtype = TREE_TYPE (expr);
3734
3735 /* We represent conversion within a hierarchy using RVALUE_CONV and
3736 BASE_CONV, as specified by [over.best.ics]; these become plain
3737 constructor calls, as specified in [dcl.init]. */
3738 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
3739 || !DERIVED_FROM_P (totype, fromtype));
3740
3741 if (CLASS_TYPE_P (totype))
3742 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
3743 creating a garbage BASELINK; constructors can't be inherited. */
3744 ctors = get_class_binding (totype, complete_ctor_identifier);
3745
3746 /* FIXME P0135 doesn't say what to do in C++17 about list-initialization from
3747 a single element. For now, let's handle constructors as before and also
3748 consider conversion operators from the element. */
3749 if (cxx_dialect >= cxx17
3750 && BRACE_ENCLOSED_INITIALIZER_P (expr)
3751 && CONSTRUCTOR_NELTS (expr) == 1)
3752 fromtype = TREE_TYPE (CONSTRUCTOR_ELT (expr, 0)->value);
3753
3754 if (MAYBE_CLASS_TYPE_P (fromtype))
3755 {
3756 tree to_nonref = non_reference (totype);
3757 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
3758 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
3759 && DERIVED_FROM_P (to_nonref, fromtype)))
3760 {
3761 /* [class.conv.fct] A conversion function is never used to
3762 convert a (possibly cv-qualified) object to the (possibly
3763 cv-qualified) same object type (or a reference to it), to a
3764 (possibly cv-qualified) base class of that type (or a
3765 reference to it)... */
3766 }
3767 else
3768 conv_fns = lookup_conversions (fromtype);
3769 }
3770
3771 candidates = 0;
3772 flags |= LOOKUP_NO_CONVERSION;
3773 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3774 flags |= LOOKUP_NO_NARROWING;
3775
3776 /* It's OK to bind a temporary for converting constructor arguments, but
3777 not in converting the return value of a conversion operator. */
3778 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
3779 | (flags & LOOKUP_NO_NARROWING));
3780 flags &= ~LOOKUP_NO_TEMP_BIND;
3781
3782 if (ctors)
3783 {
3784 int ctorflags = flags;
3785
3786 first_arg = build_dummy_object (totype);
3787
3788 /* We should never try to call the abstract or base constructor
3789 from here. */
3790 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
3791 && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
3792
3793 args = make_tree_vector_single (expr);
3794 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3795 {
3796 /* List-initialization. */
3797 add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
3798 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
3799 ctorflags, &candidates, complain);
3800 }
3801 else
3802 {
3803 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
3804 TYPE_BINFO (totype), TYPE_BINFO (totype),
3805 ctorflags, &candidates, complain);
3806 }
3807
3808 for (cand = candidates; cand; cand = cand->next)
3809 {
3810 cand->second_conv = build_identity_conv (totype, NULL_TREE);
3811
3812 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
3813 set, then this is copy-initialization. In that case, "The
3814 result of the call is then used to direct-initialize the
3815 object that is the destination of the copy-initialization."
3816 [dcl.init]
3817
3818 We represent this in the conversion sequence with an
3819 rvalue conversion, which means a constructor call. */
3820 if (TREE_CODE (totype) != REFERENCE_TYPE
3821 && !(convflags & LOOKUP_NO_TEMP_BIND))
3822 cand->second_conv
3823 = build_conv (ck_rvalue, totype, cand->second_conv);
3824 }
3825 }
3826
3827 if (conv_fns)
3828 {
3829 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3830 /* FIXME see above about C++17. */
3831 first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
3832 else
3833 first_arg = expr;
3834 }
3835
3836 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
3837 {
3838 tree conversion_path = TREE_PURPOSE (conv_fns);
3839 struct z_candidate *old_candidates;
3840
3841 /* If we are called to convert to a reference type, we are trying to
3842 find a direct binding, so don't even consider temporaries. If
3843 we don't find a direct binding, the caller will try again to
3844 look for a temporary binding. */
3845 if (TREE_CODE (totype) == REFERENCE_TYPE)
3846 convflags |= LOOKUP_NO_TEMP_BIND;
3847
3848 old_candidates = candidates;
3849 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3850 NULL_TREE, false,
3851 conversion_path, TYPE_BINFO (fromtype),
3852 flags, &candidates, complain);
3853
3854 for (cand = candidates; cand != old_candidates; cand = cand->next)
3855 {
3856 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
3857 conversion *ics
3858 = implicit_conversion (totype,
3859 rettype,
3860 0,
3861 /*c_cast_p=*/false, convflags,
3862 complain);
3863
3864 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3865 copy-initialization. In that case, "The result of the
3866 call is then used to direct-initialize the object that is
3867 the destination of the copy-initialization." [dcl.init]
3868
3869 We represent this in the conversion sequence with an
3870 rvalue conversion, which means a constructor call. But
3871 don't add a second rvalue conversion if there's already
3872 one there. Which there really shouldn't be, but it's
3873 harmless since we'd add it here anyway. */
3874 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3875 && !(convflags & LOOKUP_NO_TEMP_BIND))
3876 ics = build_conv (ck_rvalue, totype, ics);
3877
3878 cand->second_conv = ics;
3879
3880 if (!ics)
3881 {
3882 cand->viable = 0;
3883 cand->reason = arg_conversion_rejection (NULL_TREE, -2,
3884 rettype, totype);
3885 }
3886 else if (DECL_NONCONVERTING_P (cand->fn)
3887 && ics->rank > cr_exact)
3888 {
3889 /* 13.3.1.5: For direct-initialization, those explicit
3890 conversion functions that are not hidden within S and
3891 yield type T or a type that can be converted to type T
3892 with a qualification conversion (4.4) are also candidate
3893 functions. */
3894 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
3895 I've raised this issue with the committee. --jason 9/2011 */
3896 cand->viable = -1;
3897 cand->reason = explicit_conversion_rejection (rettype, totype);
3898 }
3899 else if (cand->viable == 1 && ics->bad_p)
3900 {
3901 cand->viable = -1;
3902 cand->reason
3903 = bad_arg_conversion_rejection (NULL_TREE, -2,
3904 rettype, totype);
3905 }
3906 else if (primary_template_instantiation_p (cand->fn)
3907 && ics->rank > cr_exact)
3908 {
3909 /* 13.3.3.1.2: If the user-defined conversion is specified by
3910 a specialization of a conversion function template, the
3911 second standard conversion sequence shall have exact match
3912 rank. */
3913 cand->viable = -1;
3914 cand->reason = template_conversion_rejection (rettype, totype);
3915 }
3916 }
3917 }
3918
3919 candidates = splice_viable (candidates, false, &any_viable_p);
3920 if (!any_viable_p)
3921 {
3922 if (args)
3923 release_tree_vector (args);
3924 return NULL;
3925 }
3926
3927 cand = tourney (candidates, complain);
3928 if (cand == 0)
3929 {
3930 if (complain & tf_error)
3931 {
3932 error ("conversion from %qH to %qI is ambiguous",
3933 fromtype, totype);
3934 print_z_candidates (location_of (expr), candidates);
3935 }
3936
3937 cand = candidates; /* any one will do */
3938 cand->second_conv = build_ambiguous_conv (totype, expr);
3939 cand->second_conv->user_conv_p = true;
3940 if (!any_strictly_viable (candidates))
3941 cand->second_conv->bad_p = true;
3942 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3943 ambiguous conversion is no worse than another user-defined
3944 conversion. */
3945
3946 return cand;
3947 }
3948
3949 tree convtype;
3950 if (!DECL_CONSTRUCTOR_P (cand->fn))
3951 convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
3952 else if (cand->second_conv->kind == ck_rvalue)
3953 /* DR 5: [in the first step of copy-initialization]...if the function
3954 is a constructor, the call initializes a temporary of the
3955 cv-unqualified version of the destination type. */
3956 convtype = cv_unqualified (totype);
3957 else
3958 convtype = totype;
3959 /* Build the user conversion sequence. */
3960 conv = build_conv
3961 (ck_user,
3962 convtype,
3963 build_identity_conv (TREE_TYPE (expr), expr));
3964 conv->cand = cand;
3965 if (cand->viable == -1)
3966 conv->bad_p = true;
3967
3968 /* Remember that this was a list-initialization. */
3969 if (flags & LOOKUP_NO_NARROWING)
3970 conv->check_narrowing = true;
3971
3972 /* Combine it with the second conversion sequence. */
3973 cand->second_conv = merge_conversion_sequences (conv,
3974 cand->second_conv);
3975
3976 return cand;
3977 }
3978
3979 /* Wrapper for above. */
3980
3981 tree
3982 build_user_type_conversion (tree totype, tree expr, int flags,
3983 tsubst_flags_t complain)
3984 {
3985 struct z_candidate *cand;
3986 tree ret;
3987
3988 bool subtime = timevar_cond_start (TV_OVERLOAD);
3989 cand = build_user_type_conversion_1 (totype, expr, flags, complain);
3990
3991 if (cand)
3992 {
3993 if (cand->second_conv->kind == ck_ambig)
3994 ret = error_mark_node;
3995 else
3996 {
3997 expr = convert_like (cand->second_conv, expr, complain);
3998 ret = convert_from_reference (expr);
3999 }
4000 }
4001 else
4002 ret = NULL_TREE;
4003
4004 timevar_cond_stop (TV_OVERLOAD, subtime);
4005 return ret;
4006 }
4007
4008 /* Subroutine of convert_nontype_argument.
4009
4010 EXPR is an expression used in a context that requires a converted
4011 constant-expression, such as a template non-type parameter. Do any
4012 necessary conversions (that are permitted for converted
4013 constant-expressions) to convert it to the desired type.
4014
4015 If conversion is successful, returns the converted expression;
4016 otherwise, returns error_mark_node. */
4017
4018 tree
4019 build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
4020 {
4021 conversion *conv;
4022 void *p;
4023 tree t;
4024 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
4025
4026 if (error_operand_p (expr))
4027 return error_mark_node;
4028
4029 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4030 p = conversion_obstack_alloc (0);
4031
4032 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4033 /*c_cast_p=*/false,
4034 LOOKUP_IMPLICIT, complain);
4035
4036 /* A converted constant expression of type T is an expression, implicitly
4037 converted to type T, where the converted expression is a constant
4038 expression and the implicit conversion sequence contains only
4039
4040 * user-defined conversions,
4041 * lvalue-to-rvalue conversions (7.1),
4042 * array-to-pointer conversions (7.2),
4043 * function-to-pointer conversions (7.3),
4044 * qualification conversions (7.5),
4045 * integral promotions (7.6),
4046 * integral conversions (7.8) other than narrowing conversions (11.6.4),
4047 * null pointer conversions (7.11) from std::nullptr_t,
4048 * null member pointer conversions (7.12) from std::nullptr_t, and
4049 * function pointer conversions (7.13),
4050
4051 and where the reference binding (if any) binds directly. */
4052
4053 for (conversion *c = conv;
4054 conv && c->kind != ck_identity;
4055 c = next_conversion (c))
4056 {
4057 switch (c->kind)
4058 {
4059 /* A conversion function is OK. If it isn't constexpr, we'll
4060 complain later that the argument isn't constant. */
4061 case ck_user:
4062 /* The lvalue-to-rvalue conversion is OK. */
4063 case ck_rvalue:
4064 /* Array-to-pointer and function-to-pointer. */
4065 case ck_lvalue:
4066 /* Function pointer conversions. */
4067 case ck_fnptr:
4068 /* Qualification conversions. */
4069 case ck_qual:
4070 break;
4071
4072 case ck_ref_bind:
4073 if (c->need_temporary_p)
4074 {
4075 if (complain & tf_error)
4076 error_at (loc, "initializing %qH with %qI in converted "
4077 "constant expression does not bind directly",
4078 type, next_conversion (c)->type);
4079 conv = NULL;
4080 }
4081 break;
4082
4083 case ck_base:
4084 case ck_pmem:
4085 case ck_ptr:
4086 case ck_std:
4087 t = next_conversion (c)->type;
4088 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
4089 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4090 /* Integral promotion or conversion. */
4091 break;
4092 if (NULLPTR_TYPE_P (t))
4093 /* Conversion from nullptr to pointer or pointer-to-member. */
4094 break;
4095
4096 if (complain & tf_error)
4097 error_at (loc, "conversion from %qH to %qI in a "
4098 "converted constant expression", t, type);
4099 /* fall through. */
4100
4101 default:
4102 conv = NULL;
4103 break;
4104 }
4105 }
4106
4107 /* Avoid confusing convert_nontype_argument by introducing
4108 a redundant conversion to the same reference type. */
4109 if (conv && conv->kind == ck_ref_bind
4110 && REFERENCE_REF_P (expr))
4111 {
4112 tree ref = TREE_OPERAND (expr, 0);
4113 if (same_type_p (type, TREE_TYPE (ref)))
4114 return ref;
4115 }
4116
4117 if (conv)
4118 expr = convert_like (conv, expr, complain);
4119 else
4120 expr = error_mark_node;
4121
4122 /* Free all the conversions we allocated. */
4123 obstack_free (&conversion_obstack, p);
4124
4125 return expr;
4126 }
4127
4128 /* Do any initial processing on the arguments to a function call. */
4129
4130 static vec<tree, va_gc> *
4131 resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
4132 {
4133 unsigned int ix;
4134 tree arg;
4135
4136 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
4137 {
4138 if (error_operand_p (arg))
4139 return NULL;
4140 else if (VOID_TYPE_P (TREE_TYPE (arg)))
4141 {
4142 if (complain & tf_error)
4143 error ("invalid use of void expression");
4144 return NULL;
4145 }
4146 else if (invalid_nonstatic_memfn_p (input_location, arg, complain))
4147 return NULL;
4148 }
4149 return args;
4150 }
4151
4152 /* Perform overload resolution on FN, which is called with the ARGS.
4153
4154 Return the candidate function selected by overload resolution, or
4155 NULL if the event that overload resolution failed. In the case
4156 that overload resolution fails, *CANDIDATES will be the set of
4157 candidates considered, and ANY_VIABLE_P will be set to true or
4158 false to indicate whether or not any of the candidates were
4159 viable.
4160
4161 The ARGS should already have gone through RESOLVE_ARGS before this
4162 function is called. */
4163
4164 static struct z_candidate *
4165 perform_overload_resolution (tree fn,
4166 const vec<tree, va_gc> *args,
4167 struct z_candidate **candidates,
4168 bool *any_viable_p, tsubst_flags_t complain)
4169 {
4170 struct z_candidate *cand;
4171 tree explicit_targs;
4172 int template_only;
4173
4174 bool subtime = timevar_cond_start (TV_OVERLOAD);
4175
4176 explicit_targs = NULL_TREE;
4177 template_only = 0;
4178
4179 *candidates = NULL;
4180 *any_viable_p = true;
4181
4182 /* Check FN. */
4183 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
4184 || TREE_CODE (fn) == TEMPLATE_DECL
4185 || TREE_CODE (fn) == OVERLOAD
4186 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
4187
4188 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4189 {
4190 explicit_targs = TREE_OPERAND (fn, 1);
4191 fn = TREE_OPERAND (fn, 0);
4192 template_only = 1;
4193 }
4194
4195 /* Add the various candidate functions. */
4196 add_candidates (fn, NULL_TREE, args, NULL_TREE,
4197 explicit_targs, template_only,
4198 /*conversion_path=*/NULL_TREE,
4199 /*access_path=*/NULL_TREE,
4200 LOOKUP_NORMAL,
4201 candidates, complain);
4202
4203 *candidates = splice_viable (*candidates, false, any_viable_p);
4204 if (*any_viable_p)
4205 cand = tourney (*candidates, complain);
4206 else
4207 cand = NULL;
4208
4209 timevar_cond_stop (TV_OVERLOAD, subtime);
4210 return cand;
4211 }
4212
4213 /* Print an error message about being unable to build a call to FN with
4214 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
4215 be located; CANDIDATES is a possibly empty list of such
4216 functions. */
4217
4218 static void
4219 print_error_for_call_failure (tree fn, vec<tree, va_gc> *args,
4220 struct z_candidate *candidates)
4221 {
4222 tree targs = NULL_TREE;
4223 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4224 {
4225 targs = TREE_OPERAND (fn, 1);
4226 fn = TREE_OPERAND (fn, 0);
4227 }
4228 tree name = OVL_NAME (fn);
4229 location_t loc = location_of (name);
4230 if (targs)
4231 name = lookup_template_function (name, targs);
4232
4233 if (!any_strictly_viable (candidates))
4234 error_at (loc, "no matching function for call to %<%D(%A)%>",
4235 name, build_tree_list_vec (args));
4236 else
4237 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
4238 name, build_tree_list_vec (args));
4239 if (candidates)
4240 print_z_candidates (loc, candidates);
4241 }
4242
4243 /* Return an expression for a call to FN (a namespace-scope function,
4244 or a static member function) with the ARGS. This may change
4245 ARGS. */
4246
4247 tree
4248 build_new_function_call (tree fn, vec<tree, va_gc> **args,
4249 tsubst_flags_t complain)
4250 {
4251 struct z_candidate *candidates, *cand;
4252 bool any_viable_p;
4253 void *p;
4254 tree result;
4255
4256 if (args != NULL && *args != NULL)
4257 {
4258 *args = resolve_args (*args, complain);
4259 if (*args == NULL)
4260 return error_mark_node;
4261 }
4262
4263 if (flag_tm)
4264 tm_malloc_replacement (fn);
4265
4266 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4267 p = conversion_obstack_alloc (0);
4268
4269 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
4270 complain);
4271
4272 if (!cand)
4273 {
4274 if (complain & tf_error)
4275 {
4276 // If there is a single (non-viable) function candidate,
4277 // let the error be diagnosed by cp_build_function_call_vec.
4278 if (!any_viable_p && candidates && ! candidates->next
4279 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
4280 return cp_build_function_call_vec (candidates->fn, args, complain);
4281
4282 // Otherwise, emit notes for non-viable candidates.
4283 print_error_for_call_failure (fn, *args, candidates);
4284 }
4285 result = error_mark_node;
4286 }
4287 else
4288 {
4289 int flags = LOOKUP_NORMAL;
4290 /* If fn is template_id_expr, the call has explicit template arguments
4291 (e.g. func<int>(5)), communicate this info to build_over_call
4292 through flags so that later we can use it to decide whether to warn
4293 about peculiar null pointer conversion. */
4294 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4295 {
4296 /* If overload resolution selects a specialization of a
4297 function concept for non-dependent template arguments,
4298 the expression is true if the constraints are satisfied
4299 and false otherwise.
4300
4301 NOTE: This is an extension of Concepts Lite TS that
4302 allows constraints to be used in expressions. */
4303 if (flag_concepts && !processing_template_decl)
4304 {
4305 tree tmpl = DECL_TI_TEMPLATE (cand->fn);
4306 tree targs = DECL_TI_ARGS (cand->fn);
4307 tree decl = DECL_TEMPLATE_RESULT (tmpl);
4308 if (DECL_DECLARED_CONCEPT_P (decl))
4309 return evaluate_function_concept (decl, targs);
4310 }
4311
4312 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
4313 }
4314
4315 result = build_over_call (cand, flags, complain);
4316 }
4317
4318 /* Free all the conversions we allocated. */
4319 obstack_free (&conversion_obstack, p);
4320
4321 return result;
4322 }
4323
4324 /* Build a call to a global operator new. FNNAME is the name of the
4325 operator (either "operator new" or "operator new[]") and ARGS are
4326 the arguments provided. This may change ARGS. *SIZE points to the
4327 total number of bytes required by the allocation, and is updated if
4328 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
4329 be used. If this function determines that no cookie should be
4330 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
4331 is not NULL_TREE, it is evaluated before calculating the final
4332 array size, and if it fails, the array size is replaced with
4333 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
4334 is non-NULL, it will be set, upon return, to the allocation
4335 function called. */
4336
4337 tree
4338 build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
4339 tree *size, tree *cookie_size,
4340 tree align_arg, tree size_check,
4341 tree *fn, tsubst_flags_t complain)
4342 {
4343 tree original_size = *size;
4344 tree fns;
4345 struct z_candidate *candidates;
4346 struct z_candidate *cand = NULL;
4347 bool any_viable_p;
4348
4349 if (fn)
4350 *fn = NULL_TREE;
4351 /* Set to (size_t)-1 if the size check fails. */
4352 if (size_check != NULL_TREE)
4353 {
4354 tree errval = TYPE_MAX_VALUE (sizetype);
4355 if (cxx_dialect >= cxx11 && flag_exceptions)
4356 errval = throw_bad_array_new_length ();
4357 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4358 original_size, errval);
4359 }
4360 vec_safe_insert (*args, 0, *size);
4361 *args = resolve_args (*args, complain);
4362 if (*args == NULL)
4363 return error_mark_node;
4364
4365 /* Based on:
4366
4367 [expr.new]
4368
4369 If this lookup fails to find the name, or if the allocated type
4370 is not a class type, the allocation function's name is looked
4371 up in the global scope.
4372
4373 we disregard block-scope declarations of "operator new". */
4374 fns = lookup_name_real (fnname, 0, 1, /*block_p=*/false, 0, 0);
4375 fns = lookup_arg_dependent (fnname, fns, *args);
4376
4377 if (align_arg)
4378 {
4379 vec<tree, va_gc>* align_args
4380 = vec_copy_and_insert (*args, align_arg, 1);
4381 cand = perform_overload_resolution (fns, align_args, &candidates,
4382 &any_viable_p, tf_none);
4383 /* If no aligned allocation function matches, try again without the
4384 alignment. */
4385 }
4386
4387 /* Figure out what function is being called. */
4388 if (!cand)
4389 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
4390 complain);
4391
4392 /* If no suitable function could be found, issue an error message
4393 and give up. */
4394 if (!cand)
4395 {
4396 if (complain & tf_error)
4397 print_error_for_call_failure (fns, *args, candidates);
4398 return error_mark_node;
4399 }
4400
4401 /* If a cookie is required, add some extra space. Whether
4402 or not a cookie is required cannot be determined until
4403 after we know which function was called. */
4404 if (*cookie_size)
4405 {
4406 bool use_cookie = true;
4407 tree arg_types;
4408
4409 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
4410 /* Skip the size_t parameter. */
4411 arg_types = TREE_CHAIN (arg_types);
4412 /* Check the remaining parameters (if any). */
4413 if (arg_types
4414 && TREE_CHAIN (arg_types) == void_list_node
4415 && same_type_p (TREE_VALUE (arg_types),
4416 ptr_type_node))
4417 use_cookie = false;
4418 /* If we need a cookie, adjust the number of bytes allocated. */
4419 if (use_cookie)
4420 {
4421 /* Update the total size. */
4422 *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
4423 if (size_check)
4424 {
4425 /* Set to (size_t)-1 if the size check fails. */
4426 gcc_assert (size_check != NULL_TREE);
4427 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4428 *size, TYPE_MAX_VALUE (sizetype));
4429 }
4430 /* Update the argument list to reflect the adjusted size. */
4431 (**args)[0] = *size;
4432 }
4433 else
4434 *cookie_size = NULL_TREE;
4435 }
4436
4437 /* Tell our caller which function we decided to call. */
4438 if (fn)
4439 *fn = cand->fn;
4440
4441 /* Build the CALL_EXPR. */
4442 return build_over_call (cand, LOOKUP_NORMAL, complain);
4443 }
4444
4445 /* Build a new call to operator(). This may change ARGS. */
4446
4447 static tree
4448 build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4449 {
4450 struct z_candidate *candidates = 0, *cand;
4451 tree fns, convs, first_mem_arg = NULL_TREE;
4452 bool any_viable_p;
4453 tree result = NULL_TREE;
4454 void *p;
4455
4456 obj = mark_lvalue_use (obj);
4457
4458 if (error_operand_p (obj))
4459 return error_mark_node;
4460
4461 tree type = TREE_TYPE (obj);
4462
4463 obj = prep_operand (obj);
4464
4465 if (TYPE_PTRMEMFUNC_P (type))
4466 {
4467 if (complain & tf_error)
4468 /* It's no good looking for an overloaded operator() on a
4469 pointer-to-member-function. */
4470 error ("pointer-to-member function %qE cannot be called without "
4471 "an object; consider using %<.*%> or %<->*%>", obj);
4472 return error_mark_node;
4473 }
4474
4475 if (TYPE_BINFO (type))
4476 {
4477 fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1);
4478 if (fns == error_mark_node)
4479 return error_mark_node;
4480 }
4481 else
4482 fns = NULL_TREE;
4483
4484 if (args != NULL && *args != NULL)
4485 {
4486 *args = resolve_args (*args, complain);
4487 if (*args == NULL)
4488 return error_mark_node;
4489 }
4490
4491 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4492 p = conversion_obstack_alloc (0);
4493
4494 if (fns)
4495 {
4496 first_mem_arg = obj;
4497
4498 add_candidates (BASELINK_FUNCTIONS (fns),
4499 first_mem_arg, *args, NULL_TREE,
4500 NULL_TREE, false,
4501 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
4502 LOOKUP_NORMAL, &candidates, complain);
4503 }
4504
4505 convs = lookup_conversions (type);
4506
4507 for (; convs; convs = TREE_CHAIN (convs))
4508 {
4509 tree totype = TREE_TYPE (convs);
4510
4511 if (TYPE_PTRFN_P (totype)
4512 || TYPE_REFFN_P (totype)
4513 || (TREE_CODE (totype) == REFERENCE_TYPE
4514 && TYPE_PTRFN_P (TREE_TYPE (totype))))
4515 for (ovl_iterator iter (TREE_VALUE (convs)); iter; ++iter)
4516 {
4517 tree fn = *iter;
4518
4519 if (DECL_NONCONVERTING_P (fn))
4520 continue;
4521
4522 if (TREE_CODE (fn) == TEMPLATE_DECL)
4523 add_template_conv_candidate
4524 (&candidates, fn, obj, *args, totype,
4525 /*access_path=*/NULL_TREE,
4526 /*conversion_path=*/NULL_TREE, complain);
4527 else
4528 add_conv_candidate (&candidates, fn, obj,
4529 *args, /*conversion_path=*/NULL_TREE,
4530 /*access_path=*/NULL_TREE, complain);
4531 }
4532 }
4533
4534 /* Be strict here because if we choose a bad conversion candidate, the
4535 errors we get won't mention the call context. */
4536 candidates = splice_viable (candidates, true, &any_viable_p);
4537 if (!any_viable_p)
4538 {
4539 if (complain & tf_error)
4540 {
4541 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
4542 build_tree_list_vec (*args));
4543 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4544 }
4545 result = error_mark_node;
4546 }
4547 else
4548 {
4549 cand = tourney (candidates, complain);
4550 if (cand == 0)
4551 {
4552 if (complain & tf_error)
4553 {
4554 error ("call of %<(%T) (%A)%> is ambiguous",
4555 TREE_TYPE (obj), build_tree_list_vec (*args));
4556 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4557 }
4558 result = error_mark_node;
4559 }
4560 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
4561 && DECL_OVERLOADED_OPERATOR_P (cand->fn)
4562 && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
4563 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4564 else
4565 {
4566 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
4567 obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
4568 -1, complain);
4569 else
4570 {
4571 gcc_checking_assert (TYPE_P (cand->fn));
4572 obj = convert_like (cand->convs[0], obj, complain);
4573 }
4574 obj = convert_from_reference (obj);
4575 result = cp_build_function_call_vec (obj, args, complain);
4576 }
4577 }
4578
4579 /* Free all the conversions we allocated. */
4580 obstack_free (&conversion_obstack, p);
4581
4582 return result;
4583 }
4584
4585 /* Wrapper for above. */
4586
4587 tree
4588 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4589 {
4590 tree ret;
4591 bool subtime = timevar_cond_start (TV_OVERLOAD);
4592 ret = build_op_call_1 (obj, args, complain);
4593 timevar_cond_stop (TV_OVERLOAD, subtime);
4594 return ret;
4595 }
4596
4597 /* Called by op_error to prepare format strings suitable for the error
4598 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4599 and a suffix (controlled by NTYPES). */
4600
4601 static const char *
4602 op_error_string (const char *errmsg, int ntypes, bool match)
4603 {
4604 const char *msg;
4605
4606 const char *msgp = concat (match ? G_("ambiguous overload for ")
4607 : G_("no match for "), errmsg, NULL);
4608
4609 if (ntypes == 3)
4610 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
4611 else if (ntypes == 2)
4612 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
4613 else
4614 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
4615
4616 return msg;
4617 }
4618
4619 static void
4620 op_error (location_t loc, enum tree_code code, enum tree_code code2,
4621 tree arg1, tree arg2, tree arg3, bool match)
4622 {
4623 const char *opname;
4624
4625 if (code == MODIFY_EXPR)
4626 opname = assignment_operator_name_info[code2].name;
4627 else
4628 opname = operator_name_info[code].name;
4629
4630 switch (code)
4631 {
4632 case COND_EXPR:
4633 if (flag_diagnostics_show_caret)
4634 error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
4635 3, match),
4636 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4637 else
4638 error_at (loc, op_error_string (G_("ternary %<operator?:%> "
4639 "in %<%E ? %E : %E%>"), 3, match),
4640 arg1, arg2, arg3,
4641 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4642 break;
4643
4644 case POSTINCREMENT_EXPR:
4645 case POSTDECREMENT_EXPR:
4646 if (flag_diagnostics_show_caret)
4647 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4648 opname, TREE_TYPE (arg1));
4649 else
4650 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4651 1, match),
4652 opname, arg1, opname, TREE_TYPE (arg1));
4653 break;
4654
4655 case ARRAY_REF:
4656 if (flag_diagnostics_show_caret)
4657 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
4658 TREE_TYPE (arg1), TREE_TYPE (arg2));
4659 else
4660 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
4661 2, match),
4662 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
4663 break;
4664
4665 case REALPART_EXPR:
4666 case IMAGPART_EXPR:
4667 if (flag_diagnostics_show_caret)
4668 error_at (loc, op_error_string (G_("%qs"), 1, match),
4669 opname, TREE_TYPE (arg1));
4670 else
4671 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
4672 opname, opname, arg1, TREE_TYPE (arg1));
4673 break;
4674
4675 default:
4676 if (arg2)
4677 if (flag_diagnostics_show_caret)
4678 error_at (loc, op_error_string (G_("%<operator%s%>"), 2, match),
4679 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
4680 else
4681 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
4682 2, match),
4683 opname, arg1, opname, arg2,
4684 TREE_TYPE (arg1), TREE_TYPE (arg2));
4685 else
4686 if (flag_diagnostics_show_caret)
4687 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4688 opname, TREE_TYPE (arg1));
4689 else
4690 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
4691 1, match),
4692 opname, opname, arg1, TREE_TYPE (arg1));
4693 break;
4694 }
4695 }
4696
4697 /* Return the implicit conversion sequence that could be used to
4698 convert E1 to E2 in [expr.cond]. */
4699
4700 static conversion *
4701 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
4702 {
4703 tree t1 = non_reference (TREE_TYPE (e1));
4704 tree t2 = non_reference (TREE_TYPE (e2));
4705 conversion *conv;
4706 bool good_base;
4707
4708 /* [expr.cond]
4709
4710 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
4711 implicitly converted (clause _conv_) to the type "lvalue reference to
4712 T2", subject to the constraint that in the conversion the
4713 reference must bind directly (_dcl.init.ref_) to an lvalue.
4714
4715 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
4716 implicitly converted to the type "rvalue reference to T2", subject to
4717 the constraint that the reference must bind directly. */
4718 if (glvalue_p (e2))
4719 {
4720 tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
4721 conv = implicit_conversion (rtype,
4722 t1,
4723 e1,
4724 /*c_cast_p=*/false,
4725 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
4726 |LOOKUP_ONLYCONVERTING,
4727 complain);
4728 if (conv && !conv->bad_p)
4729 return conv;
4730 }
4731
4732 /* If E2 is a prvalue or if neither of the conversions above can be done
4733 and at least one of the operands has (possibly cv-qualified) class
4734 type: */
4735 if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
4736 return NULL;
4737
4738 /* [expr.cond]
4739
4740 If E1 and E2 have class type, and the underlying class types are
4741 the same or one is a base class of the other: E1 can be converted
4742 to match E2 if the class of T2 is the same type as, or a base
4743 class of, the class of T1, and the cv-qualification of T2 is the
4744 same cv-qualification as, or a greater cv-qualification than, the
4745 cv-qualification of T1. If the conversion is applied, E1 is
4746 changed to an rvalue of type T2 that still refers to the original
4747 source class object (or the appropriate subobject thereof). */
4748 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
4749 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
4750 {
4751 if (good_base && at_least_as_qualified_p (t2, t1))
4752 {
4753 conv = build_identity_conv (t1, e1);
4754 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
4755 TYPE_MAIN_VARIANT (t2)))
4756 conv = build_conv (ck_base, t2, conv);
4757 else
4758 conv = build_conv (ck_rvalue, t2, conv);
4759 return conv;
4760 }
4761 else
4762 return NULL;
4763 }
4764 else
4765 /* [expr.cond]
4766
4767 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
4768 converted to the type that expression E2 would have if E2 were
4769 converted to an rvalue (or the type it has, if E2 is an rvalue). */
4770 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
4771 LOOKUP_IMPLICIT, complain);
4772 }
4773
4774 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
4775 arguments to the conditional expression. */
4776
4777 static tree
4778 build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
4779 tsubst_flags_t complain)
4780 {
4781 tree arg2_type;
4782 tree arg3_type;
4783 tree result = NULL_TREE;
4784 tree result_type = NULL_TREE;
4785 bool is_lvalue = true;
4786 struct z_candidate *candidates = 0;
4787 struct z_candidate *cand;
4788 void *p;
4789 tree orig_arg2, orig_arg3;
4790
4791 /* As a G++ extension, the second argument to the conditional can be
4792 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
4793 c'.) If the second operand is omitted, make sure it is
4794 calculated only once. */
4795 if (!arg2)
4796 {
4797 if (complain & tf_error)
4798 pedwarn (loc, OPT_Wpedantic,
4799 "ISO C++ forbids omitting the middle term of a ?: expression");
4800
4801 if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
4802 warn_for_omitted_condop (loc, arg1);
4803
4804 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
4805 if (lvalue_p (arg1))
4806 arg2 = arg1 = cp_stabilize_reference (arg1);
4807 else
4808 arg2 = arg1 = save_expr (arg1);
4809 }
4810
4811 /* If something has already gone wrong, just pass that fact up the
4812 tree. */
4813 if (error_operand_p (arg1)
4814 || error_operand_p (arg2)
4815 || error_operand_p (arg3))
4816 return error_mark_node;
4817
4818 orig_arg2 = arg2;
4819 orig_arg3 = arg3;
4820
4821 if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
4822 {
4823 tree arg1_type = TREE_TYPE (arg1);
4824
4825 /* If arg1 is another cond_expr choosing between -1 and 0,
4826 then we can use its comparison. It may help to avoid
4827 additional comparison, produce more accurate diagnostics
4828 and enables folding. */
4829 if (TREE_CODE (arg1) == VEC_COND_EXPR
4830 && integer_minus_onep (TREE_OPERAND (arg1, 1))
4831 && integer_zerop (TREE_OPERAND (arg1, 2)))
4832 arg1 = TREE_OPERAND (arg1, 0);
4833
4834 arg1 = force_rvalue (arg1, complain);
4835 arg2 = force_rvalue (arg2, complain);
4836 arg3 = force_rvalue (arg3, complain);
4837
4838 /* force_rvalue can return error_mark on valid arguments. */
4839 if (error_operand_p (arg1)
4840 || error_operand_p (arg2)
4841 || error_operand_p (arg3))
4842 return error_mark_node;
4843
4844 arg2_type = TREE_TYPE (arg2);
4845 arg3_type = TREE_TYPE (arg3);
4846
4847 if (!VECTOR_TYPE_P (arg2_type)
4848 && !VECTOR_TYPE_P (arg3_type))
4849 {
4850 /* Rely on the error messages of the scalar version. */
4851 tree scal = build_conditional_expr_1 (loc, integer_one_node,
4852 orig_arg2, orig_arg3, complain);
4853 if (scal == error_mark_node)
4854 return error_mark_node;
4855 tree stype = TREE_TYPE (scal);
4856 tree ctype = TREE_TYPE (arg1_type);
4857 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
4858 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
4859 {
4860 if (complain & tf_error)
4861 error_at (loc, "inferred scalar type %qT is not an integer or "
4862 "floating point type of the same size as %qT", stype,
4863 COMPARISON_CLASS_P (arg1)
4864 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4865 : ctype);
4866 return error_mark_node;
4867 }
4868
4869 tree vtype = build_opaque_vector_type (stype,
4870 TYPE_VECTOR_SUBPARTS (arg1_type));
4871 /* We could pass complain & tf_warning to unsafe_conversion_p,
4872 but the warnings (like Wsign-conversion) have already been
4873 given by the scalar build_conditional_expr_1. We still check
4874 unsafe_conversion_p to forbid truncating long long -> float. */
4875 if (unsafe_conversion_p (loc, stype, arg2, NULL_TREE, false))
4876 {
4877 if (complain & tf_error)
4878 error_at (loc, "conversion of scalar %qH to vector %qI "
4879 "involves truncation", arg2_type, vtype);
4880 return error_mark_node;
4881 }
4882 if (unsafe_conversion_p (loc, stype, arg3, NULL_TREE, false))
4883 {
4884 if (complain & tf_error)
4885 error_at (loc, "conversion of scalar %qH to vector %qI "
4886 "involves truncation", arg3_type, vtype);
4887 return error_mark_node;
4888 }
4889
4890 arg2 = cp_convert (stype, arg2, complain);
4891 arg2 = save_expr (arg2);
4892 arg2 = build_vector_from_val (vtype, arg2);
4893 arg2_type = vtype;
4894 arg3 = cp_convert (stype, arg3, complain);
4895 arg3 = save_expr (arg3);
4896 arg3 = build_vector_from_val (vtype, arg3);
4897 arg3_type = vtype;
4898 }
4899
4900 if (VECTOR_TYPE_P (arg2_type) != VECTOR_TYPE_P (arg3_type))
4901 {
4902 enum stv_conv convert_flag =
4903 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
4904 complain & tf_error);
4905
4906 switch (convert_flag)
4907 {
4908 case stv_error:
4909 return error_mark_node;
4910 case stv_firstarg:
4911 {
4912 arg2 = save_expr (arg2);
4913 arg2 = convert (TREE_TYPE (arg3_type), arg2);
4914 arg2 = build_vector_from_val (arg3_type, arg2);
4915 arg2_type = TREE_TYPE (arg2);
4916 break;
4917 }
4918 case stv_secondarg:
4919 {
4920 arg3 = save_expr (arg3);
4921 arg3 = convert (TREE_TYPE (arg2_type), arg3);
4922 arg3 = build_vector_from_val (arg2_type, arg3);
4923 arg3_type = TREE_TYPE (arg3);
4924 break;
4925 }
4926 default:
4927 break;
4928 }
4929 }
4930
4931 if (!same_type_p (arg2_type, arg3_type)
4932 || TYPE_VECTOR_SUBPARTS (arg1_type)
4933 != TYPE_VECTOR_SUBPARTS (arg2_type)
4934 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
4935 {
4936 if (complain & tf_error)
4937 error_at (loc,
4938 "incompatible vector types in conditional expression: "
4939 "%qT, %qT and %qT", TREE_TYPE (arg1),
4940 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
4941 return error_mark_node;
4942 }
4943
4944 if (!COMPARISON_CLASS_P (arg1))
4945 {
4946 tree cmp_type = build_same_sized_truth_vector_type (arg1_type);
4947 arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
4948 }
4949 return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
4950 }
4951
4952 /* [expr.cond]
4953
4954 The first expression is implicitly converted to bool (clause
4955 _conv_). */
4956 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
4957 LOOKUP_NORMAL);
4958 if (error_operand_p (arg1))
4959 return error_mark_node;
4960
4961 /* [expr.cond]
4962
4963 If either the second or the third operand has type (possibly
4964 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
4965 array-to-pointer (_conv.array_), and function-to-pointer
4966 (_conv.func_) standard conversions are performed on the second
4967 and third operands. */
4968 arg2_type = unlowered_expr_type (arg2);
4969 arg3_type = unlowered_expr_type (arg3);
4970 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
4971 {
4972 /* Do the conversions. We don't these for `void' type arguments
4973 since it can't have any effect and since decay_conversion
4974 does not handle that case gracefully. */
4975 if (!VOID_TYPE_P (arg2_type))
4976 arg2 = decay_conversion (arg2, complain);
4977 if (!VOID_TYPE_P (arg3_type))
4978 arg3 = decay_conversion (arg3, complain);
4979 arg2_type = TREE_TYPE (arg2);
4980 arg3_type = TREE_TYPE (arg3);
4981
4982 /* [expr.cond]
4983
4984 One of the following shall hold:
4985
4986 --The second or the third operand (but not both) is a
4987 throw-expression (_except.throw_); the result is of the
4988 type of the other and is an rvalue.
4989
4990 --Both the second and the third operands have type void; the
4991 result is of type void and is an rvalue.
4992
4993 We must avoid calling force_rvalue for expressions of type
4994 "void" because it will complain that their value is being
4995 used. */
4996 if (TREE_CODE (arg2) == THROW_EXPR
4997 && TREE_CODE (arg3) != THROW_EXPR)
4998 {
4999 if (!VOID_TYPE_P (arg3_type))
5000 {
5001 arg3 = force_rvalue (arg3, complain);
5002 if (arg3 == error_mark_node)
5003 return error_mark_node;
5004 }
5005 arg3_type = TREE_TYPE (arg3);
5006 result_type = arg3_type;
5007 }
5008 else if (TREE_CODE (arg2) != THROW_EXPR
5009 && TREE_CODE (arg3) == THROW_EXPR)
5010 {
5011 if (!VOID_TYPE_P (arg2_type))
5012 {
5013 arg2 = force_rvalue (arg2, complain);
5014 if (arg2 == error_mark_node)
5015 return error_mark_node;
5016 }
5017 arg2_type = TREE_TYPE (arg2);
5018 result_type = arg2_type;
5019 }
5020 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
5021 result_type = void_type_node;
5022 else
5023 {
5024 if (complain & tf_error)
5025 {
5026 if (VOID_TYPE_P (arg2_type))
5027 error_at (EXPR_LOC_OR_LOC (arg3, loc),
5028 "second operand to the conditional operator "
5029 "is of type %<void%>, but the third operand is "
5030 "neither a throw-expression nor of type %<void%>");
5031 else
5032 error_at (EXPR_LOC_OR_LOC (arg2, loc),
5033 "third operand to the conditional operator "
5034 "is of type %<void%>, but the second operand is "
5035 "neither a throw-expression nor of type %<void%>");
5036 }
5037 return error_mark_node;
5038 }
5039
5040 is_lvalue = false;
5041 goto valid_operands;
5042 }
5043 /* [expr.cond]
5044
5045 Otherwise, if the second and third operand have different types,
5046 and either has (possibly cv-qualified) class type, or if both are
5047 glvalues of the same value category and the same type except for
5048 cv-qualification, an attempt is made to convert each of those operands
5049 to the type of the other. */
5050 else if (!same_type_p (arg2_type, arg3_type)
5051 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
5052 || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
5053 arg3_type)
5054 && glvalue_p (arg2) && glvalue_p (arg3)
5055 && lvalue_p (arg2) == lvalue_p (arg3))))
5056 {
5057 conversion *conv2;
5058 conversion *conv3;
5059 bool converted = false;
5060
5061 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5062 p = conversion_obstack_alloc (0);
5063
5064 conv2 = conditional_conversion (arg2, arg3, complain);
5065 conv3 = conditional_conversion (arg3, arg2, complain);
5066
5067 /* [expr.cond]
5068
5069 If both can be converted, or one can be converted but the
5070 conversion is ambiguous, the program is ill-formed. If
5071 neither can be converted, the operands are left unchanged and
5072 further checking is performed as described below. If exactly
5073 one conversion is possible, that conversion is applied to the
5074 chosen operand and the converted operand is used in place of
5075 the original operand for the remainder of this section. */
5076 if ((conv2 && !conv2->bad_p
5077 && conv3 && !conv3->bad_p)
5078 || (conv2 && conv2->kind == ck_ambig)
5079 || (conv3 && conv3->kind == ck_ambig))
5080 {
5081 if (complain & tf_error)
5082 {
5083 error_at (loc, "operands to ?: have different types %qT and %qT",
5084 arg2_type, arg3_type);
5085 if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
5086 inform (loc, " and each type can be converted to the other");
5087 else if (conv2 && conv2->kind == ck_ambig)
5088 convert_like (conv2, arg2, complain);
5089 else
5090 convert_like (conv3, arg3, complain);
5091 }
5092 result = error_mark_node;
5093 }
5094 else if (conv2 && !conv2->bad_p)
5095 {
5096 arg2 = convert_like (conv2, arg2, complain);
5097 arg2 = convert_from_reference (arg2);
5098 arg2_type = TREE_TYPE (arg2);
5099 /* Even if CONV2 is a valid conversion, the result of the
5100 conversion may be invalid. For example, if ARG3 has type
5101 "volatile X", and X does not have a copy constructor
5102 accepting a "volatile X&", then even if ARG2 can be
5103 converted to X, the conversion will fail. */
5104 if (error_operand_p (arg2))
5105 result = error_mark_node;
5106 converted = true;
5107 }
5108 else if (conv3 && !conv3->bad_p)
5109 {
5110 arg3 = convert_like (conv3, arg3, complain);
5111 arg3 = convert_from_reference (arg3);
5112 arg3_type = TREE_TYPE (arg3);
5113 if (error_operand_p (arg3))
5114 result = error_mark_node;
5115 converted = true;
5116 }
5117
5118 /* Free all the conversions we allocated. */
5119 obstack_free (&conversion_obstack, p);
5120
5121 if (result)
5122 return result;
5123
5124 /* If, after the conversion, both operands have class type,
5125 treat the cv-qualification of both operands as if it were the
5126 union of the cv-qualification of the operands.
5127
5128 The standard is not clear about what to do in this
5129 circumstance. For example, if the first operand has type
5130 "const X" and the second operand has a user-defined
5131 conversion to "volatile X", what is the type of the second
5132 operand after this step? Making it be "const X" (matching
5133 the first operand) seems wrong, as that discards the
5134 qualification without actually performing a copy. Leaving it
5135 as "volatile X" seems wrong as that will result in the
5136 conditional expression failing altogether, even though,
5137 according to this step, the one operand could be converted to
5138 the type of the other. */
5139 if (converted
5140 && CLASS_TYPE_P (arg2_type)
5141 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
5142 arg2_type = arg3_type =
5143 cp_build_qualified_type (arg2_type,
5144 cp_type_quals (arg2_type)
5145 | cp_type_quals (arg3_type));
5146 }
5147
5148 /* [expr.cond]
5149
5150 If the second and third operands are glvalues of the same value
5151 category and have the same type, the result is of that type and
5152 value category. */
5153 if (((lvalue_p (arg2) && lvalue_p (arg3))
5154 || (xvalue_p (arg2) && xvalue_p (arg3)))
5155 && same_type_p (arg2_type, arg3_type))
5156 {
5157 result_type = arg2_type;
5158 arg2 = mark_lvalue_use (arg2);
5159 arg3 = mark_lvalue_use (arg3);
5160 goto valid_operands;
5161 }
5162
5163 /* [expr.cond]
5164
5165 Otherwise, the result is an rvalue. If the second and third
5166 operand do not have the same type, and either has (possibly
5167 cv-qualified) class type, overload resolution is used to
5168 determine the conversions (if any) to be applied to the operands
5169 (_over.match.oper_, _over.built_). */
5170 is_lvalue = false;
5171 if (!same_type_p (arg2_type, arg3_type)
5172 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
5173 {
5174 tree args[3];
5175 conversion *conv;
5176 bool any_viable_p;
5177
5178 /* Rearrange the arguments so that add_builtin_candidate only has
5179 to know about two args. In build_builtin_candidate, the
5180 arguments are unscrambled. */
5181 args[0] = arg2;
5182 args[1] = arg3;
5183 args[2] = arg1;
5184 add_builtin_candidates (&candidates,
5185 COND_EXPR,
5186 NOP_EXPR,
5187 cp_operator_id (COND_EXPR),
5188 args,
5189 LOOKUP_NORMAL, complain);
5190
5191 /* [expr.cond]
5192
5193 If the overload resolution fails, the program is
5194 ill-formed. */
5195 candidates = splice_viable (candidates, false, &any_viable_p);
5196 if (!any_viable_p)
5197 {
5198 if (complain & tf_error)
5199 error_at (loc, "operands to ?: have different types %qT and %qT",
5200 arg2_type, arg3_type);
5201 return error_mark_node;
5202 }
5203 cand = tourney (candidates, complain);
5204 if (!cand)
5205 {
5206 if (complain & tf_error)
5207 {
5208 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
5209 print_z_candidates (loc, candidates);
5210 }
5211 return error_mark_node;
5212 }
5213
5214 /* [expr.cond]
5215
5216 Otherwise, the conversions thus determined are applied, and
5217 the converted operands are used in place of the original
5218 operands for the remainder of this section. */
5219 conv = cand->convs[0];
5220 arg1 = convert_like (conv, arg1, complain);
5221 conv = cand->convs[1];
5222 arg2 = convert_like (conv, arg2, complain);
5223 arg2_type = TREE_TYPE (arg2);
5224 conv = cand->convs[2];
5225 arg3 = convert_like (conv, arg3, complain);
5226 arg3_type = TREE_TYPE (arg3);
5227 }
5228
5229 /* [expr.cond]
5230
5231 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
5232 and function-to-pointer (_conv.func_) standard conversions are
5233 performed on the second and third operands.
5234
5235 We need to force the lvalue-to-rvalue conversion here for class types,
5236 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
5237 that isn't wrapped with a TARGET_EXPR plays havoc with exception
5238 regions. */
5239
5240 arg2 = force_rvalue (arg2, complain);
5241 if (!CLASS_TYPE_P (arg2_type))
5242 arg2_type = TREE_TYPE (arg2);
5243
5244 arg3 = force_rvalue (arg3, complain);
5245 if (!CLASS_TYPE_P (arg3_type))
5246 arg3_type = TREE_TYPE (arg3);
5247
5248 if (arg2 == error_mark_node || arg3 == error_mark_node)
5249 return error_mark_node;
5250
5251 /* [expr.cond]
5252
5253 After those conversions, one of the following shall hold:
5254
5255 --The second and third operands have the same type; the result is of
5256 that type. */
5257 if (same_type_p (arg2_type, arg3_type))
5258 result_type = arg2_type;
5259 /* [expr.cond]
5260
5261 --The second and third operands have arithmetic or enumeration
5262 type; the usual arithmetic conversions are performed to bring
5263 them to a common type, and the result is of that type. */
5264 else if ((ARITHMETIC_TYPE_P (arg2_type)
5265 || UNSCOPED_ENUM_P (arg2_type))
5266 && (ARITHMETIC_TYPE_P (arg3_type)
5267 || UNSCOPED_ENUM_P (arg3_type)))
5268 {
5269 /* In this case, there is always a common type. */
5270 result_type = type_after_usual_arithmetic_conversions (arg2_type,
5271 arg3_type);
5272 if (complain & tf_warning)
5273 do_warn_double_promotion (result_type, arg2_type, arg3_type,
5274 "implicit conversion from %qH to %qI to "
5275 "match other result of conditional",
5276 loc);
5277
5278 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
5279 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
5280 {
5281 if (TREE_CODE (orig_arg2) == CONST_DECL
5282 && TREE_CODE (orig_arg3) == CONST_DECL
5283 && DECL_CONTEXT (orig_arg2) == DECL_CONTEXT (orig_arg3))
5284 /* Two enumerators from the same enumeration can have different
5285 types when the enumeration is still being defined. */;
5286 else if (complain & tf_warning)
5287 warning_at (loc, OPT_Wenum_compare, "enumeral mismatch in "
5288 "conditional expression: %qT vs %qT",
5289 arg2_type, arg3_type);
5290 }
5291 else if (extra_warnings
5292 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
5293 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
5294 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
5295 && !same_type_p (arg2_type,
5296 type_promotes_to (arg3_type)))))
5297 {
5298 if (complain & tf_warning)
5299 warning_at (loc, OPT_Wextra, "enumeral and non-enumeral type in "
5300 "conditional expression");
5301 }
5302
5303 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5304 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5305 }
5306 /* [expr.cond]
5307
5308 --The second and third operands have pointer type, or one has
5309 pointer type and the other is a null pointer constant; pointer
5310 conversions (_conv.ptr_) and qualification conversions
5311 (_conv.qual_) are performed to bring them to their composite
5312 pointer type (_expr.rel_). The result is of the composite
5313 pointer type.
5314
5315 --The second and third operands have pointer to member type, or
5316 one has pointer to member type and the other is a null pointer
5317 constant; pointer to member conversions (_conv.mem_) and
5318 qualification conversions (_conv.qual_) are performed to bring
5319 them to a common type, whose cv-qualification shall match the
5320 cv-qualification of either the second or the third operand.
5321 The result is of the common type. */
5322 else if ((null_ptr_cst_p (arg2)
5323 && TYPE_PTR_OR_PTRMEM_P (arg3_type))
5324 || (null_ptr_cst_p (arg3)
5325 && TYPE_PTR_OR_PTRMEM_P (arg2_type))
5326 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
5327 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
5328 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
5329 {
5330 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
5331 arg3, CPO_CONDITIONAL_EXPR,
5332 complain);
5333 if (result_type == error_mark_node)
5334 return error_mark_node;
5335 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5336 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5337 }
5338
5339 if (!result_type)
5340 {
5341 if (complain & tf_error)
5342 error_at (loc, "operands to ?: have different types %qT and %qT",
5343 arg2_type, arg3_type);
5344 return error_mark_node;
5345 }
5346
5347 if (arg2 == error_mark_node || arg3 == error_mark_node)
5348 return error_mark_node;
5349
5350 valid_operands:
5351 result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
5352
5353 /* If the ARG2 and ARG3 are the same and don't have side-effects,
5354 warn here, because the COND_EXPR will be turned into ARG2. */
5355 if (warn_duplicated_branches
5356 && (arg2 == arg3 || operand_equal_p (arg2, arg3, 0)))
5357 warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
5358 "this condition has identical branches");
5359
5360 /* We can't use result_type below, as fold might have returned a
5361 throw_expr. */
5362
5363 if (!is_lvalue)
5364 {
5365 /* Expand both sides into the same slot, hopefully the target of
5366 the ?: expression. We used to check for TARGET_EXPRs here,
5367 but now we sometimes wrap them in NOP_EXPRs so the test would
5368 fail. */
5369 if (CLASS_TYPE_P (TREE_TYPE (result)))
5370 result = get_target_expr_sfinae (result, complain);
5371 /* If this expression is an rvalue, but might be mistaken for an
5372 lvalue, we must add a NON_LVALUE_EXPR. */
5373 result = rvalue (result);
5374 }
5375 else
5376 result = force_paren_expr (result);
5377
5378 return result;
5379 }
5380
5381 /* Wrapper for above. */
5382
5383 tree
5384 build_conditional_expr (location_t loc, tree arg1, tree arg2, tree arg3,
5385 tsubst_flags_t complain)
5386 {
5387 tree ret;
5388 bool subtime = timevar_cond_start (TV_OVERLOAD);
5389 ret = build_conditional_expr_1 (loc, arg1, arg2, arg3, complain);
5390 timevar_cond_stop (TV_OVERLOAD, subtime);
5391 return ret;
5392 }
5393
5394 /* OPERAND is an operand to an expression. Perform necessary steps
5395 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
5396 returned. */
5397
5398 static tree
5399 prep_operand (tree operand)
5400 {
5401 if (operand)
5402 {
5403 if (CLASS_TYPE_P (TREE_TYPE (operand))
5404 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
5405 /* Make sure the template type is instantiated now. */
5406 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
5407 }
5408
5409 return operand;
5410 }
5411
5412 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5413 OVERLOAD) to the CANDIDATES, returning an updated list of
5414 CANDIDATES. The ARGS are the arguments provided to the call;
5415 if FIRST_ARG is non-null it is the implicit object argument,
5416 otherwise the first element of ARGS is used if needed. The
5417 EXPLICIT_TARGS are explicit template arguments provided.
5418 TEMPLATE_ONLY is true if only template functions should be
5419 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5420 add_function_candidate. */
5421
5422 static void
5423 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
5424 tree return_type,
5425 tree explicit_targs, bool template_only,
5426 tree conversion_path, tree access_path,
5427 int flags,
5428 struct z_candidate **candidates,
5429 tsubst_flags_t complain)
5430 {
5431 tree ctype;
5432 const vec<tree, va_gc> *non_static_args;
5433 bool check_list_ctor = false;
5434 bool check_converting = false;
5435 unification_kind_t strict;
5436
5437 if (!fns)
5438 return;
5439
5440 /* Precalculate special handling of constructors and conversion ops. */
5441 tree fn = OVL_FIRST (fns);
5442 if (DECL_CONV_FN_P (fn))
5443 {
5444 check_list_ctor = false;
5445 check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
5446 if (flags & LOOKUP_NO_CONVERSION)
5447 /* We're doing return_type(x). */
5448 strict = DEDUCE_CONV;
5449 else
5450 /* We're doing x.operator return_type(). */
5451 strict = DEDUCE_EXACT;
5452 /* [over.match.funcs] For conversion functions, the function
5453 is considered to be a member of the class of the implicit
5454 object argument for the purpose of defining the type of
5455 the implicit object parameter. */
5456 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
5457 }
5458 else
5459 {
5460 if (DECL_CONSTRUCTOR_P (fn))
5461 {
5462 check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
5463 /* For list-initialization we consider explicit constructors
5464 and complain if one is chosen. */
5465 check_converting
5466 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
5467 == LOOKUP_ONLYCONVERTING);
5468 }
5469 strict = DEDUCE_CALL;
5470 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
5471 }
5472
5473 if (first_arg)
5474 non_static_args = args;
5475 else
5476 /* Delay creating the implicit this parameter until it is needed. */
5477 non_static_args = NULL;
5478
5479 for (lkp_iterator iter (fns); iter; ++iter)
5480 {
5481 fn = *iter;
5482
5483 if (check_converting && DECL_NONCONVERTING_P (fn))
5484 continue;
5485 if (check_list_ctor && !is_list_ctor (fn))
5486 continue;
5487
5488 tree fn_first_arg = NULL_TREE;
5489 const vec<tree, va_gc> *fn_args = args;
5490
5491 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
5492 {
5493 /* Figure out where the object arg comes from. If this
5494 function is a non-static member and we didn't get an
5495 implicit object argument, move it out of args. */
5496 if (first_arg == NULL_TREE)
5497 {
5498 unsigned int ix;
5499 tree arg;
5500 vec<tree, va_gc> *tempvec;
5501 vec_alloc (tempvec, args->length () - 1);
5502 for (ix = 1; args->iterate (ix, &arg); ++ix)
5503 tempvec->quick_push (arg);
5504 non_static_args = tempvec;
5505 first_arg = (*args)[0];
5506 }
5507
5508 fn_first_arg = first_arg;
5509 fn_args = non_static_args;
5510 }
5511
5512 if (TREE_CODE (fn) == TEMPLATE_DECL)
5513 add_template_candidate (candidates,
5514 fn,
5515 ctype,
5516 explicit_targs,
5517 fn_first_arg,
5518 fn_args,
5519 return_type,
5520 access_path,
5521 conversion_path,
5522 flags,
5523 strict,
5524 complain);
5525 else if (!template_only)
5526 add_function_candidate (candidates,
5527 fn,
5528 ctype,
5529 fn_first_arg,
5530 fn_args,
5531 access_path,
5532 conversion_path,
5533 flags,
5534 complain);
5535 }
5536 }
5537
5538 /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
5539 -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
5540
5541 static int
5542 op_is_ordered (tree_code code)
5543 {
5544 switch (code)
5545 {
5546 // 5. b @= a
5547 case MODIFY_EXPR:
5548 return (flag_strong_eval_order > 1 ? -1 : 0);
5549
5550 // 6. a[b]
5551 case ARRAY_REF:
5552 return (flag_strong_eval_order > 1 ? 1 : 0);
5553
5554 // 1. a.b
5555 // Not overloadable (yet).
5556 // 2. a->b
5557 // Only one argument.
5558 // 3. a->*b
5559 case MEMBER_REF:
5560 // 7. a << b
5561 case LSHIFT_EXPR:
5562 // 8. a >> b
5563 case RSHIFT_EXPR:
5564 return (flag_strong_eval_order ? 1 : 0);
5565
5566 default:
5567 return 0;
5568 }
5569 }
5570
5571 static tree
5572 build_new_op_1 (location_t loc, enum tree_code code, int flags, tree arg1,
5573 tree arg2, tree arg3, tree *overload, tsubst_flags_t complain)
5574 {
5575 struct z_candidate *candidates = 0, *cand;
5576 vec<tree, va_gc> *arglist;
5577 tree fnname;
5578 tree args[3];
5579 tree result = NULL_TREE;
5580 bool result_valid_p = false;
5581 enum tree_code code2 = NOP_EXPR;
5582 enum tree_code code_orig_arg1 = ERROR_MARK;
5583 enum tree_code code_orig_arg2 = ERROR_MARK;
5584 conversion *conv;
5585 void *p;
5586 bool strict_p;
5587 bool any_viable_p;
5588
5589 if (error_operand_p (arg1)
5590 || error_operand_p (arg2)
5591 || error_operand_p (arg3))
5592 return error_mark_node;
5593
5594 if (code == MODIFY_EXPR)
5595 {
5596 code2 = TREE_CODE (arg3);
5597 arg3 = NULL_TREE;
5598 fnname = cp_assignment_operator_id (code2);
5599 }
5600 else
5601 fnname = cp_operator_id (code);
5602
5603 arg1 = prep_operand (arg1);
5604
5605 bool memonly = false;
5606 switch (code)
5607 {
5608 case NEW_EXPR:
5609 case VEC_NEW_EXPR:
5610 case VEC_DELETE_EXPR:
5611 case DELETE_EXPR:
5612 /* Use build_op_new_call and build_op_delete_call instead. */
5613 gcc_unreachable ();
5614
5615 case CALL_EXPR:
5616 /* Use build_op_call instead. */
5617 gcc_unreachable ();
5618
5619 case TRUTH_ORIF_EXPR:
5620 case TRUTH_ANDIF_EXPR:
5621 case TRUTH_AND_EXPR:
5622 case TRUTH_OR_EXPR:
5623 /* These are saved for the sake of warn_logical_operator. */
5624 code_orig_arg1 = TREE_CODE (arg1);
5625 code_orig_arg2 = TREE_CODE (arg2);
5626 break;
5627 case GT_EXPR:
5628 case LT_EXPR:
5629 case GE_EXPR:
5630 case LE_EXPR:
5631 case EQ_EXPR:
5632 case NE_EXPR:
5633 /* These are saved for the sake of maybe_warn_bool_compare. */
5634 code_orig_arg1 = TREE_CODE (TREE_TYPE (arg1));
5635 code_orig_arg2 = TREE_CODE (TREE_TYPE (arg2));
5636 break;
5637
5638 /* =, ->, [], () must be non-static member functions. */
5639 case MODIFY_EXPR:
5640 if (code2 != NOP_EXPR)
5641 break;
5642 /* FALLTHRU */
5643 case COMPONENT_REF:
5644 case ARRAY_REF:
5645 memonly = true;
5646 break;
5647
5648 default:
5649 break;
5650 }
5651
5652 arg2 = prep_operand (arg2);
5653 arg3 = prep_operand (arg3);
5654
5655 if (code == COND_EXPR)
5656 /* Use build_conditional_expr instead. */
5657 gcc_unreachable ();
5658 else if (! OVERLOAD_TYPE_P (TREE_TYPE (arg1))
5659 && (! arg2 || ! OVERLOAD_TYPE_P (TREE_TYPE (arg2))))
5660 goto builtin;
5661
5662 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5663 arg2 = integer_zero_node;
5664
5665 vec_alloc (arglist, 3);
5666 arglist->quick_push (arg1);
5667 if (arg2 != NULL_TREE)
5668 arglist->quick_push (arg2);
5669 if (arg3 != NULL_TREE)
5670 arglist->quick_push (arg3);
5671
5672 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5673 p = conversion_obstack_alloc (0);
5674
5675 /* Add namespace-scope operators to the list of functions to
5676 consider. */
5677 if (!memonly)
5678 {
5679 tree fns = lookup_name_real (fnname, 0, 1, /*block_p=*/true, 0, 0);
5680 fns = lookup_arg_dependent (fnname, fns, arglist);
5681 add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
5682 NULL_TREE, false, NULL_TREE, NULL_TREE,
5683 flags, &candidates, complain);
5684 }
5685
5686 args[0] = arg1;
5687 args[1] = arg2;
5688 args[2] = NULL_TREE;
5689
5690 /* Add class-member operators to the candidate set. */
5691 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
5692 {
5693 tree fns;
5694
5695 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
5696 if (fns == error_mark_node)
5697 {
5698 result = error_mark_node;
5699 goto user_defined_result_ready;
5700 }
5701 if (fns)
5702 add_candidates (BASELINK_FUNCTIONS (fns),
5703 NULL_TREE, arglist, NULL_TREE,
5704 NULL_TREE, false,
5705 BASELINK_BINFO (fns),
5706 BASELINK_ACCESS_BINFO (fns),
5707 flags, &candidates, complain);
5708 }
5709 /* Per 13.3.1.2/3, 2nd bullet, if no operand has a class type, then
5710 only non-member functions that have type T1 or reference to
5711 cv-qualified-opt T1 for the first argument, if the first argument
5712 has an enumeration type, or T2 or reference to cv-qualified-opt
5713 T2 for the second argument, if the second argument has an
5714 enumeration type. Filter out those that don't match. */
5715 else if (! arg2 || ! CLASS_TYPE_P (TREE_TYPE (arg2)))
5716 {
5717 struct z_candidate **candp, **next;
5718
5719 for (candp = &candidates; *candp; candp = next)
5720 {
5721 tree parmlist, parmtype;
5722 int i, nargs = (arg2 ? 2 : 1);
5723
5724 cand = *candp;
5725 next = &cand->next;
5726
5727 parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5728
5729 for (i = 0; i < nargs; ++i)
5730 {
5731 parmtype = TREE_VALUE (parmlist);
5732
5733 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
5734 parmtype = TREE_TYPE (parmtype);
5735 if (TREE_CODE (TREE_TYPE (args[i])) == ENUMERAL_TYPE
5736 && (same_type_ignoring_top_level_qualifiers_p
5737 (TREE_TYPE (args[i]), parmtype)))
5738 break;
5739
5740 parmlist = TREE_CHAIN (parmlist);
5741 }
5742
5743 /* No argument has an appropriate type, so remove this
5744 candidate function from the list. */
5745 if (i == nargs)
5746 {
5747 *candp = cand->next;
5748 next = candp;
5749 }
5750 }
5751 }
5752
5753 add_builtin_candidates (&candidates, code, code2, fnname, args,
5754 flags, complain);
5755
5756 switch (code)
5757 {
5758 case COMPOUND_EXPR:
5759 case ADDR_EXPR:
5760 /* For these, the built-in candidates set is empty
5761 [over.match.oper]/3. We don't want non-strict matches
5762 because exact matches are always possible with built-in
5763 operators. The built-in candidate set for COMPONENT_REF
5764 would be empty too, but since there are no such built-in
5765 operators, we accept non-strict matches for them. */
5766 strict_p = true;
5767 break;
5768
5769 default:
5770 strict_p = false;
5771 break;
5772 }
5773
5774 candidates = splice_viable (candidates, strict_p, &any_viable_p);
5775 if (!any_viable_p)
5776 {
5777 switch (code)
5778 {
5779 case POSTINCREMENT_EXPR:
5780 case POSTDECREMENT_EXPR:
5781 /* Don't try anything fancy if we're not allowed to produce
5782 errors. */
5783 if (!(complain & tf_error))
5784 return error_mark_node;
5785
5786 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
5787 distinguish between prefix and postfix ++ and
5788 operator++() was used for both, so we allow this with
5789 -fpermissive. */
5790 else
5791 {
5792 const char *msg = (flag_permissive)
5793 ? G_("no %<%D(int)%> declared for postfix %qs,"
5794 " trying prefix operator instead")
5795 : G_("no %<%D(int)%> declared for postfix %qs");
5796 permerror (loc, msg, fnname, operator_name_info[code].name);
5797 }
5798
5799 if (!flag_permissive)
5800 return error_mark_node;
5801
5802 if (code == POSTINCREMENT_EXPR)
5803 code = PREINCREMENT_EXPR;
5804 else
5805 code = PREDECREMENT_EXPR;
5806 result = build_new_op_1 (loc, code, flags, arg1, NULL_TREE,
5807 NULL_TREE, overload, complain);
5808 break;
5809
5810 /* The caller will deal with these. */
5811 case ADDR_EXPR:
5812 case COMPOUND_EXPR:
5813 case COMPONENT_REF:
5814 result = NULL_TREE;
5815 result_valid_p = true;
5816 break;
5817
5818 default:
5819 if (complain & tf_error)
5820 {
5821 /* If one of the arguments of the operator represents
5822 an invalid use of member function pointer, try to report
5823 a meaningful error ... */
5824 if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
5825 || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
5826 || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
5827 /* We displayed the error message. */;
5828 else
5829 {
5830 /* ... Otherwise, report the more generic
5831 "no matching operator found" error */
5832 op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
5833 print_z_candidates (loc, candidates);
5834 }
5835 }
5836 result = error_mark_node;
5837 break;
5838 }
5839 }
5840 else
5841 {
5842 cand = tourney (candidates, complain);
5843 if (cand == 0)
5844 {
5845 if (complain & tf_error)
5846 {
5847 op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
5848 print_z_candidates (loc, candidates);
5849 }
5850 result = error_mark_node;
5851 }
5852 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5853 {
5854 if (overload)
5855 *overload = cand->fn;
5856
5857 if (resolve_args (arglist, complain) == NULL)
5858 result = error_mark_node;
5859 else
5860 result = build_over_call (cand, LOOKUP_NORMAL, complain);
5861
5862 if (trivial_fn_p (cand->fn))
5863 /* There won't be a CALL_EXPR. */;
5864 else if (result && result != error_mark_node)
5865 {
5866 tree call = extract_call_expr (result);
5867 CALL_EXPR_OPERATOR_SYNTAX (call) = true;
5868
5869 if (processing_template_decl && DECL_HIDDEN_FRIEND_P (cand->fn))
5870 /* This prevents build_new_function_call from discarding this
5871 function during instantiation of the enclosing template. */
5872 KOENIG_LOOKUP_P (call) = 1;
5873
5874 /* Specify evaluation order as per P0145R2. */
5875 CALL_EXPR_ORDERED_ARGS (call) = false;
5876 switch (op_is_ordered (code))
5877 {
5878 case -1:
5879 CALL_EXPR_REVERSE_ARGS (call) = true;
5880 break;
5881
5882 case 1:
5883 CALL_EXPR_ORDERED_ARGS (call) = true;
5884 break;
5885
5886 default:
5887 break;
5888 }
5889 }
5890 }
5891 else
5892 {
5893 /* Give any warnings we noticed during overload resolution. */
5894 if (cand->warnings && (complain & tf_warning))
5895 {
5896 struct candidate_warning *w;
5897 for (w = cand->warnings; w; w = w->next)
5898 joust (cand, w->loser, 1, complain);
5899 }
5900
5901 /* Check for comparison of different enum types. */
5902 switch (code)
5903 {
5904 case GT_EXPR:
5905 case LT_EXPR:
5906 case GE_EXPR:
5907 case LE_EXPR:
5908 case EQ_EXPR:
5909 case NE_EXPR:
5910 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
5911 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
5912 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5913 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
5914 && (complain & tf_warning))
5915 {
5916 warning (OPT_Wenum_compare,
5917 "comparison between %q#T and %q#T",
5918 TREE_TYPE (arg1), TREE_TYPE (arg2));
5919 }
5920 break;
5921 default:
5922 break;
5923 }
5924
5925 /* We need to strip any leading REF_BIND so that bitfields
5926 don't cause errors. This should not remove any important
5927 conversions, because builtins don't apply to class
5928 objects directly. */
5929 conv = cand->convs[0];
5930 if (conv->kind == ck_ref_bind)
5931 conv = next_conversion (conv);
5932 arg1 = convert_like (conv, arg1, complain);
5933
5934 if (arg2)
5935 {
5936 conv = cand->convs[1];
5937 if (conv->kind == ck_ref_bind)
5938 conv = next_conversion (conv);
5939 else
5940 arg2 = decay_conversion (arg2, complain);
5941
5942 /* We need to call warn_logical_operator before
5943 converting arg2 to a boolean_type, but after
5944 decaying an enumerator to its value. */
5945 if (complain & tf_warning)
5946 warn_logical_operator (loc, code, boolean_type_node,
5947 code_orig_arg1, arg1,
5948 code_orig_arg2, arg2);
5949
5950 arg2 = convert_like (conv, arg2, complain);
5951 }
5952 if (arg3)
5953 {
5954 conv = cand->convs[2];
5955 if (conv->kind == ck_ref_bind)
5956 conv = next_conversion (conv);
5957 arg3 = convert_like (conv, arg3, complain);
5958 }
5959
5960 }
5961 }
5962
5963 user_defined_result_ready:
5964
5965 /* Free all the conversions we allocated. */
5966 obstack_free (&conversion_obstack, p);
5967
5968 if (result || result_valid_p)
5969 return result;
5970
5971 builtin:
5972 switch (code)
5973 {
5974 case MODIFY_EXPR:
5975 return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
5976
5977 case INDIRECT_REF:
5978 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
5979
5980 case TRUTH_ANDIF_EXPR:
5981 case TRUTH_ORIF_EXPR:
5982 case TRUTH_AND_EXPR:
5983 case TRUTH_OR_EXPR:
5984 if (complain & tf_warning)
5985 warn_logical_operator (loc, code, boolean_type_node,
5986 code_orig_arg1, arg1,
5987 code_orig_arg2, arg2);
5988 /* Fall through. */
5989 case GT_EXPR:
5990 case LT_EXPR:
5991 case GE_EXPR:
5992 case LE_EXPR:
5993 case EQ_EXPR:
5994 case NE_EXPR:
5995 if ((complain & tf_warning)
5996 && ((code_orig_arg1 == BOOLEAN_TYPE)
5997 ^ (code_orig_arg2 == BOOLEAN_TYPE)))
5998 maybe_warn_bool_compare (loc, code, arg1, arg2);
5999 if (complain & tf_warning && warn_tautological_compare)
6000 warn_tautological_cmp (loc, code, arg1, arg2);
6001 /* Fall through. */
6002 case PLUS_EXPR:
6003 case MINUS_EXPR:
6004 case MULT_EXPR:
6005 case TRUNC_DIV_EXPR:
6006 case MAX_EXPR:
6007 case MIN_EXPR:
6008 case LSHIFT_EXPR:
6009 case RSHIFT_EXPR:
6010 case TRUNC_MOD_EXPR:
6011 case BIT_AND_EXPR:
6012 case BIT_IOR_EXPR:
6013 case BIT_XOR_EXPR:
6014 return cp_build_binary_op (loc, code, arg1, arg2, complain);
6015
6016 case UNARY_PLUS_EXPR:
6017 case NEGATE_EXPR:
6018 case BIT_NOT_EXPR:
6019 case TRUTH_NOT_EXPR:
6020 case PREINCREMENT_EXPR:
6021 case POSTINCREMENT_EXPR:
6022 case PREDECREMENT_EXPR:
6023 case POSTDECREMENT_EXPR:
6024 case REALPART_EXPR:
6025 case IMAGPART_EXPR:
6026 case ABS_EXPR:
6027 return cp_build_unary_op (code, arg1, candidates != 0, complain);
6028
6029 case ARRAY_REF:
6030 return cp_build_array_ref (input_location, arg1, arg2, complain);
6031
6032 case MEMBER_REF:
6033 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_ARROW_STAR,
6034 complain),
6035 arg2, complain);
6036
6037 /* The caller will deal with these. */
6038 case ADDR_EXPR:
6039 case COMPONENT_REF:
6040 case COMPOUND_EXPR:
6041 return NULL_TREE;
6042
6043 default:
6044 gcc_unreachable ();
6045 }
6046 return NULL_TREE;
6047 }
6048
6049 /* Wrapper for above. */
6050
6051 tree
6052 build_new_op (location_t loc, enum tree_code code, int flags,
6053 tree arg1, tree arg2, tree arg3,
6054 tree *overload, tsubst_flags_t complain)
6055 {
6056 tree ret;
6057 bool subtime = timevar_cond_start (TV_OVERLOAD);
6058 ret = build_new_op_1 (loc, code, flags, arg1, arg2, arg3,
6059 overload, complain);
6060 timevar_cond_stop (TV_OVERLOAD, subtime);
6061 return ret;
6062 }
6063
6064 /* CALL was returned by some call-building function; extract the actual
6065 CALL_EXPR from any bits that have been tacked on, e.g. by
6066 convert_from_reference. */
6067
6068 tree
6069 extract_call_expr (tree call)
6070 {
6071 while (TREE_CODE (call) == COMPOUND_EXPR)
6072 call = TREE_OPERAND (call, 1);
6073 if (REFERENCE_REF_P (call))
6074 call = TREE_OPERAND (call, 0);
6075 if (TREE_CODE (call) == TARGET_EXPR)
6076 call = TARGET_EXPR_INITIAL (call);
6077 gcc_assert (TREE_CODE (call) == CALL_EXPR
6078 || TREE_CODE (call) == AGGR_INIT_EXPR
6079 || call == error_mark_node);
6080 return call;
6081 }
6082
6083 /* Returns true if FN has two parameters, of which the second has type
6084 size_t. */
6085
6086 static bool
6087 second_parm_is_size_t (tree fn)
6088 {
6089 tree t = FUNCTION_ARG_CHAIN (fn);
6090 if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
6091 return false;
6092 t = TREE_CHAIN (t);
6093 if (t == void_list_node)
6094 return true;
6095 if (aligned_new_threshold && t
6096 && same_type_p (TREE_VALUE (t), align_type_node)
6097 && TREE_CHAIN (t) == void_list_node)
6098 return true;
6099 return false;
6100 }
6101
6102 /* True if T, an allocation function, has std::align_val_t as its second
6103 argument. */
6104
6105 bool
6106 aligned_allocation_fn_p (tree t)
6107 {
6108 if (!aligned_new_threshold)
6109 return false;
6110
6111 tree a = FUNCTION_ARG_CHAIN (t);
6112 return (a && same_type_p (TREE_VALUE (a), align_type_node));
6113 }
6114
6115 /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
6116 function (3.7.4.2 [basic.stc.dynamic.deallocation]) with a parameter of
6117 std::align_val_t. */
6118
6119 static bool
6120 aligned_deallocation_fn_p (tree t)
6121 {
6122 if (!aligned_new_threshold)
6123 return false;
6124
6125 /* A template instance is never a usual deallocation function,
6126 regardless of its signature. */
6127 if (TREE_CODE (t) == TEMPLATE_DECL
6128 || primary_template_instantiation_p (t))
6129 return false;
6130
6131 tree a = FUNCTION_ARG_CHAIN (t);
6132 if (same_type_p (TREE_VALUE (a), align_type_node)
6133 && TREE_CHAIN (a) == void_list_node)
6134 return true;
6135 if (!same_type_p (TREE_VALUE (a), size_type_node))
6136 return false;
6137 a = TREE_CHAIN (a);
6138 if (a && same_type_p (TREE_VALUE (a), align_type_node)
6139 && TREE_CHAIN (a) == void_list_node)
6140 return true;
6141 return false;
6142 }
6143
6144 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
6145 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
6146
6147 bool
6148 usual_deallocation_fn_p (tree t)
6149 {
6150 /* A template instance is never a usual deallocation function,
6151 regardless of its signature. */
6152 if (TREE_CODE (t) == TEMPLATE_DECL
6153 || primary_template_instantiation_p (t))
6154 return false;
6155
6156 /* If a class T has a member deallocation function named operator delete
6157 with exactly one parameter, then that function is a usual
6158 (non-placement) deallocation function. If class T does not declare
6159 such an operator delete but does declare a member deallocation
6160 function named operator delete with exactly two parameters, the second
6161 of which has type std::size_t (18.2), then this function is a usual
6162 deallocation function. */
6163 bool global = DECL_NAMESPACE_SCOPE_P (t);
6164 tree chain = FUNCTION_ARG_CHAIN (t);
6165 if (!chain)
6166 return false;
6167 if (chain == void_list_node
6168 || ((!global || flag_sized_deallocation)
6169 && second_parm_is_size_t (t)))
6170 return true;
6171 if (aligned_deallocation_fn_p (t))
6172 return true;
6173 return false;
6174 }
6175
6176 /* Build a call to operator delete. This has to be handled very specially,
6177 because the restrictions on what signatures match are different from all
6178 other call instances. For a normal delete, only a delete taking (void *)
6179 or (void *, size_t) is accepted. For a placement delete, only an exact
6180 match with the placement new is accepted.
6181
6182 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
6183 ADDR is the pointer to be deleted.
6184 SIZE is the size of the memory block to be deleted.
6185 GLOBAL_P is true if the delete-expression should not consider
6186 class-specific delete operators.
6187 PLACEMENT is the corresponding placement new call, or NULL_TREE.
6188
6189 If this call to "operator delete" is being generated as part to
6190 deallocate memory allocated via a new-expression (as per [expr.new]
6191 which requires that if the initialization throws an exception then
6192 we call a deallocation function), then ALLOC_FN is the allocation
6193 function. */
6194
6195 tree
6196 build_op_delete_call (enum tree_code code, tree addr, tree size,
6197 bool global_p, tree placement,
6198 tree alloc_fn, tsubst_flags_t complain)
6199 {
6200 tree fn = NULL_TREE;
6201 tree fns, fnname, type, t;
6202
6203 if (addr == error_mark_node)
6204 return error_mark_node;
6205
6206 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
6207
6208 fnname = cp_operator_id (code);
6209
6210 if (CLASS_TYPE_P (type)
6211 && COMPLETE_TYPE_P (complete_type (type))
6212 && !global_p)
6213 /* In [class.free]
6214
6215 If the result of the lookup is ambiguous or inaccessible, or if
6216 the lookup selects a placement deallocation function, the
6217 program is ill-formed.
6218
6219 Therefore, we ask lookup_fnfields to complain about ambiguity. */
6220 {
6221 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
6222 if (fns == error_mark_node)
6223 return error_mark_node;
6224 }
6225 else
6226 fns = NULL_TREE;
6227
6228 if (fns == NULL_TREE)
6229 fns = lookup_name_nonclass (fnname);
6230
6231 /* Strip const and volatile from addr. */
6232 addr = cp_convert (ptr_type_node, addr, complain);
6233
6234 if (placement)
6235 {
6236 /* "A declaration of a placement deallocation function matches the
6237 declaration of a placement allocation function if it has the same
6238 number of parameters and, after parameter transformations (8.3.5),
6239 all parameter types except the first are identical."
6240
6241 So we build up the function type we want and ask instantiate_type
6242 to get it for us. */
6243 t = FUNCTION_ARG_CHAIN (alloc_fn);
6244 t = tree_cons (NULL_TREE, ptr_type_node, t);
6245 t = build_function_type (void_type_node, t);
6246
6247 fn = instantiate_type (t, fns, tf_none);
6248 if (fn == error_mark_node)
6249 return NULL_TREE;
6250
6251 fn = MAYBE_BASELINK_FUNCTIONS (fn);
6252
6253 /* "If the lookup finds the two-parameter form of a usual deallocation
6254 function (3.7.4.2) and that function, considered as a placement
6255 deallocation function, would have been selected as a match for the
6256 allocation function, the program is ill-formed." */
6257 if (second_parm_is_size_t (fn))
6258 {
6259 const char *const msg1
6260 = G_("exception cleanup for this placement new selects "
6261 "non-placement operator delete");
6262 const char *const msg2
6263 = G_("%qD is a usual (non-placement) deallocation "
6264 "function in C++14 (or with -fsized-deallocation)");
6265
6266 /* But if the class has an operator delete (void *), then that is
6267 the usual deallocation function, so we shouldn't complain
6268 about using the operator delete (void *, size_t). */
6269 if (DECL_CLASS_SCOPE_P (fn))
6270 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns));
6271 iter; ++iter)
6272 {
6273 tree elt = *iter;
6274 if (usual_deallocation_fn_p (elt)
6275 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
6276 goto ok;
6277 }
6278 /* Before C++14 a two-parameter global deallocation function is
6279 always a placement deallocation function, but warn if
6280 -Wc++14-compat. */
6281 else if (!flag_sized_deallocation)
6282 {
6283 if ((complain & tf_warning)
6284 && warning (OPT_Wc__14_compat, msg1))
6285 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6286 goto ok;
6287 }
6288
6289 if (complain & tf_warning_or_error)
6290 {
6291 if (permerror (input_location, msg1))
6292 {
6293 /* Only mention C++14 for namespace-scope delete. */
6294 if (DECL_NAMESPACE_SCOPE_P (fn))
6295 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6296 else
6297 inform (DECL_SOURCE_LOCATION (fn),
6298 "%qD is a usual (non-placement) deallocation "
6299 "function", fn);
6300 }
6301 }
6302 else
6303 return error_mark_node;
6304 ok:;
6305 }
6306 }
6307 else
6308 /* "Any non-placement deallocation function matches a non-placement
6309 allocation function. If the lookup finds a single matching
6310 deallocation function, that function will be called; otherwise, no
6311 deallocation function will be called." */
6312 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
6313 {
6314 tree elt = *iter;
6315 if (usual_deallocation_fn_p (elt))
6316 {
6317 if (!fn)
6318 {
6319 fn = elt;
6320 continue;
6321 }
6322
6323 /* -- If the type has new-extended alignment, a function with a
6324 parameter of type std::align_val_t is preferred; otherwise a
6325 function without such a parameter is preferred. If exactly one
6326 preferred function is found, that function is selected and the
6327 selection process terminates. If more than one preferred
6328 function is found, all non-preferred functions are eliminated
6329 from further consideration. */
6330 if (aligned_new_threshold)
6331 {
6332 bool want_align = type_has_new_extended_alignment (type);
6333 bool fn_align = aligned_deallocation_fn_p (fn);
6334 bool elt_align = aligned_deallocation_fn_p (elt);
6335
6336 if (elt_align != fn_align)
6337 {
6338 if (want_align == elt_align)
6339 fn = elt;
6340 continue;
6341 }
6342 }
6343
6344 /* -- If the deallocation functions have class scope, the one
6345 without a parameter of type std::size_t is selected. */
6346 bool want_size;
6347 if (DECL_CLASS_SCOPE_P (fn))
6348 want_size = false;
6349
6350 /* -- If the type is complete and if, for the second alternative
6351 (delete array) only, the operand is a pointer to a class type
6352 with a non-trivial destructor or a (possibly multi-dimensional)
6353 array thereof, the function with a parameter of type std::size_t
6354 is selected.
6355
6356 -- Otherwise, it is unspecified whether a deallocation function
6357 with a parameter of type std::size_t is selected. */
6358 else
6359 {
6360 want_size = COMPLETE_TYPE_P (type);
6361 if (code == VEC_DELETE_EXPR
6362 && !TYPE_VEC_NEW_USES_COOKIE (type))
6363 /* We need a cookie to determine the array size. */
6364 want_size = false;
6365 }
6366 bool fn_size = second_parm_is_size_t (fn);
6367 bool elt_size = second_parm_is_size_t (elt);
6368 gcc_assert (fn_size != elt_size);
6369 if (want_size == elt_size)
6370 fn = elt;
6371 }
6372 }
6373
6374 /* If we have a matching function, call it. */
6375 if (fn)
6376 {
6377 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6378
6379 /* If the FN is a member function, make sure that it is
6380 accessible. */
6381 if (BASELINK_P (fns))
6382 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
6383 complain);
6384
6385 /* Core issue 901: It's ok to new a type with deleted delete. */
6386 if (DECL_DELETED_FN (fn) && alloc_fn)
6387 return NULL_TREE;
6388
6389 if (placement)
6390 {
6391 /* The placement args might not be suitable for overload
6392 resolution at this point, so build the call directly. */
6393 int nargs = call_expr_nargs (placement);
6394 tree *argarray = XALLOCAVEC (tree, nargs);
6395 int i;
6396 argarray[0] = addr;
6397 for (i = 1; i < nargs; i++)
6398 argarray[i] = CALL_EXPR_ARG (placement, i);
6399 if (!mark_used (fn, complain) && !(complain & tf_error))
6400 return error_mark_node;
6401 return build_cxx_call (fn, nargs, argarray, complain);
6402 }
6403 else
6404 {
6405 tree ret;
6406 vec<tree, va_gc> *args = make_tree_vector ();
6407 args->quick_push (addr);
6408 if (second_parm_is_size_t (fn))
6409 args->quick_push (size);
6410 if (aligned_deallocation_fn_p (fn))
6411 {
6412 tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
6413 args->quick_push (al);
6414 }
6415 ret = cp_build_function_call_vec (fn, &args, complain);
6416 release_tree_vector (args);
6417 return ret;
6418 }
6419 }
6420
6421 /* [expr.new]
6422
6423 If no unambiguous matching deallocation function can be found,
6424 propagating the exception does not cause the object's memory to
6425 be freed. */
6426 if (alloc_fn)
6427 {
6428 if ((complain & tf_warning)
6429 && !placement)
6430 warning (0, "no corresponding deallocation function for %qD",
6431 alloc_fn);
6432 return NULL_TREE;
6433 }
6434
6435 if (complain & tf_error)
6436 error ("no suitable %<operator %s%> for %qT",
6437 operator_name_info[(int)code].name, type);
6438 return error_mark_node;
6439 }
6440
6441 /* If the current scope isn't allowed to access DECL along
6442 BASETYPE_PATH, give an error. The most derived class in
6443 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
6444 the declaration to use in the error diagnostic. */
6445
6446 bool
6447 enforce_access (tree basetype_path, tree decl, tree diag_decl,
6448 tsubst_flags_t complain, access_failure_info *afi)
6449 {
6450 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
6451
6452 if (flag_new_inheriting_ctors
6453 && DECL_INHERITED_CTOR (decl))
6454 {
6455 /* 7.3.3/18: The additional constructors are accessible if they would be
6456 accessible when used to construct an object of the corresponding base
6457 class. */
6458 decl = strip_inheriting_ctors (decl);
6459 basetype_path = lookup_base (basetype_path, DECL_CONTEXT (decl),
6460 ba_any, NULL, complain);
6461 }
6462
6463 if (!accessible_p (basetype_path, decl, true))
6464 {
6465 if (complain & tf_error)
6466 {
6467 if (flag_new_inheriting_ctors)
6468 diag_decl = strip_inheriting_ctors (diag_decl);
6469 if (TREE_PRIVATE (decl))
6470 {
6471 error ("%q#D is private within this context", diag_decl);
6472 inform (DECL_SOURCE_LOCATION (diag_decl),
6473 "declared private here");
6474 if (afi)
6475 afi->record_access_failure (basetype_path, diag_decl);
6476 }
6477 else if (TREE_PROTECTED (decl))
6478 {
6479 error ("%q#D is protected within this context", diag_decl);
6480 inform (DECL_SOURCE_LOCATION (diag_decl),
6481 "declared protected here");
6482 if (afi)
6483 afi->record_access_failure (basetype_path, diag_decl);
6484 }
6485 else
6486 {
6487 error ("%q#D is inaccessible within this context", diag_decl);
6488 inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
6489 if (afi)
6490 afi->record_access_failure (basetype_path, diag_decl);
6491 }
6492 }
6493 return false;
6494 }
6495
6496 return true;
6497 }
6498
6499 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
6500 bitwise or of LOOKUP_* values. If any errors are warnings are
6501 generated, set *DIAGNOSTIC_FN to "error" or "warning",
6502 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
6503 to NULL. */
6504
6505 static tree
6506 build_temp (tree expr, tree type, int flags,
6507 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
6508 {
6509 int savew, savee;
6510 vec<tree, va_gc> *args;
6511
6512 *diagnostic_kind = DK_UNSPECIFIED;
6513
6514 /* If the source is a packed field, calling the copy constructor will require
6515 binding the field to the reference parameter to the copy constructor, and
6516 we'll end up with an infinite loop. If we can use a bitwise copy, then
6517 do that now. */
6518 if ((lvalue_kind (expr) & clk_packed)
6519 && CLASS_TYPE_P (TREE_TYPE (expr))
6520 && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
6521 return get_target_expr_sfinae (expr, complain);
6522
6523 savew = warningcount + werrorcount, savee = errorcount;
6524 args = make_tree_vector_single (expr);
6525 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6526 &args, type, flags, complain);
6527 release_tree_vector (args);
6528 if (warningcount + werrorcount > savew)
6529 *diagnostic_kind = DK_WARNING;
6530 else if (errorcount > savee)
6531 *diagnostic_kind = DK_ERROR;
6532 return expr;
6533 }
6534
6535 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
6536 EXPR is implicitly converted to type TOTYPE.
6537 FN and ARGNUM are used for diagnostics. */
6538
6539 static void
6540 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
6541 {
6542 /* Issue warnings about peculiar, but valid, uses of NULL. */
6543 if (expr == null_node && TREE_CODE (totype) != BOOLEAN_TYPE
6544 && ARITHMETIC_TYPE_P (totype))
6545 {
6546 source_location loc =
6547 expansion_point_location_if_in_system_header (input_location);
6548
6549 if (fn)
6550 warning_at (loc, OPT_Wconversion_null,
6551 "passing NULL to non-pointer argument %P of %qD",
6552 argnum, fn);
6553 else
6554 warning_at (loc, OPT_Wconversion_null,
6555 "converting to non-pointer type %qT from NULL", totype);
6556 }
6557
6558 /* Issue warnings if "false" is converted to a NULL pointer */
6559 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
6560 && TYPE_PTR_P (totype))
6561 {
6562 if (fn)
6563 warning_at (input_location, OPT_Wconversion_null,
6564 "converting %<false%> to pointer type for argument %P "
6565 "of %qD", argnum, fn);
6566 else
6567 warning_at (input_location, OPT_Wconversion_null,
6568 "converting %<false%> to pointer type %qT", totype);
6569 }
6570 }
6571
6572 /* We gave a diagnostic during a conversion. If this was in the second
6573 standard conversion sequence of a user-defined conversion sequence, say
6574 which user-defined conversion. */
6575
6576 static void
6577 maybe_print_user_conv_context (conversion *convs)
6578 {
6579 if (convs->user_conv_p)
6580 for (conversion *t = convs; t; t = next_conversion (t))
6581 if (t->kind == ck_user)
6582 {
6583 print_z_candidate (0, " after user-defined conversion:",
6584 t->cand);
6585 break;
6586 }
6587 }
6588
6589 /* Locate the parameter with the given index within FNDECL.
6590 ARGNUM is zero based, -1 indicates the `this' argument of a method.
6591 Return the location of the FNDECL itself if there are problems. */
6592
6593 static location_t
6594 get_fndecl_argument_location (tree fndecl, int argnum)
6595 {
6596 int i;
6597 tree param;
6598
6599 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6600 for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
6601 i < argnum && param;
6602 i++, param = TREE_CHAIN (param))
6603 ;
6604
6605 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6606 return the location of FNDECL. */
6607 if (param == NULL)
6608 return DECL_SOURCE_LOCATION (fndecl);
6609
6610 return DECL_SOURCE_LOCATION (param);
6611 }
6612
6613 /* Perform the conversions in CONVS on the expression EXPR. FN and
6614 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
6615 indicates the `this' argument of a method. INNER is nonzero when
6616 being called to continue a conversion chain. It is negative when a
6617 reference binding will be applied, positive otherwise. If
6618 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
6619 conversions will be emitted if appropriate. If C_CAST_P is true,
6620 this conversion is coming from a C-style cast; in that case,
6621 conversions to inaccessible bases are permitted. */
6622
6623 static tree
6624 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
6625 bool issue_conversion_warnings,
6626 bool c_cast_p, tsubst_flags_t complain)
6627 {
6628 tree totype = convs->type;
6629 diagnostic_t diag_kind;
6630 int flags;
6631 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6632
6633 if (convs->bad_p && !(complain & tf_error))
6634 return error_mark_node;
6635
6636 if (convs->bad_p
6637 && convs->kind != ck_user
6638 && convs->kind != ck_list
6639 && convs->kind != ck_ambig
6640 && (convs->kind != ck_ref_bind
6641 || (convs->user_conv_p && next_conversion (convs)->bad_p))
6642 && (convs->kind != ck_rvalue
6643 || SCALAR_TYPE_P (totype))
6644 && convs->kind != ck_base)
6645 {
6646 bool complained = false;
6647 conversion *t = convs;
6648
6649 /* Give a helpful error if this is bad because of excess braces. */
6650 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6651 && SCALAR_TYPE_P (totype)
6652 && CONSTRUCTOR_NELTS (expr) > 0
6653 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
6654 {
6655 complained = permerror (loc, "too many braces around initializer "
6656 "for %qT", totype);
6657 while (BRACE_ENCLOSED_INITIALIZER_P (expr)
6658 && CONSTRUCTOR_NELTS (expr) == 1)
6659 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6660 }
6661
6662 /* Give a helpful error if this is bad because a conversion to bool
6663 from std::nullptr_t requires direct-initialization. */
6664 if (NULLPTR_TYPE_P (TREE_TYPE (expr))
6665 && TREE_CODE (totype) == BOOLEAN_TYPE)
6666 complained = permerror (loc, "converting to %qH from %qI requires "
6667 "direct-initialization",
6668 totype, TREE_TYPE (expr));
6669
6670 for (; t ; t = next_conversion (t))
6671 {
6672 if (t->kind == ck_user && t->cand->reason)
6673 {
6674 complained = permerror (loc, "invalid user-defined conversion "
6675 "from %qH to %qI", TREE_TYPE (expr),
6676 totype);
6677 if (complained)
6678 print_z_candidate (loc, "candidate is:", t->cand);
6679 expr = convert_like_real (t, expr, fn, argnum,
6680 /*issue_conversion_warnings=*/false,
6681 /*c_cast_p=*/false,
6682 complain);
6683 if (convs->kind == ck_ref_bind)
6684 expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
6685 LOOKUP_NORMAL, NULL_TREE,
6686 complain);
6687 else
6688 expr = cp_convert (totype, expr, complain);
6689 if (complained && fn)
6690 inform (DECL_SOURCE_LOCATION (fn),
6691 " initializing argument %P of %qD", argnum, fn);
6692 return expr;
6693 }
6694 else if (t->kind == ck_user || !t->bad_p)
6695 {
6696 expr = convert_like_real (t, expr, fn, argnum,
6697 /*issue_conversion_warnings=*/false,
6698 /*c_cast_p=*/false,
6699 complain);
6700 break;
6701 }
6702 else if (t->kind == ck_ambig)
6703 return convert_like_real (t, expr, fn, argnum,
6704 /*issue_conversion_warnings=*/false,
6705 /*c_cast_p=*/false,
6706 complain);
6707 else if (t->kind == ck_identity)
6708 break;
6709 }
6710 if (!complained)
6711 complained = permerror (loc, "invalid conversion from %qH to %qI",
6712 TREE_TYPE (expr), totype);
6713 if (complained && fn)
6714 inform (get_fndecl_argument_location (fn, argnum),
6715 " initializing argument %P of %qD", argnum, fn);
6716
6717 return cp_convert (totype, expr, complain);
6718 }
6719
6720 if (issue_conversion_warnings && (complain & tf_warning))
6721 conversion_null_warnings (totype, expr, fn, argnum);
6722
6723 switch (convs->kind)
6724 {
6725 case ck_user:
6726 {
6727 struct z_candidate *cand = convs->cand;
6728
6729 if (cand == NULL)
6730 /* We chose the surrogate function from add_conv_candidate, now we
6731 actually need to build the conversion. */
6732 cand = build_user_type_conversion_1 (totype, expr,
6733 LOOKUP_NO_CONVERSION, complain);
6734
6735 tree convfn = cand->fn;
6736
6737 /* When converting from an init list we consider explicit
6738 constructors, but actually trying to call one is an error. */
6739 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
6740 && BRACE_ENCLOSED_INITIALIZER_P (expr)
6741 /* Unless this is for direct-list-initialization. */
6742 && !CONSTRUCTOR_IS_DIRECT_INIT (expr)
6743 /* And in C++98 a default constructor can't be explicit. */
6744 && cxx_dialect >= cxx11)
6745 {
6746 if (!(complain & tf_error))
6747 return error_mark_node;
6748 location_t loc = location_of (expr);
6749 if (CONSTRUCTOR_NELTS (expr) == 0
6750 && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
6751 {
6752 if (pedwarn (loc, 0, "converting to %qT from initializer list "
6753 "would use explicit constructor %qD",
6754 totype, convfn))
6755 inform (loc, "in C++11 and above a default constructor "
6756 "can be explicit");
6757 }
6758 else
6759 error ("converting to %qT from initializer list would use "
6760 "explicit constructor %qD", totype, convfn);
6761 }
6762
6763 /* If we're initializing from {}, it's value-initialization. */
6764 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6765 && CONSTRUCTOR_NELTS (expr) == 0
6766 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
6767 {
6768 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
6769 expr = build_value_init (totype, complain);
6770 expr = get_target_expr_sfinae (expr, complain);
6771 if (expr != error_mark_node)
6772 {
6773 TARGET_EXPR_LIST_INIT_P (expr) = true;
6774 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
6775 }
6776 return expr;
6777 }
6778
6779 expr = mark_rvalue_use (expr);
6780
6781 /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
6782 any more UDCs. */
6783 expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
6784 complain);
6785
6786 /* If this is a constructor or a function returning an aggr type,
6787 we need to build up a TARGET_EXPR. */
6788 if (DECL_CONSTRUCTOR_P (convfn))
6789 {
6790 expr = build_cplus_new (totype, expr, complain);
6791
6792 /* Remember that this was list-initialization. */
6793 if (convs->check_narrowing && expr != error_mark_node)
6794 TARGET_EXPR_LIST_INIT_P (expr) = true;
6795 }
6796
6797 return expr;
6798 }
6799 case ck_identity:
6800 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
6801 {
6802 int nelts = CONSTRUCTOR_NELTS (expr);
6803 if (nelts == 0)
6804 expr = build_value_init (totype, complain);
6805 else if (nelts == 1)
6806 expr = CONSTRUCTOR_ELT (expr, 0)->value;
6807 else
6808 gcc_unreachable ();
6809 }
6810 expr = mark_rvalue_use (expr);
6811
6812 if (type_unknown_p (expr))
6813 expr = instantiate_type (totype, expr, complain);
6814 return expr;
6815 case ck_ambig:
6816 /* We leave bad_p off ck_ambig because overload resolution considers
6817 it valid, it just fails when we try to perform it. So we need to
6818 check complain here, too. */
6819 if (complain & tf_error)
6820 {
6821 /* Call build_user_type_conversion again for the error. */
6822 build_user_type_conversion (totype, convs->u.expr, LOOKUP_IMPLICIT,
6823 complain);
6824 if (fn)
6825 inform (DECL_SOURCE_LOCATION (fn),
6826 " initializing argument %P of %qD", argnum, fn);
6827 }
6828 return error_mark_node;
6829
6830 case ck_list:
6831 {
6832 /* Conversion to std::initializer_list<T>. */
6833 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
6834 tree new_ctor = build_constructor (init_list_type_node, NULL);
6835 unsigned len = CONSTRUCTOR_NELTS (expr);
6836 tree array, val, field;
6837 vec<constructor_elt, va_gc> *vec = NULL;
6838 unsigned ix;
6839
6840 /* Convert all the elements. */
6841 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
6842 {
6843 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
6844 false, false, complain);
6845 if (sub == error_mark_node)
6846 return sub;
6847 if (!BRACE_ENCLOSED_INITIALIZER_P (val)
6848 && !check_narrowing (TREE_TYPE (sub), val, complain))
6849 return error_mark_node;
6850 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
6851 if (!TREE_CONSTANT (sub))
6852 TREE_CONSTANT (new_ctor) = false;
6853 }
6854 /* Build up the array. */
6855 elttype = cp_build_qualified_type
6856 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
6857 array = build_array_of_n_type (elttype, len);
6858 array = finish_compound_literal (array, new_ctor, complain);
6859 /* Take the address explicitly rather than via decay_conversion
6860 to avoid the error about taking the address of a temporary. */
6861 array = cp_build_addr_expr (array, complain);
6862 array = cp_convert (build_pointer_type (elttype), array, complain);
6863 if (array == error_mark_node)
6864 return error_mark_node;
6865
6866 /* Build up the initializer_list object. */
6867 totype = complete_type (totype);
6868 field = next_initializable_field (TYPE_FIELDS (totype));
6869 CONSTRUCTOR_APPEND_ELT (vec, field, array);
6870 field = next_initializable_field (DECL_CHAIN (field));
6871 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
6872 new_ctor = build_constructor (totype, vec);
6873 return get_target_expr_sfinae (new_ctor, complain);
6874 }
6875
6876 case ck_aggr:
6877 if (TREE_CODE (totype) == COMPLEX_TYPE)
6878 {
6879 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
6880 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
6881 real = perform_implicit_conversion (TREE_TYPE (totype),
6882 real, complain);
6883 imag = perform_implicit_conversion (TREE_TYPE (totype),
6884 imag, complain);
6885 expr = build2 (COMPLEX_EXPR, totype, real, imag);
6886 return expr;
6887 }
6888 expr = reshape_init (totype, expr, complain);
6889 expr = get_target_expr_sfinae (digest_init (totype, expr, complain),
6890 complain);
6891 if (expr != error_mark_node)
6892 TARGET_EXPR_LIST_INIT_P (expr) = true;
6893 return expr;
6894
6895 default:
6896 break;
6897 };
6898
6899 expr = convert_like_real (next_conversion (convs), expr, fn, argnum,
6900 convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
6901 c_cast_p,
6902 complain);
6903 if (expr == error_mark_node)
6904 return error_mark_node;
6905
6906 switch (convs->kind)
6907 {
6908 case ck_rvalue:
6909 expr = decay_conversion (expr, complain);
6910 if (expr == error_mark_node)
6911 {
6912 if (complain & tf_error)
6913 {
6914 maybe_print_user_conv_context (convs);
6915 if (fn)
6916 inform (DECL_SOURCE_LOCATION (fn),
6917 " initializing argument %P of %qD", argnum, fn);
6918 }
6919 return error_mark_node;
6920 }
6921
6922 if (! MAYBE_CLASS_TYPE_P (totype))
6923 return expr;
6924
6925 /* Don't introduce copies when passing arguments along to the inherited
6926 constructor. */
6927 if (current_function_decl
6928 && flag_new_inheriting_ctors
6929 && DECL_INHERITED_CTOR (current_function_decl))
6930 return expr;
6931
6932 /* Fall through. */
6933 case ck_base:
6934 if (convs->kind == ck_base && !convs->need_temporary_p)
6935 {
6936 /* We are going to bind a reference directly to a base-class
6937 subobject of EXPR. */
6938 /* Build an expression for `*((base*) &expr)'. */
6939 expr = convert_to_base (expr, totype,
6940 !c_cast_p, /*nonnull=*/true, complain);
6941 return expr;
6942 }
6943
6944 /* Copy-initialization where the cv-unqualified version of the source
6945 type is the same class as, or a derived class of, the class of the
6946 destination [is treated as direct-initialization]. [dcl.init] */
6947 flags = LOOKUP_NORMAL;
6948 if (convs->user_conv_p)
6949 /* This conversion is being done in the context of a user-defined
6950 conversion (i.e. the second step of copy-initialization), so
6951 don't allow any more. */
6952 flags |= LOOKUP_NO_CONVERSION;
6953 else
6954 flags |= LOOKUP_ONLYCONVERTING;
6955 if (convs->rvaluedness_matches_p)
6956 /* standard_conversion got LOOKUP_PREFER_RVALUE. */
6957 flags |= LOOKUP_PREFER_RVALUE;
6958 if (TREE_CODE (expr) == TARGET_EXPR
6959 && TARGET_EXPR_LIST_INIT_P (expr))
6960 /* Copy-list-initialization doesn't actually involve a copy. */
6961 return expr;
6962 expr = build_temp (expr, totype, flags, &diag_kind, complain);
6963 if (diag_kind && complain)
6964 {
6965 maybe_print_user_conv_context (convs);
6966 if (fn)
6967 inform (DECL_SOURCE_LOCATION (fn),
6968 " initializing argument %P of %qD", argnum, fn);
6969 }
6970
6971 return build_cplus_new (totype, expr, complain);
6972
6973 case ck_ref_bind:
6974 {
6975 tree ref_type = totype;
6976
6977 if (convs->bad_p && !next_conversion (convs)->bad_p)
6978 {
6979 tree extype = TREE_TYPE (expr);
6980 if (TYPE_REF_IS_RVALUE (ref_type)
6981 && lvalue_p (expr))
6982 error_at (loc, "cannot bind rvalue reference of type %qH to "
6983 "lvalue of type %qI", totype, extype);
6984 else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
6985 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
6986 error_at (loc, "cannot bind non-const lvalue reference of "
6987 "type %qH to an rvalue of type %qI", totype, extype);
6988 else if (!reference_compatible_p (TREE_TYPE (totype), extype))
6989 error_at (loc, "binding reference of type %qH to %qI "
6990 "discards qualifiers", totype, extype);
6991 else
6992 gcc_unreachable ();
6993 maybe_print_user_conv_context (convs);
6994 if (fn)
6995 inform (DECL_SOURCE_LOCATION (fn),
6996 " initializing argument %P of %qD", argnum, fn);
6997 return error_mark_node;
6998 }
6999
7000 /* If necessary, create a temporary.
7001
7002 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
7003 that need temporaries, even when their types are reference
7004 compatible with the type of reference being bound, so the
7005 upcoming call to cp_build_addr_expr doesn't fail. */
7006 if (convs->need_temporary_p
7007 || TREE_CODE (expr) == CONSTRUCTOR
7008 || TREE_CODE (expr) == VA_ARG_EXPR)
7009 {
7010 /* Otherwise, a temporary of type "cv1 T1" is created and
7011 initialized from the initializer expression using the rules
7012 for a non-reference copy-initialization (8.5). */
7013
7014 tree type = TREE_TYPE (ref_type);
7015 cp_lvalue_kind lvalue = lvalue_kind (expr);
7016
7017 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7018 (type, next_conversion (convs)->type));
7019 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
7020 && !TYPE_REF_IS_RVALUE (ref_type))
7021 {
7022 /* If the reference is volatile or non-const, we
7023 cannot create a temporary. */
7024 if (lvalue & clk_bitfield)
7025 error_at (loc, "cannot bind bitfield %qE to %qT",
7026 expr, ref_type);
7027 else if (lvalue & clk_packed)
7028 error_at (loc, "cannot bind packed field %qE to %qT",
7029 expr, ref_type);
7030 else
7031 error_at (loc, "cannot bind rvalue %qE to %qT",
7032 expr, ref_type);
7033 return error_mark_node;
7034 }
7035 /* If the source is a packed field, and we must use a copy
7036 constructor, then building the target expr will require
7037 binding the field to the reference parameter to the
7038 copy constructor, and we'll end up with an infinite
7039 loop. If we can use a bitwise copy, then we'll be
7040 OK. */
7041 if ((lvalue & clk_packed)
7042 && CLASS_TYPE_P (type)
7043 && type_has_nontrivial_copy_init (type))
7044 {
7045 error_at (loc, "cannot bind packed field %qE to %qT",
7046 expr, ref_type);
7047 return error_mark_node;
7048 }
7049 if (lvalue & clk_bitfield)
7050 {
7051 expr = convert_bitfield_to_declared_type (expr);
7052 expr = fold_convert (type, expr);
7053 }
7054 expr = build_target_expr_with_type (expr, type, complain);
7055 }
7056
7057 /* Take the address of the thing to which we will bind the
7058 reference. */
7059 expr = cp_build_addr_expr (expr, complain);
7060 if (expr == error_mark_node)
7061 return error_mark_node;
7062
7063 /* Convert it to a pointer to the type referred to by the
7064 reference. This will adjust the pointer if a derived to
7065 base conversion is being performed. */
7066 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
7067 expr, complain);
7068 /* Convert the pointer to the desired reference type. */
7069 return build_nop (ref_type, expr);
7070 }
7071
7072 case ck_lvalue:
7073 return decay_conversion (expr, complain);
7074
7075 case ck_fnptr:
7076 /* ??? Should the address of a transaction-safe pointer point to the TM
7077 clone, and this conversion look up the primary function? */
7078 return build_nop (totype, expr);
7079
7080 case ck_qual:
7081 /* Warn about deprecated conversion if appropriate. */
7082 string_conv_p (totype, expr, 1);
7083 break;
7084
7085 case ck_ptr:
7086 if (convs->base_p)
7087 expr = convert_to_base (expr, totype, !c_cast_p,
7088 /*nonnull=*/false, complain);
7089 return build_nop (totype, expr);
7090
7091 case ck_pmem:
7092 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
7093 c_cast_p, complain);
7094
7095 default:
7096 break;
7097 }
7098
7099 if (convs->check_narrowing
7100 && !check_narrowing (totype, expr, complain))
7101 return error_mark_node;
7102
7103 if (issue_conversion_warnings)
7104 expr = cp_convert_and_check (totype, expr, complain);
7105 else
7106 expr = cp_convert (totype, expr, complain);
7107
7108 return expr;
7109 }
7110
7111 /* ARG is being passed to a varargs function. Perform any conversions
7112 required. Return the converted value. */
7113
7114 tree
7115 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
7116 {
7117 tree arg_type;
7118 location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
7119
7120 /* [expr.call]
7121
7122 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7123 standard conversions are performed. */
7124 arg = decay_conversion (arg, complain);
7125 arg_type = TREE_TYPE (arg);
7126 /* [expr.call]
7127
7128 If the argument has integral or enumeration type that is subject
7129 to the integral promotions (_conv.prom_), or a floating point
7130 type that is subject to the floating point promotion
7131 (_conv.fpprom_), the value of the argument is converted to the
7132 promoted type before the call. */
7133 if (TREE_CODE (arg_type) == REAL_TYPE
7134 && (TYPE_PRECISION (arg_type)
7135 < TYPE_PRECISION (double_type_node))
7136 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
7137 {
7138 if ((complain & tf_warning)
7139 && warn_double_promotion && !c_inhibit_evaluation_warnings)
7140 warning_at (loc, OPT_Wdouble_promotion,
7141 "implicit conversion from %qH to %qI when passing "
7142 "argument to function",
7143 arg_type, double_type_node);
7144 arg = convert_to_real_nofold (double_type_node, arg);
7145 }
7146 else if (NULLPTR_TYPE_P (arg_type))
7147 arg = null_pointer_node;
7148 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
7149 {
7150 if (SCOPED_ENUM_P (arg_type))
7151 {
7152 tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
7153 complain);
7154 prom = cp_perform_integral_promotions (prom, complain);
7155 if (abi_version_crosses (6)
7156 && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
7157 && (complain & tf_warning))
7158 warning_at (loc, OPT_Wabi, "scoped enum %qT passed through ... as "
7159 "%qT before -fabi-version=6, %qT after", arg_type,
7160 TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
7161 if (!abi_version_at_least (6))
7162 arg = prom;
7163 }
7164 else
7165 arg = cp_perform_integral_promotions (arg, complain);
7166 }
7167
7168 arg = require_complete_type_sfinae (arg, complain);
7169 arg_type = TREE_TYPE (arg);
7170
7171 if (arg != error_mark_node
7172 /* In a template (or ill-formed code), we can have an incomplete type
7173 even after require_complete_type_sfinae, in which case we don't know
7174 whether it has trivial copy or not. */
7175 && COMPLETE_TYPE_P (arg_type)
7176 && !cp_unevaluated_operand)
7177 {
7178 /* [expr.call] 5.2.2/7:
7179 Passing a potentially-evaluated argument of class type (Clause 9)
7180 with a non-trivial copy constructor or a non-trivial destructor
7181 with no corresponding parameter is conditionally-supported, with
7182 implementation-defined semantics.
7183
7184 We support it as pass-by-invisible-reference, just like a normal
7185 value parameter.
7186
7187 If the call appears in the context of a sizeof expression,
7188 it is not potentially-evaluated. */
7189 if (type_has_nontrivial_copy_init (arg_type)
7190 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
7191 {
7192 arg = force_rvalue (arg, complain);
7193 if (complain & tf_warning)
7194 warning (OPT_Wconditionally_supported,
7195 "passing objects of non-trivially-copyable "
7196 "type %q#T through %<...%> is conditionally supported",
7197 arg_type);
7198 return cp_build_addr_expr (arg, complain);
7199 }
7200 /* Build up a real lvalue-to-rvalue conversion in case the
7201 copy constructor is trivial but not callable. */
7202 else if (CLASS_TYPE_P (arg_type))
7203 force_rvalue (arg, complain);
7204
7205 }
7206
7207 return arg;
7208 }
7209
7210 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
7211
7212 tree
7213 build_x_va_arg (source_location loc, tree expr, tree type)
7214 {
7215 if (processing_template_decl)
7216 {
7217 tree r = build_min (VA_ARG_EXPR, type, expr);
7218 SET_EXPR_LOCATION (r, loc);
7219 return r;
7220 }
7221
7222 type = complete_type_or_else (type, NULL_TREE);
7223
7224 if (expr == error_mark_node || !type)
7225 return error_mark_node;
7226
7227 expr = mark_lvalue_use (expr);
7228
7229 if (TREE_CODE (type) == REFERENCE_TYPE)
7230 {
7231 error ("cannot receive reference type %qT through %<...%>", type);
7232 return error_mark_node;
7233 }
7234
7235 if (type_has_nontrivial_copy_init (type)
7236 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7237 {
7238 /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
7239 it as pass by invisible reference. */
7240 warning_at (loc, OPT_Wconditionally_supported,
7241 "receiving objects of non-trivially-copyable type %q#T "
7242 "through %<...%> is conditionally-supported", type);
7243
7244 tree ref = cp_build_reference_type (type, false);
7245 expr = build_va_arg (loc, expr, ref);
7246 return convert_from_reference (expr);
7247 }
7248
7249 tree ret = build_va_arg (loc, expr, type);
7250 if (CLASS_TYPE_P (type))
7251 /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
7252 know how to handle it. */
7253 ret = get_target_expr (ret);
7254 return ret;
7255 }
7256
7257 /* TYPE has been given to va_arg. Apply the default conversions which
7258 would have happened when passed via ellipsis. Return the promoted
7259 type, or the passed type if there is no change. */
7260
7261 tree
7262 cxx_type_promotes_to (tree type)
7263 {
7264 tree promote;
7265
7266 /* Perform the array-to-pointer and function-to-pointer
7267 conversions. */
7268 type = type_decays_to (type);
7269
7270 promote = type_promotes_to (type);
7271 if (same_type_p (type, promote))
7272 promote = type;
7273
7274 return promote;
7275 }
7276
7277 /* ARG is a default argument expression being passed to a parameter of
7278 the indicated TYPE, which is a parameter to FN. PARMNUM is the
7279 zero-based argument number. Do any required conversions. Return
7280 the converted value. */
7281
7282 static GTY(()) vec<tree, va_gc> *default_arg_context;
7283 void
7284 push_defarg_context (tree fn)
7285 { vec_safe_push (default_arg_context, fn); }
7286
7287 void
7288 pop_defarg_context (void)
7289 { default_arg_context->pop (); }
7290
7291 tree
7292 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
7293 tsubst_flags_t complain)
7294 {
7295 int i;
7296 tree t;
7297
7298 /* See through clones. */
7299 fn = DECL_ORIGIN (fn);
7300 /* And inheriting ctors. */
7301 if (flag_new_inheriting_ctors)
7302 fn = strip_inheriting_ctors (fn);
7303
7304 /* Detect recursion. */
7305 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
7306 if (t == fn)
7307 {
7308 if (complain & tf_error)
7309 error ("recursive evaluation of default argument for %q#D", fn);
7310 return error_mark_node;
7311 }
7312
7313 /* If the ARG is an unparsed default argument expression, the
7314 conversion cannot be performed. */
7315 if (TREE_CODE (arg) == DEFAULT_ARG)
7316 {
7317 if (complain & tf_error)
7318 error ("call to %qD uses the default argument for parameter %P, which "
7319 "is not yet defined", fn, parmnum);
7320 return error_mark_node;
7321 }
7322
7323 push_defarg_context (fn);
7324
7325 if (fn && DECL_TEMPLATE_INFO (fn))
7326 arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
7327
7328 /* Due to:
7329
7330 [dcl.fct.default]
7331
7332 The names in the expression are bound, and the semantic
7333 constraints are checked, at the point where the default
7334 expressions appears.
7335
7336 we must not perform access checks here. */
7337 push_deferring_access_checks (dk_no_check);
7338 /* We must make a copy of ARG, in case subsequent processing
7339 alters any part of it. */
7340 arg = break_out_target_exprs (arg);
7341 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
7342 ICR_DEFAULT_ARGUMENT, fn, parmnum,
7343 complain);
7344 arg = convert_for_arg_passing (type, arg, complain);
7345 pop_deferring_access_checks();
7346
7347 pop_defarg_context ();
7348
7349 return arg;
7350 }
7351
7352 /* Returns the type which will really be used for passing an argument of
7353 type TYPE. */
7354
7355 tree
7356 type_passed_as (tree type)
7357 {
7358 /* Pass classes with copy ctors by invisible reference. */
7359 if (TREE_ADDRESSABLE (type))
7360 {
7361 type = build_reference_type (type);
7362 /* There are no other pointers to this temporary. */
7363 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
7364 }
7365 else if (targetm.calls.promote_prototypes (type)
7366 && INTEGRAL_TYPE_P (type)
7367 && COMPLETE_TYPE_P (type)
7368 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
7369 type = integer_type_node;
7370
7371 return type;
7372 }
7373
7374 /* Actually perform the appropriate conversion. */
7375
7376 tree
7377 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
7378 {
7379 tree bitfield_type;
7380
7381 /* If VAL is a bitfield, then -- since it has already been converted
7382 to TYPE -- it cannot have a precision greater than TYPE.
7383
7384 If it has a smaller precision, we must widen it here. For
7385 example, passing "int f:3;" to a function expecting an "int" will
7386 not result in any conversion before this point.
7387
7388 If the precision is the same we must not risk widening. For
7389 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
7390 often have type "int", even though the C++ type for the field is
7391 "long long". If the value is being passed to a function
7392 expecting an "int", then no conversions will be required. But,
7393 if we call convert_bitfield_to_declared_type, the bitfield will
7394 be converted to "long long". */
7395 bitfield_type = is_bitfield_expr_with_lowered_type (val);
7396 if (bitfield_type
7397 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
7398 val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
7399
7400 if (val == error_mark_node)
7401 ;
7402 /* Pass classes with copy ctors by invisible reference. */
7403 else if (TREE_ADDRESSABLE (type))
7404 val = build1 (ADDR_EXPR, build_reference_type (type), val);
7405 else if (targetm.calls.promote_prototypes (type)
7406 && INTEGRAL_TYPE_P (type)
7407 && COMPLETE_TYPE_P (type)
7408 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
7409 val = cp_perform_integral_promotions (val, complain);
7410 if (complain & tf_warning)
7411 {
7412 if (warn_suggest_attribute_format)
7413 {
7414 tree rhstype = TREE_TYPE (val);
7415 const enum tree_code coder = TREE_CODE (rhstype);
7416 const enum tree_code codel = TREE_CODE (type);
7417 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7418 && coder == codel
7419 && check_missing_format_attribute (type, rhstype))
7420 warning (OPT_Wsuggest_attribute_format,
7421 "argument of function call might be a candidate "
7422 "for a format attribute");
7423 }
7424 maybe_warn_parm_abi (type, EXPR_LOC_OR_LOC (val, input_location));
7425 }
7426 return val;
7427 }
7428
7429 /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
7430 which just decay_conversion or no conversions at all should be done.
7431 This is true for some builtins which don't act like normal functions.
7432 Return 2 if no conversions at all should be done, 1 if just
7433 decay_conversion. Return 3 for special treatment of the 3rd argument
7434 for __builtin_*_overflow_p. */
7435
7436 int
7437 magic_varargs_p (tree fn)
7438 {
7439 if (flag_cilkplus && is_cilkplus_reduce_builtin (fn) != BUILT_IN_NONE)
7440 return 2;
7441
7442 if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
7443 switch (DECL_FUNCTION_CODE (fn))
7444 {
7445 case BUILT_IN_CLASSIFY_TYPE:
7446 case BUILT_IN_CONSTANT_P:
7447 case BUILT_IN_NEXT_ARG:
7448 case BUILT_IN_VA_START:
7449 return 1;
7450
7451 case BUILT_IN_ADD_OVERFLOW_P:
7452 case BUILT_IN_SUB_OVERFLOW_P:
7453 case BUILT_IN_MUL_OVERFLOW_P:
7454 return 3;
7455
7456 default:;
7457 return lookup_attribute ("type generic",
7458 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
7459 }
7460
7461 return 0;
7462 }
7463
7464 /* Returns the decl of the dispatcher function if FN is a function version. */
7465
7466 tree
7467 get_function_version_dispatcher (tree fn)
7468 {
7469 tree dispatcher_decl = NULL;
7470
7471 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7472 && DECL_FUNCTION_VERSIONED (fn));
7473
7474 gcc_assert (targetm.get_function_versions_dispatcher);
7475 dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
7476
7477 if (dispatcher_decl == NULL)
7478 {
7479 error_at (input_location, "use of multiversioned function "
7480 "without a default");
7481 return NULL;
7482 }
7483
7484 retrofit_lang_decl (dispatcher_decl);
7485 gcc_assert (dispatcher_decl != NULL);
7486 return dispatcher_decl;
7487 }
7488
7489 /* fn is a function version dispatcher that is marked used. Mark all the
7490 semantically identical function versions it will dispatch as used. */
7491
7492 void
7493 mark_versions_used (tree fn)
7494 {
7495 struct cgraph_node *node;
7496 struct cgraph_function_version_info *node_v;
7497 struct cgraph_function_version_info *it_v;
7498
7499 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7500
7501 node = cgraph_node::get (fn);
7502 if (node == NULL)
7503 return;
7504
7505 gcc_assert (node->dispatcher_function);
7506
7507 node_v = node->function_version ();
7508 if (node_v == NULL)
7509 return;
7510
7511 /* All semantically identical versions are chained. Traverse and mark each
7512 one of them as used. */
7513 it_v = node_v->next;
7514 while (it_v != NULL)
7515 {
7516 mark_used (it_v->this_node->decl);
7517 it_v = it_v->next;
7518 }
7519 }
7520
7521 /* Build a call to "the copy constructor" for the type of A, even if it
7522 wouldn't be selected by normal overload resolution. Used for
7523 diagnostics. */
7524
7525 static tree
7526 call_copy_ctor (tree a, tsubst_flags_t complain)
7527 {
7528 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
7529 tree binfo = TYPE_BINFO (ctype);
7530 tree copy = get_copy_ctor (ctype, complain);
7531 copy = build_baselink (binfo, binfo, copy, NULL_TREE);
7532 tree ob = build_dummy_object (ctype);
7533 vec<tree, va_gc>* args = make_tree_vector_single (a);
7534 tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
7535 LOOKUP_NORMAL, NULL, complain);
7536 release_tree_vector (args);
7537 return r;
7538 }
7539
7540 /* Return true iff T refers to a base field. */
7541
7542 static bool
7543 is_base_field_ref (tree t)
7544 {
7545 STRIP_NOPS (t);
7546 if (TREE_CODE (t) == ADDR_EXPR)
7547 t = TREE_OPERAND (t, 0);
7548 if (TREE_CODE (t) == COMPONENT_REF)
7549 t = TREE_OPERAND (t, 1);
7550 if (TREE_CODE (t) == FIELD_DECL)
7551 return DECL_FIELD_IS_BASE (t);
7552 return false;
7553 }
7554
7555 /* We can't elide a copy from a function returning by value to a base
7556 subobject, as the callee might clobber tail padding. Return true iff this
7557 could be that case. */
7558
7559 static bool
7560 unsafe_copy_elision_p (tree target, tree exp)
7561 {
7562 /* Copy elision only happens with a TARGET_EXPR. */
7563 if (TREE_CODE (exp) != TARGET_EXPR)
7564 return false;
7565 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7566 /* It's safe to elide the copy for a class with no tail padding. */
7567 if (tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
7568 return false;
7569 /* It's safe to elide the copy if we aren't initializing a base object. */
7570 if (!is_base_field_ref (target))
7571 return false;
7572 tree init = TARGET_EXPR_INITIAL (exp);
7573 /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
7574 while (TREE_CODE (init) == COMPOUND_EXPR)
7575 init = TREE_OPERAND (init, 1);
7576 return (TREE_CODE (init) == AGGR_INIT_EXPR
7577 && !AGGR_INIT_VIA_CTOR_P (init));
7578 }
7579
7580 /* Subroutine of the various build_*_call functions. Overload resolution
7581 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
7582 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
7583 bitmask of various LOOKUP_* flags which apply to the call itself. */
7584
7585 static tree
7586 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
7587 {
7588 tree fn = cand->fn;
7589 const vec<tree, va_gc> *args = cand->args;
7590 tree first_arg = cand->first_arg;
7591 conversion **convs = cand->convs;
7592 conversion *conv;
7593 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
7594 int parmlen;
7595 tree val;
7596 int i = 0;
7597 int j = 0;
7598 unsigned int arg_index = 0;
7599 int is_method = 0;
7600 int nargs;
7601 tree *argarray;
7602 bool already_used = false;
7603
7604 /* In a template, there is no need to perform all of the work that
7605 is normally done. We are only interested in the type of the call
7606 expression, i.e., the return type of the function. Any semantic
7607 errors will be deferred until the template is instantiated. */
7608 if (processing_template_decl)
7609 {
7610 tree expr, addr;
7611 tree return_type;
7612 const tree *argarray;
7613 unsigned int nargs;
7614
7615 if (undeduced_auto_decl (fn))
7616 mark_used (fn, complain);
7617
7618 return_type = TREE_TYPE (TREE_TYPE (fn));
7619 nargs = vec_safe_length (args);
7620 if (first_arg == NULL_TREE)
7621 argarray = args->address ();
7622 else
7623 {
7624 tree *alcarray;
7625 unsigned int ix;
7626 tree arg;
7627
7628 ++nargs;
7629 alcarray = XALLOCAVEC (tree, nargs);
7630 alcarray[0] = build_this (first_arg);
7631 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
7632 alcarray[ix + 1] = arg;
7633 argarray = alcarray;
7634 }
7635
7636 addr = build_addr_func (fn, complain);
7637 if (addr == error_mark_node)
7638 return error_mark_node;
7639 expr = build_call_array_loc (input_location, return_type,
7640 addr, nargs, argarray);
7641 if (TREE_THIS_VOLATILE (fn) && cfun)
7642 current_function_returns_abnormally = 1;
7643 return convert_from_reference (expr);
7644 }
7645
7646 /* Give any warnings we noticed during overload resolution. */
7647 if (cand->warnings && (complain & tf_warning))
7648 {
7649 struct candidate_warning *w;
7650 for (w = cand->warnings; w; w = w->next)
7651 joust (cand, w->loser, 1, complain);
7652 }
7653
7654 /* OK, we're actually calling this inherited constructor; set its deletedness
7655 appropriately. We can get away with doing this here because calling is
7656 the only way to refer to a constructor. */
7657 if (DECL_INHERITED_CTOR (fn))
7658 deduce_inheriting_ctor (fn);
7659
7660 /* Make =delete work with SFINAE. */
7661 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7662 return error_mark_node;
7663
7664 if (DECL_FUNCTION_MEMBER_P (fn))
7665 {
7666 tree access_fn;
7667 /* If FN is a template function, two cases must be considered.
7668 For example:
7669
7670 struct A {
7671 protected:
7672 template <class T> void f();
7673 };
7674 template <class T> struct B {
7675 protected:
7676 void g();
7677 };
7678 struct C : A, B<int> {
7679 using A::f; // #1
7680 using B<int>::g; // #2
7681 };
7682
7683 In case #1 where `A::f' is a member template, DECL_ACCESS is
7684 recorded in the primary template but not in its specialization.
7685 We check access of FN using its primary template.
7686
7687 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
7688 because it is a member of class template B, DECL_ACCESS is
7689 recorded in the specialization `B<int>::g'. We cannot use its
7690 primary template because `B<T>::g' and `B<int>::g' may have
7691 different access. */
7692 if (DECL_TEMPLATE_INFO (fn)
7693 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
7694 access_fn = DECL_TI_TEMPLATE (fn);
7695 else
7696 access_fn = fn;
7697 if (!perform_or_defer_access_check (cand->access_path, access_fn,
7698 fn, complain))
7699 return error_mark_node;
7700 }
7701
7702 /* If we're checking for implicit delete, don't bother with argument
7703 conversions. */
7704 if (flags & LOOKUP_SPECULATIVE)
7705 {
7706 if (DECL_DELETED_FN (fn))
7707 {
7708 if (complain & tf_error)
7709 mark_used (fn);
7710 return error_mark_node;
7711 }
7712 if (cand->viable == 1)
7713 return fn;
7714 else if (!(complain & tf_error))
7715 /* Reject bad conversions now. */
7716 return error_mark_node;
7717 /* else continue to get conversion error. */
7718 }
7719
7720 /* N3276 magic doesn't apply to nested calls. */
7721 tsubst_flags_t decltype_flag = (complain & tf_decltype);
7722 complain &= ~tf_decltype;
7723 /* No-Cleanup doesn't apply to nested calls either. */
7724 tsubst_flags_t no_cleanup_complain = complain;
7725 complain &= ~tf_no_cleanup;
7726
7727 /* Find maximum size of vector to hold converted arguments. */
7728 parmlen = list_length (parm);
7729 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
7730 if (parmlen > nargs)
7731 nargs = parmlen;
7732 argarray = XALLOCAVEC (tree, nargs);
7733
7734 /* The implicit parameters to a constructor are not considered by overload
7735 resolution, and must be of the proper type. */
7736 if (DECL_CONSTRUCTOR_P (fn))
7737 {
7738 tree object_arg;
7739 if (first_arg != NULL_TREE)
7740 {
7741 object_arg = first_arg;
7742 first_arg = NULL_TREE;
7743 }
7744 else
7745 {
7746 object_arg = (*args)[arg_index];
7747 ++arg_index;
7748 }
7749 argarray[j++] = build_this (object_arg);
7750 parm = TREE_CHAIN (parm);
7751 /* We should never try to call the abstract constructor. */
7752 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
7753
7754 if (DECL_HAS_VTT_PARM_P (fn))
7755 {
7756 argarray[j++] = (*args)[arg_index];
7757 ++arg_index;
7758 parm = TREE_CHAIN (parm);
7759 }
7760
7761 if (flags & LOOKUP_PREFER_RVALUE)
7762 {
7763 /* The implicit move specified in 15.8.3/3 fails "...if the type of
7764 the first parameter of the selected constructor is not an rvalue
7765 reference to the object’s type (possibly cv-qualified)...." */
7766 gcc_assert (!(complain & tf_error));
7767 tree ptype = convs[0]->type;
7768 if (TREE_CODE (ptype) != REFERENCE_TYPE
7769 || !TYPE_REF_IS_RVALUE (ptype)
7770 || CONVERSION_RANK (convs[0]) > cr_exact)
7771 return error_mark_node;
7772 }
7773 }
7774 /* Bypass access control for 'this' parameter. */
7775 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7776 {
7777 tree parmtype = TREE_VALUE (parm);
7778 tree arg = build_this (first_arg != NULL_TREE
7779 ? first_arg
7780 : (*args)[arg_index]);
7781 tree argtype = TREE_TYPE (arg);
7782 tree converted_arg;
7783 tree base_binfo;
7784
7785 if (arg == error_mark_node)
7786 return error_mark_node;
7787
7788 if (convs[i]->bad_p)
7789 {
7790 if (complain & tf_error)
7791 {
7792 if (permerror (input_location, "passing %qT as %<this%> "
7793 "argument discards qualifiers",
7794 TREE_TYPE (argtype)))
7795 inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
7796 }
7797 else
7798 return error_mark_node;
7799 }
7800
7801 /* See if the function member or the whole class type is declared
7802 final and the call can be devirtualized. */
7803 if (DECL_FINAL_P (fn)
7804 || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
7805 flags |= LOOKUP_NONVIRTUAL;
7806
7807 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
7808 X is called for an object that is not of type X, or of a type
7809 derived from X, the behavior is undefined.
7810
7811 So we can assume that anything passed as 'this' is non-null, and
7812 optimize accordingly. */
7813 gcc_assert (TYPE_PTR_P (parmtype));
7814 /* Convert to the base in which the function was declared. */
7815 gcc_assert (cand->conversion_path != NULL_TREE);
7816 converted_arg = build_base_path (PLUS_EXPR,
7817 arg,
7818 cand->conversion_path,
7819 1, complain);
7820 /* Check that the base class is accessible. */
7821 if (!accessible_base_p (TREE_TYPE (argtype),
7822 BINFO_TYPE (cand->conversion_path), true))
7823 {
7824 if (complain & tf_error)
7825 error ("%qT is not an accessible base of %qT",
7826 BINFO_TYPE (cand->conversion_path),
7827 TREE_TYPE (argtype));
7828 else
7829 return error_mark_node;
7830 }
7831 /* If fn was found by a using declaration, the conversion path
7832 will be to the derived class, not the base declaring fn. We
7833 must convert from derived to base. */
7834 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
7835 TREE_TYPE (parmtype), ba_unique,
7836 NULL, complain);
7837 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
7838 base_binfo, 1, complain);
7839
7840 argarray[j++] = converted_arg;
7841 parm = TREE_CHAIN (parm);
7842 if (first_arg != NULL_TREE)
7843 first_arg = NULL_TREE;
7844 else
7845 ++arg_index;
7846 ++i;
7847 is_method = 1;
7848 }
7849
7850 gcc_assert (first_arg == NULL_TREE);
7851 for (; arg_index < vec_safe_length (args) && parm;
7852 parm = TREE_CHAIN (parm), ++arg_index, ++i)
7853 {
7854 tree type = TREE_VALUE (parm);
7855 tree arg = (*args)[arg_index];
7856 bool conversion_warning = true;
7857
7858 conv = convs[i];
7859
7860 /* If the argument is NULL and used to (implicitly) instantiate a
7861 template function (and bind one of the template arguments to
7862 the type of 'long int'), we don't want to warn about passing NULL
7863 to non-pointer argument.
7864 For example, if we have this template function:
7865
7866 template<typename T> void func(T x) {}
7867
7868 we want to warn (when -Wconversion is enabled) in this case:
7869
7870 void foo() {
7871 func<int>(NULL);
7872 }
7873
7874 but not in this case:
7875
7876 void foo() {
7877 func(NULL);
7878 }
7879 */
7880 if (arg == null_node
7881 && DECL_TEMPLATE_INFO (fn)
7882 && cand->template_decl
7883 && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
7884 conversion_warning = false;
7885
7886 /* Warn about initializer_list deduction that isn't currently in the
7887 working draft. */
7888 if (cxx_dialect > cxx98
7889 && flag_deduce_init_list
7890 && cand->template_decl
7891 && is_std_init_list (non_reference (type))
7892 && BRACE_ENCLOSED_INITIALIZER_P (arg))
7893 {
7894 tree tmpl = TI_TEMPLATE (cand->template_decl);
7895 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
7896 tree patparm = get_pattern_parm (realparm, tmpl);
7897 tree pattype = TREE_TYPE (patparm);
7898 if (PACK_EXPANSION_P (pattype))
7899 pattype = PACK_EXPANSION_PATTERN (pattype);
7900 pattype = non_reference (pattype);
7901
7902 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
7903 && (cand->explicit_targs == NULL_TREE
7904 || (TREE_VEC_LENGTH (cand->explicit_targs)
7905 <= TEMPLATE_TYPE_IDX (pattype))))
7906 {
7907 pedwarn (input_location, 0, "deducing %qT as %qT",
7908 non_reference (TREE_TYPE (patparm)),
7909 non_reference (type));
7910 pedwarn (DECL_SOURCE_LOCATION (cand->fn), 0,
7911 " in call to %qD", cand->fn);
7912 pedwarn (input_location, 0,
7913 " (you can disable this with -fno-deduce-init-list)");
7914 }
7915 }
7916
7917 /* Set user_conv_p on the argument conversions, so rvalue/base handling
7918 knows not to allow any more UDCs. This needs to happen after we
7919 process cand->warnings. */
7920 if (flags & LOOKUP_NO_CONVERSION)
7921 conv->user_conv_p = true;
7922
7923 tsubst_flags_t arg_complain = complain;
7924 if (!conversion_warning)
7925 arg_complain &= ~tf_warning;
7926
7927 val = convert_like_with_context (conv, arg, fn, i - is_method,
7928 arg_complain);
7929 val = convert_for_arg_passing (type, val, arg_complain);
7930
7931 if (val == error_mark_node)
7932 return error_mark_node;
7933 else
7934 argarray[j++] = val;
7935 }
7936
7937 /* Default arguments */
7938 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
7939 {
7940 if (TREE_VALUE (parm) == error_mark_node)
7941 return error_mark_node;
7942 val = convert_default_arg (TREE_VALUE (parm),
7943 TREE_PURPOSE (parm),
7944 fn, i - is_method,
7945 complain);
7946 if (val == error_mark_node)
7947 return error_mark_node;
7948 argarray[j++] = val;
7949 }
7950
7951 /* Ellipsis */
7952 int magic = magic_varargs_p (fn);
7953 for (; arg_index < vec_safe_length (args); ++arg_index)
7954 {
7955 tree a = (*args)[arg_index];
7956 if ((magic == 3 && arg_index == 2) || magic == 2)
7957 {
7958 /* Do no conversions for certain magic varargs. */
7959 a = mark_type_use (a);
7960 if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
7961 return error_mark_node;
7962 }
7963 else if (magic != 0)
7964 /* For other magic varargs only do decay_conversion. */
7965 a = decay_conversion (a, complain);
7966 else if (DECL_CONSTRUCTOR_P (fn)
7967 && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
7968 TREE_TYPE (a)))
7969 {
7970 /* Avoid infinite recursion trying to call A(...). */
7971 if (complain & tf_error)
7972 /* Try to call the actual copy constructor for a good error. */
7973 call_copy_ctor (a, complain);
7974 return error_mark_node;
7975 }
7976 else
7977 a = convert_arg_to_ellipsis (a, complain);
7978 if (a == error_mark_node)
7979 return error_mark_node;
7980 argarray[j++] = a;
7981 }
7982
7983 gcc_assert (j <= nargs);
7984 nargs = j;
7985
7986 /* Avoid to do argument-transformation, if warnings for format, and for
7987 nonnull are disabled. Just in case that at least one of them is active
7988 the check_function_arguments function might warn about something. */
7989
7990 bool warned_p = false;
7991 if (warn_nonnull
7992 || warn_format
7993 || warn_suggest_attribute_format
7994 || warn_restrict)
7995 {
7996 tree *fargs = (!nargs ? argarray
7997 : (tree *) alloca (nargs * sizeof (tree)));
7998 for (j = 0; j < nargs; j++)
7999 fargs[j] = maybe_constant_value (argarray[j]);
8000
8001 warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
8002 nargs, fargs, NULL);
8003 }
8004
8005 if (DECL_INHERITED_CTOR (fn))
8006 {
8007 /* Check for passing ellipsis arguments to an inherited constructor. We
8008 could handle this by open-coding the inherited constructor rather than
8009 defining it, but let's not bother now. */
8010 if (!cp_unevaluated_operand
8011 && cand->num_convs
8012 && cand->convs[cand->num_convs-1]->ellipsis_p)
8013 {
8014 if (complain & tf_error)
8015 {
8016 sorry ("passing arguments to ellipsis of inherited constructor "
8017 "%qD", cand->fn);
8018 inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
8019 }
8020 return error_mark_node;
8021 }
8022
8023 /* A base constructor inheriting from a virtual base doesn't get the
8024 inherited arguments, just this and __vtt. */
8025 if (ctor_omit_inherited_parms (fn))
8026 nargs = 2;
8027 }
8028
8029 /* Avoid actually calling copy constructors and copy assignment operators,
8030 if possible. */
8031
8032 if (! flag_elide_constructors)
8033 /* Do things the hard way. */;
8034 else if (cand->num_convs == 1
8035 && (DECL_COPY_CONSTRUCTOR_P (fn)
8036 || DECL_MOVE_CONSTRUCTOR_P (fn))
8037 /* It's unsafe to elide the constructor when handling
8038 a noexcept-expression, it may evaluate to the wrong
8039 value (c++/53025). */
8040 && cp_noexcept_operand == 0)
8041 {
8042 tree targ;
8043 tree arg = argarray[num_artificial_parms_for (fn)];
8044 tree fa;
8045 bool trivial = trivial_fn_p (fn);
8046
8047 /* Pull out the real argument, disregarding const-correctness. */
8048 targ = arg;
8049 /* Strip the reference binding for the constructor parameter. */
8050 if (CONVERT_EXPR_P (targ)
8051 && TREE_CODE (TREE_TYPE (targ)) == REFERENCE_TYPE)
8052 targ = TREE_OPERAND (targ, 0);
8053 /* But don't strip any other reference bindings; binding a temporary to a
8054 reference prevents copy elision. */
8055 while ((CONVERT_EXPR_P (targ)
8056 && TREE_CODE (TREE_TYPE (targ)) != REFERENCE_TYPE)
8057 || TREE_CODE (targ) == NON_LVALUE_EXPR)
8058 targ = TREE_OPERAND (targ, 0);
8059 if (TREE_CODE (targ) == ADDR_EXPR)
8060 {
8061 targ = TREE_OPERAND (targ, 0);
8062 if (!same_type_ignoring_top_level_qualifiers_p
8063 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
8064 targ = NULL_TREE;
8065 }
8066 else
8067 targ = NULL_TREE;
8068
8069 if (targ)
8070 arg = targ;
8071 else
8072 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
8073
8074 /* In C++17 we shouldn't be copying a TARGET_EXPR except into a base
8075 subobject. */
8076 if (CHECKING_P && cxx_dialect >= cxx17)
8077 gcc_assert (TREE_CODE (arg) != TARGET_EXPR
8078 /* It's from binding the ref parm to a packed field. */
8079 || convs[0]->need_temporary_p
8080 || seen_error ()
8081 /* See unsafe_copy_elision_p. */
8082 || DECL_BASE_CONSTRUCTOR_P (fn));
8083
8084 /* [class.copy]: the copy constructor is implicitly defined even if
8085 the implementation elided its use. */
8086 if (!trivial || DECL_DELETED_FN (fn))
8087 {
8088 if (!mark_used (fn, complain) && !(complain & tf_error))
8089 return error_mark_node;
8090 already_used = true;
8091 }
8092
8093 /* If we're creating a temp and we already have one, don't create a
8094 new one. If we're not creating a temp but we get one, use
8095 INIT_EXPR to collapse the temp into our target. Otherwise, if the
8096 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
8097 temp or an INIT_EXPR otherwise. */
8098 fa = argarray[0];
8099 if (is_dummy_object (fa))
8100 {
8101 if (TREE_CODE (arg) == TARGET_EXPR)
8102 return arg;
8103 else if (trivial)
8104 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
8105 }
8106 else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
8107 && !unsafe_copy_elision_p (fa, arg))
8108 {
8109 tree to = cp_stabilize_reference (cp_build_indirect_ref (fa,
8110 RO_NULL,
8111 complain));
8112
8113 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
8114 return val;
8115 }
8116 }
8117 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
8118 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
8119 && trivial_fn_p (fn)
8120 && !DECL_DELETED_FN (fn))
8121 {
8122 tree to = cp_stabilize_reference
8123 (cp_build_indirect_ref (argarray[0], RO_NULL, complain));
8124 tree type = TREE_TYPE (to);
8125 tree as_base = CLASSTYPE_AS_BASE (type);
8126 tree arg = argarray[1];
8127
8128 if (is_really_empty_class (type))
8129 {
8130 /* Avoid copying empty classes. */
8131 val = build2 (COMPOUND_EXPR, type, arg, to);
8132 TREE_NO_WARNING (val) = 1;
8133 }
8134 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
8135 {
8136 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
8137 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
8138 /* Handle NSDMI that refer to the object being initialized. */
8139 replace_placeholders (arg, to);
8140 }
8141 else
8142 {
8143 /* We must only copy the non-tail padding parts. */
8144 tree arg0, arg2, t;
8145 tree array_type, alias_set;
8146
8147 arg2 = TYPE_SIZE_UNIT (as_base);
8148 arg0 = cp_build_addr_expr (to, complain);
8149
8150 array_type = build_array_type (unsigned_char_type_node,
8151 build_index_type
8152 (size_binop (MINUS_EXPR,
8153 arg2, size_int (1))));
8154 alias_set = build_int_cst (build_pointer_type (type), 0);
8155 t = build2 (MODIFY_EXPR, void_type_node,
8156 build2 (MEM_REF, array_type, arg0, alias_set),
8157 build2 (MEM_REF, array_type, arg, alias_set));
8158 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
8159 TREE_NO_WARNING (val) = 1;
8160 }
8161
8162 return val;
8163 }
8164 else if (!DECL_DELETED_FN (fn)
8165 && trivial_fn_p (fn))
8166 {
8167 if (DECL_DESTRUCTOR_P (fn))
8168 return fold_convert (void_type_node, argarray[0]);
8169 else if (default_ctor_p (fn))
8170 {
8171 if (is_dummy_object (argarray[0]))
8172 return force_target_expr (DECL_CONTEXT (fn), void_node,
8173 no_cleanup_complain);
8174 else
8175 return cp_build_indirect_ref (argarray[0], RO_NULL, complain);
8176 }
8177 }
8178
8179 /* For calls to a multi-versioned function, overload resolution
8180 returns the function with the highest target priority, that is,
8181 the version that will checked for dispatching first. If this
8182 version is inlinable, a direct call to this version can be made
8183 otherwise the call should go through the dispatcher. */
8184
8185 if (DECL_FUNCTION_VERSIONED (fn)
8186 && (current_function_decl == NULL
8187 || !targetm.target_option.can_inline_p (current_function_decl, fn)))
8188 {
8189 fn = get_function_version_dispatcher (fn);
8190 if (fn == NULL)
8191 return NULL;
8192 if (!already_used)
8193 mark_versions_used (fn);
8194 }
8195
8196 if (!already_used
8197 && !mark_used (fn, complain))
8198 return error_mark_node;
8199
8200 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
8201 /* Don't mess with virtual lookup in instantiate_non_dependent_expr;
8202 virtual functions can't be constexpr. */
8203 && !in_template_function ())
8204 {
8205 tree t;
8206 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
8207 DECL_CONTEXT (fn),
8208 ba_any, NULL, complain);
8209 gcc_assert (binfo && binfo != error_mark_node);
8210
8211 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
8212 complain);
8213 if (TREE_SIDE_EFFECTS (argarray[0]))
8214 argarray[0] = save_expr (argarray[0]);
8215 t = build_pointer_type (TREE_TYPE (fn));
8216 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
8217 TREE_TYPE (fn) = t;
8218 }
8219 else
8220 {
8221 fn = build_addr_func (fn, complain);
8222 if (fn == error_mark_node)
8223 return error_mark_node;
8224 }
8225
8226 tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
8227 if (call == error_mark_node)
8228 return call;
8229 if (cand->flags & LOOKUP_LIST_INIT_CTOR)
8230 {
8231 tree c = extract_call_expr (call);
8232 /* build_new_op_1 will clear this when appropriate. */
8233 CALL_EXPR_ORDERED_ARGS (c) = true;
8234 }
8235 if (warned_p)
8236 {
8237 tree c = extract_call_expr (call);
8238 if (TREE_CODE (c) == CALL_EXPR)
8239 TREE_NO_WARNING (c) = 1;
8240 }
8241 return call;
8242 }
8243
8244 /* Return the DECL of the first non-public data member of class TYPE
8245 or null if none can be found. */
8246
8247 static tree
8248 first_non_public_field (tree type)
8249 {
8250 if (!CLASS_TYPE_P (type))
8251 return NULL_TREE;
8252
8253 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8254 {
8255 if (TREE_CODE (field) != FIELD_DECL)
8256 continue;
8257 if (TREE_STATIC (field))
8258 continue;
8259 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
8260 return field;
8261 }
8262
8263 int i = 0;
8264
8265 for (tree base_binfo, binfo = TYPE_BINFO (type);
8266 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8267 {
8268 tree base = TREE_TYPE (base_binfo);
8269
8270 if (tree field = first_non_public_field (base))
8271 return field;
8272 }
8273
8274 return NULL_TREE;
8275 }
8276
8277 /* Return true if all copy and move assignment operator overloads for
8278 class TYPE are trivial and at least one of them is not deleted and,
8279 when ACCESS is set, accessible. Return false otherwise. Set
8280 HASASSIGN to true when the TYPE has a (not necessarily trivial)
8281 copy or move assignment. */
8282
8283 static bool
8284 has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
8285 {
8286 tree fns = get_class_binding (type, assign_op_identifier);
8287 bool all_trivial = true;
8288
8289 /* Iterate over overloads of the assignment operator, checking
8290 accessible copy assignments for triviality. */
8291
8292 for (ovl_iterator oi (fns); oi; ++oi)
8293 {
8294 tree f = *oi;
8295
8296 /* Skip operators that aren't copy assignments. */
8297 if (!copy_fn_p (f))
8298 continue;
8299
8300 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
8301 || accessible_p (TYPE_BINFO (type), f, true));
8302
8303 /* Skip template assignment operators and deleted functions. */
8304 if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
8305 continue;
8306
8307 if (accessible)
8308 *hasassign = true;
8309
8310 if (!accessible || !trivial_fn_p (f))
8311 all_trivial = false;
8312
8313 /* Break early when both properties have been determined. */
8314 if (*hasassign && !all_trivial)
8315 break;
8316 }
8317
8318 /* Return true if they're all trivial and one of the expressions
8319 TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
8320 tree ref = cp_build_reference_type (type, false);
8321 return (all_trivial
8322 && (is_trivially_xible (MODIFY_EXPR, type, type)
8323 || is_trivially_xible (MODIFY_EXPR, type, ref)));
8324 }
8325
8326 /* Return true if all copy and move ctor overloads for class TYPE are
8327 trivial and at least one of them is not deleted and, when ACCESS is
8328 set, accessible. Return false otherwise. Set each element of HASCTOR[]
8329 to true when the TYPE has a (not necessarily trivial) default and copy
8330 (or move) ctor, respectively. */
8331
8332 static bool
8333 has_trivial_copy_p (tree type, bool access, bool hasctor[2])
8334 {
8335 tree fns = get_class_binding (type, complete_ctor_identifier);
8336 bool all_trivial = true;
8337
8338 for (ovl_iterator oi (fns); oi; ++oi)
8339 {
8340 tree f = *oi;
8341
8342 /* Skip template constructors. */
8343 if (TREE_CODE (f) != FUNCTION_DECL)
8344 continue;
8345
8346 bool cpy_or_move_ctor_p = copy_fn_p (f);
8347
8348 /* Skip ctors other than default, copy, and move. */
8349 if (!cpy_or_move_ctor_p && !default_ctor_p (f))
8350 continue;
8351
8352 if (DECL_DELETED_FN (f))
8353 continue;
8354
8355 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
8356 || accessible_p (TYPE_BINFO (type), f, true));
8357
8358 if (accessible)
8359 hasctor[cpy_or_move_ctor_p] = true;
8360
8361 if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
8362 all_trivial = false;
8363
8364 /* Break early when both properties have been determined. */
8365 if (hasctor[0] && hasctor[1] && !all_trivial)
8366 break;
8367 }
8368
8369 return all_trivial;
8370 }
8371
8372 /* Issue a warning on a call to the built-in function FNDECL if it is
8373 a raw memory write whose destination is not an object of (something
8374 like) trivial or standard layout type with a non-deleted assignment
8375 and copy ctor. Detects const correctness violations, corrupting
8376 references, virtual table pointers, and bypassing non-trivial
8377 assignments. */
8378
8379 static void
8380 maybe_warn_class_memaccess (location_t loc, tree fndecl, tree *args)
8381 {
8382 /* Except for bcopy where it's second, the destination pointer is
8383 the first argument for all functions handled here. Compute
8384 the index of the destination and source arguments. */
8385 unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
8386 unsigned srcidx = !dstidx;
8387
8388 tree dest = args[dstidx];
8389 if (!dest || !TREE_TYPE (dest) || !POINTER_TYPE_P (TREE_TYPE (dest)))
8390 return;
8391
8392 /* Remove the outermost (usually implicit) conversion to the void*
8393 argument type. */
8394 if (TREE_CODE (dest) == NOP_EXPR)
8395 dest = TREE_OPERAND (dest, 0);
8396
8397 tree srctype = NULL_TREE;
8398
8399 /* Determine the type of the pointed-to object and whether it's
8400 a complete class type. */
8401 tree desttype = TREE_TYPE (TREE_TYPE (dest));
8402
8403 if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
8404 return;
8405
8406 /* Check to see if the raw memory call is made by a ctor or dtor
8407 with this as the destination argument for the destination type.
8408 If so, be more permissive. */
8409 if (current_function_decl
8410 && (DECL_CONSTRUCTOR_P (current_function_decl)
8411 || DECL_DESTRUCTOR_P (current_function_decl))
8412 && is_this_parameter (tree_strip_nop_conversions (dest)))
8413 {
8414 tree ctx = DECL_CONTEXT (current_function_decl);
8415 bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
8416
8417 tree binfo = TYPE_BINFO (ctx);
8418
8419 /* A ctor and dtor for a class with no bases and no virtual functions
8420 can do whatever they want. Bail early with no further checking. */
8421 if (special && !BINFO_VTABLE (binfo) && !BINFO_N_BASE_BINFOS (binfo))
8422 return;
8423 }
8424
8425 /* True if the class is trivial. */
8426 bool trivial = trivial_type_p (desttype);
8427
8428 /* Set to true if DESTYPE has an accessible copy assignment. */
8429 bool hasassign = false;
8430 /* True if all of the class' overloaded copy assignment operators
8431 are all trivial (and not deleted) and at least one of them is
8432 accessible. */
8433 bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
8434
8435 /* Set to true if DESTTYPE has an accessible default and copy ctor,
8436 respectively. */
8437 bool hasctors[2] = { false, false };
8438
8439 /* True if all of the class' overloaded copy constructors are all
8440 trivial (and not deleted) and at least one of them is accessible. */
8441 bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
8442
8443 /* Set FLD to the first private/protected member of the class. */
8444 tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
8445
8446 /* The warning format string. */
8447 const char *warnfmt = NULL;
8448 /* A suggested alternative to offer instead of the raw memory call.
8449 Empty string when none can be come up with. */
8450 const char *suggest = "";
8451 bool warned = false;
8452
8453 switch (DECL_FUNCTION_CODE (fndecl))
8454 {
8455 case BUILT_IN_MEMSET:
8456 if (!integer_zerop (args[1]))
8457 {
8458 /* Diagnose setting non-copy-assignable or non-trivial types,
8459 or types with a private member, to (potentially) non-zero
8460 bytes. Since the value of the bytes being written is unknown,
8461 suggest using assignment instead (if one exists). Also warn
8462 for writes into objects for which zero-initialization doesn't
8463 mean all bits clear (pointer-to-member data, where null is all
8464 bits set). Since the value being written is (most likely)
8465 non-zero, simply suggest assignment (but not copy assignment). */
8466 suggest = "; use assignment instead";
8467 if (!trivassign)
8468 warnfmt = G_("%qD writing to an object of type %#qT with "
8469 "no trivial copy-assignment");
8470 else if (!trivial)
8471 warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
8472 else if (fld)
8473 {
8474 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
8475 warned = warning_at (loc, OPT_Wclass_memaccess,
8476 "%qD writing to an object of type %#qT with "
8477 "%qs member %qD",
8478 fndecl, desttype, access, fld);
8479 }
8480 else if (!zero_init_p (desttype))
8481 warnfmt = G_("%qD writing to an object of type %#qT containing "
8482 "a pointer to data member%s");
8483
8484 break;
8485 }
8486 /* Fall through. */
8487
8488 case BUILT_IN_BZERO:
8489 /* Similarly to the above, diagnose clearing non-trivial or non-
8490 standard layout objects, or objects of types with no assignmenmt.
8491 Since the value being written is known to be zero, suggest either
8492 copy assignment, copy ctor, or default ctor as an alternative,
8493 depending on what's available. */
8494
8495 if (hasassign && hasctors[0])
8496 suggest = G_("; use assignment or value-initialization instead");
8497 else if (hasassign)
8498 suggest = G_("; use assignment instead");
8499 else if (hasctors[0])
8500 suggest = G_("; use value-initialization instead");
8501
8502 if (!trivassign)
8503 warnfmt = G_("%qD clearing an object of type %#qT with "
8504 "no trivial copy-assignment%s");
8505 else if (!trivial)
8506 warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
8507 else if (!zero_init_p (desttype))
8508 warnfmt = G_("%qD clearing an object of type %#qT containing "
8509 "a pointer-to-member%s");
8510 break;
8511
8512 case BUILT_IN_BCOPY:
8513 case BUILT_IN_MEMCPY:
8514 case BUILT_IN_MEMMOVE:
8515 case BUILT_IN_MEMPCPY:
8516 /* Determine the type of the source object. */
8517 srctype = STRIP_NOPS (args[srcidx]);
8518 srctype = TREE_TYPE (TREE_TYPE (srctype));
8519
8520 /* Since it's impossible to determine wheter the byte copy is
8521 being used in place of assignment to an existing object or
8522 as a substitute for initialization, assume it's the former.
8523 Determine the best alternative to use instead depending on
8524 what's not deleted. */
8525 if (hasassign && hasctors[1])
8526 suggest = G_("; use copy-assignment or copy-initialization instead");
8527 else if (hasassign)
8528 suggest = G_("; use copy-assignment instead");
8529 else if (hasctors[1])
8530 suggest = G_("; use copy-initialization instead");
8531
8532 if (!trivassign)
8533 warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
8534 "copy-assignment%s");
8535 else if (!trivially_copyable_p (desttype))
8536 warnfmt = G_("%qD writing to an object of non-trivially copyable "
8537 "type %#qT%s");
8538 else if (!trivcopy)
8539 warnfmt = G_("%qD writing to an object with a deleted copy constructor");
8540
8541 else if (!trivial
8542 && !VOID_TYPE_P (srctype)
8543 && !char_type_p (TYPE_MAIN_VARIANT (srctype))
8544 && !same_type_ignoring_top_level_qualifiers_p (desttype,
8545 srctype))
8546 {
8547 /* Warn when copying into a non-trivial object from an object
8548 of a different type other than void or char. */
8549 warned = warning_at (loc, OPT_Wclass_memaccess,
8550 "%qD copying an object of non-trivial type "
8551 "%#qT from an array of %#qT",
8552 fndecl, desttype, srctype);
8553 }
8554 else if (fld
8555 && !VOID_TYPE_P (srctype)
8556 && !char_type_p (TYPE_MAIN_VARIANT (srctype))
8557 && !same_type_ignoring_top_level_qualifiers_p (desttype,
8558 srctype))
8559 {
8560 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
8561 warned = warning_at (loc, OPT_Wclass_memaccess,
8562 "%qD copying an object of type %#qT with "
8563 "%qs member %qD from an array of %#qT; use "
8564 "assignment or copy-initialization instead",
8565 fndecl, desttype, access, fld, srctype);
8566 }
8567 else if (!trivial && TREE_CODE (args[2]) == INTEGER_CST)
8568 {
8569 /* Finally, warn on partial copies. */
8570 unsigned HOST_WIDE_INT typesize
8571 = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
8572 if (unsigned HOST_WIDE_INT partial
8573 = tree_to_uhwi (args[2]) % typesize)
8574 warned = warning_at (loc, OPT_Wclass_memaccess,
8575 (typesize - partial > 1
8576 ? G_("%qD writing to an object of "
8577 "a non-trivial type %#qT leaves %wu "
8578 "bytes unchanged")
8579 : G_("%qD writing to an object of "
8580 "a non-trivial type %#qT leaves %wu "
8581 "byte unchanged")),
8582 fndecl, desttype, typesize - partial);
8583 }
8584 break;
8585
8586 case BUILT_IN_REALLOC:
8587
8588 if (!trivially_copyable_p (desttype))
8589 warnfmt = G_("%qD moving an object of non-trivially copyable type "
8590 "%#qT; use %<new%> and %<delete%> instead");
8591 else if (!trivcopy)
8592 warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
8593 "constructor; use %<new%> and %<delete%> instead");
8594 else if (!get_dtor (desttype, tf_none))
8595 warnfmt = G_("%qD moving an object of type %#qT with deleted "
8596 "destructor");
8597 else if (!trivial
8598 && TREE_CODE (args[1]) == INTEGER_CST
8599 && tree_int_cst_lt (args[1], TYPE_SIZE_UNIT (desttype)))
8600 {
8601 /* Finally, warn on reallocation into insufficient space. */
8602 warned = warning_at (loc, OPT_Wclass_memaccess,
8603 "%qD moving an object of non-trivial type "
8604 "%#qT and size %E into a region of size %E",
8605 fndecl, desttype, TYPE_SIZE_UNIT (desttype),
8606 args[1]);
8607 }
8608 break;
8609
8610 default:
8611 return;
8612 }
8613
8614 if (!warned && !warnfmt)
8615 return;
8616
8617 if (warnfmt)
8618 {
8619 if (suggest)
8620 warned = warning_at (loc, OPT_Wclass_memaccess,
8621 warnfmt, fndecl, desttype, suggest);
8622 else
8623 warned = warning_at (loc, OPT_Wclass_memaccess,
8624 warnfmt, fndecl, desttype);
8625 }
8626
8627 if (warned)
8628 inform (location_of (desttype), "%#qT declared here", desttype);
8629 }
8630
8631 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
8632 This function performs no overload resolution, conversion, or other
8633 high-level operations. */
8634
8635 tree
8636 build_cxx_call (tree fn, int nargs, tree *argarray,
8637 tsubst_flags_t complain)
8638 {
8639 tree fndecl;
8640
8641 /* Remember roughly where this call is. */
8642 location_t loc = EXPR_LOC_OR_LOC (fn, input_location);
8643 fn = build_call_a (fn, nargs, argarray);
8644 SET_EXPR_LOCATION (fn, loc);
8645
8646 fndecl = get_callee_fndecl (fn);
8647
8648 /* Check that arguments to builtin functions match the expectations. */
8649 if (fndecl
8650 && DECL_BUILT_IN (fndecl)
8651 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
8652 {
8653 int i;
8654
8655 /* We need to take care that values to BUILT_IN_NORMAL
8656 are reduced. */
8657 for (i = 0; i < nargs; i++)
8658 argarray[i] = fold_non_dependent_expr (argarray[i]);
8659
8660 if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
8661 nargs, argarray))
8662 return error_mark_node;
8663
8664 /* Warn if the built-in writes to an object of a non-trivial type. */
8665 if (nargs)
8666 maybe_warn_class_memaccess (loc, fndecl, argarray);
8667 }
8668
8669 /* If it is a built-in array notation function, then the return type of
8670 the function is the element type of the array passed in as array
8671 notation (i.e. the first parameter of the function). */
8672 if (flag_cilkplus && TREE_CODE (fn) == CALL_EXPR)
8673 {
8674 enum built_in_function bif =
8675 is_cilkplus_reduce_builtin (CALL_EXPR_FN (fn));
8676 if (bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
8677 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
8678 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
8679 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
8680 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE
8681 || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
8682 {
8683 if (call_expr_nargs (fn) == 0)
8684 {
8685 error_at (EXPR_LOCATION (fn), "Invalid builtin arguments");
8686 return error_mark_node;
8687 }
8688 /* for bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO or
8689 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO or
8690 BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO or
8691 BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO or
8692 BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND or
8693 BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
8694 The pre-defined return-type is the correct one. */
8695 tree array_ntn = CALL_EXPR_ARG (fn, 0);
8696 TREE_TYPE (fn) = TREE_TYPE (array_ntn);
8697 return fn;
8698 }
8699 }
8700
8701 if (VOID_TYPE_P (TREE_TYPE (fn)))
8702 return fn;
8703
8704 /* 5.2.2/11: If a function call is a prvalue of object type: if the
8705 function call is either the operand of a decltype-specifier or the
8706 right operand of a comma operator that is the operand of a
8707 decltype-specifier, a temporary object is not introduced for the
8708 prvalue. The type of the prvalue may be incomplete. */
8709 if (!(complain & tf_decltype))
8710 {
8711 fn = require_complete_type_sfinae (fn, complain);
8712 if (fn == error_mark_node)
8713 return error_mark_node;
8714
8715 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
8716 {
8717 fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
8718 maybe_warn_parm_abi (TREE_TYPE (fn), loc);
8719 }
8720 }
8721 return convert_from_reference (fn);
8722 }
8723
8724 /* Returns the value to use for the in-charge parameter when making a
8725 call to a function with the indicated NAME.
8726
8727 FIXME:Can't we find a neater way to do this mapping? */
8728
8729 tree
8730 in_charge_arg_for_name (tree name)
8731 {
8732 if (IDENTIFIER_CTOR_P (name))
8733 {
8734 if (name == complete_ctor_identifier)
8735 return integer_one_node;
8736 gcc_checking_assert (name == base_ctor_identifier);
8737 }
8738 else
8739 {
8740 if (name == complete_dtor_identifier)
8741 return integer_two_node;
8742 else if (name == deleting_dtor_identifier)
8743 return integer_three_node;
8744 gcc_checking_assert (name == base_dtor_identifier);
8745 }
8746
8747 return integer_zero_node;
8748 }
8749
8750 /* We've built up a constructor call RET. Complain if it delegates to the
8751 constructor we're currently compiling. */
8752
8753 static void
8754 check_self_delegation (tree ret)
8755 {
8756 if (TREE_CODE (ret) == TARGET_EXPR)
8757 ret = TARGET_EXPR_INITIAL (ret);
8758 tree fn = cp_get_callee_fndecl (ret);
8759 if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
8760 error ("constructor delegates to itself");
8761 }
8762
8763 /* Build a call to a constructor, destructor, or an assignment
8764 operator for INSTANCE, an expression with class type. NAME
8765 indicates the special member function to call; *ARGS are the
8766 arguments. ARGS may be NULL. This may change ARGS. BINFO
8767 indicates the base of INSTANCE that is to be passed as the `this'
8768 parameter to the member function called.
8769
8770 FLAGS are the LOOKUP_* flags to use when processing the call.
8771
8772 If NAME indicates a complete object constructor, INSTANCE may be
8773 NULL_TREE. In this case, the caller will call build_cplus_new to
8774 store the newly constructed object into a VAR_DECL. */
8775
8776 tree
8777 build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
8778 tree binfo, int flags, tsubst_flags_t complain)
8779 {
8780 tree fns;
8781 /* The type of the subobject to be constructed or destroyed. */
8782 tree class_type;
8783 vec<tree, va_gc> *allocated = NULL;
8784 tree ret;
8785
8786 gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
8787 if (TYPE_P (binfo))
8788 {
8789 /* Resolve the name. */
8790 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
8791 return error_mark_node;
8792
8793 binfo = TYPE_BINFO (binfo);
8794 }
8795
8796 gcc_assert (binfo != NULL_TREE);
8797
8798 class_type = BINFO_TYPE (binfo);
8799
8800 /* Handle the special case where INSTANCE is NULL_TREE. */
8801 if (name == complete_ctor_identifier && !instance)
8802 instance = build_dummy_object (class_type);
8803 else
8804 {
8805 if (IDENTIFIER_DTOR_P (name))
8806 gcc_assert (args == NULL || vec_safe_is_empty (*args));
8807
8808 /* Convert to the base class, if necessary. */
8809 if (!same_type_ignoring_top_level_qualifiers_p
8810 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
8811 {
8812 if (IDENTIFIER_CDTOR_P (name))
8813 /* For constructors and destructors, either the base is
8814 non-virtual, or it is virtual but we are doing the
8815 conversion from a constructor or destructor for the
8816 complete object. In either case, we can convert
8817 statically. */
8818 instance = convert_to_base_statically (instance, binfo);
8819 else
8820 {
8821 /* However, for assignment operators, we must convert
8822 dynamically if the base is virtual. */
8823 gcc_checking_assert (name == assign_op_identifier);
8824 instance = build_base_path (PLUS_EXPR, instance,
8825 binfo, /*nonnull=*/1, complain);
8826 }
8827 }
8828 }
8829
8830 gcc_assert (instance != NULL_TREE);
8831
8832 /* In C++17, "If the initializer expression is a prvalue and the
8833 cv-unqualified version of the source type is the same class as the class
8834 of the destination, the initializer expression is used to initialize the
8835 destination object." Handle that here to avoid doing overload
8836 resolution. */
8837 if (cxx_dialect >= cxx17
8838 && args && vec_safe_length (*args) == 1
8839 && name == complete_ctor_identifier)
8840 {
8841 tree arg = (**args)[0];
8842
8843 /* FIXME P0135 doesn't say how to handle direct initialization from a
8844 type with a suitable conversion operator. Let's handle it like
8845 copy-initialization, but allowing explict conversions. */
8846 tsubst_flags_t sub_complain = tf_warning;
8847 if (!is_dummy_object (instance))
8848 /* If we're using this to initialize a non-temporary object, don't
8849 require the destructor to be accessible. */
8850 sub_complain |= tf_no_cleanup;
8851 if (!reference_related_p (class_type, TREE_TYPE (arg)))
8852 arg = perform_implicit_conversion_flags (class_type, arg,
8853 sub_complain,
8854 flags);
8855 if ((TREE_CODE (arg) == TARGET_EXPR
8856 || TREE_CODE (arg) == CONSTRUCTOR)
8857 && (same_type_ignoring_top_level_qualifiers_p
8858 (class_type, TREE_TYPE (arg))))
8859 {
8860 if (is_dummy_object (instance))
8861 return arg;
8862 if ((complain & tf_error)
8863 && (flags & LOOKUP_DELEGATING_CONS))
8864 check_self_delegation (arg);
8865 /* Avoid change of behavior on Wunused-var-2.C. */
8866 instance = mark_lvalue_use (instance);
8867 return build2 (INIT_EXPR, class_type, instance, arg);
8868 }
8869 }
8870
8871 fns = lookup_fnfields (binfo, name, 1);
8872
8873 /* When making a call to a constructor or destructor for a subobject
8874 that uses virtual base classes, pass down a pointer to a VTT for
8875 the subobject. */
8876 if ((name == base_ctor_identifier
8877 || name == base_dtor_identifier)
8878 && CLASSTYPE_VBASECLASSES (class_type))
8879 {
8880 tree vtt;
8881 tree sub_vtt;
8882
8883 /* If the current function is a complete object constructor
8884 or destructor, then we fetch the VTT directly.
8885 Otherwise, we look it up using the VTT we were given. */
8886 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
8887 vtt = decay_conversion (vtt, complain);
8888 if (vtt == error_mark_node)
8889 return error_mark_node;
8890 vtt = build_if_in_charge (vtt, current_vtt_parm);
8891 if (BINFO_SUBVTT_INDEX (binfo))
8892 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
8893 else
8894 sub_vtt = vtt;
8895
8896 if (args == NULL)
8897 {
8898 allocated = make_tree_vector ();
8899 args = &allocated;
8900 }
8901
8902 vec_safe_insert (*args, 0, sub_vtt);
8903 }
8904
8905 ret = build_new_method_call (instance, fns, args,
8906 TYPE_BINFO (BINFO_TYPE (binfo)),
8907 flags, /*fn=*/NULL,
8908 complain);
8909
8910 if (allocated != NULL)
8911 release_tree_vector (allocated);
8912
8913 if ((complain & tf_error)
8914 && (flags & LOOKUP_DELEGATING_CONS)
8915 && name == complete_ctor_identifier)
8916 check_self_delegation (ret);
8917
8918 return ret;
8919 }
8920
8921 /* Return the NAME, as a C string. The NAME indicates a function that
8922 is a member of TYPE. *FREE_P is set to true if the caller must
8923 free the memory returned.
8924
8925 Rather than go through all of this, we should simply set the names
8926 of constructors and destructors appropriately, and dispense with
8927 ctor_identifier, dtor_identifier, etc. */
8928
8929 static char *
8930 name_as_c_string (tree name, tree type, bool *free_p)
8931 {
8932 const char *pretty_name;
8933
8934 /* Assume that we will not allocate memory. */
8935 *free_p = false;
8936 /* Constructors and destructors are special. */
8937 if (IDENTIFIER_CDTOR_P (name))
8938 {
8939 pretty_name
8940 = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
8941 /* For a destructor, add the '~'. */
8942 if (IDENTIFIER_DTOR_P (name))
8943 {
8944 pretty_name = concat ("~", pretty_name, NULL);
8945 /* Remember that we need to free the memory allocated. */
8946 *free_p = true;
8947 }
8948 }
8949 else if (IDENTIFIER_CONV_OP_P (name))
8950 {
8951 pretty_name = concat ("operator ",
8952 type_as_string_translate (TREE_TYPE (name),
8953 TFF_PLAIN_IDENTIFIER),
8954 NULL);
8955 /* Remember that we need to free the memory allocated. */
8956 *free_p = true;
8957 }
8958 else
8959 pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
8960
8961 return CONST_CAST (char *, pretty_name);
8962 }
8963
8964 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
8965 be set, upon return, to the function called. ARGS may be NULL.
8966 This may change ARGS. */
8967
8968 static tree
8969 build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
8970 tree conversion_path, int flags,
8971 tree *fn_p, tsubst_flags_t complain)
8972 {
8973 struct z_candidate *candidates = 0, *cand;
8974 tree explicit_targs = NULL_TREE;
8975 tree basetype = NULL_TREE;
8976 tree access_binfo, binfo;
8977 tree optype;
8978 tree first_mem_arg = NULL_TREE;
8979 tree name;
8980 bool skip_first_for_error;
8981 vec<tree, va_gc> *user_args;
8982 tree call;
8983 tree fn;
8984 int template_only = 0;
8985 bool any_viable_p;
8986 tree orig_instance;
8987 tree orig_fns;
8988 vec<tree, va_gc> *orig_args = NULL;
8989 void *p;
8990
8991 gcc_assert (instance != NULL_TREE);
8992
8993 /* We don't know what function we're going to call, yet. */
8994 if (fn_p)
8995 *fn_p = NULL_TREE;
8996
8997 if (error_operand_p (instance)
8998 || !fns || error_operand_p (fns))
8999 return error_mark_node;
9000
9001 if (!BASELINK_P (fns))
9002 {
9003 if (complain & tf_error)
9004 error ("call to non-function %qD", fns);
9005 return error_mark_node;
9006 }
9007
9008 orig_instance = instance;
9009 orig_fns = fns;
9010
9011 /* Dismantle the baselink to collect all the information we need. */
9012 if (!conversion_path)
9013 conversion_path = BASELINK_BINFO (fns);
9014 access_binfo = BASELINK_ACCESS_BINFO (fns);
9015 binfo = BASELINK_BINFO (fns);
9016 optype = BASELINK_OPTYPE (fns);
9017 fns = BASELINK_FUNCTIONS (fns);
9018 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
9019 {
9020 explicit_targs = TREE_OPERAND (fns, 1);
9021 fns = TREE_OPERAND (fns, 0);
9022 template_only = 1;
9023 }
9024 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
9025 || TREE_CODE (fns) == TEMPLATE_DECL
9026 || TREE_CODE (fns) == OVERLOAD);
9027 fn = OVL_FIRST (fns);
9028 name = DECL_NAME (fn);
9029
9030 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
9031 gcc_assert (CLASS_TYPE_P (basetype));
9032
9033 if (processing_template_decl)
9034 {
9035 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
9036 instance = build_non_dependent_expr (instance);
9037 if (args != NULL)
9038 make_args_non_dependent (*args);
9039 }
9040
9041 user_args = args == NULL ? NULL : *args;
9042 /* Under DR 147 A::A() is an invalid constructor call,
9043 not a functional cast. */
9044 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
9045 {
9046 if (! (complain & tf_error))
9047 return error_mark_node;
9048
9049 basetype = DECL_CONTEXT (fn);
9050 name = constructor_name (basetype);
9051 if (permerror (input_location,
9052 "cannot call constructor %<%T::%D%> directly",
9053 basetype, name))
9054 inform (input_location, "for a function-style cast, remove the "
9055 "redundant %<::%D%>", name);
9056 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
9057 complain);
9058 return call;
9059 }
9060
9061 /* Process the argument list. */
9062 if (args != NULL && *args != NULL)
9063 {
9064 *args = resolve_args (*args, complain);
9065 if (*args == NULL)
9066 return error_mark_node;
9067 }
9068
9069 /* Consider the object argument to be used even if we end up selecting a
9070 static member function. */
9071 instance = mark_type_use (instance);
9072
9073 /* Figure out whether to skip the first argument for the error
9074 message we will display to users if an error occurs. We don't
9075 want to display any compiler-generated arguments. The "this"
9076 pointer hasn't been added yet. However, we must remove the VTT
9077 pointer if this is a call to a base-class constructor or
9078 destructor. */
9079 skip_first_for_error = false;
9080 if (IDENTIFIER_CDTOR_P (name))
9081 {
9082 /* Callers should explicitly indicate whether they want to ctor
9083 the complete object or just the part without virtual bases. */
9084 gcc_assert (name != ctor_identifier);
9085
9086 /* Remove the VTT pointer, if present. */
9087 if ((name == base_ctor_identifier || name == base_dtor_identifier)
9088 && CLASSTYPE_VBASECLASSES (basetype))
9089 skip_first_for_error = true;
9090
9091 /* It's OK to call destructors and constructors on cv-qualified
9092 objects. Therefore, convert the INSTANCE to the unqualified
9093 type, if necessary. */
9094 if (!same_type_p (basetype, TREE_TYPE (instance)))
9095 {
9096 instance = build_this (instance);
9097 instance = build_nop (build_pointer_type (basetype), instance);
9098 instance = build_fold_indirect_ref (instance);
9099 }
9100 }
9101 else
9102 gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
9103
9104 /* For the overload resolution we need to find the actual `this`
9105 that would be captured if the call turns out to be to a
9106 non-static member function. Do not actually capture it at this
9107 point. */
9108 if (DECL_CONSTRUCTOR_P (fn))
9109 /* Constructors don't use the enclosing 'this'. */
9110 first_mem_arg = instance;
9111 else
9112 first_mem_arg = maybe_resolve_dummy (instance, false);
9113
9114 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9115 p = conversion_obstack_alloc (0);
9116
9117 /* The number of arguments artificial parms in ARGS; we subtract one because
9118 there's no 'this' in ARGS. */
9119 unsigned skip = num_artificial_parms_for (fn) - 1;
9120
9121 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
9122 initializer, not T({ }). */
9123 if (DECL_CONSTRUCTOR_P (fn)
9124 && vec_safe_length (user_args) > skip
9125 && DIRECT_LIST_INIT_P ((*user_args)[skip]))
9126 {
9127 tree init_list = (*user_args)[skip];
9128 tree init = NULL_TREE;
9129
9130 gcc_assert (user_args->length () == skip + 1
9131 && !(flags & LOOKUP_ONLYCONVERTING));
9132
9133 /* If the initializer list has no elements and T is a class type with
9134 a default constructor, the object is value-initialized. Handle
9135 this here so we don't need to handle it wherever we use
9136 build_special_member_call. */
9137 if (CONSTRUCTOR_NELTS (init_list) == 0
9138 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
9139 /* For a user-provided default constructor, use the normal
9140 mechanisms so that protected access works. */
9141 && type_has_non_user_provided_default_constructor (basetype)
9142 && !processing_template_decl)
9143 init = build_value_init (basetype, complain);
9144
9145 /* If BASETYPE is an aggregate, we need to do aggregate
9146 initialization. */
9147 else if (CP_AGGREGATE_TYPE_P (basetype))
9148 {
9149 init = reshape_init (basetype, init_list, complain);
9150 init = digest_init (basetype, init, complain);
9151 }
9152
9153 if (init)
9154 {
9155 if (is_dummy_object (instance))
9156 return get_target_expr_sfinae (init, complain);
9157 init = build2 (INIT_EXPR, TREE_TYPE (instance), instance, init);
9158 TREE_SIDE_EFFECTS (init) = true;
9159 return init;
9160 }
9161
9162 /* Otherwise go ahead with overload resolution. */
9163 add_list_candidates (fns, first_mem_arg, user_args,
9164 basetype, explicit_targs, template_only,
9165 conversion_path, access_binfo, flags,
9166 &candidates, complain);
9167 }
9168 else
9169 add_candidates (fns, first_mem_arg, user_args, optype,
9170 explicit_targs, template_only, conversion_path,
9171 access_binfo, flags, &candidates, complain);
9172
9173 any_viable_p = false;
9174 candidates = splice_viable (candidates, false, &any_viable_p);
9175
9176 if (!any_viable_p)
9177 {
9178 if (complain & tf_error)
9179 {
9180 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
9181 cxx_incomplete_type_error (instance, basetype);
9182 else if (optype)
9183 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
9184 basetype, optype, build_tree_list_vec (user_args),
9185 TREE_TYPE (instance));
9186 else
9187 {
9188 tree arglist = build_tree_list_vec (user_args);
9189 tree errname = name;
9190 bool twiddle = false;
9191 if (IDENTIFIER_CDTOR_P (errname))
9192 {
9193 twiddle = IDENTIFIER_DTOR_P (errname);
9194 errname = constructor_name (basetype);
9195 }
9196 if (explicit_targs)
9197 errname = lookup_template_function (errname, explicit_targs);
9198 if (skip_first_for_error)
9199 arglist = TREE_CHAIN (arglist);
9200 error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
9201 basetype, &"~"[!twiddle], errname, arglist,
9202 TREE_TYPE (instance));
9203 }
9204 print_z_candidates (location_of (name), candidates);
9205 }
9206 call = error_mark_node;
9207 }
9208 else
9209 {
9210 cand = tourney (candidates, complain);
9211 if (cand == 0)
9212 {
9213 char *pretty_name;
9214 bool free_p;
9215 tree arglist;
9216
9217 if (complain & tf_error)
9218 {
9219 pretty_name = name_as_c_string (name, basetype, &free_p);
9220 arglist = build_tree_list_vec (user_args);
9221 if (skip_first_for_error)
9222 arglist = TREE_CHAIN (arglist);
9223 if (!any_strictly_viable (candidates))
9224 error ("no matching function for call to %<%s(%A)%>",
9225 pretty_name, arglist);
9226 else
9227 error ("call of overloaded %<%s(%A)%> is ambiguous",
9228 pretty_name, arglist);
9229 print_z_candidates (location_of (name), candidates);
9230 if (free_p)
9231 free (pretty_name);
9232 }
9233 call = error_mark_node;
9234 }
9235 else
9236 {
9237 fn = cand->fn;
9238 call = NULL_TREE;
9239
9240 if (!(flags & LOOKUP_NONVIRTUAL)
9241 && DECL_PURE_VIRTUAL_P (fn)
9242 && instance == current_class_ref
9243 && (complain & tf_warning))
9244 {
9245 /* This is not an error, it is runtime undefined
9246 behavior. */
9247 if (!current_function_decl)
9248 warning (0, "pure virtual %q#D called from "
9249 "non-static data member initializer", fn);
9250 else if (DECL_CONSTRUCTOR_P (current_function_decl)
9251 || DECL_DESTRUCTOR_P (current_function_decl))
9252 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
9253 ? G_("pure virtual %q#D called from constructor")
9254 : G_("pure virtual %q#D called from destructor")),
9255 fn);
9256 }
9257
9258 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
9259 && !DECL_CONSTRUCTOR_P (fn)
9260 && is_dummy_object (instance))
9261 {
9262 instance = maybe_resolve_dummy (instance, true);
9263 if (instance == error_mark_node)
9264 call = error_mark_node;
9265 else if (!is_dummy_object (instance))
9266 {
9267 /* We captured 'this' in the current lambda now that
9268 we know we really need it. */
9269 cand->first_arg = instance;
9270 }
9271 else if (any_dependent_bases_p ())
9272 /* We can't tell until instantiation time whether we can use
9273 *this as the implicit object argument. */;
9274 else
9275 {
9276 if (complain & tf_error)
9277 error ("cannot call member function %qD without object",
9278 fn);
9279 call = error_mark_node;
9280 }
9281 }
9282
9283 if (call != error_mark_node)
9284 {
9285 /* Optimize away vtable lookup if we know that this
9286 function can't be overridden. We need to check if
9287 the context and the type where we found fn are the same,
9288 actually FN might be defined in a different class
9289 type because of a using-declaration. In this case, we
9290 do not want to perform a non-virtual call. */
9291 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
9292 && same_type_ignoring_top_level_qualifiers_p
9293 (DECL_CONTEXT (fn), BINFO_TYPE (binfo))
9294 && resolves_to_fixed_type_p (instance, 0))
9295 flags |= LOOKUP_NONVIRTUAL;
9296 if (explicit_targs)
9297 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
9298 /* Now we know what function is being called. */
9299 if (fn_p)
9300 *fn_p = fn;
9301 /* Build the actual CALL_EXPR. */
9302 call = build_over_call (cand, flags, complain);
9303 /* In an expression of the form `a->f()' where `f' turns
9304 out to be a static member function, `a' is
9305 none-the-less evaluated. */
9306 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
9307 && !is_dummy_object (instance)
9308 && TREE_SIDE_EFFECTS (instance))
9309 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
9310 instance, call);
9311 else if (call != error_mark_node
9312 && DECL_DESTRUCTOR_P (cand->fn)
9313 && !VOID_TYPE_P (TREE_TYPE (call)))
9314 /* An explicit call of the form "x->~X()" has type
9315 "void". However, on platforms where destructors
9316 return "this" (i.e., those where
9317 targetm.cxx.cdtor_returns_this is true), such calls
9318 will appear to have a return value of pointer type
9319 to the low-level call machinery. We do not want to
9320 change the low-level machinery, since we want to be
9321 able to optimize "delete f()" on such platforms as
9322 "operator delete(~X(f()))" (rather than generating
9323 "t = f(), ~X(t), operator delete (t)"). */
9324 call = build_nop (void_type_node, call);
9325 }
9326 }
9327 }
9328
9329 if (processing_template_decl && call != error_mark_node)
9330 {
9331 bool cast_to_void = false;
9332
9333 if (TREE_CODE (call) == COMPOUND_EXPR)
9334 call = TREE_OPERAND (call, 1);
9335 else if (TREE_CODE (call) == NOP_EXPR)
9336 {
9337 cast_to_void = true;
9338 call = TREE_OPERAND (call, 0);
9339 }
9340 if (INDIRECT_REF_P (call))
9341 call = TREE_OPERAND (call, 0);
9342 call = (build_min_non_dep_call_vec
9343 (call,
9344 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
9345 orig_instance, orig_fns, NULL_TREE),
9346 orig_args));
9347 SET_EXPR_LOCATION (call, input_location);
9348 call = convert_from_reference (call);
9349 if (cast_to_void)
9350 call = build_nop (void_type_node, call);
9351 }
9352
9353 /* Free all the conversions we allocated. */
9354 obstack_free (&conversion_obstack, p);
9355
9356 if (orig_args != NULL)
9357 release_tree_vector (orig_args);
9358
9359 return call;
9360 }
9361
9362 /* Wrapper for above. */
9363
9364 tree
9365 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
9366 tree conversion_path, int flags,
9367 tree *fn_p, tsubst_flags_t complain)
9368 {
9369 tree ret;
9370 bool subtime = timevar_cond_start (TV_OVERLOAD);
9371 ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
9372 fn_p, complain);
9373 timevar_cond_stop (TV_OVERLOAD, subtime);
9374 return ret;
9375 }
9376
9377 /* Returns true iff standard conversion sequence ICS1 is a proper
9378 subsequence of ICS2. */
9379
9380 static bool
9381 is_subseq (conversion *ics1, conversion *ics2)
9382 {
9383 /* We can assume that a conversion of the same code
9384 between the same types indicates a subsequence since we only get
9385 here if the types we are converting from are the same. */
9386
9387 while (ics1->kind == ck_rvalue
9388 || ics1->kind == ck_lvalue)
9389 ics1 = next_conversion (ics1);
9390
9391 while (1)
9392 {
9393 while (ics2->kind == ck_rvalue
9394 || ics2->kind == ck_lvalue)
9395 ics2 = next_conversion (ics2);
9396
9397 if (ics2->kind == ck_user
9398 || ics2->kind == ck_ambig
9399 || ics2->kind == ck_aggr
9400 || ics2->kind == ck_list
9401 || ics2->kind == ck_identity)
9402 /* At this point, ICS1 cannot be a proper subsequence of
9403 ICS2. We can get a USER_CONV when we are comparing the
9404 second standard conversion sequence of two user conversion
9405 sequences. */
9406 return false;
9407
9408 ics2 = next_conversion (ics2);
9409
9410 while (ics2->kind == ck_rvalue
9411 || ics2->kind == ck_lvalue)
9412 ics2 = next_conversion (ics2);
9413
9414 if (ics2->kind == ics1->kind
9415 && same_type_p (ics2->type, ics1->type)
9416 && (ics1->kind == ck_identity
9417 || same_type_p (next_conversion (ics2)->type,
9418 next_conversion (ics1)->type)))
9419 return true;
9420 }
9421 }
9422
9423 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
9424 be any _TYPE nodes. */
9425
9426 bool
9427 is_properly_derived_from (tree derived, tree base)
9428 {
9429 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
9430 return false;
9431
9432 /* We only allow proper derivation here. The DERIVED_FROM_P macro
9433 considers every class derived from itself. */
9434 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
9435 && DERIVED_FROM_P (base, derived));
9436 }
9437
9438 /* We build the ICS for an implicit object parameter as a pointer
9439 conversion sequence. However, such a sequence should be compared
9440 as if it were a reference conversion sequence. If ICS is the
9441 implicit conversion sequence for an implicit object parameter,
9442 modify it accordingly. */
9443
9444 static void
9445 maybe_handle_implicit_object (conversion **ics)
9446 {
9447 if ((*ics)->this_p)
9448 {
9449 /* [over.match.funcs]
9450
9451 For non-static member functions, the type of the
9452 implicit object parameter is "reference to cv X"
9453 where X is the class of which the function is a
9454 member and cv is the cv-qualification on the member
9455 function declaration. */
9456 conversion *t = *ics;
9457 tree reference_type;
9458
9459 /* The `this' parameter is a pointer to a class type. Make the
9460 implicit conversion talk about a reference to that same class
9461 type. */
9462 reference_type = TREE_TYPE (t->type);
9463 reference_type = build_reference_type (reference_type);
9464
9465 if (t->kind == ck_qual)
9466 t = next_conversion (t);
9467 if (t->kind == ck_ptr)
9468 t = next_conversion (t);
9469 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
9470 t = direct_reference_binding (reference_type, t);
9471 t->this_p = 1;
9472 t->rvaluedness_matches_p = 0;
9473 *ics = t;
9474 }
9475 }
9476
9477 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
9478 and return the initial reference binding conversion. Otherwise,
9479 leave *ICS unchanged and return NULL. */
9480
9481 static conversion *
9482 maybe_handle_ref_bind (conversion **ics)
9483 {
9484 if ((*ics)->kind == ck_ref_bind)
9485 {
9486 conversion *old_ics = *ics;
9487 *ics = next_conversion (old_ics);
9488 (*ics)->user_conv_p = old_ics->user_conv_p;
9489 return old_ics;
9490 }
9491
9492 return NULL;
9493 }
9494
9495 /* Compare two implicit conversion sequences according to the rules set out in
9496 [over.ics.rank]. Return values:
9497
9498 1: ics1 is better than ics2
9499 -1: ics2 is better than ics1
9500 0: ics1 and ics2 are indistinguishable */
9501
9502 static int
9503 compare_ics (conversion *ics1, conversion *ics2)
9504 {
9505 tree from_type1;
9506 tree from_type2;
9507 tree to_type1;
9508 tree to_type2;
9509 tree deref_from_type1 = NULL_TREE;
9510 tree deref_from_type2 = NULL_TREE;
9511 tree deref_to_type1 = NULL_TREE;
9512 tree deref_to_type2 = NULL_TREE;
9513 conversion_rank rank1, rank2;
9514
9515 /* REF_BINDING is nonzero if the result of the conversion sequence
9516 is a reference type. In that case REF_CONV is the reference
9517 binding conversion. */
9518 conversion *ref_conv1;
9519 conversion *ref_conv2;
9520
9521 /* Compare badness before stripping the reference conversion. */
9522 if (ics1->bad_p > ics2->bad_p)
9523 return -1;
9524 else if (ics1->bad_p < ics2->bad_p)
9525 return 1;
9526
9527 /* Handle implicit object parameters. */
9528 maybe_handle_implicit_object (&ics1);
9529 maybe_handle_implicit_object (&ics2);
9530
9531 /* Handle reference parameters. */
9532 ref_conv1 = maybe_handle_ref_bind (&ics1);
9533 ref_conv2 = maybe_handle_ref_bind (&ics2);
9534
9535 /* List-initialization sequence L1 is a better conversion sequence than
9536 list-initialization sequence L2 if L1 converts to
9537 std::initializer_list<X> for some X and L2 does not. */
9538 if (ics1->kind == ck_list && ics2->kind != ck_list)
9539 return 1;
9540 if (ics2->kind == ck_list && ics1->kind != ck_list)
9541 return -1;
9542
9543 /* [over.ics.rank]
9544
9545 When comparing the basic forms of implicit conversion sequences (as
9546 defined in _over.best.ics_)
9547
9548 --a standard conversion sequence (_over.ics.scs_) is a better
9549 conversion sequence than a user-defined conversion sequence
9550 or an ellipsis conversion sequence, and
9551
9552 --a user-defined conversion sequence (_over.ics.user_) is a
9553 better conversion sequence than an ellipsis conversion sequence
9554 (_over.ics.ellipsis_). */
9555 /* Use BAD_CONVERSION_RANK because we already checked for a badness
9556 mismatch. If both ICS are bad, we try to make a decision based on
9557 what would have happened if they'd been good. This is not an
9558 extension, we'll still give an error when we build up the call; this
9559 just helps us give a more helpful error message. */
9560 rank1 = BAD_CONVERSION_RANK (ics1);
9561 rank2 = BAD_CONVERSION_RANK (ics2);
9562
9563 if (rank1 > rank2)
9564 return -1;
9565 else if (rank1 < rank2)
9566 return 1;
9567
9568 if (ics1->ellipsis_p)
9569 /* Both conversions are ellipsis conversions. */
9570 return 0;
9571
9572 /* User-defined conversion sequence U1 is a better conversion sequence
9573 than another user-defined conversion sequence U2 if they contain the
9574 same user-defined conversion operator or constructor and if the sec-
9575 ond standard conversion sequence of U1 is better than the second
9576 standard conversion sequence of U2. */
9577
9578 /* Handle list-conversion with the same code even though it isn't always
9579 ranked as a user-defined conversion and it doesn't have a second
9580 standard conversion sequence; it will still have the desired effect.
9581 Specifically, we need to do the reference binding comparison at the
9582 end of this function. */
9583
9584 if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
9585 {
9586 conversion *t1;
9587 conversion *t2;
9588
9589 for (t1 = ics1; t1->kind != ck_user; t1 = next_conversion (t1))
9590 if (t1->kind == ck_ambig || t1->kind == ck_aggr
9591 || t1->kind == ck_list)
9592 break;
9593 for (t2 = ics2; t2->kind != ck_user; t2 = next_conversion (t2))
9594 if (t2->kind == ck_ambig || t2->kind == ck_aggr
9595 || t2->kind == ck_list)
9596 break;
9597
9598 if (t1->kind != t2->kind)
9599 return 0;
9600 else if (t1->kind == ck_user)
9601 {
9602 tree f1 = t1->cand ? t1->cand->fn : t1->type;
9603 tree f2 = t2->cand ? t2->cand->fn : t2->type;
9604 if (f1 != f2)
9605 return 0;
9606 }
9607 else
9608 {
9609 /* For ambiguous or aggregate conversions, use the target type as
9610 a proxy for the conversion function. */
9611 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
9612 return 0;
9613 }
9614
9615 /* We can just fall through here, after setting up
9616 FROM_TYPE1 and FROM_TYPE2. */
9617 from_type1 = t1->type;
9618 from_type2 = t2->type;
9619 }
9620 else
9621 {
9622 conversion *t1;
9623 conversion *t2;
9624
9625 /* We're dealing with two standard conversion sequences.
9626
9627 [over.ics.rank]
9628
9629 Standard conversion sequence S1 is a better conversion
9630 sequence than standard conversion sequence S2 if
9631
9632 --S1 is a proper subsequence of S2 (comparing the conversion
9633 sequences in the canonical form defined by _over.ics.scs_,
9634 excluding any Lvalue Transformation; the identity
9635 conversion sequence is considered to be a subsequence of
9636 any non-identity conversion sequence */
9637
9638 t1 = ics1;
9639 while (t1->kind != ck_identity)
9640 t1 = next_conversion (t1);
9641 from_type1 = t1->type;
9642
9643 t2 = ics2;
9644 while (t2->kind != ck_identity)
9645 t2 = next_conversion (t2);
9646 from_type2 = t2->type;
9647 }
9648
9649 /* One sequence can only be a subsequence of the other if they start with
9650 the same type. They can start with different types when comparing the
9651 second standard conversion sequence in two user-defined conversion
9652 sequences. */
9653 if (same_type_p (from_type1, from_type2))
9654 {
9655 if (is_subseq (ics1, ics2))
9656 return 1;
9657 if (is_subseq (ics2, ics1))
9658 return -1;
9659 }
9660
9661 /* [over.ics.rank]
9662
9663 Or, if not that,
9664
9665 --the rank of S1 is better than the rank of S2 (by the rules
9666 defined below):
9667
9668 Standard conversion sequences are ordered by their ranks: an Exact
9669 Match is a better conversion than a Promotion, which is a better
9670 conversion than a Conversion.
9671
9672 Two conversion sequences with the same rank are indistinguishable
9673 unless one of the following rules applies:
9674
9675 --A conversion that does not a convert a pointer, pointer to member,
9676 or std::nullptr_t to bool is better than one that does.
9677
9678 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
9679 so that we do not have to check it explicitly. */
9680 if (ics1->rank < ics2->rank)
9681 return 1;
9682 else if (ics2->rank < ics1->rank)
9683 return -1;
9684
9685 to_type1 = ics1->type;
9686 to_type2 = ics2->type;
9687
9688 /* A conversion from scalar arithmetic type to complex is worse than a
9689 conversion between scalar arithmetic types. */
9690 if (same_type_p (from_type1, from_type2)
9691 && ARITHMETIC_TYPE_P (from_type1)
9692 && ARITHMETIC_TYPE_P (to_type1)
9693 && ARITHMETIC_TYPE_P (to_type2)
9694 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
9695 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
9696 {
9697 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
9698 return -1;
9699 else
9700 return 1;
9701 }
9702
9703 if (TYPE_PTR_P (from_type1)
9704 && TYPE_PTR_P (from_type2)
9705 && TYPE_PTR_P (to_type1)
9706 && TYPE_PTR_P (to_type2))
9707 {
9708 deref_from_type1 = TREE_TYPE (from_type1);
9709 deref_from_type2 = TREE_TYPE (from_type2);
9710 deref_to_type1 = TREE_TYPE (to_type1);
9711 deref_to_type2 = TREE_TYPE (to_type2);
9712 }
9713 /* The rules for pointers to members A::* are just like the rules
9714 for pointers A*, except opposite: if B is derived from A then
9715 A::* converts to B::*, not vice versa. For that reason, we
9716 switch the from_ and to_ variables here. */
9717 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
9718 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
9719 || (TYPE_PTRMEMFUNC_P (from_type1)
9720 && TYPE_PTRMEMFUNC_P (from_type2)
9721 && TYPE_PTRMEMFUNC_P (to_type1)
9722 && TYPE_PTRMEMFUNC_P (to_type2)))
9723 {
9724 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
9725 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
9726 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
9727 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
9728 }
9729
9730 if (deref_from_type1 != NULL_TREE
9731 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
9732 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
9733 {
9734 /* This was one of the pointer or pointer-like conversions.
9735
9736 [over.ics.rank]
9737
9738 --If class B is derived directly or indirectly from class A,
9739 conversion of B* to A* is better than conversion of B* to
9740 void*, and conversion of A* to void* is better than
9741 conversion of B* to void*. */
9742 if (VOID_TYPE_P (deref_to_type1)
9743 && VOID_TYPE_P (deref_to_type2))
9744 {
9745 if (is_properly_derived_from (deref_from_type1,
9746 deref_from_type2))
9747 return -1;
9748 else if (is_properly_derived_from (deref_from_type2,
9749 deref_from_type1))
9750 return 1;
9751 }
9752 else if (VOID_TYPE_P (deref_to_type1)
9753 || VOID_TYPE_P (deref_to_type2))
9754 {
9755 if (same_type_p (deref_from_type1, deref_from_type2))
9756 {
9757 if (VOID_TYPE_P (deref_to_type2))
9758 {
9759 if (is_properly_derived_from (deref_from_type1,
9760 deref_to_type1))
9761 return 1;
9762 }
9763 /* We know that DEREF_TO_TYPE1 is `void' here. */
9764 else if (is_properly_derived_from (deref_from_type1,
9765 deref_to_type2))
9766 return -1;
9767 }
9768 }
9769 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
9770 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
9771 {
9772 /* [over.ics.rank]
9773
9774 --If class B is derived directly or indirectly from class A
9775 and class C is derived directly or indirectly from B,
9776
9777 --conversion of C* to B* is better than conversion of C* to
9778 A*,
9779
9780 --conversion of B* to A* is better than conversion of C* to
9781 A* */
9782 if (same_type_p (deref_from_type1, deref_from_type2))
9783 {
9784 if (is_properly_derived_from (deref_to_type1,
9785 deref_to_type2))
9786 return 1;
9787 else if (is_properly_derived_from (deref_to_type2,
9788 deref_to_type1))
9789 return -1;
9790 }
9791 else if (same_type_p (deref_to_type1, deref_to_type2))
9792 {
9793 if (is_properly_derived_from (deref_from_type2,
9794 deref_from_type1))
9795 return 1;
9796 else if (is_properly_derived_from (deref_from_type1,
9797 deref_from_type2))
9798 return -1;
9799 }
9800 }
9801 }
9802 else if (CLASS_TYPE_P (non_reference (from_type1))
9803 && same_type_p (from_type1, from_type2))
9804 {
9805 tree from = non_reference (from_type1);
9806
9807 /* [over.ics.rank]
9808
9809 --binding of an expression of type C to a reference of type
9810 B& is better than binding an expression of type C to a
9811 reference of type A&
9812
9813 --conversion of C to B is better than conversion of C to A, */
9814 if (is_properly_derived_from (from, to_type1)
9815 && is_properly_derived_from (from, to_type2))
9816 {
9817 if (is_properly_derived_from (to_type1, to_type2))
9818 return 1;
9819 else if (is_properly_derived_from (to_type2, to_type1))
9820 return -1;
9821 }
9822 }
9823 else if (CLASS_TYPE_P (non_reference (to_type1))
9824 && same_type_p (to_type1, to_type2))
9825 {
9826 tree to = non_reference (to_type1);
9827
9828 /* [over.ics.rank]
9829
9830 --binding of an expression of type B to a reference of type
9831 A& is better than binding an expression of type C to a
9832 reference of type A&,
9833
9834 --conversion of B to A is better than conversion of C to A */
9835 if (is_properly_derived_from (from_type1, to)
9836 && is_properly_derived_from (from_type2, to))
9837 {
9838 if (is_properly_derived_from (from_type2, from_type1))
9839 return 1;
9840 else if (is_properly_derived_from (from_type1, from_type2))
9841 return -1;
9842 }
9843 }
9844
9845 /* [over.ics.rank]
9846
9847 --S1 and S2 differ only in their qualification conversion and yield
9848 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
9849 qualification signature of type T1 is a proper subset of the cv-
9850 qualification signature of type T2 */
9851 if (ics1->kind == ck_qual
9852 && ics2->kind == ck_qual
9853 && same_type_p (from_type1, from_type2))
9854 {
9855 int result = comp_cv_qual_signature (to_type1, to_type2);
9856 if (result != 0)
9857 return result;
9858 }
9859
9860 /* [over.ics.rank]
9861
9862 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
9863 to an implicit object parameter of a non-static member function
9864 declared without a ref-qualifier, and either S1 binds an lvalue
9865 reference to an lvalue and S2 binds an rvalue reference or S1 binds an
9866 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
9867 draft standard, 13.3.3.2)
9868
9869 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
9870 types to which the references refer are the same type except for
9871 top-level cv-qualifiers, and the type to which the reference
9872 initialized by S2 refers is more cv-qualified than the type to
9873 which the reference initialized by S1 refers.
9874
9875 DR 1328 [over.match.best]: the context is an initialization by
9876 conversion function for direct reference binding (13.3.1.6) of a
9877 reference to function type, the return type of F1 is the same kind of
9878 reference (i.e. lvalue or rvalue) as the reference being initialized,
9879 and the return type of F2 is not. */
9880
9881 if (ref_conv1 && ref_conv2)
9882 {
9883 if (!ref_conv1->this_p && !ref_conv2->this_p
9884 && (ref_conv1->rvaluedness_matches_p
9885 != ref_conv2->rvaluedness_matches_p)
9886 && (same_type_p (ref_conv1->type, ref_conv2->type)
9887 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
9888 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
9889 {
9890 if (ref_conv1->bad_p
9891 && !same_type_p (TREE_TYPE (ref_conv1->type),
9892 TREE_TYPE (ref_conv2->type)))
9893 /* Don't prefer a bad conversion that drops cv-quals to a bad
9894 conversion with the wrong rvalueness. */
9895 return 0;
9896 return (ref_conv1->rvaluedness_matches_p
9897 - ref_conv2->rvaluedness_matches_p);
9898 }
9899
9900 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
9901 {
9902 int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
9903 int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
9904 if (ref_conv1->bad_p)
9905 {
9906 /* Prefer the one that drops fewer cv-quals. */
9907 tree ftype = next_conversion (ref_conv1)->type;
9908 int fquals = cp_type_quals (ftype);
9909 q1 ^= fquals;
9910 q2 ^= fquals;
9911 }
9912 return comp_cv_qualification (q2, q1);
9913 }
9914 }
9915
9916 /* Neither conversion sequence is better than the other. */
9917 return 0;
9918 }
9919
9920 /* The source type for this standard conversion sequence. */
9921
9922 static tree
9923 source_type (conversion *t)
9924 {
9925 for (;; t = next_conversion (t))
9926 {
9927 if (t->kind == ck_user
9928 || t->kind == ck_ambig
9929 || t->kind == ck_identity)
9930 return t->type;
9931 }
9932 gcc_unreachable ();
9933 }
9934
9935 /* Note a warning about preferring WINNER to LOSER. We do this by storing
9936 a pointer to LOSER and re-running joust to produce the warning if WINNER
9937 is actually used. */
9938
9939 static void
9940 add_warning (struct z_candidate *winner, struct z_candidate *loser)
9941 {
9942 candidate_warning *cw = (candidate_warning *)
9943 conversion_obstack_alloc (sizeof (candidate_warning));
9944 cw->loser = loser;
9945 cw->next = winner->warnings;
9946 winner->warnings = cw;
9947 }
9948
9949 /* Compare two candidates for overloading as described in
9950 [over.match.best]. Return values:
9951
9952 1: cand1 is better than cand2
9953 -1: cand2 is better than cand1
9954 0: cand1 and cand2 are indistinguishable */
9955
9956 static int
9957 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
9958 tsubst_flags_t complain)
9959 {
9960 int winner = 0;
9961 int off1 = 0, off2 = 0;
9962 size_t i;
9963 size_t len;
9964
9965 /* Candidates that involve bad conversions are always worse than those
9966 that don't. */
9967 if (cand1->viable > cand2->viable)
9968 return 1;
9969 if (cand1->viable < cand2->viable)
9970 return -1;
9971
9972 /* If we have two pseudo-candidates for conversions to the same type,
9973 or two candidates for the same function, arbitrarily pick one. */
9974 if (cand1->fn == cand2->fn
9975 && (IS_TYPE_OR_DECL_P (cand1->fn)))
9976 return 1;
9977
9978 /* Prefer a non-deleted function over an implicitly deleted move
9979 constructor or assignment operator. This differs slightly from the
9980 wording for issue 1402 (which says the move op is ignored by overload
9981 resolution), but this way produces better error messages. */
9982 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
9983 && TREE_CODE (cand2->fn) == FUNCTION_DECL
9984 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
9985 {
9986 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
9987 && move_fn_p (cand1->fn))
9988 return -1;
9989 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
9990 && move_fn_p (cand2->fn))
9991 return 1;
9992 }
9993
9994 /* a viable function F1
9995 is defined to be a better function than another viable function F2 if
9996 for all arguments i, ICSi(F1) is not a worse conversion sequence than
9997 ICSi(F2), and then */
9998
9999 /* for some argument j, ICSj(F1) is a better conversion sequence than
10000 ICSj(F2) */
10001
10002 /* For comparing static and non-static member functions, we ignore
10003 the implicit object parameter of the non-static function. The
10004 standard says to pretend that the static function has an object
10005 parm, but that won't work with operator overloading. */
10006 len = cand1->num_convs;
10007 if (len != cand2->num_convs)
10008 {
10009 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
10010 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
10011
10012 if (DECL_CONSTRUCTOR_P (cand1->fn)
10013 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
10014 /* We're comparing a near-match list constructor and a near-match
10015 non-list constructor. Just treat them as unordered. */
10016 return 0;
10017
10018 gcc_assert (static_1 != static_2);
10019
10020 if (static_1)
10021 off2 = 1;
10022 else
10023 {
10024 off1 = 1;
10025 --len;
10026 }
10027 }
10028
10029 for (i = 0; i < len; ++i)
10030 {
10031 conversion *t1 = cand1->convs[i + off1];
10032 conversion *t2 = cand2->convs[i + off2];
10033 int comp = compare_ics (t1, t2);
10034
10035 if (comp != 0)
10036 {
10037 if ((complain & tf_warning)
10038 && warn_sign_promo
10039 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
10040 == cr_std + cr_promotion)
10041 && t1->kind == ck_std
10042 && t2->kind == ck_std
10043 && TREE_CODE (t1->type) == INTEGER_TYPE
10044 && TREE_CODE (t2->type) == INTEGER_TYPE
10045 && (TYPE_PRECISION (t1->type)
10046 == TYPE_PRECISION (t2->type))
10047 && (TYPE_UNSIGNED (next_conversion (t1)->type)
10048 || (TREE_CODE (next_conversion (t1)->type)
10049 == ENUMERAL_TYPE)))
10050 {
10051 tree type = next_conversion (t1)->type;
10052 tree type1, type2;
10053 struct z_candidate *w, *l;
10054 if (comp > 0)
10055 type1 = t1->type, type2 = t2->type,
10056 w = cand1, l = cand2;
10057 else
10058 type1 = t2->type, type2 = t1->type,
10059 w = cand2, l = cand1;
10060
10061 if (warn)
10062 {
10063 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
10064 type, type1, type2);
10065 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
10066 }
10067 else
10068 add_warning (w, l);
10069 }
10070
10071 if (winner && comp != winner)
10072 {
10073 winner = 0;
10074 goto tweak;
10075 }
10076 winner = comp;
10077 }
10078 }
10079
10080 /* warn about confusing overload resolution for user-defined conversions,
10081 either between a constructor and a conversion op, or between two
10082 conversion ops. */
10083 if ((complain & tf_warning)
10084 && winner && warn_conversion && cand1->second_conv
10085 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
10086 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
10087 {
10088 struct z_candidate *w, *l;
10089 bool give_warning = false;
10090
10091 if (winner == 1)
10092 w = cand1, l = cand2;
10093 else
10094 w = cand2, l = cand1;
10095
10096 /* We don't want to complain about `X::operator T1 ()'
10097 beating `X::operator T2 () const', when T2 is a no less
10098 cv-qualified version of T1. */
10099 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
10100 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
10101 {
10102 tree t = TREE_TYPE (TREE_TYPE (l->fn));
10103 tree f = TREE_TYPE (TREE_TYPE (w->fn));
10104
10105 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
10106 {
10107 t = TREE_TYPE (t);
10108 f = TREE_TYPE (f);
10109 }
10110 if (!comp_ptr_ttypes (t, f))
10111 give_warning = true;
10112 }
10113 else
10114 give_warning = true;
10115
10116 if (!give_warning)
10117 /*NOP*/;
10118 else if (warn)
10119 {
10120 tree source = source_type (w->convs[0]);
10121 if (! DECL_CONSTRUCTOR_P (w->fn))
10122 source = TREE_TYPE (source);
10123 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
10124 && warning (OPT_Wconversion, " for conversion from %qH to %qI",
10125 source, w->second_conv->type))
10126 {
10127 inform (input_location, " because conversion sequence for the argument is better");
10128 }
10129 }
10130 else
10131 add_warning (w, l);
10132 }
10133
10134 if (winner)
10135 return winner;
10136
10137 /* DR 495 moved this tiebreaker above the template ones. */
10138 /* or, if not that,
10139 the context is an initialization by user-defined conversion (see
10140 _dcl.init_ and _over.match.user_) and the standard conversion
10141 sequence from the return type of F1 to the destination type (i.e.,
10142 the type of the entity being initialized) is a better conversion
10143 sequence than the standard conversion sequence from the return type
10144 of F2 to the destination type. */
10145
10146 if (cand1->second_conv)
10147 {
10148 winner = compare_ics (cand1->second_conv, cand2->second_conv);
10149 if (winner)
10150 return winner;
10151 }
10152
10153 /* or, if not that,
10154 F1 is a non-template function and F2 is a template function
10155 specialization. */
10156
10157 if (!cand1->template_decl && cand2->template_decl)
10158 return 1;
10159 else if (cand1->template_decl && !cand2->template_decl)
10160 return -1;
10161
10162 /* or, if not that,
10163 F1 and F2 are template functions and the function template for F1 is
10164 more specialized than the template for F2 according to the partial
10165 ordering rules. */
10166
10167 if (cand1->template_decl && cand2->template_decl)
10168 {
10169 winner = more_specialized_fn
10170 (TI_TEMPLATE (cand1->template_decl),
10171 TI_TEMPLATE (cand2->template_decl),
10172 /* [temp.func.order]: The presence of unused ellipsis and default
10173 arguments has no effect on the partial ordering of function
10174 templates. add_function_candidate() will not have
10175 counted the "this" argument for constructors. */
10176 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
10177 if (winner)
10178 return winner;
10179 }
10180
10181 // C++ Concepts
10182 // or, if not that, F1 is more constrained than F2.
10183 if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn))
10184 {
10185 winner = more_constrained (cand1->fn, cand2->fn);
10186 if (winner)
10187 return winner;
10188 }
10189
10190 /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
10191 if (deduction_guide_p (cand1->fn))
10192 {
10193 gcc_assert (deduction_guide_p (cand2->fn));
10194 /* We distinguish between candidates from an explicit deduction guide and
10195 candidates built from a constructor based on DECL_ARTIFICIAL. */
10196 int art1 = DECL_ARTIFICIAL (cand1->fn);
10197 int art2 = DECL_ARTIFICIAL (cand2->fn);
10198 if (art1 != art2)
10199 return art2 - art1;
10200
10201 if (art1)
10202 {
10203 /* Prefer the special copy guide over a declared copy/move
10204 constructor. */
10205 if (copy_guide_p (cand1->fn))
10206 return 1;
10207 if (copy_guide_p (cand2->fn))
10208 return -1;
10209
10210 /* Prefer a candidate generated from a non-template constructor. */
10211 int tg1 = template_guide_p (cand1->fn);
10212 int tg2 = template_guide_p (cand2->fn);
10213 if (tg1 != tg2)
10214 return tg2 - tg1;
10215 }
10216 }
10217
10218 /* F1 is a member of a class D, F2 is a member of a base class B of D, and
10219 for all arguments the corresponding parameters of F1 and F2 have the same
10220 type (CWG 2273/2277). */
10221 if (DECL_P (cand1->fn) && DECL_CLASS_SCOPE_P (cand1->fn)
10222 && !DECL_CONV_FN_P (cand1->fn)
10223 && DECL_P (cand2->fn) && DECL_CLASS_SCOPE_P (cand2->fn)
10224 && !DECL_CONV_FN_P (cand2->fn))
10225 {
10226 tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
10227 tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
10228
10229 bool used1 = false;
10230 bool used2 = false;
10231 if (base1 == base2)
10232 /* No difference. */;
10233 else if (DERIVED_FROM_P (base1, base2))
10234 used1 = true;
10235 else if (DERIVED_FROM_P (base2, base1))
10236 used2 = true;
10237
10238 if (int diff = used2 - used1)
10239 {
10240 for (i = 0; i < len; ++i)
10241 {
10242 conversion *t1 = cand1->convs[i + off1];
10243 conversion *t2 = cand2->convs[i + off2];
10244 if (!same_type_p (t1->type, t2->type))
10245 break;
10246 }
10247 if (i == len)
10248 return diff;
10249 }
10250 }
10251
10252 /* Check whether we can discard a builtin candidate, either because we
10253 have two identical ones or matching builtin and non-builtin candidates.
10254
10255 (Pedantically in the latter case the builtin which matched the user
10256 function should not be added to the overload set, but we spot it here.
10257
10258 [over.match.oper]
10259 ... the builtin candidates include ...
10260 - do not have the same parameter type list as any non-template
10261 non-member candidate. */
10262
10263 if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
10264 {
10265 for (i = 0; i < len; ++i)
10266 if (!same_type_p (cand1->convs[i]->type,
10267 cand2->convs[i]->type))
10268 break;
10269 if (i == cand1->num_convs)
10270 {
10271 if (cand1->fn == cand2->fn)
10272 /* Two built-in candidates; arbitrarily pick one. */
10273 return 1;
10274 else if (identifier_p (cand1->fn))
10275 /* cand1 is built-in; prefer cand2. */
10276 return -1;
10277 else
10278 /* cand2 is built-in; prefer cand1. */
10279 return 1;
10280 }
10281 }
10282
10283 /* For candidates of a multi-versioned function, make the version with
10284 the highest priority win. This version will be checked for dispatching
10285 first. If this version can be inlined into the caller, the front-end
10286 will simply make a direct call to this function. */
10287
10288 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
10289 && DECL_FUNCTION_VERSIONED (cand1->fn)
10290 && TREE_CODE (cand2->fn) == FUNCTION_DECL
10291 && DECL_FUNCTION_VERSIONED (cand2->fn))
10292 {
10293 tree f1 = TREE_TYPE (cand1->fn);
10294 tree f2 = TREE_TYPE (cand2->fn);
10295 tree p1 = TYPE_ARG_TYPES (f1);
10296 tree p2 = TYPE_ARG_TYPES (f2);
10297
10298 /* Check if cand1->fn and cand2->fn are versions of the same function. It
10299 is possible that cand1->fn and cand2->fn are function versions but of
10300 different functions. Check types to see if they are versions of the same
10301 function. */
10302 if (compparms (p1, p2)
10303 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
10304 {
10305 /* Always make the version with the higher priority, more
10306 specialized, win. */
10307 gcc_assert (targetm.compare_version_priority);
10308 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
10309 return 1;
10310 else
10311 return -1;
10312 }
10313 }
10314
10315 /* If the two function declarations represent the same function (this can
10316 happen with declarations in multiple scopes and arg-dependent lookup),
10317 arbitrarily choose one. But first make sure the default args we're
10318 using match. */
10319 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
10320 && equal_functions (cand1->fn, cand2->fn))
10321 {
10322 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
10323 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
10324
10325 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
10326
10327 for (i = 0; i < len; ++i)
10328 {
10329 /* Don't crash if the fn is variadic. */
10330 if (!parms1)
10331 break;
10332 parms1 = TREE_CHAIN (parms1);
10333 parms2 = TREE_CHAIN (parms2);
10334 }
10335
10336 if (off1)
10337 parms1 = TREE_CHAIN (parms1);
10338 else if (off2)
10339 parms2 = TREE_CHAIN (parms2);
10340
10341 for (; parms1; ++i)
10342 {
10343 if (!cp_tree_equal (TREE_PURPOSE (parms1),
10344 TREE_PURPOSE (parms2)))
10345 {
10346 if (warn)
10347 {
10348 if (complain & tf_error)
10349 {
10350 if (permerror (input_location,
10351 "default argument mismatch in "
10352 "overload resolution"))
10353 {
10354 inform (DECL_SOURCE_LOCATION (cand1->fn),
10355 " candidate 1: %q#F", cand1->fn);
10356 inform (DECL_SOURCE_LOCATION (cand2->fn),
10357 " candidate 2: %q#F", cand2->fn);
10358 }
10359 }
10360 else
10361 return 0;
10362 }
10363 else
10364 add_warning (cand1, cand2);
10365 break;
10366 }
10367 parms1 = TREE_CHAIN (parms1);
10368 parms2 = TREE_CHAIN (parms2);
10369 }
10370
10371 return 1;
10372 }
10373
10374 tweak:
10375
10376 /* Extension: If the worst conversion for one candidate is worse than the
10377 worst conversion for the other, take the first. */
10378 if (!pedantic && (complain & tf_warning_or_error))
10379 {
10380 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
10381 struct z_candidate *w = 0, *l = 0;
10382
10383 for (i = 0; i < len; ++i)
10384 {
10385 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
10386 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
10387 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
10388 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
10389 }
10390 if (rank1 < rank2)
10391 winner = 1, w = cand1, l = cand2;
10392 if (rank1 > rank2)
10393 winner = -1, w = cand2, l = cand1;
10394 if (winner)
10395 {
10396 /* Don't choose a deleted function over ambiguity. */
10397 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
10398 return 0;
10399 if (warn)
10400 {
10401 pedwarn (input_location, 0,
10402 "ISO C++ says that these are ambiguous, even "
10403 "though the worst conversion for the first is better than "
10404 "the worst conversion for the second:");
10405 print_z_candidate (input_location, _("candidate 1:"), w);
10406 print_z_candidate (input_location, _("candidate 2:"), l);
10407 }
10408 else
10409 add_warning (w, l);
10410 return winner;
10411 }
10412 }
10413
10414 gcc_assert (!winner);
10415 return 0;
10416 }
10417
10418 /* Given a list of candidates for overloading, find the best one, if any.
10419 This algorithm has a worst case of O(2n) (winner is last), and a best
10420 case of O(n/2) (totally ambiguous); much better than a sorting
10421 algorithm. */
10422
10423 static struct z_candidate *
10424 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
10425 {
10426 struct z_candidate *champ = candidates, *challenger;
10427 int fate;
10428 int champ_compared_to_predecessor = 0;
10429
10430 /* Walk through the list once, comparing each current champ to the next
10431 candidate, knocking out a candidate or two with each comparison. */
10432
10433 for (challenger = champ->next; challenger; )
10434 {
10435 fate = joust (champ, challenger, 0, complain);
10436 if (fate == 1)
10437 challenger = challenger->next;
10438 else
10439 {
10440 if (fate == 0)
10441 {
10442 champ = challenger->next;
10443 if (champ == 0)
10444 return NULL;
10445 champ_compared_to_predecessor = 0;
10446 }
10447 else
10448 {
10449 champ = challenger;
10450 champ_compared_to_predecessor = 1;
10451 }
10452
10453 challenger = champ->next;
10454 }
10455 }
10456
10457 /* Make sure the champ is better than all the candidates it hasn't yet
10458 been compared to. */
10459
10460 for (challenger = candidates;
10461 challenger != champ
10462 && !(champ_compared_to_predecessor && challenger->next == champ);
10463 challenger = challenger->next)
10464 {
10465 fate = joust (champ, challenger, 0, complain);
10466 if (fate != 1)
10467 return NULL;
10468 }
10469
10470 return champ;
10471 }
10472
10473 /* Returns nonzero if things of type FROM can be converted to TO. */
10474
10475 bool
10476 can_convert (tree to, tree from, tsubst_flags_t complain)
10477 {
10478 tree arg = NULL_TREE;
10479 /* implicit_conversion only considers user-defined conversions
10480 if it has an expression for the call argument list. */
10481 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
10482 arg = build1 (CAST_EXPR, from, NULL_TREE);
10483 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
10484 }
10485
10486 /* Returns nonzero if things of type FROM can be converted to TO with a
10487 standard conversion. */
10488
10489 bool
10490 can_convert_standard (tree to, tree from, tsubst_flags_t complain)
10491 {
10492 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
10493 }
10494
10495 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
10496
10497 bool
10498 can_convert_arg (tree to, tree from, tree arg, int flags,
10499 tsubst_flags_t complain)
10500 {
10501 conversion *t;
10502 void *p;
10503 bool ok_p;
10504
10505 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10506 p = conversion_obstack_alloc (0);
10507 /* We want to discard any access checks done for this test,
10508 as we might not be in the appropriate access context and
10509 we'll do the check again when we actually perform the
10510 conversion. */
10511 push_deferring_access_checks (dk_deferred);
10512
10513 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
10514 flags, complain);
10515 ok_p = (t && !t->bad_p);
10516
10517 /* Discard the access checks now. */
10518 pop_deferring_access_checks ();
10519 /* Free all the conversions we allocated. */
10520 obstack_free (&conversion_obstack, p);
10521
10522 return ok_p;
10523 }
10524
10525 /* Like can_convert_arg, but allows dubious conversions as well. */
10526
10527 bool
10528 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
10529 tsubst_flags_t complain)
10530 {
10531 conversion *t;
10532 void *p;
10533
10534 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10535 p = conversion_obstack_alloc (0);
10536 /* Try to perform the conversion. */
10537 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
10538 flags, complain);
10539 /* Free all the conversions we allocated. */
10540 obstack_free (&conversion_obstack, p);
10541
10542 return t != NULL;
10543 }
10544
10545 /* Convert EXPR to TYPE. Return the converted expression.
10546
10547 Note that we allow bad conversions here because by the time we get to
10548 this point we are committed to doing the conversion. If we end up
10549 doing a bad conversion, convert_like will complain. */
10550
10551 tree
10552 perform_implicit_conversion_flags (tree type, tree expr,
10553 tsubst_flags_t complain, int flags)
10554 {
10555 conversion *conv;
10556 void *p;
10557 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
10558
10559 if (error_operand_p (expr))
10560 return error_mark_node;
10561
10562 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10563 p = conversion_obstack_alloc (0);
10564
10565 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10566 /*c_cast_p=*/false,
10567 flags, complain);
10568
10569 if (!conv)
10570 {
10571 if (complain & tf_error)
10572 {
10573 /* If expr has unknown type, then it is an overloaded function.
10574 Call instantiate_type to get good error messages. */
10575 if (TREE_TYPE (expr) == unknown_type_node)
10576 instantiate_type (type, expr, complain);
10577 else if (invalid_nonstatic_memfn_p (loc, expr, complain))
10578 /* We gave an error. */;
10579 else
10580 error_at (loc, "could not convert %qE from %qH to %qI", expr,
10581 TREE_TYPE (expr), type);
10582 }
10583 expr = error_mark_node;
10584 }
10585 else if (processing_template_decl && conv->kind != ck_identity)
10586 {
10587 /* In a template, we are only concerned about determining the
10588 type of non-dependent expressions, so we do not have to
10589 perform the actual conversion. But for initializers, we
10590 need to be able to perform it at instantiation
10591 (or instantiate_non_dependent_expr) time. */
10592 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
10593 if (!(flags & LOOKUP_ONLYCONVERTING))
10594 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
10595 }
10596 else
10597 expr = convert_like (conv, expr, complain);
10598
10599 /* Free all the conversions we allocated. */
10600 obstack_free (&conversion_obstack, p);
10601
10602 return expr;
10603 }
10604
10605 tree
10606 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
10607 {
10608 return perform_implicit_conversion_flags (type, expr, complain,
10609 LOOKUP_IMPLICIT);
10610 }
10611
10612 /* Convert EXPR to TYPE (as a direct-initialization) if that is
10613 permitted. If the conversion is valid, the converted expression is
10614 returned. Otherwise, NULL_TREE is returned, except in the case
10615 that TYPE is a class type; in that case, an error is issued. If
10616 C_CAST_P is true, then this direct-initialization is taking
10617 place as part of a static_cast being attempted as part of a C-style
10618 cast. */
10619
10620 tree
10621 perform_direct_initialization_if_possible (tree type,
10622 tree expr,
10623 bool c_cast_p,
10624 tsubst_flags_t complain)
10625 {
10626 conversion *conv;
10627 void *p;
10628
10629 if (type == error_mark_node || error_operand_p (expr))
10630 return error_mark_node;
10631 /* [dcl.init]
10632
10633 If the destination type is a (possibly cv-qualified) class type:
10634
10635 -- If the initialization is direct-initialization ...,
10636 constructors are considered. ... If no constructor applies, or
10637 the overload resolution is ambiguous, the initialization is
10638 ill-formed. */
10639 if (CLASS_TYPE_P (type))
10640 {
10641 vec<tree, va_gc> *args = make_tree_vector_single (expr);
10642 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
10643 &args, type, LOOKUP_NORMAL, complain);
10644 release_tree_vector (args);
10645 return build_cplus_new (type, expr, complain);
10646 }
10647
10648 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10649 p = conversion_obstack_alloc (0);
10650
10651 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10652 c_cast_p,
10653 LOOKUP_NORMAL, complain);
10654 if (!conv || conv->bad_p)
10655 expr = NULL_TREE;
10656 else
10657 expr = convert_like_real (conv, expr, NULL_TREE, 0,
10658 /*issue_conversion_warnings=*/false,
10659 c_cast_p,
10660 complain);
10661
10662 /* Free all the conversions we allocated. */
10663 obstack_free (&conversion_obstack, p);
10664
10665 return expr;
10666 }
10667
10668 /* When initializing a reference that lasts longer than a full-expression,
10669 this special rule applies:
10670
10671 [class.temporary]
10672
10673 The temporary to which the reference is bound or the temporary
10674 that is the complete object to which the reference is bound
10675 persists for the lifetime of the reference.
10676
10677 The temporaries created during the evaluation of the expression
10678 initializing the reference, except the temporary to which the
10679 reference is bound, are destroyed at the end of the
10680 full-expression in which they are created.
10681
10682 In that case, we store the converted expression into a new
10683 VAR_DECL in a new scope.
10684
10685 However, we want to be careful not to create temporaries when
10686 they are not required. For example, given:
10687
10688 struct B {};
10689 struct D : public B {};
10690 D f();
10691 const B& b = f();
10692
10693 there is no need to copy the return value from "f"; we can just
10694 extend its lifetime. Similarly, given:
10695
10696 struct S {};
10697 struct T { operator S(); };
10698 T t;
10699 const S& s = t;
10700
10701 we can extend the lifetime of the return value of the conversion
10702 operator.
10703
10704 The next several functions are involved in this lifetime extension. */
10705
10706 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
10707 reference is being bound to a temporary. Create and return a new
10708 VAR_DECL with the indicated TYPE; this variable will store the value to
10709 which the reference is bound. */
10710
10711 tree
10712 make_temporary_var_for_ref_to_temp (tree decl, tree type)
10713 {
10714 tree var = create_temporary_var (type);
10715
10716 /* Register the variable. */
10717 if (VAR_P (decl)
10718 && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
10719 {
10720 /* Namespace-scope or local static; give it a mangled name. */
10721 /* FIXME share comdat with decl? */
10722
10723 TREE_STATIC (var) = TREE_STATIC (decl);
10724 CP_DECL_THREAD_LOCAL_P (var) = CP_DECL_THREAD_LOCAL_P (decl);
10725 set_decl_tls_model (var, DECL_TLS_MODEL (decl));
10726
10727 tree name = mangle_ref_init_variable (decl);
10728 DECL_NAME (var) = name;
10729 SET_DECL_ASSEMBLER_NAME (var, name);
10730
10731 var = pushdecl (var);
10732 }
10733 else
10734 /* Create a new cleanup level if necessary. */
10735 maybe_push_cleanup_level (type);
10736
10737 return var;
10738 }
10739
10740 /* EXPR is the initializer for a variable DECL of reference or
10741 std::initializer_list type. Create, push and return a new VAR_DECL
10742 for the initializer so that it will live as long as DECL. Any
10743 cleanup for the new variable is returned through CLEANUP, and the
10744 code to initialize the new variable is returned through INITP. */
10745
10746 static tree
10747 set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
10748 tree *initp)
10749 {
10750 tree init;
10751 tree type;
10752 tree var;
10753
10754 /* Create the temporary variable. */
10755 type = TREE_TYPE (expr);
10756 var = make_temporary_var_for_ref_to_temp (decl, type);
10757 layout_decl (var, 0);
10758 /* If the rvalue is the result of a function call it will be
10759 a TARGET_EXPR. If it is some other construct (such as a
10760 member access expression where the underlying object is
10761 itself the result of a function call), turn it into a
10762 TARGET_EXPR here. It is important that EXPR be a
10763 TARGET_EXPR below since otherwise the INIT_EXPR will
10764 attempt to make a bitwise copy of EXPR to initialize
10765 VAR. */
10766 if (TREE_CODE (expr) != TARGET_EXPR)
10767 expr = get_target_expr (expr);
10768
10769 if (TREE_CODE (decl) == FIELD_DECL
10770 && extra_warnings && !TREE_NO_WARNING (decl))
10771 {
10772 warning (OPT_Wextra, "a temporary bound to %qD only persists "
10773 "until the constructor exits", decl);
10774 TREE_NO_WARNING (decl) = true;
10775 }
10776
10777 /* Recursively extend temps in this initializer. */
10778 TARGET_EXPR_INITIAL (expr)
10779 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups);
10780
10781 /* Any reference temp has a non-trivial initializer. */
10782 DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
10783
10784 /* If the initializer is constant, put it in DECL_INITIAL so we get
10785 static initialization and use in constant expressions. */
10786 init = maybe_constant_init (expr);
10787 if (TREE_CONSTANT (init))
10788 {
10789 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
10790 {
10791 /* 5.19 says that a constant expression can include an
10792 lvalue-rvalue conversion applied to "a glvalue of literal type
10793 that refers to a non-volatile temporary object initialized
10794 with a constant expression". Rather than try to communicate
10795 that this VAR_DECL is a temporary, just mark it constexpr.
10796
10797 Currently this is only useful for initializer_list temporaries,
10798 since reference vars can't appear in constant expressions. */
10799 DECL_DECLARED_CONSTEXPR_P (var) = true;
10800 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
10801 TREE_CONSTANT (var) = true;
10802 }
10803 DECL_INITIAL (var) = init;
10804 init = NULL_TREE;
10805 }
10806 else
10807 /* Create the INIT_EXPR that will initialize the temporary
10808 variable. */
10809 init = split_nonconstant_init (var, expr);
10810 if (at_function_scope_p ())
10811 {
10812 add_decl_expr (var);
10813
10814 if (TREE_STATIC (var))
10815 init = add_stmt_to_compound (init, register_dtor_fn (var));
10816 else
10817 {
10818 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
10819 if (cleanup)
10820 vec_safe_push (*cleanups, cleanup);
10821 }
10822
10823 /* We must be careful to destroy the temporary only
10824 after its initialization has taken place. If the
10825 initialization throws an exception, then the
10826 destructor should not be run. We cannot simply
10827 transform INIT into something like:
10828
10829 (INIT, ({ CLEANUP_STMT; }))
10830
10831 because emit_local_var always treats the
10832 initializer as a full-expression. Thus, the
10833 destructor would run too early; it would run at the
10834 end of initializing the reference variable, rather
10835 than at the end of the block enclosing the
10836 reference variable.
10837
10838 The solution is to pass back a cleanup expression
10839 which the caller is responsible for attaching to
10840 the statement tree. */
10841 }
10842 else
10843 {
10844 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
10845 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
10846 {
10847 if (CP_DECL_THREAD_LOCAL_P (var))
10848 tls_aggregates = tree_cons (NULL_TREE, var,
10849 tls_aggregates);
10850 else
10851 static_aggregates = tree_cons (NULL_TREE, var,
10852 static_aggregates);
10853 }
10854 else
10855 /* Check whether the dtor is callable. */
10856 cxx_maybe_build_cleanup (var, tf_warning_or_error);
10857 }
10858 /* Avoid -Wunused-variable warning (c++/38958). */
10859 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
10860 && VAR_P (decl))
10861 TREE_USED (decl) = DECL_READ_P (decl) = true;
10862
10863 *initp = init;
10864 return var;
10865 }
10866
10867 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
10868 initializing a variable of that TYPE. */
10869
10870 tree
10871 initialize_reference (tree type, tree expr,
10872 int flags, tsubst_flags_t complain)
10873 {
10874 conversion *conv;
10875 void *p;
10876 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
10877
10878 if (type == error_mark_node || error_operand_p (expr))
10879 return error_mark_node;
10880
10881 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10882 p = conversion_obstack_alloc (0);
10883
10884 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
10885 flags, complain);
10886 if (!conv || conv->bad_p)
10887 {
10888 if (complain & tf_error)
10889 {
10890 if (conv)
10891 convert_like (conv, expr, complain);
10892 else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
10893 && !TYPE_REF_IS_RVALUE (type)
10894 && !lvalue_p (expr))
10895 error_at (loc, "invalid initialization of non-const reference of "
10896 "type %qH from an rvalue of type %qI",
10897 type, TREE_TYPE (expr));
10898 else
10899 error_at (loc, "invalid initialization of reference of type "
10900 "%qH from expression of type %qI", type,
10901 TREE_TYPE (expr));
10902 }
10903 return error_mark_node;
10904 }
10905
10906 if (conv->kind == ck_ref_bind)
10907 /* Perform the conversion. */
10908 expr = convert_like (conv, expr, complain);
10909 else if (conv->kind == ck_ambig)
10910 /* We gave an error in build_user_type_conversion_1. */
10911 expr = error_mark_node;
10912 else
10913 gcc_unreachable ();
10914
10915 /* Free all the conversions we allocated. */
10916 obstack_free (&conversion_obstack, p);
10917
10918 return expr;
10919 }
10920
10921 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
10922 which is bound either to a reference or a std::initializer_list. */
10923
10924 static tree
10925 extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups)
10926 {
10927 tree sub = init;
10928 tree *p;
10929 STRIP_NOPS (sub);
10930 if (TREE_CODE (sub) == COMPOUND_EXPR)
10931 {
10932 TREE_OPERAND (sub, 1)
10933 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups);
10934 return init;
10935 }
10936 if (TREE_CODE (sub) != ADDR_EXPR)
10937 return init;
10938 /* Deal with binding to a subobject. */
10939 for (p = &TREE_OPERAND (sub, 0); TREE_CODE (*p) == COMPONENT_REF; )
10940 p = &TREE_OPERAND (*p, 0);
10941 if (TREE_CODE (*p) == TARGET_EXPR)
10942 {
10943 tree subinit = NULL_TREE;
10944 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit);
10945 recompute_tree_invariant_for_addr_expr (sub);
10946 if (init != sub)
10947 init = fold_convert (TREE_TYPE (init), sub);
10948 if (subinit)
10949 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
10950 }
10951 return init;
10952 }
10953
10954 /* INIT is part of the initializer for DECL. If there are any
10955 reference or initializer lists being initialized, extend their
10956 lifetime to match that of DECL. */
10957
10958 tree
10959 extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
10960 {
10961 tree type = TREE_TYPE (init);
10962 if (processing_template_decl)
10963 return init;
10964 if (TREE_CODE (type) == REFERENCE_TYPE)
10965 init = extend_ref_init_temps_1 (decl, init, cleanups);
10966 else
10967 {
10968 tree ctor = init;
10969 if (TREE_CODE (ctor) == TARGET_EXPR)
10970 ctor = TARGET_EXPR_INITIAL (ctor);
10971 if (TREE_CODE (ctor) == CONSTRUCTOR)
10972 {
10973 if (is_std_init_list (type))
10974 {
10975 /* The temporary array underlying a std::initializer_list
10976 is handled like a reference temporary. */
10977 tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
10978 array = extend_ref_init_temps_1 (decl, array, cleanups);
10979 CONSTRUCTOR_ELT (ctor, 0)->value = array;
10980 }
10981 else
10982 {
10983 unsigned i;
10984 constructor_elt *p;
10985 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
10986 FOR_EACH_VEC_SAFE_ELT (elts, i, p)
10987 p->value = extend_ref_init_temps (decl, p->value, cleanups);
10988 }
10989 recompute_constructor_flags (ctor);
10990 if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
10991 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10992 }
10993 }
10994
10995 return init;
10996 }
10997
10998 /* Returns true iff an initializer for TYPE could contain temporaries that
10999 need to be extended because they are bound to references or
11000 std::initializer_list. */
11001
11002 bool
11003 type_has_extended_temps (tree type)
11004 {
11005 type = strip_array_types (type);
11006 if (TREE_CODE (type) == REFERENCE_TYPE)
11007 return true;
11008 if (CLASS_TYPE_P (type))
11009 {
11010 if (is_std_init_list (type))
11011 return true;
11012 for (tree f = next_initializable_field (TYPE_FIELDS (type));
11013 f; f = next_initializable_field (DECL_CHAIN (f)))
11014 if (type_has_extended_temps (TREE_TYPE (f)))
11015 return true;
11016 }
11017 return false;
11018 }
11019
11020 /* Returns true iff TYPE is some variant of std::initializer_list. */
11021
11022 bool
11023 is_std_init_list (tree type)
11024 {
11025 if (!TYPE_P (type))
11026 return false;
11027 if (cxx_dialect == cxx98)
11028 return false;
11029 /* Look through typedefs. */
11030 type = TYPE_MAIN_VARIANT (type);
11031 return (CLASS_TYPE_P (type)
11032 && CP_TYPE_CONTEXT (type) == std_node
11033 && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
11034 }
11035
11036 /* Returns true iff DECL is a list constructor: i.e. a constructor which
11037 will accept an argument list of a single std::initializer_list<T>. */
11038
11039 bool
11040 is_list_ctor (tree decl)
11041 {
11042 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
11043 tree arg;
11044
11045 if (!args || args == void_list_node)
11046 return false;
11047
11048 arg = non_reference (TREE_VALUE (args));
11049 if (!is_std_init_list (arg))
11050 return false;
11051
11052 args = TREE_CHAIN (args);
11053
11054 if (args && args != void_list_node && !TREE_PURPOSE (args))
11055 /* There are more non-defaulted parms. */
11056 return false;
11057
11058 return true;
11059 }
11060
11061 #include "gt-cp-call.h"