1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 92-97, 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
39 extern int inhibit_warnings;
40 extern tree ctor_label, dtor_label;
42 static tree build_new_method_call PROTO((tree, tree, tree, tree, int));
44 static tree build_field_call PROTO((tree, tree, tree, tree));
45 static 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 maybe_handle_ref_bind PROTO((tree*, tree*));
91 static void maybe_handle_implicit_object PROTO((tree*));
92 static struct z_candidate * add_candidate PROTO((struct z_candidate *,
94 static tree source_type PROTO((tree));
95 static void add_warning PROTO((struct z_candidate *, struct z_candidate *));
98 build_vfield_ref (datum, type)
103 if (datum == error_mark_node)
104 return error_mark_node;
106 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
107 datum = convert_from_reference (datum);
109 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
110 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
111 datum, CLASSTYPE_VFIELD (type));
113 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
118 /* Build a call to a member of an object. I.e., one that overloads
119 operator ()(), or is a pointer-to-function or pointer-to-method. */
122 build_field_call (basetype_path, instance_ptr, name, parms)
123 tree basetype_path, instance_ptr, name, parms;
125 tree field, instance;
127 if (name == ctor_identifier || name == dtor_identifier)
130 /* Speed up the common case. */
131 if (instance_ptr == current_class_ptr
132 && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
135 field = lookup_field (basetype_path, name, 1, 0);
137 if (field == error_mark_node || field == NULL_TREE)
140 if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
142 /* If it's a field, try overloading operator (),
143 or calling if the field is a pointer-to-function. */
144 instance = build_indirect_ref (instance_ptr, NULL_PTR);
145 instance = build_component_ref_1 (instance, field, 0);
147 if (instance == error_mark_node)
148 return error_mark_node;
150 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
151 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
152 instance, parms, NULL_TREE);
153 else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
155 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
156 return build_function_call (instance, parms);
157 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
159 return build_function_call
160 (instance, expr_tree_cons (NULL_TREE, instance_ptr, parms));
168 find_scoped_type (type, inner_name, inner_types)
169 tree type, inner_name, inner_types;
171 tree tags = CLASSTYPE_TAGS (type);
175 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
176 enclosing class) is set to the name for the enum type. So, if
177 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
178 then this test will be true. */
179 if (TREE_PURPOSE (tags) == inner_name)
181 if (inner_types == NULL_TREE)
182 return TYPE_MAIN_DECL (TREE_VALUE (tags));
183 return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
185 tags = TREE_CHAIN (tags);
188 /* Look for a TYPE_DECL. */
189 for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
190 if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
192 /* Code by raeburn. */
193 if (inner_types == NULL_TREE)
195 return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
201 /* Resolve an expression NAME1::NAME2::...::NAMEn to
202 the name that names the above nested type. INNER_TYPES
203 is a chain of nested type names (held together by SCOPE_REFs);
204 OUTER_TYPE is the type we know to enclose INNER_TYPES.
205 Returns NULL_TREE if there is an error. */
208 resolve_scope_to_name (outer_type, inner_stuff)
209 tree outer_type, inner_stuff;
212 tree inner_name, inner_type;
214 if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
216 /* We first try to look for a nesting in our current class context,
217 then try any enclosing classes. */
218 tree type = current_class_type;
220 while (type && (TREE_CODE (type) == RECORD_TYPE
221 || TREE_CODE (type) == UNION_TYPE))
223 tree rval = resolve_scope_to_name (type, inner_stuff);
225 if (rval != NULL_TREE)
227 type = DECL_CONTEXT (TYPE_MAIN_DECL (type));
231 if (TREE_CODE (inner_stuff) == SCOPE_REF)
233 inner_name = TREE_OPERAND (inner_stuff, 0);
234 inner_type = TREE_OPERAND (inner_stuff, 1);
238 inner_name = inner_stuff;
239 inner_type = NULL_TREE;
242 if (outer_type == NULL_TREE)
245 /* If we have something that's already a type by itself,
247 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
250 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
255 x = lookup_name (inner_name, 0);
257 if (x && TREE_CODE (x) == NAMESPACE_DECL)
259 x = lookup_namespace_name (x, inner_type);
265 if (! IS_AGGR_TYPE (outer_type))
268 /* Look for member classes or enums. */
269 tmp = find_scoped_type (outer_type, inner_name, inner_type);
271 /* If it's not a type in this class, then go down into the
272 base classes and search there. */
273 if (! tmp && TYPE_BINFO (outer_type))
275 tree binfos = TYPE_BINFO_BASETYPES (outer_type);
276 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
278 for (i = 0; i < n_baselinks; i++)
280 tree base_binfo = TREE_VEC_ELT (binfos, i);
281 tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
291 /* Returns nonzero iff the destructor name specified in NAME
292 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
296 check_dtor_name (basetype, name)
299 name = TREE_OPERAND (name, 0);
301 /* Just accept something we've already complained about. */
302 if (name == error_mark_node)
305 if (TREE_CODE (name) == TYPE_DECL)
306 name = TREE_TYPE (name);
307 else if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
309 else if (TREE_CODE (name) == IDENTIFIER_NODE)
311 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
312 || (TREE_CODE (basetype) == ENUMERAL_TYPE
313 && name == TYPE_IDENTIFIER (basetype)))
316 name = get_type_value (name);
319 my_friendly_abort (980605);
321 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
326 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
327 This is how virtual function calls are avoided. */
330 build_scoped_method_call (exp, basetype, name, parms)
331 tree exp, basetype, name, parms;
333 /* Because this syntactic form does not allow
334 a pointer to a base class to be `stolen',
335 we need not protect the derived->base conversion
338 @@ But we do have to check access privileges later. */
340 tree type = TREE_TYPE (exp);
342 if (type == error_mark_node
343 || basetype == error_mark_node)
344 return error_mark_node;
346 if (processing_template_decl)
348 if (TREE_CODE (name) == BIT_NOT_EXPR
349 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
351 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
353 name = build_min_nt (BIT_NOT_EXPR, type);
355 name = build_min_nt (SCOPE_REF, basetype, name);
356 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
359 if (TREE_CODE (type) == REFERENCE_TYPE)
360 type = TREE_TYPE (type);
362 if (TREE_CODE (basetype) == TREE_VEC)
365 basetype = BINFO_TYPE (binfo);
370 /* Check the destructor call syntax. */
371 if (TREE_CODE (name) == BIT_NOT_EXPR)
373 /* We can get here if someone writes their destructor call like
374 `obj.NS::~T()'; this isn't really a scoped method call, so hand
376 if (TREE_CODE (basetype) == NAMESPACE_DECL)
377 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
379 if (! check_dtor_name (basetype, name))
380 cp_error ("qualified type `%T' does not match destructor name `~%T'",
381 basetype, TREE_OPERAND (name, 0));
383 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
384 that explicit ~int is caught in the parser; this deals with typedefs
385 and template parms. */
386 if (! IS_AGGR_TYPE (basetype))
388 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
389 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
390 exp, basetype, type);
392 return cp_convert (void_type_node, exp);
396 if (! is_aggr_type (basetype, 1))
397 return error_mark_node;
399 if (! IS_AGGR_TYPE (type))
401 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
403 return error_mark_node;
408 binfo = get_binfo (basetype, type, 1);
409 if (binfo == error_mark_node)
410 return error_mark_node;
412 error_not_base_type (basetype, type);
417 if (TREE_CODE (exp) == INDIRECT_REF)
418 decl = build_indirect_ref
419 (convert_pointer_to_real
420 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
422 decl = build_scoped_ref (exp, basetype);
424 /* Call to a destructor. */
425 if (TREE_CODE (name) == BIT_NOT_EXPR)
427 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
428 return cp_convert (void_type_node, exp);
430 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
431 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
435 /* Call to a method. */
436 return build_method_call (decl, name, parms, binfo,
437 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
439 return error_mark_node;
442 /* We want the address of a function or method. We avoid creating a
443 pointer-to-member function. */
446 build_addr_func (function)
449 tree type = TREE_TYPE (function);
451 /* We have to do these by hand to avoid real pointer to member
453 if (TREE_CODE (type) == METHOD_TYPE)
457 type = build_pointer_type (type);
459 if (mark_addressable (function) == 0)
460 return error_mark_node;
462 addr = build1 (ADDR_EXPR, type, function);
464 /* Address of a static or external variable or function counts
466 if (staticp (function))
467 TREE_CONSTANT (addr) = 1;
472 function = default_conversion (function);
477 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
478 POINTER_TYPE to those. Note, pointer to member function types
479 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
482 build_call (function, result_type, parms)
483 tree function, result_type, parms;
485 int is_constructor = 0;
489 function = build_addr_func (function);
491 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
493 sorry ("unable to call pointer to member function here");
494 return error_mark_node;
497 if (TREE_CODE (function) == ADDR_EXPR
498 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
499 decl = TREE_OPERAND (function, 0);
503 if (decl && DECL_CONSTRUCTOR_P (decl))
507 my_friendly_assert (TREE_USED (decl), 990125);
509 /* Don't pass empty class objects by value. This is useful
510 for tags in STL, which are used to control overload resolution.
511 We don't need to handle other cases of copying empty classes. */
512 if (! decl || ! DECL_BUILT_IN (decl))
513 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
514 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
515 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
517 tree t = make_node (RTL_EXPR);
518 TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
519 RTL_EXPR_RTL (t) = const0_rtx;
520 RTL_EXPR_SEQUENCE (t) = NULL_RTX;
521 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
522 TREE_VALUE (tmp), t);
525 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
526 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
527 TREE_TYPE (function) = result_type;
528 TREE_SIDE_EFFECTS (function) = 1;
533 /* Build something of the form ptr->method (args)
534 or object.method (args). This can also build
535 calls to constructors, and find friends.
537 Member functions always take their class variable
540 INSTANCE is a class instance.
542 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
544 PARMS help to figure out what that NAME really refers to.
546 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
547 down to the real instance type to use for access checking. We need this
548 information to get protected accesses correct. This parameter is used
549 by build_member_call.
551 FLAGS is the logical disjunction of zero or more LOOKUP_
552 flags. See cp-tree.h for more info.
554 If this is all OK, calls build_function_call with the resolved
557 This function must also handle being called to perform
558 initialization, promotion/coercion of arguments, and
559 instantiation of default parameters.
561 Note that NAME may refer to an instance variable name. If
562 `operator()()' is defined for the type of that field, then we return
566 build_method_call (instance, name, parms, basetype_path, flags)
567 tree instance, name, parms, basetype_path;
570 tree basetype, instance_ptr;
572 #ifdef GATHER_STATISTICS
573 n_build_method_call++;
576 if (instance == error_mark_node
577 || name == error_mark_node
578 || parms == error_mark_node
579 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
580 return error_mark_node;
582 if (processing_template_decl)
584 /* We need to process template parm names here so that tsubst catches
585 them properly. Other type names can wait. */
586 if (TREE_CODE (name) == BIT_NOT_EXPR)
588 tree type = NULL_TREE;
590 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
591 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
592 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
593 type = TREE_TYPE (TREE_OPERAND (name, 0));
595 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
596 name = build_min_nt (BIT_NOT_EXPR, type);
599 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
602 if (TREE_CODE (name) == BIT_NOT_EXPR)
605 error ("destructors take no parameters");
606 basetype = TREE_TYPE (instance);
607 if (TREE_CODE (basetype) == REFERENCE_TYPE)
608 basetype = TREE_TYPE (basetype);
610 if (! check_dtor_name (basetype, name))
612 ("destructor name `~%T' does not match type `%T' of expression",
613 TREE_OPERAND (name, 0), basetype);
615 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
616 return cp_convert (void_type_node, instance);
617 instance = default_conversion (instance);
618 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
619 return build_delete (build_pointer_type (basetype),
620 instance_ptr, integer_two_node,
621 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
624 return build_new_method_call (instance, name, parms, basetype_path, flags);
627 /* New overloading code. */
637 struct z_candidate *next;
640 #define IDENTITY_RANK 0
646 #define ELLIPSIS_RANK 6
649 #define ICS_RANK(NODE) \
650 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
651 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
652 : ICS_USER_FLAG (NODE) ? USER_RANK \
653 : ICS_STD_RANK (NODE))
655 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
657 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
658 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
659 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
660 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
662 #define USER_CONV_CAND(NODE) \
663 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
664 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
671 || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE))
677 build_conv (code, type, from)
681 tree t = build1 (code, type, from);
682 int rank = ICS_STD_RANK (from);
694 if (rank < EXACT_RANK)
700 ICS_STD_RANK (t) = rank;
701 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
702 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
710 if (TREE_CODE (t) == REFERENCE_TYPE)
719 if (TREE_CODE (t) == ARRAY_TYPE)
721 return TYPE_MAIN_VARIANT (t);
724 /* Returns the standard conversion path (see [conv]) from type FROM to type
725 TO, if any. For proper handling of null pointer constants, you must
726 also pass the expression EXPR to convert from. */
729 standard_conversion (to, from, expr)
732 enum tree_code fcode, tcode;
736 if (TREE_CODE (to) == REFERENCE_TYPE)
738 if (TREE_CODE (from) == REFERENCE_TYPE)
741 from = TREE_TYPE (from);
743 to = strip_top_quals (to);
744 from = strip_top_quals (from);
746 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
747 && expr && type_unknown_p (expr))
749 expr = instantiate_type (to, expr, 0);
750 if (expr == error_mark_node)
752 from = TREE_TYPE (expr);
755 fcode = TREE_CODE (from);
756 tcode = TREE_CODE (to);
758 conv = build1 (IDENTITY_CONV, from, expr);
760 if (fcode == FUNCTION_TYPE)
762 from = build_pointer_type (from);
763 fcode = TREE_CODE (from);
764 conv = build_conv (LVALUE_CONV, from, conv);
766 else if (fcode == ARRAY_TYPE)
768 from = build_pointer_type (TREE_TYPE (from));
769 fcode = TREE_CODE (from);
770 conv = build_conv (LVALUE_CONV, from, conv);
772 else if (fromref || (expr && real_lvalue_p (expr)))
773 conv = build_conv (RVALUE_CONV, from, conv);
778 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
779 && expr && null_ptr_cst_p (expr))
781 conv = build_conv (STD_CONV, to, conv);
783 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
785 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
786 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
788 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
789 TYPE_MAIN_VARIANT (TREE_TYPE (to))))
791 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
792 && ufcode != FUNCTION_TYPE)
794 from = build_pointer_type
795 (cp_build_qualified_type (void_type_node,
796 CP_TYPE_QUALS (TREE_TYPE (from))));
797 conv = build_conv (PTR_CONV, from, conv);
799 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
801 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
802 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
804 if (DERIVED_FROM_P (fbase, tbase)
806 (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
807 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))))))
809 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
810 from = build_pointer_type (from);
811 conv = build_conv (PMEM_CONV, from, conv);
814 else if (IS_AGGR_TYPE (TREE_TYPE (from))
815 && IS_AGGR_TYPE (TREE_TYPE (to)))
817 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
820 cp_build_qualified_type (TREE_TYPE (to),
821 CP_TYPE_QUALS (TREE_TYPE (from)));
822 from = build_pointer_type (from);
823 conv = build_conv (PTR_CONV, from, conv);
827 if (same_type_p (from, to))
829 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
830 conv = build_conv (QUAL_CONV, to, conv);
831 else if (expr && string_conv_p (to, expr, 0))
832 /* converting from string constant to char *. */
833 conv = build_conv (QUAL_CONV, to, conv);
834 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
836 conv = build_conv (PTR_CONV, to, conv);
837 ICS_BAD_FLAG (conv) = 1;
844 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
846 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
847 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
848 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
849 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
851 if (! DERIVED_FROM_P (fbase, tbase)
852 || ! same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
853 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
854 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
855 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
858 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
859 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
860 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
861 from = build_ptrmemfunc_type (build_pointer_type (from));
862 conv = build_conv (PMEM_CONV, from, conv);
864 else if (tcode == BOOLEAN_TYPE)
866 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
867 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
870 conv = build_conv (STD_CONV, to, conv);
871 if (fcode == POINTER_TYPE
872 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
873 ICS_STD_RANK (conv) = PBOOL_RANK;
875 /* We don't check for ENUMERAL_TYPE here because there are no standard
876 conversions to enum type. */
877 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
878 || tcode == REAL_TYPE)
880 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
882 conv = build_conv (STD_CONV, to, conv);
884 /* Give this a better rank if it's a promotion. */
885 if (to == type_promotes_to (from)
886 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
887 ICS_STD_RANK (conv) = PROMO_RANK;
889 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
890 && DERIVED_FROM_P (to, from))
892 if (TREE_CODE (conv) == RVALUE_CONV)
893 conv = TREE_OPERAND (conv, 0);
894 conv = build_conv (BASE_CONV, to, conv);
902 /* Returns the conversion path from type FROM to reference type TO for
903 purposes of reference binding. For lvalue binding, either pass a
904 reference type to FROM or an lvalue expression to EXPR.
906 Currently does not distinguish in the generated trees between binding to
907 an lvalue and a temporary. Should it? */
910 reference_binding (rto, rfrom, expr, flags)
911 tree rto, rfrom, expr;
916 tree to = TREE_TYPE (rto);
920 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
922 expr = instantiate_type (to, expr, 0);
923 if (expr == error_mark_node)
925 from = TREE_TYPE (expr);
928 if (TREE_CODE (from) == REFERENCE_TYPE)
929 from = TREE_TYPE (from);
930 else if (! expr || ! real_lvalue_p (expr))
933 related = (same_type_p (TYPE_MAIN_VARIANT (to),
934 TYPE_MAIN_VARIANT (from))
935 || (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
936 && DERIVED_FROM_P (to, from)));
938 if (lvalue && related && at_least_as_qualified_p (to, from))
940 conv = build1 (IDENTITY_CONV, from, expr);
942 if (same_type_p (TYPE_MAIN_VARIANT (to),
943 TYPE_MAIN_VARIANT (from)))
944 conv = build_conv (REF_BIND, rto, conv);
947 conv = build_conv (REF_BIND, rto, conv);
948 ICS_STD_RANK (conv) = STD_RANK;
956 conv = standard_conversion (to, rfrom, expr);
959 conv = build_conv (REF_BIND, rto, conv);
961 /* Bind directly to a base subobject of a class rvalue. Do it
962 after building the conversion for proper handling of ICS_RANK. */
963 if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
964 TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
967 && ((! (CP_TYPE_CONST_NON_VOLATILE_P (to)
968 && (flags & LOOKUP_NO_TEMP_BIND) == 0))
969 /* If T1 is reference-related to T2, cv1 must be the same
970 cv-qualification as, or greater cv-qualification than,
971 cv2; otherwise, the program is ill-formed. */
972 || (related && !at_least_as_qualified_p (to, from))))
973 ICS_BAD_FLAG (conv) = 1;
979 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
980 to type TO. The optional expression EXPR may affect the conversion.
981 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
985 implicit_conversion (to, from, expr, flags)
990 struct z_candidate *cand;
992 if (TREE_CODE (to) == REFERENCE_TYPE)
993 conv = reference_binding (to, from, expr, flags);
995 conv = standard_conversion (to, from, expr);
999 else if (expr != NULL_TREE
1000 && (IS_AGGR_TYPE (non_reference (from))
1001 || IS_AGGR_TYPE (non_reference (to)))
1002 && (flags & LOOKUP_NO_CONVERSION) == 0)
1004 cand = build_user_type_conversion_1
1005 (to, expr, LOOKUP_ONLYCONVERTING);
1007 conv = cand->second_conv;
1008 if ((! conv || ICS_BAD_FLAG (conv))
1009 && TREE_CODE (to) == REFERENCE_TYPE
1010 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1012 cand = build_user_type_conversion_1
1013 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1016 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to)))
1017 ICS_BAD_FLAG (cand->second_conv) = 1;
1018 if (!conv || (ICS_BAD_FLAG (conv)
1019 > ICS_BAD_FLAG (cand->second_conv)))
1020 conv = build_conv (REF_BIND, to, cand->second_conv);
1028 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1031 static struct z_candidate *
1032 add_candidate (candidates, fn, convs, viable)
1033 struct z_candidate *candidates;
1037 struct z_candidate *cand
1038 = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
1041 cand->convs = convs;
1042 cand->second_conv = NULL_TREE;
1043 cand->viable = viable;
1044 cand->basetype_path = NULL_TREE;
1045 cand->template = NULL_TREE;
1046 cand->warnings = NULL_TREE;
1047 cand->next = candidates;
1052 /* Create an overload candidate for the function or method FN called with
1053 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1054 to implicit_conversion. */
1056 static struct z_candidate *
1057 add_function_candidate (candidates, fn, arglist, flags)
1058 struct z_candidate *candidates;
1062 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1065 tree parmnode, argnode;
1068 /* The `this', `in_chrg', and `vlist' arguments to constructors are
1069 not considered in overload resolution. */
1070 if (DECL_CONSTRUCTOR_P (fn))
1072 parmlist = TREE_CHAIN (parmlist);
1073 arglist = TREE_CHAIN (arglist);
1074 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1076 parmlist = TREE_CHAIN (parmlist);
1077 arglist = TREE_CHAIN (arglist);
1079 if ((flags & LOOKUP_HAS_VLIST)
1080 && DECL_CONSTRUCTOR_FOR_PVBASE_P (fn))
1082 parmlist = TREE_CHAIN (parmlist);
1083 arglist = TREE_CHAIN (arglist);
1085 else if (!(flags & LOOKUP_HAS_VLIST)
1086 && !DECL_CONSTRUCTOR_FOR_PVBASE_P (fn))
1090 /* The ctor expects a vlist and the arguments don't have
1091 one, or vice versa, so fn is not even a candidate, since
1092 the corresponding ctor would be the candidate. */
1097 len = list_length (arglist);
1098 convs = make_scratch_vec (len);
1100 /* 13.3.2 - Viable functions [over.match.viable]
1101 First, to be a viable function, a candidate function shall have enough
1102 parameters to agree in number with the arguments in the list.
1104 We need to check this first; otherwise, checking the ICSes might cause
1105 us to produce an ill-formed template instantiation. */
1107 parmnode = parmlist;
1108 for (i = 0; i < len; ++i)
1110 if (parmnode == NULL_TREE || parmnode == void_list_node)
1112 parmnode = TREE_CHAIN (parmnode);
1115 if (i < len && parmnode)
1118 /* Make sure there are default args for the rest of the parms. */
1119 else for (; parmnode && parmnode != void_list_node;
1120 parmnode = TREE_CHAIN (parmnode))
1121 if (! TREE_PURPOSE (parmnode))
1130 /* Second, for F to be a viable function, there shall exist for each
1131 argument an implicit conversion sequence that converts that argument
1132 to the corresponding parameter of F. */
1134 parmnode = parmlist;
1137 for (i = 0; i < len; ++i)
1139 tree arg = TREE_VALUE (argnode);
1140 tree argtype = lvalue_type (arg);
1143 if (parmnode == void_list_node)
1148 tree parmtype = TREE_VALUE (parmnode);
1150 /* [over.match.funcs] For conversion functions, the function is
1151 considered to be a member of the class of the implicit object
1152 argument for the purpose of defining the type of the implicit
1155 Since build_over_call ignores the ICS for the `this' parameter,
1156 we can just change the parm type. */
1157 if (DECL_CONV_FN_P (fn) && i == 0)
1160 = build_qualified_type (TREE_TYPE (argtype),
1161 TYPE_QUALS (TREE_TYPE (parmtype)));
1162 parmtype = build_pointer_type (parmtype);
1165 t = implicit_conversion (parmtype, argtype, arg, flags);
1169 t = build1 (IDENTITY_CONV, argtype, arg);
1170 ICS_ELLIPSIS_FLAG (t) = 1;
1173 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1174 && ! DECL_CONSTRUCTOR_P (fn))
1175 ICS_THIS_FLAG (t) = 1;
1177 TREE_VEC_ELT (convs, i) = t;
1184 if (ICS_BAD_FLAG (t))
1188 parmnode = TREE_CHAIN (parmnode);
1189 argnode = TREE_CHAIN (argnode);
1193 return add_candidate (candidates, fn, convs, viable);
1196 /* Create an overload candidate for the conversion function FN which will
1197 be invoked for expression OBJ, producing a pointer-to-function which
1198 will in turn be called with the argument list ARGLIST, and add it to
1199 CANDIDATES. FLAGS is passed on to implicit_conversion.
1201 Actually, we don't really care about FN; we care about the type it
1202 converts to. There may be multiple conversion functions that will
1203 convert to that type, and we rely on build_user_type_conversion_1 to
1204 choose the best one; so when we create our candidate, we record the type
1205 instead of the function. */
1207 static struct z_candidate *
1208 add_conv_candidate (candidates, fn, obj, arglist)
1209 struct z_candidate *candidates;
1210 tree fn, obj, arglist;
1212 tree totype = TREE_TYPE (TREE_TYPE (fn));
1213 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1214 int i, len = list_length (arglist) + 1;
1215 tree convs = make_scratch_vec (len);
1216 tree parmnode = parmlist;
1217 tree argnode = arglist;
1219 int flags = LOOKUP_NORMAL;
1221 /* Don't bother looking up the same type twice. */
1222 if (candidates && candidates->fn == totype)
1225 for (i = 0; i < len; ++i)
1227 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1228 tree argtype = lvalue_type (arg);
1232 t = implicit_conversion (totype, argtype, arg, flags);
1233 else if (parmnode == void_list_node)
1236 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1239 t = build1 (IDENTITY_CONV, argtype, arg);
1240 ICS_ELLIPSIS_FLAG (t) = 1;
1243 TREE_VEC_ELT (convs, i) = t;
1247 if (ICS_BAD_FLAG (t))
1254 parmnode = TREE_CHAIN (parmnode);
1255 argnode = TREE_CHAIN (argnode);
1261 for (; parmnode && parmnode != void_list_node;
1262 parmnode = TREE_CHAIN (parmnode))
1263 if (! TREE_PURPOSE (parmnode))
1269 return add_candidate (candidates, totype, convs, viable);
1272 static struct z_candidate *
1273 build_builtin_candidate (candidates, fnname, type1, type2,
1274 args, argtypes, flags)
1275 struct z_candidate *candidates;
1276 tree fnname, type1, type2, *args, *argtypes;
1287 convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1289 for (i = 0; i < 2; ++i)
1294 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1298 /* We need something for printing the candidate. */
1299 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1301 else if (ICS_BAD_FLAG (t))
1303 TREE_VEC_ELT (convs, i) = t;
1306 /* For COND_EXPR we rearranged the arguments; undo that now. */
1309 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1310 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1311 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1313 TREE_VEC_ELT (convs, 0) = t;
1318 return add_candidate (candidates, fnname, convs, viable);
1325 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1328 /* Create any builtin operator overload candidates for the operator in
1329 question given the converted operand types TYPE1 and TYPE2. The other
1330 args are passed through from add_builtin_candidates to
1331 build_builtin_candidate. */
1333 static struct z_candidate *
1334 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1335 args, argtypes, flags)
1336 struct z_candidate *candidates;
1337 enum tree_code code, code2;
1338 tree fnname, type1, type2, *args, *argtypes;
1343 case POSTINCREMENT_EXPR:
1344 case POSTDECREMENT_EXPR:
1345 args[1] = integer_zero_node;
1346 type2 = integer_type_node;
1355 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1356 and VQ is either volatile or empty, there exist candidate operator
1357 functions of the form
1358 VQ T& operator++(VQ T&);
1359 T operator++(VQ T&, int);
1360 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1361 type other than bool, and VQ is either volatile or empty, there exist
1362 candidate operator functions of the form
1363 VQ T& operator--(VQ T&);
1364 T operator--(VQ T&, int);
1365 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1366 complete object type, and VQ is either volatile or empty, there exist
1367 candidate operator functions of the form
1368 T*VQ& operator++(T*VQ&);
1369 T*VQ& operator--(T*VQ&);
1370 T* operator++(T*VQ&, int);
1371 T* operator--(T*VQ&, int); */
1373 case POSTDECREMENT_EXPR:
1374 case PREDECREMENT_EXPR:
1375 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1377 case POSTINCREMENT_EXPR:
1378 case PREINCREMENT_EXPR:
1379 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1380 || TYPE_PTROB_P (type1))
1382 type1 = build_reference_type (type1);
1387 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1388 exist candidate operator functions of the form
1392 8 For every function type T, there exist candidate operator functions of
1394 T& operator*(T*); */
1397 if (TREE_CODE (type1) == POINTER_TYPE
1398 && (TYPE_PTROB_P (type1)
1399 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1403 /* 9 For every type T, there exist candidate operator functions of the form
1406 10For every promoted arithmetic type T, there exist candidate operator
1407 functions of the form
1411 case CONVERT_EXPR: /* unary + */
1412 if (TREE_CODE (type1) == POINTER_TYPE
1413 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1416 if (ARITHMETIC_TYPE_P (type1))
1420 /* 11For every promoted integral type T, there exist candidate operator
1421 functions of the form
1425 if (INTEGRAL_TYPE_P (type1))
1429 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1430 is the same type as C2 or is a derived class of C2, T is a complete
1431 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1432 there exist candidate operator functions of the form
1433 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1434 where CV12 is the union of CV1 and CV2. */
1437 if (TREE_CODE (type1) == POINTER_TYPE
1438 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1440 tree c1 = TREE_TYPE (type1);
1441 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1442 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1443 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1445 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1446 && (TYPE_PTRMEMFUNC_P (type2)
1447 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1452 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1453 didate operator functions of the form
1458 bool operator<(L, R);
1459 bool operator>(L, R);
1460 bool operator<=(L, R);
1461 bool operator>=(L, R);
1462 bool operator==(L, R);
1463 bool operator!=(L, R);
1464 where LR is the result of the usual arithmetic conversions between
1467 14For every pair of types T and I, where T is a cv-qualified or cv-
1468 unqualified complete object type and I is a promoted integral type,
1469 there exist candidate operator functions of the form
1470 T* operator+(T*, I);
1471 T& operator[](T*, I);
1472 T* operator-(T*, I);
1473 T* operator+(I, T*);
1474 T& operator[](I, T*);
1476 15For every T, where T is a pointer to complete object type, there exist
1477 candidate operator functions of the form112)
1478 ptrdiff_t operator-(T, T);
1480 16For every pointer type T, there exist candidate operator functions of
1482 bool operator<(T, T);
1483 bool operator>(T, T);
1484 bool operator<=(T, T);
1485 bool operator>=(T, T);
1486 bool operator==(T, T);
1487 bool operator!=(T, T);
1489 17For every pointer to member type T, there exist candidate operator
1490 functions of the form
1491 bool operator==(T, T);
1492 bool operator!=(T, T); */
1495 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1497 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1499 type2 = ptrdiff_type_node;
1503 case TRUNC_DIV_EXPR:
1504 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1510 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1511 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1513 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1514 && null_ptr_cst_p (args[1]))
1519 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1520 && null_ptr_cst_p (args[0]))
1531 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1532 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1534 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1539 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1547 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1550 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1552 type1 = ptrdiff_type_node;
1555 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1557 type2 = ptrdiff_type_node;
1562 /* 18For every pair of promoted integral types L and R, there exist candi-
1563 date operator functions of the form
1570 where LR is the result of the usual arithmetic conversions between
1573 case TRUNC_MOD_EXPR:
1579 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1583 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1584 type, VQ is either volatile or empty, and R is a promoted arithmetic
1585 type, there exist candidate operator functions of the form
1586 VQ L& operator=(VQ L&, R);
1587 VQ L& operator*=(VQ L&, R);
1588 VQ L& operator/=(VQ L&, R);
1589 VQ L& operator+=(VQ L&, R);
1590 VQ L& operator-=(VQ L&, R);
1592 20For every pair T, VQ), where T is any type and VQ is either volatile
1593 or empty, there exist candidate operator functions of the form
1594 T*VQ& operator=(T*VQ&, T*);
1596 21For every pair T, VQ), where T is a pointer to member type and VQ is
1597 either volatile or empty, there exist candidate operator functions of
1599 VQ T& operator=(VQ T&, T);
1601 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1602 unqualified complete object type, VQ is either volatile or empty, and
1603 I is a promoted integral type, there exist candidate operator func-
1605 T*VQ& operator+=(T*VQ&, I);
1606 T*VQ& operator-=(T*VQ&, I);
1608 23For every triple L, VQ, R), where L is an integral or enumeration
1609 type, VQ is either volatile or empty, and R is a promoted integral
1610 type, there exist candidate operator functions of the form
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);
1616 VQ L& operator^=(VQ L&, R);
1617 VQ L& operator|=(VQ L&, R); */
1624 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1626 type2 = ptrdiff_type_node;
1630 case TRUNC_DIV_EXPR:
1631 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1635 case TRUNC_MOD_EXPR:
1641 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1646 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1648 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1649 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1650 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1651 || ((TYPE_PTRMEMFUNC_P (type1)
1652 || TREE_CODE (type1) == POINTER_TYPE)
1653 && null_ptr_cst_p (args[1])))
1661 my_friendly_abort (367);
1663 type1 = build_reference_type (type1);
1667 /* Kludge around broken overloading rules whereby
1668 bool ? const char& : enum is ambiguous
1669 (between int and const char&). */
1670 flags |= LOOKUP_NO_TEMP_BIND;
1672 /* Extension: Support ?: of enumeral type. Hopefully this will not
1673 be an extension for long. */
1674 if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
1676 else if (TREE_CODE (type1) == ENUMERAL_TYPE
1677 || TREE_CODE (type2) == ENUMERAL_TYPE)
1679 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1681 if (TREE_CODE (type1) == TREE_CODE (type2)
1682 && (TREE_CODE (type1) == REFERENCE_TYPE
1683 || TREE_CODE (type1) == POINTER_TYPE
1684 || TYPE_PTRMEMFUNC_P (type1)
1685 || IS_AGGR_TYPE (type1)))
1687 if (TREE_CODE (type1) == REFERENCE_TYPE
1688 || TREE_CODE (type2) == REFERENCE_TYPE)
1690 if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
1691 && null_ptr_cst_p (args[1]))
1692 || IS_AGGR_TYPE (type1))
1697 if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
1698 && null_ptr_cst_p (args[0]))
1699 || IS_AGGR_TYPE (type2))
1707 my_friendly_abort (367);
1710 /* If we're dealing with two pointer types, we need candidates
1711 for both of them. */
1712 if (type2 && type1 != type2
1713 && TREE_CODE (type1) == TREE_CODE (type2)
1714 && (TREE_CODE (type1) == REFERENCE_TYPE
1715 || (TREE_CODE (type1) == POINTER_TYPE
1716 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1717 || TYPE_PTRMEMFUNC_P (type1)
1718 || IS_AGGR_TYPE (type1)))
1720 candidates = build_builtin_candidate
1721 (candidates, fnname, type1, type1, args, argtypes, flags);
1722 return build_builtin_candidate
1723 (candidates, fnname, type2, type2, args, argtypes, flags);
1726 return build_builtin_candidate
1727 (candidates, fnname, type1, type2, args, argtypes, flags);
1731 type_decays_to (type)
1734 if (TREE_CODE (type) == ARRAY_TYPE)
1735 return build_pointer_type (TREE_TYPE (type));
1736 if (TREE_CODE (type) == FUNCTION_TYPE)
1737 return build_pointer_type (type);
1741 /* There are three conditions of builtin candidates:
1743 1) bool-taking candidates. These are the same regardless of the input.
1744 2) pointer-pair taking candidates. These are generated for each type
1745 one of the input types converts to.
1746 3) arithmetic candidates. According to the WP, we should generate
1747 all of these, but I'm trying not to... */
1749 static struct z_candidate *
1750 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1751 struct z_candidate *candidates;
1752 enum tree_code code, code2;
1757 tree type, argtypes[3], types[2];
1759 for (i = 0; i < 3; ++i)
1762 argtypes[i] = lvalue_type (args[i]);
1764 argtypes[i] = NULL_TREE;
1769 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1770 and VQ is either volatile or empty, there exist candidate operator
1771 functions of the form
1772 VQ T& operator++(VQ T&); */
1774 case POSTINCREMENT_EXPR:
1775 case PREINCREMENT_EXPR:
1776 case POSTDECREMENT_EXPR:
1777 case PREDECREMENT_EXPR:
1782 /* 24There also exist candidate operator functions of the form
1783 bool operator!(bool);
1784 bool operator&&(bool, bool);
1785 bool operator||(bool, bool); */
1787 case TRUTH_NOT_EXPR:
1788 return build_builtin_candidate
1789 (candidates, fnname, boolean_type_node,
1790 NULL_TREE, args, argtypes, flags);
1792 case TRUTH_ORIF_EXPR:
1793 case TRUTH_ANDIF_EXPR:
1794 return build_builtin_candidate
1795 (candidates, fnname, boolean_type_node,
1796 boolean_type_node, args, argtypes, flags);
1807 types[0] = types[1] = NULL_TREE;
1809 for (i = 0; i < 2; ++i)
1813 else if (IS_AGGR_TYPE (argtypes[i]))
1817 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1820 convs = lookup_conversions (argtypes[i]);
1822 if (code == COND_EXPR)
1824 if (real_lvalue_p (args[i]))
1825 types[i] = scratch_tree_cons
1826 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1828 types[i] = scratch_tree_cons
1829 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1835 for (; convs; convs = TREE_CHAIN (convs))
1837 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
1840 && (TREE_CODE (type) != REFERENCE_TYPE
1841 || CP_TYPE_CONST_P (TREE_TYPE (type))))
1844 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1845 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1847 type = non_reference (type);
1848 if (i != 0 || ! ref1)
1850 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1851 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1852 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1853 if (INTEGRAL_TYPE_P (type))
1854 type = type_promotes_to (type);
1857 if (! value_member (type, types[i]))
1858 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1863 if (code == COND_EXPR && real_lvalue_p (args[i]))
1864 types[i] = scratch_tree_cons
1865 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1866 type = non_reference (argtypes[i]);
1867 if (i != 0 || ! ref1)
1869 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1870 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1871 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1872 if (INTEGRAL_TYPE_P (type))
1873 type = type_promotes_to (type);
1875 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1879 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1882 for (type = types[1]; type; type = TREE_CHAIN (type))
1883 candidates = add_builtin_candidate
1884 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1885 TREE_VALUE (type), args, argtypes, flags);
1887 candidates = add_builtin_candidate
1888 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1889 NULL_TREE, args, argtypes, flags);
1896 /* If TMPL can be successfully instantiated as indicated by
1897 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
1899 TMPL is the template. EXPLICIT_TARGS are any explicit template
1900 arguments. ARGLIST is the arguments provided at the call-site.
1901 The RETURN_TYPE is the desired type for conversion operators. If
1902 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
1903 OBJ is supplied, FLAGS are ignored, and OBJ is as for
1904 add_conv_candidate. */
1906 static struct z_candidate*
1907 add_template_candidate_real (candidates, tmpl, explicit_targs,
1908 arglist, return_type, flags,
1910 struct z_candidate *candidates;
1911 tree tmpl, explicit_targs, arglist, return_type;
1914 unification_kind_t strict;
1916 int ntparms = DECL_NTPARMS (tmpl);
1917 tree targs = make_scratch_vec (ntparms);
1918 struct z_candidate *cand;
1922 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
1923 return_type, strict);
1928 fn = instantiate_template (tmpl, targs);
1929 if (fn == error_mark_node)
1932 if (obj != NULL_TREE)
1933 /* Aha, this is a conversion function. */
1934 cand = add_conv_candidate (candidates, fn, obj, arglist);
1936 cand = add_function_candidate (candidates, fn, arglist, flags);
1937 if (DECL_TI_TEMPLATE (fn) != tmpl)
1938 /* This situation can occur if a member template of a template
1939 class is specialized. Then, instantiate_template might return
1940 an instantiation of the specialization, in which case the
1941 DECL_TI_TEMPLATE field will point at the original
1942 specialization. For example:
1944 template <class T> struct S { template <class U> void f(U);
1945 template <> void f(int) {}; };
1949 Here, TMPL will be template <class U> S<double>::f(U).
1950 And, instantiate template will give us the specialization
1951 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
1952 for this will point at template <class T> template <> S<T>::f(int),
1953 so that we can find the definition. For the purposes of
1954 overload resolution, however, we want the original TMPL. */
1955 cand->template = tree_cons (tmpl, targs, NULL_TREE);
1957 cand->template = DECL_TEMPLATE_INFO (fn);
1963 static struct z_candidate *
1964 add_template_candidate (candidates, tmpl, explicit_targs,
1965 arglist, return_type, flags, strict)
1966 struct z_candidate *candidates;
1967 tree tmpl, explicit_targs, arglist, return_type;
1969 unification_kind_t strict;
1972 add_template_candidate_real (candidates, tmpl, explicit_targs,
1973 arglist, return_type, flags,
1978 static struct z_candidate *
1979 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
1980 struct z_candidate *candidates;
1981 tree tmpl, obj, arglist, return_type;
1984 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
1985 return_type, 0, obj, DEDUCE_CONV);
1991 struct z_candidate *cands;
1993 for (; cands; cands = cands->next)
1994 if (pedantic ? cands->viable == 1 : cands->viable)
1999 static struct z_candidate *
2000 splice_viable (cands)
2001 struct z_candidate *cands;
2003 struct z_candidate **p = &cands;
2007 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2020 /* Fix this to work on non-lvalues. */
2021 if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
2022 || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
2025 return build_unary_op (ADDR_EXPR, obj, 0);
2029 print_z_candidates (candidates)
2030 struct z_candidate *candidates;
2032 const char *str = "candidates are:";
2033 for (; candidates; candidates = candidates->next)
2035 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2037 if (candidates->fn == ansi_opname [COND_EXPR])
2038 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2039 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2040 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2041 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2042 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2043 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2044 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2045 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2047 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2048 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2050 else if (TYPE_P (candidates->fn))
2051 cp_error ("%s %T <conversion>", str, candidates->fn);
2053 cp_error_at ("%s %+#D%s", str, candidates->fn,
2054 candidates->viable == -1 ? " <near match>" : "");
2059 /* Returns the best overload candidate to perform the requested
2060 conversion. This function is used for three the overloading situations
2061 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2062 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2063 per [dcl.init.ref], so we ignore temporary bindings. */
2065 static struct z_candidate *
2066 build_user_type_conversion_1 (totype, expr, flags)
2070 struct z_candidate *candidates, *cand;
2071 tree fromtype = TREE_TYPE (expr);
2072 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2073 tree args = NULL_TREE;
2074 tree templates = NULL_TREE;
2076 if (IS_AGGR_TYPE (totype))
2077 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2078 if (IS_AGGR_TYPE (fromtype)
2079 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2080 convs = lookup_conversions (fromtype);
2083 flags |= LOOKUP_NO_CONVERSION;
2087 tree t = build_int_2 (0, 0);
2088 TREE_TYPE (t) = build_pointer_type (totype);
2089 args = build_scratch_list (NULL_TREE, expr);
2090 if (TYPE_USES_PVBASES (totype) && !flag_vtable_thunks_compat)
2092 args = scratch_tree_cons (NULL_TREE, vlist_zero_node, args);
2093 flags |= LOOKUP_HAS_VLIST;
2095 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2096 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2097 args = scratch_tree_cons (NULL_TREE, t, args);
2099 ctors = TREE_VALUE (ctors);
2101 for (; ctors; ctors = OVL_NEXT (ctors))
2103 tree ctor = OVL_CURRENT (ctors);
2104 if (DECL_NONCONVERTING_P (ctor))
2107 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2109 templates = scratch_tree_cons (NULL_TREE, ctor, templates);
2111 add_template_candidate (candidates, ctor,
2112 NULL_TREE, args, NULL_TREE, flags,
2116 candidates = add_function_candidate (candidates, ctor,
2121 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2122 candidates->basetype_path = TYPE_BINFO (totype);
2127 args = build_scratch_list (NULL_TREE, build_this (expr));
2129 for (; convs; convs = TREE_CHAIN (convs))
2131 tree fns = TREE_VALUE (convs);
2132 int convflags = LOOKUP_NO_CONVERSION;
2135 /* If we are called to convert to a reference type, we are trying to
2136 find an lvalue binding, so don't even consider temporaries. If
2137 we don't find an lvalue binding, the caller will try again to
2138 look for a temporary binding. */
2139 if (TREE_CODE (totype) == REFERENCE_TYPE)
2140 convflags |= LOOKUP_NO_TEMP_BIND;
2142 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2143 ics = implicit_conversion
2144 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2146 /* We can't compute this yet. */
2147 ics = error_mark_node;
2149 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2150 /* ignore the near match. */;
2152 for (; fns; fns = OVL_NEXT (fns))
2154 tree fn = OVL_CURRENT (fns);
2155 struct z_candidate *old_candidates = candidates;
2157 if (TREE_CODE (fn) == TEMPLATE_DECL)
2159 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2161 add_template_candidate (candidates, fn, NULL_TREE,
2162 args, totype, flags,
2166 candidates = add_function_candidate (candidates, fn,
2169 if (candidates != old_candidates)
2171 if (TREE_CODE (fn) == TEMPLATE_DECL)
2172 ics = implicit_conversion
2173 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2176 candidates->second_conv = ics;
2177 candidates->basetype_path = TREE_PURPOSE (convs);
2179 if (ics == NULL_TREE)
2180 candidates->viable = 0;
2181 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2182 candidates->viable = -1;
2187 if (! any_viable (candidates))
2190 if (flags & LOOKUP_COMPLAIN)
2192 if (candidates && ! candidates->next)
2193 /* say why this one won't work or try to be loose */;
2195 cp_error ("no viable candidates");
2202 candidates = splice_viable (candidates);
2203 cand = tourney (candidates);
2207 if (flags & LOOKUP_COMPLAIN)
2209 cp_error ("conversion from `%T' to `%T' is ambiguous",
2211 print_z_candidates (candidates);
2214 cand = candidates; /* any one will do */
2215 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2216 ICS_USER_FLAG (cand->second_conv) = 1;
2217 ICS_BAD_FLAG (cand->second_conv) = 1;
2222 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2223 p = &(TREE_OPERAND (*p, 0));
2225 /* Pedantically, normal function declarations are never considered
2226 to refer to template instantiations, so we only do this with
2228 if (flag_guiding_decls && templates && ! cand->template
2229 && !DECL_INITIAL (cand->fn)
2230 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2231 add_maybe_template (cand->fn, templates);
2235 (DECL_CONSTRUCTOR_P (cand->fn)
2236 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2237 expr, build_expr_ptr_wrapper (cand));
2238 ICS_USER_FLAG (cand->second_conv) = 1;
2239 if (cand->viable == -1)
2240 ICS_BAD_FLAG (cand->second_conv) = 1;
2246 build_user_type_conversion (totype, expr, flags)
2250 struct z_candidate *cand
2251 = build_user_type_conversion_1 (totype, expr, flags);
2255 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2256 return error_mark_node;
2257 return convert_from_reference (convert_like (cand->second_conv, expr));
2262 /* Do any initial processing on the arguments to a function call. */
2269 for (t = args; t; t = TREE_CHAIN (t))
2271 if (TREE_VALUE (t) == error_mark_node)
2272 return error_mark_node;
2273 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2275 error ("invalid use of void expression");
2276 return error_mark_node;
2278 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2279 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2285 build_new_function_call (fn, args)
2288 struct z_candidate *candidates = 0, *cand;
2289 tree explicit_targs = NULL_TREE;
2290 int template_only = 0;
2292 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2294 explicit_targs = TREE_OPERAND (fn, 1);
2295 fn = TREE_OPERAND (fn, 0);
2299 if (really_overloaded_fn (fn))
2302 tree templates = NULL_TREE;
2304 args = resolve_args (args);
2306 if (args == error_mark_node)
2307 return error_mark_node;
2309 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2311 tree t = OVL_FUNCTION (t1);
2312 struct z_candidate *old_candidates = candidates;
2314 if (TREE_CODE (t) == TEMPLATE_DECL)
2316 templates = scratch_tree_cons (NULL_TREE, t, templates);
2317 candidates = add_template_candidate
2318 (candidates, t, explicit_targs, args, NULL_TREE,
2319 LOOKUP_NORMAL, DEDUCE_CALL);
2321 else if (! template_only)
2322 candidates = add_function_candidate
2323 (candidates, t, args, LOOKUP_NORMAL);
2325 if (candidates != old_candidates)
2326 candidates->basetype_path = DECL_REAL_CONTEXT (t);
2329 if (! any_viable (candidates))
2331 if (candidates && ! candidates->next)
2332 return build_function_call (candidates->fn, args);
2333 cp_error ("no matching function for call to `%D (%A)'",
2334 DECL_NAME (OVL_FUNCTION (fn)), args);
2336 print_z_candidates (candidates);
2337 return error_mark_node;
2339 candidates = splice_viable (candidates);
2340 cand = tourney (candidates);
2344 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2345 DECL_NAME (OVL_FUNCTION (fn)), args);
2346 print_z_candidates (candidates);
2347 return error_mark_node;
2350 /* Pedantically, normal function declarations are never considered
2351 to refer to template instantiations, so we only do this with
2353 if (flag_guiding_decls && templates && ! cand->template
2354 && ! DECL_INITIAL (cand->fn))
2355 add_maybe_template (cand->fn, templates);
2357 return build_over_call (cand, args, LOOKUP_NORMAL);
2360 /* This is not really overloaded. */
2361 fn = OVL_CURRENT (fn);
2363 return build_function_call (fn, args);
2367 build_object_call (obj, args)
2370 struct z_candidate *candidates = 0, *cand;
2371 tree fns, convs, mem_args = NULL_TREE;
2372 tree type = TREE_TYPE (obj);
2374 if (TYPE_PTRMEMFUNC_P (type))
2376 /* It's no good looking for an overloaded operator() on a
2377 pointer-to-member-function. */
2378 cp_error ("pointer-to-member function %E cannot be called", obj);
2379 cp_error ("without an object; consider using .* or ->*");
2380 return error_mark_node;
2383 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2384 if (fns == error_mark_node)
2385 return error_mark_node;
2387 args = resolve_args (args);
2389 if (args == error_mark_node)
2390 return error_mark_node;
2394 tree base = TREE_PURPOSE (fns);
2395 mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2397 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2399 tree fn = OVL_CURRENT (fns);
2400 if (TREE_CODE (fn) == TEMPLATE_DECL)
2403 = add_template_candidate (candidates, fn, NULL_TREE,
2404 mem_args, NULL_TREE,
2405 LOOKUP_NORMAL, DEDUCE_CALL);
2408 candidates = add_function_candidate
2409 (candidates, fn, mem_args, LOOKUP_NORMAL);
2412 candidates->basetype_path = base;
2416 convs = lookup_conversions (type);
2418 for (; convs; convs = TREE_CHAIN (convs))
2420 tree fns = TREE_VALUE (convs);
2421 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2423 if ((TREE_CODE (totype) == POINTER_TYPE
2424 || TREE_CODE (totype) == REFERENCE_TYPE)
2425 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2426 for (; fns; fns = OVL_NEXT (fns))
2428 tree fn = OVL_CURRENT (fns);
2429 if (TREE_CODE (fn) == TEMPLATE_DECL)
2431 candidates = add_template_conv_candidate (candidates,
2438 candidates = add_conv_candidate (candidates, fn, obj, args);
2441 candidates->basetype_path = TREE_PURPOSE (convs);
2445 if (! any_viable (candidates))
2447 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2448 print_z_candidates (candidates);
2449 return error_mark_node;
2452 candidates = splice_viable (candidates);
2453 cand = tourney (candidates);
2457 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2458 print_z_candidates (candidates);
2459 return error_mark_node;
2462 /* Since cand->fn will be a type, not a function, for a conversion
2463 function, we must be careful not to unconditionally look at
2465 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2466 && DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2467 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2469 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2472 return build_function_call (obj, args);
2476 op_error (code, code2, arg1, arg2, arg3, problem)
2477 enum tree_code code, code2;
2478 tree arg1, arg2, arg3;
2479 const char *problem;
2482 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2487 cp_error ("%s for `%T ? %T : %T'", problem,
2488 error_type (arg1), error_type (arg2), error_type (arg3));
2490 case POSTINCREMENT_EXPR:
2491 case POSTDECREMENT_EXPR:
2492 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2495 cp_error ("%s for `%T[%T]'", problem,
2496 error_type (arg1), error_type (arg2));
2500 cp_error ("%s for `%T %s %T'", problem,
2501 error_type (arg1), opname, error_type (arg2));
2503 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2508 build_new_op (code, flags, arg1, arg2, arg3)
2509 enum tree_code code;
2511 tree arg1, arg2, arg3;
2513 struct z_candidate *candidates = 0, *cand;
2514 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
2515 enum tree_code code2 = NOP_EXPR;
2516 tree templates = NULL_TREE;
2519 if (arg1 == error_mark_node
2520 || arg2 == error_mark_node
2521 || arg3 == error_mark_node)
2522 return error_mark_node;
2524 /* This can happen if a template takes all non-type parameters, e.g.
2525 undeclared_template<1, 5, 72>a; */
2526 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
2528 cp_error ("`%D' must be declared before use", arg1);
2529 return error_mark_node;
2532 if (code == MODIFY_EXPR)
2534 code2 = TREE_CODE (arg3);
2536 fnname = ansi_assopname[code2];
2539 fnname = ansi_opname[code];
2545 case VEC_DELETE_EXPR:
2547 /* Use build_op_new_call and build_op_delete_call instead. */
2548 my_friendly_abort (981018);
2551 return build_object_call (arg1, arg2);
2557 /* The comma operator can have void args. */
2558 if (TREE_CODE (arg1) == OFFSET_REF)
2559 arg1 = resolve_offset_ref (arg1);
2560 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
2561 arg2 = resolve_offset_ref (arg2);
2562 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
2563 arg3 = resolve_offset_ref (arg3);
2565 if (code == COND_EXPR)
2567 if (arg2 == NULL_TREE
2568 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
2569 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
2570 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
2571 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
2574 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
2575 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
2578 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2579 arg2 = integer_zero_node;
2582 arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
2583 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
2585 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2587 arglist = build_scratch_list (NULL_TREE, arg1);
2589 fns = lookup_function_nonclass (fnname, arglist);
2591 if (fns && TREE_CODE (fns) == TREE_LIST)
2592 fns = TREE_VALUE (fns);
2593 for (; fns; fns = OVL_NEXT (fns))
2595 tree fn = OVL_CURRENT (fns);
2596 if (TREE_CODE (fn) == TEMPLATE_DECL)
2598 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2600 = add_template_candidate (candidates, fn, NULL_TREE,
2601 arglist, TREE_TYPE (fnname),
2602 flags, DEDUCE_CALL);
2605 candidates = add_function_candidate (candidates, fn, arglist, flags);
2608 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
2610 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
2611 if (fns == error_mark_node)
2619 tree basetype = TREE_PURPOSE (fns);
2620 mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
2621 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2623 tree fn = OVL_CURRENT (fns);
2626 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
2627 this_arglist = mem_arglist;
2629 this_arglist = arglist;
2631 if (TREE_CODE (fn) == TEMPLATE_DECL)
2633 /* A member template. */
2634 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2636 = add_template_candidate (candidates, fn, NULL_TREE,
2637 this_arglist, TREE_TYPE (fnname),
2638 flags, DEDUCE_CALL);
2641 candidates = add_function_candidate
2642 (candidates, fn, this_arglist, flags);
2645 candidates->basetype_path = basetype;
2652 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
2653 to know about two args; a builtin candidate will always have a first
2654 parameter of type bool. We'll handle that in
2655 build_builtin_candidate. */
2656 if (code == COND_EXPR)
2666 args[2] = NULL_TREE;
2669 candidates = add_builtin_candidates
2670 (candidates, code, code2, fnname, args, flags);
2673 if (! any_viable (candidates))
2677 case POSTINCREMENT_EXPR:
2678 case POSTDECREMENT_EXPR:
2679 /* Look for an `operator++ (int)'. If they didn't have
2680 one, then we fall back to the old way of doing things. */
2681 if (flags & LOOKUP_COMPLAIN)
2682 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
2683 fnname, opname_tab [code]);
2684 if (code == POSTINCREMENT_EXPR)
2685 code = PREINCREMENT_EXPR;
2687 code = PREDECREMENT_EXPR;
2688 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
2690 /* The caller will deal with these. */
2699 if (flags & LOOKUP_COMPLAIN)
2701 op_error (code, code2, arg1, arg2, arg3, "no match");
2702 print_z_candidates (candidates);
2704 return error_mark_node;
2706 candidates = splice_viable (candidates);
2707 cand = tourney (candidates);
2711 if (flags & LOOKUP_COMPLAIN)
2713 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
2714 print_z_candidates (candidates);
2716 return error_mark_node;
2719 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
2721 extern int warn_synth;
2723 && fnname == ansi_opname[MODIFY_EXPR]
2724 && DECL_ARTIFICIAL (cand->fn)
2726 && ! candidates->next->next)
2728 cp_warning ("using synthesized `%#D' for copy assignment",
2730 cp_warning_at (" where cfront would use `%#D'",
2732 ? candidates->next->fn
2736 /* Pedantically, normal function declarations are never considered
2737 to refer to template instantiations, so we only do this with
2739 if (flag_guiding_decls && templates && ! cand->template
2740 && ! DECL_INITIAL (cand->fn)
2741 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2742 add_maybe_template (cand->fn, templates);
2744 return build_over_call
2746 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
2747 ? mem_arglist : arglist,
2751 /* Check for comparison of different enum types. */
2760 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
2761 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
2762 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
2763 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
2765 cp_warning ("comparison between `%#T' and `%#T'",
2766 TREE_TYPE (arg1), TREE_TYPE (arg2));
2773 /* We need to strip any leading REF_BIND so that bitfields don't cause
2774 errors. This should not remove any important conversions, because
2775 builtins don't apply to class objects directly. */
2776 conv = TREE_VEC_ELT (cand->convs, 0);
2777 if (TREE_CODE (conv) == REF_BIND)
2778 conv = TREE_OPERAND (conv, 0);
2779 arg1 = convert_like (conv, arg1);
2782 conv = TREE_VEC_ELT (cand->convs, 1);
2783 if (TREE_CODE (conv) == REF_BIND)
2784 conv = TREE_OPERAND (conv, 0);
2785 arg2 = convert_like (conv, arg2);
2789 conv = TREE_VEC_ELT (cand->convs, 2);
2790 if (TREE_CODE (conv) == REF_BIND)
2791 conv = TREE_OPERAND (conv, 0);
2792 arg3 = convert_like (conv, arg3);
2799 return build_modify_expr (arg1, code2, arg2);
2802 return build_indirect_ref (arg1, "unary *");
2807 case TRUNC_DIV_EXPR:
2818 case TRUNC_MOD_EXPR:
2822 case TRUTH_ANDIF_EXPR:
2823 case TRUTH_ORIF_EXPR:
2824 return build_binary_op_nodefault (code, arg1, arg2, code);
2829 case TRUTH_NOT_EXPR:
2830 case PREINCREMENT_EXPR:
2831 case POSTINCREMENT_EXPR:
2832 case PREDECREMENT_EXPR:
2833 case POSTDECREMENT_EXPR:
2836 return build_unary_op (code, arg1, candidates != 0);
2839 return build_array_ref (arg1, arg2);
2842 return build_conditional_expr (arg1, arg2, arg3);
2845 return build_m_component_ref
2846 (build_indirect_ref (arg1, NULL_PTR), arg2);
2848 /* The caller will deal with these. */
2855 my_friendly_abort (367);
2860 /* Build up a call to operator new. This has to be handled differently
2861 from other operators in the way lookup is handled; first members are
2862 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
2863 TYPE is the type to be created. ARGS are any new-placement args.
2864 FLAGS are the usual overloading flags. */
2867 build_op_new_call (code, type, args, flags)
2868 enum tree_code code;
2872 tree fnname = ansi_opname[code];
2874 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
2875 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
2877 return build_method_call (build_dummy_object (type),
2878 fnname, args, NULL_TREE, flags);
2881 return build_new_function_call
2882 (lookup_function_nonclass (fnname, args), args);
2885 /* Build a call to operator delete. This has to be handled very specially,
2886 because the restrictions on what signatures match are different from all
2887 other call instances. For a normal delete, only a delete taking (void *)
2888 or (void *, size_t) is accepted. For a placement delete, only an exact
2889 match with the placement new is accepted.
2891 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
2892 ADDR is the pointer to be deleted. For placement delete, it is also
2893 used to determine what the corresponding new looked like.
2894 SIZE is the size of the memory block to be deleted.
2895 FLAGS are the usual overloading flags.
2896 PLACEMENT is the corresponding placement new call, or 0. */
2899 build_op_delete_call (code, addr, size, flags, placement)
2900 enum tree_code code;
2901 tree addr, size, placement;
2904 tree fn, fns, fnname, fntype, argtypes, args, type;
2906 if (addr == error_mark_node)
2907 return error_mark_node;
2909 type = TREE_TYPE (TREE_TYPE (addr));
2910 fnname = ansi_opname[code];
2912 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
2915 If the result of the lookup is ambiguous or inaccessible, or if
2916 the lookup selects a placement deallocation function, the
2917 program is ill-formed.
2919 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
2921 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
2922 if (fns == error_mark_node)
2923 return error_mark_node;
2928 if (fns == NULL_TREE)
2929 fns = lookup_name_nonclass (fnname);
2933 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
2935 /* Extract the function. */
2936 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
2937 /* Then the second parm type. */
2938 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
2940 /* Also the second argument. */
2941 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
2945 /* First try it without the size argument. */
2946 argtypes = void_list_node;
2950 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
2951 fntype = build_function_type (void_type_node, argtypes);
2953 /* Strip const and volatile from addr. */
2954 if (type != TYPE_MAIN_VARIANT (type))
2955 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
2957 fn = instantiate_type (fntype, fns, 2);
2959 if (fn != error_mark_node)
2961 if (TREE_CODE (fns) == TREE_LIST)
2962 /* Member functions. */
2963 enforce_access (TREE_PURPOSE (fns), fn);
2964 return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
2967 /* If we are doing placement delete we do nothing if we don't find a
2968 matching op delete. */
2972 /* Normal delete; now try to find a match including the size argument. */
2973 argtypes = tree_cons (NULL_TREE, ptr_type_node,
2974 tree_cons (NULL_TREE, sizetype, void_list_node));
2975 fntype = build_function_type (void_type_node, argtypes);
2977 fn = instantiate_type (fntype, fns, 2);
2979 if (fn != error_mark_node)
2981 if (BASELINK_P (fns))
2982 /* Member functions. */
2983 enforce_access (TREE_PURPOSE (fns), fn);
2984 return build_function_call
2985 (fn, expr_tree_cons (NULL_TREE, addr,
2986 build_expr_list (NULL_TREE, size)));
2989 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
2990 destructor, in which case the error should be deferred
2991 until someone actually tries to delete one of these. */
2992 if (flags & LOOKUP_SPECULATIVELY)
2995 cp_error ("no suitable operator delete for `%T'", type);
2996 return error_mark_node;
2999 /* If the current scope isn't allowed to access DECL along
3000 BASETYPE_PATH, give an error. The most derived class in
3001 BASETYPE_PATH is the one used to qualify DECL. */
3004 enforce_access (basetype_path, decl)
3010 accessible = accessible_p (basetype_path, decl);
3013 if (TREE_PRIVATE (decl))
3014 cp_error_at ("`%+#D' is private", decl);
3015 else if (TREE_PROTECTED (decl))
3016 cp_error_at ("`%+#D' is protected", decl);
3018 cp_error_at ("`%+#D' is inaccessible", decl);
3019 cp_error ("within this context");
3026 /* Perform the conversions in CONVS on the expression EXPR. */
3029 convert_like (convs, expr)
3032 if (ICS_BAD_FLAG (convs)
3033 && TREE_CODE (convs) != USER_CONV
3034 && TREE_CODE (convs) != AMBIG_CONV)
3037 for (; t; t = TREE_OPERAND (t, 0))
3039 if (TREE_CODE (t) == USER_CONV)
3041 expr = convert_like (t, expr);
3044 else if (TREE_CODE (t) == AMBIG_CONV)
3045 return convert_like (t, expr);
3046 else if (TREE_CODE (t) == IDENTITY_CONV)
3049 return convert_for_initialization
3050 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3051 "conversion", NULL_TREE, 0);
3054 switch (TREE_CODE (convs))
3058 struct z_candidate *cand
3059 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3062 int flags = LOOKUP_NORMAL;
3064 if (DECL_CONSTRUCTOR_P (fn))
3066 tree t = build_int_2 (0, 0);
3067 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3069 args = build_scratch_list (NULL_TREE, expr);
3070 if (TYPE_USES_PVBASES (DECL_CONTEXT (fn))
3071 && !flag_vtable_thunks_compat)
3073 args = scratch_tree_cons (NULL_TREE, vlist_zero_node, args);
3074 flags != LOOKUP_HAS_VLIST;
3076 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3077 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3078 args = scratch_tree_cons (NULL_TREE, t, args);
3081 args = build_this (expr);
3082 expr = build_over_call (cand, args, flags);
3084 /* If this is a constructor or a function returning an aggr type,
3085 we need to build up a TARGET_EXPR. */
3086 if (DECL_CONSTRUCTOR_P (fn))
3087 expr = build_cplus_new (TREE_TYPE (convs), expr);
3092 if (type_unknown_p (expr))
3093 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3094 if (TREE_READONLY_DECL_P (expr))
3095 expr = decl_constant_value (expr);
3098 /* Call build_user_type_conversion again for the error. */
3099 return build_user_type_conversion
3100 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3106 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3107 if (expr == error_mark_node)
3108 return error_mark_node;
3110 switch (TREE_CODE (convs))
3113 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3115 /* else fall through */
3118 tree cvt_expr = build_user_type_conversion
3119 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3122 /* This can occur if, for example, the EXPR has incomplete
3123 type. We can't check for that before attempting the
3124 conversion because the type might be an incomplete
3125 array type, which is OK if some constructor for the
3126 destination type takes a pointer argument. */
3127 if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
3129 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
3130 incomplete_type_error (expr, TREE_TYPE (expr));
3132 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3133 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3136 cp_error ("could not convert `%E' to `%T'",
3137 expr, TREE_TYPE (convs));
3138 return error_mark_node;
3144 return convert_to_reference
3145 (TREE_TYPE (convs), expr,
3146 CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
3149 return decay_conversion (expr);
3152 /* Warn about deprecated conversion if appropriate. */
3153 string_conv_p (TREE_TYPE (convs), expr, 1);
3159 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3160 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3163 /* ARG is being passed to a varargs function. Perform any conversions
3164 required. Return the converted value. */
3167 convert_arg_to_ellipsis (arg)
3170 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3171 && (TYPE_PRECISION (TREE_TYPE (arg))
3172 < TYPE_PRECISION (double_type_node)))
3173 /* Convert `float' to `double'. */
3174 arg = cp_convert (double_type_node, arg);
3175 else if (IS_AGGR_TYPE (TREE_TYPE (arg))
3176 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (arg)))
3177 cp_warning ("cannot pass objects of type `%T' through `...'",
3180 /* Convert `short' and `char' to full-size `int'. */
3181 arg = default_conversion (arg);
3183 arg = require_complete_type (arg);
3188 /* ARG is a default argument expression being passed to a parameter of
3189 the indicated TYPE, which is a parameter to FN. Do any required
3190 conversions. Return the converted value. */
3193 convert_default_arg (type, arg, fn)
3198 if (fn && DECL_TEMPLATE_INFO (fn))
3200 /* This default argument came from a template. Instantiate the
3201 default argument here, not in tsubst. In the case of
3210 we must be careful to do name lookup in the scope of S<T>,
3211 rather than in the current class. */
3212 if (DECL_CLASS_SCOPE_P (fn))
3213 pushclass (DECL_REAL_CONTEXT (fn), 2);
3215 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), /*complain=*/1, NULL_TREE);
3217 if (DECL_CLASS_SCOPE_P (fn))
3220 /* Make sure the default argument is reasonable. */
3221 arg = check_default_argument (type, arg);
3224 arg = break_out_target_exprs (arg);
3226 if (TREE_CODE (arg) == CONSTRUCTOR)
3228 arg = digest_init (type, arg, 0);
3229 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3230 "default argument", 0, 0);
3234 /* This could get clobbered by the following call. */
3235 if (TREE_HAS_CONSTRUCTOR (arg))
3236 arg = copy_node (arg);
3238 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3239 "default argument", 0, 0);
3240 #ifdef PROMOTE_PROTOTYPES
3241 if ((TREE_CODE (type) == INTEGER_TYPE
3242 || TREE_CODE (type) == ENUMERAL_TYPE)
3243 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3244 arg = default_conversion (arg);
3252 build_over_call (cand, args, flags)
3253 struct z_candidate *cand;
3258 tree convs = cand->convs;
3259 tree converted_args = NULL_TREE;
3260 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3261 tree conv, arg, val;
3265 /* Give any warnings we noticed during overload resolution. */
3267 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3268 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3270 if (DECL_FUNCTION_MEMBER_P (fn))
3271 enforce_access (cand->basetype_path, fn);
3273 if (args && TREE_CODE (args) != TREE_LIST)
3274 args = build_scratch_list (NULL_TREE, args);
3277 /* The implicit parameters to a constructor are not considered by overload
3278 resolution, and must be of the proper type. */
3279 if (DECL_CONSTRUCTOR_P (fn))
3281 converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3282 arg = TREE_CHAIN (arg);
3283 parm = TREE_CHAIN (parm);
3284 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3286 converted_args = expr_tree_cons
3287 (NULL_TREE, TREE_VALUE (arg), converted_args);
3288 arg = TREE_CHAIN (arg);
3289 parm = TREE_CHAIN (parm);
3291 if (flags & LOOKUP_HAS_VLIST)
3293 converted_args = expr_tree_cons
3294 (NULL_TREE, TREE_VALUE (arg), converted_args);
3295 arg = TREE_CHAIN (arg);
3296 parm = TREE_CHAIN (parm);
3299 /* Bypass access control for 'this' parameter. */
3300 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3302 tree parmtype = TREE_VALUE (parm);
3303 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3305 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3306 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3307 TREE_TYPE (argtype), fn);
3309 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3310 X is called for an object that is not of type X, or of a type
3311 derived from X, the behavior is undefined.
3313 So we can assume that anything passed as 'this' is non-null, and
3314 optimize accordingly. */
3315 if (TREE_CODE (parmtype) == POINTER_TYPE)
3316 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3318 /* This happens with signatures. */
3319 t = convert_force (parmtype, TREE_VALUE (arg), CONV_C_CAST);
3320 converted_args = expr_tree_cons (NULL_TREE, t, converted_args);
3321 parm = TREE_CHAIN (parm);
3322 arg = TREE_CHAIN (arg);
3328 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3330 tree type = TREE_VALUE (parm);
3332 conv = TREE_VEC_ELT (convs, i);
3333 if (ICS_BAD_FLAG (conv))
3336 val = TREE_VALUE (arg);
3338 for (; t; t = TREE_OPERAND (t, 0))
3340 if (TREE_CODE (t) == USER_CONV
3341 || TREE_CODE (t) == AMBIG_CONV)
3343 val = convert_like (t, val);
3346 else if (TREE_CODE (t) == IDENTITY_CONV)
3349 val = convert_for_initialization
3350 (NULL_TREE, type, val, LOOKUP_NORMAL,
3351 "argument passing", fn, i - is_method);
3355 /* Issue warnings about peculiar, but legal, uses of NULL. */
3356 if (ARITHMETIC_TYPE_P (TREE_VALUE (parm))
3357 && TREE_VALUE (arg) == null_node)
3358 cp_warning ("converting NULL to non-pointer type");
3360 val = convert_like (conv, TREE_VALUE (arg));
3363 #ifdef PROMOTE_PROTOTYPES
3364 if ((TREE_CODE (type) == INTEGER_TYPE
3365 || TREE_CODE (type) == ENUMERAL_TYPE)
3366 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3367 val = default_conversion (val);
3369 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3372 /* Default arguments */
3373 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3375 = expr_tree_cons (NULL_TREE,
3376 convert_default_arg (TREE_VALUE (parm),
3377 TREE_PURPOSE (parm),
3382 for (; arg; arg = TREE_CHAIN (arg))
3384 = expr_tree_cons (NULL_TREE,
3385 convert_arg_to_ellipsis (TREE_VALUE (arg)),
3388 converted_args = nreverse (converted_args);
3390 if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
3391 check_function_format (DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
3394 /* Avoid actually calling copy constructors and copy assignment operators,
3397 if (! flag_elide_constructors)
3398 /* Do things the hard way. */;
3399 else if (DECL_CONSTRUCTOR_P (fn)
3400 && TREE_VEC_LENGTH (convs) == 1
3401 && copy_args_p (fn))
3404 arg = TREE_CHAIN (converted_args);
3405 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3406 arg = TREE_CHAIN (arg);
3407 if (flags & LOOKUP_HAS_VLIST)
3408 arg = TREE_CHAIN (arg);
3409 arg = TREE_VALUE (arg);
3411 /* Pull out the real argument, disregarding const-correctness. */
3413 while (TREE_CODE (targ) == NOP_EXPR
3414 || TREE_CODE (targ) == NON_LVALUE_EXPR
3415 || TREE_CODE (targ) == CONVERT_EXPR)
3416 targ = TREE_OPERAND (targ, 0);
3417 if (TREE_CODE (targ) == ADDR_EXPR)
3419 targ = TREE_OPERAND (targ, 0);
3420 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3421 TYPE_MAIN_VARIANT (TREE_TYPE (targ))))
3430 arg = build_indirect_ref (arg, 0);
3432 /* [class.copy]: the copy constructor is implicitly defined even if
3433 the implementation elided its use. */
3434 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3437 /* If we're creating a temp and we already have one, don't create a
3438 new one. If we're not creating a temp but we get one, use
3439 INIT_EXPR to collapse the temp into our target. Otherwise, if the
3440 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3441 temp or an INIT_EXPR otherwise. */
3442 if (integer_zerop (TREE_VALUE (args)))
3444 if (! real_lvalue_p (arg))
3446 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3448 val = build_decl (VAR_DECL, NULL_TREE, DECL_CONTEXT (fn));
3449 val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
3450 TREE_SIDE_EFFECTS (val) = 1;
3454 else if (! real_lvalue_p (arg)
3455 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3458 tree to = stabilize_reference
3459 (build_indirect_ref (TREE_VALUE (args), 0));
3461 /* If we're initializing an empty class, then we actually
3462 have to use a MODIFY_EXPR rather than an INIT_EXPR. The
3463 reason is that the dummy padding member in the target may
3464 not actually be allocated if TO is a base class
3465 subobject. Since we've set TYPE_NONCOPIED_PARTS on the
3466 padding, a MODIFY_EXPR will preserve its value, which is
3467 the right thing to do if it's not really padding at all.
3469 It's not safe to just throw away the ARG if we're looking
3470 at an empty class because the ARG might contain a
3471 TARGET_EXPR which wants to be bound to TO. If it is not,
3472 expand_expr will assign a dummy slot for the TARGET_EXPR,
3473 and we will call a destructor for it, which is wrong,
3474 because we will also destroy TO, but will never have
3476 val = build (is_empty_class (DECL_CLASS_CONTEXT (fn))
3477 ? MODIFY_EXPR : INIT_EXPR,
3478 DECL_CONTEXT (fn), to, arg);
3479 TREE_SIDE_EFFECTS (val) = 1;
3480 address = build_unary_op (ADDR_EXPR, val, 0);
3481 /* Avoid a warning about this expression, if the address is
3483 TREE_USED (address) = 1;
3487 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
3489 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
3491 tree to = stabilize_reference
3492 (build_indirect_ref (TREE_VALUE (converted_args), 0));
3494 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
3496 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
3497 TREE_SIDE_EFFECTS (val) = 1;
3503 if (DECL_CLASS_SCOPE_P (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
3504 return build_signature_method_call (fn, converted_args);
3505 else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3507 tree t, *p = &TREE_VALUE (converted_args);
3508 tree binfo = get_binfo
3509 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
3510 *p = convert_pointer_to_real (binfo, *p);
3511 if (TREE_SIDE_EFFECTS (*p))
3512 *p = save_expr (*p);
3513 t = build_pointer_type (TREE_TYPE (fn));
3514 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
3517 else if (DECL_INLINE (fn))
3518 fn = inline_conversion (fn);
3520 fn = build_addr_func (fn);
3522 /* Recognize certain built-in functions so we can make tree-codes
3523 other than CALL_EXPR. We do this when it enables fold-const.c
3524 to do something useful. */
3526 if (TREE_CODE (fn) == ADDR_EXPR
3527 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3528 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
3529 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
3534 if (converted_args == 0)
3535 return integer_zero_node;
3536 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
3541 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3542 if (TREE_CODE (TREE_TYPE (fn)) == VOID_TYPE)
3544 fn = require_complete_type (fn);
3545 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
3546 fn = build_cplus_new (TREE_TYPE (fn), fn);
3547 return convert_from_reference (fn);
3551 build_new_method_call (instance, name, args, basetype_path, flags)
3552 tree instance, name, args, basetype_path;
3555 struct z_candidate *candidates = 0, *cand;
3556 tree explicit_targs = NULL_TREE;
3557 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
3559 tree user_args = args;
3560 tree templates = NULL_TREE;
3561 int template_only = 0;
3563 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3565 explicit_targs = TREE_OPERAND (name, 1);
3566 name = TREE_OPERAND (name, 0);
3567 if (TREE_CODE_CLASS (TREE_CODE (name)) == 'd')
3568 name = DECL_NAME (name);
3571 if (TREE_CODE (name) == COMPONENT_REF)
3572 name = TREE_OPERAND (name, 1);
3573 if (TREE_CODE (name) == OVERLOAD)
3574 name = DECL_NAME (OVL_CURRENT (name));
3580 /* If there is an extra argument for controlling virtual bases,
3581 remove it for error reporting. */
3582 if (flags & LOOKUP_HAS_IN_CHARGE)
3583 user_args = TREE_CHAIN (args);
3584 if (flags & LOOKUP_HAS_VLIST)
3585 user_args = TREE_CHAIN (user_args);
3587 args = resolve_args (args);
3589 if (args == error_mark_node)
3590 return error_mark_node;
3592 if (instance == NULL_TREE)
3593 basetype = BINFO_TYPE (basetype_path);
3596 if (TREE_CODE (instance) == OFFSET_REF)
3597 instance = resolve_offset_ref (instance);
3598 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3599 instance = convert_from_reference (instance);
3600 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
3602 /* XXX this should be handled before we get here. */
3603 if (! IS_AGGR_TYPE (basetype)
3604 && ! (TYPE_LANG_SPECIFIC (basetype)
3605 && (IS_SIGNATURE_POINTER (basetype)
3606 || IS_SIGNATURE_REFERENCE (basetype))))
3608 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
3609 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
3610 name, instance, basetype);
3612 return error_mark_node;
3615 /* If `instance' is a signature pointer/reference and `name' is
3616 not a constructor, we are calling a signature member function.
3617 In that case set the `basetype' to the signature type. */
3618 if ((IS_SIGNATURE_POINTER (basetype)
3619 || IS_SIGNATURE_REFERENCE (basetype))
3620 && TYPE_IDENTIFIER (basetype) != name)
3621 basetype = SIGNATURE_TYPE (basetype);
3624 if (basetype_path == NULL_TREE)
3625 basetype_path = TYPE_BINFO (basetype);
3629 instance_ptr = build_this (instance);
3631 if (! template_only)
3633 /* XXX this should be handled before we get here. */
3634 fns = build_field_call (basetype_path, instance_ptr, name, args);
3641 instance_ptr = build_int_2 (0, 0);
3642 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
3646 = (name == ctor_identifier ? constructor_name (basetype) : name);
3648 fns = lookup_fnfields (basetype_path, name, 1);
3650 if (fns == error_mark_node)
3651 return error_mark_node;
3654 tree fn = TREE_VALUE (fns);
3655 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3656 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3658 if (TYPE_USES_PVBASES(basetype)
3659 && (!flag_vtable_thunks_compat || (name == dtor_identifier)))
3661 args = scratch_tree_cons (NULL_TREE, vlist_zero_node, args);
3662 flags |= LOOKUP_HAS_VLIST;
3664 flags |= LOOKUP_HAS_IN_CHARGE;
3665 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3667 mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
3668 for (; fn; fn = OVL_NEXT (fn))
3670 tree t = OVL_CURRENT (fn);
3673 /* We can end up here for copy-init of same or base class. */
3674 if (name == ctor_identifier
3675 && (flags & LOOKUP_ONLYCONVERTING)
3676 && DECL_NONCONVERTING_P (t))
3678 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
3679 this_arglist = mem_args;
3681 this_arglist = args;
3683 if (TREE_CODE (t) == TEMPLATE_DECL)
3685 /* A member template. */
3686 templates = scratch_tree_cons (NULL_TREE, t, templates);
3688 add_template_candidate (candidates, t, explicit_targs,
3690 TREE_TYPE (name), flags, DEDUCE_CALL);
3692 else if (! template_only)
3693 candidates = add_function_candidate (candidates, t,
3694 this_arglist, flags);
3697 candidates->basetype_path = TREE_PURPOSE (fns);
3701 if (! any_viable (candidates))
3703 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
3704 if (flags & LOOKUP_SPECULATIVELY)
3706 if (TYPE_SIZE (basetype) == 0)
3707 incomplete_type_error (instance_ptr, basetype);
3709 cp_error ("no matching function for call to `%T::%D (%A)%V'",
3710 basetype, pretty_name, user_args,
3711 TREE_TYPE (TREE_TYPE (instance_ptr)));
3712 print_z_candidates (candidates);
3713 return error_mark_node;
3715 candidates = splice_viable (candidates);
3716 cand = tourney (candidates);
3720 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
3722 print_z_candidates (candidates);
3723 return error_mark_node;
3726 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
3727 && instance == current_class_ref
3728 && DECL_CONSTRUCTOR_P (current_function_decl)
3729 && ! (flags & LOOKUP_NONVIRTUAL)
3730 && value_member (cand->fn, CLASSTYPE_ABSTRACT_VIRTUALS (basetype)))
3731 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
3732 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3733 && is_dummy_object (instance_ptr))
3735 cp_error ("cannot call member function `%D' without object", cand->fn);
3736 return error_mark_node;
3739 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
3740 && ((instance == current_class_ref && (dtor_label || ctor_label))
3741 || resolves_to_fixed_type_p (instance, 0)))
3742 flags |= LOOKUP_NONVIRTUAL;
3744 /* Pedantically, normal function declarations are never considered
3745 to refer to template instantiations, so we only do this with
3747 if (flag_guiding_decls && templates && ! cand->template
3748 && ! DECL_INITIAL (cand->fn))
3749 add_maybe_template (cand->fn, templates);
3751 return build_over_call
3753 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
3757 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
3758 subsequence of ICS2. */
3761 is_subseq (ics1, ics2)
3764 /* We can assume that a conversion of the same code
3765 between the same types indicates a subsequence since we only get
3766 here if the types we are converting from are the same. */
3768 while (TREE_CODE (ics1) == RVALUE_CONV
3769 || TREE_CODE (ics1) == LVALUE_CONV)
3770 ics1 = TREE_OPERAND (ics1, 0);
3774 while (TREE_CODE (ics2) == RVALUE_CONV
3775 || TREE_CODE (ics2) == LVALUE_CONV)
3776 ics2 = TREE_OPERAND (ics2, 0);
3778 if (TREE_CODE (ics2) == USER_CONV
3779 || TREE_CODE (ics2) == AMBIG_CONV
3780 || TREE_CODE (ics2) == IDENTITY_CONV)
3781 /* At this point, ICS1 cannot be a proper subsequence of
3782 ICS2. We can get a USER_CONV when we are comparing the
3783 second standard conversion sequence of two user conversion
3787 ics2 = TREE_OPERAND (ics2, 0);
3789 if (TREE_CODE (ics2) == TREE_CODE (ics1)
3790 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
3791 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
3792 TREE_TYPE (TREE_OPERAND (ics1, 0))))
3797 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
3798 be any _TYPE nodes. */
3801 is_properly_derived_from (derived, base)
3805 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
3806 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
3809 /* We only allow proper derivation here. The DERIVED_FROM_P macro
3810 considers every class derived from itself. */
3811 return (!same_type_p (TYPE_MAIN_VARIANT (derived),
3812 TYPE_MAIN_VARIANT (base))
3813 && DERIVED_FROM_P (base, derived));
3816 /* We build the ICS for an implicit object parameter as a pointer
3817 conversion sequence. However, such a sequence should be compared
3818 as if it were a reference conversion sequence. If ICS is the
3819 implicit conversion sequence for an implicit object parameter,
3820 modify it accordingly. */
3823 maybe_handle_implicit_object (ics)
3826 if (ICS_THIS_FLAG (*ics))
3828 /* [over.match.funcs]
3830 For non-static member functions, the type of the
3831 implicit object parameter is "reference to cv X"
3832 where X is the class of which the function is a
3833 member and cv is the cv-qualification on the member
3834 function declaration. */
3836 if (TREE_CODE (t) == QUAL_CONV)
3837 t = TREE_OPERAND (t, 0);
3838 if (TREE_CODE (t) == PTR_CONV)
3839 t = TREE_OPERAND (t, 0);
3840 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3841 t = build_conv (REF_BIND,
3842 build_reference_type (TREE_TYPE (TREE_TYPE (*ics))),
3844 ICS_STD_RANK (t) = ICS_STD_RANK (*ics);
3849 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
3850 to the type the reference originally referred to, and return 1.
3851 Otherwise, return 0. */
3854 maybe_handle_ref_bind (ics, target_type)
3858 if (TREE_CODE (*ics) == REF_BIND)
3862 When a parameter of reference type binds directly
3863 (_dcl.init.ref_) to an argument expression, the implicit
3864 conversion sequence is the identity conversion, unless the
3865 argument expression has a type that is a derived class of the
3866 parameter type, in which case the implicit conversion
3867 sequence is a derived-to-base Conversion.
3869 If the parameter binds directly to the result of applying a
3870 conversion function to the argument expression, the implicit
3871 conversion sequence is a user-defined conversion sequence
3872 (_over.ics.user_), with the second standard conversion
3873 sequence either an identity conversion or, if the conversion
3874 function returns an entity of a type that is a derived class
3875 of the parameter type, a derived-to-base Conversion.
3877 When a parameter of reference type is not bound directly to
3878 an argument expression, the conversion sequence is the one
3879 required to convert the argument expression to the underlying
3880 type of the reference according to _over.best.ics_.
3881 Conceptually, this conversion sequence corresponds to
3882 copy-initializing a temporary of the underlying type with the
3883 argument expression. Any difference in top-level
3884 cv-qualification is subsumed by the initialization itself and
3885 does not constitute a conversion. */
3887 tree old_ics = *ics;
3889 *target_type = TREE_TYPE (TREE_TYPE (*ics));
3890 *ics = TREE_OPERAND (*ics, 0);
3891 if (TREE_CODE (*ics) == IDENTITY_CONV
3892 && is_properly_derived_from (TREE_TYPE (*ics), *target_type))
3893 *ics = build_conv (BASE_CONV, *target_type, *ics);
3894 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
3895 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
3903 /* Compare two implicit conversion sequences according to the rules set out in
3904 [over.ics.rank]. Return values:
3906 1: ics1 is better than ics2
3907 -1: ics2 is better than ics1
3908 0: ics1 and ics2 are indistinguishable */
3911 compare_ics (ics1, ics2)
3918 tree deref_from_type1 = NULL_TREE;
3919 tree deref_from_type2 = NULL_TREE;
3920 tree deref_to_type1 = NULL_TREE;
3921 tree deref_to_type2 = NULL_TREE;
3923 /* REF_BINDING is non-zero if the result of the conversion sequence
3924 is a reference type. In that case TARGET_TYPE is the
3925 type referred to by the reference. */
3931 /* Handle implicit object parameters. */
3932 maybe_handle_implicit_object (&ics1);
3933 maybe_handle_implicit_object (&ics2);
3935 /* Handle reference parameters. */
3936 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
3937 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
3941 When comparing the basic forms of implicit conversion sequences (as
3942 defined in _over.best.ics_)
3944 --a standard conversion sequence (_over.ics.scs_) is a better
3945 conversion sequence than a user-defined conversion sequence
3946 or an ellipsis conversion sequence, and
3948 --a user-defined conversion sequence (_over.ics.user_) is a
3949 better conversion sequence than an ellipsis conversion sequence
3950 (_over.ics.ellipsis_). */
3951 if (ICS_RANK (ics1) > ICS_RANK (ics2))
3953 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
3956 if (ICS_RANK (ics1) == BAD_RANK)
3958 /* Both ICS are bad. We try to make a decision based on what
3959 would have happenned if they'd been good. */
3960 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
3961 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3963 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
3964 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3967 /* We couldn't make up our minds; try to figure it out below. */
3970 if (ICS_ELLIPSIS_FLAG (ics1))
3971 /* Both conversions are ellipsis conversions. */
3974 /* User-defined conversion sequence U1 is a better conversion sequence
3975 than another user-defined conversion sequence U2 if they contain the
3976 same user-defined conversion operator or constructor and if the sec-
3977 ond standard conversion sequence of U1 is better than the second
3978 standard conversion sequence of U2. */
3980 if (ICS_USER_FLAG (ics1))
3984 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
3985 if (TREE_CODE (t1) == AMBIG_CONV)
3987 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
3988 if (TREE_CODE (t2) == AMBIG_CONV)
3991 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
3994 /* We can just fall through here, after setting up
3995 FROM_TYPE1 and FROM_TYPE2. */
3996 from_type1 = TREE_TYPE (t1);
3997 from_type2 = TREE_TYPE (t2);
4001 /* We're dealing with two standard conversion sequences.
4005 Standard conversion sequence S1 is a better conversion
4006 sequence than standard conversion sequence S2 if
4008 --S1 is a proper subsequence of S2 (comparing the conversion
4009 sequences in the canonical form defined by _over.ics.scs_,
4010 excluding any Lvalue Transformation; the identity
4011 conversion sequence is considered to be a subsequence of
4012 any non-identity conversion sequence */
4015 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4016 from_type1 = TREE_OPERAND (from_type1, 0);
4017 from_type1 = TREE_TYPE (from_type1);
4020 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4021 from_type2 = TREE_OPERAND (from_type2, 0);
4022 from_type2 = TREE_TYPE (from_type2);
4025 if (same_type_p (from_type1, from_type2))
4027 if (is_subseq (ics1, ics2))
4029 if (is_subseq (ics2, ics1))
4032 /* Otherwise, one sequence cannot be a subsequence of the other; they
4033 don't start with the same type. This can happen when comparing the
4034 second standard conversion sequence in two user-defined conversion
4041 --the rank of S1 is better than the rank of S2 (by the rules
4044 Standard conversion sequences are ordered by their ranks: an Exact
4045 Match is a better conversion than a Promotion, which is a better
4046 conversion than a Conversion.
4048 Two conversion sequences with the same rank are indistinguishable
4049 unless one of the following rules applies:
4051 --A conversion that is not a conversion of a pointer, or pointer
4052 to member, to bool is better than another conversion that is such
4055 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4056 so that we do not have to check it explicitly. */
4057 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4059 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4062 to_type1 = TREE_TYPE (ics1);
4063 to_type2 = TREE_TYPE (ics2);
4065 if (TYPE_PTR_P (from_type1)
4066 && TYPE_PTR_P (from_type2)
4067 && TYPE_PTR_P (to_type1)
4068 && TYPE_PTR_P (to_type2))
4070 deref_from_type1 = TREE_TYPE (from_type1);
4071 deref_from_type2 = TREE_TYPE (from_type2);
4072 deref_to_type1 = TREE_TYPE (to_type1);
4073 deref_to_type2 = TREE_TYPE (to_type2);
4075 /* The rules for pointers to members A::* are just like the rules
4076 for pointers A*, except opposite: if B is derived from A then
4077 A::* converts to B::*, not vice versa. For that reason, we
4078 switch the from_ and to_ variables here. */
4079 else if (TYPE_PTRMEM_P (from_type1)
4080 && TYPE_PTRMEM_P (from_type2)
4081 && TYPE_PTRMEM_P (to_type1)
4082 && TYPE_PTRMEM_P (to_type2))
4084 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4085 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4086 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4087 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4089 else if (TYPE_PTRMEMFUNC_P (from_type1)
4090 && TYPE_PTRMEMFUNC_P (from_type2)
4091 && TYPE_PTRMEMFUNC_P (to_type1)
4092 && TYPE_PTRMEMFUNC_P (to_type2))
4094 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4095 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4096 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4097 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4100 if (deref_from_type1 != NULL_TREE
4101 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4102 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4104 /* This was one of the pointer or pointer-like conversions.
4108 --If class B is derived directly or indirectly from class A,
4109 conversion of B* to A* is better than conversion of B* to
4110 void*, and conversion of A* to void* is better than
4111 conversion of B* to void*. */
4112 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4113 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4115 if (is_properly_derived_from (deref_from_type1,
4118 else if (is_properly_derived_from (deref_from_type2,
4122 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4123 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4125 if (same_type_p (deref_from_type1, deref_from_type2))
4127 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4129 if (is_properly_derived_from (deref_from_type1,
4133 /* We know that DEREF_TO_TYPE1 is `void' here. */
4134 else if (is_properly_derived_from (deref_from_type1,
4139 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4140 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4144 --If class B is derived directly or indirectly from class A
4145 and class C is derived directly or indirectly from B,
4147 --conversion of C* to B* is better than conversion of C* to
4150 --conversion of B* to A* is better than conversion of C* to
4152 if (same_type_p (deref_from_type1, deref_from_type2))
4154 if (is_properly_derived_from (deref_to_type1,
4157 else if (is_properly_derived_from (deref_to_type2,
4161 else if (same_type_p (deref_to_type1, deref_to_type2))
4163 if (is_properly_derived_from (deref_from_type2,
4166 else if (is_properly_derived_from (deref_from_type1,
4172 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4173 && same_type_p (from_type1, from_type2))
4177 --binding of an expression of type C to a reference of type
4178 B& is better than binding an expression of type C to a
4179 reference of type A&
4181 --conversion of C to B is better than conversion of C to A, */
4182 if (is_properly_derived_from (from_type1, to_type1)
4183 && is_properly_derived_from (from_type1, to_type2))
4185 if (is_properly_derived_from (to_type1, to_type2))
4187 else if (is_properly_derived_from (to_type2, to_type1))
4191 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4192 && same_type_p (to_type1, to_type2))
4196 --binding of an expression of type B to a reference of type
4197 A& is better than binding an expression of type C to a
4198 reference of type A&,
4200 --onversion of B to A is better than conversion of C to A */
4201 if (is_properly_derived_from (from_type1, to_type1)
4202 && is_properly_derived_from (from_type2, to_type1))
4204 if (is_properly_derived_from (from_type2, from_type1))
4206 else if (is_properly_derived_from (from_type1, from_type2))
4213 --S1 and S2 differ only in their qualification conversion and yield
4214 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4215 qualification signature of type T1 is a proper subset of the cv-
4216 qualification signature of type T2 */
4217 if (TREE_CODE (ics1) == QUAL_CONV
4218 && TREE_CODE (ics2) == QUAL_CONV
4219 && same_type_p (from_type1, from_type2))
4220 return comp_cv_qual_signature (to_type1, to_type2);
4224 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4225 types to which the references refer are the same type except for
4226 top-level cv-qualifiers, and the type to which the reference
4227 initialized by S2 refers is more cv-qualified than the type to
4228 which the reference initialized by S1 refers */
4230 if (ref_binding1 && ref_binding2
4231 && same_type_p (TYPE_MAIN_VARIANT (to_type1),
4232 TYPE_MAIN_VARIANT (to_type2)))
4233 return comp_cv_qualification (target_type2, target_type1);
4235 /* Neither conversion sequence is better than the other. */
4239 /* The source type for this standard conversion sequence. */
4245 for (;; t = TREE_OPERAND (t, 0))
4247 if (TREE_CODE (t) == USER_CONV
4248 || TREE_CODE (t) == AMBIG_CONV
4249 || TREE_CODE (t) == IDENTITY_CONV)
4250 return TREE_TYPE (t);
4252 my_friendly_abort (1823);
4255 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4256 a pointer to LOSER and re-running joust to produce the warning if WINNER
4257 is actually used. */
4260 add_warning (winner, loser)
4261 struct z_candidate *winner, *loser;
4263 winner->warnings = expr_tree_cons (NULL_PTR,
4264 build_expr_ptr_wrapper (loser),
4268 /* Compare two candidates for overloading as described in
4269 [over.match.best]. Return values:
4271 1: cand1 is better than cand2
4272 -1: cand2 is better than cand1
4273 0: cand1 and cand2 are indistinguishable */
4276 joust (cand1, cand2, warn)
4277 struct z_candidate *cand1, *cand2;
4281 int i, off1 = 0, off2 = 0, len;
4283 /* Candidates that involve bad conversions are always worse than those
4285 if (cand1->viable > cand2->viable)
4287 if (cand1->viable < cand2->viable)
4290 /* If we have two pseudo-candidates for conversions to the same type,
4291 arbitrarily pick one. */
4292 if (TYPE_P (cand1->fn) && cand1->fn == cand2->fn)
4295 /* a viable function F1
4296 is defined to be a better function than another viable function F2 if
4297 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4298 ICSi(F2), and then */
4300 /* for some argument j, ICSj(F1) is a better conversion sequence than
4303 /* For comparing static and non-static member functions, we ignore the
4304 implicit object parameter of the non-static function. The WP says to
4305 pretend that the static function has an object parm, but that won't
4306 work with operator overloading. */
4307 len = TREE_VEC_LENGTH (cand1->convs);
4308 if (len != TREE_VEC_LENGTH (cand2->convs))
4310 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4311 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4313 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4314 && DECL_STATIC_FUNCTION_P (cand2->fn))
4320 my_friendly_abort (42);
4323 for (i = 0; i < len; ++i)
4325 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4326 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4327 int comp = compare_ics (t1, t2);
4332 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4333 && TREE_CODE (t1) == STD_CONV
4334 && TREE_CODE (t2) == STD_CONV
4335 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4336 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4337 && (TYPE_PRECISION (TREE_TYPE (t1))
4338 == TYPE_PRECISION (TREE_TYPE (t2)))
4339 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4340 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4343 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4345 struct z_candidate *w, *l;
4347 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4348 w = cand1, l = cand2;
4350 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4351 w = cand2, l = cand1;
4355 cp_warning ("passing `%T' chooses `%T' over `%T'",
4356 type, type1, type2);
4357 cp_warning (" in call to `%D'", w->fn);
4363 if (winner && comp != winner)
4372 /* warn about confusing overload resolution for user-defined conversions,
4373 either between a constructor and a conversion op, or between two
4375 if (winner && cand1->second_conv
4376 && ((DECL_CONSTRUCTOR_P (cand1->fn)
4377 != DECL_CONSTRUCTOR_P (cand2->fn))
4378 /* Don't warn if the two conv ops convert to the same type... */
4379 || (! DECL_CONSTRUCTOR_P (cand1->fn)
4380 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
4381 TREE_TYPE (TREE_TYPE (cand2->fn))))))
4383 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4386 struct z_candidate *w, *l;
4388 w = cand1, l = cand2;
4390 w = cand2, l = cand1;
4393 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4394 if (! DECL_CONSTRUCTOR_P (w->fn))
4395 source = TREE_TYPE (source);
4396 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4397 cp_warning (" for conversion from `%T' to `%T'",
4398 source, TREE_TYPE (w->second_conv));
4399 cp_warning (" because conversion sequence for the argument is better");
4410 F1 is a non-template function and F2 is a template function */
4412 if (! cand1->template && cand2->template)
4414 else if (cand1->template && ! cand2->template)
4416 else if (cand1->template && cand2->template)
4417 winner = more_specialized
4418 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4422 the context is an initialization by user-defined conversion (see
4423 _dcl.init_ and _over.match.user_) and the standard conversion
4424 sequence from the return type of F1 to the destination type (i.e.,
4425 the type of the entity being initialized) is a better conversion
4426 sequence than the standard conversion sequence from the return type
4427 of F2 to the destination type. */
4429 if (! winner && cand1->second_conv)
4430 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4432 /* If the built-in candidates are the same, arbitrarily pick one. */
4433 if (! winner && cand1->fn == cand2->fn
4434 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4436 for (i = 0; i < len; ++i)
4437 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4438 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
4440 if (i == TREE_VEC_LENGTH (cand1->convs))
4443 /* Kludge around broken overloading rules whereby
4444 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4445 that takes references and another that takes values. */
4446 if (cand1->fn == ansi_opname[COND_EXPR])
4448 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4449 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4450 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4451 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4453 if (same_type_p (t1, t2))
4455 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4457 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4465 /* Extension: If the worst conversion for one candidate is worse than the
4466 worst conversion for the other, take the first. */
4467 if (! winner && ! pedantic)
4469 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4471 for (i = 0; i < len; ++i)
4473 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4474 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4475 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
4476 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
4488 /* Given a list of candidates for overloading, find the best one, if any.
4489 This algorithm has a worst case of O(2n) (winner is last), and a best
4490 case of O(n/2) (totally ambiguous); much better than a sorting
4493 static struct z_candidate *
4494 tourney (candidates)
4495 struct z_candidate *candidates;
4497 struct z_candidate *champ = candidates, *challenger;
4499 int champ_compared_to_predecessor = 0;
4501 /* Walk through the list once, comparing each current champ to the next
4502 candidate, knocking out a candidate or two with each comparison. */
4504 for (challenger = champ->next; challenger; )
4506 fate = joust (champ, challenger, 0);
4508 challenger = challenger->next;
4513 champ = challenger->next;
4516 champ_compared_to_predecessor = 0;
4521 champ_compared_to_predecessor = 1;
4524 challenger = champ->next;
4528 /* Make sure the champ is better than all the candidates it hasn't yet
4529 been compared to. */
4531 for (challenger = candidates;
4533 && !(champ_compared_to_predecessor && challenger->next == champ);
4534 challenger = challenger->next)
4536 fate = joust (champ, challenger, 0);
4545 can_convert (to, from)
4548 tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
4549 return (t && ! ICS_BAD_FLAG (t));
4553 can_convert_arg (to, from, arg)
4556 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
4557 return (t && ! ICS_BAD_FLAG (t));