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. */
29 #include "coretypes.h"
39 #include "diagnostic.h"
41 extern int inhibit_warnings
;
43 static tree
build_field_call (tree
, tree
, tree
);
44 static struct z_candidate
* tourney (struct z_candidate
*);
45 static int equal_functions (tree
, tree
);
46 static int joust (struct z_candidate
*, struct z_candidate
*, bool);
47 static int compare_ics (tree
, tree
);
48 static tree
build_over_call (struct z_candidate
*, tree
, int);
49 static tree
build_java_interface_fn_ref (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 (tree
, tree
, tree
, int, int);
55 static void op_error (enum tree_code
, enum tree_code
, tree
, tree
,
57 static tree
build_object_call (tree
, tree
);
58 static tree
resolve_args (tree
);
59 static struct z_candidate
*build_user_type_conversion_1 (tree
, tree
, int);
60 static void print_z_candidates (struct z_candidate
*);
61 static tree
build_this (tree
);
62 static struct z_candidate
*splice_viable (struct z_candidate
*);
63 static bool any_viable (struct z_candidate
*);
64 static bool any_strictly_viable (struct z_candidate
*);
65 static struct z_candidate
*add_template_candidate
66 (struct z_candidate
*, tree
, tree
, tree
, tree
, tree
,
67 tree
, tree
, int, unification_kind_t
);
68 static struct z_candidate
*add_template_candidate_real
69 (struct z_candidate
*, tree
, tree
, tree
, tree
, tree
,
70 tree
, tree
, int, tree
, unification_kind_t
);
71 static struct z_candidate
*add_template_conv_candidate
72 (struct z_candidate
*, tree
, tree
, tree
, tree
, tree
, tree
);
73 static struct z_candidate
*add_builtin_candidates
74 (struct z_candidate
*, enum tree_code
, enum tree_code
,
76 static struct z_candidate
*add_builtin_candidate
77 (struct z_candidate
*, enum tree_code
, enum tree_code
,
78 tree
, tree
, tree
, tree
*, tree
*, int);
79 static bool is_complete (tree
);
80 static struct z_candidate
*build_builtin_candidate
81 (struct z_candidate
*, tree
, tree
, tree
, tree
*, tree
*,
83 static struct z_candidate
*add_conv_candidate
84 (struct z_candidate
*, tree
, tree
, tree
, tree
, tree
);
85 static struct z_candidate
*add_function_candidate
86 (struct z_candidate
*, tree
, tree
, tree
, tree
, tree
, int);
87 static tree
implicit_conversion (tree
, tree
, tree
, int);
88 static tree
standard_conversion (tree
, tree
, tree
);
89 static tree
reference_binding (tree
, tree
, tree
, int);
90 static tree
non_reference (tree
);
91 static tree
build_conv (enum tree_code
, tree
, tree
);
92 static bool is_subseq (tree
, tree
);
93 static tree
maybe_handle_ref_bind (tree
*);
94 static void maybe_handle_implicit_object (tree
*);
95 static struct z_candidate
*add_candidate
96 (struct z_candidate
*, tree
, tree
, tree
, tree
, int);
97 static tree
source_type (tree
);
98 static void add_warning (struct z_candidate
*, struct z_candidate
*);
99 static bool reference_related_p (tree
, tree
);
100 static bool reference_compatible_p (tree
, tree
);
101 static tree
convert_class_to_reference (tree
, tree
, tree
);
102 static tree
direct_reference_binding (tree
, tree
);
103 static bool promoted_arithmetic_type_p (tree
);
104 static tree
conditional_conversion (tree
, tree
);
105 static char *name_as_c_string (tree
, tree
, bool *);
106 static tree
call_builtin_trap (void);
109 build_vfield_ref (tree datum
, tree type
)
111 if (datum
== error_mark_node
)
112 return error_mark_node
;
114 if (TREE_CODE (TREE_TYPE (datum
)) == REFERENCE_TYPE
)
115 datum
= convert_from_reference (datum
);
117 if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type
)
118 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum
), type
))
119 datum
= convert_to_base (datum
, type
, /*check_access=*/false);
121 return build (COMPONENT_REF
, TREE_TYPE (TYPE_VFIELD (type
)),
122 datum
, TYPE_VFIELD (type
));
125 /* Build a call to a member of an object. I.e., one that overloads
126 operator ()(), or is a pointer-to-function or pointer-to-method. */
129 build_field_call (tree instance_ptr
, tree decl
, tree parms
)
133 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
136 if (TREE_CODE (decl
) == FIELD_DECL
|| TREE_CODE (decl
) == VAR_DECL
)
138 /* If it's a field, try overloading operator (),
139 or calling if the field is a pointer-to-function. */
140 instance
= build_indirect_ref (instance_ptr
, NULL
);
141 instance
= build_class_member_access_expr (instance
, decl
,
142 /*access_path=*/NULL_TREE
,
143 /*preserve_reference=*/false);
145 if (instance
== error_mark_node
)
146 return error_mark_node
;
148 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
149 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
,
150 instance
, parms
, NULL_TREE
);
151 else if (TREE_CODE (TREE_TYPE (instance
)) == FUNCTION_TYPE
152 || (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
153 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
)))
155 return build_function_call (instance
, parms
);
161 /* Returns nonzero iff the destructor name specified in NAME
162 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
166 check_dtor_name (tree basetype
, tree name
)
168 name
= TREE_OPERAND (name
, 0);
170 /* Just accept something we've already complained about. */
171 if (name
== error_mark_node
)
174 if (TREE_CODE (name
) == TYPE_DECL
)
175 name
= TREE_TYPE (name
);
176 else if (TYPE_P (name
))
178 else if (TREE_CODE (name
) == IDENTIFIER_NODE
)
180 if ((IS_AGGR_TYPE (basetype
) && name
== constructor_name (basetype
))
181 || (TREE_CODE (basetype
) == ENUMERAL_TYPE
182 && name
== TYPE_IDENTIFIER (basetype
)))
185 name
= get_type_value (name
);
189 template <class T> struct S { ~S(); };
193 NAME will be a class template. */
194 else if (DECL_CLASS_TEMPLATE_P (name
))
199 if (name
&& TYPE_MAIN_VARIANT (basetype
) == TYPE_MAIN_VARIANT (name
))
204 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
205 This is how virtual function calls are avoided. */
208 build_scoped_method_call (tree exp
, tree basetype
, tree name
, tree parms
)
210 /* Because this syntactic form does not allow
211 a pointer to a base class to be `stolen',
212 we need not protect the derived->base conversion
215 @@ But we do have to check access privileges later. */
217 tree type
= TREE_TYPE (exp
);
219 if (type
== error_mark_node
220 || basetype
== error_mark_node
)
221 return error_mark_node
;
223 if (processing_template_decl
)
225 name
= build_min_nt (SCOPE_REF
, basetype
, name
);
226 return build_min_nt (METHOD_CALL_EXPR
, name
, exp
, parms
, NULL_TREE
);
229 if (TREE_CODE (type
) == REFERENCE_TYPE
)
230 type
= TREE_TYPE (type
);
232 if (TREE_CODE (basetype
) == TREE_VEC
)
235 basetype
= BINFO_TYPE (binfo
);
240 /* Check the destructor call syntax. */
241 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
243 /* We can get here if someone writes their destructor call like
244 `obj.NS::~T()'; this isn't really a scoped method call, so hand
246 if (TREE_CODE (basetype
) == NAMESPACE_DECL
)
247 return build_method_call (exp
, name
, parms
, NULL_TREE
, LOOKUP_NORMAL
);
249 if (! check_dtor_name (basetype
, name
))
250 error ("qualified type `%T' does not match destructor name `~%T'",
251 basetype
, TREE_OPERAND (name
, 0));
253 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
254 that explicit ~int is caught in the parser; this deals with typedefs
255 and template parms. */
256 if (! IS_AGGR_TYPE (basetype
))
258 if (TYPE_MAIN_VARIANT (type
) != TYPE_MAIN_VARIANT (basetype
))
259 error ("type of `%E' does not match destructor type `%T' (type was `%T')",
260 exp
, basetype
, type
);
262 return cp_convert (void_type_node
, exp
);
266 if (TREE_CODE (basetype
) == NAMESPACE_DECL
)
268 error ("`%D' is a namespace", basetype
);
269 return error_mark_node
;
271 if (! is_aggr_type (basetype
, 1))
272 return error_mark_node
;
274 if (! IS_AGGR_TYPE (type
))
276 error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
278 return error_mark_node
;
281 decl
= build_scoped_ref (exp
, basetype
, &binfo
);
285 /* Call to a destructor. */
286 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
288 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl
)))
289 return cp_convert (void_type_node
, exp
);
291 return build_delete (TREE_TYPE (decl
), decl
,
292 sfk_complete_destructor
,
293 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
,
297 /* Call to a method. */
298 return build_method_call (decl
, name
, parms
, binfo
,
299 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
);
301 return error_mark_node
;
304 /* We want the address of a function or method. We avoid creating a
305 pointer-to-member function. */
308 build_addr_func (tree function
)
310 tree type
= TREE_TYPE (function
);
312 /* We have to do these by hand to avoid real pointer to member
314 if (TREE_CODE (type
) == METHOD_TYPE
)
318 type
= build_pointer_type (type
);
320 if (!cxx_mark_addressable (function
))
321 return error_mark_node
;
323 addr
= build1 (ADDR_EXPR
, type
, function
);
325 /* Address of a static or external variable or function counts
327 if (staticp (function
))
328 TREE_CONSTANT (addr
) = 1;
333 function
= default_conversion (function
);
338 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
339 POINTER_TYPE to those. Note, pointer to member function types
340 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
343 build_call (tree function
, tree parms
)
345 int is_constructor
= 0;
352 function
= build_addr_func (function
);
354 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
356 sorry ("unable to call pointer to member function here");
357 return error_mark_node
;
360 fntype
= TREE_TYPE (TREE_TYPE (function
));
361 result_type
= TREE_TYPE (fntype
);
363 if (TREE_CODE (function
) == ADDR_EXPR
364 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
365 decl
= TREE_OPERAND (function
, 0);
369 /* We check both the decl and the type; a function may be known not to
370 throw without being declared throw(). */
371 nothrow
= ((decl
&& TREE_NOTHROW (decl
))
372 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function
))));
374 if (decl
&& TREE_THIS_VOLATILE (decl
) && cfun
)
375 current_function_returns_abnormally
= 1;
377 if (decl
&& TREE_DEPRECATED (decl
))
378 warn_deprecated_use (decl
);
379 require_complete_eh_spec_types (fntype
, decl
);
381 if (decl
&& DECL_CONSTRUCTOR_P (decl
))
384 if (decl
&& ! TREE_USED (decl
))
386 /* We invoke build_call directly for several library functions.
387 These may have been declared normally if we're building libgcc,
388 so we can't just check DECL_ARTIFICIAL. */
389 if (DECL_ARTIFICIAL (decl
)
390 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl
)), "__", 2))
396 /* Don't pass empty class objects by value. This is useful
397 for tags in STL, which are used to control overload resolution.
398 We don't need to handle other cases of copying empty classes. */
399 if (! decl
|| ! DECL_BUILT_IN (decl
))
400 for (tmp
= parms
; tmp
; tmp
= TREE_CHAIN (tmp
))
401 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp
)))
402 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp
))))
404 tree t
= build (EMPTY_CLASS_EXPR
, TREE_TYPE (TREE_VALUE (tmp
)));
405 TREE_VALUE (tmp
) = build (COMPOUND_EXPR
, TREE_TYPE (t
),
406 TREE_VALUE (tmp
), t
);
409 function
= build_nt (CALL_EXPR
, function
, parms
, NULL_TREE
);
410 TREE_HAS_CONSTRUCTOR (function
) = is_constructor
;
411 TREE_TYPE (function
) = result_type
;
412 TREE_SIDE_EFFECTS (function
) = 1;
413 TREE_NOTHROW (function
) = nothrow
;
418 /* Build something of the form ptr->method (args)
419 or object.method (args). This can also build
420 calls to constructors, and find friends.
422 Member functions always take their class variable
425 INSTANCE is a class instance.
427 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
429 PARMS help to figure out what that NAME really refers to.
431 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
432 down to the real instance type to use for access checking. We need this
433 information to get protected accesses correct. This parameter is used
434 by build_member_call.
436 FLAGS is the logical disjunction of zero or more LOOKUP_
437 flags. See cp-tree.h for more info.
439 If this is all OK, calls build_function_call with the resolved
442 This function must also handle being called to perform
443 initialization, promotion/coercion of arguments, and
444 instantiation of default parameters.
446 Note that NAME may refer to an instance variable name. If
447 `operator()()' is defined for the type of that field, then we return
450 #ifdef GATHER_STATISTICS
451 extern int n_build_method_call
;
455 build_method_call (tree instance
, tree name
, tree parms
,
456 tree basetype_path
, int flags
)
460 tree template_args
= NULL_TREE
;
461 bool has_template_args
= false;
463 #ifdef GATHER_STATISTICS
464 n_build_method_call
++;
467 if (instance
== error_mark_node
468 || name
== error_mark_node
469 || parms
== error_mark_node
470 || (instance
&& TREE_TYPE (instance
) == error_mark_node
))
471 return error_mark_node
;
473 if (processing_template_decl
)
474 return build_min_nt (METHOD_CALL_EXPR
, name
, instance
, parms
, NULL_TREE
);
476 if (TREE_CODE (instance
) == OFFSET_REF
)
477 instance
= resolve_offset_ref (instance
);
478 if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
479 instance
= convert_from_reference (instance
);
480 object_type
= TREE_TYPE (instance
);
482 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
487 error ("destructors take no parameters");
489 if (! check_dtor_name (object_type
, name
))
491 ("destructor name `~%T' does not match type `%T' of expression",
492 TREE_OPERAND (name
, 0), object_type
);
494 if (! TYPE_HAS_DESTRUCTOR (complete_type (object_type
)))
495 return cp_convert (void_type_node
, instance
);
496 instance
= default_conversion (instance
);
497 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
498 return build_delete (build_pointer_type (object_type
),
499 instance_ptr
, sfk_complete_destructor
,
500 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0);
503 if (!CLASS_TYPE_P (object_type
))
505 if ((flags
& LOOKUP_COMPLAIN
)
506 && TREE_TYPE (instance
) != error_mark_node
)
507 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
508 name
, instance
, object_type
);
509 return error_mark_node
;
512 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
514 template_args
= TREE_OPERAND (name
, 1);
515 has_template_args
= true;
516 name
= TREE_OPERAND (name
, 0);
518 if (TREE_CODE (name
) == OVERLOAD
)
519 name
= DECL_NAME (get_first_fn (name
));
520 else if (DECL_P (name
))
521 name
= DECL_NAME (name
);
522 if (has_template_args
)
523 fn
= lookup_fnfields (object_type
, name
, /*protect=*/2);
525 fn
= lookup_member (object_type
, name
, /*protect=*/2, /*want_type=*/0);
527 if (fn
&& TREE_CODE (fn
) == TREE_LIST
&& !BASELINK_P (fn
))
529 error ("request for member `%D' is ambiguous", name
);
530 print_candidates (fn
);
531 return error_mark_node
;
534 /* If the name could not be found, issue an error. */
537 unqualified_name_lookup_error (name
);
538 return error_mark_node
;
541 if (BASELINK_P (fn
) && has_template_args
)
542 BASELINK_FUNCTIONS (fn
)
543 = build_nt (TEMPLATE_ID_EXPR
,
544 BASELINK_FUNCTIONS (fn
),
546 if (BASELINK_P (fn
) && basetype_path
)
547 BASELINK_ACCESS_BINFO (fn
) = basetype_path
;
549 return build_new_method_call (instance
, fn
, parms
,
550 /*conversion_path=*/NULL_TREE
, flags
);
553 /* New overloading code. */
555 struct z_candidate
GTY(()) {
556 /* The FUNCTION_DECL that will be called if this candidate is
557 selected by overload resolution. */
562 /* If FN is a member function, the binfo indicating the path used to
563 qualify the name of FN at the call site. This path is used to
564 determine whether or not FN is accessible if it is selected by
565 overload resolution. The DECL_CONTEXT of FN will always be a
566 (possibly improper) base of this binfo. */
568 /* If FN is a non-static member function, the binfo indicating the
569 subobject to which the `this' pointer should be converted if FN
570 is selected by overload resolution. The type pointed to the by
571 the `this' pointer must correspond to the most derived class
572 indicated by the CONVERSION_PATH. */
573 tree conversion_path
;
576 struct z_candidate
*next
;
579 #define IDENTITY_RANK 0
585 #define ELLIPSIS_RANK 6
588 #define ICS_RANK(NODE) \
589 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
590 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
591 : ICS_USER_FLAG (NODE) ? USER_RANK \
592 : ICS_STD_RANK (NODE))
594 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
596 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
597 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
598 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
599 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
601 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
602 should be created to hold the result of the conversion. */
603 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
605 #define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
606 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
609 null_ptr_cst_p (tree t
)
613 A null pointer constant is an integral constant expression
614 (_expr.const_) rvalue of integer type that evaluates to zero. */
616 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t
)) && integer_zerop (t
)))
622 /* Returns nonzero if PARMLIST consists of only default parms and/or
626 sufficient_parms_p (tree parmlist
)
628 for (; parmlist
&& parmlist
!= void_list_node
;
629 parmlist
= TREE_CHAIN (parmlist
))
630 if (!TREE_PURPOSE (parmlist
))
636 build_conv (enum tree_code code
, tree type
, tree from
)
639 int rank
= ICS_STD_RANK (from
);
641 /* We can't use buildl1 here because CODE could be USER_CONV, which
642 takes two arguments. In that case, the caller is responsible for
643 filling in the second argument. */
644 t
= make_node (code
);
645 TREE_TYPE (t
) = type
;
646 TREE_OPERAND (t
, 0) = from
;
659 if (rank
< EXACT_RANK
)
665 ICS_STD_RANK (t
) = rank
;
666 ICS_USER_FLAG (t
) = ICS_USER_FLAG (from
);
667 ICS_BAD_FLAG (t
) = ICS_BAD_FLAG (from
);
671 /* If T is a REFERENCE_TYPE return the type to which T refers.
672 Otherwise, return T itself. */
675 non_reference (tree t
)
677 if (TREE_CODE (t
) == REFERENCE_TYPE
)
683 strip_top_quals (tree t
)
685 if (TREE_CODE (t
) == ARRAY_TYPE
)
687 return TYPE_MAIN_VARIANT (t
);
690 /* Returns the standard conversion path (see [conv]) from type FROM to type
691 TO, if any. For proper handling of null pointer constants, you must
692 also pass the expression EXPR to convert from. */
695 standard_conversion (tree to
, tree from
, tree expr
)
697 enum tree_code fcode
, tcode
;
699 bool fromref
= false;
701 if (TREE_CODE (to
) == REFERENCE_TYPE
)
703 if (TREE_CODE (from
) == REFERENCE_TYPE
)
706 from
= TREE_TYPE (from
);
708 to
= strip_top_quals (to
);
709 from
= strip_top_quals (from
);
711 if ((TYPE_PTRFN_P (to
) || TYPE_PTRMEMFUNC_P (to
))
712 && expr
&& type_unknown_p (expr
))
714 expr
= instantiate_type (to
, expr
, tf_none
);
715 if (expr
== error_mark_node
)
717 from
= TREE_TYPE (expr
);
720 fcode
= TREE_CODE (from
);
721 tcode
= TREE_CODE (to
);
723 conv
= build1 (IDENTITY_CONV
, from
, expr
);
725 if (fcode
== FUNCTION_TYPE
)
727 from
= build_pointer_type (from
);
728 fcode
= TREE_CODE (from
);
729 conv
= build_conv (LVALUE_CONV
, from
, conv
);
731 else if (fcode
== ARRAY_TYPE
)
733 from
= build_pointer_type (TREE_TYPE (from
));
734 fcode
= TREE_CODE (from
);
735 conv
= build_conv (LVALUE_CONV
, from
, conv
);
737 else if (fromref
|| (expr
&& lvalue_p (expr
)))
738 conv
= build_conv (RVALUE_CONV
, from
, conv
);
740 /* Allow conversion between `__complex__' data types */
741 if (tcode
== COMPLEX_TYPE
&& fcode
== COMPLEX_TYPE
)
743 /* The standard conversion sequence to convert FROM to TO is
744 the standard conversion sequence to perform componentwise
746 tree part_conv
= standard_conversion
747 (TREE_TYPE (to
), TREE_TYPE (from
), NULL_TREE
);
751 conv
= build_conv (TREE_CODE (part_conv
), to
, conv
);
752 ICS_STD_RANK (conv
) = ICS_STD_RANK (part_conv
);
760 if (same_type_p (from
, to
))
763 if ((tcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (to
))
764 && expr
&& null_ptr_cst_p (expr
))
766 conv
= build_conv (STD_CONV
, to
, conv
);
768 else if ((tcode
== INTEGER_TYPE
&& fcode
== POINTER_TYPE
)
769 || (tcode
== POINTER_TYPE
&& fcode
== INTEGER_TYPE
))
771 /* For backwards brain damage compatibility, allow interconversion of
772 pointers and integers with a pedwarn. */
773 conv
= build_conv (STD_CONV
, to
, conv
);
774 ICS_BAD_FLAG (conv
) = 1;
776 else if (tcode
== ENUMERAL_TYPE
&& fcode
== INTEGER_TYPE
777 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
779 /* For backwards brain damage compatibility, allow interconversion of
780 enums and integers with a pedwarn. */
781 conv
= build_conv (STD_CONV
, to
, conv
);
782 ICS_BAD_FLAG (conv
) = 1;
784 else if (tcode
== POINTER_TYPE
&& fcode
== POINTER_TYPE
)
786 enum tree_code ufcode
= TREE_CODE (TREE_TYPE (from
));
787 enum tree_code utcode
= TREE_CODE (TREE_TYPE (to
));
789 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from
),
792 else if (utcode
== VOID_TYPE
&& ufcode
!= OFFSET_TYPE
793 && ufcode
!= FUNCTION_TYPE
)
795 from
= build_pointer_type
796 (cp_build_qualified_type (void_type_node
,
797 cp_type_quals (TREE_TYPE (from
))));
798 conv
= build_conv (PTR_CONV
, from
, conv
);
800 else if (ufcode
== OFFSET_TYPE
&& utcode
== OFFSET_TYPE
)
802 tree fbase
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from
));
803 tree tbase
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to
));
805 if (DERIVED_FROM_P (fbase
, tbase
)
806 && (same_type_ignoring_top_level_qualifiers_p
807 (TREE_TYPE (TREE_TYPE (from
)),
808 TREE_TYPE (TREE_TYPE (to
)))))
810 from
= build_ptrmem_type (tbase
, TREE_TYPE (TREE_TYPE (from
)));
811 conv
= build_conv (PMEM_CONV
, from
, conv
);
814 else if (IS_AGGR_TYPE (TREE_TYPE (from
))
815 && IS_AGGR_TYPE (TREE_TYPE (to
)))
817 if (DERIVED_FROM_P (TREE_TYPE (to
), TREE_TYPE (from
)))
820 cp_build_qualified_type (TREE_TYPE (to
),
821 cp_type_quals (TREE_TYPE (from
)));
822 from
= build_pointer_type (from
);
823 conv
= build_conv (PTR_CONV
, from
, conv
);
827 if (same_type_p (from
, to
))
829 else if (comp_ptr_ttypes (TREE_TYPE (to
), TREE_TYPE (from
)))
830 conv
= build_conv (QUAL_CONV
, to
, conv
);
831 else if (expr
&& string_conv_p (to
, expr
, 0))
832 /* converting from string constant to char *. */
833 conv
= build_conv (QUAL_CONV
, to
, conv
);
834 else if (ptr_reasonably_similar (TREE_TYPE (to
), TREE_TYPE (from
)))
836 conv
= build_conv (PTR_CONV
, to
, conv
);
837 ICS_BAD_FLAG (conv
) = 1;
844 else if (TYPE_PTRMEMFUNC_P (to
) && TYPE_PTRMEMFUNC_P (from
))
846 tree fromfn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from
));
847 tree tofn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to
));
848 tree fbase
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn
)));
849 tree tbase
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn
)));
851 if (!DERIVED_FROM_P (fbase
, tbase
)
852 || !same_type_p (TREE_TYPE (fromfn
), TREE_TYPE (tofn
))
853 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn
)),
854 TREE_CHAIN (TYPE_ARG_TYPES (tofn
)))
855 || cp_type_quals (fbase
) != cp_type_quals (tbase
))
858 from
= cp_build_qualified_type (tbase
, cp_type_quals (fbase
));
859 from
= build_cplus_method_type (from
, TREE_TYPE (fromfn
),
860 TREE_CHAIN (TYPE_ARG_TYPES (fromfn
)));
861 from
= build_ptrmemfunc_type (build_pointer_type (from
));
862 conv
= build_conv (PMEM_CONV
, from
, conv
);
864 else if (tcode
== BOOLEAN_TYPE
)
866 if (! (INTEGRAL_CODE_P (fcode
) || fcode
== REAL_TYPE
867 || fcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (from
)))
870 conv
= build_conv (STD_CONV
, to
, conv
);
871 if (fcode
== POINTER_TYPE
872 || (TYPE_PTRMEMFUNC_P (from
) && ICS_STD_RANK (conv
) < PBOOL_RANK
))
873 ICS_STD_RANK (conv
) = PBOOL_RANK
;
875 /* We don't check for ENUMERAL_TYPE here because there are no standard
876 conversions to enum type. */
877 else if (tcode
== INTEGER_TYPE
|| tcode
== BOOLEAN_TYPE
878 || tcode
== REAL_TYPE
)
880 if (! (INTEGRAL_CODE_P (fcode
) || fcode
== REAL_TYPE
))
882 conv
= build_conv (STD_CONV
, to
, conv
);
884 /* Give this a better rank if it's a promotion. */
885 if (to
== type_promotes_to (from
)
886 && ICS_STD_RANK (TREE_OPERAND (conv
, 0)) <= PROMO_RANK
)
887 ICS_STD_RANK (conv
) = PROMO_RANK
;
889 else if (IS_AGGR_TYPE (to
) && IS_AGGR_TYPE (from
)
890 && is_properly_derived_from (from
, to
))
892 if (TREE_CODE (conv
) == RVALUE_CONV
)
893 conv
= TREE_OPERAND (conv
, 0);
894 conv
= build_conv (BASE_CONV
, to
, conv
);
895 /* The derived-to-base conversion indicates the initialization
896 of a parameter with base type from an object of a derived
897 type. A temporary object is created to hold the result of
899 NEED_TEMPORARY_P (conv
) = 1;
907 /* Returns nonzero if T1 is reference-related to T2. */
910 reference_related_p (tree t1
, tree t2
)
912 t1
= TYPE_MAIN_VARIANT (t1
);
913 t2
= TYPE_MAIN_VARIANT (t2
);
917 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
918 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
920 return (same_type_p (t1
, t2
)
921 || (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
922 && DERIVED_FROM_P (t1
, t2
)));
925 /* Returns nonzero if T1 is reference-compatible with T2. */
928 reference_compatible_p (tree t1
, tree t2
)
932 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
933 reference-related to T2 and cv1 is the same cv-qualification as,
934 or greater cv-qualification than, cv2. */
935 return (reference_related_p (t1
, t2
)
936 && at_least_as_qualified_p (t1
, t2
));
939 /* Determine whether or not the EXPR (of class type S) can be
940 converted to T as in [over.match.ref]. */
943 convert_class_to_reference (tree t
, tree s
, tree expr
)
948 struct z_candidate
*candidates
;
949 struct z_candidate
*cand
;
953 Assuming that "cv1 T" is the underlying type of the reference
954 being initialized, and "cv S" is the type of the initializer
955 expression, with S a class type, the candidate functions are
958 --The conversion functions of S and its base classes are
959 considered. Those that are not hidden within S and yield type
960 "reference to cv2 T2", where "cv1 T" is reference-compatible
961 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
963 The argument list has one argument, which is the initializer
968 /* Conceptually, we should take the address of EXPR and put it in
969 the argument list. Unfortunately, however, that can result in
970 error messages, which we should not issue now because we are just
971 trying to find a conversion operator. Therefore, we use NULL,
972 cast to the appropriate type. */
973 arglist
= build_int_2 (0, 0);
974 TREE_TYPE (arglist
) = build_pointer_type (s
);
975 arglist
= build_tree_list (NULL_TREE
, arglist
);
977 for (conversions
= lookup_conversions (s
);
979 conversions
= TREE_CHAIN (conversions
))
981 tree fns
= TREE_VALUE (conversions
);
983 for (; fns
; fns
= OVL_NEXT (fns
))
985 tree f
= OVL_CURRENT (fns
);
986 tree t2
= TREE_TYPE (TREE_TYPE (f
));
987 struct z_candidate
*old_candidates
= candidates
;
989 /* If this is a template function, try to get an exact
991 if (TREE_CODE (f
) == TEMPLATE_DECL
)
994 = add_template_candidate (candidates
,
998 build_reference_type (t
),
1000 TREE_PURPOSE (conversions
),
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
,
1022 TREE_PURPOSE (conversions
),
1027 /* If none of the conversion functions worked out, let our caller
1029 if (!any_viable (candidates
))
1032 candidates
= splice_viable (candidates
);
1033 cand
= tourney (candidates
);
1037 conv
= build1 (IDENTITY_CONV
, s
, expr
);
1038 conv
= build_conv (USER_CONV
, TREE_TYPE (TREE_TYPE (cand
->fn
)),
1040 TREE_OPERAND (conv
, 1) = build_zc_wrapper (cand
);
1041 ICS_USER_FLAG (conv
) = 1;
1042 if (cand
->viable
== -1)
1043 ICS_BAD_FLAG (conv
) = 1;
1044 cand
->second_conv
= conv
;
1049 /* A reference of the indicated TYPE is being bound directly to the
1050 expression represented by the implicit conversion sequence CONV.
1051 Return a conversion sequence for this binding. */
1054 direct_reference_binding (tree type
, tree conv
)
1056 tree t
= TREE_TYPE (type
);
1060 When a parameter of reference type binds directly
1061 (_dcl.init.ref_) to an argument expression, the implicit
1062 conversion sequence is the identity conversion, unless the
1063 argument expression has a type that is a derived class of the
1064 parameter type, in which case the implicit conversion sequence is
1065 a derived-to-base Conversion.
1067 If the parameter binds directly to the result of applying a
1068 conversion function to the argument expression, the implicit
1069 conversion sequence is a user-defined conversion sequence
1070 (_over.ics.user_), with the second standard conversion sequence
1071 either an identity conversion or, if the conversion function
1072 returns an entity of a type that is a derived class of the
1073 parameter type, a derived-to-base conversion. */
1074 if (!same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (conv
)))
1076 /* Represent the derived-to-base conversion. */
1077 conv
= build_conv (BASE_CONV
, t
, conv
);
1078 /* We will actually be binding to the base-class subobject in
1079 the derived class, so we mark this conversion appropriately.
1080 That way, convert_like knows not to generate a temporary. */
1081 NEED_TEMPORARY_P (conv
) = 0;
1083 return build_conv (REF_BIND
, type
, conv
);
1086 /* Returns the conversion path from type FROM to reference type TO for
1087 purposes of reference binding. For lvalue binding, either pass a
1088 reference type to FROM or an lvalue expression to EXPR. If the
1089 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1090 the conversion returned. */
1093 reference_binding (tree rto
, tree rfrom
, tree expr
, int flags
)
1095 tree conv
= NULL_TREE
;
1096 tree to
= TREE_TYPE (rto
);
1100 cp_lvalue_kind lvalue_p
= clk_none
;
1102 if (TREE_CODE (to
) == FUNCTION_TYPE
&& expr
&& type_unknown_p (expr
))
1104 expr
= instantiate_type (to
, expr
, tf_none
);
1105 if (expr
== error_mark_node
)
1107 from
= TREE_TYPE (expr
);
1110 if (TREE_CODE (from
) == REFERENCE_TYPE
)
1112 /* Anything with reference type is an lvalue. */
1113 lvalue_p
= clk_ordinary
;
1114 from
= TREE_TYPE (from
);
1117 lvalue_p
= real_lvalue_p (expr
);
1119 /* Figure out whether or not the types are reference-related and
1120 reference compatible. We have do do this after stripping
1121 references from FROM. */
1122 related_p
= reference_related_p (to
, from
);
1123 compatible_p
= reference_compatible_p (to
, from
);
1125 if (lvalue_p
&& compatible_p
)
1129 If the initializer expression
1131 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1132 is reference-compatible with "cv2 T2,"
1134 the reference is bound directly to the initializer exprssion
1136 conv
= build1 (IDENTITY_CONV
, from
, expr
);
1137 conv
= direct_reference_binding (rto
, conv
);
1138 if ((lvalue_p
& clk_bitfield
) != 0
1139 && CP_TYPE_CONST_NON_VOLATILE_P (to
))
1140 /* For the purposes of overload resolution, we ignore the fact
1141 this expression is a bitfield. (In particular,
1142 [over.ics.ref] says specifically that a function with a
1143 non-const reference parameter is viable even if the
1144 argument is a bitfield.)
1146 However, when we actually call the function we must create
1147 a temporary to which to bind the reference. If the
1148 reference is volatile, or isn't const, then we cannot make
1149 a temporary, so we just issue an error when the conversion
1151 NEED_TEMPORARY_P (conv
) = 1;
1154 else if (CLASS_TYPE_P (from
) && !(flags
& LOOKUP_NO_CONVERSION
))
1158 If the initializer exprsesion
1160 -- has a class type (i.e., T2 is a class type) can be
1161 implicitly converted to an lvalue of type "cv3 T3," where
1162 "cv1 T1" is reference-compatible with "cv3 T3". (this
1163 conversion is selected by enumerating the applicable
1164 conversion functions (_over.match.ref_) and choosing the
1165 best one through overload resolution. (_over.match_).
1167 the reference is bound to the lvalue result of the conversion
1168 in the second case. */
1169 conv
= convert_class_to_reference (to
, from
, expr
);
1171 return direct_reference_binding (rto
, conv
);
1174 /* From this point on, we conceptually need temporaries, even if we
1175 elide them. Only the cases above are "direct bindings". */
1176 if (flags
& LOOKUP_NO_TEMP_BIND
)
1181 When a parameter of reference type is not bound directly to an
1182 argument expression, the conversion sequence is the one required
1183 to convert the argument expression to the underlying type of the
1184 reference according to _over.best.ics_. Conceptually, this
1185 conversion sequence corresponds to copy-initializing a temporary
1186 of the underlying type with the argument expression. Any
1187 difference in top-level cv-qualification is subsumed by the
1188 initialization itself and does not constitute a conversion. */
1192 Otherwise, the reference shall be to a non-volatile const type. */
1193 if (!CP_TYPE_CONST_NON_VOLATILE_P (to
))
1198 If the initializer expression is an rvalue, with T2 a class type,
1199 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1200 is bound in one of the following ways:
1202 -- The reference is bound to the object represented by the rvalue
1203 or to a sub-object within that object.
1205 In this case, the implicit conversion sequence is supposed to be
1206 same as we would obtain by generating a temporary. Fortunately,
1207 if the types are reference compatible, then this is either an
1208 identity conversion or the derived-to-base conversion, just as
1209 for direct binding. */
1210 if (CLASS_TYPE_P (from
) && compatible_p
)
1212 conv
= build1 (IDENTITY_CONV
, from
, expr
);
1213 return direct_reference_binding (rto
, conv
);
1218 Otherwise, a temporary of type "cv1 T1" is created and
1219 initialized from the initializer expression using the rules for a
1220 non-reference copy initialization. If T1 is reference-related to
1221 T2, cv1 must be the same cv-qualification as, or greater
1222 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1223 if (related_p
&& !at_least_as_qualified_p (to
, from
))
1226 conv
= implicit_conversion (to
, from
, expr
, flags
);
1230 conv
= build_conv (REF_BIND
, rto
, conv
);
1231 /* This reference binding, unlike those above, requires the
1232 creation of a temporary. */
1233 NEED_TEMPORARY_P (conv
) = 1;
1238 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1239 to type TO. The optional expression EXPR may affect the conversion.
1240 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1244 implicit_conversion (tree to
, tree from
, tree expr
, int flags
)
1247 struct z_candidate
*cand
;
1249 /* Resolve expressions like `A::p' that we thought might become
1250 pointers-to-members. */
1251 if (expr
&& TREE_CODE (expr
) == OFFSET_REF
)
1253 expr
= resolve_offset_ref (expr
);
1254 from
= TREE_TYPE (expr
);
1257 if (from
== error_mark_node
|| to
== error_mark_node
1258 || expr
== error_mark_node
)
1261 /* Make sure both the FROM and TO types are complete so that
1262 user-defined conversions are available. */
1263 complete_type (from
);
1266 if (TREE_CODE (to
) == REFERENCE_TYPE
)
1267 conv
= reference_binding (to
, from
, expr
, flags
);
1269 conv
= standard_conversion (to
, from
, expr
);
1273 else if (expr
!= NULL_TREE
1274 && (IS_AGGR_TYPE (from
)
1275 || IS_AGGR_TYPE (to
))
1276 && (flags
& LOOKUP_NO_CONVERSION
) == 0)
1278 cand
= build_user_type_conversion_1
1279 (to
, expr
, LOOKUP_ONLYCONVERTING
);
1281 conv
= cand
->second_conv
;
1283 /* We used to try to bind a reference to a temporary here, but that
1284 is now handled by the recursive call to this function at the end
1285 of reference_binding. */
1291 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1294 static struct z_candidate
*
1295 add_candidate (struct z_candidate
*candidates
,
1296 tree fn
, tree convs
, tree access_path
, tree
1297 conversion_path
, int viable
)
1299 struct z_candidate
*cand
1300 = (struct z_candidate
*) ggc_alloc_cleared (sizeof (struct z_candidate
));
1303 cand
->convs
= convs
;
1304 cand
->access_path
= access_path
;
1305 cand
->conversion_path
= conversion_path
;
1306 cand
->viable
= viable
;
1307 cand
->next
= candidates
;
1312 /* Create an overload candidate for the function or method FN called with
1313 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1314 to implicit_conversion.
1316 CTYPE, if non-NULL, is the type we want to pretend this function
1317 comes from for purposes of overload resolution. */
1319 static struct z_candidate
*
1320 add_function_candidate (struct z_candidate
*candidates
,
1321 tree fn
, tree ctype
, tree arglist
,
1322 tree access_path
, tree conversion_path
,
1325 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1328 tree parmnode
, argnode
;
1331 /* The `this', `in_chrg' and VTT arguments to constructors are not
1332 considered in overload resolution. */
1333 if (DECL_CONSTRUCTOR_P (fn
))
1335 parmlist
= skip_artificial_parms_for (fn
, parmlist
);
1336 arglist
= skip_artificial_parms_for (fn
, arglist
);
1339 len
= list_length (arglist
);
1340 convs
= make_tree_vec (len
);
1342 /* 13.3.2 - Viable functions [over.match.viable]
1343 First, to be a viable function, a candidate function shall have enough
1344 parameters to agree in number with the arguments in the list.
1346 We need to check this first; otherwise, checking the ICSes might cause
1347 us to produce an ill-formed template instantiation. */
1349 parmnode
= parmlist
;
1350 for (i
= 0; i
< len
; ++i
)
1352 if (parmnode
== NULL_TREE
|| parmnode
== void_list_node
)
1354 parmnode
= TREE_CHAIN (parmnode
);
1357 if (i
< len
&& parmnode
)
1360 /* Make sure there are default args for the rest of the parms. */
1361 else if (!sufficient_parms_p (parmnode
))
1367 /* Second, for F to be a viable function, there shall exist for each
1368 argument an implicit conversion sequence that converts that argument
1369 to the corresponding parameter of F. */
1371 parmnode
= parmlist
;
1374 for (i
= 0; i
< len
; ++i
)
1376 tree arg
= TREE_VALUE (argnode
);
1377 tree argtype
= lvalue_type (arg
);
1381 if (parmnode
== void_list_node
)
1384 is_this
= (i
== 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1385 && ! DECL_CONSTRUCTOR_P (fn
));
1389 tree parmtype
= TREE_VALUE (parmnode
);
1391 /* The type of the implicit object parameter ('this') for
1392 overload resolution is not always the same as for the
1393 function itself; conversion functions are considered to
1394 be members of the class being converted, and functions
1395 introduced by a using-declaration are considered to be
1396 members of the class that uses them.
1398 Since build_over_call ignores the ICS for the `this'
1399 parameter, we can just change the parm type. */
1400 if (ctype
&& is_this
)
1403 = build_qualified_type (ctype
,
1404 TYPE_QUALS (TREE_TYPE (parmtype
)));
1405 parmtype
= build_pointer_type (parmtype
);
1408 t
= implicit_conversion (parmtype
, argtype
, arg
, flags
);
1412 t
= build1 (IDENTITY_CONV
, argtype
, arg
);
1413 ICS_ELLIPSIS_FLAG (t
) = 1;
1417 ICS_THIS_FLAG (t
) = 1;
1419 TREE_VEC_ELT (convs
, i
) = t
;
1426 if (ICS_BAD_FLAG (t
))
1430 parmnode
= TREE_CHAIN (parmnode
);
1431 argnode
= TREE_CHAIN (argnode
);
1435 return add_candidate (candidates
, fn
, convs
, access_path
,
1436 conversion_path
, viable
);
1439 /* Create an overload candidate for the conversion function FN which will
1440 be invoked for expression OBJ, producing a pointer-to-function which
1441 will in turn be called with the argument list ARGLIST, and add it to
1442 CANDIDATES. FLAGS is passed on to implicit_conversion.
1444 Actually, we don't really care about FN; we care about the type it
1445 converts to. There may be multiple conversion functions that will
1446 convert to that type, and we rely on build_user_type_conversion_1 to
1447 choose the best one; so when we create our candidate, we record the type
1448 instead of the function. */
1450 static struct z_candidate
*
1451 add_conv_candidate (struct z_candidate
*candidates
, tree fn
, tree obj
,
1452 tree arglist
, tree access_path
, tree conversion_path
)
1454 tree totype
= TREE_TYPE (TREE_TYPE (fn
));
1455 int i
, len
, viable
, flags
;
1456 tree parmlist
, convs
, parmnode
, argnode
;
1458 for (parmlist
= totype
; TREE_CODE (parmlist
) != FUNCTION_TYPE
; )
1459 parmlist
= TREE_TYPE (parmlist
);
1460 parmlist
= TYPE_ARG_TYPES (parmlist
);
1462 len
= list_length (arglist
) + 1;
1463 convs
= make_tree_vec (len
);
1464 parmnode
= parmlist
;
1467 flags
= LOOKUP_NORMAL
;
1469 /* Don't bother looking up the same type twice. */
1470 if (candidates
&& candidates
->fn
== totype
)
1473 for (i
= 0; i
< len
; ++i
)
1475 tree arg
= i
== 0 ? obj
: TREE_VALUE (argnode
);
1476 tree argtype
= lvalue_type (arg
);
1480 t
= implicit_conversion (totype
, argtype
, arg
, flags
);
1481 else if (parmnode
== void_list_node
)
1484 t
= implicit_conversion (TREE_VALUE (parmnode
), argtype
, arg
, flags
);
1487 t
= build1 (IDENTITY_CONV
, argtype
, arg
);
1488 ICS_ELLIPSIS_FLAG (t
) = 1;
1491 TREE_VEC_ELT (convs
, i
) = t
;
1495 if (ICS_BAD_FLAG (t
))
1502 parmnode
= TREE_CHAIN (parmnode
);
1503 argnode
= TREE_CHAIN (argnode
);
1509 if (!sufficient_parms_p (parmnode
))
1512 return add_candidate (candidates
, totype
, convs
, access_path
,
1513 conversion_path
, viable
);
1516 static struct z_candidate
*
1517 build_builtin_candidate (struct z_candidate
*candidates
, tree fnname
,
1518 tree type1
, tree type2
, tree
*args
, tree
*argtypes
,
1528 convs
= make_tree_vec (args
[2] ? 3 : (args
[1] ? 2 : 1));
1530 for (i
= 0; i
< 2; ++i
)
1535 t
= implicit_conversion (types
[i
], argtypes
[i
], args
[i
], flags
);
1539 /* We need something for printing the candidate. */
1540 t
= build1 (IDENTITY_CONV
, types
[i
], NULL_TREE
);
1542 else if (ICS_BAD_FLAG (t
))
1544 TREE_VEC_ELT (convs
, i
) = t
;
1547 /* For COND_EXPR we rearranged the arguments; undo that now. */
1550 TREE_VEC_ELT (convs
, 2) = TREE_VEC_ELT (convs
, 1);
1551 TREE_VEC_ELT (convs
, 1) = TREE_VEC_ELT (convs
, 0);
1552 t
= implicit_conversion (boolean_type_node
, argtypes
[2], args
[2], flags
);
1554 TREE_VEC_ELT (convs
, 0) = t
;
1559 return add_candidate (candidates
, fnname
, convs
,
1560 /*access_path=*/NULL_TREE
,
1561 /*conversion_path=*/NULL_TREE
,
1566 is_complete (tree t
)
1568 return COMPLETE_TYPE_P (complete_type (t
));
1571 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1574 promoted_arithmetic_type_p (tree type
)
1578 In this section, the term promoted integral type is used to refer
1579 to those integral types which are preserved by integral promotion
1580 (including e.g. int and long but excluding e.g. char).
1581 Similarly, the term promoted arithmetic type refers to promoted
1582 integral types plus floating types. */
1583 return ((INTEGRAL_TYPE_P (type
)
1584 && same_type_p (type_promotes_to (type
), type
))
1585 || TREE_CODE (type
) == REAL_TYPE
);
1588 /* Create any builtin operator overload candidates for the operator in
1589 question given the converted operand types TYPE1 and TYPE2. The other
1590 args are passed through from add_builtin_candidates to
1591 build_builtin_candidate.
1593 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1594 If CODE is requires candidates operands of the same type of the kind
1595 of which TYPE1 and TYPE2 are, we add both candidates
1596 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1598 static struct z_candidate
*
1599 add_builtin_candidate (struct z_candidate
*candidates
, enum tree_code code
,
1600 enum tree_code code2
, tree fnname
, tree type1
,
1601 tree type2
, tree
*args
, tree
*argtypes
, int flags
)
1605 case POSTINCREMENT_EXPR
:
1606 case POSTDECREMENT_EXPR
:
1607 args
[1] = integer_zero_node
;
1608 type2
= integer_type_node
;
1617 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1618 and VQ is either volatile or empty, there exist candidate operator
1619 functions of the form
1620 VQ T& operator++(VQ T&);
1621 T operator++(VQ T&, int);
1622 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1623 type other than bool, and VQ is either volatile or empty, there exist
1624 candidate operator functions of the form
1625 VQ T& operator--(VQ T&);
1626 T operator--(VQ T&, int);
1627 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1628 complete object type, and VQ is either volatile or empty, there exist
1629 candidate operator functions of the form
1630 T*VQ& operator++(T*VQ&);
1631 T*VQ& operator--(T*VQ&);
1632 T* operator++(T*VQ&, int);
1633 T* operator--(T*VQ&, int); */
1635 case POSTDECREMENT_EXPR
:
1636 case PREDECREMENT_EXPR
:
1637 if (TREE_CODE (type1
) == BOOLEAN_TYPE
)
1639 case POSTINCREMENT_EXPR
:
1640 case PREINCREMENT_EXPR
:
1641 if (ARITHMETIC_TYPE_P (type1
) || TYPE_PTROB_P (type1
))
1643 type1
= build_reference_type (type1
);
1648 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1649 exist candidate operator functions of the form
1653 8 For every function type T, there exist candidate operator functions of
1655 T& operator*(T*); */
1658 if (TREE_CODE (type1
) == POINTER_TYPE
1659 && (TYPE_PTROB_P (type1
)
1660 || TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
))
1664 /* 9 For every type T, there exist candidate operator functions of the form
1667 10For every promoted arithmetic type T, there exist candidate operator
1668 functions of the form
1672 case CONVERT_EXPR
: /* unary + */
1673 if (TREE_CODE (type1
) == POINTER_TYPE
1674 && TREE_CODE (TREE_TYPE (type1
)) != OFFSET_TYPE
)
1677 if (ARITHMETIC_TYPE_P (type1
))
1681 /* 11For every promoted integral type T, there exist candidate operator
1682 functions of the form
1686 if (INTEGRAL_TYPE_P (type1
))
1690 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1691 is the same type as C2 or is a derived class of C2, T is a complete
1692 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1693 there exist candidate operator functions of the form
1694 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1695 where CV12 is the union of CV1 and CV2. */
1698 if (TREE_CODE (type1
) == POINTER_TYPE
1699 && (TYPE_PTRMEMFUNC_P (type2
) || TYPE_PTRMEM_P (type2
)))
1701 tree c1
= TREE_TYPE (type1
);
1702 tree c2
= (TYPE_PTRMEMFUNC_P (type2
)
1703 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2
)))
1704 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2
)));
1706 if (IS_AGGR_TYPE (c1
) && DERIVED_FROM_P (c2
, c1
)
1707 && (TYPE_PTRMEMFUNC_P (type2
)
1708 || is_complete (TREE_TYPE (TREE_TYPE (type2
)))))
1713 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1714 didate operator functions of the form
1719 bool operator<(L, R);
1720 bool operator>(L, R);
1721 bool operator<=(L, R);
1722 bool operator>=(L, R);
1723 bool operator==(L, R);
1724 bool operator!=(L, R);
1725 where LR is the result of the usual arithmetic conversions between
1728 14For every pair of types T and I, where T is a cv-qualified or cv-
1729 unqualified complete object type and I is a promoted integral type,
1730 there exist candidate operator functions of the form
1731 T* operator+(T*, I);
1732 T& operator[](T*, I);
1733 T* operator-(T*, I);
1734 T* operator+(I, T*);
1735 T& operator[](I, T*);
1737 15For every T, where T is a pointer to complete object type, there exist
1738 candidate operator functions of the form112)
1739 ptrdiff_t operator-(T, T);
1741 16For every pointer or enumeration type T, there exist candidate operator
1742 functions of the form
1743 bool operator<(T, T);
1744 bool operator>(T, T);
1745 bool operator<=(T, T);
1746 bool operator>=(T, T);
1747 bool operator==(T, T);
1748 bool operator!=(T, T);
1750 17For every pointer to member type T, there exist candidate operator
1751 functions of the form
1752 bool operator==(T, T);
1753 bool operator!=(T, T); */
1756 if (TYPE_PTROB_P (type1
) && TYPE_PTROB_P (type2
))
1758 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1760 type2
= ptrdiff_type_node
;
1764 case TRUNC_DIV_EXPR
:
1765 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1771 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
1772 || (TYPE_PTRMEM_P (type1
) && TYPE_PTRMEM_P (type2
)))
1774 if ((TYPE_PTRMEMFUNC_P (type1
) || TYPE_PTRMEM_P (type1
))
1775 && null_ptr_cst_p (args
[1]))
1780 if ((TYPE_PTRMEMFUNC_P (type2
) || TYPE_PTRMEM_P (type2
))
1781 && null_ptr_cst_p (args
[0]))
1793 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1795 if (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
1797 if (TREE_CODE (type1
) == ENUMERAL_TYPE
&& TREE_CODE (type2
) == ENUMERAL_TYPE
)
1799 if (TYPE_PTR_P (type1
) && null_ptr_cst_p (args
[1]))
1804 if (null_ptr_cst_p (args
[0]) && TYPE_PTR_P (type2
))
1812 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1815 if (INTEGRAL_TYPE_P (type1
) && TYPE_PTROB_P (type2
))
1817 type1
= ptrdiff_type_node
;
1820 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1822 type2
= ptrdiff_type_node
;
1827 /* 18For every pair of promoted integral types L and R, there exist candi-
1828 date operator functions of the form
1835 where LR is the result of the usual arithmetic conversions between
1838 case TRUNC_MOD_EXPR
:
1844 if (INTEGRAL_TYPE_P (type1
) && INTEGRAL_TYPE_P (type2
))
1848 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1849 type, VQ is either volatile or empty, and R is a promoted arithmetic
1850 type, there exist candidate operator functions of the form
1851 VQ L& operator=(VQ L&, R);
1852 VQ L& operator*=(VQ L&, R);
1853 VQ L& operator/=(VQ L&, R);
1854 VQ L& operator+=(VQ L&, R);
1855 VQ L& operator-=(VQ L&, R);
1857 20For every pair T, VQ), where T is any type and VQ is either volatile
1858 or empty, there exist candidate operator functions of the form
1859 T*VQ& operator=(T*VQ&, T*);
1861 21For every pair T, VQ), where T is a pointer to member type and VQ is
1862 either volatile or empty, there exist candidate operator functions of
1864 VQ T& operator=(VQ T&, T);
1866 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1867 unqualified complete object type, VQ is either volatile or empty, and
1868 I is a promoted integral type, there exist candidate operator func-
1870 T*VQ& operator+=(T*VQ&, I);
1871 T*VQ& operator-=(T*VQ&, I);
1873 23For every triple L, VQ, R), where L is an integral or enumeration
1874 type, VQ is either volatile or empty, and R is a promoted integral
1875 type, there exist candidate operator functions of the form
1877 VQ L& operator%=(VQ L&, R);
1878 VQ L& operator<<=(VQ L&, R);
1879 VQ L& operator>>=(VQ L&, R);
1880 VQ L& operator&=(VQ L&, R);
1881 VQ L& operator^=(VQ L&, R);
1882 VQ L& operator|=(VQ L&, R); */
1889 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1891 type2
= ptrdiff_type_node
;
1895 case TRUNC_DIV_EXPR
:
1896 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1900 case TRUNC_MOD_EXPR
:
1906 if (INTEGRAL_TYPE_P (type1
) && INTEGRAL_TYPE_P (type2
))
1911 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1913 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
1914 || (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
1915 || (TYPE_PTRMEM_P (type1
) && TYPE_PTRMEM_P (type2
))
1916 || ((TYPE_PTRMEMFUNC_P (type1
)
1917 || TREE_CODE (type1
) == POINTER_TYPE
)
1918 && null_ptr_cst_p (args
[1])))
1928 type1
= build_reference_type (type1
);
1934 For every pair of promoted arithmetic types L and R, there
1935 exist candidate operator functions of the form
1937 LR operator?(bool, L, R);
1939 where LR is the result of the usual arithmetic conversions
1940 between types L and R.
1942 For every type T, where T is a pointer or pointer-to-member
1943 type, there exist candidate operator functions of the form T
1944 operator?(bool, T, T); */
1946 if (promoted_arithmetic_type_p (type1
)
1947 && promoted_arithmetic_type_p (type2
))
1951 /* Otherwise, the types should be pointers. */
1952 if (!(TREE_CODE (type1
) == POINTER_TYPE
1953 || TYPE_PTRMEM_P (type1
)
1954 || TYPE_PTRMEMFUNC_P (type1
))
1955 || !(TREE_CODE (type2
) == POINTER_TYPE
1956 || TYPE_PTRMEM_P (type2
)
1957 || TYPE_PTRMEMFUNC_P (type2
)))
1960 /* We don't check that the two types are the same; the logic
1961 below will actually create two candidates; one in which both
1962 parameter types are TYPE1, and one in which both parameter
1966 /* These arguments do not make for a valid overloaded operator. */
1973 /* If we're dealing with two pointer types or two enumeral types,
1974 we need candidates for both of them. */
1975 if (type2
&& !same_type_p (type1
, type2
)
1976 && TREE_CODE (type1
) == TREE_CODE (type2
)
1977 && (TREE_CODE (type1
) == REFERENCE_TYPE
1978 || (TREE_CODE (type1
) == POINTER_TYPE
1979 && TYPE_PTRMEM_P (type1
) == TYPE_PTRMEM_P (type2
))
1980 || TYPE_PTRMEMFUNC_P (type1
)
1981 || IS_AGGR_TYPE (type1
)
1982 || TREE_CODE (type1
) == ENUMERAL_TYPE
))
1984 candidates
= build_builtin_candidate
1985 (candidates
, fnname
, type1
, type1
, args
, argtypes
, flags
);
1986 return build_builtin_candidate
1987 (candidates
, fnname
, type2
, type2
, args
, argtypes
, flags
);
1990 return build_builtin_candidate
1991 (candidates
, fnname
, type1
, type2
, args
, argtypes
, flags
);
1995 type_decays_to (tree type
)
1997 if (TREE_CODE (type
) == ARRAY_TYPE
)
1998 return build_pointer_type (TREE_TYPE (type
));
1999 if (TREE_CODE (type
) == FUNCTION_TYPE
)
2000 return build_pointer_type (type
);
2004 /* There are three conditions of builtin candidates:
2006 1) bool-taking candidates. These are the same regardless of the input.
2007 2) pointer-pair taking candidates. These are generated for each type
2008 one of the input types converts to.
2009 3) arithmetic candidates. According to the standard, we should generate
2010 all of these, but I'm trying not to...
2012 Here we generate a superset of the possible candidates for this particular
2013 case. That is a subset of the full set the standard defines, plus some
2014 other cases which the standard disallows. add_builtin_candidate will
2015 filter out the invalid set. */
2017 static struct z_candidate
*
2018 add_builtin_candidates (struct z_candidate
*candidates
, enum tree_code code
,
2019 enum tree_code code2
, tree fnname
, tree
*args
,
2024 tree type
, argtypes
[3];
2025 /* TYPES[i] is the set of possible builtin-operator parameter types
2026 we will consider for the Ith argument. These are represented as
2027 a TREE_LIST; the TREE_VALUE of each node is the potential
2031 for (i
= 0; i
< 3; ++i
)
2034 argtypes
[i
] = lvalue_type (args
[i
]);
2036 argtypes
[i
] = NULL_TREE
;
2041 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2042 and VQ is either volatile or empty, there exist candidate operator
2043 functions of the form
2044 VQ T& operator++(VQ T&); */
2046 case POSTINCREMENT_EXPR
:
2047 case PREINCREMENT_EXPR
:
2048 case POSTDECREMENT_EXPR
:
2049 case PREDECREMENT_EXPR
:
2054 /* 24There also exist candidate operator functions of the form
2055 bool operator!(bool);
2056 bool operator&&(bool, bool);
2057 bool operator||(bool, bool); */
2059 case TRUTH_NOT_EXPR
:
2060 return build_builtin_candidate
2061 (candidates
, fnname
, boolean_type_node
,
2062 NULL_TREE
, args
, argtypes
, flags
);
2064 case TRUTH_ORIF_EXPR
:
2065 case TRUTH_ANDIF_EXPR
:
2066 return build_builtin_candidate
2067 (candidates
, fnname
, boolean_type_node
,
2068 boolean_type_node
, args
, argtypes
, flags
);
2089 types
[0] = types
[1] = NULL_TREE
;
2091 for (i
= 0; i
< 2; ++i
)
2095 else if (IS_AGGR_TYPE (argtypes
[i
]))
2099 if (i
== 0 && code
== MODIFY_EXPR
&& code2
== NOP_EXPR
)
2102 convs
= lookup_conversions (argtypes
[i
]);
2104 if (code
== COND_EXPR
)
2106 if (real_lvalue_p (args
[i
]))
2107 types
[i
] = tree_cons
2108 (NULL_TREE
, build_reference_type (argtypes
[i
]), types
[i
]);
2110 types
[i
] = tree_cons
2111 (NULL_TREE
, TYPE_MAIN_VARIANT (argtypes
[i
]), types
[i
]);
2117 for (; convs
; convs
= TREE_CHAIN (convs
))
2119 type
= TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs
))));
2122 && (TREE_CODE (type
) != REFERENCE_TYPE
2123 || CP_TYPE_CONST_P (TREE_TYPE (type
))))
2126 if (code
== COND_EXPR
&& TREE_CODE (type
) == REFERENCE_TYPE
)
2127 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2129 type
= non_reference (type
);
2130 if (i
!= 0 || ! ref1
)
2132 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
2133 if (enum_p
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
2134 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2135 if (INTEGRAL_TYPE_P (type
))
2136 type
= type_promotes_to (type
);
2139 if (! value_member (type
, types
[i
]))
2140 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2145 if (code
== COND_EXPR
&& real_lvalue_p (args
[i
]))
2146 types
[i
] = tree_cons
2147 (NULL_TREE
, build_reference_type (argtypes
[i
]), types
[i
]);
2148 type
= non_reference (argtypes
[i
]);
2149 if (i
!= 0 || ! ref1
)
2151 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
2152 if (enum_p
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
2153 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2154 if (INTEGRAL_TYPE_P (type
))
2155 type
= type_promotes_to (type
);
2157 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2161 /* Run through the possible parameter types of both arguments,
2162 creating candidates with those parameter types. */
2163 for (; types
[0]; types
[0] = TREE_CHAIN (types
[0]))
2166 for (type
= types
[1]; type
; type
= TREE_CHAIN (type
))
2167 candidates
= add_builtin_candidate
2168 (candidates
, code
, code2
, fnname
, TREE_VALUE (types
[0]),
2169 TREE_VALUE (type
), args
, argtypes
, flags
);
2171 candidates
= add_builtin_candidate
2172 (candidates
, code
, code2
, fnname
, TREE_VALUE (types
[0]),
2173 NULL_TREE
, args
, argtypes
, flags
);
2180 /* If TMPL can be successfully instantiated as indicated by
2181 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2183 TMPL is the template. EXPLICIT_TARGS are any explicit template
2184 arguments. ARGLIST is the arguments provided at the call-site.
2185 The RETURN_TYPE is the desired type for conversion operators. If
2186 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2187 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2188 add_conv_candidate. */
2190 static struct z_candidate
*
2191 add_template_candidate_real (struct z_candidate
*candidates
, tree tmpl
,
2192 tree ctype
, tree explicit_targs
, tree arglist
,
2193 tree return_type
, tree access_path
,
2194 tree conversion_path
, int flags
, tree obj
,
2195 unification_kind_t strict
)
2197 int ntparms
= DECL_NTPARMS (tmpl
);
2198 tree targs
= make_tree_vec (ntparms
);
2199 tree args_without_in_chrg
= arglist
;
2200 struct z_candidate
*cand
;
2204 /* We don't do deduction on the in-charge parameter, the VTT
2205 parameter or 'this'. */
2206 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl
))
2207 args_without_in_chrg
= TREE_CHAIN (args_without_in_chrg
);
2209 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl
)
2210 || DECL_BASE_CONSTRUCTOR_P (tmpl
))
2211 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl
)))
2212 args_without_in_chrg
= TREE_CHAIN (args_without_in_chrg
);
2214 i
= fn_type_unification (tmpl
, explicit_targs
, targs
,
2215 args_without_in_chrg
,
2216 return_type
, strict
, -1);
2221 fn
= instantiate_template (tmpl
, targs
);
2222 if (fn
== error_mark_node
)
2227 A member function template is never instantiated to perform the
2228 copy of a class object to an object of its class type.
2230 It's a little unclear what this means; the standard explicitly
2231 does allow a template to be used to copy a class. For example,
2236 template <class T> A(const T&);
2239 void g () { A a (f ()); }
2241 the member template will be used to make the copy. The section
2242 quoted above appears in the paragraph that forbids constructors
2243 whose only parameter is (a possibly cv-qualified variant of) the
2244 class type, and a logical interpretation is that the intent was
2245 to forbid the instantiation of member templates which would then
2247 if (DECL_CONSTRUCTOR_P (fn
) && list_length (arglist
) == 2)
2249 tree arg_types
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
2250 if (arg_types
&& same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types
)),
2255 if (obj
!= NULL_TREE
)
2256 /* Aha, this is a conversion function. */
2257 cand
= add_conv_candidate (candidates
, fn
, obj
, access_path
,
2258 conversion_path
, arglist
);
2260 cand
= add_function_candidate (candidates
, fn
, ctype
,
2261 arglist
, access_path
,
2262 conversion_path
, flags
);
2263 if (DECL_TI_TEMPLATE (fn
) != tmpl
)
2264 /* This situation can occur if a member template of a template
2265 class is specialized. Then, instantiate_template might return
2266 an instantiation of the specialization, in which case the
2267 DECL_TI_TEMPLATE field will point at the original
2268 specialization. For example:
2270 template <class T> struct S { template <class U> void f(U);
2271 template <> void f(int) {}; };
2275 Here, TMPL will be template <class U> S<double>::f(U).
2276 And, instantiate template will give us the specialization
2277 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2278 for this will point at template <class T> template <> S<T>::f(int),
2279 so that we can find the definition. For the purposes of
2280 overload resolution, however, we want the original TMPL. */
2281 cand
->template = tree_cons (tmpl
, targs
, NULL_TREE
);
2283 cand
->template = DECL_TEMPLATE_INFO (fn
);
2289 static struct z_candidate
*
2290 add_template_candidate (struct z_candidate
*candidates
, tree tmpl
, tree ctype
,
2291 tree explicit_targs
, tree arglist
, tree return_type
,
2292 tree access_path
, tree conversion_path
, int flags
,
2293 unification_kind_t strict
)
2296 add_template_candidate_real (candidates
, tmpl
, ctype
,
2297 explicit_targs
, arglist
, return_type
,
2298 access_path
, conversion_path
,
2299 flags
, NULL_TREE
, strict
);
2303 static struct z_candidate
*
2304 add_template_conv_candidate (struct z_candidate
*candidates
, tree tmpl
,
2305 tree obj
, tree arglist
, tree return_type
,
2306 tree access_path
, tree conversion_path
)
2309 add_template_candidate_real (candidates
, tmpl
, NULL_TREE
, NULL_TREE
,
2310 arglist
, return_type
, access_path
,
2311 conversion_path
, 0, obj
, DEDUCE_CONV
);
2316 any_viable (struct z_candidate
*cands
)
2318 for (; cands
; cands
= cands
->next
)
2319 if (pedantic
? cands
->viable
== 1 : cands
->viable
)
2325 any_strictly_viable (struct z_candidate
*cands
)
2327 for (; cands
; cands
= cands
->next
)
2328 if (cands
->viable
== 1)
2333 static struct z_candidate
*
2334 splice_viable (struct z_candidate
*cands
)
2336 struct z_candidate
**p
= &cands
;
2340 if (pedantic
? (*p
)->viable
== 1 : (*p
)->viable
)
2350 build_this (tree obj
)
2352 /* Fix this to work on non-lvalues. */
2353 return build_unary_op (ADDR_EXPR
, obj
, 0);
2357 print_z_candidates (struct z_candidate
*candidates
)
2359 const char *str
= "candidates are:";
2360 for (; candidates
; candidates
= candidates
->next
)
2362 if (TREE_CODE (candidates
->fn
) == IDENTIFIER_NODE
)
2364 if (TREE_VEC_LENGTH (candidates
->convs
) == 3)
2365 error ("%s %D(%T, %T, %T) <built-in>", str
, candidates
->fn
,
2366 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)),
2367 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 1)),
2368 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 2)));
2369 else if (TREE_VEC_LENGTH (candidates
->convs
) == 2)
2370 error ("%s %D(%T, %T) <built-in>", str
, candidates
->fn
,
2371 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)),
2372 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 1)));
2374 error ("%s %D(%T) <built-in>", str
, candidates
->fn
,
2375 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)));
2377 else if (TYPE_P (candidates
->fn
))
2378 error ("%s %T <conversion>", str
, candidates
->fn
);
2380 cp_error_at ("%s %+#D%s", str
, candidates
->fn
,
2381 candidates
->viable
== -1 ? " <near match>" : "");
2386 /* Returns the best overload candidate to perform the requested
2387 conversion. This function is used for three the overloading situations
2388 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2389 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2390 per [dcl.init.ref], so we ignore temporary bindings. */
2392 static struct z_candidate
*
2393 build_user_type_conversion_1 (tree totype
, tree expr
, int flags
)
2395 struct z_candidate
*candidates
, *cand
;
2396 tree fromtype
= TREE_TYPE (expr
);
2397 tree ctors
= NULL_TREE
, convs
= NULL_TREE
, *p
;
2398 tree args
= NULL_TREE
;
2399 tree templates
= NULL_TREE
;
2401 /* We represent conversion within a hierarchy using RVALUE_CONV and
2402 BASE_CONV, as specified by [over.best.ics]; these become plain
2403 constructor calls, as specified in [dcl.init]. */
2404 my_friendly_assert (!IS_AGGR_TYPE (fromtype
) || !IS_AGGR_TYPE (totype
)
2405 || !DERIVED_FROM_P (totype
, fromtype
), 20011226);
2407 if (IS_AGGR_TYPE (totype
))
2408 ctors
= lookup_fnfields (TYPE_BINFO (totype
),
2409 complete_ctor_identifier
,
2412 if (IS_AGGR_TYPE (fromtype
))
2413 convs
= lookup_conversions (fromtype
);
2416 flags
|= LOOKUP_NO_CONVERSION
;
2422 ctors
= BASELINK_FUNCTIONS (ctors
);
2424 t
= build_int_2 (0, 0);
2425 TREE_TYPE (t
) = build_pointer_type (totype
);
2426 args
= build_tree_list (NULL_TREE
, expr
);
2427 /* We should never try to call the abstract or base constructor
2429 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors
))
2430 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors
)),
2432 args
= tree_cons (NULL_TREE
, t
, args
);
2434 for (; ctors
; ctors
= OVL_NEXT (ctors
))
2436 tree ctor
= OVL_CURRENT (ctors
);
2437 if (DECL_NONCONVERTING_P (ctor
))
2440 if (TREE_CODE (ctor
) == TEMPLATE_DECL
)
2442 templates
= tree_cons (NULL_TREE
, ctor
, templates
);
2444 add_template_candidate (candidates
, ctor
, totype
,
2445 NULL_TREE
, args
, NULL_TREE
,
2446 TYPE_BINFO (totype
),
2447 TYPE_BINFO (totype
),
2452 candidates
= add_function_candidate (candidates
, ctor
, totype
,
2453 args
, TYPE_BINFO (totype
),
2454 TYPE_BINFO (totype
),
2458 candidates
->second_conv
= build1 (IDENTITY_CONV
, totype
, NULL_TREE
);
2462 args
= build_tree_list (NULL_TREE
, build_this (expr
));
2464 for (; convs
; convs
= TREE_CHAIN (convs
))
2467 tree conversion_path
= TREE_PURPOSE (convs
);
2468 int convflags
= LOOKUP_NO_CONVERSION
;
2470 /* If we are called to convert to a reference type, we are trying to
2471 find an lvalue binding, so don't even consider temporaries. If
2472 we don't find an lvalue binding, the caller will try again to
2473 look for a temporary binding. */
2474 if (TREE_CODE (totype
) == REFERENCE_TYPE
)
2475 convflags
|= LOOKUP_NO_TEMP_BIND
;
2477 for (fns
= TREE_VALUE (convs
); fns
; fns
= OVL_NEXT (fns
))
2479 tree fn
= OVL_CURRENT (fns
);
2480 struct z_candidate
*old_candidates
= candidates
;
2482 /* [over.match.funcs] For conversion functions, the function
2483 is considered to be a member of the class of the implicit
2484 object argument for the purpose of defining the type of
2485 the implicit object parameter.
2487 So we pass fromtype as CTYPE to add_*_candidate. */
2489 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2491 templates
= tree_cons (NULL_TREE
, fn
, templates
);
2493 add_template_candidate (candidates
, fn
, fromtype
, NULL_TREE
,
2494 args
, totype
, TYPE_BINFO (fromtype
),
2500 candidates
= add_function_candidate (candidates
, fn
, fromtype
,
2502 TYPE_BINFO (fromtype
),
2506 if (candidates
!= old_candidates
)
2508 tree ics
= implicit_conversion
2509 (totype
, TREE_TYPE (TREE_TYPE (candidates
->fn
)),
2512 candidates
->second_conv
= ics
;
2514 if (ics
== NULL_TREE
)
2515 candidates
->viable
= 0;
2516 else if (candidates
->viable
== 1 && ICS_BAD_FLAG (ics
))
2517 candidates
->viable
= -1;
2522 if (! any_viable (candidates
))
2525 candidates
= splice_viable (candidates
);
2526 cand
= tourney (candidates
);
2530 if (flags
& LOOKUP_COMPLAIN
)
2532 error ("conversion from `%T' to `%T' is ambiguous",
2534 print_z_candidates (candidates
);
2537 cand
= candidates
; /* any one will do */
2538 cand
->second_conv
= build1 (AMBIG_CONV
, totype
, expr
);
2539 ICS_USER_FLAG (cand
->second_conv
) = 1;
2540 /* Don't set ICS_BAD_FLAG; an ambiguous conversion is no worse than
2541 another user-defined conversion. */
2546 for (p
= &(cand
->second_conv
); TREE_CODE (*p
) != IDENTITY_CONV
; )
2547 p
= &(TREE_OPERAND (*p
, 0));
2551 (DECL_CONSTRUCTOR_P (cand
->fn
)
2552 ? totype
: non_reference (TREE_TYPE (TREE_TYPE (cand
->fn
)))),
2553 expr
, build_zc_wrapper (cand
));
2555 ICS_USER_FLAG (cand
->second_conv
) = ICS_USER_FLAG (*p
) = 1;
2556 if (cand
->viable
== -1)
2557 ICS_BAD_FLAG (cand
->second_conv
) = ICS_BAD_FLAG (*p
) = 1;
2563 build_user_type_conversion (tree totype
, tree expr
, int flags
)
2565 struct z_candidate
*cand
2566 = build_user_type_conversion_1 (totype
, expr
, flags
);
2570 if (TREE_CODE (cand
->second_conv
) == AMBIG_CONV
)
2571 return error_mark_node
;
2572 return convert_from_reference (convert_like (cand
->second_conv
, expr
));
2577 /* Find the possibly overloaded set of functions corresponding to a
2578 call of the form SCOPE::NAME (...). NAME might be a
2579 TEMPLATE_ID_EXPR, OVERLOAD, _DECL, IDENTIFIER_NODE or LOOKUP_EXPR. */
2582 resolve_scoped_fn_name (tree scope
, tree name
)
2585 tree template_args
= NULL_TREE
;
2586 bool is_template_id
= TREE_CODE (name
) == TEMPLATE_ID_EXPR
;
2590 template_args
= TREE_OPERAND (name
, 1);
2591 name
= TREE_OPERAND (name
, 0);
2593 if (TREE_CODE (name
) == OVERLOAD
)
2594 name
= DECL_NAME (get_first_fn (name
));
2595 else if (TREE_CODE (name
) == LOOKUP_EXPR
)
2596 name
= TREE_OPERAND (name
, 0);
2598 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2599 fn
= lookup_namespace_name (scope
, name
);
2602 if (!TYPE_BEING_DEFINED (scope
)
2603 && !COMPLETE_TYPE_P (complete_type (scope
)))
2605 error ("incomplete type '%T' cannot be used to name a scope",
2607 return error_mark_node
;
2610 if (BASELINK_P (name
))
2613 fn
= lookup_member (scope
, name
, /*protect=*/1, /*prefer_type=*/0);
2614 if (fn
&& current_class_type
)
2615 fn
= (adjust_result_of_qualified_name_lookup
2616 (fn
, scope
, current_class_type
));
2621 error ("'%D' has no member named '%E'", scope
, name
);
2622 return error_mark_node
;
2628 if (BASELINK_P (fn
))
2629 fns
= BASELINK_FUNCTIONS (fns
);
2630 fns
= build_nt (TEMPLATE_ID_EXPR
, fns
, template_args
);
2631 if (BASELINK_P (fn
))
2632 BASELINK_FUNCTIONS (fn
) = fns
;
2640 /* Do any initial processing on the arguments to a function call. */
2643 resolve_args (tree args
)
2646 for (t
= args
; t
; t
= TREE_CHAIN (t
))
2648 tree arg
= TREE_VALUE (t
);
2650 if (arg
== error_mark_node
)
2651 return error_mark_node
;
2652 else if (VOID_TYPE_P (TREE_TYPE (arg
)))
2654 error ("invalid use of void expression");
2655 return error_mark_node
;
2657 else if (TREE_CODE (arg
) == OFFSET_REF
)
2658 arg
= resolve_offset_ref (arg
);
2659 arg
= convert_from_reference (arg
);
2660 TREE_VALUE (t
) = arg
;
2665 /* Return an expression for a call to FN (a namespace-scope function,
2666 or a static member function) with the ARGS. */
2669 build_new_function_call (tree fn
, tree args
)
2671 struct z_candidate
*candidates
= 0, *cand
;
2672 tree explicit_targs
= NULL_TREE
;
2673 int template_only
= 0;
2675 /* Check FN and ARGS. */
2676 my_friendly_assert (TREE_CODE (fn
) == FUNCTION_DECL
2677 || TREE_CODE (fn
) == TEMPLATE_DECL
2678 || TREE_CODE (fn
) == OVERLOAD
2679 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
,
2681 my_friendly_assert (!args
|| TREE_CODE (args
) == TREE_LIST
,
2684 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
2686 explicit_targs
= TREE_OPERAND (fn
, 1);
2687 fn
= TREE_OPERAND (fn
, 0);
2691 if (really_overloaded_fn (fn
)
2692 || TREE_CODE (fn
) == TEMPLATE_DECL
)
2695 tree templates
= NULL_TREE
;
2697 args
= resolve_args (args
);
2699 if (args
== error_mark_node
)
2700 return error_mark_node
;
2702 for (t1
= fn
; t1
; t1
= OVL_NEXT (t1
))
2704 tree t
= OVL_CURRENT (t1
);
2706 my_friendly_assert (!DECL_FUNCTION_MEMBER_P (t
), 20020913);
2708 if (TREE_CODE (t
) == TEMPLATE_DECL
)
2710 templates
= tree_cons (NULL_TREE
, t
, templates
);
2711 candidates
= add_template_candidate
2712 (candidates
, t
, NULL_TREE
, explicit_targs
, args
,
2713 NULL_TREE
, /*access_path=*/NULL_TREE
,
2714 /*conversion_path=*/NULL_TREE
,
2715 LOOKUP_NORMAL
, DEDUCE_CALL
);
2717 else if (! template_only
)
2718 candidates
= add_function_candidate
2719 (candidates
, t
, NULL_TREE
, args
,
2720 /*access_path=*/NULL_TREE
,
2721 /*conversion_path=*/NULL_TREE
, LOOKUP_NORMAL
);
2724 if (! any_viable (candidates
))
2726 if (candidates
&& ! candidates
->next
)
2727 return build_function_call (candidates
->fn
, args
);
2728 error ("no matching function for call to `%D(%A)'",
2729 DECL_NAME (OVL_CURRENT (fn
)), args
);
2731 print_z_candidates (candidates
);
2732 return error_mark_node
;
2734 candidates
= splice_viable (candidates
);
2735 cand
= tourney (candidates
);
2739 error ("call of overloaded `%D(%A)' is ambiguous",
2740 DECL_NAME (OVL_FUNCTION (fn
)), args
);
2741 print_z_candidates (candidates
);
2742 return error_mark_node
;
2745 return build_over_call (cand
, args
, LOOKUP_NORMAL
);
2748 /* This is not really overloaded. */
2749 fn
= OVL_CURRENT (fn
);
2751 return build_function_call (fn
, args
);
2755 build_object_call (tree obj
, tree args
)
2757 struct z_candidate
*candidates
= 0, *cand
;
2758 tree fns
, convs
, mem_args
= NULL_TREE
;
2759 tree type
= TREE_TYPE (obj
);
2761 if (TYPE_PTRMEMFUNC_P (type
))
2763 /* It's no good looking for an overloaded operator() on a
2764 pointer-to-member-function. */
2765 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj
);
2766 return error_mark_node
;
2769 fns
= lookup_fnfields (TYPE_BINFO (type
), ansi_opname (CALL_EXPR
), 1);
2770 if (fns
== error_mark_node
)
2771 return error_mark_node
;
2773 args
= resolve_args (args
);
2775 if (args
== error_mark_node
)
2776 return error_mark_node
;
2780 tree base
= BINFO_TYPE (BASELINK_BINFO (fns
));
2781 mem_args
= tree_cons (NULL_TREE
, build_this (obj
), args
);
2783 for (fns
= BASELINK_FUNCTIONS (fns
); fns
; fns
= OVL_NEXT (fns
))
2785 tree fn
= OVL_CURRENT (fns
);
2786 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2789 = add_template_candidate (candidates
, fn
, base
, NULL_TREE
,
2790 mem_args
, NULL_TREE
,
2793 LOOKUP_NORMAL
, DEDUCE_CALL
);
2796 candidates
= add_function_candidate
2797 (candidates
, fn
, base
, mem_args
, TYPE_BINFO (type
),
2798 TYPE_BINFO (type
), LOOKUP_NORMAL
);
2802 convs
= lookup_conversions (type
);
2804 for (; convs
; convs
= TREE_CHAIN (convs
))
2806 tree fns
= TREE_VALUE (convs
);
2807 tree totype
= TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns
)));
2809 if ((TREE_CODE (totype
) == POINTER_TYPE
2810 && TREE_CODE (TREE_TYPE (totype
)) == FUNCTION_TYPE
)
2811 || (TREE_CODE (totype
) == REFERENCE_TYPE
2812 && TREE_CODE (TREE_TYPE (totype
)) == FUNCTION_TYPE
)
2813 || (TREE_CODE (totype
) == REFERENCE_TYPE
2814 && TREE_CODE (TREE_TYPE (totype
)) == POINTER_TYPE
2815 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype
))) == FUNCTION_TYPE
))
2816 for (; fns
; fns
= OVL_NEXT (fns
))
2818 tree fn
= OVL_CURRENT (fns
);
2819 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2821 candidates
= (add_template_conv_candidate
2822 (candidates
, fn
, obj
, args
, totype
,
2823 /*access_path=*/NULL_TREE
,
2824 /*conversion_path=*/NULL_TREE
));
2827 candidates
= add_conv_candidate (candidates
, fn
, obj
, args
,
2828 /*conversion_path=*/NULL_TREE
,
2829 /*access_path=*/NULL_TREE
);
2833 if (! any_viable (candidates
))
2835 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj
), args
);
2836 print_z_candidates (candidates
);
2837 return error_mark_node
;
2840 candidates
= splice_viable (candidates
);
2841 cand
= tourney (candidates
);
2845 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj
), args
);
2846 print_z_candidates (candidates
);
2847 return error_mark_node
;
2850 /* Since cand->fn will be a type, not a function, for a conversion
2851 function, we must be careful not to unconditionally look at
2853 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
2854 && DECL_OVERLOADED_OPERATOR_P (cand
->fn
) == CALL_EXPR
)
2855 return build_over_call (cand
, mem_args
, LOOKUP_NORMAL
);
2857 obj
= convert_like_with_context
2858 (TREE_VEC_ELT (cand
->convs
, 0), obj
, cand
->fn
, -1);
2861 return build_function_call (obj
, args
);
2865 op_error (enum tree_code code
, enum tree_code code2
,
2866 tree arg1
, tree arg2
, tree arg3
, const char *problem
)
2870 if (code
== MODIFY_EXPR
)
2871 opname
= assignment_operator_name_info
[code2
].name
;
2873 opname
= operator_name_info
[code
].name
;
2878 error ("%s for `%T ? %T : %T' operator", problem
,
2879 error_type (arg1
), error_type (arg2
), error_type (arg3
));
2881 case POSTINCREMENT_EXPR
:
2882 case POSTDECREMENT_EXPR
:
2883 error ("%s for `%T %s' operator", problem
, error_type (arg1
), opname
);
2886 error ("%s for `%T [%T]' operator", problem
,
2887 error_type (arg1
), error_type (arg2
));
2891 error ("%s for `%T %s %T' operator", problem
,
2892 error_type (arg1
), opname
, error_type (arg2
));
2894 error ("%s for `%s %T' operator", problem
, opname
, error_type (arg1
));
2898 /* Return the implicit conversion sequence that could be used to
2899 convert E1 to E2 in [expr.cond]. */
2902 conditional_conversion (tree e1
, tree e2
)
2904 tree t1
= non_reference (TREE_TYPE (e1
));
2905 tree t2
= non_reference (TREE_TYPE (e2
));
2910 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2911 implicitly converted (clause _conv_) to the type "reference to
2912 T2", subject to the constraint that in the conversion the
2913 reference must bind directly (_dcl.init.ref_) to E1. */
2914 if (real_lvalue_p (e2
))
2916 conv
= implicit_conversion (build_reference_type (t2
),
2919 LOOKUP_NO_TEMP_BIND
);
2926 If E1 and E2 have class type, and the underlying class types are
2927 the same or one is a base class of the other: E1 can be converted
2928 to match E2 if the class of T2 is the same type as, or a base
2929 class of, the class of T1, and the cv-qualification of T2 is the
2930 same cv-qualification as, or a greater cv-qualification than, the
2931 cv-qualification of T1. If the conversion is applied, E1 is
2932 changed to an rvalue of type T2 that still refers to the original
2933 source class object (or the appropriate subobject thereof). */
2934 if (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
2935 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2
),
2936 TYPE_MAIN_VARIANT (t1
)))
2938 if (at_least_as_qualified_p (t2
, t1
))
2940 conv
= build1 (IDENTITY_CONV
, t1
, e1
);
2941 if (!same_type_p (TYPE_MAIN_VARIANT (t1
),
2942 TYPE_MAIN_VARIANT (t2
)))
2943 conv
= build_conv (BASE_CONV
, t2
, conv
);
2952 E1 can be converted to match E2 if E1 can be implicitly converted
2953 to the type that expression E2 would have if E2 were converted to
2954 an rvalue (or the type it has, if E2 is an rvalue). */
2955 return implicit_conversion (t2
, t1
, e1
, LOOKUP_NORMAL
);
2958 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2959 arguments to the conditional expression. */
2962 build_conditional_expr (tree arg1
, tree arg2
, tree arg3
)
2967 tree result_type
= NULL_TREE
;
2968 bool lvalue_p
= true;
2969 struct z_candidate
*candidates
= 0;
2970 struct z_candidate
*cand
;
2972 /* As a G++ extension, the second argument to the conditional can be
2973 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2974 c'.) If the second operand is omitted, make sure it is
2975 calculated only once. */
2979 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2980 arg1
= arg2
= save_expr (arg1
);
2985 The first expr ession is implicitly converted to bool (clause
2987 arg1
= cp_convert (boolean_type_node
, arg1
);
2989 /* If something has already gone wrong, just pass that fact up the
2991 if (arg1
== error_mark_node
2992 || arg2
== error_mark_node
2993 || arg3
== error_mark_node
2994 || TREE_TYPE (arg1
) == error_mark_node
2995 || TREE_TYPE (arg2
) == error_mark_node
2996 || TREE_TYPE (arg3
) == error_mark_node
)
2997 return error_mark_node
;
3001 If either the second or the third operand has type (possibly
3002 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3003 array-to-pointer (_conv.array_), and function-to-pointer
3004 (_conv.func_) standard conversions are performed on the second
3005 and third operands. */
3006 arg2_type
= TREE_TYPE (arg2
);
3007 arg3_type
= TREE_TYPE (arg3
);
3008 if (VOID_TYPE_P (arg2_type
) || VOID_TYPE_P (arg3_type
))
3010 /* Do the conversions. We don't these for `void' type arguments
3011 since it can't have any effect and since decay_conversion
3012 does not handle that case gracefully. */
3013 if (!VOID_TYPE_P (arg2_type
))
3014 arg2
= decay_conversion (arg2
);
3015 if (!VOID_TYPE_P (arg3_type
))
3016 arg3
= decay_conversion (arg3
);
3017 arg2_type
= TREE_TYPE (arg2
);
3018 arg3_type
= TREE_TYPE (arg3
);
3022 One of the following shall hold:
3024 --The second or the third operand (but not both) is a
3025 throw-expression (_except.throw_); the result is of the
3026 type of the other and is an rvalue.
3028 --Both the second and the third operands have type void; the
3029 result is of type void and is an rvalue. */
3030 if ((TREE_CODE (arg2
) == THROW_EXPR
)
3031 ^ (TREE_CODE (arg3
) == THROW_EXPR
))
3032 result_type
= ((TREE_CODE (arg2
) == THROW_EXPR
)
3033 ? arg3_type
: arg2_type
);
3034 else if (VOID_TYPE_P (arg2_type
) && VOID_TYPE_P (arg3_type
))
3035 result_type
= void_type_node
;
3038 error ("`%E' has type `void' and is not a throw-expression",
3039 VOID_TYPE_P (arg2_type
) ? arg2
: arg3
);
3040 return error_mark_node
;
3044 goto valid_operands
;
3048 Otherwise, if the second and third operand have different types,
3049 and either has (possibly cv-qualified) class type, an attempt is
3050 made to convert each of those operands to the type of the other. */
3051 else if (!same_type_p (arg2_type
, arg3_type
)
3052 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)))
3054 tree conv2
= conditional_conversion (arg2
, arg3
);
3055 tree conv3
= conditional_conversion (arg3
, arg2
);
3059 If both can be converted, or one can be converted but the
3060 conversion is ambiguous, the program is ill-formed. If
3061 neither can be converted, the operands are left unchanged and
3062 further checking is performed as described below. If exactly
3063 one conversion is possible, that conversion is applied to the
3064 chosen operand and the converted operand is used in place of
3065 the original operand for the remainder of this section. */
3066 if ((conv2
&& !ICS_BAD_FLAG (conv2
)
3067 && conv3
&& !ICS_BAD_FLAG (conv3
))
3068 || (conv2
&& TREE_CODE (conv2
) == AMBIG_CONV
)
3069 || (conv3
&& TREE_CODE (conv3
) == AMBIG_CONV
))
3071 error ("operands to ?: have different types");
3072 return error_mark_node
;
3074 else if (conv2
&& !ICS_BAD_FLAG (conv2
))
3076 arg2
= convert_like (conv2
, arg2
);
3077 arg2
= convert_from_reference (arg2
);
3078 /* That may not quite have done the trick. If the two types
3079 are cv-qualified variants of one another, we will have
3080 just used an IDENTITY_CONV. (There's no conversion from
3081 an lvalue of one class type to an lvalue of another type,
3082 even a cv-qualified variant, and we don't want to lose
3083 lvalue-ness here.) So, we manually add a NOP_EXPR here
3085 if (!same_type_p (TREE_TYPE (arg2
), arg3_type
))
3086 arg2
= build1 (NOP_EXPR
, arg3_type
, arg2
);
3087 arg2_type
= TREE_TYPE (arg2
);
3089 else if (conv3
&& !ICS_BAD_FLAG (conv3
))
3091 arg3
= convert_like (conv3
, arg3
);
3092 arg3
= convert_from_reference (arg3
);
3093 if (!same_type_p (TREE_TYPE (arg3
), arg2_type
))
3094 arg3
= build1 (NOP_EXPR
, arg2_type
, arg3
);
3095 arg3_type
= TREE_TYPE (arg3
);
3101 If the second and third operands are lvalues and have the same
3102 type, the result is of that type and is an lvalue. */
3103 if (real_lvalue_p (arg2
) && real_lvalue_p (arg3
) &&
3104 same_type_p (arg2_type
, arg3_type
))
3106 result_type
= arg2_type
;
3107 goto valid_operands
;
3112 Otherwise, the result is an rvalue. If the second and third
3113 operand do not have the same type, and either has (possibly
3114 cv-qualified) class type, overload resolution is used to
3115 determine the conversions (if any) to be applied to the operands
3116 (_over.match.oper_, _over.built_). */
3118 if (!same_type_p (arg2_type
, arg3_type
)
3119 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)))
3124 /* Rearrange the arguments so that add_builtin_candidate only has
3125 to know about two args. In build_builtin_candidates, the
3126 arguments are unscrambled. */
3130 candidates
= add_builtin_candidates (candidates
,
3133 ansi_opname (COND_EXPR
),
3139 If the overload resolution fails, the program is
3141 if (!any_viable (candidates
))
3143 op_error (COND_EXPR
, NOP_EXPR
, arg1
, arg2
, arg3
, "no match");
3144 print_z_candidates (candidates
);
3145 return error_mark_node
;
3147 candidates
= splice_viable (candidates
);
3148 cand
= tourney (candidates
);
3151 op_error (COND_EXPR
, NOP_EXPR
, arg1
, arg2
, arg3
, "no match");
3152 print_z_candidates (candidates
);
3153 return error_mark_node
;
3158 Otherwise, the conversions thus determined are applied, and
3159 the converted operands are used in place of the original
3160 operands for the remainder of this section. */
3161 conv
= TREE_VEC_ELT (cand
->convs
, 0);
3162 arg1
= convert_like (conv
, arg1
);
3163 conv
= TREE_VEC_ELT (cand
->convs
, 1);
3164 arg2
= convert_like (conv
, arg2
);
3165 conv
= TREE_VEC_ELT (cand
->convs
, 2);
3166 arg3
= convert_like (conv
, arg3
);
3171 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3172 and function-to-pointer (_conv.func_) standard conversions are
3173 performed on the second and third operands.
3175 We need to force the lvalue-to-rvalue conversion here for class types,
3176 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3177 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3180 We use ocp_convert rather than build_user_type_conversion because the
3181 latter returns NULL_TREE on failure, while the former gives an error. */
3183 if (IS_AGGR_TYPE (TREE_TYPE (arg2
)) && real_lvalue_p (arg2
))
3184 arg2
= ocp_convert (TREE_TYPE (arg2
), arg2
,
3185 CONV_IMPLICIT
|CONV_FORCE_TEMP
, LOOKUP_NORMAL
);
3187 arg2
= decay_conversion (arg2
);
3188 arg2_type
= TREE_TYPE (arg2
);
3190 if (IS_AGGR_TYPE (TREE_TYPE (arg3
)) && real_lvalue_p (arg3
))
3191 arg3
= ocp_convert (TREE_TYPE (arg3
), arg3
,
3192 CONV_IMPLICIT
|CONV_FORCE_TEMP
, LOOKUP_NORMAL
);
3194 arg3
= decay_conversion (arg3
);
3195 arg3_type
= TREE_TYPE (arg3
);
3197 if (arg2
== error_mark_node
|| arg3
== error_mark_node
)
3198 return error_mark_node
;
3202 After those conversions, one of the following shall hold:
3204 --The second and third operands have the same type; the result is of
3206 if (same_type_p (arg2_type
, arg3_type
))
3207 result_type
= arg2_type
;
3210 --The second and third operands have arithmetic or enumeration
3211 type; the usual arithmetic conversions are performed to bring
3212 them to a common type, and the result is of that type. */
3213 else if ((ARITHMETIC_TYPE_P (arg2_type
)
3214 || TREE_CODE (arg2_type
) == ENUMERAL_TYPE
)
3215 && (ARITHMETIC_TYPE_P (arg3_type
)
3216 || TREE_CODE (arg3_type
) == ENUMERAL_TYPE
))
3218 /* In this case, there is always a common type. */
3219 result_type
= type_after_usual_arithmetic_conversions (arg2_type
,
3222 if (TREE_CODE (arg2_type
) == ENUMERAL_TYPE
3223 && TREE_CODE (arg3_type
) == ENUMERAL_TYPE
)
3224 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3225 arg2_type
, arg3_type
);
3226 else if (extra_warnings
3227 && ((TREE_CODE (arg2_type
) == ENUMERAL_TYPE
3228 && !same_type_p (arg3_type
, type_promotes_to (arg2_type
)))
3229 || (TREE_CODE (arg3_type
) == ENUMERAL_TYPE
3230 && !same_type_p (arg2_type
, type_promotes_to (arg3_type
)))))
3231 warning ("enumeral and non-enumeral type in conditional expression");
3233 arg2
= perform_implicit_conversion (result_type
, arg2
);
3234 arg3
= perform_implicit_conversion (result_type
, arg3
);
3238 --The second and third operands have pointer type, or one has
3239 pointer type and the other is a null pointer constant; pointer
3240 conversions (_conv.ptr_) and qualification conversions
3241 (_conv.qual_) are performed to bring them to their composite
3242 pointer type (_expr.rel_). The result is of the composite
3245 --The second and third operands have pointer to member type, or
3246 one has pointer to member type and the other is a null pointer
3247 constant; pointer to member conversions (_conv.mem_) and
3248 qualification conversions (_conv.qual_) are performed to bring
3249 them to a common type, whose cv-qualification shall match the
3250 cv-qualification of either the second or the third operand.
3251 The result is of the common type. */
3252 else if ((null_ptr_cst_p (arg2
)
3253 && (TYPE_PTR_P (arg3_type
) || TYPE_PTRMEM_P (arg3_type
)
3254 || TYPE_PTRMEMFUNC_P (arg3_type
)))
3255 || (null_ptr_cst_p (arg3
)
3256 && (TYPE_PTR_P (arg2_type
) || TYPE_PTRMEM_P (arg2_type
)
3257 || TYPE_PTRMEMFUNC_P (arg2_type
)))
3258 || (TYPE_PTR_P (arg2_type
) && TYPE_PTR_P (arg3_type
))
3259 || (TYPE_PTRMEM_P (arg2_type
) && TYPE_PTRMEM_P (arg3_type
))
3260 || (TYPE_PTRMEMFUNC_P (arg2_type
)
3261 && TYPE_PTRMEMFUNC_P (arg3_type
)))
3263 result_type
= composite_pointer_type (arg2_type
, arg3_type
, arg2
,
3264 arg3
, "conditional expression");
3265 arg2
= perform_implicit_conversion (result_type
, arg2
);
3266 arg3
= perform_implicit_conversion (result_type
, arg3
);
3271 error ("operands to ?: have different types");
3272 return error_mark_node
;
3276 result
= fold (build (COND_EXPR
, result_type
, arg1
, arg2
, arg3
));
3277 /* Expand both sides into the same slot, hopefully the target of the
3278 ?: expression. We used to check for TARGET_EXPRs here, but now we
3279 sometimes wrap them in NOP_EXPRs so the test would fail. */
3280 if (!lvalue_p
&& IS_AGGR_TYPE (result_type
))
3281 result
= build_target_expr_with_type (result
, result_type
);
3283 /* If this expression is an rvalue, but might be mistaken for an
3284 lvalue, we must add a NON_LVALUE_EXPR. */
3285 if (!lvalue_p
&& real_lvalue_p (result
))
3286 result
= build1 (NON_LVALUE_EXPR
, result_type
, result
);
3292 build_new_op (enum tree_code code
, int flags
, tree arg1
, tree arg2
, tree arg3
)
3294 struct z_candidate
*candidates
= 0, *cand
;
3295 tree fns
, mem_arglist
= NULL_TREE
, arglist
, fnname
;
3296 enum tree_code code2
= NOP_EXPR
;
3297 tree templates
= NULL_TREE
;
3299 bool viable_candidates
;
3301 if (error_operand_p (arg1
)
3302 || error_operand_p (arg2
)
3303 || error_operand_p (arg3
))
3304 return error_mark_node
;
3306 /* This can happen if a template takes all non-type parameters, e.g.
3307 undeclared_template<1, 5, 72>a; */
3308 if (code
== LT_EXPR
&& TREE_CODE (arg1
) == TEMPLATE_DECL
)
3310 error ("`%D' must be declared before use", arg1
);
3311 return error_mark_node
;
3314 if (code
== MODIFY_EXPR
)
3316 code2
= TREE_CODE (arg3
);
3318 fnname
= ansi_assopname (code2
);
3321 fnname
= ansi_opname (code
);
3323 if (TREE_CODE (arg1
) == OFFSET_REF
)
3324 arg1
= resolve_offset_ref (arg1
);
3325 arg1
= convert_from_reference (arg1
);
3326 if (CLASS_TYPE_P (TREE_TYPE (arg1
))
3327 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg1
)))
3328 /* Make sure the template type is instantiated now. */
3329 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)));
3335 case VEC_DELETE_EXPR
:
3337 /* Use build_op_new_call and build_op_delete_call instead. */
3341 return build_object_call (arg1
, arg2
);
3349 if (TREE_CODE (arg2
) == OFFSET_REF
)
3350 arg2
= resolve_offset_ref (arg2
);
3351 arg2
= convert_from_reference (arg2
);
3352 if (CLASS_TYPE_P (TREE_TYPE (arg2
))
3353 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg2
)))
3354 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (arg2
)));
3358 if (TREE_CODE (arg3
) == OFFSET_REF
)
3359 arg3
= resolve_offset_ref (arg3
);
3360 arg3
= convert_from_reference (arg3
);
3361 if (CLASS_TYPE_P (TREE_TYPE (arg3
))
3362 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg3
)))
3363 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (arg3
)));
3366 if (code
== COND_EXPR
)
3368 if (arg2
== NULL_TREE
3369 || TREE_CODE (TREE_TYPE (arg2
)) == VOID_TYPE
3370 || TREE_CODE (TREE_TYPE (arg3
)) == VOID_TYPE
3371 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2
))
3372 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3
))))
3375 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1
))
3376 && (! arg2
|| ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2
))))
3379 if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
3380 arg2
= integer_zero_node
;
3382 arglist
= NULL_TREE
;
3384 arglist
= tree_cons (NULL_TREE
, arg3
, arglist
);
3386 arglist
= tree_cons (NULL_TREE
, arg2
, arglist
);
3387 arglist
= tree_cons (NULL_TREE
, arg1
, arglist
);
3389 fns
= lookup_function_nonclass (fnname
, arglist
);
3391 if (fns
&& TREE_CODE (fns
) == TREE_LIST
)
3392 fns
= TREE_VALUE (fns
);
3393 for (; fns
; fns
= OVL_NEXT (fns
))
3395 tree fn
= OVL_CURRENT (fns
);
3396 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
3398 templates
= tree_cons (NULL_TREE
, fn
, templates
);
3400 = add_template_candidate (candidates
, fn
, NULL_TREE
, NULL_TREE
,
3401 arglist
, TREE_TYPE (fnname
),
3402 /*access_path=*/NULL_TREE
,
3403 /*conversion_path=*/NULL_TREE
,
3404 flags
, DEDUCE_CALL
);
3407 candidates
= add_function_candidate (candidates
, fn
, NULL_TREE
,
3409 /*access_path=*/NULL_TREE
,
3410 /*conversion_path=*/NULL_TREE
,
3414 if (IS_AGGR_TYPE (TREE_TYPE (arg1
)))
3416 fns
= lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1
)), fnname
, 1);
3417 if (fns
== error_mark_node
)
3425 tree conversion_path
= BASELINK_BINFO (fns
);
3427 mem_arglist
= tree_cons (NULL_TREE
, build_this (arg1
), TREE_CHAIN (arglist
));
3428 for (fns
= BASELINK_FUNCTIONS (fns
); fns
; fns
= OVL_NEXT (fns
))
3430 tree fn
= OVL_CURRENT (fns
);
3432 tree access_path
= TYPE_BINFO (TREE_TYPE (arg1
));
3434 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
3435 this_arglist
= mem_arglist
;
3437 this_arglist
= arglist
;
3439 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
3441 /* A member template. */
3442 templates
= tree_cons (NULL_TREE
, fn
, templates
);
3444 = add_template_candidate (candidates
, fn
,
3445 BINFO_TYPE (conversion_path
),
3447 this_arglist
, TREE_TYPE (fnname
),
3448 access_path
, conversion_path
,
3449 flags
, DEDUCE_CALL
);
3452 candidates
= add_function_candidate
3453 (candidates
, fn
, BINFO_TYPE (conversion_path
), this_arglist
,
3454 access_path
, conversion_path
, flags
);
3461 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3462 to know about two args; a builtin candidate will always have a first
3463 parameter of type bool. We'll handle that in
3464 build_builtin_candidate. */
3465 if (code
== COND_EXPR
)
3475 args
[2] = NULL_TREE
;
3478 candidates
= add_builtin_candidates
3479 (candidates
, code
, code2
, fnname
, args
, flags
);
3486 /* For these, the built-in candidates set is empty
3487 [over.match.oper]/3. We don't want non-strict matches
3488 because exact matches are always possible with built-in
3489 operators. The built-in candidate set for COMPONENT_REF
3490 would be empty too, but since there are no such built-in
3491 operators, we accept non-strict matches for them. */
3492 viable_candidates
= any_strictly_viable (candidates
);
3496 viable_candidates
= any_viable (candidates
);
3500 if (! viable_candidates
)
3504 case POSTINCREMENT_EXPR
:
3505 case POSTDECREMENT_EXPR
:
3506 /* Look for an `operator++ (int)'. If they didn't have
3507 one, then we fall back to the old way of doing things. */
3508 if (flags
& LOOKUP_COMPLAIN
)
3509 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3511 operator_name_info
[code
].name
);
3512 if (code
== POSTINCREMENT_EXPR
)
3513 code
= PREINCREMENT_EXPR
;
3515 code
= PREDECREMENT_EXPR
;
3516 return build_new_op (code
, flags
, arg1
, NULL_TREE
, NULL_TREE
);
3518 /* The caller will deal with these. */
3527 if (flags
& LOOKUP_COMPLAIN
)
3529 op_error (code
, code2
, arg1
, arg2
, arg3
, "no match");
3530 print_z_candidates (candidates
);
3532 return error_mark_node
;
3534 candidates
= splice_viable (candidates
);
3535 cand
= tourney (candidates
);
3539 if (flags
& LOOKUP_COMPLAIN
)
3541 op_error (code
, code2
, arg1
, arg2
, arg3
, "ambiguous overload");
3542 print_z_candidates (candidates
);
3544 return error_mark_node
;
3547 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
)
3549 extern int warn_synth
;
3551 && fnname
== ansi_assopname (NOP_EXPR
)
3552 && DECL_ARTIFICIAL (cand
->fn
)
3554 && ! candidates
->next
->next
)
3556 warning ("using synthesized `%#D' for copy assignment",
3558 cp_warning_at (" where cfront would use `%#D'",
3560 ? candidates
->next
->fn
3564 return build_over_call
3566 TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
3567 ? mem_arglist
: arglist
,
3571 /* Check for comparison of different enum types. */
3580 if (TREE_CODE (TREE_TYPE (arg1
)) == ENUMERAL_TYPE
3581 && TREE_CODE (TREE_TYPE (arg2
)) == ENUMERAL_TYPE
3582 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
))
3583 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2
))))
3585 warning ("comparison between `%#T' and `%#T'",
3586 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
3593 /* We need to strip any leading REF_BIND so that bitfields don't cause
3594 errors. This should not remove any important conversions, because
3595 builtins don't apply to class objects directly. */
3596 conv
= TREE_VEC_ELT (cand
->convs
, 0);
3597 if (TREE_CODE (conv
) == REF_BIND
)
3598 conv
= TREE_OPERAND (conv
, 0);
3599 arg1
= convert_like (conv
, arg1
);
3602 conv
= TREE_VEC_ELT (cand
->convs
, 1);
3603 if (TREE_CODE (conv
) == REF_BIND
)
3604 conv
= TREE_OPERAND (conv
, 0);
3605 arg2
= convert_like (conv
, arg2
);
3609 conv
= TREE_VEC_ELT (cand
->convs
, 2);
3610 if (TREE_CODE (conv
) == REF_BIND
)
3611 conv
= TREE_OPERAND (conv
, 0);
3612 arg3
= convert_like (conv
, arg3
);
3619 return build_modify_expr (arg1
, code2
, arg2
);
3622 return build_indirect_ref (arg1
, "unary *");
3627 case TRUNC_DIV_EXPR
:
3638 case TRUNC_MOD_EXPR
:
3642 case TRUTH_ANDIF_EXPR
:
3643 case TRUTH_ORIF_EXPR
:
3644 return cp_build_binary_op (code
, arg1
, arg2
);
3649 case TRUTH_NOT_EXPR
:
3650 case PREINCREMENT_EXPR
:
3651 case POSTINCREMENT_EXPR
:
3652 case PREDECREMENT_EXPR
:
3653 case POSTDECREMENT_EXPR
:
3656 return build_unary_op (code
, arg1
, candidates
!= 0);
3659 return build_array_ref (arg1
, arg2
);
3662 return build_conditional_expr (arg1
, arg2
, arg3
);
3665 return build_m_component_ref
3666 (build_indirect_ref (arg1
, NULL
), arg2
);
3668 /* The caller will deal with these. */
3680 /* Build a call to operator delete. This has to be handled very specially,
3681 because the restrictions on what signatures match are different from all
3682 other call instances. For a normal delete, only a delete taking (void *)
3683 or (void *, size_t) is accepted. For a placement delete, only an exact
3684 match with the placement new is accepted.
3686 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3687 ADDR is the pointer to be deleted.
3688 SIZE is the size of the memory block to be deleted.
3689 FLAGS are the usual overloading flags.
3690 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3693 build_op_delete_call (enum tree_code code
, tree addr
, tree size
,
3694 int flags
, tree placement
)
3696 tree fn
= NULL_TREE
;
3697 tree fns
, fnname
, fntype
, argtypes
, args
, type
;
3700 if (addr
== error_mark_node
)
3701 return error_mark_node
;
3703 type
= TREE_TYPE (TREE_TYPE (addr
));
3704 while (TREE_CODE (type
) == ARRAY_TYPE
)
3705 type
= TREE_TYPE (type
);
3707 fnname
= ansi_opname (code
);
3709 if (IS_AGGR_TYPE (type
) && ! (flags
& LOOKUP_GLOBAL
))
3712 If the result of the lookup is ambiguous or inaccessible, or if
3713 the lookup selects a placement deallocation function, the
3714 program is ill-formed.
3716 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3718 fns
= lookup_fnfields (TYPE_BINFO (type
), fnname
, 1);
3719 if (fns
== error_mark_node
)
3720 return error_mark_node
;
3725 if (fns
== NULL_TREE
)
3726 fns
= lookup_name_nonclass (fnname
);
3733 /* Find the allocation function that is being called. */
3734 call_expr
= placement
;
3735 /* Sometimes we have a COMPOUND_EXPR, rather than a simple
3737 while (TREE_CODE (call_expr
) == COMPOUND_EXPR
)
3738 call_expr
= TREE_OPERAND (call_expr
, 1);
3739 /* Extract the function. */
3740 alloc_fn
= get_callee_fndecl (call_expr
);
3741 my_friendly_assert (alloc_fn
!= NULL_TREE
, 20020327);
3742 /* Then the second parm type. */
3743 argtypes
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn
)));
3744 /* Also the second argument. */
3745 args
= TREE_CHAIN (TREE_OPERAND (call_expr
, 1));
3749 /* First try it without the size argument. */
3750 argtypes
= void_list_node
;
3754 /* Strip const and volatile from addr. */
3755 addr
= cp_convert (ptr_type_node
, addr
);
3757 /* We make two tries at finding a matching `operator delete'. On
3758 the first pass, we look for an one-operator (or placement)
3759 operator delete. If we're not doing placement delete, then on
3760 the second pass we look for a two-argument delete. */
3761 for (pass
= 0; pass
< (placement
? 1 : 2); ++pass
)
3764 argtypes
= tree_cons (NULL_TREE
, ptr_type_node
, argtypes
);
3766 /* Normal delete; now try to find a match including the size
3768 argtypes
= tree_cons (NULL_TREE
, ptr_type_node
,
3769 tree_cons (NULL_TREE
, sizetype
,
3771 fntype
= build_function_type (void_type_node
, argtypes
);
3773 /* Go through the `operator delete' functions looking for one
3774 with a matching type. */
3775 for (fn
= BASELINK_P (fns
) ? BASELINK_FUNCTIONS (fns
) : fns
;
3781 /* Exception specifications on the `delete' operator do not
3783 t
= build_exception_variant (TREE_TYPE (OVL_CURRENT (fn
)),
3785 /* We also don't compare attributes. We're really just
3786 trying to check the types of the first two parameters. */
3787 if (comptypes (t
, fntype
, COMPARE_NO_ATTRIBUTES
))
3791 /* If we found a match, we're done. */
3796 /* If we have a matching function, call it. */
3799 /* Make sure we have the actual function, and not an
3801 fn
= OVL_CURRENT (fn
);
3803 /* If the FN is a member function, make sure that it is
3805 if (DECL_CLASS_SCOPE_P (fn
))
3806 enforce_access (type
, fn
);
3809 args
= tree_cons (NULL_TREE
, addr
, args
);
3811 args
= tree_cons (NULL_TREE
, addr
,
3812 build_tree_list (NULL_TREE
, size
));
3814 return build_function_call (fn
, args
);
3817 /* If we are doing placement delete we do nothing if we don't find a
3818 matching op delete. */
3822 error ("no suitable `operator delete' for `%T'", type
);
3823 return error_mark_node
;
3826 /* If the current scope isn't allowed to access DECL along
3827 BASETYPE_PATH, give an error. The most derived class in
3828 BASETYPE_PATH is the one used to qualify DECL. */
3831 enforce_access (tree basetype_path
, tree decl
)
3833 if (!accessible_p (basetype_path
, decl
))
3835 if (TREE_PRIVATE (decl
))
3836 cp_error_at ("`%+#D' is private", decl
);
3837 else if (TREE_PROTECTED (decl
))
3838 cp_error_at ("`%+#D' is protected", decl
);
3840 cp_error_at ("`%+#D' is inaccessible", decl
);
3841 error ("within this context");
3848 /* Perform the conversions in CONVS on the expression EXPR.
3849 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3850 indicates the `this' argument of a method. INNER is nonzero when
3851 being called to continue a conversion chain. It is negative when a
3852 reference binding will be applied, positive otherwise. */
3855 convert_like_real (tree convs
, tree expr
, tree fn
, int argnum
, int inner
)
3859 tree totype
= TREE_TYPE (convs
);
3861 if (ICS_BAD_FLAG (convs
)
3862 && TREE_CODE (convs
) != USER_CONV
3863 && TREE_CODE (convs
) != AMBIG_CONV
3864 && TREE_CODE (convs
) != REF_BIND
)
3867 for (; t
; t
= TREE_OPERAND (t
, 0))
3869 if (TREE_CODE (t
) == USER_CONV
|| !ICS_BAD_FLAG (t
))
3871 expr
= convert_like_real (t
, expr
, fn
, argnum
, 1);
3874 else if (TREE_CODE (t
) == AMBIG_CONV
)
3875 return convert_like_real (t
, expr
, fn
, argnum
, 1);
3876 else if (TREE_CODE (t
) == IDENTITY_CONV
)
3879 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr
), totype
);
3881 pedwarn (" initializing argument %P of `%D'", argnum
, fn
);
3882 return cp_convert (totype
, expr
);
3886 expr
= dubious_conversion_warnings
3887 (totype
, expr
, "argument", fn
, argnum
);
3888 switch (TREE_CODE (convs
))
3892 struct z_candidate
*cand
3893 = WRAPPER_ZC (TREE_OPERAND (convs
, 1));
3894 tree convfn
= cand
->fn
;
3897 if (DECL_CONSTRUCTOR_P (convfn
))
3899 tree t
= build_int_2 (0, 0);
3900 TREE_TYPE (t
) = build_pointer_type (DECL_CONTEXT (convfn
));
3902 args
= build_tree_list (NULL_TREE
, expr
);
3903 if (DECL_HAS_IN_CHARGE_PARM_P (convfn
)
3904 || DECL_HAS_VTT_PARM_P (convfn
))
3905 /* We should never try to call the abstract or base constructor
3908 args
= tree_cons (NULL_TREE
, t
, args
);
3911 args
= build_this (expr
);
3912 expr
= build_over_call (cand
, args
, LOOKUP_NORMAL
);
3914 /* If this is a constructor or a function returning an aggr type,
3915 we need to build up a TARGET_EXPR. */
3916 if (DECL_CONSTRUCTOR_P (convfn
))
3917 expr
= build_cplus_new (totype
, expr
);
3919 /* The result of the call is then used to direct-initialize the object
3920 that is the destination of the copy-initialization. [dcl.init]
3922 Note that this step is not reflected in the conversion sequence;
3923 it affects the semantics when we actually perform the
3924 conversion, but is not considered during overload resolution.
3926 If the target is a class, that means call a ctor. */
3927 if (IS_AGGR_TYPE (totype
)
3928 && (inner
>= 0 || !lvalue_p (expr
)))
3930 savew
= warningcount
, savee
= errorcount
;
3931 expr
= build_special_member_call
3932 (NULL_TREE
, complete_ctor_identifier
,
3933 build_tree_list (NULL_TREE
, expr
), TYPE_BINFO (totype
),
3934 /* Core issue 84, now a DR, says that we don't allow UDCs
3935 for these args (which deliberately breaks copy-init of an
3936 auto_ptr<Base> from an auto_ptr<Derived>). */
3937 LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
|LOOKUP_NO_CONVERSION
);
3939 /* Tell the user where this failing constructor call came from. */
3942 if (warningcount
> savew
)
3944 (" initializing argument %P of `%D' from result of `%D'",
3945 argnum
, fn
, convfn
);
3946 else if (errorcount
> savee
)
3948 (" initializing argument %P of `%D' from result of `%D'",
3949 argnum
, fn
, convfn
);
3953 if (warningcount
> savew
)
3954 warning (" initializing temporary from result of `%D'",
3956 else if (errorcount
> savee
)
3957 error (" initializing temporary from result of `%D'",
3960 expr
= build_cplus_new (totype
, expr
);
3965 if (type_unknown_p (expr
))
3966 expr
= instantiate_type (totype
, expr
, tf_error
| tf_warning
);
3969 /* Call build_user_type_conversion again for the error. */
3970 return build_user_type_conversion
3971 (totype
, TREE_OPERAND (convs
, 0), LOOKUP_NORMAL
);
3977 expr
= convert_like_real (TREE_OPERAND (convs
, 0), expr
, fn
, argnum
,
3978 TREE_CODE (convs
) == REF_BIND
? -1 : 1);
3979 if (expr
== error_mark_node
)
3980 return error_mark_node
;
3982 /* Convert a non-array constant variable to its underlying value, unless we
3983 are about to bind it to a reference, in which case we need to
3984 leave it as an lvalue. */
3985 if (TREE_CODE (convs
) != REF_BIND
3986 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
3987 expr
= decl_constant_value (expr
);
3989 switch (TREE_CODE (convs
))
3992 if (! IS_AGGR_TYPE (totype
))
3994 /* else fall through */
3996 if (TREE_CODE (convs
) == BASE_CONV
&& !NEED_TEMPORARY_P (convs
))
3998 /* We are going to bind a reference directly to a base-class
3999 subobject of EXPR. */
4000 tree base_ptr
= build_pointer_type (totype
);
4002 /* Build an expression for `*((base*) &expr)'. */
4003 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
4004 expr
= perform_implicit_conversion (base_ptr
, expr
);
4005 expr
= build_indirect_ref (expr
, "implicit conversion");
4009 /* Copy-initialization where the cv-unqualified version of the source
4010 type is the same class as, or a derived class of, the class of the
4011 destination [is treated as direct-initialization]. [dcl.init] */
4012 savew
= warningcount
, savee
= errorcount
;
4013 expr
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
4014 build_tree_list (NULL_TREE
, expr
),
4015 TYPE_BINFO (totype
),
4016 LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
);
4019 if (warningcount
> savew
)
4020 warning (" initializing argument %P of `%D'", argnum
, fn
);
4021 else if (errorcount
> savee
)
4022 error (" initializing argument %P of `%D'", argnum
, fn
);
4024 return build_cplus_new (totype
, expr
);
4028 tree ref_type
= totype
;
4030 /* If necessary, create a temporary. */
4031 if (NEED_TEMPORARY_P (convs
) || !non_cast_lvalue_p (expr
))
4033 tree type
= TREE_TYPE (TREE_OPERAND (convs
, 0));
4034 expr
= build_target_expr_with_type (expr
, type
);
4037 /* Take the address of the thing to which we will bind the
4039 expr
= build_unary_op (ADDR_EXPR
, expr
, 1);
4040 if (expr
== error_mark_node
)
4041 return error_mark_node
;
4043 /* Convert it to a pointer to the type referred to by the
4044 reference. This will adjust the pointer if a derived to
4045 base conversion is being performed. */
4046 expr
= cp_convert (build_pointer_type (TREE_TYPE (ref_type
)),
4048 /* Convert the pointer to the desired reference type. */
4049 expr
= build1 (NOP_EXPR
, ref_type
, expr
);
4055 return decay_conversion (expr
);
4058 /* Warn about deprecated conversion if appropriate. */
4059 string_conv_p (totype
, expr
, 1);
4065 return ocp_convert (totype
, expr
, CONV_IMPLICIT
,
4066 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
);
4069 /* Build a call to __builtin_trap which can be used in an expression. */
4072 call_builtin_trap (void)
4074 tree fn
= get_identifier ("__builtin_trap");
4075 if (IDENTIFIER_GLOBAL_VALUE (fn
))
4076 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
4080 fn
= build_call (fn
, NULL_TREE
);
4081 fn
= build (COMPOUND_EXPR
, integer_type_node
, fn
, integer_zero_node
);
4085 /* ARG is being passed to a varargs function. Perform any conversions
4086 required. Array/function to pointer decay must have already happened.
4087 Return the converted value. */
4090 convert_arg_to_ellipsis (tree arg
)
4092 if (TREE_CODE (TREE_TYPE (arg
)) == REAL_TYPE
4093 && (TYPE_PRECISION (TREE_TYPE (arg
))
4094 < TYPE_PRECISION (double_type_node
)))
4095 /* Convert `float' to `double'. */
4096 arg
= cp_convert (double_type_node
, arg
);
4098 /* Convert `short' and `char' to full-size `int'. */
4099 arg
= default_conversion (arg
);
4101 arg
= require_complete_type (arg
);
4103 if (arg
!= error_mark_node
&& ! pod_type_p (TREE_TYPE (arg
)))
4105 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
4106 here and do a bitwise copy, but now cp_expr_size will abort if we
4108 warning ("cannot pass objects of non-POD type `%#T' through `...'; \
4109 call will abort at runtime",
4111 arg
= call_builtin_trap ();
4117 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4120 build_x_va_arg (tree expr
, tree type
)
4122 if (processing_template_decl
)
4123 return build_min (VA_ARG_EXPR
, type
, expr
);
4125 type
= complete_type_or_else (type
, NULL_TREE
);
4127 if (expr
== error_mark_node
|| !type
)
4128 return error_mark_node
;
4130 if (! pod_type_p (type
))
4132 /* Undefined behavior [expr.call] 5.2.2/7. */
4133 warning ("cannot receive objects of non-POD type `%#T' through `...'",
4137 return build_va_arg (expr
, type
);
4140 /* TYPE has been given to va_arg. Apply the default conversions which
4141 would have happened when passed via ellipsis. Return the promoted
4142 type, or the passed type if there is no change. */
4145 cxx_type_promotes_to (tree type
)
4149 if (TREE_CODE (type
) == ARRAY_TYPE
)
4150 return build_pointer_type (TREE_TYPE (type
));
4152 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4153 return build_pointer_type (type
);
4155 promote
= type_promotes_to (type
);
4156 if (same_type_p (type
, promote
))
4162 /* ARG is a default argument expression being passed to a parameter of
4163 the indicated TYPE, which is a parameter to FN. Do any required
4164 conversions. Return the converted value. */
4167 convert_default_arg (tree type
, tree arg
, tree fn
, int parmnum
)
4169 /* If the ARG is an unparsed default argument expression, the
4170 conversion cannot be performed. */
4171 if (TREE_CODE (arg
) == DEFAULT_ARG
)
4173 error ("the default argument for parameter %d of `%D' has "
4174 "not yet been parsed",
4176 return error_mark_node
;
4179 if (fn
&& DECL_TEMPLATE_INFO (fn
))
4180 arg
= tsubst_default_argument (fn
, type
, arg
);
4182 arg
= break_out_target_exprs (arg
);
4184 if (TREE_CODE (arg
) == CONSTRUCTOR
)
4186 arg
= digest_init (type
, arg
, 0);
4187 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_NORMAL
,
4188 "default argument", fn
, parmnum
);
4192 /* This could get clobbered by the following call. */
4193 if (TREE_HAS_CONSTRUCTOR (arg
))
4194 arg
= copy_node (arg
);
4196 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_NORMAL
,
4197 "default argument", fn
, parmnum
);
4198 arg
= convert_for_arg_passing (type
, arg
);
4204 /* Returns the type which will really be used for passing an argument of
4208 type_passed_as (tree type
)
4210 /* Pass classes with copy ctors by invisible reference. */
4211 if (TREE_ADDRESSABLE (type
))
4212 type
= build_reference_type (type
);
4213 else if (PROMOTE_PROTOTYPES
4214 && INTEGRAL_TYPE_P (type
)
4215 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
4216 type
= integer_type_node
;
4221 /* Actually perform the appropriate conversion. */
4224 convert_for_arg_passing (tree type
, tree val
)
4226 if (val
== error_mark_node
)
4228 /* Pass classes with copy ctors by invisible reference. */
4229 else if (TREE_ADDRESSABLE (type
))
4230 val
= build1 (ADDR_EXPR
, build_reference_type (type
), val
);
4231 else if (PROMOTE_PROTOTYPES
4232 && INTEGRAL_TYPE_P (type
)
4233 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
4234 val
= default_conversion (val
);
4238 /* Subroutine of the various build_*_call functions. Overload resolution
4239 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4240 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4241 bitmask of various LOOKUP_* flags which apply to the call itself. */
4244 build_over_call (struct z_candidate
*cand
, tree args
, int flags
)
4247 tree convs
= cand
->convs
;
4248 tree converted_args
= NULL_TREE
;
4249 tree parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4250 tree conv
, arg
, val
;
4254 /* Give any warnings we noticed during overload resolution. */
4256 for (val
= cand
->warnings
; val
; val
= TREE_CHAIN (val
))
4257 joust (cand
, WRAPPER_ZC (TREE_VALUE (val
)), 1);
4259 if (DECL_FUNCTION_MEMBER_P (fn
))
4260 enforce_access (cand
->access_path
, fn
);
4262 if (args
&& TREE_CODE (args
) != TREE_LIST
)
4263 args
= build_tree_list (NULL_TREE
, args
);
4266 /* The implicit parameters to a constructor are not considered by overload
4267 resolution, and must be of the proper type. */
4268 if (DECL_CONSTRUCTOR_P (fn
))
4270 converted_args
= tree_cons (NULL_TREE
, TREE_VALUE (arg
), converted_args
);
4271 arg
= TREE_CHAIN (arg
);
4272 parm
= TREE_CHAIN (parm
);
4273 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
4274 /* We should never try to call the abstract constructor. */
4276 if (DECL_HAS_VTT_PARM_P (fn
))
4278 converted_args
= tree_cons
4279 (NULL_TREE
, TREE_VALUE (arg
), converted_args
);
4280 arg
= TREE_CHAIN (arg
);
4281 parm
= TREE_CHAIN (parm
);
4284 /* Bypass access control for 'this' parameter. */
4285 else if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
4287 tree parmtype
= TREE_VALUE (parm
);
4288 tree argtype
= TREE_TYPE (TREE_VALUE (arg
));
4292 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs
, i
)))
4293 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4294 TREE_TYPE (argtype
), fn
);
4296 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4297 X is called for an object that is not of type X, or of a type
4298 derived from X, the behavior is undefined.
4300 So we can assume that anything passed as 'this' is non-null, and
4301 optimize accordingly. */
4302 my_friendly_assert (TREE_CODE (parmtype
) == POINTER_TYPE
, 19990811);
4303 /* Convert to the base in which the function was declared. */
4304 my_friendly_assert (cand
->conversion_path
!= NULL_TREE
, 20020730);
4305 converted_arg
= build_base_path (PLUS_EXPR
,
4307 cand
->conversion_path
,
4309 /* If fn was found by a using declaration, the conversion path
4310 will be to the derived class, not the base declaring fn. We
4311 must convert from derived to base. */
4312 base_binfo
= lookup_base (TREE_TYPE (TREE_TYPE (converted_arg
)),
4313 TREE_TYPE (parmtype
), ba_ignore
, NULL
);
4315 converted_arg
= build_base_path (PLUS_EXPR
, converted_arg
,
4318 converted_args
= tree_cons (NULL_TREE
, converted_arg
, converted_args
);
4319 parm
= TREE_CHAIN (parm
);
4320 arg
= TREE_CHAIN (arg
);
4326 parm
= TREE_CHAIN (parm
), arg
= TREE_CHAIN (arg
), ++i
)
4328 tree type
= TREE_VALUE (parm
);
4330 conv
= TREE_VEC_ELT (convs
, i
);
4331 val
= convert_like_with_context
4332 (conv
, TREE_VALUE (arg
), fn
, i
- is_method
);
4334 val
= convert_for_arg_passing (type
, val
);
4335 converted_args
= tree_cons (NULL_TREE
, val
, converted_args
);
4338 /* Default arguments */
4339 for (; parm
&& parm
!= void_list_node
; parm
= TREE_CHAIN (parm
), i
++)
4341 = tree_cons (NULL_TREE
,
4342 convert_default_arg (TREE_VALUE (parm
),
4343 TREE_PURPOSE (parm
),
4348 for (; arg
; arg
= TREE_CHAIN (arg
))
4350 = tree_cons (NULL_TREE
,
4351 convert_arg_to_ellipsis (TREE_VALUE (arg
)),
4354 converted_args
= nreverse (converted_args
);
4357 check_function_format (NULL
, TYPE_ATTRIBUTES (TREE_TYPE (fn
)),
4360 /* Avoid actually calling copy constructors and copy assignment operators,
4363 if (! flag_elide_constructors
)
4364 /* Do things the hard way. */;
4365 else if (TREE_VEC_LENGTH (convs
) == 1
4366 && DECL_COPY_CONSTRUCTOR_P (fn
))
4369 arg
= skip_artificial_parms_for (fn
, converted_args
);
4370 arg
= TREE_VALUE (arg
);
4372 /* Pull out the real argument, disregarding const-correctness. */
4374 while (TREE_CODE (targ
) == NOP_EXPR
4375 || TREE_CODE (targ
) == NON_LVALUE_EXPR
4376 || TREE_CODE (targ
) == CONVERT_EXPR
)
4377 targ
= TREE_OPERAND (targ
, 0);
4378 if (TREE_CODE (targ
) == ADDR_EXPR
)
4380 targ
= TREE_OPERAND (targ
, 0);
4381 if (!same_type_ignoring_top_level_qualifiers_p
4382 (TREE_TYPE (TREE_TYPE (arg
)), TREE_TYPE (targ
)))
4391 arg
= build_indirect_ref (arg
, 0);
4393 /* [class.copy]: the copy constructor is implicitly defined even if
4394 the implementation elided its use. */
4395 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn
)))
4398 /* If we're creating a temp and we already have one, don't create a
4399 new one. If we're not creating a temp but we get one, use
4400 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4401 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4402 temp or an INIT_EXPR otherwise. */
4403 if (integer_zerop (TREE_VALUE (args
)))
4405 if (TREE_CODE (arg
) == TARGET_EXPR
)
4407 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn
)))
4408 return build_target_expr_with_type (arg
, DECL_CONTEXT (fn
));
4410 else if (TREE_CODE (arg
) == TARGET_EXPR
4411 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn
)))
4414 tree to
= stabilize_reference
4415 (build_indirect_ref (TREE_VALUE (args
), 0));
4417 val
= build (INIT_EXPR
, DECL_CONTEXT (fn
), to
, arg
);
4418 address
= build_unary_op (ADDR_EXPR
, val
, 0);
4419 /* Avoid a warning about this expression, if the address is
4421 TREE_USED (address
) = 1;
4425 else if (DECL_OVERLOADED_OPERATOR_P (fn
) == NOP_EXPR
4427 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn
)))
4429 tree to
= stabilize_reference
4430 (build_indirect_ref (TREE_VALUE (converted_args
), 0));
4432 arg
= build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args
)), 0);
4433 val
= build (MODIFY_EXPR
, TREE_TYPE (to
), to
, arg
);
4439 if (DECL_VINDEX (fn
) && (flags
& LOOKUP_NONVIRTUAL
) == 0)
4441 tree t
, *p
= &TREE_VALUE (converted_args
);
4442 tree binfo
= lookup_base (TREE_TYPE (TREE_TYPE (*p
)),
4445 my_friendly_assert (binfo
&& binfo
!= error_mark_node
, 20010730);
4447 *p
= build_base_path (PLUS_EXPR
, *p
, binfo
, 1);
4448 if (TREE_SIDE_EFFECTS (*p
))
4449 *p
= save_expr (*p
);
4450 t
= build_pointer_type (TREE_TYPE (fn
));
4451 if (DECL_CONTEXT (fn
) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn
)))
4452 fn
= build_java_interface_fn_ref (fn
, *p
);
4454 fn
= build_vfn_ref (build_indirect_ref (*p
, 0), DECL_VINDEX (fn
));
4457 else if (DECL_INLINE (fn
))
4458 fn
= inline_conversion (fn
);
4460 fn
= build_addr_func (fn
);
4462 /* Recognize certain built-in functions so we can make tree-codes
4463 other than CALL_EXPR. We do this when it enables fold-const.c
4464 to do something useful. */
4466 if (TREE_CODE (fn
) == ADDR_EXPR
4467 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
4468 && DECL_BUILT_IN (TREE_OPERAND (fn
, 0)))
4471 exp
= expand_tree_builtin (TREE_OPERAND (fn
, 0), args
, converted_args
);
4476 /* Some built-in function calls will be evaluated at
4477 compile-time in fold (). */
4478 fn
= fold (build_call (fn
, converted_args
));
4479 if (VOID_TYPE_P (TREE_TYPE (fn
)))
4481 fn
= require_complete_type (fn
);
4482 if (fn
== error_mark_node
)
4483 return error_mark_node
;
4484 if (IS_AGGR_TYPE (TREE_TYPE (fn
)))
4485 fn
= build_cplus_new (TREE_TYPE (fn
), fn
);
4486 return convert_from_reference (fn
);
4489 static GTY(()) tree java_iface_lookup_fn
;
4491 /* Make an expression which yields the address of the Java interface
4492 method FN. This is achieved by generating a call to libjava's
4493 _Jv_LookupInterfaceMethodIdx(). */
4496 build_java_interface_fn_ref (tree fn
, tree instance
)
4498 tree lookup_args
, lookup_fn
, method
, idx
;
4499 tree klass_ref
, iface
, iface_ref
;
4502 if (!java_iface_lookup_fn
)
4504 tree endlink
= build_void_list_node ();
4505 tree t
= tree_cons (NULL_TREE
, ptr_type_node
,
4506 tree_cons (NULL_TREE
, ptr_type_node
,
4507 tree_cons (NULL_TREE
, java_int_type_node
,
4509 java_iface_lookup_fn
4510 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4511 build_function_type (ptr_type_node
, t
),
4512 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
4515 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4516 This is the first entry in the vtable. */
4517 klass_ref
= build_vtbl_ref (build_indirect_ref (instance
, 0),
4520 /* Get the java.lang.Class pointer for the interface being called. */
4521 iface
= DECL_CONTEXT (fn
);
4522 iface_ref
= lookup_field (iface
, get_identifier ("class$"), 0, 0);
4523 if (!iface_ref
|| TREE_CODE (iface_ref
) != VAR_DECL
4524 || DECL_CONTEXT (iface_ref
) != iface
)
4526 error ("could not find class$ field in java interface type `%T'",
4528 return error_mark_node
;
4530 iface_ref
= build1 (ADDR_EXPR
, build_pointer_type (iface
), iface_ref
);
4532 /* Determine the itable index of FN. */
4534 for (method
= TYPE_METHODS (iface
); method
; method
= TREE_CHAIN (method
))
4536 if (!DECL_VIRTUAL_P (method
))
4542 idx
= build_int_2 (i
, 0);
4544 lookup_args
= tree_cons (NULL_TREE
, klass_ref
,
4545 tree_cons (NULL_TREE
, iface_ref
,
4546 build_tree_list (NULL_TREE
, idx
)));
4547 lookup_fn
= build1 (ADDR_EXPR
,
4548 build_pointer_type (TREE_TYPE (java_iface_lookup_fn
)),
4549 java_iface_lookup_fn
);
4550 return build (CALL_EXPR
, ptr_type_node
, lookup_fn
, lookup_args
, NULL_TREE
);
4553 /* Returns the value to use for the in-charge parameter when making a
4554 call to a function with the indicated NAME. */
4557 in_charge_arg_for_name (tree name
)
4559 if (name
== base_ctor_identifier
4560 || name
== base_dtor_identifier
)
4561 return integer_zero_node
;
4562 else if (name
== complete_ctor_identifier
)
4563 return integer_one_node
;
4564 else if (name
== complete_dtor_identifier
)
4565 return integer_two_node
;
4566 else if (name
== deleting_dtor_identifier
)
4567 return integer_three_node
;
4569 /* This function should only be called with one of the names listed
4575 /* Build a call to a constructor, destructor, or an assignment
4576 operator for INSTANCE, an expression with class type. NAME
4577 indicates the special member function to call; ARGS are the
4578 arguments. BINFO indicates the base of INSTANCE that is to be
4579 passed as the `this' parameter to the member function called.
4581 FLAGS are the LOOKUP_* flags to use when processing the call.
4583 If NAME indicates a complete object constructor, INSTANCE may be
4584 NULL_TREE. In this case, the caller will call build_cplus_new to
4585 store the newly constructed object into a VAR_DECL. */
4588 build_special_member_call (tree instance
, tree name
, tree args
,
4589 tree binfo
, int flags
)
4592 /* The type of the subobject to be constructed or destroyed. */
4595 my_friendly_assert (name
== complete_ctor_identifier
4596 || name
== base_ctor_identifier
4597 || name
== complete_dtor_identifier
4598 || name
== base_dtor_identifier
4599 || name
== deleting_dtor_identifier
4600 || name
== ansi_assopname (NOP_EXPR
),
4602 my_friendly_assert (binfo
!= NULL_TREE
, 20020712);
4604 class_type
= BINFO_TYPE (binfo
);
4606 /* Handle the special case where INSTANCE is NULL_TREE. */
4607 if (name
== complete_ctor_identifier
&& !instance
)
4609 instance
= build_int_2 (0, 0);
4610 TREE_TYPE (instance
) = build_pointer_type (class_type
);
4611 instance
= build1 (INDIRECT_REF
, class_type
, instance
);
4613 else if (name
== complete_dtor_identifier
4614 || name
== base_dtor_identifier
4615 || name
== deleting_dtor_identifier
)
4616 my_friendly_assert (args
== NULL_TREE
, 20020712);
4618 my_friendly_assert (instance
!= NULL_TREE
, 20020712);
4620 /* Resolve the name. */
4621 if (!complete_type_or_else (BINFO_TYPE (binfo
), NULL_TREE
))
4622 return error_mark_node
;
4624 fns
= lookup_fnfields (binfo
, name
, 1);
4626 /* When making a call to a constructor or destructor for a subobject
4627 that uses virtual base classes, pass down a pointer to a VTT for
4629 if ((name
== base_ctor_identifier
4630 || name
== base_dtor_identifier
)
4631 && TYPE_USES_VIRTUAL_BASECLASSES (class_type
))
4636 /* If the current function is a complete object constructor
4637 or destructor, then we fetch the VTT directly.
4638 Otherwise, we look it up using the VTT we were given. */
4639 vtt
= TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type
));
4640 vtt
= decay_conversion (vtt
);
4641 vtt
= build (COND_EXPR
, TREE_TYPE (vtt
),
4642 build (EQ_EXPR
, boolean_type_node
,
4643 current_in_charge_parm
, integer_zero_node
),
4646 if (TREE_VIA_VIRTUAL (binfo
))
4647 binfo
= binfo_for_vbase (class_type
, current_class_type
);
4648 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo
), 20010110);
4649 sub_vtt
= build (PLUS_EXPR
, TREE_TYPE (vtt
), vtt
,
4650 BINFO_SUBVTT_INDEX (binfo
));
4652 args
= tree_cons (NULL_TREE
, sub_vtt
, args
);
4655 return build_new_method_call (instance
, fns
, args
, binfo
, flags
);
4658 /* Return the NAME, as a C string. The NAME indicates a function that
4659 is a member of TYPE. *FREE_P is set to true if the caller must
4660 free the memory returned.
4662 Rather than go through all of this, we should simply set the names
4663 of constructors and destructors appropriately, and dispense with
4664 ctor_identifier, dtor_identifier, etc. */
4667 name_as_c_string (tree name
, tree type
, bool *free_p
)
4671 /* Assume that we will not allocate memory. */
4673 /* Constructors and destructors are special. */
4674 if (IDENTIFIER_CTOR_OR_DTOR_P (name
))
4677 = (char *) IDENTIFIER_POINTER (constructor_name (type
));
4678 /* For a destructor, add the '~'. */
4679 if (name
== complete_dtor_identifier
4680 || name
== base_dtor_identifier
4681 || name
== deleting_dtor_identifier
)
4683 pretty_name
= concat ("~", pretty_name
, NULL
);
4684 /* Remember that we need to free the memory allocated. */
4689 pretty_name
= (char *) IDENTIFIER_POINTER (name
);
4694 /* Build a call to "INSTANCE.FN (ARGS)". */
4697 build_new_method_call (tree instance
, tree fns
, tree args
,
4698 tree conversion_path
, int flags
)
4700 struct z_candidate
*candidates
= 0, *cand
;
4701 tree explicit_targs
= NULL_TREE
;
4702 tree basetype
= NULL_TREE
;
4705 tree mem_args
= NULL_TREE
, instance_ptr
;
4708 tree templates
= NULL_TREE
;
4712 int template_only
= 0;
4714 my_friendly_assert (instance
!= NULL_TREE
, 20020729);
4716 if (error_operand_p (instance
)
4717 || error_operand_p (fns
)
4718 || args
== error_mark_node
)
4719 return error_mark_node
;
4721 /* Process the argument list. */
4723 args
= resolve_args (args
);
4724 if (args
== error_mark_node
)
4725 return error_mark_node
;
4727 if (TREE_CODE (instance
) == OFFSET_REF
)
4728 instance
= resolve_offset_ref (instance
);
4729 if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
4730 instance
= convert_from_reference (instance
);
4731 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (instance
));
4732 instance_ptr
= build_this (instance
);
4734 if (!BASELINK_P (fns
))
4736 call
= build_field_call (instance_ptr
, fns
, args
);
4739 error ("call to non-function `%D'", fns
);
4740 return error_mark_node
;
4743 if (!conversion_path
)
4744 conversion_path
= BASELINK_BINFO (fns
);
4745 access_binfo
= BASELINK_ACCESS_BINFO (fns
);
4746 optype
= BASELINK_OPTYPE (fns
);
4747 fns
= BASELINK_FUNCTIONS (fns
);
4749 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
4751 explicit_targs
= TREE_OPERAND (fns
, 1);
4752 fns
= TREE_OPERAND (fns
, 0);
4756 my_friendly_assert (TREE_CODE (fns
) == FUNCTION_DECL
4757 || TREE_CODE (fns
) == TEMPLATE_DECL
4758 || TREE_CODE (fns
) == OVERLOAD
,
4761 /* XXX this should be handled before we get here. */
4762 if (! IS_AGGR_TYPE (basetype
))
4764 if ((flags
& LOOKUP_COMPLAIN
) && basetype
!= error_mark_node
)
4765 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4766 fns
, instance
, basetype
);
4768 return error_mark_node
;
4771 fn
= get_first_fn (fns
);
4772 name
= DECL_NAME (fn
);
4774 if (IDENTIFIER_CTOR_OR_DTOR_P (name
))
4776 /* Callers should explicitly indicate whether they want to construct
4777 the complete object or just the part without virtual bases. */
4778 my_friendly_assert (name
!= ctor_identifier
, 20000408);
4779 /* Similarly for destructors. */
4780 my_friendly_assert (name
!= dtor_identifier
, 20000408);
4783 /* It's OK to call destructors on cv-qualified objects. Therefore,
4784 convert the INSTANCE_PTR to the unqualified type, if necessary. */
4785 if (DECL_DESTRUCTOR_P (fn
))
4787 tree type
= build_pointer_type (basetype
);
4788 if (!same_type_p (type
, TREE_TYPE (instance_ptr
)))
4789 instance_ptr
= build1 (NOP_EXPR
, type
, instance_ptr
);
4792 class_type
= (conversion_path
? BINFO_TYPE (conversion_path
) : NULL_TREE
);
4793 mem_args
= tree_cons (NULL_TREE
, instance_ptr
, args
);
4795 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
4797 tree t
= OVL_CURRENT (fn
);
4800 /* We can end up here for copy-init of same or base class. */
4801 if ((flags
& LOOKUP_ONLYCONVERTING
)
4802 && DECL_NONCONVERTING_P (t
))
4805 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
4806 this_arglist
= mem_args
;
4808 this_arglist
= args
;
4810 if (TREE_CODE (t
) == TEMPLATE_DECL
)
4812 /* A member template. */
4813 templates
= tree_cons (NULL_TREE
, t
, templates
);
4815 add_template_candidate (candidates
, t
,
4818 this_arglist
, optype
,
4824 else if (! template_only
)
4825 candidates
= add_function_candidate (candidates
, t
,
4833 if (! any_viable (candidates
))
4835 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4836 if (flags
& LOOKUP_SPECULATIVELY
)
4838 if (!COMPLETE_TYPE_P (basetype
))
4839 cxx_incomplete_type_error (instance_ptr
, basetype
);
4845 pretty_name
= name_as_c_string (name
, basetype
, &free_p
);
4846 error ("no matching function for call to `%T::%s(%A)%#V'",
4847 basetype
, pretty_name
, user_args
,
4848 TREE_TYPE (TREE_TYPE (instance_ptr
)));
4852 print_z_candidates (candidates
);
4853 return error_mark_node
;
4855 candidates
= splice_viable (candidates
);
4856 cand
= tourney (candidates
);
4863 pretty_name
= name_as_c_string (name
, basetype
, &free_p
);
4864 error ("call of overloaded `%D(%A)' is ambiguous", pretty_name
,
4866 print_z_candidates (candidates
);
4869 return error_mark_node
;
4872 if (DECL_PURE_VIRTUAL_P (cand
->fn
)
4873 && instance
== current_class_ref
4874 && (DECL_CONSTRUCTOR_P (current_function_decl
)
4875 || DECL_DESTRUCTOR_P (current_function_decl
))
4876 && ! (flags
& LOOKUP_NONVIRTUAL
)
4877 && value_member (cand
->fn
, CLASSTYPE_PURE_VIRTUALS (basetype
)))
4878 error ((DECL_CONSTRUCTOR_P (current_function_decl
) ?
4879 "abstract virtual `%#D' called from constructor"
4880 : "abstract virtual `%#D' called from destructor"),
4882 if (TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
4883 && is_dummy_object (instance_ptr
))
4885 error ("cannot call member function `%D' without object", cand
->fn
);
4886 return error_mark_node
;
4889 if (DECL_VINDEX (cand
->fn
) && ! (flags
& LOOKUP_NONVIRTUAL
)
4890 && resolves_to_fixed_type_p (instance
, 0))
4891 flags
|= LOOKUP_NONVIRTUAL
;
4893 if (TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
)
4894 call
= build_over_call (cand
, mem_args
, flags
);
4897 call
= build_over_call (cand
, args
, flags
);
4898 /* In an expression of the form `a->f()' where `f' turns out to
4899 be a static member function, `a' is none-the-less evaluated. */
4900 if (!is_dummy_object (instance_ptr
) && TREE_SIDE_EFFECTS (instance
))
4901 call
= build (COMPOUND_EXPR
, TREE_TYPE (call
), instance
, call
);
4907 /* Returns true iff standard conversion sequence ICS1 is a proper
4908 subsequence of ICS2. */
4911 is_subseq (tree ics1
, tree ics2
)
4913 /* We can assume that a conversion of the same code
4914 between the same types indicates a subsequence since we only get
4915 here if the types we are converting from are the same. */
4917 while (TREE_CODE (ics1
) == RVALUE_CONV
4918 || TREE_CODE (ics1
) == LVALUE_CONV
)
4919 ics1
= TREE_OPERAND (ics1
, 0);
4923 while (TREE_CODE (ics2
) == RVALUE_CONV
4924 || TREE_CODE (ics2
) == LVALUE_CONV
)
4925 ics2
= TREE_OPERAND (ics2
, 0);
4927 if (TREE_CODE (ics2
) == USER_CONV
4928 || TREE_CODE (ics2
) == AMBIG_CONV
4929 || TREE_CODE (ics2
) == IDENTITY_CONV
)
4930 /* At this point, ICS1 cannot be a proper subsequence of
4931 ICS2. We can get a USER_CONV when we are comparing the
4932 second standard conversion sequence of two user conversion
4936 ics2
= TREE_OPERAND (ics2
, 0);
4938 if (TREE_CODE (ics2
) == TREE_CODE (ics1
)
4939 && same_type_p (TREE_TYPE (ics2
), TREE_TYPE (ics1
))
4940 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2
, 0)),
4941 TREE_TYPE (TREE_OPERAND (ics1
, 0))))
4946 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
4947 be any _TYPE nodes. */
4950 is_properly_derived_from (tree derived
, tree base
)
4952 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived
))
4953 || !IS_AGGR_TYPE_CODE (TREE_CODE (base
)))
4956 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4957 considers every class derived from itself. */
4958 return (!same_type_ignoring_top_level_qualifiers_p (derived
, base
)
4959 && DERIVED_FROM_P (base
, derived
));
4962 /* We build the ICS for an implicit object parameter as a pointer
4963 conversion sequence. However, such a sequence should be compared
4964 as if it were a reference conversion sequence. If ICS is the
4965 implicit conversion sequence for an implicit object parameter,
4966 modify it accordingly. */
4969 maybe_handle_implicit_object (tree
*ics
)
4971 if (ICS_THIS_FLAG (*ics
))
4973 /* [over.match.funcs]
4975 For non-static member functions, the type of the
4976 implicit object parameter is "reference to cv X"
4977 where X is the class of which the function is a
4978 member and cv is the cv-qualification on the member
4979 function declaration. */
4981 tree reference_type
;
4983 /* The `this' parameter is a pointer to a class type. Make the
4984 implict conversion talk about a reference to that same class
4986 reference_type
= TREE_TYPE (TREE_TYPE (*ics
));
4987 reference_type
= build_reference_type (reference_type
);
4989 if (TREE_CODE (t
) == QUAL_CONV
)
4990 t
= TREE_OPERAND (t
, 0);
4991 if (TREE_CODE (t
) == PTR_CONV
)
4992 t
= TREE_OPERAND (t
, 0);
4993 t
= build1 (IDENTITY_CONV
, TREE_TYPE (TREE_TYPE (t
)), NULL_TREE
);
4994 t
= direct_reference_binding (reference_type
, t
);
4999 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5000 and return the type to which the reference refers. Otherwise,
5001 leave *ICS unchanged and return NULL_TREE. */
5004 maybe_handle_ref_bind (tree
*ics
)
5006 if (TREE_CODE (*ics
) == REF_BIND
)
5008 tree old_ics
= *ics
;
5009 tree type
= TREE_TYPE (TREE_TYPE (old_ics
));
5010 *ics
= TREE_OPERAND (old_ics
, 0);
5011 ICS_USER_FLAG (*ics
) = ICS_USER_FLAG (old_ics
);
5012 ICS_BAD_FLAG (*ics
) = ICS_BAD_FLAG (old_ics
);
5019 /* Compare two implicit conversion sequences according to the rules set out in
5020 [over.ics.rank]. Return values:
5022 1: ics1 is better than ics2
5023 -1: ics2 is better than ics1
5024 0: ics1 and ics2 are indistinguishable */
5027 compare_ics (tree ics1
, tree ics2
)
5033 tree deref_from_type1
= NULL_TREE
;
5034 tree deref_from_type2
= NULL_TREE
;
5035 tree deref_to_type1
= NULL_TREE
;
5036 tree deref_to_type2
= NULL_TREE
;
5039 /* REF_BINDING is nonzero if the result of the conversion sequence
5040 is a reference type. In that case TARGET_TYPE is the
5041 type referred to by the reference. */
5045 /* Handle implicit object parameters. */
5046 maybe_handle_implicit_object (&ics1
);
5047 maybe_handle_implicit_object (&ics2
);
5049 /* Handle reference parameters. */
5050 target_type1
= maybe_handle_ref_bind (&ics1
);
5051 target_type2
= maybe_handle_ref_bind (&ics2
);
5055 When comparing the basic forms of implicit conversion sequences (as
5056 defined in _over.best.ics_)
5058 --a standard conversion sequence (_over.ics.scs_) is a better
5059 conversion sequence than a user-defined conversion sequence
5060 or an ellipsis conversion sequence, and
5062 --a user-defined conversion sequence (_over.ics.user_) is a
5063 better conversion sequence than an ellipsis conversion sequence
5064 (_over.ics.ellipsis_). */
5065 rank1
= ICS_RANK (ics1
);
5066 rank2
= ICS_RANK (ics2
);
5070 else if (rank1
< rank2
)
5073 if (rank1
== BAD_RANK
)
5075 /* XXX Isn't this an extension? */
5076 /* Both ICS are bad. We try to make a decision based on what
5077 would have happenned if they'd been good. */
5078 if (ICS_USER_FLAG (ics1
) > ICS_USER_FLAG (ics2
)
5079 || ICS_STD_RANK (ics1
) > ICS_STD_RANK (ics2
))
5081 else if (ICS_USER_FLAG (ics1
) < ICS_USER_FLAG (ics2
)
5082 || ICS_STD_RANK (ics1
) < ICS_STD_RANK (ics2
))
5085 /* We couldn't make up our minds; try to figure it out below. */
5088 if (ICS_ELLIPSIS_FLAG (ics1
))
5089 /* Both conversions are ellipsis conversions. */
5092 /* User-defined conversion sequence U1 is a better conversion sequence
5093 than another user-defined conversion sequence U2 if they contain the
5094 same user-defined conversion operator or constructor and if the sec-
5095 ond standard conversion sequence of U1 is better than the second
5096 standard conversion sequence of U2. */
5098 if (ICS_USER_FLAG (ics1
))
5102 for (t1
= ics1
; TREE_CODE (t1
) != USER_CONV
; t1
= TREE_OPERAND (t1
, 0))
5103 if (TREE_CODE (t1
) == AMBIG_CONV
)
5105 for (t2
= ics2
; TREE_CODE (t2
) != USER_CONV
; t2
= TREE_OPERAND (t2
, 0))
5106 if (TREE_CODE (t2
) == AMBIG_CONV
)
5109 if (USER_CONV_FN (t1
) != USER_CONV_FN (t2
))
5112 /* We can just fall through here, after setting up
5113 FROM_TYPE1 and FROM_TYPE2. */
5114 from_type1
= TREE_TYPE (t1
);
5115 from_type2
= TREE_TYPE (t2
);
5119 /* We're dealing with two standard conversion sequences.
5123 Standard conversion sequence S1 is a better conversion
5124 sequence than standard conversion sequence S2 if
5126 --S1 is a proper subsequence of S2 (comparing the conversion
5127 sequences in the canonical form defined by _over.ics.scs_,
5128 excluding any Lvalue Transformation; the identity
5129 conversion sequence is considered to be a subsequence of
5130 any non-identity conversion sequence */
5133 while (TREE_CODE (from_type1
) != IDENTITY_CONV
)
5134 from_type1
= TREE_OPERAND (from_type1
, 0);
5135 from_type1
= TREE_TYPE (from_type1
);
5138 while (TREE_CODE (from_type2
) != IDENTITY_CONV
)
5139 from_type2
= TREE_OPERAND (from_type2
, 0);
5140 from_type2
= TREE_TYPE (from_type2
);
5143 if (same_type_p (from_type1
, from_type2
))
5145 if (is_subseq (ics1
, ics2
))
5147 if (is_subseq (ics2
, ics1
))
5150 /* Otherwise, one sequence cannot be a subsequence of the other; they
5151 don't start with the same type. This can happen when comparing the
5152 second standard conversion sequence in two user-defined conversion
5159 --the rank of S1 is better than the rank of S2 (by the rules
5162 Standard conversion sequences are ordered by their ranks: an Exact
5163 Match is a better conversion than a Promotion, which is a better
5164 conversion than a Conversion.
5166 Two conversion sequences with the same rank are indistinguishable
5167 unless one of the following rules applies:
5169 --A conversion that is not a conversion of a pointer, or pointer
5170 to member, to bool is better than another conversion that is such
5173 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5174 so that we do not have to check it explicitly. */
5175 if (ICS_STD_RANK (ics1
) < ICS_STD_RANK (ics2
))
5177 else if (ICS_STD_RANK (ics2
) < ICS_STD_RANK (ics1
))
5180 to_type1
= TREE_TYPE (ics1
);
5181 to_type2
= TREE_TYPE (ics2
);
5183 if (TYPE_PTR_P (from_type1
)
5184 && TYPE_PTR_P (from_type2
)
5185 && TYPE_PTR_P (to_type1
)
5186 && TYPE_PTR_P (to_type2
))
5188 deref_from_type1
= TREE_TYPE (from_type1
);
5189 deref_from_type2
= TREE_TYPE (from_type2
);
5190 deref_to_type1
= TREE_TYPE (to_type1
);
5191 deref_to_type2
= TREE_TYPE (to_type2
);
5193 /* The rules for pointers to members A::* are just like the rules
5194 for pointers A*, except opposite: if B is derived from A then
5195 A::* converts to B::*, not vice versa. For that reason, we
5196 switch the from_ and to_ variables here. */
5197 else if (TYPE_PTRMEM_P (from_type1
)
5198 && TYPE_PTRMEM_P (from_type2
)
5199 && TYPE_PTRMEM_P (to_type1
)
5200 && TYPE_PTRMEM_P (to_type2
))
5202 deref_to_type1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1
));
5203 deref_to_type2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2
));
5204 deref_from_type1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1
));
5205 deref_from_type2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2
));
5207 else if (TYPE_PTRMEMFUNC_P (from_type1
)
5208 && TYPE_PTRMEMFUNC_P (from_type2
)
5209 && TYPE_PTRMEMFUNC_P (to_type1
)
5210 && TYPE_PTRMEMFUNC_P (to_type2
))
5212 deref_to_type1
= TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1
);
5213 deref_to_type2
= TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2
);
5214 deref_from_type1
= TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1
);
5215 deref_from_type2
= TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2
);
5218 if (deref_from_type1
!= NULL_TREE
5219 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1
))
5220 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2
)))
5222 /* This was one of the pointer or pointer-like conversions.
5226 --If class B is derived directly or indirectly from class A,
5227 conversion of B* to A* is better than conversion of B* to
5228 void*, and conversion of A* to void* is better than
5229 conversion of B* to void*. */
5230 if (TREE_CODE (deref_to_type1
) == VOID_TYPE
5231 && TREE_CODE (deref_to_type2
) == VOID_TYPE
)
5233 if (is_properly_derived_from (deref_from_type1
,
5236 else if (is_properly_derived_from (deref_from_type2
,
5240 else if (TREE_CODE (deref_to_type1
) == VOID_TYPE
5241 || TREE_CODE (deref_to_type2
) == VOID_TYPE
)
5243 if (same_type_p (deref_from_type1
, deref_from_type2
))
5245 if (TREE_CODE (deref_to_type2
) == VOID_TYPE
)
5247 if (is_properly_derived_from (deref_from_type1
,
5251 /* We know that DEREF_TO_TYPE1 is `void' here. */
5252 else if (is_properly_derived_from (deref_from_type1
,
5257 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1
))
5258 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2
)))
5262 --If class B is derived directly or indirectly from class A
5263 and class C is derived directly or indirectly from B,
5265 --conversion of C* to B* is better than conversion of C* to
5268 --conversion of B* to A* is better than conversion of C* to
5270 if (same_type_p (deref_from_type1
, deref_from_type2
))
5272 if (is_properly_derived_from (deref_to_type1
,
5275 else if (is_properly_derived_from (deref_to_type2
,
5279 else if (same_type_p (deref_to_type1
, deref_to_type2
))
5281 if (is_properly_derived_from (deref_from_type2
,
5284 else if (is_properly_derived_from (deref_from_type1
,
5290 else if (CLASS_TYPE_P (non_reference (from_type1
))
5291 && same_type_p (from_type1
, from_type2
))
5293 tree from
= non_reference (from_type1
);
5297 --binding of an expression of type C to a reference of type
5298 B& is better than binding an expression of type C to a
5299 reference of type A&
5301 --conversion of C to B is better than conversion of C to A, */
5302 if (is_properly_derived_from (from
, to_type1
)
5303 && is_properly_derived_from (from
, to_type2
))
5305 if (is_properly_derived_from (to_type1
, to_type2
))
5307 else if (is_properly_derived_from (to_type2
, to_type1
))
5311 else if (CLASS_TYPE_P (non_reference (to_type1
))
5312 && same_type_p (to_type1
, to_type2
))
5314 tree to
= non_reference (to_type1
);
5318 --binding of an expression of type B to a reference of type
5319 A& is better than binding an expression of type C to a
5320 reference of type A&,
5322 --onversion of B to A is better than conversion of C to A */
5323 if (is_properly_derived_from (from_type1
, to
)
5324 && is_properly_derived_from (from_type2
, to
))
5326 if (is_properly_derived_from (from_type2
, from_type1
))
5328 else if (is_properly_derived_from (from_type1
, from_type2
))
5335 --S1 and S2 differ only in their qualification conversion and yield
5336 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5337 qualification signature of type T1 is a proper subset of the cv-
5338 qualification signature of type T2 */
5339 if (TREE_CODE (ics1
) == QUAL_CONV
5340 && TREE_CODE (ics2
) == QUAL_CONV
5341 && same_type_p (from_type1
, from_type2
))
5342 return comp_cv_qual_signature (to_type1
, to_type2
);
5346 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5347 types to which the references refer are the same type except for
5348 top-level cv-qualifiers, and the type to which the reference
5349 initialized by S2 refers is more cv-qualified than the type to
5350 which the reference initialized by S1 refers */
5352 if (target_type1
&& target_type2
5353 && same_type_ignoring_top_level_qualifiers_p (to_type1
, to_type2
))
5354 return comp_cv_qualification (target_type2
, target_type1
);
5356 /* Neither conversion sequence is better than the other. */
5360 /* The source type for this standard conversion sequence. */
5363 source_type (tree t
)
5365 for (;; t
= TREE_OPERAND (t
, 0))
5367 if (TREE_CODE (t
) == USER_CONV
5368 || TREE_CODE (t
) == AMBIG_CONV
5369 || TREE_CODE (t
) == IDENTITY_CONV
)
5370 return TREE_TYPE (t
);
5375 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5376 a pointer to LOSER and re-running joust to produce the warning if WINNER
5377 is actually used. */
5380 add_warning (struct z_candidate
*winner
, struct z_candidate
*loser
)
5382 winner
->warnings
= tree_cons (NULL_TREE
,
5383 build_zc_wrapper (loser
),
5387 /* Returns true iff functions are equivalent. Equivalent functions are
5388 not '==' only if one is a function-local extern function or if
5389 both are extern "C". */
5392 equal_functions (tree fn1
, tree fn2
)
5394 if (DECL_LOCAL_FUNCTION_P (fn1
) || DECL_LOCAL_FUNCTION_P (fn2
)
5395 || DECL_EXTERN_C_FUNCTION_P (fn1
))
5396 return decls_match (fn1
, fn2
);
5400 /* Compare two candidates for overloading as described in
5401 [over.match.best]. Return values:
5403 1: cand1 is better than cand2
5404 -1: cand2 is better than cand1
5405 0: cand1 and cand2 are indistinguishable */
5408 joust (struct z_candidate
*cand1
, struct z_candidate
*cand2
, bool warn
)
5411 int i
, off1
= 0, off2
= 0, len
;
5413 /* Candidates that involve bad conversions are always worse than those
5415 if (cand1
->viable
> cand2
->viable
)
5417 if (cand1
->viable
< cand2
->viable
)
5420 /* If we have two pseudo-candidates for conversions to the same type,
5421 or two candidates for the same function, arbitrarily pick one. */
5422 if (cand1
->fn
== cand2
->fn
5423 && (TYPE_P (cand1
->fn
) || DECL_P (cand1
->fn
)))
5426 /* a viable function F1
5427 is defined to be a better function than another viable function F2 if
5428 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5429 ICSi(F2), and then */
5431 /* for some argument j, ICSj(F1) is a better conversion sequence than
5434 /* For comparing static and non-static member functions, we ignore
5435 the implicit object parameter of the non-static function. The
5436 standard says to pretend that the static function has an object
5437 parm, but that won't work with operator overloading. */
5438 len
= TREE_VEC_LENGTH (cand1
->convs
);
5439 if (len
!= TREE_VEC_LENGTH (cand2
->convs
))
5441 if (DECL_STATIC_FUNCTION_P (cand1
->fn
)
5442 && ! DECL_STATIC_FUNCTION_P (cand2
->fn
))
5444 else if (! DECL_STATIC_FUNCTION_P (cand1
->fn
)
5445 && DECL_STATIC_FUNCTION_P (cand2
->fn
))
5454 for (i
= 0; i
< len
; ++i
)
5456 tree t1
= TREE_VEC_ELT (cand1
->convs
, i
+off1
);
5457 tree t2
= TREE_VEC_ELT (cand2
->convs
, i
+off2
);
5458 int comp
= compare_ics (t1
, t2
);
5463 && ICS_RANK (t1
) + ICS_RANK (t2
) == STD_RANK
+ PROMO_RANK
5464 && TREE_CODE (t1
) == STD_CONV
5465 && TREE_CODE (t2
) == STD_CONV
5466 && TREE_CODE (TREE_TYPE (t1
)) == INTEGER_TYPE
5467 && TREE_CODE (TREE_TYPE (t2
)) == INTEGER_TYPE
5468 && (TYPE_PRECISION (TREE_TYPE (t1
))
5469 == TYPE_PRECISION (TREE_TYPE (t2
)))
5470 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1
, 0)))
5471 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1
, 0)))
5474 tree type
= TREE_TYPE (TREE_OPERAND (t1
, 0));
5476 struct z_candidate
*w
, *l
;
5478 type1
= TREE_TYPE (t1
), type2
= TREE_TYPE (t2
),
5479 w
= cand1
, l
= cand2
;
5481 type1
= TREE_TYPE (t2
), type2
= TREE_TYPE (t1
),
5482 w
= cand2
, l
= cand1
;
5486 warning ("passing `%T' chooses `%T' over `%T'",
5487 type
, type1
, type2
);
5488 warning (" in call to `%D'", w
->fn
);
5494 if (winner
&& comp
!= winner
)
5503 /* warn about confusing overload resolution for user-defined conversions,
5504 either between a constructor and a conversion op, or between two
5506 if (winner
&& cand1
->second_conv
5507 && ((DECL_CONSTRUCTOR_P (cand1
->fn
)
5508 != DECL_CONSTRUCTOR_P (cand2
->fn
))
5509 /* Don't warn if the two conv ops convert to the same type... */
5510 || (! DECL_CONSTRUCTOR_P (cand1
->fn
)
5511 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1
->fn
)),
5512 TREE_TYPE (TREE_TYPE (cand2
->fn
))))))
5514 int comp
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
5517 struct z_candidate
*w
, *l
;
5520 w
= cand1
, l
= cand2
;
5522 w
= cand2
, l
= cand1
;
5523 if (DECL_CONTEXT (cand1
->fn
) == DECL_CONTEXT (cand2
->fn
)
5524 && ! DECL_CONSTRUCTOR_P (cand1
->fn
)
5525 && ! DECL_CONSTRUCTOR_P (cand2
->fn
)
5526 && (convn
= standard_conversion
5527 (TREE_TYPE (TREE_TYPE (l
->fn
)),
5528 TREE_TYPE (TREE_TYPE (w
->fn
)), NULL_TREE
))
5529 && TREE_CODE (convn
) == QUAL_CONV
)
5530 /* Don't complain about `operator char *()' beating
5531 `operator const char *() const'. */;
5534 tree source
= source_type (TREE_VEC_ELT (w
->convs
, 0));
5535 if (! DECL_CONSTRUCTOR_P (w
->fn
))
5536 source
= TREE_TYPE (source
);
5537 warning ("choosing `%D' over `%D'", w
->fn
, l
->fn
);
5538 warning (" for conversion from `%T' to `%T'",
5539 source
, TREE_TYPE (w
->second_conv
));
5540 warning (" because conversion sequence for the argument is better");
5551 F1 is a non-template function and F2 is a template function
5554 if (! cand1
->template && cand2
->template)
5556 else if (cand1
->template && ! cand2
->template)
5560 F1 and F2 are template functions and the function template for F1 is
5561 more specialized than the template for F2 according to the partial
5564 if (cand1
->template && cand2
->template)
5566 winner
= more_specialized
5567 (TI_TEMPLATE (cand1
->template), TI_TEMPLATE (cand2
->template),
5569 /* Tell the deduction code how many real function arguments
5570 we saw, not counting the implicit 'this' argument. But,
5571 add_function_candidate() suppresses the "this" argument
5574 [temp.func.order]: The presence of unused ellipsis and default
5575 arguments has no effect on the partial ordering of function
5577 TREE_VEC_LENGTH (cand1
->convs
)
5578 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1
->fn
)
5579 - DECL_CONSTRUCTOR_P (cand1
->fn
)));
5586 the context is an initialization by user-defined conversion (see
5587 _dcl.init_ and _over.match.user_) and the standard conversion
5588 sequence from the return type of F1 to the destination type (i.e.,
5589 the type of the entity being initialized) is a better conversion
5590 sequence than the standard conversion sequence from the return type
5591 of F2 to the destination type. */
5593 if (cand1
->second_conv
)
5595 winner
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
5600 /* Check whether we can discard a builtin candidate, either because we
5601 have two identical ones or matching builtin and non-builtin candidates.
5603 (Pedantically in the latter case the builtin which matched the user
5604 function should not be added to the overload set, but we spot it here.
5607 ... the builtin candidates include ...
5608 - do not have the same parameter type list as any non-template
5609 non-member candidate. */
5611 if (TREE_CODE (cand1
->fn
) == IDENTIFIER_NODE
5612 || TREE_CODE (cand2
->fn
) == IDENTIFIER_NODE
)
5614 for (i
= 0; i
< len
; ++i
)
5615 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1
->convs
, i
)),
5616 TREE_TYPE (TREE_VEC_ELT (cand2
->convs
, i
))))
5618 if (i
== TREE_VEC_LENGTH (cand1
->convs
))
5620 if (cand1
->fn
== cand2
->fn
)
5621 /* Two built-in candidates; arbitrarily pick one. */
5623 else if (TREE_CODE (cand1
->fn
) == IDENTIFIER_NODE
)
5624 /* cand1 is built-in; prefer cand2. */
5627 /* cand2 is built-in; prefer cand1. */
5632 /* If the two functions are the same (this can happen with declarations
5633 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5634 if (DECL_P (cand1
->fn
) && DECL_P (cand2
->fn
)
5635 && equal_functions (cand1
->fn
, cand2
->fn
))
5640 /* Extension: If the worst conversion for one candidate is worse than the
5641 worst conversion for the other, take the first. */
5644 int rank1
= IDENTITY_RANK
, rank2
= IDENTITY_RANK
;
5645 struct z_candidate
*w
= 0, *l
= 0;
5647 for (i
= 0; i
< len
; ++i
)
5649 if (ICS_RANK (TREE_VEC_ELT (cand1
->convs
, i
+off1
)) > rank1
)
5650 rank1
= ICS_RANK (TREE_VEC_ELT (cand1
->convs
, i
+off1
));
5651 if (ICS_RANK (TREE_VEC_ELT (cand2
->convs
, i
+off2
)) > rank2
)
5652 rank2
= ICS_RANK (TREE_VEC_ELT (cand2
->convs
, i
+off2
));
5655 winner
= 1, w
= cand1
, l
= cand2
;
5657 winner
= -1, w
= cand2
, l
= cand1
;
5662 pedwarn ("choosing `%D' over `%D'", w
->fn
, l
->fn
);
5664 " because worst conversion for the former is better than worst conversion for the latter");
5672 my_friendly_assert (!winner
, 20010121);
5676 /* Given a list of candidates for overloading, find the best one, if any.
5677 This algorithm has a worst case of O(2n) (winner is last), and a best
5678 case of O(n/2) (totally ambiguous); much better than a sorting
5681 static struct z_candidate
*
5682 tourney (struct z_candidate
*candidates
)
5684 struct z_candidate
*champ
= candidates
, *challenger
;
5686 int champ_compared_to_predecessor
= 0;
5688 /* Walk through the list once, comparing each current champ to the next
5689 candidate, knocking out a candidate or two with each comparison. */
5691 for (challenger
= champ
->next
; challenger
; )
5693 fate
= joust (champ
, challenger
, 0);
5695 challenger
= challenger
->next
;
5700 champ
= challenger
->next
;
5703 champ_compared_to_predecessor
= 0;
5708 champ_compared_to_predecessor
= 1;
5711 challenger
= champ
->next
;
5715 /* Make sure the champ is better than all the candidates it hasn't yet
5716 been compared to. */
5718 for (challenger
= candidates
;
5720 && !(champ_compared_to_predecessor
&& challenger
->next
== champ
);
5721 challenger
= challenger
->next
)
5723 fate
= joust (champ
, challenger
, 0);
5731 /* Returns nonzero if things of type FROM can be converted to TO. */
5734 can_convert (tree to
, tree from
)
5736 return can_convert_arg (to
, from
, NULL_TREE
);
5739 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
5742 can_convert_arg (tree to
, tree from
, tree arg
)
5744 tree t
= implicit_conversion (to
, from
, arg
, LOOKUP_NORMAL
);
5745 return (t
&& ! ICS_BAD_FLAG (t
));
5748 /* Like can_convert_arg, but allows dubious conversions as well. */
5751 can_convert_arg_bad (tree to
, tree from
, tree arg
)
5753 return implicit_conversion (to
, from
, arg
, LOOKUP_NORMAL
) != 0;
5756 /* Convert EXPR to TYPE. Return the converted expression.
5758 Note that we allow bad conversions here because by the time we get to
5759 this point we are committed to doing the conversion. If we end up
5760 doing a bad conversion, convert_like will complain. */
5763 perform_implicit_conversion (tree type
, tree expr
)
5767 if (error_operand_p (expr
))
5768 return error_mark_node
;
5769 conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
5773 error ("could not convert `%E' to `%T'", expr
, type
);
5774 return error_mark_node
;
5777 return convert_like (conv
, expr
);
5780 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5781 initializing a variable of that TYPE. Return the converted
5785 initialize_reference (tree type
, tree expr
)
5789 conv
= reference_binding (type
, TREE_TYPE (expr
), expr
, LOOKUP_NORMAL
);
5790 if (!conv
|| ICS_BAD_FLAG (conv
))
5792 error ("could not convert `%E' to `%T'", expr
, type
);
5793 return error_mark_node
;
5796 return convert_like (conv
, expr
);
5799 #include "gt-cp-call.h"