* call.c (joust): Fix handling of overloaded builtin operators.
[official-gcc.git] / gcc / cp / call.c
blobacb5cfeced32638b4af871369fd4ea2790796fce
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_obj (sizeof (struct z_candidate), 1);
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 struct z_candidate *cand;
2145 int i;
2146 tree fn;
2148 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
2149 return_type, strict);
2151 if (i != 0)
2152 return candidates;
2154 fn = instantiate_template (tmpl, targs);
2155 if (fn == error_mark_node)
2156 return candidates;
2158 if (obj != NULL_TREE)
2159 /* Aha, this is a conversion function. */
2160 cand = add_conv_candidate (candidates, fn, obj, arglist);
2161 else
2162 cand = add_function_candidate (candidates, fn, ctype,
2163 arglist, flags);
2164 if (DECL_TI_TEMPLATE (fn) != tmpl)
2165 /* This situation can occur if a member template of a template
2166 class is specialized. Then, instantiate_template might return
2167 an instantiation of the specialization, in which case the
2168 DECL_TI_TEMPLATE field will point at the original
2169 specialization. For example:
2171 template <class T> struct S { template <class U> void f(U);
2172 template <> void f(int) {}; };
2173 S<double> sd;
2174 sd.f(3);
2176 Here, TMPL will be template <class U> S<double>::f(U).
2177 And, instantiate template will give us the specialization
2178 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2179 for this will point at template <class T> template <> S<T>::f(int),
2180 so that we can find the definition. For the purposes of
2181 overload resolution, however, we want the original TMPL. */
2182 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2183 else
2184 cand->template = DECL_TEMPLATE_INFO (fn);
2186 return cand;
2190 static struct z_candidate *
2191 add_template_candidate (candidates, tmpl, ctype, explicit_targs,
2192 arglist, return_type, flags, strict)
2193 struct z_candidate *candidates;
2194 tree tmpl, ctype, explicit_targs, arglist, return_type;
2195 int flags;
2196 unification_kind_t strict;
2198 return
2199 add_template_candidate_real (candidates, tmpl, ctype,
2200 explicit_targs, arglist, return_type, flags,
2201 NULL_TREE, strict);
2205 static struct z_candidate *
2206 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2207 struct z_candidate *candidates;
2208 tree tmpl, obj, arglist, return_type;
2210 return
2211 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2212 arglist, return_type, 0, obj, DEDUCE_CONV);
2216 static int
2217 any_viable (cands)
2218 struct z_candidate *cands;
2220 for (; cands; cands = cands->next)
2221 if (pedantic ? cands->viable == 1 : cands->viable)
2222 return 1;
2223 return 0;
2226 static struct z_candidate *
2227 splice_viable (cands)
2228 struct z_candidate *cands;
2230 struct z_candidate **p = &cands;
2232 for (; *p; )
2234 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2235 p = &((*p)->next);
2236 else
2237 *p = (*p)->next;
2240 return cands;
2243 static tree
2244 build_this (obj)
2245 tree obj;
2247 /* Fix this to work on non-lvalues. */
2248 return build_unary_op (ADDR_EXPR, obj, 0);
2251 static void
2252 print_z_candidates (candidates)
2253 struct z_candidate *candidates;
2255 const char *str = "candidates are:";
2256 for (; candidates; candidates = candidates->next)
2258 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2260 if (TREE_VEC_LENGTH (candidates->convs) == 3)
2261 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2262 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2263 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2264 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2265 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2266 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2267 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2268 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2269 else
2270 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2271 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2273 else if (TYPE_P (candidates->fn))
2274 cp_error ("%s %T <conversion>", str, candidates->fn);
2275 else
2276 cp_error_at ("%s %+#D%s", str, candidates->fn,
2277 candidates->viable == -1 ? " <near match>" : "");
2278 str = " ";
2282 /* Returns the best overload candidate to perform the requested
2283 conversion. This function is used for three the overloading situations
2284 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2285 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2286 per [dcl.init.ref], so we ignore temporary bindings. */
2288 static struct z_candidate *
2289 build_user_type_conversion_1 (totype, expr, flags)
2290 tree totype, expr;
2291 int flags;
2293 struct z_candidate *candidates, *cand;
2294 tree fromtype = TREE_TYPE (expr);
2295 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2296 tree args = NULL_TREE;
2297 tree templates = NULL_TREE;
2299 if (IS_AGGR_TYPE (totype))
2300 ctors = lookup_fnfields (TYPE_BINFO (totype),
2301 (flag_new_abi
2302 ? complete_ctor_identifier
2303 : ctor_identifier),
2306 if (IS_AGGR_TYPE (fromtype)
2307 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2308 convs = lookup_conversions (fromtype);
2310 candidates = 0;
2311 flags |= LOOKUP_NO_CONVERSION;
2313 if (ctors)
2315 tree t;
2317 ctors = TREE_VALUE (ctors);
2319 t = build_int_2 (0, 0);
2320 TREE_TYPE (t) = build_pointer_type (totype);
2321 args = build_tree_list (NULL_TREE, expr);
2322 if (DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors)))
2323 args = tree_cons (NULL_TREE, integer_one_node, args);
2324 args = tree_cons (NULL_TREE, t, args);
2326 for (; ctors; ctors = OVL_NEXT (ctors))
2328 tree ctor = OVL_CURRENT (ctors);
2329 if (DECL_NONCONVERTING_P (ctor))
2330 continue;
2332 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2334 templates = tree_cons (NULL_TREE, ctor, templates);
2335 candidates =
2336 add_template_candidate (candidates, ctor, totype,
2337 NULL_TREE, args, NULL_TREE, flags,
2338 DEDUCE_CALL);
2340 else
2341 candidates = add_function_candidate (candidates, ctor, totype,
2342 args, flags);
2344 if (candidates)
2346 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2347 candidates->basetype_path = TYPE_BINFO (totype);
2351 if (convs)
2352 args = build_tree_list (NULL_TREE, build_this (expr));
2354 for (; convs; convs = TREE_CHAIN (convs))
2356 tree fns = TREE_VALUE (convs);
2357 int convflags = LOOKUP_NO_CONVERSION;
2358 tree ics;
2360 /* If we are called to convert to a reference type, we are trying to
2361 find an lvalue binding, so don't even consider temporaries. If
2362 we don't find an lvalue binding, the caller will try again to
2363 look for a temporary binding. */
2364 if (TREE_CODE (totype) == REFERENCE_TYPE)
2365 convflags |= LOOKUP_NO_TEMP_BIND;
2367 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2368 ics = implicit_conversion
2369 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2370 else
2371 /* We can't compute this yet. */
2372 ics = error_mark_node;
2374 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2375 /* ignore the near match. */;
2376 else if (ics)
2377 for (; fns; fns = OVL_NEXT (fns))
2379 tree fn = OVL_CURRENT (fns);
2380 struct z_candidate *old_candidates = candidates;
2382 /* [over.match.funcs] For conversion functions, the function is
2383 considered to be a member of the class of the implicit object
2384 argument for the purpose of defining the type of the implicit
2385 object parameter.
2387 So we pass fromtype as CTYPE to add_*_candidate. */
2389 if (TREE_CODE (fn) == TEMPLATE_DECL)
2391 templates = tree_cons (NULL_TREE, fn, templates);
2392 candidates =
2393 add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2394 args, totype, flags,
2395 DEDUCE_CONV);
2397 else
2398 candidates = add_function_candidate (candidates, fn, fromtype,
2399 args, flags);
2401 if (candidates != old_candidates)
2403 if (TREE_CODE (fn) == TEMPLATE_DECL)
2404 ics = implicit_conversion
2405 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2406 0, convflags);
2408 candidates->second_conv = ics;
2409 candidates->basetype_path = TYPE_BINFO (fromtype);
2411 if (ics == NULL_TREE)
2412 candidates->viable = 0;
2413 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2414 candidates->viable = -1;
2419 if (! any_viable (candidates))
2421 #if 0
2422 if (flags & LOOKUP_COMPLAIN)
2424 if (candidates && ! candidates->next)
2425 /* say why this one won't work or try to be loose */;
2426 else
2427 cp_error ("no viable candidates");
2429 #endif
2431 return 0;
2434 candidates = splice_viable (candidates);
2435 cand = tourney (candidates);
2437 if (cand == 0)
2439 if (flags & LOOKUP_COMPLAIN)
2441 cp_error ("conversion from `%T' to `%T' is ambiguous",
2442 fromtype, totype);
2443 print_z_candidates (candidates);
2446 cand = candidates; /* any one will do */
2447 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2448 ICS_USER_FLAG (cand->second_conv) = 1;
2449 ICS_BAD_FLAG (cand->second_conv) = 1;
2451 return cand;
2454 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2455 p = &(TREE_OPERAND (*p, 0));
2457 /* Pedantically, normal function declarations are never considered
2458 to refer to template instantiations, so we only do this with
2459 -fguiding-decls. */
2460 if (flag_guiding_decls && templates && ! cand->template
2461 && !DECL_INITIAL (cand->fn)
2462 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2463 add_maybe_template (cand->fn, templates);
2465 *p = build
2466 (USER_CONV,
2467 (DECL_CONSTRUCTOR_P (cand->fn)
2468 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2469 expr, build_expr_ptr_wrapper (cand));
2470 ICS_USER_FLAG (cand->second_conv) = 1;
2471 if (cand->viable == -1)
2472 ICS_BAD_FLAG (cand->second_conv) = 1;
2474 return cand;
2477 tree
2478 build_user_type_conversion (totype, expr, flags)
2479 tree totype, expr;
2480 int flags;
2482 struct z_candidate *cand
2483 = build_user_type_conversion_1 (totype, expr, flags);
2485 if (cand)
2487 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2488 return error_mark_node;
2489 return convert_from_reference
2490 (convert_like_with_context
2491 (cand->second_conv, expr, cand->fn, 0));
2493 return NULL_TREE;
2496 /* Do any initial processing on the arguments to a function call. */
2498 static tree
2499 resolve_args (args)
2500 tree args;
2502 tree t;
2503 for (t = args; t; t = TREE_CHAIN (t))
2505 if (TREE_VALUE (t) == error_mark_node)
2506 return error_mark_node;
2507 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2509 error ("invalid use of void expression");
2510 return error_mark_node;
2512 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2513 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2515 return args;
2518 tree
2519 build_new_function_call (fn, args)
2520 tree fn, args;
2522 struct z_candidate *candidates = 0, *cand;
2523 tree explicit_targs = NULL_TREE;
2524 int template_only = 0;
2526 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2528 explicit_targs = TREE_OPERAND (fn, 1);
2529 fn = TREE_OPERAND (fn, 0);
2530 template_only = 1;
2533 if (really_overloaded_fn (fn))
2535 tree t1;
2536 tree templates = NULL_TREE;
2538 args = resolve_args (args);
2540 if (args == error_mark_node)
2541 return error_mark_node;
2543 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2545 tree t = OVL_FUNCTION (t1);
2547 if (TREE_CODE (t) == TEMPLATE_DECL)
2549 templates = tree_cons (NULL_TREE, t, templates);
2550 candidates = add_template_candidate
2551 (candidates, t, NULL_TREE, explicit_targs, args, NULL_TREE,
2552 LOOKUP_NORMAL, DEDUCE_CALL);
2554 else if (! template_only)
2555 candidates = add_function_candidate
2556 (candidates, t, NULL_TREE, args, LOOKUP_NORMAL);
2559 if (! any_viable (candidates))
2561 if (candidates && ! candidates->next)
2562 return build_function_call (candidates->fn, args);
2563 cp_error ("no matching function for call to `%D (%A)'",
2564 DECL_NAME (OVL_FUNCTION (fn)), args);
2565 if (candidates)
2566 print_z_candidates (candidates);
2567 return error_mark_node;
2569 candidates = splice_viable (candidates);
2570 cand = tourney (candidates);
2572 if (cand == 0)
2574 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2575 DECL_NAME (OVL_FUNCTION (fn)), args);
2576 print_z_candidates (candidates);
2577 return error_mark_node;
2580 /* Pedantically, normal function declarations are never considered
2581 to refer to template instantiations, so we only do this with
2582 -fguiding-decls. */
2583 if (flag_guiding_decls && templates && ! cand->template
2584 && ! DECL_INITIAL (cand->fn))
2585 add_maybe_template (cand->fn, templates);
2587 return build_over_call (cand, args, LOOKUP_NORMAL);
2590 /* This is not really overloaded. */
2591 fn = OVL_CURRENT (fn);
2593 return build_function_call (fn, args);
2596 static tree
2597 build_object_call (obj, args)
2598 tree obj, args;
2600 struct z_candidate *candidates = 0, *cand;
2601 tree fns, convs, mem_args = NULL_TREE;
2602 tree type = TREE_TYPE (obj);
2604 if (TYPE_PTRMEMFUNC_P (type))
2606 /* It's no good looking for an overloaded operator() on a
2607 pointer-to-member-function. */
2608 cp_error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2609 return error_mark_node;
2612 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2613 if (fns == error_mark_node)
2614 return error_mark_node;
2616 args = resolve_args (args);
2618 if (args == error_mark_node)
2619 return error_mark_node;
2621 if (fns)
2623 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
2624 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2626 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2628 tree fn = OVL_CURRENT (fns);
2629 if (TREE_CODE (fn) == TEMPLATE_DECL)
2631 candidates
2632 = add_template_candidate (candidates, fn, base, NULL_TREE,
2633 mem_args, NULL_TREE,
2634 LOOKUP_NORMAL, DEDUCE_CALL);
2636 else
2637 candidates = add_function_candidate
2638 (candidates, fn, base, mem_args, LOOKUP_NORMAL);
2640 if (candidates)
2641 candidates->basetype_path = TYPE_BINFO (type);
2645 convs = lookup_conversions (type);
2647 for (; convs; convs = TREE_CHAIN (convs))
2649 tree fns = TREE_VALUE (convs);
2650 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2652 if ((TREE_CODE (totype) == POINTER_TYPE
2653 || TREE_CODE (totype) == REFERENCE_TYPE)
2654 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2655 for (; fns; fns = OVL_NEXT (fns))
2657 tree fn = OVL_CURRENT (fns);
2658 if (TREE_CODE (fn) == TEMPLATE_DECL)
2660 candidates = add_template_conv_candidate (candidates,
2662 obj,
2663 args,
2664 totype);
2666 else
2667 candidates = add_conv_candidate (candidates, fn, obj, args);
2671 if (! any_viable (candidates))
2673 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2674 print_z_candidates (candidates);
2675 return error_mark_node;
2678 candidates = splice_viable (candidates);
2679 cand = tourney (candidates);
2681 if (cand == 0)
2683 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2684 print_z_candidates (candidates);
2685 return error_mark_node;
2688 /* Since cand->fn will be a type, not a function, for a conversion
2689 function, we must be careful not to unconditionally look at
2690 DECL_NAME here. */
2691 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2692 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2693 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2695 obj = convert_like_with_context
2696 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2698 /* FIXME */
2699 return build_function_call (obj, args);
2702 static void
2703 op_error (code, code2, arg1, arg2, arg3, problem)
2704 enum tree_code code, code2;
2705 tree arg1, arg2, arg3;
2706 const char *problem;
2708 const char * opname;
2710 if (code == MODIFY_EXPR)
2711 opname = assignment_operator_name_info[code2].name;
2712 else
2713 opname = operator_name_info[code].name;
2715 switch (code)
2717 case COND_EXPR:
2718 cp_error ("%s for `%T ? %T : %T'", problem,
2719 error_type (arg1), error_type (arg2), error_type (arg3));
2720 break;
2721 case POSTINCREMENT_EXPR:
2722 case POSTDECREMENT_EXPR:
2723 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2724 break;
2725 case ARRAY_REF:
2726 cp_error ("%s for `%T[%T]'", problem,
2727 error_type (arg1), error_type (arg2));
2728 break;
2729 default:
2730 if (arg2)
2731 cp_error ("%s for `%T %s %T'", problem,
2732 error_type (arg1), opname, error_type (arg2));
2733 else
2734 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2738 /* Return the implicit conversion sequence that could be used to
2739 convert E1 to E2 in [expr.cond]. */
2741 static tree
2742 conditional_conversion (e1, e2)
2743 tree e1;
2744 tree e2;
2746 tree t1 = non_reference (TREE_TYPE (e1));
2747 tree t2 = non_reference (TREE_TYPE (e2));
2748 tree conv;
2750 /* [expr.cond]
2752 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2753 implicitly converted (clause _conv_) to the type "reference to
2754 T2", subject to the constraint that in the conversion the
2755 reference must bind directly (_dcl.init.ref_) to E1. */
2756 if (real_lvalue_p (e2))
2758 conv = implicit_conversion (build_reference_type (t2),
2761 LOOKUP_NO_TEMP_BIND);
2762 if (conv)
2763 return conv;
2766 /* [expr.cond]
2768 If E1 and E2 have class type, and the underlying class types are
2769 the same or one is a base class of the other: E1 can be converted
2770 to match E2 if the class of T2 is the same type as, or a base
2771 class of, the class of T1, and the cv-qualification of T2 is the
2772 same cv-qualification as, or a greater cv-qualification than, the
2773 cv-qualification of T1. If the conversion is applied, E1 is
2774 changed to an rvalue of type T2 that still refers to the original
2775 source class object (or the appropriate subobject thereof). */
2776 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2777 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2778 TYPE_MAIN_VARIANT (t1)))
2780 if (at_least_as_qualified_p (t2, t1))
2782 conv = build1 (IDENTITY_CONV, t1, e1);
2783 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2784 TYPE_MAIN_VARIANT (t2)))
2785 conv = build_conv (BASE_CONV, t2, conv);
2786 return conv;
2788 else
2789 return NULL_TREE;
2792 /* [expr.cond]
2794 E1 can be converted to match E2 if E1 can be implicitly converted
2795 to the type that expression E2 would have if E2 were converted to
2796 an rvalue (or the type it has, if E2 is an rvalue). */
2797 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2800 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2801 arguments to the conditional expression. By the time this function
2802 is called, any suitable candidate functions are included in
2803 CANDIDATES. */
2805 tree
2806 build_conditional_expr (arg1, arg2, arg3)
2807 tree arg1;
2808 tree arg2;
2809 tree arg3;
2811 tree arg2_type;
2812 tree arg3_type;
2813 tree result;
2814 tree result_type = NULL_TREE;
2815 int lvalue_p = 1;
2816 struct z_candidate *candidates = 0;
2817 struct z_candidate *cand;
2819 /* As a G++ extension, the second argument to the conditional can be
2820 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2821 c'.) If the second operand is omitted, make sure it is
2822 calculated only once. */
2823 if (!arg2)
2825 if (pedantic)
2826 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2827 arg1 = arg2 = save_expr (arg1);
2830 /* [expr.cond]
2832 The first expr ession is implicitly converted to bool (clause
2833 _conv_). */
2834 arg1 = cp_convert (boolean_type_node, arg1);
2836 /* If something has already gone wrong, just pass that fact up the
2837 tree. */
2838 if (arg1 == error_mark_node
2839 || arg2 == error_mark_node
2840 || arg3 == error_mark_node
2841 || TREE_TYPE (arg1) == error_mark_node
2842 || TREE_TYPE (arg2) == error_mark_node
2843 || TREE_TYPE (arg3) == error_mark_node)
2844 return error_mark_node;
2846 /* Convert from reference types to ordinary types; no expressions
2847 really have reference type in C++. */
2848 arg2 = convert_from_reference (arg2);
2849 arg3 = convert_from_reference (arg3);
2851 /* [expr.cond]
2853 If either the second or the third operand has type (possibly
2854 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2855 array-to-pointer (_conv.array_), and function-to-pointer
2856 (_conv.func_) standard conversions are performed on the second
2857 and third operands. */
2858 arg2_type = TREE_TYPE (arg2);
2859 arg3_type = TREE_TYPE (arg3);
2860 if (same_type_p (TYPE_MAIN_VARIANT (arg2_type), void_type_node)
2861 || same_type_p (TYPE_MAIN_VARIANT (arg3_type), void_type_node))
2863 int arg2_void_p;
2864 int arg3_void_p;
2866 /* Do the conversions. We don't these for `void' type arguments
2867 since it can't have any effect and since decay_conversion
2868 does not handle that case gracefully. */
2869 if (!same_type_p (TYPE_MAIN_VARIANT (arg2_type), void_type_node))
2870 arg2 = decay_conversion (arg2);
2871 if (!same_type_p (TYPE_MAIN_VARIANT (arg3_type), void_type_node))
2872 arg3 = decay_conversion (arg3);
2873 arg2_type = TREE_TYPE (arg2);
2874 arg3_type = TREE_TYPE (arg3);
2876 arg2_void_p = same_type_p (TYPE_MAIN_VARIANT (arg2_type),
2877 void_type_node);
2878 arg3_void_p = same_type_p (TYPE_MAIN_VARIANT (arg3_type),
2879 void_type_node);
2881 /* [expr.cond]
2883 One of the following shall hold:
2885 --The second or the third operand (but not both) is a
2886 throw-expression (_except.throw_); the result is of the
2887 type of the other and is an rvalue.
2889 --Both the second and the third operands have type void; the
2890 result is of type void and is an rvalue. */
2891 if ((TREE_CODE (arg2) == THROW_EXPR)
2892 ^ (TREE_CODE (arg3) == THROW_EXPR))
2893 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2894 ? arg3_type : arg2_type);
2895 else if (arg2_void_p && arg3_void_p)
2896 result_type = void_type_node;
2897 else
2899 cp_error ("`%E' has type `void' and is not a throw-expression",
2900 arg2_void_p ? arg2 : arg3);
2901 return error_mark_node;
2904 lvalue_p = 0;
2905 goto valid_operands;
2907 /* [expr.cond]
2909 Otherwise, if the second and third operand have different types,
2910 and either has (possibly cv-qualified) class type, an attempt is
2911 made to convert each of those operands to the type of the other. */
2912 else if (!same_type_p (arg2_type, arg3_type)
2913 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2915 tree conv2 = conditional_conversion (arg2, arg3);
2916 tree conv3 = conditional_conversion (arg3, arg2);
2918 /* [expr.cond]
2920 If both can be converted, or one can be converted but the
2921 conversion is ambiguous, the program is ill-formed. If
2922 neither can be converted, the operands are left unchanged and
2923 further checking is performed as described below. If exactly
2924 one conversion is possible, that conversion is applied to the
2925 chosen operand and the converted operand is used in place of
2926 the original operand for the remainder of this section. */
2927 if ((conv2 && !ICS_BAD_FLAG (conv2)
2928 && conv3 && !ICS_BAD_FLAG (conv3))
2929 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
2930 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
2932 cp_error ("operands to ?: have different types");
2933 return error_mark_node;
2935 else if (conv2 && !ICS_BAD_FLAG (conv2))
2937 arg2 = convert_like (conv2, arg2);
2938 arg2 = convert_from_reference (arg2);
2939 /* That may not quite have done the trick. If the two types
2940 are cv-qualified variants of one another, we will have
2941 just used an IDENTITY_CONV. (There's no conversion from
2942 an lvalue of one class type to an lvalue of another type,
2943 even a cv-qualified variant, and we don't want to lose
2944 lvalue-ness here.) So, we manually add a NOP_EXPR here
2945 if necessary. */
2946 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
2947 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
2948 arg2_type = TREE_TYPE (arg2);
2950 else if (conv3 && !ICS_BAD_FLAG (conv3))
2952 arg3 = convert_like (conv3, arg3);
2953 arg3 = convert_from_reference (arg3);
2954 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
2955 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
2956 arg3_type = TREE_TYPE (arg3);
2960 /* [expr.cond]
2962 If the second and third operands are lvalues and have the same
2963 type, the result is of that type and is an lvalue. */
2964 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
2965 same_type_p (arg2_type, arg3_type))
2967 result_type = arg2_type;
2968 goto valid_operands;
2971 /* [expr.cond]
2973 Otherwise, the result is an rvalue. If the second and third
2974 operand do not have the same type, and either has (possibly
2975 cv-qualified) class type, overload resolution is used to
2976 determine the conversions (if any) to be applied to the operands
2977 (_over.match.oper_, _over.built_). */
2978 lvalue_p = 0;
2979 if (!same_type_p (arg2_type, arg3_type)
2980 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2982 tree args[3];
2983 tree conv;
2985 /* Rearrange the arguments so that add_builtin_candidate only has
2986 to know about two args. In build_builtin_candidates, the
2987 arguments are unscrambled. */
2988 args[0] = arg2;
2989 args[1] = arg3;
2990 args[2] = arg1;
2991 candidates = add_builtin_candidates (candidates,
2992 COND_EXPR,
2993 NOP_EXPR,
2994 ansi_opname (COND_EXPR),
2995 args,
2996 LOOKUP_NORMAL);
2998 /* [expr.cond]
3000 If the overload resolution fails, the program is
3001 ill-formed. */
3002 if (!any_viable (candidates))
3004 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3005 print_z_candidates (candidates);
3006 return error_mark_node;
3008 candidates = splice_viable (candidates);
3009 cand = tourney (candidates);
3010 if (!cand)
3012 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3013 print_z_candidates (candidates);
3014 return error_mark_node;
3017 /* [expr.cond]
3019 Otherwise, the conversions thus determined are applied, and
3020 the converted operands are used in place of the original
3021 operands for the remainder of this section. */
3022 conv = TREE_VEC_ELT (cand->convs, 0);
3023 arg1 = convert_like (conv, arg1);
3024 conv = TREE_VEC_ELT (cand->convs, 1);
3025 arg2 = convert_like (conv, arg2);
3026 conv = TREE_VEC_ELT (cand->convs, 2);
3027 arg3 = convert_like (conv, arg3);
3030 /* [expr.cond]
3032 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3033 and function-to-pointer (_conv.func_) standard conversions are
3034 performed on the second and third operands.
3036 We need to force the lvalue-to-rvalue conversion here for class types,
3037 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3038 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3039 regions. */
3041 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3042 arg2 = build_user_type_conversion (TREE_TYPE (arg2), arg2, LOOKUP_NORMAL);
3043 else
3044 arg2 = decay_conversion (arg2);
3045 arg2_type = TREE_TYPE (arg2);
3047 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3048 arg3 = build_user_type_conversion (TREE_TYPE (arg3), arg3, LOOKUP_NORMAL);
3049 else
3050 arg3 = decay_conversion (arg3);
3051 arg3_type = TREE_TYPE (arg3);
3053 /* [expr.cond]
3055 After those conversions, one of the following shall hold:
3057 --The second and third operands have the same type; the result is of
3058 that type. */
3059 if (same_type_p (arg2_type, arg3_type))
3060 result_type = arg2_type;
3061 /* [expr.cond]
3063 --The second and third operands have arithmetic or enumeration
3064 type; the usual arithmetic conversions are performed to bring
3065 them to a common type, and the result is of that type. */
3066 else if ((ARITHMETIC_TYPE_P (arg2_type)
3067 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3068 && (ARITHMETIC_TYPE_P (arg3_type)
3069 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3071 /* In this case, there is always a common type. */
3072 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3073 arg3_type);
3075 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3076 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3077 cp_warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3078 arg2_type, arg3_type);
3079 else if (extra_warnings
3080 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3081 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3082 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3083 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3084 cp_warning ("enumeral and non-enumeral type in conditional expression");
3086 arg2 = perform_implicit_conversion (result_type, arg2);
3087 arg3 = perform_implicit_conversion (result_type, arg3);
3089 /* [expr.cond]
3091 --The second and third operands have pointer type, or one has
3092 pointer type and the other is a null pointer constant; pointer
3093 conversions (_conv.ptr_) and qualification conversions
3094 (_conv.qual_) are performed to bring them to their composite
3095 pointer type (_expr.rel_). The result is of the composite
3096 pointer type.
3098 --The second and third operands have pointer to member type, or
3099 one has pointer to member type and the other is a null pointer
3100 constant; pointer to member conversions (_conv.mem_) and
3101 qualification conversions (_conv.qual_) are performed to bring
3102 them to a common type, whose cv-qualification shall match the
3103 cv-qualification of either the second or the third operand.
3104 The result is of the common type. */
3105 else if ((null_ptr_cst_p (arg2)
3106 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3107 || TYPE_PTRMEMFUNC_P (arg3_type)))
3108 || (null_ptr_cst_p (arg3)
3109 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3110 || TYPE_PTRMEMFUNC_P (arg2_type)))
3111 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3112 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3113 || (TYPE_PTRMEMFUNC_P (arg2_type)
3114 && TYPE_PTRMEMFUNC_P (arg3_type)))
3116 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3117 arg3, "conditional expression");
3118 arg2 = perform_implicit_conversion (result_type, arg2);
3119 arg3 = perform_implicit_conversion (result_type, arg3);
3122 if (!result_type)
3124 cp_error ("operands to ?: have different types");
3125 return error_mark_node;
3128 valid_operands:
3129 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3130 /* Expand both sides into the same slot, hopefully the target of the
3131 ?: expression. We used to check for TARGET_EXPRs here, but now we
3132 sometimes wrap them in NOP_EXPRs so the test would fail. */
3133 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3134 result = build_target_expr_with_type (result, result_type);
3136 /* If this expression is an rvalue, but might be mistaken for an
3137 lvalue, we must add a NON_LVALUE_EXPR. */
3138 if (!lvalue_p && real_lvalue_p (result))
3139 result = build1 (NON_LVALUE_EXPR, result_type, result);
3141 return result;
3144 tree
3145 build_new_op (code, flags, arg1, arg2, arg3)
3146 enum tree_code code;
3147 int flags;
3148 tree arg1, arg2, arg3;
3150 struct z_candidate *candidates = 0, *cand;
3151 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3152 enum tree_code code2 = NOP_EXPR;
3153 tree templates = NULL_TREE;
3154 tree conv;
3156 if (arg1 == error_mark_node
3157 || arg2 == error_mark_node
3158 || arg3 == error_mark_node)
3159 return error_mark_node;
3161 /* This can happen if a template takes all non-type parameters, e.g.
3162 undeclared_template<1, 5, 72>a; */
3163 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3165 cp_error ("`%D' must be declared before use", arg1);
3166 return error_mark_node;
3169 if (code == MODIFY_EXPR)
3171 code2 = TREE_CODE (arg3);
3172 arg3 = NULL_TREE;
3173 fnname = ansi_assopname (code2);
3175 else
3176 fnname = ansi_opname (code);
3178 switch (code)
3180 case NEW_EXPR:
3181 case VEC_NEW_EXPR:
3182 case VEC_DELETE_EXPR:
3183 case DELETE_EXPR:
3184 /* Use build_op_new_call and build_op_delete_call instead. */
3185 my_friendly_abort (981018);
3187 case CALL_EXPR:
3188 return build_object_call (arg1, arg2);
3190 default:
3191 break;
3194 /* The comma operator can have void args. */
3195 if (TREE_CODE (arg1) == OFFSET_REF)
3196 arg1 = resolve_offset_ref (arg1);
3197 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
3198 arg2 = resolve_offset_ref (arg2);
3199 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
3200 arg3 = resolve_offset_ref (arg3);
3202 if (code == COND_EXPR)
3204 if (arg2 == NULL_TREE
3205 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3206 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3207 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3208 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3209 goto builtin;
3211 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3212 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3213 goto builtin;
3215 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3216 arg2 = integer_zero_node;
3218 if (arg2 && arg3)
3219 arglist = tree_cons (NULL_TREE, arg1, tree_cons
3220 (NULL_TREE, arg2, build_tree_list (NULL_TREE, arg3)));
3221 else if (arg2)
3222 arglist = tree_cons (NULL_TREE, arg1, build_tree_list (NULL_TREE, arg2));
3223 else
3224 arglist = build_tree_list (NULL_TREE, arg1);
3226 fns = lookup_function_nonclass (fnname, arglist);
3228 if (fns && TREE_CODE (fns) == TREE_LIST)
3229 fns = TREE_VALUE (fns);
3230 for (; fns; fns = OVL_NEXT (fns))
3232 tree fn = OVL_CURRENT (fns);
3233 if (TREE_CODE (fn) == TEMPLATE_DECL)
3235 templates = tree_cons (NULL_TREE, fn, templates);
3236 candidates
3237 = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3238 arglist, TREE_TYPE (fnname),
3239 flags, DEDUCE_CALL);
3241 else
3242 candidates = add_function_candidate (candidates, fn, NULL_TREE,
3243 arglist, flags);
3246 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3248 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3249 if (fns == error_mark_node)
3250 return fns;
3252 else
3253 fns = NULL_TREE;
3255 if (fns)
3257 tree basetype = BINFO_TYPE (TREE_PURPOSE (fns));
3258 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3259 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3261 tree fn = OVL_CURRENT (fns);
3262 tree this_arglist;
3264 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3265 this_arglist = mem_arglist;
3266 else
3267 this_arglist = arglist;
3269 if (TREE_CODE (fn) == TEMPLATE_DECL)
3271 /* A member template. */
3272 templates = tree_cons (NULL_TREE, fn, templates);
3273 candidates
3274 = add_template_candidate (candidates, fn, basetype, NULL_TREE,
3275 this_arglist, TREE_TYPE (fnname),
3276 flags, DEDUCE_CALL);
3278 else
3279 candidates = add_function_candidate
3280 (candidates, fn, basetype, this_arglist, flags);
3282 if (candidates)
3283 candidates->basetype_path = TYPE_BINFO (TREE_TYPE (arg1));
3288 tree args[3];
3290 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3291 to know about two args; a builtin candidate will always have a first
3292 parameter of type bool. We'll handle that in
3293 build_builtin_candidate. */
3294 if (code == COND_EXPR)
3296 args[0] = arg2;
3297 args[1] = arg3;
3298 args[2] = arg1;
3300 else
3302 args[0] = arg1;
3303 args[1] = arg2;
3304 args[2] = NULL_TREE;
3307 candidates = add_builtin_candidates
3308 (candidates, code, code2, fnname, args, flags);
3311 if (! any_viable (candidates))
3313 switch (code)
3315 case POSTINCREMENT_EXPR:
3316 case POSTDECREMENT_EXPR:
3317 /* Look for an `operator++ (int)'. If they didn't have
3318 one, then we fall back to the old way of doing things. */
3319 if (flags & LOOKUP_COMPLAIN)
3320 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
3321 fnname,
3322 operator_name_info[code].name);
3323 if (code == POSTINCREMENT_EXPR)
3324 code = PREINCREMENT_EXPR;
3325 else
3326 code = PREDECREMENT_EXPR;
3327 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3329 /* The caller will deal with these. */
3330 case ADDR_EXPR:
3331 case COMPOUND_EXPR:
3332 case COMPONENT_REF:
3333 return NULL_TREE;
3335 default:
3336 break;
3338 if (flags & LOOKUP_COMPLAIN)
3340 op_error (code, code2, arg1, arg2, arg3, "no match");
3341 print_z_candidates (candidates);
3343 return error_mark_node;
3345 candidates = splice_viable (candidates);
3346 cand = tourney (candidates);
3348 if (cand == 0)
3350 if (flags & LOOKUP_COMPLAIN)
3352 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3353 print_z_candidates (candidates);
3355 return error_mark_node;
3358 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3360 extern int warn_synth;
3361 if (warn_synth
3362 && fnname == ansi_assopname (NOP_EXPR)
3363 && DECL_ARTIFICIAL (cand->fn)
3364 && candidates->next
3365 && ! candidates->next->next)
3367 cp_warning ("using synthesized `%#D' for copy assignment",
3368 cand->fn);
3369 cp_warning_at (" where cfront would use `%#D'",
3370 cand == candidates
3371 ? candidates->next->fn
3372 : candidates->fn);
3375 /* Pedantically, normal function declarations are never considered
3376 to refer to template instantiations, so we only do this with
3377 -fguiding-decls. */
3378 if (flag_guiding_decls && templates && ! cand->template
3379 && ! DECL_INITIAL (cand->fn)
3380 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
3381 add_maybe_template (cand->fn, templates);
3383 return build_over_call
3384 (cand,
3385 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3386 ? mem_arglist : arglist,
3387 LOOKUP_NORMAL);
3390 /* Check for comparison of different enum types. */
3391 switch (code)
3393 case GT_EXPR:
3394 case LT_EXPR:
3395 case GE_EXPR:
3396 case LE_EXPR:
3397 case EQ_EXPR:
3398 case NE_EXPR:
3399 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3400 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3401 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3402 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3404 cp_warning ("comparison between `%#T' and `%#T'",
3405 TREE_TYPE (arg1), TREE_TYPE (arg2));
3407 break;
3408 default:
3409 break;
3412 /* We need to strip any leading REF_BIND so that bitfields don't cause
3413 errors. This should not remove any important conversions, because
3414 builtins don't apply to class objects directly. */
3415 conv = TREE_VEC_ELT (cand->convs, 0);
3416 if (TREE_CODE (conv) == REF_BIND)
3417 conv = TREE_OPERAND (conv, 0);
3418 arg1 = convert_like (conv, arg1);
3419 if (arg2)
3421 conv = TREE_VEC_ELT (cand->convs, 1);
3422 if (TREE_CODE (conv) == REF_BIND)
3423 conv = TREE_OPERAND (conv, 0);
3424 arg2 = convert_like (conv, arg2);
3426 if (arg3)
3428 conv = TREE_VEC_ELT (cand->convs, 2);
3429 if (TREE_CODE (conv) == REF_BIND)
3430 conv = TREE_OPERAND (conv, 0);
3431 arg3 = convert_like (conv, arg3);
3434 builtin:
3435 switch (code)
3437 case MODIFY_EXPR:
3438 return build_modify_expr (arg1, code2, arg2);
3440 case INDIRECT_REF:
3441 return build_indirect_ref (arg1, "unary *");
3443 case PLUS_EXPR:
3444 case MINUS_EXPR:
3445 case MULT_EXPR:
3446 case TRUNC_DIV_EXPR:
3447 case GT_EXPR:
3448 case LT_EXPR:
3449 case GE_EXPR:
3450 case LE_EXPR:
3451 case EQ_EXPR:
3452 case NE_EXPR:
3453 case MAX_EXPR:
3454 case MIN_EXPR:
3455 case LSHIFT_EXPR:
3456 case RSHIFT_EXPR:
3457 case TRUNC_MOD_EXPR:
3458 case BIT_AND_EXPR:
3459 case BIT_IOR_EXPR:
3460 case BIT_XOR_EXPR:
3461 case TRUTH_ANDIF_EXPR:
3462 case TRUTH_ORIF_EXPR:
3463 return build_binary_op (code, arg1, arg2);
3465 case CONVERT_EXPR:
3466 case NEGATE_EXPR:
3467 case BIT_NOT_EXPR:
3468 case TRUTH_NOT_EXPR:
3469 case PREINCREMENT_EXPR:
3470 case POSTINCREMENT_EXPR:
3471 case PREDECREMENT_EXPR:
3472 case POSTDECREMENT_EXPR:
3473 case REALPART_EXPR:
3474 case IMAGPART_EXPR:
3475 return build_unary_op (code, arg1, candidates != 0);
3477 case ARRAY_REF:
3478 return build_array_ref (arg1, arg2);
3480 case COND_EXPR:
3481 return build_conditional_expr (arg1, arg2, arg3);
3483 case MEMBER_REF:
3484 return build_m_component_ref
3485 (build_indirect_ref (arg1, NULL_PTR), arg2);
3487 /* The caller will deal with these. */
3488 case ADDR_EXPR:
3489 case COMPONENT_REF:
3490 case COMPOUND_EXPR:
3491 return NULL_TREE;
3493 default:
3494 my_friendly_abort (367);
3495 return NULL_TREE;
3499 /* Build a call to operator delete. This has to be handled very specially,
3500 because the restrictions on what signatures match are different from all
3501 other call instances. For a normal delete, only a delete taking (void *)
3502 or (void *, size_t) is accepted. For a placement delete, only an exact
3503 match with the placement new is accepted.
3505 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3506 ADDR is the pointer to be deleted. For placement delete, it is also
3507 used to determine what the corresponding new looked like.
3508 SIZE is the size of the memory block to be deleted.
3509 FLAGS are the usual overloading flags.
3510 PLACEMENT is the corresponding placement new call, or 0. */
3512 tree
3513 build_op_delete_call (code, addr, size, flags, placement)
3514 enum tree_code code;
3515 tree addr, size, placement;
3516 int flags;
3518 tree fn, fns, fnname, fntype, argtypes, args, type;
3520 if (addr == error_mark_node)
3521 return error_mark_node;
3523 type = TREE_TYPE (TREE_TYPE (addr));
3524 fnname = ansi_opname (code);
3526 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3527 /* In [class.free]
3529 If the result of the lookup is ambiguous or inaccessible, or if
3530 the lookup selects a placement deallocation function, the
3531 program is ill-formed.
3533 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3535 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3536 if (fns == error_mark_node)
3537 return error_mark_node;
3539 else
3540 fns = NULL_TREE;
3542 if (fns == NULL_TREE)
3543 fns = lookup_name_nonclass (fnname);
3545 if (placement)
3547 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3549 /* Extract the function. */
3550 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3551 /* Then the second parm type. */
3552 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3554 /* Also the second argument. */
3555 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3557 else
3559 /* First try it without the size argument. */
3560 argtypes = void_list_node;
3561 args = NULL_TREE;
3564 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3565 fntype = build_function_type (void_type_node, argtypes);
3567 /* Strip const and volatile from addr. */
3568 if (type != TYPE_MAIN_VARIANT (type))
3569 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
3571 fn = instantiate_type (fntype, fns, 2);
3573 if (fn != error_mark_node)
3575 if (TREE_CODE (fns) == TREE_LIST)
3576 /* Member functions. */
3577 enforce_access (type, fn);
3578 return build_function_call (fn, tree_cons (NULL_TREE, addr, args));
3581 /* If we are doing placement delete we do nothing if we don't find a
3582 matching op delete. */
3583 if (placement)
3584 return NULL_TREE;
3586 /* Normal delete; now try to find a match including the size argument. */
3587 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3588 tree_cons (NULL_TREE, sizetype, void_list_node));
3589 fntype = build_function_type (void_type_node, argtypes);
3591 fn = instantiate_type (fntype, fns, 2);
3593 if (fn != error_mark_node)
3595 if (BASELINK_P (fns))
3596 /* Member functions. */
3597 enforce_access (type, fn);
3598 return build_function_call
3599 (fn, tree_cons (NULL_TREE, addr,
3600 build_tree_list (NULL_TREE, size)));
3603 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3604 destructor, in which case the error should be deferred
3605 until someone actually tries to delete one of these. */
3606 if (flags & LOOKUP_SPECULATIVELY)
3607 return NULL_TREE;
3609 cp_error ("no suitable `operator delete' for `%T'", type);
3610 return error_mark_node;
3613 /* If the current scope isn't allowed to access DECL along
3614 BASETYPE_PATH, give an error. The most derived class in
3615 BASETYPE_PATH is the one used to qualify DECL. */
3618 enforce_access (basetype_path, decl)
3619 tree basetype_path;
3620 tree decl;
3622 int accessible;
3624 accessible = accessible_p (basetype_path, decl);
3625 if (!accessible)
3627 if (TREE_PRIVATE (decl))
3628 cp_error_at ("`%+#D' is private", decl);
3629 else if (TREE_PROTECTED (decl))
3630 cp_error_at ("`%+#D' is protected", decl);
3631 else
3632 cp_error_at ("`%+#D' is inaccessible", decl);
3633 cp_error ("within this context");
3634 return 0;
3637 return 1;
3640 /* Perform the conversions in CONVS on the expression EXPR.
3641 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3642 indicates the `this' argument of a method. INNER is non-zero when
3643 being called to continue a conversion chain. */
3645 static tree
3646 convert_like_real (convs, expr, fn, argnum, inner)
3647 tree convs, expr;
3648 tree fn;
3649 int argnum;
3650 int inner;
3652 if (ICS_BAD_FLAG (convs)
3653 && TREE_CODE (convs) != USER_CONV
3654 && TREE_CODE (convs) != AMBIG_CONV
3655 && TREE_CODE (convs) != REF_BIND)
3657 tree t = convs;
3658 for (; t; t = TREE_OPERAND (t, 0))
3660 if (TREE_CODE (t) == USER_CONV)
3662 expr = convert_like_real (t, expr, fn, argnum, 1);
3663 break;
3665 else if (TREE_CODE (t) == AMBIG_CONV)
3666 return convert_like_real (t, expr, fn, argnum, 1);
3667 else if (TREE_CODE (t) == IDENTITY_CONV)
3668 break;
3670 return convert_for_initialization
3671 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3672 "conversion", fn, argnum);
3675 if (!inner)
3676 expr = dubious_conversion_warnings
3677 (TREE_TYPE (convs), expr, "argument", fn, argnum);
3678 switch (TREE_CODE (convs))
3680 case USER_CONV:
3682 struct z_candidate *cand
3683 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3684 tree fn = cand->fn;
3685 tree args;
3687 if (DECL_CONSTRUCTOR_P (fn))
3689 tree t = build_int_2 (0, 0);
3690 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3692 args = build_tree_list (NULL_TREE, expr);
3693 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3694 args = tree_cons (NULL_TREE, integer_one_node, args);
3695 args = tree_cons (NULL_TREE, t, args);
3697 else
3698 args = build_this (expr);
3699 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3701 /* If this is a constructor or a function returning an aggr type,
3702 we need to build up a TARGET_EXPR. */
3703 if (DECL_CONSTRUCTOR_P (fn))
3704 expr = build_cplus_new (TREE_TYPE (convs), expr);
3706 return expr;
3708 case IDENTITY_CONV:
3709 if (type_unknown_p (expr))
3710 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3711 return expr;
3712 case AMBIG_CONV:
3713 /* Call build_user_type_conversion again for the error. */
3714 return build_user_type_conversion
3715 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3717 default:
3718 break;
3721 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum, 1);
3722 if (expr == error_mark_node)
3723 return error_mark_node;
3725 /* Convert a non-array constant variable to its underlying value, unless we
3726 are about to bind it to a reference, in which case we need to
3727 leave it as an lvalue. */
3728 if (TREE_CODE (convs) != REF_BIND
3729 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3730 expr = decl_constant_value (expr);
3732 switch (TREE_CODE (convs))
3734 case RVALUE_CONV:
3735 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3736 return expr;
3737 /* else fall through */
3738 case BASE_CONV:
3739 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3741 /* We are going to bind a reference directly to a base-class
3742 subobject of EXPR. */
3743 tree base_ptr = build_pointer_type (TREE_TYPE (convs));
3745 /* Build an expression for `*((base*) &expr)'. */
3746 expr = build_unary_op (ADDR_EXPR, expr, 0);
3747 expr = perform_implicit_conversion (base_ptr, expr);
3748 expr = build_indirect_ref (expr, "implicit conversion");
3749 return expr;
3753 tree cvt_expr = build_user_type_conversion
3754 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3755 if (!cvt_expr)
3757 /* This can occur if, for example, the EXPR has incomplete
3758 type. We can't check for that before attempting the
3759 conversion because the type might be an incomplete
3760 array type, which is OK if some constructor for the
3761 destination type takes a pointer argument. */
3762 if (!COMPLETE_TYPE_P (TREE_TYPE (expr)))
3764 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
3765 incomplete_type_error (expr, TREE_TYPE (expr));
3766 else
3767 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3768 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3770 else
3771 cp_error ("could not convert `%E' to `%T'",
3772 expr, TREE_TYPE (convs));
3773 return error_mark_node;
3775 return cvt_expr;
3778 case REF_BIND:
3780 tree ref_type = TREE_TYPE (convs);
3782 /* If necessary, create a temporary. */
3783 if (NEED_TEMPORARY_P (convs))
3785 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3786 expr = build_target_expr_with_type (expr, type);
3789 /* Take the address of the thing to which we will bind the
3790 reference. */
3791 expr = build_unary_op (ADDR_EXPR, expr, 1);
3792 if (expr == error_mark_node)
3793 return error_mark_node;
3795 /* Convert it to a pointer to the type referred to by the
3796 reference. This will adjust the pointer if a derived to
3797 base conversion is being performed. */
3798 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3799 expr);
3800 /* Convert the pointer to the desired reference type. */
3801 expr = build1 (NOP_EXPR, ref_type, expr);
3803 return expr;
3806 case LVALUE_CONV:
3807 return decay_conversion (expr);
3809 case QUAL_CONV:
3810 /* Warn about deprecated conversion if appropriate. */
3811 string_conv_p (TREE_TYPE (convs), expr, 1);
3812 break;
3814 default:
3815 break;
3817 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3818 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3821 /* ARG is being passed to a varargs function. Perform any conversions
3822 required. Array/function to pointer decay must have already happened.
3823 Return the converted value. */
3825 tree
3826 convert_arg_to_ellipsis (arg)
3827 tree arg;
3829 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3830 && (TYPE_PRECISION (TREE_TYPE (arg))
3831 < TYPE_PRECISION (double_type_node)))
3832 /* Convert `float' to `double'. */
3833 arg = cp_convert (double_type_node, arg);
3834 else
3835 /* Convert `short' and `char' to full-size `int'. */
3836 arg = default_conversion (arg);
3838 arg = require_complete_type (arg);
3840 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
3842 /* Undefined behaviour [expr.call] 5.2.2/7. */
3843 cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3844 TREE_TYPE (arg));
3847 return arg;
3850 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
3852 tree
3853 build_x_va_arg (expr, type)
3854 tree expr;
3855 tree type;
3857 type = complete_type_or_else (type, NULL_TREE);
3859 if (expr == error_mark_node || !type)
3860 return error_mark_node;
3862 if (! pod_type_p (type))
3864 /* Undefined behaviour [expr.call] 5.2.2/7. */
3865 cp_warning ("cannot receive objects of non-POD type `%#T' through `...'",
3866 type);
3869 return build_va_arg (expr, type);
3872 /* TYPE has been given to va_arg. Apply the default conversions which would
3873 have happened when passed via ellipsis. Return the promoted type, or
3874 NULL_TREE, if there is no change. */
3876 tree
3877 convert_type_from_ellipsis (type)
3878 tree type;
3880 tree promote;
3882 if (TREE_CODE (type) == ARRAY_TYPE)
3883 promote = build_pointer_type (TREE_TYPE (type));
3884 else if (TREE_CODE (type) == FUNCTION_TYPE)
3885 promote = build_pointer_type (type);
3886 else
3887 promote = type_promotes_to (type);
3889 return same_type_p (type, promote) ? NULL_TREE : promote;
3892 /* ARG is a default argument expression being passed to a parameter of
3893 the indicated TYPE, which is a parameter to FN. Do any required
3894 conversions. Return the converted value. */
3896 tree
3897 convert_default_arg (type, arg, fn, parmnum)
3898 tree type;
3899 tree arg;
3900 tree fn;
3901 int parmnum;
3903 if (fn && DECL_TEMPLATE_INFO (fn))
3904 arg = tsubst_default_argument (fn, type, arg);
3906 arg = break_out_target_exprs (arg);
3908 if (TREE_CODE (arg) == CONSTRUCTOR)
3910 arg = digest_init (type, arg, 0);
3911 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3912 "default argument", fn, parmnum);
3914 else
3916 /* This could get clobbered by the following call. */
3917 if (TREE_HAS_CONSTRUCTOR (arg))
3918 arg = copy_node (arg);
3920 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3921 "default argument", fn, parmnum);
3922 if (PROMOTE_PROTOTYPES
3923 && (TREE_CODE (type) == INTEGER_TYPE
3924 || TREE_CODE (type) == ENUMERAL_TYPE)
3925 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3926 arg = default_conversion (arg);
3929 return arg;
3932 static tree
3933 build_over_call (cand, args, flags)
3934 struct z_candidate *cand;
3935 tree args;
3936 int flags;
3938 tree fn = cand->fn;
3939 tree convs = cand->convs;
3940 tree converted_args = NULL_TREE;
3941 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3942 tree conv, arg, val;
3943 int i = 0;
3944 int is_method = 0;
3946 /* Give any warnings we noticed during overload resolution. */
3947 if (cand->warnings)
3948 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3949 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3951 if (DECL_FUNCTION_MEMBER_P (fn))
3952 enforce_access (cand->basetype_path, fn);
3954 if (args && TREE_CODE (args) != TREE_LIST)
3955 args = build_tree_list (NULL_TREE, args);
3956 arg = args;
3958 /* The implicit parameters to a constructor are not considered by overload
3959 resolution, and must be of the proper type. */
3960 if (DECL_CONSTRUCTOR_P (fn))
3962 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3963 arg = TREE_CHAIN (arg);
3964 parm = TREE_CHAIN (parm);
3965 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3967 converted_args = tree_cons
3968 (NULL_TREE, TREE_VALUE (arg), converted_args);
3969 arg = TREE_CHAIN (arg);
3970 parm = TREE_CHAIN (parm);
3973 /* Bypass access control for 'this' parameter. */
3974 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3976 tree parmtype = TREE_VALUE (parm);
3977 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3978 tree t;
3979 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3980 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3981 TREE_TYPE (argtype), fn);
3983 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3984 X is called for an object that is not of type X, or of a type
3985 derived from X, the behavior is undefined.
3987 So we can assume that anything passed as 'this' is non-null, and
3988 optimize accordingly. */
3989 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
3990 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3991 converted_args = tree_cons (NULL_TREE, t, converted_args);
3992 parm = TREE_CHAIN (parm);
3993 arg = TREE_CHAIN (arg);
3994 ++i;
3995 is_method = 1;
3998 for (; arg && parm;
3999 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4001 tree type = TREE_VALUE (parm);
4003 conv = TREE_VEC_ELT (convs, i);
4004 if (ICS_BAD_FLAG (conv))
4006 tree t = conv;
4007 val = TREE_VALUE (arg);
4009 for (; t; t = TREE_OPERAND (t, 0))
4011 if (TREE_CODE (t) == USER_CONV
4012 || TREE_CODE (t) == AMBIG_CONV)
4014 val = convert_like_with_context (t, val, fn, i - is_method);
4015 break;
4017 else if (TREE_CODE (t) == IDENTITY_CONV)
4018 break;
4020 val = convert_for_initialization
4021 (NULL_TREE, type, val, LOOKUP_NORMAL,
4022 "argument", fn, i - is_method);
4024 else
4026 val = TREE_VALUE (arg);
4027 val = convert_like_with_context
4028 (conv, TREE_VALUE (arg), fn, i - is_method);
4031 if (PROMOTE_PROTOTYPES
4032 && (TREE_CODE (type) == INTEGER_TYPE
4033 || TREE_CODE (type) == ENUMERAL_TYPE)
4034 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4035 val = default_conversion (val);
4036 converted_args = tree_cons (NULL_TREE, val, converted_args);
4039 /* Default arguments */
4040 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4041 converted_args
4042 = tree_cons (NULL_TREE,
4043 convert_default_arg (TREE_VALUE (parm),
4044 TREE_PURPOSE (parm),
4045 fn, i - is_method),
4046 converted_args);
4048 /* Ellipsis */
4049 for (; arg; arg = TREE_CHAIN (arg))
4050 converted_args
4051 = tree_cons (NULL_TREE,
4052 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4053 converted_args);
4055 converted_args = nreverse (converted_args);
4057 if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
4058 check_function_format (DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
4059 converted_args);
4061 /* Avoid actually calling copy constructors and copy assignment operators,
4062 if possible. */
4064 if (! flag_elide_constructors)
4065 /* Do things the hard way. */;
4066 else if (TREE_VEC_LENGTH (convs) == 1
4067 && DECL_COPY_CONSTRUCTOR_P (fn))
4069 tree targ;
4070 arg = TREE_CHAIN (converted_args);
4071 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4072 arg = TREE_CHAIN (arg);
4073 arg = TREE_VALUE (arg);
4075 /* Pull out the real argument, disregarding const-correctness. */
4076 targ = arg;
4077 while (TREE_CODE (targ) == NOP_EXPR
4078 || TREE_CODE (targ) == NON_LVALUE_EXPR
4079 || TREE_CODE (targ) == CONVERT_EXPR)
4080 targ = TREE_OPERAND (targ, 0);
4081 if (TREE_CODE (targ) == ADDR_EXPR)
4083 targ = TREE_OPERAND (targ, 0);
4084 if (!same_type_ignoring_top_level_qualifiers_p
4085 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4086 targ = NULL_TREE;
4088 else
4089 targ = NULL_TREE;
4091 if (targ)
4092 arg = targ;
4093 else
4094 arg = build_indirect_ref (arg, 0);
4096 /* [class.copy]: the copy constructor is implicitly defined even if
4097 the implementation elided its use. */
4098 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4099 mark_used (fn);
4101 /* If we're creating a temp and we already have one, don't create a
4102 new one. If we're not creating a temp but we get one, use
4103 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4104 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4105 temp or an INIT_EXPR otherwise. */
4106 if (integer_zerop (TREE_VALUE (args)))
4108 if (! real_lvalue_p (arg))
4109 return arg;
4110 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4111 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4113 else if (! real_lvalue_p (arg)
4114 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4116 tree address;
4117 tree to = stabilize_reference
4118 (build_indirect_ref (TREE_VALUE (args), 0));
4120 /* If we're initializing an empty class, then we actually
4121 have to use a MODIFY_EXPR rather than an INIT_EXPR. The
4122 reason is that the dummy padding member in the target may
4123 not actually be allocated if TO is a base class
4124 subobject. Since we've set TYPE_NONCOPIED_PARTS on the
4125 padding, a MODIFY_EXPR will preserve its value, which is
4126 the right thing to do if it's not really padding at all.
4128 It's not safe to just throw away the ARG if we're looking
4129 at an empty class because the ARG might contain a
4130 TARGET_EXPR which wants to be bound to TO. If it is not,
4131 expand_expr will assign a dummy slot for the TARGET_EXPR,
4132 and we will call a destructor for it, which is wrong,
4133 because we will also destroy TO, but will never have
4134 constructed it. */
4135 val = build (is_empty_class (DECL_CONTEXT (fn))
4136 ? MODIFY_EXPR : INIT_EXPR,
4137 DECL_CONTEXT (fn), to, arg);
4138 address = build_unary_op (ADDR_EXPR, val, 0);
4139 /* Avoid a warning about this expression, if the address is
4140 never used. */
4141 TREE_USED (address) = 1;
4142 return address;
4145 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4146 && copy_args_p (fn)
4147 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4149 tree to = stabilize_reference
4150 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4152 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4154 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4155 return val;
4158 mark_used (fn);
4160 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4162 tree t, *p = &TREE_VALUE (converted_args);
4163 tree binfo = get_binfo
4164 (DECL_VIRTUAL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
4165 *p = convert_pointer_to_real (binfo, *p);
4166 if (TREE_SIDE_EFFECTS (*p))
4167 *p = save_expr (*p);
4168 t = build_pointer_type (TREE_TYPE (fn));
4169 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4170 TREE_TYPE (fn) = t;
4172 else if (DECL_INLINE (fn))
4173 fn = inline_conversion (fn);
4174 else
4175 fn = build_addr_func (fn);
4177 /* Recognize certain built-in functions so we can make tree-codes
4178 other than CALL_EXPR. We do this when it enables fold-const.c
4179 to do something useful. */
4181 if (TREE_CODE (fn) == ADDR_EXPR
4182 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4183 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4185 tree exp;
4186 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4187 if (exp)
4188 return exp;
4191 fn = build_call (fn, converted_args);
4192 if (TREE_CODE (TREE_TYPE (fn)) == VOID_TYPE)
4193 return fn;
4194 fn = require_complete_type (fn);
4195 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4196 fn = build_cplus_new (TREE_TYPE (fn), fn);
4197 return convert_from_reference (fn);
4200 /* Returns the value to use for the in-charge parameter when making a
4201 call to a function with the indicated NAME. */
4203 tree
4204 in_charge_arg_for_name (name)
4205 tree name;
4207 if (name == base_ctor_identifier
4208 || name == base_dtor_identifier)
4209 return integer_zero_node;
4210 else if (name == complete_ctor_identifier)
4211 return integer_one_node;
4212 else if (name == complete_dtor_identifier)
4213 return integer_two_node;
4214 else if (name == deleting_dtor_identifier)
4215 return integer_three_node;
4217 /* This function should only be called with one of the names listed
4218 above. */
4219 my_friendly_abort (20000411);
4220 return NULL_TREE;
4223 static tree
4224 build_new_method_call (instance, name, args, basetype_path, flags)
4225 tree instance, name, args, basetype_path;
4226 int flags;
4228 struct z_candidate *candidates = 0, *cand;
4229 tree explicit_targs = NULL_TREE;
4230 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4231 tree pretty_name;
4232 tree user_args = args;
4233 tree templates = NULL_TREE;
4234 int template_only = 0;
4236 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4238 explicit_targs = TREE_OPERAND (name, 1);
4239 name = TREE_OPERAND (name, 0);
4240 if (DECL_P (name))
4241 name = DECL_NAME (name);
4242 else
4244 if (TREE_CODE (name) == COMPONENT_REF)
4245 name = TREE_OPERAND (name, 1);
4246 if (TREE_CODE (name) == OVERLOAD)
4247 name = DECL_NAME (OVL_CURRENT (name));
4250 template_only = 1;
4253 user_args = args;
4254 args = resolve_args (args);
4256 if (args == error_mark_node)
4257 return error_mark_node;
4259 if (instance == NULL_TREE)
4260 basetype = BINFO_TYPE (basetype_path);
4261 else
4263 if (TREE_CODE (instance) == OFFSET_REF)
4264 instance = resolve_offset_ref (instance);
4265 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4266 instance = convert_from_reference (instance);
4267 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4269 /* XXX this should be handled before we get here. */
4270 if (! IS_AGGR_TYPE (basetype))
4272 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4273 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4274 name, instance, basetype);
4276 return error_mark_node;
4280 if (basetype_path == NULL_TREE)
4281 basetype_path = TYPE_BINFO (basetype);
4283 if (instance)
4285 instance_ptr = build_this (instance);
4287 if (! template_only)
4289 /* XXX this should be handled before we get here. */
4290 fns = build_field_call (basetype_path, instance_ptr, name, args);
4291 if (fns)
4292 return fns;
4295 else
4297 instance_ptr = build_int_2 (0, 0);
4298 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4301 /* Callers should explicitly indicate whether they want to construct
4302 the complete object or just the part without virtual bases. */
4303 my_friendly_assert (name != ctor_identifier, 20000408);
4304 /* Similarly for destructors. */
4305 my_friendly_assert (name != dtor_identifier, 20000408);
4307 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4309 int constructor_p;
4311 constructor_p = (name == complete_ctor_identifier
4312 || name == base_ctor_identifier);
4313 pretty_name = (constructor_p
4314 ? constructor_name (basetype) : dtor_identifier);
4316 if (!flag_new_abi)
4318 /* Add the in-charge parameter as an implicit first argument. */
4319 if (!constructor_p
4320 || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4321 args = tree_cons (NULL_TREE,
4322 in_charge_arg_for_name (name),
4323 args);
4325 /* We want to call the normal constructor function under the
4326 old ABI. */
4327 name = constructor_p ? ctor_identifier : dtor_identifier;
4329 /* If we're call a subobject constructor or destructor for a
4330 subobject that uses virtual base classes, then we need to
4331 pass down a pointer to a VTT for the subobject. */
4332 else if ((name == base_ctor_identifier
4333 || name == base_dtor_identifier)
4334 && TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4336 tree vtt;
4337 tree sub_vtt;
4339 /* If the current function is a complete object constructor
4340 or destructor, then we fetch the VTT directly.
4341 Otherwise, we look it up using the VTT we were given. */
4342 vtt = IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type));
4343 vtt = build_unary_op (ADDR_EXPR, vtt, /*noconvert=*/1);
4344 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4345 DECL_USE_VTT_PARM (current_function_decl),
4346 DECL_VTT_PARM (current_function_decl),
4347 vtt);
4349 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4350 BINFO_SUBVTT_INDEX (basetype_path));
4351 sub_vtt = build_indirect_ref (sub_vtt, NULL);
4353 args = tree_cons (NULL_TREE, sub_vtt, args);
4356 else
4357 pretty_name = name;
4359 fns = lookup_fnfields (basetype_path, name, 1);
4361 if (fns == error_mark_node)
4362 return error_mark_node;
4363 if (fns)
4365 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
4366 tree fn = TREE_VALUE (fns);
4367 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4368 for (; fn; fn = OVL_NEXT (fn))
4370 tree t = OVL_CURRENT (fn);
4371 tree this_arglist;
4373 /* We can end up here for copy-init of same or base class. */
4374 if ((flags & LOOKUP_ONLYCONVERTING)
4375 && DECL_NONCONVERTING_P (t))
4376 continue;
4377 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
4378 this_arglist = mem_args;
4379 else
4380 this_arglist = args;
4382 if (TREE_CODE (t) == TEMPLATE_DECL)
4384 /* A member template. */
4385 templates = tree_cons (NULL_TREE, t, templates);
4386 candidates =
4387 add_template_candidate (candidates, t, base, explicit_targs,
4388 this_arglist,
4389 TREE_TYPE (name), flags, DEDUCE_CALL);
4391 else if (! template_only)
4392 candidates = add_function_candidate (candidates, t, base,
4393 this_arglist, flags);
4395 if (candidates)
4396 candidates->basetype_path = basetype_path;
4400 if (! any_viable (candidates))
4402 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4403 if (flags & LOOKUP_SPECULATIVELY)
4404 return NULL_TREE;
4405 if (!COMPLETE_TYPE_P (basetype))
4406 incomplete_type_error (instance_ptr, basetype);
4407 else
4408 cp_error ("no matching function for call to `%T::%D (%A)%V'",
4409 basetype, pretty_name, user_args,
4410 TREE_TYPE (TREE_TYPE (instance_ptr)));
4411 print_z_candidates (candidates);
4412 return error_mark_node;
4414 candidates = splice_viable (candidates);
4415 cand = tourney (candidates);
4417 if (cand == 0)
4419 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4420 user_args);
4421 print_z_candidates (candidates);
4422 return error_mark_node;
4425 if (DECL_PURE_VIRTUAL_P (cand->fn)
4426 && instance == current_class_ref
4427 && (DECL_CONSTRUCTOR_P (current_function_decl)
4428 || DECL_DESTRUCTOR_P (current_function_decl))
4429 && ! (flags & LOOKUP_NONVIRTUAL)
4430 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4431 cp_error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4432 "abstract virtual `%#D' called from constructor"
4433 : "abstract virtual `%#D' called from destructor"),
4434 cand->fn);
4435 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4436 && is_dummy_object (instance_ptr))
4438 cp_error ("cannot call member function `%D' without object", cand->fn);
4439 return error_mark_node;
4442 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4443 && ((instance == current_class_ref && (dtor_label || ctor_label))
4444 || resolves_to_fixed_type_p (instance, 0)))
4445 flags |= LOOKUP_NONVIRTUAL;
4447 /* Pedantically, normal function declarations are never considered
4448 to refer to template instantiations, so we only do this with
4449 -fguiding-decls. */
4450 if (flag_guiding_decls && templates && ! cand->template
4451 && ! DECL_INITIAL (cand->fn))
4452 add_maybe_template (cand->fn, templates);
4454 return build_over_call
4455 (cand,
4456 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
4457 flags);
4460 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4461 subsequence of ICS2. */
4463 static int
4464 is_subseq (ics1, ics2)
4465 tree ics1, ics2;
4467 /* We can assume that a conversion of the same code
4468 between the same types indicates a subsequence since we only get
4469 here if the types we are converting from are the same. */
4471 while (TREE_CODE (ics1) == RVALUE_CONV
4472 || TREE_CODE (ics1) == LVALUE_CONV)
4473 ics1 = TREE_OPERAND (ics1, 0);
4475 while (1)
4477 while (TREE_CODE (ics2) == RVALUE_CONV
4478 || TREE_CODE (ics2) == LVALUE_CONV)
4479 ics2 = TREE_OPERAND (ics2, 0);
4481 if (TREE_CODE (ics2) == USER_CONV
4482 || TREE_CODE (ics2) == AMBIG_CONV
4483 || TREE_CODE (ics2) == IDENTITY_CONV)
4484 /* At this point, ICS1 cannot be a proper subsequence of
4485 ICS2. We can get a USER_CONV when we are comparing the
4486 second standard conversion sequence of two user conversion
4487 sequences. */
4488 return 0;
4490 ics2 = TREE_OPERAND (ics2, 0);
4492 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4493 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4494 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4495 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4496 return 1;
4500 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4501 be any _TYPE nodes. */
4504 is_properly_derived_from (derived, base)
4505 tree derived;
4506 tree base;
4508 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4509 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4510 return 0;
4512 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4513 considers every class derived from itself. */
4514 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4515 && DERIVED_FROM_P (base, derived));
4518 /* We build the ICS for an implicit object parameter as a pointer
4519 conversion sequence. However, such a sequence should be compared
4520 as if it were a reference conversion sequence. If ICS is the
4521 implicit conversion sequence for an implicit object parameter,
4522 modify it accordingly. */
4524 static void
4525 maybe_handle_implicit_object (ics)
4526 tree* ics;
4528 if (ICS_THIS_FLAG (*ics))
4530 /* [over.match.funcs]
4532 For non-static member functions, the type of the
4533 implicit object parameter is "reference to cv X"
4534 where X is the class of which the function is a
4535 member and cv is the cv-qualification on the member
4536 function declaration. */
4537 tree t = *ics;
4538 tree reference_type;
4540 /* The `this' parameter is a pointer to a class type. Make the
4541 implict conversion talk about a reference to that same class
4542 type. */
4543 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4544 reference_type = build_reference_type (reference_type);
4546 if (TREE_CODE (t) == QUAL_CONV)
4547 t = TREE_OPERAND (t, 0);
4548 if (TREE_CODE (t) == PTR_CONV)
4549 t = TREE_OPERAND (t, 0);
4550 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4551 t = direct_reference_binding (reference_type, t);
4552 *ics = t;
4556 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
4557 to the type the reference originally referred to, and return 1.
4558 Otherwise, return 0. */
4560 static int
4561 maybe_handle_ref_bind (ics, target_type)
4562 tree* ics;
4563 tree* target_type;
4565 if (TREE_CODE (*ics) == REF_BIND)
4567 *target_type = TREE_TYPE (TREE_TYPE (*ics));
4568 *ics = TREE_OPERAND (*ics, 0);
4569 return 1;
4572 return 0;
4575 /* Compare two implicit conversion sequences according to the rules set out in
4576 [over.ics.rank]. Return values:
4578 1: ics1 is better than ics2
4579 -1: ics2 is better than ics1
4580 0: ics1 and ics2 are indistinguishable */
4582 static int
4583 compare_ics (ics1, ics2)
4584 tree ics1, ics2;
4586 tree from_type1;
4587 tree from_type2;
4588 tree to_type1;
4589 tree to_type2;
4590 tree deref_from_type1 = NULL_TREE;
4591 tree deref_from_type2 = NULL_TREE;
4592 tree deref_to_type1 = NULL_TREE;
4593 tree deref_to_type2 = NULL_TREE;
4595 /* REF_BINDING is non-zero if the result of the conversion sequence
4596 is a reference type. In that case TARGET_TYPE is the
4597 type referred to by the reference. */
4598 int ref_binding1;
4599 int ref_binding2;
4600 tree target_type1;
4601 tree target_type2;
4603 /* Handle implicit object parameters. */
4604 maybe_handle_implicit_object (&ics1);
4605 maybe_handle_implicit_object (&ics2);
4607 /* Handle reference parameters. */
4608 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
4609 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
4611 /* [over.ics.rank]
4613 When comparing the basic forms of implicit conversion sequences (as
4614 defined in _over.best.ics_)
4616 --a standard conversion sequence (_over.ics.scs_) is a better
4617 conversion sequence than a user-defined conversion sequence
4618 or an ellipsis conversion sequence, and
4620 --a user-defined conversion sequence (_over.ics.user_) is a
4621 better conversion sequence than an ellipsis conversion sequence
4622 (_over.ics.ellipsis_). */
4623 if (ICS_RANK (ics1) > ICS_RANK (ics2))
4624 return -1;
4625 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
4626 return 1;
4628 if (ICS_RANK (ics1) == BAD_RANK)
4630 /* Both ICS are bad. We try to make a decision based on what
4631 would have happenned if they'd been good. */
4632 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4633 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4634 return -1;
4635 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4636 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4637 return 1;
4639 /* We couldn't make up our minds; try to figure it out below. */
4642 if (ICS_ELLIPSIS_FLAG (ics1))
4643 /* Both conversions are ellipsis conversions. */
4644 return 0;
4646 /* User-defined conversion sequence U1 is a better conversion sequence
4647 than another user-defined conversion sequence U2 if they contain the
4648 same user-defined conversion operator or constructor and if the sec-
4649 ond standard conversion sequence of U1 is better than the second
4650 standard conversion sequence of U2. */
4652 if (ICS_USER_FLAG (ics1))
4654 tree t1, t2;
4656 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4657 if (TREE_CODE (t1) == AMBIG_CONV)
4658 return 0;
4659 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4660 if (TREE_CODE (t2) == AMBIG_CONV)
4661 return 0;
4663 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4664 return 0;
4666 /* We can just fall through here, after setting up
4667 FROM_TYPE1 and FROM_TYPE2. */
4668 from_type1 = TREE_TYPE (t1);
4669 from_type2 = TREE_TYPE (t2);
4671 else
4673 /* We're dealing with two standard conversion sequences.
4675 [over.ics.rank]
4677 Standard conversion sequence S1 is a better conversion
4678 sequence than standard conversion sequence S2 if
4680 --S1 is a proper subsequence of S2 (comparing the conversion
4681 sequences in the canonical form defined by _over.ics.scs_,
4682 excluding any Lvalue Transformation; the identity
4683 conversion sequence is considered to be a subsequence of
4684 any non-identity conversion sequence */
4686 from_type1 = ics1;
4687 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4688 from_type1 = TREE_OPERAND (from_type1, 0);
4689 from_type1 = TREE_TYPE (from_type1);
4691 from_type2 = ics2;
4692 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4693 from_type2 = TREE_OPERAND (from_type2, 0);
4694 from_type2 = TREE_TYPE (from_type2);
4697 if (same_type_p (from_type1, from_type2))
4699 if (is_subseq (ics1, ics2))
4700 return 1;
4701 if (is_subseq (ics2, ics1))
4702 return -1;
4704 /* Otherwise, one sequence cannot be a subsequence of the other; they
4705 don't start with the same type. This can happen when comparing the
4706 second standard conversion sequence in two user-defined conversion
4707 sequences. */
4709 /* [over.ics.rank]
4711 Or, if not that,
4713 --the rank of S1 is better than the rank of S2 (by the rules
4714 defined below):
4716 Standard conversion sequences are ordered by their ranks: an Exact
4717 Match is a better conversion than a Promotion, which is a better
4718 conversion than a Conversion.
4720 Two conversion sequences with the same rank are indistinguishable
4721 unless one of the following rules applies:
4723 --A conversion that is not a conversion of a pointer, or pointer
4724 to member, to bool is better than another conversion that is such
4725 a conversion.
4727 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4728 so that we do not have to check it explicitly. */
4729 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4730 return 1;
4731 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4732 return -1;
4734 to_type1 = TREE_TYPE (ics1);
4735 to_type2 = TREE_TYPE (ics2);
4737 if (TYPE_PTR_P (from_type1)
4738 && TYPE_PTR_P (from_type2)
4739 && TYPE_PTR_P (to_type1)
4740 && TYPE_PTR_P (to_type2))
4742 deref_from_type1 = TREE_TYPE (from_type1);
4743 deref_from_type2 = TREE_TYPE (from_type2);
4744 deref_to_type1 = TREE_TYPE (to_type1);
4745 deref_to_type2 = TREE_TYPE (to_type2);
4747 /* The rules for pointers to members A::* are just like the rules
4748 for pointers A*, except opposite: if B is derived from A then
4749 A::* converts to B::*, not vice versa. For that reason, we
4750 switch the from_ and to_ variables here. */
4751 else if (TYPE_PTRMEM_P (from_type1)
4752 && TYPE_PTRMEM_P (from_type2)
4753 && TYPE_PTRMEM_P (to_type1)
4754 && TYPE_PTRMEM_P (to_type2))
4756 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4757 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4758 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4759 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4761 else if (TYPE_PTRMEMFUNC_P (from_type1)
4762 && TYPE_PTRMEMFUNC_P (from_type2)
4763 && TYPE_PTRMEMFUNC_P (to_type1)
4764 && TYPE_PTRMEMFUNC_P (to_type2))
4766 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4767 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4768 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4769 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4772 if (deref_from_type1 != NULL_TREE
4773 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4774 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4776 /* This was one of the pointer or pointer-like conversions.
4778 [over.ics.rank]
4780 --If class B is derived directly or indirectly from class A,
4781 conversion of B* to A* is better than conversion of B* to
4782 void*, and conversion of A* to void* is better than
4783 conversion of B* to void*. */
4784 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4785 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4787 if (is_properly_derived_from (deref_from_type1,
4788 deref_from_type2))
4789 return -1;
4790 else if (is_properly_derived_from (deref_from_type2,
4791 deref_from_type1))
4792 return 1;
4794 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4795 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4797 if (same_type_p (deref_from_type1, deref_from_type2))
4799 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4801 if (is_properly_derived_from (deref_from_type1,
4802 deref_to_type1))
4803 return 1;
4805 /* We know that DEREF_TO_TYPE1 is `void' here. */
4806 else if (is_properly_derived_from (deref_from_type1,
4807 deref_to_type2))
4808 return -1;
4811 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4812 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4814 /* [over.ics.rank]
4816 --If class B is derived directly or indirectly from class A
4817 and class C is derived directly or indirectly from B,
4819 --conversion of C* to B* is better than conversion of C* to
4820 A*,
4822 --conversion of B* to A* is better than conversion of C* to
4823 A* */
4824 if (same_type_p (deref_from_type1, deref_from_type2))
4826 if (is_properly_derived_from (deref_to_type1,
4827 deref_to_type2))
4828 return 1;
4829 else if (is_properly_derived_from (deref_to_type2,
4830 deref_to_type1))
4831 return -1;
4833 else if (same_type_p (deref_to_type1, deref_to_type2))
4835 if (is_properly_derived_from (deref_from_type2,
4836 deref_from_type1))
4837 return 1;
4838 else if (is_properly_derived_from (deref_from_type1,
4839 deref_from_type2))
4840 return -1;
4844 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4845 && same_type_p (from_type1, from_type2))
4847 /* [over.ics.rank]
4849 --binding of an expression of type C to a reference of type
4850 B& is better than binding an expression of type C to a
4851 reference of type A&
4853 --conversion of C to B is better than conversion of C to A, */
4854 if (is_properly_derived_from (from_type1, to_type1)
4855 && is_properly_derived_from (from_type1, to_type2))
4857 if (is_properly_derived_from (to_type1, to_type2))
4858 return 1;
4859 else if (is_properly_derived_from (to_type2, to_type1))
4860 return -1;
4863 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4864 && same_type_p (to_type1, to_type2))
4866 /* [over.ics.rank]
4868 --binding of an expression of type B to a reference of type
4869 A& is better than binding an expression of type C to a
4870 reference of type A&,
4872 --onversion of B to A is better than conversion of C to A */
4873 if (is_properly_derived_from (from_type1, to_type1)
4874 && is_properly_derived_from (from_type2, to_type1))
4876 if (is_properly_derived_from (from_type2, from_type1))
4877 return 1;
4878 else if (is_properly_derived_from (from_type1, from_type2))
4879 return -1;
4883 /* [over.ics.rank]
4885 --S1 and S2 differ only in their qualification conversion and yield
4886 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4887 qualification signature of type T1 is a proper subset of the cv-
4888 qualification signature of type T2 */
4889 if (TREE_CODE (ics1) == QUAL_CONV
4890 && TREE_CODE (ics2) == QUAL_CONV
4891 && same_type_p (from_type1, from_type2))
4892 return comp_cv_qual_signature (to_type1, to_type2);
4894 /* [over.ics.rank]
4896 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4897 types to which the references refer are the same type except for
4898 top-level cv-qualifiers, and the type to which the reference
4899 initialized by S2 refers is more cv-qualified than the type to
4900 which the reference initialized by S1 refers */
4902 if (ref_binding1 && ref_binding2
4903 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
4904 return comp_cv_qualification (target_type2, target_type1);
4906 /* Neither conversion sequence is better than the other. */
4907 return 0;
4910 /* The source type for this standard conversion sequence. */
4912 static tree
4913 source_type (t)
4914 tree t;
4916 for (;; t = TREE_OPERAND (t, 0))
4918 if (TREE_CODE (t) == USER_CONV
4919 || TREE_CODE (t) == AMBIG_CONV
4920 || TREE_CODE (t) == IDENTITY_CONV)
4921 return TREE_TYPE (t);
4923 my_friendly_abort (1823);
4926 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4927 a pointer to LOSER and re-running joust to produce the warning if WINNER
4928 is actually used. */
4930 static void
4931 add_warning (winner, loser)
4932 struct z_candidate *winner, *loser;
4934 winner->warnings = tree_cons (NULL_PTR,
4935 build_expr_ptr_wrapper (loser),
4936 winner->warnings);
4939 /* Returns true iff functions are equivalent. Equivalent functions are
4940 not identical only if one is a function-local extern function. */
4942 static inline int
4943 equal_functions (fn1, fn2)
4944 tree fn1;
4945 tree fn2;
4947 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2))
4948 return decls_match (fn1, fn2);
4949 return fn1 == fn2;
4952 /* Compare two candidates for overloading as described in
4953 [over.match.best]. Return values:
4955 1: cand1 is better than cand2
4956 -1: cand2 is better than cand1
4957 0: cand1 and cand2 are indistinguishable */
4959 static int
4960 joust (cand1, cand2, warn)
4961 struct z_candidate *cand1, *cand2;
4962 int warn;
4964 int winner = 0;
4965 int i, off1 = 0, off2 = 0, len;
4967 /* Candidates that involve bad conversions are always worse than those
4968 that don't. */
4969 if (cand1->viable > cand2->viable)
4970 return 1;
4971 if (cand1->viable < cand2->viable)
4972 return -1;
4974 /* If we have two pseudo-candidates for conversions to the same type,
4975 or two candidates for the same function, arbitrarily pick one. */
4976 if (cand1->fn == cand2->fn
4977 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
4978 return 1;
4980 /* a viable function F1
4981 is defined to be a better function than another viable function F2 if
4982 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4983 ICSi(F2), and then */
4985 /* for some argument j, ICSj(F1) is a better conversion sequence than
4986 ICSj(F2) */
4988 /* For comparing static and non-static member functions, we ignore
4989 the implicit object parameter of the non-static function. The
4990 standard says to pretend that the static function has an object
4991 parm, but that won't work with operator overloading. */
4992 len = TREE_VEC_LENGTH (cand1->convs);
4993 if (len != TREE_VEC_LENGTH (cand2->convs))
4995 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4996 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4997 off2 = 1;
4998 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4999 && DECL_STATIC_FUNCTION_P (cand2->fn))
5001 off1 = 1;
5002 --len;
5004 else
5005 my_friendly_abort (42);
5008 for (i = 0; i < len; ++i)
5010 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5011 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5012 int comp = compare_ics (t1, t2);
5014 if (comp != 0)
5016 if (warn_sign_promo
5017 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5018 && TREE_CODE (t1) == STD_CONV
5019 && TREE_CODE (t2) == STD_CONV
5020 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5021 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5022 && (TYPE_PRECISION (TREE_TYPE (t1))
5023 == TYPE_PRECISION (TREE_TYPE (t2)))
5024 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5025 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5026 == ENUMERAL_TYPE)))
5028 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5029 tree type1, type2;
5030 struct z_candidate *w, *l;
5031 if (comp > 0)
5032 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5033 w = cand1, l = cand2;
5034 else
5035 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5036 w = cand2, l = cand1;
5038 if (warn)
5040 cp_warning ("passing `%T' chooses `%T' over `%T'",
5041 type, type1, type2);
5042 cp_warning (" in call to `%D'", w->fn);
5044 else
5045 add_warning (w, l);
5048 if (winner && comp != winner)
5050 winner = 0;
5051 goto tweak;
5053 winner = comp;
5057 /* warn about confusing overload resolution for user-defined conversions,
5058 either between a constructor and a conversion op, or between two
5059 conversion ops. */
5060 if (winner && cand1->second_conv
5061 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5062 != DECL_CONSTRUCTOR_P (cand2->fn))
5063 /* Don't warn if the two conv ops convert to the same type... */
5064 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5065 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5066 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5068 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5069 if (comp != winner)
5071 struct z_candidate *w, *l;
5072 if (winner == 1)
5073 w = cand1, l = cand2;
5074 else
5075 w = cand2, l = cand1;
5076 if (warn)
5078 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5079 if (! DECL_CONSTRUCTOR_P (w->fn))
5080 source = TREE_TYPE (source);
5081 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
5082 cp_warning (" for conversion from `%T' to `%T'",
5083 source, TREE_TYPE (w->second_conv));
5084 cp_warning (" because conversion sequence for the argument is better");
5086 else
5087 add_warning (w, l);
5091 if (winner)
5092 return winner;
5094 /* or, if not that,
5095 F1 is a non-template function and F2 is a template function */
5097 if (! cand1->template && cand2->template)
5098 return 1;
5099 else if (cand1->template && ! cand2->template)
5100 return -1;
5101 else if (cand1->template && cand2->template)
5102 winner = more_specialized
5103 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5104 NULL_TREE);
5106 /* or, if not that,
5107 the context is an initialization by user-defined conversion (see
5108 _dcl.init_ and _over.match.user_) and the standard conversion
5109 sequence from the return type of F1 to the destination type (i.e.,
5110 the type of the entity being initialized) is a better conversion
5111 sequence than the standard conversion sequence from the return type
5112 of F2 to the destination type. */
5114 if (! winner && cand1->second_conv)
5115 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5117 /* If the built-in candidates are the same, arbitrarily pick one. */
5118 if (! winner && cand1->fn == cand2->fn
5119 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5121 for (i = 0; i < len; ++i)
5122 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5123 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5124 break;
5125 if (i == TREE_VEC_LENGTH (cand1->convs))
5126 return 1;
5128 /* Kludge around broken overloading rules whereby
5129 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
5130 that takes references and another that takes values. */
5131 if (cand1->fn == ansi_opname (COND_EXPR))
5133 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
5134 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
5135 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
5136 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
5138 if (same_type_p (t1, t2))
5140 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
5141 return 1;
5142 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
5143 return -1;
5148 /* If the two functions are the same (this can happen with declarations
5149 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5150 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5151 && equal_functions (cand1->fn, cand2->fn))
5152 return 1;
5154 tweak:
5156 /* Extension: If the worst conversion for one candidate is worse than the
5157 worst conversion for the other, take the first. */
5158 if (! winner && ! pedantic)
5160 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5162 for (i = 0; i < len; ++i)
5164 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5165 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5166 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5167 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5170 if (rank1 < rank2)
5171 return 1;
5172 if (rank1 > rank2)
5173 return -1;
5176 return winner;
5179 /* Given a list of candidates for overloading, find the best one, if any.
5180 This algorithm has a worst case of O(2n) (winner is last), and a best
5181 case of O(n/2) (totally ambiguous); much better than a sorting
5182 algorithm. */
5184 static struct z_candidate *
5185 tourney (candidates)
5186 struct z_candidate *candidates;
5188 struct z_candidate *champ = candidates, *challenger;
5189 int fate;
5190 int champ_compared_to_predecessor = 0;
5192 /* Walk through the list once, comparing each current champ to the next
5193 candidate, knocking out a candidate or two with each comparison. */
5195 for (challenger = champ->next; challenger; )
5197 fate = joust (champ, challenger, 0);
5198 if (fate == 1)
5199 challenger = challenger->next;
5200 else
5202 if (fate == 0)
5204 champ = challenger->next;
5205 if (champ == 0)
5206 return 0;
5207 champ_compared_to_predecessor = 0;
5209 else
5211 champ = challenger;
5212 champ_compared_to_predecessor = 1;
5215 challenger = champ->next;
5219 /* Make sure the champ is better than all the candidates it hasn't yet
5220 been compared to. */
5222 for (challenger = candidates;
5223 challenger != champ
5224 && !(champ_compared_to_predecessor && challenger->next == champ);
5225 challenger = challenger->next)
5227 fate = joust (champ, challenger, 0);
5228 if (fate != 1)
5229 return 0;
5232 return champ;
5235 /* Returns non-zero if things of type FROM can be converted to TO. */
5238 can_convert (to, from)
5239 tree to, from;
5241 return can_convert_arg (to, from, NULL_TREE);
5244 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5247 can_convert_arg (to, from, arg)
5248 tree to, from, arg;
5250 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5251 return (t && ! ICS_BAD_FLAG (t));
5254 /* Convert EXPR to TYPE. Return the converted expression. */
5256 tree
5257 perform_implicit_conversion (type, expr)
5258 tree type;
5259 tree expr;
5261 tree conv;
5263 if (expr == error_mark_node)
5264 return error_mark_node;
5265 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5266 LOOKUP_NORMAL);
5267 if (!conv || ICS_BAD_FLAG (conv))
5269 cp_error ("could not convert `%E' to `%T'", expr, type);
5270 return error_mark_node;
5273 return convert_like (conv, expr);
5276 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5277 initializing a variable of that TYPE. Return the converted
5278 expression. */
5280 tree
5281 initialize_reference (type, expr)
5282 tree type;
5283 tree expr;
5285 tree conv;
5287 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5288 if (!conv || ICS_BAD_FLAG (conv))
5290 cp_error ("could not convert `%E' to `%T'", expr, type);
5291 return error_mark_node;
5294 return convert_like (conv, expr);