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)
100 if (datum == error_mark_node)
101 return error_mark_node;
103 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
104 datum = convert_from_reference (datum);
106 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
107 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
108 datum, CLASSTYPE_VFIELD (type));
110 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
115 /* Build a call to a member of an object. I.e., one that overloads
116 operator ()(), or is a pointer-to-function or pointer-to-method. */
119 build_field_call (basetype_path, instance_ptr, name, parms)
120 tree basetype_path, instance_ptr, name, parms;
122 tree field, instance;
124 if (name == ctor_identifier || name == dtor_identifier)
127 if (instance_ptr == current_class_ptr)
129 /* Check to see if we really have a reference to an instance variable
130 with `operator()()' overloaded. */
131 field = IDENTIFIER_CLASS_VALUE (name);
133 if (field == NULL_TREE)
135 cp_error ("`this' has no member named `%D'", name);
136 return error_mark_node;
139 if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
141 /* If it's a field, try overloading operator (),
142 or calling if the field is a pointer-to-function. */
143 instance = build_component_ref_1 (current_class_ref, field, 0);
144 if (instance == error_mark_node)
145 return error_mark_node;
147 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance)))
148 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
150 if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
152 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
153 return build_function_call (instance, parms);
154 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
155 return build_function_call (instance, expr_tree_cons (NULL_TREE, current_class_ptr, parms));
161 /* Check to see if this is not really a reference to an instance variable
162 with `operator()()' overloaded. */
163 field = lookup_field (basetype_path, name, 1, 0);
165 /* This can happen if the reference was ambiguous or for access
167 if (field == error_mark_node)
168 return error_mark_node;
170 if (field && (TREE_CODE (field) == FIELD_DECL ||
171 TREE_CODE (field) == VAR_DECL))
174 tree ftype = TREE_TYPE (field);
176 if (TREE_CODE (ftype) == REFERENCE_TYPE)
177 ftype = TREE_TYPE (ftype);
179 if (TYPE_LANG_SPECIFIC (ftype))
181 /* Make the next search for this field very short. */
182 basetype = DECL_FIELD_CONTEXT (field);
183 instance_ptr = convert_pointer_to (basetype, instance_ptr);
185 instance = build_indirect_ref (instance_ptr, NULL_PTR);
186 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
187 build_component_ref_1 (instance, field, 0),
190 if (TREE_CODE (ftype) == POINTER_TYPE)
192 if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
193 || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
195 /* This is a member which is a pointer to function. */
197 = build_component_ref_1 (build_indirect_ref (instance_ptr,
199 field, LOOKUP_COMPLAIN);
200 if (ref == error_mark_node)
201 return error_mark_node;
202 return build_function_call (ref, parms);
205 else if (TREE_CODE (ftype) == METHOD_TYPE)
207 error ("invalid call via pointer-to-member function");
208 return error_mark_node;
217 find_scoped_type (type, inner_name, inner_types)
218 tree type, inner_name, inner_types;
220 tree tags = CLASSTYPE_TAGS (type);
224 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
225 enclosing class) is set to the name for the enum type. So, if
226 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
227 then this test will be true. */
228 if (TREE_PURPOSE (tags) == inner_name)
230 if (inner_types == NULL_TREE)
231 return TYPE_MAIN_DECL (TREE_VALUE (tags));
232 return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
234 tags = TREE_CHAIN (tags);
237 /* Look for a TYPE_DECL. */
238 for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
239 if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
241 /* Code by raeburn. */
242 if (inner_types == NULL_TREE)
244 return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
250 /* Resolve an expression NAME1::NAME2::...::NAMEn to
251 the name that names the above nested type. INNER_TYPES
252 is a chain of nested type names (held together by SCOPE_REFs);
253 OUTER_TYPE is the type we know to enclose INNER_TYPES.
254 Returns NULL_TREE if there is an error. */
257 resolve_scope_to_name (outer_type, inner_stuff)
258 tree outer_type, inner_stuff;
261 tree inner_name, inner_type;
263 if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
265 /* We first try to look for a nesting in our current class context,
266 then try any enclosing classes. */
267 tree type = current_class_type;
269 while (type && (TREE_CODE (type) == RECORD_TYPE
270 || TREE_CODE (type) == UNION_TYPE))
272 tree rval = resolve_scope_to_name (type, inner_stuff);
274 if (rval != NULL_TREE)
276 type = DECL_CONTEXT (TYPE_MAIN_DECL (type));
280 if (TREE_CODE (inner_stuff) == SCOPE_REF)
282 inner_name = TREE_OPERAND (inner_stuff, 0);
283 inner_type = TREE_OPERAND (inner_stuff, 1);
287 inner_name = inner_stuff;
288 inner_type = NULL_TREE;
291 if (outer_type == NULL_TREE)
294 /* If we have something that's already a type by itself,
296 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
299 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
304 x = lookup_name (inner_name, 0);
306 if (x && TREE_CODE (x) == NAMESPACE_DECL)
308 x = lookup_namespace_name (x, inner_type);
314 if (! IS_AGGR_TYPE (outer_type))
317 /* Look for member classes or enums. */
318 tmp = find_scoped_type (outer_type, inner_name, inner_type);
320 /* If it's not a type in this class, then go down into the
321 base classes and search there. */
322 if (! tmp && TYPE_BINFO (outer_type))
324 tree binfos = TYPE_BINFO_BASETYPES (outer_type);
325 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
327 for (i = 0; i < n_baselinks; i++)
329 tree base_binfo = TREE_VEC_ELT (binfos, i);
330 tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
340 /* Returns nonzero iff the destructor name specified in NAME
341 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
345 check_dtor_name (basetype, name)
348 name = TREE_OPERAND (name, 0);
350 if (TREE_CODE (name) == TYPE_DECL)
351 name = TREE_TYPE (name);
352 else if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
354 else if (TREE_CODE (name) == IDENTIFIER_NODE)
356 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
357 || (TREE_CODE (basetype) == ENUMERAL_TYPE
358 && name == TYPE_IDENTIFIER (basetype)))
361 name = get_type_value (name);
364 my_friendly_abort (980605);
366 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
371 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
372 This is how virtual function calls are avoided. */
375 build_scoped_method_call (exp, basetype, name, parms)
376 tree exp, basetype, name, parms;
378 /* Because this syntactic form does not allow
379 a pointer to a base class to be `stolen',
380 we need not protect the derived->base conversion
383 @@ But we do have to check access privileges later. */
385 tree type = TREE_TYPE (exp);
387 if (type == error_mark_node
388 || basetype == error_mark_node)
389 return error_mark_node;
391 if (processing_template_decl)
393 if (TREE_CODE (name) == BIT_NOT_EXPR
394 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
396 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
398 name = build_min_nt (BIT_NOT_EXPR, type);
400 name = build_min_nt (SCOPE_REF, basetype, name);
401 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
404 if (TREE_CODE (type) == REFERENCE_TYPE)
405 type = TREE_TYPE (type);
407 if (TREE_CODE (basetype) == TREE_VEC)
410 basetype = BINFO_TYPE (binfo);
415 /* Check the destructor call syntax. */
416 if (TREE_CODE (name) == BIT_NOT_EXPR)
418 /* We can get here if someone writes their destructor call like
419 `obj.NS::~T()'; this isn't really a scoped method call, so hand
421 if (TREE_CODE (basetype) == NAMESPACE_DECL)
422 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
424 if (! check_dtor_name (basetype, name))
425 cp_error ("qualified type `%T' does not match destructor name `~%T'",
426 basetype, TREE_OPERAND (name, 0));
428 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
429 that explicit ~int is caught in the parser; this deals with typedefs
430 and template parms. */
431 if (! IS_AGGR_TYPE (basetype))
433 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
434 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
435 exp, basetype, type);
437 return cp_convert (void_type_node, exp);
441 if (! is_aggr_type (basetype, 1))
442 return error_mark_node;
444 if (! IS_AGGR_TYPE (type))
446 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
448 return error_mark_node;
453 binfo = get_binfo (basetype, type, 1);
454 if (binfo == error_mark_node)
455 return error_mark_node;
457 error_not_base_type (basetype, type);
462 if (TREE_CODE (exp) == INDIRECT_REF)
463 decl = build_indirect_ref
464 (convert_pointer_to_real
465 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
467 decl = build_scoped_ref (exp, basetype);
469 /* Call to a destructor. */
470 if (TREE_CODE (name) == BIT_NOT_EXPR)
472 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
473 return cp_convert (void_type_node, exp);
475 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
476 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
480 /* Call to a method. */
481 return build_method_call (decl, name, parms, binfo,
482 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
484 return error_mark_node;
487 /* We want the address of a function or method. We avoid creating a
488 pointer-to-member function. */
491 build_addr_func (function)
494 tree type = TREE_TYPE (function);
496 /* We have to do these by hand to avoid real pointer to member
498 if (TREE_CODE (type) == METHOD_TYPE)
502 type = build_pointer_type (type);
504 if (mark_addressable (function) == 0)
505 return error_mark_node;
507 addr = build1 (ADDR_EXPR, type, function);
509 /* Address of a static or external variable or function counts
511 if (staticp (function))
512 TREE_CONSTANT (addr) = 1;
517 function = default_conversion (function);
522 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
523 POINTER_TYPE to those. Note, pointer to member function types
524 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
527 build_call (function, result_type, parms)
528 tree function, result_type, parms;
530 int is_constructor = 0;
534 function = build_addr_func (function);
536 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
538 sorry ("unable to call pointer to member function here");
539 return error_mark_node;
542 if (TREE_CODE (function) == ADDR_EXPR
543 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
544 decl = TREE_OPERAND (function, 0);
548 if (decl && DECL_CONSTRUCTOR_P (decl))
551 /* Don't pass empty class objects by value. This is useful
552 for tags in STL, which are used to control overload resolution.
553 We don't need to handle other cases of copying empty classes. */
554 if (! decl || ! DECL_BUILT_IN (decl))
555 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
556 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
557 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
559 tree t = make_node (RTL_EXPR);
560 TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
561 RTL_EXPR_RTL (t) = const0_rtx;
562 RTL_EXPR_SEQUENCE (t) = NULL_RTX;
563 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
564 TREE_VALUE (tmp), t);
567 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
568 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
569 TREE_TYPE (function) = result_type;
570 TREE_SIDE_EFFECTS (function) = 1;
575 /* Build something of the form ptr->method (args)
576 or object.method (args). This can also build
577 calls to constructors, and find friends.
579 Member functions always take their class variable
582 INSTANCE is a class instance.
584 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
586 PARMS help to figure out what that NAME really refers to.
588 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
589 down to the real instance type to use for access checking. We need this
590 information to get protected accesses correct. This parameter is used
591 by build_member_call.
593 FLAGS is the logical disjunction of zero or more LOOKUP_
594 flags. See cp-tree.h for more info.
596 If this is all OK, calls build_function_call with the resolved
599 This function must also handle being called to perform
600 initialization, promotion/coercion of arguments, and
601 instantiation of default parameters.
603 Note that NAME may refer to an instance variable name. If
604 `operator()()' is defined for the type of that field, then we return
608 build_method_call (instance, name, parms, basetype_path, flags)
609 tree instance, name, parms, basetype_path;
612 tree basetype, instance_ptr;
614 #ifdef GATHER_STATISTICS
615 n_build_method_call++;
618 if (instance == error_mark_node
619 || name == error_mark_node
620 || parms == error_mark_node
621 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
622 return error_mark_node;
624 if (processing_template_decl)
626 /* We need to process template parm names here so that tsubst catches
627 them properly. Other type names can wait. */
628 if (TREE_CODE (name) == BIT_NOT_EXPR)
630 tree type = NULL_TREE;
632 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
633 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
634 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
635 type = TREE_TYPE (TREE_OPERAND (name, 0));
637 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
638 name = build_min_nt (BIT_NOT_EXPR, type);
641 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
644 /* This is the logic that magically deletes the second argument to
645 operator delete, if it is not needed. */
646 if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
648 tree save_last = TREE_CHAIN (parms);
650 /* get rid of unneeded argument */
651 TREE_CHAIN (parms) = NULL_TREE;
652 if (build_method_call (instance, name, parms, basetype_path,
653 (LOOKUP_SPECULATIVELY|flags) & ~LOOKUP_COMPLAIN))
655 /* If it finds a match, return it. */
656 return build_method_call (instance, name, parms, basetype_path, flags);
658 /* If it doesn't work, two argument delete must work */
659 TREE_CHAIN (parms) = save_last;
661 /* We already know whether it's needed or not for vec delete. */
662 else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
663 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
664 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
665 TREE_CHAIN (parms) = NULL_TREE;
667 if (TREE_CODE (name) == BIT_NOT_EXPR)
670 error ("destructors take no parameters");
671 basetype = TREE_TYPE (instance);
672 if (TREE_CODE (basetype) == REFERENCE_TYPE)
673 basetype = TREE_TYPE (basetype);
675 if (! check_dtor_name (basetype, name))
677 ("destructor name `~%T' does not match type `%T' of expression",
678 TREE_OPERAND (name, 0), basetype);
680 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
681 return cp_convert (void_type_node, instance);
682 instance = default_conversion (instance);
683 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
684 return build_delete (build_pointer_type (basetype),
685 instance_ptr, integer_two_node,
686 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
689 return build_new_method_call (instance, name, parms, basetype_path, flags);
692 /* New overloading code. */
702 struct z_candidate *next;
705 #define IDENTITY_RANK 0
711 #define ELLIPSIS_RANK 6
714 #define ICS_RANK(NODE) \
715 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
716 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
717 : ICS_USER_FLAG (NODE) ? USER_RANK \
718 : ICS_STD_RANK (NODE))
720 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
722 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
723 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
724 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
725 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
727 #define USER_CONV_CAND(NODE) \
728 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
729 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
736 || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE))
742 build_conv (code, type, from)
746 tree t = build1 (code, type, from);
747 int rank = ICS_STD_RANK (from);
759 if (rank < EXACT_RANK)
765 ICS_STD_RANK (t) = rank;
766 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
767 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
775 if (TREE_CODE (t) == REFERENCE_TYPE)
784 if (TREE_CODE (t) == ARRAY_TYPE)
786 return TYPE_MAIN_VARIANT (t);
789 /* Returns the standard conversion path (see [conv]) from type FROM to type
790 TO, if any. For proper handling of null pointer constants, you must
791 also pass the expression EXPR to convert from. */
794 standard_conversion (to, from, expr)
797 enum tree_code fcode, tcode;
801 if (TREE_CODE (to) == REFERENCE_TYPE)
803 if (TREE_CODE (from) == REFERENCE_TYPE)
806 from = TREE_TYPE (from);
808 to = strip_top_quals (to);
809 from = strip_top_quals (from);
811 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
812 && expr && type_unknown_p (expr))
814 expr = instantiate_type (to, expr, 0);
815 if (expr == error_mark_node)
817 from = TREE_TYPE (expr);
820 fcode = TREE_CODE (from);
821 tcode = TREE_CODE (to);
823 conv = build1 (IDENTITY_CONV, from, expr);
825 if (fcode == FUNCTION_TYPE)
827 from = build_pointer_type (from);
828 fcode = TREE_CODE (from);
829 conv = build_conv (LVALUE_CONV, from, conv);
831 else if (fcode == ARRAY_TYPE)
833 from = build_pointer_type (TREE_TYPE (from));
834 fcode = TREE_CODE (from);
835 conv = build_conv (LVALUE_CONV, from, conv);
837 else if (fromref || (expr && real_lvalue_p (expr)))
838 conv = build_conv (RVALUE_CONV, from, conv);
843 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
844 && expr && null_ptr_cst_p (expr))
846 conv = build_conv (STD_CONV, to, conv);
848 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
850 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
851 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
853 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
854 TYPE_MAIN_VARIANT (TREE_TYPE (to))))
856 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
857 && ufcode != FUNCTION_TYPE)
859 from = build_pointer_type
860 (cp_build_qualified_type (void_type_node,
861 CP_TYPE_QUALS (TREE_TYPE (from))));
862 conv = build_conv (PTR_CONV, from, conv);
864 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
866 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
867 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
869 if (DERIVED_FROM_P (fbase, tbase)
871 (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
872 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))))))
874 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
875 from = build_pointer_type (from);
876 conv = build_conv (PMEM_CONV, from, conv);
879 else if (IS_AGGR_TYPE (TREE_TYPE (from))
880 && IS_AGGR_TYPE (TREE_TYPE (to)))
882 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
885 cp_build_qualified_type (TREE_TYPE (to),
886 CP_TYPE_QUALS (TREE_TYPE (from)));
887 from = build_pointer_type (from);
888 conv = build_conv (PTR_CONV, from, conv);
892 if (same_type_p (from, to))
894 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
895 conv = build_conv (QUAL_CONV, to, conv);
896 else if (expr && string_conv_p (to, expr, 0))
897 /* converting from string constant to char *. */
898 conv = build_conv (QUAL_CONV, to, conv);
899 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
901 conv = build_conv (PTR_CONV, to, conv);
902 ICS_BAD_FLAG (conv) = 1;
909 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
911 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
912 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
913 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
914 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
916 if (! DERIVED_FROM_P (fbase, tbase)
917 || ! same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
918 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
919 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
920 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
923 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
924 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
925 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
926 from = build_ptrmemfunc_type (build_pointer_type (from));
927 conv = build_conv (PMEM_CONV, from, conv);
929 else if (tcode == BOOLEAN_TYPE)
931 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
932 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
935 conv = build_conv (STD_CONV, to, conv);
936 if (fcode == POINTER_TYPE
937 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
938 ICS_STD_RANK (conv) = PBOOL_RANK;
940 /* We don't check for ENUMERAL_TYPE here because there are no standard
941 conversions to enum type. */
942 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
943 || tcode == REAL_TYPE)
945 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
947 conv = build_conv (STD_CONV, to, conv);
949 /* Give this a better rank if it's a promotion. */
950 if (to == type_promotes_to (from)
951 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
952 ICS_STD_RANK (conv) = PROMO_RANK;
954 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
955 && DERIVED_FROM_P (to, from))
957 if (TREE_CODE (conv) == RVALUE_CONV)
958 conv = TREE_OPERAND (conv, 0);
959 conv = build_conv (BASE_CONV, to, conv);
967 /* Returns the conversion path from type FROM to reference type TO for
968 purposes of reference binding. For lvalue binding, either pass a
969 reference type to FROM or an lvalue expression to EXPR.
971 Currently does not distinguish in the generated trees between binding to
972 an lvalue and a temporary. Should it? */
975 reference_binding (rto, rfrom, expr, flags)
976 tree rto, rfrom, expr;
981 tree to = TREE_TYPE (rto);
985 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
987 expr = instantiate_type (to, expr, 0);
988 if (expr == error_mark_node)
990 from = TREE_TYPE (expr);
993 if (TREE_CODE (from) == REFERENCE_TYPE)
994 from = TREE_TYPE (from);
995 else if (! expr || ! real_lvalue_p (expr))
998 related = (same_type_p (TYPE_MAIN_VARIANT (to),
999 TYPE_MAIN_VARIANT (from))
1000 || (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
1001 && DERIVED_FROM_P (to, from)));
1003 if (lvalue && related && at_least_as_qualified_p (to, from))
1005 conv = build1 (IDENTITY_CONV, from, expr);
1007 if (same_type_p (TYPE_MAIN_VARIANT (to),
1008 TYPE_MAIN_VARIANT (from)))
1009 conv = build_conv (REF_BIND, rto, conv);
1012 conv = build_conv (REF_BIND, rto, conv);
1013 ICS_STD_RANK (conv) = STD_RANK;
1021 conv = standard_conversion (to, rfrom, expr);
1024 conv = build_conv (REF_BIND, rto, conv);
1026 /* Bind directly to a base subobject of a class rvalue. Do it
1027 after building the conversion for proper handling of ICS_RANK. */
1028 if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
1029 TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
1032 && ((! (CP_TYPE_CONST_NON_VOLATILE_P (to)
1033 && (flags & LOOKUP_NO_TEMP_BIND) == 0))
1034 /* If T1 is reference-related to T2, cv1 must be the same
1035 cv-qualification as, or greater cv-qualification than,
1036 cv2; otherwise, the program is ill-formed. */
1037 || (related && !at_least_as_qualified_p (to, from))))
1038 ICS_BAD_FLAG (conv) = 1;
1044 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1045 to type TO. The optional expression EXPR may affect the conversion.
1046 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1050 implicit_conversion (to, from, expr, flags)
1051 tree to, from, expr;
1055 struct z_candidate *cand;
1057 if (TREE_CODE (to) == REFERENCE_TYPE)
1058 conv = reference_binding (to, from, expr, flags);
1060 conv = standard_conversion (to, from, expr);
1064 else if (expr != NULL_TREE
1065 && (IS_AGGR_TYPE (non_reference (from))
1066 || IS_AGGR_TYPE (non_reference (to)))
1067 && (flags & LOOKUP_NO_CONVERSION) == 0)
1069 cand = build_user_type_conversion_1
1070 (to, expr, LOOKUP_ONLYCONVERTING);
1072 conv = cand->second_conv;
1073 if ((! conv || ICS_BAD_FLAG (conv))
1074 && TREE_CODE (to) == REFERENCE_TYPE
1075 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1077 cand = build_user_type_conversion_1
1078 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1081 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to)))
1082 ICS_BAD_FLAG (cand->second_conv) = 1;
1083 if (!conv || (ICS_BAD_FLAG (conv)
1084 > ICS_BAD_FLAG (cand->second_conv)))
1085 conv = build_conv (REF_BIND, to, cand->second_conv);
1093 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1096 static struct z_candidate *
1097 add_candidate (candidates, fn, convs, viable)
1098 struct z_candidate *candidates;
1102 struct z_candidate *cand
1103 = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
1106 cand->convs = convs;
1107 cand->second_conv = NULL_TREE;
1108 cand->viable = viable;
1109 cand->basetype_path = NULL_TREE;
1110 cand->template = NULL_TREE;
1111 cand->warnings = NULL_TREE;
1112 cand->next = candidates;
1117 /* Create an overload candidate for the function or method FN called with
1118 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1119 to implicit_conversion. */
1121 static struct z_candidate *
1122 add_function_candidate (candidates, fn, arglist, flags)
1123 struct z_candidate *candidates;
1127 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1130 tree parmnode = parmlist;
1131 tree argnode = arglist;
1134 /* The `this' and `in_chrg' arguments to constructors are not considered
1135 in overload resolution. */
1136 if (DECL_CONSTRUCTOR_P (fn))
1138 parmnode = TREE_CHAIN (parmnode);
1139 argnode = TREE_CHAIN (argnode);
1140 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1142 parmnode = TREE_CHAIN (parmnode);
1143 argnode = TREE_CHAIN (argnode);
1147 len = list_length (argnode);
1148 convs = make_scratch_vec (len);
1150 for (i = 0; i < len; ++i)
1152 tree arg = TREE_VALUE (argnode);
1153 tree argtype = lvalue_type (arg);
1156 if (parmnode == void_list_node)
1161 tree parmtype = TREE_VALUE (parmnode);
1163 /* [over.match.funcs] For conversion functions, the function is
1164 considered to be a member of the class of the implicit object
1165 argument for the purpose of defining the type of the implicit
1168 Since build_over_call ignores the ICS for the `this' parameter,
1169 we can just change the parm type. */
1170 if (DECL_CONV_FN_P (fn) && i == 0)
1173 = build_qualified_type (TREE_TYPE (argtype),
1174 TYPE_QUALS (TREE_TYPE (parmtype)));
1175 parmtype = build_pointer_type (parmtype);
1178 t = implicit_conversion (parmtype, argtype, arg, flags);
1182 t = build1 (IDENTITY_CONV, argtype, arg);
1183 ICS_ELLIPSIS_FLAG (t) = 1;
1186 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1187 && ! DECL_CONSTRUCTOR_P (fn))
1188 ICS_THIS_FLAG (t) = 1;
1190 TREE_VEC_ELT (convs, i) = t;
1194 if (ICS_BAD_FLAG (t))
1198 parmnode = TREE_CHAIN (parmnode);
1199 argnode = TREE_CHAIN (argnode);
1205 /* Make sure there are default args for the rest of the parms. */
1206 for (; parmnode && parmnode != void_list_node;
1207 parmnode = TREE_CHAIN (parmnode))
1208 if (! TREE_PURPOSE (parmnode))
1214 return add_candidate (candidates, fn, convs, viable);
1217 /* Create an overload candidate for the conversion function FN which will
1218 be invoked for expression OBJ, producing a pointer-to-function which
1219 will in turn be called with the argument list ARGLIST, and add it to
1220 CANDIDATES. FLAGS is passed on to implicit_conversion. */
1222 static struct z_candidate *
1223 add_conv_candidate (candidates, fn, obj, arglist)
1224 struct z_candidate *candidates;
1225 tree fn, obj, arglist;
1227 tree totype = TREE_TYPE (TREE_TYPE (fn));
1228 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1229 int i, len = list_length (arglist) + 1;
1230 tree convs = make_scratch_vec (len);
1231 tree parmnode = parmlist;
1232 tree argnode = arglist;
1234 int flags = LOOKUP_NORMAL;
1236 for (i = 0; i < len; ++i)
1238 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1239 tree argtype = lvalue_type (arg);
1243 t = implicit_conversion (totype, argtype, arg, flags);
1244 else if (parmnode == void_list_node)
1247 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1250 t = build1 (IDENTITY_CONV, argtype, arg);
1251 ICS_ELLIPSIS_FLAG (t) = 1;
1254 TREE_VEC_ELT (convs, i) = t;
1258 if (ICS_BAD_FLAG (t))
1265 parmnode = TREE_CHAIN (parmnode);
1266 argnode = TREE_CHAIN (argnode);
1272 for (; parmnode && parmnode != void_list_node;
1273 parmnode = TREE_CHAIN (parmnode))
1274 if (! TREE_PURPOSE (parmnode))
1280 return add_candidate (candidates, fn, convs, viable);
1283 static struct z_candidate *
1284 build_builtin_candidate (candidates, fnname, type1, type2,
1285 args, argtypes, flags)
1286 struct z_candidate *candidates;
1287 tree fnname, type1, type2, *args, *argtypes;
1298 convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1300 for (i = 0; i < 2; ++i)
1305 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1309 /* We need something for printing the candidate. */
1310 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1312 else if (ICS_BAD_FLAG (t))
1314 TREE_VEC_ELT (convs, i) = t;
1317 /* For COND_EXPR we rearranged the arguments; undo that now. */
1320 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1321 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1322 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1324 TREE_VEC_ELT (convs, 0) = t;
1329 return add_candidate (candidates, fnname, convs, viable);
1336 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1339 /* Create any builtin operator overload candidates for the operator in
1340 question given the converted operand types TYPE1 and TYPE2. The other
1341 args are passed through from add_builtin_candidates to
1342 build_builtin_candidate. */
1344 static struct z_candidate *
1345 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1346 args, argtypes, flags)
1347 struct z_candidate *candidates;
1348 enum tree_code code, code2;
1349 tree fnname, type1, type2, *args, *argtypes;
1354 case POSTINCREMENT_EXPR:
1355 case POSTDECREMENT_EXPR:
1356 args[1] = integer_zero_node;
1357 type2 = integer_type_node;
1366 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1367 and VQ is either volatile or empty, there exist candidate operator
1368 functions of the form
1369 VQ T& operator++(VQ T&);
1370 T operator++(VQ T&, int);
1371 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1372 type other than bool, and VQ is either volatile or empty, there exist
1373 candidate operator functions of the form
1374 VQ T& operator--(VQ T&);
1375 T operator--(VQ T&, int);
1376 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1377 complete object type, and VQ is either volatile or empty, there exist
1378 candidate operator functions of the form
1379 T*VQ& operator++(T*VQ&);
1380 T*VQ& operator--(T*VQ&);
1381 T* operator++(T*VQ&, int);
1382 T* operator--(T*VQ&, int); */
1384 case POSTDECREMENT_EXPR:
1385 case PREDECREMENT_EXPR:
1386 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1388 case POSTINCREMENT_EXPR:
1389 case PREINCREMENT_EXPR:
1390 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1391 || TYPE_PTROB_P (type1))
1393 type1 = build_reference_type (type1);
1398 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1399 exist candidate operator functions of the form
1403 8 For every function type T, there exist candidate operator functions of
1405 T& operator*(T*); */
1408 if (TREE_CODE (type1) == POINTER_TYPE
1409 && (TYPE_PTROB_P (type1)
1410 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1414 /* 9 For every type T, there exist candidate operator functions of the form
1417 10For every promoted arithmetic type T, there exist candidate operator
1418 functions of the form
1422 case CONVERT_EXPR: /* unary + */
1423 if (TREE_CODE (type1) == POINTER_TYPE
1424 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1427 if (ARITHMETIC_TYPE_P (type1))
1431 /* 11For every promoted integral type T, there exist candidate operator
1432 functions of the form
1436 if (INTEGRAL_TYPE_P (type1))
1440 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1441 is the same type as C2 or is a derived class of C2, T is a complete
1442 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1443 there exist candidate operator functions of the form
1444 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1445 where CV12 is the union of CV1 and CV2. */
1448 if (TREE_CODE (type1) == POINTER_TYPE
1449 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1451 tree c1 = TREE_TYPE (type1);
1452 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1453 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1454 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1456 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1457 && (TYPE_PTRMEMFUNC_P (type2)
1458 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1463 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1464 didate operator functions of the form
1469 bool operator<(L, R);
1470 bool operator>(L, R);
1471 bool operator<=(L, R);
1472 bool operator>=(L, R);
1473 bool operator==(L, R);
1474 bool operator!=(L, R);
1475 where LR is the result of the usual arithmetic conversions between
1478 14For every pair of types T and I, where T is a cv-qualified or cv-
1479 unqualified complete object type and I is a promoted integral type,
1480 there exist candidate operator functions of the form
1481 T* operator+(T*, I);
1482 T& operator[](T*, I);
1483 T* operator-(T*, I);
1484 T* operator+(I, T*);
1485 T& operator[](I, T*);
1487 15For every T, where T is a pointer to complete object type, there exist
1488 candidate operator functions of the form112)
1489 ptrdiff_t operator-(T, T);
1491 16For every pointer type T, there exist candidate operator functions of
1493 bool operator<(T, T);
1494 bool operator>(T, T);
1495 bool operator<=(T, T);
1496 bool operator>=(T, T);
1497 bool operator==(T, T);
1498 bool operator!=(T, T);
1500 17For every pointer to member type T, there exist candidate operator
1501 functions of the form
1502 bool operator==(T, T);
1503 bool operator!=(T, T); */
1506 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1508 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1510 type2 = ptrdiff_type_node;
1514 case TRUNC_DIV_EXPR:
1515 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1521 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1522 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1524 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1525 && null_ptr_cst_p (args[1]))
1530 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1531 && null_ptr_cst_p (args[0]))
1542 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1543 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1545 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1550 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1558 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1561 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1563 type1 = ptrdiff_type_node;
1566 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1568 type2 = ptrdiff_type_node;
1573 /* 18For every pair of promoted integral types L and R, there exist candi-
1574 date operator functions of the form
1581 where LR is the result of the usual arithmetic conversions between
1584 case TRUNC_MOD_EXPR:
1590 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1594 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1595 type, VQ is either volatile or empty, and R is a promoted arithmetic
1596 type, there exist candidate operator functions of the form
1597 VQ L& operator=(VQ L&, R);
1598 VQ L& operator*=(VQ L&, R);
1599 VQ L& operator/=(VQ L&, R);
1600 VQ L& operator+=(VQ L&, R);
1601 VQ L& operator-=(VQ L&, R);
1603 20For every pair T, VQ), where T is any type and VQ is either volatile
1604 or empty, there exist candidate operator functions of the form
1605 T*VQ& operator=(T*VQ&, T*);
1607 21For every pair T, VQ), where T is a pointer to member type and VQ is
1608 either volatile or empty, there exist candidate operator functions of
1610 VQ T& operator=(VQ T&, T);
1612 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1613 unqualified complete object type, VQ is either volatile or empty, and
1614 I is a promoted integral type, there exist candidate operator func-
1616 T*VQ& operator+=(T*VQ&, I);
1617 T*VQ& operator-=(T*VQ&, I);
1619 23For every triple L, VQ, R), where L is an integral or enumeration
1620 type, VQ is either volatile or empty, and R is a promoted integral
1621 type, there exist candidate operator functions of the form
1623 VQ L& operator%=(VQ L&, R);
1624 VQ L& operator<<=(VQ L&, R);
1625 VQ L& operator>>=(VQ L&, R);
1626 VQ L& operator&=(VQ L&, R);
1627 VQ L& operator^=(VQ L&, R);
1628 VQ L& operator|=(VQ L&, R); */
1635 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1637 type2 = ptrdiff_type_node;
1641 case TRUNC_DIV_EXPR:
1642 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1646 case TRUNC_MOD_EXPR:
1652 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1657 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1659 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1660 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1661 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1662 || ((TYPE_PTRMEMFUNC_P (type1)
1663 || TREE_CODE (type1) == POINTER_TYPE)
1664 && null_ptr_cst_p (args[1])))
1672 my_friendly_abort (367);
1674 type1 = build_reference_type (type1);
1678 /* Kludge around broken overloading rules whereby
1679 bool ? const char& : enum is ambiguous
1680 (between int and const char&). */
1681 flags |= LOOKUP_NO_TEMP_BIND;
1683 /* Extension: Support ?: of enumeral type. Hopefully this will not
1684 be an extension for long. */
1685 if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
1687 else if (TREE_CODE (type1) == ENUMERAL_TYPE
1688 || TREE_CODE (type2) == ENUMERAL_TYPE)
1690 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1692 if (TREE_CODE (type1) == TREE_CODE (type2)
1693 && (TREE_CODE (type1) == REFERENCE_TYPE
1694 || TREE_CODE (type1) == POINTER_TYPE
1695 || TYPE_PTRMEMFUNC_P (type1)
1696 || IS_AGGR_TYPE (type1)))
1698 if (TREE_CODE (type1) == REFERENCE_TYPE
1699 || TREE_CODE (type2) == REFERENCE_TYPE)
1701 if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
1702 && null_ptr_cst_p (args[1]))
1703 || IS_AGGR_TYPE (type1))
1708 if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
1709 && null_ptr_cst_p (args[0]))
1710 || IS_AGGR_TYPE (type2))
1718 my_friendly_abort (367);
1721 /* If we're dealing with two pointer types, we need candidates
1722 for both of them. */
1723 if (type2 && type1 != type2
1724 && TREE_CODE (type1) == TREE_CODE (type2)
1725 && (TREE_CODE (type1) == REFERENCE_TYPE
1726 || (TREE_CODE (type1) == POINTER_TYPE
1727 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1728 || TYPE_PTRMEMFUNC_P (type1)
1729 || IS_AGGR_TYPE (type1)))
1731 candidates = build_builtin_candidate
1732 (candidates, fnname, type1, type1, args, argtypes, flags);
1733 return build_builtin_candidate
1734 (candidates, fnname, type2, type2, args, argtypes, flags);
1737 return build_builtin_candidate
1738 (candidates, fnname, type1, type2, args, argtypes, flags);
1742 type_decays_to (type)
1745 if (TREE_CODE (type) == ARRAY_TYPE)
1746 return build_pointer_type (TREE_TYPE (type));
1747 if (TREE_CODE (type) == FUNCTION_TYPE)
1748 return build_pointer_type (type);
1752 /* There are three conditions of builtin candidates:
1754 1) bool-taking candidates. These are the same regardless of the input.
1755 2) pointer-pair taking candidates. These are generated for each type
1756 one of the input types converts to.
1757 3) arithmetic candidates. According to the WP, we should generate
1758 all of these, but I'm trying not to... */
1760 static struct z_candidate *
1761 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1762 struct z_candidate *candidates;
1763 enum tree_code code, code2;
1768 tree type, argtypes[3], types[2];
1770 for (i = 0; i < 3; ++i)
1773 argtypes[i] = lvalue_type (args[i]);
1775 argtypes[i] = NULL_TREE;
1780 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1781 and VQ is either volatile or empty, there exist candidate operator
1782 functions of the form
1783 VQ T& operator++(VQ T&); */
1785 case POSTINCREMENT_EXPR:
1786 case PREINCREMENT_EXPR:
1787 case POSTDECREMENT_EXPR:
1788 case PREDECREMENT_EXPR:
1793 /* 24There also exist candidate operator functions of the form
1794 bool operator!(bool);
1795 bool operator&&(bool, bool);
1796 bool operator||(bool, bool); */
1798 case TRUTH_NOT_EXPR:
1799 return build_builtin_candidate
1800 (candidates, fnname, boolean_type_node,
1801 NULL_TREE, args, argtypes, flags);
1803 case TRUTH_ORIF_EXPR:
1804 case TRUTH_ANDIF_EXPR:
1805 return build_builtin_candidate
1806 (candidates, fnname, boolean_type_node,
1807 boolean_type_node, args, argtypes, flags);
1818 types[0] = types[1] = NULL_TREE;
1820 for (i = 0; i < 2; ++i)
1824 else if (IS_AGGR_TYPE (argtypes[i]))
1826 tree convs = lookup_conversions (argtypes[i]);
1828 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1831 convs = lookup_conversions (argtypes[i]);
1833 if (code == COND_EXPR)
1835 if (real_lvalue_p (args[i]))
1836 types[i] = scratch_tree_cons
1837 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1839 types[i] = scratch_tree_cons
1840 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1846 for (; convs; convs = TREE_CHAIN (convs))
1848 type = TREE_TYPE (TREE_TYPE (TREE_VALUE (convs)));
1851 && (TREE_CODE (type) != REFERENCE_TYPE
1852 || CP_TYPE_CONST_P (TREE_TYPE (type))))
1855 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1856 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1858 type = non_reference (type);
1859 if (i != 0 || ! ref1)
1861 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1862 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1863 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1864 if (INTEGRAL_TYPE_P (type))
1865 type = type_promotes_to (type);
1868 if (! value_member (type, types[i]))
1869 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1874 if (code == COND_EXPR && real_lvalue_p (args[i]))
1875 types[i] = scratch_tree_cons
1876 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1877 type = non_reference (argtypes[i]);
1878 if (i != 0 || ! ref1)
1880 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1881 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1882 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1883 if (INTEGRAL_TYPE_P (type))
1884 type = type_promotes_to (type);
1886 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1890 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1893 for (type = types[1]; type; type = TREE_CHAIN (type))
1894 candidates = add_builtin_candidate
1895 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1896 TREE_VALUE (type), args, argtypes, flags);
1898 candidates = add_builtin_candidate
1899 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1900 NULL_TREE, args, argtypes, flags);
1907 /* If TMPL can be successfully instantiated as indicated by
1908 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
1910 TMPL is the template. EXPLICIT_TARGS are any explicit template
1911 arguments. ARGLIST is the arguments provided at the call-site.
1912 The RETURN_TYPE is the desired type for conversion operators. If
1913 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
1914 OBJ is supplied, FLAGS are ignored, and OBJ is as for
1915 add_conv_candidate. */
1917 static struct z_candidate*
1918 add_template_candidate_real (candidates, tmpl, explicit_targs,
1919 arglist, return_type, flags,
1921 struct z_candidate *candidates;
1922 tree tmpl, explicit_targs, arglist, return_type;
1925 unification_kind_t strict;
1927 int ntparms = DECL_NTPARMS (tmpl);
1928 tree targs = make_scratch_vec (ntparms);
1929 struct z_candidate *cand;
1933 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
1934 return_type, strict, NULL_TREE);
1939 fn = instantiate_template (tmpl, targs);
1940 if (fn == error_mark_node)
1943 if (obj != NULL_TREE)
1944 /* Aha, this is a conversion function. */
1945 cand = add_conv_candidate (candidates, fn, obj, arglist);
1947 cand = add_function_candidate (candidates, fn, arglist, flags);
1948 if (DECL_TI_TEMPLATE (fn) != tmpl)
1949 /* This situation can occur if a member template of a template
1950 class is specialized. Then, instantiate_template might return
1951 an instantiation of the specialization, in which case the
1952 DECL_TI_TEMPLATE field will point at the original
1953 specialization. For example:
1955 template <class T> struct S { template <class U> void f(U);
1956 template <> void f(int) {}; };
1960 Here, TMPL will be template <class U> S<double>::f(U).
1961 And, instantiate template will give us the specialization
1962 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
1963 for this will point at template <class T> template <> S<T>::f(int),
1964 so that we can find the definition. For the purposes of
1965 overload resolution, however, we want the original TMPL. */
1966 cand->template = tree_cons (tmpl, targs, NULL_TREE);
1968 cand->template = DECL_TEMPLATE_INFO (fn);
1974 static struct z_candidate *
1975 add_template_candidate (candidates, tmpl, explicit_targs,
1976 arglist, return_type, flags, strict)
1977 struct z_candidate *candidates;
1978 tree tmpl, explicit_targs, arglist, return_type;
1980 unification_kind_t strict;
1983 add_template_candidate_real (candidates, tmpl, explicit_targs,
1984 arglist, return_type, flags,
1989 static struct z_candidate *
1990 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
1991 struct z_candidate *candidates;
1992 tree tmpl, obj, arglist, return_type;
1995 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
1996 return_type, 0, obj, DEDUCE_CONV);
2002 struct z_candidate *cands;
2004 for (; cands; cands = cands->next)
2005 if (pedantic ? cands->viable == 1 : cands->viable)
2010 static struct z_candidate *
2011 splice_viable (cands)
2012 struct z_candidate *cands;
2014 struct z_candidate **p = &cands;
2018 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2031 /* Fix this to work on non-lvalues. */
2032 if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
2033 || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
2036 return build_unary_op (ADDR_EXPR, obj, 0);
2040 print_z_candidates (candidates)
2041 struct z_candidate *candidates;
2043 char *str = "candidates are:";
2044 for (; candidates; candidates = candidates->next)
2046 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2048 if (candidates->fn == ansi_opname [COND_EXPR])
2049 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2050 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2051 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2052 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2053 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2054 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2055 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2056 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2058 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2059 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2062 cp_error_at ("%s %+D%s", str, candidates->fn,
2063 candidates->viable == -1 ? " <near match>" : "");
2068 /* Returns the best overload candidate to perform the requested
2069 conversion. This function is used for three the overloading situations
2070 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2071 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2072 per [dcl.init.ref], so we ignore temporary bindings. */
2074 static struct z_candidate *
2075 build_user_type_conversion_1 (totype, expr, flags)
2079 struct z_candidate *candidates, *cand;
2080 tree fromtype = TREE_TYPE (expr);
2081 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2082 tree args = NULL_TREE;
2083 tree templates = NULL_TREE;
2085 if (IS_AGGR_TYPE (totype))
2086 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2087 if (IS_AGGR_TYPE (fromtype)
2088 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2089 convs = lookup_conversions (fromtype);
2092 flags |= LOOKUP_NO_CONVERSION;
2096 tree t = build_int_2 (0, 0);
2097 TREE_TYPE (t) = build_pointer_type (totype);
2098 args = build_scratch_list (NULL_TREE, expr);
2099 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2100 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2101 args = scratch_tree_cons (NULL_TREE, t, args);
2103 ctors = TREE_VALUE (ctors);
2105 for (; ctors; ctors = OVL_NEXT (ctors))
2107 tree ctor = OVL_CURRENT (ctors);
2108 if (DECL_NONCONVERTING_P (ctor))
2111 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2113 templates = scratch_tree_cons (NULL_TREE, ctor, templates);
2115 add_template_candidate (candidates, ctor,
2116 NULL_TREE, args, NULL_TREE, flags,
2120 candidates = add_function_candidate (candidates, ctor,
2125 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2126 candidates->basetype_path = TYPE_BINFO (totype);
2131 args = build_scratch_list (NULL_TREE, build_this (expr));
2133 for (; convs; convs = TREE_CHAIN (convs))
2135 tree fns = TREE_VALUE (convs);
2136 int convflags = LOOKUP_NO_CONVERSION;
2139 /* If we are called to convert to a reference type, we are trying to
2140 find an lvalue binding, so don't even consider temporaries. If
2141 we don't find an lvalue binding, the caller will try again to
2142 look for a temporary binding. */
2143 if (TREE_CODE (totype) == REFERENCE_TYPE)
2144 convflags |= LOOKUP_NO_TEMP_BIND;
2146 if (TREE_CODE (fns) != TEMPLATE_DECL)
2147 ics = implicit_conversion
2148 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2150 /* We can't compute this yet. */
2151 ics = error_mark_node;
2153 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2154 /* ignore the near match. */;
2156 for (; fns; fns = OVL_NEXT (fns))
2158 tree fn = OVL_CURRENT (fns);
2159 struct z_candidate *old_candidates = candidates;
2161 if (TREE_CODE (fn) == TEMPLATE_DECL)
2163 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2165 add_template_candidate (candidates, fn, NULL_TREE,
2166 args, totype, flags,
2170 candidates = add_function_candidate (candidates, fn,
2173 if (candidates != old_candidates)
2175 if (TREE_CODE (fn) == TEMPLATE_DECL)
2176 ics = implicit_conversion
2177 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2180 candidates->second_conv = ics;
2181 candidates->basetype_path = TREE_PURPOSE (convs);
2183 if (ics == NULL_TREE)
2184 candidates->viable = 0;
2185 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2186 candidates->viable = -1;
2191 if (! any_viable (candidates))
2194 if (flags & LOOKUP_COMPLAIN)
2196 if (candidates && ! candidates->next)
2197 /* say why this one won't work or try to be loose */;
2199 cp_error ("no viable candidates");
2206 candidates = splice_viable (candidates);
2207 cand = tourney (candidates);
2211 if (flags & LOOKUP_COMPLAIN)
2213 cp_error ("conversion from `%T' to `%T' is ambiguous",
2215 print_z_candidates (candidates);
2218 cand = candidates; /* any one will do */
2219 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2220 ICS_USER_FLAG (cand->second_conv) = 1;
2221 ICS_BAD_FLAG (cand->second_conv) = 1;
2226 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2227 p = &(TREE_OPERAND (*p, 0));
2229 /* Pedantically, normal function declarations are never considered
2230 to refer to template instantiations, so we only do this with
2232 if (flag_guiding_decls && templates && ! cand->template
2233 && !DECL_INITIAL (cand->fn)
2234 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2235 add_maybe_template (cand->fn, templates);
2239 (DECL_CONSTRUCTOR_P (cand->fn)
2240 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2241 expr, build_expr_ptr_wrapper (cand));
2242 ICS_USER_FLAG (cand->second_conv) = 1;
2243 if (cand->viable == -1)
2244 ICS_BAD_FLAG (cand->second_conv) = 1;
2250 build_user_type_conversion (totype, expr, flags)
2254 struct z_candidate *cand
2255 = build_user_type_conversion_1 (totype, expr, flags);
2259 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2260 return error_mark_node;
2261 return convert_from_reference (convert_like (cand->second_conv, expr));
2266 /* Do any initial processing on the arguments to a function call. */
2273 for (t = args; t; t = TREE_CHAIN (t))
2275 if (TREE_VALUE (t) == error_mark_node)
2276 return error_mark_node;
2277 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2279 error ("invalid use of void expression");
2280 return error_mark_node;
2282 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2283 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2289 build_new_function_call (fn, args)
2292 struct z_candidate *candidates = 0, *cand;
2293 tree explicit_targs = NULL_TREE;
2294 int template_only = 0;
2296 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2298 explicit_targs = TREE_OPERAND (fn, 1);
2299 fn = TREE_OPERAND (fn, 0);
2303 if (really_overloaded_fn (fn))
2306 tree templates = NULL_TREE;
2308 args = resolve_args (args);
2310 if (args == error_mark_node)
2311 return error_mark_node;
2313 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2315 tree t = OVL_FUNCTION (t1);
2316 if (TREE_CODE (t) == TEMPLATE_DECL)
2318 templates = scratch_tree_cons (NULL_TREE, t, templates);
2319 candidates = add_template_candidate
2320 (candidates, t, explicit_targs, args, NULL_TREE,
2321 LOOKUP_NORMAL, DEDUCE_CALL);
2323 else if (! template_only)
2324 candidates = add_function_candidate
2325 (candidates, t, args, LOOKUP_NORMAL);
2328 if (! any_viable (candidates))
2330 if (candidates && ! candidates->next)
2331 return build_function_call (candidates->fn, args);
2332 cp_error ("no matching function for call to `%D (%A)'",
2333 DECL_NAME (OVL_FUNCTION (fn)), args);
2335 print_z_candidates (candidates);
2336 return error_mark_node;
2338 candidates = splice_viable (candidates);
2339 cand = tourney (candidates);
2343 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2344 DECL_NAME (OVL_FUNCTION (fn)), args);
2345 print_z_candidates (candidates);
2346 return error_mark_node;
2349 /* Pedantically, normal function declarations are never considered
2350 to refer to template instantiations, so we only do this with
2352 if (flag_guiding_decls && templates && ! cand->template
2353 && ! DECL_INITIAL (cand->fn))
2354 add_maybe_template (cand->fn, templates);
2356 return build_over_call (cand, args, LOOKUP_NORMAL);
2359 /* This is not really overloaded. */
2360 fn = OVL_CURRENT (fn);
2362 return build_function_call (fn, args);
2366 build_object_call (obj, args)
2369 struct z_candidate *candidates = 0, *cand;
2370 tree fns, convs, mem_args = NULL_TREE;
2371 tree type = TREE_TYPE (obj);
2372 tree templates = NULL_TREE;
2374 if (TYPE_PTRMEMFUNC_P (type))
2376 /* It's no good looking for an overloaded operator() on a
2377 pointer-to-member-function. */
2378 cp_error ("pointer-to-member function %E cannot be called", obj);
2379 cp_error ("without an object; consider using .* or ->*");
2380 return error_mark_node;
2383 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2384 if (fns == error_mark_node)
2385 return error_mark_node;
2387 args = resolve_args (args);
2389 if (args == error_mark_node)
2390 return error_mark_node;
2394 tree base = TREE_PURPOSE (fns);
2395 mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2397 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2399 tree fn = OVL_CURRENT (fns);
2400 if (TREE_CODE (fn) == TEMPLATE_DECL)
2402 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2404 = add_template_candidate (candidates, fn, NULL_TREE,
2405 mem_args, NULL_TREE,
2406 LOOKUP_NORMAL, DEDUCE_CALL);
2409 candidates = add_function_candidate
2410 (candidates, fn, mem_args, LOOKUP_NORMAL);
2413 candidates->basetype_path = base;
2417 convs = lookup_conversions (type);
2419 for (; convs; convs = TREE_CHAIN (convs))
2421 tree fns = TREE_VALUE (convs);
2422 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2424 if ((TREE_CODE (totype) == POINTER_TYPE
2425 || TREE_CODE (totype) == REFERENCE_TYPE)
2426 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2427 for (; fns; fns = OVL_NEXT (fns))
2429 tree fn = OVL_CURRENT (fns);
2430 if (TREE_CODE (fn) == TEMPLATE_DECL)
2432 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2433 candidates = add_template_conv_candidate (candidates,
2440 candidates = add_conv_candidate (candidates, fn, obj, args);
2443 candidates->basetype_path = TREE_PURPOSE (convs);
2447 if (! any_viable (candidates))
2449 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2450 print_z_candidates (candidates);
2451 return error_mark_node;
2454 candidates = splice_viable (candidates);
2455 cand = tourney (candidates);
2459 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2460 print_z_candidates (candidates);
2461 return error_mark_node;
2464 if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2465 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2467 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2470 return build_function_call (obj, args);
2474 op_error (code, code2, arg1, arg2, arg3, problem)
2475 enum tree_code code, code2;
2476 tree arg1, arg2, arg3;
2480 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2485 cp_error ("%s for `%T ? %T : %T'", problem,
2486 error_type (arg1), error_type (arg2), error_type (arg3));
2488 case POSTINCREMENT_EXPR:
2489 case POSTDECREMENT_EXPR:
2490 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2493 cp_error ("%s for `%T[%T]'", problem,
2494 error_type (arg1), error_type (arg2));
2498 cp_error ("%s for `%T %s %T'", problem,
2499 error_type (arg1), opname, error_type (arg2));
2501 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2506 build_new_op (code, flags, arg1, arg2, arg3)
2507 enum tree_code code;
2509 tree arg1, arg2, arg3;
2511 struct z_candidate *candidates = 0, *cand;
2512 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
2513 enum tree_code code2 = NOP_EXPR;
2514 tree templates = NULL_TREE;
2517 if (arg1 == error_mark_node
2518 || arg2 == error_mark_node
2519 || arg3 == error_mark_node)
2520 return error_mark_node;
2522 /* This can happen if a template takes all non-type parameters, e.g.
2523 undeclared_template<1, 5, 72>a; */
2524 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
2526 cp_error ("`%D' must be declared before use", arg1);
2527 return error_mark_node;
2530 if (code == MODIFY_EXPR)
2532 code2 = TREE_CODE (arg3);
2534 fnname = ansi_assopname[code2];
2537 fnname = ansi_opname[code];
2543 case VEC_DELETE_EXPR:
2545 /* Use build_op_new_call and build_op_delete_call instead. */
2546 my_friendly_abort (981018);
2549 return build_object_call (arg1, arg2);
2555 /* The comma operator can have void args. */
2556 if (TREE_CODE (arg1) == OFFSET_REF)
2557 arg1 = resolve_offset_ref (arg1);
2558 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
2559 arg2 = resolve_offset_ref (arg2);
2560 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
2561 arg3 = resolve_offset_ref (arg3);
2563 if (code == COND_EXPR)
2565 if (arg2 == NULL_TREE
2566 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
2567 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
2568 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
2569 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
2572 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
2573 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
2576 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2577 arg2 = integer_zero_node;
2580 arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
2581 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
2583 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2585 arglist = build_scratch_list (NULL_TREE, arg1);
2587 fns = lookup_function_nonclass (fnname, arglist);
2589 if (fns && TREE_CODE (fns) == TREE_LIST)
2590 fns = TREE_VALUE (fns);
2591 for (; fns; fns = OVL_NEXT (fns))
2593 tree fn = OVL_CURRENT (fns);
2594 if (TREE_CODE (fn) == TEMPLATE_DECL)
2596 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2598 = add_template_candidate (candidates, fn, NULL_TREE,
2599 arglist, TREE_TYPE (fnname),
2600 flags, DEDUCE_CALL);
2603 candidates = add_function_candidate (candidates, fn, arglist, flags);
2606 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
2608 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
2609 if (fns == error_mark_node)
2617 tree basetype = TREE_PURPOSE (fns);
2618 mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
2619 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2621 tree fn = OVL_CURRENT (fns);
2624 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
2625 this_arglist = mem_arglist;
2627 this_arglist = arglist;
2629 if (TREE_CODE (fn) == TEMPLATE_DECL)
2631 /* A member template. */
2632 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2634 = add_template_candidate (candidates, fn, NULL_TREE,
2635 this_arglist, TREE_TYPE (fnname),
2636 flags, DEDUCE_CALL);
2639 candidates = add_function_candidate
2640 (candidates, fn, this_arglist, flags);
2643 candidates->basetype_path = basetype;
2650 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
2651 to know about two args; a builtin candidate will always have a first
2652 parameter of type bool. We'll handle that in
2653 build_builtin_candidate. */
2654 if (code == COND_EXPR)
2664 args[2] = NULL_TREE;
2667 candidates = add_builtin_candidates
2668 (candidates, code, code2, fnname, args, flags);
2671 if (! any_viable (candidates))
2675 case POSTINCREMENT_EXPR:
2676 case POSTDECREMENT_EXPR:
2677 /* Look for an `operator++ (int)'. If they didn't have
2678 one, then we fall back to the old way of doing things. */
2679 if (flags & LOOKUP_COMPLAIN)
2680 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
2681 fnname, opname_tab [code]);
2682 if (code == POSTINCREMENT_EXPR)
2683 code = PREINCREMENT_EXPR;
2685 code = PREDECREMENT_EXPR;
2686 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
2688 /* The caller will deal with these. */
2697 if (flags & LOOKUP_COMPLAIN)
2699 op_error (code, code2, arg1, arg2, arg3, "no match");
2700 print_z_candidates (candidates);
2702 return error_mark_node;
2704 candidates = splice_viable (candidates);
2705 cand = tourney (candidates);
2709 if (flags & LOOKUP_COMPLAIN)
2711 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
2712 print_z_candidates (candidates);
2714 return error_mark_node;
2717 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
2719 extern int warn_synth;
2721 && fnname == ansi_opname[MODIFY_EXPR]
2722 && DECL_ARTIFICIAL (cand->fn)
2724 && ! candidates->next->next)
2726 cp_warning ("using synthesized `%#D' for copy assignment",
2728 cp_warning_at (" where cfront would use `%#D'",
2730 ? candidates->next->fn
2734 /* Pedantically, normal function declarations are never considered
2735 to refer to template instantiations, so we only do this with
2737 if (flag_guiding_decls && templates && ! cand->template
2738 && ! DECL_INITIAL (cand->fn)
2739 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2740 add_maybe_template (cand->fn, templates);
2742 return build_over_call
2744 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
2745 ? mem_arglist : arglist,
2749 /* Check for comparison of different enum types. */
2758 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
2759 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
2760 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
2761 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
2763 cp_warning ("comparison between `%#T' and `%#T'",
2764 TREE_TYPE (arg1), TREE_TYPE (arg2));
2771 /* We need to strip any leading REF_BIND so that bitfields don't cause
2772 errors. This should not remove any important conversions, because
2773 builtins don't apply to class objects directly. */
2774 conv = TREE_VEC_ELT (cand->convs, 0);
2775 if (TREE_CODE (conv) == REF_BIND)
2776 conv = TREE_OPERAND (conv, 0);
2777 arg1 = convert_like (conv, arg1);
2779 arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
2781 arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
2787 return build_modify_expr (arg1, code2, arg2);
2790 return build_indirect_ref (arg1, "unary *");
2795 case TRUNC_DIV_EXPR:
2806 case TRUNC_MOD_EXPR:
2810 case TRUTH_ANDIF_EXPR:
2811 case TRUTH_ORIF_EXPR:
2812 return build_binary_op_nodefault (code, arg1, arg2, code);
2817 case TRUTH_NOT_EXPR:
2818 case PREINCREMENT_EXPR:
2819 case POSTINCREMENT_EXPR:
2820 case PREDECREMENT_EXPR:
2821 case POSTDECREMENT_EXPR:
2824 return build_unary_op (code, arg1, candidates != 0);
2827 return build_array_ref (arg1, arg2);
2830 return build_conditional_expr (arg1, arg2, arg3);
2833 return build_m_component_ref
2834 (build_indirect_ref (arg1, NULL_PTR), arg2);
2836 /* The caller will deal with these. */
2843 my_friendly_abort (367);
2848 /* Build up a call to operator new. This has to be handled differently
2849 from other operators in the way lookup is handled; first members are
2850 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
2851 TYPE is the type to be created. ARGS are any new-placement args.
2852 FLAGS are the usual overloading flags. */
2855 build_op_new_call (code, type, args, flags)
2856 enum tree_code code;
2860 tree fnname = ansi_opname[code];
2862 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
2863 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
2865 return build_method_call (build_dummy_object (type),
2866 fnname, args, NULL_TREE, flags);
2869 return build_new_function_call
2870 (lookup_function_nonclass (fnname, args), args);
2873 /* Build a call to operator delete. This has to be handled very specially,
2874 because the restrictions on what signatures match are different from all
2875 other call instances. For a normal delete, only a delete taking (void *)
2876 or (void *, size_t) is accepted. For a placement delete, only an exact
2877 match with the placement new is accepted.
2879 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
2880 ADDR is the pointer to be deleted. For placement delete, it is also
2881 used to determine what the corresponding new looked like.
2882 SIZE is the size of the memory block to be deleted.
2883 FLAGS are the usual overloading flags.
2884 PLACEMENT is the corresponding placement new call, or 0. */
2887 build_op_delete_call (code, addr, size, flags, placement)
2888 enum tree_code code;
2889 tree addr, size, placement;
2892 tree fn, fns, fnname, fntype, argtypes, args, type;
2894 if (addr == error_mark_node)
2895 return error_mark_node;
2897 type = TREE_TYPE (TREE_TYPE (addr));
2898 fnname = ansi_opname[code];
2900 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
2903 If the result of the lookup is ambiguous or inaccessible, or if
2904 the lookup selects a placement deallocation function, the
2905 program is ill-formed.
2907 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
2909 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
2910 if (fns == error_mark_node)
2911 return error_mark_node;
2916 if (fns == NULL_TREE)
2917 fns = lookup_name_nonclass (fnname);
2921 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
2923 /* Extract the function. */
2924 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
2925 /* Then the second parm type. */
2926 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
2928 /* Also the second argument. */
2929 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
2933 /* First try it without the size argument. */
2934 argtypes = void_list_node;
2938 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
2939 fntype = build_function_type (void_type_node, argtypes);
2941 /* Strip const and volatile from addr. */
2942 if (type != TYPE_MAIN_VARIANT (type))
2943 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
2945 fn = instantiate_type (fntype, fns, 0);
2947 if (fn != error_mark_node)
2949 if (TREE_CODE (fns) == TREE_LIST)
2950 /* Member functions. */
2951 enforce_access (TREE_PURPOSE (fns), fn);
2952 return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
2955 /* If we are doing placement delete we do nothing if we don't find a
2956 matching op delete. */
2960 /* Normal delete; now try to find a match including the size argument. */
2961 argtypes = tree_cons (NULL_TREE, ptr_type_node,
2962 tree_cons (NULL_TREE, sizetype, void_list_node));
2963 fntype = build_function_type (void_type_node, argtypes);
2965 fn = instantiate_type (fntype, fns, 0);
2967 if (fn != error_mark_node)
2969 if (TREE_CODE (fns) == TREE_LIST)
2970 /* Member functions. */
2971 enforce_access (TREE_PURPOSE (fns), fn);
2972 return build_function_call
2973 (fn, expr_tree_cons (NULL_TREE, addr,
2974 build_expr_list (NULL_TREE, size)));
2977 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
2978 destructor, in which case the error should be deferred
2979 until someone actually tries to delete one of these. */
2980 if (flags & LOOKUP_SPECULATIVELY)
2983 cp_error ("no suitable operator delete for `%T'", type);
2984 return error_mark_node;
2987 /* If the current scope isn't allowed to access DECL along
2988 BASETYPE_PATH, give an error. */
2991 enforce_access (basetype_path, decl)
2992 tree basetype_path, decl;
2994 tree access = compute_access (basetype_path, decl);
2996 if (access == access_private_node)
2998 cp_error_at ("`%+#D' is %s", decl,
2999 TREE_PRIVATE (decl) ? "private"
3000 : "from private base class");
3001 error ("within this context");
3003 else if (access == access_protected_node)
3005 cp_error_at ("`%+#D' %s", decl,
3006 TREE_PROTECTED (decl) ? "is protected"
3007 : "has protected accessibility");
3008 error ("within this context");
3012 /* Perform the conversions in CONVS on the expression EXPR. */
3015 convert_like (convs, expr)
3018 if (ICS_BAD_FLAG (convs)
3019 && TREE_CODE (convs) != USER_CONV
3020 && TREE_CODE (convs) != AMBIG_CONV)
3023 for (; t; t = TREE_OPERAND (t, 0))
3025 if (TREE_CODE (t) == USER_CONV)
3027 expr = convert_like (t, expr);
3030 else if (TREE_CODE (t) == AMBIG_CONV)
3031 return convert_like (t, expr);
3032 else if (TREE_CODE (t) == IDENTITY_CONV)
3035 return convert_for_initialization
3036 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3037 "conversion", NULL_TREE, 0);
3040 switch (TREE_CODE (convs))
3044 struct z_candidate *cand
3045 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3049 if (DECL_CONSTRUCTOR_P (fn))
3051 tree t = build_int_2 (0, 0);
3052 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3054 args = build_scratch_list (NULL_TREE, expr);
3055 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3056 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3057 args = scratch_tree_cons (NULL_TREE, t, args);
3060 args = build_this (expr);
3061 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3063 /* If this is a constructor or a function returning an aggr type,
3064 we need to build up a TARGET_EXPR. */
3065 if (DECL_CONSTRUCTOR_P (fn))
3066 expr = build_cplus_new (TREE_TYPE (convs), expr);
3071 if (type_unknown_p (expr))
3072 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3073 if (TREE_READONLY_DECL_P (expr))
3074 expr = decl_constant_value (expr);
3077 /* Call build_user_type_conversion again for the error. */
3078 return build_user_type_conversion
3079 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3085 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3086 if (expr == error_mark_node)
3087 return error_mark_node;
3089 switch (TREE_CODE (convs))
3092 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3094 /* else fall through */
3097 tree cvt_expr = build_user_type_conversion
3098 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3101 /* This can occur if, for example, the EXPR has incomplete
3102 type. We can't check for that before attempting the
3103 conversion because the type might be an incomplete
3104 array type, which is OK if some constructor for the
3105 destination type takes a pointer argument. */
3106 if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
3108 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
3109 incomplete_type_error (expr, TREE_TYPE (expr));
3111 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3112 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3115 cp_error ("could not convert `%E' to `%T'",
3116 expr, TREE_TYPE (convs));
3117 return error_mark_node;
3123 return convert_to_reference
3124 (TREE_TYPE (convs), expr,
3125 CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
3128 return decay_conversion (expr);
3131 /* Warn about deprecated conversion if appropriate. */
3132 string_conv_p (TREE_TYPE (convs), expr, 1);
3138 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3139 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3142 /* ARG is being passed to a varargs function. Perform any conversions
3143 required. Return the converted value. */
3146 convert_arg_to_ellipsis (arg)
3149 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3150 && (TYPE_PRECISION (TREE_TYPE (arg))
3151 < TYPE_PRECISION (double_type_node)))
3152 /* Convert `float' to `double'. */
3153 arg = cp_convert (double_type_node, arg);
3154 else if (IS_AGGR_TYPE (TREE_TYPE (arg))
3155 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (arg)))
3156 cp_warning ("cannot pass objects of type `%T' through `...'",
3159 /* Convert `short' and `char' to full-size `int'. */
3160 arg = default_conversion (arg);
3165 /* ARG is a default argument expression being passed to a parameter of
3166 the indicated TYPE. Do any required conversions. Return the
3170 convert_default_arg (type, arg)
3173 arg = break_out_target_exprs (arg);
3175 if (TREE_CODE (arg) == CONSTRUCTOR)
3177 arg = digest_init (type, arg, 0);
3178 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3179 "default argument", 0, 0);
3183 /* This could get clobbered by the following call. */
3184 if (TREE_HAS_CONSTRUCTOR (arg))
3185 arg = copy_node (arg);
3187 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3188 "default argument", 0, 0);
3189 #ifdef PROMOTE_PROTOTYPES
3190 if ((TREE_CODE (type) == INTEGER_TYPE
3191 || TREE_CODE (type) == ENUMERAL_TYPE)
3192 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3193 arg = default_conversion (arg);
3201 build_over_call (cand, args, flags)
3202 struct z_candidate *cand;
3207 tree convs = cand->convs;
3208 tree converted_args = NULL_TREE;
3209 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3210 tree conv, arg, val;
3214 /* Give any warnings we noticed during overload resolution. */
3216 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3217 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3219 if (DECL_FUNCTION_MEMBER_P (fn))
3220 enforce_access (cand->basetype_path, fn);
3222 if (args && TREE_CODE (args) != TREE_LIST)
3223 args = build_scratch_list (NULL_TREE, args);
3226 /* The implicit parameters to a constructor are not considered by overload
3227 resolution, and must be of the proper type. */
3228 if (DECL_CONSTRUCTOR_P (fn))
3230 converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3231 arg = TREE_CHAIN (arg);
3232 parm = TREE_CHAIN (parm);
3233 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3235 converted_args = expr_tree_cons
3236 (NULL_TREE, TREE_VALUE (arg), converted_args);
3237 arg = TREE_CHAIN (arg);
3238 parm = TREE_CHAIN (parm);
3241 /* Bypass access control for 'this' parameter. */
3242 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3244 tree parmtype = TREE_VALUE (parm);
3245 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3247 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3248 cp_error ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3249 TREE_TYPE (argtype), fn);
3251 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3252 X is called for an object that is not of type X, or of a type
3253 derived from X, the behavior is undefined.
3255 So we can assume that anything passed as 'this' is non-null, and
3256 optimize accordingly. */
3257 if (TREE_CODE (parmtype) == POINTER_TYPE)
3258 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3260 /* This happens with signatures. */
3261 t = convert_force (parmtype, TREE_VALUE (arg), CONV_C_CAST);
3262 converted_args = expr_tree_cons (NULL_TREE, t, converted_args);
3263 parm = TREE_CHAIN (parm);
3264 arg = TREE_CHAIN (arg);
3270 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3272 tree type = TREE_VALUE (parm);
3274 conv = TREE_VEC_ELT (convs, i);
3275 if (ICS_BAD_FLAG (conv))
3278 val = TREE_VALUE (arg);
3280 for (; t; t = TREE_OPERAND (t, 0))
3282 if (TREE_CODE (t) == USER_CONV
3283 || TREE_CODE (t) == AMBIG_CONV)
3285 val = convert_like (t, val);
3288 else if (TREE_CODE (t) == IDENTITY_CONV)
3291 val = convert_for_initialization
3292 (NULL_TREE, type, val, LOOKUP_NORMAL,
3293 "argument passing", fn, i - is_method);
3297 /* Issue warnings about peculiar, but legal, uses of NULL. */
3298 if (ARITHMETIC_TYPE_P (TREE_VALUE (parm))
3299 && TREE_VALUE (arg) == null_node)
3300 cp_warning ("converting NULL to non-pointer type");
3302 val = convert_like (conv, TREE_VALUE (arg));
3305 #ifdef PROMOTE_PROTOTYPES
3306 if ((TREE_CODE (type) == INTEGER_TYPE
3307 || TREE_CODE (type) == ENUMERAL_TYPE)
3308 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3309 val = default_conversion (val);
3311 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3314 /* Default arguments */
3315 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3317 tree arg = TREE_PURPOSE (parm);
3319 if (DECL_TEMPLATE_INFO (fn))
3321 /* This came from a template. Instantiate the default arg here,
3322 not in tsubst. In the case of something like:
3330 we must be careful to do name lookup in the scope of
3331 S<T>, rather than in the current class. */
3332 if (DECL_CLASS_SCOPE_P (fn))
3333 pushclass (DECL_REAL_CONTEXT (fn), 2);
3335 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), NULL_TREE);
3337 if (DECL_CLASS_SCOPE_P (fn))
3340 converted_args = expr_tree_cons
3341 (NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg),
3346 for (; arg; arg = TREE_CHAIN (arg))
3348 = expr_tree_cons (NULL_TREE,
3349 convert_arg_to_ellipsis (TREE_VALUE (arg)),
3352 converted_args = nreverse (converted_args);
3354 /* Avoid actually calling copy constructors and copy assignment operators,
3357 if (! flag_elide_constructors)
3358 /* Do things the hard way. */;
3359 else if (DECL_CONSTRUCTOR_P (fn)
3360 && TREE_VEC_LENGTH (convs) == 1
3361 && copy_args_p (fn))
3364 arg = TREE_CHAIN (converted_args);
3365 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3366 arg = TREE_CHAIN (arg);
3367 arg = TREE_VALUE (arg);
3369 /* Pull out the real argument, disregarding const-correctness. */
3371 while (TREE_CODE (targ) == NOP_EXPR
3372 || TREE_CODE (targ) == NON_LVALUE_EXPR
3373 || TREE_CODE (targ) == CONVERT_EXPR)
3374 targ = TREE_OPERAND (targ, 0);
3375 if (TREE_CODE (targ) == ADDR_EXPR)
3377 targ = TREE_OPERAND (targ, 0);
3378 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3379 TYPE_MAIN_VARIANT (TREE_TYPE (targ))))
3388 arg = build_indirect_ref (arg, 0);
3390 /* [class.copy]: the copy constructor is implicitly defined even if
3391 the implementation elided its use. */
3392 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3395 /* If we're creating a temp and we already have one, don't create a
3396 new one. If we're not creating a temp but we get one, use
3397 INIT_EXPR to collapse the temp into our target. Otherwise, if the
3398 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3399 temp or an INIT_EXPR otherwise. */
3400 if (integer_zerop (TREE_VALUE (args)))
3402 if (! real_lvalue_p (arg))
3404 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3406 val = build (VAR_DECL, DECL_CONTEXT (fn));
3407 layout_decl (val, 0);
3408 val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
3409 TREE_SIDE_EFFECTS (val) = 1;
3413 else if (! real_lvalue_p (arg)
3414 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3416 tree to = stabilize_reference
3417 (build_indirect_ref (TREE_VALUE (args), 0));
3419 /* Don't copy the padding byte; it might not have been allocated
3420 if to is a base subobject. */
3421 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3422 return build_unary_op
3423 (ADDR_EXPR, build (COMPOUND_EXPR, TREE_TYPE (to),
3424 cp_convert (void_type_node, arg), to),
3427 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
3428 TREE_SIDE_EFFECTS (val) = 1;
3429 return build_unary_op (ADDR_EXPR, val, 0);
3432 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
3434 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
3436 tree to = stabilize_reference
3437 (build_indirect_ref (TREE_VALUE (converted_args), 0));
3439 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
3441 /* Don't copy the padding byte; it might not have been allocated
3442 if to is a base subobject. */
3443 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3444 return build (COMPOUND_EXPR, TREE_TYPE (to),
3445 cp_convert (void_type_node, arg), to);
3447 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
3448 TREE_SIDE_EFFECTS (val) = 1;
3454 if (DECL_CLASS_SCOPE_P (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
3455 return build_signature_method_call (fn, converted_args);
3456 else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3458 tree t, *p = &TREE_VALUE (converted_args);
3459 tree binfo = get_binfo
3460 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
3461 *p = convert_pointer_to_real (binfo, *p);
3462 if (TREE_SIDE_EFFECTS (*p))
3463 *p = save_expr (*p);
3464 t = build_pointer_type (TREE_TYPE (fn));
3465 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
3468 else if (DECL_INLINE (fn))
3469 fn = inline_conversion (fn);
3471 fn = build_addr_func (fn);
3473 /* Recognize certain built-in functions so we can make tree-codes
3474 other than CALL_EXPR. We do this when it enables fold-const.c
3475 to do something useful. */
3477 if (TREE_CODE (fn) == ADDR_EXPR
3478 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3479 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
3480 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
3485 if (converted_args == 0)
3486 return integer_zero_node;
3487 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
3492 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3493 if (TREE_TYPE (fn) == void_type_node)
3495 fn = require_complete_type (fn);
3496 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
3497 fn = build_cplus_new (TREE_TYPE (fn), fn);
3498 return convert_from_reference (fn);
3502 build_new_method_call (instance, name, args, basetype_path, flags)
3503 tree instance, name, args, basetype_path;
3506 struct z_candidate *candidates = 0, *cand;
3507 tree explicit_targs = NULL_TREE;
3508 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
3510 tree user_args = args;
3511 tree templates = NULL_TREE;
3512 int template_only = 0;
3514 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3516 explicit_targs = TREE_OPERAND (name, 1);
3517 name = TREE_OPERAND (name, 0);
3518 if (TREE_CODE (name) == TEMPLATE_DECL)
3519 name = DECL_NAME (name);
3523 /* If there is an extra argument for controlling virtual bases,
3524 remove it for error reporting. */
3525 if (flags & LOOKUP_HAS_IN_CHARGE)
3526 user_args = TREE_CHAIN (args);
3528 args = resolve_args (args);
3530 if (args == error_mark_node)
3531 return error_mark_node;
3533 if (instance == NULL_TREE)
3534 basetype = BINFO_TYPE (basetype_path);
3537 if (TREE_CODE (instance) == OFFSET_REF)
3538 instance = resolve_offset_ref (instance);
3539 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3540 instance = convert_from_reference (instance);
3541 basetype = TREE_TYPE (instance);
3543 /* XXX this should be handled before we get here. */
3544 if (! IS_AGGR_TYPE (basetype)
3545 && ! (TYPE_LANG_SPECIFIC (basetype)
3546 && (IS_SIGNATURE_POINTER (basetype)
3547 || IS_SIGNATURE_REFERENCE (basetype))))
3549 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
3550 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
3551 name, instance, basetype);
3553 return error_mark_node;
3556 /* If `instance' is a signature pointer/reference and `name' is
3557 not a constructor, we are calling a signature member function.
3558 In that case set the `basetype' to the signature type. */
3559 if ((IS_SIGNATURE_POINTER (basetype)
3560 || IS_SIGNATURE_REFERENCE (basetype))
3561 && TYPE_IDENTIFIER (basetype) != name)
3562 basetype = SIGNATURE_TYPE (basetype);
3565 if (basetype_path == NULL_TREE)
3566 basetype_path = TYPE_BINFO (basetype);
3570 instance_ptr = build_this (instance);
3572 if (! template_only)
3574 /* XXX this should be handled before we get here. */
3575 fns = build_field_call (basetype_path, instance_ptr, name, args);
3582 instance_ptr = build_int_2 (0, 0);
3583 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
3587 = (name == ctor_identifier ? constructor_name (basetype) : name);
3589 fns = lookup_fnfields (basetype_path, name, 1);
3591 if (fns == error_mark_node)
3592 return error_mark_node;
3595 tree fn = TREE_VALUE (fns);
3596 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3597 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3599 flags |= LOOKUP_HAS_IN_CHARGE;
3600 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3602 mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
3603 for (; fn; fn = OVL_NEXT (fn))
3605 tree t = OVL_CURRENT (fn);
3608 /* We can end up here for copy-init of same or base class. */
3609 if (name == ctor_identifier
3610 && (flags & LOOKUP_ONLYCONVERTING)
3611 && DECL_NONCONVERTING_P (t))
3613 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
3614 this_arglist = mem_args;
3616 this_arglist = args;
3618 if (TREE_CODE (t) == TEMPLATE_DECL)
3620 /* A member template. */
3621 templates = scratch_tree_cons (NULL_TREE, t, templates);
3623 add_template_candidate (candidates, t, explicit_targs,
3625 TREE_TYPE (name), flags, DEDUCE_CALL);
3627 else if (! template_only)
3628 candidates = add_function_candidate (candidates, t,
3629 this_arglist, flags);
3632 candidates->basetype_path = TREE_PURPOSE (fns);
3636 if (! any_viable (candidates))
3638 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
3639 if (flags & LOOKUP_SPECULATIVELY)
3641 if (TYPE_SIZE (basetype) == 0)
3642 incomplete_type_error (instance_ptr, basetype);
3644 cp_error ("no matching function for call to `%T::%D (%A)%V'",
3645 basetype, pretty_name, user_args,
3646 TREE_TYPE (TREE_TYPE (instance_ptr)));
3647 print_z_candidates (candidates);
3648 return error_mark_node;
3650 candidates = splice_viable (candidates);
3651 cand = tourney (candidates);
3655 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
3657 print_z_candidates (candidates);
3658 return error_mark_node;
3661 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
3662 && instance == current_class_ref
3663 && DECL_CONSTRUCTOR_P (current_function_decl)
3664 && ! (flags & LOOKUP_NONVIRTUAL)
3665 && value_member (cand->fn, get_abstract_virtuals (basetype)))
3666 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
3667 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3668 && is_dummy_object (instance_ptr))
3669 cp_error ("cannot call member function `%D' without object", cand->fn);
3671 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
3672 && ((instance == current_class_ref && (dtor_label || ctor_label))
3673 || resolves_to_fixed_type_p (instance, 0)))
3674 flags |= LOOKUP_NONVIRTUAL;
3676 /* Pedantically, normal function declarations are never considered
3677 to refer to template instantiations, so we only do this with
3679 if (flag_guiding_decls && templates && ! cand->template
3680 && ! DECL_INITIAL (cand->fn))
3681 add_maybe_template (cand->fn, templates);
3683 return build_over_call
3685 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
3689 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
3690 subsequence of ICS2. */
3693 is_subseq (ics1, ics2)
3696 /* We can assume that a conversion of the same code
3697 between the same types indicates a subsequence since we only get
3698 here if the types we are converting from are the same. */
3700 while (TREE_CODE (ics1) == RVALUE_CONV
3701 || TREE_CODE (ics1) == LVALUE_CONV)
3702 ics1 = TREE_OPERAND (ics1, 0);
3706 while (TREE_CODE (ics2) == RVALUE_CONV
3707 || TREE_CODE (ics2) == LVALUE_CONV)
3708 ics2 = TREE_OPERAND (ics2, 0);
3710 if (TREE_CODE (ics2) == USER_CONV
3711 || TREE_CODE (ics2) == AMBIG_CONV
3712 || TREE_CODE (ics2) == IDENTITY_CONV)
3713 /* At this point, ICS1 cannot be a proper subsequence of
3714 ICS2. We can get a USER_CONV when we are comparing the
3715 second standard conversion sequence of two user conversion
3719 ics2 = TREE_OPERAND (ics2, 0);
3721 if (TREE_CODE (ics2) == TREE_CODE (ics1)
3722 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
3723 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
3724 TREE_TYPE (TREE_OPERAND (ics1, 0))))
3729 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
3730 be any _TYPE nodes. */
3733 is_properly_derived_from (derived, base)
3737 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
3738 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
3741 /* We only allow proper derivation here. The DERIVED_FROM_P macro
3742 considers every class derived from itself. */
3743 return (!same_type_p (TYPE_MAIN_VARIANT (derived),
3744 TYPE_MAIN_VARIANT (base))
3745 && DERIVED_FROM_P (base, derived));
3748 /* We build the ICS for an implicit object parameter as a pointer
3749 conversion sequence. However, such a sequence should be compared
3750 as if it were a reference conversion sequence. If ICS is the
3751 implicit conversion sequence for an implicit object parameter,
3752 modify it accordingly. */
3755 maybe_handle_implicit_object (ics)
3758 if (ICS_THIS_FLAG (*ics))
3760 /* [over.match.funcs]
3762 For non-static member functions, the type of the
3763 implicit object parameter is "reference to cv X"
3764 where X is the class of which the function is a
3765 member and cv is the cv-qualification on the member
3766 function declaration. */
3768 if (TREE_CODE (t) == QUAL_CONV)
3769 t = TREE_OPERAND (t, 0);
3770 if (TREE_CODE (t) == PTR_CONV)
3771 t = TREE_OPERAND (t, 0);
3772 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3773 t = build_conv (REF_BIND,
3774 build_reference_type (TREE_TYPE (TREE_TYPE (*ics))),
3776 ICS_STD_RANK (t) = ICS_STD_RANK (*ics);
3781 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
3782 to the type the reference originally referred to, and return 1.
3783 Otherwise, return 0. */
3786 maybe_handle_ref_bind (ics, target_type)
3790 if (TREE_CODE (*ics) == REF_BIND)
3794 When a parameter of reference type binds directly
3795 (_dcl.init.ref_) to an argument expression, the implicit
3796 conversion sequence is the identity conversion, unless the
3797 argument expression has a type that is a derived class of the
3798 parameter type, in which case the implicit conversion
3799 sequence is a derived-to-base Conversion.
3801 If the parameter binds directly to the result of applying a
3802 conversion function to the argument expression, the implicit
3803 conversion sequence is a user-defined conversion sequence
3804 (_over.ics.user_), with the second standard conversion
3805 sequence either an identity conversion or, if the conversion
3806 function returns an entity of a type that is a derived class
3807 of the parameter type, a derived-to-base Conversion.
3809 When a parameter of reference type is not bound directly to
3810 an argument expression, the conversion sequence is the one
3811 required to convert the argument expression to the underlying
3812 type of the reference according to _over.best.ics_.
3813 Conceptually, this conversion sequence corresponds to
3814 copy-initializing a temporary of the underlying type with the
3815 argument expression. Any difference in top-level
3816 cv-qualification is subsumed by the initialization itself and
3817 does not constitute a conversion. */
3819 tree old_ics = *ics;
3821 *target_type = TREE_TYPE (TREE_TYPE (*ics));
3822 *ics = TREE_OPERAND (*ics, 0);
3823 if (TREE_CODE (*ics) == IDENTITY_CONV
3824 && is_properly_derived_from (TREE_TYPE (*ics), *target_type))
3825 *ics = build_conv (BASE_CONV, *target_type, *ics);
3826 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
3827 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
3835 /* Compare two implicit conversion sequences according to the rules set out in
3836 [over.ics.rank]. Return values:
3838 1: ics1 is better than ics2
3839 -1: ics2 is better than ics1
3840 0: ics1 and ics2 are indistinguishable */
3843 compare_ics (ics1, ics2)
3850 tree deref_from_type1 = NULL_TREE;
3851 tree deref_from_type2 = NULL_TREE;
3852 tree deref_to_type1 = NULL_TREE;
3853 tree deref_to_type2 = NULL_TREE;
3855 /* REF_BINDING is non-zero if the result of the conversion sequence
3856 is a reference type. In that case TARGET_TYPE is the
3857 type referred to by the reference. */
3863 /* Handle implicit object parameters. */
3864 maybe_handle_implicit_object (&ics1);
3865 maybe_handle_implicit_object (&ics2);
3867 /* Handle reference parameters. */
3868 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
3869 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
3873 When comparing the basic forms of implicit conversion sequences (as
3874 defined in _over.best.ics_)
3876 --a standard conversion sequence (_over.ics.scs_) is a better
3877 conversion sequence than a user-defined conversion sequence
3878 or an ellipsis conversion sequence, and
3880 --a user-defined conversion sequence (_over.ics.user_) is a
3881 better conversion sequence than an ellipsis conversion sequence
3882 (_over.ics.ellipsis_). */
3883 if (ICS_RANK (ics1) > ICS_RANK (ics2))
3885 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
3888 if (ICS_RANK (ics1) == BAD_RANK)
3890 /* Both ICS are bad. We try to make a decision based on what
3891 would have happenned if they'd been good. */
3892 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
3893 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3895 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
3896 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3899 /* We couldn't make up our minds; try to figure it out below. */
3902 if (ICS_ELLIPSIS_FLAG (ics1))
3903 /* Both conversions are ellipsis conversions. */
3906 /* User-defined conversion sequence U1 is a better conversion sequence
3907 than another user-defined conversion sequence U2 if they contain the
3908 same user-defined conversion operator or constructor and if the sec-
3909 ond standard conversion sequence of U1 is better than the second
3910 standard conversion sequence of U2. */
3912 if (ICS_USER_FLAG (ics1))
3916 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
3917 if (TREE_CODE (t1) == AMBIG_CONV)
3919 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
3920 if (TREE_CODE (t2) == AMBIG_CONV)
3923 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
3926 /* We can just fall through here, after setting up
3927 FROM_TYPE1 and FROM_TYPE2. */
3928 from_type1 = TREE_TYPE (t1);
3929 from_type2 = TREE_TYPE (t2);
3933 /* We're dealing with two standard conversion sequences.
3937 Standard conversion sequence S1 is a better conversion
3938 sequence than standard conversion sequence S2 if
3940 --S1 is a proper subsequence of S2 (comparing the conversion
3941 sequences in the canonical form defined by _over.ics.scs_,
3942 excluding any Lvalue Transformation; the identity
3943 conversion sequence is considered to be a subsequence of
3944 any non-identity conversion sequence */
3947 while (TREE_CODE (from_type1) != IDENTITY_CONV)
3948 from_type1 = TREE_OPERAND (from_type1, 0);
3949 from_type1 = TREE_TYPE (from_type1);
3952 while (TREE_CODE (from_type2) != IDENTITY_CONV)
3953 from_type2 = TREE_OPERAND (from_type2, 0);
3954 from_type2 = TREE_TYPE (from_type2);
3957 if (same_type_p (from_type1, from_type2))
3959 if (is_subseq (ics1, ics2))
3961 if (is_subseq (ics2, ics1))
3964 /* Otherwise, one sequence cannot be a subsequence of the other; they
3965 don't start with the same type. This can happen when comparing the
3966 second standard conversion sequence in two user-defined conversion
3973 --the rank of S1 is better than the rank of S2 (by the rules
3976 Standard conversion sequences are ordered by their ranks: an Exact
3977 Match is a better conversion than a Promotion, which is a better
3978 conversion than a Conversion.
3980 Two conversion sequences with the same rank are indistinguishable
3981 unless one of the following rules applies:
3983 --A conversion that is not a conversion of a pointer, or pointer
3984 to member, to bool is better than another conversion that is such
3987 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
3988 so that we do not have to check it explicitly. */
3989 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3991 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
3994 to_type1 = TREE_TYPE (ics1);
3995 to_type2 = TREE_TYPE (ics2);
3997 if (TYPE_PTR_P (from_type1)
3998 && TYPE_PTR_P (from_type2)
3999 && TYPE_PTR_P (to_type1)
4000 && TYPE_PTR_P (to_type2))
4002 deref_from_type1 = TREE_TYPE (from_type1);
4003 deref_from_type2 = TREE_TYPE (from_type2);
4004 deref_to_type1 = TREE_TYPE (to_type1);
4005 deref_to_type2 = TREE_TYPE (to_type2);
4007 /* The rules for pointers to members A::* are just like the rules
4008 for pointers A*, except opposite: if B is derived from A then
4009 A::* converts to B::*, not vice versa. For that reason, we
4010 switch the from_ and to_ variables here. */
4011 else if (TYPE_PTRMEM_P (from_type1)
4012 && TYPE_PTRMEM_P (from_type2)
4013 && TYPE_PTRMEM_P (to_type1)
4014 && TYPE_PTRMEM_P (to_type2))
4016 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4017 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4018 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4019 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4021 else if (TYPE_PTRMEMFUNC_P (from_type1)
4022 && TYPE_PTRMEMFUNC_P (from_type2)
4023 && TYPE_PTRMEMFUNC_P (to_type1)
4024 && TYPE_PTRMEMFUNC_P (to_type2))
4026 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4027 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4028 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4029 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4032 if (deref_from_type1 != NULL_TREE
4033 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4034 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4036 /* This was one of the pointer or pointer-like conversions.
4040 --If class B is derived directly or indirectly from class A,
4041 conversion of B* to A* is better than conversion of B* to
4042 void*, and conversion of A* to void* is better than
4043 conversion of B* to void*. */
4044 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4045 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4047 if (is_properly_derived_from (deref_from_type1,
4050 else if (is_properly_derived_from (deref_from_type2,
4054 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4055 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4057 if (same_type_p (deref_from_type1, deref_from_type2))
4059 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4061 if (is_properly_derived_from (deref_from_type1,
4065 /* We know that DEREF_TO_TYPE1 is `void' here. */
4066 else if (is_properly_derived_from (deref_from_type1,
4071 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4072 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4076 --If class B is derived directly or indirectly from class A
4077 and class C is derived directly or indirectly from B,
4079 --conversion of C* to B* is better than conversion of C* to
4082 --conversion of B* to A* is better than conversion of C* to
4084 if (same_type_p (deref_from_type1, deref_from_type2))
4086 if (is_properly_derived_from (deref_to_type1,
4089 else if (is_properly_derived_from (deref_to_type2,
4093 else if (same_type_p (deref_to_type1, deref_to_type2))
4095 if (is_properly_derived_from (deref_from_type2,
4098 else if (is_properly_derived_from (deref_from_type1,
4104 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4105 && same_type_p (from_type1, from_type2))
4109 --binding of an expression of type C to a reference of type
4110 B& is better than binding an expression of type C to a
4111 reference of type A&
4113 --conversion of C to B is better than conversion of C to A, */
4114 if (is_properly_derived_from (from_type1, to_type1)
4115 && is_properly_derived_from (from_type1, to_type2))
4117 if (is_properly_derived_from (to_type1, to_type2))
4119 else if (is_properly_derived_from (to_type2, to_type1))
4123 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4124 && same_type_p (to_type1, to_type2))
4128 --binding of an expression of type B to a reference of type
4129 A& is better than binding an expression of type C to a
4130 reference of type A&,
4132 --onversion of B to A is better than conversion of C to A */
4133 if (is_properly_derived_from (from_type1, to_type1)
4134 && is_properly_derived_from (from_type2, to_type1))
4136 if (is_properly_derived_from (from_type2, from_type1))
4138 else if (is_properly_derived_from (from_type1, from_type2))
4145 --S1 and S2 differ only in their qualification conversion and yield
4146 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4147 qualification signature of type T1 is a proper subset of the cv-
4148 qualification signature of type T2 */
4149 if (TREE_CODE (ics1) == QUAL_CONV
4150 && TREE_CODE (ics2) == QUAL_CONV
4151 && same_type_p (from_type1, from_type2))
4152 return comp_cv_qual_signature (to_type1, to_type2);
4156 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4157 types to which the references refer are the same type except for
4158 top-level cv-qualifiers, and the type to which the reference
4159 initialized by S2 refers is more cv-qualified than the type to
4160 which the reference initialized by S1 refers */
4162 if (ref_binding1 && ref_binding2
4163 && same_type_p (TYPE_MAIN_VARIANT (to_type1),
4164 TYPE_MAIN_VARIANT (to_type2)))
4165 return comp_cv_qualification (target_type2, target_type1);
4167 /* Neither conversion sequence is better than the other. */
4171 /* The source type for this standard conversion sequence. */
4177 for (;; t = TREE_OPERAND (t, 0))
4179 if (TREE_CODE (t) == USER_CONV
4180 || TREE_CODE (t) == AMBIG_CONV
4181 || TREE_CODE (t) == IDENTITY_CONV)
4182 return TREE_TYPE (t);
4184 my_friendly_abort (1823);
4187 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4188 a pointer to LOSER and re-running joust to produce the warning if WINNER
4189 is actually used. */
4192 add_warning (winner, loser)
4193 struct z_candidate *winner, *loser;
4195 winner->warnings = expr_tree_cons (NULL_PTR,
4196 build_expr_ptr_wrapper (loser),
4200 /* Compare two candidates for overloading as described in
4201 [over.match.best]. Return values:
4203 1: cand1 is better than cand2
4204 -1: cand2 is better than cand1
4205 0: cand1 and cand2 are indistinguishable */
4208 joust (cand1, cand2, warn)
4209 struct z_candidate *cand1, *cand2;
4213 int i, off1 = 0, off2 = 0, len;
4215 /* Candidates that involve bad conversions are always worse than those
4217 if (cand1->viable > cand2->viable)
4219 if (cand1->viable < cand2->viable)
4222 /* a viable function F1
4223 is defined to be a better function than another viable function F2 if
4224 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4225 ICSi(F2), and then */
4227 /* for some argument j, ICSj(F1) is a better conversion sequence than
4230 /* For comparing static and non-static member functions, we ignore the
4231 implicit object parameter of the non-static function. The WP says to
4232 pretend that the static function has an object parm, but that won't
4233 work with operator overloading. */
4234 len = TREE_VEC_LENGTH (cand1->convs);
4235 if (len != TREE_VEC_LENGTH (cand2->convs))
4237 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4238 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4240 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4241 && DECL_STATIC_FUNCTION_P (cand2->fn))
4247 my_friendly_abort (42);
4250 for (i = 0; i < len; ++i)
4252 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4253 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4254 int comp = compare_ics (t1, t2);
4259 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4260 && TREE_CODE (t1) == STD_CONV
4261 && TREE_CODE (t2) == STD_CONV
4262 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4263 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4264 && (TYPE_PRECISION (TREE_TYPE (t1))
4265 == TYPE_PRECISION (TREE_TYPE (t2)))
4266 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4267 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4270 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4272 struct z_candidate *w, *l;
4274 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4275 w = cand1, l = cand2;
4277 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4278 w = cand2, l = cand1;
4282 cp_warning ("passing `%T' chooses `%T' over `%T'",
4283 type, type1, type2);
4284 cp_warning (" in call to `%D'", w->fn);
4290 if (winner && comp != winner)
4299 /* warn about confusing overload resolution for user-defined conversions,
4300 either between a constructor and a conversion op, or between two
4302 if (winner && cand1->second_conv
4303 && ((DECL_CONSTRUCTOR_P (cand1->fn)
4304 != DECL_CONSTRUCTOR_P (cand2->fn))
4305 /* Don't warn if the two conv ops convert to the same type... */
4306 || (! DECL_CONSTRUCTOR_P (cand1->fn)
4307 && ! same_type_p (TREE_TYPE (cand1->second_conv),
4308 TREE_TYPE (cand2->second_conv)))))
4310 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4313 struct z_candidate *w, *l;
4315 w = cand1, l = cand2;
4317 w = cand2, l = cand1;
4320 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4321 if (! DECL_CONSTRUCTOR_P (w->fn))
4322 source = TREE_TYPE (source);
4323 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4324 cp_warning (" for conversion from `%T' to `%T'",
4325 source, TREE_TYPE (w->second_conv));
4326 cp_warning (" because conversion sequence for the argument is better");
4337 F1 is a non-template function and F2 is a template function */
4339 if (! cand1->template && cand2->template)
4341 else if (cand1->template && ! cand2->template)
4343 else if (cand1->template && cand2->template)
4344 winner = more_specialized
4345 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4349 the context is an initialization by user-defined conversion (see
4350 _dcl.init_ and _over.match.user_) and the standard conversion
4351 sequence from the return type of F1 to the destination type (i.e.,
4352 the type of the entity being initialized) is a better conversion
4353 sequence than the standard conversion sequence from the return type
4354 of F2 to the destination type. */
4356 if (! winner && cand1->second_conv)
4357 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4359 /* If the built-in candidates are the same, arbitrarily pick one. */
4360 if (! winner && cand1->fn == cand2->fn
4361 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4363 for (i = 0; i < len; ++i)
4364 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4365 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
4367 if (i == TREE_VEC_LENGTH (cand1->convs))
4370 /* Kludge around broken overloading rules whereby
4371 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4372 that takes references and another that takes values. */
4373 if (cand1->fn == ansi_opname[COND_EXPR])
4375 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4376 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4377 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4378 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4380 if (same_type_p (t1, t2))
4382 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4384 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4392 /* Extension: If the worst conversion for one candidate is worse than the
4393 worst conversion for the other, take the first. */
4394 if (! winner && ! pedantic)
4396 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4398 for (i = 0; i < len; ++i)
4400 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4401 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4402 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
4403 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
4415 /* Given a list of candidates for overloading, find the best one, if any.
4416 This algorithm has a worst case of O(2n) (winner is last), and a best
4417 case of O(n/2) (totally ambiguous); much better than a sorting
4420 static struct z_candidate *
4421 tourney (candidates)
4422 struct z_candidate *candidates;
4424 struct z_candidate *champ = candidates, *challenger;
4426 int champ_compared_to_predecessor = 0;
4428 /* Walk through the list once, comparing each current champ to the next
4429 candidate, knocking out a candidate or two with each comparison. */
4431 for (challenger = champ->next; challenger; )
4433 fate = joust (champ, challenger, 0);
4435 challenger = challenger->next;
4440 champ = challenger->next;
4443 champ_compared_to_predecessor = 0;
4448 champ_compared_to_predecessor = 1;
4451 challenger = champ->next;
4455 /* Make sure the champ is better than all the candidates it hasn't yet
4456 been compared to. */
4458 for (challenger = candidates;
4460 && !(champ_compared_to_predecessor && challenger->next == champ);
4461 challenger = challenger->next)
4463 fate = joust (champ, challenger, 0);
4472 can_convert (to, from)
4475 tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
4476 return (t && ! ICS_BAD_FLAG (t));
4480 can_convert_arg (to, from, arg)
4483 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
4484 return (t && ! ICS_BAD_FLAG (t));