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