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