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, 2003 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 GCC.
9 GCC 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 GCC 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 GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 /* High-level class interface. */
29 #include "coretypes.h"
38 #include "diagnostic.h"
40 extern int inhibit_warnings;
42 static tree build_field_call (tree, tree, tree);
43 static struct z_candidate * tourney (struct z_candidate *);
44 static int equal_functions (tree, tree);
45 static int joust (struct z_candidate *, struct z_candidate *, bool);
46 static int compare_ics (tree, tree);
47 static tree build_over_call (struct z_candidate *, int);
48 static tree build_java_interface_fn_ref (tree, tree);
49 #define convert_like(CONV, EXPR) \
50 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0)
51 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
52 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0)
53 static tree convert_like_real (tree, tree, tree, int, int);
54 static void op_error (enum tree_code, enum tree_code, tree, tree,
56 static tree build_object_call (tree, tree);
57 static tree resolve_args (tree);
58 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
59 static void print_z_candidates (struct z_candidate *);
60 static tree build_this (tree);
61 static struct z_candidate *splice_viable (struct z_candidate *);
62 static bool any_viable (struct z_candidate *);
63 static bool any_strictly_viable (struct z_candidate *);
64 static struct z_candidate *add_template_candidate
65 (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 (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 (struct z_candidate **, tree, tree, tree, tree, tree, tree);
72 static void add_builtin_candidates
73 (struct z_candidate **, enum tree_code, enum tree_code,
75 static void add_builtin_candidate
76 (struct z_candidate **, enum tree_code, enum tree_code,
77 tree, tree, tree, tree *, tree *, int);
78 static bool is_complete (tree);
79 static void build_builtin_candidate
80 (struct z_candidate **, tree, tree, tree, tree *, tree *,
82 static struct z_candidate *add_conv_candidate
83 (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 (tree, tree, tree, int);
87 static tree standard_conversion (tree, tree, tree);
88 static tree reference_binding (tree, tree, tree, int);
89 static tree non_reference (tree);
90 static tree build_conv (enum tree_code, tree, tree);
91 static bool is_subseq (tree, tree);
92 static tree maybe_handle_ref_bind (tree *);
93 static void maybe_handle_implicit_object (tree *);
94 static struct z_candidate *add_candidate
95 (struct z_candidate **, tree, tree, tree, tree, tree, int);
96 static tree source_type (tree);
97 static void add_warning (struct z_candidate *, struct z_candidate *);
98 static bool reference_related_p (tree, tree);
99 static bool reference_compatible_p (tree, tree);
100 static tree convert_class_to_reference (tree, tree, tree);
101 static tree direct_reference_binding (tree, tree);
102 static bool promoted_arithmetic_type_p (tree);
103 static tree conditional_conversion (tree, tree);
104 static char *name_as_c_string (tree, tree, bool *);
105 static tree call_builtin_trap (void);
106 static tree prep_operand (tree);
107 static void add_candidates (tree, tree, tree, bool, tree, tree,
108 int, struct z_candidate **);
109 static tree merge_conversion_sequences (tree, tree);
112 build_vfield_ref (tree datum, tree type)
114 if (datum == error_mark_node)
115 return error_mark_node;
117 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
118 datum = convert_from_reference (datum);
120 if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
121 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
122 datum = convert_to_base (datum, type, /*check_access=*/false);
124 return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
125 datum, TYPE_VFIELD (type));
128 /* Build a call to a member of an object. I.e., one that overloads
129 operator ()(), or is a pointer-to-function or pointer-to-method. */
132 build_field_call (tree instance_ptr, tree decl, tree parms)
136 if (decl == error_mark_node || decl == NULL_TREE)
139 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
141 /* If it's a field, try overloading operator (),
142 or calling if the field is a pointer-to-function. */
143 instance = build_indirect_ref (instance_ptr, NULL);
144 instance = build_class_member_access_expr (instance, decl,
145 /*access_path=*/NULL_TREE,
146 /*preserve_reference=*/false);
148 if (instance == error_mark_node)
149 return error_mark_node;
151 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
152 return build_new_op (CALL_EXPR, LOOKUP_NORMAL,
153 instance, parms, NULL_TREE);
154 else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
155 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
156 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
158 return build_function_call (instance, parms);
164 /* Returns nonzero iff the destructor name specified in NAME
165 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
169 check_dtor_name (tree basetype, tree name)
171 name = TREE_OPERAND (name, 0);
173 /* Just accept something we've already complained about. */
174 if (name == error_mark_node)
177 if (TREE_CODE (name) == TYPE_DECL)
178 name = TREE_TYPE (name);
179 else if (TYPE_P (name))
181 else if (TREE_CODE (name) == IDENTIFIER_NODE)
183 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
184 || (TREE_CODE (basetype) == ENUMERAL_TYPE
185 && name == TYPE_IDENTIFIER (basetype)))
188 name = get_type_value (name);
192 template <class T> struct S { ~S(); };
196 NAME will be a class template. */
197 else if (DECL_CLASS_TEMPLATE_P (name))
202 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
207 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
208 This is how virtual function calls are avoided. */
211 build_scoped_method_call (tree exp, tree basetype, tree name, tree parms)
213 /* Because this syntactic form does not allow
214 a pointer to a base class to be `stolen',
215 we need not protect the derived->base conversion
218 @@ But we do have to check access privileges later. */
220 tree type = TREE_TYPE (exp);
222 if (type == error_mark_node
223 || basetype == error_mark_node)
224 return error_mark_node;
226 if (processing_template_decl)
228 name = build_min_nt (SCOPE_REF, basetype, name);
229 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
232 if (TREE_CODE (type) == REFERENCE_TYPE)
233 type = TREE_TYPE (type);
235 if (TREE_CODE (basetype) == TREE_VEC)
238 basetype = BINFO_TYPE (binfo);
243 /* Check the destructor call syntax. */
244 if (TREE_CODE (name) == BIT_NOT_EXPR)
246 /* We can get here if someone writes their destructor call like
247 `obj.NS::~T()'; this isn't really a scoped method call, so hand
249 if (TREE_CODE (basetype) == NAMESPACE_DECL)
250 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
252 if (! check_dtor_name (basetype, name))
253 error ("qualified type `%T' does not match destructor name `~%T'",
254 basetype, TREE_OPERAND (name, 0));
256 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
257 that explicit ~int is caught in the parser; this deals with typedefs
258 and template parms. */
259 if (! IS_AGGR_TYPE (basetype))
261 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
262 error ("type of `%E' does not match destructor type `%T' (type was `%T')",
263 exp, basetype, type);
265 return cp_convert (void_type_node, exp);
269 if (TREE_CODE (basetype) == NAMESPACE_DECL)
271 error ("`%D' is a namespace", basetype);
272 return error_mark_node;
274 if (! is_aggr_type (basetype, 1))
275 return error_mark_node;
277 if (! IS_AGGR_TYPE (type))
279 error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
281 return error_mark_node;
284 decl = build_scoped_ref (exp, basetype, &binfo);
288 /* Call to a destructor. */
289 if (TREE_CODE (name) == BIT_NOT_EXPR)
291 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
292 return cp_convert (void_type_node, exp);
294 return build_delete (TREE_TYPE (decl), decl,
295 sfk_complete_destructor,
296 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
300 /* Call to a method. */
301 return build_method_call (decl, name, parms, binfo,
302 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
304 return error_mark_node;
307 /* We want the address of a function or method. We avoid creating a
308 pointer-to-member function. */
311 build_addr_func (tree function)
313 tree type = TREE_TYPE (function);
315 /* We have to do these by hand to avoid real pointer to member
317 if (TREE_CODE (type) == METHOD_TYPE)
321 type = build_pointer_type (type);
323 if (!cxx_mark_addressable (function))
324 return error_mark_node;
326 addr = build1 (ADDR_EXPR, type, function);
328 /* Address of a static or external variable or function counts
330 if (staticp (function))
331 TREE_CONSTANT (addr) = 1;
336 function = default_conversion (function);
341 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
342 POINTER_TYPE to those. Note, pointer to member function types
343 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
346 build_call (tree function, tree parms)
348 int is_constructor = 0;
355 function = build_addr_func (function);
357 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
359 sorry ("unable to call pointer to member function here");
360 return error_mark_node;
363 fntype = TREE_TYPE (TREE_TYPE (function));
364 result_type = TREE_TYPE (fntype);
366 if (TREE_CODE (function) == ADDR_EXPR
367 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
368 decl = TREE_OPERAND (function, 0);
372 /* We check both the decl and the type; a function may be known not to
373 throw without being declared throw(). */
374 nothrow = ((decl && TREE_NOTHROW (decl))
375 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
377 if (decl && TREE_THIS_VOLATILE (decl) && cfun)
378 current_function_returns_abnormally = 1;
380 if (decl && TREE_DEPRECATED (decl))
381 warn_deprecated_use (decl);
382 require_complete_eh_spec_types (fntype, decl);
384 if (decl && DECL_CONSTRUCTOR_P (decl))
387 if (decl && ! TREE_USED (decl))
389 /* We invoke build_call directly for several library functions.
390 These may have been declared normally if we're building libgcc,
391 so we can't just check DECL_ARTIFICIAL. */
392 if (DECL_ARTIFICIAL (decl)
393 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
399 /* Don't pass empty class objects by value. This is useful
400 for tags in STL, which are used to control overload resolution.
401 We don't need to handle other cases of copying empty classes. */
402 if (! decl || ! DECL_BUILT_IN (decl))
403 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
404 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
405 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
407 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
408 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
409 TREE_VALUE (tmp), t);
412 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
413 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
414 TREE_TYPE (function) = result_type;
415 TREE_SIDE_EFFECTS (function) = 1;
416 TREE_NOTHROW (function) = nothrow;
421 /* Build something of the form ptr->method (args)
422 or object.method (args). This can also build
423 calls to constructors, and find friends.
425 Member functions always take their class variable
428 INSTANCE is a class instance.
430 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
432 PARMS help to figure out what that NAME really refers to.
434 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
435 down to the real instance type to use for access checking. We need this
436 information to get protected accesses correct. This parameter is used
437 by build_member_call.
439 FLAGS is the logical disjunction of zero or more LOOKUP_
440 flags. See cp-tree.h for more info.
442 If this is all OK, calls build_function_call with the resolved
445 This function must also handle being called to perform
446 initialization, promotion/coercion of arguments, and
447 instantiation of default parameters.
449 Note that NAME may refer to an instance variable name. If
450 `operator()()' is defined for the type of that field, then we return
453 #ifdef GATHER_STATISTICS
454 extern int n_build_method_call;
458 build_method_call (tree instance, tree name, tree parms,
459 tree basetype_path, int flags)
463 tree template_args = NULL_TREE;
464 bool has_template_args = false;
466 #ifdef GATHER_STATISTICS
467 n_build_method_call++;
470 if (instance == error_mark_node
471 || name == error_mark_node
472 || parms == error_mark_node
473 || (instance && TREE_TYPE (instance) == error_mark_node))
474 return error_mark_node;
476 if (processing_template_decl)
477 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
479 if (TREE_CODE (instance) == OFFSET_REF)
480 instance = resolve_offset_ref (instance);
481 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
482 instance = convert_from_reference (instance);
483 object_type = TREE_TYPE (instance);
485 if (TREE_CODE (name) == BIT_NOT_EXPR)
490 error ("destructors take no parameters");
492 if (! check_dtor_name (object_type, name))
494 ("destructor name `~%T' does not match type `%T' of expression",
495 TREE_OPERAND (name, 0), object_type);
497 if (! TYPE_HAS_DESTRUCTOR (complete_type (object_type)))
498 return cp_convert (void_type_node, instance);
499 instance = default_conversion (instance);
500 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
501 return build_delete (build_pointer_type (object_type),
502 instance_ptr, sfk_complete_destructor,
503 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
506 if (!CLASS_TYPE_P (object_type))
508 if ((flags & LOOKUP_COMPLAIN)
509 && TREE_TYPE (instance) != error_mark_node)
510 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
511 name, instance, object_type);
512 return error_mark_node;
515 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
517 template_args = TREE_OPERAND (name, 1);
518 has_template_args = true;
519 name = TREE_OPERAND (name, 0);
521 if (TREE_CODE (name) == OVERLOAD)
522 name = DECL_NAME (get_first_fn (name));
523 else if (DECL_P (name))
524 name = DECL_NAME (name);
525 if (has_template_args)
526 fn = lookup_fnfields (object_type, name, /*protect=*/2);
528 fn = lookup_member (object_type, name, /*protect=*/2, /*want_type=*/false);
530 if (fn && TREE_CODE (fn) == TREE_LIST && !BASELINK_P (fn))
532 error ("request for member `%D' is ambiguous", name);
533 print_candidates (fn);
534 return error_mark_node;
537 /* If the name could not be found, issue an error. */
540 unqualified_name_lookup_error (name);
541 return error_mark_node;
544 if (BASELINK_P (fn) && has_template_args)
545 BASELINK_FUNCTIONS (fn)
546 = build_nt (TEMPLATE_ID_EXPR,
547 BASELINK_FUNCTIONS (fn),
549 if (BASELINK_P (fn) && basetype_path)
550 BASELINK_ACCESS_BINFO (fn) = basetype_path;
552 return build_new_method_call (instance, fn, parms,
553 /*conversion_path=*/NULL_TREE, flags);
556 /* New overloading code. */
558 struct z_candidate GTY(()) {
559 /* The FUNCTION_DECL that will be called if this candidate is
560 selected by overload resolution. */
562 /* The arguments to use when calling this function. */
564 /* The implicit conversion sequences for each of the arguments to
567 /* If FN is a user-defined conversion, the standard conversion
568 sequence from the type returned by FN to the desired destination
572 /* If FN is a member function, the binfo indicating the path used to
573 qualify the name of FN at the call site. This path is used to
574 determine whether or not FN is accessible if it is selected by
575 overload resolution. The DECL_CONTEXT of FN will always be a
576 (possibly improper) base of this binfo. */
578 /* If FN is a non-static member function, the binfo indicating the
579 subobject to which the `this' pointer should be converted if FN
580 is selected by overload resolution. The type pointed to the by
581 the `this' pointer must correspond to the most derived class
582 indicated by the CONVERSION_PATH. */
583 tree conversion_path;
586 struct z_candidate *next;
589 #define IDENTITY_RANK 0
595 #define ELLIPSIS_RANK 6
598 #define ICS_RANK(NODE) \
599 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
600 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
601 : ICS_USER_FLAG (NODE) ? USER_RANK \
602 : ICS_STD_RANK (NODE))
604 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
606 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
607 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
608 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
609 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
611 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
612 should be created to hold the result of the conversion. */
613 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
615 #define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
616 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
619 null_ptr_cst_p (tree t)
623 A null pointer constant is an integral constant expression
624 (_expr.const_) rvalue of integer type that evaluates to zero. */
626 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
632 /* Returns nonzero if PARMLIST consists of only default parms and/or
636 sufficient_parms_p (tree parmlist)
638 for (; parmlist && parmlist != void_list_node;
639 parmlist = TREE_CHAIN (parmlist))
640 if (!TREE_PURPOSE (parmlist))
646 build_conv (enum tree_code code, tree type, tree from)
649 int rank = ICS_STD_RANK (from);
651 /* We can't use buildl1 here because CODE could be USER_CONV, which
652 takes two arguments. In that case, the caller is responsible for
653 filling in the second argument. */
654 t = make_node (code);
655 TREE_TYPE (t) = type;
656 TREE_OPERAND (t, 0) = from;
669 if (rank < EXACT_RANK)
675 ICS_STD_RANK (t) = rank;
676 ICS_USER_FLAG (t) = (code == USER_CONV || ICS_USER_FLAG (from));
677 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
681 /* If T is a REFERENCE_TYPE return the type to which T refers.
682 Otherwise, return T itself. */
685 non_reference (tree t)
687 if (TREE_CODE (t) == REFERENCE_TYPE)
693 strip_top_quals (tree t)
695 if (TREE_CODE (t) == ARRAY_TYPE)
697 return TYPE_MAIN_VARIANT (t);
700 /* Returns the standard conversion path (see [conv]) from type FROM to type
701 TO, if any. For proper handling of null pointer constants, you must
702 also pass the expression EXPR to convert from. */
705 standard_conversion (tree to, tree from, tree expr)
707 enum tree_code fcode, tcode;
709 bool fromref = false;
711 if (TREE_CODE (to) == REFERENCE_TYPE)
713 if (TREE_CODE (from) == REFERENCE_TYPE)
716 from = TREE_TYPE (from);
718 to = strip_top_quals (to);
719 from = strip_top_quals (from);
721 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
722 && expr && type_unknown_p (expr))
724 expr = instantiate_type (to, expr, tf_none);
725 if (expr == error_mark_node)
727 from = TREE_TYPE (expr);
730 fcode = TREE_CODE (from);
731 tcode = TREE_CODE (to);
733 conv = build1 (IDENTITY_CONV, from, expr);
735 if (fcode == FUNCTION_TYPE)
737 from = build_pointer_type (from);
738 fcode = TREE_CODE (from);
739 conv = build_conv (LVALUE_CONV, from, conv);
741 else if (fcode == ARRAY_TYPE)
743 from = build_pointer_type (TREE_TYPE (from));
744 fcode = TREE_CODE (from);
745 conv = build_conv (LVALUE_CONV, from, conv);
747 else if (fromref || (expr && lvalue_p (expr)))
748 conv = build_conv (RVALUE_CONV, from, conv);
750 /* Allow conversion between `__complex__' data types */
751 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
753 /* The standard conversion sequence to convert FROM to TO is
754 the standard conversion sequence to perform componentwise
756 tree part_conv = standard_conversion
757 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
761 conv = build_conv (TREE_CODE (part_conv), to, conv);
762 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
770 if (same_type_p (from, to))
773 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
774 && expr && null_ptr_cst_p (expr))
776 conv = build_conv (STD_CONV, to, conv);
778 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
779 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
781 /* For backwards brain damage compatibility, allow interconversion of
782 pointers and integers with a pedwarn. */
783 conv = build_conv (STD_CONV, to, conv);
784 ICS_BAD_FLAG (conv) = 1;
786 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE
787 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
789 /* For backwards brain damage compatibility, allow interconversion of
790 enums and integers with a pedwarn. */
791 conv = build_conv (STD_CONV, to, conv);
792 ICS_BAD_FLAG (conv) = 1;
794 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
796 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
797 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
799 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
802 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
803 && ufcode != FUNCTION_TYPE)
805 from = build_pointer_type
806 (cp_build_qualified_type (void_type_node,
807 cp_type_quals (TREE_TYPE (from))));
808 conv = build_conv (PTR_CONV, from, conv);
810 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
812 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
813 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
815 if (DERIVED_FROM_P (fbase, tbase)
816 && (same_type_ignoring_top_level_qualifiers_p
817 (TREE_TYPE (TREE_TYPE (from)),
818 TREE_TYPE (TREE_TYPE (to)))))
820 from = build_ptrmem_type (tbase, TREE_TYPE (TREE_TYPE (from)));
821 conv = build_conv (PMEM_CONV, from, conv);
824 else if (IS_AGGR_TYPE (TREE_TYPE (from))
825 && IS_AGGR_TYPE (TREE_TYPE (to)))
827 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
830 cp_build_qualified_type (TREE_TYPE (to),
831 cp_type_quals (TREE_TYPE (from)));
832 from = build_pointer_type (from);
833 conv = build_conv (PTR_CONV, from, conv);
837 if (same_type_p (from, to))
839 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
840 conv = build_conv (QUAL_CONV, to, conv);
841 else if (expr && string_conv_p (to, expr, 0))
842 /* converting from string constant to char *. */
843 conv = build_conv (QUAL_CONV, to, conv);
844 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
846 conv = build_conv (PTR_CONV, to, conv);
847 ICS_BAD_FLAG (conv) = 1;
854 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
856 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
857 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
858 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
859 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
861 if (!DERIVED_FROM_P (fbase, tbase)
862 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
863 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
864 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
865 || cp_type_quals (fbase) != cp_type_quals (tbase))
868 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
869 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
870 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
871 from = build_ptrmemfunc_type (build_pointer_type (from));
872 conv = build_conv (PMEM_CONV, from, conv);
874 else if (tcode == BOOLEAN_TYPE)
876 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
877 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
880 conv = build_conv (STD_CONV, to, conv);
881 if (fcode == POINTER_TYPE
882 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
883 ICS_STD_RANK (conv) = PBOOL_RANK;
885 /* We don't check for ENUMERAL_TYPE here because there are no standard
886 conversions to enum type. */
887 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
888 || tcode == REAL_TYPE)
890 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
892 conv = build_conv (STD_CONV, to, conv);
894 /* Give this a better rank if it's a promotion. */
895 if (to == type_promotes_to (from)
896 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
897 ICS_STD_RANK (conv) = PROMO_RANK;
899 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
900 && is_properly_derived_from (from, to))
902 if (TREE_CODE (conv) == RVALUE_CONV)
903 conv = TREE_OPERAND (conv, 0);
904 conv = build_conv (BASE_CONV, to, conv);
905 /* The derived-to-base conversion indicates the initialization
906 of a parameter with base type from an object of a derived
907 type. A temporary object is created to hold the result of
909 NEED_TEMPORARY_P (conv) = 1;
917 /* Returns nonzero if T1 is reference-related to T2. */
920 reference_related_p (tree t1, tree t2)
922 t1 = TYPE_MAIN_VARIANT (t1);
923 t2 = TYPE_MAIN_VARIANT (t2);
927 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
928 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
930 return (same_type_p (t1, t2)
931 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
932 && DERIVED_FROM_P (t1, t2)));
935 /* Returns nonzero if T1 is reference-compatible with T2. */
938 reference_compatible_p (tree t1, tree t2)
942 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
943 reference-related to T2 and cv1 is the same cv-qualification as,
944 or greater cv-qualification than, cv2. */
945 return (reference_related_p (t1, t2)
946 && at_least_as_qualified_p (t1, t2));
949 /* Determine whether or not the EXPR (of class type S) can be
950 converted to T as in [over.match.ref]. */
953 convert_class_to_reference (tree t, tree s, tree expr)
959 struct z_candidate *candidates;
960 struct z_candidate *cand;
962 conversions = lookup_conversions (s);
968 Assuming that "cv1 T" is the underlying type of the reference
969 being initialized, and "cv S" is the type of the initializer
970 expression, with S a class type, the candidate functions are
973 --The conversion functions of S and its base classes are
974 considered. Those that are not hidden within S and yield type
975 "reference to cv2 T2", where "cv1 T" is reference-compatible
976 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
978 The argument list has one argument, which is the initializer
983 /* Conceptually, we should take the address of EXPR and put it in
984 the argument list. Unfortunately, however, that can result in
985 error messages, which we should not issue now because we are just
986 trying to find a conversion operator. Therefore, we use NULL,
987 cast to the appropriate type. */
988 arglist = build_int_2 (0, 0);
989 TREE_TYPE (arglist) = build_pointer_type (s);
990 arglist = build_tree_list (NULL_TREE, arglist);
992 reference_type = build_reference_type (t);
996 tree fns = TREE_VALUE (conversions);
998 for (; fns; fns = OVL_NEXT (fns))
1000 tree f = OVL_CURRENT (fns);
1001 tree t2 = TREE_TYPE (TREE_TYPE (f));
1005 /* If this is a template function, try to get an exact
1007 if (TREE_CODE (f) == TEMPLATE_DECL)
1009 cand = add_template_candidate (&candidates,
1015 TREE_PURPOSE (conversions),
1021 /* Now, see if the conversion function really returns
1022 an lvalue of the appropriate type. From the
1023 point of view of unification, simply returning an
1024 rvalue of the right type is good enough. */
1026 t2 = TREE_TYPE (TREE_TYPE (f));
1027 if (TREE_CODE (t2) != REFERENCE_TYPE
1028 || !reference_compatible_p (t, TREE_TYPE (t2)))
1030 candidates = candidates->next;
1035 else if (TREE_CODE (t2) == REFERENCE_TYPE
1036 && reference_compatible_p (t, TREE_TYPE (t2)))
1037 cand = add_function_candidate (&candidates, f, s, arglist,
1039 TREE_PURPOSE (conversions),
1043 /* Build a standard conversion sequence indicating the
1044 binding from the reference type returned by the
1045 function to the desired REFERENCE_TYPE. */
1047 = (direct_reference_binding
1049 build1 (IDENTITY_CONV,
1050 TREE_TYPE (TREE_TYPE (TREE_TYPE (cand->fn))),
1053 conversions = TREE_CHAIN (conversions);
1056 /* If none of the conversion functions worked out, let our caller
1058 if (!any_viable (candidates))
1061 candidates = splice_viable (candidates);
1062 cand = tourney (candidates);
1066 /* Now that we know that this is the function we're going to use fix
1067 the dummy first argument. */
1068 cand->args = tree_cons (NULL_TREE,
1070 TREE_CHAIN (cand->args));
1072 /* Build a user-defined conversion sequence representing the
1074 conv = build_conv (USER_CONV,
1075 TREE_TYPE (TREE_TYPE (cand->fn)),
1076 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
1077 TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
1079 /* Merge it with the standard conversion sequence from the
1080 conversion function's return type to the desired type. */
1081 cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
1083 if (cand->viable == -1)
1084 ICS_BAD_FLAG (conv) = 1;
1086 return cand->second_conv;
1089 /* A reference of the indicated TYPE is being bound directly to the
1090 expression represented by the implicit conversion sequence CONV.
1091 Return a conversion sequence for this binding. */
1094 direct_reference_binding (tree type, tree conv)
1098 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 20030306);
1099 my_friendly_assert (TREE_CODE (TREE_TYPE (conv)) != REFERENCE_TYPE,
1102 t = TREE_TYPE (type);
1106 When a parameter of reference type binds directly
1107 (_dcl.init.ref_) to an argument expression, the implicit
1108 conversion sequence is the identity conversion, unless the
1109 argument expression has a type that is a derived class of the
1110 parameter type, in which case the implicit conversion sequence is
1111 a derived-to-base Conversion.
1113 If the parameter binds directly to the result of applying a
1114 conversion function to the argument expression, the implicit
1115 conversion sequence is a user-defined conversion sequence
1116 (_over.ics.user_), with the second standard conversion sequence
1117 either an identity conversion or, if the conversion function
1118 returns an entity of a type that is a derived class of the
1119 parameter type, a derived-to-base conversion. */
1120 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1122 /* Represent the derived-to-base conversion. */
1123 conv = build_conv (BASE_CONV, t, conv);
1124 /* We will actually be binding to the base-class subobject in
1125 the derived class, so we mark this conversion appropriately.
1126 That way, convert_like knows not to generate a temporary. */
1127 NEED_TEMPORARY_P (conv) = 0;
1129 return build_conv (REF_BIND, type, conv);
1132 /* Returns the conversion path from type FROM to reference type TO for
1133 purposes of reference binding. For lvalue binding, either pass a
1134 reference type to FROM or an lvalue expression to EXPR. If the
1135 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1136 the conversion returned. */
1139 reference_binding (tree rto, tree rfrom, tree expr, int flags)
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);
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.
1253 We use the first alternative. The implicit conversion sequence
1254 is supposed to be same as we would obtain by generating a
1255 temporary. Fortunately, if the types are reference compatible,
1256 then this is either an identity conversion or the derived-to-base
1257 conversion, just as for direct binding. */
1258 if (CLASS_TYPE_P (from) && compatible_p)
1260 conv = build1 (IDENTITY_CONV, from, expr);
1261 return direct_reference_binding (rto, conv);
1266 Otherwise, a temporary of type "cv1 T1" is created and
1267 initialized from the initializer expression using the rules for a
1268 non-reference copy initialization. If T1 is reference-related to
1269 T2, cv1 must be the same cv-qualification as, or greater
1270 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1271 if (related_p && !at_least_as_qualified_p (to, from))
1274 conv = implicit_conversion (to, from, expr, flags);
1278 conv = build_conv (REF_BIND, rto, conv);
1279 /* This reference binding, unlike those above, requires the
1280 creation of a temporary. */
1281 NEED_TEMPORARY_P (conv) = 1;
1286 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1287 to type TO. The optional expression EXPR may affect the conversion.
1288 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1292 implicit_conversion (tree to, tree from, tree expr, int flags)
1296 /* Resolve expressions like `A::p' that we thought might become
1297 pointers-to-members. */
1298 if (expr && TREE_CODE (expr) == OFFSET_REF)
1300 expr = resolve_offset_ref (expr);
1301 from = TREE_TYPE (expr);
1304 if (from == error_mark_node || to == error_mark_node
1305 || expr == error_mark_node)
1308 /* Make sure both the FROM and TO types are complete so that
1309 user-defined conversions are available. */
1310 complete_type (from);
1313 if (TREE_CODE (to) == REFERENCE_TYPE)
1314 conv = reference_binding (to, from, expr, flags);
1316 conv = standard_conversion (to, from, expr);
1321 if (expr != NULL_TREE
1322 && (IS_AGGR_TYPE (from)
1323 || IS_AGGR_TYPE (to))
1324 && (flags & LOOKUP_NO_CONVERSION) == 0)
1326 struct z_candidate *cand;
1328 cand = build_user_type_conversion_1
1329 (to, expr, LOOKUP_ONLYCONVERTING);
1331 conv = cand->second_conv;
1333 /* We used to try to bind a reference to a temporary here, but that
1334 is now handled by the recursive call to this function at the end
1335 of reference_binding. */
1342 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1345 static struct z_candidate *
1346 add_candidate (struct z_candidate **candidates,
1347 tree fn, tree args, tree convs, tree access_path,
1348 tree conversion_path, int viable)
1350 struct z_candidate *cand
1351 = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1355 cand->convs = convs;
1356 cand->access_path = access_path;
1357 cand->conversion_path = conversion_path;
1358 cand->viable = viable;
1359 cand->next = *candidates;
1365 /* Create an overload candidate for the function or method FN called with
1366 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1367 to implicit_conversion.
1369 CTYPE, if non-NULL, is the type we want to pretend this function
1370 comes from for purposes of overload resolution. */
1372 static struct z_candidate *
1373 add_function_candidate (struct z_candidate **candidates,
1374 tree fn, tree ctype, tree arglist,
1375 tree access_path, tree conversion_path,
1378 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1381 tree parmnode, argnode;
1385 /* Built-in functions that haven't been declared don't really
1387 if (DECL_ANTICIPATED (fn))
1390 /* The `this', `in_chrg' and VTT arguments to constructors are not
1391 considered in overload resolution. */
1392 if (DECL_CONSTRUCTOR_P (fn))
1394 parmlist = skip_artificial_parms_for (fn, parmlist);
1395 orig_arglist = arglist;
1396 arglist = skip_artificial_parms_for (fn, arglist);
1399 orig_arglist = arglist;
1401 len = list_length (arglist);
1402 convs = make_tree_vec (len);
1404 /* 13.3.2 - Viable functions [over.match.viable]
1405 First, to be a viable function, a candidate function shall have enough
1406 parameters to agree in number with the arguments in the list.
1408 We need to check this first; otherwise, checking the ICSes might cause
1409 us to produce an ill-formed template instantiation. */
1411 parmnode = parmlist;
1412 for (i = 0; i < len; ++i)
1414 if (parmnode == NULL_TREE || parmnode == void_list_node)
1416 parmnode = TREE_CHAIN (parmnode);
1419 if (i < len && parmnode)
1422 /* Make sure there are default args for the rest of the parms. */
1423 else if (!sufficient_parms_p (parmnode))
1429 /* Second, for F to be a viable function, there shall exist for each
1430 argument an implicit conversion sequence that converts that argument
1431 to the corresponding parameter of F. */
1433 parmnode = parmlist;
1436 for (i = 0; i < len; ++i)
1438 tree arg = TREE_VALUE (argnode);
1439 tree argtype = lvalue_type (arg);
1443 if (parmnode == void_list_node)
1446 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1447 && ! DECL_CONSTRUCTOR_P (fn));
1451 tree parmtype = TREE_VALUE (parmnode);
1453 /* The type of the implicit object parameter ('this') for
1454 overload resolution is not always the same as for the
1455 function itself; conversion functions are considered to
1456 be members of the class being converted, and functions
1457 introduced by a using-declaration are considered to be
1458 members of the class that uses them.
1460 Since build_over_call ignores the ICS for the `this'
1461 parameter, we can just change the parm type. */
1462 if (ctype && is_this)
1465 = build_qualified_type (ctype,
1466 TYPE_QUALS (TREE_TYPE (parmtype)));
1467 parmtype = build_pointer_type (parmtype);
1470 t = implicit_conversion (parmtype, argtype, arg, flags);
1474 t = build1 (IDENTITY_CONV, argtype, arg);
1475 ICS_ELLIPSIS_FLAG (t) = 1;
1479 ICS_THIS_FLAG (t) = 1;
1481 TREE_VEC_ELT (convs, i) = t;
1488 if (ICS_BAD_FLAG (t))
1492 parmnode = TREE_CHAIN (parmnode);
1493 argnode = TREE_CHAIN (argnode);
1497 return add_candidate (candidates, fn, orig_arglist, convs, access_path,
1498 conversion_path, viable);
1501 /* Create an overload candidate for the conversion function FN which will
1502 be invoked for expression OBJ, producing a pointer-to-function which
1503 will in turn be called with the argument list ARGLIST, and add it to
1504 CANDIDATES. FLAGS is passed on to implicit_conversion.
1506 Actually, we don't really care about FN; we care about the type it
1507 converts to. There may be multiple conversion functions that will
1508 convert to that type, and we rely on build_user_type_conversion_1 to
1509 choose the best one; so when we create our candidate, we record the type
1510 instead of the function. */
1512 static struct z_candidate *
1513 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
1514 tree arglist, tree access_path, tree conversion_path)
1516 tree totype = TREE_TYPE (TREE_TYPE (fn));
1517 int i, len, viable, flags;
1518 tree parmlist, convs, parmnode, argnode;
1520 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1521 parmlist = TREE_TYPE (parmlist);
1522 parmlist = TYPE_ARG_TYPES (parmlist);
1524 len = list_length (arglist) + 1;
1525 convs = make_tree_vec (len);
1526 parmnode = parmlist;
1529 flags = LOOKUP_NORMAL;
1531 /* Don't bother looking up the same type twice. */
1532 if (*candidates && (*candidates)->fn == totype)
1535 for (i = 0; i < len; ++i)
1537 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1538 tree argtype = lvalue_type (arg);
1542 t = implicit_conversion (totype, argtype, arg, flags);
1543 else if (parmnode == void_list_node)
1546 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1549 t = build1 (IDENTITY_CONV, argtype, arg);
1550 ICS_ELLIPSIS_FLAG (t) = 1;
1553 TREE_VEC_ELT (convs, i) = t;
1557 if (ICS_BAD_FLAG (t))
1564 parmnode = TREE_CHAIN (parmnode);
1565 argnode = TREE_CHAIN (argnode);
1571 if (!sufficient_parms_p (parmnode))
1574 return add_candidate (candidates, totype, arglist, convs, access_path,
1575 conversion_path, viable);
1579 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
1580 tree type1, tree type2, tree *args, tree *argtypes,
1590 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1592 for (i = 0; i < 2; ++i)
1597 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1601 /* We need something for printing the candidate. */
1602 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1604 else if (ICS_BAD_FLAG (t))
1606 TREE_VEC_ELT (convs, i) = t;
1609 /* For COND_EXPR we rearranged the arguments; undo that now. */
1612 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1613 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1614 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1616 TREE_VEC_ELT (convs, 0) = t;
1621 add_candidate (candidates, fnname, /*args=*/NULL_TREE, convs,
1622 /*access_path=*/NULL_TREE,
1623 /*conversion_path=*/NULL_TREE,
1628 is_complete (tree t)
1630 return COMPLETE_TYPE_P (complete_type (t));
1633 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1636 promoted_arithmetic_type_p (tree type)
1640 In this section, the term promoted integral type is used to refer
1641 to those integral types which are preserved by integral promotion
1642 (including e.g. int and long but excluding e.g. char).
1643 Similarly, the term promoted arithmetic type refers to promoted
1644 integral types plus floating types. */
1645 return ((INTEGRAL_TYPE_P (type)
1646 && same_type_p (type_promotes_to (type), type))
1647 || TREE_CODE (type) == REAL_TYPE);
1650 /* Create any builtin operator overload candidates for the operator in
1651 question given the converted operand types TYPE1 and TYPE2. The other
1652 args are passed through from add_builtin_candidates to
1653 build_builtin_candidate.
1655 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1656 If CODE is requires candidates operands of the same type of the kind
1657 of which TYPE1 and TYPE2 are, we add both candidates
1658 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1661 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
1662 enum tree_code code2, tree fnname, tree type1,
1663 tree type2, tree *args, tree *argtypes, int flags)
1667 case POSTINCREMENT_EXPR:
1668 case POSTDECREMENT_EXPR:
1669 args[1] = integer_zero_node;
1670 type2 = integer_type_node;
1679 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1680 and VQ is either volatile or empty, there exist candidate operator
1681 functions of the form
1682 VQ T& operator++(VQ T&);
1683 T operator++(VQ T&, int);
1684 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1685 type other than bool, and VQ is either volatile or empty, there exist
1686 candidate operator functions of the form
1687 VQ T& operator--(VQ T&);
1688 T operator--(VQ T&, int);
1689 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1690 complete object type, and VQ is either volatile or empty, there exist
1691 candidate operator functions of the form
1692 T*VQ& operator++(T*VQ&);
1693 T*VQ& operator--(T*VQ&);
1694 T* operator++(T*VQ&, int);
1695 T* operator--(T*VQ&, int); */
1697 case POSTDECREMENT_EXPR:
1698 case PREDECREMENT_EXPR:
1699 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1701 case POSTINCREMENT_EXPR:
1702 case PREINCREMENT_EXPR:
1703 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1705 type1 = build_reference_type (type1);
1710 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1711 exist candidate operator functions of the form
1715 8 For every function type T, there exist candidate operator functions of
1717 T& operator*(T*); */
1720 if (TREE_CODE (type1) == POINTER_TYPE
1721 && (TYPE_PTROB_P (type1)
1722 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1726 /* 9 For every type T, there exist candidate operator functions of the form
1729 10For every promoted arithmetic type T, there exist candidate operator
1730 functions of the form
1734 case CONVERT_EXPR: /* unary + */
1735 if (TREE_CODE (type1) == POINTER_TYPE
1736 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1739 if (ARITHMETIC_TYPE_P (type1))
1743 /* 11For every promoted integral type T, there exist candidate operator
1744 functions of the form
1748 if (INTEGRAL_TYPE_P (type1))
1752 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1753 is the same type as C2 or is a derived class of C2, T is a complete
1754 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1755 there exist candidate operator functions of the form
1756 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1757 where CV12 is the union of CV1 and CV2. */
1760 if (TREE_CODE (type1) == POINTER_TYPE
1761 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1763 tree c1 = TREE_TYPE (type1);
1764 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1765 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1766 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1768 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1769 && (TYPE_PTRMEMFUNC_P (type2)
1770 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1775 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1776 didate operator functions of the form
1781 bool operator<(L, R);
1782 bool operator>(L, R);
1783 bool operator<=(L, R);
1784 bool operator>=(L, R);
1785 bool operator==(L, R);
1786 bool operator!=(L, R);
1787 where LR is the result of the usual arithmetic conversions between
1790 14For every pair of types T and I, where T is a cv-qualified or cv-
1791 unqualified complete object type and I is a promoted integral type,
1792 there exist candidate operator functions of the form
1793 T* operator+(T*, I);
1794 T& operator[](T*, I);
1795 T* operator-(T*, I);
1796 T* operator+(I, T*);
1797 T& operator[](I, T*);
1799 15For every T, where T is a pointer to complete object type, there exist
1800 candidate operator functions of the form112)
1801 ptrdiff_t operator-(T, T);
1803 16For every pointer or enumeration type T, there exist candidate operator
1804 functions of the form
1805 bool operator<(T, T);
1806 bool operator>(T, T);
1807 bool operator<=(T, T);
1808 bool operator>=(T, T);
1809 bool operator==(T, T);
1810 bool operator!=(T, T);
1812 17For every pointer to member type T, there exist candidate operator
1813 functions of the form
1814 bool operator==(T, T);
1815 bool operator!=(T, T); */
1818 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1820 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1822 type2 = ptrdiff_type_node;
1826 case TRUNC_DIV_EXPR:
1827 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1833 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1834 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1836 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1837 && null_ptr_cst_p (args[1]))
1842 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1843 && null_ptr_cst_p (args[0]))
1855 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1857 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1859 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1861 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1866 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1874 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1877 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1879 type1 = ptrdiff_type_node;
1882 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1884 type2 = ptrdiff_type_node;
1889 /* 18For every pair of promoted integral types L and R, there exist candi-
1890 date operator functions of the form
1897 where LR is the result of the usual arithmetic conversions between
1900 case TRUNC_MOD_EXPR:
1906 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1910 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1911 type, VQ is either volatile or empty, and R is a promoted arithmetic
1912 type, there exist candidate operator functions of the form
1913 VQ L& operator=(VQ L&, R);
1914 VQ L& operator*=(VQ L&, R);
1915 VQ L& operator/=(VQ L&, R);
1916 VQ L& operator+=(VQ L&, R);
1917 VQ L& operator-=(VQ L&, R);
1919 20For every pair T, VQ), where T is any type and VQ is either volatile
1920 or empty, there exist candidate operator functions of the form
1921 T*VQ& operator=(T*VQ&, T*);
1923 21For every pair T, VQ), where T is a pointer to member type and VQ is
1924 either volatile or empty, there exist candidate operator functions of
1926 VQ T& operator=(VQ T&, T);
1928 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1929 unqualified complete object type, VQ is either volatile or empty, and
1930 I is a promoted integral type, there exist candidate operator func-
1932 T*VQ& operator+=(T*VQ&, I);
1933 T*VQ& operator-=(T*VQ&, I);
1935 23For every triple L, VQ, R), where L is an integral or enumeration
1936 type, VQ is either volatile or empty, and R is a promoted integral
1937 type, there exist candidate operator functions of the form
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);
1943 VQ L& operator^=(VQ L&, R);
1944 VQ L& operator|=(VQ L&, R); */
1951 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1953 type2 = ptrdiff_type_node;
1957 case TRUNC_DIV_EXPR:
1958 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1962 case TRUNC_MOD_EXPR:
1968 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1973 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1975 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1976 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1977 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1978 || ((TYPE_PTRMEMFUNC_P (type1)
1979 || TREE_CODE (type1) == POINTER_TYPE)
1980 && null_ptr_cst_p (args[1])))
1990 type1 = build_reference_type (type1);
1996 For every pair of promoted arithmetic types L and R, there
1997 exist candidate operator functions of the form
1999 LR operator?(bool, L, R);
2001 where LR is the result of the usual arithmetic conversions
2002 between types L and R.
2004 For every type T, where T is a pointer or pointer-to-member
2005 type, there exist candidate operator functions of the form T
2006 operator?(bool, T, T); */
2008 if (promoted_arithmetic_type_p (type1)
2009 && promoted_arithmetic_type_p (type2))
2013 /* Otherwise, the types should be pointers. */
2014 if (!(TREE_CODE (type1) == POINTER_TYPE
2015 || TYPE_PTRMEM_P (type1)
2016 || TYPE_PTRMEMFUNC_P (type1))
2017 || !(TREE_CODE (type2) == POINTER_TYPE
2018 || TYPE_PTRMEM_P (type2)
2019 || TYPE_PTRMEMFUNC_P (type2)))
2022 /* We don't check that the two types are the same; the logic
2023 below will actually create two candidates; one in which both
2024 parameter types are TYPE1, and one in which both parameter
2032 /* If we're dealing with two pointer types or two enumeral types,
2033 we need candidates for both of them. */
2034 if (type2 && !same_type_p (type1, type2)
2035 && TREE_CODE (type1) == TREE_CODE (type2)
2036 && (TREE_CODE (type1) == REFERENCE_TYPE
2037 || (TREE_CODE (type1) == POINTER_TYPE
2038 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
2039 || TYPE_PTRMEMFUNC_P (type1)
2040 || IS_AGGR_TYPE (type1)
2041 || TREE_CODE (type1) == ENUMERAL_TYPE))
2043 build_builtin_candidate
2044 (candidates, fnname, type1, type1, args, argtypes, flags);
2045 build_builtin_candidate
2046 (candidates, fnname, type2, type2, args, argtypes, flags);
2050 build_builtin_candidate
2051 (candidates, fnname, type1, type2, args, argtypes, flags);
2055 type_decays_to (tree type)
2057 if (TREE_CODE (type) == ARRAY_TYPE)
2058 return build_pointer_type (TREE_TYPE (type));
2059 if (TREE_CODE (type) == FUNCTION_TYPE)
2060 return build_pointer_type (type);
2064 /* There are three conditions of builtin candidates:
2066 1) bool-taking candidates. These are the same regardless of the input.
2067 2) pointer-pair taking candidates. These are generated for each type
2068 one of the input types converts to.
2069 3) arithmetic candidates. According to the standard, we should generate
2070 all of these, but I'm trying not to...
2072 Here we generate a superset of the possible candidates for this particular
2073 case. That is a subset of the full set the standard defines, plus some
2074 other cases which the standard disallows. add_builtin_candidate will
2075 filter out the invalid set. */
2078 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2079 enum tree_code code2, tree fnname, tree *args,
2084 tree type, argtypes[3];
2085 /* TYPES[i] is the set of possible builtin-operator parameter types
2086 we will consider for the Ith argument. These are represented as
2087 a TREE_LIST; the TREE_VALUE of each node is the potential
2091 for (i = 0; i < 3; ++i)
2094 argtypes[i] = lvalue_type (args[i]);
2096 argtypes[i] = NULL_TREE;
2101 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2102 and VQ is either volatile or empty, there exist candidate operator
2103 functions of the form
2104 VQ T& operator++(VQ T&); */
2106 case POSTINCREMENT_EXPR:
2107 case PREINCREMENT_EXPR:
2108 case POSTDECREMENT_EXPR:
2109 case PREDECREMENT_EXPR:
2114 /* 24There also exist candidate operator functions of the form
2115 bool operator!(bool);
2116 bool operator&&(bool, bool);
2117 bool operator||(bool, bool); */
2119 case TRUTH_NOT_EXPR:
2120 build_builtin_candidate
2121 (candidates, fnname, boolean_type_node,
2122 NULL_TREE, args, argtypes, flags);
2125 case TRUTH_ORIF_EXPR:
2126 case TRUTH_ANDIF_EXPR:
2127 build_builtin_candidate
2128 (candidates, fnname, boolean_type_node,
2129 boolean_type_node, args, argtypes, flags);
2151 types[0] = types[1] = NULL_TREE;
2153 for (i = 0; i < 2; ++i)
2157 else if (IS_AGGR_TYPE (argtypes[i]))
2161 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2164 convs = lookup_conversions (argtypes[i]);
2166 if (code == COND_EXPR)
2168 if (real_lvalue_p (args[i]))
2169 types[i] = tree_cons
2170 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2172 types[i] = tree_cons
2173 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2179 for (; convs; convs = TREE_CHAIN (convs))
2181 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2184 && (TREE_CODE (type) != REFERENCE_TYPE
2185 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2188 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2189 types[i] = tree_cons (NULL_TREE, type, types[i]);
2191 type = non_reference (type);
2192 if (i != 0 || ! ref1)
2194 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2195 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2196 types[i] = tree_cons (NULL_TREE, type, types[i]);
2197 if (INTEGRAL_TYPE_P (type))
2198 type = type_promotes_to (type);
2201 if (! value_member (type, types[i]))
2202 types[i] = tree_cons (NULL_TREE, type, types[i]);
2207 if (code == COND_EXPR && real_lvalue_p (args[i]))
2208 types[i] = tree_cons
2209 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2210 type = non_reference (argtypes[i]);
2211 if (i != 0 || ! ref1)
2213 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2214 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2215 types[i] = tree_cons (NULL_TREE, type, types[i]);
2216 if (INTEGRAL_TYPE_P (type))
2217 type = type_promotes_to (type);
2219 types[i] = tree_cons (NULL_TREE, type, types[i]);
2223 /* Run through the possible parameter types of both arguments,
2224 creating candidates with those parameter types. */
2225 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2228 for (type = types[1]; type; type = TREE_CHAIN (type))
2229 add_builtin_candidate
2230 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2231 TREE_VALUE (type), args, argtypes, flags);
2233 add_builtin_candidate
2234 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2235 NULL_TREE, args, argtypes, flags);
2242 /* If TMPL can be successfully instantiated as indicated by
2243 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2245 TMPL is the template. EXPLICIT_TARGS are any explicit template
2246 arguments. ARGLIST is the arguments provided at the call-site.
2247 The RETURN_TYPE is the desired type for conversion operators. If
2248 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2249 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2250 add_conv_candidate. */
2252 static struct z_candidate*
2253 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2254 tree ctype, tree explicit_targs, tree arglist,
2255 tree return_type, tree access_path,
2256 tree conversion_path, int flags, tree obj,
2257 unification_kind_t strict)
2259 int ntparms = DECL_NTPARMS (tmpl);
2260 tree targs = make_tree_vec (ntparms);
2261 tree args_without_in_chrg = arglist;
2262 struct z_candidate *cand;
2266 /* We don't do deduction on the in-charge parameter, the VTT
2267 parameter or 'this'. */
2268 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2269 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2271 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2272 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2273 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2274 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2276 i = fn_type_unification (tmpl, explicit_targs, targs,
2277 args_without_in_chrg,
2278 return_type, strict, -1);
2283 fn = instantiate_template (tmpl, targs);
2284 if (fn == error_mark_node)
2289 A member function template is never instantiated to perform the
2290 copy of a class object to an object of its class type.
2292 It's a little unclear what this means; the standard explicitly
2293 does allow a template to be used to copy a class. For example,
2298 template <class T> A(const T&);
2301 void g () { A a (f ()); }
2303 the member template will be used to make the copy. The section
2304 quoted above appears in the paragraph that forbids constructors
2305 whose only parameter is (a possibly cv-qualified variant of) the
2306 class type, and a logical interpretation is that the intent was
2307 to forbid the instantiation of member templates which would then
2309 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2311 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2312 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2317 if (obj != NULL_TREE)
2318 /* Aha, this is a conversion function. */
2319 cand = add_conv_candidate (candidates, fn, obj, access_path,
2320 conversion_path, arglist);
2322 cand = add_function_candidate (candidates, fn, ctype,
2323 arglist, access_path,
2324 conversion_path, flags);
2325 if (DECL_TI_TEMPLATE (fn) != tmpl)
2326 /* This situation can occur if a member template of a template
2327 class is specialized. Then, instantiate_template might return
2328 an instantiation of the specialization, in which case the
2329 DECL_TI_TEMPLATE field will point at the original
2330 specialization. For example:
2332 template <class T> struct S { template <class U> void f(U);
2333 template <> void f(int) {}; };
2337 Here, TMPL will be template <class U> S<double>::f(U).
2338 And, instantiate template will give us the specialization
2339 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2340 for this will point at template <class T> template <> S<T>::f(int),
2341 so that we can find the definition. For the purposes of
2342 overload resolution, however, we want the original TMPL. */
2343 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2345 cand->template = DECL_TEMPLATE_INFO (fn);
2351 static struct z_candidate *
2352 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2353 tree explicit_targs, tree arglist, tree return_type,
2354 tree access_path, tree conversion_path, int flags,
2355 unification_kind_t strict)
2358 add_template_candidate_real (candidates, tmpl, ctype,
2359 explicit_targs, arglist, return_type,
2360 access_path, conversion_path,
2361 flags, NULL_TREE, strict);
2365 static struct z_candidate *
2366 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2367 tree obj, tree arglist, tree return_type,
2368 tree access_path, tree conversion_path)
2371 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2372 arglist, return_type, access_path,
2373 conversion_path, 0, obj, DEDUCE_CONV);
2378 any_viable (struct z_candidate *cands)
2380 for (; cands; cands = cands->next)
2381 if (pedantic ? cands->viable == 1 : cands->viable)
2387 any_strictly_viable (struct z_candidate *cands)
2389 for (; cands; cands = cands->next)
2390 if (cands->viable == 1)
2395 static struct z_candidate *
2396 splice_viable (struct z_candidate *cands)
2398 struct z_candidate **p = &cands;
2402 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2412 build_this (tree obj)
2414 /* Fix this to work on non-lvalues. */
2415 return build_unary_op (ADDR_EXPR, obj, 0);
2419 print_z_candidates (struct z_candidate *candidates)
2421 const char *str = "candidates are:";
2422 for (; candidates; candidates = candidates->next)
2424 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2426 if (TREE_VEC_LENGTH (candidates->convs) == 3)
2427 error ("%s %D(%T, %T, %T) <built-in>", str, candidates->fn,
2428 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2429 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2430 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2431 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2432 error ("%s %D(%T, %T) <built-in>", str, candidates->fn,
2433 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2434 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2436 error ("%s %D(%T) <built-in>", str, candidates->fn,
2437 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2439 else if (TYPE_P (candidates->fn))
2440 error ("%s %T <conversion>", str, candidates->fn);
2442 cp_error_at ("%s %+#D%s", str, candidates->fn,
2443 candidates->viable == -1 ? " <near match>" : "");
2448 /* USER_SEQ is a user-defined conversion sequence, beginning with a
2449 USER_CONV. STD_SEQ is the standard conversion sequence applied to
2450 the result of the conversion function to convert it to the final
2451 desired type. Merge the the two sequences into a single sequence,
2452 and return the merged sequence. */
2455 merge_conversion_sequences (tree user_seq, tree std_seq)
2459 my_friendly_assert (TREE_CODE (user_seq) == USER_CONV,
2462 /* Find the end of the second conversion sequence. */
2464 while (TREE_CODE (*t) != IDENTITY_CONV)
2465 t = &TREE_OPERAND (*t, 0);
2467 /* Replace the identity conversion with the user conversion
2471 /* The entire sequence is a user-conversion sequence. */
2472 ICS_USER_FLAG (std_seq) = 1;
2477 /* Returns the best overload candidate to perform the requested
2478 conversion. This function is used for three the overloading situations
2479 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2480 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2481 per [dcl.init.ref], so we ignore temporary bindings. */
2483 static struct z_candidate *
2484 build_user_type_conversion_1 (tree totype, tree expr, int flags)
2486 struct z_candidate *candidates, *cand;
2487 tree fromtype = TREE_TYPE (expr);
2488 tree ctors = NULL_TREE, convs = NULL_TREE;
2489 tree args = NULL_TREE;
2491 /* We represent conversion within a hierarchy using RVALUE_CONV and
2492 BASE_CONV, as specified by [over.best.ics]; these become plain
2493 constructor calls, as specified in [dcl.init]. */
2494 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2495 || !DERIVED_FROM_P (totype, fromtype), 20011226);
2497 if (IS_AGGR_TYPE (totype))
2498 ctors = lookup_fnfields (TYPE_BINFO (totype),
2499 complete_ctor_identifier,
2502 if (IS_AGGR_TYPE (fromtype))
2503 convs = lookup_conversions (fromtype);
2506 flags |= LOOKUP_NO_CONVERSION;
2512 ctors = BASELINK_FUNCTIONS (ctors);
2514 t = build_int_2 (0, 0);
2515 TREE_TYPE (t) = build_pointer_type (totype);
2516 args = build_tree_list (NULL_TREE, expr);
2517 /* We should never try to call the abstract or base constructor
2519 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2520 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2522 args = tree_cons (NULL_TREE, t, args);
2524 for (; ctors; ctors = OVL_NEXT (ctors))
2526 tree ctor = OVL_CURRENT (ctors);
2527 if (DECL_NONCONVERTING_P (ctor))
2530 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2531 cand = add_template_candidate (&candidates, ctor, totype,
2532 NULL_TREE, args, NULL_TREE,
2533 TYPE_BINFO (totype),
2534 TYPE_BINFO (totype),
2538 cand = add_function_candidate (&candidates, ctor, totype,
2539 args, TYPE_BINFO (totype),
2540 TYPE_BINFO (totype),
2544 cand->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2548 args = build_tree_list (NULL_TREE, build_this (expr));
2550 for (; convs; convs = TREE_CHAIN (convs))
2553 tree conversion_path = TREE_PURPOSE (convs);
2554 int convflags = LOOKUP_NO_CONVERSION;
2556 /* If we are called to convert to a reference type, we are trying to
2557 find an lvalue binding, so don't even consider temporaries. If
2558 we don't find an lvalue binding, the caller will try again to
2559 look for a temporary binding. */
2560 if (TREE_CODE (totype) == REFERENCE_TYPE)
2561 convflags |= LOOKUP_NO_TEMP_BIND;
2563 for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
2565 tree fn = OVL_CURRENT (fns);
2567 /* [over.match.funcs] For conversion functions, the function
2568 is considered to be a member of the class of the implicit
2569 object argument for the purpose of defining the type of
2570 the implicit object parameter.
2572 So we pass fromtype as CTYPE to add_*_candidate. */
2574 if (TREE_CODE (fn) == TEMPLATE_DECL)
2575 cand = add_template_candidate (&candidates, fn, fromtype,
2578 TYPE_BINFO (fromtype),
2583 cand = add_function_candidate (&candidates, fn, fromtype,
2585 TYPE_BINFO (fromtype),
2591 tree ics = implicit_conversion (totype,
2592 TREE_TYPE (TREE_TYPE (cand->fn)),
2595 cand->second_conv = ics;
2597 if (ics == NULL_TREE)
2599 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
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 if (!any_strictly_viable (candidates))
2624 ICS_BAD_FLAG (cand->second_conv) = 1;
2625 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2626 ambiguous conversion is no worse than another user-defined
2632 /* Build the user conversion sequence. */
2635 (DECL_CONSTRUCTOR_P (cand->fn)
2636 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2637 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
2638 TREE_OPERAND (convs, 1) = build_zc_wrapper (cand);
2640 /* Combine it with the second conversion sequence. */
2641 cand->second_conv = merge_conversion_sequences (convs,
2644 if (cand->viable == -1)
2645 ICS_BAD_FLAG (cand->second_conv) = 1;
2651 build_user_type_conversion (tree totype, tree expr, int flags)
2653 struct z_candidate *cand
2654 = build_user_type_conversion_1 (totype, expr, flags);
2658 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2659 return error_mark_node;
2660 return convert_from_reference (convert_like (cand->second_conv, expr));
2665 /* Find the possibly overloaded set of functions corresponding to a
2666 call of the form SCOPE::NAME (...). NAME might be a
2667 TEMPLATE_ID_EXPR, OVERLOAD, _DECL, IDENTIFIER_NODE or LOOKUP_EXPR. */
2670 resolve_scoped_fn_name (tree scope, tree name)
2673 tree template_args = NULL_TREE;
2674 bool is_template_id = TREE_CODE (name) == TEMPLATE_ID_EXPR;
2678 template_args = TREE_OPERAND (name, 1);
2679 name = TREE_OPERAND (name, 0);
2681 if (TREE_CODE (name) == OVERLOAD)
2682 name = DECL_NAME (get_first_fn (name));
2683 else if (TREE_CODE (name) == LOOKUP_EXPR)
2684 name = TREE_OPERAND (name, 0);
2686 if (TREE_CODE (scope) == NAMESPACE_DECL)
2687 fn = lookup_namespace_name (scope, name);
2690 if (!TYPE_BEING_DEFINED (scope)
2691 && !COMPLETE_TYPE_P (complete_type (scope)))
2693 error ("incomplete type '%T' cannot be used to name a scope",
2695 return error_mark_node;
2698 if (BASELINK_P (name))
2701 fn = lookup_member (scope, name, /*protect=*/1, /*want_type=*/false);
2702 if (fn && current_class_type)
2703 fn = (adjust_result_of_qualified_name_lookup
2704 (fn, scope, current_class_type));
2706 /* It might be the name of a function pointer member. */
2707 if (fn && TREE_CODE (fn) == FIELD_DECL)
2708 fn = resolve_offset_ref (build_offset_ref (scope, fn));
2713 error ("'%D' has no member named '%E'", scope, name);
2714 return error_mark_node;
2720 if (BASELINK_P (fn))
2721 fns = BASELINK_FUNCTIONS (fns);
2722 fns = build_nt (TEMPLATE_ID_EXPR, fns, template_args);
2723 if (BASELINK_P (fn))
2724 BASELINK_FUNCTIONS (fn) = fns;
2732 /* Do any initial processing on the arguments to a function call. */
2735 resolve_args (tree args)
2738 for (t = args; t; t = TREE_CHAIN (t))
2740 tree arg = TREE_VALUE (t);
2742 if (arg == error_mark_node)
2743 return error_mark_node;
2744 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2746 error ("invalid use of void expression");
2747 return error_mark_node;
2749 else if (TREE_CODE (arg) == OFFSET_REF)
2750 arg = resolve_offset_ref (arg);
2751 arg = convert_from_reference (arg);
2752 TREE_VALUE (t) = arg;
2757 /* Perform overload resolution on FN, which is called with the ARGS.
2759 Return the candidate function selected by overload resolution, or
2760 NULL if the event that overload resolution failed. In the case
2761 that overload resolution fails, *CANDIDATES will be the set of
2762 candidates considered, and ANY_VIABLE_P will be set to true or
2763 false to indicate whether or not any of the candidates were
2766 The ARGS should already have gone through RESOLVE_ARGS before this
2767 function is called. */
2769 static struct z_candidate *
2770 perform_overload_resolution (tree fn,
2772 struct z_candidate **candidates,
2775 struct z_candidate *cand;
2776 tree explicit_targs = NULL_TREE;
2777 int template_only = 0;
2780 *any_viable_p = true;
2782 /* Check FN and ARGS. */
2783 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2784 || TREE_CODE (fn) == TEMPLATE_DECL
2785 || TREE_CODE (fn) == OVERLOAD
2786 || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2788 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2791 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2793 explicit_targs = TREE_OPERAND (fn, 1);
2794 fn = TREE_OPERAND (fn, 0);
2798 /* Add the various candidate functions. */
2799 add_candidates (fn, args, explicit_targs, template_only,
2800 /*conversion_path=*/NULL_TREE,
2801 /*access_path=*/NULL_TREE,
2805 if (! any_viable (*candidates))
2807 *any_viable_p = false;
2811 *candidates = splice_viable (*candidates);
2812 cand = tourney (*candidates);
2817 /* Return an expression for a call to FN (a namespace-scope function,
2818 or a static member function) with the ARGS. */
2821 build_new_function_call (tree fn, tree args)
2823 struct z_candidate *candidates, *cand;
2826 args = resolve_args (args);
2827 if (args == error_mark_node)
2828 return error_mark_node;
2830 cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p);
2834 if (!any_viable_p && candidates && ! candidates->next)
2835 return build_function_call (candidates->fn, args);
2836 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2837 fn = TREE_OPERAND (fn, 0);
2839 error ("no matching function for call to `%D(%A)'",
2840 DECL_NAME (OVL_CURRENT (fn)), args);
2842 error ("call of overloaded `%D(%A)' is ambiguous",
2843 DECL_NAME (OVL_FUNCTION (fn)), args);
2845 print_z_candidates (candidates);
2846 return error_mark_node;
2849 return build_over_call (cand, LOOKUP_NORMAL);
2852 /* Build a call to a global operator new. FNNAME is the name of the
2853 operator (either "operator new" or "operator new[]") and ARGS are
2854 the arguments provided. *SIZE points to the total number of bytes
2855 required by the allocation, and is updated if that is changed here.
2856 *COOKIE_SIZE is non-NULL if a cookie should be used. If this
2857 function determins that no cookie should be used, after all,
2858 *COOKIE_SIZE is set to NULL_TREE. */
2861 build_operator_new_call (tree fnname, tree args, tree *size, tree *cookie_size)
2864 struct z_candidate *candidates;
2865 struct z_candidate *cand;
2868 args = tree_cons (NULL_TREE, *size, args);
2869 args = resolve_args (args);
2870 if (args == error_mark_node)
2873 fns = lookup_function_nonclass (fnname, args);
2875 /* Figure out what function is being called. */
2876 cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p);
2878 /* If no suitable function could be found, issue an error message
2883 error ("no matching function for call to `%D(%A)'",
2884 DECL_NAME (OVL_CURRENT (fns)), args);
2886 error ("call of overlopaded `%D(%A)' is ambiguous",
2887 DECL_NAME (OVL_FUNCTION (fns)), args);
2889 print_z_candidates (candidates);
2890 return error_mark_node;
2893 /* If a cookie is required, add some extra space. Whether
2894 or not a cookie is required cannot be determined until
2895 after we know which function was called. */
2898 bool use_cookie = true;
2899 if (!abi_version_at_least (2))
2901 tree placement = TREE_CHAIN (args);
2902 /* In G++ 3.2, the check was implemented incorrectly; it
2903 looked at the placement expression, rather than the
2904 type of the function. */
2905 if (placement && !TREE_CHAIN (placement)
2906 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2914 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
2915 /* Skip the size_t parameter. */
2916 arg_types = TREE_CHAIN (arg_types);
2917 /* Check the remaining parameters (if any). */
2919 && TREE_CHAIN (arg_types) == void_list_node
2920 && same_type_p (TREE_VALUE (arg_types),
2924 /* If we need a cookie, adjust the number of bytes allocated. */
2927 /* Update the total size. */
2928 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
2929 /* Update the argument list to reflect the adjusted size. */
2930 TREE_VALUE (args) = *size;
2933 *cookie_size = NULL_TREE;
2936 /* Build the CALL_EXPR. */
2937 return build_over_call (cand, LOOKUP_NORMAL);
2941 build_object_call (tree obj, tree args)
2943 struct z_candidate *candidates = 0, *cand;
2944 tree fns, convs, mem_args = NULL_TREE;
2945 tree type = TREE_TYPE (obj);
2947 if (TYPE_PTRMEMFUNC_P (type))
2949 /* It's no good looking for an overloaded operator() on a
2950 pointer-to-member-function. */
2951 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2952 return error_mark_node;
2955 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2956 if (fns == error_mark_node)
2957 return error_mark_node;
2959 args = resolve_args (args);
2961 if (args == error_mark_node)
2962 return error_mark_node;
2966 tree base = BINFO_TYPE (BASELINK_BINFO (fns));
2967 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2969 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2971 tree fn = OVL_CURRENT (fns);
2972 if (TREE_CODE (fn) == TEMPLATE_DECL)
2973 add_template_candidate (&candidates, fn, base, NULL_TREE,
2974 mem_args, NULL_TREE,
2977 LOOKUP_NORMAL, DEDUCE_CALL);
2979 add_function_candidate
2980 (&candidates, fn, base, mem_args, TYPE_BINFO (type),
2981 TYPE_BINFO (type), LOOKUP_NORMAL);
2985 convs = lookup_conversions (type);
2987 for (; convs; convs = TREE_CHAIN (convs))
2989 tree fns = TREE_VALUE (convs);
2990 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2992 if ((TREE_CODE (totype) == POINTER_TYPE
2993 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2994 || (TREE_CODE (totype) == REFERENCE_TYPE
2995 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2996 || (TREE_CODE (totype) == REFERENCE_TYPE
2997 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2998 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2999 for (; fns; fns = OVL_NEXT (fns))
3001 tree fn = OVL_CURRENT (fns);
3002 if (TREE_CODE (fn) == TEMPLATE_DECL)
3003 add_template_conv_candidate
3004 (&candidates, fn, obj, args, totype,
3005 /*access_path=*/NULL_TREE,
3006 /*conversion_path=*/NULL_TREE);
3008 add_conv_candidate (&candidates, fn, obj, args,
3009 /*conversion_path=*/NULL_TREE,
3010 /*access_path=*/NULL_TREE);
3014 if (! any_viable (candidates))
3016 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
3017 print_z_candidates (candidates);
3018 return error_mark_node;
3021 candidates = splice_viable (candidates);
3022 cand = tourney (candidates);
3026 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
3027 print_z_candidates (candidates);
3028 return error_mark_node;
3031 /* Since cand->fn will be a type, not a function, for a conversion
3032 function, we must be careful not to unconditionally look at
3034 if (TREE_CODE (cand->fn) == FUNCTION_DECL
3035 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
3036 return build_over_call (cand, LOOKUP_NORMAL);
3038 obj = convert_like_with_context
3039 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
3042 return build_function_call (obj, args);
3046 op_error (enum tree_code code, enum tree_code code2,
3047 tree arg1, tree arg2, tree arg3, const char *problem)
3051 if (code == MODIFY_EXPR)
3052 opname = assignment_operator_name_info[code2].name;
3054 opname = operator_name_info[code].name;
3059 error ("%s for `%T ? %T : %T' operator", problem,
3060 error_type (arg1), error_type (arg2), error_type (arg3));
3062 case POSTINCREMENT_EXPR:
3063 case POSTDECREMENT_EXPR:
3064 error ("%s for `%T %s' operator", problem, error_type (arg1), opname);
3067 error ("%s for `%T [%T]' operator", problem,
3068 error_type (arg1), error_type (arg2));
3072 error ("%s for `%T %s %T' operator", problem,
3073 error_type (arg1), opname, error_type (arg2));
3075 error ("%s for `%s %T' operator", problem, opname, error_type (arg1));
3079 /* Return the implicit conversion sequence that could be used to
3080 convert E1 to E2 in [expr.cond]. */
3083 conditional_conversion (tree e1, tree e2)
3085 tree t1 = non_reference (TREE_TYPE (e1));
3086 tree t2 = non_reference (TREE_TYPE (e2));
3091 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
3092 implicitly converted (clause _conv_) to the type "reference to
3093 T2", subject to the constraint that in the conversion the
3094 reference must bind directly (_dcl.init.ref_) to E1. */
3095 if (real_lvalue_p (e2))
3097 conv = implicit_conversion (build_reference_type (t2),
3100 LOOKUP_NO_TEMP_BIND);
3107 If E1 and E2 have class type, and the underlying class types are
3108 the same or one is a base class of the other: E1 can be converted
3109 to match E2 if the class of T2 is the same type as, or a base
3110 class of, the class of T1, and the cv-qualification of T2 is the
3111 same cv-qualification as, or a greater cv-qualification than, the
3112 cv-qualification of T1. If the conversion is applied, E1 is
3113 changed to an rvalue of type T2 that still refers to the original
3114 source class object (or the appropriate subobject thereof). */
3115 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
3116 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
3117 TYPE_MAIN_VARIANT (t1)))
3119 if (at_least_as_qualified_p (t2, t1))
3121 conv = build1 (IDENTITY_CONV, t1, e1);
3122 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
3123 TYPE_MAIN_VARIANT (t2)))
3124 conv = build_conv (BASE_CONV, t2, conv);
3133 E1 can be converted to match E2 if E1 can be implicitly converted
3134 to the type that expression E2 would have if E2 were converted to
3135 an rvalue (or the type it has, if E2 is an rvalue). */
3136 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
3139 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
3140 arguments to the conditional expression. */
3143 build_conditional_expr (tree arg1, tree arg2, tree arg3)
3148 tree result_type = NULL_TREE;
3149 bool lvalue_p = true;
3150 struct z_candidate *candidates = 0;
3151 struct z_candidate *cand;
3153 /* As a G++ extension, the second argument to the conditional can be
3154 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
3155 c'.) If the second operand is omitted, make sure it is
3156 calculated only once. */
3160 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
3162 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
3163 if (real_lvalue_p (arg1))
3164 arg2 = arg1 = stabilize_reference (arg1);
3166 arg2 = arg1 = save_expr (arg1);
3171 The first expr ession is implicitly converted to bool (clause
3173 arg1 = cp_convert (boolean_type_node, arg1);
3175 /* If something has already gone wrong, just pass that fact up the
3177 if (arg1 == error_mark_node
3178 || arg2 == error_mark_node
3179 || arg3 == error_mark_node
3180 || TREE_TYPE (arg1) == error_mark_node
3181 || TREE_TYPE (arg2) == error_mark_node
3182 || TREE_TYPE (arg3) == error_mark_node)
3183 return error_mark_node;
3187 If either the second or the third operand has type (possibly
3188 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3189 array-to-pointer (_conv.array_), and function-to-pointer
3190 (_conv.func_) standard conversions are performed on the second
3191 and third operands. */
3192 arg2_type = TREE_TYPE (arg2);
3193 arg3_type = TREE_TYPE (arg3);
3194 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3196 /* Do the conversions. We don't these for `void' type arguments
3197 since it can't have any effect and since decay_conversion
3198 does not handle that case gracefully. */
3199 if (!VOID_TYPE_P (arg2_type))
3200 arg2 = decay_conversion (arg2);
3201 if (!VOID_TYPE_P (arg3_type))
3202 arg3 = decay_conversion (arg3);
3203 arg2_type = TREE_TYPE (arg2);
3204 arg3_type = TREE_TYPE (arg3);
3208 One of the following shall hold:
3210 --The second or the third operand (but not both) is a
3211 throw-expression (_except.throw_); the result is of the
3212 type of the other and is an rvalue.
3214 --Both the second and the third operands have type void; the
3215 result is of type void and is an rvalue. */
3216 if ((TREE_CODE (arg2) == THROW_EXPR)
3217 ^ (TREE_CODE (arg3) == THROW_EXPR))
3218 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
3219 ? arg3_type : arg2_type);
3220 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3221 result_type = void_type_node;
3224 error ("`%E' has type `void' and is not a throw-expression",
3225 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3226 return error_mark_node;
3230 goto valid_operands;
3234 Otherwise, if the second and third operand have different types,
3235 and either has (possibly cv-qualified) class type, an attempt is
3236 made to convert each of those operands to the type of the other. */
3237 else if (!same_type_p (arg2_type, arg3_type)
3238 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3240 tree conv2 = conditional_conversion (arg2, arg3);
3241 tree conv3 = conditional_conversion (arg3, arg2);
3245 If both can be converted, or one can be converted but the
3246 conversion is ambiguous, the program is ill-formed. If
3247 neither can be converted, the operands are left unchanged and
3248 further checking is performed as described below. If exactly
3249 one conversion is possible, that conversion is applied to the
3250 chosen operand and the converted operand is used in place of
3251 the original operand for the remainder of this section. */
3252 if ((conv2 && !ICS_BAD_FLAG (conv2)
3253 && conv3 && !ICS_BAD_FLAG (conv3))
3254 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3255 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3257 error ("operands to ?: have different types");
3258 return error_mark_node;
3260 else if (conv2 && !ICS_BAD_FLAG (conv2))
3262 arg2 = convert_like (conv2, arg2);
3263 arg2 = convert_from_reference (arg2);
3264 /* That may not quite have done the trick. If the two types
3265 are cv-qualified variants of one another, we will have
3266 just used an IDENTITY_CONV. */
3267 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
3268 arg2 = convert (arg3_type, arg2);
3269 arg2_type = TREE_TYPE (arg2);
3271 else if (conv3 && !ICS_BAD_FLAG (conv3))
3273 arg3 = convert_like (conv3, arg3);
3274 arg3 = convert_from_reference (arg3);
3275 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
3276 arg3 = convert (arg2_type, arg3);
3277 arg3_type = TREE_TYPE (arg3);
3283 If the second and third operands are lvalues and have the same
3284 type, the result is of that type and is an lvalue. */
3285 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
3286 same_type_p (arg2_type, arg3_type))
3288 result_type = arg2_type;
3289 goto valid_operands;
3294 Otherwise, the result is an rvalue. If the second and third
3295 operand do not have the same type, and either has (possibly
3296 cv-qualified) class type, overload resolution is used to
3297 determine the conversions (if any) to be applied to the operands
3298 (_over.match.oper_, _over.built_). */
3300 if (!same_type_p (arg2_type, arg3_type)
3301 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3306 /* Rearrange the arguments so that add_builtin_candidate only has
3307 to know about two args. In build_builtin_candidates, the
3308 arguments are unscrambled. */
3312 add_builtin_candidates (&candidates,
3315 ansi_opname (COND_EXPR),
3321 If the overload resolution fails, the program is
3323 if (!any_viable (candidates))
3325 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3326 print_z_candidates (candidates);
3327 return error_mark_node;
3329 candidates = splice_viable (candidates);
3330 cand = tourney (candidates);
3333 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3334 print_z_candidates (candidates);
3335 return error_mark_node;
3340 Otherwise, the conversions thus determined are applied, and
3341 the converted operands are used in place of the original
3342 operands for the remainder of this section. */
3343 conv = TREE_VEC_ELT (cand->convs, 0);
3344 arg1 = convert_like (conv, arg1);
3345 conv = TREE_VEC_ELT (cand->convs, 1);
3346 arg2 = convert_like (conv, arg2);
3347 conv = TREE_VEC_ELT (cand->convs, 2);
3348 arg3 = convert_like (conv, arg3);
3353 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3354 and function-to-pointer (_conv.func_) standard conversions are
3355 performed on the second and third operands.
3357 We need to force the lvalue-to-rvalue conversion here for class types,
3358 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3359 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3362 We use ocp_convert rather than build_user_type_conversion because the
3363 latter returns NULL_TREE on failure, while the former gives an error. */
3365 if (IS_AGGR_TYPE (TREE_TYPE (arg2)))
3366 arg2 = ocp_convert (TREE_TYPE (arg2), arg2,
3367 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3369 arg2 = decay_conversion (arg2);
3370 arg2_type = TREE_TYPE (arg2);
3372 if (IS_AGGR_TYPE (TREE_TYPE (arg3)))
3373 arg3 = ocp_convert (TREE_TYPE (arg3), arg3,
3374 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3376 arg3 = decay_conversion (arg3);
3377 arg3_type = TREE_TYPE (arg3);
3379 if (arg2 == error_mark_node || arg3 == error_mark_node)
3380 return error_mark_node;
3384 After those conversions, one of the following shall hold:
3386 --The second and third operands have the same type; the result is of
3388 if (same_type_p (arg2_type, arg3_type))
3389 result_type = arg2_type;
3392 --The second and third operands have arithmetic or enumeration
3393 type; the usual arithmetic conversions are performed to bring
3394 them to a common type, and the result is of that type. */
3395 else if ((ARITHMETIC_TYPE_P (arg2_type)
3396 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3397 && (ARITHMETIC_TYPE_P (arg3_type)
3398 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3400 /* In this case, there is always a common type. */
3401 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3404 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3405 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3406 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3407 arg2_type, arg3_type);
3408 else if (extra_warnings
3409 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3410 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3411 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3412 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3413 warning ("enumeral and non-enumeral type in conditional expression");
3415 arg2 = perform_implicit_conversion (result_type, arg2);
3416 arg3 = perform_implicit_conversion (result_type, arg3);
3420 --The second and third operands have pointer type, or one has
3421 pointer type and the other is a null pointer constant; pointer
3422 conversions (_conv.ptr_) and qualification conversions
3423 (_conv.qual_) are performed to bring them to their composite
3424 pointer type (_expr.rel_). The result is of the composite
3427 --The second and third operands have pointer to member type, or
3428 one has pointer to member type and the other is a null pointer
3429 constant; pointer to member conversions (_conv.mem_) and
3430 qualification conversions (_conv.qual_) are performed to bring
3431 them to a common type, whose cv-qualification shall match the
3432 cv-qualification of either the second or the third operand.
3433 The result is of the common type. */
3434 else if ((null_ptr_cst_p (arg2)
3435 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3436 || TYPE_PTRMEMFUNC_P (arg3_type)))
3437 || (null_ptr_cst_p (arg3)
3438 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3439 || TYPE_PTRMEMFUNC_P (arg2_type)))
3440 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3441 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3442 || (TYPE_PTRMEMFUNC_P (arg2_type)
3443 && TYPE_PTRMEMFUNC_P (arg3_type)))
3445 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3446 arg3, "conditional expression");
3447 arg2 = perform_implicit_conversion (result_type, arg2);
3448 arg3 = perform_implicit_conversion (result_type, arg3);
3453 error ("operands to ?: have different types");
3454 return error_mark_node;
3458 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3459 /* Expand both sides into the same slot, hopefully the target of the
3460 ?: expression. We used to check for TARGET_EXPRs here, but now we
3461 sometimes wrap them in NOP_EXPRs so the test would fail. */
3462 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3463 result = build_target_expr_with_type (result, result_type);
3465 /* If this expression is an rvalue, but might be mistaken for an
3466 lvalue, we must add a NON_LVALUE_EXPR. */
3467 if (!lvalue_p && real_lvalue_p (result))
3468 result = build1 (NON_LVALUE_EXPR, result_type, result);
3473 /* OPERAND is an operand to an expression. Perform necessary steps
3474 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
3478 prep_operand (tree operand)
3482 if (TREE_CODE (operand) == OFFSET_REF)
3483 operand = resolve_offset_ref (operand);
3484 operand = convert_from_reference (operand);
3485 if (CLASS_TYPE_P (TREE_TYPE (operand))
3486 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
3487 /* Make sure the template type is instantiated now. */
3488 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
3494 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
3495 OVERLOAD) to the CANDIDATES, returning an updated list of
3496 CANDIDATES. The ARGS are the arguments provided to the call,
3497 without any implicit object parameter. The EXPLICIT_TARGS are
3498 explicit template arguments provided. TEMPLATE_ONLY is true if
3499 only template fucntions should be considered. CONVERSION_PATH,
3500 ACCESS_PATH, and FLAGS are as for add_function_candidate. */
3503 add_candidates (tree fns, tree args,
3504 tree explicit_targs, bool template_only,
3505 tree conversion_path, tree access_path,
3507 struct z_candidate **candidates)
3510 tree non_static_args;
3512 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
3513 /* Delay creating the implicit this parameter until it is needed. */
3514 non_static_args = NULL_TREE;