* cp-tree.h (cp_function): Move here, from decl.c.
[official-gcc.git] / gcc / cp / call.c
blobdced83b335427ee51b608c0dfac862239447208c
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 92-97, 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "output.h"
31 #include "flags.h"
32 #include "rtl.h"
33 #include "toplev.h"
34 #include "defaults.h"
36 #include "obstack.h"
37 #define obstack_chunk_alloc xmalloc
38 #define obstack_chunk_free free
40 extern int inhibit_warnings;
42 static tree build_new_method_call PROTO((tree, tree, tree, tree, int));
44 static tree build_field_call PROTO((tree, tree, tree, tree));
45 static struct z_candidate * tourney PROTO((struct z_candidate *));
46 static int equal_functions PROTO((tree, tree));
47 static int joust PROTO((struct z_candidate *, struct z_candidate *, int));
48 static int compare_ics PROTO((tree, tree));
49 static tree build_over_call PROTO((struct z_candidate *, tree, int));
50 static tree convert_like PROTO((tree, tree));
51 static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
52 tree, const char *));
53 static tree build_object_call PROTO((tree, tree));
54 static tree resolve_args PROTO((tree));
55 static struct z_candidate * build_user_type_conversion_1
56 PROTO ((tree, tree, int));
57 static void print_z_candidates PROTO((struct z_candidate *));
58 static tree build_this PROTO((tree));
59 static struct z_candidate * splice_viable PROTO((struct z_candidate *));
60 static int any_viable PROTO((struct z_candidate *));
61 static struct z_candidate * add_template_candidate
62 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
63 unification_kind_t));
64 static struct z_candidate * add_template_candidate_real
65 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
66 tree, unification_kind_t));
67 static struct z_candidate * add_template_conv_candidate
68 PROTO((struct z_candidate *, tree, tree, tree, tree));
69 static struct z_candidate * add_builtin_candidates
70 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
71 tree, tree *, int));
72 static struct z_candidate * add_builtin_candidate
73 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
74 tree, tree, tree, tree *, tree *, int));
75 static int is_complete PROTO((tree));
76 static struct z_candidate * build_builtin_candidate
77 PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
78 int));
79 static struct z_candidate * add_conv_candidate
80 PROTO((struct z_candidate *, tree, tree, tree));
81 static struct z_candidate * add_function_candidate
82 PROTO((struct z_candidate *, tree, tree, int));
83 static tree implicit_conversion PROTO((tree, tree, tree, int));
84 static tree standard_conversion PROTO((tree, tree, tree));
85 static tree reference_binding PROTO((tree, tree, tree, int));
86 static tree non_reference PROTO((tree));
87 static tree build_conv PROTO((enum tree_code, tree, tree));
88 static int is_subseq PROTO((tree, tree));
89 static int maybe_handle_ref_bind PROTO((tree*, tree*));
90 static void maybe_handle_implicit_object PROTO((tree*));
91 static struct z_candidate * add_candidate PROTO((struct z_candidate *,
92 tree, tree, int));
93 static tree source_type PROTO((tree));
94 static void add_warning PROTO((struct z_candidate *, struct z_candidate *));
95 static int reference_related_p PROTO ((tree, tree));
96 static int reference_compatible_p PROTO ((tree, tree));
97 static tree convert_class_to_reference PROTO ((tree, tree, tree));
98 static tree direct_reference_binding PROTO ((tree, tree));
99 static int promoted_arithmetic_type_p PROTO ((tree));
100 static tree conditional_conversion PROTO ((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_USES_COMPLEX_INHERITANCE (type))
115 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
116 datum, CLASSTYPE_VFIELD (type));
117 else
118 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_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 (name == ctor_identifier || name == dtor_identifier)
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)) == POINTER_TYPE)
160 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
161 return build_function_call (instance, parms);
162 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
163 == METHOD_TYPE)
164 return build_function_call
165 (instance, expr_tree_cons (NULL_TREE, instance_ptr, parms));
169 return NULL_TREE;
172 /* Returns nonzero iff the destructor name specified in NAME
173 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
174 forms... */
177 check_dtor_name (basetype, name)
178 tree basetype, name;
180 name = TREE_OPERAND (name, 0);
182 /* Just accept something we've already complained about. */
183 if (name == error_mark_node)
184 return 1;
186 if (TREE_CODE (name) == TYPE_DECL)
187 name = TREE_TYPE (name);
188 else if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
189 /* OK */;
190 else if (TREE_CODE (name) == IDENTIFIER_NODE)
192 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
193 || (TREE_CODE (basetype) == ENUMERAL_TYPE
194 && name == TYPE_IDENTIFIER (basetype)))
195 name = basetype;
196 else
197 name = get_type_value (name);
199 else
200 my_friendly_abort (980605);
202 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
203 return 1;
204 return 0;
207 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
208 This is how virtual function calls are avoided. */
210 tree
211 build_scoped_method_call (exp, basetype, name, parms)
212 tree exp, basetype, name, parms;
214 /* Because this syntactic form does not allow
215 a pointer to a base class to be `stolen',
216 we need not protect the derived->base conversion
217 that happens here.
219 @@ But we do have to check access privileges later. */
220 tree binfo, decl;
221 tree type = TREE_TYPE (exp);
223 if (type == error_mark_node
224 || basetype == error_mark_node)
225 return error_mark_node;
227 if (processing_template_decl)
229 if (TREE_CODE (name) == BIT_NOT_EXPR
230 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
232 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
233 if (type)
234 name = build_min_nt (BIT_NOT_EXPR, type);
236 name = build_min_nt (SCOPE_REF, basetype, name);
237 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
240 if (TREE_CODE (type) == REFERENCE_TYPE)
241 type = TREE_TYPE (type);
243 if (TREE_CODE (basetype) == TREE_VEC)
245 binfo = basetype;
246 basetype = BINFO_TYPE (binfo);
248 else
249 binfo = NULL_TREE;
251 /* Check the destructor call syntax. */
252 if (TREE_CODE (name) == BIT_NOT_EXPR)
254 /* We can get here if someone writes their destructor call like
255 `obj.NS::~T()'; this isn't really a scoped method call, so hand
256 it off. */
257 if (TREE_CODE (basetype) == NAMESPACE_DECL)
258 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
260 if (! check_dtor_name (basetype, name))
261 cp_error ("qualified type `%T' does not match destructor name `~%T'",
262 basetype, TREE_OPERAND (name, 0));
264 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
265 that explicit ~int is caught in the parser; this deals with typedefs
266 and template parms. */
267 if (! IS_AGGR_TYPE (basetype))
269 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
270 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
271 exp, basetype, type);
273 return cp_convert (void_type_node, exp);
277 if (! is_aggr_type (basetype, 1))
278 return error_mark_node;
280 if (! IS_AGGR_TYPE (type))
282 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
283 exp, type);
284 return error_mark_node;
287 if (! binfo)
289 binfo = get_binfo (basetype, type, 1);
290 if (binfo == error_mark_node)
291 return error_mark_node;
292 if (! binfo)
293 error_not_base_type (basetype, type);
296 if (binfo)
298 if (TREE_CODE (exp) == INDIRECT_REF)
299 decl = build_indirect_ref
300 (convert_pointer_to_real
301 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
302 else
303 decl = build_scoped_ref (exp, basetype);
305 /* Call to a destructor. */
306 if (TREE_CODE (name) == BIT_NOT_EXPR)
308 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
309 return cp_convert (void_type_node, exp);
311 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
312 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
316 /* Call to a method. */
317 return build_method_call (decl, name, parms, binfo,
318 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
320 return error_mark_node;
323 /* We want the address of a function or method. We avoid creating a
324 pointer-to-member function. */
326 tree
327 build_addr_func (function)
328 tree function;
330 tree type = TREE_TYPE (function);
332 /* We have to do these by hand to avoid real pointer to member
333 functions. */
334 if (TREE_CODE (type) == METHOD_TYPE)
336 tree addr;
338 type = build_pointer_type (type);
340 if (mark_addressable (function) == 0)
341 return error_mark_node;
343 addr = build1 (ADDR_EXPR, type, function);
345 /* Address of a static or external variable or function counts
346 as a constant */
347 if (staticp (function))
348 TREE_CONSTANT (addr) = 1;
350 function = addr;
352 else
353 function = default_conversion (function);
355 return function;
358 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
359 POINTER_TYPE to those. Note, pointer to member function types
360 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
362 tree
363 build_call (function, result_type, parms)
364 tree function, result_type, parms;
366 int is_constructor = 0;
367 tree tmp;
368 tree decl;
370 function = build_addr_func (function);
372 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
374 sorry ("unable to call pointer to member function here");
375 return error_mark_node;
378 if (TREE_CODE (function) == ADDR_EXPR
379 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
380 decl = TREE_OPERAND (function, 0);
381 else
382 decl = NULL_TREE;
384 if (decl && DECL_CONSTRUCTOR_P (decl))
385 is_constructor = 1;
387 if (decl)
388 my_friendly_assert (TREE_USED (decl), 990125);
390 /* Don't pass empty class objects by value. This is useful
391 for tags in STL, which are used to control overload resolution.
392 We don't need to handle other cases of copying empty classes. */
393 if (! decl || ! DECL_BUILT_IN (decl))
394 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
395 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
396 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
398 tree t = make_node (RTL_EXPR);
399 TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
400 RTL_EXPR_RTL (t) = const0_rtx;
401 RTL_EXPR_SEQUENCE (t) = NULL_RTX;
402 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
403 TREE_VALUE (tmp), t);
406 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
407 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
408 TREE_TYPE (function) = result_type;
409 TREE_SIDE_EFFECTS (function) = 1;
411 return function;
414 /* Build something of the form ptr->method (args)
415 or object.method (args). This can also build
416 calls to constructors, and find friends.
418 Member functions always take their class variable
419 as a pointer.
421 INSTANCE is a class instance.
423 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
425 PARMS help to figure out what that NAME really refers to.
427 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
428 down to the real instance type to use for access checking. We need this
429 information to get protected accesses correct. This parameter is used
430 by build_member_call.
432 FLAGS is the logical disjunction of zero or more LOOKUP_
433 flags. See cp-tree.h for more info.
435 If this is all OK, calls build_function_call with the resolved
436 member function.
438 This function must also handle being called to perform
439 initialization, promotion/coercion of arguments, and
440 instantiation of default parameters.
442 Note that NAME may refer to an instance variable name. If
443 `operator()()' is defined for the type of that field, then we return
444 that result. */
446 tree
447 build_method_call (instance, name, parms, basetype_path, flags)
448 tree instance, name, parms, basetype_path;
449 int flags;
451 tree basetype, instance_ptr;
453 #ifdef GATHER_STATISTICS
454 n_build_method_call++;
455 #endif
457 if (instance == error_mark_node
458 || name == error_mark_node
459 || parms == error_mark_node
460 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
461 return error_mark_node;
463 if (processing_template_decl)
465 /* We need to process template parm names here so that tsubst catches
466 them properly. Other type names can wait. */
467 if (TREE_CODE (name) == BIT_NOT_EXPR)
469 tree type = NULL_TREE;
471 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
472 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
473 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
474 type = TREE_TYPE (TREE_OPERAND (name, 0));
476 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
477 name = build_min_nt (BIT_NOT_EXPR, type);
480 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
483 if (TREE_CODE (name) == BIT_NOT_EXPR)
485 if (parms)
486 error ("destructors take no parameters");
487 basetype = TREE_TYPE (instance);
488 if (TREE_CODE (basetype) == REFERENCE_TYPE)
489 basetype = TREE_TYPE (basetype);
491 if (! check_dtor_name (basetype, name))
492 cp_error
493 ("destructor name `~%T' does not match type `%T' of expression",
494 TREE_OPERAND (name, 0), basetype);
496 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
497 return cp_convert (void_type_node, instance);
498 instance = default_conversion (instance);
499 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
500 return build_delete (build_pointer_type (basetype),
501 instance_ptr, integer_two_node,
502 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
505 return build_new_method_call (instance, name, parms, basetype_path, flags);
508 /* New overloading code. */
510 struct z_candidate {
511 tree fn;
512 tree convs;
513 tree second_conv;
514 int viable;
515 tree basetype_path;
516 tree template;
517 tree warnings;
518 struct z_candidate *next;
521 #define IDENTITY_RANK 0
522 #define EXACT_RANK 1
523 #define PROMO_RANK 2
524 #define STD_RANK 3
525 #define PBOOL_RANK 4
526 #define USER_RANK 5
527 #define ELLIPSIS_RANK 6
528 #define BAD_RANK 7
530 #define ICS_RANK(NODE) \
531 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
532 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
533 : ICS_USER_FLAG (NODE) ? USER_RANK \
534 : ICS_STD_RANK (NODE))
536 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
538 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
539 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
540 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
541 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
543 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
544 should be created to hold the result of the conversion. */
545 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
547 #define USER_CONV_CAND(NODE) \
548 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
549 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
552 null_ptr_cst_p (t)
553 tree t;
555 /* [conv.ptr]
557 A null pointer constant is an integral constant expression
558 (_expr.const_) rvalue of integer type that evaluates to zero. */
559 if (t == null_node
560 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
561 return 1;
562 return 0;
565 static tree
566 build_conv (code, type, from)
567 enum tree_code code;
568 tree type, from;
570 tree t = build1 (code, type, from);
571 int rank = ICS_STD_RANK (from);
572 switch (code)
574 case PTR_CONV:
575 case PMEM_CONV:
576 case BASE_CONV:
577 case STD_CONV:
578 if (rank < STD_RANK)
579 rank = STD_RANK;
580 break;
582 case QUAL_CONV:
583 if (rank < EXACT_RANK)
584 rank = EXACT_RANK;
586 default:
587 break;
589 ICS_STD_RANK (t) = rank;
590 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
591 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
592 return t;
595 static tree
596 non_reference (t)
597 tree t;
599 if (TREE_CODE (t) == REFERENCE_TYPE)
600 t = TREE_TYPE (t);
601 return t;
604 tree
605 strip_top_quals (t)
606 tree t;
608 if (TREE_CODE (t) == ARRAY_TYPE)
609 return t;
610 return TYPE_MAIN_VARIANT (t);
613 /* Returns the standard conversion path (see [conv]) from type FROM to type
614 TO, if any. For proper handling of null pointer constants, you must
615 also pass the expression EXPR to convert from. */
617 static tree
618 standard_conversion (to, from, expr)
619 tree to, from, expr;
621 enum tree_code fcode, tcode;
622 tree conv;
623 int fromref = 0;
625 if (TREE_CODE (to) == REFERENCE_TYPE)
626 to = TREE_TYPE (to);
627 if (TREE_CODE (from) == REFERENCE_TYPE)
629 fromref = 1;
630 from = TREE_TYPE (from);
632 to = strip_top_quals (to);
633 from = strip_top_quals (from);
635 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
636 && expr && type_unknown_p (expr))
638 expr = instantiate_type (to, expr, 0);
639 if (expr == error_mark_node)
640 return NULL_TREE;
641 from = TREE_TYPE (expr);
644 fcode = TREE_CODE (from);
645 tcode = TREE_CODE (to);
647 conv = build1 (IDENTITY_CONV, from, expr);
649 if (fcode == FUNCTION_TYPE)
651 from = build_pointer_type (from);
652 fcode = TREE_CODE (from);
653 conv = build_conv (LVALUE_CONV, from, conv);
655 else if (fcode == ARRAY_TYPE)
657 from = build_pointer_type (TREE_TYPE (from));
658 fcode = TREE_CODE (from);
659 conv = build_conv (LVALUE_CONV, from, conv);
661 else if (fromref || (expr && real_lvalue_p (expr)))
662 conv = build_conv (RVALUE_CONV, from, conv);
664 if (same_type_p (from, to))
665 return conv;
667 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
668 && expr && null_ptr_cst_p (expr))
670 conv = build_conv (STD_CONV, to, conv);
672 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
674 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
675 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
677 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
678 TYPE_MAIN_VARIANT (TREE_TYPE (to))))
680 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
681 && ufcode != FUNCTION_TYPE)
683 from = build_pointer_type
684 (cp_build_qualified_type (void_type_node,
685 CP_TYPE_QUALS (TREE_TYPE (from))));
686 conv = build_conv (PTR_CONV, from, conv);
688 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
690 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
691 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
693 if (DERIVED_FROM_P (fbase, tbase)
694 && (same_type_p
695 (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
696 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))))))
698 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
699 from = build_pointer_type (from);
700 conv = build_conv (PMEM_CONV, from, conv);
703 else if (IS_AGGR_TYPE (TREE_TYPE (from))
704 && IS_AGGR_TYPE (TREE_TYPE (to)))
706 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
708 from =
709 cp_build_qualified_type (TREE_TYPE (to),
710 CP_TYPE_QUALS (TREE_TYPE (from)));
711 from = build_pointer_type (from);
712 conv = build_conv (PTR_CONV, from, conv);
716 if (same_type_p (from, to))
717 /* OK */;
718 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
719 conv = build_conv (QUAL_CONV, to, conv);
720 else if (expr && string_conv_p (to, expr, 0))
721 /* converting from string constant to char *. */
722 conv = build_conv (QUAL_CONV, to, conv);
723 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
725 conv = build_conv (PTR_CONV, to, conv);
726 ICS_BAD_FLAG (conv) = 1;
728 else
729 return 0;
731 from = to;
733 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
735 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
736 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
737 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
738 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
740 if (! DERIVED_FROM_P (fbase, tbase)
741 || ! same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
742 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
743 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
744 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
745 return 0;
747 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
748 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
749 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
750 from = build_ptrmemfunc_type (build_pointer_type (from));
751 conv = build_conv (PMEM_CONV, from, conv);
753 else if (tcode == BOOLEAN_TYPE)
755 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
756 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
757 return 0;
759 conv = build_conv (STD_CONV, to, conv);
760 if (fcode == POINTER_TYPE
761 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
762 ICS_STD_RANK (conv) = PBOOL_RANK;
764 /* We don't check for ENUMERAL_TYPE here because there are no standard
765 conversions to enum type. */
766 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
767 || tcode == REAL_TYPE)
769 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
770 return 0;
771 conv = build_conv (STD_CONV, to, conv);
773 /* Give this a better rank if it's a promotion. */
774 if (to == type_promotes_to (from)
775 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
776 ICS_STD_RANK (conv) = PROMO_RANK;
778 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
779 && is_properly_derived_from (from, to))
781 if (TREE_CODE (conv) == RVALUE_CONV)
782 conv = TREE_OPERAND (conv, 0);
783 conv = build_conv (BASE_CONV, to, conv);
784 /* The derived-to-base conversion indicates the initialization
785 of a parameter with base type from an object of a derived
786 type. A temporary object is created to hold the result of
787 the conversion. */
788 NEED_TEMPORARY_P (conv) = 1;
790 else
791 return 0;
793 return conv;
796 /* Returns non-zero if T1 is reference-related to T2. */
798 static int
799 reference_related_p (t1, t2)
800 tree t1;
801 tree t2;
803 t1 = TYPE_MAIN_VARIANT (t1);
804 t2 = TYPE_MAIN_VARIANT (t2);
806 /* [dcl.init.ref]
808 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
809 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
810 of T2. */
811 return (same_type_p (t1, t2)
812 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
813 && DERIVED_FROM_P (t1, t2)));
816 /* Returns non-zero if T1 is reference-compatible with T2. */
818 static int
819 reference_compatible_p (t1, t2)
820 tree t1;
821 tree t2;
823 /* [dcl.init.ref]
825 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
826 reference-related to T2 and cv1 is the same cv-qualification as,
827 or greater cv-qualification than, cv2. */
828 return (reference_related_p (t1, t2)
829 && at_least_as_qualified_p (t1, t2));
832 /* Determine whether or not the EXPR (of class type S) can be
833 converted to T as in [over.match.ref]. */
835 static tree
836 convert_class_to_reference (t, s, expr)
837 tree t;
838 tree s;
839 tree expr;
841 tree conversions;
842 tree arglist;
843 tree conv;
844 struct z_candidate *candidates;
845 struct z_candidate *cand;
847 /* [over.match.ref]
849 Assuming that "cv1 T" is the underlying type of the reference
850 being initialized, and "cv S" is the type of the initializer
851 expression, with S a class type, the candidate functions are
852 selected as follows:
854 --The conversion functions of S and its base classes are
855 considered. Those that are not hidden within S and yield type
856 "reference to cv2 T2", where "cv1 T" is reference-compatible
857 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
859 The argument list has one argument, which is the initializer
860 expression. */
862 candidates = 0;
864 /* Conceptually, we should take the address of EXPR and put it in
865 the argument list. Unfortunately, however, that can result in
866 error messages, which we should not issue now because we are just
867 trying to find a conversion operator. Therefore, we use NULL,
868 cast to the appropriate type. */
869 arglist = build_int_2 (0, 0);
870 TREE_TYPE (arglist) = build_pointer_type (s);
871 arglist = build_scratch_list (NULL_TREE, arglist);
873 for (conversions = lookup_conversions (s);
874 conversions;
875 conversions = TREE_CHAIN (conversions))
877 tree fns = TREE_VALUE (conversions);
879 while (fns)
881 tree f = OVL_CURRENT (fns);
882 tree t2 = TREE_TYPE (TREE_TYPE (f));
883 struct z_candidate *old_candidates = candidates;
885 /* If this is a template function, try to get an exact
886 match. */
887 if (TREE_CODE (f) == TEMPLATE_DECL)
889 candidates
890 = add_template_candidate (candidates,
892 NULL_TREE,
893 arglist,
894 build_reference_type (t),
895 LOOKUP_NORMAL,
896 DEDUCE_CONV);
898 if (candidates != old_candidates)
900 /* Now, see if the conversion function really returns
901 an lvalue of the appropriate type. From the
902 point of view of unification, simply returning an
903 rvalue of the right type is good enough. */
904 f = candidates->fn;
905 t2 = TREE_TYPE (TREE_TYPE (f));
906 if (TREE_CODE (t2) != REFERENCE_TYPE
907 || !reference_compatible_p (t, TREE_TYPE (t2)))
908 candidates = candidates->next;
911 else if (TREE_CODE (t2) == REFERENCE_TYPE
912 && reference_compatible_p (t, TREE_TYPE (t2)))
913 candidates
914 = add_function_candidate (candidates, f, arglist,
915 LOOKUP_NORMAL);
917 if (candidates != old_candidates)
918 candidates->basetype_path = TREE_PURPOSE (conversions);
920 fns = OVL_NEXT (fns);
924 /* If none of the conversion functions worked out, let our caller
925 know. */
926 if (!any_viable (candidates))
927 return NULL_TREE;
929 candidates = splice_viable (candidates);
930 cand = tourney (candidates);
931 if (!cand)
932 return NULL_TREE;
934 conv = build_conv (IDENTITY_CONV, s, expr);
935 conv = build_conv (USER_CONV,
936 non_reference (TREE_TYPE (TREE_TYPE (cand->fn))),
937 expr);
938 TREE_OPERAND (conv, 1) = build_expr_ptr_wrapper (cand);
939 ICS_USER_FLAG (conv) = 1;
940 if (cand->viable == -1)
941 ICS_BAD_FLAG (conv) = 1;
942 cand->second_conv = conv;
944 return conv;
947 /* A reference of the indicated TYPE is being bound directly to the
948 expression represented by the implicit conversion sequence CONV.
949 Return a conversion sequence for this binding. */
951 static tree
952 direct_reference_binding (type, conv)
953 tree type;
954 tree conv;
956 tree t = TREE_TYPE (type);
958 /* [over.ics.rank]
960 When a parameter of reference type binds directly
961 (_dcl.init.ref_) to an argument expression, the implicit
962 conversion sequence is the identity conversion, unless the
963 argument expression has a type that is a derived class of the
964 parameter type, in which case the implicit conversion sequence is
965 a derived-to-base Conversion.
967 If the parameter binds directly to the result of applying a
968 conversion function to the argument expression, the implicit
969 conversion sequence is a user-defined conversion sequence
970 (_over.ics.user_), with the second standard conversion sequence
971 either an identity conversion or, if the conversion function
972 returns an entity of a type that is a derived class of the
973 parameter type, a derived-to-base conversion. */
974 if (!same_type_p (TYPE_MAIN_VARIANT (t),
975 TYPE_MAIN_VARIANT (TREE_TYPE (conv))))
977 /* Represent the derived-to-base conversion. */
978 conv = build_conv (BASE_CONV, t, conv);
979 /* We will actually be binding to the base-class subobject in
980 the derived class, so we mark this conversion appropriately.
981 That way, convert_like knows not to generate a temporary. */
982 NEED_TEMPORARY_P (conv) = 0;
984 return build_conv (REF_BIND, type, conv);
987 /* Returns the conversion path from type FROM to reference type TO for
988 purposes of reference binding. For lvalue binding, either pass a
989 reference type to FROM or an lvalue expression to EXPR. If the
990 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
991 the conversion returned. */
993 static tree
994 reference_binding (rto, rfrom, expr, flags)
995 tree rto, rfrom, expr;
996 int flags;
998 tree conv = NULL_TREE;
999 tree to = TREE_TYPE (rto);
1000 tree from = rfrom;
1001 int related_p;
1002 int compatible_p;
1003 cp_lvalue_kind lvalue_p = clk_none;
1005 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1007 expr = instantiate_type (to, expr, 0);
1008 if (expr == error_mark_node)
1009 return NULL_TREE;
1010 from = TREE_TYPE (expr);
1013 if (TREE_CODE (from) == REFERENCE_TYPE)
1015 /* Anything with reference type is an lvalue. */
1016 lvalue_p = clk_ordinary;
1017 from = TREE_TYPE (from);
1019 else if (expr)
1020 lvalue_p = real_lvalue_p (expr);
1022 /* Figure out whether or not the types are reference-related and
1023 reference compatible. We have do do this after stripping
1024 references from FROM. */
1025 related_p = reference_related_p (to, from);
1026 compatible_p = reference_compatible_p (to, from);
1028 if (lvalue_p && compatible_p)
1030 /* [dcl.init.ref]
1032 If the intializer expression
1034 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1035 is reference-compatible with "cv2 T2,"
1037 the reference is bound directly to the initializer exprssion
1038 lvalue. */
1039 conv = build1 (IDENTITY_CONV, from, expr);
1040 conv = direct_reference_binding (rto, conv);
1041 if ((lvalue_p & clk_bitfield) != 0
1042 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1043 /* For the purposes of overload resolution, we ignore the fact
1044 this expression is a bitfield. (In particular,
1045 [over.ics.ref] says specifically that a function with a
1046 non-const reference parameter is viable even if the
1047 argument is a bitfield.)
1049 However, when we actually call the function we must create
1050 a temporary to which to bind the reference. If the
1051 reference is volatile, or isn't const, then we cannot make
1052 a temporary, so we just issue an error when the conversion
1053 actually occurs. */
1054 NEED_TEMPORARY_P (conv) = 1;
1055 return conv;
1057 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1059 /* [dcl.init.ref]
1061 If the initializer exprsesion
1063 -- has a class type (i.e., T2 is a class type) can be
1064 implicitly converted to an lvalue of type "cv3 T3," where
1065 "cv1 T1" is reference-compatible with "cv3 T3". (this
1066 conversion is selected by enumerating the applicable
1067 conversion functions (_over.match.ref_) and choosing the
1068 best one through overload resolution. (_over.match_).
1070 the reference is bound to the lvalue result of the conversion
1071 in the second case. */
1072 conv = convert_class_to_reference (to, from, expr);
1073 if (conv)
1074 return direct_reference_binding (rto, conv);
1077 /* From this point on, we conceptually need temporaries, even if we
1078 elide them. Only the cases above are "direct bindings". */
1079 if (flags & LOOKUP_NO_TEMP_BIND)
1080 return NULL_TREE;
1082 /* [over.ics.rank]
1084 When a parameter of reference type is not bound directly to an
1085 argument expression, the conversion sequence is the one required
1086 to convert the argument expression to the underlying type of the
1087 reference according to _over.best.ics_. Conceptually, this
1088 conversion sequence corresponds to copy-initializing a temporary
1089 of the underlying type with the argument expression. Any
1090 difference in top-level cv-qualification is subsumed by the
1091 initialization itself and does not constitute a conversion. */
1093 /* [dcl.init.ref]
1095 Otherwise, the reference shall be to a non-volatile const type. */
1096 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1097 return NULL_TREE;
1099 /* [dcl.init.ref]
1101 If the initializer expression is an rvalue, with T2 a class type,
1102 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1103 is bound in one of the following ways:
1105 -- The reference is bound to the object represented by the rvalue
1106 or to a sub-object within that object.
1108 In this case, the implicit conversion sequence is supposed to be
1109 same as we would obtain by generating a temporary. Fortunately,
1110 if the types are reference compatible, then this is either an
1111 identity conversion or the derived-to-base conversion, just as
1112 for direct binding. */
1113 if (CLASS_TYPE_P (from) && compatible_p)
1115 conv = build1 (IDENTITY_CONV, from, expr);
1116 return direct_reference_binding (rto, conv);
1119 /* [dcl.init.ref]
1121 Otherwise, a temporary of type "cv1 T1" is created and
1122 initialized from the initializer expression using the rules for a
1123 non-reference copy initialization. If T1 is reference-related to
1124 T2, cv1 must be the same cv-qualification as, or greater
1125 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1126 if (related_p && !at_least_as_qualified_p (to, from))
1127 return NULL_TREE;
1129 conv = implicit_conversion (to, from, expr, flags);
1130 if (!conv)
1131 return NULL_TREE;
1133 conv = build_conv (REF_BIND, rto, conv);
1134 /* This reference binding, unlike those above, requires the
1135 creation of a temporary. */
1136 NEED_TEMPORARY_P (conv) = 1;
1138 return conv;
1141 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1142 to type TO. The optional expression EXPR may affect the conversion.
1143 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1144 significant. */
1146 static tree
1147 implicit_conversion (to, from, expr, flags)
1148 tree to, from, expr;
1149 int flags;
1151 tree conv;
1152 struct z_candidate *cand;
1154 complete_type (from);
1155 complete_type (to);
1157 if (TREE_CODE (to) == REFERENCE_TYPE)
1158 conv = reference_binding (to, from, expr, flags);
1159 else
1160 conv = standard_conversion (to, from, expr);
1162 if (conv)
1164 else if (expr != NULL_TREE
1165 && (IS_AGGR_TYPE (non_reference (from))
1166 || IS_AGGR_TYPE (non_reference (to)))
1167 && (flags & LOOKUP_NO_CONVERSION) == 0)
1169 cand = build_user_type_conversion_1
1170 (to, expr, LOOKUP_ONLYCONVERTING);
1171 if (cand)
1172 conv = cand->second_conv;
1173 if ((! conv || ICS_BAD_FLAG (conv))
1174 && TREE_CODE (to) == REFERENCE_TYPE
1175 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1177 cand = build_user_type_conversion_1
1178 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1179 if (cand)
1181 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to)))
1182 ICS_BAD_FLAG (cand->second_conv) = 1;
1183 if (!conv || (ICS_BAD_FLAG (conv)
1184 > ICS_BAD_FLAG (cand->second_conv)))
1185 conv = build_conv (REF_BIND, to, cand->second_conv);
1190 return conv;
1193 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1194 functions. */
1196 static struct z_candidate *
1197 add_candidate (candidates, fn, convs, viable)
1198 struct z_candidate *candidates;
1199 tree fn, convs;
1200 int viable;
1202 struct z_candidate *cand
1203 = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
1205 cand->fn = fn;
1206 cand->convs = convs;
1207 cand->second_conv = NULL_TREE;
1208 cand->viable = viable;
1209 cand->basetype_path = NULL_TREE;
1210 cand->template = NULL_TREE;
1211 cand->warnings = NULL_TREE;
1212 cand->next = candidates;
1214 return cand;
1217 /* Create an overload candidate for the function or method FN called with
1218 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1219 to implicit_conversion. */
1221 static struct z_candidate *
1222 add_function_candidate (candidates, fn, arglist, flags)
1223 struct z_candidate *candidates;
1224 tree fn, arglist;
1225 int flags;
1227 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1228 int i, len;
1229 tree convs;
1230 tree parmnode, argnode;
1231 int viable = 1;
1233 /* The `this' and `in_chrg' arguments to constructors are not considered
1234 in overload resolution. */
1235 if (DECL_CONSTRUCTOR_P (fn))
1237 parmlist = TREE_CHAIN (parmlist);
1238 arglist = TREE_CHAIN (arglist);
1239 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1241 parmlist = TREE_CHAIN (parmlist);
1242 arglist = TREE_CHAIN (arglist);
1246 len = list_length (arglist);
1247 convs = make_scratch_vec (len);
1249 /* 13.3.2 - Viable functions [over.match.viable]
1250 First, to be a viable function, a candidate function shall have enough
1251 parameters to agree in number with the arguments in the list.
1253 We need to check this first; otherwise, checking the ICSes might cause
1254 us to produce an ill-formed template instantiation. */
1256 parmnode = parmlist;
1257 for (i = 0; i < len; ++i)
1259 if (parmnode == NULL_TREE || parmnode == void_list_node)
1260 break;
1261 parmnode = TREE_CHAIN (parmnode);
1264 if (i < len && parmnode)
1265 viable = 0;
1267 /* Make sure there are default args for the rest of the parms. */
1268 else for (; parmnode && parmnode != void_list_node;
1269 parmnode = TREE_CHAIN (parmnode))
1270 if (! TREE_PURPOSE (parmnode))
1272 viable = 0;
1273 break;
1276 if (! viable)
1277 goto out;
1279 /* Second, for F to be a viable function, there shall exist for each
1280 argument an implicit conversion sequence that converts that argument
1281 to the corresponding parameter of F. */
1283 parmnode = parmlist;
1284 argnode = arglist;
1286 for (i = 0; i < len; ++i)
1288 tree arg = TREE_VALUE (argnode);
1289 tree argtype = lvalue_type (arg);
1290 tree t;
1292 if (parmnode == void_list_node)
1293 break;
1295 if (parmnode)
1297 tree parmtype = TREE_VALUE (parmnode);
1299 /* [over.match.funcs] For conversion functions, the function is
1300 considered to be a member of the class of the implicit object
1301 argument for the purpose of defining the type of the implicit
1302 object parameter.
1304 Since build_over_call ignores the ICS for the `this' parameter,
1305 we can just change the parm type. */
1306 if (DECL_CONV_FN_P (fn) && i == 0)
1308 parmtype
1309 = build_qualified_type (TREE_TYPE (argtype),
1310 TYPE_QUALS (TREE_TYPE (parmtype)));
1311 parmtype = build_pointer_type (parmtype);
1314 t = implicit_conversion (parmtype, argtype, arg, flags);
1316 else
1318 t = build1 (IDENTITY_CONV, argtype, arg);
1319 ICS_ELLIPSIS_FLAG (t) = 1;
1322 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1323 && ! DECL_CONSTRUCTOR_P (fn))
1324 ICS_THIS_FLAG (t) = 1;
1326 TREE_VEC_ELT (convs, i) = t;
1327 if (! t)
1329 viable = 0;
1330 break;
1333 if (ICS_BAD_FLAG (t))
1334 viable = -1;
1336 if (parmnode)
1337 parmnode = TREE_CHAIN (parmnode);
1338 argnode = TREE_CHAIN (argnode);
1341 out:
1342 return add_candidate (candidates, fn, convs, viable);
1345 /* Create an overload candidate for the conversion function FN which will
1346 be invoked for expression OBJ, producing a pointer-to-function which
1347 will in turn be called with the argument list ARGLIST, and add it to
1348 CANDIDATES. FLAGS is passed on to implicit_conversion.
1350 Actually, we don't really care about FN; we care about the type it
1351 converts to. There may be multiple conversion functions that will
1352 convert to that type, and we rely on build_user_type_conversion_1 to
1353 choose the best one; so when we create our candidate, we record the type
1354 instead of the function. */
1356 static struct z_candidate *
1357 add_conv_candidate (candidates, fn, obj, arglist)
1358 struct z_candidate *candidates;
1359 tree fn, obj, arglist;
1361 tree totype = TREE_TYPE (TREE_TYPE (fn));
1362 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1363 int i, len = list_length (arglist) + 1;
1364 tree convs = make_scratch_vec (len);
1365 tree parmnode = parmlist;
1366 tree argnode = arglist;
1367 int viable = 1;
1368 int flags = LOOKUP_NORMAL;
1370 /* Don't bother looking up the same type twice. */
1371 if (candidates && candidates->fn == totype)
1372 return candidates;
1374 for (i = 0; i < len; ++i)
1376 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1377 tree argtype = lvalue_type (arg);
1378 tree t;
1380 if (i == 0)
1381 t = implicit_conversion (totype, argtype, arg, flags);
1382 else if (parmnode == void_list_node)
1383 break;
1384 else if (parmnode)
1385 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1386 else
1388 t = build1 (IDENTITY_CONV, argtype, arg);
1389 ICS_ELLIPSIS_FLAG (t) = 1;
1392 TREE_VEC_ELT (convs, i) = t;
1393 if (! t)
1394 break;
1396 if (ICS_BAD_FLAG (t))
1397 viable = -1;
1399 if (i == 0)
1400 continue;
1402 if (parmnode)
1403 parmnode = TREE_CHAIN (parmnode);
1404 argnode = TREE_CHAIN (argnode);
1407 if (i < len)
1408 viable = 0;
1410 for (; parmnode && parmnode != void_list_node;
1411 parmnode = TREE_CHAIN (parmnode))
1412 if (! TREE_PURPOSE (parmnode))
1414 viable = 0;
1415 break;
1418 return add_candidate (candidates, totype, convs, viable);
1421 static struct z_candidate *
1422 build_builtin_candidate (candidates, fnname, type1, type2,
1423 args, argtypes, flags)
1424 struct z_candidate *candidates;
1425 tree fnname, type1, type2, *args, *argtypes;
1426 int flags;
1429 tree t, convs;
1430 int viable = 1, i;
1431 tree types[2];
1433 types[0] = type1;
1434 types[1] = type2;
1436 convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1438 for (i = 0; i < 2; ++i)
1440 if (! args[i])
1441 break;
1443 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1444 if (! t)
1446 viable = 0;
1447 /* We need something for printing the candidate. */
1448 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1450 else if (ICS_BAD_FLAG (t))
1451 viable = 0;
1452 TREE_VEC_ELT (convs, i) = t;
1455 /* For COND_EXPR we rearranged the arguments; undo that now. */
1456 if (args[2])
1458 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1459 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1460 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1461 if (t)
1462 TREE_VEC_ELT (convs, 0) = t;
1463 else
1464 viable = 0;
1467 return add_candidate (candidates, fnname, convs, viable);
1470 static int
1471 is_complete (t)
1472 tree t;
1474 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1477 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1479 static int
1480 promoted_arithmetic_type_p (type)
1481 tree type;
1483 /* [over.built]
1485 In this section, the term promoted integral type is used to refer
1486 to those integral types which are preserved by integral promotion
1487 (including e.g. int and long but excluding e.g. char).
1488 Similarly, the term promoted arithmetic type refers to promoted
1489 integral types plus floating types. */
1490 return ((INTEGRAL_TYPE_P (type)
1491 && same_type_p (type_promotes_to (type), type))
1492 || TREE_CODE (type) == REAL_TYPE);
1495 /* Create any builtin operator overload candidates for the operator in
1496 question given the converted operand types TYPE1 and TYPE2. The other
1497 args are passed through from add_builtin_candidates to
1498 build_builtin_candidate. */
1500 static struct z_candidate *
1501 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1502 args, argtypes, flags)
1503 struct z_candidate *candidates;
1504 enum tree_code code, code2;
1505 tree fnname, type1, type2, *args, *argtypes;
1506 int flags;
1508 switch (code)
1510 case POSTINCREMENT_EXPR:
1511 case POSTDECREMENT_EXPR:
1512 args[1] = integer_zero_node;
1513 type2 = integer_type_node;
1514 break;
1515 default:
1516 break;
1519 switch (code)
1522 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1523 and VQ is either volatile or empty, there exist candidate operator
1524 functions of the form
1525 VQ T& operator++(VQ T&);
1526 T operator++(VQ T&, int);
1527 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1528 type other than bool, and VQ is either volatile or empty, there exist
1529 candidate operator functions of the form
1530 VQ T& operator--(VQ T&);
1531 T operator--(VQ T&, int);
1532 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1533 complete object type, and VQ is either volatile or empty, there exist
1534 candidate operator functions of the form
1535 T*VQ& operator++(T*VQ&);
1536 T*VQ& operator--(T*VQ&);
1537 T* operator++(T*VQ&, int);
1538 T* operator--(T*VQ&, int); */
1540 case POSTDECREMENT_EXPR:
1541 case PREDECREMENT_EXPR:
1542 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1543 return candidates;
1544 case POSTINCREMENT_EXPR:
1545 case PREINCREMENT_EXPR:
1546 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1547 || TYPE_PTROB_P (type1))
1549 type1 = build_reference_type (type1);
1550 break;
1552 return candidates;
1554 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1555 exist candidate operator functions of the form
1557 T& operator*(T*);
1559 8 For every function type T, there exist candidate operator functions of
1560 the form
1561 T& operator*(T*); */
1563 case INDIRECT_REF:
1564 if (TREE_CODE (type1) == POINTER_TYPE
1565 && (TYPE_PTROB_P (type1)
1566 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1567 break;
1568 return candidates;
1570 /* 9 For every type T, there exist candidate operator functions of the form
1571 T* operator+(T*);
1573 10For every promoted arithmetic type T, there exist candidate operator
1574 functions of the form
1575 T operator+(T);
1576 T operator-(T); */
1578 case CONVERT_EXPR: /* unary + */
1579 if (TREE_CODE (type1) == POINTER_TYPE
1580 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1581 break;
1582 case NEGATE_EXPR:
1583 if (ARITHMETIC_TYPE_P (type1))
1584 break;
1585 return candidates;
1587 /* 11For every promoted integral type T, there exist candidate operator
1588 functions of the form
1589 T operator~(T); */
1591 case BIT_NOT_EXPR:
1592 if (INTEGRAL_TYPE_P (type1))
1593 break;
1594 return candidates;
1596 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1597 is the same type as C2 or is a derived class of C2, T is a complete
1598 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1599 there exist candidate operator functions of the form
1600 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1601 where CV12 is the union of CV1 and CV2. */
1603 case MEMBER_REF:
1604 if (TREE_CODE (type1) == POINTER_TYPE
1605 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1607 tree c1 = TREE_TYPE (type1);
1608 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1609 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1610 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1612 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1613 && (TYPE_PTRMEMFUNC_P (type2)
1614 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1615 break;
1617 return candidates;
1619 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1620 didate operator functions of the form
1621 LR operator*(L, R);
1622 LR operator/(L, R);
1623 LR operator+(L, R);
1624 LR operator-(L, R);
1625 bool operator<(L, R);
1626 bool operator>(L, R);
1627 bool operator<=(L, R);
1628 bool operator>=(L, R);
1629 bool operator==(L, R);
1630 bool operator!=(L, R);
1631 where LR is the result of the usual arithmetic conversions between
1632 types L and R.
1634 14For every pair of types T and I, where T is a cv-qualified or cv-
1635 unqualified complete object type and I is a promoted integral type,
1636 there exist candidate operator functions of the form
1637 T* operator+(T*, I);
1638 T& operator[](T*, I);
1639 T* operator-(T*, I);
1640 T* operator+(I, T*);
1641 T& operator[](I, T*);
1643 15For every T, where T is a pointer to complete object type, there exist
1644 candidate operator functions of the form112)
1645 ptrdiff_t operator-(T, T);
1647 16For every pointer type T, there exist candidate operator functions of
1648 the form
1649 bool operator<(T, T);
1650 bool operator>(T, T);
1651 bool operator<=(T, T);
1652 bool operator>=(T, T);
1653 bool operator==(T, T);
1654 bool operator!=(T, T);
1656 17For every pointer to member type T, there exist candidate operator
1657 functions of the form
1658 bool operator==(T, T);
1659 bool operator!=(T, T); */
1661 case MINUS_EXPR:
1662 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1663 break;
1664 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1666 type2 = ptrdiff_type_node;
1667 break;
1669 case MULT_EXPR:
1670 case TRUNC_DIV_EXPR:
1671 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1672 break;
1673 return candidates;
1675 case EQ_EXPR:
1676 case NE_EXPR:
1677 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1678 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1679 break;
1680 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1681 && null_ptr_cst_p (args[1]))
1683 type2 = type1;
1684 break;
1686 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1687 && null_ptr_cst_p (args[0]))
1689 type1 = type2;
1690 break;
1692 case LT_EXPR:
1693 case GT_EXPR:
1694 case LE_EXPR:
1695 case GE_EXPR:
1696 case MAX_EXPR:
1697 case MIN_EXPR:
1698 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1699 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1700 break;
1701 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1703 type2 = type1;
1704 break;
1706 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1708 type1 = type2;
1709 break;
1711 return candidates;
1713 case PLUS_EXPR:
1714 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1715 break;
1716 case ARRAY_REF:
1717 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1719 type1 = ptrdiff_type_node;
1720 break;
1722 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1724 type2 = ptrdiff_type_node;
1725 break;
1727 return candidates;
1729 /* 18For every pair of promoted integral types L and R, there exist candi-
1730 date operator functions of the form
1731 LR operator%(L, R);
1732 LR operator&(L, R);
1733 LR operator^(L, R);
1734 LR operator|(L, R);
1735 L operator<<(L, R);
1736 L operator>>(L, R);
1737 where LR is the result of the usual arithmetic conversions between
1738 types L and R. */
1740 case TRUNC_MOD_EXPR:
1741 case BIT_AND_EXPR:
1742 case BIT_IOR_EXPR:
1743 case BIT_XOR_EXPR:
1744 case LSHIFT_EXPR:
1745 case RSHIFT_EXPR:
1746 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1747 break;
1748 return candidates;
1750 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1751 type, VQ is either volatile or empty, and R is a promoted arithmetic
1752 type, there exist candidate operator functions of the form
1753 VQ L& operator=(VQ L&, R);
1754 VQ L& operator*=(VQ L&, R);
1755 VQ L& operator/=(VQ L&, R);
1756 VQ L& operator+=(VQ L&, R);
1757 VQ L& operator-=(VQ L&, R);
1759 20For every pair T, VQ), where T is any type and VQ is either volatile
1760 or empty, there exist candidate operator functions of the form
1761 T*VQ& operator=(T*VQ&, T*);
1763 21For every pair T, VQ), where T is a pointer to member type and VQ is
1764 either volatile or empty, there exist candidate operator functions of
1765 the form
1766 VQ T& operator=(VQ T&, T);
1768 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1769 unqualified complete object type, VQ is either volatile or empty, and
1770 I is a promoted integral type, there exist candidate operator func-
1771 tions of the form
1772 T*VQ& operator+=(T*VQ&, I);
1773 T*VQ& operator-=(T*VQ&, I);
1775 23For every triple L, VQ, R), where L is an integral or enumeration
1776 type, VQ is either volatile or empty, and R is a promoted integral
1777 type, there exist candidate operator functions of the form
1779 VQ L& operator%=(VQ L&, R);
1780 VQ L& operator<<=(VQ L&, R);
1781 VQ L& operator>>=(VQ L&, R);
1782 VQ L& operator&=(VQ L&, R);
1783 VQ L& operator^=(VQ L&, R);
1784 VQ L& operator|=(VQ L&, R); */
1786 case MODIFY_EXPR:
1787 switch (code2)
1789 case PLUS_EXPR:
1790 case MINUS_EXPR:
1791 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1793 type2 = ptrdiff_type_node;
1794 break;
1796 case MULT_EXPR:
1797 case TRUNC_DIV_EXPR:
1798 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1799 break;
1800 return candidates;
1802 case TRUNC_MOD_EXPR:
1803 case BIT_AND_EXPR:
1804 case BIT_IOR_EXPR:
1805 case BIT_XOR_EXPR:
1806 case LSHIFT_EXPR:
1807 case RSHIFT_EXPR:
1808 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1809 break;
1810 return candidates;
1812 case NOP_EXPR:
1813 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1814 break;
1815 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1816 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1817 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1818 || ((TYPE_PTRMEMFUNC_P (type1)
1819 || TREE_CODE (type1) == POINTER_TYPE)
1820 && null_ptr_cst_p (args[1])))
1822 type2 = type1;
1823 break;
1825 return candidates;
1827 default:
1828 my_friendly_abort (367);
1830 type1 = build_reference_type (type1);
1831 break;
1833 case COND_EXPR:
1834 /* [over.builtin]
1836 For every pair of promoted arithmetic types L and R, there
1837 exist candidate operator functions of the form
1839 LR operator?(bool, L, R);
1841 where LR is the result of the usual arithmetic conversions
1842 between types L and R.
1844 For every type T, where T is a pointer or pointer-to-member
1845 type, there exist candidate operator functions of the form T
1846 operator?(bool, T, T); */
1848 if (promoted_arithmetic_type_p (type1)
1849 && promoted_arithmetic_type_p (type2))
1850 /* That's OK. */
1851 break;
1853 /* Otherwise, the types should be pointers. */
1854 if (!(TREE_CODE (type1) == POINTER_TYPE
1855 || TYPE_PTRMEM_P (type1)
1856 || TYPE_PTRMEMFUNC_P (type1))
1857 || !(TREE_CODE (type2) == POINTER_TYPE
1858 || TYPE_PTRMEM_P (type2)
1859 || TYPE_PTRMEMFUNC_P (type2)))
1860 return candidates;
1862 /* We don't check that the two types are the same; the logic
1863 below will actually create two candidates; one in which both
1864 parameter types are TYPE1, and one in which both parameter
1865 types are TYPE2. */
1866 break;
1868 /* These arguments do not make for a legal overloaded operator. */
1869 return candidates;
1871 default:
1872 my_friendly_abort (367);
1875 /* If we're dealing with two pointer types, we need candidates
1876 for both of them. */
1877 if (type2 && !same_type_p (type1, type2)
1878 && TREE_CODE (type1) == TREE_CODE (type2)
1879 && (TREE_CODE (type1) == REFERENCE_TYPE
1880 || (TREE_CODE (type1) == POINTER_TYPE
1881 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1882 || TYPE_PTRMEMFUNC_P (type1)
1883 || IS_AGGR_TYPE (type1)))
1885 candidates = build_builtin_candidate
1886 (candidates, fnname, type1, type1, args, argtypes, flags);
1887 return build_builtin_candidate
1888 (candidates, fnname, type2, type2, args, argtypes, flags);
1891 return build_builtin_candidate
1892 (candidates, fnname, type1, type2, args, argtypes, flags);
1895 tree
1896 type_decays_to (type)
1897 tree type;
1899 if (TREE_CODE (type) == ARRAY_TYPE)
1900 return build_pointer_type (TREE_TYPE (type));
1901 if (TREE_CODE (type) == FUNCTION_TYPE)
1902 return build_pointer_type (type);
1903 return type;
1906 /* There are three conditions of builtin candidates:
1908 1) bool-taking candidates. These are the same regardless of the input.
1909 2) pointer-pair taking candidates. These are generated for each type
1910 one of the input types converts to.
1911 3) arithmetic candidates. According to the WP, we should generate
1912 all of these, but I'm trying not to... */
1914 static struct z_candidate *
1915 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1916 struct z_candidate *candidates;
1917 enum tree_code code, code2;
1918 tree fnname, *args;
1919 int flags;
1921 int ref1, i;
1922 tree type, argtypes[3];
1923 /* TYPES[i] is the set of possible builtin-operator parameter types
1924 we will consider for the Ith argument. These are represented as
1925 a TREE_LIST; the TREE_VALUE of each node is the potential
1926 parameter type. */
1927 tree types[2];
1929 for (i = 0; i < 3; ++i)
1931 if (args[i])
1932 argtypes[i] = lvalue_type (args[i]);
1933 else
1934 argtypes[i] = NULL_TREE;
1937 switch (code)
1939 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1940 and VQ is either volatile or empty, there exist candidate operator
1941 functions of the form
1942 VQ T& operator++(VQ T&); */
1944 case POSTINCREMENT_EXPR:
1945 case PREINCREMENT_EXPR:
1946 case POSTDECREMENT_EXPR:
1947 case PREDECREMENT_EXPR:
1948 case MODIFY_EXPR:
1949 ref1 = 1;
1950 break;
1952 /* 24There also exist candidate operator functions of the form
1953 bool operator!(bool);
1954 bool operator&&(bool, bool);
1955 bool operator||(bool, bool); */
1957 case TRUTH_NOT_EXPR:
1958 return build_builtin_candidate
1959 (candidates, fnname, boolean_type_node,
1960 NULL_TREE, args, argtypes, flags);
1962 case TRUTH_ORIF_EXPR:
1963 case TRUTH_ANDIF_EXPR:
1964 return build_builtin_candidate
1965 (candidates, fnname, boolean_type_node,
1966 boolean_type_node, args, argtypes, flags);
1968 case ADDR_EXPR:
1969 case COMPOUND_EXPR:
1970 case COMPONENT_REF:
1971 return candidates;
1973 default:
1974 ref1 = 0;
1977 types[0] = types[1] = NULL_TREE;
1979 for (i = 0; i < 2; ++i)
1981 if (! args[i])
1983 else if (IS_AGGR_TYPE (argtypes[i]))
1985 tree convs;
1987 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1988 return candidates;
1990 convs = lookup_conversions (argtypes[i]);
1992 if (code == COND_EXPR)
1994 if (real_lvalue_p (args[i]))
1995 types[i] = scratch_tree_cons
1996 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1998 types[i] = scratch_tree_cons
1999 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2002 else if (! convs)
2003 return candidates;
2005 for (; convs; convs = TREE_CHAIN (convs))
2007 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2009 if (i == 0 && ref1
2010 && (TREE_CODE (type) != REFERENCE_TYPE
2011 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2012 continue;
2014 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2015 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
2017 type = non_reference (type);
2018 if (i != 0 || ! ref1)
2020 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2021 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2022 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
2023 if (INTEGRAL_TYPE_P (type))
2024 type = type_promotes_to (type);
2027 if (! value_member (type, types[i]))
2028 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
2031 else
2033 if (code == COND_EXPR && real_lvalue_p (args[i]))
2034 types[i] = scratch_tree_cons
2035 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2036 type = non_reference (argtypes[i]);
2037 if (i != 0 || ! ref1)
2039 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2040 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2041 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
2042 if (INTEGRAL_TYPE_P (type))
2043 type = type_promotes_to (type);
2045 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
2049 /* Run through the possible parameter types of both arguments,
2050 creating candidates with those parameter types. */
2051 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2053 if (types[1])
2054 for (type = types[1]; type; type = TREE_CHAIN (type))
2055 candidates = add_builtin_candidate
2056 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2057 TREE_VALUE (type), args, argtypes, flags);
2058 else
2059 candidates = add_builtin_candidate
2060 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2061 NULL_TREE, args, argtypes, flags);
2064 return candidates;
2068 /* If TMPL can be successfully instantiated as indicated by
2069 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2071 TMPL is the template. EXPLICIT_TARGS are any explicit template
2072 arguments. ARGLIST is the arguments provided at the call-site.
2073 The RETURN_TYPE is the desired type for conversion operators. If
2074 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
2075 OBJ is supplied, FLAGS are ignored, and OBJ is as for
2076 add_conv_candidate. */
2078 static struct z_candidate*
2079 add_template_candidate_real (candidates, tmpl, explicit_targs,
2080 arglist, return_type, flags,
2081 obj, strict)
2082 struct z_candidate *candidates;
2083 tree tmpl, explicit_targs, arglist, return_type;
2084 int flags;
2085 tree obj;
2086 unification_kind_t strict;
2088 int ntparms = DECL_NTPARMS (tmpl);
2089 tree targs = make_scratch_vec (ntparms);
2090 struct z_candidate *cand;
2091 int i;
2092 tree fn;
2094 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
2095 return_type, strict);
2097 if (i != 0)
2098 return candidates;
2100 fn = instantiate_template (tmpl, targs);
2101 if (fn == error_mark_node)
2102 return candidates;
2104 if (obj != NULL_TREE)
2105 /* Aha, this is a conversion function. */
2106 cand = add_conv_candidate (candidates, fn, obj, arglist);
2107 else
2108 cand = add_function_candidate (candidates, fn, arglist, flags);
2109 if (DECL_TI_TEMPLATE (fn) != tmpl)
2110 /* This situation can occur if a member template of a template
2111 class is specialized. Then, instantiate_template might return
2112 an instantiation of the specialization, in which case the
2113 DECL_TI_TEMPLATE field will point at the original
2114 specialization. For example:
2116 template <class T> struct S { template <class U> void f(U);
2117 template <> void f(int) {}; };
2118 S<double> sd;
2119 sd.f(3);
2121 Here, TMPL will be template <class U> S<double>::f(U).
2122 And, instantiate template will give us the specialization
2123 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2124 for this will point at template <class T> template <> S<T>::f(int),
2125 so that we can find the definition. For the purposes of
2126 overload resolution, however, we want the original TMPL. */
2127 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2128 else
2129 cand->template = DECL_TEMPLATE_INFO (fn);
2131 return cand;
2135 static struct z_candidate *
2136 add_template_candidate (candidates, tmpl, explicit_targs,
2137 arglist, return_type, flags, strict)
2138 struct z_candidate *candidates;
2139 tree tmpl, explicit_targs, arglist, return_type;
2140 int flags;
2141 unification_kind_t strict;
2143 return
2144 add_template_candidate_real (candidates, tmpl, explicit_targs,
2145 arglist, return_type, flags,
2146 NULL_TREE, strict);
2150 static struct z_candidate *
2151 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2152 struct z_candidate *candidates;
2153 tree tmpl, obj, arglist, return_type;
2155 return
2156 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
2157 return_type, 0, obj, DEDUCE_CONV);
2161 static int
2162 any_viable (cands)
2163 struct z_candidate *cands;
2165 for (; cands; cands = cands->next)
2166 if (pedantic ? cands->viable == 1 : cands->viable)
2167 return 1;
2168 return 0;
2171 static struct z_candidate *
2172 splice_viable (cands)
2173 struct z_candidate *cands;
2175 struct z_candidate **p = &cands;
2177 for (; *p; )
2179 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2180 p = &((*p)->next);
2181 else
2182 *p = (*p)->next;
2185 return cands;
2188 static tree
2189 build_this (obj)
2190 tree obj;
2192 /* Fix this to work on non-lvalues. */
2193 return build_unary_op (ADDR_EXPR, obj, 0);
2196 static void
2197 print_z_candidates (candidates)
2198 struct z_candidate *candidates;
2200 const char *str = "candidates are:";
2201 for (; candidates; candidates = candidates->next)
2203 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2205 if (candidates->fn == ansi_opname [COND_EXPR])
2206 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2207 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2208 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2209 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2210 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2211 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2212 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2213 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2214 else
2215 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2216 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2218 else if (TYPE_P (candidates->fn))
2219 cp_error ("%s %T <conversion>", str, candidates->fn);
2220 else
2221 cp_error_at ("%s %+#D%s", str, candidates->fn,
2222 candidates->viable == -1 ? " <near match>" : "");
2223 str = " ";
2227 /* Returns the best overload candidate to perform the requested
2228 conversion. This function is used for three the overloading situations
2229 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2230 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2231 per [dcl.init.ref], so we ignore temporary bindings. */
2233 static struct z_candidate *
2234 build_user_type_conversion_1 (totype, expr, flags)
2235 tree totype, expr;
2236 int flags;
2238 struct z_candidate *candidates, *cand;
2239 tree fromtype = TREE_TYPE (expr);
2240 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2241 tree args = NULL_TREE;
2242 tree templates = NULL_TREE;
2244 if (IS_AGGR_TYPE (totype))
2245 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2246 if (IS_AGGR_TYPE (fromtype)
2247 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2248 convs = lookup_conversions (fromtype);
2250 candidates = 0;
2251 flags |= LOOKUP_NO_CONVERSION;
2253 if (ctors)
2255 tree t = build_int_2 (0, 0);
2256 TREE_TYPE (t) = build_pointer_type (totype);
2257 args = build_scratch_list (NULL_TREE, expr);
2258 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2259 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2260 args = scratch_tree_cons (NULL_TREE, t, args);
2262 ctors = TREE_VALUE (ctors);
2264 for (; ctors; ctors = OVL_NEXT (ctors))
2266 tree ctor = OVL_CURRENT (ctors);
2267 if (DECL_NONCONVERTING_P (ctor))
2268 continue;
2270 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2272 templates = scratch_tree_cons (NULL_TREE, ctor, templates);
2273 candidates =
2274 add_template_candidate (candidates, ctor,
2275 NULL_TREE, args, NULL_TREE, flags,
2276 DEDUCE_CALL);
2278 else
2279 candidates = add_function_candidate (candidates, ctor,
2280 args, flags);
2282 if (candidates)
2284 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2285 candidates->basetype_path = TYPE_BINFO (totype);
2289 if (convs)
2290 args = build_scratch_list (NULL_TREE, build_this (expr));
2292 for (; convs; convs = TREE_CHAIN (convs))
2294 tree fns = TREE_VALUE (convs);
2295 int convflags = LOOKUP_NO_CONVERSION;
2296 tree ics;
2298 /* If we are called to convert to a reference type, we are trying to
2299 find an lvalue binding, so don't even consider temporaries. If
2300 we don't find an lvalue binding, the caller will try again to
2301 look for a temporary binding. */
2302 if (TREE_CODE (totype) == REFERENCE_TYPE)
2303 convflags |= LOOKUP_NO_TEMP_BIND;
2305 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2306 ics = implicit_conversion
2307 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2308 else
2309 /* We can't compute this yet. */
2310 ics = error_mark_node;
2312 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2313 /* ignore the near match. */;
2314 else if (ics)
2315 for (; fns; fns = OVL_NEXT (fns))
2317 tree fn = OVL_CURRENT (fns);
2318 struct z_candidate *old_candidates = candidates;
2320 if (TREE_CODE (fn) == TEMPLATE_DECL)
2322 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2323 candidates =
2324 add_template_candidate (candidates, fn, NULL_TREE,
2325 args, totype, flags,
2326 DEDUCE_CONV);
2328 else
2329 candidates = add_function_candidate (candidates, fn,
2330 args, flags);
2332 if (candidates != old_candidates)
2334 if (TREE_CODE (fn) == TEMPLATE_DECL)
2335 ics = implicit_conversion
2336 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2337 0, convflags);
2339 candidates->second_conv = ics;
2340 candidates->basetype_path = TREE_PURPOSE (convs);
2342 if (ics == NULL_TREE)
2343 candidates->viable = 0;
2344 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2345 candidates->viable = -1;
2350 if (! any_viable (candidates))
2352 #if 0
2353 if (flags & LOOKUP_COMPLAIN)
2355 if (candidates && ! candidates->next)
2356 /* say why this one won't work or try to be loose */;
2357 else
2358 cp_error ("no viable candidates");
2360 #endif
2362 return 0;
2365 candidates = splice_viable (candidates);
2366 cand = tourney (candidates);
2368 if (cand == 0)
2370 if (flags & LOOKUP_COMPLAIN)
2372 cp_error ("conversion from `%T' to `%T' is ambiguous",
2373 fromtype, totype);
2374 print_z_candidates (candidates);
2377 cand = candidates; /* any one will do */
2378 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2379 ICS_USER_FLAG (cand->second_conv) = 1;
2380 ICS_BAD_FLAG (cand->second_conv) = 1;
2382 return cand;
2385 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2386 p = &(TREE_OPERAND (*p, 0));
2388 /* Pedantically, normal function declarations are never considered
2389 to refer to template instantiations, so we only do this with
2390 -fguiding-decls. */
2391 if (flag_guiding_decls && templates && ! cand->template
2392 && !DECL_INITIAL (cand->fn)
2393 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2394 add_maybe_template (cand->fn, templates);
2396 *p = build
2397 (USER_CONV,
2398 (DECL_CONSTRUCTOR_P (cand->fn)
2399 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2400 expr, build_expr_ptr_wrapper (cand));
2401 ICS_USER_FLAG (cand->second_conv) = 1;
2402 if (cand->viable == -1)
2403 ICS_BAD_FLAG (cand->second_conv) = 1;
2405 return cand;
2408 tree
2409 build_user_type_conversion (totype, expr, flags)
2410 tree totype, expr;
2411 int flags;
2413 struct z_candidate *cand
2414 = build_user_type_conversion_1 (totype, expr, flags);
2416 if (cand)
2418 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2419 return error_mark_node;
2420 return convert_from_reference (convert_like (cand->second_conv, expr));
2422 return NULL_TREE;
2425 /* Do any initial processing on the arguments to a function call. */
2427 static tree
2428 resolve_args (args)
2429 tree args;
2431 tree t;
2432 for (t = args; t; t = TREE_CHAIN (t))
2434 if (TREE_VALUE (t) == error_mark_node)
2435 return error_mark_node;
2436 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2438 error ("invalid use of void expression");
2439 return error_mark_node;
2441 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2442 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2444 return args;
2447 tree
2448 build_new_function_call (fn, args)
2449 tree fn, args;
2451 struct z_candidate *candidates = 0, *cand;
2452 tree explicit_targs = NULL_TREE;
2453 int template_only = 0;
2455 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2457 explicit_targs = TREE_OPERAND (fn, 1);
2458 fn = TREE_OPERAND (fn, 0);
2459 template_only = 1;
2462 if (really_overloaded_fn (fn))
2464 tree t1;
2465 tree templates = NULL_TREE;
2467 args = resolve_args (args);
2469 if (args == error_mark_node)
2470 return error_mark_node;
2472 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2474 tree t = OVL_FUNCTION (t1);
2475 struct z_candidate *old_candidates = candidates;
2477 if (TREE_CODE (t) == TEMPLATE_DECL)
2479 templates = scratch_tree_cons (NULL_TREE, t, templates);
2480 candidates = add_template_candidate
2481 (candidates, t, explicit_targs, args, NULL_TREE,
2482 LOOKUP_NORMAL, DEDUCE_CALL);
2484 else if (! template_only)
2485 candidates = add_function_candidate
2486 (candidates, t, args, LOOKUP_NORMAL);
2488 if (candidates != old_candidates)
2489 candidates->basetype_path = DECL_REAL_CONTEXT (t);
2492 if (! any_viable (candidates))
2494 if (candidates && ! candidates->next)
2495 return build_function_call (candidates->fn, args);
2496 cp_error ("no matching function for call to `%D (%A)'",
2497 DECL_NAME (OVL_FUNCTION (fn)), args);
2498 if (candidates)
2499 print_z_candidates (candidates);
2500 return error_mark_node;
2502 candidates = splice_viable (candidates);
2503 cand = tourney (candidates);
2505 if (cand == 0)
2507 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2508 DECL_NAME (OVL_FUNCTION (fn)), args);
2509 print_z_candidates (candidates);
2510 return error_mark_node;
2513 /* Pedantically, normal function declarations are never considered
2514 to refer to template instantiations, so we only do this with
2515 -fguiding-decls. */
2516 if (flag_guiding_decls && templates && ! cand->template
2517 && ! DECL_INITIAL (cand->fn))
2518 add_maybe_template (cand->fn, templates);
2520 return build_over_call (cand, args, LOOKUP_NORMAL);
2523 /* This is not really overloaded. */
2524 fn = OVL_CURRENT (fn);
2526 return build_function_call (fn, args);
2529 static tree
2530 build_object_call (obj, args)
2531 tree obj, args;
2533 struct z_candidate *candidates = 0, *cand;
2534 tree fns, convs, mem_args = NULL_TREE;
2535 tree type = TREE_TYPE (obj);
2537 if (TYPE_PTRMEMFUNC_P (type))
2539 /* It's no good looking for an overloaded operator() on a
2540 pointer-to-member-function. */
2541 cp_error ("pointer-to-member function %E cannot be called", obj);
2542 cp_error ("without an object; consider using .* or ->*");
2543 return error_mark_node;
2546 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2547 if (fns == error_mark_node)
2548 return error_mark_node;
2550 args = resolve_args (args);
2552 if (args == error_mark_node)
2553 return error_mark_node;
2555 if (fns)
2557 tree base = TREE_PURPOSE (fns);
2558 mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2560 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2562 tree fn = OVL_CURRENT (fns);
2563 if (TREE_CODE (fn) == TEMPLATE_DECL)
2565 candidates
2566 = add_template_candidate (candidates, fn, NULL_TREE,
2567 mem_args, NULL_TREE,
2568 LOOKUP_NORMAL, DEDUCE_CALL);
2570 else
2571 candidates = add_function_candidate
2572 (candidates, fn, mem_args, LOOKUP_NORMAL);
2574 if (candidates)
2575 candidates->basetype_path = base;
2579 convs = lookup_conversions (type);
2581 for (; convs; convs = TREE_CHAIN (convs))
2583 tree fns = TREE_VALUE (convs);
2584 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2586 if ((TREE_CODE (totype) == POINTER_TYPE
2587 || TREE_CODE (totype) == REFERENCE_TYPE)
2588 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2589 for (; fns; fns = OVL_NEXT (fns))
2591 tree fn = OVL_CURRENT (fns);
2592 if (TREE_CODE (fn) == TEMPLATE_DECL)
2594 candidates = add_template_conv_candidate (candidates,
2596 obj,
2597 args,
2598 totype);
2600 else
2601 candidates = add_conv_candidate (candidates, fn, obj, args);
2603 if (candidates)
2604 candidates->basetype_path = TREE_PURPOSE (convs);
2608 if (! any_viable (candidates))
2610 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2611 print_z_candidates (candidates);
2612 return error_mark_node;
2615 candidates = splice_viable (candidates);
2616 cand = tourney (candidates);
2618 if (cand == 0)
2620 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2621 print_z_candidates (candidates);
2622 return error_mark_node;
2625 /* Since cand->fn will be a type, not a function, for a conversion
2626 function, we must be careful not to unconditionally look at
2627 DECL_NAME here. */
2628 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2629 && DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2630 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2632 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2634 /* FIXME */
2635 return build_function_call (obj, args);
2638 static void
2639 op_error (code, code2, arg1, arg2, arg3, problem)
2640 enum tree_code code, code2;
2641 tree arg1, arg2, arg3;
2642 const char *problem;
2644 const char * opname
2645 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2647 switch (code)
2649 case COND_EXPR:
2650 cp_error ("%s for `%T ? %T : %T'", problem,
2651 error_type (arg1), error_type (arg2), error_type (arg3));
2652 break;
2653 case POSTINCREMENT_EXPR:
2654 case POSTDECREMENT_EXPR:
2655 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2656 break;
2657 case ARRAY_REF:
2658 cp_error ("%s for `%T[%T]'", problem,
2659 error_type (arg1), error_type (arg2));
2660 break;
2661 default:
2662 if (arg2)
2663 cp_error ("%s for `%T %s %T'", problem,
2664 error_type (arg1), opname, error_type (arg2));
2665 else
2666 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2670 /* Return the implicit conversion sequence that could be used to
2671 convert E1 to E2 in [expr.cond]. */
2673 static tree
2674 conditional_conversion (e1, e2)
2675 tree e1;
2676 tree e2;
2678 tree t1 = non_reference (TREE_TYPE (e1));
2679 tree t2 = non_reference (TREE_TYPE (e2));
2680 tree conv;
2682 /* [expr.cond]
2684 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2685 implicitly converted (clause _conv_) to the type "reference to
2686 T2", subject to the constraint that in the conversion the
2687 reference must bind directly (_dcl.init.ref_) to E1. */
2688 if (real_lvalue_p (e2))
2690 conv = implicit_conversion (build_reference_type (t2),
2693 LOOKUP_NO_TEMP_BIND);
2694 if (conv)
2695 return conv;
2698 /* [expr.cond]
2700 If E1 and E2 have class type, and the underlying class types are
2701 the same or one is a base class of the other: E1 can be converted
2702 to match E2 if the class of T2 is the same type as, or a base
2703 class of, the class of T1, and the cv-qualification of T2 is the
2704 same cv-qualification as, or a greater cv-qualification than, the
2705 cv-qualification of T1. If the conversion is applied, E1 is
2706 changed to an rvalue of type T2 that still refers to the original
2707 source class object (or the appropriate subobject thereof). */
2708 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2709 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2710 TYPE_MAIN_VARIANT (t1)))
2712 if (at_least_as_qualified_p (t2, t1))
2714 conv = build1 (IDENTITY_CONV, t1, e1);
2715 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2716 TYPE_MAIN_VARIANT (t2)))
2717 conv = build_conv (BASE_CONV, t2, conv);
2718 return conv;
2720 else
2721 return NULL_TREE;
2724 /* [expr.cond]
2726 E1 can be converted to match E2 if E1 can be implicitly converted
2727 to the type that expression E2 would have if E2 were converted to
2728 an rvalue (or the type it has, if E2 is an rvalue). */
2729 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2732 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2733 arguments to the conditional expression. By the time this function
2734 is called, any suitable candidate functions are included in
2735 CANDIDATES. */
2737 tree
2738 build_conditional_expr (arg1, arg2, arg3)
2739 tree arg1;
2740 tree arg2;
2741 tree arg3;
2743 tree arg2_type;
2744 tree arg3_type;
2745 tree result;
2746 tree result_type = NULL_TREE;
2747 int lvalue_p = 1;
2748 struct z_candidate *candidates = 0;
2749 struct z_candidate *cand;
2751 /* As a G++ extension, the second argument to the conditional can be
2752 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2753 c'.) If the second operand is omitted, make sure it is
2754 calculated only once. */
2755 if (!arg2)
2757 if (pedantic)
2758 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
2759 arg1 = arg2 = save_expr (arg1);
2762 /* If something has already gone wrong, just pass that fact up the
2763 tree. */
2764 if (arg1 == error_mark_node
2765 || arg2 == error_mark_node
2766 || arg3 == error_mark_node
2767 || TREE_TYPE (arg1) == error_mark_node
2768 || TREE_TYPE (arg2) == error_mark_node
2769 || TREE_TYPE (arg3) == error_mark_node)
2770 return error_mark_node;
2772 /* [expr.cond]
2774 The first expr ession is implicitly converted to bool (clause
2775 _conv_). */
2776 arg1 = cp_convert (boolean_type_node, arg1);
2778 /* Convert from reference types to ordinary types; no expressions
2779 really have reference type in C++. */
2780 arg2 = convert_from_reference (arg2);
2781 arg3 = convert_from_reference (arg3);
2783 /* [expr.cond]
2785 If either the second or the third operand has type (possibly
2786 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2787 array-to-pointer (_conv.array_), and function-to-pointer
2788 (_conv.func_) standard conversions are performed on the second
2789 and third operands. */
2790 arg2_type = TREE_TYPE (arg2);
2791 arg3_type = TREE_TYPE (arg3);
2792 if (same_type_p (TYPE_MAIN_VARIANT (arg2_type), void_type_node)
2793 || same_type_p (TYPE_MAIN_VARIANT (arg3_type), void_type_node))
2795 int arg2_void_p;
2796 int arg3_void_p;
2798 /* Do the conversions. We don't these for `void' type arguments
2799 since it can't have any effect and since decay_conversion
2800 does not handle that case gracefully. */
2801 if (!same_type_p (TYPE_MAIN_VARIANT (arg2_type), void_type_node))
2802 arg2 = decay_conversion (arg2);
2803 if (!same_type_p (TYPE_MAIN_VARIANT (arg3_type), void_type_node))
2804 arg3 = decay_conversion (arg3);
2805 arg2_type = TREE_TYPE (arg2);
2806 arg3_type = TREE_TYPE (arg3);
2808 arg2_void_p = same_type_p (TYPE_MAIN_VARIANT (arg2_type),
2809 void_type_node);
2810 arg3_void_p = same_type_p (TYPE_MAIN_VARIANT (arg3_type),
2811 void_type_node);
2813 /* [expr.cond]
2815 One of the following shall hold:
2817 --The second or the third operand (but not both) is a
2818 throw-expression (_except.throw_); the result is of the
2819 type of the other and is an rvalue.
2821 --Both the second and the third operands have type void; the
2822 result is of type void and is an rvalue. */
2823 if ((TREE_CODE (arg2) == THROW_EXPR)
2824 ^ (TREE_CODE (arg3) == THROW_EXPR))
2825 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2826 ? arg3_type : arg2_type);
2827 else if (arg2_void_p && arg3_void_p)
2828 result_type = void_type_node;
2829 else
2831 cp_error ("`%E' has type `void' and is not a throw-expression",
2832 arg2_void_p ? arg2 : arg3);
2833 return error_mark_node;
2836 lvalue_p = 0;
2837 goto valid_operands;
2839 /* [expr.cond]
2841 Otherwise, if the second and third operand have different types,
2842 and either has (possibly cv-qualified) class type, an attempt is
2843 made to convert each of those operands to the type of the other. */
2844 else if (!same_type_p (arg2_type, arg3_type)
2845 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2847 tree conv2 = conditional_conversion (arg2, arg3);
2848 tree conv3 = conditional_conversion (arg3, arg2);
2850 /* [expr.cond]
2852 If both can be converted, or one can be converted but the
2853 conversion is ambiguous, the program is ill-formed. If
2854 neither can be converted, the operands are left unchanged and
2855 further checking is performed as described below. If exactly
2856 one conversion is possible, that conversion is applied to the
2857 chosen operand and the converted operand is used in place of
2858 the original operand for the remainder of this section. */
2859 if ((conv2 && !ICS_BAD_FLAG (conv2)
2860 && conv3 && !ICS_BAD_FLAG (conv3))
2861 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
2862 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
2864 cp_error ("operands to ?: have different types");
2865 return error_mark_node;
2867 else if (conv2 && !ICS_BAD_FLAG (conv2))
2869 arg2 = convert_like (conv2, arg2);
2870 arg2 = convert_from_reference (arg2);
2871 /* That may not quite have done the trick. If the two types
2872 are cv-qualified variants of one another, we will have
2873 just used an IDENTITY_CONV. (There's no conversion from
2874 an lvalue of one class type to an lvalue of another type,
2875 even a cv-qualified variant, and we don't want to lose
2876 lvalue-ness here.) So, we manually add a NOP_EXPR here
2877 if necessary. */
2878 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
2879 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
2880 arg2_type = TREE_TYPE (arg2);
2882 else if (conv3 && !ICS_BAD_FLAG (conv3))
2884 arg3 = convert_like (conv3, arg3);
2885 arg3 = convert_from_reference (arg3);
2886 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
2887 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
2888 arg3_type = TREE_TYPE (arg3);
2892 /* [expr.cond]
2894 If the second and third operands are lvalues and have the same
2895 type, the result is of that type and is an lvalue. */
2896 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
2897 same_type_p (arg2_type, arg3_type))
2899 result_type = arg2_type;
2900 goto valid_operands;
2903 /* [expr.cond]
2905 Otherwise, the result is an rvalue. If the second and third
2906 operand do not have the same type, and either has (possibly
2907 cv-qualified) class type, overload resolution is used to
2908 determine the conversions (if any) to be applied to the operands
2909 (_over.match.oper_, _over.built_). */
2910 lvalue_p = 0;
2911 if (!same_type_p (arg2_type, arg3_type)
2912 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2914 tree args[3];
2915 tree conv;
2917 /* Rearrange the arguments so that add_builtin_candidate only has
2918 to know about two args. In build_builtin_candidates, the
2919 arguments are unscrambled. */
2920 args[0] = arg2;
2921 args[1] = arg3;
2922 args[2] = arg1;
2923 candidates = add_builtin_candidates (candidates,
2924 COND_EXPR,
2925 NOP_EXPR,
2926 ansi_opname[COND_EXPR],
2927 args,
2928 LOOKUP_NORMAL);
2930 /* [expr.cond]
2932 If the overload resolution fails, the program is
2933 ill-formed. */
2934 if (!any_viable (candidates))
2936 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
2937 print_z_candidates (candidates);
2938 return error_mark_node;
2940 candidates = splice_viable (candidates);
2941 cand = tourney (candidates);
2942 if (!cand)
2944 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
2945 print_z_candidates (candidates);
2946 return error_mark_node;
2949 /* [expr.cond]
2951 Otherwise, the conversions thus determined are applied, and
2952 the converted operands are used in place of the original
2953 operands for the remainder of this section. */
2954 conv = TREE_VEC_ELT (cand->convs, 0);
2955 arg1 = convert_like (conv, arg1);
2956 conv = TREE_VEC_ELT (cand->convs, 1);
2957 arg2 = convert_like (conv, arg2);
2958 conv = TREE_VEC_ELT (cand->convs, 2);
2959 arg3 = convert_like (conv, arg3);
2962 /* [expr.cond]
2964 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
2965 and function-to-pointer (_conv.func_) standard conversions are
2966 performed on the second and third operands. */
2967 arg2 = decay_conversion (arg2);
2968 arg2_type = TREE_TYPE (arg2);
2969 arg3 = decay_conversion (arg3);
2970 arg3_type = TREE_TYPE (arg3);
2972 /* [expr.cond]
2974 After those conversions, one of the following shall hold:
2976 --The second and third operands have the same type; the result is of
2977 that type. */
2978 if (same_type_p (arg2_type, arg3_type))
2979 result_type = arg2_type;
2980 /* [expr.cond]
2982 --The second and third operands have arithmetic or enumeration
2983 type; the usual arithmetic conversions are performed to bring
2984 them to a common type, and the result is of that type. */
2985 else if ((ARITHMETIC_TYPE_P (arg2_type)
2986 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
2987 && (ARITHMETIC_TYPE_P (arg3_type)
2988 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
2990 /* In this case, there is always a common type. */
2991 result_type = type_after_usual_arithmetic_conversions (arg2_type,
2992 arg3_type);
2994 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
2995 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
2996 cp_warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
2997 arg2_type, arg3_type);
2998 else if (extra_warnings
2999 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3000 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3001 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3002 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3003 cp_warning ("enumeral and non-enumeral type in conditional expression");
3005 arg2 = perform_implicit_conversion (result_type, arg2);
3006 arg3 = perform_implicit_conversion (result_type, arg3);
3008 /* [expr.cond]
3010 --The second and third operands have pointer type, or one has
3011 pointer type and the other is a null pointer constant; pointer
3012 conversions (_conv.ptr_) and qualification conversions
3013 (_conv.qual_) are performed to bring them to their composite
3014 pointer type (_expr.rel_). The result is of the composite
3015 pointer type.
3017 --The second and third operands have pointer to member type, or
3018 one has pointer to member type and the other is a null pointer
3019 constant; pointer to member conversions (_conv.mem_) and
3020 qualification conversions (_conv.qual_) are performed to bring
3021 them to a common type, whose cv-qualification shall match the
3022 cv-qualification of either the second or the third operand.
3023 The result is of the common type. */
3024 else if ((null_ptr_cst_p (arg2)
3025 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3026 || TYPE_PTRMEMFUNC_P (arg3_type)))
3027 || (null_ptr_cst_p (arg3)
3028 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3029 || TYPE_PTRMEMFUNC_P (arg2_type)))
3030 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3031 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3032 || (TYPE_PTRMEMFUNC_P (arg2_type)
3033 && TYPE_PTRMEMFUNC_P (arg3_type)))
3035 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3036 arg3, "conditional expression");
3037 arg2 = perform_implicit_conversion (result_type, arg2);
3038 arg3 = perform_implicit_conversion (result_type, arg3);
3041 if (!result_type)
3043 cp_error ("operands to ?: have different types");
3044 return error_mark_node;
3047 valid_operands:
3048 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3049 /* Expand both sides into the same slot, hopefully the target of the
3050 ?: expression. */
3051 if (TREE_CODE (arg2) == TARGET_EXPR && TREE_CODE (arg3) == TARGET_EXPR)
3053 tree slot = build (VAR_DECL, result_type);
3054 layout_decl (slot, 0);
3055 result = build (TARGET_EXPR, result_type,
3056 slot, result, NULL_TREE, NULL_TREE);
3059 /* If this expression is an rvalue, but might be mistaken for an
3060 lvalue, we must add a NON_LVALUE_EXPR. */
3061 if (!lvalue_p && real_lvalue_p (result))
3062 result = build1 (NON_LVALUE_EXPR, result_type, result);
3064 return result;
3067 tree
3068 build_new_op (code, flags, arg1, arg2, arg3)
3069 enum tree_code code;
3070 int flags;
3071 tree arg1, arg2, arg3;
3073 struct z_candidate *candidates = 0, *cand;
3074 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3075 enum tree_code code2 = NOP_EXPR;
3076 tree templates = NULL_TREE;
3077 tree conv;
3079 if (arg1 == error_mark_node
3080 || arg2 == error_mark_node
3081 || arg3 == error_mark_node)
3082 return error_mark_node;
3084 /* This can happen if a template takes all non-type parameters, e.g.
3085 undeclared_template<1, 5, 72>a; */
3086 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3088 cp_error ("`%D' must be declared before use", arg1);
3089 return error_mark_node;
3092 if (code == MODIFY_EXPR)
3094 code2 = TREE_CODE (arg3);
3095 arg3 = NULL_TREE;
3096 fnname = ansi_assopname[code2];
3098 else
3099 fnname = ansi_opname[code];
3101 switch (code)
3103 case NEW_EXPR:
3104 case VEC_NEW_EXPR:
3105 case VEC_DELETE_EXPR:
3106 case DELETE_EXPR:
3107 /* Use build_op_new_call and build_op_delete_call instead. */
3108 my_friendly_abort (981018);
3110 case CALL_EXPR:
3111 return build_object_call (arg1, arg2);
3113 default:
3114 break;
3117 /* The comma operator can have void args. */
3118 if (TREE_CODE (arg1) == OFFSET_REF)
3119 arg1 = resolve_offset_ref (arg1);
3120 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
3121 arg2 = resolve_offset_ref (arg2);
3122 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
3123 arg3 = resolve_offset_ref (arg3);
3125 if (code == COND_EXPR)
3127 if (arg2 == NULL_TREE
3128 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3129 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3130 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3131 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3132 goto builtin;
3134 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3135 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3136 goto builtin;
3138 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3139 arg2 = integer_zero_node;
3141 if (arg2 && arg3)
3142 arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
3143 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
3144 else if (arg2)
3145 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
3146 else
3147 arglist = build_scratch_list (NULL_TREE, arg1);
3149 fns = lookup_function_nonclass (fnname, arglist);
3151 if (fns && TREE_CODE (fns) == TREE_LIST)
3152 fns = TREE_VALUE (fns);
3153 for (; fns; fns = OVL_NEXT (fns))
3155 tree fn = OVL_CURRENT (fns);
3156 if (TREE_CODE (fn) == TEMPLATE_DECL)
3158 templates = scratch_tree_cons (NULL_TREE, fn, templates);
3159 candidates
3160 = add_template_candidate (candidates, fn, NULL_TREE,
3161 arglist, TREE_TYPE (fnname),
3162 flags, DEDUCE_CALL);
3164 else
3165 candidates = add_function_candidate (candidates, fn, arglist, flags);
3168 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3170 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3171 if (fns == error_mark_node)
3172 return fns;
3174 else
3175 fns = NULL_TREE;
3177 if (fns)
3179 tree basetype = TREE_PURPOSE (fns);
3180 mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3181 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3183 tree fn = OVL_CURRENT (fns);
3184 tree this_arglist;
3186 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3187 this_arglist = mem_arglist;
3188 else
3189 this_arglist = arglist;
3191 if (TREE_CODE (fn) == TEMPLATE_DECL)
3193 /* A member template. */
3194 templates = scratch_tree_cons (NULL_TREE, fn, templates);
3195 candidates
3196 = add_template_candidate (candidates, fn, NULL_TREE,
3197 this_arglist, TREE_TYPE (fnname),
3198 flags, DEDUCE_CALL);
3200 else
3201 candidates = add_function_candidate
3202 (candidates, fn, this_arglist, flags);
3204 if (candidates)
3205 candidates->basetype_path = basetype;
3210 tree args[3];
3212 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3213 to know about two args; a builtin candidate will always have a first
3214 parameter of type bool. We'll handle that in
3215 build_builtin_candidate. */
3216 if (code == COND_EXPR)
3218 args[0] = arg2;
3219 args[1] = arg3;
3220 args[2] = arg1;
3222 else
3224 args[0] = arg1;
3225 args[1] = arg2;
3226 args[2] = NULL_TREE;
3229 candidates = add_builtin_candidates
3230 (candidates, code, code2, fnname, args, flags);
3233 if (! any_viable (candidates))
3235 switch (code)
3237 case POSTINCREMENT_EXPR:
3238 case POSTDECREMENT_EXPR:
3239 /* Look for an `operator++ (int)'. If they didn't have
3240 one, then we fall back to the old way of doing things. */
3241 if (flags & LOOKUP_COMPLAIN)
3242 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
3243 fnname, opname_tab [code]);
3244 if (code == POSTINCREMENT_EXPR)
3245 code = PREINCREMENT_EXPR;
3246 else
3247 code = PREDECREMENT_EXPR;
3248 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3250 /* The caller will deal with these. */
3251 case ADDR_EXPR:
3252 case COMPOUND_EXPR:
3253 case COMPONENT_REF:
3254 return NULL_TREE;
3256 default:
3257 break;
3259 if (flags & LOOKUP_COMPLAIN)
3261 op_error (code, code2, arg1, arg2, arg3, "no match");
3262 print_z_candidates (candidates);
3264 return error_mark_node;
3266 candidates = splice_viable (candidates);
3267 cand = tourney (candidates);
3269 if (cand == 0)
3271 if (flags & LOOKUP_COMPLAIN)
3273 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3274 print_z_candidates (candidates);
3276 return error_mark_node;
3279 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3281 extern int warn_synth;
3282 if (warn_synth
3283 && fnname == ansi_opname[MODIFY_EXPR]
3284 && DECL_ARTIFICIAL (cand->fn)
3285 && candidates->next
3286 && ! candidates->next->next)
3288 cp_warning ("using synthesized `%#D' for copy assignment",
3289 cand->fn);
3290 cp_warning_at (" where cfront would use `%#D'",
3291 cand == candidates
3292 ? candidates->next->fn
3293 : candidates->fn);
3296 /* Pedantically, normal function declarations are never considered
3297 to refer to template instantiations, so we only do this with
3298 -fguiding-decls. */
3299 if (flag_guiding_decls && templates && ! cand->template
3300 && ! DECL_INITIAL (cand->fn)
3301 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
3302 add_maybe_template (cand->fn, templates);
3304 return build_over_call
3305 (cand,
3306 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3307 ? mem_arglist : arglist,
3308 LOOKUP_NORMAL);
3311 /* Check for comparison of different enum types. */
3312 switch (code)
3314 case GT_EXPR:
3315 case LT_EXPR:
3316 case GE_EXPR:
3317 case LE_EXPR:
3318 case EQ_EXPR:
3319 case NE_EXPR:
3320 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3321 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3322 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3323 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3325 cp_warning ("comparison between `%#T' and `%#T'",
3326 TREE_TYPE (arg1), TREE_TYPE (arg2));
3328 break;
3329 default:
3330 break;
3333 /* We need to strip any leading REF_BIND so that bitfields don't cause
3334 errors. This should not remove any important conversions, because
3335 builtins don't apply to class objects directly. */
3336 conv = TREE_VEC_ELT (cand->convs, 0);
3337 if (TREE_CODE (conv) == REF_BIND)
3338 conv = TREE_OPERAND (conv, 0);
3339 arg1 = convert_like (conv, arg1);
3340 if (arg2)
3342 conv = TREE_VEC_ELT (cand->convs, 1);
3343 if (TREE_CODE (conv) == REF_BIND)
3344 conv = TREE_OPERAND (conv, 0);
3345 arg2 = convert_like (conv, arg2);
3347 if (arg3)
3349 conv = TREE_VEC_ELT (cand->convs, 2);
3350 if (TREE_CODE (conv) == REF_BIND)
3351 conv = TREE_OPERAND (conv, 0);
3352 arg3 = convert_like (conv, arg3);
3355 builtin:
3356 switch (code)
3358 case MODIFY_EXPR:
3359 return build_modify_expr (arg1, code2, arg2);
3361 case INDIRECT_REF:
3362 return build_indirect_ref (arg1, "unary *");
3364 case PLUS_EXPR:
3365 case MINUS_EXPR:
3366 case MULT_EXPR:
3367 case TRUNC_DIV_EXPR:
3368 case GT_EXPR:
3369 case LT_EXPR:
3370 case GE_EXPR:
3371 case LE_EXPR:
3372 case EQ_EXPR:
3373 case NE_EXPR:
3374 case MAX_EXPR:
3375 case MIN_EXPR:
3376 case LSHIFT_EXPR:
3377 case RSHIFT_EXPR:
3378 case TRUNC_MOD_EXPR:
3379 case BIT_AND_EXPR:
3380 case BIT_IOR_EXPR:
3381 case BIT_XOR_EXPR:
3382 case TRUTH_ANDIF_EXPR:
3383 case TRUTH_ORIF_EXPR:
3384 return build_binary_op_nodefault (code, arg1, arg2, code);
3386 case CONVERT_EXPR:
3387 case NEGATE_EXPR:
3388 case BIT_NOT_EXPR:
3389 case TRUTH_NOT_EXPR:
3390 case PREINCREMENT_EXPR:
3391 case POSTINCREMENT_EXPR:
3392 case PREDECREMENT_EXPR:
3393 case POSTDECREMENT_EXPR:
3394 case REALPART_EXPR:
3395 case IMAGPART_EXPR:
3396 return build_unary_op (code, arg1, candidates != 0);
3398 case ARRAY_REF:
3399 return build_array_ref (arg1, arg2);
3401 case COND_EXPR:
3402 return build_conditional_expr (arg1, arg2, arg3);
3404 case MEMBER_REF:
3405 return build_m_component_ref
3406 (build_indirect_ref (arg1, NULL_PTR), arg2);
3408 /* The caller will deal with these. */
3409 case ADDR_EXPR:
3410 case COMPONENT_REF:
3411 case COMPOUND_EXPR:
3412 return NULL_TREE;
3414 default:
3415 my_friendly_abort (367);
3416 return NULL_TREE;
3420 /* Build up a call to operator new. This has to be handled differently
3421 from other operators in the way lookup is handled; first members are
3422 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
3423 TYPE is the type to be created. ARGS are any new-placement args.
3424 FLAGS are the usual overloading flags. */
3426 tree
3427 build_op_new_call (code, type, args, flags)
3428 enum tree_code code;
3429 tree type, args;
3430 int flags;
3432 tree fnname = ansi_opname[code];
3434 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
3435 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
3437 return build_method_call (build_dummy_object (type),
3438 fnname, args, NULL_TREE, flags);
3440 else
3441 return build_new_function_call
3442 (lookup_function_nonclass (fnname, args), args);
3445 /* Build a call to operator delete. This has to be handled very specially,
3446 because the restrictions on what signatures match are different from all
3447 other call instances. For a normal delete, only a delete taking (void *)
3448 or (void *, size_t) is accepted. For a placement delete, only an exact
3449 match with the placement new is accepted.
3451 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3452 ADDR is the pointer to be deleted. For placement delete, it is also
3453 used to determine what the corresponding new looked like.
3454 SIZE is the size of the memory block to be deleted.
3455 FLAGS are the usual overloading flags.
3456 PLACEMENT is the corresponding placement new call, or 0. */
3458 tree
3459 build_op_delete_call (code, addr, size, flags, placement)
3460 enum tree_code code;
3461 tree addr, size, placement;
3462 int flags;
3464 tree fn, fns, fnname, fntype, argtypes, args, type;
3466 if (addr == error_mark_node)
3467 return error_mark_node;
3469 type = TREE_TYPE (TREE_TYPE (addr));
3470 fnname = ansi_opname[code];
3472 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3473 /* In [class.free]
3475 If the result of the lookup is ambiguous or inaccessible, or if
3476 the lookup selects a placement deallocation function, the
3477 program is ill-formed.
3479 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3481 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3482 if (fns == error_mark_node)
3483 return error_mark_node;
3485 else
3486 fns = NULL_TREE;
3488 if (fns == NULL_TREE)
3489 fns = lookup_name_nonclass (fnname);
3491 if (placement)
3493 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3495 /* Extract the function. */
3496 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3497 /* Then the second parm type. */
3498 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3500 /* Also the second argument. */
3501 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3503 else
3505 /* First try it without the size argument. */
3506 argtypes = void_list_node;
3507 args = NULL_TREE;
3510 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3511 fntype = build_function_type (void_type_node, argtypes);
3513 /* Strip const and volatile from addr. */
3514 if (type != TYPE_MAIN_VARIANT (type))
3515 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
3517 fn = instantiate_type (fntype, fns, 2);
3519 if (fn != error_mark_node)
3521 if (TREE_CODE (fns) == TREE_LIST)
3522 /* Member functions. */
3523 enforce_access (TREE_PURPOSE (fns), fn);
3524 return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
3527 /* If we are doing placement delete we do nothing if we don't find a
3528 matching op delete. */
3529 if (placement)
3530 return NULL_TREE;
3532 /* Normal delete; now try to find a match including the size argument. */
3533 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3534 tree_cons (NULL_TREE, sizetype, void_list_node));
3535 fntype = build_function_type (void_type_node, argtypes);
3537 fn = instantiate_type (fntype, fns, 2);
3539 if (fn != error_mark_node)
3541 if (BASELINK_P (fns))
3542 /* Member functions. */
3543 enforce_access (TREE_PURPOSE (fns), fn);
3544 return build_function_call
3545 (fn, expr_tree_cons (NULL_TREE, addr,
3546 build_expr_list (NULL_TREE, size)));
3549 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3550 destructor, in which case the error should be deferred
3551 until someone actually tries to delete one of these. */
3552 if (flags & LOOKUP_SPECULATIVELY)
3553 return NULL_TREE;
3555 cp_error ("no suitable operator delete for `%T'", type);
3556 return error_mark_node;
3559 /* If the current scope isn't allowed to access DECL along
3560 BASETYPE_PATH, give an error. The most derived class in
3561 BASETYPE_PATH is the one used to qualify DECL. */
3564 enforce_access (basetype_path, decl)
3565 tree basetype_path;
3566 tree decl;
3568 int accessible;
3570 accessible = accessible_p (basetype_path, decl);
3571 if (!accessible)
3573 if (TREE_PRIVATE (decl))
3574 cp_error_at ("`%+#D' is private", decl);
3575 else if (TREE_PROTECTED (decl))
3576 cp_error_at ("`%+#D' is protected", decl);
3577 else
3578 cp_error_at ("`%+#D' is inaccessible", decl);
3579 cp_error ("within this context");
3580 return 0;
3583 return 1;
3586 /* Perform the conversions in CONVS on the expression EXPR. */
3588 static tree
3589 convert_like (convs, expr)
3590 tree convs, expr;
3592 if (ICS_BAD_FLAG (convs)
3593 && TREE_CODE (convs) != USER_CONV
3594 && TREE_CODE (convs) != AMBIG_CONV
3595 && TREE_CODE (convs) != REF_BIND)
3597 tree t = convs;
3598 for (; t; t = TREE_OPERAND (t, 0))
3600 if (TREE_CODE (t) == USER_CONV)
3602 expr = convert_like (t, expr);
3603 break;
3605 else if (TREE_CODE (t) == AMBIG_CONV)
3606 return convert_like (t, expr);
3607 else if (TREE_CODE (t) == IDENTITY_CONV)
3608 break;
3610 return convert_for_initialization
3611 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3612 "conversion", NULL_TREE, 0);
3615 switch (TREE_CODE (convs))
3617 case USER_CONV:
3619 struct z_candidate *cand
3620 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3621 tree fn = cand->fn;
3622 tree args;
3624 if (DECL_CONSTRUCTOR_P (fn))
3626 tree t = build_int_2 (0, 0);
3627 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3629 args = build_scratch_list (NULL_TREE, expr);
3630 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3631 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3632 args = scratch_tree_cons (NULL_TREE, t, args);
3634 else
3635 args = build_this (expr);
3636 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3638 /* If this is a constructor or a function returning an aggr type,
3639 we need to build up a TARGET_EXPR. */
3640 if (DECL_CONSTRUCTOR_P (fn))
3641 expr = build_cplus_new (TREE_TYPE (convs), expr);
3643 return expr;
3645 case IDENTITY_CONV:
3646 if (type_unknown_p (expr))
3647 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3648 return expr;
3649 case AMBIG_CONV:
3650 /* Call build_user_type_conversion again for the error. */
3651 return build_user_type_conversion
3652 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3654 default:
3655 break;
3658 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3659 if (expr == error_mark_node)
3660 return error_mark_node;
3662 /* Convert a constant variable to its underlying value, unless we
3663 are about to bind it to a reference, in which case we need to
3664 leave it as an lvalue. */
3665 if (TREE_READONLY_DECL_P (expr) && TREE_CODE (convs) != REF_BIND)
3666 expr = decl_constant_value (expr);
3668 switch (TREE_CODE (convs))
3670 case RVALUE_CONV:
3671 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3672 return expr;
3673 /* else fall through */
3674 case BASE_CONV:
3675 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3677 /* We are going to bind a reference directly to a base-class
3678 subobject of EXPR. */
3679 tree base_ptr = build_pointer_type (TREE_TYPE (convs));
3681 /* Build an expression for `*((base*) &expr)'. */
3682 expr = build_unary_op (ADDR_EXPR, expr, 0);
3683 expr = perform_implicit_conversion (base_ptr, expr);
3684 expr = build_indirect_ref (expr, "implicit conversion");
3685 return expr;
3689 tree cvt_expr = build_user_type_conversion
3690 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3691 if (!cvt_expr)
3693 /* This can occur if, for example, the EXPR has incomplete
3694 type. We can't check for that before attempting the
3695 conversion because the type might be an incomplete
3696 array type, which is OK if some constructor for the
3697 destination type takes a pointer argument. */
3698 if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
3700 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
3701 incomplete_type_error (expr, TREE_TYPE (expr));
3702 else
3703 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3704 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3706 else
3707 cp_error ("could not convert `%E' to `%T'",
3708 expr, TREE_TYPE (convs));
3709 return error_mark_node;
3711 return cvt_expr;
3714 case REF_BIND:
3716 tree ref_type = TREE_TYPE (convs);
3718 /* If necessary, create a temporary. */
3719 if (NEED_TEMPORARY_P (convs))
3721 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3722 tree slot = build_decl (VAR_DECL, NULL_TREE, type);
3723 DECL_ARTIFICIAL (slot) = 1;
3724 expr = build (TARGET_EXPR, type, slot, expr,
3725 NULL_TREE, NULL_TREE);
3726 TREE_SIDE_EFFECTS (expr) = 1;
3729 /* Take the address of the thing to which we will bind the
3730 reference. */
3731 expr = build_unary_op (ADDR_EXPR, expr, 1);
3732 if (expr == error_mark_node)
3733 return error_mark_node;
3735 /* Convert it to a pointer to the type referred to by the
3736 reference. This will adjust the pointer if a derived to
3737 base conversion is being performed. */
3738 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3739 expr);
3740 /* Convert the pointer to the desired reference type. */
3741 expr = build1 (NOP_EXPR, ref_type, expr);
3743 return expr;
3746 case LVALUE_CONV:
3747 return decay_conversion (expr);
3749 case QUAL_CONV:
3750 /* Warn about deprecated conversion if appropriate. */
3751 string_conv_p (TREE_TYPE (convs), expr, 1);
3752 break;
3754 default:
3755 break;
3757 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3758 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3761 /* ARG is being passed to a varargs function. Perform any conversions
3762 required. Array/function to pointer decay must have already happened.
3763 Return the converted value. */
3765 tree
3766 convert_arg_to_ellipsis (arg)
3767 tree arg;
3769 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3770 && (TYPE_PRECISION (TREE_TYPE (arg))
3771 < TYPE_PRECISION (double_type_node)))
3772 /* Convert `float' to `double'. */
3773 arg = cp_convert (double_type_node, arg);
3774 else
3775 /* Convert `short' and `char' to full-size `int'. */
3776 arg = default_conversion (arg);
3778 arg = require_complete_type (arg);
3780 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
3782 /* Undefined behaviour [expr.call] 5.2.2/7. */
3783 cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3784 TREE_TYPE (arg));
3787 return arg;
3790 /* ARG is a default argument expression being passed to a parameter of
3791 the indicated TYPE, which is a parameter to FN. Do any required
3792 conversions. Return the converted value. */
3794 tree
3795 convert_default_arg (type, arg, fn)
3796 tree type;
3797 tree arg;
3798 tree fn;
3800 if (fn && DECL_TEMPLATE_INFO (fn))
3801 arg = tsubst_default_argument (fn, type, arg);
3803 arg = break_out_target_exprs (arg);
3805 if (TREE_CODE (arg) == CONSTRUCTOR)
3807 arg = digest_init (type, arg, 0);
3808 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3809 "default argument", 0, 0);
3811 else
3813 /* This could get clobbered by the following call. */
3814 if (TREE_HAS_CONSTRUCTOR (arg))
3815 arg = copy_node (arg);
3817 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3818 "default argument", 0, 0);
3819 if (PROMOTE_PROTOTYPES
3820 && (TREE_CODE (type) == INTEGER_TYPE
3821 || TREE_CODE (type) == ENUMERAL_TYPE)
3822 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3823 arg = default_conversion (arg);
3826 return arg;
3829 static tree
3830 build_over_call (cand, args, flags)
3831 struct z_candidate *cand;
3832 tree args;
3833 int flags;
3835 tree fn = cand->fn;
3836 tree convs = cand->convs;
3837 tree converted_args = NULL_TREE;
3838 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3839 tree conv, arg, val;
3840 int i = 0;
3841 int is_method = 0;
3843 /* Give any warnings we noticed during overload resolution. */
3844 if (cand->warnings)
3845 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3846 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3848 if (DECL_FUNCTION_MEMBER_P (fn))
3849 enforce_access (cand->basetype_path, fn);
3851 if (args && TREE_CODE (args) != TREE_LIST)
3852 args = build_scratch_list (NULL_TREE, args);
3853 arg = args;
3855 /* The implicit parameters to a constructor are not considered by overload
3856 resolution, and must be of the proper type. */
3857 if (DECL_CONSTRUCTOR_P (fn))
3859 converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3860 arg = TREE_CHAIN (arg);
3861 parm = TREE_CHAIN (parm);
3862 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3864 converted_args = expr_tree_cons
3865 (NULL_TREE, TREE_VALUE (arg), converted_args);
3866 arg = TREE_CHAIN (arg);
3867 parm = TREE_CHAIN (parm);
3870 /* Bypass access control for 'this' parameter. */
3871 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3873 tree parmtype = TREE_VALUE (parm);
3874 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3875 tree t;
3876 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3877 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3878 TREE_TYPE (argtype), fn);
3880 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3881 X is called for an object that is not of type X, or of a type
3882 derived from X, the behavior is undefined.
3884 So we can assume that anything passed as 'this' is non-null, and
3885 optimize accordingly. */
3886 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
3887 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3888 converted_args = expr_tree_cons (NULL_TREE, t, converted_args);
3889 parm = TREE_CHAIN (parm);
3890 arg = TREE_CHAIN (arg);
3891 ++i;
3892 is_method = 1;
3895 for (; arg && parm;
3896 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3898 tree type = TREE_VALUE (parm);
3900 conv = TREE_VEC_ELT (convs, i);
3901 if (ICS_BAD_FLAG (conv))
3903 tree t = conv;
3904 val = TREE_VALUE (arg);
3906 for (; t; t = TREE_OPERAND (t, 0))
3908 if (TREE_CODE (t) == USER_CONV
3909 || TREE_CODE (t) == AMBIG_CONV)
3911 val = convert_like (t, val);
3912 break;
3914 else if (TREE_CODE (t) == IDENTITY_CONV)
3915 break;
3917 val = convert_for_initialization
3918 (NULL_TREE, type, val, LOOKUP_NORMAL,
3919 "argument passing", fn, i - is_method);
3921 else
3923 /* Issue warnings about peculiar, but legal, uses of NULL. */
3924 if (ARITHMETIC_TYPE_P (TREE_VALUE (parm))
3925 && TREE_VALUE (arg) == null_node)
3926 cp_warning ("converting NULL to non-pointer type");
3928 val = convert_like (conv, TREE_VALUE (arg));
3931 if (PROMOTE_PROTOTYPES
3932 && (TREE_CODE (type) == INTEGER_TYPE
3933 || TREE_CODE (type) == ENUMERAL_TYPE)
3934 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3935 val = default_conversion (val);
3936 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3939 /* Default arguments */
3940 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3941 converted_args
3942 = expr_tree_cons (NULL_TREE,
3943 convert_default_arg (TREE_VALUE (parm),
3944 TREE_PURPOSE (parm),
3945 fn),
3946 converted_args);
3948 /* Ellipsis */
3949 for (; arg; arg = TREE_CHAIN (arg))
3950 converted_args
3951 = expr_tree_cons (NULL_TREE,
3952 convert_arg_to_ellipsis (TREE_VALUE (arg)),
3953 converted_args);
3955 converted_args = nreverse (converted_args);
3957 if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
3958 check_function_format (DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
3959 converted_args);
3961 /* Avoid actually calling copy constructors and copy assignment operators,
3962 if possible. */
3964 if (! flag_elide_constructors)
3965 /* Do things the hard way. */;
3966 else if (DECL_CONSTRUCTOR_P (fn)
3967 && TREE_VEC_LENGTH (convs) == 1
3968 && copy_args_p (fn))
3970 tree targ;
3971 arg = TREE_CHAIN (converted_args);
3972 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3973 arg = TREE_CHAIN (arg);
3974 arg = TREE_VALUE (arg);
3976 /* Pull out the real argument, disregarding const-correctness. */
3977 targ = arg;
3978 while (TREE_CODE (targ) == NOP_EXPR
3979 || TREE_CODE (targ) == NON_LVALUE_EXPR
3980 || TREE_CODE (targ) == CONVERT_EXPR)
3981 targ = TREE_OPERAND (targ, 0);
3982 if (TREE_CODE (targ) == ADDR_EXPR)
3984 targ = TREE_OPERAND (targ, 0);
3985 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3986 TYPE_MAIN_VARIANT (TREE_TYPE (targ))))
3987 targ = NULL_TREE;
3989 else
3990 targ = NULL_TREE;
3992 if (targ)
3993 arg = targ;
3994 else
3995 arg = build_indirect_ref (arg, 0);
3997 /* [class.copy]: the copy constructor is implicitly defined even if
3998 the implementation elided its use. */
3999 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4000 mark_used (fn);
4002 /* If we're creating a temp and we already have one, don't create a
4003 new one. If we're not creating a temp but we get one, use
4004 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4005 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4006 temp or an INIT_EXPR otherwise. */
4007 if (integer_zerop (TREE_VALUE (args)))
4009 if (! real_lvalue_p (arg))
4010 return arg;
4011 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4013 val = build_decl (VAR_DECL, NULL_TREE, DECL_CONTEXT (fn));
4014 val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
4015 TREE_SIDE_EFFECTS (val) = 1;
4016 return val;
4019 else if (! real_lvalue_p (arg)
4020 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4022 tree address;
4023 tree to = stabilize_reference
4024 (build_indirect_ref (TREE_VALUE (args), 0));
4026 /* If we're initializing an empty class, then we actually
4027 have to use a MODIFY_EXPR rather than an INIT_EXPR. The
4028 reason is that the dummy padding member in the target may
4029 not actually be allocated if TO is a base class
4030 subobject. Since we've set TYPE_NONCOPIED_PARTS on the
4031 padding, a MODIFY_EXPR will preserve its value, which is
4032 the right thing to do if it's not really padding at all.
4034 It's not safe to just throw away the ARG if we're looking
4035 at an empty class because the ARG might contain a
4036 TARGET_EXPR which wants to be bound to TO. If it is not,
4037 expand_expr will assign a dummy slot for the TARGET_EXPR,
4038 and we will call a destructor for it, which is wrong,
4039 because we will also destroy TO, but will never have
4040 constructed it. */
4041 val = build (is_empty_class (DECL_CLASS_CONTEXT (fn))
4042 ? MODIFY_EXPR : INIT_EXPR,
4043 DECL_CONTEXT (fn), to, arg);
4044 TREE_SIDE_EFFECTS (val) = 1;
4045 address = build_unary_op (ADDR_EXPR, val, 0);
4046 /* Avoid a warning about this expression, if the address is
4047 never used. */
4048 TREE_USED (address) = 1;
4049 return address;
4052 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
4053 && copy_args_p (fn)
4054 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
4056 tree to = stabilize_reference
4057 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4059 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4061 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4062 TREE_SIDE_EFFECTS (val) = 1;
4063 return val;
4066 mark_used (fn);
4068 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4070 tree t, *p = &TREE_VALUE (converted_args);
4071 tree binfo = get_binfo
4072 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
4073 *p = convert_pointer_to_real (binfo, *p);
4074 if (TREE_SIDE_EFFECTS (*p))
4075 *p = save_expr (*p);
4076 t = build_pointer_type (TREE_TYPE (fn));
4077 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4078 TREE_TYPE (fn) = t;
4080 else if (DECL_INLINE (fn))
4081 fn = inline_conversion (fn);
4082 else
4083 fn = build_addr_func (fn);
4085 /* Recognize certain built-in functions so we can make tree-codes
4086 other than CALL_EXPR. We do this when it enables fold-const.c
4087 to do something useful. */
4089 if (TREE_CODE (fn) == ADDR_EXPR
4090 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4091 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4092 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
4094 case BUILT_IN_ABS:
4095 case BUILT_IN_LABS:
4096 case BUILT_IN_FABS:
4097 if (converted_args == 0)
4098 return integer_zero_node;
4099 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
4100 default:
4101 break;
4104 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
4105 if (TREE_CODE (TREE_TYPE (fn)) == VOID_TYPE)
4106 return fn;
4107 fn = require_complete_type (fn);
4108 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4109 fn = build_cplus_new (TREE_TYPE (fn), fn);
4110 return convert_from_reference (fn);
4113 static tree
4114 build_new_method_call (instance, name, args, basetype_path, flags)
4115 tree instance, name, args, basetype_path;
4116 int flags;
4118 struct z_candidate *candidates = 0, *cand;
4119 tree explicit_targs = NULL_TREE;
4120 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4121 tree pretty_name;
4122 tree user_args = args;
4123 tree templates = NULL_TREE;
4124 int template_only = 0;
4126 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4128 explicit_targs = TREE_OPERAND (name, 1);
4129 name = TREE_OPERAND (name, 0);
4130 if (TREE_CODE_CLASS (TREE_CODE (name)) == 'd')
4131 name = DECL_NAME (name);
4132 else
4134 if (TREE_CODE (name) == COMPONENT_REF)
4135 name = TREE_OPERAND (name, 1);
4136 if (TREE_CODE (name) == OVERLOAD)
4137 name = DECL_NAME (OVL_CURRENT (name));
4140 template_only = 1;
4143 /* If there is an extra argument for controlling virtual bases,
4144 remove it for error reporting. */
4145 if (flags & LOOKUP_HAS_IN_CHARGE)
4146 user_args = TREE_CHAIN (args);
4148 args = resolve_args (args);
4150 if (args == error_mark_node)
4151 return error_mark_node;
4153 if (instance == NULL_TREE)
4154 basetype = BINFO_TYPE (basetype_path);
4155 else
4157 if (TREE_CODE (instance) == OFFSET_REF)
4158 instance = resolve_offset_ref (instance);
4159 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4160 instance = convert_from_reference (instance);
4161 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4163 /* XXX this should be handled before we get here. */
4164 if (! IS_AGGR_TYPE (basetype))
4166 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4167 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4168 name, instance, basetype);
4170 return error_mark_node;
4174 if (basetype_path == NULL_TREE)
4175 basetype_path = TYPE_BINFO (basetype);
4177 if (instance)
4179 instance_ptr = build_this (instance);
4181 if (! template_only)
4183 /* XXX this should be handled before we get here. */
4184 fns = build_field_call (basetype_path, instance_ptr, name, args);
4185 if (fns)
4186 return fns;
4189 else
4191 instance_ptr = build_int_2 (0, 0);
4192 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4195 pretty_name
4196 = (name == ctor_identifier ? constructor_name (basetype) : name);
4198 fns = lookup_fnfields (basetype_path, name, 1);
4200 if (fns == error_mark_node)
4201 return error_mark_node;
4202 if (fns)
4204 tree fn = TREE_VALUE (fns);
4205 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
4206 && ! (flags & LOOKUP_HAS_IN_CHARGE))
4208 flags |= LOOKUP_HAS_IN_CHARGE;
4209 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
4211 mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
4212 for (; fn; fn = OVL_NEXT (fn))
4214 tree t = OVL_CURRENT (fn);
4215 tree this_arglist;
4217 /* We can end up here for copy-init of same or base class. */
4218 if (name == ctor_identifier
4219 && (flags & LOOKUP_ONLYCONVERTING)
4220 && DECL_NONCONVERTING_P (t))
4221 continue;
4222 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
4223 this_arglist = mem_args;
4224 else
4225 this_arglist = args;
4227 if (TREE_CODE (t) == TEMPLATE_DECL)
4229 /* A member template. */
4230 templates = scratch_tree_cons (NULL_TREE, t, templates);
4231 candidates =
4232 add_template_candidate (candidates, t, explicit_targs,
4233 this_arglist,
4234 TREE_TYPE (name), flags, DEDUCE_CALL);
4236 else if (! template_only)
4237 candidates = add_function_candidate (candidates, t,
4238 this_arglist, flags);
4240 if (candidates)
4241 candidates->basetype_path = TREE_PURPOSE (fns);
4245 if (! any_viable (candidates))
4247 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4248 if (flags & LOOKUP_SPECULATIVELY)
4249 return NULL_TREE;
4250 if (TYPE_SIZE (basetype) == 0)
4251 incomplete_type_error (instance_ptr, basetype);
4252 else
4253 cp_error ("no matching function for call to `%T::%D (%A)%V'",
4254 basetype, pretty_name, user_args,
4255 TREE_TYPE (TREE_TYPE (instance_ptr)));
4256 print_z_candidates (candidates);
4257 return error_mark_node;
4259 candidates = splice_viable (candidates);
4260 cand = tourney (candidates);
4262 if (cand == 0)
4264 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4265 user_args);
4266 print_z_candidates (candidates);
4267 return error_mark_node;
4270 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
4271 && instance == current_class_ref
4272 && DECL_CONSTRUCTOR_P (current_function_decl)
4273 && ! (flags & LOOKUP_NONVIRTUAL)
4274 && value_member (cand->fn, CLASSTYPE_ABSTRACT_VIRTUALS (basetype)))
4275 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
4276 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4277 && is_dummy_object (instance_ptr))
4279 cp_error ("cannot call member function `%D' without object", cand->fn);
4280 return error_mark_node;
4283 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4284 && ((instance == current_class_ref && (dtor_label || ctor_label))
4285 || resolves_to_fixed_type_p (instance, 0)))
4286 flags |= LOOKUP_NONVIRTUAL;
4288 /* Pedantically, normal function declarations are never considered
4289 to refer to template instantiations, so we only do this with
4290 -fguiding-decls. */
4291 if (flag_guiding_decls && templates && ! cand->template
4292 && ! DECL_INITIAL (cand->fn))
4293 add_maybe_template (cand->fn, templates);
4295 return build_over_call
4296 (cand,
4297 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
4298 flags);
4301 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4302 subsequence of ICS2. */
4304 static int
4305 is_subseq (ics1, ics2)
4306 tree ics1, ics2;
4308 /* We can assume that a conversion of the same code
4309 between the same types indicates a subsequence since we only get
4310 here if the types we are converting from are the same. */
4312 while (TREE_CODE (ics1) == RVALUE_CONV
4313 || TREE_CODE (ics1) == LVALUE_CONV)
4314 ics1 = TREE_OPERAND (ics1, 0);
4316 while (1)
4318 while (TREE_CODE (ics2) == RVALUE_CONV
4319 || TREE_CODE (ics2) == LVALUE_CONV)
4320 ics2 = TREE_OPERAND (ics2, 0);
4322 if (TREE_CODE (ics2) == USER_CONV
4323 || TREE_CODE (ics2) == AMBIG_CONV
4324 || TREE_CODE (ics2) == IDENTITY_CONV)
4325 /* At this point, ICS1 cannot be a proper subsequence of
4326 ICS2. We can get a USER_CONV when we are comparing the
4327 second standard conversion sequence of two user conversion
4328 sequences. */
4329 return 0;
4331 ics2 = TREE_OPERAND (ics2, 0);
4333 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4334 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4335 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4336 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4337 return 1;
4341 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4342 be any _TYPE nodes. */
4345 is_properly_derived_from (derived, base)
4346 tree derived;
4347 tree base;
4349 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4350 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4351 return 0;
4353 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4354 considers every class derived from itself. */
4355 return (!same_type_p (TYPE_MAIN_VARIANT (derived),
4356 TYPE_MAIN_VARIANT (base))
4357 && DERIVED_FROM_P (base, derived));
4360 /* We build the ICS for an implicit object parameter as a pointer
4361 conversion sequence. However, such a sequence should be compared
4362 as if it were a reference conversion sequence. If ICS is the
4363 implicit conversion sequence for an implicit object parameter,
4364 modify it accordingly. */
4366 static void
4367 maybe_handle_implicit_object (ics)
4368 tree* ics;
4370 if (ICS_THIS_FLAG (*ics))
4372 /* [over.match.funcs]
4374 For non-static member functions, the type of the
4375 implicit object parameter is "reference to cv X"
4376 where X is the class of which the function is a
4377 member and cv is the cv-qualification on the member
4378 function declaration. */
4379 tree t = *ics;
4380 tree reference_type;
4382 /* The `this' parameter is a pointer to a class type. Make the
4383 implict conversion talk about a reference to that same class
4384 type. */
4385 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4386 reference_type = build_reference_type (reference_type);
4388 if (TREE_CODE (t) == QUAL_CONV)
4389 t = TREE_OPERAND (t, 0);
4390 if (TREE_CODE (t) == PTR_CONV)
4391 t = TREE_OPERAND (t, 0);
4392 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4393 t = direct_reference_binding (reference_type, t);
4394 *ics = t;
4398 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
4399 to the type the reference originally referred to, and return 1.
4400 Otherwise, return 0. */
4402 static int
4403 maybe_handle_ref_bind (ics, target_type)
4404 tree* ics;
4405 tree* target_type;
4407 if (TREE_CODE (*ics) == REF_BIND)
4409 *target_type = TREE_TYPE (TREE_TYPE (*ics));
4410 *ics = TREE_OPERAND (*ics, 0);
4411 return 1;
4414 return 0;
4417 /* Compare two implicit conversion sequences according to the rules set out in
4418 [over.ics.rank]. Return values:
4420 1: ics1 is better than ics2
4421 -1: ics2 is better than ics1
4422 0: ics1 and ics2 are indistinguishable */
4424 static int
4425 compare_ics (ics1, ics2)
4426 tree ics1, ics2;
4428 tree from_type1;
4429 tree from_type2;
4430 tree to_type1;
4431 tree to_type2;
4432 tree deref_from_type1 = NULL_TREE;
4433 tree deref_from_type2 = NULL_TREE;
4434 tree deref_to_type1 = NULL_TREE;
4435 tree deref_to_type2 = NULL_TREE;
4437 /* REF_BINDING is non-zero if the result of the conversion sequence
4438 is a reference type. In that case TARGET_TYPE is the
4439 type referred to by the reference. */
4440 int ref_binding1;
4441 int ref_binding2;
4442 tree target_type1;
4443 tree target_type2;
4445 /* Handle implicit object parameters. */
4446 maybe_handle_implicit_object (&ics1);
4447 maybe_handle_implicit_object (&ics2);
4449 /* Handle reference parameters. */
4450 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
4451 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
4453 /* [over.ics.rank]
4455 When comparing the basic forms of implicit conversion sequences (as
4456 defined in _over.best.ics_)
4458 --a standard conversion sequence (_over.ics.scs_) is a better
4459 conversion sequence than a user-defined conversion sequence
4460 or an ellipsis conversion sequence, and
4462 --a user-defined conversion sequence (_over.ics.user_) is a
4463 better conversion sequence than an ellipsis conversion sequence
4464 (_over.ics.ellipsis_). */
4465 if (ICS_RANK (ics1) > ICS_RANK (ics2))
4466 return -1;
4467 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
4468 return 1;
4470 if (ICS_RANK (ics1) == BAD_RANK)
4472 /* Both ICS are bad. We try to make a decision based on what
4473 would have happenned if they'd been good. */
4474 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4475 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4476 return -1;
4477 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4478 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4479 return 1;
4481 /* We couldn't make up our minds; try to figure it out below. */
4484 if (ICS_ELLIPSIS_FLAG (ics1))
4485 /* Both conversions are ellipsis conversions. */
4486 return 0;
4488 /* User-defined conversion sequence U1 is a better conversion sequence
4489 than another user-defined conversion sequence U2 if they contain the
4490 same user-defined conversion operator or constructor and if the sec-
4491 ond standard conversion sequence of U1 is better than the second
4492 standard conversion sequence of U2. */
4494 if (ICS_USER_FLAG (ics1))
4496 tree t1, t2;
4498 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4499 if (TREE_CODE (t1) == AMBIG_CONV)
4500 return 0;
4501 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4502 if (TREE_CODE (t2) == AMBIG_CONV)
4503 return 0;
4505 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4506 return 0;
4508 /* We can just fall through here, after setting up
4509 FROM_TYPE1 and FROM_TYPE2. */
4510 from_type1 = TREE_TYPE (t1);
4511 from_type2 = TREE_TYPE (t2);
4513 else
4515 /* We're dealing with two standard conversion sequences.
4517 [over.ics.rank]
4519 Standard conversion sequence S1 is a better conversion
4520 sequence than standard conversion sequence S2 if
4522 --S1 is a proper subsequence of S2 (comparing the conversion
4523 sequences in the canonical form defined by _over.ics.scs_,
4524 excluding any Lvalue Transformation; the identity
4525 conversion sequence is considered to be a subsequence of
4526 any non-identity conversion sequence */
4528 from_type1 = ics1;
4529 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4530 from_type1 = TREE_OPERAND (from_type1, 0);
4531 from_type1 = TREE_TYPE (from_type1);
4533 from_type2 = ics2;
4534 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4535 from_type2 = TREE_OPERAND (from_type2, 0);
4536 from_type2 = TREE_TYPE (from_type2);
4539 if (same_type_p (from_type1, from_type2))
4541 if (is_subseq (ics1, ics2))
4542 return 1;
4543 if (is_subseq (ics2, ics1))
4544 return -1;
4546 /* Otherwise, one sequence cannot be a subsequence of the other; they
4547 don't start with the same type. This can happen when comparing the
4548 second standard conversion sequence in two user-defined conversion
4549 sequences. */
4551 /* [over.ics.rank]
4553 Or, if not that,
4555 --the rank of S1 is better than the rank of S2 (by the rules
4556 defined below):
4558 Standard conversion sequences are ordered by their ranks: an Exact
4559 Match is a better conversion than a Promotion, which is a better
4560 conversion than a Conversion.
4562 Two conversion sequences with the same rank are indistinguishable
4563 unless one of the following rules applies:
4565 --A conversion that is not a conversion of a pointer, or pointer
4566 to member, to bool is better than another conversion that is such
4567 a conversion.
4569 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4570 so that we do not have to check it explicitly. */
4571 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4572 return 1;
4573 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4574 return -1;
4576 to_type1 = TREE_TYPE (ics1);
4577 to_type2 = TREE_TYPE (ics2);
4579 if (TYPE_PTR_P (from_type1)
4580 && TYPE_PTR_P (from_type2)
4581 && TYPE_PTR_P (to_type1)
4582 && TYPE_PTR_P (to_type2))
4584 deref_from_type1 = TREE_TYPE (from_type1);
4585 deref_from_type2 = TREE_TYPE (from_type2);
4586 deref_to_type1 = TREE_TYPE (to_type1);
4587 deref_to_type2 = TREE_TYPE (to_type2);
4589 /* The rules for pointers to members A::* are just like the rules
4590 for pointers A*, except opposite: if B is derived from A then
4591 A::* converts to B::*, not vice versa. For that reason, we
4592 switch the from_ and to_ variables here. */
4593 else if (TYPE_PTRMEM_P (from_type1)
4594 && TYPE_PTRMEM_P (from_type2)
4595 && TYPE_PTRMEM_P (to_type1)
4596 && TYPE_PTRMEM_P (to_type2))
4598 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4599 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4600 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4601 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4603 else if (TYPE_PTRMEMFUNC_P (from_type1)
4604 && TYPE_PTRMEMFUNC_P (from_type2)
4605 && TYPE_PTRMEMFUNC_P (to_type1)
4606 && TYPE_PTRMEMFUNC_P (to_type2))
4608 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4609 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4610 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4611 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4614 if (deref_from_type1 != NULL_TREE
4615 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4616 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4618 /* This was one of the pointer or pointer-like conversions.
4620 [over.ics.rank]
4622 --If class B is derived directly or indirectly from class A,
4623 conversion of B* to A* is better than conversion of B* to
4624 void*, and conversion of A* to void* is better than
4625 conversion of B* to void*. */
4626 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4627 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4629 if (is_properly_derived_from (deref_from_type1,
4630 deref_from_type2))
4631 return -1;
4632 else if (is_properly_derived_from (deref_from_type2,
4633 deref_from_type1))
4634 return 1;
4636 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4637 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4639 if (same_type_p (deref_from_type1, deref_from_type2))
4641 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4643 if (is_properly_derived_from (deref_from_type1,
4644 deref_to_type1))
4645 return 1;
4647 /* We know that DEREF_TO_TYPE1 is `void' here. */
4648 else if (is_properly_derived_from (deref_from_type1,
4649 deref_to_type2))
4650 return -1;
4653 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4654 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4656 /* [over.ics.rank]
4658 --If class B is derived directly or indirectly from class A
4659 and class C is derived directly or indirectly from B,
4661 --conversion of C* to B* is better than conversion of C* to
4662 A*,
4664 --conversion of B* to A* is better than conversion of C* to
4665 A* */
4666 if (same_type_p (deref_from_type1, deref_from_type2))
4668 if (is_properly_derived_from (deref_to_type1,
4669 deref_to_type2))
4670 return 1;
4671 else if (is_properly_derived_from (deref_to_type2,
4672 deref_to_type1))
4673 return -1;
4675 else if (same_type_p (deref_to_type1, deref_to_type2))
4677 if (is_properly_derived_from (deref_from_type2,
4678 deref_from_type1))
4679 return 1;
4680 else if (is_properly_derived_from (deref_from_type1,
4681 deref_from_type2))
4682 return -1;
4686 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4687 && same_type_p (from_type1, from_type2))
4689 /* [over.ics.rank]
4691 --binding of an expression of type C to a reference of type
4692 B& is better than binding an expression of type C to a
4693 reference of type A&
4695 --conversion of C to B is better than conversion of C to A, */
4696 if (is_properly_derived_from (from_type1, to_type1)
4697 && is_properly_derived_from (from_type1, to_type2))
4699 if (is_properly_derived_from (to_type1, to_type2))
4700 return 1;
4701 else if (is_properly_derived_from (to_type2, to_type1))
4702 return -1;
4705 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4706 && same_type_p (to_type1, to_type2))
4708 /* [over.ics.rank]
4710 --binding of an expression of type B to a reference of type
4711 A& is better than binding an expression of type C to a
4712 reference of type A&,
4714 --onversion of B to A is better than conversion of C to A */
4715 if (is_properly_derived_from (from_type1, to_type1)
4716 && is_properly_derived_from (from_type2, to_type1))
4718 if (is_properly_derived_from (from_type2, from_type1))
4719 return 1;
4720 else if (is_properly_derived_from (from_type1, from_type2))
4721 return -1;
4725 /* [over.ics.rank]
4727 --S1 and S2 differ only in their qualification conversion and yield
4728 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4729 qualification signature of type T1 is a proper subset of the cv-
4730 qualification signature of type T2 */
4731 if (TREE_CODE (ics1) == QUAL_CONV
4732 && TREE_CODE (ics2) == QUAL_CONV
4733 && same_type_p (from_type1, from_type2))
4734 return comp_cv_qual_signature (to_type1, to_type2);
4736 /* [over.ics.rank]
4738 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4739 types to which the references refer are the same type except for
4740 top-level cv-qualifiers, and the type to which the reference
4741 initialized by S2 refers is more cv-qualified than the type to
4742 which the reference initialized by S1 refers */
4744 if (ref_binding1 && ref_binding2
4745 && same_type_p (TYPE_MAIN_VARIANT (to_type1),
4746 TYPE_MAIN_VARIANT (to_type2)))
4747 return comp_cv_qualification (target_type2, target_type1);
4749 /* Neither conversion sequence is better than the other. */
4750 return 0;
4753 /* The source type for this standard conversion sequence. */
4755 static tree
4756 source_type (t)
4757 tree t;
4759 for (;; t = TREE_OPERAND (t, 0))
4761 if (TREE_CODE (t) == USER_CONV
4762 || TREE_CODE (t) == AMBIG_CONV
4763 || TREE_CODE (t) == IDENTITY_CONV)
4764 return TREE_TYPE (t);
4766 my_friendly_abort (1823);
4769 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4770 a pointer to LOSER and re-running joust to produce the warning if WINNER
4771 is actually used. */
4773 static void
4774 add_warning (winner, loser)
4775 struct z_candidate *winner, *loser;
4777 winner->warnings = expr_tree_cons (NULL_PTR,
4778 build_expr_ptr_wrapper (loser),
4779 winner->warnings);
4782 /* Returns true iff functions are equivalent. Equivalent functions are
4783 not identical only if one is a function-local extern function.
4784 This assumes that function-locals don't have TREE_PERMANENT. */
4786 static inline int
4787 equal_functions (fn1, fn2)
4788 tree fn1;
4789 tree fn2;
4791 if (!TREE_PERMANENT (fn1) || !TREE_PERMANENT (fn2))
4792 return decls_match (fn1, fn2);
4793 return fn1 == fn2;
4796 /* Compare two candidates for overloading as described in
4797 [over.match.best]. Return values:
4799 1: cand1 is better than cand2
4800 -1: cand2 is better than cand1
4801 0: cand1 and cand2 are indistinguishable */
4803 static int
4804 joust (cand1, cand2, warn)
4805 struct z_candidate *cand1, *cand2;
4806 int warn;
4808 int winner = 0;
4809 int i, off1 = 0, off2 = 0, len;
4811 /* Candidates that involve bad conversions are always worse than those
4812 that don't. */
4813 if (cand1->viable > cand2->viable)
4814 return 1;
4815 if (cand1->viable < cand2->viable)
4816 return -1;
4818 /* If we have two pseudo-candidates for conversions to the same type,
4819 arbitrarily pick one. */
4820 if (TYPE_P (cand1->fn) && cand1->fn == cand2->fn)
4821 return 1;
4823 /* a viable function F1
4824 is defined to be a better function than another viable function F2 if
4825 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4826 ICSi(F2), and then */
4828 /* for some argument j, ICSj(F1) is a better conversion sequence than
4829 ICSj(F2) */
4831 /* For comparing static and non-static member functions, we ignore the
4832 implicit object parameter of the non-static function. The WP says to
4833 pretend that the static function has an object parm, but that won't
4834 work with operator overloading. */
4835 len = TREE_VEC_LENGTH (cand1->convs);
4836 if (len != TREE_VEC_LENGTH (cand2->convs))
4838 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4839 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4840 off2 = 1;
4841 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4842 && DECL_STATIC_FUNCTION_P (cand2->fn))
4844 off1 = 1;
4845 --len;
4847 else
4848 my_friendly_abort (42);
4851 for (i = 0; i < len; ++i)
4853 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4854 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4855 int comp = compare_ics (t1, t2);
4857 if (comp != 0)
4859 if (warn_sign_promo
4860 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4861 && TREE_CODE (t1) == STD_CONV
4862 && TREE_CODE (t2) == STD_CONV
4863 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4864 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4865 && (TYPE_PRECISION (TREE_TYPE (t1))
4866 == TYPE_PRECISION (TREE_TYPE (t2)))
4867 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4868 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4869 == ENUMERAL_TYPE)))
4871 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4872 tree type1, type2;
4873 struct z_candidate *w, *l;
4874 if (comp > 0)
4875 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4876 w = cand1, l = cand2;
4877 else
4878 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4879 w = cand2, l = cand1;
4881 if (warn)
4883 cp_warning ("passing `%T' chooses `%T' over `%T'",
4884 type, type1, type2);
4885 cp_warning (" in call to `%D'", w->fn);
4887 else
4888 add_warning (w, l);
4891 if (winner && comp != winner)
4893 winner = 0;
4894 goto tweak;
4896 winner = comp;
4900 /* warn about confusing overload resolution for user-defined conversions,
4901 either between a constructor and a conversion op, or between two
4902 conversion ops. */
4903 if (winner && cand1->second_conv
4904 && ((DECL_CONSTRUCTOR_P (cand1->fn)
4905 != DECL_CONSTRUCTOR_P (cand2->fn))
4906 /* Don't warn if the two conv ops convert to the same type... */
4907 || (! DECL_CONSTRUCTOR_P (cand1->fn)
4908 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
4909 TREE_TYPE (TREE_TYPE (cand2->fn))))))
4911 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4912 if (comp != winner)
4914 struct z_candidate *w, *l;
4915 if (winner == 1)
4916 w = cand1, l = cand2;
4917 else
4918 w = cand2, l = cand1;
4919 if (warn)
4921 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4922 if (! DECL_CONSTRUCTOR_P (w->fn))
4923 source = TREE_TYPE (source);
4924 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4925 cp_warning (" for conversion from `%T' to `%T'",
4926 source, TREE_TYPE (w->second_conv));
4927 cp_warning (" because conversion sequence for the argument is better");
4929 else
4930 add_warning (w, l);
4934 if (winner)
4935 return winner;
4937 /* or, if not that,
4938 F1 is a non-template function and F2 is a template function */
4940 if (! cand1->template && cand2->template)
4941 return 1;
4942 else if (cand1->template && ! cand2->template)
4943 return -1;
4944 else if (cand1->template && cand2->template)
4945 winner = more_specialized
4946 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4947 NULL_TREE);
4949 /* or, if not that,
4950 the context is an initialization by user-defined conversion (see
4951 _dcl.init_ and _over.match.user_) and the standard conversion
4952 sequence from the return type of F1 to the destination type (i.e.,
4953 the type of the entity being initialized) is a better conversion
4954 sequence than the standard conversion sequence from the return type
4955 of F2 to the destination type. */
4957 if (! winner && cand1->second_conv)
4958 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4960 /* If the built-in candidates are the same, arbitrarily pick one. */
4961 if (! winner && cand1->fn == cand2->fn
4962 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4964 for (i = 0; i < len; ++i)
4965 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4966 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
4967 break;
4968 if (i == TREE_VEC_LENGTH (cand1->convs))
4969 return 1;
4971 /* Kludge around broken overloading rules whereby
4972 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4973 that takes references and another that takes values. */
4974 if (cand1->fn == ansi_opname[COND_EXPR])
4976 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4977 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4978 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4979 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4981 if (same_type_p (t1, t2))
4983 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4984 return 1;
4985 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4986 return -1;
4991 /* If the two functions are the same (this can happen with declarations
4992 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
4993 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
4994 && equal_functions (cand1->fn, cand2->fn))
4995 return 1;
4997 tweak:
4999 /* Extension: If the worst conversion for one candidate is worse than the
5000 worst conversion for the other, take the first. */
5001 if (! winner && ! pedantic)
5003 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5005 for (i = 0; i < len; ++i)
5007 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5008 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5009 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5010 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5013 if (rank1 < rank2)
5014 return 1;
5015 if (rank1 > rank2)
5016 return -1;
5019 return winner;
5022 /* Given a list of candidates for overloading, find the best one, if any.
5023 This algorithm has a worst case of O(2n) (winner is last), and a best
5024 case of O(n/2) (totally ambiguous); much better than a sorting
5025 algorithm. */
5027 static struct z_candidate *
5028 tourney (candidates)
5029 struct z_candidate *candidates;
5031 struct z_candidate *champ = candidates, *challenger;
5032 int fate;
5033 int champ_compared_to_predecessor = 0;
5035 /* Walk through the list once, comparing each current champ to the next
5036 candidate, knocking out a candidate or two with each comparison. */
5038 for (challenger = champ->next; challenger; )
5040 fate = joust (champ, challenger, 0);
5041 if (fate == 1)
5042 challenger = challenger->next;
5043 else
5045 if (fate == 0)
5047 champ = challenger->next;
5048 if (champ == 0)
5049 return 0;
5050 champ_compared_to_predecessor = 0;
5052 else
5054 champ = challenger;
5055 champ_compared_to_predecessor = 1;
5058 challenger = champ->next;
5062 /* Make sure the champ is better than all the candidates it hasn't yet
5063 been compared to. */
5065 for (challenger = candidates;
5066 challenger != champ
5067 && !(champ_compared_to_predecessor && challenger->next == champ);
5068 challenger = challenger->next)
5070 fate = joust (champ, challenger, 0);
5071 if (fate != 1)
5072 return 0;
5075 return champ;
5078 /* Returns non-zero if things of type FROM can be converted to TO. */
5081 can_convert (to, from)
5082 tree to, from;
5084 return can_convert_arg (to, from, NULL_TREE);
5087 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5090 can_convert_arg (to, from, arg)
5091 tree to, from, arg;
5093 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5094 return (t && ! ICS_BAD_FLAG (t));
5097 /* Convert EXPR to TYPE. Return the converted expression. */
5099 tree
5100 perform_implicit_conversion (type, expr)
5101 tree type;
5102 tree expr;
5104 tree conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5105 LOOKUP_NORMAL);
5106 if (!conv || ICS_BAD_FLAG (conv))
5108 cp_error ("could not convert `%E' to `%T'", expr, type);
5109 return error_mark_node;
5112 return convert_like (conv, expr);
5115 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5116 initializing a variable of that TYPE. Return the converted
5117 expression. */
5119 tree
5120 initialize_reference (type, expr)
5121 tree type;
5122 tree expr;
5124 tree conv;
5126 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5127 if (!conv || ICS_BAD_FLAG (conv))
5129 cp_error ("could not convert `%E' to `%T'", expr, type);
5130 return error_mark_node;
5133 return convert_like (conv, expr);