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