cp:
[official-gcc.git] / gcc / cp / call.c
blob29e0c72caeaf991ae67a2aae2a60a818d699bd63
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 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) \
51 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0)
52 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
53 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0)
54 static tree convert_like_real PARAMS ((tree, tree, tree, int, int));
55 static void op_error PARAMS ((enum tree_code, enum tree_code, tree, tree,
56 tree, const char *));
57 static tree build_object_call PARAMS ((tree, tree));
58 static tree resolve_args PARAMS ((tree));
59 static struct z_candidate * build_user_type_conversion_1
60 PARAMS ((tree, tree, int));
61 static void print_z_candidates PARAMS ((struct z_candidate *));
62 static tree build_this PARAMS ((tree));
63 static struct z_candidate * splice_viable PARAMS ((struct z_candidate *));
64 static int any_viable PARAMS ((struct z_candidate *));
65 static struct z_candidate * add_template_candidate
66 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
67 unification_kind_t));
68 static struct z_candidate * add_template_candidate_real
69 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
70 tree, unification_kind_t));
71 static struct z_candidate * add_template_conv_candidate
72 PARAMS ((struct z_candidate *, tree, tree, tree, tree));
73 static struct z_candidate * add_builtin_candidates
74 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
75 tree, tree *, int));
76 static struct z_candidate * add_builtin_candidate
77 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
78 tree, tree, tree, tree *, tree *, int));
79 static int is_complete PARAMS ((tree));
80 static struct z_candidate * build_builtin_candidate
81 PARAMS ((struct z_candidate *, tree, tree, tree, tree *, tree *,
82 int));
83 static struct z_candidate * add_conv_candidate
84 PARAMS ((struct z_candidate *, tree, tree, tree));
85 static struct z_candidate * add_function_candidate
86 PARAMS ((struct z_candidate *, tree, tree, tree, int));
87 static tree implicit_conversion PARAMS ((tree, tree, tree, int));
88 static tree standard_conversion PARAMS ((tree, tree, tree));
89 static tree reference_binding PARAMS ((tree, tree, tree, int));
90 static tree non_reference PARAMS ((tree));
91 static tree build_conv PARAMS ((enum tree_code, tree, tree));
92 static int is_subseq PARAMS ((tree, tree));
93 static tree maybe_handle_ref_bind PARAMS ((tree*));
94 static void maybe_handle_implicit_object PARAMS ((tree*));
95 static struct z_candidate * add_candidate PARAMS ((struct z_candidate *,
96 tree, tree, int));
97 static tree source_type PARAMS ((tree));
98 static void add_warning PARAMS ((struct z_candidate *, struct z_candidate *));
99 static int reference_related_p PARAMS ((tree, tree));
100 static int reference_compatible_p PARAMS ((tree, tree));
101 static tree convert_class_to_reference PARAMS ((tree, tree, tree));
102 static tree direct_reference_binding PARAMS ((tree, tree));
103 static int promoted_arithmetic_type_p PARAMS ((tree));
104 static tree conditional_conversion PARAMS ((tree, tree));
106 tree
107 build_vfield_ref (datum, type)
108 tree datum, type;
110 tree rval;
112 if (datum == error_mark_node)
113 return error_mark_node;
115 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
116 datum = convert_from_reference (datum);
118 if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type))
119 rval = build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
120 datum, TYPE_VFIELD (type));
121 else
122 rval = build_component_ref (datum, DECL_NAME (TYPE_VFIELD (type)), NULL_TREE, 0);
124 return rval;
127 /* Build a call to a member of an object. I.e., one that overloads
128 operator ()(), or is a pointer-to-function or pointer-to-method. */
130 static tree
131 build_field_call (basetype_path, instance_ptr, name, parms)
132 tree basetype_path, instance_ptr, name, parms;
134 tree field, instance;
136 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
137 return NULL_TREE;
139 /* Speed up the common case. */
140 if (instance_ptr == current_class_ptr
141 && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
142 return NULL_TREE;
144 field = lookup_field (basetype_path, name, 1, 0);
146 if (field == error_mark_node || field == NULL_TREE)
147 return field;
149 if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
151 /* If it's a field, try overloading operator (),
152 or calling if the field is a pointer-to-function. */
153 instance = build_indirect_ref (instance_ptr, NULL);
154 instance = build_component_ref_1 (instance, field, 0);
156 if (instance == error_mark_node)
157 return error_mark_node;
159 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
160 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
161 instance, parms, NULL_TREE);
162 else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
163 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
164 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
165 == FUNCTION_TYPE)))
166 return build_function_call (instance, parms);
169 return NULL_TREE;
172 /* Returns nonzero iff the destructor name specified in NAME
173 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
174 forms... */
177 check_dtor_name (basetype, name)
178 tree basetype, name;
180 name = TREE_OPERAND (name, 0);
182 /* Just accept something we've already complained about. */
183 if (name == error_mark_node)
184 return 1;
186 if (TREE_CODE (name) == TYPE_DECL)
187 name = TREE_TYPE (name);
188 else if (TYPE_P (name))
189 /* OK */;
190 else if (TREE_CODE (name) == IDENTIFIER_NODE)
192 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
193 || (TREE_CODE (basetype) == ENUMERAL_TYPE
194 && name == TYPE_IDENTIFIER (basetype)))
195 name = basetype;
196 else
197 name = get_type_value (name);
199 /* In the case of:
201 template <class T> struct S { ~S(); };
202 int i;
203 i.~S();
205 NAME will be a class template. */
206 else if (DECL_CLASS_TEMPLATE_P (name))
207 return 0;
208 else
209 abort ();
211 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
212 return 1;
213 return 0;
216 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
217 This is how virtual function calls are avoided. */
219 tree
220 build_scoped_method_call (exp, basetype, name, parms)
221 tree exp, basetype, name, parms;
223 /* Because this syntactic form does not allow
224 a pointer to a base class to be `stolen',
225 we need not protect the derived->base conversion
226 that happens here.
228 @@ But we do have to check access privileges later. */
229 tree binfo, decl;
230 tree type = TREE_TYPE (exp);
232 if (type == error_mark_node
233 || basetype == error_mark_node)
234 return error_mark_node;
236 if (processing_template_decl)
238 if (TREE_CODE (name) == BIT_NOT_EXPR
239 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
241 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
242 if (type)
243 name = build_min_nt (BIT_NOT_EXPR, type);
245 name = build_min_nt (SCOPE_REF, basetype, name);
246 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
249 if (TREE_CODE (type) == REFERENCE_TYPE)
250 type = TREE_TYPE (type);
252 if (TREE_CODE (basetype) == TREE_VEC)
254 binfo = basetype;
255 basetype = BINFO_TYPE (binfo);
257 else
258 binfo = NULL_TREE;
260 /* Check the destructor call syntax. */
261 if (TREE_CODE (name) == BIT_NOT_EXPR)
263 /* We can get here if someone writes their destructor call like
264 `obj.NS::~T()'; this isn't really a scoped method call, so hand
265 it off. */
266 if (TREE_CODE (basetype) == NAMESPACE_DECL)
267 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
269 if (! check_dtor_name (basetype, name))
270 error ("qualified type `%T' does not match destructor name `~%T'",
271 basetype, TREE_OPERAND (name, 0));
273 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
274 that explicit ~int is caught in the parser; this deals with typedefs
275 and template parms. */
276 if (! IS_AGGR_TYPE (basetype))
278 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
279 error ("type of `%E' does not match destructor type `%T' (type was `%T')",
280 exp, basetype, type);
282 return cp_convert (void_type_node, exp);
286 if (TREE_CODE (basetype) == NAMESPACE_DECL)
288 error ("`%D' is a namespace", basetype);
289 return error_mark_node;
291 if (! is_aggr_type (basetype, 1))
292 return error_mark_node;
294 if (! IS_AGGR_TYPE (type))
296 error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
297 exp, type);
298 return error_mark_node;
301 if (! binfo)
303 binfo = lookup_base (type, basetype, ba_check, NULL);
304 if (binfo == error_mark_node)
305 return error_mark_node;
306 if (! binfo)
307 error_not_base_type (basetype, type);
310 if (binfo)
312 if (TREE_CODE (exp) == INDIRECT_REF)
314 decl = build_base_path (PLUS_EXPR,
315 build_unary_op (ADDR_EXPR, exp, 0),
316 binfo, 1);
317 decl = build_indirect_ref (decl, NULL);
319 else
320 decl = build_scoped_ref (exp, basetype);
322 /* Call to a destructor. */
323 if (TREE_CODE (name) == BIT_NOT_EXPR)
325 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
326 return cp_convert (void_type_node, exp);
328 return build_delete (TREE_TYPE (decl), decl,
329 sfk_complete_destructor,
330 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
334 /* Call to a method. */
335 return build_method_call (decl, name, parms, binfo,
336 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
338 return error_mark_node;
341 /* We want the address of a function or method. We avoid creating a
342 pointer-to-member function. */
344 tree
345 build_addr_func (function)
346 tree function;
348 tree type = TREE_TYPE (function);
350 /* We have to do these by hand to avoid real pointer to member
351 functions. */
352 if (TREE_CODE (type) == METHOD_TYPE)
354 tree addr;
356 type = build_pointer_type (type);
358 if (mark_addressable (function) == 0)
359 return error_mark_node;
361 addr = build1 (ADDR_EXPR, type, function);
363 /* Address of a static or external variable or function counts
364 as a constant */
365 if (staticp (function))
366 TREE_CONSTANT (addr) = 1;
368 function = addr;
370 else
371 function = default_conversion (function);
373 return function;
376 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
377 POINTER_TYPE to those. Note, pointer to member function types
378 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
380 tree
381 build_call (function, parms)
382 tree function, parms;
384 int is_constructor = 0;
385 int nothrow;
386 tree tmp;
387 tree decl;
388 tree result_type;
390 function = build_addr_func (function);
392 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
394 sorry ("unable to call pointer to member function here");
395 return error_mark_node;
398 result_type = TREE_TYPE (TREE_TYPE (TREE_TYPE (function)));
400 if (TREE_CODE (function) == ADDR_EXPR
401 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
402 decl = TREE_OPERAND (function, 0);
403 else
404 decl = NULL_TREE;
406 /* We check both the decl and the type; a function may be known not to
407 throw without being declared throw(). */
408 nothrow = ((decl && TREE_NOTHROW (decl))
409 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
411 if (decl && TREE_THIS_VOLATILE (decl))
412 current_function_returns_abnormally = 1;
414 if (decl && TREE_DEPRECATED (decl))
415 warn_deprecated_use (decl);
417 if (decl && DECL_CONSTRUCTOR_P (decl))
418 is_constructor = 1;
420 if (decl && ! TREE_USED (decl))
422 /* We invoke build_call directly for several library functions.
423 These may have been declared normally if we're building libgcc,
424 so we can't just check DECL_ARTIFICIAL. */
425 if (DECL_ARTIFICIAL (decl)
426 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
427 mark_used (decl);
428 else
429 abort ();
432 /* Don't pass empty class objects by value. This is useful
433 for tags in STL, which are used to control overload resolution.
434 We don't need to handle other cases of copying empty classes. */
435 if (! decl || ! DECL_BUILT_IN (decl))
436 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
437 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
438 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
440 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
441 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
442 TREE_VALUE (tmp), t);
445 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
446 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
447 TREE_TYPE (function) = result_type;
448 TREE_SIDE_EFFECTS (function) = 1;
449 TREE_NOTHROW (function) = nothrow;
451 return function;
454 /* Build something of the form ptr->method (args)
455 or object.method (args). This can also build
456 calls to constructors, and find friends.
458 Member functions always take their class variable
459 as a pointer.
461 INSTANCE is a class instance.
463 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
465 PARMS help to figure out what that NAME really refers to.
467 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
468 down to the real instance type to use for access checking. We need this
469 information to get protected accesses correct. This parameter is used
470 by build_member_call.
472 FLAGS is the logical disjunction of zero or more LOOKUP_
473 flags. See cp-tree.h for more info.
475 If this is all OK, calls build_function_call with the resolved
476 member function.
478 This function must also handle being called to perform
479 initialization, promotion/coercion of arguments, and
480 instantiation of default parameters.
482 Note that NAME may refer to an instance variable name. If
483 `operator()()' is defined for the type of that field, then we return
484 that result. */
486 #ifdef GATHER_STATISTICS
487 extern int n_build_method_call;
488 #endif
490 tree
491 build_method_call (instance, name, parms, basetype_path, flags)
492 tree instance, name, parms, basetype_path;
493 int flags;
495 tree basetype, instance_ptr;
497 #ifdef GATHER_STATISTICS
498 n_build_method_call++;
499 #endif
501 if (instance == error_mark_node
502 || name == error_mark_node
503 || parms == error_mark_node
504 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
505 return error_mark_node;
507 if (processing_template_decl)
509 /* We need to process template parm names here so that tsubst catches
510 them properly. Other type names can wait. */
511 if (TREE_CODE (name) == BIT_NOT_EXPR)
513 tree type = NULL_TREE;
515 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
516 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
517 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
518 type = TREE_TYPE (TREE_OPERAND (name, 0));
520 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
521 name = build_min_nt (BIT_NOT_EXPR, type);
524 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
527 if (TREE_CODE (name) == BIT_NOT_EXPR)
529 if (parms)
530 error ("destructors take no parameters");
531 basetype = TREE_TYPE (instance);
532 if (TREE_CODE (basetype) == REFERENCE_TYPE)
533 basetype = TREE_TYPE (basetype);
535 if (! check_dtor_name (basetype, name))
536 error
537 ("destructor name `~%T' does not match type `%T' of expression",
538 TREE_OPERAND (name, 0), basetype);
540 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
541 return cp_convert (void_type_node, instance);
542 instance = default_conversion (instance);
543 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
544 return build_delete (build_pointer_type (basetype),
545 instance_ptr, sfk_complete_destructor,
546 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
549 return build_new_method_call (instance, name, parms, basetype_path, flags);
552 /* New overloading code. */
554 struct z_candidate {
555 tree fn;
556 tree convs;
557 tree second_conv;
558 int viable;
559 tree basetype_path;
560 tree template;
561 tree warnings;
562 struct z_candidate *next;
565 #define IDENTITY_RANK 0
566 #define EXACT_RANK 1
567 #define PROMO_RANK 2
568 #define STD_RANK 3
569 #define PBOOL_RANK 4
570 #define USER_RANK 5
571 #define ELLIPSIS_RANK 6
572 #define BAD_RANK 7
574 #define ICS_RANK(NODE) \
575 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
576 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
577 : ICS_USER_FLAG (NODE) ? USER_RANK \
578 : ICS_STD_RANK (NODE))
580 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
582 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
583 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
584 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
585 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
587 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
588 should be created to hold the result of the conversion. */
589 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
591 #define USER_CONV_CAND(NODE) \
592 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
593 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
596 null_ptr_cst_p (t)
597 tree t;
599 /* [conv.ptr]
601 A null pointer constant is an integral constant expression
602 (_expr.const_) rvalue of integer type that evaluates to zero. */
603 if (t == null_node
604 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
605 return 1;
606 return 0;
610 /* Returns non-zero if PARMLIST consists of only default parms and/or
611 ellipsis. */
614 sufficient_parms_p (parmlist)
615 tree parmlist;
617 for (; parmlist && parmlist != void_list_node;
618 parmlist = TREE_CHAIN (parmlist))
619 if (!TREE_PURPOSE (parmlist))
620 return 0;
621 return 1;
624 static tree
625 build_conv (code, type, from)
626 enum tree_code code;
627 tree type, from;
629 tree t;
630 int rank = ICS_STD_RANK (from);
632 /* We can't use buildl1 here because CODE could be USER_CONV, which
633 takes two arguments. In that case, the caller is responsible for
634 filling in the second argument. */
635 t = make_node (code);
636 TREE_TYPE (t) = type;
637 TREE_OPERAND (t, 0) = from;
639 switch (code)
641 case PTR_CONV:
642 case PMEM_CONV:
643 case BASE_CONV:
644 case STD_CONV:
645 if (rank < STD_RANK)
646 rank = STD_RANK;
647 break;
649 case QUAL_CONV:
650 if (rank < EXACT_RANK)
651 rank = EXACT_RANK;
653 default:
654 break;
656 ICS_STD_RANK (t) = rank;
657 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
658 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
659 return t;
662 /* If T is a REFERENCE_TYPE return the type to which T refers.
663 Otherwise, return T itself. */
665 static tree
666 non_reference (t)
667 tree t;
669 if (TREE_CODE (t) == REFERENCE_TYPE)
670 t = TREE_TYPE (t);
671 return t;
674 tree
675 strip_top_quals (t)
676 tree t;
678 if (TREE_CODE (t) == ARRAY_TYPE)
679 return t;
680 return TYPE_MAIN_VARIANT (t);
683 /* Returns the standard conversion path (see [conv]) from type FROM to type
684 TO, if any. For proper handling of null pointer constants, you must
685 also pass the expression EXPR to convert from. */
687 static tree
688 standard_conversion (to, from, expr)
689 tree to, from, expr;
691 enum tree_code fcode, tcode;
692 tree conv;
693 int fromref = 0;
695 if (TREE_CODE (to) == REFERENCE_TYPE)
696 to = TREE_TYPE (to);
697 if (TREE_CODE (from) == REFERENCE_TYPE)
699 fromref = 1;
700 from = TREE_TYPE (from);
702 to = strip_top_quals (to);
703 from = strip_top_quals (from);
705 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
706 && expr && type_unknown_p (expr))
708 expr = instantiate_type (to, expr, tf_none);
709 if (expr == error_mark_node)
710 return NULL_TREE;
711 from = TREE_TYPE (expr);
714 fcode = TREE_CODE (from);
715 tcode = TREE_CODE (to);
717 conv = build1 (IDENTITY_CONV, from, expr);
719 if (fcode == FUNCTION_TYPE)
721 from = build_pointer_type (from);
722 fcode = TREE_CODE (from);
723 conv = build_conv (LVALUE_CONV, from, conv);
725 else if (fcode == ARRAY_TYPE)
727 from = build_pointer_type (TREE_TYPE (from));
728 fcode = TREE_CODE (from);
729 conv = build_conv (LVALUE_CONV, from, conv);
731 else if (fromref || (expr && lvalue_p (expr)))
732 conv = build_conv (RVALUE_CONV, from, conv);
734 /* Allow conversion between `__complex__' data types */
735 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
737 /* The standard conversion sequence to convert FROM to TO is
738 the standard conversion sequence to perform componentwise
739 conversion. */
740 tree part_conv = standard_conversion
741 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
743 if (part_conv)
745 conv = build_conv (TREE_CODE (part_conv), to, conv);
746 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
748 else
749 conv = NULL_TREE;
751 return conv;
754 if (same_type_p (from, to))
755 return conv;
757 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
758 && expr && null_ptr_cst_p (expr))
760 conv = build_conv (STD_CONV, to, conv);
762 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
763 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
765 /* For backwards brain damage compatibility, allow interconversion of
766 pointers and integers with a pedwarn. */
767 conv = build_conv (STD_CONV, to, conv);
768 ICS_BAD_FLAG (conv) = 1;
770 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE
771 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
773 /* For backwards brain damage compatibility, allow interconversion of
774 enums and integers with a pedwarn. */
775 conv = build_conv (STD_CONV, to, conv);
776 ICS_BAD_FLAG (conv) = 1;
778 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
780 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
781 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
783 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
784 TREE_TYPE (to)))
786 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
787 && ufcode != FUNCTION_TYPE)
789 from = build_pointer_type
790 (cp_build_qualified_type (void_type_node,
791 cp_type_quals (TREE_TYPE (from))));
792 conv = build_conv (PTR_CONV, from, conv);
794 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
796 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
797 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
798 tree binfo = lookup_base (tbase, fbase, ba_check, NULL);
800 if (binfo && !binfo_from_vbase (binfo)
801 && (same_type_ignoring_top_level_qualifiers_p
802 (TREE_TYPE (TREE_TYPE (from)),
803 TREE_TYPE (TREE_TYPE (to)))))
805 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
806 from = build_pointer_type (from);
807 conv = build_conv (PMEM_CONV, from, conv);
810 else if (IS_AGGR_TYPE (TREE_TYPE (from))
811 && IS_AGGR_TYPE (TREE_TYPE (to)))
813 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
815 from =
816 cp_build_qualified_type (TREE_TYPE (to),
817 cp_type_quals (TREE_TYPE (from)));
818 from = build_pointer_type (from);
819 conv = build_conv (PTR_CONV, from, conv);
823 if (same_type_p (from, to))
824 /* OK */;
825 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
826 conv = build_conv (QUAL_CONV, to, conv);
827 else if (expr && string_conv_p (to, expr, 0))
828 /* converting from string constant to char *. */
829 conv = build_conv (QUAL_CONV, to, conv);
830 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
832 conv = build_conv (PTR_CONV, to, conv);
833 ICS_BAD_FLAG (conv) = 1;
835 else
836 return 0;
838 from = to;
840 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
842 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
843 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
844 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
845 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
846 tree binfo = lookup_base (tbase, fbase, ba_check, NULL);
848 if (!binfo || binfo_from_vbase (binfo)
849 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
850 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
851 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
852 || cp_type_quals (fbase) != cp_type_quals (tbase))
853 return 0;
855 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
856 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
857 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
858 from = build_ptrmemfunc_type (build_pointer_type (from));
859 conv = build_conv (PMEM_CONV, from, conv);
861 else if (tcode == BOOLEAN_TYPE)
863 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
864 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
865 return 0;
867 conv = build_conv (STD_CONV, to, conv);
868 if (fcode == POINTER_TYPE
869 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
870 ICS_STD_RANK (conv) = PBOOL_RANK;
872 /* We don't check for ENUMERAL_TYPE here because there are no standard
873 conversions to enum type. */
874 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
875 || tcode == REAL_TYPE)
877 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
878 return 0;
879 conv = build_conv (STD_CONV, to, conv);
881 /* Give this a better rank if it's a promotion. */
882 if (to == type_promotes_to (from)
883 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
884 ICS_STD_RANK (conv) = PROMO_RANK;
886 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
887 && is_properly_derived_from (from, to))
889 if (TREE_CODE (conv) == RVALUE_CONV)
890 conv = TREE_OPERAND (conv, 0);
891 conv = build_conv (BASE_CONV, to, conv);
892 /* The derived-to-base conversion indicates the initialization
893 of a parameter with base type from an object of a derived
894 type. A temporary object is created to hold the result of
895 the conversion. */
896 NEED_TEMPORARY_P (conv) = 1;
898 else
899 return 0;
901 return conv;
904 /* Returns non-zero if T1 is reference-related to T2. */
906 static int
907 reference_related_p (t1, t2)
908 tree t1;
909 tree t2;
911 t1 = TYPE_MAIN_VARIANT (t1);
912 t2 = TYPE_MAIN_VARIANT (t2);
914 /* [dcl.init.ref]
916 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
917 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
918 of T2. */
919 return (same_type_p (t1, t2)
920 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
921 && DERIVED_FROM_P (t1, t2)));
924 /* Returns non-zero if T1 is reference-compatible with T2. */
926 static int
927 reference_compatible_p (t1, t2)
928 tree t1;
929 tree t2;
931 /* [dcl.init.ref]
933 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
934 reference-related to T2 and cv1 is the same cv-qualification as,
935 or greater cv-qualification than, cv2. */
936 return (reference_related_p (t1, t2)
937 && at_least_as_qualified_p (t1, t2));
940 /* Determine whether or not the EXPR (of class type S) can be
941 converted to T as in [over.match.ref]. */
943 static tree
944 convert_class_to_reference (t, s, expr)
945 tree t;
946 tree s;
947 tree expr;
949 tree conversions;
950 tree arglist;
951 tree conv;
952 struct z_candidate *candidates;
953 struct z_candidate *cand;
955 /* [over.match.ref]
957 Assuming that "cv1 T" is the underlying type of the reference
958 being initialized, and "cv S" is the type of the initializer
959 expression, with S a class type, the candidate functions are
960 selected as follows:
962 --The conversion functions of S and its base classes are
963 considered. Those that are not hidden within S and yield type
964 "reference to cv2 T2", where "cv1 T" is reference-compatible
965 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
967 The argument list has one argument, which is the initializer
968 expression. */
970 candidates = 0;
972 /* Conceptually, we should take the address of EXPR and put it in
973 the argument list. Unfortunately, however, that can result in
974 error messages, which we should not issue now because we are just
975 trying to find a conversion operator. Therefore, we use NULL,
976 cast to the appropriate type. */
977 arglist = build_int_2 (0, 0);
978 TREE_TYPE (arglist) = build_pointer_type (s);
979 arglist = build_tree_list (NULL_TREE, arglist);
981 for (conversions = lookup_conversions (s);
982 conversions;
983 conversions = TREE_CHAIN (conversions))
985 tree fns = TREE_VALUE (conversions);
987 for (; fns; fns = OVL_NEXT (fns))
989 tree f = OVL_CURRENT (fns);
990 tree t2 = TREE_TYPE (TREE_TYPE (f));
991 struct z_candidate *old_candidates = candidates;
993 /* If this is a template function, try to get an exact
994 match. */
995 if (TREE_CODE (f) == TEMPLATE_DECL)
997 candidates
998 = add_template_candidate (candidates,
999 f, s,
1000 NULL_TREE,
1001 arglist,
1002 build_reference_type (t),
1003 LOOKUP_NORMAL,
1004 DEDUCE_CONV);
1006 if (candidates != old_candidates)
1008 /* Now, see if the conversion function really returns
1009 an lvalue of the appropriate type. From the
1010 point of view of unification, simply returning an
1011 rvalue of the right type is good enough. */
1012 f = candidates->fn;
1013 t2 = TREE_TYPE (TREE_TYPE (f));
1014 if (TREE_CODE (t2) != REFERENCE_TYPE
1015 || !reference_compatible_p (t, TREE_TYPE (t2)))
1016 candidates = candidates->next;
1019 else if (TREE_CODE (t2) == REFERENCE_TYPE
1020 && reference_compatible_p (t, TREE_TYPE (t2)))
1021 candidates
1022 = add_function_candidate (candidates, f, s, arglist,
1023 LOOKUP_NORMAL);
1025 if (candidates != old_candidates)
1026 candidates->basetype_path = TYPE_BINFO (s);
1030 /* If none of the conversion functions worked out, let our caller
1031 know. */
1032 if (!any_viable (candidates))
1033 return NULL_TREE;
1035 candidates = splice_viable (candidates);
1036 cand = tourney (candidates);
1037 if (!cand)
1038 return NULL_TREE;
1040 conv = build1 (IDENTITY_CONV, s, expr);
1041 conv = build_conv (USER_CONV, TREE_TYPE (TREE_TYPE (cand->fn)),
1042 conv);
1043 TREE_OPERAND (conv, 1) = build_ptr_wrapper (cand);
1044 ICS_USER_FLAG (conv) = 1;
1045 if (cand->viable == -1)
1046 ICS_BAD_FLAG (conv) = 1;
1047 cand->second_conv = conv;
1049 return conv;
1052 /* A reference of the indicated TYPE is being bound directly to the
1053 expression represented by the implicit conversion sequence CONV.
1054 Return a conversion sequence for this binding. */
1056 static tree
1057 direct_reference_binding (type, conv)
1058 tree type;
1059 tree conv;
1061 tree t = TREE_TYPE (type);
1063 /* [over.ics.rank]
1065 When a parameter of reference type binds directly
1066 (_dcl.init.ref_) to an argument expression, the implicit
1067 conversion sequence is the identity conversion, unless the
1068 argument expression has a type that is a derived class of the
1069 parameter type, in which case the implicit conversion sequence is
1070 a derived-to-base Conversion.
1072 If the parameter binds directly to the result of applying a
1073 conversion function to the argument expression, the implicit
1074 conversion sequence is a user-defined conversion sequence
1075 (_over.ics.user_), with the second standard conversion sequence
1076 either an identity conversion or, if the conversion function
1077 returns an entity of a type that is a derived class of the
1078 parameter type, a derived-to-base conversion. */
1079 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1081 /* Represent the derived-to-base conversion. */
1082 conv = build_conv (BASE_CONV, t, conv);
1083 /* We will actually be binding to the base-class subobject in
1084 the derived class, so we mark this conversion appropriately.
1085 That way, convert_like knows not to generate a temporary. */
1086 NEED_TEMPORARY_P (conv) = 0;
1088 return build_conv (REF_BIND, type, conv);
1091 /* Returns the conversion path from type FROM to reference type TO for
1092 purposes of reference binding. For lvalue binding, either pass a
1093 reference type to FROM or an lvalue expression to EXPR. If the
1094 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1095 the conversion returned. */
1097 static tree
1098 reference_binding (rto, rfrom, expr, flags)
1099 tree rto, rfrom, expr;
1100 int flags;
1102 tree conv = NULL_TREE;
1103 tree to = TREE_TYPE (rto);
1104 tree from = rfrom;
1105 int related_p;
1106 int compatible_p;
1107 cp_lvalue_kind lvalue_p = clk_none;
1109 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1111 expr = instantiate_type (to, expr, tf_none);
1112 if (expr == error_mark_node)
1113 return NULL_TREE;
1114 from = TREE_TYPE (expr);
1117 if (TREE_CODE (from) == REFERENCE_TYPE)
1119 /* Anything with reference type is an lvalue. */
1120 lvalue_p = clk_ordinary;
1121 from = TREE_TYPE (from);
1123 else if (expr)
1124 lvalue_p = real_lvalue_p (expr);
1126 /* Figure out whether or not the types are reference-related and
1127 reference compatible. We have do do this after stripping
1128 references from FROM. */
1129 related_p = reference_related_p (to, from);
1130 compatible_p = reference_compatible_p (to, from);
1132 if (lvalue_p && compatible_p)
1134 /* [dcl.init.ref]
1136 If the initializer expression
1138 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1139 is reference-compatible with "cv2 T2,"
1141 the reference is bound directly to the initializer exprssion
1142 lvalue. */
1143 conv = build1 (IDENTITY_CONV, from, expr);
1144 conv = direct_reference_binding (rto, conv);
1145 if ((lvalue_p & clk_bitfield) != 0
1146 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1147 /* For the purposes of overload resolution, we ignore the fact
1148 this expression is a bitfield. (In particular,
1149 [over.ics.ref] says specifically that a function with a
1150 non-const reference parameter is viable even if the
1151 argument is a bitfield.)
1153 However, when we actually call the function we must create
1154 a temporary to which to bind the reference. If the
1155 reference is volatile, or isn't const, then we cannot make
1156 a temporary, so we just issue an error when the conversion
1157 actually occurs. */
1158 NEED_TEMPORARY_P (conv) = 1;
1159 return conv;
1161 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1163 /* [dcl.init.ref]
1165 If the initializer exprsesion
1167 -- has a class type (i.e., T2 is a class type) can be
1168 implicitly converted to an lvalue of type "cv3 T3," where
1169 "cv1 T1" is reference-compatible with "cv3 T3". (this
1170 conversion is selected by enumerating the applicable
1171 conversion functions (_over.match.ref_) and choosing the
1172 best one through overload resolution. (_over.match_).
1174 the reference is bound to the lvalue result of the conversion
1175 in the second case. */
1176 conv = convert_class_to_reference (to, from, expr);
1177 if (conv)
1178 return direct_reference_binding (rto, conv);
1181 /* From this point on, we conceptually need temporaries, even if we
1182 elide them. Only the cases above are "direct bindings". */
1183 if (flags & LOOKUP_NO_TEMP_BIND)
1184 return NULL_TREE;
1186 /* [over.ics.rank]
1188 When a parameter of reference type is not bound directly to an
1189 argument expression, the conversion sequence is the one required
1190 to convert the argument expression to the underlying type of the
1191 reference according to _over.best.ics_. Conceptually, this
1192 conversion sequence corresponds to copy-initializing a temporary
1193 of the underlying type with the argument expression. Any
1194 difference in top-level cv-qualification is subsumed by the
1195 initialization itself and does not constitute a conversion. */
1197 /* [dcl.init.ref]
1199 Otherwise, the reference shall be to a non-volatile const type. */
1200 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1201 return NULL_TREE;
1203 /* [dcl.init.ref]
1205 If the initializer expression is an rvalue, with T2 a class type,
1206 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1207 is bound in one of the following ways:
1209 -- The reference is bound to the object represented by the rvalue
1210 or to a sub-object within that object.
1212 In this case, the implicit conversion sequence is supposed to be
1213 same as we would obtain by generating a temporary. Fortunately,
1214 if the types are reference compatible, then this is either an
1215 identity conversion or the derived-to-base conversion, just as
1216 for direct binding. */
1217 if (CLASS_TYPE_P (from) && compatible_p)
1219 conv = build1 (IDENTITY_CONV, from, expr);
1220 return direct_reference_binding (rto, conv);
1223 /* [dcl.init.ref]
1225 Otherwise, a temporary of type "cv1 T1" is created and
1226 initialized from the initializer expression using the rules for a
1227 non-reference copy initialization. If T1 is reference-related to
1228 T2, cv1 must be the same cv-qualification as, or greater
1229 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1230 if (related_p && !at_least_as_qualified_p (to, from))
1231 return NULL_TREE;
1233 conv = implicit_conversion (to, from, expr, flags);
1234 if (!conv)
1235 return NULL_TREE;
1237 conv = build_conv (REF_BIND, rto, conv);
1238 /* This reference binding, unlike those above, requires the
1239 creation of a temporary. */
1240 NEED_TEMPORARY_P (conv) = 1;
1242 return conv;
1245 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1246 to type TO. The optional expression EXPR may affect the conversion.
1247 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1248 significant. */
1250 static tree
1251 implicit_conversion (to, from, expr, flags)
1252 tree to, from, expr;
1253 int flags;
1255 tree conv;
1256 struct z_candidate *cand;
1258 /* Resolve expressions like `A::p' that we thought might become
1259 pointers-to-members. */
1260 if (expr && TREE_CODE (expr) == OFFSET_REF)
1262 expr = resolve_offset_ref (expr);
1263 from = TREE_TYPE (expr);
1266 if (from == error_mark_node || to == error_mark_node
1267 || expr == error_mark_node)
1268 return NULL_TREE;
1270 /* Make sure both the FROM and TO types are complete so that
1271 user-defined conversions are available. */
1272 complete_type (from);
1273 complete_type (to);
1275 if (TREE_CODE (to) == REFERENCE_TYPE)
1276 conv = reference_binding (to, from, expr, flags);
1277 else
1278 conv = standard_conversion (to, from, expr);
1280 if (conv)
1282 else if (expr != NULL_TREE
1283 && (IS_AGGR_TYPE (from)
1284 || IS_AGGR_TYPE (to))
1285 && (flags & LOOKUP_NO_CONVERSION) == 0)
1287 cand = build_user_type_conversion_1
1288 (to, expr, LOOKUP_ONLYCONVERTING);
1289 if (cand)
1290 conv = cand->second_conv;
1292 /* We used to try to bind a reference to a temporary here, but that
1293 is now handled by the recursive call to this function at the end
1294 of reference_binding. */
1297 return conv;
1300 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1301 functions. */
1303 static struct z_candidate *
1304 add_candidate (candidates, fn, convs, viable)
1305 struct z_candidate *candidates;
1306 tree fn, convs;
1307 int viable;
1309 struct z_candidate *cand
1310 = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1312 cand->fn = fn;
1313 cand->convs = convs;
1314 cand->viable = viable;
1315 cand->next = candidates;
1317 return cand;
1320 /* Create an overload candidate for the function or method FN called with
1321 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1322 to implicit_conversion.
1324 CTYPE, if non-NULL, is the type we want to pretend this function
1325 comes from for purposes of overload resolution. */
1327 static struct z_candidate *
1328 add_function_candidate (candidates, fn, ctype, arglist, flags)
1329 struct z_candidate *candidates;
1330 tree fn, ctype, arglist;
1331 int flags;
1333 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1334 int i, len;
1335 tree convs;
1336 tree parmnode, argnode;
1337 int viable = 1;
1339 /* The `this', `in_chrg' and VTT arguments to constructors are not
1340 considered in overload resolution. */
1341 if (DECL_CONSTRUCTOR_P (fn))
1343 parmlist = skip_artificial_parms_for (fn, parmlist);
1344 arglist = skip_artificial_parms_for (fn, arglist);
1347 len = list_length (arglist);
1348 convs = make_tree_vec (len);
1350 /* 13.3.2 - Viable functions [over.match.viable]
1351 First, to be a viable function, a candidate function shall have enough
1352 parameters to agree in number with the arguments in the list.
1354 We need to check this first; otherwise, checking the ICSes might cause
1355 us to produce an ill-formed template instantiation. */
1357 parmnode = parmlist;
1358 for (i = 0; i < len; ++i)
1360 if (parmnode == NULL_TREE || parmnode == void_list_node)
1361 break;
1362 parmnode = TREE_CHAIN (parmnode);
1365 if (i < len && parmnode)
1366 viable = 0;
1368 /* Make sure there are default args for the rest of the parms. */
1369 else if (!sufficient_parms_p (parmnode))
1370 viable = 0;
1372 if (! viable)
1373 goto out;
1375 /* Second, for F to be a viable function, there shall exist for each
1376 argument an implicit conversion sequence that converts that argument
1377 to the corresponding parameter of F. */
1379 parmnode = parmlist;
1380 argnode = arglist;
1382 for (i = 0; i < len; ++i)
1384 tree arg = TREE_VALUE (argnode);
1385 tree argtype = lvalue_type (arg);
1386 tree t;
1387 int is_this;
1389 if (parmnode == void_list_node)
1390 break;
1392 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1393 && ! DECL_CONSTRUCTOR_P (fn));
1395 if (parmnode)
1397 tree parmtype = TREE_VALUE (parmnode);
1399 /* The type of the implicit object parameter ('this') for
1400 overload resolution is not always the same as for the
1401 function itself; conversion functions are considered to
1402 be members of the class being converted, and functions
1403 introduced by a using-declaration are considered to be
1404 members of the class that uses them.
1406 Since build_over_call ignores the ICS for the `this'
1407 parameter, we can just change the parm type. */
1408 if (ctype && is_this)
1410 parmtype
1411 = build_qualified_type (ctype,
1412 TYPE_QUALS (TREE_TYPE (parmtype)));
1413 parmtype = build_pointer_type (parmtype);
1416 t = implicit_conversion (parmtype, argtype, arg, flags);
1418 else
1420 t = build1 (IDENTITY_CONV, argtype, arg);
1421 ICS_ELLIPSIS_FLAG (t) = 1;
1424 if (t && is_this)
1425 ICS_THIS_FLAG (t) = 1;
1427 TREE_VEC_ELT (convs, i) = t;
1428 if (! t)
1430 viable = 0;
1431 break;
1434 if (ICS_BAD_FLAG (t))
1435 viable = -1;
1437 if (parmnode)
1438 parmnode = TREE_CHAIN (parmnode);
1439 argnode = TREE_CHAIN (argnode);
1442 out:
1443 return add_candidate (candidates, fn, convs, viable);
1446 /* Create an overload candidate for the conversion function FN which will
1447 be invoked for expression OBJ, producing a pointer-to-function which
1448 will in turn be called with the argument list ARGLIST, and add it to
1449 CANDIDATES. FLAGS is passed on to implicit_conversion.
1451 Actually, we don't really care about FN; we care about the type it
1452 converts to. There may be multiple conversion functions that will
1453 convert to that type, and we rely on build_user_type_conversion_1 to
1454 choose the best one; so when we create our candidate, we record the type
1455 instead of the function. */
1457 static struct z_candidate *
1458 add_conv_candidate (candidates, fn, obj, arglist)
1459 struct z_candidate *candidates;
1460 tree fn, obj, arglist;
1462 tree totype = TREE_TYPE (TREE_TYPE (fn));
1463 int i, len, viable, flags;
1464 tree parmlist, convs, parmnode, argnode;
1466 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1467 parmlist = TREE_TYPE (parmlist);
1468 parmlist = TYPE_ARG_TYPES (parmlist);
1470 len = list_length (arglist) + 1;
1471 convs = make_tree_vec (len);
1472 parmnode = parmlist;
1473 argnode = arglist;
1474 viable = 1;
1475 flags = LOOKUP_NORMAL;
1477 /* Don't bother looking up the same type twice. */
1478 if (candidates && candidates->fn == totype)
1479 return candidates;
1481 for (i = 0; i < len; ++i)
1483 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1484 tree argtype = lvalue_type (arg);
1485 tree t;
1487 if (i == 0)
1488 t = implicit_conversion (totype, argtype, arg, flags);
1489 else if (parmnode == void_list_node)
1490 break;
1491 else if (parmnode)
1492 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1493 else
1495 t = build1 (IDENTITY_CONV, argtype, arg);
1496 ICS_ELLIPSIS_FLAG (t) = 1;
1499 TREE_VEC_ELT (convs, i) = t;
1500 if (! t)
1501 break;
1503 if (ICS_BAD_FLAG (t))
1504 viable = -1;
1506 if (i == 0)
1507 continue;
1509 if (parmnode)
1510 parmnode = TREE_CHAIN (parmnode);
1511 argnode = TREE_CHAIN (argnode);
1514 if (i < len)
1515 viable = 0;
1517 if (!sufficient_parms_p (parmnode))
1518 viable = 0;
1520 return add_candidate (candidates, totype, convs, viable);
1523 static struct z_candidate *
1524 build_builtin_candidate (candidates, fnname, type1, type2,
1525 args, argtypes, flags)
1526 struct z_candidate *candidates;
1527 tree fnname, type1, type2, *args, *argtypes;
1528 int flags;
1531 tree t, convs;
1532 int viable = 1, i;
1533 tree types[2];
1535 types[0] = type1;
1536 types[1] = type2;
1538 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1540 for (i = 0; i < 2; ++i)
1542 if (! args[i])
1543 break;
1545 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1546 if (! t)
1548 viable = 0;
1549 /* We need something for printing the candidate. */
1550 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1552 else if (ICS_BAD_FLAG (t))
1553 viable = 0;
1554 TREE_VEC_ELT (convs, i) = t;
1557 /* For COND_EXPR we rearranged the arguments; undo that now. */
1558 if (args[2])
1560 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1561 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1562 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1563 if (t)
1564 TREE_VEC_ELT (convs, 0) = t;
1565 else
1566 viable = 0;
1569 return add_candidate (candidates, fnname, convs, viable);
1572 static int
1573 is_complete (t)
1574 tree t;
1576 return COMPLETE_TYPE_P (complete_type (t));
1579 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1581 static int
1582 promoted_arithmetic_type_p (type)
1583 tree type;
1585 /* [over.built]
1587 In this section, the term promoted integral type is used to refer
1588 to those integral types which are preserved by integral promotion
1589 (including e.g. int and long but excluding e.g. char).
1590 Similarly, the term promoted arithmetic type refers to promoted
1591 integral types plus floating types. */
1592 return ((INTEGRAL_TYPE_P (type)
1593 && same_type_p (type_promotes_to (type), type))
1594 || TREE_CODE (type) == REAL_TYPE);
1597 /* Create any builtin operator overload candidates for the operator in
1598 question given the converted operand types TYPE1 and TYPE2. The other
1599 args are passed through from add_builtin_candidates to
1600 build_builtin_candidate.
1602 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1603 If CODE is requires candidates operands of the same type of the kind
1604 of which TYPE1 and TYPE2 are, we add both candidates
1605 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1607 static struct z_candidate *
1608 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1609 args, argtypes, flags)
1610 struct z_candidate *candidates;
1611 enum tree_code code, code2;
1612 tree fnname, type1, type2, *args, *argtypes;
1613 int flags;
1615 switch (code)
1617 case POSTINCREMENT_EXPR:
1618 case POSTDECREMENT_EXPR:
1619 args[1] = integer_zero_node;
1620 type2 = integer_type_node;
1621 break;
1622 default:
1623 break;
1626 switch (code)
1629 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1630 and VQ is either volatile or empty, there exist candidate operator
1631 functions of the form
1632 VQ T& operator++(VQ T&);
1633 T operator++(VQ T&, int);
1634 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1635 type other than bool, and VQ is either volatile or empty, there exist
1636 candidate operator functions of the form
1637 VQ T& operator--(VQ T&);
1638 T operator--(VQ T&, int);
1639 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1640 complete object type, and VQ is either volatile or empty, there exist
1641 candidate operator functions of the form
1642 T*VQ& operator++(T*VQ&);
1643 T*VQ& operator--(T*VQ&);
1644 T* operator++(T*VQ&, int);
1645 T* operator--(T*VQ&, int); */
1647 case POSTDECREMENT_EXPR:
1648 case PREDECREMENT_EXPR:
1649 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1650 return candidates;
1651 case POSTINCREMENT_EXPR:
1652 case PREINCREMENT_EXPR:
1653 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1655 type1 = build_reference_type (type1);
1656 break;
1658 return candidates;
1660 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1661 exist candidate operator functions of the form
1663 T& operator*(T*);
1665 8 For every function type T, there exist candidate operator functions of
1666 the form
1667 T& operator*(T*); */
1669 case INDIRECT_REF:
1670 if (TREE_CODE (type1) == POINTER_TYPE
1671 && (TYPE_PTROB_P (type1)
1672 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1673 break;
1674 return candidates;
1676 /* 9 For every type T, there exist candidate operator functions of the form
1677 T* operator+(T*);
1679 10For every promoted arithmetic type T, there exist candidate operator
1680 functions of the form
1681 T operator+(T);
1682 T operator-(T); */
1684 case CONVERT_EXPR: /* unary + */
1685 if (TREE_CODE (type1) == POINTER_TYPE
1686 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1687 break;
1688 case NEGATE_EXPR:
1689 if (ARITHMETIC_TYPE_P (type1))
1690 break;
1691 return candidates;
1693 /* 11For every promoted integral type T, there exist candidate operator
1694 functions of the form
1695 T operator~(T); */
1697 case BIT_NOT_EXPR:
1698 if (INTEGRAL_TYPE_P (type1))
1699 break;
1700 return candidates;
1702 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1703 is the same type as C2 or is a derived class of C2, T is a complete
1704 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1705 there exist candidate operator functions of the form
1706 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1707 where CV12 is the union of CV1 and CV2. */
1709 case MEMBER_REF:
1710 if (TREE_CODE (type1) == POINTER_TYPE
1711 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1713 tree c1 = TREE_TYPE (type1);
1714 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1715 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1716 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1718 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1719 && (TYPE_PTRMEMFUNC_P (type2)
1720 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1721 break;
1723 return candidates;
1725 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1726 didate operator functions of the form
1727 LR operator*(L, R);
1728 LR operator/(L, R);
1729 LR operator+(L, R);
1730 LR operator-(L, R);
1731 bool operator<(L, R);
1732 bool operator>(L, R);
1733 bool operator<=(L, R);
1734 bool operator>=(L, R);
1735 bool operator==(L, R);
1736 bool operator!=(L, R);
1737 where LR is the result of the usual arithmetic conversions between
1738 types L and R.
1740 14For every pair of types T and I, where T is a cv-qualified or cv-
1741 unqualified complete object type and I is a promoted integral type,
1742 there exist candidate operator functions of the form
1743 T* operator+(T*, I);
1744 T& operator[](T*, I);
1745 T* operator-(T*, I);
1746 T* operator+(I, T*);
1747 T& operator[](I, T*);
1749 15For every T, where T is a pointer to complete object type, there exist
1750 candidate operator functions of the form112)
1751 ptrdiff_t operator-(T, T);
1753 16For every pointer or enumeration type T, there exist candidate operator
1754 functions of the form
1755 bool operator<(T, T);
1756 bool operator>(T, T);
1757 bool operator<=(T, T);
1758 bool operator>=(T, T);
1759 bool operator==(T, T);
1760 bool operator!=(T, T);
1762 17For every pointer to member type T, there exist candidate operator
1763 functions of the form
1764 bool operator==(T, T);
1765 bool operator!=(T, T); */
1767 case MINUS_EXPR:
1768 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1769 break;
1770 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1772 type2 = ptrdiff_type_node;
1773 break;
1775 case MULT_EXPR:
1776 case TRUNC_DIV_EXPR:
1777 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1778 break;
1779 return candidates;
1781 case EQ_EXPR:
1782 case NE_EXPR:
1783 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1784 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1785 break;
1786 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1787 && null_ptr_cst_p (args[1]))
1789 type2 = type1;
1790 break;
1792 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1793 && null_ptr_cst_p (args[0]))
1795 type1 = type2;
1796 break;
1798 /* FALLTHROUGH */
1799 case LT_EXPR:
1800 case GT_EXPR:
1801 case LE_EXPR:
1802 case GE_EXPR:
1803 case MAX_EXPR:
1804 case MIN_EXPR:
1805 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1806 break;
1807 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1808 break;
1809 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1810 break;
1811 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1813 type2 = type1;
1814 break;
1816 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1818 type1 = type2;
1819 break;
1821 return candidates;
1823 case PLUS_EXPR:
1824 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1825 break;
1826 case ARRAY_REF:
1827 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1829 type1 = ptrdiff_type_node;
1830 break;
1832 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1834 type2 = ptrdiff_type_node;
1835 break;
1837 return candidates;
1839 /* 18For every pair of promoted integral types L and R, there exist candi-
1840 date operator functions of the form
1841 LR operator%(L, R);
1842 LR operator&(L, R);
1843 LR operator^(L, R);
1844 LR operator|(L, R);
1845 L operator<<(L, R);
1846 L operator>>(L, R);
1847 where LR is the result of the usual arithmetic conversions between
1848 types L and R. */
1850 case TRUNC_MOD_EXPR:
1851 case BIT_AND_EXPR:
1852 case BIT_IOR_EXPR:
1853 case BIT_XOR_EXPR:
1854 case LSHIFT_EXPR:
1855 case RSHIFT_EXPR:
1856 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1857 break;
1858 return candidates;
1860 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1861 type, VQ is either volatile or empty, and R is a promoted arithmetic
1862 type, there exist candidate operator functions of the form
1863 VQ L& operator=(VQ L&, R);
1864 VQ L& operator*=(VQ L&, R);
1865 VQ L& operator/=(VQ L&, R);
1866 VQ L& operator+=(VQ L&, R);
1867 VQ L& operator-=(VQ L&, R);
1869 20For every pair T, VQ), where T is any type and VQ is either volatile
1870 or empty, there exist candidate operator functions of the form
1871 T*VQ& operator=(T*VQ&, T*);
1873 21For every pair T, VQ), where T is a pointer to member type and VQ is
1874 either volatile or empty, there exist candidate operator functions of
1875 the form
1876 VQ T& operator=(VQ T&, T);
1878 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1879 unqualified complete object type, VQ is either volatile or empty, and
1880 I is a promoted integral type, there exist candidate operator func-
1881 tions of the form
1882 T*VQ& operator+=(T*VQ&, I);
1883 T*VQ& operator-=(T*VQ&, I);
1885 23For every triple L, VQ, R), where L is an integral or enumeration
1886 type, VQ is either volatile or empty, and R is a promoted integral
1887 type, there exist candidate operator functions of the form
1889 VQ L& operator%=(VQ L&, R);
1890 VQ L& operator<<=(VQ L&, R);
1891 VQ L& operator>>=(VQ L&, R);
1892 VQ L& operator&=(VQ L&, R);
1893 VQ L& operator^=(VQ L&, R);
1894 VQ L& operator|=(VQ L&, R); */
1896 case MODIFY_EXPR:
1897 switch (code2)
1899 case PLUS_EXPR:
1900 case MINUS_EXPR:
1901 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1903 type2 = ptrdiff_type_node;
1904 break;
1906 case MULT_EXPR:
1907 case TRUNC_DIV_EXPR:
1908 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1909 break;
1910 return candidates;
1912 case TRUNC_MOD_EXPR:
1913 case BIT_AND_EXPR:
1914 case BIT_IOR_EXPR:
1915 case BIT_XOR_EXPR:
1916 case LSHIFT_EXPR:
1917 case RSHIFT_EXPR:
1918 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1919 break;
1920 return candidates;
1922 case NOP_EXPR:
1923 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1924 break;
1925 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1926 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1927 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1928 || ((TYPE_PTRMEMFUNC_P (type1)
1929 || TREE_CODE (type1) == POINTER_TYPE)
1930 && null_ptr_cst_p (args[1])))
1932 type2 = type1;
1933 break;
1935 return candidates;
1937 default:
1938 abort ();
1940 type1 = build_reference_type (type1);
1941 break;
1943 case COND_EXPR:
1944 /* [over.built]
1946 For every pair of promoted arithmetic types L and R, there
1947 exist candidate operator functions of the form
1949 LR operator?(bool, L, R);
1951 where LR is the result of the usual arithmetic conversions
1952 between types L and R.
1954 For every type T, where T is a pointer or pointer-to-member
1955 type, there exist candidate operator functions of the form T
1956 operator?(bool, T, T); */
1958 if (promoted_arithmetic_type_p (type1)
1959 && promoted_arithmetic_type_p (type2))
1960 /* That's OK. */
1961 break;
1963 /* Otherwise, the types should be pointers. */
1964 if (!(TREE_CODE (type1) == POINTER_TYPE
1965 || TYPE_PTRMEM_P (type1)
1966 || TYPE_PTRMEMFUNC_P (type1))
1967 || !(TREE_CODE (type2) == POINTER_TYPE
1968 || TYPE_PTRMEM_P (type2)
1969 || TYPE_PTRMEMFUNC_P (type2)))
1970 return candidates;
1972 /* We don't check that the two types are the same; the logic
1973 below will actually create two candidates; one in which both
1974 parameter types are TYPE1, and one in which both parameter
1975 types are TYPE2. */
1976 break;
1978 /* These arguments do not make for a legal overloaded operator. */
1979 return candidates;
1981 default:
1982 abort ();
1985 /* If we're dealing with two pointer types or two enumeral types,
1986 we need candidates for both of them. */
1987 if (type2 && !same_type_p (type1, type2)
1988 && TREE_CODE (type1) == TREE_CODE (type2)
1989 && (TREE_CODE (type1) == REFERENCE_TYPE
1990 || (TREE_CODE (type1) == POINTER_TYPE
1991 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1992 || TYPE_PTRMEMFUNC_P (type1)
1993 || IS_AGGR_TYPE (type1)
1994 || TREE_CODE (type1) == ENUMERAL_TYPE))
1996 candidates = build_builtin_candidate
1997 (candidates, fnname, type1, type1, args, argtypes, flags);
1998 return build_builtin_candidate
1999 (candidates, fnname, type2, type2, args, argtypes, flags);
2002 return build_builtin_candidate
2003 (candidates, fnname, type1, type2, args, argtypes, flags);
2006 tree
2007 type_decays_to (type)
2008 tree type;
2010 if (TREE_CODE (type) == ARRAY_TYPE)
2011 return build_pointer_type (TREE_TYPE (type));
2012 if (TREE_CODE (type) == FUNCTION_TYPE)
2013 return build_pointer_type (type);
2014 return type;
2017 /* There are three conditions of builtin candidates:
2019 1) bool-taking candidates. These are the same regardless of the input.
2020 2) pointer-pair taking candidates. These are generated for each type
2021 one of the input types converts to.
2022 3) arithmetic candidates. According to the standard, we should generate
2023 all of these, but I'm trying not to...
2025 Here we generate a superset of the possible candidates for this particular
2026 case. That is a subset of the full set the standard defines, plus some
2027 other cases which the standard disallows. add_builtin_candidate will
2028 filter out the illegal set. */
2030 static struct z_candidate *
2031 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
2032 struct z_candidate *candidates;
2033 enum tree_code code, code2;
2034 tree fnname, *args;
2035 int flags;
2037 int ref1, i;
2038 int enum_p = 0;
2039 tree type, argtypes[3];
2040 /* TYPES[i] is the set of possible builtin-operator parameter types
2041 we will consider for the Ith argument. These are represented as
2042 a TREE_LIST; the TREE_VALUE of each node is the potential
2043 parameter type. */
2044 tree types[2];
2046 for (i = 0; i < 3; ++i)
2048 if (args[i])
2049 argtypes[i] = lvalue_type (args[i]);
2050 else
2051 argtypes[i] = NULL_TREE;
2054 switch (code)
2056 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2057 and VQ is either volatile or empty, there exist candidate operator
2058 functions of the form
2059 VQ T& operator++(VQ T&); */
2061 case POSTINCREMENT_EXPR:
2062 case PREINCREMENT_EXPR:
2063 case POSTDECREMENT_EXPR:
2064 case PREDECREMENT_EXPR:
2065 case MODIFY_EXPR:
2066 ref1 = 1;
2067 break;
2069 /* 24There also exist candidate operator functions of the form
2070 bool operator!(bool);
2071 bool operator&&(bool, bool);
2072 bool operator||(bool, bool); */
2074 case TRUTH_NOT_EXPR:
2075 return build_builtin_candidate
2076 (candidates, fnname, boolean_type_node,
2077 NULL_TREE, args, argtypes, flags);
2079 case TRUTH_ORIF_EXPR:
2080 case TRUTH_ANDIF_EXPR:
2081 return build_builtin_candidate
2082 (candidates, fnname, boolean_type_node,
2083 boolean_type_node, args, argtypes, flags);
2085 case ADDR_EXPR:
2086 case COMPOUND_EXPR:
2087 case COMPONENT_REF:
2088 return candidates;
2090 case COND_EXPR:
2091 case EQ_EXPR:
2092 case NE_EXPR:
2093 case LT_EXPR:
2094 case LE_EXPR:
2095 case GT_EXPR:
2096 case GE_EXPR:
2097 enum_p = 1;
2098 /* FALLTHROUGH */
2100 default:
2101 ref1 = 0;
2104 types[0] = types[1] = NULL_TREE;
2106 for (i = 0; i < 2; ++i)
2108 if (! args[i])
2110 else if (IS_AGGR_TYPE (argtypes[i]))
2112 tree convs;
2114 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2115 return candidates;
2117 convs = lookup_conversions (argtypes[i]);
2119 if (code == COND_EXPR)
2121 if (real_lvalue_p (args[i]))
2122 types[i] = tree_cons
2123 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2125 types[i] = tree_cons
2126 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2129 else if (! convs)
2130 return candidates;
2132 for (; convs; convs = TREE_CHAIN (convs))
2134 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2136 if (i == 0 && ref1
2137 && (TREE_CODE (type) != REFERENCE_TYPE
2138 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2139 continue;
2141 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2142 types[i] = tree_cons (NULL_TREE, type, types[i]);
2144 type = non_reference (type);
2145 if (i != 0 || ! ref1)
2147 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2148 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2149 types[i] = tree_cons (NULL_TREE, type, types[i]);
2150 if (INTEGRAL_TYPE_P (type))
2151 type = type_promotes_to (type);
2154 if (! value_member (type, types[i]))
2155 types[i] = tree_cons (NULL_TREE, type, types[i]);
2158 else
2160 if (code == COND_EXPR && real_lvalue_p (args[i]))
2161 types[i] = tree_cons
2162 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2163 type = non_reference (argtypes[i]);
2164 if (i != 0 || ! ref1)
2166 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2167 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2168 types[i] = tree_cons (NULL_TREE, type, types[i]);
2169 if (INTEGRAL_TYPE_P (type))
2170 type = type_promotes_to (type);
2172 types[i] = tree_cons (NULL_TREE, type, types[i]);
2176 /* Run through the possible parameter types of both arguments,
2177 creating candidates with those parameter types. */
2178 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2180 if (types[1])
2181 for (type = types[1]; type; type = TREE_CHAIN (type))
2182 candidates = add_builtin_candidate
2183 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2184 TREE_VALUE (type), args, argtypes, flags);
2185 else
2186 candidates = add_builtin_candidate
2187 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2188 NULL_TREE, args, argtypes, flags);
2191 return candidates;
2195 /* If TMPL can be successfully instantiated as indicated by
2196 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2198 TMPL is the template. EXPLICIT_TARGS are any explicit template
2199 arguments. ARGLIST is the arguments provided at the call-site.
2200 The RETURN_TYPE is the desired type for conversion operators. If
2201 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2202 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2203 add_conv_candidate. */
2205 static struct z_candidate*
2206 add_template_candidate_real (candidates, tmpl, ctype, explicit_targs,
2207 arglist, return_type, flags,
2208 obj, strict)
2209 struct z_candidate *candidates;
2210 tree tmpl, ctype, explicit_targs, arglist, return_type;
2211 int flags;
2212 tree obj;
2213 unification_kind_t strict;
2215 int ntparms = DECL_NTPARMS (tmpl);
2216 tree targs = make_tree_vec (ntparms);
2217 tree args_without_in_chrg = arglist;
2218 struct z_candidate *cand;
2219 int i;
2220 tree fn;
2222 /* We don't do deduction on the in-charge parameter, the VTT
2223 parameter or 'this'. */
2224 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2225 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2227 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2228 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2229 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2230 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2232 i = fn_type_unification (tmpl, explicit_targs, targs,
2233 args_without_in_chrg,
2234 return_type, strict, -1);
2236 if (i != 0)
2237 return candidates;
2239 fn = instantiate_template (tmpl, targs);
2240 if (fn == error_mark_node)
2241 return candidates;
2243 if (obj != NULL_TREE)
2244 /* Aha, this is a conversion function. */
2245 cand = add_conv_candidate (candidates, fn, obj, arglist);
2246 else
2247 cand = add_function_candidate (candidates, fn, ctype,
2248 arglist, flags);
2249 if (DECL_TI_TEMPLATE (fn) != tmpl)
2250 /* This situation can occur if a member template of a template
2251 class is specialized. Then, instantiate_template might return
2252 an instantiation of the specialization, in which case the
2253 DECL_TI_TEMPLATE field will point at the original
2254 specialization. For example:
2256 template <class T> struct S { template <class U> void f(U);
2257 template <> void f(int) {}; };
2258 S<double> sd;
2259 sd.f(3);
2261 Here, TMPL will be template <class U> S<double>::f(U).
2262 And, instantiate template will give us the specialization
2263 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2264 for this will point at template <class T> template <> S<T>::f(int),
2265 so that we can find the definition. For the purposes of
2266 overload resolution, however, we want the original TMPL. */
2267 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2268 else
2269 cand->template = DECL_TEMPLATE_INFO (fn);
2271 return cand;
2275 static struct z_candidate *
2276 add_template_candidate (candidates, tmpl, ctype, explicit_targs,
2277 arglist, return_type, flags, strict)
2278 struct z_candidate *candidates;
2279 tree tmpl, ctype, explicit_targs, arglist, return_type;
2280 int flags;
2281 unification_kind_t strict;
2283 return
2284 add_template_candidate_real (candidates, tmpl, ctype,
2285 explicit_targs, arglist, return_type, flags,
2286 NULL_TREE, strict);
2290 static struct z_candidate *
2291 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2292 struct z_candidate *candidates;
2293 tree tmpl, obj, arglist, return_type;
2295 return
2296 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2297 arglist, return_type, 0, obj, DEDUCE_CONV);
2301 static int
2302 any_viable (cands)
2303 struct z_candidate *cands;
2305 for (; cands; cands = cands->next)
2306 if (pedantic ? cands->viable == 1 : cands->viable)
2307 return 1;
2308 return 0;
2311 static struct z_candidate *
2312 splice_viable (cands)
2313 struct z_candidate *cands;
2315 struct z_candidate **p = &cands;
2317 for (; *p; )
2319 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2320 p = &((*p)->next);
2321 else
2322 *p = (*p)->next;
2325 return cands;
2328 static tree
2329 build_this (obj)
2330 tree obj;
2332 /* Fix this to work on non-lvalues. */
2333 return build_unary_op (ADDR_EXPR, obj, 0);
2336 static void
2337 print_z_candidates (candidates)
2338 struct z_candidate *candidates;
2340 const char *str = "candidates are:";
2341 for (; candidates; candidates = candidates->next)
2343 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2345 if (TREE_VEC_LENGTH (candidates->convs) == 3)
2346 error ("%s %D(%T, %T, %T) <built-in>", str, candidates->fn,
2347 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2348 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2349 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2350 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2351 error ("%s %D(%T, %T) <built-in>", str, candidates->fn,
2352 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2353 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2354 else
2355 error ("%s %D(%T) <built-in>", str, candidates->fn,
2356 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2358 else if (TYPE_P (candidates->fn))
2359 error ("%s %T <conversion>", str, candidates->fn);
2360 else
2361 cp_error_at ("%s %+#D%s", str, candidates->fn,
2362 candidates->viable == -1 ? " <near match>" : "");
2363 str = " ";
2367 /* Returns the best overload candidate to perform the requested
2368 conversion. This function is used for three the overloading situations
2369 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2370 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2371 per [dcl.init.ref], so we ignore temporary bindings. */
2373 static struct z_candidate *
2374 build_user_type_conversion_1 (totype, expr, flags)
2375 tree totype, expr;
2376 int flags;
2378 struct z_candidate *candidates, *cand;
2379 tree fromtype = TREE_TYPE (expr);
2380 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2381 tree args = NULL_TREE;
2382 tree templates = NULL_TREE;
2384 /* We represent conversion within a hierarchy using RVALUE_CONV and
2385 BASE_CONV, as specified by [over.best.ics]; these become plain
2386 constructor calls, as specified in [dcl.init]. */
2387 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2388 || !DERIVED_FROM_P (totype, fromtype), 20011226);
2390 if (IS_AGGR_TYPE (totype))
2391 ctors = lookup_fnfields (TYPE_BINFO (totype),
2392 complete_ctor_identifier,
2395 if (IS_AGGR_TYPE (fromtype))
2396 convs = lookup_conversions (fromtype);
2398 candidates = 0;
2399 flags |= LOOKUP_NO_CONVERSION;
2401 if (ctors)
2403 tree t;
2405 ctors = TREE_VALUE (ctors);
2407 t = build_int_2 (0, 0);
2408 TREE_TYPE (t) = build_pointer_type (totype);
2409 args = build_tree_list (NULL_TREE, expr);
2410 /* We should never try to call the abstract or base constructor
2411 from here. */
2412 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2413 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2414 20011226);
2415 args = tree_cons (NULL_TREE, t, args);
2417 for (; ctors; ctors = OVL_NEXT (ctors))
2419 tree ctor = OVL_CURRENT (ctors);
2420 if (DECL_NONCONVERTING_P (ctor))
2421 continue;
2423 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2425 templates = tree_cons (NULL_TREE, ctor, templates);
2426 candidates =
2427 add_template_candidate (candidates, ctor, totype,
2428 NULL_TREE, args, NULL_TREE, flags,
2429 DEDUCE_CALL);
2431 else
2432 candidates = add_function_candidate (candidates, ctor, totype,
2433 args, flags);
2435 if (candidates)
2437 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2438 candidates->basetype_path = TYPE_BINFO (totype);
2442 if (convs)
2443 args = build_tree_list (NULL_TREE, build_this (expr));
2445 for (; convs; convs = TREE_CHAIN (convs))
2447 tree fns = TREE_VALUE (convs);
2448 int convflags = LOOKUP_NO_CONVERSION;
2449 tree ics;
2451 /* If we are called to convert to a reference type, we are trying to
2452 find an lvalue binding, so don't even consider temporaries. If
2453 we don't find an lvalue binding, the caller will try again to
2454 look for a temporary binding. */
2455 if (TREE_CODE (totype) == REFERENCE_TYPE)
2456 convflags |= LOOKUP_NO_TEMP_BIND;
2458 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2459 ics = implicit_conversion
2460 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2461 else
2462 /* We can't compute this yet. */
2463 ics = error_mark_node;
2465 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2466 /* ignore the near match. */;
2467 else if (ics)
2468 for (; fns; fns = OVL_NEXT (fns))
2470 tree fn = OVL_CURRENT (fns);
2471 struct z_candidate *old_candidates = candidates;
2473 /* [over.match.funcs] For conversion functions, the function is
2474 considered to be a member of the class of the implicit object
2475 argument for the purpose of defining the type of the implicit
2476 object parameter.
2478 So we pass fromtype as CTYPE to add_*_candidate. */
2480 if (TREE_CODE (fn) == TEMPLATE_DECL)
2482 templates = tree_cons (NULL_TREE, fn, templates);
2483 candidates =
2484 add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2485 args, totype, flags,
2486 DEDUCE_CONV);
2488 else
2489 candidates = add_function_candidate (candidates, fn, fromtype,
2490 args, flags);
2492 if (candidates != old_candidates)
2494 if (TREE_CODE (fn) == TEMPLATE_DECL)
2495 ics = implicit_conversion
2496 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2497 0, convflags);
2499 candidates->second_conv = ics;
2500 candidates->basetype_path = TYPE_BINFO (fromtype);
2502 if (ics == NULL_TREE)
2503 candidates->viable = 0;
2504 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2505 candidates->viable = -1;
2510 if (! any_viable (candidates))
2512 #if 0
2513 if (flags & LOOKUP_COMPLAIN)
2515 if (candidates && ! candidates->next)
2516 /* say why this one won't work or try to be loose */;
2517 else
2518 error ("no viable candidates");
2520 #endif
2522 return 0;
2525 candidates = splice_viable (candidates);
2526 cand = tourney (candidates);
2528 if (cand == 0)
2530 if (flags & LOOKUP_COMPLAIN)
2532 error ("conversion from `%T' to `%T' is ambiguous",
2533 fromtype, totype);
2534 print_z_candidates (candidates);
2537 cand = candidates; /* any one will do */
2538 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2539 ICS_USER_FLAG (cand->second_conv) = 1;
2540 ICS_BAD_FLAG (cand->second_conv) = 1;
2542 return cand;
2545 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2546 p = &(TREE_OPERAND (*p, 0));
2548 *p = build
2549 (USER_CONV,
2550 (DECL_CONSTRUCTOR_P (cand->fn)
2551 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2552 expr, build_ptr_wrapper (cand));
2554 ICS_USER_FLAG (cand->second_conv) = ICS_USER_FLAG (*p) = 1;
2555 if (cand->viable == -1)
2556 ICS_BAD_FLAG (cand->second_conv) = ICS_BAD_FLAG (*p) = 1;
2558 return cand;
2561 tree
2562 build_user_type_conversion (totype, expr, flags)
2563 tree totype, expr;
2564 int flags;
2566 struct z_candidate *cand
2567 = build_user_type_conversion_1 (totype, expr, flags);
2569 if (cand)
2571 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2572 return error_mark_node;
2573 return convert_from_reference (convert_like (cand->second_conv, expr));
2575 return NULL_TREE;
2578 /* Do any initial processing on the arguments to a function call. */
2580 static tree
2581 resolve_args (args)
2582 tree args;
2584 tree t;
2585 for (t = args; t; t = TREE_CHAIN (t))
2587 tree arg = TREE_VALUE (t);
2589 if (arg == error_mark_node)
2590 return error_mark_node;
2591 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2593 error ("invalid use of void expression");
2594 return error_mark_node;
2596 else if (TREE_CODE (arg) == OFFSET_REF)
2597 arg = resolve_offset_ref (arg);
2598 arg = convert_from_reference (arg);
2599 TREE_VALUE (t) = arg;
2601 return args;
2604 tree
2605 build_new_function_call (fn, args)
2606 tree fn, args;
2608 struct z_candidate *candidates = 0, *cand;
2609 tree explicit_targs = NULL_TREE;
2610 int template_only = 0;
2612 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2614 explicit_targs = TREE_OPERAND (fn, 1);
2615 fn = TREE_OPERAND (fn, 0);
2616 template_only = 1;
2619 if (really_overloaded_fn (fn))
2621 tree t1;
2622 tree templates = NULL_TREE;
2624 args = resolve_args (args);
2626 if (args == error_mark_node)
2627 return error_mark_node;
2629 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2631 tree t = OVL_FUNCTION (t1);
2633 if (TREE_CODE (t) == TEMPLATE_DECL)
2635 templates = tree_cons (NULL_TREE, t, templates);
2636 candidates = add_template_candidate
2637 (candidates, t, NULL_TREE, explicit_targs, args, NULL_TREE,
2638 LOOKUP_NORMAL, DEDUCE_CALL);
2640 else if (! template_only)
2641 candidates = add_function_candidate
2642 (candidates, t, NULL_TREE, args, LOOKUP_NORMAL);
2645 if (! any_viable (candidates))
2647 if (candidates && ! candidates->next)
2648 return build_function_call (candidates->fn, args);
2649 error ("no matching function for call to `%D(%A)'",
2650 DECL_NAME (OVL_FUNCTION (fn)), args);
2651 if (candidates)
2652 print_z_candidates (candidates);
2653 return error_mark_node;
2655 candidates = splice_viable (candidates);
2656 cand = tourney (candidates);
2658 if (cand == 0)
2660 error ("call of overloaded `%D(%A)' is ambiguous",
2661 DECL_NAME (OVL_FUNCTION (fn)), args);
2662 print_z_candidates (candidates);
2663 return error_mark_node;
2666 return build_over_call (cand, args, LOOKUP_NORMAL);
2669 /* This is not really overloaded. */
2670 fn = OVL_CURRENT (fn);
2672 return build_function_call (fn, args);
2675 static tree
2676 build_object_call (obj, args)
2677 tree obj, args;
2679 struct z_candidate *candidates = 0, *cand;
2680 tree fns, convs, mem_args = NULL_TREE;
2681 tree type = TREE_TYPE (obj);
2683 if (TYPE_PTRMEMFUNC_P (type))
2685 /* It's no good looking for an overloaded operator() on a
2686 pointer-to-member-function. */
2687 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2688 return error_mark_node;
2691 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2692 if (fns == error_mark_node)
2693 return error_mark_node;
2695 args = resolve_args (args);
2697 if (args == error_mark_node)
2698 return error_mark_node;
2700 if (fns)
2702 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
2703 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2705 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2707 tree fn = OVL_CURRENT (fns);
2708 if (TREE_CODE (fn) == TEMPLATE_DECL)
2710 candidates
2711 = add_template_candidate (candidates, fn, base, NULL_TREE,
2712 mem_args, NULL_TREE,
2713 LOOKUP_NORMAL, DEDUCE_CALL);
2715 else
2716 candidates = add_function_candidate
2717 (candidates, fn, base, mem_args, LOOKUP_NORMAL);
2719 if (candidates)
2720 candidates->basetype_path = TYPE_BINFO (type);
2724 convs = lookup_conversions (type);
2726 for (; convs; convs = TREE_CHAIN (convs))
2728 tree fns = TREE_VALUE (convs);
2729 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2731 if ((TREE_CODE (totype) == POINTER_TYPE
2732 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2733 || (TREE_CODE (totype) == REFERENCE_TYPE
2734 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2735 || (TREE_CODE (totype) == REFERENCE_TYPE
2736 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2737 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2738 for (; fns; fns = OVL_NEXT (fns))
2740 tree fn = OVL_CURRENT (fns);
2741 if (TREE_CODE (fn) == TEMPLATE_DECL)
2743 candidates = add_template_conv_candidate (candidates,
2745 obj,
2746 args,
2747 totype);
2749 else
2750 candidates = add_conv_candidate (candidates, fn, obj, args);
2754 if (! any_viable (candidates))
2756 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2757 print_z_candidates (candidates);
2758 return error_mark_node;
2761 candidates = splice_viable (candidates);
2762 cand = tourney (candidates);
2764 if (cand == 0)
2766 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2767 print_z_candidates (candidates);
2768 return error_mark_node;
2771 /* Since cand->fn will be a type, not a function, for a conversion
2772 function, we must be careful not to unconditionally look at
2773 DECL_NAME here. */
2774 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2775 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2776 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2778 obj = convert_like_with_context
2779 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2781 /* FIXME */
2782 return build_function_call (obj, args);
2785 static void
2786 op_error (code, code2, arg1, arg2, arg3, problem)
2787 enum tree_code code, code2;
2788 tree arg1, arg2, arg3;
2789 const char *problem;
2791 const char *opname;
2793 if (code == MODIFY_EXPR)
2794 opname = assignment_operator_name_info[code2].name;
2795 else
2796 opname = operator_name_info[code].name;
2798 switch (code)
2800 case COND_EXPR:
2801 error ("%s for `%T ? %T : %T' operator", problem,
2802 error_type (arg1), error_type (arg2), error_type (arg3));
2803 break;
2804 case POSTINCREMENT_EXPR:
2805 case POSTDECREMENT_EXPR:
2806 error ("%s for `%T %s' operator", problem, error_type (arg1), opname);
2807 break;
2808 case ARRAY_REF:
2809 error ("%s for `%T [%T]' operator", problem,
2810 error_type (arg1), error_type (arg2));
2811 break;
2812 default:
2813 if (arg2)
2814 error ("%s for `%T %s %T' operator", problem,
2815 error_type (arg1), opname, error_type (arg2));
2816 else
2817 error ("%s for `%s %T' operator", problem, opname, error_type (arg1));
2821 /* Return the implicit conversion sequence that could be used to
2822 convert E1 to E2 in [expr.cond]. */
2824 static tree
2825 conditional_conversion (e1, e2)
2826 tree e1;
2827 tree e2;
2829 tree t1 = non_reference (TREE_TYPE (e1));
2830 tree t2 = non_reference (TREE_TYPE (e2));
2831 tree conv;
2833 /* [expr.cond]
2835 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2836 implicitly converted (clause _conv_) to the type "reference to
2837 T2", subject to the constraint that in the conversion the
2838 reference must bind directly (_dcl.init.ref_) to E1. */
2839 if (real_lvalue_p (e2))
2841 conv = implicit_conversion (build_reference_type (t2),
2844 LOOKUP_NO_TEMP_BIND);
2845 if (conv)
2846 return conv;
2849 /* [expr.cond]
2851 If E1 and E2 have class type, and the underlying class types are
2852 the same or one is a base class of the other: E1 can be converted
2853 to match E2 if the class of T2 is the same type as, or a base
2854 class of, the class of T1, and the cv-qualification of T2 is the
2855 same cv-qualification as, or a greater cv-qualification than, the
2856 cv-qualification of T1. If the conversion is applied, E1 is
2857 changed to an rvalue of type T2 that still refers to the original
2858 source class object (or the appropriate subobject thereof). */
2859 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2860 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2861 TYPE_MAIN_VARIANT (t1)))
2863 if (at_least_as_qualified_p (t2, t1))
2865 conv = build1 (IDENTITY_CONV, t1, e1);
2866 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2867 TYPE_MAIN_VARIANT (t2)))
2868 conv = build_conv (BASE_CONV, t2, conv);
2869 return conv;
2871 else
2872 return NULL_TREE;
2875 /* [expr.cond]
2877 E1 can be converted to match E2 if E1 can be implicitly converted
2878 to the type that expression E2 would have if E2 were converted to
2879 an rvalue (or the type it has, if E2 is an rvalue). */
2880 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2883 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2884 arguments to the conditional expression. By the time this function
2885 is called, any suitable candidate functions are included in
2886 CANDIDATES. */
2888 tree
2889 build_conditional_expr (arg1, arg2, arg3)
2890 tree arg1;
2891 tree arg2;
2892 tree arg3;
2894 tree arg2_type;
2895 tree arg3_type;
2896 tree result;
2897 tree result_type = NULL_TREE;
2898 int lvalue_p = 1;
2899 struct z_candidate *candidates = 0;
2900 struct z_candidate *cand;
2902 /* As a G++ extension, the second argument to the conditional can be
2903 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2904 c'.) If the second operand is omitted, make sure it is
2905 calculated only once. */
2906 if (!arg2)
2908 if (pedantic)
2909 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2910 arg1 = arg2 = save_expr (arg1);
2913 /* [expr.cond]
2915 The first expr ession is implicitly converted to bool (clause
2916 _conv_). */
2917 arg1 = cp_convert (boolean_type_node, arg1);
2919 /* If something has already gone wrong, just pass that fact up the
2920 tree. */
2921 if (arg1 == error_mark_node
2922 || arg2 == error_mark_node
2923 || arg3 == error_mark_node
2924 || TREE_TYPE (arg1) == error_mark_node
2925 || TREE_TYPE (arg2) == error_mark_node
2926 || TREE_TYPE (arg3) == error_mark_node)
2927 return error_mark_node;
2929 /* [expr.cond]
2931 If either the second or the third operand has type (possibly
2932 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2933 array-to-pointer (_conv.array_), and function-to-pointer
2934 (_conv.func_) standard conversions are performed on the second
2935 and third operands. */
2936 arg2_type = TREE_TYPE (arg2);
2937 arg3_type = TREE_TYPE (arg3);
2938 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
2940 /* Do the conversions. We don't these for `void' type arguments
2941 since it can't have any effect and since decay_conversion
2942 does not handle that case gracefully. */
2943 if (!VOID_TYPE_P (arg2_type))
2944 arg2 = decay_conversion (arg2);
2945 if (!VOID_TYPE_P (arg3_type))
2946 arg3 = decay_conversion (arg3);
2947 arg2_type = TREE_TYPE (arg2);
2948 arg3_type = TREE_TYPE (arg3);
2950 /* [expr.cond]
2952 One of the following shall hold:
2954 --The second or the third operand (but not both) is a
2955 throw-expression (_except.throw_); the result is of the
2956 type of the other and is an rvalue.
2958 --Both the second and the third operands have type void; the
2959 result is of type void and is an rvalue. */
2960 if ((TREE_CODE (arg2) == THROW_EXPR)
2961 ^ (TREE_CODE (arg3) == THROW_EXPR))
2962 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2963 ? arg3_type : arg2_type);
2964 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
2965 result_type = void_type_node;
2966 else
2968 error ("`%E' has type `void' and is not a throw-expression",
2969 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
2970 return error_mark_node;
2973 lvalue_p = 0;
2974 goto valid_operands;
2976 /* [expr.cond]
2978 Otherwise, if the second and third operand have different types,
2979 and either has (possibly cv-qualified) class type, an attempt is
2980 made to convert each of those operands to the type of the other. */
2981 else if (!same_type_p (arg2_type, arg3_type)
2982 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2984 tree conv2 = conditional_conversion (arg2, arg3);
2985 tree conv3 = conditional_conversion (arg3, arg2);
2987 /* [expr.cond]
2989 If both can be converted, or one can be converted but the
2990 conversion is ambiguous, the program is ill-formed. If
2991 neither can be converted, the operands are left unchanged and
2992 further checking is performed as described below. If exactly
2993 one conversion is possible, that conversion is applied to the
2994 chosen operand and the converted operand is used in place of
2995 the original operand for the remainder of this section. */
2996 if ((conv2 && !ICS_BAD_FLAG (conv2)
2997 && conv3 && !ICS_BAD_FLAG (conv3))
2998 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
2999 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3001 error ("operands to ?: have different types");
3002 return error_mark_node;
3004 else if (conv2 && !ICS_BAD_FLAG (conv2))
3006 arg2 = convert_like (conv2, arg2);
3007 arg2 = convert_from_reference (arg2);
3008 /* That may not quite have done the trick. If the two types
3009 are cv-qualified variants of one another, we will have
3010 just used an IDENTITY_CONV. (There's no conversion from
3011 an lvalue of one class type to an lvalue of another type,
3012 even a cv-qualified variant, and we don't want to lose
3013 lvalue-ness here.) So, we manually add a NOP_EXPR here
3014 if necessary. */
3015 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
3016 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
3017 arg2_type = TREE_TYPE (arg2);
3019 else if (conv3 && !ICS_BAD_FLAG (conv3))
3021 arg3 = convert_like (conv3, arg3);
3022 arg3 = convert_from_reference (arg3);
3023 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
3024 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
3025 arg3_type = TREE_TYPE (arg3);
3029 /* [expr.cond]
3031 If the second and third operands are lvalues and have the same
3032 type, the result is of that type and is an lvalue. */
3033 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
3034 same_type_p (arg2_type, arg3_type))
3036 result_type = arg2_type;
3037 goto valid_operands;
3040 /* [expr.cond]
3042 Otherwise, the result is an rvalue. If the second and third
3043 operand do not have the same type, and either has (possibly
3044 cv-qualified) class type, overload resolution is used to
3045 determine the conversions (if any) to be applied to the operands
3046 (_over.match.oper_, _over.built_). */
3047 lvalue_p = 0;
3048 if (!same_type_p (arg2_type, arg3_type)
3049 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3051 tree args[3];
3052 tree conv;
3054 /* Rearrange the arguments so that add_builtin_candidate only has
3055 to know about two args. In build_builtin_candidates, the
3056 arguments are unscrambled. */
3057 args[0] = arg2;
3058 args[1] = arg3;
3059 args[2] = arg1;
3060 candidates = add_builtin_candidates (candidates,
3061 COND_EXPR,
3062 NOP_EXPR,
3063 ansi_opname (COND_EXPR),
3064 args,
3065 LOOKUP_NORMAL);
3067 /* [expr.cond]
3069 If the overload resolution fails, the program is
3070 ill-formed. */
3071 if (!any_viable (candidates))
3073 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3074 print_z_candidates (candidates);
3075 return error_mark_node;
3077 candidates = splice_viable (candidates);
3078 cand = tourney (candidates);
3079 if (!cand)
3081 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3082 print_z_candidates (candidates);
3083 return error_mark_node;
3086 /* [expr.cond]
3088 Otherwise, the conversions thus determined are applied, and
3089 the converted operands are used in place of the original
3090 operands for the remainder of this section. */
3091 conv = TREE_VEC_ELT (cand->convs, 0);
3092 arg1 = convert_like (conv, arg1);
3093 conv = TREE_VEC_ELT (cand->convs, 1);
3094 arg2 = convert_like (conv, arg2);
3095 conv = TREE_VEC_ELT (cand->convs, 2);
3096 arg3 = convert_like (conv, arg3);
3099 /* [expr.cond]
3101 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3102 and function-to-pointer (_conv.func_) standard conversions are
3103 performed on the second and third operands.
3105 We need to force the lvalue-to-rvalue conversion here for class types,
3106 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3107 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3108 regions.
3110 We use ocp_convert rather than build_user_type_conversion because the
3111 latter returns NULL_TREE on failure, while the former gives an error. */
3113 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3114 arg2 = ocp_convert (TREE_TYPE (arg2), arg2,
3115 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3116 else
3117 arg2 = decay_conversion (arg2);
3118 arg2_type = TREE_TYPE (arg2);
3120 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3121 arg3 = ocp_convert (TREE_TYPE (arg3), arg3,
3122 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3123 else
3124 arg3 = decay_conversion (arg3);
3125 arg3_type = TREE_TYPE (arg3);
3127 if (arg2 == error_mark_node || arg3 == error_mark_node)
3128 return error_mark_node;
3130 /* [expr.cond]
3132 After those conversions, one of the following shall hold:
3134 --The second and third operands have the same type; the result is of
3135 that type. */
3136 if (same_type_p (arg2_type, arg3_type))
3137 result_type = arg2_type;
3138 /* [expr.cond]
3140 --The second and third operands have arithmetic or enumeration
3141 type; the usual arithmetic conversions are performed to bring
3142 them to a common type, and the result is of that type. */
3143 else if ((ARITHMETIC_TYPE_P (arg2_type)
3144 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3145 && (ARITHMETIC_TYPE_P (arg3_type)
3146 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3148 /* In this case, there is always a common type. */
3149 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3150 arg3_type);
3152 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3153 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3154 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3155 arg2_type, arg3_type);
3156 else if (extra_warnings
3157 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3158 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3159 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3160 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3161 warning ("enumeral and non-enumeral type in conditional expression");
3163 arg2 = perform_implicit_conversion (result_type, arg2);
3164 arg3 = perform_implicit_conversion (result_type, arg3);
3166 /* [expr.cond]
3168 --The second and third operands have pointer type, or one has
3169 pointer type and the other is a null pointer constant; pointer
3170 conversions (_conv.ptr_) and qualification conversions
3171 (_conv.qual_) are performed to bring them to their composite
3172 pointer type (_expr.rel_). The result is of the composite
3173 pointer type.
3175 --The second and third operands have pointer to member type, or
3176 one has pointer to member type and the other is a null pointer
3177 constant; pointer to member conversions (_conv.mem_) and
3178 qualification conversions (_conv.qual_) are performed to bring
3179 them to a common type, whose cv-qualification shall match the
3180 cv-qualification of either the second or the third operand.
3181 The result is of the common type. */
3182 else if ((null_ptr_cst_p (arg2)
3183 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3184 || TYPE_PTRMEMFUNC_P (arg3_type)))
3185 || (null_ptr_cst_p (arg3)
3186 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3187 || TYPE_PTRMEMFUNC_P (arg2_type)))
3188 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3189 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3190 || (TYPE_PTRMEMFUNC_P (arg2_type)
3191 && TYPE_PTRMEMFUNC_P (arg3_type)))
3193 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3194 arg3, "conditional expression");
3195 arg2 = perform_implicit_conversion (result_type, arg2);
3196 arg3 = perform_implicit_conversion (result_type, arg3);
3199 if (!result_type)
3201 error ("operands to ?: have different types");
3202 return error_mark_node;
3205 valid_operands:
3206 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3207 /* Expand both sides into the same slot, hopefully the target of the
3208 ?: expression. We used to check for TARGET_EXPRs here, but now we
3209 sometimes wrap them in NOP_EXPRs so the test would fail. */
3210 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3211 result = build_target_expr_with_type (result, result_type);
3213 /* If this expression is an rvalue, but might be mistaken for an
3214 lvalue, we must add a NON_LVALUE_EXPR. */
3215 if (!lvalue_p && real_lvalue_p (result))
3216 result = build1 (NON_LVALUE_EXPR, result_type, result);
3218 return result;
3221 tree
3222 build_new_op (code, flags, arg1, arg2, arg3)
3223 enum tree_code code;
3224 int flags;
3225 tree arg1, arg2, arg3;
3227 struct z_candidate *candidates = 0, *cand;
3228 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3229 enum tree_code code2 = NOP_EXPR;
3230 tree templates = NULL_TREE;
3231 tree conv;
3233 if (arg1 == error_mark_node
3234 || arg2 == error_mark_node
3235 || arg3 == error_mark_node)
3236 return error_mark_node;
3238 /* This can happen if a template takes all non-type parameters, e.g.
3239 undeclared_template<1, 5, 72>a; */
3240 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3242 error ("`%D' must be declared before use", arg1);
3243 return error_mark_node;
3246 if (code == MODIFY_EXPR)
3248 code2 = TREE_CODE (arg3);
3249 arg3 = NULL_TREE;
3250 fnname = ansi_assopname (code2);
3252 else
3253 fnname = ansi_opname (code);
3255 if (TREE_CODE (arg1) == OFFSET_REF)
3256 arg1 = resolve_offset_ref (arg1);
3257 arg1 = convert_from_reference (arg1);
3259 switch (code)
3261 case NEW_EXPR:
3262 case VEC_NEW_EXPR:
3263 case VEC_DELETE_EXPR:
3264 case DELETE_EXPR:
3265 /* Use build_op_new_call and build_op_delete_call instead. */
3266 abort ();
3268 case CALL_EXPR:
3269 return build_object_call (arg1, arg2);
3271 default:
3272 break;
3275 if (arg2)
3277 if (TREE_CODE (arg2) == OFFSET_REF)
3278 arg2 = resolve_offset_ref (arg2);
3279 arg2 = convert_from_reference (arg2);
3281 if (arg3)
3283 if (TREE_CODE (arg3) == OFFSET_REF)
3284 arg3 = resolve_offset_ref (arg3);
3285 arg3 = convert_from_reference (arg3);
3288 if (code == COND_EXPR)
3290 if (arg2 == NULL_TREE
3291 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3292 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3293 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3294 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3295 goto builtin;
3297 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3298 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3299 goto builtin;
3301 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3302 arg2 = integer_zero_node;
3304 arglist = NULL_TREE;
3305 if (arg3)
3306 arglist = tree_cons (NULL_TREE, arg3, arglist);
3307 if (arg2)
3308 arglist = tree_cons (NULL_TREE, arg2, arglist);
3309 arglist = tree_cons (NULL_TREE, arg1, arglist);
3311 fns = lookup_function_nonclass (fnname, arglist);
3313 if (fns && TREE_CODE (fns) == TREE_LIST)
3314 fns = TREE_VALUE (fns);
3315 for (; fns; fns = OVL_NEXT (fns))
3317 tree fn = OVL_CURRENT (fns);
3318 if (TREE_CODE (fn) == TEMPLATE_DECL)
3320 templates = tree_cons (NULL_TREE, fn, templates);
3321 candidates
3322 = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3323 arglist, TREE_TYPE (fnname),
3324 flags, DEDUCE_CALL);
3326 else
3327 candidates = add_function_candidate (candidates, fn, NULL_TREE,
3328 arglist, flags);
3331 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3333 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3334 if (fns == error_mark_node)
3335 return fns;
3337 else
3338 fns = NULL_TREE;
3340 if (fns)
3342 tree basetype = BINFO_TYPE (TREE_PURPOSE (fns));
3343 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3344 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3346 tree fn = OVL_CURRENT (fns);
3347 tree this_arglist;
3349 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3350 this_arglist = mem_arglist;
3351 else
3352 this_arglist = arglist;
3354 if (TREE_CODE (fn) == TEMPLATE_DECL)
3356 /* A member template. */
3357 templates = tree_cons (NULL_TREE, fn, templates);
3358 candidates
3359 = add_template_candidate (candidates, fn, basetype, NULL_TREE,
3360 this_arglist, TREE_TYPE (fnname),
3361 flags, DEDUCE_CALL);
3363 else
3364 candidates = add_function_candidate
3365 (candidates, fn, basetype, this_arglist, flags);
3367 if (candidates)
3368 candidates->basetype_path = TYPE_BINFO (TREE_TYPE (arg1));
3373 tree args[3];
3375 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3376 to know about two args; a builtin candidate will always have a first
3377 parameter of type bool. We'll handle that in
3378 build_builtin_candidate. */
3379 if (code == COND_EXPR)
3381 args[0] = arg2;
3382 args[1] = arg3;
3383 args[2] = arg1;
3385 else
3387 args[0] = arg1;
3388 args[1] = arg2;
3389 args[2] = NULL_TREE;
3392 candidates = add_builtin_candidates
3393 (candidates, code, code2, fnname, args, flags);
3396 if (! any_viable (candidates))
3398 switch (code)
3400 case POSTINCREMENT_EXPR:
3401 case POSTDECREMENT_EXPR:
3402 /* Look for an `operator++ (int)'. If they didn't have
3403 one, then we fall back to the old way of doing things. */
3404 if (flags & LOOKUP_COMPLAIN)
3405 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3406 fnname,
3407 operator_name_info[code].name);
3408 if (code == POSTINCREMENT_EXPR)
3409 code = PREINCREMENT_EXPR;
3410 else
3411 code = PREDECREMENT_EXPR;
3412 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3414 /* The caller will deal with these. */
3415 case ADDR_EXPR:
3416 case COMPOUND_EXPR:
3417 case COMPONENT_REF:
3418 return NULL_TREE;
3420 default:
3421 break;
3423 if (flags & LOOKUP_COMPLAIN)
3425 op_error (code, code2, arg1, arg2, arg3, "no match");
3426 print_z_candidates (candidates);
3428 return error_mark_node;
3430 candidates = splice_viable (candidates);
3431 cand = tourney (candidates);
3433 if (cand == 0)
3435 if (flags & LOOKUP_COMPLAIN)
3437 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3438 print_z_candidates (candidates);
3440 return error_mark_node;
3443 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3445 extern int warn_synth;
3446 if (warn_synth
3447 && fnname == ansi_assopname (NOP_EXPR)
3448 && DECL_ARTIFICIAL (cand->fn)
3449 && candidates->next
3450 && ! candidates->next->next)
3452 warning ("using synthesized `%#D' for copy assignment",
3453 cand->fn);
3454 cp_warning_at (" where cfront would use `%#D'",
3455 cand == candidates
3456 ? candidates->next->fn
3457 : candidates->fn);
3460 return build_over_call
3461 (cand,
3462 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3463 ? mem_arglist : arglist,
3464 LOOKUP_NORMAL);
3467 /* Check for comparison of different enum types. */
3468 switch (code)
3470 case GT_EXPR:
3471 case LT_EXPR:
3472 case GE_EXPR:
3473 case LE_EXPR:
3474 case EQ_EXPR:
3475 case NE_EXPR:
3476 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3477 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3478 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3479 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3481 warning ("comparison between `%#T' and `%#T'",
3482 TREE_TYPE (arg1), TREE_TYPE (arg2));
3484 break;
3485 default:
3486 break;
3489 /* We need to strip any leading REF_BIND so that bitfields don't cause
3490 errors. This should not remove any important conversions, because
3491 builtins don't apply to class objects directly. */
3492 conv = TREE_VEC_ELT (cand->convs, 0);
3493 if (TREE_CODE (conv) == REF_BIND)
3494 conv = TREE_OPERAND (conv, 0);
3495 arg1 = convert_like (conv, arg1);
3496 if (arg2)
3498 conv = TREE_VEC_ELT (cand->convs, 1);
3499 if (TREE_CODE (conv) == REF_BIND)
3500 conv = TREE_OPERAND (conv, 0);
3501 arg2 = convert_like (conv, arg2);
3503 if (arg3)
3505 conv = TREE_VEC_ELT (cand->convs, 2);
3506 if (TREE_CODE (conv) == REF_BIND)
3507 conv = TREE_OPERAND (conv, 0);
3508 arg3 = convert_like (conv, arg3);
3511 builtin:
3512 switch (code)
3514 case MODIFY_EXPR:
3515 return build_modify_expr (arg1, code2, arg2);
3517 case INDIRECT_REF:
3518 return build_indirect_ref (arg1, "unary *");
3520 case PLUS_EXPR:
3521 case MINUS_EXPR:
3522 case MULT_EXPR:
3523 case TRUNC_DIV_EXPR:
3524 case GT_EXPR:
3525 case LT_EXPR:
3526 case GE_EXPR:
3527 case LE_EXPR:
3528 case EQ_EXPR:
3529 case NE_EXPR:
3530 case MAX_EXPR:
3531 case MIN_EXPR:
3532 case LSHIFT_EXPR:
3533 case RSHIFT_EXPR:
3534 case TRUNC_MOD_EXPR:
3535 case BIT_AND_EXPR:
3536 case BIT_IOR_EXPR:
3537 case BIT_XOR_EXPR:
3538 case TRUTH_ANDIF_EXPR:
3539 case TRUTH_ORIF_EXPR:
3540 return cp_build_binary_op (code, arg1, arg2);
3542 case CONVERT_EXPR:
3543 case NEGATE_EXPR:
3544 case BIT_NOT_EXPR:
3545 case TRUTH_NOT_EXPR:
3546 case PREINCREMENT_EXPR:
3547 case POSTINCREMENT_EXPR:
3548 case PREDECREMENT_EXPR:
3549 case POSTDECREMENT_EXPR:
3550 case REALPART_EXPR:
3551 case IMAGPART_EXPR:
3552 return build_unary_op (code, arg1, candidates != 0);
3554 case ARRAY_REF:
3555 return build_array_ref (arg1, arg2);
3557 case COND_EXPR:
3558 return build_conditional_expr (arg1, arg2, arg3);
3560 case MEMBER_REF:
3561 return build_m_component_ref
3562 (build_indirect_ref (arg1, NULL), arg2);
3564 /* The caller will deal with these. */
3565 case ADDR_EXPR:
3566 case COMPONENT_REF:
3567 case COMPOUND_EXPR:
3568 return NULL_TREE;
3570 default:
3571 abort ();
3572 return NULL_TREE;
3576 /* Build a call to operator delete. This has to be handled very specially,
3577 because the restrictions on what signatures match are different from all
3578 other call instances. For a normal delete, only a delete taking (void *)
3579 or (void *, size_t) is accepted. For a placement delete, only an exact
3580 match with the placement new is accepted.
3582 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3583 ADDR is the pointer to be deleted. For placement delete, it is also
3584 used to determine what the corresponding new looked like.
3585 SIZE is the size of the memory block to be deleted.
3586 FLAGS are the usual overloading flags.
3587 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3589 tree
3590 build_op_delete_call (code, addr, size, flags, placement)
3591 enum tree_code code;
3592 tree addr, size, placement;
3593 int flags;
3595 tree fn = NULL_TREE;
3596 tree fns, fnname, fntype, argtypes, args, type;
3597 int pass;
3599 if (addr == error_mark_node)
3600 return error_mark_node;
3602 type = TREE_TYPE (TREE_TYPE (addr));
3603 while (TREE_CODE (type) == ARRAY_TYPE)
3604 type = TREE_TYPE (type);
3606 fnname = ansi_opname (code);
3608 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3609 /* In [class.free]
3611 If the result of the lookup is ambiguous or inaccessible, or if
3612 the lookup selects a placement deallocation function, the
3613 program is ill-formed.
3615 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3617 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3618 if (fns == error_mark_node)
3619 return error_mark_node;
3621 else
3622 fns = NULL_TREE;
3624 if (fns == NULL_TREE)
3625 fns = lookup_name_nonclass (fnname);
3627 if (placement)
3629 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3631 /* Extract the function. */
3632 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3633 /* Then the second parm type. */
3634 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3636 /* Also the second argument. */
3637 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3639 else
3641 /* First try it without the size argument. */
3642 argtypes = void_list_node;
3643 args = NULL_TREE;
3646 /* Strip const and volatile from addr. */
3647 addr = cp_convert (ptr_type_node, addr);
3649 /* We make two tries at finding a matching `operator delete'. On
3650 the first pass, we look for an one-operator (or placement)
3651 operator delete. If we're not doing placement delete, then on
3652 the second pass we look for a two-argument delete. */
3653 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3655 if (pass == 0)
3656 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3657 else
3658 /* Normal delete; now try to find a match including the size
3659 argument. */
3660 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3661 tree_cons (NULL_TREE, sizetype,
3662 void_list_node));
3663 fntype = build_function_type (void_type_node, argtypes);
3665 /* Go through the `operator delete' functions looking for one
3666 with a matching type. */
3667 for (fn = BASELINK_P (fns) ? TREE_VALUE (fns) : fns;
3668 fn;
3669 fn = OVL_NEXT (fn))
3671 tree t;
3673 /* Exception specifications on the `delete' operator do not
3674 matter. */
3675 t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3676 NULL_TREE);
3677 /* We also don't compare attributes. We're really just
3678 trying to check the types of the first two parameters. */
3679 if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3680 break;
3683 /* If we found a match, we're done. */
3684 if (fn)
3685 break;
3688 /* If we have a matching function, call it. */
3689 if (fn)
3691 /* Make sure we have the actual function, and not an
3692 OVERLOAD. */
3693 fn = OVL_CURRENT (fn);
3695 /* If the FN is a member function, make sure that it is
3696 accessible. */
3697 if (DECL_CLASS_SCOPE_P (fn))
3698 enforce_access (type, fn);
3700 if (pass == 0)
3701 args = tree_cons (NULL_TREE, addr, args);
3702 else
3703 args = tree_cons (NULL_TREE, addr,
3704 build_tree_list (NULL_TREE, size));
3706 return build_function_call (fn, args);
3709 /* If we are doing placement delete we do nothing if we don't find a
3710 matching op delete. */
3711 if (placement)
3712 return NULL_TREE;
3714 error ("no suitable `operator delete' for `%T'", type);
3715 return error_mark_node;
3718 /* If the current scope isn't allowed to access DECL along
3719 BASETYPE_PATH, give an error. The most derived class in
3720 BASETYPE_PATH is the one used to qualify DECL. */
3723 enforce_access (basetype_path, decl)
3724 tree basetype_path;
3725 tree decl;
3727 int accessible;
3729 accessible = accessible_p (basetype_path, decl);
3730 if (!accessible)
3732 if (TREE_PRIVATE (decl))
3733 cp_error_at ("`%+#D' is private", decl);
3734 else if (TREE_PROTECTED (decl))
3735 cp_error_at ("`%+#D' is protected", decl);
3736 else
3737 cp_error_at ("`%+#D' is inaccessible", decl);
3738 error ("within this context");
3739 return 0;
3742 return 1;
3745 /* Perform the conversions in CONVS on the expression EXPR.
3746 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3747 indicates the `this' argument of a method. INNER is non-zero when
3748 being called to continue a conversion chain. It is negative when a
3749 reference binding will be applied, positive otherwise. */
3751 static tree
3752 convert_like_real (convs, expr, fn, argnum, inner)
3753 tree convs, expr;
3754 tree fn;
3755 int argnum;
3756 int inner;
3758 int savew, savee;
3760 tree totype = TREE_TYPE (convs);
3762 if (ICS_BAD_FLAG (convs)
3763 && TREE_CODE (convs) != USER_CONV
3764 && TREE_CODE (convs) != AMBIG_CONV
3765 && TREE_CODE (convs) != REF_BIND)
3767 tree t = convs;
3768 for (; t; t = TREE_OPERAND (t, 0))
3770 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3772 expr = convert_like_real (t, expr, fn, argnum, 1);
3773 break;
3775 else if (TREE_CODE (t) == AMBIG_CONV)
3776 return convert_like_real (t, expr, fn, argnum, 1);
3777 else if (TREE_CODE (t) == IDENTITY_CONV)
3778 break;
3780 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3781 if (fn)
3782 pedwarn (" initializing argument %P of `%D'", argnum, fn);
3783 return cp_convert (totype, expr);
3786 if (!inner)
3787 expr = dubious_conversion_warnings
3788 (totype, expr, "argument", fn, argnum);
3789 switch (TREE_CODE (convs))
3791 case USER_CONV:
3793 struct z_candidate *cand
3794 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3795 tree convfn = cand->fn;
3796 tree args;
3798 if (DECL_CONSTRUCTOR_P (convfn))
3800 tree t = build_int_2 (0, 0);
3801 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3803 args = build_tree_list (NULL_TREE, expr);
3804 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3805 || DECL_HAS_VTT_PARM_P (convfn))
3806 /* We should never try to call the abstract or base constructor
3807 from here. */
3808 abort ();
3809 args = tree_cons (NULL_TREE, t, args);
3811 else
3812 args = build_this (expr);
3813 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3815 /* If this is a constructor or a function returning an aggr type,
3816 we need to build up a TARGET_EXPR. */
3817 if (DECL_CONSTRUCTOR_P (convfn))
3818 expr = build_cplus_new (totype, expr);
3820 /* The result of the call is then used to direct-initialize the object
3821 that is the destination of the copy-initialization. [dcl.init]
3823 Note that this step is not reflected in the conversion sequence;
3824 it affects the semantics when we actually perform the
3825 conversion, but is not considered during overload resolution.
3827 If the target is a class, that means call a ctor. */
3828 if (IS_AGGR_TYPE (totype)
3829 && (inner >= 0 || !lvalue_p (expr)))
3831 savew = warningcount, savee = errorcount;
3832 expr = build_new_method_call
3833 (NULL_TREE, complete_ctor_identifier,
3834 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3835 /* Core issue 84, now a DR, says that we don't allow UDCs
3836 for these args (which deliberately breaks copy-init of an
3837 auto_ptr<Base> from an auto_ptr<Derived>). */
3838 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3840 /* Tell the user where this failing constructor call came from. */
3841 if (fn)
3843 if (warningcount > savew)
3844 warning
3845 (" initializing argument %P of `%D' from result of `%D'",
3846 argnum, fn, convfn);
3847 else if (errorcount > savee)
3848 error
3849 (" initializing argument %P of `%D' from result of `%D'",
3850 argnum, fn, convfn);
3852 else
3854 if (warningcount > savew)
3855 warning (" initializing temporary from result of `%D'",
3856 convfn);
3857 else if (errorcount > savee)
3858 error (" initializing temporary from result of `%D'",
3859 convfn);
3861 expr = build_cplus_new (totype, expr);
3863 return expr;
3865 case IDENTITY_CONV:
3866 if (type_unknown_p (expr))
3867 expr = instantiate_type (totype, expr, tf_error | tf_warning);
3868 return expr;
3869 case AMBIG_CONV:
3870 /* Call build_user_type_conversion again for the error. */
3871 return build_user_type_conversion
3872 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3874 default:
3875 break;
3878 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
3879 TREE_CODE (convs) == REF_BIND ? -1 : 1);
3880 if (expr == error_mark_node)
3881 return error_mark_node;
3883 /* Convert a non-array constant variable to its underlying value, unless we
3884 are about to bind it to a reference, in which case we need to
3885 leave it as an lvalue. */
3886 if (TREE_CODE (convs) != REF_BIND
3887 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3888 expr = decl_constant_value (expr);
3890 switch (TREE_CODE (convs))
3892 case RVALUE_CONV:
3893 if (! IS_AGGR_TYPE (totype))
3894 return expr;
3895 /* else fall through */
3896 case BASE_CONV:
3897 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3899 /* We are going to bind a reference directly to a base-class
3900 subobject of EXPR. */
3901 tree base_ptr = build_pointer_type (totype);
3903 /* Build an expression for `*((base*) &expr)'. */
3904 expr = build_unary_op (ADDR_EXPR, expr, 0);
3905 expr = perform_implicit_conversion (base_ptr, expr);
3906 expr = build_indirect_ref (expr, "implicit conversion");
3907 return expr;
3910 /* Copy-initialization where the cv-unqualified version of the source
3911 type is the same class as, or a derived class of, the class of the
3912 destination [is treated as direct-initialization]. [dcl.init] */
3913 savew = warningcount, savee = errorcount;
3914 expr = build_new_method_call (NULL_TREE, complete_ctor_identifier,
3915 build_tree_list (NULL_TREE, expr),
3916 TYPE_BINFO (totype),
3917 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
3918 if (fn)
3920 if (warningcount > savew)
3921 warning (" initializing argument %P of `%D'", argnum, fn);
3922 else if (errorcount > savee)
3923 error (" initializing argument %P of `%D'", argnum, fn);
3925 return build_cplus_new (totype, expr);
3927 case REF_BIND:
3929 tree ref_type = totype;
3931 /* If necessary, create a temporary. */
3932 if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
3934 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3935 expr = build_target_expr_with_type (expr, type);
3938 /* Take the address of the thing to which we will bind the
3939 reference. */
3940 expr = build_unary_op (ADDR_EXPR, expr, 1);
3941 if (expr == error_mark_node)
3942 return error_mark_node;
3944 /* Convert it to a pointer to the type referred to by the
3945 reference. This will adjust the pointer if a derived to
3946 base conversion is being performed. */
3947 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3948 expr);
3949 /* Convert the pointer to the desired reference type. */
3950 expr = build1 (NOP_EXPR, ref_type, expr);
3952 return expr;
3955 case LVALUE_CONV:
3956 return decay_conversion (expr);
3958 case QUAL_CONV:
3959 /* Warn about deprecated conversion if appropriate. */
3960 string_conv_p (totype, expr, 1);
3961 break;
3963 default:
3964 break;
3966 return ocp_convert (totype, expr, CONV_IMPLICIT,
3967 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3970 /* ARG is being passed to a varargs function. Perform any conversions
3971 required. Array/function to pointer decay must have already happened.
3972 Return the converted value. */
3974 tree
3975 convert_arg_to_ellipsis (arg)
3976 tree arg;
3978 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3979 && (TYPE_PRECISION (TREE_TYPE (arg))
3980 < TYPE_PRECISION (double_type_node)))
3981 /* Convert `float' to `double'. */
3982 arg = cp_convert (double_type_node, arg);
3983 else
3984 /* Convert `short' and `char' to full-size `int'. */
3985 arg = default_conversion (arg);
3987 arg = require_complete_type (arg);
3989 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
3991 /* Undefined behaviour [expr.call] 5.2.2/7. */
3992 warning ("cannot pass objects of non-POD type `%#T' through `...'",
3993 TREE_TYPE (arg));
3996 return arg;
3999 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4001 tree
4002 build_x_va_arg (expr, type)
4003 tree expr;
4004 tree type;
4006 if (processing_template_decl)
4007 return build_min (VA_ARG_EXPR, type, expr);
4009 type = complete_type_or_else (type, NULL_TREE);
4011 if (expr == error_mark_node || !type)
4012 return error_mark_node;
4014 if (! pod_type_p (type))
4016 /* Undefined behaviour [expr.call] 5.2.2/7. */
4017 warning ("cannot receive objects of non-POD type `%#T' through `...'",
4018 type);
4021 return build_va_arg (expr, type);
4024 /* TYPE has been given to va_arg. Apply the default conversions which would
4025 have happened when passed via ellipsis. Return the promoted type, or
4026 NULL_TREE, if there is no change. */
4028 tree
4029 convert_type_from_ellipsis (type)
4030 tree type;
4032 tree promote;
4034 if (TREE_CODE (type) == ARRAY_TYPE)
4035 promote = build_pointer_type (TREE_TYPE (type));
4036 else if (TREE_CODE (type) == FUNCTION_TYPE)
4037 promote = build_pointer_type (type);
4038 else
4039 promote = type_promotes_to (type);
4041 return same_type_p (type, promote) ? NULL_TREE : promote;
4044 /* ARG is a default argument expression being passed to a parameter of
4045 the indicated TYPE, which is a parameter to FN. Do any required
4046 conversions. Return the converted value. */
4048 tree
4049 convert_default_arg (type, arg, fn, parmnum)
4050 tree type;
4051 tree arg;
4052 tree fn;
4053 int parmnum;
4055 if (TREE_CODE (arg) == DEFAULT_ARG)
4057 /* When processing the default args for a class, we can find that
4058 there is an ordering constraint, and we call a function who's
4059 default args have not yet been converted. For instance,
4060 class A {
4061 A (int = 0);
4062 void Foo (A const & = A ());
4064 We must process A::A before A::Foo's default arg can be converted.
4065 Remember the dependent function, so do_pending_defargs can retry,
4066 and check loops. */
4067 unprocessed_defarg_fn (fn);
4069 /* Don't return error_mark node, as we won't be able to distinguish
4070 genuine errors from this case, and that would lead to repeated
4071 diagnostics. Just make something of the right type. */
4072 return build1 (NOP_EXPR, type, integer_zero_node);
4075 if (fn && DECL_TEMPLATE_INFO (fn))
4076 arg = tsubst_default_argument (fn, type, arg);
4078 arg = break_out_target_exprs (arg);
4080 if (TREE_CODE (arg) == CONSTRUCTOR)
4082 arg = digest_init (type, arg, 0);
4083 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4084 "default argument", fn, parmnum);
4086 else
4088 /* This could get clobbered by the following call. */
4089 if (TREE_HAS_CONSTRUCTOR (arg))
4090 arg = copy_node (arg);
4092 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4093 "default argument", fn, parmnum);
4094 if (PROMOTE_PROTOTYPES
4095 && INTEGRAL_TYPE_P (type)
4096 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4097 arg = default_conversion (arg);
4100 return arg;
4103 /* Subroutine of the various build_*_call functions. Overload resolution
4104 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4105 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4106 bitmask of various LOOKUP_* flags which apply to the call itself. */
4108 static tree
4109 build_over_call (cand, args, flags)
4110 struct z_candidate *cand;
4111 tree args;
4112 int flags;
4114 tree fn = cand->fn;
4115 tree convs = cand->convs;
4116 tree converted_args = NULL_TREE;
4117 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4118 tree conv, arg, val;
4119 int i = 0;
4120 int is_method = 0;
4122 /* Give any warnings we noticed during overload resolution. */
4123 if (cand->warnings)
4124 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4125 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
4127 if (DECL_FUNCTION_MEMBER_P (fn))
4128 enforce_access (cand->basetype_path, fn);
4130 if (args && TREE_CODE (args) != TREE_LIST)
4131 args = build_tree_list (NULL_TREE, args);
4132 arg = args;
4134 /* The implicit parameters to a constructor are not considered by overload
4135 resolution, and must be of the proper type. */
4136 if (DECL_CONSTRUCTOR_P (fn))
4138 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4139 arg = TREE_CHAIN (arg);
4140 parm = TREE_CHAIN (parm);
4141 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4142 /* We should never try to call the abstract constructor. */
4143 abort ();
4144 if (DECL_HAS_VTT_PARM_P (fn))
4146 converted_args = tree_cons
4147 (NULL_TREE, TREE_VALUE (arg), converted_args);
4148 arg = TREE_CHAIN (arg);
4149 parm = TREE_CHAIN (parm);
4152 /* Bypass access control for 'this' parameter. */
4153 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4155 tree parmtype = TREE_VALUE (parm);
4156 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4157 tree t;
4158 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4159 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4160 TREE_TYPE (argtype), fn);
4162 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4163 X is called for an object that is not of type X, or of a type
4164 derived from X, the behavior is undefined.
4166 So we can assume that anything passed as 'this' is non-null, and
4167 optimize accordingly. */
4168 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4169 t = lookup_base (TREE_TYPE (TREE_TYPE (TREE_VALUE (arg))),
4170 TREE_TYPE (parmtype), ba_ignore, NULL);
4171 t = build_base_path (PLUS_EXPR, TREE_VALUE (arg), t, 1);
4172 converted_args = tree_cons (NULL_TREE, t, converted_args);
4173 parm = TREE_CHAIN (parm);
4174 arg = TREE_CHAIN (arg);
4175 ++i;
4176 is_method = 1;
4179 for (; arg && parm;
4180 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4182 tree type = TREE_VALUE (parm);
4184 conv = TREE_VEC_ELT (convs, i);
4185 val = convert_like_with_context
4186 (conv, TREE_VALUE (arg), fn, i - is_method);
4188 if (PROMOTE_PROTOTYPES
4189 && INTEGRAL_TYPE_P (type)
4190 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4191 val = default_conversion (val);
4192 converted_args = tree_cons (NULL_TREE, val, converted_args);
4195 /* Default arguments */
4196 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4197 converted_args
4198 = tree_cons (NULL_TREE,
4199 convert_default_arg (TREE_VALUE (parm),
4200 TREE_PURPOSE (parm),
4201 fn, i - is_method),
4202 converted_args);
4204 /* Ellipsis */
4205 for (; arg; arg = TREE_CHAIN (arg))
4206 converted_args
4207 = tree_cons (NULL_TREE,
4208 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4209 converted_args);
4211 converted_args = nreverse (converted_args);
4213 if (warn_format)
4214 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4215 converted_args);
4217 /* Avoid actually calling copy constructors and copy assignment operators,
4218 if possible. */
4220 if (! flag_elide_constructors)
4221 /* Do things the hard way. */;
4222 else if (TREE_VEC_LENGTH (convs) == 1
4223 && DECL_COPY_CONSTRUCTOR_P (fn))
4225 tree targ;
4226 arg = skip_artificial_parms_for (fn, converted_args);
4227 arg = TREE_VALUE (arg);
4229 /* Pull out the real argument, disregarding const-correctness. */
4230 targ = arg;
4231 while (TREE_CODE (targ) == NOP_EXPR
4232 || TREE_CODE (targ) == NON_LVALUE_EXPR
4233 || TREE_CODE (targ) == CONVERT_EXPR)
4234 targ = TREE_OPERAND (targ, 0);
4235 if (TREE_CODE (targ) == ADDR_EXPR)
4237 targ = TREE_OPERAND (targ, 0);
4238 if (!same_type_ignoring_top_level_qualifiers_p
4239 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4240 targ = NULL_TREE;
4242 else
4243 targ = NULL_TREE;
4245 if (targ)
4246 arg = targ;
4247 else
4248 arg = build_indirect_ref (arg, 0);
4250 /* [class.copy]: the copy constructor is implicitly defined even if
4251 the implementation elided its use. */
4252 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4253 mark_used (fn);
4255 /* If we're creating a temp and we already have one, don't create a
4256 new one. If we're not creating a temp but we get one, use
4257 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4258 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4259 temp or an INIT_EXPR otherwise. */
4260 if (integer_zerop (TREE_VALUE (args)))
4262 if (! real_lvalue_p (arg))
4263 return arg;
4264 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4265 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4267 else if ((!real_lvalue_p (arg)
4268 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4269 /* Empty classes have padding which can be hidden
4270 inside an (empty) base of the class. This must not
4271 be touched as it might overlay things. When the
4272 gcc core learns about empty classes, we can treat it
4273 like other classes. */
4274 && !is_empty_class (DECL_CONTEXT (fn)))
4276 tree address;
4277 tree to = stabilize_reference
4278 (build_indirect_ref (TREE_VALUE (args), 0));
4280 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4281 address = build_unary_op (ADDR_EXPR, val, 0);
4282 /* Avoid a warning about this expression, if the address is
4283 never used. */
4284 TREE_USED (address) = 1;
4285 return address;
4288 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4289 && copy_fn_p (fn)
4290 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4292 tree to = stabilize_reference
4293 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4295 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4296 if (is_empty_class (TREE_TYPE (to)))
4298 TREE_USED (arg) = 1;
4300 val = build (COMPOUND_EXPR, DECL_CONTEXT (fn), arg, to);
4301 /* Even though the assignment may not actually result in any
4302 code being generated, we do not want to warn about the
4303 assignment having no effect. That would be confusing to
4304 users who may be performing the assignment as part of a
4305 generic algorithm, for example.
4307 Ideally, the notions of having side-effects and of being
4308 useless would be orthogonal. */
4309 TREE_SIDE_EFFECTS (val) = 1;
4311 else
4312 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4313 return val;
4316 mark_used (fn);
4318 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4320 tree t, *p = &TREE_VALUE (converted_args);
4321 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4322 DECL_VIRTUAL_CONTEXT (fn),
4323 ba_any, NULL);
4324 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4326 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4327 if (TREE_SIDE_EFFECTS (*p))
4328 *p = save_expr (*p);
4329 t = build_pointer_type (TREE_TYPE (fn));
4330 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4331 fn = build_java_interface_fn_ref (fn, *p);
4332 else
4333 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4334 TREE_TYPE (fn) = t;
4336 else if (DECL_INLINE (fn))
4337 fn = inline_conversion (fn);
4338 else
4339 fn = build_addr_func (fn);
4341 /* Recognize certain built-in functions so we can make tree-codes
4342 other than CALL_EXPR. We do this when it enables fold-const.c
4343 to do something useful. */
4345 if (TREE_CODE (fn) == ADDR_EXPR
4346 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4347 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4349 tree exp;
4350 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4351 if (exp)
4352 return exp;
4355 /* Some built-in function calls will be evaluated at
4356 compile-time in fold (). */
4357 fn = fold (build_call (fn, converted_args));
4358 if (VOID_TYPE_P (TREE_TYPE (fn)))
4359 return fn;
4360 fn = require_complete_type (fn);
4361 if (fn == error_mark_node)
4362 return error_mark_node;
4363 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4364 fn = build_cplus_new (TREE_TYPE (fn), fn);
4365 return convert_from_reference (fn);
4368 static tree java_iface_lookup_fn;
4370 /* Make an expression which yields the address of the Java interface
4371 method FN. This is achieved by generating a call to libjava's
4372 _Jv_LookupInterfaceMethodIdx(). */
4374 static tree
4375 build_java_interface_fn_ref (fn, instance)
4376 tree fn, instance;
4378 tree lookup_args, lookup_fn, method, idx;
4379 tree klass_ref, iface, iface_ref;
4380 int i;
4382 if (!java_iface_lookup_fn)
4384 tree endlink = build_void_list_node ();
4385 tree t = tree_cons (NULL_TREE, ptr_type_node,
4386 tree_cons (NULL_TREE, ptr_type_node,
4387 tree_cons (NULL_TREE, java_int_type_node,
4388 endlink)));
4389 java_iface_lookup_fn
4390 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4391 build_function_type (ptr_type_node, t),
4392 0, NOT_BUILT_IN, NULL);
4393 ggc_add_tree_root (&java_iface_lookup_fn, 1);
4396 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4397 This is the first entry in the vtable. */
4398 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4399 integer_zero_node);
4401 /* Get the java.lang.Class pointer for the interface being called. */
4402 iface = DECL_CONTEXT (fn);
4403 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4404 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4405 || DECL_CONTEXT (iface_ref) != iface)
4407 error ("could not find class$ field in java interface type `%T'",
4408 iface);
4409 return error_mark_node;
4411 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4413 /* Determine the itable index of FN. */
4414 i = 1;
4415 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4417 if (!DECL_VIRTUAL_P (method))
4418 continue;
4419 if (fn == method)
4420 break;
4421 i++;
4423 idx = build_int_2 (i, 0);
4425 lookup_args = tree_cons (NULL_TREE, klass_ref,
4426 tree_cons (NULL_TREE, iface_ref,
4427 build_tree_list (NULL_TREE, idx)));
4428 lookup_fn = build1 (ADDR_EXPR,
4429 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4430 java_iface_lookup_fn);
4431 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4434 /* Returns the value to use for the in-charge parameter when making a
4435 call to a function with the indicated NAME. */
4437 tree
4438 in_charge_arg_for_name (name)
4439 tree name;
4441 if (name == base_ctor_identifier
4442 || name == base_dtor_identifier)
4443 return integer_zero_node;
4444 else if (name == complete_ctor_identifier)
4445 return integer_one_node;
4446 else if (name == complete_dtor_identifier)
4447 return integer_two_node;
4448 else if (name == deleting_dtor_identifier)
4449 return integer_three_node;
4451 /* This function should only be called with one of the names listed
4452 above. */
4453 abort ();
4454 return NULL_TREE;
4457 static tree
4458 build_new_method_call (instance, name, args, basetype_path, flags)
4459 tree instance, name, args, basetype_path;
4460 int flags;
4462 struct z_candidate *candidates = 0, *cand;
4463 tree explicit_targs = NULL_TREE;
4464 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4465 tree pretty_name;
4466 tree user_args;
4467 tree templates = NULL_TREE;
4468 tree call;
4469 int template_only = 0;
4471 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4473 explicit_targs = TREE_OPERAND (name, 1);
4474 name = TREE_OPERAND (name, 0);
4475 if (DECL_P (name))
4476 name = DECL_NAME (name);
4477 else
4479 if (TREE_CODE (name) == COMPONENT_REF)
4480 name = TREE_OPERAND (name, 1);
4481 if (TREE_CODE (name) == OVERLOAD)
4482 name = DECL_NAME (OVL_CURRENT (name));
4485 template_only = 1;
4488 user_args = args;
4489 args = resolve_args (args);
4491 if (args == error_mark_node)
4492 return error_mark_node;
4494 if (instance == NULL_TREE)
4495 basetype = BINFO_TYPE (basetype_path);
4496 else
4498 if (TREE_CODE (instance) == OFFSET_REF)
4499 instance = resolve_offset_ref (instance);
4500 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4501 instance = convert_from_reference (instance);
4502 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4504 /* XXX this should be handled before we get here. */
4505 if (! IS_AGGR_TYPE (basetype))
4507 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4508 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4509 name, instance, basetype);
4511 return error_mark_node;
4515 if (basetype_path == NULL_TREE)
4516 basetype_path = TYPE_BINFO (basetype);
4518 if (instance)
4520 instance_ptr = build_this (instance);
4522 if (! template_only)
4524 /* XXX this should be handled before we get here. */
4525 fns = build_field_call (basetype_path, instance_ptr, name, args);
4526 if (fns)
4527 return fns;
4530 else
4532 instance_ptr = build_int_2 (0, 0);
4533 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4536 /* Callers should explicitly indicate whether they want to construct
4537 the complete object or just the part without virtual bases. */
4538 my_friendly_assert (name != ctor_identifier, 20000408);
4539 /* Similarly for destructors. */
4540 my_friendly_assert (name != dtor_identifier, 20000408);
4542 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4544 int constructor_p;
4546 constructor_p = (name == complete_ctor_identifier
4547 || name == base_ctor_identifier);
4548 pretty_name = (constructor_p
4549 ? constructor_name (basetype) : dtor_identifier);
4551 /* If we're a call to a constructor or destructor for a
4552 subobject that uses virtual base classes, then we need to
4553 pass down a pointer to a VTT for the subobject. */
4554 if ((name == base_ctor_identifier
4555 || name == base_dtor_identifier)
4556 && TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4558 tree vtt;
4559 tree sub_vtt;
4560 tree basebinfo = basetype_path;
4562 /* If the current function is a complete object constructor
4563 or destructor, then we fetch the VTT directly.
4564 Otherwise, we look it up using the VTT we were given. */
4565 vtt = IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type));
4566 vtt = decay_conversion (vtt);
4567 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4568 build (EQ_EXPR, boolean_type_node,
4569 current_in_charge_parm, integer_zero_node),
4570 current_vtt_parm,
4571 vtt);
4572 if (TREE_VIA_VIRTUAL (basebinfo))
4573 basebinfo = binfo_for_vbase (basetype, current_class_type);
4574 my_friendly_assert (BINFO_SUBVTT_INDEX (basebinfo), 20010110);
4575 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4576 BINFO_SUBVTT_INDEX (basebinfo));
4578 args = tree_cons (NULL_TREE, sub_vtt, args);
4581 else
4582 pretty_name = name;
4584 fns = lookup_fnfields (basetype_path, name, 1);
4586 if (fns == error_mark_node)
4587 return error_mark_node;
4588 if (fns)
4590 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
4591 tree fn = TREE_VALUE (fns);
4592 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4593 for (; fn; fn = OVL_NEXT (fn))
4595 tree t = OVL_CURRENT (fn);
4596 tree this_arglist;
4598 /* We can end up here for copy-init of same or base class. */
4599 if ((flags & LOOKUP_ONLYCONVERTING)
4600 && DECL_NONCONVERTING_P (t))
4601 continue;
4603 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4604 this_arglist = mem_args;
4605 else
4606 this_arglist = args;
4608 if (TREE_CODE (t) == TEMPLATE_DECL)
4610 /* A member template. */
4611 templates = tree_cons (NULL_TREE, t, templates);
4612 candidates =
4613 add_template_candidate (candidates, t, base, explicit_targs,
4614 this_arglist,
4615 TREE_TYPE (name), flags, DEDUCE_CALL);
4617 else if (! template_only)
4618 candidates = add_function_candidate (candidates, t, base,
4619 this_arglist, flags);
4621 if (candidates)
4622 candidates->basetype_path = basetype_path;
4626 if (! any_viable (candidates))
4628 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4629 if (flags & LOOKUP_SPECULATIVELY)
4630 return NULL_TREE;
4631 if (!COMPLETE_TYPE_P (basetype))
4632 incomplete_type_error (instance_ptr, basetype);
4633 else
4634 error ("no matching function for call to `%T::%D(%A)%#V'",
4635 basetype, pretty_name, user_args,
4636 TREE_TYPE (TREE_TYPE (instance_ptr)));
4637 print_z_candidates (candidates);
4638 return error_mark_node;
4640 candidates = splice_viable (candidates);
4641 cand = tourney (candidates);
4643 if (cand == 0)
4645 error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4646 user_args);
4647 print_z_candidates (candidates);
4648 return error_mark_node;
4651 if (DECL_PURE_VIRTUAL_P (cand->fn)
4652 && instance == current_class_ref
4653 && (DECL_CONSTRUCTOR_P (current_function_decl)
4654 || DECL_DESTRUCTOR_P (current_function_decl))
4655 && ! (flags & LOOKUP_NONVIRTUAL)
4656 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4657 error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4658 "abstract virtual `%#D' called from constructor"
4659 : "abstract virtual `%#D' called from destructor"),
4660 cand->fn);
4661 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4662 && is_dummy_object (instance_ptr))
4664 error ("cannot call member function `%D' without object", cand->fn);
4665 return error_mark_node;
4668 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4669 && resolves_to_fixed_type_p (instance, 0))
4670 flags |= LOOKUP_NONVIRTUAL;
4672 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4673 call = build_over_call (cand, mem_args, flags);
4674 else
4676 call = build_over_call (cand, args, flags);
4677 /* Do evaluate the object parameter in a call to a static member
4678 function. */
4679 if (TREE_SIDE_EFFECTS (instance))
4680 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4683 return call;
4686 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4687 subsequence of ICS2. */
4689 static int
4690 is_subseq (ics1, ics2)
4691 tree ics1, ics2;
4693 /* We can assume that a conversion of the same code
4694 between the same types indicates a subsequence since we only get
4695 here if the types we are converting from are the same. */
4697 while (TREE_CODE (ics1) == RVALUE_CONV
4698 || TREE_CODE (ics1) == LVALUE_CONV)
4699 ics1 = TREE_OPERAND (ics1, 0);
4701 while (1)
4703 while (TREE_CODE (ics2) == RVALUE_CONV
4704 || TREE_CODE (ics2) == LVALUE_CONV)
4705 ics2 = TREE_OPERAND (ics2, 0);
4707 if (TREE_CODE (ics2) == USER_CONV
4708 || TREE_CODE (ics2) == AMBIG_CONV
4709 || TREE_CODE (ics2) == IDENTITY_CONV)
4710 /* At this point, ICS1 cannot be a proper subsequence of
4711 ICS2. We can get a USER_CONV when we are comparing the
4712 second standard conversion sequence of two user conversion
4713 sequences. */
4714 return 0;
4716 ics2 = TREE_OPERAND (ics2, 0);
4718 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4719 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4720 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4721 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4722 return 1;
4726 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4727 be any _TYPE nodes. */
4730 is_properly_derived_from (derived, base)
4731 tree derived;
4732 tree base;
4734 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4735 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4736 return 0;
4738 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4739 considers every class derived from itself. */
4740 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4741 && DERIVED_FROM_P (base, derived));
4744 /* We build the ICS for an implicit object parameter as a pointer
4745 conversion sequence. However, such a sequence should be compared
4746 as if it were a reference conversion sequence. If ICS is the
4747 implicit conversion sequence for an implicit object parameter,
4748 modify it accordingly. */
4750 static void
4751 maybe_handle_implicit_object (ics)
4752 tree* ics;
4754 if (ICS_THIS_FLAG (*ics))
4756 /* [over.match.funcs]
4758 For non-static member functions, the type of the
4759 implicit object parameter is "reference to cv X"
4760 where X is the class of which the function is a
4761 member and cv is the cv-qualification on the member
4762 function declaration. */
4763 tree t = *ics;
4764 tree reference_type;
4766 /* The `this' parameter is a pointer to a class type. Make the
4767 implict conversion talk about a reference to that same class
4768 type. */
4769 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4770 reference_type = build_reference_type (reference_type);
4772 if (TREE_CODE (t) == QUAL_CONV)
4773 t = TREE_OPERAND (t, 0);
4774 if (TREE_CODE (t) == PTR_CONV)
4775 t = TREE_OPERAND (t, 0);
4776 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4777 t = direct_reference_binding (reference_type, t);
4778 *ics = t;
4782 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
4783 and return the type to which the reference refers. Otherwise,
4784 leave *ICS unchanged and return NULL_TREE. */
4786 static tree
4787 maybe_handle_ref_bind (ics)
4788 tree* ics;
4790 if (TREE_CODE (*ics) == REF_BIND)
4792 tree old_ics = *ics;
4793 tree type = TREE_TYPE (TREE_TYPE (old_ics));
4794 *ics = TREE_OPERAND (old_ics, 0);
4795 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
4796 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
4797 return type;
4800 return NULL_TREE;
4803 /* Compare two implicit conversion sequences according to the rules set out in
4804 [over.ics.rank]. Return values:
4806 1: ics1 is better than ics2
4807 -1: ics2 is better than ics1
4808 0: ics1 and ics2 are indistinguishable */
4810 static int
4811 compare_ics (ics1, ics2)
4812 tree ics1, ics2;
4814 tree from_type1;
4815 tree from_type2;
4816 tree to_type1;
4817 tree to_type2;
4818 tree deref_from_type1 = NULL_TREE;
4819 tree deref_from_type2 = NULL_TREE;
4820 tree deref_to_type1 = NULL_TREE;
4821 tree deref_to_type2 = NULL_TREE;
4822 int rank1, rank2;
4824 /* REF_BINDING is non-zero if the result of the conversion sequence
4825 is a reference type. In that case TARGET_TYPE is the
4826 type referred to by the reference. */
4827 tree target_type1;
4828 tree target_type2;
4830 /* Handle implicit object parameters. */
4831 maybe_handle_implicit_object (&ics1);
4832 maybe_handle_implicit_object (&ics2);
4834 /* Handle reference parameters. */
4835 target_type1 = maybe_handle_ref_bind (&ics1);
4836 target_type2 = maybe_handle_ref_bind (&ics2);
4838 /* [over.ics.rank]
4840 When comparing the basic forms of implicit conversion sequences (as
4841 defined in _over.best.ics_)
4843 --a standard conversion sequence (_over.ics.scs_) is a better
4844 conversion sequence than a user-defined conversion sequence
4845 or an ellipsis conversion sequence, and
4847 --a user-defined conversion sequence (_over.ics.user_) is a
4848 better conversion sequence than an ellipsis conversion sequence
4849 (_over.ics.ellipsis_). */
4850 rank1 = ICS_RANK (ics1);
4851 rank2 = ICS_RANK (ics2);
4853 if (rank1 > rank2)
4854 return -1;
4855 else if (rank1 < rank2)
4856 return 1;
4858 if (rank1 == BAD_RANK)
4860 /* XXX Isn't this an extension? */
4861 /* Both ICS are bad. We try to make a decision based on what
4862 would have happenned if they'd been good. */
4863 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4864 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4865 return -1;
4866 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4867 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4868 return 1;
4870 /* We couldn't make up our minds; try to figure it out below. */
4873 if (ICS_ELLIPSIS_FLAG (ics1))
4874 /* Both conversions are ellipsis conversions. */
4875 return 0;
4877 /* User-defined conversion sequence U1 is a better conversion sequence
4878 than another user-defined conversion sequence U2 if they contain the
4879 same user-defined conversion operator or constructor and if the sec-
4880 ond standard conversion sequence of U1 is better than the second
4881 standard conversion sequence of U2. */
4883 if (ICS_USER_FLAG (ics1))
4885 tree t1, t2;
4887 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4888 if (TREE_CODE (t1) == AMBIG_CONV)
4889 return 0;
4890 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4891 if (TREE_CODE (t2) == AMBIG_CONV)
4892 return 0;
4894 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4895 return 0;
4897 /* We can just fall through here, after setting up
4898 FROM_TYPE1 and FROM_TYPE2. */
4899 from_type1 = TREE_TYPE (t1);
4900 from_type2 = TREE_TYPE (t2);
4902 else
4904 /* We're dealing with two standard conversion sequences.
4906 [over.ics.rank]
4908 Standard conversion sequence S1 is a better conversion
4909 sequence than standard conversion sequence S2 if
4911 --S1 is a proper subsequence of S2 (comparing the conversion
4912 sequences in the canonical form defined by _over.ics.scs_,
4913 excluding any Lvalue Transformation; the identity
4914 conversion sequence is considered to be a subsequence of
4915 any non-identity conversion sequence */
4917 from_type1 = ics1;
4918 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4919 from_type1 = TREE_OPERAND (from_type1, 0);
4920 from_type1 = TREE_TYPE (from_type1);
4922 from_type2 = ics2;
4923 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4924 from_type2 = TREE_OPERAND (from_type2, 0);
4925 from_type2 = TREE_TYPE (from_type2);
4928 if (same_type_p (from_type1, from_type2))
4930 if (is_subseq (ics1, ics2))
4931 return 1;
4932 if (is_subseq (ics2, ics1))
4933 return -1;
4935 /* Otherwise, one sequence cannot be a subsequence of the other; they
4936 don't start with the same type. This can happen when comparing the
4937 second standard conversion sequence in two user-defined conversion
4938 sequences. */
4940 /* [over.ics.rank]
4942 Or, if not that,
4944 --the rank of S1 is better than the rank of S2 (by the rules
4945 defined below):
4947 Standard conversion sequences are ordered by their ranks: an Exact
4948 Match is a better conversion than a Promotion, which is a better
4949 conversion than a Conversion.
4951 Two conversion sequences with the same rank are indistinguishable
4952 unless one of the following rules applies:
4954 --A conversion that is not a conversion of a pointer, or pointer
4955 to member, to bool is better than another conversion that is such
4956 a conversion.
4958 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4959 so that we do not have to check it explicitly. */
4960 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4961 return 1;
4962 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4963 return -1;
4965 to_type1 = TREE_TYPE (ics1);
4966 to_type2 = TREE_TYPE (ics2);
4968 if (TYPE_PTR_P (from_type1)
4969 && TYPE_PTR_P (from_type2)
4970 && TYPE_PTR_P (to_type1)
4971 && TYPE_PTR_P (to_type2))
4973 deref_from_type1 = TREE_TYPE (from_type1);
4974 deref_from_type2 = TREE_TYPE (from_type2);
4975 deref_to_type1 = TREE_TYPE (to_type1);
4976 deref_to_type2 = TREE_TYPE (to_type2);
4978 /* The rules for pointers to members A::* are just like the rules
4979 for pointers A*, except opposite: if B is derived from A then
4980 A::* converts to B::*, not vice versa. For that reason, we
4981 switch the from_ and to_ variables here. */
4982 else if (TYPE_PTRMEM_P (from_type1)
4983 && TYPE_PTRMEM_P (from_type2)
4984 && TYPE_PTRMEM_P (to_type1)
4985 && TYPE_PTRMEM_P (to_type2))
4987 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4988 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4989 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4990 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4992 else if (TYPE_PTRMEMFUNC_P (from_type1)
4993 && TYPE_PTRMEMFUNC_P (from_type2)
4994 && TYPE_PTRMEMFUNC_P (to_type1)
4995 && TYPE_PTRMEMFUNC_P (to_type2))
4997 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4998 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4999 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
5000 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
5003 if (deref_from_type1 != NULL_TREE
5004 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5005 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5007 /* This was one of the pointer or pointer-like conversions.
5009 [over.ics.rank]
5011 --If class B is derived directly or indirectly from class A,
5012 conversion of B* to A* is better than conversion of B* to
5013 void*, and conversion of A* to void* is better than
5014 conversion of B* to void*. */
5015 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5016 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5018 if (is_properly_derived_from (deref_from_type1,
5019 deref_from_type2))
5020 return -1;
5021 else if (is_properly_derived_from (deref_from_type2,
5022 deref_from_type1))
5023 return 1;
5025 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5026 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5028 if (same_type_p (deref_from_type1, deref_from_type2))
5030 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5032 if (is_properly_derived_from (deref_from_type1,
5033 deref_to_type1))
5034 return 1;
5036 /* We know that DEREF_TO_TYPE1 is `void' here. */
5037 else if (is_properly_derived_from (deref_from_type1,
5038 deref_to_type2))
5039 return -1;
5042 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5043 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5045 /* [over.ics.rank]
5047 --If class B is derived directly or indirectly from class A
5048 and class C is derived directly or indirectly from B,
5050 --conversion of C* to B* is better than conversion of C* to
5051 A*,
5053 --conversion of B* to A* is better than conversion of C* to
5054 A* */
5055 if (same_type_p (deref_from_type1, deref_from_type2))
5057 if (is_properly_derived_from (deref_to_type1,
5058 deref_to_type2))
5059 return 1;
5060 else if (is_properly_derived_from (deref_to_type2,
5061 deref_to_type1))
5062 return -1;
5064 else if (same_type_p (deref_to_type1, deref_to_type2))
5066 if (is_properly_derived_from (deref_from_type2,
5067 deref_from_type1))
5068 return 1;
5069 else if (is_properly_derived_from (deref_from_type1,
5070 deref_from_type2))
5071 return -1;
5075 else if (CLASS_TYPE_P (non_reference (from_type1))
5076 && same_type_p (from_type1, from_type2))
5078 tree from = non_reference (from_type1);
5080 /* [over.ics.rank]
5082 --binding of an expression of type C to a reference of type
5083 B& is better than binding an expression of type C to a
5084 reference of type A&
5086 --conversion of C to B is better than conversion of C to A, */
5087 if (is_properly_derived_from (from, to_type1)
5088 && is_properly_derived_from (from, to_type2))
5090 if (is_properly_derived_from (to_type1, to_type2))
5091 return 1;
5092 else if (is_properly_derived_from (to_type2, to_type1))
5093 return -1;
5096 else if (CLASS_TYPE_P (non_reference (to_type1))
5097 && same_type_p (to_type1, to_type2))
5099 tree to = non_reference (to_type1);
5101 /* [over.ics.rank]
5103 --binding of an expression of type B to a reference of type
5104 A& is better than binding an expression of type C to a
5105 reference of type A&,
5107 --onversion of B to A is better than conversion of C to A */
5108 if (is_properly_derived_from (from_type1, to)
5109 && is_properly_derived_from (from_type2, to))
5111 if (is_properly_derived_from (from_type2, from_type1))
5112 return 1;
5113 else if (is_properly_derived_from (from_type1, from_type2))
5114 return -1;
5118 /* [over.ics.rank]
5120 --S1 and S2 differ only in their qualification conversion and yield
5121 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5122 qualification signature of type T1 is a proper subset of the cv-
5123 qualification signature of type T2 */
5124 if (TREE_CODE (ics1) == QUAL_CONV
5125 && TREE_CODE (ics2) == QUAL_CONV
5126 && same_type_p (from_type1, from_type2))
5127 return comp_cv_qual_signature (to_type1, to_type2);
5129 /* [over.ics.rank]
5131 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5132 types to which the references refer are the same type except for
5133 top-level cv-qualifiers, and the type to which the reference
5134 initialized by S2 refers is more cv-qualified than the type to
5135 which the reference initialized by S1 refers */
5137 if (target_type1 && target_type2
5138 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5139 return comp_cv_qualification (target_type2, target_type1);
5141 /* Neither conversion sequence is better than the other. */
5142 return 0;
5145 /* The source type for this standard conversion sequence. */
5147 static tree
5148 source_type (t)
5149 tree t;
5151 for (;; t = TREE_OPERAND (t, 0))
5153 if (TREE_CODE (t) == USER_CONV
5154 || TREE_CODE (t) == AMBIG_CONV
5155 || TREE_CODE (t) == IDENTITY_CONV)
5156 return TREE_TYPE (t);
5158 abort ();
5161 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5162 a pointer to LOSER and re-running joust to produce the warning if WINNER
5163 is actually used. */
5165 static void
5166 add_warning (winner, loser)
5167 struct z_candidate *winner, *loser;
5169 winner->warnings = tree_cons (NULL_TREE,
5170 build_ptr_wrapper (loser),
5171 winner->warnings);
5174 /* Returns true iff functions are equivalent. Equivalent functions are
5175 not '==' only if one is a function-local extern function or if
5176 both are extern "C". */
5178 static inline int
5179 equal_functions (fn1, fn2)
5180 tree fn1;
5181 tree fn2;
5183 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5184 || DECL_EXTERN_C_FUNCTION_P (fn1))
5185 return decls_match (fn1, fn2);
5186 return fn1 == fn2;
5189 /* Compare two candidates for overloading as described in
5190 [over.match.best]. Return values:
5192 1: cand1 is better than cand2
5193 -1: cand2 is better than cand1
5194 0: cand1 and cand2 are indistinguishable */
5196 static int
5197 joust (cand1, cand2, warn)
5198 struct z_candidate *cand1, *cand2;
5199 int warn;
5201 int winner = 0;
5202 int i, off1 = 0, off2 = 0, len;
5204 /* Candidates that involve bad conversions are always worse than those
5205 that don't. */
5206 if (cand1->viable > cand2->viable)
5207 return 1;
5208 if (cand1->viable < cand2->viable)
5209 return -1;
5211 /* If we have two pseudo-candidates for conversions to the same type,
5212 or two candidates for the same function, arbitrarily pick one. */
5213 if (cand1->fn == cand2->fn
5214 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5215 return 1;
5217 /* a viable function F1
5218 is defined to be a better function than another viable function F2 if
5219 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5220 ICSi(F2), and then */
5222 /* for some argument j, ICSj(F1) is a better conversion sequence than
5223 ICSj(F2) */
5225 /* For comparing static and non-static member functions, we ignore
5226 the implicit object parameter of the non-static function. The
5227 standard says to pretend that the static function has an object
5228 parm, but that won't work with operator overloading. */
5229 len = TREE_VEC_LENGTH (cand1->convs);
5230 if (len != TREE_VEC_LENGTH (cand2->convs))
5232 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5233 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5234 off2 = 1;
5235 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5236 && DECL_STATIC_FUNCTION_P (cand2->fn))
5238 off1 = 1;
5239 --len;
5241 else
5242 abort ();
5245 for (i = 0; i < len; ++i)
5247 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5248 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5249 int comp = compare_ics (t1, t2);
5251 if (comp != 0)
5253 if (warn_sign_promo
5254 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5255 && TREE_CODE (t1) == STD_CONV
5256 && TREE_CODE (t2) == STD_CONV
5257 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5258 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5259 && (TYPE_PRECISION (TREE_TYPE (t1))
5260 == TYPE_PRECISION (TREE_TYPE (t2)))
5261 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5262 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5263 == ENUMERAL_TYPE)))
5265 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5266 tree type1, type2;
5267 struct z_candidate *w, *l;
5268 if (comp > 0)
5269 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5270 w = cand1, l = cand2;
5271 else
5272 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5273 w = cand2, l = cand1;
5275 if (warn)
5277 warning ("passing `%T' chooses `%T' over `%T'",
5278 type, type1, type2);
5279 warning (" in call to `%D'", w->fn);
5281 else
5282 add_warning (w, l);
5285 if (winner && comp != winner)
5287 winner = 0;
5288 goto tweak;
5290 winner = comp;
5294 /* warn about confusing overload resolution for user-defined conversions,
5295 either between a constructor and a conversion op, or between two
5296 conversion ops. */
5297 if (winner && cand1->second_conv
5298 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5299 != DECL_CONSTRUCTOR_P (cand2->fn))
5300 /* Don't warn if the two conv ops convert to the same type... */
5301 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5302 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5303 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5305 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5306 if (comp != winner)
5308 struct z_candidate *w, *l;
5309 tree convn;
5310 if (winner == 1)
5311 w = cand1, l = cand2;
5312 else
5313 w = cand2, l = cand1;
5314 if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5315 && ! DECL_CONSTRUCTOR_P (cand1->fn)
5316 && ! DECL_CONSTRUCTOR_P (cand2->fn)
5317 && (convn = standard_conversion
5318 (TREE_TYPE (TREE_TYPE (l->fn)),
5319 TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5320 && TREE_CODE (convn) == QUAL_CONV)
5321 /* Don't complain about `operator char *()' beating
5322 `operator const char *() const'. */;
5323 else if (warn)
5325 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5326 if (! DECL_CONSTRUCTOR_P (w->fn))
5327 source = TREE_TYPE (source);
5328 warning ("choosing `%D' over `%D'", w->fn, l->fn);
5329 warning (" for conversion from `%T' to `%T'",
5330 source, TREE_TYPE (w->second_conv));
5331 warning (" because conversion sequence for the argument is better");
5333 else
5334 add_warning (w, l);
5338 if (winner)
5339 return winner;
5341 /* or, if not that,
5342 F1 is a non-template function and F2 is a template function
5343 specialization. */
5345 if (! cand1->template && cand2->template)
5346 return 1;
5347 else if (cand1->template && ! cand2->template)
5348 return -1;
5350 /* or, if not that,
5351 F1 and F2 are template functions and the function template for F1 is
5352 more specialized than the template for F2 according to the partial
5353 ordering rules. */
5355 if (cand1->template && cand2->template)
5357 winner = more_specialized
5358 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5359 DEDUCE_ORDER,
5360 /* Tell the deduction code how many real function arguments
5361 we saw, not counting the implicit 'this' argument. But,
5362 add_function_candidate() suppresses the "this" argument
5363 for constructors.
5365 [temp.func.order]: The presence of unused ellipsis and default
5366 arguments has no effect on the partial ordering of function
5367 templates. */
5368 TREE_VEC_LENGTH (cand1->convs)
5369 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5370 - DECL_CONSTRUCTOR_P (cand1->fn)));
5371 /* HERE */
5372 if (winner)
5373 return winner;
5376 /* or, if not that,
5377 the context is an initialization by user-defined conversion (see
5378 _dcl.init_ and _over.match.user_) and the standard conversion
5379 sequence from the return type of F1 to the destination type (i.e.,
5380 the type of the entity being initialized) is a better conversion
5381 sequence than the standard conversion sequence from the return type
5382 of F2 to the destination type. */
5384 if (cand1->second_conv)
5386 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5387 if (winner)
5388 return winner;
5391 /* Check whether we can discard a builtin candidate, either because we
5392 have two identical ones or matching builtin and non-builtin candidates.
5394 (Pedantically in the latter case the builtin which matched the user
5395 function should not be added to the overload set, but we spot it here.
5397 [over.match.oper]
5398 ... the builtin candidates include ...
5399 - do not have the same parameter type list as any non-template
5400 non-member candidate. */
5402 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5403 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5405 for (i = 0; i < len; ++i)
5406 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5407 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5408 break;
5409 if (i == TREE_VEC_LENGTH (cand1->convs))
5411 if (cand1->fn == cand2->fn)
5412 /* Two built-in candidates; arbitrarily pick one. */
5413 return 1;
5414 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5415 /* cand1 is built-in; prefer cand2. */
5416 return -1;
5417 else
5418 /* cand2 is built-in; prefer cand1. */
5419 return 1;
5423 /* If the two functions are the same (this can happen with declarations
5424 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5425 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5426 && equal_functions (cand1->fn, cand2->fn))
5427 return 1;
5429 tweak:
5431 /* Extension: If the worst conversion for one candidate is worse than the
5432 worst conversion for the other, take the first. */
5433 if (!pedantic)
5435 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5436 struct z_candidate *w = 0, *l = 0;
5438 for (i = 0; i < len; ++i)
5440 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5441 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5442 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5443 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5445 if (rank1 < rank2)
5446 winner = 1, w = cand1, l = cand2;
5447 if (rank1 > rank2)
5448 winner = -1, w = cand2, l = cand1;
5449 if (winner)
5451 if (warn)
5453 pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5454 pedwarn (
5455 " because worst conversion for the former is better than worst conversion for the latter");
5457 else
5458 add_warning (w, l);
5459 return winner;
5463 my_friendly_assert (!winner, 20010121);
5464 return 0;
5467 /* Given a list of candidates for overloading, find the best one, if any.
5468 This algorithm has a worst case of O(2n) (winner is last), and a best
5469 case of O(n/2) (totally ambiguous); much better than a sorting
5470 algorithm. */
5472 static struct z_candidate *
5473 tourney (candidates)
5474 struct z_candidate *candidates;
5476 struct z_candidate *champ = candidates, *challenger;
5477 int fate;
5478 int champ_compared_to_predecessor = 0;
5480 /* Walk through the list once, comparing each current champ to the next
5481 candidate, knocking out a candidate or two with each comparison. */
5483 for (challenger = champ->next; challenger; )
5485 fate = joust (champ, challenger, 0);
5486 if (fate == 1)
5487 challenger = challenger->next;
5488 else
5490 if (fate == 0)
5492 champ = challenger->next;
5493 if (champ == 0)
5494 return 0;
5495 champ_compared_to_predecessor = 0;
5497 else
5499 champ = challenger;
5500 champ_compared_to_predecessor = 1;
5503 challenger = champ->next;
5507 /* Make sure the champ is better than all the candidates it hasn't yet
5508 been compared to. */
5510 for (challenger = candidates;
5511 challenger != champ
5512 && !(champ_compared_to_predecessor && challenger->next == champ);
5513 challenger = challenger->next)
5515 fate = joust (champ, challenger, 0);
5516 if (fate != 1)
5517 return 0;
5520 return champ;
5523 /* Returns non-zero if things of type FROM can be converted to TO. */
5526 can_convert (to, from)
5527 tree to, from;
5529 return can_convert_arg (to, from, NULL_TREE);
5532 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5535 can_convert_arg (to, from, arg)
5536 tree to, from, arg;
5538 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5539 return (t && ! ICS_BAD_FLAG (t));
5542 /* Like can_convert_arg, but allows dubious conversions as well. */
5545 can_convert_arg_bad (to, from, arg)
5546 tree to, from, arg;
5548 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5549 return !!t;
5552 /* Convert EXPR to TYPE. Return the converted expression.
5554 Note that we allow bad conversions here because by the time we get to
5555 this point we are committed to doing the conversion. If we end up
5556 doing a bad conversion, convert_like will complain. */
5558 tree
5559 perform_implicit_conversion (type, expr)
5560 tree type;
5561 tree expr;
5563 tree conv;
5565 if (expr == error_mark_node)
5566 return error_mark_node;
5567 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5568 LOOKUP_NORMAL);
5569 if (!conv)
5571 error ("could not convert `%E' to `%T'", expr, type);
5572 return error_mark_node;
5575 return convert_like (conv, expr);
5578 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5579 initializing a variable of that TYPE. Return the converted
5580 expression. */
5582 tree
5583 initialize_reference (type, expr)
5584 tree type;
5585 tree expr;
5587 tree conv;
5589 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5590 if (!conv || ICS_BAD_FLAG (conv))
5592 error ("could not convert `%E' to `%T'", expr, type);
5593 return error_mark_node;
5596 return convert_like (conv, expr);