1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 92-97, 1998 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
39 extern int inhibit_warnings;
40 extern tree ctor_label, dtor_label;
42 static tree build_new_method_call PROTO((tree, tree, tree, tree, int));
44 static tree build_field_call PROTO((tree, tree, tree, tree));
45 static tree find_scoped_type PROTO((tree, tree, tree));
46 static struct z_candidate * tourney PROTO((struct z_candidate *));
47 static int joust PROTO((struct z_candidate *, struct z_candidate *, int));
48 static int compare_ics PROTO((tree, tree));
49 static tree build_over_call PROTO((struct z_candidate *, tree, int));
50 static tree convert_like PROTO((tree, tree));
51 static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
53 static tree build_object_call PROTO((tree, tree));
54 static tree resolve_args PROTO((tree));
55 static struct z_candidate * build_user_type_conversion_1
56 PROTO ((tree, tree, int));
57 static void print_z_candidates PROTO((struct z_candidate *));
58 static tree build_this PROTO((tree));
59 static struct z_candidate * splice_viable PROTO((struct z_candidate *));
60 static int any_viable PROTO((struct z_candidate *));
61 static struct z_candidate * add_template_candidate
62 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
64 static struct z_candidate * add_template_candidate_real
65 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
66 tree, unification_kind_t));
67 static struct z_candidate * add_template_conv_candidate
68 PROTO((struct z_candidate *, tree, tree, tree, tree));
69 static struct z_candidate * add_builtin_candidates
70 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
72 static struct z_candidate * add_builtin_candidate
73 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
74 tree, tree, tree, tree *, tree *, int));
75 static int is_complete PROTO((tree));
76 static struct z_candidate * build_builtin_candidate
77 PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
79 static struct z_candidate * add_conv_candidate
80 PROTO((struct z_candidate *, tree, tree, tree));
81 static struct z_candidate * add_function_candidate
82 PROTO((struct z_candidate *, tree, tree, int));
83 static tree implicit_conversion PROTO((tree, tree, tree, int));
84 static tree standard_conversion PROTO((tree, tree, tree));
85 static tree reference_binding PROTO((tree, tree, tree, int));
86 static tree strip_top_quals PROTO((tree));
87 static tree non_reference PROTO((tree));
88 static tree build_conv PROTO((enum tree_code, tree, tree));
89 static int is_subseq PROTO((tree, tree));
90 static int is_properly_derived_from PROTO((tree, tree));
91 static int maybe_handle_ref_bind PROTO((tree*, tree*));
92 static void maybe_handle_implicit_object PROTO((tree*));
95 build_vfield_ref (datum, type)
99 int old_assume_nonnull_objects = flag_assume_nonnull_objects;
101 if (datum == error_mark_node)
102 return error_mark_node;
104 /* Vtable references are always made from non-null objects. */
105 flag_assume_nonnull_objects = 1;
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);
114 flag_assume_nonnull_objects = old_assume_nonnull_objects;
119 /* Build a call to a member of an object. I.e., one that overloads
120 operator ()(), or is a pointer-to-function or pointer-to-method. */
123 build_field_call (basetype_path, instance_ptr, name, parms)
124 tree basetype_path, instance_ptr, name, parms;
126 tree field, instance;
128 if (name == ctor_identifier || name == dtor_identifier)
131 if (instance_ptr == current_class_ptr)
133 /* Check to see if we really have a reference to an instance variable
134 with `operator()()' overloaded. */
135 field = IDENTIFIER_CLASS_VALUE (name);
137 if (field == NULL_TREE)
139 cp_error ("`this' has no member named `%D'", name);
140 return error_mark_node;
143 if (TREE_CODE (field) == FIELD_DECL)
145 /* If it's a field, try overloading operator (),
146 or calling if the field is a pointer-to-function. */
147 instance = build_component_ref_1 (current_class_ref, field, 0);
148 if (instance == error_mark_node)
149 return error_mark_node;
151 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance)))
152 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
154 if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
156 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
157 return build_function_call (instance, parms);
158 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
159 return build_function_call (instance, expr_tree_cons (NULL_TREE, current_class_ptr, parms));
165 /* Check to see if this is not really a reference to an instance variable
166 with `operator()()' overloaded. */
167 field = lookup_field (basetype_path, name, 1, 0);
169 /* This can happen if the reference was ambiguous or for access
171 if (field == error_mark_node)
172 return error_mark_node;
174 if (field && TREE_CODE (field) == FIELD_DECL)
177 tree ftype = TREE_TYPE (field);
179 if (TREE_CODE (ftype) == REFERENCE_TYPE)
180 ftype = TREE_TYPE (ftype);
182 if (TYPE_LANG_SPECIFIC (ftype))
184 /* Make the next search for this field very short. */
185 basetype = DECL_FIELD_CONTEXT (field);
186 instance_ptr = convert_pointer_to (basetype, instance_ptr);
188 instance = build_indirect_ref (instance_ptr, NULL_PTR);
189 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
190 build_component_ref_1 (instance, field, 0),
193 if (TREE_CODE (ftype) == POINTER_TYPE)
195 if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
196 || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
198 /* This is a member which is a pointer to function. */
200 = build_component_ref_1 (build_indirect_ref (instance_ptr,
202 field, LOOKUP_COMPLAIN);
203 if (ref == error_mark_node)
204 return error_mark_node;
205 return build_function_call (ref, parms);
208 else if (TREE_CODE (ftype) == METHOD_TYPE)
210 error ("invalid call via pointer-to-member function");
211 return error_mark_node;
220 find_scoped_type (type, inner_name, inner_types)
221 tree type, inner_name, inner_types;
223 tree tags = CLASSTYPE_TAGS (type);
227 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
228 enclosing class) is set to the name for the enum type. So, if
229 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
230 then this test will be true. */
231 if (TREE_PURPOSE (tags) == inner_name)
233 if (inner_types == NULL_TREE)
234 return TYPE_MAIN_DECL (TREE_VALUE (tags));
235 return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
237 tags = TREE_CHAIN (tags);
240 /* Look for a TYPE_DECL. */
241 for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
242 if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
244 /* Code by raeburn. */
245 if (inner_types == NULL_TREE)
247 return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
253 /* Resolve an expression NAME1::NAME2::...::NAMEn to
254 the name that names the above nested type. INNER_TYPES
255 is a chain of nested type names (held together by SCOPE_REFs);
256 OUTER_TYPE is the type we know to enclose INNER_TYPES.
257 Returns NULL_TREE if there is an error. */
260 resolve_scope_to_name (outer_type, inner_stuff)
261 tree outer_type, inner_stuff;
264 tree inner_name, inner_type;
266 if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
268 /* We first try to look for a nesting in our current class context,
269 then try any enclosing classes. */
270 tree type = current_class_type;
272 while (type && (TREE_CODE (type) == RECORD_TYPE
273 || TREE_CODE (type) == UNION_TYPE))
275 tree rval = resolve_scope_to_name (type, inner_stuff);
277 if (rval != NULL_TREE)
279 type = DECL_CONTEXT (TYPE_MAIN_DECL (type));
283 if (TREE_CODE (inner_stuff) == SCOPE_REF)
285 inner_name = TREE_OPERAND (inner_stuff, 0);
286 inner_type = TREE_OPERAND (inner_stuff, 1);
290 inner_name = inner_stuff;
291 inner_type = NULL_TREE;
294 if (outer_type == NULL_TREE)
297 /* If we have something that's already a type by itself,
299 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
302 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
307 x = lookup_name (inner_name, 0);
309 if (x && TREE_CODE (x) == NAMESPACE_DECL)
311 x = lookup_namespace_name (x, inner_type);
317 if (! IS_AGGR_TYPE (outer_type))
320 /* Look for member classes or enums. */
321 tmp = find_scoped_type (outer_type, inner_name, inner_type);
323 /* If it's not a type in this class, then go down into the
324 base classes and search there. */
325 if (! tmp && TYPE_BINFO (outer_type))
327 tree binfos = TYPE_BINFO_BASETYPES (outer_type);
328 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
330 for (i = 0; i < n_baselinks; i++)
332 tree base_binfo = TREE_VEC_ELT (binfos, i);
333 tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
343 /* Returns nonzero iff the destructor name specified in NAME
344 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
348 check_dtor_name (basetype, name)
351 name = TREE_OPERAND (name, 0);
353 if (TREE_CODE (name) == TYPE_DECL)
354 name = TREE_TYPE (name);
355 else if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
357 else if (TREE_CODE (name) == IDENTIFIER_NODE)
359 if (IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
362 name = get_type_value (name);
365 my_friendly_abort (980605);
367 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
372 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
373 This is how virtual function calls are avoided. */
376 build_scoped_method_call (exp, basetype, name, parms)
377 tree exp, basetype, name, parms;
379 /* Because this syntactic form does not allow
380 a pointer to a base class to be `stolen',
381 we need not protect the derived->base conversion
384 @@ But we do have to check access privileges later. */
386 tree type = TREE_TYPE (exp);
388 if (type == error_mark_node
389 || basetype == error_mark_node)
390 return error_mark_node;
392 if (processing_template_decl)
394 if (TREE_CODE (name) == BIT_NOT_EXPR
395 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
397 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
399 name = build_min_nt (BIT_NOT_EXPR, type);
401 name = build_min_nt (SCOPE_REF, basetype, name);
402 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
405 if (TREE_CODE (type) == REFERENCE_TYPE)
406 type = TREE_TYPE (type);
408 if (TREE_CODE (basetype) == TREE_VEC)
411 basetype = BINFO_TYPE (binfo);
416 /* Check the destructor call syntax. */
417 if (TREE_CODE (name) == BIT_NOT_EXPR && ! check_dtor_name (basetype, name))
418 cp_error ("qualified type `%T' does not match destructor name `~%T'",
419 basetype, TREE_OPERAND (name, 0));
421 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
422 that explicit ~int is caught in the parser; this deals with typedefs
423 and template parms. */
424 if (TREE_CODE (name) == BIT_NOT_EXPR && ! IS_AGGR_TYPE (basetype))
426 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
427 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
428 exp, basetype, type);
430 return cp_convert (void_type_node, exp);
433 if (! is_aggr_type (basetype, 1))
434 return error_mark_node;
436 if (! IS_AGGR_TYPE (type))
438 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
440 return error_mark_node;
445 binfo = get_binfo (basetype, type, 1);
446 if (binfo == error_mark_node)
447 return error_mark_node;
449 error_not_base_type (basetype, type);
454 if (TREE_CODE (exp) == INDIRECT_REF)
455 decl = build_indirect_ref
456 (convert_pointer_to_real
457 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
459 decl = build_scoped_ref (exp, basetype);
461 /* Call to a destructor. */
462 if (TREE_CODE (name) == BIT_NOT_EXPR)
464 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
465 return cp_convert (void_type_node, exp);
467 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
468 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
472 /* Call to a method. */
473 return build_method_call (decl, name, parms, binfo,
474 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
476 return error_mark_node;
479 /* We want the address of a function or method. We avoid creating a
480 pointer-to-member function. */
483 build_addr_func (function)
486 tree type = TREE_TYPE (function);
488 /* We have to do these by hand to avoid real pointer to member
490 if (TREE_CODE (type) == METHOD_TYPE)
494 type = build_pointer_type (type);
496 if (mark_addressable (function) == 0)
497 return error_mark_node;
499 addr = build1 (ADDR_EXPR, type, function);
501 /* Address of a static or external variable or function counts
503 if (staticp (function))
504 TREE_CONSTANT (addr) = 1;
509 function = default_conversion (function);
514 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
515 POINTER_TYPE to those. Note, pointer to member function types
516 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
519 build_call (function, result_type, parms)
520 tree function, result_type, parms;
522 int is_constructor = 0;
526 function = build_addr_func (function);
528 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
530 sorry ("unable to call pointer to member function here");
531 return error_mark_node;
534 if (TREE_CODE (function) == ADDR_EXPR
535 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
536 decl = TREE_OPERAND (function, 0);
540 if (decl && DECL_CONSTRUCTOR_P (decl))
543 /* Don't pass empty class objects by value. This is useful
544 for tags in STL, which are used to control overload resolution.
545 We don't need to handle other cases of copying empty classes. */
546 if (! decl || ! DECL_BUILT_IN (decl))
547 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
548 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
549 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
551 tree t = make_node (RTL_EXPR);
552 TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
553 RTL_EXPR_RTL (t) = const0_rtx;
554 RTL_EXPR_SEQUENCE (t) = NULL_RTX;
555 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
556 TREE_VALUE (tmp), t);
559 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
560 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
561 TREE_TYPE (function) = result_type;
562 TREE_SIDE_EFFECTS (function) = 1;
567 /* Build something of the form ptr->method (args)
568 or object.method (args). This can also build
569 calls to constructors, and find friends.
571 Member functions always take their class variable
574 INSTANCE is a class instance.
576 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
578 PARMS help to figure out what that NAME really refers to.
580 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
581 down to the real instance type to use for access checking. We need this
582 information to get protected accesses correct. This parameter is used
583 by build_member_call.
585 FLAGS is the logical disjunction of zero or more LOOKUP_
586 flags. See cp-tree.h for more info.
588 If this is all OK, calls build_function_call with the resolved
591 This function must also handle being called to perform
592 initialization, promotion/coercion of arguments, and
593 instantiation of default parameters.
595 Note that NAME may refer to an instance variable name. If
596 `operator()()' is defined for the type of that field, then we return
600 build_method_call (instance, name, parms, basetype_path, flags)
601 tree instance, name, parms, basetype_path;
604 tree basetype, instance_ptr;
606 #ifdef GATHER_STATISTICS
607 n_build_method_call++;
610 if (instance == error_mark_node
611 || name == error_mark_node
612 || parms == error_mark_node
613 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
614 return error_mark_node;
616 if (processing_template_decl)
618 if (TREE_CODE (name) == BIT_NOT_EXPR
619 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
621 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
623 name = build_min_nt (BIT_NOT_EXPR, type);
626 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
629 /* This is the logic that magically deletes the second argument to
630 operator delete, if it is not needed. */
631 if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
633 tree save_last = TREE_CHAIN (parms);
635 /* get rid of unneeded argument */
636 TREE_CHAIN (parms) = NULL_TREE;
637 if (build_method_call (instance, name, parms, basetype_path,
638 (LOOKUP_SPECULATIVELY|flags) & ~LOOKUP_COMPLAIN))
640 /* If it finds a match, return it. */
641 return build_method_call (instance, name, parms, basetype_path, flags);
643 /* If it doesn't work, two argument delete must work */
644 TREE_CHAIN (parms) = save_last;
646 /* We already know whether it's needed or not for vec delete. */
647 else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
648 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
649 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
650 TREE_CHAIN (parms) = NULL_TREE;
652 if (TREE_CODE (name) == BIT_NOT_EXPR)
655 error ("destructors take no parameters");
656 basetype = TREE_TYPE (instance);
657 if (TREE_CODE (basetype) == REFERENCE_TYPE)
658 basetype = TREE_TYPE (basetype);
660 if (! check_dtor_name (basetype, name))
662 ("destructor name `~%T' does not match type `%T' of expression",
663 TREE_OPERAND (name, 0), basetype);
665 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
666 return cp_convert (void_type_node, instance);
667 instance = default_conversion (instance);
668 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
669 return build_delete (build_pointer_type (basetype),
670 instance_ptr, integer_two_node,
671 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
674 return build_new_method_call (instance, name, parms, basetype_path, flags);
677 /* New overloading code. */
687 struct z_candidate *next;
690 #define IDENTITY_RANK 0
696 #define ELLIPSIS_RANK 6
699 #define ICS_RANK(NODE) \
700 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
701 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
702 : ICS_USER_FLAG (NODE) ? USER_RANK \
703 : ICS_STD_RANK (NODE))
705 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
707 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
708 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
709 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
710 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
712 #define USER_CONV_CAND(NODE) \
713 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
714 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
721 || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE))
727 build_conv (code, type, from)
731 tree t = build1 (code, type, from);
732 int rank = ICS_STD_RANK (from);
744 if (rank < EXACT_RANK)
750 ICS_STD_RANK (t) = rank;
751 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
752 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
760 if (TREE_CODE (t) == REFERENCE_TYPE)
769 if (TREE_CODE (t) == ARRAY_TYPE)
771 return TYPE_MAIN_VARIANT (t);
774 /* Returns the standard conversion path (see [conv]) from type FROM to type
775 TO, if any. For proper handling of null pointer constants, you must
776 also pass the expression EXPR to convert from. */
779 standard_conversion (to, from, expr)
782 enum tree_code fcode, tcode;
786 if (TREE_CODE (to) == REFERENCE_TYPE)
788 if (TREE_CODE (from) == REFERENCE_TYPE)
791 from = TREE_TYPE (from);
793 to = strip_top_quals (to);
794 from = strip_top_quals (from);
796 fcode = TREE_CODE (from);
797 tcode = TREE_CODE (to);
799 conv = build1 (IDENTITY_CONV, from, expr);
801 if (fcode == FUNCTION_TYPE)
803 from = build_pointer_type (from);
804 fcode = TREE_CODE (from);
805 conv = build_conv (LVALUE_CONV, from, conv);
807 else if (fcode == ARRAY_TYPE)
809 from = build_pointer_type (TREE_TYPE (from));
810 fcode = TREE_CODE (from);
811 conv = build_conv (LVALUE_CONV, from, conv);
813 else if (fromref || (expr && real_lvalue_p (expr)))
814 conv = build_conv (RVALUE_CONV, from, conv);
819 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
820 && expr && null_ptr_cst_p (expr))
822 conv = build_conv (STD_CONV, to, conv);
824 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
826 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
827 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
829 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
830 TYPE_MAIN_VARIANT (TREE_TYPE (to)), 1))
832 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
833 && ufcode != FUNCTION_TYPE)
835 from = build_pointer_type
836 (cp_build_type_variant (void_type_node,
837 TYPE_READONLY (TREE_TYPE (from)),
838 TYPE_VOLATILE (TREE_TYPE (from))));
839 conv = build_conv (PTR_CONV, from, conv);
841 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
843 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
844 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
846 if (DERIVED_FROM_P (fbase, tbase)
847 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
848 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))),
851 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
852 from = build_pointer_type (from);
853 conv = build_conv (PMEM_CONV, from, conv);
856 else if (IS_AGGR_TYPE (TREE_TYPE (from))
857 && IS_AGGR_TYPE (TREE_TYPE (to)))
859 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
861 from = cp_build_type_variant (TREE_TYPE (to),
862 TYPE_READONLY (TREE_TYPE (from)),
863 TYPE_VOLATILE (TREE_TYPE (from)));
864 from = build_pointer_type (from);
865 conv = build_conv (PTR_CONV, from, conv);
869 if (comptypes (from, to, 1))
871 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
872 conv = build_conv (QUAL_CONV, to, conv);
873 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
875 conv = build_conv (PTR_CONV, to, conv);
876 ICS_BAD_FLAG (conv) = 1;
883 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
885 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
886 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
887 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
888 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
890 if (! DERIVED_FROM_P (fbase, tbase)
891 || ! comptypes (TREE_TYPE (fromfn), TREE_TYPE (tofn), 1)
892 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
893 TREE_CHAIN (TYPE_ARG_TYPES (tofn)), 1)
894 || TYPE_READONLY (fbase) != TYPE_READONLY (tbase)
895 || TYPE_VOLATILE (fbase) != TYPE_VOLATILE (tbase))
898 from = cp_build_type_variant (tbase, TYPE_READONLY (fbase),
899 TYPE_VOLATILE (fbase));
900 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
901 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
902 from = build_ptrmemfunc_type (build_pointer_type (from));
903 conv = build_conv (PMEM_CONV, from, conv);
905 else if (tcode == BOOLEAN_TYPE)
907 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
908 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
911 conv = build_conv (STD_CONV, to, conv);
912 if (fcode == POINTER_TYPE
913 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
914 ICS_STD_RANK (conv) = PBOOL_RANK;
916 /* We don't check for ENUMERAL_TYPE here because there are no standard
917 conversions to enum type. */
918 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
919 || tcode == REAL_TYPE)
921 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
923 conv = build_conv (STD_CONV, to, conv);
925 /* Give this a better rank if it's a promotion. */
926 if (to == type_promotes_to (from)
927 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
928 ICS_STD_RANK (conv) = PROMO_RANK;
930 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
931 && DERIVED_FROM_P (to, from))
933 if (TREE_CODE (conv) == RVALUE_CONV)
934 conv = TREE_OPERAND (conv, 0);
935 conv = build_conv (BASE_CONV, to, conv);
943 /* Returns the conversion path from type FROM to reference type TO for
944 purposes of reference binding. For lvalue binding, either pass a
945 reference type to FROM or an lvalue expression to EXPR.
947 Currently does not distinguish in the generated trees between binding to
948 an lvalue and a temporary. Should it? */
951 reference_binding (rto, rfrom, expr, flags)
952 tree rto, rfrom, expr;
957 tree to = TREE_TYPE (rto);
961 if (TREE_CODE (from) == REFERENCE_TYPE)
962 from = TREE_TYPE (from);
963 else if (! expr || ! real_lvalue_p (expr))
966 related = (comptypes (TYPE_MAIN_VARIANT (to),
967 TYPE_MAIN_VARIANT (from), 1)
968 || (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
969 && DERIVED_FROM_P (to, from)));
971 if (lvalue && related
972 && TYPE_READONLY (to) >= TYPE_READONLY (from)
973 && TYPE_VOLATILE (to) >= TYPE_VOLATILE (from))
975 conv = build1 (IDENTITY_CONV, from, expr);
977 if (comptypes (TYPE_MAIN_VARIANT (to),
978 TYPE_MAIN_VARIANT (from), 1))
979 conv = build_conv (REF_BIND, rto, conv);
982 conv = build_conv (REF_BIND, rto, conv);
983 ICS_STD_RANK (conv) = STD_RANK;
991 conv = standard_conversion (to, rfrom, expr);
994 conv = build_conv (REF_BIND, rto, conv);
996 /* Bind directly to a base subobject of a class rvalue. Do it
997 after building the conversion for proper handling of ICS_RANK. */
998 if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
999 TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
1002 && ((! (TYPE_READONLY (to) && ! TYPE_VOLATILE (to)
1003 && (flags & LOOKUP_NO_TEMP_BIND) == 0))
1004 /* If T1 is reference-related to T2, cv1 must be the same
1005 cv-qualification as, or greater cv-qualification than,
1006 cv2; otherwise, the program is ill-formed. */
1008 && (TYPE_READONLY (to) < TYPE_READONLY (from)
1009 || TYPE_VOLATILE (to) < TYPE_VOLATILE (from)))))
1010 ICS_BAD_FLAG (conv) = 1;
1016 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1017 to type TO. The optional expression EXPR may affect the conversion.
1018 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1022 implicit_conversion (to, from, expr, flags)
1023 tree to, from, expr;
1027 struct z_candidate *cand;
1029 if (expr && type_unknown_p (expr))
1031 expr = instantiate_type (to, expr, 0);
1032 if (expr == error_mark_node)
1034 from = TREE_TYPE (expr);
1037 if (TREE_CODE (to) == REFERENCE_TYPE)
1038 conv = reference_binding (to, from, expr, flags);
1040 conv = standard_conversion (to, from, expr);
1044 else if (expr != NULL_TREE
1045 && (IS_AGGR_TYPE (non_reference (from))
1046 || IS_AGGR_TYPE (non_reference (to)))
1047 && (flags & LOOKUP_NO_CONVERSION) == 0)
1049 cand = build_user_type_conversion_1
1050 (to, expr, LOOKUP_ONLYCONVERTING);
1052 conv = cand->second_conv;
1053 if ((! conv || ICS_BAD_FLAG (conv))
1054 && TREE_CODE (to) == REFERENCE_TYPE
1055 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1057 cand = build_user_type_conversion_1
1058 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1061 if (! TYPE_READONLY (TREE_TYPE (to))
1062 || TYPE_VOLATILE (TREE_TYPE (to)))
1063 ICS_BAD_FLAG (cand->second_conv) = 1;
1064 if (!conv || (ICS_BAD_FLAG (conv)
1065 > ICS_BAD_FLAG (cand->second_conv)))
1066 conv = build_conv (REF_BIND, to, cand->second_conv);
1074 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1077 static struct z_candidate *
1078 add_candidate (candidates, fn, convs, viable)
1079 struct z_candidate *candidates;
1083 struct z_candidate *cand
1084 = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
1087 cand->convs = convs;
1088 cand->second_conv = NULL_TREE;
1089 cand->viable = viable;
1090 cand->basetype_path = NULL_TREE;
1091 cand->template = NULL_TREE;
1092 cand->warnings = NULL_TREE;
1093 cand->next = candidates;
1098 /* Create an overload candidate for the function or method FN called with
1099 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1100 to implicit_conversion. */
1102 static struct z_candidate *
1103 add_function_candidate (candidates, fn, arglist, flags)
1104 struct z_candidate *candidates;
1108 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1111 tree parmnode = parmlist;
1112 tree argnode = arglist;
1115 /* The `this' and `in_chrg' arguments to constructors are not considered
1116 in overload resolution. */
1117 if (DECL_CONSTRUCTOR_P (fn))
1119 parmnode = TREE_CHAIN (parmnode);
1120 argnode = TREE_CHAIN (argnode);
1121 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1123 parmnode = TREE_CHAIN (parmnode);
1124 argnode = TREE_CHAIN (argnode);
1128 len = list_length (argnode);
1129 convs = make_scratch_vec (len);
1131 for (i = 0; i < len; ++i)
1133 tree arg = TREE_VALUE (argnode);
1134 tree argtype = TREE_TYPE (arg);
1137 /* An overloaded function does not have an argument type */
1138 if (TREE_CODE (arg) == OVERLOAD)
1139 argtype = unknown_type_node;
1140 argtype = cp_build_type_variant
1141 (argtype, TREE_READONLY (arg), TREE_THIS_VOLATILE (arg));
1143 if (parmnode == void_list_node)
1146 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1149 t = build1 (IDENTITY_CONV, argtype, arg);
1150 ICS_ELLIPSIS_FLAG (t) = 1;
1153 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1154 && ! DECL_CONSTRUCTOR_P (fn))
1155 ICS_THIS_FLAG (t) = 1;
1157 TREE_VEC_ELT (convs, i) = t;
1161 if (ICS_BAD_FLAG (t))
1165 parmnode = TREE_CHAIN (parmnode);
1166 argnode = TREE_CHAIN (argnode);
1172 /* Make sure there are default args for the rest of the parms. */
1173 for (; parmnode && parmnode != void_list_node;
1174 parmnode = TREE_CHAIN (parmnode))
1175 if (! TREE_PURPOSE (parmnode))
1181 return add_candidate (candidates, fn, convs, viable);
1184 /* Create an overload candidate for the conversion function FN which will
1185 be invoked for expression OBJ, producing a pointer-to-function which
1186 will in turn be called with the argument list ARGLIST, and add it to
1187 CANDIDATES. FLAGS is passed on to implicit_conversion. */
1189 static struct z_candidate *
1190 add_conv_candidate (candidates, fn, obj, arglist)
1191 struct z_candidate *candidates;
1192 tree fn, obj, arglist;
1194 tree totype = TREE_TYPE (TREE_TYPE (fn));
1195 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1196 int i, len = list_length (arglist) + 1;
1197 tree convs = make_scratch_vec (len);
1198 tree parmnode = parmlist;
1199 tree argnode = arglist;
1201 int flags = LOOKUP_NORMAL;
1203 for (i = 0; i < len; ++i)
1205 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1206 tree argtype = lvalue_type (arg);
1210 t = implicit_conversion (totype, argtype, arg, flags);
1211 else if (parmnode == void_list_node)
1214 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1217 t = build1 (IDENTITY_CONV, argtype, arg);
1218 ICS_ELLIPSIS_FLAG (t) = 1;
1221 TREE_VEC_ELT (convs, i) = t;
1225 if (ICS_BAD_FLAG (t))
1232 parmnode = TREE_CHAIN (parmnode);
1233 argnode = TREE_CHAIN (argnode);
1239 for (; parmnode && parmnode != void_list_node;
1240 parmnode = TREE_CHAIN (parmnode))
1241 if (! TREE_PURPOSE (parmnode))
1247 return add_candidate (candidates, fn, convs, viable);
1250 static struct z_candidate *
1251 build_builtin_candidate (candidates, fnname, type1, type2,
1252 args, argtypes, flags)
1253 struct z_candidate *candidates;
1254 tree fnname, type1, type2, *args, *argtypes;
1265 convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1267 for (i = 0; i < 2; ++i)
1272 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1276 /* We need something for printing the candidate. */
1277 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1279 else if (ICS_BAD_FLAG (t))
1281 TREE_VEC_ELT (convs, i) = t;
1284 /* For COND_EXPR we rearranged the arguments; undo that now. */
1287 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1288 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1289 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1291 TREE_VEC_ELT (convs, 0) = t;
1296 return add_candidate (candidates, fnname, convs, viable);
1303 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1306 /* Create any builtin operator overload candidates for the operator in
1307 question given the converted operand types TYPE1 and TYPE2. The other
1308 args are passed through from add_builtin_candidates to
1309 build_builtin_candidate. */
1311 static struct z_candidate *
1312 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1313 args, argtypes, flags)
1314 struct z_candidate *candidates;
1315 enum tree_code code, code2;
1316 tree fnname, type1, type2, *args, *argtypes;
1321 case POSTINCREMENT_EXPR:
1322 case POSTDECREMENT_EXPR:
1323 args[1] = integer_zero_node;
1324 type2 = integer_type_node;
1333 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1334 and VQ is either volatile or empty, there exist candidate operator
1335 functions of the form
1336 VQ T& operator++(VQ T&);
1337 T operator++(VQ T&, int);
1338 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1339 type other than bool, and VQ is either volatile or empty, there exist
1340 candidate operator functions of the form
1341 VQ T& operator--(VQ T&);
1342 T operator--(VQ T&, int);
1343 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1344 complete object type, and VQ is either volatile or empty, there exist
1345 candidate operator functions of the form
1346 T*VQ& operator++(T*VQ&);
1347 T*VQ& operator--(T*VQ&);
1348 T* operator++(T*VQ&, int);
1349 T* operator--(T*VQ&, int); */
1351 case POSTDECREMENT_EXPR:
1352 case PREDECREMENT_EXPR:
1353 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1355 case POSTINCREMENT_EXPR:
1356 case PREINCREMENT_EXPR:
1357 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1358 || TYPE_PTROB_P (type1))
1360 type1 = build_reference_type (type1);
1365 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1366 exist candidate operator functions of the form
1370 8 For every function type T, there exist candidate operator functions of
1372 T& operator*(T*); */
1375 if (TREE_CODE (type1) == POINTER_TYPE
1376 && (TYPE_PTROB_P (type1)
1377 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1381 /* 9 For every type T, there exist candidate operator functions of the form
1384 10For every promoted arithmetic type T, there exist candidate operator
1385 functions of the form
1389 case CONVERT_EXPR: /* unary + */
1390 if (TREE_CODE (type1) == POINTER_TYPE
1391 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1394 if (ARITHMETIC_TYPE_P (type1))
1398 /* 11For every promoted integral type T, there exist candidate operator
1399 functions of the form
1403 if (INTEGRAL_TYPE_P (type1))
1407 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1408 is the same type as C2 or is a derived class of C2, T is a complete
1409 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1410 there exist candidate operator functions of the form
1411 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1412 where CV12 is the union of CV1 and CV2. */
1415 if (TREE_CODE (type1) == POINTER_TYPE
1416 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1418 tree c1 = TREE_TYPE (type1);
1419 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1420 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1421 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1423 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1424 && (TYPE_PTRMEMFUNC_P (type2)
1425 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1430 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1431 didate operator functions of the form
1436 bool operator<(L, R);
1437 bool operator>(L, R);
1438 bool operator<=(L, R);
1439 bool operator>=(L, R);
1440 bool operator==(L, R);
1441 bool operator!=(L, R);
1442 where LR is the result of the usual arithmetic conversions between
1445 14For every pair of types T and I, where T is a cv-qualified or cv-
1446 unqualified complete object type and I is a promoted integral type,
1447 there exist candidate operator functions of the form
1448 T* operator+(T*, I);
1449 T& operator[](T*, I);
1450 T* operator-(T*, I);
1451 T* operator+(I, T*);
1452 T& operator[](I, T*);
1454 15For every T, where T is a pointer to complete object type, there exist
1455 candidate operator functions of the form112)
1456 ptrdiff_t operator-(T, T);
1458 16For every pointer type T, there exist candidate operator functions of
1460 bool operator<(T, T);
1461 bool operator>(T, T);
1462 bool operator<=(T, T);
1463 bool operator>=(T, T);
1464 bool operator==(T, T);
1465 bool operator!=(T, T);
1467 17For every pointer to member type T, there exist candidate operator
1468 functions of the form
1469 bool operator==(T, T);
1470 bool operator!=(T, T); */
1473 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1475 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1477 type2 = ptrdiff_type_node;
1481 case TRUNC_DIV_EXPR:
1482 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1488 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1489 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1491 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1492 && null_ptr_cst_p (args[1]))
1497 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1498 && null_ptr_cst_p (args[0]))
1509 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1510 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1512 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1517 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1525 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1528 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1530 type1 = ptrdiff_type_node;
1533 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1535 type2 = ptrdiff_type_node;
1540 /* 18For every pair of promoted integral types L and R, there exist candi-
1541 date operator functions of the form
1548 where LR is the result of the usual arithmetic conversions between
1551 case TRUNC_MOD_EXPR:
1557 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1561 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1562 type, VQ is either volatile or empty, and R is a promoted arithmetic
1563 type, there exist candidate operator functions of the form
1564 VQ L& operator=(VQ L&, R);
1565 VQ L& operator*=(VQ L&, R);
1566 VQ L& operator/=(VQ L&, R);
1567 VQ L& operator+=(VQ L&, R);
1568 VQ L& operator-=(VQ L&, R);
1570 20For every pair T, VQ), where T is any type and VQ is either volatile
1571 or empty, there exist candidate operator functions of the form
1572 T*VQ& operator=(T*VQ&, T*);
1574 21For every pair T, VQ), where T is a pointer to member type and VQ is
1575 either volatile or empty, there exist candidate operator functions of
1577 VQ T& operator=(VQ T&, T);
1579 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1580 unqualified complete object type, VQ is either volatile or empty, and
1581 I is a promoted integral type, there exist candidate operator func-
1583 T*VQ& operator+=(T*VQ&, I);
1584 T*VQ& operator-=(T*VQ&, I);
1586 23For every triple L, VQ, R), where L is an integral or enumeration
1587 type, VQ is either volatile or empty, and R is a promoted integral
1588 type, there exist candidate operator functions of the form
1590 VQ L& operator%=(VQ L&, R);
1591 VQ L& operator<<=(VQ L&, R);
1592 VQ L& operator>>=(VQ L&, R);
1593 VQ L& operator&=(VQ L&, R);
1594 VQ L& operator^=(VQ L&, R);
1595 VQ L& operator|=(VQ L&, R); */
1602 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1604 type2 = ptrdiff_type_node;
1608 case TRUNC_DIV_EXPR:
1609 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1613 case TRUNC_MOD_EXPR:
1619 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1624 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1626 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1627 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1628 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1629 || ((TYPE_PTRMEMFUNC_P (type1)
1630 || TREE_CODE (type1) == POINTER_TYPE)
1631 && null_ptr_cst_p (args[1])))
1639 my_friendly_abort (367);
1641 type1 = build_reference_type (type1);
1645 /* Kludge around broken overloading rules whereby
1646 bool ? const char& : enum is ambiguous
1647 (between int and const char&). */
1648 flags |= LOOKUP_NO_TEMP_BIND;
1650 /* Extension: Support ?: of enumeral type. Hopefully this will not
1651 be an extension for long. */
1652 if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
1654 else if (TREE_CODE (type1) == ENUMERAL_TYPE
1655 || TREE_CODE (type2) == ENUMERAL_TYPE)
1657 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1659 if (TREE_CODE (type1) == TREE_CODE (type2)
1660 && (TREE_CODE (type1) == REFERENCE_TYPE
1661 || TREE_CODE (type1) == POINTER_TYPE
1662 || TYPE_PTRMEMFUNC_P (type1)
1663 || IS_AGGR_TYPE (type1)))
1665 if (TREE_CODE (type1) == REFERENCE_TYPE
1666 || TREE_CODE (type2) == REFERENCE_TYPE)
1668 if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
1669 && null_ptr_cst_p (args[1]))
1670 || IS_AGGR_TYPE (type1))
1675 if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
1676 && null_ptr_cst_p (args[0]))
1677 || IS_AGGR_TYPE (type2))
1685 my_friendly_abort (367);
1688 /* If we're dealing with two pointer types, we need candidates
1689 for both of them. */
1690 if (type2 && type1 != type2
1691 && TREE_CODE (type1) == TREE_CODE (type2)
1692 && (TREE_CODE (type1) == REFERENCE_TYPE
1693 || (TREE_CODE (type1) == POINTER_TYPE
1694 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1695 || TYPE_PTRMEMFUNC_P (type1)
1696 || IS_AGGR_TYPE (type1)))
1698 candidates = build_builtin_candidate
1699 (candidates, fnname, type1, type1, args, argtypes, flags);
1700 return build_builtin_candidate
1701 (candidates, fnname, type2, type2, args, argtypes, flags);
1704 return build_builtin_candidate
1705 (candidates, fnname, type1, type2, args, argtypes, flags);
1709 type_decays_to (type)
1712 if (TREE_CODE (type) == ARRAY_TYPE)
1713 return build_pointer_type (TREE_TYPE (type));
1714 if (TREE_CODE (type) == FUNCTION_TYPE)
1715 return build_pointer_type (type);
1719 /* There are three conditions of builtin candidates:
1721 1) bool-taking candidates. These are the same regardless of the input.
1722 2) pointer-pair taking candidates. These are generated for each type
1723 one of the input types converts to.
1724 3) arithmetic candidates. According to the WP, we should generate
1725 all of these, but I'm trying not to... */
1727 static struct z_candidate *
1728 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1729 struct z_candidate *candidates;
1730 enum tree_code code, code2;
1735 tree type, argtypes[3], types[2];
1737 for (i = 0; i < 3; ++i)
1740 argtypes[i] = lvalue_type (args[i]);
1742 argtypes[i] = NULL_TREE;
1747 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1748 and VQ is either volatile or empty, there exist candidate operator
1749 functions of the form
1750 VQ T& operator++(VQ T&); */
1752 case POSTINCREMENT_EXPR:
1753 case PREINCREMENT_EXPR:
1754 case POSTDECREMENT_EXPR:
1755 case PREDECREMENT_EXPR:
1760 /* 24There also exist candidate operator functions of the form
1761 bool operator!(bool);
1762 bool operator&&(bool, bool);
1763 bool operator||(bool, bool); */
1765 case TRUTH_NOT_EXPR:
1766 return build_builtin_candidate
1767 (candidates, fnname, boolean_type_node,
1768 NULL_TREE, args, argtypes, flags);
1770 case TRUTH_ORIF_EXPR:
1771 case TRUTH_ANDIF_EXPR:
1772 return build_builtin_candidate
1773 (candidates, fnname, boolean_type_node,
1774 boolean_type_node, args, argtypes, flags);
1785 types[0] = types[1] = NULL_TREE;
1787 for (i = 0; i < 2; ++i)
1791 else if (IS_AGGR_TYPE (argtypes[i]))
1793 tree convs = lookup_conversions (argtypes[i]);
1795 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1798 convs = lookup_conversions (argtypes[i]);
1800 if (code == COND_EXPR)
1802 if (real_lvalue_p (args[i]))
1803 types[i] = scratch_tree_cons
1804 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1806 types[i] = scratch_tree_cons
1807 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1813 for (; convs; convs = TREE_CHAIN (convs))
1815 type = TREE_TYPE (TREE_TYPE (TREE_VALUE (convs)));
1818 && (TREE_CODE (type) != REFERENCE_TYPE
1819 || TYPE_READONLY (TREE_TYPE (type))))
1822 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1823 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1825 type = non_reference (type);
1826 if (i != 0 || ! ref1)
1828 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1829 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1830 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1831 if (INTEGRAL_TYPE_P (type))
1832 type = type_promotes_to (type);
1835 if (! value_member (type, types[i]))
1836 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1841 if (code == COND_EXPR && real_lvalue_p (args[i]))
1842 types[i] = scratch_tree_cons
1843 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1844 type = non_reference (argtypes[i]);
1845 if (i != 0 || ! ref1)
1847 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1848 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1849 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1850 if (INTEGRAL_TYPE_P (type))
1851 type = type_promotes_to (type);
1853 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1857 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1860 for (type = types[1]; type; type = TREE_CHAIN (type))
1861 candidates = add_builtin_candidate
1862 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1863 TREE_VALUE (type), args, argtypes, flags);
1865 candidates = add_builtin_candidate
1866 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1867 NULL_TREE, args, argtypes, flags);
1874 /* If TMPL can be successfully instantiated as indicated by
1875 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
1877 TMPL is the template. EXPLICIT_TARGS are any explicit template
1878 arguments. ARGLIST is the arguments provided at the call-site.
1879 The RETURN_TYPE is the desired type for conversion operators. If
1880 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
1881 OBJ is supplied, FLAGS are ignored, and OBJ is as for
1882 add_conv_candidate. */
1884 static struct z_candidate*
1885 add_template_candidate_real (candidates, tmpl, explicit_targs,
1886 arglist, return_type, flags,
1888 struct z_candidate *candidates;
1889 tree tmpl, explicit_targs, arglist, return_type;
1892 unification_kind_t strict;
1894 int ntparms = DECL_NTPARMS (tmpl);
1895 tree targs = make_scratch_vec (ntparms);
1896 struct z_candidate *cand;
1900 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
1901 return_type, strict, NULL_TREE);
1906 fn = instantiate_template (tmpl, targs);
1907 if (fn == error_mark_node)
1910 if (obj != NULL_TREE)
1911 /* Aha, this is a conversion function. */
1912 cand = add_conv_candidate (candidates, fn, obj, arglist);
1914 cand = add_function_candidate (candidates, fn, arglist, flags);
1915 if (DECL_TI_TEMPLATE (fn) != tmpl)
1916 /* This situation can occur if a member template of a template
1917 class is specialized. Then, instantiate_template might return
1918 an instantiation of the specialization, in which case the
1919 DECL_TI_TEMPLATE field will point at the original
1920 specialization. For example:
1922 template <class T> struct S { template <class U> void f(U);
1923 template <> void f(int) {}; };
1927 Here, TMPL will be template <class U> S<double>::f(U).
1928 And, instantiate template will give us the specialization
1929 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
1930 for this will point at template <class T> template <> S<T>::f(int),
1931 so that we can find the definition. For the purposes of
1932 overload resolution, however, we want the original TMPL. */
1933 cand->template = tree_cons (tmpl, targs, NULL_TREE);
1935 cand->template = DECL_TEMPLATE_INFO (fn);
1941 static struct z_candidate *
1942 add_template_candidate (candidates, tmpl, explicit_targs,
1943 arglist, return_type, flags, strict)
1944 struct z_candidate *candidates;
1945 tree tmpl, explicit_targs, arglist, return_type;
1947 unification_kind_t strict;
1950 add_template_candidate_real (candidates, tmpl, explicit_targs,
1951 arglist, return_type, flags,
1956 static struct z_candidate *
1957 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
1958 struct z_candidate *candidates;
1959 tree tmpl, obj, arglist, return_type;
1962 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
1963 return_type, 0, obj, DEDUCE_CONV);
1969 struct z_candidate *cands;
1971 for (; cands; cands = cands->next)
1972 if (pedantic ? cands->viable == 1 : cands->viable)
1977 static struct z_candidate *
1978 splice_viable (cands)
1979 struct z_candidate *cands;
1981 struct z_candidate **p = &cands;
1985 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
1998 /* Fix this to work on non-lvalues. */
1999 if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
2000 || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
2003 return build_unary_op (ADDR_EXPR, obj, 0);
2007 print_z_candidates (candidates)
2008 struct z_candidate *candidates;
2010 char *str = "candidates are:";
2011 for (; candidates; candidates = candidates->next)
2013 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2015 if (candidates->fn == ansi_opname [COND_EXPR])
2016 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2017 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2018 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2019 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2020 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2021 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2022 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2023 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2025 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2026 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2029 cp_error_at ("%s %+D%s", str, candidates->fn,
2030 candidates->viable == -1 ? " <near match>" : "");
2035 /* Returns the best overload candidate to perform the requested
2036 conversion. This function is used for three the overloading situations
2037 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2038 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2039 per [dcl.init.ref], so we ignore temporary bindings. */
2041 static struct z_candidate *
2042 build_user_type_conversion_1 (totype, expr, flags)
2046 struct z_candidate *candidates, *cand;
2047 tree fromtype = TREE_TYPE (expr);
2048 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2049 tree args = NULL_TREE;
2050 tree templates = NULL_TREE;
2052 if (IS_AGGR_TYPE (totype))
2053 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2054 if (IS_AGGR_TYPE (fromtype)
2055 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2056 convs = lookup_conversions (fromtype);
2059 flags |= LOOKUP_NO_CONVERSION;
2063 tree t = build_int_2 (0, 0);
2064 TREE_TYPE (t) = build_pointer_type (totype);
2065 args = build_scratch_list (NULL_TREE, expr);
2066 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2067 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2068 args = scratch_tree_cons (NULL_TREE, t, args);
2070 ctors = TREE_VALUE (ctors);
2072 for (; ctors; ctors = OVL_NEXT (ctors))
2074 tree ctor = OVL_CURRENT (ctors);
2075 if (DECL_NONCONVERTING_P (ctor))
2078 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2080 templates = scratch_tree_cons (NULL_TREE, ctor, templates);
2082 add_template_candidate (candidates, ctor,
2083 NULL_TREE, args, NULL_TREE, flags,
2087 candidates = add_function_candidate (candidates, ctor,
2092 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2093 candidates->basetype_path = TYPE_BINFO (totype);
2098 args = build_scratch_list (NULL_TREE, build_this (expr));
2100 for (; convs; convs = TREE_CHAIN (convs))
2102 tree fns = TREE_VALUE (convs);
2103 int convflags = LOOKUP_NO_CONVERSION;
2106 /* If we are called to convert to a reference type, we are trying to
2107 find an lvalue binding, so don't even consider temporaries. If
2108 we don't find an lvalue binding, the caller will try again to
2109 look for a temporary binding. */
2110 if (TREE_CODE (totype) == REFERENCE_TYPE)
2111 convflags |= LOOKUP_NO_TEMP_BIND;
2113 if (TREE_CODE (fns) != TEMPLATE_DECL)
2114 ics = implicit_conversion
2115 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2117 /* We can't compute this yet. */
2118 ics = error_mark_node;
2120 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2121 /* ignore the near match. */;
2123 for (; fns; fns = OVL_NEXT (fns))
2125 tree fn = OVL_CURRENT (fns);
2126 struct z_candidate *old_candidates = candidates;
2128 if (TREE_CODE (fn) == TEMPLATE_DECL)
2130 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2132 add_template_candidate (candidates, fn, NULL_TREE,
2133 args, totype, flags,
2137 candidates = add_function_candidate (candidates, fn,
2140 if (candidates != old_candidates)
2142 if (TREE_CODE (fn) == TEMPLATE_DECL)
2143 ics = implicit_conversion
2144 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2147 candidates->second_conv = ics;
2148 candidates->basetype_path = TREE_PURPOSE (convs);
2150 if (ics == NULL_TREE)
2151 candidates->viable = 0;
2152 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2153 candidates->viable = -1;
2158 if (! any_viable (candidates))
2161 if (flags & LOOKUP_COMPLAIN)
2163 if (candidates && ! candidates->next)
2164 /* say why this one won't work or try to be loose */;
2166 cp_error ("no viable candidates");
2173 candidates = splice_viable (candidates);
2174 cand = tourney (candidates);
2178 if (flags & LOOKUP_COMPLAIN)
2180 cp_error ("conversion from `%T' to `%T' is ambiguous",
2182 print_z_candidates (candidates);
2185 cand = candidates; /* any one will do */
2186 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2187 ICS_USER_FLAG (cand->second_conv) = 1;
2188 ICS_BAD_FLAG (cand->second_conv) = 1;
2193 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2194 p = &(TREE_OPERAND (*p, 0));
2196 /* Pedantically, normal function declarations are never considered
2197 to refer to template instantiations, so we only do this with
2199 if (flag_guiding_decls && templates && ! cand->template
2200 && !DECL_INITIAL (cand->fn)
2201 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2202 add_maybe_template (cand->fn, templates);
2206 (DECL_CONSTRUCTOR_P (cand->fn)
2207 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2208 expr, build_expr_ptr_wrapper (cand));
2209 ICS_USER_FLAG (cand->second_conv) = 1;
2210 if (cand->viable == -1)
2211 ICS_BAD_FLAG (cand->second_conv) = 1;
2217 build_user_type_conversion (totype, expr, flags)
2221 struct z_candidate *cand
2222 = build_user_type_conversion_1 (totype, expr, flags);
2226 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2227 return error_mark_node;
2228 return convert_from_reference (convert_like (cand->second_conv, expr));
2233 /* Do any initial processing on the arguments to a function call. */
2240 for (t = args; t; t = TREE_CHAIN (t))
2242 if (TREE_VALUE (t) == error_mark_node)
2243 return error_mark_node;
2244 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2246 error ("invalid use of void expression");
2247 return error_mark_node;
2249 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2250 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2256 build_new_function_call (fn, args)
2259 struct z_candidate *candidates = 0, *cand;
2260 tree explicit_targs = NULL_TREE;
2261 int template_only = 0;
2263 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2265 explicit_targs = TREE_OPERAND (fn, 1);
2266 fn = TREE_OPERAND (fn, 0);
2270 if (really_overloaded_fn (fn))
2273 tree templates = NULL_TREE;
2275 args = resolve_args (args);
2277 if (args == error_mark_node)
2278 return error_mark_node;
2280 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2282 tree t = OVL_FUNCTION (t1);
2283 if (TREE_CODE (t) == TEMPLATE_DECL)
2285 templates = scratch_tree_cons (NULL_TREE, t, templates);
2286 candidates = add_template_candidate
2287 (candidates, t, explicit_targs, args, NULL_TREE,
2288 LOOKUP_NORMAL, DEDUCE_CALL);
2290 else if (! template_only)
2291 candidates = add_function_candidate
2292 (candidates, t, args, LOOKUP_NORMAL);
2295 if (! any_viable (candidates))
2297 if (candidates && ! candidates->next)
2298 return build_function_call (candidates->fn, args);
2299 cp_error ("no matching function for call to `%D (%A)'",
2300 DECL_NAME (OVL_FUNCTION (fn)), args);
2302 print_z_candidates (candidates);
2303 return error_mark_node;
2305 candidates = splice_viable (candidates);
2306 cand = tourney (candidates);
2310 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2311 DECL_NAME (OVL_FUNCTION (fn)), args);
2312 print_z_candidates (candidates);
2313 return error_mark_node;
2316 /* Pedantically, normal function declarations are never considered
2317 to refer to template instantiations, so we only do this with
2319 if (flag_guiding_decls && templates && ! cand->template
2320 && ! DECL_INITIAL (cand->fn))
2321 add_maybe_template (cand->fn, templates);
2323 return build_over_call (cand, args, LOOKUP_NORMAL);
2326 /* This is not really overloaded. */
2327 fn = OVL_CURRENT (fn);
2329 return build_function_call (fn, args);
2333 build_object_call (obj, args)
2336 struct z_candidate *candidates = 0, *cand;
2337 tree fns, convs, mem_args = NULL_TREE;
2338 tree type = TREE_TYPE (obj);
2339 tree templates = NULL_TREE;
2341 if (TYPE_PTRMEMFUNC_P (type))
2343 /* It's no good looking for an overloaded operator() on a
2344 pointer-to-member-function. */
2345 cp_error ("pointer-to-member function %E cannot be called", obj);
2346 cp_error ("without an object; consider using .* or ->*");
2347 return error_mark_node;
2350 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2351 if (fns == error_mark_node)
2352 return error_mark_node;
2354 args = resolve_args (args);
2356 if (args == error_mark_node)
2357 return error_mark_node;
2361 tree base = TREE_PURPOSE (fns);
2362 mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2364 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2366 tree fn = OVL_CURRENT (fns);
2367 if (TREE_CODE (fn) == TEMPLATE_DECL)
2369 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2371 = add_template_candidate (candidates, fn, NULL_TREE,
2372 mem_args, NULL_TREE,
2373 LOOKUP_NORMAL, DEDUCE_CALL);
2376 candidates = add_function_candidate
2377 (candidates, fn, mem_args, LOOKUP_NORMAL);
2380 candidates->basetype_path = base;
2384 convs = lookup_conversions (type);
2386 for (; convs; convs = TREE_CHAIN (convs))
2388 tree fns = TREE_VALUE (convs);
2389 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2392 if (TREE_CODE (totype) == POINTER_TYPE
2393 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2394 for (; fns; fns = OVL_NEXT (fn))
2396 fn = OVL_CURRENT (fn);
2397 if (TREE_CODE (fn) == TEMPLATE_DECL)
2399 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2400 candidates = add_template_conv_candidate (candidates,
2407 candidates = add_conv_candidate (candidates, fn, obj, args);
2410 candidates->basetype_path = TREE_PURPOSE (convs);
2414 if (! any_viable (candidates))
2416 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2417 print_z_candidates (candidates);
2418 return error_mark_node;
2421 candidates = splice_viable (candidates);
2422 cand = tourney (candidates);
2426 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2427 print_z_candidates (candidates);
2428 return error_mark_node;
2431 if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2432 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2434 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2437 return build_function_call (obj, args);
2441 op_error (code, code2, arg1, arg2, arg3, problem)
2442 enum tree_code code, code2;
2443 tree arg1, arg2, arg3;
2447 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2452 cp_error ("%s for `%T ? %T : %T'", problem,
2453 error_type (arg1), error_type (arg2), error_type (arg3));
2455 case POSTINCREMENT_EXPR:
2456 case POSTDECREMENT_EXPR:
2457 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2460 cp_error ("%s for `%T[%T]'", problem,
2461 error_type (arg1), error_type (arg2));
2465 cp_error ("%s for `%T %s %T'", problem,
2466 error_type (arg1), opname, error_type (arg2));
2468 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2473 build_new_op (code, flags, arg1, arg2, arg3)
2474 enum tree_code code;
2476 tree arg1, arg2, arg3;
2478 struct z_candidate *candidates = 0, *cand;
2479 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
2480 enum tree_code code2 = NOP_EXPR;
2481 tree templates = NULL_TREE;
2484 if (arg1 == error_mark_node
2485 || arg2 == error_mark_node
2486 || arg3 == error_mark_node)
2487 return error_mark_node;
2489 /* This can happen if a template takes all non-type parameters, e.g.
2490 undeclared_template<1, 5, 72>a; */
2491 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
2493 cp_error ("`%D' must be declared before use", arg1);
2494 return error_mark_node;
2497 if (code == MODIFY_EXPR)
2499 code2 = TREE_CODE (arg3);
2501 fnname = ansi_assopname[code2];
2504 fnname = ansi_opname[code];
2513 arglist = scratch_tree_cons (NULL_TREE, arg2, arg3);
2514 if (flags & LOOKUP_GLOBAL)
2515 return build_new_function_call
2516 (lookup_function_nonclass (fnname, arglist), arglist);
2519 rval = build_method_call
2520 (build_indirect_ref (build1 (NOP_EXPR, arg1, error_mark_node),
2522 fnname, arglist, NULL_TREE, flags);
2523 if (rval == error_mark_node)
2524 /* User might declare fancy operator new, but invoke it
2525 like standard one. */
2528 TREE_TYPE (rval) = arg1;
2532 case VEC_DELETE_EXPR:
2537 if (flags & LOOKUP_GLOBAL)
2539 arglist = build_scratch_list (NULL_TREE, arg1);
2540 return build_new_function_call
2541 (lookup_function_nonclass (fnname, arglist), arglist);
2544 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2546 arg1 = TREE_TYPE (arg1);
2548 /* This handles the case where we're trying to delete
2553 if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
2555 /* Strip off the pointer and the array. */
2556 arg1 = TREE_TYPE (TREE_TYPE (arg1));
2558 while (TREE_CODE (arg1) == ARRAY_TYPE)
2559 arg1 = (TREE_TYPE (arg1));
2561 arg1 = build_pointer_type (arg1);
2565 rval = build_method_call
2566 (build_indirect_ref (build1 (NOP_EXPR, arg1,
2569 fnname, arglist, NULL_TREE, flags);
2571 /* This can happen when operator delete is protected. */
2572 my_friendly_assert (rval != error_mark_node, 250);
2573 TREE_TYPE (rval) = void_type_node;
2579 return build_object_call (arg1, arg2);
2585 /* The comma operator can have void args. */
2586 if (TREE_CODE (arg1) == OFFSET_REF)
2587 arg1 = resolve_offset_ref (arg1);
2588 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
2589 arg2 = resolve_offset_ref (arg2);
2590 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
2591 arg3 = resolve_offset_ref (arg3);
2593 if (code == COND_EXPR)
2595 if (arg2 == NULL_TREE
2596 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
2597 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
2598 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
2599 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
2602 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
2603 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
2606 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2607 arg2 = integer_zero_node;
2610 arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
2611 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
2613 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2615 arglist = build_scratch_list (NULL_TREE, arg1);
2617 fns = lookup_function_nonclass (fnname, arglist);
2619 if (fns && TREE_CODE (fns) == TREE_LIST)
2620 fns = TREE_VALUE (fns);
2621 for (; fns; fns = OVL_NEXT (fns))
2623 tree fn = OVL_CURRENT (fns);
2624 if (TREE_CODE (fn) == TEMPLATE_DECL)
2626 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2628 = add_template_candidate (candidates, fn, NULL_TREE,
2629 arglist, TREE_TYPE (fnname),
2630 flags, DEDUCE_CALL);
2633 candidates = add_function_candidate (candidates, fn, arglist, flags);
2636 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
2638 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
2639 if (fns == error_mark_node)
2647 tree basetype = TREE_PURPOSE (fns);
2648 mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
2649 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2651 tree fn = OVL_CURRENT (fns);
2654 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
2655 this_arglist = mem_arglist;
2657 this_arglist = arglist;
2659 if (TREE_CODE (fn) == TEMPLATE_DECL)
2661 /* A member template. */
2662 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2664 = add_template_candidate (candidates, fn, NULL_TREE,
2665 this_arglist, TREE_TYPE (fnname),
2666 flags, DEDUCE_CALL);
2669 candidates = add_function_candidate
2670 (candidates, fn, this_arglist, flags);
2673 candidates->basetype_path = basetype;
2680 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
2681 to know about two args; a builtin candidate will always have a first
2682 parameter of type bool. We'll handle that in
2683 build_builtin_candidate. */
2684 if (code == COND_EXPR)
2694 args[2] = NULL_TREE;
2697 candidates = add_builtin_candidates
2698 (candidates, code, code2, fnname, args, flags);
2701 if (! any_viable (candidates))
2705 case POSTINCREMENT_EXPR:
2706 case POSTDECREMENT_EXPR:
2707 /* Look for an `operator++ (int)'. If they didn't have
2708 one, then we fall back to the old way of doing things. */
2709 if (flags & LOOKUP_COMPLAIN)
2710 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
2711 fnname, opname_tab [code]);
2712 if (code == POSTINCREMENT_EXPR)
2713 code = PREINCREMENT_EXPR;
2715 code = PREDECREMENT_EXPR;
2716 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
2718 /* The caller will deal with these. */
2727 if (flags & LOOKUP_COMPLAIN)
2729 op_error (code, code2, arg1, arg2, arg3, "no match");
2730 print_z_candidates (candidates);
2732 return error_mark_node;
2734 candidates = splice_viable (candidates);
2735 cand = tourney (candidates);
2739 if (flags & LOOKUP_COMPLAIN)
2741 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
2742 print_z_candidates (candidates);
2744 return error_mark_node;
2747 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
2749 extern int warn_synth;
2751 && fnname == ansi_opname[MODIFY_EXPR]
2752 && DECL_ARTIFICIAL (cand->fn)
2754 && ! candidates->next->next)
2756 cp_warning ("using synthesized `%#D' for copy assignment",
2758 cp_warning_at (" where cfront would use `%#D'",
2760 ? candidates->next->fn
2764 /* Pedantically, normal function declarations are never considered
2765 to refer to template instantiations, so we only do this with
2767 if (flag_guiding_decls && templates && ! cand->template
2768 && ! DECL_INITIAL (cand->fn)
2769 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2770 add_maybe_template (cand->fn, templates);
2772 return build_over_call
2774 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
2775 ? mem_arglist : arglist,
2779 /* Check for comparison of different enum types. */
2788 if (flag_int_enum_equivalence == 0
2789 && TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
2790 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
2791 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
2792 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
2794 cp_warning ("comparison between `%#T' and `%#T'",
2795 TREE_TYPE (arg1), TREE_TYPE (arg2));
2802 /* We need to strip any leading REF_BIND so that bitfields don't cause
2803 errors. This should not remove any important conversions, because
2804 builtins don't apply to class objects directly. */
2805 conv = TREE_VEC_ELT (cand->convs, 0);
2806 if (TREE_CODE (conv) == REF_BIND)
2807 conv = TREE_OPERAND (conv, 0);
2808 arg1 = convert_like (conv, arg1);
2810 arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
2812 arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
2818 return build_modify_expr (arg1, code2, arg2);
2821 return build_indirect_ref (arg1, "unary *");
2826 case TRUNC_DIV_EXPR:
2837 case TRUNC_MOD_EXPR:
2841 case TRUTH_ANDIF_EXPR:
2842 case TRUTH_ORIF_EXPR:
2843 return build_binary_op_nodefault (code, arg1, arg2, code);
2848 case TRUTH_NOT_EXPR:
2849 case PREINCREMENT_EXPR:
2850 case POSTINCREMENT_EXPR:
2851 case PREDECREMENT_EXPR:
2852 case POSTDECREMENT_EXPR:
2855 return build_unary_op (code, arg1, candidates != 0);
2858 return build_array_ref (arg1, arg2);
2861 return build_conditional_expr (arg1, arg2, arg3);
2864 return build_m_component_ref
2865 (build_indirect_ref (arg1, NULL_PTR), arg2);
2867 /* The caller will deal with these. */
2874 my_friendly_abort (367);
2879 /* Build up a call to operator new. This has to be handled differently
2880 from other operators in the way lookup is handled; first members are
2881 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
2882 TYPE is the type to be created. ARGS are any new-placement args.
2883 FLAGS are the usual overloading flags. */
2886 build_op_new_call (code, type, args, flags)
2887 enum tree_code code;
2891 tree fnname = ansi_opname[code];
2893 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
2894 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
2896 tree dummy = build1 (NOP_EXPR, build_pointer_type (type),
2898 dummy = build_indirect_ref (dummy, "new");
2899 return build_method_call (dummy, fnname, args, NULL_TREE, flags);
2902 return build_new_function_call
2903 (lookup_function_nonclass (fnname, args), args);
2906 /* Build a call to operator delete. This has to be handled very specially,
2907 because the restrictions on what signatures match are different from all
2908 other call instances. For a normal delete, only a delete taking (void *)
2909 or (void *, size_t) is accepted. For a placement delete, only an exact
2910 match with the placement new is accepted.
2912 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
2913 ADDR is the pointer to be deleted. For placement delete, it is also
2914 used to determine what the corresponding new looked like.
2915 SIZE is the size of the memory block to be deleted.
2916 FLAGS are the usual overloading flags.
2917 PLACEMENT is the corresponding placement new call, or 0. */
2920 build_op_delete_call (code, addr, size, flags, placement)
2921 enum tree_code code;
2922 tree addr, size, placement;
2925 tree fn, fns, fnname, fntype, argtypes, args, type;
2927 if (addr == error_mark_node)
2928 return error_mark_node;
2930 type = TREE_TYPE (TREE_TYPE (addr));
2931 fnname = ansi_opname[code];
2933 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
2936 If the result of the lookup is ambiguous or inaccessible, or if
2937 the lookup selects a placement deallocation function, the
2938 program is ill-formed.
2940 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
2942 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
2943 if (fns == error_mark_node)
2944 return error_mark_node;
2949 if (fns == NULL_TREE)
2950 fns = lookup_name_nonclass (fnname);
2954 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
2956 /* Extract the function. */
2957 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
2958 /* Then the second parm type. */
2959 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
2961 /* Also the second argument. */
2962 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
2966 /* First try it without the size argument. */
2967 argtypes = void_list_node;
2971 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
2972 fntype = build_function_type (void_type_node, argtypes);
2974 /* Strip const and volatile from addr. */
2975 if (type != TYPE_MAIN_VARIANT (type))
2976 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
2978 /* instantiate_type will always return a plain function; pretend it's
2980 if (TREE_CODE (fns) == FUNCTION_DECL)
2981 fns = scratch_ovl_cons (fns, NULL_TREE);
2983 fn = instantiate_type (fntype, fns, 0);
2985 if (fn != error_mark_node)
2987 if (TREE_CODE (fns) == TREE_LIST)
2988 /* Member functions. */
2989 enforce_access (TREE_PURPOSE (fns), fn);
2990 return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
2993 /* If we are doing placement delete we do nothing if we don't find a
2994 matching op delete. */
2998 /* Normal delete; now try to find a match including the size argument. */
2999 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3000 tree_cons (NULL_TREE, sizetype, void_list_node));
3001 fntype = build_function_type (void_type_node, argtypes);
3003 fn = instantiate_type (fntype, fns, 0);
3005 if (fn != error_mark_node)
3007 if (TREE_CODE (fns) == TREE_LIST)
3008 /* Member functions. */
3009 enforce_access (TREE_PURPOSE (fns), fn);
3010 return build_function_call
3011 (fn, expr_tree_cons (NULL_TREE, addr,
3012 build_expr_list (NULL_TREE, size)));
3015 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3016 destructor, in which case the error should be deferred
3017 until someone actually tries to delete one of these. */
3018 if (flags & LOOKUP_SPECULATIVELY)
3021 cp_error ("no suitable operator delete for `%T'", type);
3022 return error_mark_node;
3025 /* If the current scope isn't allowed to access DECL along
3026 BASETYPE_PATH, give an error. */
3029 enforce_access (basetype_path, decl)
3030 tree basetype_path, decl;
3032 tree access = compute_access (basetype_path, decl);
3034 if (access == access_private_node)
3036 cp_error_at ("`%+#D' is %s", decl,
3037 TREE_PRIVATE (decl) ? "private"
3038 : "from private base class");
3039 error ("within this context");
3041 else if (access == access_protected_node)
3043 cp_error_at ("`%+#D' %s", decl,
3044 TREE_PROTECTED (decl) ? "is protected"
3045 : "has protected accessibility");
3046 error ("within this context");
3050 /* Perform the conversions in CONVS on the expression EXPR. */
3053 convert_like (convs, expr)
3056 if (ICS_BAD_FLAG (convs)
3057 && TREE_CODE (convs) != USER_CONV
3058 && TREE_CODE (convs) != AMBIG_CONV)
3061 for (; t; t = TREE_OPERAND (t, 0))
3063 if (TREE_CODE (t) == USER_CONV)
3065 expr = convert_like (t, expr);
3068 else if (TREE_CODE (t) == AMBIG_CONV)
3069 return convert_like (t, expr);
3070 else if (TREE_CODE (t) == IDENTITY_CONV)
3073 return convert_for_initialization
3074 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3075 "conversion", NULL_TREE, 0);
3078 switch (TREE_CODE (convs))
3082 struct z_candidate *cand
3083 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3087 if (DECL_CONSTRUCTOR_P (fn))
3089 tree t = build_int_2 (0, 0);
3090 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3092 args = build_scratch_list (NULL_TREE, expr);
3093 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3094 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3095 args = scratch_tree_cons (NULL_TREE, t, args);
3098 args = build_this (expr);
3099 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3101 /* If this is a constructor or a function returning an aggr type,
3102 we need to build up a TARGET_EXPR. */
3103 if (DECL_CONSTRUCTOR_P (fn))
3104 expr = build_cplus_new (TREE_TYPE (convs), expr);
3109 if (type_unknown_p (expr))
3110 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3111 if (TREE_READONLY_DECL_P (expr))
3112 expr = decl_constant_value (expr);
3115 /* Call build_user_type_conversion again for the error. */
3116 return build_user_type_conversion
3117 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3123 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3124 if (expr == error_mark_node)
3125 return error_mark_node;
3127 switch (TREE_CODE (convs))
3130 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3132 /* else fall through */
3135 tree cvt_expr = build_user_type_conversion
3136 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3139 /* This can occur if, for example, the EXPR has incomplete
3140 type. We can't check for that before attempting the
3141 conversion because the type might be an incomplete
3142 array type, which is OK if some constructor for the
3143 destination type takes a pointer argument. */
3144 if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
3146 if (comptypes (TREE_TYPE (expr), TREE_TYPE (convs), 1))
3147 incomplete_type_error (expr, TREE_TYPE (expr));
3149 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3150 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3153 cp_error ("could not convert `%E' to `%T'",
3154 expr, TREE_TYPE (convs));
3155 return error_mark_node;
3161 return convert_to_reference
3162 (TREE_TYPE (convs), expr,
3163 CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
3166 return decay_conversion (expr);
3171 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3172 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3175 /* ARG is being passed to a varargs function. Perform any conversions
3176 required. Return the converted value. */
3179 convert_arg_to_ellipsis (arg)
3182 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3183 && (TYPE_PRECISION (TREE_TYPE (arg))
3184 < TYPE_PRECISION (double_type_node)))
3185 /* Convert `float' to `double'. */
3186 arg = cp_convert (double_type_node, arg);
3187 else if (IS_AGGR_TYPE (TREE_TYPE (arg))
3188 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (arg)))
3189 cp_warning ("cannot pass objects of type `%T' through `...'",
3192 /* Convert `short' and `char' to full-size `int'. */
3193 arg = default_conversion (arg);
3198 /* ARG is a default argument expression being passed to a parameter of
3199 the indicated TYPE. Do any required conversions. Return the
3203 convert_default_arg (type, arg)
3206 arg = break_out_target_exprs (arg);
3208 if (TREE_CODE (arg) == CONSTRUCTOR)
3210 arg = digest_init (type, arg, 0);
3211 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3212 "default argument", 0, 0);
3216 /* This could get clobbered by the following call. */
3217 if (TREE_HAS_CONSTRUCTOR (arg))
3218 arg = copy_node (arg);
3220 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3221 "default argument", 0, 0);
3222 #ifdef PROMOTE_PROTOTYPES
3223 if ((TREE_CODE (type) == INTEGER_TYPE
3224 || TREE_CODE (type) == ENUMERAL_TYPE)
3225 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3226 arg = default_conversion (arg);
3234 build_over_call (cand, args, flags)
3235 struct z_candidate *cand;
3240 tree convs = cand->convs;
3241 tree converted_args = NULL_TREE;
3242 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3243 tree conv, arg, val;
3247 /* Give any warnings we noticed during overload resolution. */
3249 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3250 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3252 if (DECL_FUNCTION_MEMBER_P (fn))
3253 enforce_access (cand->basetype_path, fn);
3255 if (args && TREE_CODE (args) != TREE_LIST)
3256 args = build_scratch_list (NULL_TREE, args);
3259 /* The implicit parameters to a constructor are not considered by overload
3260 resolution, and must be of the proper type. */
3261 if (DECL_CONSTRUCTOR_P (fn))
3263 converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3264 arg = TREE_CHAIN (arg);
3265 parm = TREE_CHAIN (parm);
3266 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3268 converted_args = expr_tree_cons
3269 (NULL_TREE, TREE_VALUE (arg), converted_args);
3270 arg = TREE_CHAIN (arg);
3271 parm = TREE_CHAIN (parm);
3274 /* Bypass access control for 'this' parameter. */
3275 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3277 tree parmtype = TREE_VALUE (parm);
3278 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3279 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3281 int dv = (TYPE_VOLATILE (TREE_TYPE (parmtype))
3282 < TYPE_VOLATILE (TREE_TYPE (argtype)));
3283 int dc = (TYPE_READONLY (TREE_TYPE (parmtype))
3284 < TYPE_READONLY (TREE_TYPE (argtype)));
3285 char *p = (dv && dc ? "const and volatile"
3286 : dc ? "const" : dv ? "volatile" : "");
3288 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards %s",
3289 TREE_TYPE (argtype), fn, p);
3291 converted_args = expr_tree_cons
3292 (NULL_TREE, convert_force (TREE_VALUE (parm), TREE_VALUE (arg), CONV_C_CAST),
3294 parm = TREE_CHAIN (parm);
3295 arg = TREE_CHAIN (arg);
3301 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3303 tree type = TREE_VALUE (parm);
3305 conv = TREE_VEC_ELT (convs, i);
3306 if (ICS_BAD_FLAG (conv))
3309 val = TREE_VALUE (arg);
3311 for (; t; t = TREE_OPERAND (t, 0))
3313 if (TREE_CODE (t) == USER_CONV
3314 || TREE_CODE (t) == AMBIG_CONV)
3316 val = convert_like (t, val);
3319 else if (TREE_CODE (t) == IDENTITY_CONV)
3322 val = convert_for_initialization
3323 (NULL_TREE, type, val, LOOKUP_NORMAL,
3324 "argument passing", fn, i - is_method);
3327 val = convert_like (conv, TREE_VALUE (arg));
3329 #ifdef PROMOTE_PROTOTYPES
3330 if ((TREE_CODE (type) == INTEGER_TYPE
3331 || TREE_CODE (type) == ENUMERAL_TYPE)
3332 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3333 val = default_conversion (val);
3335 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3338 /* Default arguments */
3339 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3341 tree arg = TREE_PURPOSE (parm);
3343 if (DECL_TEMPLATE_INFO (fn))
3345 /* This came from a template. Instantiate the default arg here,
3346 not in tsubst. In the case of something like:
3354 we must be careful to do name lookup in the scope of
3355 S<T>, rather than in the current class. */
3356 if (DECL_CLASS_SCOPE_P (fn))
3357 pushclass (DECL_REAL_CONTEXT (fn), 2);
3359 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), NULL_TREE);
3361 if (DECL_CLASS_SCOPE_P (fn))
3364 converted_args = expr_tree_cons
3365 (NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg),
3370 for (; arg; arg = TREE_CHAIN (arg))
3372 = expr_tree_cons (NULL_TREE,
3373 convert_arg_to_ellipsis (TREE_VALUE (arg)),
3376 converted_args = nreverse (converted_args);
3378 /* Avoid actually calling copy constructors and copy assignment operators,
3380 if (DECL_CONSTRUCTOR_P (fn)
3381 && TREE_VEC_LENGTH (convs) == 1
3382 && copy_args_p (fn))
3385 arg = TREE_CHAIN (converted_args);
3386 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3387 arg = TREE_CHAIN (arg);
3388 arg = TREE_VALUE (arg);
3390 /* Pull out the real argument, disregarding const-correctness. */
3392 while (TREE_CODE (targ) == NOP_EXPR
3393 || TREE_CODE (targ) == NON_LVALUE_EXPR
3394 || TREE_CODE (targ) == CONVERT_EXPR)
3395 targ = TREE_OPERAND (targ, 0);
3396 if (TREE_CODE (targ) == ADDR_EXPR)
3398 targ = TREE_OPERAND (targ, 0);
3399 if (! comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3400 TYPE_MAIN_VARIANT (TREE_TYPE (targ)), 1))
3409 arg = build_indirect_ref (arg, 0);
3411 /* [class.copy]: the copy constructor is implicitly defined even if
3412 the implementation elided its use. */
3413 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3416 /* If we're creating a temp and we already have one, don't create a
3417 new one. If we're not creating a temp but we get one, use
3418 INIT_EXPR to collapse the temp into our target. Otherwise, if the
3419 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3420 temp or an INIT_EXPR otherwise. */
3421 if (integer_zerop (TREE_VALUE (args)))
3423 if (! real_lvalue_p (arg))
3425 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3427 val = build (VAR_DECL, DECL_CONTEXT (fn));
3428 layout_decl (val, 0);
3429 val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
3430 TREE_SIDE_EFFECTS (val) = 1;
3434 else if (! real_lvalue_p (arg)
3435 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3437 tree to = stabilize_reference
3438 (build_indirect_ref (TREE_VALUE (args), 0));
3440 /* Don't copy the padding byte; it might not have been allocated
3441 if to is a base subobject. */
3442 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3443 return build_unary_op
3444 (ADDR_EXPR, build (COMPOUND_EXPR, TREE_TYPE (to),
3445 cp_convert (void_type_node, arg), to),
3448 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
3449 TREE_SIDE_EFFECTS (val) = 1;
3450 return build_unary_op (ADDR_EXPR, val, 0);
3453 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
3455 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
3457 tree to = stabilize_reference
3458 (build_indirect_ref (TREE_VALUE (converted_args), 0));
3460 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
3462 /* Don't copy the padding byte; it might not have been allocated
3463 if to is a base subobject. */
3464 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3465 return build (COMPOUND_EXPR, TREE_TYPE (to),
3466 cp_convert (void_type_node, arg), to);
3468 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
3469 TREE_SIDE_EFFECTS (val) = 1;
3475 if (DECL_CLASS_SCOPE_P (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
3476 return build_signature_method_call (fn, converted_args);
3477 else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3479 tree t, *p = &TREE_VALUE (converted_args);
3480 tree binfo = get_binfo
3481 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
3482 *p = convert_pointer_to_real (binfo, *p);
3483 if (TREE_SIDE_EFFECTS (*p))
3484 *p = save_expr (*p);
3485 t = build_pointer_type (TREE_TYPE (fn));
3486 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
3489 else if (DECL_INLINE (fn))
3490 fn = inline_conversion (fn);
3492 fn = build_addr_func (fn);
3494 /* Recognize certain built-in functions so we can make tree-codes
3495 other than CALL_EXPR. We do this when it enables fold-const.c
3496 to do something useful. */
3498 if (TREE_CODE (fn) == ADDR_EXPR
3499 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3500 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
3501 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
3506 if (converted_args == 0)
3507 return integer_zero_node;
3508 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
3513 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3514 if (TREE_TYPE (fn) == void_type_node)
3516 fn = require_complete_type (fn);
3517 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
3518 fn = build_cplus_new (TREE_TYPE (fn), fn);
3519 return convert_from_reference (fn);
3523 build_new_method_call (instance, name, args, basetype_path, flags)
3524 tree instance, name, args, basetype_path;
3527 struct z_candidate *candidates = 0, *cand;
3528 tree explicit_targs = NULL_TREE;
3529 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
3531 tree user_args = args;
3532 tree templates = NULL_TREE;
3533 int template_only = 0;
3535 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3537 explicit_targs = TREE_OPERAND (name, 1);
3538 name = TREE_OPERAND (name, 0);
3539 if (TREE_CODE (name) == TEMPLATE_DECL)
3540 name = DECL_NAME (name);
3544 /* If there is an extra argument for controlling virtual bases,
3545 remove it for error reporting. */
3546 if (flags & LOOKUP_HAS_IN_CHARGE)
3547 user_args = TREE_CHAIN (args);
3549 args = resolve_args (args);
3551 if (args == error_mark_node)
3552 return error_mark_node;
3554 if (instance == NULL_TREE)
3555 basetype = BINFO_TYPE (basetype_path);
3558 if (TREE_CODE (instance) == OFFSET_REF)
3559 instance = resolve_offset_ref (instance);
3560 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3561 instance = convert_from_reference (instance);
3562 basetype = TREE_TYPE (instance);
3564 /* XXX this should be handled before we get here. */
3565 if (! IS_AGGR_TYPE (basetype)
3566 && ! (TYPE_LANG_SPECIFIC (basetype)
3567 && (IS_SIGNATURE_POINTER (basetype)
3568 || IS_SIGNATURE_REFERENCE (basetype))))
3570 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
3571 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
3572 name, instance, basetype);
3574 return error_mark_node;
3577 /* If `instance' is a signature pointer/reference and `name' is
3578 not a constructor, we are calling a signature member function.
3579 In that case set the `basetype' to the signature type. */
3580 if ((IS_SIGNATURE_POINTER (basetype)
3581 || IS_SIGNATURE_REFERENCE (basetype))
3582 && TYPE_IDENTIFIER (basetype) != name)
3583 basetype = SIGNATURE_TYPE (basetype);
3586 if (basetype_path == NULL_TREE)
3587 basetype_path = TYPE_BINFO (basetype);
3591 instance_ptr = build_this (instance);
3593 if (! template_only)
3595 /* XXX this should be handled before we get here. */
3596 fns = build_field_call (basetype_path, instance_ptr, name, args);
3603 instance_ptr = build_int_2 (0, 0);
3604 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
3608 = (name == ctor_identifier ? constructor_name (basetype) : name);
3610 fns = lookup_fnfields (basetype_path, name, 1);
3612 if (fns == error_mark_node)
3613 return error_mark_node;
3616 tree fn = TREE_VALUE (fns);
3617 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3618 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3620 flags |= LOOKUP_HAS_IN_CHARGE;
3621 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3623 mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
3624 for (; fn; fn = OVL_NEXT (fn))
3626 tree t = OVL_CURRENT (fn);
3629 /* We can end up here for copy-init of same or base class. */
3630 if (name == ctor_identifier
3631 && (flags & LOOKUP_ONLYCONVERTING)
3632 && DECL_NONCONVERTING_P (t))
3634 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
3635 this_arglist = mem_args;
3637 this_arglist = args;
3639 if (TREE_CODE (t) == TEMPLATE_DECL)
3641 /* A member template. */
3642 templates = scratch_tree_cons (NULL_TREE, t, templates);
3644 add_template_candidate (candidates, t, explicit_targs,
3646 TREE_TYPE (name), flags, DEDUCE_CALL);
3648 else if (! template_only)
3649 candidates = add_function_candidate (candidates, t,
3650 this_arglist, flags);
3653 candidates->basetype_path = TREE_PURPOSE (fns);
3657 if (! any_viable (candidates))
3659 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
3660 if (flags & LOOKUP_SPECULATIVELY)
3662 cp_error ("no matching function for call to `%T::%D (%A)%V'", basetype,
3663 pretty_name, user_args, TREE_TYPE (TREE_TYPE (instance_ptr)));
3664 print_z_candidates (candidates);
3665 return error_mark_node;
3667 candidates = splice_viable (candidates);
3668 cand = tourney (candidates);
3672 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
3674 print_z_candidates (candidates);
3675 return error_mark_node;
3678 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
3679 && instance == current_class_ref
3680 && DECL_CONSTRUCTOR_P (current_function_decl)
3681 && ! (flags & LOOKUP_NONVIRTUAL)
3682 && value_member (cand->fn, get_abstract_virtuals (basetype)))
3683 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
3684 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3685 && TREE_CODE (instance_ptr) == NOP_EXPR
3686 && TREE_OPERAND (instance_ptr, 0) == error_mark_node)
3687 cp_error ("cannot call member function `%D' without object", cand->fn);
3689 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
3690 && ((instance == current_class_ref && (dtor_label || ctor_label))
3691 || resolves_to_fixed_type_p (instance, 0)))
3692 flags |= LOOKUP_NONVIRTUAL;
3694 /* Pedantically, normal function declarations are never considered
3695 to refer to template instantiations, so we only do this with
3697 if (flag_guiding_decls && templates && ! cand->template
3698 && ! DECL_INITIAL (cand->fn))
3699 add_maybe_template (cand->fn, templates);
3701 return build_over_call
3703 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
3707 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
3708 subsequence of ICS2. */
3711 is_subseq (ics1, ics2)
3714 /* We can assume that a conversion of the same code
3715 between the same types indicates a subsequence since we only get
3716 here if the types we are converting from are the same. */
3718 while (TREE_CODE (ics1) == RVALUE_CONV
3719 || TREE_CODE (ics1) == LVALUE_CONV)
3720 ics1 = TREE_OPERAND (ics1, 0);
3724 while (TREE_CODE (ics2) == RVALUE_CONV
3725 || TREE_CODE (ics2) == LVALUE_CONV)
3726 ics2 = TREE_OPERAND (ics2, 0);
3728 if (TREE_CODE (ics2) == USER_CONV
3729 || TREE_CODE (ics2) == AMBIG_CONV
3730 || TREE_CODE (ics2) == IDENTITY_CONV)
3731 /* At this point, ICS1 cannot be a proper subsequence of
3732 ICS2. We can get a USER_CONV when we are comparing the
3733 second standard conversion sequence of two user conversion
3737 ics2 = TREE_OPERAND (ics2, 0);
3739 if (TREE_CODE (ics2) == TREE_CODE (ics1)
3740 && comptypes (TREE_TYPE (ics2), TREE_TYPE (ics1), 1)
3741 && comptypes (TREE_TYPE (TREE_OPERAND (ics2, 0)),
3742 TREE_TYPE (TREE_OPERAND (ics1, 0)), 1))
3747 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
3748 be any _TYPE nodes. */
3751 is_properly_derived_from (derived, base)
3755 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
3756 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
3759 /* We only allow proper derivation here. The DERIVED_FROM_P macro
3760 considers every class derived from itself. */
3761 return (!comptypes (TYPE_MAIN_VARIANT (derived),
3762 TYPE_MAIN_VARIANT (base), 1)
3763 && DERIVED_FROM_P (base, derived));
3766 /* We build the ICS for an implicit object parameter as a pointer
3767 conversion sequence. However, such a sequence should be compared
3768 as if it were a reference conversion sequence. If ICS is the
3769 implicit conversion sequence for an implicit object parameter,
3770 modify it accordingly. */
3773 maybe_handle_implicit_object (ics)
3776 if (ICS_THIS_FLAG (*ics))
3778 /* [over.match.funcs]
3780 For non-static member functions, the type of the
3781 implicit object parameter is "reference to cv X"
3782 where X is the class of which the function is a
3783 member and cv is the cv-qualification on the member
3784 function declaration. */
3786 if (TREE_CODE (t) == QUAL_CONV)
3787 t = TREE_OPERAND (t, 0);
3788 if (TREE_CODE (t) == PTR_CONV)
3789 t = TREE_OPERAND (t, 0);
3790 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3791 t = build_conv (REF_BIND,
3792 build_reference_type (TREE_TYPE (TREE_TYPE (*ics))),
3794 ICS_STD_RANK (t) = ICS_STD_RANK (*ics);
3799 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
3800 to the type the reference originally referred to, and return 1.
3801 Otherwise, return 0. */
3804 maybe_handle_ref_bind (ics, target_type)
3808 if (TREE_CODE (*ics) == REF_BIND)
3812 When a parameter of reference type binds directly
3813 (_dcl.init.ref_) to an argument expression, the implicit
3814 conversion sequence is the identity conversion, unless the
3815 argument expression has a type that is a derived class of the
3816 parameter type, in which case the implicit conversion
3817 sequence is a derived-to-base Conversion.
3819 If the parameter binds directly to the result of applying a
3820 conversion function to the argument expression, the implicit
3821 conversion sequence is a user-defined conversion sequence
3822 (_over.ics.user_), with the second standard conversion
3823 sequence either an identity conversion or, if the conversion
3824 function returns an entity of a type that is a derived class
3825 of the parameter type, a derived-to-base Conversion.
3827 When a parameter of reference type is not bound directly to
3828 an argument expression, the conversion sequence is the one
3829 required to convert the argument expression to the underlying
3830 type of the reference according to _over.best.ics_.
3831 Conceptually, this conversion sequence corresponds to
3832 copy-initializing a temporary of the underlying type with the
3833 argument expression. Any difference in top-level
3834 cv-qualification is subsumed by the initialization itself and
3835 does not constitute a conversion. */
3837 tree old_ics = *ics;
3839 *target_type = TREE_TYPE (TREE_TYPE (*ics));
3840 *ics = TREE_OPERAND (*ics, 0);
3841 if (TREE_CODE (*ics) == IDENTITY_CONV
3842 && is_properly_derived_from (TREE_TYPE (*ics), *target_type))
3843 *ics = build_conv (BASE_CONV, *target_type, *ics);
3844 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
3845 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
3853 /* Compare two implicit conversion sequences according to the rules set out in
3854 [over.ics.rank]. Return values:
3856 1: ics1 is better than ics2
3857 -1: ics2 is better than ics1
3858 0: ics1 and ics2 are indistinguishable */
3861 compare_ics (ics1, ics2)
3868 tree deref_from_type1 = NULL_TREE;
3869 tree deref_from_type2;
3870 tree deref_to_type1;
3871 tree deref_to_type2;
3873 /* REF_BINDING is non-zero if the result of the conversion sequence
3874 is a reference type. In that case TARGET_TYPE is the
3875 type referred to by the reference. */
3881 /* Handle implicit object parameters. */
3882 maybe_handle_implicit_object (&ics1);
3883 maybe_handle_implicit_object (&ics2);
3885 /* Handle reference parameters. */
3886 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
3887 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
3891 When comparing the basic forms of implicit conversion sequences (as
3892 defined in _over.best.ics_)
3894 --a standard conversion sequence (_over.ics.scs_) is a better
3895 conversion sequence than a user-defined conversion sequence
3896 or an ellipsis conversion sequence, and
3898 --a user-defined conversion sequence (_over.ics.user_) is a
3899 better conversion sequence than an ellipsis conversion sequence
3900 (_over.ics.ellipsis_). */
3901 if (ICS_RANK (ics1) > ICS_RANK (ics2))
3903 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
3906 if (ICS_RANK (ics1) == BAD_RANK)
3908 /* Both ICS are bad. We try to make a decision based on what
3909 would have happenned if they'd been good. */
3910 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
3911 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3913 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
3914 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3917 /* We couldn't make up our minds; try to figure it out below. */
3920 if (ICS_ELLIPSIS_FLAG (ics1))
3921 /* Both conversions are ellipsis conversions. */
3924 /* User-defined conversion sequence U1 is a better conversion sequence
3925 than another user-defined conversion sequence U2 if they contain the
3926 same user-defined conversion operator or constructor and if the sec-
3927 ond standard conversion sequence of U1 is better than the second
3928 standard conversion sequence of U2. */
3930 if (ICS_USER_FLAG (ics1))
3934 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
3935 if (TREE_CODE (t1) == AMBIG_CONV)
3937 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
3938 if (TREE_CODE (t2) == AMBIG_CONV)
3941 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
3944 /* We can just fall through here, after setting up
3945 FROM_TYPE1 and FROM_TYPE2. */
3946 from_type1 = TREE_TYPE (t1);
3947 from_type2 = TREE_TYPE (t2);
3951 /* We're dealing with two standard conversion sequences.
3955 Standard conversion sequence S1 is a better conversion
3956 sequence than standard conversion sequence S2 if
3958 --S1 is a proper subsequence of S2 (comparing the conversion
3959 sequences in the canonical form defined by _over.ics.scs_,
3960 excluding any Lvalue Transformation; the identity
3961 conversion sequence is considered to be a subsequence of
3962 any non-identity conversion sequence */
3965 while (TREE_CODE (from_type1) != IDENTITY_CONV)
3966 from_type1 = TREE_OPERAND (from_type1, 0);
3967 from_type1 = TREE_TYPE (from_type1);
3970 while (TREE_CODE (from_type2) != IDENTITY_CONV)
3971 from_type2 = TREE_OPERAND (from_type2, 0);
3972 from_type2 = TREE_TYPE (from_type2);
3975 if (comptypes (from_type1, from_type2, 1))
3977 if (is_subseq (ics1, ics2))
3979 if (is_subseq (ics2, ics1))
3982 /* Otherwise, one sequence cannot be a subsequence of the other; they
3983 don't start with the same type. This can happen when comparing the
3984 second standard conversion sequence in two user-defined conversion
3991 --the rank of S1 is better than the rank of S2 (by the rules
3994 Standard conversion sequences are ordered by their ranks: an Exact
3995 Match is a better conversion than a Promotion, which is a better
3996 conversion than a Conversion.
3998 Two conversion sequences with the same rank are indistinguishable
3999 unless one of the following rules applies:
4001 --A conversion that is not a conversion of a pointer, or pointer
4002 to member, to bool is better than another conversion that is such
4005 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4006 so that we do not have to check it explicitly. */
4007 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4009 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4012 to_type1 = TREE_TYPE (ics1);
4013 to_type2 = TREE_TYPE (ics2);
4015 if (TYPE_PTR_P (from_type1)
4016 && TYPE_PTR_P (from_type2)
4017 && TYPE_PTR_P (to_type1)
4018 && TYPE_PTR_P (to_type2))
4020 deref_from_type1 = TREE_TYPE (from_type1);
4021 deref_from_type2 = TREE_TYPE (from_type2);
4022 deref_to_type1 = TREE_TYPE (to_type1);
4023 deref_to_type2 = TREE_TYPE (to_type2);
4025 /* The rules for pointers to members A::* are just like the rules
4026 for pointers A*, except opposite: if B is derived from A then
4027 A::* converts to B::*, not vice versa. For that reason, we
4028 switch the from_ and to_ variables here. */
4029 else if (TYPE_PTRMEM_P (from_type1)
4030 && TYPE_PTRMEM_P (from_type2)
4031 && TYPE_PTRMEM_P (to_type1)
4032 && TYPE_PTRMEM_P (to_type2))
4034 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4035 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4036 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4037 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4039 else if (TYPE_PTRMEMFUNC_P (from_type1)
4040 && TYPE_PTRMEMFUNC_P (from_type2)
4041 && TYPE_PTRMEMFUNC_P (to_type1)
4042 && TYPE_PTRMEMFUNC_P (to_type2))
4044 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4045 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4046 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4047 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4050 if (deref_from_type1 != NULL_TREE
4051 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4052 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4054 /* This was one of the pointer or pointer-like conversions.
4058 --If class B is derived directly or indirectly from class A,
4059 conversion of B* to A* is better than conversion of B* to
4060 void*, and conversion of A* to void* is better than
4061 conversion of B* to void*. */
4062 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4063 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4065 if (is_properly_derived_from (deref_from_type1,
4068 else if (is_properly_derived_from (deref_from_type2,
4072 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4073 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4075 if (comptypes (deref_from_type1, deref_from_type2, 1))
4077 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4079 if (is_properly_derived_from (deref_from_type1,
4083 /* We know that DEREF_TO_TYPE1 is `void' here. */
4084 else if (is_properly_derived_from (deref_from_type1,
4089 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4090 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4094 --If class B is derived directly or indirectly from class A
4095 and class C is derived directly or indirectly from B,
4097 --conversion of C* to B* is better than conversion of C* to
4100 --conversion of B* to A* is better than conversion of C* to
4102 if (comptypes (deref_from_type1, deref_from_type2, 1))
4104 if (is_properly_derived_from (deref_to_type1,
4107 else if (is_properly_derived_from (deref_to_type2,
4111 else if (comptypes (deref_to_type1, deref_to_type2, 1))
4113 if (is_properly_derived_from (deref_from_type2,
4116 else if (is_properly_derived_from (deref_from_type1,
4122 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4123 && comptypes (from_type1, from_type2, 1))
4127 --binding of an expression of type C to a reference of type
4128 B& is better than binding an expression of type C to a
4129 reference of type A&
4131 --conversion of C to B is better than conversion of C to A, */
4132 if (is_properly_derived_from (from_type1, to_type1)
4133 && is_properly_derived_from (from_type1, to_type2))
4135 if (is_properly_derived_from (to_type1, to_type2))
4137 else if (is_properly_derived_from (to_type2, to_type1))
4141 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4142 && comptypes (to_type1, to_type2, 1))
4146 --binding of an expression of type B to a reference of type
4147 A& is better than binding an expression of type C to a
4148 reference of type A&,
4150 --onversion of B to A is better than conversion of C to A */
4151 if (is_properly_derived_from (from_type1, to_type1)
4152 && is_properly_derived_from (from_type2, to_type1))
4154 if (is_properly_derived_from (from_type2, from_type1))
4156 else if (is_properly_derived_from (from_type1, from_type2))
4163 --S1 and S2 differ only in their qualification conversion and yield
4164 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4165 qualification signature of type T1 is a proper subset of the cv-
4166 qualification signature of type T2 */
4167 if (TREE_CODE (ics1) == QUAL_CONV
4168 && TREE_CODE (ics2) == QUAL_CONV
4169 && comptypes (from_type1, from_type2, 1))
4170 return comp_cv_qual_signature (to_type1, to_type2);
4174 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4175 types to which the references refer are the same type except for
4176 top-level cv-qualifiers, and the type to which the reference
4177 initialized by S2 refers is more cv-qualified than the type to
4178 which the reference initialized by S1 refers */
4180 if (ref_binding1 && ref_binding2
4181 && comptypes (TYPE_MAIN_VARIANT (to_type1),
4182 TYPE_MAIN_VARIANT (to_type2), 1))
4183 return comp_cv_qualification (target_type2, target_type1);
4185 /* Neither conversion sequence is better than the other. */
4189 /* The source type for this standard conversion sequence. */
4195 for (;; t = TREE_OPERAND (t, 0))
4197 if (TREE_CODE (t) == USER_CONV
4198 || TREE_CODE (t) == AMBIG_CONV
4199 || TREE_CODE (t) == IDENTITY_CONV)
4200 return TREE_TYPE (t);
4202 my_friendly_abort (1823);
4205 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4206 a pointer to LOSER and re-running joust to produce the warning if WINNER
4207 is actually used. */
4210 add_warning (winner, loser)
4211 struct z_candidate *winner, *loser;
4213 winner->warnings = expr_tree_cons (NULL_PTR,
4214 build_expr_ptr_wrapper (loser),
4218 /* Compare two candidates for overloading as described in
4219 [over.match.best]. Return values:
4221 1: cand1 is better than cand2
4222 -1: cand2 is better than cand1
4223 0: cand1 and cand2 are indistinguishable */
4226 joust (cand1, cand2, warn)
4227 struct z_candidate *cand1, *cand2;
4231 int i, off1 = 0, off2 = 0, len;
4233 /* Candidates that involve bad conversions are always worse than those
4235 if (cand1->viable > cand2->viable)
4237 if (cand1->viable < cand2->viable)
4240 /* a viable function F1
4241 is defined to be a better function than another viable function F2 if
4242 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4243 ICSi(F2), and then */
4245 /* for some argument j, ICSj(F1) is a better conversion sequence than
4248 /* For comparing static and non-static member functions, we ignore the
4249 implicit object parameter of the non-static function. The WP says to
4250 pretend that the static function has an object parm, but that won't
4251 work with operator overloading. */
4252 len = TREE_VEC_LENGTH (cand1->convs);
4253 if (len != TREE_VEC_LENGTH (cand2->convs))
4255 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4256 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4258 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4259 && DECL_STATIC_FUNCTION_P (cand2->fn))
4265 my_friendly_abort (42);
4268 for (i = 0; i < len; ++i)
4270 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4271 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4272 int comp = compare_ics (t1, t2);
4277 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4278 && TREE_CODE (t1) == STD_CONV
4279 && TREE_CODE (t2) == STD_CONV
4280 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4281 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4282 && (TYPE_PRECISION (TREE_TYPE (t1))
4283 == TYPE_PRECISION (TREE_TYPE (t2)))
4284 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4285 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4288 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4290 struct z_candidate *w, *l;
4292 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4293 w = cand1, l = cand2;
4295 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4296 w = cand2, l = cand1;
4300 cp_warning ("passing `%T' chooses `%T' over `%T'",
4301 type, type1, type2);
4302 cp_warning (" in call to `%D'", w->fn);
4308 if (winner && comp != winner)
4317 /* warn about confusing overload resolution for user-defined conversions,
4318 either between a constructor and a conversion op, or between two
4320 if (winner && cand1->second_conv
4321 && ((DECL_CONSTRUCTOR_P (cand1->fn)
4322 != DECL_CONSTRUCTOR_P (cand2->fn))
4323 /* Don't warn if the two conv ops convert to the same type... */
4324 || (! DECL_CONSTRUCTOR_P (cand1->fn)
4325 && ! comptypes (TREE_TYPE (cand1->second_conv),
4326 TREE_TYPE (cand2->second_conv), 1))))
4328 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4331 struct z_candidate *w, *l;
4333 w = cand1, l = cand2;
4335 w = cand2, l = cand1;
4338 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4339 if (! DECL_CONSTRUCTOR_P (w->fn))
4340 source = TREE_TYPE (source);
4341 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4342 cp_warning (" for conversion from `%T' to `%T'",
4343 source, TREE_TYPE (w->second_conv));
4344 cp_warning (" because conversion sequence for the argument is better");
4355 F1 is a non-template function and F2 is a template function */
4357 if (! cand1->template && cand2->template)
4359 else if (cand1->template && ! cand2->template)
4361 else if (cand1->template && cand2->template)
4362 winner = more_specialized
4363 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4367 the context is an initialization by user-defined conversion (see
4368 _dcl.init_ and _over.match.user_) and the standard conversion
4369 sequence from the return type of F1 to the destination type (i.e.,
4370 the type of the entity being initialized) is a better conversion
4371 sequence than the standard conversion sequence from the return type
4372 of F2 to the destination type. */
4374 if (! winner && cand1->second_conv)
4375 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4377 /* If the built-in candidates are the same, arbitrarily pick one. */
4378 if (! winner && cand1->fn == cand2->fn
4379 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4381 for (i = 0; i < len; ++i)
4382 if (! comptypes (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4383 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i)), 1))
4385 if (i == TREE_VEC_LENGTH (cand1->convs))
4388 /* Kludge around broken overloading rules whereby
4389 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4390 that takes references and another that takes values. */
4391 if (cand1->fn == ansi_opname[COND_EXPR])
4393 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4394 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4395 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4396 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4398 if (comptypes (t1, t2, 1))
4400 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4402 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4410 /* Extension: If the worst conversion for one candidate is worse than the
4411 worst conversion for the other, take the first. */
4412 if (! winner && ! pedantic)
4414 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4416 for (i = 0; i < len; ++i)
4418 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4419 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4420 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
4421 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
4433 /* Given a list of candidates for overloading, find the best one, if any.
4434 This algorithm has a worst case of O(2n) (winner is last), and a best
4435 case of O(n/2) (totally ambiguous); much better than a sorting
4438 static struct z_candidate *
4439 tourney (candidates)
4440 struct z_candidate *candidates;
4442 struct z_candidate *champ = candidates, *challenger;
4444 int champ_compared_to_predecessor = 0;
4446 /* Walk through the list once, comparing each current champ to the next
4447 candidate, knocking out a candidate or two with each comparison. */
4449 for (challenger = champ->next; challenger; )
4451 fate = joust (champ, challenger, 0);
4453 challenger = challenger->next;
4458 champ = challenger->next;
4461 champ_compared_to_predecessor = 0;
4466 champ_compared_to_predecessor = 1;
4469 challenger = champ->next;
4473 /* Make sure the champ is better than all the candidates it hasn't yet
4474 been compared to. */
4476 for (challenger = candidates;
4478 && !(champ_compared_to_predecessor && challenger->next == champ);
4479 challenger = challenger->next)
4481 fate = joust (champ, challenger, 0);
4490 can_convert (to, from)
4493 tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
4494 return (t && ! ICS_BAD_FLAG (t));
4498 can_convert_arg (to, from, arg)
4501 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
4502 return (t && ! ICS_BAD_FLAG (t));