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