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