PR c++/3637
[official-gcc.git] / gcc / cp / call.c
blob4d06dc5dcd6d984093d98204c675d2c7c3953525
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 tree maybe_handle_ref_bind PARAMS ((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, DECL_NAME (TYPE_VFIELD (type)), NULL_TREE, 0);
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_1 (instance, field, 0);
154 if (instance == error_mark_node)
155 return error_mark_node;
157 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
158 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
159 instance, parms, NULL_TREE);
160 else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
161 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
162 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
163 == FUNCTION_TYPE)))
164 return build_function_call (instance, parms);
167 return NULL_TREE;
170 /* Returns nonzero iff the destructor name specified in NAME
171 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
172 forms... */
175 check_dtor_name (basetype, name)
176 tree basetype, name;
178 name = TREE_OPERAND (name, 0);
180 /* Just accept something we've already complained about. */
181 if (name == error_mark_node)
182 return 1;
184 if (TREE_CODE (name) == TYPE_DECL)
185 name = TREE_TYPE (name);
186 else if (TYPE_P (name))
187 /* OK */;
188 else if (TREE_CODE (name) == IDENTIFIER_NODE)
190 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
191 || (TREE_CODE (basetype) == ENUMERAL_TYPE
192 && name == TYPE_IDENTIFIER (basetype)))
193 name = basetype;
194 else
195 name = get_type_value (name);
197 /* In the case of:
199 template <class T> struct S { ~S(); };
200 int i;
201 i.~S();
203 NAME will be a class template. */
204 else if (DECL_CLASS_TEMPLATE_P (name))
205 return 0;
206 else
207 my_friendly_abort (980605);
209 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
210 return 1;
211 return 0;
214 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
215 This is how virtual function calls are avoided. */
217 tree
218 build_scoped_method_call (exp, basetype, name, parms)
219 tree exp, basetype, name, parms;
221 /* Because this syntactic form does not allow
222 a pointer to a base class to be `stolen',
223 we need not protect the derived->base conversion
224 that happens here.
226 @@ But we do have to check access privileges later. */
227 tree binfo, decl;
228 tree type = TREE_TYPE (exp);
230 if (type == error_mark_node
231 || basetype == error_mark_node)
232 return error_mark_node;
234 if (processing_template_decl)
236 if (TREE_CODE (name) == BIT_NOT_EXPR
237 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
239 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
240 if (type)
241 name = build_min_nt (BIT_NOT_EXPR, type);
243 name = build_min_nt (SCOPE_REF, basetype, name);
244 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
247 if (TREE_CODE (type) == REFERENCE_TYPE)
248 type = TREE_TYPE (type);
250 if (TREE_CODE (basetype) == TREE_VEC)
252 binfo = basetype;
253 basetype = BINFO_TYPE (binfo);
255 else
256 binfo = NULL_TREE;
258 /* Check the destructor call syntax. */
259 if (TREE_CODE (name) == BIT_NOT_EXPR)
261 /* We can get here if someone writes their destructor call like
262 `obj.NS::~T()'; this isn't really a scoped method call, so hand
263 it off. */
264 if (TREE_CODE (basetype) == NAMESPACE_DECL)
265 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
267 if (! check_dtor_name (basetype, name))
268 cp_error ("qualified type `%T' does not match destructor name `~%T'",
269 basetype, TREE_OPERAND (name, 0));
271 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
272 that explicit ~int is caught in the parser; this deals with typedefs
273 and template parms. */
274 if (! IS_AGGR_TYPE (basetype))
276 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
277 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
278 exp, basetype, type);
280 return cp_convert (void_type_node, exp);
284 if (TREE_CODE (basetype) == NAMESPACE_DECL)
286 cp_error ("`%D' is a namespace", basetype);
287 return error_mark_node;
289 if (! is_aggr_type (basetype, 1))
290 return error_mark_node;
292 if (! IS_AGGR_TYPE (type))
294 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
295 exp, type);
296 return error_mark_node;
299 if (! binfo)
301 binfo = get_binfo (basetype, type, 1);
302 if (binfo == error_mark_node)
303 return error_mark_node;
304 if (! binfo)
305 error_not_base_type (basetype, type);
308 if (binfo)
310 if (TREE_CODE (exp) == INDIRECT_REF)
311 decl = build_indirect_ref
312 (convert_pointer_to_real
313 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL);
314 else
315 decl = build_scoped_ref (exp, basetype);
317 /* Call to a destructor. */
318 if (TREE_CODE (name) == BIT_NOT_EXPR)
320 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
321 return cp_convert (void_type_node, exp);
323 return build_delete (TREE_TYPE (decl), decl,
324 sfk_complete_destructor,
325 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
329 /* Call to a method. */
330 return build_method_call (decl, name, parms, binfo,
331 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
333 return error_mark_node;
336 /* We want the address of a function or method. We avoid creating a
337 pointer-to-member function. */
339 tree
340 build_addr_func (function)
341 tree function;
343 tree type = TREE_TYPE (function);
345 /* We have to do these by hand to avoid real pointer to member
346 functions. */
347 if (TREE_CODE (type) == METHOD_TYPE)
349 tree addr;
351 type = build_pointer_type (type);
353 if (mark_addressable (function) == 0)
354 return error_mark_node;
356 addr = build1 (ADDR_EXPR, type, function);
358 /* Address of a static or external variable or function counts
359 as a constant */
360 if (staticp (function))
361 TREE_CONSTANT (addr) = 1;
363 function = addr;
365 else
366 function = default_conversion (function);
368 return function;
371 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
372 POINTER_TYPE to those. Note, pointer to member function types
373 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
375 tree
376 build_call (function, parms)
377 tree function, parms;
379 int is_constructor = 0;
380 int nothrow;
381 tree tmp;
382 tree decl;
383 tree result_type;
385 function = build_addr_func (function);
387 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
389 sorry ("unable to call pointer to member function here");
390 return error_mark_node;
393 result_type = TREE_TYPE (TREE_TYPE (TREE_TYPE (function)));
395 if (TREE_CODE (function) == ADDR_EXPR
396 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
397 decl = TREE_OPERAND (function, 0);
398 else
399 decl = NULL_TREE;
401 /* We check both the decl and the type; a function may be known not to
402 throw without being declared throw(). */
403 nothrow = ((decl && TREE_NOTHROW (decl))
404 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
406 if (decl && DECL_CONSTRUCTOR_P (decl))
407 is_constructor = 1;
409 if (decl && ! TREE_USED (decl))
411 /* We invoke build_call directly for several library functions.
412 These may have been declared normally if we're building libgcc,
413 so we can't just check DECL_ARTIFICIAL. */
414 if (DECL_ARTIFICIAL (decl)
415 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
416 mark_used (decl);
417 else
418 my_friendly_abort (990125);
421 /* Don't pass empty class objects by value. This is useful
422 for tags in STL, which are used to control overload resolution.
423 We don't need to handle other cases of copying empty classes. */
424 if (! decl || ! DECL_BUILT_IN (decl))
425 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
426 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
427 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
429 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
430 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
431 TREE_VALUE (tmp), t);
434 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
435 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
436 TREE_TYPE (function) = result_type;
437 TREE_SIDE_EFFECTS (function) = 1;
438 TREE_NOTHROW (function) = nothrow;
440 return function;
443 /* Build something of the form ptr->method (args)
444 or object.method (args). This can also build
445 calls to constructors, and find friends.
447 Member functions always take their class variable
448 as a pointer.
450 INSTANCE is a class instance.
452 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
454 PARMS help to figure out what that NAME really refers to.
456 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
457 down to the real instance type to use for access checking. We need this
458 information to get protected accesses correct. This parameter is used
459 by build_member_call.
461 FLAGS is the logical disjunction of zero or more LOOKUP_
462 flags. See cp-tree.h for more info.
464 If this is all OK, calls build_function_call with the resolved
465 member function.
467 This function must also handle being called to perform
468 initialization, promotion/coercion of arguments, and
469 instantiation of default parameters.
471 Note that NAME may refer to an instance variable name. If
472 `operator()()' is defined for the type of that field, then we return
473 that result. */
475 #ifdef GATHER_STATISTICS
476 extern int n_build_method_call;
477 #endif
479 tree
480 build_method_call (instance, name, parms, basetype_path, flags)
481 tree instance, name, parms, basetype_path;
482 int flags;
484 tree basetype, instance_ptr;
486 #ifdef GATHER_STATISTICS
487 n_build_method_call++;
488 #endif
490 if (instance == error_mark_node
491 || name == error_mark_node
492 || parms == error_mark_node
493 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
494 return error_mark_node;
496 if (processing_template_decl)
498 /* We need to process template parm names here so that tsubst catches
499 them properly. Other type names can wait. */
500 if (TREE_CODE (name) == BIT_NOT_EXPR)
502 tree type = NULL_TREE;
504 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
505 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
506 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
507 type = TREE_TYPE (TREE_OPERAND (name, 0));
509 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
510 name = build_min_nt (BIT_NOT_EXPR, type);
513 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
516 if (TREE_CODE (name) == BIT_NOT_EXPR)
518 if (parms)
519 error ("destructors take no parameters");
520 basetype = TREE_TYPE (instance);
521 if (TREE_CODE (basetype) == REFERENCE_TYPE)
522 basetype = TREE_TYPE (basetype);
524 if (! check_dtor_name (basetype, name))
525 cp_error
526 ("destructor name `~%T' does not match type `%T' of expression",
527 TREE_OPERAND (name, 0), basetype);
529 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
530 return cp_convert (void_type_node, instance);
531 instance = default_conversion (instance);
532 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
533 return build_delete (build_pointer_type (basetype),
534 instance_ptr, sfk_complete_destructor,
535 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
538 return build_new_method_call (instance, name, parms, basetype_path, flags);
541 /* New overloading code. */
543 struct z_candidate {
544 tree fn;
545 tree convs;
546 tree second_conv;
547 int viable;
548 tree basetype_path;
549 tree template;
550 tree warnings;
551 struct z_candidate *next;
554 #define IDENTITY_RANK 0
555 #define EXACT_RANK 1
556 #define PROMO_RANK 2
557 #define STD_RANK 3
558 #define PBOOL_RANK 4
559 #define USER_RANK 5
560 #define ELLIPSIS_RANK 6
561 #define BAD_RANK 7
563 #define ICS_RANK(NODE) \
564 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
565 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
566 : ICS_USER_FLAG (NODE) ? USER_RANK \
567 : ICS_STD_RANK (NODE))
569 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
571 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
572 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
573 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
574 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
576 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
577 should be created to hold the result of the conversion. */
578 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
580 #define USER_CONV_CAND(NODE) \
581 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
582 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
585 null_ptr_cst_p (t)
586 tree t;
588 /* [conv.ptr]
590 A null pointer constant is an integral constant expression
591 (_expr.const_) rvalue of integer type that evaluates to zero. */
592 if (t == null_node
593 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
594 return 1;
595 return 0;
599 /* Returns non-zero if PARMLIST consists of only default parms and/or
600 ellipsis. */
603 sufficient_parms_p (parmlist)
604 tree parmlist;
606 for (; parmlist && parmlist != void_list_node;
607 parmlist = TREE_CHAIN (parmlist))
608 if (!TREE_PURPOSE (parmlist))
609 return 0;
610 return 1;
613 static tree
614 build_conv (code, type, from)
615 enum tree_code code;
616 tree type, from;
618 tree t;
619 int rank = ICS_STD_RANK (from);
621 /* We can't use buildl1 here because CODE could be USER_CONV, which
622 takes two arguments. In that case, the caller is responsible for
623 filling in the second argument. */
624 t = make_node (code);
625 TREE_TYPE (t) = type;
626 TREE_OPERAND (t, 0) = from;
628 switch (code)
630 case PTR_CONV:
631 case PMEM_CONV:
632 case BASE_CONV:
633 case STD_CONV:
634 if (rank < STD_RANK)
635 rank = STD_RANK;
636 break;
638 case QUAL_CONV:
639 if (rank < EXACT_RANK)
640 rank = EXACT_RANK;
642 default:
643 break;
645 ICS_STD_RANK (t) = rank;
646 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
647 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
648 return t;
651 /* If T is a REFERENCE_TYPE return the type to which T refers.
652 Otherwise, return T itself. */
654 static tree
655 non_reference (t)
656 tree t;
658 if (TREE_CODE (t) == REFERENCE_TYPE)
659 t = TREE_TYPE (t);
660 return t;
663 tree
664 strip_top_quals (t)
665 tree t;
667 if (TREE_CODE (t) == ARRAY_TYPE)
668 return t;
669 return TYPE_MAIN_VARIANT (t);
672 /* Returns the standard conversion path (see [conv]) from type FROM to type
673 TO, if any. For proper handling of null pointer constants, you must
674 also pass the expression EXPR to convert from. */
676 static tree
677 standard_conversion (to, from, expr)
678 tree to, from, expr;
680 enum tree_code fcode, tcode;
681 tree conv;
682 int fromref = 0;
684 if (TREE_CODE (to) == REFERENCE_TYPE)
685 to = TREE_TYPE (to);
686 if (TREE_CODE (from) == REFERENCE_TYPE)
688 fromref = 1;
689 from = TREE_TYPE (from);
691 to = strip_top_quals (to);
692 from = strip_top_quals (from);
694 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
695 && expr && type_unknown_p (expr))
697 expr = instantiate_type (to, expr, itf_none);
698 if (expr == error_mark_node)
699 return NULL_TREE;
700 from = TREE_TYPE (expr);
703 fcode = TREE_CODE (from);
704 tcode = TREE_CODE (to);
706 conv = build1 (IDENTITY_CONV, from, expr);
708 if (fcode == FUNCTION_TYPE)
710 from = build_pointer_type (from);
711 fcode = TREE_CODE (from);
712 conv = build_conv (LVALUE_CONV, from, conv);
714 else if (fcode == ARRAY_TYPE)
716 from = build_pointer_type (TREE_TYPE (from));
717 fcode = TREE_CODE (from);
718 conv = build_conv (LVALUE_CONV, from, conv);
720 else if (fromref || (expr && lvalue_p (expr)))
721 conv = build_conv (RVALUE_CONV, from, conv);
723 /* Allow conversion between `__complex__' data types */
724 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
726 /* The standard conversion sequence to convert FROM to TO is
727 the standard conversion sequence to perform componentwise
728 conversion. */
729 tree part_conv = standard_conversion
730 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
732 if (part_conv)
734 conv = build_conv (TREE_CODE (part_conv), to, conv);
735 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
737 else
738 conv = NULL_TREE;
740 return conv;
743 if (same_type_p (from, to))
744 return conv;
746 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
747 && expr && null_ptr_cst_p (expr))
749 conv = build_conv (STD_CONV, to, conv);
751 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
752 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
754 /* For backwards brain damage compatibility, allow interconversion of
755 pointers and integers with a pedwarn. */
756 conv = build_conv (STD_CONV, to, conv);
757 ICS_BAD_FLAG (conv) = 1;
759 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE
760 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
762 /* For backwards brain damage compatibility, allow interconversion of
763 enums and integers with a pedwarn. */
764 conv = build_conv (STD_CONV, to, conv);
765 ICS_BAD_FLAG (conv) = 1;
767 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
769 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
770 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
772 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
773 TREE_TYPE (to)))
775 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
776 && ufcode != FUNCTION_TYPE)
778 from = build_pointer_type
779 (cp_build_qualified_type (void_type_node,
780 CP_TYPE_QUALS (TREE_TYPE (from))));
781 conv = build_conv (PTR_CONV, from, conv);
783 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
785 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
786 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
787 tree binfo = get_binfo (fbase, tbase, 1);
789 if (binfo && !binfo_from_vbase (binfo)
790 && (same_type_ignoring_top_level_qualifiers_p
791 (TREE_TYPE (TREE_TYPE (from)),
792 TREE_TYPE (TREE_TYPE (to)))))
794 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
795 from = build_pointer_type (from);
796 conv = build_conv (PMEM_CONV, from, conv);
799 else if (IS_AGGR_TYPE (TREE_TYPE (from))
800 && IS_AGGR_TYPE (TREE_TYPE (to)))
802 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
804 from =
805 cp_build_qualified_type (TREE_TYPE (to),
806 CP_TYPE_QUALS (TREE_TYPE (from)));
807 from = build_pointer_type (from);
808 conv = build_conv (PTR_CONV, from, conv);
812 if (same_type_p (from, to))
813 /* OK */;
814 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
815 conv = build_conv (QUAL_CONV, to, conv);
816 else if (expr && string_conv_p (to, expr, 0))
817 /* converting from string constant to char *. */
818 conv = build_conv (QUAL_CONV, to, conv);
819 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
821 conv = build_conv (PTR_CONV, to, conv);
822 ICS_BAD_FLAG (conv) = 1;
824 else
825 return 0;
827 from = to;
829 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
831 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
832 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
833 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
834 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
835 tree binfo = get_binfo (fbase, tbase, 1);
837 if (!binfo || binfo_from_vbase (binfo)
838 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
839 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
840 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
841 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
842 return 0;
844 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
845 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
846 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
847 from = build_ptrmemfunc_type (build_pointer_type (from));
848 conv = build_conv (PMEM_CONV, from, conv);
850 else if (tcode == BOOLEAN_TYPE)
852 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
853 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
854 return 0;
856 conv = build_conv (STD_CONV, to, conv);
857 if (fcode == POINTER_TYPE
858 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
859 ICS_STD_RANK (conv) = PBOOL_RANK;
861 /* We don't check for ENUMERAL_TYPE here because there are no standard
862 conversions to enum type. */
863 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
864 || tcode == REAL_TYPE)
866 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
867 return 0;
868 conv = build_conv (STD_CONV, to, conv);
870 /* Give this a better rank if it's a promotion. */
871 if (to == type_promotes_to (from)
872 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
873 ICS_STD_RANK (conv) = PROMO_RANK;
875 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
876 && is_properly_derived_from (from, to))
878 if (TREE_CODE (conv) == RVALUE_CONV)
879 conv = TREE_OPERAND (conv, 0);
880 conv = build_conv (BASE_CONV, to, conv);
881 /* The derived-to-base conversion indicates the initialization
882 of a parameter with base type from an object of a derived
883 type. A temporary object is created to hold the result of
884 the conversion. */
885 NEED_TEMPORARY_P (conv) = 1;
887 else
888 return 0;
890 return conv;
893 /* Returns non-zero if T1 is reference-related to T2. */
895 static int
896 reference_related_p (t1, t2)
897 tree t1;
898 tree t2;
900 t1 = TYPE_MAIN_VARIANT (t1);
901 t2 = TYPE_MAIN_VARIANT (t2);
903 /* [dcl.init.ref]
905 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
906 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
907 of T2. */
908 return (same_type_p (t1, t2)
909 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
910 && DERIVED_FROM_P (t1, t2)));
913 /* Returns non-zero if T1 is reference-compatible with T2. */
915 static int
916 reference_compatible_p (t1, t2)
917 tree t1;
918 tree t2;
920 /* [dcl.init.ref]
922 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
923 reference-related to T2 and cv1 is the same cv-qualification as,
924 or greater cv-qualification than, cv2. */
925 return (reference_related_p (t1, t2)
926 && at_least_as_qualified_p (t1, t2));
929 /* Determine whether or not the EXPR (of class type S) can be
930 converted to T as in [over.match.ref]. */
932 static tree
933 convert_class_to_reference (t, s, expr)
934 tree t;
935 tree s;
936 tree expr;
938 tree conversions;
939 tree arglist;
940 tree conv;
941 struct z_candidate *candidates;
942 struct z_candidate *cand;
944 /* [over.match.ref]
946 Assuming that "cv1 T" is the underlying type of the reference
947 being initialized, and "cv S" is the type of the initializer
948 expression, with S a class type, the candidate functions are
949 selected as follows:
951 --The conversion functions of S and its base classes are
952 considered. Those that are not hidden within S and yield type
953 "reference to cv2 T2", where "cv1 T" is reference-compatible
954 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
956 The argument list has one argument, which is the initializer
957 expression. */
959 candidates = 0;
961 /* Conceptually, we should take the address of EXPR and put it in
962 the argument list. Unfortunately, however, that can result in
963 error messages, which we should not issue now because we are just
964 trying to find a conversion operator. Therefore, we use NULL,
965 cast to the appropriate type. */
966 arglist = build_int_2 (0, 0);
967 TREE_TYPE (arglist) = build_pointer_type (s);
968 arglist = build_tree_list (NULL_TREE, arglist);
970 for (conversions = lookup_conversions (s);
971 conversions;
972 conversions = TREE_CHAIN (conversions))
974 tree fns = TREE_VALUE (conversions);
976 for (; fns; fns = OVL_NEXT (fns))
978 tree f = OVL_CURRENT (fns);
979 tree t2 = TREE_TYPE (TREE_TYPE (f));
980 struct z_candidate *old_candidates = candidates;
982 /* If this is a template function, try to get an exact
983 match. */
984 if (TREE_CODE (f) == TEMPLATE_DECL)
986 candidates
987 = add_template_candidate (candidates,
988 f, s,
989 NULL_TREE,
990 arglist,
991 build_reference_type (t),
992 LOOKUP_NORMAL,
993 DEDUCE_CONV);
995 if (candidates != old_candidates)
997 /* Now, see if the conversion function really returns
998 an lvalue of the appropriate type. From the
999 point of view of unification, simply returning an
1000 rvalue of the right type is good enough. */
1001 f = candidates->fn;
1002 t2 = TREE_TYPE (TREE_TYPE (f));
1003 if (TREE_CODE (t2) != REFERENCE_TYPE
1004 || !reference_compatible_p (t, TREE_TYPE (t2)))
1005 candidates = candidates->next;
1008 else if (TREE_CODE (t2) == REFERENCE_TYPE
1009 && reference_compatible_p (t, TREE_TYPE (t2)))
1010 candidates
1011 = add_function_candidate (candidates, f, s, arglist,
1012 LOOKUP_NORMAL);
1014 if (candidates != old_candidates)
1015 candidates->basetype_path = TYPE_BINFO (s);
1019 /* If none of the conversion functions worked out, let our caller
1020 know. */
1021 if (!any_viable (candidates))
1022 return NULL_TREE;
1024 candidates = splice_viable (candidates);
1025 cand = tourney (candidates);
1026 if (!cand)
1027 return NULL_TREE;
1029 conv = build1 (IDENTITY_CONV, s, expr);
1030 conv = build_conv (USER_CONV, TREE_TYPE (TREE_TYPE (cand->fn)),
1031 conv);
1032 TREE_OPERAND (conv, 1) = build_ptr_wrapper (cand);
1033 ICS_USER_FLAG (conv) = 1;
1034 if (cand->viable == -1)
1035 ICS_BAD_FLAG (conv) = 1;
1036 cand->second_conv = conv;
1038 return conv;
1041 /* A reference of the indicated TYPE is being bound directly to the
1042 expression represented by the implicit conversion sequence CONV.
1043 Return a conversion sequence for this binding. */
1045 static tree
1046 direct_reference_binding (type, conv)
1047 tree type;
1048 tree conv;
1050 tree t = TREE_TYPE (type);
1052 /* [over.ics.rank]
1054 When a parameter of reference type binds directly
1055 (_dcl.init.ref_) to an argument expression, the implicit
1056 conversion sequence is the identity conversion, unless the
1057 argument expression has a type that is a derived class of the
1058 parameter type, in which case the implicit conversion sequence is
1059 a derived-to-base Conversion.
1061 If the parameter binds directly to the result of applying a
1062 conversion function to the argument expression, the implicit
1063 conversion sequence is a user-defined conversion sequence
1064 (_over.ics.user_), with the second standard conversion sequence
1065 either an identity conversion or, if the conversion function
1066 returns an entity of a type that is a derived class of the
1067 parameter type, a derived-to-base conversion. */
1068 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1070 /* Represent the derived-to-base conversion. */
1071 conv = build_conv (BASE_CONV, t, conv);
1072 /* We will actually be binding to the base-class subobject in
1073 the derived class, so we mark this conversion appropriately.
1074 That way, convert_like knows not to generate a temporary. */
1075 NEED_TEMPORARY_P (conv) = 0;
1077 return build_conv (REF_BIND, type, conv);
1080 /* Returns the conversion path from type FROM to reference type TO for
1081 purposes of reference binding. For lvalue binding, either pass a
1082 reference type to FROM or an lvalue expression to EXPR. If the
1083 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1084 the conversion returned. */
1086 static tree
1087 reference_binding (rto, rfrom, expr, flags)
1088 tree rto, rfrom, expr;
1089 int flags;
1091 tree conv = NULL_TREE;
1092 tree to = TREE_TYPE (rto);
1093 tree from = rfrom;
1094 int related_p;
1095 int compatible_p;
1096 cp_lvalue_kind lvalue_p = clk_none;
1098 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1100 expr = instantiate_type (to, expr, itf_none);
1101 if (expr == error_mark_node)
1102 return NULL_TREE;
1103 from = TREE_TYPE (expr);
1106 if (TREE_CODE (from) == REFERENCE_TYPE)
1108 /* Anything with reference type is an lvalue. */
1109 lvalue_p = clk_ordinary;
1110 from = TREE_TYPE (from);
1112 else if (expr)
1113 lvalue_p = real_lvalue_p (expr);
1115 /* Figure out whether or not the types are reference-related and
1116 reference compatible. We have do do this after stripping
1117 references from FROM. */
1118 related_p = reference_related_p (to, from);
1119 compatible_p = reference_compatible_p (to, from);
1121 if (lvalue_p && compatible_p)
1123 /* [dcl.init.ref]
1125 If the intializer expression
1127 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1128 is reference-compatible with "cv2 T2,"
1130 the reference is bound directly to the initializer exprssion
1131 lvalue. */
1132 conv = build1 (IDENTITY_CONV, from, expr);
1133 conv = direct_reference_binding (rto, conv);
1134 if ((lvalue_p & clk_bitfield) != 0
1135 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1136 /* For the purposes of overload resolution, we ignore the fact
1137 this expression is a bitfield. (In particular,
1138 [over.ics.ref] says specifically that a function with a
1139 non-const reference parameter is viable even if the
1140 argument is a bitfield.)
1142 However, when we actually call the function we must create
1143 a temporary to which to bind the reference. If the
1144 reference is volatile, or isn't const, then we cannot make
1145 a temporary, so we just issue an error when the conversion
1146 actually occurs. */
1147 NEED_TEMPORARY_P (conv) = 1;
1148 return conv;
1150 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1152 /* [dcl.init.ref]
1154 If the initializer exprsesion
1156 -- has a class type (i.e., T2 is a class type) can be
1157 implicitly converted to an lvalue of type "cv3 T3," where
1158 "cv1 T1" is reference-compatible with "cv3 T3". (this
1159 conversion is selected by enumerating the applicable
1160 conversion functions (_over.match.ref_) and choosing the
1161 best one through overload resolution. (_over.match_).
1163 the reference is bound to the lvalue result of the conversion
1164 in the second case. */
1165 conv = convert_class_to_reference (to, from, expr);
1166 if (conv)
1167 return direct_reference_binding (rto, conv);
1170 /* From this point on, we conceptually need temporaries, even if we
1171 elide them. Only the cases above are "direct bindings". */
1172 if (flags & LOOKUP_NO_TEMP_BIND)
1173 return NULL_TREE;
1175 /* [over.ics.rank]
1177 When a parameter of reference type is not bound directly to an
1178 argument expression, the conversion sequence is the one required
1179 to convert the argument expression to the underlying type of the
1180 reference according to _over.best.ics_. Conceptually, this
1181 conversion sequence corresponds to copy-initializing a temporary
1182 of the underlying type with the argument expression. Any
1183 difference in top-level cv-qualification is subsumed by the
1184 initialization itself and does not constitute a conversion. */
1186 /* [dcl.init.ref]
1188 Otherwise, the reference shall be to a non-volatile const type. */
1189 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1190 return NULL_TREE;
1192 /* [dcl.init.ref]
1194 If the initializer expression is an rvalue, with T2 a class type,
1195 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1196 is bound in one of the following ways:
1198 -- The reference is bound to the object represented by the rvalue
1199 or to a sub-object within that object.
1201 In this case, the implicit conversion sequence is supposed to be
1202 same as we would obtain by generating a temporary. Fortunately,
1203 if the types are reference compatible, then this is either an
1204 identity conversion or the derived-to-base conversion, just as
1205 for direct binding. */
1206 if (CLASS_TYPE_P (from) && compatible_p)
1208 conv = build1 (IDENTITY_CONV, from, expr);
1209 return direct_reference_binding (rto, conv);
1212 /* [dcl.init.ref]
1214 Otherwise, a temporary of type "cv1 T1" is created and
1215 initialized from the initializer expression using the rules for a
1216 non-reference copy initialization. If T1 is reference-related to
1217 T2, cv1 must be the same cv-qualification as, or greater
1218 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1219 if (related_p && !at_least_as_qualified_p (to, from))
1220 return NULL_TREE;
1222 conv = implicit_conversion (to, from, expr, flags);
1223 if (!conv)
1224 return NULL_TREE;
1226 conv = build_conv (REF_BIND, rto, conv);
1227 /* This reference binding, unlike those above, requires the
1228 creation of a temporary. */
1229 NEED_TEMPORARY_P (conv) = 1;
1231 return conv;
1234 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1235 to type TO. The optional expression EXPR may affect the conversion.
1236 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1237 significant. */
1239 static tree
1240 implicit_conversion (to, from, expr, flags)
1241 tree to, from, expr;
1242 int flags;
1244 tree conv;
1245 struct z_candidate *cand;
1247 /* Resolve expressions like `A::p' that we thought might become
1248 pointers-to-members. */
1249 if (expr && TREE_CODE (expr) == OFFSET_REF)
1251 expr = resolve_offset_ref (expr);
1252 from = TREE_TYPE (expr);
1255 if (from == error_mark_node || to == error_mark_node
1256 || expr == error_mark_node)
1257 return NULL_TREE;
1259 /* Make sure both the FROM and TO types are complete so that
1260 user-defined conversions are available. */
1261 complete_type (from);
1262 complete_type (to);
1264 if (TREE_CODE (to) == REFERENCE_TYPE)
1265 conv = reference_binding (to, from, expr, flags);
1266 else
1267 conv = standard_conversion (to, from, expr);
1269 if (conv)
1271 else if (expr != NULL_TREE
1272 && (IS_AGGR_TYPE (from)
1273 || IS_AGGR_TYPE (to))
1274 && (flags & LOOKUP_NO_CONVERSION) == 0)
1276 cand = build_user_type_conversion_1
1277 (to, expr, LOOKUP_ONLYCONVERTING);
1278 if (cand)
1279 conv = cand->second_conv;
1281 /* We used to try to bind a reference to a temporary here, but that
1282 is now handled by the recursive call to this function at the end
1283 of reference_binding. */
1286 return conv;
1289 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1290 functions. */
1292 static struct z_candidate *
1293 add_candidate (candidates, fn, convs, viable)
1294 struct z_candidate *candidates;
1295 tree fn, convs;
1296 int viable;
1298 struct z_candidate *cand
1299 = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1301 cand->fn = fn;
1302 cand->convs = convs;
1303 cand->viable = viable;
1304 cand->next = candidates;
1306 return cand;
1309 /* Create an overload candidate for the function or method FN called with
1310 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1311 to implicit_conversion.
1313 CTYPE, if non-NULL, is the type we want to pretend this function
1314 comes from for purposes of overload resolution. */
1316 static struct z_candidate *
1317 add_function_candidate (candidates, fn, ctype, arglist, flags)
1318 struct z_candidate *candidates;
1319 tree fn, ctype, arglist;
1320 int flags;
1322 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1323 int i, len;
1324 tree convs;
1325 tree parmnode, argnode;
1326 int viable = 1;
1328 /* The `this', `in_chrg' and VTT arguments to constructors are not
1329 considered in overload resolution. */
1330 if (DECL_CONSTRUCTOR_P (fn))
1332 parmlist = skip_artificial_parms_for (fn, parmlist);
1333 arglist = skip_artificial_parms_for (fn, arglist);
1336 len = list_length (arglist);
1337 convs = make_tree_vec (len);
1339 /* 13.3.2 - Viable functions [over.match.viable]
1340 First, to be a viable function, a candidate function shall have enough
1341 parameters to agree in number with the arguments in the list.
1343 We need to check this first; otherwise, checking the ICSes might cause
1344 us to produce an ill-formed template instantiation. */
1346 parmnode = parmlist;
1347 for (i = 0; i < len; ++i)
1349 if (parmnode == NULL_TREE || parmnode == void_list_node)
1350 break;
1351 parmnode = TREE_CHAIN (parmnode);
1354 if (i < len && parmnode)
1355 viable = 0;
1357 /* Make sure there are default args for the rest of the parms. */
1358 else if (!sufficient_parms_p (parmnode))
1359 viable = 0;
1361 if (! viable)
1362 goto out;
1364 /* Second, for F to be a viable function, there shall exist for each
1365 argument an implicit conversion sequence that converts that argument
1366 to the corresponding parameter of F. */
1368 parmnode = parmlist;
1369 argnode = arglist;
1371 for (i = 0; i < len; ++i)
1373 tree arg = TREE_VALUE (argnode);
1374 tree argtype = lvalue_type (arg);
1375 tree t;
1376 int is_this;
1378 if (parmnode == void_list_node)
1379 break;
1381 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1382 && ! DECL_CONSTRUCTOR_P (fn));
1384 if (parmnode)
1386 tree parmtype = TREE_VALUE (parmnode);
1388 /* The type of the implicit object parameter ('this') for
1389 overload resolution is not always the same as for the
1390 function itself; conversion functions are considered to
1391 be members of the class being converted, and functions
1392 introduced by a using-declaration are considered to be
1393 members of the class that uses them.
1395 Since build_over_call ignores the ICS for the `this'
1396 parameter, we can just change the parm type. */
1397 if (ctype && is_this)
1399 parmtype
1400 = build_qualified_type (ctype,
1401 TYPE_QUALS (TREE_TYPE (parmtype)));
1402 parmtype = build_pointer_type (parmtype);
1405 t = implicit_conversion (parmtype, argtype, arg, flags);
1407 else
1409 t = build1 (IDENTITY_CONV, argtype, arg);
1410 ICS_ELLIPSIS_FLAG (t) = 1;
1413 if (t && is_this)
1414 ICS_THIS_FLAG (t) = 1;
1416 TREE_VEC_ELT (convs, i) = t;
1417 if (! t)
1419 viable = 0;
1420 break;
1423 if (ICS_BAD_FLAG (t))
1424 viable = -1;
1426 if (parmnode)
1427 parmnode = TREE_CHAIN (parmnode);
1428 argnode = TREE_CHAIN (argnode);
1431 out:
1432 return add_candidate (candidates, fn, convs, viable);
1435 /* Create an overload candidate for the conversion function FN which will
1436 be invoked for expression OBJ, producing a pointer-to-function which
1437 will in turn be called with the argument list ARGLIST, and add it to
1438 CANDIDATES. FLAGS is passed on to implicit_conversion.
1440 Actually, we don't really care about FN; we care about the type it
1441 converts to. There may be multiple conversion functions that will
1442 convert to that type, and we rely on build_user_type_conversion_1 to
1443 choose the best one; so when we create our candidate, we record the type
1444 instead of the function. */
1446 static struct z_candidate *
1447 add_conv_candidate (candidates, fn, obj, arglist)
1448 struct z_candidate *candidates;
1449 tree fn, obj, arglist;
1451 tree totype = TREE_TYPE (TREE_TYPE (fn));
1452 int i, len, viable, flags;
1453 tree parmlist, convs, parmnode, argnode;
1455 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1456 parmlist = TREE_TYPE (parmlist);
1457 parmlist = TYPE_ARG_TYPES (parmlist);
1459 len = list_length (arglist) + 1;
1460 convs = make_tree_vec (len);
1461 parmnode = parmlist;
1462 argnode = arglist;
1463 viable = 1;
1464 flags = LOOKUP_NORMAL;
1466 /* Don't bother looking up the same type twice. */
1467 if (candidates && candidates->fn == totype)
1468 return candidates;
1470 for (i = 0; i < len; ++i)
1472 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1473 tree argtype = lvalue_type (arg);
1474 tree t;
1476 if (i == 0)
1477 t = implicit_conversion (totype, argtype, arg, flags);
1478 else if (parmnode == void_list_node)
1479 break;
1480 else if (parmnode)
1481 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1482 else
1484 t = build1 (IDENTITY_CONV, argtype, arg);
1485 ICS_ELLIPSIS_FLAG (t) = 1;
1488 TREE_VEC_ELT (convs, i) = t;
1489 if (! t)
1490 break;
1492 if (ICS_BAD_FLAG (t))
1493 viable = -1;
1495 if (i == 0)
1496 continue;
1498 if (parmnode)
1499 parmnode = TREE_CHAIN (parmnode);
1500 argnode = TREE_CHAIN (argnode);
1503 if (i < len)
1504 viable = 0;
1506 if (!sufficient_parms_p (parmnode))
1507 viable = 0;
1509 return add_candidate (candidates, totype, convs, viable);
1512 static struct z_candidate *
1513 build_builtin_candidate (candidates, fnname, type1, type2,
1514 args, argtypes, flags)
1515 struct z_candidate *candidates;
1516 tree fnname, type1, type2, *args, *argtypes;
1517 int flags;
1520 tree t, convs;
1521 int viable = 1, i;
1522 tree types[2];
1524 types[0] = type1;
1525 types[1] = type2;
1527 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1529 for (i = 0; i < 2; ++i)
1531 if (! args[i])
1532 break;
1534 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1535 if (! t)
1537 viable = 0;
1538 /* We need something for printing the candidate. */
1539 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1541 else if (ICS_BAD_FLAG (t))
1542 viable = 0;
1543 TREE_VEC_ELT (convs, i) = t;
1546 /* For COND_EXPR we rearranged the arguments; undo that now. */
1547 if (args[2])
1549 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1550 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1551 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1552 if (t)
1553 TREE_VEC_ELT (convs, 0) = t;
1554 else
1555 viable = 0;
1558 return add_candidate (candidates, fnname, convs, viable);
1561 static int
1562 is_complete (t)
1563 tree t;
1565 return COMPLETE_TYPE_P (complete_type (t));
1568 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1570 static int
1571 promoted_arithmetic_type_p (type)
1572 tree type;
1574 /* [over.built]
1576 In this section, the term promoted integral type is used to refer
1577 to those integral types which are preserved by integral promotion
1578 (including e.g. int and long but excluding e.g. char).
1579 Similarly, the term promoted arithmetic type refers to promoted
1580 integral types plus floating types. */
1581 return ((INTEGRAL_TYPE_P (type)
1582 && same_type_p (type_promotes_to (type), type))
1583 || TREE_CODE (type) == REAL_TYPE);
1586 /* Create any builtin operator overload candidates for the operator in
1587 question given the converted operand types TYPE1 and TYPE2. The other
1588 args are passed through from add_builtin_candidates to
1589 build_builtin_candidate.
1591 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1592 If CODE is requires candidates operands of the same type of the kind
1593 of which TYPE1 and TYPE2 are, we add both candidates
1594 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1596 static struct z_candidate *
1597 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1598 args, argtypes, flags)
1599 struct z_candidate *candidates;
1600 enum tree_code code, code2;
1601 tree fnname, type1, type2, *args, *argtypes;
1602 int flags;
1604 switch (code)
1606 case POSTINCREMENT_EXPR:
1607 case POSTDECREMENT_EXPR:
1608 args[1] = integer_zero_node;
1609 type2 = integer_type_node;
1610 break;
1611 default:
1612 break;
1615 switch (code)
1618 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1619 and VQ is either volatile or empty, there exist candidate operator
1620 functions of the form
1621 VQ T& operator++(VQ T&);
1622 T operator++(VQ T&, int);
1623 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1624 type other than bool, and VQ is either volatile or empty, there exist
1625 candidate operator functions of the form
1626 VQ T& operator--(VQ T&);
1627 T operator--(VQ T&, int);
1628 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1629 complete object type, and VQ is either volatile or empty, there exist
1630 candidate operator functions of the form
1631 T*VQ& operator++(T*VQ&);
1632 T*VQ& operator--(T*VQ&);
1633 T* operator++(T*VQ&, int);
1634 T* operator--(T*VQ&, int); */
1636 case POSTDECREMENT_EXPR:
1637 case PREDECREMENT_EXPR:
1638 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1639 return candidates;
1640 case POSTINCREMENT_EXPR:
1641 case PREINCREMENT_EXPR:
1642 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1644 type1 = build_reference_type (type1);
1645 break;
1647 return candidates;
1649 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1650 exist candidate operator functions of the form
1652 T& operator*(T*);
1654 8 For every function type T, there exist candidate operator functions of
1655 the form
1656 T& operator*(T*); */
1658 case INDIRECT_REF:
1659 if (TREE_CODE (type1) == POINTER_TYPE
1660 && (TYPE_PTROB_P (type1)
1661 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1662 break;
1663 return candidates;
1665 /* 9 For every type T, there exist candidate operator functions of the form
1666 T* operator+(T*);
1668 10For every promoted arithmetic type T, there exist candidate operator
1669 functions of the form
1670 T operator+(T);
1671 T operator-(T); */
1673 case CONVERT_EXPR: /* unary + */
1674 if (TREE_CODE (type1) == POINTER_TYPE
1675 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1676 break;
1677 case NEGATE_EXPR:
1678 if (ARITHMETIC_TYPE_P (type1))
1679 break;
1680 return candidates;
1682 /* 11For every promoted integral type T, there exist candidate operator
1683 functions of the form
1684 T operator~(T); */
1686 case BIT_NOT_EXPR:
1687 if (INTEGRAL_TYPE_P (type1))
1688 break;
1689 return candidates;
1691 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1692 is the same type as C2 or is a derived class of C2, T is a complete
1693 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1694 there exist candidate operator functions of the form
1695 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1696 where CV12 is the union of CV1 and CV2. */
1698 case MEMBER_REF:
1699 if (TREE_CODE (type1) == POINTER_TYPE
1700 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1702 tree c1 = TREE_TYPE (type1);
1703 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1704 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1705 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1707 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1708 && (TYPE_PTRMEMFUNC_P (type2)
1709 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1710 break;
1712 return candidates;
1714 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1715 didate operator functions of the form
1716 LR operator*(L, R);
1717 LR operator/(L, R);
1718 LR operator+(L, R);
1719 LR operator-(L, R);
1720 bool operator<(L, R);
1721 bool operator>(L, R);
1722 bool operator<=(L, R);
1723 bool operator>=(L, R);
1724 bool operator==(L, R);
1725 bool operator!=(L, R);
1726 where LR is the result of the usual arithmetic conversions between
1727 types L and R.
1729 14For every pair of types T and I, where T is a cv-qualified or cv-
1730 unqualified complete object type and I is a promoted integral type,
1731 there exist candidate operator functions of the form
1732 T* operator+(T*, I);
1733 T& operator[](T*, I);
1734 T* operator-(T*, I);
1735 T* operator+(I, T*);
1736 T& operator[](I, T*);
1738 15For every T, where T is a pointer to complete object type, there exist
1739 candidate operator functions of the form112)
1740 ptrdiff_t operator-(T, T);
1742 16For every pointer or enumeration type T, there exist candidate operator
1743 functions of the form
1744 bool operator<(T, T);
1745 bool operator>(T, T);
1746 bool operator<=(T, T);
1747 bool operator>=(T, T);
1748 bool operator==(T, T);
1749 bool operator!=(T, T);
1751 17For every pointer to member type T, there exist candidate operator
1752 functions of the form
1753 bool operator==(T, T);
1754 bool operator!=(T, T); */
1756 case MINUS_EXPR:
1757 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1758 break;
1759 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1761 type2 = ptrdiff_type_node;
1762 break;
1764 case MULT_EXPR:
1765 case TRUNC_DIV_EXPR:
1766 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1767 break;
1768 return candidates;
1770 case EQ_EXPR:
1771 case NE_EXPR:
1772 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1773 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1774 break;
1775 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1776 && null_ptr_cst_p (args[1]))
1778 type2 = type1;
1779 break;
1781 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1782 && null_ptr_cst_p (args[0]))
1784 type1 = type2;
1785 break;
1787 /* FALLTHROUGH */
1788 case LT_EXPR:
1789 case GT_EXPR:
1790 case LE_EXPR:
1791 case GE_EXPR:
1792 case MAX_EXPR:
1793 case MIN_EXPR:
1794 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1795 break;
1796 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1797 break;
1798 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1799 break;
1800 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1802 type2 = type1;
1803 break;
1805 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1807 type1 = type2;
1808 break;
1810 return candidates;
1812 case PLUS_EXPR:
1813 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1814 break;
1815 case ARRAY_REF:
1816 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1818 type1 = ptrdiff_type_node;
1819 break;
1821 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1823 type2 = ptrdiff_type_node;
1824 break;
1826 return candidates;
1828 /* 18For every pair of promoted integral types L and R, there exist candi-
1829 date operator functions of the form
1830 LR operator%(L, R);
1831 LR operator&(L, R);
1832 LR operator^(L, R);
1833 LR operator|(L, R);
1834 L operator<<(L, R);
1835 L operator>>(L, R);
1836 where LR is the result of the usual arithmetic conversions between
1837 types L and R. */
1839 case TRUNC_MOD_EXPR:
1840 case BIT_AND_EXPR:
1841 case BIT_IOR_EXPR:
1842 case BIT_XOR_EXPR:
1843 case LSHIFT_EXPR:
1844 case RSHIFT_EXPR:
1845 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1846 break;
1847 return candidates;
1849 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1850 type, VQ is either volatile or empty, and R is a promoted arithmetic
1851 type, there exist candidate operator functions of the form
1852 VQ L& operator=(VQ L&, R);
1853 VQ L& operator*=(VQ L&, R);
1854 VQ L& operator/=(VQ L&, R);
1855 VQ L& operator+=(VQ L&, R);
1856 VQ L& operator-=(VQ L&, R);
1858 20For every pair T, VQ), where T is any type and VQ is either volatile
1859 or empty, there exist candidate operator functions of the form
1860 T*VQ& operator=(T*VQ&, T*);
1862 21For every pair T, VQ), where T is a pointer to member type and VQ is
1863 either volatile or empty, there exist candidate operator functions of
1864 the form
1865 VQ T& operator=(VQ T&, T);
1867 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1868 unqualified complete object type, VQ is either volatile or empty, and
1869 I is a promoted integral type, there exist candidate operator func-
1870 tions of the form
1871 T*VQ& operator+=(T*VQ&, I);
1872 T*VQ& operator-=(T*VQ&, I);
1874 23For every triple L, VQ, R), where L is an integral or enumeration
1875 type, VQ is either volatile or empty, and R is a promoted integral
1876 type, there exist candidate operator functions of the form
1878 VQ L& operator%=(VQ L&, R);
1879 VQ L& operator<<=(VQ L&, R);
1880 VQ L& operator>>=(VQ L&, R);
1881 VQ L& operator&=(VQ L&, R);
1882 VQ L& operator^=(VQ L&, R);
1883 VQ L& operator|=(VQ L&, R); */
1885 case MODIFY_EXPR:
1886 switch (code2)
1888 case PLUS_EXPR:
1889 case MINUS_EXPR:
1890 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1892 type2 = ptrdiff_type_node;
1893 break;
1895 case MULT_EXPR:
1896 case TRUNC_DIV_EXPR:
1897 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1898 break;
1899 return candidates;
1901 case TRUNC_MOD_EXPR:
1902 case BIT_AND_EXPR:
1903 case BIT_IOR_EXPR:
1904 case BIT_XOR_EXPR:
1905 case LSHIFT_EXPR:
1906 case RSHIFT_EXPR:
1907 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1908 break;
1909 return candidates;
1911 case NOP_EXPR:
1912 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1913 break;
1914 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1915 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1916 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1917 || ((TYPE_PTRMEMFUNC_P (type1)
1918 || TREE_CODE (type1) == POINTER_TYPE)
1919 && null_ptr_cst_p (args[1])))
1921 type2 = type1;
1922 break;
1924 return candidates;
1926 default:
1927 my_friendly_abort (367);
1929 type1 = build_reference_type (type1);
1930 break;
1932 case COND_EXPR:
1933 /* [over.builtin]
1935 For every pair of promoted arithmetic types L and R, there
1936 exist candidate operator functions of the form
1938 LR operator?(bool, L, R);
1940 where LR is the result of the usual arithmetic conversions
1941 between types L and R.
1943 For every type T, where T is a pointer or pointer-to-member
1944 type, there exist candidate operator functions of the form T
1945 operator?(bool, T, T); */
1947 if (promoted_arithmetic_type_p (type1)
1948 && promoted_arithmetic_type_p (type2))
1949 /* That's OK. */
1950 break;
1952 /* Otherwise, the types should be pointers. */
1953 if (!(TREE_CODE (type1) == POINTER_TYPE
1954 || TYPE_PTRMEM_P (type1)
1955 || TYPE_PTRMEMFUNC_P (type1))
1956 || !(TREE_CODE (type2) == POINTER_TYPE
1957 || TYPE_PTRMEM_P (type2)
1958 || TYPE_PTRMEMFUNC_P (type2)))
1959 return candidates;
1961 /* We don't check that the two types are the same; the logic
1962 below will actually create two candidates; one in which both
1963 parameter types are TYPE1, and one in which both parameter
1964 types are TYPE2. */
1965 break;
1967 /* These arguments do not make for a legal overloaded operator. */
1968 return candidates;
1970 default:
1971 my_friendly_abort (367);
1974 /* If we're dealing with two pointer types or two enumeral types,
1975 we need candidates for both of them. */
1976 if (type2 && !same_type_p (type1, type2)
1977 && TREE_CODE (type1) == TREE_CODE (type2)
1978 && (TREE_CODE (type1) == REFERENCE_TYPE
1979 || (TREE_CODE (type1) == POINTER_TYPE
1980 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1981 || TYPE_PTRMEMFUNC_P (type1)
1982 || IS_AGGR_TYPE (type1)
1983 || TREE_CODE (type1) == ENUMERAL_TYPE))
1985 candidates = build_builtin_candidate
1986 (candidates, fnname, type1, type1, args, argtypes, flags);
1987 return build_builtin_candidate
1988 (candidates, fnname, type2, type2, args, argtypes, flags);
1991 return build_builtin_candidate
1992 (candidates, fnname, type1, type2, args, argtypes, flags);
1995 tree
1996 type_decays_to (type)
1997 tree type;
1999 if (TREE_CODE (type) == ARRAY_TYPE)
2000 return build_pointer_type (TREE_TYPE (type));
2001 if (TREE_CODE (type) == FUNCTION_TYPE)
2002 return build_pointer_type (type);
2003 return type;
2006 /* There are three conditions of builtin candidates:
2008 1) bool-taking candidates. These are the same regardless of the input.
2009 2) pointer-pair taking candidates. These are generated for each type
2010 one of the input types converts to.
2011 3) arithmetic candidates. According to the standard, we should generate
2012 all of these, but I'm trying not to...
2014 Here we generate a superset of the possible candidates for this particular
2015 case. That is a subset of the full set the standard defines, plus some
2016 other cases which the standard disallows. add_builtin_candidate will
2017 filter out the illegal set. */
2019 static struct z_candidate *
2020 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
2021 struct z_candidate *candidates;
2022 enum tree_code code, code2;
2023 tree fnname, *args;
2024 int flags;
2026 int ref1, i;
2027 int enum_p = 0;
2028 tree type, argtypes[3];
2029 /* TYPES[i] is the set of possible builtin-operator parameter types
2030 we will consider for the Ith argument. These are represented as
2031 a TREE_LIST; the TREE_VALUE of each node is the potential
2032 parameter type. */
2033 tree types[2];
2035 for (i = 0; i < 3; ++i)
2037 if (args[i])
2038 argtypes[i] = lvalue_type (args[i]);
2039 else
2040 argtypes[i] = NULL_TREE;
2043 switch (code)
2045 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2046 and VQ is either volatile or empty, there exist candidate operator
2047 functions of the form
2048 VQ T& operator++(VQ T&); */
2050 case POSTINCREMENT_EXPR:
2051 case PREINCREMENT_EXPR:
2052 case POSTDECREMENT_EXPR:
2053 case PREDECREMENT_EXPR:
2054 case MODIFY_EXPR:
2055 ref1 = 1;
2056 break;
2058 /* 24There also exist candidate operator functions of the form
2059 bool operator!(bool);
2060 bool operator&&(bool, bool);
2061 bool operator||(bool, bool); */
2063 case TRUTH_NOT_EXPR:
2064 return build_builtin_candidate
2065 (candidates, fnname, boolean_type_node,
2066 NULL_TREE, args, argtypes, flags);
2068 case TRUTH_ORIF_EXPR:
2069 case TRUTH_ANDIF_EXPR:
2070 return build_builtin_candidate
2071 (candidates, fnname, boolean_type_node,
2072 boolean_type_node, args, argtypes, flags);
2074 case ADDR_EXPR:
2075 case COMPOUND_EXPR:
2076 case COMPONENT_REF:
2077 return candidates;
2079 case COND_EXPR:
2080 case EQ_EXPR:
2081 case NE_EXPR:
2082 case LT_EXPR:
2083 case LE_EXPR:
2084 case GT_EXPR:
2085 case GE_EXPR:
2086 enum_p = 1;
2087 /* FALLTHROUGH */
2089 default:
2090 ref1 = 0;
2093 types[0] = types[1] = NULL_TREE;
2095 for (i = 0; i < 2; ++i)
2097 if (! args[i])
2099 else if (IS_AGGR_TYPE (argtypes[i]))
2101 tree convs;
2103 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2104 return candidates;
2106 convs = lookup_conversions (argtypes[i]);
2108 if (code == COND_EXPR)
2110 if (real_lvalue_p (args[i]))
2111 types[i] = tree_cons
2112 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2114 types[i] = tree_cons
2115 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2118 else if (! convs)
2119 return candidates;
2121 for (; convs; convs = TREE_CHAIN (convs))
2123 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2125 if (i == 0 && ref1
2126 && (TREE_CODE (type) != REFERENCE_TYPE
2127 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2128 continue;
2130 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2131 types[i] = tree_cons (NULL_TREE, type, types[i]);
2133 type = non_reference (type);
2134 if (i != 0 || ! ref1)
2136 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2137 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2138 types[i] = tree_cons (NULL_TREE, type, types[i]);
2139 if (INTEGRAL_TYPE_P (type))
2140 type = type_promotes_to (type);
2143 if (! value_member (type, types[i]))
2144 types[i] = tree_cons (NULL_TREE, type, types[i]);
2147 else
2149 if (code == COND_EXPR && real_lvalue_p (args[i]))
2150 types[i] = tree_cons
2151 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2152 type = non_reference (argtypes[i]);
2153 if (i != 0 || ! ref1)
2155 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2156 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2157 types[i] = tree_cons (NULL_TREE, type, types[i]);
2158 if (INTEGRAL_TYPE_P (type))
2159 type = type_promotes_to (type);
2161 types[i] = tree_cons (NULL_TREE, type, types[i]);
2165 /* Run through the possible parameter types of both arguments,
2166 creating candidates with those parameter types. */
2167 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2169 if (types[1])
2170 for (type = types[1]; type; type = TREE_CHAIN (type))
2171 candidates = add_builtin_candidate
2172 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2173 TREE_VALUE (type), args, argtypes, flags);
2174 else
2175 candidates = add_builtin_candidate
2176 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2177 NULL_TREE, args, argtypes, flags);
2180 return candidates;
2184 /* If TMPL can be successfully instantiated as indicated by
2185 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2187 TMPL is the template. EXPLICIT_TARGS are any explicit template
2188 arguments. ARGLIST is the arguments provided at the call-site.
2189 The RETURN_TYPE is the desired type for conversion operators. If
2190 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2191 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2192 add_conv_candidate. */
2194 static struct z_candidate*
2195 add_template_candidate_real (candidates, tmpl, ctype, explicit_targs,
2196 arglist, return_type, flags,
2197 obj, strict)
2198 struct z_candidate *candidates;
2199 tree tmpl, ctype, explicit_targs, arglist, return_type;
2200 int flags;
2201 tree obj;
2202 unification_kind_t strict;
2204 int ntparms = DECL_NTPARMS (tmpl);
2205 tree targs = make_tree_vec (ntparms);
2206 tree args_without_in_chrg = arglist;
2207 struct z_candidate *cand;
2208 int i;
2209 tree fn;
2211 /* We don't do deduction on the in-charge parameter, the VTT
2212 parameter or 'this'. */
2213 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2214 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2216 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2217 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2218 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2219 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2221 i = fn_type_unification (tmpl, explicit_targs, targs,
2222 args_without_in_chrg,
2223 return_type, strict, -1);
2225 if (i != 0)
2226 return candidates;
2228 fn = instantiate_template (tmpl, targs);
2229 if (fn == error_mark_node)
2230 return candidates;
2232 if (obj != NULL_TREE)
2233 /* Aha, this is a conversion function. */
2234 cand = add_conv_candidate (candidates, fn, obj, arglist);
2235 else
2236 cand = add_function_candidate (candidates, fn, ctype,
2237 arglist, flags);
2238 if (DECL_TI_TEMPLATE (fn) != tmpl)
2239 /* This situation can occur if a member template of a template
2240 class is specialized. Then, instantiate_template might return
2241 an instantiation of the specialization, in which case the
2242 DECL_TI_TEMPLATE field will point at the original
2243 specialization. For example:
2245 template <class T> struct S { template <class U> void f(U);
2246 template <> void f(int) {}; };
2247 S<double> sd;
2248 sd.f(3);
2250 Here, TMPL will be template <class U> S<double>::f(U).
2251 And, instantiate template will give us the specialization
2252 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2253 for this will point at template <class T> template <> S<T>::f(int),
2254 so that we can find the definition. For the purposes of
2255 overload resolution, however, we want the original TMPL. */
2256 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2257 else
2258 cand->template = DECL_TEMPLATE_INFO (fn);
2260 return cand;
2264 static struct z_candidate *
2265 add_template_candidate (candidates, tmpl, ctype, explicit_targs,
2266 arglist, return_type, flags, strict)
2267 struct z_candidate *candidates;
2268 tree tmpl, ctype, explicit_targs, arglist, return_type;
2269 int flags;
2270 unification_kind_t strict;
2272 return
2273 add_template_candidate_real (candidates, tmpl, ctype,
2274 explicit_targs, arglist, return_type, flags,
2275 NULL_TREE, strict);
2279 static struct z_candidate *
2280 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2281 struct z_candidate *candidates;
2282 tree tmpl, obj, arglist, return_type;
2284 return
2285 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2286 arglist, return_type, 0, obj, DEDUCE_CONV);
2290 static int
2291 any_viable (cands)
2292 struct z_candidate *cands;
2294 for (; cands; cands = cands->next)
2295 if (pedantic ? cands->viable == 1 : cands->viable)
2296 return 1;
2297 return 0;
2300 static struct z_candidate *
2301 splice_viable (cands)
2302 struct z_candidate *cands;
2304 struct z_candidate **p = &cands;
2306 for (; *p; )
2308 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2309 p = &((*p)->next);
2310 else
2311 *p = (*p)->next;
2314 return cands;
2317 static tree
2318 build_this (obj)
2319 tree obj;
2321 /* Fix this to work on non-lvalues. */
2322 return build_unary_op (ADDR_EXPR, obj, 0);
2325 static void
2326 print_z_candidates (candidates)
2327 struct z_candidate *candidates;
2329 const char *str = "candidates are:";
2330 for (; candidates; candidates = candidates->next)
2332 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2334 if (TREE_VEC_LENGTH (candidates->convs) == 3)
2335 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2336 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2337 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2338 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2339 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2340 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2341 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2342 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2343 else
2344 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2345 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2347 else if (TYPE_P (candidates->fn))
2348 cp_error ("%s %T <conversion>", str, candidates->fn);
2349 else
2350 cp_error_at ("%s %+#D%s", str, candidates->fn,
2351 candidates->viable == -1 ? " <near match>" : "");
2352 str = " ";
2356 /* Returns the best overload candidate to perform the requested
2357 conversion. This function is used for three the overloading situations
2358 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2359 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2360 per [dcl.init.ref], so we ignore temporary bindings. */
2362 static struct z_candidate *
2363 build_user_type_conversion_1 (totype, expr, flags)
2364 tree totype, expr;
2365 int flags;
2367 struct z_candidate *candidates, *cand;
2368 tree fromtype = TREE_TYPE (expr);
2369 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2370 tree args = NULL_TREE;
2371 tree templates = NULL_TREE;
2373 /* We represent conversion within a hierarchy using RVALUE_CONV and
2374 BASE_CONV, as specified by [over.best.ics]; these become plain
2375 constructor calls, as specified in [dcl.init]. */
2376 if (IS_AGGR_TYPE (fromtype) && IS_AGGR_TYPE (totype)
2377 && DERIVED_FROM_P (totype, fromtype))
2378 abort ();
2380 if (IS_AGGR_TYPE (totype))
2381 ctors = lookup_fnfields (TYPE_BINFO (totype),
2382 complete_ctor_identifier,
2385 if (IS_AGGR_TYPE (fromtype))
2386 convs = lookup_conversions (fromtype);
2388 candidates = 0;
2389 flags |= LOOKUP_NO_CONVERSION;
2391 if (ctors)
2393 tree t;
2395 ctors = TREE_VALUE (ctors);
2397 t = build_int_2 (0, 0);
2398 TREE_TYPE (t) = build_pointer_type (totype);
2399 args = build_tree_list (NULL_TREE, expr);
2400 if (DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2401 || DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)))
2402 /* We should never try to call the abstract or base constructor
2403 from here. */
2404 abort ();
2405 args = tree_cons (NULL_TREE, t, args);
2407 for (; ctors; ctors = OVL_NEXT (ctors))
2409 tree ctor = OVL_CURRENT (ctors);
2410 if (DECL_NONCONVERTING_P (ctor))
2411 continue;
2413 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2415 templates = tree_cons (NULL_TREE, ctor, templates);
2416 candidates =
2417 add_template_candidate (candidates, ctor, totype,
2418 NULL_TREE, args, NULL_TREE, flags,
2419 DEDUCE_CALL);
2421 else
2422 candidates = add_function_candidate (candidates, ctor, totype,
2423 args, flags);
2425 if (candidates)
2427 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2428 candidates->basetype_path = TYPE_BINFO (totype);
2432 if (convs)
2433 args = build_tree_list (NULL_TREE, build_this (expr));
2435 for (; convs; convs = TREE_CHAIN (convs))
2437 tree fns = TREE_VALUE (convs);
2438 int convflags = LOOKUP_NO_CONVERSION;
2439 tree ics;
2441 /* If we are called to convert to a reference type, we are trying to
2442 find an lvalue binding, so don't even consider temporaries. If
2443 we don't find an lvalue binding, the caller will try again to
2444 look for a temporary binding. */
2445 if (TREE_CODE (totype) == REFERENCE_TYPE)
2446 convflags |= LOOKUP_NO_TEMP_BIND;
2448 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2449 ics = implicit_conversion
2450 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2451 else
2452 /* We can't compute this yet. */
2453 ics = error_mark_node;
2455 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2456 /* ignore the near match. */;
2457 else if (ics)
2458 for (; fns; fns = OVL_NEXT (fns))
2460 tree fn = OVL_CURRENT (fns);
2461 struct z_candidate *old_candidates = candidates;
2463 /* [over.match.funcs] For conversion functions, the function is
2464 considered to be a member of the class of the implicit object
2465 argument for the purpose of defining the type of the implicit
2466 object parameter.
2468 So we pass fromtype as CTYPE to add_*_candidate. */
2470 if (TREE_CODE (fn) == TEMPLATE_DECL)
2472 templates = tree_cons (NULL_TREE, fn, templates);
2473 candidates =
2474 add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2475 args, totype, flags,
2476 DEDUCE_CONV);
2478 else
2479 candidates = add_function_candidate (candidates, fn, fromtype,
2480 args, flags);
2482 if (candidates != old_candidates)
2484 if (TREE_CODE (fn) == TEMPLATE_DECL)
2485 ics = implicit_conversion
2486 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2487 0, convflags);
2489 candidates->second_conv = ics;
2490 candidates->basetype_path = TYPE_BINFO (fromtype);
2492 if (ics == NULL_TREE)
2493 candidates->viable = 0;
2494 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2495 candidates->viable = -1;
2500 if (! any_viable (candidates))
2502 #if 0
2503 if (flags & LOOKUP_COMPLAIN)
2505 if (candidates && ! candidates->next)
2506 /* say why this one won't work or try to be loose */;
2507 else
2508 cp_error ("no viable candidates");
2510 #endif
2512 return 0;
2515 candidates = splice_viable (candidates);
2516 cand = tourney (candidates);
2518 if (cand == 0)
2520 if (flags & LOOKUP_COMPLAIN)
2522 cp_error ("conversion from `%T' to `%T' is ambiguous",
2523 fromtype, totype);
2524 print_z_candidates (candidates);
2527 cand = candidates; /* any one will do */
2528 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2529 ICS_USER_FLAG (cand->second_conv) = 1;
2530 ICS_BAD_FLAG (cand->second_conv) = 1;
2532 return cand;
2535 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2536 p = &(TREE_OPERAND (*p, 0));
2538 *p = build
2539 (USER_CONV,
2540 (DECL_CONSTRUCTOR_P (cand->fn)
2541 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2542 expr, build_ptr_wrapper (cand));
2544 ICS_USER_FLAG (cand->second_conv) = ICS_USER_FLAG (*p) = 1;
2545 if (cand->viable == -1)
2546 ICS_BAD_FLAG (cand->second_conv) = ICS_BAD_FLAG (*p) = 1;
2548 return cand;
2551 tree
2552 build_user_type_conversion (totype, expr, flags)
2553 tree totype, expr;
2554 int flags;
2556 struct z_candidate *cand
2557 = build_user_type_conversion_1 (totype, expr, flags);
2559 if (cand)
2561 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2562 return error_mark_node;
2563 return convert_from_reference (convert_like (cand->second_conv, expr));
2565 return NULL_TREE;
2568 /* Do any initial processing on the arguments to a function call. */
2570 static tree
2571 resolve_args (args)
2572 tree args;
2574 tree t;
2575 for (t = args; t; t = TREE_CHAIN (t))
2577 tree arg = TREE_VALUE (t);
2579 if (arg == error_mark_node)
2580 return error_mark_node;
2581 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2583 error ("invalid use of void expression");
2584 return error_mark_node;
2586 else if (TREE_CODE (arg) == OFFSET_REF)
2587 arg = resolve_offset_ref (arg);
2588 arg = convert_from_reference (arg);
2589 TREE_VALUE (t) = arg;
2591 return args;
2594 tree
2595 build_new_function_call (fn, args)
2596 tree fn, args;
2598 struct z_candidate *candidates = 0, *cand;
2599 tree explicit_targs = NULL_TREE;
2600 int template_only = 0;
2602 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2604 explicit_targs = TREE_OPERAND (fn, 1);
2605 fn = TREE_OPERAND (fn, 0);
2606 template_only = 1;
2609 if (really_overloaded_fn (fn))
2611 tree t1;
2612 tree templates = NULL_TREE;
2614 args = resolve_args (args);
2616 if (args == error_mark_node)
2617 return error_mark_node;
2619 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2621 tree t = OVL_FUNCTION (t1);
2623 if (TREE_CODE (t) == TEMPLATE_DECL)
2625 templates = tree_cons (NULL_TREE, t, templates);
2626 candidates = add_template_candidate
2627 (candidates, t, NULL_TREE, explicit_targs, args, NULL_TREE,
2628 LOOKUP_NORMAL, DEDUCE_CALL);
2630 else if (! template_only)
2631 candidates = add_function_candidate
2632 (candidates, t, NULL_TREE, args, LOOKUP_NORMAL);
2635 if (! any_viable (candidates))
2637 if (candidates && ! candidates->next)
2638 return build_function_call (candidates->fn, args);
2639 cp_error ("no matching function for call to `%D(%A)'",
2640 DECL_NAME (OVL_FUNCTION (fn)), args);
2641 if (candidates)
2642 print_z_candidates (candidates);
2643 return error_mark_node;
2645 candidates = splice_viable (candidates);
2646 cand = tourney (candidates);
2648 if (cand == 0)
2650 cp_error ("call of overloaded `%D(%A)' is ambiguous",
2651 DECL_NAME (OVL_FUNCTION (fn)), args);
2652 print_z_candidates (candidates);
2653 return error_mark_node;
2656 return build_over_call (cand, args, LOOKUP_NORMAL);
2659 /* This is not really overloaded. */
2660 fn = OVL_CURRENT (fn);
2662 return build_function_call (fn, args);
2665 static tree
2666 build_object_call (obj, args)
2667 tree obj, args;
2669 struct z_candidate *candidates = 0, *cand;
2670 tree fns, convs, mem_args = NULL_TREE;
2671 tree type = TREE_TYPE (obj);
2673 if (TYPE_PTRMEMFUNC_P (type))
2675 /* It's no good looking for an overloaded operator() on a
2676 pointer-to-member-function. */
2677 cp_error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2678 return error_mark_node;
2681 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2682 if (fns == error_mark_node)
2683 return error_mark_node;
2685 args = resolve_args (args);
2687 if (args == error_mark_node)
2688 return error_mark_node;
2690 if (fns)
2692 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
2693 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2695 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2697 tree fn = OVL_CURRENT (fns);
2698 if (TREE_CODE (fn) == TEMPLATE_DECL)
2700 candidates
2701 = add_template_candidate (candidates, fn, base, NULL_TREE,
2702 mem_args, NULL_TREE,
2703 LOOKUP_NORMAL, DEDUCE_CALL);
2705 else
2706 candidates = add_function_candidate
2707 (candidates, fn, base, mem_args, LOOKUP_NORMAL);
2709 if (candidates)
2710 candidates->basetype_path = TYPE_BINFO (type);
2714 convs = lookup_conversions (type);
2716 for (; convs; convs = TREE_CHAIN (convs))
2718 tree fns = TREE_VALUE (convs);
2719 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2721 if ((TREE_CODE (totype) == POINTER_TYPE
2722 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2723 || (TREE_CODE (totype) == REFERENCE_TYPE
2724 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2725 || (TREE_CODE (totype) == REFERENCE_TYPE
2726 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2727 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2728 for (; fns; fns = OVL_NEXT (fns))
2730 tree fn = OVL_CURRENT (fns);
2731 if (TREE_CODE (fn) == TEMPLATE_DECL)
2733 candidates = add_template_conv_candidate (candidates,
2735 obj,
2736 args,
2737 totype);
2739 else
2740 candidates = add_conv_candidate (candidates, fn, obj, args);
2744 if (! any_viable (candidates))
2746 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2747 print_z_candidates (candidates);
2748 return error_mark_node;
2751 candidates = splice_viable (candidates);
2752 cand = tourney (candidates);
2754 if (cand == 0)
2756 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2757 print_z_candidates (candidates);
2758 return error_mark_node;
2761 /* Since cand->fn will be a type, not a function, for a conversion
2762 function, we must be careful not to unconditionally look at
2763 DECL_NAME here. */
2764 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2765 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2766 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2768 obj = convert_like_with_context
2769 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2771 /* FIXME */
2772 return build_function_call (obj, args);
2775 static void
2776 op_error (code, code2, arg1, arg2, arg3, problem)
2777 enum tree_code code, code2;
2778 tree arg1, arg2, arg3;
2779 const char *problem;
2781 const char *opname;
2783 if (code == MODIFY_EXPR)
2784 opname = assignment_operator_name_info[code2].name;
2785 else
2786 opname = operator_name_info[code].name;
2788 switch (code)
2790 case COND_EXPR:
2791 cp_error ("%s for `%T ? %T : %T' operator", problem,
2792 error_type (arg1), error_type (arg2), error_type (arg3));
2793 break;
2794 case POSTINCREMENT_EXPR:
2795 case POSTDECREMENT_EXPR:
2796 cp_error ("%s for `%T %s' operator", problem, error_type (arg1), opname);
2797 break;
2798 case ARRAY_REF:
2799 cp_error ("%s for `%T [%T]' operator", problem,
2800 error_type (arg1), error_type (arg2));
2801 break;
2802 default:
2803 if (arg2)
2804 cp_error ("%s for `%T %s %T' operator", problem,
2805 error_type (arg1), opname, error_type (arg2));
2806 else
2807 cp_error ("%s for `%s %T' operator", problem, opname, error_type (arg1));
2811 /* Return the implicit conversion sequence that could be used to
2812 convert E1 to E2 in [expr.cond]. */
2814 static tree
2815 conditional_conversion (e1, e2)
2816 tree e1;
2817 tree e2;
2819 tree t1 = non_reference (TREE_TYPE (e1));
2820 tree t2 = non_reference (TREE_TYPE (e2));
2821 tree conv;
2823 /* [expr.cond]
2825 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2826 implicitly converted (clause _conv_) to the type "reference to
2827 T2", subject to the constraint that in the conversion the
2828 reference must bind directly (_dcl.init.ref_) to E1. */
2829 if (real_lvalue_p (e2))
2831 conv = implicit_conversion (build_reference_type (t2),
2834 LOOKUP_NO_TEMP_BIND);
2835 if (conv)
2836 return conv;
2839 /* [expr.cond]
2841 If E1 and E2 have class type, and the underlying class types are
2842 the same or one is a base class of the other: E1 can be converted
2843 to match E2 if the class of T2 is the same type as, or a base
2844 class of, the class of T1, and the cv-qualification of T2 is the
2845 same cv-qualification as, or a greater cv-qualification than, the
2846 cv-qualification of T1. If the conversion is applied, E1 is
2847 changed to an rvalue of type T2 that still refers to the original
2848 source class object (or the appropriate subobject thereof). */
2849 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2850 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2851 TYPE_MAIN_VARIANT (t1)))
2853 if (at_least_as_qualified_p (t2, t1))
2855 conv = build1 (IDENTITY_CONV, t1, e1);
2856 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2857 TYPE_MAIN_VARIANT (t2)))
2858 conv = build_conv (BASE_CONV, t2, conv);
2859 return conv;
2861 else
2862 return NULL_TREE;
2865 /* [expr.cond]
2867 E1 can be converted to match E2 if E1 can be implicitly converted
2868 to the type that expression E2 would have if E2 were converted to
2869 an rvalue (or the type it has, if E2 is an rvalue). */
2870 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2873 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2874 arguments to the conditional expression. By the time this function
2875 is called, any suitable candidate functions are included in
2876 CANDIDATES. */
2878 tree
2879 build_conditional_expr (arg1, arg2, arg3)
2880 tree arg1;
2881 tree arg2;
2882 tree arg3;
2884 tree arg2_type;
2885 tree arg3_type;
2886 tree result;
2887 tree result_type = NULL_TREE;
2888 int lvalue_p = 1;
2889 struct z_candidate *candidates = 0;
2890 struct z_candidate *cand;
2892 /* As a G++ extension, the second argument to the conditional can be
2893 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2894 c'.) If the second operand is omitted, make sure it is
2895 calculated only once. */
2896 if (!arg2)
2898 if (pedantic)
2899 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2900 arg1 = arg2 = save_expr (arg1);
2903 /* [expr.cond]
2905 The first expr ession is implicitly converted to bool (clause
2906 _conv_). */
2907 arg1 = cp_convert (boolean_type_node, arg1);
2909 /* If something has already gone wrong, just pass that fact up the
2910 tree. */
2911 if (arg1 == error_mark_node
2912 || arg2 == error_mark_node
2913 || arg3 == error_mark_node
2914 || TREE_TYPE (arg1) == error_mark_node
2915 || TREE_TYPE (arg2) == error_mark_node
2916 || TREE_TYPE (arg3) == error_mark_node)
2917 return error_mark_node;
2919 /* [expr.cond]
2921 If either the second or the third operand has type (possibly
2922 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2923 array-to-pointer (_conv.array_), and function-to-pointer
2924 (_conv.func_) standard conversions are performed on the second
2925 and third operands. */
2926 arg2_type = TREE_TYPE (arg2);
2927 arg3_type = TREE_TYPE (arg3);
2928 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
2930 /* Do the conversions. We don't these for `void' type arguments
2931 since it can't have any effect and since decay_conversion
2932 does not handle that case gracefully. */
2933 if (!VOID_TYPE_P (arg2_type))
2934 arg2 = decay_conversion (arg2);
2935 if (!VOID_TYPE_P (arg3_type))
2936 arg3 = decay_conversion (arg3);
2937 arg2_type = TREE_TYPE (arg2);
2938 arg3_type = TREE_TYPE (arg3);
2940 /* [expr.cond]
2942 One of the following shall hold:
2944 --The second or the third operand (but not both) is a
2945 throw-expression (_except.throw_); the result is of the
2946 type of the other and is an rvalue.
2948 --Both the second and the third operands have type void; the
2949 result is of type void and is an rvalue. */
2950 if ((TREE_CODE (arg2) == THROW_EXPR)
2951 ^ (TREE_CODE (arg3) == THROW_EXPR))
2952 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2953 ? arg3_type : arg2_type);
2954 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
2955 result_type = void_type_node;
2956 else
2958 cp_error ("`%E' has type `void' and is not a throw-expression",
2959 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
2960 return error_mark_node;
2963 lvalue_p = 0;
2964 goto valid_operands;
2966 /* [expr.cond]
2968 Otherwise, if the second and third operand have different types,
2969 and either has (possibly cv-qualified) class type, an attempt is
2970 made to convert each of those operands to the type of the other. */
2971 else if (!same_type_p (arg2_type, arg3_type)
2972 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2974 tree conv2 = conditional_conversion (arg2, arg3);
2975 tree conv3 = conditional_conversion (arg3, arg2);
2977 /* [expr.cond]
2979 If both can be converted, or one can be converted but the
2980 conversion is ambiguous, the program is ill-formed. If
2981 neither can be converted, the operands are left unchanged and
2982 further checking is performed as described below. If exactly
2983 one conversion is possible, that conversion is applied to the
2984 chosen operand and the converted operand is used in place of
2985 the original operand for the remainder of this section. */
2986 if ((conv2 && !ICS_BAD_FLAG (conv2)
2987 && conv3 && !ICS_BAD_FLAG (conv3))
2988 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
2989 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
2991 cp_error ("operands to ?: have different types");
2992 return error_mark_node;
2994 else if (conv2 && !ICS_BAD_FLAG (conv2))
2996 arg2 = convert_like (conv2, arg2);
2997 arg2 = convert_from_reference (arg2);
2998 /* That may not quite have done the trick. If the two types
2999 are cv-qualified variants of one another, we will have
3000 just used an IDENTITY_CONV. (There's no conversion from
3001 an lvalue of one class type to an lvalue of another type,
3002 even a cv-qualified variant, and we don't want to lose
3003 lvalue-ness here.) So, we manually add a NOP_EXPR here
3004 if necessary. */
3005 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
3006 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
3007 arg2_type = TREE_TYPE (arg2);
3009 else if (conv3 && !ICS_BAD_FLAG (conv3))
3011 arg3 = convert_like (conv3, arg3);
3012 arg3 = convert_from_reference (arg3);
3013 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
3014 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
3015 arg3_type = TREE_TYPE (arg3);
3019 /* [expr.cond]
3021 If the second and third operands are lvalues and have the same
3022 type, the result is of that type and is an lvalue. */
3023 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
3024 same_type_p (arg2_type, arg3_type))
3026 result_type = arg2_type;
3027 goto valid_operands;
3030 /* [expr.cond]
3032 Otherwise, the result is an rvalue. If the second and third
3033 operand do not have the same type, and either has (possibly
3034 cv-qualified) class type, overload resolution is used to
3035 determine the conversions (if any) to be applied to the operands
3036 (_over.match.oper_, _over.built_). */
3037 lvalue_p = 0;
3038 if (!same_type_p (arg2_type, arg3_type)
3039 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3041 tree args[3];
3042 tree conv;
3044 /* Rearrange the arguments so that add_builtin_candidate only has
3045 to know about two args. In build_builtin_candidates, the
3046 arguments are unscrambled. */
3047 args[0] = arg2;
3048 args[1] = arg3;
3049 args[2] = arg1;
3050 candidates = add_builtin_candidates (candidates,
3051 COND_EXPR,
3052 NOP_EXPR,
3053 ansi_opname (COND_EXPR),
3054 args,
3055 LOOKUP_NORMAL);
3057 /* [expr.cond]
3059 If the overload resolution fails, the program is
3060 ill-formed. */
3061 if (!any_viable (candidates))
3063 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3064 print_z_candidates (candidates);
3065 return error_mark_node;
3067 candidates = splice_viable (candidates);
3068 cand = tourney (candidates);
3069 if (!cand)
3071 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3072 print_z_candidates (candidates);
3073 return error_mark_node;
3076 /* [expr.cond]
3078 Otherwise, the conversions thus determined are applied, and
3079 the converted operands are used in place of the original
3080 operands for the remainder of this section. */
3081 conv = TREE_VEC_ELT (cand->convs, 0);
3082 arg1 = convert_like (conv, arg1);
3083 conv = TREE_VEC_ELT (cand->convs, 1);
3084 arg2 = convert_like (conv, arg2);
3085 conv = TREE_VEC_ELT (cand->convs, 2);
3086 arg3 = convert_like (conv, arg3);
3089 /* [expr.cond]
3091 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3092 and function-to-pointer (_conv.func_) standard conversions are
3093 performed on the second and third operands.
3095 We need to force the lvalue-to-rvalue conversion here for class types,
3096 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3097 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3098 regions.
3100 We use ocp_convert rather than build_user_type_conversion because the
3101 latter returns NULL_TREE on failure, while the former gives an error. */
3103 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3104 arg2 = ocp_convert (TREE_TYPE (arg2), arg2,
3105 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3106 else
3107 arg2 = decay_conversion (arg2);
3108 arg2_type = TREE_TYPE (arg2);
3110 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3111 arg3 = ocp_convert (TREE_TYPE (arg3), arg3,
3112 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3113 else
3114 arg3 = decay_conversion (arg3);
3115 arg3_type = TREE_TYPE (arg3);
3117 if (arg2 == error_mark_node || arg3 == error_mark_node)
3118 return error_mark_node;
3120 /* [expr.cond]
3122 After those conversions, one of the following shall hold:
3124 --The second and third operands have the same type; the result is of
3125 that type. */
3126 if (same_type_p (arg2_type, arg3_type))
3127 result_type = arg2_type;
3128 /* [expr.cond]
3130 --The second and third operands have arithmetic or enumeration
3131 type; the usual arithmetic conversions are performed to bring
3132 them to a common type, and the result is of that type. */
3133 else if ((ARITHMETIC_TYPE_P (arg2_type)
3134 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3135 && (ARITHMETIC_TYPE_P (arg3_type)
3136 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3138 /* In this case, there is always a common type. */
3139 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3140 arg3_type);
3142 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3143 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3144 cp_warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3145 arg2_type, arg3_type);
3146 else if (extra_warnings
3147 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3148 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3149 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3150 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3151 cp_warning ("enumeral and non-enumeral type in conditional expression");
3153 arg2 = perform_implicit_conversion (result_type, arg2);
3154 arg3 = perform_implicit_conversion (result_type, arg3);
3156 /* [expr.cond]
3158 --The second and third operands have pointer type, or one has
3159 pointer type and the other is a null pointer constant; pointer
3160 conversions (_conv.ptr_) and qualification conversions
3161 (_conv.qual_) are performed to bring them to their composite
3162 pointer type (_expr.rel_). The result is of the composite
3163 pointer type.
3165 --The second and third operands have pointer to member type, or
3166 one has pointer to member type and the other is a null pointer
3167 constant; pointer to member conversions (_conv.mem_) and
3168 qualification conversions (_conv.qual_) are performed to bring
3169 them to a common type, whose cv-qualification shall match the
3170 cv-qualification of either the second or the third operand.
3171 The result is of the common type. */
3172 else if ((null_ptr_cst_p (arg2)
3173 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3174 || TYPE_PTRMEMFUNC_P (arg3_type)))
3175 || (null_ptr_cst_p (arg3)
3176 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3177 || TYPE_PTRMEMFUNC_P (arg2_type)))
3178 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3179 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3180 || (TYPE_PTRMEMFUNC_P (arg2_type)
3181 && TYPE_PTRMEMFUNC_P (arg3_type)))
3183 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3184 arg3, "conditional expression");
3185 arg2 = perform_implicit_conversion (result_type, arg2);
3186 arg3 = perform_implicit_conversion (result_type, arg3);
3189 if (!result_type)
3191 cp_error ("operands to ?: have different types");
3192 return error_mark_node;
3195 valid_operands:
3196 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3197 /* Expand both sides into the same slot, hopefully the target of the
3198 ?: expression. We used to check for TARGET_EXPRs here, but now we
3199 sometimes wrap them in NOP_EXPRs so the test would fail. */
3200 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3201 result = build_target_expr_with_type (result, result_type);
3203 /* If this expression is an rvalue, but might be mistaken for an
3204 lvalue, we must add a NON_LVALUE_EXPR. */
3205 if (!lvalue_p && real_lvalue_p (result))
3206 result = build1 (NON_LVALUE_EXPR, result_type, result);
3208 return result;
3211 tree
3212 build_new_op (code, flags, arg1, arg2, arg3)
3213 enum tree_code code;
3214 int flags;
3215 tree arg1, arg2, arg3;
3217 struct z_candidate *candidates = 0, *cand;
3218 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3219 enum tree_code code2 = NOP_EXPR;
3220 tree templates = NULL_TREE;
3221 tree conv;
3223 if (arg1 == error_mark_node
3224 || arg2 == error_mark_node
3225 || arg3 == error_mark_node)
3226 return error_mark_node;
3228 /* This can happen if a template takes all non-type parameters, e.g.
3229 undeclared_template<1, 5, 72>a; */
3230 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3232 cp_error ("`%D' must be declared before use", arg1);
3233 return error_mark_node;
3236 if (code == MODIFY_EXPR)
3238 code2 = TREE_CODE (arg3);
3239 arg3 = NULL_TREE;
3240 fnname = ansi_assopname (code2);
3242 else
3243 fnname = ansi_opname (code);
3245 if (TREE_CODE (arg1) == OFFSET_REF)
3246 arg1 = resolve_offset_ref (arg1);
3247 arg1 = convert_from_reference (arg1);
3249 switch (code)
3251 case NEW_EXPR:
3252 case VEC_NEW_EXPR:
3253 case VEC_DELETE_EXPR:
3254 case DELETE_EXPR:
3255 /* Use build_op_new_call and build_op_delete_call instead. */
3256 my_friendly_abort (981018);
3258 case CALL_EXPR:
3259 return build_object_call (arg1, arg2);
3261 default:
3262 break;
3265 if (arg2)
3267 if (TREE_CODE (arg2) == OFFSET_REF)
3268 arg2 = resolve_offset_ref (arg2);
3269 arg2 = convert_from_reference (arg2);
3271 if (arg3)
3273 if (TREE_CODE (arg3) == OFFSET_REF)
3274 arg3 = resolve_offset_ref (arg3);
3275 arg3 = convert_from_reference (arg3);
3278 if (code == COND_EXPR)
3280 if (arg2 == NULL_TREE
3281 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3282 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3283 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3284 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3285 goto builtin;
3287 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3288 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3289 goto builtin;
3291 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3292 arg2 = integer_zero_node;
3294 if (arg2 && arg3)
3295 arglist = tree_cons (NULL_TREE, arg1, tree_cons
3296 (NULL_TREE, arg2, build_tree_list (NULL_TREE, arg3)));
3297 else if (arg2)
3298 arglist = tree_cons (NULL_TREE, arg1, build_tree_list (NULL_TREE, arg2));
3299 else
3300 arglist = build_tree_list (NULL_TREE, arg1);
3302 fns = lookup_function_nonclass (fnname, arglist);
3304 if (fns && TREE_CODE (fns) == TREE_LIST)
3305 fns = TREE_VALUE (fns);
3306 for (; fns; fns = OVL_NEXT (fns))
3308 tree fn = OVL_CURRENT (fns);
3309 if (TREE_CODE (fn) == TEMPLATE_DECL)
3311 templates = tree_cons (NULL_TREE, fn, templates);
3312 candidates
3313 = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3314 arglist, TREE_TYPE (fnname),
3315 flags, DEDUCE_CALL);
3317 else
3318 candidates = add_function_candidate (candidates, fn, NULL_TREE,
3319 arglist, flags);
3322 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3324 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3325 if (fns == error_mark_node)
3326 return fns;
3328 else
3329 fns = NULL_TREE;
3331 if (fns)
3333 tree basetype = BINFO_TYPE (TREE_PURPOSE (fns));
3334 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3335 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3337 tree fn = OVL_CURRENT (fns);
3338 tree this_arglist;
3340 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3341 this_arglist = mem_arglist;
3342 else
3343 this_arglist = arglist;
3345 if (TREE_CODE (fn) == TEMPLATE_DECL)
3347 /* A member template. */
3348 templates = tree_cons (NULL_TREE, fn, templates);
3349 candidates
3350 = add_template_candidate (candidates, fn, basetype, NULL_TREE,
3351 this_arglist, TREE_TYPE (fnname),
3352 flags, DEDUCE_CALL);
3354 else
3355 candidates = add_function_candidate
3356 (candidates, fn, basetype, this_arglist, flags);
3358 if (candidates)
3359 candidates->basetype_path = TYPE_BINFO (TREE_TYPE (arg1));
3364 tree args[3];
3366 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3367 to know about two args; a builtin candidate will always have a first
3368 parameter of type bool. We'll handle that in
3369 build_builtin_candidate. */
3370 if (code == COND_EXPR)
3372 args[0] = arg2;
3373 args[1] = arg3;
3374 args[2] = arg1;
3376 else
3378 args[0] = arg1;
3379 args[1] = arg2;
3380 args[2] = NULL_TREE;
3383 candidates = add_builtin_candidates
3384 (candidates, code, code2, fnname, args, flags);
3387 if (! any_viable (candidates))
3389 switch (code)
3391 case POSTINCREMENT_EXPR:
3392 case POSTDECREMENT_EXPR:
3393 /* Look for an `operator++ (int)'. If they didn't have
3394 one, then we fall back to the old way of doing things. */
3395 if (flags & LOOKUP_COMPLAIN)
3396 cp_pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3397 fnname,
3398 operator_name_info[code].name);
3399 if (code == POSTINCREMENT_EXPR)
3400 code = PREINCREMENT_EXPR;
3401 else
3402 code = PREDECREMENT_EXPR;
3403 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3405 /* The caller will deal with these. */
3406 case ADDR_EXPR:
3407 case COMPOUND_EXPR:
3408 case COMPONENT_REF:
3409 return NULL_TREE;
3411 default:
3412 break;
3414 if (flags & LOOKUP_COMPLAIN)
3416 op_error (code, code2, arg1, arg2, arg3, "no match");
3417 print_z_candidates (candidates);
3419 return error_mark_node;
3421 candidates = splice_viable (candidates);
3422 cand = tourney (candidates);
3424 if (cand == 0)
3426 if (flags & LOOKUP_COMPLAIN)
3428 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3429 print_z_candidates (candidates);
3431 return error_mark_node;
3434 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3436 extern int warn_synth;
3437 if (warn_synth
3438 && fnname == ansi_assopname (NOP_EXPR)
3439 && DECL_ARTIFICIAL (cand->fn)
3440 && candidates->next
3441 && ! candidates->next->next)
3443 cp_warning ("using synthesized `%#D' for copy assignment",
3444 cand->fn);
3445 cp_warning_at (" where cfront would use `%#D'",
3446 cand == candidates
3447 ? candidates->next->fn
3448 : candidates->fn);
3451 return build_over_call
3452 (cand,
3453 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3454 ? mem_arglist : arglist,
3455 LOOKUP_NORMAL);
3458 /* Check for comparison of different enum types. */
3459 switch (code)
3461 case GT_EXPR:
3462 case LT_EXPR:
3463 case GE_EXPR:
3464 case LE_EXPR:
3465 case EQ_EXPR:
3466 case NE_EXPR:
3467 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3468 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3469 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3470 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3472 cp_warning ("comparison between `%#T' and `%#T'",
3473 TREE_TYPE (arg1), TREE_TYPE (arg2));
3475 break;
3476 default:
3477 break;
3480 /* We need to strip any leading REF_BIND so that bitfields don't cause
3481 errors. This should not remove any important conversions, because
3482 builtins don't apply to class objects directly. */
3483 conv = TREE_VEC_ELT (cand->convs, 0);
3484 if (TREE_CODE (conv) == REF_BIND)
3485 conv = TREE_OPERAND (conv, 0);
3486 arg1 = convert_like (conv, arg1);
3487 if (arg2)
3489 conv = TREE_VEC_ELT (cand->convs, 1);
3490 if (TREE_CODE (conv) == REF_BIND)
3491 conv = TREE_OPERAND (conv, 0);
3492 arg2 = convert_like (conv, arg2);
3494 if (arg3)
3496 conv = TREE_VEC_ELT (cand->convs, 2);
3497 if (TREE_CODE (conv) == REF_BIND)
3498 conv = TREE_OPERAND (conv, 0);
3499 arg3 = convert_like (conv, arg3);
3502 builtin:
3503 switch (code)
3505 case MODIFY_EXPR:
3506 return build_modify_expr (arg1, code2, arg2);
3508 case INDIRECT_REF:
3509 return build_indirect_ref (arg1, "unary *");
3511 case PLUS_EXPR:
3512 case MINUS_EXPR:
3513 case MULT_EXPR:
3514 case TRUNC_DIV_EXPR:
3515 case GT_EXPR:
3516 case LT_EXPR:
3517 case GE_EXPR:
3518 case LE_EXPR:
3519 case EQ_EXPR:
3520 case NE_EXPR:
3521 case MAX_EXPR:
3522 case MIN_EXPR:
3523 case LSHIFT_EXPR:
3524 case RSHIFT_EXPR:
3525 case TRUNC_MOD_EXPR:
3526 case BIT_AND_EXPR:
3527 case BIT_IOR_EXPR:
3528 case BIT_XOR_EXPR:
3529 case TRUTH_ANDIF_EXPR:
3530 case TRUTH_ORIF_EXPR:
3531 return cp_build_binary_op (code, arg1, arg2);
3533 case CONVERT_EXPR:
3534 case NEGATE_EXPR:
3535 case BIT_NOT_EXPR:
3536 case TRUTH_NOT_EXPR:
3537 case PREINCREMENT_EXPR:
3538 case POSTINCREMENT_EXPR:
3539 case PREDECREMENT_EXPR:
3540 case POSTDECREMENT_EXPR:
3541 case REALPART_EXPR:
3542 case IMAGPART_EXPR:
3543 return build_unary_op (code, arg1, candidates != 0);
3545 case ARRAY_REF:
3546 return build_array_ref (arg1, arg2);
3548 case COND_EXPR:
3549 return build_conditional_expr (arg1, arg2, arg3);
3551 case MEMBER_REF:
3552 return build_m_component_ref
3553 (build_indirect_ref (arg1, NULL), arg2);
3555 /* The caller will deal with these. */
3556 case ADDR_EXPR:
3557 case COMPONENT_REF:
3558 case COMPOUND_EXPR:
3559 return NULL_TREE;
3561 default:
3562 my_friendly_abort (367);
3563 return NULL_TREE;
3567 /* Build a call to operator delete. This has to be handled very specially,
3568 because the restrictions on what signatures match are different from all
3569 other call instances. For a normal delete, only a delete taking (void *)
3570 or (void *, size_t) is accepted. For a placement delete, only an exact
3571 match with the placement new is accepted.
3573 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3574 ADDR is the pointer to be deleted. For placement delete, it is also
3575 used to determine what the corresponding new looked like.
3576 SIZE is the size of the memory block to be deleted.
3577 FLAGS are the usual overloading flags.
3578 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3580 tree
3581 build_op_delete_call (code, addr, size, flags, placement)
3582 enum tree_code code;
3583 tree addr, size, placement;
3584 int flags;
3586 tree fn = NULL_TREE;
3587 tree fns, fnname, fntype, argtypes, args, type;
3588 int pass;
3590 if (addr == error_mark_node)
3591 return error_mark_node;
3593 type = TREE_TYPE (TREE_TYPE (addr));
3594 while (TREE_CODE (type) == ARRAY_TYPE)
3595 type = TREE_TYPE (type);
3597 fnname = ansi_opname (code);
3599 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3600 /* In [class.free]
3602 If the result of the lookup is ambiguous or inaccessible, or if
3603 the lookup selects a placement deallocation function, the
3604 program is ill-formed.
3606 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3608 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3609 if (fns == error_mark_node)
3610 return error_mark_node;
3612 else
3613 fns = NULL_TREE;
3615 if (fns == NULL_TREE)
3616 fns = lookup_name_nonclass (fnname);
3618 if (placement)
3620 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3622 /* Extract the function. */
3623 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3624 /* Then the second parm type. */
3625 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3627 /* Also the second argument. */
3628 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3630 else
3632 /* First try it without the size argument. */
3633 argtypes = void_list_node;
3634 args = NULL_TREE;
3637 /* Strip const and volatile from addr. */
3638 addr = cp_convert (ptr_type_node, addr);
3640 /* We make two tries at finding a matching `operator delete'. On
3641 the first pass, we look for an one-operator (or placement)
3642 operator delete. If we're not doing placement delete, then on
3643 the second pass we look for a two-argument delete. */
3644 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3646 if (pass == 0)
3647 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3648 else
3649 /* Normal delete; now try to find a match including the size
3650 argument. */
3651 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3652 tree_cons (NULL_TREE, sizetype,
3653 void_list_node));
3654 fntype = build_function_type (void_type_node, argtypes);
3656 /* Go through the `operator delete' functions looking for one
3657 with a matching type. */
3658 for (fn = BASELINK_P (fns) ? TREE_VALUE (fns) : fns;
3659 fn;
3660 fn = OVL_NEXT (fn))
3662 tree t;
3664 /* Exception specifications on the `delete' operator do not
3665 matter. */
3666 t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3667 NULL_TREE);
3668 /* We also don't compare attributes. We're really just
3669 trying to check the types of the first two parameters. */
3670 if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3671 break;
3674 /* If we found a match, we're done. */
3675 if (fn)
3676 break;
3679 /* If we have a matching function, call it. */
3680 if (fn)
3682 /* Make sure we have the actual function, and not an
3683 OVERLOAD. */
3684 fn = OVL_CURRENT (fn);
3686 /* If the FN is a member function, make sure that it is
3687 accessible. */
3688 if (DECL_CLASS_SCOPE_P (fn))
3689 enforce_access (type, fn);
3691 if (pass == 0)
3692 args = tree_cons (NULL_TREE, addr, args);
3693 else
3694 args = tree_cons (NULL_TREE, addr,
3695 build_tree_list (NULL_TREE, size));
3697 return build_function_call (fn, args);
3700 /* If we are doing placement delete we do nothing if we don't find a
3701 matching op delete. */
3702 if (placement)
3703 return NULL_TREE;
3705 cp_error ("no suitable `operator delete' for `%T'", type);
3706 return error_mark_node;
3709 /* If the current scope isn't allowed to access DECL along
3710 BASETYPE_PATH, give an error. The most derived class in
3711 BASETYPE_PATH is the one used to qualify DECL. */
3714 enforce_access (basetype_path, decl)
3715 tree basetype_path;
3716 tree decl;
3718 int accessible;
3720 accessible = accessible_p (basetype_path, decl);
3721 if (!accessible)
3723 if (TREE_PRIVATE (decl))
3724 cp_error_at ("`%+#D' is private", decl);
3725 else if (TREE_PROTECTED (decl))
3726 cp_error_at ("`%+#D' is protected", decl);
3727 else
3728 cp_error_at ("`%+#D' is inaccessible", decl);
3729 cp_error ("within this context");
3730 return 0;
3733 return 1;
3736 /* Perform the conversions in CONVS on the expression EXPR.
3737 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3738 indicates the `this' argument of a method. INNER is non-zero when
3739 being called to continue a conversion chain. It is negative when a
3740 reference binding will be applied, positive otherwise. */
3742 static tree
3743 convert_like_real (convs, expr, fn, argnum, inner)
3744 tree convs, expr;
3745 tree fn;
3746 int argnum;
3747 int inner;
3749 int savew, savee;
3751 tree totype = TREE_TYPE (convs);
3753 if (ICS_BAD_FLAG (convs)
3754 && TREE_CODE (convs) != USER_CONV
3755 && TREE_CODE (convs) != AMBIG_CONV
3756 && TREE_CODE (convs) != REF_BIND)
3758 tree t = convs;
3759 for (; t; t = TREE_OPERAND (t, 0))
3761 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3763 expr = convert_like_real (t, expr, fn, argnum, 1);
3764 break;
3766 else if (TREE_CODE (t) == AMBIG_CONV)
3767 return convert_like_real (t, expr, fn, argnum, 1);
3768 else if (TREE_CODE (t) == IDENTITY_CONV)
3769 break;
3771 cp_pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3772 if (fn)
3773 cp_pedwarn (" initializing argument %P of `%D'", argnum, fn);
3774 return cp_convert (totype, expr);
3777 if (!inner)
3778 expr = dubious_conversion_warnings
3779 (totype, expr, "argument", fn, argnum);
3780 switch (TREE_CODE (convs))
3782 case USER_CONV:
3784 struct z_candidate *cand
3785 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3786 tree convfn = cand->fn;
3787 tree args;
3789 if (DECL_CONSTRUCTOR_P (convfn))
3791 tree t = build_int_2 (0, 0);
3792 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3794 args = build_tree_list (NULL_TREE, expr);
3795 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3796 || DECL_HAS_VTT_PARM_P (convfn))
3797 /* We should never try to call the abstract or base constructor
3798 from here. */
3799 abort ();
3800 args = tree_cons (NULL_TREE, t, args);
3802 else
3803 args = build_this (expr);
3804 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3806 /* If this is a constructor or a function returning an aggr type,
3807 we need to build up a TARGET_EXPR. */
3808 if (DECL_CONSTRUCTOR_P (convfn))
3809 expr = build_cplus_new (totype, expr);
3811 /* The result of the call is then used to direct-initialize the object
3812 that is the destination of the copy-initialization. [dcl.init]
3814 Note that this step is not reflected in the conversion sequence;
3815 it affects the semantics when we actually perform the
3816 conversion, but is not considered during overload resolution.
3818 If the target is a class, that means call a ctor. */
3819 if (IS_AGGR_TYPE (totype)
3820 && (inner >= 0 || !real_lvalue_p (expr)))
3822 savew = warningcount, savee = errorcount;
3823 expr = build_new_method_call
3824 (NULL_TREE, complete_ctor_identifier,
3825 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3826 /* Core issue 84, now a DR, says that we don't allow UDCs
3827 for these args (which deliberately breaks copy-init of an
3828 auto_ptr<Base> from an auto_ptr<Derived>). */
3829 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3831 /* Tell the user where this failing constructor call came from. */
3832 if (fn)
3834 if (warningcount > savew)
3835 cp_warning
3836 (" initializing argument %P of `%D' from result of `%D'",
3837 argnum, fn, convfn);
3838 else if (errorcount > savee)
3839 cp_error
3840 (" initializing argument %P of `%D' from result of `%D'",
3841 argnum, fn, convfn);
3843 else
3845 if (warningcount > savew)
3846 cp_warning (" initializing temporary from result of `%D'",
3847 convfn);
3848 else if (errorcount > savee)
3849 cp_error (" initializing temporary from result of `%D'",
3850 convfn);
3852 expr = build_cplus_new (totype, expr);
3854 return expr;
3856 case IDENTITY_CONV:
3857 if (type_unknown_p (expr))
3858 expr = instantiate_type (totype, expr, itf_complain);
3859 return expr;
3860 case AMBIG_CONV:
3861 /* Call build_user_type_conversion again for the error. */
3862 return build_user_type_conversion
3863 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3865 default:
3866 break;
3869 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
3870 TREE_CODE (convs) == REF_BIND ? -1 : 1);
3871 if (expr == error_mark_node)
3872 return error_mark_node;
3874 /* Convert a non-array constant variable to its underlying value, unless we
3875 are about to bind it to a reference, in which case we need to
3876 leave it as an lvalue. */
3877 if (TREE_CODE (convs) != REF_BIND
3878 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3879 expr = decl_constant_value (expr);
3881 switch (TREE_CODE (convs))
3883 case RVALUE_CONV:
3884 if (! IS_AGGR_TYPE (totype))
3885 return expr;
3886 /* else fall through */
3887 case BASE_CONV:
3888 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3890 /* We are going to bind a reference directly to a base-class
3891 subobject of EXPR. */
3892 tree base_ptr = build_pointer_type (totype);
3894 /* Build an expression for `*((base*) &expr)'. */
3895 expr = build_unary_op (ADDR_EXPR, expr, 0);
3896 expr = perform_implicit_conversion (base_ptr, expr);
3897 expr = build_indirect_ref (expr, "implicit conversion");
3898 return expr;
3901 /* Copy-initialization where the cv-unqualified version of the source
3902 type is the same class as, or a derived class of, the class of the
3903 destination [is treated as direct-initialization]. [dcl.init] */
3904 savew = warningcount, savee = errorcount;
3905 expr = build_new_method_call (NULL_TREE, complete_ctor_identifier,
3906 build_tree_list (NULL_TREE, expr),
3907 TYPE_BINFO (totype),
3908 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
3909 if (fn)
3911 if (warningcount > savew)
3912 cp_warning (" initializing argument %P of `%D'", argnum, fn);
3913 else if (errorcount > savee)
3914 cp_error (" initializing argument %P of `%D'", argnum, fn);
3916 return build_cplus_new (totype, expr);
3918 case REF_BIND:
3920 tree ref_type = totype;
3922 /* If necessary, create a temporary. */
3923 if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
3925 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3926 expr = build_target_expr_with_type (expr, type);
3929 /* Take the address of the thing to which we will bind the
3930 reference. */
3931 expr = build_unary_op (ADDR_EXPR, expr, 1);
3932 if (expr == error_mark_node)
3933 return error_mark_node;
3935 /* Convert it to a pointer to the type referred to by the
3936 reference. This will adjust the pointer if a derived to
3937 base conversion is being performed. */
3938 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3939 expr);
3940 /* Convert the pointer to the desired reference type. */
3941 expr = build1 (NOP_EXPR, ref_type, expr);
3943 return expr;
3946 case LVALUE_CONV:
3947 return decay_conversion (expr);
3949 case QUAL_CONV:
3950 /* Warn about deprecated conversion if appropriate. */
3951 string_conv_p (totype, expr, 1);
3952 break;
3954 default:
3955 break;
3957 return ocp_convert (totype, expr, CONV_IMPLICIT,
3958 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3961 /* ARG is being passed to a varargs function. Perform any conversions
3962 required. Array/function to pointer decay must have already happened.
3963 Return the converted value. */
3965 tree
3966 convert_arg_to_ellipsis (arg)
3967 tree arg;
3969 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3970 && (TYPE_PRECISION (TREE_TYPE (arg))
3971 < TYPE_PRECISION (double_type_node)))
3972 /* Convert `float' to `double'. */
3973 arg = cp_convert (double_type_node, arg);
3974 else
3975 /* Convert `short' and `char' to full-size `int'. */
3976 arg = default_conversion (arg);
3978 arg = require_complete_type (arg);
3980 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
3982 /* Undefined behaviour [expr.call] 5.2.2/7. */
3983 cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3984 TREE_TYPE (arg));
3987 return arg;
3990 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
3992 tree
3993 build_x_va_arg (expr, type)
3994 tree expr;
3995 tree type;
3997 if (processing_template_decl)
3998 return build_min (VA_ARG_EXPR, type, expr);
4000 type = complete_type_or_else (type, NULL_TREE);
4002 if (expr == error_mark_node || !type)
4003 return error_mark_node;
4005 if (! pod_type_p (type))
4007 /* Undefined behaviour [expr.call] 5.2.2/7. */
4008 cp_warning ("cannot receive objects of non-POD type `%#T' through `...'",
4009 type);
4012 return build_va_arg (expr, type);
4015 /* TYPE has been given to va_arg. Apply the default conversions which would
4016 have happened when passed via ellipsis. Return the promoted type, or
4017 NULL_TREE, if there is no change. */
4019 tree
4020 convert_type_from_ellipsis (type)
4021 tree type;
4023 tree promote;
4025 if (TREE_CODE (type) == ARRAY_TYPE)
4026 promote = build_pointer_type (TREE_TYPE (type));
4027 else if (TREE_CODE (type) == FUNCTION_TYPE)
4028 promote = build_pointer_type (type);
4029 else
4030 promote = type_promotes_to (type);
4032 return same_type_p (type, promote) ? NULL_TREE : promote;
4035 /* ARG is a default argument expression being passed to a parameter of
4036 the indicated TYPE, which is a parameter to FN. Do any required
4037 conversions. Return the converted value. */
4039 tree
4040 convert_default_arg (type, arg, fn, parmnum)
4041 tree type;
4042 tree arg;
4043 tree fn;
4044 int parmnum;
4046 if (TREE_CODE (arg) == DEFAULT_ARG)
4048 /* When processing the default args for a class, we can find that
4049 there is an ordering constraint, and we call a function who's
4050 default args have not yet been converted. For instance,
4051 class A {
4052 A (int = 0);
4053 void Foo (A const & = A ());
4055 We must process A::A before A::Foo's default arg can be converted.
4056 Remember the dependent function, so do_pending_defargs can retry,
4057 and check loops. */
4058 unprocessed_defarg_fn (fn);
4060 /* Don't return error_mark node, as we won't be able to distinguish
4061 genuine errors from this case, and that would lead to repeated
4062 diagnostics. Just make something of the right type. */
4063 return build1 (NOP_EXPR, type, integer_zero_node);
4066 if (fn && DECL_TEMPLATE_INFO (fn))
4067 arg = tsubst_default_argument (fn, type, arg);
4069 arg = break_out_target_exprs (arg);
4071 if (TREE_CODE (arg) == CONSTRUCTOR)
4073 arg = digest_init (type, arg, 0);
4074 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4075 "default argument", fn, parmnum);
4077 else
4079 /* This could get clobbered by the following call. */
4080 if (TREE_HAS_CONSTRUCTOR (arg))
4081 arg = copy_node (arg);
4083 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4084 "default argument", fn, parmnum);
4085 if (PROMOTE_PROTOTYPES
4086 && INTEGRAL_TYPE_P (type)
4087 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4088 arg = default_conversion (arg);
4091 return arg;
4094 /* Subroutine of the various build_*_call functions. Overload resolution
4095 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4096 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4097 bitmask of various LOOKUP_* flags which apply to the call itself. */
4099 static tree
4100 build_over_call (cand, args, flags)
4101 struct z_candidate *cand;
4102 tree args;
4103 int flags;
4105 tree fn = cand->fn;
4106 tree convs = cand->convs;
4107 tree converted_args = NULL_TREE;
4108 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4109 tree conv, arg, val;
4110 int i = 0;
4111 int is_method = 0;
4113 /* Give any warnings we noticed during overload resolution. */
4114 if (cand->warnings)
4115 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4116 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
4118 if (DECL_FUNCTION_MEMBER_P (fn))
4119 enforce_access (cand->basetype_path, fn);
4121 if (args && TREE_CODE (args) != TREE_LIST)
4122 args = build_tree_list (NULL_TREE, args);
4123 arg = args;
4125 /* The implicit parameters to a constructor are not considered by overload
4126 resolution, and must be of the proper type. */
4127 if (DECL_CONSTRUCTOR_P (fn))
4129 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4130 arg = TREE_CHAIN (arg);
4131 parm = TREE_CHAIN (parm);
4132 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4133 /* We should never try to call the abstract constructor. */
4134 abort ();
4135 if (DECL_HAS_VTT_PARM_P (fn))
4137 converted_args = tree_cons
4138 (NULL_TREE, TREE_VALUE (arg), converted_args);
4139 arg = TREE_CHAIN (arg);
4140 parm = TREE_CHAIN (parm);
4143 /* Bypass access control for 'this' parameter. */
4144 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4146 tree parmtype = TREE_VALUE (parm);
4147 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4148 tree t;
4149 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4150 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4151 TREE_TYPE (argtype), fn);
4153 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4154 X is called for an object that is not of type X, or of a type
4155 derived from X, the behavior is undefined.
4157 So we can assume that anything passed as 'this' is non-null, and
4158 optimize accordingly. */
4159 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4160 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
4161 converted_args = tree_cons (NULL_TREE, t, converted_args);
4162 parm = TREE_CHAIN (parm);
4163 arg = TREE_CHAIN (arg);
4164 ++i;
4165 is_method = 1;
4168 for (; arg && parm;
4169 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4171 tree type = TREE_VALUE (parm);
4173 conv = TREE_VEC_ELT (convs, i);
4174 val = convert_like_with_context
4175 (conv, TREE_VALUE (arg), fn, i - is_method);
4177 if (PROMOTE_PROTOTYPES
4178 && INTEGRAL_TYPE_P (type)
4179 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4180 val = default_conversion (val);
4181 converted_args = tree_cons (NULL_TREE, val, converted_args);
4184 /* Default arguments */
4185 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4186 converted_args
4187 = tree_cons (NULL_TREE,
4188 convert_default_arg (TREE_VALUE (parm),
4189 TREE_PURPOSE (parm),
4190 fn, i - is_method),
4191 converted_args);
4193 /* Ellipsis */
4194 for (; arg; arg = TREE_CHAIN (arg))
4195 converted_args
4196 = tree_cons (NULL_TREE,
4197 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4198 converted_args);
4200 converted_args = nreverse (converted_args);
4202 if (warn_format)
4203 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4204 converted_args);
4206 /* Avoid actually calling copy constructors and copy assignment operators,
4207 if possible. */
4209 if (! flag_elide_constructors)
4210 /* Do things the hard way. */;
4211 else if (TREE_VEC_LENGTH (convs) == 1
4212 && DECL_COPY_CONSTRUCTOR_P (fn))
4214 tree targ;
4215 arg = skip_artificial_parms_for (fn, converted_args);
4216 arg = TREE_VALUE (arg);
4218 /* Pull out the real argument, disregarding const-correctness. */
4219 targ = arg;
4220 while (TREE_CODE (targ) == NOP_EXPR
4221 || TREE_CODE (targ) == NON_LVALUE_EXPR
4222 || TREE_CODE (targ) == CONVERT_EXPR)
4223 targ = TREE_OPERAND (targ, 0);
4224 if (TREE_CODE (targ) == ADDR_EXPR)
4226 targ = TREE_OPERAND (targ, 0);
4227 if (!same_type_ignoring_top_level_qualifiers_p
4228 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4229 targ = NULL_TREE;
4231 else
4232 targ = NULL_TREE;
4234 if (targ)
4235 arg = targ;
4236 else
4237 arg = build_indirect_ref (arg, 0);
4239 /* [class.copy]: the copy constructor is implicitly defined even if
4240 the implementation elided its use. */
4241 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4242 mark_used (fn);
4244 /* If we're creating a temp and we already have one, don't create a
4245 new one. If we're not creating a temp but we get one, use
4246 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4247 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4248 temp or an INIT_EXPR otherwise. */
4249 if (integer_zerop (TREE_VALUE (args)))
4251 if (! real_lvalue_p (arg))
4252 return arg;
4253 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4254 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4256 else if ((!real_lvalue_p (arg)
4257 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4258 /* Empty classes have padding which can be hidden
4259 inside an (empty) base of the class. This must not
4260 be touched as it might overlay things. When the
4261 gcc core learns about empty classes, we can treat it
4262 like other classes. */
4263 && !is_empty_class (DECL_CONTEXT (fn)))
4265 tree address;
4266 tree to = stabilize_reference
4267 (build_indirect_ref (TREE_VALUE (args), 0));
4269 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4270 address = build_unary_op (ADDR_EXPR, val, 0);
4271 /* Avoid a warning about this expression, if the address is
4272 never used. */
4273 TREE_USED (address) = 1;
4274 return address;
4277 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4278 && copy_args_p (fn)
4279 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4281 tree to = stabilize_reference
4282 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4284 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4285 if (is_empty_class (TREE_TYPE (to)))
4287 TREE_USED (arg) = 1;
4289 val = build (COMPOUND_EXPR, DECL_CONTEXT (fn), arg, to);
4290 /* Even though the assignment may not actually result in any
4291 code being generated, we do not want to warn about the
4292 assignment having no effect. That would be confusing to
4293 users who may be performing the assignment as part of a
4294 generic algorithm, for example.
4296 Ideally, the notions of having side-effects and of being
4297 useless would be orthogonal. */
4298 TREE_SIDE_EFFECTS (val) = 1;
4300 else
4301 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4302 return val;
4305 mark_used (fn);
4307 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4309 tree t, *p = &TREE_VALUE (converted_args);
4310 tree binfo = get_binfo
4311 (DECL_VIRTUAL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
4312 *p = convert_pointer_to_real (binfo, *p);
4313 if (TREE_SIDE_EFFECTS (*p))
4314 *p = save_expr (*p);
4315 t = build_pointer_type (TREE_TYPE (fn));
4316 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4317 fn = build_java_interface_fn_ref (fn, *p);
4318 else
4319 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4320 TREE_TYPE (fn) = t;
4322 else if (DECL_INLINE (fn))
4323 fn = inline_conversion (fn);
4324 else
4325 fn = build_addr_func (fn);
4327 /* Recognize certain built-in functions so we can make tree-codes
4328 other than CALL_EXPR. We do this when it enables fold-const.c
4329 to do something useful. */
4331 if (TREE_CODE (fn) == ADDR_EXPR
4332 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4333 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4335 tree exp;
4336 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4337 if (exp)
4338 return exp;
4341 /* Some built-in function calls will be evaluated at
4342 compile-time in fold (). */
4343 fn = fold (build_call (fn, converted_args));
4344 if (VOID_TYPE_P (TREE_TYPE (fn)))
4345 return fn;
4346 fn = require_complete_type (fn);
4347 if (fn == error_mark_node)
4348 return error_mark_node;
4349 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4350 fn = build_cplus_new (TREE_TYPE (fn), fn);
4351 return convert_from_reference (fn);
4354 static tree java_iface_lookup_fn;
4356 /* Make an expression which yields the address of the Java interface
4357 method FN. This is achieved by generating a call to libjava's
4358 _Jv_LookupInterfaceMethodIdx(). */
4360 static tree
4361 build_java_interface_fn_ref (fn, instance)
4362 tree fn, instance;
4364 tree lookup_args, lookup_fn, method, idx;
4365 tree klass_ref, iface, iface_ref;
4366 int i;
4368 if (!java_iface_lookup_fn)
4370 tree endlink = build_void_list_node ();
4371 tree t = tree_cons (NULL_TREE, ptr_type_node,
4372 tree_cons (NULL_TREE, ptr_type_node,
4373 tree_cons (NULL_TREE, java_int_type_node,
4374 endlink)));
4375 java_iface_lookup_fn
4376 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4377 build_function_type (ptr_type_node, t),
4378 0, NOT_BUILT_IN, NULL);
4379 ggc_add_tree_root (&java_iface_lookup_fn, 1);
4382 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4383 This is the first entry in the vtable. */
4384 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4385 integer_zero_node);
4387 /* Get the java.lang.Class pointer for the interface being called. */
4388 iface = DECL_CONTEXT (fn);
4389 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4390 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4391 || DECL_CONTEXT (iface_ref) != iface)
4393 cp_error ("Could not find class$ field in java interface type `%T'",
4394 iface);
4395 return error_mark_node;
4397 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4399 /* Determine the itable index of FN. */
4400 i = 1;
4401 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4403 if (!DECL_VIRTUAL_P (method))
4404 continue;
4405 if (fn == method)
4406 break;
4407 i++;
4409 idx = build_int_2 (i, 0);
4411 lookup_args = tree_cons (NULL_TREE, klass_ref,
4412 tree_cons (NULL_TREE, iface_ref,
4413 build_tree_list (NULL_TREE, idx)));
4414 lookup_fn = build1 (ADDR_EXPR,
4415 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4416 java_iface_lookup_fn);
4417 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4420 /* Returns the value to use for the in-charge parameter when making a
4421 call to a function with the indicated NAME. */
4423 tree
4424 in_charge_arg_for_name (name)
4425 tree name;
4427 if (name == base_ctor_identifier
4428 || name == base_dtor_identifier)
4429 return integer_zero_node;
4430 else if (name == complete_ctor_identifier)
4431 return integer_one_node;
4432 else if (name == complete_dtor_identifier)
4433 return integer_two_node;
4434 else if (name == deleting_dtor_identifier)
4435 return integer_three_node;
4437 /* This function should only be called with one of the names listed
4438 above. */
4439 my_friendly_abort (20000411);
4440 return NULL_TREE;
4443 static tree
4444 build_new_method_call (instance, name, args, basetype_path, flags)
4445 tree instance, name, args, basetype_path;
4446 int flags;
4448 struct z_candidate *candidates = 0, *cand;
4449 tree explicit_targs = NULL_TREE;
4450 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4451 tree pretty_name;
4452 tree user_args;
4453 tree templates = NULL_TREE;
4454 tree call;
4455 int template_only = 0;
4457 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4459 explicit_targs = TREE_OPERAND (name, 1);
4460 name = TREE_OPERAND (name, 0);
4461 if (DECL_P (name))
4462 name = DECL_NAME (name);
4463 else
4465 if (TREE_CODE (name) == COMPONENT_REF)
4466 name = TREE_OPERAND (name, 1);
4467 if (TREE_CODE (name) == OVERLOAD)
4468 name = DECL_NAME (OVL_CURRENT (name));
4471 template_only = 1;
4474 user_args = args;
4475 args = resolve_args (args);
4477 if (args == error_mark_node)
4478 return error_mark_node;
4480 if (instance == NULL_TREE)
4481 basetype = BINFO_TYPE (basetype_path);
4482 else
4484 if (TREE_CODE (instance) == OFFSET_REF)
4485 instance = resolve_offset_ref (instance);
4486 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4487 instance = convert_from_reference (instance);
4488 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4490 /* XXX this should be handled before we get here. */
4491 if (! IS_AGGR_TYPE (basetype))
4493 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4494 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4495 name, instance, basetype);
4497 return error_mark_node;
4501 if (basetype_path == NULL_TREE)
4502 basetype_path = TYPE_BINFO (basetype);
4504 if (instance)
4506 instance_ptr = build_this (instance);
4508 if (! template_only)
4510 /* XXX this should be handled before we get here. */
4511 fns = build_field_call (basetype_path, instance_ptr, name, args);
4512 if (fns)
4513 return fns;
4516 else
4518 instance_ptr = build_int_2 (0, 0);
4519 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4522 /* Callers should explicitly indicate whether they want to construct
4523 the complete object or just the part without virtual bases. */
4524 my_friendly_assert (name != ctor_identifier, 20000408);
4525 /* Similarly for destructors. */
4526 my_friendly_assert (name != dtor_identifier, 20000408);
4528 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4530 int constructor_p;
4532 constructor_p = (name == complete_ctor_identifier
4533 || name == base_ctor_identifier);
4534 pretty_name = (constructor_p
4535 ? constructor_name (basetype) : dtor_identifier);
4537 /* If we're a call to a constructor or destructor for a
4538 subobject that uses virtual base classes, then we need to
4539 pass down a pointer to a VTT for the subobject. */
4540 if ((name == base_ctor_identifier
4541 || name == base_dtor_identifier)
4542 && TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4544 tree vtt;
4545 tree sub_vtt;
4546 tree basebinfo = basetype_path;
4548 /* If the current function is a complete object constructor
4549 or destructor, then we fetch the VTT directly.
4550 Otherwise, we look it up using the VTT we were given. */
4551 vtt = IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type));
4552 vtt = decay_conversion (vtt);
4553 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4554 build (EQ_EXPR, boolean_type_node,
4555 current_in_charge_parm, integer_zero_node),
4556 current_vtt_parm,
4557 vtt);
4558 if (TREE_VIA_VIRTUAL (basebinfo))
4559 basebinfo = binfo_for_vbase (basetype, current_class_type);
4560 my_friendly_assert (BINFO_SUBVTT_INDEX (basebinfo), 20010110);
4561 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4562 BINFO_SUBVTT_INDEX (basebinfo));
4564 args = tree_cons (NULL_TREE, sub_vtt, args);
4567 else
4568 pretty_name = name;
4570 fns = lookup_fnfields (basetype_path, name, 1);
4572 if (fns == error_mark_node)
4573 return error_mark_node;
4574 if (fns)
4576 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
4577 tree fn = TREE_VALUE (fns);
4578 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4579 for (; fn; fn = OVL_NEXT (fn))
4581 tree t = OVL_CURRENT (fn);
4582 tree this_arglist;
4584 /* We can end up here for copy-init of same or base class. */
4585 if ((flags & LOOKUP_ONLYCONVERTING)
4586 && DECL_NONCONVERTING_P (t))
4587 continue;
4589 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4590 this_arglist = mem_args;
4591 else
4592 this_arglist = args;
4594 if (TREE_CODE (t) == TEMPLATE_DECL)
4596 /* A member template. */
4597 templates = tree_cons (NULL_TREE, t, templates);
4598 candidates =
4599 add_template_candidate (candidates, t, base, explicit_targs,
4600 this_arglist,
4601 TREE_TYPE (name), flags, DEDUCE_CALL);
4603 else if (! template_only)
4604 candidates = add_function_candidate (candidates, t, base,
4605 this_arglist, flags);
4607 if (candidates)
4608 candidates->basetype_path = basetype_path;
4612 if (! any_viable (candidates))
4614 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4615 if (flags & LOOKUP_SPECULATIVELY)
4616 return NULL_TREE;
4617 if (!COMPLETE_TYPE_P (basetype))
4618 incomplete_type_error (instance_ptr, basetype);
4619 else
4620 cp_error ("no matching function for call to `%T::%D(%A)%V'",
4621 basetype, pretty_name, user_args,
4622 TREE_TYPE (TREE_TYPE (instance_ptr)));
4623 print_z_candidates (candidates);
4624 return error_mark_node;
4626 candidates = splice_viable (candidates);
4627 cand = tourney (candidates);
4629 if (cand == 0)
4631 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4632 user_args);
4633 print_z_candidates (candidates);
4634 return error_mark_node;
4637 if (DECL_PURE_VIRTUAL_P (cand->fn)
4638 && instance == current_class_ref
4639 && (DECL_CONSTRUCTOR_P (current_function_decl)
4640 || DECL_DESTRUCTOR_P (current_function_decl))
4641 && ! (flags & LOOKUP_NONVIRTUAL)
4642 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4643 cp_error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4644 "abstract virtual `%#D' called from constructor"
4645 : "abstract virtual `%#D' called from destructor"),
4646 cand->fn);
4647 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4648 && is_dummy_object (instance_ptr))
4650 cp_error ("cannot call member function `%D' without object", cand->fn);
4651 return error_mark_node;
4654 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4655 && ((instance == current_class_ref && (dtor_label || ctor_label))
4656 || resolves_to_fixed_type_p (instance, 0)))
4657 flags |= LOOKUP_NONVIRTUAL;
4659 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4660 call = build_over_call (cand, mem_args, flags);
4661 else
4663 call = build_over_call (cand, args, flags);
4664 /* Do evaluate the object parameter in a call to a static member
4665 function. */
4666 if (TREE_SIDE_EFFECTS (instance))
4667 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4670 return call;
4673 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4674 subsequence of ICS2. */
4676 static int
4677 is_subseq (ics1, ics2)
4678 tree ics1, ics2;
4680 /* We can assume that a conversion of the same code
4681 between the same types indicates a subsequence since we only get
4682 here if the types we are converting from are the same. */
4684 while (TREE_CODE (ics1) == RVALUE_CONV
4685 || TREE_CODE (ics1) == LVALUE_CONV)
4686 ics1 = TREE_OPERAND (ics1, 0);
4688 while (1)
4690 while (TREE_CODE (ics2) == RVALUE_CONV
4691 || TREE_CODE (ics2) == LVALUE_CONV)
4692 ics2 = TREE_OPERAND (ics2, 0);
4694 if (TREE_CODE (ics2) == USER_CONV
4695 || TREE_CODE (ics2) == AMBIG_CONV
4696 || TREE_CODE (ics2) == IDENTITY_CONV)
4697 /* At this point, ICS1 cannot be a proper subsequence of
4698 ICS2. We can get a USER_CONV when we are comparing the
4699 second standard conversion sequence of two user conversion
4700 sequences. */
4701 return 0;
4703 ics2 = TREE_OPERAND (ics2, 0);
4705 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4706 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4707 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4708 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4709 return 1;
4713 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4714 be any _TYPE nodes. */
4717 is_properly_derived_from (derived, base)
4718 tree derived;
4719 tree base;
4721 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4722 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4723 return 0;
4725 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4726 considers every class derived from itself. */
4727 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4728 && DERIVED_FROM_P (base, derived));
4731 /* We build the ICS for an implicit object parameter as a pointer
4732 conversion sequence. However, such a sequence should be compared
4733 as if it were a reference conversion sequence. If ICS is the
4734 implicit conversion sequence for an implicit object parameter,
4735 modify it accordingly. */
4737 static void
4738 maybe_handle_implicit_object (ics)
4739 tree* ics;
4741 if (ICS_THIS_FLAG (*ics))
4743 /* [over.match.funcs]
4745 For non-static member functions, the type of the
4746 implicit object parameter is "reference to cv X"
4747 where X is the class of which the function is a
4748 member and cv is the cv-qualification on the member
4749 function declaration. */
4750 tree t = *ics;
4751 tree reference_type;
4753 /* The `this' parameter is a pointer to a class type. Make the
4754 implict conversion talk about a reference to that same class
4755 type. */
4756 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4757 reference_type = build_reference_type (reference_type);
4759 if (TREE_CODE (t) == QUAL_CONV)
4760 t = TREE_OPERAND (t, 0);
4761 if (TREE_CODE (t) == PTR_CONV)
4762 t = TREE_OPERAND (t, 0);
4763 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4764 t = direct_reference_binding (reference_type, t);
4765 *ics = t;
4769 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
4770 and return the type to which the reference refers. Otherwise,
4771 leave *ICS unchanged and return NULL_TREE. */
4773 static tree
4774 maybe_handle_ref_bind (ics)
4775 tree* ics;
4777 if (TREE_CODE (*ics) == REF_BIND)
4779 tree old_ics = *ics;
4780 tree type = TREE_TYPE (TREE_TYPE (old_ics));
4781 *ics = TREE_OPERAND (old_ics, 0);
4782 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
4783 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
4784 return type;
4787 return NULL_TREE;
4790 /* Compare two implicit conversion sequences according to the rules set out in
4791 [over.ics.rank]. Return values:
4793 1: ics1 is better than ics2
4794 -1: ics2 is better than ics1
4795 0: ics1 and ics2 are indistinguishable */
4797 static int
4798 compare_ics (ics1, ics2)
4799 tree ics1, ics2;
4801 tree from_type1;
4802 tree from_type2;
4803 tree to_type1;
4804 tree to_type2;
4805 tree deref_from_type1 = NULL_TREE;
4806 tree deref_from_type2 = NULL_TREE;
4807 tree deref_to_type1 = NULL_TREE;
4808 tree deref_to_type2 = NULL_TREE;
4809 int rank1, rank2;
4811 /* REF_BINDING is non-zero if the result of the conversion sequence
4812 is a reference type. In that case TARGET_TYPE is the
4813 type referred to by the reference. */
4814 tree target_type1;
4815 tree target_type2;
4817 /* Handle implicit object parameters. */
4818 maybe_handle_implicit_object (&ics1);
4819 maybe_handle_implicit_object (&ics2);
4821 /* Handle reference parameters. */
4822 target_type1 = maybe_handle_ref_bind (&ics1);
4823 target_type2 = maybe_handle_ref_bind (&ics2);
4825 /* [over.ics.rank]
4827 When comparing the basic forms of implicit conversion sequences (as
4828 defined in _over.best.ics_)
4830 --a standard conversion sequence (_over.ics.scs_) is a better
4831 conversion sequence than a user-defined conversion sequence
4832 or an ellipsis conversion sequence, and
4834 --a user-defined conversion sequence (_over.ics.user_) is a
4835 better conversion sequence than an ellipsis conversion sequence
4836 (_over.ics.ellipsis_). */
4837 rank1 = ICS_RANK (ics1);
4838 rank2 = ICS_RANK (ics2);
4840 if (rank1 > rank2)
4841 return -1;
4842 else if (rank1 < rank2)
4843 return 1;
4845 if (rank1 == BAD_RANK)
4847 /* XXX Isn't this an extension? */
4848 /* Both ICS are bad. We try to make a decision based on what
4849 would have happenned if they'd been good. */
4850 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4851 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4852 return -1;
4853 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4854 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4855 return 1;
4857 /* We couldn't make up our minds; try to figure it out below. */
4860 if (ICS_ELLIPSIS_FLAG (ics1))
4861 /* Both conversions are ellipsis conversions. */
4862 return 0;
4864 /* User-defined conversion sequence U1 is a better conversion sequence
4865 than another user-defined conversion sequence U2 if they contain the
4866 same user-defined conversion operator or constructor and if the sec-
4867 ond standard conversion sequence of U1 is better than the second
4868 standard conversion sequence of U2. */
4870 if (ICS_USER_FLAG (ics1))
4872 tree t1, t2;
4874 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4875 if (TREE_CODE (t1) == AMBIG_CONV)
4876 return 0;
4877 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4878 if (TREE_CODE (t2) == AMBIG_CONV)
4879 return 0;
4881 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4882 return 0;
4884 /* We can just fall through here, after setting up
4885 FROM_TYPE1 and FROM_TYPE2. */
4886 from_type1 = TREE_TYPE (t1);
4887 from_type2 = TREE_TYPE (t2);
4889 else
4891 /* We're dealing with two standard conversion sequences.
4893 [over.ics.rank]
4895 Standard conversion sequence S1 is a better conversion
4896 sequence than standard conversion sequence S2 if
4898 --S1 is a proper subsequence of S2 (comparing the conversion
4899 sequences in the canonical form defined by _over.ics.scs_,
4900 excluding any Lvalue Transformation; the identity
4901 conversion sequence is considered to be a subsequence of
4902 any non-identity conversion sequence */
4904 from_type1 = ics1;
4905 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4906 from_type1 = TREE_OPERAND (from_type1, 0);
4907 from_type1 = TREE_TYPE (from_type1);
4909 from_type2 = ics2;
4910 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4911 from_type2 = TREE_OPERAND (from_type2, 0);
4912 from_type2 = TREE_TYPE (from_type2);
4915 if (same_type_p (from_type1, from_type2))
4917 if (is_subseq (ics1, ics2))
4918 return 1;
4919 if (is_subseq (ics2, ics1))
4920 return -1;
4922 /* Otherwise, one sequence cannot be a subsequence of the other; they
4923 don't start with the same type. This can happen when comparing the
4924 second standard conversion sequence in two user-defined conversion
4925 sequences. */
4927 /* [over.ics.rank]
4929 Or, if not that,
4931 --the rank of S1 is better than the rank of S2 (by the rules
4932 defined below):
4934 Standard conversion sequences are ordered by their ranks: an Exact
4935 Match is a better conversion than a Promotion, which is a better
4936 conversion than a Conversion.
4938 Two conversion sequences with the same rank are indistinguishable
4939 unless one of the following rules applies:
4941 --A conversion that is not a conversion of a pointer, or pointer
4942 to member, to bool is better than another conversion that is such
4943 a conversion.
4945 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4946 so that we do not have to check it explicitly. */
4947 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4948 return 1;
4949 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4950 return -1;
4952 to_type1 = TREE_TYPE (ics1);
4953 to_type2 = TREE_TYPE (ics2);
4955 if (TYPE_PTR_P (from_type1)
4956 && TYPE_PTR_P (from_type2)
4957 && TYPE_PTR_P (to_type1)
4958 && TYPE_PTR_P (to_type2))
4960 deref_from_type1 = TREE_TYPE (from_type1);
4961 deref_from_type2 = TREE_TYPE (from_type2);
4962 deref_to_type1 = TREE_TYPE (to_type1);
4963 deref_to_type2 = TREE_TYPE (to_type2);
4965 /* The rules for pointers to members A::* are just like the rules
4966 for pointers A*, except opposite: if B is derived from A then
4967 A::* converts to B::*, not vice versa. For that reason, we
4968 switch the from_ and to_ variables here. */
4969 else if (TYPE_PTRMEM_P (from_type1)
4970 && TYPE_PTRMEM_P (from_type2)
4971 && TYPE_PTRMEM_P (to_type1)
4972 && TYPE_PTRMEM_P (to_type2))
4974 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4975 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4976 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4977 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4979 else if (TYPE_PTRMEMFUNC_P (from_type1)
4980 && TYPE_PTRMEMFUNC_P (from_type2)
4981 && TYPE_PTRMEMFUNC_P (to_type1)
4982 && TYPE_PTRMEMFUNC_P (to_type2))
4984 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4985 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4986 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4987 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4990 if (deref_from_type1 != NULL_TREE
4991 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4992 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4994 /* This was one of the pointer or pointer-like conversions.
4996 [over.ics.rank]
4998 --If class B is derived directly or indirectly from class A,
4999 conversion of B* to A* is better than conversion of B* to
5000 void*, and conversion of A* to void* is better than
5001 conversion of B* to void*. */
5002 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5003 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5005 if (is_properly_derived_from (deref_from_type1,
5006 deref_from_type2))
5007 return -1;
5008 else if (is_properly_derived_from (deref_from_type2,
5009 deref_from_type1))
5010 return 1;
5012 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5013 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5015 if (same_type_p (deref_from_type1, deref_from_type2))
5017 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5019 if (is_properly_derived_from (deref_from_type1,
5020 deref_to_type1))
5021 return 1;
5023 /* We know that DEREF_TO_TYPE1 is `void' here. */
5024 else if (is_properly_derived_from (deref_from_type1,
5025 deref_to_type2))
5026 return -1;
5029 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5030 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5032 /* [over.ics.rank]
5034 --If class B is derived directly or indirectly from class A
5035 and class C is derived directly or indirectly from B,
5037 --conversion of C* to B* is better than conversion of C* to
5038 A*,
5040 --conversion of B* to A* is better than conversion of C* to
5041 A* */
5042 if (same_type_p (deref_from_type1, deref_from_type2))
5044 if (is_properly_derived_from (deref_to_type1,
5045 deref_to_type2))
5046 return 1;
5047 else if (is_properly_derived_from (deref_to_type2,
5048 deref_to_type1))
5049 return -1;
5051 else if (same_type_p (deref_to_type1, deref_to_type2))
5053 if (is_properly_derived_from (deref_from_type2,
5054 deref_from_type1))
5055 return 1;
5056 else if (is_properly_derived_from (deref_from_type1,
5057 deref_from_type2))
5058 return -1;
5062 else if (CLASS_TYPE_P (non_reference (from_type1))
5063 && same_type_p (from_type1, from_type2))
5065 tree from = non_reference (from_type1);
5067 /* [over.ics.rank]
5069 --binding of an expression of type C to a reference of type
5070 B& is better than binding an expression of type C to a
5071 reference of type A&
5073 --conversion of C to B is better than conversion of C to A, */
5074 if (is_properly_derived_from (from, to_type1)
5075 && is_properly_derived_from (from, to_type2))
5077 if (is_properly_derived_from (to_type1, to_type2))
5078 return 1;
5079 else if (is_properly_derived_from (to_type2, to_type1))
5080 return -1;
5083 else if (CLASS_TYPE_P (non_reference (to_type1))
5084 && same_type_p (to_type1, to_type2))
5086 tree to = non_reference (to_type1);
5088 /* [over.ics.rank]
5090 --binding of an expression of type B to a reference of type
5091 A& is better than binding an expression of type C to a
5092 reference of type A&,
5094 --onversion of B to A is better than conversion of C to A */
5095 if (is_properly_derived_from (from_type1, to)
5096 && is_properly_derived_from (from_type2, to))
5098 if (is_properly_derived_from (from_type2, from_type1))
5099 return 1;
5100 else if (is_properly_derived_from (from_type1, from_type2))
5101 return -1;
5105 /* [over.ics.rank]
5107 --S1 and S2 differ only in their qualification conversion and yield
5108 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5109 qualification signature of type T1 is a proper subset of the cv-
5110 qualification signature of type T2 */
5111 if (TREE_CODE (ics1) == QUAL_CONV
5112 && TREE_CODE (ics2) == QUAL_CONV
5113 && same_type_p (from_type1, from_type2))
5114 return comp_cv_qual_signature (to_type1, to_type2);
5116 /* [over.ics.rank]
5118 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5119 types to which the references refer are the same type except for
5120 top-level cv-qualifiers, and the type to which the reference
5121 initialized by S2 refers is more cv-qualified than the type to
5122 which the reference initialized by S1 refers */
5124 if (target_type1 && target_type2
5125 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5126 return comp_cv_qualification (target_type2, target_type1);
5128 /* Neither conversion sequence is better than the other. */
5129 return 0;
5132 /* The source type for this standard conversion sequence. */
5134 static tree
5135 source_type (t)
5136 tree t;
5138 for (;; t = TREE_OPERAND (t, 0))
5140 if (TREE_CODE (t) == USER_CONV
5141 || TREE_CODE (t) == AMBIG_CONV
5142 || TREE_CODE (t) == IDENTITY_CONV)
5143 return TREE_TYPE (t);
5145 my_friendly_abort (1823);
5148 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5149 a pointer to LOSER and re-running joust to produce the warning if WINNER
5150 is actually used. */
5152 static void
5153 add_warning (winner, loser)
5154 struct z_candidate *winner, *loser;
5156 winner->warnings = tree_cons (NULL_TREE,
5157 build_ptr_wrapper (loser),
5158 winner->warnings);
5161 /* Returns true iff functions are equivalent. Equivalent functions are
5162 not '==' only if one is a function-local extern function or if
5163 both are extern "C". */
5165 static inline int
5166 equal_functions (fn1, fn2)
5167 tree fn1;
5168 tree fn2;
5170 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5171 || DECL_EXTERN_C_FUNCTION_P (fn1))
5172 return decls_match (fn1, fn2);
5173 return fn1 == fn2;
5176 /* Compare two candidates for overloading as described in
5177 [over.match.best]. Return values:
5179 1: cand1 is better than cand2
5180 -1: cand2 is better than cand1
5181 0: cand1 and cand2 are indistinguishable */
5183 static int
5184 joust (cand1, cand2, warn)
5185 struct z_candidate *cand1, *cand2;
5186 int warn;
5188 int winner = 0;
5189 int i, off1 = 0, off2 = 0, len;
5191 /* Candidates that involve bad conversions are always worse than those
5192 that don't. */
5193 if (cand1->viable > cand2->viable)
5194 return 1;
5195 if (cand1->viable < cand2->viable)
5196 return -1;
5198 /* If we have two pseudo-candidates for conversions to the same type,
5199 or two candidates for the same function, arbitrarily pick one. */
5200 if (cand1->fn == cand2->fn
5201 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5202 return 1;
5204 /* a viable function F1
5205 is defined to be a better function than another viable function F2 if
5206 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5207 ICSi(F2), and then */
5209 /* for some argument j, ICSj(F1) is a better conversion sequence than
5210 ICSj(F2) */
5212 /* For comparing static and non-static member functions, we ignore
5213 the implicit object parameter of the non-static function. The
5214 standard says to pretend that the static function has an object
5215 parm, but that won't work with operator overloading. */
5216 len = TREE_VEC_LENGTH (cand1->convs);
5217 if (len != TREE_VEC_LENGTH (cand2->convs))
5219 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5220 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5221 off2 = 1;
5222 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5223 && DECL_STATIC_FUNCTION_P (cand2->fn))
5225 off1 = 1;
5226 --len;
5228 else
5229 my_friendly_abort (42);
5232 for (i = 0; i < len; ++i)
5234 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5235 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5236 int comp = compare_ics (t1, t2);
5238 if (comp != 0)
5240 if (warn_sign_promo
5241 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5242 && TREE_CODE (t1) == STD_CONV
5243 && TREE_CODE (t2) == STD_CONV
5244 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5245 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5246 && (TYPE_PRECISION (TREE_TYPE (t1))
5247 == TYPE_PRECISION (TREE_TYPE (t2)))
5248 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5249 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5250 == ENUMERAL_TYPE)))
5252 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5253 tree type1, type2;
5254 struct z_candidate *w, *l;
5255 if (comp > 0)
5256 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5257 w = cand1, l = cand2;
5258 else
5259 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5260 w = cand2, l = cand1;
5262 if (warn)
5264 cp_warning ("passing `%T' chooses `%T' over `%T'",
5265 type, type1, type2);
5266 cp_warning (" in call to `%D'", w->fn);
5268 else
5269 add_warning (w, l);
5272 if (winner && comp != winner)
5274 winner = 0;
5275 goto tweak;
5277 winner = comp;
5281 /* warn about confusing overload resolution for user-defined conversions,
5282 either between a constructor and a conversion op, or between two
5283 conversion ops. */
5284 if (winner && cand1->second_conv
5285 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5286 != DECL_CONSTRUCTOR_P (cand2->fn))
5287 /* Don't warn if the two conv ops convert to the same type... */
5288 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5289 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5290 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5292 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5293 if (comp != winner)
5295 struct z_candidate *w, *l;
5296 tree convn;
5297 if (winner == 1)
5298 w = cand1, l = cand2;
5299 else
5300 w = cand2, l = cand1;
5301 if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5302 && ! DECL_CONSTRUCTOR_P (cand1->fn)
5303 && ! DECL_CONSTRUCTOR_P (cand2->fn)
5304 && (convn = standard_conversion
5305 (TREE_TYPE (TREE_TYPE (l->fn)),
5306 TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5307 && TREE_CODE (convn) == QUAL_CONV)
5308 /* Don't complain about `operator char *()' beating
5309 `operator const char *() const'. */;
5310 else if (warn)
5312 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5313 if (! DECL_CONSTRUCTOR_P (w->fn))
5314 source = TREE_TYPE (source);
5315 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
5316 cp_warning (" for conversion from `%T' to `%T'",
5317 source, TREE_TYPE (w->second_conv));
5318 cp_warning (" because conversion sequence for the argument is better");
5320 else
5321 add_warning (w, l);
5325 if (winner)
5326 return winner;
5328 /* or, if not that,
5329 F1 is a non-template function and F2 is a template function
5330 specialization. */
5332 if (! cand1->template && cand2->template)
5333 return 1;
5334 else if (cand1->template && ! cand2->template)
5335 return -1;
5337 /* or, if not that,
5338 F1 and F2 are template functions and the function template for F1 is
5339 more specialized than the template for F2 according to the partial
5340 ordering rules. */
5342 if (cand1->template && cand2->template)
5344 winner = more_specialized
5345 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5346 DEDUCE_ORDER,
5347 /* Tell the deduction code how many real function arguments
5348 we saw, not counting the implicit 'this' argument. But,
5349 add_function_candidate() suppresses the "this" argument
5350 for constructors.
5352 [temp.func.order]: The presence of unused ellipsis and default
5353 arguments has no effect on the partial ordering of function
5354 templates. */
5355 TREE_VEC_LENGTH (cand1->convs)
5356 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5357 - DECL_CONSTRUCTOR_P (cand1->fn)));
5358 /* HERE */
5359 if (winner)
5360 return winner;
5363 /* or, if not that,
5364 the context is an initialization by user-defined conversion (see
5365 _dcl.init_ and _over.match.user_) and the standard conversion
5366 sequence from the return type of F1 to the destination type (i.e.,
5367 the type of the entity being initialized) is a better conversion
5368 sequence than the standard conversion sequence from the return type
5369 of F2 to the destination type. */
5371 if (cand1->second_conv)
5373 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5374 if (winner)
5375 return winner;
5378 /* Check whether we can discard a builtin candidate, either because we
5379 have two identical ones or matching builtin and non-builtin candidates.
5381 (Pedantically in the latter case the builtin which matched the user
5382 function should not be added to the overload set, but we spot it here.
5384 [over.match.oper]
5385 ... the builtin candidates include ...
5386 - do not have the same parameter type list as any non-template
5387 non-member candidate. */
5389 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5390 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5392 for (i = 0; i < len; ++i)
5393 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5394 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5395 break;
5396 if (i == TREE_VEC_LENGTH (cand1->convs))
5398 if (cand1->fn == cand2->fn)
5399 /* Two built-in candidates; arbitrarily pick one. */
5400 return 1;
5401 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5402 /* cand1 is built-in; prefer cand2. */
5403 return -1;
5404 else
5405 /* cand2 is built-in; prefer cand1. */
5406 return 1;
5409 /* Kludge around broken overloading rules whereby
5410 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
5411 that takes references and another that takes values. */
5412 if (cand1->fn == cand2->fn
5413 && cand1->fn == ansi_opname (COND_EXPR))
5415 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
5416 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
5417 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
5418 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
5420 if (same_type_p (t1, t2))
5422 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
5423 return 1;
5424 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
5425 return -1;
5430 /* If the two functions are the same (this can happen with declarations
5431 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5432 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5433 && equal_functions (cand1->fn, cand2->fn))
5434 return 1;
5436 tweak:
5438 /* Extension: If the worst conversion for one candidate is worse than the
5439 worst conversion for the other, take the first. */
5440 if (!pedantic)
5442 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5443 struct z_candidate *w = 0, *l = 0;
5445 for (i = 0; i < len; ++i)
5447 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5448 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5449 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5450 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5452 if (rank1 < rank2)
5453 winner = 1, w = cand1, l = cand2;
5454 if (rank1 > rank2)
5455 winner = -1, w = cand2, l = cand1;
5456 if (winner)
5458 if (warn)
5460 cp_pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5461 cp_pedwarn (
5462 " because worst conversion for the former is better than worst conversion for the latter");
5464 else
5465 add_warning (w, l);
5466 return winner;
5470 my_friendly_assert (!winner, 20010121);
5471 return 0;
5474 /* Given a list of candidates for overloading, find the best one, if any.
5475 This algorithm has a worst case of O(2n) (winner is last), and a best
5476 case of O(n/2) (totally ambiguous); much better than a sorting
5477 algorithm. */
5479 static struct z_candidate *
5480 tourney (candidates)
5481 struct z_candidate *candidates;
5483 struct z_candidate *champ = candidates, *challenger;
5484 int fate;
5485 int champ_compared_to_predecessor = 0;
5487 /* Walk through the list once, comparing each current champ to the next
5488 candidate, knocking out a candidate or two with each comparison. */
5490 for (challenger = champ->next; challenger; )
5492 fate = joust (champ, challenger, 0);
5493 if (fate == 1)
5494 challenger = challenger->next;
5495 else
5497 if (fate == 0)
5499 champ = challenger->next;
5500 if (champ == 0)
5501 return 0;
5502 champ_compared_to_predecessor = 0;
5504 else
5506 champ = challenger;
5507 champ_compared_to_predecessor = 1;
5510 challenger = champ->next;
5514 /* Make sure the champ is better than all the candidates it hasn't yet
5515 been compared to. */
5517 for (challenger = candidates;
5518 challenger != champ
5519 && !(champ_compared_to_predecessor && challenger->next == champ);
5520 challenger = challenger->next)
5522 fate = joust (champ, challenger, 0);
5523 if (fate != 1)
5524 return 0;
5527 return champ;
5530 /* Returns non-zero if things of type FROM can be converted to TO. */
5533 can_convert (to, from)
5534 tree to, from;
5536 return can_convert_arg (to, from, NULL_TREE);
5539 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5542 can_convert_arg (to, from, arg)
5543 tree to, from, arg;
5545 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5546 return (t && ! ICS_BAD_FLAG (t));
5549 /* Like can_convert_arg, but allows dubious conversions as well. */
5552 can_convert_arg_bad (to, from, arg)
5553 tree to, from, arg;
5555 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5556 return !!t;
5559 /* Convert EXPR to TYPE. Return the converted expression.
5561 Note that we allow bad conversions here because by the time we get to
5562 this point we are committed to doing the conversion. If we end up
5563 doing a bad conversion, convert_like will complain. */
5565 tree
5566 perform_implicit_conversion (type, expr)
5567 tree type;
5568 tree expr;
5570 tree conv;
5572 if (expr == error_mark_node)
5573 return error_mark_node;
5574 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5575 LOOKUP_NORMAL);
5576 if (!conv)
5578 cp_error ("could not convert `%E' to `%T'", expr, type);
5579 return error_mark_node;
5582 return convert_like (conv, expr);
5585 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5586 initializing a variable of that TYPE. Return the converted
5587 expression. */
5589 tree
5590 initialize_reference (type, expr)
5591 tree type;
5592 tree expr;
5594 tree conv;
5596 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5597 if (!conv || ICS_BAD_FLAG (conv))
5599 cp_error ("could not convert `%E' to `%T'", expr, type);
5600 return error_mark_node;
5603 return convert_like (conv, expr);