1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 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)
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. */
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 #define convert_like(CONV, EXPR) convert_like_real (CONV, EXPR, NULL_TREE, 0, 0)
50 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) convert_like_real (CONV, EXPR, FN, ARGNO, 0)
51 static tree convert_like_real PARAMS ((tree, tree, tree, int, int));
52 static void op_error PARAMS ((enum tree_code, enum tree_code, tree, tree,
54 static tree build_object_call PARAMS ((tree, tree));
55 static tree resolve_args PARAMS ((tree));
56 static struct z_candidate * build_user_type_conversion_1
57 PARAMS ((tree, tree, int));
58 static void print_z_candidates PARAMS ((struct z_candidate *));
59 static tree build_this PARAMS ((tree));
60 static struct z_candidate * splice_viable PARAMS ((struct z_candidate *));
61 static int any_viable PARAMS ((struct z_candidate *));
62 static struct z_candidate * add_template_candidate
63 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
65 static struct z_candidate * add_template_candidate_real
66 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
67 tree, unification_kind_t));
68 static struct z_candidate * add_template_conv_candidate
69 PARAMS ((struct z_candidate *, tree, tree, tree, tree));
70 static struct z_candidate * add_builtin_candidates
71 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
73 static struct z_candidate * add_builtin_candidate
74 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
75 tree, tree, tree, tree *, tree *, int));
76 static int is_complete PARAMS ((tree));
77 static struct z_candidate * build_builtin_candidate
78 PARAMS ((struct z_candidate *, tree, tree, tree, tree *, tree *,
80 static struct z_candidate * add_conv_candidate
81 PARAMS ((struct z_candidate *, tree, tree, tree));
82 static struct z_candidate * add_function_candidate
83 PARAMS ((struct z_candidate *, tree, tree, tree, int));
84 static tree implicit_conversion PARAMS ((tree, tree, tree, int));
85 static tree standard_conversion PARAMS ((tree, tree, tree));
86 static tree reference_binding PARAMS ((tree, tree, tree, int));
87 static tree non_reference PARAMS ((tree));
88 static tree build_conv PARAMS ((enum tree_code, tree, tree));
89 static int is_subseq PARAMS ((tree, tree));
90 static int maybe_handle_ref_bind PARAMS ((tree*, tree*));
91 static void maybe_handle_implicit_object PARAMS ((tree*));
92 static struct z_candidate * add_candidate PARAMS ((struct z_candidate *,
94 static tree source_type PARAMS ((tree));
95 static void add_warning PARAMS ((struct z_candidate *, struct z_candidate *));
96 static int reference_related_p PARAMS ((tree, tree));
97 static int reference_compatible_p PARAMS ((tree, tree));
98 static tree convert_class_to_reference PARAMS ((tree, tree, tree));
99 static tree direct_reference_binding PARAMS ((tree, tree));
100 static int promoted_arithmetic_type_p PARAMS ((tree));
101 static tree conditional_conversion PARAMS ((tree, tree));
104 build_vfield_ref (datum, type)
109 if (datum == error_mark_node)
110 return error_mark_node;
112 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
113 datum = convert_from_reference (datum);
115 if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type))
116 rval = build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
117 datum, TYPE_VFIELD (type));
119 rval = build_component_ref (datum, DECL_NAME (TYPE_VFIELD (type)), NULL_TREE, 0);
124 /* Build a call to a member of an object. I.e., one that overloads
125 operator ()(), or is a pointer-to-function or pointer-to-method. */
128 build_field_call (basetype_path, instance_ptr, name, parms)
129 tree basetype_path, instance_ptr, name, parms;
131 tree field, instance;
133 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
136 /* Speed up the common case. */
137 if (instance_ptr == current_class_ptr
138 && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
141 field = lookup_field (basetype_path, name, 1, 0);
143 if (field == error_mark_node || field == NULL_TREE)
146 if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
148 /* If it's a field, try overloading operator (),
149 or calling if the field is a pointer-to-function. */
150 instance = build_indirect_ref (instance_ptr, NULL_PTR);
151 instance = build_component_ref_1 (instance, field, 0);
153 if (instance == error_mark_node)
154 return error_mark_node;
156 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
157 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
158 instance, parms, NULL_TREE);
159 else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
161 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
162 return build_function_call (instance, parms);
163 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
165 return build_function_call
166 (instance, tree_cons (NULL_TREE, instance_ptr, parms));
173 /* Returns nonzero iff the destructor name specified in NAME
174 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
178 check_dtor_name (basetype, name)
181 name = TREE_OPERAND (name, 0);
183 /* Just accept something we've already complained about. */
184 if (name == error_mark_node)
187 if (TREE_CODE (name) == TYPE_DECL)
188 name = TREE_TYPE (name);
189 else if (TYPE_P (name))
191 else if (TREE_CODE (name) == IDENTIFIER_NODE)
193 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
194 || (TREE_CODE (basetype) == ENUMERAL_TYPE
195 && name == TYPE_IDENTIFIER (basetype)))
198 name = get_type_value (name);
201 my_friendly_abort (980605);
203 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
208 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
209 This is how virtual function calls are avoided. */
212 build_scoped_method_call (exp, basetype, name, parms)
213 tree exp, basetype, name, parms;
215 /* Because this syntactic form does not allow
216 a pointer to a base class to be `stolen',
217 we need not protect the derived->base conversion
220 @@ But we do have to check access privileges later. */
222 tree type = TREE_TYPE (exp);
224 if (type == error_mark_node
225 || basetype == error_mark_node)
226 return error_mark_node;
228 if (processing_template_decl)
230 if (TREE_CODE (name) == BIT_NOT_EXPR
231 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
233 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
235 name = build_min_nt (BIT_NOT_EXPR, type);
237 name = build_min_nt (SCOPE_REF, basetype, name);
238 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
241 if (TREE_CODE (type) == REFERENCE_TYPE)
242 type = TREE_TYPE (type);
244 if (TREE_CODE (basetype) == TREE_VEC)
247 basetype = BINFO_TYPE (binfo);
252 /* Check the destructor call syntax. */
253 if (TREE_CODE (name) == BIT_NOT_EXPR)
255 /* We can get here if someone writes their destructor call like
256 `obj.NS::~T()'; this isn't really a scoped method call, so hand
258 if (TREE_CODE (basetype) == NAMESPACE_DECL)
259 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
261 if (! check_dtor_name (basetype, name))
262 cp_error ("qualified type `%T' does not match destructor name `~%T'",
263 basetype, TREE_OPERAND (name, 0));
265 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
266 that explicit ~int is caught in the parser; this deals with typedefs
267 and template parms. */
268 if (! IS_AGGR_TYPE (basetype))
270 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
271 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
272 exp, basetype, type);
274 return cp_convert (void_type_node, exp);
278 if (! is_aggr_type (basetype, 1))
279 return error_mark_node;
281 if (! IS_AGGR_TYPE (type))
283 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
285 return error_mark_node;
290 binfo = get_binfo (basetype, type, 1);
291 if (binfo == error_mark_node)
292 return error_mark_node;
294 error_not_base_type (basetype, type);
299 if (TREE_CODE (exp) == INDIRECT_REF)
300 decl = build_indirect_ref
301 (convert_pointer_to_real
302 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
304 decl = build_scoped_ref (exp, basetype);
306 /* Call to a destructor. */
307 if (TREE_CODE (name) == BIT_NOT_EXPR)
309 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
310 return cp_convert (void_type_node, exp);
312 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
313 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
317 /* Call to a method. */
318 return build_method_call (decl, name, parms, binfo,
319 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
321 return error_mark_node;
324 /* We want the address of a function or method. We avoid creating a
325 pointer-to-member function. */
328 build_addr_func (function)
331 tree type = TREE_TYPE (function);
333 /* We have to do these by hand to avoid real pointer to member
335 if (TREE_CODE (type) == METHOD_TYPE)
339 type = build_pointer_type (type);
341 if (mark_addressable (function) == 0)
342 return error_mark_node;
344 addr = build1 (ADDR_EXPR, type, function);
346 /* Address of a static or external variable or function counts
348 if (staticp (function))
349 TREE_CONSTANT (addr) = 1;
354 function = default_conversion (function);
359 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
360 POINTER_TYPE to those. Note, pointer to member function types
361 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
364 build_call (function, parms)
365 tree function, parms;
367 int is_constructor = 0;
373 function = build_addr_func (function);
375 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
377 sorry ("unable to call pointer to member function here");
378 return error_mark_node;
381 result_type = TREE_TYPE (TREE_TYPE (TREE_TYPE (function)));
383 if (TREE_CODE (function) == ADDR_EXPR
384 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
385 decl = TREE_OPERAND (function, 0);
389 /* We check both the decl and the type; a function may be known not to
390 throw without being declared throw(). */
391 nothrow = ((decl && TREE_NOTHROW (decl))
392 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
394 if (decl && DECL_CONSTRUCTOR_P (decl))
397 if (decl && ! TREE_USED (decl))
399 /* We invoke build_call directly for several library functions. */
400 if (DECL_ARTIFICIAL (decl))
403 my_friendly_abort (990125);
406 /* Don't pass empty class objects by value. This is useful
407 for tags in STL, which are used to control overload resolution.
408 We don't need to handle other cases of copying empty classes. */
409 if (! decl || ! DECL_BUILT_IN (decl))
410 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
411 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
412 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
414 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
415 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
416 TREE_VALUE (tmp), t);
419 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
420 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
421 TREE_TYPE (function) = result_type;
422 TREE_SIDE_EFFECTS (function) = 1;
423 TREE_NOTHROW (function) = nothrow;
428 /* Build something of the form ptr->method (args)
429 or object.method (args). This can also build
430 calls to constructors, and find friends.
432 Member functions always take their class variable
435 INSTANCE is a class instance.
437 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
439 PARMS help to figure out what that NAME really refers to.
441 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
442 down to the real instance type to use for access checking. We need this
443 information to get protected accesses correct. This parameter is used
444 by build_member_call.
446 FLAGS is the logical disjunction of zero or more LOOKUP_
447 flags. See cp-tree.h for more info.
449 If this is all OK, calls build_function_call with the resolved
452 This function must also handle being called to perform
453 initialization, promotion/coercion of arguments, and
454 instantiation of default parameters.
456 Note that NAME may refer to an instance variable name. If
457 `operator()()' is defined for the type of that field, then we return
460 #ifdef GATHER_STATISTICS
461 extern int n_build_method_call;
465 build_method_call (instance, name, parms, basetype_path, flags)
466 tree instance, name, parms, basetype_path;
469 tree basetype, instance_ptr;
471 #ifdef GATHER_STATISTICS
472 n_build_method_call++;
475 if (instance == error_mark_node
476 || name == error_mark_node
477 || parms == error_mark_node
478 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
479 return error_mark_node;
481 if (processing_template_decl)
483 /* We need to process template parm names here so that tsubst catches
484 them properly. Other type names can wait. */
485 if (TREE_CODE (name) == BIT_NOT_EXPR)
487 tree type = NULL_TREE;
489 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
490 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
491 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
492 type = TREE_TYPE (TREE_OPERAND (name, 0));
494 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
495 name = build_min_nt (BIT_NOT_EXPR, type);
498 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
501 if (TREE_CODE (name) == BIT_NOT_EXPR)
504 error ("destructors take no parameters");
505 basetype = TREE_TYPE (instance);
506 if (TREE_CODE (basetype) == REFERENCE_TYPE)
507 basetype = TREE_TYPE (basetype);
509 if (! check_dtor_name (basetype, name))
511 ("destructor name `~%T' does not match type `%T' of expression",
512 TREE_OPERAND (name, 0), basetype);
514 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
515 return cp_convert (void_type_node, instance);
516 instance = default_conversion (instance);
517 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
518 return build_delete (build_pointer_type (basetype),
519 instance_ptr, integer_two_node,
520 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
523 return build_new_method_call (instance, name, parms, basetype_path, flags);
526 /* New overloading code. */
536 struct z_candidate *next;
539 #define IDENTITY_RANK 0
545 #define ELLIPSIS_RANK 6
548 #define ICS_RANK(NODE) \
549 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
550 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
551 : ICS_USER_FLAG (NODE) ? USER_RANK \
552 : ICS_STD_RANK (NODE))
554 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
556 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
557 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
558 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
559 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
561 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
562 should be created to hold the result of the conversion. */
563 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
565 #define USER_CONV_CAND(NODE) \
566 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
567 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
575 A null pointer constant is an integral constant expression
576 (_expr.const_) rvalue of integer type that evaluates to zero. */
578 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
584 build_conv (code, type, from)
588 tree t = build1 (code, type, from);
589 int rank = ICS_STD_RANK (from);
601 if (rank < EXACT_RANK)
607 ICS_STD_RANK (t) = rank;
608 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
609 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
617 if (TREE_CODE (t) == REFERENCE_TYPE)
626 if (TREE_CODE (t) == ARRAY_TYPE)
628 return TYPE_MAIN_VARIANT (t);
631 /* Returns the standard conversion path (see [conv]) from type FROM to type
632 TO, if any. For proper handling of null pointer constants, you must
633 also pass the expression EXPR to convert from. */
636 standard_conversion (to, from, expr)
639 enum tree_code fcode, tcode;
643 if (TREE_CODE (to) == REFERENCE_TYPE)
645 if (TREE_CODE (from) == REFERENCE_TYPE)
648 from = TREE_TYPE (from);
650 to = strip_top_quals (to);
651 from = strip_top_quals (from);
653 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
654 && expr && type_unknown_p (expr))
656 expr = instantiate_type (to, expr, 0);
657 if (expr == error_mark_node)
659 from = TREE_TYPE (expr);
662 fcode = TREE_CODE (from);
663 tcode = TREE_CODE (to);
665 conv = build1 (IDENTITY_CONV, from, expr);
667 if (fcode == FUNCTION_TYPE)
669 from = build_pointer_type (from);
670 fcode = TREE_CODE (from);
671 conv = build_conv (LVALUE_CONV, from, conv);
673 else if (fcode == ARRAY_TYPE)
675 from = build_pointer_type (TREE_TYPE (from));
676 fcode = TREE_CODE (from);
677 conv = build_conv (LVALUE_CONV, from, conv);
679 else if (fromref || (expr && real_lvalue_p (expr)))
680 conv = build_conv (RVALUE_CONV, from, conv);
682 if (same_type_p (from, to))
685 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
686 && expr && null_ptr_cst_p (expr))
688 conv = build_conv (STD_CONV, to, conv);
690 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
692 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
693 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
695 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
696 TYPE_MAIN_VARIANT (TREE_TYPE (to))))
698 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
699 && ufcode != FUNCTION_TYPE)
701 from = build_pointer_type
702 (cp_build_qualified_type (void_type_node,
703 CP_TYPE_QUALS (TREE_TYPE (from))));
704 conv = build_conv (PTR_CONV, from, conv);
706 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
708 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
709 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
711 if (DERIVED_FROM_P (fbase, tbase)
713 (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
714 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))))))
716 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
717 from = build_pointer_type (from);
718 conv = build_conv (PMEM_CONV, from, conv);
721 else if (IS_AGGR_TYPE (TREE_TYPE (from))
722 && IS_AGGR_TYPE (TREE_TYPE (to)))
724 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
727 cp_build_qualified_type (TREE_TYPE (to),
728 CP_TYPE_QUALS (TREE_TYPE (from)));
729 from = build_pointer_type (from);
730 conv = build_conv (PTR_CONV, from, conv);
734 if (same_type_p (from, to))
736 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
737 conv = build_conv (QUAL_CONV, to, conv);
738 else if (expr && string_conv_p (to, expr, 0))
739 /* converting from string constant to char *. */
740 conv = build_conv (QUAL_CONV, to, conv);
741 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
743 conv = build_conv (PTR_CONV, to, conv);
744 ICS_BAD_FLAG (conv) = 1;
751 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
753 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
754 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
755 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
756 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
758 if (! DERIVED_FROM_P (fbase, tbase)
759 || ! same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
760 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
761 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
762 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
765 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
766 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
767 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
768 from = build_ptrmemfunc_type (build_pointer_type (from));
769 conv = build_conv (PMEM_CONV, from, conv);
771 else if (tcode == BOOLEAN_TYPE)
773 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
774 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
777 conv = build_conv (STD_CONV, to, conv);
778 if (fcode == POINTER_TYPE
779 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
780 ICS_STD_RANK (conv) = PBOOL_RANK;
782 /* We don't check for ENUMERAL_TYPE here because there are no standard
783 conversions to enum type. */
784 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
785 || tcode == REAL_TYPE)
787 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
789 conv = build_conv (STD_CONV, to, conv);
791 /* Give this a better rank if it's a promotion. */
792 if (to == type_promotes_to (from)
793 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
794 ICS_STD_RANK (conv) = PROMO_RANK;
796 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
797 && is_properly_derived_from (from, to))
799 if (TREE_CODE (conv) == RVALUE_CONV)
800 conv = TREE_OPERAND (conv, 0);
801 conv = build_conv (BASE_CONV, to, conv);
802 /* The derived-to-base conversion indicates the initialization
803 of a parameter with base type from an object of a derived
804 type. A temporary object is created to hold the result of
806 NEED_TEMPORARY_P (conv) = 1;
814 /* Returns non-zero if T1 is reference-related to T2. */
817 reference_related_p (t1, t2)
821 t1 = TYPE_MAIN_VARIANT (t1);
822 t2 = TYPE_MAIN_VARIANT (t2);
826 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
827 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
829 return (same_type_p (t1, t2)
830 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
831 && DERIVED_FROM_P (t1, t2)));
834 /* Returns non-zero if T1 is reference-compatible with T2. */
837 reference_compatible_p (t1, t2)
843 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
844 reference-related to T2 and cv1 is the same cv-qualification as,
845 or greater cv-qualification than, cv2. */
846 return (reference_related_p (t1, t2)
847 && at_least_as_qualified_p (t1, t2));
850 /* Determine whether or not the EXPR (of class type S) can be
851 converted to T as in [over.match.ref]. */
854 convert_class_to_reference (t, s, expr)
862 struct z_candidate *candidates;
863 struct z_candidate *cand;
867 Assuming that "cv1 T" is the underlying type of the reference
868 being initialized, and "cv S" is the type of the initializer
869 expression, with S a class type, the candidate functions are
872 --The conversion functions of S and its base classes are
873 considered. Those that are not hidden within S and yield type
874 "reference to cv2 T2", where "cv1 T" is reference-compatible
875 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
877 The argument list has one argument, which is the initializer
882 /* Conceptually, we should take the address of EXPR and put it in
883 the argument list. Unfortunately, however, that can result in
884 error messages, which we should not issue now because we are just
885 trying to find a conversion operator. Therefore, we use NULL,
886 cast to the appropriate type. */
887 arglist = build_int_2 (0, 0);
888 TREE_TYPE (arglist) = build_pointer_type (s);
889 arglist = build_tree_list (NULL_TREE, arglist);
891 for (conversions = lookup_conversions (s);
893 conversions = TREE_CHAIN (conversions))
895 tree fns = TREE_VALUE (conversions);
897 for (; fns; fns = OVL_NEXT (fns))
899 tree f = OVL_CURRENT (fns);
900 tree t2 = TREE_TYPE (TREE_TYPE (f));
901 struct z_candidate *old_candidates = candidates;
903 /* If this is a template function, try to get an exact
905 if (TREE_CODE (f) == TEMPLATE_DECL)
908 = add_template_candidate (candidates,
912 build_reference_type (t),
916 if (candidates != old_candidates)
918 /* Now, see if the conversion function really returns
919 an lvalue of the appropriate type. From the
920 point of view of unification, simply returning an
921 rvalue of the right type is good enough. */
923 t2 = TREE_TYPE (TREE_TYPE (f));
924 if (TREE_CODE (t2) != REFERENCE_TYPE
925 || !reference_compatible_p (t, TREE_TYPE (t2)))
926 candidates = candidates->next;
929 else if (TREE_CODE (t2) == REFERENCE_TYPE
930 && reference_compatible_p (t, TREE_TYPE (t2)))
932 = add_function_candidate (candidates, f, s, arglist,
935 if (candidates != old_candidates)
936 candidates->basetype_path = TYPE_BINFO (s);
940 /* If none of the conversion functions worked out, let our caller
942 if (!any_viable (candidates))
945 candidates = splice_viable (candidates);
946 cand = tourney (candidates);
950 conv = build1 (IDENTITY_CONV, s, expr);
951 conv = build_conv (USER_CONV,
952 non_reference (TREE_TYPE (TREE_TYPE (cand->fn))),
954 TREE_OPERAND (conv, 1) = build_expr_ptr_wrapper (cand);
955 ICS_USER_FLAG (conv) = 1;
956 if (cand->viable == -1)
957 ICS_BAD_FLAG (conv) = 1;
958 cand->second_conv = conv;
963 /* A reference of the indicated TYPE is being bound directly to the
964 expression represented by the implicit conversion sequence CONV.
965 Return a conversion sequence for this binding. */
968 direct_reference_binding (type, conv)
972 tree t = TREE_TYPE (type);
976 When a parameter of reference type binds directly
977 (_dcl.init.ref_) to an argument expression, the implicit
978 conversion sequence is the identity conversion, unless the
979 argument expression has a type that is a derived class of the
980 parameter type, in which case the implicit conversion sequence is
981 a derived-to-base Conversion.
983 If the parameter binds directly to the result of applying a
984 conversion function to the argument expression, the implicit
985 conversion sequence is a user-defined conversion sequence
986 (_over.ics.user_), with the second standard conversion sequence
987 either an identity conversion or, if the conversion function
988 returns an entity of a type that is a derived class of the
989 parameter type, a derived-to-base conversion. */
990 if (!same_type_p (TYPE_MAIN_VARIANT (t),
991 TYPE_MAIN_VARIANT (TREE_TYPE (conv))))
993 /* Represent the derived-to-base conversion. */
994 conv = build_conv (BASE_CONV, t, conv);
995 /* We will actually be binding to the base-class subobject in
996 the derived class, so we mark this conversion appropriately.
997 That way, convert_like knows not to generate a temporary. */
998 NEED_TEMPORARY_P (conv) = 0;
1000 return build_conv (REF_BIND, type, conv);
1003 /* Returns the conversion path from type FROM to reference type TO for
1004 purposes of reference binding. For lvalue binding, either pass a
1005 reference type to FROM or an lvalue expression to EXPR. If the
1006 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1007 the conversion returned. */
1010 reference_binding (rto, rfrom, expr, flags)
1011 tree rto, rfrom, expr;
1014 tree conv = NULL_TREE;
1015 tree to = TREE_TYPE (rto);
1019 cp_lvalue_kind lvalue_p = clk_none;
1021 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1023 expr = instantiate_type (to, expr, 0);
1024 if (expr == error_mark_node)
1026 from = TREE_TYPE (expr);
1029 if (TREE_CODE (from) == REFERENCE_TYPE)
1031 /* Anything with reference type is an lvalue. */
1032 lvalue_p = clk_ordinary;
1033 from = TREE_TYPE (from);
1036 lvalue_p = real_lvalue_p (expr);
1038 /* Figure out whether or not the types are reference-related and
1039 reference compatible. We have do do this after stripping
1040 references from FROM. */
1041 related_p = reference_related_p (to, from);
1042 compatible_p = reference_compatible_p (to, from);
1044 if (lvalue_p && compatible_p)
1048 If the intializer expression
1050 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1051 is reference-compatible with "cv2 T2,"
1053 the reference is bound directly to the initializer exprssion
1055 conv = build1 (IDENTITY_CONV, from, expr);
1056 conv = direct_reference_binding (rto, conv);
1057 if ((lvalue_p & clk_bitfield) != 0
1058 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1059 /* For the purposes of overload resolution, we ignore the fact
1060 this expression is a bitfield. (In particular,
1061 [over.ics.ref] says specifically that a function with a
1062 non-const reference parameter is viable even if the
1063 argument is a bitfield.)
1065 However, when we actually call the function we must create
1066 a temporary to which to bind the reference. If the
1067 reference is volatile, or isn't const, then we cannot make
1068 a temporary, so we just issue an error when the conversion
1070 NEED_TEMPORARY_P (conv) = 1;
1073 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1077 If the initializer exprsesion
1079 -- has a class type (i.e., T2 is a class type) can be
1080 implicitly converted to an lvalue of type "cv3 T3," where
1081 "cv1 T1" is reference-compatible with "cv3 T3". (this
1082 conversion is selected by enumerating the applicable
1083 conversion functions (_over.match.ref_) and choosing the
1084 best one through overload resolution. (_over.match_).
1086 the reference is bound to the lvalue result of the conversion
1087 in the second case. */
1088 conv = convert_class_to_reference (to, from, expr);
1090 return direct_reference_binding (rto, conv);
1093 /* From this point on, we conceptually need temporaries, even if we
1094 elide them. Only the cases above are "direct bindings". */
1095 if (flags & LOOKUP_NO_TEMP_BIND)
1100 When a parameter of reference type is not bound directly to an
1101 argument expression, the conversion sequence is the one required
1102 to convert the argument expression to the underlying type of the
1103 reference according to _over.best.ics_. Conceptually, this
1104 conversion sequence corresponds to copy-initializing a temporary
1105 of the underlying type with the argument expression. Any
1106 difference in top-level cv-qualification is subsumed by the
1107 initialization itself and does not constitute a conversion. */
1111 Otherwise, the reference shall be to a non-volatile const type. */
1112 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1117 If the initializer expression is an rvalue, with T2 a class type,
1118 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1119 is bound in one of the following ways:
1121 -- The reference is bound to the object represented by the rvalue
1122 or to a sub-object within that object.
1124 In this case, the implicit conversion sequence is supposed to be
1125 same as we would obtain by generating a temporary. Fortunately,
1126 if the types are reference compatible, then this is either an
1127 identity conversion or the derived-to-base conversion, just as
1128 for direct binding. */
1129 if (CLASS_TYPE_P (from) && compatible_p)
1131 conv = build1 (IDENTITY_CONV, from, expr);
1132 return direct_reference_binding (rto, conv);
1137 Otherwise, a temporary of type "cv1 T1" is created and
1138 initialized from the initializer expression using the rules for a
1139 non-reference copy initialization. If T1 is reference-related to
1140 T2, cv1 must be the same cv-qualification as, or greater
1141 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1142 if (related_p && !at_least_as_qualified_p (to, from))
1145 conv = implicit_conversion (to, from, expr, flags);
1149 conv = build_conv (REF_BIND, rto, conv);
1150 /* This reference binding, unlike those above, requires the
1151 creation of a temporary. */
1152 NEED_TEMPORARY_P (conv) = 1;
1157 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1158 to type TO. The optional expression EXPR may affect the conversion.
1159 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1163 implicit_conversion (to, from, expr, flags)
1164 tree to, from, expr;
1168 struct z_candidate *cand;
1170 /* Resolve expressions like `A::p' that we thought might become
1171 pointers-to-members. */
1172 if (expr && TREE_CODE (expr) == OFFSET_REF)
1174 expr = resolve_offset_ref (expr);
1175 from = TREE_TYPE (expr);
1178 if (from == error_mark_node || to == error_mark_node
1179 || expr == error_mark_node)
1182 /* Make sure both the FROM and TO types are complete so that
1183 user-defined conversions are available. */
1184 complete_type (from);
1187 if (TREE_CODE (to) == REFERENCE_TYPE)
1188 conv = reference_binding (to, from, expr, flags);
1190 conv = standard_conversion (to, from, expr);
1194 else if (expr != NULL_TREE
1195 && (IS_AGGR_TYPE (non_reference (from))
1196 || IS_AGGR_TYPE (non_reference (to)))
1197 && (flags & LOOKUP_NO_CONVERSION) == 0)
1199 cand = build_user_type_conversion_1
1200 (to, expr, LOOKUP_ONLYCONVERTING);
1202 conv = cand->second_conv;
1203 if ((! conv || ICS_BAD_FLAG (conv))
1204 && TREE_CODE (to) == REFERENCE_TYPE
1205 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1207 cand = build_user_type_conversion_1
1208 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1211 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to)))
1212 ICS_BAD_FLAG (cand->second_conv) = 1;
1213 if (!conv || (ICS_BAD_FLAG (conv)
1214 > ICS_BAD_FLAG (cand->second_conv)))
1215 conv = build_conv (REF_BIND, to, cand->second_conv);
1223 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1226 static struct z_candidate *
1227 add_candidate (candidates, fn, convs, viable)
1228 struct z_candidate *candidates;
1232 struct z_candidate *cand
1233 = (struct z_candidate *) ggc_alloc_obj (sizeof (struct z_candidate), 1);
1236 cand->convs = convs;
1237 cand->viable = viable;
1238 cand->next = candidates;
1243 /* Create an overload candidate for the function or method FN called with
1244 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1245 to implicit_conversion.
1247 CTYPE, if non-NULL, is the type we want to pretend this function
1248 comes from for purposes of overload resolution. */
1250 static struct z_candidate *
1251 add_function_candidate (candidates, fn, ctype, arglist, flags)
1252 struct z_candidate *candidates;
1253 tree fn, ctype, arglist;
1256 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1259 tree parmnode, argnode;
1262 /* The `this' and `in_chrg' arguments to constructors are not considered
1263 in overload resolution. */
1264 if (DECL_CONSTRUCTOR_P (fn))
1266 parmlist = TREE_CHAIN (parmlist);
1267 arglist = TREE_CHAIN (arglist);
1268 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1270 parmlist = TREE_CHAIN (parmlist);
1271 arglist = TREE_CHAIN (arglist);
1275 len = list_length (arglist);
1276 convs = make_tree_vec (len);
1278 /* 13.3.2 - Viable functions [over.match.viable]
1279 First, to be a viable function, a candidate function shall have enough
1280 parameters to agree in number with the arguments in the list.
1282 We need to check this first; otherwise, checking the ICSes might cause
1283 us to produce an ill-formed template instantiation. */
1285 parmnode = parmlist;
1286 for (i = 0; i < len; ++i)
1288 if (parmnode == NULL_TREE || parmnode == void_list_node)
1290 parmnode = TREE_CHAIN (parmnode);
1293 if (i < len && parmnode)
1296 /* Make sure there are default args for the rest of the parms. */
1297 else for (; parmnode && parmnode != void_list_node;
1298 parmnode = TREE_CHAIN (parmnode))
1299 if (! TREE_PURPOSE (parmnode))
1308 /* Second, for F to be a viable function, there shall exist for each
1309 argument an implicit conversion sequence that converts that argument
1310 to the corresponding parameter of F. */
1312 parmnode = parmlist;
1315 for (i = 0; i < len; ++i)
1317 tree arg = TREE_VALUE (argnode);
1318 tree argtype = lvalue_type (arg);
1322 if (parmnode == void_list_node)
1325 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1326 && ! DECL_CONSTRUCTOR_P (fn));
1330 tree parmtype = TREE_VALUE (parmnode);
1332 /* The type of the implicit object parameter ('this') for
1333 overload resolution is not always the same as for the
1334 function itself; conversion functions are considered to
1335 be members of the class being converted, and functions
1336 introduced by a using-declaration are considered to be
1337 members of the class that uses them.
1339 Since build_over_call ignores the ICS for the `this'
1340 parameter, we can just change the parm type. */
1341 if (ctype && is_this)
1344 = build_qualified_type (ctype,
1345 TYPE_QUALS (TREE_TYPE (parmtype)));
1346 parmtype = build_pointer_type (parmtype);
1349 t = implicit_conversion (parmtype, argtype, arg, flags);
1353 t = build1 (IDENTITY_CONV, argtype, arg);
1354 ICS_ELLIPSIS_FLAG (t) = 1;
1358 ICS_THIS_FLAG (t) = 1;
1360 TREE_VEC_ELT (convs, i) = t;
1367 if (ICS_BAD_FLAG (t))
1371 parmnode = TREE_CHAIN (parmnode);
1372 argnode = TREE_CHAIN (argnode);
1376 return add_candidate (candidates, fn, convs, viable);
1379 /* Create an overload candidate for the conversion function FN which will
1380 be invoked for expression OBJ, producing a pointer-to-function which
1381 will in turn be called with the argument list ARGLIST, and add it to
1382 CANDIDATES. FLAGS is passed on to implicit_conversion.
1384 Actually, we don't really care about FN; we care about the type it
1385 converts to. There may be multiple conversion functions that will
1386 convert to that type, and we rely on build_user_type_conversion_1 to
1387 choose the best one; so when we create our candidate, we record the type
1388 instead of the function. */
1390 static struct z_candidate *
1391 add_conv_candidate (candidates, fn, obj, arglist)
1392 struct z_candidate *candidates;
1393 tree fn, obj, arglist;
1395 tree totype = TREE_TYPE (TREE_TYPE (fn));
1396 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1397 int i, len = list_length (arglist) + 1;
1398 tree convs = make_tree_vec (len);
1399 tree parmnode = parmlist;
1400 tree argnode = arglist;
1402 int flags = LOOKUP_NORMAL;
1404 /* Don't bother looking up the same type twice. */
1405 if (candidates && candidates->fn == totype)
1408 for (i = 0; i < len; ++i)
1410 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1411 tree argtype = lvalue_type (arg);
1415 t = implicit_conversion (totype, argtype, arg, flags);
1416 else if (parmnode == void_list_node)
1419 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1422 t = build1 (IDENTITY_CONV, argtype, arg);
1423 ICS_ELLIPSIS_FLAG (t) = 1;
1426 TREE_VEC_ELT (convs, i) = t;
1430 if (ICS_BAD_FLAG (t))
1437 parmnode = TREE_CHAIN (parmnode);
1438 argnode = TREE_CHAIN (argnode);
1444 for (; parmnode && parmnode != void_list_node;
1445 parmnode = TREE_CHAIN (parmnode))
1446 if (! TREE_PURPOSE (parmnode))
1452 return add_candidate (candidates, totype, convs, viable);
1455 static struct z_candidate *
1456 build_builtin_candidate (candidates, fnname, type1, type2,
1457 args, argtypes, flags)
1458 struct z_candidate *candidates;
1459 tree fnname, type1, type2, *args, *argtypes;
1470 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1472 for (i = 0; i < 2; ++i)
1477 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1481 /* We need something for printing the candidate. */
1482 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1484 else if (ICS_BAD_FLAG (t))
1486 TREE_VEC_ELT (convs, i) = t;
1489 /* For COND_EXPR we rearranged the arguments; undo that now. */
1492 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1493 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1494 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1496 TREE_VEC_ELT (convs, 0) = t;
1501 return add_candidate (candidates, fnname, convs, viable);
1508 return COMPLETE_TYPE_P (complete_type (t));
1511 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1514 promoted_arithmetic_type_p (type)
1519 In this section, the term promoted integral type is used to refer
1520 to those integral types which are preserved by integral promotion
1521 (including e.g. int and long but excluding e.g. char).
1522 Similarly, the term promoted arithmetic type refers to promoted
1523 integral types plus floating types. */
1524 return ((INTEGRAL_TYPE_P (type)
1525 && same_type_p (type_promotes_to (type), type))
1526 || TREE_CODE (type) == REAL_TYPE);
1529 /* Create any builtin operator overload candidates for the operator in
1530 question given the converted operand types TYPE1 and TYPE2. The other
1531 args are passed through from add_builtin_candidates to
1532 build_builtin_candidate. */
1534 static struct z_candidate *
1535 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1536 args, argtypes, flags)
1537 struct z_candidate *candidates;
1538 enum tree_code code, code2;
1539 tree fnname, type1, type2, *args, *argtypes;
1544 case POSTINCREMENT_EXPR:
1545 case POSTDECREMENT_EXPR:
1546 args[1] = integer_zero_node;
1547 type2 = integer_type_node;
1556 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1557 and VQ is either volatile or empty, there exist candidate operator
1558 functions of the form
1559 VQ T& operator++(VQ T&);
1560 T operator++(VQ T&, int);
1561 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1562 type other than bool, and VQ is either volatile or empty, there exist
1563 candidate operator functions of the form
1564 VQ T& operator--(VQ T&);
1565 T operator--(VQ T&, int);
1566 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1567 complete object type, and VQ is either volatile or empty, there exist
1568 candidate operator functions of the form
1569 T*VQ& operator++(T*VQ&);
1570 T*VQ& operator--(T*VQ&);
1571 T* operator++(T*VQ&, int);
1572 T* operator--(T*VQ&, int); */
1574 case POSTDECREMENT_EXPR:
1575 case PREDECREMENT_EXPR:
1576 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1578 case POSTINCREMENT_EXPR:
1579 case PREINCREMENT_EXPR:
1580 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1581 || TYPE_PTROB_P (type1))
1583 type1 = build_reference_type (type1);
1588 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1589 exist candidate operator functions of the form
1593 8 For every function type T, there exist candidate operator functions of
1595 T& operator*(T*); */
1598 if (TREE_CODE (type1) == POINTER_TYPE
1599 && (TYPE_PTROB_P (type1)
1600 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1604 /* 9 For every type T, there exist candidate operator functions of the form
1607 10For every promoted arithmetic type T, there exist candidate operator
1608 functions of the form
1612 case CONVERT_EXPR: /* unary + */
1613 if (TREE_CODE (type1) == POINTER_TYPE
1614 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1617 if (ARITHMETIC_TYPE_P (type1))
1621 /* 11For every promoted integral type T, there exist candidate operator
1622 functions of the form
1626 if (INTEGRAL_TYPE_P (type1))
1630 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1631 is the same type as C2 or is a derived class of C2, T is a complete
1632 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1633 there exist candidate operator functions of the form
1634 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1635 where CV12 is the union of CV1 and CV2. */
1638 if (TREE_CODE (type1) == POINTER_TYPE
1639 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1641 tree c1 = TREE_TYPE (type1);
1642 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1643 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1644 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1646 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1647 && (TYPE_PTRMEMFUNC_P (type2)
1648 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1653 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1654 didate operator functions of the form
1659 bool operator<(L, R);
1660 bool operator>(L, R);
1661 bool operator<=(L, R);
1662 bool operator>=(L, R);
1663 bool operator==(L, R);
1664 bool operator!=(L, R);
1665 where LR is the result of the usual arithmetic conversions between
1668 14For every pair of types T and I, where T is a cv-qualified or cv-
1669 unqualified complete object type and I is a promoted integral type,
1670 there exist candidate operator functions of the form
1671 T* operator+(T*, I);
1672 T& operator[](T*, I);
1673 T* operator-(T*, I);
1674 T* operator+(I, T*);
1675 T& operator[](I, T*);
1677 15For every T, where T is a pointer to complete object type, there exist
1678 candidate operator functions of the form112)
1679 ptrdiff_t operator-(T, T);
1681 16For every pointer type T, there exist candidate operator functions of
1683 bool operator<(T, T);
1684 bool operator>(T, T);
1685 bool operator<=(T, T);
1686 bool operator>=(T, T);
1687 bool operator==(T, T);
1688 bool operator!=(T, T);
1690 17For every pointer to member type T, there exist candidate operator
1691 functions of the form
1692 bool operator==(T, T);
1693 bool operator!=(T, T); */
1696 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1698 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1700 type2 = ptrdiff_type_node;
1704 case TRUNC_DIV_EXPR:
1705 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1711 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1712 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1714 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1715 && null_ptr_cst_p (args[1]))
1720 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1721 && null_ptr_cst_p (args[0]))
1732 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1733 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1735 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1740 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1748 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1751 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1753 type1 = ptrdiff_type_node;
1756 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1758 type2 = ptrdiff_type_node;
1763 /* 18For every pair of promoted integral types L and R, there exist candi-
1764 date operator functions of the form
1771 where LR is the result of the usual arithmetic conversions between
1774 case TRUNC_MOD_EXPR:
1780 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1784 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1785 type, VQ is either volatile or empty, and R is a promoted arithmetic
1786 type, there exist candidate operator functions of the form
1787 VQ L& operator=(VQ L&, R);
1788 VQ L& operator*=(VQ L&, R);
1789 VQ L& operator/=(VQ L&, R);
1790 VQ L& operator+=(VQ L&, R);
1791 VQ L& operator-=(VQ L&, R);
1793 20For every pair T, VQ), where T is any type and VQ is either volatile
1794 or empty, there exist candidate operator functions of the form
1795 T*VQ& operator=(T*VQ&, T*);
1797 21For every pair T, VQ), where T is a pointer to member type and VQ is
1798 either volatile or empty, there exist candidate operator functions of
1800 VQ T& operator=(VQ T&, T);
1802 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1803 unqualified complete object type, VQ is either volatile or empty, and
1804 I is a promoted integral type, there exist candidate operator func-
1806 T*VQ& operator+=(T*VQ&, I);
1807 T*VQ& operator-=(T*VQ&, I);
1809 23For every triple L, VQ, R), where L is an integral or enumeration
1810 type, VQ is either volatile or empty, and R is a promoted integral
1811 type, there exist candidate operator functions of the form
1813 VQ L& operator%=(VQ L&, R);
1814 VQ L& operator<<=(VQ L&, R);
1815 VQ L& operator>>=(VQ L&, R);
1816 VQ L& operator&=(VQ L&, R);
1817 VQ L& operator^=(VQ L&, R);
1818 VQ L& operator|=(VQ L&, R); */
1825 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1827 type2 = ptrdiff_type_node;
1831 case TRUNC_DIV_EXPR:
1832 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1836 case TRUNC_MOD_EXPR:
1842 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1847 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1849 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1850 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1851 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1852 || ((TYPE_PTRMEMFUNC_P (type1)
1853 || TREE_CODE (type1) == POINTER_TYPE)
1854 && null_ptr_cst_p (args[1])))
1862 my_friendly_abort (367);
1864 type1 = build_reference_type (type1);
1870 For every pair of promoted arithmetic types L and R, there
1871 exist candidate operator functions of the form
1873 LR operator?(bool, L, R);
1875 where LR is the result of the usual arithmetic conversions
1876 between types L and R.
1878 For every type T, where T is a pointer or pointer-to-member
1879 type, there exist candidate operator functions of the form T
1880 operator?(bool, T, T); */
1882 if (promoted_arithmetic_type_p (type1)
1883 && promoted_arithmetic_type_p (type2))
1887 /* Otherwise, the types should be pointers. */
1888 if (!(TREE_CODE (type1) == POINTER_TYPE
1889 || TYPE_PTRMEM_P (type1)
1890 || TYPE_PTRMEMFUNC_P (type1))
1891 || !(TREE_CODE (type2) == POINTER_TYPE
1892 || TYPE_PTRMEM_P (type2)
1893 || TYPE_PTRMEMFUNC_P (type2)))
1896 /* We don't check that the two types are the same; the logic
1897 below will actually create two candidates; one in which both
1898 parameter types are TYPE1, and one in which both parameter
1902 /* These arguments do not make for a legal overloaded operator. */
1906 my_friendly_abort (367);
1909 /* If we're dealing with two pointer types, we need candidates
1910 for both of them. */
1911 if (type2 && !same_type_p (type1, type2)
1912 && TREE_CODE (type1) == TREE_CODE (type2)
1913 && (TREE_CODE (type1) == REFERENCE_TYPE
1914 || (TREE_CODE (type1) == POINTER_TYPE
1915 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1916 || TYPE_PTRMEMFUNC_P (type1)
1917 || IS_AGGR_TYPE (type1)))
1919 candidates = build_builtin_candidate
1920 (candidates, fnname, type1, type1, args, argtypes, flags);
1921 return build_builtin_candidate
1922 (candidates, fnname, type2, type2, args, argtypes, flags);
1925 return build_builtin_candidate
1926 (candidates, fnname, type1, type2, args, argtypes, flags);
1930 type_decays_to (type)
1933 if (TREE_CODE (type) == ARRAY_TYPE)
1934 return build_pointer_type (TREE_TYPE (type));
1935 if (TREE_CODE (type) == FUNCTION_TYPE)
1936 return build_pointer_type (type);
1940 /* There are three conditions of builtin candidates:
1942 1) bool-taking candidates. These are the same regardless of the input.
1943 2) pointer-pair taking candidates. These are generated for each type
1944 one of the input types converts to.
1945 3) arithmetic candidates. According to the standard, we should generate
1946 all of these, but I'm trying not to... */
1948 static struct z_candidate *
1949 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1950 struct z_candidate *candidates;
1951 enum tree_code code, code2;
1956 tree type, argtypes[3];
1957 /* TYPES[i] is the set of possible builtin-operator parameter types
1958 we will consider for the Ith argument. These are represented as
1959 a TREE_LIST; the TREE_VALUE of each node is the potential
1963 for (i = 0; i < 3; ++i)
1966 argtypes[i] = lvalue_type (args[i]);
1968 argtypes[i] = NULL_TREE;
1973 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1974 and VQ is either volatile or empty, there exist candidate operator
1975 functions of the form
1976 VQ T& operator++(VQ T&); */
1978 case POSTINCREMENT_EXPR:
1979 case PREINCREMENT_EXPR:
1980 case POSTDECREMENT_EXPR:
1981 case PREDECREMENT_EXPR:
1986 /* 24There also exist candidate operator functions of the form
1987 bool operator!(bool);
1988 bool operator&&(bool, bool);
1989 bool operator||(bool, bool); */
1991 case TRUTH_NOT_EXPR:
1992 return build_builtin_candidate
1993 (candidates, fnname, boolean_type_node,
1994 NULL_TREE, args, argtypes, flags);
1996 case TRUTH_ORIF_EXPR:
1997 case TRUTH_ANDIF_EXPR:
1998 return build_builtin_candidate
1999 (candidates, fnname, boolean_type_node,
2000 boolean_type_node, args, argtypes, flags);
2011 types[0] = types[1] = NULL_TREE;
2013 for (i = 0; i < 2; ++i)
2017 else if (IS_AGGR_TYPE (argtypes[i]))
2021 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2024 convs = lookup_conversions (argtypes[i]);
2026 if (code == COND_EXPR)
2028 if (real_lvalue_p (args[i]))
2029 types[i] = tree_cons
2030 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2032 types[i] = tree_cons
2033 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2039 for (; convs; convs = TREE_CHAIN (convs))
2041 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2044 && (TREE_CODE (type) != REFERENCE_TYPE
2045 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2048 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2049 types[i] = tree_cons (NULL_TREE, type, types[i]);
2051 type = non_reference (type);
2052 if (i != 0 || ! ref1)
2054 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2055 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2056 types[i] = tree_cons (NULL_TREE, type, types[i]);
2057 if (INTEGRAL_TYPE_P (type))
2058 type = type_promotes_to (type);
2061 if (! value_member (type, types[i]))
2062 types[i] = tree_cons (NULL_TREE, type, types[i]);
2067 if (code == COND_EXPR && real_lvalue_p (args[i]))
2068 types[i] = tree_cons
2069 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2070 type = non_reference (argtypes[i]);
2071 if (i != 0 || ! ref1)
2073 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2074 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2075 types[i] = tree_cons (NULL_TREE, type, types[i]);
2076 if (INTEGRAL_TYPE_P (type))
2077 type = type_promotes_to (type);
2079 types[i] = tree_cons (NULL_TREE, type, types[i]);
2083 /* Run through the possible parameter types of both arguments,
2084 creating candidates with those parameter types. */
2085 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2088 for (type = types[1]; type; type = TREE_CHAIN (type))
2089 candidates = add_builtin_candidate
2090 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2091 TREE_VALUE (type), args, argtypes, flags);
2093 candidates = add_builtin_candidate
2094 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2095 NULL_TREE, args, argtypes, flags);
2102 /* If TMPL can be successfully instantiated as indicated by
2103 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2105 TMPL is the template. EXPLICIT_TARGS are any explicit template
2106 arguments. ARGLIST is the arguments provided at the call-site.
2107 The RETURN_TYPE is the desired type for conversion operators. If
2108 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2109 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2110 add_conv_candidate. */
2112 static struct z_candidate*
2113 add_template_candidate_real (candidates, tmpl, ctype, explicit_targs,
2114 arglist, return_type, flags,
2116 struct z_candidate *candidates;
2117 tree tmpl, ctype, explicit_targs, arglist, return_type;
2120 unification_kind_t strict;
2122 int ntparms = DECL_NTPARMS (tmpl);
2123 tree targs = make_tree_vec (ntparms);
2124 struct z_candidate *cand;
2128 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
2129 return_type, strict);
2134 fn = instantiate_template (tmpl, targs);
2135 if (fn == error_mark_node)
2138 if (obj != NULL_TREE)
2139 /* Aha, this is a conversion function. */
2140 cand = add_conv_candidate (candidates, fn, obj, arglist);
2142 cand = add_function_candidate (candidates, fn, ctype,
2144 if (DECL_TI_TEMPLATE (fn) != tmpl)
2145 /* This situation can occur if a member template of a template
2146 class is specialized. Then, instantiate_template might return
2147 an instantiation of the specialization, in which case the
2148 DECL_TI_TEMPLATE field will point at the original
2149 specialization. For example:
2151 template <class T> struct S { template <class U> void f(U);
2152 template <> void f(int) {}; };
2156 Here, TMPL will be template <class U> S<double>::f(U).
2157 And, instantiate template will give us the specialization
2158 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2159 for this will point at template <class T> template <> S<T>::f(int),
2160 so that we can find the definition. For the purposes of
2161 overload resolution, however, we want the original TMPL. */
2162 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2164 cand->template = DECL_TEMPLATE_INFO (fn);
2170 static struct z_candidate *
2171 add_template_candidate (candidates, tmpl, ctype, explicit_targs,
2172 arglist, return_type, flags, strict)
2173 struct z_candidate *candidates;
2174 tree tmpl, ctype, explicit_targs, arglist, return_type;
2176 unification_kind_t strict;
2179 add_template_candidate_real (candidates, tmpl, ctype,
2180 explicit_targs, arglist, return_type, flags,
2185 static struct z_candidate *
2186 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2187 struct z_candidate *candidates;
2188 tree tmpl, obj, arglist, return_type;
2191 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2192 arglist, return_type, 0, obj, DEDUCE_CONV);
2198 struct z_candidate *cands;
2200 for (; cands; cands = cands->next)
2201 if (pedantic ? cands->viable == 1 : cands->viable)
2206 static struct z_candidate *
2207 splice_viable (cands)
2208 struct z_candidate *cands;
2210 struct z_candidate **p = &cands;
2214 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2227 /* Fix this to work on non-lvalues. */
2228 return build_unary_op (ADDR_EXPR, obj, 0);
2232 print_z_candidates (candidates)
2233 struct z_candidate *candidates;
2235 const char *str = "candidates are:";
2236 for (; candidates; candidates = candidates->next)
2238 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2240 if (candidates->fn == ansi_opname [COND_EXPR])
2241 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2242 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2243 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2244 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2245 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2246 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2247 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2248 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2250 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2251 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2253 else if (TYPE_P (candidates->fn))
2254 cp_error ("%s %T <conversion>", str, candidates->fn);
2256 cp_error_at ("%s %+#D%s", str, candidates->fn,
2257 candidates->viable == -1 ? " <near match>" : "");
2262 /* Returns the best overload candidate to perform the requested
2263 conversion. This function is used for three the overloading situations
2264 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2265 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2266 per [dcl.init.ref], so we ignore temporary bindings. */
2268 static struct z_candidate *
2269 build_user_type_conversion_1 (totype, expr, flags)
2273 struct z_candidate *candidates, *cand;
2274 tree fromtype = TREE_TYPE (expr);
2275 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2276 tree args = NULL_TREE;
2277 tree templates = NULL_TREE;
2279 if (IS_AGGR_TYPE (totype))
2280 ctors = lookup_fnfields (TYPE_BINFO (totype),
2282 ? complete_ctor_identifier
2286 if (IS_AGGR_TYPE (fromtype)
2287 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2288 convs = lookup_conversions (fromtype);
2291 flags |= LOOKUP_NO_CONVERSION;
2297 ctors = TREE_VALUE (ctors);
2299 t = build_int_2 (0, 0);
2300 TREE_TYPE (t) = build_pointer_type (totype);
2301 args = build_tree_list (NULL_TREE, expr);
2302 if (DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors)))
2303 args = tree_cons (NULL_TREE, integer_one_node, args);
2304 args = tree_cons (NULL_TREE, t, args);
2306 for (; ctors; ctors = OVL_NEXT (ctors))
2308 tree ctor = OVL_CURRENT (ctors);
2309 if (DECL_NONCONVERTING_P (ctor))
2312 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2314 templates = tree_cons (NULL_TREE, ctor, templates);
2316 add_template_candidate (candidates, ctor, totype,
2317 NULL_TREE, args, NULL_TREE, flags,
2321 candidates = add_function_candidate (candidates, ctor, totype,
2326 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2327 candidates->basetype_path = TYPE_BINFO (totype);
2332 args = build_tree_list (NULL_TREE, build_this (expr));
2334 for (; convs; convs = TREE_CHAIN (convs))
2336 tree fns = TREE_VALUE (convs);
2337 int convflags = LOOKUP_NO_CONVERSION;
2340 /* If we are called to convert to a reference type, we are trying to
2341 find an lvalue binding, so don't even consider temporaries. If
2342 we don't find an lvalue binding, the caller will try again to
2343 look for a temporary binding. */
2344 if (TREE_CODE (totype) == REFERENCE_TYPE)
2345 convflags |= LOOKUP_NO_TEMP_BIND;
2347 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2348 ics = implicit_conversion
2349 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2351 /* We can't compute this yet. */
2352 ics = error_mark_node;
2354 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2355 /* ignore the near match. */;
2357 for (; fns; fns = OVL_NEXT (fns))
2359 tree fn = OVL_CURRENT (fns);
2360 struct z_candidate *old_candidates = candidates;
2362 /* [over.match.funcs] For conversion functions, the function is
2363 considered to be a member of the class of the implicit object
2364 argument for the purpose of defining the type of the implicit
2367 So we pass fromtype as CTYPE to add_*_candidate. */
2369 if (TREE_CODE (fn) == TEMPLATE_DECL)
2371 templates = tree_cons (NULL_TREE, fn, templates);
2373 add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2374 args, totype, flags,
2378 candidates = add_function_candidate (candidates, fn, fromtype,
2381 if (candidates != old_candidates)
2383 if (TREE_CODE (fn) == TEMPLATE_DECL)
2384 ics = implicit_conversion
2385 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2388 candidates->second_conv = ics;
2389 candidates->basetype_path = TYPE_BINFO (fromtype);
2391 if (ics == NULL_TREE)
2392 candidates->viable = 0;
2393 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2394 candidates->viable = -1;
2399 if (! any_viable (candidates))
2402 if (flags & LOOKUP_COMPLAIN)
2404 if (candidates && ! candidates->next)
2405 /* say why this one won't work or try to be loose */;
2407 cp_error ("no viable candidates");
2414 candidates = splice_viable (candidates);
2415 cand = tourney (candidates);
2419 if (flags & LOOKUP_COMPLAIN)
2421 cp_error ("conversion from `%T' to `%T' is ambiguous",
2423 print_z_candidates (candidates);
2426 cand = candidates; /* any one will do */
2427 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2428 ICS_USER_FLAG (cand->second_conv) = 1;
2429 ICS_BAD_FLAG (cand->second_conv) = 1;
2434 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2435 p = &(TREE_OPERAND (*p, 0));
2437 /* Pedantically, normal function declarations are never considered
2438 to refer to template instantiations, so we only do this with
2440 if (flag_guiding_decls && templates && ! cand->template
2441 && !DECL_INITIAL (cand->fn)
2442 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2443 add_maybe_template (cand->fn, templates);
2447 (DECL_CONSTRUCTOR_P (cand->fn)
2448 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2449 expr, build_expr_ptr_wrapper (cand));
2450 ICS_USER_FLAG (cand->second_conv) = 1;
2451 if (cand->viable == -1)
2452 ICS_BAD_FLAG (cand->second_conv) = 1;
2458 build_user_type_conversion (totype, expr, flags)
2462 struct z_candidate *cand
2463 = build_user_type_conversion_1 (totype, expr, flags);
2467 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2468 return error_mark_node;
2469 return convert_from_reference
2470 (convert_like_with_context
2471 (cand->second_conv, expr, cand->fn, 0));
2476 /* Do any initial processing on the arguments to a function call. */
2483 for (t = args; t; t = TREE_CHAIN (t))
2485 if (TREE_VALUE (t) == error_mark_node)
2486 return error_mark_node;
2487 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2489 error ("invalid use of void expression");
2490 return error_mark_node;
2492 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2493 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2499 build_new_function_call (fn, args)
2502 struct z_candidate *candidates = 0, *cand;
2503 tree explicit_targs = NULL_TREE;
2504 int template_only = 0;
2506 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2508 explicit_targs = TREE_OPERAND (fn, 1);
2509 fn = TREE_OPERAND (fn, 0);
2513 if (really_overloaded_fn (fn))
2516 tree templates = NULL_TREE;
2518 args = resolve_args (args);
2520 if (args == error_mark_node)
2521 return error_mark_node;
2523 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2525 tree t = OVL_FUNCTION (t1);
2527 if (TREE_CODE (t) == TEMPLATE_DECL)
2529 templates = tree_cons (NULL_TREE, t, templates);
2530 candidates = add_template_candidate
2531 (candidates, t, NULL_TREE, explicit_targs, args, NULL_TREE,
2532 LOOKUP_NORMAL, DEDUCE_CALL);
2534 else if (! template_only)
2535 candidates = add_function_candidate
2536 (candidates, t, NULL_TREE, args, LOOKUP_NORMAL);
2539 if (! any_viable (candidates))
2541 if (candidates && ! candidates->next)
2542 return build_function_call (candidates->fn, args);
2543 cp_error ("no matching function for call to `%D (%A)'",
2544 DECL_NAME (OVL_FUNCTION (fn)), args);
2546 print_z_candidates (candidates);
2547 return error_mark_node;
2549 candidates = splice_viable (candidates);
2550 cand = tourney (candidates);
2554 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2555 DECL_NAME (OVL_FUNCTION (fn)), args);
2556 print_z_candidates (candidates);
2557 return error_mark_node;
2560 /* Pedantically, normal function declarations are never considered
2561 to refer to template instantiations, so we only do this with
2563 if (flag_guiding_decls && templates && ! cand->template
2564 && ! DECL_INITIAL (cand->fn))
2565 add_maybe_template (cand->fn, templates);
2567 return build_over_call (cand, args, LOOKUP_NORMAL);
2570 /* This is not really overloaded. */
2571 fn = OVL_CURRENT (fn);
2573 return build_function_call (fn, args);
2577 build_object_call (obj, args)
2580 struct z_candidate *candidates = 0, *cand;
2581 tree fns, convs, mem_args = NULL_TREE;
2582 tree type = TREE_TYPE (obj);
2584 if (TYPE_PTRMEMFUNC_P (type))
2586 /* It's no good looking for an overloaded operator() on a
2587 pointer-to-member-function. */
2588 cp_error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2589 return error_mark_node;
2592 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2593 if (fns == error_mark_node)
2594 return error_mark_node;
2596 args = resolve_args (args);
2598 if (args == error_mark_node)
2599 return error_mark_node;
2603 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
2604 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2606 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2608 tree fn = OVL_CURRENT (fns);
2609 if (TREE_CODE (fn) == TEMPLATE_DECL)
2612 = add_template_candidate (candidates, fn, base, NULL_TREE,
2613 mem_args, NULL_TREE,
2614 LOOKUP_NORMAL, DEDUCE_CALL);
2617 candidates = add_function_candidate
2618 (candidates, fn, base, mem_args, LOOKUP_NORMAL);
2621 candidates->basetype_path = TYPE_BINFO (type);
2625 convs = lookup_conversions (type);
2627 for (; convs; convs = TREE_CHAIN (convs))
2629 tree fns = TREE_VALUE (convs);
2630 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2632 if ((TREE_CODE (totype) == POINTER_TYPE
2633 || TREE_CODE (totype) == REFERENCE_TYPE)
2634 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2635 for (; fns; fns = OVL_NEXT (fns))
2637 tree fn = OVL_CURRENT (fns);
2638 if (TREE_CODE (fn) == TEMPLATE_DECL)
2640 candidates = add_template_conv_candidate (candidates,
2647 candidates = add_conv_candidate (candidates, fn, obj, args);
2651 if (! any_viable (candidates))
2653 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2654 print_z_candidates (candidates);
2655 return error_mark_node;
2658 candidates = splice_viable (candidates);
2659 cand = tourney (candidates);
2663 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2664 print_z_candidates (candidates);
2665 return error_mark_node;
2668 /* Since cand->fn will be a type, not a function, for a conversion
2669 function, we must be careful not to unconditionally look at
2671 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2672 && DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2673 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2675 obj = convert_like_with_context
2676 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2679 return build_function_call (obj, args);
2683 op_error (code, code2, arg1, arg2, arg3, problem)
2684 enum tree_code code, code2;
2685 tree arg1, arg2, arg3;
2686 const char *problem;
2689 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2694 cp_error ("%s for `%T ? %T : %T'", problem,
2695 error_type (arg1), error_type (arg2), error_type (arg3));
2697 case POSTINCREMENT_EXPR:
2698 case POSTDECREMENT_EXPR:
2699 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2702 cp_error ("%s for `%T[%T]'", problem,
2703 error_type (arg1), error_type (arg2));
2707 cp_error ("%s for `%T %s %T'", problem,
2708 error_type (arg1), opname, error_type (arg2));
2710 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2714 /* Return the implicit conversion sequence that could be used to
2715 convert E1 to E2 in [expr.cond]. */
2718 conditional_conversion (e1, e2)
2722 tree t1 = non_reference (TREE_TYPE (e1));
2723 tree t2 = non_reference (TREE_TYPE (e2));
2728 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2729 implicitly converted (clause _conv_) to the type "reference to
2730 T2", subject to the constraint that in the conversion the
2731 reference must bind directly (_dcl.init.ref_) to E1. */
2732 if (real_lvalue_p (e2))
2734 conv = implicit_conversion (build_reference_type (t2),
2737 LOOKUP_NO_TEMP_BIND);
2744 If E1 and E2 have class type, and the underlying class types are
2745 the same or one is a base class of the other: E1 can be converted
2746 to match E2 if the class of T2 is the same type as, or a base
2747 class of, the class of T1, and the cv-qualification of T2 is the
2748 same cv-qualification as, or a greater cv-qualification than, the
2749 cv-qualification of T1. If the conversion is applied, E1 is
2750 changed to an rvalue of type T2 that still refers to the original
2751 source class object (or the appropriate subobject thereof). */
2752 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2753 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2754 TYPE_MAIN_VARIANT (t1)))
2756 if (at_least_as_qualified_p (t2, t1))
2758 conv = build1 (IDENTITY_CONV, t1, e1);
2759 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2760 TYPE_MAIN_VARIANT (t2)))
2761 conv = build_conv (BASE_CONV, t2, conv);
2770 E1 can be converted to match E2 if E1 can be implicitly converted
2771 to the type that expression E2 would have if E2 were converted to
2772 an rvalue (or the type it has, if E2 is an rvalue). */
2773 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2776 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2777 arguments to the conditional expression. By the time this function
2778 is called, any suitable candidate functions are included in
2782 build_conditional_expr (arg1, arg2, arg3)
2790 tree result_type = NULL_TREE;
2792 struct z_candidate *candidates = 0;
2793 struct z_candidate *cand;
2795 /* As a G++ extension, the second argument to the conditional can be
2796 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2797 c'.) If the second operand is omitted, make sure it is
2798 calculated only once. */
2802 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2803 arg1 = arg2 = save_expr (arg1);
2808 The first expr ession is implicitly converted to bool (clause
2810 arg1 = cp_convert (boolean_type_node, arg1);
2812 /* If something has already gone wrong, just pass that fact up the
2814 if (arg1 == error_mark_node
2815 || arg2 == error_mark_node
2816 || arg3 == error_mark_node
2817 || TREE_TYPE (arg1) == error_mark_node
2818 || TREE_TYPE (arg2) == error_mark_node
2819 || TREE_TYPE (arg3) == error_mark_node)
2820 return error_mark_node;
2822 /* Convert from reference types to ordinary types; no expressions
2823 really have reference type in C++. */
2824 arg2 = convert_from_reference (arg2);
2825 arg3 = convert_from_reference (arg3);
2829 If either the second or the third operand has type (possibly
2830 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2831 array-to-pointer (_conv.array_), and function-to-pointer
2832 (_conv.func_) standard conversions are performed on the second
2833 and third operands. */
2834 arg2_type = TREE_TYPE (arg2);
2835 arg3_type = TREE_TYPE (arg3);
2836 if (same_type_p (TYPE_MAIN_VARIANT (arg2_type), void_type_node)
2837 || same_type_p (TYPE_MAIN_VARIANT (arg3_type), void_type_node))
2842 /* Do the conversions. We don't these for `void' type arguments
2843 since it can't have any effect and since decay_conversion
2844 does not handle that case gracefully. */
2845 if (!same_type_p (TYPE_MAIN_VARIANT (arg2_type), void_type_node))
2846 arg2 = decay_conversion (arg2);
2847 if (!same_type_p (TYPE_MAIN_VARIANT (arg3_type), void_type_node))
2848 arg3 = decay_conversion (arg3);
2849 arg2_type = TREE_TYPE (arg2);
2850 arg3_type = TREE_TYPE (arg3);
2852 arg2_void_p = same_type_p (TYPE_MAIN_VARIANT (arg2_type),
2854 arg3_void_p = same_type_p (TYPE_MAIN_VARIANT (arg3_type),
2859 One of the following shall hold:
2861 --The second or the third operand (but not both) is a
2862 throw-expression (_except.throw_); the result is of the
2863 type of the other and is an rvalue.
2865 --Both the second and the third operands have type void; the
2866 result is of type void and is an rvalue. */
2867 if ((TREE_CODE (arg2) == THROW_EXPR)
2868 ^ (TREE_CODE (arg3) == THROW_EXPR))
2869 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2870 ? arg3_type : arg2_type);
2871 else if (arg2_void_p && arg3_void_p)
2872 result_type = void_type_node;
2875 cp_error ("`%E' has type `void' and is not a throw-expression",
2876 arg2_void_p ? arg2 : arg3);
2877 return error_mark_node;
2881 goto valid_operands;
2885 Otherwise, if the second and third operand have different types,
2886 and either has (possibly cv-qualified) class type, an attempt is
2887 made to convert each of those operands to the type of the other. */
2888 else if (!same_type_p (arg2_type, arg3_type)
2889 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2891 tree conv2 = conditional_conversion (arg2, arg3);
2892 tree conv3 = conditional_conversion (arg3, arg2);
2896 If both can be converted, or one can be converted but the
2897 conversion is ambiguous, the program is ill-formed. If
2898 neither can be converted, the operands are left unchanged and
2899 further checking is performed as described below. If exactly
2900 one conversion is possible, that conversion is applied to the
2901 chosen operand and the converted operand is used in place of
2902 the original operand for the remainder of this section. */
2903 if ((conv2 && !ICS_BAD_FLAG (conv2)
2904 && conv3 && !ICS_BAD_FLAG (conv3))
2905 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
2906 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
2908 cp_error ("operands to ?: have different types");
2909 return error_mark_node;
2911 else if (conv2 && !ICS_BAD_FLAG (conv2))
2913 arg2 = convert_like (conv2, arg2);
2914 arg2 = convert_from_reference (arg2);
2915 /* That may not quite have done the trick. If the two types
2916 are cv-qualified variants of one another, we will have
2917 just used an IDENTITY_CONV. (There's no conversion from
2918 an lvalue of one class type to an lvalue of another type,
2919 even a cv-qualified variant, and we don't want to lose
2920 lvalue-ness here.) So, we manually add a NOP_EXPR here
2922 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
2923 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
2924 arg2_type = TREE_TYPE (arg2);
2926 else if (conv3 && !ICS_BAD_FLAG (conv3))
2928 arg3 = convert_like (conv3, arg3);
2929 arg3 = convert_from_reference (arg3);
2930 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
2931 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
2932 arg3_type = TREE_TYPE (arg3);
2938 If the second and third operands are lvalues and have the same
2939 type, the result is of that type and is an lvalue. */
2940 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
2941 same_type_p (arg2_type, arg3_type))
2943 result_type = arg2_type;
2944 goto valid_operands;
2949 Otherwise, the result is an rvalue. If the second and third
2950 operand do not have the same type, and either has (possibly
2951 cv-qualified) class type, overload resolution is used to
2952 determine the conversions (if any) to be applied to the operands
2953 (_over.match.oper_, _over.built_). */
2955 if (!same_type_p (arg2_type, arg3_type)
2956 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2961 /* Rearrange the arguments so that add_builtin_candidate only has
2962 to know about two args. In build_builtin_candidates, the
2963 arguments are unscrambled. */
2967 candidates = add_builtin_candidates (candidates,
2970 ansi_opname[COND_EXPR],
2976 If the overload resolution fails, the program is
2978 if (!any_viable (candidates))
2980 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
2981 print_z_candidates (candidates);
2982 return error_mark_node;
2984 candidates = splice_viable (candidates);
2985 cand = tourney (candidates);
2988 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
2989 print_z_candidates (candidates);
2990 return error_mark_node;
2995 Otherwise, the conversions thus determined are applied, and
2996 the converted operands are used in place of the original
2997 operands for the remainder of this section. */
2998 conv = TREE_VEC_ELT (cand->convs, 0);
2999 arg1 = convert_like (conv, arg1);
3000 conv = TREE_VEC_ELT (cand->convs, 1);
3001 arg2 = convert_like (conv, arg2);
3002 conv = TREE_VEC_ELT (cand->convs, 2);
3003 arg3 = convert_like (conv, arg3);
3008 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3009 and function-to-pointer (_conv.func_) standard conversions are
3010 performed on the second and third operands.
3012 We need to force the lvalue-to-rvalue conversion here for class types,
3013 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3014 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3017 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3018 arg2 = build_user_type_conversion (TREE_TYPE (arg2), arg2, LOOKUP_NORMAL);
3020 arg2 = decay_conversion (arg2);
3021 arg2_type = TREE_TYPE (arg2);
3023 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3024 arg3 = build_user_type_conversion (TREE_TYPE (arg3), arg3, LOOKUP_NORMAL);
3026 arg3 = decay_conversion (arg3);
3027 arg3_type = TREE_TYPE (arg3);
3031 After those conversions, one of the following shall hold:
3033 --The second and third operands have the same type; the result is of
3035 if (same_type_p (arg2_type, arg3_type))
3036 result_type = arg2_type;
3039 --The second and third operands have arithmetic or enumeration
3040 type; the usual arithmetic conversions are performed to bring
3041 them to a common type, and the result is of that type. */
3042 else if ((ARITHMETIC_TYPE_P (arg2_type)
3043 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3044 && (ARITHMETIC_TYPE_P (arg3_type)
3045 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3047 /* In this case, there is always a common type. */
3048 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3051 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3052 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3053 cp_warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3054 arg2_type, arg3_type);
3055 else if (extra_warnings
3056 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3057 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3058 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3059 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3060 cp_warning ("enumeral and non-enumeral type in conditional expression");
3062 arg2 = perform_implicit_conversion (result_type, arg2);
3063 arg3 = perform_implicit_conversion (result_type, arg3);
3067 --The second and third operands have pointer type, or one has
3068 pointer type and the other is a null pointer constant; pointer
3069 conversions (_conv.ptr_) and qualification conversions
3070 (_conv.qual_) are performed to bring them to their composite
3071 pointer type (_expr.rel_). The result is of the composite
3074 --The second and third operands have pointer to member type, or
3075 one has pointer to member type and the other is a null pointer
3076 constant; pointer to member conversions (_conv.mem_) and
3077 qualification conversions (_conv.qual_) are performed to bring
3078 them to a common type, whose cv-qualification shall match the
3079 cv-qualification of either the second or the third operand.
3080 The result is of the common type. */
3081 else if ((null_ptr_cst_p (arg2)
3082 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3083 || TYPE_PTRMEMFUNC_P (arg3_type)))
3084 || (null_ptr_cst_p (arg3)
3085 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3086 || TYPE_PTRMEMFUNC_P (arg2_type)))
3087 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3088 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3089 || (TYPE_PTRMEMFUNC_P (arg2_type)
3090 && TYPE_PTRMEMFUNC_P (arg3_type)))
3092 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3093 arg3, "conditional expression");
3094 arg2 = perform_implicit_conversion (result_type, arg2);
3095 arg3 = perform_implicit_conversion (result_type, arg3);
3100 cp_error ("operands to ?: have different types");
3101 return error_mark_node;
3105 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3106 /* Expand both sides into the same slot, hopefully the target of the
3107 ?: expression. We used to check for TARGET_EXPRs here, but now we
3108 sometimes wrap them in NOP_EXPRs so the test would fail. */
3109 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3110 result = build_target_expr_with_type (result, result_type);
3112 /* If this expression is an rvalue, but might be mistaken for an
3113 lvalue, we must add a NON_LVALUE_EXPR. */
3114 if (!lvalue_p && real_lvalue_p (result))
3115 result = build1 (NON_LVALUE_EXPR, result_type, result);
3121 build_new_op (code, flags, arg1, arg2, arg3)
3122 enum tree_code code;
3124 tree arg1, arg2, arg3;
3126 struct z_candidate *candidates = 0, *cand;
3127 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3128 enum tree_code code2 = NOP_EXPR;
3129 tree templates = NULL_TREE;
3132 if (arg1 == error_mark_node
3133 || arg2 == error_mark_node
3134 || arg3 == error_mark_node)
3135 return error_mark_node;
3137 /* This can happen if a template takes all non-type parameters, e.g.
3138 undeclared_template<1, 5, 72>a; */
3139 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3141 cp_error ("`%D' must be declared before use", arg1);
3142 return error_mark_node;
3145 if (code == MODIFY_EXPR)
3147 code2 = TREE_CODE (arg3);
3149 fnname = ansi_assopname[code2];
3152 fnname = ansi_opname[code];
3158 case VEC_DELETE_EXPR:
3160 /* Use build_op_new_call and build_op_delete_call instead. */
3161 my_friendly_abort (981018);
3164 return build_object_call (arg1, arg2);
3170 /* The comma operator can have void args. */
3171 if (TREE_CODE (arg1) == OFFSET_REF)
3172 arg1 = resolve_offset_ref (arg1);
3173 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
3174 arg2 = resolve_offset_ref (arg2);
3175 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
3176 arg3 = resolve_offset_ref (arg3);
3178 if (code == COND_EXPR)
3180 if (arg2 == NULL_TREE
3181 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3182 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3183 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3184 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3187 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3188 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3191 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3192 arg2 = integer_zero_node;
3195 arglist = tree_cons (NULL_TREE, arg1, tree_cons
3196 (NULL_TREE, arg2, build_tree_list (NULL_TREE, arg3)));
3198 arglist = tree_cons (NULL_TREE, arg1, build_tree_list (NULL_TREE, arg2));
3200 arglist = build_tree_list (NULL_TREE, arg1);
3202 fns = lookup_function_nonclass (fnname, arglist);
3204 if (fns && TREE_CODE (fns) == TREE_LIST)
3205 fns = TREE_VALUE (fns);
3206 for (; fns; fns = OVL_NEXT (fns))
3208 tree fn = OVL_CURRENT (fns);
3209 if (TREE_CODE (fn) == TEMPLATE_DECL)
3211 templates = tree_cons (NULL_TREE, fn, templates);
3213 = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3214 arglist, TREE_TYPE (fnname),
3215 flags, DEDUCE_CALL);
3218 candidates = add_function_candidate (candidates, fn, NULL_TREE,
3222 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3224 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3225 if (fns == error_mark_node)
3233 tree basetype = BINFO_TYPE (TREE_PURPOSE (fns));
3234 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3235 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3237 tree fn = OVL_CURRENT (fns);
3240 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3241 this_arglist = mem_arglist;
3243 this_arglist = arglist;
3245 if (TREE_CODE (fn) == TEMPLATE_DECL)
3247 /* A member template. */
3248 templates = tree_cons (NULL_TREE, fn, templates);
3250 = add_template_candidate (candidates, fn, basetype, NULL_TREE,
3251 this_arglist, TREE_TYPE (fnname),
3252 flags, DEDUCE_CALL);
3255 candidates = add_function_candidate
3256 (candidates, fn, basetype, this_arglist, flags);
3259 candidates->basetype_path = TYPE_BINFO (TREE_TYPE (arg1));
3266 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3267 to know about two args; a builtin candidate will always have a first
3268 parameter of type bool. We'll handle that in
3269 build_builtin_candidate. */
3270 if (code == COND_EXPR)
3280 args[2] = NULL_TREE;
3283 candidates = add_builtin_candidates
3284 (candidates, code, code2, fnname, args, flags);
3287 if (! any_viable (candidates))
3291 case POSTINCREMENT_EXPR:
3292 case POSTDECREMENT_EXPR:
3293 /* Look for an `operator++ (int)'. If they didn't have
3294 one, then we fall back to the old way of doing things. */
3295 if (flags & LOOKUP_COMPLAIN)
3296 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
3297 fnname, opname_tab [code]);
3298 if (code == POSTINCREMENT_EXPR)
3299 code = PREINCREMENT_EXPR;
3301 code = PREDECREMENT_EXPR;
3302 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3304 /* The caller will deal with these. */
3313 if (flags & LOOKUP_COMPLAIN)
3315 op_error (code, code2, arg1, arg2, arg3, "no match");
3316 print_z_candidates (candidates);
3318 return error_mark_node;
3320 candidates = splice_viable (candidates);
3321 cand = tourney (candidates);
3325 if (flags & LOOKUP_COMPLAIN)
3327 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3328 print_z_candidates (candidates);
3330 return error_mark_node;
3333 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3335 extern int warn_synth;
3337 && fnname == ansi_opname[MODIFY_EXPR]
3338 && DECL_ARTIFICIAL (cand->fn)
3340 && ! candidates->next->next)
3342 cp_warning ("using synthesized `%#D' for copy assignment",
3344 cp_warning_at (" where cfront would use `%#D'",
3346 ? candidates->next->fn
3350 /* Pedantically, normal function declarations are never considered
3351 to refer to template instantiations, so we only do this with
3353 if (flag_guiding_decls && templates && ! cand->template
3354 && ! DECL_INITIAL (cand->fn)
3355 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
3356 add_maybe_template (cand->fn, templates);
3358 return build_over_call
3360 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3361 ? mem_arglist : arglist,
3365 /* Check for comparison of different enum types. */
3374 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3375 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3376 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3377 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3379 cp_warning ("comparison between `%#T' and `%#T'",
3380 TREE_TYPE (arg1), TREE_TYPE (arg2));
3387 /* We need to strip any leading REF_BIND so that bitfields don't cause
3388 errors. This should not remove any important conversions, because
3389 builtins don't apply to class objects directly. */
3390 conv = TREE_VEC_ELT (cand->convs, 0);
3391 if (TREE_CODE (conv) == REF_BIND)
3392 conv = TREE_OPERAND (conv, 0);
3393 arg1 = convert_like (conv, arg1);
3396 conv = TREE_VEC_ELT (cand->convs, 1);
3397 if (TREE_CODE (conv) == REF_BIND)
3398 conv = TREE_OPERAND (conv, 0);
3399 arg2 = convert_like (conv, arg2);
3403 conv = TREE_VEC_ELT (cand->convs, 2);
3404 if (TREE_CODE (conv) == REF_BIND)
3405 conv = TREE_OPERAND (conv, 0);
3406 arg3 = convert_like (conv, arg3);
3413 return build_modify_expr (arg1, code2, arg2);
3416 return build_indirect_ref (arg1, "unary *");
3421 case TRUNC_DIV_EXPR:
3432 case TRUNC_MOD_EXPR:
3436 case TRUTH_ANDIF_EXPR:
3437 case TRUTH_ORIF_EXPR:
3438 return build_binary_op (code, arg1, arg2);
3443 case TRUTH_NOT_EXPR:
3444 case PREINCREMENT_EXPR:
3445 case POSTINCREMENT_EXPR:
3446 case PREDECREMENT_EXPR:
3447 case POSTDECREMENT_EXPR:
3450 return build_unary_op (code, arg1, candidates != 0);
3453 return build_array_ref (arg1, arg2);
3456 return build_conditional_expr (arg1, arg2, arg3);
3459 return build_m_component_ref
3460 (build_indirect_ref (arg1, NULL_PTR), arg2);
3462 /* The caller will deal with these. */
3469 my_friendly_abort (367);
3474 /* Build a call to operator delete. This has to be handled very specially,
3475 because the restrictions on what signatures match are different from all
3476 other call instances. For a normal delete, only a delete taking (void *)
3477 or (void *, size_t) is accepted. For a placement delete, only an exact
3478 match with the placement new is accepted.
3480 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3481 ADDR is the pointer to be deleted. For placement delete, it is also
3482 used to determine what the corresponding new looked like.
3483 SIZE is the size of the memory block to be deleted.
3484 FLAGS are the usual overloading flags.
3485 PLACEMENT is the corresponding placement new call, or 0. */
3488 build_op_delete_call (code, addr, size, flags, placement)
3489 enum tree_code code;
3490 tree addr, size, placement;
3493 tree fn, fns, fnname, fntype, argtypes, args, type;
3495 if (addr == error_mark_node)
3496 return error_mark_node;
3498 type = TREE_TYPE (TREE_TYPE (addr));
3499 fnname = ansi_opname[code];
3501 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3504 If the result of the lookup is ambiguous or inaccessible, or if
3505 the lookup selects a placement deallocation function, the
3506 program is ill-formed.
3508 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3510 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3511 if (fns == error_mark_node)
3512 return error_mark_node;
3517 if (fns == NULL_TREE)
3518 fns = lookup_name_nonclass (fnname);
3522 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3524 /* Extract the function. */
3525 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3526 /* Then the second parm type. */
3527 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3529 /* Also the second argument. */
3530 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3534 /* First try it without the size argument. */
3535 argtypes = void_list_node;
3539 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3540 fntype = build_function_type (void_type_node, argtypes);
3542 /* Strip const and volatile from addr. */
3543 if (type != TYPE_MAIN_VARIANT (type))
3544 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
3546 fn = instantiate_type (fntype, fns, 2);
3548 if (fn != error_mark_node)
3550 if (TREE_CODE (fns) == TREE_LIST)
3551 /* Member functions. */
3552 enforce_access (type, fn);
3553 return build_function_call (fn, tree_cons (NULL_TREE, addr, args));
3556 /* If we are doing placement delete we do nothing if we don't find a
3557 matching op delete. */
3561 /* Normal delete; now try to find a match including the size argument. */
3562 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3563 tree_cons (NULL_TREE, sizetype, void_list_node));
3564 fntype = build_function_type (void_type_node, argtypes);
3566 fn = instantiate_type (fntype, fns, 2);
3568 if (fn != error_mark_node)
3570 if (BASELINK_P (fns))
3571 /* Member functions. */
3572 enforce_access (type, fn);
3573 return build_function_call
3574 (fn, tree_cons (NULL_TREE, addr,
3575 build_tree_list (NULL_TREE, size)));
3578 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3579 destructor, in which case the error should be deferred
3580 until someone actually tries to delete one of these. */
3581 if (flags & LOOKUP_SPECULATIVELY)
3584 cp_error ("no suitable `operator delete' for `%T'", type);
3585 return error_mark_node;
3588 /* If the current scope isn't allowed to access DECL along
3589 BASETYPE_PATH, give an error. The most derived class in
3590 BASETYPE_PATH is the one used to qualify DECL. */
3593 enforce_access (basetype_path, decl)
3599 accessible = accessible_p (basetype_path, decl);
3602 if (TREE_PRIVATE (decl))
3603 cp_error_at ("`%+#D' is private", decl);
3604 else if (TREE_PROTECTED (decl))
3605 cp_error_at ("`%+#D' is protected", decl);
3607 cp_error_at ("`%+#D' is inaccessible", decl);
3608 cp_error ("within this context");
3615 /* Perform the conversions in CONVS on the expression EXPR.
3616 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3617 indicates the `this' argument of a method. INNER is non-zero when
3618 being called to continue a conversion chain. */
3621 convert_like_real (convs, expr, fn, argnum, inner)
3627 if (ICS_BAD_FLAG (convs)
3628 && TREE_CODE (convs) != USER_CONV
3629 && TREE_CODE (convs) != AMBIG_CONV
3630 && TREE_CODE (convs) != REF_BIND)
3633 for (; t; t = TREE_OPERAND (t, 0))
3635 if (TREE_CODE (t) == USER_CONV)
3637 expr = convert_like_real (t, expr, fn, argnum, 1);
3640 else if (TREE_CODE (t) == AMBIG_CONV)
3641 return convert_like_real (t, expr, fn, argnum, 1);
3642 else if (TREE_CODE (t) == IDENTITY_CONV)
3645 return convert_for_initialization
3646 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3647 "conversion", fn, argnum);
3651 expr = dubious_conversion_warnings
3652 (TREE_TYPE (convs), expr, "argument", fn, argnum);
3653 switch (TREE_CODE (convs))
3657 struct z_candidate *cand
3658 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3662 if (DECL_CONSTRUCTOR_P (fn))
3664 tree t = build_int_2 (0, 0);
3665 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3667 args = build_tree_list (NULL_TREE, expr);
3668 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3669 args = tree_cons (NULL_TREE, integer_one_node, args);
3670 args = tree_cons (NULL_TREE, t, args);
3673 args = build_this (expr);
3674 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3676 /* If this is a constructor or a function returning an aggr type,
3677 we need to build up a TARGET_EXPR. */
3678 if (DECL_CONSTRUCTOR_P (fn))
3679 expr = build_cplus_new (TREE_TYPE (convs), expr);
3684 if (type_unknown_p (expr))
3685 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3688 /* Call build_user_type_conversion again for the error. */
3689 return build_user_type_conversion
3690 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3696 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum, 1);
3697 if (expr == error_mark_node)
3698 return error_mark_node;
3700 /* Convert a non-array constant variable to its underlying value, unless we
3701 are about to bind it to a reference, in which case we need to
3702 leave it as an lvalue. */
3703 if (TREE_READONLY_DECL_P (expr) && TREE_CODE (convs) != REF_BIND
3704 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3705 expr = decl_constant_value (expr);
3707 switch (TREE_CODE (convs))
3710 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3712 /* else fall through */
3714 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3716 /* We are going to bind a reference directly to a base-class
3717 subobject of EXPR. */
3718 tree base_ptr = build_pointer_type (TREE_TYPE (convs));
3720 /* Build an expression for `*((base*) &expr)'. */
3721 expr = build_unary_op (ADDR_EXPR, expr, 0);
3722 expr = perform_implicit_conversion (base_ptr, expr);
3723 expr = build_indirect_ref (expr, "implicit conversion");
3728 tree cvt_expr = build_user_type_conversion
3729 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3732 /* This can occur if, for example, the EXPR has incomplete
3733 type. We can't check for that before attempting the
3734 conversion because the type might be an incomplete
3735 array type, which is OK if some constructor for the
3736 destination type takes a pointer argument. */
3737 if (!COMPLETE_TYPE_P (TREE_TYPE (expr)))
3739 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
3740 incomplete_type_error (expr, TREE_TYPE (expr));
3742 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3743 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3746 cp_error ("could not convert `%E' to `%T'",
3747 expr, TREE_TYPE (convs));
3748 return error_mark_node;
3755 tree ref_type = TREE_TYPE (convs);
3757 /* If necessary, create a temporary. */
3758 if (NEED_TEMPORARY_P (convs))
3760 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3761 expr = build_target_expr_with_type (expr, type);
3764 /* Take the address of the thing to which we will bind the
3766 expr = build_unary_op (ADDR_EXPR, expr, 1);
3767 if (expr == error_mark_node)
3768 return error_mark_node;
3770 /* Convert it to a pointer to the type referred to by the
3771 reference. This will adjust the pointer if a derived to
3772 base conversion is being performed. */
3773 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3775 /* Convert the pointer to the desired reference type. */
3776 expr = build1 (NOP_EXPR, ref_type, expr);
3782 return decay_conversion (expr);
3785 /* Warn about deprecated conversion if appropriate. */
3786 string_conv_p (TREE_TYPE (convs), expr, 1);
3792 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3793 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3796 /* ARG is being passed to a varargs function. Perform any conversions
3797 required. Array/function to pointer decay must have already happened.
3798 Return the converted value. */
3801 convert_arg_to_ellipsis (arg)
3804 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3805 && (TYPE_PRECISION (TREE_TYPE (arg))
3806 < TYPE_PRECISION (double_type_node)))
3807 /* Convert `float' to `double'. */
3808 arg = cp_convert (double_type_node, arg);
3810 /* Convert `short' and `char' to full-size `int'. */
3811 arg = default_conversion (arg);
3813 arg = require_complete_type (arg);
3815 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
3817 /* Undefined behaviour [expr.call] 5.2.2/7. */
3818 cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3825 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
3828 build_x_va_arg (expr, type)
3832 type = complete_type_or_else (type, NULL_TREE);
3834 if (expr == error_mark_node || !type)
3835 return error_mark_node;
3837 if (! pod_type_p (type))
3839 /* Undefined behaviour [expr.call] 5.2.2/7. */
3840 cp_warning ("cannot receive objects of non-POD type `%#T' through `...'",
3844 return build_va_arg (expr, type);
3847 /* TYPE has been given to va_arg. Apply the default conversions which would
3848 have happened when passed via ellipsis. Return the promoted type, or
3849 NULL_TREE, if there is no change. */
3852 convert_type_from_ellipsis (type)
3857 if (TREE_CODE (type) == ARRAY_TYPE)
3858 promote = build_pointer_type (TREE_TYPE (type));
3859 else if (TREE_CODE (type) == FUNCTION_TYPE)
3860 promote = build_pointer_type (type);
3862 promote = type_promotes_to (type);
3864 return same_type_p (type, promote) ? NULL_TREE : promote;
3867 /* ARG is a default argument expression being passed to a parameter of
3868 the indicated TYPE, which is a parameter to FN. Do any required
3869 conversions. Return the converted value. */
3872 convert_default_arg (type, arg, fn, parmnum)
3878 if (fn && DECL_TEMPLATE_INFO (fn))
3879 arg = tsubst_default_argument (fn, type, arg);
3881 arg = break_out_target_exprs (arg);
3883 if (TREE_CODE (arg) == CONSTRUCTOR)
3885 arg = digest_init (type, arg, 0);
3886 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3887 "default argument", fn, parmnum);
3891 /* This could get clobbered by the following call. */
3892 if (TREE_HAS_CONSTRUCTOR (arg))
3893 arg = copy_node (arg);
3895 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3896 "default argument", fn, parmnum);
3897 if (PROMOTE_PROTOTYPES
3898 && (TREE_CODE (type) == INTEGER_TYPE
3899 || TREE_CODE (type) == ENUMERAL_TYPE)
3900 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3901 arg = default_conversion (arg);
3908 build_over_call (cand, args, flags)
3909 struct z_candidate *cand;
3914 tree convs = cand->convs;
3915 tree converted_args = NULL_TREE;
3916 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3917 tree conv, arg, val;
3921 /* Give any warnings we noticed during overload resolution. */
3923 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3924 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3926 if (DECL_FUNCTION_MEMBER_P (fn))
3927 enforce_access (cand->basetype_path, fn);
3929 if (args && TREE_CODE (args) != TREE_LIST)
3930 args = build_tree_list (NULL_TREE, args);
3933 /* The implicit parameters to a constructor are not considered by overload
3934 resolution, and must be of the proper type. */
3935 if (DECL_CONSTRUCTOR_P (fn))
3937 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3938 arg = TREE_CHAIN (arg);
3939 parm = TREE_CHAIN (parm);
3940 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3942 converted_args = tree_cons
3943 (NULL_TREE, TREE_VALUE (arg), converted_args);
3944 arg = TREE_CHAIN (arg);
3945 parm = TREE_CHAIN (parm);
3948 /* Bypass access control for 'this' parameter. */
3949 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3951 tree parmtype = TREE_VALUE (parm);
3952 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3954 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3955 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3956 TREE_TYPE (argtype), fn);
3958 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3959 X is called for an object that is not of type X, or of a type
3960 derived from X, the behavior is undefined.
3962 So we can assume that anything passed as 'this' is non-null, and
3963 optimize accordingly. */
3964 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
3965 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3966 converted_args = tree_cons (NULL_TREE, t, converted_args);
3967 parm = TREE_CHAIN (parm);
3968 arg = TREE_CHAIN (arg);
3974 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3976 tree type = TREE_VALUE (parm);
3978 conv = TREE_VEC_ELT (convs, i);
3979 if (ICS_BAD_FLAG (conv))
3982 val = TREE_VALUE (arg);
3984 for (; t; t = TREE_OPERAND (t, 0))
3986 if (TREE_CODE (t) == USER_CONV
3987 || TREE_CODE (t) == AMBIG_CONV)
3989 val = convert_like_with_context (t, val, fn, i - is_method);
3992 else if (TREE_CODE (t) == IDENTITY_CONV)
3995 val = convert_for_initialization
3996 (NULL_TREE, type, val, LOOKUP_NORMAL,
3997 "argument", fn, i - is_method);
4001 val = TREE_VALUE (arg);
4002 val = convert_like_with_context
4003 (conv, TREE_VALUE (arg), fn, i - is_method);
4006 if (PROMOTE_PROTOTYPES
4007 && (TREE_CODE (type) == INTEGER_TYPE
4008 || TREE_CODE (type) == ENUMERAL_TYPE)
4009 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4010 val = default_conversion (val);
4011 converted_args = tree_cons (NULL_TREE, val, converted_args);
4014 /* Default arguments */
4015 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4017 = tree_cons (NULL_TREE,
4018 convert_default_arg (TREE_VALUE (parm),
4019 TREE_PURPOSE (parm),
4024 for (; arg; arg = TREE_CHAIN (arg))
4026 = tree_cons (NULL_TREE,
4027 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4030 converted_args = nreverse (converted_args);
4032 if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
4033 check_function_format (DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
4036 /* Avoid actually calling copy constructors and copy assignment operators,
4039 if (! flag_elide_constructors)
4040 /* Do things the hard way. */;
4041 else if (TREE_VEC_LENGTH (convs) == 1
4042 && DECL_COPY_CONSTRUCTOR_P (fn))
4045 arg = TREE_CHAIN (converted_args);
4046 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4047 arg = TREE_CHAIN (arg);
4048 arg = TREE_VALUE (arg);
4050 /* Pull out the real argument, disregarding const-correctness. */
4052 while (TREE_CODE (targ) == NOP_EXPR
4053 || TREE_CODE (targ) == NON_LVALUE_EXPR
4054 || TREE_CODE (targ) == CONVERT_EXPR)
4055 targ = TREE_OPERAND (targ, 0);
4056 if (TREE_CODE (targ) == ADDR_EXPR)
4058 targ = TREE_OPERAND (targ, 0);
4059 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
4060 TYPE_MAIN_VARIANT (TREE_TYPE (targ))))
4069 arg = build_indirect_ref (arg, 0);
4071 /* [class.copy]: the copy constructor is implicitly defined even if
4072 the implementation elided its use. */
4073 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4076 /* If we're creating a temp and we already have one, don't create a
4077 new one. If we're not creating a temp but we get one, use
4078 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4079 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4080 temp or an INIT_EXPR otherwise. */
4081 if (integer_zerop (TREE_VALUE (args)))
4083 if (! real_lvalue_p (arg))
4085 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4086 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4088 else if (! real_lvalue_p (arg)
4089 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4092 tree to = stabilize_reference
4093 (build_indirect_ref (TREE_VALUE (args), 0));
4095 /* If we're initializing an empty class, then we actually
4096 have to use a MODIFY_EXPR rather than an INIT_EXPR. The
4097 reason is that the dummy padding member in the target may
4098 not actually be allocated if TO is a base class
4099 subobject. Since we've set TYPE_NONCOPIED_PARTS on the
4100 padding, a MODIFY_EXPR will preserve its value, which is
4101 the right thing to do if it's not really padding at all.
4103 It's not safe to just throw away the ARG if we're looking
4104 at an empty class because the ARG might contain a
4105 TARGET_EXPR which wants to be bound to TO. If it is not,
4106 expand_expr will assign a dummy slot for the TARGET_EXPR,
4107 and we will call a destructor for it, which is wrong,
4108 because we will also destroy TO, but will never have
4110 val = build (is_empty_class (DECL_CONTEXT (fn))
4111 ? MODIFY_EXPR : INIT_EXPR,
4112 DECL_CONTEXT (fn), to, arg);
4113 address = build_unary_op (ADDR_EXPR, val, 0);
4114 /* Avoid a warning about this expression, if the address is
4116 TREE_USED (address) = 1;
4120 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
4122 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4124 tree to = stabilize_reference
4125 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4127 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4129 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4135 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4137 tree t, *p = &TREE_VALUE (converted_args);
4138 tree binfo = get_binfo
4139 (DECL_VIRTUAL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
4140 *p = convert_pointer_to_real (binfo, *p);
4141 if (TREE_SIDE_EFFECTS (*p))
4142 *p = save_expr (*p);
4143 t = build_pointer_type (TREE_TYPE (fn));
4144 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4147 else if (DECL_INLINE (fn))
4148 fn = inline_conversion (fn);
4150 fn = build_addr_func (fn);
4152 /* Recognize certain built-in functions so we can make tree-codes
4153 other than CALL_EXPR. We do this when it enables fold-const.c
4154 to do something useful. */
4156 if (TREE_CODE (fn) == ADDR_EXPR
4157 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4158 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4161 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4166 fn = build_call (fn, converted_args);
4167 if (TREE_CODE (TREE_TYPE (fn)) == VOID_TYPE)
4169 fn = require_complete_type (fn);
4170 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4171 fn = build_cplus_new (TREE_TYPE (fn), fn);
4172 return convert_from_reference (fn);
4175 /* Returns the value to use for the in-charge parameter when making a
4176 call to a function with the indicated NAME. */
4179 in_charge_arg_for_name (name)
4182 if (name == base_ctor_identifier
4183 || name == base_dtor_identifier)
4184 return integer_zero_node;
4185 else if (name == complete_ctor_identifier)
4186 return integer_one_node;
4187 else if (name == complete_dtor_identifier)
4188 return integer_two_node;
4189 else if (name == deleting_dtor_identifier)
4190 return integer_three_node;
4192 /* This function should only be called with one of the names listed
4194 my_friendly_abort (20000411);
4199 build_new_method_call (instance, name, args, basetype_path, flags)
4200 tree instance, name, args, basetype_path;
4203 struct z_candidate *candidates = 0, *cand;
4204 tree explicit_targs = NULL_TREE;
4205 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4207 tree user_args = args;
4208 tree templates = NULL_TREE;
4209 int template_only = 0;
4211 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4213 explicit_targs = TREE_OPERAND (name, 1);
4214 name = TREE_OPERAND (name, 0);
4216 name = DECL_NAME (name);
4219 if (TREE_CODE (name) == COMPONENT_REF)
4220 name = TREE_OPERAND (name, 1);
4221 if (TREE_CODE (name) == OVERLOAD)
4222 name = DECL_NAME (OVL_CURRENT (name));
4229 args = resolve_args (args);
4231 if (args == error_mark_node)
4232 return error_mark_node;
4234 if (instance == NULL_TREE)
4235 basetype = BINFO_TYPE (basetype_path);
4238 if (TREE_CODE (instance) == OFFSET_REF)
4239 instance = resolve_offset_ref (instance);
4240 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4241 instance = convert_from_reference (instance);
4242 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4244 /* XXX this should be handled before we get here. */
4245 if (! IS_AGGR_TYPE (basetype))
4247 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4248 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4249 name, instance, basetype);
4251 return error_mark_node;
4255 if (basetype_path == NULL_TREE)
4256 basetype_path = TYPE_BINFO (basetype);
4260 instance_ptr = build_this (instance);
4262 if (! template_only)
4264 /* XXX this should be handled before we get here. */
4265 fns = build_field_call (basetype_path, instance_ptr, name, args);
4272 instance_ptr = build_int_2 (0, 0);
4273 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4276 /* Callers should explicitly indicate whether they want to construct
4277 the complete object or just the part without virtual bases. */
4278 my_friendly_assert (name != ctor_identifier, 20000408);
4279 /* Similarly for destructors. */
4280 my_friendly_assert (name != dtor_identifier, 20000408);
4282 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4286 constructor_p = (name == complete_ctor_identifier
4287 || name == base_ctor_identifier);
4288 pretty_name = (constructor_p
4289 ? constructor_name (basetype) : dtor_identifier);
4293 /* Add the in-charge parameter as an implicit first argument. */
4295 || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4296 args = tree_cons (NULL_TREE,
4297 in_charge_arg_for_name (name),
4300 /* We want to call the normal constructor function under the
4302 name = constructor_p ? ctor_identifier : dtor_identifier;
4308 fns = lookup_fnfields (basetype_path, name, 1);
4310 if (fns == error_mark_node)
4311 return error_mark_node;
4314 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
4315 tree fn = TREE_VALUE (fns);
4316 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4317 for (; fn; fn = OVL_NEXT (fn))
4319 tree t = OVL_CURRENT (fn);
4322 /* We can end up here for copy-init of same or base class. */
4323 if ((flags & LOOKUP_ONLYCONVERTING)
4324 && DECL_NONCONVERTING_P (t))
4326 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
4327 this_arglist = mem_args;
4329 this_arglist = args;
4331 if (TREE_CODE (t) == TEMPLATE_DECL)
4333 /* A member template. */
4334 templates = tree_cons (NULL_TREE, t, templates);
4336 add_template_candidate (candidates, t, base, explicit_targs,
4338 TREE_TYPE (name), flags, DEDUCE_CALL);
4340 else if (! template_only)
4341 candidates = add_function_candidate (candidates, t, base,
4342 this_arglist, flags);
4345 candidates->basetype_path = basetype_path;
4349 if (! any_viable (candidates))
4351 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4352 if (flags & LOOKUP_SPECULATIVELY)
4354 if (!COMPLETE_TYPE_P (basetype))
4355 incomplete_type_error (instance_ptr, basetype);
4357 cp_error ("no matching function for call to `%T::%D (%A)%V'",
4358 basetype, pretty_name, user_args,
4359 TREE_TYPE (TREE_TYPE (instance_ptr)));
4360 print_z_candidates (candidates);
4361 return error_mark_node;
4363 candidates = splice_viable (candidates);
4364 cand = tourney (candidates);
4368 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4370 print_z_candidates (candidates);
4371 return error_mark_node;
4374 if (DECL_PURE_VIRTUAL_P (cand->fn)
4375 && instance == current_class_ref
4376 && (DECL_CONSTRUCTOR_P (current_function_decl)
4377 || DECL_DESTRUCTOR_P (current_function_decl))
4378 && ! (flags & LOOKUP_NONVIRTUAL)
4379 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4380 cp_error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4381 "abstract virtual `%#D' called from constructor"
4382 : "abstract virtual `%#D' called from destructor"),
4384 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4385 && is_dummy_object (instance_ptr))
4387 cp_error ("cannot call member function `%D' without object", cand->fn);
4388 return error_mark_node;
4391 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4392 && ((instance == current_class_ref && (dtor_label || ctor_label))
4393 || resolves_to_fixed_type_p (instance, 0)))
4394 flags |= LOOKUP_NONVIRTUAL;
4396 /* Pedantically, normal function declarations are never considered
4397 to refer to template instantiations, so we only do this with
4399 if (flag_guiding_decls && templates && ! cand->template
4400 && ! DECL_INITIAL (cand->fn))
4401 add_maybe_template (cand->fn, templates);
4403 return build_over_call
4405 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
4409 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4410 subsequence of ICS2. */
4413 is_subseq (ics1, ics2)
4416 /* We can assume that a conversion of the same code
4417 between the same types indicates a subsequence since we only get
4418 here if the types we are converting from are the same. */
4420 while (TREE_CODE (ics1) == RVALUE_CONV
4421 || TREE_CODE (ics1) == LVALUE_CONV)
4422 ics1 = TREE_OPERAND (ics1, 0);
4426 while (TREE_CODE (ics2) == RVALUE_CONV
4427 || TREE_CODE (ics2) == LVALUE_CONV)
4428 ics2 = TREE_OPERAND (ics2, 0);
4430 if (TREE_CODE (ics2) == USER_CONV
4431 || TREE_CODE (ics2) == AMBIG_CONV
4432 || TREE_CODE (ics2) == IDENTITY_CONV)
4433 /* At this point, ICS1 cannot be a proper subsequence of
4434 ICS2. We can get a USER_CONV when we are comparing the
4435 second standard conversion sequence of two user conversion
4439 ics2 = TREE_OPERAND (ics2, 0);
4441 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4442 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4443 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4444 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4449 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4450 be any _TYPE nodes. */
4453 is_properly_derived_from (derived, base)
4457 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4458 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4461 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4462 considers every class derived from itself. */
4463 return (!same_type_p (TYPE_MAIN_VARIANT (derived),
4464 TYPE_MAIN_VARIANT (base))
4465 && DERIVED_FROM_P (base, derived));
4468 /* We build the ICS for an implicit object parameter as a pointer
4469 conversion sequence. However, such a sequence should be compared
4470 as if it were a reference conversion sequence. If ICS is the
4471 implicit conversion sequence for an implicit object parameter,
4472 modify it accordingly. */
4475 maybe_handle_implicit_object (ics)
4478 if (ICS_THIS_FLAG (*ics))
4480 /* [over.match.funcs]
4482 For non-static member functions, the type of the
4483 implicit object parameter is "reference to cv X"
4484 where X is the class of which the function is a
4485 member and cv is the cv-qualification on the member
4486 function declaration. */
4488 tree reference_type;
4490 /* The `this' parameter is a pointer to a class type. Make the
4491 implict conversion talk about a reference to that same class
4493 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4494 reference_type = build_reference_type (reference_type);
4496 if (TREE_CODE (t) == QUAL_CONV)
4497 t = TREE_OPERAND (t, 0);
4498 if (TREE_CODE (t) == PTR_CONV)
4499 t = TREE_OPERAND (t, 0);
4500 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4501 t = direct_reference_binding (reference_type, t);
4506 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
4507 to the type the reference originally referred to, and return 1.
4508 Otherwise, return 0. */
4511 maybe_handle_ref_bind (ics, target_type)
4515 if (TREE_CODE (*ics) == REF_BIND)
4517 *target_type = TREE_TYPE (TREE_TYPE (*ics));
4518 *ics = TREE_OPERAND (*ics, 0);
4525 /* Compare two implicit conversion sequences according to the rules set out in
4526 [over.ics.rank]. Return values:
4528 1: ics1 is better than ics2
4529 -1: ics2 is better than ics1
4530 0: ics1 and ics2 are indistinguishable */
4533 compare_ics (ics1, ics2)
4540 tree deref_from_type1 = NULL_TREE;
4541 tree deref_from_type2 = NULL_TREE;
4542 tree deref_to_type1 = NULL_TREE;
4543 tree deref_to_type2 = NULL_TREE;
4545 /* REF_BINDING is non-zero if the result of the conversion sequence
4546 is a reference type. In that case TARGET_TYPE is the
4547 type referred to by the reference. */
4553 /* Handle implicit object parameters. */
4554 maybe_handle_implicit_object (&ics1);
4555 maybe_handle_implicit_object (&ics2);
4557 /* Handle reference parameters. */
4558 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
4559 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
4563 When comparing the basic forms of implicit conversion sequences (as
4564 defined in _over.best.ics_)
4566 --a standard conversion sequence (_over.ics.scs_) is a better
4567 conversion sequence than a user-defined conversion sequence
4568 or an ellipsis conversion sequence, and
4570 --a user-defined conversion sequence (_over.ics.user_) is a
4571 better conversion sequence than an ellipsis conversion sequence
4572 (_over.ics.ellipsis_). */
4573 if (ICS_RANK (ics1) > ICS_RANK (ics2))
4575 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
4578 if (ICS_RANK (ics1) == BAD_RANK)
4580 /* Both ICS are bad. We try to make a decision based on what
4581 would have happenned if they'd been good. */
4582 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4583 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4585 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4586 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4589 /* We couldn't make up our minds; try to figure it out below. */
4592 if (ICS_ELLIPSIS_FLAG (ics1))
4593 /* Both conversions are ellipsis conversions. */
4596 /* User-defined conversion sequence U1 is a better conversion sequence
4597 than another user-defined conversion sequence U2 if they contain the
4598 same user-defined conversion operator or constructor and if the sec-
4599 ond standard conversion sequence of U1 is better than the second
4600 standard conversion sequence of U2. */
4602 if (ICS_USER_FLAG (ics1))
4606 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4607 if (TREE_CODE (t1) == AMBIG_CONV)
4609 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4610 if (TREE_CODE (t2) == AMBIG_CONV)
4613 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4616 /* We can just fall through here, after setting up
4617 FROM_TYPE1 and FROM_TYPE2. */
4618 from_type1 = TREE_TYPE (t1);
4619 from_type2 = TREE_TYPE (t2);
4623 /* We're dealing with two standard conversion sequences.
4627 Standard conversion sequence S1 is a better conversion
4628 sequence than standard conversion sequence S2 if
4630 --S1 is a proper subsequence of S2 (comparing the conversion
4631 sequences in the canonical form defined by _over.ics.scs_,
4632 excluding any Lvalue Transformation; the identity
4633 conversion sequence is considered to be a subsequence of
4634 any non-identity conversion sequence */
4637 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4638 from_type1 = TREE_OPERAND (from_type1, 0);
4639 from_type1 = TREE_TYPE (from_type1);
4642 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4643 from_type2 = TREE_OPERAND (from_type2, 0);
4644 from_type2 = TREE_TYPE (from_type2);
4647 if (same_type_p (from_type1, from_type2))
4649 if (is_subseq (ics1, ics2))
4651 if (is_subseq (ics2, ics1))
4654 /* Otherwise, one sequence cannot be a subsequence of the other; they
4655 don't start with the same type. This can happen when comparing the
4656 second standard conversion sequence in two user-defined conversion
4663 --the rank of S1 is better than the rank of S2 (by the rules
4666 Standard conversion sequences are ordered by their ranks: an Exact
4667 Match is a better conversion than a Promotion, which is a better
4668 conversion than a Conversion.
4670 Two conversion sequences with the same rank are indistinguishable
4671 unless one of the following rules applies:
4673 --A conversion that is not a conversion of a pointer, or pointer
4674 to member, to bool is better than another conversion that is such
4677 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4678 so that we do not have to check it explicitly. */
4679 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4681 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4684 to_type1 = TREE_TYPE (ics1);
4685 to_type2 = TREE_TYPE (ics2);
4687 if (TYPE_PTR_P (from_type1)
4688 && TYPE_PTR_P (from_type2)
4689 && TYPE_PTR_P (to_type1)
4690 && TYPE_PTR_P (to_type2))
4692 deref_from_type1 = TREE_TYPE (from_type1);
4693 deref_from_type2 = TREE_TYPE (from_type2);
4694 deref_to_type1 = TREE_TYPE (to_type1);
4695 deref_to_type2 = TREE_TYPE (to_type2);
4697 /* The rules for pointers to members A::* are just like the rules
4698 for pointers A*, except opposite: if B is derived from A then
4699 A::* converts to B::*, not vice versa. For that reason, we
4700 switch the from_ and to_ variables here. */
4701 else if (TYPE_PTRMEM_P (from_type1)
4702 && TYPE_PTRMEM_P (from_type2)
4703 && TYPE_PTRMEM_P (to_type1)
4704 && TYPE_PTRMEM_P (to_type2))
4706 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4707 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4708 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4709 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4711 else if (TYPE_PTRMEMFUNC_P (from_type1)
4712 && TYPE_PTRMEMFUNC_P (from_type2)
4713 && TYPE_PTRMEMFUNC_P (to_type1)
4714 && TYPE_PTRMEMFUNC_P (to_type2))
4716 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4717 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4718 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4719 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4722 if (deref_from_type1 != NULL_TREE
4723 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4724 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4726 /* This was one of the pointer or pointer-like conversions.
4730 --If class B is derived directly or indirectly from class A,
4731 conversion of B* to A* is better than conversion of B* to
4732 void*, and conversion of A* to void* is better than
4733 conversion of B* to void*. */
4734 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4735 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4737 if (is_properly_derived_from (deref_from_type1,
4740 else if (is_properly_derived_from (deref_from_type2,
4744 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4745 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4747 if (same_type_p (deref_from_type1, deref_from_type2))
4749 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4751 if (is_properly_derived_from (deref_from_type1,
4755 /* We know that DEREF_TO_TYPE1 is `void' here. */
4756 else if (is_properly_derived_from (deref_from_type1,
4761 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4762 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4766 --If class B is derived directly or indirectly from class A
4767 and class C is derived directly or indirectly from B,
4769 --conversion of C* to B* is better than conversion of C* to
4772 --conversion of B* to A* is better than conversion of C* to
4774 if (same_type_p (deref_from_type1, deref_from_type2))
4776 if (is_properly_derived_from (deref_to_type1,
4779 else if (is_properly_derived_from (deref_to_type2,
4783 else if (same_type_p (deref_to_type1, deref_to_type2))
4785 if (is_properly_derived_from (deref_from_type2,
4788 else if (is_properly_derived_from (deref_from_type1,
4794 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4795 && same_type_p (from_type1, from_type2))
4799 --binding of an expression of type C to a reference of type
4800 B& is better than binding an expression of type C to a
4801 reference of type A&
4803 --conversion of C to B is better than conversion of C to A, */
4804 if (is_properly_derived_from (from_type1, to_type1)
4805 && is_properly_derived_from (from_type1, to_type2))
4807 if (is_properly_derived_from (to_type1, to_type2))
4809 else if (is_properly_derived_from (to_type2, to_type1))
4813 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4814 && same_type_p (to_type1, to_type2))
4818 --binding of an expression of type B to a reference of type
4819 A& is better than binding an expression of type C to a
4820 reference of type A&,
4822 --onversion of B to A is better than conversion of C to A */
4823 if (is_properly_derived_from (from_type1, to_type1)
4824 && is_properly_derived_from (from_type2, to_type1))
4826 if (is_properly_derived_from (from_type2, from_type1))
4828 else if (is_properly_derived_from (from_type1, from_type2))
4835 --S1 and S2 differ only in their qualification conversion and yield
4836 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4837 qualification signature of type T1 is a proper subset of the cv-
4838 qualification signature of type T2 */
4839 if (TREE_CODE (ics1) == QUAL_CONV
4840 && TREE_CODE (ics2) == QUAL_CONV
4841 && same_type_p (from_type1, from_type2))
4842 return comp_cv_qual_signature (to_type1, to_type2);
4846 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4847 types to which the references refer are the same type except for
4848 top-level cv-qualifiers, and the type to which the reference
4849 initialized by S2 refers is more cv-qualified than the type to
4850 which the reference initialized by S1 refers */
4852 if (ref_binding1 && ref_binding2
4853 && same_type_p (TYPE_MAIN_VARIANT (to_type1),
4854 TYPE_MAIN_VARIANT (to_type2)))
4855 return comp_cv_qualification (target_type2, target_type1);
4857 /* Neither conversion sequence is better than the other. */
4861 /* The source type for this standard conversion sequence. */
4867 for (;; t = TREE_OPERAND (t, 0))
4869 if (TREE_CODE (t) == USER_CONV
4870 || TREE_CODE (t) == AMBIG_CONV
4871 || TREE_CODE (t) == IDENTITY_CONV)
4872 return TREE_TYPE (t);
4874 my_friendly_abort (1823);
4877 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4878 a pointer to LOSER and re-running joust to produce the warning if WINNER
4879 is actually used. */
4882 add_warning (winner, loser)
4883 struct z_candidate *winner, *loser;
4885 winner->warnings = tree_cons (NULL_PTR,
4886 build_expr_ptr_wrapper (loser),
4890 /* Returns true iff functions are equivalent. Equivalent functions are
4891 not identical only if one is a function-local extern function. */
4894 equal_functions (fn1, fn2)
4898 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2))
4899 return decls_match (fn1, fn2);
4903 /* Compare two candidates for overloading as described in
4904 [over.match.best]. Return values:
4906 1: cand1 is better than cand2
4907 -1: cand2 is better than cand1
4908 0: cand1 and cand2 are indistinguishable */
4911 joust (cand1, cand2, warn)
4912 struct z_candidate *cand1, *cand2;
4916 int i, off1 = 0, off2 = 0, len;
4918 /* Candidates that involve bad conversions are always worse than those
4920 if (cand1->viable > cand2->viable)
4922 if (cand1->viable < cand2->viable)
4925 /* If we have two pseudo-candidates for conversions to the same type,
4926 arbitrarily pick one. */
4927 if (TYPE_P (cand1->fn) && cand1->fn == cand2->fn)
4930 /* a viable function F1
4931 is defined to be a better function than another viable function F2 if
4932 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4933 ICSi(F2), and then */
4935 /* for some argument j, ICSj(F1) is a better conversion sequence than
4938 /* For comparing static and non-static member functions, we ignore
4939 the implicit object parameter of the non-static function. The
4940 standard says to pretend that the static function has an object
4941 parm, but that won't work with operator overloading. */
4942 len = TREE_VEC_LENGTH (cand1->convs);
4943 if (len != TREE_VEC_LENGTH (cand2->convs))
4945 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4946 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4948 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4949 && DECL_STATIC_FUNCTION_P (cand2->fn))
4955 my_friendly_abort (42);
4958 for (i = 0; i < len; ++i)
4960 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4961 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4962 int comp = compare_ics (t1, t2);
4967 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4968 && TREE_CODE (t1) == STD_CONV
4969 && TREE_CODE (t2) == STD_CONV
4970 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4971 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4972 && (TYPE_PRECISION (TREE_TYPE (t1))
4973 == TYPE_PRECISION (TREE_TYPE (t2)))
4974 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4975 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4978 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4980 struct z_candidate *w, *l;
4982 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4983 w = cand1, l = cand2;
4985 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4986 w = cand2, l = cand1;
4990 cp_warning ("passing `%T' chooses `%T' over `%T'",
4991 type, type1, type2);
4992 cp_warning (" in call to `%D'", w->fn);
4998 if (winner && comp != winner)
5007 /* warn about confusing overload resolution for user-defined conversions,
5008 either between a constructor and a conversion op, or between two
5010 if (winner && cand1->second_conv
5011 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5012 != DECL_CONSTRUCTOR_P (cand2->fn))
5013 /* Don't warn if the two conv ops convert to the same type... */
5014 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5015 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5016 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5018 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5021 struct z_candidate *w, *l;
5023 w = cand1, l = cand2;
5025 w = cand2, l = cand1;
5028 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5029 if (! DECL_CONSTRUCTOR_P (w->fn))
5030 source = TREE_TYPE (source);
5031 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
5032 cp_warning (" for conversion from `%T' to `%T'",
5033 source, TREE_TYPE (w->second_conv));
5034 cp_warning (" because conversion sequence for the argument is better");
5045 F1 is a non-template function and F2 is a template function */
5047 if (! cand1->template && cand2->template)
5049 else if (cand1->template && ! cand2->template)
5051 else if (cand1->template && cand2->template)
5052 winner = more_specialized
5053 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5057 the context is an initialization by user-defined conversion (see
5058 _dcl.init_ and _over.match.user_) and the standard conversion
5059 sequence from the return type of F1 to the destination type (i.e.,
5060 the type of the entity being initialized) is a better conversion
5061 sequence than the standard conversion sequence from the return type
5062 of F2 to the destination type. */
5064 if (! winner && cand1->second_conv)
5065 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5067 /* If the built-in candidates are the same, arbitrarily pick one. */
5068 if (! winner && cand1->fn == cand2->fn
5069 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5071 for (i = 0; i < len; ++i)
5072 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5073 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5075 if (i == TREE_VEC_LENGTH (cand1->convs))
5078 /* Kludge around broken overloading rules whereby
5079 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
5080 that takes references and another that takes values. */
5081 if (cand1->fn == ansi_opname[COND_EXPR])
5083 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
5084 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
5085 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
5086 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
5088 if (same_type_p (t1, t2))
5090 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
5092 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
5098 /* If the two functions are the same (this can happen with declarations
5099 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5100 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5101 && equal_functions (cand1->fn, cand2->fn))
5106 /* Extension: If the worst conversion for one candidate is worse than the
5107 worst conversion for the other, take the first. */
5108 if (! winner && ! pedantic)
5110 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5112 for (i = 0; i < len; ++i)
5114 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5115 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5116 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5117 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5129 /* Given a list of candidates for overloading, find the best one, if any.
5130 This algorithm has a worst case of O(2n) (winner is last), and a best
5131 case of O(n/2) (totally ambiguous); much better than a sorting
5134 static struct z_candidate *
5135 tourney (candidates)
5136 struct z_candidate *candidates;
5138 struct z_candidate *champ = candidates, *challenger;
5140 int champ_compared_to_predecessor = 0;
5142 /* Walk through the list once, comparing each current champ to the next
5143 candidate, knocking out a candidate or two with each comparison. */
5145 for (challenger = champ->next; challenger; )
5147 fate = joust (champ, challenger, 0);
5149 challenger = challenger->next;
5154 champ = challenger->next;
5157 champ_compared_to_predecessor = 0;
5162 champ_compared_to_predecessor = 1;
5165 challenger = champ->next;
5169 /* Make sure the champ is better than all the candidates it hasn't yet
5170 been compared to. */
5172 for (challenger = candidates;
5174 && !(champ_compared_to_predecessor && challenger->next == champ);
5175 challenger = challenger->next)
5177 fate = joust (champ, challenger, 0);
5185 /* Returns non-zero if things of type FROM can be converted to TO. */
5188 can_convert (to, from)
5191 return can_convert_arg (to, from, NULL_TREE);
5194 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5197 can_convert_arg (to, from, arg)
5200 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5201 return (t && ! ICS_BAD_FLAG (t));
5204 /* Convert EXPR to TYPE. Return the converted expression. */
5207 perform_implicit_conversion (type, expr)
5213 if (expr == error_mark_node)
5214 return error_mark_node;
5215 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5217 if (!conv || ICS_BAD_FLAG (conv))
5219 cp_error ("could not convert `%E' to `%T'", expr, type);
5220 return error_mark_node;
5223 return convert_like (conv, expr);
5226 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5227 initializing a variable of that TYPE. Return the converted
5231 initialize_reference (type, expr)
5237 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5238 if (!conv || ICS_BAD_FLAG (conv))
5240 cp_error ("could not convert `%E' to `%T'", expr, type);
5241 return error_mark_node;
5244 return convert_like (conv, expr);