1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 92-97, 1998 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
39 extern int inhibit_warnings;
40 extern tree ctor_label, dtor_label;
42 static tree build_new_method_call PROTO((tree, tree, tree, tree, int));
44 static tree build_field_call PROTO((tree, tree, tree, tree));
45 static tree find_scoped_type PROTO((tree, tree, tree));
46 static struct z_candidate * tourney PROTO((struct z_candidate *));
47 static int joust PROTO((struct z_candidate *, struct z_candidate *, int));
48 static int compare_ics PROTO((tree, tree));
49 static tree build_over_call PROTO((struct z_candidate *, tree, int));
50 static tree convert_default_arg PROTO((tree, tree));
51 static tree convert_like PROTO((tree, tree));
52 static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
54 static tree build_object_call PROTO((tree, tree));
55 static tree resolve_args PROTO((tree));
56 static struct z_candidate * build_user_type_conversion_1
57 PROTO ((tree, tree, int));
58 static void print_z_candidates PROTO((struct z_candidate *));
59 static tree build_this PROTO((tree));
60 static struct z_candidate * splice_viable PROTO((struct z_candidate *));
61 static int any_viable PROTO((struct z_candidate *));
62 static struct z_candidate * add_template_candidate
63 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
65 static struct z_candidate * add_template_candidate_real
66 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
67 tree, unification_kind_t));
68 static struct z_candidate * add_template_conv_candidate
69 PROTO((struct z_candidate *, tree, tree, tree, tree));
70 static struct z_candidate * add_builtin_candidates
71 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
73 static struct z_candidate * add_builtin_candidate
74 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
75 tree, tree, tree, tree *, tree *, int));
76 static int is_complete PROTO((tree));
77 static struct z_candidate * build_builtin_candidate
78 PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
80 static struct z_candidate * add_conv_candidate
81 PROTO((struct z_candidate *, tree, tree, tree));
82 static struct z_candidate * add_function_candidate
83 PROTO((struct z_candidate *, tree, tree, int));
84 static tree implicit_conversion PROTO((tree, tree, tree, int));
85 static tree standard_conversion PROTO((tree, tree, tree));
86 static tree reference_binding PROTO((tree, tree, tree, int));
87 static tree strip_top_quals PROTO((tree));
88 static tree non_reference PROTO((tree));
89 static tree build_conv PROTO((enum tree_code, tree, tree));
90 static int is_subseq PROTO((tree, tree));
91 static int is_properly_derived_from PROTO((tree, tree));
92 static int maybe_handle_ref_bind PROTO((tree*, tree*));
93 static void maybe_handle_implicit_object PROTO((tree*));
96 build_vfield_ref (datum, type)
100 int old_assume_nonnull_objects = flag_assume_nonnull_objects;
102 if (datum == error_mark_node)
103 return error_mark_node;
105 /* Vtable references are always made from non-null objects. */
106 flag_assume_nonnull_objects = 1;
107 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
108 datum = convert_from_reference (datum);
110 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
111 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
112 datum, CLASSTYPE_VFIELD (type));
114 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
115 flag_assume_nonnull_objects = old_assume_nonnull_objects;
120 /* Build a call to a member of an object. I.e., one that overloads
121 operator ()(), or is a pointer-to-function or pointer-to-method. */
124 build_field_call (basetype_path, instance_ptr, name, parms)
125 tree basetype_path, instance_ptr, name, parms;
127 tree field, instance;
129 if (name == ctor_identifier || name == dtor_identifier)
132 if (instance_ptr == current_class_ptr)
134 /* Check to see if we really have a reference to an instance variable
135 with `operator()()' overloaded. */
136 field = IDENTIFIER_CLASS_VALUE (name);
138 if (field == NULL_TREE)
140 cp_error ("`this' has no member named `%D'", name);
141 return error_mark_node;
144 if (TREE_CODE (field) == FIELD_DECL)
146 /* If it's a field, try overloading operator (),
147 or calling if the field is a pointer-to-function. */
148 instance = build_component_ref_1 (current_class_ref, field, 0);
149 if (instance == error_mark_node)
150 return error_mark_node;
152 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance)))
153 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
155 if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
157 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
158 return build_function_call (instance, parms);
159 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
160 return build_function_call (instance, expr_tree_cons (NULL_TREE, current_class_ptr, parms));
166 /* Check to see if this is not really a reference to an instance variable
167 with `operator()()' overloaded. */
168 field = lookup_field (basetype_path, name, 1, 0);
170 /* This can happen if the reference was ambiguous or for access
172 if (field == error_mark_node)
173 return error_mark_node;
175 if (field && TREE_CODE (field) == FIELD_DECL)
178 tree ftype = TREE_TYPE (field);
180 if (TREE_CODE (ftype) == REFERENCE_TYPE)
181 ftype = TREE_TYPE (ftype);
183 if (TYPE_LANG_SPECIFIC (ftype))
185 /* Make the next search for this field very short. */
186 basetype = DECL_FIELD_CONTEXT (field);
187 instance_ptr = convert_pointer_to (basetype, instance_ptr);
189 instance = build_indirect_ref (instance_ptr, NULL_PTR);
190 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
191 build_component_ref_1 (instance, field, 0),
194 if (TREE_CODE (ftype) == POINTER_TYPE)
196 if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
197 || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
199 /* This is a member which is a pointer to function. */
201 = build_component_ref_1 (build_indirect_ref (instance_ptr,
203 field, LOOKUP_COMPLAIN);
204 if (ref == error_mark_node)
205 return error_mark_node;
206 return build_function_call (ref, parms);
209 else if (TREE_CODE (ftype) == METHOD_TYPE)
211 error ("invalid call via pointer-to-member function");
212 return error_mark_node;
221 find_scoped_type (type, inner_name, inner_types)
222 tree type, inner_name, inner_types;
224 tree tags = CLASSTYPE_TAGS (type);
228 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
229 enclosing class) is set to the name for the enum type. So, if
230 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
231 then this test will be true. */
232 if (TREE_PURPOSE (tags) == inner_name)
234 if (inner_types == NULL_TREE)
235 return TYPE_MAIN_DECL (TREE_VALUE (tags));
236 return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
238 tags = TREE_CHAIN (tags);
241 /* Look for a TYPE_DECL. */
242 for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
243 if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
245 /* Code by raeburn. */
246 if (inner_types == NULL_TREE)
248 return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
254 /* Resolve an expression NAME1::NAME2::...::NAMEn to
255 the name that names the above nested type. INNER_TYPES
256 is a chain of nested type names (held together by SCOPE_REFs);
257 OUTER_TYPE is the type we know to enclose INNER_TYPES.
258 Returns NULL_TREE if there is an error. */
261 resolve_scope_to_name (outer_type, inner_stuff)
262 tree outer_type, inner_stuff;
265 tree inner_name, inner_type;
267 if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
269 /* We first try to look for a nesting in our current class context,
270 then try any enclosing classes. */
271 tree type = current_class_type;
273 while (type && (TREE_CODE (type) == RECORD_TYPE
274 || TREE_CODE (type) == UNION_TYPE))
276 tree rval = resolve_scope_to_name (type, inner_stuff);
278 if (rval != NULL_TREE)
280 type = DECL_CONTEXT (TYPE_MAIN_DECL (type));
284 if (TREE_CODE (inner_stuff) == SCOPE_REF)
286 inner_name = TREE_OPERAND (inner_stuff, 0);
287 inner_type = TREE_OPERAND (inner_stuff, 1);
291 inner_name = inner_stuff;
292 inner_type = NULL_TREE;
295 if (outer_type == NULL_TREE)
298 /* If we have something that's already a type by itself,
300 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
303 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
308 x = lookup_name (inner_name, 0);
310 if (x && TREE_CODE (x) == NAMESPACE_DECL)
312 x = lookup_namespace_name (x, inner_type);
318 if (! IS_AGGR_TYPE (outer_type))
321 /* Look for member classes or enums. */
322 tmp = find_scoped_type (outer_type, inner_name, inner_type);
324 /* If it's not a type in this class, then go down into the
325 base classes and search there. */
326 if (! tmp && TYPE_BINFO (outer_type))
328 tree binfos = TYPE_BINFO_BASETYPES (outer_type);
329 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
331 for (i = 0; i < n_baselinks; i++)
333 tree base_binfo = TREE_VEC_ELT (binfos, i);
334 tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
344 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
345 This is how virtual function calls are avoided. */
348 build_scoped_method_call (exp, basetype, name, parms)
349 tree exp, basetype, name, parms;
351 /* Because this syntactic form does not allow
352 a pointer to a base class to be `stolen',
353 we need not protect the derived->base conversion
356 @@ But we do have to check access privileges later. */
358 tree type = TREE_TYPE (exp);
360 if (type == error_mark_node
361 || basetype == error_mark_node)
362 return error_mark_node;
364 if (processing_template_decl)
366 if (TREE_CODE (name) == BIT_NOT_EXPR)
368 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
370 name = build_min_nt (BIT_NOT_EXPR, type);
372 name = build_min_nt (SCOPE_REF, basetype, name);
373 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
376 if (TREE_CODE (type) == REFERENCE_TYPE)
377 type = TREE_TYPE (type);
379 if (TREE_CODE (basetype) == TREE_VEC)
382 basetype = BINFO_TYPE (binfo);
387 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
388 that explicit ~int is caught in the parser; this deals with typedefs
389 and template parms. */
390 if (TREE_CODE (name) == BIT_NOT_EXPR && ! IS_AGGR_TYPE (basetype))
392 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
393 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
394 exp, basetype, type);
395 name = TREE_OPERAND (name, 0);
396 if (TYPE_MAIN_VARIANT (basetype) != name
397 && (TYPE_MAIN_VARIANT (basetype)
398 != TYPE_MAIN_VARIANT (get_type_value (name))))
399 cp_error ("qualified type `%T' does not match destructor name `~%T'",
401 return cp_convert (void_type_node, exp);
404 if (! is_aggr_type (basetype, 1))
405 return error_mark_node;
407 if (! IS_AGGR_TYPE (type))
409 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
411 return error_mark_node;
416 binfo = get_binfo (basetype, type, 1);
417 if (binfo == error_mark_node)
418 return error_mark_node;
420 error_not_base_type (basetype, type);
425 if (TREE_CODE (exp) == INDIRECT_REF)
426 decl = build_indirect_ref
427 (convert_pointer_to_real
428 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
430 decl = build_scoped_ref (exp, basetype);
432 /* Call to a destructor. */
433 if (TREE_CODE (name) == BIT_NOT_EXPR)
435 /* Explicit call to destructor. */
436 name = TREE_OPERAND (name, 0);
437 if (! (name == TYPE_MAIN_VARIANT (TREE_TYPE (decl))
438 || name == constructor_name (TREE_TYPE (decl))
439 || TREE_TYPE (decl) == get_type_value (name)))
442 ("qualified type `%T' does not match destructor name `~%T'",
443 TREE_TYPE (decl), name);
444 return error_mark_node;
446 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
447 return cp_convert (void_type_node, exp);
449 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
450 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
454 /* Call to a method. */
455 return build_method_call (decl, name, parms, binfo,
456 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
458 return error_mark_node;
461 /* We want the address of a function or method. We avoid creating a
462 pointer-to-member function. */
465 build_addr_func (function)
468 tree type = TREE_TYPE (function);
470 /* We have to do these by hand to avoid real pointer to member
472 if (TREE_CODE (type) == METHOD_TYPE)
476 type = build_pointer_type (type);
478 if (mark_addressable (function) == 0)
479 return error_mark_node;
481 addr = build1 (ADDR_EXPR, type, function);
483 /* Address of a static or external variable or function counts
485 if (staticp (function))
486 TREE_CONSTANT (addr) = 1;
491 function = default_conversion (function);
496 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
497 POINTER_TYPE to those. Note, pointer to member function types
498 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
501 build_call (function, result_type, parms)
502 tree function, result_type, parms;
504 int is_constructor = 0;
508 function = build_addr_func (function);
510 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
512 sorry ("unable to call pointer to member function here");
513 return error_mark_node;
516 if (TREE_CODE (function) == ADDR_EXPR
517 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
518 decl = TREE_OPERAND (function, 0);
522 if (decl && DECL_CONSTRUCTOR_P (decl))
525 /* Don't pass empty class objects by value. This is useful
526 for tags in STL, which are used to control overload resolution.
527 We don't need to handle other cases of copying empty classes. */
528 if (! decl || ! DECL_BUILT_IN (decl))
529 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
530 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
531 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
533 tree t = make_node (RTL_EXPR);
534 TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
535 RTL_EXPR_RTL (t) = const0_rtx;
536 RTL_EXPR_SEQUENCE (t) = NULL_RTX;
537 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
538 TREE_VALUE (tmp), t);
541 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
542 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
543 TREE_TYPE (function) = result_type;
544 TREE_SIDE_EFFECTS (function) = 1;
549 /* Build something of the form ptr->method (args)
550 or object.method (args). This can also build
551 calls to constructors, and find friends.
553 Member functions always take their class variable
556 INSTANCE is a class instance.
558 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
560 PARMS help to figure out what that NAME really refers to.
562 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
563 down to the real instance type to use for access checking. We need this
564 information to get protected accesses correct. This parameter is used
565 by build_member_call.
567 FLAGS is the logical disjunction of zero or more LOOKUP_
568 flags. See cp-tree.h for more info.
570 If this is all OK, calls build_function_call with the resolved
573 This function must also handle being called to perform
574 initialization, promotion/coercion of arguments, and
575 instantiation of default parameters.
577 Note that NAME may refer to an instance variable name. If
578 `operator()()' is defined for the type of that field, then we return
582 build_method_call (instance, name, parms, basetype_path, flags)
583 tree instance, name, parms, basetype_path;
586 tree basetype, instance_ptr;
588 #ifdef GATHER_STATISTICS
589 n_build_method_call++;
592 if (instance == error_mark_node
593 || name == error_mark_node
594 || parms == error_mark_node
595 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
596 return error_mark_node;
598 if (processing_template_decl)
600 if (TREE_CODE (name) == BIT_NOT_EXPR)
602 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
604 name = build_min_nt (BIT_NOT_EXPR, type);
607 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
610 /* This is the logic that magically deletes the second argument to
611 operator delete, if it is not needed. */
612 if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
614 tree save_last = TREE_CHAIN (parms);
616 /* get rid of unneeded argument */
617 TREE_CHAIN (parms) = NULL_TREE;
618 if (build_method_call (instance, name, parms, basetype_path,
619 (LOOKUP_SPECULATIVELY|flags) & ~LOOKUP_COMPLAIN))
621 /* If it finds a match, return it. */
622 return build_method_call (instance, name, parms, basetype_path, flags);
624 /* If it doesn't work, two argument delete must work */
625 TREE_CHAIN (parms) = save_last;
627 /* We already know whether it's needed or not for vec delete. */
628 else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
629 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
630 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
631 TREE_CHAIN (parms) = NULL_TREE;
633 if (TREE_CODE (name) == BIT_NOT_EXPR)
635 flags |= LOOKUP_DESTRUCTOR;
636 name = TREE_OPERAND (name, 0);
638 error ("destructors take no parameters");
639 basetype = TREE_TYPE (instance);
640 if (TREE_CODE (basetype) == REFERENCE_TYPE)
641 basetype = TREE_TYPE (basetype);
642 if (! (name == TYPE_MAIN_VARIANT (basetype)
643 || (IS_AGGR_TYPE (basetype)
644 && name == constructor_name (basetype))
645 || (TYPE_MAIN_VARIANT (basetype)
646 == TYPE_MAIN_VARIANT (get_type_value (name)))))
648 cp_error ("destructor name `~%D' does not match type `%T' of expression",
650 return cp_convert (void_type_node, instance);
653 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
654 return cp_convert (void_type_node, instance);
655 instance = default_conversion (instance);
656 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
657 return build_delete (build_pointer_type (basetype),
658 instance_ptr, integer_two_node,
659 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
662 return build_new_method_call (instance, name, parms, basetype_path, flags);
665 /* New overloading code. */
675 struct z_candidate *next;
678 #define IDENTITY_RANK 0
684 #define ELLIPSIS_RANK 6
687 #define ICS_RANK(NODE) \
688 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
689 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
690 : ICS_USER_FLAG (NODE) ? USER_RANK \
691 : ICS_STD_RANK (NODE))
693 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
695 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
696 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
697 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
698 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
700 #define USER_CONV_CAND(NODE) \
701 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
702 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
709 || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE))
715 build_conv (code, type, from)
719 tree t = build1 (code, type, from);
720 int rank = ICS_STD_RANK (from);
732 if (rank < EXACT_RANK)
738 ICS_STD_RANK (t) = rank;
739 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
740 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
748 if (TREE_CODE (t) == REFERENCE_TYPE)
757 if (TREE_CODE (t) == ARRAY_TYPE)
759 return TYPE_MAIN_VARIANT (t);
762 /* Returns the standard conversion path (see [conv]) from type FROM to type
763 TO, if any. For proper handling of null pointer constants, you must
764 also pass the expression EXPR to convert from. */
767 standard_conversion (to, from, expr)
770 enum tree_code fcode, tcode;
774 if (TREE_CODE (to) == REFERENCE_TYPE)
776 if (TREE_CODE (from) == REFERENCE_TYPE)
779 from = TREE_TYPE (from);
781 to = strip_top_quals (to);
782 from = strip_top_quals (from);
784 fcode = TREE_CODE (from);
785 tcode = TREE_CODE (to);
787 conv = build1 (IDENTITY_CONV, from, expr);
789 if (fcode == FUNCTION_TYPE)
791 from = build_pointer_type (from);
792 fcode = TREE_CODE (from);
793 conv = build_conv (LVALUE_CONV, from, conv);
795 else if (fcode == ARRAY_TYPE)
797 from = build_pointer_type (TREE_TYPE (from));
798 fcode = TREE_CODE (from);
799 conv = build_conv (LVALUE_CONV, from, conv);
801 else if (fromref || (expr && real_lvalue_p (expr)))
802 conv = build_conv (RVALUE_CONV, from, conv);
807 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
808 && expr && null_ptr_cst_p (expr))
810 conv = build_conv (STD_CONV, to, conv);
812 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
814 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
815 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
817 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
818 TYPE_MAIN_VARIANT (TREE_TYPE (to)), 1))
820 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
821 && ufcode != FUNCTION_TYPE)
823 from = build_pointer_type
824 (cp_build_type_variant (void_type_node,
825 TYPE_READONLY (TREE_TYPE (from)),
826 TYPE_VOLATILE (TREE_TYPE (from))));
827 conv = build_conv (PTR_CONV, from, conv);
829 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
831 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
832 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
834 if (DERIVED_FROM_P (fbase, tbase)
835 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
836 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))),
839 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
840 from = build_pointer_type (from);
841 conv = build_conv (PMEM_CONV, from, conv);
844 else if (IS_AGGR_TYPE (TREE_TYPE (from))
845 && IS_AGGR_TYPE (TREE_TYPE (to)))
847 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
849 from = cp_build_type_variant (TREE_TYPE (to),
850 TYPE_READONLY (TREE_TYPE (from)),
851 TYPE_VOLATILE (TREE_TYPE (from)));
852 from = build_pointer_type (from);
853 conv = build_conv (PTR_CONV, from, conv);
857 if (comptypes (from, to, 1))
859 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
860 conv = build_conv (QUAL_CONV, to, conv);
861 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
863 conv = build_conv (PTR_CONV, to, conv);
864 ICS_BAD_FLAG (conv) = 1;
871 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
873 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
874 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
875 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
876 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
878 if (! DERIVED_FROM_P (fbase, tbase)
879 || ! comptypes (TREE_TYPE (fromfn), TREE_TYPE (tofn), 1)
880 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
881 TREE_CHAIN (TYPE_ARG_TYPES (tofn)), 1)
882 || TYPE_READONLY (fbase) != TYPE_READONLY (tbase)
883 || TYPE_VOLATILE (fbase) != TYPE_VOLATILE (tbase))
886 from = cp_build_type_variant (tbase, TYPE_READONLY (fbase),
887 TYPE_VOLATILE (fbase));
888 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
889 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
890 from = build_ptrmemfunc_type (build_pointer_type (from));
891 conv = build_conv (PMEM_CONV, from, conv);
893 else if (tcode == BOOLEAN_TYPE)
895 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
896 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
899 conv = build_conv (STD_CONV, to, conv);
900 if (fcode == POINTER_TYPE
901 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
902 ICS_STD_RANK (conv) = PBOOL_RANK;
904 /* We don't check for ENUMERAL_TYPE here because there are no standard
905 conversions to enum type. */
906 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
907 || tcode == REAL_TYPE)
909 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
911 conv = build_conv (STD_CONV, to, conv);
913 /* Give this a better rank if it's a promotion. */
914 if (to == type_promotes_to (from)
915 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
916 ICS_STD_RANK (conv) = PROMO_RANK;
918 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
919 && DERIVED_FROM_P (to, from))
921 if (TREE_CODE (conv) == RVALUE_CONV)
922 conv = TREE_OPERAND (conv, 0);
923 conv = build_conv (BASE_CONV, to, conv);
931 /* Returns the conversion path from type FROM to reference type TO for
932 purposes of reference binding. For lvalue binding, either pass a
933 reference type to FROM or an lvalue expression to EXPR.
935 Currently does not distinguish in the generated trees between binding to
936 an lvalue and a temporary. Should it? */
939 reference_binding (rto, rfrom, expr, flags)
940 tree rto, rfrom, expr;
945 tree to = TREE_TYPE (rto);
949 if (TREE_CODE (from) == REFERENCE_TYPE)
950 from = TREE_TYPE (from);
951 else if (! expr || ! real_lvalue_p (expr))
954 related = (comptypes (TYPE_MAIN_VARIANT (to),
955 TYPE_MAIN_VARIANT (from), 1)
956 || (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
957 && DERIVED_FROM_P (to, from)));
959 if (lvalue && related
960 && TYPE_READONLY (to) >= TYPE_READONLY (from)
961 && TYPE_VOLATILE (to) >= TYPE_VOLATILE (from))
963 conv = build1 (IDENTITY_CONV, from, expr);
965 if (comptypes (TYPE_MAIN_VARIANT (to),
966 TYPE_MAIN_VARIANT (from), 1))
967 conv = build_conv (REF_BIND, rto, conv);
970 conv = build_conv (REF_BIND, rto, conv);
971 ICS_STD_RANK (conv) = STD_RANK;
979 conv = standard_conversion (to, rfrom, expr);
982 conv = build_conv (REF_BIND, rto, conv);
984 /* Bind directly to a base subobject of a class rvalue. Do it
985 after building the conversion for proper handling of ICS_RANK. */
986 if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
987 TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
990 && ((! (TYPE_READONLY (to) && ! TYPE_VOLATILE (to)
991 && (flags & LOOKUP_NO_TEMP_BIND) == 0))
992 /* If T1 is reference-related to T2, cv1 must be the same
993 cv-qualification as, or greater cv-qualification than,
994 cv2; otherwise, the program is ill-formed. */
996 && (TYPE_READONLY (to) < TYPE_READONLY (from)
997 || TYPE_VOLATILE (to) < TYPE_VOLATILE (from)))))
998 ICS_BAD_FLAG (conv) = 1;
1004 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1005 to type TO. The optional expression EXPR may affect the conversion.
1006 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1010 implicit_conversion (to, from, expr, flags)
1011 tree to, from, expr;
1015 struct z_candidate *cand;
1017 if (expr && type_unknown_p (expr))
1019 expr = instantiate_type (to, expr, 0);
1020 if (expr == error_mark_node)
1022 from = TREE_TYPE (expr);
1025 if (TREE_CODE (to) == REFERENCE_TYPE)
1026 conv = reference_binding (to, from, expr, flags);
1028 conv = standard_conversion (to, from, expr);
1032 else if (expr != NULL_TREE
1033 && (IS_AGGR_TYPE (non_reference (from))
1034 || IS_AGGR_TYPE (non_reference (to)))
1035 && (flags & LOOKUP_NO_CONVERSION) == 0)
1037 cand = build_user_type_conversion_1
1038 (to, expr, LOOKUP_ONLYCONVERTING);
1040 conv = cand->second_conv;
1041 if ((! conv || ICS_BAD_FLAG (conv))
1042 && TREE_CODE (to) == REFERENCE_TYPE
1043 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1045 cand = build_user_type_conversion_1
1046 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1049 if (! TYPE_READONLY (TREE_TYPE (to))
1050 || TYPE_VOLATILE (TREE_TYPE (to)))
1051 ICS_BAD_FLAG (cand->second_conv) = 1;
1052 if (!conv || (ICS_BAD_FLAG (conv)
1053 > ICS_BAD_FLAG (cand->second_conv)))
1054 conv = build_conv (REF_BIND, to, cand->second_conv);
1062 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1065 static struct z_candidate *
1066 add_candidate (candidates, fn, convs, viable)
1067 struct z_candidate *candidates;
1071 struct z_candidate *cand
1072 = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
1075 cand->convs = convs;
1076 cand->second_conv = NULL_TREE;
1077 cand->viable = viable;
1078 cand->basetype_path = NULL_TREE;
1079 cand->template = NULL_TREE;
1080 cand->warnings = NULL_TREE;
1081 cand->next = candidates;
1086 /* Create an overload candidate for the function or method FN called with
1087 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1088 to implicit_conversion. */
1090 static struct z_candidate *
1091 add_function_candidate (candidates, fn, arglist, flags)
1092 struct z_candidate *candidates;
1096 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1099 tree parmnode = parmlist;
1100 tree argnode = arglist;
1103 /* The `this' and `in_chrg' arguments to constructors are not considered
1104 in overload resolution. */
1105 if (DECL_CONSTRUCTOR_P (fn))
1107 parmnode = TREE_CHAIN (parmnode);
1108 argnode = TREE_CHAIN (argnode);
1109 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1111 parmnode = TREE_CHAIN (parmnode);
1112 argnode = TREE_CHAIN (argnode);
1116 len = list_length (argnode);
1117 convs = make_scratch_vec (len);
1119 for (i = 0; i < len; ++i)
1121 tree arg = TREE_VALUE (argnode);
1122 tree argtype = TREE_TYPE (arg);
1125 /* An overloaded function does not have an argument type */
1126 if (TREE_CODE (arg) == OVERLOAD)
1127 argtype = unknown_type_node;
1128 argtype = cp_build_type_variant
1129 (argtype, TREE_READONLY (arg), TREE_THIS_VOLATILE (arg));
1131 if (parmnode == void_list_node)
1134 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1137 t = build1 (IDENTITY_CONV, argtype, arg);
1138 ICS_ELLIPSIS_FLAG (t) = 1;
1141 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1142 && ! DECL_CONSTRUCTOR_P (fn))
1143 ICS_THIS_FLAG (t) = 1;
1145 TREE_VEC_ELT (convs, i) = t;
1149 if (ICS_BAD_FLAG (t))
1153 parmnode = TREE_CHAIN (parmnode);
1154 argnode = TREE_CHAIN (argnode);
1160 /* Make sure there are default args for the rest of the parms. */
1161 for (; parmnode && parmnode != void_list_node;
1162 parmnode = TREE_CHAIN (parmnode))
1163 if (! TREE_PURPOSE (parmnode))
1169 return add_candidate (candidates, fn, convs, viable);
1172 /* Create an overload candidate for the conversion function FN which will
1173 be invoked for expression OBJ, producing a pointer-to-function which
1174 will in turn be called with the argument list ARGLIST, and add it to
1175 CANDIDATES. FLAGS is passed on to implicit_conversion. */
1177 static struct z_candidate *
1178 add_conv_candidate (candidates, fn, obj, arglist)
1179 struct z_candidate *candidates;
1180 tree fn, obj, arglist;
1182 tree totype = TREE_TYPE (TREE_TYPE (fn));
1183 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1184 int i, len = list_length (arglist) + 1;
1185 tree convs = make_scratch_vec (len);
1186 tree parmnode = parmlist;
1187 tree argnode = arglist;
1189 int flags = LOOKUP_NORMAL;
1191 for (i = 0; i < len; ++i)
1193 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1194 tree argtype = lvalue_type (arg);
1198 t = implicit_conversion (totype, argtype, arg, flags);
1199 else if (parmnode == void_list_node)
1202 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1205 t = build1 (IDENTITY_CONV, argtype, arg);
1206 ICS_ELLIPSIS_FLAG (t) = 1;
1209 TREE_VEC_ELT (convs, i) = t;
1213 if (ICS_BAD_FLAG (t))
1220 parmnode = TREE_CHAIN (parmnode);
1221 argnode = TREE_CHAIN (argnode);
1227 for (; parmnode && parmnode != void_list_node;
1228 parmnode = TREE_CHAIN (parmnode))
1229 if (! TREE_PURPOSE (parmnode))
1235 return add_candidate (candidates, fn, convs, viable);
1238 static struct z_candidate *
1239 build_builtin_candidate (candidates, fnname, type1, type2,
1240 args, argtypes, flags)
1241 struct z_candidate *candidates;
1242 tree fnname, type1, type2, *args, *argtypes;
1253 convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1255 for (i = 0; i < 2; ++i)
1260 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1264 /* We need something for printing the candidate. */
1265 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1267 else if (ICS_BAD_FLAG (t))
1269 TREE_VEC_ELT (convs, i) = t;
1272 /* For COND_EXPR we rearranged the arguments; undo that now. */
1275 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1276 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1277 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1279 TREE_VEC_ELT (convs, 0) = t;
1284 return add_candidate (candidates, fnname, convs, viable);
1291 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1294 /* Create any builtin operator overload candidates for the operator in
1295 question given the converted operand types TYPE1 and TYPE2. The other
1296 args are passed through from add_builtin_candidates to
1297 build_builtin_candidate. */
1299 static struct z_candidate *
1300 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1301 args, argtypes, flags)
1302 struct z_candidate *candidates;
1303 enum tree_code code, code2;
1304 tree fnname, type1, type2, *args, *argtypes;
1309 case POSTINCREMENT_EXPR:
1310 case POSTDECREMENT_EXPR:
1311 args[1] = integer_zero_node;
1312 type2 = integer_type_node;
1321 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1322 and VQ is either volatile or empty, there exist candidate operator
1323 functions of the form
1324 VQ T& operator++(VQ T&);
1325 T operator++(VQ T&, int);
1326 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1327 type other than bool, and VQ is either volatile or empty, there exist
1328 candidate operator functions of the form
1329 VQ T& operator--(VQ T&);
1330 T operator--(VQ T&, int);
1331 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1332 complete object type, and VQ is either volatile or empty, there exist
1333 candidate operator functions of the form
1334 T*VQ& operator++(T*VQ&);
1335 T*VQ& operator--(T*VQ&);
1336 T* operator++(T*VQ&, int);
1337 T* operator--(T*VQ&, int); */
1339 case POSTDECREMENT_EXPR:
1340 case PREDECREMENT_EXPR:
1341 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1343 case POSTINCREMENT_EXPR:
1344 case PREINCREMENT_EXPR:
1345 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1346 || TYPE_PTROB_P (type1))
1348 type1 = build_reference_type (type1);
1353 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1354 exist candidate operator functions of the form
1358 8 For every function type T, there exist candidate operator functions of
1360 T& operator*(T*); */
1363 if (TREE_CODE (type1) == POINTER_TYPE
1364 && (TYPE_PTROB_P (type1)
1365 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1369 /* 9 For every type T, there exist candidate operator functions of the form
1372 10For every promoted arithmetic type T, there exist candidate operator
1373 functions of the form
1377 case CONVERT_EXPR: /* unary + */
1378 if (TREE_CODE (type1) == POINTER_TYPE
1379 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1382 if (ARITHMETIC_TYPE_P (type1))
1386 /* 11For every promoted integral type T, there exist candidate operator
1387 functions of the form
1391 if (INTEGRAL_TYPE_P (type1))
1395 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1396 is the same type as C2 or is a derived class of C2, T is a complete
1397 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1398 there exist candidate operator functions of the form
1399 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1400 where CV12 is the union of CV1 and CV2. */
1403 if (TREE_CODE (type1) == POINTER_TYPE
1404 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1406 tree c1 = TREE_TYPE (type1);
1407 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1408 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1409 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1411 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1412 && (TYPE_PTRMEMFUNC_P (type2)
1413 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1418 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1419 didate operator functions of the form
1424 bool operator<(L, R);
1425 bool operator>(L, R);
1426 bool operator<=(L, R);
1427 bool operator>=(L, R);
1428 bool operator==(L, R);
1429 bool operator!=(L, R);
1430 where LR is the result of the usual arithmetic conversions between
1433 14For every pair of types T and I, where T is a cv-qualified or cv-
1434 unqualified complete object type and I is a promoted integral type,
1435 there exist candidate operator functions of the form
1436 T* operator+(T*, I);
1437 T& operator[](T*, I);
1438 T* operator-(T*, I);
1439 T* operator+(I, T*);
1440 T& operator[](I, T*);
1442 15For every T, where T is a pointer to complete object type, there exist
1443 candidate operator functions of the form112)
1444 ptrdiff_t operator-(T, T);
1446 16For every pointer type T, there exist candidate operator functions of
1448 bool operator<(T, T);
1449 bool operator>(T, T);
1450 bool operator<=(T, T);
1451 bool operator>=(T, T);
1452 bool operator==(T, T);
1453 bool operator!=(T, T);
1455 17For every pointer to member type T, there exist candidate operator
1456 functions of the form
1457 bool operator==(T, T);
1458 bool operator!=(T, T); */
1461 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1463 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1465 type2 = ptrdiff_type_node;
1469 case TRUNC_DIV_EXPR:
1470 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1476 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1477 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1479 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1480 && null_ptr_cst_p (args[1]))
1485 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1486 && null_ptr_cst_p (args[0]))
1497 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1498 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1500 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1505 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1513 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1516 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1518 type1 = ptrdiff_type_node;
1521 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1523 type2 = ptrdiff_type_node;
1528 /* 18For every pair of promoted integral types L and R, there exist candi-
1529 date operator functions of the form
1536 where LR is the result of the usual arithmetic conversions between
1539 case TRUNC_MOD_EXPR:
1545 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1549 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1550 type, VQ is either volatile or empty, and R is a promoted arithmetic
1551 type, there exist candidate operator functions of the form
1552 VQ L& operator=(VQ L&, R);
1553 VQ L& operator*=(VQ L&, R);
1554 VQ L& operator/=(VQ L&, R);
1555 VQ L& operator+=(VQ L&, R);
1556 VQ L& operator-=(VQ L&, R);
1558 20For every pair T, VQ), where T is any type and VQ is either volatile
1559 or empty, there exist candidate operator functions of the form
1560 T*VQ& operator=(T*VQ&, T*);
1562 21For every pair T, VQ), where T is a pointer to member type and VQ is
1563 either volatile or empty, there exist candidate operator functions of
1565 VQ T& operator=(VQ T&, T);
1567 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1568 unqualified complete object type, VQ is either volatile or empty, and
1569 I is a promoted integral type, there exist candidate operator func-
1571 T*VQ& operator+=(T*VQ&, I);
1572 T*VQ& operator-=(T*VQ&, I);
1574 23For every triple L, VQ, R), where L is an integral or enumeration
1575 type, VQ is either volatile or empty, and R is a promoted integral
1576 type, there exist candidate operator functions of the form
1578 VQ L& operator%=(VQ L&, R);
1579 VQ L& operator<<=(VQ L&, R);
1580 VQ L& operator>>=(VQ L&, R);
1581 VQ L& operator&=(VQ L&, R);
1582 VQ L& operator^=(VQ L&, R);
1583 VQ L& operator|=(VQ L&, R); */
1590 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1592 type2 = ptrdiff_type_node;
1596 case TRUNC_DIV_EXPR:
1597 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1601 case TRUNC_MOD_EXPR:
1607 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1612 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1614 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1615 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1616 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1617 || ((TYPE_PTRMEMFUNC_P (type1)
1618 || TREE_CODE (type1) == POINTER_TYPE)
1619 && null_ptr_cst_p (args[1])))
1627 my_friendly_abort (367);
1629 type1 = build_reference_type (type1);
1633 /* Kludge around broken overloading rules whereby
1634 bool ? const char& : enum is ambiguous
1635 (between int and const char&). */
1636 flags |= LOOKUP_NO_TEMP_BIND;
1638 /* Extension: Support ?: of enumeral type. Hopefully this will not
1639 be an extension for long. */
1640 if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
1642 else if (TREE_CODE (type1) == ENUMERAL_TYPE
1643 || TREE_CODE (type2) == ENUMERAL_TYPE)
1645 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1647 if (TREE_CODE (type1) == TREE_CODE (type2)
1648 && (TREE_CODE (type1) == REFERENCE_TYPE
1649 || TREE_CODE (type1) == POINTER_TYPE
1650 || TYPE_PTRMEMFUNC_P (type1)
1651 || IS_AGGR_TYPE (type1)))
1653 if (TREE_CODE (type1) == REFERENCE_TYPE
1654 || TREE_CODE (type2) == REFERENCE_TYPE)
1656 if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
1657 && null_ptr_cst_p (args[1]))
1658 || IS_AGGR_TYPE (type1))
1663 if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
1664 && null_ptr_cst_p (args[0]))
1665 || IS_AGGR_TYPE (type2))
1673 my_friendly_abort (367);
1676 /* If we're dealing with two pointer types, we need candidates
1677 for both of them. */
1678 if (type2 && type1 != type2
1679 && TREE_CODE (type1) == TREE_CODE (type2)
1680 && (TREE_CODE (type1) == REFERENCE_TYPE
1681 || (TREE_CODE (type1) == POINTER_TYPE
1682 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1683 || TYPE_PTRMEMFUNC_P (type1)
1684 || IS_AGGR_TYPE (type1)))
1686 candidates = build_builtin_candidate
1687 (candidates, fnname, type1, type1, args, argtypes, flags);
1688 return build_builtin_candidate
1689 (candidates, fnname, type2, type2, args, argtypes, flags);
1692 return build_builtin_candidate
1693 (candidates, fnname, type1, type2, args, argtypes, flags);
1697 type_decays_to (type)
1700 if (TREE_CODE (type) == ARRAY_TYPE)
1701 return build_pointer_type (TREE_TYPE (type));
1702 if (TREE_CODE (type) == FUNCTION_TYPE)
1703 return build_pointer_type (type);
1707 /* There are three conditions of builtin candidates:
1709 1) bool-taking candidates. These are the same regardless of the input.
1710 2) pointer-pair taking candidates. These are generated for each type
1711 one of the input types converts to.
1712 3) arithmetic candidates. According to the WP, we should generate
1713 all of these, but I'm trying not to... */
1715 static struct z_candidate *
1716 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1717 struct z_candidate *candidates;
1718 enum tree_code code, code2;
1723 tree type, argtypes[3], types[2];
1725 for (i = 0; i < 3; ++i)
1728 argtypes[i] = lvalue_type (args[i]);
1730 argtypes[i] = NULL_TREE;
1735 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1736 and VQ is either volatile or empty, there exist candidate operator
1737 functions of the form
1738 VQ T& operator++(VQ T&); */
1740 case POSTINCREMENT_EXPR:
1741 case PREINCREMENT_EXPR:
1742 case POSTDECREMENT_EXPR:
1743 case PREDECREMENT_EXPR:
1748 /* 24There also exist candidate operator functions of the form
1749 bool operator!(bool);
1750 bool operator&&(bool, bool);
1751 bool operator||(bool, bool); */
1753 case TRUTH_NOT_EXPR:
1754 return build_builtin_candidate
1755 (candidates, fnname, boolean_type_node,
1756 NULL_TREE, args, argtypes, flags);
1758 case TRUTH_ORIF_EXPR:
1759 case TRUTH_ANDIF_EXPR:
1760 return build_builtin_candidate
1761 (candidates, fnname, boolean_type_node,
1762 boolean_type_node, args, argtypes, flags);
1773 types[0] = types[1] = NULL_TREE;
1775 for (i = 0; i < 2; ++i)
1779 else if (IS_AGGR_TYPE (argtypes[i]))
1781 tree convs = lookup_conversions (argtypes[i]);
1783 if (code == COND_EXPR)
1785 if (real_lvalue_p (args[i]))
1786 types[i] = scratch_tree_cons
1787 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1789 types[i] = scratch_tree_cons
1790 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1793 else if (! convs || (i == 0 && code == MODIFY_EXPR
1794 && code2 == NOP_EXPR))
1797 for (; convs; convs = TREE_CHAIN (convs))
1799 type = TREE_TYPE (TREE_TYPE (TREE_VALUE (convs)));
1802 && (TREE_CODE (type) != REFERENCE_TYPE
1803 || TYPE_READONLY (TREE_TYPE (type))))
1806 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1807 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1809 type = non_reference (type);
1810 if (i != 0 || ! ref1)
1812 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1813 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1814 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1815 if (INTEGRAL_TYPE_P (type))
1816 type = type_promotes_to (type);
1819 if (! value_member (type, types[i]))
1820 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1825 if (code == COND_EXPR && real_lvalue_p (args[i]))
1826 types[i] = scratch_tree_cons
1827 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1828 type = non_reference (argtypes[i]);
1829 if (i != 0 || ! ref1)
1831 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1832 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1833 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1834 if (INTEGRAL_TYPE_P (type))
1835 type = type_promotes_to (type);
1837 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1841 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1844 for (type = types[1]; type; type = TREE_CHAIN (type))
1845 candidates = add_builtin_candidate
1846 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1847 TREE_VALUE (type), args, argtypes, flags);
1849 candidates = add_builtin_candidate
1850 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1851 NULL_TREE, args, argtypes, flags);
1858 /* If TMPL can be successfully instantiated as indicated by
1859 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
1861 TMPL is the template. EXPLICIT_TARGS are any explicit template
1862 arguments. ARGLIST is the arguments provided at the call-site.
1863 The RETURN_TYPE is the desired type for conversion operators. If
1864 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
1865 OBJ is supplied, FLAGS are ignored, and OBJ is as for
1866 add_conv_candidate. */
1868 static struct z_candidate*
1869 add_template_candidate_real (candidates, tmpl, explicit_targs,
1870 arglist, return_type, flags,
1872 struct z_candidate *candidates;
1873 tree tmpl, explicit_targs, arglist, return_type;
1876 unification_kind_t strict;
1878 int ntparms = DECL_NTPARMS (tmpl);
1879 tree targs = make_scratch_vec (ntparms);
1880 struct z_candidate *cand;
1884 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
1885 return_type, strict, NULL_TREE);
1890 fn = instantiate_template (tmpl, targs);
1891 if (fn == error_mark_node)
1894 if (obj != NULL_TREE)
1895 /* Aha, this is a conversion function. */
1896 cand = add_conv_candidate (candidates, fn, obj, arglist);
1898 cand = add_function_candidate (candidates, fn, arglist, flags);
1899 if (DECL_TI_TEMPLATE (fn) != tmpl)
1900 /* This situation can occur if a member template of a template
1901 class is specialized. Then, instantiate_template might return
1902 an instantiation of the specialization, in which case the
1903 DECL_TI_TEMPLATE field will point at the original
1904 specialization. For example:
1906 template <class T> struct S { template <class U> void f(U);
1907 template <> void f(int) {}; };
1911 Here, TMPL will be template <class U> S<double>::f(U).
1912 And, instantiate template will give us the specialization
1913 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
1914 for this will point at template <class T> template <> S<T>::f(int),
1915 so that we can find the definition. For the purposes of
1916 overload resolution, however, we want the original TMPL. */
1917 cand->template = tree_cons (tmpl, targs, NULL_TREE);
1919 cand->template = DECL_TEMPLATE_INFO (fn);
1925 static struct z_candidate *
1926 add_template_candidate (candidates, tmpl, explicit_targs,
1927 arglist, return_type, flags, strict)
1928 struct z_candidate *candidates;
1929 tree tmpl, explicit_targs, arglist, return_type;
1931 unification_kind_t strict;
1934 add_template_candidate_real (candidates, tmpl, explicit_targs,
1935 arglist, return_type, flags,
1940 static struct z_candidate *
1941 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
1942 struct z_candidate *candidates;
1943 tree tmpl, obj, arglist, return_type;
1946 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
1947 return_type, 0, obj, DEDUCE_CONV);
1953 struct z_candidate *cands;
1955 for (; cands; cands = cands->next)
1956 if (pedantic ? cands->viable == 1 : cands->viable)
1961 static struct z_candidate *
1962 splice_viable (cands)
1963 struct z_candidate *cands;
1965 struct z_candidate **p = &cands;
1969 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
1982 /* Fix this to work on non-lvalues. */
1983 if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
1984 || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
1987 return build_unary_op (ADDR_EXPR, obj, 0);
1991 print_z_candidates (candidates)
1992 struct z_candidate *candidates;
1994 char *str = "candidates are:";
1995 for (; candidates; candidates = candidates->next)
1997 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
1999 if (candidates->fn == ansi_opname [COND_EXPR])
2000 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2001 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2002 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2003 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2004 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2005 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2006 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2007 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2009 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2010 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2013 cp_error_at ("%s %+D%s", str, candidates->fn,
2014 candidates->viable == -1 ? " <near match>" : "");
2019 /* Returns the best overload candidate to perform the requested
2020 conversion. This function is used for three the overloading situations
2021 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2022 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2023 per [dcl.init.ref], so we ignore temporary bindings. */
2025 static struct z_candidate *
2026 build_user_type_conversion_1 (totype, expr, flags)
2030 struct z_candidate *candidates, *cand;
2031 tree fromtype = TREE_TYPE (expr);
2032 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2033 tree args = NULL_TREE;
2034 tree templates = NULL_TREE;
2036 if (IS_AGGR_TYPE (totype))
2037 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2038 if (IS_AGGR_TYPE (fromtype)
2039 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2040 convs = lookup_conversions (fromtype);
2043 flags |= LOOKUP_NO_CONVERSION;
2047 tree t = build_int_2 (0, 0);
2048 TREE_TYPE (t) = build_pointer_type (totype);
2049 args = build_scratch_list (NULL_TREE, expr);
2050 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2051 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2052 args = scratch_tree_cons (NULL_TREE, t, args);
2054 ctors = TREE_VALUE (ctors);
2056 for (; ctors; ctors = OVL_NEXT (ctors))
2058 tree ctor = OVL_CURRENT (ctors);
2059 if (DECL_NONCONVERTING_P (ctor))
2062 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2064 templates = scratch_tree_cons (NULL_TREE, ctor, templates);
2066 add_template_candidate (candidates, ctor,
2067 NULL_TREE, args, NULL_TREE, flags,
2071 candidates = add_function_candidate (candidates, ctor,
2076 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2077 candidates->basetype_path = TYPE_BINFO (totype);
2082 args = build_scratch_list (NULL_TREE, build_this (expr));
2084 for (; convs; convs = TREE_CHAIN (convs))
2086 tree fns = TREE_VALUE (convs);
2087 int convflags = LOOKUP_NO_CONVERSION;
2090 /* If we are called to convert to a reference type, we are trying to
2091 find an lvalue binding, so don't even consider temporaries. If
2092 we don't find an lvalue binding, the caller will try again to
2093 look for a temporary binding. */
2094 if (TREE_CODE (totype) == REFERENCE_TYPE)
2095 convflags |= LOOKUP_NO_TEMP_BIND;
2097 if (TREE_CODE (fns) != TEMPLATE_DECL)
2098 ics = implicit_conversion
2099 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2101 /* We can't compute this yet. */
2102 ics = error_mark_node;
2104 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2105 /* ignore the near match. */;
2107 for (; fns; fns = OVL_NEXT (fns))
2109 tree fn = OVL_CURRENT (fns);
2110 struct z_candidate *old_candidates = candidates;
2112 if (TREE_CODE (fn) == TEMPLATE_DECL)
2114 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2116 add_template_candidate (candidates, fn, NULL_TREE,
2117 args, totype, flags,
2121 candidates = add_function_candidate (candidates, fn,
2124 if (candidates != old_candidates)
2126 if (TREE_CODE (fn) == TEMPLATE_DECL)
2127 ics = implicit_conversion
2128 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2131 candidates->second_conv = ics;
2132 candidates->basetype_path = TREE_PURPOSE (convs);
2134 if (ics == NULL_TREE)
2135 candidates->viable = 0;
2136 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2137 candidates->viable = -1;
2142 if (! any_viable (candidates))
2145 if (flags & LOOKUP_COMPLAIN)
2147 if (candidates && ! candidates->next)
2148 /* say why this one won't work or try to be loose */;
2150 cp_error ("no viable candidates");
2157 candidates = splice_viable (candidates);
2158 cand = tourney (candidates);
2162 if (flags & LOOKUP_COMPLAIN)
2164 cp_error ("conversion from `%T' to `%T' is ambiguous",
2166 print_z_candidates (candidates);
2169 cand = candidates; /* any one will do */
2170 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2171 ICS_USER_FLAG (cand->second_conv) = 1;
2172 ICS_BAD_FLAG (cand->second_conv) = 1;
2177 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2178 p = &(TREE_OPERAND (*p, 0));
2180 /* Pedantically, normal function declarations are never considered
2181 to refer to template instantiations, so we only do this with
2183 if (flag_guiding_decls && templates && ! cand->template
2184 && !DECL_INITIAL (cand->fn)
2185 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2186 add_maybe_template (cand->fn, templates);
2190 (DECL_CONSTRUCTOR_P (cand->fn)
2191 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2192 expr, build_expr_ptr_wrapper (cand));
2193 ICS_USER_FLAG (cand->second_conv) = 1;
2194 if (cand->viable == -1)
2195 ICS_BAD_FLAG (cand->second_conv) = 1;
2201 build_user_type_conversion (totype, expr, flags)
2205 struct z_candidate *cand
2206 = build_user_type_conversion_1 (totype, expr, flags);
2210 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2211 return error_mark_node;
2212 return convert_from_reference (convert_like (cand->second_conv, expr));
2217 /* Do any initial processing on the arguments to a function call. */
2224 for (t = args; t; t = TREE_CHAIN (t))
2226 if (TREE_VALUE (t) == error_mark_node)
2227 return error_mark_node;
2228 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2230 error ("invalid use of void expression");
2231 return error_mark_node;
2233 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2234 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2240 build_new_function_call (fn, args)
2243 struct z_candidate *candidates = 0, *cand;
2244 tree explicit_targs = NULL_TREE;
2245 int template_only = 0;
2247 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2249 explicit_targs = TREE_OPERAND (fn, 1);
2250 fn = TREE_OPERAND (fn, 0);
2254 if (really_overloaded_fn (fn))
2257 tree templates = NULL_TREE;
2259 args = resolve_args (args);
2261 if (args == error_mark_node)
2262 return error_mark_node;
2264 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2266 tree t = OVL_FUNCTION (t1);
2267 if (TREE_CODE (t) == TEMPLATE_DECL)
2269 templates = scratch_tree_cons (NULL_TREE, t, templates);
2270 candidates = add_template_candidate
2271 (candidates, t, explicit_targs, args, NULL_TREE,
2272 LOOKUP_NORMAL, DEDUCE_CALL);
2274 else if (! template_only)
2275 candidates = add_function_candidate
2276 (candidates, t, args, LOOKUP_NORMAL);
2279 if (! any_viable (candidates))
2281 if (candidates && ! candidates->next)
2282 return build_function_call (candidates->fn, args);
2283 cp_error ("no matching function for call to `%D (%A)'",
2284 DECL_NAME (OVL_FUNCTION (fn)), args);
2286 print_z_candidates (candidates);
2287 return error_mark_node;
2289 candidates = splice_viable (candidates);
2290 cand = tourney (candidates);
2294 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2295 DECL_NAME (OVL_FUNCTION (fn)), args);
2296 print_z_candidates (candidates);
2297 return error_mark_node;
2300 /* Pedantically, normal function declarations are never considered
2301 to refer to template instantiations, so we only do this with
2303 if (flag_guiding_decls && templates && ! cand->template
2304 && ! DECL_INITIAL (cand->fn))
2305 add_maybe_template (cand->fn, templates);
2307 return build_over_call (cand, args, LOOKUP_NORMAL);
2310 /* This is not really overloaded. */
2311 fn = OVL_CURRENT (fn);
2313 return build_function_call (fn, args);
2317 build_object_call (obj, args)
2320 struct z_candidate *candidates = 0, *cand;
2321 tree fns, convs, mem_args = NULL_TREE;
2322 tree type = TREE_TYPE (obj);
2323 tree templates = NULL_TREE;
2325 if (TYPE_PTRMEMFUNC_P (type))
2327 /* It's no good looking for an overloaded operator() on a
2328 pointer-to-member-function. */
2329 cp_error ("pointer-to-member function %E cannot be called", obj);
2330 cp_error ("without an object; consider using .* or ->*");
2331 return error_mark_node;
2334 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2335 if (fns == error_mark_node)
2336 return error_mark_node;
2338 args = resolve_args (args);
2340 if (args == error_mark_node)
2341 return error_mark_node;
2345 tree base = TREE_PURPOSE (fns);
2346 mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2348 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2350 tree fn = OVL_CURRENT (fns);
2351 if (TREE_CODE (fn) == TEMPLATE_DECL)
2353 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2355 = add_template_candidate (candidates, fn, NULL_TREE,
2356 mem_args, NULL_TREE,
2357 LOOKUP_NORMAL, DEDUCE_CALL);
2360 candidates = add_function_candidate
2361 (candidates, fn, mem_args, LOOKUP_NORMAL);
2364 candidates->basetype_path = base;
2368 convs = lookup_conversions (type);
2370 for (; convs; convs = TREE_CHAIN (convs))
2372 tree fns = TREE_VALUE (convs);
2373 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2376 if (TREE_CODE (totype) == POINTER_TYPE
2377 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2378 for (; fns; fns = OVL_NEXT (fn))
2380 fn = OVL_CURRENT (fn);
2381 if (TREE_CODE (fn) == TEMPLATE_DECL)
2383 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2384 candidates = add_template_conv_candidate (candidates,
2391 candidates = add_conv_candidate (candidates, fn, obj, args);
2394 candidates->basetype_path = TREE_PURPOSE (convs);
2398 if (! any_viable (candidates))
2400 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2401 print_z_candidates (candidates);
2402 return error_mark_node;
2405 candidates = splice_viable (candidates);
2406 cand = tourney (candidates);
2410 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2411 print_z_candidates (candidates);
2412 return error_mark_node;
2415 if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2416 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2418 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2421 return build_function_call (obj, args);
2425 op_error (code, code2, arg1, arg2, arg3, problem)
2426 enum tree_code code, code2;
2427 tree arg1, arg2, arg3;
2431 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2436 cp_error ("%s for `%T ? %T : %T'", problem,
2437 error_type (arg1), error_type (arg2), error_type (arg3));
2439 case POSTINCREMENT_EXPR:
2440 case POSTDECREMENT_EXPR:
2441 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2444 cp_error ("%s for `%T[%T]'", problem,
2445 error_type (arg1), error_type (arg2));
2449 cp_error ("%s for `%T %s %T'", problem,
2450 error_type (arg1), opname, error_type (arg2));
2452 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2457 build_new_op (code, flags, arg1, arg2, arg3)
2458 enum tree_code code;
2460 tree arg1, arg2, arg3;
2462 struct z_candidate *candidates = 0, *cand;
2463 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
2464 enum tree_code code2 = NOP_EXPR;
2465 tree templates = NULL_TREE;
2468 if (arg1 == error_mark_node
2469 || arg2 == error_mark_node
2470 || arg3 == error_mark_node)
2471 return error_mark_node;
2473 /* This can happen if a template takes all non-type parameters, e.g.
2474 undeclared_template<1, 5, 72>a; */
2475 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
2477 cp_error ("`%D' must be declared before use", arg1);
2478 return error_mark_node;
2481 if (code == MODIFY_EXPR)
2483 code2 = TREE_CODE (arg3);
2485 fnname = ansi_assopname[code2];
2488 fnname = ansi_opname[code];
2497 arglist = scratch_tree_cons (NULL_TREE, arg2, arg3);
2498 if (flags & LOOKUP_GLOBAL)
2499 return build_new_function_call
2500 (lookup_function_nonclass (fnname, arglist), arglist);
2503 rval = build_method_call
2504 (build_indirect_ref (build1 (NOP_EXPR, arg1, error_mark_node),
2506 fnname, arglist, NULL_TREE, flags);
2507 if (rval == error_mark_node)
2508 /* User might declare fancy operator new, but invoke it
2509 like standard one. */
2512 TREE_TYPE (rval) = arg1;
2516 case VEC_DELETE_EXPR:
2521 if (flags & LOOKUP_GLOBAL)
2523 arglist = build_scratch_list (NULL_TREE, arg1);
2524 return build_new_function_call
2525 (lookup_function_nonclass (fnname, arglist), arglist);
2528 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2530 arg1 = TREE_TYPE (arg1);
2532 /* This handles the case where we're trying to delete
2537 if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
2539 /* Strip off the pointer and the array. */
2540 arg1 = TREE_TYPE (TREE_TYPE (arg1));
2542 while (TREE_CODE (arg1) == ARRAY_TYPE)
2543 arg1 = (TREE_TYPE (arg1));
2545 arg1 = build_pointer_type (arg1);
2549 rval = build_method_call
2550 (build_indirect_ref (build1 (NOP_EXPR, arg1,
2553 fnname, arglist, NULL_TREE, flags);
2555 /* This can happen when operator delete is protected. */
2556 my_friendly_assert (rval != error_mark_node, 250);
2557 TREE_TYPE (rval) = void_type_node;
2563 return build_object_call (arg1, arg2);
2569 /* The comma operator can have void args. */
2570 if (TREE_CODE (arg1) == OFFSET_REF)
2571 arg1 = resolve_offset_ref (arg1);
2572 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
2573 arg2 = resolve_offset_ref (arg2);
2574 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
2575 arg3 = resolve_offset_ref (arg3);
2577 if (code == COND_EXPR)
2579 if (arg2 == NULL_TREE
2580 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
2581 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
2582 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
2583 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
2586 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
2587 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
2590 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2591 arg2 = integer_zero_node;
2594 arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
2595 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
2597 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2599 arglist = build_scratch_list (NULL_TREE, arg1);
2601 fns = lookup_function_nonclass (fnname, arglist);
2603 if (fns && TREE_CODE (fns) == TREE_LIST)
2604 fns = TREE_VALUE (fns);
2605 for (; fns; fns = OVL_NEXT (fns))
2607 tree fn = OVL_CURRENT (fns);
2608 if (TREE_CODE (fn) == TEMPLATE_DECL)
2610 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2612 = add_template_candidate (candidates, fn, NULL_TREE,
2613 arglist, TREE_TYPE (fnname),
2614 flags, DEDUCE_CALL);
2617 candidates = add_function_candidate (candidates, fn, arglist, flags);
2620 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
2622 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
2623 if (fns == error_mark_node)
2631 tree basetype = TREE_PURPOSE (fns);
2632 mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
2633 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2635 tree fn = OVL_CURRENT (fns);
2638 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
2639 this_arglist = mem_arglist;
2641 this_arglist = arglist;
2643 if (TREE_CODE (fn) == TEMPLATE_DECL)
2645 /* A member template. */
2646 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2648 = add_template_candidate (candidates, fn, NULL_TREE,
2649 this_arglist, TREE_TYPE (fnname),
2650 flags, DEDUCE_CALL);
2653 candidates = add_function_candidate
2654 (candidates, fn, this_arglist, flags);
2657 candidates->basetype_path = basetype;
2664 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
2665 to know about two args; a builtin candidate will always have a first
2666 parameter of type bool. We'll handle that in
2667 build_builtin_candidate. */
2668 if (code == COND_EXPR)
2678 args[2] = NULL_TREE;
2681 candidates = add_builtin_candidates
2682 (candidates, code, code2, fnname, args, flags);
2685 if (! any_viable (candidates))
2689 case POSTINCREMENT_EXPR:
2690 case POSTDECREMENT_EXPR:
2691 /* Look for an `operator++ (int)'. If they didn't have
2692 one, then we fall back to the old way of doing things. */
2693 if (flags & LOOKUP_COMPLAIN)
2694 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
2695 fnname, opname_tab [code]);
2696 if (code == POSTINCREMENT_EXPR)
2697 code = PREINCREMENT_EXPR;
2699 code = PREDECREMENT_EXPR;
2700 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
2702 /* The caller will deal with these. */
2711 if (flags & LOOKUP_COMPLAIN)
2713 op_error (code, code2, arg1, arg2, arg3, "no match");
2714 print_z_candidates (candidates);
2716 return error_mark_node;
2718 candidates = splice_viable (candidates);
2719 cand = tourney (candidates);
2723 if (flags & LOOKUP_COMPLAIN)
2725 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
2726 print_z_candidates (candidates);
2728 return error_mark_node;
2731 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
2733 extern int warn_synth;
2735 && fnname == ansi_opname[MODIFY_EXPR]
2736 && DECL_ARTIFICIAL (cand->fn)
2738 && ! candidates->next->next)
2740 cp_warning ("using synthesized `%#D' for copy assignment",
2742 cp_warning_at (" where cfront would use `%#D'",
2744 ? candidates->next->fn
2748 /* Pedantically, normal function declarations are never considered
2749 to refer to template instantiations, so we only do this with
2751 if (flag_guiding_decls && templates && ! cand->template
2752 && ! DECL_INITIAL (cand->fn)
2753 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2754 add_maybe_template (cand->fn, templates);
2756 return build_over_call
2758 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
2759 ? mem_arglist : arglist,
2763 /* Check for comparison of different enum types. */
2772 if (flag_int_enum_equivalence == 0
2773 && TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
2774 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
2775 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
2776 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
2778 cp_warning ("comparison between `%#T' and `%#T'",
2779 TREE_TYPE (arg1), TREE_TYPE (arg2));
2786 /* We need to strip any leading REF_BIND so that bitfields don't cause
2787 errors. This should not remove any important conversions, because
2788 builtins don't apply to class objects directly. */
2789 conv = TREE_VEC_ELT (cand->convs, 0);
2790 if (TREE_CODE (conv) == REF_BIND)
2791 conv = TREE_OPERAND (conv, 0);
2792 arg1 = convert_like (conv, arg1);
2794 arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
2796 arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
2802 return build_modify_expr (arg1, code2, arg2);
2805 return build_indirect_ref (arg1, "unary *");
2810 case TRUNC_DIV_EXPR:
2821 case TRUNC_MOD_EXPR:
2825 case TRUTH_ANDIF_EXPR:
2826 case TRUTH_ORIF_EXPR:
2827 return build_binary_op_nodefault (code, arg1, arg2, code);
2832 case TRUTH_NOT_EXPR:
2833 case PREINCREMENT_EXPR:
2834 case POSTINCREMENT_EXPR:
2835 case PREDECREMENT_EXPR:
2836 case POSTDECREMENT_EXPR:
2839 return build_unary_op (code, arg1, candidates != 0);
2842 return build_array_ref (arg1, arg2);
2845 return build_conditional_expr (arg1, arg2, arg3);
2848 return build_m_component_ref
2849 (build_indirect_ref (arg1, NULL_PTR), arg2);
2851 /* The caller will deal with these. */
2858 my_friendly_abort (367);
2863 /* Build up a call to operator new. This has to be handled differently
2864 from other operators in the way lookup is handled; first members are
2865 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
2866 TYPE is the type to be created. ARGS are any new-placement args.
2867 FLAGS are the usual overloading flags. */
2870 build_op_new_call (code, type, args, flags)
2871 enum tree_code code;
2875 tree fnname = ansi_opname[code];
2877 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
2878 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
2880 tree dummy = build1 (NOP_EXPR, build_pointer_type (type),
2882 dummy = build_indirect_ref (dummy, "new");
2883 return build_method_call (dummy, fnname, args, NULL_TREE, flags);
2886 return build_new_function_call
2887 (lookup_function_nonclass (fnname, args), args);
2890 /* Build a call to operator delete. This has to be handled very specially,
2891 because the restrictions on what signatures match are different from all
2892 other call instances. For a normal delete, only a delete taking (void *)
2893 or (void *, size_t) is accepted. For a placement delete, only an exact
2894 match with the placement new is accepted.
2896 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
2897 ADDR is the pointer to be deleted. For placement delete, it is also
2898 used to determine what the corresponding new looked like.
2899 SIZE is the size of the memory block to be deleted.
2900 FLAGS are the usual overloading flags. */
2903 build_op_delete_call (code, addr, size, flags)
2904 enum tree_code code;
2908 tree fn, fns, fnname, fntype, argtypes, args, type;
2911 if (addr == error_mark_node)
2912 return error_mark_node;
2914 type = TREE_TYPE (TREE_TYPE (addr));
2915 fnname = ansi_opname[code];
2917 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
2920 If the result of the lookup is ambiguous or inaccessible, or if
2921 the lookup selects a placement deallocation function, the
2922 program is ill-formed.
2924 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
2926 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
2927 if (fns == error_mark_node)
2928 return error_mark_node;
2936 /* It is unnecessary to wrap another TREE_LIST around it. (MvL) */
2937 /* Build this up like build_offset_ref does. */
2938 fns = build_tree_list (error_mark_node, fns);
2939 TREE_TYPE (fns) = build_offset_type (type, unknown_type_node);
2943 fns = lookup_name_nonclass (fnname);
2945 /* We can recognize a placement delete because of LOOKUP_SPECULATIVELY;
2946 if we are doing placement delete we do nothing if we don't find a
2947 matching op delete. */
2948 placement = !!(flags & LOOKUP_SPECULATIVELY);
2951 /* If placement, we are coming from build_new, and we know that addr
2952 is the allocation expression, so extract the info we need from it.
2953 Obviously, if the build_new process changes this may have to
2957 tree t = TREE_OPERAND (addr, 1);
2958 /* The CALL_EXPR. */
2959 t = TREE_OPERAND (t, 0);
2961 argtypes = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2962 /* The second parm type. */
2963 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
2964 /* The second argument. */
2965 args = TREE_CHAIN (TREE_OPERAND (t, 1));
2967 /* Pull the dummy var out of the TARGET_EXPR for use in our call. */
2968 addr = TREE_OPERAND (addr, 0);
2972 /* First try it without the size argument. */
2973 argtypes = void_list_node;
2977 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
2978 fntype = build_function_type (void_type_node, argtypes);
2980 /* Strip const and volatile from addr. */
2981 if (type != TYPE_MAIN_VARIANT (type))
2982 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
2984 /* instantiate_type will always return a plain function; pretend it's
2986 if (TREE_CODE (fns) == FUNCTION_DECL)
2987 fns = scratch_ovl_cons (fns, NULL_TREE);
2989 fn = instantiate_type (fntype, fns, 0);
2991 if (fn != error_mark_node)
2993 if (TREE_CODE (fns) == TREE_LIST)
2994 /* Member functions. */
2995 enforce_access (TREE_PURPOSE (fns), fn);
2996 return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
3002 /* Normal delete; now try to find a match including the size argument. */
3003 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3004 tree_cons (NULL_TREE, sizetype, void_list_node));
3005 fntype = build_function_type (void_type_node, argtypes);
3007 fn = instantiate_type (fntype, fns, 0);
3009 if (fn != error_mark_node)
3010 return build_function_call
3011 (fn, expr_tree_cons (NULL_TREE, addr,
3012 build_expr_list (NULL_TREE, size)));
3014 cp_error ("no suitable operator delete for `%T'", type);
3015 return error_mark_node;
3018 /* If the current scope isn't allowed to access DECL along
3019 BASETYPE_PATH, give an error. */
3022 enforce_access (basetype_path, decl)
3023 tree basetype_path, decl;
3025 tree access = compute_access (basetype_path, decl);
3027 if (access == access_private_node)
3029 cp_error_at ("`%+#D' is %s", decl,
3030 TREE_PRIVATE (decl) ? "private"
3031 : "from private base class");
3032 error ("within this context");
3034 else if (access == access_protected_node)
3036 cp_error_at ("`%+#D' %s", decl,
3037 TREE_PROTECTED (decl) ? "is protected"
3038 : "has protected accessibility");
3039 error ("within this context");
3043 /* Perform the conversions in CONVS on the expression EXPR. */
3046 convert_like (convs, expr)
3049 if (ICS_BAD_FLAG (convs)
3050 && TREE_CODE (convs) != USER_CONV
3051 && TREE_CODE (convs) != AMBIG_CONV)
3054 for (; t; t = TREE_OPERAND (t, 0))
3056 if (TREE_CODE (t) == USER_CONV)
3058 expr = convert_like (t, expr);
3061 else if (TREE_CODE (t) == AMBIG_CONV)
3062 return convert_like (t, expr);
3063 else if (TREE_CODE (t) == IDENTITY_CONV)
3066 return convert_for_initialization
3067 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3068 "conversion", NULL_TREE, 0);
3071 switch (TREE_CODE (convs))
3075 struct z_candidate *cand
3076 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3080 if (DECL_CONSTRUCTOR_P (fn))
3082 tree t = build_int_2 (0, 0);
3083 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3085 args = build_scratch_list (NULL_TREE, expr);
3086 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3087 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3088 args = scratch_tree_cons (NULL_TREE, t, args);
3091 args = build_this (expr);
3092 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3094 /* If this is a constructor or a function returning an aggr type,
3095 we need to build up a TARGET_EXPR. */
3096 if (DECL_CONSTRUCTOR_P (fn))
3097 expr = build_cplus_new (TREE_TYPE (convs), expr);
3102 if (type_unknown_p (expr))
3103 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3104 if (TREE_READONLY_DECL_P (expr))
3105 expr = decl_constant_value (expr);
3108 /* Call build_user_type_conversion again for the error. */
3109 return build_user_type_conversion
3110 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3116 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3117 if (expr == error_mark_node)
3118 return error_mark_node;
3120 switch (TREE_CODE (convs))
3123 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3125 /* else fall through */
3127 return build_user_type_conversion
3128 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3130 return convert_to_reference
3131 (TREE_TYPE (convs), expr,
3132 CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
3135 return decay_conversion (expr);
3140 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3141 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3145 convert_default_arg (type, arg)
3148 arg = break_out_target_exprs (arg);
3150 if (TREE_CODE (arg) == CONSTRUCTOR)
3152 arg = digest_init (type, arg, 0);
3153 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3154 "default argument", 0, 0);
3158 /* This could get clobbered by the following call. */
3159 if (TREE_HAS_CONSTRUCTOR (arg))
3160 arg = copy_node (arg);
3162 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3163 "default argument", 0, 0);
3164 #ifdef PROMOTE_PROTOTYPES
3165 if ((TREE_CODE (type) == INTEGER_TYPE
3166 || TREE_CODE (type) == ENUMERAL_TYPE)
3167 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3168 arg = default_conversion (arg);
3176 build_over_call (cand, args, flags)
3177 struct z_candidate *cand;
3182 tree convs = cand->convs;
3183 tree converted_args = NULL_TREE;
3184 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3185 tree conv, arg, val;
3189 /* Give any warnings we noticed during overload resolution. */
3191 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3192 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3194 if (DECL_FUNCTION_MEMBER_P (fn))
3195 enforce_access (cand->basetype_path, fn);
3197 if (args && TREE_CODE (args) != TREE_LIST)
3198 args = build_scratch_list (NULL_TREE, args);
3201 /* The implicit parameters to a constructor are not considered by overload
3202 resolution, and must be of the proper type. */
3203 if (DECL_CONSTRUCTOR_P (fn))
3205 converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3206 arg = TREE_CHAIN (arg);
3207 parm = TREE_CHAIN (parm);
3208 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3210 converted_args = expr_tree_cons
3211 (NULL_TREE, TREE_VALUE (arg), converted_args);
3212 arg = TREE_CHAIN (arg);
3213 parm = TREE_CHAIN (parm);
3216 /* Bypass access control for 'this' parameter. */
3217 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3219 tree parmtype = TREE_VALUE (parm);
3220 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3221 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3223 int dv = (TYPE_VOLATILE (TREE_TYPE (parmtype))
3224 < TYPE_VOLATILE (TREE_TYPE (argtype)));
3225 int dc = (TYPE_READONLY (TREE_TYPE (parmtype))
3226 < TYPE_READONLY (TREE_TYPE (argtype)));
3227 char *p = (dv && dc ? "const and volatile"
3228 : dc ? "const" : dv ? "volatile" : "");
3230 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards %s",
3231 TREE_TYPE (argtype), fn, p);
3233 converted_args = expr_tree_cons
3234 (NULL_TREE, convert_force (TREE_VALUE (parm), TREE_VALUE (arg), CONV_C_CAST),
3236 parm = TREE_CHAIN (parm);
3237 arg = TREE_CHAIN (arg);
3243 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3245 tree type = TREE_VALUE (parm);
3247 conv = TREE_VEC_ELT (convs, i);
3248 if (ICS_BAD_FLAG (conv))
3251 val = TREE_VALUE (arg);
3253 for (; t; t = TREE_OPERAND (t, 0))
3255 if (TREE_CODE (t) == USER_CONV
3256 || TREE_CODE (t) == AMBIG_CONV)
3258 val = convert_like (t, val);
3261 else if (TREE_CODE (t) == IDENTITY_CONV)
3264 val = convert_for_initialization
3265 (NULL_TREE, type, val, LOOKUP_NORMAL,
3266 "argument passing", fn, i - is_method);
3269 val = convert_like (conv, TREE_VALUE (arg));
3271 #ifdef PROMOTE_PROTOTYPES
3272 if ((TREE_CODE (type) == INTEGER_TYPE
3273 || TREE_CODE (type) == ENUMERAL_TYPE)
3274 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3275 val = default_conversion (val);
3277 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3280 /* Default arguments */
3281 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3283 tree arg = TREE_PURPOSE (parm);
3285 if (DECL_TEMPLATE_INFO (fn))
3287 /* This came from a template. Instantiate the default arg here,
3288 not in tsubst. In the case of something like:
3296 we must be careful to do name lookup in the scope of
3297 S<T>, rather than in the current class. */
3298 if (DECL_CLASS_SCOPE_P (fn))
3299 pushclass (DECL_REAL_CONTEXT (fn), 2);
3301 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), NULL_TREE);
3303 if (DECL_CLASS_SCOPE_P (fn))
3306 converted_args = expr_tree_cons
3307 (NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg),
3312 for (; arg; arg = TREE_CHAIN (arg))
3314 val = TREE_VALUE (arg);
3316 if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
3317 && (TYPE_PRECISION (TREE_TYPE (val))
3318 < TYPE_PRECISION (double_type_node)))
3319 /* Convert `float' to `double'. */
3320 val = cp_convert (double_type_node, val);
3321 else if (IS_AGGR_TYPE (TREE_TYPE (val))
3322 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val)))
3323 cp_warning ("cannot pass objects of type `%T' through `...'",
3326 /* Convert `short' and `char' to full-size `int'. */
3327 val = default_conversion (val);
3329 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3332 converted_args = nreverse (converted_args);
3334 /* Avoid actually calling copy constructors and copy assignment operators,
3336 if (DECL_CONSTRUCTOR_P (fn)
3337 && TREE_VEC_LENGTH (convs) == 1
3338 && copy_args_p (fn))
3341 arg = TREE_CHAIN (converted_args);
3342 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3343 arg = TREE_CHAIN (arg);
3344 arg = TREE_VALUE (arg);
3346 /* Pull out the real argument, disregarding const-correctness. */
3348 while (TREE_CODE (targ) == NOP_EXPR
3349 || TREE_CODE (targ) == NON_LVALUE_EXPR
3350 || TREE_CODE (targ) == CONVERT_EXPR)
3351 targ = TREE_OPERAND (targ, 0);
3352 if (TREE_CODE (targ) == ADDR_EXPR)
3354 targ = TREE_OPERAND (targ, 0);
3355 if (! comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3356 TYPE_MAIN_VARIANT (TREE_TYPE (targ)), 1))
3365 arg = build_indirect_ref (arg, 0);
3367 /* [class.copy]: the copy constructor is implicitly defined even if
3368 the implementation elided its use. */
3369 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3372 /* If we're creating a temp and we already have one, don't create a
3373 new one. If we're not creating a temp but we get one, use
3374 INIT_EXPR to collapse the temp into our target. Otherwise, if the
3375 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3376 temp or an INIT_EXPR otherwise. */
3377 if (integer_zerop (TREE_VALUE (args)))
3379 if (! real_lvalue_p (arg))
3381 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3383 val = build (VAR_DECL, DECL_CONTEXT (fn));
3384 layout_decl (val, 0);
3385 val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
3386 TREE_SIDE_EFFECTS (val) = 1;
3390 else if (! real_lvalue_p (arg)
3391 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3393 tree to = stabilize_reference
3394 (build_indirect_ref (TREE_VALUE (args), 0));
3396 /* Don't copy the padding byte; it might not have been allocated
3397 if to is a base subobject. */
3398 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3399 return build_unary_op
3400 (ADDR_EXPR, build (COMPOUND_EXPR, TREE_TYPE (to),
3401 cp_convert (void_type_node, arg), to),
3404 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
3405 TREE_SIDE_EFFECTS (val) = 1;
3406 return build_unary_op (ADDR_EXPR, val, 0);
3409 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
3411 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
3413 tree to = stabilize_reference
3414 (build_indirect_ref (TREE_VALUE (converted_args), 0));
3416 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
3418 /* Don't copy the padding byte; it might not have been allocated
3419 if to is a base subobject. */
3420 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3421 return build (COMPOUND_EXPR, TREE_TYPE (to),
3422 cp_convert (void_type_node, arg), to);
3424 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
3425 TREE_SIDE_EFFECTS (val) = 1;
3431 if (DECL_CLASS_SCOPE_P (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
3432 return build_signature_method_call (fn, converted_args);
3433 else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3435 tree t, *p = &TREE_VALUE (converted_args);
3436 tree binfo = get_binfo
3437 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
3438 *p = convert_pointer_to_real (binfo, *p);
3439 if (TREE_SIDE_EFFECTS (*p))
3440 *p = save_expr (*p);
3441 t = build_pointer_type (TREE_TYPE (fn));
3442 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
3445 else if (DECL_INLINE (fn))
3446 fn = inline_conversion (fn);
3448 fn = build_addr_func (fn);
3450 /* Recognize certain built-in functions so we can make tree-codes
3451 other than CALL_EXPR. We do this when it enables fold-const.c
3452 to do something useful. */
3454 if (TREE_CODE (fn) == ADDR_EXPR
3455 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3456 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
3457 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
3462 if (converted_args == 0)
3463 return integer_zero_node;
3464 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
3469 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3470 if (TREE_TYPE (fn) == void_type_node)
3472 fn = require_complete_type (fn);
3473 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
3474 fn = build_cplus_new (TREE_TYPE (fn), fn);
3475 return convert_from_reference (fn);
3479 build_new_method_call (instance, name, args, basetype_path, flags)
3480 tree instance, name, args, basetype_path;
3483 struct z_candidate *candidates = 0, *cand;
3484 tree explicit_targs = NULL_TREE;
3485 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
3487 tree user_args = args;
3488 tree templates = NULL_TREE;
3489 int template_only = 0;
3491 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3493 explicit_targs = TREE_OPERAND (name, 1);
3494 name = TREE_OPERAND (name, 0);
3495 if (TREE_CODE (name) == TEMPLATE_DECL)
3496 name = DECL_NAME (name);
3500 /* If there is an extra argument for controlling virtual bases,
3501 remove it for error reporting. */
3502 if (flags & LOOKUP_HAS_IN_CHARGE)
3503 user_args = TREE_CHAIN (args);
3505 args = resolve_args (args);
3507 if (args == error_mark_node)
3508 return error_mark_node;
3510 if (instance == NULL_TREE)
3511 basetype = BINFO_TYPE (basetype_path);
3514 if (TREE_CODE (instance) == OFFSET_REF)
3515 instance = resolve_offset_ref (instance);
3516 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3517 instance = convert_from_reference (instance);
3518 basetype = TREE_TYPE (instance);
3520 /* XXX this should be handled before we get here. */
3521 if (! IS_AGGR_TYPE (basetype)
3522 && ! (TYPE_LANG_SPECIFIC (basetype)
3523 && (IS_SIGNATURE_POINTER (basetype)
3524 || IS_SIGNATURE_REFERENCE (basetype))))
3526 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
3527 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
3528 name, instance, basetype);
3530 return error_mark_node;
3533 /* If `instance' is a signature pointer/reference and `name' is
3534 not a constructor, we are calling a signature member function.
3535 In that case set the `basetype' to the signature type. */
3536 if ((IS_SIGNATURE_POINTER (basetype)
3537 || IS_SIGNATURE_REFERENCE (basetype))
3538 && TYPE_IDENTIFIER (basetype) != name)
3539 basetype = SIGNATURE_TYPE (basetype);
3542 if (basetype_path == NULL_TREE)
3543 basetype_path = TYPE_BINFO (basetype);
3547 instance_ptr = build_this (instance);
3549 if (! template_only)
3551 /* XXX this should be handled before we get here. */
3552 fns = build_field_call (basetype_path, instance_ptr, name, args);
3559 instance_ptr = build_int_2 (0, 0);
3560 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
3564 = (name == ctor_identifier ? constructor_name (basetype) : name);
3566 fns = lookup_fnfields (basetype_path, name, 1);
3568 if (fns == error_mark_node)
3569 return error_mark_node;
3572 tree fn = TREE_VALUE (fns);
3573 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3574 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3576 flags |= LOOKUP_HAS_IN_CHARGE;
3577 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3579 mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
3580 for (; fn; fn = OVL_NEXT (fn))
3582 tree t = OVL_CURRENT (fn);
3585 /* We can end up here for copy-init of same or base class. */
3586 if (name == ctor_identifier
3587 && (flags & LOOKUP_ONLYCONVERTING)
3588 && DECL_NONCONVERTING_P (t))
3590 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
3591 this_arglist = mem_args;
3593 this_arglist = args;
3595 if (TREE_CODE (t) == TEMPLATE_DECL)
3597 /* A member template. */
3598 templates = scratch_tree_cons (NULL_TREE, t, templates);
3600 add_template_candidate (candidates, t, explicit_targs,
3602 TREE_TYPE (name), flags, DEDUCE_CALL);
3604 else if (! template_only)
3605 candidates = add_function_candidate (candidates, t,
3606 this_arglist, flags);
3609 candidates->basetype_path = TREE_PURPOSE (fns);
3613 if (! any_viable (candidates))
3615 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
3616 if (flags & LOOKUP_SPECULATIVELY)
3618 cp_error ("no matching function for call to `%T::%D (%A)%V'", basetype,
3619 pretty_name, user_args, TREE_TYPE (TREE_TYPE (instance_ptr)));
3620 print_z_candidates (candidates);
3621 return error_mark_node;
3623 candidates = splice_viable (candidates);
3624 cand = tourney (candidates);
3628 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
3630 print_z_candidates (candidates);
3631 return error_mark_node;
3634 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
3635 && instance == current_class_ref
3636 && DECL_CONSTRUCTOR_P (current_function_decl)
3637 && ! (flags & LOOKUP_NONVIRTUAL)
3638 && value_member (cand->fn, get_abstract_virtuals (basetype)))
3639 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
3640 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3641 && TREE_CODE (instance_ptr) == NOP_EXPR
3642 && TREE_OPERAND (instance_ptr, 0) == error_mark_node)
3643 cp_error ("cannot call member function `%D' without object", cand->fn);
3645 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
3646 && ((instance == current_class_ref && (dtor_label || ctor_label))
3647 || resolves_to_fixed_type_p (instance, 0)))
3648 flags |= LOOKUP_NONVIRTUAL;
3650 /* Pedantically, normal function declarations are never considered
3651 to refer to template instantiations, so we only do this with
3653 if (flag_guiding_decls && templates && ! cand->template
3654 && ! DECL_INITIAL (cand->fn))
3655 add_maybe_template (cand->fn, templates);
3657 return build_over_call
3659 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
3663 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
3664 subsequence of ICS2. */
3667 is_subseq (ics1, ics2)
3670 /* We can assume that a conversion of the same code
3671 between the same types indicates a subsequence since we only get
3672 here if the types we are converting from are the same. */
3674 while (TREE_CODE (ics1) == RVALUE_CONV
3675 || TREE_CODE (ics1) == LVALUE_CONV)
3676 ics1 = TREE_OPERAND (ics1, 0);
3680 while (TREE_CODE (ics2) == RVALUE_CONV
3681 || TREE_CODE (ics2) == LVALUE_CONV)
3682 ics2 = TREE_OPERAND (ics2, 0);
3684 if (TREE_CODE (ics2) == USER_CONV
3685 || TREE_CODE (ics2) == AMBIG_CONV
3686 || TREE_CODE (ics2) == IDENTITY_CONV)
3687 /* At this point, ICS1 cannot be a proper subsequence of
3688 ICS2. We can get a USER_CONV when we are comparing the
3689 second standard conversion sequence of two user conversion
3693 ics2 = TREE_OPERAND (ics2, 0);
3695 if (TREE_CODE (ics2) == TREE_CODE (ics1)
3696 && comptypes (TREE_TYPE (ics2), TREE_TYPE (ics1), 1)
3697 && comptypes (TREE_TYPE (TREE_OPERAND (ics2, 0)),
3698 TREE_TYPE (TREE_OPERAND (ics1, 0)), 1))
3703 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
3704 be any _TYPE nodes. */
3707 is_properly_derived_from (derived, base)
3711 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
3712 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
3715 /* We only allow proper derivation here. The DERIVED_FROM_P macro
3716 considers every class derived from itself. */
3717 return (!comptypes (TYPE_MAIN_VARIANT (derived),
3718 TYPE_MAIN_VARIANT (base), 1)
3719 && DERIVED_FROM_P (base, derived));
3722 /* We build the ICS for an implicit object parameter as a pointer
3723 conversion sequence. However, such a sequence should be compared
3724 as if it were a reference conversion sequence. If ICS is the
3725 implicit conversion sequence for an implicit object parameter,
3726 modify it accordingly. */
3729 maybe_handle_implicit_object (ics)
3732 if (ICS_THIS_FLAG (*ics))
3734 /* [over.match.funcs]
3736 For non-static member functions, the type of the
3737 implicit object parameter is "reference to cv X"
3738 where X is the class of which the function is a
3739 member and cv is the cv-qualification on the member
3740 function declaration. */
3742 if (TREE_CODE (t) == PTR_CONV)
3743 t = TREE_OPERAND (t, 0);
3744 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3745 t = build_conv (REF_BIND, TREE_TYPE (*ics), t);
3746 ICS_STD_RANK (t) = ICS_STD_RANK (*ics);
3751 /* If ICS is a REF_BIND, modify it appropriately, set ORIG_TO_TYPE
3752 to the type the reference originally referred to, and return 1.
3753 Otherwise, return 0. */
3756 maybe_handle_ref_bind (ics, reference_type)
3758 tree* reference_type;
3760 if (TREE_CODE (*ics) == REF_BIND)
3764 When a parameter of reference type binds directly
3765 (_dcl.init.ref_) to an argument expression, the implicit
3766 conversion sequence is the identity conversion, unless the
3767 argument expression has a type that is a derived class of the
3768 parameter type, in which case the implicit conversion
3769 sequence is a derived-to-base Conversion.
3771 If the parameter binds directly to the result of applying a
3772 conversion function to the argument expression, the implicit
3773 conversion sequence is a user-defined conversion sequence
3774 (_over.ics.user_), with the second standard conversion
3775 sequence either an identity conversion or, if the conversion
3776 function returns an entity of a type that is a derived class
3777 of the parameter type, a derived-to-base Conversion.
3779 When a parameter of reference type is not bound directly to
3780 an argument expression, the conversion sequence is the one
3781 required to convert the argument expression to the underlying
3782 type of the reference according to _over.best.ics_.
3783 Conceptually, this conversion sequence corresponds to
3784 copy-initializing a temporary of the underlying type with the
3785 argument expression. Any difference in top-level
3786 cv-qualification is subsumed by the initialization itself and
3787 does not constitute a conversion. */
3789 *reference_type = TREE_TYPE (TREE_TYPE (*ics));
3790 *ics = TREE_OPERAND (*ics, 0);
3791 if (TREE_CODE (*ics) == IDENTITY_CONV
3792 && is_properly_derived_from (TREE_TYPE (*ics), *reference_type))
3793 *ics = build_conv (BASE_CONV, *reference_type, *ics);
3800 /* Compare two implicit conversion sequences according to the rules set out in
3801 [over.ics.rank]. Return values:
3803 1: ics1 is better than ics2
3804 -1: ics2 is better than ics1
3805 0: ics1 and ics2 are indistinguishable */
3808 compare_ics (ics1, ics2)
3815 tree deref_from_type1 = NULL_TREE;
3816 tree deref_from_type2;
3817 tree deref_to_type1;
3818 tree deref_to_type2;
3820 /* REF_BINDING is non-zero if the result of the conversion sequence
3821 is a reference type. In that case REFERENCE_TYPE is the
3825 tree reference_type1;
3826 tree reference_type2;
3828 /* Handle implicit object parameters. */
3829 maybe_handle_implicit_object (&ics1);
3830 maybe_handle_implicit_object (&ics2);
3832 /* Handle reference parameters. */
3833 ref_binding1 = maybe_handle_ref_bind (&ics1, &reference_type1);
3834 ref_binding2 = maybe_handle_ref_bind (&ics2, &reference_type2);
3838 When comparing the basic forms of implicit conversion sequences (as
3839 defined in _over.best.ics_)
3841 --a standard conversion sequence (_over.ics.scs_) is a better
3842 conversion sequence than a user-defined conversion sequence
3843 or an ellipsis conversion sequence, and
3845 --a user-defined conversion sequence (_over.ics.user_) is a
3846 better conversion sequence than an ellipsis conversion sequence
3847 (_over.ics.ellipsis_). */
3848 if (ICS_RANK (ics1) > ICS_RANK (ics2))
3850 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
3853 if (ICS_RANK (ics1) == BAD_RANK)
3855 /* Both ICS are bad. We try to make a decision based on what
3856 would have happenned if they'd been good. */
3857 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
3858 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3860 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
3861 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3864 /* We couldn't make up our minds; try to figure it out below. */
3867 if (ICS_ELLIPSIS_FLAG (ics1))
3868 /* Both conversions are ellipsis conversions. */
3871 /* User-defined conversion sequence U1 is a better conversion sequence
3872 than another user-defined conversion sequence U2 if they contain the
3873 same user-defined conversion operator or constructor and if the sec-
3874 ond standard conversion sequence of U1 is better than the second
3875 standard conversion sequence of U2. */
3877 if (ICS_USER_FLAG (ics1))
3881 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
3882 if (TREE_CODE (t1) == AMBIG_CONV)
3884 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
3885 if (TREE_CODE (t2) == AMBIG_CONV)
3888 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
3891 /* We can just fall through here, after setting up
3892 FROM_TYPE1 and FROM_TYPE2. */
3893 from_type1 = TREE_TYPE (t1);
3894 from_type2 = TREE_TYPE (t2);
3898 /* We're dealing with two standard conversion sequences.
3902 Standard conversion sequence S1 is a better conversion
3903 sequence than standard conversion sequence S2 if
3905 --S1 is a proper subsequence of S2 (comparing the conversion
3906 sequences in the canonical form defined by _over.ics.scs_,
3907 excluding any Lvalue Transformation; the identity
3908 conversion sequence is considered to be a subsequence of
3909 any non-identity conversion sequence */
3912 while (TREE_CODE (from_type1) != IDENTITY_CONV)
3913 from_type1 = TREE_OPERAND (from_type1, 0);
3914 from_type1 = TREE_TYPE (from_type1);
3917 while (TREE_CODE (from_type2) != IDENTITY_CONV)
3918 from_type2 = TREE_OPERAND (from_type2, 0);
3919 from_type2 = TREE_TYPE (from_type2);
3922 if (comptypes (from_type1, from_type2, 1))
3924 if (is_subseq (ics1, ics2))
3926 if (is_subseq (ics2, ics1))
3929 /* Otherwise, one sequence cannot be a subsequence of the other; they
3930 don't start with the same type. This can happen when comparing the
3931 second standard conversion sequence in two user-defined conversion
3938 --the rank of S1 is better than the rank of S2 (by the rules
3941 Standard conversion sequences are ordered by their ranks: an Exact
3942 Match is a better conversion than a Promotion, which is a better
3943 conversion than a Conversion.
3945 Two conversion sequences with the same rank are indistinguishable
3946 unless one of the following rules applies:
3948 --A conversion that is not a conversion of a pointer, or pointer
3949 to member, to bool is better than another conversion that is such
3952 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
3953 so that we do not have to check it explicitly. */
3954 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3956 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
3959 to_type1 = TREE_TYPE (ics1);
3960 to_type2 = TREE_TYPE (ics2);
3962 if (TYPE_PTR_P (from_type1)
3963 && TYPE_PTR_P (from_type2)
3964 && TYPE_PTR_P (to_type1)
3965 && TYPE_PTR_P (to_type2))
3967 deref_from_type1 = TREE_TYPE (from_type1);
3968 deref_from_type2 = TREE_TYPE (from_type2);
3969 deref_to_type1 = TREE_TYPE (to_type1);
3970 deref_to_type2 = TREE_TYPE (to_type2);
3972 /* The rules for pointers to members A::* are just like the rules
3973 for pointers A*, except opposite: if B is derived from A then
3974 A::* converts to B::*, not vice versa. For that reason, we
3975 switch the from_ and to_ variables here. */
3976 else if (TYPE_PTRMEM_P (from_type1)
3977 && TYPE_PTRMEM_P (from_type2)
3978 && TYPE_PTRMEM_P (to_type1)
3979 && TYPE_PTRMEM_P (to_type2))
3981 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
3982 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
3983 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
3984 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
3986 else if (TYPE_PTRMEMFUNC_P (from_type1)
3987 && TYPE_PTRMEMFUNC_P (from_type2)
3988 && TYPE_PTRMEMFUNC_P (to_type1)
3989 && TYPE_PTRMEMFUNC_P (to_type2))
3991 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
3992 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
3993 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
3994 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
3997 if (deref_from_type1 != NULL_TREE
3998 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
3999 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4001 /* This was one of the pointer or pointer-like conversions.
4005 --If class B is derived directly or indirectly from class A,
4006 conversion of B* to A* is better than conversion of B* to
4007 void*, and conversion of A* to void* is better than
4008 conversion of B* to void*. */
4009 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4010 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4012 if (is_properly_derived_from (deref_from_type1,
4015 else if (is_properly_derived_from (deref_from_type2,
4019 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4020 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4022 if (comptypes (deref_from_type1, deref_from_type2, 1))
4024 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4026 if (is_properly_derived_from (deref_from_type1,
4030 /* We know that DEREF_TO_TYPE1 is `void' here. */
4031 else if (is_properly_derived_from (deref_from_type1,
4036 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4037 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4041 --If class B is derived directly or indirectly from class A
4042 and class C is derived directly or indirectly from B,
4044 --conversion of C* to B* is better than conversion of C* to
4047 --conversion of B* to A* is better than conversion of C* to
4049 if (comptypes (deref_from_type1, deref_from_type2, 1))
4051 if (is_properly_derived_from (deref_to_type1,
4054 else if (is_properly_derived_from (deref_to_type2,
4058 else if (comptypes (deref_to_type1, deref_to_type2, 1))
4060 if (is_properly_derived_from (deref_from_type2,
4063 else if (is_properly_derived_from (deref_from_type1,
4069 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4070 && comptypes (from_type1, from_type2, 1))
4074 --binding of an expression of type C to a reference of type
4075 B& is better than binding an expression of type C to a
4076 reference of type A&
4078 --conversion of C to B is better than conversion of C to A, */
4079 if (is_properly_derived_from (from_type1, to_type1)
4080 && is_properly_derived_from (from_type1, to_type2))
4082 if (is_properly_derived_from (to_type1, to_type2))
4084 else if (is_properly_derived_from (to_type2, to_type1))
4088 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4089 && comptypes (to_type1, to_type2, 1))
4093 --binding of an expression of type B to a reference of type
4094 A& is better than binding an expression of type C to a
4095 reference of type A&,
4097 --onversion of B to A is better than conversion of C to A */
4098 if (is_properly_derived_from (from_type1, to_type1)
4099 && is_properly_derived_from (from_type2, to_type1))
4101 if (is_properly_derived_from (from_type2, from_type1))
4103 else if (is_properly_derived_from (from_type1, from_type2))
4110 --S1 and S2 differ only in their qualification conversion and yield
4111 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4112 qualification signature of type T1 is a proper subset of the cv-
4113 qualification signature of type T2 */
4114 if (TREE_CODE (ics1) == QUAL_CONV
4115 && TREE_CODE (ics2) == QUAL_CONV
4116 && comptypes (from_type1, from_type2, 1))
4117 return comp_cv_qual_signature (to_type1, to_type2);
4121 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4122 types to which the references refer are the same type except for
4123 top-level cv-qualifiers, and the type to which the reference
4124 initialized by S2 refers is more cv-qualified than the type to
4125 which the reference initialized by S1 refers */
4127 if (ref_binding1 && ref_binding2
4128 && comptypes (TYPE_MAIN_VARIANT (to_type1),
4129 TYPE_MAIN_VARIANT (to_type2), 1))
4130 return comp_cv_qualification (reference_type2, reference_type1);
4132 /* Neither conversion sequence is better than the other. */
4136 /* The source type for this standard conversion sequence. */
4142 for (;; t = TREE_OPERAND (t, 0))
4144 if (TREE_CODE (t) == USER_CONV
4145 || TREE_CODE (t) == AMBIG_CONV
4146 || TREE_CODE (t) == IDENTITY_CONV)
4147 return TREE_TYPE (t);
4149 my_friendly_abort (1823);
4152 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4153 a pointer to LOSER and re-running joust to produce the warning if WINNER
4154 is actually used. */
4157 add_warning (winner, loser)
4158 struct z_candidate *winner, *loser;
4160 winner->warnings = expr_tree_cons (NULL_PTR,
4161 build_expr_ptr_wrapper (loser),
4165 /* Compare two candidates for overloading as described in
4166 [over.match.best]. Return values:
4168 1: cand1 is better than cand2
4169 -1: cand2 is better than cand1
4170 0: cand1 and cand2 are indistinguishable */
4173 joust (cand1, cand2, warn)
4174 struct z_candidate *cand1, *cand2;
4178 int i, off1 = 0, off2 = 0, len;
4180 /* Candidates that involve bad conversions are always worse than those
4182 if (cand1->viable > cand2->viable)
4184 if (cand1->viable < cand2->viable)
4187 /* a viable function F1
4188 is defined to be a better function than another viable function F2 if
4189 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4190 ICSi(F2), and then */
4192 /* for some argument j, ICSj(F1) is a better conversion sequence than
4195 /* For comparing static and non-static member functions, we ignore the
4196 implicit object parameter of the non-static function. The WP says to
4197 pretend that the static function has an object parm, but that won't
4198 work with operator overloading. */
4199 len = TREE_VEC_LENGTH (cand1->convs);
4200 if (len != TREE_VEC_LENGTH (cand2->convs))
4202 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4203 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4205 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4206 && DECL_STATIC_FUNCTION_P (cand2->fn))
4212 my_friendly_abort (42);
4215 for (i = 0; i < len; ++i)
4217 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4218 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4219 int comp = compare_ics (t1, t2);
4224 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4225 && TREE_CODE (t1) == STD_CONV
4226 && TREE_CODE (t2) == STD_CONV
4227 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4228 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4229 && (TYPE_PRECISION (TREE_TYPE (t1))
4230 == TYPE_PRECISION (TREE_TYPE (t2)))
4231 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4232 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4235 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4237 struct z_candidate *w, *l;
4239 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4240 w = cand1, l = cand2;
4242 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4243 w = cand2, l = cand1;
4247 cp_warning ("passing `%T' chooses `%T' over `%T'",
4248 type, type1, type2);
4249 cp_warning (" in call to `%D'", w->fn);
4255 if (winner && comp != winner)
4264 /* warn about confusing overload resolution */
4265 if (winner && cand1->second_conv
4266 && ! DECL_CONSTRUCTOR_P (cand1->fn)
4267 && ! DECL_CONSTRUCTOR_P (cand2->fn))
4269 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4270 if (comp && comp != winner)
4272 struct z_candidate *w, *l;
4274 w = cand1, l = cand2;
4276 w = cand2, l = cand1;
4279 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4280 cp_warning (" for conversion from `%T' to `%T'",
4281 TREE_TYPE (source_type (TREE_VEC_ELT (w->convs, 0))),
4282 TREE_TYPE (w->second_conv));
4283 cp_warning (" because conversion sequence for `this' argument is better");
4294 F1 is a non-template function and F2 is a template function */
4296 if (! cand1->template && cand2->template)
4298 else if (cand1->template && ! cand2->template)
4300 else if (cand1->template && cand2->template)
4301 winner = more_specialized
4302 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4306 the context is an initialization by user-defined conversion (see
4307 _dcl.init_ and _over.match.user_) and the standard conversion
4308 sequence from the return type of F1 to the destination type (i.e.,
4309 the type of the entity being initialized) is a better conversion
4310 sequence than the standard conversion sequence from the return type
4311 of F2 to the destination type. */
4313 if (! winner && cand1->second_conv)
4314 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4316 /* If the built-in candidates are the same, arbitrarily pick one. */
4317 if (! winner && cand1->fn == cand2->fn
4318 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4320 for (i = 0; i < len; ++i)
4321 if (! comptypes (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4322 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i)), 1))
4324 if (i == TREE_VEC_LENGTH (cand1->convs))
4327 /* Kludge around broken overloading rules whereby
4328 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4329 that takes references and another that takes values. */
4330 if (cand1->fn == ansi_opname[COND_EXPR])
4332 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4333 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4334 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4335 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4337 if (comptypes (t1, t2, 1))
4339 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4341 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4349 /* Extension: If the worst conversion for one candidate is worse than the
4350 worst conversion for the other, take the first. */
4351 if (! winner && ! pedantic)
4353 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4355 for (i = 0; i < len; ++i)
4357 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4358 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4359 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
4360 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
4372 /* Given a list of candidates for overloading, find the best one, if any.
4373 This algorithm has a worst case of O(2n) (winner is last), and a best
4374 case of O(n/2) (totally ambiguous); much better than a sorting
4377 static struct z_candidate *
4378 tourney (candidates)
4379 struct z_candidate *candidates;
4381 struct z_candidate *champ = candidates, *challenger;
4384 /* Walk through the list once, comparing each current champ to the next
4385 candidate, knocking out a candidate or two with each comparison. */
4387 for (challenger = champ->next; challenger; )
4389 fate = joust (champ, challenger, 0);
4391 challenger = challenger->next;
4396 champ = challenger->next;
4403 challenger = champ->next;
4407 /* Make sure the champ is better than all the candidates it hasn't yet
4408 been compared to. This may do one more comparison than necessary. Oh
4411 for (challenger = candidates; challenger != champ;
4412 challenger = challenger->next)
4414 fate = joust (champ, challenger, 0);
4423 can_convert (to, from)
4426 tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
4427 return (t && ! ICS_BAD_FLAG (t));
4431 can_convert_arg (to, from, arg)
4434 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
4435 return (t && ! ICS_BAD_FLAG (t));