1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) and
5 modified by Brendan Kehoe (brendan@cygnus.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 /* High-level class interface. */
37 #include "diagnostic.h"
39 extern int inhibit_warnings
;
41 static tree build_new_method_call
PARAMS ((tree
, tree
, tree
, tree
, int));
43 static tree build_field_call
PARAMS ((tree
, tree
, tree
, tree
));
44 static struct z_candidate
* tourney
PARAMS ((struct z_candidate
*));
45 static int equal_functions
PARAMS ((tree
, tree
));
46 static int joust
PARAMS ((struct z_candidate
*, struct z_candidate
*, int));
47 static int compare_ics
PARAMS ((tree
, tree
));
48 static tree build_over_call
PARAMS ((struct z_candidate
*, tree
, int));
49 static tree build_java_interface_fn_ref
PARAMS ((tree
, tree
));
50 #define convert_like(CONV, EXPR) \
51 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0)
52 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
53 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0)
54 static tree convert_like_real
PARAMS ((tree
, tree
, tree
, int, int));
55 static void op_error
PARAMS ((enum tree_code
, enum tree_code
, tree
, tree
,
57 static tree build_object_call
PARAMS ((tree
, tree
));
58 static tree resolve_args
PARAMS ((tree
));
59 static struct z_candidate
* build_user_type_conversion_1
60 PARAMS ((tree
, tree
, int));
61 static void print_z_candidates
PARAMS ((struct z_candidate
*));
62 static tree build_this
PARAMS ((tree
));
63 static struct z_candidate
* splice_viable
PARAMS ((struct z_candidate
*));
64 static int any_viable
PARAMS ((struct z_candidate
*));
65 static struct z_candidate
* add_template_candidate
66 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, tree
, tree
, int,
68 static struct z_candidate
* add_template_candidate_real
69 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, tree
, tree
, int,
70 tree
, unification_kind_t
));
71 static struct z_candidate
* add_template_conv_candidate
72 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, tree
));
73 static struct z_candidate
* add_builtin_candidates
74 PARAMS ((struct z_candidate
*, enum tree_code
, enum tree_code
,
76 static struct z_candidate
* add_builtin_candidate
77 PARAMS ((struct z_candidate
*, enum tree_code
, enum tree_code
,
78 tree
, tree
, tree
, tree
*, tree
*, int));
79 static int is_complete
PARAMS ((tree
));
80 static struct z_candidate
* build_builtin_candidate
81 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, tree
*, tree
*,
83 static struct z_candidate
* add_conv_candidate
84 PARAMS ((struct z_candidate
*, tree
, tree
, tree
));
85 static struct z_candidate
* add_function_candidate
86 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, int));
87 static tree implicit_conversion
PARAMS ((tree
, tree
, tree
, int));
88 static tree standard_conversion
PARAMS ((tree
, tree
, tree
));
89 static tree reference_binding
PARAMS ((tree
, tree
, tree
, int));
90 static tree non_reference
PARAMS ((tree
));
91 static tree build_conv
PARAMS ((enum tree_code
, tree
, tree
));
92 static int is_subseq
PARAMS ((tree
, tree
));
93 static tree maybe_handle_ref_bind
PARAMS ((tree
*));
94 static void maybe_handle_implicit_object
PARAMS ((tree
*));
95 static struct z_candidate
* add_candidate
PARAMS ((struct z_candidate
*,
97 static tree source_type
PARAMS ((tree
));
98 static void add_warning
PARAMS ((struct z_candidate
*, struct z_candidate
*));
99 static int reference_related_p
PARAMS ((tree
, tree
));
100 static int reference_compatible_p
PARAMS ((tree
, tree
));
101 static tree convert_class_to_reference
PARAMS ((tree
, tree
, tree
));
102 static tree direct_reference_binding
PARAMS ((tree
, tree
));
103 static int promoted_arithmetic_type_p
PARAMS ((tree
));
104 static tree conditional_conversion
PARAMS ((tree
, tree
));
107 build_vfield_ref (datum
, type
)
112 if (datum
== error_mark_node
)
113 return error_mark_node
;
115 if (TREE_CODE (TREE_TYPE (datum
)) == REFERENCE_TYPE
)
116 datum
= convert_from_reference (datum
);
118 if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type
))
119 rval
= build (COMPONENT_REF
, TREE_TYPE (TYPE_VFIELD (type
)),
120 datum
, TYPE_VFIELD (type
));
122 rval
= build_component_ref (datum
, DECL_NAME (TYPE_VFIELD (type
)), NULL_TREE
, 0);
127 /* Build a call to a member of an object. I.e., one that overloads
128 operator ()(), or is a pointer-to-function or pointer-to-method. */
131 build_field_call (basetype_path
, instance_ptr
, name
, parms
)
132 tree basetype_path
, instance_ptr
, name
, parms
;
134 tree field
, instance
;
136 if (IDENTIFIER_CTOR_OR_DTOR_P (name
))
139 /* Speed up the common case. */
140 if (instance_ptr
== current_class_ptr
141 && IDENTIFIER_CLASS_VALUE (name
) == NULL_TREE
)
144 field
= lookup_field (basetype_path
, name
, 1, 0);
146 if (field
== error_mark_node
|| field
== NULL_TREE
)
149 if (TREE_CODE (field
) == FIELD_DECL
|| TREE_CODE (field
) == VAR_DECL
)
151 /* If it's a field, try overloading operator (),
152 or calling if the field is a pointer-to-function. */
153 instance
= build_indirect_ref (instance_ptr
, NULL
);
154 instance
= build_component_ref_1 (instance
, field
, 0);
156 if (instance
== error_mark_node
)
157 return error_mark_node
;
159 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
160 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
,
161 instance
, parms
, NULL_TREE
);
162 else if (TREE_CODE (TREE_TYPE (instance
)) == FUNCTION_TYPE
163 || (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
164 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
)))
166 return build_function_call (instance
, parms
);
172 /* Returns nonzero iff the destructor name specified in NAME
173 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
177 check_dtor_name (basetype
, name
)
180 name
= TREE_OPERAND (name
, 0);
182 /* Just accept something we've already complained about. */
183 if (name
== error_mark_node
)
186 if (TREE_CODE (name
) == TYPE_DECL
)
187 name
= TREE_TYPE (name
);
188 else if (TYPE_P (name
))
190 else if (TREE_CODE (name
) == IDENTIFIER_NODE
)
192 if ((IS_AGGR_TYPE (basetype
) && name
== constructor_name (basetype
))
193 || (TREE_CODE (basetype
) == ENUMERAL_TYPE
194 && name
== TYPE_IDENTIFIER (basetype
)))
197 name
= get_type_value (name
);
201 template <class T> struct S { ~S(); };
205 NAME will be a class template. */
206 else if (DECL_CLASS_TEMPLATE_P (name
))
211 if (name
&& TYPE_MAIN_VARIANT (basetype
) == TYPE_MAIN_VARIANT (name
))
216 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
217 This is how virtual function calls are avoided. */
220 build_scoped_method_call (exp
, basetype
, name
, parms
)
221 tree exp
, basetype
, name
, parms
;
223 /* Because this syntactic form does not allow
224 a pointer to a base class to be `stolen',
225 we need not protect the derived->base conversion
228 @@ But we do have to check access privileges later. */
230 tree type
= TREE_TYPE (exp
);
232 if (type
== error_mark_node
233 || basetype
== error_mark_node
)
234 return error_mark_node
;
236 if (processing_template_decl
)
238 if (TREE_CODE (name
) == BIT_NOT_EXPR
239 && TREE_CODE (TREE_OPERAND (name
, 0)) == IDENTIFIER_NODE
)
241 tree type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 0);
243 name
= build_min_nt (BIT_NOT_EXPR
, type
);
245 name
= build_min_nt (SCOPE_REF
, basetype
, name
);
246 return build_min_nt (METHOD_CALL_EXPR
, name
, exp
, parms
, NULL_TREE
);
249 if (TREE_CODE (type
) == REFERENCE_TYPE
)
250 type
= TREE_TYPE (type
);
252 if (TREE_CODE (basetype
) == TREE_VEC
)
255 basetype
= BINFO_TYPE (binfo
);
260 /* Check the destructor call syntax. */
261 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
263 /* We can get here if someone writes their destructor call like
264 `obj.NS::~T()'; this isn't really a scoped method call, so hand
266 if (TREE_CODE (basetype
) == NAMESPACE_DECL
)
267 return build_method_call (exp
, name
, parms
, NULL_TREE
, LOOKUP_NORMAL
);
269 if (! check_dtor_name (basetype
, name
))
270 error ("qualified type `%T' does not match destructor name `~%T'",
271 basetype
, TREE_OPERAND (name
, 0));
273 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
274 that explicit ~int is caught in the parser; this deals with typedefs
275 and template parms. */
276 if (! IS_AGGR_TYPE (basetype
))
278 if (TYPE_MAIN_VARIANT (type
) != TYPE_MAIN_VARIANT (basetype
))
279 error ("type of `%E' does not match destructor type `%T' (type was `%T')",
280 exp
, basetype
, type
);
282 return cp_convert (void_type_node
, exp
);
286 if (TREE_CODE (basetype
) == NAMESPACE_DECL
)
288 error ("`%D' is a namespace", basetype
);
289 return error_mark_node
;
291 if (! is_aggr_type (basetype
, 1))
292 return error_mark_node
;
294 if (! IS_AGGR_TYPE (type
))
296 error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
298 return error_mark_node
;
303 binfo
= lookup_base (type
, basetype
, ba_check
, NULL
);
304 if (binfo
== error_mark_node
)
305 return error_mark_node
;
307 error_not_base_type (basetype
, type
);
312 if (TREE_CODE (exp
) == INDIRECT_REF
)
314 decl
= build_base_path (PLUS_EXPR
,
315 build_unary_op (ADDR_EXPR
, exp
, 0),
317 decl
= build_indirect_ref (decl
, NULL
);
320 decl
= build_scoped_ref (exp
, basetype
);
322 /* Call to a destructor. */
323 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
325 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl
)))
326 return cp_convert (void_type_node
, exp
);
328 return build_delete (TREE_TYPE (decl
), decl
,
329 sfk_complete_destructor
,
330 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
,
334 /* Call to a method. */
335 return build_method_call (decl
, name
, parms
, binfo
,
336 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
);
338 return error_mark_node
;
341 /* We want the address of a function or method. We avoid creating a
342 pointer-to-member function. */
345 build_addr_func (function
)
348 tree type
= TREE_TYPE (function
);
350 /* We have to do these by hand to avoid real pointer to member
352 if (TREE_CODE (type
) == METHOD_TYPE
)
356 type
= build_pointer_type (type
);
358 if (mark_addressable (function
) == 0)
359 return error_mark_node
;
361 addr
= build1 (ADDR_EXPR
, type
, function
);
363 /* Address of a static or external variable or function counts
365 if (staticp (function
))
366 TREE_CONSTANT (addr
) = 1;
371 function
= default_conversion (function
);
376 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
377 POINTER_TYPE to those. Note, pointer to member function types
378 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
381 build_call (function
, parms
)
382 tree function
, parms
;
384 int is_constructor
= 0;
390 function
= build_addr_func (function
);
392 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
394 sorry ("unable to call pointer to member function here");
395 return error_mark_node
;
398 result_type
= TREE_TYPE (TREE_TYPE (TREE_TYPE (function
)));
400 if (TREE_CODE (function
) == ADDR_EXPR
401 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
402 decl
= TREE_OPERAND (function
, 0);
406 /* We check both the decl and the type; a function may be known not to
407 throw without being declared throw(). */
408 nothrow
= ((decl
&& TREE_NOTHROW (decl
))
409 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function
))));
411 if (decl
&& TREE_THIS_VOLATILE (decl
))
412 current_function_returns_abnormally
= 1;
414 if (decl
&& TREE_DEPRECATED (decl
))
415 warn_deprecated_use (decl
);
417 if (decl
&& DECL_CONSTRUCTOR_P (decl
))
420 if (decl
&& ! TREE_USED (decl
))
422 /* We invoke build_call directly for several library functions.
423 These may have been declared normally if we're building libgcc,
424 so we can't just check DECL_ARTIFICIAL. */
425 if (DECL_ARTIFICIAL (decl
)
426 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl
)), "__", 2))
432 /* Don't pass empty class objects by value. This is useful
433 for tags in STL, which are used to control overload resolution.
434 We don't need to handle other cases of copying empty classes. */
435 if (! decl
|| ! DECL_BUILT_IN (decl
))
436 for (tmp
= parms
; tmp
; tmp
= TREE_CHAIN (tmp
))
437 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp
)))
438 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp
))))
440 tree t
= build (EMPTY_CLASS_EXPR
, TREE_TYPE (TREE_VALUE (tmp
)));
441 TREE_VALUE (tmp
) = build (COMPOUND_EXPR
, TREE_TYPE (t
),
442 TREE_VALUE (tmp
), t
);
445 function
= build_nt (CALL_EXPR
, function
, parms
, NULL_TREE
);
446 TREE_HAS_CONSTRUCTOR (function
) = is_constructor
;
447 TREE_TYPE (function
) = result_type
;
448 TREE_SIDE_EFFECTS (function
) = 1;
449 TREE_NOTHROW (function
) = nothrow
;
454 /* Build something of the form ptr->method (args)
455 or object.method (args). This can also build
456 calls to constructors, and find friends.
458 Member functions always take their class variable
461 INSTANCE is a class instance.
463 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
465 PARMS help to figure out what that NAME really refers to.
467 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
468 down to the real instance type to use for access checking. We need this
469 information to get protected accesses correct. This parameter is used
470 by build_member_call.
472 FLAGS is the logical disjunction of zero or more LOOKUP_
473 flags. See cp-tree.h for more info.
475 If this is all OK, calls build_function_call with the resolved
478 This function must also handle being called to perform
479 initialization, promotion/coercion of arguments, and
480 instantiation of default parameters.
482 Note that NAME may refer to an instance variable name. If
483 `operator()()' is defined for the type of that field, then we return
486 #ifdef GATHER_STATISTICS
487 extern int n_build_method_call
;
491 build_method_call (instance
, name
, parms
, basetype_path
, flags
)
492 tree instance
, name
, parms
, basetype_path
;
495 tree basetype
, instance_ptr
;
497 #ifdef GATHER_STATISTICS
498 n_build_method_call
++;
501 if (instance
== error_mark_node
502 || name
== error_mark_node
503 || parms
== error_mark_node
504 || (instance
!= NULL_TREE
&& TREE_TYPE (instance
) == error_mark_node
))
505 return error_mark_node
;
507 if (processing_template_decl
)
509 /* We need to process template parm names here so that tsubst catches
510 them properly. Other type names can wait. */
511 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
513 tree type
= NULL_TREE
;
515 if (TREE_CODE (TREE_OPERAND (name
, 0)) == IDENTIFIER_NODE
)
516 type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 0);
517 else if (TREE_CODE (TREE_OPERAND (name
, 0)) == TYPE_DECL
)
518 type
= TREE_TYPE (TREE_OPERAND (name
, 0));
520 if (type
&& TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
521 name
= build_min_nt (BIT_NOT_EXPR
, type
);
524 return build_min_nt (METHOD_CALL_EXPR
, name
, instance
, parms
, NULL_TREE
);
527 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
530 error ("destructors take no parameters");
531 basetype
= TREE_TYPE (instance
);
532 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
533 basetype
= TREE_TYPE (basetype
);
535 if (! check_dtor_name (basetype
, name
))
537 ("destructor name `~%T' does not match type `%T' of expression",
538 TREE_OPERAND (name
, 0), basetype
);
540 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype
)))
541 return cp_convert (void_type_node
, instance
);
542 instance
= default_conversion (instance
);
543 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
544 return build_delete (build_pointer_type (basetype
),
545 instance_ptr
, sfk_complete_destructor
,
546 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0);
549 return build_new_method_call (instance
, name
, parms
, basetype_path
, flags
);
552 /* New overloading code. */
562 struct z_candidate
*next
;
565 #define IDENTITY_RANK 0
571 #define ELLIPSIS_RANK 6
574 #define ICS_RANK(NODE) \
575 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
576 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
577 : ICS_USER_FLAG (NODE) ? USER_RANK \
578 : ICS_STD_RANK (NODE))
580 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
582 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
583 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
584 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
585 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
587 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
588 should be created to hold the result of the conversion. */
589 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
591 #define USER_CONV_CAND(NODE) \
592 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
593 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
601 A null pointer constant is an integral constant expression
602 (_expr.const_) rvalue of integer type that evaluates to zero. */
604 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t
)) && integer_zerop (t
)))
610 /* Returns non-zero if PARMLIST consists of only default parms and/or
614 sufficient_parms_p (parmlist
)
617 for (; parmlist
&& parmlist
!= void_list_node
;
618 parmlist
= TREE_CHAIN (parmlist
))
619 if (!TREE_PURPOSE (parmlist
))
625 build_conv (code
, type
, from
)
630 int rank
= ICS_STD_RANK (from
);
632 /* We can't use buildl1 here because CODE could be USER_CONV, which
633 takes two arguments. In that case, the caller is responsible for
634 filling in the second argument. */
635 t
= make_node (code
);
636 TREE_TYPE (t
) = type
;
637 TREE_OPERAND (t
, 0) = from
;
650 if (rank
< EXACT_RANK
)
656 ICS_STD_RANK (t
) = rank
;
657 ICS_USER_FLAG (t
) = ICS_USER_FLAG (from
);
658 ICS_BAD_FLAG (t
) = ICS_BAD_FLAG (from
);
662 /* If T is a REFERENCE_TYPE return the type to which T refers.
663 Otherwise, return T itself. */
669 if (TREE_CODE (t
) == REFERENCE_TYPE
)
678 if (TREE_CODE (t
) == ARRAY_TYPE
)
680 return TYPE_MAIN_VARIANT (t
);
683 /* Returns the standard conversion path (see [conv]) from type FROM to type
684 TO, if any. For proper handling of null pointer constants, you must
685 also pass the expression EXPR to convert from. */
688 standard_conversion (to
, from
, expr
)
691 enum tree_code fcode
, tcode
;
695 if (TREE_CODE (to
) == REFERENCE_TYPE
)
697 if (TREE_CODE (from
) == REFERENCE_TYPE
)
700 from
= TREE_TYPE (from
);
702 to
= strip_top_quals (to
);
703 from
= strip_top_quals (from
);
705 if ((TYPE_PTRFN_P (to
) || TYPE_PTRMEMFUNC_P (to
))
706 && expr
&& type_unknown_p (expr
))
708 expr
= instantiate_type (to
, expr
, tf_none
);
709 if (expr
== error_mark_node
)
711 from
= TREE_TYPE (expr
);
714 fcode
= TREE_CODE (from
);
715 tcode
= TREE_CODE (to
);
717 conv
= build1 (IDENTITY_CONV
, from
, expr
);
719 if (fcode
== FUNCTION_TYPE
)
721 from
= build_pointer_type (from
);
722 fcode
= TREE_CODE (from
);
723 conv
= build_conv (LVALUE_CONV
, from
, conv
);
725 else if (fcode
== ARRAY_TYPE
)
727 from
= build_pointer_type (TREE_TYPE (from
));
728 fcode
= TREE_CODE (from
);
729 conv
= build_conv (LVALUE_CONV
, from
, conv
);
731 else if (fromref
|| (expr
&& lvalue_p (expr
)))
732 conv
= build_conv (RVALUE_CONV
, from
, conv
);
734 /* Allow conversion between `__complex__' data types */
735 if (tcode
== COMPLEX_TYPE
&& fcode
== COMPLEX_TYPE
)
737 /* The standard conversion sequence to convert FROM to TO is
738 the standard conversion sequence to perform componentwise
740 tree part_conv
= standard_conversion
741 (TREE_TYPE (to
), TREE_TYPE (from
), NULL_TREE
);
745 conv
= build_conv (TREE_CODE (part_conv
), to
, conv
);
746 ICS_STD_RANK (conv
) = ICS_STD_RANK (part_conv
);
754 if (same_type_p (from
, to
))
757 if ((tcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (to
))
758 && expr
&& null_ptr_cst_p (expr
))
760 conv
= build_conv (STD_CONV
, to
, conv
);
762 else if ((tcode
== INTEGER_TYPE
&& fcode
== POINTER_TYPE
)
763 || (tcode
== POINTER_TYPE
&& fcode
== INTEGER_TYPE
))
765 /* For backwards brain damage compatibility, allow interconversion of
766 pointers and integers with a pedwarn. */
767 conv
= build_conv (STD_CONV
, to
, conv
);
768 ICS_BAD_FLAG (conv
) = 1;
770 else if (tcode
== ENUMERAL_TYPE
&& fcode
== INTEGER_TYPE
771 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
773 /* For backwards brain damage compatibility, allow interconversion of
774 enums and integers with a pedwarn. */
775 conv
= build_conv (STD_CONV
, to
, conv
);
776 ICS_BAD_FLAG (conv
) = 1;
778 else if (tcode
== POINTER_TYPE
&& fcode
== POINTER_TYPE
)
780 enum tree_code ufcode
= TREE_CODE (TREE_TYPE (from
));
781 enum tree_code utcode
= TREE_CODE (TREE_TYPE (to
));
783 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from
),
786 else if (utcode
== VOID_TYPE
&& ufcode
!= OFFSET_TYPE
787 && ufcode
!= FUNCTION_TYPE
)
789 from
= build_pointer_type
790 (cp_build_qualified_type (void_type_node
,
791 cp_type_quals (TREE_TYPE (from
))));
792 conv
= build_conv (PTR_CONV
, from
, conv
);
794 else if (ufcode
== OFFSET_TYPE
&& utcode
== OFFSET_TYPE
)
796 tree fbase
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from
));
797 tree tbase
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to
));
799 if (DERIVED_FROM_P (fbase
, tbase
)
800 && (same_type_ignoring_top_level_qualifiers_p
801 (TREE_TYPE (TREE_TYPE (from
)),
802 TREE_TYPE (TREE_TYPE (to
)))))
804 from
= build_offset_type (tbase
, TREE_TYPE (TREE_TYPE (from
)));
805 from
= build_pointer_type (from
);
806 conv
= build_conv (PMEM_CONV
, from
, conv
);
809 else if (IS_AGGR_TYPE (TREE_TYPE (from
))
810 && IS_AGGR_TYPE (TREE_TYPE (to
)))
812 if (DERIVED_FROM_P (TREE_TYPE (to
), TREE_TYPE (from
)))
815 cp_build_qualified_type (TREE_TYPE (to
),
816 cp_type_quals (TREE_TYPE (from
)));
817 from
= build_pointer_type (from
);
818 conv
= build_conv (PTR_CONV
, from
, conv
);
822 if (same_type_p (from
, to
))
824 else if (comp_ptr_ttypes (TREE_TYPE (to
), TREE_TYPE (from
)))
825 conv
= build_conv (QUAL_CONV
, to
, conv
);
826 else if (expr
&& string_conv_p (to
, expr
, 0))
827 /* converting from string constant to char *. */
828 conv
= build_conv (QUAL_CONV
, to
, conv
);
829 else if (ptr_reasonably_similar (TREE_TYPE (to
), TREE_TYPE (from
)))
831 conv
= build_conv (PTR_CONV
, to
, conv
);
832 ICS_BAD_FLAG (conv
) = 1;
839 else if (TYPE_PTRMEMFUNC_P (to
) && TYPE_PTRMEMFUNC_P (from
))
841 tree fromfn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from
));
842 tree tofn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to
));
843 tree fbase
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn
)));
844 tree tbase
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn
)));
846 if (!DERIVED_FROM_P (fbase
, tbase
)
847 || !same_type_p (TREE_TYPE (fromfn
), TREE_TYPE (tofn
))
848 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn
)),
849 TREE_CHAIN (TYPE_ARG_TYPES (tofn
)))
850 || cp_type_quals (fbase
) != cp_type_quals (tbase
))
853 from
= cp_build_qualified_type (tbase
, cp_type_quals (fbase
));
854 from
= build_cplus_method_type (from
, TREE_TYPE (fromfn
),
855 TREE_CHAIN (TYPE_ARG_TYPES (fromfn
)));
856 from
= build_ptrmemfunc_type (build_pointer_type (from
));
857 conv
= build_conv (PMEM_CONV
, from
, conv
);
859 else if (tcode
== BOOLEAN_TYPE
)
861 if (! (INTEGRAL_CODE_P (fcode
) || fcode
== REAL_TYPE
862 || fcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (from
)))
865 conv
= build_conv (STD_CONV
, to
, conv
);
866 if (fcode
== POINTER_TYPE
867 || (TYPE_PTRMEMFUNC_P (from
) && ICS_STD_RANK (conv
) < PBOOL_RANK
))
868 ICS_STD_RANK (conv
) = PBOOL_RANK
;
870 /* We don't check for ENUMERAL_TYPE here because there are no standard
871 conversions to enum type. */
872 else if (tcode
== INTEGER_TYPE
|| tcode
== BOOLEAN_TYPE
873 || tcode
== REAL_TYPE
)
875 if (! (INTEGRAL_CODE_P (fcode
) || fcode
== REAL_TYPE
))
877 conv
= build_conv (STD_CONV
, to
, conv
);
879 /* Give this a better rank if it's a promotion. */
880 if (to
== type_promotes_to (from
)
881 && ICS_STD_RANK (TREE_OPERAND (conv
, 0)) <= PROMO_RANK
)
882 ICS_STD_RANK (conv
) = PROMO_RANK
;
884 else if (IS_AGGR_TYPE (to
) && IS_AGGR_TYPE (from
)
885 && is_properly_derived_from (from
, to
))
887 if (TREE_CODE (conv
) == RVALUE_CONV
)
888 conv
= TREE_OPERAND (conv
, 0);
889 conv
= build_conv (BASE_CONV
, to
, conv
);
890 /* The derived-to-base conversion indicates the initialization
891 of a parameter with base type from an object of a derived
892 type. A temporary object is created to hold the result of
894 NEED_TEMPORARY_P (conv
) = 1;
902 /* Returns non-zero if T1 is reference-related to T2. */
905 reference_related_p (t1
, t2
)
909 t1
= TYPE_MAIN_VARIANT (t1
);
910 t2
= TYPE_MAIN_VARIANT (t2
);
914 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
915 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
917 return (same_type_p (t1
, t2
)
918 || (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
919 && DERIVED_FROM_P (t1
, t2
)));
922 /* Returns non-zero if T1 is reference-compatible with T2. */
925 reference_compatible_p (t1
, t2
)
931 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
932 reference-related to T2 and cv1 is the same cv-qualification as,
933 or greater cv-qualification than, cv2. */
934 return (reference_related_p (t1
, t2
)
935 && at_least_as_qualified_p (t1
, t2
));
938 /* Determine whether or not the EXPR (of class type S) can be
939 converted to T as in [over.match.ref]. */
942 convert_class_to_reference (t
, s
, expr
)
950 struct z_candidate
*candidates
;
951 struct z_candidate
*cand
;
955 Assuming that "cv1 T" is the underlying type of the reference
956 being initialized, and "cv S" is the type of the initializer
957 expression, with S a class type, the candidate functions are
960 --The conversion functions of S and its base classes are
961 considered. Those that are not hidden within S and yield type
962 "reference to cv2 T2", where "cv1 T" is reference-compatible
963 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
965 The argument list has one argument, which is the initializer
970 /* Conceptually, we should take the address of EXPR and put it in
971 the argument list. Unfortunately, however, that can result in
972 error messages, which we should not issue now because we are just
973 trying to find a conversion operator. Therefore, we use NULL,
974 cast to the appropriate type. */
975 arglist
= build_int_2 (0, 0);
976 TREE_TYPE (arglist
) = build_pointer_type (s
);
977 arglist
= build_tree_list (NULL_TREE
, arglist
);
979 for (conversions
= lookup_conversions (s
);
981 conversions
= TREE_CHAIN (conversions
))
983 tree fns
= TREE_VALUE (conversions
);
985 for (; fns
; fns
= OVL_NEXT (fns
))
987 tree f
= OVL_CURRENT (fns
);
988 tree t2
= TREE_TYPE (TREE_TYPE (f
));
989 struct z_candidate
*old_candidates
= candidates
;
991 /* If this is a template function, try to get an exact
993 if (TREE_CODE (f
) == TEMPLATE_DECL
)
996 = add_template_candidate (candidates
,
1000 build_reference_type (t
),
1004 if (candidates
!= old_candidates
)
1006 /* Now, see if the conversion function really returns
1007 an lvalue of the appropriate type. From the
1008 point of view of unification, simply returning an
1009 rvalue of the right type is good enough. */
1011 t2
= TREE_TYPE (TREE_TYPE (f
));
1012 if (TREE_CODE (t2
) != REFERENCE_TYPE
1013 || !reference_compatible_p (t
, TREE_TYPE (t2
)))
1014 candidates
= candidates
->next
;
1017 else if (TREE_CODE (t2
) == REFERENCE_TYPE
1018 && reference_compatible_p (t
, TREE_TYPE (t2
)))
1020 = add_function_candidate (candidates
, f
, s
, arglist
,
1023 if (candidates
!= old_candidates
)
1024 candidates
->basetype_path
= TYPE_BINFO (s
);
1028 /* If none of the conversion functions worked out, let our caller
1030 if (!any_viable (candidates
))
1033 candidates
= splice_viable (candidates
);
1034 cand
= tourney (candidates
);
1038 conv
= build1 (IDENTITY_CONV
, s
, expr
);
1039 conv
= build_conv (USER_CONV
, TREE_TYPE (TREE_TYPE (cand
->fn
)),
1041 TREE_OPERAND (conv
, 1) = build_ptr_wrapper (cand
);
1042 ICS_USER_FLAG (conv
) = 1;
1043 if (cand
->viable
== -1)
1044 ICS_BAD_FLAG (conv
) = 1;
1045 cand
->second_conv
= conv
;
1050 /* A reference of the indicated TYPE is being bound directly to the
1051 expression represented by the implicit conversion sequence CONV.
1052 Return a conversion sequence for this binding. */
1055 direct_reference_binding (type
, conv
)
1059 tree t
= TREE_TYPE (type
);
1063 When a parameter of reference type binds directly
1064 (_dcl.init.ref_) to an argument expression, the implicit
1065 conversion sequence is the identity conversion, unless the
1066 argument expression has a type that is a derived class of the
1067 parameter type, in which case the implicit conversion sequence is
1068 a derived-to-base Conversion.
1070 If the parameter binds directly to the result of applying a
1071 conversion function to the argument expression, the implicit
1072 conversion sequence is a user-defined conversion sequence
1073 (_over.ics.user_), with the second standard conversion sequence
1074 either an identity conversion or, if the conversion function
1075 returns an entity of a type that is a derived class of the
1076 parameter type, a derived-to-base conversion. */
1077 if (!same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (conv
)))
1079 /* Represent the derived-to-base conversion. */
1080 conv
= build_conv (BASE_CONV
, t
, conv
);
1081 /* We will actually be binding to the base-class subobject in
1082 the derived class, so we mark this conversion appropriately.
1083 That way, convert_like knows not to generate a temporary. */
1084 NEED_TEMPORARY_P (conv
) = 0;
1086 return build_conv (REF_BIND
, type
, conv
);
1089 /* Returns the conversion path from type FROM to reference type TO for
1090 purposes of reference binding. For lvalue binding, either pass a
1091 reference type to FROM or an lvalue expression to EXPR. If the
1092 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1093 the conversion returned. */
1096 reference_binding (rto
, rfrom
, expr
, flags
)
1097 tree rto
, rfrom
, expr
;
1100 tree conv
= NULL_TREE
;
1101 tree to
= TREE_TYPE (rto
);
1105 cp_lvalue_kind lvalue_p
= clk_none
;
1107 if (TREE_CODE (to
) == FUNCTION_TYPE
&& expr
&& type_unknown_p (expr
))
1109 expr
= instantiate_type (to
, expr
, tf_none
);
1110 if (expr
== error_mark_node
)
1112 from
= TREE_TYPE (expr
);
1115 if (TREE_CODE (from
) == REFERENCE_TYPE
)
1117 /* Anything with reference type is an lvalue. */
1118 lvalue_p
= clk_ordinary
;
1119 from
= TREE_TYPE (from
);
1122 lvalue_p
= real_lvalue_p (expr
);
1124 /* Figure out whether or not the types are reference-related and
1125 reference compatible. We have do do this after stripping
1126 references from FROM. */
1127 related_p
= reference_related_p (to
, from
);
1128 compatible_p
= reference_compatible_p (to
, from
);
1130 if (lvalue_p
&& compatible_p
)
1134 If the initializer expression
1136 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1137 is reference-compatible with "cv2 T2,"
1139 the reference is bound directly to the initializer exprssion
1141 conv
= build1 (IDENTITY_CONV
, from
, expr
);
1142 conv
= direct_reference_binding (rto
, conv
);
1143 if ((lvalue_p
& clk_bitfield
) != 0
1144 && CP_TYPE_CONST_NON_VOLATILE_P (to
))
1145 /* For the purposes of overload resolution, we ignore the fact
1146 this expression is a bitfield. (In particular,
1147 [over.ics.ref] says specifically that a function with a
1148 non-const reference parameter is viable even if the
1149 argument is a bitfield.)
1151 However, when we actually call the function we must create
1152 a temporary to which to bind the reference. If the
1153 reference is volatile, or isn't const, then we cannot make
1154 a temporary, so we just issue an error when the conversion
1156 NEED_TEMPORARY_P (conv
) = 1;
1159 else if (CLASS_TYPE_P (from
) && !(flags
& LOOKUP_NO_CONVERSION
))
1163 If the initializer exprsesion
1165 -- has a class type (i.e., T2 is a class type) can be
1166 implicitly converted to an lvalue of type "cv3 T3," where
1167 "cv1 T1" is reference-compatible with "cv3 T3". (this
1168 conversion is selected by enumerating the applicable
1169 conversion functions (_over.match.ref_) and choosing the
1170 best one through overload resolution. (_over.match_).
1172 the reference is bound to the lvalue result of the conversion
1173 in the second case. */
1174 conv
= convert_class_to_reference (to
, from
, expr
);
1176 return direct_reference_binding (rto
, conv
);
1179 /* From this point on, we conceptually need temporaries, even if we
1180 elide them. Only the cases above are "direct bindings". */
1181 if (flags
& LOOKUP_NO_TEMP_BIND
)
1186 When a parameter of reference type is not bound directly to an
1187 argument expression, the conversion sequence is the one required
1188 to convert the argument expression to the underlying type of the
1189 reference according to _over.best.ics_. Conceptually, this
1190 conversion sequence corresponds to copy-initializing a temporary
1191 of the underlying type with the argument expression. Any
1192 difference in top-level cv-qualification is subsumed by the
1193 initialization itself and does not constitute a conversion. */
1197 Otherwise, the reference shall be to a non-volatile const type. */
1198 if (!CP_TYPE_CONST_NON_VOLATILE_P (to
))
1203 If the initializer expression is an rvalue, with T2 a class type,
1204 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1205 is bound in one of the following ways:
1207 -- The reference is bound to the object represented by the rvalue
1208 or to a sub-object within that object.
1210 In this case, the implicit conversion sequence is supposed to be
1211 same as we would obtain by generating a temporary. Fortunately,
1212 if the types are reference compatible, then this is either an
1213 identity conversion or the derived-to-base conversion, just as
1214 for direct binding. */
1215 if (CLASS_TYPE_P (from
) && compatible_p
)
1217 conv
= build1 (IDENTITY_CONV
, from
, expr
);
1218 return direct_reference_binding (rto
, conv
);
1223 Otherwise, a temporary of type "cv1 T1" is created and
1224 initialized from the initializer expression using the rules for a
1225 non-reference copy initialization. If T1 is reference-related to
1226 T2, cv1 must be the same cv-qualification as, or greater
1227 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1228 if (related_p
&& !at_least_as_qualified_p (to
, from
))
1231 conv
= implicit_conversion (to
, from
, expr
, flags
);
1235 conv
= build_conv (REF_BIND
, rto
, conv
);
1236 /* This reference binding, unlike those above, requires the
1237 creation of a temporary. */
1238 NEED_TEMPORARY_P (conv
) = 1;
1243 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1244 to type TO. The optional expression EXPR may affect the conversion.
1245 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1249 implicit_conversion (to
, from
, expr
, flags
)
1250 tree to
, from
, expr
;
1254 struct z_candidate
*cand
;
1256 /* Resolve expressions like `A::p' that we thought might become
1257 pointers-to-members. */
1258 if (expr
&& TREE_CODE (expr
) == OFFSET_REF
)
1260 expr
= resolve_offset_ref (expr
);
1261 from
= TREE_TYPE (expr
);
1264 if (from
== error_mark_node
|| to
== error_mark_node
1265 || expr
== error_mark_node
)
1268 /* Make sure both the FROM and TO types are complete so that
1269 user-defined conversions are available. */
1270 complete_type (from
);
1273 if (TREE_CODE (to
) == REFERENCE_TYPE
)
1274 conv
= reference_binding (to
, from
, expr
, flags
);
1276 conv
= standard_conversion (to
, from
, expr
);
1280 else if (expr
!= NULL_TREE
1281 && (IS_AGGR_TYPE (from
)
1282 || IS_AGGR_TYPE (to
))
1283 && (flags
& LOOKUP_NO_CONVERSION
) == 0)
1285 cand
= build_user_type_conversion_1
1286 (to
, expr
, LOOKUP_ONLYCONVERTING
);
1288 conv
= cand
->second_conv
;
1290 /* We used to try to bind a reference to a temporary here, but that
1291 is now handled by the recursive call to this function at the end
1292 of reference_binding. */
1298 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1301 static struct z_candidate
*
1302 add_candidate (candidates
, fn
, convs
, viable
)
1303 struct z_candidate
*candidates
;
1307 struct z_candidate
*cand
1308 = (struct z_candidate
*) ggc_alloc_cleared (sizeof (struct z_candidate
));
1311 cand
->convs
= convs
;
1312 cand
->viable
= viable
;
1313 cand
->next
= candidates
;
1318 /* Create an overload candidate for the function or method FN called with
1319 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1320 to implicit_conversion.
1322 CTYPE, if non-NULL, is the type we want to pretend this function
1323 comes from for purposes of overload resolution. */
1325 static struct z_candidate
*
1326 add_function_candidate (candidates
, fn
, ctype
, arglist
, flags
)
1327 struct z_candidate
*candidates
;
1328 tree fn
, ctype
, arglist
;
1331 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1334 tree parmnode
, argnode
;
1337 /* The `this', `in_chrg' and VTT arguments to constructors are not
1338 considered in overload resolution. */
1339 if (DECL_CONSTRUCTOR_P (fn
))
1341 parmlist
= skip_artificial_parms_for (fn
, parmlist
);
1342 arglist
= skip_artificial_parms_for (fn
, arglist
);
1345 len
= list_length (arglist
);
1346 convs
= make_tree_vec (len
);
1348 /* 13.3.2 - Viable functions [over.match.viable]
1349 First, to be a viable function, a candidate function shall have enough
1350 parameters to agree in number with the arguments in the list.
1352 We need to check this first; otherwise, checking the ICSes might cause
1353 us to produce an ill-formed template instantiation. */
1355 parmnode
= parmlist
;
1356 for (i
= 0; i
< len
; ++i
)
1358 if (parmnode
== NULL_TREE
|| parmnode
== void_list_node
)
1360 parmnode
= TREE_CHAIN (parmnode
);
1363 if (i
< len
&& parmnode
)
1366 /* Make sure there are default args for the rest of the parms. */
1367 else if (!sufficient_parms_p (parmnode
))
1373 /* Second, for F to be a viable function, there shall exist for each
1374 argument an implicit conversion sequence that converts that argument
1375 to the corresponding parameter of F. */
1377 parmnode
= parmlist
;
1380 for (i
= 0; i
< len
; ++i
)
1382 tree arg
= TREE_VALUE (argnode
);
1383 tree argtype
= lvalue_type (arg
);
1387 if (parmnode
== void_list_node
)
1390 is_this
= (i
== 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1391 && ! DECL_CONSTRUCTOR_P (fn
));
1395 tree parmtype
= TREE_VALUE (parmnode
);
1397 /* The type of the implicit object parameter ('this') for
1398 overload resolution is not always the same as for the
1399 function itself; conversion functions are considered to
1400 be members of the class being converted, and functions
1401 introduced by a using-declaration are considered to be
1402 members of the class that uses them.
1404 Since build_over_call ignores the ICS for the `this'
1405 parameter, we can just change the parm type. */
1406 if (ctype
&& is_this
)
1409 = build_qualified_type (ctype
,
1410 TYPE_QUALS (TREE_TYPE (parmtype
)));
1411 parmtype
= build_pointer_type (parmtype
);
1414 t
= implicit_conversion (parmtype
, argtype
, arg
, flags
);
1418 t
= build1 (IDENTITY_CONV
, argtype
, arg
);
1419 ICS_ELLIPSIS_FLAG (t
) = 1;
1423 ICS_THIS_FLAG (t
) = 1;
1425 TREE_VEC_ELT (convs
, i
) = t
;
1432 if (ICS_BAD_FLAG (t
))
1436 parmnode
= TREE_CHAIN (parmnode
);
1437 argnode
= TREE_CHAIN (argnode
);
1441 return add_candidate (candidates
, fn
, convs
, viable
);
1444 /* Create an overload candidate for the conversion function FN which will
1445 be invoked for expression OBJ, producing a pointer-to-function which
1446 will in turn be called with the argument list ARGLIST, and add it to
1447 CANDIDATES. FLAGS is passed on to implicit_conversion.
1449 Actually, we don't really care about FN; we care about the type it
1450 converts to. There may be multiple conversion functions that will
1451 convert to that type, and we rely on build_user_type_conversion_1 to
1452 choose the best one; so when we create our candidate, we record the type
1453 instead of the function. */
1455 static struct z_candidate
*
1456 add_conv_candidate (candidates
, fn
, obj
, arglist
)
1457 struct z_candidate
*candidates
;
1458 tree fn
, obj
, arglist
;
1460 tree totype
= TREE_TYPE (TREE_TYPE (fn
));
1461 int i
, len
, viable
, flags
;
1462 tree parmlist
, convs
, parmnode
, argnode
;
1464 for (parmlist
= totype
; TREE_CODE (parmlist
) != FUNCTION_TYPE
; )
1465 parmlist
= TREE_TYPE (parmlist
);
1466 parmlist
= TYPE_ARG_TYPES (parmlist
);
1468 len
= list_length (arglist
) + 1;
1469 convs
= make_tree_vec (len
);
1470 parmnode
= parmlist
;
1473 flags
= LOOKUP_NORMAL
;
1475 /* Don't bother looking up the same type twice. */
1476 if (candidates
&& candidates
->fn
== totype
)
1479 for (i
= 0; i
< len
; ++i
)
1481 tree arg
= i
== 0 ? obj
: TREE_VALUE (argnode
);
1482 tree argtype
= lvalue_type (arg
);
1486 t
= implicit_conversion (totype
, argtype
, arg
, flags
);
1487 else if (parmnode
== void_list_node
)
1490 t
= implicit_conversion (TREE_VALUE (parmnode
), argtype
, arg
, flags
);
1493 t
= build1 (IDENTITY_CONV
, argtype
, arg
);
1494 ICS_ELLIPSIS_FLAG (t
) = 1;
1497 TREE_VEC_ELT (convs
, i
) = t
;
1501 if (ICS_BAD_FLAG (t
))
1508 parmnode
= TREE_CHAIN (parmnode
);
1509 argnode
= TREE_CHAIN (argnode
);
1515 if (!sufficient_parms_p (parmnode
))
1518 return add_candidate (candidates
, totype
, convs
, viable
);
1521 static struct z_candidate
*
1522 build_builtin_candidate (candidates
, fnname
, type1
, type2
,
1523 args
, argtypes
, flags
)
1524 struct z_candidate
*candidates
;
1525 tree fnname
, type1
, type2
, *args
, *argtypes
;
1536 convs
= make_tree_vec (args
[2] ? 3 : (args
[1] ? 2 : 1));
1538 for (i
= 0; i
< 2; ++i
)
1543 t
= implicit_conversion (types
[i
], argtypes
[i
], args
[i
], flags
);
1547 /* We need something for printing the candidate. */
1548 t
= build1 (IDENTITY_CONV
, types
[i
], NULL_TREE
);
1550 else if (ICS_BAD_FLAG (t
))
1552 TREE_VEC_ELT (convs
, i
) = t
;
1555 /* For COND_EXPR we rearranged the arguments; undo that now. */
1558 TREE_VEC_ELT (convs
, 2) = TREE_VEC_ELT (convs
, 1);
1559 TREE_VEC_ELT (convs
, 1) = TREE_VEC_ELT (convs
, 0);
1560 t
= implicit_conversion (boolean_type_node
, argtypes
[2], args
[2], flags
);
1562 TREE_VEC_ELT (convs
, 0) = t
;
1567 return add_candidate (candidates
, fnname
, convs
, viable
);
1574 return COMPLETE_TYPE_P (complete_type (t
));
1577 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1580 promoted_arithmetic_type_p (type
)
1585 In this section, the term promoted integral type is used to refer
1586 to those integral types which are preserved by integral promotion
1587 (including e.g. int and long but excluding e.g. char).
1588 Similarly, the term promoted arithmetic type refers to promoted
1589 integral types plus floating types. */
1590 return ((INTEGRAL_TYPE_P (type
)
1591 && same_type_p (type_promotes_to (type
), type
))
1592 || TREE_CODE (type
) == REAL_TYPE
);
1595 /* Create any builtin operator overload candidates for the operator in
1596 question given the converted operand types TYPE1 and TYPE2. The other
1597 args are passed through from add_builtin_candidates to
1598 build_builtin_candidate.
1600 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1601 If CODE is requires candidates operands of the same type of the kind
1602 of which TYPE1 and TYPE2 are, we add both candidates
1603 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1605 static struct z_candidate
*
1606 add_builtin_candidate (candidates
, code
, code2
, fnname
, type1
, type2
,
1607 args
, argtypes
, flags
)
1608 struct z_candidate
*candidates
;
1609 enum tree_code code
, code2
;
1610 tree fnname
, type1
, type2
, *args
, *argtypes
;
1615 case POSTINCREMENT_EXPR
:
1616 case POSTDECREMENT_EXPR
:
1617 args
[1] = integer_zero_node
;
1618 type2
= integer_type_node
;
1627 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1628 and VQ is either volatile or empty, there exist candidate operator
1629 functions of the form
1630 VQ T& operator++(VQ T&);
1631 T operator++(VQ T&, int);
1632 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1633 type other than bool, and VQ is either volatile or empty, there exist
1634 candidate operator functions of the form
1635 VQ T& operator--(VQ T&);
1636 T operator--(VQ T&, int);
1637 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1638 complete object type, and VQ is either volatile or empty, there exist
1639 candidate operator functions of the form
1640 T*VQ& operator++(T*VQ&);
1641 T*VQ& operator--(T*VQ&);
1642 T* operator++(T*VQ&, int);
1643 T* operator--(T*VQ&, int); */
1645 case POSTDECREMENT_EXPR
:
1646 case PREDECREMENT_EXPR
:
1647 if (TREE_CODE (type1
) == BOOLEAN_TYPE
)
1649 case POSTINCREMENT_EXPR
:
1650 case PREINCREMENT_EXPR
:
1651 if (ARITHMETIC_TYPE_P (type1
) || TYPE_PTROB_P (type1
))
1653 type1
= build_reference_type (type1
);
1658 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1659 exist candidate operator functions of the form
1663 8 For every function type T, there exist candidate operator functions of
1665 T& operator*(T*); */
1668 if (TREE_CODE (type1
) == POINTER_TYPE
1669 && (TYPE_PTROB_P (type1
)
1670 || TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
))
1674 /* 9 For every type T, there exist candidate operator functions of the form
1677 10For every promoted arithmetic type T, there exist candidate operator
1678 functions of the form
1682 case CONVERT_EXPR
: /* unary + */
1683 if (TREE_CODE (type1
) == POINTER_TYPE
1684 && TREE_CODE (TREE_TYPE (type1
)) != OFFSET_TYPE
)
1687 if (ARITHMETIC_TYPE_P (type1
))
1691 /* 11For every promoted integral type T, there exist candidate operator
1692 functions of the form
1696 if (INTEGRAL_TYPE_P (type1
))
1700 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1701 is the same type as C2 or is a derived class of C2, T is a complete
1702 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1703 there exist candidate operator functions of the form
1704 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1705 where CV12 is the union of CV1 and CV2. */
1708 if (TREE_CODE (type1
) == POINTER_TYPE
1709 && (TYPE_PTRMEMFUNC_P (type2
) || TYPE_PTRMEM_P (type2
)))
1711 tree c1
= TREE_TYPE (type1
);
1712 tree c2
= (TYPE_PTRMEMFUNC_P (type2
)
1713 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2
)))
1714 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2
)));
1716 if (IS_AGGR_TYPE (c1
) && DERIVED_FROM_P (c2
, c1
)
1717 && (TYPE_PTRMEMFUNC_P (type2
)
1718 || is_complete (TREE_TYPE (TREE_TYPE (type2
)))))
1723 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1724 didate operator functions of the form
1729 bool operator<(L, R);
1730 bool operator>(L, R);
1731 bool operator<=(L, R);
1732 bool operator>=(L, R);
1733 bool operator==(L, R);
1734 bool operator!=(L, R);
1735 where LR is the result of the usual arithmetic conversions between
1738 14For every pair of types T and I, where T is a cv-qualified or cv-
1739 unqualified complete object type and I is a promoted integral type,
1740 there exist candidate operator functions of the form
1741 T* operator+(T*, I);
1742 T& operator[](T*, I);
1743 T* operator-(T*, I);
1744 T* operator+(I, T*);
1745 T& operator[](I, T*);
1747 15For every T, where T is a pointer to complete object type, there exist
1748 candidate operator functions of the form112)
1749 ptrdiff_t operator-(T, T);
1751 16For every pointer or enumeration type T, there exist candidate operator
1752 functions of the form
1753 bool operator<(T, T);
1754 bool operator>(T, T);
1755 bool operator<=(T, T);
1756 bool operator>=(T, T);
1757 bool operator==(T, T);
1758 bool operator!=(T, T);
1760 17For every pointer to member type T, there exist candidate operator
1761 functions of the form
1762 bool operator==(T, T);
1763 bool operator!=(T, T); */
1766 if (TYPE_PTROB_P (type1
) && TYPE_PTROB_P (type2
))
1768 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1770 type2
= ptrdiff_type_node
;
1774 case TRUNC_DIV_EXPR
:
1775 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1781 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
1782 || (TYPE_PTRMEM_P (type1
) && TYPE_PTRMEM_P (type2
)))
1784 if ((TYPE_PTRMEMFUNC_P (type1
) || TYPE_PTRMEM_P (type1
))
1785 && null_ptr_cst_p (args
[1]))
1790 if ((TYPE_PTRMEMFUNC_P (type2
) || TYPE_PTRMEM_P (type2
))
1791 && null_ptr_cst_p (args
[0]))
1803 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1805 if (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
1807 if (TREE_CODE (type1
) == ENUMERAL_TYPE
&& TREE_CODE (type2
) == ENUMERAL_TYPE
)
1809 if (TYPE_PTR_P (type1
) && null_ptr_cst_p (args
[1]))
1814 if (null_ptr_cst_p (args
[0]) && TYPE_PTR_P (type2
))
1822 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1825 if (INTEGRAL_TYPE_P (type1
) && TYPE_PTROB_P (type2
))
1827 type1
= ptrdiff_type_node
;
1830 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1832 type2
= ptrdiff_type_node
;
1837 /* 18For every pair of promoted integral types L and R, there exist candi-
1838 date operator functions of the form
1845 where LR is the result of the usual arithmetic conversions between
1848 case TRUNC_MOD_EXPR
:
1854 if (INTEGRAL_TYPE_P (type1
) && INTEGRAL_TYPE_P (type2
))
1858 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1859 type, VQ is either volatile or empty, and R is a promoted arithmetic
1860 type, there exist candidate operator functions of the form
1861 VQ L& operator=(VQ L&, R);
1862 VQ L& operator*=(VQ L&, R);
1863 VQ L& operator/=(VQ L&, R);
1864 VQ L& operator+=(VQ L&, R);
1865 VQ L& operator-=(VQ L&, R);
1867 20For every pair T, VQ), where T is any type and VQ is either volatile
1868 or empty, there exist candidate operator functions of the form
1869 T*VQ& operator=(T*VQ&, T*);
1871 21For every pair T, VQ), where T is a pointer to member type and VQ is
1872 either volatile or empty, there exist candidate operator functions of
1874 VQ T& operator=(VQ T&, T);
1876 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1877 unqualified complete object type, VQ is either volatile or empty, and
1878 I is a promoted integral type, there exist candidate operator func-
1880 T*VQ& operator+=(T*VQ&, I);
1881 T*VQ& operator-=(T*VQ&, I);
1883 23For every triple L, VQ, R), where L is an integral or enumeration
1884 type, VQ is either volatile or empty, and R is a promoted integral
1885 type, there exist candidate operator functions of the form
1887 VQ L& operator%=(VQ L&, R);
1888 VQ L& operator<<=(VQ L&, R);
1889 VQ L& operator>>=(VQ L&, R);
1890 VQ L& operator&=(VQ L&, R);
1891 VQ L& operator^=(VQ L&, R);
1892 VQ L& operator|=(VQ L&, R); */
1899 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1901 type2
= ptrdiff_type_node
;
1905 case TRUNC_DIV_EXPR
:
1906 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1910 case TRUNC_MOD_EXPR
:
1916 if (INTEGRAL_TYPE_P (type1
) && INTEGRAL_TYPE_P (type2
))
1921 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1923 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
1924 || (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
1925 || (TYPE_PTRMEM_P (type1
) && TYPE_PTRMEM_P (type2
))
1926 || ((TYPE_PTRMEMFUNC_P (type1
)
1927 || TREE_CODE (type1
) == POINTER_TYPE
)
1928 && null_ptr_cst_p (args
[1])))
1938 type1
= build_reference_type (type1
);
1944 For every pair of promoted arithmetic types L and R, there
1945 exist candidate operator functions of the form
1947 LR operator?(bool, L, R);
1949 where LR is the result of the usual arithmetic conversions
1950 between types L and R.
1952 For every type T, where T is a pointer or pointer-to-member
1953 type, there exist candidate operator functions of the form T
1954 operator?(bool, T, T); */
1956 if (promoted_arithmetic_type_p (type1
)
1957 && promoted_arithmetic_type_p (type2
))
1961 /* Otherwise, the types should be pointers. */
1962 if (!(TREE_CODE (type1
) == POINTER_TYPE
1963 || TYPE_PTRMEM_P (type1
)
1964 || TYPE_PTRMEMFUNC_P (type1
))
1965 || !(TREE_CODE (type2
) == POINTER_TYPE
1966 || TYPE_PTRMEM_P (type2
)
1967 || TYPE_PTRMEMFUNC_P (type2
)))
1970 /* We don't check that the two types are the same; the logic
1971 below will actually create two candidates; one in which both
1972 parameter types are TYPE1, and one in which both parameter
1976 /* These arguments do not make for a legal overloaded operator. */
1983 /* If we're dealing with two pointer types or two enumeral types,
1984 we need candidates for both of them. */
1985 if (type2
&& !same_type_p (type1
, type2
)
1986 && TREE_CODE (type1
) == TREE_CODE (type2
)
1987 && (TREE_CODE (type1
) == REFERENCE_TYPE
1988 || (TREE_CODE (type1
) == POINTER_TYPE
1989 && TYPE_PTRMEM_P (type1
) == TYPE_PTRMEM_P (type2
))
1990 || TYPE_PTRMEMFUNC_P (type1
)
1991 || IS_AGGR_TYPE (type1
)
1992 || TREE_CODE (type1
) == ENUMERAL_TYPE
))
1994 candidates
= build_builtin_candidate
1995 (candidates
, fnname
, type1
, type1
, args
, argtypes
, flags
);
1996 return build_builtin_candidate
1997 (candidates
, fnname
, type2
, type2
, args
, argtypes
, flags
);
2000 return build_builtin_candidate
2001 (candidates
, fnname
, type1
, type2
, args
, argtypes
, flags
);
2005 type_decays_to (type
)
2008 if (TREE_CODE (type
) == ARRAY_TYPE
)
2009 return build_pointer_type (TREE_TYPE (type
));
2010 if (TREE_CODE (type
) == FUNCTION_TYPE
)
2011 return build_pointer_type (type
);
2015 /* There are three conditions of builtin candidates:
2017 1) bool-taking candidates. These are the same regardless of the input.
2018 2) pointer-pair taking candidates. These are generated for each type
2019 one of the input types converts to.
2020 3) arithmetic candidates. According to the standard, we should generate
2021 all of these, but I'm trying not to...
2023 Here we generate a superset of the possible candidates for this particular
2024 case. That is a subset of the full set the standard defines, plus some
2025 other cases which the standard disallows. add_builtin_candidate will
2026 filter out the illegal set. */
2028 static struct z_candidate
*
2029 add_builtin_candidates (candidates
, code
, code2
, fnname
, args
, flags
)
2030 struct z_candidate
*candidates
;
2031 enum tree_code code
, code2
;
2037 tree type
, argtypes
[3];
2038 /* TYPES[i] is the set of possible builtin-operator parameter types
2039 we will consider for the Ith argument. These are represented as
2040 a TREE_LIST; the TREE_VALUE of each node is the potential
2044 for (i
= 0; i
< 3; ++i
)
2047 argtypes
[i
] = lvalue_type (args
[i
]);
2049 argtypes
[i
] = NULL_TREE
;
2054 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2055 and VQ is either volatile or empty, there exist candidate operator
2056 functions of the form
2057 VQ T& operator++(VQ T&); */
2059 case POSTINCREMENT_EXPR
:
2060 case PREINCREMENT_EXPR
:
2061 case POSTDECREMENT_EXPR
:
2062 case PREDECREMENT_EXPR
:
2067 /* 24There also exist candidate operator functions of the form
2068 bool operator!(bool);
2069 bool operator&&(bool, bool);
2070 bool operator||(bool, bool); */
2072 case TRUTH_NOT_EXPR
:
2073 return build_builtin_candidate
2074 (candidates
, fnname
, boolean_type_node
,
2075 NULL_TREE
, args
, argtypes
, flags
);
2077 case TRUTH_ORIF_EXPR
:
2078 case TRUTH_ANDIF_EXPR
:
2079 return build_builtin_candidate
2080 (candidates
, fnname
, boolean_type_node
,
2081 boolean_type_node
, args
, argtypes
, flags
);
2102 types
[0] = types
[1] = NULL_TREE
;
2104 for (i
= 0; i
< 2; ++i
)
2108 else if (IS_AGGR_TYPE (argtypes
[i
]))
2112 if (i
== 0 && code
== MODIFY_EXPR
&& code2
== NOP_EXPR
)
2115 convs
= lookup_conversions (argtypes
[i
]);
2117 if (code
== COND_EXPR
)
2119 if (real_lvalue_p (args
[i
]))
2120 types
[i
] = tree_cons
2121 (NULL_TREE
, build_reference_type (argtypes
[i
]), types
[i
]);
2123 types
[i
] = tree_cons
2124 (NULL_TREE
, TYPE_MAIN_VARIANT (argtypes
[i
]), types
[i
]);
2130 for (; convs
; convs
= TREE_CHAIN (convs
))
2132 type
= TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs
))));
2135 && (TREE_CODE (type
) != REFERENCE_TYPE
2136 || CP_TYPE_CONST_P (TREE_TYPE (type
))))
2139 if (code
== COND_EXPR
&& TREE_CODE (type
) == REFERENCE_TYPE
)
2140 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2142 type
= non_reference (type
);
2143 if (i
!= 0 || ! ref1
)
2145 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
2146 if (enum_p
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
2147 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2148 if (INTEGRAL_TYPE_P (type
))
2149 type
= type_promotes_to (type
);
2152 if (! value_member (type
, types
[i
]))
2153 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2158 if (code
== COND_EXPR
&& real_lvalue_p (args
[i
]))
2159 types
[i
] = tree_cons
2160 (NULL_TREE
, build_reference_type (argtypes
[i
]), types
[i
]);
2161 type
= non_reference (argtypes
[i
]);
2162 if (i
!= 0 || ! ref1
)
2164 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
2165 if (enum_p
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
2166 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2167 if (INTEGRAL_TYPE_P (type
))
2168 type
= type_promotes_to (type
);
2170 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2174 /* Run through the possible parameter types of both arguments,
2175 creating candidates with those parameter types. */
2176 for (; types
[0]; types
[0] = TREE_CHAIN (types
[0]))
2179 for (type
= types
[1]; type
; type
= TREE_CHAIN (type
))
2180 candidates
= add_builtin_candidate
2181 (candidates
, code
, code2
, fnname
, TREE_VALUE (types
[0]),
2182 TREE_VALUE (type
), args
, argtypes
, flags
);
2184 candidates
= add_builtin_candidate
2185 (candidates
, code
, code2
, fnname
, TREE_VALUE (types
[0]),
2186 NULL_TREE
, args
, argtypes
, flags
);
2193 /* If TMPL can be successfully instantiated as indicated by
2194 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2196 TMPL is the template. EXPLICIT_TARGS are any explicit template
2197 arguments. ARGLIST is the arguments provided at the call-site.
2198 The RETURN_TYPE is the desired type for conversion operators. If
2199 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2200 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2201 add_conv_candidate. */
2203 static struct z_candidate
*
2204 add_template_candidate_real (candidates
, tmpl
, ctype
, explicit_targs
,
2205 arglist
, return_type
, flags
,
2207 struct z_candidate
*candidates
;
2208 tree tmpl
, ctype
, explicit_targs
, arglist
, return_type
;
2211 unification_kind_t strict
;
2213 int ntparms
= DECL_NTPARMS (tmpl
);
2214 tree targs
= make_tree_vec (ntparms
);
2215 tree args_without_in_chrg
= arglist
;
2216 struct z_candidate
*cand
;
2220 /* We don't do deduction on the in-charge parameter, the VTT
2221 parameter or 'this'. */
2222 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl
))
2223 args_without_in_chrg
= TREE_CHAIN (args_without_in_chrg
);
2225 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl
)
2226 || DECL_BASE_CONSTRUCTOR_P (tmpl
))
2227 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl
)))
2228 args_without_in_chrg
= TREE_CHAIN (args_without_in_chrg
);
2230 i
= fn_type_unification (tmpl
, explicit_targs
, targs
,
2231 args_without_in_chrg
,
2232 return_type
, strict
, -1);
2237 fn
= instantiate_template (tmpl
, targs
);
2238 if (fn
== error_mark_node
)
2241 if (obj
!= NULL_TREE
)
2242 /* Aha, this is a conversion function. */
2243 cand
= add_conv_candidate (candidates
, fn
, obj
, arglist
);
2245 cand
= add_function_candidate (candidates
, fn
, ctype
,
2247 if (DECL_TI_TEMPLATE (fn
) != tmpl
)
2248 /* This situation can occur if a member template of a template
2249 class is specialized. Then, instantiate_template might return
2250 an instantiation of the specialization, in which case the
2251 DECL_TI_TEMPLATE field will point at the original
2252 specialization. For example:
2254 template <class T> struct S { template <class U> void f(U);
2255 template <> void f(int) {}; };
2259 Here, TMPL will be template <class U> S<double>::f(U).
2260 And, instantiate template will give us the specialization
2261 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2262 for this will point at template <class T> template <> S<T>::f(int),
2263 so that we can find the definition. For the purposes of
2264 overload resolution, however, we want the original TMPL. */
2265 cand
->template = tree_cons (tmpl
, targs
, NULL_TREE
);
2267 cand
->template = DECL_TEMPLATE_INFO (fn
);
2273 static struct z_candidate
*
2274 add_template_candidate (candidates
, tmpl
, ctype
, explicit_targs
,
2275 arglist
, return_type
, flags
, strict
)
2276 struct z_candidate
*candidates
;
2277 tree tmpl
, ctype
, explicit_targs
, arglist
, return_type
;
2279 unification_kind_t strict
;
2282 add_template_candidate_real (candidates
, tmpl
, ctype
,
2283 explicit_targs
, arglist
, return_type
, flags
,
2288 static struct z_candidate
*
2289 add_template_conv_candidate (candidates
, tmpl
, obj
, arglist
, return_type
)
2290 struct z_candidate
*candidates
;
2291 tree tmpl
, obj
, arglist
, return_type
;
2294 add_template_candidate_real (candidates
, tmpl
, NULL_TREE
, NULL_TREE
,
2295 arglist
, return_type
, 0, obj
, DEDUCE_CONV
);
2301 struct z_candidate
*cands
;
2303 for (; cands
; cands
= cands
->next
)
2304 if (pedantic
? cands
->viable
== 1 : cands
->viable
)
2309 static struct z_candidate
*
2310 splice_viable (cands
)
2311 struct z_candidate
*cands
;
2313 struct z_candidate
**p
= &cands
;
2317 if (pedantic
? (*p
)->viable
== 1 : (*p
)->viable
)
2330 /* Fix this to work on non-lvalues. */
2331 return build_unary_op (ADDR_EXPR
, obj
, 0);
2335 print_z_candidates (candidates
)
2336 struct z_candidate
*candidates
;
2338 const char *str
= "candidates are:";
2339 for (; candidates
; candidates
= candidates
->next
)
2341 if (TREE_CODE (candidates
->fn
) == IDENTIFIER_NODE
)
2343 if (TREE_VEC_LENGTH (candidates
->convs
) == 3)
2344 error ("%s %D(%T, %T, %T) <built-in>", str
, candidates
->fn
,
2345 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)),
2346 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 1)),
2347 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 2)));
2348 else if (TREE_VEC_LENGTH (candidates
->convs
) == 2)
2349 error ("%s %D(%T, %T) <built-in>", str
, candidates
->fn
,
2350 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)),
2351 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 1)));
2353 error ("%s %D(%T) <built-in>", str
, candidates
->fn
,
2354 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)));
2356 else if (TYPE_P (candidates
->fn
))
2357 error ("%s %T <conversion>", str
, candidates
->fn
);
2359 cp_error_at ("%s %+#D%s", str
, candidates
->fn
,
2360 candidates
->viable
== -1 ? " <near match>" : "");
2365 /* Returns the best overload candidate to perform the requested
2366 conversion. This function is used for three the overloading situations
2367 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2368 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2369 per [dcl.init.ref], so we ignore temporary bindings. */
2371 static struct z_candidate
*
2372 build_user_type_conversion_1 (totype
, expr
, flags
)
2376 struct z_candidate
*candidates
, *cand
;
2377 tree fromtype
= TREE_TYPE (expr
);
2378 tree ctors
= NULL_TREE
, convs
= NULL_TREE
, *p
;
2379 tree args
= NULL_TREE
;
2380 tree templates
= NULL_TREE
;
2382 /* We represent conversion within a hierarchy using RVALUE_CONV and
2383 BASE_CONV, as specified by [over.best.ics]; these become plain
2384 constructor calls, as specified in [dcl.init]. */
2385 my_friendly_assert (!IS_AGGR_TYPE (fromtype
) || !IS_AGGR_TYPE (totype
)
2386 || !DERIVED_FROM_P (totype
, fromtype
), 20011226);
2388 if (IS_AGGR_TYPE (totype
))
2389 ctors
= lookup_fnfields (TYPE_BINFO (totype
),
2390 complete_ctor_identifier
,
2393 if (IS_AGGR_TYPE (fromtype
))
2394 convs
= lookup_conversions (fromtype
);
2397 flags
|= LOOKUP_NO_CONVERSION
;
2403 ctors
= TREE_VALUE (ctors
);
2405 t
= build_int_2 (0, 0);
2406 TREE_TYPE (t
) = build_pointer_type (totype
);
2407 args
= build_tree_list (NULL_TREE
, expr
);
2408 /* We should never try to call the abstract or base constructor
2410 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors
))
2411 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors
)),
2413 args
= tree_cons (NULL_TREE
, t
, args
);
2415 for (; ctors
; ctors
= OVL_NEXT (ctors
))
2417 tree ctor
= OVL_CURRENT (ctors
);
2418 if (DECL_NONCONVERTING_P (ctor
))
2421 if (TREE_CODE (ctor
) == TEMPLATE_DECL
)
2423 templates
= tree_cons (NULL_TREE
, ctor
, templates
);
2425 add_template_candidate (candidates
, ctor
, totype
,
2426 NULL_TREE
, args
, NULL_TREE
, flags
,
2430 candidates
= add_function_candidate (candidates
, ctor
, totype
,
2435 candidates
->second_conv
= build1 (IDENTITY_CONV
, totype
, NULL_TREE
);
2436 candidates
->basetype_path
= TYPE_BINFO (totype
);
2441 args
= build_tree_list (NULL_TREE
, build_this (expr
));
2443 for (; convs
; convs
= TREE_CHAIN (convs
))
2445 tree fns
= TREE_VALUE (convs
);
2446 int convflags
= LOOKUP_NO_CONVERSION
;
2448 /* If we are called to convert to a reference type, we are trying to
2449 find an lvalue binding, so don't even consider temporaries. If
2450 we don't find an lvalue binding, the caller will try again to
2451 look for a temporary binding. */
2452 if (TREE_CODE (totype
) == REFERENCE_TYPE
)
2453 convflags
|= LOOKUP_NO_TEMP_BIND
;
2455 for (; fns
; fns
= OVL_NEXT (fns
))
2457 tree fn
= OVL_CURRENT (fns
);
2458 struct z_candidate
*old_candidates
= candidates
;
2460 /* [over.match.funcs] For conversion functions, the function
2461 is considered to be a member of the class of the implicit
2462 object argument for the purpose of defining the type of
2463 the implicit object parameter.
2465 So we pass fromtype as CTYPE to add_*_candidate. */
2467 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2469 templates
= tree_cons (NULL_TREE
, fn
, templates
);
2471 add_template_candidate (candidates
, fn
, fromtype
, NULL_TREE
,
2472 args
, totype
, flags
,
2476 candidates
= add_function_candidate (candidates
, fn
, fromtype
,
2479 if (candidates
!= old_candidates
)
2481 tree ics
= implicit_conversion
2482 (totype
, TREE_TYPE (TREE_TYPE (candidates
->fn
)),
2485 candidates
->second_conv
= ics
;
2486 candidates
->basetype_path
= TYPE_BINFO (fromtype
);
2488 if (ics
== NULL_TREE
)
2489 candidates
->viable
= 0;
2490 else if (candidates
->viable
== 1 && ICS_BAD_FLAG (ics
))
2491 candidates
->viable
= -1;
2496 if (! any_viable (candidates
))
2499 if (flags
& LOOKUP_COMPLAIN
)
2501 if (candidates
&& ! candidates
->next
)
2502 /* say why this one won't work or try to be loose */;
2504 error ("no viable candidates");
2511 candidates
= splice_viable (candidates
);
2512 cand
= tourney (candidates
);
2516 if (flags
& LOOKUP_COMPLAIN
)
2518 error ("conversion from `%T' to `%T' is ambiguous",
2520 print_z_candidates (candidates
);
2523 cand
= candidates
; /* any one will do */
2524 cand
->second_conv
= build1 (AMBIG_CONV
, totype
, expr
);
2525 ICS_USER_FLAG (cand
->second_conv
) = 1;
2526 ICS_BAD_FLAG (cand
->second_conv
) = 1;
2531 for (p
= &(cand
->second_conv
); TREE_CODE (*p
) != IDENTITY_CONV
; )
2532 p
= &(TREE_OPERAND (*p
, 0));
2536 (DECL_CONSTRUCTOR_P (cand
->fn
)
2537 ? totype
: non_reference (TREE_TYPE (TREE_TYPE (cand
->fn
)))),
2538 expr
, build_ptr_wrapper (cand
));
2540 ICS_USER_FLAG (cand
->second_conv
) = ICS_USER_FLAG (*p
) = 1;
2541 if (cand
->viable
== -1)
2542 ICS_BAD_FLAG (cand
->second_conv
) = ICS_BAD_FLAG (*p
) = 1;
2548 build_user_type_conversion (totype
, expr
, flags
)
2552 struct z_candidate
*cand
2553 = build_user_type_conversion_1 (totype
, expr
, flags
);
2557 if (TREE_CODE (cand
->second_conv
) == AMBIG_CONV
)
2558 return error_mark_node
;
2559 return convert_from_reference (convert_like (cand
->second_conv
, expr
));
2564 /* Do any initial processing on the arguments to a function call. */
2571 for (t
= args
; t
; t
= TREE_CHAIN (t
))
2573 tree arg
= TREE_VALUE (t
);
2575 if (arg
== error_mark_node
)
2576 return error_mark_node
;
2577 else if (VOID_TYPE_P (TREE_TYPE (arg
)))
2579 error ("invalid use of void expression");
2580 return error_mark_node
;
2582 else if (TREE_CODE (arg
) == OFFSET_REF
)
2583 arg
= resolve_offset_ref (arg
);
2584 arg
= convert_from_reference (arg
);
2585 TREE_VALUE (t
) = arg
;
2591 build_new_function_call (fn
, args
)
2594 struct z_candidate
*candidates
= 0, *cand
;
2595 tree explicit_targs
= NULL_TREE
;
2596 int template_only
= 0;
2598 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
2600 explicit_targs
= TREE_OPERAND (fn
, 1);
2601 fn
= TREE_OPERAND (fn
, 0);
2605 if (really_overloaded_fn (fn
))
2608 tree templates
= NULL_TREE
;
2610 args
= resolve_args (args
);
2612 if (args
== error_mark_node
)
2613 return error_mark_node
;
2615 for (t1
= fn
; t1
; t1
= OVL_CHAIN (t1
))
2617 tree t
= OVL_FUNCTION (t1
);
2619 if (TREE_CODE (t
) == TEMPLATE_DECL
)
2621 templates
= tree_cons (NULL_TREE
, t
, templates
);
2622 candidates
= add_template_candidate
2623 (candidates
, t
, NULL_TREE
, explicit_targs
, args
, NULL_TREE
,
2624 LOOKUP_NORMAL
, DEDUCE_CALL
);
2626 else if (! template_only
)
2627 candidates
= add_function_candidate
2628 (candidates
, t
, NULL_TREE
, args
, LOOKUP_NORMAL
);
2631 if (! any_viable (candidates
))
2633 if (candidates
&& ! candidates
->next
)
2634 return build_function_call (candidates
->fn
, args
);
2635 error ("no matching function for call to `%D(%A)'",
2636 DECL_NAME (OVL_FUNCTION (fn
)), args
);
2638 print_z_candidates (candidates
);
2639 return error_mark_node
;
2641 candidates
= splice_viable (candidates
);
2642 cand
= tourney (candidates
);
2646 error ("call of overloaded `%D(%A)' is ambiguous",
2647 DECL_NAME (OVL_FUNCTION (fn
)), args
);
2648 print_z_candidates (candidates
);
2649 return error_mark_node
;
2652 return build_over_call (cand
, args
, LOOKUP_NORMAL
);
2655 /* This is not really overloaded. */
2656 fn
= OVL_CURRENT (fn
);
2658 return build_function_call (fn
, args
);
2662 build_object_call (obj
, args
)
2665 struct z_candidate
*candidates
= 0, *cand
;
2666 tree fns
, convs
, mem_args
= NULL_TREE
;
2667 tree type
= TREE_TYPE (obj
);
2669 if (TYPE_PTRMEMFUNC_P (type
))
2671 /* It's no good looking for an overloaded operator() on a
2672 pointer-to-member-function. */
2673 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj
);
2674 return error_mark_node
;
2677 fns
= lookup_fnfields (TYPE_BINFO (type
), ansi_opname (CALL_EXPR
), 1);
2678 if (fns
== error_mark_node
)
2679 return error_mark_node
;
2681 args
= resolve_args (args
);
2683 if (args
== error_mark_node
)
2684 return error_mark_node
;
2688 tree base
= BINFO_TYPE (TREE_PURPOSE (fns
));
2689 mem_args
= tree_cons (NULL_TREE
, build_this (obj
), args
);
2691 for (fns
= TREE_VALUE (fns
); fns
; fns
= OVL_NEXT (fns
))
2693 tree fn
= OVL_CURRENT (fns
);
2694 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2697 = add_template_candidate (candidates
, fn
, base
, NULL_TREE
,
2698 mem_args
, NULL_TREE
,
2699 LOOKUP_NORMAL
, DEDUCE_CALL
);
2702 candidates
= add_function_candidate
2703 (candidates
, fn
, base
, mem_args
, LOOKUP_NORMAL
);
2706 candidates
->basetype_path
= TYPE_BINFO (type
);
2710 convs
= lookup_conversions (type
);
2712 for (; convs
; convs
= TREE_CHAIN (convs
))
2714 tree fns
= TREE_VALUE (convs
);
2715 tree totype
= TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns
)));
2717 if ((TREE_CODE (totype
) == POINTER_TYPE
2718 && TREE_CODE (TREE_TYPE (totype
)) == FUNCTION_TYPE
)
2719 || (TREE_CODE (totype
) == REFERENCE_TYPE
2720 && TREE_CODE (TREE_TYPE (totype
)) == FUNCTION_TYPE
)
2721 || (TREE_CODE (totype
) == REFERENCE_TYPE
2722 && TREE_CODE (TREE_TYPE (totype
)) == POINTER_TYPE
2723 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype
))) == FUNCTION_TYPE
))
2724 for (; fns
; fns
= OVL_NEXT (fns
))
2726 tree fn
= OVL_CURRENT (fns
);
2727 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2729 candidates
= add_template_conv_candidate (candidates
,
2736 candidates
= add_conv_candidate (candidates
, fn
, obj
, args
);
2740 if (! any_viable (candidates
))
2742 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj
), args
);
2743 print_z_candidates (candidates
);
2744 return error_mark_node
;
2747 candidates
= splice_viable (candidates
);
2748 cand
= tourney (candidates
);
2752 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj
), args
);
2753 print_z_candidates (candidates
);
2754 return error_mark_node
;
2757 /* Since cand->fn will be a type, not a function, for a conversion
2758 function, we must be careful not to unconditionally look at
2760 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
2761 && DECL_OVERLOADED_OPERATOR_P (cand
->fn
) == CALL_EXPR
)
2762 return build_over_call (cand
, mem_args
, LOOKUP_NORMAL
);
2764 obj
= convert_like_with_context
2765 (TREE_VEC_ELT (cand
->convs
, 0), obj
, cand
->fn
, -1);
2768 return build_function_call (obj
, args
);
2772 op_error (code
, code2
, arg1
, arg2
, arg3
, problem
)
2773 enum tree_code code
, code2
;
2774 tree arg1
, arg2
, arg3
;
2775 const char *problem
;
2779 if (code
== MODIFY_EXPR
)
2780 opname
= assignment_operator_name_info
[code2
].name
;
2782 opname
= operator_name_info
[code
].name
;
2787 error ("%s for `%T ? %T : %T' operator", problem
,
2788 error_type (arg1
), error_type (arg2
), error_type (arg3
));
2790 case POSTINCREMENT_EXPR
:
2791 case POSTDECREMENT_EXPR
:
2792 error ("%s for `%T %s' operator", problem
, error_type (arg1
), opname
);
2795 error ("%s for `%T [%T]' operator", problem
,
2796 error_type (arg1
), error_type (arg2
));
2800 error ("%s for `%T %s %T' operator", problem
,
2801 error_type (arg1
), opname
, error_type (arg2
));
2803 error ("%s for `%s %T' operator", problem
, opname
, error_type (arg1
));
2807 /* Return the implicit conversion sequence that could be used to
2808 convert E1 to E2 in [expr.cond]. */
2811 conditional_conversion (e1
, e2
)
2815 tree t1
= non_reference (TREE_TYPE (e1
));
2816 tree t2
= non_reference (TREE_TYPE (e2
));
2821 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2822 implicitly converted (clause _conv_) to the type "reference to
2823 T2", subject to the constraint that in the conversion the
2824 reference must bind directly (_dcl.init.ref_) to E1. */
2825 if (real_lvalue_p (e2
))
2827 conv
= implicit_conversion (build_reference_type (t2
),
2830 LOOKUP_NO_TEMP_BIND
);
2837 If E1 and E2 have class type, and the underlying class types are
2838 the same or one is a base class of the other: E1 can be converted
2839 to match E2 if the class of T2 is the same type as, or a base
2840 class of, the class of T1, and the cv-qualification of T2 is the
2841 same cv-qualification as, or a greater cv-qualification than, the
2842 cv-qualification of T1. If the conversion is applied, E1 is
2843 changed to an rvalue of type T2 that still refers to the original
2844 source class object (or the appropriate subobject thereof). */
2845 if (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
2846 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2
),
2847 TYPE_MAIN_VARIANT (t1
)))
2849 if (at_least_as_qualified_p (t2
, t1
))
2851 conv
= build1 (IDENTITY_CONV
, t1
, e1
);
2852 if (!same_type_p (TYPE_MAIN_VARIANT (t1
),
2853 TYPE_MAIN_VARIANT (t2
)))
2854 conv
= build_conv (BASE_CONV
, t2
, conv
);
2863 E1 can be converted to match E2 if E1 can be implicitly converted
2864 to the type that expression E2 would have if E2 were converted to
2865 an rvalue (or the type it has, if E2 is an rvalue). */
2866 return implicit_conversion (t2
, t1
, e1
, LOOKUP_NORMAL
);
2869 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2870 arguments to the conditional expression. By the time this function
2871 is called, any suitable candidate functions are included in
2875 build_conditional_expr (arg1
, arg2
, arg3
)
2883 tree result_type
= NULL_TREE
;
2885 struct z_candidate
*candidates
= 0;
2886 struct z_candidate
*cand
;
2888 /* As a G++ extension, the second argument to the conditional can be
2889 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2890 c'.) If the second operand is omitted, make sure it is
2891 calculated only once. */
2895 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2896 arg1
= arg2
= save_expr (arg1
);
2901 The first expr ession is implicitly converted to bool (clause
2903 arg1
= cp_convert (boolean_type_node
, arg1
);
2905 /* If something has already gone wrong, just pass that fact up the
2907 if (arg1
== error_mark_node
2908 || arg2
== error_mark_node
2909 || arg3
== error_mark_node
2910 || TREE_TYPE (arg1
) == error_mark_node
2911 || TREE_TYPE (arg2
) == error_mark_node
2912 || TREE_TYPE (arg3
) == error_mark_node
)
2913 return error_mark_node
;
2917 If either the second or the third operand has type (possibly
2918 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2919 array-to-pointer (_conv.array_), and function-to-pointer
2920 (_conv.func_) standard conversions are performed on the second
2921 and third operands. */
2922 arg2_type
= TREE_TYPE (arg2
);
2923 arg3_type
= TREE_TYPE (arg3
);
2924 if (VOID_TYPE_P (arg2_type
) || VOID_TYPE_P (arg3_type
))
2926 /* Do the conversions. We don't these for `void' type arguments
2927 since it can't have any effect and since decay_conversion
2928 does not handle that case gracefully. */
2929 if (!VOID_TYPE_P (arg2_type
))
2930 arg2
= decay_conversion (arg2
);
2931 if (!VOID_TYPE_P (arg3_type
))
2932 arg3
= decay_conversion (arg3
);
2933 arg2_type
= TREE_TYPE (arg2
);
2934 arg3_type
= TREE_TYPE (arg3
);
2938 One of the following shall hold:
2940 --The second or the third operand (but not both) is a
2941 throw-expression (_except.throw_); the result is of the
2942 type of the other and is an rvalue.
2944 --Both the second and the third operands have type void; the
2945 result is of type void and is an rvalue. */
2946 if ((TREE_CODE (arg2
) == THROW_EXPR
)
2947 ^ (TREE_CODE (arg3
) == THROW_EXPR
))
2948 result_type
= ((TREE_CODE (arg2
) == THROW_EXPR
)
2949 ? arg3_type
: arg2_type
);
2950 else if (VOID_TYPE_P (arg2_type
) && VOID_TYPE_P (arg3_type
))
2951 result_type
= void_type_node
;
2954 error ("`%E' has type `void' and is not a throw-expression",
2955 VOID_TYPE_P (arg2_type
) ? arg2
: arg3
);
2956 return error_mark_node
;
2960 goto valid_operands
;
2964 Otherwise, if the second and third operand have different types,
2965 and either has (possibly cv-qualified) class type, an attempt is
2966 made to convert each of those operands to the type of the other. */
2967 else if (!same_type_p (arg2_type
, arg3_type
)
2968 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)))
2970 tree conv2
= conditional_conversion (arg2
, arg3
);
2971 tree conv3
= conditional_conversion (arg3
, arg2
);
2975 If both can be converted, or one can be converted but the
2976 conversion is ambiguous, the program is ill-formed. If
2977 neither can be converted, the operands are left unchanged and
2978 further checking is performed as described below. If exactly
2979 one conversion is possible, that conversion is applied to the
2980 chosen operand and the converted operand is used in place of
2981 the original operand for the remainder of this section. */
2982 if ((conv2
&& !ICS_BAD_FLAG (conv2
)
2983 && conv3
&& !ICS_BAD_FLAG (conv3
))
2984 || (conv2
&& TREE_CODE (conv2
) == AMBIG_CONV
)
2985 || (conv3
&& TREE_CODE (conv3
) == AMBIG_CONV
))
2987 error ("operands to ?: have different types");
2988 return error_mark_node
;
2990 else if (conv2
&& !ICS_BAD_FLAG (conv2
))
2992 arg2
= convert_like (conv2
, arg2
);
2993 arg2
= convert_from_reference (arg2
);
2994 /* That may not quite have done the trick. If the two types
2995 are cv-qualified variants of one another, we will have
2996 just used an IDENTITY_CONV. (There's no conversion from
2997 an lvalue of one class type to an lvalue of another type,
2998 even a cv-qualified variant, and we don't want to lose
2999 lvalue-ness here.) So, we manually add a NOP_EXPR here
3001 if (!same_type_p (TREE_TYPE (arg2
), arg3_type
))
3002 arg2
= build1 (NOP_EXPR
, arg3_type
, arg2
);
3003 arg2_type
= TREE_TYPE (arg2
);
3005 else if (conv3
&& !ICS_BAD_FLAG (conv3
))
3007 arg3
= convert_like (conv3
, arg3
);
3008 arg3
= convert_from_reference (arg3
);
3009 if (!same_type_p (TREE_TYPE (arg3
), arg2_type
))
3010 arg3
= build1 (NOP_EXPR
, arg2_type
, arg3
);
3011 arg3_type
= TREE_TYPE (arg3
);
3017 If the second and third operands are lvalues and have the same
3018 type, the result is of that type and is an lvalue. */
3019 if (real_lvalue_p (arg2
) && real_lvalue_p (arg3
) &&
3020 same_type_p (arg2_type
, arg3_type
))
3022 result_type
= arg2_type
;
3023 goto valid_operands
;
3028 Otherwise, the result is an rvalue. If the second and third
3029 operand do not have the same type, and either has (possibly
3030 cv-qualified) class type, overload resolution is used to
3031 determine the conversions (if any) to be applied to the operands
3032 (_over.match.oper_, _over.built_). */
3034 if (!same_type_p (arg2_type
, arg3_type
)
3035 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)))
3040 /* Rearrange the arguments so that add_builtin_candidate only has
3041 to know about two args. In build_builtin_candidates, the
3042 arguments are unscrambled. */
3046 candidates
= add_builtin_candidates (candidates
,
3049 ansi_opname (COND_EXPR
),
3055 If the overload resolution fails, the program is
3057 if (!any_viable (candidates
))
3059 op_error (COND_EXPR
, NOP_EXPR
, arg1
, arg2
, arg3
, "no match");
3060 print_z_candidates (candidates
);
3061 return error_mark_node
;
3063 candidates
= splice_viable (candidates
);
3064 cand
= tourney (candidates
);
3067 op_error (COND_EXPR
, NOP_EXPR
, arg1
, arg2
, arg3
, "no match");
3068 print_z_candidates (candidates
);
3069 return error_mark_node
;
3074 Otherwise, the conversions thus determined are applied, and
3075 the converted operands are used in place of the original
3076 operands for the remainder of this section. */
3077 conv
= TREE_VEC_ELT (cand
->convs
, 0);
3078 arg1
= convert_like (conv
, arg1
);
3079 conv
= TREE_VEC_ELT (cand
->convs
, 1);
3080 arg2
= convert_like (conv
, arg2
);
3081 conv
= TREE_VEC_ELT (cand
->convs
, 2);
3082 arg3
= convert_like (conv
, arg3
);
3087 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3088 and function-to-pointer (_conv.func_) standard conversions are
3089 performed on the second and third operands.
3091 We need to force the lvalue-to-rvalue conversion here for class types,
3092 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3093 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3096 We use ocp_convert rather than build_user_type_conversion because the
3097 latter returns NULL_TREE on failure, while the former gives an error. */
3099 if (IS_AGGR_TYPE (TREE_TYPE (arg2
)) && real_lvalue_p (arg2
))
3100 arg2
= ocp_convert (TREE_TYPE (arg2
), arg2
,
3101 CONV_IMPLICIT
|CONV_FORCE_TEMP
, LOOKUP_NORMAL
);
3103 arg2
= decay_conversion (arg2
);
3104 arg2_type
= TREE_TYPE (arg2
);
3106 if (IS_AGGR_TYPE (TREE_TYPE (arg3
)) && real_lvalue_p (arg3
))
3107 arg3
= ocp_convert (TREE_TYPE (arg3
), arg3
,
3108 CONV_IMPLICIT
|CONV_FORCE_TEMP
, LOOKUP_NORMAL
);
3110 arg3
= decay_conversion (arg3
);
3111 arg3_type
= TREE_TYPE (arg3
);
3113 if (arg2
== error_mark_node
|| arg3
== error_mark_node
)
3114 return error_mark_node
;
3118 After those conversions, one of the following shall hold:
3120 --The second and third operands have the same type; the result is of
3122 if (same_type_p (arg2_type
, arg3_type
))
3123 result_type
= arg2_type
;
3126 --The second and third operands have arithmetic or enumeration
3127 type; the usual arithmetic conversions are performed to bring
3128 them to a common type, and the result is of that type. */
3129 else if ((ARITHMETIC_TYPE_P (arg2_type
)
3130 || TREE_CODE (arg2_type
) == ENUMERAL_TYPE
)
3131 && (ARITHMETIC_TYPE_P (arg3_type
)
3132 || TREE_CODE (arg3_type
) == ENUMERAL_TYPE
))
3134 /* In this case, there is always a common type. */
3135 result_type
= type_after_usual_arithmetic_conversions (arg2_type
,
3138 if (TREE_CODE (arg2_type
) == ENUMERAL_TYPE
3139 && TREE_CODE (arg3_type
) == ENUMERAL_TYPE
)
3140 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3141 arg2_type
, arg3_type
);
3142 else if (extra_warnings
3143 && ((TREE_CODE (arg2_type
) == ENUMERAL_TYPE
3144 && !same_type_p (arg3_type
, type_promotes_to (arg2_type
)))
3145 || (TREE_CODE (arg3_type
) == ENUMERAL_TYPE
3146 && !same_type_p (arg2_type
, type_promotes_to (arg3_type
)))))
3147 warning ("enumeral and non-enumeral type in conditional expression");
3149 arg2
= perform_implicit_conversion (result_type
, arg2
);
3150 arg3
= perform_implicit_conversion (result_type
, arg3
);
3154 --The second and third operands have pointer type, or one has
3155 pointer type and the other is a null pointer constant; pointer
3156 conversions (_conv.ptr_) and qualification conversions
3157 (_conv.qual_) are performed to bring them to their composite
3158 pointer type (_expr.rel_). The result is of the composite
3161 --The second and third operands have pointer to member type, or
3162 one has pointer to member type and the other is a null pointer
3163 constant; pointer to member conversions (_conv.mem_) and
3164 qualification conversions (_conv.qual_) are performed to bring
3165 them to a common type, whose cv-qualification shall match the
3166 cv-qualification of either the second or the third operand.
3167 The result is of the common type. */
3168 else if ((null_ptr_cst_p (arg2
)
3169 && (TYPE_PTR_P (arg3_type
) || TYPE_PTRMEM_P (arg3_type
)
3170 || TYPE_PTRMEMFUNC_P (arg3_type
)))
3171 || (null_ptr_cst_p (arg3
)
3172 && (TYPE_PTR_P (arg2_type
) || TYPE_PTRMEM_P (arg2_type
)
3173 || TYPE_PTRMEMFUNC_P (arg2_type
)))
3174 || (TYPE_PTR_P (arg2_type
) && TYPE_PTR_P (arg3_type
))
3175 || (TYPE_PTRMEM_P (arg2_type
) && TYPE_PTRMEM_P (arg3_type
))
3176 || (TYPE_PTRMEMFUNC_P (arg2_type
)
3177 && TYPE_PTRMEMFUNC_P (arg3_type
)))
3179 result_type
= composite_pointer_type (arg2_type
, arg3_type
, arg2
,
3180 arg3
, "conditional expression");
3181 arg2
= perform_implicit_conversion (result_type
, arg2
);
3182 arg3
= perform_implicit_conversion (result_type
, arg3
);
3187 error ("operands to ?: have different types");
3188 return error_mark_node
;
3192 result
= fold (build (COND_EXPR
, result_type
, arg1
, arg2
, arg3
));
3193 /* Expand both sides into the same slot, hopefully the target of the
3194 ?: expression. We used to check for TARGET_EXPRs here, but now we
3195 sometimes wrap them in NOP_EXPRs so the test would fail. */
3196 if (!lvalue_p
&& IS_AGGR_TYPE (result_type
))
3197 result
= build_target_expr_with_type (result
, result_type
);
3199 /* If this expression is an rvalue, but might be mistaken for an
3200 lvalue, we must add a NON_LVALUE_EXPR. */
3201 if (!lvalue_p
&& real_lvalue_p (result
))
3202 result
= build1 (NON_LVALUE_EXPR
, result_type
, result
);
3208 build_new_op (code
, flags
, arg1
, arg2
, arg3
)
3209 enum tree_code code
;
3211 tree arg1
, arg2
, arg3
;
3213 struct z_candidate
*candidates
= 0, *cand
;
3214 tree fns
, mem_arglist
= NULL_TREE
, arglist
, fnname
;
3215 enum tree_code code2
= NOP_EXPR
;
3216 tree templates
= NULL_TREE
;
3219 if (arg1
== error_mark_node
3220 || arg2
== error_mark_node
3221 || arg3
== error_mark_node
)
3222 return error_mark_node
;
3224 /* This can happen if a template takes all non-type parameters, e.g.
3225 undeclared_template<1, 5, 72>a; */
3226 if (code
== LT_EXPR
&& TREE_CODE (arg1
) == TEMPLATE_DECL
)
3228 error ("`%D' must be declared before use", arg1
);
3229 return error_mark_node
;
3232 if (code
== MODIFY_EXPR
)
3234 code2
= TREE_CODE (arg3
);
3236 fnname
= ansi_assopname (code2
);
3239 fnname
= ansi_opname (code
);
3241 if (TREE_CODE (arg1
) == OFFSET_REF
)
3242 arg1
= resolve_offset_ref (arg1
);
3243 arg1
= convert_from_reference (arg1
);
3249 case VEC_DELETE_EXPR
:
3251 /* Use build_op_new_call and build_op_delete_call instead. */
3255 return build_object_call (arg1
, arg2
);
3263 if (TREE_CODE (arg2
) == OFFSET_REF
)
3264 arg2
= resolve_offset_ref (arg2
);
3265 arg2
= convert_from_reference (arg2
);
3269 if (TREE_CODE (arg3
) == OFFSET_REF
)
3270 arg3
= resolve_offset_ref (arg3
);
3271 arg3
= convert_from_reference (arg3
);
3274 if (code
== COND_EXPR
)
3276 if (arg2
== NULL_TREE
3277 || TREE_CODE (TREE_TYPE (arg2
)) == VOID_TYPE
3278 || TREE_CODE (TREE_TYPE (arg3
)) == VOID_TYPE
3279 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2
))
3280 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3
))))
3283 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1
))
3284 && (! arg2
|| ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2
))))
3287 if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
3288 arg2
= integer_zero_node
;
3290 arglist
= NULL_TREE
;
3292 arglist
= tree_cons (NULL_TREE
, arg3
, arglist
);
3294 arglist
= tree_cons (NULL_TREE
, arg2
, arglist
);
3295 arglist
= tree_cons (NULL_TREE
, arg1
, arglist
);
3297 fns
= lookup_function_nonclass (fnname
, arglist
);
3299 if (fns
&& TREE_CODE (fns
) == TREE_LIST
)
3300 fns
= TREE_VALUE (fns
);
3301 for (; fns
; fns
= OVL_NEXT (fns
))
3303 tree fn
= OVL_CURRENT (fns
);
3304 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
3306 templates
= tree_cons (NULL_TREE
, fn
, templates
);
3308 = add_template_candidate (candidates
, fn
, NULL_TREE
, NULL_TREE
,
3309 arglist
, TREE_TYPE (fnname
),
3310 flags
, DEDUCE_CALL
);
3313 candidates
= add_function_candidate (candidates
, fn
, NULL_TREE
,
3317 if (IS_AGGR_TYPE (TREE_TYPE (arg1
)))
3319 fns
= lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1
)), fnname
, 1);
3320 if (fns
== error_mark_node
)
3328 tree basetype
= BINFO_TYPE (TREE_PURPOSE (fns
));
3329 mem_arglist
= tree_cons (NULL_TREE
, build_this (arg1
), TREE_CHAIN (arglist
));
3330 for (fns
= TREE_VALUE (fns
); fns
; fns
= OVL_NEXT (fns
))
3332 tree fn
= OVL_CURRENT (fns
);
3335 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
3336 this_arglist
= mem_arglist
;
3338 this_arglist
= arglist
;
3340 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
3342 /* A member template. */
3343 templates
= tree_cons (NULL_TREE
, fn
, templates
);
3345 = add_template_candidate (candidates
, fn
, basetype
, NULL_TREE
,
3346 this_arglist
, TREE_TYPE (fnname
),
3347 flags
, DEDUCE_CALL
);
3350 candidates
= add_function_candidate
3351 (candidates
, fn
, basetype
, this_arglist
, flags
);
3354 candidates
->basetype_path
= TYPE_BINFO (TREE_TYPE (arg1
));
3361 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3362 to know about two args; a builtin candidate will always have a first
3363 parameter of type bool. We'll handle that in
3364 build_builtin_candidate. */
3365 if (code
== COND_EXPR
)
3375 args
[2] = NULL_TREE
;
3378 candidates
= add_builtin_candidates
3379 (candidates
, code
, code2
, fnname
, args
, flags
);
3382 if (! any_viable (candidates
))
3386 case POSTINCREMENT_EXPR
:
3387 case POSTDECREMENT_EXPR
:
3388 /* Look for an `operator++ (int)'. If they didn't have
3389 one, then we fall back to the old way of doing things. */
3390 if (flags
& LOOKUP_COMPLAIN
)
3391 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3393 operator_name_info
[code
].name
);
3394 if (code
== POSTINCREMENT_EXPR
)
3395 code
= PREINCREMENT_EXPR
;
3397 code
= PREDECREMENT_EXPR
;
3398 return build_new_op (code
, flags
, arg1
, NULL_TREE
, NULL_TREE
);
3400 /* The caller will deal with these. */
3409 if (flags
& LOOKUP_COMPLAIN
)
3411 op_error (code
, code2
, arg1
, arg2
, arg3
, "no match");
3412 print_z_candidates (candidates
);
3414 return error_mark_node
;
3416 candidates
= splice_viable (candidates
);
3417 cand
= tourney (candidates
);
3421 if (flags
& LOOKUP_COMPLAIN
)
3423 op_error (code
, code2
, arg1
, arg2
, arg3
, "ambiguous overload");
3424 print_z_candidates (candidates
);
3426 return error_mark_node
;
3429 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
)
3431 extern int warn_synth
;
3433 && fnname
== ansi_assopname (NOP_EXPR
)
3434 && DECL_ARTIFICIAL (cand
->fn
)
3436 && ! candidates
->next
->next
)
3438 warning ("using synthesized `%#D' for copy assignment",
3440 cp_warning_at (" where cfront would use `%#D'",
3442 ? candidates
->next
->fn
3446 return build_over_call
3448 TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
3449 ? mem_arglist
: arglist
,
3453 /* Check for comparison of different enum types. */
3462 if (TREE_CODE (TREE_TYPE (arg1
)) == ENUMERAL_TYPE
3463 && TREE_CODE (TREE_TYPE (arg2
)) == ENUMERAL_TYPE
3464 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
))
3465 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2
))))
3467 warning ("comparison between `%#T' and `%#T'",
3468 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
3475 /* We need to strip any leading REF_BIND so that bitfields don't cause
3476 errors. This should not remove any important conversions, because
3477 builtins don't apply to class objects directly. */
3478 conv
= TREE_VEC_ELT (cand
->convs
, 0);
3479 if (TREE_CODE (conv
) == REF_BIND
)
3480 conv
= TREE_OPERAND (conv
, 0);
3481 arg1
= convert_like (conv
, arg1
);
3484 conv
= TREE_VEC_ELT (cand
->convs
, 1);
3485 if (TREE_CODE (conv
) == REF_BIND
)
3486 conv
= TREE_OPERAND (conv
, 0);
3487 arg2
= convert_like (conv
, arg2
);
3491 conv
= TREE_VEC_ELT (cand
->convs
, 2);
3492 if (TREE_CODE (conv
) == REF_BIND
)
3493 conv
= TREE_OPERAND (conv
, 0);
3494 arg3
= convert_like (conv
, arg3
);
3501 return build_modify_expr (arg1
, code2
, arg2
);
3504 return build_indirect_ref (arg1
, "unary *");
3509 case TRUNC_DIV_EXPR
:
3520 case TRUNC_MOD_EXPR
:
3524 case TRUTH_ANDIF_EXPR
:
3525 case TRUTH_ORIF_EXPR
:
3526 return cp_build_binary_op (code
, arg1
, arg2
);
3531 case TRUTH_NOT_EXPR
:
3532 case PREINCREMENT_EXPR
:
3533 case POSTINCREMENT_EXPR
:
3534 case PREDECREMENT_EXPR
:
3535 case POSTDECREMENT_EXPR
:
3538 return build_unary_op (code
, arg1
, candidates
!= 0);
3541 return build_array_ref (arg1
, arg2
);
3544 return build_conditional_expr (arg1
, arg2
, arg3
);
3547 return build_m_component_ref
3548 (build_indirect_ref (arg1
, NULL
), arg2
);
3550 /* The caller will deal with these. */
3562 /* Build a call to operator delete. This has to be handled very specially,
3563 because the restrictions on what signatures match are different from all
3564 other call instances. For a normal delete, only a delete taking (void *)
3565 or (void *, size_t) is accepted. For a placement delete, only an exact
3566 match with the placement new is accepted.
3568 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3569 ADDR is the pointer to be deleted.
3570 SIZE is the size of the memory block to be deleted.
3571 FLAGS are the usual overloading flags.
3572 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3575 build_op_delete_call (code
, addr
, size
, flags
, placement
)
3576 enum tree_code code
;
3577 tree addr
, size
, placement
;
3580 tree fn
= NULL_TREE
;
3581 tree fns
, fnname
, fntype
, argtypes
, args
, type
;
3584 if (addr
== error_mark_node
)
3585 return error_mark_node
;
3587 type
= TREE_TYPE (TREE_TYPE (addr
));
3588 while (TREE_CODE (type
) == ARRAY_TYPE
)
3589 type
= TREE_TYPE (type
);
3591 fnname
= ansi_opname (code
);
3593 if (IS_AGGR_TYPE (type
) && ! (flags
& LOOKUP_GLOBAL
))
3596 If the result of the lookup is ambiguous or inaccessible, or if
3597 the lookup selects a placement deallocation function, the
3598 program is ill-formed.
3600 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3602 fns
= lookup_fnfields (TYPE_BINFO (type
), fnname
, 1);
3603 if (fns
== error_mark_node
)
3604 return error_mark_node
;
3609 if (fns
== NULL_TREE
)
3610 fns
= lookup_name_nonclass (fnname
);
3614 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3616 /* Extract the function. */
3617 argtypes
= TREE_OPERAND (TREE_OPERAND (placement
, 0), 0);
3618 /* Then the second parm type. */
3619 argtypes
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes
)));
3621 /* Also the second argument. */
3622 args
= TREE_CHAIN (TREE_OPERAND (placement
, 1));
3626 /* First try it without the size argument. */
3627 argtypes
= void_list_node
;
3631 /* Strip const and volatile from addr. */
3632 addr
= cp_convert (ptr_type_node
, addr
);
3634 /* We make two tries at finding a matching `operator delete'. On
3635 the first pass, we look for an one-operator (or placement)
3636 operator delete. If we're not doing placement delete, then on
3637 the second pass we look for a two-argument delete. */
3638 for (pass
= 0; pass
< (placement
? 1 : 2); ++pass
)
3641 argtypes
= tree_cons (NULL_TREE
, ptr_type_node
, argtypes
);
3643 /* Normal delete; now try to find a match including the size
3645 argtypes
= tree_cons (NULL_TREE
, ptr_type_node
,
3646 tree_cons (NULL_TREE
, sizetype
,
3648 fntype
= build_function_type (void_type_node
, argtypes
);
3650 /* Go through the `operator delete' functions looking for one
3651 with a matching type. */
3652 for (fn
= BASELINK_P (fns
) ? TREE_VALUE (fns
) : fns
;
3658 /* Exception specifications on the `delete' operator do not
3660 t
= build_exception_variant (TREE_TYPE (OVL_CURRENT (fn
)),
3662 /* We also don't compare attributes. We're really just
3663 trying to check the types of the first two parameters. */
3664 if (comptypes (t
, fntype
, COMPARE_NO_ATTRIBUTES
))
3668 /* If we found a match, we're done. */
3673 /* If we have a matching function, call it. */
3676 /* Make sure we have the actual function, and not an
3678 fn
= OVL_CURRENT (fn
);
3680 /* If the FN is a member function, make sure that it is
3682 if (DECL_CLASS_SCOPE_P (fn
))
3683 enforce_access (type
, fn
);
3686 args
= tree_cons (NULL_TREE
, addr
, args
);
3688 args
= tree_cons (NULL_TREE
, addr
,
3689 build_tree_list (NULL_TREE
, size
));
3691 return build_function_call (fn
, args
);
3694 /* If we are doing placement delete we do nothing if we don't find a
3695 matching op delete. */
3699 error ("no suitable `operator delete' for `%T'", type
);
3700 return error_mark_node
;
3703 /* If the current scope isn't allowed to access DECL along
3704 BASETYPE_PATH, give an error. The most derived class in
3705 BASETYPE_PATH is the one used to qualify DECL. */
3708 enforce_access (basetype_path
, decl
)
3714 accessible
= accessible_p (basetype_path
, decl
);
3717 if (TREE_PRIVATE (decl
))
3718 cp_error_at ("`%+#D' is private", decl
);
3719 else if (TREE_PROTECTED (decl
))
3720 cp_error_at ("`%+#D' is protected", decl
);
3722 cp_error_at ("`%+#D' is inaccessible", decl
);
3723 error ("within this context");
3730 /* Perform the conversions in CONVS on the expression EXPR.
3731 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3732 indicates the `this' argument of a method. INNER is non-zero when
3733 being called to continue a conversion chain. It is negative when a
3734 reference binding will be applied, positive otherwise. */
3737 convert_like_real (convs
, expr
, fn
, argnum
, inner
)
3745 tree totype
= TREE_TYPE (convs
);
3747 if (ICS_BAD_FLAG (convs
)
3748 && TREE_CODE (convs
) != USER_CONV
3749 && TREE_CODE (convs
) != AMBIG_CONV
3750 && TREE_CODE (convs
) != REF_BIND
)
3753 for (; t
; t
= TREE_OPERAND (t
, 0))
3755 if (TREE_CODE (t
) == USER_CONV
|| !ICS_BAD_FLAG (t
))
3757 expr
= convert_like_real (t
, expr
, fn
, argnum
, 1);
3760 else if (TREE_CODE (t
) == AMBIG_CONV
)
3761 return convert_like_real (t
, expr
, fn
, argnum
, 1);
3762 else if (TREE_CODE (t
) == IDENTITY_CONV
)
3765 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr
), totype
);
3767 pedwarn (" initializing argument %P of `%D'", argnum
, fn
);
3768 return cp_convert (totype
, expr
);
3772 expr
= dubious_conversion_warnings
3773 (totype
, expr
, "argument", fn
, argnum
);
3774 switch (TREE_CODE (convs
))
3778 struct z_candidate
*cand
3779 = WRAPPER_PTR (TREE_OPERAND (convs
, 1));
3780 tree convfn
= cand
->fn
;
3783 if (DECL_CONSTRUCTOR_P (convfn
))
3785 tree t
= build_int_2 (0, 0);
3786 TREE_TYPE (t
) = build_pointer_type (DECL_CONTEXT (convfn
));
3788 args
= build_tree_list (NULL_TREE
, expr
);
3789 if (DECL_HAS_IN_CHARGE_PARM_P (convfn
)
3790 || DECL_HAS_VTT_PARM_P (convfn
))
3791 /* We should never try to call the abstract or base constructor
3794 args
= tree_cons (NULL_TREE
, t
, args
);
3797 args
= build_this (expr
);
3798 expr
= build_over_call (cand
, args
, LOOKUP_NORMAL
);
3800 /* If this is a constructor or a function returning an aggr type,
3801 we need to build up a TARGET_EXPR. */
3802 if (DECL_CONSTRUCTOR_P (convfn
))
3803 expr
= build_cplus_new (totype
, expr
);
3805 /* The result of the call is then used to direct-initialize the object
3806 that is the destination of the copy-initialization. [dcl.init]
3808 Note that this step is not reflected in the conversion sequence;
3809 it affects the semantics when we actually perform the
3810 conversion, but is not considered during overload resolution.
3812 If the target is a class, that means call a ctor. */
3813 if (IS_AGGR_TYPE (totype
)
3814 && (inner
>= 0 || !lvalue_p (expr
)))
3816 savew
= warningcount
, savee
= errorcount
;
3817 expr
= build_new_method_call
3818 (NULL_TREE
, complete_ctor_identifier
,
3819 build_tree_list (NULL_TREE
, expr
), TYPE_BINFO (totype
),
3820 /* Core issue 84, now a DR, says that we don't allow UDCs
3821 for these args (which deliberately breaks copy-init of an
3822 auto_ptr<Base> from an auto_ptr<Derived>). */
3823 LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
|LOOKUP_NO_CONVERSION
);
3825 /* Tell the user where this failing constructor call came from. */
3828 if (warningcount
> savew
)
3830 (" initializing argument %P of `%D' from result of `%D'",
3831 argnum
, fn
, convfn
);
3832 else if (errorcount
> savee
)
3834 (" initializing argument %P of `%D' from result of `%D'",
3835 argnum
, fn
, convfn
);
3839 if (warningcount
> savew
)
3840 warning (" initializing temporary from result of `%D'",
3842 else if (errorcount
> savee
)
3843 error (" initializing temporary from result of `%D'",
3846 expr
= build_cplus_new (totype
, expr
);
3851 if (type_unknown_p (expr
))
3852 expr
= instantiate_type (totype
, expr
, tf_error
| tf_warning
);
3855 /* Call build_user_type_conversion again for the error. */
3856 return build_user_type_conversion
3857 (totype
, TREE_OPERAND (convs
, 0), LOOKUP_NORMAL
);
3863 expr
= convert_like_real (TREE_OPERAND (convs
, 0), expr
, fn
, argnum
,
3864 TREE_CODE (convs
) == REF_BIND
? -1 : 1);
3865 if (expr
== error_mark_node
)
3866 return error_mark_node
;
3868 /* Convert a non-array constant variable to its underlying value, unless we
3869 are about to bind it to a reference, in which case we need to
3870 leave it as an lvalue. */
3871 if (TREE_CODE (convs
) != REF_BIND
3872 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
3873 expr
= decl_constant_value (expr
);
3875 switch (TREE_CODE (convs
))
3878 if (! IS_AGGR_TYPE (totype
))
3880 /* else fall through */
3882 if (TREE_CODE (convs
) == BASE_CONV
&& !NEED_TEMPORARY_P (convs
))
3884 /* We are going to bind a reference directly to a base-class
3885 subobject of EXPR. */
3886 tree base_ptr
= build_pointer_type (totype
);
3888 /* Build an expression for `*((base*) &expr)'. */
3889 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
3890 expr
= perform_implicit_conversion (base_ptr
, expr
);
3891 expr
= build_indirect_ref (expr
, "implicit conversion");
3895 /* Copy-initialization where the cv-unqualified version of the source
3896 type is the same class as, or a derived class of, the class of the
3897 destination [is treated as direct-initialization]. [dcl.init] */
3898 savew
= warningcount
, savee
= errorcount
;
3899 expr
= build_new_method_call (NULL_TREE
, complete_ctor_identifier
,
3900 build_tree_list (NULL_TREE
, expr
),
3901 TYPE_BINFO (totype
),
3902 LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
);
3905 if (warningcount
> savew
)
3906 warning (" initializing argument %P of `%D'", argnum
, fn
);
3907 else if (errorcount
> savee
)
3908 error (" initializing argument %P of `%D'", argnum
, fn
);
3910 return build_cplus_new (totype
, expr
);
3914 tree ref_type
= totype
;
3916 /* If necessary, create a temporary. */
3917 if (NEED_TEMPORARY_P (convs
) || !lvalue_p (expr
))
3919 tree type
= TREE_TYPE (TREE_OPERAND (convs
, 0));
3920 expr
= build_target_expr_with_type (expr
, type
);
3923 /* Take the address of the thing to which we will bind the
3925 expr
= build_unary_op (ADDR_EXPR
, expr
, 1);
3926 if (expr
== error_mark_node
)
3927 return error_mark_node
;
3929 /* Convert it to a pointer to the type referred to by the
3930 reference. This will adjust the pointer if a derived to
3931 base conversion is being performed. */
3932 expr
= cp_convert (build_pointer_type (TREE_TYPE (ref_type
)),
3934 /* Convert the pointer to the desired reference type. */
3935 expr
= build1 (NOP_EXPR
, ref_type
, expr
);
3941 return decay_conversion (expr
);
3944 /* Warn about deprecated conversion if appropriate. */
3945 string_conv_p (totype
, expr
, 1);
3951 return ocp_convert (totype
, expr
, CONV_IMPLICIT
,
3952 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
);
3955 /* ARG is being passed to a varargs function. Perform any conversions
3956 required. Array/function to pointer decay must have already happened.
3957 Return the converted value. */
3960 convert_arg_to_ellipsis (arg
)
3963 if (TREE_CODE (TREE_TYPE (arg
)) == REAL_TYPE
3964 && (TYPE_PRECISION (TREE_TYPE (arg
))
3965 < TYPE_PRECISION (double_type_node
)))
3966 /* Convert `float' to `double'. */
3967 arg
= cp_convert (double_type_node
, arg
);
3969 /* Convert `short' and `char' to full-size `int'. */
3970 arg
= default_conversion (arg
);
3972 arg
= require_complete_type (arg
);
3974 if (arg
!= error_mark_node
&& ! pod_type_p (TREE_TYPE (arg
)))
3976 /* Undefined behaviour [expr.call] 5.2.2/7. */
3977 warning ("cannot pass objects of non-POD type `%#T' through `...'",
3984 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
3987 build_x_va_arg (expr
, type
)
3991 if (processing_template_decl
)
3992 return build_min (VA_ARG_EXPR
, type
, expr
);
3994 type
= complete_type_or_else (type
, NULL_TREE
);
3996 if (expr
== error_mark_node
|| !type
)
3997 return error_mark_node
;
3999 if (! pod_type_p (type
))
4001 /* Undefined behaviour [expr.call] 5.2.2/7. */
4002 warning ("cannot receive objects of non-POD type `%#T' through `...'",
4006 return build_va_arg (expr
, type
);
4009 /* TYPE has been given to va_arg. Apply the default conversions which would
4010 have happened when passed via ellipsis. Return the promoted type, or
4011 NULL_TREE, if there is no change. */
4014 convert_type_from_ellipsis (type
)
4019 if (TREE_CODE (type
) == ARRAY_TYPE
)
4020 promote
= build_pointer_type (TREE_TYPE (type
));
4021 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
4022 promote
= build_pointer_type (type
);
4024 promote
= type_promotes_to (type
);
4026 return same_type_p (type
, promote
) ? NULL_TREE
: promote
;
4029 /* ARG is a default argument expression being passed to a parameter of
4030 the indicated TYPE, which is a parameter to FN. Do any required
4031 conversions. Return the converted value. */
4034 convert_default_arg (type
, arg
, fn
, parmnum
)
4040 if (TREE_CODE (arg
) == DEFAULT_ARG
)
4042 /* When processing the default args for a class, we can find that
4043 there is an ordering constraint, and we call a function who's
4044 default args have not yet been converted. For instance,
4047 void Foo (A const & = A ());
4049 We must process A::A before A::Foo's default arg can be converted.
4050 Remember the dependent function, so do_pending_defargs can retry,
4052 unprocessed_defarg_fn (fn
);
4054 /* Don't return error_mark node, as we won't be able to distinguish
4055 genuine errors from this case, and that would lead to repeated
4056 diagnostics. Just make something of the right type. */
4057 return build1 (NOP_EXPR
, type
, integer_zero_node
);
4060 if (fn
&& DECL_TEMPLATE_INFO (fn
))
4061 arg
= tsubst_default_argument (fn
, type
, arg
);
4063 arg
= break_out_target_exprs (arg
);
4065 if (TREE_CODE (arg
) == CONSTRUCTOR
)
4067 arg
= digest_init (type
, arg
, 0);
4068 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_NORMAL
,
4069 "default argument", fn
, parmnum
);
4073 /* This could get clobbered by the following call. */
4074 if (TREE_HAS_CONSTRUCTOR (arg
))
4075 arg
= copy_node (arg
);
4077 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_NORMAL
,
4078 "default argument", fn
, parmnum
);
4079 if (PROMOTE_PROTOTYPES
4080 && INTEGRAL_TYPE_P (type
)
4081 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
4082 arg
= default_conversion (arg
);
4088 /* Subroutine of the various build_*_call functions. Overload resolution
4089 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4090 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4091 bitmask of various LOOKUP_* flags which apply to the call itself. */
4094 build_over_call (cand
, args
, flags
)
4095 struct z_candidate
*cand
;
4100 tree convs
= cand
->convs
;
4101 tree converted_args
= NULL_TREE
;
4102 tree parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4103 tree conv
, arg
, val
;
4107 /* Give any warnings we noticed during overload resolution. */
4109 for (val
= cand
->warnings
; val
; val
= TREE_CHAIN (val
))
4110 joust (cand
, WRAPPER_PTR (TREE_VALUE (val
)), 1);
4112 if (DECL_FUNCTION_MEMBER_P (fn
))
4113 enforce_access (cand
->basetype_path
, fn
);
4115 if (args
&& TREE_CODE (args
) != TREE_LIST
)
4116 args
= build_tree_list (NULL_TREE
, args
);
4119 /* The implicit parameters to a constructor are not considered by overload
4120 resolution, and must be of the proper type. */
4121 if (DECL_CONSTRUCTOR_P (fn
))
4123 converted_args
= tree_cons (NULL_TREE
, TREE_VALUE (arg
), converted_args
);
4124 arg
= TREE_CHAIN (arg
);
4125 parm
= TREE_CHAIN (parm
);
4126 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
4127 /* We should never try to call the abstract constructor. */
4129 if (DECL_HAS_VTT_PARM_P (fn
))
4131 converted_args
= tree_cons
4132 (NULL_TREE
, TREE_VALUE (arg
), converted_args
);
4133 arg
= TREE_CHAIN (arg
);
4134 parm
= TREE_CHAIN (parm
);
4137 /* Bypass access control for 'this' parameter. */
4138 else if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
4140 tree parmtype
= TREE_VALUE (parm
);
4141 tree argtype
= TREE_TYPE (TREE_VALUE (arg
));
4143 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs
, i
)))
4144 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4145 TREE_TYPE (argtype
), fn
);
4147 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4148 X is called for an object that is not of type X, or of a type
4149 derived from X, the behavior is undefined.
4151 So we can assume that anything passed as 'this' is non-null, and
4152 optimize accordingly. */
4153 my_friendly_assert (TREE_CODE (parmtype
) == POINTER_TYPE
, 19990811);
4154 t
= lookup_base (TREE_TYPE (TREE_TYPE (TREE_VALUE (arg
))),
4155 TREE_TYPE (parmtype
), ba_ignore
, NULL
);
4156 t
= build_base_path (PLUS_EXPR
, TREE_VALUE (arg
), t
, 1);
4157 converted_args
= tree_cons (NULL_TREE
, t
, converted_args
);
4158 parm
= TREE_CHAIN (parm
);
4159 arg
= TREE_CHAIN (arg
);
4165 parm
= TREE_CHAIN (parm
), arg
= TREE_CHAIN (arg
), ++i
)
4167 tree type
= TREE_VALUE (parm
);
4169 conv
= TREE_VEC_ELT (convs
, i
);
4170 val
= convert_like_with_context
4171 (conv
, TREE_VALUE (arg
), fn
, i
- is_method
);
4173 if (PROMOTE_PROTOTYPES
4174 && INTEGRAL_TYPE_P (type
)
4175 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
4176 val
= default_conversion (val
);
4177 converted_args
= tree_cons (NULL_TREE
, val
, converted_args
);
4180 /* Default arguments */
4181 for (; parm
&& parm
!= void_list_node
; parm
= TREE_CHAIN (parm
), i
++)
4183 = tree_cons (NULL_TREE
,
4184 convert_default_arg (TREE_VALUE (parm
),
4185 TREE_PURPOSE (parm
),
4190 for (; arg
; arg
= TREE_CHAIN (arg
))
4192 = tree_cons (NULL_TREE
,
4193 convert_arg_to_ellipsis (TREE_VALUE (arg
)),
4196 converted_args
= nreverse (converted_args
);
4199 check_function_format (NULL
, TYPE_ATTRIBUTES (TREE_TYPE (fn
)),
4202 /* Avoid actually calling copy constructors and copy assignment operators,
4205 if (! flag_elide_constructors
)
4206 /* Do things the hard way. */;
4207 else if (TREE_VEC_LENGTH (convs
) == 1
4208 && DECL_COPY_CONSTRUCTOR_P (fn
))
4211 arg
= skip_artificial_parms_for (fn
, converted_args
);
4212 arg
= TREE_VALUE (arg
);
4214 /* Pull out the real argument, disregarding const-correctness. */
4216 while (TREE_CODE (targ
) == NOP_EXPR
4217 || TREE_CODE (targ
) == NON_LVALUE_EXPR
4218 || TREE_CODE (targ
) == CONVERT_EXPR
)
4219 targ
= TREE_OPERAND (targ
, 0);
4220 if (TREE_CODE (targ
) == ADDR_EXPR
)
4222 targ
= TREE_OPERAND (targ
, 0);
4223 if (!same_type_ignoring_top_level_qualifiers_p
4224 (TREE_TYPE (TREE_TYPE (arg
)), TREE_TYPE (targ
)))
4233 arg
= build_indirect_ref (arg
, 0);
4235 /* [class.copy]: the copy constructor is implicitly defined even if
4236 the implementation elided its use. */
4237 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn
)))
4240 /* If we're creating a temp and we already have one, don't create a
4241 new one. If we're not creating a temp but we get one, use
4242 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4243 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4244 temp or an INIT_EXPR otherwise. */
4245 if (integer_zerop (TREE_VALUE (args
)))
4247 if (! real_lvalue_p (arg
))
4249 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn
)))
4250 return build_target_expr_with_type (arg
, DECL_CONTEXT (fn
));
4252 else if ((!real_lvalue_p (arg
)
4253 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn
)))
4254 /* Empty classes have padding which can be hidden
4255 inside an (empty) base of the class. This must not
4256 be touched as it might overlay things. When the
4257 gcc core learns about empty classes, we can treat it
4258 like other classes. */
4259 && !(is_empty_class (DECL_CONTEXT (fn
))
4260 && TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn
))))
4263 tree to
= stabilize_reference
4264 (build_indirect_ref (TREE_VALUE (args
), 0));
4266 val
= build (INIT_EXPR
, DECL_CONTEXT (fn
), to
, arg
);
4267 address
= build_unary_op (ADDR_EXPR
, val
, 0);
4268 /* Avoid a warning about this expression, if the address is
4270 TREE_USED (address
) = 1;
4274 else if (DECL_OVERLOADED_OPERATOR_P (fn
) == NOP_EXPR
4276 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn
)))
4278 tree to
= stabilize_reference
4279 (build_indirect_ref (TREE_VALUE (converted_args
), 0));
4281 arg
= build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args
)), 0);
4282 if (is_empty_class (TREE_TYPE (to
)))
4284 TREE_USED (arg
) = 1;
4286 val
= build (COMPOUND_EXPR
, DECL_CONTEXT (fn
), arg
, to
);
4287 /* Even though the assignment may not actually result in any
4288 code being generated, we do not want to warn about the
4289 assignment having no effect. That would be confusing to
4290 users who may be performing the assignment as part of a
4291 generic algorithm, for example.
4293 Ideally, the notions of having side-effects and of being
4294 useless would be orthogonal. */
4295 TREE_SIDE_EFFECTS (val
) = 1;
4296 TREE_NO_UNUSED_WARNING (val
) = 1;
4299 val
= build (MODIFY_EXPR
, TREE_TYPE (to
), to
, arg
);
4305 if (DECL_VINDEX (fn
) && (flags
& LOOKUP_NONVIRTUAL
) == 0)
4307 tree t
, *p
= &TREE_VALUE (converted_args
);
4308 tree binfo
= lookup_base (TREE_TYPE (TREE_TYPE (*p
)),
4309 DECL_VIRTUAL_CONTEXT (fn
),
4311 my_friendly_assert (binfo
&& binfo
!= error_mark_node
, 20010730);
4313 *p
= build_base_path (PLUS_EXPR
, *p
, binfo
, 1);
4314 if (TREE_SIDE_EFFECTS (*p
))
4315 *p
= save_expr (*p
);
4316 t
= build_pointer_type (TREE_TYPE (fn
));
4317 if (DECL_CONTEXT (fn
) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn
)))
4318 fn
= build_java_interface_fn_ref (fn
, *p
);
4320 fn
= build_vfn_ref (build_indirect_ref (*p
, 0), DECL_VINDEX (fn
));
4323 else if (DECL_INLINE (fn
))
4324 fn
= inline_conversion (fn
);
4326 fn
= build_addr_func (fn
);
4328 /* Recognize certain built-in functions so we can make tree-codes
4329 other than CALL_EXPR. We do this when it enables fold-const.c
4330 to do something useful. */
4332 if (TREE_CODE (fn
) == ADDR_EXPR
4333 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
4334 && DECL_BUILT_IN (TREE_OPERAND (fn
, 0)))
4337 exp
= expand_tree_builtin (TREE_OPERAND (fn
, 0), args
, converted_args
);
4342 /* Some built-in function calls will be evaluated at
4343 compile-time in fold (). */
4344 fn
= fold (build_call (fn
, converted_args
));
4345 if (VOID_TYPE_P (TREE_TYPE (fn
)))
4347 fn
= require_complete_type (fn
);
4348 if (fn
== error_mark_node
)
4349 return error_mark_node
;
4350 if (IS_AGGR_TYPE (TREE_TYPE (fn
)))
4351 fn
= build_cplus_new (TREE_TYPE (fn
), fn
);
4352 return convert_from_reference (fn
);
4355 static tree java_iface_lookup_fn
;
4357 /* Make an expression which yields the address of the Java interface
4358 method FN. This is achieved by generating a call to libjava's
4359 _Jv_LookupInterfaceMethodIdx(). */
4362 build_java_interface_fn_ref (fn
, instance
)
4365 tree lookup_args
, lookup_fn
, method
, idx
;
4366 tree klass_ref
, iface
, iface_ref
;
4369 if (!java_iface_lookup_fn
)
4371 tree endlink
= build_void_list_node ();
4372 tree t
= tree_cons (NULL_TREE
, ptr_type_node
,
4373 tree_cons (NULL_TREE
, ptr_type_node
,
4374 tree_cons (NULL_TREE
, java_int_type_node
,
4376 java_iface_lookup_fn
4377 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4378 build_function_type (ptr_type_node
, t
),
4379 0, NOT_BUILT_IN
, NULL
);
4380 ggc_add_tree_root (&java_iface_lookup_fn
, 1);
4383 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4384 This is the first entry in the vtable. */
4385 klass_ref
= build_vtbl_ref (build_indirect_ref (instance
, 0),
4388 /* Get the java.lang.Class pointer for the interface being called. */
4389 iface
= DECL_CONTEXT (fn
);
4390 iface_ref
= lookup_field (iface
, get_identifier ("class$"), 0, 0);
4391 if (!iface_ref
|| TREE_CODE (iface_ref
) != VAR_DECL
4392 || DECL_CONTEXT (iface_ref
) != iface
)
4394 error ("could not find class$ field in java interface type `%T'",
4396 return error_mark_node
;
4398 iface_ref
= build1 (ADDR_EXPR
, build_pointer_type (iface
), iface_ref
);
4400 /* Determine the itable index of FN. */
4402 for (method
= TYPE_METHODS (iface
); method
; method
= TREE_CHAIN (method
))
4404 if (!DECL_VIRTUAL_P (method
))
4410 idx
= build_int_2 (i
, 0);
4412 lookup_args
= tree_cons (NULL_TREE
, klass_ref
,
4413 tree_cons (NULL_TREE
, iface_ref
,
4414 build_tree_list (NULL_TREE
, idx
)));
4415 lookup_fn
= build1 (ADDR_EXPR
,
4416 build_pointer_type (TREE_TYPE (java_iface_lookup_fn
)),
4417 java_iface_lookup_fn
);
4418 return build (CALL_EXPR
, ptr_type_node
, lookup_fn
, lookup_args
, NULL_TREE
);
4421 /* Returns the value to use for the in-charge parameter when making a
4422 call to a function with the indicated NAME. */
4425 in_charge_arg_for_name (name
)
4428 if (name
== base_ctor_identifier
4429 || name
== base_dtor_identifier
)
4430 return integer_zero_node
;
4431 else if (name
== complete_ctor_identifier
)
4432 return integer_one_node
;
4433 else if (name
== complete_dtor_identifier
)
4434 return integer_two_node
;
4435 else if (name
== deleting_dtor_identifier
)
4436 return integer_three_node
;
4438 /* This function should only be called with one of the names listed
4445 build_new_method_call (instance
, name
, args
, basetype_path
, flags
)
4446 tree instance
, name
, args
, basetype_path
;
4449 struct z_candidate
*candidates
= 0, *cand
;
4450 tree explicit_targs
= NULL_TREE
;
4451 tree basetype
, mem_args
= NULL_TREE
, fns
, instance_ptr
;
4454 tree templates
= NULL_TREE
;
4456 int template_only
= 0;
4458 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
4460 explicit_targs
= TREE_OPERAND (name
, 1);
4461 name
= TREE_OPERAND (name
, 0);
4463 name
= DECL_NAME (name
);
4466 if (TREE_CODE (name
) == COMPONENT_REF
)
4467 name
= TREE_OPERAND (name
, 1);
4468 if (TREE_CODE (name
) == OVERLOAD
)
4469 name
= DECL_NAME (OVL_CURRENT (name
));
4476 args
= resolve_args (args
);
4478 if (args
== error_mark_node
)
4479 return error_mark_node
;
4481 if (instance
== NULL_TREE
)
4482 basetype
= BINFO_TYPE (basetype_path
);
4485 if (TREE_CODE (instance
) == OFFSET_REF
)
4486 instance
= resolve_offset_ref (instance
);
4487 if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
4488 instance
= convert_from_reference (instance
);
4489 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (instance
));
4491 /* XXX this should be handled before we get here. */
4492 if (! IS_AGGR_TYPE (basetype
))
4494 if ((flags
& LOOKUP_COMPLAIN
) && basetype
!= error_mark_node
)
4495 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4496 name
, instance
, basetype
);
4498 return error_mark_node
;
4502 if (basetype_path
== NULL_TREE
)
4503 basetype_path
= TYPE_BINFO (basetype
);
4507 instance_ptr
= build_this (instance
);
4509 if (! template_only
)
4511 /* XXX this should be handled before we get here. */
4512 fns
= build_field_call (basetype_path
, instance_ptr
, name
, args
);
4519 instance_ptr
= build_int_2 (0, 0);
4520 TREE_TYPE (instance_ptr
) = build_pointer_type (basetype
);
4523 /* Callers should explicitly indicate whether they want to construct
4524 the complete object or just the part without virtual bases. */
4525 my_friendly_assert (name
!= ctor_identifier
, 20000408);
4526 /* Similarly for destructors. */
4527 my_friendly_assert (name
!= dtor_identifier
, 20000408);
4529 if (IDENTIFIER_CTOR_OR_DTOR_P (name
))
4533 constructor_p
= (name
== complete_ctor_identifier
4534 || name
== base_ctor_identifier
);
4535 pretty_name
= (constructor_p
4536 ? constructor_name (basetype
) : dtor_identifier
);
4538 /* If we're a call to a constructor or destructor for a
4539 subobject that uses virtual base classes, then we need to
4540 pass down a pointer to a VTT for the subobject. */
4541 if ((name
== base_ctor_identifier
4542 || name
== base_dtor_identifier
)
4543 && TYPE_USES_VIRTUAL_BASECLASSES (basetype
))
4547 tree basebinfo
= basetype_path
;
4549 /* If the current function is a complete object constructor
4550 or destructor, then we fetch the VTT directly.
4551 Otherwise, we look it up using the VTT we were given. */
4552 vtt
= IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type
));
4553 vtt
= decay_conversion (vtt
);
4554 vtt
= build (COND_EXPR
, TREE_TYPE (vtt
),
4555 build (EQ_EXPR
, boolean_type_node
,
4556 current_in_charge_parm
, integer_zero_node
),
4559 if (TREE_VIA_VIRTUAL (basebinfo
))
4560 basebinfo
= binfo_for_vbase (basetype
, current_class_type
);
4561 my_friendly_assert (BINFO_SUBVTT_INDEX (basebinfo
), 20010110);
4562 sub_vtt
= build (PLUS_EXPR
, TREE_TYPE (vtt
), vtt
,
4563 BINFO_SUBVTT_INDEX (basebinfo
));
4565 args
= tree_cons (NULL_TREE
, sub_vtt
, args
);
4571 fns
= lookup_fnfields (basetype_path
, name
, 1);
4573 if (fns
== error_mark_node
)
4574 return error_mark_node
;
4577 tree base
= BINFO_TYPE (TREE_PURPOSE (fns
));
4578 tree fn
= TREE_VALUE (fns
);
4579 mem_args
= tree_cons (NULL_TREE
, instance_ptr
, args
);
4580 for (; fn
; fn
= OVL_NEXT (fn
))
4582 tree t
= OVL_CURRENT (fn
);
4585 /* We can end up here for copy-init of same or base class. */
4586 if ((flags
& LOOKUP_ONLYCONVERTING
)
4587 && DECL_NONCONVERTING_P (t
))
4590 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
4591 this_arglist
= mem_args
;
4593 this_arglist
= args
;
4595 if (TREE_CODE (t
) == TEMPLATE_DECL
)
4597 /* A member template. */
4598 templates
= tree_cons (NULL_TREE
, t
, templates
);
4600 add_template_candidate (candidates
, t
, base
, explicit_targs
,
4602 TREE_TYPE (name
), flags
, DEDUCE_CALL
);
4604 else if (! template_only
)
4605 candidates
= add_function_candidate (candidates
, t
, base
,
4606 this_arglist
, flags
);
4609 candidates
->basetype_path
= basetype_path
;
4613 if (! any_viable (candidates
))
4615 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4616 if (flags
& LOOKUP_SPECULATIVELY
)
4618 if (!COMPLETE_TYPE_P (basetype
))
4619 incomplete_type_error (instance_ptr
, basetype
);
4621 error ("no matching function for call to `%T::%D(%A)%#V'",
4622 basetype
, pretty_name
, user_args
,
4623 TREE_TYPE (TREE_TYPE (instance_ptr
)));
4624 print_z_candidates (candidates
);
4625 return error_mark_node
;
4627 candidates
= splice_viable (candidates
);
4628 cand
= tourney (candidates
);
4632 error ("call of overloaded `%D(%A)' is ambiguous", pretty_name
,
4634 print_z_candidates (candidates
);
4635 return error_mark_node
;
4638 if (DECL_PURE_VIRTUAL_P (cand
->fn
)
4639 && instance
== current_class_ref
4640 && (DECL_CONSTRUCTOR_P (current_function_decl
)
4641 || DECL_DESTRUCTOR_P (current_function_decl
))
4642 && ! (flags
& LOOKUP_NONVIRTUAL
)
4643 && value_member (cand
->fn
, CLASSTYPE_PURE_VIRTUALS (basetype
)))
4644 error ((DECL_CONSTRUCTOR_P (current_function_decl
) ?
4645 "abstract virtual `%#D' called from constructor"
4646 : "abstract virtual `%#D' called from destructor"),
4648 if (TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
4649 && is_dummy_object (instance_ptr
))
4651 error ("cannot call member function `%D' without object", cand
->fn
);
4652 return error_mark_node
;
4655 if (DECL_VINDEX (cand
->fn
) && ! (flags
& LOOKUP_NONVIRTUAL
)
4656 && resolves_to_fixed_type_p (instance
, 0))
4657 flags
|= LOOKUP_NONVIRTUAL
;
4659 if (TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
)
4660 call
= build_over_call (cand
, mem_args
, flags
);
4663 call
= build_over_call (cand
, args
, flags
);
4664 /* Do evaluate the object parameter in a call to a static member
4666 if (TREE_SIDE_EFFECTS (instance
))
4667 call
= build (COMPOUND_EXPR
, TREE_TYPE (call
), instance
, call
);
4673 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4674 subsequence of ICS2. */
4677 is_subseq (ics1
, ics2
)
4680 /* We can assume that a conversion of the same code
4681 between the same types indicates a subsequence since we only get
4682 here if the types we are converting from are the same. */
4684 while (TREE_CODE (ics1
) == RVALUE_CONV
4685 || TREE_CODE (ics1
) == LVALUE_CONV
)
4686 ics1
= TREE_OPERAND (ics1
, 0);
4690 while (TREE_CODE (ics2
) == RVALUE_CONV
4691 || TREE_CODE (ics2
) == LVALUE_CONV
)
4692 ics2
= TREE_OPERAND (ics2
, 0);
4694 if (TREE_CODE (ics2
) == USER_CONV
4695 || TREE_CODE (ics2
) == AMBIG_CONV
4696 || TREE_CODE (ics2
) == IDENTITY_CONV
)
4697 /* At this point, ICS1 cannot be a proper subsequence of
4698 ICS2. We can get a USER_CONV when we are comparing the
4699 second standard conversion sequence of two user conversion
4703 ics2
= TREE_OPERAND (ics2
, 0);
4705 if (TREE_CODE (ics2
) == TREE_CODE (ics1
)
4706 && same_type_p (TREE_TYPE (ics2
), TREE_TYPE (ics1
))
4707 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2
, 0)),
4708 TREE_TYPE (TREE_OPERAND (ics1
, 0))))
4713 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4714 be any _TYPE nodes. */
4717 is_properly_derived_from (derived
, base
)
4721 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived
))
4722 || !IS_AGGR_TYPE_CODE (TREE_CODE (base
)))
4725 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4726 considers every class derived from itself. */
4727 return (!same_type_ignoring_top_level_qualifiers_p (derived
, base
)
4728 && DERIVED_FROM_P (base
, derived
));
4731 /* We build the ICS for an implicit object parameter as a pointer
4732 conversion sequence. However, such a sequence should be compared
4733 as if it were a reference conversion sequence. If ICS is the
4734 implicit conversion sequence for an implicit object parameter,
4735 modify it accordingly. */
4738 maybe_handle_implicit_object (ics
)
4741 if (ICS_THIS_FLAG (*ics
))
4743 /* [over.match.funcs]
4745 For non-static member functions, the type of the
4746 implicit object parameter is "reference to cv X"
4747 where X is the class of which the function is a
4748 member and cv is the cv-qualification on the member
4749 function declaration. */
4751 tree reference_type
;
4753 /* The `this' parameter is a pointer to a class type. Make the
4754 implict conversion talk about a reference to that same class
4756 reference_type
= TREE_TYPE (TREE_TYPE (*ics
));
4757 reference_type
= build_reference_type (reference_type
);
4759 if (TREE_CODE (t
) == QUAL_CONV
)
4760 t
= TREE_OPERAND (t
, 0);
4761 if (TREE_CODE (t
) == PTR_CONV
)
4762 t
= TREE_OPERAND (t
, 0);
4763 t
= build1 (IDENTITY_CONV
, TREE_TYPE (TREE_TYPE (t
)), NULL_TREE
);
4764 t
= direct_reference_binding (reference_type
, t
);
4769 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
4770 and return the type to which the reference refers. Otherwise,
4771 leave *ICS unchanged and return NULL_TREE. */
4774 maybe_handle_ref_bind (ics
)
4777 if (TREE_CODE (*ics
) == REF_BIND
)
4779 tree old_ics
= *ics
;
4780 tree type
= TREE_TYPE (TREE_TYPE (old_ics
));
4781 *ics
= TREE_OPERAND (old_ics
, 0);
4782 ICS_USER_FLAG (*ics
) = ICS_USER_FLAG (old_ics
);
4783 ICS_BAD_FLAG (*ics
) = ICS_BAD_FLAG (old_ics
);
4790 /* Compare two implicit conversion sequences according to the rules set out in
4791 [over.ics.rank]. Return values:
4793 1: ics1 is better than ics2
4794 -1: ics2 is better than ics1
4795 0: ics1 and ics2 are indistinguishable */
4798 compare_ics (ics1
, ics2
)
4805 tree deref_from_type1
= NULL_TREE
;
4806 tree deref_from_type2
= NULL_TREE
;
4807 tree deref_to_type1
= NULL_TREE
;
4808 tree deref_to_type2
= NULL_TREE
;
4811 /* REF_BINDING is non-zero if the result of the conversion sequence
4812 is a reference type. In that case TARGET_TYPE is the
4813 type referred to by the reference. */
4817 /* Handle implicit object parameters. */
4818 maybe_handle_implicit_object (&ics1
);
4819 maybe_handle_implicit_object (&ics2
);
4821 /* Handle reference parameters. */
4822 target_type1
= maybe_handle_ref_bind (&ics1
);
4823 target_type2
= maybe_handle_ref_bind (&ics2
);
4827 When comparing the basic forms of implicit conversion sequences (as
4828 defined in _over.best.ics_)
4830 --a standard conversion sequence (_over.ics.scs_) is a better
4831 conversion sequence than a user-defined conversion sequence
4832 or an ellipsis conversion sequence, and
4834 --a user-defined conversion sequence (_over.ics.user_) is a
4835 better conversion sequence than an ellipsis conversion sequence
4836 (_over.ics.ellipsis_). */
4837 rank1
= ICS_RANK (ics1
);
4838 rank2
= ICS_RANK (ics2
);
4842 else if (rank1
< rank2
)
4845 if (rank1
== BAD_RANK
)
4847 /* XXX Isn't this an extension? */
4848 /* Both ICS are bad. We try to make a decision based on what
4849 would have happenned if they'd been good. */
4850 if (ICS_USER_FLAG (ics1
) > ICS_USER_FLAG (ics2
)
4851 || ICS_STD_RANK (ics1
) > ICS_STD_RANK (ics2
))
4853 else if (ICS_USER_FLAG (ics1
) < ICS_USER_FLAG (ics2
)
4854 || ICS_STD_RANK (ics1
) < ICS_STD_RANK (ics2
))
4857 /* We couldn't make up our minds; try to figure it out below. */
4860 if (ICS_ELLIPSIS_FLAG (ics1
))
4861 /* Both conversions are ellipsis conversions. */
4864 /* User-defined conversion sequence U1 is a better conversion sequence
4865 than another user-defined conversion sequence U2 if they contain the
4866 same user-defined conversion operator or constructor and if the sec-
4867 ond standard conversion sequence of U1 is better than the second
4868 standard conversion sequence of U2. */
4870 if (ICS_USER_FLAG (ics1
))
4874 for (t1
= ics1
; TREE_CODE (t1
) != USER_CONV
; t1
= TREE_OPERAND (t1
, 0))
4875 if (TREE_CODE (t1
) == AMBIG_CONV
)
4877 for (t2
= ics2
; TREE_CODE (t2
) != USER_CONV
; t2
= TREE_OPERAND (t2
, 0))
4878 if (TREE_CODE (t2
) == AMBIG_CONV
)
4881 if (USER_CONV_FN (t1
) != USER_CONV_FN (t2
))
4884 /* We can just fall through here, after setting up
4885 FROM_TYPE1 and FROM_TYPE2. */
4886 from_type1
= TREE_TYPE (t1
);
4887 from_type2
= TREE_TYPE (t2
);
4891 /* We're dealing with two standard conversion sequences.
4895 Standard conversion sequence S1 is a better conversion
4896 sequence than standard conversion sequence S2 if
4898 --S1 is a proper subsequence of S2 (comparing the conversion
4899 sequences in the canonical form defined by _over.ics.scs_,
4900 excluding any Lvalue Transformation; the identity
4901 conversion sequence is considered to be a subsequence of
4902 any non-identity conversion sequence */
4905 while (TREE_CODE (from_type1
) != IDENTITY_CONV
)
4906 from_type1
= TREE_OPERAND (from_type1
, 0);
4907 from_type1
= TREE_TYPE (from_type1
);
4910 while (TREE_CODE (from_type2
) != IDENTITY_CONV
)
4911 from_type2
= TREE_OPERAND (from_type2
, 0);
4912 from_type2
= TREE_TYPE (from_type2
);
4915 if (same_type_p (from_type1
, from_type2
))
4917 if (is_subseq (ics1
, ics2
))
4919 if (is_subseq (ics2
, ics1
))
4922 /* Otherwise, one sequence cannot be a subsequence of the other; they
4923 don't start with the same type. This can happen when comparing the
4924 second standard conversion sequence in two user-defined conversion
4931 --the rank of S1 is better than the rank of S2 (by the rules
4934 Standard conversion sequences are ordered by their ranks: an Exact
4935 Match is a better conversion than a Promotion, which is a better
4936 conversion than a Conversion.
4938 Two conversion sequences with the same rank are indistinguishable
4939 unless one of the following rules applies:
4941 --A conversion that is not a conversion of a pointer, or pointer
4942 to member, to bool is better than another conversion that is such
4945 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4946 so that we do not have to check it explicitly. */
4947 if (ICS_STD_RANK (ics1
) < ICS_STD_RANK (ics2
))
4949 else if (ICS_STD_RANK (ics2
) < ICS_STD_RANK (ics1
))
4952 to_type1
= TREE_TYPE (ics1
);
4953 to_type2
= TREE_TYPE (ics2
);
4955 if (TYPE_PTR_P (from_type1
)
4956 && TYPE_PTR_P (from_type2
)
4957 && TYPE_PTR_P (to_type1
)
4958 && TYPE_PTR_P (to_type2
))
4960 deref_from_type1
= TREE_TYPE (from_type1
);
4961 deref_from_type2
= TREE_TYPE (from_type2
);
4962 deref_to_type1
= TREE_TYPE (to_type1
);
4963 deref_to_type2
= TREE_TYPE (to_type2
);
4965 /* The rules for pointers to members A::* are just like the rules
4966 for pointers A*, except opposite: if B is derived from A then
4967 A::* converts to B::*, not vice versa. For that reason, we
4968 switch the from_ and to_ variables here. */
4969 else if (TYPE_PTRMEM_P (from_type1
)
4970 && TYPE_PTRMEM_P (from_type2
)
4971 && TYPE_PTRMEM_P (to_type1
)
4972 && TYPE_PTRMEM_P (to_type2
))
4974 deref_to_type1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1
));
4975 deref_to_type2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2
));
4976 deref_from_type1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1
));
4977 deref_from_type2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2
));
4979 else if (TYPE_PTRMEMFUNC_P (from_type1
)
4980 && TYPE_PTRMEMFUNC_P (from_type2
)
4981 && TYPE_PTRMEMFUNC_P (to_type1
)
4982 && TYPE_PTRMEMFUNC_P (to_type2
))
4984 deref_to_type1
= TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1
);
4985 deref_to_type2
= TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2
);
4986 deref_from_type1
= TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1
);
4987 deref_from_type2
= TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2
);
4990 if (deref_from_type1
!= NULL_TREE
4991 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1
))
4992 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2
)))
4994 /* This was one of the pointer or pointer-like conversions.
4998 --If class B is derived directly or indirectly from class A,
4999 conversion of B* to A* is better than conversion of B* to
5000 void*, and conversion of A* to void* is better than
5001 conversion of B* to void*. */
5002 if (TREE_CODE (deref_to_type1
) == VOID_TYPE
5003 && TREE_CODE (deref_to_type2
) == VOID_TYPE
)
5005 if (is_properly_derived_from (deref_from_type1
,
5008 else if (is_properly_derived_from (deref_from_type2
,
5012 else if (TREE_CODE (deref_to_type1
) == VOID_TYPE
5013 || TREE_CODE (deref_to_type2
) == VOID_TYPE
)
5015 if (same_type_p (deref_from_type1
, deref_from_type2
))
5017 if (TREE_CODE (deref_to_type2
) == VOID_TYPE
)
5019 if (is_properly_derived_from (deref_from_type1
,
5023 /* We know that DEREF_TO_TYPE1 is `void' here. */
5024 else if (is_properly_derived_from (deref_from_type1
,
5029 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1
))
5030 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2
)))
5034 --If class B is derived directly or indirectly from class A
5035 and class C is derived directly or indirectly from B,
5037 --conversion of C* to B* is better than conversion of C* to
5040 --conversion of B* to A* is better than conversion of C* to
5042 if (same_type_p (deref_from_type1
, deref_from_type2
))
5044 if (is_properly_derived_from (deref_to_type1
,
5047 else if (is_properly_derived_from (deref_to_type2
,
5051 else if (same_type_p (deref_to_type1
, deref_to_type2
))
5053 if (is_properly_derived_from (deref_from_type2
,
5056 else if (is_properly_derived_from (deref_from_type1
,
5062 else if (CLASS_TYPE_P (non_reference (from_type1
))
5063 && same_type_p (from_type1
, from_type2
))
5065 tree from
= non_reference (from_type1
);
5069 --binding of an expression of type C to a reference of type
5070 B& is better than binding an expression of type C to a
5071 reference of type A&
5073 --conversion of C to B is better than conversion of C to A, */
5074 if (is_properly_derived_from (from
, to_type1
)
5075 && is_properly_derived_from (from
, to_type2
))
5077 if (is_properly_derived_from (to_type1
, to_type2
))
5079 else if (is_properly_derived_from (to_type2
, to_type1
))
5083 else if (CLASS_TYPE_P (non_reference (to_type1
))
5084 && same_type_p (to_type1
, to_type2
))
5086 tree to
= non_reference (to_type1
);
5090 --binding of an expression of type B to a reference of type
5091 A& is better than binding an expression of type C to a
5092 reference of type A&,
5094 --onversion of B to A is better than conversion of C to A */
5095 if (is_properly_derived_from (from_type1
, to
)
5096 && is_properly_derived_from (from_type2
, to
))
5098 if (is_properly_derived_from (from_type2
, from_type1
))
5100 else if (is_properly_derived_from (from_type1
, from_type2
))
5107 --S1 and S2 differ only in their qualification conversion and yield
5108 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5109 qualification signature of type T1 is a proper subset of the cv-
5110 qualification signature of type T2 */
5111 if (TREE_CODE (ics1
) == QUAL_CONV
5112 && TREE_CODE (ics2
) == QUAL_CONV
5113 && same_type_p (from_type1
, from_type2
))
5114 return comp_cv_qual_signature (to_type1
, to_type2
);
5118 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5119 types to which the references refer are the same type except for
5120 top-level cv-qualifiers, and the type to which the reference
5121 initialized by S2 refers is more cv-qualified than the type to
5122 which the reference initialized by S1 refers */
5124 if (target_type1
&& target_type2
5125 && same_type_ignoring_top_level_qualifiers_p (to_type1
, to_type2
))
5126 return comp_cv_qualification (target_type2
, target_type1
);
5128 /* Neither conversion sequence is better than the other. */
5132 /* The source type for this standard conversion sequence. */
5138 for (;; t
= TREE_OPERAND (t
, 0))
5140 if (TREE_CODE (t
) == USER_CONV
5141 || TREE_CODE (t
) == AMBIG_CONV
5142 || TREE_CODE (t
) == IDENTITY_CONV
)
5143 return TREE_TYPE (t
);
5148 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5149 a pointer to LOSER and re-running joust to produce the warning if WINNER
5150 is actually used. */
5153 add_warning (winner
, loser
)
5154 struct z_candidate
*winner
, *loser
;
5156 winner
->warnings
= tree_cons (NULL_TREE
,
5157 build_ptr_wrapper (loser
),
5161 /* Returns true iff functions are equivalent. Equivalent functions are
5162 not '==' only if one is a function-local extern function or if
5163 both are extern "C". */
5166 equal_functions (fn1
, fn2
)
5170 if (DECL_LOCAL_FUNCTION_P (fn1
) || DECL_LOCAL_FUNCTION_P (fn2
)
5171 || DECL_EXTERN_C_FUNCTION_P (fn1
))
5172 return decls_match (fn1
, fn2
);
5176 /* Compare two candidates for overloading as described in
5177 [over.match.best]. Return values:
5179 1: cand1 is better than cand2
5180 -1: cand2 is better than cand1
5181 0: cand1 and cand2 are indistinguishable */
5184 joust (cand1
, cand2
, warn
)
5185 struct z_candidate
*cand1
, *cand2
;
5189 int i
, off1
= 0, off2
= 0, len
;
5191 /* Candidates that involve bad conversions are always worse than those
5193 if (cand1
->viable
> cand2
->viable
)
5195 if (cand1
->viable
< cand2
->viable
)
5198 /* If we have two pseudo-candidates for conversions to the same type,
5199 or two candidates for the same function, arbitrarily pick one. */
5200 if (cand1
->fn
== cand2
->fn
5201 && (TYPE_P (cand1
->fn
) || DECL_P (cand1
->fn
)))
5204 /* a viable function F1
5205 is defined to be a better function than another viable function F2 if
5206 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5207 ICSi(F2), and then */
5209 /* for some argument j, ICSj(F1) is a better conversion sequence than
5212 /* For comparing static and non-static member functions, we ignore
5213 the implicit object parameter of the non-static function. The
5214 standard says to pretend that the static function has an object
5215 parm, but that won't work with operator overloading. */
5216 len
= TREE_VEC_LENGTH (cand1
->convs
);
5217 if (len
!= TREE_VEC_LENGTH (cand2
->convs
))
5219 if (DECL_STATIC_FUNCTION_P (cand1
->fn
)
5220 && ! DECL_STATIC_FUNCTION_P (cand2
->fn
))
5222 else if (! DECL_STATIC_FUNCTION_P (cand1
->fn
)
5223 && DECL_STATIC_FUNCTION_P (cand2
->fn
))
5232 for (i
= 0; i
< len
; ++i
)
5234 tree t1
= TREE_VEC_ELT (cand1
->convs
, i
+off1
);
5235 tree t2
= TREE_VEC_ELT (cand2
->convs
, i
+off2
);
5236 int comp
= compare_ics (t1
, t2
);
5241 && ICS_RANK (t1
) + ICS_RANK (t2
) == STD_RANK
+ PROMO_RANK
5242 && TREE_CODE (t1
) == STD_CONV
5243 && TREE_CODE (t2
) == STD_CONV
5244 && TREE_CODE (TREE_TYPE (t1
)) == INTEGER_TYPE
5245 && TREE_CODE (TREE_TYPE (t2
)) == INTEGER_TYPE
5246 && (TYPE_PRECISION (TREE_TYPE (t1
))
5247 == TYPE_PRECISION (TREE_TYPE (t2
)))
5248 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1
, 0)))
5249 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1
, 0)))
5252 tree type
= TREE_TYPE (TREE_OPERAND (t1
, 0));
5254 struct z_candidate
*w
, *l
;
5256 type1
= TREE_TYPE (t1
), type2
= TREE_TYPE (t2
),
5257 w
= cand1
, l
= cand2
;
5259 type1
= TREE_TYPE (t2
), type2
= TREE_TYPE (t1
),
5260 w
= cand2
, l
= cand1
;
5264 warning ("passing `%T' chooses `%T' over `%T'",
5265 type
, type1
, type2
);
5266 warning (" in call to `%D'", w
->fn
);
5272 if (winner
&& comp
!= winner
)
5281 /* warn about confusing overload resolution for user-defined conversions,
5282 either between a constructor and a conversion op, or between two
5284 if (winner
&& cand1
->second_conv
5285 && ((DECL_CONSTRUCTOR_P (cand1
->fn
)
5286 != DECL_CONSTRUCTOR_P (cand2
->fn
))
5287 /* Don't warn if the two conv ops convert to the same type... */
5288 || (! DECL_CONSTRUCTOR_P (cand1
->fn
)
5289 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1
->fn
)),
5290 TREE_TYPE (TREE_TYPE (cand2
->fn
))))))
5292 int comp
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
5295 struct z_candidate
*w
, *l
;
5298 w
= cand1
, l
= cand2
;
5300 w
= cand2
, l
= cand1
;
5301 if (DECL_CONTEXT (cand1
->fn
) == DECL_CONTEXT (cand2
->fn
)
5302 && ! DECL_CONSTRUCTOR_P (cand1
->fn
)
5303 && ! DECL_CONSTRUCTOR_P (cand2
->fn
)
5304 && (convn
= standard_conversion
5305 (TREE_TYPE (TREE_TYPE (l
->fn
)),
5306 TREE_TYPE (TREE_TYPE (w
->fn
)), NULL_TREE
))
5307 && TREE_CODE (convn
) == QUAL_CONV
)
5308 /* Don't complain about `operator char *()' beating
5309 `operator const char *() const'. */;
5312 tree source
= source_type (TREE_VEC_ELT (w
->convs
, 0));
5313 if (! DECL_CONSTRUCTOR_P (w
->fn
))
5314 source
= TREE_TYPE (source
);
5315 warning ("choosing `%D' over `%D'", w
->fn
, l
->fn
);
5316 warning (" for conversion from `%T' to `%T'",
5317 source
, TREE_TYPE (w
->second_conv
));
5318 warning (" because conversion sequence for the argument is better");
5329 F1 is a non-template function and F2 is a template function
5332 if (! cand1
->template && cand2
->template)
5334 else if (cand1
->template && ! cand2
->template)
5338 F1 and F2 are template functions and the function template for F1 is
5339 more specialized than the template for F2 according to the partial
5342 if (cand1
->template && cand2
->template)
5344 winner
= more_specialized
5345 (TI_TEMPLATE (cand1
->template), TI_TEMPLATE (cand2
->template),
5347 /* Tell the deduction code how many real function arguments
5348 we saw, not counting the implicit 'this' argument. But,
5349 add_function_candidate() suppresses the "this" argument
5352 [temp.func.order]: The presence of unused ellipsis and default
5353 arguments has no effect on the partial ordering of function
5355 TREE_VEC_LENGTH (cand1
->convs
)
5356 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1
->fn
)
5357 - DECL_CONSTRUCTOR_P (cand1
->fn
)));
5364 the context is an initialization by user-defined conversion (see
5365 _dcl.init_ and _over.match.user_) and the standard conversion
5366 sequence from the return type of F1 to the destination type (i.e.,
5367 the type of the entity being initialized) is a better conversion
5368 sequence than the standard conversion sequence from the return type
5369 of F2 to the destination type. */
5371 if (cand1
->second_conv
)
5373 winner
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
5378 /* Check whether we can discard a builtin candidate, either because we
5379 have two identical ones or matching builtin and non-builtin candidates.
5381 (Pedantically in the latter case the builtin which matched the user
5382 function should not be added to the overload set, but we spot it here.
5385 ... the builtin candidates include ...
5386 - do not have the same parameter type list as any non-template
5387 non-member candidate. */
5389 if (TREE_CODE (cand1
->fn
) == IDENTIFIER_NODE
5390 || TREE_CODE (cand2
->fn
) == IDENTIFIER_NODE
)
5392 for (i
= 0; i
< len
; ++i
)
5393 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1
->convs
, i
)),
5394 TREE_TYPE (TREE_VEC_ELT (cand2
->convs
, i
))))
5396 if (i
== TREE_VEC_LENGTH (cand1
->convs
))
5398 if (cand1
->fn
== cand2
->fn
)
5399 /* Two built-in candidates; arbitrarily pick one. */
5401 else if (TREE_CODE (cand1
->fn
) == IDENTIFIER_NODE
)
5402 /* cand1 is built-in; prefer cand2. */
5405 /* cand2 is built-in; prefer cand1. */
5410 /* If the two functions are the same (this can happen with declarations
5411 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5412 if (DECL_P (cand1
->fn
) && DECL_P (cand2
->fn
)
5413 && equal_functions (cand1
->fn
, cand2
->fn
))
5418 /* Extension: If the worst conversion for one candidate is worse than the
5419 worst conversion for the other, take the first. */
5422 int rank1
= IDENTITY_RANK
, rank2
= IDENTITY_RANK
;
5423 struct z_candidate
*w
= 0, *l
= 0;
5425 for (i
= 0; i
< len
; ++i
)
5427 if (ICS_RANK (TREE_VEC_ELT (cand1
->convs
, i
+off1
)) > rank1
)
5428 rank1
= ICS_RANK (TREE_VEC_ELT (cand1
->convs
, i
+off1
));
5429 if (ICS_RANK (TREE_VEC_ELT (cand2
->convs
, i
+off2
)) > rank2
)
5430 rank2
= ICS_RANK (TREE_VEC_ELT (cand2
->convs
, i
+off2
));
5433 winner
= 1, w
= cand1
, l
= cand2
;
5435 winner
= -1, w
= cand2
, l
= cand1
;
5440 pedwarn ("choosing `%D' over `%D'", w
->fn
, l
->fn
);
5442 " because worst conversion for the former is better than worst conversion for the latter");
5450 my_friendly_assert (!winner
, 20010121);
5454 /* Given a list of candidates for overloading, find the best one, if any.
5455 This algorithm has a worst case of O(2n) (winner is last), and a best
5456 case of O(n/2) (totally ambiguous); much better than a sorting
5459 static struct z_candidate
*
5460 tourney (candidates
)
5461 struct z_candidate
*candidates
;
5463 struct z_candidate
*champ
= candidates
, *challenger
;
5465 int champ_compared_to_predecessor
= 0;
5467 /* Walk through the list once, comparing each current champ to the next
5468 candidate, knocking out a candidate or two with each comparison. */
5470 for (challenger
= champ
->next
; challenger
; )
5472 fate
= joust (champ
, challenger
, 0);
5474 challenger
= challenger
->next
;
5479 champ
= challenger
->next
;
5482 champ_compared_to_predecessor
= 0;
5487 champ_compared_to_predecessor
= 1;
5490 challenger
= champ
->next
;
5494 /* Make sure the champ is better than all the candidates it hasn't yet
5495 been compared to. */
5497 for (challenger
= candidates
;
5499 && !(champ_compared_to_predecessor
&& challenger
->next
== champ
);
5500 challenger
= challenger
->next
)
5502 fate
= joust (champ
, challenger
, 0);
5510 /* Returns non-zero if things of type FROM can be converted to TO. */
5513 can_convert (to
, from
)
5516 return can_convert_arg (to
, from
, NULL_TREE
);
5519 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5522 can_convert_arg (to
, from
, arg
)
5525 tree t
= implicit_conversion (to
, from
, arg
, LOOKUP_NORMAL
);
5526 return (t
&& ! ICS_BAD_FLAG (t
));
5529 /* Like can_convert_arg, but allows dubious conversions as well. */
5532 can_convert_arg_bad (to
, from
, arg
)
5535 tree t
= implicit_conversion (to
, from
, arg
, LOOKUP_NORMAL
);
5539 /* Convert EXPR to TYPE. Return the converted expression.
5541 Note that we allow bad conversions here because by the time we get to
5542 this point we are committed to doing the conversion. If we end up
5543 doing a bad conversion, convert_like will complain. */
5546 perform_implicit_conversion (type
, expr
)
5552 if (expr
== error_mark_node
)
5553 return error_mark_node
;
5554 conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
5558 error ("could not convert `%E' to `%T'", expr
, type
);
5559 return error_mark_node
;
5562 return convert_like (conv
, expr
);
5565 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5566 initializing a variable of that TYPE. Return the converted
5570 initialize_reference (type
, expr
)
5576 conv
= reference_binding (type
, TREE_TYPE (expr
), expr
, LOOKUP_NORMAL
);
5577 if (!conv
|| ICS_BAD_FLAG (conv
))
5579 error ("could not convert `%E' to `%T'", expr
, type
);
5580 return error_mark_node
;
5583 return convert_like (conv
, expr
);