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_default_arg PROTO((tree, tree));
51 static tree convert_like PROTO((tree, tree));
52 static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
54 static tree build_object_call PROTO((tree, tree));
55 static tree resolve_args PROTO((tree));
56 static struct z_candidate * build_user_type_conversion_1
57 PROTO ((tree, tree, int));
58 static void print_z_candidates PROTO((struct z_candidate *));
59 static tree build_this PROTO((tree));
60 static struct z_candidate * splice_viable PROTO((struct z_candidate *));
61 static int any_viable PROTO((struct z_candidate *));
62 static struct z_candidate * add_template_candidate
63 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
65 static struct z_candidate * add_template_candidate_real
66 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
67 tree, unification_kind_t));
68 static struct z_candidate * add_template_conv_candidate
69 PROTO((struct z_candidate *, tree, tree, tree, tree));
70 static struct z_candidate * add_builtin_candidates
71 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
73 static struct z_candidate * add_builtin_candidate
74 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
75 tree, tree, tree, tree *, tree *, int));
76 static int is_complete PROTO((tree));
77 static struct z_candidate * build_builtin_candidate
78 PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
80 static struct z_candidate * add_conv_candidate
81 PROTO((struct z_candidate *, tree, tree, tree));
82 static struct z_candidate * add_function_candidate
83 PROTO((struct z_candidate *, tree, tree, int));
84 static tree implicit_conversion PROTO((tree, tree, tree, int));
85 static tree standard_conversion PROTO((tree, tree, tree));
86 static tree reference_binding PROTO((tree, tree, tree, int));
87 static tree strip_top_quals PROTO((tree));
88 static tree non_reference PROTO((tree));
89 static tree build_conv PROTO((enum tree_code, tree, tree));
90 static int is_subseq PROTO((tree, tree));
91 static int is_properly_derived_from PROTO((tree, tree));
92 static int maybe_handle_ref_bind PROTO((tree*, tree*));
93 static void maybe_handle_implicit_object PROTO((tree*));
96 build_vfield_ref (datum, type)
100 int old_assume_nonnull_objects = flag_assume_nonnull_objects;
102 if (datum == error_mark_node)
103 return error_mark_node;
105 /* Vtable references are always made from non-null objects. */
106 flag_assume_nonnull_objects = 1;
107 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
108 datum = convert_from_reference (datum);
110 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
111 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
112 datum, CLASSTYPE_VFIELD (type));
114 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
115 flag_assume_nonnull_objects = old_assume_nonnull_objects;
120 /* Build a call to a member of an object. I.e., one that overloads
121 operator ()(), or is a pointer-to-function or pointer-to-method. */
124 build_field_call (basetype_path, instance_ptr, name, parms)
125 tree basetype_path, instance_ptr, name, parms;
127 tree field, instance;
129 if (name == ctor_identifier || name == dtor_identifier)
132 if (instance_ptr == current_class_ptr)
134 /* Check to see if we really have a reference to an instance variable
135 with `operator()()' overloaded. */
136 field = IDENTIFIER_CLASS_VALUE (name);
138 if (field == NULL_TREE)
140 cp_error ("`this' has no member named `%D'", name);
141 return error_mark_node;
144 if (TREE_CODE (field) == FIELD_DECL)
146 /* If it's a field, try overloading operator (),
147 or calling if the field is a pointer-to-function. */
148 instance = build_component_ref_1 (current_class_ref, field, 0);
149 if (instance == error_mark_node)
150 return error_mark_node;
152 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance)))
153 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
155 if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
157 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
158 return build_function_call (instance, parms);
159 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
160 return build_function_call (instance, expr_tree_cons (NULL_TREE, current_class_ptr, parms));
166 /* Check to see if this is not really a reference to an instance variable
167 with `operator()()' overloaded. */
168 field = lookup_field (basetype_path, name, 1, 0);
170 /* This can happen if the reference was ambiguous or for access
172 if (field == error_mark_node)
173 return error_mark_node;
175 if (field && TREE_CODE (field) == FIELD_DECL)
178 tree ftype = TREE_TYPE (field);
180 if (TREE_CODE (ftype) == REFERENCE_TYPE)
181 ftype = TREE_TYPE (ftype);
183 if (TYPE_LANG_SPECIFIC (ftype))
185 /* Make the next search for this field very short. */
186 basetype = DECL_FIELD_CONTEXT (field);
187 instance_ptr = convert_pointer_to (basetype, instance_ptr);
189 instance = build_indirect_ref (instance_ptr, NULL_PTR);
190 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
191 build_component_ref_1 (instance, field, 0),
194 if (TREE_CODE (ftype) == POINTER_TYPE)
196 if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
197 || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
199 /* This is a member which is a pointer to function. */
201 = build_component_ref_1 (build_indirect_ref (instance_ptr,
203 field, LOOKUP_COMPLAIN);
204 if (ref == error_mark_node)
205 return error_mark_node;
206 return build_function_call (ref, parms);
209 else if (TREE_CODE (ftype) == METHOD_TYPE)
211 error ("invalid call via pointer-to-member function");
212 return error_mark_node;
221 find_scoped_type (type, inner_name, inner_types)
222 tree type, inner_name, inner_types;
224 tree tags = CLASSTYPE_TAGS (type);
228 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
229 enclosing class) is set to the name for the enum type. So, if
230 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
231 then this test will be true. */
232 if (TREE_PURPOSE (tags) == inner_name)
234 if (inner_types == NULL_TREE)
235 return TYPE_MAIN_DECL (TREE_VALUE (tags));
236 return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
238 tags = TREE_CHAIN (tags);
241 /* Look for a TYPE_DECL. */
242 for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
243 if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
245 /* Code by raeburn. */
246 if (inner_types == NULL_TREE)
248 return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
254 /* Resolve an expression NAME1::NAME2::...::NAMEn to
255 the name that names the above nested type. INNER_TYPES
256 is a chain of nested type names (held together by SCOPE_REFs);
257 OUTER_TYPE is the type we know to enclose INNER_TYPES.
258 Returns NULL_TREE if there is an error. */
261 resolve_scope_to_name (outer_type, inner_stuff)
262 tree outer_type, inner_stuff;
265 tree inner_name, inner_type;
267 if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
269 /* We first try to look for a nesting in our current class context,
270 then try any enclosing classes. */
271 tree type = current_class_type;
273 while (type && (TREE_CODE (type) == RECORD_TYPE
274 || TREE_CODE (type) == UNION_TYPE))
276 tree rval = resolve_scope_to_name (type, inner_stuff);
278 if (rval != NULL_TREE)
280 type = DECL_CONTEXT (TYPE_MAIN_DECL (type));
284 if (TREE_CODE (inner_stuff) == SCOPE_REF)
286 inner_name = TREE_OPERAND (inner_stuff, 0);
287 inner_type = TREE_OPERAND (inner_stuff, 1);
291 inner_name = inner_stuff;
292 inner_type = NULL_TREE;
295 if (outer_type == NULL_TREE)
298 /* If we have something that's already a type by itself,
300 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
303 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
308 x = lookup_name (inner_name, 0);
310 if (x && TREE_CODE (x) == NAMESPACE_DECL)
312 x = lookup_namespace_name (x, inner_type);
318 if (! IS_AGGR_TYPE (outer_type))
321 /* Look for member classes or enums. */
322 tmp = find_scoped_type (outer_type, inner_name, inner_type);
324 /* If it's not a type in this class, then go down into the
325 base classes and search there. */
326 if (! tmp && TYPE_BINFO (outer_type))
328 tree binfos = TYPE_BINFO_BASETYPES (outer_type);
329 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
331 for (i = 0; i < n_baselinks; i++)
333 tree base_binfo = TREE_VEC_ELT (binfos, i);
334 tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
344 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
345 This is how virtual function calls are avoided. */
348 build_scoped_method_call (exp, basetype, name, parms)
349 tree exp, basetype, name, parms;
351 /* Because this syntactic form does not allow
352 a pointer to a base class to be `stolen',
353 we need not protect the derived->base conversion
356 @@ But we do have to check access privileges later. */
358 tree type = TREE_TYPE (exp);
361 if (type == error_mark_node
362 || basetype == error_mark_node)
363 return error_mark_node;
365 if (processing_template_decl)
367 if (TREE_CODE (name) == BIT_NOT_EXPR
368 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
370 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
372 name = build_min_nt (BIT_NOT_EXPR, type);
374 name = build_min_nt (SCOPE_REF, basetype, name);
375 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
378 if (TREE_CODE (type) == REFERENCE_TYPE)
379 type = TREE_TYPE (type);
381 if (TREE_CODE (basetype) == TREE_VEC)
384 basetype = BINFO_TYPE (binfo);
389 /* Check the destructor call syntax. */
390 if (TREE_CODE (name) == BIT_NOT_EXPR)
392 tmp = TREE_OPERAND (name, 0);
394 if (TREE_CODE (tmp) == TYPE_DECL)
395 tmp = TREE_TYPE (tmp);
396 else if (TREE_CODE_CLASS (TREE_CODE (tmp)) == 't')
398 else if (TREE_CODE (tmp) == IDENTIFIER_NODE)
400 if (IS_AGGR_TYPE (basetype) && tmp == constructor_name (basetype))
403 tmp = get_type_value (tmp);
406 my_friendly_abort (980605);
408 if (! (tmp && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (tmp)))
410 cp_error ("qualified type `%T' does not match destructor name `~%T'",
411 basetype, TREE_OPERAND (name, 0));
412 return error_mark_node;
416 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
417 that explicit ~int is caught in the parser; this deals with typedefs
418 and template parms. */
419 if (TREE_CODE (name) == BIT_NOT_EXPR && ! IS_AGGR_TYPE (basetype))
421 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
422 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
423 exp, basetype, type);
425 return cp_convert (void_type_node, exp);
428 if (! is_aggr_type (basetype, 1))
429 return error_mark_node;
431 if (! IS_AGGR_TYPE (type))
433 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
435 return error_mark_node;
440 binfo = get_binfo (basetype, type, 1);
441 if (binfo == error_mark_node)
442 return error_mark_node;
444 error_not_base_type (basetype, type);
449 if (TREE_CODE (exp) == INDIRECT_REF)
450 decl = build_indirect_ref
451 (convert_pointer_to_real
452 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
454 decl = build_scoped_ref (exp, basetype);
456 /* Call to a destructor. */
457 if (TREE_CODE (name) == BIT_NOT_EXPR)
459 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
460 return cp_convert (void_type_node, exp);
462 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
463 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
467 /* Call to a method. */
468 return build_method_call (decl, name, parms, binfo,
469 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
471 return error_mark_node;
474 /* We want the address of a function or method. We avoid creating a
475 pointer-to-member function. */
478 build_addr_func (function)
481 tree type = TREE_TYPE (function);
483 /* We have to do these by hand to avoid real pointer to member
485 if (TREE_CODE (type) == METHOD_TYPE)
489 type = build_pointer_type (type);
491 if (mark_addressable (function) == 0)
492 return error_mark_node;
494 addr = build1 (ADDR_EXPR, type, function);
496 /* Address of a static or external variable or function counts
498 if (staticp (function))
499 TREE_CONSTANT (addr) = 1;
504 function = default_conversion (function);
509 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
510 POINTER_TYPE to those. Note, pointer to member function types
511 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
514 build_call (function, result_type, parms)
515 tree function, result_type, parms;
517 int is_constructor = 0;
521 function = build_addr_func (function);
523 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
525 sorry ("unable to call pointer to member function here");
526 return error_mark_node;
529 if (TREE_CODE (function) == ADDR_EXPR
530 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
531 decl = TREE_OPERAND (function, 0);
535 if (decl && DECL_CONSTRUCTOR_P (decl))
538 /* Don't pass empty class objects by value. This is useful
539 for tags in STL, which are used to control overload resolution.
540 We don't need to handle other cases of copying empty classes. */
541 if (! decl || ! DECL_BUILT_IN (decl))
542 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
543 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
544 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
546 tree t = make_node (RTL_EXPR);
547 TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
548 RTL_EXPR_RTL (t) = const0_rtx;
549 RTL_EXPR_SEQUENCE (t) = NULL_RTX;
550 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
551 TREE_VALUE (tmp), t);
554 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
555 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
556 TREE_TYPE (function) = result_type;
557 TREE_SIDE_EFFECTS (function) = 1;
562 /* Build something of the form ptr->method (args)
563 or object.method (args). This can also build
564 calls to constructors, and find friends.
566 Member functions always take their class variable
569 INSTANCE is a class instance.
571 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
573 PARMS help to figure out what that NAME really refers to.
575 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
576 down to the real instance type to use for access checking. We need this
577 information to get protected accesses correct. This parameter is used
578 by build_member_call.
580 FLAGS is the logical disjunction of zero or more LOOKUP_
581 flags. See cp-tree.h for more info.
583 If this is all OK, calls build_function_call with the resolved
586 This function must also handle being called to perform
587 initialization, promotion/coercion of arguments, and
588 instantiation of default parameters.
590 Note that NAME may refer to an instance variable name. If
591 `operator()()' is defined for the type of that field, then we return
595 build_method_call (instance, name, parms, basetype_path, flags)
596 tree instance, name, parms, basetype_path;
599 tree basetype, instance_ptr;
601 #ifdef GATHER_STATISTICS
602 n_build_method_call++;
605 if (instance == error_mark_node
606 || name == error_mark_node
607 || parms == error_mark_node
608 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
609 return error_mark_node;
611 if (processing_template_decl)
613 if (TREE_CODE (name) == BIT_NOT_EXPR
614 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
616 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
618 name = build_min_nt (BIT_NOT_EXPR, type);
621 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
624 /* This is the logic that magically deletes the second argument to
625 operator delete, if it is not needed. */
626 if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
628 tree save_last = TREE_CHAIN (parms);
630 /* get rid of unneeded argument */
631 TREE_CHAIN (parms) = NULL_TREE;
632 if (build_method_call (instance, name, parms, basetype_path,
633 (LOOKUP_SPECULATIVELY|flags) & ~LOOKUP_COMPLAIN))
635 /* If it finds a match, return it. */
636 return build_method_call (instance, name, parms, basetype_path, flags);
638 /* If it doesn't work, two argument delete must work */
639 TREE_CHAIN (parms) = save_last;
641 /* We already know whether it's needed or not for vec delete. */
642 else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
643 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
644 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
645 TREE_CHAIN (parms) = NULL_TREE;
647 if (TREE_CODE (name) == BIT_NOT_EXPR)
651 tmp = name = TREE_OPERAND (name, 0);
654 error ("destructors take no parameters");
655 basetype = TREE_TYPE (instance);
656 if (TREE_CODE (basetype) == REFERENCE_TYPE)
657 basetype = TREE_TYPE (basetype);
659 if (TREE_CODE (tmp) == TYPE_DECL)
660 tmp = TREE_TYPE (tmp);
661 else if (TREE_CODE_CLASS (TREE_CODE (tmp)) == 't')
663 else if (TREE_CODE (tmp) == IDENTIFIER_NODE)
665 if (IS_AGGR_TYPE (basetype) && tmp == constructor_name (basetype))
668 tmp = get_type_value (tmp);
671 my_friendly_abort (980605);
673 if (! (tmp && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (tmp)))
675 cp_error ("destructor name `~%T' does not match type `%T' of expression",
677 return cp_convert (void_type_node, instance);
680 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
681 return cp_convert (void_type_node, instance);
682 instance = default_conversion (instance);
683 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
684 return build_delete (build_pointer_type (basetype),
685 instance_ptr, integer_two_node,
686 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
689 return build_new_method_call (instance, name, parms, basetype_path, flags);
692 /* New overloading code. */
702 struct z_candidate *next;
705 #define IDENTITY_RANK 0
711 #define ELLIPSIS_RANK 6
714 #define ICS_RANK(NODE) \
715 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
716 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
717 : ICS_USER_FLAG (NODE) ? USER_RANK \
718 : ICS_STD_RANK (NODE))
720 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
722 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
723 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
724 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
725 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
727 #define USER_CONV_CAND(NODE) \
728 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
729 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
736 || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE))
742 build_conv (code, type, from)
746 tree t = build1 (code, type, from);
747 int rank = ICS_STD_RANK (from);
759 if (rank < EXACT_RANK)
765 ICS_STD_RANK (t) = rank;
766 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
767 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
775 if (TREE_CODE (t) == REFERENCE_TYPE)
784 if (TREE_CODE (t) == ARRAY_TYPE)
786 return TYPE_MAIN_VARIANT (t);
789 /* Returns the standard conversion path (see [conv]) from type FROM to type
790 TO, if any. For proper handling of null pointer constants, you must
791 also pass the expression EXPR to convert from. */
794 standard_conversion (to, from, expr)
797 enum tree_code fcode, tcode;
801 if (TREE_CODE (to) == REFERENCE_TYPE)
803 if (TREE_CODE (from) == REFERENCE_TYPE)
806 from = TREE_TYPE (from);
808 to = strip_top_quals (to);
809 from = strip_top_quals (from);
811 fcode = TREE_CODE (from);
812 tcode = TREE_CODE (to);
814 conv = build1 (IDENTITY_CONV, from, expr);
816 if (fcode == FUNCTION_TYPE)
818 from = build_pointer_type (from);
819 fcode = TREE_CODE (from);
820 conv = build_conv (LVALUE_CONV, from, conv);
822 else if (fcode == ARRAY_TYPE)
824 from = build_pointer_type (TREE_TYPE (from));
825 fcode = TREE_CODE (from);
826 conv = build_conv (LVALUE_CONV, from, conv);
828 else if (fromref || (expr && real_lvalue_p (expr)))
829 conv = build_conv (RVALUE_CONV, from, conv);
834 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
835 && expr && null_ptr_cst_p (expr))
837 conv = build_conv (STD_CONV, to, conv);
839 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
841 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
842 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
844 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
845 TYPE_MAIN_VARIANT (TREE_TYPE (to)), 1))
847 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
848 && ufcode != FUNCTION_TYPE)
850 from = build_pointer_type
851 (cp_build_type_variant (void_type_node,
852 TYPE_READONLY (TREE_TYPE (from)),
853 TYPE_VOLATILE (TREE_TYPE (from))));
854 conv = build_conv (PTR_CONV, from, conv);
856 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
858 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
859 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
861 if (DERIVED_FROM_P (fbase, tbase)
862 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
863 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))),
866 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
867 from = build_pointer_type (from);
868 conv = build_conv (PMEM_CONV, from, conv);
871 else if (IS_AGGR_TYPE (TREE_TYPE (from))
872 && IS_AGGR_TYPE (TREE_TYPE (to)))
874 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
876 from = cp_build_type_variant (TREE_TYPE (to),
877 TYPE_READONLY (TREE_TYPE (from)),
878 TYPE_VOLATILE (TREE_TYPE (from)));
879 from = build_pointer_type (from);
880 conv = build_conv (PTR_CONV, from, conv);
884 if (comptypes (from, to, 1))
886 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
887 conv = build_conv (QUAL_CONV, to, conv);
888 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
890 conv = build_conv (PTR_CONV, to, conv);
891 ICS_BAD_FLAG (conv) = 1;
898 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
900 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
901 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
902 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
903 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
905 if (! DERIVED_FROM_P (fbase, tbase)
906 || ! comptypes (TREE_TYPE (fromfn), TREE_TYPE (tofn), 1)
907 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
908 TREE_CHAIN (TYPE_ARG_TYPES (tofn)), 1)
909 || TYPE_READONLY (fbase) != TYPE_READONLY (tbase)
910 || TYPE_VOLATILE (fbase) != TYPE_VOLATILE (tbase))
913 from = cp_build_type_variant (tbase, TYPE_READONLY (fbase),
914 TYPE_VOLATILE (fbase));
915 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
916 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
917 from = build_ptrmemfunc_type (build_pointer_type (from));
918 conv = build_conv (PMEM_CONV, from, conv);
920 else if (tcode == BOOLEAN_TYPE)
922 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
923 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
926 conv = build_conv (STD_CONV, to, conv);
927 if (fcode == POINTER_TYPE
928 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
929 ICS_STD_RANK (conv) = PBOOL_RANK;
931 /* We don't check for ENUMERAL_TYPE here because there are no standard
932 conversions to enum type. */
933 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
934 || tcode == REAL_TYPE)
936 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
938 conv = build_conv (STD_CONV, to, conv);
940 /* Give this a better rank if it's a promotion. */
941 if (to == type_promotes_to (from)
942 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
943 ICS_STD_RANK (conv) = PROMO_RANK;
945 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
946 && DERIVED_FROM_P (to, from))
948 if (TREE_CODE (conv) == RVALUE_CONV)
949 conv = TREE_OPERAND (conv, 0);
950 conv = build_conv (BASE_CONV, to, conv);
958 /* Returns the conversion path from type FROM to reference type TO for
959 purposes of reference binding. For lvalue binding, either pass a
960 reference type to FROM or an lvalue expression to EXPR.
962 Currently does not distinguish in the generated trees between binding to
963 an lvalue and a temporary. Should it? */
966 reference_binding (rto, rfrom, expr, flags)
967 tree rto, rfrom, expr;
972 tree to = TREE_TYPE (rto);
976 if (TREE_CODE (from) == REFERENCE_TYPE)
977 from = TREE_TYPE (from);
978 else if (! expr || ! real_lvalue_p (expr))
981 related = (comptypes (TYPE_MAIN_VARIANT (to),
982 TYPE_MAIN_VARIANT (from), 1)
983 || (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
984 && DERIVED_FROM_P (to, from)));
986 if (lvalue && related
987 && TYPE_READONLY (to) >= TYPE_READONLY (from)
988 && TYPE_VOLATILE (to) >= TYPE_VOLATILE (from))
990 conv = build1 (IDENTITY_CONV, from, expr);
992 if (comptypes (TYPE_MAIN_VARIANT (to),
993 TYPE_MAIN_VARIANT (from), 1))
994 conv = build_conv (REF_BIND, rto, conv);
997 conv = build_conv (REF_BIND, rto, conv);
998 ICS_STD_RANK (conv) = STD_RANK;
1006 conv = standard_conversion (to, rfrom, expr);
1009 conv = build_conv (REF_BIND, rto, conv);
1011 /* Bind directly to a base subobject of a class rvalue. Do it
1012 after building the conversion for proper handling of ICS_RANK. */
1013 if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
1014 TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
1017 && ((! (TYPE_READONLY (to) && ! TYPE_VOLATILE (to)
1018 && (flags & LOOKUP_NO_TEMP_BIND) == 0))
1019 /* If T1 is reference-related to T2, cv1 must be the same
1020 cv-qualification as, or greater cv-qualification than,
1021 cv2; otherwise, the program is ill-formed. */
1023 && (TYPE_READONLY (to) < TYPE_READONLY (from)
1024 || TYPE_VOLATILE (to) < TYPE_VOLATILE (from)))))
1025 ICS_BAD_FLAG (conv) = 1;
1031 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1032 to type TO. The optional expression EXPR may affect the conversion.
1033 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1037 implicit_conversion (to, from, expr, flags)
1038 tree to, from, expr;
1042 struct z_candidate *cand;
1044 if (expr && type_unknown_p (expr))
1046 expr = instantiate_type (to, expr, 0);
1047 if (expr == error_mark_node)
1049 from = TREE_TYPE (expr);
1052 if (TREE_CODE (to) == REFERENCE_TYPE)
1053 conv = reference_binding (to, from, expr, flags);
1055 conv = standard_conversion (to, from, expr);
1059 else if (expr != NULL_TREE
1060 && (IS_AGGR_TYPE (non_reference (from))
1061 || IS_AGGR_TYPE (non_reference (to)))
1062 && (flags & LOOKUP_NO_CONVERSION) == 0)
1064 cand = build_user_type_conversion_1
1065 (to, expr, LOOKUP_ONLYCONVERTING);
1067 conv = cand->second_conv;
1068 if ((! conv || ICS_BAD_FLAG (conv))
1069 && TREE_CODE (to) == REFERENCE_TYPE
1070 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1072 cand = build_user_type_conversion_1
1073 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1076 if (! TYPE_READONLY (TREE_TYPE (to))
1077 || TYPE_VOLATILE (TREE_TYPE (to)))
1078 ICS_BAD_FLAG (cand->second_conv) = 1;
1079 if (!conv || (ICS_BAD_FLAG (conv)
1080 > ICS_BAD_FLAG (cand->second_conv)))
1081 conv = build_conv (REF_BIND, to, cand->second_conv);
1089 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1092 static struct z_candidate *
1093 add_candidate (candidates, fn, convs, viable)
1094 struct z_candidate *candidates;
1098 struct z_candidate *cand
1099 = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
1102 cand->convs = convs;
1103 cand->second_conv = NULL_TREE;
1104 cand->viable = viable;
1105 cand->basetype_path = NULL_TREE;
1106 cand->template = NULL_TREE;
1107 cand->warnings = NULL_TREE;
1108 cand->next = candidates;
1113 /* Create an overload candidate for the function or method FN called with
1114 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1115 to implicit_conversion. */
1117 static struct z_candidate *
1118 add_function_candidate (candidates, fn, arglist, flags)
1119 struct z_candidate *candidates;
1123 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1126 tree parmnode = parmlist;
1127 tree argnode = arglist;
1130 /* The `this' and `in_chrg' arguments to constructors are not considered
1131 in overload resolution. */
1132 if (DECL_CONSTRUCTOR_P (fn))
1134 parmnode = TREE_CHAIN (parmnode);
1135 argnode = TREE_CHAIN (argnode);
1136 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1138 parmnode = TREE_CHAIN (parmnode);
1139 argnode = TREE_CHAIN (argnode);
1143 len = list_length (argnode);
1144 convs = make_scratch_vec (len);
1146 for (i = 0; i < len; ++i)
1148 tree arg = TREE_VALUE (argnode);
1149 tree argtype = TREE_TYPE (arg);
1152 /* An overloaded function does not have an argument type */
1153 if (TREE_CODE (arg) == OVERLOAD)
1154 argtype = unknown_type_node;
1155 argtype = cp_build_type_variant
1156 (argtype, TREE_READONLY (arg), TREE_THIS_VOLATILE (arg));
1158 if (parmnode == void_list_node)
1161 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1164 t = build1 (IDENTITY_CONV, argtype, arg);
1165 ICS_ELLIPSIS_FLAG (t) = 1;
1168 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1169 && ! DECL_CONSTRUCTOR_P (fn))
1170 ICS_THIS_FLAG (t) = 1;
1172 TREE_VEC_ELT (convs, i) = t;
1176 if (ICS_BAD_FLAG (t))
1180 parmnode = TREE_CHAIN (parmnode);
1181 argnode = TREE_CHAIN (argnode);
1187 /* Make sure there are default args for the rest of the parms. */
1188 for (; parmnode && parmnode != void_list_node;
1189 parmnode = TREE_CHAIN (parmnode))
1190 if (! TREE_PURPOSE (parmnode))
1196 return add_candidate (candidates, fn, convs, viable);
1199 /* Create an overload candidate for the conversion function FN which will
1200 be invoked for expression OBJ, producing a pointer-to-function which
1201 will in turn be called with the argument list ARGLIST, and add it to
1202 CANDIDATES. FLAGS is passed on to implicit_conversion. */
1204 static struct z_candidate *
1205 add_conv_candidate (candidates, fn, obj, arglist)
1206 struct z_candidate *candidates;
1207 tree fn, obj, arglist;
1209 tree totype = TREE_TYPE (TREE_TYPE (fn));
1210 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1211 int i, len = list_length (arglist) + 1;
1212 tree convs = make_scratch_vec (len);
1213 tree parmnode = parmlist;
1214 tree argnode = arglist;
1216 int flags = LOOKUP_NORMAL;
1218 for (i = 0; i < len; ++i)
1220 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1221 tree argtype = lvalue_type (arg);
1225 t = implicit_conversion (totype, argtype, arg, flags);
1226 else if (parmnode == void_list_node)
1229 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1232 t = build1 (IDENTITY_CONV, argtype, arg);
1233 ICS_ELLIPSIS_FLAG (t) = 1;
1236 TREE_VEC_ELT (convs, i) = t;
1240 if (ICS_BAD_FLAG (t))
1247 parmnode = TREE_CHAIN (parmnode);
1248 argnode = TREE_CHAIN (argnode);
1254 for (; parmnode && parmnode != void_list_node;
1255 parmnode = TREE_CHAIN (parmnode))
1256 if (! TREE_PURPOSE (parmnode))
1262 return add_candidate (candidates, fn, convs, viable);
1265 static struct z_candidate *
1266 build_builtin_candidate (candidates, fnname, type1, type2,
1267 args, argtypes, flags)
1268 struct z_candidate *candidates;
1269 tree fnname, type1, type2, *args, *argtypes;
1280 convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1282 for (i = 0; i < 2; ++i)
1287 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1291 /* We need something for printing the candidate. */
1292 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1294 else if (ICS_BAD_FLAG (t))
1296 TREE_VEC_ELT (convs, i) = t;
1299 /* For COND_EXPR we rearranged the arguments; undo that now. */
1302 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1303 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1304 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1306 TREE_VEC_ELT (convs, 0) = t;
1311 return add_candidate (candidates, fnname, convs, viable);
1318 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1321 /* Create any builtin operator overload candidates for the operator in
1322 question given the converted operand types TYPE1 and TYPE2. The other
1323 args are passed through from add_builtin_candidates to
1324 build_builtin_candidate. */
1326 static struct z_candidate *
1327 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1328 args, argtypes, flags)
1329 struct z_candidate *candidates;
1330 enum tree_code code, code2;
1331 tree fnname, type1, type2, *args, *argtypes;
1336 case POSTINCREMENT_EXPR:
1337 case POSTDECREMENT_EXPR:
1338 args[1] = integer_zero_node;
1339 type2 = integer_type_node;
1348 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1349 and VQ is either volatile or empty, there exist candidate operator
1350 functions of the form
1351 VQ T& operator++(VQ T&);
1352 T operator++(VQ T&, int);
1353 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1354 type other than bool, and VQ is either volatile or empty, there exist
1355 candidate operator functions of the form
1356 VQ T& operator--(VQ T&);
1357 T operator--(VQ T&, int);
1358 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1359 complete object type, and VQ is either volatile or empty, there exist
1360 candidate operator functions of the form
1361 T*VQ& operator++(T*VQ&);
1362 T*VQ& operator--(T*VQ&);
1363 T* operator++(T*VQ&, int);
1364 T* operator--(T*VQ&, int); */
1366 case POSTDECREMENT_EXPR:
1367 case PREDECREMENT_EXPR:
1368 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1370 case POSTINCREMENT_EXPR:
1371 case PREINCREMENT_EXPR:
1372 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1373 || TYPE_PTROB_P (type1))
1375 type1 = build_reference_type (type1);
1380 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1381 exist candidate operator functions of the form
1385 8 For every function type T, there exist candidate operator functions of
1387 T& operator*(T*); */
1390 if (TREE_CODE (type1) == POINTER_TYPE
1391 && (TYPE_PTROB_P (type1)
1392 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1396 /* 9 For every type T, there exist candidate operator functions of the form
1399 10For every promoted arithmetic type T, there exist candidate operator
1400 functions of the form
1404 case CONVERT_EXPR: /* unary + */
1405 if (TREE_CODE (type1) == POINTER_TYPE
1406 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1409 if (ARITHMETIC_TYPE_P (type1))
1413 /* 11For every promoted integral type T, there exist candidate operator
1414 functions of the form
1418 if (INTEGRAL_TYPE_P (type1))
1422 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1423 is the same type as C2 or is a derived class of C2, T is a complete
1424 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1425 there exist candidate operator functions of the form
1426 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1427 where CV12 is the union of CV1 and CV2. */
1430 if (TREE_CODE (type1) == POINTER_TYPE
1431 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1433 tree c1 = TREE_TYPE (type1);
1434 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1435 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1436 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1438 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1439 && (TYPE_PTRMEMFUNC_P (type2)
1440 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1445 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1446 didate operator functions of the form
1451 bool operator<(L, R);
1452 bool operator>(L, R);
1453 bool operator<=(L, R);
1454 bool operator>=(L, R);
1455 bool operator==(L, R);
1456 bool operator!=(L, R);
1457 where LR is the result of the usual arithmetic conversions between
1460 14For every pair of types T and I, where T is a cv-qualified or cv-
1461 unqualified complete object type and I is a promoted integral type,
1462 there exist candidate operator functions of the form
1463 T* operator+(T*, I);
1464 T& operator[](T*, I);
1465 T* operator-(T*, I);
1466 T* operator+(I, T*);
1467 T& operator[](I, T*);
1469 15For every T, where T is a pointer to complete object type, there exist
1470 candidate operator functions of the form112)
1471 ptrdiff_t operator-(T, T);
1473 16For every pointer type T, there exist candidate operator functions of
1475 bool operator<(T, T);
1476 bool operator>(T, T);
1477 bool operator<=(T, T);
1478 bool operator>=(T, T);
1479 bool operator==(T, T);
1480 bool operator!=(T, T);
1482 17For every pointer to member type T, there exist candidate operator
1483 functions of the form
1484 bool operator==(T, T);
1485 bool operator!=(T, T); */
1488 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1490 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1492 type2 = ptrdiff_type_node;
1496 case TRUNC_DIV_EXPR:
1497 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1503 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1504 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1506 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1507 && null_ptr_cst_p (args[1]))
1512 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1513 && null_ptr_cst_p (args[0]))
1524 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1525 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1527 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1532 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1540 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1543 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1545 type1 = ptrdiff_type_node;
1548 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1550 type2 = ptrdiff_type_node;
1555 /* 18For every pair of promoted integral types L and R, there exist candi-
1556 date operator functions of the form
1563 where LR is the result of the usual arithmetic conversions between
1566 case TRUNC_MOD_EXPR:
1572 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1576 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1577 type, VQ is either volatile or empty, and R is a promoted arithmetic
1578 type, there exist candidate operator functions of the form
1579 VQ L& operator=(VQ L&, R);
1580 VQ L& operator*=(VQ L&, R);
1581 VQ L& operator/=(VQ L&, R);
1582 VQ L& operator+=(VQ L&, R);
1583 VQ L& operator-=(VQ L&, R);
1585 20For every pair T, VQ), where T is any type and VQ is either volatile
1586 or empty, there exist candidate operator functions of the form
1587 T*VQ& operator=(T*VQ&, T*);
1589 21For every pair T, VQ), where T is a pointer to member type and VQ is
1590 either volatile or empty, there exist candidate operator functions of
1592 VQ T& operator=(VQ T&, T);
1594 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1595 unqualified complete object type, VQ is either volatile or empty, and
1596 I is a promoted integral type, there exist candidate operator func-
1598 T*VQ& operator+=(T*VQ&, I);
1599 T*VQ& operator-=(T*VQ&, I);
1601 23For every triple L, VQ, R), where L is an integral or enumeration
1602 type, VQ is either volatile or empty, and R is a promoted integral
1603 type, there exist candidate operator functions of the form
1605 VQ L& operator%=(VQ L&, R);
1606 VQ L& operator<<=(VQ L&, R);
1607 VQ L& operator>>=(VQ L&, R);
1608 VQ L& operator&=(VQ L&, R);
1609 VQ L& operator^=(VQ L&, R);
1610 VQ L& operator|=(VQ L&, R); */
1617 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1619 type2 = ptrdiff_type_node;
1623 case TRUNC_DIV_EXPR:
1624 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1628 case TRUNC_MOD_EXPR:
1634 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1639 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1641 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1642 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1643 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1644 || ((TYPE_PTRMEMFUNC_P (type1)
1645 || TREE_CODE (type1) == POINTER_TYPE)
1646 && null_ptr_cst_p (args[1])))
1654 my_friendly_abort (367);
1656 type1 = build_reference_type (type1);
1660 /* Kludge around broken overloading rules whereby
1661 bool ? const char& : enum is ambiguous
1662 (between int and const char&). */
1663 flags |= LOOKUP_NO_TEMP_BIND;
1665 /* Extension: Support ?: of enumeral type. Hopefully this will not
1666 be an extension for long. */
1667 if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
1669 else if (TREE_CODE (type1) == ENUMERAL_TYPE
1670 || TREE_CODE (type2) == ENUMERAL_TYPE)
1672 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1674 if (TREE_CODE (type1) == TREE_CODE (type2)
1675 && (TREE_CODE (type1) == REFERENCE_TYPE
1676 || TREE_CODE (type1) == POINTER_TYPE
1677 || TYPE_PTRMEMFUNC_P (type1)
1678 || IS_AGGR_TYPE (type1)))
1680 if (TREE_CODE (type1) == REFERENCE_TYPE
1681 || TREE_CODE (type2) == REFERENCE_TYPE)
1683 if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
1684 && null_ptr_cst_p (args[1]))
1685 || IS_AGGR_TYPE (type1))
1690 if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
1691 && null_ptr_cst_p (args[0]))
1692 || IS_AGGR_TYPE (type2))
1700 my_friendly_abort (367);
1703 /* If we're dealing with two pointer types, we need candidates
1704 for both of them. */
1705 if (type2 && type1 != type2
1706 && TREE_CODE (type1) == TREE_CODE (type2)
1707 && (TREE_CODE (type1) == REFERENCE_TYPE
1708 || (TREE_CODE (type1) == POINTER_TYPE
1709 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1710 || TYPE_PTRMEMFUNC_P (type1)
1711 || IS_AGGR_TYPE (type1)))
1713 candidates = build_builtin_candidate
1714 (candidates, fnname, type1, type1, args, argtypes, flags);
1715 return build_builtin_candidate
1716 (candidates, fnname, type2, type2, args, argtypes, flags);
1719 return build_builtin_candidate
1720 (candidates, fnname, type1, type2, args, argtypes, flags);
1724 type_decays_to (type)
1727 if (TREE_CODE (type) == ARRAY_TYPE)
1728 return build_pointer_type (TREE_TYPE (type));
1729 if (TREE_CODE (type) == FUNCTION_TYPE)
1730 return build_pointer_type (type);
1734 /* There are three conditions of builtin candidates:
1736 1) bool-taking candidates. These are the same regardless of the input.
1737 2) pointer-pair taking candidates. These are generated for each type
1738 one of the input types converts to.
1739 3) arithmetic candidates. According to the WP, we should generate
1740 all of these, but I'm trying not to... */
1742 static struct z_candidate *
1743 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1744 struct z_candidate *candidates;
1745 enum tree_code code, code2;
1750 tree type, argtypes[3], types[2];
1752 for (i = 0; i < 3; ++i)
1755 argtypes[i] = lvalue_type (args[i]);
1757 argtypes[i] = NULL_TREE;
1762 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1763 and VQ is either volatile or empty, there exist candidate operator
1764 functions of the form
1765 VQ T& operator++(VQ T&); */
1767 case POSTINCREMENT_EXPR:
1768 case PREINCREMENT_EXPR:
1769 case POSTDECREMENT_EXPR:
1770 case PREDECREMENT_EXPR:
1775 /* 24There also exist candidate operator functions of the form
1776 bool operator!(bool);
1777 bool operator&&(bool, bool);
1778 bool operator||(bool, bool); */
1780 case TRUTH_NOT_EXPR:
1781 return build_builtin_candidate
1782 (candidates, fnname, boolean_type_node,
1783 NULL_TREE, args, argtypes, flags);
1785 case TRUTH_ORIF_EXPR:
1786 case TRUTH_ANDIF_EXPR:
1787 return build_builtin_candidate
1788 (candidates, fnname, boolean_type_node,
1789 boolean_type_node, args, argtypes, flags);
1800 types[0] = types[1] = NULL_TREE;
1802 for (i = 0; i < 2; ++i)
1806 else if (IS_AGGR_TYPE (argtypes[i]))
1808 tree convs = lookup_conversions (argtypes[i]);
1810 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1813 convs = lookup_conversions (argtypes[i]);
1815 if (code == COND_EXPR)
1817 if (real_lvalue_p (args[i]))
1818 types[i] = scratch_tree_cons
1819 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1821 types[i] = scratch_tree_cons
1822 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1828 for (; convs; convs = TREE_CHAIN (convs))
1830 type = TREE_TYPE (TREE_TYPE (TREE_VALUE (convs)));
1833 && (TREE_CODE (type) != REFERENCE_TYPE
1834 || TYPE_READONLY (TREE_TYPE (type))))
1837 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1838 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1840 type = non_reference (type);
1841 if (i != 0 || ! ref1)
1843 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1844 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1845 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1846 if (INTEGRAL_TYPE_P (type))
1847 type = type_promotes_to (type);
1850 if (! value_member (type, types[i]))
1851 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1856 if (code == COND_EXPR && real_lvalue_p (args[i]))
1857 types[i] = scratch_tree_cons
1858 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1859 type = non_reference (argtypes[i]);
1860 if (i != 0 || ! ref1)
1862 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1863 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1864 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1865 if (INTEGRAL_TYPE_P (type))
1866 type = type_promotes_to (type);
1868 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1872 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1875 for (type = types[1]; type; type = TREE_CHAIN (type))
1876 candidates = add_builtin_candidate
1877 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1878 TREE_VALUE (type), args, argtypes, flags);
1880 candidates = add_builtin_candidate
1881 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1882 NULL_TREE, args, argtypes, flags);
1889 /* If TMPL can be successfully instantiated as indicated by
1890 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
1892 TMPL is the template. EXPLICIT_TARGS are any explicit template
1893 arguments. ARGLIST is the arguments provided at the call-site.
1894 The RETURN_TYPE is the desired type for conversion operators. If
1895 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
1896 OBJ is supplied, FLAGS are ignored, and OBJ is as for
1897 add_conv_candidate. */
1899 static struct z_candidate*
1900 add_template_candidate_real (candidates, tmpl, explicit_targs,
1901 arglist, return_type, flags,
1903 struct z_candidate *candidates;
1904 tree tmpl, explicit_targs, arglist, return_type;
1907 unification_kind_t strict;
1909 int ntparms = DECL_NTPARMS (tmpl);
1910 tree targs = make_scratch_vec (ntparms);
1911 struct z_candidate *cand;
1915 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
1916 return_type, strict, NULL_TREE);
1921 fn = instantiate_template (tmpl, targs);
1922 if (fn == error_mark_node)
1925 if (obj != NULL_TREE)
1926 /* Aha, this is a conversion function. */
1927 cand = add_conv_candidate (candidates, fn, obj, arglist);
1929 cand = add_function_candidate (candidates, fn, arglist, flags);
1930 if (DECL_TI_TEMPLATE (fn) != tmpl)
1931 /* This situation can occur if a member template of a template
1932 class is specialized. Then, instantiate_template might return
1933 an instantiation of the specialization, in which case the
1934 DECL_TI_TEMPLATE field will point at the original
1935 specialization. For example:
1937 template <class T> struct S { template <class U> void f(U);
1938 template <> void f(int) {}; };
1942 Here, TMPL will be template <class U> S<double>::f(U).
1943 And, instantiate template will give us the specialization
1944 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
1945 for this will point at template <class T> template <> S<T>::f(int),
1946 so that we can find the definition. For the purposes of
1947 overload resolution, however, we want the original TMPL. */
1948 cand->template = tree_cons (tmpl, targs, NULL_TREE);
1950 cand->template = DECL_TEMPLATE_INFO (fn);
1956 static struct z_candidate *
1957 add_template_candidate (candidates, tmpl, explicit_targs,
1958 arglist, return_type, flags, strict)
1959 struct z_candidate *candidates;
1960 tree tmpl, explicit_targs, arglist, return_type;
1962 unification_kind_t strict;
1965 add_template_candidate_real (candidates, tmpl, explicit_targs,
1966 arglist, return_type, flags,
1971 static struct z_candidate *
1972 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
1973 struct z_candidate *candidates;
1974 tree tmpl, obj, arglist, return_type;
1977 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
1978 return_type, 0, obj, DEDUCE_CONV);
1984 struct z_candidate *cands;
1986 for (; cands; cands = cands->next)
1987 if (pedantic ? cands->viable == 1 : cands->viable)
1992 static struct z_candidate *
1993 splice_viable (cands)
1994 struct z_candidate *cands;
1996 struct z_candidate **p = &cands;
2000 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2013 /* Fix this to work on non-lvalues. */
2014 if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
2015 || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
2018 return build_unary_op (ADDR_EXPR, obj, 0);
2022 print_z_candidates (candidates)
2023 struct z_candidate *candidates;
2025 char *str = "candidates are:";
2026 for (; candidates; candidates = candidates->next)
2028 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2030 if (candidates->fn == ansi_opname [COND_EXPR])
2031 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2032 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2033 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2034 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2035 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2036 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2037 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2038 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2040 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2041 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2044 cp_error_at ("%s %+D%s", str, candidates->fn,
2045 candidates->viable == -1 ? " <near match>" : "");
2050 /* Returns the best overload candidate to perform the requested
2051 conversion. This function is used for three the overloading situations
2052 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2053 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2054 per [dcl.init.ref], so we ignore temporary bindings. */
2056 static struct z_candidate *
2057 build_user_type_conversion_1 (totype, expr, flags)
2061 struct z_candidate *candidates, *cand;
2062 tree fromtype = TREE_TYPE (expr);
2063 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2064 tree args = NULL_TREE;
2065 tree templates = NULL_TREE;
2067 if (IS_AGGR_TYPE (totype))
2068 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2069 if (IS_AGGR_TYPE (fromtype)
2070 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2071 convs = lookup_conversions (fromtype);
2074 flags |= LOOKUP_NO_CONVERSION;
2078 tree t = build_int_2 (0, 0);
2079 TREE_TYPE (t) = build_pointer_type (totype);
2080 args = build_scratch_list (NULL_TREE, expr);
2081 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2082 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2083 args = scratch_tree_cons (NULL_TREE, t, args);
2085 ctors = TREE_VALUE (ctors);
2087 for (; ctors; ctors = OVL_NEXT (ctors))
2089 tree ctor = OVL_CURRENT (ctors);
2090 if (DECL_NONCONVERTING_P (ctor))
2093 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2095 templates = scratch_tree_cons (NULL_TREE, ctor, templates);
2097 add_template_candidate (candidates, ctor,
2098 NULL_TREE, args, NULL_TREE, flags,
2102 candidates = add_function_candidate (candidates, ctor,
2107 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2108 candidates->basetype_path = TYPE_BINFO (totype);
2113 args = build_scratch_list (NULL_TREE, build_this (expr));
2115 for (; convs; convs = TREE_CHAIN (convs))
2117 tree fns = TREE_VALUE (convs);
2118 int convflags = LOOKUP_NO_CONVERSION;
2121 /* If we are called to convert to a reference type, we are trying to
2122 find an lvalue binding, so don't even consider temporaries. If
2123 we don't find an lvalue binding, the caller will try again to
2124 look for a temporary binding. */
2125 if (TREE_CODE (totype) == REFERENCE_TYPE)
2126 convflags |= LOOKUP_NO_TEMP_BIND;
2128 if (TREE_CODE (fns) != TEMPLATE_DECL)
2129 ics = implicit_conversion
2130 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2132 /* We can't compute this yet. */
2133 ics = error_mark_node;
2135 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2136 /* ignore the near match. */;
2138 for (; fns; fns = OVL_NEXT (fns))
2140 tree fn = OVL_CURRENT (fns);
2141 struct z_candidate *old_candidates = candidates;
2143 if (TREE_CODE (fn) == TEMPLATE_DECL)
2145 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2147 add_template_candidate (candidates, fn, NULL_TREE,
2148 args, totype, flags,
2152 candidates = add_function_candidate (candidates, fn,
2155 if (candidates != old_candidates)
2157 if (TREE_CODE (fn) == TEMPLATE_DECL)
2158 ics = implicit_conversion
2159 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2162 candidates->second_conv = ics;
2163 candidates->basetype_path = TREE_PURPOSE (convs);
2165 if (ics == NULL_TREE)
2166 candidates->viable = 0;
2167 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2168 candidates->viable = -1;
2173 if (! any_viable (candidates))
2176 if (flags & LOOKUP_COMPLAIN)
2178 if (candidates && ! candidates->next)
2179 /* say why this one won't work or try to be loose */;
2181 cp_error ("no viable candidates");
2188 candidates = splice_viable (candidates);
2189 cand = tourney (candidates);
2193 if (flags & LOOKUP_COMPLAIN)
2195 cp_error ("conversion from `%T' to `%T' is ambiguous",
2197 print_z_candidates (candidates);
2200 cand = candidates; /* any one will do */
2201 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2202 ICS_USER_FLAG (cand->second_conv) = 1;
2203 ICS_BAD_FLAG (cand->second_conv) = 1;
2208 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2209 p = &(TREE_OPERAND (*p, 0));
2211 /* Pedantically, normal function declarations are never considered
2212 to refer to template instantiations, so we only do this with
2214 if (flag_guiding_decls && templates && ! cand->template
2215 && !DECL_INITIAL (cand->fn)
2216 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2217 add_maybe_template (cand->fn, templates);
2221 (DECL_CONSTRUCTOR_P (cand->fn)
2222 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2223 expr, build_expr_ptr_wrapper (cand));
2224 ICS_USER_FLAG (cand->second_conv) = 1;
2225 if (cand->viable == -1)
2226 ICS_BAD_FLAG (cand->second_conv) = 1;
2232 build_user_type_conversion (totype, expr, flags)
2236 struct z_candidate *cand
2237 = build_user_type_conversion_1 (totype, expr, flags);
2241 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2242 return error_mark_node;
2243 return convert_from_reference (convert_like (cand->second_conv, expr));
2248 /* Do any initial processing on the arguments to a function call. */
2255 for (t = args; t; t = TREE_CHAIN (t))
2257 if (TREE_VALUE (t) == error_mark_node)
2258 return error_mark_node;
2259 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2261 error ("invalid use of void expression");
2262 return error_mark_node;
2264 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2265 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2271 build_new_function_call (fn, args)
2274 struct z_candidate *candidates = 0, *cand;
2275 tree explicit_targs = NULL_TREE;
2276 int template_only = 0;
2278 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2280 explicit_targs = TREE_OPERAND (fn, 1);
2281 fn = TREE_OPERAND (fn, 0);
2285 if (really_overloaded_fn (fn))
2288 tree templates = NULL_TREE;
2290 args = resolve_args (args);
2292 if (args == error_mark_node)
2293 return error_mark_node;
2295 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2297 tree t = OVL_FUNCTION (t1);
2298 if (TREE_CODE (t) == TEMPLATE_DECL)
2300 templates = scratch_tree_cons (NULL_TREE, t, templates);
2301 candidates = add_template_candidate
2302 (candidates, t, explicit_targs, args, NULL_TREE,
2303 LOOKUP_NORMAL, DEDUCE_CALL);
2305 else if (! template_only)
2306 candidates = add_function_candidate
2307 (candidates, t, args, LOOKUP_NORMAL);
2310 if (! any_viable (candidates))
2312 if (candidates && ! candidates->next)
2313 return build_function_call (candidates->fn, args);
2314 cp_error ("no matching function for call to `%D (%A)'",
2315 DECL_NAME (OVL_FUNCTION (fn)), args);
2317 print_z_candidates (candidates);
2318 return error_mark_node;
2320 candidates = splice_viable (candidates);
2321 cand = tourney (candidates);
2325 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2326 DECL_NAME (OVL_FUNCTION (fn)), args);
2327 print_z_candidates (candidates);
2328 return error_mark_node;
2331 /* Pedantically, normal function declarations are never considered
2332 to refer to template instantiations, so we only do this with
2334 if (flag_guiding_decls && templates && ! cand->template
2335 && ! DECL_INITIAL (cand->fn))
2336 add_maybe_template (cand->fn, templates);
2338 return build_over_call (cand, args, LOOKUP_NORMAL);
2341 /* This is not really overloaded. */
2342 fn = OVL_CURRENT (fn);
2344 return build_function_call (fn, args);
2348 build_object_call (obj, args)
2351 struct z_candidate *candidates = 0, *cand;
2352 tree fns, convs, mem_args = NULL_TREE;
2353 tree type = TREE_TYPE (obj);
2354 tree templates = NULL_TREE;
2356 if (TYPE_PTRMEMFUNC_P (type))
2358 /* It's no good looking for an overloaded operator() on a
2359 pointer-to-member-function. */
2360 cp_error ("pointer-to-member function %E cannot be called", obj);
2361 cp_error ("without an object; consider using .* or ->*");
2362 return error_mark_node;
2365 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2366 if (fns == error_mark_node)
2367 return error_mark_node;
2369 args = resolve_args (args);
2371 if (args == error_mark_node)
2372 return error_mark_node;
2376 tree base = TREE_PURPOSE (fns);
2377 mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2379 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2381 tree fn = OVL_CURRENT (fns);
2382 if (TREE_CODE (fn) == TEMPLATE_DECL)
2384 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2386 = add_template_candidate (candidates, fn, NULL_TREE,
2387 mem_args, NULL_TREE,
2388 LOOKUP_NORMAL, DEDUCE_CALL);
2391 candidates = add_function_candidate
2392 (candidates, fn, mem_args, LOOKUP_NORMAL);
2395 candidates->basetype_path = base;
2399 convs = lookup_conversions (type);
2401 for (; convs; convs = TREE_CHAIN (convs))
2403 tree fns = TREE_VALUE (convs);
2404 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2407 if (TREE_CODE (totype) == POINTER_TYPE
2408 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2409 for (; fns; fns = OVL_NEXT (fn))
2411 fn = OVL_CURRENT (fn);
2412 if (TREE_CODE (fn) == TEMPLATE_DECL)
2414 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2415 candidates = add_template_conv_candidate (candidates,
2422 candidates = add_conv_candidate (candidates, fn, obj, args);
2425 candidates->basetype_path = TREE_PURPOSE (convs);
2429 if (! any_viable (candidates))
2431 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2432 print_z_candidates (candidates);
2433 return error_mark_node;
2436 candidates = splice_viable (candidates);
2437 cand = tourney (candidates);
2441 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2442 print_z_candidates (candidates);
2443 return error_mark_node;
2446 if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2447 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2449 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2452 return build_function_call (obj, args);
2456 op_error (code, code2, arg1, arg2, arg3, problem)
2457 enum tree_code code, code2;
2458 tree arg1, arg2, arg3;
2462 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2467 cp_error ("%s for `%T ? %T : %T'", problem,
2468 error_type (arg1), error_type (arg2), error_type (arg3));
2470 case POSTINCREMENT_EXPR:
2471 case POSTDECREMENT_EXPR:
2472 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2475 cp_error ("%s for `%T[%T]'", problem,
2476 error_type (arg1), error_type (arg2));
2480 cp_error ("%s for `%T %s %T'", problem,
2481 error_type (arg1), opname, error_type (arg2));
2483 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2488 build_new_op (code, flags, arg1, arg2, arg3)
2489 enum tree_code code;
2491 tree arg1, arg2, arg3;
2493 struct z_candidate *candidates = 0, *cand;
2494 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
2495 enum tree_code code2 = NOP_EXPR;
2496 tree templates = NULL_TREE;
2499 if (arg1 == error_mark_node
2500 || arg2 == error_mark_node
2501 || arg3 == error_mark_node)
2502 return error_mark_node;
2504 /* This can happen if a template takes all non-type parameters, e.g.
2505 undeclared_template<1, 5, 72>a; */
2506 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
2508 cp_error ("`%D' must be declared before use", arg1);
2509 return error_mark_node;
2512 if (code == MODIFY_EXPR)
2514 code2 = TREE_CODE (arg3);
2516 fnname = ansi_assopname[code2];
2519 fnname = ansi_opname[code];
2528 arglist = scratch_tree_cons (NULL_TREE, arg2, arg3);
2529 if (flags & LOOKUP_GLOBAL)
2530 return build_new_function_call
2531 (lookup_function_nonclass (fnname, arglist), arglist);
2534 rval = build_method_call
2535 (build_indirect_ref (build1 (NOP_EXPR, arg1, error_mark_node),
2537 fnname, arglist, NULL_TREE, flags);
2538 if (rval == error_mark_node)
2539 /* User might declare fancy operator new, but invoke it
2540 like standard one. */
2543 TREE_TYPE (rval) = arg1;
2547 case VEC_DELETE_EXPR:
2552 if (flags & LOOKUP_GLOBAL)
2554 arglist = build_scratch_list (NULL_TREE, arg1);
2555 return build_new_function_call
2556 (lookup_function_nonclass (fnname, arglist), arglist);
2559 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2561 arg1 = TREE_TYPE (arg1);
2563 /* This handles the case where we're trying to delete
2568 if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
2570 /* Strip off the pointer and the array. */
2571 arg1 = TREE_TYPE (TREE_TYPE (arg1));
2573 while (TREE_CODE (arg1) == ARRAY_TYPE)
2574 arg1 = (TREE_TYPE (arg1));
2576 arg1 = build_pointer_type (arg1);
2580 rval = build_method_call
2581 (build_indirect_ref (build1 (NOP_EXPR, arg1,
2584 fnname, arglist, NULL_TREE, flags);
2586 /* This can happen when operator delete is protected. */
2587 my_friendly_assert (rval != error_mark_node, 250);
2588 TREE_TYPE (rval) = void_type_node;
2594 return build_object_call (arg1, arg2);
2600 /* The comma operator can have void args. */
2601 if (TREE_CODE (arg1) == OFFSET_REF)
2602 arg1 = resolve_offset_ref (arg1);
2603 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
2604 arg2 = resolve_offset_ref (arg2);
2605 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
2606 arg3 = resolve_offset_ref (arg3);
2608 if (code == COND_EXPR)
2610 if (arg2 == NULL_TREE
2611 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
2612 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
2613 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
2614 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
2617 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
2618 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
2621 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2622 arg2 = integer_zero_node;
2625 arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
2626 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
2628 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2630 arglist = build_scratch_list (NULL_TREE, arg1);
2632 fns = lookup_function_nonclass (fnname, arglist);
2634 if (fns && TREE_CODE (fns) == TREE_LIST)
2635 fns = TREE_VALUE (fns);
2636 for (; fns; fns = OVL_NEXT (fns))
2638 tree fn = OVL_CURRENT (fns);
2639 if (TREE_CODE (fn) == TEMPLATE_DECL)
2641 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2643 = add_template_candidate (candidates, fn, NULL_TREE,
2644 arglist, TREE_TYPE (fnname),
2645 flags, DEDUCE_CALL);
2648 candidates = add_function_candidate (candidates, fn, arglist, flags);
2651 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
2653 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
2654 if (fns == error_mark_node)
2662 tree basetype = TREE_PURPOSE (fns);
2663 mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
2664 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2666 tree fn = OVL_CURRENT (fns);
2669 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
2670 this_arglist = mem_arglist;
2672 this_arglist = arglist;
2674 if (TREE_CODE (fn) == TEMPLATE_DECL)
2676 /* A member template. */
2677 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2679 = add_template_candidate (candidates, fn, NULL_TREE,
2680 this_arglist, TREE_TYPE (fnname),
2681 flags, DEDUCE_CALL);
2684 candidates = add_function_candidate
2685 (candidates, fn, this_arglist, flags);
2688 candidates->basetype_path = basetype;
2695 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
2696 to know about two args; a builtin candidate will always have a first
2697 parameter of type bool. We'll handle that in
2698 build_builtin_candidate. */
2699 if (code == COND_EXPR)
2709 args[2] = NULL_TREE;
2712 candidates = add_builtin_candidates
2713 (candidates, code, code2, fnname, args, flags);
2716 if (! any_viable (candidates))
2720 case POSTINCREMENT_EXPR:
2721 case POSTDECREMENT_EXPR:
2722 /* Look for an `operator++ (int)'. If they didn't have
2723 one, then we fall back to the old way of doing things. */
2724 if (flags & LOOKUP_COMPLAIN)
2725 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
2726 fnname, opname_tab [code]);
2727 if (code == POSTINCREMENT_EXPR)
2728 code = PREINCREMENT_EXPR;
2730 code = PREDECREMENT_EXPR;
2731 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
2733 /* The caller will deal with these. */
2742 if (flags & LOOKUP_COMPLAIN)
2744 op_error (code, code2, arg1, arg2, arg3, "no match");
2745 print_z_candidates (candidates);
2747 return error_mark_node;
2749 candidates = splice_viable (candidates);
2750 cand = tourney (candidates);
2754 if (flags & LOOKUP_COMPLAIN)
2756 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
2757 print_z_candidates (candidates);
2759 return error_mark_node;
2762 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
2764 extern int warn_synth;
2766 && fnname == ansi_opname[MODIFY_EXPR]
2767 && DECL_ARTIFICIAL (cand->fn)
2769 && ! candidates->next->next)
2771 cp_warning ("using synthesized `%#D' for copy assignment",
2773 cp_warning_at (" where cfront would use `%#D'",
2775 ? candidates->next->fn
2779 /* Pedantically, normal function declarations are never considered
2780 to refer to template instantiations, so we only do this with
2782 if (flag_guiding_decls && templates && ! cand->template
2783 && ! DECL_INITIAL (cand->fn)
2784 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2785 add_maybe_template (cand->fn, templates);
2787 return build_over_call
2789 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
2790 ? mem_arglist : arglist,
2794 /* Check for comparison of different enum types. */
2803 if (flag_int_enum_equivalence == 0
2804 && TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
2805 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
2806 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
2807 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
2809 cp_warning ("comparison between `%#T' and `%#T'",
2810 TREE_TYPE (arg1), TREE_TYPE (arg2));
2817 /* We need to strip any leading REF_BIND so that bitfields don't cause
2818 errors. This should not remove any important conversions, because
2819 builtins don't apply to class objects directly. */
2820 conv = TREE_VEC_ELT (cand->convs, 0);
2821 if (TREE_CODE (conv) == REF_BIND)
2822 conv = TREE_OPERAND (conv, 0);
2823 arg1 = convert_like (conv, arg1);
2825 arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
2827 arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
2833 return build_modify_expr (arg1, code2, arg2);
2836 return build_indirect_ref (arg1, "unary *");
2841 case TRUNC_DIV_EXPR:
2852 case TRUNC_MOD_EXPR:
2856 case TRUTH_ANDIF_EXPR:
2857 case TRUTH_ORIF_EXPR:
2858 return build_binary_op_nodefault (code, arg1, arg2, code);
2863 case TRUTH_NOT_EXPR:
2864 case PREINCREMENT_EXPR:
2865 case POSTINCREMENT_EXPR:
2866 case PREDECREMENT_EXPR:
2867 case POSTDECREMENT_EXPR:
2870 return build_unary_op (code, arg1, candidates != 0);
2873 return build_array_ref (arg1, arg2);
2876 return build_conditional_expr (arg1, arg2, arg3);
2879 return build_m_component_ref
2880 (build_indirect_ref (arg1, NULL_PTR), arg2);
2882 /* The caller will deal with these. */
2889 my_friendly_abort (367);
2894 /* Build up a call to operator new. This has to be handled differently
2895 from other operators in the way lookup is handled; first members are
2896 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
2897 TYPE is the type to be created. ARGS are any new-placement args.
2898 FLAGS are the usual overloading flags. */
2901 build_op_new_call (code, type, args, flags)
2902 enum tree_code code;
2906 tree fnname = ansi_opname[code];
2908 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
2909 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
2911 tree dummy = build1 (NOP_EXPR, build_pointer_type (type),
2913 dummy = build_indirect_ref (dummy, "new");
2914 return build_method_call (dummy, fnname, args, NULL_TREE, flags);
2917 return build_new_function_call
2918 (lookup_function_nonclass (fnname, args), args);
2921 /* Build a call to operator delete. This has to be handled very specially,
2922 because the restrictions on what signatures match are different from all
2923 other call instances. For a normal delete, only a delete taking (void *)
2924 or (void *, size_t) is accepted. For a placement delete, only an exact
2925 match with the placement new is accepted.
2927 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
2928 ADDR is the pointer to be deleted. For placement delete, it is also
2929 used to determine what the corresponding new looked like.
2930 SIZE is the size of the memory block to be deleted.
2931 FLAGS are the usual overloading flags. */
2934 build_op_delete_call (code, addr, size, flags)
2935 enum tree_code code;
2939 tree fn, fns, fnname, fntype, argtypes, args, type;
2942 if (addr == error_mark_node)
2943 return error_mark_node;
2945 type = TREE_TYPE (TREE_TYPE (addr));
2946 fnname = ansi_opname[code];
2948 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
2951 If the result of the lookup is ambiguous or inaccessible, or if
2952 the lookup selects a placement deallocation function, the
2953 program is ill-formed.
2955 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
2957 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
2958 if (fns == error_mark_node)
2959 return error_mark_node;
2967 /* It is unnecessary to wrap another TREE_LIST around it. (MvL) */
2968 /* Build this up like build_offset_ref does. */
2969 fns = build_tree_list (error_mark_node, fns);
2970 TREE_TYPE (fns) = build_offset_type (type, unknown_type_node);
2974 fns = lookup_name_nonclass (fnname);
2976 /* We can recognize a placement delete because of LOOKUP_SPECULATIVELY;
2977 if we are doing placement delete we do nothing if we don't find a
2978 matching op delete. */
2979 placement = !!(flags & LOOKUP_SPECULATIVELY);
2982 /* If placement, we are coming from build_new, and we know that addr
2983 is the allocation expression, so extract the info we need from it.
2984 Obviously, if the build_new process changes this may have to
2988 tree t = TREE_OPERAND (addr, 1);
2989 /* The CALL_EXPR. */
2990 t = TREE_OPERAND (t, 0);
2992 argtypes = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2993 /* The second parm type. */
2994 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
2995 /* The second argument. */
2996 args = TREE_CHAIN (TREE_OPERAND (t, 1));
2998 /* Pull the dummy var out of the TARGET_EXPR for use in our call. */
2999 addr = TREE_OPERAND (addr, 0);
3003 /* First try it without the size argument. */
3004 argtypes = void_list_node;
3008 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3009 fntype = build_function_type (void_type_node, argtypes);
3011 /* Strip const and volatile from addr. */
3012 if (type != TYPE_MAIN_VARIANT (type))
3013 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
3015 /* instantiate_type will always return a plain function; pretend it's
3017 if (TREE_CODE (fns) == FUNCTION_DECL)
3018 fns = scratch_ovl_cons (fns, NULL_TREE);
3020 fn = instantiate_type (fntype, fns, 0);
3022 if (fn != error_mark_node)
3024 if (TREE_CODE (fns) == TREE_LIST)
3025 /* Member functions. */
3026 enforce_access (TREE_PURPOSE (fns), fn);
3027 return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
3033 /* Normal delete; now try to find a match including the size argument. */
3034 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3035 tree_cons (NULL_TREE, sizetype, void_list_node));
3036 fntype = build_function_type (void_type_node, argtypes);
3038 fn = instantiate_type (fntype, fns, 0);
3040 if (fn != error_mark_node)
3041 return build_function_call
3042 (fn, expr_tree_cons (NULL_TREE, addr,
3043 build_expr_list (NULL_TREE, size)));
3045 cp_error ("no suitable operator delete for `%T'", type);
3046 return error_mark_node;
3049 /* If the current scope isn't allowed to access DECL along
3050 BASETYPE_PATH, give an error. */
3053 enforce_access (basetype_path, decl)
3054 tree basetype_path, decl;
3056 tree access = compute_access (basetype_path, decl);
3058 if (access == access_private_node)
3060 cp_error_at ("`%+#D' is %s", decl,
3061 TREE_PRIVATE (decl) ? "private"
3062 : "from private base class");
3063 error ("within this context");
3065 else if (access == access_protected_node)
3067 cp_error_at ("`%+#D' %s", decl,
3068 TREE_PROTECTED (decl) ? "is protected"
3069 : "has protected accessibility");
3070 error ("within this context");
3074 /* Perform the conversions in CONVS on the expression EXPR. */
3077 convert_like (convs, expr)
3080 if (ICS_BAD_FLAG (convs)
3081 && TREE_CODE (convs) != USER_CONV
3082 && TREE_CODE (convs) != AMBIG_CONV)
3085 for (; t; t = TREE_OPERAND (t, 0))
3087 if (TREE_CODE (t) == USER_CONV)
3089 expr = convert_like (t, expr);
3092 else if (TREE_CODE (t) == AMBIG_CONV)
3093 return convert_like (t, expr);
3094 else if (TREE_CODE (t) == IDENTITY_CONV)
3097 return convert_for_initialization
3098 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3099 "conversion", NULL_TREE, 0);
3102 switch (TREE_CODE (convs))
3106 struct z_candidate *cand
3107 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3111 if (DECL_CONSTRUCTOR_P (fn))
3113 tree t = build_int_2 (0, 0);
3114 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3116 args = build_scratch_list (NULL_TREE, expr);
3117 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3118 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3119 args = scratch_tree_cons (NULL_TREE, t, args);
3122 args = build_this (expr);
3123 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3125 /* If this is a constructor or a function returning an aggr type,
3126 we need to build up a TARGET_EXPR. */
3127 if (DECL_CONSTRUCTOR_P (fn))
3128 expr = build_cplus_new (TREE_TYPE (convs), expr);
3133 if (type_unknown_p (expr))
3134 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3135 if (TREE_READONLY_DECL_P (expr))
3136 expr = decl_constant_value (expr);
3139 /* Call build_user_type_conversion again for the error. */
3140 return build_user_type_conversion
3141 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3147 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3148 if (expr == error_mark_node)
3149 return error_mark_node;
3151 switch (TREE_CODE (convs))
3154 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3156 /* else fall through */
3158 return build_user_type_conversion
3159 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
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);
3176 convert_default_arg (type, arg)
3179 arg = break_out_target_exprs (arg);
3181 if (TREE_CODE (arg) == CONSTRUCTOR)
3183 arg = digest_init (type, arg, 0);
3184 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3185 "default argument", 0, 0);
3189 /* This could get clobbered by the following call. */
3190 if (TREE_HAS_CONSTRUCTOR (arg))
3191 arg = copy_node (arg);
3193 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3194 "default argument", 0, 0);
3195 #ifdef PROMOTE_PROTOTYPES
3196 if ((TREE_CODE (type) == INTEGER_TYPE
3197 || TREE_CODE (type) == ENUMERAL_TYPE)
3198 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3199 arg = default_conversion (arg);
3207 build_over_call (cand, args, flags)
3208 struct z_candidate *cand;
3213 tree convs = cand->convs;
3214 tree converted_args = NULL_TREE;
3215 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3216 tree conv, arg, val;
3220 /* Give any warnings we noticed during overload resolution. */
3222 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3223 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3225 if (DECL_FUNCTION_MEMBER_P (fn))
3226 enforce_access (cand->basetype_path, fn);
3228 if (args && TREE_CODE (args) != TREE_LIST)
3229 args = build_scratch_list (NULL_TREE, args);
3232 /* The implicit parameters to a constructor are not considered by overload
3233 resolution, and must be of the proper type. */
3234 if (DECL_CONSTRUCTOR_P (fn))
3236 converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3237 arg = TREE_CHAIN (arg);
3238 parm = TREE_CHAIN (parm);
3239 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3241 converted_args = expr_tree_cons
3242 (NULL_TREE, TREE_VALUE (arg), converted_args);
3243 arg = TREE_CHAIN (arg);
3244 parm = TREE_CHAIN (parm);
3247 /* Bypass access control for 'this' parameter. */
3248 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3250 tree parmtype = TREE_VALUE (parm);
3251 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3252 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3254 int dv = (TYPE_VOLATILE (TREE_TYPE (parmtype))
3255 < TYPE_VOLATILE (TREE_TYPE (argtype)));
3256 int dc = (TYPE_READONLY (TREE_TYPE (parmtype))
3257 < TYPE_READONLY (TREE_TYPE (argtype)));
3258 char *p = (dv && dc ? "const and volatile"
3259 : dc ? "const" : dv ? "volatile" : "");
3261 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards %s",
3262 TREE_TYPE (argtype), fn, p);
3264 converted_args = expr_tree_cons
3265 (NULL_TREE, convert_force (TREE_VALUE (parm), TREE_VALUE (arg), CONV_C_CAST),
3267 parm = TREE_CHAIN (parm);
3268 arg = TREE_CHAIN (arg);
3274 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3276 tree type = TREE_VALUE (parm);
3278 conv = TREE_VEC_ELT (convs, i);
3279 if (ICS_BAD_FLAG (conv))
3282 val = TREE_VALUE (arg);
3284 for (; t; t = TREE_OPERAND (t, 0))
3286 if (TREE_CODE (t) == USER_CONV
3287 || TREE_CODE (t) == AMBIG_CONV)
3289 val = convert_like (t, val);
3292 else if (TREE_CODE (t) == IDENTITY_CONV)
3295 val = convert_for_initialization
3296 (NULL_TREE, type, val, LOOKUP_NORMAL,
3297 "argument passing", fn, i - is_method);
3300 val = convert_like (conv, TREE_VALUE (arg));
3302 #ifdef PROMOTE_PROTOTYPES
3303 if ((TREE_CODE (type) == INTEGER_TYPE
3304 || TREE_CODE (type) == ENUMERAL_TYPE)
3305 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3306 val = default_conversion (val);
3308 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3311 /* Default arguments */
3312 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3314 tree arg = TREE_PURPOSE (parm);
3316 if (DECL_TEMPLATE_INFO (fn))
3318 /* This came from a template. Instantiate the default arg here,
3319 not in tsubst. In the case of something like:
3327 we must be careful to do name lookup in the scope of
3328 S<T>, rather than in the current class. */
3329 if (DECL_CLASS_SCOPE_P (fn))
3330 pushclass (DECL_REAL_CONTEXT (fn), 2);
3332 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), NULL_TREE);
3334 if (DECL_CLASS_SCOPE_P (fn))
3337 converted_args = expr_tree_cons
3338 (NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg),
3343 for (; arg; arg = TREE_CHAIN (arg))
3345 val = TREE_VALUE (arg);
3347 if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
3348 && (TYPE_PRECISION (TREE_TYPE (val))
3349 < TYPE_PRECISION (double_type_node)))
3350 /* Convert `float' to `double'. */
3351 val = cp_convert (double_type_node, val);
3352 else if (IS_AGGR_TYPE (TREE_TYPE (val))
3353 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val)))
3354 cp_warning ("cannot pass objects of type `%T' through `...'",
3357 /* Convert `short' and `char' to full-size `int'. */
3358 val = default_conversion (val);
3360 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3363 converted_args = nreverse (converted_args);
3365 /* Avoid actually calling copy constructors and copy assignment operators,
3367 if (DECL_CONSTRUCTOR_P (fn)
3368 && TREE_VEC_LENGTH (convs) == 1
3369 && copy_args_p (fn))
3372 arg = TREE_CHAIN (converted_args);
3373 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3374 arg = TREE_CHAIN (arg);
3375 arg = TREE_VALUE (arg);
3377 /* Pull out the real argument, disregarding const-correctness. */
3379 while (TREE_CODE (targ) == NOP_EXPR
3380 || TREE_CODE (targ) == NON_LVALUE_EXPR
3381 || TREE_CODE (targ) == CONVERT_EXPR)
3382 targ = TREE_OPERAND (targ, 0);
3383 if (TREE_CODE (targ) == ADDR_EXPR)
3385 targ = TREE_OPERAND (targ, 0);
3386 if (! comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3387 TYPE_MAIN_VARIANT (TREE_TYPE (targ)), 1))
3396 arg = build_indirect_ref (arg, 0);
3398 /* [class.copy]: the copy constructor is implicitly defined even if
3399 the implementation elided its use. */
3400 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3403 /* If we're creating a temp and we already have one, don't create a
3404 new one. If we're not creating a temp but we get one, use
3405 INIT_EXPR to collapse the temp into our target. Otherwise, if the
3406 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3407 temp or an INIT_EXPR otherwise. */
3408 if (integer_zerop (TREE_VALUE (args)))
3410 if (! real_lvalue_p (arg))
3412 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3414 val = build (VAR_DECL, DECL_CONTEXT (fn));
3415 layout_decl (val, 0);
3416 val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
3417 TREE_SIDE_EFFECTS (val) = 1;
3421 else if (! real_lvalue_p (arg)
3422 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3424 tree to = stabilize_reference
3425 (build_indirect_ref (TREE_VALUE (args), 0));
3427 /* Don't copy the padding byte; it might not have been allocated
3428 if to is a base subobject. */
3429 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3430 return build_unary_op
3431 (ADDR_EXPR, build (COMPOUND_EXPR, TREE_TYPE (to),
3432 cp_convert (void_type_node, arg), to),
3435 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
3436 TREE_SIDE_EFFECTS (val) = 1;
3437 return build_unary_op (ADDR_EXPR, val, 0);
3440 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
3442 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
3444 tree to = stabilize_reference
3445 (build_indirect_ref (TREE_VALUE (converted_args), 0));
3447 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
3449 /* Don't copy the padding byte; it might not have been allocated
3450 if to is a base subobject. */
3451 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3452 return build (COMPOUND_EXPR, TREE_TYPE (to),
3453 cp_convert (void_type_node, arg), to);
3455 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
3456 TREE_SIDE_EFFECTS (val) = 1;
3462 if (DECL_CLASS_SCOPE_P (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
3463 return build_signature_method_call (fn, converted_args);
3464 else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3466 tree t, *p = &TREE_VALUE (converted_args);
3467 tree binfo = get_binfo
3468 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
3469 *p = convert_pointer_to_real (binfo, *p);
3470 if (TREE_SIDE_EFFECTS (*p))
3471 *p = save_expr (*p);
3472 t = build_pointer_type (TREE_TYPE (fn));
3473 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
3476 else if (DECL_INLINE (fn))
3477 fn = inline_conversion (fn);
3479 fn = build_addr_func (fn);
3481 /* Recognize certain built-in functions so we can make tree-codes
3482 other than CALL_EXPR. We do this when it enables fold-const.c
3483 to do something useful. */
3485 if (TREE_CODE (fn) == ADDR_EXPR
3486 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3487 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
3488 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
3493 if (converted_args == 0)
3494 return integer_zero_node;
3495 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
3500 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3501 if (TREE_TYPE (fn) == void_type_node)
3503 fn = require_complete_type (fn);
3504 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
3505 fn = build_cplus_new (TREE_TYPE (fn), fn);
3506 return convert_from_reference (fn);
3510 build_new_method_call (instance, name, args, basetype_path, flags)
3511 tree instance, name, args, basetype_path;
3514 struct z_candidate *candidates = 0, *cand;
3515 tree explicit_targs = NULL_TREE;
3516 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
3518 tree user_args = args;
3519 tree templates = NULL_TREE;
3520 int template_only = 0;
3522 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3524 explicit_targs = TREE_OPERAND (name, 1);
3525 name = TREE_OPERAND (name, 0);
3526 if (TREE_CODE (name) == TEMPLATE_DECL)
3527 name = DECL_NAME (name);
3531 /* If there is an extra argument for controlling virtual bases,
3532 remove it for error reporting. */
3533 if (flags & LOOKUP_HAS_IN_CHARGE)
3534 user_args = TREE_CHAIN (args);
3536 args = resolve_args (args);
3538 if (args == error_mark_node)
3539 return error_mark_node;
3541 if (instance == NULL_TREE)
3542 basetype = BINFO_TYPE (basetype_path);
3545 if (TREE_CODE (instance) == OFFSET_REF)
3546 instance = resolve_offset_ref (instance);
3547 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3548 instance = convert_from_reference (instance);
3549 basetype = TREE_TYPE (instance);
3551 /* XXX this should be handled before we get here. */
3552 if (! IS_AGGR_TYPE (basetype)
3553 && ! (TYPE_LANG_SPECIFIC (basetype)
3554 && (IS_SIGNATURE_POINTER (basetype)
3555 || IS_SIGNATURE_REFERENCE (basetype))))
3557 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
3558 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
3559 name, instance, basetype);
3561 return error_mark_node;
3564 /* If `instance' is a signature pointer/reference and `name' is
3565 not a constructor, we are calling a signature member function.
3566 In that case set the `basetype' to the signature type. */
3567 if ((IS_SIGNATURE_POINTER (basetype)
3568 || IS_SIGNATURE_REFERENCE (basetype))
3569 && TYPE_IDENTIFIER (basetype) != name)
3570 basetype = SIGNATURE_TYPE (basetype);
3573 if (basetype_path == NULL_TREE)
3574 basetype_path = TYPE_BINFO (basetype);
3578 instance_ptr = build_this (instance);
3580 if (! template_only)
3582 /* XXX this should be handled before we get here. */
3583 fns = build_field_call (basetype_path, instance_ptr, name, args);
3590 instance_ptr = build_int_2 (0, 0);
3591 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
3595 = (name == ctor_identifier ? constructor_name (basetype) : name);
3597 fns = lookup_fnfields (basetype_path, name, 1);
3599 if (fns == error_mark_node)
3600 return error_mark_node;
3603 tree fn = TREE_VALUE (fns);
3604 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3605 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3607 flags |= LOOKUP_HAS_IN_CHARGE;
3608 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3610 mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
3611 for (; fn; fn = OVL_NEXT (fn))
3613 tree t = OVL_CURRENT (fn);
3616 /* We can end up here for copy-init of same or base class. */
3617 if (name == ctor_identifier
3618 && (flags & LOOKUP_ONLYCONVERTING)
3619 && DECL_NONCONVERTING_P (t))
3621 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
3622 this_arglist = mem_args;
3624 this_arglist = args;
3626 if (TREE_CODE (t) == TEMPLATE_DECL)
3628 /* A member template. */
3629 templates = scratch_tree_cons (NULL_TREE, t, templates);
3631 add_template_candidate (candidates, t, explicit_targs,
3633 TREE_TYPE (name), flags, DEDUCE_CALL);
3635 else if (! template_only)
3636 candidates = add_function_candidate (candidates, t,
3637 this_arglist, flags);
3640 candidates->basetype_path = TREE_PURPOSE (fns);
3644 if (! any_viable (candidates))
3646 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
3647 if (flags & LOOKUP_SPECULATIVELY)
3649 cp_error ("no matching function for call to `%T::%D (%A)%V'", basetype,
3650 pretty_name, user_args, TREE_TYPE (TREE_TYPE (instance_ptr)));
3651 print_z_candidates (candidates);
3652 return error_mark_node;
3654 candidates = splice_viable (candidates);
3655 cand = tourney (candidates);
3659 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
3661 print_z_candidates (candidates);
3662 return error_mark_node;
3665 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
3666 && instance == current_class_ref
3667 && DECL_CONSTRUCTOR_P (current_function_decl)
3668 && ! (flags & LOOKUP_NONVIRTUAL)
3669 && value_member (cand->fn, get_abstract_virtuals (basetype)))
3670 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
3671 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3672 && TREE_CODE (instance_ptr) == NOP_EXPR
3673 && TREE_OPERAND (instance_ptr, 0) == error_mark_node)
3674 cp_error ("cannot call member function `%D' without object", cand->fn);
3676 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
3677 && ((instance == current_class_ref && (dtor_label || ctor_label))
3678 || resolves_to_fixed_type_p (instance, 0)))
3679 flags |= LOOKUP_NONVIRTUAL;
3681 /* Pedantically, normal function declarations are never considered
3682 to refer to template instantiations, so we only do this with
3684 if (flag_guiding_decls && templates && ! cand->template
3685 && ! DECL_INITIAL (cand->fn))
3686 add_maybe_template (cand->fn, templates);
3688 return build_over_call
3690 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
3694 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
3695 subsequence of ICS2. */
3698 is_subseq (ics1, ics2)
3701 /* We can assume that a conversion of the same code
3702 between the same types indicates a subsequence since we only get
3703 here if the types we are converting from are the same. */
3705 while (TREE_CODE (ics1) == RVALUE_CONV
3706 || TREE_CODE (ics1) == LVALUE_CONV)
3707 ics1 = TREE_OPERAND (ics1, 0);
3711 while (TREE_CODE (ics2) == RVALUE_CONV
3712 || TREE_CODE (ics2) == LVALUE_CONV)
3713 ics2 = TREE_OPERAND (ics2, 0);
3715 if (TREE_CODE (ics2) == USER_CONV
3716 || TREE_CODE (ics2) == AMBIG_CONV
3717 || TREE_CODE (ics2) == IDENTITY_CONV)
3718 /* At this point, ICS1 cannot be a proper subsequence of
3719 ICS2. We can get a USER_CONV when we are comparing the
3720 second standard conversion sequence of two user conversion
3724 ics2 = TREE_OPERAND (ics2, 0);
3726 if (TREE_CODE (ics2) == TREE_CODE (ics1)
3727 && comptypes (TREE_TYPE (ics2), TREE_TYPE (ics1), 1)
3728 && comptypes (TREE_TYPE (TREE_OPERAND (ics2, 0)),
3729 TREE_TYPE (TREE_OPERAND (ics1, 0)), 1))
3734 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
3735 be any _TYPE nodes. */
3738 is_properly_derived_from (derived, base)
3742 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
3743 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
3746 /* We only allow proper derivation here. The DERIVED_FROM_P macro
3747 considers every class derived from itself. */
3748 return (!comptypes (TYPE_MAIN_VARIANT (derived),
3749 TYPE_MAIN_VARIANT (base), 1)
3750 && DERIVED_FROM_P (base, derived));
3753 /* We build the ICS for an implicit object parameter as a pointer
3754 conversion sequence. However, such a sequence should be compared
3755 as if it were a reference conversion sequence. If ICS is the
3756 implicit conversion sequence for an implicit object parameter,
3757 modify it accordingly. */
3760 maybe_handle_implicit_object (ics)
3763 if (ICS_THIS_FLAG (*ics))
3765 /* [over.match.funcs]
3767 For non-static member functions, the type of the
3768 implicit object parameter is "reference to cv X"
3769 where X is the class of which the function is a
3770 member and cv is the cv-qualification on the member
3771 function declaration. */
3773 if (TREE_CODE (t) == QUAL_CONV)
3774 t = TREE_OPERAND (t, 0);
3775 if (TREE_CODE (t) == PTR_CONV)
3776 t = TREE_OPERAND (t, 0);
3777 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3778 t = build_conv (REF_BIND,
3779 build_reference_type (TREE_TYPE (TREE_TYPE (*ics))),
3781 ICS_STD_RANK (t) = ICS_STD_RANK (*ics);
3786 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
3787 to the type the reference originally referred to, and return 1.
3788 Otherwise, return 0. */
3791 maybe_handle_ref_bind (ics, target_type)
3795 if (TREE_CODE (*ics) == REF_BIND)
3799 When a parameter of reference type binds directly
3800 (_dcl.init.ref_) to an argument expression, the implicit
3801 conversion sequence is the identity conversion, unless the
3802 argument expression has a type that is a derived class of the
3803 parameter type, in which case the implicit conversion
3804 sequence is a derived-to-base Conversion.
3806 If the parameter binds directly to the result of applying a
3807 conversion function to the argument expression, the implicit
3808 conversion sequence is a user-defined conversion sequence
3809 (_over.ics.user_), with the second standard conversion
3810 sequence either an identity conversion or, if the conversion
3811 function returns an entity of a type that is a derived class
3812 of the parameter type, a derived-to-base Conversion.
3814 When a parameter of reference type is not bound directly to
3815 an argument expression, the conversion sequence is the one
3816 required to convert the argument expression to the underlying
3817 type of the reference according to _over.best.ics_.
3818 Conceptually, this conversion sequence corresponds to
3819 copy-initializing a temporary of the underlying type with the
3820 argument expression. Any difference in top-level
3821 cv-qualification is subsumed by the initialization itself and
3822 does not constitute a conversion. */
3824 tree old_ics = *ics;
3826 *target_type = TREE_TYPE (TREE_TYPE (*ics));
3827 *ics = TREE_OPERAND (*ics, 0);
3828 if (TREE_CODE (*ics) == IDENTITY_CONV
3829 && is_properly_derived_from (TREE_TYPE (*ics), *target_type))
3830 *ics = build_conv (BASE_CONV, *target_type, *ics);
3831 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
3832 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
3840 /* Compare two implicit conversion sequences according to the rules set out in
3841 [over.ics.rank]. Return values:
3843 1: ics1 is better than ics2
3844 -1: ics2 is better than ics1
3845 0: ics1 and ics2 are indistinguishable */
3848 compare_ics (ics1, ics2)
3855 tree deref_from_type1 = NULL_TREE;
3856 tree deref_from_type2;
3857 tree deref_to_type1;
3858 tree deref_to_type2;
3860 /* REF_BINDING is non-zero if the result of the conversion sequence
3861 is a reference type. In that case TARGET_TYPE is the
3862 type referred to by the reference. */
3868 /* Handle implicit object parameters. */
3869 maybe_handle_implicit_object (&ics1);
3870 maybe_handle_implicit_object (&ics2);
3872 /* Handle reference parameters. */
3873 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
3874 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
3878 When comparing the basic forms of implicit conversion sequences (as
3879 defined in _over.best.ics_)
3881 --a standard conversion sequence (_over.ics.scs_) is a better
3882 conversion sequence than a user-defined conversion sequence
3883 or an ellipsis conversion sequence, and
3885 --a user-defined conversion sequence (_over.ics.user_) is a
3886 better conversion sequence than an ellipsis conversion sequence
3887 (_over.ics.ellipsis_). */
3888 if (ICS_RANK (ics1) > ICS_RANK (ics2))
3890 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
3893 if (ICS_RANK (ics1) == BAD_RANK)
3895 /* Both ICS are bad. We try to make a decision based on what
3896 would have happenned if they'd been good. */
3897 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
3898 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3900 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
3901 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3904 /* We couldn't make up our minds; try to figure it out below. */
3907 if (ICS_ELLIPSIS_FLAG (ics1))
3908 /* Both conversions are ellipsis conversions. */
3911 /* User-defined conversion sequence U1 is a better conversion sequence
3912 than another user-defined conversion sequence U2 if they contain the
3913 same user-defined conversion operator or constructor and if the sec-
3914 ond standard conversion sequence of U1 is better than the second
3915 standard conversion sequence of U2. */
3917 if (ICS_USER_FLAG (ics1))
3921 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
3922 if (TREE_CODE (t1) == AMBIG_CONV)
3924 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
3925 if (TREE_CODE (t2) == AMBIG_CONV)
3928 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
3931 /* We can just fall through here, after setting up
3932 FROM_TYPE1 and FROM_TYPE2. */
3933 from_type1 = TREE_TYPE (t1);
3934 from_type2 = TREE_TYPE (t2);
3938 /* We're dealing with two standard conversion sequences.
3942 Standard conversion sequence S1 is a better conversion
3943 sequence than standard conversion sequence S2 if
3945 --S1 is a proper subsequence of S2 (comparing the conversion
3946 sequences in the canonical form defined by _over.ics.scs_,
3947 excluding any Lvalue Transformation; the identity
3948 conversion sequence is considered to be a subsequence of
3949 any non-identity conversion sequence */
3952 while (TREE_CODE (from_type1) != IDENTITY_CONV)
3953 from_type1 = TREE_OPERAND (from_type1, 0);
3954 from_type1 = TREE_TYPE (from_type1);
3957 while (TREE_CODE (from_type2) != IDENTITY_CONV)
3958 from_type2 = TREE_OPERAND (from_type2, 0);
3959 from_type2 = TREE_TYPE (from_type2);
3962 if (comptypes (from_type1, from_type2, 1))
3964 if (is_subseq (ics1, ics2))
3966 if (is_subseq (ics2, ics1))
3969 /* Otherwise, one sequence cannot be a subsequence of the other; they
3970 don't start with the same type. This can happen when comparing the
3971 second standard conversion sequence in two user-defined conversion
3978 --the rank of S1 is better than the rank of S2 (by the rules
3981 Standard conversion sequences are ordered by their ranks: an Exact
3982 Match is a better conversion than a Promotion, which is a better
3983 conversion than a Conversion.
3985 Two conversion sequences with the same rank are indistinguishable
3986 unless one of the following rules applies:
3988 --A conversion that is not a conversion of a pointer, or pointer
3989 to member, to bool is better than another conversion that is such
3992 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
3993 so that we do not have to check it explicitly. */
3994 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3996 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
3999 to_type1 = TREE_TYPE (ics1);
4000 to_type2 = TREE_TYPE (ics2);
4002 if (TYPE_PTR_P (from_type1)
4003 && TYPE_PTR_P (from_type2)
4004 && TYPE_PTR_P (to_type1)
4005 && TYPE_PTR_P (to_type2))
4007 deref_from_type1 = TREE_TYPE (from_type1);
4008 deref_from_type2 = TREE_TYPE (from_type2);
4009 deref_to_type1 = TREE_TYPE (to_type1);
4010 deref_to_type2 = TREE_TYPE (to_type2);
4012 /* The rules for pointers to members A::* are just like the rules
4013 for pointers A*, except opposite: if B is derived from A then
4014 A::* converts to B::*, not vice versa. For that reason, we
4015 switch the from_ and to_ variables here. */
4016 else if (TYPE_PTRMEM_P (from_type1)
4017 && TYPE_PTRMEM_P (from_type2)
4018 && TYPE_PTRMEM_P (to_type1)
4019 && TYPE_PTRMEM_P (to_type2))
4021 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4022 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4023 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4024 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4026 else if (TYPE_PTRMEMFUNC_P (from_type1)
4027 && TYPE_PTRMEMFUNC_P (from_type2)
4028 && TYPE_PTRMEMFUNC_P (to_type1)
4029 && TYPE_PTRMEMFUNC_P (to_type2))
4031 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4032 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4033 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4034 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4037 if (deref_from_type1 != NULL_TREE
4038 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4039 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4041 /* This was one of the pointer or pointer-like conversions.
4045 --If class B is derived directly or indirectly from class A,
4046 conversion of B* to A* is better than conversion of B* to
4047 void*, and conversion of A* to void* is better than
4048 conversion of B* to void*. */
4049 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4050 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4052 if (is_properly_derived_from (deref_from_type1,
4055 else if (is_properly_derived_from (deref_from_type2,
4059 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4060 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4062 if (comptypes (deref_from_type1, deref_from_type2, 1))
4064 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4066 if (is_properly_derived_from (deref_from_type1,
4070 /* We know that DEREF_TO_TYPE1 is `void' here. */
4071 else if (is_properly_derived_from (deref_from_type1,
4076 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4077 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4081 --If class B is derived directly or indirectly from class A
4082 and class C is derived directly or indirectly from B,
4084 --conversion of C* to B* is better than conversion of C* to
4087 --conversion of B* to A* is better than conversion of C* to
4089 if (comptypes (deref_from_type1, deref_from_type2, 1))
4091 if (is_properly_derived_from (deref_to_type1,
4094 else if (is_properly_derived_from (deref_to_type2,
4098 else if (comptypes (deref_to_type1, deref_to_type2, 1))
4100 if (is_properly_derived_from (deref_from_type2,
4103 else if (is_properly_derived_from (deref_from_type1,
4109 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4110 && comptypes (from_type1, from_type2, 1))
4114 --binding of an expression of type C to a reference of type
4115 B& is better than binding an expression of type C to a
4116 reference of type A&
4118 --conversion of C to B is better than conversion of C to A, */
4119 if (is_properly_derived_from (from_type1, to_type1)
4120 && is_properly_derived_from (from_type1, to_type2))
4122 if (is_properly_derived_from (to_type1, to_type2))
4124 else if (is_properly_derived_from (to_type2, to_type1))
4128 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4129 && comptypes (to_type1, to_type2, 1))
4133 --binding of an expression of type B to a reference of type
4134 A& is better than binding an expression of type C to a
4135 reference of type A&,
4137 --onversion of B to A is better than conversion of C to A */
4138 if (is_properly_derived_from (from_type1, to_type1)
4139 && is_properly_derived_from (from_type2, to_type1))
4141 if (is_properly_derived_from (from_type2, from_type1))
4143 else if (is_properly_derived_from (from_type1, from_type2))
4150 --S1 and S2 differ only in their qualification conversion and yield
4151 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4152 qualification signature of type T1 is a proper subset of the cv-
4153 qualification signature of type T2 */
4154 if (TREE_CODE (ics1) == QUAL_CONV
4155 && TREE_CODE (ics2) == QUAL_CONV
4156 && comptypes (from_type1, from_type2, 1))
4157 return comp_cv_qual_signature (to_type1, to_type2);
4161 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4162 types to which the references refer are the same type except for
4163 top-level cv-qualifiers, and the type to which the reference
4164 initialized by S2 refers is more cv-qualified than the type to
4165 which the reference initialized by S1 refers */
4167 if (ref_binding1 && ref_binding2
4168 && comptypes (TYPE_MAIN_VARIANT (to_type1),
4169 TYPE_MAIN_VARIANT (to_type2), 1))
4170 return comp_cv_qualification (target_type2, target_type1);
4172 /* Neither conversion sequence is better than the other. */
4176 /* The source type for this standard conversion sequence. */
4182 for (;; t = TREE_OPERAND (t, 0))
4184 if (TREE_CODE (t) == USER_CONV
4185 || TREE_CODE (t) == AMBIG_CONV
4186 || TREE_CODE (t) == IDENTITY_CONV)
4187 return TREE_TYPE (t);
4189 my_friendly_abort (1823);
4192 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4193 a pointer to LOSER and re-running joust to produce the warning if WINNER
4194 is actually used. */
4197 add_warning (winner, loser)
4198 struct z_candidate *winner, *loser;
4200 winner->warnings = expr_tree_cons (NULL_PTR,
4201 build_expr_ptr_wrapper (loser),
4205 /* Compare two candidates for overloading as described in
4206 [over.match.best]. Return values:
4208 1: cand1 is better than cand2
4209 -1: cand2 is better than cand1
4210 0: cand1 and cand2 are indistinguishable */
4213 joust (cand1, cand2, warn)
4214 struct z_candidate *cand1, *cand2;
4218 int i, off1 = 0, off2 = 0, len;
4220 /* Candidates that involve bad conversions are always worse than those
4222 if (cand1->viable > cand2->viable)
4224 if (cand1->viable < cand2->viable)
4227 /* a viable function F1
4228 is defined to be a better function than another viable function F2 if
4229 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4230 ICSi(F2), and then */
4232 /* for some argument j, ICSj(F1) is a better conversion sequence than
4235 /* For comparing static and non-static member functions, we ignore the
4236 implicit object parameter of the non-static function. The WP says to
4237 pretend that the static function has an object parm, but that won't
4238 work with operator overloading. */
4239 len = TREE_VEC_LENGTH (cand1->convs);
4240 if (len != TREE_VEC_LENGTH (cand2->convs))
4242 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4243 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4245 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4246 && DECL_STATIC_FUNCTION_P (cand2->fn))
4252 my_friendly_abort (42);
4255 for (i = 0; i < len; ++i)
4257 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4258 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4259 int comp = compare_ics (t1, t2);
4264 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4265 && TREE_CODE (t1) == STD_CONV
4266 && TREE_CODE (t2) == STD_CONV
4267 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4268 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4269 && (TYPE_PRECISION (TREE_TYPE (t1))
4270 == TYPE_PRECISION (TREE_TYPE (t2)))
4271 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4272 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4275 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4277 struct z_candidate *w, *l;
4279 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4280 w = cand1, l = cand2;
4282 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4283 w = cand2, l = cand1;
4287 cp_warning ("passing `%T' chooses `%T' over `%T'",
4288 type, type1, type2);
4289 cp_warning (" in call to `%D'", w->fn);
4295 if (winner && comp != winner)
4304 /* warn about confusing overload resolution */
4305 if (winner && cand1->second_conv)
4307 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4310 struct z_candidate *w, *l;
4312 w = cand1, l = cand2;
4314 w = cand2, l = cand1;
4317 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4318 cp_warning (" for conversion from `%T' to `%T'",
4319 TREE_TYPE (source_type (TREE_VEC_ELT (w->convs, 0))),
4320 TREE_TYPE (w->second_conv));
4321 cp_warning (" because conversion sequence for the argument is better");
4332 F1 is a non-template function and F2 is a template function */
4334 if (! cand1->template && cand2->template)
4336 else if (cand1->template && ! cand2->template)
4338 else if (cand1->template && cand2->template)
4339 winner = more_specialized
4340 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4344 the context is an initialization by user-defined conversion (see
4345 _dcl.init_ and _over.match.user_) and the standard conversion
4346 sequence from the return type of F1 to the destination type (i.e.,
4347 the type of the entity being initialized) is a better conversion
4348 sequence than the standard conversion sequence from the return type
4349 of F2 to the destination type. */
4351 if (! winner && cand1->second_conv)
4352 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4354 /* If the built-in candidates are the same, arbitrarily pick one. */
4355 if (! winner && cand1->fn == cand2->fn
4356 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4358 for (i = 0; i < len; ++i)
4359 if (! comptypes (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4360 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i)), 1))
4362 if (i == TREE_VEC_LENGTH (cand1->convs))
4365 /* Kludge around broken overloading rules whereby
4366 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4367 that takes references and another that takes values. */
4368 if (cand1->fn == ansi_opname[COND_EXPR])
4370 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4371 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4372 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4373 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4375 if (comptypes (t1, t2, 1))
4377 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4379 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4387 /* Extension: If the worst conversion for one candidate is worse than the
4388 worst conversion for the other, take the first. */
4389 if (! winner && ! pedantic)
4391 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4393 for (i = 0; i < len; ++i)
4395 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4396 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4397 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
4398 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
4410 /* Given a list of candidates for overloading, find the best one, if any.
4411 This algorithm has a worst case of O(2n) (winner is last), and a best
4412 case of O(n/2) (totally ambiguous); much better than a sorting
4415 static struct z_candidate *
4416 tourney (candidates)
4417 struct z_candidate *candidates;
4419 struct z_candidate *champ = candidates, *challenger;
4421 int champ_compared_to_predecessor = 0;
4423 /* Walk through the list once, comparing each current champ to the next
4424 candidate, knocking out a candidate or two with each comparison. */
4426 for (challenger = champ->next; challenger; )
4428 fate = joust (champ, challenger, 0);
4430 challenger = challenger->next;
4435 champ = challenger->next;
4438 champ_compared_to_predecessor = 0;
4443 champ_compared_to_predecessor = 1;
4446 challenger = champ->next;
4450 /* Make sure the champ is better than all the candidates it hasn't yet
4451 been compared to. */
4453 for (challenger = candidates;
4455 && !(champ_compared_to_predecessor && challenger->next == champ);
4456 challenger = challenger->next)
4458 fate = joust (champ, challenger, 0);
4467 can_convert (to, from)
4470 tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
4471 return (t && ! ICS_BAD_FLAG (t));
4475 can_convert_arg (to, from, arg)
4478 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
4479 return (t && ! ICS_BAD_FLAG (t));