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 = TREE_TYPE (arg);
1150 /* An overloaded function does not have an argument type */
1151 if (TREE_CODE (arg) == OVERLOAD)
1152 argtype = unknown_type_node;
1153 argtype = cp_build_type_variant
1154 (argtype, TREE_READONLY (arg), TREE_THIS_VOLATILE (arg));
1156 if (parmnode == void_list_node)
1159 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1162 t = build1 (IDENTITY_CONV, argtype, arg);
1163 ICS_ELLIPSIS_FLAG (t) = 1;
1166 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1167 && ! DECL_CONSTRUCTOR_P (fn))
1168 ICS_THIS_FLAG (t) = 1;
1170 TREE_VEC_ELT (convs, i) = t;
1174 if (ICS_BAD_FLAG (t))
1178 parmnode = TREE_CHAIN (parmnode);
1179 argnode = TREE_CHAIN (argnode);
1185 /* Make sure there are default args for the rest of the parms. */
1186 for (; parmnode && parmnode != void_list_node;
1187 parmnode = TREE_CHAIN (parmnode))
1188 if (! TREE_PURPOSE (parmnode))
1194 return add_candidate (candidates, fn, convs, viable);
1197 /* Create an overload candidate for the conversion function FN which will
1198 be invoked for expression OBJ, producing a pointer-to-function which
1199 will in turn be called with the argument list ARGLIST, and add it to
1200 CANDIDATES. FLAGS is passed on to implicit_conversion. */
1202 static struct z_candidate *
1203 add_conv_candidate (candidates, fn, obj, arglist)
1204 struct z_candidate *candidates;
1205 tree fn, obj, arglist;
1207 tree totype = TREE_TYPE (TREE_TYPE (fn));
1208 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1209 int i, len = list_length (arglist) + 1;
1210 tree convs = make_scratch_vec (len);
1211 tree parmnode = parmlist;
1212 tree argnode = arglist;
1214 int flags = LOOKUP_NORMAL;
1216 for (i = 0; i < len; ++i)
1218 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1219 tree argtype = lvalue_type (arg);
1223 t = implicit_conversion (totype, argtype, arg, flags);
1224 else if (parmnode == void_list_node)
1227 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1230 t = build1 (IDENTITY_CONV, argtype, arg);
1231 ICS_ELLIPSIS_FLAG (t) = 1;
1234 TREE_VEC_ELT (convs, i) = t;
1238 if (ICS_BAD_FLAG (t))
1245 parmnode = TREE_CHAIN (parmnode);
1246 argnode = TREE_CHAIN (argnode);
1252 for (; parmnode && parmnode != void_list_node;
1253 parmnode = TREE_CHAIN (parmnode))
1254 if (! TREE_PURPOSE (parmnode))
1260 return add_candidate (candidates, fn, convs, viable);
1263 static struct z_candidate *
1264 build_builtin_candidate (candidates, fnname, type1, type2,
1265 args, argtypes, flags)
1266 struct z_candidate *candidates;
1267 tree fnname, type1, type2, *args, *argtypes;
1278 convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1280 for (i = 0; i < 2; ++i)
1285 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1289 /* We need something for printing the candidate. */
1290 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1292 else if (ICS_BAD_FLAG (t))
1294 TREE_VEC_ELT (convs, i) = t;
1297 /* For COND_EXPR we rearranged the arguments; undo that now. */
1300 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1301 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1302 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1304 TREE_VEC_ELT (convs, 0) = t;
1309 return add_candidate (candidates, fnname, convs, viable);
1316 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1319 /* Create any builtin operator overload candidates for the operator in
1320 question given the converted operand types TYPE1 and TYPE2. The other
1321 args are passed through from add_builtin_candidates to
1322 build_builtin_candidate. */
1324 static struct z_candidate *
1325 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1326 args, argtypes, flags)
1327 struct z_candidate *candidates;
1328 enum tree_code code, code2;
1329 tree fnname, type1, type2, *args, *argtypes;
1334 case POSTINCREMENT_EXPR:
1335 case POSTDECREMENT_EXPR:
1336 args[1] = integer_zero_node;
1337 type2 = integer_type_node;
1346 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1347 and VQ is either volatile or empty, there exist candidate operator
1348 functions of the form
1349 VQ T& operator++(VQ T&);
1350 T operator++(VQ T&, int);
1351 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1352 type other than bool, and VQ is either volatile or empty, there exist
1353 candidate operator functions of the form
1354 VQ T& operator--(VQ T&);
1355 T operator--(VQ T&, int);
1356 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1357 complete object type, and VQ is either volatile or empty, there exist
1358 candidate operator functions of the form
1359 T*VQ& operator++(T*VQ&);
1360 T*VQ& operator--(T*VQ&);
1361 T* operator++(T*VQ&, int);
1362 T* operator--(T*VQ&, int); */
1364 case POSTDECREMENT_EXPR:
1365 case PREDECREMENT_EXPR:
1366 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1368 case POSTINCREMENT_EXPR:
1369 case PREINCREMENT_EXPR:
1370 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1371 || TYPE_PTROB_P (type1))
1373 type1 = build_reference_type (type1);
1378 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1379 exist candidate operator functions of the form
1383 8 For every function type T, there exist candidate operator functions of
1385 T& operator*(T*); */
1388 if (TREE_CODE (type1) == POINTER_TYPE
1389 && (TYPE_PTROB_P (type1)
1390 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1394 /* 9 For every type T, there exist candidate operator functions of the form
1397 10For every promoted arithmetic type T, there exist candidate operator
1398 functions of the form
1402 case CONVERT_EXPR: /* unary + */
1403 if (TREE_CODE (type1) == POINTER_TYPE
1404 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1407 if (ARITHMETIC_TYPE_P (type1))
1411 /* 11For every promoted integral type T, there exist candidate operator
1412 functions of the form
1416 if (INTEGRAL_TYPE_P (type1))
1420 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1421 is the same type as C2 or is a derived class of C2, T is a complete
1422 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1423 there exist candidate operator functions of the form
1424 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1425 where CV12 is the union of CV1 and CV2. */
1428 if (TREE_CODE (type1) == POINTER_TYPE
1429 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1431 tree c1 = TREE_TYPE (type1);
1432 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1433 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1434 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1436 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1437 && (TYPE_PTRMEMFUNC_P (type2)
1438 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1443 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1444 didate operator functions of the form
1449 bool operator<(L, R);
1450 bool operator>(L, R);
1451 bool operator<=(L, R);
1452 bool operator>=(L, R);
1453 bool operator==(L, R);
1454 bool operator!=(L, R);
1455 where LR is the result of the usual arithmetic conversions between
1458 14For every pair of types T and I, where T is a cv-qualified or cv-
1459 unqualified complete object type and I is a promoted integral type,
1460 there exist candidate operator functions of the form
1461 T* operator+(T*, I);
1462 T& operator[](T*, I);
1463 T* operator-(T*, I);
1464 T* operator+(I, T*);
1465 T& operator[](I, T*);
1467 15For every T, where T is a pointer to complete object type, there exist
1468 candidate operator functions of the form112)
1469 ptrdiff_t operator-(T, T);
1471 16For every pointer type T, there exist candidate operator functions of
1473 bool operator<(T, T);
1474 bool operator>(T, T);
1475 bool operator<=(T, T);
1476 bool operator>=(T, T);
1477 bool operator==(T, T);
1478 bool operator!=(T, T);
1480 17For every pointer to member type T, there exist candidate operator
1481 functions of the form
1482 bool operator==(T, T);
1483 bool operator!=(T, T); */
1486 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1488 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1490 type2 = ptrdiff_type_node;
1494 case TRUNC_DIV_EXPR:
1495 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1501 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1502 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1504 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1505 && null_ptr_cst_p (args[1]))
1510 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1511 && null_ptr_cst_p (args[0]))
1522 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1523 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1525 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1530 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1538 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1541 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1543 type1 = ptrdiff_type_node;
1546 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1548 type2 = ptrdiff_type_node;
1553 /* 18For every pair of promoted integral types L and R, there exist candi-
1554 date operator functions of the form
1561 where LR is the result of the usual arithmetic conversions between
1564 case TRUNC_MOD_EXPR:
1570 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1574 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1575 type, VQ is either volatile or empty, and R is a promoted arithmetic
1576 type, there exist candidate operator functions of the form
1577 VQ L& operator=(VQ L&, R);
1578 VQ L& operator*=(VQ L&, R);
1579 VQ L& operator/=(VQ L&, R);
1580 VQ L& operator+=(VQ L&, R);
1581 VQ L& operator-=(VQ L&, R);
1583 20For every pair T, VQ), where T is any type and VQ is either volatile
1584 or empty, there exist candidate operator functions of the form
1585 T*VQ& operator=(T*VQ&, T*);
1587 21For every pair T, VQ), where T is a pointer to member type and VQ is
1588 either volatile or empty, there exist candidate operator functions of
1590 VQ T& operator=(VQ T&, T);
1592 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1593 unqualified complete object type, VQ is either volatile or empty, and
1594 I is a promoted integral type, there exist candidate operator func-
1596 T*VQ& operator+=(T*VQ&, I);
1597 T*VQ& operator-=(T*VQ&, I);
1599 23For every triple L, VQ, R), where L is an integral or enumeration
1600 type, VQ is either volatile or empty, and R is a promoted integral
1601 type, there exist candidate operator functions of the form
1603 VQ L& operator%=(VQ L&, R);
1604 VQ L& operator<<=(VQ L&, R);
1605 VQ L& operator>>=(VQ L&, R);
1606 VQ L& operator&=(VQ L&, R);
1607 VQ L& operator^=(VQ L&, R);
1608 VQ L& operator|=(VQ L&, R); */
1615 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1617 type2 = ptrdiff_type_node;
1621 case TRUNC_DIV_EXPR:
1622 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1626 case TRUNC_MOD_EXPR:
1632 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1637 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1639 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1640 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1641 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1642 || ((TYPE_PTRMEMFUNC_P (type1)
1643 || TREE_CODE (type1) == POINTER_TYPE)
1644 && null_ptr_cst_p (args[1])))
1652 my_friendly_abort (367);
1654 type1 = build_reference_type (type1);
1658 /* Kludge around broken overloading rules whereby
1659 bool ? const char& : enum is ambiguous
1660 (between int and const char&). */
1661 flags |= LOOKUP_NO_TEMP_BIND;
1663 /* Extension: Support ?: of enumeral type. Hopefully this will not
1664 be an extension for long. */
1665 if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
1667 else if (TREE_CODE (type1) == ENUMERAL_TYPE
1668 || TREE_CODE (type2) == ENUMERAL_TYPE)
1670 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1672 if (TREE_CODE (type1) == TREE_CODE (type2)
1673 && (TREE_CODE (type1) == REFERENCE_TYPE
1674 || TREE_CODE (type1) == POINTER_TYPE
1675 || TYPE_PTRMEMFUNC_P (type1)
1676 || IS_AGGR_TYPE (type1)))
1678 if (TREE_CODE (type1) == REFERENCE_TYPE
1679 || TREE_CODE (type2) == REFERENCE_TYPE)
1681 if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
1682 && null_ptr_cst_p (args[1]))
1683 || IS_AGGR_TYPE (type1))
1688 if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
1689 && null_ptr_cst_p (args[0]))
1690 || IS_AGGR_TYPE (type2))
1698 my_friendly_abort (367);
1701 /* If we're dealing with two pointer types, we need candidates
1702 for both of them. */
1703 if (type2 && type1 != type2
1704 && TREE_CODE (type1) == TREE_CODE (type2)
1705 && (TREE_CODE (type1) == REFERENCE_TYPE
1706 || (TREE_CODE (type1) == POINTER_TYPE
1707 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1708 || TYPE_PTRMEMFUNC_P (type1)
1709 || IS_AGGR_TYPE (type1)))
1711 candidates = build_builtin_candidate
1712 (candidates, fnname, type1, type1, args, argtypes, flags);
1713 return build_builtin_candidate
1714 (candidates, fnname, type2, type2, args, argtypes, flags);
1717 return build_builtin_candidate
1718 (candidates, fnname, type1, type2, args, argtypes, flags);
1722 type_decays_to (type)
1725 if (TREE_CODE (type) == ARRAY_TYPE)
1726 return build_pointer_type (TREE_TYPE (type));
1727 if (TREE_CODE (type) == FUNCTION_TYPE)
1728 return build_pointer_type (type);
1732 /* There are three conditions of builtin candidates:
1734 1) bool-taking candidates. These are the same regardless of the input.
1735 2) pointer-pair taking candidates. These are generated for each type
1736 one of the input types converts to.
1737 3) arithmetic candidates. According to the WP, we should generate
1738 all of these, but I'm trying not to... */
1740 static struct z_candidate *
1741 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1742 struct z_candidate *candidates;
1743 enum tree_code code, code2;
1748 tree type, argtypes[3], types[2];
1750 for (i = 0; i < 3; ++i)
1753 argtypes[i] = lvalue_type (args[i]);
1755 argtypes[i] = NULL_TREE;
1760 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1761 and VQ is either volatile or empty, there exist candidate operator
1762 functions of the form
1763 VQ T& operator++(VQ T&); */
1765 case POSTINCREMENT_EXPR:
1766 case PREINCREMENT_EXPR:
1767 case POSTDECREMENT_EXPR:
1768 case PREDECREMENT_EXPR:
1773 /* 24There also exist candidate operator functions of the form
1774 bool operator!(bool);
1775 bool operator&&(bool, bool);
1776 bool operator||(bool, bool); */
1778 case TRUTH_NOT_EXPR:
1779 return build_builtin_candidate
1780 (candidates, fnname, boolean_type_node,
1781 NULL_TREE, args, argtypes, flags);
1783 case TRUTH_ORIF_EXPR:
1784 case TRUTH_ANDIF_EXPR:
1785 return build_builtin_candidate
1786 (candidates, fnname, boolean_type_node,
1787 boolean_type_node, args, argtypes, flags);
1798 types[0] = types[1] = NULL_TREE;
1800 for (i = 0; i < 2; ++i)
1804 else if (IS_AGGR_TYPE (argtypes[i]))
1806 tree convs = lookup_conversions (argtypes[i]);
1808 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1811 convs = lookup_conversions (argtypes[i]);
1813 if (code == COND_EXPR)
1815 if (real_lvalue_p (args[i]))
1816 types[i] = scratch_tree_cons
1817 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1819 types[i] = scratch_tree_cons
1820 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1826 for (; convs; convs = TREE_CHAIN (convs))
1828 type = TREE_TYPE (TREE_TYPE (TREE_VALUE (convs)));
1831 && (TREE_CODE (type) != REFERENCE_TYPE
1832 || TYPE_READONLY (TREE_TYPE (type))))
1835 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1836 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1838 type = non_reference (type);
1839 if (i != 0 || ! ref1)
1841 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1842 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1843 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1844 if (INTEGRAL_TYPE_P (type))
1845 type = type_promotes_to (type);
1848 if (! value_member (type, types[i]))
1849 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1854 if (code == COND_EXPR && real_lvalue_p (args[i]))
1855 types[i] = scratch_tree_cons
1856 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1857 type = non_reference (argtypes[i]);
1858 if (i != 0 || ! ref1)
1860 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1861 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1862 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1863 if (INTEGRAL_TYPE_P (type))
1864 type = type_promotes_to (type);
1866 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1870 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1873 for (type = types[1]; type; type = TREE_CHAIN (type))
1874 candidates = add_builtin_candidate
1875 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1876 TREE_VALUE (type), args, argtypes, flags);
1878 candidates = add_builtin_candidate
1879 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1880 NULL_TREE, args, argtypes, flags);
1887 /* If TMPL can be successfully instantiated as indicated by
1888 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
1890 TMPL is the template. EXPLICIT_TARGS are any explicit template
1891 arguments. ARGLIST is the arguments provided at the call-site.
1892 The RETURN_TYPE is the desired type for conversion operators. If
1893 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
1894 OBJ is supplied, FLAGS are ignored, and OBJ is as for
1895 add_conv_candidate. */
1897 static struct z_candidate*
1898 add_template_candidate_real (candidates, tmpl, explicit_targs,
1899 arglist, return_type, flags,
1901 struct z_candidate *candidates;
1902 tree tmpl, explicit_targs, arglist, return_type;
1905 unification_kind_t strict;
1907 int ntparms = DECL_NTPARMS (tmpl);
1908 tree targs = make_scratch_vec (ntparms);
1909 struct z_candidate *cand;
1913 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
1914 return_type, strict, NULL_TREE);
1919 fn = instantiate_template (tmpl, targs);
1920 if (fn == error_mark_node)
1923 if (obj != NULL_TREE)
1924 /* Aha, this is a conversion function. */
1925 cand = add_conv_candidate (candidates, fn, obj, arglist);
1927 cand = add_function_candidate (candidates, fn, arglist, flags);
1928 if (DECL_TI_TEMPLATE (fn) != tmpl)
1929 /* This situation can occur if a member template of a template
1930 class is specialized. Then, instantiate_template might return
1931 an instantiation of the specialization, in which case the
1932 DECL_TI_TEMPLATE field will point at the original
1933 specialization. For example:
1935 template <class T> struct S { template <class U> void f(U);
1936 template <> void f(int) {}; };
1940 Here, TMPL will be template <class U> S<double>::f(U).
1941 And, instantiate template will give us the specialization
1942 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
1943 for this will point at template <class T> template <> S<T>::f(int),
1944 so that we can find the definition. For the purposes of
1945 overload resolution, however, we want the original TMPL. */
1946 cand->template = tree_cons (tmpl, targs, NULL_TREE);
1948 cand->template = DECL_TEMPLATE_INFO (fn);
1954 static struct z_candidate *
1955 add_template_candidate (candidates, tmpl, explicit_targs,
1956 arglist, return_type, flags, strict)
1957 struct z_candidate *candidates;
1958 tree tmpl, explicit_targs, arglist, return_type;
1960 unification_kind_t strict;
1963 add_template_candidate_real (candidates, tmpl, explicit_targs,
1964 arglist, return_type, flags,
1969 static struct z_candidate *
1970 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
1971 struct z_candidate *candidates;
1972 tree tmpl, obj, arglist, return_type;
1975 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
1976 return_type, 0, obj, DEDUCE_CONV);
1982 struct z_candidate *cands;
1984 for (; cands; cands = cands->next)
1985 if (pedantic ? cands->viable == 1 : cands->viable)
1990 static struct z_candidate *
1991 splice_viable (cands)
1992 struct z_candidate *cands;
1994 struct z_candidate **p = &cands;
1998 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2011 /* Fix this to work on non-lvalues. */
2012 if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
2013 || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
2016 return build_unary_op (ADDR_EXPR, obj, 0);
2020 print_z_candidates (candidates)
2021 struct z_candidate *candidates;
2023 char *str = "candidates are:";
2024 for (; candidates; candidates = candidates->next)
2026 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2028 if (candidates->fn == ansi_opname [COND_EXPR])
2029 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2030 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2031 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2032 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2033 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2034 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2035 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2036 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2038 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2039 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2042 cp_error_at ("%s %+D%s", str, candidates->fn,
2043 candidates->viable == -1 ? " <near match>" : "");
2048 /* Returns the best overload candidate to perform the requested
2049 conversion. This function is used for three the overloading situations
2050 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2051 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2052 per [dcl.init.ref], so we ignore temporary bindings. */
2054 static struct z_candidate *
2055 build_user_type_conversion_1 (totype, expr, flags)
2059 struct z_candidate *candidates, *cand;
2060 tree fromtype = TREE_TYPE (expr);
2061 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2062 tree args = NULL_TREE;
2063 tree templates = NULL_TREE;
2065 if (IS_AGGR_TYPE (totype))
2066 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2067 if (IS_AGGR_TYPE (fromtype)
2068 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2069 convs = lookup_conversions (fromtype);
2072 flags |= LOOKUP_NO_CONVERSION;
2076 tree t = build_int_2 (0, 0);
2077 TREE_TYPE (t) = build_pointer_type (totype);
2078 args = build_scratch_list (NULL_TREE, expr);
2079 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2080 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2081 args = scratch_tree_cons (NULL_TREE, t, args);
2083 ctors = TREE_VALUE (ctors);
2085 for (; ctors; ctors = OVL_NEXT (ctors))
2087 tree ctor = OVL_CURRENT (ctors);
2088 if (DECL_NONCONVERTING_P (ctor))
2091 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2093 templates = scratch_tree_cons (NULL_TREE, ctor, templates);
2095 add_template_candidate (candidates, ctor,
2096 NULL_TREE, args, NULL_TREE, flags,
2100 candidates = add_function_candidate (candidates, ctor,
2105 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2106 candidates->basetype_path = TYPE_BINFO (totype);
2111 args = build_scratch_list (NULL_TREE, build_this (expr));
2113 for (; convs; convs = TREE_CHAIN (convs))
2115 tree fns = TREE_VALUE (convs);
2116 int convflags = LOOKUP_NO_CONVERSION;
2119 /* If we are called to convert to a reference type, we are trying to
2120 find an lvalue binding, so don't even consider temporaries. If
2121 we don't find an lvalue binding, the caller will try again to
2122 look for a temporary binding. */
2123 if (TREE_CODE (totype) == REFERENCE_TYPE)
2124 convflags |= LOOKUP_NO_TEMP_BIND;
2126 if (TREE_CODE (fns) != TEMPLATE_DECL)
2127 ics = implicit_conversion
2128 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2130 /* We can't compute this yet. */
2131 ics = error_mark_node;
2133 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2134 /* ignore the near match. */;
2136 for (; fns; fns = OVL_NEXT (fns))
2138 tree fn = OVL_CURRENT (fns);
2139 struct z_candidate *old_candidates = candidates;
2141 if (TREE_CODE (fn) == TEMPLATE_DECL)
2143 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2145 add_template_candidate (candidates, fn, NULL_TREE,
2146 args, totype, flags,
2150 candidates = add_function_candidate (candidates, fn,
2153 if (candidates != old_candidates)
2155 if (TREE_CODE (fn) == TEMPLATE_DECL)
2156 ics = implicit_conversion
2157 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2160 candidates->second_conv = ics;
2161 candidates->basetype_path = TREE_PURPOSE (convs);
2163 if (ics == NULL_TREE)
2164 candidates->viable = 0;
2165 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2166 candidates->viable = -1;
2171 if (! any_viable (candidates))
2174 if (flags & LOOKUP_COMPLAIN)
2176 if (candidates && ! candidates->next)
2177 /* say why this one won't work or try to be loose */;
2179 cp_error ("no viable candidates");
2186 candidates = splice_viable (candidates);
2187 cand = tourney (candidates);
2191 if (flags & LOOKUP_COMPLAIN)
2193 cp_error ("conversion from `%T' to `%T' is ambiguous",
2195 print_z_candidates (candidates);
2198 cand = candidates; /* any one will do */
2199 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2200 ICS_USER_FLAG (cand->second_conv) = 1;
2201 ICS_BAD_FLAG (cand->second_conv) = 1;
2206 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2207 p = &(TREE_OPERAND (*p, 0));
2209 /* Pedantically, normal function declarations are never considered
2210 to refer to template instantiations, so we only do this with
2212 if (flag_guiding_decls && templates && ! cand->template
2213 && !DECL_INITIAL (cand->fn)
2214 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2215 add_maybe_template (cand->fn, templates);
2219 (DECL_CONSTRUCTOR_P (cand->fn)
2220 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2221 expr, build_expr_ptr_wrapper (cand));
2222 ICS_USER_FLAG (cand->second_conv) = 1;
2223 if (cand->viable == -1)
2224 ICS_BAD_FLAG (cand->second_conv) = 1;
2230 build_user_type_conversion (totype, expr, flags)
2234 struct z_candidate *cand
2235 = build_user_type_conversion_1 (totype, expr, flags);
2239 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2240 return error_mark_node;
2241 return convert_from_reference (convert_like (cand->second_conv, expr));
2246 /* Do any initial processing on the arguments to a function call. */
2253 for (t = args; t; t = TREE_CHAIN (t))
2255 if (TREE_VALUE (t) == error_mark_node)
2256 return error_mark_node;
2257 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2259 error ("invalid use of void expression");
2260 return error_mark_node;
2262 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2263 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2269 build_new_function_call (fn, args)
2272 struct z_candidate *candidates = 0, *cand;
2273 tree explicit_targs = NULL_TREE;
2274 int template_only = 0;
2276 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2278 explicit_targs = TREE_OPERAND (fn, 1);
2279 fn = TREE_OPERAND (fn, 0);
2283 if (really_overloaded_fn (fn))
2286 tree templates = NULL_TREE;
2288 args = resolve_args (args);
2290 if (args == error_mark_node)
2291 return error_mark_node;
2293 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2295 tree t = OVL_FUNCTION (t1);
2296 if (TREE_CODE (t) == TEMPLATE_DECL)
2298 templates = scratch_tree_cons (NULL_TREE, t, templates);
2299 candidates = add_template_candidate
2300 (candidates, t, explicit_targs, args, NULL_TREE,
2301 LOOKUP_NORMAL, DEDUCE_CALL);
2303 else if (! template_only)
2304 candidates = add_function_candidate
2305 (candidates, t, args, LOOKUP_NORMAL);
2308 if (! any_viable (candidates))
2310 if (candidates && ! candidates->next)
2311 return build_function_call (candidates->fn, args);
2312 cp_error ("no matching function for call to `%D (%A)'",
2313 DECL_NAME (OVL_FUNCTION (fn)), args);
2315 print_z_candidates (candidates);
2316 return error_mark_node;
2318 candidates = splice_viable (candidates);
2319 cand = tourney (candidates);
2323 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2324 DECL_NAME (OVL_FUNCTION (fn)), args);
2325 print_z_candidates (candidates);
2326 return error_mark_node;
2329 /* Pedantically, normal function declarations are never considered
2330 to refer to template instantiations, so we only do this with
2332 if (flag_guiding_decls && templates && ! cand->template
2333 && ! DECL_INITIAL (cand->fn))
2334 add_maybe_template (cand->fn, templates);
2336 return build_over_call (cand, args, LOOKUP_NORMAL);
2339 /* This is not really overloaded. */
2340 fn = OVL_CURRENT (fn);
2342 return build_function_call (fn, args);
2346 build_object_call (obj, args)
2349 struct z_candidate *candidates = 0, *cand;
2350 tree fns, convs, mem_args = NULL_TREE;
2351 tree type = TREE_TYPE (obj);
2352 tree templates = NULL_TREE;
2354 if (TYPE_PTRMEMFUNC_P (type))
2356 /* It's no good looking for an overloaded operator() on a
2357 pointer-to-member-function. */
2358 cp_error ("pointer-to-member function %E cannot be called", obj);
2359 cp_error ("without an object; consider using .* or ->*");
2360 return error_mark_node;
2363 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2364 if (fns == error_mark_node)
2365 return error_mark_node;
2367 args = resolve_args (args);
2369 if (args == error_mark_node)
2370 return error_mark_node;
2374 tree base = TREE_PURPOSE (fns);
2375 mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2377 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2379 tree fn = OVL_CURRENT (fns);
2380 if (TREE_CODE (fn) == TEMPLATE_DECL)
2382 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2384 = add_template_candidate (candidates, fn, NULL_TREE,
2385 mem_args, NULL_TREE,
2386 LOOKUP_NORMAL, DEDUCE_CALL);
2389 candidates = add_function_candidate
2390 (candidates, fn, mem_args, LOOKUP_NORMAL);
2393 candidates->basetype_path = base;
2397 convs = lookup_conversions (type);
2399 for (; convs; convs = TREE_CHAIN (convs))
2401 tree fns = TREE_VALUE (convs);
2402 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2405 if (TREE_CODE (totype) == POINTER_TYPE
2406 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2407 for (; fns; fns = OVL_NEXT (fn))
2409 fn = OVL_CURRENT (fn);
2410 if (TREE_CODE (fn) == TEMPLATE_DECL)
2412 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2413 candidates = add_template_conv_candidate (candidates,
2420 candidates = add_conv_candidate (candidates, fn, obj, args);
2423 candidates->basetype_path = TREE_PURPOSE (convs);
2427 if (! any_viable (candidates))
2429 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2430 print_z_candidates (candidates);
2431 return error_mark_node;
2434 candidates = splice_viable (candidates);
2435 cand = tourney (candidates);
2439 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2440 print_z_candidates (candidates);
2441 return error_mark_node;
2444 if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2445 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2447 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2450 return build_function_call (obj, args);
2454 op_error (code, code2, arg1, arg2, arg3, problem)
2455 enum tree_code code, code2;
2456 tree arg1, arg2, arg3;
2460 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2465 cp_error ("%s for `%T ? %T : %T'", problem,
2466 error_type (arg1), error_type (arg2), error_type (arg3));
2468 case POSTINCREMENT_EXPR:
2469 case POSTDECREMENT_EXPR:
2470 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2473 cp_error ("%s for `%T[%T]'", problem,
2474 error_type (arg1), error_type (arg2));
2478 cp_error ("%s for `%T %s %T'", problem,
2479 error_type (arg1), opname, error_type (arg2));
2481 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2486 build_new_op (code, flags, arg1, arg2, arg3)
2487 enum tree_code code;
2489 tree arg1, arg2, arg3;
2491 struct z_candidate *candidates = 0, *cand;
2492 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
2493 enum tree_code code2 = NOP_EXPR;
2494 tree templates = NULL_TREE;
2497 if (arg1 == error_mark_node
2498 || arg2 == error_mark_node
2499 || arg3 == error_mark_node)
2500 return error_mark_node;
2502 /* This can happen if a template takes all non-type parameters, e.g.
2503 undeclared_template<1, 5, 72>a; */
2504 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
2506 cp_error ("`%D' must be declared before use", arg1);
2507 return error_mark_node;
2510 if (code == MODIFY_EXPR)
2512 code2 = TREE_CODE (arg3);
2514 fnname = ansi_assopname[code2];
2517 fnname = ansi_opname[code];
2526 arglist = scratch_tree_cons (NULL_TREE, arg2, arg3);
2527 if (flags & LOOKUP_GLOBAL)
2528 return build_new_function_call
2529 (lookup_function_nonclass (fnname, arglist), arglist);
2532 rval = build_method_call
2533 (build_indirect_ref (build1 (NOP_EXPR, arg1, error_mark_node),
2535 fnname, arglist, NULL_TREE, flags);
2536 if (rval == error_mark_node)
2537 /* User might declare fancy operator new, but invoke it
2538 like standard one. */
2541 TREE_TYPE (rval) = arg1;
2545 case VEC_DELETE_EXPR:
2550 if (flags & LOOKUP_GLOBAL)
2552 arglist = build_scratch_list (NULL_TREE, arg1);
2553 return build_new_function_call
2554 (lookup_function_nonclass (fnname, arglist), arglist);
2557 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2559 arg1 = TREE_TYPE (arg1);
2561 /* This handles the case where we're trying to delete
2566 if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
2568 /* Strip off the pointer and the array. */
2569 arg1 = TREE_TYPE (TREE_TYPE (arg1));
2571 while (TREE_CODE (arg1) == ARRAY_TYPE)
2572 arg1 = (TREE_TYPE (arg1));
2574 arg1 = build_pointer_type (arg1);
2578 rval = build_method_call
2579 (build_indirect_ref (build1 (NOP_EXPR, arg1,
2582 fnname, arglist, NULL_TREE, flags);
2584 /* This can happen when operator delete is protected. */
2585 my_friendly_assert (rval != error_mark_node, 250);
2586 TREE_TYPE (rval) = void_type_node;
2592 return build_object_call (arg1, arg2);
2598 /* The comma operator can have void args. */
2599 if (TREE_CODE (arg1) == OFFSET_REF)
2600 arg1 = resolve_offset_ref (arg1);
2601 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
2602 arg2 = resolve_offset_ref (arg2);
2603 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
2604 arg3 = resolve_offset_ref (arg3);
2606 if (code == COND_EXPR)
2608 if (arg2 == NULL_TREE
2609 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
2610 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
2611 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
2612 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
2615 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
2616 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
2619 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2620 arg2 = integer_zero_node;
2623 arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
2624 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
2626 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2628 arglist = build_scratch_list (NULL_TREE, arg1);
2630 fns = lookup_function_nonclass (fnname, arglist);
2632 if (fns && TREE_CODE (fns) == TREE_LIST)
2633 fns = TREE_VALUE (fns);
2634 for (; fns; fns = OVL_NEXT (fns))
2636 tree fn = OVL_CURRENT (fns);
2637 if (TREE_CODE (fn) == TEMPLATE_DECL)
2639 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2641 = add_template_candidate (candidates, fn, NULL_TREE,
2642 arglist, TREE_TYPE (fnname),
2643 flags, DEDUCE_CALL);
2646 candidates = add_function_candidate (candidates, fn, arglist, flags);
2649 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
2651 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
2652 if (fns == error_mark_node)
2660 tree basetype = TREE_PURPOSE (fns);
2661 mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
2662 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2664 tree fn = OVL_CURRENT (fns);
2667 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
2668 this_arglist = mem_arglist;
2670 this_arglist = arglist;
2672 if (TREE_CODE (fn) == TEMPLATE_DECL)
2674 /* A member template. */
2675 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2677 = add_template_candidate (candidates, fn, NULL_TREE,
2678 this_arglist, TREE_TYPE (fnname),
2679 flags, DEDUCE_CALL);
2682 candidates = add_function_candidate
2683 (candidates, fn, this_arglist, flags);
2686 candidates->basetype_path = basetype;
2693 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
2694 to know about two args; a builtin candidate will always have a first
2695 parameter of type bool. We'll handle that in
2696 build_builtin_candidate. */
2697 if (code == COND_EXPR)
2707 args[2] = NULL_TREE;
2710 candidates = add_builtin_candidates
2711 (candidates, code, code2, fnname, args, flags);
2714 if (! any_viable (candidates))
2718 case POSTINCREMENT_EXPR:
2719 case POSTDECREMENT_EXPR:
2720 /* Look for an `operator++ (int)'. If they didn't have
2721 one, then we fall back to the old way of doing things. */
2722 if (flags & LOOKUP_COMPLAIN)
2723 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
2724 fnname, opname_tab [code]);
2725 if (code == POSTINCREMENT_EXPR)
2726 code = PREINCREMENT_EXPR;
2728 code = PREDECREMENT_EXPR;
2729 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
2731 /* The caller will deal with these. */
2740 if (flags & LOOKUP_COMPLAIN)
2742 op_error (code, code2, arg1, arg2, arg3, "no match");
2743 print_z_candidates (candidates);
2745 return error_mark_node;
2747 candidates = splice_viable (candidates);
2748 cand = tourney (candidates);
2752 if (flags & LOOKUP_COMPLAIN)
2754 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
2755 print_z_candidates (candidates);
2757 return error_mark_node;
2760 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
2762 extern int warn_synth;
2764 && fnname == ansi_opname[MODIFY_EXPR]
2765 && DECL_ARTIFICIAL (cand->fn)
2767 && ! candidates->next->next)
2769 cp_warning ("using synthesized `%#D' for copy assignment",
2771 cp_warning_at (" where cfront would use `%#D'",
2773 ? candidates->next->fn
2777 /* Pedantically, normal function declarations are never considered
2778 to refer to template instantiations, so we only do this with
2780 if (flag_guiding_decls && templates && ! cand->template
2781 && ! DECL_INITIAL (cand->fn)
2782 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2783 add_maybe_template (cand->fn, templates);
2785 return build_over_call
2787 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
2788 ? mem_arglist : arglist,
2792 /* Check for comparison of different enum types. */
2801 if (flag_int_enum_equivalence == 0
2802 && TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
2803 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
2804 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
2805 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
2807 cp_warning ("comparison between `%#T' and `%#T'",
2808 TREE_TYPE (arg1), TREE_TYPE (arg2));
2815 /* We need to strip any leading REF_BIND so that bitfields don't cause
2816 errors. This should not remove any important conversions, because
2817 builtins don't apply to class objects directly. */
2818 conv = TREE_VEC_ELT (cand->convs, 0);
2819 if (TREE_CODE (conv) == REF_BIND)
2820 conv = TREE_OPERAND (conv, 0);
2821 arg1 = convert_like (conv, arg1);
2823 arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
2825 arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
2831 return build_modify_expr (arg1, code2, arg2);
2834 return build_indirect_ref (arg1, "unary *");
2839 case TRUNC_DIV_EXPR:
2850 case TRUNC_MOD_EXPR:
2854 case TRUTH_ANDIF_EXPR:
2855 case TRUTH_ORIF_EXPR:
2856 return build_binary_op_nodefault (code, arg1, arg2, code);
2861 case TRUTH_NOT_EXPR:
2862 case PREINCREMENT_EXPR:
2863 case POSTINCREMENT_EXPR:
2864 case PREDECREMENT_EXPR:
2865 case POSTDECREMENT_EXPR:
2868 return build_unary_op (code, arg1, candidates != 0);
2871 return build_array_ref (arg1, arg2);
2874 return build_conditional_expr (arg1, arg2, arg3);
2877 return build_m_component_ref
2878 (build_indirect_ref (arg1, NULL_PTR), arg2);
2880 /* The caller will deal with these. */
2887 my_friendly_abort (367);
2892 /* Build up a call to operator new. This has to be handled differently
2893 from other operators in the way lookup is handled; first members are
2894 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
2895 TYPE is the type to be created. ARGS are any new-placement args.
2896 FLAGS are the usual overloading flags. */
2899 build_op_new_call (code, type, args, flags)
2900 enum tree_code code;
2904 tree fnname = ansi_opname[code];
2906 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
2907 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
2909 tree dummy = build1 (NOP_EXPR, build_pointer_type (type),
2911 dummy = build_indirect_ref (dummy, "new");
2912 return build_method_call (dummy, fnname, args, NULL_TREE, flags);
2915 return build_new_function_call
2916 (lookup_function_nonclass (fnname, args), args);
2919 /* Build a call to operator delete. This has to be handled very specially,
2920 because the restrictions on what signatures match are different from all
2921 other call instances. For a normal delete, only a delete taking (void *)
2922 or (void *, size_t) is accepted. For a placement delete, only an exact
2923 match with the placement new is accepted.
2925 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
2926 ADDR is the pointer to be deleted. For placement delete, it is also
2927 used to determine what the corresponding new looked like.
2928 SIZE is the size of the memory block to be deleted.
2929 FLAGS are the usual overloading flags.
2930 PLACEMENT is the corresponding placement new call, or 0. */
2933 build_op_delete_call (code, addr, size, flags, placement)
2934 enum tree_code code;
2935 tree addr, size, placement;
2938 tree fn, fns, fnname, fntype, argtypes, args, type;
2940 if (addr == error_mark_node)
2941 return error_mark_node;
2943 type = TREE_TYPE (TREE_TYPE (addr));
2944 fnname = ansi_opname[code];
2946 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
2949 If the result of the lookup is ambiguous or inaccessible, or if
2950 the lookup selects a placement deallocation function, the
2951 program is ill-formed.
2953 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
2955 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
2956 if (fns == error_mark_node)
2957 return error_mark_node;
2962 if (fns == NULL_TREE)
2963 fns = lookup_name_nonclass (fnname);
2967 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
2969 /* Extract the function. */
2970 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
2971 /* Then the second parm type. */
2972 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
2974 /* Also the second argument. */
2975 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
2979 /* First try it without the size argument. */
2980 argtypes = void_list_node;
2984 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
2985 fntype = build_function_type (void_type_node, argtypes);
2987 /* Strip const and volatile from addr. */
2988 if (type != TYPE_MAIN_VARIANT (type))
2989 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
2991 /* instantiate_type will always return a plain function; pretend it's
2993 if (TREE_CODE (fns) == FUNCTION_DECL)
2994 fns = scratch_ovl_cons (fns, NULL_TREE);
2996 fn = instantiate_type (fntype, fns, 0);
2998 if (fn != error_mark_node)
3000 if (TREE_CODE (fns) == TREE_LIST)
3001 /* Member functions. */
3002 enforce_access (TREE_PURPOSE (fns), fn);
3003 return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
3006 /* If we are doing placement delete we do nothing if we don't find a
3007 matching op delete. */
3011 /* Normal delete; now try to find a match including the size argument. */
3012 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3013 tree_cons (NULL_TREE, sizetype, void_list_node));
3014 fntype = build_function_type (void_type_node, argtypes);
3016 fn = instantiate_type (fntype, fns, 0);
3018 if (fn != error_mark_node)
3020 if (TREE_CODE (fns) == TREE_LIST)
3021 /* Member functions. */
3022 enforce_access (TREE_PURPOSE (fns), fn);
3023 return build_function_call
3024 (fn, expr_tree_cons (NULL_TREE, addr,
3025 build_expr_list (NULL_TREE, size)));
3028 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3029 destructor, in which case the error should be deferred
3030 until someone actually tries to delete one of these. */
3031 if (flags & LOOKUP_SPECULATIVELY)
3034 cp_error ("no suitable operator delete for `%T'", type);
3035 return error_mark_node;
3038 /* If the current scope isn't allowed to access DECL along
3039 BASETYPE_PATH, give an error. */
3042 enforce_access (basetype_path, decl)
3043 tree basetype_path, decl;
3045 tree access = compute_access (basetype_path, decl);
3047 if (access == access_private_node)
3049 cp_error_at ("`%+#D' is %s", decl,
3050 TREE_PRIVATE (decl) ? "private"
3051 : "from private base class");
3052 error ("within this context");
3054 else if (access == access_protected_node)
3056 cp_error_at ("`%+#D' %s", decl,
3057 TREE_PROTECTED (decl) ? "is protected"
3058 : "has protected accessibility");
3059 error ("within this context");
3063 /* Perform the conversions in CONVS on the expression EXPR. */
3066 convert_like (convs, expr)
3069 if (ICS_BAD_FLAG (convs)
3070 && TREE_CODE (convs) != USER_CONV
3071 && TREE_CODE (convs) != AMBIG_CONV)
3074 for (; t; t = TREE_OPERAND (t, 0))
3076 if (TREE_CODE (t) == USER_CONV)
3078 expr = convert_like (t, expr);
3081 else if (TREE_CODE (t) == AMBIG_CONV)
3082 return convert_like (t, expr);
3083 else if (TREE_CODE (t) == IDENTITY_CONV)
3086 return convert_for_initialization
3087 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3088 "conversion", NULL_TREE, 0);
3091 switch (TREE_CODE (convs))
3095 struct z_candidate *cand
3096 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3100 if (DECL_CONSTRUCTOR_P (fn))
3102 tree t = build_int_2 (0, 0);
3103 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3105 args = build_scratch_list (NULL_TREE, expr);
3106 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3107 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3108 args = scratch_tree_cons (NULL_TREE, t, args);
3111 args = build_this (expr);
3112 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3114 /* If this is a constructor or a function returning an aggr type,
3115 we need to build up a TARGET_EXPR. */
3116 if (DECL_CONSTRUCTOR_P (fn))
3117 expr = build_cplus_new (TREE_TYPE (convs), expr);
3122 if (type_unknown_p (expr))
3123 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3124 if (TREE_READONLY_DECL_P (expr))
3125 expr = decl_constant_value (expr);
3128 /* Call build_user_type_conversion again for the error. */
3129 return build_user_type_conversion
3130 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3136 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3137 if (expr == error_mark_node)
3138 return error_mark_node;
3140 switch (TREE_CODE (convs))
3143 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3145 /* else fall through */
3148 tree cvt_expr = build_user_type_conversion
3149 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3152 /* This can occur if, for example, the EXPR has incomplete
3153 type. We can't check for that before attempting the
3154 conversion because the type might be an incomplete
3155 array type, which is OK if some constructor for the
3156 destination type takes a pointer argument. */
3157 if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
3159 if (comptypes (TREE_TYPE (expr), TREE_TYPE (convs), 1))
3160 incomplete_type_error (expr, TREE_TYPE (expr));
3162 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3163 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3166 cp_error ("could not convert `%E' to `%T'",
3167 expr, TREE_TYPE (convs));
3168 return error_mark_node;
3174 return convert_to_reference
3175 (TREE_TYPE (convs), expr,
3176 CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
3179 return decay_conversion (expr);
3184 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3185 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3188 /* ARG is being passed to a varargs function. Perform any conversions
3189 required. Return the converted value. */
3192 convert_arg_to_ellipsis (arg)
3195 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3196 && (TYPE_PRECISION (TREE_TYPE (arg))
3197 < TYPE_PRECISION (double_type_node)))
3198 /* Convert `float' to `double'. */
3199 arg = cp_convert (double_type_node, arg);
3200 else if (IS_AGGR_TYPE (TREE_TYPE (arg))
3201 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (arg)))
3202 cp_warning ("cannot pass objects of type `%T' through `...'",
3205 /* Convert `short' and `char' to full-size `int'. */
3206 arg = default_conversion (arg);
3211 /* ARG is a default argument expression being passed to a parameter of
3212 the indicated TYPE. Do any required conversions. Return the
3216 convert_default_arg (type, arg)
3219 arg = break_out_target_exprs (arg);
3221 if (TREE_CODE (arg) == CONSTRUCTOR)
3223 arg = digest_init (type, arg, 0);
3224 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3225 "default argument", 0, 0);
3229 /* This could get clobbered by the following call. */
3230 if (TREE_HAS_CONSTRUCTOR (arg))
3231 arg = copy_node (arg);
3233 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3234 "default argument", 0, 0);
3235 #ifdef PROMOTE_PROTOTYPES
3236 if ((TREE_CODE (type) == INTEGER_TYPE
3237 || TREE_CODE (type) == ENUMERAL_TYPE)
3238 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3239 arg = default_conversion (arg);
3247 build_over_call (cand, args, flags)
3248 struct z_candidate *cand;
3253 tree convs = cand->convs;
3254 tree converted_args = NULL_TREE;
3255 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3256 tree conv, arg, val;
3260 /* Give any warnings we noticed during overload resolution. */
3262 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3263 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3265 if (DECL_FUNCTION_MEMBER_P (fn))
3266 enforce_access (cand->basetype_path, fn);
3268 if (args && TREE_CODE (args) != TREE_LIST)
3269 args = build_scratch_list (NULL_TREE, args);
3272 /* The implicit parameters to a constructor are not considered by overload
3273 resolution, and must be of the proper type. */
3274 if (DECL_CONSTRUCTOR_P (fn))
3276 converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3277 arg = TREE_CHAIN (arg);
3278 parm = TREE_CHAIN (parm);
3279 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3281 converted_args = expr_tree_cons
3282 (NULL_TREE, TREE_VALUE (arg), converted_args);
3283 arg = TREE_CHAIN (arg);
3284 parm = TREE_CHAIN (parm);
3287 /* Bypass access control for 'this' parameter. */
3288 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3290 tree parmtype = TREE_VALUE (parm);
3291 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3292 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3294 int dv = (TYPE_VOLATILE (TREE_TYPE (parmtype))
3295 < TYPE_VOLATILE (TREE_TYPE (argtype)));
3296 int dc = (TYPE_READONLY (TREE_TYPE (parmtype))
3297 < TYPE_READONLY (TREE_TYPE (argtype)));
3298 char *p = (dv && dc ? "const and volatile"
3299 : dc ? "const" : dv ? "volatile" : "");
3301 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards %s",
3302 TREE_TYPE (argtype), fn, p);
3304 converted_args = expr_tree_cons
3305 (NULL_TREE, convert_force (TREE_VALUE (parm), TREE_VALUE (arg), CONV_C_CAST),
3307 parm = TREE_CHAIN (parm);
3308 arg = TREE_CHAIN (arg);
3314 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3316 tree type = TREE_VALUE (parm);
3318 conv = TREE_VEC_ELT (convs, i);
3319 if (ICS_BAD_FLAG (conv))
3322 val = TREE_VALUE (arg);
3324 for (; t; t = TREE_OPERAND (t, 0))
3326 if (TREE_CODE (t) == USER_CONV
3327 || TREE_CODE (t) == AMBIG_CONV)
3329 val = convert_like (t, val);
3332 else if (TREE_CODE (t) == IDENTITY_CONV)
3335 val = convert_for_initialization
3336 (NULL_TREE, type, val, LOOKUP_NORMAL,
3337 "argument passing", fn, i - is_method);
3340 val = convert_like (conv, TREE_VALUE (arg));
3342 #ifdef PROMOTE_PROTOTYPES
3343 if ((TREE_CODE (type) == INTEGER_TYPE
3344 || TREE_CODE (type) == ENUMERAL_TYPE)
3345 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3346 val = default_conversion (val);
3348 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3351 /* Default arguments */
3352 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3354 tree arg = TREE_PURPOSE (parm);
3356 if (DECL_TEMPLATE_INFO (fn))
3358 /* This came from a template. Instantiate the default arg here,
3359 not in tsubst. In the case of something like:
3367 we must be careful to do name lookup in the scope of
3368 S<T>, rather than in the current class. */
3369 if (DECL_CLASS_SCOPE_P (fn))
3370 pushclass (DECL_REAL_CONTEXT (fn), 2);
3372 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), NULL_TREE);
3374 if (DECL_CLASS_SCOPE_P (fn))
3377 converted_args = expr_tree_cons
3378 (NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg),
3383 for (; arg; arg = TREE_CHAIN (arg))
3385 = expr_tree_cons (NULL_TREE,
3386 convert_arg_to_ellipsis (TREE_VALUE (arg)),
3389 converted_args = nreverse (converted_args);
3391 /* Avoid actually calling copy constructors and copy assignment operators,
3393 if (DECL_CONSTRUCTOR_P (fn)
3394 && TREE_VEC_LENGTH (convs) == 1
3395 && copy_args_p (fn))
3398 arg = TREE_CHAIN (converted_args);
3399 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3400 arg = TREE_CHAIN (arg);
3401 arg = TREE_VALUE (arg);
3403 /* Pull out the real argument, disregarding const-correctness. */
3405 while (TREE_CODE (targ) == NOP_EXPR
3406 || TREE_CODE (targ) == NON_LVALUE_EXPR
3407 || TREE_CODE (targ) == CONVERT_EXPR)
3408 targ = TREE_OPERAND (targ, 0);
3409 if (TREE_CODE (targ) == ADDR_EXPR)
3411 targ = TREE_OPERAND (targ, 0);
3412 if (! comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3413 TYPE_MAIN_VARIANT (TREE_TYPE (targ)), 1))
3422 arg = build_indirect_ref (arg, 0);
3424 /* [class.copy]: the copy constructor is implicitly defined even if
3425 the implementation elided its use. */
3426 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3429 /* If we're creating a temp and we already have one, don't create a
3430 new one. If we're not creating a temp but we get one, use
3431 INIT_EXPR to collapse the temp into our target. Otherwise, if the
3432 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3433 temp or an INIT_EXPR otherwise. */
3434 if (integer_zerop (TREE_VALUE (args)))
3436 if (! real_lvalue_p (arg))
3438 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3440 val = build (VAR_DECL, DECL_CONTEXT (fn));
3441 layout_decl (val, 0);
3442 val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
3443 TREE_SIDE_EFFECTS (val) = 1;
3447 else if (! real_lvalue_p (arg)
3448 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3450 tree to = stabilize_reference
3451 (build_indirect_ref (TREE_VALUE (args), 0));
3453 /* Don't copy the padding byte; it might not have been allocated
3454 if to is a base subobject. */
3455 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3456 return build_unary_op
3457 (ADDR_EXPR, build (COMPOUND_EXPR, TREE_TYPE (to),
3458 cp_convert (void_type_node, arg), to),
3461 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
3462 TREE_SIDE_EFFECTS (val) = 1;
3463 return build_unary_op (ADDR_EXPR, val, 0);
3466 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
3468 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
3470 tree to = stabilize_reference
3471 (build_indirect_ref (TREE_VALUE (converted_args), 0));
3473 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
3475 /* Don't copy the padding byte; it might not have been allocated
3476 if to is a base subobject. */
3477 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3478 return build (COMPOUND_EXPR, TREE_TYPE (to),
3479 cp_convert (void_type_node, arg), to);
3481 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
3482 TREE_SIDE_EFFECTS (val) = 1;
3488 if (DECL_CLASS_SCOPE_P (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
3489 return build_signature_method_call (fn, converted_args);
3490 else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3492 tree t, *p = &TREE_VALUE (converted_args);
3493 tree binfo = get_binfo
3494 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
3495 *p = convert_pointer_to_real (binfo, *p);
3496 if (TREE_SIDE_EFFECTS (*p))
3497 *p = save_expr (*p);
3498 t = build_pointer_type (TREE_TYPE (fn));
3499 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
3502 else if (DECL_INLINE (fn))
3503 fn = inline_conversion (fn);
3505 fn = build_addr_func (fn);
3507 /* Recognize certain built-in functions so we can make tree-codes
3508 other than CALL_EXPR. We do this when it enables fold-const.c
3509 to do something useful. */
3511 if (TREE_CODE (fn) == ADDR_EXPR
3512 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3513 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
3514 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
3519 if (converted_args == 0)
3520 return integer_zero_node;
3521 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
3526 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3527 if (TREE_TYPE (fn) == void_type_node)
3529 fn = require_complete_type (fn);
3530 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
3531 fn = build_cplus_new (TREE_TYPE (fn), fn);
3532 return convert_from_reference (fn);
3536 build_new_method_call (instance, name, args, basetype_path, flags)
3537 tree instance, name, args, basetype_path;
3540 struct z_candidate *candidates = 0, *cand;
3541 tree explicit_targs = NULL_TREE;
3542 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
3544 tree user_args = args;
3545 tree templates = NULL_TREE;
3546 int template_only = 0;
3548 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3550 explicit_targs = TREE_OPERAND (name, 1);
3551 name = TREE_OPERAND (name, 0);
3552 if (TREE_CODE (name) == TEMPLATE_DECL)
3553 name = DECL_NAME (name);
3557 /* If there is an extra argument for controlling virtual bases,
3558 remove it for error reporting. */
3559 if (flags & LOOKUP_HAS_IN_CHARGE)
3560 user_args = TREE_CHAIN (args);
3562 args = resolve_args (args);
3564 if (args == error_mark_node)
3565 return error_mark_node;
3567 if (instance == NULL_TREE)
3568 basetype = BINFO_TYPE (basetype_path);
3571 if (TREE_CODE (instance) == OFFSET_REF)
3572 instance = resolve_offset_ref (instance);
3573 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3574 instance = convert_from_reference (instance);
3575 basetype = TREE_TYPE (instance);
3577 /* XXX this should be handled before we get here. */
3578 if (! IS_AGGR_TYPE (basetype)
3579 && ! (TYPE_LANG_SPECIFIC (basetype)
3580 && (IS_SIGNATURE_POINTER (basetype)
3581 || IS_SIGNATURE_REFERENCE (basetype))))
3583 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
3584 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
3585 name, instance, basetype);
3587 return error_mark_node;
3590 /* If `instance' is a signature pointer/reference and `name' is
3591 not a constructor, we are calling a signature member function.
3592 In that case set the `basetype' to the signature type. */
3593 if ((IS_SIGNATURE_POINTER (basetype)
3594 || IS_SIGNATURE_REFERENCE (basetype))
3595 && TYPE_IDENTIFIER (basetype) != name)
3596 basetype = SIGNATURE_TYPE (basetype);
3599 if (basetype_path == NULL_TREE)
3600 basetype_path = TYPE_BINFO (basetype);
3604 instance_ptr = build_this (instance);
3606 if (! template_only)
3608 /* XXX this should be handled before we get here. */
3609 fns = build_field_call (basetype_path, instance_ptr, name, args);
3616 instance_ptr = build_int_2 (0, 0);
3617 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
3621 = (name == ctor_identifier ? constructor_name (basetype) : name);
3623 fns = lookup_fnfields (basetype_path, name, 1);
3625 if (fns == error_mark_node)
3626 return error_mark_node;
3629 tree fn = TREE_VALUE (fns);
3630 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3631 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3633 flags |= LOOKUP_HAS_IN_CHARGE;
3634 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3636 mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
3637 for (; fn; fn = OVL_NEXT (fn))
3639 tree t = OVL_CURRENT (fn);
3642 /* We can end up here for copy-init of same or base class. */
3643 if (name == ctor_identifier
3644 && (flags & LOOKUP_ONLYCONVERTING)
3645 && DECL_NONCONVERTING_P (t))
3647 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
3648 this_arglist = mem_args;
3650 this_arglist = args;
3652 if (TREE_CODE (t) == TEMPLATE_DECL)
3654 /* A member template. */
3655 templates = scratch_tree_cons (NULL_TREE, t, templates);
3657 add_template_candidate (candidates, t, explicit_targs,
3659 TREE_TYPE (name), flags, DEDUCE_CALL);
3661 else if (! template_only)
3662 candidates = add_function_candidate (candidates, t,
3663 this_arglist, flags);
3666 candidates->basetype_path = TREE_PURPOSE (fns);
3670 if (! any_viable (candidates))
3672 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
3673 if (flags & LOOKUP_SPECULATIVELY)
3675 cp_error ("no matching function for call to `%T::%D (%A)%V'", basetype,
3676 pretty_name, user_args, TREE_TYPE (TREE_TYPE (instance_ptr)));
3677 print_z_candidates (candidates);
3678 return error_mark_node;
3680 candidates = splice_viable (candidates);
3681 cand = tourney (candidates);
3685 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
3687 print_z_candidates (candidates);
3688 return error_mark_node;
3691 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
3692 && instance == current_class_ref
3693 && DECL_CONSTRUCTOR_P (current_function_decl)
3694 && ! (flags & LOOKUP_NONVIRTUAL)
3695 && value_member (cand->fn, get_abstract_virtuals (basetype)))
3696 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
3697 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3698 && TREE_CODE (instance_ptr) == NOP_EXPR
3699 && TREE_OPERAND (instance_ptr, 0) == error_mark_node)
3700 cp_error ("cannot call member function `%D' without object", cand->fn);
3702 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
3703 && ((instance == current_class_ref && (dtor_label || ctor_label))
3704 || resolves_to_fixed_type_p (instance, 0)))
3705 flags |= LOOKUP_NONVIRTUAL;
3707 /* Pedantically, normal function declarations are never considered
3708 to refer to template instantiations, so we only do this with
3710 if (flag_guiding_decls && templates && ! cand->template
3711 && ! DECL_INITIAL (cand->fn))
3712 add_maybe_template (cand->fn, templates);
3714 return build_over_call
3716 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
3720 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
3721 subsequence of ICS2. */
3724 is_subseq (ics1, ics2)
3727 /* We can assume that a conversion of the same code
3728 between the same types indicates a subsequence since we only get
3729 here if the types we are converting from are the same. */
3731 while (TREE_CODE (ics1) == RVALUE_CONV
3732 || TREE_CODE (ics1) == LVALUE_CONV)
3733 ics1 = TREE_OPERAND (ics1, 0);
3737 while (TREE_CODE (ics2) == RVALUE_CONV
3738 || TREE_CODE (ics2) == LVALUE_CONV)
3739 ics2 = TREE_OPERAND (ics2, 0);
3741 if (TREE_CODE (ics2) == USER_CONV
3742 || TREE_CODE (ics2) == AMBIG_CONV
3743 || TREE_CODE (ics2) == IDENTITY_CONV)
3744 /* At this point, ICS1 cannot be a proper subsequence of
3745 ICS2. We can get a USER_CONV when we are comparing the
3746 second standard conversion sequence of two user conversion
3750 ics2 = TREE_OPERAND (ics2, 0);
3752 if (TREE_CODE (ics2) == TREE_CODE (ics1)
3753 && comptypes (TREE_TYPE (ics2), TREE_TYPE (ics1), 1)
3754 && comptypes (TREE_TYPE (TREE_OPERAND (ics2, 0)),
3755 TREE_TYPE (TREE_OPERAND (ics1, 0)), 1))
3760 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
3761 be any _TYPE nodes. */
3764 is_properly_derived_from (derived, base)
3768 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
3769 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
3772 /* We only allow proper derivation here. The DERIVED_FROM_P macro
3773 considers every class derived from itself. */
3774 return (!comptypes (TYPE_MAIN_VARIANT (derived),
3775 TYPE_MAIN_VARIANT (base), 1)
3776 && DERIVED_FROM_P (base, derived));
3779 /* We build the ICS for an implicit object parameter as a pointer
3780 conversion sequence. However, such a sequence should be compared
3781 as if it were a reference conversion sequence. If ICS is the
3782 implicit conversion sequence for an implicit object parameter,
3783 modify it accordingly. */
3786 maybe_handle_implicit_object (ics)
3789 if (ICS_THIS_FLAG (*ics))
3791 /* [over.match.funcs]
3793 For non-static member functions, the type of the
3794 implicit object parameter is "reference to cv X"
3795 where X is the class of which the function is a
3796 member and cv is the cv-qualification on the member
3797 function declaration. */
3799 if (TREE_CODE (t) == QUAL_CONV)
3800 t = TREE_OPERAND (t, 0);
3801 if (TREE_CODE (t) == PTR_CONV)
3802 t = TREE_OPERAND (t, 0);
3803 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3804 t = build_conv (REF_BIND,
3805 build_reference_type (TREE_TYPE (TREE_TYPE (*ics))),
3807 ICS_STD_RANK (t) = ICS_STD_RANK (*ics);
3812 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
3813 to the type the reference originally referred to, and return 1.
3814 Otherwise, return 0. */
3817 maybe_handle_ref_bind (ics, target_type)
3821 if (TREE_CODE (*ics) == REF_BIND)
3825 When a parameter of reference type binds directly
3826 (_dcl.init.ref_) to an argument expression, the implicit
3827 conversion sequence is the identity conversion, unless the
3828 argument expression has a type that is a derived class of the
3829 parameter type, in which case the implicit conversion
3830 sequence is a derived-to-base Conversion.
3832 If the parameter binds directly to the result of applying a
3833 conversion function to the argument expression, the implicit
3834 conversion sequence is a user-defined conversion sequence
3835 (_over.ics.user_), with the second standard conversion
3836 sequence either an identity conversion or, if the conversion
3837 function returns an entity of a type that is a derived class
3838 of the parameter type, a derived-to-base Conversion.
3840 When a parameter of reference type is not bound directly to
3841 an argument expression, the conversion sequence is the one
3842 required to convert the argument expression to the underlying
3843 type of the reference according to _over.best.ics_.
3844 Conceptually, this conversion sequence corresponds to
3845 copy-initializing a temporary of the underlying type with the
3846 argument expression. Any difference in top-level
3847 cv-qualification is subsumed by the initialization itself and
3848 does not constitute a conversion. */
3850 tree old_ics = *ics;
3852 *target_type = TREE_TYPE (TREE_TYPE (*ics));
3853 *ics = TREE_OPERAND (*ics, 0);
3854 if (TREE_CODE (*ics) == IDENTITY_CONV
3855 && is_properly_derived_from (TREE_TYPE (*ics), *target_type))
3856 *ics = build_conv (BASE_CONV, *target_type, *ics);
3857 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
3858 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
3866 /* Compare two implicit conversion sequences according to the rules set out in
3867 [over.ics.rank]. Return values:
3869 1: ics1 is better than ics2
3870 -1: ics2 is better than ics1
3871 0: ics1 and ics2 are indistinguishable */
3874 compare_ics (ics1, ics2)
3881 tree deref_from_type1 = NULL_TREE;
3882 tree deref_from_type2;
3883 tree deref_to_type1;
3884 tree deref_to_type2;
3886 /* REF_BINDING is non-zero if the result of the conversion sequence
3887 is a reference type. In that case TARGET_TYPE is the
3888 type referred to by the reference. */
3894 /* Handle implicit object parameters. */
3895 maybe_handle_implicit_object (&ics1);
3896 maybe_handle_implicit_object (&ics2);
3898 /* Handle reference parameters. */
3899 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
3900 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
3904 When comparing the basic forms of implicit conversion sequences (as
3905 defined in _over.best.ics_)
3907 --a standard conversion sequence (_over.ics.scs_) is a better
3908 conversion sequence than a user-defined conversion sequence
3909 or an ellipsis conversion sequence, and
3911 --a user-defined conversion sequence (_over.ics.user_) is a
3912 better conversion sequence than an ellipsis conversion sequence
3913 (_over.ics.ellipsis_). */
3914 if (ICS_RANK (ics1) > ICS_RANK (ics2))
3916 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
3919 if (ICS_RANK (ics1) == BAD_RANK)
3921 /* Both ICS are bad. We try to make a decision based on what
3922 would have happenned if they'd been good. */
3923 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
3924 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3926 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
3927 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3930 /* We couldn't make up our minds; try to figure it out below. */
3933 if (ICS_ELLIPSIS_FLAG (ics1))
3934 /* Both conversions are ellipsis conversions. */
3937 /* User-defined conversion sequence U1 is a better conversion sequence
3938 than another user-defined conversion sequence U2 if they contain the
3939 same user-defined conversion operator or constructor and if the sec-
3940 ond standard conversion sequence of U1 is better than the second
3941 standard conversion sequence of U2. */
3943 if (ICS_USER_FLAG (ics1))
3947 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
3948 if (TREE_CODE (t1) == AMBIG_CONV)
3950 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
3951 if (TREE_CODE (t2) == AMBIG_CONV)
3954 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
3957 /* We can just fall through here, after setting up
3958 FROM_TYPE1 and FROM_TYPE2. */
3959 from_type1 = TREE_TYPE (t1);
3960 from_type2 = TREE_TYPE (t2);
3964 /* We're dealing with two standard conversion sequences.
3968 Standard conversion sequence S1 is a better conversion
3969 sequence than standard conversion sequence S2 if
3971 --S1 is a proper subsequence of S2 (comparing the conversion
3972 sequences in the canonical form defined by _over.ics.scs_,
3973 excluding any Lvalue Transformation; the identity
3974 conversion sequence is considered to be a subsequence of
3975 any non-identity conversion sequence */
3978 while (TREE_CODE (from_type1) != IDENTITY_CONV)
3979 from_type1 = TREE_OPERAND (from_type1, 0);
3980 from_type1 = TREE_TYPE (from_type1);
3983 while (TREE_CODE (from_type2) != IDENTITY_CONV)
3984 from_type2 = TREE_OPERAND (from_type2, 0);
3985 from_type2 = TREE_TYPE (from_type2);
3988 if (comptypes (from_type1, from_type2, 1))
3990 if (is_subseq (ics1, ics2))
3992 if (is_subseq (ics2, ics1))
3995 /* Otherwise, one sequence cannot be a subsequence of the other; they
3996 don't start with the same type. This can happen when comparing the
3997 second standard conversion sequence in two user-defined conversion
4004 --the rank of S1 is better than the rank of S2 (by the rules
4007 Standard conversion sequences are ordered by their ranks: an Exact
4008 Match is a better conversion than a Promotion, which is a better
4009 conversion than a Conversion.
4011 Two conversion sequences with the same rank are indistinguishable
4012 unless one of the following rules applies:
4014 --A conversion that is not a conversion of a pointer, or pointer
4015 to member, to bool is better than another conversion that is such
4018 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4019 so that we do not have to check it explicitly. */
4020 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4022 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4025 to_type1 = TREE_TYPE (ics1);
4026 to_type2 = TREE_TYPE (ics2);
4028 if (TYPE_PTR_P (from_type1)
4029 && TYPE_PTR_P (from_type2)
4030 && TYPE_PTR_P (to_type1)
4031 && TYPE_PTR_P (to_type2))
4033 deref_from_type1 = TREE_TYPE (from_type1);
4034 deref_from_type2 = TREE_TYPE (from_type2);
4035 deref_to_type1 = TREE_TYPE (to_type1);
4036 deref_to_type2 = TREE_TYPE (to_type2);
4038 /* The rules for pointers to members A::* are just like the rules
4039 for pointers A*, except opposite: if B is derived from A then
4040 A::* converts to B::*, not vice versa. For that reason, we
4041 switch the from_ and to_ variables here. */
4042 else if (TYPE_PTRMEM_P (from_type1)
4043 && TYPE_PTRMEM_P (from_type2)
4044 && TYPE_PTRMEM_P (to_type1)
4045 && TYPE_PTRMEM_P (to_type2))
4047 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4048 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4049 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4050 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4052 else if (TYPE_PTRMEMFUNC_P (from_type1)
4053 && TYPE_PTRMEMFUNC_P (from_type2)
4054 && TYPE_PTRMEMFUNC_P (to_type1)
4055 && TYPE_PTRMEMFUNC_P (to_type2))
4057 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4058 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4059 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4060 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4063 if (deref_from_type1 != NULL_TREE
4064 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4065 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4067 /* This was one of the pointer or pointer-like conversions.
4071 --If class B is derived directly or indirectly from class A,
4072 conversion of B* to A* is better than conversion of B* to
4073 void*, and conversion of A* to void* is better than
4074 conversion of B* to void*. */
4075 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4076 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4078 if (is_properly_derived_from (deref_from_type1,
4081 else if (is_properly_derived_from (deref_from_type2,
4085 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4086 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4088 if (comptypes (deref_from_type1, deref_from_type2, 1))
4090 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4092 if (is_properly_derived_from (deref_from_type1,
4096 /* We know that DEREF_TO_TYPE1 is `void' here. */
4097 else if (is_properly_derived_from (deref_from_type1,
4102 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4103 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4107 --If class B is derived directly or indirectly from class A
4108 and class C is derived directly or indirectly from B,
4110 --conversion of C* to B* is better than conversion of C* to
4113 --conversion of B* to A* is better than conversion of C* to
4115 if (comptypes (deref_from_type1, deref_from_type2, 1))
4117 if (is_properly_derived_from (deref_to_type1,
4120 else if (is_properly_derived_from (deref_to_type2,
4124 else if (comptypes (deref_to_type1, deref_to_type2, 1))
4126 if (is_properly_derived_from (deref_from_type2,
4129 else if (is_properly_derived_from (deref_from_type1,
4135 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4136 && comptypes (from_type1, from_type2, 1))
4140 --binding of an expression of type C to a reference of type
4141 B& is better than binding an expression of type C to a
4142 reference of type A&
4144 --conversion of C to B is better than conversion of C to A, */
4145 if (is_properly_derived_from (from_type1, to_type1)
4146 && is_properly_derived_from (from_type1, to_type2))
4148 if (is_properly_derived_from (to_type1, to_type2))
4150 else if (is_properly_derived_from (to_type2, to_type1))
4154 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4155 && comptypes (to_type1, to_type2, 1))
4159 --binding of an expression of type B to a reference of type
4160 A& is better than binding an expression of type C to a
4161 reference of type A&,
4163 --onversion of B to A is better than conversion of C to A */
4164 if (is_properly_derived_from (from_type1, to_type1)
4165 && is_properly_derived_from (from_type2, to_type1))
4167 if (is_properly_derived_from (from_type2, from_type1))
4169 else if (is_properly_derived_from (from_type1, from_type2))
4176 --S1 and S2 differ only in their qualification conversion and yield
4177 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4178 qualification signature of type T1 is a proper subset of the cv-
4179 qualification signature of type T2 */
4180 if (TREE_CODE (ics1) == QUAL_CONV
4181 && TREE_CODE (ics2) == QUAL_CONV
4182 && comptypes (from_type1, from_type2, 1))
4183 return comp_cv_qual_signature (to_type1, to_type2);
4187 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4188 types to which the references refer are the same type except for
4189 top-level cv-qualifiers, and the type to which the reference
4190 initialized by S2 refers is more cv-qualified than the type to
4191 which the reference initialized by S1 refers */
4193 if (ref_binding1 && ref_binding2
4194 && comptypes (TYPE_MAIN_VARIANT (to_type1),
4195 TYPE_MAIN_VARIANT (to_type2), 1))
4196 return comp_cv_qualification (target_type2, target_type1);
4198 /* Neither conversion sequence is better than the other. */
4202 /* The source type for this standard conversion sequence. */
4208 for (;; t = TREE_OPERAND (t, 0))
4210 if (TREE_CODE (t) == USER_CONV
4211 || TREE_CODE (t) == AMBIG_CONV
4212 || TREE_CODE (t) == IDENTITY_CONV)
4213 return TREE_TYPE (t);
4215 my_friendly_abort (1823);
4218 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4219 a pointer to LOSER and re-running joust to produce the warning if WINNER
4220 is actually used. */
4223 add_warning (winner, loser)
4224 struct z_candidate *winner, *loser;
4226 winner->warnings = expr_tree_cons (NULL_PTR,
4227 build_expr_ptr_wrapper (loser),
4231 /* Compare two candidates for overloading as described in
4232 [over.match.best]. Return values:
4234 1: cand1 is better than cand2
4235 -1: cand2 is better than cand1
4236 0: cand1 and cand2 are indistinguishable */
4239 joust (cand1, cand2, warn)
4240 struct z_candidate *cand1, *cand2;
4244 int i, off1 = 0, off2 = 0, len;
4246 /* Candidates that involve bad conversions are always worse than those
4248 if (cand1->viable > cand2->viable)
4250 if (cand1->viable < cand2->viable)
4253 /* a viable function F1
4254 is defined to be a better function than another viable function F2 if
4255 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4256 ICSi(F2), and then */
4258 /* for some argument j, ICSj(F1) is a better conversion sequence than
4261 /* For comparing static and non-static member functions, we ignore the
4262 implicit object parameter of the non-static function. The WP says to
4263 pretend that the static function has an object parm, but that won't
4264 work with operator overloading. */
4265 len = TREE_VEC_LENGTH (cand1->convs);
4266 if (len != TREE_VEC_LENGTH (cand2->convs))
4268 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4269 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4271 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4272 && DECL_STATIC_FUNCTION_P (cand2->fn))
4278 my_friendly_abort (42);
4281 for (i = 0; i < len; ++i)
4283 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4284 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4285 int comp = compare_ics (t1, t2);
4290 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4291 && TREE_CODE (t1) == STD_CONV
4292 && TREE_CODE (t2) == STD_CONV
4293 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4294 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4295 && (TYPE_PRECISION (TREE_TYPE (t1))
4296 == TYPE_PRECISION (TREE_TYPE (t2)))
4297 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4298 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4301 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4303 struct z_candidate *w, *l;
4305 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4306 w = cand1, l = cand2;
4308 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4309 w = cand2, l = cand1;
4313 cp_warning ("passing `%T' chooses `%T' over `%T'",
4314 type, type1, type2);
4315 cp_warning (" in call to `%D'", w->fn);
4321 if (winner && comp != winner)
4330 /* warn about confusing overload resolution for user-defined conversions,
4331 either between a constructor and a conversion op, or between two
4333 if (winner && cand1->second_conv
4334 && ((DECL_CONSTRUCTOR_P (cand1->fn)
4335 != DECL_CONSTRUCTOR_P (cand2->fn))
4336 /* Don't warn if the two conv ops convert to the same type... */
4337 || (! DECL_CONSTRUCTOR_P (cand1->fn)
4338 && ! comptypes (TREE_TYPE (cand1->second_conv),
4339 TREE_TYPE (cand2->second_conv), 1))))
4341 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4344 struct z_candidate *w, *l;
4346 w = cand1, l = cand2;
4348 w = cand2, l = cand1;
4351 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4352 if (! DECL_CONSTRUCTOR_P (w->fn))
4353 source = TREE_TYPE (source);
4354 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4355 cp_warning (" for conversion from `%T' to `%T'",
4356 source, TREE_TYPE (w->second_conv));
4357 cp_warning (" because conversion sequence for the argument is better");
4368 F1 is a non-template function and F2 is a template function */
4370 if (! cand1->template && cand2->template)
4372 else if (cand1->template && ! cand2->template)
4374 else if (cand1->template && cand2->template)
4375 winner = more_specialized
4376 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4380 the context is an initialization by user-defined conversion (see
4381 _dcl.init_ and _over.match.user_) and the standard conversion
4382 sequence from the return type of F1 to the destination type (i.e.,
4383 the type of the entity being initialized) is a better conversion
4384 sequence than the standard conversion sequence from the return type
4385 of F2 to the destination type. */
4387 if (! winner && cand1->second_conv)
4388 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4390 /* If the built-in candidates are the same, arbitrarily pick one. */
4391 if (! winner && cand1->fn == cand2->fn
4392 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4394 for (i = 0; i < len; ++i)
4395 if (! comptypes (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4396 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i)), 1))
4398 if (i == TREE_VEC_LENGTH (cand1->convs))
4401 /* Kludge around broken overloading rules whereby
4402 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4403 that takes references and another that takes values. */
4404 if (cand1->fn == ansi_opname[COND_EXPR])
4406 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4407 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4408 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4409 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4411 if (comptypes (t1, t2, 1))
4413 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4415 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4423 /* Extension: If the worst conversion for one candidate is worse than the
4424 worst conversion for the other, take the first. */
4425 if (! winner && ! pedantic)
4427 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4429 for (i = 0; i < len; ++i)
4431 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4432 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4433 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
4434 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
4446 /* Given a list of candidates for overloading, find the best one, if any.
4447 This algorithm has a worst case of O(2n) (winner is last), and a best
4448 case of O(n/2) (totally ambiguous); much better than a sorting
4451 static struct z_candidate *
4452 tourney (candidates)
4453 struct z_candidate *candidates;
4455 struct z_candidate *champ = candidates, *challenger;
4457 int champ_compared_to_predecessor = 0;
4459 /* Walk through the list once, comparing each current champ to the next
4460 candidate, knocking out a candidate or two with each comparison. */
4462 for (challenger = champ->next; challenger; )
4464 fate = joust (champ, challenger, 0);
4466 challenger = challenger->next;
4471 champ = challenger->next;
4474 champ_compared_to_predecessor = 0;
4479 champ_compared_to_predecessor = 1;
4482 challenger = champ->next;
4486 /* Make sure the champ is better than all the candidates it hasn't yet
4487 been compared to. */
4489 for (challenger = candidates;
4491 && !(champ_compared_to_predecessor && challenger->next == champ);
4492 challenger = challenger->next)
4494 fate = joust (champ, challenger, 0);
4503 can_convert (to, from)
4506 tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
4507 return (t && ! ICS_BAD_FLAG (t));
4511 can_convert_arg (to, from, arg)
4514 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
4515 return (t && ! ICS_BAD_FLAG (t));