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