1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 92-97, 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
39 extern int inhibit_warnings;
40 extern tree ctor_label, dtor_label;
42 static tree build_new_method_call PROTO((tree, tree, tree, tree, int));
44 static tree build_field_call PROTO((tree, tree, tree, tree));
45 static struct z_candidate * tourney PROTO((struct z_candidate *));
46 static int joust PROTO((struct z_candidate *, struct z_candidate *, int));
47 static int compare_ics PROTO((tree, tree));
48 static tree build_over_call PROTO((struct z_candidate *, tree, int));
49 static tree convert_like PROTO((tree, tree));
50 static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
52 static tree build_object_call PROTO((tree, tree));
53 static tree resolve_args PROTO((tree));
54 static struct z_candidate * build_user_type_conversion_1
55 PROTO ((tree, tree, int));
56 static void print_z_candidates PROTO((struct z_candidate *));
57 static tree build_this PROTO((tree));
58 static struct z_candidate * splice_viable PROTO((struct z_candidate *));
59 static int any_viable PROTO((struct z_candidate *));
60 static struct z_candidate * add_template_candidate
61 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
63 static struct z_candidate * add_template_candidate_real
64 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
65 tree, unification_kind_t));
66 static struct z_candidate * add_template_conv_candidate
67 PROTO((struct z_candidate *, tree, tree, tree, tree));
68 static struct z_candidate * add_builtin_candidates
69 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
71 static struct z_candidate * add_builtin_candidate
72 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
73 tree, tree, tree, tree *, tree *, int));
74 static int is_complete PROTO((tree));
75 static struct z_candidate * build_builtin_candidate
76 PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
78 static struct z_candidate * add_conv_candidate
79 PROTO((struct z_candidate *, tree, tree, tree));
80 static struct z_candidate * add_function_candidate
81 PROTO((struct z_candidate *, tree, tree, int));
82 static tree implicit_conversion PROTO((tree, tree, tree, int));
83 static tree standard_conversion PROTO((tree, tree, tree));
84 static tree reference_binding PROTO((tree, tree, tree, int));
85 static tree strip_top_quals PROTO((tree));
86 static tree non_reference PROTO((tree));
87 static tree build_conv PROTO((enum tree_code, tree, tree));
88 static int is_subseq PROTO((tree, tree));
89 static int maybe_handle_ref_bind PROTO((tree*, tree*));
90 static void maybe_handle_implicit_object PROTO((tree*));
91 static struct z_candidate * add_candidate PROTO((struct z_candidate *,
93 static tree source_type PROTO((tree));
94 static void add_warning PROTO((struct z_candidate *, struct z_candidate *));
97 build_vfield_ref (datum, type)
102 if (datum == error_mark_node)
103 return error_mark_node;
105 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
106 datum = convert_from_reference (datum);
108 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
109 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
110 datum, CLASSTYPE_VFIELD (type));
112 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
117 /* Build a call to a member of an object. I.e., one that overloads
118 operator ()(), or is a pointer-to-function or pointer-to-method. */
121 build_field_call (basetype_path, instance_ptr, name, parms)
122 tree basetype_path, instance_ptr, name, parms;
124 tree field, instance;
126 if (name == ctor_identifier || name == dtor_identifier)
129 /* Speed up the common case. */
130 if (instance_ptr == current_class_ptr
131 && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
134 field = lookup_field (basetype_path, name, 1, 0);
136 if (field == error_mark_node || field == NULL_TREE)
139 if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
141 /* If it's a field, try overloading operator (),
142 or calling if the field is a pointer-to-function. */
143 instance = build_indirect_ref (instance_ptr, NULL_PTR);
144 instance = build_component_ref_1 (instance, field, 0);
146 if (instance == error_mark_node)
147 return error_mark_node;
149 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
150 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
151 instance, parms, NULL_TREE);
152 else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
154 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
155 return build_function_call (instance, parms);
156 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
158 return build_function_call
159 (instance, expr_tree_cons (NULL_TREE, instance_ptr, parms));
166 /* Returns nonzero iff the destructor name specified in NAME
167 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
171 check_dtor_name (basetype, name)
174 name = TREE_OPERAND (name, 0);
176 /* Just accept something we've already complained about. */
177 if (name == error_mark_node)
180 if (TREE_CODE (name) == TYPE_DECL)
181 name = TREE_TYPE (name);
182 else if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
184 else if (TREE_CODE (name) == IDENTIFIER_NODE)
186 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
187 || (TREE_CODE (basetype) == ENUMERAL_TYPE
188 && name == TYPE_IDENTIFIER (basetype)))
191 name = get_type_value (name);
194 my_friendly_abort (980605);
196 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
201 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
202 This is how virtual function calls are avoided. */
205 build_scoped_method_call (exp, basetype, name, parms)
206 tree exp, basetype, name, parms;
208 /* Because this syntactic form does not allow
209 a pointer to a base class to be `stolen',
210 we need not protect the derived->base conversion
213 @@ But we do have to check access privileges later. */
215 tree type = TREE_TYPE (exp);
217 if (type == error_mark_node
218 || basetype == error_mark_node)
219 return error_mark_node;
221 if (processing_template_decl)
223 if (TREE_CODE (name) == BIT_NOT_EXPR
224 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
226 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
228 name = build_min_nt (BIT_NOT_EXPR, type);
230 name = build_min_nt (SCOPE_REF, basetype, name);
231 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
234 if (TREE_CODE (type) == REFERENCE_TYPE)
235 type = TREE_TYPE (type);
237 if (TREE_CODE (basetype) == TREE_VEC)
240 basetype = BINFO_TYPE (binfo);
245 /* Check the destructor call syntax. */
246 if (TREE_CODE (name) == BIT_NOT_EXPR)
248 /* We can get here if someone writes their destructor call like
249 `obj.NS::~T()'; this isn't really a scoped method call, so hand
251 if (TREE_CODE (basetype) == NAMESPACE_DECL)
252 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
254 if (! check_dtor_name (basetype, name))
255 cp_error ("qualified type `%T' does not match destructor name `~%T'",
256 basetype, TREE_OPERAND (name, 0));
258 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
259 that explicit ~int is caught in the parser; this deals with typedefs
260 and template parms. */
261 if (! IS_AGGR_TYPE (basetype))
263 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
264 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
265 exp, basetype, type);
267 return cp_convert (void_type_node, exp);
271 if (! is_aggr_type (basetype, 1))
272 return error_mark_node;
274 if (! IS_AGGR_TYPE (type))
276 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
278 return error_mark_node;
283 binfo = get_binfo (basetype, type, 1);
284 if (binfo == error_mark_node)
285 return error_mark_node;
287 error_not_base_type (basetype, type);
292 if (TREE_CODE (exp) == INDIRECT_REF)
293 decl = build_indirect_ref
294 (convert_pointer_to_real
295 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
297 decl = build_scoped_ref (exp, basetype);
299 /* Call to a destructor. */
300 if (TREE_CODE (name) == BIT_NOT_EXPR)
302 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
303 return cp_convert (void_type_node, exp);
305 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
306 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
310 /* Call to a method. */
311 return build_method_call (decl, name, parms, binfo,
312 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
314 return error_mark_node;
317 /* We want the address of a function or method. We avoid creating a
318 pointer-to-member function. */
321 build_addr_func (function)
324 tree type = TREE_TYPE (function);
326 /* We have to do these by hand to avoid real pointer to member
328 if (TREE_CODE (type) == METHOD_TYPE)
332 type = build_pointer_type (type);
334 if (mark_addressable (function) == 0)
335 return error_mark_node;
337 addr = build1 (ADDR_EXPR, type, function);
339 /* Address of a static or external variable or function counts
341 if (staticp (function))
342 TREE_CONSTANT (addr) = 1;
347 function = default_conversion (function);
352 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
353 POINTER_TYPE to those. Note, pointer to member function types
354 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
357 build_call (function, result_type, parms)
358 tree function, result_type, parms;
360 int is_constructor = 0;
364 function = build_addr_func (function);
366 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
368 sorry ("unable to call pointer to member function here");
369 return error_mark_node;
372 if (TREE_CODE (function) == ADDR_EXPR
373 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
374 decl = TREE_OPERAND (function, 0);
378 if (decl && DECL_CONSTRUCTOR_P (decl))
382 my_friendly_assert (TREE_USED (decl), 990125);
384 /* Don't pass empty class objects by value. This is useful
385 for tags in STL, which are used to control overload resolution.
386 We don't need to handle other cases of copying empty classes. */
387 if (! decl || ! DECL_BUILT_IN (decl))
388 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
389 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
390 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
392 tree t = make_node (RTL_EXPR);
393 TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
394 RTL_EXPR_RTL (t) = const0_rtx;
395 RTL_EXPR_SEQUENCE (t) = NULL_RTX;
396 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
397 TREE_VALUE (tmp), t);
400 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
401 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
402 TREE_TYPE (function) = result_type;
403 TREE_SIDE_EFFECTS (function) = 1;
408 /* Build something of the form ptr->method (args)
409 or object.method (args). This can also build
410 calls to constructors, and find friends.
412 Member functions always take their class variable
415 INSTANCE is a class instance.
417 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
419 PARMS help to figure out what that NAME really refers to.
421 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
422 down to the real instance type to use for access checking. We need this
423 information to get protected accesses correct. This parameter is used
424 by build_member_call.
426 FLAGS is the logical disjunction of zero or more LOOKUP_
427 flags. See cp-tree.h for more info.
429 If this is all OK, calls build_function_call with the resolved
432 This function must also handle being called to perform
433 initialization, promotion/coercion of arguments, and
434 instantiation of default parameters.
436 Note that NAME may refer to an instance variable name. If
437 `operator()()' is defined for the type of that field, then we return
441 build_method_call (instance, name, parms, basetype_path, flags)
442 tree instance, name, parms, basetype_path;
445 tree basetype, instance_ptr;
447 #ifdef GATHER_STATISTICS
448 n_build_method_call++;
451 if (instance == error_mark_node
452 || name == error_mark_node
453 || parms == error_mark_node
454 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
455 return error_mark_node;
457 if (processing_template_decl)
459 /* We need to process template parm names here so that tsubst catches
460 them properly. Other type names can wait. */
461 if (TREE_CODE (name) == BIT_NOT_EXPR)
463 tree type = NULL_TREE;
465 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
466 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
467 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
468 type = TREE_TYPE (TREE_OPERAND (name, 0));
470 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
471 name = build_min_nt (BIT_NOT_EXPR, type);
474 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
477 /* This is the logic that magically deletes the second argument to
478 operator delete, if it is not needed. */
479 if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
481 tree save_last = TREE_CHAIN (parms);
483 /* get rid of unneeded argument */
484 TREE_CHAIN (parms) = NULL_TREE;
485 if (build_method_call (instance, name, parms, basetype_path,
486 (LOOKUP_SPECULATIVELY|flags) & ~LOOKUP_COMPLAIN))
488 /* If it finds a match, return it. */
489 return build_method_call (instance, name, parms, basetype_path, flags);
491 /* If it doesn't work, two argument delete must work */
492 TREE_CHAIN (parms) = save_last;
494 /* We already know whether it's needed or not for vec delete. */
495 else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
496 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
497 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
498 TREE_CHAIN (parms) = NULL_TREE;
500 if (TREE_CODE (name) == BIT_NOT_EXPR)
503 error ("destructors take no parameters");
504 basetype = TREE_TYPE (instance);
505 if (TREE_CODE (basetype) == REFERENCE_TYPE)
506 basetype = TREE_TYPE (basetype);
508 if (! check_dtor_name (basetype, name))
510 ("destructor name `~%T' does not match type `%T' of expression",
511 TREE_OPERAND (name, 0), basetype);
513 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
514 return cp_convert (void_type_node, instance);
515 instance = default_conversion (instance);
516 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
517 return build_delete (build_pointer_type (basetype),
518 instance_ptr, integer_two_node,
519 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
522 return build_new_method_call (instance, name, parms, basetype_path, flags);
525 /* New overloading code. */
535 struct z_candidate *next;
538 #define IDENTITY_RANK 0
544 #define ELLIPSIS_RANK 6
547 #define ICS_RANK(NODE) \
548 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
549 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
550 : ICS_USER_FLAG (NODE) ? USER_RANK \
551 : ICS_STD_RANK (NODE))
553 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
555 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
556 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
557 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
558 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
560 #define USER_CONV_CAND(NODE) \
561 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
562 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
569 || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE))
575 build_conv (code, type, from)
579 tree t = build1 (code, type, from);
580 int rank = ICS_STD_RANK (from);
592 if (rank < EXACT_RANK)
598 ICS_STD_RANK (t) = rank;
599 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
600 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
608 if (TREE_CODE (t) == REFERENCE_TYPE)
617 if (TREE_CODE (t) == ARRAY_TYPE)
619 return TYPE_MAIN_VARIANT (t);
622 /* Returns the standard conversion path (see [conv]) from type FROM to type
623 TO, if any. For proper handling of null pointer constants, you must
624 also pass the expression EXPR to convert from. */
627 standard_conversion (to, from, expr)
630 enum tree_code fcode, tcode;
634 if (TREE_CODE (to) == REFERENCE_TYPE)
636 if (TREE_CODE (from) == REFERENCE_TYPE)
639 from = TREE_TYPE (from);
641 to = strip_top_quals (to);
642 from = strip_top_quals (from);
644 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
645 && expr && type_unknown_p (expr))
647 expr = instantiate_type (to, expr, 0);
648 if (expr == error_mark_node)
650 from = TREE_TYPE (expr);
653 fcode = TREE_CODE (from);
654 tcode = TREE_CODE (to);
656 conv = build1 (IDENTITY_CONV, from, expr);
658 if (fcode == FUNCTION_TYPE)
660 from = build_pointer_type (from);
661 fcode = TREE_CODE (from);
662 conv = build_conv (LVALUE_CONV, from, conv);
664 else if (fcode == ARRAY_TYPE)
666 from = build_pointer_type (TREE_TYPE (from));
667 fcode = TREE_CODE (from);
668 conv = build_conv (LVALUE_CONV, from, conv);
670 else if (fromref || (expr && real_lvalue_p (expr)))
671 conv = build_conv (RVALUE_CONV, from, conv);
676 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
677 && expr && null_ptr_cst_p (expr))
679 conv = build_conv (STD_CONV, to, conv);
681 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
683 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
684 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
686 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
687 TYPE_MAIN_VARIANT (TREE_TYPE (to))))
689 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
690 && ufcode != FUNCTION_TYPE)
692 from = build_pointer_type
693 (cp_build_qualified_type (void_type_node,
694 CP_TYPE_QUALS (TREE_TYPE (from))));
695 conv = build_conv (PTR_CONV, from, conv);
697 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
699 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
700 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
702 if (DERIVED_FROM_P (fbase, tbase)
704 (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
705 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))))))
707 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
708 from = build_pointer_type (from);
709 conv = build_conv (PMEM_CONV, from, conv);
712 else if (IS_AGGR_TYPE (TREE_TYPE (from))
713 && IS_AGGR_TYPE (TREE_TYPE (to)))
715 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
718 cp_build_qualified_type (TREE_TYPE (to),
719 CP_TYPE_QUALS (TREE_TYPE (from)));
720 from = build_pointer_type (from);
721 conv = build_conv (PTR_CONV, from, conv);
725 if (same_type_p (from, to))
727 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
728 conv = build_conv (QUAL_CONV, to, conv);
729 else if (expr && string_conv_p (to, expr, 0))
730 /* converting from string constant to char *. */
731 conv = build_conv (QUAL_CONV, to, conv);
732 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
734 conv = build_conv (PTR_CONV, to, conv);
735 ICS_BAD_FLAG (conv) = 1;
742 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
744 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
745 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
746 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
747 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
749 if (! DERIVED_FROM_P (fbase, tbase)
750 || ! same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
751 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
752 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
753 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
756 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
757 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
758 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
759 from = build_ptrmemfunc_type (build_pointer_type (from));
760 conv = build_conv (PMEM_CONV, from, conv);
762 else if (tcode == BOOLEAN_TYPE)
764 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
765 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
768 conv = build_conv (STD_CONV, to, conv);
769 if (fcode == POINTER_TYPE
770 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
771 ICS_STD_RANK (conv) = PBOOL_RANK;
773 /* We don't check for ENUMERAL_TYPE here because there are no standard
774 conversions to enum type. */
775 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
776 || tcode == REAL_TYPE)
778 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
780 conv = build_conv (STD_CONV, to, conv);
782 /* Give this a better rank if it's a promotion. */
783 if (to == type_promotes_to (from)
784 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
785 ICS_STD_RANK (conv) = PROMO_RANK;
787 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
788 && DERIVED_FROM_P (to, from))
790 if (TREE_CODE (conv) == RVALUE_CONV)
791 conv = TREE_OPERAND (conv, 0);
792 conv = build_conv (BASE_CONV, to, conv);
800 /* Returns the conversion path from type FROM to reference type TO for
801 purposes of reference binding. For lvalue binding, either pass a
802 reference type to FROM or an lvalue expression to EXPR.
804 Currently does not distinguish in the generated trees between binding to
805 an lvalue and a temporary. Should it? */
808 reference_binding (rto, rfrom, expr, flags)
809 tree rto, rfrom, expr;
814 tree to = TREE_TYPE (rto);
818 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
820 expr = instantiate_type (to, expr, 0);
821 if (expr == error_mark_node)
823 from = TREE_TYPE (expr);
826 if (TREE_CODE (from) == REFERENCE_TYPE)
827 from = TREE_TYPE (from);
828 else if (! expr || ! real_lvalue_p (expr))
831 related = (same_type_p (TYPE_MAIN_VARIANT (to),
832 TYPE_MAIN_VARIANT (from))
833 || (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
834 && DERIVED_FROM_P (to, from)));
836 if (lvalue && related && at_least_as_qualified_p (to, from))
838 conv = build1 (IDENTITY_CONV, from, expr);
840 if (same_type_p (TYPE_MAIN_VARIANT (to),
841 TYPE_MAIN_VARIANT (from)))
842 conv = build_conv (REF_BIND, rto, conv);
845 conv = build_conv (REF_BIND, rto, conv);
846 ICS_STD_RANK (conv) = STD_RANK;
854 conv = standard_conversion (to, rfrom, expr);
857 conv = build_conv (REF_BIND, rto, conv);
859 /* Bind directly to a base subobject of a class rvalue. Do it
860 after building the conversion for proper handling of ICS_RANK. */
861 if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
862 TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
865 && ((! (CP_TYPE_CONST_NON_VOLATILE_P (to)
866 && (flags & LOOKUP_NO_TEMP_BIND) == 0))
867 /* If T1 is reference-related to T2, cv1 must be the same
868 cv-qualification as, or greater cv-qualification than,
869 cv2; otherwise, the program is ill-formed. */
870 || (related && !at_least_as_qualified_p (to, from))))
871 ICS_BAD_FLAG (conv) = 1;
877 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
878 to type TO. The optional expression EXPR may affect the conversion.
879 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
883 implicit_conversion (to, from, expr, flags)
888 struct z_candidate *cand;
890 if (TREE_CODE (to) == REFERENCE_TYPE)
891 conv = reference_binding (to, from, expr, flags);
893 conv = standard_conversion (to, from, expr);
897 else if (expr != NULL_TREE
898 && (IS_AGGR_TYPE (non_reference (from))
899 || IS_AGGR_TYPE (non_reference (to)))
900 && (flags & LOOKUP_NO_CONVERSION) == 0)
902 cand = build_user_type_conversion_1
903 (to, expr, LOOKUP_ONLYCONVERTING);
905 conv = cand->second_conv;
906 if ((! conv || ICS_BAD_FLAG (conv))
907 && TREE_CODE (to) == REFERENCE_TYPE
908 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
910 cand = build_user_type_conversion_1
911 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
914 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to)))
915 ICS_BAD_FLAG (cand->second_conv) = 1;
916 if (!conv || (ICS_BAD_FLAG (conv)
917 > ICS_BAD_FLAG (cand->second_conv)))
918 conv = build_conv (REF_BIND, to, cand->second_conv);
926 /* Add a new entry to the list of candidates. Used by the add_*_candidate
929 static struct z_candidate *
930 add_candidate (candidates, fn, convs, viable)
931 struct z_candidate *candidates;
935 struct z_candidate *cand
936 = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
940 cand->second_conv = NULL_TREE;
941 cand->viable = viable;
942 cand->basetype_path = NULL_TREE;
943 cand->template = NULL_TREE;
944 cand->warnings = NULL_TREE;
945 cand->next = candidates;
950 /* Create an overload candidate for the function or method FN called with
951 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
952 to implicit_conversion. */
954 static struct z_candidate *
955 add_function_candidate (candidates, fn, arglist, flags)
956 struct z_candidate *candidates;
960 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
963 tree parmnode, argnode;
966 /* The `this' and `in_chrg' arguments to constructors are not considered
967 in overload resolution. */
968 if (DECL_CONSTRUCTOR_P (fn))
970 parmlist = TREE_CHAIN (parmlist);
971 arglist = TREE_CHAIN (arglist);
972 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
974 parmlist = TREE_CHAIN (parmlist);
975 arglist = TREE_CHAIN (arglist);
979 len = list_length (arglist);
980 convs = make_scratch_vec (len);
982 /* 13.3.2 - Viable functions [over.match.viable]
983 First, to be a viable function, a candidate function shall have enough
984 parameters to agree in number with the arguments in the list.
986 We need to check this first; otherwise, checking the ICSes might cause
987 us to produce an ill-formed template instantiation. */
990 for (i = 0; i < len; ++i)
992 if (parmnode == NULL_TREE || parmnode == void_list_node)
994 parmnode = TREE_CHAIN (parmnode);
997 if (i < len && parmnode)
1000 /* Make sure there are default args for the rest of the parms. */
1001 else for (; parmnode && parmnode != void_list_node;
1002 parmnode = TREE_CHAIN (parmnode))
1003 if (! TREE_PURPOSE (parmnode))
1012 /* Second, for F to be a viable function, there shall exist for each
1013 argument an implicit conversion sequence that converts that argument
1014 to the corresponding parameter of F. */
1016 parmnode = parmlist;
1019 for (i = 0; i < len; ++i)
1021 tree arg = TREE_VALUE (argnode);
1022 tree argtype = lvalue_type (arg);
1025 if (parmnode == void_list_node)
1030 tree parmtype = TREE_VALUE (parmnode);
1032 /* [over.match.funcs] For conversion functions, the function is
1033 considered to be a member of the class of the implicit object
1034 argument for the purpose of defining the type of the implicit
1037 Since build_over_call ignores the ICS for the `this' parameter,
1038 we can just change the parm type. */
1039 if (DECL_CONV_FN_P (fn) && i == 0)
1042 = build_qualified_type (TREE_TYPE (argtype),
1043 TYPE_QUALS (TREE_TYPE (parmtype)));
1044 parmtype = build_pointer_type (parmtype);
1047 t = implicit_conversion (parmtype, argtype, arg, flags);
1051 t = build1 (IDENTITY_CONV, argtype, arg);
1052 ICS_ELLIPSIS_FLAG (t) = 1;
1055 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1056 && ! DECL_CONSTRUCTOR_P (fn))
1057 ICS_THIS_FLAG (t) = 1;
1059 TREE_VEC_ELT (convs, i) = t;
1066 if (ICS_BAD_FLAG (t))
1070 parmnode = TREE_CHAIN (parmnode);
1071 argnode = TREE_CHAIN (argnode);
1075 return add_candidate (candidates, fn, convs, viable);
1078 /* Create an overload candidate for the conversion function FN which will
1079 be invoked for expression OBJ, producing a pointer-to-function which
1080 will in turn be called with the argument list ARGLIST, and add it to
1081 CANDIDATES. FLAGS is passed on to implicit_conversion.
1083 Actually, we don't really care about FN; we care about the type it
1084 converts to. There may be multiple conversion functions that will
1085 convert to that type, and we rely on build_user_type_conversion_1 to
1086 choose the best one; so when we create our candidate, we record the type
1087 instead of the function. */
1089 static struct z_candidate *
1090 add_conv_candidate (candidates, fn, obj, arglist)
1091 struct z_candidate *candidates;
1092 tree fn, obj, arglist;
1094 tree totype = TREE_TYPE (TREE_TYPE (fn));
1095 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1096 int i, len = list_length (arglist) + 1;
1097 tree convs = make_scratch_vec (len);
1098 tree parmnode = parmlist;
1099 tree argnode = arglist;
1101 int flags = LOOKUP_NORMAL;
1103 /* Don't bother looking up the same type twice. */
1104 if (candidates && candidates->fn == totype)
1107 for (i = 0; i < len; ++i)
1109 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1110 tree argtype = lvalue_type (arg);
1114 t = implicit_conversion (totype, argtype, arg, flags);
1115 else if (parmnode == void_list_node)
1118 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1121 t = build1 (IDENTITY_CONV, argtype, arg);
1122 ICS_ELLIPSIS_FLAG (t) = 1;
1125 TREE_VEC_ELT (convs, i) = t;
1129 if (ICS_BAD_FLAG (t))
1136 parmnode = TREE_CHAIN (parmnode);
1137 argnode = TREE_CHAIN (argnode);
1143 for (; parmnode && parmnode != void_list_node;
1144 parmnode = TREE_CHAIN (parmnode))
1145 if (! TREE_PURPOSE (parmnode))
1151 return add_candidate (candidates, totype, convs, viable);
1154 static struct z_candidate *
1155 build_builtin_candidate (candidates, fnname, type1, type2,
1156 args, argtypes, flags)
1157 struct z_candidate *candidates;
1158 tree fnname, type1, type2, *args, *argtypes;
1169 convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1171 for (i = 0; i < 2; ++i)
1176 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1180 /* We need something for printing the candidate. */
1181 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1183 else if (ICS_BAD_FLAG (t))
1185 TREE_VEC_ELT (convs, i) = t;
1188 /* For COND_EXPR we rearranged the arguments; undo that now. */
1191 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1192 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1193 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1195 TREE_VEC_ELT (convs, 0) = t;
1200 return add_candidate (candidates, fnname, convs, viable);
1207 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1210 /* Create any builtin operator overload candidates for the operator in
1211 question given the converted operand types TYPE1 and TYPE2. The other
1212 args are passed through from add_builtin_candidates to
1213 build_builtin_candidate. */
1215 static struct z_candidate *
1216 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1217 args, argtypes, flags)
1218 struct z_candidate *candidates;
1219 enum tree_code code, code2;
1220 tree fnname, type1, type2, *args, *argtypes;
1225 case POSTINCREMENT_EXPR:
1226 case POSTDECREMENT_EXPR:
1227 args[1] = integer_zero_node;
1228 type2 = integer_type_node;
1237 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1238 and VQ is either volatile or empty, there exist candidate operator
1239 functions of the form
1240 VQ T& operator++(VQ T&);
1241 T operator++(VQ T&, int);
1242 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1243 type other than bool, and VQ is either volatile or empty, there exist
1244 candidate operator functions of the form
1245 VQ T& operator--(VQ T&);
1246 T operator--(VQ T&, int);
1247 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1248 complete object type, and VQ is either volatile or empty, there exist
1249 candidate operator functions of the form
1250 T*VQ& operator++(T*VQ&);
1251 T*VQ& operator--(T*VQ&);
1252 T* operator++(T*VQ&, int);
1253 T* operator--(T*VQ&, int); */
1255 case POSTDECREMENT_EXPR:
1256 case PREDECREMENT_EXPR:
1257 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1259 case POSTINCREMENT_EXPR:
1260 case PREINCREMENT_EXPR:
1261 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1262 || TYPE_PTROB_P (type1))
1264 type1 = build_reference_type (type1);
1269 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1270 exist candidate operator functions of the form
1274 8 For every function type T, there exist candidate operator functions of
1276 T& operator*(T*); */
1279 if (TREE_CODE (type1) == POINTER_TYPE
1280 && (TYPE_PTROB_P (type1)
1281 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1285 /* 9 For every type T, there exist candidate operator functions of the form
1288 10For every promoted arithmetic type T, there exist candidate operator
1289 functions of the form
1293 case CONVERT_EXPR: /* unary + */
1294 if (TREE_CODE (type1) == POINTER_TYPE
1295 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1298 if (ARITHMETIC_TYPE_P (type1))
1302 /* 11For every promoted integral type T, there exist candidate operator
1303 functions of the form
1307 if (INTEGRAL_TYPE_P (type1))
1311 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1312 is the same type as C2 or is a derived class of C2, T is a complete
1313 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1314 there exist candidate operator functions of the form
1315 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1316 where CV12 is the union of CV1 and CV2. */
1319 if (TREE_CODE (type1) == POINTER_TYPE
1320 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1322 tree c1 = TREE_TYPE (type1);
1323 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1324 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1325 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1327 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1328 && (TYPE_PTRMEMFUNC_P (type2)
1329 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1334 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1335 didate operator functions of the form
1340 bool operator<(L, R);
1341 bool operator>(L, R);
1342 bool operator<=(L, R);
1343 bool operator>=(L, R);
1344 bool operator==(L, R);
1345 bool operator!=(L, R);
1346 where LR is the result of the usual arithmetic conversions between
1349 14For every pair of types T and I, where T is a cv-qualified or cv-
1350 unqualified complete object type and I is a promoted integral type,
1351 there exist candidate operator functions of the form
1352 T* operator+(T*, I);
1353 T& operator[](T*, I);
1354 T* operator-(T*, I);
1355 T* operator+(I, T*);
1356 T& operator[](I, T*);
1358 15For every T, where T is a pointer to complete object type, there exist
1359 candidate operator functions of the form112)
1360 ptrdiff_t operator-(T, T);
1362 16For every pointer type T, there exist candidate operator functions of
1364 bool operator<(T, T);
1365 bool operator>(T, T);
1366 bool operator<=(T, T);
1367 bool operator>=(T, T);
1368 bool operator==(T, T);
1369 bool operator!=(T, T);
1371 17For every pointer to member type T, there exist candidate operator
1372 functions of the form
1373 bool operator==(T, T);
1374 bool operator!=(T, T); */
1377 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1379 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1381 type2 = ptrdiff_type_node;
1385 case TRUNC_DIV_EXPR:
1386 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1392 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1393 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1395 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1396 && null_ptr_cst_p (args[1]))
1401 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1402 && null_ptr_cst_p (args[0]))
1413 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1414 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1416 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1421 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1429 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1432 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1434 type1 = ptrdiff_type_node;
1437 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1439 type2 = ptrdiff_type_node;
1444 /* 18For every pair of promoted integral types L and R, there exist candi-
1445 date operator functions of the form
1452 where LR is the result of the usual arithmetic conversions between
1455 case TRUNC_MOD_EXPR:
1461 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1465 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1466 type, VQ is either volatile or empty, and R is a promoted arithmetic
1467 type, there exist candidate operator functions of the form
1468 VQ L& operator=(VQ L&, R);
1469 VQ L& operator*=(VQ L&, R);
1470 VQ L& operator/=(VQ L&, R);
1471 VQ L& operator+=(VQ L&, R);
1472 VQ L& operator-=(VQ L&, R);
1474 20For every pair T, VQ), where T is any type and VQ is either volatile
1475 or empty, there exist candidate operator functions of the form
1476 T*VQ& operator=(T*VQ&, T*);
1478 21For every pair T, VQ), where T is a pointer to member type and VQ is
1479 either volatile or empty, there exist candidate operator functions of
1481 VQ T& operator=(VQ T&, T);
1483 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1484 unqualified complete object type, VQ is either volatile or empty, and
1485 I is a promoted integral type, there exist candidate operator func-
1487 T*VQ& operator+=(T*VQ&, I);
1488 T*VQ& operator-=(T*VQ&, I);
1490 23For every triple L, VQ, R), where L is an integral or enumeration
1491 type, VQ is either volatile or empty, and R is a promoted integral
1492 type, there exist candidate operator functions of the form
1494 VQ L& operator%=(VQ L&, R);
1495 VQ L& operator<<=(VQ L&, R);
1496 VQ L& operator>>=(VQ L&, R);
1497 VQ L& operator&=(VQ L&, R);
1498 VQ L& operator^=(VQ L&, R);
1499 VQ L& operator|=(VQ L&, R); */
1506 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1508 type2 = ptrdiff_type_node;
1512 case TRUNC_DIV_EXPR:
1513 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1517 case TRUNC_MOD_EXPR:
1523 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1528 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1530 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1531 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1532 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1533 || ((TYPE_PTRMEMFUNC_P (type1)
1534 || TREE_CODE (type1) == POINTER_TYPE)
1535 && null_ptr_cst_p (args[1])))
1543 my_friendly_abort (367);
1545 type1 = build_reference_type (type1);
1549 /* Kludge around broken overloading rules whereby
1550 bool ? const char& : enum is ambiguous
1551 (between int and const char&). */
1552 flags |= LOOKUP_NO_TEMP_BIND;
1554 /* Extension: Support ?: of enumeral type. Hopefully this will not
1555 be an extension for long. */
1556 if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
1558 else if (TREE_CODE (type1) == ENUMERAL_TYPE
1559 || TREE_CODE (type2) == ENUMERAL_TYPE)
1561 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1563 if (TREE_CODE (type1) == TREE_CODE (type2)
1564 && (TREE_CODE (type1) == REFERENCE_TYPE
1565 || TREE_CODE (type1) == POINTER_TYPE
1566 || TYPE_PTRMEMFUNC_P (type1)
1567 || IS_AGGR_TYPE (type1)))
1569 if (TREE_CODE (type1) == REFERENCE_TYPE
1570 || TREE_CODE (type2) == REFERENCE_TYPE)
1572 if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
1573 && null_ptr_cst_p (args[1]))
1574 || IS_AGGR_TYPE (type1))
1579 if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
1580 && null_ptr_cst_p (args[0]))
1581 || IS_AGGR_TYPE (type2))
1589 my_friendly_abort (367);
1592 /* If we're dealing with two pointer types, we need candidates
1593 for both of them. */
1594 if (type2 && type1 != type2
1595 && TREE_CODE (type1) == TREE_CODE (type2)
1596 && (TREE_CODE (type1) == REFERENCE_TYPE
1597 || (TREE_CODE (type1) == POINTER_TYPE
1598 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1599 || TYPE_PTRMEMFUNC_P (type1)
1600 || IS_AGGR_TYPE (type1)))
1602 candidates = build_builtin_candidate
1603 (candidates, fnname, type1, type1, args, argtypes, flags);
1604 return build_builtin_candidate
1605 (candidates, fnname, type2, type2, args, argtypes, flags);
1608 return build_builtin_candidate
1609 (candidates, fnname, type1, type2, args, argtypes, flags);
1613 type_decays_to (type)
1616 if (TREE_CODE (type) == ARRAY_TYPE)
1617 return build_pointer_type (TREE_TYPE (type));
1618 if (TREE_CODE (type) == FUNCTION_TYPE)
1619 return build_pointer_type (type);
1623 /* There are three conditions of builtin candidates:
1625 1) bool-taking candidates. These are the same regardless of the input.
1626 2) pointer-pair taking candidates. These are generated for each type
1627 one of the input types converts to.
1628 3) arithmetic candidates. According to the WP, we should generate
1629 all of these, but I'm trying not to... */
1631 static struct z_candidate *
1632 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1633 struct z_candidate *candidates;
1634 enum tree_code code, code2;
1639 tree type, argtypes[3], types[2];
1641 for (i = 0; i < 3; ++i)
1644 argtypes[i] = lvalue_type (args[i]);
1646 argtypes[i] = NULL_TREE;
1651 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1652 and VQ is either volatile or empty, there exist candidate operator
1653 functions of the form
1654 VQ T& operator++(VQ T&); */
1656 case POSTINCREMENT_EXPR:
1657 case PREINCREMENT_EXPR:
1658 case POSTDECREMENT_EXPR:
1659 case PREDECREMENT_EXPR:
1664 /* 24There also exist candidate operator functions of the form
1665 bool operator!(bool);
1666 bool operator&&(bool, bool);
1667 bool operator||(bool, bool); */
1669 case TRUTH_NOT_EXPR:
1670 return build_builtin_candidate
1671 (candidates, fnname, boolean_type_node,
1672 NULL_TREE, args, argtypes, flags);
1674 case TRUTH_ORIF_EXPR:
1675 case TRUTH_ANDIF_EXPR:
1676 return build_builtin_candidate
1677 (candidates, fnname, boolean_type_node,
1678 boolean_type_node, args, argtypes, flags);
1689 types[0] = types[1] = NULL_TREE;
1691 for (i = 0; i < 2; ++i)
1695 else if (IS_AGGR_TYPE (argtypes[i]))
1699 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1702 convs = lookup_conversions (argtypes[i]);
1704 if (code == COND_EXPR)
1706 if (real_lvalue_p (args[i]))
1707 types[i] = scratch_tree_cons
1708 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1710 types[i] = scratch_tree_cons
1711 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1717 for (; convs; convs = TREE_CHAIN (convs))
1719 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
1722 && (TREE_CODE (type) != REFERENCE_TYPE
1723 || CP_TYPE_CONST_P (TREE_TYPE (type))))
1726 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1727 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1729 type = non_reference (type);
1730 if (i != 0 || ! ref1)
1732 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1733 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1734 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1735 if (INTEGRAL_TYPE_P (type))
1736 type = type_promotes_to (type);
1739 if (! value_member (type, types[i]))
1740 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1745 if (code == COND_EXPR && real_lvalue_p (args[i]))
1746 types[i] = scratch_tree_cons
1747 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1748 type = non_reference (argtypes[i]);
1749 if (i != 0 || ! ref1)
1751 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1752 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1753 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1754 if (INTEGRAL_TYPE_P (type))
1755 type = type_promotes_to (type);
1757 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1761 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1764 for (type = types[1]; type; type = TREE_CHAIN (type))
1765 candidates = add_builtin_candidate
1766 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1767 TREE_VALUE (type), args, argtypes, flags);
1769 candidates = add_builtin_candidate
1770 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1771 NULL_TREE, args, argtypes, flags);
1778 /* If TMPL can be successfully instantiated as indicated by
1779 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
1781 TMPL is the template. EXPLICIT_TARGS are any explicit template
1782 arguments. ARGLIST is the arguments provided at the call-site.
1783 The RETURN_TYPE is the desired type for conversion operators. If
1784 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
1785 OBJ is supplied, FLAGS are ignored, and OBJ is as for
1786 add_conv_candidate. */
1788 static struct z_candidate*
1789 add_template_candidate_real (candidates, tmpl, explicit_targs,
1790 arglist, return_type, flags,
1792 struct z_candidate *candidates;
1793 tree tmpl, explicit_targs, arglist, return_type;
1796 unification_kind_t strict;
1798 int ntparms = DECL_NTPARMS (tmpl);
1799 tree targs = make_scratch_vec (ntparms);
1800 struct z_candidate *cand;
1804 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
1805 return_type, strict);
1810 fn = instantiate_template (tmpl, targs);
1811 if (fn == error_mark_node)
1814 if (obj != NULL_TREE)
1815 /* Aha, this is a conversion function. */
1816 cand = add_conv_candidate (candidates, fn, obj, arglist);
1818 cand = add_function_candidate (candidates, fn, arglist, flags);
1819 if (DECL_TI_TEMPLATE (fn) != tmpl)
1820 /* This situation can occur if a member template of a template
1821 class is specialized. Then, instantiate_template might return
1822 an instantiation of the specialization, in which case the
1823 DECL_TI_TEMPLATE field will point at the original
1824 specialization. For example:
1826 template <class T> struct S { template <class U> void f(U);
1827 template <> void f(int) {}; };
1831 Here, TMPL will be template <class U> S<double>::f(U).
1832 And, instantiate template will give us the specialization
1833 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
1834 for this will point at template <class T> template <> S<T>::f(int),
1835 so that we can find the definition. For the purposes of
1836 overload resolution, however, we want the original TMPL. */
1837 cand->template = tree_cons (tmpl, targs, NULL_TREE);
1839 cand->template = DECL_TEMPLATE_INFO (fn);
1845 static struct z_candidate *
1846 add_template_candidate (candidates, tmpl, explicit_targs,
1847 arglist, return_type, flags, strict)
1848 struct z_candidate *candidates;
1849 tree tmpl, explicit_targs, arglist, return_type;
1851 unification_kind_t strict;
1854 add_template_candidate_real (candidates, tmpl, explicit_targs,
1855 arglist, return_type, flags,
1860 static struct z_candidate *
1861 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
1862 struct z_candidate *candidates;
1863 tree tmpl, obj, arglist, return_type;
1866 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
1867 return_type, 0, obj, DEDUCE_CONV);
1873 struct z_candidate *cands;
1875 for (; cands; cands = cands->next)
1876 if (pedantic ? cands->viable == 1 : cands->viable)
1881 static struct z_candidate *
1882 splice_viable (cands)
1883 struct z_candidate *cands;
1885 struct z_candidate **p = &cands;
1889 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
1902 /* Fix this to work on non-lvalues. */
1903 if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
1904 || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
1907 return build_unary_op (ADDR_EXPR, obj, 0);
1911 print_z_candidates (candidates)
1912 struct z_candidate *candidates;
1914 const char *str = "candidates are:";
1915 for (; candidates; candidates = candidates->next)
1917 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
1919 if (candidates->fn == ansi_opname [COND_EXPR])
1920 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
1921 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
1922 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
1923 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
1924 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
1925 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
1926 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
1927 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
1929 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
1930 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
1932 else if (TYPE_P (candidates->fn))
1933 cp_error ("%s %T <conversion>", str, candidates->fn);
1935 cp_error_at ("%s %+#D%s", str, candidates->fn,
1936 candidates->viable == -1 ? " <near match>" : "");
1941 /* Returns the best overload candidate to perform the requested
1942 conversion. This function is used for three the overloading situations
1943 described in [over.match.copy], [over.match.conv], and [over.match.ref].
1944 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
1945 per [dcl.init.ref], so we ignore temporary bindings. */
1947 static struct z_candidate *
1948 build_user_type_conversion_1 (totype, expr, flags)
1952 struct z_candidate *candidates, *cand;
1953 tree fromtype = TREE_TYPE (expr);
1954 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
1955 tree args = NULL_TREE;
1956 tree templates = NULL_TREE;
1958 if (IS_AGGR_TYPE (totype))
1959 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
1960 if (IS_AGGR_TYPE (fromtype)
1961 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
1962 convs = lookup_conversions (fromtype);
1965 flags |= LOOKUP_NO_CONVERSION;
1969 tree t = build_int_2 (0, 0);
1970 TREE_TYPE (t) = build_pointer_type (totype);
1971 args = build_scratch_list (NULL_TREE, expr);
1972 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
1973 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
1974 args = scratch_tree_cons (NULL_TREE, t, args);
1976 ctors = TREE_VALUE (ctors);
1978 for (; ctors; ctors = OVL_NEXT (ctors))
1980 tree ctor = OVL_CURRENT (ctors);
1981 if (DECL_NONCONVERTING_P (ctor))
1984 if (TREE_CODE (ctor) == TEMPLATE_DECL)
1986 templates = scratch_tree_cons (NULL_TREE, ctor, templates);
1988 add_template_candidate (candidates, ctor,
1989 NULL_TREE, args, NULL_TREE, flags,
1993 candidates = add_function_candidate (candidates, ctor,
1998 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
1999 candidates->basetype_path = TYPE_BINFO (totype);
2004 args = build_scratch_list (NULL_TREE, build_this (expr));
2006 for (; convs; convs = TREE_CHAIN (convs))
2008 tree fns = TREE_VALUE (convs);
2009 int convflags = LOOKUP_NO_CONVERSION;
2012 /* If we are called to convert to a reference type, we are trying to
2013 find an lvalue binding, so don't even consider temporaries. If
2014 we don't find an lvalue binding, the caller will try again to
2015 look for a temporary binding. */
2016 if (TREE_CODE (totype) == REFERENCE_TYPE)
2017 convflags |= LOOKUP_NO_TEMP_BIND;
2019 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2020 ics = implicit_conversion
2021 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2023 /* We can't compute this yet. */
2024 ics = error_mark_node;
2026 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2027 /* ignore the near match. */;
2029 for (; fns; fns = OVL_NEXT (fns))
2031 tree fn = OVL_CURRENT (fns);
2032 struct z_candidate *old_candidates = candidates;
2034 if (TREE_CODE (fn) == TEMPLATE_DECL)
2036 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2038 add_template_candidate (candidates, fn, NULL_TREE,
2039 args, totype, flags,
2043 candidates = add_function_candidate (candidates, fn,
2046 if (candidates != old_candidates)
2048 if (TREE_CODE (fn) == TEMPLATE_DECL)
2049 ics = implicit_conversion
2050 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2053 candidates->second_conv = ics;
2054 candidates->basetype_path = TREE_PURPOSE (convs);
2056 if (ics == NULL_TREE)
2057 candidates->viable = 0;
2058 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2059 candidates->viable = -1;
2064 if (! any_viable (candidates))
2067 if (flags & LOOKUP_COMPLAIN)
2069 if (candidates && ! candidates->next)
2070 /* say why this one won't work or try to be loose */;
2072 cp_error ("no viable candidates");
2079 candidates = splice_viable (candidates);
2080 cand = tourney (candidates);
2084 if (flags & LOOKUP_COMPLAIN)
2086 cp_error ("conversion from `%T' to `%T' is ambiguous",
2088 print_z_candidates (candidates);
2091 cand = candidates; /* any one will do */
2092 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2093 ICS_USER_FLAG (cand->second_conv) = 1;
2094 ICS_BAD_FLAG (cand->second_conv) = 1;
2099 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2100 p = &(TREE_OPERAND (*p, 0));
2102 /* Pedantically, normal function declarations are never considered
2103 to refer to template instantiations, so we only do this with
2105 if (flag_guiding_decls && templates && ! cand->template
2106 && !DECL_INITIAL (cand->fn)
2107 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2108 add_maybe_template (cand->fn, templates);
2112 (DECL_CONSTRUCTOR_P (cand->fn)
2113 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2114 expr, build_expr_ptr_wrapper (cand));
2115 ICS_USER_FLAG (cand->second_conv) = 1;
2116 if (cand->viable == -1)
2117 ICS_BAD_FLAG (cand->second_conv) = 1;
2123 build_user_type_conversion (totype, expr, flags)
2127 struct z_candidate *cand
2128 = build_user_type_conversion_1 (totype, expr, flags);
2132 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2133 return error_mark_node;
2134 return convert_from_reference (convert_like (cand->second_conv, expr));
2139 /* Do any initial processing on the arguments to a function call. */
2146 for (t = args; t; t = TREE_CHAIN (t))
2148 if (TREE_VALUE (t) == error_mark_node)
2149 return error_mark_node;
2150 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2152 error ("invalid use of void expression");
2153 return error_mark_node;
2155 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2156 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2162 build_new_function_call (fn, args)
2165 struct z_candidate *candidates = 0, *cand;
2166 tree explicit_targs = NULL_TREE;
2167 int template_only = 0;
2169 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2171 explicit_targs = TREE_OPERAND (fn, 1);
2172 fn = TREE_OPERAND (fn, 0);
2176 if (really_overloaded_fn (fn))
2179 tree templates = NULL_TREE;
2181 args = resolve_args (args);
2183 if (args == error_mark_node)
2184 return error_mark_node;
2186 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2188 tree t = OVL_FUNCTION (t1);
2189 struct z_candidate *old_candidates = candidates;
2191 if (TREE_CODE (t) == TEMPLATE_DECL)
2193 templates = scratch_tree_cons (NULL_TREE, t, templates);
2194 candidates = add_template_candidate
2195 (candidates, t, explicit_targs, args, NULL_TREE,
2196 LOOKUP_NORMAL, DEDUCE_CALL);
2198 else if (! template_only)
2199 candidates = add_function_candidate
2200 (candidates, t, args, LOOKUP_NORMAL);
2202 if (candidates != old_candidates)
2203 candidates->basetype_path = DECL_REAL_CONTEXT (t);
2206 if (! any_viable (candidates))
2208 if (candidates && ! candidates->next)
2209 return build_function_call (candidates->fn, args);
2210 cp_error ("no matching function for call to `%D (%A)'",
2211 DECL_NAME (OVL_FUNCTION (fn)), args);
2213 print_z_candidates (candidates);
2214 return error_mark_node;
2216 candidates = splice_viable (candidates);
2217 cand = tourney (candidates);
2221 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2222 DECL_NAME (OVL_FUNCTION (fn)), args);
2223 print_z_candidates (candidates);
2224 return error_mark_node;
2227 /* Pedantically, normal function declarations are never considered
2228 to refer to template instantiations, so we only do this with
2230 if (flag_guiding_decls && templates && ! cand->template
2231 && ! DECL_INITIAL (cand->fn))
2232 add_maybe_template (cand->fn, templates);
2234 return build_over_call (cand, args, LOOKUP_NORMAL);
2237 /* This is not really overloaded. */
2238 fn = OVL_CURRENT (fn);
2240 return build_function_call (fn, args);
2244 build_object_call (obj, args)
2247 struct z_candidate *candidates = 0, *cand;
2248 tree fns, convs, mem_args = NULL_TREE;
2249 tree type = TREE_TYPE (obj);
2251 if (TYPE_PTRMEMFUNC_P (type))
2253 /* It's no good looking for an overloaded operator() on a
2254 pointer-to-member-function. */
2255 cp_error ("pointer-to-member function %E cannot be called", obj);
2256 cp_error ("without an object; consider using .* or ->*");
2257 return error_mark_node;
2260 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2261 if (fns == error_mark_node)
2262 return error_mark_node;
2264 args = resolve_args (args);
2266 if (args == error_mark_node)
2267 return error_mark_node;
2271 tree base = TREE_PURPOSE (fns);
2272 mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2274 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2276 tree fn = OVL_CURRENT (fns);
2277 if (TREE_CODE (fn) == TEMPLATE_DECL)
2280 = add_template_candidate (candidates, fn, NULL_TREE,
2281 mem_args, NULL_TREE,
2282 LOOKUP_NORMAL, DEDUCE_CALL);
2285 candidates = add_function_candidate
2286 (candidates, fn, mem_args, LOOKUP_NORMAL);
2289 candidates->basetype_path = base;
2293 convs = lookup_conversions (type);
2295 for (; convs; convs = TREE_CHAIN (convs))
2297 tree fns = TREE_VALUE (convs);
2298 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2300 if ((TREE_CODE (totype) == POINTER_TYPE
2301 || TREE_CODE (totype) == REFERENCE_TYPE)
2302 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2303 for (; fns; fns = OVL_NEXT (fns))
2305 tree fn = OVL_CURRENT (fns);
2306 if (TREE_CODE (fn) == TEMPLATE_DECL)
2308 candidates = add_template_conv_candidate (candidates,
2315 candidates = add_conv_candidate (candidates, fn, obj, args);
2318 candidates->basetype_path = TREE_PURPOSE (convs);
2322 if (! any_viable (candidates))
2324 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2325 print_z_candidates (candidates);
2326 return error_mark_node;
2329 candidates = splice_viable (candidates);
2330 cand = tourney (candidates);
2334 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2335 print_z_candidates (candidates);
2336 return error_mark_node;
2339 if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2340 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2342 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2345 return build_function_call (obj, args);
2349 op_error (code, code2, arg1, arg2, arg3, problem)
2350 enum tree_code code, code2;
2351 tree arg1, arg2, arg3;
2352 const char *problem;
2355 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2360 cp_error ("%s for `%T ? %T : %T'", problem,
2361 error_type (arg1), error_type (arg2), error_type (arg3));
2363 case POSTINCREMENT_EXPR:
2364 case POSTDECREMENT_EXPR:
2365 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2368 cp_error ("%s for `%T[%T]'", problem,
2369 error_type (arg1), error_type (arg2));
2373 cp_error ("%s for `%T %s %T'", problem,
2374 error_type (arg1), opname, error_type (arg2));
2376 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2381 build_new_op (code, flags, arg1, arg2, arg3)
2382 enum tree_code code;
2384 tree arg1, arg2, arg3;
2386 struct z_candidate *candidates = 0, *cand;
2387 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
2388 enum tree_code code2 = NOP_EXPR;
2389 tree templates = NULL_TREE;
2392 if (arg1 == error_mark_node
2393 || arg2 == error_mark_node
2394 || arg3 == error_mark_node)
2395 return error_mark_node;
2397 /* This can happen if a template takes all non-type parameters, e.g.
2398 undeclared_template<1, 5, 72>a; */
2399 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
2401 cp_error ("`%D' must be declared before use", arg1);
2402 return error_mark_node;
2405 if (code == MODIFY_EXPR)
2407 code2 = TREE_CODE (arg3);
2409 fnname = ansi_assopname[code2];
2412 fnname = ansi_opname[code];
2418 case VEC_DELETE_EXPR:
2420 /* Use build_op_new_call and build_op_delete_call instead. */
2421 my_friendly_abort (981018);
2424 return build_object_call (arg1, arg2);
2430 /* The comma operator can have void args. */
2431 if (TREE_CODE (arg1) == OFFSET_REF)
2432 arg1 = resolve_offset_ref (arg1);
2433 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
2434 arg2 = resolve_offset_ref (arg2);
2435 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
2436 arg3 = resolve_offset_ref (arg3);
2438 if (code == COND_EXPR)
2440 if (arg2 == NULL_TREE
2441 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
2442 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
2443 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
2444 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
2447 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
2448 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
2451 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2452 arg2 = integer_zero_node;
2455 arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
2456 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
2458 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2460 arglist = build_scratch_list (NULL_TREE, arg1);
2462 fns = lookup_function_nonclass (fnname, arglist);
2464 if (fns && TREE_CODE (fns) == TREE_LIST)
2465 fns = TREE_VALUE (fns);
2466 for (; fns; fns = OVL_NEXT (fns))
2468 tree fn = OVL_CURRENT (fns);
2469 if (TREE_CODE (fn) == TEMPLATE_DECL)
2471 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2473 = add_template_candidate (candidates, fn, NULL_TREE,
2474 arglist, TREE_TYPE (fnname),
2475 flags, DEDUCE_CALL);
2478 candidates = add_function_candidate (candidates, fn, arglist, flags);
2481 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
2483 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
2484 if (fns == error_mark_node)
2492 tree basetype = TREE_PURPOSE (fns);
2493 mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
2494 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2496 tree fn = OVL_CURRENT (fns);
2499 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
2500 this_arglist = mem_arglist;
2502 this_arglist = arglist;
2504 if (TREE_CODE (fn) == TEMPLATE_DECL)
2506 /* A member template. */
2507 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2509 = add_template_candidate (candidates, fn, NULL_TREE,
2510 this_arglist, TREE_TYPE (fnname),
2511 flags, DEDUCE_CALL);
2514 candidates = add_function_candidate
2515 (candidates, fn, this_arglist, flags);
2518 candidates->basetype_path = basetype;
2525 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
2526 to know about two args; a builtin candidate will always have a first
2527 parameter of type bool. We'll handle that in
2528 build_builtin_candidate. */
2529 if (code == COND_EXPR)
2539 args[2] = NULL_TREE;
2542 candidates = add_builtin_candidates
2543 (candidates, code, code2, fnname, args, flags);
2546 if (! any_viable (candidates))
2550 case POSTINCREMENT_EXPR:
2551 case POSTDECREMENT_EXPR:
2552 /* Look for an `operator++ (int)'. If they didn't have
2553 one, then we fall back to the old way of doing things. */
2554 if (flags & LOOKUP_COMPLAIN)
2555 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
2556 fnname, opname_tab [code]);
2557 if (code == POSTINCREMENT_EXPR)
2558 code = PREINCREMENT_EXPR;
2560 code = PREDECREMENT_EXPR;
2561 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
2563 /* The caller will deal with these. */
2572 if (flags & LOOKUP_COMPLAIN)
2574 op_error (code, code2, arg1, arg2, arg3, "no match");
2575 print_z_candidates (candidates);
2577 return error_mark_node;
2579 candidates = splice_viable (candidates);
2580 cand = tourney (candidates);
2584 if (flags & LOOKUP_COMPLAIN)
2586 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
2587 print_z_candidates (candidates);
2589 return error_mark_node;
2592 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
2594 extern int warn_synth;
2596 && fnname == ansi_opname[MODIFY_EXPR]
2597 && DECL_ARTIFICIAL (cand->fn)
2599 && ! candidates->next->next)
2601 cp_warning ("using synthesized `%#D' for copy assignment",
2603 cp_warning_at (" where cfront would use `%#D'",
2605 ? candidates->next->fn
2609 /* Pedantically, normal function declarations are never considered
2610 to refer to template instantiations, so we only do this with
2612 if (flag_guiding_decls && templates && ! cand->template
2613 && ! DECL_INITIAL (cand->fn)
2614 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2615 add_maybe_template (cand->fn, templates);
2617 return build_over_call
2619 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
2620 ? mem_arglist : arglist,
2624 /* Check for comparison of different enum types. */
2633 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
2634 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
2635 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
2636 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
2638 cp_warning ("comparison between `%#T' and `%#T'",
2639 TREE_TYPE (arg1), TREE_TYPE (arg2));
2646 /* We need to strip any leading REF_BIND so that bitfields don't cause
2647 errors. This should not remove any important conversions, because
2648 builtins don't apply to class objects directly. */
2649 conv = TREE_VEC_ELT (cand->convs, 0);
2650 if (TREE_CODE (conv) == REF_BIND)
2651 conv = TREE_OPERAND (conv, 0);
2652 arg1 = convert_like (conv, arg1);
2654 arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
2656 arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
2662 return build_modify_expr (arg1, code2, arg2);
2665 return build_indirect_ref (arg1, "unary *");
2670 case TRUNC_DIV_EXPR:
2681 case TRUNC_MOD_EXPR:
2685 case TRUTH_ANDIF_EXPR:
2686 case TRUTH_ORIF_EXPR:
2687 return build_binary_op_nodefault (code, arg1, arg2, code);
2692 case TRUTH_NOT_EXPR:
2693 case PREINCREMENT_EXPR:
2694 case POSTINCREMENT_EXPR:
2695 case PREDECREMENT_EXPR:
2696 case POSTDECREMENT_EXPR:
2699 return build_unary_op (code, arg1, candidates != 0);
2702 return build_array_ref (arg1, arg2);
2705 return build_conditional_expr (arg1, arg2, arg3);
2708 return build_m_component_ref
2709 (build_indirect_ref (arg1, NULL_PTR), arg2);
2711 /* The caller will deal with these. */
2718 my_friendly_abort (367);
2723 /* Build up a call to operator new. This has to be handled differently
2724 from other operators in the way lookup is handled; first members are
2725 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
2726 TYPE is the type to be created. ARGS are any new-placement args.
2727 FLAGS are the usual overloading flags. */
2730 build_op_new_call (code, type, args, flags)
2731 enum tree_code code;
2735 tree fnname = ansi_opname[code];
2737 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
2738 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
2740 return build_method_call (build_dummy_object (type),
2741 fnname, args, NULL_TREE, flags);
2744 return build_new_function_call
2745 (lookup_function_nonclass (fnname, args), args);
2748 /* Build a call to operator delete. This has to be handled very specially,
2749 because the restrictions on what signatures match are different from all
2750 other call instances. For a normal delete, only a delete taking (void *)
2751 or (void *, size_t) is accepted. For a placement delete, only an exact
2752 match with the placement new is accepted.
2754 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
2755 ADDR is the pointer to be deleted. For placement delete, it is also
2756 used to determine what the corresponding new looked like.
2757 SIZE is the size of the memory block to be deleted.
2758 FLAGS are the usual overloading flags.
2759 PLACEMENT is the corresponding placement new call, or 0. */
2762 build_op_delete_call (code, addr, size, flags, placement)
2763 enum tree_code code;
2764 tree addr, size, placement;
2767 tree fn, fns, fnname, fntype, argtypes, args, type;
2769 if (addr == error_mark_node)
2770 return error_mark_node;
2772 type = TREE_TYPE (TREE_TYPE (addr));
2773 fnname = ansi_opname[code];
2775 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
2778 If the result of the lookup is ambiguous or inaccessible, or if
2779 the lookup selects a placement deallocation function, the
2780 program is ill-formed.
2782 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
2784 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
2785 if (fns == error_mark_node)
2786 return error_mark_node;
2791 if (fns == NULL_TREE)
2792 fns = lookup_name_nonclass (fnname);
2796 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
2798 /* Extract the function. */
2799 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
2800 /* Then the second parm type. */
2801 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
2803 /* Also the second argument. */
2804 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
2808 /* First try it without the size argument. */
2809 argtypes = void_list_node;
2813 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
2814 fntype = build_function_type (void_type_node, argtypes);
2816 /* Strip const and volatile from addr. */
2817 if (type != TYPE_MAIN_VARIANT (type))
2818 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
2820 fn = instantiate_type (fntype, fns, 2);
2822 if (fn != error_mark_node)
2824 if (TREE_CODE (fns) == TREE_LIST)
2825 /* Member functions. */
2826 enforce_access (TREE_PURPOSE (fns), fn);
2827 return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
2830 /* If we are doing placement delete we do nothing if we don't find a
2831 matching op delete. */
2835 /* Normal delete; now try to find a match including the size argument. */
2836 argtypes = tree_cons (NULL_TREE, ptr_type_node,
2837 tree_cons (NULL_TREE, sizetype, void_list_node));
2838 fntype = build_function_type (void_type_node, argtypes);
2840 fn = instantiate_type (fntype, fns, 2);
2842 if (fn != error_mark_node)
2844 if (BASELINK_P (fns))
2845 /* Member functions. */
2846 enforce_access (TREE_PURPOSE (fns), fn);
2847 return build_function_call
2848 (fn, expr_tree_cons (NULL_TREE, addr,
2849 build_expr_list (NULL_TREE, size)));
2852 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
2853 destructor, in which case the error should be deferred
2854 until someone actually tries to delete one of these. */
2855 if (flags & LOOKUP_SPECULATIVELY)
2858 cp_error ("no suitable operator delete for `%T'", type);
2859 return error_mark_node;
2862 /* If the current scope isn't allowed to access DECL along
2863 BASETYPE_PATH, give an error. The most derived class in
2864 BASETYPE_PATH is the one used to qualify DECL. */
2867 enforce_access (basetype_path, decl)
2873 accessible = accessible_p (basetype_path, decl);
2876 if (TREE_PRIVATE (decl))
2877 cp_error_at ("`%+#D' is private", decl);
2878 else if (TREE_PROTECTED (decl))
2879 cp_error_at ("`%+#D' is protected", decl);
2881 cp_error_at ("`%+#D' is inaccessible", decl);
2882 cp_error ("within this context");
2889 /* Perform the conversions in CONVS on the expression EXPR. */
2892 convert_like (convs, expr)
2895 if (ICS_BAD_FLAG (convs)
2896 && TREE_CODE (convs) != USER_CONV
2897 && TREE_CODE (convs) != AMBIG_CONV)
2900 for (; t; t = TREE_OPERAND (t, 0))
2902 if (TREE_CODE (t) == USER_CONV)
2904 expr = convert_like (t, expr);
2907 else if (TREE_CODE (t) == AMBIG_CONV)
2908 return convert_like (t, expr);
2909 else if (TREE_CODE (t) == IDENTITY_CONV)
2912 return convert_for_initialization
2913 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
2914 "conversion", NULL_TREE, 0);
2917 switch (TREE_CODE (convs))
2921 struct z_candidate *cand
2922 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
2926 if (DECL_CONSTRUCTOR_P (fn))
2928 tree t = build_int_2 (0, 0);
2929 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
2931 args = build_scratch_list (NULL_TREE, expr);
2932 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
2933 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2934 args = scratch_tree_cons (NULL_TREE, t, args);
2937 args = build_this (expr);
2938 expr = build_over_call (cand, args, LOOKUP_NORMAL);
2940 /* If this is a constructor or a function returning an aggr type,
2941 we need to build up a TARGET_EXPR. */
2942 if (DECL_CONSTRUCTOR_P (fn))
2943 expr = build_cplus_new (TREE_TYPE (convs), expr);
2948 if (type_unknown_p (expr))
2949 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
2950 if (TREE_READONLY_DECL_P (expr))
2951 expr = decl_constant_value (expr);
2954 /* Call build_user_type_conversion again for the error. */
2955 return build_user_type_conversion
2956 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
2962 expr = convert_like (TREE_OPERAND (convs, 0), expr);
2963 if (expr == error_mark_node)
2964 return error_mark_node;
2966 switch (TREE_CODE (convs))
2969 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
2971 /* else fall through */
2974 tree cvt_expr = build_user_type_conversion
2975 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
2978 /* This can occur if, for example, the EXPR has incomplete
2979 type. We can't check for that before attempting the
2980 conversion because the type might be an incomplete
2981 array type, which is OK if some constructor for the
2982 destination type takes a pointer argument. */
2983 if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
2985 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
2986 incomplete_type_error (expr, TREE_TYPE (expr));
2988 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
2989 expr, TREE_TYPE (expr), TREE_TYPE (convs));
2992 cp_error ("could not convert `%E' to `%T'",
2993 expr, TREE_TYPE (convs));
2994 return error_mark_node;
3000 return convert_to_reference
3001 (TREE_TYPE (convs), expr,
3002 CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
3005 return decay_conversion (expr);
3008 /* Warn about deprecated conversion if appropriate. */
3009 string_conv_p (TREE_TYPE (convs), expr, 1);
3015 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3016 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3019 /* ARG is being passed to a varargs function. Perform any conversions
3020 required. Return the converted value. */
3023 convert_arg_to_ellipsis (arg)
3026 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3027 && (TYPE_PRECISION (TREE_TYPE (arg))
3028 < TYPE_PRECISION (double_type_node)))
3029 /* Convert `float' to `double'. */
3030 arg = cp_convert (double_type_node, arg);
3031 else if (IS_AGGR_TYPE (TREE_TYPE (arg))
3032 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (arg)))
3033 cp_warning ("cannot pass objects of type `%T' through `...'",
3036 /* Convert `short' and `char' to full-size `int'. */
3037 arg = default_conversion (arg);
3039 arg = require_complete_type (arg);
3044 /* ARG is a default argument expression being passed to a parameter of
3045 the indicated TYPE, which is a parameter to FN. Do any required
3046 conversions. Return the converted value. */
3049 convert_default_arg (type, arg, fn)
3054 if (fn && DECL_TEMPLATE_INFO (fn))
3056 /* This default argument came from a template. Instantiate the
3057 default argument here, not in tsubst. In the case of
3066 we must be careful to do name lookup in the scope of S<T>,
3067 rather than in the current class. */
3068 if (DECL_CLASS_SCOPE_P (fn))
3069 pushclass (DECL_REAL_CONTEXT (fn), 2);
3071 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), /*complain=*/1, NULL_TREE);
3073 if (DECL_CLASS_SCOPE_P (fn))
3076 /* Make sure the default argument is reasonable. */
3077 arg = check_default_argument (type, arg);
3080 arg = break_out_target_exprs (arg);
3082 if (TREE_CODE (arg) == CONSTRUCTOR)
3084 arg = digest_init (type, arg, 0);
3085 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3086 "default argument", 0, 0);
3090 /* This could get clobbered by the following call. */
3091 if (TREE_HAS_CONSTRUCTOR (arg))
3092 arg = copy_node (arg);
3094 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3095 "default argument", 0, 0);
3096 #ifdef PROMOTE_PROTOTYPES
3097 if ((TREE_CODE (type) == INTEGER_TYPE
3098 || TREE_CODE (type) == ENUMERAL_TYPE)
3099 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3100 arg = default_conversion (arg);
3108 build_over_call (cand, args, flags)
3109 struct z_candidate *cand;
3114 tree convs = cand->convs;
3115 tree converted_args = NULL_TREE;
3116 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3117 tree conv, arg, val;
3121 /* Give any warnings we noticed during overload resolution. */
3123 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3124 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3126 if (DECL_FUNCTION_MEMBER_P (fn))
3127 enforce_access (cand->basetype_path, fn);
3129 if (args && TREE_CODE (args) != TREE_LIST)
3130 args = build_scratch_list (NULL_TREE, args);
3133 /* The implicit parameters to a constructor are not considered by overload
3134 resolution, and must be of the proper type. */
3135 if (DECL_CONSTRUCTOR_P (fn))
3137 converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3138 arg = TREE_CHAIN (arg);
3139 parm = TREE_CHAIN (parm);
3140 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3142 converted_args = expr_tree_cons
3143 (NULL_TREE, TREE_VALUE (arg), converted_args);
3144 arg = TREE_CHAIN (arg);
3145 parm = TREE_CHAIN (parm);
3148 /* Bypass access control for 'this' parameter. */
3149 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3151 tree parmtype = TREE_VALUE (parm);
3152 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3154 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3155 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3156 TREE_TYPE (argtype), fn);
3158 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3159 X is called for an object that is not of type X, or of a type
3160 derived from X, the behavior is undefined.
3162 So we can assume that anything passed as 'this' is non-null, and
3163 optimize accordingly. */
3164 if (TREE_CODE (parmtype) == POINTER_TYPE)
3165 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3167 /* This happens with signatures. */
3168 t = convert_force (parmtype, TREE_VALUE (arg), CONV_C_CAST);
3169 converted_args = expr_tree_cons (NULL_TREE, t, converted_args);
3170 parm = TREE_CHAIN (parm);
3171 arg = TREE_CHAIN (arg);
3177 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3179 tree type = TREE_VALUE (parm);
3181 conv = TREE_VEC_ELT (convs, i);
3182 if (ICS_BAD_FLAG (conv))
3185 val = TREE_VALUE (arg);
3187 for (; t; t = TREE_OPERAND (t, 0))
3189 if (TREE_CODE (t) == USER_CONV
3190 || TREE_CODE (t) == AMBIG_CONV)
3192 val = convert_like (t, val);
3195 else if (TREE_CODE (t) == IDENTITY_CONV)
3198 val = convert_for_initialization
3199 (NULL_TREE, type, val, LOOKUP_NORMAL,
3200 "argument passing", fn, i - is_method);
3204 /* Issue warnings about peculiar, but legal, uses of NULL. */
3205 if (ARITHMETIC_TYPE_P (TREE_VALUE (parm))
3206 && TREE_VALUE (arg) == null_node)
3207 cp_warning ("converting NULL to non-pointer type");
3209 val = convert_like (conv, TREE_VALUE (arg));
3212 #ifdef PROMOTE_PROTOTYPES
3213 if ((TREE_CODE (type) == INTEGER_TYPE
3214 || TREE_CODE (type) == ENUMERAL_TYPE)
3215 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3216 val = default_conversion (val);
3218 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3221 /* Default arguments */
3222 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3224 = expr_tree_cons (NULL_TREE,
3225 convert_default_arg (TREE_VALUE (parm),
3226 TREE_PURPOSE (parm),
3231 for (; arg; arg = TREE_CHAIN (arg))
3233 = expr_tree_cons (NULL_TREE,
3234 convert_arg_to_ellipsis (TREE_VALUE (arg)),
3237 converted_args = nreverse (converted_args);
3239 if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
3240 check_function_format (DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
3243 /* Avoid actually calling copy constructors and copy assignment operators,
3246 if (! flag_elide_constructors)
3247 /* Do things the hard way. */;
3248 else if (DECL_CONSTRUCTOR_P (fn)
3249 && TREE_VEC_LENGTH (convs) == 1
3250 && copy_args_p (fn))
3253 arg = TREE_CHAIN (converted_args);
3254 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3255 arg = TREE_CHAIN (arg);
3256 arg = TREE_VALUE (arg);
3258 /* Pull out the real argument, disregarding const-correctness. */
3260 while (TREE_CODE (targ) == NOP_EXPR
3261 || TREE_CODE (targ) == NON_LVALUE_EXPR
3262 || TREE_CODE (targ) == CONVERT_EXPR)
3263 targ = TREE_OPERAND (targ, 0);
3264 if (TREE_CODE (targ) == ADDR_EXPR)
3266 targ = TREE_OPERAND (targ, 0);
3267 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3268 TYPE_MAIN_VARIANT (TREE_TYPE (targ))))
3277 arg = build_indirect_ref (arg, 0);
3279 /* [class.copy]: the copy constructor is implicitly defined even if
3280 the implementation elided its use. */
3281 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3284 /* If we're creating a temp and we already have one, don't create a
3285 new one. If we're not creating a temp but we get one, use
3286 INIT_EXPR to collapse the temp into our target. Otherwise, if the
3287 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3288 temp or an INIT_EXPR otherwise. */
3289 if (integer_zerop (TREE_VALUE (args)))
3291 if (! real_lvalue_p (arg))
3293 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3295 val = build_decl (VAR_DECL, NULL_TREE, DECL_CONTEXT (fn));
3296 val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
3297 TREE_SIDE_EFFECTS (val) = 1;
3301 else if (! real_lvalue_p (arg)
3302 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3305 tree to = stabilize_reference
3306 (build_indirect_ref (TREE_VALUE (args), 0));
3308 /* If we're initializing an empty class, then we actually
3309 have to use a MODIFY_EXPR rather than an INIT_EXPR. The
3310 reason is that the dummy padding member in the target may
3311 not actually be allocated if TO is a base class
3312 subobject. Since we've set TYPE_NONCOPIED_PARTS on the
3313 padding, a MODIFY_EXPR will preserve its value, which is
3314 the right thing to do if it's not really padding at all.
3316 It's not safe to just throw away the ARG if we're looking
3317 at an empty class because the ARG might contain a
3318 TARGET_EXPR which wants to be bound to TO. If it is not,
3319 expand_expr will assign a dummy slot for the TARGET_EXPR,
3320 and we will call a destructor for it, which is wrong,
3321 because we will also destroy TO, but will never have
3323 val = build (is_empty_class (DECL_CLASS_CONTEXT (fn))
3324 ? MODIFY_EXPR : INIT_EXPR,
3325 DECL_CONTEXT (fn), to, arg);
3326 TREE_SIDE_EFFECTS (val) = 1;
3327 address = build_unary_op (ADDR_EXPR, val, 0);
3328 /* Avoid a warning about this expression, if the address is
3330 TREE_USED (address) = 1;
3334 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
3336 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
3338 tree to = stabilize_reference
3339 (build_indirect_ref (TREE_VALUE (converted_args), 0));
3341 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
3343 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
3344 TREE_SIDE_EFFECTS (val) = 1;
3350 if (DECL_CLASS_SCOPE_P (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
3351 return build_signature_method_call (fn, converted_args);
3352 else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3354 tree t, *p = &TREE_VALUE (converted_args);
3355 tree binfo = get_binfo
3356 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
3357 *p = convert_pointer_to_real (binfo, *p);
3358 if (TREE_SIDE_EFFECTS (*p))
3359 *p = save_expr (*p);
3360 t = build_pointer_type (TREE_TYPE (fn));
3361 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
3364 else if (DECL_INLINE (fn))
3365 fn = inline_conversion (fn);
3367 fn = build_addr_func (fn);
3369 /* Recognize certain built-in functions so we can make tree-codes
3370 other than CALL_EXPR. We do this when it enables fold-const.c
3371 to do something useful. */
3373 if (TREE_CODE (fn) == ADDR_EXPR
3374 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3375 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
3376 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
3381 if (converted_args == 0)
3382 return integer_zero_node;
3383 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
3388 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3389 if (TREE_CODE (TREE_TYPE (fn)) == VOID_TYPE)
3391 fn = require_complete_type (fn);
3392 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
3393 fn = build_cplus_new (TREE_TYPE (fn), fn);
3394 return convert_from_reference (fn);
3398 build_new_method_call (instance, name, args, basetype_path, flags)
3399 tree instance, name, args, basetype_path;
3402 struct z_candidate *candidates = 0, *cand;
3403 tree explicit_targs = NULL_TREE;
3404 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
3406 tree user_args = args;
3407 tree templates = NULL_TREE;
3408 int template_only = 0;
3410 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3412 explicit_targs = TREE_OPERAND (name, 1);
3413 name = TREE_OPERAND (name, 0);
3414 if (TREE_CODE_CLASS (TREE_CODE (name)) == 'd')
3415 name = DECL_NAME (name);
3418 if (TREE_CODE (name) == COMPONENT_REF)
3419 name = TREE_OPERAND (name, 1);
3420 if (TREE_CODE (name) == OVERLOAD)
3421 name = DECL_NAME (OVL_CURRENT (name));
3427 /* If there is an extra argument for controlling virtual bases,
3428 remove it for error reporting. */
3429 if (flags & LOOKUP_HAS_IN_CHARGE)
3430 user_args = TREE_CHAIN (args);
3432 args = resolve_args (args);
3434 if (args == error_mark_node)
3435 return error_mark_node;
3437 if (instance == NULL_TREE)
3438 basetype = BINFO_TYPE (basetype_path);
3441 if (TREE_CODE (instance) == OFFSET_REF)
3442 instance = resolve_offset_ref (instance);
3443 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3444 instance = convert_from_reference (instance);
3445 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
3447 /* XXX this should be handled before we get here. */
3448 if (! IS_AGGR_TYPE (basetype)
3449 && ! (TYPE_LANG_SPECIFIC (basetype)
3450 && (IS_SIGNATURE_POINTER (basetype)
3451 || IS_SIGNATURE_REFERENCE (basetype))))
3453 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
3454 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
3455 name, instance, basetype);
3457 return error_mark_node;
3460 /* If `instance' is a signature pointer/reference and `name' is
3461 not a constructor, we are calling a signature member function.
3462 In that case set the `basetype' to the signature type. */
3463 if ((IS_SIGNATURE_POINTER (basetype)
3464 || IS_SIGNATURE_REFERENCE (basetype))
3465 && TYPE_IDENTIFIER (basetype) != name)
3466 basetype = SIGNATURE_TYPE (basetype);
3469 if (basetype_path == NULL_TREE)
3470 basetype_path = TYPE_BINFO (basetype);
3474 instance_ptr = build_this (instance);
3476 if (! template_only)
3478 /* XXX this should be handled before we get here. */
3479 fns = build_field_call (basetype_path, instance_ptr, name, args);
3486 instance_ptr = build_int_2 (0, 0);
3487 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
3491 = (name == ctor_identifier ? constructor_name (basetype) : name);
3493 fns = lookup_fnfields (basetype_path, name, 1);
3495 if (fns == error_mark_node)
3496 return error_mark_node;
3499 tree fn = TREE_VALUE (fns);
3500 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3501 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3503 flags |= LOOKUP_HAS_IN_CHARGE;
3504 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3506 mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
3507 for (; fn; fn = OVL_NEXT (fn))
3509 tree t = OVL_CURRENT (fn);
3512 /* We can end up here for copy-init of same or base class. */
3513 if (name == ctor_identifier
3514 && (flags & LOOKUP_ONLYCONVERTING)
3515 && DECL_NONCONVERTING_P (t))
3517 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
3518 this_arglist = mem_args;
3520 this_arglist = args;
3522 if (TREE_CODE (t) == TEMPLATE_DECL)
3524 /* A member template. */
3525 templates = scratch_tree_cons (NULL_TREE, t, templates);
3527 add_template_candidate (candidates, t, explicit_targs,
3529 TREE_TYPE (name), flags, DEDUCE_CALL);
3531 else if (! template_only)
3532 candidates = add_function_candidate (candidates, t,
3533 this_arglist, flags);
3536 candidates->basetype_path = TREE_PURPOSE (fns);
3540 if (! any_viable (candidates))
3542 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
3543 if (flags & LOOKUP_SPECULATIVELY)
3545 if (TYPE_SIZE (basetype) == 0)
3546 incomplete_type_error (instance_ptr, basetype);
3548 cp_error ("no matching function for call to `%T::%D (%A)%V'",
3549 basetype, pretty_name, user_args,
3550 TREE_TYPE (TREE_TYPE (instance_ptr)));
3551 print_z_candidates (candidates);
3552 return error_mark_node;
3554 candidates = splice_viable (candidates);
3555 cand = tourney (candidates);
3559 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
3561 print_z_candidates (candidates);
3562 return error_mark_node;
3565 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
3566 && instance == current_class_ref
3567 && DECL_CONSTRUCTOR_P (current_function_decl)
3568 && ! (flags & LOOKUP_NONVIRTUAL)
3569 && value_member (cand->fn, CLASSTYPE_ABSTRACT_VIRTUALS (basetype)))
3570 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
3571 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3572 && is_dummy_object (instance_ptr))
3574 cp_error ("cannot call member function `%D' without object", cand->fn);
3575 return error_mark_node;
3578 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
3579 && ((instance == current_class_ref && (dtor_label || ctor_label))
3580 || resolves_to_fixed_type_p (instance, 0)))
3581 flags |= LOOKUP_NONVIRTUAL;
3583 /* Pedantically, normal function declarations are never considered
3584 to refer to template instantiations, so we only do this with
3586 if (flag_guiding_decls && templates && ! cand->template
3587 && ! DECL_INITIAL (cand->fn))
3588 add_maybe_template (cand->fn, templates);
3590 return build_over_call
3592 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
3596 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
3597 subsequence of ICS2. */
3600 is_subseq (ics1, ics2)
3603 /* We can assume that a conversion of the same code
3604 between the same types indicates a subsequence since we only get
3605 here if the types we are converting from are the same. */
3607 while (TREE_CODE (ics1) == RVALUE_CONV
3608 || TREE_CODE (ics1) == LVALUE_CONV)
3609 ics1 = TREE_OPERAND (ics1, 0);
3613 while (TREE_CODE (ics2) == RVALUE_CONV
3614 || TREE_CODE (ics2) == LVALUE_CONV)
3615 ics2 = TREE_OPERAND (ics2, 0);
3617 if (TREE_CODE (ics2) == USER_CONV
3618 || TREE_CODE (ics2) == AMBIG_CONV
3619 || TREE_CODE (ics2) == IDENTITY_CONV)
3620 /* At this point, ICS1 cannot be a proper subsequence of
3621 ICS2. We can get a USER_CONV when we are comparing the
3622 second standard conversion sequence of two user conversion
3626 ics2 = TREE_OPERAND (ics2, 0);
3628 if (TREE_CODE (ics2) == TREE_CODE (ics1)
3629 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
3630 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
3631 TREE_TYPE (TREE_OPERAND (ics1, 0))))
3636 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
3637 be any _TYPE nodes. */
3640 is_properly_derived_from (derived, base)
3644 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
3645 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
3648 /* We only allow proper derivation here. The DERIVED_FROM_P macro
3649 considers every class derived from itself. */
3650 return (!same_type_p (TYPE_MAIN_VARIANT (derived),
3651 TYPE_MAIN_VARIANT (base))
3652 && DERIVED_FROM_P (base, derived));
3655 /* We build the ICS for an implicit object parameter as a pointer
3656 conversion sequence. However, such a sequence should be compared
3657 as if it were a reference conversion sequence. If ICS is the
3658 implicit conversion sequence for an implicit object parameter,
3659 modify it accordingly. */
3662 maybe_handle_implicit_object (ics)
3665 if (ICS_THIS_FLAG (*ics))
3667 /* [over.match.funcs]
3669 For non-static member functions, the type of the
3670 implicit object parameter is "reference to cv X"
3671 where X is the class of which the function is a
3672 member and cv is the cv-qualification on the member
3673 function declaration. */
3675 if (TREE_CODE (t) == QUAL_CONV)
3676 t = TREE_OPERAND (t, 0);
3677 if (TREE_CODE (t) == PTR_CONV)
3678 t = TREE_OPERAND (t, 0);
3679 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3680 t = build_conv (REF_BIND,
3681 build_reference_type (TREE_TYPE (TREE_TYPE (*ics))),
3683 ICS_STD_RANK (t) = ICS_STD_RANK (*ics);
3688 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
3689 to the type the reference originally referred to, and return 1.
3690 Otherwise, return 0. */
3693 maybe_handle_ref_bind (ics, target_type)
3697 if (TREE_CODE (*ics) == REF_BIND)
3701 When a parameter of reference type binds directly
3702 (_dcl.init.ref_) to an argument expression, the implicit
3703 conversion sequence is the identity conversion, unless the
3704 argument expression has a type that is a derived class of the
3705 parameter type, in which case the implicit conversion
3706 sequence is a derived-to-base Conversion.
3708 If the parameter binds directly to the result of applying a
3709 conversion function to the argument expression, the implicit
3710 conversion sequence is a user-defined conversion sequence
3711 (_over.ics.user_), with the second standard conversion
3712 sequence either an identity conversion or, if the conversion
3713 function returns an entity of a type that is a derived class
3714 of the parameter type, a derived-to-base Conversion.
3716 When a parameter of reference type is not bound directly to
3717 an argument expression, the conversion sequence is the one
3718 required to convert the argument expression to the underlying
3719 type of the reference according to _over.best.ics_.
3720 Conceptually, this conversion sequence corresponds to
3721 copy-initializing a temporary of the underlying type with the
3722 argument expression. Any difference in top-level
3723 cv-qualification is subsumed by the initialization itself and
3724 does not constitute a conversion. */
3726 tree old_ics = *ics;
3728 *target_type = TREE_TYPE (TREE_TYPE (*ics));
3729 *ics = TREE_OPERAND (*ics, 0);
3730 if (TREE_CODE (*ics) == IDENTITY_CONV
3731 && is_properly_derived_from (TREE_TYPE (*ics), *target_type))
3732 *ics = build_conv (BASE_CONV, *target_type, *ics);
3733 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
3734 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
3742 /* Compare two implicit conversion sequences according to the rules set out in
3743 [over.ics.rank]. Return values:
3745 1: ics1 is better than ics2
3746 -1: ics2 is better than ics1
3747 0: ics1 and ics2 are indistinguishable */
3750 compare_ics (ics1, ics2)
3757 tree deref_from_type1 = NULL_TREE;
3758 tree deref_from_type2 = NULL_TREE;
3759 tree deref_to_type1 = NULL_TREE;
3760 tree deref_to_type2 = NULL_TREE;
3762 /* REF_BINDING is non-zero if the result of the conversion sequence
3763 is a reference type. In that case TARGET_TYPE is the
3764 type referred to by the reference. */
3770 /* Handle implicit object parameters. */
3771 maybe_handle_implicit_object (&ics1);
3772 maybe_handle_implicit_object (&ics2);
3774 /* Handle reference parameters. */
3775 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
3776 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
3780 When comparing the basic forms of implicit conversion sequences (as
3781 defined in _over.best.ics_)
3783 --a standard conversion sequence (_over.ics.scs_) is a better
3784 conversion sequence than a user-defined conversion sequence
3785 or an ellipsis conversion sequence, and
3787 --a user-defined conversion sequence (_over.ics.user_) is a
3788 better conversion sequence than an ellipsis conversion sequence
3789 (_over.ics.ellipsis_). */
3790 if (ICS_RANK (ics1) > ICS_RANK (ics2))
3792 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
3795 if (ICS_RANK (ics1) == BAD_RANK)
3797 /* Both ICS are bad. We try to make a decision based on what
3798 would have happenned if they'd been good. */
3799 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
3800 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3802 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
3803 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3806 /* We couldn't make up our minds; try to figure it out below. */
3809 if (ICS_ELLIPSIS_FLAG (ics1))
3810 /* Both conversions are ellipsis conversions. */
3813 /* User-defined conversion sequence U1 is a better conversion sequence
3814 than another user-defined conversion sequence U2 if they contain the
3815 same user-defined conversion operator or constructor and if the sec-
3816 ond standard conversion sequence of U1 is better than the second
3817 standard conversion sequence of U2. */
3819 if (ICS_USER_FLAG (ics1))
3823 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
3824 if (TREE_CODE (t1) == AMBIG_CONV)
3826 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
3827 if (TREE_CODE (t2) == AMBIG_CONV)
3830 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
3833 /* We can just fall through here, after setting up
3834 FROM_TYPE1 and FROM_TYPE2. */
3835 from_type1 = TREE_TYPE (t1);
3836 from_type2 = TREE_TYPE (t2);
3840 /* We're dealing with two standard conversion sequences.
3844 Standard conversion sequence S1 is a better conversion
3845 sequence than standard conversion sequence S2 if
3847 --S1 is a proper subsequence of S2 (comparing the conversion
3848 sequences in the canonical form defined by _over.ics.scs_,
3849 excluding any Lvalue Transformation; the identity
3850 conversion sequence is considered to be a subsequence of
3851 any non-identity conversion sequence */
3854 while (TREE_CODE (from_type1) != IDENTITY_CONV)
3855 from_type1 = TREE_OPERAND (from_type1, 0);
3856 from_type1 = TREE_TYPE (from_type1);
3859 while (TREE_CODE (from_type2) != IDENTITY_CONV)
3860 from_type2 = TREE_OPERAND (from_type2, 0);
3861 from_type2 = TREE_TYPE (from_type2);
3864 if (same_type_p (from_type1, from_type2))
3866 if (is_subseq (ics1, ics2))
3868 if (is_subseq (ics2, ics1))
3871 /* Otherwise, one sequence cannot be a subsequence of the other; they
3872 don't start with the same type. This can happen when comparing the
3873 second standard conversion sequence in two user-defined conversion
3880 --the rank of S1 is better than the rank of S2 (by the rules
3883 Standard conversion sequences are ordered by their ranks: an Exact
3884 Match is a better conversion than a Promotion, which is a better
3885 conversion than a Conversion.
3887 Two conversion sequences with the same rank are indistinguishable
3888 unless one of the following rules applies:
3890 --A conversion that is not a conversion of a pointer, or pointer
3891 to member, to bool is better than another conversion that is such
3894 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
3895 so that we do not have to check it explicitly. */
3896 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3898 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
3901 to_type1 = TREE_TYPE (ics1);
3902 to_type2 = TREE_TYPE (ics2);
3904 if (TYPE_PTR_P (from_type1)
3905 && TYPE_PTR_P (from_type2)
3906 && TYPE_PTR_P (to_type1)
3907 && TYPE_PTR_P (to_type2))
3909 deref_from_type1 = TREE_TYPE (from_type1);
3910 deref_from_type2 = TREE_TYPE (from_type2);
3911 deref_to_type1 = TREE_TYPE (to_type1);
3912 deref_to_type2 = TREE_TYPE (to_type2);
3914 /* The rules for pointers to members A::* are just like the rules
3915 for pointers A*, except opposite: if B is derived from A then
3916 A::* converts to B::*, not vice versa. For that reason, we
3917 switch the from_ and to_ variables here. */
3918 else if (TYPE_PTRMEM_P (from_type1)
3919 && TYPE_PTRMEM_P (from_type2)
3920 && TYPE_PTRMEM_P (to_type1)
3921 && TYPE_PTRMEM_P (to_type2))
3923 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
3924 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
3925 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
3926 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
3928 else if (TYPE_PTRMEMFUNC_P (from_type1)
3929 && TYPE_PTRMEMFUNC_P (from_type2)
3930 && TYPE_PTRMEMFUNC_P (to_type1)
3931 && TYPE_PTRMEMFUNC_P (to_type2))
3933 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
3934 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
3935 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
3936 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
3939 if (deref_from_type1 != NULL_TREE
3940 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
3941 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
3943 /* This was one of the pointer or pointer-like conversions.
3947 --If class B is derived directly or indirectly from class A,
3948 conversion of B* to A* is better than conversion of B* to
3949 void*, and conversion of A* to void* is better than
3950 conversion of B* to void*. */
3951 if (TREE_CODE (deref_to_type1) == VOID_TYPE
3952 && TREE_CODE (deref_to_type2) == VOID_TYPE)
3954 if (is_properly_derived_from (deref_from_type1,
3957 else if (is_properly_derived_from (deref_from_type2,
3961 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
3962 || TREE_CODE (deref_to_type2) == VOID_TYPE)
3964 if (same_type_p (deref_from_type1, deref_from_type2))
3966 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
3968 if (is_properly_derived_from (deref_from_type1,
3972 /* We know that DEREF_TO_TYPE1 is `void' here. */
3973 else if (is_properly_derived_from (deref_from_type1,
3978 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
3979 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
3983 --If class B is derived directly or indirectly from class A
3984 and class C is derived directly or indirectly from B,
3986 --conversion of C* to B* is better than conversion of C* to
3989 --conversion of B* to A* is better than conversion of C* to
3991 if (same_type_p (deref_from_type1, deref_from_type2))
3993 if (is_properly_derived_from (deref_to_type1,
3996 else if (is_properly_derived_from (deref_to_type2,
4000 else if (same_type_p (deref_to_type1, deref_to_type2))
4002 if (is_properly_derived_from (deref_from_type2,
4005 else if (is_properly_derived_from (deref_from_type1,
4011 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4012 && same_type_p (from_type1, from_type2))
4016 --binding of an expression of type C to a reference of type
4017 B& is better than binding an expression of type C to a
4018 reference of type A&
4020 --conversion of C to B is better than conversion of C to A, */
4021 if (is_properly_derived_from (from_type1, to_type1)
4022 && is_properly_derived_from (from_type1, to_type2))
4024 if (is_properly_derived_from (to_type1, to_type2))
4026 else if (is_properly_derived_from (to_type2, to_type1))
4030 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4031 && same_type_p (to_type1, to_type2))
4035 --binding of an expression of type B to a reference of type
4036 A& is better than binding an expression of type C to a
4037 reference of type A&,
4039 --onversion of B to A is better than conversion of C to A */
4040 if (is_properly_derived_from (from_type1, to_type1)
4041 && is_properly_derived_from (from_type2, to_type1))
4043 if (is_properly_derived_from (from_type2, from_type1))
4045 else if (is_properly_derived_from (from_type1, from_type2))
4052 --S1 and S2 differ only in their qualification conversion and yield
4053 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4054 qualification signature of type T1 is a proper subset of the cv-
4055 qualification signature of type T2 */
4056 if (TREE_CODE (ics1) == QUAL_CONV
4057 && TREE_CODE (ics2) == QUAL_CONV
4058 && same_type_p (from_type1, from_type2))
4059 return comp_cv_qual_signature (to_type1, to_type2);
4063 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4064 types to which the references refer are the same type except for
4065 top-level cv-qualifiers, and the type to which the reference
4066 initialized by S2 refers is more cv-qualified than the type to
4067 which the reference initialized by S1 refers */
4069 if (ref_binding1 && ref_binding2
4070 && same_type_p (TYPE_MAIN_VARIANT (to_type1),
4071 TYPE_MAIN_VARIANT (to_type2)))
4072 return comp_cv_qualification (target_type2, target_type1);
4074 /* Neither conversion sequence is better than the other. */
4078 /* The source type for this standard conversion sequence. */
4084 for (;; t = TREE_OPERAND (t, 0))
4086 if (TREE_CODE (t) == USER_CONV
4087 || TREE_CODE (t) == AMBIG_CONV
4088 || TREE_CODE (t) == IDENTITY_CONV)
4089 return TREE_TYPE (t);
4091 my_friendly_abort (1823);
4094 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4095 a pointer to LOSER and re-running joust to produce the warning if WINNER
4096 is actually used. */
4099 add_warning (winner, loser)
4100 struct z_candidate *winner, *loser;
4102 winner->warnings = expr_tree_cons (NULL_PTR,
4103 build_expr_ptr_wrapper (loser),
4107 /* Compare two candidates for overloading as described in
4108 [over.match.best]. Return values:
4110 1: cand1 is better than cand2
4111 -1: cand2 is better than cand1
4112 0: cand1 and cand2 are indistinguishable */
4115 joust (cand1, cand2, warn)
4116 struct z_candidate *cand1, *cand2;
4120 int i, off1 = 0, off2 = 0, len;
4122 /* Candidates that involve bad conversions are always worse than those
4124 if (cand1->viable > cand2->viable)
4126 if (cand1->viable < cand2->viable)
4129 /* If we have two pseudo-candidates for conversions to the same type,
4130 arbitrarily pick one. */
4131 if (TYPE_P (cand1->fn) && cand1->fn == cand2->fn)
4134 /* a viable function F1
4135 is defined to be a better function than another viable function F2 if
4136 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4137 ICSi(F2), and then */
4139 /* for some argument j, ICSj(F1) is a better conversion sequence than
4142 /* For comparing static and non-static member functions, we ignore the
4143 implicit object parameter of the non-static function. The WP says to
4144 pretend that the static function has an object parm, but that won't
4145 work with operator overloading. */
4146 len = TREE_VEC_LENGTH (cand1->convs);
4147 if (len != TREE_VEC_LENGTH (cand2->convs))
4149 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4150 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4152 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4153 && DECL_STATIC_FUNCTION_P (cand2->fn))
4159 my_friendly_abort (42);
4162 for (i = 0; i < len; ++i)
4164 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4165 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4166 int comp = compare_ics (t1, t2);
4171 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4172 && TREE_CODE (t1) == STD_CONV
4173 && TREE_CODE (t2) == STD_CONV
4174 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4175 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4176 && (TYPE_PRECISION (TREE_TYPE (t1))
4177 == TYPE_PRECISION (TREE_TYPE (t2)))
4178 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4179 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4182 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4184 struct z_candidate *w, *l;
4186 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4187 w = cand1, l = cand2;
4189 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4190 w = cand2, l = cand1;
4194 cp_warning ("passing `%T' chooses `%T' over `%T'",
4195 type, type1, type2);
4196 cp_warning (" in call to `%D'", w->fn);
4202 if (winner && comp != winner)
4211 /* warn about confusing overload resolution for user-defined conversions,
4212 either between a constructor and a conversion op, or between two
4214 if (winner && cand1->second_conv
4215 && ((DECL_CONSTRUCTOR_P (cand1->fn)
4216 != DECL_CONSTRUCTOR_P (cand2->fn))
4217 /* Don't warn if the two conv ops convert to the same type... */
4218 || (! DECL_CONSTRUCTOR_P (cand1->fn)
4219 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
4220 TREE_TYPE (TREE_TYPE (cand2->fn))))))
4222 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4225 struct z_candidate *w, *l;
4227 w = cand1, l = cand2;
4229 w = cand2, l = cand1;
4232 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4233 if (! DECL_CONSTRUCTOR_P (w->fn))
4234 source = TREE_TYPE (source);
4235 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4236 cp_warning (" for conversion from `%T' to `%T'",
4237 source, TREE_TYPE (w->second_conv));
4238 cp_warning (" because conversion sequence for the argument is better");
4249 F1 is a non-template function and F2 is a template function */
4251 if (! cand1->template && cand2->template)
4253 else if (cand1->template && ! cand2->template)
4255 else if (cand1->template && cand2->template)
4256 winner = more_specialized
4257 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4261 the context is an initialization by user-defined conversion (see
4262 _dcl.init_ and _over.match.user_) and the standard conversion
4263 sequence from the return type of F1 to the destination type (i.e.,
4264 the type of the entity being initialized) is a better conversion
4265 sequence than the standard conversion sequence from the return type
4266 of F2 to the destination type. */
4268 if (! winner && cand1->second_conv)
4269 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4271 /* If the built-in candidates are the same, arbitrarily pick one. */
4272 if (! winner && cand1->fn == cand2->fn
4273 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4275 for (i = 0; i < len; ++i)
4276 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4277 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
4279 if (i == TREE_VEC_LENGTH (cand1->convs))
4282 /* Kludge around broken overloading rules whereby
4283 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4284 that takes references and another that takes values. */
4285 if (cand1->fn == ansi_opname[COND_EXPR])
4287 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4288 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4289 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4290 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4292 if (same_type_p (t1, t2))
4294 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4296 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4304 /* Extension: If the worst conversion for one candidate is worse than the
4305 worst conversion for the other, take the first. */
4306 if (! winner && ! pedantic)
4308 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4310 for (i = 0; i < len; ++i)
4312 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4313 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4314 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
4315 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
4327 /* Given a list of candidates for overloading, find the best one, if any.
4328 This algorithm has a worst case of O(2n) (winner is last), and a best
4329 case of O(n/2) (totally ambiguous); much better than a sorting
4332 static struct z_candidate *
4333 tourney (candidates)
4334 struct z_candidate *candidates;
4336 struct z_candidate *champ = candidates, *challenger;
4338 int champ_compared_to_predecessor = 0;
4340 /* Walk through the list once, comparing each current champ to the next
4341 candidate, knocking out a candidate or two with each comparison. */
4343 for (challenger = champ->next; challenger; )
4345 fate = joust (champ, challenger, 0);
4347 challenger = challenger->next;
4352 champ = challenger->next;
4355 champ_compared_to_predecessor = 0;
4360 champ_compared_to_predecessor = 1;
4363 challenger = champ->next;
4367 /* Make sure the champ is better than all the candidates it hasn't yet
4368 been compared to. */
4370 for (challenger = candidates;
4372 && !(champ_compared_to_predecessor && challenger->next == champ);
4373 challenger = challenger->next)
4375 fate = joust (champ, challenger, 0);
4384 can_convert (to, from)
4387 tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
4388 return (t && ! ICS_BAD_FLAG (t));
4392 can_convert_arg (to, from, arg)
4395 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
4396 return (t && ! ICS_BAD_FLAG (t));