* configure.in: Arrange to include defaults.h in [ht]config.h/tm.h.
[official-gcc.git] / gcc / cp / call.c
blob98beeec753aa89c59ed5620a2e3f50f5a8ec3abc
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 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 "expr.h"
36 #include "ggc.h"
38 extern int inhibit_warnings;
40 static tree build_new_method_call PARAMS ((tree, tree, tree, tree, int));
42 static tree build_field_call PARAMS ((tree, tree, tree, tree));
43 static struct z_candidate * tourney PARAMS ((struct z_candidate *));
44 static int equal_functions PARAMS ((tree, tree));
45 static int joust PARAMS ((struct z_candidate *, struct z_candidate *, int));
46 static int compare_ics PARAMS ((tree, tree));
47 static tree build_over_call PARAMS ((struct z_candidate *, tree, int));
48 #define convert_like(CONV, EXPR) convert_like_real (CONV, EXPR, NULL_TREE, 0, 0)
49 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) convert_like_real (CONV, EXPR, FN, ARGNO, 0)
50 static tree convert_like_real PARAMS ((tree, tree, tree, int, int));
51 static void op_error PARAMS ((enum tree_code, enum tree_code, tree, tree,
52 tree, const char *));
53 static tree build_object_call PARAMS ((tree, tree));
54 static tree resolve_args PARAMS ((tree));
55 static struct z_candidate * build_user_type_conversion_1
56 PARAMS ((tree, tree, int));
57 static void print_z_candidates PARAMS ((struct z_candidate *));
58 static tree build_this PARAMS ((tree));
59 static struct z_candidate * splice_viable PARAMS ((struct z_candidate *));
60 static int any_viable PARAMS ((struct z_candidate *));
61 static struct z_candidate * add_template_candidate
62 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
63 unification_kind_t));
64 static struct z_candidate * add_template_candidate_real
65 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
66 tree, unification_kind_t));
67 static struct z_candidate * add_template_conv_candidate
68 PARAMS ((struct z_candidate *, tree, tree, tree, tree));
69 static struct z_candidate * add_builtin_candidates
70 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
71 tree, tree *, int));
72 static struct z_candidate * add_builtin_candidate
73 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
74 tree, tree, tree, tree *, tree *, int));
75 static int is_complete PARAMS ((tree));
76 static struct z_candidate * build_builtin_candidate
77 PARAMS ((struct z_candidate *, tree, tree, tree, tree *, tree *,
78 int));
79 static struct z_candidate * add_conv_candidate
80 PARAMS ((struct z_candidate *, tree, tree, tree));
81 static struct z_candidate * add_function_candidate
82 PARAMS ((struct z_candidate *, tree, tree, tree, int));
83 static tree implicit_conversion PARAMS ((tree, tree, tree, int));
84 static tree standard_conversion PARAMS ((tree, tree, tree));
85 static tree reference_binding PARAMS ((tree, tree, tree, int));
86 static tree non_reference PARAMS ((tree));
87 static tree build_conv PARAMS ((enum tree_code, tree, tree));
88 static int is_subseq PARAMS ((tree, tree));
89 static int maybe_handle_ref_bind PARAMS ((tree*, tree*));
90 static void maybe_handle_implicit_object PARAMS ((tree*));
91 static struct z_candidate * add_candidate PARAMS ((struct z_candidate *,
92 tree, tree, int));
93 static tree source_type PARAMS ((tree));
94 static void add_warning PARAMS ((struct z_candidate *, struct z_candidate *));
95 static int reference_related_p PARAMS ((tree, tree));
96 static int reference_compatible_p PARAMS ((tree, tree));
97 static tree convert_class_to_reference PARAMS ((tree, tree, tree));
98 static tree direct_reference_binding PARAMS ((tree, tree));
99 static int promoted_arithmetic_type_p PARAMS ((tree));
100 static tree conditional_conversion PARAMS ((tree, tree));
102 tree
103 build_vfield_ref (datum, type)
104 tree datum, type;
106 tree rval;
108 if (datum == error_mark_node)
109 return error_mark_node;
111 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
112 datum = convert_from_reference (datum);
114 if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type))
115 rval = build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
116 datum, TYPE_VFIELD (type));
117 else
118 rval = build_component_ref (datum, DECL_NAME (TYPE_VFIELD (type)), NULL_TREE, 0);
120 return rval;
123 /* Build a call to a member of an object. I.e., one that overloads
124 operator ()(), or is a pointer-to-function or pointer-to-method. */
126 static tree
127 build_field_call (basetype_path, instance_ptr, name, parms)
128 tree basetype_path, instance_ptr, name, parms;
130 tree field, instance;
132 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
133 return NULL_TREE;
135 /* Speed up the common case. */
136 if (instance_ptr == current_class_ptr
137 && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
138 return NULL_TREE;
140 field = lookup_field (basetype_path, name, 1, 0);
142 if (field == error_mark_node || field == NULL_TREE)
143 return field;
145 if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
147 /* If it's a field, try overloading operator (),
148 or calling if the field is a pointer-to-function. */
149 instance = build_indirect_ref (instance_ptr, NULL_PTR);
150 instance = build_component_ref_1 (instance, field, 0);
152 if (instance == error_mark_node)
153 return error_mark_node;
155 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
156 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
157 instance, parms, NULL_TREE);
158 else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
159 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
160 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
161 == FUNCTION_TYPE)))
162 return build_function_call (instance, parms);
165 return NULL_TREE;
168 /* Returns nonzero iff the destructor name specified in NAME
169 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
170 forms... */
173 check_dtor_name (basetype, name)
174 tree basetype, name;
176 name = TREE_OPERAND (name, 0);
178 /* Just accept something we've already complained about. */
179 if (name == error_mark_node)
180 return 1;
182 if (TREE_CODE (name) == TYPE_DECL)
183 name = TREE_TYPE (name);
184 else if (TYPE_P (name))
185 /* OK */;
186 else if (TREE_CODE (name) == IDENTIFIER_NODE)
188 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
189 || (TREE_CODE (basetype) == ENUMERAL_TYPE
190 && name == TYPE_IDENTIFIER (basetype)))
191 name = basetype;
192 else
193 name = get_type_value (name);
195 else
196 my_friendly_abort (980605);
198 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
199 return 1;
200 return 0;
203 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
204 This is how virtual function calls are avoided. */
206 tree
207 build_scoped_method_call (exp, basetype, name, parms)
208 tree exp, basetype, name, parms;
210 /* Because this syntactic form does not allow
211 a pointer to a base class to be `stolen',
212 we need not protect the derived->base conversion
213 that happens here.
215 @@ But we do have to check access privileges later. */
216 tree binfo, decl;
217 tree type = TREE_TYPE (exp);
219 if (type == error_mark_node
220 || basetype == error_mark_node)
221 return error_mark_node;
223 if (processing_template_decl)
225 if (TREE_CODE (name) == BIT_NOT_EXPR
226 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
228 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
229 if (type)
230 name = build_min_nt (BIT_NOT_EXPR, type);
232 name = build_min_nt (SCOPE_REF, basetype, name);
233 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
236 if (TREE_CODE (type) == REFERENCE_TYPE)
237 type = TREE_TYPE (type);
239 if (TREE_CODE (basetype) == TREE_VEC)
241 binfo = basetype;
242 basetype = BINFO_TYPE (binfo);
244 else
245 binfo = NULL_TREE;
247 /* Check the destructor call syntax. */
248 if (TREE_CODE (name) == BIT_NOT_EXPR)
250 /* We can get here if someone writes their destructor call like
251 `obj.NS::~T()'; this isn't really a scoped method call, so hand
252 it off. */
253 if (TREE_CODE (basetype) == NAMESPACE_DECL)
254 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
256 if (! check_dtor_name (basetype, name))
257 cp_error ("qualified type `%T' does not match destructor name `~%T'",
258 basetype, TREE_OPERAND (name, 0));
260 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
261 that explicit ~int is caught in the parser; this deals with typedefs
262 and template parms. */
263 if (! IS_AGGR_TYPE (basetype))
265 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
266 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
267 exp, basetype, type);
269 return cp_convert (void_type_node, exp);
273 if (TREE_CODE (basetype) == NAMESPACE_DECL)
275 cp_error ("`%D' is a namespace", basetype);
276 return error_mark_node;
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 These may have been declared normally if we're building libgcc,
402 so we can't just check DECL_ARTIFICIAL. */
403 if (DECL_ARTIFICIAL (decl)
404 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
405 mark_used (decl);
406 else
407 my_friendly_abort (990125);
410 /* Don't pass empty class objects by value. This is useful
411 for tags in STL, which are used to control overload resolution.
412 We don't need to handle other cases of copying empty classes. */
413 if (! decl || ! DECL_BUILT_IN (decl))
414 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
415 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
416 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
418 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
419 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
420 TREE_VALUE (tmp), t);
423 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
424 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
425 TREE_TYPE (function) = result_type;
426 TREE_SIDE_EFFECTS (function) = 1;
427 TREE_NOTHROW (function) = nothrow;
429 return function;
432 /* Build something of the form ptr->method (args)
433 or object.method (args). This can also build
434 calls to constructors, and find friends.
436 Member functions always take their class variable
437 as a pointer.
439 INSTANCE is a class instance.
441 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
443 PARMS help to figure out what that NAME really refers to.
445 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
446 down to the real instance type to use for access checking. We need this
447 information to get protected accesses correct. This parameter is used
448 by build_member_call.
450 FLAGS is the logical disjunction of zero or more LOOKUP_
451 flags. See cp-tree.h for more info.
453 If this is all OK, calls build_function_call with the resolved
454 member function.
456 This function must also handle being called to perform
457 initialization, promotion/coercion of arguments, and
458 instantiation of default parameters.
460 Note that NAME may refer to an instance variable name. If
461 `operator()()' is defined for the type of that field, then we return
462 that result. */
464 #ifdef GATHER_STATISTICS
465 extern int n_build_method_call;
466 #endif
468 tree
469 build_method_call (instance, name, parms, basetype_path, flags)
470 tree instance, name, parms, basetype_path;
471 int flags;
473 tree basetype, instance_ptr;
475 #ifdef GATHER_STATISTICS
476 n_build_method_call++;
477 #endif
479 if (instance == error_mark_node
480 || name == error_mark_node
481 || parms == error_mark_node
482 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
483 return error_mark_node;
485 if (processing_template_decl)
487 /* We need to process template parm names here so that tsubst catches
488 them properly. Other type names can wait. */
489 if (TREE_CODE (name) == BIT_NOT_EXPR)
491 tree type = NULL_TREE;
493 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
494 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
495 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
496 type = TREE_TYPE (TREE_OPERAND (name, 0));
498 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
499 name = build_min_nt (BIT_NOT_EXPR, type);
502 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
505 if (TREE_CODE (name) == BIT_NOT_EXPR)
507 if (parms)
508 error ("destructors take no parameters");
509 basetype = TREE_TYPE (instance);
510 if (TREE_CODE (basetype) == REFERENCE_TYPE)
511 basetype = TREE_TYPE (basetype);
513 if (! check_dtor_name (basetype, name))
514 cp_error
515 ("destructor name `~%T' does not match type `%T' of expression",
516 TREE_OPERAND (name, 0), basetype);
518 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
519 return cp_convert (void_type_node, instance);
520 instance = default_conversion (instance);
521 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
522 return build_delete (build_pointer_type (basetype),
523 instance_ptr, sfk_complete_destructor,
524 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
527 return build_new_method_call (instance, name, parms, basetype_path, flags);
530 /* New overloading code. */
532 struct z_candidate {
533 tree fn;
534 tree convs;
535 tree second_conv;
536 int viable;
537 tree basetype_path;
538 tree template;
539 tree warnings;
540 struct z_candidate *next;
543 #define IDENTITY_RANK 0
544 #define EXACT_RANK 1
545 #define PROMO_RANK 2
546 #define STD_RANK 3
547 #define PBOOL_RANK 4
548 #define USER_RANK 5
549 #define ELLIPSIS_RANK 6
550 #define BAD_RANK 7
552 #define ICS_RANK(NODE) \
553 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
554 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
555 : ICS_USER_FLAG (NODE) ? USER_RANK \
556 : ICS_STD_RANK (NODE))
558 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
560 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
561 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
562 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
563 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
565 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
566 should be created to hold the result of the conversion. */
567 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
569 #define USER_CONV_CAND(NODE) \
570 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
571 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
574 null_ptr_cst_p (t)
575 tree t;
577 /* [conv.ptr]
579 A null pointer constant is an integral constant expression
580 (_expr.const_) rvalue of integer type that evaluates to zero. */
581 if (t == null_node
582 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
583 return 1;
584 return 0;
588 /* Returns non-zero if PARMLIST consists of only default parms and/or
589 ellipsis. */
592 sufficient_parms_p (parmlist)
593 tree parmlist;
595 for (; parmlist && parmlist != void_list_node;
596 parmlist = TREE_CHAIN (parmlist))
597 if (!TREE_PURPOSE (parmlist))
598 return 0;
599 return 1;
602 static tree
603 build_conv (code, type, from)
604 enum tree_code code;
605 tree type, from;
607 tree t;
608 int rank = ICS_STD_RANK (from);
610 /* We can't use buildl1 here because CODE could be USER_CONV, which
611 takes two arguments. In that case, the caller is responsible for
612 filling in the second argument. */
613 t = make_node (code);
614 TREE_TYPE (t) = type;
615 TREE_OPERAND (t, 0) = from;
617 switch (code)
619 case PTR_CONV:
620 case PMEM_CONV:
621 case BASE_CONV:
622 case STD_CONV:
623 if (rank < STD_RANK)
624 rank = STD_RANK;
625 break;
627 case QUAL_CONV:
628 if (rank < EXACT_RANK)
629 rank = EXACT_RANK;
631 default:
632 break;
634 ICS_STD_RANK (t) = rank;
635 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
636 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
637 return t;
640 static tree
641 non_reference (t)
642 tree t;
644 if (TREE_CODE (t) == REFERENCE_TYPE)
645 t = TREE_TYPE (t);
646 return t;
649 tree
650 strip_top_quals (t)
651 tree t;
653 if (TREE_CODE (t) == ARRAY_TYPE)
654 return t;
655 return TYPE_MAIN_VARIANT (t);
658 /* Returns the standard conversion path (see [conv]) from type FROM to type
659 TO, if any. For proper handling of null pointer constants, you must
660 also pass the expression EXPR to convert from. */
662 static tree
663 standard_conversion (to, from, expr)
664 tree to, from, expr;
666 enum tree_code fcode, tcode;
667 tree conv;
668 int fromref = 0;
670 if (TREE_CODE (to) == REFERENCE_TYPE)
671 to = TREE_TYPE (to);
672 if (TREE_CODE (from) == REFERENCE_TYPE)
674 fromref = 1;
675 from = TREE_TYPE (from);
677 to = strip_top_quals (to);
678 from = strip_top_quals (from);
680 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
681 && expr && type_unknown_p (expr))
683 expr = instantiate_type (to, expr, itf_none);
684 if (expr == error_mark_node)
685 return NULL_TREE;
686 from = TREE_TYPE (expr);
689 fcode = TREE_CODE (from);
690 tcode = TREE_CODE (to);
692 conv = build1 (IDENTITY_CONV, from, expr);
694 if (fcode == FUNCTION_TYPE)
696 from = build_pointer_type (from);
697 fcode = TREE_CODE (from);
698 conv = build_conv (LVALUE_CONV, from, conv);
700 else if (fcode == ARRAY_TYPE)
702 from = build_pointer_type (TREE_TYPE (from));
703 fcode = TREE_CODE (from);
704 conv = build_conv (LVALUE_CONV, from, conv);
706 else if (fromref || (expr && lvalue_p (expr)))
707 conv = build_conv (RVALUE_CONV, from, conv);
709 /* Allow conversion between `__complex__' data types */
710 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
712 /* The standard conversion sequence to convert FROM to TO is
713 the standard conversion sequence to perform componentwise
714 conversion. */
715 tree part_conv = standard_conversion
716 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
718 if (part_conv)
720 conv = build_conv (TREE_CODE (part_conv), to, conv);
721 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
723 else
724 conv = NULL_TREE;
726 return conv;
729 if (same_type_p (from, to))
730 return conv;
732 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
733 && expr && null_ptr_cst_p (expr))
735 conv = build_conv (STD_CONV, to, conv);
737 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
739 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
740 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
742 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
743 TREE_TYPE (to)))
745 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
746 && ufcode != FUNCTION_TYPE)
748 from = build_pointer_type
749 (cp_build_qualified_type (void_type_node,
750 CP_TYPE_QUALS (TREE_TYPE (from))));
751 conv = build_conv (PTR_CONV, from, conv);
753 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
755 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
756 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
757 tree binfo = get_binfo (fbase, tbase, 1);
759 if (binfo && !binfo_from_vbase (binfo)
760 && (same_type_ignoring_top_level_qualifiers_p
761 (TREE_TYPE (TREE_TYPE (from)),
762 TREE_TYPE (TREE_TYPE (to)))))
764 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
765 from = build_pointer_type (from);
766 conv = build_conv (PMEM_CONV, from, conv);
769 else if (IS_AGGR_TYPE (TREE_TYPE (from))
770 && IS_AGGR_TYPE (TREE_TYPE (to)))
772 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
774 from =
775 cp_build_qualified_type (TREE_TYPE (to),
776 CP_TYPE_QUALS (TREE_TYPE (from)));
777 from = build_pointer_type (from);
778 conv = build_conv (PTR_CONV, from, conv);
782 if (same_type_p (from, to))
783 /* OK */;
784 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
785 conv = build_conv (QUAL_CONV, to, conv);
786 else if (expr && string_conv_p (to, expr, 0))
787 /* converting from string constant to char *. */
788 conv = build_conv (QUAL_CONV, to, conv);
789 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
791 conv = build_conv (PTR_CONV, to, conv);
792 ICS_BAD_FLAG (conv) = 1;
794 else
795 return 0;
797 from = to;
799 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
801 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
802 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
803 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
804 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
805 tree binfo = get_binfo (fbase, tbase, 1);
807 if (!binfo || binfo_from_vbase (binfo)
808 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
809 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
810 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
811 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
812 return 0;
814 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
815 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
816 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
817 from = build_ptrmemfunc_type (build_pointer_type (from));
818 conv = build_conv (PMEM_CONV, from, conv);
820 else if (tcode == BOOLEAN_TYPE)
822 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
823 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
824 return 0;
826 conv = build_conv (STD_CONV, to, conv);
827 if (fcode == POINTER_TYPE
828 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
829 ICS_STD_RANK (conv) = PBOOL_RANK;
831 /* We don't check for ENUMERAL_TYPE here because there are no standard
832 conversions to enum type. */
833 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
834 || tcode == REAL_TYPE)
836 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
837 return 0;
838 conv = build_conv (STD_CONV, to, conv);
840 /* Give this a better rank if it's a promotion. */
841 if (to == type_promotes_to (from)
842 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
843 ICS_STD_RANK (conv) = PROMO_RANK;
845 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
846 && is_properly_derived_from (from, to))
848 if (TREE_CODE (conv) == RVALUE_CONV)
849 conv = TREE_OPERAND (conv, 0);
850 conv = build_conv (BASE_CONV, to, conv);
851 /* The derived-to-base conversion indicates the initialization
852 of a parameter with base type from an object of a derived
853 type. A temporary object is created to hold the result of
854 the conversion. */
855 NEED_TEMPORARY_P (conv) = 1;
857 else
858 return 0;
860 return conv;
863 /* Returns non-zero if T1 is reference-related to T2. */
865 static int
866 reference_related_p (t1, t2)
867 tree t1;
868 tree t2;
870 t1 = TYPE_MAIN_VARIANT (t1);
871 t2 = TYPE_MAIN_VARIANT (t2);
873 /* [dcl.init.ref]
875 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
876 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
877 of T2. */
878 return (same_type_p (t1, t2)
879 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
880 && DERIVED_FROM_P (t1, t2)));
883 /* Returns non-zero if T1 is reference-compatible with T2. */
885 static int
886 reference_compatible_p (t1, t2)
887 tree t1;
888 tree t2;
890 /* [dcl.init.ref]
892 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
893 reference-related to T2 and cv1 is the same cv-qualification as,
894 or greater cv-qualification than, cv2. */
895 return (reference_related_p (t1, t2)
896 && at_least_as_qualified_p (t1, t2));
899 /* Determine whether or not the EXPR (of class type S) can be
900 converted to T as in [over.match.ref]. */
902 static tree
903 convert_class_to_reference (t, s, expr)
904 tree t;
905 tree s;
906 tree expr;
908 tree conversions;
909 tree arglist;
910 tree conv;
911 struct z_candidate *candidates;
912 struct z_candidate *cand;
914 /* [over.match.ref]
916 Assuming that "cv1 T" is the underlying type of the reference
917 being initialized, and "cv S" is the type of the initializer
918 expression, with S a class type, the candidate functions are
919 selected as follows:
921 --The conversion functions of S and its base classes are
922 considered. Those that are not hidden within S and yield type
923 "reference to cv2 T2", where "cv1 T" is reference-compatible
924 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
926 The argument list has one argument, which is the initializer
927 expression. */
929 candidates = 0;
931 /* Conceptually, we should take the address of EXPR and put it in
932 the argument list. Unfortunately, however, that can result in
933 error messages, which we should not issue now because we are just
934 trying to find a conversion operator. Therefore, we use NULL,
935 cast to the appropriate type. */
936 arglist = build_int_2 (0, 0);
937 TREE_TYPE (arglist) = build_pointer_type (s);
938 arglist = build_tree_list (NULL_TREE, arglist);
940 for (conversions = lookup_conversions (s);
941 conversions;
942 conversions = TREE_CHAIN (conversions))
944 tree fns = TREE_VALUE (conversions);
946 for (; fns; fns = OVL_NEXT (fns))
948 tree f = OVL_CURRENT (fns);
949 tree t2 = TREE_TYPE (TREE_TYPE (f));
950 struct z_candidate *old_candidates = candidates;
952 /* If this is a template function, try to get an exact
953 match. */
954 if (TREE_CODE (f) == TEMPLATE_DECL)
956 candidates
957 = add_template_candidate (candidates,
958 f, s,
959 NULL_TREE,
960 arglist,
961 build_reference_type (t),
962 LOOKUP_NORMAL,
963 DEDUCE_CONV);
965 if (candidates != old_candidates)
967 /* Now, see if the conversion function really returns
968 an lvalue of the appropriate type. From the
969 point of view of unification, simply returning an
970 rvalue of the right type is good enough. */
971 f = candidates->fn;
972 t2 = TREE_TYPE (TREE_TYPE (f));
973 if (TREE_CODE (t2) != REFERENCE_TYPE
974 || !reference_compatible_p (t, TREE_TYPE (t2)))
975 candidates = candidates->next;
978 else if (TREE_CODE (t2) == REFERENCE_TYPE
979 && reference_compatible_p (t, TREE_TYPE (t2)))
980 candidates
981 = add_function_candidate (candidates, f, s, arglist,
982 LOOKUP_NORMAL);
984 if (candidates != old_candidates)
985 candidates->basetype_path = TYPE_BINFO (s);
989 /* If none of the conversion functions worked out, let our caller
990 know. */
991 if (!any_viable (candidates))
992 return NULL_TREE;
994 candidates = splice_viable (candidates);
995 cand = tourney (candidates);
996 if (!cand)
997 return NULL_TREE;
999 conv = build1 (IDENTITY_CONV, s, expr);
1000 conv = build_conv (USER_CONV,
1001 non_reference (TREE_TYPE (TREE_TYPE (cand->fn))),
1002 conv);
1003 TREE_OPERAND (conv, 1) = build_expr_ptr_wrapper (cand);
1004 ICS_USER_FLAG (conv) = 1;
1005 if (cand->viable == -1)
1006 ICS_BAD_FLAG (conv) = 1;
1007 cand->second_conv = conv;
1009 return conv;
1012 /* A reference of the indicated TYPE is being bound directly to the
1013 expression represented by the implicit conversion sequence CONV.
1014 Return a conversion sequence for this binding. */
1016 static tree
1017 direct_reference_binding (type, conv)
1018 tree type;
1019 tree conv;
1021 tree t = TREE_TYPE (type);
1023 /* [over.ics.rank]
1025 When a parameter of reference type binds directly
1026 (_dcl.init.ref_) to an argument expression, the implicit
1027 conversion sequence is the identity conversion, unless the
1028 argument expression has a type that is a derived class of the
1029 parameter type, in which case the implicit conversion sequence is
1030 a derived-to-base Conversion.
1032 If the parameter binds directly to the result of applying a
1033 conversion function to the argument expression, the implicit
1034 conversion sequence is a user-defined conversion sequence
1035 (_over.ics.user_), with the second standard conversion sequence
1036 either an identity conversion or, if the conversion function
1037 returns an entity of a type that is a derived class of the
1038 parameter type, a derived-to-base conversion. */
1039 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1041 /* Represent the derived-to-base conversion. */
1042 conv = build_conv (BASE_CONV, t, conv);
1043 /* We will actually be binding to the base-class subobject in
1044 the derived class, so we mark this conversion appropriately.
1045 That way, convert_like knows not to generate a temporary. */
1046 NEED_TEMPORARY_P (conv) = 0;
1048 return build_conv (REF_BIND, type, conv);
1051 /* Returns the conversion path from type FROM to reference type TO for
1052 purposes of reference binding. For lvalue binding, either pass a
1053 reference type to FROM or an lvalue expression to EXPR. If the
1054 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1055 the conversion returned. */
1057 static tree
1058 reference_binding (rto, rfrom, expr, flags)
1059 tree rto, rfrom, expr;
1060 int flags;
1062 tree conv = NULL_TREE;
1063 tree to = TREE_TYPE (rto);
1064 tree from = rfrom;
1065 int related_p;
1066 int compatible_p;
1067 cp_lvalue_kind lvalue_p = clk_none;
1069 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1071 expr = instantiate_type (to, expr, itf_none);
1072 if (expr == error_mark_node)
1073 return NULL_TREE;
1074 from = TREE_TYPE (expr);
1077 if (TREE_CODE (from) == REFERENCE_TYPE)
1079 /* Anything with reference type is an lvalue. */
1080 lvalue_p = clk_ordinary;
1081 from = TREE_TYPE (from);
1083 else if (expr)
1084 lvalue_p = real_lvalue_p (expr);
1086 /* Figure out whether or not the types are reference-related and
1087 reference compatible. We have do do this after stripping
1088 references from FROM. */
1089 related_p = reference_related_p (to, from);
1090 compatible_p = reference_compatible_p (to, from);
1092 if (lvalue_p && compatible_p)
1094 /* [dcl.init.ref]
1096 If the intializer expression
1098 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1099 is reference-compatible with "cv2 T2,"
1101 the reference is bound directly to the initializer exprssion
1102 lvalue. */
1103 conv = build1 (IDENTITY_CONV, from, expr);
1104 conv = direct_reference_binding (rto, conv);
1105 if ((lvalue_p & clk_bitfield) != 0
1106 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1107 /* For the purposes of overload resolution, we ignore the fact
1108 this expression is a bitfield. (In particular,
1109 [over.ics.ref] says specifically that a function with a
1110 non-const reference parameter is viable even if the
1111 argument is a bitfield.)
1113 However, when we actually call the function we must create
1114 a temporary to which to bind the reference. If the
1115 reference is volatile, or isn't const, then we cannot make
1116 a temporary, so we just issue an error when the conversion
1117 actually occurs. */
1118 NEED_TEMPORARY_P (conv) = 1;
1119 return conv;
1121 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1123 /* [dcl.init.ref]
1125 If the initializer exprsesion
1127 -- has a class type (i.e., T2 is a class type) can be
1128 implicitly converted to an lvalue of type "cv3 T3," where
1129 "cv1 T1" is reference-compatible with "cv3 T3". (this
1130 conversion is selected by enumerating the applicable
1131 conversion functions (_over.match.ref_) and choosing the
1132 best one through overload resolution. (_over.match_).
1134 the reference is bound to the lvalue result of the conversion
1135 in the second case. */
1136 conv = convert_class_to_reference (to, from, expr);
1137 if (conv)
1138 return direct_reference_binding (rto, conv);
1141 /* From this point on, we conceptually need temporaries, even if we
1142 elide them. Only the cases above are "direct bindings". */
1143 if (flags & LOOKUP_NO_TEMP_BIND)
1144 return NULL_TREE;
1146 /* [over.ics.rank]
1148 When a parameter of reference type is not bound directly to an
1149 argument expression, the conversion sequence is the one required
1150 to convert the argument expression to the underlying type of the
1151 reference according to _over.best.ics_. Conceptually, this
1152 conversion sequence corresponds to copy-initializing a temporary
1153 of the underlying type with the argument expression. Any
1154 difference in top-level cv-qualification is subsumed by the
1155 initialization itself and does not constitute a conversion. */
1157 /* [dcl.init.ref]
1159 Otherwise, the reference shall be to a non-volatile const type. */
1160 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1161 return NULL_TREE;
1163 /* [dcl.init.ref]
1165 If the initializer expression is an rvalue, with T2 a class type,
1166 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1167 is bound in one of the following ways:
1169 -- The reference is bound to the object represented by the rvalue
1170 or to a sub-object within that object.
1172 In this case, the implicit conversion sequence is supposed to be
1173 same as we would obtain by generating a temporary. Fortunately,
1174 if the types are reference compatible, then this is either an
1175 identity conversion or the derived-to-base conversion, just as
1176 for direct binding. */
1177 if (CLASS_TYPE_P (from) && compatible_p)
1179 conv = build1 (IDENTITY_CONV, from, expr);
1180 return direct_reference_binding (rto, conv);
1183 /* [dcl.init.ref]
1185 Otherwise, a temporary of type "cv1 T1" is created and
1186 initialized from the initializer expression using the rules for a
1187 non-reference copy initialization. If T1 is reference-related to
1188 T2, cv1 must be the same cv-qualification as, or greater
1189 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1190 if (related_p && !at_least_as_qualified_p (to, from))
1191 return NULL_TREE;
1193 conv = implicit_conversion (to, from, expr, flags);
1194 if (!conv)
1195 return NULL_TREE;
1197 conv = build_conv (REF_BIND, rto, conv);
1198 /* This reference binding, unlike those above, requires the
1199 creation of a temporary. */
1200 NEED_TEMPORARY_P (conv) = 1;
1202 return conv;
1205 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1206 to type TO. The optional expression EXPR may affect the conversion.
1207 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1208 significant. */
1210 static tree
1211 implicit_conversion (to, from, expr, flags)
1212 tree to, from, expr;
1213 int flags;
1215 tree conv;
1216 struct z_candidate *cand;
1218 /* Resolve expressions like `A::p' that we thought might become
1219 pointers-to-members. */
1220 if (expr && TREE_CODE (expr) == OFFSET_REF)
1222 expr = resolve_offset_ref (expr);
1223 from = TREE_TYPE (expr);
1226 if (from == error_mark_node || to == error_mark_node
1227 || expr == error_mark_node)
1228 return NULL_TREE;
1230 /* Make sure both the FROM and TO types are complete so that
1231 user-defined conversions are available. */
1232 complete_type (from);
1233 complete_type (to);
1235 if (TREE_CODE (to) == REFERENCE_TYPE)
1236 conv = reference_binding (to, from, expr, flags);
1237 else
1238 conv = standard_conversion (to, from, expr);
1240 if (conv)
1242 else if (expr != NULL_TREE
1243 && (IS_AGGR_TYPE (from)
1244 || IS_AGGR_TYPE (to))
1245 && (flags & LOOKUP_NO_CONVERSION) == 0)
1247 cand = build_user_type_conversion_1
1248 (to, expr, LOOKUP_ONLYCONVERTING);
1249 if (cand)
1250 conv = cand->second_conv;
1252 /* We used to try to bind a reference to a temporary here, but that
1253 is now handled by the recursive call to this function at the end
1254 of reference_binding. */
1257 return conv;
1260 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1261 functions. */
1263 static struct z_candidate *
1264 add_candidate (candidates, fn, convs, viable)
1265 struct z_candidate *candidates;
1266 tree fn, convs;
1267 int viable;
1269 struct z_candidate *cand
1270 = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1272 cand->fn = fn;
1273 cand->convs = convs;
1274 cand->viable = viable;
1275 cand->next = candidates;
1277 return cand;
1280 /* Create an overload candidate for the function or method FN called with
1281 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1282 to implicit_conversion.
1284 CTYPE, if non-NULL, is the type we want to pretend this function
1285 comes from for purposes of overload resolution. */
1287 static struct z_candidate *
1288 add_function_candidate (candidates, fn, ctype, arglist, flags)
1289 struct z_candidate *candidates;
1290 tree fn, ctype, arglist;
1291 int flags;
1293 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1294 int i, len;
1295 tree convs;
1296 tree parmnode, argnode;
1297 int viable = 1;
1299 /* The `this' and `in_chrg' arguments to constructors are not considered
1300 in overload resolution. */
1301 if (DECL_CONSTRUCTOR_P (fn))
1303 parmlist = TREE_CHAIN (parmlist);
1304 arglist = TREE_CHAIN (arglist);
1305 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1307 parmlist = TREE_CHAIN (parmlist);
1308 arglist = TREE_CHAIN (arglist);
1312 len = list_length (arglist);
1313 convs = make_tree_vec (len);
1315 /* 13.3.2 - Viable functions [over.match.viable]
1316 First, to be a viable function, a candidate function shall have enough
1317 parameters to agree in number with the arguments in the list.
1319 We need to check this first; otherwise, checking the ICSes might cause
1320 us to produce an ill-formed template instantiation. */
1322 parmnode = parmlist;
1323 for (i = 0; i < len; ++i)
1325 if (parmnode == NULL_TREE || parmnode == void_list_node)
1326 break;
1327 parmnode = TREE_CHAIN (parmnode);
1330 if (i < len && parmnode)
1331 viable = 0;
1333 /* Make sure there are default args for the rest of the parms. */
1334 else if (!sufficient_parms_p (parmnode))
1335 viable = 0;
1337 if (! viable)
1338 goto out;
1340 /* Second, for F to be a viable function, there shall exist for each
1341 argument an implicit conversion sequence that converts that argument
1342 to the corresponding parameter of F. */
1344 parmnode = parmlist;
1345 argnode = arglist;
1347 for (i = 0; i < len; ++i)
1349 tree arg = TREE_VALUE (argnode);
1350 tree argtype = lvalue_type (arg);
1351 tree t;
1352 int is_this;
1354 if (parmnode == void_list_node)
1355 break;
1357 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1358 && ! DECL_CONSTRUCTOR_P (fn));
1360 if (parmnode)
1362 tree parmtype = TREE_VALUE (parmnode);
1364 /* The type of the implicit object parameter ('this') for
1365 overload resolution is not always the same as for the
1366 function itself; conversion functions are considered to
1367 be members of the class being converted, and functions
1368 introduced by a using-declaration are considered to be
1369 members of the class that uses them.
1371 Since build_over_call ignores the ICS for the `this'
1372 parameter, we can just change the parm type. */
1373 if (ctype && is_this)
1375 parmtype
1376 = build_qualified_type (ctype,
1377 TYPE_QUALS (TREE_TYPE (parmtype)));
1378 parmtype = build_pointer_type (parmtype);
1381 t = implicit_conversion (parmtype, argtype, arg, flags);
1383 else
1385 t = build1 (IDENTITY_CONV, argtype, arg);
1386 ICS_ELLIPSIS_FLAG (t) = 1;
1389 if (t && is_this)
1390 ICS_THIS_FLAG (t) = 1;
1392 TREE_VEC_ELT (convs, i) = t;
1393 if (! t)
1395 viable = 0;
1396 break;
1399 if (ICS_BAD_FLAG (t))
1400 viable = -1;
1402 if (parmnode)
1403 parmnode = TREE_CHAIN (parmnode);
1404 argnode = TREE_CHAIN (argnode);
1407 out:
1408 return add_candidate (candidates, fn, convs, viable);
1411 /* Create an overload candidate for the conversion function FN which will
1412 be invoked for expression OBJ, producing a pointer-to-function which
1413 will in turn be called with the argument list ARGLIST, and add it to
1414 CANDIDATES. FLAGS is passed on to implicit_conversion.
1416 Actually, we don't really care about FN; we care about the type it
1417 converts to. There may be multiple conversion functions that will
1418 convert to that type, and we rely on build_user_type_conversion_1 to
1419 choose the best one; so when we create our candidate, we record the type
1420 instead of the function. */
1422 static struct z_candidate *
1423 add_conv_candidate (candidates, fn, obj, arglist)
1424 struct z_candidate *candidates;
1425 tree fn, obj, arglist;
1427 tree totype = TREE_TYPE (TREE_TYPE (fn));
1428 int i, len, viable, flags;
1429 tree parmlist, convs, parmnode, argnode;
1431 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1432 parmlist = TREE_TYPE (parmlist);
1433 parmlist = TYPE_ARG_TYPES (parmlist);
1435 len = list_length (arglist) + 1;
1436 convs = make_tree_vec (len);
1437 parmnode = parmlist;
1438 argnode = arglist;
1439 viable = 1;
1440 flags = LOOKUP_NORMAL;
1442 /* Don't bother looking up the same type twice. */
1443 if (candidates && candidates->fn == totype)
1444 return candidates;
1446 for (i = 0; i < len; ++i)
1448 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1449 tree argtype = lvalue_type (arg);
1450 tree t;
1452 if (i == 0)
1453 t = implicit_conversion (totype, argtype, arg, flags);
1454 else if (parmnode == void_list_node)
1455 break;
1456 else if (parmnode)
1457 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1458 else
1460 t = build1 (IDENTITY_CONV, argtype, arg);
1461 ICS_ELLIPSIS_FLAG (t) = 1;
1464 TREE_VEC_ELT (convs, i) = t;
1465 if (! t)
1466 break;
1468 if (ICS_BAD_FLAG (t))
1469 viable = -1;
1471 if (i == 0)
1472 continue;
1474 if (parmnode)
1475 parmnode = TREE_CHAIN (parmnode);
1476 argnode = TREE_CHAIN (argnode);
1479 if (i < len)
1480 viable = 0;
1482 if (!sufficient_parms_p (parmnode))
1483 viable = 0;
1485 return add_candidate (candidates, totype, convs, viable);
1488 static struct z_candidate *
1489 build_builtin_candidate (candidates, fnname, type1, type2,
1490 args, argtypes, flags)
1491 struct z_candidate *candidates;
1492 tree fnname, type1, type2, *args, *argtypes;
1493 int flags;
1496 tree t, convs;
1497 int viable = 1, i;
1498 tree types[2];
1500 types[0] = type1;
1501 types[1] = type2;
1503 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1505 for (i = 0; i < 2; ++i)
1507 if (! args[i])
1508 break;
1510 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1511 if (! t)
1513 viable = 0;
1514 /* We need something for printing the candidate. */
1515 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1517 else if (ICS_BAD_FLAG (t))
1518 viable = 0;
1519 TREE_VEC_ELT (convs, i) = t;
1522 /* For COND_EXPR we rearranged the arguments; undo that now. */
1523 if (args[2])
1525 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1526 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1527 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1528 if (t)
1529 TREE_VEC_ELT (convs, 0) = t;
1530 else
1531 viable = 0;
1534 return add_candidate (candidates, fnname, convs, viable);
1537 static int
1538 is_complete (t)
1539 tree t;
1541 return COMPLETE_TYPE_P (complete_type (t));
1544 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1546 static int
1547 promoted_arithmetic_type_p (type)
1548 tree type;
1550 /* [over.built]
1552 In this section, the term promoted integral type is used to refer
1553 to those integral types which are preserved by integral promotion
1554 (including e.g. int and long but excluding e.g. char).
1555 Similarly, the term promoted arithmetic type refers to promoted
1556 integral types plus floating types. */
1557 return ((INTEGRAL_TYPE_P (type)
1558 && same_type_p (type_promotes_to (type), type))
1559 || TREE_CODE (type) == REAL_TYPE);
1562 /* Create any builtin operator overload candidates for the operator in
1563 question given the converted operand types TYPE1 and TYPE2. The other
1564 args are passed through from add_builtin_candidates to
1565 build_builtin_candidate.
1567 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1568 If CODE is requires candidates operands of the same type of the kind
1569 of which TYPE1 and TYPE2 are, we add both candidates
1570 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1572 static struct z_candidate *
1573 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1574 args, argtypes, flags)
1575 struct z_candidate *candidates;
1576 enum tree_code code, code2;
1577 tree fnname, type1, type2, *args, *argtypes;
1578 int flags;
1580 switch (code)
1582 case POSTINCREMENT_EXPR:
1583 case POSTDECREMENT_EXPR:
1584 args[1] = integer_zero_node;
1585 type2 = integer_type_node;
1586 break;
1587 default:
1588 break;
1591 switch (code)
1594 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1595 and VQ is either volatile or empty, there exist candidate operator
1596 functions of the form
1597 VQ T& operator++(VQ T&);
1598 T operator++(VQ T&, int);
1599 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1600 type other than bool, and VQ is either volatile or empty, there exist
1601 candidate operator functions of the form
1602 VQ T& operator--(VQ T&);
1603 T operator--(VQ T&, int);
1604 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1605 complete object type, and VQ is either volatile or empty, there exist
1606 candidate operator functions of the form
1607 T*VQ& operator++(T*VQ&);
1608 T*VQ& operator--(T*VQ&);
1609 T* operator++(T*VQ&, int);
1610 T* operator--(T*VQ&, int); */
1612 case POSTDECREMENT_EXPR:
1613 case PREDECREMENT_EXPR:
1614 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1615 return candidates;
1616 case POSTINCREMENT_EXPR:
1617 case PREINCREMENT_EXPR:
1618 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1620 type1 = build_reference_type (type1);
1621 break;
1623 return candidates;
1625 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1626 exist candidate operator functions of the form
1628 T& operator*(T*);
1630 8 For every function type T, there exist candidate operator functions of
1631 the form
1632 T& operator*(T*); */
1634 case INDIRECT_REF:
1635 if (TREE_CODE (type1) == POINTER_TYPE
1636 && (TYPE_PTROB_P (type1)
1637 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1638 break;
1639 return candidates;
1641 /* 9 For every type T, there exist candidate operator functions of the form
1642 T* operator+(T*);
1644 10For every promoted arithmetic type T, there exist candidate operator
1645 functions of the form
1646 T operator+(T);
1647 T operator-(T); */
1649 case CONVERT_EXPR: /* unary + */
1650 if (TREE_CODE (type1) == POINTER_TYPE
1651 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1652 break;
1653 case NEGATE_EXPR:
1654 if (ARITHMETIC_TYPE_P (type1))
1655 break;
1656 return candidates;
1658 /* 11For every promoted integral type T, there exist candidate operator
1659 functions of the form
1660 T operator~(T); */
1662 case BIT_NOT_EXPR:
1663 if (INTEGRAL_TYPE_P (type1))
1664 break;
1665 return candidates;
1667 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1668 is the same type as C2 or is a derived class of C2, T is a complete
1669 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1670 there exist candidate operator functions of the form
1671 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1672 where CV12 is the union of CV1 and CV2. */
1674 case MEMBER_REF:
1675 if (TREE_CODE (type1) == POINTER_TYPE
1676 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1678 tree c1 = TREE_TYPE (type1);
1679 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1680 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1681 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1683 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1684 && (TYPE_PTRMEMFUNC_P (type2)
1685 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1686 break;
1688 return candidates;
1690 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1691 didate operator functions of the form
1692 LR operator*(L, R);
1693 LR operator/(L, R);
1694 LR operator+(L, R);
1695 LR operator-(L, R);
1696 bool operator<(L, R);
1697 bool operator>(L, R);
1698 bool operator<=(L, R);
1699 bool operator>=(L, R);
1700 bool operator==(L, R);
1701 bool operator!=(L, R);
1702 where LR is the result of the usual arithmetic conversions between
1703 types L and R.
1705 14For every pair of types T and I, where T is a cv-qualified or cv-
1706 unqualified complete object type and I is a promoted integral type,
1707 there exist candidate operator functions of the form
1708 T* operator+(T*, I);
1709 T& operator[](T*, I);
1710 T* operator-(T*, I);
1711 T* operator+(I, T*);
1712 T& operator[](I, T*);
1714 15For every T, where T is a pointer to complete object type, there exist
1715 candidate operator functions of the form112)
1716 ptrdiff_t operator-(T, T);
1718 16For every pointer or enumeration type T, there exist candidate operator
1719 functions of the form
1720 bool operator<(T, T);
1721 bool operator>(T, T);
1722 bool operator<=(T, T);
1723 bool operator>=(T, T);
1724 bool operator==(T, T);
1725 bool operator!=(T, T);
1727 17For every pointer to member type T, there exist candidate operator
1728 functions of the form
1729 bool operator==(T, T);
1730 bool operator!=(T, T); */
1732 case MINUS_EXPR:
1733 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1734 break;
1735 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1737 type2 = ptrdiff_type_node;
1738 break;
1740 case MULT_EXPR:
1741 case TRUNC_DIV_EXPR:
1742 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1743 break;
1744 return candidates;
1746 case EQ_EXPR:
1747 case NE_EXPR:
1748 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1749 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1750 break;
1751 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1752 && null_ptr_cst_p (args[1]))
1754 type2 = type1;
1755 break;
1757 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1758 && null_ptr_cst_p (args[0]))
1760 type1 = type2;
1761 break;
1763 /* FALLTHROUGH */
1764 case LT_EXPR:
1765 case GT_EXPR:
1766 case LE_EXPR:
1767 case GE_EXPR:
1768 case MAX_EXPR:
1769 case MIN_EXPR:
1770 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1771 break;
1772 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1773 break;
1774 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1775 break;
1776 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1778 type2 = type1;
1779 break;
1781 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1783 type1 = type2;
1784 break;
1786 return candidates;
1788 case PLUS_EXPR:
1789 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1790 break;
1791 case ARRAY_REF:
1792 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1794 type1 = ptrdiff_type_node;
1795 break;
1797 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1799 type2 = ptrdiff_type_node;
1800 break;
1802 return candidates;
1804 /* 18For every pair of promoted integral types L and R, there exist candi-
1805 date operator functions of the form
1806 LR operator%(L, R);
1807 LR operator&(L, R);
1808 LR operator^(L, R);
1809 LR operator|(L, R);
1810 L operator<<(L, R);
1811 L operator>>(L, R);
1812 where LR is the result of the usual arithmetic conversions between
1813 types L and R. */
1815 case TRUNC_MOD_EXPR:
1816 case BIT_AND_EXPR:
1817 case BIT_IOR_EXPR:
1818 case BIT_XOR_EXPR:
1819 case LSHIFT_EXPR:
1820 case RSHIFT_EXPR:
1821 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1822 break;
1823 return candidates;
1825 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1826 type, VQ is either volatile or empty, and R is a promoted arithmetic
1827 type, there exist candidate operator functions of the form
1828 VQ L& operator=(VQ L&, R);
1829 VQ L& operator*=(VQ L&, R);
1830 VQ L& operator/=(VQ L&, R);
1831 VQ L& operator+=(VQ L&, R);
1832 VQ L& operator-=(VQ L&, R);
1834 20For every pair T, VQ), where T is any type and VQ is either volatile
1835 or empty, there exist candidate operator functions of the form
1836 T*VQ& operator=(T*VQ&, T*);
1838 21For every pair T, VQ), where T is a pointer to member type and VQ is
1839 either volatile or empty, there exist candidate operator functions of
1840 the form
1841 VQ T& operator=(VQ T&, T);
1843 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1844 unqualified complete object type, VQ is either volatile or empty, and
1845 I is a promoted integral type, there exist candidate operator func-
1846 tions of the form
1847 T*VQ& operator+=(T*VQ&, I);
1848 T*VQ& operator-=(T*VQ&, I);
1850 23For every triple L, VQ, R), where L is an integral or enumeration
1851 type, VQ is either volatile or empty, and R is a promoted integral
1852 type, there exist candidate operator functions of the form
1854 VQ L& operator%=(VQ L&, R);
1855 VQ L& operator<<=(VQ L&, R);
1856 VQ L& operator>>=(VQ L&, R);
1857 VQ L& operator&=(VQ L&, R);
1858 VQ L& operator^=(VQ L&, R);
1859 VQ L& operator|=(VQ L&, R); */
1861 case MODIFY_EXPR:
1862 switch (code2)
1864 case PLUS_EXPR:
1865 case MINUS_EXPR:
1866 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1868 type2 = ptrdiff_type_node;
1869 break;
1871 case MULT_EXPR:
1872 case TRUNC_DIV_EXPR:
1873 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1874 break;
1875 return candidates;
1877 case TRUNC_MOD_EXPR:
1878 case BIT_AND_EXPR:
1879 case BIT_IOR_EXPR:
1880 case BIT_XOR_EXPR:
1881 case LSHIFT_EXPR:
1882 case RSHIFT_EXPR:
1883 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1884 break;
1885 return candidates;
1887 case NOP_EXPR:
1888 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1889 break;
1890 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1891 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1892 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1893 || ((TYPE_PTRMEMFUNC_P (type1)
1894 || TREE_CODE (type1) == POINTER_TYPE)
1895 && null_ptr_cst_p (args[1])))
1897 type2 = type1;
1898 break;
1900 return candidates;
1902 default:
1903 my_friendly_abort (367);
1905 type1 = build_reference_type (type1);
1906 break;
1908 case COND_EXPR:
1909 /* [over.builtin]
1911 For every pair of promoted arithmetic types L and R, there
1912 exist candidate operator functions of the form
1914 LR operator?(bool, L, R);
1916 where LR is the result of the usual arithmetic conversions
1917 between types L and R.
1919 For every type T, where T is a pointer or pointer-to-member
1920 type, there exist candidate operator functions of the form T
1921 operator?(bool, T, T); */
1923 if (promoted_arithmetic_type_p (type1)
1924 && promoted_arithmetic_type_p (type2))
1925 /* That's OK. */
1926 break;
1928 /* Otherwise, the types should be pointers. */
1929 if (!(TREE_CODE (type1) == POINTER_TYPE
1930 || TYPE_PTRMEM_P (type1)
1931 || TYPE_PTRMEMFUNC_P (type1))
1932 || !(TREE_CODE (type2) == POINTER_TYPE
1933 || TYPE_PTRMEM_P (type2)
1934 || TYPE_PTRMEMFUNC_P (type2)))
1935 return candidates;
1937 /* We don't check that the two types are the same; the logic
1938 below will actually create two candidates; one in which both
1939 parameter types are TYPE1, and one in which both parameter
1940 types are TYPE2. */
1941 break;
1943 /* These arguments do not make for a legal overloaded operator. */
1944 return candidates;
1946 default:
1947 my_friendly_abort (367);
1950 /* If we're dealing with two pointer types or two enumeral types,
1951 we need candidates for both of them. */
1952 if (type2 && !same_type_p (type1, type2)
1953 && TREE_CODE (type1) == TREE_CODE (type2)
1954 && (TREE_CODE (type1) == REFERENCE_TYPE
1955 || (TREE_CODE (type1) == POINTER_TYPE
1956 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1957 || TYPE_PTRMEMFUNC_P (type1)
1958 || IS_AGGR_TYPE (type1)
1959 || TREE_CODE (type1) == ENUMERAL_TYPE))
1961 candidates = build_builtin_candidate
1962 (candidates, fnname, type1, type1, args, argtypes, flags);
1963 return build_builtin_candidate
1964 (candidates, fnname, type2, type2, args, argtypes, flags);
1967 return build_builtin_candidate
1968 (candidates, fnname, type1, type2, args, argtypes, flags);
1971 tree
1972 type_decays_to (type)
1973 tree type;
1975 if (TREE_CODE (type) == ARRAY_TYPE)
1976 return build_pointer_type (TREE_TYPE (type));
1977 if (TREE_CODE (type) == FUNCTION_TYPE)
1978 return build_pointer_type (type);
1979 return type;
1982 /* There are three conditions of builtin candidates:
1984 1) bool-taking candidates. These are the same regardless of the input.
1985 2) pointer-pair taking candidates. These are generated for each type
1986 one of the input types converts to.
1987 3) arithmetic candidates. According to the standard, we should generate
1988 all of these, but I'm trying not to...
1990 Here we generate a superset of the possible candidates for this particular
1991 case. That is a subset of the full set the standard defines, plus some
1992 other cases which the standard disallows. add_builtin_candidate will
1993 filter out the illegal set. */
1995 static struct z_candidate *
1996 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1997 struct z_candidate *candidates;
1998 enum tree_code code, code2;
1999 tree fnname, *args;
2000 int flags;
2002 int ref1, i;
2003 int enum_p = 0;
2004 tree type, argtypes[3];
2005 /* TYPES[i] is the set of possible builtin-operator parameter types
2006 we will consider for the Ith argument. These are represented as
2007 a TREE_LIST; the TREE_VALUE of each node is the potential
2008 parameter type. */
2009 tree types[2];
2011 for (i = 0; i < 3; ++i)
2013 if (args[i])
2014 argtypes[i] = lvalue_type (args[i]);
2015 else
2016 argtypes[i] = NULL_TREE;
2019 switch (code)
2021 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2022 and VQ is either volatile or empty, there exist candidate operator
2023 functions of the form
2024 VQ T& operator++(VQ T&); */
2026 case POSTINCREMENT_EXPR:
2027 case PREINCREMENT_EXPR:
2028 case POSTDECREMENT_EXPR:
2029 case PREDECREMENT_EXPR:
2030 case MODIFY_EXPR:
2031 ref1 = 1;
2032 break;
2034 /* 24There also exist candidate operator functions of the form
2035 bool operator!(bool);
2036 bool operator&&(bool, bool);
2037 bool operator||(bool, bool); */
2039 case TRUTH_NOT_EXPR:
2040 return build_builtin_candidate
2041 (candidates, fnname, boolean_type_node,
2042 NULL_TREE, args, argtypes, flags);
2044 case TRUTH_ORIF_EXPR:
2045 case TRUTH_ANDIF_EXPR:
2046 return build_builtin_candidate
2047 (candidates, fnname, boolean_type_node,
2048 boolean_type_node, args, argtypes, flags);
2050 case ADDR_EXPR:
2051 case COMPOUND_EXPR:
2052 case COMPONENT_REF:
2053 return candidates;
2055 case COND_EXPR:
2056 case EQ_EXPR:
2057 case NE_EXPR:
2058 case LT_EXPR:
2059 case LE_EXPR:
2060 case GT_EXPR:
2061 case GE_EXPR:
2062 enum_p = 1;
2063 /* FALLTHROUGH */
2065 default:
2066 ref1 = 0;
2069 types[0] = types[1] = NULL_TREE;
2071 for (i = 0; i < 2; ++i)
2073 if (! args[i])
2075 else if (IS_AGGR_TYPE (argtypes[i]))
2077 tree convs;
2079 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2080 return candidates;
2082 convs = lookup_conversions (argtypes[i]);
2084 if (code == COND_EXPR)
2086 if (real_lvalue_p (args[i]))
2087 types[i] = tree_cons
2088 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2090 types[i] = tree_cons
2091 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2094 else if (! convs)
2095 return candidates;
2097 for (; convs; convs = TREE_CHAIN (convs))
2099 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2101 if (i == 0 && ref1
2102 && (TREE_CODE (type) != REFERENCE_TYPE
2103 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2104 continue;
2106 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2107 types[i] = tree_cons (NULL_TREE, type, types[i]);
2109 type = non_reference (type);
2110 if (i != 0 || ! ref1)
2112 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2113 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2114 types[i] = tree_cons (NULL_TREE, type, types[i]);
2115 if (INTEGRAL_TYPE_P (type))
2116 type = type_promotes_to (type);
2119 if (! value_member (type, types[i]))
2120 types[i] = tree_cons (NULL_TREE, type, types[i]);
2123 else
2125 if (code == COND_EXPR && real_lvalue_p (args[i]))
2126 types[i] = tree_cons
2127 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2128 type = non_reference (argtypes[i]);
2129 if (i != 0 || ! ref1)
2131 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2132 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2133 types[i] = tree_cons (NULL_TREE, type, types[i]);
2134 if (INTEGRAL_TYPE_P (type))
2135 type = type_promotes_to (type);
2137 types[i] = tree_cons (NULL_TREE, type, types[i]);
2141 /* Run through the possible parameter types of both arguments,
2142 creating candidates with those parameter types. */
2143 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2145 if (types[1])
2146 for (type = types[1]; type; type = TREE_CHAIN (type))
2147 candidates = add_builtin_candidate
2148 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2149 TREE_VALUE (type), args, argtypes, flags);
2150 else
2151 candidates = add_builtin_candidate
2152 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2153 NULL_TREE, args, argtypes, flags);
2156 return candidates;
2160 /* If TMPL can be successfully instantiated as indicated by
2161 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2163 TMPL is the template. EXPLICIT_TARGS are any explicit template
2164 arguments. ARGLIST is the arguments provided at the call-site.
2165 The RETURN_TYPE is the desired type for conversion operators. If
2166 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2167 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2168 add_conv_candidate. */
2170 static struct z_candidate*
2171 add_template_candidate_real (candidates, tmpl, ctype, explicit_targs,
2172 arglist, return_type, flags,
2173 obj, strict)
2174 struct z_candidate *candidates;
2175 tree tmpl, ctype, explicit_targs, arglist, return_type;
2176 int flags;
2177 tree obj;
2178 unification_kind_t strict;
2180 int ntparms = DECL_NTPARMS (tmpl);
2181 tree targs = make_tree_vec (ntparms);
2182 tree args_without_in_chrg = arglist;
2183 struct z_candidate *cand;
2184 int i;
2185 tree fn;
2187 /* We don't do deduction on the in-charge parameter, the VTT
2188 parameter or 'this'. */
2189 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2190 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2192 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2193 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2194 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2195 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2197 i = fn_type_unification (tmpl, explicit_targs, targs,
2198 args_without_in_chrg,
2199 return_type, strict, -1);
2201 if (i != 0)
2202 return candidates;
2204 fn = instantiate_template (tmpl, targs);
2205 if (fn == error_mark_node)
2206 return candidates;
2208 if (obj != NULL_TREE)
2209 /* Aha, this is a conversion function. */
2210 cand = add_conv_candidate (candidates, fn, obj, arglist);
2211 else
2212 cand = add_function_candidate (candidates, fn, ctype,
2213 arglist, flags);
2214 if (DECL_TI_TEMPLATE (fn) != tmpl)
2215 /* This situation can occur if a member template of a template
2216 class is specialized. Then, instantiate_template might return
2217 an instantiation of the specialization, in which case the
2218 DECL_TI_TEMPLATE field will point at the original
2219 specialization. For example:
2221 template <class T> struct S { template <class U> void f(U);
2222 template <> void f(int) {}; };
2223 S<double> sd;
2224 sd.f(3);
2226 Here, TMPL will be template <class U> S<double>::f(U).
2227 And, instantiate template will give us the specialization
2228 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2229 for this will point at template <class T> template <> S<T>::f(int),
2230 so that we can find the definition. For the purposes of
2231 overload resolution, however, we want the original TMPL. */
2232 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2233 else
2234 cand->template = DECL_TEMPLATE_INFO (fn);
2236 return cand;
2240 static struct z_candidate *
2241 add_template_candidate (candidates, tmpl, ctype, explicit_targs,
2242 arglist, return_type, flags, strict)
2243 struct z_candidate *candidates;
2244 tree tmpl, ctype, explicit_targs, arglist, return_type;
2245 int flags;
2246 unification_kind_t strict;
2248 return
2249 add_template_candidate_real (candidates, tmpl, ctype,
2250 explicit_targs, arglist, return_type, flags,
2251 NULL_TREE, strict);
2255 static struct z_candidate *
2256 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2257 struct z_candidate *candidates;
2258 tree tmpl, obj, arglist, return_type;
2260 return
2261 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2262 arglist, return_type, 0, obj, DEDUCE_CONV);
2266 static int
2267 any_viable (cands)
2268 struct z_candidate *cands;
2270 for (; cands; cands = cands->next)
2271 if (pedantic ? cands->viable == 1 : cands->viable)
2272 return 1;
2273 return 0;
2276 static struct z_candidate *
2277 splice_viable (cands)
2278 struct z_candidate *cands;
2280 struct z_candidate **p = &cands;
2282 for (; *p; )
2284 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2285 p = &((*p)->next);
2286 else
2287 *p = (*p)->next;
2290 return cands;
2293 static tree
2294 build_this (obj)
2295 tree obj;
2297 /* Fix this to work on non-lvalues. */
2298 return build_unary_op (ADDR_EXPR, obj, 0);
2301 static void
2302 print_z_candidates (candidates)
2303 struct z_candidate *candidates;
2305 const char *str = "candidates are:";
2306 for (; candidates; candidates = candidates->next)
2308 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2310 if (TREE_VEC_LENGTH (candidates->convs) == 3)
2311 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2312 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2313 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2314 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2315 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2316 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2317 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2318 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2319 else
2320 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2321 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2323 else if (TYPE_P (candidates->fn))
2324 cp_error ("%s %T <conversion>", str, candidates->fn);
2325 else
2326 cp_error_at ("%s %+#D%s", str, candidates->fn,
2327 candidates->viable == -1 ? " <near match>" : "");
2328 str = " ";
2332 /* Returns the best overload candidate to perform the requested
2333 conversion. This function is used for three the overloading situations
2334 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2335 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2336 per [dcl.init.ref], so we ignore temporary bindings. */
2338 static struct z_candidate *
2339 build_user_type_conversion_1 (totype, expr, flags)
2340 tree totype, expr;
2341 int flags;
2343 struct z_candidate *candidates, *cand;
2344 tree fromtype = TREE_TYPE (expr);
2345 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2346 tree args = NULL_TREE;
2347 tree templates = NULL_TREE;
2349 /* We represent conversion within a hierarchy using RVALUE_CONV and
2350 BASE_CONV, as specified by [over.best.ics]; these become plain
2351 constructor calls, as specified in [dcl.init]. */
2352 if (IS_AGGR_TYPE (fromtype) && IS_AGGR_TYPE (totype)
2353 && DERIVED_FROM_P (totype, fromtype))
2354 abort ();
2356 if (IS_AGGR_TYPE (totype))
2357 ctors = lookup_fnfields (TYPE_BINFO (totype),
2358 (flag_new_abi
2359 ? complete_ctor_identifier
2360 : ctor_identifier),
2363 if (IS_AGGR_TYPE (fromtype))
2364 convs = lookup_conversions (fromtype);
2366 candidates = 0;
2367 flags |= LOOKUP_NO_CONVERSION;
2369 if (ctors)
2371 tree t;
2373 ctors = TREE_VALUE (ctors);
2375 t = build_int_2 (0, 0);
2376 TREE_TYPE (t) = build_pointer_type (totype);
2377 args = build_tree_list (NULL_TREE, expr);
2378 if (DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors)))
2379 args = tree_cons (NULL_TREE,
2380 in_charge_arg_for_name (complete_ctor_identifier),
2381 args);
2382 args = tree_cons (NULL_TREE, t, args);
2384 for (; ctors; ctors = OVL_NEXT (ctors))
2386 tree ctor = OVL_CURRENT (ctors);
2387 if (DECL_NONCONVERTING_P (ctor))
2388 continue;
2390 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2392 templates = tree_cons (NULL_TREE, ctor, templates);
2393 candidates =
2394 add_template_candidate (candidates, ctor, totype,
2395 NULL_TREE, args, NULL_TREE, flags,
2396 DEDUCE_CALL);
2398 else
2399 candidates = add_function_candidate (candidates, ctor, totype,
2400 args, flags);
2402 if (candidates)
2404 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2405 candidates->basetype_path = TYPE_BINFO (totype);
2409 if (convs)
2410 args = build_tree_list (NULL_TREE, build_this (expr));
2412 for (; convs; convs = TREE_CHAIN (convs))
2414 tree fns = TREE_VALUE (convs);
2415 int convflags = LOOKUP_NO_CONVERSION;
2416 tree ics;
2418 /* If we are called to convert to a reference type, we are trying to
2419 find an lvalue binding, so don't even consider temporaries. If
2420 we don't find an lvalue binding, the caller will try again to
2421 look for a temporary binding. */
2422 if (TREE_CODE (totype) == REFERENCE_TYPE)
2423 convflags |= LOOKUP_NO_TEMP_BIND;
2425 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2426 ics = implicit_conversion
2427 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2428 else
2429 /* We can't compute this yet. */
2430 ics = error_mark_node;
2432 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2433 /* ignore the near match. */;
2434 else if (ics)
2435 for (; fns; fns = OVL_NEXT (fns))
2437 tree fn = OVL_CURRENT (fns);
2438 struct z_candidate *old_candidates = candidates;
2440 /* [over.match.funcs] For conversion functions, the function is
2441 considered to be a member of the class of the implicit object
2442 argument for the purpose of defining the type of the implicit
2443 object parameter.
2445 So we pass fromtype as CTYPE to add_*_candidate. */
2447 if (TREE_CODE (fn) == TEMPLATE_DECL)
2449 templates = tree_cons (NULL_TREE, fn, templates);
2450 candidates =
2451 add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2452 args, totype, flags,
2453 DEDUCE_CONV);
2455 else
2456 candidates = add_function_candidate (candidates, fn, fromtype,
2457 args, flags);
2459 if (candidates != old_candidates)
2461 if (TREE_CODE (fn) == TEMPLATE_DECL)
2462 ics = implicit_conversion
2463 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2464 0, convflags);
2466 candidates->second_conv = ics;
2467 candidates->basetype_path = TYPE_BINFO (fromtype);
2469 if (ics == NULL_TREE)
2470 candidates->viable = 0;
2471 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2472 candidates->viable = -1;
2477 if (! any_viable (candidates))
2479 #if 0
2480 if (flags & LOOKUP_COMPLAIN)
2482 if (candidates && ! candidates->next)
2483 /* say why this one won't work or try to be loose */;
2484 else
2485 cp_error ("no viable candidates");
2487 #endif
2489 return 0;
2492 candidates = splice_viable (candidates);
2493 cand = tourney (candidates);
2495 if (cand == 0)
2497 if (flags & LOOKUP_COMPLAIN)
2499 cp_error ("conversion from `%T' to `%T' is ambiguous",
2500 fromtype, totype);
2501 print_z_candidates (candidates);
2504 cand = candidates; /* any one will do */
2505 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2506 ICS_USER_FLAG (cand->second_conv) = 1;
2507 ICS_BAD_FLAG (cand->second_conv) = 1;
2509 return cand;
2512 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2513 p = &(TREE_OPERAND (*p, 0));
2515 *p = build
2516 (USER_CONV,
2517 (DECL_CONSTRUCTOR_P (cand->fn)
2518 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2519 expr, build_expr_ptr_wrapper (cand));
2520 ICS_USER_FLAG (cand->second_conv) = 1;
2521 if (cand->viable == -1)
2522 ICS_BAD_FLAG (cand->second_conv) = 1;
2524 return cand;
2527 tree
2528 build_user_type_conversion (totype, expr, flags)
2529 tree totype, expr;
2530 int flags;
2532 struct z_candidate *cand
2533 = build_user_type_conversion_1 (totype, expr, flags);
2535 if (cand)
2537 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2538 return error_mark_node;
2539 return convert_from_reference (convert_like (cand->second_conv, expr));
2541 return NULL_TREE;
2544 /* Do any initial processing on the arguments to a function call. */
2546 static tree
2547 resolve_args (args)
2548 tree args;
2550 tree t;
2551 for (t = args; t; t = TREE_CHAIN (t))
2553 if (TREE_VALUE (t) == error_mark_node)
2554 return error_mark_node;
2555 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2557 error ("invalid use of void expression");
2558 return error_mark_node;
2560 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2561 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2563 return args;
2566 tree
2567 build_new_function_call (fn, args)
2568 tree fn, args;
2570 struct z_candidate *candidates = 0, *cand;
2571 tree explicit_targs = NULL_TREE;
2572 int template_only = 0;
2574 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2576 explicit_targs = TREE_OPERAND (fn, 1);
2577 fn = TREE_OPERAND (fn, 0);
2578 template_only = 1;
2581 if (really_overloaded_fn (fn))
2583 tree t1;
2584 tree templates = NULL_TREE;
2586 args = resolve_args (args);
2588 if (args == error_mark_node)
2589 return error_mark_node;
2591 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2593 tree t = OVL_FUNCTION (t1);
2595 if (TREE_CODE (t) == TEMPLATE_DECL)
2597 templates = tree_cons (NULL_TREE, t, templates);
2598 candidates = add_template_candidate
2599 (candidates, t, NULL_TREE, explicit_targs, args, NULL_TREE,
2600 LOOKUP_NORMAL, DEDUCE_CALL);
2602 else if (! template_only)
2603 candidates = add_function_candidate
2604 (candidates, t, NULL_TREE, args, LOOKUP_NORMAL);
2607 if (! any_viable (candidates))
2609 if (candidates && ! candidates->next)
2610 return build_function_call (candidates->fn, args);
2611 cp_error ("no matching function for call to `%D(%A)'",
2612 DECL_NAME (OVL_FUNCTION (fn)), args);
2613 if (candidates)
2614 print_z_candidates (candidates);
2615 return error_mark_node;
2617 candidates = splice_viable (candidates);
2618 cand = tourney (candidates);
2620 if (cand == 0)
2622 cp_error ("call of overloaded `%D(%A)' is ambiguous",
2623 DECL_NAME (OVL_FUNCTION (fn)), args);
2624 print_z_candidates (candidates);
2625 return error_mark_node;
2628 return build_over_call (cand, args, LOOKUP_NORMAL);
2631 /* This is not really overloaded. */
2632 fn = OVL_CURRENT (fn);
2634 return build_function_call (fn, args);
2637 static tree
2638 build_object_call (obj, args)
2639 tree obj, args;
2641 struct z_candidate *candidates = 0, *cand;
2642 tree fns, convs, mem_args = NULL_TREE;
2643 tree type = TREE_TYPE (obj);
2645 if (TYPE_PTRMEMFUNC_P (type))
2647 /* It's no good looking for an overloaded operator() on a
2648 pointer-to-member-function. */
2649 cp_error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2650 return error_mark_node;
2653 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2654 if (fns == error_mark_node)
2655 return error_mark_node;
2657 args = resolve_args (args);
2659 if (args == error_mark_node)
2660 return error_mark_node;
2662 if (fns)
2664 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
2665 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2667 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2669 tree fn = OVL_CURRENT (fns);
2670 if (TREE_CODE (fn) == TEMPLATE_DECL)
2672 candidates
2673 = add_template_candidate (candidates, fn, base, NULL_TREE,
2674 mem_args, NULL_TREE,
2675 LOOKUP_NORMAL, DEDUCE_CALL);
2677 else
2678 candidates = add_function_candidate
2679 (candidates, fn, base, mem_args, LOOKUP_NORMAL);
2681 if (candidates)
2682 candidates->basetype_path = TYPE_BINFO (type);
2686 convs = lookup_conversions (type);
2688 for (; convs; convs = TREE_CHAIN (convs))
2690 tree fns = TREE_VALUE (convs);
2691 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2693 if ((TREE_CODE (totype) == POINTER_TYPE
2694 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2695 || (TREE_CODE (totype) == REFERENCE_TYPE
2696 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2697 || (TREE_CODE (totype) == REFERENCE_TYPE
2698 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2699 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2700 for (; fns; fns = OVL_NEXT (fns))
2702 tree fn = OVL_CURRENT (fns);
2703 if (TREE_CODE (fn) == TEMPLATE_DECL)
2705 candidates = add_template_conv_candidate (candidates,
2707 obj,
2708 args,
2709 totype);
2711 else
2712 candidates = add_conv_candidate (candidates, fn, obj, args);
2716 if (! any_viable (candidates))
2718 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2719 print_z_candidates (candidates);
2720 return error_mark_node;
2723 candidates = splice_viable (candidates);
2724 cand = tourney (candidates);
2726 if (cand == 0)
2728 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2729 print_z_candidates (candidates);
2730 return error_mark_node;
2733 /* Since cand->fn will be a type, not a function, for a conversion
2734 function, we must be careful not to unconditionally look at
2735 DECL_NAME here. */
2736 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2737 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2738 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2740 obj = convert_like_with_context
2741 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2743 /* FIXME */
2744 return build_function_call (obj, args);
2747 static void
2748 op_error (code, code2, arg1, arg2, arg3, problem)
2749 enum tree_code code, code2;
2750 tree arg1, arg2, arg3;
2751 const char *problem;
2753 const char *opname;
2755 if (code == MODIFY_EXPR)
2756 opname = assignment_operator_name_info[code2].name;
2757 else
2758 opname = operator_name_info[code].name;
2760 switch (code)
2762 case COND_EXPR:
2763 cp_error ("%s for `%T ? %T : %T' operator", problem,
2764 error_type (arg1), error_type (arg2), error_type (arg3));
2765 break;
2766 case POSTINCREMENT_EXPR:
2767 case POSTDECREMENT_EXPR:
2768 cp_error ("%s for `%T %s' operator", problem, error_type (arg1), opname);
2769 break;
2770 case ARRAY_REF:
2771 cp_error ("%s for `%T [%T]' operator", problem,
2772 error_type (arg1), error_type (arg2));
2773 break;
2774 default:
2775 if (arg2)
2776 cp_error ("%s for `%T %s %T' operator", problem,
2777 error_type (arg1), opname, error_type (arg2));
2778 else
2779 cp_error ("%s for `%s %T' operator", problem, opname, error_type (arg1));
2783 /* Return the implicit conversion sequence that could be used to
2784 convert E1 to E2 in [expr.cond]. */
2786 static tree
2787 conditional_conversion (e1, e2)
2788 tree e1;
2789 tree e2;
2791 tree t1 = non_reference (TREE_TYPE (e1));
2792 tree t2 = non_reference (TREE_TYPE (e2));
2793 tree conv;
2795 /* [expr.cond]
2797 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2798 implicitly converted (clause _conv_) to the type "reference to
2799 T2", subject to the constraint that in the conversion the
2800 reference must bind directly (_dcl.init.ref_) to E1. */
2801 if (real_lvalue_p (e2))
2803 conv = implicit_conversion (build_reference_type (t2),
2806 LOOKUP_NO_TEMP_BIND);
2807 if (conv)
2808 return conv;
2811 /* [expr.cond]
2813 If E1 and E2 have class type, and the underlying class types are
2814 the same or one is a base class of the other: E1 can be converted
2815 to match E2 if the class of T2 is the same type as, or a base
2816 class of, the class of T1, and the cv-qualification of T2 is the
2817 same cv-qualification as, or a greater cv-qualification than, the
2818 cv-qualification of T1. If the conversion is applied, E1 is
2819 changed to an rvalue of type T2 that still refers to the original
2820 source class object (or the appropriate subobject thereof). */
2821 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2822 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2823 TYPE_MAIN_VARIANT (t1)))
2825 if (at_least_as_qualified_p (t2, t1))
2827 conv = build1 (IDENTITY_CONV, t1, e1);
2828 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2829 TYPE_MAIN_VARIANT (t2)))
2830 conv = build_conv (BASE_CONV, t2, conv);
2831 return conv;
2833 else
2834 return NULL_TREE;
2837 /* [expr.cond]
2839 E1 can be converted to match E2 if E1 can be implicitly converted
2840 to the type that expression E2 would have if E2 were converted to
2841 an rvalue (or the type it has, if E2 is an rvalue). */
2842 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2845 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2846 arguments to the conditional expression. By the time this function
2847 is called, any suitable candidate functions are included in
2848 CANDIDATES. */
2850 tree
2851 build_conditional_expr (arg1, arg2, arg3)
2852 tree arg1;
2853 tree arg2;
2854 tree arg3;
2856 tree arg2_type;
2857 tree arg3_type;
2858 tree result;
2859 tree result_type = NULL_TREE;
2860 int lvalue_p = 1;
2861 struct z_candidate *candidates = 0;
2862 struct z_candidate *cand;
2864 /* As a G++ extension, the second argument to the conditional can be
2865 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2866 c'.) If the second operand is omitted, make sure it is
2867 calculated only once. */
2868 if (!arg2)
2870 if (pedantic)
2871 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2872 arg1 = arg2 = save_expr (arg1);
2875 /* [expr.cond]
2877 The first expr ession is implicitly converted to bool (clause
2878 _conv_). */
2879 arg1 = cp_convert (boolean_type_node, arg1);
2881 /* If something has already gone wrong, just pass that fact up the
2882 tree. */
2883 if (arg1 == error_mark_node
2884 || arg2 == error_mark_node
2885 || arg3 == error_mark_node
2886 || TREE_TYPE (arg1) == error_mark_node
2887 || TREE_TYPE (arg2) == error_mark_node
2888 || TREE_TYPE (arg3) == error_mark_node)
2889 return error_mark_node;
2891 /* Convert from reference types to ordinary types; no expressions
2892 really have reference type in C++. */
2893 arg2 = convert_from_reference (arg2);
2894 arg3 = convert_from_reference (arg3);
2896 /* [expr.cond]
2898 If either the second or the third operand has type (possibly
2899 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2900 array-to-pointer (_conv.array_), and function-to-pointer
2901 (_conv.func_) standard conversions are performed on the second
2902 and third operands. */
2903 arg2_type = TREE_TYPE (arg2);
2904 arg3_type = TREE_TYPE (arg3);
2905 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
2907 /* Do the conversions. We don't these for `void' type arguments
2908 since it can't have any effect and since decay_conversion
2909 does not handle that case gracefully. */
2910 if (!VOID_TYPE_P (arg2_type))
2911 arg2 = decay_conversion (arg2);
2912 if (!VOID_TYPE_P (arg3_type))
2913 arg3 = decay_conversion (arg3);
2914 arg2_type = TREE_TYPE (arg2);
2915 arg3_type = TREE_TYPE (arg3);
2917 /* [expr.cond]
2919 One of the following shall hold:
2921 --The second or the third operand (but not both) is a
2922 throw-expression (_except.throw_); the result is of the
2923 type of the other and is an rvalue.
2925 --Both the second and the third operands have type void; the
2926 result is of type void and is an rvalue. */
2927 if ((TREE_CODE (arg2) == THROW_EXPR)
2928 ^ (TREE_CODE (arg3) == THROW_EXPR))
2929 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2930 ? arg3_type : arg2_type);
2931 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
2932 result_type = void_type_node;
2933 else
2935 cp_error ("`%E' has type `void' and is not a throw-expression",
2936 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
2937 return error_mark_node;
2940 lvalue_p = 0;
2941 goto valid_operands;
2943 /* [expr.cond]
2945 Otherwise, if the second and third operand have different types,
2946 and either has (possibly cv-qualified) class type, an attempt is
2947 made to convert each of those operands to the type of the other. */
2948 else if (!same_type_p (arg2_type, arg3_type)
2949 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2951 tree conv2 = conditional_conversion (arg2, arg3);
2952 tree conv3 = conditional_conversion (arg3, arg2);
2954 /* [expr.cond]
2956 If both can be converted, or one can be converted but the
2957 conversion is ambiguous, the program is ill-formed. If
2958 neither can be converted, the operands are left unchanged and
2959 further checking is performed as described below. If exactly
2960 one conversion is possible, that conversion is applied to the
2961 chosen operand and the converted operand is used in place of
2962 the original operand for the remainder of this section. */
2963 if ((conv2 && !ICS_BAD_FLAG (conv2)
2964 && conv3 && !ICS_BAD_FLAG (conv3))
2965 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
2966 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
2968 cp_error ("operands to ?: have different types");
2969 return error_mark_node;
2971 else if (conv2 && !ICS_BAD_FLAG (conv2))
2973 arg2 = convert_like (conv2, arg2);
2974 arg2 = convert_from_reference (arg2);
2975 /* That may not quite have done the trick. If the two types
2976 are cv-qualified variants of one another, we will have
2977 just used an IDENTITY_CONV. (There's no conversion from
2978 an lvalue of one class type to an lvalue of another type,
2979 even a cv-qualified variant, and we don't want to lose
2980 lvalue-ness here.) So, we manually add a NOP_EXPR here
2981 if necessary. */
2982 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
2983 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
2984 arg2_type = TREE_TYPE (arg2);
2986 else if (conv3 && !ICS_BAD_FLAG (conv3))
2988 arg3 = convert_like (conv3, arg3);
2989 arg3 = convert_from_reference (arg3);
2990 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
2991 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
2992 arg3_type = TREE_TYPE (arg3);
2996 /* [expr.cond]
2998 If the second and third operands are lvalues and have the same
2999 type, the result is of that type and is an lvalue. */
3000 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
3001 same_type_p (arg2_type, arg3_type))
3003 result_type = arg2_type;
3004 goto valid_operands;
3007 /* [expr.cond]
3009 Otherwise, the result is an rvalue. If the second and third
3010 operand do not have the same type, and either has (possibly
3011 cv-qualified) class type, overload resolution is used to
3012 determine the conversions (if any) to be applied to the operands
3013 (_over.match.oper_, _over.built_). */
3014 lvalue_p = 0;
3015 if (!same_type_p (arg2_type, arg3_type)
3016 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3018 tree args[3];
3019 tree conv;
3021 /* Rearrange the arguments so that add_builtin_candidate only has
3022 to know about two args. In build_builtin_candidates, the
3023 arguments are unscrambled. */
3024 args[0] = arg2;
3025 args[1] = arg3;
3026 args[2] = arg1;
3027 candidates = add_builtin_candidates (candidates,
3028 COND_EXPR,
3029 NOP_EXPR,
3030 ansi_opname (COND_EXPR),
3031 args,
3032 LOOKUP_NORMAL);
3034 /* [expr.cond]
3036 If the overload resolution fails, the program is
3037 ill-formed. */
3038 if (!any_viable (candidates))
3040 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3041 print_z_candidates (candidates);
3042 return error_mark_node;
3044 candidates = splice_viable (candidates);
3045 cand = tourney (candidates);
3046 if (!cand)
3048 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3049 print_z_candidates (candidates);
3050 return error_mark_node;
3053 /* [expr.cond]
3055 Otherwise, the conversions thus determined are applied, and
3056 the converted operands are used in place of the original
3057 operands for the remainder of this section. */
3058 conv = TREE_VEC_ELT (cand->convs, 0);
3059 arg1 = convert_like (conv, arg1);
3060 conv = TREE_VEC_ELT (cand->convs, 1);
3061 arg2 = convert_like (conv, arg2);
3062 conv = TREE_VEC_ELT (cand->convs, 2);
3063 arg3 = convert_like (conv, arg3);
3066 /* [expr.cond]
3068 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3069 and function-to-pointer (_conv.func_) standard conversions are
3070 performed on the second and third operands.
3072 We need to force the lvalue-to-rvalue conversion here for class types,
3073 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3074 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3075 regions.
3077 We use ocp_convert rather than build_user_type_conversion because the
3078 latter returns NULL_TREE on failure, while the former gives an error. */
3080 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3081 arg2 = ocp_convert (TREE_TYPE (arg2), arg2,
3082 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3083 else
3084 arg2 = decay_conversion (arg2);
3085 arg2_type = TREE_TYPE (arg2);
3087 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3088 arg3 = ocp_convert (TREE_TYPE (arg3), arg3,
3089 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3090 else
3091 arg3 = decay_conversion (arg3);
3092 arg3_type = TREE_TYPE (arg3);
3094 /* [expr.cond]
3096 After those conversions, one of the following shall hold:
3098 --The second and third operands have the same type; the result is of
3099 that type. */
3100 if (same_type_p (arg2_type, arg3_type))
3101 result_type = arg2_type;
3102 /* [expr.cond]
3104 --The second and third operands have arithmetic or enumeration
3105 type; the usual arithmetic conversions are performed to bring
3106 them to a common type, and the result is of that type. */
3107 else if ((ARITHMETIC_TYPE_P (arg2_type)
3108 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3109 && (ARITHMETIC_TYPE_P (arg3_type)
3110 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3112 /* In this case, there is always a common type. */
3113 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3114 arg3_type);
3116 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3117 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3118 cp_warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3119 arg2_type, arg3_type);
3120 else if (extra_warnings
3121 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3122 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3123 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3124 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3125 cp_warning ("enumeral and non-enumeral type in conditional expression");
3127 arg2 = perform_implicit_conversion (result_type, arg2);
3128 arg3 = perform_implicit_conversion (result_type, arg3);
3130 /* [expr.cond]
3132 --The second and third operands have pointer type, or one has
3133 pointer type and the other is a null pointer constant; pointer
3134 conversions (_conv.ptr_) and qualification conversions
3135 (_conv.qual_) are performed to bring them to their composite
3136 pointer type (_expr.rel_). The result is of the composite
3137 pointer type.
3139 --The second and third operands have pointer to member type, or
3140 one has pointer to member type and the other is a null pointer
3141 constant; pointer to member conversions (_conv.mem_) and
3142 qualification conversions (_conv.qual_) are performed to bring
3143 them to a common type, whose cv-qualification shall match the
3144 cv-qualification of either the second or the third operand.
3145 The result is of the common type. */
3146 else if ((null_ptr_cst_p (arg2)
3147 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3148 || TYPE_PTRMEMFUNC_P (arg3_type)))
3149 || (null_ptr_cst_p (arg3)
3150 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3151 || TYPE_PTRMEMFUNC_P (arg2_type)))
3152 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3153 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3154 || (TYPE_PTRMEMFUNC_P (arg2_type)
3155 && TYPE_PTRMEMFUNC_P (arg3_type)))
3157 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3158 arg3, "conditional expression");
3159 arg2 = perform_implicit_conversion (result_type, arg2);
3160 arg3 = perform_implicit_conversion (result_type, arg3);
3163 if (!result_type)
3165 cp_error ("operands to ?: have different types");
3166 return error_mark_node;
3169 valid_operands:
3170 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3171 /* Expand both sides into the same slot, hopefully the target of the
3172 ?: expression. We used to check for TARGET_EXPRs here, but now we
3173 sometimes wrap them in NOP_EXPRs so the test would fail. */
3174 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3175 result = build_target_expr_with_type (result, result_type);
3177 /* If this expression is an rvalue, but might be mistaken for an
3178 lvalue, we must add a NON_LVALUE_EXPR. */
3179 if (!lvalue_p && real_lvalue_p (result))
3180 result = build1 (NON_LVALUE_EXPR, result_type, result);
3182 return result;
3185 tree
3186 build_new_op (code, flags, arg1, arg2, arg3)
3187 enum tree_code code;
3188 int flags;
3189 tree arg1, arg2, arg3;
3191 struct z_candidate *candidates = 0, *cand;
3192 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3193 enum tree_code code2 = NOP_EXPR;
3194 tree templates = NULL_TREE;
3195 tree conv;
3197 if (arg1 == error_mark_node
3198 || arg2 == error_mark_node
3199 || arg3 == error_mark_node)
3200 return error_mark_node;
3202 /* This can happen if a template takes all non-type parameters, e.g.
3203 undeclared_template<1, 5, 72>a; */
3204 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3206 cp_error ("`%D' must be declared before use", arg1);
3207 return error_mark_node;
3210 if (code == MODIFY_EXPR)
3212 code2 = TREE_CODE (arg3);
3213 arg3 = NULL_TREE;
3214 fnname = ansi_assopname (code2);
3216 else
3217 fnname = ansi_opname (code);
3219 switch (code)
3221 case NEW_EXPR:
3222 case VEC_NEW_EXPR:
3223 case VEC_DELETE_EXPR:
3224 case DELETE_EXPR:
3225 /* Use build_op_new_call and build_op_delete_call instead. */
3226 my_friendly_abort (981018);
3228 case CALL_EXPR:
3229 return build_object_call (arg1, arg2);
3231 default:
3232 break;
3235 /* The comma operator can have void args. */
3236 if (TREE_CODE (arg1) == OFFSET_REF)
3237 arg1 = resolve_offset_ref (arg1);
3238 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
3239 arg2 = resolve_offset_ref (arg2);
3240 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
3241 arg3 = resolve_offset_ref (arg3);
3243 if (code == COND_EXPR)
3245 if (arg2 == NULL_TREE
3246 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3247 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3248 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3249 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3250 goto builtin;
3252 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3253 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3254 goto builtin;
3256 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3257 arg2 = integer_zero_node;
3259 if (arg2 && arg3)
3260 arglist = tree_cons (NULL_TREE, arg1, tree_cons
3261 (NULL_TREE, arg2, build_tree_list (NULL_TREE, arg3)));
3262 else if (arg2)
3263 arglist = tree_cons (NULL_TREE, arg1, build_tree_list (NULL_TREE, arg2));
3264 else
3265 arglist = build_tree_list (NULL_TREE, arg1);
3267 fns = lookup_function_nonclass (fnname, arglist);
3269 if (fns && TREE_CODE (fns) == TREE_LIST)
3270 fns = TREE_VALUE (fns);
3271 for (; fns; fns = OVL_NEXT (fns))
3273 tree fn = OVL_CURRENT (fns);
3274 if (TREE_CODE (fn) == TEMPLATE_DECL)
3276 templates = tree_cons (NULL_TREE, fn, templates);
3277 candidates
3278 = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3279 arglist, TREE_TYPE (fnname),
3280 flags, DEDUCE_CALL);
3282 else
3283 candidates = add_function_candidate (candidates, fn, NULL_TREE,
3284 arglist, flags);
3287 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3289 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3290 if (fns == error_mark_node)
3291 return fns;
3293 else
3294 fns = NULL_TREE;
3296 if (fns)
3298 tree basetype = BINFO_TYPE (TREE_PURPOSE (fns));
3299 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3300 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3302 tree fn = OVL_CURRENT (fns);
3303 tree this_arglist;
3305 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3306 this_arglist = mem_arglist;
3307 else
3308 this_arglist = arglist;
3310 if (TREE_CODE (fn) == TEMPLATE_DECL)
3312 /* A member template. */
3313 templates = tree_cons (NULL_TREE, fn, templates);
3314 candidates
3315 = add_template_candidate (candidates, fn, basetype, NULL_TREE,
3316 this_arglist, TREE_TYPE (fnname),
3317 flags, DEDUCE_CALL);
3319 else
3320 candidates = add_function_candidate
3321 (candidates, fn, basetype, this_arglist, flags);
3323 if (candidates)
3324 candidates->basetype_path = TYPE_BINFO (TREE_TYPE (arg1));
3329 tree args[3];
3331 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3332 to know about two args; a builtin candidate will always have a first
3333 parameter of type bool. We'll handle that in
3334 build_builtin_candidate. */
3335 if (code == COND_EXPR)
3337 args[0] = arg2;
3338 args[1] = arg3;
3339 args[2] = arg1;
3341 else
3343 args[0] = arg1;
3344 args[1] = arg2;
3345 args[2] = NULL_TREE;
3348 candidates = add_builtin_candidates
3349 (candidates, code, code2, fnname, args, flags);
3352 if (! any_viable (candidates))
3354 switch (code)
3356 case POSTINCREMENT_EXPR:
3357 case POSTDECREMENT_EXPR:
3358 /* Look for an `operator++ (int)'. If they didn't have
3359 one, then we fall back to the old way of doing things. */
3360 if (flags & LOOKUP_COMPLAIN)
3361 cp_pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3362 fnname,
3363 operator_name_info[code].name);
3364 if (code == POSTINCREMENT_EXPR)
3365 code = PREINCREMENT_EXPR;
3366 else
3367 code = PREDECREMENT_EXPR;
3368 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3370 /* The caller will deal with these. */
3371 case ADDR_EXPR:
3372 case COMPOUND_EXPR:
3373 case COMPONENT_REF:
3374 return NULL_TREE;
3376 default:
3377 break;
3379 if (flags & LOOKUP_COMPLAIN)
3381 op_error (code, code2, arg1, arg2, arg3, "no match");
3382 print_z_candidates (candidates);
3384 return error_mark_node;
3386 candidates = splice_viable (candidates);
3387 cand = tourney (candidates);
3389 if (cand == 0)
3391 if (flags & LOOKUP_COMPLAIN)
3393 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3394 print_z_candidates (candidates);
3396 return error_mark_node;
3399 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3401 extern int warn_synth;
3402 if (warn_synth
3403 && fnname == ansi_assopname (NOP_EXPR)
3404 && DECL_ARTIFICIAL (cand->fn)
3405 && candidates->next
3406 && ! candidates->next->next)
3408 cp_warning ("using synthesized `%#D' for copy assignment",
3409 cand->fn);
3410 cp_warning_at (" where cfront would use `%#D'",
3411 cand == candidates
3412 ? candidates->next->fn
3413 : candidates->fn);
3416 return build_over_call
3417 (cand,
3418 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3419 ? mem_arglist : arglist,
3420 LOOKUP_NORMAL);
3423 /* Check for comparison of different enum types. */
3424 switch (code)
3426 case GT_EXPR:
3427 case LT_EXPR:
3428 case GE_EXPR:
3429 case LE_EXPR:
3430 case EQ_EXPR:
3431 case NE_EXPR:
3432 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3433 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3434 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3435 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3437 cp_warning ("comparison between `%#T' and `%#T'",
3438 TREE_TYPE (arg1), TREE_TYPE (arg2));
3440 break;
3441 default:
3442 break;
3445 /* We need to strip any leading REF_BIND so that bitfields don't cause
3446 errors. This should not remove any important conversions, because
3447 builtins don't apply to class objects directly. */
3448 conv = TREE_VEC_ELT (cand->convs, 0);
3449 if (TREE_CODE (conv) == REF_BIND)
3450 conv = TREE_OPERAND (conv, 0);
3451 arg1 = convert_like (conv, arg1);
3452 if (arg2)
3454 conv = TREE_VEC_ELT (cand->convs, 1);
3455 if (TREE_CODE (conv) == REF_BIND)
3456 conv = TREE_OPERAND (conv, 0);
3457 arg2 = convert_like (conv, arg2);
3459 if (arg3)
3461 conv = TREE_VEC_ELT (cand->convs, 2);
3462 if (TREE_CODE (conv) == REF_BIND)
3463 conv = TREE_OPERAND (conv, 0);
3464 arg3 = convert_like (conv, arg3);
3467 builtin:
3468 switch (code)
3470 case MODIFY_EXPR:
3471 return build_modify_expr (arg1, code2, arg2);
3473 case INDIRECT_REF:
3474 return build_indirect_ref (arg1, "unary *");
3476 case PLUS_EXPR:
3477 case MINUS_EXPR:
3478 case MULT_EXPR:
3479 case TRUNC_DIV_EXPR:
3480 case GT_EXPR:
3481 case LT_EXPR:
3482 case GE_EXPR:
3483 case LE_EXPR:
3484 case EQ_EXPR:
3485 case NE_EXPR:
3486 case MAX_EXPR:
3487 case MIN_EXPR:
3488 case LSHIFT_EXPR:
3489 case RSHIFT_EXPR:
3490 case TRUNC_MOD_EXPR:
3491 case BIT_AND_EXPR:
3492 case BIT_IOR_EXPR:
3493 case BIT_XOR_EXPR:
3494 case TRUTH_ANDIF_EXPR:
3495 case TRUTH_ORIF_EXPR:
3496 return cp_build_binary_op (code, arg1, arg2);
3498 case CONVERT_EXPR:
3499 case NEGATE_EXPR:
3500 case BIT_NOT_EXPR:
3501 case TRUTH_NOT_EXPR:
3502 case PREINCREMENT_EXPR:
3503 case POSTINCREMENT_EXPR:
3504 case PREDECREMENT_EXPR:
3505 case POSTDECREMENT_EXPR:
3506 case REALPART_EXPR:
3507 case IMAGPART_EXPR:
3508 return build_unary_op (code, arg1, candidates != 0);
3510 case ARRAY_REF:
3511 return build_array_ref (arg1, arg2);
3513 case COND_EXPR:
3514 return build_conditional_expr (arg1, arg2, arg3);
3516 case MEMBER_REF:
3517 return build_m_component_ref
3518 (build_indirect_ref (arg1, NULL_PTR), arg2);
3520 /* The caller will deal with these. */
3521 case ADDR_EXPR:
3522 case COMPONENT_REF:
3523 case COMPOUND_EXPR:
3524 return NULL_TREE;
3526 default:
3527 my_friendly_abort (367);
3528 return NULL_TREE;
3532 /* Build a call to operator delete. This has to be handled very specially,
3533 because the restrictions on what signatures match are different from all
3534 other call instances. For a normal delete, only a delete taking (void *)
3535 or (void *, size_t) is accepted. For a placement delete, only an exact
3536 match with the placement new is accepted.
3538 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3539 ADDR is the pointer to be deleted. For placement delete, it is also
3540 used to determine what the corresponding new looked like.
3541 SIZE is the size of the memory block to be deleted.
3542 FLAGS are the usual overloading flags.
3543 PLACEMENT is the corresponding placement new call, or 0. */
3545 tree
3546 build_op_delete_call (code, addr, size, flags, placement)
3547 enum tree_code code;
3548 tree addr, size, placement;
3549 int flags;
3551 tree fn, fns, fnname, fntype, argtypes, args, type;
3553 if (addr == error_mark_node)
3554 return error_mark_node;
3556 type = TREE_TYPE (TREE_TYPE (addr));
3557 while (TREE_CODE (type) == ARRAY_TYPE)
3558 type = TREE_TYPE (type);
3560 fnname = ansi_opname (code);
3562 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3563 /* In [class.free]
3565 If the result of the lookup is ambiguous or inaccessible, or if
3566 the lookup selects a placement deallocation function, the
3567 program is ill-formed.
3569 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3571 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3572 if (fns == error_mark_node)
3573 return error_mark_node;
3575 else
3576 fns = NULL_TREE;
3578 if (fns == NULL_TREE)
3579 fns = lookup_name_nonclass (fnname);
3581 if (placement)
3583 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3585 /* Extract the function. */
3586 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3587 /* Then the second parm type. */
3588 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3590 /* Also the second argument. */
3591 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3593 else
3595 /* First try it without the size argument. */
3596 argtypes = void_list_node;
3597 args = NULL_TREE;
3600 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3601 fntype = build_function_type (void_type_node, argtypes);
3603 /* Strip const and volatile from addr. */
3604 addr = cp_convert (ptr_type_node, addr);
3606 fn = instantiate_type (fntype, fns, itf_no_attributes);
3608 if (fn != error_mark_node)
3610 if (TREE_CODE (fns) == TREE_LIST)
3611 /* Member functions. */
3612 enforce_access (type, fn);
3613 return build_function_call (fn, tree_cons (NULL_TREE, addr, args));
3616 /* If we are doing placement delete we do nothing if we don't find a
3617 matching op delete. */
3618 if (placement)
3619 return NULL_TREE;
3621 /* Normal delete; now try to find a match including the size argument. */
3622 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3623 tree_cons (NULL_TREE, sizetype, void_list_node));
3624 fntype = build_function_type (void_type_node, argtypes);
3626 fn = instantiate_type (fntype, fns, itf_no_attributes);
3628 if (fn != error_mark_node)
3630 if (BASELINK_P (fns))
3631 /* Member functions. */
3632 enforce_access (type, fn);
3633 return build_function_call
3634 (fn, tree_cons (NULL_TREE, addr,
3635 build_tree_list (NULL_TREE, size)));
3638 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3639 destructor, in which case the error should be deferred
3640 until someone actually tries to delete one of these. */
3641 if (flags & LOOKUP_SPECULATIVELY)
3642 return NULL_TREE;
3644 cp_error ("no suitable `operator delete' for `%T'", type);
3645 return error_mark_node;
3648 /* If the current scope isn't allowed to access DECL along
3649 BASETYPE_PATH, give an error. The most derived class in
3650 BASETYPE_PATH is the one used to qualify DECL. */
3653 enforce_access (basetype_path, decl)
3654 tree basetype_path;
3655 tree decl;
3657 int accessible;
3659 accessible = accessible_p (basetype_path, decl);
3660 if (!accessible)
3662 if (TREE_PRIVATE (decl))
3663 cp_error_at ("`%+#D' is private", decl);
3664 else if (TREE_PROTECTED (decl))
3665 cp_error_at ("`%+#D' is protected", decl);
3666 else
3667 cp_error_at ("`%+#D' is inaccessible", decl);
3668 cp_error ("within this context");
3669 return 0;
3672 return 1;
3675 /* Perform the conversions in CONVS on the expression EXPR.
3676 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3677 indicates the `this' argument of a method. INNER is non-zero when
3678 being called to continue a conversion chain. */
3680 static tree
3681 convert_like_real (convs, expr, fn, argnum, inner)
3682 tree convs, expr;
3683 tree fn;
3684 int argnum;
3685 int inner;
3687 extern int warningcount, errorcount;
3688 int savew, savee;
3690 tree totype = TREE_TYPE (convs);
3692 if (ICS_BAD_FLAG (convs)
3693 && TREE_CODE (convs) != USER_CONV
3694 && TREE_CODE (convs) != AMBIG_CONV
3695 && TREE_CODE (convs) != REF_BIND)
3697 tree t = convs;
3698 for (; t; t = TREE_OPERAND (t, 0))
3700 if (TREE_CODE (t) == USER_CONV)
3702 expr = convert_like_real (t, expr, fn, argnum, 1);
3703 break;
3705 else if (TREE_CODE (t) == AMBIG_CONV)
3706 return convert_like_real (t, expr, fn, argnum, 1);
3707 else if (TREE_CODE (t) == IDENTITY_CONV)
3708 break;
3710 return convert_for_initialization
3711 (NULL_TREE, totype, expr, LOOKUP_NORMAL,
3712 "conversion", fn, argnum);
3715 if (!inner)
3716 expr = dubious_conversion_warnings
3717 (totype, expr, "argument", fn, argnum);
3718 switch (TREE_CODE (convs))
3720 case USER_CONV:
3722 struct z_candidate *cand
3723 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3724 tree convfn = cand->fn;
3725 tree args;
3727 if (DECL_CONSTRUCTOR_P (convfn))
3729 tree t = build_int_2 (0, 0);
3730 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3732 args = build_tree_list (NULL_TREE, expr);
3733 if (DECL_HAS_IN_CHARGE_PARM_P (convfn))
3734 args = tree_cons (NULL_TREE, integer_one_node, args);
3735 args = tree_cons (NULL_TREE, t, args);
3737 else
3738 args = build_this (expr);
3739 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3741 /* If this is a constructor or a function returning an aggr type,
3742 we need to build up a TARGET_EXPR. */
3743 if (DECL_CONSTRUCTOR_P (convfn))
3744 expr = build_cplus_new (totype, expr);
3746 /* The result of the call is then used to direct-initialize the object
3747 that is the destination of the copy-initialization. [dcl.init]
3749 Note that this step is not reflected in the conversion sequence;
3750 it affects the semantics when we actually perform the
3751 conversion, but is not considered during overload resolution.
3753 If the target is a class, that means call a ctor. */
3754 if (IS_AGGR_TYPE (totype))
3756 savew = warningcount, savee = errorcount;
3757 expr = build_new_method_call
3758 (NULL_TREE, complete_ctor_identifier,
3759 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3760 /* Core issue 84, now a DR, says that we don't allow UDCs
3761 for these args (which deliberately breaks copy-init of an
3762 auto_ptr<Base> from an auto_ptr<Derived>). */
3763 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3765 /* Tell the user where this failing constructor call came from. */
3766 if (fn)
3768 if (warningcount > savew)
3769 cp_warning
3770 (" initializing argument %P of `%D' from result of `%D'",
3771 argnum, fn, convfn);
3772 else if (errorcount > savee)
3773 cp_error
3774 (" initializing argument %P of `%D' from result of `%D'",
3775 argnum, fn, convfn);
3777 else
3779 if (warningcount > savew)
3780 cp_warning (" initializing temporary from result of `%D'",
3781 convfn);
3782 else if (errorcount > savee)
3783 cp_error (" initializing temporary from result of `%D'",
3784 convfn);
3786 expr = build_cplus_new (totype, expr);
3788 return expr;
3790 case IDENTITY_CONV:
3791 if (type_unknown_p (expr))
3792 expr = instantiate_type (totype, expr, itf_complain);
3793 return expr;
3794 case AMBIG_CONV:
3795 /* Call build_user_type_conversion again for the error. */
3796 return build_user_type_conversion
3797 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3799 default:
3800 break;
3803 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum, 1);
3804 if (expr == error_mark_node)
3805 return error_mark_node;
3807 /* Convert a non-array constant variable to its underlying value, unless we
3808 are about to bind it to a reference, in which case we need to
3809 leave it as an lvalue. */
3810 if (TREE_CODE (convs) != REF_BIND
3811 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3812 expr = decl_constant_value (expr);
3814 switch (TREE_CODE (convs))
3816 case RVALUE_CONV:
3817 if (! IS_AGGR_TYPE (totype))
3818 return expr;
3819 /* else fall through */
3820 case BASE_CONV:
3821 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3823 /* We are going to bind a reference directly to a base-class
3824 subobject of EXPR. */
3825 tree base_ptr = build_pointer_type (totype);
3827 /* Build an expression for `*((base*) &expr)'. */
3828 expr = build_unary_op (ADDR_EXPR, expr, 0);
3829 expr = perform_implicit_conversion (base_ptr, expr);
3830 expr = build_indirect_ref (expr, "implicit conversion");
3831 return expr;
3834 /* Copy-initialization where the cv-unqualified version of the source
3835 type is the same class as, or a derived class of, the class of the
3836 destination [is treated as direct-initialization]. [dcl.init] */
3837 if (fn)
3838 savew = warningcount, savee = errorcount;
3839 expr = build_new_method_call (NULL_TREE, complete_ctor_identifier,
3840 build_tree_list (NULL_TREE, expr),
3841 TYPE_BINFO (totype),
3842 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
3843 if (fn)
3845 if (warningcount > savew)
3846 cp_warning (" initializing argument %P of `%D'", argnum, fn);
3847 else if (errorcount > savee)
3848 cp_error (" initializing argument %P of `%D'", argnum, fn);
3850 return build_cplus_new (totype, expr);
3852 case REF_BIND:
3854 tree ref_type = totype;
3856 /* If necessary, create a temporary. */
3857 if (NEED_TEMPORARY_P (convs))
3859 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3860 expr = build_target_expr_with_type (expr, type);
3863 /* Take the address of the thing to which we will bind the
3864 reference. */
3865 expr = build_unary_op (ADDR_EXPR, expr, 1);
3866 if (expr == error_mark_node)
3867 return error_mark_node;
3869 /* Convert it to a pointer to the type referred to by the
3870 reference. This will adjust the pointer if a derived to
3871 base conversion is being performed. */
3872 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3873 expr);
3874 /* Convert the pointer to the desired reference type. */
3875 expr = build1 (NOP_EXPR, ref_type, expr);
3877 return expr;
3880 case LVALUE_CONV:
3881 return decay_conversion (expr);
3883 case QUAL_CONV:
3884 /* Warn about deprecated conversion if appropriate. */
3885 string_conv_p (totype, expr, 1);
3886 break;
3888 default:
3889 break;
3891 return ocp_convert (totype, expr, CONV_IMPLICIT,
3892 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3895 /* ARG is being passed to a varargs function. Perform any conversions
3896 required. Array/function to pointer decay must have already happened.
3897 Return the converted value. */
3899 tree
3900 convert_arg_to_ellipsis (arg)
3901 tree arg;
3903 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3904 && (TYPE_PRECISION (TREE_TYPE (arg))
3905 < TYPE_PRECISION (double_type_node)))
3906 /* Convert `float' to `double'. */
3907 arg = cp_convert (double_type_node, arg);
3908 else
3909 /* Convert `short' and `char' to full-size `int'. */
3910 arg = default_conversion (arg);
3912 arg = require_complete_type (arg);
3914 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
3916 /* Undefined behaviour [expr.call] 5.2.2/7. */
3917 cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3918 TREE_TYPE (arg));
3921 return arg;
3924 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
3926 tree
3927 build_x_va_arg (expr, type)
3928 tree expr;
3929 tree type;
3931 if (processing_template_decl)
3932 return build_min (VA_ARG_EXPR, type, expr);
3934 type = complete_type_or_else (type, NULL_TREE);
3936 if (expr == error_mark_node || !type)
3937 return error_mark_node;
3939 if (! pod_type_p (type))
3941 /* Undefined behaviour [expr.call] 5.2.2/7. */
3942 cp_warning ("cannot receive objects of non-POD type `%#T' through `...'",
3943 type);
3946 return build_va_arg (expr, type);
3949 /* TYPE has been given to va_arg. Apply the default conversions which would
3950 have happened when passed via ellipsis. Return the promoted type, or
3951 NULL_TREE, if there is no change. */
3953 tree
3954 convert_type_from_ellipsis (type)
3955 tree type;
3957 tree promote;
3959 if (TREE_CODE (type) == ARRAY_TYPE)
3960 promote = build_pointer_type (TREE_TYPE (type));
3961 else if (TREE_CODE (type) == FUNCTION_TYPE)
3962 promote = build_pointer_type (type);
3963 else
3964 promote = type_promotes_to (type);
3966 return same_type_p (type, promote) ? NULL_TREE : promote;
3969 /* ARG is a default argument expression being passed to a parameter of
3970 the indicated TYPE, which is a parameter to FN. Do any required
3971 conversions. Return the converted value. */
3973 tree
3974 convert_default_arg (type, arg, fn, parmnum)
3975 tree type;
3976 tree arg;
3977 tree fn;
3978 int parmnum;
3980 if (TREE_CODE (arg) == DEFAULT_ARG)
3982 /* When processing the default args for a class, we can find that
3983 there is an ordering constraint, and we call a function who's
3984 default args have not yet been converted. For instance,
3985 class A {
3986 A (int = 0);
3987 void Foo (A const & = A ());
3989 We must process A::A before A::Foo's default arg can be converted.
3990 Remember the dependent function, so do_pending_defargs can retry,
3991 and check loops. */
3992 unprocessed_defarg_fn (fn);
3994 /* Don't return error_mark node, as we won't be able to distinguish
3995 genuine errors from this case, and that would lead to repeated
3996 diagnostics. Just make something of the right type. */
3997 return build1 (NOP_EXPR, type, integer_zero_node);
4000 if (fn && DECL_TEMPLATE_INFO (fn))
4001 arg = tsubst_default_argument (fn, type, arg);
4003 arg = break_out_target_exprs (arg);
4005 if (TREE_CODE (arg) == CONSTRUCTOR)
4007 arg = digest_init (type, arg, 0);
4008 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4009 "default argument", fn, parmnum);
4011 else
4013 /* This could get clobbered by the following call. */
4014 if (TREE_HAS_CONSTRUCTOR (arg))
4015 arg = copy_node (arg);
4017 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4018 "default argument", fn, parmnum);
4019 if (PROMOTE_PROTOTYPES
4020 && (TREE_CODE (type) == INTEGER_TYPE
4021 || TREE_CODE (type) == ENUMERAL_TYPE)
4022 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4023 arg = default_conversion (arg);
4026 return arg;
4029 static tree
4030 build_over_call (cand, args, flags)
4031 struct z_candidate *cand;
4032 tree args;
4033 int flags;
4035 tree fn = cand->fn;
4036 tree convs = cand->convs;
4037 tree converted_args = NULL_TREE;
4038 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4039 tree conv, arg, val;
4040 int i = 0;
4041 int is_method = 0;
4043 /* Give any warnings we noticed during overload resolution. */
4044 if (cand->warnings)
4045 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4046 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
4048 if (DECL_FUNCTION_MEMBER_P (fn))
4049 enforce_access (cand->basetype_path, fn);
4051 if (args && TREE_CODE (args) != TREE_LIST)
4052 args = build_tree_list (NULL_TREE, args);
4053 arg = args;
4055 /* The implicit parameters to a constructor are not considered by overload
4056 resolution, and must be of the proper type. */
4057 if (DECL_CONSTRUCTOR_P (fn))
4059 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4060 arg = TREE_CHAIN (arg);
4061 parm = TREE_CHAIN (parm);
4062 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4064 converted_args = tree_cons
4065 (NULL_TREE, TREE_VALUE (arg), converted_args);
4066 arg = TREE_CHAIN (arg);
4067 parm = TREE_CHAIN (parm);
4070 /* Bypass access control for 'this' parameter. */
4071 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4073 tree parmtype = TREE_VALUE (parm);
4074 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4075 tree t;
4076 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4077 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4078 TREE_TYPE (argtype), fn);
4080 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4081 X is called for an object that is not of type X, or of a type
4082 derived from X, the behavior is undefined.
4084 So we can assume that anything passed as 'this' is non-null, and
4085 optimize accordingly. */
4086 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4087 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
4088 converted_args = tree_cons (NULL_TREE, t, converted_args);
4089 parm = TREE_CHAIN (parm);
4090 arg = TREE_CHAIN (arg);
4091 ++i;
4092 is_method = 1;
4095 for (; arg && parm;
4096 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4098 tree type = TREE_VALUE (parm);
4100 conv = TREE_VEC_ELT (convs, i);
4101 if (ICS_BAD_FLAG (conv))
4103 tree t = conv;
4104 val = TREE_VALUE (arg);
4106 for (; t; t = TREE_OPERAND (t, 0))
4108 if (TREE_CODE (t) == USER_CONV
4109 || TREE_CODE (t) == AMBIG_CONV)
4111 val = convert_like_with_context (t, val, fn, i - is_method);
4112 break;
4114 else if (TREE_CODE (t) == IDENTITY_CONV)
4115 break;
4117 val = convert_for_initialization
4118 (NULL_TREE, type, val, LOOKUP_NORMAL,
4119 "argument", fn, i - is_method);
4121 else
4123 val = TREE_VALUE (arg);
4124 val = convert_like_with_context
4125 (conv, TREE_VALUE (arg), fn, i - is_method);
4128 if (PROMOTE_PROTOTYPES
4129 && (TREE_CODE (type) == INTEGER_TYPE
4130 || TREE_CODE (type) == ENUMERAL_TYPE)
4131 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4132 val = default_conversion (val);
4133 converted_args = tree_cons (NULL_TREE, val, converted_args);
4136 /* Default arguments */
4137 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4138 converted_args
4139 = tree_cons (NULL_TREE,
4140 convert_default_arg (TREE_VALUE (parm),
4141 TREE_PURPOSE (parm),
4142 fn, i - is_method),
4143 converted_args);
4145 /* Ellipsis */
4146 for (; arg; arg = TREE_CHAIN (arg))
4147 converted_args
4148 = tree_cons (NULL_TREE,
4149 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4150 converted_args);
4152 converted_args = nreverse (converted_args);
4154 if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
4155 check_function_format (NULL, DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
4156 converted_args);
4158 /* Avoid actually calling copy constructors and copy assignment operators,
4159 if possible. */
4161 if (! flag_elide_constructors)
4162 /* Do things the hard way. */;
4163 else if (TREE_VEC_LENGTH (convs) == 1
4164 && DECL_COPY_CONSTRUCTOR_P (fn))
4166 tree targ;
4167 arg = TREE_CHAIN (converted_args);
4168 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4169 arg = TREE_CHAIN (arg);
4170 arg = TREE_VALUE (arg);
4172 /* Pull out the real argument, disregarding const-correctness. */
4173 targ = arg;
4174 while (TREE_CODE (targ) == NOP_EXPR
4175 || TREE_CODE (targ) == NON_LVALUE_EXPR
4176 || TREE_CODE (targ) == CONVERT_EXPR)
4177 targ = TREE_OPERAND (targ, 0);
4178 if (TREE_CODE (targ) == ADDR_EXPR)
4180 targ = TREE_OPERAND (targ, 0);
4181 if (!same_type_ignoring_top_level_qualifiers_p
4182 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4183 targ = NULL_TREE;
4185 else
4186 targ = NULL_TREE;
4188 if (targ)
4189 arg = targ;
4190 else
4191 arg = build_indirect_ref (arg, 0);
4193 /* [class.copy]: the copy constructor is implicitly defined even if
4194 the implementation elided its use. */
4195 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4196 mark_used (fn);
4198 /* If we're creating a temp and we already have one, don't create a
4199 new one. If we're not creating a temp but we get one, use
4200 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4201 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4202 temp or an INIT_EXPR otherwise. */
4203 if (integer_zerop (TREE_VALUE (args)))
4205 if (! real_lvalue_p (arg))
4206 return arg;
4207 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4208 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4210 else if (! real_lvalue_p (arg)
4211 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4213 tree address;
4214 tree to = stabilize_reference
4215 (build_indirect_ref (TREE_VALUE (args), 0));
4217 /* If we're initializing an empty class, then we actually
4218 have to use a MODIFY_EXPR rather than an INIT_EXPR. The
4219 reason is that the dummy padding member in the target may
4220 not actually be allocated if TO is a base class
4221 subobject. Since we've set TYPE_NONCOPIED_PARTS on the
4222 padding, a MODIFY_EXPR will preserve its value, which is
4223 the right thing to do if it's not really padding at all.
4225 It's not safe to just throw away the ARG if we're looking
4226 at an empty class because the ARG might contain a
4227 TARGET_EXPR which wants to be bound to TO. If it is not,
4228 expand_expr will assign a dummy slot for the TARGET_EXPR,
4229 and we will call a destructor for it, which is wrong,
4230 because we will also destroy TO, but will never have
4231 constructed it. */
4232 val = build (is_empty_class (DECL_CONTEXT (fn))
4233 ? MODIFY_EXPR : INIT_EXPR,
4234 DECL_CONTEXT (fn), to, arg);
4235 address = build_unary_op (ADDR_EXPR, val, 0);
4236 /* Avoid a warning about this expression, if the address is
4237 never used. */
4238 TREE_USED (address) = 1;
4239 return address;
4242 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4243 && copy_args_p (fn)
4244 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4246 tree to = stabilize_reference
4247 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4249 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4251 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4252 return val;
4255 mark_used (fn);
4257 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4259 tree t, *p = &TREE_VALUE (converted_args);
4260 tree binfo = get_binfo
4261 (DECL_VIRTUAL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
4262 *p = convert_pointer_to_real (binfo, *p);
4263 if (TREE_SIDE_EFFECTS (*p))
4264 *p = save_expr (*p);
4265 t = build_pointer_type (TREE_TYPE (fn));
4266 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4267 TREE_TYPE (fn) = t;
4269 else if (DECL_INLINE (fn))
4270 fn = inline_conversion (fn);
4271 else
4272 fn = build_addr_func (fn);
4274 /* Recognize certain built-in functions so we can make tree-codes
4275 other than CALL_EXPR. We do this when it enables fold-const.c
4276 to do something useful. */
4278 if (TREE_CODE (fn) == ADDR_EXPR
4279 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4280 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4282 tree exp;
4283 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4284 if (exp)
4285 return exp;
4288 /* Some built-in function calls will be evaluated at
4289 compile-time in fold (). */
4290 fn = fold (build_call (fn, converted_args));
4291 if (VOID_TYPE_P (TREE_TYPE (fn)))
4292 return fn;
4293 fn = require_complete_type (fn);
4294 if (fn == error_mark_node)
4295 return error_mark_node;
4296 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4297 fn = build_cplus_new (TREE_TYPE (fn), fn);
4298 return convert_from_reference (fn);
4301 /* Returns the value to use for the in-charge parameter when making a
4302 call to a function with the indicated NAME. */
4304 tree
4305 in_charge_arg_for_name (name)
4306 tree name;
4308 if (name == base_ctor_identifier
4309 || name == base_dtor_identifier)
4310 return integer_zero_node;
4311 else if (name == complete_ctor_identifier)
4312 return integer_one_node;
4313 else if (name == complete_dtor_identifier)
4314 return integer_two_node;
4315 else if (name == deleting_dtor_identifier)
4316 return integer_three_node;
4318 /* This function should only be called with one of the names listed
4319 above. */
4320 my_friendly_abort (20000411);
4321 return NULL_TREE;
4324 static tree
4325 build_new_method_call (instance, name, args, basetype_path, flags)
4326 tree instance, name, args, basetype_path;
4327 int flags;
4329 struct z_candidate *candidates = 0, *cand;
4330 tree explicit_targs = NULL_TREE;
4331 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4332 tree pretty_name;
4333 tree user_args;
4334 tree templates = NULL_TREE;
4335 tree call;
4336 int template_only = 0;
4338 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4340 explicit_targs = TREE_OPERAND (name, 1);
4341 name = TREE_OPERAND (name, 0);
4342 if (DECL_P (name))
4343 name = DECL_NAME (name);
4344 else
4346 if (TREE_CODE (name) == COMPONENT_REF)
4347 name = TREE_OPERAND (name, 1);
4348 if (TREE_CODE (name) == OVERLOAD)
4349 name = DECL_NAME (OVL_CURRENT (name));
4352 template_only = 1;
4355 user_args = args;
4356 args = resolve_args (args);
4358 if (args == error_mark_node)
4359 return error_mark_node;
4361 if (instance == NULL_TREE)
4362 basetype = BINFO_TYPE (basetype_path);
4363 else
4365 if (TREE_CODE (instance) == OFFSET_REF)
4366 instance = resolve_offset_ref (instance);
4367 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4368 instance = convert_from_reference (instance);
4369 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4371 /* XXX this should be handled before we get here. */
4372 if (! IS_AGGR_TYPE (basetype))
4374 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4375 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4376 name, instance, basetype);
4378 return error_mark_node;
4382 if (basetype_path == NULL_TREE)
4383 basetype_path = TYPE_BINFO (basetype);
4385 if (instance)
4387 instance_ptr = build_this (instance);
4389 if (! template_only)
4391 /* XXX this should be handled before we get here. */
4392 fns = build_field_call (basetype_path, instance_ptr, name, args);
4393 if (fns)
4394 return fns;
4397 else
4399 instance_ptr = build_int_2 (0, 0);
4400 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4403 /* Callers should explicitly indicate whether they want to construct
4404 the complete object or just the part without virtual bases. */
4405 my_friendly_assert (name != ctor_identifier, 20000408);
4406 /* Similarly for destructors. */
4407 my_friendly_assert (name != dtor_identifier, 20000408);
4409 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4411 int constructor_p;
4413 constructor_p = (name == complete_ctor_identifier
4414 || name == base_ctor_identifier);
4415 pretty_name = (constructor_p
4416 ? constructor_name (basetype) : dtor_identifier);
4418 if (!flag_new_abi)
4420 /* Add the in-charge parameter as an implicit first argument. */
4421 if (!constructor_p
4422 || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4423 args = tree_cons (NULL_TREE,
4424 in_charge_arg_for_name (name),
4425 args);
4427 /* We want to call the normal constructor function under the
4428 old ABI. */
4429 name = constructor_p ? ctor_identifier : dtor_identifier;
4431 /* If we're a call to a constructor or destructor for a
4432 subobject that uses virtual base classes, then we need to
4433 pass down a pointer to a VTT for the subobject. */
4434 else if ((name == base_ctor_identifier
4435 || name == base_dtor_identifier)
4436 && TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4438 tree vtt;
4439 tree sub_vtt;
4440 tree basebinfo = basetype_path;
4442 /* If the current function is a complete object constructor
4443 or destructor, then we fetch the VTT directly.
4444 Otherwise, we look it up using the VTT we were given. */
4445 vtt = IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type));
4446 vtt = build_unary_op (ADDR_EXPR, vtt, /*noconvert=*/1);
4447 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4448 DECL_USE_VTT_PARM (current_function_decl),
4449 DECL_VTT_PARM (current_function_decl),
4450 vtt);
4451 if (TREE_VIA_VIRTUAL (basebinfo))
4452 basebinfo = binfo_for_vbase (basetype, current_class_type);
4453 my_friendly_assert (BINFO_SUBVTT_INDEX (basebinfo), 20010110);
4454 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4455 BINFO_SUBVTT_INDEX (basebinfo));
4456 sub_vtt = build_indirect_ref (sub_vtt, NULL);
4458 args = tree_cons (NULL_TREE, sub_vtt, args);
4461 else
4462 pretty_name = name;
4464 fns = lookup_fnfields (basetype_path, name, 1);
4466 if (fns == error_mark_node)
4467 return error_mark_node;
4468 if (fns)
4470 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
4471 tree fn = TREE_VALUE (fns);
4472 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4473 for (; fn; fn = OVL_NEXT (fn))
4475 tree t = OVL_CURRENT (fn);
4476 tree this_arglist;
4478 /* We can end up here for copy-init of same or base class. */
4479 if ((flags & LOOKUP_ONLYCONVERTING)
4480 && DECL_NONCONVERTING_P (t))
4481 continue;
4483 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4484 this_arglist = mem_args;
4485 else
4486 this_arglist = args;
4488 if (TREE_CODE (t) == TEMPLATE_DECL)
4490 /* A member template. */
4491 templates = tree_cons (NULL_TREE, t, templates);
4492 candidates =
4493 add_template_candidate (candidates, t, base, explicit_targs,
4494 this_arglist,
4495 TREE_TYPE (name), flags, DEDUCE_CALL);
4497 else if (! template_only)
4498 candidates = add_function_candidate (candidates, t, base,
4499 this_arglist, flags);
4501 if (candidates)
4502 candidates->basetype_path = basetype_path;
4506 if (! any_viable (candidates))
4508 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4509 if (flags & LOOKUP_SPECULATIVELY)
4510 return NULL_TREE;
4511 if (!COMPLETE_TYPE_P (basetype))
4512 incomplete_type_error (instance_ptr, basetype);
4513 else
4514 cp_error ("no matching function for call to `%T::%D(%A)%V'",
4515 basetype, pretty_name, user_args,
4516 TREE_TYPE (TREE_TYPE (instance_ptr)));
4517 print_z_candidates (candidates);
4518 return error_mark_node;
4520 candidates = splice_viable (candidates);
4521 cand = tourney (candidates);
4523 if (cand == 0)
4525 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4526 user_args);
4527 print_z_candidates (candidates);
4528 return error_mark_node;
4531 if (DECL_PURE_VIRTUAL_P (cand->fn)
4532 && instance == current_class_ref
4533 && (DECL_CONSTRUCTOR_P (current_function_decl)
4534 || DECL_DESTRUCTOR_P (current_function_decl))
4535 && ! (flags & LOOKUP_NONVIRTUAL)
4536 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4537 cp_error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4538 "abstract virtual `%#D' called from constructor"
4539 : "abstract virtual `%#D' called from destructor"),
4540 cand->fn);
4541 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4542 && is_dummy_object (instance_ptr))
4544 cp_error ("cannot call member function `%D' without object", cand->fn);
4545 return error_mark_node;
4548 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4549 && ((instance == current_class_ref && (dtor_label || ctor_label))
4550 || resolves_to_fixed_type_p (instance, 0)))
4551 flags |= LOOKUP_NONVIRTUAL;
4553 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4554 call = build_over_call (cand, mem_args, flags);
4555 else
4557 call = build_over_call (cand, args, flags);
4558 /* Do evaluate the object parameter in a call to a static member
4559 function. */
4560 if (TREE_SIDE_EFFECTS (instance))
4561 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4564 return call;
4567 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4568 subsequence of ICS2. */
4570 static int
4571 is_subseq (ics1, ics2)
4572 tree ics1, ics2;
4574 /* We can assume that a conversion of the same code
4575 between the same types indicates a subsequence since we only get
4576 here if the types we are converting from are the same. */
4578 while (TREE_CODE (ics1) == RVALUE_CONV
4579 || TREE_CODE (ics1) == LVALUE_CONV)
4580 ics1 = TREE_OPERAND (ics1, 0);
4582 while (1)
4584 while (TREE_CODE (ics2) == RVALUE_CONV
4585 || TREE_CODE (ics2) == LVALUE_CONV)
4586 ics2 = TREE_OPERAND (ics2, 0);
4588 if (TREE_CODE (ics2) == USER_CONV
4589 || TREE_CODE (ics2) == AMBIG_CONV
4590 || TREE_CODE (ics2) == IDENTITY_CONV)
4591 /* At this point, ICS1 cannot be a proper subsequence of
4592 ICS2. We can get a USER_CONV when we are comparing the
4593 second standard conversion sequence of two user conversion
4594 sequences. */
4595 return 0;
4597 ics2 = TREE_OPERAND (ics2, 0);
4599 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4600 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4601 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4602 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4603 return 1;
4607 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4608 be any _TYPE nodes. */
4611 is_properly_derived_from (derived, base)
4612 tree derived;
4613 tree base;
4615 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4616 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4617 return 0;
4619 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4620 considers every class derived from itself. */
4621 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4622 && DERIVED_FROM_P (base, derived));
4625 /* We build the ICS for an implicit object parameter as a pointer
4626 conversion sequence. However, such a sequence should be compared
4627 as if it were a reference conversion sequence. If ICS is the
4628 implicit conversion sequence for an implicit object parameter,
4629 modify it accordingly. */
4631 static void
4632 maybe_handle_implicit_object (ics)
4633 tree* ics;
4635 if (ICS_THIS_FLAG (*ics))
4637 /* [over.match.funcs]
4639 For non-static member functions, the type of the
4640 implicit object parameter is "reference to cv X"
4641 where X is the class of which the function is a
4642 member and cv is the cv-qualification on the member
4643 function declaration. */
4644 tree t = *ics;
4645 tree reference_type;
4647 /* The `this' parameter is a pointer to a class type. Make the
4648 implict conversion talk about a reference to that same class
4649 type. */
4650 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4651 reference_type = build_reference_type (reference_type);
4653 if (TREE_CODE (t) == QUAL_CONV)
4654 t = TREE_OPERAND (t, 0);
4655 if (TREE_CODE (t) == PTR_CONV)
4656 t = TREE_OPERAND (t, 0);
4657 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4658 t = direct_reference_binding (reference_type, t);
4659 *ics = t;
4663 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
4664 to the type the reference originally referred to, and return 1.
4665 Otherwise, return 0. */
4667 static int
4668 maybe_handle_ref_bind (ics, target_type)
4669 tree* ics;
4670 tree* target_type;
4672 if (TREE_CODE (*ics) == REF_BIND)
4674 *target_type = TREE_TYPE (TREE_TYPE (*ics));
4675 *ics = TREE_OPERAND (*ics, 0);
4676 return 1;
4679 return 0;
4682 /* Compare two implicit conversion sequences according to the rules set out in
4683 [over.ics.rank]. Return values:
4685 1: ics1 is better than ics2
4686 -1: ics2 is better than ics1
4687 0: ics1 and ics2 are indistinguishable */
4689 static int
4690 compare_ics (ics1, ics2)
4691 tree ics1, ics2;
4693 tree from_type1;
4694 tree from_type2;
4695 tree to_type1;
4696 tree to_type2;
4697 tree deref_from_type1 = NULL_TREE;
4698 tree deref_from_type2 = NULL_TREE;
4699 tree deref_to_type1 = NULL_TREE;
4700 tree deref_to_type2 = NULL_TREE;
4702 /* REF_BINDING is non-zero if the result of the conversion sequence
4703 is a reference type. In that case TARGET_TYPE is the
4704 type referred to by the reference. */
4705 int ref_binding1;
4706 int ref_binding2;
4707 tree target_type1;
4708 tree target_type2;
4710 /* Handle implicit object parameters. */
4711 maybe_handle_implicit_object (&ics1);
4712 maybe_handle_implicit_object (&ics2);
4714 /* Handle reference parameters. */
4715 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
4716 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
4718 /* [over.ics.rank]
4720 When comparing the basic forms of implicit conversion sequences (as
4721 defined in _over.best.ics_)
4723 --a standard conversion sequence (_over.ics.scs_) is a better
4724 conversion sequence than a user-defined conversion sequence
4725 or an ellipsis conversion sequence, and
4727 --a user-defined conversion sequence (_over.ics.user_) is a
4728 better conversion sequence than an ellipsis conversion sequence
4729 (_over.ics.ellipsis_). */
4730 if (ICS_RANK (ics1) > ICS_RANK (ics2))
4731 return -1;
4732 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
4733 return 1;
4735 if (ICS_RANK (ics1) == BAD_RANK)
4737 /* Both ICS are bad. We try to make a decision based on what
4738 would have happenned if they'd been good. */
4739 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4740 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4741 return -1;
4742 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4743 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4744 return 1;
4746 /* We couldn't make up our minds; try to figure it out below. */
4749 if (ICS_ELLIPSIS_FLAG (ics1))
4750 /* Both conversions are ellipsis conversions. */
4751 return 0;
4753 /* User-defined conversion sequence U1 is a better conversion sequence
4754 than another user-defined conversion sequence U2 if they contain the
4755 same user-defined conversion operator or constructor and if the sec-
4756 ond standard conversion sequence of U1 is better than the second
4757 standard conversion sequence of U2. */
4759 if (ICS_USER_FLAG (ics1))
4761 tree t1, t2;
4763 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4764 if (TREE_CODE (t1) == AMBIG_CONV)
4765 return 0;
4766 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4767 if (TREE_CODE (t2) == AMBIG_CONV)
4768 return 0;
4770 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4771 return 0;
4773 /* We can just fall through here, after setting up
4774 FROM_TYPE1 and FROM_TYPE2. */
4775 from_type1 = TREE_TYPE (t1);
4776 from_type2 = TREE_TYPE (t2);
4778 else
4780 /* We're dealing with two standard conversion sequences.
4782 [over.ics.rank]
4784 Standard conversion sequence S1 is a better conversion
4785 sequence than standard conversion sequence S2 if
4787 --S1 is a proper subsequence of S2 (comparing the conversion
4788 sequences in the canonical form defined by _over.ics.scs_,
4789 excluding any Lvalue Transformation; the identity
4790 conversion sequence is considered to be a subsequence of
4791 any non-identity conversion sequence */
4793 from_type1 = ics1;
4794 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4795 from_type1 = TREE_OPERAND (from_type1, 0);
4796 from_type1 = TREE_TYPE (from_type1);
4798 from_type2 = ics2;
4799 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4800 from_type2 = TREE_OPERAND (from_type2, 0);
4801 from_type2 = TREE_TYPE (from_type2);
4804 if (same_type_p (from_type1, from_type2))
4806 if (is_subseq (ics1, ics2))
4807 return 1;
4808 if (is_subseq (ics2, ics1))
4809 return -1;
4811 /* Otherwise, one sequence cannot be a subsequence of the other; they
4812 don't start with the same type. This can happen when comparing the
4813 second standard conversion sequence in two user-defined conversion
4814 sequences. */
4816 /* [over.ics.rank]
4818 Or, if not that,
4820 --the rank of S1 is better than the rank of S2 (by the rules
4821 defined below):
4823 Standard conversion sequences are ordered by their ranks: an Exact
4824 Match is a better conversion than a Promotion, which is a better
4825 conversion than a Conversion.
4827 Two conversion sequences with the same rank are indistinguishable
4828 unless one of the following rules applies:
4830 --A conversion that is not a conversion of a pointer, or pointer
4831 to member, to bool is better than another conversion that is such
4832 a conversion.
4834 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4835 so that we do not have to check it explicitly. */
4836 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4837 return 1;
4838 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4839 return -1;
4841 to_type1 = TREE_TYPE (ics1);
4842 to_type2 = TREE_TYPE (ics2);
4844 if (TYPE_PTR_P (from_type1)
4845 && TYPE_PTR_P (from_type2)
4846 && TYPE_PTR_P (to_type1)
4847 && TYPE_PTR_P (to_type2))
4849 deref_from_type1 = TREE_TYPE (from_type1);
4850 deref_from_type2 = TREE_TYPE (from_type2);
4851 deref_to_type1 = TREE_TYPE (to_type1);
4852 deref_to_type2 = TREE_TYPE (to_type2);
4854 /* The rules for pointers to members A::* are just like the rules
4855 for pointers A*, except opposite: if B is derived from A then
4856 A::* converts to B::*, not vice versa. For that reason, we
4857 switch the from_ and to_ variables here. */
4858 else if (TYPE_PTRMEM_P (from_type1)
4859 && TYPE_PTRMEM_P (from_type2)
4860 && TYPE_PTRMEM_P (to_type1)
4861 && TYPE_PTRMEM_P (to_type2))
4863 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4864 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4865 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4866 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4868 else if (TYPE_PTRMEMFUNC_P (from_type1)
4869 && TYPE_PTRMEMFUNC_P (from_type2)
4870 && TYPE_PTRMEMFUNC_P (to_type1)
4871 && TYPE_PTRMEMFUNC_P (to_type2))
4873 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4874 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4875 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4876 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4879 if (deref_from_type1 != NULL_TREE
4880 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4881 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4883 /* This was one of the pointer or pointer-like conversions.
4885 [over.ics.rank]
4887 --If class B is derived directly or indirectly from class A,
4888 conversion of B* to A* is better than conversion of B* to
4889 void*, and conversion of A* to void* is better than
4890 conversion of B* to void*. */
4891 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4892 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4894 if (is_properly_derived_from (deref_from_type1,
4895 deref_from_type2))
4896 return -1;
4897 else if (is_properly_derived_from (deref_from_type2,
4898 deref_from_type1))
4899 return 1;
4901 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4902 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4904 if (same_type_p (deref_from_type1, deref_from_type2))
4906 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4908 if (is_properly_derived_from (deref_from_type1,
4909 deref_to_type1))
4910 return 1;
4912 /* We know that DEREF_TO_TYPE1 is `void' here. */
4913 else if (is_properly_derived_from (deref_from_type1,
4914 deref_to_type2))
4915 return -1;
4918 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4919 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4921 /* [over.ics.rank]
4923 --If class B is derived directly or indirectly from class A
4924 and class C is derived directly or indirectly from B,
4926 --conversion of C* to B* is better than conversion of C* to
4927 A*,
4929 --conversion of B* to A* is better than conversion of C* to
4930 A* */
4931 if (same_type_p (deref_from_type1, deref_from_type2))
4933 if (is_properly_derived_from (deref_to_type1,
4934 deref_to_type2))
4935 return 1;
4936 else if (is_properly_derived_from (deref_to_type2,
4937 deref_to_type1))
4938 return -1;
4940 else if (same_type_p (deref_to_type1, deref_to_type2))
4942 if (is_properly_derived_from (deref_from_type2,
4943 deref_from_type1))
4944 return 1;
4945 else if (is_properly_derived_from (deref_from_type1,
4946 deref_from_type2))
4947 return -1;
4951 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4952 && same_type_p (from_type1, from_type2))
4954 /* [over.ics.rank]
4956 --binding of an expression of type C to a reference of type
4957 B& is better than binding an expression of type C to a
4958 reference of type A&
4960 --conversion of C to B is better than conversion of C to A, */
4961 if (is_properly_derived_from (from_type1, to_type1)
4962 && is_properly_derived_from (from_type1, to_type2))
4964 if (is_properly_derived_from (to_type1, to_type2))
4965 return 1;
4966 else if (is_properly_derived_from (to_type2, to_type1))
4967 return -1;
4970 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4971 && same_type_p (to_type1, to_type2))
4973 /* [over.ics.rank]
4975 --binding of an expression of type B to a reference of type
4976 A& is better than binding an expression of type C to a
4977 reference of type A&,
4979 --onversion of B to A is better than conversion of C to A */
4980 if (is_properly_derived_from (from_type1, to_type1)
4981 && is_properly_derived_from (from_type2, to_type1))
4983 if (is_properly_derived_from (from_type2, from_type1))
4984 return 1;
4985 else if (is_properly_derived_from (from_type1, from_type2))
4986 return -1;
4990 /* [over.ics.rank]
4992 --S1 and S2 differ only in their qualification conversion and yield
4993 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4994 qualification signature of type T1 is a proper subset of the cv-
4995 qualification signature of type T2 */
4996 if (TREE_CODE (ics1) == QUAL_CONV
4997 && TREE_CODE (ics2) == QUAL_CONV
4998 && same_type_p (from_type1, from_type2))
4999 return comp_cv_qual_signature (to_type1, to_type2);
5001 /* [over.ics.rank]
5003 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5004 types to which the references refer are the same type except for
5005 top-level cv-qualifiers, and the type to which the reference
5006 initialized by S2 refers is more cv-qualified than the type to
5007 which the reference initialized by S1 refers */
5009 if (ref_binding1 && ref_binding2
5010 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5011 return comp_cv_qualification (target_type2, target_type1);
5013 /* Neither conversion sequence is better than the other. */
5014 return 0;
5017 /* The source type for this standard conversion sequence. */
5019 static tree
5020 source_type (t)
5021 tree t;
5023 for (;; t = TREE_OPERAND (t, 0))
5025 if (TREE_CODE (t) == USER_CONV
5026 || TREE_CODE (t) == AMBIG_CONV
5027 || TREE_CODE (t) == IDENTITY_CONV)
5028 return TREE_TYPE (t);
5030 my_friendly_abort (1823);
5033 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5034 a pointer to LOSER and re-running joust to produce the warning if WINNER
5035 is actually used. */
5037 static void
5038 add_warning (winner, loser)
5039 struct z_candidate *winner, *loser;
5041 winner->warnings = tree_cons (NULL_PTR,
5042 build_expr_ptr_wrapper (loser),
5043 winner->warnings);
5046 /* Returns true iff functions are equivalent. Equivalent functions are
5047 not '==' only if one is a function-local extern function or if
5048 both are extern "C". */
5050 static inline int
5051 equal_functions (fn1, fn2)
5052 tree fn1;
5053 tree fn2;
5055 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5056 || DECL_EXTERN_C_FUNCTION_P (fn1))
5057 return decls_match (fn1, fn2);
5058 return fn1 == fn2;
5061 /* Compare two candidates for overloading as described in
5062 [over.match.best]. Return values:
5064 1: cand1 is better than cand2
5065 -1: cand2 is better than cand1
5066 0: cand1 and cand2 are indistinguishable */
5068 static int
5069 joust (cand1, cand2, warn)
5070 struct z_candidate *cand1, *cand2;
5071 int warn;
5073 int winner = 0;
5074 int i, off1 = 0, off2 = 0, len;
5076 /* Candidates that involve bad conversions are always worse than those
5077 that don't. */
5078 if (cand1->viable > cand2->viable)
5079 return 1;
5080 if (cand1->viable < cand2->viable)
5081 return -1;
5083 /* If we have two pseudo-candidates for conversions to the same type,
5084 or two candidates for the same function, arbitrarily pick one. */
5085 if (cand1->fn == cand2->fn
5086 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5087 return 1;
5089 /* a viable function F1
5090 is defined to be a better function than another viable function F2 if
5091 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5092 ICSi(F2), and then */
5094 /* for some argument j, ICSj(F1) is a better conversion sequence than
5095 ICSj(F2) */
5097 /* For comparing static and non-static member functions, we ignore
5098 the implicit object parameter of the non-static function. The
5099 standard says to pretend that the static function has an object
5100 parm, but that won't work with operator overloading. */
5101 len = TREE_VEC_LENGTH (cand1->convs);
5102 if (len != TREE_VEC_LENGTH (cand2->convs))
5104 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5105 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5106 off2 = 1;
5107 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5108 && DECL_STATIC_FUNCTION_P (cand2->fn))
5110 off1 = 1;
5111 --len;
5113 else
5114 my_friendly_abort (42);
5117 for (i = 0; i < len; ++i)
5119 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5120 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5121 int comp = compare_ics (t1, t2);
5123 if (comp != 0)
5125 if (warn_sign_promo
5126 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5127 && TREE_CODE (t1) == STD_CONV
5128 && TREE_CODE (t2) == STD_CONV
5129 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5130 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5131 && (TYPE_PRECISION (TREE_TYPE (t1))
5132 == TYPE_PRECISION (TREE_TYPE (t2)))
5133 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5134 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5135 == ENUMERAL_TYPE)))
5137 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5138 tree type1, type2;
5139 struct z_candidate *w, *l;
5140 if (comp > 0)
5141 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5142 w = cand1, l = cand2;
5143 else
5144 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5145 w = cand2, l = cand1;
5147 if (warn)
5149 cp_warning ("passing `%T' chooses `%T' over `%T'",
5150 type, type1, type2);
5151 cp_warning (" in call to `%D'", w->fn);
5153 else
5154 add_warning (w, l);
5157 if (winner && comp != winner)
5159 winner = 0;
5160 goto tweak;
5162 winner = comp;
5166 /* warn about confusing overload resolution for user-defined conversions,
5167 either between a constructor and a conversion op, or between two
5168 conversion ops. */
5169 if (winner && cand1->second_conv
5170 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5171 != DECL_CONSTRUCTOR_P (cand2->fn))
5172 /* Don't warn if the two conv ops convert to the same type... */
5173 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5174 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5175 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5177 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5178 if (comp != winner)
5180 struct z_candidate *w, *l;
5181 tree convn;
5182 if (winner == 1)
5183 w = cand1, l = cand2;
5184 else
5185 w = cand2, l = cand1;
5186 if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5187 && ! DECL_CONSTRUCTOR_P (cand1->fn)
5188 && ! DECL_CONSTRUCTOR_P (cand2->fn)
5189 && (convn = standard_conversion
5190 (TREE_TYPE (TREE_TYPE (l->fn)),
5191 TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5192 && TREE_CODE (convn) == QUAL_CONV)
5193 /* Don't complain about `operator char *()' beating
5194 `operator const char *() const'. */;
5195 else if (warn)
5197 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5198 if (! DECL_CONSTRUCTOR_P (w->fn))
5199 source = TREE_TYPE (source);
5200 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
5201 cp_warning (" for conversion from `%T' to `%T'",
5202 source, TREE_TYPE (w->second_conv));
5203 cp_warning (" because conversion sequence for the argument is better");
5205 else
5206 add_warning (w, l);
5210 if (winner)
5211 return winner;
5213 /* or, if not that,
5214 F1 is a non-template function and F2 is a template function
5215 specialization. */
5217 if (! cand1->template && cand2->template)
5218 return 1;
5219 else if (cand1->template && ! cand2->template)
5220 return -1;
5222 /* or, if not that,
5223 F1 and F2 are template functions and the function template for F1 is
5224 more specialized than the template for F2 according to the partial
5225 ordering rules. */
5227 if (cand1->template && cand2->template)
5229 winner = more_specialized
5230 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5231 DEDUCE_ORDER,
5232 /* Never do unification on the 'this' parameter. */
5233 TREE_VEC_LENGTH (cand1->convs)
5234 - DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn));
5235 if (winner)
5236 return winner;
5239 /* a non-template user function is better than a builtin. (Pedantically
5240 the builtin which matched the user function should not be added to
5241 the overload set, but we spot it here.
5243 [over.match.oper]
5244 ... the builtin candidates include ...
5245 - do not have the same parameter type list as any non-template
5246 non-member candidate. */
5248 if (TREE_CODE (cand1->fn) != IDENTIFIER_NODE
5249 && TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5250 return 1;
5251 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5252 && TREE_CODE (cand2->fn) != IDENTIFIER_NODE)
5253 return -1;
5255 /* or, if not that,
5256 the context is an initialization by user-defined conversion (see
5257 _dcl.init_ and _over.match.user_) and the standard conversion
5258 sequence from the return type of F1 to the destination type (i.e.,
5259 the type of the entity being initialized) is a better conversion
5260 sequence than the standard conversion sequence from the return type
5261 of F2 to the destination type. */
5263 if (cand1->second_conv)
5265 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5266 if (winner)
5267 return winner;
5270 /* If the built-in candidates are the same, arbitrarily pick one. */
5271 if (cand1->fn == cand2->fn
5272 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5274 for (i = 0; i < len; ++i)
5275 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5276 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5277 break;
5278 if (i == TREE_VEC_LENGTH (cand1->convs))
5279 return 1;
5281 /* Kludge around broken overloading rules whereby
5282 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
5283 that takes references and another that takes values. */
5284 if (cand1->fn == ansi_opname (COND_EXPR))
5286 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
5287 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
5288 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
5289 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
5291 if (same_type_p (t1, t2))
5293 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
5294 return 1;
5295 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
5296 return -1;
5301 /* If the two functions are the same (this can happen with declarations
5302 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5303 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5304 && equal_functions (cand1->fn, cand2->fn))
5305 return 1;
5307 tweak:
5309 /* Extension: If the worst conversion for one candidate is worse than the
5310 worst conversion for the other, take the first. */
5311 if (!pedantic)
5313 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5315 for (i = 0; i < len; ++i)
5317 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5318 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5319 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5320 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5323 if (rank1 < rank2)
5324 return 1;
5325 if (rank1 > rank2)
5326 return -1;
5329 my_friendly_assert (!winner, 20010121);
5330 return 0;
5333 /* Given a list of candidates for overloading, find the best one, if any.
5334 This algorithm has a worst case of O(2n) (winner is last), and a best
5335 case of O(n/2) (totally ambiguous); much better than a sorting
5336 algorithm. */
5338 static struct z_candidate *
5339 tourney (candidates)
5340 struct z_candidate *candidates;
5342 struct z_candidate *champ = candidates, *challenger;
5343 int fate;
5344 int champ_compared_to_predecessor = 0;
5346 /* Walk through the list once, comparing each current champ to the next
5347 candidate, knocking out a candidate or two with each comparison. */
5349 for (challenger = champ->next; challenger; )
5351 fate = joust (champ, challenger, 0);
5352 if (fate == 1)
5353 challenger = challenger->next;
5354 else
5356 if (fate == 0)
5358 champ = challenger->next;
5359 if (champ == 0)
5360 return 0;
5361 champ_compared_to_predecessor = 0;
5363 else
5365 champ = challenger;
5366 champ_compared_to_predecessor = 1;
5369 challenger = champ->next;
5373 /* Make sure the champ is better than all the candidates it hasn't yet
5374 been compared to. */
5376 for (challenger = candidates;
5377 challenger != champ
5378 && !(champ_compared_to_predecessor && challenger->next == champ);
5379 challenger = challenger->next)
5381 fate = joust (champ, challenger, 0);
5382 if (fate != 1)
5383 return 0;
5386 return champ;
5389 /* Returns non-zero if things of type FROM can be converted to TO. */
5392 can_convert (to, from)
5393 tree to, from;
5395 return can_convert_arg (to, from, NULL_TREE);
5398 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5401 can_convert_arg (to, from, arg)
5402 tree to, from, arg;
5404 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5405 return (t && ! ICS_BAD_FLAG (t));
5408 /* Convert EXPR to TYPE. Return the converted expression. */
5410 tree
5411 perform_implicit_conversion (type, expr)
5412 tree type;
5413 tree expr;
5415 tree conv;
5417 if (expr == error_mark_node)
5418 return error_mark_node;
5419 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5420 LOOKUP_NORMAL);
5421 if (!conv || ICS_BAD_FLAG (conv))
5423 cp_error ("could not convert `%E' to `%T'", expr, type);
5424 return error_mark_node;
5427 return convert_like (conv, expr);
5430 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5431 initializing a variable of that TYPE. Return the converted
5432 expression. */
5434 tree
5435 initialize_reference (type, expr)
5436 tree type;
5437 tree expr;
5439 tree conv;
5441 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5442 if (!conv || ICS_BAD_FLAG (conv))
5444 cp_error ("could not convert `%E' to `%T'", expr, type);
5445 return error_mark_node;
5448 return convert_like (conv, expr);