* fortran/error.c (show_locus): Remove "In file" from error messages.
[official-gcc.git] / gcc / cp / call.c
blob79a2c4a8346fd996bb95bfef5fbaa9528f0eacf2
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 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) and
5 modified by Brendan Kehoe (brendan@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
25 /* High-level class interface. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "output.h"
34 #include "flags.h"
35 #include "rtl.h"
36 #include "toplev.h"
37 #include "expr.h"
38 #include "diagnostic.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "convert.h"
42 #include "langhooks.h"
44 /* The various kinds of conversion. */
46 typedef enum conversion_kind {
47 ck_identity,
48 ck_lvalue,
49 ck_qual,
50 ck_std,
51 ck_ptr,
52 ck_pmem,
53 ck_base,
54 ck_ref_bind,
55 ck_user,
56 ck_ambig,
57 ck_rvalue
58 } conversion_kind;
60 /* The rank of the conversion. Order of the enumerals matters; better
61 conversions should come earlier in the list. */
63 typedef enum conversion_rank {
64 cr_identity,
65 cr_exact,
66 cr_promotion,
67 cr_std,
68 cr_pbool,
69 cr_user,
70 cr_ellipsis,
71 cr_bad
72 } conversion_rank;
74 /* An implicit conversion sequence, in the sense of [over.best.ics].
75 The first conversion to be performed is at the end of the chain.
76 That conversion is always a cr_identity conversion. */
78 typedef struct conversion conversion;
79 struct conversion {
80 /* The kind of conversion represented by this step. */
81 conversion_kind kind;
82 /* The rank of this conversion. */
83 conversion_rank rank;
84 BOOL_BITFIELD user_conv_p : 1;
85 BOOL_BITFIELD ellipsis_p : 1;
86 BOOL_BITFIELD this_p : 1;
87 BOOL_BITFIELD bad_p : 1;
88 /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
89 temporary should be created to hold the result of the
90 conversion. */
91 BOOL_BITFIELD need_temporary_p : 1;
92 /* If KIND is ck_identity or ck_base_conv, true to indicate that the
93 copy constructor must be accessible, even though it is not being
94 used. */
95 BOOL_BITFIELD check_copy_constructor_p : 1;
96 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
97 from a pointer-to-derived to pointer-to-base is being performed. */
98 BOOL_BITFIELD base_p : 1;
99 /* The type of the expression resulting from the conversion. */
100 tree type;
101 union {
102 /* The next conversion in the chain. Since the conversions are
103 arranged from outermost to innermost, the NEXT conversion will
104 actually be performed before this conversion. This variant is
105 used only when KIND is neither ck_identity nor ck_ambig. */
106 conversion *next;
107 /* The expression at the beginning of the conversion chain. This
108 variant is used only if KIND is ck_identity or ck_ambig. */
109 tree expr;
110 } u;
111 /* The function candidate corresponding to this conversion
112 sequence. This field is only used if KIND is ck_user. */
113 struct z_candidate *cand;
116 #define CONVERSION_RANK(NODE) \
117 ((NODE)->bad_p ? cr_bad \
118 : (NODE)->ellipsis_p ? cr_ellipsis \
119 : (NODE)->user_conv_p ? cr_user \
120 : (NODE)->rank)
122 static struct obstack conversion_obstack;
123 static bool conversion_obstack_initialized;
125 static struct z_candidate * tourney (struct z_candidate *);
126 static int equal_functions (tree, tree);
127 static int joust (struct z_candidate *, struct z_candidate *, bool);
128 static int compare_ics (conversion *, conversion *);
129 static tree build_over_call (struct z_candidate *, int);
130 static tree build_java_interface_fn_ref (tree, tree);
131 #define convert_like(CONV, EXPR) \
132 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \
133 /*issue_conversion_warnings=*/true, \
134 /*c_cast_p=*/false)
135 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
136 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
137 /*issue_conversion_warnings=*/true, \
138 /*c_cast_p=*/false)
139 static tree convert_like_real (conversion *, tree, tree, int, int, bool,
140 bool);
141 static void op_error (enum tree_code, enum tree_code, tree, tree,
142 tree, const char *);
143 static tree build_object_call (tree, tree);
144 static tree resolve_args (tree);
145 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
146 static void print_z_candidate (const char *, struct z_candidate *);
147 static void print_z_candidates (struct z_candidate *);
148 static tree build_this (tree);
149 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
150 static bool any_strictly_viable (struct z_candidate *);
151 static struct z_candidate *add_template_candidate
152 (struct z_candidate **, tree, tree, tree, tree, tree,
153 tree, tree, int, unification_kind_t);
154 static struct z_candidate *add_template_candidate_real
155 (struct z_candidate **, tree, tree, tree, tree, tree,
156 tree, tree, int, tree, unification_kind_t);
157 static struct z_candidate *add_template_conv_candidate
158 (struct z_candidate **, tree, tree, tree, tree, tree, tree);
159 static void add_builtin_candidates
160 (struct z_candidate **, enum tree_code, enum tree_code,
161 tree, tree *, int);
162 static void add_builtin_candidate
163 (struct z_candidate **, enum tree_code, enum tree_code,
164 tree, tree, tree, tree *, tree *, int);
165 static bool is_complete (tree);
166 static void build_builtin_candidate
167 (struct z_candidate **, tree, tree, tree, tree *, tree *,
168 int);
169 static struct z_candidate *add_conv_candidate
170 (struct z_candidate **, tree, tree, tree, tree, tree);
171 static struct z_candidate *add_function_candidate
172 (struct z_candidate **, tree, tree, tree, tree, tree, int);
173 static conversion *implicit_conversion (tree, tree, tree, bool, int);
174 static conversion *standard_conversion (tree, tree, tree, bool, int);
175 static conversion *reference_binding (tree, tree, tree, int);
176 static conversion *build_conv (conversion_kind, tree, conversion *);
177 static bool is_subseq (conversion *, conversion *);
178 static tree maybe_handle_ref_bind (conversion **);
179 static void maybe_handle_implicit_object (conversion **);
180 static struct z_candidate *add_candidate
181 (struct z_candidate **, tree, tree, size_t,
182 conversion **, tree, tree, int);
183 static tree source_type (conversion *);
184 static void add_warning (struct z_candidate *, struct z_candidate *);
185 static bool reference_related_p (tree, tree);
186 static bool reference_compatible_p (tree, tree);
187 static conversion *convert_class_to_reference (tree, tree, tree);
188 static conversion *direct_reference_binding (tree, conversion *);
189 static bool promoted_arithmetic_type_p (tree);
190 static conversion *conditional_conversion (tree, tree);
191 static char *name_as_c_string (tree, tree, bool *);
192 static tree call_builtin_trap (void);
193 static tree prep_operand (tree);
194 static void add_candidates (tree, tree, tree, bool, tree, tree,
195 int, struct z_candidate **);
196 static conversion *merge_conversion_sequences (conversion *, conversion *);
197 static bool magic_varargs_p (tree);
198 typedef void (*diagnostic_fn_t) (const char *, ...) ATTRIBUTE_GCC_CXXDIAG(1,2);
199 static tree build_temp (tree, tree, int, diagnostic_fn_t *);
200 static void check_constructor_callable (tree, tree);
202 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
203 NAME can take many forms... */
205 bool
206 check_dtor_name (tree basetype, tree name)
208 /* Just accept something we've already complained about. */
209 if (name == error_mark_node)
210 return true;
212 if (TREE_CODE (name) == TYPE_DECL)
213 name = TREE_TYPE (name);
214 else if (TYPE_P (name))
215 /* OK */;
216 else if (TREE_CODE (name) == IDENTIFIER_NODE)
218 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
219 || (TREE_CODE (basetype) == ENUMERAL_TYPE
220 && name == TYPE_IDENTIFIER (basetype)))
221 return true;
222 else
223 name = get_type_value (name);
225 else
227 /* In the case of:
229 template <class T> struct S { ~S(); };
230 int i;
231 i.~S();
233 NAME will be a class template. */
234 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
235 return false;
238 if (!name)
239 return false;
240 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
243 /* We want the address of a function or method. We avoid creating a
244 pointer-to-member function. */
246 tree
247 build_addr_func (tree function)
249 tree type = TREE_TYPE (function);
251 /* We have to do these by hand to avoid real pointer to member
252 functions. */
253 if (TREE_CODE (type) == METHOD_TYPE)
255 if (TREE_CODE (function) == OFFSET_REF)
257 tree object = build_address (TREE_OPERAND (function, 0));
258 return get_member_function_from_ptrfunc (&object,
259 TREE_OPERAND (function, 1));
261 function = build_address (function);
263 else
264 function = decay_conversion (function);
266 return function;
269 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
270 POINTER_TYPE to those. Note, pointer to member function types
271 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
273 tree
274 build_call (tree function, tree parms)
276 int is_constructor = 0;
277 int nothrow;
278 tree tmp;
279 tree decl;
280 tree result_type;
281 tree fntype;
283 function = build_addr_func (function);
285 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
286 fntype = TREE_TYPE (TREE_TYPE (function));
287 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
288 || TREE_CODE (fntype) == METHOD_TYPE);
289 result_type = TREE_TYPE (fntype);
291 if (TREE_CODE (function) == ADDR_EXPR
292 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
294 decl = TREE_OPERAND (function, 0);
295 if (!TREE_USED (decl))
297 /* We invoke build_call directly for several library
298 functions. These may have been declared normally if
299 we're building libgcc, so we can't just check
300 DECL_ARTIFICIAL. */
301 gcc_assert (DECL_ARTIFICIAL (decl)
302 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
303 "__", 2));
304 mark_used (decl);
307 else
308 decl = NULL_TREE;
310 /* We check both the decl and the type; a function may be known not to
311 throw without being declared throw(). */
312 nothrow = ((decl && TREE_NOTHROW (decl))
313 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
315 if (decl && TREE_THIS_VOLATILE (decl) && cfun)
316 current_function_returns_abnormally = 1;
318 if (decl && TREE_DEPRECATED (decl))
319 warn_deprecated_use (decl);
320 require_complete_eh_spec_types (fntype, decl);
322 if (decl && DECL_CONSTRUCTOR_P (decl))
323 is_constructor = 1;
325 /* Don't pass empty class objects by value. This is useful
326 for tags in STL, which are used to control overload resolution.
327 We don't need to handle other cases of copying empty classes. */
328 if (! decl || ! DECL_BUILT_IN (decl))
329 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
330 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
331 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
333 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
334 TREE_VALUE (tmp) = build2 (COMPOUND_EXPR, TREE_TYPE (t),
335 TREE_VALUE (tmp), t);
338 function = build3 (CALL_EXPR, result_type, function, parms, NULL_TREE);
339 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
340 TREE_NOTHROW (function) = nothrow;
342 return function;
345 /* Build something of the form ptr->method (args)
346 or object.method (args). This can also build
347 calls to constructors, and find friends.
349 Member functions always take their class variable
350 as a pointer.
352 INSTANCE is a class instance.
354 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
356 PARMS help to figure out what that NAME really refers to.
358 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
359 down to the real instance type to use for access checking. We need this
360 information to get protected accesses correct.
362 FLAGS is the logical disjunction of zero or more LOOKUP_
363 flags. See cp-tree.h for more info.
365 If this is all OK, calls build_function_call with the resolved
366 member function.
368 This function must also handle being called to perform
369 initialization, promotion/coercion of arguments, and
370 instantiation of default parameters.
372 Note that NAME may refer to an instance variable name. If
373 `operator()()' is defined for the type of that field, then we return
374 that result. */
376 /* New overloading code. */
378 typedef struct z_candidate z_candidate;
380 typedef struct candidate_warning candidate_warning;
381 struct candidate_warning {
382 z_candidate *loser;
383 candidate_warning *next;
386 struct z_candidate {
387 /* The FUNCTION_DECL that will be called if this candidate is
388 selected by overload resolution. */
389 tree fn;
390 /* The arguments to use when calling this function. */
391 tree args;
392 /* The implicit conversion sequences for each of the arguments to
393 FN. */
394 conversion **convs;
395 /* The number of implicit conversion sequences. */
396 size_t num_convs;
397 /* If FN is a user-defined conversion, the standard conversion
398 sequence from the type returned by FN to the desired destination
399 type. */
400 conversion *second_conv;
401 int viable;
402 /* If FN is a member function, the binfo indicating the path used to
403 qualify the name of FN at the call site. This path is used to
404 determine whether or not FN is accessible if it is selected by
405 overload resolution. The DECL_CONTEXT of FN will always be a
406 (possibly improper) base of this binfo. */
407 tree access_path;
408 /* If FN is a non-static member function, the binfo indicating the
409 subobject to which the `this' pointer should be converted if FN
410 is selected by overload resolution. The type pointed to the by
411 the `this' pointer must correspond to the most derived class
412 indicated by the CONVERSION_PATH. */
413 tree conversion_path;
414 tree template_decl;
415 candidate_warning *warnings;
416 z_candidate *next;
419 /* Returns true iff T is a null pointer constant in the sense of
420 [conv.ptr]. */
422 bool
423 null_ptr_cst_p (tree t)
425 /* [conv.ptr]
427 A null pointer constant is an integral constant expression
428 (_expr.const_) rvalue of integer type that evaluates to zero. */
429 t = integral_constant_value (t);
430 if (t == null_node)
431 return true;
432 if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t))
434 STRIP_NOPS (t);
435 if (!TREE_CONSTANT_OVERFLOW (t))
436 return true;
438 return false;
441 /* Returns nonzero if PARMLIST consists of only default parms and/or
442 ellipsis. */
444 bool
445 sufficient_parms_p (tree parmlist)
447 for (; parmlist && parmlist != void_list_node;
448 parmlist = TREE_CHAIN (parmlist))
449 if (!TREE_PURPOSE (parmlist))
450 return false;
451 return true;
454 /* Allocate N bytes of memory from the conversion obstack. The memory
455 is zeroed before being returned. */
457 static void *
458 conversion_obstack_alloc (size_t n)
460 void *p;
461 if (!conversion_obstack_initialized)
463 gcc_obstack_init (&conversion_obstack);
464 conversion_obstack_initialized = true;
466 p = obstack_alloc (&conversion_obstack, n);
467 memset (p, 0, n);
468 return p;
471 /* Dynamically allocate a conversion. */
473 static conversion *
474 alloc_conversion (conversion_kind kind)
476 conversion *c;
477 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
478 c->kind = kind;
479 return c;
482 #ifdef ENABLE_CHECKING
484 /* Make sure that all memory on the conversion obstack has been
485 freed. */
487 void
488 validate_conversion_obstack (void)
490 if (conversion_obstack_initialized)
491 gcc_assert ((obstack_next_free (&conversion_obstack)
492 == obstack_base (&conversion_obstack)));
495 #endif /* ENABLE_CHECKING */
497 /* Dynamically allocate an array of N conversions. */
499 static conversion **
500 alloc_conversions (size_t n)
502 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
505 static conversion *
506 build_conv (conversion_kind code, tree type, conversion *from)
508 conversion *t;
509 conversion_rank rank = CONVERSION_RANK (from);
511 /* We can't use buildl1 here because CODE could be USER_CONV, which
512 takes two arguments. In that case, the caller is responsible for
513 filling in the second argument. */
514 t = alloc_conversion (code);
515 t->type = type;
516 t->u.next = from;
518 switch (code)
520 case ck_ptr:
521 case ck_pmem:
522 case ck_base:
523 case ck_std:
524 if (rank < cr_std)
525 rank = cr_std;
526 break;
528 case ck_qual:
529 if (rank < cr_exact)
530 rank = cr_exact;
531 break;
533 default:
534 break;
536 t->rank = rank;
537 t->user_conv_p = (code == ck_user || from->user_conv_p);
538 t->bad_p = from->bad_p;
539 t->base_p = false;
540 return t;
543 /* Build a representation of the identity conversion from EXPR to
544 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
546 static conversion *
547 build_identity_conv (tree type, tree expr)
549 conversion *c;
551 c = alloc_conversion (ck_identity);
552 c->type = type;
553 c->u.expr = expr;
555 return c;
558 /* Converting from EXPR to TYPE was ambiguous in the sense that there
559 were multiple user-defined conversions to accomplish the job.
560 Build a conversion that indicates that ambiguity. */
562 static conversion *
563 build_ambiguous_conv (tree type, tree expr)
565 conversion *c;
567 c = alloc_conversion (ck_ambig);
568 c->type = type;
569 c->u.expr = expr;
571 return c;
574 tree
575 strip_top_quals (tree t)
577 if (TREE_CODE (t) == ARRAY_TYPE)
578 return t;
579 return cp_build_qualified_type (t, 0);
582 /* Returns the standard conversion path (see [conv]) from type FROM to type
583 TO, if any. For proper handling of null pointer constants, you must
584 also pass the expression EXPR to convert from. If C_CAST_P is true,
585 this conversion is coming from a C-style cast. */
587 static conversion *
588 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
589 int flags)
591 enum tree_code fcode, tcode;
592 conversion *conv;
593 bool fromref = false;
595 to = non_reference (to);
596 if (TREE_CODE (from) == REFERENCE_TYPE)
598 fromref = true;
599 from = TREE_TYPE (from);
601 to = strip_top_quals (to);
602 from = strip_top_quals (from);
604 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
605 && expr && type_unknown_p (expr))
607 expr = instantiate_type (to, expr, tf_conv);
608 if (expr == error_mark_node)
609 return NULL;
610 from = TREE_TYPE (expr);
613 fcode = TREE_CODE (from);
614 tcode = TREE_CODE (to);
616 conv = build_identity_conv (from, expr);
617 if (fcode == FUNCTION_TYPE)
619 from = build_pointer_type (from);
620 fcode = TREE_CODE (from);
621 conv = build_conv (ck_lvalue, from, conv);
623 else if (fcode == ARRAY_TYPE)
625 from = build_pointer_type (TREE_TYPE (from));
626 fcode = TREE_CODE (from);
627 conv = build_conv (ck_lvalue, from, conv);
629 else if (fromref || (expr && lvalue_p (expr)))
631 if (expr)
633 tree bitfield_type;
634 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
635 if (bitfield_type)
636 from = strip_top_quals (bitfield_type);
638 conv = build_conv (ck_rvalue, from, conv);
641 /* Allow conversion between `__complex__' data types. */
642 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
644 /* The standard conversion sequence to convert FROM to TO is
645 the standard conversion sequence to perform componentwise
646 conversion. */
647 conversion *part_conv = standard_conversion
648 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags);
650 if (part_conv)
652 conv = build_conv (part_conv->kind, to, conv);
653 conv->rank = part_conv->rank;
655 else
656 conv = NULL;
658 return conv;
661 if (same_type_p (from, to))
662 return conv;
664 if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to))
665 && expr && null_ptr_cst_p (expr))
666 conv = build_conv (ck_std, to, conv);
667 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
668 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
670 /* For backwards brain damage compatibility, allow interconversion of
671 pointers and integers with a pedwarn. */
672 conv = build_conv (ck_std, to, conv);
673 conv->bad_p = true;
675 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE)
677 /* For backwards brain damage compatibility, allow interconversion of
678 enums and integers with a pedwarn. */
679 conv = build_conv (ck_std, to, conv);
680 conv->bad_p = true;
682 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
683 || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
685 tree to_pointee;
686 tree from_pointee;
688 if (tcode == POINTER_TYPE
689 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
690 TREE_TYPE (to)))
692 else if (VOID_TYPE_P (TREE_TYPE (to))
693 && !TYPE_PTRMEM_P (from)
694 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
696 from = build_pointer_type
697 (cp_build_qualified_type (void_type_node,
698 cp_type_quals (TREE_TYPE (from))));
699 conv = build_conv (ck_ptr, from, conv);
701 else if (TYPE_PTRMEM_P (from))
703 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
704 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
706 if (DERIVED_FROM_P (fbase, tbase)
707 && (same_type_ignoring_top_level_qualifiers_p
708 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
709 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
711 from = build_ptrmem_type (tbase,
712 TYPE_PTRMEM_POINTED_TO_TYPE (from));
713 conv = build_conv (ck_pmem, from, conv);
715 else if (!same_type_p (fbase, tbase))
716 return NULL;
718 else if (IS_AGGR_TYPE (TREE_TYPE (from))
719 && IS_AGGR_TYPE (TREE_TYPE (to))
720 /* [conv.ptr]
722 An rvalue of type "pointer to cv D," where D is a
723 class type, can be converted to an rvalue of type
724 "pointer to cv B," where B is a base class (clause
725 _class.derived_) of D. If B is an inaccessible
726 (clause _class.access_) or ambiguous
727 (_class.member.lookup_) base class of D, a program
728 that necessitates this conversion is ill-formed.
729 Therefore, we use DERIVED_FROM_P, and do not check
730 access or uniqueness. */
731 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from))
732 /* If FROM is not yet complete, then we must be parsing
733 the body of a class. We know what's derived from
734 what, but we can't actually perform a
735 derived-to-base conversion. For example, in:
737 struct D : public B {
738 static const int i = sizeof((B*)(D*)0);
741 the D*-to-B* conversion is a reinterpret_cast, not a
742 static_cast. */
743 && COMPLETE_TYPE_P (TREE_TYPE (from)))
745 from =
746 cp_build_qualified_type (TREE_TYPE (to),
747 cp_type_quals (TREE_TYPE (from)));
748 from = build_pointer_type (from);
749 conv = build_conv (ck_ptr, from, conv);
750 conv->base_p = true;
753 if (tcode == POINTER_TYPE)
755 to_pointee = TREE_TYPE (to);
756 from_pointee = TREE_TYPE (from);
758 else
760 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
761 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
764 if (same_type_p (from, to))
765 /* OK */;
766 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
767 /* In a C-style cast, we ignore CV-qualification because we
768 are allowed to perform a static_cast followed by a
769 const_cast. */
770 conv = build_conv (ck_qual, to, conv);
771 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
772 conv = build_conv (ck_qual, to, conv);
773 else if (expr && string_conv_p (to, expr, 0))
774 /* converting from string constant to char *. */
775 conv = build_conv (ck_qual, to, conv);
776 else if (ptr_reasonably_similar (to_pointee, from_pointee))
778 conv = build_conv (ck_ptr, to, conv);
779 conv->bad_p = true;
781 else
782 return NULL;
784 from = to;
786 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
788 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
789 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
790 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
791 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
793 if (!DERIVED_FROM_P (fbase, tbase)
794 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
795 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
796 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
797 || cp_type_quals (fbase) != cp_type_quals (tbase))
798 return NULL;
800 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
801 from = build_method_type_directly (from,
802 TREE_TYPE (fromfn),
803 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
804 from = build_ptrmemfunc_type (build_pointer_type (from));
805 conv = build_conv (ck_pmem, from, conv);
806 conv->base_p = true;
808 else if (tcode == BOOLEAN_TYPE)
810 /* [conv.bool]
812 An rvalue of arithmetic, enumeration, pointer, or pointer to
813 member type can be converted to an rvalue of type bool. */
814 if (ARITHMETIC_TYPE_P (from)
815 || fcode == ENUMERAL_TYPE
816 || fcode == POINTER_TYPE
817 || TYPE_PTR_TO_MEMBER_P (from))
819 conv = build_conv (ck_std, to, conv);
820 if (fcode == POINTER_TYPE
821 || TYPE_PTRMEM_P (from)
822 || (TYPE_PTRMEMFUNC_P (from)
823 && conv->rank < cr_pbool))
824 conv->rank = cr_pbool;
825 return conv;
828 return NULL;
830 /* We don't check for ENUMERAL_TYPE here because there are no standard
831 conversions to enum type. */
832 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
833 || tcode == REAL_TYPE)
835 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
836 return NULL;
837 conv = build_conv (ck_std, to, conv);
839 /* Give this a better rank if it's a promotion. */
840 if (same_type_p (to, type_promotes_to (from))
841 && conv->u.next->rank <= cr_promotion)
842 conv->rank = cr_promotion;
844 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
845 && vector_types_convertible_p (from, to))
846 return build_conv (ck_std, to, conv);
847 else if (!(flags & LOOKUP_CONSTRUCTOR_CALLABLE)
848 && IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
849 && is_properly_derived_from (from, to))
851 if (conv->kind == ck_rvalue)
852 conv = conv->u.next;
853 conv = build_conv (ck_base, to, conv);
854 /* The derived-to-base conversion indicates the initialization
855 of a parameter with base type from an object of a derived
856 type. A temporary object is created to hold the result of
857 the conversion. */
858 conv->need_temporary_p = true;
860 else
861 return NULL;
863 return conv;
866 /* Returns nonzero if T1 is reference-related to T2. */
868 static bool
869 reference_related_p (tree t1, tree t2)
871 t1 = TYPE_MAIN_VARIANT (t1);
872 t2 = TYPE_MAIN_VARIANT (t2);
874 /* [dcl.init.ref]
876 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
877 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
878 of T2. */
879 return (same_type_p (t1, t2)
880 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
881 && DERIVED_FROM_P (t1, t2)));
884 /* Returns nonzero if T1 is reference-compatible with T2. */
886 static bool
887 reference_compatible_p (tree t1, tree t2)
889 /* [dcl.init.ref]
891 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
892 reference-related to T2 and cv1 is the same cv-qualification as,
893 or greater cv-qualification than, cv2. */
894 return (reference_related_p (t1, t2)
895 && at_least_as_qualified_p (t1, t2));
898 /* Determine whether or not the EXPR (of class type S) can be
899 converted to T as in [over.match.ref]. */
901 static conversion *
902 convert_class_to_reference (tree t, tree s, tree expr)
904 tree conversions;
905 tree arglist;
906 conversion *conv;
907 tree reference_type;
908 struct z_candidate *candidates;
909 struct z_candidate *cand;
910 bool any_viable_p;
912 conversions = lookup_conversions (s);
913 if (!conversions)
914 return NULL;
916 /* [over.match.ref]
918 Assuming that "cv1 T" is the underlying type of the reference
919 being initialized, and "cv S" is the type of the initializer
920 expression, with S a class type, the candidate functions are
921 selected as follows:
923 --The conversion functions of S and its base classes are
924 considered. Those that are not hidden within S and yield type
925 "reference to cv2 T2", where "cv1 T" is reference-compatible
926 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
928 The argument list has one argument, which is the initializer
929 expression. */
931 candidates = 0;
933 /* Conceptually, we should take the address of EXPR and put it in
934 the argument list. Unfortunately, however, that can result in
935 error messages, which we should not issue now because we are just
936 trying to find a conversion operator. Therefore, we use NULL,
937 cast to the appropriate type. */
938 arglist = build_int_cst (build_pointer_type (s), 0);
939 arglist = build_tree_list (NULL_TREE, arglist);
941 reference_type = build_reference_type (t);
943 while (conversions)
945 tree fns = TREE_VALUE (conversions);
947 for (; fns; fns = OVL_NEXT (fns))
949 tree f = OVL_CURRENT (fns);
950 tree t2 = TREE_TYPE (TREE_TYPE (f));
952 cand = NULL;
954 /* If this is a template function, try to get an exact
955 match. */
956 if (TREE_CODE (f) == TEMPLATE_DECL)
958 cand = add_template_candidate (&candidates,
959 f, s,
960 NULL_TREE,
961 arglist,
962 reference_type,
963 TYPE_BINFO (s),
964 TREE_PURPOSE (conversions),
965 LOOKUP_NORMAL,
966 DEDUCE_CONV);
968 if (cand)
970 /* Now, see if the conversion function really returns
971 an lvalue of the appropriate type. From the
972 point of view of unification, simply returning an
973 rvalue of the right type is good enough. */
974 f = cand->fn;
975 t2 = TREE_TYPE (TREE_TYPE (f));
976 if (TREE_CODE (t2) != REFERENCE_TYPE
977 || !reference_compatible_p (t, TREE_TYPE (t2)))
979 candidates = candidates->next;
980 cand = NULL;
984 else if (TREE_CODE (t2) == REFERENCE_TYPE
985 && reference_compatible_p (t, TREE_TYPE (t2)))
986 cand = add_function_candidate (&candidates, f, s, arglist,
987 TYPE_BINFO (s),
988 TREE_PURPOSE (conversions),
989 LOOKUP_NORMAL);
991 if (cand)
993 conversion *identity_conv;
994 /* Build a standard conversion sequence indicating the
995 binding from the reference type returned by the
996 function to the desired REFERENCE_TYPE. */
997 identity_conv
998 = build_identity_conv (TREE_TYPE (TREE_TYPE
999 (TREE_TYPE (cand->fn))),
1000 NULL_TREE);
1001 cand->second_conv
1002 = (direct_reference_binding
1003 (reference_type, identity_conv));
1004 cand->second_conv->bad_p |= cand->convs[0]->bad_p;
1007 conversions = TREE_CHAIN (conversions);
1010 candidates = splice_viable (candidates, pedantic, &any_viable_p);
1011 /* If none of the conversion functions worked out, let our caller
1012 know. */
1013 if (!any_viable_p)
1014 return NULL;
1016 cand = tourney (candidates);
1017 if (!cand)
1018 return NULL;
1020 /* Now that we know that this is the function we're going to use fix
1021 the dummy first argument. */
1022 cand->args = tree_cons (NULL_TREE,
1023 build_this (expr),
1024 TREE_CHAIN (cand->args));
1026 /* Build a user-defined conversion sequence representing the
1027 conversion. */
1028 conv = build_conv (ck_user,
1029 TREE_TYPE (TREE_TYPE (cand->fn)),
1030 build_identity_conv (TREE_TYPE (expr), expr));
1031 conv->cand = cand;
1033 /* Merge it with the standard conversion sequence from the
1034 conversion function's return type to the desired type. */
1035 cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
1037 if (cand->viable == -1)
1038 conv->bad_p = true;
1040 return cand->second_conv;
1043 /* A reference of the indicated TYPE is being bound directly to the
1044 expression represented by the implicit conversion sequence CONV.
1045 Return a conversion sequence for this binding. */
1047 static conversion *
1048 direct_reference_binding (tree type, conversion *conv)
1050 tree t;
1052 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1053 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1055 t = TREE_TYPE (type);
1057 /* [over.ics.rank]
1059 When a parameter of reference type binds directly
1060 (_dcl.init.ref_) to an argument expression, the implicit
1061 conversion sequence is the identity conversion, unless the
1062 argument expression has a type that is a derived class of the
1063 parameter type, in which case the implicit conversion sequence is
1064 a derived-to-base Conversion.
1066 If the parameter binds directly to the result of applying a
1067 conversion function to the argument expression, the implicit
1068 conversion sequence is a user-defined conversion sequence
1069 (_over.ics.user_), with the second standard conversion sequence
1070 either an identity conversion or, if the conversion function
1071 returns an entity of a type that is a derived class of the
1072 parameter type, a derived-to-base conversion. */
1073 if (!same_type_ignoring_top_level_qualifiers_p (t, conv->type))
1075 /* Represent the derived-to-base conversion. */
1076 conv = build_conv (ck_base, t, conv);
1077 /* We will actually be binding to the base-class subobject in
1078 the derived class, so we mark this conversion appropriately.
1079 That way, convert_like knows not to generate a temporary. */
1080 conv->need_temporary_p = false;
1082 return build_conv (ck_ref_bind, type, conv);
1085 /* Returns the conversion path from type FROM to reference type TO for
1086 purposes of reference binding. For lvalue binding, either pass a
1087 reference type to FROM or an lvalue expression to EXPR. If the
1088 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1089 the conversion returned. */
1091 static conversion *
1092 reference_binding (tree rto, tree rfrom, tree expr, int flags)
1094 conversion *conv = NULL;
1095 tree to = TREE_TYPE (rto);
1096 tree from = rfrom;
1097 bool related_p;
1098 bool compatible_p;
1099 cp_lvalue_kind lvalue_p = clk_none;
1101 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1103 expr = instantiate_type (to, expr, tf_none);
1104 if (expr == error_mark_node)
1105 return NULL;
1106 from = TREE_TYPE (expr);
1109 if (TREE_CODE (from) == REFERENCE_TYPE)
1111 /* Anything with reference type is an lvalue. */
1112 lvalue_p = clk_ordinary;
1113 from = TREE_TYPE (from);
1115 else if (expr)
1116 lvalue_p = real_lvalue_p (expr);
1118 /* Figure out whether or not the types are reference-related and
1119 reference compatible. We have do do this after stripping
1120 references from FROM. */
1121 related_p = reference_related_p (to, from);
1122 compatible_p = reference_compatible_p (to, from);
1124 if (lvalue_p && compatible_p)
1126 /* [dcl.init.ref]
1128 If the initializer expression
1130 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1131 is reference-compatible with "cv2 T2,"
1133 the reference is bound directly to the initializer expression
1134 lvalue. */
1135 conv = build_identity_conv (from, expr);
1136 conv = direct_reference_binding (rto, conv);
1137 if ((lvalue_p & clk_bitfield) != 0
1138 || ((lvalue_p & clk_packed) != 0 && !TYPE_PACKED (to)))
1139 /* For the purposes of overload resolution, we ignore the fact
1140 this expression is a bitfield or packed field. (In particular,
1141 [over.ics.ref] says specifically that a function with a
1142 non-const reference parameter is viable even if the
1143 argument is a bitfield.)
1145 However, when we actually call the function we must create
1146 a temporary to which to bind the reference. If the
1147 reference is volatile, or isn't const, then we cannot make
1148 a temporary, so we just issue an error when the conversion
1149 actually occurs. */
1150 conv->need_temporary_p = true;
1152 return conv;
1154 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1156 /* [dcl.init.ref]
1158 If the initializer expression
1160 -- has a class type (i.e., T2 is a class type) can be
1161 implicitly converted to an lvalue of type "cv3 T3," where
1162 "cv1 T1" is reference-compatible with "cv3 T3". (this
1163 conversion is selected by enumerating the applicable
1164 conversion functions (_over.match.ref_) and choosing the
1165 best one through overload resolution. (_over.match_).
1167 the reference is bound to the lvalue result of the conversion
1168 in the second case. */
1169 conv = convert_class_to_reference (to, from, expr);
1170 if (conv)
1171 return conv;
1174 /* From this point on, we conceptually need temporaries, even if we
1175 elide them. Only the cases above are "direct bindings". */
1176 if (flags & LOOKUP_NO_TEMP_BIND)
1177 return NULL;
1179 /* [over.ics.rank]
1181 When a parameter of reference type is not bound directly to an
1182 argument expression, the conversion sequence is the one required
1183 to convert the argument expression to the underlying type of the
1184 reference according to _over.best.ics_. Conceptually, this
1185 conversion sequence corresponds to copy-initializing a temporary
1186 of the underlying type with the argument expression. Any
1187 difference in top-level cv-qualification is subsumed by the
1188 initialization itself and does not constitute a conversion. */
1190 /* [dcl.init.ref]
1192 Otherwise, the reference shall be to a non-volatile const type. */
1193 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1194 return NULL;
1196 /* [dcl.init.ref]
1198 If the initializer expression is an rvalue, with T2 a class type,
1199 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1200 is bound in one of the following ways:
1202 -- The reference is bound to the object represented by the rvalue
1203 or to a sub-object within that object.
1205 -- ...
1207 We use the first alternative. The implicit conversion sequence
1208 is supposed to be same as we would obtain by generating a
1209 temporary. Fortunately, if the types are reference compatible,
1210 then this is either an identity conversion or the derived-to-base
1211 conversion, just as for direct binding. */
1212 if (CLASS_TYPE_P (from) && compatible_p)
1214 conv = build_identity_conv (from, expr);
1215 conv = direct_reference_binding (rto, conv);
1216 if (!(flags & LOOKUP_CONSTRUCTOR_CALLABLE))
1217 conv->u.next->check_copy_constructor_p = true;
1218 return conv;
1221 /* [dcl.init.ref]
1223 Otherwise, a temporary of type "cv1 T1" is created and
1224 initialized from the initializer expression using the rules for a
1225 non-reference copy initialization. If T1 is reference-related to
1226 T2, cv1 must be the same cv-qualification as, or greater
1227 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1228 if (related_p && !at_least_as_qualified_p (to, from))
1229 return NULL;
1231 conv = implicit_conversion (to, from, expr, /*c_cast_p=*/false,
1232 flags);
1233 if (!conv)
1234 return NULL;
1236 conv = build_conv (ck_ref_bind, rto, conv);
1237 /* This reference binding, unlike those above, requires the
1238 creation of a temporary. */
1239 conv->need_temporary_p = true;
1241 return conv;
1244 /* Returns the implicit conversion sequence (see [over.ics]) from type
1245 FROM to type TO. The optional expression EXPR may affect the
1246 conversion. FLAGS are the usual overloading flags. Only
1247 LOOKUP_NO_CONVERSION is significant. If C_CAST_P is true, this
1248 conversion is coming from a C-style cast. */
1250 static conversion *
1251 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1252 int flags)
1254 conversion *conv;
1256 if (from == error_mark_node || to == error_mark_node
1257 || expr == error_mark_node)
1258 return NULL;
1260 if (TREE_CODE (to) == REFERENCE_TYPE)
1261 conv = reference_binding (to, from, expr, flags);
1262 else
1263 conv = standard_conversion (to, from, expr, c_cast_p, flags);
1265 if (conv)
1266 return conv;
1268 if (expr != NULL_TREE
1269 && (IS_AGGR_TYPE (from)
1270 || IS_AGGR_TYPE (to))
1271 && (flags & LOOKUP_NO_CONVERSION) == 0)
1273 struct z_candidate *cand;
1275 cand = build_user_type_conversion_1
1276 (to, expr, LOOKUP_ONLYCONVERTING);
1277 if (cand)
1278 conv = cand->second_conv;
1280 /* We used to try to bind a reference to a temporary here, but that
1281 is now handled by the recursive call to this function at the end
1282 of reference_binding. */
1283 return conv;
1286 return NULL;
1289 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1290 functions. */
1292 static struct z_candidate *
1293 add_candidate (struct z_candidate **candidates,
1294 tree fn, tree args,
1295 size_t num_convs, conversion **convs,
1296 tree access_path, tree conversion_path,
1297 int viable)
1299 struct z_candidate *cand = (struct z_candidate *)
1300 conversion_obstack_alloc (sizeof (struct z_candidate));
1302 cand->fn = fn;
1303 cand->args = args;
1304 cand->convs = convs;
1305 cand->num_convs = num_convs;
1306 cand->access_path = access_path;
1307 cand->conversion_path = conversion_path;
1308 cand->viable = viable;
1309 cand->next = *candidates;
1310 *candidates = cand;
1312 return cand;
1315 /* Create an overload candidate for the function or method FN called with
1316 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1317 to implicit_conversion.
1319 CTYPE, if non-NULL, is the type we want to pretend this function
1320 comes from for purposes of overload resolution. */
1322 static struct z_candidate *
1323 add_function_candidate (struct z_candidate **candidates,
1324 tree fn, tree ctype, tree arglist,
1325 tree access_path, tree conversion_path,
1326 int flags)
1328 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1329 int i, len;
1330 conversion **convs;
1331 tree parmnode, argnode;
1332 tree orig_arglist;
1333 int viable = 1;
1335 /* At this point we should not see any functions which haven't been
1336 explicitly declared, except for friend functions which will have
1337 been found using argument dependent lookup. */
1338 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
1340 /* The `this', `in_chrg' and VTT arguments to constructors are not
1341 considered in overload resolution. */
1342 if (DECL_CONSTRUCTOR_P (fn))
1344 parmlist = skip_artificial_parms_for (fn, parmlist);
1345 orig_arglist = arglist;
1346 arglist = skip_artificial_parms_for (fn, arglist);
1348 else
1349 orig_arglist = arglist;
1351 len = list_length (arglist);
1352 convs = alloc_conversions (len);
1354 /* 13.3.2 - Viable functions [over.match.viable]
1355 First, to be a viable function, a candidate function shall have enough
1356 parameters to agree in number with the arguments in the list.
1358 We need to check this first; otherwise, checking the ICSes might cause
1359 us to produce an ill-formed template instantiation. */
1361 parmnode = parmlist;
1362 for (i = 0; i < len; ++i)
1364 if (parmnode == NULL_TREE || parmnode == void_list_node)
1365 break;
1366 parmnode = TREE_CHAIN (parmnode);
1369 if (i < len && parmnode)
1370 viable = 0;
1372 /* Make sure there are default args for the rest of the parms. */
1373 else if (!sufficient_parms_p (parmnode))
1374 viable = 0;
1376 if (! viable)
1377 goto out;
1379 /* Second, for F to be a viable function, there shall exist for each
1380 argument an implicit conversion sequence that converts that argument
1381 to the corresponding parameter of F. */
1383 parmnode = parmlist;
1384 argnode = arglist;
1386 for (i = 0; i < len; ++i)
1388 tree arg = TREE_VALUE (argnode);
1389 tree argtype = lvalue_type (arg);
1390 conversion *t;
1391 int is_this;
1393 if (parmnode == void_list_node)
1394 break;
1396 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1397 && ! DECL_CONSTRUCTOR_P (fn));
1399 if (parmnode)
1401 tree parmtype = TREE_VALUE (parmnode);
1403 /* The type of the implicit object parameter ('this') for
1404 overload resolution is not always the same as for the
1405 function itself; conversion functions are considered to
1406 be members of the class being converted, and functions
1407 introduced by a using-declaration are considered to be
1408 members of the class that uses them.
1410 Since build_over_call ignores the ICS for the `this'
1411 parameter, we can just change the parm type. */
1412 if (ctype && is_this)
1414 parmtype
1415 = build_qualified_type (ctype,
1416 TYPE_QUALS (TREE_TYPE (parmtype)));
1417 parmtype = build_pointer_type (parmtype);
1420 t = implicit_conversion (parmtype, argtype, arg,
1421 /*c_cast_p=*/false, flags);
1423 else
1425 t = build_identity_conv (argtype, arg);
1426 t->ellipsis_p = true;
1429 if (t && is_this)
1430 t->this_p = true;
1432 convs[i] = t;
1433 if (! t)
1435 viable = 0;
1436 break;
1439 if (t->bad_p)
1440 viable = -1;
1442 if (parmnode)
1443 parmnode = TREE_CHAIN (parmnode);
1444 argnode = TREE_CHAIN (argnode);
1447 out:
1448 return add_candidate (candidates, fn, orig_arglist, len, convs,
1449 access_path, conversion_path, viable);
1452 /* Create an overload candidate for the conversion function FN which will
1453 be invoked for expression OBJ, producing a pointer-to-function which
1454 will in turn be called with the argument list ARGLIST, and add it to
1455 CANDIDATES. FLAGS is passed on to implicit_conversion.
1457 Actually, we don't really care about FN; we care about the type it
1458 converts to. There may be multiple conversion functions that will
1459 convert to that type, and we rely on build_user_type_conversion_1 to
1460 choose the best one; so when we create our candidate, we record the type
1461 instead of the function. */
1463 static struct z_candidate *
1464 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
1465 tree arglist, tree access_path, tree conversion_path)
1467 tree totype = TREE_TYPE (TREE_TYPE (fn));
1468 int i, len, viable, flags;
1469 tree parmlist, parmnode, argnode;
1470 conversion **convs;
1472 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1473 parmlist = TREE_TYPE (parmlist);
1474 parmlist = TYPE_ARG_TYPES (parmlist);
1476 len = list_length (arglist) + 1;
1477 convs = alloc_conversions (len);
1478 parmnode = parmlist;
1479 argnode = arglist;
1480 viable = 1;
1481 flags = LOOKUP_NORMAL;
1483 /* Don't bother looking up the same type twice. */
1484 if (*candidates && (*candidates)->fn == totype)
1485 return NULL;
1487 for (i = 0; i < len; ++i)
1489 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1490 tree argtype = lvalue_type (arg);
1491 conversion *t;
1493 if (i == 0)
1494 t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false,
1495 flags);
1496 else if (parmnode == void_list_node)
1497 break;
1498 else if (parmnode)
1499 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
1500 /*c_cast_p=*/false, flags);
1501 else
1503 t = build_identity_conv (argtype, arg);
1504 t->ellipsis_p = true;
1507 convs[i] = t;
1508 if (! t)
1509 break;
1511 if (t->bad_p)
1512 viable = -1;
1514 if (i == 0)
1515 continue;
1517 if (parmnode)
1518 parmnode = TREE_CHAIN (parmnode);
1519 argnode = TREE_CHAIN (argnode);
1522 if (i < len)
1523 viable = 0;
1525 if (!sufficient_parms_p (parmnode))
1526 viable = 0;
1528 return add_candidate (candidates, totype, arglist, len, convs,
1529 access_path, conversion_path, viable);
1532 static void
1533 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
1534 tree type1, tree type2, tree *args, tree *argtypes,
1535 int flags)
1537 conversion *t;
1538 conversion **convs;
1539 size_t num_convs;
1540 int viable = 1, i;
1541 tree types[2];
1543 types[0] = type1;
1544 types[1] = type2;
1546 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
1547 convs = alloc_conversions (num_convs);
1549 for (i = 0; i < 2; ++i)
1551 if (! args[i])
1552 break;
1554 t = implicit_conversion (types[i], argtypes[i], args[i],
1555 /*c_cast_p=*/false, flags);
1556 if (! t)
1558 viable = 0;
1559 /* We need something for printing the candidate. */
1560 t = build_identity_conv (types[i], NULL_TREE);
1562 else if (t->bad_p)
1563 viable = 0;
1564 convs[i] = t;
1567 /* For COND_EXPR we rearranged the arguments; undo that now. */
1568 if (args[2])
1570 convs[2] = convs[1];
1571 convs[1] = convs[0];
1572 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
1573 /*c_cast_p=*/false, flags);
1574 if (t)
1575 convs[0] = t;
1576 else
1577 viable = 0;
1580 add_candidate (candidates, fnname, /*args=*/NULL_TREE,
1581 num_convs, convs,
1582 /*access_path=*/NULL_TREE,
1583 /*conversion_path=*/NULL_TREE,
1584 viable);
1587 static bool
1588 is_complete (tree t)
1590 return COMPLETE_TYPE_P (complete_type (t));
1593 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1595 static bool
1596 promoted_arithmetic_type_p (tree type)
1598 /* [over.built]
1600 In this section, the term promoted integral type is used to refer
1601 to those integral types which are preserved by integral promotion
1602 (including e.g. int and long but excluding e.g. char).
1603 Similarly, the term promoted arithmetic type refers to promoted
1604 integral types plus floating types. */
1605 return ((INTEGRAL_TYPE_P (type)
1606 && same_type_p (type_promotes_to (type), type))
1607 || TREE_CODE (type) == REAL_TYPE);
1610 /* Create any builtin operator overload candidates for the operator in
1611 question given the converted operand types TYPE1 and TYPE2. The other
1612 args are passed through from add_builtin_candidates to
1613 build_builtin_candidate.
1615 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1616 If CODE is requires candidates operands of the same type of the kind
1617 of which TYPE1 and TYPE2 are, we add both candidates
1618 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1620 static void
1621 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
1622 enum tree_code code2, tree fnname, tree type1,
1623 tree type2, tree *args, tree *argtypes, int flags)
1625 switch (code)
1627 case POSTINCREMENT_EXPR:
1628 case POSTDECREMENT_EXPR:
1629 args[1] = integer_zero_node;
1630 type2 = integer_type_node;
1631 break;
1632 default:
1633 break;
1636 switch (code)
1639 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1640 and VQ is either volatile or empty, there exist candidate operator
1641 functions of the form
1642 VQ T& operator++(VQ T&);
1643 T operator++(VQ T&, int);
1644 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1645 type other than bool, and VQ is either volatile or empty, there exist
1646 candidate operator functions of the form
1647 VQ T& operator--(VQ T&);
1648 T operator--(VQ T&, int);
1649 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1650 complete object type, and VQ is either volatile or empty, there exist
1651 candidate operator functions of the form
1652 T*VQ& operator++(T*VQ&);
1653 T*VQ& operator--(T*VQ&);
1654 T* operator++(T*VQ&, int);
1655 T* operator--(T*VQ&, int); */
1657 case POSTDECREMENT_EXPR:
1658 case PREDECREMENT_EXPR:
1659 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1660 return;
1661 case POSTINCREMENT_EXPR:
1662 case PREINCREMENT_EXPR:
1663 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1665 type1 = build_reference_type (type1);
1666 break;
1668 return;
1670 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1671 exist candidate operator functions of the form
1673 T& operator*(T*);
1675 8 For every function type T, there exist candidate operator functions of
1676 the form
1677 T& operator*(T*); */
1679 case INDIRECT_REF:
1680 if (TREE_CODE (type1) == POINTER_TYPE
1681 && (TYPE_PTROB_P (type1)
1682 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1683 break;
1684 return;
1686 /* 9 For every type T, there exist candidate operator functions of the form
1687 T* operator+(T*);
1689 10For every promoted arithmetic type T, there exist candidate operator
1690 functions of the form
1691 T operator+(T);
1692 T operator-(T); */
1694 case UNARY_PLUS_EXPR: /* unary + */
1695 if (TREE_CODE (type1) == POINTER_TYPE)
1696 break;
1697 case NEGATE_EXPR:
1698 if (ARITHMETIC_TYPE_P (type1))
1699 break;
1700 return;
1702 /* 11For every promoted integral type T, there exist candidate operator
1703 functions of the form
1704 T operator~(T); */
1706 case BIT_NOT_EXPR:
1707 if (INTEGRAL_TYPE_P (type1))
1708 break;
1709 return;
1711 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1712 is the same type as C2 or is a derived class of C2, T is a complete
1713 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1714 there exist candidate operator functions of the form
1715 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1716 where CV12 is the union of CV1 and CV2. */
1718 case MEMBER_REF:
1719 if (TREE_CODE (type1) == POINTER_TYPE
1720 && TYPE_PTR_TO_MEMBER_P (type2))
1722 tree c1 = TREE_TYPE (type1);
1723 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
1725 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1726 && (TYPE_PTRMEMFUNC_P (type2)
1727 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
1728 break;
1730 return;
1732 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1733 didate operator functions of the form
1734 LR operator*(L, R);
1735 LR operator/(L, R);
1736 LR operator+(L, R);
1737 LR operator-(L, R);
1738 bool operator<(L, R);
1739 bool operator>(L, R);
1740 bool operator<=(L, R);
1741 bool operator>=(L, R);
1742 bool operator==(L, R);
1743 bool operator!=(L, R);
1744 where LR is the result of the usual arithmetic conversions between
1745 types L and R.
1747 14For every pair of types T and I, where T is a cv-qualified or cv-
1748 unqualified complete object type and I is a promoted integral type,
1749 there exist candidate operator functions of the form
1750 T* operator+(T*, I);
1751 T& operator[](T*, I);
1752 T* operator-(T*, I);
1753 T* operator+(I, T*);
1754 T& operator[](I, T*);
1756 15For every T, where T is a pointer to complete object type, there exist
1757 candidate operator functions of the form112)
1758 ptrdiff_t operator-(T, T);
1760 16For every pointer or enumeration type T, there exist candidate operator
1761 functions of the form
1762 bool operator<(T, T);
1763 bool operator>(T, T);
1764 bool operator<=(T, T);
1765 bool operator>=(T, T);
1766 bool operator==(T, T);
1767 bool operator!=(T, T);
1769 17For every pointer to member type T, there exist candidate operator
1770 functions of the form
1771 bool operator==(T, T);
1772 bool operator!=(T, T); */
1774 case MINUS_EXPR:
1775 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1776 break;
1777 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1779 type2 = ptrdiff_type_node;
1780 break;
1782 case MULT_EXPR:
1783 case TRUNC_DIV_EXPR:
1784 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1785 break;
1786 return;
1788 case EQ_EXPR:
1789 case NE_EXPR:
1790 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1791 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1792 break;
1793 if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
1795 type2 = type1;
1796 break;
1798 if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
1800 type1 = type2;
1801 break;
1803 /* Fall through. */
1804 case LT_EXPR:
1805 case GT_EXPR:
1806 case LE_EXPR:
1807 case GE_EXPR:
1808 case MAX_EXPR:
1809 case MIN_EXPR:
1810 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1811 break;
1812 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1813 break;
1814 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1815 break;
1816 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1818 type2 = type1;
1819 break;
1821 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1823 type1 = type2;
1824 break;
1826 return;
1828 case PLUS_EXPR:
1829 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1830 break;
1831 case ARRAY_REF:
1832 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1834 type1 = ptrdiff_type_node;
1835 break;
1837 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1839 type2 = ptrdiff_type_node;
1840 break;
1842 return;
1844 /* 18For every pair of promoted integral types L and R, there exist candi-
1845 date operator functions of the form
1846 LR operator%(L, R);
1847 LR operator&(L, R);
1848 LR operator^(L, R);
1849 LR operator|(L, R);
1850 L operator<<(L, R);
1851 L operator>>(L, R);
1852 where LR is the result of the usual arithmetic conversions between
1853 types L and R. */
1855 case TRUNC_MOD_EXPR:
1856 case BIT_AND_EXPR:
1857 case BIT_IOR_EXPR:
1858 case BIT_XOR_EXPR:
1859 case LSHIFT_EXPR:
1860 case RSHIFT_EXPR:
1861 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1862 break;
1863 return;
1865 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1866 type, VQ is either volatile or empty, and R is a promoted arithmetic
1867 type, there exist candidate operator functions of the form
1868 VQ L& operator=(VQ L&, R);
1869 VQ L& operator*=(VQ L&, R);
1870 VQ L& operator/=(VQ L&, R);
1871 VQ L& operator+=(VQ L&, R);
1872 VQ L& operator-=(VQ L&, R);
1874 20For every pair T, VQ), where T is any type and VQ is either volatile
1875 or empty, there exist candidate operator functions of the form
1876 T*VQ& operator=(T*VQ&, T*);
1878 21For every pair T, VQ), where T is a pointer to member type and VQ is
1879 either volatile or empty, there exist candidate operator functions of
1880 the form
1881 VQ T& operator=(VQ T&, T);
1883 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1884 unqualified complete object type, VQ is either volatile or empty, and
1885 I is a promoted integral type, there exist candidate operator func-
1886 tions of the form
1887 T*VQ& operator+=(T*VQ&, I);
1888 T*VQ& operator-=(T*VQ&, I);
1890 23For every triple L, VQ, R), where L is an integral or enumeration
1891 type, VQ is either volatile or empty, and R is a promoted integral
1892 type, there exist candidate operator functions of the form
1894 VQ L& operator%=(VQ L&, R);
1895 VQ L& operator<<=(VQ L&, R);
1896 VQ L& operator>>=(VQ L&, R);
1897 VQ L& operator&=(VQ L&, R);
1898 VQ L& operator^=(VQ L&, R);
1899 VQ L& operator|=(VQ L&, R); */
1901 case MODIFY_EXPR:
1902 switch (code2)
1904 case PLUS_EXPR:
1905 case MINUS_EXPR:
1906 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1908 type2 = ptrdiff_type_node;
1909 break;
1911 case MULT_EXPR:
1912 case TRUNC_DIV_EXPR:
1913 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1914 break;
1915 return;
1917 case TRUNC_MOD_EXPR:
1918 case BIT_AND_EXPR:
1919 case BIT_IOR_EXPR:
1920 case BIT_XOR_EXPR:
1921 case LSHIFT_EXPR:
1922 case RSHIFT_EXPR:
1923 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1924 break;
1925 return;
1927 case NOP_EXPR:
1928 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1929 break;
1930 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1931 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1932 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1933 || ((TYPE_PTRMEMFUNC_P (type1)
1934 || TREE_CODE (type1) == POINTER_TYPE)
1935 && null_ptr_cst_p (args[1])))
1937 type2 = type1;
1938 break;
1940 return;
1942 default:
1943 gcc_unreachable ();
1945 type1 = build_reference_type (type1);
1946 break;
1948 case COND_EXPR:
1949 /* [over.built]
1951 For every pair of promoted arithmetic types L and R, there
1952 exist candidate operator functions of the form
1954 LR operator?(bool, L, R);
1956 where LR is the result of the usual arithmetic conversions
1957 between types L and R.
1959 For every type T, where T is a pointer or pointer-to-member
1960 type, there exist candidate operator functions of the form T
1961 operator?(bool, T, T); */
1963 if (promoted_arithmetic_type_p (type1)
1964 && promoted_arithmetic_type_p (type2))
1965 /* That's OK. */
1966 break;
1968 /* Otherwise, the types should be pointers. */
1969 if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
1970 || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
1971 return;
1973 /* We don't check that the two types are the same; the logic
1974 below will actually create two candidates; one in which both
1975 parameter types are TYPE1, and one in which both parameter
1976 types are TYPE2. */
1977 break;
1979 default:
1980 gcc_unreachable ();
1983 /* If we're dealing with two pointer types or two enumeral types,
1984 we need candidates for both of them. */
1985 if (type2 && !same_type_p (type1, type2)
1986 && TREE_CODE (type1) == TREE_CODE (type2)
1987 && (TREE_CODE (type1) == REFERENCE_TYPE
1988 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1989 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1990 || TYPE_PTRMEMFUNC_P (type1)
1991 || IS_AGGR_TYPE (type1)
1992 || TREE_CODE (type1) == ENUMERAL_TYPE))
1994 build_builtin_candidate
1995 (candidates, fnname, type1, type1, args, argtypes, flags);
1996 build_builtin_candidate
1997 (candidates, fnname, type2, type2, args, argtypes, flags);
1998 return;
2001 build_builtin_candidate
2002 (candidates, fnname, type1, type2, args, argtypes, flags);
2005 tree
2006 type_decays_to (tree type)
2008 if (TREE_CODE (type) == ARRAY_TYPE)
2009 return build_pointer_type (TREE_TYPE (type));
2010 if (TREE_CODE (type) == FUNCTION_TYPE)
2011 return build_pointer_type (type);
2012 return type;
2015 /* There are three conditions of builtin candidates:
2017 1) bool-taking candidates. These are the same regardless of the input.
2018 2) pointer-pair taking candidates. These are generated for each type
2019 one of the input types converts to.
2020 3) arithmetic candidates. According to the standard, we should generate
2021 all of these, but I'm trying not to...
2023 Here we generate a superset of the possible candidates for this particular
2024 case. That is a subset of the full set the standard defines, plus some
2025 other cases which the standard disallows. add_builtin_candidate will
2026 filter out the invalid set. */
2028 static void
2029 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2030 enum tree_code code2, tree fnname, tree *args,
2031 int flags)
2033 int ref1, i;
2034 int enum_p = 0;
2035 tree type, argtypes[3];
2036 /* TYPES[i] is the set of possible builtin-operator parameter types
2037 we will consider for the Ith argument. These are represented as
2038 a TREE_LIST; the TREE_VALUE of each node is the potential
2039 parameter type. */
2040 tree types[2];
2042 for (i = 0; i < 3; ++i)
2044 if (args[i])
2045 argtypes[i] = lvalue_type (args[i]);
2046 else
2047 argtypes[i] = NULL_TREE;
2050 switch (code)
2052 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2053 and VQ is either volatile or empty, there exist candidate operator
2054 functions of the form
2055 VQ T& operator++(VQ T&); */
2057 case POSTINCREMENT_EXPR:
2058 case PREINCREMENT_EXPR:
2059 case POSTDECREMENT_EXPR:
2060 case PREDECREMENT_EXPR:
2061 case MODIFY_EXPR:
2062 ref1 = 1;
2063 break;
2065 /* 24There also exist candidate operator functions of the form
2066 bool operator!(bool);
2067 bool operator&&(bool, bool);
2068 bool operator||(bool, bool); */
2070 case TRUTH_NOT_EXPR:
2071 build_builtin_candidate
2072 (candidates, fnname, boolean_type_node,
2073 NULL_TREE, args, argtypes, flags);
2074 return;
2076 case TRUTH_ORIF_EXPR:
2077 case TRUTH_ANDIF_EXPR:
2078 build_builtin_candidate
2079 (candidates, fnname, boolean_type_node,
2080 boolean_type_node, args, argtypes, flags);
2081 return;
2083 case ADDR_EXPR:
2084 case COMPOUND_EXPR:
2085 case COMPONENT_REF:
2086 return;
2088 case COND_EXPR:
2089 case EQ_EXPR:
2090 case NE_EXPR:
2091 case LT_EXPR:
2092 case LE_EXPR:
2093 case GT_EXPR:
2094 case GE_EXPR:
2095 enum_p = 1;
2096 /* Fall through. */
2098 default:
2099 ref1 = 0;
2102 types[0] = types[1] = NULL_TREE;
2104 for (i = 0; i < 2; ++i)
2106 if (! args[i])
2108 else if (IS_AGGR_TYPE (argtypes[i]))
2110 tree convs;
2112 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2113 return;
2115 convs = lookup_conversions (argtypes[i]);
2117 if (code == COND_EXPR)
2119 if (real_lvalue_p (args[i]))
2120 types[i] = tree_cons
2121 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2123 types[i] = tree_cons
2124 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2127 else if (! convs)
2128 return;
2130 for (; convs; convs = TREE_CHAIN (convs))
2132 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2134 if (i == 0 && ref1
2135 && (TREE_CODE (type) != REFERENCE_TYPE
2136 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2137 continue;
2139 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2140 types[i] = tree_cons (NULL_TREE, type, types[i]);
2142 type = non_reference (type);
2143 if (i != 0 || ! ref1)
2145 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2146 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2147 types[i] = tree_cons (NULL_TREE, type, types[i]);
2148 if (INTEGRAL_TYPE_P (type))
2149 type = type_promotes_to (type);
2152 if (! value_member (type, types[i]))
2153 types[i] = tree_cons (NULL_TREE, type, types[i]);
2156 else
2158 if (code == COND_EXPR && real_lvalue_p (args[i]))
2159 types[i] = tree_cons
2160 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2161 type = non_reference (argtypes[i]);
2162 if (i != 0 || ! ref1)
2164 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2165 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2166 types[i] = tree_cons (NULL_TREE, type, types[i]);
2167 if (INTEGRAL_TYPE_P (type))
2168 type = type_promotes_to (type);
2170 types[i] = tree_cons (NULL_TREE, type, types[i]);
2174 /* Run through the possible parameter types of both arguments,
2175 creating candidates with those parameter types. */
2176 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2178 if (types[1])
2179 for (type = types[1]; type; type = TREE_CHAIN (type))
2180 add_builtin_candidate
2181 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2182 TREE_VALUE (type), args, argtypes, flags);
2183 else
2184 add_builtin_candidate
2185 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2186 NULL_TREE, args, argtypes, flags);
2191 /* If TMPL can be successfully instantiated as indicated by
2192 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2194 TMPL is the template. EXPLICIT_TARGS are any explicit template
2195 arguments. ARGLIST is the arguments provided at the call-site.
2196 The RETURN_TYPE is the desired type for conversion operators. If
2197 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2198 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2199 add_conv_candidate. */
2201 static struct z_candidate*
2202 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2203 tree ctype, tree explicit_targs, tree arglist,
2204 tree return_type, tree access_path,
2205 tree conversion_path, int flags, tree obj,
2206 unification_kind_t strict)
2208 int ntparms = DECL_NTPARMS (tmpl);
2209 tree targs = make_tree_vec (ntparms);
2210 tree args_without_in_chrg = arglist;
2211 struct z_candidate *cand;
2212 int i;
2213 tree fn;
2215 /* We don't do deduction on the in-charge parameter, the VTT
2216 parameter or 'this'. */
2217 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2218 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2220 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2221 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2222 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
2223 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2225 i = fn_type_unification (tmpl, explicit_targs, targs,
2226 args_without_in_chrg,
2227 return_type, strict, flags);
2229 if (i != 0)
2230 return NULL;
2232 fn = instantiate_template (tmpl, targs, tf_none);
2233 if (fn == error_mark_node)
2234 return NULL;
2236 /* In [class.copy]:
2238 A member function template is never instantiated to perform the
2239 copy of a class object to an object of its class type.
2241 It's a little unclear what this means; the standard explicitly
2242 does allow a template to be used to copy a class. For example,
2245 struct A {
2246 A(A&);
2247 template <class T> A(const T&);
2249 const A f ();
2250 void g () { A a (f ()); }
2252 the member template will be used to make the copy. The section
2253 quoted above appears in the paragraph that forbids constructors
2254 whose only parameter is (a possibly cv-qualified variant of) the
2255 class type, and a logical interpretation is that the intent was
2256 to forbid the instantiation of member templates which would then
2257 have that form. */
2258 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2260 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2261 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2262 ctype))
2263 return NULL;
2266 if (obj != NULL_TREE)
2267 /* Aha, this is a conversion function. */
2268 cand = add_conv_candidate (candidates, fn, obj, access_path,
2269 conversion_path, arglist);
2270 else
2271 cand = add_function_candidate (candidates, fn, ctype,
2272 arglist, access_path,
2273 conversion_path, flags);
2274 if (DECL_TI_TEMPLATE (fn) != tmpl)
2275 /* This situation can occur if a member template of a template
2276 class is specialized. Then, instantiate_template might return
2277 an instantiation of the specialization, in which case the
2278 DECL_TI_TEMPLATE field will point at the original
2279 specialization. For example:
2281 template <class T> struct S { template <class U> void f(U);
2282 template <> void f(int) {}; };
2283 S<double> sd;
2284 sd.f(3);
2286 Here, TMPL will be template <class U> S<double>::f(U).
2287 And, instantiate template will give us the specialization
2288 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2289 for this will point at template <class T> template <> S<T>::f(int),
2290 so that we can find the definition. For the purposes of
2291 overload resolution, however, we want the original TMPL. */
2292 cand->template_decl = tree_cons (tmpl, targs, NULL_TREE);
2293 else
2294 cand->template_decl = DECL_TEMPLATE_INFO (fn);
2296 return cand;
2300 static struct z_candidate *
2301 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2302 tree explicit_targs, tree arglist, tree return_type,
2303 tree access_path, tree conversion_path, int flags,
2304 unification_kind_t strict)
2306 return
2307 add_template_candidate_real (candidates, tmpl, ctype,
2308 explicit_targs, arglist, return_type,
2309 access_path, conversion_path,
2310 flags, NULL_TREE, strict);
2314 static struct z_candidate *
2315 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2316 tree obj, tree arglist, tree return_type,
2317 tree access_path, tree conversion_path)
2319 return
2320 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2321 arglist, return_type, access_path,
2322 conversion_path, 0, obj, DEDUCE_CONV);
2325 /* The CANDS are the set of candidates that were considered for
2326 overload resolution. Return the set of viable candidates. If none
2327 of the candidates were viable, set *ANY_VIABLE_P to true. STRICT_P
2328 is true if a candidate should be considered viable only if it is
2329 strictly viable. */
2331 static struct z_candidate*
2332 splice_viable (struct z_candidate *cands,
2333 bool strict_p,
2334 bool *any_viable_p)
2336 struct z_candidate *viable;
2337 struct z_candidate **last_viable;
2338 struct z_candidate **cand;
2340 viable = NULL;
2341 last_viable = &viable;
2342 *any_viable_p = false;
2344 cand = &cands;
2345 while (*cand)
2347 struct z_candidate *c = *cand;
2348 if (strict_p ? c->viable == 1 : c->viable)
2350 *last_viable = c;
2351 *cand = c->next;
2352 c->next = NULL;
2353 last_viable = &c->next;
2354 *any_viable_p = true;
2356 else
2357 cand = &c->next;
2360 return viable ? viable : cands;
2363 static bool
2364 any_strictly_viable (struct z_candidate *cands)
2366 for (; cands; cands = cands->next)
2367 if (cands->viable == 1)
2368 return true;
2369 return false;
2372 /* OBJ is being used in an expression like "OBJ.f (...)". In other
2373 words, it is about to become the "this" pointer for a member
2374 function call. Take the address of the object. */
2376 static tree
2377 build_this (tree obj)
2379 /* In a template, we are only concerned about the type of the
2380 expression, so we can take a shortcut. */
2381 if (processing_template_decl)
2382 return build_address (obj);
2384 return build_unary_op (ADDR_EXPR, obj, 0);
2387 /* Returns true iff functions are equivalent. Equivalent functions are
2388 not '==' only if one is a function-local extern function or if
2389 both are extern "C". */
2391 static inline int
2392 equal_functions (tree fn1, tree fn2)
2394 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2395 || DECL_EXTERN_C_FUNCTION_P (fn1))
2396 return decls_match (fn1, fn2);
2397 return fn1 == fn2;
2400 /* Print information about one overload candidate CANDIDATE. MSGSTR
2401 is the text to print before the candidate itself.
2403 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2404 to have been run through gettext by the caller. This wart makes
2405 life simpler in print_z_candidates and for the translators. */
2407 static void
2408 print_z_candidate (const char *msgstr, struct z_candidate *candidate)
2410 if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2412 if (candidate->num_convs == 3)
2413 inform ("%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
2414 candidate->convs[0]->type,
2415 candidate->convs[1]->type,
2416 candidate->convs[2]->type);
2417 else if (candidate->num_convs == 2)
2418 inform ("%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
2419 candidate->convs[0]->type,
2420 candidate->convs[1]->type);
2421 else
2422 inform ("%s %D(%T) <built-in>", msgstr, candidate->fn,
2423 candidate->convs[0]->type);
2425 else if (TYPE_P (candidate->fn))
2426 inform ("%s %T <conversion>", msgstr, candidate->fn);
2427 else if (candidate->viable == -1)
2428 inform ("%s %+#D <near match>", msgstr, candidate->fn);
2429 else
2430 inform ("%s %+#D", msgstr, candidate->fn);
2433 static void
2434 print_z_candidates (struct z_candidate *candidates)
2436 const char *str;
2437 struct z_candidate *cand1;
2438 struct z_candidate **cand2;
2440 /* There may be duplicates in the set of candidates. We put off
2441 checking this condition as long as possible, since we have no way
2442 to eliminate duplicates from a set of functions in less than n^2
2443 time. Now we are about to emit an error message, so it is more
2444 permissible to go slowly. */
2445 for (cand1 = candidates; cand1; cand1 = cand1->next)
2447 tree fn = cand1->fn;
2448 /* Skip builtin candidates and conversion functions. */
2449 if (TREE_CODE (fn) != FUNCTION_DECL)
2450 continue;
2451 cand2 = &cand1->next;
2452 while (*cand2)
2454 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2455 && equal_functions (fn, (*cand2)->fn))
2456 *cand2 = (*cand2)->next;
2457 else
2458 cand2 = &(*cand2)->next;
2462 if (!candidates)
2463 return;
2465 str = _("candidates are:");
2466 print_z_candidate (str, candidates);
2467 if (candidates->next)
2469 /* Indent successive candidates by the width of the translation
2470 of the above string. */
2471 size_t len = gcc_gettext_width (str) + 1;
2472 char *spaces = (char *) alloca (len);
2473 memset (spaces, ' ', len-1);
2474 spaces[len - 1] = '\0';
2476 candidates = candidates->next;
2479 print_z_candidate (spaces, candidates);
2480 candidates = candidates->next;
2482 while (candidates);
2486 /* USER_SEQ is a user-defined conversion sequence, beginning with a
2487 USER_CONV. STD_SEQ is the standard conversion sequence applied to
2488 the result of the conversion function to convert it to the final
2489 desired type. Merge the two sequences into a single sequence,
2490 and return the merged sequence. */
2492 static conversion *
2493 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
2495 conversion **t;
2497 gcc_assert (user_seq->kind == ck_user);
2499 /* Find the end of the second conversion sequence. */
2500 t = &(std_seq);
2501 while ((*t)->kind != ck_identity)
2502 t = &((*t)->u.next);
2504 /* Replace the identity conversion with the user conversion
2505 sequence. */
2506 *t = user_seq;
2508 /* The entire sequence is a user-conversion sequence. */
2509 std_seq->user_conv_p = true;
2511 return std_seq;
2514 /* Returns the best overload candidate to perform the requested
2515 conversion. This function is used for three the overloading situations
2516 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2517 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2518 per [dcl.init.ref], so we ignore temporary bindings. */
2520 static struct z_candidate *
2521 build_user_type_conversion_1 (tree totype, tree expr, int flags)
2523 struct z_candidate *candidates, *cand;
2524 tree fromtype = TREE_TYPE (expr);
2525 tree ctors = NULL_TREE;
2526 tree conv_fns = NULL_TREE;
2527 conversion *conv = NULL;
2528 tree args = NULL_TREE;
2529 bool any_viable_p;
2531 /* We represent conversion within a hierarchy using RVALUE_CONV and
2532 BASE_CONV, as specified by [over.best.ics]; these become plain
2533 constructor calls, as specified in [dcl.init]. */
2534 gcc_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2535 || !DERIVED_FROM_P (totype, fromtype));
2537 if (IS_AGGR_TYPE (totype))
2538 ctors = lookup_fnfields (totype, complete_ctor_identifier, 0);
2540 if (IS_AGGR_TYPE (fromtype))
2541 conv_fns = lookup_conversions (fromtype);
2543 candidates = 0;
2544 flags |= LOOKUP_NO_CONVERSION;
2546 if (ctors)
2548 tree t;
2550 ctors = BASELINK_FUNCTIONS (ctors);
2552 t = build_int_cst (build_pointer_type (totype), 0);
2553 args = build_tree_list (NULL_TREE, expr);
2554 /* We should never try to call the abstract or base constructor
2555 from here. */
2556 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2557 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)));
2558 args = tree_cons (NULL_TREE, t, args);
2560 for (; ctors; ctors = OVL_NEXT (ctors))
2562 tree ctor = OVL_CURRENT (ctors);
2563 if (DECL_NONCONVERTING_P (ctor))
2564 continue;
2566 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2567 cand = add_template_candidate (&candidates, ctor, totype,
2568 NULL_TREE, args, NULL_TREE,
2569 TYPE_BINFO (totype),
2570 TYPE_BINFO (totype),
2571 flags,
2572 DEDUCE_CALL);
2573 else
2574 cand = add_function_candidate (&candidates, ctor, totype,
2575 args, TYPE_BINFO (totype),
2576 TYPE_BINFO (totype),
2577 flags);
2579 if (cand)
2580 cand->second_conv = build_identity_conv (totype, NULL_TREE);
2583 if (conv_fns)
2584 args = build_tree_list (NULL_TREE, build_this (expr));
2586 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
2588 tree fns;
2589 tree conversion_path = TREE_PURPOSE (conv_fns);
2590 int convflags = LOOKUP_NO_CONVERSION;
2592 /* If we are called to convert to a reference type, we are trying to
2593 find an lvalue binding, so don't even consider temporaries. If
2594 we don't find an lvalue binding, the caller will try again to
2595 look for a temporary binding. */
2596 if (TREE_CODE (totype) == REFERENCE_TYPE)
2597 convflags |= LOOKUP_NO_TEMP_BIND;
2599 for (fns = TREE_VALUE (conv_fns); fns; fns = OVL_NEXT (fns))
2601 tree fn = OVL_CURRENT (fns);
2603 /* [over.match.funcs] For conversion functions, the function
2604 is considered to be a member of the class of the implicit
2605 object argument for the purpose of defining the type of
2606 the implicit object parameter.
2608 So we pass fromtype as CTYPE to add_*_candidate. */
2610 if (TREE_CODE (fn) == TEMPLATE_DECL)
2611 cand = add_template_candidate (&candidates, fn, fromtype,
2612 NULL_TREE,
2613 args, totype,
2614 TYPE_BINFO (fromtype),
2615 conversion_path,
2616 flags,
2617 DEDUCE_CONV);
2618 else
2619 cand = add_function_candidate (&candidates, fn, fromtype,
2620 args,
2621 TYPE_BINFO (fromtype),
2622 conversion_path,
2623 flags);
2625 if (cand)
2627 conversion *ics
2628 = implicit_conversion (totype,
2629 TREE_TYPE (TREE_TYPE (cand->fn)),
2631 /*c_cast_p=*/false, convflags);
2633 cand->second_conv = ics;
2635 if (!ics)
2636 cand->viable = 0;
2637 else if (candidates->viable == 1 && ics->bad_p)
2638 cand->viable = -1;
2643 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2644 if (!any_viable_p)
2645 return NULL;
2647 cand = tourney (candidates);
2648 if (cand == 0)
2650 if (flags & LOOKUP_COMPLAIN)
2652 error ("conversion from %qT to %qT is ambiguous",
2653 fromtype, totype);
2654 print_z_candidates (candidates);
2657 cand = candidates; /* any one will do */
2658 cand->second_conv = build_ambiguous_conv (totype, expr);
2659 cand->second_conv->user_conv_p = true;
2660 if (!any_strictly_viable (candidates))
2661 cand->second_conv->bad_p = true;
2662 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2663 ambiguous conversion is no worse than another user-defined
2664 conversion. */
2666 return cand;
2669 /* Build the user conversion sequence. */
2670 conv = build_conv
2671 (ck_user,
2672 (DECL_CONSTRUCTOR_P (cand->fn)
2673 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2674 build_identity_conv (TREE_TYPE (expr), expr));
2675 conv->cand = cand;
2677 /* Combine it with the second conversion sequence. */
2678 cand->second_conv = merge_conversion_sequences (conv,
2679 cand->second_conv);
2681 if (cand->viable == -1)
2682 cand->second_conv->bad_p = true;
2684 return cand;
2687 tree
2688 build_user_type_conversion (tree totype, tree expr, int flags)
2690 struct z_candidate *cand
2691 = build_user_type_conversion_1 (totype, expr, flags);
2693 if (cand)
2695 if (cand->second_conv->kind == ck_ambig)
2696 return error_mark_node;
2697 expr = convert_like (cand->second_conv, expr);
2698 return convert_from_reference (expr);
2700 return NULL_TREE;
2703 /* Do any initial processing on the arguments to a function call. */
2705 static tree
2706 resolve_args (tree args)
2708 tree t;
2709 for (t = args; t; t = TREE_CHAIN (t))
2711 tree arg = TREE_VALUE (t);
2713 if (error_operand_p (arg))
2714 return error_mark_node;
2715 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2717 error ("invalid use of void expression");
2718 return error_mark_node;
2720 else if (invalid_nonstatic_memfn_p (arg))
2721 return error_mark_node;
2723 return args;
2726 /* Perform overload resolution on FN, which is called with the ARGS.
2728 Return the candidate function selected by overload resolution, or
2729 NULL if the event that overload resolution failed. In the case
2730 that overload resolution fails, *CANDIDATES will be the set of
2731 candidates considered, and ANY_VIABLE_P will be set to true or
2732 false to indicate whether or not any of the candidates were
2733 viable.
2735 The ARGS should already have gone through RESOLVE_ARGS before this
2736 function is called. */
2738 static struct z_candidate *
2739 perform_overload_resolution (tree fn,
2740 tree args,
2741 struct z_candidate **candidates,
2742 bool *any_viable_p)
2744 struct z_candidate *cand;
2745 tree explicit_targs = NULL_TREE;
2746 int template_only = 0;
2748 *candidates = NULL;
2749 *any_viable_p = true;
2751 /* Check FN and ARGS. */
2752 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
2753 || TREE_CODE (fn) == TEMPLATE_DECL
2754 || TREE_CODE (fn) == OVERLOAD
2755 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
2756 gcc_assert (!args || TREE_CODE (args) == TREE_LIST);
2758 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2760 explicit_targs = TREE_OPERAND (fn, 1);
2761 fn = TREE_OPERAND (fn, 0);
2762 template_only = 1;
2765 /* Add the various candidate functions. */
2766 add_candidates (fn, args, explicit_targs, template_only,
2767 /*conversion_path=*/NULL_TREE,
2768 /*access_path=*/NULL_TREE,
2769 LOOKUP_NORMAL,
2770 candidates);
2772 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
2773 if (!*any_viable_p)
2774 return NULL;
2776 cand = tourney (*candidates);
2777 return cand;
2780 /* Return an expression for a call to FN (a namespace-scope function,
2781 or a static member function) with the ARGS. */
2783 tree
2784 build_new_function_call (tree fn, tree args, bool koenig_p)
2786 struct z_candidate *candidates, *cand;
2787 bool any_viable_p;
2788 void *p;
2789 tree result;
2791 args = resolve_args (args);
2792 if (args == error_mark_node)
2793 return error_mark_node;
2795 /* If this function was found without using argument dependent
2796 lookup, then we want to ignore any undeclared friend
2797 functions. */
2798 if (!koenig_p)
2800 tree orig_fn = fn;
2802 fn = remove_hidden_names (fn);
2803 if (!fn)
2805 error ("no matching function for call to %<%D(%A)%>",
2806 DECL_NAME (OVL_CURRENT (orig_fn)), args);
2807 return error_mark_node;
2811 /* Get the high-water mark for the CONVERSION_OBSTACK. */
2812 p = conversion_obstack_alloc (0);
2814 cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p);
2816 if (!cand)
2818 if (!any_viable_p && candidates && ! candidates->next)
2819 return build_function_call (candidates->fn, args);
2820 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2821 fn = TREE_OPERAND (fn, 0);
2822 if (!any_viable_p)
2823 error ("no matching function for call to %<%D(%A)%>",
2824 DECL_NAME (OVL_CURRENT (fn)), args);
2825 else
2826 error ("call of overloaded %<%D(%A)%> is ambiguous",
2827 DECL_NAME (OVL_CURRENT (fn)), args);
2828 if (candidates)
2829 print_z_candidates (candidates);
2830 result = error_mark_node;
2832 else
2833 result = build_over_call (cand, LOOKUP_NORMAL);
2835 /* Free all the conversions we allocated. */
2836 obstack_free (&conversion_obstack, p);
2838 return result;
2841 /* Build a call to a global operator new. FNNAME is the name of the
2842 operator (either "operator new" or "operator new[]") and ARGS are
2843 the arguments provided. *SIZE points to the total number of bytes
2844 required by the allocation, and is updated if that is changed here.
2845 *COOKIE_SIZE is non-NULL if a cookie should be used. If this
2846 function determines that no cookie should be used, after all,
2847 *COOKIE_SIZE is set to NULL_TREE. If FN is non-NULL, it will be
2848 set, upon return, to the allocation function called. */
2850 tree
2851 build_operator_new_call (tree fnname, tree args,
2852 tree *size, tree *cookie_size,
2853 tree *fn)
2855 tree fns;
2856 struct z_candidate *candidates;
2857 struct z_candidate *cand;
2858 bool any_viable_p;
2860 if (fn)
2861 *fn = NULL_TREE;
2862 args = tree_cons (NULL_TREE, *size, args);
2863 args = resolve_args (args);
2864 if (args == error_mark_node)
2865 return args;
2867 /* Based on:
2869 [expr.new]
2871 If this lookup fails to find the name, or if the allocated type
2872 is not a class type, the allocation function's name is looked
2873 up in the global scope.
2875 we disregard block-scope declarations of "operator new". */
2876 fns = lookup_function_nonclass (fnname, args, /*block_p=*/false);
2878 /* Figure out what function is being called. */
2879 cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p);
2881 /* If no suitable function could be found, issue an error message
2882 and give up. */
2883 if (!cand)
2885 if (!any_viable_p)
2886 error ("no matching function for call to %<%D(%A)%>",
2887 DECL_NAME (OVL_CURRENT (fns)), args);
2888 else
2889 error ("call of overloaded %<%D(%A)%> is ambiguous",
2890 DECL_NAME (OVL_CURRENT (fns)), args);
2891 if (candidates)
2892 print_z_candidates (candidates);
2893 return error_mark_node;
2896 /* If a cookie is required, add some extra space. Whether
2897 or not a cookie is required cannot be determined until
2898 after we know which function was called. */
2899 if (*cookie_size)
2901 bool use_cookie = true;
2902 if (!abi_version_at_least (2))
2904 tree placement = TREE_CHAIN (args);
2905 /* In G++ 3.2, the check was implemented incorrectly; it
2906 looked at the placement expression, rather than the
2907 type of the function. */
2908 if (placement && !TREE_CHAIN (placement)
2909 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2910 ptr_type_node))
2911 use_cookie = false;
2913 else
2915 tree arg_types;
2917 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
2918 /* Skip the size_t parameter. */
2919 arg_types = TREE_CHAIN (arg_types);
2920 /* Check the remaining parameters (if any). */
2921 if (arg_types
2922 && TREE_CHAIN (arg_types) == void_list_node
2923 && same_type_p (TREE_VALUE (arg_types),
2924 ptr_type_node))
2925 use_cookie = false;
2927 /* If we need a cookie, adjust the number of bytes allocated. */
2928 if (use_cookie)
2930 /* Update the total size. */
2931 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
2932 /* Update the argument list to reflect the adjusted size. */
2933 TREE_VALUE (args) = *size;
2935 else
2936 *cookie_size = NULL_TREE;
2939 /* Tell our caller which function we decided to call. */
2940 if (fn)
2941 *fn = cand->fn;
2943 /* Build the CALL_EXPR. */
2944 return build_over_call (cand, LOOKUP_NORMAL);
2947 static tree
2948 build_object_call (tree obj, tree args)
2950 struct z_candidate *candidates = 0, *cand;
2951 tree fns, convs, mem_args = NULL_TREE;
2952 tree type = TREE_TYPE (obj);
2953 bool any_viable_p;
2954 tree result = NULL_TREE;
2955 void *p;
2957 if (TYPE_PTRMEMFUNC_P (type))
2959 /* It's no good looking for an overloaded operator() on a
2960 pointer-to-member-function. */
2961 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2962 return error_mark_node;
2965 if (TYPE_BINFO (type))
2967 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2968 if (fns == error_mark_node)
2969 return error_mark_node;
2971 else
2972 fns = NULL_TREE;
2974 args = resolve_args (args);
2976 if (args == error_mark_node)
2977 return error_mark_node;
2979 /* Get the high-water mark for the CONVERSION_OBSTACK. */
2980 p = conversion_obstack_alloc (0);
2982 if (fns)
2984 tree base = BINFO_TYPE (BASELINK_BINFO (fns));
2985 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2987 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2989 tree fn = OVL_CURRENT (fns);
2990 if (TREE_CODE (fn) == TEMPLATE_DECL)
2991 add_template_candidate (&candidates, fn, base, NULL_TREE,
2992 mem_args, NULL_TREE,
2993 TYPE_BINFO (type),
2994 TYPE_BINFO (type),
2995 LOOKUP_NORMAL, DEDUCE_CALL);
2996 else
2997 add_function_candidate
2998 (&candidates, fn, base, mem_args, TYPE_BINFO (type),
2999 TYPE_BINFO (type), LOOKUP_NORMAL);
3003 convs = lookup_conversions (type);
3005 for (; convs; convs = TREE_CHAIN (convs))
3007 tree fns = TREE_VALUE (convs);
3008 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
3010 if ((TREE_CODE (totype) == POINTER_TYPE
3011 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
3012 || (TREE_CODE (totype) == REFERENCE_TYPE
3013 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
3014 || (TREE_CODE (totype) == REFERENCE_TYPE
3015 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
3016 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
3017 for (; fns; fns = OVL_NEXT (fns))
3019 tree fn = OVL_CURRENT (fns);
3020 if (TREE_CODE (fn) == TEMPLATE_DECL)
3021 add_template_conv_candidate
3022 (&candidates, fn, obj, args, totype,
3023 /*access_path=*/NULL_TREE,
3024 /*conversion_path=*/NULL_TREE);
3025 else
3026 add_conv_candidate (&candidates, fn, obj, args,
3027 /*conversion_path=*/NULL_TREE,
3028 /*access_path=*/NULL_TREE);
3032 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3033 if (!any_viable_p)
3035 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj), args);
3036 print_z_candidates (candidates);
3037 result = error_mark_node;
3039 else
3041 cand = tourney (candidates);
3042 if (cand == 0)
3044 error ("call of %<(%T) (%A)%> is ambiguous", TREE_TYPE (obj), args);
3045 print_z_candidates (candidates);
3046 result = error_mark_node;
3048 /* Since cand->fn will be a type, not a function, for a conversion
3049 function, we must be careful not to unconditionally look at
3050 DECL_NAME here. */
3051 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
3052 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
3053 result = build_over_call (cand, LOOKUP_NORMAL);
3054 else
3056 obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1);
3057 obj = convert_from_reference (obj);
3058 result = build_function_call (obj, args);
3062 /* Free all the conversions we allocated. */
3063 obstack_free (&conversion_obstack, p);
3065 return result;
3068 static void
3069 op_error (enum tree_code code, enum tree_code code2,
3070 tree arg1, tree arg2, tree arg3, const char *problem)
3072 const char *opname;
3074 if (code == MODIFY_EXPR)
3075 opname = assignment_operator_name_info[code2].name;
3076 else
3077 opname = operator_name_info[code].name;
3079 switch (code)
3081 case COND_EXPR:
3082 error ("%s for ternary %<operator?:%> in %<%E ? %E : %E%>",
3083 problem, arg1, arg2, arg3);
3084 break;
3086 case POSTINCREMENT_EXPR:
3087 case POSTDECREMENT_EXPR:
3088 error ("%s for %<operator%s%> in %<%E%s%>", problem, opname, arg1, opname);
3089 break;
3091 case ARRAY_REF:
3092 error ("%s for %<operator[]%> in %<%E[%E]%>", problem, arg1, arg2);
3093 break;
3095 case REALPART_EXPR:
3096 case IMAGPART_EXPR:
3097 error ("%s for %qs in %<%s %E%>", problem, opname, opname, arg1);
3098 break;
3100 default:
3101 if (arg2)
3102 error ("%s for %<operator%s%> in %<%E %s %E%>",
3103 problem, opname, arg1, opname, arg2);
3104 else
3105 error ("%s for %<operator%s%> in %<%s%E%>",
3106 problem, opname, opname, arg1);
3107 break;
3111 /* Return the implicit conversion sequence that could be used to
3112 convert E1 to E2 in [expr.cond]. */
3114 static conversion *
3115 conditional_conversion (tree e1, tree e2)
3117 tree t1 = non_reference (TREE_TYPE (e1));
3118 tree t2 = non_reference (TREE_TYPE (e2));
3119 conversion *conv;
3120 bool good_base;
3122 /* [expr.cond]
3124 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
3125 implicitly converted (clause _conv_) to the type "reference to
3126 T2", subject to the constraint that in the conversion the
3127 reference must bind directly (_dcl.init.ref_) to E1. */
3128 if (real_lvalue_p (e2))
3130 conv = implicit_conversion (build_reference_type (t2),
3133 /*c_cast_p=*/false,
3134 LOOKUP_NO_TEMP_BIND);
3135 if (conv)
3136 return conv;
3139 /* [expr.cond]
3141 If E1 and E2 have class type, and the underlying class types are
3142 the same or one is a base class of the other: E1 can be converted
3143 to match E2 if the class of T2 is the same type as, or a base
3144 class of, the class of T1, and the cv-qualification of T2 is the
3145 same cv-qualification as, or a greater cv-qualification than, the
3146 cv-qualification of T1. If the conversion is applied, E1 is
3147 changed to an rvalue of type T2 that still refers to the original
3148 source class object (or the appropriate subobject thereof). */
3149 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
3150 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
3152 if (good_base && at_least_as_qualified_p (t2, t1))
3154 conv = build_identity_conv (t1, e1);
3155 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
3156 TYPE_MAIN_VARIANT (t2)))
3157 conv = build_conv (ck_base, t2, conv);
3158 else
3159 conv = build_conv (ck_rvalue, t2, conv);
3160 return conv;
3162 else
3163 return NULL;
3165 else
3166 /* [expr.cond]
3168 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
3169 converted to the type that expression E2 would have if E2 were
3170 converted to an rvalue (or the type it has, if E2 is an rvalue). */
3171 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
3172 LOOKUP_NORMAL);
3175 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
3176 arguments to the conditional expression. */
3178 tree
3179 build_conditional_expr (tree arg1, tree arg2, tree arg3)
3181 tree arg2_type;
3182 tree arg3_type;
3183 tree result = NULL_TREE;
3184 tree result_type = NULL_TREE;
3185 bool lvalue_p = true;
3186 struct z_candidate *candidates = 0;
3187 struct z_candidate *cand;
3188 void *p;
3190 /* As a G++ extension, the second argument to the conditional can be
3191 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
3192 c'.) If the second operand is omitted, make sure it is
3193 calculated only once. */
3194 if (!arg2)
3196 if (pedantic)
3197 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
3199 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
3200 if (real_lvalue_p (arg1))
3201 arg2 = arg1 = stabilize_reference (arg1);
3202 else
3203 arg2 = arg1 = save_expr (arg1);
3206 /* [expr.cond]
3208 The first expr ession is implicitly converted to bool (clause
3209 _conv_). */
3210 arg1 = perform_implicit_conversion (boolean_type_node, arg1);
3212 /* If something has already gone wrong, just pass that fact up the
3213 tree. */
3214 if (error_operand_p (arg1)
3215 || error_operand_p (arg2)
3216 || error_operand_p (arg3))
3217 return error_mark_node;
3219 /* [expr.cond]
3221 If either the second or the third operand has type (possibly
3222 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3223 array-to-pointer (_conv.array_), and function-to-pointer
3224 (_conv.func_) standard conversions are performed on the second
3225 and third operands. */
3226 arg2_type = is_bitfield_expr_with_lowered_type (arg2);
3227 if (!arg2_type)
3228 arg2_type = TREE_TYPE (arg2);
3229 arg3_type = is_bitfield_expr_with_lowered_type (arg3);
3230 if (!arg3_type)
3231 arg3_type = TREE_TYPE (arg3);
3232 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3234 /* Do the conversions. We don't these for `void' type arguments
3235 since it can't have any effect and since decay_conversion
3236 does not handle that case gracefully. */
3237 if (!VOID_TYPE_P (arg2_type))
3238 arg2 = decay_conversion (arg2);
3239 if (!VOID_TYPE_P (arg3_type))
3240 arg3 = decay_conversion (arg3);
3241 arg2_type = TREE_TYPE (arg2);
3242 arg3_type = TREE_TYPE (arg3);
3244 /* [expr.cond]
3246 One of the following shall hold:
3248 --The second or the third operand (but not both) is a
3249 throw-expression (_except.throw_); the result is of the
3250 type of the other and is an rvalue.
3252 --Both the second and the third operands have type void; the
3253 result is of type void and is an rvalue.
3255 We must avoid calling force_rvalue for expressions of type
3256 "void" because it will complain that their value is being
3257 used. */
3258 if (TREE_CODE (arg2) == THROW_EXPR
3259 && TREE_CODE (arg3) != THROW_EXPR)
3261 if (!VOID_TYPE_P (arg3_type))
3262 arg3 = force_rvalue (arg3);
3263 arg3_type = TREE_TYPE (arg3);
3264 result_type = arg3_type;
3266 else if (TREE_CODE (arg2) != THROW_EXPR
3267 && TREE_CODE (arg3) == THROW_EXPR)
3269 if (!VOID_TYPE_P (arg2_type))
3270 arg2 = force_rvalue (arg2);
3271 arg2_type = TREE_TYPE (arg2);
3272 result_type = arg2_type;
3274 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3275 result_type = void_type_node;
3276 else
3278 error ("%qE has type %<void%> and is not a throw-expression",
3279 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3280 return error_mark_node;
3283 lvalue_p = false;
3284 goto valid_operands;
3286 /* [expr.cond]
3288 Otherwise, if the second and third operand have different types,
3289 and either has (possibly cv-qualified) class type, an attempt is
3290 made to convert each of those operands to the type of the other. */
3291 else if (!same_type_p (arg2_type, arg3_type)
3292 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3294 conversion *conv2;
3295 conversion *conv3;
3297 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3298 p = conversion_obstack_alloc (0);
3300 conv2 = conditional_conversion (arg2, arg3);
3301 conv3 = conditional_conversion (arg3, arg2);
3303 /* [expr.cond]
3305 If both can be converted, or one can be converted but the
3306 conversion is ambiguous, the program is ill-formed. If
3307 neither can be converted, the operands are left unchanged and
3308 further checking is performed as described below. If exactly
3309 one conversion is possible, that conversion is applied to the
3310 chosen operand and the converted operand is used in place of
3311 the original operand for the remainder of this section. */
3312 if ((conv2 && !conv2->bad_p
3313 && conv3 && !conv3->bad_p)
3314 || (conv2 && conv2->kind == ck_ambig)
3315 || (conv3 && conv3->kind == ck_ambig))
3317 error ("operands to ?: have different types %qT and %qT",
3318 arg2_type, arg3_type);
3319 result = error_mark_node;
3321 else if (conv2 && (!conv2->bad_p || !conv3))
3323 arg2 = convert_like (conv2, arg2);
3324 arg2 = convert_from_reference (arg2);
3325 arg2_type = TREE_TYPE (arg2);
3326 /* Even if CONV2 is a valid conversion, the result of the
3327 conversion may be invalid. For example, if ARG3 has type
3328 "volatile X", and X does not have a copy constructor
3329 accepting a "volatile X&", then even if ARG2 can be
3330 converted to X, the conversion will fail. */
3331 if (error_operand_p (arg2))
3332 result = error_mark_node;
3334 else if (conv3 && (!conv3->bad_p || !conv2))
3336 arg3 = convert_like (conv3, arg3);
3337 arg3 = convert_from_reference (arg3);
3338 arg3_type = TREE_TYPE (arg3);
3339 if (error_operand_p (arg3))
3340 result = error_mark_node;
3343 /* Free all the conversions we allocated. */
3344 obstack_free (&conversion_obstack, p);
3346 if (result)
3347 return result;
3349 /* If, after the conversion, both operands have class type,
3350 treat the cv-qualification of both operands as if it were the
3351 union of the cv-qualification of the operands.
3353 The standard is not clear about what to do in this
3354 circumstance. For example, if the first operand has type
3355 "const X" and the second operand has a user-defined
3356 conversion to "volatile X", what is the type of the second
3357 operand after this step? Making it be "const X" (matching
3358 the first operand) seems wrong, as that discards the
3359 qualification without actually performing a copy. Leaving it
3360 as "volatile X" seems wrong as that will result in the
3361 conditional expression failing altogether, even though,
3362 according to this step, the one operand could be converted to
3363 the type of the other. */
3364 if ((conv2 || conv3)
3365 && CLASS_TYPE_P (arg2_type)
3366 && TYPE_QUALS (arg2_type) != TYPE_QUALS (arg3_type))
3367 arg2_type = arg3_type =
3368 cp_build_qualified_type (arg2_type,
3369 TYPE_QUALS (arg2_type)
3370 | TYPE_QUALS (arg3_type));
3373 /* [expr.cond]
3375 If the second and third operands are lvalues and have the same
3376 type, the result is of that type and is an lvalue. */
3377 if (real_lvalue_p (arg2)
3378 && real_lvalue_p (arg3)
3379 && same_type_p (arg2_type, arg3_type))
3381 result_type = arg2_type;
3382 goto valid_operands;
3385 /* [expr.cond]
3387 Otherwise, the result is an rvalue. If the second and third
3388 operand do not have the same type, and either has (possibly
3389 cv-qualified) class type, overload resolution is used to
3390 determine the conversions (if any) to be applied to the operands
3391 (_over.match.oper_, _over.built_). */
3392 lvalue_p = false;
3393 if (!same_type_p (arg2_type, arg3_type)
3394 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3396 tree args[3];
3397 conversion *conv;
3398 bool any_viable_p;
3400 /* Rearrange the arguments so that add_builtin_candidate only has
3401 to know about two args. In build_builtin_candidates, the
3402 arguments are unscrambled. */
3403 args[0] = arg2;
3404 args[1] = arg3;
3405 args[2] = arg1;
3406 add_builtin_candidates (&candidates,
3407 COND_EXPR,
3408 NOP_EXPR,
3409 ansi_opname (COND_EXPR),
3410 args,
3411 LOOKUP_NORMAL);
3413 /* [expr.cond]
3415 If the overload resolution fails, the program is
3416 ill-formed. */
3417 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3418 if (!any_viable_p)
3420 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3421 print_z_candidates (candidates);
3422 return error_mark_node;
3424 cand = tourney (candidates);
3425 if (!cand)
3427 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3428 print_z_candidates (candidates);
3429 return error_mark_node;
3432 /* [expr.cond]
3434 Otherwise, the conversions thus determined are applied, and
3435 the converted operands are used in place of the original
3436 operands for the remainder of this section. */
3437 conv = cand->convs[0];
3438 arg1 = convert_like (conv, arg1);
3439 conv = cand->convs[1];
3440 arg2 = convert_like (conv, arg2);
3441 conv = cand->convs[2];
3442 arg3 = convert_like (conv, arg3);
3445 /* [expr.cond]
3447 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3448 and function-to-pointer (_conv.func_) standard conversions are
3449 performed on the second and third operands.
3451 We need to force the lvalue-to-rvalue conversion here for class types,
3452 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3453 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3454 regions. */
3456 arg2 = force_rvalue (arg2);
3457 if (!CLASS_TYPE_P (arg2_type))
3458 arg2_type = TREE_TYPE (arg2);
3460 arg3 = force_rvalue (arg3);
3461 if (!CLASS_TYPE_P (arg2_type))
3462 arg3_type = TREE_TYPE (arg3);
3464 if (arg2 == error_mark_node || arg3 == error_mark_node)
3465 return error_mark_node;
3467 /* [expr.cond]
3469 After those conversions, one of the following shall hold:
3471 --The second and third operands have the same type; the result is of
3472 that type. */
3473 if (same_type_p (arg2_type, arg3_type))
3474 result_type = arg2_type;
3475 /* [expr.cond]
3477 --The second and third operands have arithmetic or enumeration
3478 type; the usual arithmetic conversions are performed to bring
3479 them to a common type, and the result is of that type. */
3480 else if ((ARITHMETIC_TYPE_P (arg2_type)
3481 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3482 && (ARITHMETIC_TYPE_P (arg3_type)
3483 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3485 /* In this case, there is always a common type. */
3486 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3487 arg3_type);
3489 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3490 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3491 warning (0, "enumeral mismatch in conditional expression: %qT vs %qT",
3492 arg2_type, arg3_type);
3493 else if (extra_warnings
3494 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3495 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3496 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3497 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3498 warning (0, "enumeral and non-enumeral type in conditional expression");
3500 arg2 = perform_implicit_conversion (result_type, arg2);
3501 arg3 = perform_implicit_conversion (result_type, arg3);
3503 /* [expr.cond]
3505 --The second and third operands have pointer type, or one has
3506 pointer type and the other is a null pointer constant; pointer
3507 conversions (_conv.ptr_) and qualification conversions
3508 (_conv.qual_) are performed to bring them to their composite
3509 pointer type (_expr.rel_). The result is of the composite
3510 pointer type.
3512 --The second and third operands have pointer to member type, or
3513 one has pointer to member type and the other is a null pointer
3514 constant; pointer to member conversions (_conv.mem_) and
3515 qualification conversions (_conv.qual_) are performed to bring
3516 them to a common type, whose cv-qualification shall match the
3517 cv-qualification of either the second or the third operand.
3518 The result is of the common type. */
3519 else if ((null_ptr_cst_p (arg2)
3520 && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
3521 || (null_ptr_cst_p (arg3)
3522 && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
3523 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3524 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3525 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
3527 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3528 arg3, "conditional expression");
3529 if (result_type == error_mark_node)
3530 return error_mark_node;
3531 arg2 = perform_implicit_conversion (result_type, arg2);
3532 arg3 = perform_implicit_conversion (result_type, arg3);
3535 if (!result_type)
3537 error ("operands to ?: have different types %qT and %qT",
3538 arg2_type, arg3_type);
3539 return error_mark_node;
3542 valid_operands:
3543 result = fold_if_not_in_template (build3 (COND_EXPR, result_type, arg1,
3544 arg2, arg3));
3545 /* We can't use result_type below, as fold might have returned a
3546 throw_expr. */
3548 if (!lvalue_p)
3550 /* Expand both sides into the same slot, hopefully the target of
3551 the ?: expression. We used to check for TARGET_EXPRs here,
3552 but now we sometimes wrap them in NOP_EXPRs so the test would
3553 fail. */
3554 if (CLASS_TYPE_P (TREE_TYPE (result)))
3555 result = get_target_expr (result);
3556 /* If this expression is an rvalue, but might be mistaken for an
3557 lvalue, we must add a NON_LVALUE_EXPR. */
3558 result = rvalue (result);
3561 return result;
3564 /* OPERAND is an operand to an expression. Perform necessary steps
3565 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
3566 returned. */
3568 static tree
3569 prep_operand (tree operand)
3571 if (operand)
3573 if (CLASS_TYPE_P (TREE_TYPE (operand))
3574 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
3575 /* Make sure the template type is instantiated now. */
3576 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
3579 return operand;
3582 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
3583 OVERLOAD) to the CANDIDATES, returning an updated list of
3584 CANDIDATES. The ARGS are the arguments provided to the call,
3585 without any implicit object parameter. The EXPLICIT_TARGS are
3586 explicit template arguments provided. TEMPLATE_ONLY is true if
3587 only template functions should be considered. CONVERSION_PATH,
3588 ACCESS_PATH, and FLAGS are as for add_function_candidate. */
3590 static void
3591 add_candidates (tree fns, tree args,
3592 tree explicit_targs, bool template_only,
3593 tree conversion_path, tree access_path,
3594 int flags,
3595 struct z_candidate **candidates)
3597 tree ctype;
3598 tree non_static_args;
3600 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
3601 /* Delay creating the implicit this parameter until it is needed. */
3602 non_static_args = NULL_TREE;
3604 while (fns)
3606 tree fn;
3607 tree fn_args;
3609 fn = OVL_CURRENT (fns);
3610 /* Figure out which set of arguments to use. */
3611 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3613 /* If this function is a non-static member, prepend the implicit
3614 object parameter. */
3615 if (!non_static_args)
3616 non_static_args = tree_cons (NULL_TREE,
3617 build_this (TREE_VALUE (args)),
3618 TREE_CHAIN (args));
3619 fn_args = non_static_args;
3621 else
3622 /* Otherwise, just use the list of arguments provided. */
3623 fn_args = args;
3625 if (TREE_CODE (fn) == TEMPLATE_DECL)
3626 add_template_candidate (candidates,
3628 ctype,
3629 explicit_targs,
3630 fn_args,
3631 NULL_TREE,
3632 access_path,
3633 conversion_path,
3634 flags,
3635 DEDUCE_CALL);
3636 else if (!template_only)
3637 add_function_candidate (candidates,
3639 ctype,
3640 fn_args,
3641 access_path,
3642 conversion_path,
3643 flags);
3644 fns = OVL_NEXT (fns);
3648 tree
3649 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
3650 bool *overloaded_p)
3652 struct z_candidate *candidates = 0, *cand;
3653 tree arglist, fnname;
3654 tree args[3];
3655 tree result = NULL_TREE;
3656 bool result_valid_p = false;
3657 enum tree_code code2 = NOP_EXPR;
3658 conversion *conv;
3659 void *p;
3660 bool strict_p;
3661 bool any_viable_p;
3663 if (error_operand_p (arg1)
3664 || error_operand_p (arg2)
3665 || error_operand_p (arg3))
3666 return error_mark_node;
3668 if (code == MODIFY_EXPR)
3670 code2 = TREE_CODE (arg3);
3671 arg3 = NULL_TREE;
3672 fnname = ansi_assopname (code2);
3674 else
3675 fnname = ansi_opname (code);
3677 arg1 = prep_operand (arg1);
3679 switch (code)
3681 case NEW_EXPR:
3682 case VEC_NEW_EXPR:
3683 case VEC_DELETE_EXPR:
3684 case DELETE_EXPR:
3685 /* Use build_op_new_call and build_op_delete_call instead. */
3686 gcc_unreachable ();
3688 case CALL_EXPR:
3689 return build_object_call (arg1, arg2);
3691 default:
3692 break;
3695 arg2 = prep_operand (arg2);
3696 arg3 = prep_operand (arg3);
3698 if (code == COND_EXPR)
3700 if (arg2 == NULL_TREE
3701 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3702 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3703 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3704 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3705 goto builtin;
3707 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3708 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3709 goto builtin;
3711 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3712 arg2 = integer_zero_node;
3714 arglist = NULL_TREE;
3715 if (arg3)
3716 arglist = tree_cons (NULL_TREE, arg3, arglist);
3717 if (arg2)
3718 arglist = tree_cons (NULL_TREE, arg2, arglist);
3719 arglist = tree_cons (NULL_TREE, arg1, arglist);
3721 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3722 p = conversion_obstack_alloc (0);
3724 /* Add namespace-scope operators to the list of functions to
3725 consider. */
3726 add_candidates (lookup_function_nonclass (fnname, arglist, /*block_p=*/true),
3727 arglist, NULL_TREE, false, NULL_TREE, NULL_TREE,
3728 flags, &candidates);
3729 /* Add class-member operators to the candidate set. */
3730 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
3732 tree fns;
3734 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
3735 if (fns == error_mark_node)
3737 result = error_mark_node;
3738 goto user_defined_result_ready;
3740 if (fns)
3741 add_candidates (BASELINK_FUNCTIONS (fns), arglist,
3742 NULL_TREE, false,
3743 BASELINK_BINFO (fns),
3744 TYPE_BINFO (TREE_TYPE (arg1)),
3745 flags, &candidates);
3748 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3749 to know about two args; a builtin candidate will always have a first
3750 parameter of type bool. We'll handle that in
3751 build_builtin_candidate. */
3752 if (code == COND_EXPR)
3754 args[0] = arg2;
3755 args[1] = arg3;
3756 args[2] = arg1;
3758 else
3760 args[0] = arg1;
3761 args[1] = arg2;
3762 args[2] = NULL_TREE;
3765 add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
3767 switch (code)
3769 case COMPOUND_EXPR:
3770 case ADDR_EXPR:
3771 /* For these, the built-in candidates set is empty
3772 [over.match.oper]/3. We don't want non-strict matches
3773 because exact matches are always possible with built-in
3774 operators. The built-in candidate set for COMPONENT_REF
3775 would be empty too, but since there are no such built-in
3776 operators, we accept non-strict matches for them. */
3777 strict_p = true;
3778 break;
3780 default:
3781 strict_p = pedantic;
3782 break;
3785 candidates = splice_viable (candidates, strict_p, &any_viable_p);
3786 if (!any_viable_p)
3788 switch (code)
3790 case POSTINCREMENT_EXPR:
3791 case POSTDECREMENT_EXPR:
3792 /* Look for an `operator++ (int)'. If they didn't have
3793 one, then we fall back to the old way of doing things. */
3794 if (flags & LOOKUP_COMPLAIN)
3795 pedwarn ("no %<%D(int)%> declared for postfix %qs, "
3796 "trying prefix operator instead",
3797 fnname,
3798 operator_name_info[code].name);
3799 if (code == POSTINCREMENT_EXPR)
3800 code = PREINCREMENT_EXPR;
3801 else
3802 code = PREDECREMENT_EXPR;
3803 result = build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE,
3804 overloaded_p);
3805 break;
3807 /* The caller will deal with these. */
3808 case ADDR_EXPR:
3809 case COMPOUND_EXPR:
3810 case COMPONENT_REF:
3811 result = NULL_TREE;
3812 result_valid_p = true;
3813 break;
3815 default:
3816 if (flags & LOOKUP_COMPLAIN)
3818 op_error (code, code2, arg1, arg2, arg3, "no match");
3819 print_z_candidates (candidates);
3821 result = error_mark_node;
3822 break;
3825 else
3827 cand = tourney (candidates);
3828 if (cand == 0)
3830 if (flags & LOOKUP_COMPLAIN)
3832 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3833 print_z_candidates (candidates);
3835 result = error_mark_node;
3837 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3839 if (overloaded_p)
3840 *overloaded_p = true;
3842 result = build_over_call (cand, LOOKUP_NORMAL);
3844 else
3846 /* Give any warnings we noticed during overload resolution. */
3847 if (cand->warnings)
3849 struct candidate_warning *w;
3850 for (w = cand->warnings; w; w = w->next)
3851 joust (cand, w->loser, 1);
3854 /* Check for comparison of different enum types. */
3855 switch (code)
3857 case GT_EXPR:
3858 case LT_EXPR:
3859 case GE_EXPR:
3860 case LE_EXPR:
3861 case EQ_EXPR:
3862 case NE_EXPR:
3863 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3864 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3865 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3866 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3868 warning (0, "comparison between %q#T and %q#T",
3869 TREE_TYPE (arg1), TREE_TYPE (arg2));
3871 break;
3872 default:
3873 break;
3876 /* We need to strip any leading REF_BIND so that bitfields
3877 don't cause errors. This should not remove any important
3878 conversions, because builtins don't apply to class
3879 objects directly. */
3880 conv = cand->convs[0];
3881 if (conv->kind == ck_ref_bind)
3882 conv = conv->u.next;
3883 arg1 = convert_like (conv, arg1);
3884 if (arg2)
3886 conv = cand->convs[1];
3887 if (conv->kind == ck_ref_bind)
3888 conv = conv->u.next;
3889 arg2 = convert_like (conv, arg2);
3891 if (arg3)
3893 conv = cand->convs[2];
3894 if (conv->kind == ck_ref_bind)
3895 conv = conv->u.next;
3896 arg3 = convert_like (conv, arg3);
3901 user_defined_result_ready:
3903 /* Free all the conversions we allocated. */
3904 obstack_free (&conversion_obstack, p);
3906 if (result || result_valid_p)
3907 return result;
3909 builtin:
3910 switch (code)
3912 case MODIFY_EXPR:
3913 return build_modify_expr (arg1, code2, arg2);
3915 case INDIRECT_REF:
3916 return build_indirect_ref (arg1, "unary *");
3918 case PLUS_EXPR:
3919 case MINUS_EXPR:
3920 case MULT_EXPR:
3921 case TRUNC_DIV_EXPR:
3922 case GT_EXPR:
3923 case LT_EXPR:
3924 case GE_EXPR:
3925 case LE_EXPR:
3926 case EQ_EXPR:
3927 case NE_EXPR:
3928 case MAX_EXPR:
3929 case MIN_EXPR:
3930 case LSHIFT_EXPR:
3931 case RSHIFT_EXPR:
3932 case TRUNC_MOD_EXPR:
3933 case BIT_AND_EXPR:
3934 case BIT_IOR_EXPR:
3935 case BIT_XOR_EXPR:
3936 case TRUTH_ANDIF_EXPR:
3937 case TRUTH_ORIF_EXPR:
3938 return cp_build_binary_op (code, arg1, arg2);
3940 case UNARY_PLUS_EXPR:
3941 case NEGATE_EXPR:
3942 case BIT_NOT_EXPR:
3943 case TRUTH_NOT_EXPR:
3944 case PREINCREMENT_EXPR:
3945 case POSTINCREMENT_EXPR:
3946 case PREDECREMENT_EXPR:
3947 case POSTDECREMENT_EXPR:
3948 case REALPART_EXPR:
3949 case IMAGPART_EXPR:
3950 return build_unary_op (code, arg1, candidates != 0);
3952 case ARRAY_REF:
3953 return build_array_ref (arg1, arg2);
3955 case COND_EXPR:
3956 return build_conditional_expr (arg1, arg2, arg3);
3958 case MEMBER_REF:
3959 return build_m_component_ref (build_indirect_ref (arg1, NULL), arg2);
3961 /* The caller will deal with these. */
3962 case ADDR_EXPR:
3963 case COMPONENT_REF:
3964 case COMPOUND_EXPR:
3965 return NULL_TREE;
3967 default:
3968 gcc_unreachable ();
3970 return NULL_TREE;
3973 /* Build a call to operator delete. This has to be handled very specially,
3974 because the restrictions on what signatures match are different from all
3975 other call instances. For a normal delete, only a delete taking (void *)
3976 or (void *, size_t) is accepted. For a placement delete, only an exact
3977 match with the placement new is accepted.
3979 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3980 ADDR is the pointer to be deleted.
3981 SIZE is the size of the memory block to be deleted.
3982 GLOBAL_P is true if the delete-expression should not consider
3983 class-specific delete operators.
3984 PLACEMENT is the corresponding placement new call, or NULL_TREE.
3985 If PLACEMENT is non-NULL, then ALLOC_FN is the allocation function
3986 called to perform the placement new. */
3988 tree
3989 build_op_delete_call (enum tree_code code, tree addr, tree size,
3990 bool global_p, tree placement,
3991 tree alloc_fn)
3993 tree fn = NULL_TREE;
3994 tree fns, fnname, argtypes, args, type;
3995 int pass;
3997 if (addr == error_mark_node)
3998 return error_mark_node;
4000 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
4002 fnname = ansi_opname (code);
4004 if (CLASS_TYPE_P (type)
4005 && COMPLETE_TYPE_P (complete_type (type))
4006 && !global_p)
4007 /* In [class.free]
4009 If the result of the lookup is ambiguous or inaccessible, or if
4010 the lookup selects a placement deallocation function, the
4011 program is ill-formed.
4013 Therefore, we ask lookup_fnfields to complain about ambiguity. */
4015 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
4016 if (fns == error_mark_node)
4017 return error_mark_node;
4019 else
4020 fns = NULL_TREE;
4022 if (fns == NULL_TREE)
4023 fns = lookup_name_nonclass (fnname);
4025 if (placement)
4027 /* Get the parameter types for the allocation function that is
4028 being called. */
4029 gcc_assert (alloc_fn != NULL_TREE);
4030 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
4031 /* Also the second argument. */
4032 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
4034 else
4036 /* First try it without the size argument. */
4037 argtypes = void_list_node;
4038 args = NULL_TREE;
4041 /* Strip const and volatile from addr. */
4042 addr = cp_convert (ptr_type_node, addr);
4044 /* We make two tries at finding a matching `operator delete'. On
4045 the first pass, we look for a one-operator (or placement)
4046 operator delete. If we're not doing placement delete, then on
4047 the second pass we look for a two-argument delete. */
4048 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
4050 /* Go through the `operator delete' functions looking for one
4051 with a matching type. */
4052 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
4054 fn = OVL_NEXT (fn))
4056 tree t;
4058 /* The first argument must be "void *". */
4059 t = TYPE_ARG_TYPES (TREE_TYPE (OVL_CURRENT (fn)));
4060 if (!same_type_p (TREE_VALUE (t), ptr_type_node))
4061 continue;
4062 t = TREE_CHAIN (t);
4063 /* On the first pass, check the rest of the arguments. */
4064 if (pass == 0)
4066 tree a = argtypes;
4067 while (a && t)
4069 if (!same_type_p (TREE_VALUE (a), TREE_VALUE (t)))
4070 break;
4071 a = TREE_CHAIN (a);
4072 t = TREE_CHAIN (t);
4074 if (!a && !t)
4075 break;
4077 /* On the second pass, the second argument must be
4078 "size_t". */
4079 else if (pass == 1
4080 && same_type_p (TREE_VALUE (t), sizetype)
4081 && TREE_CHAIN (t) == void_list_node)
4082 break;
4085 /* If we found a match, we're done. */
4086 if (fn)
4087 break;
4090 /* If we have a matching function, call it. */
4091 if (fn)
4093 /* Make sure we have the actual function, and not an
4094 OVERLOAD. */
4095 fn = OVL_CURRENT (fn);
4097 /* If the FN is a member function, make sure that it is
4098 accessible. */
4099 if (DECL_CLASS_SCOPE_P (fn))
4100 perform_or_defer_access_check (TYPE_BINFO (type), fn);
4102 if (pass == 0)
4103 args = tree_cons (NULL_TREE, addr, args);
4104 else
4105 args = tree_cons (NULL_TREE, addr,
4106 build_tree_list (NULL_TREE, size));
4108 if (placement)
4110 /* The placement args might not be suitable for overload
4111 resolution at this point, so build the call directly. */
4112 mark_used (fn);
4113 return build_cxx_call (fn, args);
4115 else
4116 return build_function_call (fn, args);
4119 /* If we are doing placement delete we do nothing if we don't find a
4120 matching op delete. */
4121 if (placement)
4122 return NULL_TREE;
4124 error ("no suitable %<operator %s%> for %qT",
4125 operator_name_info[(int)code].name, type);
4126 return error_mark_node;
4129 /* If the current scope isn't allowed to access DECL along
4130 BASETYPE_PATH, give an error. The most derived class in
4131 BASETYPE_PATH is the one used to qualify DECL. */
4133 bool
4134 enforce_access (tree basetype_path, tree decl)
4136 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
4138 if (!accessible_p (basetype_path, decl, true))
4140 if (TREE_PRIVATE (decl))
4141 error ("%q+#D is private", decl);
4142 else if (TREE_PROTECTED (decl))
4143 error ("%q+#D is protected", decl);
4144 else
4145 error ("%q+#D is inaccessible", decl);
4146 error ("within this context");
4147 return false;
4150 return true;
4153 /* Check that a callable constructor to initialize a temporary of
4154 TYPE from an EXPR exists. */
4156 static void
4157 check_constructor_callable (tree type, tree expr)
4159 build_special_member_call (NULL_TREE,
4160 complete_ctor_identifier,
4161 build_tree_list (NULL_TREE, expr),
4162 type,
4163 LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING
4164 | LOOKUP_NO_CONVERSION
4165 | LOOKUP_CONSTRUCTOR_CALLABLE);
4168 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
4169 bitwise or of LOOKUP_* values. If any errors are warnings are
4170 generated, set *DIAGNOSTIC_FN to "error" or "warning",
4171 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
4172 to NULL. */
4174 static tree
4175 build_temp (tree expr, tree type, int flags,
4176 diagnostic_fn_t *diagnostic_fn)
4178 int savew, savee;
4180 savew = warningcount, savee = errorcount;
4181 expr = build_special_member_call (NULL_TREE,
4182 complete_ctor_identifier,
4183 build_tree_list (NULL_TREE, expr),
4184 type, flags);
4185 if (warningcount > savew)
4186 *diagnostic_fn = warning0;
4187 else if (errorcount > savee)
4188 *diagnostic_fn = error;
4189 else
4190 *diagnostic_fn = NULL;
4191 return expr;
4195 /* Perform the conversions in CONVS on the expression EXPR. FN and
4196 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
4197 indicates the `this' argument of a method. INNER is nonzero when
4198 being called to continue a conversion chain. It is negative when a
4199 reference binding will be applied, positive otherwise. If
4200 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
4201 conversions will be emitted if appropriate. If C_CAST_P is true,
4202 this conversion is coming from a C-style cast; in that case,
4203 conversions to inaccessible bases are permitted. */
4205 static tree
4206 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
4207 int inner, bool issue_conversion_warnings,
4208 bool c_cast_p)
4210 tree totype = convs->type;
4211 diagnostic_fn_t diagnostic_fn;
4213 if (convs->bad_p
4214 && convs->kind != ck_user
4215 && convs->kind != ck_ambig
4216 && convs->kind != ck_ref_bind)
4218 conversion *t = convs;
4219 for (; t; t = convs->u.next)
4221 if (t->kind == ck_user || !t->bad_p)
4223 expr = convert_like_real (t, expr, fn, argnum, 1,
4224 /*issue_conversion_warnings=*/false,
4225 /*c_cast_p=*/false);
4226 break;
4228 else if (t->kind == ck_ambig)
4229 return convert_like_real (t, expr, fn, argnum, 1,
4230 /*issue_conversion_warnings=*/false,
4231 /*c_cast_p=*/false);
4232 else if (t->kind == ck_identity)
4233 break;
4235 pedwarn ("invalid conversion from %qT to %qT", TREE_TYPE (expr), totype);
4236 if (fn)
4237 pedwarn (" initializing argument %P of %qD", argnum, fn);
4238 return cp_convert (totype, expr);
4241 if (issue_conversion_warnings)
4243 tree t = non_reference (totype);
4245 /* Issue warnings about peculiar, but valid, uses of NULL. */
4246 if (ARITHMETIC_TYPE_P (t) && expr == null_node)
4248 if (fn)
4249 warning (OPT_Wconversion, "passing NULL to non-pointer argument %P of %qD",
4250 argnum, fn);
4251 else
4252 warning (OPT_Wconversion, "converting to non-pointer type %qT from NULL", t);
4255 /* Warn about assigning a floating-point type to an integer type. */
4256 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
4257 && TREE_CODE (t) == INTEGER_TYPE)
4259 if (fn)
4260 warning (OPT_Wconversion, "passing %qT for argument %P to %qD",
4261 TREE_TYPE (expr), argnum, fn);
4262 else
4263 warning (OPT_Wconversion, "converting to %qT from %qT", t, TREE_TYPE (expr));
4267 switch (convs->kind)
4269 case ck_user:
4271 struct z_candidate *cand = convs->cand;
4272 tree convfn = cand->fn;
4273 tree args;
4275 if (DECL_CONSTRUCTOR_P (convfn))
4277 tree t = build_int_cst (build_pointer_type (DECL_CONTEXT (convfn)),
4280 args = build_tree_list (NULL_TREE, expr);
4281 /* We should never try to call the abstract or base constructor
4282 from here. */
4283 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (convfn)
4284 && !DECL_HAS_VTT_PARM_P (convfn));
4285 args = tree_cons (NULL_TREE, t, args);
4287 else
4288 args = build_this (expr);
4289 expr = build_over_call (cand, LOOKUP_NORMAL);
4291 /* If this is a constructor or a function returning an aggr type,
4292 we need to build up a TARGET_EXPR. */
4293 if (DECL_CONSTRUCTOR_P (convfn))
4294 expr = build_cplus_new (totype, expr);
4296 /* The result of the call is then used to direct-initialize the object
4297 that is the destination of the copy-initialization. [dcl.init]
4299 Note that this step is not reflected in the conversion sequence;
4300 it affects the semantics when we actually perform the
4301 conversion, but is not considered during overload resolution.
4303 If the target is a class, that means call a ctor. */
4304 if (IS_AGGR_TYPE (totype)
4305 && (inner >= 0 || !lvalue_p (expr)))
4307 expr = (build_temp
4308 (expr, totype,
4309 /* Core issue 84, now a DR, says that we don't
4310 allow UDCs for these args (which deliberately
4311 breaks copy-init of an auto_ptr<Base> from an
4312 auto_ptr<Derived>). */
4313 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION,
4314 &diagnostic_fn));
4316 if (diagnostic_fn)
4318 if (fn)
4319 diagnostic_fn
4320 (" initializing argument %P of %qD from result of %qD",
4321 argnum, fn, convfn);
4322 else
4323 diagnostic_fn
4324 (" initializing temporary from result of %qD", convfn);
4326 expr = build_cplus_new (totype, expr);
4328 return expr;
4330 case ck_identity:
4331 if (type_unknown_p (expr))
4332 expr = instantiate_type (totype, expr, tf_warning_or_error);
4333 /* Convert a constant to its underlying value, unless we are
4334 about to bind it to a reference, in which case we need to
4335 leave it as an lvalue. */
4336 if (inner >= 0)
4337 expr = decl_constant_value (expr);
4338 if (convs->check_copy_constructor_p)
4339 check_constructor_callable (totype, expr);
4340 return expr;
4341 case ck_ambig:
4342 /* Call build_user_type_conversion again for the error. */
4343 return build_user_type_conversion
4344 (totype, convs->u.expr, LOOKUP_NORMAL);
4346 default:
4347 break;
4350 expr = convert_like_real (convs->u.next, expr, fn, argnum,
4351 convs->kind == ck_ref_bind ? -1 : 1,
4352 /*issue_conversion_warnings=*/false,
4353 c_cast_p);
4354 if (expr == error_mark_node)
4355 return error_mark_node;
4357 switch (convs->kind)
4359 case ck_rvalue:
4360 expr = convert_bitfield_to_declared_type (expr);
4361 if (! IS_AGGR_TYPE (totype))
4362 return expr;
4363 /* Else fall through. */
4364 case ck_base:
4365 if (convs->kind == ck_base && !convs->need_temporary_p)
4367 /* We are going to bind a reference directly to a base-class
4368 subobject of EXPR. */
4369 if (convs->check_copy_constructor_p)
4370 check_constructor_callable (TREE_TYPE (expr), expr);
4371 /* Build an expression for `*((base*) &expr)'. */
4372 expr = build_unary_op (ADDR_EXPR, expr, 0);
4373 expr = convert_to_base (expr, build_pointer_type (totype),
4374 !c_cast_p, /*nonnull=*/true);
4375 expr = build_indirect_ref (expr, "implicit conversion");
4376 return expr;
4379 /* Copy-initialization where the cv-unqualified version of the source
4380 type is the same class as, or a derived class of, the class of the
4381 destination [is treated as direct-initialization]. [dcl.init] */
4382 expr = build_temp (expr, totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
4383 &diagnostic_fn);
4384 if (diagnostic_fn && fn)
4385 diagnostic_fn (" initializing argument %P of %qD", argnum, fn);
4386 return build_cplus_new (totype, expr);
4388 case ck_ref_bind:
4390 tree ref_type = totype;
4392 /* If necessary, create a temporary. */
4393 if (convs->need_temporary_p || !lvalue_p (expr))
4395 tree type = convs->u.next->type;
4396 cp_lvalue_kind lvalue = real_lvalue_p (expr);
4398 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
4400 /* If the reference is volatile or non-const, we
4401 cannot create a temporary. */
4402 if (lvalue & clk_bitfield)
4403 error ("cannot bind bitfield %qE to %qT",
4404 expr, ref_type);
4405 else if (lvalue & clk_packed)
4406 error ("cannot bind packed field %qE to %qT",
4407 expr, ref_type);
4408 else
4409 error ("cannot bind rvalue %qE to %qT", expr, ref_type);
4410 return error_mark_node;
4412 /* If the source is a packed field, and we must use a copy
4413 constructor, then building the target expr will require
4414 binding the field to the reference parameter to the
4415 copy constructor, and we'll end up with an infinite
4416 loop. If we can use a bitwise copy, then we'll be
4417 OK. */
4418 if ((lvalue & clk_packed)
4419 && CLASS_TYPE_P (type)
4420 && !TYPE_HAS_TRIVIAL_INIT_REF (type))
4422 error ("cannot bind packed field %qE to %qT",
4423 expr, ref_type);
4424 return error_mark_node;
4426 expr = build_target_expr_with_type (expr, type);
4429 /* Take the address of the thing to which we will bind the
4430 reference. */
4431 expr = build_unary_op (ADDR_EXPR, expr, 1);
4432 if (expr == error_mark_node)
4433 return error_mark_node;
4435 /* Convert it to a pointer to the type referred to by the
4436 reference. This will adjust the pointer if a derived to
4437 base conversion is being performed. */
4438 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4439 expr);
4440 /* Convert the pointer to the desired reference type. */
4441 return build_nop (ref_type, expr);
4444 case ck_lvalue:
4445 return decay_conversion (expr);
4447 case ck_qual:
4448 /* Warn about deprecated conversion if appropriate. */
4449 string_conv_p (totype, expr, 1);
4450 break;
4452 case ck_ptr:
4453 if (convs->base_p)
4454 expr = convert_to_base (expr, totype, !c_cast_p,
4455 /*nonnull=*/false);
4456 return build_nop (totype, expr);
4458 case ck_pmem:
4459 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
4460 c_cast_p);
4462 default:
4463 break;
4466 if (issue_conversion_warnings)
4467 expr = convert_and_check (totype, expr);
4468 else
4469 expr = convert (totype, expr);
4471 return expr;
4474 /* Build a call to __builtin_trap. */
4476 static tree
4477 call_builtin_trap (void)
4479 tree fn = implicit_built_in_decls[BUILT_IN_TRAP];
4481 gcc_assert (fn != NULL);
4482 fn = build_call (fn, NULL_TREE);
4483 return fn;
4486 /* ARG is being passed to a varargs function. Perform any conversions
4487 required. Return the converted value. */
4489 tree
4490 convert_arg_to_ellipsis (tree arg)
4492 /* [expr.call]
4494 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
4495 standard conversions are performed. */
4496 arg = decay_conversion (arg);
4497 /* [expr.call]
4499 If the argument has integral or enumeration type that is subject
4500 to the integral promotions (_conv.prom_), or a floating point
4501 type that is subject to the floating point promotion
4502 (_conv.fpprom_), the value of the argument is converted to the
4503 promoted type before the call. */
4504 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4505 && (TYPE_PRECISION (TREE_TYPE (arg))
4506 < TYPE_PRECISION (double_type_node)))
4507 arg = convert_to_real (double_type_node, arg);
4508 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
4509 arg = perform_integral_promotions (arg);
4511 arg = require_complete_type (arg);
4513 if (arg != error_mark_node
4514 && !pod_type_p (TREE_TYPE (arg)))
4516 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
4517 here and do a bitwise copy, but now cp_expr_size will abort if we
4518 try to do that.
4519 If the call appears in the context of a sizeof expression,
4520 there is no need to emit a warning, since the expression won't be
4521 evaluated. We keep the builtin_trap just as a safety check. */
4522 if (!skip_evaluation)
4523 warning (0, "cannot pass objects of non-POD type %q#T through %<...%>; "
4524 "call will abort at runtime", TREE_TYPE (arg));
4525 arg = call_builtin_trap ();
4526 arg = build2 (COMPOUND_EXPR, integer_type_node, arg,
4527 integer_zero_node);
4530 return arg;
4533 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4535 tree
4536 build_x_va_arg (tree expr, tree type)
4538 if (processing_template_decl)
4539 return build_min (VA_ARG_EXPR, type, expr);
4541 type = complete_type_or_else (type, NULL_TREE);
4543 if (expr == error_mark_node || !type)
4544 return error_mark_node;
4546 if (! pod_type_p (type))
4548 /* Remove reference types so we don't ICE later on. */
4549 tree type1 = non_reference (type);
4550 /* Undefined behavior [expr.call] 5.2.2/7. */
4551 warning (0, "cannot receive objects of non-POD type %q#T through %<...%>; "
4552 "call will abort at runtime", type);
4553 expr = convert (build_pointer_type (type1), null_node);
4554 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr),
4555 call_builtin_trap (), expr);
4556 expr = build_indirect_ref (expr, NULL);
4557 return expr;
4560 return build_va_arg (expr, type);
4563 /* TYPE has been given to va_arg. Apply the default conversions which
4564 would have happened when passed via ellipsis. Return the promoted
4565 type, or the passed type if there is no change. */
4567 tree
4568 cxx_type_promotes_to (tree type)
4570 tree promote;
4572 /* Perform the array-to-pointer and function-to-pointer
4573 conversions. */
4574 type = type_decays_to (type);
4576 promote = type_promotes_to (type);
4577 if (same_type_p (type, promote))
4578 promote = type;
4580 return promote;
4583 /* ARG is a default argument expression being passed to a parameter of
4584 the indicated TYPE, which is a parameter to FN. Do any required
4585 conversions. Return the converted value. */
4587 tree
4588 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
4590 /* If the ARG is an unparsed default argument expression, the
4591 conversion cannot be performed. */
4592 if (TREE_CODE (arg) == DEFAULT_ARG)
4594 error ("the default argument for parameter %d of %qD has "
4595 "not yet been parsed",
4596 parmnum, fn);
4597 return error_mark_node;
4600 if (fn && DECL_TEMPLATE_INFO (fn))
4601 arg = tsubst_default_argument (fn, type, arg);
4603 arg = break_out_target_exprs (arg);
4605 if (TREE_CODE (arg) == CONSTRUCTOR)
4607 arg = digest_init (type, arg);
4608 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4609 "default argument", fn, parmnum);
4611 else
4613 /* This could get clobbered by the following call. */
4614 if (TREE_HAS_CONSTRUCTOR (arg))
4615 arg = copy_node (arg);
4617 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4618 "default argument", fn, parmnum);
4619 arg = convert_for_arg_passing (type, arg);
4622 return arg;
4625 /* Returns the type which will really be used for passing an argument of
4626 type TYPE. */
4628 tree
4629 type_passed_as (tree type)
4631 /* Pass classes with copy ctors by invisible reference. */
4632 if (TREE_ADDRESSABLE (type))
4634 type = build_reference_type (type);
4635 /* There are no other pointers to this temporary. */
4636 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
4638 else if (targetm.calls.promote_prototypes (type)
4639 && INTEGRAL_TYPE_P (type)
4640 && COMPLETE_TYPE_P (type)
4641 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4642 TYPE_SIZE (integer_type_node)))
4643 type = integer_type_node;
4645 return type;
4648 /* Actually perform the appropriate conversion. */
4650 tree
4651 convert_for_arg_passing (tree type, tree val)
4653 if (val == error_mark_node)
4655 /* Pass classes with copy ctors by invisible reference. */
4656 else if (TREE_ADDRESSABLE (type))
4657 val = build1 (ADDR_EXPR, build_reference_type (type), val);
4658 else if (targetm.calls.promote_prototypes (type)
4659 && INTEGRAL_TYPE_P (type)
4660 && COMPLETE_TYPE_P (type)
4661 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4662 TYPE_SIZE (integer_type_node)))
4663 val = perform_integral_promotions (val);
4664 if (warn_missing_format_attribute)
4666 tree rhstype = TREE_TYPE (val);
4667 const enum tree_code coder = TREE_CODE (rhstype);
4668 const enum tree_code codel = TREE_CODE (type);
4669 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4670 && coder == codel
4671 && check_missing_format_attribute (type, rhstype))
4672 warning (OPT_Wmissing_format_attribute,
4673 "argument of function call might be a candidate for a format attribute");
4675 return val;
4678 /* Returns true iff FN is a function with magic varargs, i.e. ones for
4679 which no conversions at all should be done. This is true for some
4680 builtins which don't act like normal functions. */
4682 static bool
4683 magic_varargs_p (tree fn)
4685 if (DECL_BUILT_IN (fn))
4686 switch (DECL_FUNCTION_CODE (fn))
4688 case BUILT_IN_CLASSIFY_TYPE:
4689 case BUILT_IN_CONSTANT_P:
4690 case BUILT_IN_NEXT_ARG:
4691 case BUILT_IN_STDARG_START:
4692 case BUILT_IN_VA_START:
4693 return true;
4695 default:;
4698 return false;
4701 /* Subroutine of the various build_*_call functions. Overload resolution
4702 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4703 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4704 bitmask of various LOOKUP_* flags which apply to the call itself. */
4706 static tree
4707 build_over_call (struct z_candidate *cand, int flags)
4709 tree fn = cand->fn;
4710 tree args = cand->args;
4711 conversion **convs = cand->convs;
4712 conversion *conv;
4713 tree converted_args = NULL_TREE;
4714 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4715 tree arg, val;
4716 int i = 0;
4717 int is_method = 0;
4719 /* In a template, there is no need to perform all of the work that
4720 is normally done. We are only interested in the type of the call
4721 expression, i.e., the return type of the function. Any semantic
4722 errors will be deferred until the template is instantiated. */
4723 if (processing_template_decl)
4725 tree expr;
4726 tree return_type;
4727 return_type = TREE_TYPE (TREE_TYPE (fn));
4728 expr = build3 (CALL_EXPR, return_type, fn, args, NULL_TREE);
4729 if (TREE_THIS_VOLATILE (fn) && cfun)
4730 current_function_returns_abnormally = 1;
4731 if (!VOID_TYPE_P (return_type))
4732 require_complete_type (return_type);
4733 return convert_from_reference (expr);
4736 /* Give any warnings we noticed during overload resolution. */
4737 if (cand->warnings)
4739 struct candidate_warning *w;
4740 for (w = cand->warnings; w; w = w->next)
4741 joust (cand, w->loser, 1);
4744 if (DECL_FUNCTION_MEMBER_P (fn))
4746 /* If FN is a template function, two cases must be considered.
4747 For example:
4749 struct A {
4750 protected:
4751 template <class T> void f();
4753 template <class T> struct B {
4754 protected:
4755 void g();
4757 struct C : A, B<int> {
4758 using A::f; // #1
4759 using B<int>::g; // #2
4762 In case #1 where `A::f' is a member template, DECL_ACCESS is
4763 recorded in the primary template but not in its specialization.
4764 We check access of FN using its primary template.
4766 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
4767 because it is a member of class template B, DECL_ACCESS is
4768 recorded in the specialization `B<int>::g'. We cannot use its
4769 primary template because `B<T>::g' and `B<int>::g' may have
4770 different access. */
4771 if (DECL_TEMPLATE_INFO (fn)
4772 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
4773 perform_or_defer_access_check (cand->access_path,
4774 DECL_TI_TEMPLATE (fn));
4775 else
4776 perform_or_defer_access_check (cand->access_path, fn);
4779 if (args && TREE_CODE (args) != TREE_LIST)
4780 args = build_tree_list (NULL_TREE, args);
4781 arg = args;
4783 /* The implicit parameters to a constructor are not considered by overload
4784 resolution, and must be of the proper type. */
4785 if (DECL_CONSTRUCTOR_P (fn))
4787 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4788 arg = TREE_CHAIN (arg);
4789 parm = TREE_CHAIN (parm);
4790 /* We should never try to call the abstract constructor. */
4791 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
4793 if (DECL_HAS_VTT_PARM_P (fn))
4795 converted_args = tree_cons
4796 (NULL_TREE, TREE_VALUE (arg), converted_args);
4797 arg = TREE_CHAIN (arg);
4798 parm = TREE_CHAIN (parm);
4801 /* Bypass access control for 'this' parameter. */
4802 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4804 tree parmtype = TREE_VALUE (parm);
4805 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4806 tree converted_arg;
4807 tree base_binfo;
4809 if (convs[i]->bad_p)
4810 pedwarn ("passing %qT as %<this%> argument of %q#D discards qualifiers",
4811 TREE_TYPE (argtype), fn);
4813 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4814 X is called for an object that is not of type X, or of a type
4815 derived from X, the behavior is undefined.
4817 So we can assume that anything passed as 'this' is non-null, and
4818 optimize accordingly. */
4819 gcc_assert (TREE_CODE (parmtype) == POINTER_TYPE);
4820 /* Convert to the base in which the function was declared. */
4821 gcc_assert (cand->conversion_path != NULL_TREE);
4822 converted_arg = build_base_path (PLUS_EXPR,
4823 TREE_VALUE (arg),
4824 cand->conversion_path,
4826 /* Check that the base class is accessible. */
4827 if (!accessible_base_p (TREE_TYPE (argtype),
4828 BINFO_TYPE (cand->conversion_path), true))
4829 error ("%qT is not an accessible base of %qT",
4830 BINFO_TYPE (cand->conversion_path),
4831 TREE_TYPE (argtype));
4832 /* If fn was found by a using declaration, the conversion path
4833 will be to the derived class, not the base declaring fn. We
4834 must convert from derived to base. */
4835 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4836 TREE_TYPE (parmtype), ba_unique, NULL);
4837 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4838 base_binfo, 1);
4840 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4841 parm = TREE_CHAIN (parm);
4842 arg = TREE_CHAIN (arg);
4843 ++i;
4844 is_method = 1;
4847 for (; arg && parm;
4848 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4850 tree type = TREE_VALUE (parm);
4852 conv = convs[i];
4854 /* Don't make a copy here if build_call is going to. */
4855 if (conv->kind == ck_rvalue
4856 && !TREE_ADDRESSABLE (complete_type (type)))
4857 conv = conv->u.next;
4859 val = convert_like_with_context
4860 (conv, TREE_VALUE (arg), fn, i - is_method);
4862 val = convert_for_arg_passing (type, val);
4863 converted_args = tree_cons (NULL_TREE, val, converted_args);
4866 /* Default arguments */
4867 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4868 converted_args
4869 = tree_cons (NULL_TREE,
4870 convert_default_arg (TREE_VALUE (parm),
4871 TREE_PURPOSE (parm),
4872 fn, i - is_method),
4873 converted_args);
4875 /* Ellipsis */
4876 for (; arg; arg = TREE_CHAIN (arg))
4878 tree a = TREE_VALUE (arg);
4879 if (magic_varargs_p (fn))
4880 /* Do no conversions for magic varargs. */;
4881 else
4882 a = convert_arg_to_ellipsis (a);
4883 converted_args = tree_cons (NULL_TREE, a, converted_args);
4886 converted_args = nreverse (converted_args);
4888 check_function_arguments (TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4889 converted_args, TYPE_ARG_TYPES (TREE_TYPE (fn)));
4891 /* Avoid actually calling copy constructors and copy assignment operators,
4892 if possible. */
4894 if (! flag_elide_constructors)
4895 /* Do things the hard way. */;
4896 else if (cand->num_convs == 1 && DECL_COPY_CONSTRUCTOR_P (fn))
4898 tree targ;
4899 arg = skip_artificial_parms_for (fn, converted_args);
4900 arg = TREE_VALUE (arg);
4902 /* Pull out the real argument, disregarding const-correctness. */
4903 targ = arg;
4904 while (TREE_CODE (targ) == NOP_EXPR
4905 || TREE_CODE (targ) == NON_LVALUE_EXPR
4906 || TREE_CODE (targ) == CONVERT_EXPR)
4907 targ = TREE_OPERAND (targ, 0);
4908 if (TREE_CODE (targ) == ADDR_EXPR)
4910 targ = TREE_OPERAND (targ, 0);
4911 if (!same_type_ignoring_top_level_qualifiers_p
4912 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4913 targ = NULL_TREE;
4915 else
4916 targ = NULL_TREE;
4918 if (targ)
4919 arg = targ;
4920 else
4921 arg = build_indirect_ref (arg, 0);
4923 /* [class.copy]: the copy constructor is implicitly defined even if
4924 the implementation elided its use. */
4925 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4926 mark_used (fn);
4928 /* If we're creating a temp and we already have one, don't create a
4929 new one. If we're not creating a temp but we get one, use
4930 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4931 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4932 temp or an INIT_EXPR otherwise. */
4933 if (integer_zerop (TREE_VALUE (args)))
4935 if (TREE_CODE (arg) == TARGET_EXPR)
4936 return arg;
4937 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4938 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4940 else if (TREE_CODE (arg) == TARGET_EXPR
4941 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4943 tree to = stabilize_reference
4944 (build_indirect_ref (TREE_VALUE (args), 0));
4946 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4947 return val;
4950 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4951 && copy_fn_p (fn)
4952 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4954 tree to = stabilize_reference
4955 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4956 tree type = TREE_TYPE (to);
4957 tree as_base = CLASSTYPE_AS_BASE (type);
4959 arg = TREE_VALUE (TREE_CHAIN (converted_args));
4960 if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
4962 arg = build_indirect_ref (arg, 0);
4963 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4965 else
4967 /* We must only copy the non-tail padding parts.
4968 Use __builtin_memcpy for the bitwise copy. */
4970 tree args, t;
4972 args = tree_cons (NULL, TYPE_SIZE_UNIT (as_base), NULL);
4973 args = tree_cons (NULL, arg, args);
4974 t = build_unary_op (ADDR_EXPR, to, 0);
4975 args = tree_cons (NULL, t, args);
4976 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
4977 t = build_call (t, args);
4979 t = convert (TREE_TYPE (TREE_VALUE (args)), t);
4980 val = build_indirect_ref (t, 0);
4983 return val;
4986 mark_used (fn);
4988 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4990 tree t, *p = &TREE_VALUE (converted_args);
4991 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4992 DECL_CONTEXT (fn),
4993 ba_any, NULL);
4994 gcc_assert (binfo && binfo != error_mark_node);
4996 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4997 if (TREE_SIDE_EFFECTS (*p))
4998 *p = save_expr (*p);
4999 t = build_pointer_type (TREE_TYPE (fn));
5000 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
5001 fn = build_java_interface_fn_ref (fn, *p);
5002 else
5003 fn = build_vfn_ref (*p, DECL_VINDEX (fn));
5004 TREE_TYPE (fn) = t;
5006 else if (DECL_INLINE (fn))
5007 fn = inline_conversion (fn);
5008 else
5009 fn = build_addr_func (fn);
5011 return build_cxx_call (fn, converted_args);
5014 /* Build and return a call to FN, using ARGS. This function performs
5015 no overload resolution, conversion, or other high-level
5016 operations. */
5018 tree
5019 build_cxx_call (tree fn, tree args)
5021 tree fndecl;
5023 fn = build_call (fn, args);
5025 /* If this call might throw an exception, note that fact. */
5026 fndecl = get_callee_fndecl (fn);
5027 if ((!fndecl || !TREE_NOTHROW (fndecl))
5028 && at_function_scope_p ()
5029 && cfun)
5030 cp_function_chain->can_throw = 1;
5032 /* Some built-in function calls will be evaluated at compile-time in
5033 fold (). */
5034 fn = fold_if_not_in_template (fn);
5036 if (VOID_TYPE_P (TREE_TYPE (fn)))
5037 return fn;
5039 fn = require_complete_type (fn);
5040 if (fn == error_mark_node)
5041 return error_mark_node;
5043 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
5044 fn = build_cplus_new (TREE_TYPE (fn), fn);
5045 return convert_from_reference (fn);
5048 static GTY(()) tree java_iface_lookup_fn;
5050 /* Make an expression which yields the address of the Java interface
5051 method FN. This is achieved by generating a call to libjava's
5052 _Jv_LookupInterfaceMethodIdx(). */
5054 static tree
5055 build_java_interface_fn_ref (tree fn, tree instance)
5057 tree lookup_args, lookup_fn, method, idx;
5058 tree klass_ref, iface, iface_ref;
5059 int i;
5061 if (!java_iface_lookup_fn)
5063 tree endlink = build_void_list_node ();
5064 tree t = tree_cons (NULL_TREE, ptr_type_node,
5065 tree_cons (NULL_TREE, ptr_type_node,
5066 tree_cons (NULL_TREE, java_int_type_node,
5067 endlink)));
5068 java_iface_lookup_fn
5069 = add_builtin_function ("_Jv_LookupInterfaceMethodIdx",
5070 build_function_type (ptr_type_node, t),
5071 0, NOT_BUILT_IN, NULL, NULL_TREE);
5074 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
5075 This is the first entry in the vtable. */
5076 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
5077 integer_zero_node);
5079 /* Get the java.lang.Class pointer for the interface being called. */
5080 iface = DECL_CONTEXT (fn);
5081 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
5082 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
5083 || DECL_CONTEXT (iface_ref) != iface)
5085 error ("could not find class$ field in java interface type %qT",
5086 iface);
5087 return error_mark_node;
5089 iface_ref = build_address (iface_ref);
5090 iface_ref = convert (build_pointer_type (iface), iface_ref);
5092 /* Determine the itable index of FN. */
5093 i = 1;
5094 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
5096 if (!DECL_VIRTUAL_P (method))
5097 continue;
5098 if (fn == method)
5099 break;
5100 i++;
5102 idx = build_int_cst (NULL_TREE, i);
5104 lookup_args = tree_cons (NULL_TREE, klass_ref,
5105 tree_cons (NULL_TREE, iface_ref,
5106 build_tree_list (NULL_TREE, idx)));
5107 lookup_fn = build1 (ADDR_EXPR,
5108 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
5109 java_iface_lookup_fn);
5110 return build3 (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
5113 /* Returns the value to use for the in-charge parameter when making a
5114 call to a function with the indicated NAME.
5116 FIXME:Can't we find a neater way to do this mapping? */
5118 tree
5119 in_charge_arg_for_name (tree name)
5121 if (name == base_ctor_identifier
5122 || name == base_dtor_identifier)
5123 return integer_zero_node;
5124 else if (name == complete_ctor_identifier)
5125 return integer_one_node;
5126 else if (name == complete_dtor_identifier)
5127 return integer_two_node;
5128 else if (name == deleting_dtor_identifier)
5129 return integer_three_node;
5131 /* This function should only be called with one of the names listed
5132 above. */
5133 gcc_unreachable ();
5134 return NULL_TREE;
5137 /* Build a call to a constructor, destructor, or an assignment
5138 operator for INSTANCE, an expression with class type. NAME
5139 indicates the special member function to call; ARGS are the
5140 arguments. BINFO indicates the base of INSTANCE that is to be
5141 passed as the `this' parameter to the member function called.
5143 FLAGS are the LOOKUP_* flags to use when processing the call.
5145 If NAME indicates a complete object constructor, INSTANCE may be
5146 NULL_TREE. In this case, the caller will call build_cplus_new to
5147 store the newly constructed object into a VAR_DECL. */
5149 tree
5150 build_special_member_call (tree instance, tree name, tree args,
5151 tree binfo, int flags)
5153 tree fns;
5154 /* The type of the subobject to be constructed or destroyed. */
5155 tree class_type;
5157 gcc_assert (name == complete_ctor_identifier
5158 || name == base_ctor_identifier
5159 || name == complete_dtor_identifier
5160 || name == base_dtor_identifier
5161 || name == deleting_dtor_identifier
5162 || name == ansi_assopname (NOP_EXPR));
5163 if (TYPE_P (binfo))
5165 /* Resolve the name. */
5166 if (!complete_type_or_else (binfo, NULL_TREE))
5167 return error_mark_node;
5169 binfo = TYPE_BINFO (binfo);
5172 gcc_assert (binfo != NULL_TREE);
5174 class_type = BINFO_TYPE (binfo);
5176 /* Handle the special case where INSTANCE is NULL_TREE. */
5177 if (name == complete_ctor_identifier && !instance)
5179 instance = build_int_cst (build_pointer_type (class_type), 0);
5180 instance = build1 (INDIRECT_REF, class_type, instance);
5182 else
5184 if (name == complete_dtor_identifier
5185 || name == base_dtor_identifier
5186 || name == deleting_dtor_identifier)
5187 gcc_assert (args == NULL_TREE);
5189 /* Convert to the base class, if necessary. */
5190 if (!same_type_ignoring_top_level_qualifiers_p
5191 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
5193 if (name != ansi_assopname (NOP_EXPR))
5194 /* For constructors and destructors, either the base is
5195 non-virtual, or it is virtual but we are doing the
5196 conversion from a constructor or destructor for the
5197 complete object. In either case, we can convert
5198 statically. */
5199 instance = convert_to_base_statically (instance, binfo);
5200 else
5201 /* However, for assignment operators, we must convert
5202 dynamically if the base is virtual. */
5203 instance = build_base_path (PLUS_EXPR, instance,
5204 binfo, /*nonnull=*/1);
5208 gcc_assert (instance != NULL_TREE);
5210 fns = lookup_fnfields (binfo, name, 1);
5212 /* When making a call to a constructor or destructor for a subobject
5213 that uses virtual base classes, pass down a pointer to a VTT for
5214 the subobject. */
5215 if ((name == base_ctor_identifier
5216 || name == base_dtor_identifier)
5217 && CLASSTYPE_VBASECLASSES (class_type))
5219 tree vtt;
5220 tree sub_vtt;
5222 /* If the current function is a complete object constructor
5223 or destructor, then we fetch the VTT directly.
5224 Otherwise, we look it up using the VTT we were given. */
5225 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
5226 vtt = decay_conversion (vtt);
5227 vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
5228 build2 (EQ_EXPR, boolean_type_node,
5229 current_in_charge_parm, integer_zero_node),
5230 current_vtt_parm,
5231 vtt);
5232 gcc_assert (BINFO_SUBVTT_INDEX (binfo));
5233 sub_vtt = build2 (PLUS_EXPR, TREE_TYPE (vtt), vtt,
5234 BINFO_SUBVTT_INDEX (binfo));
5236 args = tree_cons (NULL_TREE, sub_vtt, args);
5239 return build_new_method_call (instance, fns, args,
5240 TYPE_BINFO (BINFO_TYPE (binfo)),
5241 flags, /*fn=*/NULL);
5244 /* Return the NAME, as a C string. The NAME indicates a function that
5245 is a member of TYPE. *FREE_P is set to true if the caller must
5246 free the memory returned.
5248 Rather than go through all of this, we should simply set the names
5249 of constructors and destructors appropriately, and dispense with
5250 ctor_identifier, dtor_identifier, etc. */
5252 static char *
5253 name_as_c_string (tree name, tree type, bool *free_p)
5255 char *pretty_name;
5257 /* Assume that we will not allocate memory. */
5258 *free_p = false;
5259 /* Constructors and destructors are special. */
5260 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
5262 pretty_name
5263 = (char *) IDENTIFIER_POINTER (constructor_name (type));
5264 /* For a destructor, add the '~'. */
5265 if (name == complete_dtor_identifier
5266 || name == base_dtor_identifier
5267 || name == deleting_dtor_identifier)
5269 pretty_name = concat ("~", pretty_name, NULL);
5270 /* Remember that we need to free the memory allocated. */
5271 *free_p = true;
5274 else if (IDENTIFIER_TYPENAME_P (name))
5276 pretty_name = concat ("operator ",
5277 type_as_string (TREE_TYPE (name),
5278 TFF_PLAIN_IDENTIFIER),
5279 NULL);
5280 /* Remember that we need to free the memory allocated. */
5281 *free_p = true;
5283 else
5284 pretty_name = (char *) IDENTIFIER_POINTER (name);
5286 return pretty_name;
5289 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
5290 be set, upon return, to the function called. */
5292 tree
5293 build_new_method_call (tree instance, tree fns, tree args,
5294 tree conversion_path, int flags,
5295 tree *fn_p)
5297 struct z_candidate *candidates = 0, *cand;
5298 tree explicit_targs = NULL_TREE;
5299 tree basetype = NULL_TREE;
5300 tree access_binfo;
5301 tree optype;
5302 tree mem_args = NULL_TREE, instance_ptr;
5303 tree name;
5304 tree user_args;
5305 tree call;
5306 tree fn;
5307 tree class_type;
5308 int template_only = 0;
5309 bool any_viable_p;
5310 tree orig_instance;
5311 tree orig_fns;
5312 tree orig_args;
5313 void *p;
5315 gcc_assert (instance != NULL_TREE);
5317 /* We don't know what function we're going to call, yet. */
5318 if (fn_p)
5319 *fn_p = NULL_TREE;
5321 if (error_operand_p (instance)
5322 || error_operand_p (fns)
5323 || args == error_mark_node)
5324 return error_mark_node;
5326 if (!BASELINK_P (fns))
5328 error ("call to non-function %qD", fns);
5329 return error_mark_node;
5332 orig_instance = instance;
5333 orig_fns = fns;
5334 orig_args = args;
5336 /* Dismantle the baselink to collect all the information we need. */
5337 if (!conversion_path)
5338 conversion_path = BASELINK_BINFO (fns);
5339 access_binfo = BASELINK_ACCESS_BINFO (fns);
5340 optype = BASELINK_OPTYPE (fns);
5341 fns = BASELINK_FUNCTIONS (fns);
5342 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
5344 explicit_targs = TREE_OPERAND (fns, 1);
5345 fns = TREE_OPERAND (fns, 0);
5346 template_only = 1;
5348 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
5349 || TREE_CODE (fns) == TEMPLATE_DECL
5350 || TREE_CODE (fns) == OVERLOAD);
5351 fn = get_first_fn (fns);
5352 name = DECL_NAME (fn);
5354 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
5355 gcc_assert (CLASS_TYPE_P (basetype));
5357 if (processing_template_decl)
5359 instance = build_non_dependent_expr (instance);
5360 args = build_non_dependent_args (orig_args);
5363 /* The USER_ARGS are the arguments we will display to users if an
5364 error occurs. The USER_ARGS should not include any
5365 compiler-generated arguments. The "this" pointer hasn't been
5366 added yet. However, we must remove the VTT pointer if this is a
5367 call to a base-class constructor or destructor. */
5368 user_args = args;
5369 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
5371 /* Callers should explicitly indicate whether they want to construct
5372 the complete object or just the part without virtual bases. */
5373 gcc_assert (name != ctor_identifier);
5374 /* Similarly for destructors. */
5375 gcc_assert (name != dtor_identifier);
5376 /* Remove the VTT pointer, if present. */
5377 if ((name == base_ctor_identifier || name == base_dtor_identifier)
5378 && CLASSTYPE_VBASECLASSES (basetype))
5379 user_args = TREE_CHAIN (user_args);
5382 /* Process the argument list. */
5383 args = resolve_args (args);
5384 if (args == error_mark_node)
5385 return error_mark_node;
5387 instance_ptr = build_this (instance);
5389 /* It's OK to call destructors on cv-qualified objects. Therefore,
5390 convert the INSTANCE_PTR to the unqualified type, if necessary. */
5391 if (DECL_DESTRUCTOR_P (fn))
5393 tree type = build_pointer_type (basetype);
5394 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
5395 instance_ptr = build_nop (type, instance_ptr);
5396 name = complete_dtor_identifier;
5399 class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE);
5400 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
5402 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5403 p = conversion_obstack_alloc (0);
5405 for (fn = fns; fn; fn = OVL_NEXT (fn))
5407 tree t = OVL_CURRENT (fn);
5408 tree this_arglist;
5410 /* We can end up here for copy-init of same or base class. */
5411 if ((flags & LOOKUP_ONLYCONVERTING)
5412 && DECL_NONCONVERTING_P (t))
5413 continue;
5415 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
5416 this_arglist = mem_args;
5417 else
5418 this_arglist = args;
5420 if (TREE_CODE (t) == TEMPLATE_DECL)
5421 /* A member template. */
5422 add_template_candidate (&candidates, t,
5423 class_type,
5424 explicit_targs,
5425 this_arglist, optype,
5426 access_binfo,
5427 conversion_path,
5428 flags,
5429 DEDUCE_CALL);
5430 else if (! template_only)
5431 add_function_candidate (&candidates, t,
5432 class_type,
5433 this_arglist,
5434 access_binfo,
5435 conversion_path,
5436 flags);
5439 candidates = splice_viable (candidates, pedantic, &any_viable_p);
5440 if (!any_viable_p)
5442 if (!COMPLETE_TYPE_P (basetype))
5443 cxx_incomplete_type_error (instance_ptr, basetype);
5444 else
5446 char *pretty_name;
5447 bool free_p;
5449 pretty_name = name_as_c_string (name, basetype, &free_p);
5450 error ("no matching function for call to %<%T::%s(%A)%#V%>",
5451 basetype, pretty_name, user_args,
5452 TREE_TYPE (TREE_TYPE (instance_ptr)));
5453 if (free_p)
5454 free (pretty_name);
5456 print_z_candidates (candidates);
5457 call = error_mark_node;
5459 else
5461 cand = tourney (candidates);
5462 if (cand == 0)
5464 char *pretty_name;
5465 bool free_p;
5467 pretty_name = name_as_c_string (name, basetype, &free_p);
5468 error ("call of overloaded %<%s(%A)%> is ambiguous", pretty_name,
5469 user_args);
5470 print_z_candidates (candidates);
5471 if (free_p)
5472 free (pretty_name);
5473 call = error_mark_node;
5475 else
5477 fn = cand->fn;
5479 if (!(flags & LOOKUP_NONVIRTUAL)
5480 && DECL_PURE_VIRTUAL_P (fn)
5481 && instance == current_class_ref
5482 && (DECL_CONSTRUCTOR_P (current_function_decl)
5483 || DECL_DESTRUCTOR_P (current_function_decl)))
5484 /* This is not an error, it is runtime undefined
5485 behavior. */
5486 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl) ?
5487 "abstract virtual %q#D called from constructor"
5488 : "abstract virtual %q#D called from destructor"),
5489 fn);
5491 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
5492 && is_dummy_object (instance_ptr))
5494 error ("cannot call member function %qD without object",
5495 fn);
5496 call = error_mark_node;
5498 else
5500 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
5501 && resolves_to_fixed_type_p (instance, 0))
5502 flags |= LOOKUP_NONVIRTUAL;
5503 /* Now we know what function is being called. */
5504 if (fn_p)
5505 *fn_p = fn;
5506 /* Build the actual CALL_EXPR. */
5507 call = build_over_call (cand, flags);
5508 /* In an expression of the form `a->f()' where `f' turns
5509 out to be a static member function, `a' is
5510 none-the-less evaluated. */
5511 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
5512 && !is_dummy_object (instance_ptr)
5513 && TREE_SIDE_EFFECTS (instance_ptr))
5514 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
5515 instance_ptr, call);
5520 if (processing_template_decl && call != error_mark_node)
5521 call = (build_min_non_dep
5522 (CALL_EXPR, call,
5523 build_min_nt (COMPONENT_REF, orig_instance, orig_fns, NULL_TREE),
5524 orig_args, NULL_TREE));
5526 /* Free all the conversions we allocated. */
5527 obstack_free (&conversion_obstack, p);
5529 return call;
5532 /* Returns true iff standard conversion sequence ICS1 is a proper
5533 subsequence of ICS2. */
5535 static bool
5536 is_subseq (conversion *ics1, conversion *ics2)
5538 /* We can assume that a conversion of the same code
5539 between the same types indicates a subsequence since we only get
5540 here if the types we are converting from are the same. */
5542 while (ics1->kind == ck_rvalue
5543 || ics1->kind == ck_lvalue)
5544 ics1 = ics1->u.next;
5546 while (1)
5548 while (ics2->kind == ck_rvalue
5549 || ics2->kind == ck_lvalue)
5550 ics2 = ics2->u.next;
5552 if (ics2->kind == ck_user
5553 || ics2->kind == ck_ambig
5554 || ics2->kind == ck_identity)
5555 /* At this point, ICS1 cannot be a proper subsequence of
5556 ICS2. We can get a USER_CONV when we are comparing the
5557 second standard conversion sequence of two user conversion
5558 sequences. */
5559 return false;
5561 ics2 = ics2->u.next;
5563 if (ics2->kind == ics1->kind
5564 && same_type_p (ics2->type, ics1->type)
5565 && same_type_p (ics2->u.next->type,
5566 ics1->u.next->type))
5567 return true;
5571 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
5572 be any _TYPE nodes. */
5574 bool
5575 is_properly_derived_from (tree derived, tree base)
5577 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
5578 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
5579 return false;
5581 /* We only allow proper derivation here. The DERIVED_FROM_P macro
5582 considers every class derived from itself. */
5583 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
5584 && DERIVED_FROM_P (base, derived));
5587 /* We build the ICS for an implicit object parameter as a pointer
5588 conversion sequence. However, such a sequence should be compared
5589 as if it were a reference conversion sequence. If ICS is the
5590 implicit conversion sequence for an implicit object parameter,
5591 modify it accordingly. */
5593 static void
5594 maybe_handle_implicit_object (conversion **ics)
5596 if ((*ics)->this_p)
5598 /* [over.match.funcs]
5600 For non-static member functions, the type of the
5601 implicit object parameter is "reference to cv X"
5602 where X is the class of which the function is a
5603 member and cv is the cv-qualification on the member
5604 function declaration. */
5605 conversion *t = *ics;
5606 tree reference_type;
5608 /* The `this' parameter is a pointer to a class type. Make the
5609 implicit conversion talk about a reference to that same class
5610 type. */
5611 reference_type = TREE_TYPE (t->type);
5612 reference_type = build_reference_type (reference_type);
5614 if (t->kind == ck_qual)
5615 t = t->u.next;
5616 if (t->kind == ck_ptr)
5617 t = t->u.next;
5618 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
5619 t = direct_reference_binding (reference_type, t);
5620 *ics = t;
5624 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5625 and return the type to which the reference refers. Otherwise,
5626 leave *ICS unchanged and return NULL_TREE. */
5628 static tree
5629 maybe_handle_ref_bind (conversion **ics)
5631 if ((*ics)->kind == ck_ref_bind)
5633 conversion *old_ics = *ics;
5634 tree type = TREE_TYPE (old_ics->type);
5635 *ics = old_ics->u.next;
5636 (*ics)->user_conv_p = old_ics->user_conv_p;
5637 (*ics)->bad_p = old_ics->bad_p;
5638 return type;
5641 return NULL_TREE;
5644 /* Compare two implicit conversion sequences according to the rules set out in
5645 [over.ics.rank]. Return values:
5647 1: ics1 is better than ics2
5648 -1: ics2 is better than ics1
5649 0: ics1 and ics2 are indistinguishable */
5651 static int
5652 compare_ics (conversion *ics1, conversion *ics2)
5654 tree from_type1;
5655 tree from_type2;
5656 tree to_type1;
5657 tree to_type2;
5658 tree deref_from_type1 = NULL_TREE;
5659 tree deref_from_type2 = NULL_TREE;
5660 tree deref_to_type1 = NULL_TREE;
5661 tree deref_to_type2 = NULL_TREE;
5662 conversion_rank rank1, rank2;
5664 /* REF_BINDING is nonzero if the result of the conversion sequence
5665 is a reference type. In that case TARGET_TYPE is the
5666 type referred to by the reference. */
5667 tree target_type1;
5668 tree target_type2;
5670 /* Handle implicit object parameters. */
5671 maybe_handle_implicit_object (&ics1);
5672 maybe_handle_implicit_object (&ics2);
5674 /* Handle reference parameters. */
5675 target_type1 = maybe_handle_ref_bind (&ics1);
5676 target_type2 = maybe_handle_ref_bind (&ics2);
5678 /* [over.ics.rank]
5680 When comparing the basic forms of implicit conversion sequences (as
5681 defined in _over.best.ics_)
5683 --a standard conversion sequence (_over.ics.scs_) is a better
5684 conversion sequence than a user-defined conversion sequence
5685 or an ellipsis conversion sequence, and
5687 --a user-defined conversion sequence (_over.ics.user_) is a
5688 better conversion sequence than an ellipsis conversion sequence
5689 (_over.ics.ellipsis_). */
5690 rank1 = CONVERSION_RANK (ics1);
5691 rank2 = CONVERSION_RANK (ics2);
5693 if (rank1 > rank2)
5694 return -1;
5695 else if (rank1 < rank2)
5696 return 1;
5698 if (rank1 == cr_bad)
5700 /* XXX Isn't this an extension? */
5701 /* Both ICS are bad. We try to make a decision based on what
5702 would have happened if they'd been good. */
5703 if (ics1->user_conv_p > ics2->user_conv_p
5704 || ics1->rank > ics2->rank)
5705 return -1;
5706 else if (ics1->user_conv_p < ics2->user_conv_p
5707 || ics1->rank < ics2->rank)
5708 return 1;
5710 /* We couldn't make up our minds; try to figure it out below. */
5713 if (ics1->ellipsis_p)
5714 /* Both conversions are ellipsis conversions. */
5715 return 0;
5717 /* User-defined conversion sequence U1 is a better conversion sequence
5718 than another user-defined conversion sequence U2 if they contain the
5719 same user-defined conversion operator or constructor and if the sec-
5720 ond standard conversion sequence of U1 is better than the second
5721 standard conversion sequence of U2. */
5723 if (ics1->user_conv_p)
5725 conversion *t1;
5726 conversion *t2;
5728 for (t1 = ics1; t1->kind != ck_user; t1 = t1->u.next)
5729 if (t1->kind == ck_ambig)
5730 return 0;
5731 for (t2 = ics2; t2->kind != ck_user; t2 = t2->u.next)
5732 if (t2->kind == ck_ambig)
5733 return 0;
5735 if (t1->cand->fn != t2->cand->fn)
5736 return 0;
5738 /* We can just fall through here, after setting up
5739 FROM_TYPE1 and FROM_TYPE2. */
5740 from_type1 = t1->type;
5741 from_type2 = t2->type;
5743 else
5745 conversion *t1;
5746 conversion *t2;
5748 /* We're dealing with two standard conversion sequences.
5750 [over.ics.rank]
5752 Standard conversion sequence S1 is a better conversion
5753 sequence than standard conversion sequence S2 if
5755 --S1 is a proper subsequence of S2 (comparing the conversion
5756 sequences in the canonical form defined by _over.ics.scs_,
5757 excluding any Lvalue Transformation; the identity
5758 conversion sequence is considered to be a subsequence of
5759 any non-identity conversion sequence */
5761 t1 = ics1;
5762 while (t1->kind != ck_identity)
5763 t1 = t1->u.next;
5764 from_type1 = t1->type;
5766 t2 = ics2;
5767 while (t2->kind != ck_identity)
5768 t2 = t2->u.next;
5769 from_type2 = t2->type;
5772 if (same_type_p (from_type1, from_type2))
5774 if (is_subseq (ics1, ics2))
5775 return 1;
5776 if (is_subseq (ics2, ics1))
5777 return -1;
5779 /* Otherwise, one sequence cannot be a subsequence of the other; they
5780 don't start with the same type. This can happen when comparing the
5781 second standard conversion sequence in two user-defined conversion
5782 sequences. */
5784 /* [over.ics.rank]
5786 Or, if not that,
5788 --the rank of S1 is better than the rank of S2 (by the rules
5789 defined below):
5791 Standard conversion sequences are ordered by their ranks: an Exact
5792 Match is a better conversion than a Promotion, which is a better
5793 conversion than a Conversion.
5795 Two conversion sequences with the same rank are indistinguishable
5796 unless one of the following rules applies:
5798 --A conversion that is not a conversion of a pointer, or pointer
5799 to member, to bool is better than another conversion that is such
5800 a conversion.
5802 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5803 so that we do not have to check it explicitly. */
5804 if (ics1->rank < ics2->rank)
5805 return 1;
5806 else if (ics2->rank < ics1->rank)
5807 return -1;
5809 to_type1 = ics1->type;
5810 to_type2 = ics2->type;
5812 if (TYPE_PTR_P (from_type1)
5813 && TYPE_PTR_P (from_type2)
5814 && TYPE_PTR_P (to_type1)
5815 && TYPE_PTR_P (to_type2))
5817 deref_from_type1 = TREE_TYPE (from_type1);
5818 deref_from_type2 = TREE_TYPE (from_type2);
5819 deref_to_type1 = TREE_TYPE (to_type1);
5820 deref_to_type2 = TREE_TYPE (to_type2);
5822 /* The rules for pointers to members A::* are just like the rules
5823 for pointers A*, except opposite: if B is derived from A then
5824 A::* converts to B::*, not vice versa. For that reason, we
5825 switch the from_ and to_ variables here. */
5826 else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
5827 && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
5828 || (TYPE_PTRMEMFUNC_P (from_type1)
5829 && TYPE_PTRMEMFUNC_P (from_type2)
5830 && TYPE_PTRMEMFUNC_P (to_type1)
5831 && TYPE_PTRMEMFUNC_P (to_type2)))
5833 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
5834 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
5835 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
5836 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
5839 if (deref_from_type1 != NULL_TREE
5840 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5841 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5843 /* This was one of the pointer or pointer-like conversions.
5845 [over.ics.rank]
5847 --If class B is derived directly or indirectly from class A,
5848 conversion of B* to A* is better than conversion of B* to
5849 void*, and conversion of A* to void* is better than
5850 conversion of B* to void*. */
5851 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5852 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5854 if (is_properly_derived_from (deref_from_type1,
5855 deref_from_type2))
5856 return -1;
5857 else if (is_properly_derived_from (deref_from_type2,
5858 deref_from_type1))
5859 return 1;
5861 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5862 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5864 if (same_type_p (deref_from_type1, deref_from_type2))
5866 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5868 if (is_properly_derived_from (deref_from_type1,
5869 deref_to_type1))
5870 return 1;
5872 /* We know that DEREF_TO_TYPE1 is `void' here. */
5873 else if (is_properly_derived_from (deref_from_type1,
5874 deref_to_type2))
5875 return -1;
5878 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5879 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5881 /* [over.ics.rank]
5883 --If class B is derived directly or indirectly from class A
5884 and class C is derived directly or indirectly from B,
5886 --conversion of C* to B* is better than conversion of C* to
5889 --conversion of B* to A* is better than conversion of C* to
5890 A* */
5891 if (same_type_p (deref_from_type1, deref_from_type2))
5893 if (is_properly_derived_from (deref_to_type1,
5894 deref_to_type2))
5895 return 1;
5896 else if (is_properly_derived_from (deref_to_type2,
5897 deref_to_type1))
5898 return -1;
5900 else if (same_type_p (deref_to_type1, deref_to_type2))
5902 if (is_properly_derived_from (deref_from_type2,
5903 deref_from_type1))
5904 return 1;
5905 else if (is_properly_derived_from (deref_from_type1,
5906 deref_from_type2))
5907 return -1;
5911 else if (CLASS_TYPE_P (non_reference (from_type1))
5912 && same_type_p (from_type1, from_type2))
5914 tree from = non_reference (from_type1);
5916 /* [over.ics.rank]
5918 --binding of an expression of type C to a reference of type
5919 B& is better than binding an expression of type C to a
5920 reference of type A&
5922 --conversion of C to B is better than conversion of C to A, */
5923 if (is_properly_derived_from (from, to_type1)
5924 && is_properly_derived_from (from, to_type2))
5926 if (is_properly_derived_from (to_type1, to_type2))
5927 return 1;
5928 else if (is_properly_derived_from (to_type2, to_type1))
5929 return -1;
5932 else if (CLASS_TYPE_P (non_reference (to_type1))
5933 && same_type_p (to_type1, to_type2))
5935 tree to = non_reference (to_type1);
5937 /* [over.ics.rank]
5939 --binding of an expression of type B to a reference of type
5940 A& is better than binding an expression of type C to a
5941 reference of type A&,
5943 --conversion of B to A is better than conversion of C to A */
5944 if (is_properly_derived_from (from_type1, to)
5945 && is_properly_derived_from (from_type2, to))
5947 if (is_properly_derived_from (from_type2, from_type1))
5948 return 1;
5949 else if (is_properly_derived_from (from_type1, from_type2))
5950 return -1;
5954 /* [over.ics.rank]
5956 --S1 and S2 differ only in their qualification conversion and yield
5957 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5958 qualification signature of type T1 is a proper subset of the cv-
5959 qualification signature of type T2 */
5960 if (ics1->kind == ck_qual
5961 && ics2->kind == ck_qual
5962 && same_type_p (from_type1, from_type2))
5963 return comp_cv_qual_signature (to_type1, to_type2);
5965 /* [over.ics.rank]
5967 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5968 types to which the references refer are the same type except for
5969 top-level cv-qualifiers, and the type to which the reference
5970 initialized by S2 refers is more cv-qualified than the type to
5971 which the reference initialized by S1 refers */
5973 if (target_type1 && target_type2
5974 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5975 return comp_cv_qualification (target_type2, target_type1);
5977 /* Neither conversion sequence is better than the other. */
5978 return 0;
5981 /* The source type for this standard conversion sequence. */
5983 static tree
5984 source_type (conversion *t)
5986 for (;; t = t->u.next)
5988 if (t->kind == ck_user
5989 || t->kind == ck_ambig
5990 || t->kind == ck_identity)
5991 return t->type;
5993 gcc_unreachable ();
5996 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5997 a pointer to LOSER and re-running joust to produce the warning if WINNER
5998 is actually used. */
6000 static void
6001 add_warning (struct z_candidate *winner, struct z_candidate *loser)
6003 candidate_warning *cw = (candidate_warning *)
6004 conversion_obstack_alloc (sizeof (candidate_warning));
6005 cw->loser = loser;
6006 cw->next = winner->warnings;
6007 winner->warnings = cw;
6010 /* Compare two candidates for overloading as described in
6011 [over.match.best]. Return values:
6013 1: cand1 is better than cand2
6014 -1: cand2 is better than cand1
6015 0: cand1 and cand2 are indistinguishable */
6017 static int
6018 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
6020 int winner = 0;
6021 int off1 = 0, off2 = 0;
6022 size_t i;
6023 size_t len;
6025 /* Candidates that involve bad conversions are always worse than those
6026 that don't. */
6027 if (cand1->viable > cand2->viable)
6028 return 1;
6029 if (cand1->viable < cand2->viable)
6030 return -1;
6032 /* If we have two pseudo-candidates for conversions to the same type,
6033 or two candidates for the same function, arbitrarily pick one. */
6034 if (cand1->fn == cand2->fn
6035 && (IS_TYPE_OR_DECL_P (cand1->fn)))
6036 return 1;
6038 /* a viable function F1
6039 is defined to be a better function than another viable function F2 if
6040 for all arguments i, ICSi(F1) is not a worse conversion sequence than
6041 ICSi(F2), and then */
6043 /* for some argument j, ICSj(F1) is a better conversion sequence than
6044 ICSj(F2) */
6046 /* For comparing static and non-static member functions, we ignore
6047 the implicit object parameter of the non-static function. The
6048 standard says to pretend that the static function has an object
6049 parm, but that won't work with operator overloading. */
6050 len = cand1->num_convs;
6051 if (len != cand2->num_convs)
6053 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
6054 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
6056 gcc_assert (static_1 != static_2);
6058 if (static_1)
6059 off2 = 1;
6060 else
6062 off1 = 1;
6063 --len;
6067 for (i = 0; i < len; ++i)
6069 conversion *t1 = cand1->convs[i + off1];
6070 conversion *t2 = cand2->convs[i + off2];
6071 int comp = compare_ics (t1, t2);
6073 if (comp != 0)
6075 if (warn_sign_promo
6076 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
6077 == cr_std + cr_promotion)
6078 && t1->kind == ck_std
6079 && t2->kind == ck_std
6080 && TREE_CODE (t1->type) == INTEGER_TYPE
6081 && TREE_CODE (t2->type) == INTEGER_TYPE
6082 && (TYPE_PRECISION (t1->type)
6083 == TYPE_PRECISION (t2->type))
6084 && (TYPE_UNSIGNED (t1->u.next->type)
6085 || (TREE_CODE (t1->u.next->type)
6086 == ENUMERAL_TYPE)))
6088 tree type = t1->u.next->type;
6089 tree type1, type2;
6090 struct z_candidate *w, *l;
6091 if (comp > 0)
6092 type1 = t1->type, type2 = t2->type,
6093 w = cand1, l = cand2;
6094 else
6095 type1 = t2->type, type2 = t1->type,
6096 w = cand2, l = cand1;
6098 if (warn)
6100 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
6101 type, type1, type2);
6102 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
6104 else
6105 add_warning (w, l);
6108 if (winner && comp != winner)
6110 winner = 0;
6111 goto tweak;
6113 winner = comp;
6117 /* warn about confusing overload resolution for user-defined conversions,
6118 either between a constructor and a conversion op, or between two
6119 conversion ops. */
6120 if (winner && warn_conversion && cand1->second_conv
6121 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
6122 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
6124 struct z_candidate *w, *l;
6125 bool give_warning = false;
6127 if (winner == 1)
6128 w = cand1, l = cand2;
6129 else
6130 w = cand2, l = cand1;
6132 /* We don't want to complain about `X::operator T1 ()'
6133 beating `X::operator T2 () const', when T2 is a no less
6134 cv-qualified version of T1. */
6135 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
6136 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
6138 tree t = TREE_TYPE (TREE_TYPE (l->fn));
6139 tree f = TREE_TYPE (TREE_TYPE (w->fn));
6141 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
6143 t = TREE_TYPE (t);
6144 f = TREE_TYPE (f);
6146 if (!comp_ptr_ttypes (t, f))
6147 give_warning = true;
6149 else
6150 give_warning = true;
6152 if (!give_warning)
6153 /*NOP*/;
6154 else if (warn)
6156 tree source = source_type (w->convs[0]);
6157 if (! DECL_CONSTRUCTOR_P (w->fn))
6158 source = TREE_TYPE (source);
6159 warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn);
6160 warning (OPT_Wconversion, " for conversion from %qT to %qT",
6161 source, w->second_conv->type);
6162 inform (" because conversion sequence for the argument is better");
6164 else
6165 add_warning (w, l);
6168 if (winner)
6169 return winner;
6171 /* or, if not that,
6172 F1 is a non-template function and F2 is a template function
6173 specialization. */
6175 if (!cand1->template_decl && cand2->template_decl)
6176 return 1;
6177 else if (cand1->template_decl && !cand2->template_decl)
6178 return -1;
6180 /* or, if not that,
6181 F1 and F2 are template functions and the function template for F1 is
6182 more specialized than the template for F2 according to the partial
6183 ordering rules. */
6185 if (cand1->template_decl && cand2->template_decl)
6187 winner = more_specialized_fn
6188 (TI_TEMPLATE (cand1->template_decl),
6189 TI_TEMPLATE (cand2->template_decl),
6190 /* [temp.func.order]: The presence of unused ellipsis and default
6191 arguments has no effect on the partial ordering of function
6192 templates. add_function_candidate() will not have
6193 counted the "this" argument for constructors. */
6194 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
6195 if (winner)
6196 return winner;
6199 /* or, if not that,
6200 the context is an initialization by user-defined conversion (see
6201 _dcl.init_ and _over.match.user_) and the standard conversion
6202 sequence from the return type of F1 to the destination type (i.e.,
6203 the type of the entity being initialized) is a better conversion
6204 sequence than the standard conversion sequence from the return type
6205 of F2 to the destination type. */
6207 if (cand1->second_conv)
6209 winner = compare_ics (cand1->second_conv, cand2->second_conv);
6210 if (winner)
6211 return winner;
6214 /* Check whether we can discard a builtin candidate, either because we
6215 have two identical ones or matching builtin and non-builtin candidates.
6217 (Pedantically in the latter case the builtin which matched the user
6218 function should not be added to the overload set, but we spot it here.
6220 [over.match.oper]
6221 ... the builtin candidates include ...
6222 - do not have the same parameter type list as any non-template
6223 non-member candidate. */
6225 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
6226 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
6228 for (i = 0; i < len; ++i)
6229 if (!same_type_p (cand1->convs[i]->type,
6230 cand2->convs[i]->type))
6231 break;
6232 if (i == cand1->num_convs)
6234 if (cand1->fn == cand2->fn)
6235 /* Two built-in candidates; arbitrarily pick one. */
6236 return 1;
6237 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
6238 /* cand1 is built-in; prefer cand2. */
6239 return -1;
6240 else
6241 /* cand2 is built-in; prefer cand1. */
6242 return 1;
6246 /* If the two functions are the same (this can happen with declarations
6247 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
6248 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
6249 && equal_functions (cand1->fn, cand2->fn))
6250 return 1;
6252 tweak:
6254 /* Extension: If the worst conversion for one candidate is worse than the
6255 worst conversion for the other, take the first. */
6256 if (!pedantic)
6258 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
6259 struct z_candidate *w = 0, *l = 0;
6261 for (i = 0; i < len; ++i)
6263 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
6264 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
6265 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
6266 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
6268 if (rank1 < rank2)
6269 winner = 1, w = cand1, l = cand2;
6270 if (rank1 > rank2)
6271 winner = -1, w = cand2, l = cand1;
6272 if (winner)
6274 if (warn)
6276 pedwarn ("\
6277 ISO C++ says that these are ambiguous, even \
6278 though the worst conversion for the first is better than \
6279 the worst conversion for the second:");
6280 print_z_candidate (_("candidate 1:"), w);
6281 print_z_candidate (_("candidate 2:"), l);
6283 else
6284 add_warning (w, l);
6285 return winner;
6289 gcc_assert (!winner);
6290 return 0;
6293 /* Given a list of candidates for overloading, find the best one, if any.
6294 This algorithm has a worst case of O(2n) (winner is last), and a best
6295 case of O(n/2) (totally ambiguous); much better than a sorting
6296 algorithm. */
6298 static struct z_candidate *
6299 tourney (struct z_candidate *candidates)
6301 struct z_candidate *champ = candidates, *challenger;
6302 int fate;
6303 int champ_compared_to_predecessor = 0;
6305 /* Walk through the list once, comparing each current champ to the next
6306 candidate, knocking out a candidate or two with each comparison. */
6308 for (challenger = champ->next; challenger; )
6310 fate = joust (champ, challenger, 0);
6311 if (fate == 1)
6312 challenger = challenger->next;
6313 else
6315 if (fate == 0)
6317 champ = challenger->next;
6318 if (champ == 0)
6319 return NULL;
6320 champ_compared_to_predecessor = 0;
6322 else
6324 champ = challenger;
6325 champ_compared_to_predecessor = 1;
6328 challenger = champ->next;
6332 /* Make sure the champ is better than all the candidates it hasn't yet
6333 been compared to. */
6335 for (challenger = candidates;
6336 challenger != champ
6337 && !(champ_compared_to_predecessor && challenger->next == champ);
6338 challenger = challenger->next)
6340 fate = joust (champ, challenger, 0);
6341 if (fate != 1)
6342 return NULL;
6345 return champ;
6348 /* Returns nonzero if things of type FROM can be converted to TO. */
6350 bool
6351 can_convert (tree to, tree from)
6353 return can_convert_arg (to, from, NULL_TREE, LOOKUP_NORMAL);
6356 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
6358 bool
6359 can_convert_arg (tree to, tree from, tree arg, int flags)
6361 conversion *t;
6362 void *p;
6363 bool ok_p;
6365 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6366 p = conversion_obstack_alloc (0);
6368 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
6369 flags);
6370 ok_p = (t && !t->bad_p);
6372 /* Free all the conversions we allocated. */
6373 obstack_free (&conversion_obstack, p);
6375 return ok_p;
6378 /* Like can_convert_arg, but allows dubious conversions as well. */
6380 bool
6381 can_convert_arg_bad (tree to, tree from, tree arg)
6383 conversion *t;
6384 void *p;
6386 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6387 p = conversion_obstack_alloc (0);
6388 /* Try to perform the conversion. */
6389 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
6390 LOOKUP_NORMAL);
6391 /* Free all the conversions we allocated. */
6392 obstack_free (&conversion_obstack, p);
6394 return t != NULL;
6397 /* Convert EXPR to TYPE. Return the converted expression.
6399 Note that we allow bad conversions here because by the time we get to
6400 this point we are committed to doing the conversion. If we end up
6401 doing a bad conversion, convert_like will complain. */
6403 tree
6404 perform_implicit_conversion (tree type, tree expr)
6406 conversion *conv;
6407 void *p;
6409 if (error_operand_p (expr))
6410 return error_mark_node;
6412 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6413 p = conversion_obstack_alloc (0);
6415 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6416 /*c_cast_p=*/false,
6417 LOOKUP_NORMAL);
6418 if (!conv)
6420 error ("could not convert %qE to %qT", expr, type);
6421 expr = error_mark_node;
6423 else if (processing_template_decl)
6425 /* In a template, we are only concerned about determining the
6426 type of non-dependent expressions, so we do not have to
6427 perform the actual conversion. */
6428 if (TREE_TYPE (expr) != type)
6429 expr = build_nop (type, expr);
6431 else
6432 expr = convert_like (conv, expr);
6434 /* Free all the conversions we allocated. */
6435 obstack_free (&conversion_obstack, p);
6437 return expr;
6440 /* Convert EXPR to TYPE (as a direct-initialization) if that is
6441 permitted. If the conversion is valid, the converted expression is
6442 returned. Otherwise, NULL_TREE is returned, except in the case
6443 that TYPE is a class type; in that case, an error is issued. If
6444 C_CAST_P is true, then this direction initialization is taking
6445 place as part of a static_cast being attempted as part of a C-style
6446 cast. */
6448 tree
6449 perform_direct_initialization_if_possible (tree type,
6450 tree expr,
6451 bool c_cast_p)
6453 conversion *conv;
6454 void *p;
6456 if (type == error_mark_node || error_operand_p (expr))
6457 return error_mark_node;
6458 /* [dcl.init]
6460 If the destination type is a (possibly cv-qualified) class type:
6462 -- If the initialization is direct-initialization ...,
6463 constructors are considered. ... If no constructor applies, or
6464 the overload resolution is ambiguous, the initialization is
6465 ill-formed. */
6466 if (CLASS_TYPE_P (type))
6468 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6469 build_tree_list (NULL_TREE, expr),
6470 type, LOOKUP_NORMAL);
6471 return build_cplus_new (type, expr);
6474 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6475 p = conversion_obstack_alloc (0);
6477 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6478 c_cast_p,
6479 LOOKUP_NORMAL);
6480 if (!conv || conv->bad_p)
6481 expr = NULL_TREE;
6482 else
6483 expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
6484 /*issue_conversion_warnings=*/false,
6485 c_cast_p);
6487 /* Free all the conversions we allocated. */
6488 obstack_free (&conversion_obstack, p);
6490 return expr;
6493 /* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference
6494 is being bound to a temporary. Create and return a new VAR_DECL
6495 with the indicated TYPE; this variable will store the value to
6496 which the reference is bound. */
6498 tree
6499 make_temporary_var_for_ref_to_temp (tree decl, tree type)
6501 tree var;
6503 /* Create the variable. */
6504 var = create_temporary_var (type);
6506 /* Register the variable. */
6507 if (TREE_STATIC (decl))
6509 /* Namespace-scope or local static; give it a mangled name. */
6510 tree name;
6512 TREE_STATIC (var) = 1;
6513 name = mangle_ref_init_variable (decl);
6514 DECL_NAME (var) = name;
6515 SET_DECL_ASSEMBLER_NAME (var, name);
6516 var = pushdecl_top_level (var);
6518 else
6519 /* Create a new cleanup level if necessary. */
6520 maybe_push_cleanup_level (type);
6522 return var;
6525 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
6526 initializing a variable of that TYPE. If DECL is non-NULL, it is
6527 the VAR_DECL being initialized with the EXPR. (In that case, the
6528 type of DECL will be TYPE.) If DECL is non-NULL, then CLEANUP must
6529 also be non-NULL, and with *CLEANUP initialized to NULL. Upon
6530 return, if *CLEANUP is no longer NULL, it will be an expression
6531 that should be pushed as a cleanup after the returned expression
6532 is used to initialize DECL.
6534 Return the converted expression. */
6536 tree
6537 initialize_reference (tree type, tree expr, tree decl, tree *cleanup)
6539 conversion *conv;
6540 void *p;
6542 if (type == error_mark_node || error_operand_p (expr))
6543 return error_mark_node;
6545 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6546 p = conversion_obstack_alloc (0);
6548 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
6549 if (!conv || conv->bad_p)
6551 if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
6552 && !real_lvalue_p (expr))
6553 error ("invalid initialization of non-const reference of "
6554 "type %qT from a temporary of type %qT",
6555 type, TREE_TYPE (expr));
6556 else
6557 error ("invalid initialization of reference of type "
6558 "%qT from expression of type %qT", type,
6559 TREE_TYPE (expr));
6560 return error_mark_node;
6563 /* If DECL is non-NULL, then this special rule applies:
6565 [class.temporary]
6567 The temporary to which the reference is bound or the temporary
6568 that is the complete object to which the reference is bound
6569 persists for the lifetime of the reference.
6571 The temporaries created during the evaluation of the expression
6572 initializing the reference, except the temporary to which the
6573 reference is bound, are destroyed at the end of the
6574 full-expression in which they are created.
6576 In that case, we store the converted expression into a new
6577 VAR_DECL in a new scope.
6579 However, we want to be careful not to create temporaries when
6580 they are not required. For example, given:
6582 struct B {};
6583 struct D : public B {};
6584 D f();
6585 const B& b = f();
6587 there is no need to copy the return value from "f"; we can just
6588 extend its lifetime. Similarly, given:
6590 struct S {};
6591 struct T { operator S(); };
6592 T t;
6593 const S& s = t;
6595 we can extend the lifetime of the return value of the conversion
6596 operator. */
6597 gcc_assert (conv->kind == ck_ref_bind);
6598 if (decl)
6600 tree var;
6601 tree base_conv_type;
6603 /* Skip over the REF_BIND. */
6604 conv = conv->u.next;
6605 /* If the next conversion is a BASE_CONV, skip that too -- but
6606 remember that the conversion was required. */
6607 if (conv->kind == ck_base)
6609 if (conv->check_copy_constructor_p)
6610 check_constructor_callable (TREE_TYPE (expr), expr);
6611 base_conv_type = conv->type;
6612 conv = conv->u.next;
6614 else
6615 base_conv_type = NULL_TREE;
6616 /* Perform the remainder of the conversion. */
6617 expr = convert_like_real (conv, expr,
6618 /*fn=*/NULL_TREE, /*argnum=*/0,
6619 /*inner=*/-1,
6620 /*issue_conversion_warnings=*/true,
6621 /*c_cast_p=*/false);
6622 if (error_operand_p (expr))
6623 expr = error_mark_node;
6624 else
6626 if (!real_lvalue_p (expr))
6628 tree init;
6629 tree type;
6631 /* Create the temporary variable. */
6632 type = TREE_TYPE (expr);
6633 var = make_temporary_var_for_ref_to_temp (decl, type);
6634 layout_decl (var, 0);
6635 /* If the rvalue is the result of a function call it will be
6636 a TARGET_EXPR. If it is some other construct (such as a
6637 member access expression where the underlying object is
6638 itself the result of a function call), turn it into a
6639 TARGET_EXPR here. It is important that EXPR be a
6640 TARGET_EXPR below since otherwise the INIT_EXPR will
6641 attempt to make a bitwise copy of EXPR to initialize
6642 VAR. */
6643 if (TREE_CODE (expr) != TARGET_EXPR)
6644 expr = get_target_expr (expr);
6645 /* Create the INIT_EXPR that will initialize the temporary
6646 variable. */
6647 init = build2 (INIT_EXPR, type, var, expr);
6648 if (at_function_scope_p ())
6650 add_decl_expr (var);
6651 *cleanup = cxx_maybe_build_cleanup (var);
6653 /* We must be careful to destroy the temporary only
6654 after its initialization has taken place. If the
6655 initialization throws an exception, then the
6656 destructor should not be run. We cannot simply
6657 transform INIT into something like:
6659 (INIT, ({ CLEANUP_STMT; }))
6661 because emit_local_var always treats the
6662 initializer as a full-expression. Thus, the
6663 destructor would run too early; it would run at the
6664 end of initializing the reference variable, rather
6665 than at the end of the block enclosing the
6666 reference variable.
6668 The solution is to pass back a cleanup expression
6669 which the caller is responsible for attaching to
6670 the statement tree. */
6672 else
6674 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
6675 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6676 static_aggregates = tree_cons (NULL_TREE, var,
6677 static_aggregates);
6679 /* Use its address to initialize the reference variable. */
6680 expr = build_address (var);
6681 if (base_conv_type)
6682 expr = convert_to_base (expr,
6683 build_pointer_type (base_conv_type),
6684 /*check_access=*/true,
6685 /*nonnull=*/true);
6686 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
6688 else
6689 /* Take the address of EXPR. */
6690 expr = build_unary_op (ADDR_EXPR, expr, 0);
6691 /* If a BASE_CONV was required, perform it now. */
6692 if (base_conv_type)
6693 expr = (perform_implicit_conversion
6694 (build_pointer_type (base_conv_type), expr));
6695 expr = build_nop (type, expr);
6698 else
6699 /* Perform the conversion. */
6700 expr = convert_like (conv, expr);
6702 /* Free all the conversions we allocated. */
6703 obstack_free (&conversion_obstack, p);
6705 return expr;
6708 #include "gt-cp-call.h"