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