Merge from mainline
[official-gcc.git] / gcc / cp / call.c
blobb522991c18e4aef2198a665d2e00cfc298ae82ac
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 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 GNU CC.
9 GNU CC 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 GNU CC 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 GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 /* High-level class interface. */
27 #include "config.h"
28 #include "system.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "output.h"
32 #include "flags.h"
33 #include "rtl.h"
34 #include "toplev.h"
35 #include "expr.h"
36 #include "ggc.h"
37 #include "diagnostic.h"
39 extern int inhibit_warnings;
41 static tree build_new_method_call PARAMS ((tree, tree, tree, tree, int));
43 static tree build_field_call PARAMS ((tree, tree, tree, tree));
44 static struct z_candidate * tourney PARAMS ((struct z_candidate *));
45 static int equal_functions PARAMS ((tree, tree));
46 static int joust PARAMS ((struct z_candidate *, struct z_candidate *, int));
47 static int compare_ics PARAMS ((tree, tree));
48 static tree build_over_call PARAMS ((struct z_candidate *, tree, int));
49 static tree build_java_interface_fn_ref PARAMS ((tree, tree));
50 #define convert_like(CONV, EXPR) convert_like_real (CONV, EXPR, NULL_TREE, 0, 0)
51 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) convert_like_real (CONV, EXPR, FN, ARGNO, 0)
52 static tree convert_like_real PARAMS ((tree, tree, tree, int, int));
53 static void op_error PARAMS ((enum tree_code, enum tree_code, tree, tree,
54 tree, const char *));
55 static tree build_object_call PARAMS ((tree, tree));
56 static tree resolve_args PARAMS ((tree));
57 static struct z_candidate * build_user_type_conversion_1
58 PARAMS ((tree, tree, int));
59 static void print_z_candidates PARAMS ((struct z_candidate *));
60 static tree build_this PARAMS ((tree));
61 static struct z_candidate * splice_viable PARAMS ((struct z_candidate *));
62 static int any_viable PARAMS ((struct z_candidate *));
63 static struct z_candidate * add_template_candidate
64 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
65 unification_kind_t));
66 static struct z_candidate * add_template_candidate_real
67 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
68 tree, unification_kind_t));
69 static struct z_candidate * add_template_conv_candidate
70 PARAMS ((struct z_candidate *, tree, tree, tree, tree));
71 static struct z_candidate * add_builtin_candidates
72 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
73 tree, tree *, int));
74 static struct z_candidate * add_builtin_candidate
75 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
76 tree, tree, tree, tree *, tree *, int));
77 static int is_complete PARAMS ((tree));
78 static struct z_candidate * build_builtin_candidate
79 PARAMS ((struct z_candidate *, tree, tree, tree, tree *, tree *,
80 int));
81 static struct z_candidate * add_conv_candidate
82 PARAMS ((struct z_candidate *, tree, tree, tree));
83 static struct z_candidate * add_function_candidate
84 PARAMS ((struct z_candidate *, tree, tree, tree, int));
85 static tree implicit_conversion PARAMS ((tree, tree, tree, int));
86 static tree standard_conversion PARAMS ((tree, tree, tree));
87 static tree reference_binding PARAMS ((tree, tree, tree, int));
88 static tree non_reference PARAMS ((tree));
89 static tree build_conv PARAMS ((enum tree_code, tree, tree));
90 static int is_subseq PARAMS ((tree, tree));
91 static int maybe_handle_ref_bind PARAMS ((tree*, tree*));
92 static void maybe_handle_implicit_object PARAMS ((tree*));
93 static struct z_candidate * add_candidate PARAMS ((struct z_candidate *,
94 tree, tree, int));
95 static tree source_type PARAMS ((tree));
96 static void add_warning PARAMS ((struct z_candidate *, struct z_candidate *));
97 static int reference_related_p PARAMS ((tree, tree));
98 static int reference_compatible_p PARAMS ((tree, tree));
99 static tree convert_class_to_reference PARAMS ((tree, tree, tree));
100 static tree direct_reference_binding PARAMS ((tree, tree));
101 static int promoted_arithmetic_type_p PARAMS ((tree));
102 static tree conditional_conversion PARAMS ((tree, tree));
104 tree
105 build_vfield_ref (datum, type)
106 tree datum, type;
108 tree rval;
110 if (datum == error_mark_node)
111 return error_mark_node;
113 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
114 datum = convert_from_reference (datum);
116 if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type))
117 rval = build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
118 datum, TYPE_VFIELD (type));
119 else
120 rval = build_component_ref (datum, TYPE_VFIELD (type), NULL_TREE);
122 return rval;
125 /* Build a call to a member of an object. I.e., one that overloads
126 operator ()(), or is a pointer-to-function or pointer-to-method. */
128 static tree
129 build_field_call (basetype_path, instance_ptr, name, parms)
130 tree basetype_path, instance_ptr, name, parms;
132 tree field, instance;
134 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
135 return NULL_TREE;
137 /* Speed up the common case. */
138 if (instance_ptr == current_class_ptr
139 && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
140 return NULL_TREE;
142 field = lookup_field (basetype_path, name, 1, 0);
144 if (field == error_mark_node || field == NULL_TREE)
145 return field;
147 if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
149 /* If it's a field, try overloading operator (),
150 or calling if the field is a pointer-to-function. */
151 instance = build_indirect_ref (instance_ptr, NULL);
152 instance = build_component_ref (instance, field,
153 /*basetype_path=*/NULL_TREE);
155 if (instance == error_mark_node)
156 return error_mark_node;
158 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
159 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
160 instance, parms, NULL_TREE);
161 else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
162 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
163 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
164 == FUNCTION_TYPE)))
165 return build_function_call (instance, parms);
168 return NULL_TREE;
171 /* Issue an error message iff the destructor name specified in NAME (a
172 BIT_NOT_EXPR) does not match BASETYPE. The operand of NAME can
173 take many forms... */
175 void
176 check_dtor_name (basetype, name)
177 tree basetype, name;
179 tree type;
181 type = DESTRUCTOR_NAME_TYPE (name);
183 /* Just accept something we've already complained about. */
184 if (type == error_mark_node)
185 return;
187 if (type && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (type))
188 return;
190 cp_error ("qualified type `%T' does not match destructor name `~%T'",
191 basetype, TREE_OPERAND (name, 0));
194 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
195 This is how virtual function calls are avoided. */
197 tree
198 build_scoped_method_call (exp, basetype, name, parms)
199 tree exp, basetype, name, parms;
201 /* Because this syntactic form does not allow
202 a pointer to a base class to be `stolen',
203 we need not protect the derived->base conversion
204 that happens here.
206 @@ But we do have to check access privileges later. */
207 tree binfo, decl;
208 tree type = TREE_TYPE (exp);
210 if (type == error_mark_node
211 || basetype == error_mark_node)
212 return error_mark_node;
214 if (TREE_CODE (type) == REFERENCE_TYPE)
215 type = TREE_TYPE (type);
217 if (TREE_CODE (basetype) == TREE_VEC)
219 binfo = basetype;
220 basetype = BINFO_TYPE (binfo);
222 else
223 binfo = NULL_TREE;
225 /* Check the destructor call syntax. */
226 if (TREE_CODE (name) == BIT_NOT_EXPR)
228 /* We can get here if someone writes their destructor call like
229 `obj.NS::~T()'; this isn't really a scoped method call, so hand
230 it off. */
231 if (TREE_CODE (basetype) == NAMESPACE_DECL)
232 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
234 check_dtor_name (basetype, name);
236 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
237 that explicit ~int is caught in the parser; this deals with typedefs
238 and template parms. */
239 if (! IS_AGGR_TYPE (basetype))
241 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
242 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
243 exp, basetype, type);
245 return cp_convert (void_type_node, exp);
249 if (TREE_CODE (basetype) == NAMESPACE_DECL)
251 cp_error ("`%D' is a namespace", basetype);
252 return error_mark_node;
254 if (! is_aggr_type (basetype, 1))
255 return error_mark_node;
257 if (! IS_AGGR_TYPE (type))
259 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
260 exp, type);
261 return error_mark_node;
264 if (! binfo)
266 binfo = get_binfo (basetype, type, 1);
267 if (binfo == error_mark_node)
268 return error_mark_node;
269 if (! binfo)
270 error_not_base_type (basetype, type);
273 if (binfo)
275 if (TREE_CODE (exp) == INDIRECT_REF)
276 decl = build_indirect_ref
277 (convert_pointer_to_real
278 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL);
279 else
280 decl = build_scoped_ref (exp, basetype);
282 /* Call to a destructor. */
283 if (TREE_CODE (name) == BIT_NOT_EXPR)
285 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
286 return cp_convert (void_type_node, exp);
288 return build_delete (TREE_TYPE (decl), decl,
289 sfk_complete_destructor,
290 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
294 /* Call to a method. */
295 return build_method_call (decl, name, parms, binfo,
296 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
298 return error_mark_node;
301 /* We want the address of a function or method. We avoid creating a
302 pointer-to-member function. */
304 tree
305 build_addr_func (function)
306 tree function;
308 tree type = TREE_TYPE (function);
310 /* We have to do these by hand to avoid real pointer to member
311 functions. */
312 if (TREE_CODE (type) == METHOD_TYPE)
314 tree addr;
316 type = build_pointer_type (type);
318 if (mark_addressable (function) == 0)
319 return error_mark_node;
321 addr = build1 (ADDR_EXPR, type, function);
323 /* Address of a static or external variable or function counts
324 as a constant */
325 if (staticp (function))
326 TREE_CONSTANT (addr) = 1;
328 function = addr;
330 else
331 function = default_conversion (function);
333 return function;
336 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
337 POINTER_TYPE to those. Note, pointer to member function types
338 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
340 tree
341 build_call (function, parms)
342 tree function, parms;
344 int is_constructor = 0;
345 int nothrow;
346 tree tmp;
347 tree decl;
348 tree result_type;
350 function = build_addr_func (function);
352 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
354 sorry ("unable to call pointer to member function here");
355 return error_mark_node;
358 result_type = TREE_TYPE (TREE_TYPE (TREE_TYPE (function)));
360 if (TREE_CODE (function) == ADDR_EXPR
361 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
362 decl = TREE_OPERAND (function, 0);
363 else
364 decl = NULL_TREE;
366 /* We check both the decl and the type; a function may be known not to
367 throw without being declared throw(). */
368 nothrow = ((decl && TREE_NOTHROW (decl))
369 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
371 if (decl && DECL_CONSTRUCTOR_P (decl))
372 is_constructor = 1;
374 if (decl && ! TREE_USED (decl))
376 /* We invoke build_call directly for several library functions.
377 These may have been declared normally if we're building libgcc,
378 so we can't just check DECL_ARTIFICIAL. */
379 if (DECL_ARTIFICIAL (decl)
380 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
381 mark_used (decl);
382 else
383 my_friendly_abort (990125);
386 /* Don't pass empty class objects by value. This is useful
387 for tags in STL, which are used to control overload resolution.
388 We don't need to handle other cases of copying empty classes. */
389 if (! decl || ! DECL_BUILT_IN (decl))
390 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
391 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
392 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
394 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
395 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
396 TREE_VALUE (tmp), t);
399 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
400 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
401 TREE_TYPE (function) = result_type;
402 TREE_SIDE_EFFECTS (function) = 1;
403 TREE_NOTHROW (function) = nothrow;
405 return function;
408 /* Build something of the form ptr->method (args)
409 or object.method (args). This can also build
410 calls to constructors, and find friends.
412 Member functions always take their class variable
413 as a pointer.
415 INSTANCE is a class instance.
417 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
419 PARMS help to figure out what that NAME really refers to.
421 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
422 down to the real instance type to use for access checking. We need this
423 information to get protected accesses correct. This parameter is used
424 by build_member_call.
426 FLAGS is the logical disjunction of zero or more LOOKUP_
427 flags. See cp-tree.h for more info.
429 If this is all OK, calls build_function_call with the resolved
430 member function.
432 This function must also handle being called to perform
433 initialization, promotion/coercion of arguments, and
434 instantiation of default parameters.
436 Note that NAME may refer to an instance variable name. If
437 `operator()()' is defined for the type of that field, then we return
438 that result. */
440 #ifdef GATHER_STATISTICS
441 extern int n_build_method_call;
442 #endif
444 /* FIXME: Should basetype_path just come from NAME which should be a
445 BASELINK? */
447 tree
448 build_method_call (instance, name, parms, basetype_path, flags)
449 tree instance, name, parms, basetype_path;
450 int flags;
452 tree basetype, instance_ptr;
454 #ifdef GATHER_STATISTICS
455 n_build_method_call++;
456 #endif
458 if (instance == error_mark_node
459 || name == error_mark_node
460 || parms == error_mark_node
461 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
462 return error_mark_node;
464 if (processing_template_decl)
465 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
467 if (is_overloaded_fn (name)
468 && DECL_DESTRUCTOR_P (get_first_fn (name)))
470 if (parms)
471 error ("destructors take no parameters");
472 basetype = TREE_TYPE (instance);
473 if (TREE_CODE (basetype) == REFERENCE_TYPE)
474 basetype = TREE_TYPE (basetype);
476 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (complete_type (basetype)))
477 return cp_convert (void_type_node, instance);
478 instance = default_conversion (instance);
479 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
480 return build_delete (build_pointer_type (basetype),
481 instance_ptr, sfk_complete_destructor,
482 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
485 return build_new_method_call (instance, name, parms, basetype_path, flags);
488 /* New overloading code. */
490 struct z_candidate {
491 tree fn;
492 tree convs;
493 tree second_conv;
494 int viable;
495 tree basetype_path;
496 tree template;
497 tree warnings;
498 struct z_candidate *next;
501 #define IDENTITY_RANK 0
502 #define EXACT_RANK 1
503 #define PROMO_RANK 2
504 #define STD_RANK 3
505 #define PBOOL_RANK 4
506 #define USER_RANK 5
507 #define ELLIPSIS_RANK 6
508 #define BAD_RANK 7
510 #define ICS_RANK(NODE) \
511 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
512 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
513 : ICS_USER_FLAG (NODE) ? USER_RANK \
514 : ICS_STD_RANK (NODE))
516 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
518 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
519 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
520 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
521 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
523 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
524 should be created to hold the result of the conversion. */
525 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
527 #define USER_CONV_CAND(NODE) \
528 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
529 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
532 null_ptr_cst_p (t)
533 tree t;
535 /* [conv.ptr]
537 A null pointer constant is an integral constant expression
538 (_expr.const_) rvalue of integer type that evaluates to zero. */
539 if (t == null_node
540 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
541 return 1;
542 return 0;
546 /* Returns non-zero if PARMLIST consists of only default parms and/or
547 ellipsis. */
550 sufficient_parms_p (parmlist)
551 tree parmlist;
553 for (; parmlist && parmlist != void_list_node;
554 parmlist = TREE_CHAIN (parmlist))
555 if (!TREE_PURPOSE (parmlist))
556 return 0;
557 return 1;
560 static tree
561 build_conv (code, type, from)
562 enum tree_code code;
563 tree type, from;
565 tree t;
566 int rank = ICS_STD_RANK (from);
568 /* We can't use buildl1 here because CODE could be USER_CONV, which
569 takes two arguments. In that case, the caller is responsible for
570 filling in the second argument. */
571 t = make_node (code);
572 TREE_TYPE (t) = type;
573 TREE_OPERAND (t, 0) = from;
575 switch (code)
577 case PTR_CONV:
578 case PMEM_CONV:
579 case BASE_CONV:
580 case STD_CONV:
581 if (rank < STD_RANK)
582 rank = STD_RANK;
583 break;
585 case QUAL_CONV:
586 if (rank < EXACT_RANK)
587 rank = EXACT_RANK;
589 default:
590 break;
592 ICS_STD_RANK (t) = rank;
593 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
594 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
595 return t;
598 static tree
599 non_reference (t)
600 tree t;
602 if (TREE_CODE (t) == REFERENCE_TYPE)
603 t = TREE_TYPE (t);
604 return t;
607 tree
608 strip_top_quals (t)
609 tree t;
611 if (TREE_CODE (t) == ARRAY_TYPE)
612 return t;
613 return TYPE_MAIN_VARIANT (t);
616 /* Returns the standard conversion path (see [conv]) from type FROM to type
617 TO, if any. For proper handling of null pointer constants, you must
618 also pass the expression EXPR to convert from. */
620 static tree
621 standard_conversion (to, from, expr)
622 tree to, from, expr;
624 enum tree_code fcode, tcode;
625 tree conv;
626 int fromref = 0;
628 if (TREE_CODE (to) == REFERENCE_TYPE)
629 to = TREE_TYPE (to);
630 if (TREE_CODE (from) == REFERENCE_TYPE)
632 fromref = 1;
633 from = TREE_TYPE (from);
635 to = strip_top_quals (to);
636 from = strip_top_quals (from);
638 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
639 && expr && type_unknown_p (expr))
641 expr = instantiate_type (to, expr, itf_none);
642 if (expr == error_mark_node)
643 return NULL_TREE;
644 from = TREE_TYPE (expr);
647 fcode = TREE_CODE (from);
648 tcode = TREE_CODE (to);
650 conv = build1 (IDENTITY_CONV, from, expr);
652 if (fcode == FUNCTION_TYPE)
654 from = build_pointer_type (from);
655 fcode = TREE_CODE (from);
656 conv = build_conv (LVALUE_CONV, from, conv);
658 else if (fcode == ARRAY_TYPE)
660 from = build_pointer_type (TREE_TYPE (from));
661 fcode = TREE_CODE (from);
662 conv = build_conv (LVALUE_CONV, from, conv);
664 else if (fromref || (expr && lvalue_p (expr)))
665 conv = build_conv (RVALUE_CONV, from, conv);
667 /* Allow conversion between `__complex__' data types */
668 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
670 /* The standard conversion sequence to convert FROM to TO is
671 the standard conversion sequence to perform componentwise
672 conversion. */
673 tree part_conv = standard_conversion
674 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
676 if (part_conv)
678 conv = build_conv (TREE_CODE (part_conv), to, conv);
679 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
681 else
682 conv = NULL_TREE;
684 return conv;
687 if (same_type_p (from, to))
688 return conv;
690 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
691 && expr && null_ptr_cst_p (expr))
693 conv = build_conv (STD_CONV, to, conv);
695 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
697 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
698 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
700 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
701 TREE_TYPE (to)))
703 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
704 && ufcode != FUNCTION_TYPE)
706 from = build_pointer_type
707 (cp_build_qualified_type (void_type_node,
708 CP_TYPE_QUALS (TREE_TYPE (from))));
709 conv = build_conv (PTR_CONV, from, conv);
711 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
713 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
714 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
715 tree binfo = get_binfo (fbase, tbase, 1);
717 if (binfo && !binfo_from_vbase (binfo)
718 && (same_type_ignoring_top_level_qualifiers_p
719 (TREE_TYPE (TREE_TYPE (from)),
720 TREE_TYPE (TREE_TYPE (to)))))
722 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
723 from = build_pointer_type (from);
724 conv = build_conv (PMEM_CONV, from, conv);
727 else if (IS_AGGR_TYPE (TREE_TYPE (from))
728 && IS_AGGR_TYPE (TREE_TYPE (to)))
730 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
732 from =
733 cp_build_qualified_type (TREE_TYPE (to),
734 CP_TYPE_QUALS (TREE_TYPE (from)));
735 from = build_pointer_type (from);
736 conv = build_conv (PTR_CONV, from, conv);
740 if (same_type_p (from, to))
741 /* OK */;
742 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
743 conv = build_conv (QUAL_CONV, to, conv);
744 else if (expr && string_conv_p (to, expr, 0))
745 /* converting from string constant to char *. */
746 conv = build_conv (QUAL_CONV, to, conv);
747 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
749 conv = build_conv (PTR_CONV, to, conv);
750 ICS_BAD_FLAG (conv) = 1;
752 else
753 return 0;
755 from = to;
757 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
759 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
760 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
761 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
762 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
763 tree binfo = get_binfo (fbase, tbase, 1);
765 if (!binfo || binfo_from_vbase (binfo)
766 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
767 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
768 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
769 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
770 return 0;
772 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
773 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
774 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
775 from = build_ptrmemfunc_type (build_pointer_type (from));
776 conv = build_conv (PMEM_CONV, from, conv);
778 else if (tcode == BOOLEAN_TYPE)
780 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
781 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
782 return 0;
784 conv = build_conv (STD_CONV, to, conv);
785 if (fcode == POINTER_TYPE
786 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
787 ICS_STD_RANK (conv) = PBOOL_RANK;
789 /* We don't check for ENUMERAL_TYPE here because there are no standard
790 conversions to enum type. */
791 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
792 || tcode == REAL_TYPE)
794 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
795 return 0;
796 conv = build_conv (STD_CONV, to, conv);
798 /* Give this a better rank if it's a promotion. */
799 if (to == type_promotes_to (from)
800 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
801 ICS_STD_RANK (conv) = PROMO_RANK;
803 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
804 && is_properly_derived_from (from, to))
806 if (TREE_CODE (conv) == RVALUE_CONV)
807 conv = TREE_OPERAND (conv, 0);
808 conv = build_conv (BASE_CONV, to, conv);
809 /* The derived-to-base conversion indicates the initialization
810 of a parameter with base type from an object of a derived
811 type. A temporary object is created to hold the result of
812 the conversion. */
813 NEED_TEMPORARY_P (conv) = 1;
815 else
816 return 0;
818 return conv;
821 /* Returns non-zero if T1 is reference-related to T2. */
823 static int
824 reference_related_p (t1, t2)
825 tree t1;
826 tree t2;
828 t1 = TYPE_MAIN_VARIANT (t1);
829 t2 = TYPE_MAIN_VARIANT (t2);
831 /* [dcl.init.ref]
833 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
834 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
835 of T2. */
836 return (same_type_p (t1, t2)
837 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
838 && DERIVED_FROM_P (t1, t2)));
841 /* Returns non-zero if T1 is reference-compatible with T2. */
843 static int
844 reference_compatible_p (t1, t2)
845 tree t1;
846 tree t2;
848 /* [dcl.init.ref]
850 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
851 reference-related to T2 and cv1 is the same cv-qualification as,
852 or greater cv-qualification than, cv2. */
853 return (reference_related_p (t1, t2)
854 && at_least_as_qualified_p (t1, t2));
857 /* Determine whether or not the EXPR (of class type S) can be
858 converted to T as in [over.match.ref]. */
860 static tree
861 convert_class_to_reference (t, s, expr)
862 tree t;
863 tree s;
864 tree expr;
866 tree conversions;
867 tree arglist;
868 tree conv;
869 struct z_candidate *candidates;
870 struct z_candidate *cand;
872 /* [over.match.ref]
874 Assuming that "cv1 T" is the underlying type of the reference
875 being initialized, and "cv S" is the type of the initializer
876 expression, with S a class type, the candidate functions are
877 selected as follows:
879 --The conversion functions of S and its base classes are
880 considered. Those that are not hidden within S and yield type
881 "reference to cv2 T2", where "cv1 T" is reference-compatible
882 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
884 The argument list has one argument, which is the initializer
885 expression. */
887 candidates = 0;
889 /* Conceptually, we should take the address of EXPR and put it in
890 the argument list. Unfortunately, however, that can result in
891 error messages, which we should not issue now because we are just
892 trying to find a conversion operator. Therefore, we use NULL,
893 cast to the appropriate type. */
894 arglist = build_int_2 (0, 0);
895 TREE_TYPE (arglist) = build_pointer_type (s);
896 arglist = build_tree_list (NULL_TREE, arglist);
898 for (conversions = lookup_conversions (s);
899 conversions;
900 conversions = TREE_CHAIN (conversions))
902 tree fns = TREE_VALUE (conversions);
904 for (; fns; fns = OVL_NEXT (fns))
906 tree f = OVL_CURRENT (fns);
907 tree t2 = TREE_TYPE (TREE_TYPE (f));
908 struct z_candidate *old_candidates = candidates;
910 /* If this is a template function, try to get an exact
911 match. */
912 if (TREE_CODE (f) == TEMPLATE_DECL)
914 candidates
915 = add_template_candidate (candidates,
916 f, s,
917 NULL_TREE,
918 arglist,
919 build_reference_type (t),
920 LOOKUP_NORMAL,
921 DEDUCE_CONV);
923 if (candidates != old_candidates)
925 /* Now, see if the conversion function really returns
926 an lvalue of the appropriate type. From the
927 point of view of unification, simply returning an
928 rvalue of the right type is good enough. */
929 f = candidates->fn;
930 t2 = TREE_TYPE (TREE_TYPE (f));
931 if (TREE_CODE (t2) != REFERENCE_TYPE
932 || !reference_compatible_p (t, TREE_TYPE (t2)))
933 candidates = candidates->next;
936 else if (TREE_CODE (t2) == REFERENCE_TYPE
937 && reference_compatible_p (t, TREE_TYPE (t2)))
938 candidates
939 = add_function_candidate (candidates, f, s, arglist,
940 LOOKUP_NORMAL);
942 if (candidates != old_candidates)
943 candidates->basetype_path = TYPE_BINFO (s);
947 /* If none of the conversion functions worked out, let our caller
948 know. */
949 if (!any_viable (candidates))
950 return NULL_TREE;
952 candidates = splice_viable (candidates);
953 cand = tourney (candidates);
954 if (!cand)
955 return NULL_TREE;
957 conv = build1 (IDENTITY_CONV, s, expr);
958 conv = build_conv (USER_CONV,
959 non_reference (TREE_TYPE (TREE_TYPE (cand->fn))),
960 conv);
961 TREE_OPERAND (conv, 1) = build_ptr_wrapper (cand);
962 ICS_USER_FLAG (conv) = 1;
963 if (cand->viable == -1)
964 ICS_BAD_FLAG (conv) = 1;
965 cand->second_conv = conv;
967 return conv;
970 /* A reference of the indicated TYPE is being bound directly to the
971 expression represented by the implicit conversion sequence CONV.
972 Return a conversion sequence for this binding. */
974 static tree
975 direct_reference_binding (type, conv)
976 tree type;
977 tree conv;
979 tree t = TREE_TYPE (type);
981 /* [over.ics.rank]
983 When a parameter of reference type binds directly
984 (_dcl.init.ref_) to an argument expression, the implicit
985 conversion sequence is the identity conversion, unless the
986 argument expression has a type that is a derived class of the
987 parameter type, in which case the implicit conversion sequence is
988 a derived-to-base Conversion.
990 If the parameter binds directly to the result of applying a
991 conversion function to the argument expression, the implicit
992 conversion sequence is a user-defined conversion sequence
993 (_over.ics.user_), with the second standard conversion sequence
994 either an identity conversion or, if the conversion function
995 returns an entity of a type that is a derived class of the
996 parameter type, a derived-to-base conversion. */
997 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
999 /* Represent the derived-to-base conversion. */
1000 conv = build_conv (BASE_CONV, t, conv);
1001 /* We will actually be binding to the base-class subobject in
1002 the derived class, so we mark this conversion appropriately.
1003 That way, convert_like knows not to generate a temporary. */
1004 NEED_TEMPORARY_P (conv) = 0;
1006 return build_conv (REF_BIND, type, conv);
1009 /* Returns the conversion path from type FROM to reference type TO for
1010 purposes of reference binding. For lvalue binding, either pass a
1011 reference type to FROM or an lvalue expression to EXPR. If the
1012 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1013 the conversion returned. */
1015 static tree
1016 reference_binding (rto, rfrom, expr, flags)
1017 tree rto, rfrom, expr;
1018 int flags;
1020 tree conv = NULL_TREE;
1021 tree to = TREE_TYPE (rto);
1022 tree from = rfrom;
1023 int related_p;
1024 int compatible_p;
1025 cp_lvalue_kind lvalue_p = clk_none;
1027 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1029 expr = instantiate_type (to, expr, itf_none);
1030 if (expr == error_mark_node)
1031 return NULL_TREE;
1032 from = TREE_TYPE (expr);
1035 if (TREE_CODE (from) == REFERENCE_TYPE)
1037 /* Anything with reference type is an lvalue. */
1038 lvalue_p = clk_ordinary;
1039 from = TREE_TYPE (from);
1041 else if (expr)
1042 lvalue_p = real_lvalue_p (expr);
1044 /* Figure out whether or not the types are reference-related and
1045 reference compatible. We have do do this after stripping
1046 references from FROM. */
1047 related_p = reference_related_p (to, from);
1048 compatible_p = reference_compatible_p (to, from);
1050 if (lvalue_p && compatible_p)
1052 /* [dcl.init.ref]
1054 If the intializer expression
1056 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1057 is reference-compatible with "cv2 T2,"
1059 the reference is bound directly to the initializer exprssion
1060 lvalue. */
1061 conv = build1 (IDENTITY_CONV, from, expr);
1062 conv = direct_reference_binding (rto, conv);
1063 if ((lvalue_p & clk_bitfield) != 0
1064 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1065 /* For the purposes of overload resolution, we ignore the fact
1066 this expression is a bitfield. (In particular,
1067 [over.ics.ref] says specifically that a function with a
1068 non-const reference parameter is viable even if the
1069 argument is a bitfield.)
1071 However, when we actually call the function we must create
1072 a temporary to which to bind the reference. If the
1073 reference is volatile, or isn't const, then we cannot make
1074 a temporary, so we just issue an error when the conversion
1075 actually occurs. */
1076 NEED_TEMPORARY_P (conv) = 1;
1077 return conv;
1079 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1081 /* [dcl.init.ref]
1083 If the initializer exprsesion
1085 -- has a class type (i.e., T2 is a class type) can be
1086 implicitly converted to an lvalue of type "cv3 T3," where
1087 "cv1 T1" is reference-compatible with "cv3 T3". (this
1088 conversion is selected by enumerating the applicable
1089 conversion functions (_over.match.ref_) and choosing the
1090 best one through overload resolution. (_over.match_).
1092 the reference is bound to the lvalue result of the conversion
1093 in the second case. */
1094 conv = convert_class_to_reference (to, from, expr);
1095 if (conv)
1096 return direct_reference_binding (rto, conv);
1099 /* From this point on, we conceptually need temporaries, even if we
1100 elide them. Only the cases above are "direct bindings". */
1101 if (flags & LOOKUP_NO_TEMP_BIND)
1102 return NULL_TREE;
1104 /* [over.ics.rank]
1106 When a parameter of reference type is not bound directly to an
1107 argument expression, the conversion sequence is the one required
1108 to convert the argument expression to the underlying type of the
1109 reference according to _over.best.ics_. Conceptually, this
1110 conversion sequence corresponds to copy-initializing a temporary
1111 of the underlying type with the argument expression. Any
1112 difference in top-level cv-qualification is subsumed by the
1113 initialization itself and does not constitute a conversion. */
1115 /* [dcl.init.ref]
1117 Otherwise, the reference shall be to a non-volatile const type. */
1118 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1119 return NULL_TREE;
1121 /* [dcl.init.ref]
1123 If the initializer expression is an rvalue, with T2 a class type,
1124 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1125 is bound in one of the following ways:
1127 -- The reference is bound to the object represented by the rvalue
1128 or to a sub-object within that object.
1130 In this case, the implicit conversion sequence is supposed to be
1131 same as we would obtain by generating a temporary. Fortunately,
1132 if the types are reference compatible, then this is either an
1133 identity conversion or the derived-to-base conversion, just as
1134 for direct binding. */
1135 if (CLASS_TYPE_P (from) && compatible_p)
1137 conv = build1 (IDENTITY_CONV, from, expr);
1138 return direct_reference_binding (rto, conv);
1141 /* [dcl.init.ref]
1143 Otherwise, a temporary of type "cv1 T1" is created and
1144 initialized from the initializer expression using the rules for a
1145 non-reference copy initialization. If T1 is reference-related to
1146 T2, cv1 must be the same cv-qualification as, or greater
1147 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1148 if (related_p && !at_least_as_qualified_p (to, from))
1149 return NULL_TREE;
1151 conv = implicit_conversion (to, from, expr, flags);
1152 if (!conv)
1153 return NULL_TREE;
1155 conv = build_conv (REF_BIND, rto, conv);
1156 /* This reference binding, unlike those above, requires the
1157 creation of a temporary. */
1158 NEED_TEMPORARY_P (conv) = 1;
1160 return conv;
1163 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1164 to type TO. The optional expression EXPR may affect the conversion.
1165 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1166 significant. */
1168 static tree
1169 implicit_conversion (to, from, expr, flags)
1170 tree to, from, expr;
1171 int flags;
1173 tree conv;
1174 struct z_candidate *cand;
1176 /* Resolve expressions like `A::p' that we thought might become
1177 pointers-to-members. */
1178 if (expr && TREE_CODE (expr) == OFFSET_REF)
1180 expr = resolve_offset_ref (expr);
1181 from = TREE_TYPE (expr);
1184 if (from == error_mark_node || to == error_mark_node
1185 || expr == error_mark_node)
1186 return NULL_TREE;
1188 /* Make sure both the FROM and TO types are complete so that
1189 user-defined conversions are available. */
1190 complete_type (from);
1191 complete_type (to);
1193 if (TREE_CODE (to) == REFERENCE_TYPE)
1194 conv = reference_binding (to, from, expr, flags);
1195 else
1196 conv = standard_conversion (to, from, expr);
1198 if (conv)
1200 else if (expr != NULL_TREE
1201 && (IS_AGGR_TYPE (from)
1202 || IS_AGGR_TYPE (to))
1203 && (flags & LOOKUP_NO_CONVERSION) == 0)
1205 cand = build_user_type_conversion_1
1206 (to, expr, LOOKUP_ONLYCONVERTING);
1207 if (cand)
1208 conv = cand->second_conv;
1210 /* We used to try to bind a reference to a temporary here, but that
1211 is now handled by the recursive call to this function at the end
1212 of reference_binding. */
1215 return conv;
1218 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1219 functions. */
1221 static struct z_candidate *
1222 add_candidate (candidates, fn, convs, viable)
1223 struct z_candidate *candidates;
1224 tree fn, convs;
1225 int viable;
1227 struct z_candidate *cand
1228 = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1230 cand->fn = fn;
1231 cand->convs = convs;
1232 cand->viable = viable;
1233 cand->next = candidates;
1235 return cand;
1238 /* Create an overload candidate for the function or method FN called with
1239 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1240 to implicit_conversion.
1242 CTYPE, if non-NULL, is the type we want to pretend this function
1243 comes from for purposes of overload resolution. */
1245 static struct z_candidate *
1246 add_function_candidate (candidates, fn, ctype, arglist, flags)
1247 struct z_candidate *candidates;
1248 tree fn, ctype, arglist;
1249 int flags;
1251 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1252 int i, len;
1253 tree convs;
1254 tree parmnode, argnode;
1255 int viable = 1;
1257 /* The `this', `in_chrg' and VTT arguments to constructors are not
1258 considered in overload resolution. */
1259 if (DECL_CONSTRUCTOR_P (fn))
1261 parmlist = skip_artificial_parms_for (fn, parmlist);
1262 arglist = skip_artificial_parms_for (fn, arglist);
1265 len = list_length (arglist);
1266 convs = make_tree_vec (len);
1268 /* 13.3.2 - Viable functions [over.match.viable]
1269 First, to be a viable function, a candidate function shall have enough
1270 parameters to agree in number with the arguments in the list.
1272 We need to check this first; otherwise, checking the ICSes might cause
1273 us to produce an ill-formed template instantiation. */
1275 parmnode = parmlist;
1276 for (i = 0; i < len; ++i)
1278 if (parmnode == NULL_TREE || parmnode == void_list_node)
1279 break;
1280 parmnode = TREE_CHAIN (parmnode);
1283 if (i < len && parmnode)
1284 viable = 0;
1286 /* Make sure there are default args for the rest of the parms. */
1287 else if (!sufficient_parms_p (parmnode))
1288 viable = 0;
1290 if (! viable)
1291 goto out;
1293 /* Second, for F to be a viable function, there shall exist for each
1294 argument an implicit conversion sequence that converts that argument
1295 to the corresponding parameter of F. */
1297 parmnode = parmlist;
1298 argnode = arglist;
1300 for (i = 0; i < len; ++i)
1302 tree arg = TREE_VALUE (argnode);
1303 tree argtype = lvalue_type (arg);
1304 tree t;
1305 int is_this;
1307 if (parmnode == void_list_node)
1308 break;
1310 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1311 && ! DECL_CONSTRUCTOR_P (fn));
1313 if (parmnode)
1315 tree parmtype = TREE_VALUE (parmnode);
1317 /* The type of the implicit object parameter ('this') for
1318 overload resolution is not always the same as for the
1319 function itself; conversion functions are considered to
1320 be members of the class being converted, and functions
1321 introduced by a using-declaration are considered to be
1322 members of the class that uses them.
1324 Since build_over_call ignores the ICS for the `this'
1325 parameter, we can just change the parm type. */
1326 if (ctype && is_this)
1328 parmtype
1329 = build_qualified_type (ctype,
1330 TYPE_QUALS (TREE_TYPE (parmtype)));
1331 parmtype = build_pointer_type (parmtype);
1334 t = implicit_conversion (parmtype, argtype, arg, flags);
1336 else
1338 t = build1 (IDENTITY_CONV, argtype, arg);
1339 ICS_ELLIPSIS_FLAG (t) = 1;
1342 if (t && is_this)
1343 ICS_THIS_FLAG (t) = 1;
1345 TREE_VEC_ELT (convs, i) = t;
1346 if (! t)
1348 viable = 0;
1349 break;
1352 if (ICS_BAD_FLAG (t))
1353 viable = -1;
1355 if (parmnode)
1356 parmnode = TREE_CHAIN (parmnode);
1357 argnode = TREE_CHAIN (argnode);
1360 out:
1361 return add_candidate (candidates, fn, convs, viable);
1364 /* Create an overload candidate for the conversion function FN which will
1365 be invoked for expression OBJ, producing a pointer-to-function which
1366 will in turn be called with the argument list ARGLIST, and add it to
1367 CANDIDATES. FLAGS is passed on to implicit_conversion.
1369 Actually, we don't really care about FN; we care about the type it
1370 converts to. There may be multiple conversion functions that will
1371 convert to that type, and we rely on build_user_type_conversion_1 to
1372 choose the best one; so when we create our candidate, we record the type
1373 instead of the function. */
1375 static struct z_candidate *
1376 add_conv_candidate (candidates, fn, obj, arglist)
1377 struct z_candidate *candidates;
1378 tree fn, obj, arglist;
1380 tree totype = TREE_TYPE (TREE_TYPE (fn));
1381 int i, len, viable, flags;
1382 tree parmlist, convs, parmnode, argnode;
1384 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1385 parmlist = TREE_TYPE (parmlist);
1386 parmlist = TYPE_ARG_TYPES (parmlist);
1388 len = list_length (arglist) + 1;
1389 convs = make_tree_vec (len);
1390 parmnode = parmlist;
1391 argnode = arglist;
1392 viable = 1;
1393 flags = LOOKUP_NORMAL;
1395 /* Don't bother looking up the same type twice. */
1396 if (candidates && candidates->fn == totype)
1397 return candidates;
1399 for (i = 0; i < len; ++i)
1401 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1402 tree argtype = lvalue_type (arg);
1403 tree t;
1405 if (i == 0)
1406 t = implicit_conversion (totype, argtype, arg, flags);
1407 else if (parmnode == void_list_node)
1408 break;
1409 else if (parmnode)
1410 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1411 else
1413 t = build1 (IDENTITY_CONV, argtype, arg);
1414 ICS_ELLIPSIS_FLAG (t) = 1;
1417 TREE_VEC_ELT (convs, i) = t;
1418 if (! t)
1419 break;
1421 if (ICS_BAD_FLAG (t))
1422 viable = -1;
1424 if (i == 0)
1425 continue;
1427 if (parmnode)
1428 parmnode = TREE_CHAIN (parmnode);
1429 argnode = TREE_CHAIN (argnode);
1432 if (i < len)
1433 viable = 0;
1435 if (!sufficient_parms_p (parmnode))
1436 viable = 0;
1438 return add_candidate (candidates, totype, convs, viable);
1441 static struct z_candidate *
1442 build_builtin_candidate (candidates, fnname, type1, type2,
1443 args, argtypes, flags)
1444 struct z_candidate *candidates;
1445 tree fnname, type1, type2, *args, *argtypes;
1446 int flags;
1449 tree t, convs;
1450 int viable = 1, i;
1451 tree types[2];
1453 types[0] = type1;
1454 types[1] = type2;
1456 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1458 for (i = 0; i < 2; ++i)
1460 if (! args[i])
1461 break;
1463 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1464 if (! t)
1466 viable = 0;
1467 /* We need something for printing the candidate. */
1468 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1470 else if (ICS_BAD_FLAG (t))
1471 viable = 0;
1472 TREE_VEC_ELT (convs, i) = t;
1475 /* For COND_EXPR we rearranged the arguments; undo that now. */
1476 if (args[2])
1478 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1479 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1480 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1481 if (t)
1482 TREE_VEC_ELT (convs, 0) = t;
1483 else
1484 viable = 0;
1487 return add_candidate (candidates, fnname, convs, viable);
1490 static int
1491 is_complete (t)
1492 tree t;
1494 return COMPLETE_TYPE_P (complete_type (t));
1497 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1499 static int
1500 promoted_arithmetic_type_p (type)
1501 tree type;
1503 /* [over.built]
1505 In this section, the term promoted integral type is used to refer
1506 to those integral types which are preserved by integral promotion
1507 (including e.g. int and long but excluding e.g. char).
1508 Similarly, the term promoted arithmetic type refers to promoted
1509 integral types plus floating types. */
1510 return ((INTEGRAL_TYPE_P (type)
1511 && same_type_p (type_promotes_to (type), type))
1512 || TREE_CODE (type) == REAL_TYPE);
1515 /* Create any builtin operator overload candidates for the operator in
1516 question given the converted operand types TYPE1 and TYPE2. The other
1517 args are passed through from add_builtin_candidates to
1518 build_builtin_candidate.
1520 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1521 If CODE is requires candidates operands of the same type of the kind
1522 of which TYPE1 and TYPE2 are, we add both candidates
1523 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1525 static struct z_candidate *
1526 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1527 args, argtypes, flags)
1528 struct z_candidate *candidates;
1529 enum tree_code code, code2;
1530 tree fnname, type1, type2, *args, *argtypes;
1531 int flags;
1533 switch (code)
1535 case POSTINCREMENT_EXPR:
1536 case POSTDECREMENT_EXPR:
1537 args[1] = integer_zero_node;
1538 type2 = integer_type_node;
1539 break;
1540 default:
1541 break;
1544 switch (code)
1547 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1548 and VQ is either volatile or empty, there exist candidate operator
1549 functions of the form
1550 VQ T& operator++(VQ T&);
1551 T operator++(VQ T&, int);
1552 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1553 type other than bool, and VQ is either volatile or empty, there exist
1554 candidate operator functions of the form
1555 VQ T& operator--(VQ T&);
1556 T operator--(VQ T&, int);
1557 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1558 complete object type, and VQ is either volatile or empty, there exist
1559 candidate operator functions of the form
1560 T*VQ& operator++(T*VQ&);
1561 T*VQ& operator--(T*VQ&);
1562 T* operator++(T*VQ&, int);
1563 T* operator--(T*VQ&, int); */
1565 case POSTDECREMENT_EXPR:
1566 case PREDECREMENT_EXPR:
1567 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1568 return candidates;
1569 case POSTINCREMENT_EXPR:
1570 case PREINCREMENT_EXPR:
1571 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1573 type1 = build_reference_type (type1);
1574 break;
1576 return candidates;
1578 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1579 exist candidate operator functions of the form
1581 T& operator*(T*);
1583 8 For every function type T, there exist candidate operator functions of
1584 the form
1585 T& operator*(T*); */
1587 case INDIRECT_REF:
1588 if (TREE_CODE (type1) == POINTER_TYPE
1589 && (TYPE_PTROB_P (type1)
1590 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1591 break;
1592 return candidates;
1594 /* 9 For every type T, there exist candidate operator functions of the form
1595 T* operator+(T*);
1597 10For every promoted arithmetic type T, there exist candidate operator
1598 functions of the form
1599 T operator+(T);
1600 T operator-(T); */
1602 case CONVERT_EXPR: /* unary + */
1603 if (TREE_CODE (type1) == POINTER_TYPE
1604 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1605 break;
1606 case NEGATE_EXPR:
1607 if (ARITHMETIC_TYPE_P (type1))
1608 break;
1609 return candidates;
1611 /* 11For every promoted integral type T, there exist candidate operator
1612 functions of the form
1613 T operator~(T); */
1615 case BIT_NOT_EXPR:
1616 if (INTEGRAL_TYPE_P (type1))
1617 break;
1618 return candidates;
1620 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1621 is the same type as C2 or is a derived class of C2, T is a complete
1622 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1623 there exist candidate operator functions of the form
1624 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1625 where CV12 is the union of CV1 and CV2. */
1627 case MEMBER_REF:
1628 if (TREE_CODE (type1) == POINTER_TYPE
1629 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1631 tree c1 = TREE_TYPE (type1);
1632 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1633 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1634 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1636 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1637 && (TYPE_PTRMEMFUNC_P (type2)
1638 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1639 break;
1641 return candidates;
1643 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1644 didate operator functions of the form
1645 LR operator*(L, R);
1646 LR operator/(L, R);
1647 LR operator+(L, R);
1648 LR operator-(L, R);
1649 bool operator<(L, R);
1650 bool operator>(L, R);
1651 bool operator<=(L, R);
1652 bool operator>=(L, R);
1653 bool operator==(L, R);
1654 bool operator!=(L, R);
1655 where LR is the result of the usual arithmetic conversions between
1656 types L and R.
1658 14For every pair of types T and I, where T is a cv-qualified or cv-
1659 unqualified complete object type and I is a promoted integral type,
1660 there exist candidate operator functions of the form
1661 T* operator+(T*, I);
1662 T& operator[](T*, I);
1663 T* operator-(T*, I);
1664 T* operator+(I, T*);
1665 T& operator[](I, T*);
1667 15For every T, where T is a pointer to complete object type, there exist
1668 candidate operator functions of the form112)
1669 ptrdiff_t operator-(T, T);
1671 16For every pointer or enumeration type T, there exist candidate operator
1672 functions of the form
1673 bool operator<(T, T);
1674 bool operator>(T, T);
1675 bool operator<=(T, T);
1676 bool operator>=(T, T);
1677 bool operator==(T, T);
1678 bool operator!=(T, T);
1680 17For every pointer to member type T, there exist candidate operator
1681 functions of the form
1682 bool operator==(T, T);
1683 bool operator!=(T, T); */
1685 case MINUS_EXPR:
1686 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1687 break;
1688 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1690 type2 = ptrdiff_type_node;
1691 break;
1693 case MULT_EXPR:
1694 case TRUNC_DIV_EXPR:
1695 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1696 break;
1697 return candidates;
1699 case EQ_EXPR:
1700 case NE_EXPR:
1701 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1702 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1703 break;
1704 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1705 && null_ptr_cst_p (args[1]))
1707 type2 = type1;
1708 break;
1710 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1711 && null_ptr_cst_p (args[0]))
1713 type1 = type2;
1714 break;
1716 /* FALLTHROUGH */
1717 case LT_EXPR:
1718 case GT_EXPR:
1719 case LE_EXPR:
1720 case GE_EXPR:
1721 case MAX_EXPR:
1722 case MIN_EXPR:
1723 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1724 break;
1725 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1726 break;
1727 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1728 break;
1729 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1731 type2 = type1;
1732 break;
1734 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1736 type1 = type2;
1737 break;
1739 return candidates;
1741 case PLUS_EXPR:
1742 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1743 break;
1744 case ARRAY_REF:
1745 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1747 type1 = ptrdiff_type_node;
1748 break;
1750 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1752 type2 = ptrdiff_type_node;
1753 break;
1755 return candidates;
1757 /* 18For every pair of promoted integral types L and R, there exist candi-
1758 date operator functions of the form
1759 LR operator%(L, R);
1760 LR operator&(L, R);
1761 LR operator^(L, R);
1762 LR operator|(L, R);
1763 L operator<<(L, R);
1764 L operator>>(L, R);
1765 where LR is the result of the usual arithmetic conversions between
1766 types L and R. */
1768 case TRUNC_MOD_EXPR:
1769 case BIT_AND_EXPR:
1770 case BIT_IOR_EXPR:
1771 case BIT_XOR_EXPR:
1772 case LSHIFT_EXPR:
1773 case RSHIFT_EXPR:
1774 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1775 break;
1776 return candidates;
1778 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1779 type, VQ is either volatile or empty, and R is a promoted arithmetic
1780 type, there exist candidate operator functions of the form
1781 VQ L& operator=(VQ L&, R);
1782 VQ L& operator*=(VQ L&, R);
1783 VQ L& operator/=(VQ L&, R);
1784 VQ L& operator+=(VQ L&, R);
1785 VQ L& operator-=(VQ L&, R);
1787 20For every pair T, VQ), where T is any type and VQ is either volatile
1788 or empty, there exist candidate operator functions of the form
1789 T*VQ& operator=(T*VQ&, T*);
1791 21For every pair T, VQ), where T is a pointer to member type and VQ is
1792 either volatile or empty, there exist candidate operator functions of
1793 the form
1794 VQ T& operator=(VQ T&, T);
1796 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1797 unqualified complete object type, VQ is either volatile or empty, and
1798 I is a promoted integral type, there exist candidate operator func-
1799 tions of the form
1800 T*VQ& operator+=(T*VQ&, I);
1801 T*VQ& operator-=(T*VQ&, I);
1803 23For every triple L, VQ, R), where L is an integral or enumeration
1804 type, VQ is either volatile or empty, and R is a promoted integral
1805 type, there exist candidate operator functions of the form
1807 VQ L& operator%=(VQ L&, R);
1808 VQ L& operator<<=(VQ L&, R);
1809 VQ L& operator>>=(VQ L&, R);
1810 VQ L& operator&=(VQ L&, R);
1811 VQ L& operator^=(VQ L&, R);
1812 VQ L& operator|=(VQ L&, R); */
1814 case MODIFY_EXPR:
1815 switch (code2)
1817 case PLUS_EXPR:
1818 case MINUS_EXPR:
1819 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1821 type2 = ptrdiff_type_node;
1822 break;
1824 case MULT_EXPR:
1825 case TRUNC_DIV_EXPR:
1826 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1827 break;
1828 return candidates;
1830 case TRUNC_MOD_EXPR:
1831 case BIT_AND_EXPR:
1832 case BIT_IOR_EXPR:
1833 case BIT_XOR_EXPR:
1834 case LSHIFT_EXPR:
1835 case RSHIFT_EXPR:
1836 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1837 break;
1838 return candidates;
1840 case NOP_EXPR:
1841 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1842 break;
1843 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1844 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1845 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1846 || ((TYPE_PTRMEMFUNC_P (type1)
1847 || TREE_CODE (type1) == POINTER_TYPE)
1848 && null_ptr_cst_p (args[1])))
1850 type2 = type1;
1851 break;
1853 return candidates;
1855 default:
1856 my_friendly_abort (367);
1858 type1 = build_reference_type (type1);
1859 break;
1861 case COND_EXPR:
1862 /* [over.builtin]
1864 For every pair of promoted arithmetic types L and R, there
1865 exist candidate operator functions of the form
1867 LR operator?(bool, L, R);
1869 where LR is the result of the usual arithmetic conversions
1870 between types L and R.
1872 For every type T, where T is a pointer or pointer-to-member
1873 type, there exist candidate operator functions of the form T
1874 operator?(bool, T, T); */
1876 if (promoted_arithmetic_type_p (type1)
1877 && promoted_arithmetic_type_p (type2))
1878 /* That's OK. */
1879 break;
1881 /* Otherwise, the types should be pointers. */
1882 if (!(TREE_CODE (type1) == POINTER_TYPE
1883 || TYPE_PTRMEM_P (type1)
1884 || TYPE_PTRMEMFUNC_P (type1))
1885 || !(TREE_CODE (type2) == POINTER_TYPE
1886 || TYPE_PTRMEM_P (type2)
1887 || TYPE_PTRMEMFUNC_P (type2)))
1888 return candidates;
1890 /* We don't check that the two types are the same; the logic
1891 below will actually create two candidates; one in which both
1892 parameter types are TYPE1, and one in which both parameter
1893 types are TYPE2. */
1894 break;
1896 /* These arguments do not make for a legal overloaded operator. */
1897 return candidates;
1899 default:
1900 my_friendly_abort (367);
1903 /* If we're dealing with two pointer types or two enumeral types,
1904 we need candidates for both of them. */
1905 if (type2 && !same_type_p (type1, type2)
1906 && TREE_CODE (type1) == TREE_CODE (type2)
1907 && (TREE_CODE (type1) == REFERENCE_TYPE
1908 || (TREE_CODE (type1) == POINTER_TYPE
1909 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1910 || TYPE_PTRMEMFUNC_P (type1)
1911 || IS_AGGR_TYPE (type1)
1912 || TREE_CODE (type1) == ENUMERAL_TYPE))
1914 candidates = build_builtin_candidate
1915 (candidates, fnname, type1, type1, args, argtypes, flags);
1916 return build_builtin_candidate
1917 (candidates, fnname, type2, type2, args, argtypes, flags);
1920 return build_builtin_candidate
1921 (candidates, fnname, type1, type2, args, argtypes, flags);
1924 tree
1925 type_decays_to (type)
1926 tree type;
1928 if (TREE_CODE (type) == ARRAY_TYPE)
1929 return build_pointer_type (TREE_TYPE (type));
1930 if (TREE_CODE (type) == FUNCTION_TYPE)
1931 return build_pointer_type (type);
1932 return type;
1935 /* There are three conditions of builtin candidates:
1937 1) bool-taking candidates. These are the same regardless of the input.
1938 2) pointer-pair taking candidates. These are generated for each type
1939 one of the input types converts to.
1940 3) arithmetic candidates. According to the standard, we should generate
1941 all of these, but I'm trying not to...
1943 Here we generate a superset of the possible candidates for this particular
1944 case. That is a subset of the full set the standard defines, plus some
1945 other cases which the standard disallows. add_builtin_candidate will
1946 filter out the illegal set. */
1948 static struct z_candidate *
1949 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1950 struct z_candidate *candidates;
1951 enum tree_code code, code2;
1952 tree fnname, *args;
1953 int flags;
1955 int ref1, i;
1956 int enum_p = 0;
1957 tree type, argtypes[3];
1958 /* TYPES[i] is the set of possible builtin-operator parameter types
1959 we will consider for the Ith argument. These are represented as
1960 a TREE_LIST; the TREE_VALUE of each node is the potential
1961 parameter type. */
1962 tree types[2];
1964 for (i = 0; i < 3; ++i)
1966 if (args[i])
1967 argtypes[i] = lvalue_type (args[i]);
1968 else
1969 argtypes[i] = NULL_TREE;
1972 switch (code)
1974 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1975 and VQ is either volatile or empty, there exist candidate operator
1976 functions of the form
1977 VQ T& operator++(VQ T&); */
1979 case POSTINCREMENT_EXPR:
1980 case PREINCREMENT_EXPR:
1981 case POSTDECREMENT_EXPR:
1982 case PREDECREMENT_EXPR:
1983 case MODIFY_EXPR:
1984 ref1 = 1;
1985 break;
1987 /* 24There also exist candidate operator functions of the form
1988 bool operator!(bool);
1989 bool operator&&(bool, bool);
1990 bool operator||(bool, bool); */
1992 case TRUTH_NOT_EXPR:
1993 return build_builtin_candidate
1994 (candidates, fnname, boolean_type_node,
1995 NULL_TREE, args, argtypes, flags);
1997 case TRUTH_ORIF_EXPR:
1998 case TRUTH_ANDIF_EXPR:
1999 return build_builtin_candidate
2000 (candidates, fnname, boolean_type_node,
2001 boolean_type_node, args, argtypes, flags);
2003 case ADDR_EXPR:
2004 case COMPOUND_EXPR:
2005 case COMPONENT_REF:
2006 return candidates;
2008 case COND_EXPR:
2009 case EQ_EXPR:
2010 case NE_EXPR:
2011 case LT_EXPR:
2012 case LE_EXPR:
2013 case GT_EXPR:
2014 case GE_EXPR:
2015 enum_p = 1;
2016 /* FALLTHROUGH */
2018 default:
2019 ref1 = 0;
2022 types[0] = types[1] = NULL_TREE;
2024 for (i = 0; i < 2; ++i)
2026 if (! args[i])
2028 else if (IS_AGGR_TYPE (argtypes[i]))
2030 tree convs;
2032 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2033 return candidates;
2035 convs = lookup_conversions (argtypes[i]);
2037 if (code == COND_EXPR)
2039 if (real_lvalue_p (args[i]))
2040 types[i] = tree_cons
2041 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2043 types[i] = tree_cons
2044 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2047 else if (! convs)
2048 return candidates;
2050 for (; convs; convs = TREE_CHAIN (convs))
2052 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2054 if (i == 0 && ref1
2055 && (TREE_CODE (type) != REFERENCE_TYPE
2056 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2057 continue;
2059 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2060 types[i] = tree_cons (NULL_TREE, type, types[i]);
2062 type = non_reference (type);
2063 if (i != 0 || ! ref1)
2065 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2066 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2067 types[i] = tree_cons (NULL_TREE, type, types[i]);
2068 if (INTEGRAL_TYPE_P (type))
2069 type = type_promotes_to (type);
2072 if (! value_member (type, types[i]))
2073 types[i] = tree_cons (NULL_TREE, type, types[i]);
2076 else
2078 if (code == COND_EXPR && real_lvalue_p (args[i]))
2079 types[i] = tree_cons
2080 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2081 type = non_reference (argtypes[i]);
2082 if (i != 0 || ! ref1)
2084 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2085 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2086 types[i] = tree_cons (NULL_TREE, type, types[i]);
2087 if (INTEGRAL_TYPE_P (type))
2088 type = type_promotes_to (type);
2090 types[i] = tree_cons (NULL_TREE, type, types[i]);
2094 /* Run through the possible parameter types of both arguments,
2095 creating candidates with those parameter types. */
2096 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2098 if (types[1])
2099 for (type = types[1]; type; type = TREE_CHAIN (type))
2100 candidates = add_builtin_candidate
2101 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2102 TREE_VALUE (type), args, argtypes, flags);
2103 else
2104 candidates = add_builtin_candidate
2105 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2106 NULL_TREE, args, argtypes, flags);
2109 return candidates;
2113 /* If TMPL can be successfully instantiated as indicated by
2114 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2116 TMPL is the template. EXPLICIT_TARGS are any explicit template
2117 arguments. ARGLIST is the arguments provided at the call-site.
2118 The RETURN_TYPE is the desired type for conversion operators. If
2119 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2120 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2121 add_conv_candidate. */
2123 static struct z_candidate*
2124 add_template_candidate_real (candidates, tmpl, ctype, explicit_targs,
2125 arglist, return_type, flags,
2126 obj, strict)
2127 struct z_candidate *candidates;
2128 tree tmpl, ctype, explicit_targs, arglist, return_type;
2129 int flags;
2130 tree obj;
2131 unification_kind_t strict;
2133 int ntparms = DECL_NTPARMS (tmpl);
2134 tree targs = make_tree_vec (ntparms);
2135 tree args_without_in_chrg = arglist;
2136 struct z_candidate *cand;
2137 int i;
2138 tree fn;
2140 /* We don't do deduction on the in-charge parameter, the VTT
2141 parameter or 'this'. */
2142 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2143 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2145 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2146 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2147 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2148 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2150 i = fn_type_unification (tmpl, explicit_targs, targs,
2151 args_without_in_chrg,
2152 return_type, strict, -1);
2154 if (i != 0)
2155 return candidates;
2157 fn = instantiate_template (tmpl, targs);
2158 if (fn == error_mark_node)
2159 return candidates;
2161 if (obj != NULL_TREE)
2162 /* Aha, this is a conversion function. */
2163 cand = add_conv_candidate (candidates, fn, obj, arglist);
2164 else
2165 cand = add_function_candidate (candidates, fn, ctype,
2166 arglist, flags);
2167 if (DECL_TI_TEMPLATE (fn) != tmpl)
2168 /* This situation can occur if a member template of a template
2169 class is specialized. Then, instantiate_template might return
2170 an instantiation of the specialization, in which case the
2171 DECL_TI_TEMPLATE field will point at the original
2172 specialization. For example:
2174 template <class T> struct S { template <class U> void f(U);
2175 template <> void f(int) {}; };
2176 S<double> sd;
2177 sd.f(3);
2179 Here, TMPL will be template <class U> S<double>::f(U).
2180 And, instantiate template will give us the specialization
2181 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2182 for this will point at template <class T> template <> S<T>::f(int),
2183 so that we can find the definition. For the purposes of
2184 overload resolution, however, we want the original TMPL. */
2185 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2186 else
2187 cand->template = DECL_TEMPLATE_INFO (fn);
2189 return cand;
2193 static struct z_candidate *
2194 add_template_candidate (candidates, tmpl, ctype, explicit_targs,
2195 arglist, return_type, flags, strict)
2196 struct z_candidate *candidates;
2197 tree tmpl, ctype, explicit_targs, arglist, return_type;
2198 int flags;
2199 unification_kind_t strict;
2201 return
2202 add_template_candidate_real (candidates, tmpl, ctype,
2203 explicit_targs, arglist, return_type, flags,
2204 NULL_TREE, strict);
2208 static struct z_candidate *
2209 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2210 struct z_candidate *candidates;
2211 tree tmpl, obj, arglist, return_type;
2213 return
2214 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2215 arglist, return_type, 0, obj, DEDUCE_CONV);
2219 static int
2220 any_viable (cands)
2221 struct z_candidate *cands;
2223 for (; cands; cands = cands->next)
2224 if (pedantic ? cands->viable == 1 : cands->viable)
2225 return 1;
2226 return 0;
2229 static struct z_candidate *
2230 splice_viable (cands)
2231 struct z_candidate *cands;
2233 struct z_candidate **p = &cands;
2235 for (; *p; )
2237 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2238 p = &((*p)->next);
2239 else
2240 *p = (*p)->next;
2243 return cands;
2246 static tree
2247 build_this (obj)
2248 tree obj;
2250 /* Fix this to work on non-lvalues. */
2251 return build_unary_op (ADDR_EXPR, obj, 0);
2254 static void
2255 print_z_candidates (candidates)
2256 struct z_candidate *candidates;
2258 const char *str = "candidates are:";
2259 for (; candidates; candidates = candidates->next)
2261 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2263 if (TREE_VEC_LENGTH (candidates->convs) == 3)
2264 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2265 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2266 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2267 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2268 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2269 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2270 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2271 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2272 else
2273 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2274 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2276 else if (TYPE_P (candidates->fn))
2277 cp_error ("%s %T <conversion>", str, candidates->fn);
2278 else
2279 cp_error_at ("%s %+#D%s", str, candidates->fn,
2280 candidates->viable == -1 ? " <near match>" : "");
2281 str = " ";
2285 /* Returns the best overload candidate to perform the requested
2286 conversion. This function is used for three the overloading situations
2287 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2288 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2289 per [dcl.init.ref], so we ignore temporary bindings. */
2291 static struct z_candidate *
2292 build_user_type_conversion_1 (totype, expr, flags)
2293 tree totype, expr;
2294 int flags;
2296 struct z_candidate *candidates, *cand;
2297 tree fromtype = TREE_TYPE (expr);
2298 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2299 tree args = NULL_TREE;
2300 tree templates = NULL_TREE;
2302 /* We represent conversion within a hierarchy using RVALUE_CONV and
2303 BASE_CONV, as specified by [over.best.ics]; these become plain
2304 constructor calls, as specified in [dcl.init]. */
2305 if (IS_AGGR_TYPE (fromtype) && IS_AGGR_TYPE (totype)
2306 && DERIVED_FROM_P (totype, fromtype))
2307 abort ();
2309 if (IS_AGGR_TYPE (totype))
2310 ctors = lookup_fnfields (TYPE_BINFO (totype),
2311 complete_ctor_identifier,
2314 if (IS_AGGR_TYPE (fromtype))
2315 convs = lookup_conversions (fromtype);
2317 candidates = 0;
2318 flags |= LOOKUP_NO_CONVERSION;
2320 if (ctors)
2322 tree t;
2324 ctors = BASELINK_FUNCTIONS (ctors);
2326 t = build_int_2 (0, 0);
2327 TREE_TYPE (t) = build_pointer_type (totype);
2328 args = build_tree_list (NULL_TREE, expr);
2329 if (DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2330 || DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)))
2331 /* We should never try to call the abstract or base constructor
2332 from here. */
2333 abort ();
2334 args = tree_cons (NULL_TREE, t, args);
2336 for (; ctors; ctors = OVL_NEXT (ctors))
2338 tree ctor = OVL_CURRENT (ctors);
2339 if (DECL_NONCONVERTING_P (ctor))
2340 continue;
2342 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2344 templates = tree_cons (NULL_TREE, ctor, templates);
2345 candidates =
2346 add_template_candidate (candidates, ctor, totype,
2347 NULL_TREE, args, NULL_TREE, flags,
2348 DEDUCE_CALL);
2350 else
2351 candidates = add_function_candidate (candidates, ctor, totype,
2352 args, flags);
2354 if (candidates)
2356 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2357 candidates->basetype_path = TYPE_BINFO (totype);
2361 if (convs)
2362 args = build_tree_list (NULL_TREE, build_this (expr));
2364 for (; convs; convs = TREE_CHAIN (convs))
2366 tree fns = TREE_VALUE (convs);
2367 int convflags = LOOKUP_NO_CONVERSION;
2368 tree ics;
2370 /* If we are called to convert to a reference type, we are trying to
2371 find an lvalue binding, so don't even consider temporaries. If
2372 we don't find an lvalue binding, the caller will try again to
2373 look for a temporary binding. */
2374 if (TREE_CODE (totype) == REFERENCE_TYPE)
2375 convflags |= LOOKUP_NO_TEMP_BIND;
2377 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2378 ics = implicit_conversion
2379 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2380 else
2381 /* We can't compute this yet. */
2382 ics = error_mark_node;
2384 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2385 /* ignore the near match. */;
2386 else if (ics)
2387 for (; fns; fns = OVL_NEXT (fns))
2389 tree fn = OVL_CURRENT (fns);
2390 struct z_candidate *old_candidates = candidates;
2392 /* [over.match.funcs] For conversion functions, the function is
2393 considered to be a member of the class of the implicit object
2394 argument for the purpose of defining the type of the implicit
2395 object parameter.
2397 So we pass fromtype as CTYPE to add_*_candidate. */
2399 if (TREE_CODE (fn) == TEMPLATE_DECL)
2401 templates = tree_cons (NULL_TREE, fn, templates);
2402 candidates =
2403 add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2404 args, totype, flags,
2405 DEDUCE_CONV);
2407 else
2408 candidates = add_function_candidate (candidates, fn, fromtype,
2409 args, flags);
2411 if (candidates != old_candidates)
2413 if (TREE_CODE (fn) == TEMPLATE_DECL)
2414 ics = implicit_conversion
2415 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2416 0, convflags);
2418 candidates->second_conv = ics;
2419 candidates->basetype_path = TYPE_BINFO (fromtype);
2421 if (ics == NULL_TREE)
2422 candidates->viable = 0;
2423 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2424 candidates->viable = -1;
2429 if (! any_viable (candidates))
2431 #if 0
2432 if (flags & LOOKUP_COMPLAIN)
2434 if (candidates && ! candidates->next)
2435 /* say why this one won't work or try to be loose */;
2436 else
2437 cp_error ("no viable candidates");
2439 #endif
2441 return 0;
2444 candidates = splice_viable (candidates);
2445 cand = tourney (candidates);
2447 if (cand == 0)
2449 if (flags & LOOKUP_COMPLAIN)
2451 cp_error ("conversion from `%T' to `%T' is ambiguous",
2452 fromtype, totype);
2453 print_z_candidates (candidates);
2456 cand = candidates; /* any one will do */
2457 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2458 ICS_USER_FLAG (cand->second_conv) = 1;
2459 ICS_BAD_FLAG (cand->second_conv) = 1;
2461 return cand;
2464 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2465 p = &(TREE_OPERAND (*p, 0));
2467 *p = build
2468 (USER_CONV,
2469 (DECL_CONSTRUCTOR_P (cand->fn)
2470 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2471 expr, build_ptr_wrapper (cand));
2473 ICS_USER_FLAG (cand->second_conv) = ICS_USER_FLAG (*p) = 1;
2474 if (cand->viable == -1)
2475 ICS_BAD_FLAG (cand->second_conv) = ICS_BAD_FLAG (*p) = 1;
2477 return cand;
2480 tree
2481 build_user_type_conversion (totype, expr, flags)
2482 tree totype, expr;
2483 int flags;
2485 struct z_candidate *cand
2486 = build_user_type_conversion_1 (totype, expr, flags);
2488 if (cand)
2490 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2491 return error_mark_node;
2492 return convert_from_reference (convert_like (cand->second_conv, expr));
2494 return NULL_TREE;
2497 /* Do any initial processing on the arguments to a function call. */
2499 static tree
2500 resolve_args (args)
2501 tree args;
2503 tree t;
2504 for (t = args; t; t = TREE_CHAIN (t))
2506 tree arg = TREE_VALUE (t);
2508 if (arg == error_mark_node)
2509 return error_mark_node;
2510 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2512 error ("invalid use of void expression");
2513 return error_mark_node;
2515 else if (TREE_CODE (arg) == OFFSET_REF)
2516 arg = resolve_offset_ref (arg);
2517 arg = convert_from_reference (arg);
2518 TREE_VALUE (t) = arg;
2520 return args;
2523 tree
2524 build_new_function_call (fn, args)
2525 tree fn, args;
2527 struct z_candidate *candidates = 0, *cand;
2528 tree explicit_targs = NULL_TREE;
2529 int template_only = 0;
2531 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2533 explicit_targs = TREE_OPERAND (fn, 1);
2534 fn = TREE_OPERAND (fn, 0);
2535 template_only = 1;
2538 if (really_overloaded_fn (fn))
2540 tree t1;
2541 tree templates = NULL_TREE;
2543 args = resolve_args (args);
2545 if (args == error_mark_node)
2546 return error_mark_node;
2548 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2550 tree t = OVL_FUNCTION (t1);
2552 if (TREE_CODE (t) == TEMPLATE_DECL)
2554 templates = tree_cons (NULL_TREE, t, templates);
2555 candidates = add_template_candidate
2556 (candidates, t, NULL_TREE, explicit_targs, args, NULL_TREE,
2557 LOOKUP_NORMAL, DEDUCE_CALL);
2559 else if (! template_only)
2560 candidates = add_function_candidate
2561 (candidates, t, NULL_TREE, args, LOOKUP_NORMAL);
2564 if (! any_viable (candidates))
2566 if (candidates && ! candidates->next)
2567 return build_function_call (candidates->fn, args);
2568 cp_error ("no matching function for call to `%D(%A)'",
2569 DECL_NAME (OVL_FUNCTION (fn)), args);
2570 if (candidates)
2571 print_z_candidates (candidates);
2572 return error_mark_node;
2574 candidates = splice_viable (candidates);
2575 cand = tourney (candidates);
2577 if (cand == 0)
2579 cp_error ("call of overloaded `%D(%A)' is ambiguous",
2580 DECL_NAME (OVL_FUNCTION (fn)), args);
2581 print_z_candidates (candidates);
2582 return error_mark_node;
2585 return build_over_call (cand, args, LOOKUP_NORMAL);
2588 /* This is not really overloaded. */
2589 fn = OVL_CURRENT (fn);
2591 return build_function_call (fn, args);
2594 static tree
2595 build_object_call (obj, args)
2596 tree obj, args;
2598 struct z_candidate *candidates = 0, *cand;
2599 tree fns, convs, mem_args = NULL_TREE;
2600 tree type = TREE_TYPE (obj);
2602 if (TYPE_PTRMEMFUNC_P (type))
2604 /* It's no good looking for an overloaded operator() on a
2605 pointer-to-member-function. */
2606 cp_error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2607 return error_mark_node;
2610 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2611 if (fns == error_mark_node)
2612 return error_mark_node;
2614 args = resolve_args (args);
2616 if (args == error_mark_node)
2617 return error_mark_node;
2619 if (fns)
2621 tree base = BINFO_TYPE (BASELINK_SUBOBJECT (fns));
2622 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2624 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2626 tree fn = OVL_CURRENT (fns);
2627 if (TREE_CODE (fn) == TEMPLATE_DECL)
2629 candidates
2630 = add_template_candidate (candidates, fn, base, NULL_TREE,
2631 mem_args, NULL_TREE,
2632 LOOKUP_NORMAL, DEDUCE_CALL);
2634 else
2635 candidates = add_function_candidate
2636 (candidates, fn, base, mem_args, LOOKUP_NORMAL);
2638 if (candidates)
2639 candidates->basetype_path = TYPE_BINFO (type);
2643 convs = lookup_conversions (type);
2645 for (; convs; convs = TREE_CHAIN (convs))
2647 tree fns = TREE_VALUE (convs);
2648 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2650 if ((TREE_CODE (totype) == POINTER_TYPE
2651 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2652 || (TREE_CODE (totype) == REFERENCE_TYPE
2653 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2654 || (TREE_CODE (totype) == REFERENCE_TYPE
2655 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2656 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2657 for (; fns; fns = OVL_NEXT (fns))
2659 tree fn = OVL_CURRENT (fns);
2660 if (TREE_CODE (fn) == TEMPLATE_DECL)
2662 candidates = add_template_conv_candidate (candidates,
2664 obj,
2665 args,
2666 totype);
2668 else
2669 candidates = add_conv_candidate (candidates, fn, obj, args);
2673 if (! any_viable (candidates))
2675 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2676 print_z_candidates (candidates);
2677 return error_mark_node;
2680 candidates = splice_viable (candidates);
2681 cand = tourney (candidates);
2683 if (cand == 0)
2685 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2686 print_z_candidates (candidates);
2687 return error_mark_node;
2690 /* Since cand->fn will be a type, not a function, for a conversion
2691 function, we must be careful not to unconditionally look at
2692 DECL_NAME here. */
2693 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2694 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2695 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2697 obj = convert_like_with_context
2698 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2700 /* FIXME */
2701 return build_function_call (obj, args);
2704 static void
2705 op_error (code, code2, arg1, arg2, arg3, problem)
2706 enum tree_code code, code2;
2707 tree arg1, arg2, arg3;
2708 const char *problem;
2710 const char *opname;
2712 if (code == MODIFY_EXPR)
2713 opname = assignment_operator_name_info[code2].name;
2714 else
2715 opname = operator_name_info[code].name;
2717 switch (code)
2719 case COND_EXPR:
2720 cp_error ("%s for `%T ? %T : %T' operator", problem,
2721 error_type (arg1), error_type (arg2), error_type (arg3));
2722 break;
2723 case POSTINCREMENT_EXPR:
2724 case POSTDECREMENT_EXPR:
2725 cp_error ("%s for `%T %s' operator", problem, error_type (arg1), opname);
2726 break;
2727 case ARRAY_REF:
2728 cp_error ("%s for `%T [%T]' operator", problem,
2729 error_type (arg1), error_type (arg2));
2730 break;
2731 default:
2732 if (arg2)
2733 cp_error ("%s for `%T %s %T' operator", problem,
2734 error_type (arg1), opname, error_type (arg2));
2735 else
2736 cp_error ("%s for `%s %T' operator", problem, opname, error_type (arg1));
2740 /* Return the implicit conversion sequence that could be used to
2741 convert E1 to E2 in [expr.cond]. */
2743 static tree
2744 conditional_conversion (e1, e2)
2745 tree e1;
2746 tree e2;
2748 tree t1 = non_reference (TREE_TYPE (e1));
2749 tree t2 = non_reference (TREE_TYPE (e2));
2750 tree conv;
2752 /* [expr.cond]
2754 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2755 implicitly converted (clause _conv_) to the type "reference to
2756 T2", subject to the constraint that in the conversion the
2757 reference must bind directly (_dcl.init.ref_) to E1. */
2758 if (real_lvalue_p (e2))
2760 conv = implicit_conversion (build_reference_type (t2),
2763 LOOKUP_NO_TEMP_BIND);
2764 if (conv)
2765 return conv;
2768 /* [expr.cond]
2770 If E1 and E2 have class type, and the underlying class types are
2771 the same or one is a base class of the other: E1 can be converted
2772 to match E2 if the class of T2 is the same type as, or a base
2773 class of, the class of T1, and the cv-qualification of T2 is the
2774 same cv-qualification as, or a greater cv-qualification than, the
2775 cv-qualification of T1. If the conversion is applied, E1 is
2776 changed to an rvalue of type T2 that still refers to the original
2777 source class object (or the appropriate subobject thereof). */
2778 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2779 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2780 TYPE_MAIN_VARIANT (t1)))
2782 if (at_least_as_qualified_p (t2, t1))
2784 conv = build1 (IDENTITY_CONV, t1, e1);
2785 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2786 TYPE_MAIN_VARIANT (t2)))
2787 conv = build_conv (BASE_CONV, t2, conv);
2788 return conv;
2790 else
2791 return NULL_TREE;
2794 /* [expr.cond]
2796 E1 can be converted to match E2 if E1 can be implicitly converted
2797 to the type that expression E2 would have if E2 were converted to
2798 an rvalue (or the type it has, if E2 is an rvalue). */
2799 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2802 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2803 arguments to the conditional expression. By the time this function
2804 is called, any suitable candidate functions are included in
2805 CANDIDATES. */
2807 tree
2808 build_conditional_expr (arg1, arg2, arg3)
2809 tree arg1;
2810 tree arg2;
2811 tree arg3;
2813 tree arg2_type;
2814 tree arg3_type;
2815 tree result;
2816 tree result_type = NULL_TREE;
2817 int lvalue_p = 1;
2818 struct z_candidate *candidates = 0;
2819 struct z_candidate *cand;
2821 /* As a G++ extension, the second argument to the conditional can be
2822 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2823 c'.) If the second operand is omitted, make sure it is
2824 calculated only once. */
2825 if (!arg2)
2827 if (pedantic)
2828 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2829 arg1 = arg2 = save_expr (arg1);
2832 /* [expr.cond]
2834 The first expr ession is implicitly converted to bool (clause
2835 _conv_). */
2836 arg1 = cp_convert (boolean_type_node, arg1);
2838 /* If something has already gone wrong, just pass that fact up the
2839 tree. */
2840 if (arg1 == error_mark_node
2841 || arg2 == error_mark_node
2842 || arg3 == error_mark_node
2843 || TREE_TYPE (arg1) == error_mark_node
2844 || TREE_TYPE (arg2) == error_mark_node
2845 || TREE_TYPE (arg3) == error_mark_node)
2846 return error_mark_node;
2848 /* [expr.cond]
2850 If either the second or the third operand has type (possibly
2851 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2852 array-to-pointer (_conv.array_), and function-to-pointer
2853 (_conv.func_) standard conversions are performed on the second
2854 and third operands. */
2855 arg2_type = TREE_TYPE (arg2);
2856 arg3_type = TREE_TYPE (arg3);
2857 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
2859 /* Do the conversions. We don't these for `void' type arguments
2860 since it can't have any effect and since decay_conversion
2861 does not handle that case gracefully. */
2862 if (!VOID_TYPE_P (arg2_type))
2863 arg2 = decay_conversion (arg2);
2864 if (!VOID_TYPE_P (arg3_type))
2865 arg3 = decay_conversion (arg3);
2866 arg2_type = TREE_TYPE (arg2);
2867 arg3_type = TREE_TYPE (arg3);
2869 /* [expr.cond]
2871 One of the following shall hold:
2873 --The second or the third operand (but not both) is a
2874 throw-expression (_except.throw_); the result is of the
2875 type of the other and is an rvalue.
2877 --Both the second and the third operands have type void; the
2878 result is of type void and is an rvalue. */
2879 if ((TREE_CODE (arg2) == THROW_EXPR)
2880 ^ (TREE_CODE (arg3) == THROW_EXPR))
2881 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2882 ? arg3_type : arg2_type);
2883 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
2884 result_type = void_type_node;
2885 else
2887 cp_error ("`%E' has type `void' and is not a throw-expression",
2888 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
2889 return error_mark_node;
2892 lvalue_p = 0;
2893 goto valid_operands;
2895 /* [expr.cond]
2897 Otherwise, if the second and third operand have different types,
2898 and either has (possibly cv-qualified) class type, an attempt is
2899 made to convert each of those operands to the type of the other. */
2900 else if (!same_type_p (arg2_type, arg3_type)
2901 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2903 tree conv2 = conditional_conversion (arg2, arg3);
2904 tree conv3 = conditional_conversion (arg3, arg2);
2906 /* [expr.cond]
2908 If both can be converted, or one can be converted but the
2909 conversion is ambiguous, the program is ill-formed. If
2910 neither can be converted, the operands are left unchanged and
2911 further checking is performed as described below. If exactly
2912 one conversion is possible, that conversion is applied to the
2913 chosen operand and the converted operand is used in place of
2914 the original operand for the remainder of this section. */
2915 if ((conv2 && !ICS_BAD_FLAG (conv2)
2916 && conv3 && !ICS_BAD_FLAG (conv3))
2917 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
2918 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
2920 cp_error ("operands to ?: have different types");
2921 return error_mark_node;
2923 else if (conv2 && !ICS_BAD_FLAG (conv2))
2925 arg2 = convert_like (conv2, arg2);
2926 arg2 = convert_from_reference (arg2);
2927 /* That may not quite have done the trick. If the two types
2928 are cv-qualified variants of one another, we will have
2929 just used an IDENTITY_CONV. (There's no conversion from
2930 an lvalue of one class type to an lvalue of another type,
2931 even a cv-qualified variant, and we don't want to lose
2932 lvalue-ness here.) So, we manually add a NOP_EXPR here
2933 if necessary. */
2934 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
2935 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
2936 arg2_type = TREE_TYPE (arg2);
2938 else if (conv3 && !ICS_BAD_FLAG (conv3))
2940 arg3 = convert_like (conv3, arg3);
2941 arg3 = convert_from_reference (arg3);
2942 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
2943 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
2944 arg3_type = TREE_TYPE (arg3);
2948 /* [expr.cond]
2950 If the second and third operands are lvalues and have the same
2951 type, the result is of that type and is an lvalue. */
2952 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
2953 same_type_p (arg2_type, arg3_type))
2955 result_type = arg2_type;
2956 goto valid_operands;
2959 /* [expr.cond]
2961 Otherwise, the result is an rvalue. If the second and third
2962 operand do not have the same type, and either has (possibly
2963 cv-qualified) class type, overload resolution is used to
2964 determine the conversions (if any) to be applied to the operands
2965 (_over.match.oper_, _over.built_). */
2966 lvalue_p = 0;
2967 if (!same_type_p (arg2_type, arg3_type)
2968 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2970 tree args[3];
2971 tree conv;
2973 /* Rearrange the arguments so that add_builtin_candidate only has
2974 to know about two args. In build_builtin_candidates, the
2975 arguments are unscrambled. */
2976 args[0] = arg2;
2977 args[1] = arg3;
2978 args[2] = arg1;
2979 candidates = add_builtin_candidates (candidates,
2980 COND_EXPR,
2981 NOP_EXPR,
2982 ansi_opname (COND_EXPR),
2983 args,
2984 LOOKUP_NORMAL);
2986 /* [expr.cond]
2988 If the overload resolution fails, the program is
2989 ill-formed. */
2990 if (!any_viable (candidates))
2992 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
2993 print_z_candidates (candidates);
2994 return error_mark_node;
2996 candidates = splice_viable (candidates);
2997 cand = tourney (candidates);
2998 if (!cand)
3000 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3001 print_z_candidates (candidates);
3002 return error_mark_node;
3005 /* [expr.cond]
3007 Otherwise, the conversions thus determined are applied, and
3008 the converted operands are used in place of the original
3009 operands for the remainder of this section. */
3010 conv = TREE_VEC_ELT (cand->convs, 0);
3011 arg1 = convert_like (conv, arg1);
3012 conv = TREE_VEC_ELT (cand->convs, 1);
3013 arg2 = convert_like (conv, arg2);
3014 conv = TREE_VEC_ELT (cand->convs, 2);
3015 arg3 = convert_like (conv, arg3);
3018 /* [expr.cond]
3020 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3021 and function-to-pointer (_conv.func_) standard conversions are
3022 performed on the second and third operands.
3024 We need to force the lvalue-to-rvalue conversion here for class types,
3025 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3026 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3027 regions.
3029 We use ocp_convert rather than build_user_type_conversion because the
3030 latter returns NULL_TREE on failure, while the former gives an error. */
3032 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3033 arg2 = ocp_convert (TREE_TYPE (arg2), arg2,
3034 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3035 else
3036 arg2 = decay_conversion (arg2);
3037 arg2_type = TREE_TYPE (arg2);
3039 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3040 arg3 = ocp_convert (TREE_TYPE (arg3), arg3,
3041 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3042 else
3043 arg3 = decay_conversion (arg3);
3044 arg3_type = TREE_TYPE (arg3);
3046 if (arg2 == error_mark_node || arg3 == error_mark_node)
3047 return error_mark_node;
3049 /* [expr.cond]
3051 After those conversions, one of the following shall hold:
3053 --The second and third operands have the same type; the result is of
3054 that type. */
3055 if (same_type_p (arg2_type, arg3_type))
3056 result_type = arg2_type;
3057 /* [expr.cond]
3059 --The second and third operands have arithmetic or enumeration
3060 type; the usual arithmetic conversions are performed to bring
3061 them to a common type, and the result is of that type. */
3062 else if ((ARITHMETIC_TYPE_P (arg2_type)
3063 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3064 && (ARITHMETIC_TYPE_P (arg3_type)
3065 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3067 /* In this case, there is always a common type. */
3068 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3069 arg3_type);
3071 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3072 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3073 cp_warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3074 arg2_type, arg3_type);
3075 else if (extra_warnings
3076 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3077 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3078 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3079 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3080 cp_warning ("enumeral and non-enumeral type in conditional expression");
3082 arg2 = perform_implicit_conversion (result_type, arg2);
3083 arg3 = perform_implicit_conversion (result_type, arg3);
3085 /* [expr.cond]
3087 --The second and third operands have pointer type, or one has
3088 pointer type and the other is a null pointer constant; pointer
3089 conversions (_conv.ptr_) and qualification conversions
3090 (_conv.qual_) are performed to bring them to their composite
3091 pointer type (_expr.rel_). The result is of the composite
3092 pointer type.
3094 --The second and third operands have pointer to member type, or
3095 one has pointer to member type and the other is a null pointer
3096 constant; pointer to member conversions (_conv.mem_) and
3097 qualification conversions (_conv.qual_) are performed to bring
3098 them to a common type, whose cv-qualification shall match the
3099 cv-qualification of either the second or the third operand.
3100 The result is of the common type. */
3101 else if ((null_ptr_cst_p (arg2)
3102 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3103 || TYPE_PTRMEMFUNC_P (arg3_type)))
3104 || (null_ptr_cst_p (arg3)
3105 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3106 || TYPE_PTRMEMFUNC_P (arg2_type)))
3107 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3108 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3109 || (TYPE_PTRMEMFUNC_P (arg2_type)
3110 && TYPE_PTRMEMFUNC_P (arg3_type)))
3112 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3113 arg3, "conditional expression");
3114 arg2 = perform_implicit_conversion (result_type, arg2);
3115 arg3 = perform_implicit_conversion (result_type, arg3);
3118 if (!result_type)
3120 cp_error ("operands to ?: have different types");
3121 return error_mark_node;
3124 valid_operands:
3125 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3126 /* Expand both sides into the same slot, hopefully the target of the
3127 ?: expression. We used to check for TARGET_EXPRs here, but now we
3128 sometimes wrap them in NOP_EXPRs so the test would fail. */
3129 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3130 result = build_target_expr_with_type (result, result_type);
3132 /* If this expression is an rvalue, but might be mistaken for an
3133 lvalue, we must add a NON_LVALUE_EXPR. */
3134 if (!lvalue_p && real_lvalue_p (result))
3135 result = build1 (NON_LVALUE_EXPR, result_type, result);
3137 return result;
3140 tree
3141 build_new_op (code, flags, arg1, arg2, arg3)
3142 enum tree_code code;
3143 int flags;
3144 tree arg1, arg2, arg3;
3146 struct z_candidate *candidates = 0, *cand;
3147 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3148 enum tree_code code2 = NOP_EXPR;
3149 tree templates = NULL_TREE;
3150 tree conv;
3152 if (arg1 == error_mark_node
3153 || arg2 == error_mark_node
3154 || arg3 == error_mark_node)
3155 return error_mark_node;
3157 /* This can happen if a template takes all non-type parameters, e.g.
3158 undeclared_template<1, 5, 72>a; */
3159 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3161 cp_error ("`%D' must be declared before use", arg1);
3162 return error_mark_node;
3165 if (code == MODIFY_EXPR)
3167 code2 = TREE_CODE (arg3);
3168 arg3 = NULL_TREE;
3169 fnname = ansi_assopname (code2);
3171 else
3172 fnname = ansi_opname (code);
3174 if (TREE_CODE (arg1) == OFFSET_REF)
3175 arg1 = resolve_offset_ref (arg1);
3176 arg1 = convert_from_reference (arg1);
3178 switch (code)
3180 case NEW_EXPR:
3181 case VEC_NEW_EXPR:
3182 case VEC_DELETE_EXPR:
3183 case DELETE_EXPR:
3184 /* Use build_op_new_call and build_op_delete_call instead. */
3185 my_friendly_abort (981018);
3187 case CALL_EXPR:
3188 return build_object_call (arg1, arg2);
3190 default:
3191 break;
3194 if (arg2)
3196 if (TREE_CODE (arg2) == OFFSET_REF)
3197 arg2 = resolve_offset_ref (arg2);
3198 arg2 = convert_from_reference (arg2);
3200 if (arg3)
3202 if (TREE_CODE (arg3) == OFFSET_REF)
3203 arg3 = resolve_offset_ref (arg3);
3204 arg3 = convert_from_reference (arg3);
3207 if (code == COND_EXPR)
3209 if (arg2 == NULL_TREE
3210 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3211 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3212 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3213 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3214 goto builtin;
3216 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3217 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3218 goto builtin;
3220 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3221 arg2 = integer_zero_node;
3223 if (arg2 && arg3)
3224 arglist = tree_cons (NULL_TREE, arg1, tree_cons
3225 (NULL_TREE, arg2, build_tree_list (NULL_TREE, arg3)));
3226 else if (arg2)
3227 arglist = tree_cons (NULL_TREE, arg1, build_tree_list (NULL_TREE, arg2));
3228 else
3229 arglist = build_tree_list (NULL_TREE, arg1);
3231 fns = lookup_function_nonclass (fnname, arglist);
3233 if (fns && TREE_CODE (fns) == TREE_LIST)
3234 fns = TREE_VALUE (fns);
3235 for (; fns; fns = OVL_NEXT (fns))
3237 tree fn = OVL_CURRENT (fns);
3238 if (TREE_CODE (fn) == TEMPLATE_DECL)
3240 templates = tree_cons (NULL_TREE, fn, templates);
3241 candidates
3242 = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3243 arglist, TREE_TYPE (fnname),
3244 flags, DEDUCE_CALL);
3246 else
3247 candidates = add_function_candidate (candidates, fn, NULL_TREE,
3248 arglist, flags);
3251 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3253 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3254 if (fns == error_mark_node)
3255 return fns;
3257 else
3258 fns = NULL_TREE;
3260 if (fns)
3262 tree basetype = BINFO_TYPE (BASELINK_SUBOBJECT (fns));
3263 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3264 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
3266 tree fn = OVL_CURRENT (fns);
3267 tree this_arglist;
3269 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3270 this_arglist = mem_arglist;
3271 else
3272 this_arglist = arglist;
3274 if (TREE_CODE (fn) == TEMPLATE_DECL)
3276 /* A member template. */
3277 templates = tree_cons (NULL_TREE, fn, templates);
3278 candidates
3279 = add_template_candidate (candidates, fn, basetype, NULL_TREE,
3280 this_arglist, TREE_TYPE (fnname),
3281 flags, DEDUCE_CALL);
3283 else
3284 candidates = add_function_candidate
3285 (candidates, fn, basetype, this_arglist, flags);
3287 if (candidates)
3288 candidates->basetype_path = TYPE_BINFO (TREE_TYPE (arg1));
3293 tree args[3];
3295 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3296 to know about two args; a builtin candidate will always have a first
3297 parameter of type bool. We'll handle that in
3298 build_builtin_candidate. */
3299 if (code == COND_EXPR)
3301 args[0] = arg2;
3302 args[1] = arg3;
3303 args[2] = arg1;
3305 else
3307 args[0] = arg1;
3308 args[1] = arg2;
3309 args[2] = NULL_TREE;
3312 candidates = add_builtin_candidates
3313 (candidates, code, code2, fnname, args, flags);
3316 if (! any_viable (candidates))
3318 switch (code)
3320 case POSTINCREMENT_EXPR:
3321 case POSTDECREMENT_EXPR:
3322 /* Look for an `operator++ (int)'. If they didn't have
3323 one, then we fall back to the old way of doing things. */
3324 if (flags & LOOKUP_COMPLAIN)
3325 cp_pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3326 fnname,
3327 operator_name_info[code].name);
3328 if (code == POSTINCREMENT_EXPR)
3329 code = PREINCREMENT_EXPR;
3330 else
3331 code = PREDECREMENT_EXPR;
3332 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3334 /* The caller will deal with these. */
3335 case ADDR_EXPR:
3336 case COMPOUND_EXPR:
3337 case COMPONENT_REF:
3338 return NULL_TREE;
3340 default:
3341 break;
3343 if (flags & LOOKUP_COMPLAIN)
3345 op_error (code, code2, arg1, arg2, arg3, "no match");
3346 print_z_candidates (candidates);
3348 return error_mark_node;
3350 candidates = splice_viable (candidates);
3351 cand = tourney (candidates);
3353 if (cand == 0)
3355 if (flags & LOOKUP_COMPLAIN)
3357 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3358 print_z_candidates (candidates);
3360 return error_mark_node;
3363 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3365 extern int warn_synth;
3366 if (warn_synth
3367 && fnname == ansi_assopname (NOP_EXPR)
3368 && DECL_ARTIFICIAL (cand->fn)
3369 && candidates->next
3370 && ! candidates->next->next)
3372 cp_warning ("using synthesized `%#D' for copy assignment",
3373 cand->fn);
3374 cp_warning_at (" where cfront would use `%#D'",
3375 cand == candidates
3376 ? candidates->next->fn
3377 : candidates->fn);
3380 return build_over_call
3381 (cand,
3382 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3383 ? mem_arglist : arglist,
3384 LOOKUP_NORMAL);
3387 /* Check for comparison of different enum types. */
3388 switch (code)
3390 case GT_EXPR:
3391 case LT_EXPR:
3392 case GE_EXPR:
3393 case LE_EXPR:
3394 case EQ_EXPR:
3395 case NE_EXPR:
3396 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3397 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3398 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3399 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3401 cp_warning ("comparison between `%#T' and `%#T'",
3402 TREE_TYPE (arg1), TREE_TYPE (arg2));
3404 break;
3405 default:
3406 break;
3409 /* We need to strip any leading REF_BIND so that bitfields don't cause
3410 errors. This should not remove any important conversions, because
3411 builtins don't apply to class objects directly. */
3412 conv = TREE_VEC_ELT (cand->convs, 0);
3413 if (TREE_CODE (conv) == REF_BIND)
3414 conv = TREE_OPERAND (conv, 0);
3415 arg1 = convert_like (conv, arg1);
3416 if (arg2)
3418 conv = TREE_VEC_ELT (cand->convs, 1);
3419 if (TREE_CODE (conv) == REF_BIND)
3420 conv = TREE_OPERAND (conv, 0);
3421 arg2 = convert_like (conv, arg2);
3423 if (arg3)
3425 conv = TREE_VEC_ELT (cand->convs, 2);
3426 if (TREE_CODE (conv) == REF_BIND)
3427 conv = TREE_OPERAND (conv, 0);
3428 arg3 = convert_like (conv, arg3);
3431 builtin:
3432 switch (code)
3434 case MODIFY_EXPR:
3435 return build_modify_expr (arg1, code2, arg2);
3437 case INDIRECT_REF:
3438 return build_indirect_ref (arg1, "unary *");
3440 case PLUS_EXPR:
3441 case MINUS_EXPR:
3442 case MULT_EXPR:
3443 case TRUNC_DIV_EXPR:
3444 case GT_EXPR:
3445 case LT_EXPR:
3446 case GE_EXPR:
3447 case LE_EXPR:
3448 case EQ_EXPR:
3449 case NE_EXPR:
3450 case MAX_EXPR:
3451 case MIN_EXPR:
3452 case LSHIFT_EXPR:
3453 case RSHIFT_EXPR:
3454 case TRUNC_MOD_EXPR:
3455 case BIT_AND_EXPR:
3456 case BIT_IOR_EXPR:
3457 case BIT_XOR_EXPR:
3458 case TRUTH_ANDIF_EXPR:
3459 case TRUTH_ORIF_EXPR:
3460 return cp_build_binary_op (code, arg1, arg2);
3462 case CONVERT_EXPR:
3463 case NEGATE_EXPR:
3464 case BIT_NOT_EXPR:
3465 case TRUTH_NOT_EXPR:
3466 case PREINCREMENT_EXPR:
3467 case POSTINCREMENT_EXPR:
3468 case PREDECREMENT_EXPR:
3469 case POSTDECREMENT_EXPR:
3470 case REALPART_EXPR:
3471 case IMAGPART_EXPR:
3472 return build_unary_op (code, arg1, candidates != 0);
3474 case ARRAY_REF:
3475 return build_array_ref (arg1, arg2);
3477 case COND_EXPR:
3478 return build_conditional_expr (arg1, arg2, arg3);
3480 case MEMBER_REF:
3481 return build_m_component_ref
3482 (build_indirect_ref (arg1, NULL), arg2);
3484 /* The caller will deal with these. */
3485 case ADDR_EXPR:
3486 case COMPONENT_REF:
3487 case COMPOUND_EXPR:
3488 return NULL_TREE;
3490 default:
3491 my_friendly_abort (367);
3492 return NULL_TREE;
3496 /* Build a call to operator delete. This has to be handled very specially,
3497 because the restrictions on what signatures match are different from all
3498 other call instances. For a normal delete, only a delete taking (void *)
3499 or (void *, size_t) is accepted. For a placement delete, only an exact
3500 match with the placement new is accepted.
3502 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3503 ADDR is the pointer to be deleted. For placement delete, it is also
3504 used to determine what the corresponding new looked like.
3505 SIZE is the size of the memory block to be deleted.
3506 FLAGS are the usual overloading flags.
3507 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3509 tree
3510 build_op_delete_call (code, addr, size, flags, placement)
3511 enum tree_code code;
3512 tree addr, size, placement;
3513 int flags;
3515 tree fn = NULL_TREE;
3516 tree fns, fnname, fntype, argtypes, args, type;
3517 int pass;
3519 if (addr == error_mark_node)
3520 return error_mark_node;
3522 type = TREE_TYPE (TREE_TYPE (addr));
3523 while (TREE_CODE (type) == ARRAY_TYPE)
3524 type = TREE_TYPE (type);
3526 fnname = ansi_opname (code);
3528 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3529 /* In [class.free]
3531 If the result of the lookup is ambiguous or inaccessible, or if
3532 the lookup selects a placement deallocation function, the
3533 program is ill-formed.
3535 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3537 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3538 if (fns == error_mark_node)
3539 return error_mark_node;
3541 else
3542 fns = NULL_TREE;
3544 if (fns == NULL_TREE)
3545 fns = lookup_name_nonclass (fnname);
3547 if (placement)
3549 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3551 /* Extract the function. */
3552 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3553 /* Then the second parm type. */
3554 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3556 /* Also the second argument. */
3557 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3559 else
3561 /* First try it without the size argument. */
3562 argtypes = void_list_node;
3563 args = NULL_TREE;
3566 /* Strip const and volatile from addr. */
3567 addr = cp_convert (ptr_type_node, addr);
3569 /* We make two tries at finding a matching `operator delete'. On
3570 the first pass, we look for an one-operator (or placement)
3571 operator delete. If we're not doing placement delete, then on
3572 the second pass we look for a two-argument delete. */
3573 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3575 if (pass == 0)
3576 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3577 else
3578 /* Normal delete; now try to find a match including the size
3579 argument. */
3580 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3581 tree_cons (NULL_TREE, sizetype,
3582 void_list_node));
3583 fntype = build_function_type (void_type_node, argtypes);
3585 /* Go through the `operator delete' functions looking for one
3586 with a matching type. */
3587 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3588 fn;
3589 fn = OVL_NEXT (fn))
3591 tree t;
3593 /* Exception specifications on the `delete' operator do not
3594 matter. */
3595 t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3596 NULL_TREE);
3597 /* We also don't compare attributes. We're really just
3598 trying to check the types of the first two parameters. */
3599 if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3600 break;
3603 /* If we found a match, we're done. */
3604 if (fn)
3605 break;
3608 /* If we have a matching function, call it. */
3609 if (fn)
3611 /* Make sure we have the actual function, and not an
3612 OVERLOAD. */
3613 fn = OVL_CURRENT (fn);
3615 /* If the FN is a member function, make sure that it is
3616 accessible. */
3617 if (DECL_CLASS_SCOPE_P (fn))
3618 enforce_access (type, fn);
3620 if (pass == 0)
3621 args = tree_cons (NULL_TREE, addr, args);
3622 else
3623 args = tree_cons (NULL_TREE, addr,
3624 build_tree_list (NULL_TREE, size));
3626 return build_function_call (fn, args);
3629 /* If we are doing placement delete we do nothing if we don't find a
3630 matching op delete. */
3631 if (placement)
3632 return NULL_TREE;
3634 cp_error ("no suitable `operator delete' for `%T'", type);
3635 return error_mark_node;
3638 /* If the current scope isn't allowed to access DECL along
3639 BASETYPE_PATH, give an error. The most derived class in
3640 BASETYPE_PATH is the one used to qualify DECL. */
3643 enforce_access (basetype_path, decl)
3644 tree basetype_path;
3645 tree decl;
3647 int accessible;
3649 accessible = accessible_p (basetype_path, decl);
3650 if (!accessible)
3652 if (TREE_PRIVATE (decl))
3653 cp_error_at ("`%+#D' is private", decl);
3654 else if (TREE_PROTECTED (decl))
3655 cp_error_at ("`%+#D' is protected", decl);
3656 else
3657 cp_error_at ("`%+#D' is inaccessible", decl);
3658 cp_error ("within this context");
3659 return 0;
3662 return 1;
3665 /* Perform the conversions in CONVS on the expression EXPR.
3666 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3667 indicates the `this' argument of a method. INNER is non-zero when
3668 being called to continue a conversion chain. It is negative when a
3669 reference binding will be applied, positive otherwise. */
3671 static tree
3672 convert_like_real (convs, expr, fn, argnum, inner)
3673 tree convs, expr;
3674 tree fn;
3675 int argnum;
3676 int inner;
3678 int savew, savee;
3680 tree totype = TREE_TYPE (convs);
3682 if (ICS_BAD_FLAG (convs)
3683 && TREE_CODE (convs) != USER_CONV
3684 && TREE_CODE (convs) != AMBIG_CONV
3685 && TREE_CODE (convs) != REF_BIND)
3687 tree t = convs;
3688 for (; t; t = TREE_OPERAND (t, 0))
3690 if (TREE_CODE (t) == USER_CONV)
3692 expr = convert_like_real (t, expr, fn, argnum, 1);
3693 break;
3695 else if (TREE_CODE (t) == AMBIG_CONV)
3696 return convert_like_real (t, expr, fn, argnum, 1);
3697 else if (TREE_CODE (t) == IDENTITY_CONV)
3698 break;
3700 return convert_for_initialization
3701 (NULL_TREE, totype, expr, LOOKUP_NORMAL,
3702 "conversion", fn, argnum);
3705 if (!inner)
3706 expr = dubious_conversion_warnings
3707 (totype, expr, "argument", fn, argnum);
3708 switch (TREE_CODE (convs))
3710 case USER_CONV:
3712 struct z_candidate *cand
3713 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3714 tree convfn = cand->fn;
3715 tree args;
3717 if (DECL_CONSTRUCTOR_P (convfn))
3719 tree t = build_int_2 (0, 0);
3720 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3722 args = build_tree_list (NULL_TREE, expr);
3723 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3724 || DECL_HAS_VTT_PARM_P (convfn))
3725 /* We should never try to call the abstract or base constructor
3726 from here. */
3727 abort ();
3728 args = tree_cons (NULL_TREE, t, args);
3730 else
3731 args = build_this (expr);
3732 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3734 /* If this is a constructor or a function returning an aggr type,
3735 we need to build up a TARGET_EXPR. */
3736 if (DECL_CONSTRUCTOR_P (convfn))
3737 expr = build_cplus_new (totype, expr);
3739 /* The result of the call is then used to direct-initialize the object
3740 that is the destination of the copy-initialization. [dcl.init]
3742 Note that this step is not reflected in the conversion sequence;
3743 it affects the semantics when we actually perform the
3744 conversion, but is not considered during overload resolution.
3746 If the target is a class, that means call a ctor. */
3747 if (IS_AGGR_TYPE (totype)
3748 && (inner >= 0 || !real_lvalue_p (expr)))
3750 savew = warningcount, savee = errorcount;
3751 expr = build_new_method_call
3752 (NULL_TREE, complete_ctor_identifier,
3753 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3754 /* Core issue 84, now a DR, says that we don't allow UDCs
3755 for these args (which deliberately breaks copy-init of an
3756 auto_ptr<Base> from an auto_ptr<Derived>). */
3757 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3759 /* Tell the user where this failing constructor call came from. */
3760 if (fn)
3762 if (warningcount > savew)
3763 cp_warning
3764 (" initializing argument %P of `%D' from result of `%D'",
3765 argnum, fn, convfn);
3766 else if (errorcount > savee)
3767 cp_error
3768 (" initializing argument %P of `%D' from result of `%D'",
3769 argnum, fn, convfn);
3771 else
3773 if (warningcount > savew)
3774 cp_warning (" initializing temporary from result of `%D'",
3775 convfn);
3776 else if (errorcount > savee)
3777 cp_error (" initializing temporary from result of `%D'",
3778 convfn);
3780 expr = build_cplus_new (totype, expr);
3782 return expr;
3784 case IDENTITY_CONV:
3785 if (type_unknown_p (expr))
3786 expr = instantiate_type (totype, expr, itf_complain);
3787 return expr;
3788 case AMBIG_CONV:
3789 /* Call build_user_type_conversion again for the error. */
3790 return build_user_type_conversion
3791 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3793 default:
3794 break;
3797 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
3798 TREE_CODE (convs) == REF_BIND ? -1 : 1);
3799 if (expr == error_mark_node)
3800 return error_mark_node;
3802 /* Convert a non-array constant variable to its underlying value, unless we
3803 are about to bind it to a reference, in which case we need to
3804 leave it as an lvalue. */
3805 if (TREE_CODE (convs) != REF_BIND
3806 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3807 expr = decl_constant_value (expr);
3809 switch (TREE_CODE (convs))
3811 case RVALUE_CONV:
3812 if (! IS_AGGR_TYPE (totype))
3813 return expr;
3814 /* else fall through */
3815 case BASE_CONV:
3816 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3818 /* We are going to bind a reference directly to a base-class
3819 subobject of EXPR. */
3820 tree base_ptr = build_pointer_type (totype);
3822 /* Build an expression for `*((base*) &expr)'. */
3823 expr = build_unary_op (ADDR_EXPR, expr, 0);
3824 expr = perform_implicit_conversion (base_ptr, expr);
3825 expr = build_indirect_ref (expr, "implicit conversion");
3826 return expr;
3829 /* Copy-initialization where the cv-unqualified version of the source
3830 type is the same class as, or a derived class of, the class of the
3831 destination [is treated as direct-initialization]. [dcl.init] */
3832 savew = warningcount, savee = errorcount;
3833 expr = build_new_method_call (NULL_TREE, complete_ctor_identifier,
3834 build_tree_list (NULL_TREE, expr),
3835 TYPE_BINFO (totype),
3836 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
3837 if (fn)
3839 if (warningcount > savew)
3840 cp_warning (" initializing argument %P of `%D'", argnum, fn);
3841 else if (errorcount > savee)
3842 cp_error (" initializing argument %P of `%D'", argnum, fn);
3844 return build_cplus_new (totype, expr);
3846 case REF_BIND:
3848 tree ref_type = totype;
3850 /* If necessary, create a temporary. */
3851 if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
3853 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3854 expr = build_target_expr_with_type (expr, type);
3857 /* Take the address of the thing to which we will bind the
3858 reference. */
3859 expr = build_unary_op (ADDR_EXPR, expr, 1);
3860 if (expr == error_mark_node)
3861 return error_mark_node;
3863 /* Convert it to a pointer to the type referred to by the
3864 reference. This will adjust the pointer if a derived to
3865 base conversion is being performed. */
3866 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3867 expr);
3868 /* Convert the pointer to the desired reference type. */
3869 expr = build1 (NOP_EXPR, ref_type, expr);
3871 return expr;
3874 case LVALUE_CONV:
3875 return decay_conversion (expr);
3877 case QUAL_CONV:
3878 /* Warn about deprecated conversion if appropriate. */
3879 string_conv_p (totype, expr, 1);
3880 break;
3882 default:
3883 break;
3885 return ocp_convert (totype, expr, CONV_IMPLICIT,
3886 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3889 /* ARG is being passed to a varargs function. Perform any conversions
3890 required. Array/function to pointer decay must have already happened.
3891 Return the converted value. */
3893 tree
3894 convert_arg_to_ellipsis (arg)
3895 tree arg;
3897 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3898 && (TYPE_PRECISION (TREE_TYPE (arg))
3899 < TYPE_PRECISION (double_type_node)))
3900 /* Convert `float' to `double'. */
3901 arg = cp_convert (double_type_node, arg);
3902 else
3903 /* Convert `short' and `char' to full-size `int'. */
3904 arg = default_conversion (arg);
3906 arg = require_complete_type (arg);
3908 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
3910 /* Undefined behaviour [expr.call] 5.2.2/7. */
3911 cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3912 TREE_TYPE (arg));
3915 return arg;
3918 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
3920 tree
3921 build_x_va_arg (expr, type)
3922 tree expr;
3923 tree type;
3925 if (processing_template_decl)
3926 return build_min (VA_ARG_EXPR, type, expr);
3928 type = complete_type_or_else (type, NULL_TREE);
3930 if (expr == error_mark_node || !type)
3931 return error_mark_node;
3933 if (! pod_type_p (type))
3935 /* Undefined behaviour [expr.call] 5.2.2/7. */
3936 cp_warning ("cannot receive objects of non-POD type `%#T' through `...'",
3937 type);
3940 return build_va_arg (expr, type);
3943 /* TYPE has been given to va_arg. Apply the default conversions which would
3944 have happened when passed via ellipsis. Return the promoted type, or
3945 NULL_TREE, if there is no change. */
3947 tree
3948 convert_type_from_ellipsis (type)
3949 tree type;
3951 tree promote;
3953 if (TREE_CODE (type) == ARRAY_TYPE)
3954 promote = build_pointer_type (TREE_TYPE (type));
3955 else if (TREE_CODE (type) == FUNCTION_TYPE)
3956 promote = build_pointer_type (type);
3957 else
3958 promote = type_promotes_to (type);
3960 return same_type_p (type, promote) ? NULL_TREE : promote;
3963 /* ARG is a default argument expression being passed to a parameter of
3964 the indicated TYPE, which is a parameter to FN. Do any required
3965 conversions. Return the converted value. */
3967 tree
3968 convert_default_arg (type, arg, fn, parmnum)
3969 tree type;
3970 tree arg;
3971 tree fn;
3972 int parmnum;
3974 if (TREE_CODE (arg) == DEFAULT_ARG)
3976 cp_error ("circularity detected in use of default argument for `%D'",
3977 fn);
3978 return error_mark_node;
3981 if (fn && DECL_TEMPLATE_INFO (fn))
3982 arg = tsubst_default_argument (fn, type, arg);
3984 arg = break_out_target_exprs (arg);
3986 if (TREE_CODE (arg) == CONSTRUCTOR)
3988 arg = digest_init (type, arg, 0);
3989 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3990 "default argument", fn, parmnum);
3992 else
3994 /* This could get clobbered by the following call. */
3995 if (TREE_HAS_CONSTRUCTOR (arg))
3996 arg = copy_node (arg);
3998 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3999 "default argument", fn, parmnum);
4000 if (PROMOTE_PROTOTYPES
4001 && INTEGRAL_TYPE_P (type)
4002 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4003 arg = default_conversion (arg);
4006 return arg;
4009 /* Subroutine of the various build_*_call functions. Overload resolution
4010 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4011 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4012 bitmask of various LOOKUP_* flags which apply to the call itself. */
4014 static tree
4015 build_over_call (cand, args, flags)
4016 struct z_candidate *cand;
4017 tree args;
4018 int flags;
4020 tree fn = cand->fn;
4021 tree convs = cand->convs;
4022 tree converted_args = NULL_TREE;
4023 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4024 tree conv, arg, val;
4025 int i = 0;
4026 int is_method = 0;
4028 /* Give any warnings we noticed during overload resolution. */
4029 if (cand->warnings)
4030 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4031 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
4033 if (DECL_FUNCTION_MEMBER_P (fn))
4034 enforce_access (cand->basetype_path, fn);
4036 if (args && TREE_CODE (args) != TREE_LIST)
4037 args = build_tree_list (NULL_TREE, args);
4038 arg = args;
4040 /* The implicit parameters to a constructor are not considered by overload
4041 resolution, and must be of the proper type. */
4042 if (DECL_CONSTRUCTOR_P (fn))
4044 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4045 arg = TREE_CHAIN (arg);
4046 parm = TREE_CHAIN (parm);
4047 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4048 /* We should never try to call the abstract constructor. */
4049 abort ();
4050 if (DECL_HAS_VTT_PARM_P (fn))
4052 converted_args = tree_cons
4053 (NULL_TREE, TREE_VALUE (arg), converted_args);
4054 arg = TREE_CHAIN (arg);
4055 parm = TREE_CHAIN (parm);
4058 /* Bypass access control for 'this' parameter. */
4059 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4061 tree parmtype = TREE_VALUE (parm);
4062 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4063 tree t;
4064 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4065 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4066 TREE_TYPE (argtype), fn);
4068 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4069 X is called for an object that is not of type X, or of a type
4070 derived from X, the behavior is undefined.
4072 So we can assume that anything passed as 'this' is non-null, and
4073 optimize accordingly. */
4074 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4075 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
4076 converted_args = tree_cons (NULL_TREE, t, converted_args);
4077 parm = TREE_CHAIN (parm);
4078 arg = TREE_CHAIN (arg);
4079 ++i;
4080 is_method = 1;
4083 for (; arg && parm;
4084 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4086 tree type = TREE_VALUE (parm);
4088 conv = TREE_VEC_ELT (convs, i);
4089 if (ICS_BAD_FLAG (conv))
4091 tree t = conv;
4092 val = TREE_VALUE (arg);
4094 for (; t; t = TREE_OPERAND (t, 0))
4096 if (TREE_CODE (t) == USER_CONV
4097 || TREE_CODE (t) == AMBIG_CONV)
4099 val = convert_like_with_context (t, val, fn, i - is_method);
4100 break;
4102 else if (TREE_CODE (t) == IDENTITY_CONV)
4103 break;
4105 val = convert_for_initialization
4106 (NULL_TREE, type, val, LOOKUP_NORMAL,
4107 "argument", fn, i - is_method);
4109 else
4111 val = TREE_VALUE (arg);
4112 val = convert_like_with_context
4113 (conv, TREE_VALUE (arg), fn, i - is_method);
4116 if (PROMOTE_PROTOTYPES
4117 && INTEGRAL_TYPE_P (type)
4118 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4119 val = default_conversion (val);
4120 converted_args = tree_cons (NULL_TREE, val, converted_args);
4123 /* Default arguments */
4124 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4125 converted_args
4126 = tree_cons (NULL_TREE,
4127 convert_default_arg (TREE_VALUE (parm),
4128 TREE_PURPOSE (parm),
4129 fn, i - is_method),
4130 converted_args);
4132 /* Ellipsis */
4133 for (; arg; arg = TREE_CHAIN (arg))
4134 converted_args
4135 = tree_cons (NULL_TREE,
4136 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4137 converted_args);
4139 converted_args = nreverse (converted_args);
4141 if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
4142 check_function_format (NULL, DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
4143 converted_args);
4145 /* Avoid actually calling copy constructors and copy assignment operators,
4146 if possible. */
4148 if (! flag_elide_constructors)
4149 /* Do things the hard way. */;
4150 else if (TREE_VEC_LENGTH (convs) == 1
4151 && DECL_COPY_CONSTRUCTOR_P (fn))
4153 tree targ;
4154 arg = skip_artificial_parms_for (fn, converted_args);
4155 arg = TREE_VALUE (arg);
4157 /* Pull out the real argument, disregarding const-correctness. */
4158 targ = arg;
4159 while (TREE_CODE (targ) == NOP_EXPR
4160 || TREE_CODE (targ) == NON_LVALUE_EXPR
4161 || TREE_CODE (targ) == CONVERT_EXPR)
4162 targ = TREE_OPERAND (targ, 0);
4163 if (TREE_CODE (targ) == ADDR_EXPR)
4165 targ = TREE_OPERAND (targ, 0);
4166 if (!same_type_ignoring_top_level_qualifiers_p
4167 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4168 targ = NULL_TREE;
4170 else
4171 targ = NULL_TREE;
4173 if (targ)
4174 arg = targ;
4175 else
4176 arg = build_indirect_ref (arg, 0);
4178 /* [class.copy]: the copy constructor is implicitly defined even if
4179 the implementation elided its use. */
4180 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4181 mark_used (fn);
4183 /* If we're creating a temp and we already have one, don't create a
4184 new one. If we're not creating a temp but we get one, use
4185 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4186 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4187 temp or an INIT_EXPR otherwise. */
4188 if (integer_zerop (TREE_VALUE (args)))
4190 if (! real_lvalue_p (arg))
4191 return arg;
4192 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4193 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4195 else if (! real_lvalue_p (arg)
4196 /* Empty classes have padding which can be hidden
4197 inside an (empty) base of the class. This must not
4198 be touched as it might overlay things. When the
4199 gcc core learns about empty classes, we can treat it
4200 like other classes. */
4201 || (!is_empty_class (DECL_CONTEXT (fn))
4202 && TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn))))
4204 tree address;
4205 tree to = stabilize_reference
4206 (build_indirect_ref (TREE_VALUE (args), 0));
4208 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4209 address = build_unary_op (ADDR_EXPR, val, 0);
4210 /* Avoid a warning about this expression, if the address is
4211 never used. */
4212 TREE_USED (address) = 1;
4213 return address;
4216 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4217 && copy_args_p (fn)
4218 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4220 tree to = stabilize_reference
4221 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4223 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4224 if (is_empty_class (TREE_TYPE (to)))
4226 TREE_USED (arg) = 1;
4228 val = build (COMPOUND_EXPR, DECL_CONTEXT (fn), arg, to);
4229 /* Even though the assignment may not actually result in any
4230 code being generated, we do not want to warn about the
4231 assignment having no effect. That would be confusing to
4232 users who may be performing the assignment as part of a
4233 generic algorithm, for example.
4235 Ideally, the notions of having side-effects and of being
4236 useless would be orthogonal. */
4237 TREE_SIDE_EFFECTS (val) = 1;
4239 else
4240 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4241 return val;
4244 mark_used (fn);
4246 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4248 tree t, *p = &TREE_VALUE (converted_args);
4249 tree binfo = get_binfo
4250 (DECL_VIRTUAL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
4251 *p = convert_pointer_to_real (binfo, *p);
4252 if (TREE_SIDE_EFFECTS (*p))
4253 *p = save_expr (*p);
4254 t = build_pointer_type (TREE_TYPE (fn));
4255 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4256 fn = build_java_interface_fn_ref (fn, *p);
4257 else
4258 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4259 TREE_TYPE (fn) = t;
4261 else if (DECL_INLINE (fn))
4262 fn = inline_conversion (fn);
4263 else
4264 fn = build_addr_func (fn);
4266 /* Recognize certain built-in functions so we can make tree-codes
4267 other than CALL_EXPR. We do this when it enables fold-const.c
4268 to do something useful. */
4270 if (TREE_CODE (fn) == ADDR_EXPR
4271 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4272 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4274 tree exp;
4275 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4276 if (exp)
4277 return exp;
4280 /* Some built-in function calls will be evaluated at
4281 compile-time in fold (). */
4282 fn = fold (build_call (fn, converted_args));
4283 if (VOID_TYPE_P (TREE_TYPE (fn)))
4284 return fn;
4285 fn = require_complete_type (fn);
4286 if (fn == error_mark_node)
4287 return error_mark_node;
4288 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4289 fn = build_cplus_new (TREE_TYPE (fn), fn);
4290 return convert_from_reference (fn);
4293 static tree java_iface_lookup_fn;
4295 /* Make an expression which yields the address of the Java interface
4296 method FN. This is achieved by generating a call to libjava's
4297 _Jv_LookupInterfaceMethodIdx(). */
4299 static tree
4300 build_java_interface_fn_ref (fn, instance)
4301 tree fn, instance;
4303 tree lookup_args, lookup_fn, method, idx;
4304 tree klass_ref, iface, iface_ref;
4305 int i;
4307 if (!java_iface_lookup_fn)
4309 tree endlink = build_void_list_node ();
4310 tree t = tree_cons (NULL_TREE, ptr_type_node,
4311 tree_cons (NULL_TREE, ptr_type_node,
4312 tree_cons (NULL_TREE, java_int_type_node,
4313 endlink)));
4314 java_iface_lookup_fn
4315 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4316 build_function_type (ptr_type_node, t),
4317 0, NOT_BUILT_IN, NULL);
4318 ggc_add_tree_root (&java_iface_lookup_fn, 1);
4321 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4322 This is the first entry in the vtable. */
4323 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4324 integer_zero_node);
4326 /* Get the java.lang.Class pointer for the interface being called. */
4327 iface = DECL_CONTEXT (fn);
4328 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4329 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4330 || DECL_CONTEXT (iface_ref) != iface)
4332 cp_error ("Could not find class$ field in java interface type `%T'",
4333 iface);
4334 return error_mark_node;
4336 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4338 /* Determine the itable index of FN. */
4339 i = 1;
4340 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4342 if (!DECL_VIRTUAL_P (method))
4343 continue;
4344 if (fn == method)
4345 break;
4346 i++;
4348 idx = build_int_2 (i, 0);
4350 lookup_args = tree_cons (NULL_TREE, klass_ref,
4351 tree_cons (NULL_TREE, iface_ref,
4352 build_tree_list (NULL_TREE, idx)));
4353 lookup_fn = build1 (ADDR_EXPR,
4354 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4355 java_iface_lookup_fn);
4356 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4359 /* Returns the value to use for the in-charge parameter when making a
4360 call to a function with the indicated NAME. */
4362 tree
4363 in_charge_arg_for_name (name)
4364 tree name;
4366 if (name == base_ctor_identifier
4367 || name == base_dtor_identifier)
4368 return integer_zero_node;
4369 else if (name == complete_ctor_identifier)
4370 return integer_one_node;
4371 else if (name == complete_dtor_identifier)
4372 return integer_two_node;
4373 else if (name == deleting_dtor_identifier)
4374 return integer_three_node;
4376 /* This function should only be called with one of the names listed
4377 above. */
4378 my_friendly_abort (20000411);
4379 return NULL_TREE;
4382 /* FIXME: Is NAME still an IDENTIFIER_NODE, or is it always a list of
4383 functions? */
4385 static tree
4386 build_new_method_call (instance, name, args, basetype_path, flags)
4387 tree instance, name, args, basetype_path;
4388 int flags;
4390 struct z_candidate *candidates = 0, *cand;
4391 tree explicit_targs = NULL_TREE;
4392 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4393 tree pretty_name;
4394 tree user_args;
4395 tree templates = NULL_TREE;
4396 tree call;
4397 int template_only = 0;
4399 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4401 explicit_targs = TREE_OPERAND (name, 1);
4402 name = TREE_OPERAND (name, 0);
4403 if (DECL_P (name))
4404 name = DECL_NAME (name);
4405 else
4407 if (TREE_CODE (name) == COMPONENT_REF)
4408 name = TREE_OPERAND (name, 1);
4409 if (TREE_CODE (name) == OVERLOAD)
4410 name = DECL_NAME (OVL_CURRENT (name));
4413 template_only = 1;
4416 user_args = args;
4417 args = resolve_args (args);
4419 if (args == error_mark_node)
4420 return error_mark_node;
4422 if (instance == NULL_TREE)
4423 basetype = BINFO_TYPE (basetype_path);
4424 else
4426 if (TREE_CODE (instance) == OFFSET_REF)
4427 instance = resolve_offset_ref (instance);
4428 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4429 instance = convert_from_reference (instance);
4430 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4432 /* XXX this should be handled before we get here. */
4433 if (! IS_AGGR_TYPE (basetype))
4435 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4436 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4437 name, instance, basetype);
4439 return error_mark_node;
4443 if (basetype_path == NULL_TREE)
4444 basetype_path = TYPE_BINFO (basetype);
4446 if (instance)
4447 instance_ptr = build_this (instance);
4448 else
4450 instance_ptr = build_int_2 (0, 0);
4451 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4454 /* Callers should explicitly indicate whether they want to construct
4455 the complete object or just the part without virtual bases. */
4456 my_friendly_assert (name != ctor_identifier, 20000408);
4457 /* Similarly for destructors. */
4458 my_friendly_assert (name != dtor_identifier, 20000408);
4460 if (TREE_CODE (name) == IDENTIFIER_NODE
4461 && IDENTIFIER_CTOR_OR_DTOR_P (name))
4463 int constructor_p;
4465 constructor_p = (name == complete_ctor_identifier
4466 || name == base_ctor_identifier);
4467 pretty_name = (constructor_p
4468 ? constructor_name (basetype) : dtor_identifier);
4470 /* If we're a call to a constructor or destructor for a
4471 subobject that uses virtual base classes, then we need to
4472 pass down a pointer to a VTT for the subobject. */
4473 if ((name == base_ctor_identifier
4474 || name == base_dtor_identifier)
4475 && TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4477 tree vtt;
4478 tree sub_vtt;
4479 tree basebinfo = basetype_path;
4481 /* If the current function is a complete object constructor
4482 or destructor, then we fetch the VTT directly.
4483 Otherwise, we look it up using the VTT we were given. */
4484 vtt = IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type));
4485 vtt = decay_conversion (vtt);
4486 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4487 build (EQ_EXPR, boolean_type_node,
4488 current_in_charge_parm, integer_zero_node),
4489 current_vtt_parm,
4490 vtt);
4491 if (TREE_VIA_VIRTUAL (basebinfo))
4492 basebinfo = binfo_for_vbase (basetype, current_class_type);
4493 my_friendly_assert (BINFO_SUBVTT_INDEX (basebinfo), 20010110);
4494 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4495 BINFO_SUBVTT_INDEX (basebinfo));
4497 args = tree_cons (NULL_TREE, sub_vtt, args);
4500 else
4501 pretty_name = name;
4503 if (TREE_CODE (name) == IDENTIFIER_NODE)
4504 fns = lookup_fnfields (basetype_path, name, 1);
4505 else
4506 fns = name;
4508 if (fns == error_mark_node)
4509 return error_mark_node;
4510 if (fns)
4512 tree base;
4513 tree fn;
4514 tree return_type;
4516 if (BASELINK_P (fns))
4518 base = BINFO_TYPE (BASELINK_SUBOBJECT (fns));
4519 fn = BASELINK_FUNCTIONS (fns);
4520 return_type = TREE_TYPE (fns);
4522 else
4524 base = basetype;
4525 fn = fns;
4526 return_type = NULL_TREE;
4529 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4530 for (; fn; fn = OVL_NEXT (fn))
4532 tree t = OVL_CURRENT (fn);
4533 tree this_arglist;
4535 /* We can end up here for copy-init of same or base class. */
4536 if ((flags & LOOKUP_ONLYCONVERTING)
4537 && DECL_NONCONVERTING_P (t))
4538 continue;
4540 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4541 this_arglist = mem_args;
4542 else
4543 this_arglist = args;
4545 if (TREE_CODE (t) == TEMPLATE_DECL)
4547 /* A member template. */
4548 templates = tree_cons (NULL_TREE, t, templates);
4549 candidates =
4550 add_template_candidate (candidates, t, base, explicit_targs,
4551 this_arglist,
4552 return_type, flags, DEDUCE_CALL);
4554 else if (! template_only)
4555 candidates = add_function_candidate (candidates, t, base,
4556 this_arglist, flags);
4558 if (candidates)
4559 candidates->basetype_path = basetype_path;
4563 if (! any_viable (candidates))
4565 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4566 if (flags & LOOKUP_SPECULATIVELY)
4567 return NULL_TREE;
4568 if (!COMPLETE_TYPE_P (basetype))
4569 incomplete_type_error (instance_ptr, basetype);
4570 else
4571 cp_error ("no matching function for call to `%T::%D(%A)%V'",
4572 basetype, pretty_name, user_args,
4573 TREE_TYPE (TREE_TYPE (instance_ptr)));
4574 print_z_candidates (candidates);
4575 return error_mark_node;
4577 candidates = splice_viable (candidates);
4578 cand = tourney (candidates);
4580 if (cand == 0)
4582 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4583 user_args);
4584 print_z_candidates (candidates);
4585 return error_mark_node;
4588 if (DECL_PURE_VIRTUAL_P (cand->fn)
4589 && instance == current_class_ref
4590 && (DECL_CONSTRUCTOR_P (current_function_decl)
4591 || DECL_DESTRUCTOR_P (current_function_decl))
4592 && ! (flags & LOOKUP_NONVIRTUAL)
4593 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4594 cp_error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4595 "abstract virtual `%#D' called from constructor"
4596 : "abstract virtual `%#D' called from destructor"),
4597 cand->fn);
4598 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4599 && is_dummy_object (instance_ptr))
4601 cp_error ("cannot call member function `%D' without object", cand->fn);
4602 return error_mark_node;
4605 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4606 && ((instance == current_class_ref && (dtor_label || ctor_label))
4607 || resolves_to_fixed_type_p (instance, 0)))
4608 flags |= LOOKUP_NONVIRTUAL;
4610 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4611 call = build_over_call (cand, mem_args, flags);
4612 else
4614 call = build_over_call (cand, args, flags);
4615 /* Do evaluate the object parameter in a call to a static member
4616 function. */
4617 if (TREE_SIDE_EFFECTS (instance))
4618 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4621 return call;
4624 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4625 subsequence of ICS2. */
4627 static int
4628 is_subseq (ics1, ics2)
4629 tree ics1, ics2;
4631 /* We can assume that a conversion of the same code
4632 between the same types indicates a subsequence since we only get
4633 here if the types we are converting from are the same. */
4635 while (TREE_CODE (ics1) == RVALUE_CONV
4636 || TREE_CODE (ics1) == LVALUE_CONV)
4637 ics1 = TREE_OPERAND (ics1, 0);
4639 while (1)
4641 while (TREE_CODE (ics2) == RVALUE_CONV
4642 || TREE_CODE (ics2) == LVALUE_CONV)
4643 ics2 = TREE_OPERAND (ics2, 0);
4645 if (TREE_CODE (ics2) == USER_CONV
4646 || TREE_CODE (ics2) == AMBIG_CONV
4647 || TREE_CODE (ics2) == IDENTITY_CONV)
4648 /* At this point, ICS1 cannot be a proper subsequence of
4649 ICS2. We can get a USER_CONV when we are comparing the
4650 second standard conversion sequence of two user conversion
4651 sequences. */
4652 return 0;
4654 ics2 = TREE_OPERAND (ics2, 0);
4656 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4657 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4658 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4659 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4660 return 1;
4664 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4665 be any _TYPE nodes. */
4668 is_properly_derived_from (derived, base)
4669 tree derived;
4670 tree base;
4672 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4673 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4674 return 0;
4676 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4677 considers every class derived from itself. */
4678 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4679 && DERIVED_FROM_P (base, derived));
4682 /* We build the ICS for an implicit object parameter as a pointer
4683 conversion sequence. However, such a sequence should be compared
4684 as if it were a reference conversion sequence. If ICS is the
4685 implicit conversion sequence for an implicit object parameter,
4686 modify it accordingly. */
4688 static void
4689 maybe_handle_implicit_object (ics)
4690 tree* ics;
4692 if (ICS_THIS_FLAG (*ics))
4694 /* [over.match.funcs]
4696 For non-static member functions, the type of the
4697 implicit object parameter is "reference to cv X"
4698 where X is the class of which the function is a
4699 member and cv is the cv-qualification on the member
4700 function declaration. */
4701 tree t = *ics;
4702 tree reference_type;
4704 /* The `this' parameter is a pointer to a class type. Make the
4705 implict conversion talk about a reference to that same class
4706 type. */
4707 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4708 reference_type = build_reference_type (reference_type);
4710 if (TREE_CODE (t) == QUAL_CONV)
4711 t = TREE_OPERAND (t, 0);
4712 if (TREE_CODE (t) == PTR_CONV)
4713 t = TREE_OPERAND (t, 0);
4714 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4715 t = direct_reference_binding (reference_type, t);
4716 *ics = t;
4720 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
4721 to the type the reference originally referred to, and return 1.
4722 Otherwise, return 0. */
4724 static int
4725 maybe_handle_ref_bind (ics, target_type)
4726 tree* ics;
4727 tree* target_type;
4729 if (TREE_CODE (*ics) == REF_BIND)
4731 tree old_ics = *ics;
4732 *target_type = TREE_TYPE (TREE_TYPE (*ics));
4733 *ics = TREE_OPERAND (*ics, 0);
4734 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
4735 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
4736 return 1;
4739 return 0;
4742 /* Compare two implicit conversion sequences according to the rules set out in
4743 [over.ics.rank]. Return values:
4745 1: ics1 is better than ics2
4746 -1: ics2 is better than ics1
4747 0: ics1 and ics2 are indistinguishable */
4749 static int
4750 compare_ics (ics1, ics2)
4751 tree ics1, ics2;
4753 tree from_type1;
4754 tree from_type2;
4755 tree to_type1;
4756 tree to_type2;
4757 tree deref_from_type1 = NULL_TREE;
4758 tree deref_from_type2 = NULL_TREE;
4759 tree deref_to_type1 = NULL_TREE;
4760 tree deref_to_type2 = NULL_TREE;
4761 int rank1, rank2;
4763 /* REF_BINDING is non-zero if the result of the conversion sequence
4764 is a reference type. In that case TARGET_TYPE is the
4765 type referred to by the reference. */
4766 int ref_binding1;
4767 int ref_binding2;
4768 tree target_type1;
4769 tree target_type2;
4771 /* Handle implicit object parameters. */
4772 maybe_handle_implicit_object (&ics1);
4773 maybe_handle_implicit_object (&ics2);
4775 /* Handle reference parameters. */
4776 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
4777 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
4779 /* [over.ics.rank]
4781 When comparing the basic forms of implicit conversion sequences (as
4782 defined in _over.best.ics_)
4784 --a standard conversion sequence (_over.ics.scs_) is a better
4785 conversion sequence than a user-defined conversion sequence
4786 or an ellipsis conversion sequence, and
4788 --a user-defined conversion sequence (_over.ics.user_) is a
4789 better conversion sequence than an ellipsis conversion sequence
4790 (_over.ics.ellipsis_). */
4791 rank1 = ICS_RANK (ics1);
4792 rank2 = ICS_RANK (ics2);
4794 if (rank1 > rank2)
4795 return -1;
4796 else if (rank1 < rank2)
4797 return 1;
4799 if (rank1 == BAD_RANK)
4801 /* XXX Isn't this an extension? */
4802 /* Both ICS are bad. We try to make a decision based on what
4803 would have happenned if they'd been good. */
4804 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4805 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4806 return -1;
4807 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4808 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4809 return 1;
4811 /* We couldn't make up our minds; try to figure it out below. */
4814 if (ICS_ELLIPSIS_FLAG (ics1))
4815 /* Both conversions are ellipsis conversions. */
4816 return 0;
4818 /* User-defined conversion sequence U1 is a better conversion sequence
4819 than another user-defined conversion sequence U2 if they contain the
4820 same user-defined conversion operator or constructor and if the sec-
4821 ond standard conversion sequence of U1 is better than the second
4822 standard conversion sequence of U2. */
4824 if (ICS_USER_FLAG (ics1))
4826 tree t1, t2;
4828 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4829 if (TREE_CODE (t1) == AMBIG_CONV)
4830 return 0;
4831 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4832 if (TREE_CODE (t2) == AMBIG_CONV)
4833 return 0;
4835 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4836 return 0;
4838 /* We can just fall through here, after setting up
4839 FROM_TYPE1 and FROM_TYPE2. */
4840 from_type1 = TREE_TYPE (t1);
4841 from_type2 = TREE_TYPE (t2);
4843 else
4845 /* We're dealing with two standard conversion sequences.
4847 [over.ics.rank]
4849 Standard conversion sequence S1 is a better conversion
4850 sequence than standard conversion sequence S2 if
4852 --S1 is a proper subsequence of S2 (comparing the conversion
4853 sequences in the canonical form defined by _over.ics.scs_,
4854 excluding any Lvalue Transformation; the identity
4855 conversion sequence is considered to be a subsequence of
4856 any non-identity conversion sequence */
4858 from_type1 = ics1;
4859 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4860 from_type1 = TREE_OPERAND (from_type1, 0);
4861 from_type1 = TREE_TYPE (from_type1);
4863 from_type2 = ics2;
4864 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4865 from_type2 = TREE_OPERAND (from_type2, 0);
4866 from_type2 = TREE_TYPE (from_type2);
4869 if (same_type_p (from_type1, from_type2))
4871 if (is_subseq (ics1, ics2))
4872 return 1;
4873 if (is_subseq (ics2, ics1))
4874 return -1;
4876 /* Otherwise, one sequence cannot be a subsequence of the other; they
4877 don't start with the same type. This can happen when comparing the
4878 second standard conversion sequence in two user-defined conversion
4879 sequences. */
4881 /* [over.ics.rank]
4883 Or, if not that,
4885 --the rank of S1 is better than the rank of S2 (by the rules
4886 defined below):
4888 Standard conversion sequences are ordered by their ranks: an Exact
4889 Match is a better conversion than a Promotion, which is a better
4890 conversion than a Conversion.
4892 Two conversion sequences with the same rank are indistinguishable
4893 unless one of the following rules applies:
4895 --A conversion that is not a conversion of a pointer, or pointer
4896 to member, to bool is better than another conversion that is such
4897 a conversion.
4899 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4900 so that we do not have to check it explicitly. */
4901 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4902 return 1;
4903 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4904 return -1;
4906 to_type1 = TREE_TYPE (ics1);
4907 to_type2 = TREE_TYPE (ics2);
4909 if (TYPE_PTR_P (from_type1)
4910 && TYPE_PTR_P (from_type2)
4911 && TYPE_PTR_P (to_type1)
4912 && TYPE_PTR_P (to_type2))
4914 deref_from_type1 = TREE_TYPE (from_type1);
4915 deref_from_type2 = TREE_TYPE (from_type2);
4916 deref_to_type1 = TREE_TYPE (to_type1);
4917 deref_to_type2 = TREE_TYPE (to_type2);
4919 /* The rules for pointers to members A::* are just like the rules
4920 for pointers A*, except opposite: if B is derived from A then
4921 A::* converts to B::*, not vice versa. For that reason, we
4922 switch the from_ and to_ variables here. */
4923 else if (TYPE_PTRMEM_P (from_type1)
4924 && TYPE_PTRMEM_P (from_type2)
4925 && TYPE_PTRMEM_P (to_type1)
4926 && TYPE_PTRMEM_P (to_type2))
4928 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4929 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4930 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4931 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4933 else if (TYPE_PTRMEMFUNC_P (from_type1)
4934 && TYPE_PTRMEMFUNC_P (from_type2)
4935 && TYPE_PTRMEMFUNC_P (to_type1)
4936 && TYPE_PTRMEMFUNC_P (to_type2))
4938 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4939 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4940 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4941 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4944 if (deref_from_type1 != NULL_TREE
4945 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4946 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4948 /* This was one of the pointer or pointer-like conversions.
4950 [over.ics.rank]
4952 --If class B is derived directly or indirectly from class A,
4953 conversion of B* to A* is better than conversion of B* to
4954 void*, and conversion of A* to void* is better than
4955 conversion of B* to void*. */
4956 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4957 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4959 if (is_properly_derived_from (deref_from_type1,
4960 deref_from_type2))
4961 return -1;
4962 else if (is_properly_derived_from (deref_from_type2,
4963 deref_from_type1))
4964 return 1;
4966 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4967 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4969 if (same_type_p (deref_from_type1, deref_from_type2))
4971 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4973 if (is_properly_derived_from (deref_from_type1,
4974 deref_to_type1))
4975 return 1;
4977 /* We know that DEREF_TO_TYPE1 is `void' here. */
4978 else if (is_properly_derived_from (deref_from_type1,
4979 deref_to_type2))
4980 return -1;
4983 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4984 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4986 /* [over.ics.rank]
4988 --If class B is derived directly or indirectly from class A
4989 and class C is derived directly or indirectly from B,
4991 --conversion of C* to B* is better than conversion of C* to
4992 A*,
4994 --conversion of B* to A* is better than conversion of C* to
4995 A* */
4996 if (same_type_p (deref_from_type1, deref_from_type2))
4998 if (is_properly_derived_from (deref_to_type1,
4999 deref_to_type2))
5000 return 1;
5001 else if (is_properly_derived_from (deref_to_type2,
5002 deref_to_type1))
5003 return -1;
5005 else if (same_type_p (deref_to_type1, deref_to_type2))
5007 if (is_properly_derived_from (deref_from_type2,
5008 deref_from_type1))
5009 return 1;
5010 else if (is_properly_derived_from (deref_from_type1,
5011 deref_from_type2))
5012 return -1;
5016 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
5017 && same_type_p (from_type1, from_type2))
5019 /* [over.ics.rank]
5021 --binding of an expression of type C to a reference of type
5022 B& is better than binding an expression of type C to a
5023 reference of type A&
5025 --conversion of C to B is better than conversion of C to A, */
5026 if (is_properly_derived_from (from_type1, to_type1)
5027 && is_properly_derived_from (from_type1, to_type2))
5029 if (is_properly_derived_from (to_type1, to_type2))
5030 return 1;
5031 else if (is_properly_derived_from (to_type2, to_type1))
5032 return -1;
5035 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
5036 && same_type_p (to_type1, to_type2))
5038 /* [over.ics.rank]
5040 --binding of an expression of type B to a reference of type
5041 A& is better than binding an expression of type C to a
5042 reference of type A&,
5044 --onversion of B to A is better than conversion of C to A */
5045 if (is_properly_derived_from (from_type1, to_type1)
5046 && is_properly_derived_from (from_type2, to_type1))
5048 if (is_properly_derived_from (from_type2, from_type1))
5049 return 1;
5050 else if (is_properly_derived_from (from_type1, from_type2))
5051 return -1;
5055 /* [over.ics.rank]
5057 --S1 and S2 differ only in their qualification conversion and yield
5058 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5059 qualification signature of type T1 is a proper subset of the cv-
5060 qualification signature of type T2 */
5061 if (TREE_CODE (ics1) == QUAL_CONV
5062 && TREE_CODE (ics2) == QUAL_CONV
5063 && same_type_p (from_type1, from_type2))
5064 return comp_cv_qual_signature (to_type1, to_type2);
5066 /* [over.ics.rank]
5068 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5069 types to which the references refer are the same type except for
5070 top-level cv-qualifiers, and the type to which the reference
5071 initialized by S2 refers is more cv-qualified than the type to
5072 which the reference initialized by S1 refers */
5074 if (ref_binding1 && ref_binding2
5075 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5076 return comp_cv_qualification (target_type2, target_type1);
5078 /* Neither conversion sequence is better than the other. */
5079 return 0;
5082 /* The source type for this standard conversion sequence. */
5084 static tree
5085 source_type (t)
5086 tree t;
5088 for (;; t = TREE_OPERAND (t, 0))
5090 if (TREE_CODE (t) == USER_CONV
5091 || TREE_CODE (t) == AMBIG_CONV
5092 || TREE_CODE (t) == IDENTITY_CONV)
5093 return TREE_TYPE (t);
5095 my_friendly_abort (1823);
5098 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5099 a pointer to LOSER and re-running joust to produce the warning if WINNER
5100 is actually used. */
5102 static void
5103 add_warning (winner, loser)
5104 struct z_candidate *winner, *loser;
5106 winner->warnings = tree_cons (NULL_TREE,
5107 build_ptr_wrapper (loser),
5108 winner->warnings);
5111 /* Returns true iff functions are equivalent. Equivalent functions are
5112 not '==' only if one is a function-local extern function or if
5113 both are extern "C". */
5115 static inline int
5116 equal_functions (fn1, fn2)
5117 tree fn1;
5118 tree fn2;
5120 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5121 || DECL_EXTERN_C_FUNCTION_P (fn1))
5122 return decls_match (fn1, fn2);
5123 return fn1 == fn2;
5126 /* Compare two candidates for overloading as described in
5127 [over.match.best]. Return values:
5129 1: cand1 is better than cand2
5130 -1: cand2 is better than cand1
5131 0: cand1 and cand2 are indistinguishable */
5133 static int
5134 joust (cand1, cand2, warn)
5135 struct z_candidate *cand1, *cand2;
5136 int warn;
5138 int winner = 0;
5139 int i, off1 = 0, off2 = 0, len;
5141 /* Candidates that involve bad conversions are always worse than those
5142 that don't. */
5143 if (cand1->viable > cand2->viable)
5144 return 1;
5145 if (cand1->viable < cand2->viable)
5146 return -1;
5148 /* If we have two pseudo-candidates for conversions to the same type,
5149 or two candidates for the same function, arbitrarily pick one. */
5150 if (cand1->fn == cand2->fn
5151 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5152 return 1;
5154 /* a viable function F1
5155 is defined to be a better function than another viable function F2 if
5156 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5157 ICSi(F2), and then */
5159 /* for some argument j, ICSj(F1) is a better conversion sequence than
5160 ICSj(F2) */
5162 /* For comparing static and non-static member functions, we ignore
5163 the implicit object parameter of the non-static function. The
5164 standard says to pretend that the static function has an object
5165 parm, but that won't work with operator overloading. */
5166 len = TREE_VEC_LENGTH (cand1->convs);
5167 if (len != TREE_VEC_LENGTH (cand2->convs))
5169 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5170 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5171 off2 = 1;
5172 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5173 && DECL_STATIC_FUNCTION_P (cand2->fn))
5175 off1 = 1;
5176 --len;
5178 else
5179 my_friendly_abort (42);
5182 for (i = 0; i < len; ++i)
5184 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5185 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5186 int comp = compare_ics (t1, t2);
5188 if (comp != 0)
5190 if (warn_sign_promo
5191 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5192 && TREE_CODE (t1) == STD_CONV
5193 && TREE_CODE (t2) == STD_CONV
5194 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5195 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5196 && (TYPE_PRECISION (TREE_TYPE (t1))
5197 == TYPE_PRECISION (TREE_TYPE (t2)))
5198 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5199 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5200 == ENUMERAL_TYPE)))
5202 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5203 tree type1, type2;
5204 struct z_candidate *w, *l;
5205 if (comp > 0)
5206 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5207 w = cand1, l = cand2;
5208 else
5209 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5210 w = cand2, l = cand1;
5212 if (warn)
5214 cp_warning ("passing `%T' chooses `%T' over `%T'",
5215 type, type1, type2);
5216 cp_warning (" in call to `%D'", w->fn);
5218 else
5219 add_warning (w, l);
5222 if (winner && comp != winner)
5224 winner = 0;
5225 goto tweak;
5227 winner = comp;
5231 /* warn about confusing overload resolution for user-defined conversions,
5232 either between a constructor and a conversion op, or between two
5233 conversion ops. */
5234 if (winner && cand1->second_conv
5235 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5236 != DECL_CONSTRUCTOR_P (cand2->fn))
5237 /* Don't warn if the two conv ops convert to the same type... */
5238 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5239 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5240 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5242 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5243 if (comp != winner)
5245 struct z_candidate *w, *l;
5246 tree convn;
5247 if (winner == 1)
5248 w = cand1, l = cand2;
5249 else
5250 w = cand2, l = cand1;
5251 if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5252 && ! DECL_CONSTRUCTOR_P (cand1->fn)
5253 && ! DECL_CONSTRUCTOR_P (cand2->fn)
5254 && (convn = standard_conversion
5255 (TREE_TYPE (TREE_TYPE (l->fn)),
5256 TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5257 && TREE_CODE (convn) == QUAL_CONV)
5258 /* Don't complain about `operator char *()' beating
5259 `operator const char *() const'. */;
5260 else if (warn)
5262 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5263 if (! DECL_CONSTRUCTOR_P (w->fn))
5264 source = TREE_TYPE (source);
5265 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
5266 cp_warning (" for conversion from `%T' to `%T'",
5267 source, TREE_TYPE (w->second_conv));
5268 cp_warning (" because conversion sequence for the argument is better");
5270 else
5271 add_warning (w, l);
5275 if (winner)
5276 return winner;
5278 /* or, if not that,
5279 F1 is a non-template function and F2 is a template function
5280 specialization. */
5282 if (! cand1->template && cand2->template)
5283 return 1;
5284 else if (cand1->template && ! cand2->template)
5285 return -1;
5287 /* or, if not that,
5288 F1 and F2 are template functions and the function template for F1 is
5289 more specialized than the template for F2 according to the partial
5290 ordering rules. */
5292 if (cand1->template && cand2->template)
5294 winner = more_specialized
5295 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5296 DEDUCE_ORDER,
5297 /* Tell the deduction code how many real function arguments
5298 we saw, not counting the implicit 'this' argument. But,
5299 add_function_candidate() suppresses the "this" argument
5300 for constructors.
5302 [temp.func.order]: The presence of unused ellipsis and default
5303 arguments has no effect on the partial ordering of function
5304 templates. */
5305 TREE_VEC_LENGTH (cand1->convs)
5306 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5307 - DECL_CONSTRUCTOR_P (cand1->fn)));
5308 /* HERE */
5309 if (winner)
5310 return winner;
5313 /* or, if not that,
5314 the context is an initialization by user-defined conversion (see
5315 _dcl.init_ and _over.match.user_) and the standard conversion
5316 sequence from the return type of F1 to the destination type (i.e.,
5317 the type of the entity being initialized) is a better conversion
5318 sequence than the standard conversion sequence from the return type
5319 of F2 to the destination type. */
5321 if (cand1->second_conv)
5323 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5324 if (winner)
5325 return winner;
5328 /* Check whether we can discard a builtin candidate, either because we
5329 have two identical ones or matching builtin and non-builtin candidates.
5331 (Pedantically in the latter case the builtin which matched the user
5332 function should not be added to the overload set, but we spot it here.
5334 [over.match.oper]
5335 ... the builtin candidates include ...
5336 - do not have the same parameter type list as any non-template
5337 non-member candidate. */
5339 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5340 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5342 for (i = 0; i < len; ++i)
5343 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5344 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5345 break;
5346 if (i == TREE_VEC_LENGTH (cand1->convs))
5348 if (cand1->fn == cand2->fn)
5349 /* Two built-in candidates; arbitrarily pick one. */
5350 return 1;
5351 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5352 /* cand1 is built-in; prefer cand2. */
5353 return -1;
5354 else
5355 /* cand2 is built-in; prefer cand1. */
5356 return 1;
5359 /* Kludge around broken overloading rules whereby
5360 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
5361 that takes references and another that takes values. */
5362 if (cand1->fn == cand2->fn
5363 && cand1->fn == ansi_opname (COND_EXPR))
5365 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
5366 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
5367 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
5368 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
5370 if (same_type_p (t1, t2))
5372 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
5373 return 1;
5374 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
5375 return -1;
5380 /* If the two functions are the same (this can happen with declarations
5381 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5382 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5383 && equal_functions (cand1->fn, cand2->fn))
5384 return 1;
5386 tweak:
5388 /* Extension: If the worst conversion for one candidate is worse than the
5389 worst conversion for the other, take the first. */
5390 if (!pedantic)
5392 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5393 struct z_candidate *w = 0, *l = 0;
5395 for (i = 0; i < len; ++i)
5397 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5398 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5399 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5400 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5402 if (rank1 < rank2)
5403 winner = 1, w = cand1, l = cand2;
5404 if (rank1 > rank2)
5405 winner = -1, w = cand2, l = cand1;
5406 if (winner)
5408 if (warn)
5410 cp_pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5411 cp_pedwarn (
5412 " because worst conversion for the former is better than worst conversion for the latter");
5414 else
5415 add_warning (w, l);
5416 return winner;
5420 my_friendly_assert (!winner, 20010121);
5421 return 0;
5424 /* Given a list of candidates for overloading, find the best one, if any.
5425 This algorithm has a worst case of O(2n) (winner is last), and a best
5426 case of O(n/2) (totally ambiguous); much better than a sorting
5427 algorithm. */
5429 static struct z_candidate *
5430 tourney (candidates)
5431 struct z_candidate *candidates;
5433 struct z_candidate *champ = candidates, *challenger;
5434 int fate;
5435 int champ_compared_to_predecessor = 0;
5437 /* Walk through the list once, comparing each current champ to the next
5438 candidate, knocking out a candidate or two with each comparison. */
5440 for (challenger = champ->next; challenger; )
5442 fate = joust (champ, challenger, 0);
5443 if (fate == 1)
5444 challenger = challenger->next;
5445 else
5447 if (fate == 0)
5449 champ = challenger->next;
5450 if (champ == 0)
5451 return 0;
5452 champ_compared_to_predecessor = 0;
5454 else
5456 champ = challenger;
5457 champ_compared_to_predecessor = 1;
5460 challenger = champ->next;
5464 /* Make sure the champ is better than all the candidates it hasn't yet
5465 been compared to. */
5467 for (challenger = candidates;
5468 challenger != champ
5469 && !(champ_compared_to_predecessor && challenger->next == champ);
5470 challenger = challenger->next)
5472 fate = joust (champ, challenger, 0);
5473 if (fate != 1)
5474 return 0;
5477 return champ;
5480 /* Returns non-zero if things of type FROM can be converted to TO. */
5483 can_convert (to, from)
5484 tree to, from;
5486 return can_convert_arg (to, from, NULL_TREE);
5489 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5492 can_convert_arg (to, from, arg)
5493 tree to, from, arg;
5495 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5496 return (t && ! ICS_BAD_FLAG (t));
5499 /* Convert EXPR to TYPE. Return the converted expression. */
5501 tree
5502 perform_implicit_conversion (type, expr)
5503 tree type;
5504 tree expr;
5506 tree conv;
5508 if (expr == error_mark_node)
5509 return error_mark_node;
5510 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5511 LOOKUP_NORMAL);
5512 if (!conv || ICS_BAD_FLAG (conv))
5514 cp_error ("could not convert `%E' to `%T'", expr, type);
5515 return error_mark_node;
5518 return convert_like (conv, expr);
5521 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5522 initializing a variable of that TYPE. Return the converted
5523 expression. */
5525 tree
5526 initialize_reference (type, expr)
5527 tree type;
5528 tree expr;
5530 tree conv;
5532 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5533 if (!conv || ICS_BAD_FLAG (conv))
5535 cp_error ("could not convert `%E' to `%T'", expr, type);
5536 return error_mark_node;
5539 return convert_like (conv, expr);