1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 92-97, 1998 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
39 extern int inhibit_warnings;
40 extern tree ctor_label, dtor_label;
42 static tree build_new_method_call PROTO((tree, tree, tree, tree, int));
44 static tree build_field_call PROTO((tree, tree, tree, tree));
45 static tree find_scoped_type PROTO((tree, tree, tree));
46 static struct z_candidate * tourney PROTO((struct z_candidate *));
47 static int joust PROTO((struct z_candidate *, struct z_candidate *, int));
48 static int compare_ics PROTO((tree, tree));
49 static tree build_over_call PROTO((struct z_candidate *, tree, int));
50 static tree convert_like PROTO((tree, tree));
51 static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
53 static tree build_object_call PROTO((tree, tree));
54 static tree resolve_args PROTO((tree));
55 static struct z_candidate * build_user_type_conversion_1
56 PROTO ((tree, tree, int));
57 static void print_z_candidates PROTO((struct z_candidate *));
58 static tree build_this PROTO((tree));
59 static struct z_candidate * splice_viable PROTO((struct z_candidate *));
60 static int any_viable PROTO((struct z_candidate *));
61 static struct z_candidate * add_template_candidate
62 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
64 static struct z_candidate * add_template_candidate_real
65 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
66 tree, unification_kind_t));
67 static struct z_candidate * add_template_conv_candidate
68 PROTO((struct z_candidate *, tree, tree, tree, tree));
69 static struct z_candidate * add_builtin_candidates
70 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
72 static struct z_candidate * add_builtin_candidate
73 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
74 tree, tree, tree, tree *, tree *, int));
75 static int is_complete PROTO((tree));
76 static struct z_candidate * build_builtin_candidate
77 PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
79 static struct z_candidate * add_conv_candidate
80 PROTO((struct z_candidate *, tree, tree, tree));
81 static struct z_candidate * add_function_candidate
82 PROTO((struct z_candidate *, tree, tree, int));
83 static tree implicit_conversion PROTO((tree, tree, tree, int));
84 static tree standard_conversion PROTO((tree, tree, tree));
85 static tree reference_binding PROTO((tree, tree, tree, int));
86 static tree strip_top_quals PROTO((tree));
87 static tree non_reference PROTO((tree));
88 static tree build_conv PROTO((enum tree_code, tree, tree));
89 static int is_subseq PROTO((tree, tree));
90 static int is_properly_derived_from PROTO((tree, tree));
91 static int maybe_handle_ref_bind PROTO((tree*, tree*));
92 static void maybe_handle_implicit_object PROTO((tree*));
95 build_vfield_ref (datum, type)
99 int old_assume_nonnull_objects = flag_assume_nonnull_objects;
101 if (datum == error_mark_node)
102 return error_mark_node;
104 /* Vtable references are always made from non-null objects. */
105 flag_assume_nonnull_objects = 1;
106 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
107 datum = convert_from_reference (datum);
109 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
110 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
111 datum, CLASSTYPE_VFIELD (type));
113 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
114 flag_assume_nonnull_objects = old_assume_nonnull_objects;
119 /* Build a call to a member of an object. I.e., one that overloads
120 operator ()(), or is a pointer-to-function or pointer-to-method. */
123 build_field_call (basetype_path, instance_ptr, name, parms)
124 tree basetype_path, instance_ptr, name, parms;
126 tree field, instance;
128 if (name == ctor_identifier || name == dtor_identifier)
131 if (instance_ptr == current_class_ptr)
133 /* Check to see if we really have a reference to an instance variable
134 with `operator()()' overloaded. */
135 field = IDENTIFIER_CLASS_VALUE (name);
137 if (field == NULL_TREE)
139 cp_error ("`this' has no member named `%D'", name);
140 return error_mark_node;
143 if (TREE_CODE (field) == FIELD_DECL)
145 /* If it's a field, try overloading operator (),
146 or calling if the field is a pointer-to-function. */
147 instance = build_component_ref_1 (current_class_ref, field, 0);
148 if (instance == error_mark_node)
149 return error_mark_node;
151 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance)))
152 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
154 if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
156 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
157 return build_function_call (instance, parms);
158 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
159 return build_function_call (instance, expr_tree_cons (NULL_TREE, current_class_ptr, parms));
165 /* Check to see if this is not really a reference to an instance variable
166 with `operator()()' overloaded. */
167 field = lookup_field (basetype_path, name, 1, 0);
169 /* This can happen if the reference was ambiguous or for access
171 if (field == error_mark_node)
172 return error_mark_node;
174 if (field && TREE_CODE (field) == FIELD_DECL)
177 tree ftype = TREE_TYPE (field);
179 if (TREE_CODE (ftype) == REFERENCE_TYPE)
180 ftype = TREE_TYPE (ftype);
182 if (TYPE_LANG_SPECIFIC (ftype))
184 /* Make the next search for this field very short. */
185 basetype = DECL_FIELD_CONTEXT (field);
186 instance_ptr = convert_pointer_to (basetype, instance_ptr);
188 instance = build_indirect_ref (instance_ptr, NULL_PTR);
189 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
190 build_component_ref_1 (instance, field, 0),
193 if (TREE_CODE (ftype) == POINTER_TYPE)
195 if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
196 || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
198 /* This is a member which is a pointer to function. */
200 = build_component_ref_1 (build_indirect_ref (instance_ptr,
202 field, LOOKUP_COMPLAIN);
203 if (ref == error_mark_node)
204 return error_mark_node;
205 return build_function_call (ref, parms);
208 else if (TREE_CODE (ftype) == METHOD_TYPE)
210 error ("invalid call via pointer-to-member function");
211 return error_mark_node;
220 find_scoped_type (type, inner_name, inner_types)
221 tree type, inner_name, inner_types;
223 tree tags = CLASSTYPE_TAGS (type);
227 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
228 enclosing class) is set to the name for the enum type. So, if
229 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
230 then this test will be true. */
231 if (TREE_PURPOSE (tags) == inner_name)
233 if (inner_types == NULL_TREE)
234 return TYPE_MAIN_DECL (TREE_VALUE (tags));
235 return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
237 tags = TREE_CHAIN (tags);
240 /* Look for a TYPE_DECL. */
241 for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
242 if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
244 /* Code by raeburn. */
245 if (inner_types == NULL_TREE)
247 return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
253 /* Resolve an expression NAME1::NAME2::...::NAMEn to
254 the name that names the above nested type. INNER_TYPES
255 is a chain of nested type names (held together by SCOPE_REFs);
256 OUTER_TYPE is the type we know to enclose INNER_TYPES.
257 Returns NULL_TREE if there is an error. */
260 resolve_scope_to_name (outer_type, inner_stuff)
261 tree outer_type, inner_stuff;
264 tree inner_name, inner_type;
266 if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
268 /* We first try to look for a nesting in our current class context,
269 then try any enclosing classes. */
270 tree type = current_class_type;
272 while (type && (TREE_CODE (type) == RECORD_TYPE
273 || TREE_CODE (type) == UNION_TYPE))
275 tree rval = resolve_scope_to_name (type, inner_stuff);
277 if (rval != NULL_TREE)
279 type = DECL_CONTEXT (TYPE_MAIN_DECL (type));
283 if (TREE_CODE (inner_stuff) == SCOPE_REF)
285 inner_name = TREE_OPERAND (inner_stuff, 0);
286 inner_type = TREE_OPERAND (inner_stuff, 1);
290 inner_name = inner_stuff;
291 inner_type = NULL_TREE;
294 if (outer_type == NULL_TREE)
297 /* If we have something that's already a type by itself,
299 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
302 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
307 x = lookup_name (inner_name, 0);
309 if (x && TREE_CODE (x) == NAMESPACE_DECL)
311 x = lookup_namespace_name (x, inner_type);
317 if (! IS_AGGR_TYPE (outer_type))
320 /* Look for member classes or enums. */
321 tmp = find_scoped_type (outer_type, inner_name, inner_type);
323 /* If it's not a type in this class, then go down into the
324 base classes and search there. */
325 if (! tmp && TYPE_BINFO (outer_type))
327 tree binfos = TYPE_BINFO_BASETYPES (outer_type);
328 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
330 for (i = 0; i < n_baselinks; i++)
332 tree base_binfo = TREE_VEC_ELT (binfos, i);
333 tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
343 /* Returns nonzero iff the destructor name specified in NAME
344 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
348 check_dtor_name (basetype, name)
351 name = TREE_OPERAND (name, 0);
353 if (TREE_CODE (name) == TYPE_DECL)
354 name = TREE_TYPE (name);
355 else if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
357 else if (TREE_CODE (name) == IDENTIFIER_NODE)
359 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
360 || (TREE_CODE (basetype) == ENUMERAL_TYPE
361 && name == TYPE_IDENTIFIER (basetype)))
364 name = get_type_value (name);
367 my_friendly_abort (980605);
369 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
374 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
375 This is how virtual function calls are avoided. */
378 build_scoped_method_call (exp, basetype, name, parms)
379 tree exp, basetype, name, parms;
381 /* Because this syntactic form does not allow
382 a pointer to a base class to be `stolen',
383 we need not protect the derived->base conversion
386 @@ But we do have to check access privileges later. */
388 tree type = TREE_TYPE (exp);
390 if (type == error_mark_node
391 || basetype == error_mark_node)
392 return error_mark_node;
394 if (processing_template_decl)
396 if (TREE_CODE (name) == BIT_NOT_EXPR
397 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
399 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
401 name = build_min_nt (BIT_NOT_EXPR, type);
403 name = build_min_nt (SCOPE_REF, basetype, name);
404 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
407 if (TREE_CODE (type) == REFERENCE_TYPE)
408 type = TREE_TYPE (type);
410 if (TREE_CODE (basetype) == TREE_VEC)
413 basetype = BINFO_TYPE (binfo);
418 /* Check the destructor call syntax. */
419 if (TREE_CODE (name) == BIT_NOT_EXPR)
421 /* We can get here if someone writes their destructor call like
422 `obj.NS::~T()'; this isn't really a scoped method call, so hand
424 if (TREE_CODE (basetype) == NAMESPACE_DECL)
425 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
427 if (! check_dtor_name (basetype, name))
428 cp_error ("qualified type `%T' does not match destructor name `~%T'",
429 basetype, TREE_OPERAND (name, 0));
431 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
432 that explicit ~int is caught in the parser; this deals with typedefs
433 and template parms. */
434 if (! IS_AGGR_TYPE (basetype))
436 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
437 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
438 exp, basetype, type);
440 return cp_convert (void_type_node, exp);
444 if (! is_aggr_type (basetype, 1))
445 return error_mark_node;
447 if (! IS_AGGR_TYPE (type))
449 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
451 return error_mark_node;
456 binfo = get_binfo (basetype, type, 1);
457 if (binfo == error_mark_node)
458 return error_mark_node;
460 error_not_base_type (basetype, type);
465 if (TREE_CODE (exp) == INDIRECT_REF)
466 decl = build_indirect_ref
467 (convert_pointer_to_real
468 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
470 decl = build_scoped_ref (exp, basetype);
472 /* Call to a destructor. */
473 if (TREE_CODE (name) == BIT_NOT_EXPR)
475 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
476 return cp_convert (void_type_node, exp);
478 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
479 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
483 /* Call to a method. */
484 return build_method_call (decl, name, parms, binfo,
485 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
487 return error_mark_node;
490 /* We want the address of a function or method. We avoid creating a
491 pointer-to-member function. */
494 build_addr_func (function)
497 tree type = TREE_TYPE (function);
499 /* We have to do these by hand to avoid real pointer to member
501 if (TREE_CODE (type) == METHOD_TYPE)
505 type = build_pointer_type (type);
507 if (mark_addressable (function) == 0)
508 return error_mark_node;
510 addr = build1 (ADDR_EXPR, type, function);
512 /* Address of a static or external variable or function counts
514 if (staticp (function))
515 TREE_CONSTANT (addr) = 1;
520 function = default_conversion (function);
525 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
526 POINTER_TYPE to those. Note, pointer to member function types
527 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
530 build_call (function, result_type, parms)
531 tree function, result_type, parms;
533 int is_constructor = 0;
537 function = build_addr_func (function);
539 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
541 sorry ("unable to call pointer to member function here");
542 return error_mark_node;
545 if (TREE_CODE (function) == ADDR_EXPR
546 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
547 decl = TREE_OPERAND (function, 0);
551 if (decl && DECL_CONSTRUCTOR_P (decl))
554 /* Don't pass empty class objects by value. This is useful
555 for tags in STL, which are used to control overload resolution.
556 We don't need to handle other cases of copying empty classes. */
557 if (! decl || ! DECL_BUILT_IN (decl))
558 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
559 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
560 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
562 tree t = make_node (RTL_EXPR);
563 TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
564 RTL_EXPR_RTL (t) = const0_rtx;
565 RTL_EXPR_SEQUENCE (t) = NULL_RTX;
566 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
567 TREE_VALUE (tmp), t);
570 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
571 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
572 TREE_TYPE (function) = result_type;
573 TREE_SIDE_EFFECTS (function) = 1;
578 /* Build something of the form ptr->method (args)
579 or object.method (args). This can also build
580 calls to constructors, and find friends.
582 Member functions always take their class variable
585 INSTANCE is a class instance.
587 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
589 PARMS help to figure out what that NAME really refers to.
591 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
592 down to the real instance type to use for access checking. We need this
593 information to get protected accesses correct. This parameter is used
594 by build_member_call.
596 FLAGS is the logical disjunction of zero or more LOOKUP_
597 flags. See cp-tree.h for more info.
599 If this is all OK, calls build_function_call with the resolved
602 This function must also handle being called to perform
603 initialization, promotion/coercion of arguments, and
604 instantiation of default parameters.
606 Note that NAME may refer to an instance variable name. If
607 `operator()()' is defined for the type of that field, then we return
611 build_method_call (instance, name, parms, basetype_path, flags)
612 tree instance, name, parms, basetype_path;
615 tree basetype, instance_ptr;
617 #ifdef GATHER_STATISTICS
618 n_build_method_call++;
621 if (instance == error_mark_node
622 || name == error_mark_node
623 || parms == error_mark_node
624 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
625 return error_mark_node;
627 if (processing_template_decl)
629 /* We need to process template parm names here so that tsubst catches
630 them properly. Other type names can wait. */
631 if (TREE_CODE (name) == BIT_NOT_EXPR
632 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
634 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
635 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
636 name = build_min_nt (BIT_NOT_EXPR, type);
639 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
642 /* This is the logic that magically deletes the second argument to
643 operator delete, if it is not needed. */
644 if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
646 tree save_last = TREE_CHAIN (parms);
648 /* get rid of unneeded argument */
649 TREE_CHAIN (parms) = NULL_TREE;
650 if (build_method_call (instance, name, parms, basetype_path,
651 (LOOKUP_SPECULATIVELY|flags) & ~LOOKUP_COMPLAIN))
653 /* If it finds a match, return it. */
654 return build_method_call (instance, name, parms, basetype_path, flags);
656 /* If it doesn't work, two argument delete must work */
657 TREE_CHAIN (parms) = save_last;
659 /* We already know whether it's needed or not for vec delete. */
660 else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
661 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
662 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
663 TREE_CHAIN (parms) = NULL_TREE;
665 if (TREE_CODE (name) == BIT_NOT_EXPR)
668 error ("destructors take no parameters");
669 basetype = TREE_TYPE (instance);
670 if (TREE_CODE (basetype) == REFERENCE_TYPE)
671 basetype = TREE_TYPE (basetype);
673 if (! check_dtor_name (basetype, name))
675 ("destructor name `~%T' does not match type `%T' of expression",
676 TREE_OPERAND (name, 0), basetype);
678 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
679 return cp_convert (void_type_node, instance);
680 instance = default_conversion (instance);
681 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
682 return build_delete (build_pointer_type (basetype),
683 instance_ptr, integer_two_node,
684 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
687 return build_new_method_call (instance, name, parms, basetype_path, flags);
690 /* New overloading code. */
700 struct z_candidate *next;
703 #define IDENTITY_RANK 0
709 #define ELLIPSIS_RANK 6
712 #define ICS_RANK(NODE) \
713 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
714 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
715 : ICS_USER_FLAG (NODE) ? USER_RANK \
716 : ICS_STD_RANK (NODE))
718 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
720 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
721 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
722 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
723 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
725 #define USER_CONV_CAND(NODE) \
726 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
727 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
734 || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE))
740 build_conv (code, type, from)
744 tree t = build1 (code, type, from);
745 int rank = ICS_STD_RANK (from);
757 if (rank < EXACT_RANK)
763 ICS_STD_RANK (t) = rank;
764 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
765 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
773 if (TREE_CODE (t) == REFERENCE_TYPE)
782 if (TREE_CODE (t) == ARRAY_TYPE)
784 return TYPE_MAIN_VARIANT (t);
787 /* Returns the standard conversion path (see [conv]) from type FROM to type
788 TO, if any. For proper handling of null pointer constants, you must
789 also pass the expression EXPR to convert from. */
792 standard_conversion (to, from, expr)
795 enum tree_code fcode, tcode;
799 if (TREE_CODE (to) == REFERENCE_TYPE)
801 if (TREE_CODE (from) == REFERENCE_TYPE)
804 from = TREE_TYPE (from);
806 to = strip_top_quals (to);
807 from = strip_top_quals (from);
809 fcode = TREE_CODE (from);
810 tcode = TREE_CODE (to);
812 conv = build1 (IDENTITY_CONV, from, expr);
814 if (fcode == FUNCTION_TYPE)
816 from = build_pointer_type (from);
817 fcode = TREE_CODE (from);
818 conv = build_conv (LVALUE_CONV, from, conv);
820 else if (fcode == ARRAY_TYPE)
822 from = build_pointer_type (TREE_TYPE (from));
823 fcode = TREE_CODE (from);
824 conv = build_conv (LVALUE_CONV, from, conv);
826 else if (fromref || (expr && real_lvalue_p (expr)))
827 conv = build_conv (RVALUE_CONV, from, conv);
832 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
833 && expr && null_ptr_cst_p (expr))
835 conv = build_conv (STD_CONV, to, conv);
837 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
839 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
840 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
842 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
843 TYPE_MAIN_VARIANT (TREE_TYPE (to)), 1))
845 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
846 && ufcode != FUNCTION_TYPE)
848 from = build_pointer_type
849 (cp_build_type_variant (void_type_node,
850 TYPE_READONLY (TREE_TYPE (from)),
851 TYPE_VOLATILE (TREE_TYPE (from))));
852 conv = build_conv (PTR_CONV, from, conv);
854 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
856 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
857 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
859 if (DERIVED_FROM_P (fbase, tbase)
860 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
861 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))),
864 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
865 from = build_pointer_type (from);
866 conv = build_conv (PMEM_CONV, from, conv);
869 else if (IS_AGGR_TYPE (TREE_TYPE (from))
870 && IS_AGGR_TYPE (TREE_TYPE (to)))
872 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
874 from = cp_build_type_variant (TREE_TYPE (to),
875 TYPE_READONLY (TREE_TYPE (from)),
876 TYPE_VOLATILE (TREE_TYPE (from)));
877 from = build_pointer_type (from);
878 conv = build_conv (PTR_CONV, from, conv);
882 if (comptypes (from, to, 1))
884 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
885 conv = build_conv (QUAL_CONV, to, conv);
886 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
888 conv = build_conv (PTR_CONV, to, conv);
889 ICS_BAD_FLAG (conv) = 1;
896 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
898 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
899 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
900 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
901 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
903 if (! DERIVED_FROM_P (fbase, tbase)
904 || ! comptypes (TREE_TYPE (fromfn), TREE_TYPE (tofn), 1)
905 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
906 TREE_CHAIN (TYPE_ARG_TYPES (tofn)), 1)
907 || TYPE_READONLY (fbase) != TYPE_READONLY (tbase)
908 || TYPE_VOLATILE (fbase) != TYPE_VOLATILE (tbase))
911 from = cp_build_type_variant (tbase, TYPE_READONLY (fbase),
912 TYPE_VOLATILE (fbase));
913 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
914 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
915 from = build_ptrmemfunc_type (build_pointer_type (from));
916 conv = build_conv (PMEM_CONV, from, conv);
918 else if (tcode == BOOLEAN_TYPE)
920 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
921 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
924 conv = build_conv (STD_CONV, to, conv);
925 if (fcode == POINTER_TYPE
926 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
927 ICS_STD_RANK (conv) = PBOOL_RANK;
929 /* We don't check for ENUMERAL_TYPE here because there are no standard
930 conversions to enum type. */
931 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
932 || tcode == REAL_TYPE)
934 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
936 conv = build_conv (STD_CONV, to, conv);
938 /* Give this a better rank if it's a promotion. */
939 if (to == type_promotes_to (from)
940 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
941 ICS_STD_RANK (conv) = PROMO_RANK;
943 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
944 && DERIVED_FROM_P (to, from))
946 if (TREE_CODE (conv) == RVALUE_CONV)
947 conv = TREE_OPERAND (conv, 0);
948 conv = build_conv (BASE_CONV, to, conv);
956 /* Returns the conversion path from type FROM to reference type TO for
957 purposes of reference binding. For lvalue binding, either pass a
958 reference type to FROM or an lvalue expression to EXPR.
960 Currently does not distinguish in the generated trees between binding to
961 an lvalue and a temporary. Should it? */
964 reference_binding (rto, rfrom, expr, flags)
965 tree rto, rfrom, expr;
970 tree to = TREE_TYPE (rto);
974 if (TREE_CODE (from) == REFERENCE_TYPE)
975 from = TREE_TYPE (from);
976 else if (! expr || ! real_lvalue_p (expr))
979 related = (comptypes (TYPE_MAIN_VARIANT (to),
980 TYPE_MAIN_VARIANT (from), 1)
981 || (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
982 && DERIVED_FROM_P (to, from)));
984 if (lvalue && related
985 && TYPE_READONLY (to) >= TYPE_READONLY (from)
986 && TYPE_VOLATILE (to) >= TYPE_VOLATILE (from))
988 conv = build1 (IDENTITY_CONV, from, expr);
990 if (comptypes (TYPE_MAIN_VARIANT (to),
991 TYPE_MAIN_VARIANT (from), 1))
992 conv = build_conv (REF_BIND, rto, conv);
995 conv = build_conv (REF_BIND, rto, conv);
996 ICS_STD_RANK (conv) = STD_RANK;
1004 conv = standard_conversion (to, rfrom, expr);
1007 conv = build_conv (REF_BIND, rto, conv);
1009 /* Bind directly to a base subobject of a class rvalue. Do it
1010 after building the conversion for proper handling of ICS_RANK. */
1011 if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
1012 TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
1015 && ((! (TYPE_READONLY (to) && ! TYPE_VOLATILE (to)
1016 && (flags & LOOKUP_NO_TEMP_BIND) == 0))
1017 /* If T1 is reference-related to T2, cv1 must be the same
1018 cv-qualification as, or greater cv-qualification than,
1019 cv2; otherwise, the program is ill-formed. */
1021 && (TYPE_READONLY (to) < TYPE_READONLY (from)
1022 || TYPE_VOLATILE (to) < TYPE_VOLATILE (from)))))
1023 ICS_BAD_FLAG (conv) = 1;
1029 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1030 to type TO. The optional expression EXPR may affect the conversion.
1031 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1035 implicit_conversion (to, from, expr, flags)
1036 tree to, from, expr;
1040 struct z_candidate *cand;
1042 if (expr && type_unknown_p (expr))
1044 expr = instantiate_type (to, expr, 0);
1045 if (expr == error_mark_node)
1047 from = TREE_TYPE (expr);
1050 if (TREE_CODE (to) == REFERENCE_TYPE)
1051 conv = reference_binding (to, from, expr, flags);
1053 conv = standard_conversion (to, from, expr);
1057 else if (expr != NULL_TREE
1058 && (IS_AGGR_TYPE (non_reference (from))
1059 || IS_AGGR_TYPE (non_reference (to)))
1060 && (flags & LOOKUP_NO_CONVERSION) == 0)
1062 cand = build_user_type_conversion_1
1063 (to, expr, LOOKUP_ONLYCONVERTING);
1065 conv = cand->second_conv;
1066 if ((! conv || ICS_BAD_FLAG (conv))
1067 && TREE_CODE (to) == REFERENCE_TYPE
1068 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1070 cand = build_user_type_conversion_1
1071 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1074 if (! TYPE_READONLY (TREE_TYPE (to))
1075 || TYPE_VOLATILE (TREE_TYPE (to)))
1076 ICS_BAD_FLAG (cand->second_conv) = 1;
1077 if (!conv || (ICS_BAD_FLAG (conv)
1078 > ICS_BAD_FLAG (cand->second_conv)))
1079 conv = build_conv (REF_BIND, to, cand->second_conv);
1087 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1090 static struct z_candidate *
1091 add_candidate (candidates, fn, convs, viable)
1092 struct z_candidate *candidates;
1096 struct z_candidate *cand
1097 = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
1100 cand->convs = convs;
1101 cand->second_conv = NULL_TREE;
1102 cand->viable = viable;
1103 cand->basetype_path = NULL_TREE;
1104 cand->template = NULL_TREE;
1105 cand->warnings = NULL_TREE;
1106 cand->next = candidates;
1111 /* Create an overload candidate for the function or method FN called with
1112 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1113 to implicit_conversion. */
1115 static struct z_candidate *
1116 add_function_candidate (candidates, fn, arglist, flags)
1117 struct z_candidate *candidates;
1121 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1124 tree parmnode = parmlist;
1125 tree argnode = arglist;
1128 /* The `this' and `in_chrg' arguments to constructors are not considered
1129 in overload resolution. */
1130 if (DECL_CONSTRUCTOR_P (fn))
1132 parmnode = TREE_CHAIN (parmnode);
1133 argnode = TREE_CHAIN (argnode);
1134 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1136 parmnode = TREE_CHAIN (parmnode);
1137 argnode = TREE_CHAIN (argnode);
1141 len = list_length (argnode);
1142 convs = make_scratch_vec (len);
1144 for (i = 0; i < len; ++i)
1146 tree arg = TREE_VALUE (argnode);
1147 tree argtype = lvalue_type (arg);
1150 if (parmnode == void_list_node)
1153 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1156 t = build1 (IDENTITY_CONV, argtype, arg);
1157 ICS_ELLIPSIS_FLAG (t) = 1;
1160 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1161 && ! DECL_CONSTRUCTOR_P (fn))
1162 ICS_THIS_FLAG (t) = 1;
1164 TREE_VEC_ELT (convs, i) = t;
1168 if (ICS_BAD_FLAG (t))
1172 parmnode = TREE_CHAIN (parmnode);
1173 argnode = TREE_CHAIN (argnode);
1179 /* Make sure there are default args for the rest of the parms. */
1180 for (; parmnode && parmnode != void_list_node;
1181 parmnode = TREE_CHAIN (parmnode))
1182 if (! TREE_PURPOSE (parmnode))
1188 return add_candidate (candidates, fn, convs, viable);
1191 /* Create an overload candidate for the conversion function FN which will
1192 be invoked for expression OBJ, producing a pointer-to-function which
1193 will in turn be called with the argument list ARGLIST, and add it to
1194 CANDIDATES. FLAGS is passed on to implicit_conversion. */
1196 static struct z_candidate *
1197 add_conv_candidate (candidates, fn, obj, arglist)
1198 struct z_candidate *candidates;
1199 tree fn, obj, arglist;
1201 tree totype = TREE_TYPE (TREE_TYPE (fn));
1202 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1203 int i, len = list_length (arglist) + 1;
1204 tree convs = make_scratch_vec (len);
1205 tree parmnode = parmlist;
1206 tree argnode = arglist;
1208 int flags = LOOKUP_NORMAL;
1210 for (i = 0; i < len; ++i)
1212 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1213 tree argtype = lvalue_type (arg);
1217 t = implicit_conversion (totype, argtype, arg, flags);
1218 else if (parmnode == void_list_node)
1221 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1224 t = build1 (IDENTITY_CONV, argtype, arg);
1225 ICS_ELLIPSIS_FLAG (t) = 1;
1228 TREE_VEC_ELT (convs, i) = t;
1232 if (ICS_BAD_FLAG (t))
1239 parmnode = TREE_CHAIN (parmnode);
1240 argnode = TREE_CHAIN (argnode);
1246 for (; parmnode && parmnode != void_list_node;
1247 parmnode = TREE_CHAIN (parmnode))
1248 if (! TREE_PURPOSE (parmnode))
1254 return add_candidate (candidates, fn, convs, viable);
1257 static struct z_candidate *
1258 build_builtin_candidate (candidates, fnname, type1, type2,
1259 args, argtypes, flags)
1260 struct z_candidate *candidates;
1261 tree fnname, type1, type2, *args, *argtypes;
1272 convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1274 for (i = 0; i < 2; ++i)
1279 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1283 /* We need something for printing the candidate. */
1284 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1286 else if (ICS_BAD_FLAG (t))
1288 TREE_VEC_ELT (convs, i) = t;
1291 /* For COND_EXPR we rearranged the arguments; undo that now. */
1294 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1295 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1296 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1298 TREE_VEC_ELT (convs, 0) = t;
1303 return add_candidate (candidates, fnname, convs, viable);
1310 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1313 /* Create any builtin operator overload candidates for the operator in
1314 question given the converted operand types TYPE1 and TYPE2. The other
1315 args are passed through from add_builtin_candidates to
1316 build_builtin_candidate. */
1318 static struct z_candidate *
1319 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1320 args, argtypes, flags)
1321 struct z_candidate *candidates;
1322 enum tree_code code, code2;
1323 tree fnname, type1, type2, *args, *argtypes;
1328 case POSTINCREMENT_EXPR:
1329 case POSTDECREMENT_EXPR:
1330 args[1] = integer_zero_node;
1331 type2 = integer_type_node;
1340 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1341 and VQ is either volatile or empty, there exist candidate operator
1342 functions of the form
1343 VQ T& operator++(VQ T&);
1344 T operator++(VQ T&, int);
1345 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1346 type other than bool, and VQ is either volatile or empty, there exist
1347 candidate operator functions of the form
1348 VQ T& operator--(VQ T&);
1349 T operator--(VQ T&, int);
1350 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1351 complete object type, and VQ is either volatile or empty, there exist
1352 candidate operator functions of the form
1353 T*VQ& operator++(T*VQ&);
1354 T*VQ& operator--(T*VQ&);
1355 T* operator++(T*VQ&, int);
1356 T* operator--(T*VQ&, int); */
1358 case POSTDECREMENT_EXPR:
1359 case PREDECREMENT_EXPR:
1360 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1362 case POSTINCREMENT_EXPR:
1363 case PREINCREMENT_EXPR:
1364 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1365 || TYPE_PTROB_P (type1))
1367 type1 = build_reference_type (type1);
1372 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1373 exist candidate operator functions of the form
1377 8 For every function type T, there exist candidate operator functions of
1379 T& operator*(T*); */
1382 if (TREE_CODE (type1) == POINTER_TYPE
1383 && (TYPE_PTROB_P (type1)
1384 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1388 /* 9 For every type T, there exist candidate operator functions of the form
1391 10For every promoted arithmetic type T, there exist candidate operator
1392 functions of the form
1396 case CONVERT_EXPR: /* unary + */
1397 if (TREE_CODE (type1) == POINTER_TYPE
1398 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1401 if (ARITHMETIC_TYPE_P (type1))
1405 /* 11For every promoted integral type T, there exist candidate operator
1406 functions of the form
1410 if (INTEGRAL_TYPE_P (type1))
1414 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1415 is the same type as C2 or is a derived class of C2, T is a complete
1416 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1417 there exist candidate operator functions of the form
1418 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1419 where CV12 is the union of CV1 and CV2. */
1422 if (TREE_CODE (type1) == POINTER_TYPE
1423 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1425 tree c1 = TREE_TYPE (type1);
1426 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1427 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1428 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1430 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1431 && (TYPE_PTRMEMFUNC_P (type2)
1432 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1437 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1438 didate operator functions of the form
1443 bool operator<(L, R);
1444 bool operator>(L, R);
1445 bool operator<=(L, R);
1446 bool operator>=(L, R);
1447 bool operator==(L, R);
1448 bool operator!=(L, R);
1449 where LR is the result of the usual arithmetic conversions between
1452 14For every pair of types T and I, where T is a cv-qualified or cv-
1453 unqualified complete object type and I is a promoted integral type,
1454 there exist candidate operator functions of the form
1455 T* operator+(T*, I);
1456 T& operator[](T*, I);
1457 T* operator-(T*, I);
1458 T* operator+(I, T*);
1459 T& operator[](I, T*);
1461 15For every T, where T is a pointer to complete object type, there exist
1462 candidate operator functions of the form112)
1463 ptrdiff_t operator-(T, T);
1465 16For every pointer type T, there exist candidate operator functions of
1467 bool operator<(T, T);
1468 bool operator>(T, T);
1469 bool operator<=(T, T);
1470 bool operator>=(T, T);
1471 bool operator==(T, T);
1472 bool operator!=(T, T);
1474 17For every pointer to member type T, there exist candidate operator
1475 functions of the form
1476 bool operator==(T, T);
1477 bool operator!=(T, T); */
1480 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1482 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1484 type2 = ptrdiff_type_node;
1488 case TRUNC_DIV_EXPR:
1489 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1495 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1496 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1498 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1499 && null_ptr_cst_p (args[1]))
1504 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1505 && null_ptr_cst_p (args[0]))
1516 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1517 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1519 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1524 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1532 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1535 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1537 type1 = ptrdiff_type_node;
1540 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1542 type2 = ptrdiff_type_node;
1547 /* 18For every pair of promoted integral types L and R, there exist candi-
1548 date operator functions of the form
1555 where LR is the result of the usual arithmetic conversions between
1558 case TRUNC_MOD_EXPR:
1564 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1568 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1569 type, VQ is either volatile or empty, and R is a promoted arithmetic
1570 type, there exist candidate operator functions of the form
1571 VQ L& operator=(VQ L&, R);
1572 VQ L& operator*=(VQ L&, R);
1573 VQ L& operator/=(VQ L&, R);
1574 VQ L& operator+=(VQ L&, R);
1575 VQ L& operator-=(VQ L&, R);
1577 20For every pair T, VQ), where T is any type and VQ is either volatile
1578 or empty, there exist candidate operator functions of the form
1579 T*VQ& operator=(T*VQ&, T*);
1581 21For every pair T, VQ), where T is a pointer to member type and VQ is
1582 either volatile or empty, there exist candidate operator functions of
1584 VQ T& operator=(VQ T&, T);
1586 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1587 unqualified complete object type, VQ is either volatile or empty, and
1588 I is a promoted integral type, there exist candidate operator func-
1590 T*VQ& operator+=(T*VQ&, I);
1591 T*VQ& operator-=(T*VQ&, I);
1593 23For every triple L, VQ, R), where L is an integral or enumeration
1594 type, VQ is either volatile or empty, and R is a promoted integral
1595 type, there exist candidate operator functions of the form
1597 VQ L& operator%=(VQ L&, R);
1598 VQ L& operator<<=(VQ L&, R);
1599 VQ L& operator>>=(VQ L&, R);
1600 VQ L& operator&=(VQ L&, R);
1601 VQ L& operator^=(VQ L&, R);
1602 VQ L& operator|=(VQ L&, R); */
1609 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1611 type2 = ptrdiff_type_node;
1615 case TRUNC_DIV_EXPR:
1616 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1620 case TRUNC_MOD_EXPR:
1626 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1631 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1633 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1634 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1635 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1636 || ((TYPE_PTRMEMFUNC_P (type1)
1637 || TREE_CODE (type1) == POINTER_TYPE)
1638 && null_ptr_cst_p (args[1])))
1646 my_friendly_abort (367);
1648 type1 = build_reference_type (type1);
1652 /* Kludge around broken overloading rules whereby
1653 bool ? const char& : enum is ambiguous
1654 (between int and const char&). */
1655 flags |= LOOKUP_NO_TEMP_BIND;
1657 /* Extension: Support ?: of enumeral type. Hopefully this will not
1658 be an extension for long. */
1659 if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
1661 else if (TREE_CODE (type1) == ENUMERAL_TYPE
1662 || TREE_CODE (type2) == ENUMERAL_TYPE)
1664 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1666 if (TREE_CODE (type1) == TREE_CODE (type2)
1667 && (TREE_CODE (type1) == REFERENCE_TYPE
1668 || TREE_CODE (type1) == POINTER_TYPE
1669 || TYPE_PTRMEMFUNC_P (type1)
1670 || IS_AGGR_TYPE (type1)))
1672 if (TREE_CODE (type1) == REFERENCE_TYPE
1673 || TREE_CODE (type2) == REFERENCE_TYPE)
1675 if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
1676 && null_ptr_cst_p (args[1]))
1677 || IS_AGGR_TYPE (type1))
1682 if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
1683 && null_ptr_cst_p (args[0]))
1684 || IS_AGGR_TYPE (type2))
1692 my_friendly_abort (367);
1695 /* If we're dealing with two pointer types, we need candidates
1696 for both of them. */
1697 if (type2 && type1 != type2
1698 && TREE_CODE (type1) == TREE_CODE (type2)
1699 && (TREE_CODE (type1) == REFERENCE_TYPE
1700 || (TREE_CODE (type1) == POINTER_TYPE
1701 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1702 || TYPE_PTRMEMFUNC_P (type1)
1703 || IS_AGGR_TYPE (type1)))
1705 candidates = build_builtin_candidate
1706 (candidates, fnname, type1, type1, args, argtypes, flags);
1707 return build_builtin_candidate
1708 (candidates, fnname, type2, type2, args, argtypes, flags);
1711 return build_builtin_candidate
1712 (candidates, fnname, type1, type2, args, argtypes, flags);
1716 type_decays_to (type)
1719 if (TREE_CODE (type) == ARRAY_TYPE)
1720 return build_pointer_type (TREE_TYPE (type));
1721 if (TREE_CODE (type) == FUNCTION_TYPE)
1722 return build_pointer_type (type);
1726 /* There are three conditions of builtin candidates:
1728 1) bool-taking candidates. These are the same regardless of the input.
1729 2) pointer-pair taking candidates. These are generated for each type
1730 one of the input types converts to.
1731 3) arithmetic candidates. According to the WP, we should generate
1732 all of these, but I'm trying not to... */
1734 static struct z_candidate *
1735 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1736 struct z_candidate *candidates;
1737 enum tree_code code, code2;
1742 tree type, argtypes[3], types[2];
1744 for (i = 0; i < 3; ++i)
1747 argtypes[i] = lvalue_type (args[i]);
1749 argtypes[i] = NULL_TREE;
1754 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1755 and VQ is either volatile or empty, there exist candidate operator
1756 functions of the form
1757 VQ T& operator++(VQ T&); */
1759 case POSTINCREMENT_EXPR:
1760 case PREINCREMENT_EXPR:
1761 case POSTDECREMENT_EXPR:
1762 case PREDECREMENT_EXPR:
1767 /* 24There also exist candidate operator functions of the form
1768 bool operator!(bool);
1769 bool operator&&(bool, bool);
1770 bool operator||(bool, bool); */
1772 case TRUTH_NOT_EXPR:
1773 return build_builtin_candidate
1774 (candidates, fnname, boolean_type_node,
1775 NULL_TREE, args, argtypes, flags);
1777 case TRUTH_ORIF_EXPR:
1778 case TRUTH_ANDIF_EXPR:
1779 return build_builtin_candidate
1780 (candidates, fnname, boolean_type_node,
1781 boolean_type_node, args, argtypes, flags);
1792 types[0] = types[1] = NULL_TREE;
1794 for (i = 0; i < 2; ++i)
1798 else if (IS_AGGR_TYPE (argtypes[i]))
1800 tree convs = lookup_conversions (argtypes[i]);
1802 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1805 convs = lookup_conversions (argtypes[i]);
1807 if (code == COND_EXPR)
1809 if (real_lvalue_p (args[i]))
1810 types[i] = scratch_tree_cons
1811 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1813 types[i] = scratch_tree_cons
1814 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1820 for (; convs; convs = TREE_CHAIN (convs))
1822 type = TREE_TYPE (TREE_TYPE (TREE_VALUE (convs)));
1825 && (TREE_CODE (type) != REFERENCE_TYPE
1826 || TYPE_READONLY (TREE_TYPE (type))))
1829 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1830 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1832 type = non_reference (type);
1833 if (i != 0 || ! ref1)
1835 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1836 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1837 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1838 if (INTEGRAL_TYPE_P (type))
1839 type = type_promotes_to (type);
1842 if (! value_member (type, types[i]))
1843 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1848 if (code == COND_EXPR && real_lvalue_p (args[i]))
1849 types[i] = scratch_tree_cons
1850 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1851 type = non_reference (argtypes[i]);
1852 if (i != 0 || ! ref1)
1854 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1855 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1856 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1857 if (INTEGRAL_TYPE_P (type))
1858 type = type_promotes_to (type);
1860 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1864 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1867 for (type = types[1]; type; type = TREE_CHAIN (type))
1868 candidates = add_builtin_candidate
1869 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1870 TREE_VALUE (type), args, argtypes, flags);
1872 candidates = add_builtin_candidate
1873 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1874 NULL_TREE, args, argtypes, flags);
1881 /* If TMPL can be successfully instantiated as indicated by
1882 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
1884 TMPL is the template. EXPLICIT_TARGS are any explicit template
1885 arguments. ARGLIST is the arguments provided at the call-site.
1886 The RETURN_TYPE is the desired type for conversion operators. If
1887 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
1888 OBJ is supplied, FLAGS are ignored, and OBJ is as for
1889 add_conv_candidate. */
1891 static struct z_candidate*
1892 add_template_candidate_real (candidates, tmpl, explicit_targs,
1893 arglist, return_type, flags,
1895 struct z_candidate *candidates;
1896 tree tmpl, explicit_targs, arglist, return_type;
1899 unification_kind_t strict;
1901 int ntparms = DECL_NTPARMS (tmpl);
1902 tree targs = make_scratch_vec (ntparms);
1903 struct z_candidate *cand;
1907 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
1908 return_type, strict, NULL_TREE);
1913 fn = instantiate_template (tmpl, targs);
1914 if (fn == error_mark_node)
1917 if (obj != NULL_TREE)
1918 /* Aha, this is a conversion function. */
1919 cand = add_conv_candidate (candidates, fn, obj, arglist);
1921 cand = add_function_candidate (candidates, fn, arglist, flags);
1922 if (DECL_TI_TEMPLATE (fn) != tmpl)
1923 /* This situation can occur if a member template of a template
1924 class is specialized. Then, instantiate_template might return
1925 an instantiation of the specialization, in which case the
1926 DECL_TI_TEMPLATE field will point at the original
1927 specialization. For example:
1929 template <class T> struct S { template <class U> void f(U);
1930 template <> void f(int) {}; };
1934 Here, TMPL will be template <class U> S<double>::f(U).
1935 And, instantiate template will give us the specialization
1936 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
1937 for this will point at template <class T> template <> S<T>::f(int),
1938 so that we can find the definition. For the purposes of
1939 overload resolution, however, we want the original TMPL. */
1940 cand->template = tree_cons (tmpl, targs, NULL_TREE);
1942 cand->template = DECL_TEMPLATE_INFO (fn);
1948 static struct z_candidate *
1949 add_template_candidate (candidates, tmpl, explicit_targs,
1950 arglist, return_type, flags, strict)
1951 struct z_candidate *candidates;
1952 tree tmpl, explicit_targs, arglist, return_type;
1954 unification_kind_t strict;
1957 add_template_candidate_real (candidates, tmpl, explicit_targs,
1958 arglist, return_type, flags,
1963 static struct z_candidate *
1964 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
1965 struct z_candidate *candidates;
1966 tree tmpl, obj, arglist, return_type;
1969 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
1970 return_type, 0, obj, DEDUCE_CONV);
1976 struct z_candidate *cands;
1978 for (; cands; cands = cands->next)
1979 if (pedantic ? cands->viable == 1 : cands->viable)
1984 static struct z_candidate *
1985 splice_viable (cands)
1986 struct z_candidate *cands;
1988 struct z_candidate **p = &cands;
1992 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2005 /* Fix this to work on non-lvalues. */
2006 if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
2007 || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
2010 return build_unary_op (ADDR_EXPR, obj, 0);
2014 print_z_candidates (candidates)
2015 struct z_candidate *candidates;
2017 char *str = "candidates are:";
2018 for (; candidates; candidates = candidates->next)
2020 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2022 if (candidates->fn == ansi_opname [COND_EXPR])
2023 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2024 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2025 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2026 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2027 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2028 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2029 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2030 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2032 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2033 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2036 cp_error_at ("%s %+D%s", str, candidates->fn,
2037 candidates->viable == -1 ? " <near match>" : "");
2042 /* Returns the best overload candidate to perform the requested
2043 conversion. This function is used for three the overloading situations
2044 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2045 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2046 per [dcl.init.ref], so we ignore temporary bindings. */
2048 static struct z_candidate *
2049 build_user_type_conversion_1 (totype, expr, flags)
2053 struct z_candidate *candidates, *cand;
2054 tree fromtype = TREE_TYPE (expr);
2055 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2056 tree args = NULL_TREE;
2057 tree templates = NULL_TREE;
2059 if (IS_AGGR_TYPE (totype))
2060 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2061 if (IS_AGGR_TYPE (fromtype)
2062 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2063 convs = lookup_conversions (fromtype);
2066 flags |= LOOKUP_NO_CONVERSION;
2070 tree t = build_int_2 (0, 0);
2071 TREE_TYPE (t) = build_pointer_type (totype);
2072 args = build_scratch_list (NULL_TREE, expr);
2073 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2074 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2075 args = scratch_tree_cons (NULL_TREE, t, args);
2077 ctors = TREE_VALUE (ctors);
2079 for (; ctors; ctors = OVL_NEXT (ctors))
2081 tree ctor = OVL_CURRENT (ctors);
2082 if (DECL_NONCONVERTING_P (ctor))
2085 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2087 templates = scratch_tree_cons (NULL_TREE, ctor, templates);
2089 add_template_candidate (candidates, ctor,
2090 NULL_TREE, args, NULL_TREE, flags,
2094 candidates = add_function_candidate (candidates, ctor,
2099 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2100 candidates->basetype_path = TYPE_BINFO (totype);
2105 args = build_scratch_list (NULL_TREE, build_this (expr));
2107 for (; convs; convs = TREE_CHAIN (convs))
2109 tree fns = TREE_VALUE (convs);
2110 int convflags = LOOKUP_NO_CONVERSION;
2113 /* If we are called to convert to a reference type, we are trying to
2114 find an lvalue binding, so don't even consider temporaries. If
2115 we don't find an lvalue binding, the caller will try again to
2116 look for a temporary binding. */
2117 if (TREE_CODE (totype) == REFERENCE_TYPE)
2118 convflags |= LOOKUP_NO_TEMP_BIND;
2120 if (TREE_CODE (fns) != TEMPLATE_DECL)
2121 ics = implicit_conversion
2122 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2124 /* We can't compute this yet. */
2125 ics = error_mark_node;
2127 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2128 /* ignore the near match. */;
2130 for (; fns; fns = OVL_NEXT (fns))
2132 tree fn = OVL_CURRENT (fns);
2133 struct z_candidate *old_candidates = candidates;
2135 if (TREE_CODE (fn) == TEMPLATE_DECL)
2137 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2139 add_template_candidate (candidates, fn, NULL_TREE,
2140 args, totype, flags,
2144 candidates = add_function_candidate (candidates, fn,
2147 if (candidates != old_candidates)
2149 if (TREE_CODE (fn) == TEMPLATE_DECL)
2150 ics = implicit_conversion
2151 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2154 candidates->second_conv = ics;
2155 candidates->basetype_path = TREE_PURPOSE (convs);
2157 if (ics == NULL_TREE)
2158 candidates->viable = 0;
2159 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2160 candidates->viable = -1;
2165 if (! any_viable (candidates))
2168 if (flags & LOOKUP_COMPLAIN)
2170 if (candidates && ! candidates->next)
2171 /* say why this one won't work or try to be loose */;
2173 cp_error ("no viable candidates");
2180 candidates = splice_viable (candidates);
2181 cand = tourney (candidates);
2185 if (flags & LOOKUP_COMPLAIN)
2187 cp_error ("conversion from `%T' to `%T' is ambiguous",
2189 print_z_candidates (candidates);
2192 cand = candidates; /* any one will do */
2193 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2194 ICS_USER_FLAG (cand->second_conv) = 1;
2195 ICS_BAD_FLAG (cand->second_conv) = 1;
2200 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2201 p = &(TREE_OPERAND (*p, 0));
2203 /* Pedantically, normal function declarations are never considered
2204 to refer to template instantiations, so we only do this with
2206 if (flag_guiding_decls && templates && ! cand->template
2207 && !DECL_INITIAL (cand->fn)
2208 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2209 add_maybe_template (cand->fn, templates);
2213 (DECL_CONSTRUCTOR_P (cand->fn)
2214 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2215 expr, build_expr_ptr_wrapper (cand));
2216 ICS_USER_FLAG (cand->second_conv) = 1;
2217 if (cand->viable == -1)
2218 ICS_BAD_FLAG (cand->second_conv) = 1;
2224 build_user_type_conversion (totype, expr, flags)
2228 struct z_candidate *cand
2229 = build_user_type_conversion_1 (totype, expr, flags);
2233 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2234 return error_mark_node;
2235 return convert_from_reference (convert_like (cand->second_conv, expr));
2240 /* Do any initial processing on the arguments to a function call. */
2247 for (t = args; t; t = TREE_CHAIN (t))
2249 if (TREE_VALUE (t) == error_mark_node)
2250 return error_mark_node;
2251 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2253 error ("invalid use of void expression");
2254 return error_mark_node;
2256 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2257 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2263 build_new_function_call (fn, args)
2266 struct z_candidate *candidates = 0, *cand;
2267 tree explicit_targs = NULL_TREE;
2268 int template_only = 0;
2270 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2272 explicit_targs = TREE_OPERAND (fn, 1);
2273 fn = TREE_OPERAND (fn, 0);
2277 if (really_overloaded_fn (fn))
2280 tree templates = NULL_TREE;
2282 args = resolve_args (args);
2284 if (args == error_mark_node)
2285 return error_mark_node;
2287 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2289 tree t = OVL_FUNCTION (t1);
2290 if (TREE_CODE (t) == TEMPLATE_DECL)
2292 templates = scratch_tree_cons (NULL_TREE, t, templates);
2293 candidates = add_template_candidate
2294 (candidates, t, explicit_targs, args, NULL_TREE,
2295 LOOKUP_NORMAL, DEDUCE_CALL);
2297 else if (! template_only)
2298 candidates = add_function_candidate
2299 (candidates, t, args, LOOKUP_NORMAL);
2302 if (! any_viable (candidates))
2304 if (candidates && ! candidates->next)
2305 return build_function_call (candidates->fn, args);
2306 cp_error ("no matching function for call to `%D (%A)'",
2307 DECL_NAME (OVL_FUNCTION (fn)), args);
2309 print_z_candidates (candidates);
2310 return error_mark_node;
2312 candidates = splice_viable (candidates);
2313 cand = tourney (candidates);
2317 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2318 DECL_NAME (OVL_FUNCTION (fn)), args);
2319 print_z_candidates (candidates);
2320 return error_mark_node;
2323 /* Pedantically, normal function declarations are never considered
2324 to refer to template instantiations, so we only do this with
2326 if (flag_guiding_decls && templates && ! cand->template
2327 && ! DECL_INITIAL (cand->fn))
2328 add_maybe_template (cand->fn, templates);
2330 return build_over_call (cand, args, LOOKUP_NORMAL);
2333 /* This is not really overloaded. */
2334 fn = OVL_CURRENT (fn);
2336 return build_function_call (fn, args);
2340 build_object_call (obj, args)
2343 struct z_candidate *candidates = 0, *cand;
2344 tree fns, convs, mem_args = NULL_TREE;
2345 tree type = TREE_TYPE (obj);
2346 tree templates = NULL_TREE;
2348 if (TYPE_PTRMEMFUNC_P (type))
2350 /* It's no good looking for an overloaded operator() on a
2351 pointer-to-member-function. */
2352 cp_error ("pointer-to-member function %E cannot be called", obj);
2353 cp_error ("without an object; consider using .* or ->*");
2354 return error_mark_node;
2357 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2358 if (fns == error_mark_node)
2359 return error_mark_node;
2361 args = resolve_args (args);
2363 if (args == error_mark_node)
2364 return error_mark_node;
2368 tree base = TREE_PURPOSE (fns);
2369 mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2371 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2373 tree fn = OVL_CURRENT (fns);
2374 if (TREE_CODE (fn) == TEMPLATE_DECL)
2376 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2378 = add_template_candidate (candidates, fn, NULL_TREE,
2379 mem_args, NULL_TREE,
2380 LOOKUP_NORMAL, DEDUCE_CALL);
2383 candidates = add_function_candidate
2384 (candidates, fn, mem_args, LOOKUP_NORMAL);
2387 candidates->basetype_path = base;
2391 convs = lookup_conversions (type);
2393 for (; convs; convs = TREE_CHAIN (convs))
2395 tree fns = TREE_VALUE (convs);
2396 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2399 if (TREE_CODE (totype) == POINTER_TYPE
2400 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2401 for (; fns; fns = OVL_NEXT (fn))
2403 fn = OVL_CURRENT (fn);
2404 if (TREE_CODE (fn) == TEMPLATE_DECL)
2406 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2407 candidates = add_template_conv_candidate (candidates,
2414 candidates = add_conv_candidate (candidates, fn, obj, args);
2417 candidates->basetype_path = TREE_PURPOSE (convs);
2421 if (! any_viable (candidates))
2423 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2424 print_z_candidates (candidates);
2425 return error_mark_node;
2428 candidates = splice_viable (candidates);
2429 cand = tourney (candidates);
2433 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2434 print_z_candidates (candidates);
2435 return error_mark_node;
2438 if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2439 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2441 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2444 return build_function_call (obj, args);
2448 op_error (code, code2, arg1, arg2, arg3, problem)
2449 enum tree_code code, code2;
2450 tree arg1, arg2, arg3;
2454 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2459 cp_error ("%s for `%T ? %T : %T'", problem,
2460 error_type (arg1), error_type (arg2), error_type (arg3));
2462 case POSTINCREMENT_EXPR:
2463 case POSTDECREMENT_EXPR:
2464 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2467 cp_error ("%s for `%T[%T]'", problem,
2468 error_type (arg1), error_type (arg2));
2472 cp_error ("%s for `%T %s %T'", problem,
2473 error_type (arg1), opname, error_type (arg2));
2475 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2480 build_new_op (code, flags, arg1, arg2, arg3)
2481 enum tree_code code;
2483 tree arg1, arg2, arg3;
2485 struct z_candidate *candidates = 0, *cand;
2486 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
2487 enum tree_code code2 = NOP_EXPR;
2488 tree templates = NULL_TREE;
2491 if (arg1 == error_mark_node
2492 || arg2 == error_mark_node
2493 || arg3 == error_mark_node)
2494 return error_mark_node;
2496 /* This can happen if a template takes all non-type parameters, e.g.
2497 undeclared_template<1, 5, 72>a; */
2498 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
2500 cp_error ("`%D' must be declared before use", arg1);
2501 return error_mark_node;
2504 if (code == MODIFY_EXPR)
2506 code2 = TREE_CODE (arg3);
2508 fnname = ansi_assopname[code2];
2511 fnname = ansi_opname[code];
2520 arglist = scratch_tree_cons (NULL_TREE, arg2, arg3);
2521 if (flags & LOOKUP_GLOBAL)
2522 return build_new_function_call
2523 (lookup_function_nonclass (fnname, arglist), arglist);
2526 rval = build_method_call
2527 (build_indirect_ref (build1 (NOP_EXPR, arg1, error_mark_node),
2529 fnname, arglist, NULL_TREE, flags);
2530 if (rval == error_mark_node)
2531 /* User might declare fancy operator new, but invoke it
2532 like standard one. */
2535 TREE_TYPE (rval) = arg1;
2539 case VEC_DELETE_EXPR:
2544 if (flags & LOOKUP_GLOBAL)
2546 arglist = build_scratch_list (NULL_TREE, arg1);
2547 return build_new_function_call
2548 (lookup_function_nonclass (fnname, arglist), arglist);
2551 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2553 arg1 = TREE_TYPE (arg1);
2555 /* This handles the case where we're trying to delete
2560 if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
2562 /* Strip off the pointer and the array. */
2563 arg1 = TREE_TYPE (TREE_TYPE (arg1));
2565 while (TREE_CODE (arg1) == ARRAY_TYPE)
2566 arg1 = (TREE_TYPE (arg1));
2568 arg1 = build_pointer_type (arg1);
2572 rval = build_method_call
2573 (build_indirect_ref (build1 (NOP_EXPR, arg1,
2576 fnname, arglist, NULL_TREE, flags);
2578 /* This can happen when operator delete is protected. */
2579 my_friendly_assert (rval != error_mark_node, 250);
2580 TREE_TYPE (rval) = void_type_node;
2586 return build_object_call (arg1, arg2);
2592 /* The comma operator can have void args. */
2593 if (TREE_CODE (arg1) == OFFSET_REF)
2594 arg1 = resolve_offset_ref (arg1);
2595 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
2596 arg2 = resolve_offset_ref (arg2);
2597 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
2598 arg3 = resolve_offset_ref (arg3);
2600 if (code == COND_EXPR)
2602 if (arg2 == NULL_TREE
2603 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
2604 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
2605 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
2606 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
2609 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
2610 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
2613 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2614 arg2 = integer_zero_node;
2617 arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
2618 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
2620 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2622 arglist = build_scratch_list (NULL_TREE, arg1);
2624 fns = lookup_function_nonclass (fnname, arglist);
2626 if (fns && TREE_CODE (fns) == TREE_LIST)
2627 fns = TREE_VALUE (fns);
2628 for (; fns; fns = OVL_NEXT (fns))
2630 tree fn = OVL_CURRENT (fns);
2631 if (TREE_CODE (fn) == TEMPLATE_DECL)
2633 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2635 = add_template_candidate (candidates, fn, NULL_TREE,
2636 arglist, TREE_TYPE (fnname),
2637 flags, DEDUCE_CALL);
2640 candidates = add_function_candidate (candidates, fn, arglist, flags);
2643 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
2645 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
2646 if (fns == error_mark_node)
2654 tree basetype = TREE_PURPOSE (fns);
2655 mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
2656 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2658 tree fn = OVL_CURRENT (fns);
2661 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
2662 this_arglist = mem_arglist;
2664 this_arglist = arglist;
2666 if (TREE_CODE (fn) == TEMPLATE_DECL)
2668 /* A member template. */
2669 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2671 = add_template_candidate (candidates, fn, NULL_TREE,
2672 this_arglist, TREE_TYPE (fnname),
2673 flags, DEDUCE_CALL);
2676 candidates = add_function_candidate
2677 (candidates, fn, this_arglist, flags);
2680 candidates->basetype_path = basetype;
2687 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
2688 to know about two args; a builtin candidate will always have a first
2689 parameter of type bool. We'll handle that in
2690 build_builtin_candidate. */
2691 if (code == COND_EXPR)
2701 args[2] = NULL_TREE;
2704 candidates = add_builtin_candidates
2705 (candidates, code, code2, fnname, args, flags);
2708 if (! any_viable (candidates))
2712 case POSTINCREMENT_EXPR:
2713 case POSTDECREMENT_EXPR:
2714 /* Look for an `operator++ (int)'. If they didn't have
2715 one, then we fall back to the old way of doing things. */
2716 if (flags & LOOKUP_COMPLAIN)
2717 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
2718 fnname, opname_tab [code]);
2719 if (code == POSTINCREMENT_EXPR)
2720 code = PREINCREMENT_EXPR;
2722 code = PREDECREMENT_EXPR;
2723 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
2725 /* The caller will deal with these. */
2734 if (flags & LOOKUP_COMPLAIN)
2736 op_error (code, code2, arg1, arg2, arg3, "no match");
2737 print_z_candidates (candidates);
2739 return error_mark_node;
2741 candidates = splice_viable (candidates);
2742 cand = tourney (candidates);
2746 if (flags & LOOKUP_COMPLAIN)
2748 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
2749 print_z_candidates (candidates);
2751 return error_mark_node;
2754 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
2756 extern int warn_synth;
2758 && fnname == ansi_opname[MODIFY_EXPR]
2759 && DECL_ARTIFICIAL (cand->fn)
2761 && ! candidates->next->next)
2763 cp_warning ("using synthesized `%#D' for copy assignment",
2765 cp_warning_at (" where cfront would use `%#D'",
2767 ? candidates->next->fn
2771 /* Pedantically, normal function declarations are never considered
2772 to refer to template instantiations, so we only do this with
2774 if (flag_guiding_decls && templates && ! cand->template
2775 && ! DECL_INITIAL (cand->fn)
2776 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2777 add_maybe_template (cand->fn, templates);
2779 return build_over_call
2781 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
2782 ? mem_arglist : arglist,
2786 /* Check for comparison of different enum types. */
2795 if (flag_int_enum_equivalence == 0
2796 && TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
2797 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
2798 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
2799 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
2801 cp_warning ("comparison between `%#T' and `%#T'",
2802 TREE_TYPE (arg1), TREE_TYPE (arg2));
2809 /* We need to strip any leading REF_BIND so that bitfields don't cause
2810 errors. This should not remove any important conversions, because
2811 builtins don't apply to class objects directly. */
2812 conv = TREE_VEC_ELT (cand->convs, 0);
2813 if (TREE_CODE (conv) == REF_BIND)
2814 conv = TREE_OPERAND (conv, 0);
2815 arg1 = convert_like (conv, arg1);
2817 arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
2819 arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
2825 return build_modify_expr (arg1, code2, arg2);
2828 return build_indirect_ref (arg1, "unary *");
2833 case TRUNC_DIV_EXPR:
2844 case TRUNC_MOD_EXPR:
2848 case TRUTH_ANDIF_EXPR:
2849 case TRUTH_ORIF_EXPR:
2850 return build_binary_op_nodefault (code, arg1, arg2, code);
2855 case TRUTH_NOT_EXPR:
2856 case PREINCREMENT_EXPR:
2857 case POSTINCREMENT_EXPR:
2858 case PREDECREMENT_EXPR:
2859 case POSTDECREMENT_EXPR:
2862 return build_unary_op (code, arg1, candidates != 0);
2865 return build_array_ref (arg1, arg2);
2868 return build_conditional_expr (arg1, arg2, arg3);
2871 return build_m_component_ref
2872 (build_indirect_ref (arg1, NULL_PTR), arg2);
2874 /* The caller will deal with these. */
2881 my_friendly_abort (367);
2886 /* Build up a call to operator new. This has to be handled differently
2887 from other operators in the way lookup is handled; first members are
2888 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
2889 TYPE is the type to be created. ARGS are any new-placement args.
2890 FLAGS are the usual overloading flags. */
2893 build_op_new_call (code, type, args, flags)
2894 enum tree_code code;
2898 tree fnname = ansi_opname[code];
2900 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
2901 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
2903 tree dummy = build1 (NOP_EXPR, build_pointer_type (type),
2905 dummy = build_indirect_ref (dummy, "new");
2906 return build_method_call (dummy, fnname, args, NULL_TREE, flags);
2909 return build_new_function_call
2910 (lookup_function_nonclass (fnname, args), args);
2913 /* Build a call to operator delete. This has to be handled very specially,
2914 because the restrictions on what signatures match are different from all
2915 other call instances. For a normal delete, only a delete taking (void *)
2916 or (void *, size_t) is accepted. For a placement delete, only an exact
2917 match with the placement new is accepted.
2919 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
2920 ADDR is the pointer to be deleted. For placement delete, it is also
2921 used to determine what the corresponding new looked like.
2922 SIZE is the size of the memory block to be deleted.
2923 FLAGS are the usual overloading flags.
2924 PLACEMENT is the corresponding placement new call, or 0. */
2927 build_op_delete_call (code, addr, size, flags, placement)
2928 enum tree_code code;
2929 tree addr, size, placement;
2932 tree fn, fns, fnname, fntype, argtypes, args, type;
2934 if (addr == error_mark_node)
2935 return error_mark_node;
2937 type = TREE_TYPE (TREE_TYPE (addr));
2938 fnname = ansi_opname[code];
2940 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
2943 If the result of the lookup is ambiguous or inaccessible, or if
2944 the lookup selects a placement deallocation function, the
2945 program is ill-formed.
2947 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
2949 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
2950 if (fns == error_mark_node)
2951 return error_mark_node;
2956 if (fns == NULL_TREE)
2957 fns = lookup_name_nonclass (fnname);
2961 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
2963 /* Extract the function. */
2964 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
2965 /* Then the second parm type. */
2966 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
2968 /* Also the second argument. */
2969 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
2973 /* First try it without the size argument. */
2974 argtypes = void_list_node;
2978 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
2979 fntype = build_function_type (void_type_node, argtypes);
2981 /* Strip const and volatile from addr. */
2982 if (type != TYPE_MAIN_VARIANT (type))
2983 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
2985 /* instantiate_type will always return a plain function; pretend it's
2987 if (TREE_CODE (fns) == FUNCTION_DECL)
2988 fns = scratch_ovl_cons (fns, NULL_TREE);
2990 fn = instantiate_type (fntype, fns, 0);
2992 if (fn != error_mark_node)
2994 if (TREE_CODE (fns) == TREE_LIST)
2995 /* Member functions. */
2996 enforce_access (TREE_PURPOSE (fns), fn);
2997 return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
3000 /* If we are doing placement delete we do nothing if we don't find a
3001 matching op delete. */
3005 /* Normal delete; now try to find a match including the size argument. */
3006 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3007 tree_cons (NULL_TREE, sizetype, void_list_node));
3008 fntype = build_function_type (void_type_node, argtypes);
3010 fn = instantiate_type (fntype, fns, 0);
3012 if (fn != error_mark_node)
3014 if (TREE_CODE (fns) == TREE_LIST)
3015 /* Member functions. */
3016 enforce_access (TREE_PURPOSE (fns), fn);
3017 return build_function_call
3018 (fn, expr_tree_cons (NULL_TREE, addr,
3019 build_expr_list (NULL_TREE, size)));
3022 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3023 destructor, in which case the error should be deferred
3024 until someone actually tries to delete one of these. */
3025 if (flags & LOOKUP_SPECULATIVELY)
3028 cp_error ("no suitable operator delete for `%T'", type);
3029 return error_mark_node;
3032 /* If the current scope isn't allowed to access DECL along
3033 BASETYPE_PATH, give an error. */
3036 enforce_access (basetype_path, decl)
3037 tree basetype_path, decl;
3039 tree access = compute_access (basetype_path, decl);
3041 if (access == access_private_node)
3043 cp_error_at ("`%+#D' is %s", decl,
3044 TREE_PRIVATE (decl) ? "private"
3045 : "from private base class");
3046 error ("within this context");
3048 else if (access == access_protected_node)
3050 cp_error_at ("`%+#D' %s", decl,
3051 TREE_PROTECTED (decl) ? "is protected"
3052 : "has protected accessibility");
3053 error ("within this context");
3057 /* Perform the conversions in CONVS on the expression EXPR. */
3060 convert_like (convs, expr)
3063 if (ICS_BAD_FLAG (convs)
3064 && TREE_CODE (convs) != USER_CONV
3065 && TREE_CODE (convs) != AMBIG_CONV)
3068 for (; t; t = TREE_OPERAND (t, 0))
3070 if (TREE_CODE (t) == USER_CONV)
3072 expr = convert_like (t, expr);
3075 else if (TREE_CODE (t) == AMBIG_CONV)
3076 return convert_like (t, expr);
3077 else if (TREE_CODE (t) == IDENTITY_CONV)
3080 return convert_for_initialization
3081 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3082 "conversion", NULL_TREE, 0);
3085 switch (TREE_CODE (convs))
3089 struct z_candidate *cand
3090 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3094 if (DECL_CONSTRUCTOR_P (fn))
3096 tree t = build_int_2 (0, 0);
3097 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3099 args = build_scratch_list (NULL_TREE, expr);
3100 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3101 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3102 args = scratch_tree_cons (NULL_TREE, t, args);
3105 args = build_this (expr);
3106 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3108 /* If this is a constructor or a function returning an aggr type,
3109 we need to build up a TARGET_EXPR. */
3110 if (DECL_CONSTRUCTOR_P (fn))
3111 expr = build_cplus_new (TREE_TYPE (convs), expr);
3116 if (type_unknown_p (expr))
3117 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3118 if (TREE_READONLY_DECL_P (expr))
3119 expr = decl_constant_value (expr);
3122 /* Call build_user_type_conversion again for the error. */
3123 return build_user_type_conversion
3124 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3130 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3131 if (expr == error_mark_node)
3132 return error_mark_node;
3134 switch (TREE_CODE (convs))
3137 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3139 /* else fall through */
3142 tree cvt_expr = build_user_type_conversion
3143 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3146 /* This can occur if, for example, the EXPR has incomplete
3147 type. We can't check for that before attempting the
3148 conversion because the type might be an incomplete
3149 array type, which is OK if some constructor for the
3150 destination type takes a pointer argument. */
3151 if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
3153 if (comptypes (TREE_TYPE (expr), TREE_TYPE (convs), 1))
3154 incomplete_type_error (expr, TREE_TYPE (expr));
3156 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3157 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3160 cp_error ("could not convert `%E' to `%T'",
3161 expr, TREE_TYPE (convs));
3162 return error_mark_node;
3168 return convert_to_reference
3169 (TREE_TYPE (convs), expr,
3170 CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
3173 return decay_conversion (expr);
3178 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3179 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3182 /* ARG is being passed to a varargs function. Perform any conversions
3183 required. Return the converted value. */
3186 convert_arg_to_ellipsis (arg)
3189 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3190 && (TYPE_PRECISION (TREE_TYPE (arg))
3191 < TYPE_PRECISION (double_type_node)))
3192 /* Convert `float' to `double'. */
3193 arg = cp_convert (double_type_node, arg);
3194 else if (IS_AGGR_TYPE (TREE_TYPE (arg))
3195 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (arg)))
3196 cp_warning ("cannot pass objects of type `%T' through `...'",
3199 /* Convert `short' and `char' to full-size `int'. */
3200 arg = default_conversion (arg);
3205 /* ARG is a default argument expression being passed to a parameter of
3206 the indicated TYPE. Do any required conversions. Return the
3210 convert_default_arg (type, arg)
3213 arg = break_out_target_exprs (arg);
3215 if (TREE_CODE (arg) == CONSTRUCTOR)
3217 arg = digest_init (type, arg, 0);
3218 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3219 "default argument", 0, 0);
3223 /* This could get clobbered by the following call. */
3224 if (TREE_HAS_CONSTRUCTOR (arg))
3225 arg = copy_node (arg);
3227 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3228 "default argument", 0, 0);
3229 #ifdef PROMOTE_PROTOTYPES
3230 if ((TREE_CODE (type) == INTEGER_TYPE
3231 || TREE_CODE (type) == ENUMERAL_TYPE)
3232 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3233 arg = default_conversion (arg);
3241 build_over_call (cand, args, flags)
3242 struct z_candidate *cand;
3247 tree convs = cand->convs;
3248 tree converted_args = NULL_TREE;
3249 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3250 tree conv, arg, val;
3254 /* Give any warnings we noticed during overload resolution. */
3256 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3257 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3259 if (DECL_FUNCTION_MEMBER_P (fn))
3260 enforce_access (cand->basetype_path, fn);
3262 if (args && TREE_CODE (args) != TREE_LIST)
3263 args = build_scratch_list (NULL_TREE, args);
3266 /* The implicit parameters to a constructor are not considered by overload
3267 resolution, and must be of the proper type. */
3268 if (DECL_CONSTRUCTOR_P (fn))
3270 converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3271 arg = TREE_CHAIN (arg);
3272 parm = TREE_CHAIN (parm);
3273 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3275 converted_args = expr_tree_cons
3276 (NULL_TREE, TREE_VALUE (arg), converted_args);
3277 arg = TREE_CHAIN (arg);
3278 parm = TREE_CHAIN (parm);
3281 /* Bypass access control for 'this' parameter. */
3282 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3284 tree parmtype = TREE_VALUE (parm);
3285 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3286 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3288 int dv = (TYPE_VOLATILE (TREE_TYPE (parmtype))
3289 < TYPE_VOLATILE (TREE_TYPE (argtype)));
3290 int dc = (TYPE_READONLY (TREE_TYPE (parmtype))
3291 < TYPE_READONLY (TREE_TYPE (argtype)));
3292 char *p = (dv && dc ? "const and volatile"
3293 : dc ? "const" : dv ? "volatile" : "");
3295 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards %s",
3296 TREE_TYPE (argtype), fn, p);
3298 converted_args = expr_tree_cons
3299 (NULL_TREE, convert_force (TREE_VALUE (parm), TREE_VALUE (arg), CONV_C_CAST),
3301 parm = TREE_CHAIN (parm);
3302 arg = TREE_CHAIN (arg);
3308 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3310 tree type = TREE_VALUE (parm);
3312 conv = TREE_VEC_ELT (convs, i);
3313 if (ICS_BAD_FLAG (conv))
3316 val = TREE_VALUE (arg);
3318 for (; t; t = TREE_OPERAND (t, 0))
3320 if (TREE_CODE (t) == USER_CONV
3321 || TREE_CODE (t) == AMBIG_CONV)
3323 val = convert_like (t, val);
3326 else if (TREE_CODE (t) == IDENTITY_CONV)
3329 val = convert_for_initialization
3330 (NULL_TREE, type, val, LOOKUP_NORMAL,
3331 "argument passing", fn, i - is_method);
3335 /* Issue warnings about peculiar, but legal, uses of NULL. */
3336 if (ARITHMETIC_TYPE_P (TREE_VALUE (parm))
3337 && TREE_VALUE (arg) == null_node)
3338 cp_warning ("converting NULL to non-pointer type");
3340 val = convert_like (conv, TREE_VALUE (arg));
3343 #ifdef PROMOTE_PROTOTYPES
3344 if ((TREE_CODE (type) == INTEGER_TYPE
3345 || TREE_CODE (type) == ENUMERAL_TYPE)
3346 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3347 val = default_conversion (val);
3349 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3352 /* Default arguments */
3353 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3355 tree arg = TREE_PURPOSE (parm);
3357 if (DECL_TEMPLATE_INFO (fn))
3359 /* This came from a template. Instantiate the default arg here,
3360 not in tsubst. In the case of something like:
3368 we must be careful to do name lookup in the scope of
3369 S<T>, rather than in the current class. */
3370 if (DECL_CLASS_SCOPE_P (fn))
3371 pushclass (DECL_REAL_CONTEXT (fn), 2);
3373 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), NULL_TREE);
3375 if (DECL_CLASS_SCOPE_P (fn))
3378 converted_args = expr_tree_cons
3379 (NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg),
3384 for (; arg; arg = TREE_CHAIN (arg))
3386 = expr_tree_cons (NULL_TREE,
3387 convert_arg_to_ellipsis (TREE_VALUE (arg)),
3390 converted_args = nreverse (converted_args);
3392 /* Avoid actually calling copy constructors and copy assignment operators,
3394 if (DECL_CONSTRUCTOR_P (fn)
3395 && TREE_VEC_LENGTH (convs) == 1
3396 && copy_args_p (fn))
3399 arg = TREE_CHAIN (converted_args);
3400 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3401 arg = TREE_CHAIN (arg);
3402 arg = TREE_VALUE (arg);
3404 /* Pull out the real argument, disregarding const-correctness. */
3406 while (TREE_CODE (targ) == NOP_EXPR
3407 || TREE_CODE (targ) == NON_LVALUE_EXPR
3408 || TREE_CODE (targ) == CONVERT_EXPR)
3409 targ = TREE_OPERAND (targ, 0);
3410 if (TREE_CODE (targ) == ADDR_EXPR)
3412 targ = TREE_OPERAND (targ, 0);
3413 if (! comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3414 TYPE_MAIN_VARIANT (TREE_TYPE (targ)), 1))
3423 arg = build_indirect_ref (arg, 0);
3425 /* [class.copy]: the copy constructor is implicitly defined even if
3426 the implementation elided its use. */
3427 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3430 /* If we're creating a temp and we already have one, don't create a
3431 new one. If we're not creating a temp but we get one, use
3432 INIT_EXPR to collapse the temp into our target. Otherwise, if the
3433 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3434 temp or an INIT_EXPR otherwise. */
3435 if (integer_zerop (TREE_VALUE (args)))
3437 if (! real_lvalue_p (arg))
3439 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3441 val = build (VAR_DECL, DECL_CONTEXT (fn));
3442 layout_decl (val, 0);
3443 val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
3444 TREE_SIDE_EFFECTS (val) = 1;
3448 else if (! real_lvalue_p (arg)
3449 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3451 tree to = stabilize_reference
3452 (build_indirect_ref (TREE_VALUE (args), 0));
3454 /* Don't copy the padding byte; it might not have been allocated
3455 if to is a base subobject. */
3456 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3457 return build_unary_op
3458 (ADDR_EXPR, build (COMPOUND_EXPR, TREE_TYPE (to),
3459 cp_convert (void_type_node, arg), to),
3462 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
3463 TREE_SIDE_EFFECTS (val) = 1;
3464 return build_unary_op (ADDR_EXPR, val, 0);
3467 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
3469 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
3471 tree to = stabilize_reference
3472 (build_indirect_ref (TREE_VALUE (converted_args), 0));
3474 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
3476 /* Don't copy the padding byte; it might not have been allocated
3477 if to is a base subobject. */
3478 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3479 return build (COMPOUND_EXPR, TREE_TYPE (to),
3480 cp_convert (void_type_node, arg), to);
3482 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
3483 TREE_SIDE_EFFECTS (val) = 1;
3489 if (DECL_CLASS_SCOPE_P (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
3490 return build_signature_method_call (fn, converted_args);
3491 else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3493 tree t, *p = &TREE_VALUE (converted_args);
3494 tree binfo = get_binfo
3495 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
3496 *p = convert_pointer_to_real (binfo, *p);
3497 if (TREE_SIDE_EFFECTS (*p))
3498 *p = save_expr (*p);
3499 t = build_pointer_type (TREE_TYPE (fn));
3500 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
3503 else if (DECL_INLINE (fn))
3504 fn = inline_conversion (fn);
3506 fn = build_addr_func (fn);
3508 /* Recognize certain built-in functions so we can make tree-codes
3509 other than CALL_EXPR. We do this when it enables fold-const.c
3510 to do something useful. */
3512 if (TREE_CODE (fn) == ADDR_EXPR
3513 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3514 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
3515 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
3520 if (converted_args == 0)
3521 return integer_zero_node;
3522 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
3527 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3528 if (TREE_TYPE (fn) == void_type_node)
3530 fn = require_complete_type (fn);
3531 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
3532 fn = build_cplus_new (TREE_TYPE (fn), fn);
3533 return convert_from_reference (fn);
3537 build_new_method_call (instance, name, args, basetype_path, flags)
3538 tree instance, name, args, basetype_path;
3541 struct z_candidate *candidates = 0, *cand;
3542 tree explicit_targs = NULL_TREE;
3543 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
3545 tree user_args = args;
3546 tree templates = NULL_TREE;
3547 int template_only = 0;
3549 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3551 explicit_targs = TREE_OPERAND (name, 1);
3552 name = TREE_OPERAND (name, 0);
3553 if (TREE_CODE (name) == TEMPLATE_DECL)
3554 name = DECL_NAME (name);
3558 /* If there is an extra argument for controlling virtual bases,
3559 remove it for error reporting. */
3560 if (flags & LOOKUP_HAS_IN_CHARGE)
3561 user_args = TREE_CHAIN (args);
3563 args = resolve_args (args);
3565 if (args == error_mark_node)
3566 return error_mark_node;
3568 if (instance == NULL_TREE)
3569 basetype = BINFO_TYPE (basetype_path);
3572 if (TREE_CODE (instance) == OFFSET_REF)
3573 instance = resolve_offset_ref (instance);
3574 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3575 instance = convert_from_reference (instance);
3576 basetype = TREE_TYPE (instance);
3578 /* XXX this should be handled before we get here. */
3579 if (! IS_AGGR_TYPE (basetype)
3580 && ! (TYPE_LANG_SPECIFIC (basetype)
3581 && (IS_SIGNATURE_POINTER (basetype)
3582 || IS_SIGNATURE_REFERENCE (basetype))))
3584 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
3585 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
3586 name, instance, basetype);
3588 return error_mark_node;
3591 /* If `instance' is a signature pointer/reference and `name' is
3592 not a constructor, we are calling a signature member function.
3593 In that case set the `basetype' to the signature type. */
3594 if ((IS_SIGNATURE_POINTER (basetype)
3595 || IS_SIGNATURE_REFERENCE (basetype))
3596 && TYPE_IDENTIFIER (basetype) != name)
3597 basetype = SIGNATURE_TYPE (basetype);
3600 if (basetype_path == NULL_TREE)
3601 basetype_path = TYPE_BINFO (basetype);
3605 instance_ptr = build_this (instance);
3607 if (! template_only)
3609 /* XXX this should be handled before we get here. */
3610 fns = build_field_call (basetype_path, instance_ptr, name, args);
3617 instance_ptr = build_int_2 (0, 0);
3618 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
3622 = (name == ctor_identifier ? constructor_name (basetype) : name);
3624 fns = lookup_fnfields (basetype_path, name, 1);
3626 if (fns == error_mark_node)
3627 return error_mark_node;
3630 tree fn = TREE_VALUE (fns);
3631 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3632 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3634 flags |= LOOKUP_HAS_IN_CHARGE;
3635 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3637 mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
3638 for (; fn; fn = OVL_NEXT (fn))
3640 tree t = OVL_CURRENT (fn);
3643 /* We can end up here for copy-init of same or base class. */
3644 if (name == ctor_identifier
3645 && (flags & LOOKUP_ONLYCONVERTING)
3646 && DECL_NONCONVERTING_P (t))
3648 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
3649 this_arglist = mem_args;
3651 this_arglist = args;
3653 if (TREE_CODE (t) == TEMPLATE_DECL)
3655 /* A member template. */
3656 templates = scratch_tree_cons (NULL_TREE, t, templates);
3658 add_template_candidate (candidates, t, explicit_targs,
3660 TREE_TYPE (name), flags, DEDUCE_CALL);
3662 else if (! template_only)
3663 candidates = add_function_candidate (candidates, t,
3664 this_arglist, flags);
3667 candidates->basetype_path = TREE_PURPOSE (fns);
3671 if (! any_viable (candidates))
3673 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
3674 if (flags & LOOKUP_SPECULATIVELY)
3676 cp_error ("no matching function for call to `%T::%D (%A)%V'", basetype,
3677 pretty_name, user_args, TREE_TYPE (TREE_TYPE (instance_ptr)));
3678 print_z_candidates (candidates);
3679 return error_mark_node;
3681 candidates = splice_viable (candidates);
3682 cand = tourney (candidates);
3686 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
3688 print_z_candidates (candidates);
3689 return error_mark_node;
3692 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
3693 && instance == current_class_ref
3694 && DECL_CONSTRUCTOR_P (current_function_decl)
3695 && ! (flags & LOOKUP_NONVIRTUAL)
3696 && value_member (cand->fn, get_abstract_virtuals (basetype)))
3697 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
3698 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3699 && TREE_CODE (instance_ptr) == NOP_EXPR
3700 && TREE_OPERAND (instance_ptr, 0) == error_mark_node)
3701 cp_error ("cannot call member function `%D' without object", cand->fn);
3703 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
3704 && ((instance == current_class_ref && (dtor_label || ctor_label))
3705 || resolves_to_fixed_type_p (instance, 0)))
3706 flags |= LOOKUP_NONVIRTUAL;
3708 /* Pedantically, normal function declarations are never considered
3709 to refer to template instantiations, so we only do this with
3711 if (flag_guiding_decls && templates && ! cand->template
3712 && ! DECL_INITIAL (cand->fn))
3713 add_maybe_template (cand->fn, templates);
3715 return build_over_call
3717 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
3721 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
3722 subsequence of ICS2. */
3725 is_subseq (ics1, ics2)
3728 /* We can assume that a conversion of the same code
3729 between the same types indicates a subsequence since we only get
3730 here if the types we are converting from are the same. */
3732 while (TREE_CODE (ics1) == RVALUE_CONV
3733 || TREE_CODE (ics1) == LVALUE_CONV)
3734 ics1 = TREE_OPERAND (ics1, 0);
3738 while (TREE_CODE (ics2) == RVALUE_CONV
3739 || TREE_CODE (ics2) == LVALUE_CONV)
3740 ics2 = TREE_OPERAND (ics2, 0);
3742 if (TREE_CODE (ics2) == USER_CONV
3743 || TREE_CODE (ics2) == AMBIG_CONV
3744 || TREE_CODE (ics2) == IDENTITY_CONV)
3745 /* At this point, ICS1 cannot be a proper subsequence of
3746 ICS2. We can get a USER_CONV when we are comparing the
3747 second standard conversion sequence of two user conversion
3751 ics2 = TREE_OPERAND (ics2, 0);
3753 if (TREE_CODE (ics2) == TREE_CODE (ics1)
3754 && comptypes (TREE_TYPE (ics2), TREE_TYPE (ics1), 1)
3755 && comptypes (TREE_TYPE (TREE_OPERAND (ics2, 0)),
3756 TREE_TYPE (TREE_OPERAND (ics1, 0)), 1))
3761 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
3762 be any _TYPE nodes. */
3765 is_properly_derived_from (derived, base)
3769 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
3770 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
3773 /* We only allow proper derivation here. The DERIVED_FROM_P macro
3774 considers every class derived from itself. */
3775 return (!comptypes (TYPE_MAIN_VARIANT (derived),
3776 TYPE_MAIN_VARIANT (base), 1)
3777 && DERIVED_FROM_P (base, derived));
3780 /* We build the ICS for an implicit object parameter as a pointer
3781 conversion sequence. However, such a sequence should be compared
3782 as if it were a reference conversion sequence. If ICS is the
3783 implicit conversion sequence for an implicit object parameter,
3784 modify it accordingly. */
3787 maybe_handle_implicit_object (ics)
3790 if (ICS_THIS_FLAG (*ics))
3792 /* [over.match.funcs]
3794 For non-static member functions, the type of the
3795 implicit object parameter is "reference to cv X"
3796 where X is the class of which the function is a
3797 member and cv is the cv-qualification on the member
3798 function declaration. */
3800 if (TREE_CODE (t) == QUAL_CONV)
3801 t = TREE_OPERAND (t, 0);
3802 if (TREE_CODE (t) == PTR_CONV)
3803 t = TREE_OPERAND (t, 0);
3804 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3805 t = build_conv (REF_BIND,
3806 build_reference_type (TREE_TYPE (TREE_TYPE (*ics))),
3808 ICS_STD_RANK (t) = ICS_STD_RANK (*ics);
3813 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
3814 to the type the reference originally referred to, and return 1.
3815 Otherwise, return 0. */
3818 maybe_handle_ref_bind (ics, target_type)
3822 if (TREE_CODE (*ics) == REF_BIND)
3826 When a parameter of reference type binds directly
3827 (_dcl.init.ref_) to an argument expression, the implicit
3828 conversion sequence is the identity conversion, unless the
3829 argument expression has a type that is a derived class of the
3830 parameter type, in which case the implicit conversion
3831 sequence is a derived-to-base Conversion.
3833 If the parameter binds directly to the result of applying a
3834 conversion function to the argument expression, the implicit
3835 conversion sequence is a user-defined conversion sequence
3836 (_over.ics.user_), with the second standard conversion
3837 sequence either an identity conversion or, if the conversion
3838 function returns an entity of a type that is a derived class
3839 of the parameter type, a derived-to-base Conversion.
3841 When a parameter of reference type is not bound directly to
3842 an argument expression, the conversion sequence is the one
3843 required to convert the argument expression to the underlying
3844 type of the reference according to _over.best.ics_.
3845 Conceptually, this conversion sequence corresponds to
3846 copy-initializing a temporary of the underlying type with the
3847 argument expression. Any difference in top-level
3848 cv-qualification is subsumed by the initialization itself and
3849 does not constitute a conversion. */
3851 tree old_ics = *ics;
3853 *target_type = TREE_TYPE (TREE_TYPE (*ics));
3854 *ics = TREE_OPERAND (*ics, 0);
3855 if (TREE_CODE (*ics) == IDENTITY_CONV
3856 && is_properly_derived_from (TREE_TYPE (*ics), *target_type))
3857 *ics = build_conv (BASE_CONV, *target_type, *ics);
3858 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
3859 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
3867 /* Compare two implicit conversion sequences according to the rules set out in
3868 [over.ics.rank]. Return values:
3870 1: ics1 is better than ics2
3871 -1: ics2 is better than ics1
3872 0: ics1 and ics2 are indistinguishable */
3875 compare_ics (ics1, ics2)
3882 tree deref_from_type1 = NULL_TREE;
3883 tree deref_from_type2;
3884 tree deref_to_type1;
3885 tree deref_to_type2;
3887 /* REF_BINDING is non-zero if the result of the conversion sequence
3888 is a reference type. In that case TARGET_TYPE is the
3889 type referred to by the reference. */
3895 /* Handle implicit object parameters. */
3896 maybe_handle_implicit_object (&ics1);
3897 maybe_handle_implicit_object (&ics2);
3899 /* Handle reference parameters. */
3900 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
3901 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
3905 When comparing the basic forms of implicit conversion sequences (as
3906 defined in _over.best.ics_)
3908 --a standard conversion sequence (_over.ics.scs_) is a better
3909 conversion sequence than a user-defined conversion sequence
3910 or an ellipsis conversion sequence, and
3912 --a user-defined conversion sequence (_over.ics.user_) is a
3913 better conversion sequence than an ellipsis conversion sequence
3914 (_over.ics.ellipsis_). */
3915 if (ICS_RANK (ics1) > ICS_RANK (ics2))
3917 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
3920 if (ICS_RANK (ics1) == BAD_RANK)
3922 /* Both ICS are bad. We try to make a decision based on what
3923 would have happenned if they'd been good. */
3924 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
3925 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3927 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
3928 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3931 /* We couldn't make up our minds; try to figure it out below. */
3934 if (ICS_ELLIPSIS_FLAG (ics1))
3935 /* Both conversions are ellipsis conversions. */
3938 /* User-defined conversion sequence U1 is a better conversion sequence
3939 than another user-defined conversion sequence U2 if they contain the
3940 same user-defined conversion operator or constructor and if the sec-
3941 ond standard conversion sequence of U1 is better than the second
3942 standard conversion sequence of U2. */
3944 if (ICS_USER_FLAG (ics1))
3948 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
3949 if (TREE_CODE (t1) == AMBIG_CONV)
3951 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
3952 if (TREE_CODE (t2) == AMBIG_CONV)
3955 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
3958 /* We can just fall through here, after setting up
3959 FROM_TYPE1 and FROM_TYPE2. */
3960 from_type1 = TREE_TYPE (t1);
3961 from_type2 = TREE_TYPE (t2);
3965 /* We're dealing with two standard conversion sequences.
3969 Standard conversion sequence S1 is a better conversion
3970 sequence than standard conversion sequence S2 if
3972 --S1 is a proper subsequence of S2 (comparing the conversion
3973 sequences in the canonical form defined by _over.ics.scs_,
3974 excluding any Lvalue Transformation; the identity
3975 conversion sequence is considered to be a subsequence of
3976 any non-identity conversion sequence */
3979 while (TREE_CODE (from_type1) != IDENTITY_CONV)
3980 from_type1 = TREE_OPERAND (from_type1, 0);
3981 from_type1 = TREE_TYPE (from_type1);
3984 while (TREE_CODE (from_type2) != IDENTITY_CONV)
3985 from_type2 = TREE_OPERAND (from_type2, 0);
3986 from_type2 = TREE_TYPE (from_type2);
3989 if (comptypes (from_type1, from_type2, 1))
3991 if (is_subseq (ics1, ics2))
3993 if (is_subseq (ics2, ics1))
3996 /* Otherwise, one sequence cannot be a subsequence of the other; they
3997 don't start with the same type. This can happen when comparing the
3998 second standard conversion sequence in two user-defined conversion
4005 --the rank of S1 is better than the rank of S2 (by the rules
4008 Standard conversion sequences are ordered by their ranks: an Exact
4009 Match is a better conversion than a Promotion, which is a better
4010 conversion than a Conversion.
4012 Two conversion sequences with the same rank are indistinguishable
4013 unless one of the following rules applies:
4015 --A conversion that is not a conversion of a pointer, or pointer
4016 to member, to bool is better than another conversion that is such
4019 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4020 so that we do not have to check it explicitly. */
4021 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4023 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4026 to_type1 = TREE_TYPE (ics1);
4027 to_type2 = TREE_TYPE (ics2);
4029 if (TYPE_PTR_P (from_type1)
4030 && TYPE_PTR_P (from_type2)
4031 && TYPE_PTR_P (to_type1)
4032 && TYPE_PTR_P (to_type2))
4034 deref_from_type1 = TREE_TYPE (from_type1);
4035 deref_from_type2 = TREE_TYPE (from_type2);
4036 deref_to_type1 = TREE_TYPE (to_type1);
4037 deref_to_type2 = TREE_TYPE (to_type2);
4039 /* The rules for pointers to members A::* are just like the rules
4040 for pointers A*, except opposite: if B is derived from A then
4041 A::* converts to B::*, not vice versa. For that reason, we
4042 switch the from_ and to_ variables here. */
4043 else if (TYPE_PTRMEM_P (from_type1)
4044 && TYPE_PTRMEM_P (from_type2)
4045 && TYPE_PTRMEM_P (to_type1)
4046 && TYPE_PTRMEM_P (to_type2))
4048 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4049 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4050 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4051 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4053 else if (TYPE_PTRMEMFUNC_P (from_type1)
4054 && TYPE_PTRMEMFUNC_P (from_type2)
4055 && TYPE_PTRMEMFUNC_P (to_type1)
4056 && TYPE_PTRMEMFUNC_P (to_type2))
4058 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4059 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4060 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4061 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4064 if (deref_from_type1 != NULL_TREE
4065 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4066 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4068 /* This was one of the pointer or pointer-like conversions.
4072 --If class B is derived directly or indirectly from class A,
4073 conversion of B* to A* is better than conversion of B* to
4074 void*, and conversion of A* to void* is better than
4075 conversion of B* to void*. */
4076 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4077 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4079 if (is_properly_derived_from (deref_from_type1,
4082 else if (is_properly_derived_from (deref_from_type2,
4086 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4087 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4089 if (comptypes (deref_from_type1, deref_from_type2, 1))
4091 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4093 if (is_properly_derived_from (deref_from_type1,
4097 /* We know that DEREF_TO_TYPE1 is `void' here. */
4098 else if (is_properly_derived_from (deref_from_type1,
4103 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4104 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4108 --If class B is derived directly or indirectly from class A
4109 and class C is derived directly or indirectly from B,
4111 --conversion of C* to B* is better than conversion of C* to
4114 --conversion of B* to A* is better than conversion of C* to
4116 if (comptypes (deref_from_type1, deref_from_type2, 1))
4118 if (is_properly_derived_from (deref_to_type1,
4121 else if (is_properly_derived_from (deref_to_type2,
4125 else if (comptypes (deref_to_type1, deref_to_type2, 1))
4127 if (is_properly_derived_from (deref_from_type2,
4130 else if (is_properly_derived_from (deref_from_type1,
4136 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4137 && comptypes (from_type1, from_type2, 1))
4141 --binding of an expression of type C to a reference of type
4142 B& is better than binding an expression of type C to a
4143 reference of type A&
4145 --conversion of C to B is better than conversion of C to A, */
4146 if (is_properly_derived_from (from_type1, to_type1)
4147 && is_properly_derived_from (from_type1, to_type2))
4149 if (is_properly_derived_from (to_type1, to_type2))
4151 else if (is_properly_derived_from (to_type2, to_type1))
4155 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4156 && comptypes (to_type1, to_type2, 1))
4160 --binding of an expression of type B to a reference of type
4161 A& is better than binding an expression of type C to a
4162 reference of type A&,
4164 --onversion of B to A is better than conversion of C to A */
4165 if (is_properly_derived_from (from_type1, to_type1)
4166 && is_properly_derived_from (from_type2, to_type1))
4168 if (is_properly_derived_from (from_type2, from_type1))
4170 else if (is_properly_derived_from (from_type1, from_type2))
4177 --S1 and S2 differ only in their qualification conversion and yield
4178 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4179 qualification signature of type T1 is a proper subset of the cv-
4180 qualification signature of type T2 */
4181 if (TREE_CODE (ics1) == QUAL_CONV
4182 && TREE_CODE (ics2) == QUAL_CONV
4183 && comptypes (from_type1, from_type2, 1))
4184 return comp_cv_qual_signature (to_type1, to_type2);
4188 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4189 types to which the references refer are the same type except for
4190 top-level cv-qualifiers, and the type to which the reference
4191 initialized by S2 refers is more cv-qualified than the type to
4192 which the reference initialized by S1 refers */
4194 if (ref_binding1 && ref_binding2
4195 && comptypes (TYPE_MAIN_VARIANT (to_type1),
4196 TYPE_MAIN_VARIANT (to_type2), 1))
4197 return comp_cv_qualification (target_type2, target_type1);
4199 /* Neither conversion sequence is better than the other. */
4203 /* The source type for this standard conversion sequence. */
4209 for (;; t = TREE_OPERAND (t, 0))
4211 if (TREE_CODE (t) == USER_CONV
4212 || TREE_CODE (t) == AMBIG_CONV
4213 || TREE_CODE (t) == IDENTITY_CONV)
4214 return TREE_TYPE (t);
4216 my_friendly_abort (1823);
4219 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4220 a pointer to LOSER and re-running joust to produce the warning if WINNER
4221 is actually used. */
4224 add_warning (winner, loser)
4225 struct z_candidate *winner, *loser;
4227 winner->warnings = expr_tree_cons (NULL_PTR,
4228 build_expr_ptr_wrapper (loser),
4232 /* Compare two candidates for overloading as described in
4233 [over.match.best]. Return values:
4235 1: cand1 is better than cand2
4236 -1: cand2 is better than cand1
4237 0: cand1 and cand2 are indistinguishable */
4240 joust (cand1, cand2, warn)
4241 struct z_candidate *cand1, *cand2;
4245 int i, off1 = 0, off2 = 0, len;
4247 /* Candidates that involve bad conversions are always worse than those
4249 if (cand1->viable > cand2->viable)
4251 if (cand1->viable < cand2->viable)
4254 /* a viable function F1
4255 is defined to be a better function than another viable function F2 if
4256 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4257 ICSi(F2), and then */
4259 /* for some argument j, ICSj(F1) is a better conversion sequence than
4262 /* For comparing static and non-static member functions, we ignore the
4263 implicit object parameter of the non-static function. The WP says to
4264 pretend that the static function has an object parm, but that won't
4265 work with operator overloading. */
4266 len = TREE_VEC_LENGTH (cand1->convs);
4267 if (len != TREE_VEC_LENGTH (cand2->convs))
4269 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4270 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4272 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4273 && DECL_STATIC_FUNCTION_P (cand2->fn))
4279 my_friendly_abort (42);
4282 for (i = 0; i < len; ++i)
4284 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4285 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4286 int comp = compare_ics (t1, t2);
4291 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4292 && TREE_CODE (t1) == STD_CONV
4293 && TREE_CODE (t2) == STD_CONV
4294 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4295 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4296 && (TYPE_PRECISION (TREE_TYPE (t1))
4297 == TYPE_PRECISION (TREE_TYPE (t2)))
4298 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4299 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4302 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4304 struct z_candidate *w, *l;
4306 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4307 w = cand1, l = cand2;
4309 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4310 w = cand2, l = cand1;
4314 cp_warning ("passing `%T' chooses `%T' over `%T'",
4315 type, type1, type2);
4316 cp_warning (" in call to `%D'", w->fn);
4322 if (winner && comp != winner)
4331 /* warn about confusing overload resolution for user-defined conversions,
4332 either between a constructor and a conversion op, or between two
4334 if (winner && cand1->second_conv
4335 && ((DECL_CONSTRUCTOR_P (cand1->fn)
4336 != DECL_CONSTRUCTOR_P (cand2->fn))
4337 /* Don't warn if the two conv ops convert to the same type... */
4338 || (! DECL_CONSTRUCTOR_P (cand1->fn)
4339 && ! comptypes (TREE_TYPE (cand1->second_conv),
4340 TREE_TYPE (cand2->second_conv), 1))))
4342 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4345 struct z_candidate *w, *l;
4347 w = cand1, l = cand2;
4349 w = cand2, l = cand1;
4352 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4353 if (! DECL_CONSTRUCTOR_P (w->fn))
4354 source = TREE_TYPE (source);
4355 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4356 cp_warning (" for conversion from `%T' to `%T'",
4357 source, TREE_TYPE (w->second_conv));
4358 cp_warning (" because conversion sequence for the argument is better");
4369 F1 is a non-template function and F2 is a template function */
4371 if (! cand1->template && cand2->template)
4373 else if (cand1->template && ! cand2->template)
4375 else if (cand1->template && cand2->template)
4376 winner = more_specialized
4377 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4381 the context is an initialization by user-defined conversion (see
4382 _dcl.init_ and _over.match.user_) and the standard conversion
4383 sequence from the return type of F1 to the destination type (i.e.,
4384 the type of the entity being initialized) is a better conversion
4385 sequence than the standard conversion sequence from the return type
4386 of F2 to the destination type. */
4388 if (! winner && cand1->second_conv)
4389 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4391 /* If the built-in candidates are the same, arbitrarily pick one. */
4392 if (! winner && cand1->fn == cand2->fn
4393 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4395 for (i = 0; i < len; ++i)
4396 if (! comptypes (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4397 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i)), 1))
4399 if (i == TREE_VEC_LENGTH (cand1->convs))
4402 /* Kludge around broken overloading rules whereby
4403 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4404 that takes references and another that takes values. */
4405 if (cand1->fn == ansi_opname[COND_EXPR])
4407 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4408 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4409 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4410 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4412 if (comptypes (t1, t2, 1))
4414 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4416 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4424 /* Extension: If the worst conversion for one candidate is worse than the
4425 worst conversion for the other, take the first. */
4426 if (! winner && ! pedantic)
4428 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4430 for (i = 0; i < len; ++i)
4432 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4433 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4434 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
4435 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
4447 /* Given a list of candidates for overloading, find the best one, if any.
4448 This algorithm has a worst case of O(2n) (winner is last), and a best
4449 case of O(n/2) (totally ambiguous); much better than a sorting
4452 static struct z_candidate *
4453 tourney (candidates)
4454 struct z_candidate *candidates;
4456 struct z_candidate *champ = candidates, *challenger;
4458 int champ_compared_to_predecessor = 0;
4460 /* Walk through the list once, comparing each current champ to the next
4461 candidate, knocking out a candidate or two with each comparison. */
4463 for (challenger = champ->next; challenger; )
4465 fate = joust (champ, challenger, 0);
4467 challenger = challenger->next;
4472 champ = challenger->next;
4475 champ_compared_to_predecessor = 0;
4480 champ_compared_to_predecessor = 1;
4483 challenger = champ->next;
4487 /* Make sure the champ is better than all the candidates it hasn't yet
4488 been compared to. */
4490 for (challenger = candidates;
4492 && !(champ_compared_to_predecessor && challenger->next == champ);
4493 challenger = challenger->next)
4495 fate = joust (champ, challenger, 0);
4504 can_convert (to, from)
4507 tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
4508 return (t && ! ICS_BAD_FLAG (t));
4512 can_convert_arg (to, from, arg)
4515 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
4516 return (t && ! ICS_BAD_FLAG (t));