Fix cut and paste error in last change
[official-gcc.git] / gcc / cp / call.c
bloba155f1089676d68eaa553a4b151dd0fb4c0488bc
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) and
5 modified by Brendan Kehoe (brendan@cygnus.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 /* High-level class interface. */
27 #include "config.h"
28 #include "system.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "output.h"
32 #include "flags.h"
33 #include "rtl.h"
34 #include "toplev.h"
35 #include "defaults.h"
36 #include "expr.h"
37 #include "ggc.h"
39 extern int inhibit_warnings;
41 static tree build_new_method_call PARAMS ((tree, tree, tree, tree, int));
43 static tree build_field_call PARAMS ((tree, tree, tree, tree));
44 static struct z_candidate * tourney PARAMS ((struct z_candidate *));
45 static int equal_functions PARAMS ((tree, tree));
46 static int joust PARAMS ((struct z_candidate *, struct z_candidate *, int));
47 static int compare_ics PARAMS ((tree, tree));
48 static tree build_over_call PARAMS ((struct z_candidate *, tree, int));
49 #define convert_like(CONV, EXPR) convert_like_real (CONV, EXPR, NULL_TREE, 0, 0)
50 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) convert_like_real (CONV, EXPR, FN, ARGNO, 0)
51 static tree convert_like_real PARAMS ((tree, tree, tree, int, int));
52 static void op_error PARAMS ((enum tree_code, enum tree_code, tree, tree,
53 tree, const char *));
54 static tree build_object_call PARAMS ((tree, tree));
55 static tree resolve_args PARAMS ((tree));
56 static struct z_candidate * build_user_type_conversion_1
57 PARAMS ((tree, tree, int));
58 static void print_z_candidates PARAMS ((struct z_candidate *));
59 static tree build_this PARAMS ((tree));
60 static struct z_candidate * splice_viable PARAMS ((struct z_candidate *));
61 static int any_viable PARAMS ((struct z_candidate *));
62 static struct z_candidate * add_template_candidate
63 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
64 unification_kind_t));
65 static struct z_candidate * add_template_candidate_real
66 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
67 tree, unification_kind_t));
68 static struct z_candidate * add_template_conv_candidate
69 PARAMS ((struct z_candidate *, tree, tree, tree, tree));
70 static struct z_candidate * add_builtin_candidates
71 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
72 tree, tree *, int));
73 static struct z_candidate * add_builtin_candidate
74 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
75 tree, tree, tree, tree *, tree *, int));
76 static int is_complete PARAMS ((tree));
77 static struct z_candidate * build_builtin_candidate
78 PARAMS ((struct z_candidate *, tree, tree, tree, tree *, tree *,
79 int));
80 static struct z_candidate * add_conv_candidate
81 PARAMS ((struct z_candidate *, tree, tree, tree));
82 static struct z_candidate * add_function_candidate
83 PARAMS ((struct z_candidate *, tree, tree, tree, int));
84 static tree implicit_conversion PARAMS ((tree, tree, tree, int));
85 static tree standard_conversion PARAMS ((tree, tree, tree));
86 static tree reference_binding PARAMS ((tree, tree, tree, int));
87 static tree non_reference PARAMS ((tree));
88 static tree build_conv PARAMS ((enum tree_code, tree, tree));
89 static int is_subseq PARAMS ((tree, tree));
90 static int maybe_handle_ref_bind PARAMS ((tree*, tree*));
91 static void maybe_handle_implicit_object PARAMS ((tree*));
92 static struct z_candidate * add_candidate PARAMS ((struct z_candidate *,
93 tree, tree, int));
94 static tree source_type PARAMS ((tree));
95 static void add_warning PARAMS ((struct z_candidate *, struct z_candidate *));
96 static int reference_related_p PARAMS ((tree, tree));
97 static int reference_compatible_p PARAMS ((tree, tree));
98 static tree convert_class_to_reference PARAMS ((tree, tree, tree));
99 static tree direct_reference_binding PARAMS ((tree, tree));
100 static int promoted_arithmetic_type_p PARAMS ((tree));
101 static tree conditional_conversion PARAMS ((tree, tree));
103 tree
104 build_vfield_ref (datum, type)
105 tree datum, type;
107 tree rval;
109 if (datum == error_mark_node)
110 return error_mark_node;
112 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
113 datum = convert_from_reference (datum);
115 if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type))
116 rval = build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
117 datum, TYPE_VFIELD (type));
118 else
119 rval = build_component_ref (datum, DECL_NAME (TYPE_VFIELD (type)), NULL_TREE, 0);
121 return rval;
124 /* Build a call to a member of an object. I.e., one that overloads
125 operator ()(), or is a pointer-to-function or pointer-to-method. */
127 static tree
128 build_field_call (basetype_path, instance_ptr, name, parms)
129 tree basetype_path, instance_ptr, name, parms;
131 tree field, instance;
133 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
134 return NULL_TREE;
136 /* Speed up the common case. */
137 if (instance_ptr == current_class_ptr
138 && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
139 return NULL_TREE;
141 field = lookup_field (basetype_path, name, 1, 0);
143 if (field == error_mark_node || field == NULL_TREE)
144 return field;
146 if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
148 /* If it's a field, try overloading operator (),
149 or calling if the field is a pointer-to-function. */
150 instance = build_indirect_ref (instance_ptr, NULL_PTR);
151 instance = build_component_ref_1 (instance, field, 0);
153 if (instance == error_mark_node)
154 return error_mark_node;
156 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
157 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
158 instance, parms, NULL_TREE);
159 else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
161 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
162 return build_function_call (instance, parms);
163 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
164 == METHOD_TYPE)
165 return build_function_call
166 (instance, tree_cons (NULL_TREE, instance_ptr, parms));
170 return NULL_TREE;
173 /* Returns nonzero iff the destructor name specified in NAME
174 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
175 forms... */
178 check_dtor_name (basetype, name)
179 tree basetype, name;
181 name = TREE_OPERAND (name, 0);
183 /* Just accept something we've already complained about. */
184 if (name == error_mark_node)
185 return 1;
187 if (TREE_CODE (name) == TYPE_DECL)
188 name = TREE_TYPE (name);
189 else if (TYPE_P (name))
190 /* OK */;
191 else if (TREE_CODE (name) == IDENTIFIER_NODE)
193 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
194 || (TREE_CODE (basetype) == ENUMERAL_TYPE
195 && name == TYPE_IDENTIFIER (basetype)))
196 name = basetype;
197 else
198 name = get_type_value (name);
200 else
201 my_friendly_abort (980605);
203 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
204 return 1;
205 return 0;
208 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
209 This is how virtual function calls are avoided. */
211 tree
212 build_scoped_method_call (exp, basetype, name, parms)
213 tree exp, basetype, name, parms;
215 /* Because this syntactic form does not allow
216 a pointer to a base class to be `stolen',
217 we need not protect the derived->base conversion
218 that happens here.
220 @@ But we do have to check access privileges later. */
221 tree binfo, decl;
222 tree type = TREE_TYPE (exp);
224 if (type == error_mark_node
225 || basetype == error_mark_node)
226 return error_mark_node;
228 if (processing_template_decl)
230 if (TREE_CODE (name) == BIT_NOT_EXPR
231 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
233 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
234 if (type)
235 name = build_min_nt (BIT_NOT_EXPR, type);
237 name = build_min_nt (SCOPE_REF, basetype, name);
238 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
241 if (TREE_CODE (type) == REFERENCE_TYPE)
242 type = TREE_TYPE (type);
244 if (TREE_CODE (basetype) == TREE_VEC)
246 binfo = basetype;
247 basetype = BINFO_TYPE (binfo);
249 else
250 binfo = NULL_TREE;
252 /* Check the destructor call syntax. */
253 if (TREE_CODE (name) == BIT_NOT_EXPR)
255 /* We can get here if someone writes their destructor call like
256 `obj.NS::~T()'; this isn't really a scoped method call, so hand
257 it off. */
258 if (TREE_CODE (basetype) == NAMESPACE_DECL)
259 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
261 if (! check_dtor_name (basetype, name))
262 cp_error ("qualified type `%T' does not match destructor name `~%T'",
263 basetype, TREE_OPERAND (name, 0));
265 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
266 that explicit ~int is caught in the parser; this deals with typedefs
267 and template parms. */
268 if (! IS_AGGR_TYPE (basetype))
270 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
271 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
272 exp, basetype, type);
274 return cp_convert (void_type_node, exp);
278 if (! is_aggr_type (basetype, 1))
279 return error_mark_node;
281 if (! IS_AGGR_TYPE (type))
283 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
284 exp, type);
285 return error_mark_node;
288 if (! binfo)
290 binfo = get_binfo (basetype, type, 1);
291 if (binfo == error_mark_node)
292 return error_mark_node;
293 if (! binfo)
294 error_not_base_type (basetype, type);
297 if (binfo)
299 if (TREE_CODE (exp) == INDIRECT_REF)
300 decl = build_indirect_ref
301 (convert_pointer_to_real
302 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
303 else
304 decl = build_scoped_ref (exp, basetype);
306 /* Call to a destructor. */
307 if (TREE_CODE (name) == BIT_NOT_EXPR)
309 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
310 return cp_convert (void_type_node, exp);
312 return build_delete (TREE_TYPE (decl), decl,
313 sfk_complete_destructor,
314 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
318 /* Call to a method. */
319 return build_method_call (decl, name, parms, binfo,
320 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
322 return error_mark_node;
325 /* We want the address of a function or method. We avoid creating a
326 pointer-to-member function. */
328 tree
329 build_addr_func (function)
330 tree function;
332 tree type = TREE_TYPE (function);
334 /* We have to do these by hand to avoid real pointer to member
335 functions. */
336 if (TREE_CODE (type) == METHOD_TYPE)
338 tree addr;
340 type = build_pointer_type (type);
342 if (mark_addressable (function) == 0)
343 return error_mark_node;
345 addr = build1 (ADDR_EXPR, type, function);
347 /* Address of a static or external variable or function counts
348 as a constant */
349 if (staticp (function))
350 TREE_CONSTANT (addr) = 1;
352 function = addr;
354 else
355 function = default_conversion (function);
357 return function;
360 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
361 POINTER_TYPE to those. Note, pointer to member function types
362 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
364 tree
365 build_call (function, parms)
366 tree function, parms;
368 int is_constructor = 0;
369 int nothrow;
370 tree tmp;
371 tree decl;
372 tree result_type;
374 function = build_addr_func (function);
376 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
378 sorry ("unable to call pointer to member function here");
379 return error_mark_node;
382 result_type = TREE_TYPE (TREE_TYPE (TREE_TYPE (function)));
384 if (TREE_CODE (function) == ADDR_EXPR
385 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
386 decl = TREE_OPERAND (function, 0);
387 else
388 decl = NULL_TREE;
390 /* We check both the decl and the type; a function may be known not to
391 throw without being declared throw(). */
392 nothrow = ((decl && TREE_NOTHROW (decl))
393 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
395 if (decl && DECL_CONSTRUCTOR_P (decl))
396 is_constructor = 1;
398 if (decl && ! TREE_USED (decl))
400 /* We invoke build_call directly for several library functions. */
401 if (DECL_ARTIFICIAL (decl))
402 mark_used (decl);
403 else
404 my_friendly_abort (990125);
407 /* Don't pass empty class objects by value. This is useful
408 for tags in STL, which are used to control overload resolution.
409 We don't need to handle other cases of copying empty classes. */
410 if (! decl || ! DECL_BUILT_IN (decl))
411 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
412 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
413 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
415 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
416 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
417 TREE_VALUE (tmp), t);
420 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
421 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
422 TREE_TYPE (function) = result_type;
423 TREE_SIDE_EFFECTS (function) = 1;
424 TREE_NOTHROW (function) = nothrow;
426 return function;
429 /* Build something of the form ptr->method (args)
430 or object.method (args). This can also build
431 calls to constructors, and find friends.
433 Member functions always take their class variable
434 as a pointer.
436 INSTANCE is a class instance.
438 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
440 PARMS help to figure out what that NAME really refers to.
442 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
443 down to the real instance type to use for access checking. We need this
444 information to get protected accesses correct. This parameter is used
445 by build_member_call.
447 FLAGS is the logical disjunction of zero or more LOOKUP_
448 flags. See cp-tree.h for more info.
450 If this is all OK, calls build_function_call with the resolved
451 member function.
453 This function must also handle being called to perform
454 initialization, promotion/coercion of arguments, and
455 instantiation of default parameters.
457 Note that NAME may refer to an instance variable name. If
458 `operator()()' is defined for the type of that field, then we return
459 that result. */
461 #ifdef GATHER_STATISTICS
462 extern int n_build_method_call;
463 #endif
465 tree
466 build_method_call (instance, name, parms, basetype_path, flags)
467 tree instance, name, parms, basetype_path;
468 int flags;
470 tree basetype, instance_ptr;
472 #ifdef GATHER_STATISTICS
473 n_build_method_call++;
474 #endif
476 if (instance == error_mark_node
477 || name == error_mark_node
478 || parms == error_mark_node
479 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
480 return error_mark_node;
482 if (processing_template_decl)
484 /* We need to process template parm names here so that tsubst catches
485 them properly. Other type names can wait. */
486 if (TREE_CODE (name) == BIT_NOT_EXPR)
488 tree type = NULL_TREE;
490 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
491 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
492 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
493 type = TREE_TYPE (TREE_OPERAND (name, 0));
495 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
496 name = build_min_nt (BIT_NOT_EXPR, type);
499 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
502 if (TREE_CODE (name) == BIT_NOT_EXPR)
504 if (parms)
505 error ("destructors take no parameters");
506 basetype = TREE_TYPE (instance);
507 if (TREE_CODE (basetype) == REFERENCE_TYPE)
508 basetype = TREE_TYPE (basetype);
510 if (! check_dtor_name (basetype, name))
511 cp_error
512 ("destructor name `~%T' does not match type `%T' of expression",
513 TREE_OPERAND (name, 0), basetype);
515 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
516 return cp_convert (void_type_node, instance);
517 instance = default_conversion (instance);
518 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
519 return build_delete (build_pointer_type (basetype),
520 instance_ptr, sfk_complete_destructor,
521 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
524 return build_new_method_call (instance, name, parms, basetype_path, flags);
527 /* New overloading code. */
529 struct z_candidate {
530 tree fn;
531 tree convs;
532 tree second_conv;
533 int viable;
534 tree basetype_path;
535 tree template;
536 tree warnings;
537 struct z_candidate *next;
540 #define IDENTITY_RANK 0
541 #define EXACT_RANK 1
542 #define PROMO_RANK 2
543 #define STD_RANK 3
544 #define PBOOL_RANK 4
545 #define USER_RANK 5
546 #define ELLIPSIS_RANK 6
547 #define BAD_RANK 7
549 #define ICS_RANK(NODE) \
550 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
551 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
552 : ICS_USER_FLAG (NODE) ? USER_RANK \
553 : ICS_STD_RANK (NODE))
555 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
557 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
558 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
559 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
560 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
562 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
563 should be created to hold the result of the conversion. */
564 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
566 #define USER_CONV_CAND(NODE) \
567 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
568 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
571 null_ptr_cst_p (t)
572 tree t;
574 /* [conv.ptr]
576 A null pointer constant is an integral constant expression
577 (_expr.const_) rvalue of integer type that evaluates to zero. */
578 if (t == null_node
579 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
580 return 1;
581 return 0;
584 static tree
585 build_conv (code, type, from)
586 enum tree_code code;
587 tree type, from;
589 tree t = build1 (code, type, from);
590 int rank = ICS_STD_RANK (from);
591 switch (code)
593 case PTR_CONV:
594 case PMEM_CONV:
595 case BASE_CONV:
596 case STD_CONV:
597 if (rank < STD_RANK)
598 rank = STD_RANK;
599 break;
601 case QUAL_CONV:
602 if (rank < EXACT_RANK)
603 rank = EXACT_RANK;
605 default:
606 break;
608 ICS_STD_RANK (t) = rank;
609 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
610 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
611 return t;
614 static tree
615 non_reference (t)
616 tree t;
618 if (TREE_CODE (t) == REFERENCE_TYPE)
619 t = TREE_TYPE (t);
620 return t;
623 tree
624 strip_top_quals (t)
625 tree t;
627 if (TREE_CODE (t) == ARRAY_TYPE)
628 return t;
629 return TYPE_MAIN_VARIANT (t);
632 /* Returns the standard conversion path (see [conv]) from type FROM to type
633 TO, if any. For proper handling of null pointer constants, you must
634 also pass the expression EXPR to convert from. */
636 static tree
637 standard_conversion (to, from, expr)
638 tree to, from, expr;
640 enum tree_code fcode, tcode;
641 tree conv;
642 int fromref = 0;
644 if (TREE_CODE (to) == REFERENCE_TYPE)
645 to = TREE_TYPE (to);
646 if (TREE_CODE (from) == REFERENCE_TYPE)
648 fromref = 1;
649 from = TREE_TYPE (from);
651 to = strip_top_quals (to);
652 from = strip_top_quals (from);
654 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
655 && expr && type_unknown_p (expr))
657 expr = instantiate_type (to, expr, 0);
658 if (expr == error_mark_node)
659 return NULL_TREE;
660 from = TREE_TYPE (expr);
663 fcode = TREE_CODE (from);
664 tcode = TREE_CODE (to);
666 conv = build1 (IDENTITY_CONV, from, expr);
668 if (fcode == FUNCTION_TYPE)
670 from = build_pointer_type (from);
671 fcode = TREE_CODE (from);
672 conv = build_conv (LVALUE_CONV, from, conv);
674 else if (fcode == ARRAY_TYPE)
676 from = build_pointer_type (TREE_TYPE (from));
677 fcode = TREE_CODE (from);
678 conv = build_conv (LVALUE_CONV, from, conv);
680 else if (fromref || (expr && real_lvalue_p (expr)))
681 conv = build_conv (RVALUE_CONV, from, conv);
683 /* Allow conversion between `__complex__' data types */
684 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
686 /* The standard conversion sequence to convert FROM to TO is
687 the standard conversion sequence to perform componentwise
688 conversion. */
689 tree part_conv = standard_conversion
690 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
692 if (part_conv)
694 conv = build_conv (TREE_CODE (part_conv), to, conv);
695 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
697 else
698 conv = NULL_TREE;
700 return conv;
703 if (same_type_p (from, to))
704 return conv;
706 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
707 && expr && null_ptr_cst_p (expr))
709 conv = build_conv (STD_CONV, to, conv);
711 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
713 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
714 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
716 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
717 TREE_TYPE (to)))
719 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
720 && ufcode != FUNCTION_TYPE)
722 from = build_pointer_type
723 (cp_build_qualified_type (void_type_node,
724 CP_TYPE_QUALS (TREE_TYPE (from))));
725 conv = build_conv (PTR_CONV, from, conv);
727 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
729 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
730 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
732 if (DERIVED_FROM_P (fbase, tbase)
733 && (same_type_ignoring_top_level_qualifiers_p
734 (TREE_TYPE (TREE_TYPE (from)),
735 TREE_TYPE (TREE_TYPE (to)))))
737 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
738 from = build_pointer_type (from);
739 conv = build_conv (PMEM_CONV, from, conv);
742 else if (IS_AGGR_TYPE (TREE_TYPE (from))
743 && IS_AGGR_TYPE (TREE_TYPE (to)))
745 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
747 from =
748 cp_build_qualified_type (TREE_TYPE (to),
749 CP_TYPE_QUALS (TREE_TYPE (from)));
750 from = build_pointer_type (from);
751 conv = build_conv (PTR_CONV, from, conv);
755 if (same_type_p (from, to))
756 /* OK */;
757 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
758 conv = build_conv (QUAL_CONV, to, conv);
759 else if (expr && string_conv_p (to, expr, 0))
760 /* converting from string constant to char *. */
761 conv = build_conv (QUAL_CONV, to, conv);
762 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
764 conv = build_conv (PTR_CONV, to, conv);
765 ICS_BAD_FLAG (conv) = 1;
767 else
768 return 0;
770 from = to;
772 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
774 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
775 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
776 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
777 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
779 if (! DERIVED_FROM_P (fbase, tbase)
780 || ! same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
781 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
782 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
783 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
784 return 0;
786 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
787 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
788 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
789 from = build_ptrmemfunc_type (build_pointer_type (from));
790 conv = build_conv (PMEM_CONV, from, conv);
792 else if (tcode == BOOLEAN_TYPE)
794 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
795 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
796 return 0;
798 conv = build_conv (STD_CONV, to, conv);
799 if (fcode == POINTER_TYPE
800 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
801 ICS_STD_RANK (conv) = PBOOL_RANK;
803 /* We don't check for ENUMERAL_TYPE here because there are no standard
804 conversions to enum type. */
805 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
806 || tcode == REAL_TYPE)
808 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
809 return 0;
810 conv = build_conv (STD_CONV, to, conv);
812 /* Give this a better rank if it's a promotion. */
813 if (to == type_promotes_to (from)
814 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
815 ICS_STD_RANK (conv) = PROMO_RANK;
817 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
818 && is_properly_derived_from (from, to))
820 if (TREE_CODE (conv) == RVALUE_CONV)
821 conv = TREE_OPERAND (conv, 0);
822 conv = build_conv (BASE_CONV, to, conv);
823 /* The derived-to-base conversion indicates the initialization
824 of a parameter with base type from an object of a derived
825 type. A temporary object is created to hold the result of
826 the conversion. */
827 NEED_TEMPORARY_P (conv) = 1;
829 else
830 return 0;
832 return conv;
835 /* Returns non-zero if T1 is reference-related to T2. */
837 static int
838 reference_related_p (t1, t2)
839 tree t1;
840 tree t2;
842 t1 = TYPE_MAIN_VARIANT (t1);
843 t2 = TYPE_MAIN_VARIANT (t2);
845 /* [dcl.init.ref]
847 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
848 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
849 of T2. */
850 return (same_type_p (t1, t2)
851 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
852 && DERIVED_FROM_P (t1, t2)));
855 /* Returns non-zero if T1 is reference-compatible with T2. */
857 static int
858 reference_compatible_p (t1, t2)
859 tree t1;
860 tree t2;
862 /* [dcl.init.ref]
864 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
865 reference-related to T2 and cv1 is the same cv-qualification as,
866 or greater cv-qualification than, cv2. */
867 return (reference_related_p (t1, t2)
868 && at_least_as_qualified_p (t1, t2));
871 /* Determine whether or not the EXPR (of class type S) can be
872 converted to T as in [over.match.ref]. */
874 static tree
875 convert_class_to_reference (t, s, expr)
876 tree t;
877 tree s;
878 tree expr;
880 tree conversions;
881 tree arglist;
882 tree conv;
883 struct z_candidate *candidates;
884 struct z_candidate *cand;
886 /* [over.match.ref]
888 Assuming that "cv1 T" is the underlying type of the reference
889 being initialized, and "cv S" is the type of the initializer
890 expression, with S a class type, the candidate functions are
891 selected as follows:
893 --The conversion functions of S and its base classes are
894 considered. Those that are not hidden within S and yield type
895 "reference to cv2 T2", where "cv1 T" is reference-compatible
896 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
898 The argument list has one argument, which is the initializer
899 expression. */
901 candidates = 0;
903 /* Conceptually, we should take the address of EXPR and put it in
904 the argument list. Unfortunately, however, that can result in
905 error messages, which we should not issue now because we are just
906 trying to find a conversion operator. Therefore, we use NULL,
907 cast to the appropriate type. */
908 arglist = build_int_2 (0, 0);
909 TREE_TYPE (arglist) = build_pointer_type (s);
910 arglist = build_tree_list (NULL_TREE, arglist);
912 for (conversions = lookup_conversions (s);
913 conversions;
914 conversions = TREE_CHAIN (conversions))
916 tree fns = TREE_VALUE (conversions);
918 for (; fns; fns = OVL_NEXT (fns))
920 tree f = OVL_CURRENT (fns);
921 tree t2 = TREE_TYPE (TREE_TYPE (f));
922 struct z_candidate *old_candidates = candidates;
924 /* If this is a template function, try to get an exact
925 match. */
926 if (TREE_CODE (f) == TEMPLATE_DECL)
928 candidates
929 = add_template_candidate (candidates,
930 f, s,
931 NULL_TREE,
932 arglist,
933 build_reference_type (t),
934 LOOKUP_NORMAL,
935 DEDUCE_CONV);
937 if (candidates != old_candidates)
939 /* Now, see if the conversion function really returns
940 an lvalue of the appropriate type. From the
941 point of view of unification, simply returning an
942 rvalue of the right type is good enough. */
943 f = candidates->fn;
944 t2 = TREE_TYPE (TREE_TYPE (f));
945 if (TREE_CODE (t2) != REFERENCE_TYPE
946 || !reference_compatible_p (t, TREE_TYPE (t2)))
947 candidates = candidates->next;
950 else if (TREE_CODE (t2) == REFERENCE_TYPE
951 && reference_compatible_p (t, TREE_TYPE (t2)))
952 candidates
953 = add_function_candidate (candidates, f, s, arglist,
954 LOOKUP_NORMAL);
956 if (candidates != old_candidates)
957 candidates->basetype_path = TYPE_BINFO (s);
961 /* If none of the conversion functions worked out, let our caller
962 know. */
963 if (!any_viable (candidates))
964 return NULL_TREE;
966 candidates = splice_viable (candidates);
967 cand = tourney (candidates);
968 if (!cand)
969 return NULL_TREE;
971 conv = build1 (IDENTITY_CONV, s, expr);
972 conv = build_conv (USER_CONV,
973 non_reference (TREE_TYPE (TREE_TYPE (cand->fn))),
974 conv);
975 TREE_OPERAND (conv, 1) = build_expr_ptr_wrapper (cand);
976 ICS_USER_FLAG (conv) = 1;
977 if (cand->viable == -1)
978 ICS_BAD_FLAG (conv) = 1;
979 cand->second_conv = conv;
981 return conv;
984 /* A reference of the indicated TYPE is being bound directly to the
985 expression represented by the implicit conversion sequence CONV.
986 Return a conversion sequence for this binding. */
988 static tree
989 direct_reference_binding (type, conv)
990 tree type;
991 tree conv;
993 tree t = TREE_TYPE (type);
995 /* [over.ics.rank]
997 When a parameter of reference type binds directly
998 (_dcl.init.ref_) to an argument expression, the implicit
999 conversion sequence is the identity conversion, unless the
1000 argument expression has a type that is a derived class of the
1001 parameter type, in which case the implicit conversion sequence is
1002 a derived-to-base Conversion.
1004 If the parameter binds directly to the result of applying a
1005 conversion function to the argument expression, the implicit
1006 conversion sequence is a user-defined conversion sequence
1007 (_over.ics.user_), with the second standard conversion sequence
1008 either an identity conversion or, if the conversion function
1009 returns an entity of a type that is a derived class of the
1010 parameter type, a derived-to-base conversion. */
1011 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1013 /* Represent the derived-to-base conversion. */
1014 conv = build_conv (BASE_CONV, t, conv);
1015 /* We will actually be binding to the base-class subobject in
1016 the derived class, so we mark this conversion appropriately.
1017 That way, convert_like knows not to generate a temporary. */
1018 NEED_TEMPORARY_P (conv) = 0;
1020 return build_conv (REF_BIND, type, conv);
1023 /* Returns the conversion path from type FROM to reference type TO for
1024 purposes of reference binding. For lvalue binding, either pass a
1025 reference type to FROM or an lvalue expression to EXPR. If the
1026 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1027 the conversion returned. */
1029 static tree
1030 reference_binding (rto, rfrom, expr, flags)
1031 tree rto, rfrom, expr;
1032 int flags;
1034 tree conv = NULL_TREE;
1035 tree to = TREE_TYPE (rto);
1036 tree from = rfrom;
1037 int related_p;
1038 int compatible_p;
1039 cp_lvalue_kind lvalue_p = clk_none;
1041 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1043 expr = instantiate_type (to, expr, 0);
1044 if (expr == error_mark_node)
1045 return NULL_TREE;
1046 from = TREE_TYPE (expr);
1049 if (TREE_CODE (from) == REFERENCE_TYPE)
1051 /* Anything with reference type is an lvalue. */
1052 lvalue_p = clk_ordinary;
1053 from = TREE_TYPE (from);
1055 else if (expr)
1056 lvalue_p = real_lvalue_p (expr);
1058 /* Figure out whether or not the types are reference-related and
1059 reference compatible. We have do do this after stripping
1060 references from FROM. */
1061 related_p = reference_related_p (to, from);
1062 compatible_p = reference_compatible_p (to, from);
1064 if (lvalue_p && compatible_p)
1066 /* [dcl.init.ref]
1068 If the intializer expression
1070 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1071 is reference-compatible with "cv2 T2,"
1073 the reference is bound directly to the initializer exprssion
1074 lvalue. */
1075 conv = build1 (IDENTITY_CONV, from, expr);
1076 conv = direct_reference_binding (rto, conv);
1077 if ((lvalue_p & clk_bitfield) != 0
1078 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1079 /* For the purposes of overload resolution, we ignore the fact
1080 this expression is a bitfield. (In particular,
1081 [over.ics.ref] says specifically that a function with a
1082 non-const reference parameter is viable even if the
1083 argument is a bitfield.)
1085 However, when we actually call the function we must create
1086 a temporary to which to bind the reference. If the
1087 reference is volatile, or isn't const, then we cannot make
1088 a temporary, so we just issue an error when the conversion
1089 actually occurs. */
1090 NEED_TEMPORARY_P (conv) = 1;
1091 return conv;
1093 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1095 /* [dcl.init.ref]
1097 If the initializer exprsesion
1099 -- has a class type (i.e., T2 is a class type) can be
1100 implicitly converted to an lvalue of type "cv3 T3," where
1101 "cv1 T1" is reference-compatible with "cv3 T3". (this
1102 conversion is selected by enumerating the applicable
1103 conversion functions (_over.match.ref_) and choosing the
1104 best one through overload resolution. (_over.match_).
1106 the reference is bound to the lvalue result of the conversion
1107 in the second case. */
1108 conv = convert_class_to_reference (to, from, expr);
1109 if (conv)
1110 return direct_reference_binding (rto, conv);
1113 /* From this point on, we conceptually need temporaries, even if we
1114 elide them. Only the cases above are "direct bindings". */
1115 if (flags & LOOKUP_NO_TEMP_BIND)
1116 return NULL_TREE;
1118 /* [over.ics.rank]
1120 When a parameter of reference type is not bound directly to an
1121 argument expression, the conversion sequence is the one required
1122 to convert the argument expression to the underlying type of the
1123 reference according to _over.best.ics_. Conceptually, this
1124 conversion sequence corresponds to copy-initializing a temporary
1125 of the underlying type with the argument expression. Any
1126 difference in top-level cv-qualification is subsumed by the
1127 initialization itself and does not constitute a conversion. */
1129 /* [dcl.init.ref]
1131 Otherwise, the reference shall be to a non-volatile const type. */
1132 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1133 return NULL_TREE;
1135 /* [dcl.init.ref]
1137 If the initializer expression is an rvalue, with T2 a class type,
1138 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1139 is bound in one of the following ways:
1141 -- The reference is bound to the object represented by the rvalue
1142 or to a sub-object within that object.
1144 In this case, the implicit conversion sequence is supposed to be
1145 same as we would obtain by generating a temporary. Fortunately,
1146 if the types are reference compatible, then this is either an
1147 identity conversion or the derived-to-base conversion, just as
1148 for direct binding. */
1149 if (CLASS_TYPE_P (from) && compatible_p)
1151 conv = build1 (IDENTITY_CONV, from, expr);
1152 return direct_reference_binding (rto, conv);
1155 /* [dcl.init.ref]
1157 Otherwise, a temporary of type "cv1 T1" is created and
1158 initialized from the initializer expression using the rules for a
1159 non-reference copy initialization. If T1 is reference-related to
1160 T2, cv1 must be the same cv-qualification as, or greater
1161 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1162 if (related_p && !at_least_as_qualified_p (to, from))
1163 return NULL_TREE;
1165 conv = implicit_conversion (to, from, expr, flags);
1166 if (!conv)
1167 return NULL_TREE;
1169 conv = build_conv (REF_BIND, rto, conv);
1170 /* This reference binding, unlike those above, requires the
1171 creation of a temporary. */
1172 NEED_TEMPORARY_P (conv) = 1;
1174 return conv;
1177 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1178 to type TO. The optional expression EXPR may affect the conversion.
1179 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1180 significant. */
1182 static tree
1183 implicit_conversion (to, from, expr, flags)
1184 tree to, from, expr;
1185 int flags;
1187 tree conv;
1188 struct z_candidate *cand;
1190 /* Resolve expressions like `A::p' that we thought might become
1191 pointers-to-members. */
1192 if (expr && TREE_CODE (expr) == OFFSET_REF)
1194 expr = resolve_offset_ref (expr);
1195 from = TREE_TYPE (expr);
1198 if (from == error_mark_node || to == error_mark_node
1199 || expr == error_mark_node)
1200 return NULL_TREE;
1202 /* Make sure both the FROM and TO types are complete so that
1203 user-defined conversions are available. */
1204 complete_type (from);
1205 complete_type (to);
1207 if (TREE_CODE (to) == REFERENCE_TYPE)
1208 conv = reference_binding (to, from, expr, flags);
1209 else
1210 conv = standard_conversion (to, from, expr);
1212 if (conv)
1214 else if (expr != NULL_TREE
1215 && (IS_AGGR_TYPE (non_reference (from))
1216 || IS_AGGR_TYPE (non_reference (to)))
1217 && (flags & LOOKUP_NO_CONVERSION) == 0)
1219 cand = build_user_type_conversion_1
1220 (to, expr, LOOKUP_ONLYCONVERTING);
1221 if (cand)
1222 conv = cand->second_conv;
1223 if ((! conv || ICS_BAD_FLAG (conv))
1224 && TREE_CODE (to) == REFERENCE_TYPE
1225 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1227 cand = build_user_type_conversion_1
1228 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1229 if (cand)
1231 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to)))
1232 ICS_BAD_FLAG (cand->second_conv) = 1;
1233 if (!conv || (ICS_BAD_FLAG (conv)
1234 > ICS_BAD_FLAG (cand->second_conv)))
1235 conv = build_conv (REF_BIND, to, cand->second_conv);
1240 return conv;
1243 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1244 functions. */
1246 static struct z_candidate *
1247 add_candidate (candidates, fn, convs, viable)
1248 struct z_candidate *candidates;
1249 tree fn, convs;
1250 int viable;
1252 struct z_candidate *cand
1253 = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1255 cand->fn = fn;
1256 cand->convs = convs;
1257 cand->viable = viable;
1258 cand->next = candidates;
1260 return cand;
1263 /* Create an overload candidate for the function or method FN called with
1264 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1265 to implicit_conversion.
1267 CTYPE, if non-NULL, is the type we want to pretend this function
1268 comes from for purposes of overload resolution. */
1270 static struct z_candidate *
1271 add_function_candidate (candidates, fn, ctype, arglist, flags)
1272 struct z_candidate *candidates;
1273 tree fn, ctype, arglist;
1274 int flags;
1276 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1277 int i, len;
1278 tree convs;
1279 tree parmnode, argnode;
1280 int viable = 1;
1282 /* The `this' and `in_chrg' arguments to constructors are not considered
1283 in overload resolution. */
1284 if (DECL_CONSTRUCTOR_P (fn))
1286 parmlist = TREE_CHAIN (parmlist);
1287 arglist = TREE_CHAIN (arglist);
1288 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1290 parmlist = TREE_CHAIN (parmlist);
1291 arglist = TREE_CHAIN (arglist);
1295 len = list_length (arglist);
1296 convs = make_tree_vec (len);
1298 /* 13.3.2 - Viable functions [over.match.viable]
1299 First, to be a viable function, a candidate function shall have enough
1300 parameters to agree in number with the arguments in the list.
1302 We need to check this first; otherwise, checking the ICSes might cause
1303 us to produce an ill-formed template instantiation. */
1305 parmnode = parmlist;
1306 for (i = 0; i < len; ++i)
1308 if (parmnode == NULL_TREE || parmnode == void_list_node)
1309 break;
1310 parmnode = TREE_CHAIN (parmnode);
1313 if (i < len && parmnode)
1314 viable = 0;
1316 /* Make sure there are default args for the rest of the parms. */
1317 else for (; parmnode && parmnode != void_list_node;
1318 parmnode = TREE_CHAIN (parmnode))
1319 if (! TREE_PURPOSE (parmnode))
1321 viable = 0;
1322 break;
1325 if (! viable)
1326 goto out;
1328 /* Second, for F to be a viable function, there shall exist for each
1329 argument an implicit conversion sequence that converts that argument
1330 to the corresponding parameter of F. */
1332 parmnode = parmlist;
1333 argnode = arglist;
1335 for (i = 0; i < len; ++i)
1337 tree arg = TREE_VALUE (argnode);
1338 tree argtype = lvalue_type (arg);
1339 tree t;
1340 int is_this;
1342 if (parmnode == void_list_node)
1343 break;
1345 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1346 && ! DECL_CONSTRUCTOR_P (fn));
1348 if (parmnode)
1350 tree parmtype = TREE_VALUE (parmnode);
1352 /* The type of the implicit object parameter ('this') for
1353 overload resolution is not always the same as for the
1354 function itself; conversion functions are considered to
1355 be members of the class being converted, and functions
1356 introduced by a using-declaration are considered to be
1357 members of the class that uses them.
1359 Since build_over_call ignores the ICS for the `this'
1360 parameter, we can just change the parm type. */
1361 if (ctype && is_this)
1363 parmtype
1364 = build_qualified_type (ctype,
1365 TYPE_QUALS (TREE_TYPE (parmtype)));
1366 parmtype = build_pointer_type (parmtype);
1369 t = implicit_conversion (parmtype, argtype, arg, flags);
1371 else
1373 t = build1 (IDENTITY_CONV, argtype, arg);
1374 ICS_ELLIPSIS_FLAG (t) = 1;
1377 if (t && is_this)
1378 ICS_THIS_FLAG (t) = 1;
1380 TREE_VEC_ELT (convs, i) = t;
1381 if (! t)
1383 viable = 0;
1384 break;
1387 if (ICS_BAD_FLAG (t))
1388 viable = -1;
1390 if (parmnode)
1391 parmnode = TREE_CHAIN (parmnode);
1392 argnode = TREE_CHAIN (argnode);
1395 out:
1396 return add_candidate (candidates, fn, convs, viable);
1399 /* Create an overload candidate for the conversion function FN which will
1400 be invoked for expression OBJ, producing a pointer-to-function which
1401 will in turn be called with the argument list ARGLIST, and add it to
1402 CANDIDATES. FLAGS is passed on to implicit_conversion.
1404 Actually, we don't really care about FN; we care about the type it
1405 converts to. There may be multiple conversion functions that will
1406 convert to that type, and we rely on build_user_type_conversion_1 to
1407 choose the best one; so when we create our candidate, we record the type
1408 instead of the function. */
1410 static struct z_candidate *
1411 add_conv_candidate (candidates, fn, obj, arglist)
1412 struct z_candidate *candidates;
1413 tree fn, obj, arglist;
1415 tree totype = TREE_TYPE (TREE_TYPE (fn));
1416 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1417 int i, len = list_length (arglist) + 1;
1418 tree convs = make_tree_vec (len);
1419 tree parmnode = parmlist;
1420 tree argnode = arglist;
1421 int viable = 1;
1422 int flags = LOOKUP_NORMAL;
1424 /* Don't bother looking up the same type twice. */
1425 if (candidates && candidates->fn == totype)
1426 return candidates;
1428 for (i = 0; i < len; ++i)
1430 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1431 tree argtype = lvalue_type (arg);
1432 tree t;
1434 if (i == 0)
1435 t = implicit_conversion (totype, argtype, arg, flags);
1436 else if (parmnode == void_list_node)
1437 break;
1438 else if (parmnode)
1439 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1440 else
1442 t = build1 (IDENTITY_CONV, argtype, arg);
1443 ICS_ELLIPSIS_FLAG (t) = 1;
1446 TREE_VEC_ELT (convs, i) = t;
1447 if (! t)
1448 break;
1450 if (ICS_BAD_FLAG (t))
1451 viable = -1;
1453 if (i == 0)
1454 continue;
1456 if (parmnode)
1457 parmnode = TREE_CHAIN (parmnode);
1458 argnode = TREE_CHAIN (argnode);
1461 if (i < len)
1462 viable = 0;
1464 for (; parmnode && parmnode != void_list_node;
1465 parmnode = TREE_CHAIN (parmnode))
1466 if (! TREE_PURPOSE (parmnode))
1468 viable = 0;
1469 break;
1472 return add_candidate (candidates, totype, convs, viable);
1475 static struct z_candidate *
1476 build_builtin_candidate (candidates, fnname, type1, type2,
1477 args, argtypes, flags)
1478 struct z_candidate *candidates;
1479 tree fnname, type1, type2, *args, *argtypes;
1480 int flags;
1483 tree t, convs;
1484 int viable = 1, i;
1485 tree types[2];
1487 types[0] = type1;
1488 types[1] = type2;
1490 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1492 for (i = 0; i < 2; ++i)
1494 if (! args[i])
1495 break;
1497 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1498 if (! t)
1500 viable = 0;
1501 /* We need something for printing the candidate. */
1502 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1504 else if (ICS_BAD_FLAG (t))
1505 viable = 0;
1506 TREE_VEC_ELT (convs, i) = t;
1509 /* For COND_EXPR we rearranged the arguments; undo that now. */
1510 if (args[2])
1512 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1513 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1514 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1515 if (t)
1516 TREE_VEC_ELT (convs, 0) = t;
1517 else
1518 viable = 0;
1521 return add_candidate (candidates, fnname, convs, viable);
1524 static int
1525 is_complete (t)
1526 tree t;
1528 return COMPLETE_TYPE_P (complete_type (t));
1531 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1533 static int
1534 promoted_arithmetic_type_p (type)
1535 tree type;
1537 /* [over.built]
1539 In this section, the term promoted integral type is used to refer
1540 to those integral types which are preserved by integral promotion
1541 (including e.g. int and long but excluding e.g. char).
1542 Similarly, the term promoted arithmetic type refers to promoted
1543 integral types plus floating types. */
1544 return ((INTEGRAL_TYPE_P (type)
1545 && same_type_p (type_promotes_to (type), type))
1546 || TREE_CODE (type) == REAL_TYPE);
1549 /* Create any builtin operator overload candidates for the operator in
1550 question given the converted operand types TYPE1 and TYPE2. The other
1551 args are passed through from add_builtin_candidates to
1552 build_builtin_candidate. */
1554 static struct z_candidate *
1555 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1556 args, argtypes, flags)
1557 struct z_candidate *candidates;
1558 enum tree_code code, code2;
1559 tree fnname, type1, type2, *args, *argtypes;
1560 int flags;
1562 switch (code)
1564 case POSTINCREMENT_EXPR:
1565 case POSTDECREMENT_EXPR:
1566 args[1] = integer_zero_node;
1567 type2 = integer_type_node;
1568 break;
1569 default:
1570 break;
1573 switch (code)
1576 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1577 and VQ is either volatile or empty, there exist candidate operator
1578 functions of the form
1579 VQ T& operator++(VQ T&);
1580 T operator++(VQ T&, int);
1581 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1582 type other than bool, and VQ is either volatile or empty, there exist
1583 candidate operator functions of the form
1584 VQ T& operator--(VQ T&);
1585 T operator--(VQ T&, int);
1586 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1587 complete object type, and VQ is either volatile or empty, there exist
1588 candidate operator functions of the form
1589 T*VQ& operator++(T*VQ&);
1590 T*VQ& operator--(T*VQ&);
1591 T* operator++(T*VQ&, int);
1592 T* operator--(T*VQ&, int); */
1594 case POSTDECREMENT_EXPR:
1595 case PREDECREMENT_EXPR:
1596 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1597 return candidates;
1598 case POSTINCREMENT_EXPR:
1599 case PREINCREMENT_EXPR:
1600 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1601 || TYPE_PTROB_P (type1))
1603 type1 = build_reference_type (type1);
1604 break;
1606 return candidates;
1608 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1609 exist candidate operator functions of the form
1611 T& operator*(T*);
1613 8 For every function type T, there exist candidate operator functions of
1614 the form
1615 T& operator*(T*); */
1617 case INDIRECT_REF:
1618 if (TREE_CODE (type1) == POINTER_TYPE
1619 && (TYPE_PTROB_P (type1)
1620 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1621 break;
1622 return candidates;
1624 /* 9 For every type T, there exist candidate operator functions of the form
1625 T* operator+(T*);
1627 10For every promoted arithmetic type T, there exist candidate operator
1628 functions of the form
1629 T operator+(T);
1630 T operator-(T); */
1632 case CONVERT_EXPR: /* unary + */
1633 if (TREE_CODE (type1) == POINTER_TYPE
1634 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1635 break;
1636 case NEGATE_EXPR:
1637 if (ARITHMETIC_TYPE_P (type1))
1638 break;
1639 return candidates;
1641 /* 11For every promoted integral type T, there exist candidate operator
1642 functions of the form
1643 T operator~(T); */
1645 case BIT_NOT_EXPR:
1646 if (INTEGRAL_TYPE_P (type1))
1647 break;
1648 return candidates;
1650 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1651 is the same type as C2 or is a derived class of C2, T is a complete
1652 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1653 there exist candidate operator functions of the form
1654 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1655 where CV12 is the union of CV1 and CV2. */
1657 case MEMBER_REF:
1658 if (TREE_CODE (type1) == POINTER_TYPE
1659 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1661 tree c1 = TREE_TYPE (type1);
1662 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1663 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1664 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1666 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1667 && (TYPE_PTRMEMFUNC_P (type2)
1668 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1669 break;
1671 return candidates;
1673 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1674 didate operator functions of the form
1675 LR operator*(L, R);
1676 LR operator/(L, R);
1677 LR operator+(L, R);
1678 LR operator-(L, R);
1679 bool operator<(L, R);
1680 bool operator>(L, R);
1681 bool operator<=(L, R);
1682 bool operator>=(L, R);
1683 bool operator==(L, R);
1684 bool operator!=(L, R);
1685 where LR is the result of the usual arithmetic conversions between
1686 types L and R.
1688 14For every pair of types T and I, where T is a cv-qualified or cv-
1689 unqualified complete object type and I is a promoted integral type,
1690 there exist candidate operator functions of the form
1691 T* operator+(T*, I);
1692 T& operator[](T*, I);
1693 T* operator-(T*, I);
1694 T* operator+(I, T*);
1695 T& operator[](I, T*);
1697 15For every T, where T is a pointer to complete object type, there exist
1698 candidate operator functions of the form112)
1699 ptrdiff_t operator-(T, T);
1701 16For every pointer type T, there exist candidate operator functions of
1702 the form
1703 bool operator<(T, T);
1704 bool operator>(T, T);
1705 bool operator<=(T, T);
1706 bool operator>=(T, T);
1707 bool operator==(T, T);
1708 bool operator!=(T, T);
1710 17For every pointer to member type T, there exist candidate operator
1711 functions of the form
1712 bool operator==(T, T);
1713 bool operator!=(T, T); */
1715 case MINUS_EXPR:
1716 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1717 break;
1718 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1720 type2 = ptrdiff_type_node;
1721 break;
1723 case MULT_EXPR:
1724 case TRUNC_DIV_EXPR:
1725 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1726 break;
1727 return candidates;
1729 case EQ_EXPR:
1730 case NE_EXPR:
1731 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1732 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1733 break;
1734 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1735 && null_ptr_cst_p (args[1]))
1737 type2 = type1;
1738 break;
1740 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1741 && null_ptr_cst_p (args[0]))
1743 type1 = type2;
1744 break;
1746 case LT_EXPR:
1747 case GT_EXPR:
1748 case LE_EXPR:
1749 case GE_EXPR:
1750 case MAX_EXPR:
1751 case MIN_EXPR:
1752 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1753 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1754 break;
1755 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1757 type2 = type1;
1758 break;
1760 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1762 type1 = type2;
1763 break;
1765 return candidates;
1767 case PLUS_EXPR:
1768 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1769 break;
1770 case ARRAY_REF:
1771 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1773 type1 = ptrdiff_type_node;
1774 break;
1776 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1778 type2 = ptrdiff_type_node;
1779 break;
1781 return candidates;
1783 /* 18For every pair of promoted integral types L and R, there exist candi-
1784 date operator functions of the form
1785 LR operator%(L, R);
1786 LR operator&(L, R);
1787 LR operator^(L, R);
1788 LR operator|(L, R);
1789 L operator<<(L, R);
1790 L operator>>(L, R);
1791 where LR is the result of the usual arithmetic conversions between
1792 types L and R. */
1794 case TRUNC_MOD_EXPR:
1795 case BIT_AND_EXPR:
1796 case BIT_IOR_EXPR:
1797 case BIT_XOR_EXPR:
1798 case LSHIFT_EXPR:
1799 case RSHIFT_EXPR:
1800 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1801 break;
1802 return candidates;
1804 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1805 type, VQ is either volatile or empty, and R is a promoted arithmetic
1806 type, there exist candidate operator functions of the form
1807 VQ L& operator=(VQ L&, R);
1808 VQ L& operator*=(VQ L&, R);
1809 VQ L& operator/=(VQ L&, R);
1810 VQ L& operator+=(VQ L&, R);
1811 VQ L& operator-=(VQ L&, R);
1813 20For every pair T, VQ), where T is any type and VQ is either volatile
1814 or empty, there exist candidate operator functions of the form
1815 T*VQ& operator=(T*VQ&, T*);
1817 21For every pair T, VQ), where T is a pointer to member type and VQ is
1818 either volatile or empty, there exist candidate operator functions of
1819 the form
1820 VQ T& operator=(VQ T&, T);
1822 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1823 unqualified complete object type, VQ is either volatile or empty, and
1824 I is a promoted integral type, there exist candidate operator func-
1825 tions of the form
1826 T*VQ& operator+=(T*VQ&, I);
1827 T*VQ& operator-=(T*VQ&, I);
1829 23For every triple L, VQ, R), where L is an integral or enumeration
1830 type, VQ is either volatile or empty, and R is a promoted integral
1831 type, there exist candidate operator functions of the form
1833 VQ L& operator%=(VQ L&, R);
1834 VQ L& operator<<=(VQ L&, R);
1835 VQ L& operator>>=(VQ L&, R);
1836 VQ L& operator&=(VQ L&, R);
1837 VQ L& operator^=(VQ L&, R);
1838 VQ L& operator|=(VQ L&, R); */
1840 case MODIFY_EXPR:
1841 switch (code2)
1843 case PLUS_EXPR:
1844 case MINUS_EXPR:
1845 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1847 type2 = ptrdiff_type_node;
1848 break;
1850 case MULT_EXPR:
1851 case TRUNC_DIV_EXPR:
1852 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1853 break;
1854 return candidates;
1856 case TRUNC_MOD_EXPR:
1857 case BIT_AND_EXPR:
1858 case BIT_IOR_EXPR:
1859 case BIT_XOR_EXPR:
1860 case LSHIFT_EXPR:
1861 case RSHIFT_EXPR:
1862 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1863 break;
1864 return candidates;
1866 case NOP_EXPR:
1867 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1868 break;
1869 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1870 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1871 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1872 || ((TYPE_PTRMEMFUNC_P (type1)
1873 || TREE_CODE (type1) == POINTER_TYPE)
1874 && null_ptr_cst_p (args[1])))
1876 type2 = type1;
1877 break;
1879 return candidates;
1881 default:
1882 my_friendly_abort (367);
1884 type1 = build_reference_type (type1);
1885 break;
1887 case COND_EXPR:
1888 /* [over.builtin]
1890 For every pair of promoted arithmetic types L and R, there
1891 exist candidate operator functions of the form
1893 LR operator?(bool, L, R);
1895 where LR is the result of the usual arithmetic conversions
1896 between types L and R.
1898 For every type T, where T is a pointer or pointer-to-member
1899 type, there exist candidate operator functions of the form T
1900 operator?(bool, T, T); */
1902 if (promoted_arithmetic_type_p (type1)
1903 && promoted_arithmetic_type_p (type2))
1904 /* That's OK. */
1905 break;
1907 /* Otherwise, the types should be pointers. */
1908 if (!(TREE_CODE (type1) == POINTER_TYPE
1909 || TYPE_PTRMEM_P (type1)
1910 || TYPE_PTRMEMFUNC_P (type1))
1911 || !(TREE_CODE (type2) == POINTER_TYPE
1912 || TYPE_PTRMEM_P (type2)
1913 || TYPE_PTRMEMFUNC_P (type2)))
1914 return candidates;
1916 /* We don't check that the two types are the same; the logic
1917 below will actually create two candidates; one in which both
1918 parameter types are TYPE1, and one in which both parameter
1919 types are TYPE2. */
1920 break;
1922 /* These arguments do not make for a legal overloaded operator. */
1923 return candidates;
1925 default:
1926 my_friendly_abort (367);
1929 /* If we're dealing with two pointer types, we need candidates
1930 for both of them. */
1931 if (type2 && !same_type_p (type1, type2)
1932 && TREE_CODE (type1) == TREE_CODE (type2)
1933 && (TREE_CODE (type1) == REFERENCE_TYPE
1934 || (TREE_CODE (type1) == POINTER_TYPE
1935 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1936 || TYPE_PTRMEMFUNC_P (type1)
1937 || IS_AGGR_TYPE (type1)))
1939 candidates = build_builtin_candidate
1940 (candidates, fnname, type1, type1, args, argtypes, flags);
1941 return build_builtin_candidate
1942 (candidates, fnname, type2, type2, args, argtypes, flags);
1945 return build_builtin_candidate
1946 (candidates, fnname, type1, type2, args, argtypes, flags);
1949 tree
1950 type_decays_to (type)
1951 tree type;
1953 if (TREE_CODE (type) == ARRAY_TYPE)
1954 return build_pointer_type (TREE_TYPE (type));
1955 if (TREE_CODE (type) == FUNCTION_TYPE)
1956 return build_pointer_type (type);
1957 return type;
1960 /* There are three conditions of builtin candidates:
1962 1) bool-taking candidates. These are the same regardless of the input.
1963 2) pointer-pair taking candidates. These are generated for each type
1964 one of the input types converts to.
1965 3) arithmetic candidates. According to the standard, we should generate
1966 all of these, but I'm trying not to... */
1968 static struct z_candidate *
1969 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1970 struct z_candidate *candidates;
1971 enum tree_code code, code2;
1972 tree fnname, *args;
1973 int flags;
1975 int ref1, i;
1976 tree type, argtypes[3];
1977 /* TYPES[i] is the set of possible builtin-operator parameter types
1978 we will consider for the Ith argument. These are represented as
1979 a TREE_LIST; the TREE_VALUE of each node is the potential
1980 parameter type. */
1981 tree types[2];
1983 for (i = 0; i < 3; ++i)
1985 if (args[i])
1986 argtypes[i] = lvalue_type (args[i]);
1987 else
1988 argtypes[i] = NULL_TREE;
1991 switch (code)
1993 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1994 and VQ is either volatile or empty, there exist candidate operator
1995 functions of the form
1996 VQ T& operator++(VQ T&); */
1998 case POSTINCREMENT_EXPR:
1999 case PREINCREMENT_EXPR:
2000 case POSTDECREMENT_EXPR:
2001 case PREDECREMENT_EXPR:
2002 case MODIFY_EXPR:
2003 ref1 = 1;
2004 break;
2006 /* 24There also exist candidate operator functions of the form
2007 bool operator!(bool);
2008 bool operator&&(bool, bool);
2009 bool operator||(bool, bool); */
2011 case TRUTH_NOT_EXPR:
2012 return build_builtin_candidate
2013 (candidates, fnname, boolean_type_node,
2014 NULL_TREE, args, argtypes, flags);
2016 case TRUTH_ORIF_EXPR:
2017 case TRUTH_ANDIF_EXPR:
2018 return build_builtin_candidate
2019 (candidates, fnname, boolean_type_node,
2020 boolean_type_node, args, argtypes, flags);
2022 case ADDR_EXPR:
2023 case COMPOUND_EXPR:
2024 case COMPONENT_REF:
2025 return candidates;
2027 default:
2028 ref1 = 0;
2031 types[0] = types[1] = NULL_TREE;
2033 for (i = 0; i < 2; ++i)
2035 if (! args[i])
2037 else if (IS_AGGR_TYPE (argtypes[i]))
2039 tree convs;
2041 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2042 return candidates;
2044 convs = lookup_conversions (argtypes[i]);
2046 if (code == COND_EXPR)
2048 if (real_lvalue_p (args[i]))
2049 types[i] = tree_cons
2050 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2052 types[i] = tree_cons
2053 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2056 else if (! convs)
2057 return candidates;
2059 for (; convs; convs = TREE_CHAIN (convs))
2061 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2063 if (i == 0 && ref1
2064 && (TREE_CODE (type) != REFERENCE_TYPE
2065 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2066 continue;
2068 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2069 types[i] = tree_cons (NULL_TREE, type, types[i]);
2071 type = non_reference (type);
2072 if (i != 0 || ! ref1)
2074 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2075 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2076 types[i] = tree_cons (NULL_TREE, type, types[i]);
2077 if (INTEGRAL_TYPE_P (type))
2078 type = type_promotes_to (type);
2081 if (! value_member (type, types[i]))
2082 types[i] = tree_cons (NULL_TREE, type, types[i]);
2085 else
2087 if (code == COND_EXPR && real_lvalue_p (args[i]))
2088 types[i] = tree_cons
2089 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2090 type = non_reference (argtypes[i]);
2091 if (i != 0 || ! ref1)
2093 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2094 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2095 types[i] = tree_cons (NULL_TREE, type, types[i]);
2096 if (INTEGRAL_TYPE_P (type))
2097 type = type_promotes_to (type);
2099 types[i] = tree_cons (NULL_TREE, type, types[i]);
2103 /* Run through the possible parameter types of both arguments,
2104 creating candidates with those parameter types. */
2105 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2107 if (types[1])
2108 for (type = types[1]; type; type = TREE_CHAIN (type))
2109 candidates = add_builtin_candidate
2110 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2111 TREE_VALUE (type), args, argtypes, flags);
2112 else
2113 candidates = add_builtin_candidate
2114 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2115 NULL_TREE, args, argtypes, flags);
2118 return candidates;
2122 /* If TMPL can be successfully instantiated as indicated by
2123 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2125 TMPL is the template. EXPLICIT_TARGS are any explicit template
2126 arguments. ARGLIST is the arguments provided at the call-site.
2127 The RETURN_TYPE is the desired type for conversion operators. If
2128 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2129 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2130 add_conv_candidate. */
2132 static struct z_candidate*
2133 add_template_candidate_real (candidates, tmpl, ctype, explicit_targs,
2134 arglist, return_type, flags,
2135 obj, strict)
2136 struct z_candidate *candidates;
2137 tree tmpl, ctype, explicit_targs, arglist, return_type;
2138 int flags;
2139 tree obj;
2140 unification_kind_t strict;
2142 int ntparms = DECL_NTPARMS (tmpl);
2143 tree targs = make_tree_vec (ntparms);
2144 tree args_without_in_chrg;
2145 struct z_candidate *cand;
2146 int i;
2147 tree fn;
2149 /* TEMPLATE_DECLs do not have the in-charge parameter, nor the VTT
2150 parameter. So, skip it here before attempting to perform
2151 argument deduction. */
2152 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2153 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2154 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2155 args_without_in_chrg = tree_cons (NULL_TREE,
2156 TREE_VALUE (arglist),
2157 TREE_CHAIN (TREE_CHAIN (arglist)));
2158 else
2159 args_without_in_chrg = arglist;
2161 i = fn_type_unification (tmpl, explicit_targs, targs,
2162 args_without_in_chrg,
2163 return_type, strict);
2165 if (i != 0)
2166 return candidates;
2168 fn = instantiate_template (tmpl, targs);
2169 if (fn == error_mark_node)
2170 return candidates;
2172 if (obj != NULL_TREE)
2173 /* Aha, this is a conversion function. */
2174 cand = add_conv_candidate (candidates, fn, obj, arglist);
2175 else
2176 cand = add_function_candidate (candidates, fn, ctype,
2177 arglist, flags);
2178 if (DECL_TI_TEMPLATE (fn) != tmpl)
2179 /* This situation can occur if a member template of a template
2180 class is specialized. Then, instantiate_template might return
2181 an instantiation of the specialization, in which case the
2182 DECL_TI_TEMPLATE field will point at the original
2183 specialization. For example:
2185 template <class T> struct S { template <class U> void f(U);
2186 template <> void f(int) {}; };
2187 S<double> sd;
2188 sd.f(3);
2190 Here, TMPL will be template <class U> S<double>::f(U).
2191 And, instantiate template will give us the specialization
2192 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2193 for this will point at template <class T> template <> S<T>::f(int),
2194 so that we can find the definition. For the purposes of
2195 overload resolution, however, we want the original TMPL. */
2196 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2197 else
2198 cand->template = DECL_TEMPLATE_INFO (fn);
2200 return cand;
2204 static struct z_candidate *
2205 add_template_candidate (candidates, tmpl, ctype, explicit_targs,
2206 arglist, return_type, flags, strict)
2207 struct z_candidate *candidates;
2208 tree tmpl, ctype, explicit_targs, arglist, return_type;
2209 int flags;
2210 unification_kind_t strict;
2212 return
2213 add_template_candidate_real (candidates, tmpl, ctype,
2214 explicit_targs, arglist, return_type, flags,
2215 NULL_TREE, strict);
2219 static struct z_candidate *
2220 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2221 struct z_candidate *candidates;
2222 tree tmpl, obj, arglist, return_type;
2224 return
2225 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2226 arglist, return_type, 0, obj, DEDUCE_CONV);
2230 static int
2231 any_viable (cands)
2232 struct z_candidate *cands;
2234 for (; cands; cands = cands->next)
2235 if (pedantic ? cands->viable == 1 : cands->viable)
2236 return 1;
2237 return 0;
2240 static struct z_candidate *
2241 splice_viable (cands)
2242 struct z_candidate *cands;
2244 struct z_candidate **p = &cands;
2246 for (; *p; )
2248 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2249 p = &((*p)->next);
2250 else
2251 *p = (*p)->next;
2254 return cands;
2257 static tree
2258 build_this (obj)
2259 tree obj;
2261 /* Fix this to work on non-lvalues. */
2262 return build_unary_op (ADDR_EXPR, obj, 0);
2265 static void
2266 print_z_candidates (candidates)
2267 struct z_candidate *candidates;
2269 const char *str = "candidates are:";
2270 for (; candidates; candidates = candidates->next)
2272 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2274 if (TREE_VEC_LENGTH (candidates->convs) == 3)
2275 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2276 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2277 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2278 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2279 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2280 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2281 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2282 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2283 else
2284 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2285 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2287 else if (TYPE_P (candidates->fn))
2288 cp_error ("%s %T <conversion>", str, candidates->fn);
2289 else
2290 cp_error_at ("%s %+#D%s", str, candidates->fn,
2291 candidates->viable == -1 ? " <near match>" : "");
2292 str = " ";
2296 /* Returns the best overload candidate to perform the requested
2297 conversion. This function is used for three the overloading situations
2298 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2299 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2300 per [dcl.init.ref], so we ignore temporary bindings. */
2302 static struct z_candidate *
2303 build_user_type_conversion_1 (totype, expr, flags)
2304 tree totype, expr;
2305 int flags;
2307 struct z_candidate *candidates, *cand;
2308 tree fromtype = TREE_TYPE (expr);
2309 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2310 tree args = NULL_TREE;
2311 tree templates = NULL_TREE;
2313 if (IS_AGGR_TYPE (totype))
2314 ctors = lookup_fnfields (TYPE_BINFO (totype),
2315 (flag_new_abi
2316 ? complete_ctor_identifier
2317 : ctor_identifier),
2320 if (IS_AGGR_TYPE (fromtype)
2321 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2322 convs = lookup_conversions (fromtype);
2324 candidates = 0;
2325 flags |= LOOKUP_NO_CONVERSION;
2327 if (ctors)
2329 tree t;
2331 ctors = TREE_VALUE (ctors);
2333 t = build_int_2 (0, 0);
2334 TREE_TYPE (t) = build_pointer_type (totype);
2335 args = build_tree_list (NULL_TREE, expr);
2336 if (DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors)))
2337 args = tree_cons (NULL_TREE,
2338 in_charge_arg_for_name (complete_ctor_identifier),
2339 args);
2340 args = tree_cons (NULL_TREE, t, args);
2342 for (; ctors; ctors = OVL_NEXT (ctors))
2344 tree ctor = OVL_CURRENT (ctors);
2345 if (DECL_NONCONVERTING_P (ctor))
2346 continue;
2348 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2350 templates = tree_cons (NULL_TREE, ctor, templates);
2351 candidates =
2352 add_template_candidate (candidates, ctor, totype,
2353 NULL_TREE, args, NULL_TREE, flags,
2354 DEDUCE_CALL);
2356 else
2357 candidates = add_function_candidate (candidates, ctor, totype,
2358 args, flags);
2360 if (candidates)
2362 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2363 candidates->basetype_path = TYPE_BINFO (totype);
2367 if (convs)
2368 args = build_tree_list (NULL_TREE, build_this (expr));
2370 for (; convs; convs = TREE_CHAIN (convs))
2372 tree fns = TREE_VALUE (convs);
2373 int convflags = LOOKUP_NO_CONVERSION;
2374 tree ics;
2376 /* If we are called to convert to a reference type, we are trying to
2377 find an lvalue binding, so don't even consider temporaries. If
2378 we don't find an lvalue binding, the caller will try again to
2379 look for a temporary binding. */
2380 if (TREE_CODE (totype) == REFERENCE_TYPE)
2381 convflags |= LOOKUP_NO_TEMP_BIND;
2383 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2384 ics = implicit_conversion
2385 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2386 else
2387 /* We can't compute this yet. */
2388 ics = error_mark_node;
2390 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2391 /* ignore the near match. */;
2392 else if (ics)
2393 for (; fns; fns = OVL_NEXT (fns))
2395 tree fn = OVL_CURRENT (fns);
2396 struct z_candidate *old_candidates = candidates;
2398 /* [over.match.funcs] For conversion functions, the function is
2399 considered to be a member of the class of the implicit object
2400 argument for the purpose of defining the type of the implicit
2401 object parameter.
2403 So we pass fromtype as CTYPE to add_*_candidate. */
2405 if (TREE_CODE (fn) == TEMPLATE_DECL)
2407 templates = tree_cons (NULL_TREE, fn, templates);
2408 candidates =
2409 add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2410 args, totype, flags,
2411 DEDUCE_CONV);
2413 else
2414 candidates = add_function_candidate (candidates, fn, fromtype,
2415 args, flags);
2417 if (candidates != old_candidates)
2419 if (TREE_CODE (fn) == TEMPLATE_DECL)
2420 ics = implicit_conversion
2421 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2422 0, convflags);
2424 candidates->second_conv = ics;
2425 candidates->basetype_path = TYPE_BINFO (fromtype);
2427 if (ics == NULL_TREE)
2428 candidates->viable = 0;
2429 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2430 candidates->viable = -1;
2435 if (! any_viable (candidates))
2437 #if 0
2438 if (flags & LOOKUP_COMPLAIN)
2440 if (candidates && ! candidates->next)
2441 /* say why this one won't work or try to be loose */;
2442 else
2443 cp_error ("no viable candidates");
2445 #endif
2447 return 0;
2450 candidates = splice_viable (candidates);
2451 cand = tourney (candidates);
2453 if (cand == 0)
2455 if (flags & LOOKUP_COMPLAIN)
2457 cp_error ("conversion from `%T' to `%T' is ambiguous",
2458 fromtype, totype);
2459 print_z_candidates (candidates);
2462 cand = candidates; /* any one will do */
2463 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2464 ICS_USER_FLAG (cand->second_conv) = 1;
2465 ICS_BAD_FLAG (cand->second_conv) = 1;
2467 return cand;
2470 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2471 p = &(TREE_OPERAND (*p, 0));
2473 *p = build
2474 (USER_CONV,
2475 (DECL_CONSTRUCTOR_P (cand->fn)
2476 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2477 expr, build_expr_ptr_wrapper (cand));
2478 ICS_USER_FLAG (cand->second_conv) = 1;
2479 if (cand->viable == -1)
2480 ICS_BAD_FLAG (cand->second_conv) = 1;
2482 return cand;
2485 tree
2486 build_user_type_conversion (totype, expr, flags)
2487 tree totype, expr;
2488 int flags;
2490 struct z_candidate *cand
2491 = build_user_type_conversion_1 (totype, expr, flags);
2493 if (cand)
2495 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2496 return error_mark_node;
2497 return convert_from_reference
2498 (convert_like_with_context
2499 (cand->second_conv, expr, cand->fn, 0));
2501 return NULL_TREE;
2504 /* Do any initial processing on the arguments to a function call. */
2506 static tree
2507 resolve_args (args)
2508 tree args;
2510 tree t;
2511 for (t = args; t; t = TREE_CHAIN (t))
2513 if (TREE_VALUE (t) == error_mark_node)
2514 return error_mark_node;
2515 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2517 error ("invalid use of void expression");
2518 return error_mark_node;
2520 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2521 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2523 return args;
2526 tree
2527 build_new_function_call (fn, args)
2528 tree fn, args;
2530 struct z_candidate *candidates = 0, *cand;
2531 tree explicit_targs = NULL_TREE;
2532 int template_only = 0;
2534 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2536 explicit_targs = TREE_OPERAND (fn, 1);
2537 fn = TREE_OPERAND (fn, 0);
2538 template_only = 1;
2541 if (really_overloaded_fn (fn))
2543 tree t1;
2544 tree templates = NULL_TREE;
2546 args = resolve_args (args);
2548 if (args == error_mark_node)
2549 return error_mark_node;
2551 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2553 tree t = OVL_FUNCTION (t1);
2555 if (TREE_CODE (t) == TEMPLATE_DECL)
2557 templates = tree_cons (NULL_TREE, t, templates);
2558 candidates = add_template_candidate
2559 (candidates, t, NULL_TREE, explicit_targs, args, NULL_TREE,
2560 LOOKUP_NORMAL, DEDUCE_CALL);
2562 else if (! template_only)
2563 candidates = add_function_candidate
2564 (candidates, t, NULL_TREE, args, LOOKUP_NORMAL);
2567 if (! any_viable (candidates))
2569 if (candidates && ! candidates->next)
2570 return build_function_call (candidates->fn, args);
2571 cp_error ("no matching function for call to `%D (%A)'",
2572 DECL_NAME (OVL_FUNCTION (fn)), args);
2573 if (candidates)
2574 print_z_candidates (candidates);
2575 return error_mark_node;
2577 candidates = splice_viable (candidates);
2578 cand = tourney (candidates);
2580 if (cand == 0)
2582 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2583 DECL_NAME (OVL_FUNCTION (fn)), args);
2584 print_z_candidates (candidates);
2585 return error_mark_node;
2588 return build_over_call (cand, args, LOOKUP_NORMAL);
2591 /* This is not really overloaded. */
2592 fn = OVL_CURRENT (fn);
2594 return build_function_call (fn, args);
2597 static tree
2598 build_object_call (obj, args)
2599 tree obj, args;
2601 struct z_candidate *candidates = 0, *cand;
2602 tree fns, convs, mem_args = NULL_TREE;
2603 tree type = TREE_TYPE (obj);
2605 if (TYPE_PTRMEMFUNC_P (type))
2607 /* It's no good looking for an overloaded operator() on a
2608 pointer-to-member-function. */
2609 cp_error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2610 return error_mark_node;
2613 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2614 if (fns == error_mark_node)
2615 return error_mark_node;
2617 args = resolve_args (args);
2619 if (args == error_mark_node)
2620 return error_mark_node;
2622 if (fns)
2624 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
2625 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2627 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2629 tree fn = OVL_CURRENT (fns);
2630 if (TREE_CODE (fn) == TEMPLATE_DECL)
2632 candidates
2633 = add_template_candidate (candidates, fn, base, NULL_TREE,
2634 mem_args, NULL_TREE,
2635 LOOKUP_NORMAL, DEDUCE_CALL);
2637 else
2638 candidates = add_function_candidate
2639 (candidates, fn, base, mem_args, LOOKUP_NORMAL);
2641 if (candidates)
2642 candidates->basetype_path = TYPE_BINFO (type);
2646 convs = lookup_conversions (type);
2648 for (; convs; convs = TREE_CHAIN (convs))
2650 tree fns = TREE_VALUE (convs);
2651 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2653 if ((TREE_CODE (totype) == POINTER_TYPE
2654 || TREE_CODE (totype) == REFERENCE_TYPE)
2655 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2656 for (; fns; fns = OVL_NEXT (fns))
2658 tree fn = OVL_CURRENT (fns);
2659 if (TREE_CODE (fn) == TEMPLATE_DECL)
2661 candidates = add_template_conv_candidate (candidates,
2663 obj,
2664 args,
2665 totype);
2667 else
2668 candidates = add_conv_candidate (candidates, fn, obj, args);
2672 if (! any_viable (candidates))
2674 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2675 print_z_candidates (candidates);
2676 return error_mark_node;
2679 candidates = splice_viable (candidates);
2680 cand = tourney (candidates);
2682 if (cand == 0)
2684 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2685 print_z_candidates (candidates);
2686 return error_mark_node;
2689 /* Since cand->fn will be a type, not a function, for a conversion
2690 function, we must be careful not to unconditionally look at
2691 DECL_NAME here. */
2692 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2693 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2694 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2696 obj = convert_like_with_context
2697 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2699 /* FIXME */
2700 return build_function_call (obj, args);
2703 static void
2704 op_error (code, code2, arg1, arg2, arg3, problem)
2705 enum tree_code code, code2;
2706 tree arg1, arg2, arg3;
2707 const char *problem;
2709 const char * opname;
2711 if (code == MODIFY_EXPR)
2712 opname = assignment_operator_name_info[code2].name;
2713 else
2714 opname = operator_name_info[code].name;
2716 switch (code)
2718 case COND_EXPR:
2719 cp_error ("%s for `%T ? %T : %T'", problem,
2720 error_type (arg1), error_type (arg2), error_type (arg3));
2721 break;
2722 case POSTINCREMENT_EXPR:
2723 case POSTDECREMENT_EXPR:
2724 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2725 break;
2726 case ARRAY_REF:
2727 cp_error ("%s for `%T[%T]'", problem,
2728 error_type (arg1), error_type (arg2));
2729 break;
2730 default:
2731 if (arg2)
2732 cp_error ("%s for `%T %s %T'", problem,
2733 error_type (arg1), opname, error_type (arg2));
2734 else
2735 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2739 /* Return the implicit conversion sequence that could be used to
2740 convert E1 to E2 in [expr.cond]. */
2742 static tree
2743 conditional_conversion (e1, e2)
2744 tree e1;
2745 tree e2;
2747 tree t1 = non_reference (TREE_TYPE (e1));
2748 tree t2 = non_reference (TREE_TYPE (e2));
2749 tree conv;
2751 /* [expr.cond]
2753 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2754 implicitly converted (clause _conv_) to the type "reference to
2755 T2", subject to the constraint that in the conversion the
2756 reference must bind directly (_dcl.init.ref_) to E1. */
2757 if (real_lvalue_p (e2))
2759 conv = implicit_conversion (build_reference_type (t2),
2762 LOOKUP_NO_TEMP_BIND);
2763 if (conv)
2764 return conv;
2767 /* [expr.cond]
2769 If E1 and E2 have class type, and the underlying class types are
2770 the same or one is a base class of the other: E1 can be converted
2771 to match E2 if the class of T2 is the same type as, or a base
2772 class of, the class of T1, and the cv-qualification of T2 is the
2773 same cv-qualification as, or a greater cv-qualification than, the
2774 cv-qualification of T1. If the conversion is applied, E1 is
2775 changed to an rvalue of type T2 that still refers to the original
2776 source class object (or the appropriate subobject thereof). */
2777 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2778 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2779 TYPE_MAIN_VARIANT (t1)))
2781 if (at_least_as_qualified_p (t2, t1))
2783 conv = build1 (IDENTITY_CONV, t1, e1);
2784 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2785 TYPE_MAIN_VARIANT (t2)))
2786 conv = build_conv (BASE_CONV, t2, conv);
2787 return conv;
2789 else
2790 return NULL_TREE;
2793 /* [expr.cond]
2795 E1 can be converted to match E2 if E1 can be implicitly converted
2796 to the type that expression E2 would have if E2 were converted to
2797 an rvalue (or the type it has, if E2 is an rvalue). */
2798 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2801 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2802 arguments to the conditional expression. By the time this function
2803 is called, any suitable candidate functions are included in
2804 CANDIDATES. */
2806 tree
2807 build_conditional_expr (arg1, arg2, arg3)
2808 tree arg1;
2809 tree arg2;
2810 tree arg3;
2812 tree arg2_type;
2813 tree arg3_type;
2814 tree result;
2815 tree result_type = NULL_TREE;
2816 int lvalue_p = 1;
2817 struct z_candidate *candidates = 0;
2818 struct z_candidate *cand;
2820 /* As a G++ extension, the second argument to the conditional can be
2821 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2822 c'.) If the second operand is omitted, make sure it is
2823 calculated only once. */
2824 if (!arg2)
2826 if (pedantic)
2827 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2828 arg1 = arg2 = save_expr (arg1);
2831 /* [expr.cond]
2833 The first expr ession is implicitly converted to bool (clause
2834 _conv_). */
2835 arg1 = cp_convert (boolean_type_node, arg1);
2837 /* If something has already gone wrong, just pass that fact up the
2838 tree. */
2839 if (arg1 == error_mark_node
2840 || arg2 == error_mark_node
2841 || arg3 == error_mark_node
2842 || TREE_TYPE (arg1) == error_mark_node
2843 || TREE_TYPE (arg2) == error_mark_node
2844 || TREE_TYPE (arg3) == error_mark_node)
2845 return error_mark_node;
2847 /* Convert from reference types to ordinary types; no expressions
2848 really have reference type in C++. */
2849 arg2 = convert_from_reference (arg2);
2850 arg3 = convert_from_reference (arg3);
2852 /* [expr.cond]
2854 If either the second or the third operand has type (possibly
2855 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2856 array-to-pointer (_conv.array_), and function-to-pointer
2857 (_conv.func_) standard conversions are performed on the second
2858 and third operands. */
2859 arg2_type = TREE_TYPE (arg2);
2860 arg3_type = TREE_TYPE (arg3);
2861 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
2863 /* Do the conversions. We don't these for `void' type arguments
2864 since it can't have any effect and since decay_conversion
2865 does not handle that case gracefully. */
2866 if (!VOID_TYPE_P (arg2_type))
2867 arg2 = decay_conversion (arg2);
2868 if (!VOID_TYPE_P (arg3_type))
2869 arg3 = decay_conversion (arg3);
2870 arg2_type = TREE_TYPE (arg2);
2871 arg3_type = TREE_TYPE (arg3);
2873 /* [expr.cond]
2875 One of the following shall hold:
2877 --The second or the third operand (but not both) is a
2878 throw-expression (_except.throw_); the result is of the
2879 type of the other and is an rvalue.
2881 --Both the second and the third operands have type void; the
2882 result is of type void and is an rvalue. */
2883 if ((TREE_CODE (arg2) == THROW_EXPR)
2884 ^ (TREE_CODE (arg3) == THROW_EXPR))
2885 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2886 ? arg3_type : arg2_type);
2887 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
2888 result_type = void_type_node;
2889 else
2891 cp_error ("`%E' has type `void' and is not a throw-expression",
2892 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
2893 return error_mark_node;
2896 lvalue_p = 0;
2897 goto valid_operands;
2899 /* [expr.cond]
2901 Otherwise, if the second and third operand have different types,
2902 and either has (possibly cv-qualified) class type, an attempt is
2903 made to convert each of those operands to the type of the other. */
2904 else if (!same_type_p (arg2_type, arg3_type)
2905 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2907 tree conv2 = conditional_conversion (arg2, arg3);
2908 tree conv3 = conditional_conversion (arg3, arg2);
2910 /* [expr.cond]
2912 If both can be converted, or one can be converted but the
2913 conversion is ambiguous, the program is ill-formed. If
2914 neither can be converted, the operands are left unchanged and
2915 further checking is performed as described below. If exactly
2916 one conversion is possible, that conversion is applied to the
2917 chosen operand and the converted operand is used in place of
2918 the original operand for the remainder of this section. */
2919 if ((conv2 && !ICS_BAD_FLAG (conv2)
2920 && conv3 && !ICS_BAD_FLAG (conv3))
2921 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
2922 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
2924 cp_error ("operands to ?: have different types");
2925 return error_mark_node;
2927 else if (conv2 && !ICS_BAD_FLAG (conv2))
2929 arg2 = convert_like (conv2, arg2);
2930 arg2 = convert_from_reference (arg2);
2931 /* That may not quite have done the trick. If the two types
2932 are cv-qualified variants of one another, we will have
2933 just used an IDENTITY_CONV. (There's no conversion from
2934 an lvalue of one class type to an lvalue of another type,
2935 even a cv-qualified variant, and we don't want to lose
2936 lvalue-ness here.) So, we manually add a NOP_EXPR here
2937 if necessary. */
2938 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
2939 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
2940 arg2_type = TREE_TYPE (arg2);
2942 else if (conv3 && !ICS_BAD_FLAG (conv3))
2944 arg3 = convert_like (conv3, arg3);
2945 arg3 = convert_from_reference (arg3);
2946 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
2947 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
2948 arg3_type = TREE_TYPE (arg3);
2952 /* [expr.cond]
2954 If the second and third operands are lvalues and have the same
2955 type, the result is of that type and is an lvalue. */
2956 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
2957 same_type_p (arg2_type, arg3_type))
2959 result_type = arg2_type;
2960 goto valid_operands;
2963 /* [expr.cond]
2965 Otherwise, the result is an rvalue. If the second and third
2966 operand do not have the same type, and either has (possibly
2967 cv-qualified) class type, overload resolution is used to
2968 determine the conversions (if any) to be applied to the operands
2969 (_over.match.oper_, _over.built_). */
2970 lvalue_p = 0;
2971 if (!same_type_p (arg2_type, arg3_type)
2972 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2974 tree args[3];
2975 tree conv;
2977 /* Rearrange the arguments so that add_builtin_candidate only has
2978 to know about two args. In build_builtin_candidates, the
2979 arguments are unscrambled. */
2980 args[0] = arg2;
2981 args[1] = arg3;
2982 args[2] = arg1;
2983 candidates = add_builtin_candidates (candidates,
2984 COND_EXPR,
2985 NOP_EXPR,
2986 ansi_opname (COND_EXPR),
2987 args,
2988 LOOKUP_NORMAL);
2990 /* [expr.cond]
2992 If the overload resolution fails, the program is
2993 ill-formed. */
2994 if (!any_viable (candidates))
2996 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
2997 print_z_candidates (candidates);
2998 return error_mark_node;
3000 candidates = splice_viable (candidates);
3001 cand = tourney (candidates);
3002 if (!cand)
3004 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3005 print_z_candidates (candidates);
3006 return error_mark_node;
3009 /* [expr.cond]
3011 Otherwise, the conversions thus determined are applied, and
3012 the converted operands are used in place of the original
3013 operands for the remainder of this section. */
3014 conv = TREE_VEC_ELT (cand->convs, 0);
3015 arg1 = convert_like (conv, arg1);
3016 conv = TREE_VEC_ELT (cand->convs, 1);
3017 arg2 = convert_like (conv, arg2);
3018 conv = TREE_VEC_ELT (cand->convs, 2);
3019 arg3 = convert_like (conv, arg3);
3022 /* [expr.cond]
3024 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3025 and function-to-pointer (_conv.func_) standard conversions are
3026 performed on the second and third operands.
3028 We need to force the lvalue-to-rvalue conversion here for class types,
3029 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3030 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3031 regions. */
3033 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3034 arg2 = build_user_type_conversion (TREE_TYPE (arg2), arg2, LOOKUP_NORMAL);
3035 else
3036 arg2 = decay_conversion (arg2);
3037 arg2_type = TREE_TYPE (arg2);
3039 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3040 arg3 = build_user_type_conversion (TREE_TYPE (arg3), arg3, LOOKUP_NORMAL);
3041 else
3042 arg3 = decay_conversion (arg3);
3043 arg3_type = TREE_TYPE (arg3);
3045 /* [expr.cond]
3047 After those conversions, one of the following shall hold:
3049 --The second and third operands have the same type; the result is of
3050 that type. */
3051 if (same_type_p (arg2_type, arg3_type))
3052 result_type = arg2_type;
3053 /* [expr.cond]
3055 --The second and third operands have arithmetic or enumeration
3056 type; the usual arithmetic conversions are performed to bring
3057 them to a common type, and the result is of that type. */
3058 else if ((ARITHMETIC_TYPE_P (arg2_type)
3059 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3060 && (ARITHMETIC_TYPE_P (arg3_type)
3061 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3063 /* In this case, there is always a common type. */
3064 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3065 arg3_type);
3067 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3068 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3069 cp_warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3070 arg2_type, arg3_type);
3071 else if (extra_warnings
3072 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3073 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3074 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3075 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3076 cp_warning ("enumeral and non-enumeral type in conditional expression");
3078 arg2 = perform_implicit_conversion (result_type, arg2);
3079 arg3 = perform_implicit_conversion (result_type, arg3);
3081 /* [expr.cond]
3083 --The second and third operands have pointer type, or one has
3084 pointer type and the other is a null pointer constant; pointer
3085 conversions (_conv.ptr_) and qualification conversions
3086 (_conv.qual_) are performed to bring them to their composite
3087 pointer type (_expr.rel_). The result is of the composite
3088 pointer type.
3090 --The second and third operands have pointer to member type, or
3091 one has pointer to member type and the other is a null pointer
3092 constant; pointer to member conversions (_conv.mem_) and
3093 qualification conversions (_conv.qual_) are performed to bring
3094 them to a common type, whose cv-qualification shall match the
3095 cv-qualification of either the second or the third operand.
3096 The result is of the common type. */
3097 else if ((null_ptr_cst_p (arg2)
3098 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3099 || TYPE_PTRMEMFUNC_P (arg3_type)))
3100 || (null_ptr_cst_p (arg3)
3101 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3102 || TYPE_PTRMEMFUNC_P (arg2_type)))
3103 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3104 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3105 || (TYPE_PTRMEMFUNC_P (arg2_type)
3106 && TYPE_PTRMEMFUNC_P (arg3_type)))
3108 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3109 arg3, "conditional expression");
3110 arg2 = perform_implicit_conversion (result_type, arg2);
3111 arg3 = perform_implicit_conversion (result_type, arg3);
3114 if (!result_type)
3116 cp_error ("operands to ?: have different types");
3117 return error_mark_node;
3120 valid_operands:
3121 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3122 /* Expand both sides into the same slot, hopefully the target of the
3123 ?: expression. We used to check for TARGET_EXPRs here, but now we
3124 sometimes wrap them in NOP_EXPRs so the test would fail. */
3125 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3126 result = build_target_expr_with_type (result, result_type);
3128 /* If this expression is an rvalue, but might be mistaken for an
3129 lvalue, we must add a NON_LVALUE_EXPR. */
3130 if (!lvalue_p && real_lvalue_p (result))
3131 result = build1 (NON_LVALUE_EXPR, result_type, result);
3133 return result;
3136 tree
3137 build_new_op (code, flags, arg1, arg2, arg3)
3138 enum tree_code code;
3139 int flags;
3140 tree arg1, arg2, arg3;
3142 struct z_candidate *candidates = 0, *cand;
3143 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3144 enum tree_code code2 = NOP_EXPR;
3145 tree templates = NULL_TREE;
3146 tree conv;
3148 if (arg1 == error_mark_node
3149 || arg2 == error_mark_node
3150 || arg3 == error_mark_node)
3151 return error_mark_node;
3153 /* This can happen if a template takes all non-type parameters, e.g.
3154 undeclared_template<1, 5, 72>a; */
3155 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3157 cp_error ("`%D' must be declared before use", arg1);
3158 return error_mark_node;
3161 if (code == MODIFY_EXPR)
3163 code2 = TREE_CODE (arg3);
3164 arg3 = NULL_TREE;
3165 fnname = ansi_assopname (code2);
3167 else
3168 fnname = ansi_opname (code);
3170 switch (code)
3172 case NEW_EXPR:
3173 case VEC_NEW_EXPR:
3174 case VEC_DELETE_EXPR:
3175 case DELETE_EXPR:
3176 /* Use build_op_new_call and build_op_delete_call instead. */
3177 my_friendly_abort (981018);
3179 case CALL_EXPR:
3180 return build_object_call (arg1, arg2);
3182 default:
3183 break;
3186 /* The comma operator can have void args. */
3187 if (TREE_CODE (arg1) == OFFSET_REF)
3188 arg1 = resolve_offset_ref (arg1);
3189 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
3190 arg2 = resolve_offset_ref (arg2);
3191 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
3192 arg3 = resolve_offset_ref (arg3);
3194 if (code == COND_EXPR)
3196 if (arg2 == NULL_TREE
3197 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3198 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3199 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3200 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3201 goto builtin;
3203 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3204 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3205 goto builtin;
3207 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3208 arg2 = integer_zero_node;
3210 if (arg2 && arg3)
3211 arglist = tree_cons (NULL_TREE, arg1, tree_cons
3212 (NULL_TREE, arg2, build_tree_list (NULL_TREE, arg3)));
3213 else if (arg2)
3214 arglist = tree_cons (NULL_TREE, arg1, build_tree_list (NULL_TREE, arg2));
3215 else
3216 arglist = build_tree_list (NULL_TREE, arg1);
3218 fns = lookup_function_nonclass (fnname, arglist);
3220 if (fns && TREE_CODE (fns) == TREE_LIST)
3221 fns = TREE_VALUE (fns);
3222 for (; fns; fns = OVL_NEXT (fns))
3224 tree fn = OVL_CURRENT (fns);
3225 if (TREE_CODE (fn) == TEMPLATE_DECL)
3227 templates = tree_cons (NULL_TREE, fn, templates);
3228 candidates
3229 = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3230 arglist, TREE_TYPE (fnname),
3231 flags, DEDUCE_CALL);
3233 else
3234 candidates = add_function_candidate (candidates, fn, NULL_TREE,
3235 arglist, flags);
3238 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3240 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3241 if (fns == error_mark_node)
3242 return fns;
3244 else
3245 fns = NULL_TREE;
3247 if (fns)
3249 tree basetype = BINFO_TYPE (TREE_PURPOSE (fns));
3250 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3251 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3253 tree fn = OVL_CURRENT (fns);
3254 tree this_arglist;
3256 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3257 this_arglist = mem_arglist;
3258 else
3259 this_arglist = arglist;
3261 if (TREE_CODE (fn) == TEMPLATE_DECL)
3263 /* A member template. */
3264 templates = tree_cons (NULL_TREE, fn, templates);
3265 candidates
3266 = add_template_candidate (candidates, fn, basetype, NULL_TREE,
3267 this_arglist, TREE_TYPE (fnname),
3268 flags, DEDUCE_CALL);
3270 else
3271 candidates = add_function_candidate
3272 (candidates, fn, basetype, this_arglist, flags);
3274 if (candidates)
3275 candidates->basetype_path = TYPE_BINFO (TREE_TYPE (arg1));
3280 tree args[3];
3282 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3283 to know about two args; a builtin candidate will always have a first
3284 parameter of type bool. We'll handle that in
3285 build_builtin_candidate. */
3286 if (code == COND_EXPR)
3288 args[0] = arg2;
3289 args[1] = arg3;
3290 args[2] = arg1;
3292 else
3294 args[0] = arg1;
3295 args[1] = arg2;
3296 args[2] = NULL_TREE;
3299 candidates = add_builtin_candidates
3300 (candidates, code, code2, fnname, args, flags);
3303 if (! any_viable (candidates))
3305 switch (code)
3307 case POSTINCREMENT_EXPR:
3308 case POSTDECREMENT_EXPR:
3309 /* Look for an `operator++ (int)'. If they didn't have
3310 one, then we fall back to the old way of doing things. */
3311 if (flags & LOOKUP_COMPLAIN)
3312 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
3313 fnname,
3314 operator_name_info[code].name);
3315 if (code == POSTINCREMENT_EXPR)
3316 code = PREINCREMENT_EXPR;
3317 else
3318 code = PREDECREMENT_EXPR;
3319 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3321 /* The caller will deal with these. */
3322 case ADDR_EXPR:
3323 case COMPOUND_EXPR:
3324 case COMPONENT_REF:
3325 return NULL_TREE;
3327 default:
3328 break;
3330 if (flags & LOOKUP_COMPLAIN)
3332 op_error (code, code2, arg1, arg2, arg3, "no match");
3333 print_z_candidates (candidates);
3335 return error_mark_node;
3337 candidates = splice_viable (candidates);
3338 cand = tourney (candidates);
3340 if (cand == 0)
3342 if (flags & LOOKUP_COMPLAIN)
3344 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3345 print_z_candidates (candidates);
3347 return error_mark_node;
3350 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3352 extern int warn_synth;
3353 if (warn_synth
3354 && fnname == ansi_assopname (NOP_EXPR)
3355 && DECL_ARTIFICIAL (cand->fn)
3356 && candidates->next
3357 && ! candidates->next->next)
3359 cp_warning ("using synthesized `%#D' for copy assignment",
3360 cand->fn);
3361 cp_warning_at (" where cfront would use `%#D'",
3362 cand == candidates
3363 ? candidates->next->fn
3364 : candidates->fn);
3367 return build_over_call
3368 (cand,
3369 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3370 ? mem_arglist : arglist,
3371 LOOKUP_NORMAL);
3374 /* Check for comparison of different enum types. */
3375 switch (code)
3377 case GT_EXPR:
3378 case LT_EXPR:
3379 case GE_EXPR:
3380 case LE_EXPR:
3381 case EQ_EXPR:
3382 case NE_EXPR:
3383 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3384 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3385 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3386 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3388 cp_warning ("comparison between `%#T' and `%#T'",
3389 TREE_TYPE (arg1), TREE_TYPE (arg2));
3391 break;
3392 default:
3393 break;
3396 /* We need to strip any leading REF_BIND so that bitfields don't cause
3397 errors. This should not remove any important conversions, because
3398 builtins don't apply to class objects directly. */
3399 conv = TREE_VEC_ELT (cand->convs, 0);
3400 if (TREE_CODE (conv) == REF_BIND)
3401 conv = TREE_OPERAND (conv, 0);
3402 arg1 = convert_like (conv, arg1);
3403 if (arg2)
3405 conv = TREE_VEC_ELT (cand->convs, 1);
3406 if (TREE_CODE (conv) == REF_BIND)
3407 conv = TREE_OPERAND (conv, 0);
3408 arg2 = convert_like (conv, arg2);
3410 if (arg3)
3412 conv = TREE_VEC_ELT (cand->convs, 2);
3413 if (TREE_CODE (conv) == REF_BIND)
3414 conv = TREE_OPERAND (conv, 0);
3415 arg3 = convert_like (conv, arg3);
3418 builtin:
3419 switch (code)
3421 case MODIFY_EXPR:
3422 return build_modify_expr (arg1, code2, arg2);
3424 case INDIRECT_REF:
3425 return build_indirect_ref (arg1, "unary *");
3427 case PLUS_EXPR:
3428 case MINUS_EXPR:
3429 case MULT_EXPR:
3430 case TRUNC_DIV_EXPR:
3431 case GT_EXPR:
3432 case LT_EXPR:
3433 case GE_EXPR:
3434 case LE_EXPR:
3435 case EQ_EXPR:
3436 case NE_EXPR:
3437 case MAX_EXPR:
3438 case MIN_EXPR:
3439 case LSHIFT_EXPR:
3440 case RSHIFT_EXPR:
3441 case TRUNC_MOD_EXPR:
3442 case BIT_AND_EXPR:
3443 case BIT_IOR_EXPR:
3444 case BIT_XOR_EXPR:
3445 case TRUTH_ANDIF_EXPR:
3446 case TRUTH_ORIF_EXPR:
3447 return build_binary_op (code, arg1, arg2);
3449 case CONVERT_EXPR:
3450 case NEGATE_EXPR:
3451 case BIT_NOT_EXPR:
3452 case TRUTH_NOT_EXPR:
3453 case PREINCREMENT_EXPR:
3454 case POSTINCREMENT_EXPR:
3455 case PREDECREMENT_EXPR:
3456 case POSTDECREMENT_EXPR:
3457 case REALPART_EXPR:
3458 case IMAGPART_EXPR:
3459 return build_unary_op (code, arg1, candidates != 0);
3461 case ARRAY_REF:
3462 return build_array_ref (arg1, arg2);
3464 case COND_EXPR:
3465 return build_conditional_expr (arg1, arg2, arg3);
3467 case MEMBER_REF:
3468 return build_m_component_ref
3469 (build_indirect_ref (arg1, NULL_PTR), arg2);
3471 /* The caller will deal with these. */
3472 case ADDR_EXPR:
3473 case COMPONENT_REF:
3474 case COMPOUND_EXPR:
3475 return NULL_TREE;
3477 default:
3478 my_friendly_abort (367);
3479 return NULL_TREE;
3483 /* Build a call to operator delete. This has to be handled very specially,
3484 because the restrictions on what signatures match are different from all
3485 other call instances. For a normal delete, only a delete taking (void *)
3486 or (void *, size_t) is accepted. For a placement delete, only an exact
3487 match with the placement new is accepted.
3489 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3490 ADDR is the pointer to be deleted. For placement delete, it is also
3491 used to determine what the corresponding new looked like.
3492 SIZE is the size of the memory block to be deleted.
3493 FLAGS are the usual overloading flags.
3494 PLACEMENT is the corresponding placement new call, or 0. */
3496 tree
3497 build_op_delete_call (code, addr, size, flags, placement)
3498 enum tree_code code;
3499 tree addr, size, placement;
3500 int flags;
3502 tree fn, fns, fnname, fntype, argtypes, args, type;
3504 if (addr == error_mark_node)
3505 return error_mark_node;
3507 type = TREE_TYPE (TREE_TYPE (addr));
3508 fnname = ansi_opname (code);
3510 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3511 /* In [class.free]
3513 If the result of the lookup is ambiguous or inaccessible, or if
3514 the lookup selects a placement deallocation function, the
3515 program is ill-formed.
3517 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3519 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3520 if (fns == error_mark_node)
3521 return error_mark_node;
3523 else
3524 fns = NULL_TREE;
3526 if (fns == NULL_TREE)
3527 fns = lookup_name_nonclass (fnname);
3529 if (placement)
3531 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3533 /* Extract the function. */
3534 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3535 /* Then the second parm type. */
3536 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3538 /* Also the second argument. */
3539 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3541 else
3543 /* First try it without the size argument. */
3544 argtypes = void_list_node;
3545 args = NULL_TREE;
3548 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3549 fntype = build_function_type (void_type_node, argtypes);
3551 /* Strip const and volatile from addr. */
3552 if (type != TYPE_MAIN_VARIANT (type))
3553 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
3555 fn = instantiate_type (fntype, fns, 2);
3557 if (fn != error_mark_node)
3559 if (TREE_CODE (fns) == TREE_LIST)
3560 /* Member functions. */
3561 enforce_access (type, fn);
3562 return build_function_call (fn, tree_cons (NULL_TREE, addr, args));
3565 /* If we are doing placement delete we do nothing if we don't find a
3566 matching op delete. */
3567 if (placement)
3568 return NULL_TREE;
3570 /* Normal delete; now try to find a match including the size argument. */
3571 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3572 tree_cons (NULL_TREE, sizetype, void_list_node));
3573 fntype = build_function_type (void_type_node, argtypes);
3575 fn = instantiate_type (fntype, fns, 2);
3577 if (fn != error_mark_node)
3579 if (BASELINK_P (fns))
3580 /* Member functions. */
3581 enforce_access (type, fn);
3582 return build_function_call
3583 (fn, tree_cons (NULL_TREE, addr,
3584 build_tree_list (NULL_TREE, size)));
3587 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3588 destructor, in which case the error should be deferred
3589 until someone actually tries to delete one of these. */
3590 if (flags & LOOKUP_SPECULATIVELY)
3591 return NULL_TREE;
3593 cp_error ("no suitable `operator delete' for `%T'", type);
3594 return error_mark_node;
3597 /* If the current scope isn't allowed to access DECL along
3598 BASETYPE_PATH, give an error. The most derived class in
3599 BASETYPE_PATH is the one used to qualify DECL. */
3602 enforce_access (basetype_path, decl)
3603 tree basetype_path;
3604 tree decl;
3606 int accessible;
3608 accessible = accessible_p (basetype_path, decl);
3609 if (!accessible)
3611 if (TREE_PRIVATE (decl))
3612 cp_error_at ("`%+#D' is private", decl);
3613 else if (TREE_PROTECTED (decl))
3614 cp_error_at ("`%+#D' is protected", decl);
3615 else
3616 cp_error_at ("`%+#D' is inaccessible", decl);
3617 cp_error ("within this context");
3618 return 0;
3621 return 1;
3624 /* Perform the conversions in CONVS on the expression EXPR.
3625 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3626 indicates the `this' argument of a method. INNER is non-zero when
3627 being called to continue a conversion chain. */
3629 static tree
3630 convert_like_real (convs, expr, fn, argnum, inner)
3631 tree convs, expr;
3632 tree fn;
3633 int argnum;
3634 int inner;
3636 if (ICS_BAD_FLAG (convs)
3637 && TREE_CODE (convs) != USER_CONV
3638 && TREE_CODE (convs) != AMBIG_CONV
3639 && TREE_CODE (convs) != REF_BIND)
3641 tree t = convs;
3642 for (; t; t = TREE_OPERAND (t, 0))
3644 if (TREE_CODE (t) == USER_CONV)
3646 expr = convert_like_real (t, expr, fn, argnum, 1);
3647 break;
3649 else if (TREE_CODE (t) == AMBIG_CONV)
3650 return convert_like_real (t, expr, fn, argnum, 1);
3651 else if (TREE_CODE (t) == IDENTITY_CONV)
3652 break;
3654 return convert_for_initialization
3655 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3656 "conversion", fn, argnum);
3659 if (!inner)
3660 expr = dubious_conversion_warnings
3661 (TREE_TYPE (convs), expr, "argument", fn, argnum);
3662 switch (TREE_CODE (convs))
3664 case USER_CONV:
3666 struct z_candidate *cand
3667 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3668 tree fn = cand->fn;
3669 tree args;
3671 if (DECL_CONSTRUCTOR_P (fn))
3673 tree t = build_int_2 (0, 0);
3674 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3676 args = build_tree_list (NULL_TREE, expr);
3677 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3678 args = tree_cons (NULL_TREE, integer_one_node, args);
3679 args = tree_cons (NULL_TREE, t, args);
3681 else
3682 args = build_this (expr);
3683 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3685 /* If this is a constructor or a function returning an aggr type,
3686 we need to build up a TARGET_EXPR. */
3687 if (DECL_CONSTRUCTOR_P (fn))
3688 expr = build_cplus_new (TREE_TYPE (convs), expr);
3690 return expr;
3692 case IDENTITY_CONV:
3693 if (type_unknown_p (expr))
3694 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3695 return expr;
3696 case AMBIG_CONV:
3697 /* Call build_user_type_conversion again for the error. */
3698 return build_user_type_conversion
3699 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3701 default:
3702 break;
3705 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum, 1);
3706 if (expr == error_mark_node)
3707 return error_mark_node;
3709 /* Convert a non-array constant variable to its underlying value, unless we
3710 are about to bind it to a reference, in which case we need to
3711 leave it as an lvalue. */
3712 if (TREE_CODE (convs) != REF_BIND
3713 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3714 expr = decl_constant_value (expr);
3716 switch (TREE_CODE (convs))
3718 case RVALUE_CONV:
3719 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3720 return expr;
3721 /* else fall through */
3722 case BASE_CONV:
3723 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3725 /* We are going to bind a reference directly to a base-class
3726 subobject of EXPR. */
3727 tree base_ptr = build_pointer_type (TREE_TYPE (convs));
3729 /* Build an expression for `*((base*) &expr)'. */
3730 expr = build_unary_op (ADDR_EXPR, expr, 0);
3731 expr = perform_implicit_conversion (base_ptr, expr);
3732 expr = build_indirect_ref (expr, "implicit conversion");
3733 return expr;
3737 tree cvt_expr = build_user_type_conversion
3738 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3739 if (!cvt_expr)
3741 /* This can occur if, for example, the EXPR has incomplete
3742 type. We can't check for that before attempting the
3743 conversion because the type might be an incomplete
3744 array type, which is OK if some constructor for the
3745 destination type takes a pointer argument. */
3746 if (!COMPLETE_TYPE_P (TREE_TYPE (expr)))
3748 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
3749 incomplete_type_error (expr, TREE_TYPE (expr));
3750 else
3751 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3752 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3754 else
3755 cp_error ("could not convert `%E' to `%T'",
3756 expr, TREE_TYPE (convs));
3757 return error_mark_node;
3759 return cvt_expr;
3762 case REF_BIND:
3764 tree ref_type = TREE_TYPE (convs);
3766 /* If necessary, create a temporary. */
3767 if (NEED_TEMPORARY_P (convs))
3769 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3770 expr = build_target_expr_with_type (expr, type);
3773 /* Take the address of the thing to which we will bind the
3774 reference. */
3775 expr = build_unary_op (ADDR_EXPR, expr, 1);
3776 if (expr == error_mark_node)
3777 return error_mark_node;
3779 /* Convert it to a pointer to the type referred to by the
3780 reference. This will adjust the pointer if a derived to
3781 base conversion is being performed. */
3782 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3783 expr);
3784 /* Convert the pointer to the desired reference type. */
3785 expr = build1 (NOP_EXPR, ref_type, expr);
3787 return expr;
3790 case LVALUE_CONV:
3791 return decay_conversion (expr);
3793 case QUAL_CONV:
3794 /* Warn about deprecated conversion if appropriate. */
3795 string_conv_p (TREE_TYPE (convs), expr, 1);
3796 break;
3798 default:
3799 break;
3801 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3802 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3805 /* ARG is being passed to a varargs function. Perform any conversions
3806 required. Array/function to pointer decay must have already happened.
3807 Return the converted value. */
3809 tree
3810 convert_arg_to_ellipsis (arg)
3811 tree arg;
3813 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3814 && (TYPE_PRECISION (TREE_TYPE (arg))
3815 < TYPE_PRECISION (double_type_node)))
3816 /* Convert `float' to `double'. */
3817 arg = cp_convert (double_type_node, arg);
3818 else
3819 /* Convert `short' and `char' to full-size `int'. */
3820 arg = default_conversion (arg);
3822 arg = require_complete_type (arg);
3824 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
3826 /* Undefined behaviour [expr.call] 5.2.2/7. */
3827 cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3828 TREE_TYPE (arg));
3831 return arg;
3834 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
3836 tree
3837 build_x_va_arg (expr, type)
3838 tree expr;
3839 tree type;
3841 if (processing_template_decl)
3842 return build_min (VA_ARG_EXPR, type, expr);
3844 type = complete_type_or_else (type, NULL_TREE);
3846 if (expr == error_mark_node || !type)
3847 return error_mark_node;
3849 if (! pod_type_p (type))
3851 /* Undefined behaviour [expr.call] 5.2.2/7. */
3852 cp_warning ("cannot receive objects of non-POD type `%#T' through `...'",
3853 type);
3856 return build_va_arg (expr, type);
3859 /* TYPE has been given to va_arg. Apply the default conversions which would
3860 have happened when passed via ellipsis. Return the promoted type, or
3861 NULL_TREE, if there is no change. */
3863 tree
3864 convert_type_from_ellipsis (type)
3865 tree type;
3867 tree promote;
3869 if (TREE_CODE (type) == ARRAY_TYPE)
3870 promote = build_pointer_type (TREE_TYPE (type));
3871 else if (TREE_CODE (type) == FUNCTION_TYPE)
3872 promote = build_pointer_type (type);
3873 else
3874 promote = type_promotes_to (type);
3876 return same_type_p (type, promote) ? NULL_TREE : promote;
3879 /* ARG is a default argument expression being passed to a parameter of
3880 the indicated TYPE, which is a parameter to FN. Do any required
3881 conversions. Return the converted value. */
3883 tree
3884 convert_default_arg (type, arg, fn, parmnum)
3885 tree type;
3886 tree arg;
3887 tree fn;
3888 int parmnum;
3890 if (fn && DECL_TEMPLATE_INFO (fn))
3891 arg = tsubst_default_argument (fn, type, arg);
3893 arg = break_out_target_exprs (arg);
3895 if (TREE_CODE (arg) == CONSTRUCTOR)
3897 arg = digest_init (type, arg, 0);
3898 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3899 "default argument", fn, parmnum);
3901 else
3903 /* This could get clobbered by the following call. */
3904 if (TREE_HAS_CONSTRUCTOR (arg))
3905 arg = copy_node (arg);
3907 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3908 "default argument", fn, parmnum);
3909 if (PROMOTE_PROTOTYPES
3910 && (TREE_CODE (type) == INTEGER_TYPE
3911 || TREE_CODE (type) == ENUMERAL_TYPE)
3912 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3913 arg = default_conversion (arg);
3916 return arg;
3919 static tree
3920 build_over_call (cand, args, flags)
3921 struct z_candidate *cand;
3922 tree args;
3923 int flags;
3925 tree fn = cand->fn;
3926 tree convs = cand->convs;
3927 tree converted_args = NULL_TREE;
3928 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3929 tree conv, arg, val;
3930 int i = 0;
3931 int is_method = 0;
3933 /* Give any warnings we noticed during overload resolution. */
3934 if (cand->warnings)
3935 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3936 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3938 if (DECL_FUNCTION_MEMBER_P (fn))
3939 enforce_access (cand->basetype_path, fn);
3941 if (args && TREE_CODE (args) != TREE_LIST)
3942 args = build_tree_list (NULL_TREE, args);
3943 arg = args;
3945 /* The implicit parameters to a constructor are not considered by overload
3946 resolution, and must be of the proper type. */
3947 if (DECL_CONSTRUCTOR_P (fn))
3949 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3950 arg = TREE_CHAIN (arg);
3951 parm = TREE_CHAIN (parm);
3952 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3954 converted_args = tree_cons
3955 (NULL_TREE, TREE_VALUE (arg), converted_args);
3956 arg = TREE_CHAIN (arg);
3957 parm = TREE_CHAIN (parm);
3960 /* Bypass access control for 'this' parameter. */
3961 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3963 tree parmtype = TREE_VALUE (parm);
3964 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3965 tree t;
3966 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3967 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3968 TREE_TYPE (argtype), fn);
3970 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3971 X is called for an object that is not of type X, or of a type
3972 derived from X, the behavior is undefined.
3974 So we can assume that anything passed as 'this' is non-null, and
3975 optimize accordingly. */
3976 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
3977 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3978 converted_args = tree_cons (NULL_TREE, t, converted_args);
3979 parm = TREE_CHAIN (parm);
3980 arg = TREE_CHAIN (arg);
3981 ++i;
3982 is_method = 1;
3985 for (; arg && parm;
3986 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3988 tree type = TREE_VALUE (parm);
3990 conv = TREE_VEC_ELT (convs, i);
3991 if (ICS_BAD_FLAG (conv))
3993 tree t = conv;
3994 val = TREE_VALUE (arg);
3996 for (; t; t = TREE_OPERAND (t, 0))
3998 if (TREE_CODE (t) == USER_CONV
3999 || TREE_CODE (t) == AMBIG_CONV)
4001 val = convert_like_with_context (t, val, fn, i - is_method);
4002 break;
4004 else if (TREE_CODE (t) == IDENTITY_CONV)
4005 break;
4007 val = convert_for_initialization
4008 (NULL_TREE, type, val, LOOKUP_NORMAL,
4009 "argument", fn, i - is_method);
4011 else
4013 val = TREE_VALUE (arg);
4014 val = convert_like_with_context
4015 (conv, TREE_VALUE (arg), fn, i - is_method);
4018 if (PROMOTE_PROTOTYPES
4019 && (TREE_CODE (type) == INTEGER_TYPE
4020 || TREE_CODE (type) == ENUMERAL_TYPE)
4021 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4022 val = default_conversion (val);
4023 converted_args = tree_cons (NULL_TREE, val, converted_args);
4026 /* Default arguments */
4027 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4028 converted_args
4029 = tree_cons (NULL_TREE,
4030 convert_default_arg (TREE_VALUE (parm),
4031 TREE_PURPOSE (parm),
4032 fn, i - is_method),
4033 converted_args);
4035 /* Ellipsis */
4036 for (; arg; arg = TREE_CHAIN (arg))
4037 converted_args
4038 = tree_cons (NULL_TREE,
4039 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4040 converted_args);
4042 converted_args = nreverse (converted_args);
4044 if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
4045 check_function_format (DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
4046 converted_args);
4048 /* Avoid actually calling copy constructors and copy assignment operators,
4049 if possible. */
4051 if (! flag_elide_constructors)
4052 /* Do things the hard way. */;
4053 else if (TREE_VEC_LENGTH (convs) == 1
4054 && DECL_COPY_CONSTRUCTOR_P (fn))
4056 tree targ;
4057 arg = TREE_CHAIN (converted_args);
4058 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4059 arg = TREE_CHAIN (arg);
4060 arg = TREE_VALUE (arg);
4062 /* Pull out the real argument, disregarding const-correctness. */
4063 targ = arg;
4064 while (TREE_CODE (targ) == NOP_EXPR
4065 || TREE_CODE (targ) == NON_LVALUE_EXPR
4066 || TREE_CODE (targ) == CONVERT_EXPR)
4067 targ = TREE_OPERAND (targ, 0);
4068 if (TREE_CODE (targ) == ADDR_EXPR)
4070 targ = TREE_OPERAND (targ, 0);
4071 if (!same_type_ignoring_top_level_qualifiers_p
4072 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4073 targ = NULL_TREE;
4075 else
4076 targ = NULL_TREE;
4078 if (targ)
4079 arg = targ;
4080 else
4081 arg = build_indirect_ref (arg, 0);
4083 /* [class.copy]: the copy constructor is implicitly defined even if
4084 the implementation elided its use. */
4085 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4086 mark_used (fn);
4088 /* If we're creating a temp and we already have one, don't create a
4089 new one. If we're not creating a temp but we get one, use
4090 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4091 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4092 temp or an INIT_EXPR otherwise. */
4093 if (integer_zerop (TREE_VALUE (args)))
4095 if (! real_lvalue_p (arg))
4096 return arg;
4097 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4098 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4100 else if (! real_lvalue_p (arg)
4101 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4103 tree address;
4104 tree to = stabilize_reference
4105 (build_indirect_ref (TREE_VALUE (args), 0));
4107 /* If we're initializing an empty class, then we actually
4108 have to use a MODIFY_EXPR rather than an INIT_EXPR. The
4109 reason is that the dummy padding member in the target may
4110 not actually be allocated if TO is a base class
4111 subobject. Since we've set TYPE_NONCOPIED_PARTS on the
4112 padding, a MODIFY_EXPR will preserve its value, which is
4113 the right thing to do if it's not really padding at all.
4115 It's not safe to just throw away the ARG if we're looking
4116 at an empty class because the ARG might contain a
4117 TARGET_EXPR which wants to be bound to TO. If it is not,
4118 expand_expr will assign a dummy slot for the TARGET_EXPR,
4119 and we will call a destructor for it, which is wrong,
4120 because we will also destroy TO, but will never have
4121 constructed it. */
4122 val = build (is_empty_class (DECL_CONTEXT (fn))
4123 ? MODIFY_EXPR : INIT_EXPR,
4124 DECL_CONTEXT (fn), to, arg);
4125 address = build_unary_op (ADDR_EXPR, val, 0);
4126 /* Avoid a warning about this expression, if the address is
4127 never used. */
4128 TREE_USED (address) = 1;
4129 return address;
4132 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4133 && copy_args_p (fn)
4134 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4136 tree to = stabilize_reference
4137 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4139 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4141 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4142 return val;
4145 mark_used (fn);
4147 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4149 tree t, *p = &TREE_VALUE (converted_args);
4150 tree binfo = get_binfo
4151 (DECL_VIRTUAL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
4152 *p = convert_pointer_to_real (binfo, *p);
4153 if (TREE_SIDE_EFFECTS (*p))
4154 *p = save_expr (*p);
4155 t = build_pointer_type (TREE_TYPE (fn));
4156 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4157 TREE_TYPE (fn) = t;
4159 else if (DECL_INLINE (fn))
4160 fn = inline_conversion (fn);
4161 else
4162 fn = build_addr_func (fn);
4164 /* Recognize certain built-in functions so we can make tree-codes
4165 other than CALL_EXPR. We do this when it enables fold-const.c
4166 to do something useful. */
4168 if (TREE_CODE (fn) == ADDR_EXPR
4169 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4170 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4172 tree exp;
4173 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4174 if (exp)
4175 return exp;
4178 fn = build_call (fn, converted_args);
4179 if (TREE_CODE (TREE_TYPE (fn)) == VOID_TYPE)
4180 return fn;
4181 fn = require_complete_type (fn);
4182 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4183 fn = build_cplus_new (TREE_TYPE (fn), fn);
4184 return convert_from_reference (fn);
4187 /* Returns the value to use for the in-charge parameter when making a
4188 call to a function with the indicated NAME. */
4190 tree
4191 in_charge_arg_for_name (name)
4192 tree name;
4194 if (name == base_ctor_identifier
4195 || name == base_dtor_identifier)
4196 return integer_zero_node;
4197 else if (name == complete_ctor_identifier)
4198 return integer_one_node;
4199 else if (name == complete_dtor_identifier)
4200 return integer_two_node;
4201 else if (name == deleting_dtor_identifier)
4202 return integer_three_node;
4204 /* This function should only be called with one of the names listed
4205 above. */
4206 my_friendly_abort (20000411);
4207 return NULL_TREE;
4210 static tree
4211 build_new_method_call (instance, name, args, basetype_path, flags)
4212 tree instance, name, args, basetype_path;
4213 int flags;
4215 struct z_candidate *candidates = 0, *cand;
4216 tree explicit_targs = NULL_TREE;
4217 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4218 tree pretty_name;
4219 tree user_args;
4220 tree templates = NULL_TREE;
4221 int template_only = 0;
4223 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4225 explicit_targs = TREE_OPERAND (name, 1);
4226 name = TREE_OPERAND (name, 0);
4227 if (DECL_P (name))
4228 name = DECL_NAME (name);
4229 else
4231 if (TREE_CODE (name) == COMPONENT_REF)
4232 name = TREE_OPERAND (name, 1);
4233 if (TREE_CODE (name) == OVERLOAD)
4234 name = DECL_NAME (OVL_CURRENT (name));
4237 template_only = 1;
4240 user_args = args;
4241 args = resolve_args (args);
4243 if (args == error_mark_node)
4244 return error_mark_node;
4246 if (instance == NULL_TREE)
4247 basetype = BINFO_TYPE (basetype_path);
4248 else
4250 if (TREE_CODE (instance) == OFFSET_REF)
4251 instance = resolve_offset_ref (instance);
4252 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4253 instance = convert_from_reference (instance);
4254 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4256 /* XXX this should be handled before we get here. */
4257 if (! IS_AGGR_TYPE (basetype))
4259 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4260 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4261 name, instance, basetype);
4263 return error_mark_node;
4267 if (basetype_path == NULL_TREE)
4268 basetype_path = TYPE_BINFO (basetype);
4270 if (instance)
4272 instance_ptr = build_this (instance);
4274 if (! template_only)
4276 /* XXX this should be handled before we get here. */
4277 fns = build_field_call (basetype_path, instance_ptr, name, args);
4278 if (fns)
4279 return fns;
4282 else
4284 instance_ptr = build_int_2 (0, 0);
4285 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4288 /* Callers should explicitly indicate whether they want to construct
4289 the complete object or just the part without virtual bases. */
4290 my_friendly_assert (name != ctor_identifier, 20000408);
4291 /* Similarly for destructors. */
4292 my_friendly_assert (name != dtor_identifier, 20000408);
4294 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4296 int constructor_p;
4298 constructor_p = (name == complete_ctor_identifier
4299 || name == base_ctor_identifier);
4300 pretty_name = (constructor_p
4301 ? constructor_name (basetype) : dtor_identifier);
4303 if (!flag_new_abi)
4305 /* Add the in-charge parameter as an implicit first argument. */
4306 if (!constructor_p
4307 || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4308 args = tree_cons (NULL_TREE,
4309 in_charge_arg_for_name (name),
4310 args);
4312 /* We want to call the normal constructor function under the
4313 old ABI. */
4314 name = constructor_p ? ctor_identifier : dtor_identifier;
4316 /* If we're call a subobject constructor or destructor for a
4317 subobject that uses virtual base classes, then we need to
4318 pass down a pointer to a VTT for the subobject. */
4319 else if ((name == base_ctor_identifier
4320 || name == base_dtor_identifier)
4321 && TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4323 tree vtt;
4324 tree sub_vtt;
4326 /* If the current function is a complete object constructor
4327 or destructor, then we fetch the VTT directly.
4328 Otherwise, we look it up using the VTT we were given. */
4329 vtt = IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type));
4330 vtt = build_unary_op (ADDR_EXPR, vtt, /*noconvert=*/1);
4331 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4332 DECL_USE_VTT_PARM (current_function_decl),
4333 DECL_VTT_PARM (current_function_decl),
4334 vtt);
4336 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4337 BINFO_SUBVTT_INDEX (basetype_path));
4338 sub_vtt = build_indirect_ref (sub_vtt, NULL);
4340 args = tree_cons (NULL_TREE, sub_vtt, args);
4343 else
4344 pretty_name = name;
4346 fns = lookup_fnfields (basetype_path, name, 1);
4348 if (fns == error_mark_node)
4349 return error_mark_node;
4350 if (fns)
4352 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
4353 tree fn = TREE_VALUE (fns);
4354 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4355 for (; fn; fn = OVL_NEXT (fn))
4357 tree t = OVL_CURRENT (fn);
4358 tree this_arglist;
4360 /* We can end up here for copy-init of same or base class. */
4361 if ((flags & LOOKUP_ONLYCONVERTING)
4362 && DECL_NONCONVERTING_P (t))
4363 continue;
4365 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4366 this_arglist = mem_args;
4367 else
4368 this_arglist = args;
4370 if (TREE_CODE (t) == TEMPLATE_DECL)
4372 /* A member template. */
4373 templates = tree_cons (NULL_TREE, t, templates);
4374 candidates =
4375 add_template_candidate (candidates, t, base, explicit_targs,
4376 this_arglist,
4377 TREE_TYPE (name), flags, DEDUCE_CALL);
4379 else if (! template_only)
4380 candidates = add_function_candidate (candidates, t, base,
4381 this_arglist, flags);
4383 if (candidates)
4384 candidates->basetype_path = basetype_path;
4388 if (! any_viable (candidates))
4390 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4391 if (flags & LOOKUP_SPECULATIVELY)
4392 return NULL_TREE;
4393 if (!COMPLETE_TYPE_P (basetype))
4394 incomplete_type_error (instance_ptr, basetype);
4395 else
4396 cp_error ("no matching function for call to `%T::%D (%A)%V'",
4397 basetype, pretty_name, user_args,
4398 TREE_TYPE (TREE_TYPE (instance_ptr)));
4399 print_z_candidates (candidates);
4400 return error_mark_node;
4402 candidates = splice_viable (candidates);
4403 cand = tourney (candidates);
4405 if (cand == 0)
4407 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4408 user_args);
4409 print_z_candidates (candidates);
4410 return error_mark_node;
4413 if (DECL_PURE_VIRTUAL_P (cand->fn)
4414 && instance == current_class_ref
4415 && (DECL_CONSTRUCTOR_P (current_function_decl)
4416 || DECL_DESTRUCTOR_P (current_function_decl))
4417 && ! (flags & LOOKUP_NONVIRTUAL)
4418 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4419 cp_error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4420 "abstract virtual `%#D' called from constructor"
4421 : "abstract virtual `%#D' called from destructor"),
4422 cand->fn);
4423 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4424 && is_dummy_object (instance_ptr))
4426 cp_error ("cannot call member function `%D' without object", cand->fn);
4427 return error_mark_node;
4430 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4431 && ((instance == current_class_ref && (dtor_label || ctor_label))
4432 || resolves_to_fixed_type_p (instance, 0)))
4433 flags |= LOOKUP_NONVIRTUAL;
4435 return build_over_call
4436 (cand,
4437 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
4438 flags);
4441 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4442 subsequence of ICS2. */
4444 static int
4445 is_subseq (ics1, ics2)
4446 tree ics1, ics2;
4448 /* We can assume that a conversion of the same code
4449 between the same types indicates a subsequence since we only get
4450 here if the types we are converting from are the same. */
4452 while (TREE_CODE (ics1) == RVALUE_CONV
4453 || TREE_CODE (ics1) == LVALUE_CONV)
4454 ics1 = TREE_OPERAND (ics1, 0);
4456 while (1)
4458 while (TREE_CODE (ics2) == RVALUE_CONV
4459 || TREE_CODE (ics2) == LVALUE_CONV)
4460 ics2 = TREE_OPERAND (ics2, 0);
4462 if (TREE_CODE (ics2) == USER_CONV
4463 || TREE_CODE (ics2) == AMBIG_CONV
4464 || TREE_CODE (ics2) == IDENTITY_CONV)
4465 /* At this point, ICS1 cannot be a proper subsequence of
4466 ICS2. We can get a USER_CONV when we are comparing the
4467 second standard conversion sequence of two user conversion
4468 sequences. */
4469 return 0;
4471 ics2 = TREE_OPERAND (ics2, 0);
4473 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4474 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4475 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4476 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4477 return 1;
4481 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4482 be any _TYPE nodes. */
4485 is_properly_derived_from (derived, base)
4486 tree derived;
4487 tree base;
4489 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4490 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4491 return 0;
4493 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4494 considers every class derived from itself. */
4495 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4496 && DERIVED_FROM_P (base, derived));
4499 /* We build the ICS for an implicit object parameter as a pointer
4500 conversion sequence. However, such a sequence should be compared
4501 as if it were a reference conversion sequence. If ICS is the
4502 implicit conversion sequence for an implicit object parameter,
4503 modify it accordingly. */
4505 static void
4506 maybe_handle_implicit_object (ics)
4507 tree* ics;
4509 if (ICS_THIS_FLAG (*ics))
4511 /* [over.match.funcs]
4513 For non-static member functions, the type of the
4514 implicit object parameter is "reference to cv X"
4515 where X is the class of which the function is a
4516 member and cv is the cv-qualification on the member
4517 function declaration. */
4518 tree t = *ics;
4519 tree reference_type;
4521 /* The `this' parameter is a pointer to a class type. Make the
4522 implict conversion talk about a reference to that same class
4523 type. */
4524 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4525 reference_type = build_reference_type (reference_type);
4527 if (TREE_CODE (t) == QUAL_CONV)
4528 t = TREE_OPERAND (t, 0);
4529 if (TREE_CODE (t) == PTR_CONV)
4530 t = TREE_OPERAND (t, 0);
4531 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4532 t = direct_reference_binding (reference_type, t);
4533 *ics = t;
4537 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
4538 to the type the reference originally referred to, and return 1.
4539 Otherwise, return 0. */
4541 static int
4542 maybe_handle_ref_bind (ics, target_type)
4543 tree* ics;
4544 tree* target_type;
4546 if (TREE_CODE (*ics) == REF_BIND)
4548 *target_type = TREE_TYPE (TREE_TYPE (*ics));
4549 *ics = TREE_OPERAND (*ics, 0);
4550 return 1;
4553 return 0;
4556 /* Compare two implicit conversion sequences according to the rules set out in
4557 [over.ics.rank]. Return values:
4559 1: ics1 is better than ics2
4560 -1: ics2 is better than ics1
4561 0: ics1 and ics2 are indistinguishable */
4563 static int
4564 compare_ics (ics1, ics2)
4565 tree ics1, ics2;
4567 tree from_type1;
4568 tree from_type2;
4569 tree to_type1;
4570 tree to_type2;
4571 tree deref_from_type1 = NULL_TREE;
4572 tree deref_from_type2 = NULL_TREE;
4573 tree deref_to_type1 = NULL_TREE;
4574 tree deref_to_type2 = NULL_TREE;
4576 /* REF_BINDING is non-zero if the result of the conversion sequence
4577 is a reference type. In that case TARGET_TYPE is the
4578 type referred to by the reference. */
4579 int ref_binding1;
4580 int ref_binding2;
4581 tree target_type1;
4582 tree target_type2;
4584 /* Handle implicit object parameters. */
4585 maybe_handle_implicit_object (&ics1);
4586 maybe_handle_implicit_object (&ics2);
4588 /* Handle reference parameters. */
4589 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
4590 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
4592 /* [over.ics.rank]
4594 When comparing the basic forms of implicit conversion sequences (as
4595 defined in _over.best.ics_)
4597 --a standard conversion sequence (_over.ics.scs_) is a better
4598 conversion sequence than a user-defined conversion sequence
4599 or an ellipsis conversion sequence, and
4601 --a user-defined conversion sequence (_over.ics.user_) is a
4602 better conversion sequence than an ellipsis conversion sequence
4603 (_over.ics.ellipsis_). */
4604 if (ICS_RANK (ics1) > ICS_RANK (ics2))
4605 return -1;
4606 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
4607 return 1;
4609 if (ICS_RANK (ics1) == BAD_RANK)
4611 /* Both ICS are bad. We try to make a decision based on what
4612 would have happenned if they'd been good. */
4613 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4614 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4615 return -1;
4616 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4617 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4618 return 1;
4620 /* We couldn't make up our minds; try to figure it out below. */
4623 if (ICS_ELLIPSIS_FLAG (ics1))
4624 /* Both conversions are ellipsis conversions. */
4625 return 0;
4627 /* User-defined conversion sequence U1 is a better conversion sequence
4628 than another user-defined conversion sequence U2 if they contain the
4629 same user-defined conversion operator or constructor and if the sec-
4630 ond standard conversion sequence of U1 is better than the second
4631 standard conversion sequence of U2. */
4633 if (ICS_USER_FLAG (ics1))
4635 tree t1, t2;
4637 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4638 if (TREE_CODE (t1) == AMBIG_CONV)
4639 return 0;
4640 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4641 if (TREE_CODE (t2) == AMBIG_CONV)
4642 return 0;
4644 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4645 return 0;
4647 /* We can just fall through here, after setting up
4648 FROM_TYPE1 and FROM_TYPE2. */
4649 from_type1 = TREE_TYPE (t1);
4650 from_type2 = TREE_TYPE (t2);
4652 else
4654 /* We're dealing with two standard conversion sequences.
4656 [over.ics.rank]
4658 Standard conversion sequence S1 is a better conversion
4659 sequence than standard conversion sequence S2 if
4661 --S1 is a proper subsequence of S2 (comparing the conversion
4662 sequences in the canonical form defined by _over.ics.scs_,
4663 excluding any Lvalue Transformation; the identity
4664 conversion sequence is considered to be a subsequence of
4665 any non-identity conversion sequence */
4667 from_type1 = ics1;
4668 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4669 from_type1 = TREE_OPERAND (from_type1, 0);
4670 from_type1 = TREE_TYPE (from_type1);
4672 from_type2 = ics2;
4673 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4674 from_type2 = TREE_OPERAND (from_type2, 0);
4675 from_type2 = TREE_TYPE (from_type2);
4678 if (same_type_p (from_type1, from_type2))
4680 if (is_subseq (ics1, ics2))
4681 return 1;
4682 if (is_subseq (ics2, ics1))
4683 return -1;
4685 /* Otherwise, one sequence cannot be a subsequence of the other; they
4686 don't start with the same type. This can happen when comparing the
4687 second standard conversion sequence in two user-defined conversion
4688 sequences. */
4690 /* [over.ics.rank]
4692 Or, if not that,
4694 --the rank of S1 is better than the rank of S2 (by the rules
4695 defined below):
4697 Standard conversion sequences are ordered by their ranks: an Exact
4698 Match is a better conversion than a Promotion, which is a better
4699 conversion than a Conversion.
4701 Two conversion sequences with the same rank are indistinguishable
4702 unless one of the following rules applies:
4704 --A conversion that is not a conversion of a pointer, or pointer
4705 to member, to bool is better than another conversion that is such
4706 a conversion.
4708 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4709 so that we do not have to check it explicitly. */
4710 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4711 return 1;
4712 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4713 return -1;
4715 to_type1 = TREE_TYPE (ics1);
4716 to_type2 = TREE_TYPE (ics2);
4718 if (TYPE_PTR_P (from_type1)
4719 && TYPE_PTR_P (from_type2)
4720 && TYPE_PTR_P (to_type1)
4721 && TYPE_PTR_P (to_type2))
4723 deref_from_type1 = TREE_TYPE (from_type1);
4724 deref_from_type2 = TREE_TYPE (from_type2);
4725 deref_to_type1 = TREE_TYPE (to_type1);
4726 deref_to_type2 = TREE_TYPE (to_type2);
4728 /* The rules for pointers to members A::* are just like the rules
4729 for pointers A*, except opposite: if B is derived from A then
4730 A::* converts to B::*, not vice versa. For that reason, we
4731 switch the from_ and to_ variables here. */
4732 else if (TYPE_PTRMEM_P (from_type1)
4733 && TYPE_PTRMEM_P (from_type2)
4734 && TYPE_PTRMEM_P (to_type1)
4735 && TYPE_PTRMEM_P (to_type2))
4737 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4738 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4739 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4740 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4742 else if (TYPE_PTRMEMFUNC_P (from_type1)
4743 && TYPE_PTRMEMFUNC_P (from_type2)
4744 && TYPE_PTRMEMFUNC_P (to_type1)
4745 && TYPE_PTRMEMFUNC_P (to_type2))
4747 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4748 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4749 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4750 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4753 if (deref_from_type1 != NULL_TREE
4754 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4755 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4757 /* This was one of the pointer or pointer-like conversions.
4759 [over.ics.rank]
4761 --If class B is derived directly or indirectly from class A,
4762 conversion of B* to A* is better than conversion of B* to
4763 void*, and conversion of A* to void* is better than
4764 conversion of B* to void*. */
4765 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4766 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4768 if (is_properly_derived_from (deref_from_type1,
4769 deref_from_type2))
4770 return -1;
4771 else if (is_properly_derived_from (deref_from_type2,
4772 deref_from_type1))
4773 return 1;
4775 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4776 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4778 if (same_type_p (deref_from_type1, deref_from_type2))
4780 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4782 if (is_properly_derived_from (deref_from_type1,
4783 deref_to_type1))
4784 return 1;
4786 /* We know that DEREF_TO_TYPE1 is `void' here. */
4787 else if (is_properly_derived_from (deref_from_type1,
4788 deref_to_type2))
4789 return -1;
4792 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4793 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4795 /* [over.ics.rank]
4797 --If class B is derived directly or indirectly from class A
4798 and class C is derived directly or indirectly from B,
4800 --conversion of C* to B* is better than conversion of C* to
4801 A*,
4803 --conversion of B* to A* is better than conversion of C* to
4804 A* */
4805 if (same_type_p (deref_from_type1, deref_from_type2))
4807 if (is_properly_derived_from (deref_to_type1,
4808 deref_to_type2))
4809 return 1;
4810 else if (is_properly_derived_from (deref_to_type2,
4811 deref_to_type1))
4812 return -1;
4814 else if (same_type_p (deref_to_type1, deref_to_type2))
4816 if (is_properly_derived_from (deref_from_type2,
4817 deref_from_type1))
4818 return 1;
4819 else if (is_properly_derived_from (deref_from_type1,
4820 deref_from_type2))
4821 return -1;
4825 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4826 && same_type_p (from_type1, from_type2))
4828 /* [over.ics.rank]
4830 --binding of an expression of type C to a reference of type
4831 B& is better than binding an expression of type C to a
4832 reference of type A&
4834 --conversion of C to B is better than conversion of C to A, */
4835 if (is_properly_derived_from (from_type1, to_type1)
4836 && is_properly_derived_from (from_type1, to_type2))
4838 if (is_properly_derived_from (to_type1, to_type2))
4839 return 1;
4840 else if (is_properly_derived_from (to_type2, to_type1))
4841 return -1;
4844 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4845 && same_type_p (to_type1, to_type2))
4847 /* [over.ics.rank]
4849 --binding of an expression of type B to a reference of type
4850 A& is better than binding an expression of type C to a
4851 reference of type A&,
4853 --onversion of B to A is better than conversion of C to A */
4854 if (is_properly_derived_from (from_type1, to_type1)
4855 && is_properly_derived_from (from_type2, to_type1))
4857 if (is_properly_derived_from (from_type2, from_type1))
4858 return 1;
4859 else if (is_properly_derived_from (from_type1, from_type2))
4860 return -1;
4864 /* [over.ics.rank]
4866 --S1 and S2 differ only in their qualification conversion and yield
4867 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4868 qualification signature of type T1 is a proper subset of the cv-
4869 qualification signature of type T2 */
4870 if (TREE_CODE (ics1) == QUAL_CONV
4871 && TREE_CODE (ics2) == QUAL_CONV
4872 && same_type_p (from_type1, from_type2))
4873 return comp_cv_qual_signature (to_type1, to_type2);
4875 /* [over.ics.rank]
4877 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4878 types to which the references refer are the same type except for
4879 top-level cv-qualifiers, and the type to which the reference
4880 initialized by S2 refers is more cv-qualified than the type to
4881 which the reference initialized by S1 refers */
4883 if (ref_binding1 && ref_binding2
4884 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
4885 return comp_cv_qualification (target_type2, target_type1);
4887 /* Neither conversion sequence is better than the other. */
4888 return 0;
4891 /* The source type for this standard conversion sequence. */
4893 static tree
4894 source_type (t)
4895 tree t;
4897 for (;; t = TREE_OPERAND (t, 0))
4899 if (TREE_CODE (t) == USER_CONV
4900 || TREE_CODE (t) == AMBIG_CONV
4901 || TREE_CODE (t) == IDENTITY_CONV)
4902 return TREE_TYPE (t);
4904 my_friendly_abort (1823);
4907 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4908 a pointer to LOSER and re-running joust to produce the warning if WINNER
4909 is actually used. */
4911 static void
4912 add_warning (winner, loser)
4913 struct z_candidate *winner, *loser;
4915 winner->warnings = tree_cons (NULL_PTR,
4916 build_expr_ptr_wrapper (loser),
4917 winner->warnings);
4920 /* Returns true iff functions are equivalent. Equivalent functions are
4921 not identical only if one is a function-local extern function. */
4923 static inline int
4924 equal_functions (fn1, fn2)
4925 tree fn1;
4926 tree fn2;
4928 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2))
4929 return decls_match (fn1, fn2);
4930 return fn1 == fn2;
4933 /* Compare two candidates for overloading as described in
4934 [over.match.best]. Return values:
4936 1: cand1 is better than cand2
4937 -1: cand2 is better than cand1
4938 0: cand1 and cand2 are indistinguishable */
4940 static int
4941 joust (cand1, cand2, warn)
4942 struct z_candidate *cand1, *cand2;
4943 int warn;
4945 int winner = 0;
4946 int i, off1 = 0, off2 = 0, len;
4948 /* Candidates that involve bad conversions are always worse than those
4949 that don't. */
4950 if (cand1->viable > cand2->viable)
4951 return 1;
4952 if (cand1->viable < cand2->viable)
4953 return -1;
4955 /* If we have two pseudo-candidates for conversions to the same type,
4956 or two candidates for the same function, arbitrarily pick one. */
4957 if (cand1->fn == cand2->fn
4958 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
4959 return 1;
4961 /* a viable function F1
4962 is defined to be a better function than another viable function F2 if
4963 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4964 ICSi(F2), and then */
4966 /* for some argument j, ICSj(F1) is a better conversion sequence than
4967 ICSj(F2) */
4969 /* For comparing static and non-static member functions, we ignore
4970 the implicit object parameter of the non-static function. The
4971 standard says to pretend that the static function has an object
4972 parm, but that won't work with operator overloading. */
4973 len = TREE_VEC_LENGTH (cand1->convs);
4974 if (len != TREE_VEC_LENGTH (cand2->convs))
4976 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4977 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4978 off2 = 1;
4979 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4980 && DECL_STATIC_FUNCTION_P (cand2->fn))
4982 off1 = 1;
4983 --len;
4985 else
4986 my_friendly_abort (42);
4989 for (i = 0; i < len; ++i)
4991 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4992 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4993 int comp = compare_ics (t1, t2);
4995 if (comp != 0)
4997 if (warn_sign_promo
4998 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4999 && TREE_CODE (t1) == STD_CONV
5000 && TREE_CODE (t2) == STD_CONV
5001 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5002 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5003 && (TYPE_PRECISION (TREE_TYPE (t1))
5004 == TYPE_PRECISION (TREE_TYPE (t2)))
5005 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5006 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5007 == ENUMERAL_TYPE)))
5009 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5010 tree type1, type2;
5011 struct z_candidate *w, *l;
5012 if (comp > 0)
5013 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5014 w = cand1, l = cand2;
5015 else
5016 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5017 w = cand2, l = cand1;
5019 if (warn)
5021 cp_warning ("passing `%T' chooses `%T' over `%T'",
5022 type, type1, type2);
5023 cp_warning (" in call to `%D'", w->fn);
5025 else
5026 add_warning (w, l);
5029 if (winner && comp != winner)
5031 winner = 0;
5032 goto tweak;
5034 winner = comp;
5038 /* warn about confusing overload resolution for user-defined conversions,
5039 either between a constructor and a conversion op, or between two
5040 conversion ops. */
5041 if (winner && cand1->second_conv
5042 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5043 != DECL_CONSTRUCTOR_P (cand2->fn))
5044 /* Don't warn if the two conv ops convert to the same type... */
5045 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5046 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5047 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5049 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5050 if (comp != winner)
5052 struct z_candidate *w, *l;
5053 if (winner == 1)
5054 w = cand1, l = cand2;
5055 else
5056 w = cand2, l = cand1;
5057 if (warn)
5059 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5060 if (! DECL_CONSTRUCTOR_P (w->fn))
5061 source = TREE_TYPE (source);
5062 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
5063 cp_warning (" for conversion from `%T' to `%T'",
5064 source, TREE_TYPE (w->second_conv));
5065 cp_warning (" because conversion sequence for the argument is better");
5067 else
5068 add_warning (w, l);
5072 if (winner)
5073 return winner;
5075 /* or, if not that,
5076 F1 is a non-template function and F2 is a template function */
5078 if (! cand1->template && cand2->template)
5079 return 1;
5080 else if (cand1->template && ! cand2->template)
5081 return -1;
5082 else if (cand1->template && cand2->template)
5083 winner = more_specialized
5084 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5085 NULL_TREE);
5087 /* or, if not that,
5088 the context is an initialization by user-defined conversion (see
5089 _dcl.init_ and _over.match.user_) and the standard conversion
5090 sequence from the return type of F1 to the destination type (i.e.,
5091 the type of the entity being initialized) is a better conversion
5092 sequence than the standard conversion sequence from the return type
5093 of F2 to the destination type. */
5095 if (! winner && cand1->second_conv)
5096 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5098 /* If the built-in candidates are the same, arbitrarily pick one. */
5099 if (! winner && cand1->fn == cand2->fn
5100 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5102 for (i = 0; i < len; ++i)
5103 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5104 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5105 break;
5106 if (i == TREE_VEC_LENGTH (cand1->convs))
5107 return 1;
5109 /* Kludge around broken overloading rules whereby
5110 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
5111 that takes references and another that takes values. */
5112 if (cand1->fn == ansi_opname (COND_EXPR))
5114 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
5115 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
5116 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
5117 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
5119 if (same_type_p (t1, t2))
5121 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
5122 return 1;
5123 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
5124 return -1;
5129 /* If the two functions are the same (this can happen with declarations
5130 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5131 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5132 && equal_functions (cand1->fn, cand2->fn))
5133 return 1;
5135 tweak:
5137 /* Extension: If the worst conversion for one candidate is worse than the
5138 worst conversion for the other, take the first. */
5139 if (! winner && ! pedantic)
5141 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5143 for (i = 0; i < len; ++i)
5145 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5146 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5147 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5148 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5151 if (rank1 < rank2)
5152 return 1;
5153 if (rank1 > rank2)
5154 return -1;
5157 return winner;
5160 /* Given a list of candidates for overloading, find the best one, if any.
5161 This algorithm has a worst case of O(2n) (winner is last), and a best
5162 case of O(n/2) (totally ambiguous); much better than a sorting
5163 algorithm. */
5165 static struct z_candidate *
5166 tourney (candidates)
5167 struct z_candidate *candidates;
5169 struct z_candidate *champ = candidates, *challenger;
5170 int fate;
5171 int champ_compared_to_predecessor = 0;
5173 /* Walk through the list once, comparing each current champ to the next
5174 candidate, knocking out a candidate or two with each comparison. */
5176 for (challenger = champ->next; challenger; )
5178 fate = joust (champ, challenger, 0);
5179 if (fate == 1)
5180 challenger = challenger->next;
5181 else
5183 if (fate == 0)
5185 champ = challenger->next;
5186 if (champ == 0)
5187 return 0;
5188 champ_compared_to_predecessor = 0;
5190 else
5192 champ = challenger;
5193 champ_compared_to_predecessor = 1;
5196 challenger = champ->next;
5200 /* Make sure the champ is better than all the candidates it hasn't yet
5201 been compared to. */
5203 for (challenger = candidates;
5204 challenger != champ
5205 && !(champ_compared_to_predecessor && challenger->next == champ);
5206 challenger = challenger->next)
5208 fate = joust (champ, challenger, 0);
5209 if (fate != 1)
5210 return 0;
5213 return champ;
5216 /* Returns non-zero if things of type FROM can be converted to TO. */
5219 can_convert (to, from)
5220 tree to, from;
5222 return can_convert_arg (to, from, NULL_TREE);
5225 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5228 can_convert_arg (to, from, arg)
5229 tree to, from, arg;
5231 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5232 return (t && ! ICS_BAD_FLAG (t));
5235 /* Convert EXPR to TYPE. Return the converted expression. */
5237 tree
5238 perform_implicit_conversion (type, expr)
5239 tree type;
5240 tree expr;
5242 tree conv;
5244 if (expr == error_mark_node)
5245 return error_mark_node;
5246 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5247 LOOKUP_NORMAL);
5248 if (!conv || ICS_BAD_FLAG (conv))
5250 cp_error ("could not convert `%E' to `%T'", expr, type);
5251 return error_mark_node;
5254 return convert_like (conv, expr);
5257 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5258 initializing a variable of that TYPE. Return the converted
5259 expression. */
5261 tree
5262 initialize_reference (type, expr)
5263 tree type;
5264 tree expr;
5266 tree conv;
5268 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5269 if (!conv || ICS_BAD_FLAG (conv))
5271 cp_error ("could not convert `%E' to `%T'", expr, type);
5272 return error_mark_node;
5275 return convert_like (conv, expr);