* config/alpha/alpha.md, arm/arm.c, darwin.c, frv/frv.md,
[official-gcc.git] / gcc / cp / call.c
blob34187039243affbc6b0da6a6686f9e87b9bd77db
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
1815 && TREE_CODE (type2) == ENUMERAL_TYPE)
1816 break;
1817 if (TYPE_PTR_P (type1)
1818 && null_ptr_cst_p (args[1])
1819 && !uses_template_parms (type1))
1821 type2 = type1;
1822 break;
1824 if (null_ptr_cst_p (args[0])
1825 && TYPE_PTR_P (type2)
1826 && !uses_template_parms (type2))
1828 type1 = type2;
1829 break;
1831 return;
1833 case PLUS_EXPR:
1834 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1835 break;
1836 case ARRAY_REF:
1837 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1839 type1 = ptrdiff_type_node;
1840 break;
1842 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1844 type2 = ptrdiff_type_node;
1845 break;
1847 return;
1849 /* 18For every pair of promoted integral types L and R, there exist candi-
1850 date operator functions of the form
1851 LR operator%(L, R);
1852 LR operator&(L, R);
1853 LR operator^(L, R);
1854 LR operator|(L, R);
1855 L operator<<(L, R);
1856 L operator>>(L, R);
1857 where LR is the result of the usual arithmetic conversions between
1858 types L and R. */
1860 case TRUNC_MOD_EXPR:
1861 case BIT_AND_EXPR:
1862 case BIT_IOR_EXPR:
1863 case BIT_XOR_EXPR:
1864 case LSHIFT_EXPR:
1865 case RSHIFT_EXPR:
1866 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1867 break;
1868 return;
1870 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1871 type, VQ is either volatile or empty, and R is a promoted arithmetic
1872 type, there exist candidate operator functions of the form
1873 VQ L& operator=(VQ L&, R);
1874 VQ L& operator*=(VQ L&, R);
1875 VQ L& operator/=(VQ L&, R);
1876 VQ L& operator+=(VQ L&, R);
1877 VQ L& operator-=(VQ L&, R);
1879 20For every pair T, VQ), where T is any type and VQ is either volatile
1880 or empty, there exist candidate operator functions of the form
1881 T*VQ& operator=(T*VQ&, T*);
1883 21For every pair T, VQ), where T is a pointer to member type and VQ is
1884 either volatile or empty, there exist candidate operator functions of
1885 the form
1886 VQ T& operator=(VQ T&, T);
1888 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1889 unqualified complete object type, VQ is either volatile or empty, and
1890 I is a promoted integral type, there exist candidate operator func-
1891 tions of the form
1892 T*VQ& operator+=(T*VQ&, I);
1893 T*VQ& operator-=(T*VQ&, I);
1895 23For every triple L, VQ, R), where L is an integral or enumeration
1896 type, VQ is either volatile or empty, and R is a promoted integral
1897 type, there exist candidate operator functions of the form
1899 VQ L& operator%=(VQ L&, R);
1900 VQ L& operator<<=(VQ L&, R);
1901 VQ L& operator>>=(VQ L&, R);
1902 VQ L& operator&=(VQ L&, R);
1903 VQ L& operator^=(VQ L&, R);
1904 VQ L& operator|=(VQ L&, R); */
1906 case MODIFY_EXPR:
1907 switch (code2)
1909 case PLUS_EXPR:
1910 case MINUS_EXPR:
1911 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1913 type2 = ptrdiff_type_node;
1914 break;
1916 case MULT_EXPR:
1917 case TRUNC_DIV_EXPR:
1918 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1919 break;
1920 return;
1922 case TRUNC_MOD_EXPR:
1923 case BIT_AND_EXPR:
1924 case BIT_IOR_EXPR:
1925 case BIT_XOR_EXPR:
1926 case LSHIFT_EXPR:
1927 case RSHIFT_EXPR:
1928 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1929 break;
1930 return;
1932 case NOP_EXPR:
1933 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1934 break;
1935 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1936 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1937 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1938 || ((TYPE_PTRMEMFUNC_P (type1)
1939 || TREE_CODE (type1) == POINTER_TYPE)
1940 && null_ptr_cst_p (args[1])))
1942 type2 = type1;
1943 break;
1945 return;
1947 default:
1948 gcc_unreachable ();
1950 type1 = build_reference_type (type1);
1951 break;
1953 case COND_EXPR:
1954 /* [over.built]
1956 For every pair of promoted arithmetic types L and R, there
1957 exist candidate operator functions of the form
1959 LR operator?(bool, L, R);
1961 where LR is the result of the usual arithmetic conversions
1962 between types L and R.
1964 For every type T, where T is a pointer or pointer-to-member
1965 type, there exist candidate operator functions of the form T
1966 operator?(bool, T, T); */
1968 if (promoted_arithmetic_type_p (type1)
1969 && promoted_arithmetic_type_p (type2))
1970 /* That's OK. */
1971 break;
1973 /* Otherwise, the types should be pointers. */
1974 if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
1975 || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
1976 return;
1978 /* We don't check that the two types are the same; the logic
1979 below will actually create two candidates; one in which both
1980 parameter types are TYPE1, and one in which both parameter
1981 types are TYPE2. */
1982 break;
1984 default:
1985 gcc_unreachable ();
1988 /* If we're dealing with two pointer types or two enumeral types,
1989 we need candidates for both of them. */
1990 if (type2 && !same_type_p (type1, type2)
1991 && TREE_CODE (type1) == TREE_CODE (type2)
1992 && (TREE_CODE (type1) == REFERENCE_TYPE
1993 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1994 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1995 || TYPE_PTRMEMFUNC_P (type1)
1996 || IS_AGGR_TYPE (type1)
1997 || TREE_CODE (type1) == ENUMERAL_TYPE))
1999 build_builtin_candidate
2000 (candidates, fnname, type1, type1, args, argtypes, flags);
2001 build_builtin_candidate
2002 (candidates, fnname, type2, type2, args, argtypes, flags);
2003 return;
2006 build_builtin_candidate
2007 (candidates, fnname, type1, type2, args, argtypes, flags);
2010 tree
2011 type_decays_to (tree type)
2013 if (TREE_CODE (type) == ARRAY_TYPE)
2014 return build_pointer_type (TREE_TYPE (type));
2015 if (TREE_CODE (type) == FUNCTION_TYPE)
2016 return build_pointer_type (type);
2017 return type;
2020 /* There are three conditions of builtin candidates:
2022 1) bool-taking candidates. These are the same regardless of the input.
2023 2) pointer-pair taking candidates. These are generated for each type
2024 one of the input types converts to.
2025 3) arithmetic candidates. According to the standard, we should generate
2026 all of these, but I'm trying not to...
2028 Here we generate a superset of the possible candidates for this particular
2029 case. That is a subset of the full set the standard defines, plus some
2030 other cases which the standard disallows. add_builtin_candidate will
2031 filter out the invalid set. */
2033 static void
2034 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2035 enum tree_code code2, tree fnname, tree *args,
2036 int flags)
2038 int ref1, i;
2039 int enum_p = 0;
2040 tree type, argtypes[3];
2041 /* TYPES[i] is the set of possible builtin-operator parameter types
2042 we will consider for the Ith argument. These are represented as
2043 a TREE_LIST; the TREE_VALUE of each node is the potential
2044 parameter type. */
2045 tree types[2];
2047 for (i = 0; i < 3; ++i)
2049 if (args[i])
2050 argtypes[i] = lvalue_type (args[i]);
2051 else
2052 argtypes[i] = NULL_TREE;
2055 switch (code)
2057 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2058 and VQ is either volatile or empty, there exist candidate operator
2059 functions of the form
2060 VQ T& operator++(VQ T&); */
2062 case POSTINCREMENT_EXPR:
2063 case PREINCREMENT_EXPR:
2064 case POSTDECREMENT_EXPR:
2065 case PREDECREMENT_EXPR:
2066 case MODIFY_EXPR:
2067 ref1 = 1;
2068 break;
2070 /* 24There also exist candidate operator functions of the form
2071 bool operator!(bool);
2072 bool operator&&(bool, bool);
2073 bool operator||(bool, bool); */
2075 case TRUTH_NOT_EXPR:
2076 build_builtin_candidate
2077 (candidates, fnname, boolean_type_node,
2078 NULL_TREE, args, argtypes, flags);
2079 return;
2081 case TRUTH_ORIF_EXPR:
2082 case TRUTH_ANDIF_EXPR:
2083 build_builtin_candidate
2084 (candidates, fnname, boolean_type_node,
2085 boolean_type_node, args, argtypes, flags);
2086 return;
2088 case ADDR_EXPR:
2089 case COMPOUND_EXPR:
2090 case COMPONENT_REF:
2091 return;
2093 case COND_EXPR:
2094 case EQ_EXPR:
2095 case NE_EXPR:
2096 case LT_EXPR:
2097 case LE_EXPR:
2098 case GT_EXPR:
2099 case GE_EXPR:
2100 enum_p = 1;
2101 /* Fall through. */
2103 default:
2104 ref1 = 0;
2107 types[0] = types[1] = NULL_TREE;
2109 for (i = 0; i < 2; ++i)
2111 if (! args[i])
2113 else if (IS_AGGR_TYPE (argtypes[i]))
2115 tree convs;
2117 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2118 return;
2120 convs = lookup_conversions (argtypes[i]);
2122 if (code == COND_EXPR)
2124 if (real_lvalue_p (args[i]))
2125 types[i] = tree_cons
2126 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2128 types[i] = tree_cons
2129 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2132 else if (! convs)
2133 return;
2135 for (; convs; convs = TREE_CHAIN (convs))
2137 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2139 if (i == 0 && ref1
2140 && (TREE_CODE (type) != REFERENCE_TYPE
2141 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2142 continue;
2144 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2145 types[i] = tree_cons (NULL_TREE, type, types[i]);
2147 type = non_reference (type);
2148 if (i != 0 || ! ref1)
2150 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2151 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2152 types[i] = tree_cons (NULL_TREE, type, types[i]);
2153 if (INTEGRAL_TYPE_P (type))
2154 type = type_promotes_to (type);
2157 if (! value_member (type, types[i]))
2158 types[i] = tree_cons (NULL_TREE, type, types[i]);
2161 else
2163 if (code == COND_EXPR && real_lvalue_p (args[i]))
2164 types[i] = tree_cons
2165 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2166 type = non_reference (argtypes[i]);
2167 if (i != 0 || ! ref1)
2169 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2170 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2171 types[i] = tree_cons (NULL_TREE, type, types[i]);
2172 if (INTEGRAL_TYPE_P (type))
2173 type = type_promotes_to (type);
2175 types[i] = tree_cons (NULL_TREE, type, types[i]);
2179 /* Run through the possible parameter types of both arguments,
2180 creating candidates with those parameter types. */
2181 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2183 if (types[1])
2184 for (type = types[1]; type; type = TREE_CHAIN (type))
2185 add_builtin_candidate
2186 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2187 TREE_VALUE (type), args, argtypes, flags);
2188 else
2189 add_builtin_candidate
2190 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2191 NULL_TREE, args, argtypes, flags);
2196 /* If TMPL can be successfully instantiated as indicated by
2197 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2199 TMPL is the template. EXPLICIT_TARGS are any explicit template
2200 arguments. ARGLIST is the arguments provided at the call-site.
2201 The RETURN_TYPE is the desired type for conversion operators. If
2202 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2203 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2204 add_conv_candidate. */
2206 static struct z_candidate*
2207 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2208 tree ctype, tree explicit_targs, tree arglist,
2209 tree return_type, tree access_path,
2210 tree conversion_path, int flags, tree obj,
2211 unification_kind_t strict)
2213 int ntparms = DECL_NTPARMS (tmpl);
2214 tree targs = make_tree_vec (ntparms);
2215 tree args_without_in_chrg = arglist;
2216 struct z_candidate *cand;
2217 int i;
2218 tree fn;
2220 /* We don't do deduction on the in-charge parameter, the VTT
2221 parameter or 'this'. */
2222 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2223 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2225 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2226 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2227 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
2228 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2230 i = fn_type_unification (tmpl, explicit_targs, targs,
2231 args_without_in_chrg,
2232 return_type, strict, flags);
2234 if (i != 0)
2235 return NULL;
2237 fn = instantiate_template (tmpl, targs, tf_none);
2238 if (fn == error_mark_node)
2239 return NULL;
2241 /* In [class.copy]:
2243 A member function template is never instantiated to perform the
2244 copy of a class object to an object of its class type.
2246 It's a little unclear what this means; the standard explicitly
2247 does allow a template to be used to copy a class. For example,
2250 struct A {
2251 A(A&);
2252 template <class T> A(const T&);
2254 const A f ();
2255 void g () { A a (f ()); }
2257 the member template will be used to make the copy. The section
2258 quoted above appears in the paragraph that forbids constructors
2259 whose only parameter is (a possibly cv-qualified variant of) the
2260 class type, and a logical interpretation is that the intent was
2261 to forbid the instantiation of member templates which would then
2262 have that form. */
2263 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2265 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2266 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2267 ctype))
2268 return NULL;
2271 if (obj != NULL_TREE)
2272 /* Aha, this is a conversion function. */
2273 cand = add_conv_candidate (candidates, fn, obj, access_path,
2274 conversion_path, arglist);
2275 else
2276 cand = add_function_candidate (candidates, fn, ctype,
2277 arglist, access_path,
2278 conversion_path, flags);
2279 if (DECL_TI_TEMPLATE (fn) != tmpl)
2280 /* This situation can occur if a member template of a template
2281 class is specialized. Then, instantiate_template might return
2282 an instantiation of the specialization, in which case the
2283 DECL_TI_TEMPLATE field will point at the original
2284 specialization. For example:
2286 template <class T> struct S { template <class U> void f(U);
2287 template <> void f(int) {}; };
2288 S<double> sd;
2289 sd.f(3);
2291 Here, TMPL will be template <class U> S<double>::f(U).
2292 And, instantiate template will give us the specialization
2293 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2294 for this will point at template <class T> template <> S<T>::f(int),
2295 so that we can find the definition. For the purposes of
2296 overload resolution, however, we want the original TMPL. */
2297 cand->template_decl = tree_cons (tmpl, targs, NULL_TREE);
2298 else
2299 cand->template_decl = DECL_TEMPLATE_INFO (fn);
2301 return cand;
2305 static struct z_candidate *
2306 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2307 tree explicit_targs, tree arglist, tree return_type,
2308 tree access_path, tree conversion_path, int flags,
2309 unification_kind_t strict)
2311 return
2312 add_template_candidate_real (candidates, tmpl, ctype,
2313 explicit_targs, arglist, return_type,
2314 access_path, conversion_path,
2315 flags, NULL_TREE, strict);
2319 static struct z_candidate *
2320 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2321 tree obj, tree arglist, tree return_type,
2322 tree access_path, tree conversion_path)
2324 return
2325 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2326 arglist, return_type, access_path,
2327 conversion_path, 0, obj, DEDUCE_CONV);
2330 /* The CANDS are the set of candidates that were considered for
2331 overload resolution. Return the set of viable candidates. If none
2332 of the candidates were viable, set *ANY_VIABLE_P to true. STRICT_P
2333 is true if a candidate should be considered viable only if it is
2334 strictly viable. */
2336 static struct z_candidate*
2337 splice_viable (struct z_candidate *cands,
2338 bool strict_p,
2339 bool *any_viable_p)
2341 struct z_candidate *viable;
2342 struct z_candidate **last_viable;
2343 struct z_candidate **cand;
2345 viable = NULL;
2346 last_viable = &viable;
2347 *any_viable_p = false;
2349 cand = &cands;
2350 while (*cand)
2352 struct z_candidate *c = *cand;
2353 if (strict_p ? c->viable == 1 : c->viable)
2355 *last_viable = c;
2356 *cand = c->next;
2357 c->next = NULL;
2358 last_viable = &c->next;
2359 *any_viable_p = true;
2361 else
2362 cand = &c->next;
2365 return viable ? viable : cands;
2368 static bool
2369 any_strictly_viable (struct z_candidate *cands)
2371 for (; cands; cands = cands->next)
2372 if (cands->viable == 1)
2373 return true;
2374 return false;
2377 /* OBJ is being used in an expression like "OBJ.f (...)". In other
2378 words, it is about to become the "this" pointer for a member
2379 function call. Take the address of the object. */
2381 static tree
2382 build_this (tree obj)
2384 /* In a template, we are only concerned about the type of the
2385 expression, so we can take a shortcut. */
2386 if (processing_template_decl)
2387 return build_address (obj);
2389 return build_unary_op (ADDR_EXPR, obj, 0);
2392 /* Returns true iff functions are equivalent. Equivalent functions are
2393 not '==' only if one is a function-local extern function or if
2394 both are extern "C". */
2396 static inline int
2397 equal_functions (tree fn1, tree fn2)
2399 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2400 || DECL_EXTERN_C_FUNCTION_P (fn1))
2401 return decls_match (fn1, fn2);
2402 return fn1 == fn2;
2405 /* Print information about one overload candidate CANDIDATE. MSGSTR
2406 is the text to print before the candidate itself.
2408 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2409 to have been run through gettext by the caller. This wart makes
2410 life simpler in print_z_candidates and for the translators. */
2412 static void
2413 print_z_candidate (const char *msgstr, struct z_candidate *candidate)
2415 if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2417 if (candidate->num_convs == 3)
2418 inform ("%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
2419 candidate->convs[0]->type,
2420 candidate->convs[1]->type,
2421 candidate->convs[2]->type);
2422 else if (candidate->num_convs == 2)
2423 inform ("%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
2424 candidate->convs[0]->type,
2425 candidate->convs[1]->type);
2426 else
2427 inform ("%s %D(%T) <built-in>", msgstr, candidate->fn,
2428 candidate->convs[0]->type);
2430 else if (TYPE_P (candidate->fn))
2431 inform ("%s %T <conversion>", msgstr, candidate->fn);
2432 else if (candidate->viable == -1)
2433 inform ("%s %+#D <near match>", msgstr, candidate->fn);
2434 else
2435 inform ("%s %+#D", msgstr, candidate->fn);
2438 static void
2439 print_z_candidates (struct z_candidate *candidates)
2441 const char *str;
2442 struct z_candidate *cand1;
2443 struct z_candidate **cand2;
2445 /* There may be duplicates in the set of candidates. We put off
2446 checking this condition as long as possible, since we have no way
2447 to eliminate duplicates from a set of functions in less than n^2
2448 time. Now we are about to emit an error message, so it is more
2449 permissible to go slowly. */
2450 for (cand1 = candidates; cand1; cand1 = cand1->next)
2452 tree fn = cand1->fn;
2453 /* Skip builtin candidates and conversion functions. */
2454 if (TREE_CODE (fn) != FUNCTION_DECL)
2455 continue;
2456 cand2 = &cand1->next;
2457 while (*cand2)
2459 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2460 && equal_functions (fn, (*cand2)->fn))
2461 *cand2 = (*cand2)->next;
2462 else
2463 cand2 = &(*cand2)->next;
2467 if (!candidates)
2468 return;
2470 str = _("candidates are:");
2471 print_z_candidate (str, candidates);
2472 if (candidates->next)
2474 /* Indent successive candidates by the width of the translation
2475 of the above string. */
2476 size_t len = gcc_gettext_width (str) + 1;
2477 char *spaces = (char *) alloca (len);
2478 memset (spaces, ' ', len-1);
2479 spaces[len - 1] = '\0';
2481 candidates = candidates->next;
2484 print_z_candidate (spaces, candidates);
2485 candidates = candidates->next;
2487 while (candidates);
2491 /* USER_SEQ is a user-defined conversion sequence, beginning with a
2492 USER_CONV. STD_SEQ is the standard conversion sequence applied to
2493 the result of the conversion function to convert it to the final
2494 desired type. Merge the two sequences into a single sequence,
2495 and return the merged sequence. */
2497 static conversion *
2498 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
2500 conversion **t;
2502 gcc_assert (user_seq->kind == ck_user);
2504 /* Find the end of the second conversion sequence. */
2505 t = &(std_seq);
2506 while ((*t)->kind != ck_identity)
2507 t = &((*t)->u.next);
2509 /* Replace the identity conversion with the user conversion
2510 sequence. */
2511 *t = user_seq;
2513 /* The entire sequence is a user-conversion sequence. */
2514 std_seq->user_conv_p = true;
2516 return std_seq;
2519 /* Returns the best overload candidate to perform the requested
2520 conversion. This function is used for three the overloading situations
2521 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2522 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2523 per [dcl.init.ref], so we ignore temporary bindings. */
2525 static struct z_candidate *
2526 build_user_type_conversion_1 (tree totype, tree expr, int flags)
2528 struct z_candidate *candidates, *cand;
2529 tree fromtype = TREE_TYPE (expr);
2530 tree ctors = NULL_TREE;
2531 tree conv_fns = NULL_TREE;
2532 conversion *conv = NULL;
2533 tree args = NULL_TREE;
2534 bool any_viable_p;
2536 /* We represent conversion within a hierarchy using RVALUE_CONV and
2537 BASE_CONV, as specified by [over.best.ics]; these become plain
2538 constructor calls, as specified in [dcl.init]. */
2539 gcc_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2540 || !DERIVED_FROM_P (totype, fromtype));
2542 if (IS_AGGR_TYPE (totype))
2543 ctors = lookup_fnfields (totype, complete_ctor_identifier, 0);
2545 if (IS_AGGR_TYPE (fromtype))
2546 conv_fns = lookup_conversions (fromtype);
2548 candidates = 0;
2549 flags |= LOOKUP_NO_CONVERSION;
2551 if (ctors)
2553 tree t;
2555 ctors = BASELINK_FUNCTIONS (ctors);
2557 t = build_int_cst (build_pointer_type (totype), 0);
2558 args = build_tree_list (NULL_TREE, expr);
2559 /* We should never try to call the abstract or base constructor
2560 from here. */
2561 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2562 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)));
2563 args = tree_cons (NULL_TREE, t, args);
2565 for (; ctors; ctors = OVL_NEXT (ctors))
2567 tree ctor = OVL_CURRENT (ctors);
2568 if (DECL_NONCONVERTING_P (ctor))
2569 continue;
2571 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2572 cand = add_template_candidate (&candidates, ctor, totype,
2573 NULL_TREE, args, NULL_TREE,
2574 TYPE_BINFO (totype),
2575 TYPE_BINFO (totype),
2576 flags,
2577 DEDUCE_CALL);
2578 else
2579 cand = add_function_candidate (&candidates, ctor, totype,
2580 args, TYPE_BINFO (totype),
2581 TYPE_BINFO (totype),
2582 flags);
2584 if (cand)
2585 cand->second_conv = build_identity_conv (totype, NULL_TREE);
2588 if (conv_fns)
2589 args = build_tree_list (NULL_TREE, build_this (expr));
2591 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
2593 tree fns;
2594 tree conversion_path = TREE_PURPOSE (conv_fns);
2595 int convflags = LOOKUP_NO_CONVERSION;
2597 /* If we are called to convert to a reference type, we are trying to
2598 find an lvalue binding, so don't even consider temporaries. If
2599 we don't find an lvalue binding, the caller will try again to
2600 look for a temporary binding. */
2601 if (TREE_CODE (totype) == REFERENCE_TYPE)
2602 convflags |= LOOKUP_NO_TEMP_BIND;
2604 for (fns = TREE_VALUE (conv_fns); fns; fns = OVL_NEXT (fns))
2606 tree fn = OVL_CURRENT (fns);
2608 /* [over.match.funcs] For conversion functions, the function
2609 is considered to be a member of the class of the implicit
2610 object argument for the purpose of defining the type of
2611 the implicit object parameter.
2613 So we pass fromtype as CTYPE to add_*_candidate. */
2615 if (TREE_CODE (fn) == TEMPLATE_DECL)
2616 cand = add_template_candidate (&candidates, fn, fromtype,
2617 NULL_TREE,
2618 args, totype,
2619 TYPE_BINFO (fromtype),
2620 conversion_path,
2621 flags,
2622 DEDUCE_CONV);
2623 else
2624 cand = add_function_candidate (&candidates, fn, fromtype,
2625 args,
2626 TYPE_BINFO (fromtype),
2627 conversion_path,
2628 flags);
2630 if (cand)
2632 conversion *ics
2633 = implicit_conversion (totype,
2634 TREE_TYPE (TREE_TYPE (cand->fn)),
2636 /*c_cast_p=*/false, convflags);
2638 cand->second_conv = ics;
2640 if (!ics)
2641 cand->viable = 0;
2642 else if (candidates->viable == 1 && ics->bad_p)
2643 cand->viable = -1;
2648 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2649 if (!any_viable_p)
2650 return NULL;
2652 cand = tourney (candidates);
2653 if (cand == 0)
2655 if (flags & LOOKUP_COMPLAIN)
2657 error ("conversion from %qT to %qT is ambiguous",
2658 fromtype, totype);
2659 print_z_candidates (candidates);
2662 cand = candidates; /* any one will do */
2663 cand->second_conv = build_ambiguous_conv (totype, expr);
2664 cand->second_conv->user_conv_p = true;
2665 if (!any_strictly_viable (candidates))
2666 cand->second_conv->bad_p = true;
2667 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2668 ambiguous conversion is no worse than another user-defined
2669 conversion. */
2671 return cand;
2674 /* Build the user conversion sequence. */
2675 conv = build_conv
2676 (ck_user,
2677 (DECL_CONSTRUCTOR_P (cand->fn)
2678 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2679 build_identity_conv (TREE_TYPE (expr), expr));
2680 conv->cand = cand;
2682 /* Combine it with the second conversion sequence. */
2683 cand->second_conv = merge_conversion_sequences (conv,
2684 cand->second_conv);
2686 if (cand->viable == -1)
2687 cand->second_conv->bad_p = true;
2689 return cand;
2692 tree
2693 build_user_type_conversion (tree totype, tree expr, int flags)
2695 struct z_candidate *cand
2696 = build_user_type_conversion_1 (totype, expr, flags);
2698 if (cand)
2700 if (cand->second_conv->kind == ck_ambig)
2701 return error_mark_node;
2702 expr = convert_like (cand->second_conv, expr);
2703 return convert_from_reference (expr);
2705 return NULL_TREE;
2708 /* Do any initial processing on the arguments to a function call. */
2710 static tree
2711 resolve_args (tree args)
2713 tree t;
2714 for (t = args; t; t = TREE_CHAIN (t))
2716 tree arg = TREE_VALUE (t);
2718 if (error_operand_p (arg))
2719 return error_mark_node;
2720 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2722 error ("invalid use of void expression");
2723 return error_mark_node;
2725 else if (invalid_nonstatic_memfn_p (arg))
2726 return error_mark_node;
2728 return args;
2731 /* Perform overload resolution on FN, which is called with the ARGS.
2733 Return the candidate function selected by overload resolution, or
2734 NULL if the event that overload resolution failed. In the case
2735 that overload resolution fails, *CANDIDATES will be the set of
2736 candidates considered, and ANY_VIABLE_P will be set to true or
2737 false to indicate whether or not any of the candidates were
2738 viable.
2740 The ARGS should already have gone through RESOLVE_ARGS before this
2741 function is called. */
2743 static struct z_candidate *
2744 perform_overload_resolution (tree fn,
2745 tree args,
2746 struct z_candidate **candidates,
2747 bool *any_viable_p)
2749 struct z_candidate *cand;
2750 tree explicit_targs = NULL_TREE;
2751 int template_only = 0;
2753 *candidates = NULL;
2754 *any_viable_p = true;
2756 /* Check FN and ARGS. */
2757 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
2758 || TREE_CODE (fn) == TEMPLATE_DECL
2759 || TREE_CODE (fn) == OVERLOAD
2760 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
2761 gcc_assert (!args || TREE_CODE (args) == TREE_LIST);
2763 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2765 explicit_targs = TREE_OPERAND (fn, 1);
2766 fn = TREE_OPERAND (fn, 0);
2767 template_only = 1;
2770 /* Add the various candidate functions. */
2771 add_candidates (fn, args, explicit_targs, template_only,
2772 /*conversion_path=*/NULL_TREE,
2773 /*access_path=*/NULL_TREE,
2774 LOOKUP_NORMAL,
2775 candidates);
2777 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
2778 if (!*any_viable_p)
2779 return NULL;
2781 cand = tourney (*candidates);
2782 return cand;
2785 /* Return an expression for a call to FN (a namespace-scope function,
2786 or a static member function) with the ARGS. */
2788 tree
2789 build_new_function_call (tree fn, tree args, bool koenig_p)
2791 struct z_candidate *candidates, *cand;
2792 bool any_viable_p;
2793 void *p;
2794 tree result;
2796 args = resolve_args (args);
2797 if (args == error_mark_node)
2798 return error_mark_node;
2800 /* If this function was found without using argument dependent
2801 lookup, then we want to ignore any undeclared friend
2802 functions. */
2803 if (!koenig_p)
2805 tree orig_fn = fn;
2807 fn = remove_hidden_names (fn);
2808 if (!fn)
2810 error ("no matching function for call to %<%D(%A)%>",
2811 DECL_NAME (OVL_CURRENT (orig_fn)), args);
2812 return error_mark_node;
2816 /* Get the high-water mark for the CONVERSION_OBSTACK. */
2817 p = conversion_obstack_alloc (0);
2819 cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p);
2821 if (!cand)
2823 if (!any_viable_p && candidates && ! candidates->next)
2824 return build_function_call (candidates->fn, args);
2825 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2826 fn = TREE_OPERAND (fn, 0);
2827 if (!any_viable_p)
2828 error ("no matching function for call to %<%D(%A)%>",
2829 DECL_NAME (OVL_CURRENT (fn)), args);
2830 else
2831 error ("call of overloaded %<%D(%A)%> is ambiguous",
2832 DECL_NAME (OVL_CURRENT (fn)), args);
2833 if (candidates)
2834 print_z_candidates (candidates);
2835 result = error_mark_node;
2837 else
2838 result = build_over_call (cand, LOOKUP_NORMAL);
2840 /* Free all the conversions we allocated. */
2841 obstack_free (&conversion_obstack, p);
2843 return result;
2846 /* Build a call to a global operator new. FNNAME is the name of the
2847 operator (either "operator new" or "operator new[]") and ARGS are
2848 the arguments provided. *SIZE points to the total number of bytes
2849 required by the allocation, and is updated if that is changed here.
2850 *COOKIE_SIZE is non-NULL if a cookie should be used. If this
2851 function determines that no cookie should be used, after all,
2852 *COOKIE_SIZE is set to NULL_TREE. If FN is non-NULL, it will be
2853 set, upon return, to the allocation function called. */
2855 tree
2856 build_operator_new_call (tree fnname, tree args,
2857 tree *size, tree *cookie_size,
2858 tree *fn)
2860 tree fns;
2861 struct z_candidate *candidates;
2862 struct z_candidate *cand;
2863 bool any_viable_p;
2865 if (fn)
2866 *fn = NULL_TREE;
2867 args = tree_cons (NULL_TREE, *size, args);
2868 args = resolve_args (args);
2869 if (args == error_mark_node)
2870 return args;
2872 /* Based on:
2874 [expr.new]
2876 If this lookup fails to find the name, or if the allocated type
2877 is not a class type, the allocation function's name is looked
2878 up in the global scope.
2880 we disregard block-scope declarations of "operator new". */
2881 fns = lookup_function_nonclass (fnname, args, /*block_p=*/false);
2883 /* Figure out what function is being called. */
2884 cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p);
2886 /* If no suitable function could be found, issue an error message
2887 and give up. */
2888 if (!cand)
2890 if (!any_viable_p)
2891 error ("no matching function for call to %<%D(%A)%>",
2892 DECL_NAME (OVL_CURRENT (fns)), args);
2893 else
2894 error ("call of overloaded %<%D(%A)%> is ambiguous",
2895 DECL_NAME (OVL_CURRENT (fns)), args);
2896 if (candidates)
2897 print_z_candidates (candidates);
2898 return error_mark_node;
2901 /* If a cookie is required, add some extra space. Whether
2902 or not a cookie is required cannot be determined until
2903 after we know which function was called. */
2904 if (*cookie_size)
2906 bool use_cookie = true;
2907 if (!abi_version_at_least (2))
2909 tree placement = TREE_CHAIN (args);
2910 /* In G++ 3.2, the check was implemented incorrectly; it
2911 looked at the placement expression, rather than the
2912 type of the function. */
2913 if (placement && !TREE_CHAIN (placement)
2914 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2915 ptr_type_node))
2916 use_cookie = false;
2918 else
2920 tree arg_types;
2922 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
2923 /* Skip the size_t parameter. */
2924 arg_types = TREE_CHAIN (arg_types);
2925 /* Check the remaining parameters (if any). */
2926 if (arg_types
2927 && TREE_CHAIN (arg_types) == void_list_node
2928 && same_type_p (TREE_VALUE (arg_types),
2929 ptr_type_node))
2930 use_cookie = false;
2932 /* If we need a cookie, adjust the number of bytes allocated. */
2933 if (use_cookie)
2935 /* Update the total size. */
2936 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
2937 /* Update the argument list to reflect the adjusted size. */
2938 TREE_VALUE (args) = *size;
2940 else
2941 *cookie_size = NULL_TREE;
2944 /* Tell our caller which function we decided to call. */
2945 if (fn)
2946 *fn = cand->fn;
2948 /* Build the CALL_EXPR. */
2949 return build_over_call (cand, LOOKUP_NORMAL);
2952 static tree
2953 build_object_call (tree obj, tree args)
2955 struct z_candidate *candidates = 0, *cand;
2956 tree fns, convs, mem_args = NULL_TREE;
2957 tree type = TREE_TYPE (obj);
2958 bool any_viable_p;
2959 tree result = NULL_TREE;
2960 void *p;
2962 if (TYPE_PTRMEMFUNC_P (type))
2964 /* It's no good looking for an overloaded operator() on a
2965 pointer-to-member-function. */
2966 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2967 return error_mark_node;
2970 if (TYPE_BINFO (type))
2972 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2973 if (fns == error_mark_node)
2974 return error_mark_node;
2976 else
2977 fns = NULL_TREE;
2979 args = resolve_args (args);
2981 if (args == error_mark_node)
2982 return error_mark_node;
2984 /* Get the high-water mark for the CONVERSION_OBSTACK. */
2985 p = conversion_obstack_alloc (0);
2987 if (fns)
2989 tree base = BINFO_TYPE (BASELINK_BINFO (fns));
2990 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2992 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2994 tree fn = OVL_CURRENT (fns);
2995 if (TREE_CODE (fn) == TEMPLATE_DECL)
2996 add_template_candidate (&candidates, fn, base, NULL_TREE,
2997 mem_args, NULL_TREE,
2998 TYPE_BINFO (type),
2999 TYPE_BINFO (type),
3000 LOOKUP_NORMAL, DEDUCE_CALL);
3001 else
3002 add_function_candidate
3003 (&candidates, fn, base, mem_args, TYPE_BINFO (type),
3004 TYPE_BINFO (type), LOOKUP_NORMAL);
3008 convs = lookup_conversions (type);
3010 for (; convs; convs = TREE_CHAIN (convs))
3012 tree fns = TREE_VALUE (convs);
3013 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
3015 if ((TREE_CODE (totype) == POINTER_TYPE
3016 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
3017 || (TREE_CODE (totype) == REFERENCE_TYPE
3018 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
3019 || (TREE_CODE (totype) == REFERENCE_TYPE
3020 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
3021 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
3022 for (; fns; fns = OVL_NEXT (fns))
3024 tree fn = OVL_CURRENT (fns);
3025 if (TREE_CODE (fn) == TEMPLATE_DECL)
3026 add_template_conv_candidate
3027 (&candidates, fn, obj, args, totype,
3028 /*access_path=*/NULL_TREE,
3029 /*conversion_path=*/NULL_TREE);
3030 else
3031 add_conv_candidate (&candidates, fn, obj, args,
3032 /*conversion_path=*/NULL_TREE,
3033 /*access_path=*/NULL_TREE);
3037 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3038 if (!any_viable_p)
3040 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj), args);
3041 print_z_candidates (candidates);
3042 result = error_mark_node;
3044 else
3046 cand = tourney (candidates);
3047 if (cand == 0)
3049 error ("call of %<(%T) (%A)%> is ambiguous", TREE_TYPE (obj), args);
3050 print_z_candidates (candidates);
3051 result = error_mark_node;
3053 /* Since cand->fn will be a type, not a function, for a conversion
3054 function, we must be careful not to unconditionally look at
3055 DECL_NAME here. */
3056 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
3057 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
3058 result = build_over_call (cand, LOOKUP_NORMAL);
3059 else
3061 obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1);
3062 obj = convert_from_reference (obj);
3063 result = build_function_call (obj, args);
3067 /* Free all the conversions we allocated. */
3068 obstack_free (&conversion_obstack, p);
3070 return result;
3073 static void
3074 op_error (enum tree_code code, enum tree_code code2,
3075 tree arg1, tree arg2, tree arg3, const char *problem)
3077 const char *opname;
3079 if (code == MODIFY_EXPR)
3080 opname = assignment_operator_name_info[code2].name;
3081 else
3082 opname = operator_name_info[code].name;
3084 switch (code)
3086 case COND_EXPR:
3087 error ("%s for ternary %<operator?:%> in %<%E ? %E : %E%>",
3088 problem, arg1, arg2, arg3);
3089 break;
3091 case POSTINCREMENT_EXPR:
3092 case POSTDECREMENT_EXPR:
3093 error ("%s for %<operator%s%> in %<%E%s%>", problem, opname, arg1, opname);
3094 break;
3096 case ARRAY_REF:
3097 error ("%s for %<operator[]%> in %<%E[%E]%>", problem, arg1, arg2);
3098 break;
3100 case REALPART_EXPR:
3101 case IMAGPART_EXPR:
3102 error ("%s for %qs in %<%s %E%>", problem, opname, opname, arg1);
3103 break;
3105 default:
3106 if (arg2)
3107 error ("%s for %<operator%s%> in %<%E %s %E%>",
3108 problem, opname, arg1, opname, arg2);
3109 else
3110 error ("%s for %<operator%s%> in %<%s%E%>",
3111 problem, opname, opname, arg1);
3112 break;
3116 /* Return the implicit conversion sequence that could be used to
3117 convert E1 to E2 in [expr.cond]. */
3119 static conversion *
3120 conditional_conversion (tree e1, tree e2)
3122 tree t1 = non_reference (TREE_TYPE (e1));
3123 tree t2 = non_reference (TREE_TYPE (e2));
3124 conversion *conv;
3125 bool good_base;
3127 /* [expr.cond]
3129 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
3130 implicitly converted (clause _conv_) to the type "reference to
3131 T2", subject to the constraint that in the conversion the
3132 reference must bind directly (_dcl.init.ref_) to E1. */
3133 if (real_lvalue_p (e2))
3135 conv = implicit_conversion (build_reference_type (t2),
3138 /*c_cast_p=*/false,
3139 LOOKUP_NO_TEMP_BIND);
3140 if (conv)
3141 return conv;
3144 /* [expr.cond]
3146 If E1 and E2 have class type, and the underlying class types are
3147 the same or one is a base class of the other: E1 can be converted
3148 to match E2 if the class of T2 is the same type as, or a base
3149 class of, the class of T1, and the cv-qualification of T2 is the
3150 same cv-qualification as, or a greater cv-qualification than, the
3151 cv-qualification of T1. If the conversion is applied, E1 is
3152 changed to an rvalue of type T2 that still refers to the original
3153 source class object (or the appropriate subobject thereof). */
3154 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
3155 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
3157 if (good_base && at_least_as_qualified_p (t2, t1))
3159 conv = build_identity_conv (t1, e1);
3160 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
3161 TYPE_MAIN_VARIANT (t2)))
3162 conv = build_conv (ck_base, t2, conv);
3163 else
3164 conv = build_conv (ck_rvalue, t2, conv);
3165 return conv;
3167 else
3168 return NULL;
3170 else
3171 /* [expr.cond]
3173 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
3174 converted to the type that expression E2 would have if E2 were
3175 converted to an rvalue (or the type it has, if E2 is an rvalue). */
3176 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
3177 LOOKUP_NORMAL);
3180 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
3181 arguments to the conditional expression. */
3183 tree
3184 build_conditional_expr (tree arg1, tree arg2, tree arg3)
3186 tree arg2_type;
3187 tree arg3_type;
3188 tree result = NULL_TREE;
3189 tree result_type = NULL_TREE;
3190 bool lvalue_p = true;
3191 struct z_candidate *candidates = 0;
3192 struct z_candidate *cand;
3193 void *p;
3195 /* As a G++ extension, the second argument to the conditional can be
3196 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
3197 c'.) If the second operand is omitted, make sure it is
3198 calculated only once. */
3199 if (!arg2)
3201 if (pedantic)
3202 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
3204 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
3205 if (real_lvalue_p (arg1))
3206 arg2 = arg1 = stabilize_reference (arg1);
3207 else
3208 arg2 = arg1 = save_expr (arg1);
3211 /* [expr.cond]
3213 The first expr ession is implicitly converted to bool (clause
3214 _conv_). */
3215 arg1 = perform_implicit_conversion (boolean_type_node, arg1);
3217 /* If something has already gone wrong, just pass that fact up the
3218 tree. */
3219 if (error_operand_p (arg1)
3220 || error_operand_p (arg2)
3221 || error_operand_p (arg3))
3222 return error_mark_node;
3224 /* [expr.cond]
3226 If either the second or the third operand has type (possibly
3227 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3228 array-to-pointer (_conv.array_), and function-to-pointer
3229 (_conv.func_) standard conversions are performed on the second
3230 and third operands. */
3231 arg2_type = is_bitfield_expr_with_lowered_type (arg2);
3232 if (!arg2_type)
3233 arg2_type = TREE_TYPE (arg2);
3234 arg3_type = is_bitfield_expr_with_lowered_type (arg3);
3235 if (!arg3_type)
3236 arg3_type = TREE_TYPE (arg3);
3237 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3239 /* Do the conversions. We don't these for `void' type arguments
3240 since it can't have any effect and since decay_conversion
3241 does not handle that case gracefully. */
3242 if (!VOID_TYPE_P (arg2_type))
3243 arg2 = decay_conversion (arg2);
3244 if (!VOID_TYPE_P (arg3_type))
3245 arg3 = decay_conversion (arg3);
3246 arg2_type = TREE_TYPE (arg2);
3247 arg3_type = TREE_TYPE (arg3);
3249 /* [expr.cond]
3251 One of the following shall hold:
3253 --The second or the third operand (but not both) is a
3254 throw-expression (_except.throw_); the result is of the
3255 type of the other and is an rvalue.
3257 --Both the second and the third operands have type void; the
3258 result is of type void and is an rvalue.
3260 We must avoid calling force_rvalue for expressions of type
3261 "void" because it will complain that their value is being
3262 used. */
3263 if (TREE_CODE (arg2) == THROW_EXPR
3264 && TREE_CODE (arg3) != THROW_EXPR)
3266 if (!VOID_TYPE_P (arg3_type))
3267 arg3 = force_rvalue (arg3);
3268 arg3_type = TREE_TYPE (arg3);
3269 result_type = arg3_type;
3271 else if (TREE_CODE (arg2) != THROW_EXPR
3272 && TREE_CODE (arg3) == THROW_EXPR)
3274 if (!VOID_TYPE_P (arg2_type))
3275 arg2 = force_rvalue (arg2);
3276 arg2_type = TREE_TYPE (arg2);
3277 result_type = arg2_type;
3279 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3280 result_type = void_type_node;
3281 else
3283 error ("%qE has type %<void%> and is not a throw-expression",
3284 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3285 return error_mark_node;
3288 lvalue_p = false;
3289 goto valid_operands;
3291 /* [expr.cond]
3293 Otherwise, if the second and third operand have different types,
3294 and either has (possibly cv-qualified) class type, an attempt is
3295 made to convert each of those operands to the type of the other. */
3296 else if (!same_type_p (arg2_type, arg3_type)
3297 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3299 conversion *conv2;
3300 conversion *conv3;
3302 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3303 p = conversion_obstack_alloc (0);
3305 conv2 = conditional_conversion (arg2, arg3);
3306 conv3 = conditional_conversion (arg3, arg2);
3308 /* [expr.cond]
3310 If both can be converted, or one can be converted but the
3311 conversion is ambiguous, the program is ill-formed. If
3312 neither can be converted, the operands are left unchanged and
3313 further checking is performed as described below. If exactly
3314 one conversion is possible, that conversion is applied to the
3315 chosen operand and the converted operand is used in place of
3316 the original operand for the remainder of this section. */
3317 if ((conv2 && !conv2->bad_p
3318 && conv3 && !conv3->bad_p)
3319 || (conv2 && conv2->kind == ck_ambig)
3320 || (conv3 && conv3->kind == ck_ambig))
3322 error ("operands to ?: have different types %qT and %qT",
3323 arg2_type, arg3_type);
3324 result = error_mark_node;
3326 else if (conv2 && (!conv2->bad_p || !conv3))
3328 arg2 = convert_like (conv2, arg2);
3329 arg2 = convert_from_reference (arg2);
3330 arg2_type = TREE_TYPE (arg2);
3331 /* Even if CONV2 is a valid conversion, the result of the
3332 conversion may be invalid. For example, if ARG3 has type
3333 "volatile X", and X does not have a copy constructor
3334 accepting a "volatile X&", then even if ARG2 can be
3335 converted to X, the conversion will fail. */
3336 if (error_operand_p (arg2))
3337 result = error_mark_node;
3339 else if (conv3 && (!conv3->bad_p || !conv2))
3341 arg3 = convert_like (conv3, arg3);
3342 arg3 = convert_from_reference (arg3);
3343 arg3_type = TREE_TYPE (arg3);
3344 if (error_operand_p (arg3))
3345 result = error_mark_node;
3348 /* Free all the conversions we allocated. */
3349 obstack_free (&conversion_obstack, p);
3351 if (result)
3352 return result;
3354 /* If, after the conversion, both operands have class type,
3355 treat the cv-qualification of both operands as if it were the
3356 union of the cv-qualification of the operands.
3358 The standard is not clear about what to do in this
3359 circumstance. For example, if the first operand has type
3360 "const X" and the second operand has a user-defined
3361 conversion to "volatile X", what is the type of the second
3362 operand after this step? Making it be "const X" (matching
3363 the first operand) seems wrong, as that discards the
3364 qualification without actually performing a copy. Leaving it
3365 as "volatile X" seems wrong as that will result in the
3366 conditional expression failing altogether, even though,
3367 according to this step, the one operand could be converted to
3368 the type of the other. */
3369 if ((conv2 || conv3)
3370 && CLASS_TYPE_P (arg2_type)
3371 && TYPE_QUALS (arg2_type) != TYPE_QUALS (arg3_type))
3372 arg2_type = arg3_type =
3373 cp_build_qualified_type (arg2_type,
3374 TYPE_QUALS (arg2_type)
3375 | TYPE_QUALS (arg3_type));
3378 /* [expr.cond]
3380 If the second and third operands are lvalues and have the same
3381 type, the result is of that type and is an lvalue. */
3382 if (real_lvalue_p (arg2)
3383 && real_lvalue_p (arg3)
3384 && same_type_p (arg2_type, arg3_type))
3386 result_type = arg2_type;
3387 goto valid_operands;
3390 /* [expr.cond]
3392 Otherwise, the result is an rvalue. If the second and third
3393 operand do not have the same type, and either has (possibly
3394 cv-qualified) class type, overload resolution is used to
3395 determine the conversions (if any) to be applied to the operands
3396 (_over.match.oper_, _over.built_). */
3397 lvalue_p = false;
3398 if (!same_type_p (arg2_type, arg3_type)
3399 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3401 tree args[3];
3402 conversion *conv;
3403 bool any_viable_p;
3405 /* Rearrange the arguments so that add_builtin_candidate only has
3406 to know about two args. In build_builtin_candidates, the
3407 arguments are unscrambled. */
3408 args[0] = arg2;
3409 args[1] = arg3;
3410 args[2] = arg1;
3411 add_builtin_candidates (&candidates,
3412 COND_EXPR,
3413 NOP_EXPR,
3414 ansi_opname (COND_EXPR),
3415 args,
3416 LOOKUP_NORMAL);
3418 /* [expr.cond]
3420 If the overload resolution fails, the program is
3421 ill-formed. */
3422 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3423 if (!any_viable_p)
3425 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3426 print_z_candidates (candidates);
3427 return error_mark_node;
3429 cand = tourney (candidates);
3430 if (!cand)
3432 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3433 print_z_candidates (candidates);
3434 return error_mark_node;
3437 /* [expr.cond]
3439 Otherwise, the conversions thus determined are applied, and
3440 the converted operands are used in place of the original
3441 operands for the remainder of this section. */
3442 conv = cand->convs[0];
3443 arg1 = convert_like (conv, arg1);
3444 conv = cand->convs[1];
3445 arg2 = convert_like (conv, arg2);
3446 conv = cand->convs[2];
3447 arg3 = convert_like (conv, arg3);
3450 /* [expr.cond]
3452 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3453 and function-to-pointer (_conv.func_) standard conversions are
3454 performed on the second and third operands.
3456 We need to force the lvalue-to-rvalue conversion here for class types,
3457 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3458 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3459 regions. */
3461 arg2 = force_rvalue (arg2);
3462 if (!CLASS_TYPE_P (arg2_type))
3463 arg2_type = TREE_TYPE (arg2);
3465 arg3 = force_rvalue (arg3);
3466 if (!CLASS_TYPE_P (arg2_type))
3467 arg3_type = TREE_TYPE (arg3);
3469 if (arg2 == error_mark_node || arg3 == error_mark_node)
3470 return error_mark_node;
3472 /* [expr.cond]
3474 After those conversions, one of the following shall hold:
3476 --The second and third operands have the same type; the result is of
3477 that type. */
3478 if (same_type_p (arg2_type, arg3_type))
3479 result_type = arg2_type;
3480 /* [expr.cond]
3482 --The second and third operands have arithmetic or enumeration
3483 type; the usual arithmetic conversions are performed to bring
3484 them to a common type, and the result is of that type. */
3485 else if ((ARITHMETIC_TYPE_P (arg2_type)
3486 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3487 && (ARITHMETIC_TYPE_P (arg3_type)
3488 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3490 /* In this case, there is always a common type. */
3491 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3492 arg3_type);
3494 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3495 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3496 warning (0, "enumeral mismatch in conditional expression: %qT vs %qT",
3497 arg2_type, arg3_type);
3498 else if (extra_warnings
3499 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3500 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3501 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3502 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3503 warning (0, "enumeral and non-enumeral type in conditional expression");
3505 arg2 = perform_implicit_conversion (result_type, arg2);
3506 arg3 = perform_implicit_conversion (result_type, arg3);
3508 /* [expr.cond]
3510 --The second and third operands have pointer type, or one has
3511 pointer type and the other is a null pointer constant; pointer
3512 conversions (_conv.ptr_) and qualification conversions
3513 (_conv.qual_) are performed to bring them to their composite
3514 pointer type (_expr.rel_). The result is of the composite
3515 pointer type.
3517 --The second and third operands have pointer to member type, or
3518 one has pointer to member type and the other is a null pointer
3519 constant; pointer to member conversions (_conv.mem_) and
3520 qualification conversions (_conv.qual_) are performed to bring
3521 them to a common type, whose cv-qualification shall match the
3522 cv-qualification of either the second or the third operand.
3523 The result is of the common type. */
3524 else if ((null_ptr_cst_p (arg2)
3525 && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
3526 || (null_ptr_cst_p (arg3)
3527 && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
3528 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3529 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3530 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
3532 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3533 arg3, "conditional expression");
3534 if (result_type == error_mark_node)
3535 return error_mark_node;
3536 arg2 = perform_implicit_conversion (result_type, arg2);
3537 arg3 = perform_implicit_conversion (result_type, arg3);
3540 if (!result_type)
3542 error ("operands to ?: have different types %qT and %qT",
3543 arg2_type, arg3_type);
3544 return error_mark_node;
3547 valid_operands:
3548 result = fold_if_not_in_template (build3 (COND_EXPR, result_type, arg1,
3549 arg2, arg3));
3550 /* We can't use result_type below, as fold might have returned a
3551 throw_expr. */
3553 if (!lvalue_p)
3555 /* Expand both sides into the same slot, hopefully the target of
3556 the ?: expression. We used to check for TARGET_EXPRs here,
3557 but now we sometimes wrap them in NOP_EXPRs so the test would
3558 fail. */
3559 if (CLASS_TYPE_P (TREE_TYPE (result)))
3560 result = get_target_expr (result);
3561 /* If this expression is an rvalue, but might be mistaken for an
3562 lvalue, we must add a NON_LVALUE_EXPR. */
3563 result = rvalue (result);
3566 return result;
3569 /* OPERAND is an operand to an expression. Perform necessary steps
3570 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
3571 returned. */
3573 static tree
3574 prep_operand (tree operand)
3576 if (operand)
3578 if (CLASS_TYPE_P (TREE_TYPE (operand))
3579 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
3580 /* Make sure the template type is instantiated now. */
3581 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
3584 return operand;
3587 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
3588 OVERLOAD) to the CANDIDATES, returning an updated list of
3589 CANDIDATES. The ARGS are the arguments provided to the call,
3590 without any implicit object parameter. The EXPLICIT_TARGS are
3591 explicit template arguments provided. TEMPLATE_ONLY is true if
3592 only template functions should be considered. CONVERSION_PATH,
3593 ACCESS_PATH, and FLAGS are as for add_function_candidate. */
3595 static void
3596 add_candidates (tree fns, tree args,
3597 tree explicit_targs, bool template_only,
3598 tree conversion_path, tree access_path,
3599 int flags,
3600 struct z_candidate **candidates)
3602 tree ctype;
3603 tree non_static_args;
3605 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
3606 /* Delay creating the implicit this parameter until it is needed. */
3607 non_static_args = NULL_TREE;
3609 while (fns)
3611 tree fn;
3612 tree fn_args;
3614 fn = OVL_CURRENT (fns);
3615 /* Figure out which set of arguments to use. */
3616 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3618 /* If this function is a non-static member, prepend the implicit
3619 object parameter. */
3620 if (!non_static_args)
3621 non_static_args = tree_cons (NULL_TREE,
3622 build_this (TREE_VALUE (args)),
3623 TREE_CHAIN (args));
3624 fn_args = non_static_args;
3626 else
3627 /* Otherwise, just use the list of arguments provided. */
3628 fn_args = args;
3630 if (TREE_CODE (fn) == TEMPLATE_DECL)
3631 add_template_candidate (candidates,
3633 ctype,
3634 explicit_targs,
3635 fn_args,
3636 NULL_TREE,
3637 access_path,
3638 conversion_path,
3639 flags,
3640 DEDUCE_CALL);
3641 else if (!template_only)
3642 add_function_candidate (candidates,
3644 ctype,
3645 fn_args,
3646 access_path,
3647 conversion_path,
3648 flags);
3649 fns = OVL_NEXT (fns);
3653 tree
3654 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
3655 bool *overloaded_p)
3657 struct z_candidate *candidates = 0, *cand;
3658 tree arglist, fnname;
3659 tree args[3];
3660 tree result = NULL_TREE;
3661 bool result_valid_p = false;
3662 enum tree_code code2 = NOP_EXPR;
3663 conversion *conv;
3664 void *p;
3665 bool strict_p;
3666 bool any_viable_p;
3668 if (error_operand_p (arg1)
3669 || error_operand_p (arg2)
3670 || error_operand_p (arg3))
3671 return error_mark_node;
3673 if (code == MODIFY_EXPR)
3675 code2 = TREE_CODE (arg3);
3676 arg3 = NULL_TREE;
3677 fnname = ansi_assopname (code2);
3679 else
3680 fnname = ansi_opname (code);
3682 arg1 = prep_operand (arg1);
3684 switch (code)
3686 case NEW_EXPR:
3687 case VEC_NEW_EXPR:
3688 case VEC_DELETE_EXPR:
3689 case DELETE_EXPR:
3690 /* Use build_op_new_call and build_op_delete_call instead. */
3691 gcc_unreachable ();
3693 case CALL_EXPR:
3694 return build_object_call (arg1, arg2);
3696 default:
3697 break;
3700 arg2 = prep_operand (arg2);
3701 arg3 = prep_operand (arg3);
3703 if (code == COND_EXPR)
3705 if (arg2 == NULL_TREE
3706 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3707 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3708 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3709 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3710 goto builtin;
3712 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3713 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3714 goto builtin;
3716 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3717 arg2 = integer_zero_node;
3719 arglist = NULL_TREE;
3720 if (arg3)
3721 arglist = tree_cons (NULL_TREE, arg3, arglist);
3722 if (arg2)
3723 arglist = tree_cons (NULL_TREE, arg2, arglist);
3724 arglist = tree_cons (NULL_TREE, arg1, arglist);
3726 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3727 p = conversion_obstack_alloc (0);
3729 /* Add namespace-scope operators to the list of functions to
3730 consider. */
3731 add_candidates (lookup_function_nonclass (fnname, arglist, /*block_p=*/true),
3732 arglist, NULL_TREE, false, NULL_TREE, NULL_TREE,
3733 flags, &candidates);
3734 /* Add class-member operators to the candidate set. */
3735 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
3737 tree fns;
3739 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
3740 if (fns == error_mark_node)
3742 result = error_mark_node;
3743 goto user_defined_result_ready;
3745 if (fns)
3746 add_candidates (BASELINK_FUNCTIONS (fns), arglist,
3747 NULL_TREE, false,
3748 BASELINK_BINFO (fns),
3749 TYPE_BINFO (TREE_TYPE (arg1)),
3750 flags, &candidates);
3753 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3754 to know about two args; a builtin candidate will always have a first
3755 parameter of type bool. We'll handle that in
3756 build_builtin_candidate. */
3757 if (code == COND_EXPR)
3759 args[0] = arg2;
3760 args[1] = arg3;
3761 args[2] = arg1;
3763 else
3765 args[0] = arg1;
3766 args[1] = arg2;
3767 args[2] = NULL_TREE;
3770 add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
3772 switch (code)
3774 case COMPOUND_EXPR:
3775 case ADDR_EXPR:
3776 /* For these, the built-in candidates set is empty
3777 [over.match.oper]/3. We don't want non-strict matches
3778 because exact matches are always possible with built-in
3779 operators. The built-in candidate set for COMPONENT_REF
3780 would be empty too, but since there are no such built-in
3781 operators, we accept non-strict matches for them. */
3782 strict_p = true;
3783 break;
3785 default:
3786 strict_p = pedantic;
3787 break;
3790 candidates = splice_viable (candidates, strict_p, &any_viable_p);
3791 if (!any_viable_p)
3793 switch (code)
3795 case POSTINCREMENT_EXPR:
3796 case POSTDECREMENT_EXPR:
3797 /* Look for an `operator++ (int)'. If they didn't have
3798 one, then we fall back to the old way of doing things. */
3799 if (flags & LOOKUP_COMPLAIN)
3800 pedwarn ("no %<%D(int)%> declared for postfix %qs, "
3801 "trying prefix operator instead",
3802 fnname,
3803 operator_name_info[code].name);
3804 if (code == POSTINCREMENT_EXPR)
3805 code = PREINCREMENT_EXPR;
3806 else
3807 code = PREDECREMENT_EXPR;
3808 result = build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE,
3809 overloaded_p);
3810 break;
3812 /* The caller will deal with these. */
3813 case ADDR_EXPR:
3814 case COMPOUND_EXPR:
3815 case COMPONENT_REF:
3816 result = NULL_TREE;
3817 result_valid_p = true;
3818 break;
3820 default:
3821 if (flags & LOOKUP_COMPLAIN)
3823 op_error (code, code2, arg1, arg2, arg3, "no match");
3824 print_z_candidates (candidates);
3826 result = error_mark_node;
3827 break;
3830 else
3832 cand = tourney (candidates);
3833 if (cand == 0)
3835 if (flags & LOOKUP_COMPLAIN)
3837 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3838 print_z_candidates (candidates);
3840 result = error_mark_node;
3842 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3844 if (overloaded_p)
3845 *overloaded_p = true;
3847 result = build_over_call (cand, LOOKUP_NORMAL);
3849 else
3851 /* Give any warnings we noticed during overload resolution. */
3852 if (cand->warnings)
3854 struct candidate_warning *w;
3855 for (w = cand->warnings; w; w = w->next)
3856 joust (cand, w->loser, 1);
3859 /* Check for comparison of different enum types. */
3860 switch (code)
3862 case GT_EXPR:
3863 case LT_EXPR:
3864 case GE_EXPR:
3865 case LE_EXPR:
3866 case EQ_EXPR:
3867 case NE_EXPR:
3868 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3869 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3870 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3871 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3873 warning (0, "comparison between %q#T and %q#T",
3874 TREE_TYPE (arg1), TREE_TYPE (arg2));
3876 break;
3877 default:
3878 break;
3881 /* We need to strip any leading REF_BIND so that bitfields
3882 don't cause errors. This should not remove any important
3883 conversions, because builtins don't apply to class
3884 objects directly. */
3885 conv = cand->convs[0];
3886 if (conv->kind == ck_ref_bind)
3887 conv = conv->u.next;
3888 arg1 = convert_like (conv, arg1);
3889 if (arg2)
3891 conv = cand->convs[1];
3892 if (conv->kind == ck_ref_bind)
3893 conv = conv->u.next;
3894 arg2 = convert_like (conv, arg2);
3896 if (arg3)
3898 conv = cand->convs[2];
3899 if (conv->kind == ck_ref_bind)
3900 conv = conv->u.next;
3901 arg3 = convert_like (conv, arg3);
3906 user_defined_result_ready:
3908 /* Free all the conversions we allocated. */
3909 obstack_free (&conversion_obstack, p);
3911 if (result || result_valid_p)
3912 return result;
3914 builtin:
3915 switch (code)
3917 case MODIFY_EXPR:
3918 return build_modify_expr (arg1, code2, arg2);
3920 case INDIRECT_REF:
3921 return build_indirect_ref (arg1, "unary *");
3923 case PLUS_EXPR:
3924 case MINUS_EXPR:
3925 case MULT_EXPR:
3926 case TRUNC_DIV_EXPR:
3927 case GT_EXPR:
3928 case LT_EXPR:
3929 case GE_EXPR:
3930 case LE_EXPR:
3931 case EQ_EXPR:
3932 case NE_EXPR:
3933 case MAX_EXPR:
3934 case MIN_EXPR:
3935 case LSHIFT_EXPR:
3936 case RSHIFT_EXPR:
3937 case TRUNC_MOD_EXPR:
3938 case BIT_AND_EXPR:
3939 case BIT_IOR_EXPR:
3940 case BIT_XOR_EXPR:
3941 case TRUTH_ANDIF_EXPR:
3942 case TRUTH_ORIF_EXPR:
3943 return cp_build_binary_op (code, arg1, arg2);
3945 case UNARY_PLUS_EXPR:
3946 case NEGATE_EXPR:
3947 case BIT_NOT_EXPR:
3948 case TRUTH_NOT_EXPR:
3949 case PREINCREMENT_EXPR:
3950 case POSTINCREMENT_EXPR:
3951 case PREDECREMENT_EXPR:
3952 case POSTDECREMENT_EXPR:
3953 case REALPART_EXPR:
3954 case IMAGPART_EXPR:
3955 return build_unary_op (code, arg1, candidates != 0);
3957 case ARRAY_REF:
3958 return build_array_ref (arg1, arg2);
3960 case COND_EXPR:
3961 return build_conditional_expr (arg1, arg2, arg3);
3963 case MEMBER_REF:
3964 return build_m_component_ref (build_indirect_ref (arg1, NULL), arg2);
3966 /* The caller will deal with these. */
3967 case ADDR_EXPR:
3968 case COMPONENT_REF:
3969 case COMPOUND_EXPR:
3970 return NULL_TREE;
3972 default:
3973 gcc_unreachable ();
3975 return NULL_TREE;
3978 /* Build a call to operator delete. This has to be handled very specially,
3979 because the restrictions on what signatures match are different from all
3980 other call instances. For a normal delete, only a delete taking (void *)
3981 or (void *, size_t) is accepted. For a placement delete, only an exact
3982 match with the placement new is accepted.
3984 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3985 ADDR is the pointer to be deleted.
3986 SIZE is the size of the memory block to be deleted.
3987 GLOBAL_P is true if the delete-expression should not consider
3988 class-specific delete operators.
3989 PLACEMENT is the corresponding placement new call, or NULL_TREE.
3990 If PLACEMENT is non-NULL, then ALLOC_FN is the allocation function
3991 called to perform the placement new. */
3993 tree
3994 build_op_delete_call (enum tree_code code, tree addr, tree size,
3995 bool global_p, tree placement,
3996 tree alloc_fn)
3998 tree fn = NULL_TREE;
3999 tree fns, fnname, argtypes, args, type;
4000 int pass;
4002 if (addr == error_mark_node)
4003 return error_mark_node;
4005 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
4007 fnname = ansi_opname (code);
4009 if (CLASS_TYPE_P (type)
4010 && COMPLETE_TYPE_P (complete_type (type))
4011 && !global_p)
4012 /* In [class.free]
4014 If the result of the lookup is ambiguous or inaccessible, or if
4015 the lookup selects a placement deallocation function, the
4016 program is ill-formed.
4018 Therefore, we ask lookup_fnfields to complain about ambiguity. */
4020 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
4021 if (fns == error_mark_node)
4022 return error_mark_node;
4024 else
4025 fns = NULL_TREE;
4027 if (fns == NULL_TREE)
4028 fns = lookup_name_nonclass (fnname);
4030 if (placement)
4032 /* Get the parameter types for the allocation function that is
4033 being called. */
4034 gcc_assert (alloc_fn != NULL_TREE);
4035 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
4036 /* Also the second argument. */
4037 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
4039 else
4041 /* First try it without the size argument. */
4042 argtypes = void_list_node;
4043 args = NULL_TREE;
4046 /* Strip const and volatile from addr. */
4047 addr = cp_convert (ptr_type_node, addr);
4049 /* We make two tries at finding a matching `operator delete'. On
4050 the first pass, we look for a one-operator (or placement)
4051 operator delete. If we're not doing placement delete, then on
4052 the second pass we look for a two-argument delete. */
4053 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
4055 /* Go through the `operator delete' functions looking for one
4056 with a matching type. */
4057 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
4059 fn = OVL_NEXT (fn))
4061 tree t;
4063 /* The first argument must be "void *". */
4064 t = TYPE_ARG_TYPES (TREE_TYPE (OVL_CURRENT (fn)));
4065 if (!same_type_p (TREE_VALUE (t), ptr_type_node))
4066 continue;
4067 t = TREE_CHAIN (t);
4068 /* On the first pass, check the rest of the arguments. */
4069 if (pass == 0)
4071 tree a = argtypes;
4072 while (a && t)
4074 if (!same_type_p (TREE_VALUE (a), TREE_VALUE (t)))
4075 break;
4076 a = TREE_CHAIN (a);
4077 t = TREE_CHAIN (t);
4079 if (!a && !t)
4080 break;
4082 /* On the second pass, the second argument must be
4083 "size_t". */
4084 else if (pass == 1
4085 && same_type_p (TREE_VALUE (t), size_type_node)
4086 && TREE_CHAIN (t) == void_list_node)
4087 break;
4090 /* If we found a match, we're done. */
4091 if (fn)
4092 break;
4095 /* If we have a matching function, call it. */
4096 if (fn)
4098 /* Make sure we have the actual function, and not an
4099 OVERLOAD. */
4100 fn = OVL_CURRENT (fn);
4102 /* If the FN is a member function, make sure that it is
4103 accessible. */
4104 if (DECL_CLASS_SCOPE_P (fn))
4105 perform_or_defer_access_check (TYPE_BINFO (type), fn, fn);
4107 if (pass == 0)
4108 args = tree_cons (NULL_TREE, addr, args);
4109 else
4110 args = tree_cons (NULL_TREE, addr,
4111 build_tree_list (NULL_TREE, size));
4113 if (placement)
4115 /* The placement args might not be suitable for overload
4116 resolution at this point, so build the call directly. */
4117 mark_used (fn);
4118 return build_cxx_call (fn, args);
4120 else
4121 return build_function_call (fn, args);
4124 /* If we are doing placement delete we do nothing if we don't find a
4125 matching op delete. */
4126 if (placement)
4127 return NULL_TREE;
4129 error ("no suitable %<operator %s%> for %qT",
4130 operator_name_info[(int)code].name, type);
4131 return error_mark_node;
4134 /* If the current scope isn't allowed to access DECL along
4135 BASETYPE_PATH, give an error. The most derived class in
4136 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
4137 the declaration to use in the error diagnostic. */
4139 bool
4140 enforce_access (tree basetype_path, tree decl, tree diag_decl)
4142 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
4144 if (!accessible_p (basetype_path, decl, true))
4146 if (TREE_PRIVATE (decl))
4147 error ("%q+#D is private", diag_decl);
4148 else if (TREE_PROTECTED (decl))
4149 error ("%q+#D is protected", diag_decl);
4150 else
4151 error ("%q+#D is inaccessible", diag_decl);
4152 error ("within this context");
4153 return false;
4156 return true;
4159 /* Check that a callable constructor to initialize a temporary of
4160 TYPE from an EXPR exists. */
4162 static void
4163 check_constructor_callable (tree type, tree expr)
4165 build_special_member_call (NULL_TREE,
4166 complete_ctor_identifier,
4167 build_tree_list (NULL_TREE, expr),
4168 type,
4169 LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING
4170 | LOOKUP_NO_CONVERSION
4171 | LOOKUP_CONSTRUCTOR_CALLABLE);
4174 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
4175 bitwise or of LOOKUP_* values. If any errors are warnings are
4176 generated, set *DIAGNOSTIC_FN to "error" or "warning",
4177 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
4178 to NULL. */
4180 static tree
4181 build_temp (tree expr, tree type, int flags,
4182 diagnostic_fn_t *diagnostic_fn)
4184 int savew, savee;
4186 savew = warningcount, savee = errorcount;
4187 expr = build_special_member_call (NULL_TREE,
4188 complete_ctor_identifier,
4189 build_tree_list (NULL_TREE, expr),
4190 type, flags);
4191 if (warningcount > savew)
4192 *diagnostic_fn = warning0;
4193 else if (errorcount > savee)
4194 *diagnostic_fn = error;
4195 else
4196 *diagnostic_fn = NULL;
4197 return expr;
4201 /* Perform the conversions in CONVS on the expression EXPR. FN and
4202 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
4203 indicates the `this' argument of a method. INNER is nonzero when
4204 being called to continue a conversion chain. It is negative when a
4205 reference binding will be applied, positive otherwise. If
4206 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
4207 conversions will be emitted if appropriate. If C_CAST_P is true,
4208 this conversion is coming from a C-style cast; in that case,
4209 conversions to inaccessible bases are permitted. */
4211 static tree
4212 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
4213 int inner, bool issue_conversion_warnings,
4214 bool c_cast_p)
4216 tree totype = convs->type;
4217 diagnostic_fn_t diagnostic_fn;
4219 if (convs->bad_p
4220 && convs->kind != ck_user
4221 && convs->kind != ck_ambig
4222 && convs->kind != ck_ref_bind)
4224 conversion *t = convs;
4225 for (; t; t = convs->u.next)
4227 if (t->kind == ck_user || !t->bad_p)
4229 expr = convert_like_real (t, expr, fn, argnum, 1,
4230 /*issue_conversion_warnings=*/false,
4231 /*c_cast_p=*/false);
4232 break;
4234 else if (t->kind == ck_ambig)
4235 return convert_like_real (t, expr, fn, argnum, 1,
4236 /*issue_conversion_warnings=*/false,
4237 /*c_cast_p=*/false);
4238 else if (t->kind == ck_identity)
4239 break;
4241 pedwarn ("invalid conversion from %qT to %qT", TREE_TYPE (expr), totype);
4242 if (fn)
4243 pedwarn (" initializing argument %P of %qD", argnum, fn);
4244 return cp_convert (totype, expr);
4247 if (issue_conversion_warnings)
4249 tree t = non_reference (totype);
4251 /* Issue warnings about peculiar, but valid, uses of NULL. */
4252 if (ARITHMETIC_TYPE_P (t) && expr == null_node)
4254 if (fn)
4255 warning (OPT_Wconversion, "passing NULL to non-pointer argument %P of %qD",
4256 argnum, fn);
4257 else
4258 warning (OPT_Wconversion, "converting to non-pointer type %qT from NULL", t);
4261 /* Warn about assigning a floating-point type to an integer type. */
4262 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
4263 && TREE_CODE (t) == INTEGER_TYPE)
4265 if (fn)
4266 warning (OPT_Wconversion, "passing %qT for argument %P to %qD",
4267 TREE_TYPE (expr), argnum, fn);
4268 else
4269 warning (OPT_Wconversion, "converting to %qT from %qT", t, TREE_TYPE (expr));
4273 switch (convs->kind)
4275 case ck_user:
4277 struct z_candidate *cand = convs->cand;
4278 tree convfn = cand->fn;
4279 tree args;
4281 if (DECL_CONSTRUCTOR_P (convfn))
4283 tree t = build_int_cst (build_pointer_type (DECL_CONTEXT (convfn)),
4286 args = build_tree_list (NULL_TREE, expr);
4287 /* We should never try to call the abstract or base constructor
4288 from here. */
4289 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (convfn)
4290 && !DECL_HAS_VTT_PARM_P (convfn));
4291 args = tree_cons (NULL_TREE, t, args);
4293 else
4294 args = build_this (expr);
4295 expr = build_over_call (cand, LOOKUP_NORMAL);
4297 /* If this is a constructor or a function returning an aggr type,
4298 we need to build up a TARGET_EXPR. */
4299 if (DECL_CONSTRUCTOR_P (convfn))
4300 expr = build_cplus_new (totype, expr);
4302 /* The result of the call is then used to direct-initialize the object
4303 that is the destination of the copy-initialization. [dcl.init]
4305 Note that this step is not reflected in the conversion sequence;
4306 it affects the semantics when we actually perform the
4307 conversion, but is not considered during overload resolution.
4309 If the target is a class, that means call a ctor. */
4310 if (IS_AGGR_TYPE (totype)
4311 && (inner >= 0 || !lvalue_p (expr)))
4313 expr = (build_temp
4314 (expr, totype,
4315 /* Core issue 84, now a DR, says that we don't
4316 allow UDCs for these args (which deliberately
4317 breaks copy-init of an auto_ptr<Base> from an
4318 auto_ptr<Derived>). */
4319 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION,
4320 &diagnostic_fn));
4322 if (diagnostic_fn)
4324 if (fn)
4325 diagnostic_fn
4326 (" initializing argument %P of %qD from result of %qD",
4327 argnum, fn, convfn);
4328 else
4329 diagnostic_fn
4330 (" initializing temporary from result of %qD", convfn);
4332 expr = build_cplus_new (totype, expr);
4334 return expr;
4336 case ck_identity:
4337 if (type_unknown_p (expr))
4338 expr = instantiate_type (totype, expr, tf_warning_or_error);
4339 /* Convert a constant to its underlying value, unless we are
4340 about to bind it to a reference, in which case we need to
4341 leave it as an lvalue. */
4342 if (inner >= 0)
4343 expr = decl_constant_value (expr);
4344 if (convs->check_copy_constructor_p)
4345 check_constructor_callable (totype, expr);
4346 return expr;
4347 case ck_ambig:
4348 /* Call build_user_type_conversion again for the error. */
4349 return build_user_type_conversion
4350 (totype, convs->u.expr, LOOKUP_NORMAL);
4352 default:
4353 break;
4356 expr = convert_like_real (convs->u.next, expr, fn, argnum,
4357 convs->kind == ck_ref_bind ? -1 : 1,
4358 /*issue_conversion_warnings=*/false,
4359 c_cast_p);
4360 if (expr == error_mark_node)
4361 return error_mark_node;
4363 switch (convs->kind)
4365 case ck_rvalue:
4366 expr = convert_bitfield_to_declared_type (expr);
4367 if (! IS_AGGR_TYPE (totype))
4368 return expr;
4369 /* Else fall through. */
4370 case ck_base:
4371 if (convs->kind == ck_base && !convs->need_temporary_p)
4373 /* We are going to bind a reference directly to a base-class
4374 subobject of EXPR. */
4375 if (convs->check_copy_constructor_p)
4376 check_constructor_callable (TREE_TYPE (expr), expr);
4377 /* Build an expression for `*((base*) &expr)'. */
4378 expr = build_unary_op (ADDR_EXPR, expr, 0);
4379 expr = convert_to_base (expr, build_pointer_type (totype),
4380 !c_cast_p, /*nonnull=*/true);
4381 expr = build_indirect_ref (expr, "implicit conversion");
4382 return expr;
4385 /* Copy-initialization where the cv-unqualified version of the source
4386 type is the same class as, or a derived class of, the class of the
4387 destination [is treated as direct-initialization]. [dcl.init] */
4388 expr = build_temp (expr, totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
4389 &diagnostic_fn);
4390 if (diagnostic_fn && fn)
4391 diagnostic_fn (" initializing argument %P of %qD", argnum, fn);
4392 return build_cplus_new (totype, expr);
4394 case ck_ref_bind:
4396 tree ref_type = totype;
4398 /* If necessary, create a temporary. */
4399 if (convs->need_temporary_p || !lvalue_p (expr))
4401 tree type = convs->u.next->type;
4402 cp_lvalue_kind lvalue = real_lvalue_p (expr);
4404 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
4406 /* If the reference is volatile or non-const, we
4407 cannot create a temporary. */
4408 if (lvalue & clk_bitfield)
4409 error ("cannot bind bitfield %qE to %qT",
4410 expr, ref_type);
4411 else if (lvalue & clk_packed)
4412 error ("cannot bind packed field %qE to %qT",
4413 expr, ref_type);
4414 else
4415 error ("cannot bind rvalue %qE to %qT", expr, ref_type);
4416 return error_mark_node;
4418 /* If the source is a packed field, and we must use a copy
4419 constructor, then building the target expr will require
4420 binding the field to the reference parameter to the
4421 copy constructor, and we'll end up with an infinite
4422 loop. If we can use a bitwise copy, then we'll be
4423 OK. */
4424 if ((lvalue & clk_packed)
4425 && CLASS_TYPE_P (type)
4426 && !TYPE_HAS_TRIVIAL_INIT_REF (type))
4428 error ("cannot bind packed field %qE to %qT",
4429 expr, ref_type);
4430 return error_mark_node;
4432 expr = build_target_expr_with_type (expr, type);
4435 /* Take the address of the thing to which we will bind the
4436 reference. */
4437 expr = build_unary_op (ADDR_EXPR, expr, 1);
4438 if (expr == error_mark_node)
4439 return error_mark_node;
4441 /* Convert it to a pointer to the type referred to by the
4442 reference. This will adjust the pointer if a derived to
4443 base conversion is being performed. */
4444 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4445 expr);
4446 /* Convert the pointer to the desired reference type. */
4447 return build_nop (ref_type, expr);
4450 case ck_lvalue:
4451 return decay_conversion (expr);
4453 case ck_qual:
4454 /* Warn about deprecated conversion if appropriate. */
4455 string_conv_p (totype, expr, 1);
4456 break;
4458 case ck_ptr:
4459 if (convs->base_p)
4460 expr = convert_to_base (expr, totype, !c_cast_p,
4461 /*nonnull=*/false);
4462 return build_nop (totype, expr);
4464 case ck_pmem:
4465 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
4466 c_cast_p);
4468 default:
4469 break;
4472 if (issue_conversion_warnings)
4473 expr = convert_and_check (totype, expr);
4474 else
4475 expr = convert (totype, expr);
4477 return expr;
4480 /* Build a call to __builtin_trap. */
4482 static tree
4483 call_builtin_trap (void)
4485 tree fn = implicit_built_in_decls[BUILT_IN_TRAP];
4487 gcc_assert (fn != NULL);
4488 fn = build_call (fn, NULL_TREE);
4489 return fn;
4492 /* ARG is being passed to a varargs function. Perform any conversions
4493 required. Return the converted value. */
4495 tree
4496 convert_arg_to_ellipsis (tree arg)
4498 /* [expr.call]
4500 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
4501 standard conversions are performed. */
4502 arg = decay_conversion (arg);
4503 /* [expr.call]
4505 If the argument has integral or enumeration type that is subject
4506 to the integral promotions (_conv.prom_), or a floating point
4507 type that is subject to the floating point promotion
4508 (_conv.fpprom_), the value of the argument is converted to the
4509 promoted type before the call. */
4510 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4511 && (TYPE_PRECISION (TREE_TYPE (arg))
4512 < TYPE_PRECISION (double_type_node)))
4513 arg = convert_to_real (double_type_node, arg);
4514 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
4515 arg = perform_integral_promotions (arg);
4517 arg = require_complete_type (arg);
4519 if (arg != error_mark_node
4520 && !pod_type_p (TREE_TYPE (arg)))
4522 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
4523 here and do a bitwise copy, but now cp_expr_size will abort if we
4524 try to do that.
4525 If the call appears in the context of a sizeof expression,
4526 there is no need to emit a warning, since the expression won't be
4527 evaluated. We keep the builtin_trap just as a safety check. */
4528 if (!skip_evaluation)
4529 warning (0, "cannot pass objects of non-POD type %q#T through %<...%>; "
4530 "call will abort at runtime", TREE_TYPE (arg));
4531 arg = call_builtin_trap ();
4532 arg = build2 (COMPOUND_EXPR, integer_type_node, arg,
4533 integer_zero_node);
4536 return arg;
4539 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4541 tree
4542 build_x_va_arg (tree expr, tree type)
4544 if (processing_template_decl)
4545 return build_min (VA_ARG_EXPR, type, expr);
4547 type = complete_type_or_else (type, NULL_TREE);
4549 if (expr == error_mark_node || !type)
4550 return error_mark_node;
4552 if (! pod_type_p (type))
4554 /* Remove reference types so we don't ICE later on. */
4555 tree type1 = non_reference (type);
4556 /* Undefined behavior [expr.call] 5.2.2/7. */
4557 warning (0, "cannot receive objects of non-POD type %q#T through %<...%>; "
4558 "call will abort at runtime", type);
4559 expr = convert (build_pointer_type (type1), null_node);
4560 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr),
4561 call_builtin_trap (), expr);
4562 expr = build_indirect_ref (expr, NULL);
4563 return expr;
4566 return build_va_arg (expr, type);
4569 /* TYPE has been given to va_arg. Apply the default conversions which
4570 would have happened when passed via ellipsis. Return the promoted
4571 type, or the passed type if there is no change. */
4573 tree
4574 cxx_type_promotes_to (tree type)
4576 tree promote;
4578 /* Perform the array-to-pointer and function-to-pointer
4579 conversions. */
4580 type = type_decays_to (type);
4582 promote = type_promotes_to (type);
4583 if (same_type_p (type, promote))
4584 promote = type;
4586 return promote;
4589 /* ARG is a default argument expression being passed to a parameter of
4590 the indicated TYPE, which is a parameter to FN. Do any required
4591 conversions. Return the converted value. */
4593 tree
4594 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
4596 /* If the ARG is an unparsed default argument expression, the
4597 conversion cannot be performed. */
4598 if (TREE_CODE (arg) == DEFAULT_ARG)
4600 error ("the default argument for parameter %d of %qD has "
4601 "not yet been parsed",
4602 parmnum, fn);
4603 return error_mark_node;
4606 if (fn && DECL_TEMPLATE_INFO (fn))
4607 arg = tsubst_default_argument (fn, type, arg);
4609 arg = break_out_target_exprs (arg);
4611 if (TREE_CODE (arg) == CONSTRUCTOR)
4613 arg = digest_init (type, arg);
4614 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4615 "default argument", fn, parmnum);
4617 else
4619 /* This could get clobbered by the following call. */
4620 if (TREE_HAS_CONSTRUCTOR (arg))
4621 arg = copy_node (arg);
4623 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4624 "default argument", fn, parmnum);
4625 arg = convert_for_arg_passing (type, arg);
4628 return arg;
4631 /* Returns the type which will really be used for passing an argument of
4632 type TYPE. */
4634 tree
4635 type_passed_as (tree type)
4637 /* Pass classes with copy ctors by invisible reference. */
4638 if (TREE_ADDRESSABLE (type))
4640 type = build_reference_type (type);
4641 /* There are no other pointers to this temporary. */
4642 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
4644 else if (targetm.calls.promote_prototypes (type)
4645 && INTEGRAL_TYPE_P (type)
4646 && COMPLETE_TYPE_P (type)
4647 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4648 TYPE_SIZE (integer_type_node)))
4649 type = integer_type_node;
4651 return type;
4654 /* Actually perform the appropriate conversion. */
4656 tree
4657 convert_for_arg_passing (tree type, tree val)
4659 if (val == error_mark_node)
4661 /* Pass classes with copy ctors by invisible reference. */
4662 else if (TREE_ADDRESSABLE (type))
4663 val = build1 (ADDR_EXPR, build_reference_type (type), val);
4664 else if (targetm.calls.promote_prototypes (type)
4665 && INTEGRAL_TYPE_P (type)
4666 && COMPLETE_TYPE_P (type)
4667 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4668 TYPE_SIZE (integer_type_node)))
4669 val = perform_integral_promotions (val);
4670 if (warn_missing_format_attribute)
4672 tree rhstype = TREE_TYPE (val);
4673 const enum tree_code coder = TREE_CODE (rhstype);
4674 const enum tree_code codel = TREE_CODE (type);
4675 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4676 && coder == codel
4677 && check_missing_format_attribute (type, rhstype))
4678 warning (OPT_Wmissing_format_attribute,
4679 "argument of function call might be a candidate for a format attribute");
4681 return val;
4684 /* Returns true iff FN is a function with magic varargs, i.e. ones for
4685 which no conversions at all should be done. This is true for some
4686 builtins which don't act like normal functions. */
4688 static bool
4689 magic_varargs_p (tree fn)
4691 if (DECL_BUILT_IN (fn))
4692 switch (DECL_FUNCTION_CODE (fn))
4694 case BUILT_IN_CLASSIFY_TYPE:
4695 case BUILT_IN_CONSTANT_P:
4696 case BUILT_IN_NEXT_ARG:
4697 case BUILT_IN_STDARG_START:
4698 case BUILT_IN_VA_START:
4699 return true;
4701 default:;
4704 return false;
4707 /* Subroutine of the various build_*_call functions. Overload resolution
4708 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4709 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4710 bitmask of various LOOKUP_* flags which apply to the call itself. */
4712 static tree
4713 build_over_call (struct z_candidate *cand, int flags)
4715 tree fn = cand->fn;
4716 tree args = cand->args;
4717 conversion **convs = cand->convs;
4718 conversion *conv;
4719 tree converted_args = NULL_TREE;
4720 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4721 tree arg, val;
4722 int i = 0;
4723 int is_method = 0;
4725 /* In a template, there is no need to perform all of the work that
4726 is normally done. We are only interested in the type of the call
4727 expression, i.e., the return type of the function. Any semantic
4728 errors will be deferred until the template is instantiated. */
4729 if (processing_template_decl)
4731 tree expr;
4732 tree return_type;
4733 return_type = TREE_TYPE (TREE_TYPE (fn));
4734 expr = build3 (CALL_EXPR, return_type, fn, args, NULL_TREE);
4735 if (TREE_THIS_VOLATILE (fn) && cfun)
4736 current_function_returns_abnormally = 1;
4737 if (!VOID_TYPE_P (return_type))
4738 require_complete_type (return_type);
4739 return convert_from_reference (expr);
4742 /* Give any warnings we noticed during overload resolution. */
4743 if (cand->warnings)
4745 struct candidate_warning *w;
4746 for (w = cand->warnings; w; w = w->next)
4747 joust (cand, w->loser, 1);
4750 if (DECL_FUNCTION_MEMBER_P (fn))
4752 /* If FN is a template function, two cases must be considered.
4753 For example:
4755 struct A {
4756 protected:
4757 template <class T> void f();
4759 template <class T> struct B {
4760 protected:
4761 void g();
4763 struct C : A, B<int> {
4764 using A::f; // #1
4765 using B<int>::g; // #2
4768 In case #1 where `A::f' is a member template, DECL_ACCESS is
4769 recorded in the primary template but not in its specialization.
4770 We check access of FN using its primary template.
4772 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
4773 because it is a member of class template B, DECL_ACCESS is
4774 recorded in the specialization `B<int>::g'. We cannot use its
4775 primary template because `B<T>::g' and `B<int>::g' may have
4776 different access. */
4777 if (DECL_TEMPLATE_INFO (fn)
4778 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
4779 perform_or_defer_access_check (cand->access_path,
4780 DECL_TI_TEMPLATE (fn), fn);
4781 else
4782 perform_or_defer_access_check (cand->access_path, fn, fn);
4785 if (args && TREE_CODE (args) != TREE_LIST)
4786 args = build_tree_list (NULL_TREE, args);
4787 arg = args;
4789 /* The implicit parameters to a constructor are not considered by overload
4790 resolution, and must be of the proper type. */
4791 if (DECL_CONSTRUCTOR_P (fn))
4793 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4794 arg = TREE_CHAIN (arg);
4795 parm = TREE_CHAIN (parm);
4796 /* We should never try to call the abstract constructor. */
4797 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
4799 if (DECL_HAS_VTT_PARM_P (fn))
4801 converted_args = tree_cons
4802 (NULL_TREE, TREE_VALUE (arg), converted_args);
4803 arg = TREE_CHAIN (arg);
4804 parm = TREE_CHAIN (parm);
4807 /* Bypass access control for 'this' parameter. */
4808 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4810 tree parmtype = TREE_VALUE (parm);
4811 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4812 tree converted_arg;
4813 tree base_binfo;
4815 if (convs[i]->bad_p)
4816 pedwarn ("passing %qT as %<this%> argument of %q#D discards qualifiers",
4817 TREE_TYPE (argtype), fn);
4819 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4820 X is called for an object that is not of type X, or of a type
4821 derived from X, the behavior is undefined.
4823 So we can assume that anything passed as 'this' is non-null, and
4824 optimize accordingly. */
4825 gcc_assert (TREE_CODE (parmtype) == POINTER_TYPE);
4826 /* Convert to the base in which the function was declared. */
4827 gcc_assert (cand->conversion_path != NULL_TREE);
4828 converted_arg = build_base_path (PLUS_EXPR,
4829 TREE_VALUE (arg),
4830 cand->conversion_path,
4832 /* Check that the base class is accessible. */
4833 if (!accessible_base_p (TREE_TYPE (argtype),
4834 BINFO_TYPE (cand->conversion_path), true))
4835 error ("%qT is not an accessible base of %qT",
4836 BINFO_TYPE (cand->conversion_path),
4837 TREE_TYPE (argtype));
4838 /* If fn was found by a using declaration, the conversion path
4839 will be to the derived class, not the base declaring fn. We
4840 must convert from derived to base. */
4841 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4842 TREE_TYPE (parmtype), ba_unique, NULL);
4843 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4844 base_binfo, 1);
4846 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4847 parm = TREE_CHAIN (parm);
4848 arg = TREE_CHAIN (arg);
4849 ++i;
4850 is_method = 1;
4853 for (; arg && parm;
4854 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4856 tree type = TREE_VALUE (parm);
4858 conv = convs[i];
4860 /* Don't make a copy here if build_call is going to. */
4861 if (conv->kind == ck_rvalue
4862 && !TREE_ADDRESSABLE (complete_type (type)))
4863 conv = conv->u.next;
4865 val = convert_like_with_context
4866 (conv, TREE_VALUE (arg), fn, i - is_method);
4868 val = convert_for_arg_passing (type, val);
4869 converted_args = tree_cons (NULL_TREE, val, converted_args);
4872 /* Default arguments */
4873 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4874 converted_args
4875 = tree_cons (NULL_TREE,
4876 convert_default_arg (TREE_VALUE (parm),
4877 TREE_PURPOSE (parm),
4878 fn, i - is_method),
4879 converted_args);
4881 /* Ellipsis */
4882 for (; arg; arg = TREE_CHAIN (arg))
4884 tree a = TREE_VALUE (arg);
4885 if (magic_varargs_p (fn))
4886 /* Do no conversions for magic varargs. */;
4887 else
4888 a = convert_arg_to_ellipsis (a);
4889 converted_args = tree_cons (NULL_TREE, a, converted_args);
4892 converted_args = nreverse (converted_args);
4894 check_function_arguments (TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4895 converted_args, TYPE_ARG_TYPES (TREE_TYPE (fn)));
4897 /* Avoid actually calling copy constructors and copy assignment operators,
4898 if possible. */
4900 if (! flag_elide_constructors)
4901 /* Do things the hard way. */;
4902 else if (cand->num_convs == 1 && DECL_COPY_CONSTRUCTOR_P (fn))
4904 tree targ;
4905 arg = skip_artificial_parms_for (fn, converted_args);
4906 arg = TREE_VALUE (arg);
4908 /* Pull out the real argument, disregarding const-correctness. */
4909 targ = arg;
4910 while (TREE_CODE (targ) == NOP_EXPR
4911 || TREE_CODE (targ) == NON_LVALUE_EXPR
4912 || TREE_CODE (targ) == CONVERT_EXPR)
4913 targ = TREE_OPERAND (targ, 0);
4914 if (TREE_CODE (targ) == ADDR_EXPR)
4916 targ = TREE_OPERAND (targ, 0);
4917 if (!same_type_ignoring_top_level_qualifiers_p
4918 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4919 targ = NULL_TREE;
4921 else
4922 targ = NULL_TREE;
4924 if (targ)
4925 arg = targ;
4926 else
4927 arg = build_indirect_ref (arg, 0);
4929 /* [class.copy]: the copy constructor is implicitly defined even if
4930 the implementation elided its use. */
4931 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4932 mark_used (fn);
4934 /* If we're creating a temp and we already have one, don't create a
4935 new one. If we're not creating a temp but we get one, use
4936 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4937 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4938 temp or an INIT_EXPR otherwise. */
4939 if (integer_zerop (TREE_VALUE (args)))
4941 if (TREE_CODE (arg) == TARGET_EXPR)
4942 return arg;
4943 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4944 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4946 else if (TREE_CODE (arg) == TARGET_EXPR
4947 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4949 tree to = stabilize_reference
4950 (build_indirect_ref (TREE_VALUE (args), 0));
4952 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4953 return val;
4956 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4957 && copy_fn_p (fn)
4958 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4960 tree to = stabilize_reference
4961 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4962 tree type = TREE_TYPE (to);
4963 tree as_base = CLASSTYPE_AS_BASE (type);
4965 arg = TREE_VALUE (TREE_CHAIN (converted_args));
4966 if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
4968 arg = build_indirect_ref (arg, 0);
4969 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4971 else
4973 /* We must only copy the non-tail padding parts.
4974 Use __builtin_memcpy for the bitwise copy. */
4976 tree args, t;
4978 args = tree_cons (NULL, TYPE_SIZE_UNIT (as_base), NULL);
4979 args = tree_cons (NULL, arg, args);
4980 t = build_unary_op (ADDR_EXPR, to, 0);
4981 args = tree_cons (NULL, t, args);
4982 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
4983 t = build_call (t, args);
4985 t = convert (TREE_TYPE (TREE_VALUE (args)), t);
4986 val = build_indirect_ref (t, 0);
4989 return val;
4992 mark_used (fn);
4994 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4996 tree t, *p = &TREE_VALUE (converted_args);
4997 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4998 DECL_CONTEXT (fn),
4999 ba_any, NULL);
5000 gcc_assert (binfo && binfo != error_mark_node);
5002 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
5003 if (TREE_SIDE_EFFECTS (*p))
5004 *p = save_expr (*p);
5005 t = build_pointer_type (TREE_TYPE (fn));
5006 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
5007 fn = build_java_interface_fn_ref (fn, *p);
5008 else
5009 fn = build_vfn_ref (*p, DECL_VINDEX (fn));
5010 TREE_TYPE (fn) = t;
5012 else if (DECL_INLINE (fn))
5013 fn = inline_conversion (fn);
5014 else
5015 fn = build_addr_func (fn);
5017 return build_cxx_call (fn, converted_args);
5020 /* Build and return a call to FN, using ARGS. This function performs
5021 no overload resolution, conversion, or other high-level
5022 operations. */
5024 tree
5025 build_cxx_call (tree fn, tree args)
5027 tree fndecl;
5029 fn = build_call (fn, args);
5031 /* If this call might throw an exception, note that fact. */
5032 fndecl = get_callee_fndecl (fn);
5033 if ((!fndecl || !TREE_NOTHROW (fndecl))
5034 && at_function_scope_p ()
5035 && cfun)
5036 cp_function_chain->can_throw = 1;
5038 /* Some built-in function calls will be evaluated at compile-time in
5039 fold (). */
5040 fn = fold_if_not_in_template (fn);
5042 if (VOID_TYPE_P (TREE_TYPE (fn)))
5043 return fn;
5045 fn = require_complete_type (fn);
5046 if (fn == error_mark_node)
5047 return error_mark_node;
5049 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
5050 fn = build_cplus_new (TREE_TYPE (fn), fn);
5051 return convert_from_reference (fn);
5054 static GTY(()) tree java_iface_lookup_fn;
5056 /* Make an expression which yields the address of the Java interface
5057 method FN. This is achieved by generating a call to libjava's
5058 _Jv_LookupInterfaceMethodIdx(). */
5060 static tree
5061 build_java_interface_fn_ref (tree fn, tree instance)
5063 tree lookup_args, lookup_fn, method, idx;
5064 tree klass_ref, iface, iface_ref;
5065 int i;
5067 if (!java_iface_lookup_fn)
5069 tree endlink = build_void_list_node ();
5070 tree t = tree_cons (NULL_TREE, ptr_type_node,
5071 tree_cons (NULL_TREE, ptr_type_node,
5072 tree_cons (NULL_TREE, java_int_type_node,
5073 endlink)));
5074 java_iface_lookup_fn
5075 = add_builtin_function ("_Jv_LookupInterfaceMethodIdx",
5076 build_function_type (ptr_type_node, t),
5077 0, NOT_BUILT_IN, NULL, NULL_TREE);
5080 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
5081 This is the first entry in the vtable. */
5082 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
5083 integer_zero_node);
5085 /* Get the java.lang.Class pointer for the interface being called. */
5086 iface = DECL_CONTEXT (fn);
5087 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
5088 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
5089 || DECL_CONTEXT (iface_ref) != iface)
5091 error ("could not find class$ field in java interface type %qT",
5092 iface);
5093 return error_mark_node;
5095 iface_ref = build_address (iface_ref);
5096 iface_ref = convert (build_pointer_type (iface), iface_ref);
5098 /* Determine the itable index of FN. */
5099 i = 1;
5100 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
5102 if (!DECL_VIRTUAL_P (method))
5103 continue;
5104 if (fn == method)
5105 break;
5106 i++;
5108 idx = build_int_cst (NULL_TREE, i);
5110 lookup_args = tree_cons (NULL_TREE, klass_ref,
5111 tree_cons (NULL_TREE, iface_ref,
5112 build_tree_list (NULL_TREE, idx)));
5113 lookup_fn = build1 (ADDR_EXPR,
5114 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
5115 java_iface_lookup_fn);
5116 return build3 (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
5119 /* Returns the value to use for the in-charge parameter when making a
5120 call to a function with the indicated NAME.
5122 FIXME:Can't we find a neater way to do this mapping? */
5124 tree
5125 in_charge_arg_for_name (tree name)
5127 if (name == base_ctor_identifier
5128 || name == base_dtor_identifier)
5129 return integer_zero_node;
5130 else if (name == complete_ctor_identifier)
5131 return integer_one_node;
5132 else if (name == complete_dtor_identifier)
5133 return integer_two_node;
5134 else if (name == deleting_dtor_identifier)
5135 return integer_three_node;
5137 /* This function should only be called with one of the names listed
5138 above. */
5139 gcc_unreachable ();
5140 return NULL_TREE;
5143 /* Build a call to a constructor, destructor, or an assignment
5144 operator for INSTANCE, an expression with class type. NAME
5145 indicates the special member function to call; ARGS are the
5146 arguments. BINFO indicates the base of INSTANCE that is to be
5147 passed as the `this' parameter to the member function called.
5149 FLAGS are the LOOKUP_* flags to use when processing the call.
5151 If NAME indicates a complete object constructor, INSTANCE may be
5152 NULL_TREE. In this case, the caller will call build_cplus_new to
5153 store the newly constructed object into a VAR_DECL. */
5155 tree
5156 build_special_member_call (tree instance, tree name, tree args,
5157 tree binfo, int flags)
5159 tree fns;
5160 /* The type of the subobject to be constructed or destroyed. */
5161 tree class_type;
5163 gcc_assert (name == complete_ctor_identifier
5164 || name == base_ctor_identifier
5165 || name == complete_dtor_identifier
5166 || name == base_dtor_identifier
5167 || name == deleting_dtor_identifier
5168 || name == ansi_assopname (NOP_EXPR));
5169 if (TYPE_P (binfo))
5171 /* Resolve the name. */
5172 if (!complete_type_or_else (binfo, NULL_TREE))
5173 return error_mark_node;
5175 binfo = TYPE_BINFO (binfo);
5178 gcc_assert (binfo != NULL_TREE);
5180 class_type = BINFO_TYPE (binfo);
5182 /* Handle the special case where INSTANCE is NULL_TREE. */
5183 if (name == complete_ctor_identifier && !instance)
5185 instance = build_int_cst (build_pointer_type (class_type), 0);
5186 instance = build1 (INDIRECT_REF, class_type, instance);
5188 else
5190 if (name == complete_dtor_identifier
5191 || name == base_dtor_identifier
5192 || name == deleting_dtor_identifier)
5193 gcc_assert (args == NULL_TREE);
5195 /* Convert to the base class, if necessary. */
5196 if (!same_type_ignoring_top_level_qualifiers_p
5197 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
5199 if (name != ansi_assopname (NOP_EXPR))
5200 /* For constructors and destructors, either the base is
5201 non-virtual, or it is virtual but we are doing the
5202 conversion from a constructor or destructor for the
5203 complete object. In either case, we can convert
5204 statically. */
5205 instance = convert_to_base_statically (instance, binfo);
5206 else
5207 /* However, for assignment operators, we must convert
5208 dynamically if the base is virtual. */
5209 instance = build_base_path (PLUS_EXPR, instance,
5210 binfo, /*nonnull=*/1);
5214 gcc_assert (instance != NULL_TREE);
5216 fns = lookup_fnfields (binfo, name, 1);
5218 /* When making a call to a constructor or destructor for a subobject
5219 that uses virtual base classes, pass down a pointer to a VTT for
5220 the subobject. */
5221 if ((name == base_ctor_identifier
5222 || name == base_dtor_identifier)
5223 && CLASSTYPE_VBASECLASSES (class_type))
5225 tree vtt;
5226 tree sub_vtt;
5228 /* If the current function is a complete object constructor
5229 or destructor, then we fetch the VTT directly.
5230 Otherwise, we look it up using the VTT we were given. */
5231 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
5232 vtt = decay_conversion (vtt);
5233 vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
5234 build2 (EQ_EXPR, boolean_type_node,
5235 current_in_charge_parm, integer_zero_node),
5236 current_vtt_parm,
5237 vtt);
5238 gcc_assert (BINFO_SUBVTT_INDEX (binfo));
5239 sub_vtt = build2 (PLUS_EXPR, TREE_TYPE (vtt), vtt,
5240 BINFO_SUBVTT_INDEX (binfo));
5242 args = tree_cons (NULL_TREE, sub_vtt, args);
5245 return build_new_method_call (instance, fns, args,
5246 TYPE_BINFO (BINFO_TYPE (binfo)),
5247 flags, /*fn=*/NULL);
5250 /* Return the NAME, as a C string. The NAME indicates a function that
5251 is a member of TYPE. *FREE_P is set to true if the caller must
5252 free the memory returned.
5254 Rather than go through all of this, we should simply set the names
5255 of constructors and destructors appropriately, and dispense with
5256 ctor_identifier, dtor_identifier, etc. */
5258 static char *
5259 name_as_c_string (tree name, tree type, bool *free_p)
5261 char *pretty_name;
5263 /* Assume that we will not allocate memory. */
5264 *free_p = false;
5265 /* Constructors and destructors are special. */
5266 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
5268 pretty_name
5269 = (char *) IDENTIFIER_POINTER (constructor_name (type));
5270 /* For a destructor, add the '~'. */
5271 if (name == complete_dtor_identifier
5272 || name == base_dtor_identifier
5273 || name == deleting_dtor_identifier)
5275 pretty_name = concat ("~", pretty_name, NULL);
5276 /* Remember that we need to free the memory allocated. */
5277 *free_p = true;
5280 else if (IDENTIFIER_TYPENAME_P (name))
5282 pretty_name = concat ("operator ",
5283 type_as_string (TREE_TYPE (name),
5284 TFF_PLAIN_IDENTIFIER),
5285 NULL);
5286 /* Remember that we need to free the memory allocated. */
5287 *free_p = true;
5289 else
5290 pretty_name = (char *) IDENTIFIER_POINTER (name);
5292 return pretty_name;
5295 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
5296 be set, upon return, to the function called. */
5298 tree
5299 build_new_method_call (tree instance, tree fns, tree args,
5300 tree conversion_path, int flags,
5301 tree *fn_p)
5303 struct z_candidate *candidates = 0, *cand;
5304 tree explicit_targs = NULL_TREE;
5305 tree basetype = NULL_TREE;
5306 tree access_binfo;
5307 tree optype;
5308 tree mem_args = NULL_TREE, instance_ptr;
5309 tree name;
5310 tree user_args;
5311 tree call;
5312 tree fn;
5313 tree class_type;
5314 int template_only = 0;
5315 bool any_viable_p;
5316 tree orig_instance;
5317 tree orig_fns;
5318 tree orig_args;
5319 void *p;
5321 gcc_assert (instance != NULL_TREE);
5323 /* We don't know what function we're going to call, yet. */
5324 if (fn_p)
5325 *fn_p = NULL_TREE;
5327 if (error_operand_p (instance)
5328 || error_operand_p (fns)
5329 || args == error_mark_node)
5330 return error_mark_node;
5332 if (!BASELINK_P (fns))
5334 error ("call to non-function %qD", fns);
5335 return error_mark_node;
5338 orig_instance = instance;
5339 orig_fns = fns;
5340 orig_args = args;
5342 /* Dismantle the baselink to collect all the information we need. */
5343 if (!conversion_path)
5344 conversion_path = BASELINK_BINFO (fns);
5345 access_binfo = BASELINK_ACCESS_BINFO (fns);
5346 optype = BASELINK_OPTYPE (fns);
5347 fns = BASELINK_FUNCTIONS (fns);
5348 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
5350 explicit_targs = TREE_OPERAND (fns, 1);
5351 fns = TREE_OPERAND (fns, 0);
5352 template_only = 1;
5354 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
5355 || TREE_CODE (fns) == TEMPLATE_DECL
5356 || TREE_CODE (fns) == OVERLOAD);
5357 fn = get_first_fn (fns);
5358 name = DECL_NAME (fn);
5360 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
5361 gcc_assert (CLASS_TYPE_P (basetype));
5363 if (processing_template_decl)
5365 instance = build_non_dependent_expr (instance);
5366 args = build_non_dependent_args (orig_args);
5369 /* The USER_ARGS are the arguments we will display to users if an
5370 error occurs. The USER_ARGS should not include any
5371 compiler-generated arguments. The "this" pointer hasn't been
5372 added yet. However, we must remove the VTT pointer if this is a
5373 call to a base-class constructor or destructor. */
5374 user_args = args;
5375 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
5377 /* Callers should explicitly indicate whether they want to construct
5378 the complete object or just the part without virtual bases. */
5379 gcc_assert (name != ctor_identifier);
5380 /* Similarly for destructors. */
5381 gcc_assert (name != dtor_identifier);
5382 /* Remove the VTT pointer, if present. */
5383 if ((name == base_ctor_identifier || name == base_dtor_identifier)
5384 && CLASSTYPE_VBASECLASSES (basetype))
5385 user_args = TREE_CHAIN (user_args);
5388 /* Process the argument list. */
5389 args = resolve_args (args);
5390 if (args == error_mark_node)
5391 return error_mark_node;
5393 instance_ptr = build_this (instance);
5395 /* It's OK to call destructors on cv-qualified objects. Therefore,
5396 convert the INSTANCE_PTR to the unqualified type, if necessary. */
5397 if (DECL_DESTRUCTOR_P (fn))
5399 tree type = build_pointer_type (basetype);
5400 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
5401 instance_ptr = build_nop (type, instance_ptr);
5402 name = complete_dtor_identifier;
5405 class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE);
5406 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
5408 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5409 p = conversion_obstack_alloc (0);
5411 for (fn = fns; fn; fn = OVL_NEXT (fn))
5413 tree t = OVL_CURRENT (fn);
5414 tree this_arglist;
5416 /* We can end up here for copy-init of same or base class. */
5417 if ((flags & LOOKUP_ONLYCONVERTING)
5418 && DECL_NONCONVERTING_P (t))
5419 continue;
5421 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
5422 this_arglist = mem_args;
5423 else
5424 this_arglist = args;
5426 if (TREE_CODE (t) == TEMPLATE_DECL)
5427 /* A member template. */
5428 add_template_candidate (&candidates, t,
5429 class_type,
5430 explicit_targs,
5431 this_arglist, optype,
5432 access_binfo,
5433 conversion_path,
5434 flags,
5435 DEDUCE_CALL);
5436 else if (! template_only)
5437 add_function_candidate (&candidates, t,
5438 class_type,
5439 this_arglist,
5440 access_binfo,
5441 conversion_path,
5442 flags);
5445 candidates = splice_viable (candidates, pedantic, &any_viable_p);
5446 if (!any_viable_p)
5448 if (!COMPLETE_TYPE_P (basetype))
5449 cxx_incomplete_type_error (instance_ptr, basetype);
5450 else
5452 char *pretty_name;
5453 bool free_p;
5455 pretty_name = name_as_c_string (name, basetype, &free_p);
5456 error ("no matching function for call to %<%T::%s(%A)%#V%>",
5457 basetype, pretty_name, user_args,
5458 TREE_TYPE (TREE_TYPE (instance_ptr)));
5459 if (free_p)
5460 free (pretty_name);
5462 print_z_candidates (candidates);
5463 call = error_mark_node;
5465 else
5467 cand = tourney (candidates);
5468 if (cand == 0)
5470 char *pretty_name;
5471 bool free_p;
5473 pretty_name = name_as_c_string (name, basetype, &free_p);
5474 error ("call of overloaded %<%s(%A)%> is ambiguous", pretty_name,
5475 user_args);
5476 print_z_candidates (candidates);
5477 if (free_p)
5478 free (pretty_name);
5479 call = error_mark_node;
5481 else
5483 fn = cand->fn;
5485 if (!(flags & LOOKUP_NONVIRTUAL)
5486 && DECL_PURE_VIRTUAL_P (fn)
5487 && instance == current_class_ref
5488 && (DECL_CONSTRUCTOR_P (current_function_decl)
5489 || DECL_DESTRUCTOR_P (current_function_decl)))
5490 /* This is not an error, it is runtime undefined
5491 behavior. */
5492 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl) ?
5493 "abstract virtual %q#D called from constructor"
5494 : "abstract virtual %q#D called from destructor"),
5495 fn);
5497 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
5498 && is_dummy_object (instance_ptr))
5500 error ("cannot call member function %qD without object",
5501 fn);
5502 call = error_mark_node;
5504 else
5506 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
5507 && resolves_to_fixed_type_p (instance, 0))
5508 flags |= LOOKUP_NONVIRTUAL;
5509 /* Now we know what function is being called. */
5510 if (fn_p)
5511 *fn_p = fn;
5512 /* Build the actual CALL_EXPR. */
5513 call = build_over_call (cand, flags);
5514 /* In an expression of the form `a->f()' where `f' turns
5515 out to be a static member function, `a' is
5516 none-the-less evaluated. */
5517 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
5518 && !is_dummy_object (instance_ptr)
5519 && TREE_SIDE_EFFECTS (instance_ptr))
5520 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
5521 instance_ptr, call);
5526 if (processing_template_decl && call != error_mark_node)
5527 call = (build_min_non_dep
5528 (CALL_EXPR, call,
5529 build_min_nt (COMPONENT_REF, orig_instance, orig_fns, NULL_TREE),
5530 orig_args, NULL_TREE));
5532 /* Free all the conversions we allocated. */
5533 obstack_free (&conversion_obstack, p);
5535 return call;
5538 /* Returns true iff standard conversion sequence ICS1 is a proper
5539 subsequence of ICS2. */
5541 static bool
5542 is_subseq (conversion *ics1, conversion *ics2)
5544 /* We can assume that a conversion of the same code
5545 between the same types indicates a subsequence since we only get
5546 here if the types we are converting from are the same. */
5548 while (ics1->kind == ck_rvalue
5549 || ics1->kind == ck_lvalue)
5550 ics1 = ics1->u.next;
5552 while (1)
5554 while (ics2->kind == ck_rvalue
5555 || ics2->kind == ck_lvalue)
5556 ics2 = ics2->u.next;
5558 if (ics2->kind == ck_user
5559 || ics2->kind == ck_ambig
5560 || ics2->kind == ck_identity)
5561 /* At this point, ICS1 cannot be a proper subsequence of
5562 ICS2. We can get a USER_CONV when we are comparing the
5563 second standard conversion sequence of two user conversion
5564 sequences. */
5565 return false;
5567 ics2 = ics2->u.next;
5569 if (ics2->kind == ics1->kind
5570 && same_type_p (ics2->type, ics1->type)
5571 && same_type_p (ics2->u.next->type,
5572 ics1->u.next->type))
5573 return true;
5577 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
5578 be any _TYPE nodes. */
5580 bool
5581 is_properly_derived_from (tree derived, tree base)
5583 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
5584 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
5585 return false;
5587 /* We only allow proper derivation here. The DERIVED_FROM_P macro
5588 considers every class derived from itself. */
5589 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
5590 && DERIVED_FROM_P (base, derived));
5593 /* We build the ICS for an implicit object parameter as a pointer
5594 conversion sequence. However, such a sequence should be compared
5595 as if it were a reference conversion sequence. If ICS is the
5596 implicit conversion sequence for an implicit object parameter,
5597 modify it accordingly. */
5599 static void
5600 maybe_handle_implicit_object (conversion **ics)
5602 if ((*ics)->this_p)
5604 /* [over.match.funcs]
5606 For non-static member functions, the type of the
5607 implicit object parameter is "reference to cv X"
5608 where X is the class of which the function is a
5609 member and cv is the cv-qualification on the member
5610 function declaration. */
5611 conversion *t = *ics;
5612 tree reference_type;
5614 /* The `this' parameter is a pointer to a class type. Make the
5615 implicit conversion talk about a reference to that same class
5616 type. */
5617 reference_type = TREE_TYPE (t->type);
5618 reference_type = build_reference_type (reference_type);
5620 if (t->kind == ck_qual)
5621 t = t->u.next;
5622 if (t->kind == ck_ptr)
5623 t = t->u.next;
5624 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
5625 t = direct_reference_binding (reference_type, t);
5626 *ics = t;
5630 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5631 and return the type to which the reference refers. Otherwise,
5632 leave *ICS unchanged and return NULL_TREE. */
5634 static tree
5635 maybe_handle_ref_bind (conversion **ics)
5637 if ((*ics)->kind == ck_ref_bind)
5639 conversion *old_ics = *ics;
5640 tree type = TREE_TYPE (old_ics->type);
5641 *ics = old_ics->u.next;
5642 (*ics)->user_conv_p = old_ics->user_conv_p;
5643 (*ics)->bad_p = old_ics->bad_p;
5644 return type;
5647 return NULL_TREE;
5650 /* Compare two implicit conversion sequences according to the rules set out in
5651 [over.ics.rank]. Return values:
5653 1: ics1 is better than ics2
5654 -1: ics2 is better than ics1
5655 0: ics1 and ics2 are indistinguishable */
5657 static int
5658 compare_ics (conversion *ics1, conversion *ics2)
5660 tree from_type1;
5661 tree from_type2;
5662 tree to_type1;
5663 tree to_type2;
5664 tree deref_from_type1 = NULL_TREE;
5665 tree deref_from_type2 = NULL_TREE;
5666 tree deref_to_type1 = NULL_TREE;
5667 tree deref_to_type2 = NULL_TREE;
5668 conversion_rank rank1, rank2;
5670 /* REF_BINDING is nonzero if the result of the conversion sequence
5671 is a reference type. In that case TARGET_TYPE is the
5672 type referred to by the reference. */
5673 tree target_type1;
5674 tree target_type2;
5676 /* Handle implicit object parameters. */
5677 maybe_handle_implicit_object (&ics1);
5678 maybe_handle_implicit_object (&ics2);
5680 /* Handle reference parameters. */
5681 target_type1 = maybe_handle_ref_bind (&ics1);
5682 target_type2 = maybe_handle_ref_bind (&ics2);
5684 /* [over.ics.rank]
5686 When comparing the basic forms of implicit conversion sequences (as
5687 defined in _over.best.ics_)
5689 --a standard conversion sequence (_over.ics.scs_) is a better
5690 conversion sequence than a user-defined conversion sequence
5691 or an ellipsis conversion sequence, and
5693 --a user-defined conversion sequence (_over.ics.user_) is a
5694 better conversion sequence than an ellipsis conversion sequence
5695 (_over.ics.ellipsis_). */
5696 rank1 = CONVERSION_RANK (ics1);
5697 rank2 = CONVERSION_RANK (ics2);
5699 if (rank1 > rank2)
5700 return -1;
5701 else if (rank1 < rank2)
5702 return 1;
5704 if (rank1 == cr_bad)
5706 /* XXX Isn't this an extension? */
5707 /* Both ICS are bad. We try to make a decision based on what
5708 would have happened if they'd been good. */
5709 if (ics1->user_conv_p > ics2->user_conv_p
5710 || ics1->rank > ics2->rank)
5711 return -1;
5712 else if (ics1->user_conv_p < ics2->user_conv_p
5713 || ics1->rank < ics2->rank)
5714 return 1;
5716 /* We couldn't make up our minds; try to figure it out below. */
5719 if (ics1->ellipsis_p)
5720 /* Both conversions are ellipsis conversions. */
5721 return 0;
5723 /* User-defined conversion sequence U1 is a better conversion sequence
5724 than another user-defined conversion sequence U2 if they contain the
5725 same user-defined conversion operator or constructor and if the sec-
5726 ond standard conversion sequence of U1 is better than the second
5727 standard conversion sequence of U2. */
5729 if (ics1->user_conv_p)
5731 conversion *t1;
5732 conversion *t2;
5734 for (t1 = ics1; t1->kind != ck_user; t1 = t1->u.next)
5735 if (t1->kind == ck_ambig)
5736 return 0;
5737 for (t2 = ics2; t2->kind != ck_user; t2 = t2->u.next)
5738 if (t2->kind == ck_ambig)
5739 return 0;
5741 if (t1->cand->fn != t2->cand->fn)
5742 return 0;
5744 /* We can just fall through here, after setting up
5745 FROM_TYPE1 and FROM_TYPE2. */
5746 from_type1 = t1->type;
5747 from_type2 = t2->type;
5749 else
5751 conversion *t1;
5752 conversion *t2;
5754 /* We're dealing with two standard conversion sequences.
5756 [over.ics.rank]
5758 Standard conversion sequence S1 is a better conversion
5759 sequence than standard conversion sequence S2 if
5761 --S1 is a proper subsequence of S2 (comparing the conversion
5762 sequences in the canonical form defined by _over.ics.scs_,
5763 excluding any Lvalue Transformation; the identity
5764 conversion sequence is considered to be a subsequence of
5765 any non-identity conversion sequence */
5767 t1 = ics1;
5768 while (t1->kind != ck_identity)
5769 t1 = t1->u.next;
5770 from_type1 = t1->type;
5772 t2 = ics2;
5773 while (t2->kind != ck_identity)
5774 t2 = t2->u.next;
5775 from_type2 = t2->type;
5778 if (same_type_p (from_type1, from_type2))
5780 if (is_subseq (ics1, ics2))
5781 return 1;
5782 if (is_subseq (ics2, ics1))
5783 return -1;
5785 /* Otherwise, one sequence cannot be a subsequence of the other; they
5786 don't start with the same type. This can happen when comparing the
5787 second standard conversion sequence in two user-defined conversion
5788 sequences. */
5790 /* [over.ics.rank]
5792 Or, if not that,
5794 --the rank of S1 is better than the rank of S2 (by the rules
5795 defined below):
5797 Standard conversion sequences are ordered by their ranks: an Exact
5798 Match is a better conversion than a Promotion, which is a better
5799 conversion than a Conversion.
5801 Two conversion sequences with the same rank are indistinguishable
5802 unless one of the following rules applies:
5804 --A conversion that is not a conversion of a pointer, or pointer
5805 to member, to bool is better than another conversion that is such
5806 a conversion.
5808 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5809 so that we do not have to check it explicitly. */
5810 if (ics1->rank < ics2->rank)
5811 return 1;
5812 else if (ics2->rank < ics1->rank)
5813 return -1;
5815 to_type1 = ics1->type;
5816 to_type2 = ics2->type;
5818 if (TYPE_PTR_P (from_type1)
5819 && TYPE_PTR_P (from_type2)
5820 && TYPE_PTR_P (to_type1)
5821 && TYPE_PTR_P (to_type2))
5823 deref_from_type1 = TREE_TYPE (from_type1);
5824 deref_from_type2 = TREE_TYPE (from_type2);
5825 deref_to_type1 = TREE_TYPE (to_type1);
5826 deref_to_type2 = TREE_TYPE (to_type2);
5828 /* The rules for pointers to members A::* are just like the rules
5829 for pointers A*, except opposite: if B is derived from A then
5830 A::* converts to B::*, not vice versa. For that reason, we
5831 switch the from_ and to_ variables here. */
5832 else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
5833 && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
5834 || (TYPE_PTRMEMFUNC_P (from_type1)
5835 && TYPE_PTRMEMFUNC_P (from_type2)
5836 && TYPE_PTRMEMFUNC_P (to_type1)
5837 && TYPE_PTRMEMFUNC_P (to_type2)))
5839 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
5840 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
5841 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
5842 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
5845 if (deref_from_type1 != NULL_TREE
5846 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5847 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5849 /* This was one of the pointer or pointer-like conversions.
5851 [over.ics.rank]
5853 --If class B is derived directly or indirectly from class A,
5854 conversion of B* to A* is better than conversion of B* to
5855 void*, and conversion of A* to void* is better than
5856 conversion of B* to void*. */
5857 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5858 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5860 if (is_properly_derived_from (deref_from_type1,
5861 deref_from_type2))
5862 return -1;
5863 else if (is_properly_derived_from (deref_from_type2,
5864 deref_from_type1))
5865 return 1;
5867 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5868 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5870 if (same_type_p (deref_from_type1, deref_from_type2))
5872 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5874 if (is_properly_derived_from (deref_from_type1,
5875 deref_to_type1))
5876 return 1;
5878 /* We know that DEREF_TO_TYPE1 is `void' here. */
5879 else if (is_properly_derived_from (deref_from_type1,
5880 deref_to_type2))
5881 return -1;
5884 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5885 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5887 /* [over.ics.rank]
5889 --If class B is derived directly or indirectly from class A
5890 and class C is derived directly or indirectly from B,
5892 --conversion of C* to B* is better than conversion of C* to
5895 --conversion of B* to A* is better than conversion of C* to
5896 A* */
5897 if (same_type_p (deref_from_type1, deref_from_type2))
5899 if (is_properly_derived_from (deref_to_type1,
5900 deref_to_type2))
5901 return 1;
5902 else if (is_properly_derived_from (deref_to_type2,
5903 deref_to_type1))
5904 return -1;
5906 else if (same_type_p (deref_to_type1, deref_to_type2))
5908 if (is_properly_derived_from (deref_from_type2,
5909 deref_from_type1))
5910 return 1;
5911 else if (is_properly_derived_from (deref_from_type1,
5912 deref_from_type2))
5913 return -1;
5917 else if (CLASS_TYPE_P (non_reference (from_type1))
5918 && same_type_p (from_type1, from_type2))
5920 tree from = non_reference (from_type1);
5922 /* [over.ics.rank]
5924 --binding of an expression of type C to a reference of type
5925 B& is better than binding an expression of type C to a
5926 reference of type A&
5928 --conversion of C to B is better than conversion of C to A, */
5929 if (is_properly_derived_from (from, to_type1)
5930 && is_properly_derived_from (from, to_type2))
5932 if (is_properly_derived_from (to_type1, to_type2))
5933 return 1;
5934 else if (is_properly_derived_from (to_type2, to_type1))
5935 return -1;
5938 else if (CLASS_TYPE_P (non_reference (to_type1))
5939 && same_type_p (to_type1, to_type2))
5941 tree to = non_reference (to_type1);
5943 /* [over.ics.rank]
5945 --binding of an expression of type B to a reference of type
5946 A& is better than binding an expression of type C to a
5947 reference of type A&,
5949 --conversion of B to A is better than conversion of C to A */
5950 if (is_properly_derived_from (from_type1, to)
5951 && is_properly_derived_from (from_type2, to))
5953 if (is_properly_derived_from (from_type2, from_type1))
5954 return 1;
5955 else if (is_properly_derived_from (from_type1, from_type2))
5956 return -1;
5960 /* [over.ics.rank]
5962 --S1 and S2 differ only in their qualification conversion and yield
5963 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5964 qualification signature of type T1 is a proper subset of the cv-
5965 qualification signature of type T2 */
5966 if (ics1->kind == ck_qual
5967 && ics2->kind == ck_qual
5968 && same_type_p (from_type1, from_type2))
5969 return comp_cv_qual_signature (to_type1, to_type2);
5971 /* [over.ics.rank]
5973 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5974 types to which the references refer are the same type except for
5975 top-level cv-qualifiers, and the type to which the reference
5976 initialized by S2 refers is more cv-qualified than the type to
5977 which the reference initialized by S1 refers */
5979 if (target_type1 && target_type2
5980 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5981 return comp_cv_qualification (target_type2, target_type1);
5983 /* Neither conversion sequence is better than the other. */
5984 return 0;
5987 /* The source type for this standard conversion sequence. */
5989 static tree
5990 source_type (conversion *t)
5992 for (;; t = t->u.next)
5994 if (t->kind == ck_user
5995 || t->kind == ck_ambig
5996 || t->kind == ck_identity)
5997 return t->type;
5999 gcc_unreachable ();
6002 /* Note a warning about preferring WINNER to LOSER. We do this by storing
6003 a pointer to LOSER and re-running joust to produce the warning if WINNER
6004 is actually used. */
6006 static void
6007 add_warning (struct z_candidate *winner, struct z_candidate *loser)
6009 candidate_warning *cw = (candidate_warning *)
6010 conversion_obstack_alloc (sizeof (candidate_warning));
6011 cw->loser = loser;
6012 cw->next = winner->warnings;
6013 winner->warnings = cw;
6016 /* Compare two candidates for overloading as described in
6017 [over.match.best]. Return values:
6019 1: cand1 is better than cand2
6020 -1: cand2 is better than cand1
6021 0: cand1 and cand2 are indistinguishable */
6023 static int
6024 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
6026 int winner = 0;
6027 int off1 = 0, off2 = 0;
6028 size_t i;
6029 size_t len;
6031 /* Candidates that involve bad conversions are always worse than those
6032 that don't. */
6033 if (cand1->viable > cand2->viable)
6034 return 1;
6035 if (cand1->viable < cand2->viable)
6036 return -1;
6038 /* If we have two pseudo-candidates for conversions to the same type,
6039 or two candidates for the same function, arbitrarily pick one. */
6040 if (cand1->fn == cand2->fn
6041 && (IS_TYPE_OR_DECL_P (cand1->fn)))
6042 return 1;
6044 /* a viable function F1
6045 is defined to be a better function than another viable function F2 if
6046 for all arguments i, ICSi(F1) is not a worse conversion sequence than
6047 ICSi(F2), and then */
6049 /* for some argument j, ICSj(F1) is a better conversion sequence than
6050 ICSj(F2) */
6052 /* For comparing static and non-static member functions, we ignore
6053 the implicit object parameter of the non-static function. The
6054 standard says to pretend that the static function has an object
6055 parm, but that won't work with operator overloading. */
6056 len = cand1->num_convs;
6057 if (len != cand2->num_convs)
6059 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
6060 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
6062 gcc_assert (static_1 != static_2);
6064 if (static_1)
6065 off2 = 1;
6066 else
6068 off1 = 1;
6069 --len;
6073 for (i = 0; i < len; ++i)
6075 conversion *t1 = cand1->convs[i + off1];
6076 conversion *t2 = cand2->convs[i + off2];
6077 int comp = compare_ics (t1, t2);
6079 if (comp != 0)
6081 if (warn_sign_promo
6082 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
6083 == cr_std + cr_promotion)
6084 && t1->kind == ck_std
6085 && t2->kind == ck_std
6086 && TREE_CODE (t1->type) == INTEGER_TYPE
6087 && TREE_CODE (t2->type) == INTEGER_TYPE
6088 && (TYPE_PRECISION (t1->type)
6089 == TYPE_PRECISION (t2->type))
6090 && (TYPE_UNSIGNED (t1->u.next->type)
6091 || (TREE_CODE (t1->u.next->type)
6092 == ENUMERAL_TYPE)))
6094 tree type = t1->u.next->type;
6095 tree type1, type2;
6096 struct z_candidate *w, *l;
6097 if (comp > 0)
6098 type1 = t1->type, type2 = t2->type,
6099 w = cand1, l = cand2;
6100 else
6101 type1 = t2->type, type2 = t1->type,
6102 w = cand2, l = cand1;
6104 if (warn)
6106 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
6107 type, type1, type2);
6108 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
6110 else
6111 add_warning (w, l);
6114 if (winner && comp != winner)
6116 winner = 0;
6117 goto tweak;
6119 winner = comp;
6123 /* warn about confusing overload resolution for user-defined conversions,
6124 either between a constructor and a conversion op, or between two
6125 conversion ops. */
6126 if (winner && warn_conversion && cand1->second_conv
6127 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
6128 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
6130 struct z_candidate *w, *l;
6131 bool give_warning = false;
6133 if (winner == 1)
6134 w = cand1, l = cand2;
6135 else
6136 w = cand2, l = cand1;
6138 /* We don't want to complain about `X::operator T1 ()'
6139 beating `X::operator T2 () const', when T2 is a no less
6140 cv-qualified version of T1. */
6141 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
6142 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
6144 tree t = TREE_TYPE (TREE_TYPE (l->fn));
6145 tree f = TREE_TYPE (TREE_TYPE (w->fn));
6147 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
6149 t = TREE_TYPE (t);
6150 f = TREE_TYPE (f);
6152 if (!comp_ptr_ttypes (t, f))
6153 give_warning = true;
6155 else
6156 give_warning = true;
6158 if (!give_warning)
6159 /*NOP*/;
6160 else if (warn)
6162 tree source = source_type (w->convs[0]);
6163 if (! DECL_CONSTRUCTOR_P (w->fn))
6164 source = TREE_TYPE (source);
6165 warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn);
6166 warning (OPT_Wconversion, " for conversion from %qT to %qT",
6167 source, w->second_conv->type);
6168 inform (" because conversion sequence for the argument is better");
6170 else
6171 add_warning (w, l);
6174 if (winner)
6175 return winner;
6177 /* or, if not that,
6178 F1 is a non-template function and F2 is a template function
6179 specialization. */
6181 if (!cand1->template_decl && cand2->template_decl)
6182 return 1;
6183 else if (cand1->template_decl && !cand2->template_decl)
6184 return -1;
6186 /* or, if not that,
6187 F1 and F2 are template functions and the function template for F1 is
6188 more specialized than the template for F2 according to the partial
6189 ordering rules. */
6191 if (cand1->template_decl && cand2->template_decl)
6193 winner = more_specialized_fn
6194 (TI_TEMPLATE (cand1->template_decl),
6195 TI_TEMPLATE (cand2->template_decl),
6196 /* [temp.func.order]: The presence of unused ellipsis and default
6197 arguments has no effect on the partial ordering of function
6198 templates. add_function_candidate() will not have
6199 counted the "this" argument for constructors. */
6200 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
6201 if (winner)
6202 return winner;
6205 /* or, if not that,
6206 the context is an initialization by user-defined conversion (see
6207 _dcl.init_ and _over.match.user_) and the standard conversion
6208 sequence from the return type of F1 to the destination type (i.e.,
6209 the type of the entity being initialized) is a better conversion
6210 sequence than the standard conversion sequence from the return type
6211 of F2 to the destination type. */
6213 if (cand1->second_conv)
6215 winner = compare_ics (cand1->second_conv, cand2->second_conv);
6216 if (winner)
6217 return winner;
6220 /* Check whether we can discard a builtin candidate, either because we
6221 have two identical ones or matching builtin and non-builtin candidates.
6223 (Pedantically in the latter case the builtin which matched the user
6224 function should not be added to the overload set, but we spot it here.
6226 [over.match.oper]
6227 ... the builtin candidates include ...
6228 - do not have the same parameter type list as any non-template
6229 non-member candidate. */
6231 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
6232 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
6234 for (i = 0; i < len; ++i)
6235 if (!same_type_p (cand1->convs[i]->type,
6236 cand2->convs[i]->type))
6237 break;
6238 if (i == cand1->num_convs)
6240 if (cand1->fn == cand2->fn)
6241 /* Two built-in candidates; arbitrarily pick one. */
6242 return 1;
6243 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
6244 /* cand1 is built-in; prefer cand2. */
6245 return -1;
6246 else
6247 /* cand2 is built-in; prefer cand1. */
6248 return 1;
6252 /* If the two functions are the same (this can happen with declarations
6253 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
6254 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
6255 && equal_functions (cand1->fn, cand2->fn))
6256 return 1;
6258 tweak:
6260 /* Extension: If the worst conversion for one candidate is worse than the
6261 worst conversion for the other, take the first. */
6262 if (!pedantic)
6264 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
6265 struct z_candidate *w = 0, *l = 0;
6267 for (i = 0; i < len; ++i)
6269 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
6270 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
6271 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
6272 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
6274 if (rank1 < rank2)
6275 winner = 1, w = cand1, l = cand2;
6276 if (rank1 > rank2)
6277 winner = -1, w = cand2, l = cand1;
6278 if (winner)
6280 if (warn)
6282 pedwarn ("\
6283 ISO C++ says that these are ambiguous, even \
6284 though the worst conversion for the first is better than \
6285 the worst conversion for the second:");
6286 print_z_candidate (_("candidate 1:"), w);
6287 print_z_candidate (_("candidate 2:"), l);
6289 else
6290 add_warning (w, l);
6291 return winner;
6295 gcc_assert (!winner);
6296 return 0;
6299 /* Given a list of candidates for overloading, find the best one, if any.
6300 This algorithm has a worst case of O(2n) (winner is last), and a best
6301 case of O(n/2) (totally ambiguous); much better than a sorting
6302 algorithm. */
6304 static struct z_candidate *
6305 tourney (struct z_candidate *candidates)
6307 struct z_candidate *champ = candidates, *challenger;
6308 int fate;
6309 int champ_compared_to_predecessor = 0;
6311 /* Walk through the list once, comparing each current champ to the next
6312 candidate, knocking out a candidate or two with each comparison. */
6314 for (challenger = champ->next; challenger; )
6316 fate = joust (champ, challenger, 0);
6317 if (fate == 1)
6318 challenger = challenger->next;
6319 else
6321 if (fate == 0)
6323 champ = challenger->next;
6324 if (champ == 0)
6325 return NULL;
6326 champ_compared_to_predecessor = 0;
6328 else
6330 champ = challenger;
6331 champ_compared_to_predecessor = 1;
6334 challenger = champ->next;
6338 /* Make sure the champ is better than all the candidates it hasn't yet
6339 been compared to. */
6341 for (challenger = candidates;
6342 challenger != champ
6343 && !(champ_compared_to_predecessor && challenger->next == champ);
6344 challenger = challenger->next)
6346 fate = joust (champ, challenger, 0);
6347 if (fate != 1)
6348 return NULL;
6351 return champ;
6354 /* Returns nonzero if things of type FROM can be converted to TO. */
6356 bool
6357 can_convert (tree to, tree from)
6359 return can_convert_arg (to, from, NULL_TREE, LOOKUP_NORMAL);
6362 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
6364 bool
6365 can_convert_arg (tree to, tree from, tree arg, int flags)
6367 conversion *t;
6368 void *p;
6369 bool ok_p;
6371 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6372 p = conversion_obstack_alloc (0);
6374 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
6375 flags);
6376 ok_p = (t && !t->bad_p);
6378 /* Free all the conversions we allocated. */
6379 obstack_free (&conversion_obstack, p);
6381 return ok_p;
6384 /* Like can_convert_arg, but allows dubious conversions as well. */
6386 bool
6387 can_convert_arg_bad (tree to, tree from, tree arg)
6389 conversion *t;
6390 void *p;
6392 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6393 p = conversion_obstack_alloc (0);
6394 /* Try to perform the conversion. */
6395 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
6396 LOOKUP_NORMAL);
6397 /* Free all the conversions we allocated. */
6398 obstack_free (&conversion_obstack, p);
6400 return t != NULL;
6403 /* Convert EXPR to TYPE. Return the converted expression.
6405 Note that we allow bad conversions here because by the time we get to
6406 this point we are committed to doing the conversion. If we end up
6407 doing a bad conversion, convert_like will complain. */
6409 tree
6410 perform_implicit_conversion (tree type, tree expr)
6412 conversion *conv;
6413 void *p;
6415 if (error_operand_p (expr))
6416 return error_mark_node;
6418 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6419 p = conversion_obstack_alloc (0);
6421 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6422 /*c_cast_p=*/false,
6423 LOOKUP_NORMAL);
6424 if (!conv)
6426 error ("could not convert %qE to %qT", expr, type);
6427 expr = error_mark_node;
6429 else if (processing_template_decl)
6431 /* In a template, we are only concerned about determining the
6432 type of non-dependent expressions, so we do not have to
6433 perform the actual conversion. */
6434 if (TREE_TYPE (expr) != type)
6435 expr = build_nop (type, expr);
6437 else
6438 expr = convert_like (conv, expr);
6440 /* Free all the conversions we allocated. */
6441 obstack_free (&conversion_obstack, p);
6443 return expr;
6446 /* Convert EXPR to TYPE (as a direct-initialization) if that is
6447 permitted. If the conversion is valid, the converted expression is
6448 returned. Otherwise, NULL_TREE is returned, except in the case
6449 that TYPE is a class type; in that case, an error is issued. If
6450 C_CAST_P is true, then this direction initialization is taking
6451 place as part of a static_cast being attempted as part of a C-style
6452 cast. */
6454 tree
6455 perform_direct_initialization_if_possible (tree type,
6456 tree expr,
6457 bool c_cast_p)
6459 conversion *conv;
6460 void *p;
6462 if (type == error_mark_node || error_operand_p (expr))
6463 return error_mark_node;
6464 /* [dcl.init]
6466 If the destination type is a (possibly cv-qualified) class type:
6468 -- If the initialization is direct-initialization ...,
6469 constructors are considered. ... If no constructor applies, or
6470 the overload resolution is ambiguous, the initialization is
6471 ill-formed. */
6472 if (CLASS_TYPE_P (type))
6474 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6475 build_tree_list (NULL_TREE, expr),
6476 type, LOOKUP_NORMAL);
6477 return build_cplus_new (type, expr);
6480 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6481 p = conversion_obstack_alloc (0);
6483 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6484 c_cast_p,
6485 LOOKUP_NORMAL);
6486 if (!conv || conv->bad_p)
6487 expr = NULL_TREE;
6488 else
6489 expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
6490 /*issue_conversion_warnings=*/false,
6491 c_cast_p);
6493 /* Free all the conversions we allocated. */
6494 obstack_free (&conversion_obstack, p);
6496 return expr;
6499 /* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference
6500 is being bound to a temporary. Create and return a new VAR_DECL
6501 with the indicated TYPE; this variable will store the value to
6502 which the reference is bound. */
6504 tree
6505 make_temporary_var_for_ref_to_temp (tree decl, tree type)
6507 tree var;
6509 /* Create the variable. */
6510 var = create_temporary_var (type);
6512 /* Register the variable. */
6513 if (TREE_STATIC (decl))
6515 /* Namespace-scope or local static; give it a mangled name. */
6516 tree name;
6518 TREE_STATIC (var) = 1;
6519 name = mangle_ref_init_variable (decl);
6520 DECL_NAME (var) = name;
6521 SET_DECL_ASSEMBLER_NAME (var, name);
6522 var = pushdecl_top_level (var);
6524 else
6525 /* Create a new cleanup level if necessary. */
6526 maybe_push_cleanup_level (type);
6528 return var;
6531 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
6532 initializing a variable of that TYPE. If DECL is non-NULL, it is
6533 the VAR_DECL being initialized with the EXPR. (In that case, the
6534 type of DECL will be TYPE.) If DECL is non-NULL, then CLEANUP must
6535 also be non-NULL, and with *CLEANUP initialized to NULL. Upon
6536 return, if *CLEANUP is no longer NULL, it will be an expression
6537 that should be pushed as a cleanup after the returned expression
6538 is used to initialize DECL.
6540 Return the converted expression. */
6542 tree
6543 initialize_reference (tree type, tree expr, tree decl, tree *cleanup)
6545 conversion *conv;
6546 void *p;
6548 if (type == error_mark_node || error_operand_p (expr))
6549 return error_mark_node;
6551 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6552 p = conversion_obstack_alloc (0);
6554 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
6555 if (!conv || conv->bad_p)
6557 if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
6558 && !real_lvalue_p (expr))
6559 error ("invalid initialization of non-const reference of "
6560 "type %qT from a temporary of type %qT",
6561 type, TREE_TYPE (expr));
6562 else
6563 error ("invalid initialization of reference of type "
6564 "%qT from expression of type %qT", type,
6565 TREE_TYPE (expr));
6566 return error_mark_node;
6569 /* If DECL is non-NULL, then this special rule applies:
6571 [class.temporary]
6573 The temporary to which the reference is bound or the temporary
6574 that is the complete object to which the reference is bound
6575 persists for the lifetime of the reference.
6577 The temporaries created during the evaluation of the expression
6578 initializing the reference, except the temporary to which the
6579 reference is bound, are destroyed at the end of the
6580 full-expression in which they are created.
6582 In that case, we store the converted expression into a new
6583 VAR_DECL in a new scope.
6585 However, we want to be careful not to create temporaries when
6586 they are not required. For example, given:
6588 struct B {};
6589 struct D : public B {};
6590 D f();
6591 const B& b = f();
6593 there is no need to copy the return value from "f"; we can just
6594 extend its lifetime. Similarly, given:
6596 struct S {};
6597 struct T { operator S(); };
6598 T t;
6599 const S& s = t;
6601 we can extend the lifetime of the return value of the conversion
6602 operator. */
6603 gcc_assert (conv->kind == ck_ref_bind);
6604 if (decl)
6606 tree var;
6607 tree base_conv_type;
6609 /* Skip over the REF_BIND. */
6610 conv = conv->u.next;
6611 /* If the next conversion is a BASE_CONV, skip that too -- but
6612 remember that the conversion was required. */
6613 if (conv->kind == ck_base)
6615 if (conv->check_copy_constructor_p)
6616 check_constructor_callable (TREE_TYPE (expr), expr);
6617 base_conv_type = conv->type;
6618 conv = conv->u.next;
6620 else
6621 base_conv_type = NULL_TREE;
6622 /* Perform the remainder of the conversion. */
6623 expr = convert_like_real (conv, expr,
6624 /*fn=*/NULL_TREE, /*argnum=*/0,
6625 /*inner=*/-1,
6626 /*issue_conversion_warnings=*/true,
6627 /*c_cast_p=*/false);
6628 if (error_operand_p (expr))
6629 expr = error_mark_node;
6630 else
6632 if (!real_lvalue_p (expr))
6634 tree init;
6635 tree type;
6637 /* Create the temporary variable. */
6638 type = TREE_TYPE (expr);
6639 var = make_temporary_var_for_ref_to_temp (decl, type);
6640 layout_decl (var, 0);
6641 /* If the rvalue is the result of a function call it will be
6642 a TARGET_EXPR. If it is some other construct (such as a
6643 member access expression where the underlying object is
6644 itself the result of a function call), turn it into a
6645 TARGET_EXPR here. It is important that EXPR be a
6646 TARGET_EXPR below since otherwise the INIT_EXPR will
6647 attempt to make a bitwise copy of EXPR to initialize
6648 VAR. */
6649 if (TREE_CODE (expr) != TARGET_EXPR)
6650 expr = get_target_expr (expr);
6651 /* Create the INIT_EXPR that will initialize the temporary
6652 variable. */
6653 init = build2 (INIT_EXPR, type, var, expr);
6654 if (at_function_scope_p ())
6656 add_decl_expr (var);
6657 *cleanup = cxx_maybe_build_cleanup (var);
6659 /* We must be careful to destroy the temporary only
6660 after its initialization has taken place. If the
6661 initialization throws an exception, then the
6662 destructor should not be run. We cannot simply
6663 transform INIT into something like:
6665 (INIT, ({ CLEANUP_STMT; }))
6667 because emit_local_var always treats the
6668 initializer as a full-expression. Thus, the
6669 destructor would run too early; it would run at the
6670 end of initializing the reference variable, rather
6671 than at the end of the block enclosing the
6672 reference variable.
6674 The solution is to pass back a cleanup expression
6675 which the caller is responsible for attaching to
6676 the statement tree. */
6678 else
6680 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
6681 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6682 static_aggregates = tree_cons (NULL_TREE, var,
6683 static_aggregates);
6685 /* Use its address to initialize the reference variable. */
6686 expr = build_address (var);
6687 if (base_conv_type)
6688 expr = convert_to_base (expr,
6689 build_pointer_type (base_conv_type),
6690 /*check_access=*/true,
6691 /*nonnull=*/true);
6692 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
6694 else
6695 /* Take the address of EXPR. */
6696 expr = build_unary_op (ADDR_EXPR, expr, 0);
6697 /* If a BASE_CONV was required, perform it now. */
6698 if (base_conv_type)
6699 expr = (perform_implicit_conversion
6700 (build_pointer_type (base_conv_type), expr));
6701 expr = build_nop (type, expr);
6704 else
6705 /* Perform the conversion. */
6706 expr = convert_like (conv, expr);
6708 /* Free all the conversions we allocated. */
6709 obstack_free (&conversion_obstack, p);
6711 return expr;
6714 #include "gt-cp-call.h"