1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 92-97, 1998 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 tree find_scoped_type PROTO((tree, tree, tree));
46 static struct z_candidate * tourney PROTO((struct z_candidate *));
47 static int joust PROTO((struct z_candidate *, struct z_candidate *, int));
48 static int compare_ics PROTO((tree, tree));
49 static tree build_over_call PROTO((struct z_candidate *, tree, int));
50 static tree convert_like PROTO((tree, tree));
51 static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
53 static tree build_object_call PROTO((tree, tree));
54 static tree resolve_args PROTO((tree));
55 static struct z_candidate * build_user_type_conversion_1
56 PROTO ((tree, tree, int));
57 static void print_z_candidates PROTO((struct z_candidate *));
58 static tree build_this PROTO((tree));
59 static struct z_candidate * splice_viable PROTO((struct z_candidate *));
60 static int any_viable PROTO((struct z_candidate *));
61 static struct z_candidate * add_template_candidate
62 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
64 static struct z_candidate * add_template_candidate_real
65 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
66 tree, unification_kind_t));
67 static struct z_candidate * add_template_conv_candidate
68 PROTO((struct z_candidate *, tree, tree, tree, tree));
69 static struct z_candidate * add_builtin_candidates
70 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
72 static struct z_candidate * add_builtin_candidate
73 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
74 tree, tree, tree, tree *, tree *, int));
75 static int is_complete PROTO((tree));
76 static struct z_candidate * build_builtin_candidate
77 PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
79 static struct z_candidate * add_conv_candidate
80 PROTO((struct z_candidate *, tree, tree, tree));
81 static struct z_candidate * add_function_candidate
82 PROTO((struct z_candidate *, tree, tree, int));
83 static tree implicit_conversion PROTO((tree, tree, tree, int));
84 static tree standard_conversion PROTO((tree, tree, tree));
85 static tree reference_binding PROTO((tree, tree, tree, int));
86 static tree strip_top_quals PROTO((tree));
87 static tree non_reference PROTO((tree));
88 static tree build_conv PROTO((enum tree_code, tree, tree));
89 static int is_subseq PROTO((tree, tree));
90 static int is_properly_derived_from PROTO((tree, tree));
91 static int maybe_handle_ref_bind PROTO((tree*, tree*));
92 static void maybe_handle_implicit_object PROTO((tree*));
95 build_vfield_ref (datum, type)
100 if (datum == error_mark_node)
101 return error_mark_node;
103 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
104 datum = convert_from_reference (datum);
106 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
107 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
108 datum, CLASSTYPE_VFIELD (type));
110 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
115 /* Build a call to a member of an object. I.e., one that overloads
116 operator ()(), or is a pointer-to-function or pointer-to-method. */
119 build_field_call (basetype_path, instance_ptr, name, parms)
120 tree basetype_path, instance_ptr, name, parms;
122 tree field, instance;
124 if (name == ctor_identifier || name == dtor_identifier)
127 if (instance_ptr == current_class_ptr)
129 /* Check to see if we really have a reference to an instance variable
130 with `operator()()' overloaded. */
131 field = IDENTIFIER_CLASS_VALUE (name);
133 if (field == NULL_TREE)
135 cp_error ("`this' has no member named `%D'", name);
136 return error_mark_node;
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_component_ref_1 (current_class_ref, field, 0);
144 if (instance == error_mark_node)
145 return error_mark_node;
147 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance)))
148 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
150 if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
152 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
153 return build_function_call (instance, parms);
154 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
155 return build_function_call (instance, expr_tree_cons (NULL_TREE, current_class_ptr, parms));
161 /* Check to see if this is not really a reference to an instance variable
162 with `operator()()' overloaded. */
163 field = lookup_field (basetype_path, name, 1, 0);
165 /* This can happen if the reference was ambiguous or for access
167 if (field == error_mark_node)
168 return error_mark_node;
170 if (field && (TREE_CODE (field) == FIELD_DECL ||
171 TREE_CODE (field) == VAR_DECL))
174 tree ftype = TREE_TYPE (field);
176 if (TREE_CODE (ftype) == REFERENCE_TYPE)
177 ftype = TREE_TYPE (ftype);
179 if (TYPE_LANG_SPECIFIC (ftype))
181 /* Make the next search for this field very short. */
182 basetype = DECL_FIELD_CONTEXT (field);
183 instance_ptr = convert_pointer_to (basetype, instance_ptr);
185 instance = build_indirect_ref (instance_ptr, NULL_PTR);
186 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
187 build_component_ref_1 (instance, field, 0),
190 if (TREE_CODE (ftype) == POINTER_TYPE)
192 if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
193 || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
195 /* This is a member which is a pointer to function. */
197 = build_component_ref_1 (build_indirect_ref (instance_ptr,
199 field, LOOKUP_COMPLAIN);
200 if (ref == error_mark_node)
201 return error_mark_node;
202 return build_function_call (ref, parms);
205 else if (TREE_CODE (ftype) == METHOD_TYPE)
207 error ("invalid call via pointer-to-member function");
208 return error_mark_node;
217 find_scoped_type (type, inner_name, inner_types)
218 tree type, inner_name, inner_types;
220 tree tags = CLASSTYPE_TAGS (type);
224 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
225 enclosing class) is set to the name for the enum type. So, if
226 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
227 then this test will be true. */
228 if (TREE_PURPOSE (tags) == inner_name)
230 if (inner_types == NULL_TREE)
231 return TYPE_MAIN_DECL (TREE_VALUE (tags));
232 return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
234 tags = TREE_CHAIN (tags);
237 /* Look for a TYPE_DECL. */
238 for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
239 if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
241 /* Code by raeburn. */
242 if (inner_types == NULL_TREE)
244 return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
250 /* Resolve an expression NAME1::NAME2::...::NAMEn to
251 the name that names the above nested type. INNER_TYPES
252 is a chain of nested type names (held together by SCOPE_REFs);
253 OUTER_TYPE is the type we know to enclose INNER_TYPES.
254 Returns NULL_TREE if there is an error. */
257 resolve_scope_to_name (outer_type, inner_stuff)
258 tree outer_type, inner_stuff;
261 tree inner_name, inner_type;
263 if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
265 /* We first try to look for a nesting in our current class context,
266 then try any enclosing classes. */
267 tree type = current_class_type;
269 while (type && (TREE_CODE (type) == RECORD_TYPE
270 || TREE_CODE (type) == UNION_TYPE))
272 tree rval = resolve_scope_to_name (type, inner_stuff);
274 if (rval != NULL_TREE)
276 type = DECL_CONTEXT (TYPE_MAIN_DECL (type));
280 if (TREE_CODE (inner_stuff) == SCOPE_REF)
282 inner_name = TREE_OPERAND (inner_stuff, 0);
283 inner_type = TREE_OPERAND (inner_stuff, 1);
287 inner_name = inner_stuff;
288 inner_type = NULL_TREE;
291 if (outer_type == NULL_TREE)
294 /* If we have something that's already a type by itself,
296 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
299 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
304 x = lookup_name (inner_name, 0);
306 if (x && TREE_CODE (x) == NAMESPACE_DECL)
308 x = lookup_namespace_name (x, inner_type);
314 if (! IS_AGGR_TYPE (outer_type))
317 /* Look for member classes or enums. */
318 tmp = find_scoped_type (outer_type, inner_name, inner_type);
320 /* If it's not a type in this class, then go down into the
321 base classes and search there. */
322 if (! tmp && TYPE_BINFO (outer_type))
324 tree binfos = TYPE_BINFO_BASETYPES (outer_type);
325 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
327 for (i = 0; i < n_baselinks; i++)
329 tree base_binfo = TREE_VEC_ELT (binfos, i);
330 tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
340 /* Returns nonzero iff the destructor name specified in NAME
341 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
345 check_dtor_name (basetype, name)
348 name = TREE_OPERAND (name, 0);
350 /* Just accept something we've already complained about. */
351 if (name == error_mark_node)
354 if (TREE_CODE (name) == TYPE_DECL)
355 name = TREE_TYPE (name);
356 else if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
358 else if (TREE_CODE (name) == IDENTIFIER_NODE)
360 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
361 || (TREE_CODE (basetype) == ENUMERAL_TYPE
362 && name == TYPE_IDENTIFIER (basetype)))
365 name = get_type_value (name);
368 my_friendly_abort (980605);
370 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
375 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
376 This is how virtual function calls are avoided. */
379 build_scoped_method_call (exp, basetype, name, parms)
380 tree exp, basetype, name, parms;
382 /* Because this syntactic form does not allow
383 a pointer to a base class to be `stolen',
384 we need not protect the derived->base conversion
387 @@ But we do have to check access privileges later. */
389 tree type = TREE_TYPE (exp);
391 if (type == error_mark_node
392 || basetype == error_mark_node)
393 return error_mark_node;
395 if (processing_template_decl)
397 if (TREE_CODE (name) == BIT_NOT_EXPR
398 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
400 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
402 name = build_min_nt (BIT_NOT_EXPR, type);
404 name = build_min_nt (SCOPE_REF, basetype, name);
405 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
408 if (TREE_CODE (type) == REFERENCE_TYPE)
409 type = TREE_TYPE (type);
411 if (TREE_CODE (basetype) == TREE_VEC)
414 basetype = BINFO_TYPE (binfo);
419 /* Check the destructor call syntax. */
420 if (TREE_CODE (name) == BIT_NOT_EXPR)
422 /* We can get here if someone writes their destructor call like
423 `obj.NS::~T()'; this isn't really a scoped method call, so hand
425 if (TREE_CODE (basetype) == NAMESPACE_DECL)
426 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
428 if (! check_dtor_name (basetype, name))
429 cp_error ("qualified type `%T' does not match destructor name `~%T'",
430 basetype, TREE_OPERAND (name, 0));
432 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
433 that explicit ~int is caught in the parser; this deals with typedefs
434 and template parms. */
435 if (! IS_AGGR_TYPE (basetype))
437 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
438 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
439 exp, basetype, type);
441 return cp_convert (void_type_node, exp);
445 if (! is_aggr_type (basetype, 1))
446 return error_mark_node;
448 if (! IS_AGGR_TYPE (type))
450 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
452 return error_mark_node;
457 binfo = get_binfo (basetype, type, 1);
458 if (binfo == error_mark_node)
459 return error_mark_node;
461 error_not_base_type (basetype, type);
466 if (TREE_CODE (exp) == INDIRECT_REF)
467 decl = build_indirect_ref
468 (convert_pointer_to_real
469 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
471 decl = build_scoped_ref (exp, basetype);
473 /* Call to a destructor. */
474 if (TREE_CODE (name) == BIT_NOT_EXPR)
476 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
477 return cp_convert (void_type_node, exp);
479 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
480 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
484 /* Call to a method. */
485 return build_method_call (decl, name, parms, binfo,
486 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
488 return error_mark_node;
491 /* We want the address of a function or method. We avoid creating a
492 pointer-to-member function. */
495 build_addr_func (function)
498 tree type = TREE_TYPE (function);
500 /* We have to do these by hand to avoid real pointer to member
502 if (TREE_CODE (type) == METHOD_TYPE)
506 type = build_pointer_type (type);
508 if (mark_addressable (function) == 0)
509 return error_mark_node;
511 addr = build1 (ADDR_EXPR, type, function);
513 /* Address of a static or external variable or function counts
515 if (staticp (function))
516 TREE_CONSTANT (addr) = 1;
521 function = default_conversion (function);
526 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
527 POINTER_TYPE to those. Note, pointer to member function types
528 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
531 build_call (function, result_type, parms)
532 tree function, result_type, parms;
534 int is_constructor = 0;
538 function = build_addr_func (function);
540 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
542 sorry ("unable to call pointer to member function here");
543 return error_mark_node;
546 if (TREE_CODE (function) == ADDR_EXPR
547 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
548 decl = TREE_OPERAND (function, 0);
552 if (decl && DECL_CONSTRUCTOR_P (decl))
555 /* Don't pass empty class objects by value. This is useful
556 for tags in STL, which are used to control overload resolution.
557 We don't need to handle other cases of copying empty classes. */
558 if (! decl || ! DECL_BUILT_IN (decl))
559 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
560 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
561 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
563 tree t = make_node (RTL_EXPR);
564 TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
565 RTL_EXPR_RTL (t) = const0_rtx;
566 RTL_EXPR_SEQUENCE (t) = NULL_RTX;
567 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
568 TREE_VALUE (tmp), t);
571 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
572 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
573 TREE_TYPE (function) = result_type;
574 TREE_SIDE_EFFECTS (function) = 1;
579 /* Build something of the form ptr->method (args)
580 or object.method (args). This can also build
581 calls to constructors, and find friends.
583 Member functions always take their class variable
586 INSTANCE is a class instance.
588 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
590 PARMS help to figure out what that NAME really refers to.
592 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
593 down to the real instance type to use for access checking. We need this
594 information to get protected accesses correct. This parameter is used
595 by build_member_call.
597 FLAGS is the logical disjunction of zero or more LOOKUP_
598 flags. See cp-tree.h for more info.
600 If this is all OK, calls build_function_call with the resolved
603 This function must also handle being called to perform
604 initialization, promotion/coercion of arguments, and
605 instantiation of default parameters.
607 Note that NAME may refer to an instance variable name. If
608 `operator()()' is defined for the type of that field, then we return
612 build_method_call (instance, name, parms, basetype_path, flags)
613 tree instance, name, parms, basetype_path;
616 tree basetype, instance_ptr;
618 #ifdef GATHER_STATISTICS
619 n_build_method_call++;
622 if (instance == error_mark_node
623 || name == error_mark_node
624 || parms == error_mark_node
625 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
626 return error_mark_node;
628 if (processing_template_decl)
630 /* We need to process template parm names here so that tsubst catches
631 them properly. Other type names can wait. */
632 if (TREE_CODE (name) == BIT_NOT_EXPR)
634 tree type = NULL_TREE;
636 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
637 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
638 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
639 type = TREE_TYPE (TREE_OPERAND (name, 0));
641 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
642 name = build_min_nt (BIT_NOT_EXPR, type);
645 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
648 /* This is the logic that magically deletes the second argument to
649 operator delete, if it is not needed. */
650 if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
652 tree save_last = TREE_CHAIN (parms);
654 /* get rid of unneeded argument */
655 TREE_CHAIN (parms) = NULL_TREE;
656 if (build_method_call (instance, name, parms, basetype_path,
657 (LOOKUP_SPECULATIVELY|flags) & ~LOOKUP_COMPLAIN))
659 /* If it finds a match, return it. */
660 return build_method_call (instance, name, parms, basetype_path, flags);
662 /* If it doesn't work, two argument delete must work */
663 TREE_CHAIN (parms) = save_last;
665 /* We already know whether it's needed or not for vec delete. */
666 else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
667 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
668 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
669 TREE_CHAIN (parms) = NULL_TREE;
671 if (TREE_CODE (name) == BIT_NOT_EXPR)
674 error ("destructors take no parameters");
675 basetype = TREE_TYPE (instance);
676 if (TREE_CODE (basetype) == REFERENCE_TYPE)
677 basetype = TREE_TYPE (basetype);
679 if (! check_dtor_name (basetype, name))
681 ("destructor name `~%T' does not match type `%T' of expression",
682 TREE_OPERAND (name, 0), basetype);
684 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
685 return cp_convert (void_type_node, instance);
686 instance = default_conversion (instance);
687 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
688 return build_delete (build_pointer_type (basetype),
689 instance_ptr, integer_two_node,
690 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
693 return build_new_method_call (instance, name, parms, basetype_path, flags);
696 /* New overloading code. */
706 struct z_candidate *next;
709 #define IDENTITY_RANK 0
715 #define ELLIPSIS_RANK 6
718 #define ICS_RANK(NODE) \
719 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
720 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
721 : ICS_USER_FLAG (NODE) ? USER_RANK \
722 : ICS_STD_RANK (NODE))
724 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
726 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
727 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
728 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
729 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
731 #define USER_CONV_CAND(NODE) \
732 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
733 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
740 || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE))
746 build_conv (code, type, from)
750 tree t = build1 (code, type, from);
751 int rank = ICS_STD_RANK (from);
763 if (rank < EXACT_RANK)
769 ICS_STD_RANK (t) = rank;
770 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
771 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
779 if (TREE_CODE (t) == REFERENCE_TYPE)
788 if (TREE_CODE (t) == ARRAY_TYPE)
790 return TYPE_MAIN_VARIANT (t);
793 /* Returns the standard conversion path (see [conv]) from type FROM to type
794 TO, if any. For proper handling of null pointer constants, you must
795 also pass the expression EXPR to convert from. */
798 standard_conversion (to, from, expr)
801 enum tree_code fcode, tcode;
805 if (TREE_CODE (to) == REFERENCE_TYPE)
807 if (TREE_CODE (from) == REFERENCE_TYPE)
810 from = TREE_TYPE (from);
812 to = strip_top_quals (to);
813 from = strip_top_quals (from);
815 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
816 && expr && type_unknown_p (expr))
818 expr = instantiate_type (to, expr, 0);
819 if (expr == error_mark_node)
821 from = TREE_TYPE (expr);
824 fcode = TREE_CODE (from);
825 tcode = TREE_CODE (to);
827 conv = build1 (IDENTITY_CONV, from, expr);
829 if (fcode == FUNCTION_TYPE)
831 from = build_pointer_type (from);
832 fcode = TREE_CODE (from);
833 conv = build_conv (LVALUE_CONV, from, conv);
835 else if (fcode == ARRAY_TYPE)
837 from = build_pointer_type (TREE_TYPE (from));
838 fcode = TREE_CODE (from);
839 conv = build_conv (LVALUE_CONV, from, conv);
841 else if (fromref || (expr && real_lvalue_p (expr)))
842 conv = build_conv (RVALUE_CONV, from, conv);
847 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
848 && expr && null_ptr_cst_p (expr))
850 conv = build_conv (STD_CONV, to, conv);
852 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
854 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
855 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
857 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
858 TYPE_MAIN_VARIANT (TREE_TYPE (to))))
860 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
861 && ufcode != FUNCTION_TYPE)
863 from = build_pointer_type
864 (cp_build_qualified_type (void_type_node,
865 CP_TYPE_QUALS (TREE_TYPE (from))));
866 conv = build_conv (PTR_CONV, from, conv);
868 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
870 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
871 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
873 if (DERIVED_FROM_P (fbase, tbase)
875 (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
876 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))))))
878 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
879 from = build_pointer_type (from);
880 conv = build_conv (PMEM_CONV, from, conv);
883 else if (IS_AGGR_TYPE (TREE_TYPE (from))
884 && IS_AGGR_TYPE (TREE_TYPE (to)))
886 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
889 cp_build_qualified_type (TREE_TYPE (to),
890 CP_TYPE_QUALS (TREE_TYPE (from)));
891 from = build_pointer_type (from);
892 conv = build_conv (PTR_CONV, from, conv);
896 if (same_type_p (from, to))
898 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
899 conv = build_conv (QUAL_CONV, to, conv);
900 else if (expr && string_conv_p (to, expr, 0))
901 /* converting from string constant to char *. */
902 conv = build_conv (QUAL_CONV, to, conv);
903 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
905 conv = build_conv (PTR_CONV, to, conv);
906 ICS_BAD_FLAG (conv) = 1;
913 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
915 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
916 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
917 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
918 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
920 if (! DERIVED_FROM_P (fbase, tbase)
921 || ! same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
922 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
923 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
924 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
927 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
928 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
929 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
930 from = build_ptrmemfunc_type (build_pointer_type (from));
931 conv = build_conv (PMEM_CONV, from, conv);
933 else if (tcode == BOOLEAN_TYPE)
935 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
936 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
939 conv = build_conv (STD_CONV, to, conv);
940 if (fcode == POINTER_TYPE
941 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
942 ICS_STD_RANK (conv) = PBOOL_RANK;
944 /* We don't check for ENUMERAL_TYPE here because there are no standard
945 conversions to enum type. */
946 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
947 || tcode == REAL_TYPE)
949 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
951 conv = build_conv (STD_CONV, to, conv);
953 /* Give this a better rank if it's a promotion. */
954 if (to == type_promotes_to (from)
955 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
956 ICS_STD_RANK (conv) = PROMO_RANK;
958 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
959 && DERIVED_FROM_P (to, from))
961 if (TREE_CODE (conv) == RVALUE_CONV)
962 conv = TREE_OPERAND (conv, 0);
963 conv = build_conv (BASE_CONV, to, conv);
971 /* Returns the conversion path from type FROM to reference type TO for
972 purposes of reference binding. For lvalue binding, either pass a
973 reference type to FROM or an lvalue expression to EXPR.
975 Currently does not distinguish in the generated trees between binding to
976 an lvalue and a temporary. Should it? */
979 reference_binding (rto, rfrom, expr, flags)
980 tree rto, rfrom, expr;
985 tree to = TREE_TYPE (rto);
989 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
991 expr = instantiate_type (to, expr, 0);
992 if (expr == error_mark_node)
994 from = TREE_TYPE (expr);
997 if (TREE_CODE (from) == REFERENCE_TYPE)
998 from = TREE_TYPE (from);
999 else if (! expr || ! real_lvalue_p (expr))
1002 related = (same_type_p (TYPE_MAIN_VARIANT (to),
1003 TYPE_MAIN_VARIANT (from))
1004 || (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
1005 && DERIVED_FROM_P (to, from)));
1007 if (lvalue && related && at_least_as_qualified_p (to, from))
1009 conv = build1 (IDENTITY_CONV, from, expr);
1011 if (same_type_p (TYPE_MAIN_VARIANT (to),
1012 TYPE_MAIN_VARIANT (from)))
1013 conv = build_conv (REF_BIND, rto, conv);
1016 conv = build_conv (REF_BIND, rto, conv);
1017 ICS_STD_RANK (conv) = STD_RANK;
1025 conv = standard_conversion (to, rfrom, expr);
1028 conv = build_conv (REF_BIND, rto, conv);
1030 /* Bind directly to a base subobject of a class rvalue. Do it
1031 after building the conversion for proper handling of ICS_RANK. */
1032 if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
1033 TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
1036 && ((! (CP_TYPE_CONST_NON_VOLATILE_P (to)
1037 && (flags & LOOKUP_NO_TEMP_BIND) == 0))
1038 /* If T1 is reference-related to T2, cv1 must be the same
1039 cv-qualification as, or greater cv-qualification than,
1040 cv2; otherwise, the program is ill-formed. */
1041 || (related && !at_least_as_qualified_p (to, from))))
1042 ICS_BAD_FLAG (conv) = 1;
1048 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1049 to type TO. The optional expression EXPR may affect the conversion.
1050 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1054 implicit_conversion (to, from, expr, flags)
1055 tree to, from, expr;
1059 struct z_candidate *cand;
1061 if (TREE_CODE (to) == REFERENCE_TYPE)
1062 conv = reference_binding (to, from, expr, flags);
1064 conv = standard_conversion (to, from, expr);
1068 else if (expr != NULL_TREE
1069 && (IS_AGGR_TYPE (non_reference (from))
1070 || IS_AGGR_TYPE (non_reference (to)))
1071 && (flags & LOOKUP_NO_CONVERSION) == 0)
1073 cand = build_user_type_conversion_1
1074 (to, expr, LOOKUP_ONLYCONVERTING);
1076 conv = cand->second_conv;
1077 if ((! conv || ICS_BAD_FLAG (conv))
1078 && TREE_CODE (to) == REFERENCE_TYPE
1079 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1081 cand = build_user_type_conversion_1
1082 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1085 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to)))
1086 ICS_BAD_FLAG (cand->second_conv) = 1;
1087 if (!conv || (ICS_BAD_FLAG (conv)
1088 > ICS_BAD_FLAG (cand->second_conv)))
1089 conv = build_conv (REF_BIND, to, cand->second_conv);
1097 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1100 static struct z_candidate *
1101 add_candidate (candidates, fn, convs, viable)
1102 struct z_candidate *candidates;
1106 struct z_candidate *cand
1107 = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
1110 cand->convs = convs;
1111 cand->second_conv = NULL_TREE;
1112 cand->viable = viable;
1113 cand->basetype_path = NULL_TREE;
1114 cand->template = NULL_TREE;
1115 cand->warnings = NULL_TREE;
1116 cand->next = candidates;
1121 /* Create an overload candidate for the function or method FN called with
1122 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1123 to implicit_conversion. */
1125 static struct z_candidate *
1126 add_function_candidate (candidates, fn, arglist, flags)
1127 struct z_candidate *candidates;
1131 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1134 tree parmnode = parmlist;
1135 tree argnode = arglist;
1138 /* The `this' and `in_chrg' arguments to constructors are not considered
1139 in overload resolution. */
1140 if (DECL_CONSTRUCTOR_P (fn))
1142 parmnode = TREE_CHAIN (parmnode);
1143 argnode = TREE_CHAIN (argnode);
1144 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1146 parmnode = TREE_CHAIN (parmnode);
1147 argnode = TREE_CHAIN (argnode);
1151 len = list_length (argnode);
1152 convs = make_scratch_vec (len);
1154 for (i = 0; i < len; ++i)
1156 tree arg = TREE_VALUE (argnode);
1157 tree argtype = lvalue_type (arg);
1160 if (parmnode == void_list_node)
1165 tree parmtype = TREE_VALUE (parmnode);
1167 /* [over.match.funcs] For conversion functions, the function is
1168 considered to be a member of the class of the implicit object
1169 argument for the purpose of defining the type of the implicit
1172 Since build_over_call ignores the ICS for the `this' parameter,
1173 we can just change the parm type. */
1174 if (DECL_CONV_FN_P (fn) && i == 0)
1177 = build_qualified_type (TREE_TYPE (argtype),
1178 TYPE_QUALS (TREE_TYPE (parmtype)));
1179 parmtype = build_pointer_type (parmtype);
1182 t = implicit_conversion (parmtype, argtype, arg, flags);
1186 t = build1 (IDENTITY_CONV, argtype, arg);
1187 ICS_ELLIPSIS_FLAG (t) = 1;
1190 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1191 && ! DECL_CONSTRUCTOR_P (fn))
1192 ICS_THIS_FLAG (t) = 1;
1194 TREE_VEC_ELT (convs, i) = t;
1198 if (ICS_BAD_FLAG (t))
1202 parmnode = TREE_CHAIN (parmnode);
1203 argnode = TREE_CHAIN (argnode);
1209 /* Make sure there are default args for the rest of the parms. */
1210 for (; parmnode && parmnode != void_list_node;
1211 parmnode = TREE_CHAIN (parmnode))
1212 if (! TREE_PURPOSE (parmnode))
1218 return add_candidate (candidates, fn, convs, viable);
1221 /* Create an overload candidate for the conversion function FN which will
1222 be invoked for expression OBJ, producing a pointer-to-function which
1223 will in turn be called with the argument list ARGLIST, and add it to
1224 CANDIDATES. FLAGS is passed on to implicit_conversion.
1226 Actually, we don't really care about FN; we care about the type it
1227 converts to. There may be multiple conversion functions that will
1228 convert to that type, and we rely on build_user_type_conversion_1 to
1229 choose the best one; so when we create our candidate, we record the type
1230 instead of the function. */
1232 static struct z_candidate *
1233 add_conv_candidate (candidates, fn, obj, arglist)
1234 struct z_candidate *candidates;
1235 tree fn, obj, arglist;
1237 tree totype = TREE_TYPE (TREE_TYPE (fn));
1238 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1239 int i, len = list_length (arglist) + 1;
1240 tree convs = make_scratch_vec (len);
1241 tree parmnode = parmlist;
1242 tree argnode = arglist;
1244 int flags = LOOKUP_NORMAL;
1246 /* Don't bother looking up the same type twice. */
1247 if (candidates && candidates->fn == totype)
1250 for (i = 0; i < len; ++i)
1252 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1253 tree argtype = lvalue_type (arg);
1257 t = implicit_conversion (totype, argtype, arg, flags);
1258 else if (parmnode == void_list_node)
1261 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1264 t = build1 (IDENTITY_CONV, argtype, arg);
1265 ICS_ELLIPSIS_FLAG (t) = 1;
1268 TREE_VEC_ELT (convs, i) = t;
1272 if (ICS_BAD_FLAG (t))
1279 parmnode = TREE_CHAIN (parmnode);
1280 argnode = TREE_CHAIN (argnode);
1286 for (; parmnode && parmnode != void_list_node;
1287 parmnode = TREE_CHAIN (parmnode))
1288 if (! TREE_PURPOSE (parmnode))
1294 return add_candidate (candidates, totype, convs, viable);
1297 static struct z_candidate *
1298 build_builtin_candidate (candidates, fnname, type1, type2,
1299 args, argtypes, flags)
1300 struct z_candidate *candidates;
1301 tree fnname, type1, type2, *args, *argtypes;
1312 convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1314 for (i = 0; i < 2; ++i)
1319 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1323 /* We need something for printing the candidate. */
1324 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1326 else if (ICS_BAD_FLAG (t))
1328 TREE_VEC_ELT (convs, i) = t;
1331 /* For COND_EXPR we rearranged the arguments; undo that now. */
1334 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1335 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1336 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1338 TREE_VEC_ELT (convs, 0) = t;
1343 return add_candidate (candidates, fnname, convs, viable);
1350 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1353 /* Create any builtin operator overload candidates for the operator in
1354 question given the converted operand types TYPE1 and TYPE2. The other
1355 args are passed through from add_builtin_candidates to
1356 build_builtin_candidate. */
1358 static struct z_candidate *
1359 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1360 args, argtypes, flags)
1361 struct z_candidate *candidates;
1362 enum tree_code code, code2;
1363 tree fnname, type1, type2, *args, *argtypes;
1368 case POSTINCREMENT_EXPR:
1369 case POSTDECREMENT_EXPR:
1370 args[1] = integer_zero_node;
1371 type2 = integer_type_node;
1380 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1381 and VQ is either volatile or empty, there exist candidate operator
1382 functions of the form
1383 VQ T& operator++(VQ T&);
1384 T operator++(VQ T&, int);
1385 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1386 type other than bool, and VQ is either volatile or empty, there exist
1387 candidate operator functions of the form
1388 VQ T& operator--(VQ T&);
1389 T operator--(VQ T&, int);
1390 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1391 complete object type, and VQ is either volatile or empty, there exist
1392 candidate operator functions of the form
1393 T*VQ& operator++(T*VQ&);
1394 T*VQ& operator--(T*VQ&);
1395 T* operator++(T*VQ&, int);
1396 T* operator--(T*VQ&, int); */
1398 case POSTDECREMENT_EXPR:
1399 case PREDECREMENT_EXPR:
1400 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1402 case POSTINCREMENT_EXPR:
1403 case PREINCREMENT_EXPR:
1404 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1405 || TYPE_PTROB_P (type1))
1407 type1 = build_reference_type (type1);
1412 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1413 exist candidate operator functions of the form
1417 8 For every function type T, there exist candidate operator functions of
1419 T& operator*(T*); */
1422 if (TREE_CODE (type1) == POINTER_TYPE
1423 && (TYPE_PTROB_P (type1)
1424 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1428 /* 9 For every type T, there exist candidate operator functions of the form
1431 10For every promoted arithmetic type T, there exist candidate operator
1432 functions of the form
1436 case CONVERT_EXPR: /* unary + */
1437 if (TREE_CODE (type1) == POINTER_TYPE
1438 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1441 if (ARITHMETIC_TYPE_P (type1))
1445 /* 11For every promoted integral type T, there exist candidate operator
1446 functions of the form
1450 if (INTEGRAL_TYPE_P (type1))
1454 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1455 is the same type as C2 or is a derived class of C2, T is a complete
1456 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1457 there exist candidate operator functions of the form
1458 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1459 where CV12 is the union of CV1 and CV2. */
1462 if (TREE_CODE (type1) == POINTER_TYPE
1463 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1465 tree c1 = TREE_TYPE (type1);
1466 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1467 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1468 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1470 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1471 && (TYPE_PTRMEMFUNC_P (type2)
1472 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1477 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1478 didate operator functions of the form
1483 bool operator<(L, R);
1484 bool operator>(L, R);
1485 bool operator<=(L, R);
1486 bool operator>=(L, R);
1487 bool operator==(L, R);
1488 bool operator!=(L, R);
1489 where LR is the result of the usual arithmetic conversions between
1492 14For every pair of types T and I, where T is a cv-qualified or cv-
1493 unqualified complete object type and I is a promoted integral type,
1494 there exist candidate operator functions of the form
1495 T* operator+(T*, I);
1496 T& operator[](T*, I);
1497 T* operator-(T*, I);
1498 T* operator+(I, T*);
1499 T& operator[](I, T*);
1501 15For every T, where T is a pointer to complete object type, there exist
1502 candidate operator functions of the form112)
1503 ptrdiff_t operator-(T, T);
1505 16For every pointer type T, there exist candidate operator functions of
1507 bool operator<(T, T);
1508 bool operator>(T, T);
1509 bool operator<=(T, T);
1510 bool operator>=(T, T);
1511 bool operator==(T, T);
1512 bool operator!=(T, T);
1514 17For every pointer to member type T, there exist candidate operator
1515 functions of the form
1516 bool operator==(T, T);
1517 bool operator!=(T, T); */
1520 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1522 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1524 type2 = ptrdiff_type_node;
1528 case TRUNC_DIV_EXPR:
1529 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1535 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1536 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1538 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1539 && null_ptr_cst_p (args[1]))
1544 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1545 && null_ptr_cst_p (args[0]))
1556 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1557 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1559 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1564 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1572 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1575 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1577 type1 = ptrdiff_type_node;
1580 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1582 type2 = ptrdiff_type_node;
1587 /* 18For every pair of promoted integral types L and R, there exist candi-
1588 date operator functions of the form
1595 where LR is the result of the usual arithmetic conversions between
1598 case TRUNC_MOD_EXPR:
1604 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1608 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1609 type, VQ is either volatile or empty, and R is a promoted arithmetic
1610 type, there exist candidate operator functions of the form
1611 VQ L& operator=(VQ L&, R);
1612 VQ L& operator*=(VQ L&, R);
1613 VQ L& operator/=(VQ L&, R);
1614 VQ L& operator+=(VQ L&, R);
1615 VQ L& operator-=(VQ L&, R);
1617 20For every pair T, VQ), where T is any type and VQ is either volatile
1618 or empty, there exist candidate operator functions of the form
1619 T*VQ& operator=(T*VQ&, T*);
1621 21For every pair T, VQ), where T is a pointer to member type and VQ is
1622 either volatile or empty, there exist candidate operator functions of
1624 VQ T& operator=(VQ T&, T);
1626 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1627 unqualified complete object type, VQ is either volatile or empty, and
1628 I is a promoted integral type, there exist candidate operator func-
1630 T*VQ& operator+=(T*VQ&, I);
1631 T*VQ& operator-=(T*VQ&, I);
1633 23For every triple L, VQ, R), where L is an integral or enumeration
1634 type, VQ is either volatile or empty, and R is a promoted integral
1635 type, there exist candidate operator functions of the form
1637 VQ L& operator%=(VQ L&, R);
1638 VQ L& operator<<=(VQ L&, R);
1639 VQ L& operator>>=(VQ L&, R);
1640 VQ L& operator&=(VQ L&, R);
1641 VQ L& operator^=(VQ L&, R);
1642 VQ L& operator|=(VQ L&, R); */
1649 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1651 type2 = ptrdiff_type_node;
1655 case TRUNC_DIV_EXPR:
1656 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1660 case TRUNC_MOD_EXPR:
1666 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1671 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1673 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1674 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1675 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1676 || ((TYPE_PTRMEMFUNC_P (type1)
1677 || TREE_CODE (type1) == POINTER_TYPE)
1678 && null_ptr_cst_p (args[1])))
1686 my_friendly_abort (367);
1688 type1 = build_reference_type (type1);
1692 /* Kludge around broken overloading rules whereby
1693 bool ? const char& : enum is ambiguous
1694 (between int and const char&). */
1695 flags |= LOOKUP_NO_TEMP_BIND;
1697 /* Extension: Support ?: of enumeral type. Hopefully this will not
1698 be an extension for long. */
1699 if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
1701 else if (TREE_CODE (type1) == ENUMERAL_TYPE
1702 || TREE_CODE (type2) == ENUMERAL_TYPE)
1704 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1706 if (TREE_CODE (type1) == TREE_CODE (type2)
1707 && (TREE_CODE (type1) == REFERENCE_TYPE
1708 || TREE_CODE (type1) == POINTER_TYPE
1709 || TYPE_PTRMEMFUNC_P (type1)
1710 || IS_AGGR_TYPE (type1)))
1712 if (TREE_CODE (type1) == REFERENCE_TYPE
1713 || TREE_CODE (type2) == REFERENCE_TYPE)
1715 if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
1716 && null_ptr_cst_p (args[1]))
1717 || IS_AGGR_TYPE (type1))
1722 if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
1723 && null_ptr_cst_p (args[0]))
1724 || IS_AGGR_TYPE (type2))
1732 my_friendly_abort (367);
1735 /* If we're dealing with two pointer types, we need candidates
1736 for both of them. */
1737 if (type2 && type1 != type2
1738 && TREE_CODE (type1) == TREE_CODE (type2)
1739 && (TREE_CODE (type1) == REFERENCE_TYPE
1740 || (TREE_CODE (type1) == POINTER_TYPE
1741 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1742 || TYPE_PTRMEMFUNC_P (type1)
1743 || IS_AGGR_TYPE (type1)))
1745 candidates = build_builtin_candidate
1746 (candidates, fnname, type1, type1, args, argtypes, flags);
1747 return build_builtin_candidate
1748 (candidates, fnname, type2, type2, args, argtypes, flags);
1751 return build_builtin_candidate
1752 (candidates, fnname, type1, type2, args, argtypes, flags);
1756 type_decays_to (type)
1759 if (TREE_CODE (type) == ARRAY_TYPE)
1760 return build_pointer_type (TREE_TYPE (type));
1761 if (TREE_CODE (type) == FUNCTION_TYPE)
1762 return build_pointer_type (type);
1766 /* There are three conditions of builtin candidates:
1768 1) bool-taking candidates. These are the same regardless of the input.
1769 2) pointer-pair taking candidates. These are generated for each type
1770 one of the input types converts to.
1771 3) arithmetic candidates. According to the WP, we should generate
1772 all of these, but I'm trying not to... */
1774 static struct z_candidate *
1775 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1776 struct z_candidate *candidates;
1777 enum tree_code code, code2;
1782 tree type, argtypes[3], types[2];
1784 for (i = 0; i < 3; ++i)
1787 argtypes[i] = lvalue_type (args[i]);
1789 argtypes[i] = NULL_TREE;
1794 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1795 and VQ is either volatile or empty, there exist candidate operator
1796 functions of the form
1797 VQ T& operator++(VQ T&); */
1799 case POSTINCREMENT_EXPR:
1800 case PREINCREMENT_EXPR:
1801 case POSTDECREMENT_EXPR:
1802 case PREDECREMENT_EXPR:
1807 /* 24There also exist candidate operator functions of the form
1808 bool operator!(bool);
1809 bool operator&&(bool, bool);
1810 bool operator||(bool, bool); */
1812 case TRUTH_NOT_EXPR:
1813 return build_builtin_candidate
1814 (candidates, fnname, boolean_type_node,
1815 NULL_TREE, args, argtypes, flags);
1817 case TRUTH_ORIF_EXPR:
1818 case TRUTH_ANDIF_EXPR:
1819 return build_builtin_candidate
1820 (candidates, fnname, boolean_type_node,
1821 boolean_type_node, args, argtypes, flags);
1832 types[0] = types[1] = NULL_TREE;
1834 for (i = 0; i < 2; ++i)
1838 else if (IS_AGGR_TYPE (argtypes[i]))
1840 tree convs = lookup_conversions (argtypes[i]);
1842 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1845 convs = lookup_conversions (argtypes[i]);
1847 if (code == COND_EXPR)
1849 if (real_lvalue_p (args[i]))
1850 types[i] = scratch_tree_cons
1851 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1853 types[i] = scratch_tree_cons
1854 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1860 for (; convs; convs = TREE_CHAIN (convs))
1862 type = TREE_TYPE (TREE_TYPE (TREE_VALUE (convs)));
1865 && (TREE_CODE (type) != REFERENCE_TYPE
1866 || CP_TYPE_CONST_P (TREE_TYPE (type))))
1869 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1870 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1872 type = non_reference (type);
1873 if (i != 0 || ! ref1)
1875 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1876 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1877 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1878 if (INTEGRAL_TYPE_P (type))
1879 type = type_promotes_to (type);
1882 if (! value_member (type, types[i]))
1883 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1888 if (code == COND_EXPR && real_lvalue_p (args[i]))
1889 types[i] = scratch_tree_cons
1890 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1891 type = non_reference (argtypes[i]);
1892 if (i != 0 || ! ref1)
1894 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1895 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1896 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1897 if (INTEGRAL_TYPE_P (type))
1898 type = type_promotes_to (type);
1900 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1904 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1907 for (type = types[1]; type; type = TREE_CHAIN (type))
1908 candidates = add_builtin_candidate
1909 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1910 TREE_VALUE (type), args, argtypes, flags);
1912 candidates = add_builtin_candidate
1913 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1914 NULL_TREE, args, argtypes, flags);
1921 /* If TMPL can be successfully instantiated as indicated by
1922 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
1924 TMPL is the template. EXPLICIT_TARGS are any explicit template
1925 arguments. ARGLIST is the arguments provided at the call-site.
1926 The RETURN_TYPE is the desired type for conversion operators. If
1927 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
1928 OBJ is supplied, FLAGS are ignored, and OBJ is as for
1929 add_conv_candidate. */
1931 static struct z_candidate*
1932 add_template_candidate_real (candidates, tmpl, explicit_targs,
1933 arglist, return_type, flags,
1935 struct z_candidate *candidates;
1936 tree tmpl, explicit_targs, arglist, return_type;
1939 unification_kind_t strict;
1941 int ntparms = DECL_NTPARMS (tmpl);
1942 tree targs = make_scratch_vec (ntparms);
1943 struct z_candidate *cand;
1947 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
1948 return_type, strict, NULL_TREE);
1953 fn = instantiate_template (tmpl, targs);
1954 if (fn == error_mark_node)
1957 if (obj != NULL_TREE)
1958 /* Aha, this is a conversion function. */
1959 cand = add_conv_candidate (candidates, fn, obj, arglist);
1961 cand = add_function_candidate (candidates, fn, arglist, flags);
1962 if (DECL_TI_TEMPLATE (fn) != tmpl)
1963 /* This situation can occur if a member template of a template
1964 class is specialized. Then, instantiate_template might return
1965 an instantiation of the specialization, in which case the
1966 DECL_TI_TEMPLATE field will point at the original
1967 specialization. For example:
1969 template <class T> struct S { template <class U> void f(U);
1970 template <> void f(int) {}; };
1974 Here, TMPL will be template <class U> S<double>::f(U).
1975 And, instantiate template will give us the specialization
1976 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
1977 for this will point at template <class T> template <> S<T>::f(int),
1978 so that we can find the definition. For the purposes of
1979 overload resolution, however, we want the original TMPL. */
1980 cand->template = tree_cons (tmpl, targs, NULL_TREE);
1982 cand->template = DECL_TEMPLATE_INFO (fn);
1988 static struct z_candidate *
1989 add_template_candidate (candidates, tmpl, explicit_targs,
1990 arglist, return_type, flags, strict)
1991 struct z_candidate *candidates;
1992 tree tmpl, explicit_targs, arglist, return_type;
1994 unification_kind_t strict;
1997 add_template_candidate_real (candidates, tmpl, explicit_targs,
1998 arglist, return_type, flags,
2003 static struct z_candidate *
2004 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2005 struct z_candidate *candidates;
2006 tree tmpl, obj, arglist, return_type;
2009 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
2010 return_type, 0, obj, DEDUCE_CONV);
2016 struct z_candidate *cands;
2018 for (; cands; cands = cands->next)
2019 if (pedantic ? cands->viable == 1 : cands->viable)
2024 static struct z_candidate *
2025 splice_viable (cands)
2026 struct z_candidate *cands;
2028 struct z_candidate **p = &cands;
2032 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2045 /* Fix this to work on non-lvalues. */
2046 if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
2047 || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
2050 return build_unary_op (ADDR_EXPR, obj, 0);
2054 print_z_candidates (candidates)
2055 struct z_candidate *candidates;
2057 char *str = "candidates are:";
2058 for (; candidates; candidates = candidates->next)
2060 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2062 if (candidates->fn == ansi_opname [COND_EXPR])
2063 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2064 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2065 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2066 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2067 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2068 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2069 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2070 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2072 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2073 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2075 else if (TYPE_P (candidates->fn))
2076 cp_error ("%s %T <conversion>", str, candidates->fn);
2078 cp_error_at ("%s %+D%s", str, candidates->fn,
2079 candidates->viable == -1 ? " <near match>" : "");
2084 /* Returns the best overload candidate to perform the requested
2085 conversion. This function is used for three the overloading situations
2086 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2087 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2088 per [dcl.init.ref], so we ignore temporary bindings. */
2090 static struct z_candidate *
2091 build_user_type_conversion_1 (totype, expr, flags)
2095 struct z_candidate *candidates, *cand;
2096 tree fromtype = TREE_TYPE (expr);
2097 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2098 tree args = NULL_TREE;
2099 tree templates = NULL_TREE;
2101 if (IS_AGGR_TYPE (totype))
2102 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2103 if (IS_AGGR_TYPE (fromtype)
2104 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2105 convs = lookup_conversions (fromtype);
2108 flags |= LOOKUP_NO_CONVERSION;
2112 tree t = build_int_2 (0, 0);
2113 TREE_TYPE (t) = build_pointer_type (totype);
2114 args = build_scratch_list (NULL_TREE, expr);
2115 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2116 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2117 args = scratch_tree_cons (NULL_TREE, t, args);
2119 ctors = TREE_VALUE (ctors);
2121 for (; ctors; ctors = OVL_NEXT (ctors))
2123 tree ctor = OVL_CURRENT (ctors);
2124 if (DECL_NONCONVERTING_P (ctor))
2127 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2129 templates = scratch_tree_cons (NULL_TREE, ctor, templates);
2131 add_template_candidate (candidates, ctor,
2132 NULL_TREE, args, NULL_TREE, flags,
2136 candidates = add_function_candidate (candidates, ctor,
2141 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2142 candidates->basetype_path = TYPE_BINFO (totype);
2147 args = build_scratch_list (NULL_TREE, build_this (expr));
2149 for (; convs; convs = TREE_CHAIN (convs))
2151 tree fns = TREE_VALUE (convs);
2152 int convflags = LOOKUP_NO_CONVERSION;
2155 /* If we are called to convert to a reference type, we are trying to
2156 find an lvalue binding, so don't even consider temporaries. If
2157 we don't find an lvalue binding, the caller will try again to
2158 look for a temporary binding. */
2159 if (TREE_CODE (totype) == REFERENCE_TYPE)
2160 convflags |= LOOKUP_NO_TEMP_BIND;
2162 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2163 ics = implicit_conversion
2164 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2166 /* We can't compute this yet. */
2167 ics = error_mark_node;
2169 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2170 /* ignore the near match. */;
2172 for (; fns; fns = OVL_NEXT (fns))
2174 tree fn = OVL_CURRENT (fns);
2175 struct z_candidate *old_candidates = candidates;
2177 if (TREE_CODE (fn) == TEMPLATE_DECL)
2179 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2181 add_template_candidate (candidates, fn, NULL_TREE,
2182 args, totype, flags,
2186 candidates = add_function_candidate (candidates, fn,
2189 if (candidates != old_candidates)
2191 if (TREE_CODE (fn) == TEMPLATE_DECL)
2192 ics = implicit_conversion
2193 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2196 candidates->second_conv = ics;
2197 candidates->basetype_path = TREE_PURPOSE (convs);
2199 if (ics == NULL_TREE)
2200 candidates->viable = 0;
2201 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2202 candidates->viable = -1;
2207 if (! any_viable (candidates))
2210 if (flags & LOOKUP_COMPLAIN)
2212 if (candidates && ! candidates->next)
2213 /* say why this one won't work or try to be loose */;
2215 cp_error ("no viable candidates");
2222 candidates = splice_viable (candidates);
2223 cand = tourney (candidates);
2227 if (flags & LOOKUP_COMPLAIN)
2229 cp_error ("conversion from `%T' to `%T' is ambiguous",
2231 print_z_candidates (candidates);
2234 cand = candidates; /* any one will do */
2235 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2236 ICS_USER_FLAG (cand->second_conv) = 1;
2237 ICS_BAD_FLAG (cand->second_conv) = 1;
2242 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2243 p = &(TREE_OPERAND (*p, 0));
2245 /* Pedantically, normal function declarations are never considered
2246 to refer to template instantiations, so we only do this with
2248 if (flag_guiding_decls && templates && ! cand->template
2249 && !DECL_INITIAL (cand->fn)
2250 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2251 add_maybe_template (cand->fn, templates);
2255 (DECL_CONSTRUCTOR_P (cand->fn)
2256 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2257 expr, build_expr_ptr_wrapper (cand));
2258 ICS_USER_FLAG (cand->second_conv) = 1;
2259 if (cand->viable == -1)
2260 ICS_BAD_FLAG (cand->second_conv) = 1;
2266 build_user_type_conversion (totype, expr, flags)
2270 struct z_candidate *cand
2271 = build_user_type_conversion_1 (totype, expr, flags);
2275 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2276 return error_mark_node;
2277 return convert_from_reference (convert_like (cand->second_conv, expr));
2282 /* Do any initial processing on the arguments to a function call. */
2289 for (t = args; t; t = TREE_CHAIN (t))
2291 if (TREE_VALUE (t) == error_mark_node)
2292 return error_mark_node;
2293 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2295 error ("invalid use of void expression");
2296 return error_mark_node;
2298 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2299 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2305 build_new_function_call (fn, args)
2308 struct z_candidate *candidates = 0, *cand;
2309 tree explicit_targs = NULL_TREE;
2310 int template_only = 0;
2312 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2314 explicit_targs = TREE_OPERAND (fn, 1);
2315 fn = TREE_OPERAND (fn, 0);
2319 if (really_overloaded_fn (fn))
2322 tree templates = NULL_TREE;
2324 args = resolve_args (args);
2326 if (args == error_mark_node)
2327 return error_mark_node;
2329 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2331 tree t = OVL_FUNCTION (t1);
2332 if (TREE_CODE (t) == TEMPLATE_DECL)
2334 templates = scratch_tree_cons (NULL_TREE, t, templates);
2335 candidates = add_template_candidate
2336 (candidates, t, explicit_targs, args, NULL_TREE,
2337 LOOKUP_NORMAL, DEDUCE_CALL);
2339 else if (! template_only)
2340 candidates = add_function_candidate
2341 (candidates, t, args, LOOKUP_NORMAL);
2344 if (! any_viable (candidates))
2346 if (candidates && ! candidates->next)
2347 return build_function_call (candidates->fn, args);
2348 cp_error ("no matching function for call to `%D (%A)'",
2349 DECL_NAME (OVL_FUNCTION (fn)), args);
2351 print_z_candidates (candidates);
2352 return error_mark_node;
2354 candidates = splice_viable (candidates);
2355 cand = tourney (candidates);
2359 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2360 DECL_NAME (OVL_FUNCTION (fn)), args);
2361 print_z_candidates (candidates);
2362 return error_mark_node;
2365 /* Pedantically, normal function declarations are never considered
2366 to refer to template instantiations, so we only do this with
2368 if (flag_guiding_decls && templates && ! cand->template
2369 && ! DECL_INITIAL (cand->fn))
2370 add_maybe_template (cand->fn, templates);
2372 return build_over_call (cand, args, LOOKUP_NORMAL);
2375 /* This is not really overloaded. */
2376 fn = OVL_CURRENT (fn);
2378 return build_function_call (fn, args);
2382 build_object_call (obj, args)
2385 struct z_candidate *candidates = 0, *cand;
2386 tree fns, convs, mem_args = NULL_TREE;
2387 tree type = TREE_TYPE (obj);
2389 if (TYPE_PTRMEMFUNC_P (type))
2391 /* It's no good looking for an overloaded operator() on a
2392 pointer-to-member-function. */
2393 cp_error ("pointer-to-member function %E cannot be called", obj);
2394 cp_error ("without an object; consider using .* or ->*");
2395 return error_mark_node;
2398 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2399 if (fns == error_mark_node)
2400 return error_mark_node;
2402 args = resolve_args (args);
2404 if (args == error_mark_node)
2405 return error_mark_node;
2409 tree base = TREE_PURPOSE (fns);
2410 mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2412 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2414 tree fn = OVL_CURRENT (fns);
2415 if (TREE_CODE (fn) == TEMPLATE_DECL)
2418 = add_template_candidate (candidates, fn, NULL_TREE,
2419 mem_args, NULL_TREE,
2420 LOOKUP_NORMAL, DEDUCE_CALL);
2423 candidates = add_function_candidate
2424 (candidates, fn, mem_args, LOOKUP_NORMAL);
2427 candidates->basetype_path = base;
2431 convs = lookup_conversions (type);
2433 for (; convs; convs = TREE_CHAIN (convs))
2435 tree fns = TREE_VALUE (convs);
2436 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2438 if ((TREE_CODE (totype) == POINTER_TYPE
2439 || TREE_CODE (totype) == REFERENCE_TYPE)
2440 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2441 for (; fns; fns = OVL_NEXT (fns))
2443 tree fn = OVL_CURRENT (fns);
2444 if (TREE_CODE (fn) == TEMPLATE_DECL)
2446 candidates = add_template_conv_candidate (candidates,
2453 candidates = add_conv_candidate (candidates, fn, obj, args);
2456 candidates->basetype_path = TREE_PURPOSE (convs);
2460 if (! any_viable (candidates))
2462 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2463 print_z_candidates (candidates);
2464 return error_mark_node;
2467 candidates = splice_viable (candidates);
2468 cand = tourney (candidates);
2472 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2473 print_z_candidates (candidates);
2474 return error_mark_node;
2477 if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2478 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2480 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2483 return build_function_call (obj, args);
2487 op_error (code, code2, arg1, arg2, arg3, problem)
2488 enum tree_code code, code2;
2489 tree arg1, arg2, arg3;
2493 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2498 cp_error ("%s for `%T ? %T : %T'", problem,
2499 error_type (arg1), error_type (arg2), error_type (arg3));
2501 case POSTINCREMENT_EXPR:
2502 case POSTDECREMENT_EXPR:
2503 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2506 cp_error ("%s for `%T[%T]'", problem,
2507 error_type (arg1), error_type (arg2));
2511 cp_error ("%s for `%T %s %T'", problem,
2512 error_type (arg1), opname, error_type (arg2));
2514 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2519 build_new_op (code, flags, arg1, arg2, arg3)
2520 enum tree_code code;
2522 tree arg1, arg2, arg3;
2524 struct z_candidate *candidates = 0, *cand;
2525 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
2526 enum tree_code code2 = NOP_EXPR;
2527 tree templates = NULL_TREE;
2530 if (arg1 == error_mark_node
2531 || arg2 == error_mark_node
2532 || arg3 == error_mark_node)
2533 return error_mark_node;
2535 /* This can happen if a template takes all non-type parameters, e.g.
2536 undeclared_template<1, 5, 72>a; */
2537 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
2539 cp_error ("`%D' must be declared before use", arg1);
2540 return error_mark_node;
2543 if (code == MODIFY_EXPR)
2545 code2 = TREE_CODE (arg3);
2547 fnname = ansi_assopname[code2];
2550 fnname = ansi_opname[code];
2556 case VEC_DELETE_EXPR:
2558 /* Use build_op_new_call and build_op_delete_call instead. */
2559 my_friendly_abort (981018);
2562 return build_object_call (arg1, arg2);
2568 /* The comma operator can have void args. */
2569 if (TREE_CODE (arg1) == OFFSET_REF)
2570 arg1 = resolve_offset_ref (arg1);
2571 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
2572 arg2 = resolve_offset_ref (arg2);
2573 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
2574 arg3 = resolve_offset_ref (arg3);
2576 if (code == COND_EXPR)
2578 if (arg2 == NULL_TREE
2579 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
2580 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
2581 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
2582 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
2585 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
2586 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
2589 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2590 arg2 = integer_zero_node;
2593 arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
2594 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
2596 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2598 arglist = build_scratch_list (NULL_TREE, arg1);
2600 fns = lookup_function_nonclass (fnname, arglist);
2602 if (fns && TREE_CODE (fns) == TREE_LIST)
2603 fns = TREE_VALUE (fns);
2604 for (; fns; fns = OVL_NEXT (fns))
2606 tree fn = OVL_CURRENT (fns);
2607 if (TREE_CODE (fn) == TEMPLATE_DECL)
2609 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2611 = add_template_candidate (candidates, fn, NULL_TREE,
2612 arglist, TREE_TYPE (fnname),
2613 flags, DEDUCE_CALL);
2616 candidates = add_function_candidate (candidates, fn, arglist, flags);
2619 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
2621 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
2622 if (fns == error_mark_node)
2630 tree basetype = TREE_PURPOSE (fns);
2631 mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
2632 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2634 tree fn = OVL_CURRENT (fns);
2637 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
2638 this_arglist = mem_arglist;
2640 this_arglist = arglist;
2642 if (TREE_CODE (fn) == TEMPLATE_DECL)
2644 /* A member template. */
2645 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2647 = add_template_candidate (candidates, fn, NULL_TREE,
2648 this_arglist, TREE_TYPE (fnname),
2649 flags, DEDUCE_CALL);
2652 candidates = add_function_candidate
2653 (candidates, fn, this_arglist, flags);
2656 candidates->basetype_path = basetype;
2663 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
2664 to know about two args; a builtin candidate will always have a first
2665 parameter of type bool. We'll handle that in
2666 build_builtin_candidate. */
2667 if (code == COND_EXPR)
2677 args[2] = NULL_TREE;
2680 candidates = add_builtin_candidates
2681 (candidates, code, code2, fnname, args, flags);
2684 if (! any_viable (candidates))
2688 case POSTINCREMENT_EXPR:
2689 case POSTDECREMENT_EXPR:
2690 /* Look for an `operator++ (int)'. If they didn't have
2691 one, then we fall back to the old way of doing things. */
2692 if (flags & LOOKUP_COMPLAIN)
2693 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
2694 fnname, opname_tab [code]);
2695 if (code == POSTINCREMENT_EXPR)
2696 code = PREINCREMENT_EXPR;
2698 code = PREDECREMENT_EXPR;
2699 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
2701 /* The caller will deal with these. */
2710 if (flags & LOOKUP_COMPLAIN)
2712 op_error (code, code2, arg1, arg2, arg3, "no match");
2713 print_z_candidates (candidates);
2715 return error_mark_node;
2717 candidates = splice_viable (candidates);
2718 cand = tourney (candidates);
2722 if (flags & LOOKUP_COMPLAIN)
2724 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
2725 print_z_candidates (candidates);
2727 return error_mark_node;
2730 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
2732 extern int warn_synth;
2734 && fnname == ansi_opname[MODIFY_EXPR]
2735 && DECL_ARTIFICIAL (cand->fn)
2737 && ! candidates->next->next)
2739 cp_warning ("using synthesized `%#D' for copy assignment",
2741 cp_warning_at (" where cfront would use `%#D'",
2743 ? candidates->next->fn
2747 /* Pedantically, normal function declarations are never considered
2748 to refer to template instantiations, so we only do this with
2750 if (flag_guiding_decls && templates && ! cand->template
2751 && ! DECL_INITIAL (cand->fn)
2752 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2753 add_maybe_template (cand->fn, templates);
2755 return build_over_call
2757 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
2758 ? mem_arglist : arglist,
2762 /* Check for comparison of different enum types. */
2771 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
2772 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
2773 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
2774 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
2776 cp_warning ("comparison between `%#T' and `%#T'",
2777 TREE_TYPE (arg1), TREE_TYPE (arg2));
2784 /* We need to strip any leading REF_BIND so that bitfields don't cause
2785 errors. This should not remove any important conversions, because
2786 builtins don't apply to class objects directly. */
2787 conv = TREE_VEC_ELT (cand->convs, 0);
2788 if (TREE_CODE (conv) == REF_BIND)
2789 conv = TREE_OPERAND (conv, 0);
2790 arg1 = convert_like (conv, arg1);
2792 arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
2794 arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
2800 return build_modify_expr (arg1, code2, arg2);
2803 return build_indirect_ref (arg1, "unary *");
2808 case TRUNC_DIV_EXPR:
2819 case TRUNC_MOD_EXPR:
2823 case TRUTH_ANDIF_EXPR:
2824 case TRUTH_ORIF_EXPR:
2825 return build_binary_op_nodefault (code, arg1, arg2, code);
2830 case TRUTH_NOT_EXPR:
2831 case PREINCREMENT_EXPR:
2832 case POSTINCREMENT_EXPR:
2833 case PREDECREMENT_EXPR:
2834 case POSTDECREMENT_EXPR:
2837 return build_unary_op (code, arg1, candidates != 0);
2840 return build_array_ref (arg1, arg2);
2843 return build_conditional_expr (arg1, arg2, arg3);
2846 return build_m_component_ref
2847 (build_indirect_ref (arg1, NULL_PTR), arg2);
2849 /* The caller will deal with these. */
2856 my_friendly_abort (367);
2861 /* Build up a call to operator new. This has to be handled differently
2862 from other operators in the way lookup is handled; first members are
2863 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
2864 TYPE is the type to be created. ARGS are any new-placement args.
2865 FLAGS are the usual overloading flags. */
2868 build_op_new_call (code, type, args, flags)
2869 enum tree_code code;
2873 tree fnname = ansi_opname[code];
2875 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
2876 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
2878 return build_method_call (build_dummy_object (type),
2879 fnname, args, NULL_TREE, flags);
2882 return build_new_function_call
2883 (lookup_function_nonclass (fnname, args), args);
2886 /* Build a call to operator delete. This has to be handled very specially,
2887 because the restrictions on what signatures match are different from all
2888 other call instances. For a normal delete, only a delete taking (void *)
2889 or (void *, size_t) is accepted. For a placement delete, only an exact
2890 match with the placement new is accepted.
2892 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
2893 ADDR is the pointer to be deleted. For placement delete, it is also
2894 used to determine what the corresponding new looked like.
2895 SIZE is the size of the memory block to be deleted.
2896 FLAGS are the usual overloading flags.
2897 PLACEMENT is the corresponding placement new call, or 0. */
2900 build_op_delete_call (code, addr, size, flags, placement)
2901 enum tree_code code;
2902 tree addr, size, placement;
2905 tree fn, fns, fnname, fntype, argtypes, args, type;
2907 if (addr == error_mark_node)
2908 return error_mark_node;
2910 type = TREE_TYPE (TREE_TYPE (addr));
2911 fnname = ansi_opname[code];
2913 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
2916 If the result of the lookup is ambiguous or inaccessible, or if
2917 the lookup selects a placement deallocation function, the
2918 program is ill-formed.
2920 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
2922 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
2923 if (fns == error_mark_node)
2924 return error_mark_node;
2929 if (fns == NULL_TREE)
2930 fns = lookup_name_nonclass (fnname);
2934 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
2936 /* Extract the function. */
2937 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
2938 /* Then the second parm type. */
2939 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
2941 /* Also the second argument. */
2942 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
2946 /* First try it without the size argument. */
2947 argtypes = void_list_node;
2951 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
2952 fntype = build_function_type (void_type_node, argtypes);
2954 /* Strip const and volatile from addr. */
2955 if (type != TYPE_MAIN_VARIANT (type))
2956 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
2958 fn = instantiate_type (fntype, fns, 0);
2960 if (fn != error_mark_node)
2962 if (TREE_CODE (fns) == TREE_LIST)
2963 /* Member functions. */
2964 enforce_access (TREE_PURPOSE (fns), fn);
2965 return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
2968 /* If we are doing placement delete we do nothing if we don't find a
2969 matching op delete. */
2973 /* Normal delete; now try to find a match including the size argument. */
2974 argtypes = tree_cons (NULL_TREE, ptr_type_node,
2975 tree_cons (NULL_TREE, sizetype, void_list_node));
2976 fntype = build_function_type (void_type_node, argtypes);
2978 fn = instantiate_type (fntype, fns, 0);
2980 if (fn != error_mark_node)
2982 if (TREE_CODE (fns) == TREE_LIST)
2983 /* Member functions. */
2984 enforce_access (TREE_PURPOSE (fns), fn);
2985 return build_function_call
2986 (fn, expr_tree_cons (NULL_TREE, addr,
2987 build_expr_list (NULL_TREE, size)));
2990 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
2991 destructor, in which case the error should be deferred
2992 until someone actually tries to delete one of these. */
2993 if (flags & LOOKUP_SPECULATIVELY)
2996 cp_error ("no suitable operator delete for `%T'", type);
2997 return error_mark_node;
3000 /* If the current scope isn't allowed to access DECL along
3001 BASETYPE_PATH, give an error. */
3004 enforce_access (basetype_path, decl)
3005 tree basetype_path, decl;
3007 tree access = compute_access (basetype_path, decl);
3009 if (access == access_private_node)
3011 cp_error_at ("`%+#D' is %s", decl,
3012 TREE_PRIVATE (decl) ? "private"
3013 : "from private base class");
3014 error ("within this context");
3016 else if (access == access_protected_node)
3018 cp_error_at ("`%+#D' %s", decl,
3019 TREE_PROTECTED (decl) ? "is protected"
3020 : "has protected accessibility");
3021 error ("within this context");
3025 /* Perform the conversions in CONVS on the expression EXPR. */
3028 convert_like (convs, expr)
3031 if (ICS_BAD_FLAG (convs)
3032 && TREE_CODE (convs) != USER_CONV
3033 && TREE_CODE (convs) != AMBIG_CONV)
3036 for (; t; t = TREE_OPERAND (t, 0))
3038 if (TREE_CODE (t) == USER_CONV)
3040 expr = convert_like (t, expr);
3043 else if (TREE_CODE (t) == AMBIG_CONV)
3044 return convert_like (t, expr);
3045 else if (TREE_CODE (t) == IDENTITY_CONV)
3048 return convert_for_initialization
3049 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3050 "conversion", NULL_TREE, 0);
3053 switch (TREE_CODE (convs))
3057 struct z_candidate *cand
3058 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3062 if (DECL_CONSTRUCTOR_P (fn))
3064 tree t = build_int_2 (0, 0);
3065 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3067 args = build_scratch_list (NULL_TREE, expr);
3068 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3069 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3070 args = scratch_tree_cons (NULL_TREE, t, args);
3073 args = build_this (expr);
3074 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3076 /* If this is a constructor or a function returning an aggr type,
3077 we need to build up a TARGET_EXPR. */
3078 if (DECL_CONSTRUCTOR_P (fn))
3079 expr = build_cplus_new (TREE_TYPE (convs), expr);
3084 if (type_unknown_p (expr))
3085 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3086 if (TREE_READONLY_DECL_P (expr))
3087 expr = decl_constant_value (expr);
3090 /* Call build_user_type_conversion again for the error. */
3091 return build_user_type_conversion
3092 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3098 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3099 if (expr == error_mark_node)
3100 return error_mark_node;
3102 switch (TREE_CODE (convs))
3105 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3107 /* else fall through */
3110 tree cvt_expr = build_user_type_conversion
3111 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3114 /* This can occur if, for example, the EXPR has incomplete
3115 type. We can't check for that before attempting the
3116 conversion because the type might be an incomplete
3117 array type, which is OK if some constructor for the
3118 destination type takes a pointer argument. */
3119 if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
3121 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
3122 incomplete_type_error (expr, TREE_TYPE (expr));
3124 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3125 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3128 cp_error ("could not convert `%E' to `%T'",
3129 expr, TREE_TYPE (convs));
3130 return error_mark_node;
3136 return convert_to_reference
3137 (TREE_TYPE (convs), expr,
3138 CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
3141 return decay_conversion (expr);
3144 /* Warn about deprecated conversion if appropriate. */
3145 string_conv_p (TREE_TYPE (convs), expr, 1);
3151 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3152 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3155 /* ARG is being passed to a varargs function. Perform any conversions
3156 required. Return the converted value. */
3159 convert_arg_to_ellipsis (arg)
3162 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3163 && (TYPE_PRECISION (TREE_TYPE (arg))
3164 < TYPE_PRECISION (double_type_node)))
3165 /* Convert `float' to `double'. */
3166 arg = cp_convert (double_type_node, arg);
3167 else if (IS_AGGR_TYPE (TREE_TYPE (arg))
3168 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (arg)))
3169 cp_warning ("cannot pass objects of type `%T' through `...'",
3172 /* Convert `short' and `char' to full-size `int'. */
3173 arg = default_conversion (arg);
3178 /* ARG is a default argument expression being passed to a parameter of
3179 the indicated TYPE. Do any required conversions. Return the
3183 convert_default_arg (type, arg)
3186 arg = break_out_target_exprs (arg);
3188 if (TREE_CODE (arg) == CONSTRUCTOR)
3190 arg = digest_init (type, arg, 0);
3191 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3192 "default argument", 0, 0);
3196 /* This could get clobbered by the following call. */
3197 if (TREE_HAS_CONSTRUCTOR (arg))
3198 arg = copy_node (arg);
3200 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3201 "default argument", 0, 0);
3202 #ifdef PROMOTE_PROTOTYPES
3203 if ((TREE_CODE (type) == INTEGER_TYPE
3204 || TREE_CODE (type) == ENUMERAL_TYPE)
3205 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3206 arg = default_conversion (arg);
3214 build_over_call (cand, args, flags)
3215 struct z_candidate *cand;
3220 tree convs = cand->convs;
3221 tree converted_args = NULL_TREE;
3222 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3223 tree conv, arg, val;
3227 /* Give any warnings we noticed during overload resolution. */
3229 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3230 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3232 if (DECL_FUNCTION_MEMBER_P (fn))
3233 enforce_access (cand->basetype_path, fn);
3235 if (args && TREE_CODE (args) != TREE_LIST)
3236 args = build_scratch_list (NULL_TREE, args);
3239 /* The implicit parameters to a constructor are not considered by overload
3240 resolution, and must be of the proper type. */
3241 if (DECL_CONSTRUCTOR_P (fn))
3243 converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3244 arg = TREE_CHAIN (arg);
3245 parm = TREE_CHAIN (parm);
3246 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3248 converted_args = expr_tree_cons
3249 (NULL_TREE, TREE_VALUE (arg), converted_args);
3250 arg = TREE_CHAIN (arg);
3251 parm = TREE_CHAIN (parm);
3254 /* Bypass access control for 'this' parameter. */
3255 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3257 tree parmtype = TREE_VALUE (parm);
3258 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3260 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3261 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3262 TREE_TYPE (argtype), fn);
3264 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3265 X is called for an object that is not of type X, or of a type
3266 derived from X, the behavior is undefined.
3268 So we can assume that anything passed as 'this' is non-null, and
3269 optimize accordingly. */
3270 if (TREE_CODE (parmtype) == POINTER_TYPE)
3271 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3273 /* This happens with signatures. */
3274 t = convert_force (parmtype, TREE_VALUE (arg), CONV_C_CAST);
3275 converted_args = expr_tree_cons (NULL_TREE, t, converted_args);
3276 parm = TREE_CHAIN (parm);
3277 arg = TREE_CHAIN (arg);
3283 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3285 tree type = TREE_VALUE (parm);
3287 conv = TREE_VEC_ELT (convs, i);
3288 if (ICS_BAD_FLAG (conv))
3291 val = TREE_VALUE (arg);
3293 for (; t; t = TREE_OPERAND (t, 0))
3295 if (TREE_CODE (t) == USER_CONV
3296 || TREE_CODE (t) == AMBIG_CONV)
3298 val = convert_like (t, val);
3301 else if (TREE_CODE (t) == IDENTITY_CONV)
3304 val = convert_for_initialization
3305 (NULL_TREE, type, val, LOOKUP_NORMAL,
3306 "argument passing", fn, i - is_method);
3310 /* Issue warnings about peculiar, but legal, uses of NULL. */
3311 if (ARITHMETIC_TYPE_P (TREE_VALUE (parm))
3312 && TREE_VALUE (arg) == null_node)
3313 cp_warning ("converting NULL to non-pointer type");
3315 val = convert_like (conv, TREE_VALUE (arg));
3318 #ifdef PROMOTE_PROTOTYPES
3319 if ((TREE_CODE (type) == INTEGER_TYPE
3320 || TREE_CODE (type) == ENUMERAL_TYPE)
3321 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3322 val = default_conversion (val);
3324 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3327 /* Default arguments */
3328 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3330 tree arg = TREE_PURPOSE (parm);
3332 if (DECL_TEMPLATE_INFO (fn))
3334 /* This came from a template. Instantiate the default arg here,
3335 not in tsubst. In the case of something like:
3343 we must be careful to do name lookup in the scope of
3344 S<T>, rather than in the current class. */
3345 if (DECL_CLASS_SCOPE_P (fn))
3346 pushclass (DECL_REAL_CONTEXT (fn), 2);
3348 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), NULL_TREE);
3350 if (DECL_CLASS_SCOPE_P (fn))
3353 converted_args = expr_tree_cons
3354 (NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg),
3359 for (; arg; arg = TREE_CHAIN (arg))
3361 = expr_tree_cons (NULL_TREE,
3362 convert_arg_to_ellipsis (TREE_VALUE (arg)),
3365 converted_args = nreverse (converted_args);
3367 /* Avoid actually calling copy constructors and copy assignment operators,
3370 if (! flag_elide_constructors)
3371 /* Do things the hard way. */;
3372 else if (DECL_CONSTRUCTOR_P (fn)
3373 && TREE_VEC_LENGTH (convs) == 1
3374 && copy_args_p (fn))
3377 arg = TREE_CHAIN (converted_args);
3378 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3379 arg = TREE_CHAIN (arg);
3380 arg = TREE_VALUE (arg);
3382 /* Pull out the real argument, disregarding const-correctness. */
3384 while (TREE_CODE (targ) == NOP_EXPR
3385 || TREE_CODE (targ) == NON_LVALUE_EXPR
3386 || TREE_CODE (targ) == CONVERT_EXPR)
3387 targ = TREE_OPERAND (targ, 0);
3388 if (TREE_CODE (targ) == ADDR_EXPR)
3390 targ = TREE_OPERAND (targ, 0);
3391 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3392 TYPE_MAIN_VARIANT (TREE_TYPE (targ))))
3401 arg = build_indirect_ref (arg, 0);
3403 /* [class.copy]: the copy constructor is implicitly defined even if
3404 the implementation elided its use. */
3405 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3408 /* If we're creating a temp and we already have one, don't create a
3409 new one. If we're not creating a temp but we get one, use
3410 INIT_EXPR to collapse the temp into our target. Otherwise, if the
3411 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3412 temp or an INIT_EXPR otherwise. */
3413 if (integer_zerop (TREE_VALUE (args)))
3415 if (! real_lvalue_p (arg))
3417 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3419 val = build_decl (VAR_DECL, NULL_TREE, DECL_CONTEXT (fn));
3420 val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
3421 TREE_SIDE_EFFECTS (val) = 1;
3425 else if (! real_lvalue_p (arg)
3426 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3428 tree to = stabilize_reference
3429 (build_indirect_ref (TREE_VALUE (args), 0));
3431 /* Don't copy the padding byte; it might not have been allocated
3432 if to is a base subobject. */
3433 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3434 return build_unary_op
3435 (ADDR_EXPR, build (COMPOUND_EXPR, TREE_TYPE (to),
3436 cp_convert (void_type_node, arg), to),
3439 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
3440 TREE_SIDE_EFFECTS (val) = 1;
3441 return build_unary_op (ADDR_EXPR, val, 0);
3444 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
3446 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
3448 tree to = stabilize_reference
3449 (build_indirect_ref (TREE_VALUE (converted_args), 0));
3451 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
3453 /* Don't copy the padding byte; it might not have been allocated
3454 if to is a base subobject. */
3455 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3456 return build (COMPOUND_EXPR, TREE_TYPE (to),
3457 cp_convert (void_type_node, arg), to);
3459 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
3460 TREE_SIDE_EFFECTS (val) = 1;
3466 if (DECL_CLASS_SCOPE_P (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
3467 return build_signature_method_call (fn, converted_args);
3468 else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3470 tree t, *p = &TREE_VALUE (converted_args);
3471 tree binfo = get_binfo
3472 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
3473 *p = convert_pointer_to_real (binfo, *p);
3474 if (TREE_SIDE_EFFECTS (*p))
3475 *p = save_expr (*p);
3476 t = build_pointer_type (TREE_TYPE (fn));
3477 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
3480 else if (DECL_INLINE (fn))
3481 fn = inline_conversion (fn);
3483 fn = build_addr_func (fn);
3485 /* Recognize certain built-in functions so we can make tree-codes
3486 other than CALL_EXPR. We do this when it enables fold-const.c
3487 to do something useful. */
3489 if (TREE_CODE (fn) == ADDR_EXPR
3490 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3491 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
3492 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
3497 if (converted_args == 0)
3498 return integer_zero_node;
3499 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
3504 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3505 if (TREE_TYPE (fn) == void_type_node)
3507 fn = require_complete_type (fn);
3508 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
3509 fn = build_cplus_new (TREE_TYPE (fn), fn);
3510 return convert_from_reference (fn);
3514 build_new_method_call (instance, name, args, basetype_path, flags)
3515 tree instance, name, args, basetype_path;
3518 struct z_candidate *candidates = 0, *cand;
3519 tree explicit_targs = NULL_TREE;
3520 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
3522 tree user_args = args;
3523 tree templates = NULL_TREE;
3524 int template_only = 0;
3526 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3528 explicit_targs = TREE_OPERAND (name, 1);
3529 name = TREE_OPERAND (name, 0);
3530 if (TREE_CODE (name) == TEMPLATE_DECL)
3531 name = DECL_NAME (name);
3535 /* If there is an extra argument for controlling virtual bases,
3536 remove it for error reporting. */
3537 if (flags & LOOKUP_HAS_IN_CHARGE)
3538 user_args = TREE_CHAIN (args);
3540 args = resolve_args (args);
3542 if (args == error_mark_node)
3543 return error_mark_node;
3545 if (instance == NULL_TREE)
3546 basetype = BINFO_TYPE (basetype_path);
3549 if (TREE_CODE (instance) == OFFSET_REF)
3550 instance = resolve_offset_ref (instance);
3551 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3552 instance = convert_from_reference (instance);
3553 basetype = TREE_TYPE (instance);
3555 /* XXX this should be handled before we get here. */
3556 if (! IS_AGGR_TYPE (basetype)
3557 && ! (TYPE_LANG_SPECIFIC (basetype)
3558 && (IS_SIGNATURE_POINTER (basetype)
3559 || IS_SIGNATURE_REFERENCE (basetype))))
3561 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
3562 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
3563 name, instance, basetype);
3565 return error_mark_node;
3568 /* If `instance' is a signature pointer/reference and `name' is
3569 not a constructor, we are calling a signature member function.
3570 In that case set the `basetype' to the signature type. */
3571 if ((IS_SIGNATURE_POINTER (basetype)
3572 || IS_SIGNATURE_REFERENCE (basetype))
3573 && TYPE_IDENTIFIER (basetype) != name)
3574 basetype = SIGNATURE_TYPE (basetype);
3577 if (basetype_path == NULL_TREE)
3578 basetype_path = TYPE_BINFO (basetype);
3582 instance_ptr = build_this (instance);
3584 if (! template_only)
3586 /* XXX this should be handled before we get here. */
3587 fns = build_field_call (basetype_path, instance_ptr, name, args);
3594 instance_ptr = build_int_2 (0, 0);
3595 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
3599 = (name == ctor_identifier ? constructor_name (basetype) : name);
3601 fns = lookup_fnfields (basetype_path, name, 1);
3603 if (fns == error_mark_node)
3604 return error_mark_node;
3607 tree fn = TREE_VALUE (fns);
3608 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3609 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3611 flags |= LOOKUP_HAS_IN_CHARGE;
3612 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3614 mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
3615 for (; fn; fn = OVL_NEXT (fn))
3617 tree t = OVL_CURRENT (fn);
3620 /* We can end up here for copy-init of same or base class. */
3621 if (name == ctor_identifier
3622 && (flags & LOOKUP_ONLYCONVERTING)
3623 && DECL_NONCONVERTING_P (t))
3625 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
3626 this_arglist = mem_args;
3628 this_arglist = args;
3630 if (TREE_CODE (t) == TEMPLATE_DECL)
3632 /* A member template. */
3633 templates = scratch_tree_cons (NULL_TREE, t, templates);
3635 add_template_candidate (candidates, t, explicit_targs,
3637 TREE_TYPE (name), flags, DEDUCE_CALL);
3639 else if (! template_only)
3640 candidates = add_function_candidate (candidates, t,
3641 this_arglist, flags);
3644 candidates->basetype_path = TREE_PURPOSE (fns);
3648 if (! any_viable (candidates))
3650 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
3651 if (flags & LOOKUP_SPECULATIVELY)
3653 if (TYPE_SIZE (basetype) == 0)
3654 incomplete_type_error (instance_ptr, basetype);
3656 cp_error ("no matching function for call to `%T::%D (%A)%V'",
3657 basetype, pretty_name, user_args,
3658 TREE_TYPE (TREE_TYPE (instance_ptr)));
3659 print_z_candidates (candidates);
3660 return error_mark_node;
3662 candidates = splice_viable (candidates);
3663 cand = tourney (candidates);
3667 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
3669 print_z_candidates (candidates);
3670 return error_mark_node;
3673 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
3674 && instance == current_class_ref
3675 && DECL_CONSTRUCTOR_P (current_function_decl)
3676 && ! (flags & LOOKUP_NONVIRTUAL)
3677 && value_member (cand->fn, CLASSTYPE_ABSTRACT_VIRTUALS (basetype)))
3678 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
3679 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3680 && is_dummy_object (instance_ptr))
3681 cp_error ("cannot call member function `%D' without object", cand->fn);
3683 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
3684 && ((instance == current_class_ref && (dtor_label || ctor_label))
3685 || resolves_to_fixed_type_p (instance, 0)))
3686 flags |= LOOKUP_NONVIRTUAL;
3688 /* Pedantically, normal function declarations are never considered
3689 to refer to template instantiations, so we only do this with
3691 if (flag_guiding_decls && templates && ! cand->template
3692 && ! DECL_INITIAL (cand->fn))
3693 add_maybe_template (cand->fn, templates);
3695 return build_over_call
3697 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
3701 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
3702 subsequence of ICS2. */
3705 is_subseq (ics1, ics2)
3708 /* We can assume that a conversion of the same code
3709 between the same types indicates a subsequence since we only get
3710 here if the types we are converting from are the same. */
3712 while (TREE_CODE (ics1) == RVALUE_CONV
3713 || TREE_CODE (ics1) == LVALUE_CONV)
3714 ics1 = TREE_OPERAND (ics1, 0);
3718 while (TREE_CODE (ics2) == RVALUE_CONV
3719 || TREE_CODE (ics2) == LVALUE_CONV)
3720 ics2 = TREE_OPERAND (ics2, 0);
3722 if (TREE_CODE (ics2) == USER_CONV
3723 || TREE_CODE (ics2) == AMBIG_CONV
3724 || TREE_CODE (ics2) == IDENTITY_CONV)
3725 /* At this point, ICS1 cannot be a proper subsequence of
3726 ICS2. We can get a USER_CONV when we are comparing the
3727 second standard conversion sequence of two user conversion
3731 ics2 = TREE_OPERAND (ics2, 0);
3733 if (TREE_CODE (ics2) == TREE_CODE (ics1)
3734 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
3735 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
3736 TREE_TYPE (TREE_OPERAND (ics1, 0))))
3741 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
3742 be any _TYPE nodes. */
3745 is_properly_derived_from (derived, base)
3749 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
3750 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
3753 /* We only allow proper derivation here. The DERIVED_FROM_P macro
3754 considers every class derived from itself. */
3755 return (!same_type_p (TYPE_MAIN_VARIANT (derived),
3756 TYPE_MAIN_VARIANT (base))
3757 && DERIVED_FROM_P (base, derived));
3760 /* We build the ICS for an implicit object parameter as a pointer
3761 conversion sequence. However, such a sequence should be compared
3762 as if it were a reference conversion sequence. If ICS is the
3763 implicit conversion sequence for an implicit object parameter,
3764 modify it accordingly. */
3767 maybe_handle_implicit_object (ics)
3770 if (ICS_THIS_FLAG (*ics))
3772 /* [over.match.funcs]
3774 For non-static member functions, the type of the
3775 implicit object parameter is "reference to cv X"
3776 where X is the class of which the function is a
3777 member and cv is the cv-qualification on the member
3778 function declaration. */
3780 if (TREE_CODE (t) == QUAL_CONV)
3781 t = TREE_OPERAND (t, 0);
3782 if (TREE_CODE (t) == PTR_CONV)
3783 t = TREE_OPERAND (t, 0);
3784 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3785 t = build_conv (REF_BIND,
3786 build_reference_type (TREE_TYPE (TREE_TYPE (*ics))),
3788 ICS_STD_RANK (t) = ICS_STD_RANK (*ics);
3793 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
3794 to the type the reference originally referred to, and return 1.
3795 Otherwise, return 0. */
3798 maybe_handle_ref_bind (ics, target_type)
3802 if (TREE_CODE (*ics) == REF_BIND)
3806 When a parameter of reference type binds directly
3807 (_dcl.init.ref_) to an argument expression, the implicit
3808 conversion sequence is the identity conversion, unless the
3809 argument expression has a type that is a derived class of the
3810 parameter type, in which case the implicit conversion
3811 sequence is a derived-to-base Conversion.
3813 If the parameter binds directly to the result of applying a
3814 conversion function to the argument expression, the implicit
3815 conversion sequence is a user-defined conversion sequence
3816 (_over.ics.user_), with the second standard conversion
3817 sequence either an identity conversion or, if the conversion
3818 function returns an entity of a type that is a derived class
3819 of the parameter type, a derived-to-base Conversion.
3821 When a parameter of reference type is not bound directly to
3822 an argument expression, the conversion sequence is the one
3823 required to convert the argument expression to the underlying
3824 type of the reference according to _over.best.ics_.
3825 Conceptually, this conversion sequence corresponds to
3826 copy-initializing a temporary of the underlying type with the
3827 argument expression. Any difference in top-level
3828 cv-qualification is subsumed by the initialization itself and
3829 does not constitute a conversion. */
3831 tree old_ics = *ics;
3833 *target_type = TREE_TYPE (TREE_TYPE (*ics));
3834 *ics = TREE_OPERAND (*ics, 0);
3835 if (TREE_CODE (*ics) == IDENTITY_CONV
3836 && is_properly_derived_from (TREE_TYPE (*ics), *target_type))
3837 *ics = build_conv (BASE_CONV, *target_type, *ics);
3838 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
3839 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
3847 /* Compare two implicit conversion sequences according to the rules set out in
3848 [over.ics.rank]. Return values:
3850 1: ics1 is better than ics2
3851 -1: ics2 is better than ics1
3852 0: ics1 and ics2 are indistinguishable */
3855 compare_ics (ics1, ics2)
3862 tree deref_from_type1 = NULL_TREE;
3863 tree deref_from_type2 = NULL_TREE;
3864 tree deref_to_type1 = NULL_TREE;
3865 tree deref_to_type2 = NULL_TREE;
3867 /* REF_BINDING is non-zero if the result of the conversion sequence
3868 is a reference type. In that case TARGET_TYPE is the
3869 type referred to by the reference. */
3875 /* Handle implicit object parameters. */
3876 maybe_handle_implicit_object (&ics1);
3877 maybe_handle_implicit_object (&ics2);
3879 /* Handle reference parameters. */
3880 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
3881 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
3885 When comparing the basic forms of implicit conversion sequences (as
3886 defined in _over.best.ics_)
3888 --a standard conversion sequence (_over.ics.scs_) is a better
3889 conversion sequence than a user-defined conversion sequence
3890 or an ellipsis conversion sequence, and
3892 --a user-defined conversion sequence (_over.ics.user_) is a
3893 better conversion sequence than an ellipsis conversion sequence
3894 (_over.ics.ellipsis_). */
3895 if (ICS_RANK (ics1) > ICS_RANK (ics2))
3897 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
3900 if (ICS_RANK (ics1) == BAD_RANK)
3902 /* Both ICS are bad. We try to make a decision based on what
3903 would have happenned if they'd been good. */
3904 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
3905 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3907 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
3908 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3911 /* We couldn't make up our minds; try to figure it out below. */
3914 if (ICS_ELLIPSIS_FLAG (ics1))
3915 /* Both conversions are ellipsis conversions. */
3918 /* User-defined conversion sequence U1 is a better conversion sequence
3919 than another user-defined conversion sequence U2 if they contain the
3920 same user-defined conversion operator or constructor and if the sec-
3921 ond standard conversion sequence of U1 is better than the second
3922 standard conversion sequence of U2. */
3924 if (ICS_USER_FLAG (ics1))
3928 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
3929 if (TREE_CODE (t1) == AMBIG_CONV)
3931 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
3932 if (TREE_CODE (t2) == AMBIG_CONV)
3935 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
3938 /* We can just fall through here, after setting up
3939 FROM_TYPE1 and FROM_TYPE2. */
3940 from_type1 = TREE_TYPE (t1);
3941 from_type2 = TREE_TYPE (t2);
3945 /* We're dealing with two standard conversion sequences.
3949 Standard conversion sequence S1 is a better conversion
3950 sequence than standard conversion sequence S2 if
3952 --S1 is a proper subsequence of S2 (comparing the conversion
3953 sequences in the canonical form defined by _over.ics.scs_,
3954 excluding any Lvalue Transformation; the identity
3955 conversion sequence is considered to be a subsequence of
3956 any non-identity conversion sequence */
3959 while (TREE_CODE (from_type1) != IDENTITY_CONV)
3960 from_type1 = TREE_OPERAND (from_type1, 0);
3961 from_type1 = TREE_TYPE (from_type1);
3964 while (TREE_CODE (from_type2) != IDENTITY_CONV)
3965 from_type2 = TREE_OPERAND (from_type2, 0);
3966 from_type2 = TREE_TYPE (from_type2);
3969 if (same_type_p (from_type1, from_type2))
3971 if (is_subseq (ics1, ics2))
3973 if (is_subseq (ics2, ics1))
3976 /* Otherwise, one sequence cannot be a subsequence of the other; they
3977 don't start with the same type. This can happen when comparing the
3978 second standard conversion sequence in two user-defined conversion
3985 --the rank of S1 is better than the rank of S2 (by the rules
3988 Standard conversion sequences are ordered by their ranks: an Exact
3989 Match is a better conversion than a Promotion, which is a better
3990 conversion than a Conversion.
3992 Two conversion sequences with the same rank are indistinguishable
3993 unless one of the following rules applies:
3995 --A conversion that is not a conversion of a pointer, or pointer
3996 to member, to bool is better than another conversion that is such
3999 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4000 so that we do not have to check it explicitly. */
4001 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4003 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4006 to_type1 = TREE_TYPE (ics1);
4007 to_type2 = TREE_TYPE (ics2);
4009 if (TYPE_PTR_P (from_type1)
4010 && TYPE_PTR_P (from_type2)
4011 && TYPE_PTR_P (to_type1)
4012 && TYPE_PTR_P (to_type2))
4014 deref_from_type1 = TREE_TYPE (from_type1);
4015 deref_from_type2 = TREE_TYPE (from_type2);
4016 deref_to_type1 = TREE_TYPE (to_type1);
4017 deref_to_type2 = TREE_TYPE (to_type2);
4019 /* The rules for pointers to members A::* are just like the rules
4020 for pointers A*, except opposite: if B is derived from A then
4021 A::* converts to B::*, not vice versa. For that reason, we
4022 switch the from_ and to_ variables here. */
4023 else if (TYPE_PTRMEM_P (from_type1)
4024 && TYPE_PTRMEM_P (from_type2)
4025 && TYPE_PTRMEM_P (to_type1)
4026 && TYPE_PTRMEM_P (to_type2))
4028 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4029 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4030 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4031 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4033 else if (TYPE_PTRMEMFUNC_P (from_type1)
4034 && TYPE_PTRMEMFUNC_P (from_type2)
4035 && TYPE_PTRMEMFUNC_P (to_type1)
4036 && TYPE_PTRMEMFUNC_P (to_type2))
4038 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4039 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4040 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4041 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4044 if (deref_from_type1 != NULL_TREE
4045 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4046 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4048 /* This was one of the pointer or pointer-like conversions.
4052 --If class B is derived directly or indirectly from class A,
4053 conversion of B* to A* is better than conversion of B* to
4054 void*, and conversion of A* to void* is better than
4055 conversion of B* to void*. */
4056 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4057 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4059 if (is_properly_derived_from (deref_from_type1,
4062 else if (is_properly_derived_from (deref_from_type2,
4066 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4067 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4069 if (same_type_p (deref_from_type1, deref_from_type2))
4071 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4073 if (is_properly_derived_from (deref_from_type1,
4077 /* We know that DEREF_TO_TYPE1 is `void' here. */
4078 else if (is_properly_derived_from (deref_from_type1,
4083 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4084 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4088 --If class B is derived directly or indirectly from class A
4089 and class C is derived directly or indirectly from B,
4091 --conversion of C* to B* is better than conversion of C* to
4094 --conversion of B* to A* is better than conversion of C* to
4096 if (same_type_p (deref_from_type1, deref_from_type2))
4098 if (is_properly_derived_from (deref_to_type1,
4101 else if (is_properly_derived_from (deref_to_type2,
4105 else if (same_type_p (deref_to_type1, deref_to_type2))
4107 if (is_properly_derived_from (deref_from_type2,
4110 else if (is_properly_derived_from (deref_from_type1,
4116 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4117 && same_type_p (from_type1, from_type2))
4121 --binding of an expression of type C to a reference of type
4122 B& is better than binding an expression of type C to a
4123 reference of type A&
4125 --conversion of C to B is better than conversion of C to A, */
4126 if (is_properly_derived_from (from_type1, to_type1)
4127 && is_properly_derived_from (from_type1, to_type2))
4129 if (is_properly_derived_from (to_type1, to_type2))
4131 else if (is_properly_derived_from (to_type2, to_type1))
4135 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4136 && same_type_p (to_type1, to_type2))
4140 --binding of an expression of type B to a reference of type
4141 A& is better than binding an expression of type C to a
4142 reference of type A&,
4144 --onversion of B to A is better than conversion of C to A */
4145 if (is_properly_derived_from (from_type1, to_type1)
4146 && is_properly_derived_from (from_type2, to_type1))
4148 if (is_properly_derived_from (from_type2, from_type1))
4150 else if (is_properly_derived_from (from_type1, from_type2))
4157 --S1 and S2 differ only in their qualification conversion and yield
4158 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4159 qualification signature of type T1 is a proper subset of the cv-
4160 qualification signature of type T2 */
4161 if (TREE_CODE (ics1) == QUAL_CONV
4162 && TREE_CODE (ics2) == QUAL_CONV
4163 && same_type_p (from_type1, from_type2))
4164 return comp_cv_qual_signature (to_type1, to_type2);
4168 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4169 types to which the references refer are the same type except for
4170 top-level cv-qualifiers, and the type to which the reference
4171 initialized by S2 refers is more cv-qualified than the type to
4172 which the reference initialized by S1 refers */
4174 if (ref_binding1 && ref_binding2
4175 && same_type_p (TYPE_MAIN_VARIANT (to_type1),
4176 TYPE_MAIN_VARIANT (to_type2)))
4177 return comp_cv_qualification (target_type2, target_type1);
4179 /* Neither conversion sequence is better than the other. */
4183 /* The source type for this standard conversion sequence. */
4189 for (;; t = TREE_OPERAND (t, 0))
4191 if (TREE_CODE (t) == USER_CONV
4192 || TREE_CODE (t) == AMBIG_CONV
4193 || TREE_CODE (t) == IDENTITY_CONV)
4194 return TREE_TYPE (t);
4196 my_friendly_abort (1823);
4199 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4200 a pointer to LOSER and re-running joust to produce the warning if WINNER
4201 is actually used. */
4204 add_warning (winner, loser)
4205 struct z_candidate *winner, *loser;
4207 winner->warnings = expr_tree_cons (NULL_PTR,
4208 build_expr_ptr_wrapper (loser),
4212 /* Compare two candidates for overloading as described in
4213 [over.match.best]. Return values:
4215 1: cand1 is better than cand2
4216 -1: cand2 is better than cand1
4217 0: cand1 and cand2 are indistinguishable */
4220 joust (cand1, cand2, warn)
4221 struct z_candidate *cand1, *cand2;
4225 int i, off1 = 0, off2 = 0, len;
4227 /* Candidates that involve bad conversions are always worse than those
4229 if (cand1->viable > cand2->viable)
4231 if (cand1->viable < cand2->viable)
4234 /* If we have two pseudo-candidates for conversions to the same type,
4235 arbitrarily pick one. */
4236 if (TYPE_P (cand1->fn) && cand1->fn == cand2->fn)
4239 /* a viable function F1
4240 is defined to be a better function than another viable function F2 if
4241 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4242 ICSi(F2), and then */
4244 /* for some argument j, ICSj(F1) is a better conversion sequence than
4247 /* For comparing static and non-static member functions, we ignore the
4248 implicit object parameter of the non-static function. The WP says to
4249 pretend that the static function has an object parm, but that won't
4250 work with operator overloading. */
4251 len = TREE_VEC_LENGTH (cand1->convs);
4252 if (len != TREE_VEC_LENGTH (cand2->convs))
4254 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4255 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4257 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4258 && DECL_STATIC_FUNCTION_P (cand2->fn))
4264 my_friendly_abort (42);
4267 for (i = 0; i < len; ++i)
4269 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4270 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4271 int comp = compare_ics (t1, t2);
4276 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4277 && TREE_CODE (t1) == STD_CONV
4278 && TREE_CODE (t2) == STD_CONV
4279 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4280 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4281 && (TYPE_PRECISION (TREE_TYPE (t1))
4282 == TYPE_PRECISION (TREE_TYPE (t2)))
4283 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4284 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4287 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4289 struct z_candidate *w, *l;
4291 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4292 w = cand1, l = cand2;
4294 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4295 w = cand2, l = cand1;
4299 cp_warning ("passing `%T' chooses `%T' over `%T'",
4300 type, type1, type2);
4301 cp_warning (" in call to `%D'", w->fn);
4307 if (winner && comp != winner)
4316 /* warn about confusing overload resolution for user-defined conversions,
4317 either between a constructor and a conversion op, or between two
4319 if (winner && cand1->second_conv
4320 && ((DECL_CONSTRUCTOR_P (cand1->fn)
4321 != DECL_CONSTRUCTOR_P (cand2->fn))
4322 /* Don't warn if the two conv ops convert to the same type... */
4323 || (! DECL_CONSTRUCTOR_P (cand1->fn)
4324 && ! same_type_p (TREE_TYPE (cand1->second_conv),
4325 TREE_TYPE (cand2->second_conv)))))
4327 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4330 struct z_candidate *w, *l;
4332 w = cand1, l = cand2;
4334 w = cand2, l = cand1;
4337 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4338 if (! DECL_CONSTRUCTOR_P (w->fn))
4339 source = TREE_TYPE (source);
4340 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4341 cp_warning (" for conversion from `%T' to `%T'",
4342 source, TREE_TYPE (w->second_conv));
4343 cp_warning (" because conversion sequence for the argument is better");
4354 F1 is a non-template function and F2 is a template function */
4356 if (! cand1->template && cand2->template)
4358 else if (cand1->template && ! cand2->template)
4360 else if (cand1->template && cand2->template)
4361 winner = more_specialized
4362 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4366 the context is an initialization by user-defined conversion (see
4367 _dcl.init_ and _over.match.user_) and the standard conversion
4368 sequence from the return type of F1 to the destination type (i.e.,
4369 the type of the entity being initialized) is a better conversion
4370 sequence than the standard conversion sequence from the return type
4371 of F2 to the destination type. */
4373 if (! winner && cand1->second_conv)
4374 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4376 /* If the built-in candidates are the same, arbitrarily pick one. */
4377 if (! winner && cand1->fn == cand2->fn
4378 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4380 for (i = 0; i < len; ++i)
4381 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4382 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
4384 if (i == TREE_VEC_LENGTH (cand1->convs))
4387 /* Kludge around broken overloading rules whereby
4388 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4389 that takes references and another that takes values. */
4390 if (cand1->fn == ansi_opname[COND_EXPR])
4392 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4393 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4394 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4395 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4397 if (same_type_p (t1, t2))
4399 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4401 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4409 /* Extension: If the worst conversion for one candidate is worse than the
4410 worst conversion for the other, take the first. */
4411 if (! winner && ! pedantic)
4413 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4415 for (i = 0; i < len; ++i)
4417 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4418 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4419 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
4420 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
4432 /* Given a list of candidates for overloading, find the best one, if any.
4433 This algorithm has a worst case of O(2n) (winner is last), and a best
4434 case of O(n/2) (totally ambiguous); much better than a sorting
4437 static struct z_candidate *
4438 tourney (candidates)
4439 struct z_candidate *candidates;
4441 struct z_candidate *champ = candidates, *challenger;
4443 int champ_compared_to_predecessor = 0;
4445 /* Walk through the list once, comparing each current champ to the next
4446 candidate, knocking out a candidate or two with each comparison. */
4448 for (challenger = champ->next; challenger; )
4450 fate = joust (champ, challenger, 0);
4452 challenger = challenger->next;
4457 champ = challenger->next;
4460 champ_compared_to_predecessor = 0;
4465 champ_compared_to_predecessor = 1;
4468 challenger = champ->next;
4472 /* Make sure the champ is better than all the candidates it hasn't yet
4473 been compared to. */
4475 for (challenger = candidates;
4477 && !(champ_compared_to_predecessor && challenger->next == champ);
4478 challenger = challenger->next)
4480 fate = joust (champ, challenger, 0);
4489 can_convert (to, from)
4492 tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
4493 return (t && ! ICS_BAD_FLAG (t));
4497 can_convert_arg (to, from, arg)
4500 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
4501 return (t && ! ICS_BAD_FLAG (t));