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