1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) and
5 modified by Brendan Kehoe (brendan@cygnus.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 /* High-level class interface. */
37 #include "diagnostic.h"
39 extern int inhibit_warnings
;
41 static tree build_field_call
PARAMS ((tree
, tree
, tree
));
42 static struct z_candidate
* tourney
PARAMS ((struct z_candidate
*));
43 static int equal_functions
PARAMS ((tree
, tree
));
44 static int joust
PARAMS ((struct z_candidate
*, struct z_candidate
*, int));
45 static int compare_ics
PARAMS ((tree
, tree
));
46 static tree build_over_call
PARAMS ((struct z_candidate
*, tree
, int));
47 static tree build_java_interface_fn_ref
PARAMS ((tree
, tree
));
48 #define convert_like(CONV, EXPR) \
49 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0)
50 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
51 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0)
52 static tree convert_like_real
PARAMS ((tree
, tree
, tree
, int, int));
53 static void op_error
PARAMS ((enum tree_code
, enum tree_code
, tree
, tree
,
55 static tree build_object_call
PARAMS ((tree
, tree
));
56 static tree resolve_args
PARAMS ((tree
));
57 static struct z_candidate
* build_user_type_conversion_1
58 PARAMS ((tree
, tree
, int));
59 static void print_z_candidates
PARAMS ((struct z_candidate
*));
60 static tree build_this
PARAMS ((tree
));
61 static struct z_candidate
* splice_viable
PARAMS ((struct z_candidate
*));
62 static int any_viable
PARAMS ((struct z_candidate
*));
63 static int any_strictly_viable
PARAMS ((struct z_candidate
*));
64 static struct z_candidate
* add_template_candidate
65 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, tree
, tree
,
66 tree
, tree
, int, unification_kind_t
));
67 static struct z_candidate
* add_template_candidate_real
68 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, tree
, tree
,
69 tree
, tree
, int, tree
, unification_kind_t
));
70 static struct z_candidate
* add_template_conv_candidate
71 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, tree
, tree
, tree
));
72 static struct z_candidate
* add_builtin_candidates
73 PARAMS ((struct z_candidate
*, enum tree_code
, enum tree_code
,
75 static struct z_candidate
* add_builtin_candidate
76 PARAMS ((struct z_candidate
*, enum tree_code
, enum tree_code
,
77 tree
, tree
, tree
, tree
*, tree
*, int));
78 static int is_complete
PARAMS ((tree
));
79 static struct z_candidate
* build_builtin_candidate
80 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, tree
*, tree
*,
82 static struct z_candidate
* add_conv_candidate
83 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, tree
, tree
));
84 static struct z_candidate
* add_function_candidate
85 (struct z_candidate
*, tree
, tree
, tree
, tree
, tree
, int);
86 static tree implicit_conversion
PARAMS ((tree
, tree
, tree
, int));
87 static tree standard_conversion
PARAMS ((tree
, tree
, tree
));
88 static tree reference_binding
PARAMS ((tree
, tree
, tree
, int));
89 static tree non_reference
PARAMS ((tree
));
90 static tree build_conv
PARAMS ((enum tree_code
, tree
, tree
));
91 static int is_subseq
PARAMS ((tree
, tree
));
92 static tree maybe_handle_ref_bind
PARAMS ((tree
*));
93 static void maybe_handle_implicit_object
PARAMS ((tree
*));
94 static struct z_candidate
*add_candidate
95 (struct z_candidate
*, tree
, tree
, tree
, tree
, int);
96 static tree source_type
PARAMS ((tree
));
97 static void add_warning
PARAMS ((struct z_candidate
*, struct z_candidate
*));
98 static int reference_related_p
PARAMS ((tree
, tree
));
99 static int reference_compatible_p
PARAMS ((tree
, tree
));
100 static tree convert_class_to_reference
PARAMS ((tree
, tree
, tree
));
101 static tree direct_reference_binding
PARAMS ((tree
, tree
));
102 static int promoted_arithmetic_type_p
PARAMS ((tree
));
103 static tree conditional_conversion
PARAMS ((tree
, tree
));
104 static tree call_builtin_trap
PARAMS ((void));
107 build_vfield_ref (datum
, type
)
110 if (datum
== error_mark_node
)
111 return error_mark_node
;
113 if (TREE_CODE (TREE_TYPE (datum
)) == REFERENCE_TYPE
)
114 datum
= convert_from_reference (datum
);
116 if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type
)
117 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum
), type
))
118 datum
= convert_to_base (datum
, type
, /*check_access=*/false);
120 return build (COMPONENT_REF
, TREE_TYPE (TYPE_VFIELD (type
)),
121 datum
, TYPE_VFIELD (type
));
124 /* Build a call to a member of an object. I.e., one that overloads
125 operator ()(), or is a pointer-to-function or pointer-to-method. */
128 build_field_call (tree instance_ptr
, tree decl
, tree parms
)
132 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
135 if (TREE_CODE (decl
) == FIELD_DECL
|| TREE_CODE (decl
) == VAR_DECL
)
137 /* If it's a field, try overloading operator (),
138 or calling if the field is a pointer-to-function. */
139 instance
= build_indirect_ref (instance_ptr
, NULL
);
140 instance
= build_class_member_access_expr (instance
, decl
,
141 /*access_path=*/NULL_TREE
,
142 /*preserve_reference=*/false);
144 if (instance
== error_mark_node
)
145 return error_mark_node
;
147 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
148 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
,
149 instance
, parms
, NULL_TREE
);
150 else if (TREE_CODE (TREE_TYPE (instance
)) == FUNCTION_TYPE
151 || (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
152 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
)))
154 return build_function_call (instance
, parms
);
160 /* Returns nonzero iff the destructor name specified in NAME
161 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
165 check_dtor_name (basetype
, 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 (exp
, basetype
, name
, parms
)
209 tree exp
, basetype
, name
, parms
;
211 /* Because this syntactic form does not allow
212 a pointer to a base class to be `stolen',
213 we need not protect the derived->base conversion
216 @@ But we do have to check access privileges later. */
218 tree type
= TREE_TYPE (exp
);
220 if (type
== error_mark_node
221 || basetype
== error_mark_node
)
222 return error_mark_node
;
224 if (processing_template_decl
)
226 if (TREE_CODE (name
) == BIT_NOT_EXPR
227 && TREE_CODE (TREE_OPERAND (name
, 0)) == IDENTIFIER_NODE
)
229 tree type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 0);
231 name
= build_min_nt (BIT_NOT_EXPR
, type
);
233 name
= build_min_nt (SCOPE_REF
, basetype
, name
);
234 return build_min_nt (METHOD_CALL_EXPR
, name
, exp
, parms
, NULL_TREE
);
237 if (TREE_CODE (type
) == REFERENCE_TYPE
)
238 type
= TREE_TYPE (type
);
240 if (TREE_CODE (basetype
) == TREE_VEC
)
243 basetype
= BINFO_TYPE (binfo
);
248 /* Check the destructor call syntax. */
249 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
251 /* We can get here if someone writes their destructor call like
252 `obj.NS::~T()'; this isn't really a scoped method call, so hand
254 if (TREE_CODE (basetype
) == NAMESPACE_DECL
)
255 return build_method_call (exp
, name
, parms
, NULL_TREE
, LOOKUP_NORMAL
);
257 if (! check_dtor_name (basetype
, name
))
258 error ("qualified type `%T' does not match destructor name `~%T'",
259 basetype
, TREE_OPERAND (name
, 0));
261 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
262 that explicit ~int is caught in the parser; this deals with typedefs
263 and template parms. */
264 if (! IS_AGGR_TYPE (basetype
))
266 if (TYPE_MAIN_VARIANT (type
) != TYPE_MAIN_VARIANT (basetype
))
267 error ("type of `%E' does not match destructor type `%T' (type was `%T')",
268 exp
, basetype
, type
);
270 return cp_convert (void_type_node
, exp
);
274 if (TREE_CODE (basetype
) == NAMESPACE_DECL
)
276 error ("`%D' is a namespace", basetype
);
277 return error_mark_node
;
279 if (! is_aggr_type (basetype
, 1))
280 return error_mark_node
;
282 if (! IS_AGGR_TYPE (type
))
284 error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
286 return error_mark_node
;
289 decl
= build_scoped_ref (exp
, basetype
, &binfo
);
293 /* Call to a destructor. */
294 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
296 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl
)))
297 return cp_convert (void_type_node
, exp
);
299 return build_delete (TREE_TYPE (decl
), decl
,
300 sfk_complete_destructor
,
301 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
,
305 /* Call to a method. */
306 return build_method_call (decl
, name
, parms
, binfo
,
307 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
);
309 return error_mark_node
;
312 /* We want the address of a function or method. We avoid creating a
313 pointer-to-member function. */
316 build_addr_func (function
)
319 tree type
= TREE_TYPE (function
);
321 /* We have to do these by hand to avoid real pointer to member
323 if (TREE_CODE (type
) == METHOD_TYPE
)
327 type
= build_pointer_type (type
);
329 if (!cxx_mark_addressable (function
))
330 return error_mark_node
;
332 addr
= build1 (ADDR_EXPR
, type
, function
);
334 /* Address of a static or external variable or function counts
336 if (staticp (function
))
337 TREE_CONSTANT (addr
) = 1;
342 function
= default_conversion (function
);
347 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
348 POINTER_TYPE to those. Note, pointer to member function types
349 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
352 build_call (function
, parms
)
353 tree function
, parms
;
355 int is_constructor
= 0;
362 function
= build_addr_func (function
);
364 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
366 sorry ("unable to call pointer to member function here");
367 return error_mark_node
;
370 fntype
= TREE_TYPE (TREE_TYPE (function
));
371 result_type
= TREE_TYPE (fntype
);
373 if (TREE_CODE (function
) == ADDR_EXPR
374 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
375 decl
= TREE_OPERAND (function
, 0);
379 /* We check both the decl and the type; a function may be known not to
380 throw without being declared throw(). */
381 nothrow
= ((decl
&& TREE_NOTHROW (decl
))
382 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function
))));
384 if (decl
&& TREE_THIS_VOLATILE (decl
) && cfun
)
385 current_function_returns_abnormally
= 1;
387 if (decl
&& TREE_DEPRECATED (decl
))
388 warn_deprecated_use (decl
);
389 require_complete_eh_spec_types (fntype
, decl
);
391 if (decl
&& DECL_CONSTRUCTOR_P (decl
))
394 if (decl
&& ! TREE_USED (decl
))
396 /* We invoke build_call directly for several library functions.
397 These may have been declared normally if we're building libgcc,
398 so we can't just check DECL_ARTIFICIAL. */
399 if (DECL_ARTIFICIAL (decl
)
400 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl
)), "__", 2))
406 /* Don't pass empty class objects by value. This is useful
407 for tags in STL, which are used to control overload resolution.
408 We don't need to handle other cases of copying empty classes. */
409 if (! decl
|| ! DECL_BUILT_IN (decl
))
410 for (tmp
= parms
; tmp
; tmp
= TREE_CHAIN (tmp
))
411 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp
)))
412 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp
))))
414 tree t
= build (EMPTY_CLASS_EXPR
, TREE_TYPE (TREE_VALUE (tmp
)));
415 TREE_VALUE (tmp
) = build (COMPOUND_EXPR
, TREE_TYPE (t
),
416 TREE_VALUE (tmp
), t
);
419 function
= build_nt (CALL_EXPR
, function
, parms
, NULL_TREE
);
420 TREE_HAS_CONSTRUCTOR (function
) = is_constructor
;
421 TREE_TYPE (function
) = result_type
;
422 TREE_SIDE_EFFECTS (function
) = 1;
423 TREE_NOTHROW (function
) = nothrow
;
428 /* Build something of the form ptr->method (args)
429 or object.method (args). This can also build
430 calls to constructors, and find friends.
432 Member functions always take their class variable
435 INSTANCE is a class instance.
437 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
439 PARMS help to figure out what that NAME really refers to.
441 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
442 down to the real instance type to use for access checking. We need this
443 information to get protected accesses correct. This parameter is used
444 by build_member_call.
446 FLAGS is the logical disjunction of zero or more LOOKUP_
447 flags. See cp-tree.h for more info.
449 If this is all OK, calls build_function_call with the resolved
452 This function must also handle being called to perform
453 initialization, promotion/coercion of arguments, and
454 instantiation of default parameters.
456 Note that NAME may refer to an instance variable name. If
457 `operator()()' is defined for the type of that field, then we return
460 #ifdef GATHER_STATISTICS
461 extern int n_build_method_call
;
465 build_method_call (instance
, name
, parms
, basetype_path
, flags
)
466 tree instance
, name
, parms
, basetype_path
;
471 tree template_args
= NULL_TREE
;
472 bool has_template_args
= false;
474 #ifdef GATHER_STATISTICS
475 n_build_method_call
++;
478 if (instance
== error_mark_node
479 || name
== error_mark_node
480 || parms
== error_mark_node
481 || (instance
&& TREE_TYPE (instance
) == error_mark_node
))
482 return error_mark_node
;
484 if (processing_template_decl
)
486 /* We need to process template parm names here so that tsubst catches
487 them properly. Other type names can wait. */
488 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
490 tree type
= NULL_TREE
;
492 if (TREE_CODE (TREE_OPERAND (name
, 0)) == IDENTIFIER_NODE
)
493 type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 0);
494 else if (TREE_CODE (TREE_OPERAND (name
, 0)) == TYPE_DECL
)
495 type
= TREE_TYPE (TREE_OPERAND (name
, 0));
497 if (type
&& TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
498 name
= build_min_nt (BIT_NOT_EXPR
, type
);
501 return build_min_nt (METHOD_CALL_EXPR
, name
, instance
, parms
, NULL_TREE
);
504 if (TREE_CODE (instance
) == OFFSET_REF
)
505 instance
= resolve_offset_ref (instance
);
506 if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
507 instance
= convert_from_reference (instance
);
508 object_type
= TREE_TYPE (instance
);
510 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
515 error ("destructors take no parameters");
517 if (! check_dtor_name (object_type
, name
))
519 ("destructor name `~%T' does not match type `%T' of expression",
520 TREE_OPERAND (name
, 0), object_type
);
522 if (! TYPE_HAS_DESTRUCTOR (complete_type (object_type
)))
523 return cp_convert (void_type_node
, instance
);
524 instance
= default_conversion (instance
);
525 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
526 return build_delete (build_pointer_type (object_type
),
527 instance_ptr
, sfk_complete_destructor
,
528 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0);
531 if (!CLASS_TYPE_P (object_type
))
533 if ((flags
& LOOKUP_COMPLAIN
)
534 && TREE_TYPE (instance
) != error_mark_node
)
535 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
536 name
, instance
, object_type
);
537 return error_mark_node
;
540 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
542 template_args
= TREE_OPERAND (name
, 1);
543 has_template_args
= true;
544 name
= TREE_OPERAND (name
, 0);
546 if (TREE_CODE (name
) == OVERLOAD
)
547 name
= DECL_NAME (get_first_fn (name
));
548 else if (DECL_P (name
))
549 name
= DECL_NAME (name
);
550 if (has_template_args
)
551 fn
= lookup_fnfields (object_type
, name
, /*protect=*/2);
553 fn
= lookup_member (object_type
, name
, /*protect=*/2, /*want_type=*/0);
555 if (fn
&& TREE_CODE (fn
) == TREE_LIST
&& !BASELINK_P (fn
))
557 error ("request for member `%D' is ambiguous", name
);
558 print_candidates (fn
);
559 return error_mark_node
;
562 /* If the name could not be found, issue an error. */
565 unqualified_name_lookup_error (name
);
566 return error_mark_node
;
569 if (BASELINK_P (fn
) && has_template_args
)
570 BASELINK_FUNCTIONS (fn
)
571 = build_nt (TEMPLATE_ID_EXPR
,
572 BASELINK_FUNCTIONS (fn
),
574 if (BASELINK_P (fn
) && basetype_path
)
575 BASELINK_ACCESS_BINFO (fn
) = basetype_path
;
577 return build_new_method_call (instance
, fn
, parms
,
578 /*conversion_path=*/NULL_TREE
, flags
);
581 /* New overloading code. */
583 struct z_candidate
GTY(()) {
584 /* The FUNCTION_DECL that will be called if this candidate is
585 selected by overload resolution. */
590 /* If FN is a member function, the binfo indicating the path used to
591 qualify the name of FN at the call site. This path is used to
592 determine whether or not FN is accessible if it is selected by
593 overload resolution. The DECL_CONTEXT of FN will always be a
594 (possibly improper) base of this binfo. */
596 /* If FN is a non-static member function, the binfo indicating the
597 subobject to which the `this' pointer should be converted if FN
598 is selected by overload resolution. The type pointed to the by
599 the `this' pointer must correspond to the most derived class
600 indicated by the CONVERSION_PATH. */
601 tree conversion_path
;
604 struct z_candidate
*next
;
607 #define IDENTITY_RANK 0
613 #define ELLIPSIS_RANK 6
616 #define ICS_RANK(NODE) \
617 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
618 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
619 : ICS_USER_FLAG (NODE) ? USER_RANK \
620 : ICS_STD_RANK (NODE))
622 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
624 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
625 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
626 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
627 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
629 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
630 should be created to hold the result of the conversion. */
631 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
633 #define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
634 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
642 A null pointer constant is an integral constant expression
643 (_expr.const_) rvalue of integer type that evaluates to zero. */
645 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t
)) && integer_zerop (t
)))
651 /* Returns nonzero if PARMLIST consists of only default parms and/or
655 sufficient_parms_p (parmlist
)
658 for (; parmlist
&& parmlist
!= void_list_node
;
659 parmlist
= TREE_CHAIN (parmlist
))
660 if (!TREE_PURPOSE (parmlist
))
666 build_conv (code
, type
, from
)
671 int rank
= ICS_STD_RANK (from
);
673 /* We can't use buildl1 here because CODE could be USER_CONV, which
674 takes two arguments. In that case, the caller is responsible for
675 filling in the second argument. */
676 t
= make_node (code
);
677 TREE_TYPE (t
) = type
;
678 TREE_OPERAND (t
, 0) = from
;
691 if (rank
< EXACT_RANK
)
697 ICS_STD_RANK (t
) = rank
;
698 ICS_USER_FLAG (t
) = ICS_USER_FLAG (from
);
699 ICS_BAD_FLAG (t
) = ICS_BAD_FLAG (from
);
703 /* If T is a REFERENCE_TYPE return the type to which T refers.
704 Otherwise, return T itself. */
710 if (TREE_CODE (t
) == REFERENCE_TYPE
)
719 if (TREE_CODE (t
) == ARRAY_TYPE
)
721 return TYPE_MAIN_VARIANT (t
);
724 /* Returns the standard conversion path (see [conv]) from type FROM to type
725 TO, if any. For proper handling of null pointer constants, you must
726 also pass the expression EXPR to convert from. */
729 standard_conversion (to
, from
, expr
)
732 enum tree_code fcode
, tcode
;
736 if (TREE_CODE (to
) == REFERENCE_TYPE
)
738 if (TREE_CODE (from
) == REFERENCE_TYPE
)
741 from
= TREE_TYPE (from
);
743 to
= strip_top_quals (to
);
744 from
= strip_top_quals (from
);
746 if ((TYPE_PTRFN_P (to
) || TYPE_PTRMEMFUNC_P (to
))
747 && expr
&& type_unknown_p (expr
))
749 expr
= instantiate_type (to
, expr
, tf_none
);
750 if (expr
== error_mark_node
)
752 from
= TREE_TYPE (expr
);
755 fcode
= TREE_CODE (from
);
756 tcode
= TREE_CODE (to
);
758 conv
= build1 (IDENTITY_CONV
, from
, expr
);
760 if (fcode
== FUNCTION_TYPE
)
762 from
= build_pointer_type (from
);
763 fcode
= TREE_CODE (from
);
764 conv
= build_conv (LVALUE_CONV
, from
, conv
);
766 else if (fcode
== ARRAY_TYPE
)
768 from
= build_pointer_type (TREE_TYPE (from
));
769 fcode
= TREE_CODE (from
);
770 conv
= build_conv (LVALUE_CONV
, from
, conv
);
772 else if (fromref
|| (expr
&& lvalue_p (expr
)))
773 conv
= build_conv (RVALUE_CONV
, from
, conv
);
775 /* Allow conversion between `__complex__' data types */
776 if (tcode
== COMPLEX_TYPE
&& fcode
== COMPLEX_TYPE
)
778 /* The standard conversion sequence to convert FROM to TO is
779 the standard conversion sequence to perform componentwise
781 tree part_conv
= standard_conversion
782 (TREE_TYPE (to
), TREE_TYPE (from
), NULL_TREE
);
786 conv
= build_conv (TREE_CODE (part_conv
), to
, conv
);
787 ICS_STD_RANK (conv
) = ICS_STD_RANK (part_conv
);
795 if (same_type_p (from
, to
))
798 if ((tcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (to
))
799 && expr
&& null_ptr_cst_p (expr
))
801 conv
= build_conv (STD_CONV
, to
, conv
);
803 else if ((tcode
== INTEGER_TYPE
&& fcode
== POINTER_TYPE
)
804 || (tcode
== POINTER_TYPE
&& fcode
== INTEGER_TYPE
))
806 /* For backwards brain damage compatibility, allow interconversion of
807 pointers and integers with a pedwarn. */
808 conv
= build_conv (STD_CONV
, to
, conv
);
809 ICS_BAD_FLAG (conv
) = 1;
811 else if (tcode
== ENUMERAL_TYPE
&& fcode
== INTEGER_TYPE
812 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
814 /* For backwards brain damage compatibility, allow interconversion of
815 enums and integers with a pedwarn. */
816 conv
= build_conv (STD_CONV
, to
, conv
);
817 ICS_BAD_FLAG (conv
) = 1;
819 else if (tcode
== POINTER_TYPE
&& fcode
== POINTER_TYPE
)
821 enum tree_code ufcode
= TREE_CODE (TREE_TYPE (from
));
822 enum tree_code utcode
= TREE_CODE (TREE_TYPE (to
));
824 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from
),
827 else if (utcode
== VOID_TYPE
&& ufcode
!= OFFSET_TYPE
828 && ufcode
!= FUNCTION_TYPE
)
830 from
= build_pointer_type
831 (cp_build_qualified_type (void_type_node
,
832 cp_type_quals (TREE_TYPE (from
))));
833 conv
= build_conv (PTR_CONV
, from
, conv
);
835 else if (ufcode
== OFFSET_TYPE
&& utcode
== OFFSET_TYPE
)
837 tree fbase
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from
));
838 tree tbase
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to
));
840 if (DERIVED_FROM_P (fbase
, tbase
)
841 && (same_type_ignoring_top_level_qualifiers_p
842 (TREE_TYPE (TREE_TYPE (from
)),
843 TREE_TYPE (TREE_TYPE (to
)))))
845 from
= build_ptrmem_type (tbase
, TREE_TYPE (TREE_TYPE (from
)));
846 conv
= build_conv (PMEM_CONV
, from
, conv
);
849 else if (IS_AGGR_TYPE (TREE_TYPE (from
))
850 && IS_AGGR_TYPE (TREE_TYPE (to
)))
852 if (DERIVED_FROM_P (TREE_TYPE (to
), TREE_TYPE (from
)))
855 cp_build_qualified_type (TREE_TYPE (to
),
856 cp_type_quals (TREE_TYPE (from
)));
857 from
= build_pointer_type (from
);
858 conv
= build_conv (PTR_CONV
, from
, conv
);
862 if (same_type_p (from
, to
))
864 else if (comp_ptr_ttypes (TREE_TYPE (to
), TREE_TYPE (from
)))
865 conv
= build_conv (QUAL_CONV
, to
, conv
);
866 else if (expr
&& string_conv_p (to
, expr
, 0))
867 /* converting from string constant to char *. */
868 conv
= build_conv (QUAL_CONV
, to
, conv
);
869 else if (ptr_reasonably_similar (TREE_TYPE (to
), TREE_TYPE (from
)))
871 conv
= build_conv (PTR_CONV
, to
, conv
);
872 ICS_BAD_FLAG (conv
) = 1;
879 else if (TYPE_PTRMEMFUNC_P (to
) && TYPE_PTRMEMFUNC_P (from
))
881 tree fromfn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from
));
882 tree tofn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to
));
883 tree fbase
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn
)));
884 tree tbase
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn
)));
886 if (!DERIVED_FROM_P (fbase
, tbase
)
887 || !same_type_p (TREE_TYPE (fromfn
), TREE_TYPE (tofn
))
888 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn
)),
889 TREE_CHAIN (TYPE_ARG_TYPES (tofn
)))
890 || cp_type_quals (fbase
) != cp_type_quals (tbase
))
893 from
= cp_build_qualified_type (tbase
, cp_type_quals (fbase
));
894 from
= build_cplus_method_type (from
, TREE_TYPE (fromfn
),
895 TREE_CHAIN (TYPE_ARG_TYPES (fromfn
)));
896 from
= build_ptrmemfunc_type (build_pointer_type (from
));
897 conv
= build_conv (PMEM_CONV
, from
, conv
);
899 else if (tcode
== BOOLEAN_TYPE
)
901 if (! (INTEGRAL_CODE_P (fcode
) || fcode
== REAL_TYPE
902 || fcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (from
)))
905 conv
= build_conv (STD_CONV
, to
, conv
);
906 if (fcode
== POINTER_TYPE
907 || (TYPE_PTRMEMFUNC_P (from
) && ICS_STD_RANK (conv
) < PBOOL_RANK
))
908 ICS_STD_RANK (conv
) = PBOOL_RANK
;
910 /* We don't check for ENUMERAL_TYPE here because there are no standard
911 conversions to enum type. */
912 else if (tcode
== INTEGER_TYPE
|| tcode
== BOOLEAN_TYPE
913 || tcode
== REAL_TYPE
)
915 if (! (INTEGRAL_CODE_P (fcode
) || fcode
== REAL_TYPE
))
917 conv
= build_conv (STD_CONV
, to
, conv
);
919 /* Give this a better rank if it's a promotion. */
920 if (to
== type_promotes_to (from
)
921 && ICS_STD_RANK (TREE_OPERAND (conv
, 0)) <= PROMO_RANK
)
922 ICS_STD_RANK (conv
) = PROMO_RANK
;
924 else if (IS_AGGR_TYPE (to
) && IS_AGGR_TYPE (from
)
925 && is_properly_derived_from (from
, to
))
927 if (TREE_CODE (conv
) == RVALUE_CONV
)
928 conv
= TREE_OPERAND (conv
, 0);
929 conv
= build_conv (BASE_CONV
, to
, conv
);
930 /* The derived-to-base conversion indicates the initialization
931 of a parameter with base type from an object of a derived
932 type. A temporary object is created to hold the result of
934 NEED_TEMPORARY_P (conv
) = 1;
942 /* Returns nonzero if T1 is reference-related to T2. */
945 reference_related_p (t1
, t2
)
949 t1
= TYPE_MAIN_VARIANT (t1
);
950 t2
= TYPE_MAIN_VARIANT (t2
);
954 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
955 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
957 return (same_type_p (t1
, t2
)
958 || (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
959 && DERIVED_FROM_P (t1
, t2
)));
962 /* Returns nonzero if T1 is reference-compatible with T2. */
965 reference_compatible_p (t1
, t2
)
971 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
972 reference-related to T2 and cv1 is the same cv-qualification as,
973 or greater cv-qualification than, cv2. */
974 return (reference_related_p (t1
, t2
)
975 && at_least_as_qualified_p (t1
, t2
));
978 /* Determine whether or not the EXPR (of class type S) can be
979 converted to T as in [over.match.ref]. */
982 convert_class_to_reference (t
, s
, expr
)
990 struct z_candidate
*candidates
;
991 struct z_candidate
*cand
;
995 Assuming that "cv1 T" is the underlying type of the reference
996 being initialized, and "cv S" is the type of the initializer
997 expression, with S a class type, the candidate functions are
1000 --The conversion functions of S and its base classes are
1001 considered. Those that are not hidden within S and yield type
1002 "reference to cv2 T2", where "cv1 T" is reference-compatible
1003 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
1005 The argument list has one argument, which is the initializer
1010 /* Conceptually, we should take the address of EXPR and put it in
1011 the argument list. Unfortunately, however, that can result in
1012 error messages, which we should not issue now because we are just
1013 trying to find a conversion operator. Therefore, we use NULL,
1014 cast to the appropriate type. */
1015 arglist
= build_int_2 (0, 0);
1016 TREE_TYPE (arglist
) = build_pointer_type (s
);
1017 arglist
= build_tree_list (NULL_TREE
, arglist
);
1019 for (conversions
= lookup_conversions (s
);
1021 conversions
= TREE_CHAIN (conversions
))
1023 tree fns
= TREE_VALUE (conversions
);
1025 for (; fns
; fns
= OVL_NEXT (fns
))
1027 tree f
= OVL_CURRENT (fns
);
1028 tree t2
= TREE_TYPE (TREE_TYPE (f
));
1029 struct z_candidate
*old_candidates
= candidates
;
1031 /* If this is a template function, try to get an exact
1033 if (TREE_CODE (f
) == TEMPLATE_DECL
)
1036 = add_template_candidate (candidates
,
1040 build_reference_type (t
),
1042 TREE_PURPOSE (conversions
),
1046 if (candidates
!= old_candidates
)
1048 /* Now, see if the conversion function really returns
1049 an lvalue of the appropriate type. From the
1050 point of view of unification, simply returning an
1051 rvalue of the right type is good enough. */
1053 t2
= TREE_TYPE (TREE_TYPE (f
));
1054 if (TREE_CODE (t2
) != REFERENCE_TYPE
1055 || !reference_compatible_p (t
, TREE_TYPE (t2
)))
1056 candidates
= candidates
->next
;
1059 else if (TREE_CODE (t2
) == REFERENCE_TYPE
1060 && reference_compatible_p (t
, TREE_TYPE (t2
)))
1062 = add_function_candidate (candidates
, f
, s
, arglist
,
1064 TREE_PURPOSE (conversions
),
1069 /* If none of the conversion functions worked out, let our caller
1071 if (!any_viable (candidates
))
1074 candidates
= splice_viable (candidates
);
1075 cand
= tourney (candidates
);
1079 conv
= build1 (IDENTITY_CONV
, s
, expr
);
1080 conv
= build_conv (USER_CONV
, TREE_TYPE (TREE_TYPE (cand
->fn
)),
1082 TREE_OPERAND (conv
, 1) = build_zc_wrapper (cand
);
1083 ICS_USER_FLAG (conv
) = 1;
1084 if (cand
->viable
== -1)
1085 ICS_BAD_FLAG (conv
) = 1;
1086 cand
->second_conv
= conv
;
1091 /* A reference of the indicated TYPE is being bound directly to the
1092 expression represented by the implicit conversion sequence CONV.
1093 Return a conversion sequence for this binding. */
1096 direct_reference_binding (type
, conv
)
1100 tree t
= TREE_TYPE (type
);
1104 When a parameter of reference type binds directly
1105 (_dcl.init.ref_) to an argument expression, the implicit
1106 conversion sequence is the identity conversion, unless the
1107 argument expression has a type that is a derived class of the
1108 parameter type, in which case the implicit conversion sequence is
1109 a derived-to-base Conversion.
1111 If the parameter binds directly to the result of applying a
1112 conversion function to the argument expression, the implicit
1113 conversion sequence is a user-defined conversion sequence
1114 (_over.ics.user_), with the second standard conversion sequence
1115 either an identity conversion or, if the conversion function
1116 returns an entity of a type that is a derived class of the
1117 parameter type, a derived-to-base conversion. */
1118 if (!same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (conv
)))
1120 /* Represent the derived-to-base conversion. */
1121 conv
= build_conv (BASE_CONV
, t
, conv
);
1122 /* We will actually be binding to the base-class subobject in
1123 the derived class, so we mark this conversion appropriately.
1124 That way, convert_like knows not to generate a temporary. */
1125 NEED_TEMPORARY_P (conv
) = 0;
1127 return build_conv (REF_BIND
, type
, conv
);
1130 /* Returns the conversion path from type FROM to reference type TO for
1131 purposes of reference binding. For lvalue binding, either pass a
1132 reference type to FROM or an lvalue expression to EXPR. If the
1133 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1134 the conversion returned. */
1137 reference_binding (rto
, rfrom
, expr
, flags
)
1138 tree rto
, rfrom
, expr
;
1141 tree conv
= NULL_TREE
;
1142 tree to
= TREE_TYPE (rto
);
1146 cp_lvalue_kind lvalue_p
= clk_none
;
1148 if (TREE_CODE (to
) == FUNCTION_TYPE
&& expr
&& type_unknown_p (expr
))
1150 expr
= instantiate_type (to
, expr
, tf_none
);
1151 if (expr
== error_mark_node
)
1153 from
= TREE_TYPE (expr
);
1156 if (TREE_CODE (from
) == REFERENCE_TYPE
)
1158 /* Anything with reference type is an lvalue. */
1159 lvalue_p
= clk_ordinary
;
1160 from
= TREE_TYPE (from
);
1163 lvalue_p
= real_lvalue_p (expr
);
1165 /* Figure out whether or not the types are reference-related and
1166 reference compatible. We have do do this after stripping
1167 references from FROM. */
1168 related_p
= reference_related_p (to
, from
);
1169 compatible_p
= reference_compatible_p (to
, from
);
1171 if (lvalue_p
&& compatible_p
)
1175 If the initializer expression
1177 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1178 is reference-compatible with "cv2 T2,"
1180 the reference is bound directly to the initializer exprssion
1182 conv
= build1 (IDENTITY_CONV
, from
, expr
);
1183 conv
= direct_reference_binding (rto
, conv
);
1184 if ((lvalue_p
& clk_bitfield
) != 0
1185 && CP_TYPE_CONST_NON_VOLATILE_P (to
))
1186 /* For the purposes of overload resolution, we ignore the fact
1187 this expression is a bitfield. (In particular,
1188 [over.ics.ref] says specifically that a function with a
1189 non-const reference parameter is viable even if the
1190 argument is a bitfield.)
1192 However, when we actually call the function we must create
1193 a temporary to which to bind the reference. If the
1194 reference is volatile, or isn't const, then we cannot make
1195 a temporary, so we just issue an error when the conversion
1197 NEED_TEMPORARY_P (conv
) = 1;
1200 else if (CLASS_TYPE_P (from
) && !(flags
& LOOKUP_NO_CONVERSION
))
1204 If the initializer exprsesion
1206 -- has a class type (i.e., T2 is a class type) can be
1207 implicitly converted to an lvalue of type "cv3 T3," where
1208 "cv1 T1" is reference-compatible with "cv3 T3". (this
1209 conversion is selected by enumerating the applicable
1210 conversion functions (_over.match.ref_) and choosing the
1211 best one through overload resolution. (_over.match_).
1213 the reference is bound to the lvalue result of the conversion
1214 in the second case. */
1215 conv
= convert_class_to_reference (to
, from
, expr
);
1217 return direct_reference_binding (rto
, conv
);
1220 /* From this point on, we conceptually need temporaries, even if we
1221 elide them. Only the cases above are "direct bindings". */
1222 if (flags
& LOOKUP_NO_TEMP_BIND
)
1227 When a parameter of reference type is not bound directly to an
1228 argument expression, the conversion sequence is the one required
1229 to convert the argument expression to the underlying type of the
1230 reference according to _over.best.ics_. Conceptually, this
1231 conversion sequence corresponds to copy-initializing a temporary
1232 of the underlying type with the argument expression. Any
1233 difference in top-level cv-qualification is subsumed by the
1234 initialization itself and does not constitute a conversion. */
1238 Otherwise, the reference shall be to a non-volatile const type. */
1239 if (!CP_TYPE_CONST_NON_VOLATILE_P (to
))
1244 If the initializer expression is an rvalue, with T2 a class type,
1245 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1246 is bound in one of the following ways:
1248 -- The reference is bound to the object represented by the rvalue
1249 or to a sub-object within that object.
1251 In this case, the implicit conversion sequence is supposed to be
1252 same as we would obtain by generating a temporary. Fortunately,
1253 if the types are reference compatible, then this is either an
1254 identity conversion or the derived-to-base conversion, just as
1255 for direct binding. */
1256 if (CLASS_TYPE_P (from
) && compatible_p
)
1258 conv
= build1 (IDENTITY_CONV
, from
, expr
);
1259 return direct_reference_binding (rto
, conv
);
1264 Otherwise, a temporary of type "cv1 T1" is created and
1265 initialized from the initializer expression using the rules for a
1266 non-reference copy initialization. If T1 is reference-related to
1267 T2, cv1 must be the same cv-qualification as, or greater
1268 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1269 if (related_p
&& !at_least_as_qualified_p (to
, from
))
1272 conv
= implicit_conversion (to
, from
, expr
, flags
);
1276 conv
= build_conv (REF_BIND
, rto
, conv
);
1277 /* This reference binding, unlike those above, requires the
1278 creation of a temporary. */
1279 NEED_TEMPORARY_P (conv
) = 1;
1284 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1285 to type TO. The optional expression EXPR may affect the conversion.
1286 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1290 implicit_conversion (to
, from
, expr
, flags
)
1291 tree to
, from
, expr
;
1295 struct z_candidate
*cand
;
1297 /* Resolve expressions like `A::p' that we thought might become
1298 pointers-to-members. */
1299 if (expr
&& TREE_CODE (expr
) == OFFSET_REF
)
1301 expr
= resolve_offset_ref (expr
);
1302 from
= TREE_TYPE (expr
);
1305 if (from
== error_mark_node
|| to
== error_mark_node
1306 || expr
== error_mark_node
)
1309 /* Make sure both the FROM and TO types are complete so that
1310 user-defined conversions are available. */
1311 complete_type (from
);
1314 if (TREE_CODE (to
) == REFERENCE_TYPE
)
1315 conv
= reference_binding (to
, from
, expr
, flags
);
1317 conv
= standard_conversion (to
, from
, expr
);
1321 else if (expr
!= NULL_TREE
1322 && (IS_AGGR_TYPE (from
)
1323 || IS_AGGR_TYPE (to
))
1324 && (flags
& LOOKUP_NO_CONVERSION
) == 0)
1326 cand
= build_user_type_conversion_1
1327 (to
, expr
, LOOKUP_ONLYCONVERTING
);
1329 conv
= cand
->second_conv
;
1331 /* We used to try to bind a reference to a temporary here, but that
1332 is now handled by the recursive call to this function at the end
1333 of reference_binding. */
1339 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1342 static struct z_candidate
*
1343 add_candidate (struct z_candidate
*candidates
,
1344 tree fn
, tree convs
, tree access_path
, tree
1345 conversion_path
, int viable
)
1347 struct z_candidate
*cand
1348 = (struct z_candidate
*) ggc_alloc_cleared (sizeof (struct z_candidate
));
1351 cand
->convs
= convs
;
1352 cand
->access_path
= access_path
;
1353 cand
->conversion_path
= conversion_path
;
1354 cand
->viable
= viable
;
1355 cand
->next
= candidates
;
1360 /* Create an overload candidate for the function or method FN called with
1361 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1362 to implicit_conversion.
1364 CTYPE, if non-NULL, is the type we want to pretend this function
1365 comes from for purposes of overload resolution. */
1367 static struct z_candidate
*
1368 add_function_candidate (struct z_candidate
*candidates
,
1369 tree fn
, tree ctype
, tree arglist
,
1370 tree access_path
, tree conversion_path
,
1373 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1376 tree parmnode
, argnode
;
1379 /* The `this', `in_chrg' and VTT arguments to constructors are not
1380 considered in overload resolution. */
1381 if (DECL_CONSTRUCTOR_P (fn
))
1383 parmlist
= skip_artificial_parms_for (fn
, parmlist
);
1384 arglist
= skip_artificial_parms_for (fn
, arglist
);
1387 len
= list_length (arglist
);
1388 convs
= make_tree_vec (len
);
1390 /* 13.3.2 - Viable functions [over.match.viable]
1391 First, to be a viable function, a candidate function shall have enough
1392 parameters to agree in number with the arguments in the list.
1394 We need to check this first; otherwise, checking the ICSes might cause
1395 us to produce an ill-formed template instantiation. */
1397 parmnode
= parmlist
;
1398 for (i
= 0; i
< len
; ++i
)
1400 if (parmnode
== NULL_TREE
|| parmnode
== void_list_node
)
1402 parmnode
= TREE_CHAIN (parmnode
);
1405 if (i
< len
&& parmnode
)
1408 /* Make sure there are default args for the rest of the parms. */
1409 else if (!sufficient_parms_p (parmnode
))
1415 /* Second, for F to be a viable function, there shall exist for each
1416 argument an implicit conversion sequence that converts that argument
1417 to the corresponding parameter of F. */
1419 parmnode
= parmlist
;
1422 for (i
= 0; i
< len
; ++i
)
1424 tree arg
= TREE_VALUE (argnode
);
1425 tree argtype
= lvalue_type (arg
);
1429 if (parmnode
== void_list_node
)
1432 is_this
= (i
== 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1433 && ! DECL_CONSTRUCTOR_P (fn
));
1437 tree parmtype
= TREE_VALUE (parmnode
);
1439 /* The type of the implicit object parameter ('this') for
1440 overload resolution is not always the same as for the
1441 function itself; conversion functions are considered to
1442 be members of the class being converted, and functions
1443 introduced by a using-declaration are considered to be
1444 members of the class that uses them.
1446 Since build_over_call ignores the ICS for the `this'
1447 parameter, we can just change the parm type. */
1448 if (ctype
&& is_this
)
1451 = build_qualified_type (ctype
,
1452 TYPE_QUALS (TREE_TYPE (parmtype
)));
1453 parmtype
= build_pointer_type (parmtype
);
1456 t
= implicit_conversion (parmtype
, argtype
, arg
, flags
);
1460 t
= build1 (IDENTITY_CONV
, argtype
, arg
);
1461 ICS_ELLIPSIS_FLAG (t
) = 1;
1465 ICS_THIS_FLAG (t
) = 1;
1467 TREE_VEC_ELT (convs
, i
) = t
;
1474 if (ICS_BAD_FLAG (t
))
1478 parmnode
= TREE_CHAIN (parmnode
);
1479 argnode
= TREE_CHAIN (argnode
);
1483 return add_candidate (candidates
, fn
, convs
, access_path
,
1484 conversion_path
, viable
);
1487 /* Create an overload candidate for the conversion function FN which will
1488 be invoked for expression OBJ, producing a pointer-to-function which
1489 will in turn be called with the argument list ARGLIST, and add it to
1490 CANDIDATES. FLAGS is passed on to implicit_conversion.
1492 Actually, we don't really care about FN; we care about the type it
1493 converts to. There may be multiple conversion functions that will
1494 convert to that type, and we rely on build_user_type_conversion_1 to
1495 choose the best one; so when we create our candidate, we record the type
1496 instead of the function. */
1498 static struct z_candidate
*
1499 add_conv_candidate (candidates
, fn
, obj
, arglist
, access_path
,
1501 struct z_candidate
*candidates
;
1502 tree fn
, obj
, arglist
;
1504 tree conversion_path
;
1506 tree totype
= TREE_TYPE (TREE_TYPE (fn
));
1507 int i
, len
, viable
, flags
;
1508 tree parmlist
, convs
, parmnode
, argnode
;
1510 for (parmlist
= totype
; TREE_CODE (parmlist
) != FUNCTION_TYPE
; )
1511 parmlist
= TREE_TYPE (parmlist
);
1512 parmlist
= TYPE_ARG_TYPES (parmlist
);
1514 len
= list_length (arglist
) + 1;
1515 convs
= make_tree_vec (len
);
1516 parmnode
= parmlist
;
1519 flags
= LOOKUP_NORMAL
;
1521 /* Don't bother looking up the same type twice. */
1522 if (candidates
&& candidates
->fn
== totype
)
1525 for (i
= 0; i
< len
; ++i
)
1527 tree arg
= i
== 0 ? obj
: TREE_VALUE (argnode
);
1528 tree argtype
= lvalue_type (arg
);
1532 t
= implicit_conversion (totype
, argtype
, arg
, flags
);
1533 else if (parmnode
== void_list_node
)
1536 t
= implicit_conversion (TREE_VALUE (parmnode
), argtype
, arg
, flags
);
1539 t
= build1 (IDENTITY_CONV
, argtype
, arg
);
1540 ICS_ELLIPSIS_FLAG (t
) = 1;
1543 TREE_VEC_ELT (convs
, i
) = t
;
1547 if (ICS_BAD_FLAG (t
))
1554 parmnode
= TREE_CHAIN (parmnode
);
1555 argnode
= TREE_CHAIN (argnode
);
1561 if (!sufficient_parms_p (parmnode
))
1564 return add_candidate (candidates
, totype
, convs
, access_path
,
1565 conversion_path
, viable
);
1568 static struct z_candidate
*
1569 build_builtin_candidate (candidates
, fnname
, type1
, type2
,
1570 args
, argtypes
, flags
)
1571 struct z_candidate
*candidates
;
1572 tree fnname
, type1
, type2
, *args
, *argtypes
;
1583 convs
= make_tree_vec (args
[2] ? 3 : (args
[1] ? 2 : 1));
1585 for (i
= 0; i
< 2; ++i
)
1590 t
= implicit_conversion (types
[i
], argtypes
[i
], args
[i
], flags
);
1594 /* We need something for printing the candidate. */
1595 t
= build1 (IDENTITY_CONV
, types
[i
], NULL_TREE
);
1597 else if (ICS_BAD_FLAG (t
))
1599 TREE_VEC_ELT (convs
, i
) = t
;
1602 /* For COND_EXPR we rearranged the arguments; undo that now. */
1605 TREE_VEC_ELT (convs
, 2) = TREE_VEC_ELT (convs
, 1);
1606 TREE_VEC_ELT (convs
, 1) = TREE_VEC_ELT (convs
, 0);
1607 t
= implicit_conversion (boolean_type_node
, argtypes
[2], args
[2], flags
);
1609 TREE_VEC_ELT (convs
, 0) = t
;
1614 return add_candidate (candidates
, fnname
, convs
,
1615 /*access_path=*/NULL_TREE
,
1616 /*conversion_path=*/NULL_TREE
,
1624 return COMPLETE_TYPE_P (complete_type (t
));
1627 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1630 promoted_arithmetic_type_p (type
)
1635 In this section, the term promoted integral type is used to refer
1636 to those integral types which are preserved by integral promotion
1637 (including e.g. int and long but excluding e.g. char).
1638 Similarly, the term promoted arithmetic type refers to promoted
1639 integral types plus floating types. */
1640 return ((INTEGRAL_TYPE_P (type
)
1641 && same_type_p (type_promotes_to (type
), type
))
1642 || TREE_CODE (type
) == REAL_TYPE
);
1645 /* Create any builtin operator overload candidates for the operator in
1646 question given the converted operand types TYPE1 and TYPE2. The other
1647 args are passed through from add_builtin_candidates to
1648 build_builtin_candidate.
1650 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1651 If CODE is requires candidates operands of the same type of the kind
1652 of which TYPE1 and TYPE2 are, we add both candidates
1653 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1655 static struct z_candidate
*
1656 add_builtin_candidate (candidates
, code
, code2
, fnname
, type1
, type2
,
1657 args
, argtypes
, flags
)
1658 struct z_candidate
*candidates
;
1659 enum tree_code code
, code2
;
1660 tree fnname
, type1
, type2
, *args
, *argtypes
;
1665 case POSTINCREMENT_EXPR
:
1666 case POSTDECREMENT_EXPR
:
1667 args
[1] = integer_zero_node
;
1668 type2
= integer_type_node
;
1677 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1678 and VQ is either volatile or empty, there exist candidate operator
1679 functions of the form
1680 VQ T& operator++(VQ T&);
1681 T operator++(VQ T&, int);
1682 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1683 type other than bool, and VQ is either volatile or empty, there exist
1684 candidate operator functions of the form
1685 VQ T& operator--(VQ T&);
1686 T operator--(VQ T&, int);
1687 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1688 complete object type, and VQ is either volatile or empty, there exist
1689 candidate operator functions of the form
1690 T*VQ& operator++(T*VQ&);
1691 T*VQ& operator--(T*VQ&);
1692 T* operator++(T*VQ&, int);
1693 T* operator--(T*VQ&, int); */
1695 case POSTDECREMENT_EXPR
:
1696 case PREDECREMENT_EXPR
:
1697 if (TREE_CODE (type1
) == BOOLEAN_TYPE
)
1699 case POSTINCREMENT_EXPR
:
1700 case PREINCREMENT_EXPR
:
1701 if (ARITHMETIC_TYPE_P (type1
) || TYPE_PTROB_P (type1
))
1703 type1
= build_reference_type (type1
);
1708 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1709 exist candidate operator functions of the form
1713 8 For every function type T, there exist candidate operator functions of
1715 T& operator*(T*); */
1718 if (TREE_CODE (type1
) == POINTER_TYPE
1719 && (TYPE_PTROB_P (type1
)
1720 || TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
))
1724 /* 9 For every type T, there exist candidate operator functions of the form
1727 10For every promoted arithmetic type T, there exist candidate operator
1728 functions of the form
1732 case CONVERT_EXPR
: /* unary + */
1733 if (TREE_CODE (type1
) == POINTER_TYPE
1734 && TREE_CODE (TREE_TYPE (type1
)) != OFFSET_TYPE
)
1737 if (ARITHMETIC_TYPE_P (type1
))
1741 /* 11For every promoted integral type T, there exist candidate operator
1742 functions of the form
1746 if (INTEGRAL_TYPE_P (type1
))
1750 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1751 is the same type as C2 or is a derived class of C2, T is a complete
1752 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1753 there exist candidate operator functions of the form
1754 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1755 where CV12 is the union of CV1 and CV2. */
1758 if (TREE_CODE (type1
) == POINTER_TYPE
1759 && (TYPE_PTRMEMFUNC_P (type2
) || TYPE_PTRMEM_P (type2
)))
1761 tree c1
= TREE_TYPE (type1
);
1762 tree c2
= (TYPE_PTRMEMFUNC_P (type2
)
1763 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2
)))
1764 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2
)));
1766 if (IS_AGGR_TYPE (c1
) && DERIVED_FROM_P (c2
, c1
)
1767 && (TYPE_PTRMEMFUNC_P (type2
)
1768 || is_complete (TREE_TYPE (TREE_TYPE (type2
)))))
1773 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1774 didate operator functions of the form
1779 bool operator<(L, R);
1780 bool operator>(L, R);
1781 bool operator<=(L, R);
1782 bool operator>=(L, R);
1783 bool operator==(L, R);
1784 bool operator!=(L, R);
1785 where LR is the result of the usual arithmetic conversions between
1788 14For every pair of types T and I, where T is a cv-qualified or cv-
1789 unqualified complete object type and I is a promoted integral type,
1790 there exist candidate operator functions of the form
1791 T* operator+(T*, I);
1792 T& operator[](T*, I);
1793 T* operator-(T*, I);
1794 T* operator+(I, T*);
1795 T& operator[](I, T*);
1797 15For every T, where T is a pointer to complete object type, there exist
1798 candidate operator functions of the form112)
1799 ptrdiff_t operator-(T, T);
1801 16For every pointer or enumeration type T, there exist candidate operator
1802 functions of the form
1803 bool operator<(T, T);
1804 bool operator>(T, T);
1805 bool operator<=(T, T);
1806 bool operator>=(T, T);
1807 bool operator==(T, T);
1808 bool operator!=(T, T);
1810 17For every pointer to member type T, there exist candidate operator
1811 functions of the form
1812 bool operator==(T, T);
1813 bool operator!=(T, T); */
1816 if (TYPE_PTROB_P (type1
) && TYPE_PTROB_P (type2
))
1818 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1820 type2
= ptrdiff_type_node
;
1824 case TRUNC_DIV_EXPR
:
1825 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1831 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
1832 || (TYPE_PTRMEM_P (type1
) && TYPE_PTRMEM_P (type2
)))
1834 if ((TYPE_PTRMEMFUNC_P (type1
) || TYPE_PTRMEM_P (type1
))
1835 && null_ptr_cst_p (args
[1]))
1840 if ((TYPE_PTRMEMFUNC_P (type2
) || TYPE_PTRMEM_P (type2
))
1841 && null_ptr_cst_p (args
[0]))
1853 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1855 if (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
1857 if (TREE_CODE (type1
) == ENUMERAL_TYPE
&& TREE_CODE (type2
) == ENUMERAL_TYPE
)
1859 if (TYPE_PTR_P (type1
) && null_ptr_cst_p (args
[1]))
1864 if (null_ptr_cst_p (args
[0]) && TYPE_PTR_P (type2
))
1872 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1875 if (INTEGRAL_TYPE_P (type1
) && TYPE_PTROB_P (type2
))
1877 type1
= ptrdiff_type_node
;
1880 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1882 type2
= ptrdiff_type_node
;
1887 /* 18For every pair of promoted integral types L and R, there exist candi-
1888 date operator functions of the form
1895 where LR is the result of the usual arithmetic conversions between
1898 case TRUNC_MOD_EXPR
:
1904 if (INTEGRAL_TYPE_P (type1
) && INTEGRAL_TYPE_P (type2
))
1908 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1909 type, VQ is either volatile or empty, and R is a promoted arithmetic
1910 type, there exist candidate operator functions of the form
1911 VQ L& operator=(VQ L&, R);
1912 VQ L& operator*=(VQ L&, R);
1913 VQ L& operator/=(VQ L&, R);
1914 VQ L& operator+=(VQ L&, R);
1915 VQ L& operator-=(VQ L&, R);
1917 20For every pair T, VQ), where T is any type and VQ is either volatile
1918 or empty, there exist candidate operator functions of the form
1919 T*VQ& operator=(T*VQ&, T*);
1921 21For every pair T, VQ), where T is a pointer to member type and VQ is
1922 either volatile or empty, there exist candidate operator functions of
1924 VQ T& operator=(VQ T&, T);
1926 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1927 unqualified complete object type, VQ is either volatile or empty, and
1928 I is a promoted integral type, there exist candidate operator func-
1930 T*VQ& operator+=(T*VQ&, I);
1931 T*VQ& operator-=(T*VQ&, I);
1933 23For every triple L, VQ, R), where L is an integral or enumeration
1934 type, VQ is either volatile or empty, and R is a promoted integral
1935 type, there exist candidate operator functions of the form
1937 VQ L& operator%=(VQ L&, R);
1938 VQ L& operator<<=(VQ L&, R);
1939 VQ L& operator>>=(VQ L&, R);
1940 VQ L& operator&=(VQ L&, R);
1941 VQ L& operator^=(VQ L&, R);
1942 VQ L& operator|=(VQ L&, R); */
1949 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1951 type2
= ptrdiff_type_node
;
1955 case TRUNC_DIV_EXPR
:
1956 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1960 case TRUNC_MOD_EXPR
:
1966 if (INTEGRAL_TYPE_P (type1
) && INTEGRAL_TYPE_P (type2
))
1971 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1973 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
1974 || (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
1975 || (TYPE_PTRMEM_P (type1
) && TYPE_PTRMEM_P (type2
))
1976 || ((TYPE_PTRMEMFUNC_P (type1
)
1977 || TREE_CODE (type1
) == POINTER_TYPE
)
1978 && null_ptr_cst_p (args
[1])))
1988 type1
= build_reference_type (type1
);
1994 For every pair of promoted arithmetic types L and R, there
1995 exist candidate operator functions of the form
1997 LR operator?(bool, L, R);
1999 where LR is the result of the usual arithmetic conversions
2000 between types L and R.
2002 For every type T, where T is a pointer or pointer-to-member
2003 type, there exist candidate operator functions of the form T
2004 operator?(bool, T, T); */
2006 if (promoted_arithmetic_type_p (type1
)
2007 && promoted_arithmetic_type_p (type2
))
2011 /* Otherwise, the types should be pointers. */
2012 if (!(TREE_CODE (type1
) == POINTER_TYPE
2013 || TYPE_PTRMEM_P (type1
)
2014 || TYPE_PTRMEMFUNC_P (type1
))
2015 || !(TREE_CODE (type2
) == POINTER_TYPE
2016 || TYPE_PTRMEM_P (type2
)
2017 || TYPE_PTRMEMFUNC_P (type2
)))
2020 /* We don't check that the two types are the same; the logic
2021 below will actually create two candidates; one in which both
2022 parameter types are TYPE1, and one in which both parameter
2026 /* These arguments do not make for a valid overloaded operator. */
2033 /* If we're dealing with two pointer types or two enumeral types,
2034 we need candidates for both of them. */
2035 if (type2
&& !same_type_p (type1
, type2
)
2036 && TREE_CODE (type1
) == TREE_CODE (type2
)
2037 && (TREE_CODE (type1
) == REFERENCE_TYPE
2038 || (TREE_CODE (type1
) == POINTER_TYPE
2039 && TYPE_PTRMEM_P (type1
) == TYPE_PTRMEM_P (type2
))
2040 || TYPE_PTRMEMFUNC_P (type1
)
2041 || IS_AGGR_TYPE (type1
)
2042 || TREE_CODE (type1
) == ENUMERAL_TYPE
))
2044 candidates
= build_builtin_candidate
2045 (candidates
, fnname
, type1
, type1
, args
, argtypes
, flags
);
2046 return build_builtin_candidate
2047 (candidates
, fnname
, type2
, type2
, args
, argtypes
, flags
);
2050 return build_builtin_candidate
2051 (candidates
, fnname
, type1
, type2
, args
, argtypes
, flags
);
2055 type_decays_to (type
)
2058 if (TREE_CODE (type
) == ARRAY_TYPE
)
2059 return build_pointer_type (TREE_TYPE (type
));
2060 if (TREE_CODE (type
) == FUNCTION_TYPE
)
2061 return build_pointer_type (type
);
2065 /* There are three conditions of builtin candidates:
2067 1) bool-taking candidates. These are the same regardless of the input.
2068 2) pointer-pair taking candidates. These are generated for each type
2069 one of the input types converts to.
2070 3) arithmetic candidates. According to the standard, we should generate
2071 all of these, but I'm trying not to...
2073 Here we generate a superset of the possible candidates for this particular
2074 case. That is a subset of the full set the standard defines, plus some
2075 other cases which the standard disallows. add_builtin_candidate will
2076 filter out the invalid set. */
2078 static struct z_candidate
*
2079 add_builtin_candidates (candidates
, code
, code2
, fnname
, args
, flags
)
2080 struct z_candidate
*candidates
;
2081 enum tree_code code
, code2
;
2087 tree type
, argtypes
[3];
2088 /* TYPES[i] is the set of possible builtin-operator parameter types
2089 we will consider for the Ith argument. These are represented as
2090 a TREE_LIST; the TREE_VALUE of each node is the potential
2094 for (i
= 0; i
< 3; ++i
)
2097 argtypes
[i
] = lvalue_type (args
[i
]);
2099 argtypes
[i
] = NULL_TREE
;
2104 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2105 and VQ is either volatile or empty, there exist candidate operator
2106 functions of the form
2107 VQ T& operator++(VQ T&); */
2109 case POSTINCREMENT_EXPR
:
2110 case PREINCREMENT_EXPR
:
2111 case POSTDECREMENT_EXPR
:
2112 case PREDECREMENT_EXPR
:
2117 /* 24There also exist candidate operator functions of the form
2118 bool operator!(bool);
2119 bool operator&&(bool, bool);
2120 bool operator||(bool, bool); */
2122 case TRUTH_NOT_EXPR
:
2123 return build_builtin_candidate
2124 (candidates
, fnname
, boolean_type_node
,
2125 NULL_TREE
, args
, argtypes
, flags
);
2127 case TRUTH_ORIF_EXPR
:
2128 case TRUTH_ANDIF_EXPR
:
2129 return build_builtin_candidate
2130 (candidates
, fnname
, boolean_type_node
,
2131 boolean_type_node
, args
, argtypes
, flags
);
2152 types
[0] = types
[1] = NULL_TREE
;
2154 for (i
= 0; i
< 2; ++i
)
2158 else if (IS_AGGR_TYPE (argtypes
[i
]))
2162 if (i
== 0 && code
== MODIFY_EXPR
&& code2
== NOP_EXPR
)
2165 convs
= lookup_conversions (argtypes
[i
]);
2167 if (code
== COND_EXPR
)
2169 if (real_lvalue_p (args
[i
]))
2170 types
[i
] = tree_cons
2171 (NULL_TREE
, build_reference_type (argtypes
[i
]), types
[i
]);
2173 types
[i
] = tree_cons
2174 (NULL_TREE
, TYPE_MAIN_VARIANT (argtypes
[i
]), types
[i
]);
2180 for (; convs
; convs
= TREE_CHAIN (convs
))
2182 type
= TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs
))));
2185 && (TREE_CODE (type
) != REFERENCE_TYPE
2186 || CP_TYPE_CONST_P (TREE_TYPE (type
))))
2189 if (code
== COND_EXPR
&& TREE_CODE (type
) == REFERENCE_TYPE
)
2190 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2192 type
= non_reference (type
);
2193 if (i
!= 0 || ! ref1
)
2195 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
2196 if (enum_p
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
2197 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2198 if (INTEGRAL_TYPE_P (type
))
2199 type
= type_promotes_to (type
);
2202 if (! value_member (type
, types
[i
]))
2203 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2208 if (code
== COND_EXPR
&& real_lvalue_p (args
[i
]))
2209 types
[i
] = tree_cons
2210 (NULL_TREE
, build_reference_type (argtypes
[i
]), types
[i
]);
2211 type
= non_reference (argtypes
[i
]);
2212 if (i
!= 0 || ! ref1
)
2214 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
2215 if (enum_p
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
2216 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2217 if (INTEGRAL_TYPE_P (type
))
2218 type
= type_promotes_to (type
);
2220 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2224 /* Run through the possible parameter types of both arguments,
2225 creating candidates with those parameter types. */
2226 for (; types
[0]; types
[0] = TREE_CHAIN (types
[0]))
2229 for (type
= types
[1]; type
; type
= TREE_CHAIN (type
))
2230 candidates
= add_builtin_candidate
2231 (candidates
, code
, code2
, fnname
, TREE_VALUE (types
[0]),
2232 TREE_VALUE (type
), args
, argtypes
, flags
);
2234 candidates
= add_builtin_candidate
2235 (candidates
, code
, code2
, fnname
, TREE_VALUE (types
[0]),
2236 NULL_TREE
, args
, argtypes
, flags
);
2243 /* If TMPL can be successfully instantiated as indicated by
2244 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2246 TMPL is the template. EXPLICIT_TARGS are any explicit template
2247 arguments. ARGLIST is the arguments provided at the call-site.
2248 The RETURN_TYPE is the desired type for conversion operators. If
2249 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2250 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2251 add_conv_candidate. */
2253 static struct z_candidate
*
2254 add_template_candidate_real (candidates
, tmpl
, ctype
, explicit_targs
,
2255 arglist
, return_type
, access_path
,
2256 conversion_path
, flags
, obj
, strict
)
2257 struct z_candidate
*candidates
;
2258 tree tmpl
, ctype
, explicit_targs
, arglist
, return_type
;
2260 tree conversion_path
;
2263 unification_kind_t strict
;
2265 int ntparms
= DECL_NTPARMS (tmpl
);
2266 tree targs
= make_tree_vec (ntparms
);
2267 tree args_without_in_chrg
= arglist
;
2268 struct z_candidate
*cand
;
2272 /* We don't do deduction on the in-charge parameter, the VTT
2273 parameter or 'this'. */
2274 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl
))
2275 args_without_in_chrg
= TREE_CHAIN (args_without_in_chrg
);
2277 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl
)
2278 || DECL_BASE_CONSTRUCTOR_P (tmpl
))
2279 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl
)))
2280 args_without_in_chrg
= TREE_CHAIN (args_without_in_chrg
);
2282 i
= fn_type_unification (tmpl
, explicit_targs
, targs
,
2283 args_without_in_chrg
,
2284 return_type
, strict
, -1);
2289 fn
= instantiate_template (tmpl
, targs
);
2290 if (fn
== error_mark_node
)
2295 A member function template is never instantiated to perform the
2296 copy of a class object to an object of its class type.
2298 It's a little unclear what this means; the standard explicitly
2299 does allow a template to be used to copy a class. For example,
2304 template <class T> A(const T&);
2307 void g () { A a (f ()); }
2309 the member template will be used to make the copy. The section
2310 quoted above appears in the paragraph that forbids constructors
2311 whose only parameter is (a possibly cv-qualified variant of) the
2312 class type, and a logical interpretation is that the intent was
2313 to forbid the instantiation of member templates which would then
2315 if (DECL_CONSTRUCTOR_P (fn
) && list_length (arglist
) == 2)
2317 tree arg_types
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
2318 if (arg_types
&& same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types
)),
2323 if (obj
!= NULL_TREE
)
2324 /* Aha, this is a conversion function. */
2325 cand
= add_conv_candidate (candidates
, fn
, obj
, access_path
,
2326 conversion_path
, arglist
);
2328 cand
= add_function_candidate (candidates
, fn
, ctype
,
2329 arglist
, access_path
,
2330 conversion_path
, flags
);
2331 if (DECL_TI_TEMPLATE (fn
) != tmpl
)
2332 /* This situation can occur if a member template of a template
2333 class is specialized. Then, instantiate_template might return
2334 an instantiation of the specialization, in which case the
2335 DECL_TI_TEMPLATE field will point at the original
2336 specialization. For example:
2338 template <class T> struct S { template <class U> void f(U);
2339 template <> void f(int) {}; };
2343 Here, TMPL will be template <class U> S<double>::f(U).
2344 And, instantiate template will give us the specialization
2345 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2346 for this will point at template <class T> template <> S<T>::f(int),
2347 so that we can find the definition. For the purposes of
2348 overload resolution, however, we want the original TMPL. */
2349 cand
->template = tree_cons (tmpl
, targs
, NULL_TREE
);
2351 cand
->template = DECL_TEMPLATE_INFO (fn
);
2357 static struct z_candidate
*
2358 add_template_candidate (candidates
, tmpl
, ctype
, explicit_targs
,
2359 arglist
, return_type
, access_path
,
2360 conversion_path
, flags
, strict
)
2361 struct z_candidate
*candidates
;
2362 tree tmpl
, ctype
, explicit_targs
, arglist
, return_type
;
2364 tree conversion_path
;
2366 unification_kind_t strict
;
2369 add_template_candidate_real (candidates
, tmpl
, ctype
,
2370 explicit_targs
, arglist
, return_type
,
2371 access_path
, conversion_path
,
2372 flags
, NULL_TREE
, strict
);
2376 static struct z_candidate
*
2377 add_template_conv_candidate (candidates
, tmpl
, obj
, arglist
, return_type
,
2378 access_path
, conversion_path
)
2379 struct z_candidate
*candidates
;
2380 tree tmpl
, obj
, arglist
, return_type
;
2382 tree conversion_path
;
2385 add_template_candidate_real (candidates
, tmpl
, NULL_TREE
, NULL_TREE
,
2386 arglist
, return_type
, access_path
,
2387 conversion_path
, 0, obj
, DEDUCE_CONV
);
2393 struct z_candidate
*cands
;
2395 for (; cands
; cands
= cands
->next
)
2396 if (pedantic
? cands
->viable
== 1 : cands
->viable
)
2402 any_strictly_viable (cands
)
2403 struct z_candidate
*cands
;
2405 for (; cands
; cands
= cands
->next
)
2406 if (cands
->viable
== 1)
2411 static struct z_candidate
*
2412 splice_viable (cands
)
2413 struct z_candidate
*cands
;
2415 struct z_candidate
**p
= &cands
;
2419 if (pedantic
? (*p
)->viable
== 1 : (*p
)->viable
)
2432 /* Fix this to work on non-lvalues. */
2433 return build_unary_op (ADDR_EXPR
, obj
, 0);
2437 print_z_candidates (candidates
)
2438 struct z_candidate
*candidates
;
2440 const char *str
= "candidates are:";
2441 for (; candidates
; candidates
= candidates
->next
)
2443 if (TREE_CODE (candidates
->fn
) == IDENTIFIER_NODE
)
2445 if (TREE_VEC_LENGTH (candidates
->convs
) == 3)
2446 error ("%s %D(%T, %T, %T) <built-in>", str
, candidates
->fn
,
2447 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)),
2448 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 1)),
2449 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 2)));
2450 else if (TREE_VEC_LENGTH (candidates
->convs
) == 2)
2451 error ("%s %D(%T, %T) <built-in>", str
, candidates
->fn
,
2452 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)),
2453 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 1)));
2455 error ("%s %D(%T) <built-in>", str
, candidates
->fn
,
2456 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)));
2458 else if (TYPE_P (candidates
->fn
))
2459 error ("%s %T <conversion>", str
, candidates
->fn
);
2461 cp_error_at ("%s %+#D%s", str
, candidates
->fn
,
2462 candidates
->viable
== -1 ? " <near match>" : "");
2467 /* Returns the best overload candidate to perform the requested
2468 conversion. This function is used for three the overloading situations
2469 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2470 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2471 per [dcl.init.ref], so we ignore temporary bindings. */
2473 static struct z_candidate
*
2474 build_user_type_conversion_1 (totype
, expr
, flags
)
2478 struct z_candidate
*candidates
, *cand
;
2479 tree fromtype
= TREE_TYPE (expr
);
2480 tree ctors
= NULL_TREE
, convs
= NULL_TREE
, *p
;
2481 tree args
= NULL_TREE
;
2482 tree templates
= NULL_TREE
;
2484 /* We represent conversion within a hierarchy using RVALUE_CONV and
2485 BASE_CONV, as specified by [over.best.ics]; these become plain
2486 constructor calls, as specified in [dcl.init]. */
2487 my_friendly_assert (!IS_AGGR_TYPE (fromtype
) || !IS_AGGR_TYPE (totype
)
2488 || !DERIVED_FROM_P (totype
, fromtype
), 20011226);
2490 if (IS_AGGR_TYPE (totype
))
2491 ctors
= lookup_fnfields (TYPE_BINFO (totype
),
2492 complete_ctor_identifier
,
2495 if (IS_AGGR_TYPE (fromtype
))
2496 convs
= lookup_conversions (fromtype
);
2499 flags
|= LOOKUP_NO_CONVERSION
;
2505 ctors
= BASELINK_FUNCTIONS (ctors
);
2507 t
= build_int_2 (0, 0);
2508 TREE_TYPE (t
) = build_pointer_type (totype
);
2509 args
= build_tree_list (NULL_TREE
, expr
);
2510 /* We should never try to call the abstract or base constructor
2512 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors
))
2513 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors
)),
2515 args
= tree_cons (NULL_TREE
, t
, args
);
2517 for (; ctors
; ctors
= OVL_NEXT (ctors
))
2519 tree ctor
= OVL_CURRENT (ctors
);
2520 if (DECL_NONCONVERTING_P (ctor
))
2523 if (TREE_CODE (ctor
) == TEMPLATE_DECL
)
2525 templates
= tree_cons (NULL_TREE
, ctor
, templates
);
2527 add_template_candidate (candidates
, ctor
, totype
,
2528 NULL_TREE
, args
, NULL_TREE
,
2529 TYPE_BINFO (totype
),
2530 TYPE_BINFO (totype
),
2535 candidates
= add_function_candidate (candidates
, ctor
, totype
,
2536 args
, TYPE_BINFO (totype
),
2537 TYPE_BINFO (totype
),
2541 candidates
->second_conv
= build1 (IDENTITY_CONV
, totype
, NULL_TREE
);
2545 args
= build_tree_list (NULL_TREE
, build_this (expr
));
2547 for (; convs
; convs
= TREE_CHAIN (convs
))
2550 tree conversion_path
= TREE_PURPOSE (convs
);
2551 int convflags
= LOOKUP_NO_CONVERSION
;
2553 /* If we are called to convert to a reference type, we are trying to
2554 find an lvalue binding, so don't even consider temporaries. If
2555 we don't find an lvalue binding, the caller will try again to
2556 look for a temporary binding. */
2557 if (TREE_CODE (totype
) == REFERENCE_TYPE
)
2558 convflags
|= LOOKUP_NO_TEMP_BIND
;
2560 for (fns
= TREE_VALUE (convs
); fns
; fns
= OVL_NEXT (fns
))
2562 tree fn
= OVL_CURRENT (fns
);
2563 struct z_candidate
*old_candidates
= candidates
;
2565 /* [over.match.funcs] For conversion functions, the function
2566 is considered to be a member of the class of the implicit
2567 object argument for the purpose of defining the type of
2568 the implicit object parameter.
2570 So we pass fromtype as CTYPE to add_*_candidate. */
2572 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2574 templates
= tree_cons (NULL_TREE
, fn
, templates
);
2576 add_template_candidate (candidates
, fn
, fromtype
, NULL_TREE
,
2577 args
, totype
, TYPE_BINFO (fromtype
),
2583 candidates
= add_function_candidate (candidates
, fn
, fromtype
,
2585 TYPE_BINFO (fromtype
),
2589 if (candidates
!= old_candidates
)
2591 tree ics
= implicit_conversion
2592 (totype
, TREE_TYPE (TREE_TYPE (candidates
->fn
)),
2595 candidates
->second_conv
= ics
;
2597 if (ics
== NULL_TREE
)
2598 candidates
->viable
= 0;
2599 else if (candidates
->viable
== 1 && ICS_BAD_FLAG (ics
))
2600 candidates
->viable
= -1;
2605 if (! any_viable (candidates
))
2608 candidates
= splice_viable (candidates
);
2609 cand
= tourney (candidates
);
2613 if (flags
& LOOKUP_COMPLAIN
)
2615 error ("conversion from `%T' to `%T' is ambiguous",
2617 print_z_candidates (candidates
);
2620 cand
= candidates
; /* any one will do */
2621 cand
->second_conv
= build1 (AMBIG_CONV
, totype
, expr
);
2622 ICS_USER_FLAG (cand
->second_conv
) = 1;
2623 /* Don't set ICS_BAD_FLAG; an ambiguous conversion is no worse than
2624 another user-defined conversion. */
2629 for (p
= &(cand
->second_conv
); TREE_CODE (*p
) != IDENTITY_CONV
; )
2630 p
= &(TREE_OPERAND (*p
, 0));
2634 (DECL_CONSTRUCTOR_P (cand
->fn
)
2635 ? totype
: non_reference (TREE_TYPE (TREE_TYPE (cand
->fn
)))),
2636 expr
, build_zc_wrapper (cand
));
2638 ICS_USER_FLAG (cand
->second_conv
) = ICS_USER_FLAG (*p
) = 1;
2639 if (cand
->viable
== -1)
2640 ICS_BAD_FLAG (cand
->second_conv
) = ICS_BAD_FLAG (*p
) = 1;
2646 build_user_type_conversion (totype
, expr
, flags
)
2650 struct z_candidate
*cand
2651 = build_user_type_conversion_1 (totype
, expr
, flags
);
2655 if (TREE_CODE (cand
->second_conv
) == AMBIG_CONV
)
2656 return error_mark_node
;
2657 return convert_from_reference (convert_like (cand
->second_conv
, expr
));
2662 /* Do any initial processing on the arguments to a function call. */
2669 for (t
= args
; t
; t
= TREE_CHAIN (t
))
2671 tree arg
= TREE_VALUE (t
);
2673 if (arg
== error_mark_node
)
2674 return error_mark_node
;
2675 else if (VOID_TYPE_P (TREE_TYPE (arg
)))
2677 error ("invalid use of void expression");
2678 return error_mark_node
;
2680 else if (TREE_CODE (arg
) == OFFSET_REF
)
2681 arg
= resolve_offset_ref (arg
);
2682 arg
= convert_from_reference (arg
);
2683 TREE_VALUE (t
) = arg
;
2688 /* Return an expression for a call to FN (a namespace-scope function)
2692 build_new_function_call (fn
, args
)
2695 struct z_candidate
*candidates
= 0, *cand
;
2696 tree explicit_targs
= NULL_TREE
;
2697 int template_only
= 0;
2699 /* Check FN and ARGS. */
2700 my_friendly_assert (TREE_CODE (fn
) == FUNCTION_DECL
2701 || TREE_CODE (fn
) == TEMPLATE_DECL
2702 || TREE_CODE (fn
) == OVERLOAD
2703 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
,
2705 my_friendly_assert (!args
|| TREE_CODE (args
) == TREE_LIST
,
2708 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
2710 explicit_targs
= TREE_OPERAND (fn
, 1);
2711 fn
= TREE_OPERAND (fn
, 0);
2715 if (really_overloaded_fn (fn
)
2716 || TREE_CODE (fn
) == TEMPLATE_DECL
)
2719 tree templates
= NULL_TREE
;
2721 args
= resolve_args (args
);
2723 if (args
== error_mark_node
)
2724 return error_mark_node
;
2726 for (t1
= fn
; t1
; t1
= OVL_NEXT (t1
))
2728 tree t
= OVL_CURRENT (t1
);
2730 if (TREE_CODE (t
) == TEMPLATE_DECL
)
2732 templates
= tree_cons (NULL_TREE
, t
, templates
);
2733 candidates
= add_template_candidate
2734 (candidates
, t
, NULL_TREE
, explicit_targs
, args
,
2736 /*access_path=*/NULL_TREE
, /*conversion_path=*/NULL_TREE
,
2737 LOOKUP_NORMAL
, DEDUCE_CALL
);
2739 else if (! template_only
)
2740 candidates
= add_function_candidate
2741 (candidates
, t
, NULL_TREE
, args
, /*access_path=*/NULL_TREE
,
2742 /*conversion_path=*/NULL_TREE
, LOOKUP_NORMAL
);
2745 if (! any_viable (candidates
))
2747 if (candidates
&& ! candidates
->next
)
2748 return build_function_call (candidates
->fn
, args
);
2749 error ("no matching function for call to `%D(%A)'",
2750 DECL_NAME (OVL_CURRENT (fn
)), args
);
2752 print_z_candidates (candidates
);
2753 return error_mark_node
;
2755 candidates
= splice_viable (candidates
);
2756 cand
= tourney (candidates
);
2760 error ("call of overloaded `%D(%A)' is ambiguous",
2761 DECL_NAME (OVL_FUNCTION (fn
)), args
);
2762 print_z_candidates (candidates
);
2763 return error_mark_node
;
2766 return build_over_call (cand
, args
, LOOKUP_NORMAL
);
2769 /* This is not really overloaded. */
2770 fn
= OVL_CURRENT (fn
);
2772 return build_function_call (fn
, args
);
2776 build_object_call (obj
, args
)
2779 struct z_candidate
*candidates
= 0, *cand
;
2780 tree fns
, convs
, mem_args
= NULL_TREE
;
2781 tree type
= TREE_TYPE (obj
);
2783 if (TYPE_PTRMEMFUNC_P (type
))
2785 /* It's no good looking for an overloaded operator() on a
2786 pointer-to-member-function. */
2787 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj
);
2788 return error_mark_node
;
2791 fns
= lookup_fnfields (TYPE_BINFO (type
), ansi_opname (CALL_EXPR
), 1);
2792 if (fns
== error_mark_node
)
2793 return error_mark_node
;
2795 args
= resolve_args (args
);
2797 if (args
== error_mark_node
)
2798 return error_mark_node
;
2802 tree base
= BINFO_TYPE (BASELINK_BINFO (fns
));
2803 mem_args
= tree_cons (NULL_TREE
, build_this (obj
), args
);
2805 for (fns
= BASELINK_FUNCTIONS (fns
); fns
; fns
= OVL_NEXT (fns
))
2807 tree fn
= OVL_CURRENT (fns
);
2808 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2811 = add_template_candidate (candidates
, fn
, base
, NULL_TREE
,
2812 mem_args
, NULL_TREE
,
2815 LOOKUP_NORMAL
, DEDUCE_CALL
);
2818 candidates
= add_function_candidate
2819 (candidates
, fn
, base
, mem_args
, TYPE_BINFO (type
),
2820 TYPE_BINFO (type
), LOOKUP_NORMAL
);
2824 convs
= lookup_conversions (type
);
2826 for (; convs
; convs
= TREE_CHAIN (convs
))
2828 tree fns
= TREE_VALUE (convs
);
2829 tree totype
= TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns
)));
2831 if ((TREE_CODE (totype
) == POINTER_TYPE
2832 && TREE_CODE (TREE_TYPE (totype
)) == FUNCTION_TYPE
)
2833 || (TREE_CODE (totype
) == REFERENCE_TYPE
2834 && TREE_CODE (TREE_TYPE (totype
)) == FUNCTION_TYPE
)
2835 || (TREE_CODE (totype
) == REFERENCE_TYPE
2836 && TREE_CODE (TREE_TYPE (totype
)) == POINTER_TYPE
2837 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype
))) == FUNCTION_TYPE
))
2838 for (; fns
; fns
= OVL_NEXT (fns
))
2840 tree fn
= OVL_CURRENT (fns
);
2841 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2843 candidates
= (add_template_conv_candidate
2844 (candidates
, fn
, obj
, args
, totype
,
2845 /*access_path=*/NULL_TREE
,
2846 /*conversion_path=*/NULL_TREE
));
2849 candidates
= add_conv_candidate (candidates
, fn
, obj
, args
,
2850 /*conversion_path=*/NULL_TREE
,
2851 /*access_path=*/NULL_TREE
);
2855 if (! any_viable (candidates
))
2857 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj
), args
);
2858 print_z_candidates (candidates
);
2859 return error_mark_node
;
2862 candidates
= splice_viable (candidates
);
2863 cand
= tourney (candidates
);
2867 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj
), args
);
2868 print_z_candidates (candidates
);
2869 return error_mark_node
;
2872 /* Since cand->fn will be a type, not a function, for a conversion
2873 function, we must be careful not to unconditionally look at
2875 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
2876 && DECL_OVERLOADED_OPERATOR_P (cand
->fn
) == CALL_EXPR
)
2877 return build_over_call (cand
, mem_args
, LOOKUP_NORMAL
);
2879 obj
= convert_like_with_context
2880 (TREE_VEC_ELT (cand
->convs
, 0), obj
, cand
->fn
, -1);
2883 return build_function_call (obj
, args
);
2887 op_error (code
, code2
, arg1
, arg2
, arg3
, problem
)
2888 enum tree_code code
, code2
;
2889 tree arg1
, arg2
, arg3
;
2890 const char *problem
;
2894 if (code
== MODIFY_EXPR
)
2895 opname
= assignment_operator_name_info
[code2
].name
;
2897 opname
= operator_name_info
[code
].name
;
2902 error ("%s for `%T ? %T : %T' operator", problem
,
2903 error_type (arg1
), error_type (arg2
), error_type (arg3
));
2905 case POSTINCREMENT_EXPR
:
2906 case POSTDECREMENT_EXPR
:
2907 error ("%s for `%T %s' operator", problem
, error_type (arg1
), opname
);
2910 error ("%s for `%T [%T]' operator", problem
,
2911 error_type (arg1
), error_type (arg2
));
2915 error ("%s for `%T %s %T' operator", problem
,
2916 error_type (arg1
), opname
, error_type (arg2
));
2918 error ("%s for `%s %T' operator", problem
, opname
, error_type (arg1
));
2922 /* Return the implicit conversion sequence that could be used to
2923 convert E1 to E2 in [expr.cond]. */
2926 conditional_conversion (e1
, e2
)
2930 tree t1
= non_reference (TREE_TYPE (e1
));
2931 tree t2
= non_reference (TREE_TYPE (e2
));
2936 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2937 implicitly converted (clause _conv_) to the type "reference to
2938 T2", subject to the constraint that in the conversion the
2939 reference must bind directly (_dcl.init.ref_) to E1. */
2940 if (real_lvalue_p (e2
))
2942 conv
= implicit_conversion (build_reference_type (t2
),
2945 LOOKUP_NO_TEMP_BIND
);
2952 If E1 and E2 have class type, and the underlying class types are
2953 the same or one is a base class of the other: E1 can be converted
2954 to match E2 if the class of T2 is the same type as, or a base
2955 class of, the class of T1, and the cv-qualification of T2 is the
2956 same cv-qualification as, or a greater cv-qualification than, the
2957 cv-qualification of T1. If the conversion is applied, E1 is
2958 changed to an rvalue of type T2 that still refers to the original
2959 source class object (or the appropriate subobject thereof). */
2960 if (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
2961 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2
),
2962 TYPE_MAIN_VARIANT (t1
)))
2964 if (at_least_as_qualified_p (t2
, t1
))
2966 conv
= build1 (IDENTITY_CONV
, t1
, e1
);
2967 if (!same_type_p (TYPE_MAIN_VARIANT (t1
),
2968 TYPE_MAIN_VARIANT (t2
)))
2969 conv
= build_conv (BASE_CONV
, t2
, conv
);
2978 E1 can be converted to match E2 if E1 can be implicitly converted
2979 to the type that expression E2 would have if E2 were converted to
2980 an rvalue (or the type it has, if E2 is an rvalue). */
2981 return implicit_conversion (t2
, t1
, e1
, LOOKUP_NORMAL
);
2984 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2985 arguments to the conditional expression. */
2988 build_conditional_expr (arg1
, arg2
, arg3
)
2996 tree result_type
= NULL_TREE
;
2998 struct z_candidate
*candidates
= 0;
2999 struct z_candidate
*cand
;
3001 /* As a G++ extension, the second argument to the conditional can be
3002 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
3003 c'.) If the second operand is omitted, make sure it is
3004 calculated only once. */
3008 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
3009 arg1
= arg2
= save_expr (arg1
);
3014 The first expr ession is implicitly converted to bool (clause
3016 arg1
= cp_convert (boolean_type_node
, arg1
);
3018 /* If something has already gone wrong, just pass that fact up the
3020 if (arg1
== error_mark_node
3021 || arg2
== error_mark_node
3022 || arg3
== error_mark_node
3023 || TREE_TYPE (arg1
) == error_mark_node
3024 || TREE_TYPE (arg2
) == error_mark_node
3025 || TREE_TYPE (arg3
) == error_mark_node
)
3026 return error_mark_node
;
3030 If either the second or the third operand has type (possibly
3031 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3032 array-to-pointer (_conv.array_), and function-to-pointer
3033 (_conv.func_) standard conversions are performed on the second
3034 and third operands. */
3035 arg2_type
= TREE_TYPE (arg2
);
3036 arg3_type
= TREE_TYPE (arg3
);
3037 if (VOID_TYPE_P (arg2_type
) || VOID_TYPE_P (arg3_type
))
3039 /* Do the conversions. We don't these for `void' type arguments
3040 since it can't have any effect and since decay_conversion
3041 does not handle that case gracefully. */
3042 if (!VOID_TYPE_P (arg2_type
))
3043 arg2
= decay_conversion (arg2
);
3044 if (!VOID_TYPE_P (arg3_type
))
3045 arg3
= decay_conversion (arg3
);
3046 arg2_type
= TREE_TYPE (arg2
);
3047 arg3_type
= TREE_TYPE (arg3
);
3051 One of the following shall hold:
3053 --The second or the third operand (but not both) is a
3054 throw-expression (_except.throw_); the result is of the
3055 type of the other and is an rvalue.
3057 --Both the second and the third operands have type void; the
3058 result is of type void and is an rvalue. */
3059 if ((TREE_CODE (arg2
) == THROW_EXPR
)
3060 ^ (TREE_CODE (arg3
) == THROW_EXPR
))
3061 result_type
= ((TREE_CODE (arg2
) == THROW_EXPR
)
3062 ? arg3_type
: arg2_type
);
3063 else if (VOID_TYPE_P (arg2_type
) && VOID_TYPE_P (arg3_type
))
3064 result_type
= void_type_node
;
3067 error ("`%E' has type `void' and is not a throw-expression",
3068 VOID_TYPE_P (arg2_type
) ? arg2
: arg3
);
3069 return error_mark_node
;
3073 goto valid_operands
;
3077 Otherwise, if the second and third operand have different types,
3078 and either has (possibly cv-qualified) class type, an attempt is
3079 made to convert each of those operands to the type of the other. */
3080 else if (!same_type_p (arg2_type
, arg3_type
)
3081 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)))
3083 tree conv2
= conditional_conversion (arg2
, arg3
);
3084 tree conv3
= conditional_conversion (arg3
, arg2
);
3088 If both can be converted, or one can be converted but the
3089 conversion is ambiguous, the program is ill-formed. If
3090 neither can be converted, the operands are left unchanged and
3091 further checking is performed as described below. If exactly
3092 one conversion is possible, that conversion is applied to the
3093 chosen operand and the converted operand is used in place of
3094 the original operand for the remainder of this section. */
3095 if ((conv2
&& !ICS_BAD_FLAG (conv2
)
3096 && conv3
&& !ICS_BAD_FLAG (conv3
))
3097 || (conv2
&& TREE_CODE (conv2
) == AMBIG_CONV
)
3098 || (conv3
&& TREE_CODE (conv3
) == AMBIG_CONV
))
3100 error ("operands to ?: have different types");
3101 return error_mark_node
;
3103 else if (conv2
&& !ICS_BAD_FLAG (conv2
))
3105 arg2
= convert_like (conv2
, arg2
);
3106 arg2
= convert_from_reference (arg2
);
3107 /* That may not quite have done the trick. If the two types
3108 are cv-qualified variants of one another, we will have
3109 just used an IDENTITY_CONV. (There's no conversion from
3110 an lvalue of one class type to an lvalue of another type,
3111 even a cv-qualified variant, and we don't want to lose
3112 lvalue-ness here.) So, we manually add a NOP_EXPR here
3114 if (!same_type_p (TREE_TYPE (arg2
), arg3_type
))
3115 arg2
= build1 (NOP_EXPR
, arg3_type
, arg2
);
3116 arg2_type
= TREE_TYPE (arg2
);
3118 else if (conv3
&& !ICS_BAD_FLAG (conv3
))
3120 arg3
= convert_like (conv3
, arg3
);
3121 arg3
= convert_from_reference (arg3
);
3122 if (!same_type_p (TREE_TYPE (arg3
), arg2_type
))
3123 arg3
= build1 (NOP_EXPR
, arg2_type
, arg3
);
3124 arg3_type
= TREE_TYPE (arg3
);
3130 If the second and third operands are lvalues and have the same
3131 type, the result is of that type and is an lvalue. */
3132 if (real_lvalue_p (arg2
) && real_lvalue_p (arg3
) &&
3133 same_type_p (arg2_type
, arg3_type
))
3135 result_type
= arg2_type
;
3136 goto valid_operands
;
3141 Otherwise, the result is an rvalue. If the second and third
3142 operand do not have the same type, and either has (possibly
3143 cv-qualified) class type, overload resolution is used to
3144 determine the conversions (if any) to be applied to the operands
3145 (_over.match.oper_, _over.built_). */
3147 if (!same_type_p (arg2_type
, arg3_type
)
3148 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)))
3153 /* Rearrange the arguments so that add_builtin_candidate only has
3154 to know about two args. In build_builtin_candidates, the
3155 arguments are unscrambled. */
3159 candidates
= add_builtin_candidates (candidates
,
3162 ansi_opname (COND_EXPR
),
3168 If the overload resolution fails, the program is
3170 if (!any_viable (candidates
))
3172 op_error (COND_EXPR
, NOP_EXPR
, arg1
, arg2
, arg3
, "no match");
3173 print_z_candidates (candidates
);
3174 return error_mark_node
;
3176 candidates
= splice_viable (candidates
);
3177 cand
= tourney (candidates
);
3180 op_error (COND_EXPR
, NOP_EXPR
, arg1
, arg2
, arg3
, "no match");
3181 print_z_candidates (candidates
);
3182 return error_mark_node
;
3187 Otherwise, the conversions thus determined are applied, and
3188 the converted operands are used in place of the original
3189 operands for the remainder of this section. */
3190 conv
= TREE_VEC_ELT (cand
->convs
, 0);
3191 arg1
= convert_like (conv
, arg1
);
3192 conv
= TREE_VEC_ELT (cand
->convs
, 1);
3193 arg2
= convert_like (conv
, arg2
);
3194 conv
= TREE_VEC_ELT (cand
->convs
, 2);
3195 arg3
= convert_like (conv
, arg3
);
3200 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3201 and function-to-pointer (_conv.func_) standard conversions are
3202 performed on the second and third operands.
3204 We need to force the lvalue-to-rvalue conversion here for class types,
3205 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3206 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3209 We use ocp_convert rather than build_user_type_conversion because the
3210 latter returns NULL_TREE on failure, while the former gives an error. */
3212 if (IS_AGGR_TYPE (TREE_TYPE (arg2
)) && real_lvalue_p (arg2
))
3213 arg2
= ocp_convert (TREE_TYPE (arg2
), arg2
,
3214 CONV_IMPLICIT
|CONV_FORCE_TEMP
, LOOKUP_NORMAL
);
3216 arg2
= decay_conversion (arg2
);
3217 arg2_type
= TREE_TYPE (arg2
);
3219 if (IS_AGGR_TYPE (TREE_TYPE (arg3
)) && real_lvalue_p (arg3
))
3220 arg3
= ocp_convert (TREE_TYPE (arg3
), arg3
,
3221 CONV_IMPLICIT
|CONV_FORCE_TEMP
, LOOKUP_NORMAL
);
3223 arg3
= decay_conversion (arg3
);
3224 arg3_type
= TREE_TYPE (arg3
);
3226 if (arg2
== error_mark_node
|| arg3
== error_mark_node
)
3227 return error_mark_node
;
3231 After those conversions, one of the following shall hold:
3233 --The second and third operands have the same type; the result is of
3235 if (same_type_p (arg2_type
, arg3_type
))
3236 result_type
= arg2_type
;
3239 --The second and third operands have arithmetic or enumeration
3240 type; the usual arithmetic conversions are performed to bring
3241 them to a common type, and the result is of that type. */
3242 else if ((ARITHMETIC_TYPE_P (arg2_type
)
3243 || TREE_CODE (arg2_type
) == ENUMERAL_TYPE
)
3244 && (ARITHMETIC_TYPE_P (arg3_type
)
3245 || TREE_CODE (arg3_type
) == ENUMERAL_TYPE
))
3247 /* In this case, there is always a common type. */
3248 result_type
= type_after_usual_arithmetic_conversions (arg2_type
,
3251 if (TREE_CODE (arg2_type
) == ENUMERAL_TYPE
3252 && TREE_CODE (arg3_type
) == ENUMERAL_TYPE
)
3253 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3254 arg2_type
, arg3_type
);
3255 else if (extra_warnings
3256 && ((TREE_CODE (arg2_type
) == ENUMERAL_TYPE
3257 && !same_type_p (arg3_type
, type_promotes_to (arg2_type
)))
3258 || (TREE_CODE (arg3_type
) == ENUMERAL_TYPE
3259 && !same_type_p (arg2_type
, type_promotes_to (arg3_type
)))))
3260 warning ("enumeral and non-enumeral type in conditional expression");
3262 arg2
= perform_implicit_conversion (result_type
, arg2
);
3263 arg3
= perform_implicit_conversion (result_type
, arg3
);
3267 --The second and third operands have pointer type, or one has
3268 pointer type and the other is a null pointer constant; pointer
3269 conversions (_conv.ptr_) and qualification conversions
3270 (_conv.qual_) are performed to bring them to their composite
3271 pointer type (_expr.rel_). The result is of the composite
3274 --The second and third operands have pointer to member type, or
3275 one has pointer to member type and the other is a null pointer
3276 constant; pointer to member conversions (_conv.mem_) and
3277 qualification conversions (_conv.qual_) are performed to bring
3278 them to a common type, whose cv-qualification shall match the
3279 cv-qualification of either the second or the third operand.
3280 The result is of the common type. */
3281 else if ((null_ptr_cst_p (arg2
)
3282 && (TYPE_PTR_P (arg3_type
) || TYPE_PTRMEM_P (arg3_type
)
3283 || TYPE_PTRMEMFUNC_P (arg3_type
)))
3284 || (null_ptr_cst_p (arg3
)
3285 && (TYPE_PTR_P (arg2_type
) || TYPE_PTRMEM_P (arg2_type
)
3286 || TYPE_PTRMEMFUNC_P (arg2_type
)))
3287 || (TYPE_PTR_P (arg2_type
) && TYPE_PTR_P (arg3_type
))
3288 || (TYPE_PTRMEM_P (arg2_type
) && TYPE_PTRMEM_P (arg3_type
))
3289 || (TYPE_PTRMEMFUNC_P (arg2_type
)
3290 && TYPE_PTRMEMFUNC_P (arg3_type
)))
3292 result_type
= composite_pointer_type (arg2_type
, arg3_type
, arg2
,
3293 arg3
, "conditional expression");
3294 arg2
= perform_implicit_conversion (result_type
, arg2
);
3295 arg3
= perform_implicit_conversion (result_type
, arg3
);
3300 error ("operands to ?: have different types");
3301 return error_mark_node
;
3305 result
= fold (build (COND_EXPR
, result_type
, arg1
, arg2
, arg3
));
3306 /* Expand both sides into the same slot, hopefully the target of the
3307 ?: expression. We used to check for TARGET_EXPRs here, but now we
3308 sometimes wrap them in NOP_EXPRs so the test would fail. */
3309 if (!lvalue_p
&& IS_AGGR_TYPE (result_type
))
3310 result
= build_target_expr_with_type (result
, result_type
);
3312 /* If this expression is an rvalue, but might be mistaken for an
3313 lvalue, we must add a NON_LVALUE_EXPR. */
3314 if (!lvalue_p
&& real_lvalue_p (result
))
3315 result
= build1 (NON_LVALUE_EXPR
, result_type
, result
);
3321 build_new_op (code
, flags
, arg1
, arg2
, arg3
)
3322 enum tree_code code
;
3324 tree arg1
, arg2
, arg3
;
3326 struct z_candidate
*candidates
= 0, *cand
;
3327 tree fns
, mem_arglist
= NULL_TREE
, arglist
, fnname
;
3328 enum tree_code code2
= NOP_EXPR
;
3329 tree templates
= NULL_TREE
;
3331 bool viable_candidates
;
3333 if (arg1
== error_mark_node
3334 || arg2
== error_mark_node
3335 || arg3
== error_mark_node
)
3336 return error_mark_node
;
3338 /* This can happen if a template takes all non-type parameters, e.g.
3339 undeclared_template<1, 5, 72>a; */
3340 if (code
== LT_EXPR
&& TREE_CODE (arg1
) == TEMPLATE_DECL
)
3342 error ("`%D' must be declared before use", arg1
);
3343 return error_mark_node
;
3346 if (code
== MODIFY_EXPR
)
3348 code2
= TREE_CODE (arg3
);
3350 fnname
= ansi_assopname (code2
);
3353 fnname
= ansi_opname (code
);
3355 if (TREE_CODE (arg1
) == OFFSET_REF
)
3356 arg1
= resolve_offset_ref (arg1
);
3357 arg1
= convert_from_reference (arg1
);
3363 case VEC_DELETE_EXPR
:
3365 /* Use build_op_new_call and build_op_delete_call instead. */
3369 return build_object_call (arg1
, arg2
);
3377 if (TREE_CODE (arg2
) == OFFSET_REF
)
3378 arg2
= resolve_offset_ref (arg2
);
3379 arg2
= convert_from_reference (arg2
);
3383 if (TREE_CODE (arg3
) == OFFSET_REF
)
3384 arg3
= resolve_offset_ref (arg3
);
3385 arg3
= convert_from_reference (arg3
);
3388 if (code
== COND_EXPR
)
3390 if (arg2
== NULL_TREE
3391 || TREE_CODE (TREE_TYPE (arg2
)) == VOID_TYPE
3392 || TREE_CODE (TREE_TYPE (arg3
)) == VOID_TYPE
3393 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2
))
3394 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3
))))
3397 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1
))
3398 && (! arg2
|| ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2
))))
3401 if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
3402 arg2
= integer_zero_node
;
3404 arglist
= NULL_TREE
;
3406 arglist
= tree_cons (NULL_TREE
, arg3
, arglist
);
3408 arglist
= tree_cons (NULL_TREE
, arg2
, arglist
);
3409 arglist
= tree_cons (NULL_TREE
, arg1
, arglist
);
3411 fns
= lookup_function_nonclass (fnname
, arglist
);
3413 if (fns
&& TREE_CODE (fns
) == TREE_LIST
)
3414 fns
= TREE_VALUE (fns
);
3415 for (; fns
; fns
= OVL_NEXT (fns
))
3417 tree fn
= OVL_CURRENT (fns
);
3418 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
3420 templates
= tree_cons (NULL_TREE
, fn
, templates
);
3422 = add_template_candidate (candidates
, fn
, NULL_TREE
, NULL_TREE
,
3423 arglist
, TREE_TYPE (fnname
),
3424 /*access_path=*/NULL_TREE
,
3425 /*conversion_path=*/NULL_TREE
,
3426 flags
, DEDUCE_CALL
);
3429 candidates
= add_function_candidate (candidates
, fn
, NULL_TREE
,
3431 /*access_path=*/NULL_TREE
,
3432 /*conversion_path=*/NULL_TREE
,
3436 if (IS_AGGR_TYPE (TREE_TYPE (arg1
)))
3438 fns
= lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1
)), fnname
, 1);
3439 if (fns
== error_mark_node
)
3447 tree conversion_path
= BASELINK_BINFO (fns
);
3449 mem_arglist
= tree_cons (NULL_TREE
, build_this (arg1
), TREE_CHAIN (arglist
));
3450 for (fns
= BASELINK_FUNCTIONS (fns
); fns
; fns
= OVL_NEXT (fns
))
3452 tree fn
= OVL_CURRENT (fns
);
3454 tree access_path
= TYPE_BINFO (TREE_TYPE (arg1
));
3456 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
3457 this_arglist
= mem_arglist
;
3459 this_arglist
= arglist
;
3461 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
3463 /* A member template. */
3464 templates
= tree_cons (NULL_TREE
, fn
, templates
);
3466 = add_template_candidate (candidates
, fn
,
3467 BINFO_TYPE (conversion_path
),
3469 this_arglist
, TREE_TYPE (fnname
),
3470 access_path
, conversion_path
,
3471 flags
, DEDUCE_CALL
);
3474 candidates
= add_function_candidate
3475 (candidates
, fn
, BINFO_TYPE (conversion_path
), this_arglist
,
3476 access_path
, conversion_path
, flags
);
3483 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3484 to know about two args; a builtin candidate will always have a first
3485 parameter of type bool. We'll handle that in
3486 build_builtin_candidate. */
3487 if (code
== COND_EXPR
)
3497 args
[2] = NULL_TREE
;
3500 candidates
= add_builtin_candidates
3501 (candidates
, code
, code2
, fnname
, args
, flags
);
3508 /* For these, the built-in candidates set is empty
3509 [over.match.oper]/3. We don't want non-strict matches
3510 because exact matches are always possible with built-in
3511 operators. The built-in candidate set for COMPONENT_REF
3512 would be empty too, but since there are no such built-in
3513 operators, we accept non-strict matches for them. */
3514 viable_candidates
= any_strictly_viable (candidates
);
3518 viable_candidates
= any_viable (candidates
);
3522 if (! viable_candidates
)
3526 case POSTINCREMENT_EXPR
:
3527 case POSTDECREMENT_EXPR
:
3528 /* Look for an `operator++ (int)'. If they didn't have
3529 one, then we fall back to the old way of doing things. */
3530 if (flags
& LOOKUP_COMPLAIN
)
3531 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3533 operator_name_info
[code
].name
);
3534 if (code
== POSTINCREMENT_EXPR
)
3535 code
= PREINCREMENT_EXPR
;
3537 code
= PREDECREMENT_EXPR
;
3538 return build_new_op (code
, flags
, arg1
, NULL_TREE
, NULL_TREE
);
3540 /* The caller will deal with these. */
3549 if (flags
& LOOKUP_COMPLAIN
)
3551 op_error (code
, code2
, arg1
, arg2
, arg3
, "no match");
3552 print_z_candidates (candidates
);
3554 return error_mark_node
;
3556 candidates
= splice_viable (candidates
);
3557 cand
= tourney (candidates
);
3561 if (flags
& LOOKUP_COMPLAIN
)
3563 op_error (code
, code2
, arg1
, arg2
, arg3
, "ambiguous overload");
3564 print_z_candidates (candidates
);
3566 return error_mark_node
;
3569 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
)
3571 extern int warn_synth
;
3573 && fnname
== ansi_assopname (NOP_EXPR
)
3574 && DECL_ARTIFICIAL (cand
->fn
)
3576 && ! candidates
->next
->next
)
3578 warning ("using synthesized `%#D' for copy assignment",
3580 cp_warning_at (" where cfront would use `%#D'",
3582 ? candidates
->next
->fn
3586 return build_over_call
3588 TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
3589 ? mem_arglist
: arglist
,
3593 /* Check for comparison of different enum types. */
3602 if (TREE_CODE (TREE_TYPE (arg1
)) == ENUMERAL_TYPE
3603 && TREE_CODE (TREE_TYPE (arg2
)) == ENUMERAL_TYPE
3604 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
))
3605 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2
))))
3607 warning ("comparison between `%#T' and `%#T'",
3608 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
3615 /* We need to strip any leading REF_BIND so that bitfields don't cause
3616 errors. This should not remove any important conversions, because
3617 builtins don't apply to class objects directly. */
3618 conv
= TREE_VEC_ELT (cand
->convs
, 0);
3619 if (TREE_CODE (conv
) == REF_BIND
)
3620 conv
= TREE_OPERAND (conv
, 0);
3621 arg1
= convert_like (conv
, arg1
);
3624 conv
= TREE_VEC_ELT (cand
->convs
, 1);
3625 if (TREE_CODE (conv
) == REF_BIND
)
3626 conv
= TREE_OPERAND (conv
, 0);
3627 arg2
= convert_like (conv
, arg2
);
3631 conv
= TREE_VEC_ELT (cand
->convs
, 2);
3632 if (TREE_CODE (conv
) == REF_BIND
)
3633 conv
= TREE_OPERAND (conv
, 0);
3634 arg3
= convert_like (conv
, arg3
);
3641 return build_modify_expr (arg1
, code2
, arg2
);
3644 return build_indirect_ref (arg1
, "unary *");
3649 case TRUNC_DIV_EXPR
:
3660 case TRUNC_MOD_EXPR
:
3664 case TRUTH_ANDIF_EXPR
:
3665 case TRUTH_ORIF_EXPR
:
3666 return cp_build_binary_op (code
, arg1
, arg2
);
3671 case TRUTH_NOT_EXPR
:
3672 case PREINCREMENT_EXPR
:
3673 case POSTINCREMENT_EXPR
:
3674 case PREDECREMENT_EXPR
:
3675 case POSTDECREMENT_EXPR
:
3678 return build_unary_op (code
, arg1
, candidates
!= 0);
3681 return build_array_ref (arg1
, arg2
);
3684 return build_conditional_expr (arg1
, arg2
, arg3
);
3687 return build_m_component_ref
3688 (build_indirect_ref (arg1
, NULL
), arg2
);
3690 /* The caller will deal with these. */
3702 /* Build a call to operator delete. This has to be handled very specially,
3703 because the restrictions on what signatures match are different from all
3704 other call instances. For a normal delete, only a delete taking (void *)
3705 or (void *, size_t) is accepted. For a placement delete, only an exact
3706 match with the placement new is accepted.
3708 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3709 ADDR is the pointer to be deleted.
3710 SIZE is the size of the memory block to be deleted.
3711 FLAGS are the usual overloading flags.
3712 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3715 build_op_delete_call (code
, addr
, size
, flags
, placement
)
3716 enum tree_code code
;
3717 tree addr
, size
, placement
;
3720 tree fn
= NULL_TREE
;
3721 tree fns
, fnname
, fntype
, argtypes
, args
, type
;
3724 if (addr
== error_mark_node
)
3725 return error_mark_node
;
3727 type
= TREE_TYPE (TREE_TYPE (addr
));
3728 while (TREE_CODE (type
) == ARRAY_TYPE
)
3729 type
= TREE_TYPE (type
);
3731 fnname
= ansi_opname (code
);
3733 if (IS_AGGR_TYPE (type
) && ! (flags
& LOOKUP_GLOBAL
))
3736 If the result of the lookup is ambiguous or inaccessible, or if
3737 the lookup selects a placement deallocation function, the
3738 program is ill-formed.
3740 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3742 fns
= lookup_fnfields (TYPE_BINFO (type
), fnname
, 1);
3743 if (fns
== error_mark_node
)
3744 return error_mark_node
;
3749 if (fns
== NULL_TREE
)
3750 fns
= lookup_name_nonclass (fnname
);
3757 /* Find the allocation function that is being called. */
3758 call_expr
= placement
;
3759 /* Sometimes we have a COMPOUND_EXPR, rather than a simple
3761 while (TREE_CODE (call_expr
) == COMPOUND_EXPR
)
3762 call_expr
= TREE_OPERAND (call_expr
, 1);
3763 /* Extract the function. */
3764 alloc_fn
= get_callee_fndecl (call_expr
);
3765 my_friendly_assert (alloc_fn
!= NULL_TREE
, 20020327);
3766 /* Then the second parm type. */
3767 argtypes
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn
)));
3768 /* Also the second argument. */
3769 args
= TREE_CHAIN (TREE_OPERAND (call_expr
, 1));
3773 /* First try it without the size argument. */
3774 argtypes
= void_list_node
;
3778 /* Strip const and volatile from addr. */
3779 addr
= cp_convert (ptr_type_node
, addr
);
3781 /* We make two tries at finding a matching `operator delete'. On
3782 the first pass, we look for an one-operator (or placement)
3783 operator delete. If we're not doing placement delete, then on
3784 the second pass we look for a two-argument delete. */
3785 for (pass
= 0; pass
< (placement
? 1 : 2); ++pass
)
3788 argtypes
= tree_cons (NULL_TREE
, ptr_type_node
, argtypes
);
3790 /* Normal delete; now try to find a match including the size
3792 argtypes
= tree_cons (NULL_TREE
, ptr_type_node
,
3793 tree_cons (NULL_TREE
, sizetype
,
3795 fntype
= build_function_type (void_type_node
, argtypes
);
3797 /* Go through the `operator delete' functions looking for one
3798 with a matching type. */
3799 for (fn
= BASELINK_P (fns
) ? BASELINK_FUNCTIONS (fns
) : fns
;
3805 /* Exception specifications on the `delete' operator do not
3807 t
= build_exception_variant (TREE_TYPE (OVL_CURRENT (fn
)),
3809 /* We also don't compare attributes. We're really just
3810 trying to check the types of the first two parameters. */
3811 if (comptypes (t
, fntype
, COMPARE_NO_ATTRIBUTES
))
3815 /* If we found a match, we're done. */
3820 /* If we have a matching function, call it. */
3823 /* Make sure we have the actual function, and not an
3825 fn
= OVL_CURRENT (fn
);
3827 /* If the FN is a member function, make sure that it is
3829 if (DECL_CLASS_SCOPE_P (fn
))
3830 enforce_access (type
, fn
);
3833 args
= tree_cons (NULL_TREE
, addr
, args
);
3835 args
= tree_cons (NULL_TREE
, addr
,
3836 build_tree_list (NULL_TREE
, size
));
3838 return build_function_call (fn
, args
);
3841 /* If we are doing placement delete we do nothing if we don't find a
3842 matching op delete. */
3846 error ("no suitable `operator delete' for `%T'", type
);
3847 return error_mark_node
;
3850 /* If the current scope isn't allowed to access DECL along
3851 BASETYPE_PATH, give an error. The most derived class in
3852 BASETYPE_PATH is the one used to qualify DECL. */
3855 enforce_access (basetype_path
, decl
)
3861 accessible
= accessible_p (basetype_path
, decl
);
3864 if (TREE_PRIVATE (decl
))
3865 cp_error_at ("`%+#D' is private", decl
);
3866 else if (TREE_PROTECTED (decl
))
3867 cp_error_at ("`%+#D' is protected", decl
);
3869 cp_error_at ("`%+#D' is inaccessible", decl
);
3870 error ("within this context");
3877 /* Perform the conversions in CONVS on the expression EXPR.
3878 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3879 indicates the `this' argument of a method. INNER is nonzero when
3880 being called to continue a conversion chain. It is negative when a
3881 reference binding will be applied, positive otherwise. */
3884 convert_like_real (convs
, expr
, fn
, argnum
, inner
)
3892 tree totype
= TREE_TYPE (convs
);
3894 if (ICS_BAD_FLAG (convs
)
3895 && TREE_CODE (convs
) != USER_CONV
3896 && TREE_CODE (convs
) != AMBIG_CONV
3897 && TREE_CODE (convs
) != REF_BIND
)
3900 for (; t
; t
= TREE_OPERAND (t
, 0))
3902 if (TREE_CODE (t
) == USER_CONV
|| !ICS_BAD_FLAG (t
))
3904 expr
= convert_like_real (t
, expr
, fn
, argnum
, 1);
3907 else if (TREE_CODE (t
) == AMBIG_CONV
)
3908 return convert_like_real (t
, expr
, fn
, argnum
, 1);
3909 else if (TREE_CODE (t
) == IDENTITY_CONV
)
3912 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr
), totype
);
3914 pedwarn (" initializing argument %P of `%D'", argnum
, fn
);
3915 return cp_convert (totype
, expr
);
3919 expr
= dubious_conversion_warnings
3920 (totype
, expr
, "argument", fn
, argnum
);
3921 switch (TREE_CODE (convs
))
3925 struct z_candidate
*cand
3926 = WRAPPER_ZC (TREE_OPERAND (convs
, 1));
3927 tree convfn
= cand
->fn
;
3930 if (DECL_CONSTRUCTOR_P (convfn
))
3932 tree t
= build_int_2 (0, 0);
3933 TREE_TYPE (t
) = build_pointer_type (DECL_CONTEXT (convfn
));
3935 args
= build_tree_list (NULL_TREE
, expr
);
3936 if (DECL_HAS_IN_CHARGE_PARM_P (convfn
)
3937 || DECL_HAS_VTT_PARM_P (convfn
))
3938 /* We should never try to call the abstract or base constructor
3941 args
= tree_cons (NULL_TREE
, t
, args
);
3944 args
= build_this (expr
);
3945 expr
= build_over_call (cand
, args
, LOOKUP_NORMAL
);
3947 /* If this is a constructor or a function returning an aggr type,
3948 we need to build up a TARGET_EXPR. */
3949 if (DECL_CONSTRUCTOR_P (convfn
))
3950 expr
= build_cplus_new (totype
, expr
);
3952 /* The result of the call is then used to direct-initialize the object
3953 that is the destination of the copy-initialization. [dcl.init]
3955 Note that this step is not reflected in the conversion sequence;
3956 it affects the semantics when we actually perform the
3957 conversion, but is not considered during overload resolution.
3959 If the target is a class, that means call a ctor. */
3960 if (IS_AGGR_TYPE (totype
)
3961 && (inner
>= 0 || !lvalue_p (expr
)))
3963 savew
= warningcount
, savee
= errorcount
;
3964 expr
= build_special_member_call
3965 (NULL_TREE
, complete_ctor_identifier
,
3966 build_tree_list (NULL_TREE
, expr
), TYPE_BINFO (totype
),
3967 /* Core issue 84, now a DR, says that we don't allow UDCs
3968 for these args (which deliberately breaks copy-init of an
3969 auto_ptr<Base> from an auto_ptr<Derived>). */
3970 LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
|LOOKUP_NO_CONVERSION
);
3972 /* Tell the user where this failing constructor call came from. */
3975 if (warningcount
> savew
)
3977 (" initializing argument %P of `%D' from result of `%D'",
3978 argnum
, fn
, convfn
);
3979 else if (errorcount
> savee
)
3981 (" initializing argument %P of `%D' from result of `%D'",
3982 argnum
, fn
, convfn
);
3986 if (warningcount
> savew
)
3987 warning (" initializing temporary from result of `%D'",
3989 else if (errorcount
> savee
)
3990 error (" initializing temporary from result of `%D'",
3993 expr
= build_cplus_new (totype
, expr
);
3998 if (type_unknown_p (expr
))
3999 expr
= instantiate_type (totype
, expr
, tf_error
| tf_warning
);
4002 /* Call build_user_type_conversion again for the error. */
4003 return build_user_type_conversion
4004 (totype
, TREE_OPERAND (convs
, 0), LOOKUP_NORMAL
);
4010 expr
= convert_like_real (TREE_OPERAND (convs
, 0), expr
, fn
, argnum
,
4011 TREE_CODE (convs
) == REF_BIND
? -1 : 1);
4012 if (expr
== error_mark_node
)
4013 return error_mark_node
;
4015 /* Convert a non-array constant variable to its underlying value, unless we
4016 are about to bind it to a reference, in which case we need to
4017 leave it as an lvalue. */
4018 if (TREE_CODE (convs
) != REF_BIND
4019 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
4020 expr
= decl_constant_value (expr
);
4022 switch (TREE_CODE (convs
))
4025 if (! IS_AGGR_TYPE (totype
))
4027 /* else fall through */
4029 if (TREE_CODE (convs
) == BASE_CONV
&& !NEED_TEMPORARY_P (convs
))
4031 /* We are going to bind a reference directly to a base-class
4032 subobject of EXPR. */
4033 tree base_ptr
= build_pointer_type (totype
);
4035 /* Build an expression for `*((base*) &expr)'. */
4036 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
4037 expr
= perform_implicit_conversion (base_ptr
, expr
);
4038 expr
= build_indirect_ref (expr
, "implicit conversion");
4042 /* Copy-initialization where the cv-unqualified version of the source
4043 type is the same class as, or a derived class of, the class of the
4044 destination [is treated as direct-initialization]. [dcl.init] */
4045 savew
= warningcount
, savee
= errorcount
;
4046 expr
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
4047 build_tree_list (NULL_TREE
, expr
),
4048 TYPE_BINFO (totype
),
4049 LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
);
4052 if (warningcount
> savew
)
4053 warning (" initializing argument %P of `%D'", argnum
, fn
);
4054 else if (errorcount
> savee
)
4055 error (" initializing argument %P of `%D'", argnum
, fn
);
4057 return build_cplus_new (totype
, expr
);
4061 tree ref_type
= totype
;
4063 /* If necessary, create a temporary. */
4064 if (NEED_TEMPORARY_P (convs
) || !non_cast_lvalue_p (expr
))
4066 tree type
= TREE_TYPE (TREE_OPERAND (convs
, 0));
4067 expr
= build_target_expr_with_type (expr
, type
);
4070 /* Take the address of the thing to which we will bind the
4072 expr
= build_unary_op (ADDR_EXPR
, expr
, 1);
4073 if (expr
== error_mark_node
)
4074 return error_mark_node
;
4076 /* Convert it to a pointer to the type referred to by the
4077 reference. This will adjust the pointer if a derived to
4078 base conversion is being performed. */
4079 expr
= cp_convert (build_pointer_type (TREE_TYPE (ref_type
)),
4081 /* Convert the pointer to the desired reference type. */
4082 expr
= build1 (NOP_EXPR
, ref_type
, expr
);
4088 return decay_conversion (expr
);
4091 /* Warn about deprecated conversion if appropriate. */
4092 string_conv_p (totype
, expr
, 1);
4098 return ocp_convert (totype
, expr
, CONV_IMPLICIT
,
4099 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
);
4102 /* Build a call to __builtin_trap which can be used in an expression. */
4105 call_builtin_trap ()
4107 tree fn
= get_identifier ("__builtin_trap");
4108 if (IDENTIFIER_GLOBAL_VALUE (fn
))
4109 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
4113 fn
= build_call (fn
, NULL_TREE
);
4114 fn
= build (COMPOUND_EXPR
, integer_type_node
, fn
, integer_zero_node
);
4118 /* ARG is being passed to a varargs function. Perform any conversions
4119 required. Array/function to pointer decay must have already happened.
4120 Return the converted value. */
4123 convert_arg_to_ellipsis (arg
)
4126 if (TREE_CODE (TREE_TYPE (arg
)) == REAL_TYPE
4127 && (TYPE_PRECISION (TREE_TYPE (arg
))
4128 < TYPE_PRECISION (double_type_node
)))
4129 /* Convert `float' to `double'. */
4130 arg
= cp_convert (double_type_node
, arg
);
4132 /* Convert `short' and `char' to full-size `int'. */
4133 arg
= default_conversion (arg
);
4135 arg
= require_complete_type (arg
);
4137 if (arg
!= error_mark_node
&& ! pod_type_p (TREE_TYPE (arg
)))
4139 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
4140 here and do a bitwise copy, but now cp_expr_size will abort if we
4142 warning ("cannot pass objects of non-POD type `%#T' through `...'; \
4143 call will abort at runtime",
4145 arg
= call_builtin_trap ();
4151 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4154 build_x_va_arg (expr
, type
)
4158 if (processing_template_decl
)
4159 return build_min (VA_ARG_EXPR
, type
, expr
);
4161 type
= complete_type_or_else (type
, NULL_TREE
);
4163 if (expr
== error_mark_node
|| !type
)
4164 return error_mark_node
;
4166 if (! pod_type_p (type
))
4168 /* Undefined behavior [expr.call] 5.2.2/7. */
4169 warning ("cannot receive objects of non-POD type `%#T' through `...'",
4173 return build_va_arg (expr
, type
);
4176 /* TYPE has been given to va_arg. Apply the default conversions which
4177 would have happened when passed via ellipsis. Return the promoted
4178 type, or the passed type if there is no change. */
4181 cxx_type_promotes_to (type
)
4186 if (TREE_CODE (type
) == ARRAY_TYPE
)
4187 return build_pointer_type (TREE_TYPE (type
));
4189 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4190 return build_pointer_type (type
);
4192 promote
= type_promotes_to (type
);
4193 if (same_type_p (type
, promote
))
4199 /* ARG is a default argument expression being passed to a parameter of
4200 the indicated TYPE, which is a parameter to FN. Do any required
4201 conversions. Return the converted value. */
4204 convert_default_arg (type
, arg
, fn
, parmnum
)
4210 if (TREE_CODE (arg
) == DEFAULT_ARG
)
4212 /* When processing the default args for a class, we can find that
4213 there is an ordering constraint, and we call a function who's
4214 default args have not yet been converted. For instance,
4217 void Foo (A const & = A ());
4219 We must process A::A before A::Foo's default arg can be converted.
4220 Remember the dependent function, so do_pending_defargs can retry,
4222 unprocessed_defarg_fn (fn
);
4224 /* Don't return error_mark node, as we won't be able to distinguish
4225 genuine errors from this case, and that would lead to repeated
4226 diagnostics. Just make something of the right type. */
4227 return build1 (NOP_EXPR
, type
, integer_zero_node
);
4230 if (fn
&& DECL_TEMPLATE_INFO (fn
))
4231 arg
= tsubst_default_argument (fn
, type
, arg
);
4233 arg
= break_out_target_exprs (arg
);
4235 if (TREE_CODE (arg
) == CONSTRUCTOR
)
4237 arg
= digest_init (type
, arg
, 0);
4238 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_NORMAL
,
4239 "default argument", fn
, parmnum
);
4243 /* This could get clobbered by the following call. */
4244 if (TREE_HAS_CONSTRUCTOR (arg
))
4245 arg
= copy_node (arg
);
4247 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_NORMAL
,
4248 "default argument", fn
, parmnum
);
4249 arg
= convert_for_arg_passing (type
, arg
);
4255 /* Returns the type which will really be used for passing an argument of
4259 type_passed_as (type
)
4262 /* Pass classes with copy ctors by invisible reference. */
4263 if (TREE_ADDRESSABLE (type
))
4264 type
= build_reference_type (type
);
4265 else if (PROMOTE_PROTOTYPES
4266 && INTEGRAL_TYPE_P (type
)
4267 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
4268 type
= integer_type_node
;
4273 /* Actually perform the appropriate conversion. */
4276 convert_for_arg_passing (type
, val
)
4279 if (val
== error_mark_node
)
4281 /* Pass classes with copy ctors by invisible reference. */
4282 else if (TREE_ADDRESSABLE (type
))
4283 val
= build1 (ADDR_EXPR
, build_reference_type (type
), val
);
4284 else if (PROMOTE_PROTOTYPES
4285 && INTEGRAL_TYPE_P (type
)
4286 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
4287 val
= default_conversion (val
);
4291 /* Subroutine of the various build_*_call functions. Overload resolution
4292 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4293 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4294 bitmask of various LOOKUP_* flags which apply to the call itself. */
4297 build_over_call (cand
, args
, flags
)
4298 struct z_candidate
*cand
;
4303 tree convs
= cand
->convs
;
4304 tree converted_args
= NULL_TREE
;
4305 tree parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4306 tree conv
, arg
, val
;
4310 /* Give any warnings we noticed during overload resolution. */
4312 for (val
= cand
->warnings
; val
; val
= TREE_CHAIN (val
))
4313 joust (cand
, WRAPPER_ZC (TREE_VALUE (val
)), 1);
4315 if (DECL_FUNCTION_MEMBER_P (fn
))
4316 enforce_access (cand
->access_path
, fn
);
4318 if (args
&& TREE_CODE (args
) != TREE_LIST
)
4319 args
= build_tree_list (NULL_TREE
, args
);
4322 /* The implicit parameters to a constructor are not considered by overload
4323 resolution, and must be of the proper type. */
4324 if (DECL_CONSTRUCTOR_P (fn
))
4326 converted_args
= tree_cons (NULL_TREE
, TREE_VALUE (arg
), converted_args
);
4327 arg
= TREE_CHAIN (arg
);
4328 parm
= TREE_CHAIN (parm
);
4329 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
4330 /* We should never try to call the abstract constructor. */
4332 if (DECL_HAS_VTT_PARM_P (fn
))
4334 converted_args
= tree_cons
4335 (NULL_TREE
, TREE_VALUE (arg
), converted_args
);
4336 arg
= TREE_CHAIN (arg
);
4337 parm
= TREE_CHAIN (parm
);
4340 /* Bypass access control for 'this' parameter. */
4341 else if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
4343 tree parmtype
= TREE_VALUE (parm
);
4344 tree argtype
= TREE_TYPE (TREE_VALUE (arg
));
4348 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs
, i
)))
4349 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4350 TREE_TYPE (argtype
), fn
);
4352 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4353 X is called for an object that is not of type X, or of a type
4354 derived from X, the behavior is undefined.
4356 So we can assume that anything passed as 'this' is non-null, and
4357 optimize accordingly. */
4358 my_friendly_assert (TREE_CODE (parmtype
) == POINTER_TYPE
, 19990811);
4359 /* Convert to the base in which the function was declared. */
4360 my_friendly_assert (cand
->conversion_path
!= NULL_TREE
, 20020730);
4361 converted_arg
= build_base_path (PLUS_EXPR
,
4363 cand
->conversion_path
,
4365 /* If fn was found by a using declaration, the conversion path
4366 will be to the derived class, not the base declaring fn. We
4367 must convert from derived to base. */
4368 base_binfo
= lookup_base (TREE_TYPE (TREE_TYPE (converted_arg
)),
4369 TREE_TYPE (parmtype
), ba_ignore
, NULL
);
4371 converted_arg
= build_base_path (PLUS_EXPR
, converted_arg
,
4374 converted_args
= tree_cons (NULL_TREE
, converted_arg
, converted_args
);
4375 parm
= TREE_CHAIN (parm
);
4376 arg
= TREE_CHAIN (arg
);
4382 parm
= TREE_CHAIN (parm
), arg
= TREE_CHAIN (arg
), ++i
)
4384 tree type
= TREE_VALUE (parm
);
4386 conv
= TREE_VEC_ELT (convs
, i
);
4387 val
= convert_like_with_context
4388 (conv
, TREE_VALUE (arg
), fn
, i
- is_method
);
4390 val
= convert_for_arg_passing (type
, val
);
4391 converted_args
= tree_cons (NULL_TREE
, val
, converted_args
);
4394 /* Default arguments */
4395 for (; parm
&& parm
!= void_list_node
; parm
= TREE_CHAIN (parm
), i
++)
4397 = tree_cons (NULL_TREE
,
4398 convert_default_arg (TREE_VALUE (parm
),
4399 TREE_PURPOSE (parm
),
4404 for (; arg
; arg
= TREE_CHAIN (arg
))
4406 = tree_cons (NULL_TREE
,
4407 convert_arg_to_ellipsis (TREE_VALUE (arg
)),
4410 converted_args
= nreverse (converted_args
);
4413 check_function_format (NULL
, TYPE_ATTRIBUTES (TREE_TYPE (fn
)),
4416 /* Avoid actually calling copy constructors and copy assignment operators,
4419 if (! flag_elide_constructors
)
4420 /* Do things the hard way. */;
4421 else if (TREE_VEC_LENGTH (convs
) == 1
4422 && DECL_COPY_CONSTRUCTOR_P (fn
))
4425 arg
= skip_artificial_parms_for (fn
, converted_args
);
4426 arg
= TREE_VALUE (arg
);
4428 /* Pull out the real argument, disregarding const-correctness. */
4430 while (TREE_CODE (targ
) == NOP_EXPR
4431 || TREE_CODE (targ
) == NON_LVALUE_EXPR
4432 || TREE_CODE (targ
) == CONVERT_EXPR
)
4433 targ
= TREE_OPERAND (targ
, 0);
4434 if (TREE_CODE (targ
) == ADDR_EXPR
)
4436 targ
= TREE_OPERAND (targ
, 0);
4437 if (!same_type_ignoring_top_level_qualifiers_p
4438 (TREE_TYPE (TREE_TYPE (arg
)), TREE_TYPE (targ
)))
4447 arg
= build_indirect_ref (arg
, 0);
4449 /* [class.copy]: the copy constructor is implicitly defined even if
4450 the implementation elided its use. */
4451 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn
)))
4454 /* If we're creating a temp and we already have one, don't create a
4455 new one. If we're not creating a temp but we get one, use
4456 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4457 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4458 temp or an INIT_EXPR otherwise. */
4459 if (integer_zerop (TREE_VALUE (args
)))
4461 if (TREE_CODE (arg
) == TARGET_EXPR
)
4463 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn
)))
4464 return build_target_expr_with_type (arg
, DECL_CONTEXT (fn
));
4466 else if (TREE_CODE (arg
) == TARGET_EXPR
4467 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn
)))
4470 tree to
= stabilize_reference
4471 (build_indirect_ref (TREE_VALUE (args
), 0));
4473 val
= build (INIT_EXPR
, DECL_CONTEXT (fn
), to
, arg
);
4474 address
= build_unary_op (ADDR_EXPR
, val
, 0);
4475 /* Avoid a warning about this expression, if the address is
4477 TREE_USED (address
) = 1;
4481 else if (DECL_OVERLOADED_OPERATOR_P (fn
) == NOP_EXPR
4483 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn
)))
4485 tree to
= stabilize_reference
4486 (build_indirect_ref (TREE_VALUE (converted_args
), 0));
4488 arg
= build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args
)), 0);
4489 val
= build (MODIFY_EXPR
, TREE_TYPE (to
), to
, arg
);
4495 if (DECL_VINDEX (fn
) && (flags
& LOOKUP_NONVIRTUAL
) == 0)
4497 tree t
, *p
= &TREE_VALUE (converted_args
);
4498 tree binfo
= lookup_base (TREE_TYPE (TREE_TYPE (*p
)),
4501 my_friendly_assert (binfo
&& binfo
!= error_mark_node
, 20010730);
4503 *p
= build_base_path (PLUS_EXPR
, *p
, binfo
, 1);
4504 if (TREE_SIDE_EFFECTS (*p
))
4505 *p
= save_expr (*p
);
4506 t
= build_pointer_type (TREE_TYPE (fn
));
4507 if (DECL_CONTEXT (fn
) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn
)))
4508 fn
= build_java_interface_fn_ref (fn
, *p
);
4510 fn
= build_vfn_ref (build_indirect_ref (*p
, 0), DECL_VINDEX (fn
));
4513 else if (DECL_INLINE (fn
))
4514 fn
= inline_conversion (fn
);
4516 fn
= build_addr_func (fn
);
4518 /* Recognize certain built-in functions so we can make tree-codes
4519 other than CALL_EXPR. We do this when it enables fold-const.c
4520 to do something useful. */
4522 if (TREE_CODE (fn
) == ADDR_EXPR
4523 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
4524 && DECL_BUILT_IN (TREE_OPERAND (fn
, 0)))
4527 exp
= expand_tree_builtin (TREE_OPERAND (fn
, 0), args
, converted_args
);
4532 /* Some built-in function calls will be evaluated at
4533 compile-time in fold (). */
4534 fn
= fold (build_call (fn
, converted_args
));
4535 if (VOID_TYPE_P (TREE_TYPE (fn
)))
4537 fn
= require_complete_type (fn
);
4538 if (fn
== error_mark_node
)
4539 return error_mark_node
;
4540 if (IS_AGGR_TYPE (TREE_TYPE (fn
)))
4541 fn
= build_cplus_new (TREE_TYPE (fn
), fn
);
4542 return convert_from_reference (fn
);
4545 static GTY(()) tree java_iface_lookup_fn
;
4547 /* Make an expression which yields the address of the Java interface
4548 method FN. This is achieved by generating a call to libjava's
4549 _Jv_LookupInterfaceMethodIdx(). */
4552 build_java_interface_fn_ref (fn
, instance
)
4555 tree lookup_args
, lookup_fn
, method
, idx
;
4556 tree klass_ref
, iface
, iface_ref
;
4559 if (!java_iface_lookup_fn
)
4561 tree endlink
= build_void_list_node ();
4562 tree t
= tree_cons (NULL_TREE
, ptr_type_node
,
4563 tree_cons (NULL_TREE
, ptr_type_node
,
4564 tree_cons (NULL_TREE
, java_int_type_node
,
4566 java_iface_lookup_fn
4567 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4568 build_function_type (ptr_type_node
, t
),
4569 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
4572 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4573 This is the first entry in the vtable. */
4574 klass_ref
= build_vtbl_ref (build_indirect_ref (instance
, 0),
4577 /* Get the java.lang.Class pointer for the interface being called. */
4578 iface
= DECL_CONTEXT (fn
);
4579 iface_ref
= lookup_field (iface
, get_identifier ("class$"), 0, 0);
4580 if (!iface_ref
|| TREE_CODE (iface_ref
) != VAR_DECL
4581 || DECL_CONTEXT (iface_ref
) != iface
)
4583 error ("could not find class$ field in java interface type `%T'",
4585 return error_mark_node
;
4587 iface_ref
= build1 (ADDR_EXPR
, build_pointer_type (iface
), iface_ref
);
4589 /* Determine the itable index of FN. */
4591 for (method
= TYPE_METHODS (iface
); method
; method
= TREE_CHAIN (method
))
4593 if (!DECL_VIRTUAL_P (method
))
4599 idx
= build_int_2 (i
, 0);
4601 lookup_args
= tree_cons (NULL_TREE
, klass_ref
,
4602 tree_cons (NULL_TREE
, iface_ref
,
4603 build_tree_list (NULL_TREE
, idx
)));
4604 lookup_fn
= build1 (ADDR_EXPR
,
4605 build_pointer_type (TREE_TYPE (java_iface_lookup_fn
)),
4606 java_iface_lookup_fn
);
4607 return build (CALL_EXPR
, ptr_type_node
, lookup_fn
, lookup_args
, NULL_TREE
);
4610 /* Returns the value to use for the in-charge parameter when making a
4611 call to a function with the indicated NAME. */
4614 in_charge_arg_for_name (name
)
4617 if (name
== base_ctor_identifier
4618 || name
== base_dtor_identifier
)
4619 return integer_zero_node
;
4620 else if (name
== complete_ctor_identifier
)
4621 return integer_one_node
;
4622 else if (name
== complete_dtor_identifier
)
4623 return integer_two_node
;
4624 else if (name
== deleting_dtor_identifier
)
4625 return integer_three_node
;
4627 /* This function should only be called with one of the names listed
4633 /* Build a call to a constructor, destructor, or an assignment
4634 operator for INSTANCE, an expression with class type. NAME
4635 indicates the special member function to call; ARGS are the
4636 arguments. BINFO indicates the base of INSTANCE that is to be
4637 passed as the `this' parameter to the member function called.
4639 FLAGS are the LOOKUP_* flags to use when processing the call.
4641 If NAME indicates a complete object constructor, INSTANCE may be
4642 NULL_TREE. In this case, the caller will call build_cplus_new to
4643 store the newly constructed object into a VAR_DECL. */
4646 build_special_member_call (tree instance
, tree name
, tree args
,
4647 tree binfo
, int flags
)
4650 /* The type of the subobject to be constructed or destroyed. */
4653 my_friendly_assert (name
== complete_ctor_identifier
4654 || name
== base_ctor_identifier
4655 || name
== complete_dtor_identifier
4656 || name
== base_dtor_identifier
4657 || name
== deleting_dtor_identifier
4658 || name
== ansi_assopname (NOP_EXPR
),
4660 my_friendly_assert (binfo
!= NULL_TREE
, 20020712);
4662 class_type
= BINFO_TYPE (binfo
);
4664 /* Handle the special case where INSTANCE is NULL_TREE. */
4665 if (name
== complete_ctor_identifier
&& !instance
)
4667 instance
= build_int_2 (0, 0);
4668 TREE_TYPE (instance
) = build_pointer_type (class_type
);
4669 instance
= build1 (INDIRECT_REF
, class_type
, instance
);
4671 else if (name
== complete_dtor_identifier
4672 || name
== base_dtor_identifier
4673 || name
== deleting_dtor_identifier
)
4674 my_friendly_assert (args
== NULL_TREE
, 20020712);
4676 my_friendly_assert (instance
!= NULL_TREE
, 20020712);
4678 /* Resolve the name. */
4679 if (!complete_type_or_else (BINFO_TYPE (binfo
), NULL_TREE
))
4680 return error_mark_node
;
4682 fns
= lookup_fnfields (binfo
, name
, 1);
4684 /* When making a call to a constructor or destructor for a subobject
4685 that uses virtual base classes, pass down a pointer to a VTT for
4687 if ((name
== base_ctor_identifier
4688 || name
== base_dtor_identifier
)
4689 && TYPE_USES_VIRTUAL_BASECLASSES (class_type
))
4694 /* If the current function is a complete object constructor
4695 or destructor, then we fetch the VTT directly.
4696 Otherwise, we look it up using the VTT we were given. */
4697 vtt
= TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type
));
4698 vtt
= decay_conversion (vtt
);
4699 vtt
= build (COND_EXPR
, TREE_TYPE (vtt
),
4700 build (EQ_EXPR
, boolean_type_node
,
4701 current_in_charge_parm
, integer_zero_node
),
4704 if (TREE_VIA_VIRTUAL (binfo
))
4705 binfo
= binfo_for_vbase (class_type
, current_class_type
);
4706 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo
), 20010110);
4707 sub_vtt
= build (PLUS_EXPR
, TREE_TYPE (vtt
), vtt
,
4708 BINFO_SUBVTT_INDEX (binfo
));
4710 args
= tree_cons (NULL_TREE
, sub_vtt
, args
);
4713 return build_new_method_call (instance
, fns
, args
, binfo
, flags
);
4716 /* Build a call to "INSTANCE.FN (ARGS)". */
4719 build_new_method_call (tree instance
, tree fns
, tree args
,
4720 tree conversion_path
, int flags
)
4722 struct z_candidate
*candidates
= 0, *cand
;
4723 tree explicit_targs
= NULL_TREE
;
4724 tree basetype
= NULL_TREE
;
4727 tree mem_args
= NULL_TREE
, instance_ptr
;
4728 tree name
, pretty_name
;
4730 tree templates
= NULL_TREE
;
4732 int template_only
= 0;
4734 my_friendly_assert (instance
!= NULL_TREE
, 20020729);
4736 if (instance
== error_mark_node
|| fns
== error_mark_node
4737 || args
== error_mark_node
)
4738 return error_mark_node
;
4740 /* Process the argument list. */
4742 args
= resolve_args (args
);
4743 if (args
== error_mark_node
)
4744 return error_mark_node
;
4746 if (TREE_CODE (instance
) == OFFSET_REF
)
4747 instance
= resolve_offset_ref (instance
);
4748 if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
4749 instance
= convert_from_reference (instance
);
4750 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (instance
));
4751 instance_ptr
= build_this (instance
);
4753 if (!BASELINK_P (fns
))
4755 call
= build_field_call (instance_ptr
, fns
, args
);
4758 error ("call to non-function `%D'", fns
);
4759 return error_mark_node
;
4762 if (!conversion_path
)
4763 conversion_path
= BASELINK_BINFO (fns
);
4764 access_binfo
= BASELINK_ACCESS_BINFO (fns
);
4765 optype
= BASELINK_OPTYPE (fns
);
4766 fns
= BASELINK_FUNCTIONS (fns
);
4768 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
4770 explicit_targs
= TREE_OPERAND (fns
, 1);
4771 fns
= TREE_OPERAND (fns
, 0);
4775 my_friendly_assert (TREE_CODE (fns
) == FUNCTION_DECL
4776 || TREE_CODE (fns
) == TEMPLATE_DECL
4777 || TREE_CODE (fns
) == OVERLOAD
,
4780 /* XXX this should be handled before we get here. */
4781 if (! IS_AGGR_TYPE (basetype
))
4783 if ((flags
& LOOKUP_COMPLAIN
) && basetype
!= error_mark_node
)
4784 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4785 fns
, instance
, basetype
);
4787 return error_mark_node
;
4790 name
= DECL_NAME (get_first_fn (fns
));
4792 if (IDENTIFIER_CTOR_OR_DTOR_P (name
))
4794 /* Callers should explicitly indicate whether they want to construct
4795 the complete object or just the part without virtual bases. */
4796 my_friendly_assert (name
!= ctor_identifier
, 20000408);
4797 /* Similarly for destructors. */
4798 my_friendly_assert (name
!= dtor_identifier
, 20000408);
4800 if (name
== complete_ctor_identifier
4801 || name
== base_ctor_identifier
)
4802 pretty_name
= constructor_name (basetype
);
4804 pretty_name
= dtor_identifier
;
4812 tree class_type
= (conversion_path
4813 ? BINFO_TYPE (conversion_path
)
4816 mem_args
= tree_cons (NULL_TREE
, instance_ptr
, args
);
4817 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
4819 tree t
= OVL_CURRENT (fn
);
4822 /* We can end up here for copy-init of same or base class. */
4823 if ((flags
& LOOKUP_ONLYCONVERTING
)
4824 && DECL_NONCONVERTING_P (t
))
4827 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
4828 this_arglist
= mem_args
;
4830 this_arglist
= args
;
4832 if (TREE_CODE (t
) == TEMPLATE_DECL
)
4834 /* A member template. */
4835 templates
= tree_cons (NULL_TREE
, t
, templates
);
4837 add_template_candidate (candidates
, t
,
4840 this_arglist
, optype
,
4846 else if (! template_only
)
4847 candidates
= add_function_candidate (candidates
, t
,
4856 if (! any_viable (candidates
))
4858 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4859 if (flags
& LOOKUP_SPECULATIVELY
)
4861 if (!COMPLETE_TYPE_P (basetype
))
4862 cxx_incomplete_type_error (instance_ptr
, basetype
);
4864 error ("no matching function for call to `%T::%D(%A)%#V'",
4865 basetype
, pretty_name
, user_args
,
4866 TREE_TYPE (TREE_TYPE (instance_ptr
)));
4867 print_z_candidates (candidates
);
4868 return error_mark_node
;
4870 candidates
= splice_viable (candidates
);
4871 cand
= tourney (candidates
);
4875 error ("call of overloaded `%D(%A)' is ambiguous", pretty_name
,
4877 print_z_candidates (candidates
);
4878 return error_mark_node
;
4881 if (DECL_PURE_VIRTUAL_P (cand
->fn
)
4882 && instance
== current_class_ref
4883 && (DECL_CONSTRUCTOR_P (current_function_decl
)
4884 || DECL_DESTRUCTOR_P (current_function_decl
))
4885 && ! (flags
& LOOKUP_NONVIRTUAL
)
4886 && value_member (cand
->fn
, CLASSTYPE_PURE_VIRTUALS (basetype
)))
4887 error ((DECL_CONSTRUCTOR_P (current_function_decl
) ?
4888 "abstract virtual `%#D' called from constructor"
4889 : "abstract virtual `%#D' called from destructor"),
4891 if (TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
4892 && is_dummy_object (instance_ptr
))
4894 error ("cannot call member function `%D' without object", cand
->fn
);
4895 return error_mark_node
;
4898 if (DECL_VINDEX (cand
->fn
) && ! (flags
& LOOKUP_NONVIRTUAL
)
4899 && resolves_to_fixed_type_p (instance
, 0))
4900 flags
|= LOOKUP_NONVIRTUAL
;
4902 if (TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
)
4903 call
= build_over_call (cand
, mem_args
, flags
);
4906 call
= build_over_call (cand
, args
, flags
);
4907 /* In an expression of the form `a->f()' where `f' turns out to
4908 be a static member function, `a' is none-the-less evaluated. */
4909 if (instance
&& TREE_SIDE_EFFECTS (instance
))
4910 call
= build (COMPOUND_EXPR
, TREE_TYPE (call
), instance
, call
);
4916 /* Returns nonzero iff standard conversion sequence ICS1 is a proper
4917 subsequence of ICS2. */
4920 is_subseq (ics1
, ics2
)
4923 /* We can assume that a conversion of the same code
4924 between the same types indicates a subsequence since we only get
4925 here if the types we are converting from are the same. */
4927 while (TREE_CODE (ics1
) == RVALUE_CONV
4928 || TREE_CODE (ics1
) == LVALUE_CONV
)
4929 ics1
= TREE_OPERAND (ics1
, 0);
4933 while (TREE_CODE (ics2
) == RVALUE_CONV
4934 || TREE_CODE (ics2
) == LVALUE_CONV
)
4935 ics2
= TREE_OPERAND (ics2
, 0);
4937 if (TREE_CODE (ics2
) == USER_CONV
4938 || TREE_CODE (ics2
) == AMBIG_CONV
4939 || TREE_CODE (ics2
) == IDENTITY_CONV
)
4940 /* At this point, ICS1 cannot be a proper subsequence of
4941 ICS2. We can get a USER_CONV when we are comparing the
4942 second standard conversion sequence of two user conversion
4946 ics2
= TREE_OPERAND (ics2
, 0);
4948 if (TREE_CODE (ics2
) == TREE_CODE (ics1
)
4949 && same_type_p (TREE_TYPE (ics2
), TREE_TYPE (ics1
))
4950 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2
, 0)),
4951 TREE_TYPE (TREE_OPERAND (ics1
, 0))))
4956 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
4957 be any _TYPE nodes. */
4960 is_properly_derived_from (derived
, base
)
4964 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived
))
4965 || !IS_AGGR_TYPE_CODE (TREE_CODE (base
)))
4968 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4969 considers every class derived from itself. */
4970 return (!same_type_ignoring_top_level_qualifiers_p (derived
, base
)
4971 && DERIVED_FROM_P (base
, derived
));
4974 /* We build the ICS for an implicit object parameter as a pointer
4975 conversion sequence. However, such a sequence should be compared
4976 as if it were a reference conversion sequence. If ICS is the
4977 implicit conversion sequence for an implicit object parameter,
4978 modify it accordingly. */
4981 maybe_handle_implicit_object (ics
)
4984 if (ICS_THIS_FLAG (*ics
))
4986 /* [over.match.funcs]
4988 For non-static member functions, the type of the
4989 implicit object parameter is "reference to cv X"
4990 where X is the class of which the function is a
4991 member and cv is the cv-qualification on the member
4992 function declaration. */
4994 tree reference_type
;
4996 /* The `this' parameter is a pointer to a class type. Make the
4997 implict conversion talk about a reference to that same class
4999 reference_type
= TREE_TYPE (TREE_TYPE (*ics
));
5000 reference_type
= build_reference_type (reference_type
);
5002 if (TREE_CODE (t
) == QUAL_CONV
)
5003 t
= TREE_OPERAND (t
, 0);
5004 if (TREE_CODE (t
) == PTR_CONV
)
5005 t
= TREE_OPERAND (t
, 0);
5006 t
= build1 (IDENTITY_CONV
, TREE_TYPE (TREE_TYPE (t
)), NULL_TREE
);
5007 t
= direct_reference_binding (reference_type
, t
);
5012 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5013 and return the type to which the reference refers. Otherwise,
5014 leave *ICS unchanged and return NULL_TREE. */
5017 maybe_handle_ref_bind (ics
)
5020 if (TREE_CODE (*ics
) == REF_BIND
)
5022 tree old_ics
= *ics
;
5023 tree type
= TREE_TYPE (TREE_TYPE (old_ics
));
5024 *ics
= TREE_OPERAND (old_ics
, 0);
5025 ICS_USER_FLAG (*ics
) = ICS_USER_FLAG (old_ics
);
5026 ICS_BAD_FLAG (*ics
) = ICS_BAD_FLAG (old_ics
);
5033 /* Compare two implicit conversion sequences according to the rules set out in
5034 [over.ics.rank]. Return values:
5036 1: ics1 is better than ics2
5037 -1: ics2 is better than ics1
5038 0: ics1 and ics2 are indistinguishable */
5041 compare_ics (ics1
, ics2
)
5048 tree deref_from_type1
= NULL_TREE
;
5049 tree deref_from_type2
= NULL_TREE
;
5050 tree deref_to_type1
= NULL_TREE
;
5051 tree deref_to_type2
= NULL_TREE
;
5054 /* REF_BINDING is nonzero if the result of the conversion sequence
5055 is a reference type. In that case TARGET_TYPE is the
5056 type referred to by the reference. */
5060 /* Handle implicit object parameters. */
5061 maybe_handle_implicit_object (&ics1
);
5062 maybe_handle_implicit_object (&ics2
);
5064 /* Handle reference parameters. */
5065 target_type1
= maybe_handle_ref_bind (&ics1
);
5066 target_type2
= maybe_handle_ref_bind (&ics2
);
5070 When comparing the basic forms of implicit conversion sequences (as
5071 defined in _over.best.ics_)
5073 --a standard conversion sequence (_over.ics.scs_) is a better
5074 conversion sequence than a user-defined conversion sequence
5075 or an ellipsis conversion sequence, and
5077 --a user-defined conversion sequence (_over.ics.user_) is a
5078 better conversion sequence than an ellipsis conversion sequence
5079 (_over.ics.ellipsis_). */
5080 rank1
= ICS_RANK (ics1
);
5081 rank2
= ICS_RANK (ics2
);
5085 else if (rank1
< rank2
)
5088 if (rank1
== BAD_RANK
)
5090 /* XXX Isn't this an extension? */
5091 /* Both ICS are bad. We try to make a decision based on what
5092 would have happenned if they'd been good. */
5093 if (ICS_USER_FLAG (ics1
) > ICS_USER_FLAG (ics2
)
5094 || ICS_STD_RANK (ics1
) > ICS_STD_RANK (ics2
))
5096 else if (ICS_USER_FLAG (ics1
) < ICS_USER_FLAG (ics2
)
5097 || ICS_STD_RANK (ics1
) < ICS_STD_RANK (ics2
))
5100 /* We couldn't make up our minds; try to figure it out below. */
5103 if (ICS_ELLIPSIS_FLAG (ics1
))
5104 /* Both conversions are ellipsis conversions. */
5107 /* User-defined conversion sequence U1 is a better conversion sequence
5108 than another user-defined conversion sequence U2 if they contain the
5109 same user-defined conversion operator or constructor and if the sec-
5110 ond standard conversion sequence of U1 is better than the second
5111 standard conversion sequence of U2. */
5113 if (ICS_USER_FLAG (ics1
))
5117 for (t1
= ics1
; TREE_CODE (t1
) != USER_CONV
; t1
= TREE_OPERAND (t1
, 0))
5118 if (TREE_CODE (t1
) == AMBIG_CONV
)
5120 for (t2
= ics2
; TREE_CODE (t2
) != USER_CONV
; t2
= TREE_OPERAND (t2
, 0))
5121 if (TREE_CODE (t2
) == AMBIG_CONV
)
5124 if (USER_CONV_FN (t1
) != USER_CONV_FN (t2
))
5127 /* We can just fall through here, after setting up
5128 FROM_TYPE1 and FROM_TYPE2. */
5129 from_type1
= TREE_TYPE (t1
);
5130 from_type2
= TREE_TYPE (t2
);
5134 /* We're dealing with two standard conversion sequences.
5138 Standard conversion sequence S1 is a better conversion
5139 sequence than standard conversion sequence S2 if
5141 --S1 is a proper subsequence of S2 (comparing the conversion
5142 sequences in the canonical form defined by _over.ics.scs_,
5143 excluding any Lvalue Transformation; the identity
5144 conversion sequence is considered to be a subsequence of
5145 any non-identity conversion sequence */
5148 while (TREE_CODE (from_type1
) != IDENTITY_CONV
)
5149 from_type1
= TREE_OPERAND (from_type1
, 0);
5150 from_type1
= TREE_TYPE (from_type1
);
5153 while (TREE_CODE (from_type2
) != IDENTITY_CONV
)
5154 from_type2
= TREE_OPERAND (from_type2
, 0);
5155 from_type2
= TREE_TYPE (from_type2
);
5158 if (same_type_p (from_type1
, from_type2
))
5160 if (is_subseq (ics1
, ics2
))
5162 if (is_subseq (ics2
, ics1
))
5165 /* Otherwise, one sequence cannot be a subsequence of the other; they
5166 don't start with the same type. This can happen when comparing the
5167 second standard conversion sequence in two user-defined conversion
5174 --the rank of S1 is better than the rank of S2 (by the rules
5177 Standard conversion sequences are ordered by their ranks: an Exact
5178 Match is a better conversion than a Promotion, which is a better
5179 conversion than a Conversion.
5181 Two conversion sequences with the same rank are indistinguishable
5182 unless one of the following rules applies:
5184 --A conversion that is not a conversion of a pointer, or pointer
5185 to member, to bool is better than another conversion that is such
5188 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5189 so that we do not have to check it explicitly. */
5190 if (ICS_STD_RANK (ics1
) < ICS_STD_RANK (ics2
))
5192 else if (ICS_STD_RANK (ics2
) < ICS_STD_RANK (ics1
))
5195 to_type1
= TREE_TYPE (ics1
);
5196 to_type2
= TREE_TYPE (ics2
);
5198 if (TYPE_PTR_P (from_type1
)
5199 && TYPE_PTR_P (from_type2
)
5200 && TYPE_PTR_P (to_type1
)
5201 && TYPE_PTR_P (to_type2
))
5203 deref_from_type1
= TREE_TYPE (from_type1
);
5204 deref_from_type2
= TREE_TYPE (from_type2
);
5205 deref_to_type1
= TREE_TYPE (to_type1
);
5206 deref_to_type2
= TREE_TYPE (to_type2
);
5208 /* The rules for pointers to members A::* are just like the rules
5209 for pointers A*, except opposite: if B is derived from A then
5210 A::* converts to B::*, not vice versa. For that reason, we
5211 switch the from_ and to_ variables here. */
5212 else if (TYPE_PTRMEM_P (from_type1
)
5213 && TYPE_PTRMEM_P (from_type2
)
5214 && TYPE_PTRMEM_P (to_type1
)
5215 && TYPE_PTRMEM_P (to_type2
))
5217 deref_to_type1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1
));
5218 deref_to_type2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2
));
5219 deref_from_type1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1
));
5220 deref_from_type2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2
));
5222 else if (TYPE_PTRMEMFUNC_P (from_type1
)
5223 && TYPE_PTRMEMFUNC_P (from_type2
)
5224 && TYPE_PTRMEMFUNC_P (to_type1
)
5225 && TYPE_PTRMEMFUNC_P (to_type2
))
5227 deref_to_type1
= TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1
);
5228 deref_to_type2
= TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2
);
5229 deref_from_type1
= TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1
);
5230 deref_from_type2
= TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2
);
5233 if (deref_from_type1
!= NULL_TREE
5234 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1
))
5235 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2
)))
5237 /* This was one of the pointer or pointer-like conversions.
5241 --If class B is derived directly or indirectly from class A,
5242 conversion of B* to A* is better than conversion of B* to
5243 void*, and conversion of A* to void* is better than
5244 conversion of B* to void*. */
5245 if (TREE_CODE (deref_to_type1
) == VOID_TYPE
5246 && TREE_CODE (deref_to_type2
) == VOID_TYPE
)
5248 if (is_properly_derived_from (deref_from_type1
,
5251 else if (is_properly_derived_from (deref_from_type2
,
5255 else if (TREE_CODE (deref_to_type1
) == VOID_TYPE
5256 || TREE_CODE (deref_to_type2
) == VOID_TYPE
)
5258 if (same_type_p (deref_from_type1
, deref_from_type2
))
5260 if (TREE_CODE (deref_to_type2
) == VOID_TYPE
)
5262 if (is_properly_derived_from (deref_from_type1
,
5266 /* We know that DEREF_TO_TYPE1 is `void' here. */
5267 else if (is_properly_derived_from (deref_from_type1
,
5272 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1
))
5273 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2
)))
5277 --If class B is derived directly or indirectly from class A
5278 and class C is derived directly or indirectly from B,
5280 --conversion of C* to B* is better than conversion of C* to
5283 --conversion of B* to A* is better than conversion of C* to
5285 if (same_type_p (deref_from_type1
, deref_from_type2
))
5287 if (is_properly_derived_from (deref_to_type1
,
5290 else if (is_properly_derived_from (deref_to_type2
,
5294 else if (same_type_p (deref_to_type1
, deref_to_type2
))
5296 if (is_properly_derived_from (deref_from_type2
,
5299 else if (is_properly_derived_from (deref_from_type1
,
5305 else if (CLASS_TYPE_P (non_reference (from_type1
))
5306 && same_type_p (from_type1
, from_type2
))
5308 tree from
= non_reference (from_type1
);
5312 --binding of an expression of type C to a reference of type
5313 B& is better than binding an expression of type C to a
5314 reference of type A&
5316 --conversion of C to B is better than conversion of C to A, */
5317 if (is_properly_derived_from (from
, to_type1
)
5318 && is_properly_derived_from (from
, to_type2
))
5320 if (is_properly_derived_from (to_type1
, to_type2
))
5322 else if (is_properly_derived_from (to_type2
, to_type1
))
5326 else if (CLASS_TYPE_P (non_reference (to_type1
))
5327 && same_type_p (to_type1
, to_type2
))
5329 tree to
= non_reference (to_type1
);
5333 --binding of an expression of type B to a reference of type
5334 A& is better than binding an expression of type C to a
5335 reference of type A&,
5337 --onversion of B to A is better than conversion of C to A */
5338 if (is_properly_derived_from (from_type1
, to
)
5339 && is_properly_derived_from (from_type2
, to
))
5341 if (is_properly_derived_from (from_type2
, from_type1
))
5343 else if (is_properly_derived_from (from_type1
, from_type2
))
5350 --S1 and S2 differ only in their qualification conversion and yield
5351 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5352 qualification signature of type T1 is a proper subset of the cv-
5353 qualification signature of type T2 */
5354 if (TREE_CODE (ics1
) == QUAL_CONV
5355 && TREE_CODE (ics2
) == QUAL_CONV
5356 && same_type_p (from_type1
, from_type2
))
5357 return comp_cv_qual_signature (to_type1
, to_type2
);
5361 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5362 types to which the references refer are the same type except for
5363 top-level cv-qualifiers, and the type to which the reference
5364 initialized by S2 refers is more cv-qualified than the type to
5365 which the reference initialized by S1 refers */
5367 if (target_type1
&& target_type2
5368 && same_type_ignoring_top_level_qualifiers_p (to_type1
, to_type2
))
5369 return comp_cv_qualification (target_type2
, target_type1
);
5371 /* Neither conversion sequence is better than the other. */
5375 /* The source type for this standard conversion sequence. */
5381 for (;; t
= TREE_OPERAND (t
, 0))
5383 if (TREE_CODE (t
) == USER_CONV
5384 || TREE_CODE (t
) == AMBIG_CONV
5385 || TREE_CODE (t
) == IDENTITY_CONV
)
5386 return TREE_TYPE (t
);
5391 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5392 a pointer to LOSER and re-running joust to produce the warning if WINNER
5393 is actually used. */
5396 add_warning (winner
, loser
)
5397 struct z_candidate
*winner
, *loser
;
5399 winner
->warnings
= tree_cons (NULL_TREE
,
5400 build_zc_wrapper (loser
),
5404 /* Returns true iff functions are equivalent. Equivalent functions are
5405 not '==' only if one is a function-local extern function or if
5406 both are extern "C". */
5409 equal_functions (fn1
, fn2
)
5413 if (DECL_LOCAL_FUNCTION_P (fn1
) || DECL_LOCAL_FUNCTION_P (fn2
)
5414 || DECL_EXTERN_C_FUNCTION_P (fn1
))
5415 return decls_match (fn1
, fn2
);
5419 /* Compare two candidates for overloading as described in
5420 [over.match.best]. Return values:
5422 1: cand1 is better than cand2
5423 -1: cand2 is better than cand1
5424 0: cand1 and cand2 are indistinguishable */
5427 joust (cand1
, cand2
, warn
)
5428 struct z_candidate
*cand1
, *cand2
;
5432 int i
, off1
= 0, off2
= 0, len
;
5434 /* Candidates that involve bad conversions are always worse than those
5436 if (cand1
->viable
> cand2
->viable
)
5438 if (cand1
->viable
< cand2
->viable
)
5441 /* If we have two pseudo-candidates for conversions to the same type,
5442 or two candidates for the same function, arbitrarily pick one. */
5443 if (cand1
->fn
== cand2
->fn
5444 && (TYPE_P (cand1
->fn
) || DECL_P (cand1
->fn
)))
5447 /* a viable function F1
5448 is defined to be a better function than another viable function F2 if
5449 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5450 ICSi(F2), and then */
5452 /* for some argument j, ICSj(F1) is a better conversion sequence than
5455 /* For comparing static and non-static member functions, we ignore
5456 the implicit object parameter of the non-static function. The
5457 standard says to pretend that the static function has an object
5458 parm, but that won't work with operator overloading. */
5459 len
= TREE_VEC_LENGTH (cand1
->convs
);
5460 if (len
!= TREE_VEC_LENGTH (cand2
->convs
))
5462 if (DECL_STATIC_FUNCTION_P (cand1
->fn
)
5463 && ! DECL_STATIC_FUNCTION_P (cand2
->fn
))
5465 else if (! DECL_STATIC_FUNCTION_P (cand1
->fn
)
5466 && DECL_STATIC_FUNCTION_P (cand2
->fn
))
5475 for (i
= 0; i
< len
; ++i
)
5477 tree t1
= TREE_VEC_ELT (cand1
->convs
, i
+off1
);
5478 tree t2
= TREE_VEC_ELT (cand2
->convs
, i
+off2
);
5479 int comp
= compare_ics (t1
, t2
);
5484 && ICS_RANK (t1
) + ICS_RANK (t2
) == STD_RANK
+ PROMO_RANK
5485 && TREE_CODE (t1
) == STD_CONV
5486 && TREE_CODE (t2
) == STD_CONV
5487 && TREE_CODE (TREE_TYPE (t1
)) == INTEGER_TYPE
5488 && TREE_CODE (TREE_TYPE (t2
)) == INTEGER_TYPE
5489 && (TYPE_PRECISION (TREE_TYPE (t1
))
5490 == TYPE_PRECISION (TREE_TYPE (t2
)))
5491 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1
, 0)))
5492 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1
, 0)))
5495 tree type
= TREE_TYPE (TREE_OPERAND (t1
, 0));
5497 struct z_candidate
*w
, *l
;
5499 type1
= TREE_TYPE (t1
), type2
= TREE_TYPE (t2
),
5500 w
= cand1
, l
= cand2
;
5502 type1
= TREE_TYPE (t2
), type2
= TREE_TYPE (t1
),
5503 w
= cand2
, l
= cand1
;
5507 warning ("passing `%T' chooses `%T' over `%T'",
5508 type
, type1
, type2
);
5509 warning (" in call to `%D'", w
->fn
);
5515 if (winner
&& comp
!= winner
)
5524 /* warn about confusing overload resolution for user-defined conversions,
5525 either between a constructor and a conversion op, or between two
5527 if (winner
&& cand1
->second_conv
5528 && ((DECL_CONSTRUCTOR_P (cand1
->fn
)
5529 != DECL_CONSTRUCTOR_P (cand2
->fn
))
5530 /* Don't warn if the two conv ops convert to the same type... */
5531 || (! DECL_CONSTRUCTOR_P (cand1
->fn
)
5532 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1
->fn
)),
5533 TREE_TYPE (TREE_TYPE (cand2
->fn
))))))
5535 int comp
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
5538 struct z_candidate
*w
, *l
;
5541 w
= cand1
, l
= cand2
;
5543 w
= cand2
, l
= cand1
;
5544 if (DECL_CONTEXT (cand1
->fn
) == DECL_CONTEXT (cand2
->fn
)
5545 && ! DECL_CONSTRUCTOR_P (cand1
->fn
)
5546 && ! DECL_CONSTRUCTOR_P (cand2
->fn
)
5547 && (convn
= standard_conversion
5548 (TREE_TYPE (TREE_TYPE (l
->fn
)),
5549 TREE_TYPE (TREE_TYPE (w
->fn
)), NULL_TREE
))
5550 && TREE_CODE (convn
) == QUAL_CONV
)
5551 /* Don't complain about `operator char *()' beating
5552 `operator const char *() const'. */;
5555 tree source
= source_type (TREE_VEC_ELT (w
->convs
, 0));
5556 if (! DECL_CONSTRUCTOR_P (w
->fn
))
5557 source
= TREE_TYPE (source
);
5558 warning ("choosing `%D' over `%D'", w
->fn
, l
->fn
);
5559 warning (" for conversion from `%T' to `%T'",
5560 source
, TREE_TYPE (w
->second_conv
));
5561 warning (" because conversion sequence for the argument is better");
5572 F1 is a non-template function and F2 is a template function
5575 if (! cand1
->template && cand2
->template)
5577 else if (cand1
->template && ! cand2
->template)
5581 F1 and F2 are template functions and the function template for F1 is
5582 more specialized than the template for F2 according to the partial
5585 if (cand1
->template && cand2
->template)
5587 winner
= more_specialized
5588 (TI_TEMPLATE (cand1
->template), TI_TEMPLATE (cand2
->template),
5590 /* Tell the deduction code how many real function arguments
5591 we saw, not counting the implicit 'this' argument. But,
5592 add_function_candidate() suppresses the "this" argument
5595 [temp.func.order]: The presence of unused ellipsis and default
5596 arguments has no effect on the partial ordering of function
5598 TREE_VEC_LENGTH (cand1
->convs
)
5599 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1
->fn
)
5600 - DECL_CONSTRUCTOR_P (cand1
->fn
)));
5607 the context is an initialization by user-defined conversion (see
5608 _dcl.init_ and _over.match.user_) and the standard conversion
5609 sequence from the return type of F1 to the destination type (i.e.,
5610 the type of the entity being initialized) is a better conversion
5611 sequence than the standard conversion sequence from the return type
5612 of F2 to the destination type. */
5614 if (cand1
->second_conv
)
5616 winner
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
5621 /* Check whether we can discard a builtin candidate, either because we
5622 have two identical ones or matching builtin and non-builtin candidates.
5624 (Pedantically in the latter case the builtin which matched the user
5625 function should not be added to the overload set, but we spot it here.
5628 ... the builtin candidates include ...
5629 - do not have the same parameter type list as any non-template
5630 non-member candidate. */
5632 if (TREE_CODE (cand1
->fn
) == IDENTIFIER_NODE
5633 || TREE_CODE (cand2
->fn
) == IDENTIFIER_NODE
)
5635 for (i
= 0; i
< len
; ++i
)
5636 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1
->convs
, i
)),
5637 TREE_TYPE (TREE_VEC_ELT (cand2
->convs
, i
))))
5639 if (i
== TREE_VEC_LENGTH (cand1
->convs
))
5641 if (cand1
->fn
== cand2
->fn
)
5642 /* Two built-in candidates; arbitrarily pick one. */
5644 else if (TREE_CODE (cand1
->fn
) == IDENTIFIER_NODE
)
5645 /* cand1 is built-in; prefer cand2. */
5648 /* cand2 is built-in; prefer cand1. */
5653 /* If the two functions are the same (this can happen with declarations
5654 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5655 if (DECL_P (cand1
->fn
) && DECL_P (cand2
->fn
)
5656 && equal_functions (cand1
->fn
, cand2
->fn
))
5661 /* Extension: If the worst conversion for one candidate is worse than the
5662 worst conversion for the other, take the first. */
5665 int rank1
= IDENTITY_RANK
, rank2
= IDENTITY_RANK
;
5666 struct z_candidate
*w
= 0, *l
= 0;
5668 for (i
= 0; i
< len
; ++i
)
5670 if (ICS_RANK (TREE_VEC_ELT (cand1
->convs
, i
+off1
)) > rank1
)
5671 rank1
= ICS_RANK (TREE_VEC_ELT (cand1
->convs
, i
+off1
));
5672 if (ICS_RANK (TREE_VEC_ELT (cand2
->convs
, i
+off2
)) > rank2
)
5673 rank2
= ICS_RANK (TREE_VEC_ELT (cand2
->convs
, i
+off2
));
5676 winner
= 1, w
= cand1
, l
= cand2
;
5678 winner
= -1, w
= cand2
, l
= cand1
;
5683 pedwarn ("choosing `%D' over `%D'", w
->fn
, l
->fn
);
5685 " because worst conversion for the former is better than worst conversion for the latter");
5693 my_friendly_assert (!winner
, 20010121);
5697 /* Given a list of candidates for overloading, find the best one, if any.
5698 This algorithm has a worst case of O(2n) (winner is last), and a best
5699 case of O(n/2) (totally ambiguous); much better than a sorting
5702 static struct z_candidate
*
5703 tourney (candidates
)
5704 struct z_candidate
*candidates
;
5706 struct z_candidate
*champ
= candidates
, *challenger
;
5708 int champ_compared_to_predecessor
= 0;
5710 /* Walk through the list once, comparing each current champ to the next
5711 candidate, knocking out a candidate or two with each comparison. */
5713 for (challenger
= champ
->next
; challenger
; )
5715 fate
= joust (champ
, challenger
, 0);
5717 challenger
= challenger
->next
;
5722 champ
= challenger
->next
;
5725 champ_compared_to_predecessor
= 0;
5730 champ_compared_to_predecessor
= 1;
5733 challenger
= champ
->next
;
5737 /* Make sure the champ is better than all the candidates it hasn't yet
5738 been compared to. */
5740 for (challenger
= candidates
;
5742 && !(champ_compared_to_predecessor
&& challenger
->next
== champ
);
5743 challenger
= challenger
->next
)
5745 fate
= joust (champ
, challenger
, 0);
5753 /* Returns nonzero if things of type FROM can be converted to TO. */
5756 can_convert (to
, from
)
5759 return can_convert_arg (to
, from
, NULL_TREE
);
5762 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
5765 can_convert_arg (to
, from
, arg
)
5768 tree t
= implicit_conversion (to
, from
, arg
, LOOKUP_NORMAL
);
5769 return (t
&& ! ICS_BAD_FLAG (t
));
5772 /* Like can_convert_arg, but allows dubious conversions as well. */
5775 can_convert_arg_bad (to
, from
, arg
)
5778 tree t
= implicit_conversion (to
, from
, arg
, LOOKUP_NORMAL
);
5782 /* Convert EXPR to TYPE. Return the converted expression.
5784 Note that we allow bad conversions here because by the time we get to
5785 this point we are committed to doing the conversion. If we end up
5786 doing a bad conversion, convert_like will complain. */
5789 perform_implicit_conversion (type
, expr
)
5795 if (expr
== error_mark_node
)
5796 return error_mark_node
;
5797 conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
5801 error ("could not convert `%E' to `%T'", expr
, type
);
5802 return error_mark_node
;
5805 return convert_like (conv
, expr
);
5808 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5809 initializing a variable of that TYPE. Return the converted
5813 initialize_reference (type
, expr
)
5819 conv
= reference_binding (type
, TREE_TYPE (expr
), expr
, LOOKUP_NORMAL
);
5820 if (!conv
|| ICS_BAD_FLAG (conv
))
5822 error ("could not convert `%E' to `%T'", expr
, type
);
5823 return error_mark_node
;
5826 return convert_like (conv
, expr
);
5829 #include "gt-cp-call.h"