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