* call.c (reference_binding): Tweak.
[official-gcc.git] / gcc / cp / call.c
blob6e75e94dd0b2ea3a51dca746490642710ef0208f
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"
35 #include "obstack.h"
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
39 extern int inhibit_warnings;
40 extern tree ctor_label, dtor_label;
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 joust PROTO((struct z_candidate *, struct z_candidate *, int));
47 static int compare_ics PROTO((tree, tree));
48 static tree build_over_call PROTO((struct z_candidate *, tree, int));
49 static tree convert_like PROTO((tree, tree));
50 static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
51 tree, const char *));
52 static tree build_object_call PROTO((tree, tree));
53 static tree resolve_args PROTO((tree));
54 static struct z_candidate * build_user_type_conversion_1
55 PROTO ((tree, tree, int));
56 static void print_z_candidates PROTO((struct z_candidate *));
57 static tree build_this PROTO((tree));
58 static struct z_candidate * splice_viable PROTO((struct z_candidate *));
59 static int any_viable PROTO((struct z_candidate *));
60 static struct z_candidate * add_template_candidate
61 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
62 unification_kind_t));
63 static struct z_candidate * add_template_candidate_real
64 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
65 tree, unification_kind_t));
66 static struct z_candidate * add_template_conv_candidate
67 PROTO((struct z_candidate *, tree, tree, tree, tree));
68 static struct z_candidate * add_builtin_candidates
69 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
70 tree, tree *, int));
71 static struct z_candidate * add_builtin_candidate
72 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
73 tree, tree, tree, tree *, tree *, int));
74 static int is_complete PROTO((tree));
75 static struct z_candidate * build_builtin_candidate
76 PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
77 int));
78 static struct z_candidate * add_conv_candidate
79 PROTO((struct z_candidate *, tree, tree, tree));
80 static struct z_candidate * add_function_candidate
81 PROTO((struct z_candidate *, tree, tree, int));
82 static tree implicit_conversion PROTO((tree, tree, tree, int));
83 static tree standard_conversion PROTO((tree, tree, tree));
84 static tree reference_binding PROTO((tree, tree, tree, int));
85 static tree strip_top_quals PROTO((tree));
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));
100 tree
101 build_vfield_ref (datum, type)
102 tree datum, type;
104 tree rval;
106 if (datum == error_mark_node)
107 return error_mark_node;
109 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
110 datum = convert_from_reference (datum);
112 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
113 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
114 datum, CLASSTYPE_VFIELD (type));
115 else
116 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
118 return rval;
121 /* Build a call to a member of an object. I.e., one that overloads
122 operator ()(), or is a pointer-to-function or pointer-to-method. */
124 static tree
125 build_field_call (basetype_path, instance_ptr, name, parms)
126 tree basetype_path, instance_ptr, name, parms;
128 tree field, instance;
130 if (name == ctor_identifier || name == dtor_identifier)
131 return NULL_TREE;
133 /* Speed up the common case. */
134 if (instance_ptr == current_class_ptr
135 && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
136 return NULL_TREE;
138 field = lookup_field (basetype_path, name, 1, 0);
140 if (field == error_mark_node || field == NULL_TREE)
141 return field;
143 if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
145 /* If it's a field, try overloading operator (),
146 or calling if the field is a pointer-to-function. */
147 instance = build_indirect_ref (instance_ptr, NULL_PTR);
148 instance = build_component_ref_1 (instance, field, 0);
150 if (instance == error_mark_node)
151 return error_mark_node;
153 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
154 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
155 instance, parms, NULL_TREE);
156 else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
158 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
159 return build_function_call (instance, parms);
160 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
161 == METHOD_TYPE)
162 return build_function_call
163 (instance, expr_tree_cons (NULL_TREE, instance_ptr, parms));
167 return NULL_TREE;
170 /* Returns nonzero iff the destructor name specified in NAME
171 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
172 forms... */
175 check_dtor_name (basetype, name)
176 tree basetype, name;
178 name = TREE_OPERAND (name, 0);
180 /* Just accept something we've already complained about. */
181 if (name == error_mark_node)
182 return 1;
184 if (TREE_CODE (name) == TYPE_DECL)
185 name = TREE_TYPE (name);
186 else if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
187 /* OK */;
188 else if (TREE_CODE (name) == IDENTIFIER_NODE)
190 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
191 || (TREE_CODE (basetype) == ENUMERAL_TYPE
192 && name == TYPE_IDENTIFIER (basetype)))
193 name = basetype;
194 else
195 name = get_type_value (name);
197 else
198 my_friendly_abort (980605);
200 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
201 return 1;
202 return 0;
205 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
206 This is how virtual function calls are avoided. */
208 tree
209 build_scoped_method_call (exp, basetype, name, parms)
210 tree exp, basetype, name, parms;
212 /* Because this syntactic form does not allow
213 a pointer to a base class to be `stolen',
214 we need not protect the derived->base conversion
215 that happens here.
217 @@ But we do have to check access privileges later. */
218 tree binfo, decl;
219 tree type = TREE_TYPE (exp);
221 if (type == error_mark_node
222 || basetype == error_mark_node)
223 return error_mark_node;
225 if (processing_template_decl)
227 if (TREE_CODE (name) == BIT_NOT_EXPR
228 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
230 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
231 if (type)
232 name = build_min_nt (BIT_NOT_EXPR, type);
234 name = build_min_nt (SCOPE_REF, basetype, name);
235 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
238 if (TREE_CODE (type) == REFERENCE_TYPE)
239 type = TREE_TYPE (type);
241 if (TREE_CODE (basetype) == TREE_VEC)
243 binfo = basetype;
244 basetype = BINFO_TYPE (binfo);
246 else
247 binfo = NULL_TREE;
249 /* Check the destructor call syntax. */
250 if (TREE_CODE (name) == BIT_NOT_EXPR)
252 /* We can get here if someone writes their destructor call like
253 `obj.NS::~T()'; this isn't really a scoped method call, so hand
254 it off. */
255 if (TREE_CODE (basetype) == NAMESPACE_DECL)
256 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
258 if (! check_dtor_name (basetype, name))
259 cp_error ("qualified type `%T' does not match destructor name `~%T'",
260 basetype, TREE_OPERAND (name, 0));
262 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
263 that explicit ~int is caught in the parser; this deals with typedefs
264 and template parms. */
265 if (! IS_AGGR_TYPE (basetype))
267 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
268 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
269 exp, basetype, type);
271 return cp_convert (void_type_node, exp);
275 if (! is_aggr_type (basetype, 1))
276 return error_mark_node;
278 if (! IS_AGGR_TYPE (type))
280 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
281 exp, type);
282 return error_mark_node;
285 if (! binfo)
287 binfo = get_binfo (basetype, type, 1);
288 if (binfo == error_mark_node)
289 return error_mark_node;
290 if (! binfo)
291 error_not_base_type (basetype, type);
294 if (binfo)
296 if (TREE_CODE (exp) == INDIRECT_REF)
297 decl = build_indirect_ref
298 (convert_pointer_to_real
299 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
300 else
301 decl = build_scoped_ref (exp, basetype);
303 /* Call to a destructor. */
304 if (TREE_CODE (name) == BIT_NOT_EXPR)
306 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
307 return cp_convert (void_type_node, exp);
309 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
310 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
314 /* Call to a method. */
315 return build_method_call (decl, name, parms, binfo,
316 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
318 return error_mark_node;
321 /* We want the address of a function or method. We avoid creating a
322 pointer-to-member function. */
324 tree
325 build_addr_func (function)
326 tree function;
328 tree type = TREE_TYPE (function);
330 /* We have to do these by hand to avoid real pointer to member
331 functions. */
332 if (TREE_CODE (type) == METHOD_TYPE)
334 tree addr;
336 type = build_pointer_type (type);
338 if (mark_addressable (function) == 0)
339 return error_mark_node;
341 addr = build1 (ADDR_EXPR, type, function);
343 /* Address of a static or external variable or function counts
344 as a constant */
345 if (staticp (function))
346 TREE_CONSTANT (addr) = 1;
348 function = addr;
350 else
351 function = default_conversion (function);
353 return function;
356 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
357 POINTER_TYPE to those. Note, pointer to member function types
358 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
360 tree
361 build_call (function, result_type, parms)
362 tree function, result_type, parms;
364 int is_constructor = 0;
365 tree tmp;
366 tree decl;
368 function = build_addr_func (function);
370 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
372 sorry ("unable to call pointer to member function here");
373 return error_mark_node;
376 if (TREE_CODE (function) == ADDR_EXPR
377 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
378 decl = TREE_OPERAND (function, 0);
379 else
380 decl = NULL_TREE;
382 if (decl && DECL_CONSTRUCTOR_P (decl))
383 is_constructor = 1;
385 if (decl)
386 my_friendly_assert (TREE_USED (decl), 990125);
388 /* Don't pass empty class objects by value. This is useful
389 for tags in STL, which are used to control overload resolution.
390 We don't need to handle other cases of copying empty classes. */
391 if (! decl || ! DECL_BUILT_IN (decl))
392 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
393 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
394 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
396 tree t = make_node (RTL_EXPR);
397 TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
398 RTL_EXPR_RTL (t) = const0_rtx;
399 RTL_EXPR_SEQUENCE (t) = NULL_RTX;
400 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
401 TREE_VALUE (tmp), t);
404 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
405 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
406 TREE_TYPE (function) = result_type;
407 TREE_SIDE_EFFECTS (function) = 1;
409 return function;
412 /* Build something of the form ptr->method (args)
413 or object.method (args). This can also build
414 calls to constructors, and find friends.
416 Member functions always take their class variable
417 as a pointer.
419 INSTANCE is a class instance.
421 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
423 PARMS help to figure out what that NAME really refers to.
425 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
426 down to the real instance type to use for access checking. We need this
427 information to get protected accesses correct. This parameter is used
428 by build_member_call.
430 FLAGS is the logical disjunction of zero or more LOOKUP_
431 flags. See cp-tree.h for more info.
433 If this is all OK, calls build_function_call with the resolved
434 member function.
436 This function must also handle being called to perform
437 initialization, promotion/coercion of arguments, and
438 instantiation of default parameters.
440 Note that NAME may refer to an instance variable name. If
441 `operator()()' is defined for the type of that field, then we return
442 that result. */
444 tree
445 build_method_call (instance, name, parms, basetype_path, flags)
446 tree instance, name, parms, basetype_path;
447 int flags;
449 tree basetype, instance_ptr;
451 #ifdef GATHER_STATISTICS
452 n_build_method_call++;
453 #endif
455 if (instance == error_mark_node
456 || name == error_mark_node
457 || parms == error_mark_node
458 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
459 return error_mark_node;
461 if (processing_template_decl)
463 /* We need to process template parm names here so that tsubst catches
464 them properly. Other type names can wait. */
465 if (TREE_CODE (name) == BIT_NOT_EXPR)
467 tree type = NULL_TREE;
469 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
470 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
471 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
472 type = TREE_TYPE (TREE_OPERAND (name, 0));
474 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
475 name = build_min_nt (BIT_NOT_EXPR, type);
478 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
481 if (TREE_CODE (name) == BIT_NOT_EXPR)
483 if (parms)
484 error ("destructors take no parameters");
485 basetype = TREE_TYPE (instance);
486 if (TREE_CODE (basetype) == REFERENCE_TYPE)
487 basetype = TREE_TYPE (basetype);
489 if (! check_dtor_name (basetype, name))
490 cp_error
491 ("destructor name `~%T' does not match type `%T' of expression",
492 TREE_OPERAND (name, 0), basetype);
494 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
495 return cp_convert (void_type_node, instance);
496 instance = default_conversion (instance);
497 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
498 return build_delete (build_pointer_type (basetype),
499 instance_ptr, integer_two_node,
500 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
503 return build_new_method_call (instance, name, parms, basetype_path, flags);
506 /* New overloading code. */
508 struct z_candidate {
509 tree fn;
510 tree convs;
511 tree second_conv;
512 int viable;
513 tree basetype_path;
514 tree template;
515 tree warnings;
516 struct z_candidate *next;
519 #define IDENTITY_RANK 0
520 #define EXACT_RANK 1
521 #define PROMO_RANK 2
522 #define STD_RANK 3
523 #define PBOOL_RANK 4
524 #define USER_RANK 5
525 #define ELLIPSIS_RANK 6
526 #define BAD_RANK 7
528 #define ICS_RANK(NODE) \
529 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
530 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
531 : ICS_USER_FLAG (NODE) ? USER_RANK \
532 : ICS_STD_RANK (NODE))
534 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
536 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
537 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
538 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
539 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
541 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
542 should be created to hold the result of the conversion. */
543 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
545 #define USER_CONV_CAND(NODE) \
546 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
547 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
550 null_ptr_cst_p (t)
551 tree t;
553 if (t == null_node
554 || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE))
555 return 1;
556 return 0;
559 static tree
560 build_conv (code, type, from)
561 enum tree_code code;
562 tree type, from;
564 tree t = build1 (code, type, from);
565 int rank = ICS_STD_RANK (from);
566 switch (code)
568 case PTR_CONV:
569 case PMEM_CONV:
570 case BASE_CONV:
571 case STD_CONV:
572 if (rank < STD_RANK)
573 rank = STD_RANK;
574 break;
576 case QUAL_CONV:
577 if (rank < EXACT_RANK)
578 rank = EXACT_RANK;
580 default:
581 break;
583 ICS_STD_RANK (t) = rank;
584 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
585 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
586 return t;
589 static tree
590 non_reference (t)
591 tree t;
593 if (TREE_CODE (t) == REFERENCE_TYPE)
594 t = TREE_TYPE (t);
595 return t;
598 static tree
599 strip_top_quals (t)
600 tree t;
602 if (TREE_CODE (t) == ARRAY_TYPE)
603 return t;
604 return TYPE_MAIN_VARIANT (t);
607 /* Returns the standard conversion path (see [conv]) from type FROM to type
608 TO, if any. For proper handling of null pointer constants, you must
609 also pass the expression EXPR to convert from. */
611 static tree
612 standard_conversion (to, from, expr)
613 tree to, from, expr;
615 enum tree_code fcode, tcode;
616 tree conv;
617 int fromref = 0;
619 if (TREE_CODE (to) == REFERENCE_TYPE)
620 to = TREE_TYPE (to);
621 if (TREE_CODE (from) == REFERENCE_TYPE)
623 fromref = 1;
624 from = TREE_TYPE (from);
626 to = strip_top_quals (to);
627 from = strip_top_quals (from);
629 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
630 && expr && type_unknown_p (expr))
632 expr = instantiate_type (to, expr, 0);
633 if (expr == error_mark_node)
634 return NULL_TREE;
635 from = TREE_TYPE (expr);
638 fcode = TREE_CODE (from);
639 tcode = TREE_CODE (to);
641 conv = build1 (IDENTITY_CONV, from, expr);
643 if (fcode == FUNCTION_TYPE)
645 from = build_pointer_type (from);
646 fcode = TREE_CODE (from);
647 conv = build_conv (LVALUE_CONV, from, conv);
649 else if (fcode == ARRAY_TYPE)
651 from = build_pointer_type (TREE_TYPE (from));
652 fcode = TREE_CODE (from);
653 conv = build_conv (LVALUE_CONV, from, conv);
655 else if (fromref || (expr && real_lvalue_p (expr)))
656 conv = build_conv (RVALUE_CONV, from, conv);
658 if (from == to)
659 return conv;
661 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
662 && expr && null_ptr_cst_p (expr))
664 conv = build_conv (STD_CONV, to, conv);
666 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
668 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
669 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
671 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
672 TYPE_MAIN_VARIANT (TREE_TYPE (to))))
674 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
675 && ufcode != FUNCTION_TYPE)
677 from = build_pointer_type
678 (cp_build_qualified_type (void_type_node,
679 CP_TYPE_QUALS (TREE_TYPE (from))));
680 conv = build_conv (PTR_CONV, from, conv);
682 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
684 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
685 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
687 if (DERIVED_FROM_P (fbase, tbase)
688 && (same_type_p
689 (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
690 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))))))
692 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
693 from = build_pointer_type (from);
694 conv = build_conv (PMEM_CONV, from, conv);
697 else if (IS_AGGR_TYPE (TREE_TYPE (from))
698 && IS_AGGR_TYPE (TREE_TYPE (to)))
700 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
702 from =
703 cp_build_qualified_type (TREE_TYPE (to),
704 CP_TYPE_QUALS (TREE_TYPE (from)));
705 from = build_pointer_type (from);
706 conv = build_conv (PTR_CONV, from, conv);
710 if (same_type_p (from, to))
711 /* OK */;
712 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
713 conv = build_conv (QUAL_CONV, to, conv);
714 else if (expr && string_conv_p (to, expr, 0))
715 /* converting from string constant to char *. */
716 conv = build_conv (QUAL_CONV, to, conv);
717 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
719 conv = build_conv (PTR_CONV, to, conv);
720 ICS_BAD_FLAG (conv) = 1;
722 else
723 return 0;
725 from = to;
727 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
729 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
730 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
731 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
732 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
734 if (! DERIVED_FROM_P (fbase, tbase)
735 || ! same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
736 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
737 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
738 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
739 return 0;
741 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
742 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
743 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
744 from = build_ptrmemfunc_type (build_pointer_type (from));
745 conv = build_conv (PMEM_CONV, from, conv);
747 else if (tcode == BOOLEAN_TYPE)
749 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
750 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
751 return 0;
753 conv = build_conv (STD_CONV, to, conv);
754 if (fcode == POINTER_TYPE
755 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
756 ICS_STD_RANK (conv) = PBOOL_RANK;
758 /* We don't check for ENUMERAL_TYPE here because there are no standard
759 conversions to enum type. */
760 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
761 || tcode == REAL_TYPE)
763 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
764 return 0;
765 conv = build_conv (STD_CONV, to, conv);
767 /* Give this a better rank if it's a promotion. */
768 if (to == type_promotes_to (from)
769 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
770 ICS_STD_RANK (conv) = PROMO_RANK;
772 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
773 && DERIVED_FROM_P (to, from))
775 if (TREE_CODE (conv) == RVALUE_CONV)
776 conv = TREE_OPERAND (conv, 0);
777 conv = build_conv (BASE_CONV, to, conv);
778 /* The derived-to-base conversion indicates the initialization
779 of a parameter with base type from an object of a derived
780 type. A temporary object is created to hold the result of
781 the conversion. */
782 NEED_TEMPORARY_P (conv) = 1;
784 else
785 return 0;
787 return conv;
790 /* Returns non-zero if T1 is reference-related to T2. */
792 static int
793 reference_related_p (t1, t2)
794 tree t1;
795 tree t2;
797 t1 = TYPE_MAIN_VARIANT (t1);
798 t2 = TYPE_MAIN_VARIANT (t2);
800 /* [dcl.init.ref]
802 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
803 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
804 of T2. */
805 return (same_type_p (t1, t2)
806 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
807 && DERIVED_FROM_P (t1, t2)));
810 /* Returns non-zero if T1 is reference-compatible with T2. */
812 static int
813 reference_compatible_p (t1, t2)
814 tree t1;
815 tree t2;
817 /* [dcl.init.ref]
819 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
820 reference-related to T2 and cv1 is the same cv-qualification as,
821 or greater cv-qualification than, cv2. */
822 return (reference_related_p (t1, t2)
823 && at_least_as_qualified_p (t1, t2));
826 /* Determine whether or not the EXPR (of class type S) can be
827 converted to T as in [over.match.ref]. */
829 static tree
830 convert_class_to_reference (t, s, expr)
831 tree t;
832 tree s;
833 tree expr;
835 tree conversions;
836 tree arglist;
837 tree conv;
838 struct z_candidate *candidates;
839 struct z_candidate *cand;
841 /* [over.match.ref]
843 Assuming that "cv1 T" is the underlying type of the reference
844 being initialized, and "cv S" is the type of the initializer
845 expression, with S a class type, the candidate functions are
846 selected as follows:
848 --The conversion functions of S and its base classes are
849 considered. Those that are not hidden within S and yield type
850 "reference to cv2 T2", where "cv1 T" is reference-compatible
851 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
853 The argument list has one argument, which is the initializer
854 expression. */
856 candidates = 0;
858 /* Conceptually, we should take the address of EXPR and put it in
859 the argument list. Unfortunately, however, that can result in
860 error messages, which we should not issue now because we are just
861 trying to find a conversion operator. Therefore, we use NULL,
862 cast to the appropriate type. */
863 arglist = build_int_2 (0, 0);
864 TREE_TYPE (arglist) = build_pointer_type (s);
865 arglist = build_scratch_list (NULL_TREE, arglist);
867 for (conversions = lookup_conversions (s);
868 conversions;
869 conversions = TREE_CHAIN (conversions))
871 tree fns = TREE_VALUE (conversions);
873 while (fns)
875 tree f = OVL_CURRENT (fns);
876 tree t2 = TREE_TYPE (TREE_TYPE (f));
877 struct z_candidate *old_candidates = candidates;
879 /* If this is a template function, try to get an exact
880 match. */
881 if (TREE_CODE (f) == TEMPLATE_DECL)
883 candidates
884 = add_template_candidate (candidates,
886 NULL_TREE,
887 arglist,
888 build_reference_type (t),
889 LOOKUP_NORMAL,
890 DEDUCE_CONV);
892 if (candidates != old_candidates)
894 /* Now, see if the conversion function really returns
895 an lvalue of the appropriate type. From the
896 point of view of unification, simply returning an
897 rvalue of the right type is good enough. */
898 f = candidates->fn;
899 t2 = TREE_TYPE (TREE_TYPE (f));
900 if (TREE_CODE (t2) != REFERENCE_TYPE
901 || !reference_compatible_p (t, TREE_TYPE (t2)))
902 candidates = candidates->next;
905 else if (TREE_CODE (t2) == REFERENCE_TYPE
906 && reference_compatible_p (t, TREE_TYPE (t2)))
907 candidates
908 = add_function_candidate (candidates, f, arglist,
909 LOOKUP_NORMAL);
911 if (candidates != old_candidates)
912 candidates->basetype_path = TREE_PURPOSE (conversions);
914 fns = OVL_NEXT (fns);
918 /* If none of the conversion functions worked out, let our caller
919 know. */
920 if (!any_viable (candidates))
921 return NULL_TREE;
923 candidates = splice_viable (candidates);
924 cand = tourney (candidates);
925 if (!cand)
926 return NULL_TREE;
928 conv = build_conv (IDENTITY_CONV, s, expr);
929 conv = build_conv (USER_CONV,
930 non_reference (TREE_TYPE (TREE_TYPE (cand->fn))),
931 expr);
932 TREE_OPERAND (conv, 1) = build_expr_ptr_wrapper (cand);
933 ICS_USER_FLAG (conv) = 1;
934 if (cand->viable == -1)
935 ICS_BAD_FLAG (conv) = 1;
936 cand->second_conv = conv;
938 return conv;
941 /* A reference of the indicated TYPE is being bound directly to the
942 expression represented by the implicit conversion sequence CONV.
943 Return a conversion sequence for this binding. */
945 static tree
946 direct_reference_binding (type, conv)
947 tree type;
948 tree conv;
950 tree t = TREE_TYPE (type);
952 /* [over.ics.rank]
954 When a parameter of reference type binds directly
955 (_dcl.init.ref_) to an argument expression, the implicit
956 conversion sequence is the identity conversion, unless the
957 argument expression has a type that is a derived class of the
958 parameter type, in which case the implicit conversion sequence is
959 a derived-to-base Conversion.
961 If the parameter binds directly to the result of applying a
962 conversion function to the argument expression, the implicit
963 conversion sequence is a user-defined conversion sequence
964 (_over.ics.user_), with the second standard conversion sequence
965 either an identity conversion or, if the conversion function
966 returns an entity of a type that is a derived class of the
967 parameter type, a derived-to-base conversion. */
968 if (!same_type_p (TYPE_MAIN_VARIANT (t),
969 TYPE_MAIN_VARIANT (TREE_TYPE (conv))))
971 /* Represent the derived-to-base conversion. */
972 conv = build_conv (BASE_CONV, t, conv);
973 /* We will actually be binding to the base-class subobject in
974 the derived class, so we mark this conversion appropriately.
975 That way, convert_like knows not to generate a temporary. */
976 NEED_TEMPORARY_P (conv) = 0;
978 return build_conv (REF_BIND, type, conv);
981 /* Returns the conversion path from type FROM to reference type TO for
982 purposes of reference binding. For lvalue binding, either pass a
983 reference type to FROM or an lvalue expression to EXPR.
985 Currently does not distinguish in the generated trees between binding to
986 an lvalue and a temporary. Should it? */
988 static tree
989 reference_binding (rto, rfrom, expr, flags)
990 tree rto, rfrom, expr;
991 int flags;
993 tree conv = NULL_TREE;
994 tree to = TREE_TYPE (rto);
995 tree from = rfrom;
996 int related_p;
997 int compatible_p;
998 cp_lvalue_kind lvalue_p = clk_none;
1000 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1002 expr = instantiate_type (to, expr, 0);
1003 if (expr == error_mark_node)
1004 return NULL_TREE;
1005 from = TREE_TYPE (expr);
1008 if (TREE_CODE (from) == REFERENCE_TYPE)
1010 /* Anything with reference type is an lvalue. */
1011 lvalue_p = clk_ordinary;
1012 from = TREE_TYPE (from);
1014 else if (expr)
1015 lvalue_p = real_lvalue_p (expr);
1017 /* Figure out whether or not the types are reference-related and
1018 reference compatible. We have do do this after stripping
1019 references from FROM. */
1020 related_p = reference_related_p (to, from);
1021 compatible_p = reference_compatible_p (to, from);
1023 if (lvalue_p && compatible_p)
1025 /* [dcl.init.ref]
1027 If the intializer expression
1029 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1030 is reference-compatible with "cv2 T2,"
1032 the reference is bound directly to the initializer exprssion
1033 lvalue. */
1034 conv = build1 (IDENTITY_CONV, from, expr);
1035 conv = direct_reference_binding (rto, conv);
1036 if ((lvalue_p & clk_bitfield) != 0
1037 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1038 /* For the purposes of overload resolution, we ignore the fact
1039 this expression is a bitfield. (In particular,
1040 [over.ics.ref] says specifically that a function with a
1041 non-const reference parameter is viable even if the
1042 argument is a bitfield.)
1044 However, when we actually call the function we must create
1045 a temporary to which to bind the reference. If the
1046 reference is volatile, or isn't const, then we cannot make
1047 a temporary, so we just issue an error when the conversion
1048 actually occurs. */
1049 NEED_TEMPORARY_P (conv) = 1;
1050 return conv;
1052 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1054 /* [dcl.init.ref]
1056 If the initializer exprsesion
1058 -- has a class type (i.e., T2 is a class type) can be
1059 implicitly converted to an lvalue of type "cv3 T3," where
1060 "cv1 T1" is reference-compatible with "cv3 T3". (this
1061 conversion is selected by enumerating the applicable
1062 conversion functions (_over.match.ref_) and choosing the
1063 best one through overload resolution. (_over.match_).
1065 the reference is bound to the lvalue result of the conversion
1066 in the second case. */
1067 conv = convert_class_to_reference (to, from, expr);
1068 if (conv)
1069 return direct_reference_binding (rto, conv);
1072 /* [over.ics.rank]
1074 When a parameter of reference type is not bound directly to an
1075 argument expression, the conversion sequence is the one required
1076 to convert the argument expression to the underlying type of the
1077 reference according to _over.best.ics_. Conceptually, this
1078 conversion sequence corresponds to copy-initializing a temporary
1079 of the underlying type with the argument expression. Any
1080 difference in top-level cv-qualification is subsumed by the
1081 initialization itself and does not constitute a conversion. */
1083 /* [dcl.init.ref]
1085 Otherwise, the reference shall be to a non-volatile const type. */
1086 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1087 return NULL_TREE;
1089 /* [dcl.init.ref]
1091 If the initializer expression is an rvalue, with T2 a class type,
1092 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1093 is bound in one of the following ways:
1095 -- The reference is bound to the object represented by the rvalue
1096 or to a sub-object within that object.
1098 In this case, the implicit conversion sequence is supposed to be
1099 same as we would obtain by generating a temporary. Fortunately,
1100 if the types are reference compatible, then this is either an
1101 identity conversion or the derived-to-base conversion, just as
1102 for direct binding. */
1103 if (CLASS_TYPE_P (from) && compatible_p)
1105 conv = build1 (IDENTITY_CONV, from, expr);
1106 return direct_reference_binding (rto, conv);
1109 /* [dcl.init.ref]
1111 Otherwise, a temporary of type "cv1 T1" is created and
1112 initialized from the initializer expression using the rules for a
1113 non-reference copy initialization. If T1 is reference-related to
1114 T2, cv1 must be the same cv-qualification as, or greater
1115 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1116 if (related_p && !at_least_as_qualified_p (to, from))
1117 return NULL_TREE;
1119 conv = implicit_conversion (to, from, expr, flags);
1120 if (!conv)
1121 return NULL_TREE;
1123 conv = build_conv (REF_BIND, rto, conv);
1124 /* This reference binding, unlike those above, requires the
1125 creation of a temporary. */
1126 NEED_TEMPORARY_P (conv) = 1;
1128 return conv;
1131 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1132 to type TO. The optional expression EXPR may affect the conversion.
1133 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1134 significant. */
1136 static tree
1137 implicit_conversion (to, from, expr, flags)
1138 tree to, from, expr;
1139 int flags;
1141 tree conv;
1142 struct z_candidate *cand;
1144 if (TREE_CODE (to) == REFERENCE_TYPE)
1145 conv = reference_binding (to, from, expr, flags);
1146 else
1147 conv = standard_conversion (to, from, expr);
1149 if (conv)
1151 else if (expr != NULL_TREE
1152 && (IS_AGGR_TYPE (non_reference (from))
1153 || IS_AGGR_TYPE (non_reference (to)))
1154 && (flags & LOOKUP_NO_CONVERSION) == 0)
1156 cand = build_user_type_conversion_1
1157 (to, expr, LOOKUP_ONLYCONVERTING);
1158 if (cand)
1159 conv = cand->second_conv;
1160 if ((! conv || ICS_BAD_FLAG (conv))
1161 && TREE_CODE (to) == REFERENCE_TYPE
1162 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1164 cand = build_user_type_conversion_1
1165 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1166 if (cand)
1168 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to)))
1169 ICS_BAD_FLAG (cand->second_conv) = 1;
1170 if (!conv || (ICS_BAD_FLAG (conv)
1171 > ICS_BAD_FLAG (cand->second_conv)))
1172 conv = build_conv (REF_BIND, to, cand->second_conv);
1177 return conv;
1180 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1181 functions. */
1183 static struct z_candidate *
1184 add_candidate (candidates, fn, convs, viable)
1185 struct z_candidate *candidates;
1186 tree fn, convs;
1187 int viable;
1189 struct z_candidate *cand
1190 = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
1192 cand->fn = fn;
1193 cand->convs = convs;
1194 cand->second_conv = NULL_TREE;
1195 cand->viable = viable;
1196 cand->basetype_path = NULL_TREE;
1197 cand->template = NULL_TREE;
1198 cand->warnings = NULL_TREE;
1199 cand->next = candidates;
1201 return cand;
1204 /* Create an overload candidate for the function or method FN called with
1205 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1206 to implicit_conversion. */
1208 static struct z_candidate *
1209 add_function_candidate (candidates, fn, arglist, flags)
1210 struct z_candidate *candidates;
1211 tree fn, arglist;
1212 int flags;
1214 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1215 int i, len;
1216 tree convs;
1217 tree parmnode, argnode;
1218 int viable = 1;
1220 /* The `this' and `in_chrg' arguments to constructors are not considered
1221 in overload resolution. */
1222 if (DECL_CONSTRUCTOR_P (fn))
1224 parmlist = TREE_CHAIN (parmlist);
1225 arglist = TREE_CHAIN (arglist);
1226 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1228 parmlist = TREE_CHAIN (parmlist);
1229 arglist = TREE_CHAIN (arglist);
1233 len = list_length (arglist);
1234 convs = make_scratch_vec (len);
1236 /* 13.3.2 - Viable functions [over.match.viable]
1237 First, to be a viable function, a candidate function shall have enough
1238 parameters to agree in number with the arguments in the list.
1240 We need to check this first; otherwise, checking the ICSes might cause
1241 us to produce an ill-formed template instantiation. */
1243 parmnode = parmlist;
1244 for (i = 0; i < len; ++i)
1246 if (parmnode == NULL_TREE || parmnode == void_list_node)
1247 break;
1248 parmnode = TREE_CHAIN (parmnode);
1251 if (i < len && parmnode)
1252 viable = 0;
1254 /* Make sure there are default args for the rest of the parms. */
1255 else for (; parmnode && parmnode != void_list_node;
1256 parmnode = TREE_CHAIN (parmnode))
1257 if (! TREE_PURPOSE (parmnode))
1259 viable = 0;
1260 break;
1263 if (! viable)
1264 goto out;
1266 /* Second, for F to be a viable function, there shall exist for each
1267 argument an implicit conversion sequence that converts that argument
1268 to the corresponding parameter of F. */
1270 parmnode = parmlist;
1271 argnode = arglist;
1273 for (i = 0; i < len; ++i)
1275 tree arg = TREE_VALUE (argnode);
1276 tree argtype = lvalue_type (arg);
1277 tree t;
1279 if (parmnode == void_list_node)
1280 break;
1282 if (parmnode)
1284 tree parmtype = TREE_VALUE (parmnode);
1286 /* [over.match.funcs] For conversion functions, the function is
1287 considered to be a member of the class of the implicit object
1288 argument for the purpose of defining the type of the implicit
1289 object parameter.
1291 Since build_over_call ignores the ICS for the `this' parameter,
1292 we can just change the parm type. */
1293 if (DECL_CONV_FN_P (fn) && i == 0)
1295 parmtype
1296 = build_qualified_type (TREE_TYPE (argtype),
1297 TYPE_QUALS (TREE_TYPE (parmtype)));
1298 parmtype = build_pointer_type (parmtype);
1301 t = implicit_conversion (parmtype, argtype, arg, flags);
1303 else
1305 t = build1 (IDENTITY_CONV, argtype, arg);
1306 ICS_ELLIPSIS_FLAG (t) = 1;
1309 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1310 && ! DECL_CONSTRUCTOR_P (fn))
1311 ICS_THIS_FLAG (t) = 1;
1313 TREE_VEC_ELT (convs, i) = t;
1314 if (! t)
1316 viable = 0;
1317 break;
1320 if (ICS_BAD_FLAG (t))
1321 viable = -1;
1323 if (parmnode)
1324 parmnode = TREE_CHAIN (parmnode);
1325 argnode = TREE_CHAIN (argnode);
1328 out:
1329 return add_candidate (candidates, fn, convs, viable);
1332 /* Create an overload candidate for the conversion function FN which will
1333 be invoked for expression OBJ, producing a pointer-to-function which
1334 will in turn be called with the argument list ARGLIST, and add it to
1335 CANDIDATES. FLAGS is passed on to implicit_conversion.
1337 Actually, we don't really care about FN; we care about the type it
1338 converts to. There may be multiple conversion functions that will
1339 convert to that type, and we rely on build_user_type_conversion_1 to
1340 choose the best one; so when we create our candidate, we record the type
1341 instead of the function. */
1343 static struct z_candidate *
1344 add_conv_candidate (candidates, fn, obj, arglist)
1345 struct z_candidate *candidates;
1346 tree fn, obj, arglist;
1348 tree totype = TREE_TYPE (TREE_TYPE (fn));
1349 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1350 int i, len = list_length (arglist) + 1;
1351 tree convs = make_scratch_vec (len);
1352 tree parmnode = parmlist;
1353 tree argnode = arglist;
1354 int viable = 1;
1355 int flags = LOOKUP_NORMAL;
1357 /* Don't bother looking up the same type twice. */
1358 if (candidates && candidates->fn == totype)
1359 return candidates;
1361 for (i = 0; i < len; ++i)
1363 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1364 tree argtype = lvalue_type (arg);
1365 tree t;
1367 if (i == 0)
1368 t = implicit_conversion (totype, argtype, arg, flags);
1369 else if (parmnode == void_list_node)
1370 break;
1371 else if (parmnode)
1372 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1373 else
1375 t = build1 (IDENTITY_CONV, argtype, arg);
1376 ICS_ELLIPSIS_FLAG (t) = 1;
1379 TREE_VEC_ELT (convs, i) = t;
1380 if (! t)
1381 break;
1383 if (ICS_BAD_FLAG (t))
1384 viable = -1;
1386 if (i == 0)
1387 continue;
1389 if (parmnode)
1390 parmnode = TREE_CHAIN (parmnode);
1391 argnode = TREE_CHAIN (argnode);
1394 if (i < len)
1395 viable = 0;
1397 for (; parmnode && parmnode != void_list_node;
1398 parmnode = TREE_CHAIN (parmnode))
1399 if (! TREE_PURPOSE (parmnode))
1401 viable = 0;
1402 break;
1405 return add_candidate (candidates, totype, convs, viable);
1408 static struct z_candidate *
1409 build_builtin_candidate (candidates, fnname, type1, type2,
1410 args, argtypes, flags)
1411 struct z_candidate *candidates;
1412 tree fnname, type1, type2, *args, *argtypes;
1413 int flags;
1416 tree t, convs;
1417 int viable = 1, i;
1418 tree types[2];
1420 types[0] = type1;
1421 types[1] = type2;
1423 convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1425 for (i = 0; i < 2; ++i)
1427 if (! args[i])
1428 break;
1430 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1431 if (! t)
1433 viable = 0;
1434 /* We need something for printing the candidate. */
1435 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1437 else if (ICS_BAD_FLAG (t))
1438 viable = 0;
1439 TREE_VEC_ELT (convs, i) = t;
1442 /* For COND_EXPR we rearranged the arguments; undo that now. */
1443 if (args[2])
1445 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1446 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1447 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1448 if (t)
1449 TREE_VEC_ELT (convs, 0) = t;
1450 else
1451 viable = 0;
1454 return add_candidate (candidates, fnname, convs, viable);
1457 static int
1458 is_complete (t)
1459 tree t;
1461 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1464 /* Create any builtin operator overload candidates for the operator in
1465 question given the converted operand types TYPE1 and TYPE2. The other
1466 args are passed through from add_builtin_candidates to
1467 build_builtin_candidate. */
1469 static struct z_candidate *
1470 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1471 args, argtypes, flags)
1472 struct z_candidate *candidates;
1473 enum tree_code code, code2;
1474 tree fnname, type1, type2, *args, *argtypes;
1475 int flags;
1477 switch (code)
1479 case POSTINCREMENT_EXPR:
1480 case POSTDECREMENT_EXPR:
1481 args[1] = integer_zero_node;
1482 type2 = integer_type_node;
1483 break;
1484 default:
1485 break;
1488 switch (code)
1491 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1492 and VQ is either volatile or empty, there exist candidate operator
1493 functions of the form
1494 VQ T& operator++(VQ T&);
1495 T operator++(VQ T&, int);
1496 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1497 type other than bool, and VQ is either volatile or empty, there exist
1498 candidate operator functions of the form
1499 VQ T& operator--(VQ T&);
1500 T operator--(VQ T&, int);
1501 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1502 complete object type, and VQ is either volatile or empty, there exist
1503 candidate operator functions of the form
1504 T*VQ& operator++(T*VQ&);
1505 T*VQ& operator--(T*VQ&);
1506 T* operator++(T*VQ&, int);
1507 T* operator--(T*VQ&, int); */
1509 case POSTDECREMENT_EXPR:
1510 case PREDECREMENT_EXPR:
1511 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1512 return candidates;
1513 case POSTINCREMENT_EXPR:
1514 case PREINCREMENT_EXPR:
1515 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1516 || TYPE_PTROB_P (type1))
1518 type1 = build_reference_type (type1);
1519 break;
1521 return candidates;
1523 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1524 exist candidate operator functions of the form
1526 T& operator*(T*);
1528 8 For every function type T, there exist candidate operator functions of
1529 the form
1530 T& operator*(T*); */
1532 case INDIRECT_REF:
1533 if (TREE_CODE (type1) == POINTER_TYPE
1534 && (TYPE_PTROB_P (type1)
1535 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1536 break;
1537 return candidates;
1539 /* 9 For every type T, there exist candidate operator functions of the form
1540 T* operator+(T*);
1542 10For every promoted arithmetic type T, there exist candidate operator
1543 functions of the form
1544 T operator+(T);
1545 T operator-(T); */
1547 case CONVERT_EXPR: /* unary + */
1548 if (TREE_CODE (type1) == POINTER_TYPE
1549 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1550 break;
1551 case NEGATE_EXPR:
1552 if (ARITHMETIC_TYPE_P (type1))
1553 break;
1554 return candidates;
1556 /* 11For every promoted integral type T, there exist candidate operator
1557 functions of the form
1558 T operator~(T); */
1560 case BIT_NOT_EXPR:
1561 if (INTEGRAL_TYPE_P (type1))
1562 break;
1563 return candidates;
1565 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1566 is the same type as C2 or is a derived class of C2, T is a complete
1567 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1568 there exist candidate operator functions of the form
1569 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1570 where CV12 is the union of CV1 and CV2. */
1572 case MEMBER_REF:
1573 if (TREE_CODE (type1) == POINTER_TYPE
1574 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1576 tree c1 = TREE_TYPE (type1);
1577 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1578 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1579 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1581 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1582 && (TYPE_PTRMEMFUNC_P (type2)
1583 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1584 break;
1586 return candidates;
1588 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1589 didate operator functions of the form
1590 LR operator*(L, R);
1591 LR operator/(L, R);
1592 LR operator+(L, R);
1593 LR operator-(L, R);
1594 bool operator<(L, R);
1595 bool operator>(L, R);
1596 bool operator<=(L, R);
1597 bool operator>=(L, R);
1598 bool operator==(L, R);
1599 bool operator!=(L, R);
1600 where LR is the result of the usual arithmetic conversions between
1601 types L and R.
1603 14For every pair of types T and I, where T is a cv-qualified or cv-
1604 unqualified complete object type and I is a promoted integral type,
1605 there exist candidate operator functions of the form
1606 T* operator+(T*, I);
1607 T& operator[](T*, I);
1608 T* operator-(T*, I);
1609 T* operator+(I, T*);
1610 T& operator[](I, T*);
1612 15For every T, where T is a pointer to complete object type, there exist
1613 candidate operator functions of the form112)
1614 ptrdiff_t operator-(T, T);
1616 16For every pointer type T, there exist candidate operator functions of
1617 the form
1618 bool operator<(T, T);
1619 bool operator>(T, T);
1620 bool operator<=(T, T);
1621 bool operator>=(T, T);
1622 bool operator==(T, T);
1623 bool operator!=(T, T);
1625 17For every pointer to member type T, there exist candidate operator
1626 functions of the form
1627 bool operator==(T, T);
1628 bool operator!=(T, T); */
1630 case MINUS_EXPR:
1631 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1632 break;
1633 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1635 type2 = ptrdiff_type_node;
1636 break;
1638 case MULT_EXPR:
1639 case TRUNC_DIV_EXPR:
1640 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1641 break;
1642 return candidates;
1644 case EQ_EXPR:
1645 case NE_EXPR:
1646 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1647 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1648 break;
1649 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1650 && null_ptr_cst_p (args[1]))
1652 type2 = type1;
1653 break;
1655 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1656 && null_ptr_cst_p (args[0]))
1658 type1 = type2;
1659 break;
1661 case LT_EXPR:
1662 case GT_EXPR:
1663 case LE_EXPR:
1664 case GE_EXPR:
1665 case MAX_EXPR:
1666 case MIN_EXPR:
1667 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1668 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1669 break;
1670 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1672 type2 = type1;
1673 break;
1675 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1677 type1 = type2;
1678 break;
1680 return candidates;
1682 case PLUS_EXPR:
1683 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1684 break;
1685 case ARRAY_REF:
1686 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1688 type1 = ptrdiff_type_node;
1689 break;
1691 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1693 type2 = ptrdiff_type_node;
1694 break;
1696 return candidates;
1698 /* 18For every pair of promoted integral types L and R, there exist candi-
1699 date operator functions of the form
1700 LR operator%(L, R);
1701 LR operator&(L, R);
1702 LR operator^(L, R);
1703 LR operator|(L, R);
1704 L operator<<(L, R);
1705 L operator>>(L, R);
1706 where LR is the result of the usual arithmetic conversions between
1707 types L and R. */
1709 case TRUNC_MOD_EXPR:
1710 case BIT_AND_EXPR:
1711 case BIT_IOR_EXPR:
1712 case BIT_XOR_EXPR:
1713 case LSHIFT_EXPR:
1714 case RSHIFT_EXPR:
1715 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1716 break;
1717 return candidates;
1719 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1720 type, VQ is either volatile or empty, and R is a promoted arithmetic
1721 type, there exist candidate operator functions of the form
1722 VQ L& operator=(VQ L&, R);
1723 VQ L& operator*=(VQ L&, R);
1724 VQ L& operator/=(VQ L&, R);
1725 VQ L& operator+=(VQ L&, R);
1726 VQ L& operator-=(VQ L&, R);
1728 20For every pair T, VQ), where T is any type and VQ is either volatile
1729 or empty, there exist candidate operator functions of the form
1730 T*VQ& operator=(T*VQ&, T*);
1732 21For every pair T, VQ), where T is a pointer to member type and VQ is
1733 either volatile or empty, there exist candidate operator functions of
1734 the form
1735 VQ T& operator=(VQ T&, T);
1737 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1738 unqualified complete object type, VQ is either volatile or empty, and
1739 I is a promoted integral type, there exist candidate operator func-
1740 tions of the form
1741 T*VQ& operator+=(T*VQ&, I);
1742 T*VQ& operator-=(T*VQ&, I);
1744 23For every triple L, VQ, R), where L is an integral or enumeration
1745 type, VQ is either volatile or empty, and R is a promoted integral
1746 type, there exist candidate operator functions of the form
1748 VQ L& operator%=(VQ L&, R);
1749 VQ L& operator<<=(VQ L&, R);
1750 VQ L& operator>>=(VQ L&, R);
1751 VQ L& operator&=(VQ L&, R);
1752 VQ L& operator^=(VQ L&, R);
1753 VQ L& operator|=(VQ L&, R); */
1755 case MODIFY_EXPR:
1756 switch (code2)
1758 case PLUS_EXPR:
1759 case MINUS_EXPR:
1760 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1762 type2 = ptrdiff_type_node;
1763 break;
1765 case MULT_EXPR:
1766 case TRUNC_DIV_EXPR:
1767 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1768 break;
1769 return candidates;
1771 case TRUNC_MOD_EXPR:
1772 case BIT_AND_EXPR:
1773 case BIT_IOR_EXPR:
1774 case BIT_XOR_EXPR:
1775 case LSHIFT_EXPR:
1776 case RSHIFT_EXPR:
1777 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1778 break;
1779 return candidates;
1781 case NOP_EXPR:
1782 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1783 break;
1784 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1785 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1786 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1787 || ((TYPE_PTRMEMFUNC_P (type1)
1788 || TREE_CODE (type1) == POINTER_TYPE)
1789 && null_ptr_cst_p (args[1])))
1791 type2 = type1;
1792 break;
1794 return candidates;
1796 default:
1797 my_friendly_abort (367);
1799 type1 = build_reference_type (type1);
1800 break;
1802 case COND_EXPR:
1803 /* Kludge around broken overloading rules whereby
1804 bool ? const char& : enum is ambiguous
1805 (between int and const char&). */
1806 flags |= LOOKUP_NO_TEMP_BIND;
1808 /* Extension: Support ?: of enumeral type. Hopefully this will not
1809 be an extension for long. */
1810 if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
1811 break;
1812 else if (TREE_CODE (type1) == ENUMERAL_TYPE
1813 || TREE_CODE (type2) == ENUMERAL_TYPE)
1814 return candidates;
1815 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1816 break;
1817 if (TREE_CODE (type1) == TREE_CODE (type2)
1818 && (TREE_CODE (type1) == REFERENCE_TYPE
1819 || TREE_CODE (type1) == POINTER_TYPE
1820 || TYPE_PTRMEMFUNC_P (type1)
1821 || IS_AGGR_TYPE (type1)))
1822 break;
1823 if (TREE_CODE (type1) == REFERENCE_TYPE
1824 || TREE_CODE (type2) == REFERENCE_TYPE)
1825 return candidates;
1826 if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
1827 && null_ptr_cst_p (args[1]))
1828 || IS_AGGR_TYPE (type1))
1830 type2 = type1;
1831 break;
1833 if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
1834 && null_ptr_cst_p (args[0]))
1835 || IS_AGGR_TYPE (type2))
1837 type1 = type2;
1838 break;
1840 return candidates;
1842 default:
1843 my_friendly_abort (367);
1846 /* If we're dealing with two pointer types, we need candidates
1847 for both of them. */
1848 if (type2 && type1 != type2
1849 && TREE_CODE (type1) == TREE_CODE (type2)
1850 && (TREE_CODE (type1) == REFERENCE_TYPE
1851 || (TREE_CODE (type1) == POINTER_TYPE
1852 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1853 || TYPE_PTRMEMFUNC_P (type1)
1854 || IS_AGGR_TYPE (type1)))
1856 candidates = build_builtin_candidate
1857 (candidates, fnname, type1, type1, args, argtypes, flags);
1858 return build_builtin_candidate
1859 (candidates, fnname, type2, type2, args, argtypes, flags);
1862 return build_builtin_candidate
1863 (candidates, fnname, type1, type2, args, argtypes, flags);
1866 tree
1867 type_decays_to (type)
1868 tree type;
1870 if (TREE_CODE (type) == ARRAY_TYPE)
1871 return build_pointer_type (TREE_TYPE (type));
1872 if (TREE_CODE (type) == FUNCTION_TYPE)
1873 return build_pointer_type (type);
1874 return type;
1877 /* There are three conditions of builtin candidates:
1879 1) bool-taking candidates. These are the same regardless of the input.
1880 2) pointer-pair taking candidates. These are generated for each type
1881 one of the input types converts to.
1882 3) arithmetic candidates. According to the WP, we should generate
1883 all of these, but I'm trying not to... */
1885 static struct z_candidate *
1886 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1887 struct z_candidate *candidates;
1888 enum tree_code code, code2;
1889 tree fnname, *args;
1890 int flags;
1892 int ref1, i;
1893 tree type, argtypes[3], types[2];
1895 for (i = 0; i < 3; ++i)
1897 if (args[i])
1898 argtypes[i] = lvalue_type (args[i]);
1899 else
1900 argtypes[i] = NULL_TREE;
1903 switch (code)
1905 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1906 and VQ is either volatile or empty, there exist candidate operator
1907 functions of the form
1908 VQ T& operator++(VQ T&); */
1910 case POSTINCREMENT_EXPR:
1911 case PREINCREMENT_EXPR:
1912 case POSTDECREMENT_EXPR:
1913 case PREDECREMENT_EXPR:
1914 case MODIFY_EXPR:
1915 ref1 = 1;
1916 break;
1918 /* 24There also exist candidate operator functions of the form
1919 bool operator!(bool);
1920 bool operator&&(bool, bool);
1921 bool operator||(bool, bool); */
1923 case TRUTH_NOT_EXPR:
1924 return build_builtin_candidate
1925 (candidates, fnname, boolean_type_node,
1926 NULL_TREE, args, argtypes, flags);
1928 case TRUTH_ORIF_EXPR:
1929 case TRUTH_ANDIF_EXPR:
1930 return build_builtin_candidate
1931 (candidates, fnname, boolean_type_node,
1932 boolean_type_node, args, argtypes, flags);
1934 case ADDR_EXPR:
1935 case COMPOUND_EXPR:
1936 case COMPONENT_REF:
1937 return candidates;
1939 default:
1940 ref1 = 0;
1943 types[0] = types[1] = NULL_TREE;
1945 for (i = 0; i < 2; ++i)
1947 if (! args[i])
1949 else if (IS_AGGR_TYPE (argtypes[i]))
1951 tree convs;
1953 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1954 return candidates;
1956 convs = lookup_conversions (argtypes[i]);
1958 if (code == COND_EXPR)
1960 if (real_lvalue_p (args[i]))
1961 types[i] = scratch_tree_cons
1962 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1964 types[i] = scratch_tree_cons
1965 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1968 else if (! convs)
1969 return candidates;
1971 for (; convs; convs = TREE_CHAIN (convs))
1973 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
1975 if (i == 0 && ref1
1976 && (TREE_CODE (type) != REFERENCE_TYPE
1977 || CP_TYPE_CONST_P (TREE_TYPE (type))))
1978 continue;
1980 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1981 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1983 type = non_reference (type);
1984 if (i != 0 || ! ref1)
1986 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1987 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1988 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1989 if (INTEGRAL_TYPE_P (type))
1990 type = type_promotes_to (type);
1993 if (! value_member (type, types[i]))
1994 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1997 else
1999 if (code == COND_EXPR && real_lvalue_p (args[i]))
2000 types[i] = scratch_tree_cons
2001 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2002 type = non_reference (argtypes[i]);
2003 if (i != 0 || ! ref1)
2005 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2006 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2007 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
2008 if (INTEGRAL_TYPE_P (type))
2009 type = type_promotes_to (type);
2011 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
2015 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2017 if (types[1])
2018 for (type = types[1]; type; type = TREE_CHAIN (type))
2019 candidates = add_builtin_candidate
2020 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2021 TREE_VALUE (type), args, argtypes, flags);
2022 else
2023 candidates = add_builtin_candidate
2024 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2025 NULL_TREE, args, argtypes, flags);
2028 return candidates;
2032 /* If TMPL can be successfully instantiated as indicated by
2033 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2035 TMPL is the template. EXPLICIT_TARGS are any explicit template
2036 arguments. ARGLIST is the arguments provided at the call-site.
2037 The RETURN_TYPE is the desired type for conversion operators. If
2038 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
2039 OBJ is supplied, FLAGS are ignored, and OBJ is as for
2040 add_conv_candidate. */
2042 static struct z_candidate*
2043 add_template_candidate_real (candidates, tmpl, explicit_targs,
2044 arglist, return_type, flags,
2045 obj, strict)
2046 struct z_candidate *candidates;
2047 tree tmpl, explicit_targs, arglist, return_type;
2048 int flags;
2049 tree obj;
2050 unification_kind_t strict;
2052 int ntparms = DECL_NTPARMS (tmpl);
2053 tree targs = make_scratch_vec (ntparms);
2054 struct z_candidate *cand;
2055 int i;
2056 tree fn;
2058 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
2059 return_type, strict);
2061 if (i != 0)
2062 return candidates;
2064 fn = instantiate_template (tmpl, targs);
2065 if (fn == error_mark_node)
2066 return candidates;
2068 if (obj != NULL_TREE)
2069 /* Aha, this is a conversion function. */
2070 cand = add_conv_candidate (candidates, fn, obj, arglist);
2071 else
2072 cand = add_function_candidate (candidates, fn, arglist, flags);
2073 if (DECL_TI_TEMPLATE (fn) != tmpl)
2074 /* This situation can occur if a member template of a template
2075 class is specialized. Then, instantiate_template might return
2076 an instantiation of the specialization, in which case the
2077 DECL_TI_TEMPLATE field will point at the original
2078 specialization. For example:
2080 template <class T> struct S { template <class U> void f(U);
2081 template <> void f(int) {}; };
2082 S<double> sd;
2083 sd.f(3);
2085 Here, TMPL will be template <class U> S<double>::f(U).
2086 And, instantiate template will give us the specialization
2087 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2088 for this will point at template <class T> template <> S<T>::f(int),
2089 so that we can find the definition. For the purposes of
2090 overload resolution, however, we want the original TMPL. */
2091 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2092 else
2093 cand->template = DECL_TEMPLATE_INFO (fn);
2095 return cand;
2099 static struct z_candidate *
2100 add_template_candidate (candidates, tmpl, explicit_targs,
2101 arglist, return_type, flags, strict)
2102 struct z_candidate *candidates;
2103 tree tmpl, explicit_targs, arglist, return_type;
2104 int flags;
2105 unification_kind_t strict;
2107 return
2108 add_template_candidate_real (candidates, tmpl, explicit_targs,
2109 arglist, return_type, flags,
2110 NULL_TREE, strict);
2114 static struct z_candidate *
2115 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2116 struct z_candidate *candidates;
2117 tree tmpl, obj, arglist, return_type;
2119 return
2120 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
2121 return_type, 0, obj, DEDUCE_CONV);
2125 static int
2126 any_viable (cands)
2127 struct z_candidate *cands;
2129 for (; cands; cands = cands->next)
2130 if (pedantic ? cands->viable == 1 : cands->viable)
2131 return 1;
2132 return 0;
2135 static struct z_candidate *
2136 splice_viable (cands)
2137 struct z_candidate *cands;
2139 struct z_candidate **p = &cands;
2141 for (; *p; )
2143 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2144 p = &((*p)->next);
2145 else
2146 *p = (*p)->next;
2149 return cands;
2152 static tree
2153 build_this (obj)
2154 tree obj;
2156 /* Fix this to work on non-lvalues. */
2157 if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
2158 || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
2159 return obj;
2160 else
2161 return build_unary_op (ADDR_EXPR, obj, 0);
2164 static void
2165 print_z_candidates (candidates)
2166 struct z_candidate *candidates;
2168 const char *str = "candidates are:";
2169 for (; candidates; candidates = candidates->next)
2171 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2173 if (candidates->fn == ansi_opname [COND_EXPR])
2174 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2175 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2176 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2177 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2178 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2179 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2180 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2181 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2182 else
2183 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2184 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2186 else if (TYPE_P (candidates->fn))
2187 cp_error ("%s %T <conversion>", str, candidates->fn);
2188 else
2189 cp_error_at ("%s %+#D%s", str, candidates->fn,
2190 candidates->viable == -1 ? " <near match>" : "");
2191 str = " ";
2195 /* Returns the best overload candidate to perform the requested
2196 conversion. This function is used for three the overloading situations
2197 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2198 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2199 per [dcl.init.ref], so we ignore temporary bindings. */
2201 static struct z_candidate *
2202 build_user_type_conversion_1 (totype, expr, flags)
2203 tree totype, expr;
2204 int flags;
2206 struct z_candidate *candidates, *cand;
2207 tree fromtype = TREE_TYPE (expr);
2208 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2209 tree args = NULL_TREE;
2210 tree templates = NULL_TREE;
2212 if (IS_AGGR_TYPE (totype))
2213 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2214 if (IS_AGGR_TYPE (fromtype)
2215 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2216 convs = lookup_conversions (fromtype);
2218 candidates = 0;
2219 flags |= LOOKUP_NO_CONVERSION;
2221 if (ctors)
2223 tree t = build_int_2 (0, 0);
2224 TREE_TYPE (t) = build_pointer_type (totype);
2225 args = build_scratch_list (NULL_TREE, expr);
2226 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2227 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2228 args = scratch_tree_cons (NULL_TREE, t, args);
2230 ctors = TREE_VALUE (ctors);
2232 for (; ctors; ctors = OVL_NEXT (ctors))
2234 tree ctor = OVL_CURRENT (ctors);
2235 if (DECL_NONCONVERTING_P (ctor))
2236 continue;
2238 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2240 templates = scratch_tree_cons (NULL_TREE, ctor, templates);
2241 candidates =
2242 add_template_candidate (candidates, ctor,
2243 NULL_TREE, args, NULL_TREE, flags,
2244 DEDUCE_CALL);
2246 else
2247 candidates = add_function_candidate (candidates, ctor,
2248 args, flags);
2250 if (candidates)
2252 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2253 candidates->basetype_path = TYPE_BINFO (totype);
2257 if (convs)
2258 args = build_scratch_list (NULL_TREE, build_this (expr));
2260 for (; convs; convs = TREE_CHAIN (convs))
2262 tree fns = TREE_VALUE (convs);
2263 int convflags = LOOKUP_NO_CONVERSION;
2264 tree ics;
2266 /* If we are called to convert to a reference type, we are trying to
2267 find an lvalue binding, so don't even consider temporaries. If
2268 we don't find an lvalue binding, the caller will try again to
2269 look for a temporary binding. */
2270 if (TREE_CODE (totype) == REFERENCE_TYPE)
2271 convflags |= LOOKUP_NO_TEMP_BIND;
2273 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2274 ics = implicit_conversion
2275 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2276 else
2277 /* We can't compute this yet. */
2278 ics = error_mark_node;
2280 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2281 /* ignore the near match. */;
2282 else if (ics)
2283 for (; fns; fns = OVL_NEXT (fns))
2285 tree fn = OVL_CURRENT (fns);
2286 struct z_candidate *old_candidates = candidates;
2288 if (TREE_CODE (fn) == TEMPLATE_DECL)
2290 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2291 candidates =
2292 add_template_candidate (candidates, fn, NULL_TREE,
2293 args, totype, flags,
2294 DEDUCE_CONV);
2296 else
2297 candidates = add_function_candidate (candidates, fn,
2298 args, flags);
2300 if (candidates != old_candidates)
2302 if (TREE_CODE (fn) == TEMPLATE_DECL)
2303 ics = implicit_conversion
2304 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2305 0, convflags);
2307 candidates->second_conv = ics;
2308 candidates->basetype_path = TREE_PURPOSE (convs);
2310 if (ics == NULL_TREE)
2311 candidates->viable = 0;
2312 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2313 candidates->viable = -1;
2318 if (! any_viable (candidates))
2320 #if 0
2321 if (flags & LOOKUP_COMPLAIN)
2323 if (candidates && ! candidates->next)
2324 /* say why this one won't work or try to be loose */;
2325 else
2326 cp_error ("no viable candidates");
2328 #endif
2330 return 0;
2333 candidates = splice_viable (candidates);
2334 cand = tourney (candidates);
2336 if (cand == 0)
2338 if (flags & LOOKUP_COMPLAIN)
2340 cp_error ("conversion from `%T' to `%T' is ambiguous",
2341 fromtype, totype);
2342 print_z_candidates (candidates);
2345 cand = candidates; /* any one will do */
2346 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2347 ICS_USER_FLAG (cand->second_conv) = 1;
2348 ICS_BAD_FLAG (cand->second_conv) = 1;
2350 return cand;
2353 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2354 p = &(TREE_OPERAND (*p, 0));
2356 /* Pedantically, normal function declarations are never considered
2357 to refer to template instantiations, so we only do this with
2358 -fguiding-decls. */
2359 if (flag_guiding_decls && templates && ! cand->template
2360 && !DECL_INITIAL (cand->fn)
2361 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2362 add_maybe_template (cand->fn, templates);
2364 *p = build
2365 (USER_CONV,
2366 (DECL_CONSTRUCTOR_P (cand->fn)
2367 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2368 expr, build_expr_ptr_wrapper (cand));
2369 ICS_USER_FLAG (cand->second_conv) = 1;
2370 if (cand->viable == -1)
2371 ICS_BAD_FLAG (cand->second_conv) = 1;
2373 return cand;
2376 tree
2377 build_user_type_conversion (totype, expr, flags)
2378 tree totype, expr;
2379 int flags;
2381 struct z_candidate *cand
2382 = build_user_type_conversion_1 (totype, expr, flags);
2384 if (cand)
2386 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2387 return error_mark_node;
2388 return convert_from_reference (convert_like (cand->second_conv, expr));
2390 return NULL_TREE;
2393 /* Do any initial processing on the arguments to a function call. */
2395 static tree
2396 resolve_args (args)
2397 tree args;
2399 tree t;
2400 for (t = args; t; t = TREE_CHAIN (t))
2402 if (TREE_VALUE (t) == error_mark_node)
2403 return error_mark_node;
2404 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2406 error ("invalid use of void expression");
2407 return error_mark_node;
2409 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2410 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2412 return args;
2415 tree
2416 build_new_function_call (fn, args)
2417 tree fn, args;
2419 struct z_candidate *candidates = 0, *cand;
2420 tree explicit_targs = NULL_TREE;
2421 int template_only = 0;
2423 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2425 explicit_targs = TREE_OPERAND (fn, 1);
2426 fn = TREE_OPERAND (fn, 0);
2427 template_only = 1;
2430 if (really_overloaded_fn (fn))
2432 tree t1;
2433 tree templates = NULL_TREE;
2435 args = resolve_args (args);
2437 if (args == error_mark_node)
2438 return error_mark_node;
2440 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2442 tree t = OVL_FUNCTION (t1);
2443 struct z_candidate *old_candidates = candidates;
2445 if (TREE_CODE (t) == TEMPLATE_DECL)
2447 templates = scratch_tree_cons (NULL_TREE, t, templates);
2448 candidates = add_template_candidate
2449 (candidates, t, explicit_targs, args, NULL_TREE,
2450 LOOKUP_NORMAL, DEDUCE_CALL);
2452 else if (! template_only)
2453 candidates = add_function_candidate
2454 (candidates, t, args, LOOKUP_NORMAL);
2456 if (candidates != old_candidates)
2457 candidates->basetype_path = DECL_REAL_CONTEXT (t);
2460 if (! any_viable (candidates))
2462 if (candidates && ! candidates->next)
2463 return build_function_call (candidates->fn, args);
2464 cp_error ("no matching function for call to `%D (%A)'",
2465 DECL_NAME (OVL_FUNCTION (fn)), args);
2466 if (candidates)
2467 print_z_candidates (candidates);
2468 return error_mark_node;
2470 candidates = splice_viable (candidates);
2471 cand = tourney (candidates);
2473 if (cand == 0)
2475 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2476 DECL_NAME (OVL_FUNCTION (fn)), args);
2477 print_z_candidates (candidates);
2478 return error_mark_node;
2481 /* Pedantically, normal function declarations are never considered
2482 to refer to template instantiations, so we only do this with
2483 -fguiding-decls. */
2484 if (flag_guiding_decls && templates && ! cand->template
2485 && ! DECL_INITIAL (cand->fn))
2486 add_maybe_template (cand->fn, templates);
2488 return build_over_call (cand, args, LOOKUP_NORMAL);
2491 /* This is not really overloaded. */
2492 fn = OVL_CURRENT (fn);
2494 return build_function_call (fn, args);
2497 static tree
2498 build_object_call (obj, args)
2499 tree obj, args;
2501 struct z_candidate *candidates = 0, *cand;
2502 tree fns, convs, mem_args = NULL_TREE;
2503 tree type = TREE_TYPE (obj);
2505 if (TYPE_PTRMEMFUNC_P (type))
2507 /* It's no good looking for an overloaded operator() on a
2508 pointer-to-member-function. */
2509 cp_error ("pointer-to-member function %E cannot be called", obj);
2510 cp_error ("without an object; consider using .* or ->*");
2511 return error_mark_node;
2514 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2515 if (fns == error_mark_node)
2516 return error_mark_node;
2518 args = resolve_args (args);
2520 if (args == error_mark_node)
2521 return error_mark_node;
2523 if (fns)
2525 tree base = TREE_PURPOSE (fns);
2526 mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2528 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2530 tree fn = OVL_CURRENT (fns);
2531 if (TREE_CODE (fn) == TEMPLATE_DECL)
2533 candidates
2534 = add_template_candidate (candidates, fn, NULL_TREE,
2535 mem_args, NULL_TREE,
2536 LOOKUP_NORMAL, DEDUCE_CALL);
2538 else
2539 candidates = add_function_candidate
2540 (candidates, fn, mem_args, LOOKUP_NORMAL);
2542 if (candidates)
2543 candidates->basetype_path = base;
2547 convs = lookup_conversions (type);
2549 for (; convs; convs = TREE_CHAIN (convs))
2551 tree fns = TREE_VALUE (convs);
2552 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2554 if ((TREE_CODE (totype) == POINTER_TYPE
2555 || TREE_CODE (totype) == REFERENCE_TYPE)
2556 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2557 for (; fns; fns = OVL_NEXT (fns))
2559 tree fn = OVL_CURRENT (fns);
2560 if (TREE_CODE (fn) == TEMPLATE_DECL)
2562 candidates = add_template_conv_candidate (candidates,
2564 obj,
2565 args,
2566 totype);
2568 else
2569 candidates = add_conv_candidate (candidates, fn, obj, args);
2571 if (candidates)
2572 candidates->basetype_path = TREE_PURPOSE (convs);
2576 if (! any_viable (candidates))
2578 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2579 print_z_candidates (candidates);
2580 return error_mark_node;
2583 candidates = splice_viable (candidates);
2584 cand = tourney (candidates);
2586 if (cand == 0)
2588 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2589 print_z_candidates (candidates);
2590 return error_mark_node;
2593 /* Since cand->fn will be a type, not a function, for a conversion
2594 function, we must be careful not to unconditionally look at
2595 DECL_NAME here. */
2596 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2597 && DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2598 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2600 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2602 /* FIXME */
2603 return build_function_call (obj, args);
2606 static void
2607 op_error (code, code2, arg1, arg2, arg3, problem)
2608 enum tree_code code, code2;
2609 tree arg1, arg2, arg3;
2610 const char *problem;
2612 const char * opname
2613 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2615 switch (code)
2617 case COND_EXPR:
2618 cp_error ("%s for `%T ? %T : %T'", problem,
2619 error_type (arg1), error_type (arg2), error_type (arg3));
2620 break;
2621 case POSTINCREMENT_EXPR:
2622 case POSTDECREMENT_EXPR:
2623 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2624 break;
2625 case ARRAY_REF:
2626 cp_error ("%s for `%T[%T]'", problem,
2627 error_type (arg1), error_type (arg2));
2628 break;
2629 default:
2630 if (arg2)
2631 cp_error ("%s for `%T %s %T'", problem,
2632 error_type (arg1), opname, error_type (arg2));
2633 else
2634 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2638 tree
2639 build_new_op (code, flags, arg1, arg2, arg3)
2640 enum tree_code code;
2641 int flags;
2642 tree arg1, arg2, arg3;
2644 struct z_candidate *candidates = 0, *cand;
2645 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
2646 enum tree_code code2 = NOP_EXPR;
2647 tree templates = NULL_TREE;
2648 tree conv;
2650 if (arg1 == error_mark_node
2651 || arg2 == error_mark_node
2652 || arg3 == error_mark_node)
2653 return error_mark_node;
2655 /* This can happen if a template takes all non-type parameters, e.g.
2656 undeclared_template<1, 5, 72>a; */
2657 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
2659 cp_error ("`%D' must be declared before use", arg1);
2660 return error_mark_node;
2663 if (code == MODIFY_EXPR)
2665 code2 = TREE_CODE (arg3);
2666 arg3 = NULL_TREE;
2667 fnname = ansi_assopname[code2];
2669 else
2670 fnname = ansi_opname[code];
2672 switch (code)
2674 case NEW_EXPR:
2675 case VEC_NEW_EXPR:
2676 case VEC_DELETE_EXPR:
2677 case DELETE_EXPR:
2678 /* Use build_op_new_call and build_op_delete_call instead. */
2679 my_friendly_abort (981018);
2681 case CALL_EXPR:
2682 return build_object_call (arg1, arg2);
2684 default:
2685 break;
2688 /* The comma operator can have void args. */
2689 if (TREE_CODE (arg1) == OFFSET_REF)
2690 arg1 = resolve_offset_ref (arg1);
2691 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
2692 arg2 = resolve_offset_ref (arg2);
2693 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
2694 arg3 = resolve_offset_ref (arg3);
2696 if (code == COND_EXPR)
2698 if (arg2 == NULL_TREE
2699 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
2700 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
2701 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
2702 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
2703 goto builtin;
2705 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
2706 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
2707 goto builtin;
2709 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2710 arg2 = integer_zero_node;
2712 if (arg2 && arg3)
2713 arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
2714 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
2715 else if (arg2)
2716 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2717 else
2718 arglist = build_scratch_list (NULL_TREE, arg1);
2720 fns = lookup_function_nonclass (fnname, arglist);
2722 if (fns && TREE_CODE (fns) == TREE_LIST)
2723 fns = TREE_VALUE (fns);
2724 for (; fns; fns = OVL_NEXT (fns))
2726 tree fn = OVL_CURRENT (fns);
2727 if (TREE_CODE (fn) == TEMPLATE_DECL)
2729 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2730 candidates
2731 = add_template_candidate (candidates, fn, NULL_TREE,
2732 arglist, TREE_TYPE (fnname),
2733 flags, DEDUCE_CALL);
2735 else
2736 candidates = add_function_candidate (candidates, fn, arglist, flags);
2739 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
2741 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
2742 if (fns == error_mark_node)
2743 return fns;
2745 else
2746 fns = NULL_TREE;
2748 if (fns)
2750 tree basetype = TREE_PURPOSE (fns);
2751 mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
2752 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2754 tree fn = OVL_CURRENT (fns);
2755 tree this_arglist;
2757 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
2758 this_arglist = mem_arglist;
2759 else
2760 this_arglist = arglist;
2762 if (TREE_CODE (fn) == TEMPLATE_DECL)
2764 /* A member template. */
2765 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2766 candidates
2767 = add_template_candidate (candidates, fn, NULL_TREE,
2768 this_arglist, TREE_TYPE (fnname),
2769 flags, DEDUCE_CALL);
2771 else
2772 candidates = add_function_candidate
2773 (candidates, fn, this_arglist, flags);
2775 if (candidates)
2776 candidates->basetype_path = basetype;
2781 tree args[3];
2783 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
2784 to know about two args; a builtin candidate will always have a first
2785 parameter of type bool. We'll handle that in
2786 build_builtin_candidate. */
2787 if (code == COND_EXPR)
2789 args[0] = arg2;
2790 args[1] = arg3;
2791 args[2] = arg1;
2793 else
2795 args[0] = arg1;
2796 args[1] = arg2;
2797 args[2] = NULL_TREE;
2800 candidates = add_builtin_candidates
2801 (candidates, code, code2, fnname, args, flags);
2804 if (! any_viable (candidates))
2806 switch (code)
2808 case POSTINCREMENT_EXPR:
2809 case POSTDECREMENT_EXPR:
2810 /* Look for an `operator++ (int)'. If they didn't have
2811 one, then we fall back to the old way of doing things. */
2812 if (flags & LOOKUP_COMPLAIN)
2813 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
2814 fnname, opname_tab [code]);
2815 if (code == POSTINCREMENT_EXPR)
2816 code = PREINCREMENT_EXPR;
2817 else
2818 code = PREDECREMENT_EXPR;
2819 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
2821 /* The caller will deal with these. */
2822 case ADDR_EXPR:
2823 case COMPOUND_EXPR:
2824 case COMPONENT_REF:
2825 return NULL_TREE;
2827 default:
2828 break;
2830 if (flags & LOOKUP_COMPLAIN)
2832 op_error (code, code2, arg1, arg2, arg3, "no match");
2833 print_z_candidates (candidates);
2835 return error_mark_node;
2837 candidates = splice_viable (candidates);
2838 cand = tourney (candidates);
2840 if (cand == 0)
2842 if (flags & LOOKUP_COMPLAIN)
2844 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
2845 print_z_candidates (candidates);
2847 return error_mark_node;
2850 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
2852 extern int warn_synth;
2853 if (warn_synth
2854 && fnname == ansi_opname[MODIFY_EXPR]
2855 && DECL_ARTIFICIAL (cand->fn)
2856 && candidates->next
2857 && ! candidates->next->next)
2859 cp_warning ("using synthesized `%#D' for copy assignment",
2860 cand->fn);
2861 cp_warning_at (" where cfront would use `%#D'",
2862 cand == candidates
2863 ? candidates->next->fn
2864 : candidates->fn);
2867 /* Pedantically, normal function declarations are never considered
2868 to refer to template instantiations, so we only do this with
2869 -fguiding-decls. */
2870 if (flag_guiding_decls && templates && ! cand->template
2871 && ! DECL_INITIAL (cand->fn)
2872 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2873 add_maybe_template (cand->fn, templates);
2875 return build_over_call
2876 (cand,
2877 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
2878 ? mem_arglist : arglist,
2879 LOOKUP_NORMAL);
2882 /* Check for comparison of different enum types. */
2883 switch (code)
2885 case GT_EXPR:
2886 case LT_EXPR:
2887 case GE_EXPR:
2888 case LE_EXPR:
2889 case EQ_EXPR:
2890 case NE_EXPR:
2891 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
2892 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
2893 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
2894 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
2896 cp_warning ("comparison between `%#T' and `%#T'",
2897 TREE_TYPE (arg1), TREE_TYPE (arg2));
2899 break;
2900 default:
2901 break;
2904 /* We need to strip any leading REF_BIND so that bitfields don't cause
2905 errors. This should not remove any important conversions, because
2906 builtins don't apply to class objects directly. */
2907 conv = TREE_VEC_ELT (cand->convs, 0);
2908 if (TREE_CODE (conv) == REF_BIND)
2909 conv = TREE_OPERAND (conv, 0);
2910 arg1 = convert_like (conv, arg1);
2911 if (arg2)
2913 conv = TREE_VEC_ELT (cand->convs, 1);
2914 if (TREE_CODE (conv) == REF_BIND)
2915 conv = TREE_OPERAND (conv, 0);
2916 arg2 = convert_like (conv, arg2);
2918 if (arg3)
2920 conv = TREE_VEC_ELT (cand->convs, 2);
2921 if (TREE_CODE (conv) == REF_BIND)
2922 conv = TREE_OPERAND (conv, 0);
2923 arg3 = convert_like (conv, arg3);
2926 builtin:
2927 switch (code)
2929 case MODIFY_EXPR:
2930 return build_modify_expr (arg1, code2, arg2);
2932 case INDIRECT_REF:
2933 return build_indirect_ref (arg1, "unary *");
2935 case PLUS_EXPR:
2936 case MINUS_EXPR:
2937 case MULT_EXPR:
2938 case TRUNC_DIV_EXPR:
2939 case GT_EXPR:
2940 case LT_EXPR:
2941 case GE_EXPR:
2942 case LE_EXPR:
2943 case EQ_EXPR:
2944 case NE_EXPR:
2945 case MAX_EXPR:
2946 case MIN_EXPR:
2947 case LSHIFT_EXPR:
2948 case RSHIFT_EXPR:
2949 case TRUNC_MOD_EXPR:
2950 case BIT_AND_EXPR:
2951 case BIT_IOR_EXPR:
2952 case BIT_XOR_EXPR:
2953 case TRUTH_ANDIF_EXPR:
2954 case TRUTH_ORIF_EXPR:
2955 return build_binary_op_nodefault (code, arg1, arg2, code);
2957 case CONVERT_EXPR:
2958 case NEGATE_EXPR:
2959 case BIT_NOT_EXPR:
2960 case TRUTH_NOT_EXPR:
2961 case PREINCREMENT_EXPR:
2962 case POSTINCREMENT_EXPR:
2963 case PREDECREMENT_EXPR:
2964 case POSTDECREMENT_EXPR:
2965 case REALPART_EXPR:
2966 case IMAGPART_EXPR:
2967 return build_unary_op (code, arg1, candidates != 0);
2969 case ARRAY_REF:
2970 return build_array_ref (arg1, arg2);
2972 case COND_EXPR:
2973 return build_conditional_expr (arg1, arg2, arg3);
2975 case MEMBER_REF:
2976 return build_m_component_ref
2977 (build_indirect_ref (arg1, NULL_PTR), arg2);
2979 /* The caller will deal with these. */
2980 case ADDR_EXPR:
2981 case COMPONENT_REF:
2982 case COMPOUND_EXPR:
2983 return NULL_TREE;
2985 default:
2986 my_friendly_abort (367);
2987 return NULL_TREE;
2991 /* Build up a call to operator new. This has to be handled differently
2992 from other operators in the way lookup is handled; first members are
2993 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
2994 TYPE is the type to be created. ARGS are any new-placement args.
2995 FLAGS are the usual overloading flags. */
2997 tree
2998 build_op_new_call (code, type, args, flags)
2999 enum tree_code code;
3000 tree type, args;
3001 int flags;
3003 tree fnname = ansi_opname[code];
3005 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
3006 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
3008 return build_method_call (build_dummy_object (type),
3009 fnname, args, NULL_TREE, flags);
3011 else
3012 return build_new_function_call
3013 (lookup_function_nonclass (fnname, args), args);
3016 /* Build a call to operator delete. This has to be handled very specially,
3017 because the restrictions on what signatures match are different from all
3018 other call instances. For a normal delete, only a delete taking (void *)
3019 or (void *, size_t) is accepted. For a placement delete, only an exact
3020 match with the placement new is accepted.
3022 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3023 ADDR is the pointer to be deleted. For placement delete, it is also
3024 used to determine what the corresponding new looked like.
3025 SIZE is the size of the memory block to be deleted.
3026 FLAGS are the usual overloading flags.
3027 PLACEMENT is the corresponding placement new call, or 0. */
3029 tree
3030 build_op_delete_call (code, addr, size, flags, placement)
3031 enum tree_code code;
3032 tree addr, size, placement;
3033 int flags;
3035 tree fn, fns, fnname, fntype, argtypes, args, type;
3037 if (addr == error_mark_node)
3038 return error_mark_node;
3040 type = TREE_TYPE (TREE_TYPE (addr));
3041 fnname = ansi_opname[code];
3043 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3044 /* In [class.free]
3046 If the result of the lookup is ambiguous or inaccessible, or if
3047 the lookup selects a placement deallocation function, the
3048 program is ill-formed.
3050 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3052 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3053 if (fns == error_mark_node)
3054 return error_mark_node;
3056 else
3057 fns = NULL_TREE;
3059 if (fns == NULL_TREE)
3060 fns = lookup_name_nonclass (fnname);
3062 if (placement)
3064 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3066 /* Extract the function. */
3067 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3068 /* Then the second parm type. */
3069 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3071 /* Also the second argument. */
3072 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3074 else
3076 /* First try it without the size argument. */
3077 argtypes = void_list_node;
3078 args = NULL_TREE;
3081 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3082 fntype = build_function_type (void_type_node, argtypes);
3084 /* Strip const and volatile from addr. */
3085 if (type != TYPE_MAIN_VARIANT (type))
3086 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
3088 fn = instantiate_type (fntype, fns, 2);
3090 if (fn != error_mark_node)
3092 if (TREE_CODE (fns) == TREE_LIST)
3093 /* Member functions. */
3094 enforce_access (TREE_PURPOSE (fns), fn);
3095 return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
3098 /* If we are doing placement delete we do nothing if we don't find a
3099 matching op delete. */
3100 if (placement)
3101 return NULL_TREE;
3103 /* Normal delete; now try to find a match including the size argument. */
3104 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3105 tree_cons (NULL_TREE, sizetype, void_list_node));
3106 fntype = build_function_type (void_type_node, argtypes);
3108 fn = instantiate_type (fntype, fns, 2);
3110 if (fn != error_mark_node)
3112 if (BASELINK_P (fns))
3113 /* Member functions. */
3114 enforce_access (TREE_PURPOSE (fns), fn);
3115 return build_function_call
3116 (fn, expr_tree_cons (NULL_TREE, addr,
3117 build_expr_list (NULL_TREE, size)));
3120 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3121 destructor, in which case the error should be deferred
3122 until someone actually tries to delete one of these. */
3123 if (flags & LOOKUP_SPECULATIVELY)
3124 return NULL_TREE;
3126 cp_error ("no suitable operator delete for `%T'", type);
3127 return error_mark_node;
3130 /* If the current scope isn't allowed to access DECL along
3131 BASETYPE_PATH, give an error. The most derived class in
3132 BASETYPE_PATH is the one used to qualify DECL. */
3135 enforce_access (basetype_path, decl)
3136 tree basetype_path;
3137 tree decl;
3139 int accessible;
3141 accessible = accessible_p (basetype_path, decl);
3142 if (!accessible)
3144 if (TREE_PRIVATE (decl))
3145 cp_error_at ("`%+#D' is private", decl);
3146 else if (TREE_PROTECTED (decl))
3147 cp_error_at ("`%+#D' is protected", decl);
3148 else
3149 cp_error_at ("`%+#D' is inaccessible", decl);
3150 cp_error ("within this context");
3151 return 0;
3154 return 1;
3157 /* Perform the conversions in CONVS on the expression EXPR. */
3159 static tree
3160 convert_like (convs, expr)
3161 tree convs, expr;
3163 if (ICS_BAD_FLAG (convs)
3164 && TREE_CODE (convs) != USER_CONV
3165 && TREE_CODE (convs) != AMBIG_CONV
3166 && TREE_CODE (convs) != REF_BIND)
3168 tree t = convs;
3169 for (; t; t = TREE_OPERAND (t, 0))
3171 if (TREE_CODE (t) == USER_CONV)
3173 expr = convert_like (t, expr);
3174 break;
3176 else if (TREE_CODE (t) == AMBIG_CONV)
3177 return convert_like (t, expr);
3178 else if (TREE_CODE (t) == IDENTITY_CONV)
3179 break;
3181 return convert_for_initialization
3182 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3183 "conversion", NULL_TREE, 0);
3186 switch (TREE_CODE (convs))
3188 case USER_CONV:
3190 struct z_candidate *cand
3191 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3192 tree fn = cand->fn;
3193 tree args;
3195 if (DECL_CONSTRUCTOR_P (fn))
3197 tree t = build_int_2 (0, 0);
3198 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3200 args = build_scratch_list (NULL_TREE, expr);
3201 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3202 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3203 args = scratch_tree_cons (NULL_TREE, t, args);
3205 else
3206 args = build_this (expr);
3207 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3209 /* If this is a constructor or a function returning an aggr type,
3210 we need to build up a TARGET_EXPR. */
3211 if (DECL_CONSTRUCTOR_P (fn))
3212 expr = build_cplus_new (TREE_TYPE (convs), expr);
3214 return expr;
3216 case IDENTITY_CONV:
3217 if (type_unknown_p (expr))
3218 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3219 return expr;
3220 case AMBIG_CONV:
3221 /* Call build_user_type_conversion again for the error. */
3222 return build_user_type_conversion
3223 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3225 default:
3226 break;
3229 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3230 if (expr == error_mark_node)
3231 return error_mark_node;
3233 /* Convert a constant variable to its underlying value, unless we
3234 are about to bind it to a reference, in which case we need to
3235 leave it as an lvalue. */
3236 if (TREE_READONLY_DECL_P (expr) && TREE_CODE (convs) != REF_BIND)
3237 expr = decl_constant_value (expr);
3239 switch (TREE_CODE (convs))
3241 case RVALUE_CONV:
3242 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3243 return expr;
3244 /* else fall through */
3245 case BASE_CONV:
3246 if (TREE_CODE (convs) == BASE_CONV
3247 && !NEED_TEMPORARY_P (convs))
3248 /* We are going to bind a reference directly to a base-class
3249 subobject of EXPR. We don't have to generate any code
3250 here. */
3251 return expr;
3253 tree cvt_expr = build_user_type_conversion
3254 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3255 if (!cvt_expr)
3257 /* This can occur if, for example, the EXPR has incomplete
3258 type. We can't check for that before attempting the
3259 conversion because the type might be an incomplete
3260 array type, which is OK if some constructor for the
3261 destination type takes a pointer argument. */
3262 if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
3264 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
3265 incomplete_type_error (expr, TREE_TYPE (expr));
3266 else
3267 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3268 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3270 else
3271 cp_error ("could not convert `%E' to `%T'",
3272 expr, TREE_TYPE (convs));
3273 return error_mark_node;
3275 return cvt_expr;
3278 case REF_BIND:
3280 tree ref_type = TREE_TYPE (convs);
3282 /* If necessary, create a temporary. */
3283 if (NEED_TEMPORARY_P (convs))
3285 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3286 tree slot = build_decl (VAR_DECL, NULL_TREE, type);
3287 DECL_ARTIFICIAL (slot) = 1;
3288 expr = build (TARGET_EXPR, type, slot, expr,
3289 NULL_TREE, NULL_TREE);
3290 TREE_SIDE_EFFECTS (expr) = 1;
3293 /* Take the address of the thing to which we will bind the
3294 reference. */
3295 expr = build_unary_op (ADDR_EXPR, expr, 1);
3296 if (expr == error_mark_node)
3297 return error_mark_node;
3299 /* Convert it to a pointer to the type referred to by the
3300 reference. This will adjust the pointer if a derived to
3301 base conversion is being performed. */
3302 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3303 expr);
3304 /* Convert the pointer to the desired reference type. */
3305 expr = build1 (NOP_EXPR, ref_type, expr);
3307 return expr;
3310 case LVALUE_CONV:
3311 return decay_conversion (expr);
3313 case QUAL_CONV:
3314 /* Warn about deprecated conversion if appropriate. */
3315 string_conv_p (TREE_TYPE (convs), expr, 1);
3316 break;
3318 default:
3319 break;
3321 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3322 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3325 /* ARG is being passed to a varargs function. Perform any conversions
3326 required. Return the converted value. */
3328 tree
3329 convert_arg_to_ellipsis (arg)
3330 tree arg;
3332 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3333 && (TYPE_PRECISION (TREE_TYPE (arg))
3334 < TYPE_PRECISION (double_type_node)))
3335 /* Convert `float' to `double'. */
3336 arg = cp_convert (double_type_node, arg);
3337 else if (IS_AGGR_TYPE (TREE_TYPE (arg))
3338 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (arg)))
3339 cp_warning ("cannot pass objects of type `%T' through `...'",
3340 TREE_TYPE (arg));
3341 else
3342 /* Convert `short' and `char' to full-size `int'. */
3343 arg = default_conversion (arg);
3345 arg = require_complete_type (arg);
3347 return arg;
3350 /* ARG is a default argument expression being passed to a parameter of
3351 the indicated TYPE, which is a parameter to FN. Do any required
3352 conversions. Return the converted value. */
3354 tree
3355 convert_default_arg (type, arg, fn)
3356 tree type;
3357 tree arg;
3358 tree fn;
3360 if (fn && DECL_TEMPLATE_INFO (fn))
3362 /* This default argument came from a template. Instantiate the
3363 default argument here, not in tsubst. In the case of
3364 something like:
3366 template <class T>
3367 struct S {
3368 static T t();
3369 void f(T = t());
3372 we must be careful to do name lookup in the scope of S<T>,
3373 rather than in the current class. */
3374 if (DECL_CLASS_SCOPE_P (fn))
3375 pushclass (DECL_REAL_CONTEXT (fn), 2);
3377 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), /*complain=*/1, NULL_TREE);
3379 if (DECL_CLASS_SCOPE_P (fn))
3380 popclass ();
3382 /* Make sure the default argument is reasonable. */
3383 arg = check_default_argument (type, arg);
3386 arg = break_out_target_exprs (arg);
3388 if (TREE_CODE (arg) == CONSTRUCTOR)
3390 arg = digest_init (type, arg, 0);
3391 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3392 "default argument", 0, 0);
3394 else
3396 /* This could get clobbered by the following call. */
3397 if (TREE_HAS_CONSTRUCTOR (arg))
3398 arg = copy_node (arg);
3400 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3401 "default argument", 0, 0);
3402 #ifdef PROMOTE_PROTOTYPES
3403 if ((TREE_CODE (type) == INTEGER_TYPE
3404 || TREE_CODE (type) == ENUMERAL_TYPE)
3405 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3406 arg = default_conversion (arg);
3407 #endif
3410 return arg;
3413 static tree
3414 build_over_call (cand, args, flags)
3415 struct z_candidate *cand;
3416 tree args;
3417 int flags;
3419 tree fn = cand->fn;
3420 tree convs = cand->convs;
3421 tree converted_args = NULL_TREE;
3422 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3423 tree conv, arg, val;
3424 int i = 0;
3425 int is_method = 0;
3427 /* Give any warnings we noticed during overload resolution. */
3428 if (cand->warnings)
3429 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3430 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3432 if (DECL_FUNCTION_MEMBER_P (fn))
3433 enforce_access (cand->basetype_path, fn);
3435 if (args && TREE_CODE (args) != TREE_LIST)
3436 args = build_scratch_list (NULL_TREE, args);
3437 arg = args;
3439 /* The implicit parameters to a constructor are not considered by overload
3440 resolution, and must be of the proper type. */
3441 if (DECL_CONSTRUCTOR_P (fn))
3443 converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3444 arg = TREE_CHAIN (arg);
3445 parm = TREE_CHAIN (parm);
3446 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3448 converted_args = expr_tree_cons
3449 (NULL_TREE, TREE_VALUE (arg), converted_args);
3450 arg = TREE_CHAIN (arg);
3451 parm = TREE_CHAIN (parm);
3454 /* Bypass access control for 'this' parameter. */
3455 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3457 tree parmtype = TREE_VALUE (parm);
3458 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3459 tree t;
3460 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3461 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3462 TREE_TYPE (argtype), fn);
3464 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3465 X is called for an object that is not of type X, or of a type
3466 derived from X, the behavior is undefined.
3468 So we can assume that anything passed as 'this' is non-null, and
3469 optimize accordingly. */
3470 if (TREE_CODE (parmtype) == POINTER_TYPE)
3471 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3472 else
3473 /* This happens with signatures. */
3474 t = convert_force (parmtype, TREE_VALUE (arg), CONV_C_CAST);
3475 converted_args = expr_tree_cons (NULL_TREE, t, converted_args);
3476 parm = TREE_CHAIN (parm);
3477 arg = TREE_CHAIN (arg);
3478 ++i;
3479 is_method = 1;
3482 for (; arg && parm;
3483 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3485 tree type = TREE_VALUE (parm);
3487 conv = TREE_VEC_ELT (convs, i);
3488 if (ICS_BAD_FLAG (conv))
3490 tree t = conv;
3491 val = TREE_VALUE (arg);
3493 for (; t; t = TREE_OPERAND (t, 0))
3495 if (TREE_CODE (t) == USER_CONV
3496 || TREE_CODE (t) == AMBIG_CONV)
3498 val = convert_like (t, val);
3499 break;
3501 else if (TREE_CODE (t) == IDENTITY_CONV)
3502 break;
3504 val = convert_for_initialization
3505 (NULL_TREE, type, val, LOOKUP_NORMAL,
3506 "argument passing", fn, i - is_method);
3508 else
3510 /* Issue warnings about peculiar, but legal, uses of NULL. */
3511 if (ARITHMETIC_TYPE_P (TREE_VALUE (parm))
3512 && TREE_VALUE (arg) == null_node)
3513 cp_warning ("converting NULL to non-pointer type");
3515 val = convert_like (conv, TREE_VALUE (arg));
3518 #ifdef PROMOTE_PROTOTYPES
3519 if ((TREE_CODE (type) == INTEGER_TYPE
3520 || TREE_CODE (type) == ENUMERAL_TYPE)
3521 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3522 val = default_conversion (val);
3523 #endif
3524 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3527 /* Default arguments */
3528 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3529 converted_args
3530 = expr_tree_cons (NULL_TREE,
3531 convert_default_arg (TREE_VALUE (parm),
3532 TREE_PURPOSE (parm),
3533 fn),
3534 converted_args);
3536 /* Ellipsis */
3537 for (; arg; arg = TREE_CHAIN (arg))
3538 converted_args
3539 = expr_tree_cons (NULL_TREE,
3540 convert_arg_to_ellipsis (TREE_VALUE (arg)),
3541 converted_args);
3543 converted_args = nreverse (converted_args);
3545 if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
3546 check_function_format (DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
3547 converted_args);
3549 /* Avoid actually calling copy constructors and copy assignment operators,
3550 if possible. */
3552 if (! flag_elide_constructors)
3553 /* Do things the hard way. */;
3554 else if (DECL_CONSTRUCTOR_P (fn)
3555 && TREE_VEC_LENGTH (convs) == 1
3556 && copy_args_p (fn))
3558 tree targ;
3559 arg = TREE_CHAIN (converted_args);
3560 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3561 arg = TREE_CHAIN (arg);
3562 arg = TREE_VALUE (arg);
3564 /* Pull out the real argument, disregarding const-correctness. */
3565 targ = arg;
3566 while (TREE_CODE (targ) == NOP_EXPR
3567 || TREE_CODE (targ) == NON_LVALUE_EXPR
3568 || TREE_CODE (targ) == CONVERT_EXPR)
3569 targ = TREE_OPERAND (targ, 0);
3570 if (TREE_CODE (targ) == ADDR_EXPR)
3572 targ = TREE_OPERAND (targ, 0);
3573 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3574 TYPE_MAIN_VARIANT (TREE_TYPE (targ))))
3575 targ = NULL_TREE;
3577 else
3578 targ = NULL_TREE;
3580 if (targ)
3581 arg = targ;
3582 else
3583 arg = build_indirect_ref (arg, 0);
3585 /* [class.copy]: the copy constructor is implicitly defined even if
3586 the implementation elided its use. */
3587 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3588 mark_used (fn);
3590 /* If we're creating a temp and we already have one, don't create a
3591 new one. If we're not creating a temp but we get one, use
3592 INIT_EXPR to collapse the temp into our target. Otherwise, if the
3593 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3594 temp or an INIT_EXPR otherwise. */
3595 if (integer_zerop (TREE_VALUE (args)))
3597 if (! real_lvalue_p (arg))
3598 return arg;
3599 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3601 val = build_decl (VAR_DECL, NULL_TREE, DECL_CONTEXT (fn));
3602 val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
3603 TREE_SIDE_EFFECTS (val) = 1;
3604 return val;
3607 else if (! real_lvalue_p (arg)
3608 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3610 tree address;
3611 tree to = stabilize_reference
3612 (build_indirect_ref (TREE_VALUE (args), 0));
3614 /* If we're initializing an empty class, then we actually
3615 have to use a MODIFY_EXPR rather than an INIT_EXPR. The
3616 reason is that the dummy padding member in the target may
3617 not actually be allocated if TO is a base class
3618 subobject. Since we've set TYPE_NONCOPIED_PARTS on the
3619 padding, a MODIFY_EXPR will preserve its value, which is
3620 the right thing to do if it's not really padding at all.
3622 It's not safe to just throw away the ARG if we're looking
3623 at an empty class because the ARG might contain a
3624 TARGET_EXPR which wants to be bound to TO. If it is not,
3625 expand_expr will assign a dummy slot for the TARGET_EXPR,
3626 and we will call a destructor for it, which is wrong,
3627 because we will also destroy TO, but will never have
3628 constructed it. */
3629 val = build (is_empty_class (DECL_CLASS_CONTEXT (fn))
3630 ? MODIFY_EXPR : INIT_EXPR,
3631 DECL_CONTEXT (fn), to, arg);
3632 TREE_SIDE_EFFECTS (val) = 1;
3633 address = build_unary_op (ADDR_EXPR, val, 0);
3634 /* Avoid a warning about this expression, if the address is
3635 never used. */
3636 TREE_USED (address) = 1;
3637 return address;
3640 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
3641 && copy_args_p (fn)
3642 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
3644 tree to = stabilize_reference
3645 (build_indirect_ref (TREE_VALUE (converted_args), 0));
3647 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
3649 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
3650 TREE_SIDE_EFFECTS (val) = 1;
3651 return val;
3654 mark_used (fn);
3656 if (DECL_CLASS_SCOPE_P (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
3657 return build_signature_method_call (fn, converted_args);
3658 else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3660 tree t, *p = &TREE_VALUE (converted_args);
3661 tree binfo = get_binfo
3662 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
3663 *p = convert_pointer_to_real (binfo, *p);
3664 if (TREE_SIDE_EFFECTS (*p))
3665 *p = save_expr (*p);
3666 t = build_pointer_type (TREE_TYPE (fn));
3667 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
3668 TREE_TYPE (fn) = t;
3670 else if (DECL_INLINE (fn))
3671 fn = inline_conversion (fn);
3672 else
3673 fn = build_addr_func (fn);
3675 /* Recognize certain built-in functions so we can make tree-codes
3676 other than CALL_EXPR. We do this when it enables fold-const.c
3677 to do something useful. */
3679 if (TREE_CODE (fn) == ADDR_EXPR
3680 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3681 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
3682 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
3684 case BUILT_IN_ABS:
3685 case BUILT_IN_LABS:
3686 case BUILT_IN_FABS:
3687 if (converted_args == 0)
3688 return integer_zero_node;
3689 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
3690 default:
3691 break;
3694 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3695 if (TREE_CODE (TREE_TYPE (fn)) == VOID_TYPE)
3696 return fn;
3697 fn = require_complete_type (fn);
3698 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
3699 fn = build_cplus_new (TREE_TYPE (fn), fn);
3700 return convert_from_reference (fn);
3703 static tree
3704 build_new_method_call (instance, name, args, basetype_path, flags)
3705 tree instance, name, args, basetype_path;
3706 int flags;
3708 struct z_candidate *candidates = 0, *cand;
3709 tree explicit_targs = NULL_TREE;
3710 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
3711 tree pretty_name;
3712 tree user_args = args;
3713 tree templates = NULL_TREE;
3714 int template_only = 0;
3716 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3718 explicit_targs = TREE_OPERAND (name, 1);
3719 name = TREE_OPERAND (name, 0);
3720 if (TREE_CODE_CLASS (TREE_CODE (name)) == 'd')
3721 name = DECL_NAME (name);
3722 else
3724 if (TREE_CODE (name) == COMPONENT_REF)
3725 name = TREE_OPERAND (name, 1);
3726 if (TREE_CODE (name) == OVERLOAD)
3727 name = DECL_NAME (OVL_CURRENT (name));
3730 template_only = 1;
3733 /* If there is an extra argument for controlling virtual bases,
3734 remove it for error reporting. */
3735 if (flags & LOOKUP_HAS_IN_CHARGE)
3736 user_args = TREE_CHAIN (args);
3738 args = resolve_args (args);
3740 if (args == error_mark_node)
3741 return error_mark_node;
3743 if (instance == NULL_TREE)
3744 basetype = BINFO_TYPE (basetype_path);
3745 else
3747 if (TREE_CODE (instance) == OFFSET_REF)
3748 instance = resolve_offset_ref (instance);
3749 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3750 instance = convert_from_reference (instance);
3751 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
3753 /* XXX this should be handled before we get here. */
3754 if (! IS_AGGR_TYPE (basetype)
3755 && ! (TYPE_LANG_SPECIFIC (basetype)
3756 && (IS_SIGNATURE_POINTER (basetype)
3757 || IS_SIGNATURE_REFERENCE (basetype))))
3759 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
3760 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
3761 name, instance, basetype);
3763 return error_mark_node;
3766 /* If `instance' is a signature pointer/reference and `name' is
3767 not a constructor, we are calling a signature member function.
3768 In that case set the `basetype' to the signature type. */
3769 if ((IS_SIGNATURE_POINTER (basetype)
3770 || IS_SIGNATURE_REFERENCE (basetype))
3771 && TYPE_IDENTIFIER (basetype) != name)
3772 basetype = SIGNATURE_TYPE (basetype);
3775 if (basetype_path == NULL_TREE)
3776 basetype_path = TYPE_BINFO (basetype);
3778 if (instance)
3780 instance_ptr = build_this (instance);
3782 if (! template_only)
3784 /* XXX this should be handled before we get here. */
3785 fns = build_field_call (basetype_path, instance_ptr, name, args);
3786 if (fns)
3787 return fns;
3790 else
3792 instance_ptr = build_int_2 (0, 0);
3793 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
3796 pretty_name
3797 = (name == ctor_identifier ? constructor_name (basetype) : name);
3799 fns = lookup_fnfields (basetype_path, name, 1);
3801 if (fns == error_mark_node)
3802 return error_mark_node;
3803 if (fns)
3805 tree fn = TREE_VALUE (fns);
3806 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3807 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3809 flags |= LOOKUP_HAS_IN_CHARGE;
3810 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3812 mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
3813 for (; fn; fn = OVL_NEXT (fn))
3815 tree t = OVL_CURRENT (fn);
3816 tree this_arglist;
3818 /* We can end up here for copy-init of same or base class. */
3819 if (name == ctor_identifier
3820 && (flags & LOOKUP_ONLYCONVERTING)
3821 && DECL_NONCONVERTING_P (t))
3822 continue;
3823 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
3824 this_arglist = mem_args;
3825 else
3826 this_arglist = args;
3828 if (TREE_CODE (t) == TEMPLATE_DECL)
3830 /* A member template. */
3831 templates = scratch_tree_cons (NULL_TREE, t, templates);
3832 candidates =
3833 add_template_candidate (candidates, t, explicit_targs,
3834 this_arglist,
3835 TREE_TYPE (name), flags, DEDUCE_CALL);
3837 else if (! template_only)
3838 candidates = add_function_candidate (candidates, t,
3839 this_arglist, flags);
3841 if (candidates)
3842 candidates->basetype_path = TREE_PURPOSE (fns);
3846 if (! any_viable (candidates))
3848 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
3849 if (flags & LOOKUP_SPECULATIVELY)
3850 return NULL_TREE;
3851 if (TYPE_SIZE (basetype) == 0)
3852 incomplete_type_error (instance_ptr, basetype);
3853 else
3854 cp_error ("no matching function for call to `%T::%D (%A)%V'",
3855 basetype, pretty_name, user_args,
3856 TREE_TYPE (TREE_TYPE (instance_ptr)));
3857 print_z_candidates (candidates);
3858 return error_mark_node;
3860 candidates = splice_viable (candidates);
3861 cand = tourney (candidates);
3863 if (cand == 0)
3865 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
3866 user_args);
3867 print_z_candidates (candidates);
3868 return error_mark_node;
3871 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
3872 && instance == current_class_ref
3873 && DECL_CONSTRUCTOR_P (current_function_decl)
3874 && ! (flags & LOOKUP_NONVIRTUAL)
3875 && value_member (cand->fn, CLASSTYPE_ABSTRACT_VIRTUALS (basetype)))
3876 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
3877 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3878 && is_dummy_object (instance_ptr))
3880 cp_error ("cannot call member function `%D' without object", cand->fn);
3881 return error_mark_node;
3884 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
3885 && ((instance == current_class_ref && (dtor_label || ctor_label))
3886 || resolves_to_fixed_type_p (instance, 0)))
3887 flags |= LOOKUP_NONVIRTUAL;
3889 /* Pedantically, normal function declarations are never considered
3890 to refer to template instantiations, so we only do this with
3891 -fguiding-decls. */
3892 if (flag_guiding_decls && templates && ! cand->template
3893 && ! DECL_INITIAL (cand->fn))
3894 add_maybe_template (cand->fn, templates);
3896 return build_over_call
3897 (cand,
3898 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
3899 flags);
3902 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
3903 subsequence of ICS2. */
3905 static int
3906 is_subseq (ics1, ics2)
3907 tree ics1, ics2;
3909 /* We can assume that a conversion of the same code
3910 between the same types indicates a subsequence since we only get
3911 here if the types we are converting from are the same. */
3913 while (TREE_CODE (ics1) == RVALUE_CONV
3914 || TREE_CODE (ics1) == LVALUE_CONV)
3915 ics1 = TREE_OPERAND (ics1, 0);
3917 while (1)
3919 while (TREE_CODE (ics2) == RVALUE_CONV
3920 || TREE_CODE (ics2) == LVALUE_CONV)
3921 ics2 = TREE_OPERAND (ics2, 0);
3923 if (TREE_CODE (ics2) == USER_CONV
3924 || TREE_CODE (ics2) == AMBIG_CONV
3925 || TREE_CODE (ics2) == IDENTITY_CONV)
3926 /* At this point, ICS1 cannot be a proper subsequence of
3927 ICS2. We can get a USER_CONV when we are comparing the
3928 second standard conversion sequence of two user conversion
3929 sequences. */
3930 return 0;
3932 ics2 = TREE_OPERAND (ics2, 0);
3934 if (TREE_CODE (ics2) == TREE_CODE (ics1)
3935 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
3936 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
3937 TREE_TYPE (TREE_OPERAND (ics1, 0))))
3938 return 1;
3942 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
3943 be any _TYPE nodes. */
3946 is_properly_derived_from (derived, base)
3947 tree derived;
3948 tree base;
3950 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
3951 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
3952 return 0;
3954 /* We only allow proper derivation here. The DERIVED_FROM_P macro
3955 considers every class derived from itself. */
3956 return (!same_type_p (TYPE_MAIN_VARIANT (derived),
3957 TYPE_MAIN_VARIANT (base))
3958 && DERIVED_FROM_P (base, derived));
3961 /* We build the ICS for an implicit object parameter as a pointer
3962 conversion sequence. However, such a sequence should be compared
3963 as if it were a reference conversion sequence. If ICS is the
3964 implicit conversion sequence for an implicit object parameter,
3965 modify it accordingly. */
3967 static void
3968 maybe_handle_implicit_object (ics)
3969 tree* ics;
3971 if (ICS_THIS_FLAG (*ics))
3973 /* [over.match.funcs]
3975 For non-static member functions, the type of the
3976 implicit object parameter is "reference to cv X"
3977 where X is the class of which the function is a
3978 member and cv is the cv-qualification on the member
3979 function declaration. */
3980 tree t = *ics;
3981 tree reference_type;
3983 /* The `this' parameter is a pointer to a class type. Make the
3984 implict conversion talk about a reference to that same class
3985 type. */
3986 reference_type = TREE_TYPE (TREE_TYPE (*ics));
3987 reference_type = build_reference_type (reference_type);
3989 if (TREE_CODE (t) == QUAL_CONV)
3990 t = TREE_OPERAND (t, 0);
3991 if (TREE_CODE (t) == PTR_CONV)
3992 t = TREE_OPERAND (t, 0);
3993 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3994 t = direct_reference_binding (reference_type, t);
3995 *ics = t;
3999 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
4000 to the type the reference originally referred to, and return 1.
4001 Otherwise, return 0. */
4003 static int
4004 maybe_handle_ref_bind (ics, target_type)
4005 tree* ics;
4006 tree* target_type;
4008 if (TREE_CODE (*ics) == REF_BIND)
4010 *target_type = TREE_TYPE (TREE_TYPE (*ics));
4011 *ics = TREE_OPERAND (*ics, 0);
4012 return 1;
4015 return 0;
4018 /* Compare two implicit conversion sequences according to the rules set out in
4019 [over.ics.rank]. Return values:
4021 1: ics1 is better than ics2
4022 -1: ics2 is better than ics1
4023 0: ics1 and ics2 are indistinguishable */
4025 static int
4026 compare_ics (ics1, ics2)
4027 tree ics1, ics2;
4029 tree from_type1;
4030 tree from_type2;
4031 tree to_type1;
4032 tree to_type2;
4033 tree deref_from_type1 = NULL_TREE;
4034 tree deref_from_type2 = NULL_TREE;
4035 tree deref_to_type1 = NULL_TREE;
4036 tree deref_to_type2 = NULL_TREE;
4038 /* REF_BINDING is non-zero if the result of the conversion sequence
4039 is a reference type. In that case TARGET_TYPE is the
4040 type referred to by the reference. */
4041 int ref_binding1;
4042 int ref_binding2;
4043 tree target_type1;
4044 tree target_type2;
4046 /* Handle implicit object parameters. */
4047 maybe_handle_implicit_object (&ics1);
4048 maybe_handle_implicit_object (&ics2);
4050 /* Handle reference parameters. */
4051 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
4052 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
4054 /* [over.ics.rank]
4056 When comparing the basic forms of implicit conversion sequences (as
4057 defined in _over.best.ics_)
4059 --a standard conversion sequence (_over.ics.scs_) is a better
4060 conversion sequence than a user-defined conversion sequence
4061 or an ellipsis conversion sequence, and
4063 --a user-defined conversion sequence (_over.ics.user_) is a
4064 better conversion sequence than an ellipsis conversion sequence
4065 (_over.ics.ellipsis_). */
4066 if (ICS_RANK (ics1) > ICS_RANK (ics2))
4067 return -1;
4068 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
4069 return 1;
4071 if (ICS_RANK (ics1) == BAD_RANK)
4073 /* Both ICS are bad. We try to make a decision based on what
4074 would have happenned if they'd been good. */
4075 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4076 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4077 return -1;
4078 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4079 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4080 return 1;
4082 /* We couldn't make up our minds; try to figure it out below. */
4085 if (ICS_ELLIPSIS_FLAG (ics1))
4086 /* Both conversions are ellipsis conversions. */
4087 return 0;
4089 /* User-defined conversion sequence U1 is a better conversion sequence
4090 than another user-defined conversion sequence U2 if they contain the
4091 same user-defined conversion operator or constructor and if the sec-
4092 ond standard conversion sequence of U1 is better than the second
4093 standard conversion sequence of U2. */
4095 if (ICS_USER_FLAG (ics1))
4097 tree t1, t2;
4099 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4100 if (TREE_CODE (t1) == AMBIG_CONV)
4101 return 0;
4102 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4103 if (TREE_CODE (t2) == AMBIG_CONV)
4104 return 0;
4106 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4107 return 0;
4109 /* We can just fall through here, after setting up
4110 FROM_TYPE1 and FROM_TYPE2. */
4111 from_type1 = TREE_TYPE (t1);
4112 from_type2 = TREE_TYPE (t2);
4114 else
4116 /* We're dealing with two standard conversion sequences.
4118 [over.ics.rank]
4120 Standard conversion sequence S1 is a better conversion
4121 sequence than standard conversion sequence S2 if
4123 --S1 is a proper subsequence of S2 (comparing the conversion
4124 sequences in the canonical form defined by _over.ics.scs_,
4125 excluding any Lvalue Transformation; the identity
4126 conversion sequence is considered to be a subsequence of
4127 any non-identity conversion sequence */
4129 from_type1 = ics1;
4130 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4131 from_type1 = TREE_OPERAND (from_type1, 0);
4132 from_type1 = TREE_TYPE (from_type1);
4134 from_type2 = ics2;
4135 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4136 from_type2 = TREE_OPERAND (from_type2, 0);
4137 from_type2 = TREE_TYPE (from_type2);
4140 if (same_type_p (from_type1, from_type2))
4142 if (is_subseq (ics1, ics2))
4143 return 1;
4144 if (is_subseq (ics2, ics1))
4145 return -1;
4147 /* Otherwise, one sequence cannot be a subsequence of the other; they
4148 don't start with the same type. This can happen when comparing the
4149 second standard conversion sequence in two user-defined conversion
4150 sequences. */
4152 /* [over.ics.rank]
4154 Or, if not that,
4156 --the rank of S1 is better than the rank of S2 (by the rules
4157 defined below):
4159 Standard conversion sequences are ordered by their ranks: an Exact
4160 Match is a better conversion than a Promotion, which is a better
4161 conversion than a Conversion.
4163 Two conversion sequences with the same rank are indistinguishable
4164 unless one of the following rules applies:
4166 --A conversion that is not a conversion of a pointer, or pointer
4167 to member, to bool is better than another conversion that is such
4168 a conversion.
4170 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4171 so that we do not have to check it explicitly. */
4172 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4173 return 1;
4174 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4175 return -1;
4177 to_type1 = TREE_TYPE (ics1);
4178 to_type2 = TREE_TYPE (ics2);
4180 if (TYPE_PTR_P (from_type1)
4181 && TYPE_PTR_P (from_type2)
4182 && TYPE_PTR_P (to_type1)
4183 && TYPE_PTR_P (to_type2))
4185 deref_from_type1 = TREE_TYPE (from_type1);
4186 deref_from_type2 = TREE_TYPE (from_type2);
4187 deref_to_type1 = TREE_TYPE (to_type1);
4188 deref_to_type2 = TREE_TYPE (to_type2);
4190 /* The rules for pointers to members A::* are just like the rules
4191 for pointers A*, except opposite: if B is derived from A then
4192 A::* converts to B::*, not vice versa. For that reason, we
4193 switch the from_ and to_ variables here. */
4194 else if (TYPE_PTRMEM_P (from_type1)
4195 && TYPE_PTRMEM_P (from_type2)
4196 && TYPE_PTRMEM_P (to_type1)
4197 && TYPE_PTRMEM_P (to_type2))
4199 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4200 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4201 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4202 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4204 else if (TYPE_PTRMEMFUNC_P (from_type1)
4205 && TYPE_PTRMEMFUNC_P (from_type2)
4206 && TYPE_PTRMEMFUNC_P (to_type1)
4207 && TYPE_PTRMEMFUNC_P (to_type2))
4209 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4210 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4211 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4212 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4215 if (deref_from_type1 != NULL_TREE
4216 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4217 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4219 /* This was one of the pointer or pointer-like conversions.
4221 [over.ics.rank]
4223 --If class B is derived directly or indirectly from class A,
4224 conversion of B* to A* is better than conversion of B* to
4225 void*, and conversion of A* to void* is better than
4226 conversion of B* to void*. */
4227 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4228 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4230 if (is_properly_derived_from (deref_from_type1,
4231 deref_from_type2))
4232 return -1;
4233 else if (is_properly_derived_from (deref_from_type2,
4234 deref_from_type1))
4235 return 1;
4237 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4238 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4240 if (same_type_p (deref_from_type1, deref_from_type2))
4242 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4244 if (is_properly_derived_from (deref_from_type1,
4245 deref_to_type1))
4246 return 1;
4248 /* We know that DEREF_TO_TYPE1 is `void' here. */
4249 else if (is_properly_derived_from (deref_from_type1,
4250 deref_to_type2))
4251 return -1;
4254 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4255 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4257 /* [over.ics.rank]
4259 --If class B is derived directly or indirectly from class A
4260 and class C is derived directly or indirectly from B,
4262 --conversion of C* to B* is better than conversion of C* to
4263 A*,
4265 --conversion of B* to A* is better than conversion of C* to
4266 A* */
4267 if (same_type_p (deref_from_type1, deref_from_type2))
4269 if (is_properly_derived_from (deref_to_type1,
4270 deref_to_type2))
4271 return 1;
4272 else if (is_properly_derived_from (deref_to_type2,
4273 deref_to_type1))
4274 return -1;
4276 else if (same_type_p (deref_to_type1, deref_to_type2))
4278 if (is_properly_derived_from (deref_from_type2,
4279 deref_from_type1))
4280 return 1;
4281 else if (is_properly_derived_from (deref_from_type1,
4282 deref_from_type2))
4283 return -1;
4287 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4288 && same_type_p (from_type1, from_type2))
4290 /* [over.ics.rank]
4292 --binding of an expression of type C to a reference of type
4293 B& is better than binding an expression of type C to a
4294 reference of type A&
4296 --conversion of C to B is better than conversion of C to A, */
4297 if (is_properly_derived_from (from_type1, to_type1)
4298 && is_properly_derived_from (from_type1, to_type2))
4300 if (is_properly_derived_from (to_type1, to_type2))
4301 return 1;
4302 else if (is_properly_derived_from (to_type2, to_type1))
4303 return -1;
4306 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4307 && same_type_p (to_type1, to_type2))
4309 /* [over.ics.rank]
4311 --binding of an expression of type B to a reference of type
4312 A& is better than binding an expression of type C to a
4313 reference of type A&,
4315 --onversion of B to A is better than conversion of C to A */
4316 if (is_properly_derived_from (from_type1, to_type1)
4317 && is_properly_derived_from (from_type2, to_type1))
4319 if (is_properly_derived_from (from_type2, from_type1))
4320 return 1;
4321 else if (is_properly_derived_from (from_type1, from_type2))
4322 return -1;
4326 /* [over.ics.rank]
4328 --S1 and S2 differ only in their qualification conversion and yield
4329 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4330 qualification signature of type T1 is a proper subset of the cv-
4331 qualification signature of type T2 */
4332 if (TREE_CODE (ics1) == QUAL_CONV
4333 && TREE_CODE (ics2) == QUAL_CONV
4334 && same_type_p (from_type1, from_type2))
4335 return comp_cv_qual_signature (to_type1, to_type2);
4337 /* [over.ics.rank]
4339 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4340 types to which the references refer are the same type except for
4341 top-level cv-qualifiers, and the type to which the reference
4342 initialized by S2 refers is more cv-qualified than the type to
4343 which the reference initialized by S1 refers */
4345 if (ref_binding1 && ref_binding2
4346 && same_type_p (TYPE_MAIN_VARIANT (to_type1),
4347 TYPE_MAIN_VARIANT (to_type2)))
4348 return comp_cv_qualification (target_type2, target_type1);
4350 /* Neither conversion sequence is better than the other. */
4351 return 0;
4354 /* The source type for this standard conversion sequence. */
4356 static tree
4357 source_type (t)
4358 tree t;
4360 for (;; t = TREE_OPERAND (t, 0))
4362 if (TREE_CODE (t) == USER_CONV
4363 || TREE_CODE (t) == AMBIG_CONV
4364 || TREE_CODE (t) == IDENTITY_CONV)
4365 return TREE_TYPE (t);
4367 my_friendly_abort (1823);
4370 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4371 a pointer to LOSER and re-running joust to produce the warning if WINNER
4372 is actually used. */
4374 static void
4375 add_warning (winner, loser)
4376 struct z_candidate *winner, *loser;
4378 winner->warnings = expr_tree_cons (NULL_PTR,
4379 build_expr_ptr_wrapper (loser),
4380 winner->warnings);
4383 /* Compare two candidates for overloading as described in
4384 [over.match.best]. Return values:
4386 1: cand1 is better than cand2
4387 -1: cand2 is better than cand1
4388 0: cand1 and cand2 are indistinguishable */
4390 static int
4391 joust (cand1, cand2, warn)
4392 struct z_candidate *cand1, *cand2;
4393 int warn;
4395 int winner = 0;
4396 int i, off1 = 0, off2 = 0, len;
4398 /* Candidates that involve bad conversions are always worse than those
4399 that don't. */
4400 if (cand1->viable > cand2->viable)
4401 return 1;
4402 if (cand1->viable < cand2->viable)
4403 return -1;
4405 /* If we have two pseudo-candidates for conversions to the same type,
4406 arbitrarily pick one. */
4407 if (TYPE_P (cand1->fn) && cand1->fn == cand2->fn)
4408 return 1;
4410 /* a viable function F1
4411 is defined to be a better function than another viable function F2 if
4412 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4413 ICSi(F2), and then */
4415 /* for some argument j, ICSj(F1) is a better conversion sequence than
4416 ICSj(F2) */
4418 /* For comparing static and non-static member functions, we ignore the
4419 implicit object parameter of the non-static function. The WP says to
4420 pretend that the static function has an object parm, but that won't
4421 work with operator overloading. */
4422 len = TREE_VEC_LENGTH (cand1->convs);
4423 if (len != TREE_VEC_LENGTH (cand2->convs))
4425 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4426 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4427 off2 = 1;
4428 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4429 && DECL_STATIC_FUNCTION_P (cand2->fn))
4431 off1 = 1;
4432 --len;
4434 else
4435 my_friendly_abort (42);
4438 for (i = 0; i < len; ++i)
4440 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4441 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4442 int comp = compare_ics (t1, t2);
4444 if (comp != 0)
4446 if (warn_sign_promo
4447 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4448 && TREE_CODE (t1) == STD_CONV
4449 && TREE_CODE (t2) == STD_CONV
4450 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4451 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4452 && (TYPE_PRECISION (TREE_TYPE (t1))
4453 == TYPE_PRECISION (TREE_TYPE (t2)))
4454 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4455 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4456 == ENUMERAL_TYPE)))
4458 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4459 tree type1, type2;
4460 struct z_candidate *w, *l;
4461 if (comp > 0)
4462 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4463 w = cand1, l = cand2;
4464 else
4465 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4466 w = cand2, l = cand1;
4468 if (warn)
4470 cp_warning ("passing `%T' chooses `%T' over `%T'",
4471 type, type1, type2);
4472 cp_warning (" in call to `%D'", w->fn);
4474 else
4475 add_warning (w, l);
4478 if (winner && comp != winner)
4480 winner = 0;
4481 goto tweak;
4483 winner = comp;
4487 /* warn about confusing overload resolution for user-defined conversions,
4488 either between a constructor and a conversion op, or between two
4489 conversion ops. */
4490 if (winner && cand1->second_conv
4491 && ((DECL_CONSTRUCTOR_P (cand1->fn)
4492 != DECL_CONSTRUCTOR_P (cand2->fn))
4493 /* Don't warn if the two conv ops convert to the same type... */
4494 || (! DECL_CONSTRUCTOR_P (cand1->fn)
4495 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
4496 TREE_TYPE (TREE_TYPE (cand2->fn))))))
4498 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4499 if (comp != winner)
4501 struct z_candidate *w, *l;
4502 if (winner == 1)
4503 w = cand1, l = cand2;
4504 else
4505 w = cand2, l = cand1;
4506 if (warn)
4508 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4509 if (! DECL_CONSTRUCTOR_P (w->fn))
4510 source = TREE_TYPE (source);
4511 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4512 cp_warning (" for conversion from `%T' to `%T'",
4513 source, TREE_TYPE (w->second_conv));
4514 cp_warning (" because conversion sequence for the argument is better");
4516 else
4517 add_warning (w, l);
4521 if (winner)
4522 return winner;
4524 /* or, if not that,
4525 F1 is a non-template function and F2 is a template function */
4527 if (! cand1->template && cand2->template)
4528 return 1;
4529 else if (cand1->template && ! cand2->template)
4530 return -1;
4531 else if (cand1->template && cand2->template)
4532 winner = more_specialized
4533 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4534 NULL_TREE);
4536 /* or, if not that,
4537 the context is an initialization by user-defined conversion (see
4538 _dcl.init_ and _over.match.user_) and the standard conversion
4539 sequence from the return type of F1 to the destination type (i.e.,
4540 the type of the entity being initialized) is a better conversion
4541 sequence than the standard conversion sequence from the return type
4542 of F2 to the destination type. */
4544 if (! winner && cand1->second_conv)
4545 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4547 /* If the built-in candidates are the same, arbitrarily pick one. */
4548 if (! winner && cand1->fn == cand2->fn
4549 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4551 for (i = 0; i < len; ++i)
4552 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4553 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
4554 break;
4555 if (i == TREE_VEC_LENGTH (cand1->convs))
4556 return 1;
4558 /* Kludge around broken overloading rules whereby
4559 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4560 that takes references and another that takes values. */
4561 if (cand1->fn == ansi_opname[COND_EXPR])
4563 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4564 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4565 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4566 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4568 if (same_type_p (t1, t2))
4570 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4571 return 1;
4572 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4573 return -1;
4578 tweak:
4580 /* Extension: If the worst conversion for one candidate is worse than the
4581 worst conversion for the other, take the first. */
4582 if (! winner && ! pedantic)
4584 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4586 for (i = 0; i < len; ++i)
4588 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4589 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4590 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
4591 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
4594 if (rank1 < rank2)
4595 return 1;
4596 if (rank1 > rank2)
4597 return -1;
4600 return winner;
4603 /* Given a list of candidates for overloading, find the best one, if any.
4604 This algorithm has a worst case of O(2n) (winner is last), and a best
4605 case of O(n/2) (totally ambiguous); much better than a sorting
4606 algorithm. */
4608 static struct z_candidate *
4609 tourney (candidates)
4610 struct z_candidate *candidates;
4612 struct z_candidate *champ = candidates, *challenger;
4613 int fate;
4614 int champ_compared_to_predecessor = 0;
4616 /* Walk through the list once, comparing each current champ to the next
4617 candidate, knocking out a candidate or two with each comparison. */
4619 for (challenger = champ->next; challenger; )
4621 fate = joust (champ, challenger, 0);
4622 if (fate == 1)
4623 challenger = challenger->next;
4624 else
4626 if (fate == 0)
4628 champ = challenger->next;
4629 if (champ == 0)
4630 return 0;
4631 champ_compared_to_predecessor = 0;
4633 else
4635 champ = challenger;
4636 champ_compared_to_predecessor = 1;
4639 challenger = champ->next;
4643 /* Make sure the champ is better than all the candidates it hasn't yet
4644 been compared to. */
4646 for (challenger = candidates;
4647 challenger != champ
4648 && !(champ_compared_to_predecessor && challenger->next == champ);
4649 challenger = challenger->next)
4651 fate = joust (champ, challenger, 0);
4652 if (fate != 1)
4653 return 0;
4656 return champ;
4660 can_convert (to, from)
4661 tree to, from;
4663 tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
4664 return (t && ! ICS_BAD_FLAG (t));
4668 can_convert_arg (to, from, arg)
4669 tree to, from, arg;
4671 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
4672 return (t && ! ICS_BAD_FLAG (t));
4675 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
4676 initializing a variable of that TYPE. Return the converted
4677 expression. */
4679 tree
4680 initialize_reference (type, expr)
4681 tree type;
4682 tree expr;
4684 tree conv;
4686 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
4687 if (!conv || ICS_BAD_FLAG (conv))
4689 cp_error ("could not convert `%E' to `%T'", expr, type);
4690 return error_mark_node;
4693 return convert_like (conv, expr);