Move *-*-gnu* pattern below *-*-linux*.
[official-gcc.git] / gcc / cp / call.c
blobeed211f884ae235e4fbbdfacaa8a9e280d925e19
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 92-97, 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "output.h"
31 #include "flags.h"
32 #include "rtl.h"
33 #include "toplev.h"
34 #include "defaults.h"
36 #include "obstack.h"
37 #define obstack_chunk_alloc xmalloc
38 #define obstack_chunk_free free
40 extern int inhibit_warnings;
41 extern tree ctor_label, dtor_label;
43 static tree build_new_method_call PROTO((tree, tree, tree, tree, int));
45 static tree build_field_call PROTO((tree, tree, tree, tree));
46 static struct z_candidate * tourney PROTO((struct z_candidate *));
47 static int equal_functions PROTO((tree, tree));
48 static int joust PROTO((struct z_candidate *, struct z_candidate *, int));
49 static int compare_ics PROTO((tree, tree));
50 static tree build_over_call PROTO((struct z_candidate *, tree, int));
51 static tree convert_like PROTO((tree, tree));
52 static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
53 tree, const char *));
54 static tree build_object_call PROTO((tree, tree));
55 static tree resolve_args PROTO((tree));
56 static struct z_candidate * build_user_type_conversion_1
57 PROTO ((tree, tree, int));
58 static void print_z_candidates PROTO((struct z_candidate *));
59 static tree build_this PROTO((tree));
60 static struct z_candidate * splice_viable PROTO((struct z_candidate *));
61 static int any_viable PROTO((struct z_candidate *));
62 static struct z_candidate * add_template_candidate
63 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
64 unification_kind_t));
65 static struct z_candidate * add_template_candidate_real
66 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
67 tree, unification_kind_t));
68 static struct z_candidate * add_template_conv_candidate
69 PROTO((struct z_candidate *, tree, tree, tree, tree));
70 static struct z_candidate * add_builtin_candidates
71 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
72 tree, tree *, int));
73 static struct z_candidate * add_builtin_candidate
74 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
75 tree, tree, tree, tree *, tree *, int));
76 static int is_complete PROTO((tree));
77 static struct z_candidate * build_builtin_candidate
78 PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
79 int));
80 static struct z_candidate * add_conv_candidate
81 PROTO((struct z_candidate *, tree, tree, tree));
82 static struct z_candidate * add_function_candidate
83 PROTO((struct z_candidate *, tree, tree, int));
84 static tree implicit_conversion PROTO((tree, tree, tree, int));
85 static tree standard_conversion PROTO((tree, tree, tree));
86 static tree reference_binding PROTO((tree, tree, tree, int));
87 static tree non_reference PROTO((tree));
88 static tree build_conv PROTO((enum tree_code, tree, tree));
89 static int is_subseq PROTO((tree, tree));
90 static int maybe_handle_ref_bind PROTO((tree*, tree*));
91 static void maybe_handle_implicit_object PROTO((tree*));
92 static struct z_candidate * add_candidate PROTO((struct z_candidate *,
93 tree, tree, int));
94 static tree source_type PROTO((tree));
95 static void add_warning PROTO((struct z_candidate *, struct z_candidate *));
96 static int reference_related_p PROTO ((tree, tree));
97 static int reference_compatible_p PROTO ((tree, tree));
98 static tree convert_class_to_reference PROTO ((tree, tree, tree));
99 static tree direct_reference_binding PROTO ((tree, tree));
100 static int promoted_arithmetic_type_p PROTO ((tree));
101 static tree conditional_conversion PROTO ((tree, tree));
103 tree
104 build_vfield_ref (datum, type)
105 tree datum, type;
107 tree rval;
109 if (datum == error_mark_node)
110 return error_mark_node;
112 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
113 datum = convert_from_reference (datum);
115 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
116 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
117 datum, CLASSTYPE_VFIELD (type));
118 else
119 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
121 return rval;
124 /* Build a call to a member of an object. I.e., one that overloads
125 operator ()(), or is a pointer-to-function or pointer-to-method. */
127 static tree
128 build_field_call (basetype_path, instance_ptr, name, parms)
129 tree basetype_path, instance_ptr, name, parms;
131 tree field, instance;
133 if (name == ctor_identifier || name == dtor_identifier)
134 return NULL_TREE;
136 /* Speed up the common case. */
137 if (instance_ptr == current_class_ptr
138 && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
139 return NULL_TREE;
141 field = lookup_field (basetype_path, name, 1, 0);
143 if (field == error_mark_node || field == NULL_TREE)
144 return field;
146 if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
148 /* If it's a field, try overloading operator (),
149 or calling if the field is a pointer-to-function. */
150 instance = build_indirect_ref (instance_ptr, NULL_PTR);
151 instance = build_component_ref_1 (instance, field, 0);
153 if (instance == error_mark_node)
154 return error_mark_node;
156 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
157 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
158 instance, parms, NULL_TREE);
159 else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
161 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
162 return build_function_call (instance, parms);
163 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
164 == METHOD_TYPE)
165 return build_function_call
166 (instance, expr_tree_cons (NULL_TREE, instance_ptr, parms));
170 return NULL_TREE;
173 /* Returns nonzero iff the destructor name specified in NAME
174 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
175 forms... */
178 check_dtor_name (basetype, name)
179 tree basetype, name;
181 name = TREE_OPERAND (name, 0);
183 /* Just accept something we've already complained about. */
184 if (name == error_mark_node)
185 return 1;
187 if (TREE_CODE (name) == TYPE_DECL)
188 name = TREE_TYPE (name);
189 else if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
190 /* OK */;
191 else if (TREE_CODE (name) == IDENTIFIER_NODE)
193 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
194 || (TREE_CODE (basetype) == ENUMERAL_TYPE
195 && name == TYPE_IDENTIFIER (basetype)))
196 name = basetype;
197 else
198 name = get_type_value (name);
200 else
201 my_friendly_abort (980605);
203 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
204 return 1;
205 return 0;
208 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
209 This is how virtual function calls are avoided. */
211 tree
212 build_scoped_method_call (exp, basetype, name, parms)
213 tree exp, basetype, name, parms;
215 /* Because this syntactic form does not allow
216 a pointer to a base class to be `stolen',
217 we need not protect the derived->base conversion
218 that happens here.
220 @@ But we do have to check access privileges later. */
221 tree binfo, decl;
222 tree type = TREE_TYPE (exp);
224 if (type == error_mark_node
225 || basetype == error_mark_node)
226 return error_mark_node;
228 if (processing_template_decl)
230 if (TREE_CODE (name) == BIT_NOT_EXPR
231 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
233 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
234 if (type)
235 name = build_min_nt (BIT_NOT_EXPR, type);
237 name = build_min_nt (SCOPE_REF, basetype, name);
238 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
241 if (TREE_CODE (type) == REFERENCE_TYPE)
242 type = TREE_TYPE (type);
244 if (TREE_CODE (basetype) == TREE_VEC)
246 binfo = basetype;
247 basetype = BINFO_TYPE (binfo);
249 else
250 binfo = NULL_TREE;
252 /* Check the destructor call syntax. */
253 if (TREE_CODE (name) == BIT_NOT_EXPR)
255 /* We can get here if someone writes their destructor call like
256 `obj.NS::~T()'; this isn't really a scoped method call, so hand
257 it off. */
258 if (TREE_CODE (basetype) == NAMESPACE_DECL)
259 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
261 if (! check_dtor_name (basetype, name))
262 cp_error ("qualified type `%T' does not match destructor name `~%T'",
263 basetype, TREE_OPERAND (name, 0));
265 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
266 that explicit ~int is caught in the parser; this deals with typedefs
267 and template parms. */
268 if (! IS_AGGR_TYPE (basetype))
270 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
271 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
272 exp, basetype, type);
274 return cp_convert (void_type_node, exp);
278 if (! is_aggr_type (basetype, 1))
279 return error_mark_node;
281 if (! IS_AGGR_TYPE (type))
283 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
284 exp, type);
285 return error_mark_node;
288 if (! binfo)
290 binfo = get_binfo (basetype, type, 1);
291 if (binfo == error_mark_node)
292 return error_mark_node;
293 if (! binfo)
294 error_not_base_type (basetype, type);
297 if (binfo)
299 if (TREE_CODE (exp) == INDIRECT_REF)
300 decl = build_indirect_ref
301 (convert_pointer_to_real
302 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
303 else
304 decl = build_scoped_ref (exp, basetype);
306 /* Call to a destructor. */
307 if (TREE_CODE (name) == BIT_NOT_EXPR)
309 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
310 return cp_convert (void_type_node, exp);
312 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
313 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
317 /* Call to a method. */
318 return build_method_call (decl, name, parms, binfo,
319 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
321 return error_mark_node;
324 /* We want the address of a function or method. We avoid creating a
325 pointer-to-member function. */
327 tree
328 build_addr_func (function)
329 tree function;
331 tree type = TREE_TYPE (function);
333 /* We have to do these by hand to avoid real pointer to member
334 functions. */
335 if (TREE_CODE (type) == METHOD_TYPE)
337 tree addr;
339 type = build_pointer_type (type);
341 if (mark_addressable (function) == 0)
342 return error_mark_node;
344 addr = build1 (ADDR_EXPR, type, function);
346 /* Address of a static or external variable or function counts
347 as a constant */
348 if (staticp (function))
349 TREE_CONSTANT (addr) = 1;
351 function = addr;
353 else
354 function = default_conversion (function);
356 return function;
359 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
360 POINTER_TYPE to those. Note, pointer to member function types
361 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
363 tree
364 build_call (function, result_type, parms)
365 tree function, result_type, parms;
367 int is_constructor = 0;
368 tree tmp;
369 tree decl;
371 function = build_addr_func (function);
373 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
375 sorry ("unable to call pointer to member function here");
376 return error_mark_node;
379 if (TREE_CODE (function) == ADDR_EXPR
380 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
381 decl = TREE_OPERAND (function, 0);
382 else
383 decl = NULL_TREE;
385 if (decl && DECL_CONSTRUCTOR_P (decl))
386 is_constructor = 1;
388 if (decl)
389 my_friendly_assert (TREE_USED (decl), 990125);
391 /* Don't pass empty class objects by value. This is useful
392 for tags in STL, which are used to control overload resolution.
393 We don't need to handle other cases of copying empty classes. */
394 if (! decl || ! DECL_BUILT_IN (decl))
395 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
396 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
397 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
399 tree t = make_node (RTL_EXPR);
400 TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
401 RTL_EXPR_RTL (t) = const0_rtx;
402 RTL_EXPR_SEQUENCE (t) = NULL_RTX;
403 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
404 TREE_VALUE (tmp), t);
407 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
408 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
409 TREE_TYPE (function) = result_type;
410 TREE_SIDE_EFFECTS (function) = 1;
412 return function;
415 /* Build something of the form ptr->method (args)
416 or object.method (args). This can also build
417 calls to constructors, and find friends.
419 Member functions always take their class variable
420 as a pointer.
422 INSTANCE is a class instance.
424 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
426 PARMS help to figure out what that NAME really refers to.
428 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
429 down to the real instance type to use for access checking. We need this
430 information to get protected accesses correct. This parameter is used
431 by build_member_call.
433 FLAGS is the logical disjunction of zero or more LOOKUP_
434 flags. See cp-tree.h for more info.
436 If this is all OK, calls build_function_call with the resolved
437 member function.
439 This function must also handle being called to perform
440 initialization, promotion/coercion of arguments, and
441 instantiation of default parameters.
443 Note that NAME may refer to an instance variable name. If
444 `operator()()' is defined for the type of that field, then we return
445 that result. */
447 tree
448 build_method_call (instance, name, parms, basetype_path, flags)
449 tree instance, name, parms, basetype_path;
450 int flags;
452 tree basetype, instance_ptr;
454 #ifdef GATHER_STATISTICS
455 n_build_method_call++;
456 #endif
458 if (instance == error_mark_node
459 || name == error_mark_node
460 || parms == error_mark_node
461 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
462 return error_mark_node;
464 if (processing_template_decl)
466 /* We need to process template parm names here so that tsubst catches
467 them properly. Other type names can wait. */
468 if (TREE_CODE (name) == BIT_NOT_EXPR)
470 tree type = NULL_TREE;
472 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
473 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
474 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
475 type = TREE_TYPE (TREE_OPERAND (name, 0));
477 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
478 name = build_min_nt (BIT_NOT_EXPR, type);
481 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
484 if (TREE_CODE (name) == BIT_NOT_EXPR)
486 if (parms)
487 error ("destructors take no parameters");
488 basetype = TREE_TYPE (instance);
489 if (TREE_CODE (basetype) == REFERENCE_TYPE)
490 basetype = TREE_TYPE (basetype);
492 if (! check_dtor_name (basetype, name))
493 cp_error
494 ("destructor name `~%T' does not match type `%T' of expression",
495 TREE_OPERAND (name, 0), basetype);
497 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
498 return cp_convert (void_type_node, instance);
499 instance = default_conversion (instance);
500 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
501 return build_delete (build_pointer_type (basetype),
502 instance_ptr, integer_two_node,
503 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
506 return build_new_method_call (instance, name, parms, basetype_path, flags);
509 /* New overloading code. */
511 struct z_candidate {
512 tree fn;
513 tree convs;
514 tree second_conv;
515 int viable;
516 tree basetype_path;
517 tree template;
518 tree warnings;
519 struct z_candidate *next;
522 #define IDENTITY_RANK 0
523 #define EXACT_RANK 1
524 #define PROMO_RANK 2
525 #define STD_RANK 3
526 #define PBOOL_RANK 4
527 #define USER_RANK 5
528 #define ELLIPSIS_RANK 6
529 #define BAD_RANK 7
531 #define ICS_RANK(NODE) \
532 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
533 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
534 : ICS_USER_FLAG (NODE) ? USER_RANK \
535 : ICS_STD_RANK (NODE))
537 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
539 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
540 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
541 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
542 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
544 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
545 should be created to hold the result of the conversion. */
546 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
548 #define USER_CONV_CAND(NODE) \
549 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
550 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
553 null_ptr_cst_p (t)
554 tree t;
556 /* [conv.ptr]
558 A null pointer constant is an integral constant expression
559 (_expr.const_) rvalue of integer type that evaluates to zero. */
560 if (t == null_node
561 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
562 return 1;
563 return 0;
566 static tree
567 build_conv (code, type, from)
568 enum tree_code code;
569 tree type, from;
571 tree t = build1 (code, type, from);
572 int rank = ICS_STD_RANK (from);
573 switch (code)
575 case PTR_CONV:
576 case PMEM_CONV:
577 case BASE_CONV:
578 case STD_CONV:
579 if (rank < STD_RANK)
580 rank = STD_RANK;
581 break;
583 case QUAL_CONV:
584 if (rank < EXACT_RANK)
585 rank = EXACT_RANK;
587 default:
588 break;
590 ICS_STD_RANK (t) = rank;
591 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
592 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
593 return t;
596 static tree
597 non_reference (t)
598 tree t;
600 if (TREE_CODE (t) == REFERENCE_TYPE)
601 t = TREE_TYPE (t);
602 return t;
605 tree
606 strip_top_quals (t)
607 tree t;
609 if (TREE_CODE (t) == ARRAY_TYPE)
610 return t;
611 return TYPE_MAIN_VARIANT (t);
614 /* Returns the standard conversion path (see [conv]) from type FROM to type
615 TO, if any. For proper handling of null pointer constants, you must
616 also pass the expression EXPR to convert from. */
618 static tree
619 standard_conversion (to, from, expr)
620 tree to, from, expr;
622 enum tree_code fcode, tcode;
623 tree conv;
624 int fromref = 0;
626 if (TREE_CODE (to) == REFERENCE_TYPE)
627 to = TREE_TYPE (to);
628 if (TREE_CODE (from) == REFERENCE_TYPE)
630 fromref = 1;
631 from = TREE_TYPE (from);
633 to = strip_top_quals (to);
634 from = strip_top_quals (from);
636 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
637 && expr && type_unknown_p (expr))
639 expr = instantiate_type (to, expr, 0);
640 if (expr == error_mark_node)
641 return NULL_TREE;
642 from = TREE_TYPE (expr);
645 fcode = TREE_CODE (from);
646 tcode = TREE_CODE (to);
648 conv = build1 (IDENTITY_CONV, from, expr);
650 if (fcode == FUNCTION_TYPE)
652 from = build_pointer_type (from);
653 fcode = TREE_CODE (from);
654 conv = build_conv (LVALUE_CONV, from, conv);
656 else if (fcode == ARRAY_TYPE)
658 from = build_pointer_type (TREE_TYPE (from));
659 fcode = TREE_CODE (from);
660 conv = build_conv (LVALUE_CONV, from, conv);
662 else if (fromref || (expr && real_lvalue_p (expr)))
663 conv = build_conv (RVALUE_CONV, from, conv);
665 if (same_type_p (from, to))
666 return conv;
668 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
669 && expr && null_ptr_cst_p (expr))
671 conv = build_conv (STD_CONV, to, conv);
673 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
675 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
676 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
678 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
679 TYPE_MAIN_VARIANT (TREE_TYPE (to))))
681 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
682 && ufcode != FUNCTION_TYPE)
684 from = build_pointer_type
685 (cp_build_qualified_type (void_type_node,
686 CP_TYPE_QUALS (TREE_TYPE (from))));
687 conv = build_conv (PTR_CONV, from, conv);
689 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
691 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
692 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
694 if (DERIVED_FROM_P (fbase, tbase)
695 && (same_type_p
696 (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
697 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))))))
699 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
700 from = build_pointer_type (from);
701 conv = build_conv (PMEM_CONV, from, conv);
704 else if (IS_AGGR_TYPE (TREE_TYPE (from))
705 && IS_AGGR_TYPE (TREE_TYPE (to)))
707 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
709 from =
710 cp_build_qualified_type (TREE_TYPE (to),
711 CP_TYPE_QUALS (TREE_TYPE (from)));
712 from = build_pointer_type (from);
713 conv = build_conv (PTR_CONV, from, conv);
717 if (same_type_p (from, to))
718 /* OK */;
719 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
720 conv = build_conv (QUAL_CONV, to, conv);
721 else if (expr && string_conv_p (to, expr, 0))
722 /* converting from string constant to char *. */
723 conv = build_conv (QUAL_CONV, to, conv);
724 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
726 conv = build_conv (PTR_CONV, to, conv);
727 ICS_BAD_FLAG (conv) = 1;
729 else
730 return 0;
732 from = to;
734 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
736 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
737 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
738 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
739 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
741 if (! DERIVED_FROM_P (fbase, tbase)
742 || ! same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
743 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
744 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
745 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
746 return 0;
748 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
749 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
750 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
751 from = build_ptrmemfunc_type (build_pointer_type (from));
752 conv = build_conv (PMEM_CONV, from, conv);
754 else if (tcode == BOOLEAN_TYPE)
756 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
757 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
758 return 0;
760 conv = build_conv (STD_CONV, to, conv);
761 if (fcode == POINTER_TYPE
762 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
763 ICS_STD_RANK (conv) = PBOOL_RANK;
765 /* We don't check for ENUMERAL_TYPE here because there are no standard
766 conversions to enum type. */
767 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
768 || tcode == REAL_TYPE)
770 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
771 return 0;
772 conv = build_conv (STD_CONV, to, conv);
774 /* Give this a better rank if it's a promotion. */
775 if (to == type_promotes_to (from)
776 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
777 ICS_STD_RANK (conv) = PROMO_RANK;
779 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
780 && is_properly_derived_from (from, to))
782 if (TREE_CODE (conv) == RVALUE_CONV)
783 conv = TREE_OPERAND (conv, 0);
784 conv = build_conv (BASE_CONV, to, conv);
785 /* The derived-to-base conversion indicates the initialization
786 of a parameter with base type from an object of a derived
787 type. A temporary object is created to hold the result of
788 the conversion. */
789 NEED_TEMPORARY_P (conv) = 1;
791 else
792 return 0;
794 return conv;
797 /* Returns non-zero if T1 is reference-related to T2. */
799 static int
800 reference_related_p (t1, t2)
801 tree t1;
802 tree t2;
804 t1 = TYPE_MAIN_VARIANT (t1);
805 t2 = TYPE_MAIN_VARIANT (t2);
807 /* [dcl.init.ref]
809 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
810 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
811 of T2. */
812 return (same_type_p (t1, t2)
813 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
814 && DERIVED_FROM_P (t1, t2)));
817 /* Returns non-zero if T1 is reference-compatible with T2. */
819 static int
820 reference_compatible_p (t1, t2)
821 tree t1;
822 tree t2;
824 /* [dcl.init.ref]
826 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
827 reference-related to T2 and cv1 is the same cv-qualification as,
828 or greater cv-qualification than, cv2. */
829 return (reference_related_p (t1, t2)
830 && at_least_as_qualified_p (t1, t2));
833 /* Determine whether or not the EXPR (of class type S) can be
834 converted to T as in [over.match.ref]. */
836 static tree
837 convert_class_to_reference (t, s, expr)
838 tree t;
839 tree s;
840 tree expr;
842 tree conversions;
843 tree arglist;
844 tree conv;
845 struct z_candidate *candidates;
846 struct z_candidate *cand;
848 /* [over.match.ref]
850 Assuming that "cv1 T" is the underlying type of the reference
851 being initialized, and "cv S" is the type of the initializer
852 expression, with S a class type, the candidate functions are
853 selected as follows:
855 --The conversion functions of S and its base classes are
856 considered. Those that are not hidden within S and yield type
857 "reference to cv2 T2", where "cv1 T" is reference-compatible
858 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
860 The argument list has one argument, which is the initializer
861 expression. */
863 candidates = 0;
865 /* Conceptually, we should take the address of EXPR and put it in
866 the argument list. Unfortunately, however, that can result in
867 error messages, which we should not issue now because we are just
868 trying to find a conversion operator. Therefore, we use NULL,
869 cast to the appropriate type. */
870 arglist = build_int_2 (0, 0);
871 TREE_TYPE (arglist) = build_pointer_type (s);
872 arglist = build_scratch_list (NULL_TREE, arglist);
874 for (conversions = lookup_conversions (s);
875 conversions;
876 conversions = TREE_CHAIN (conversions))
878 tree fns = TREE_VALUE (conversions);
880 while (fns)
882 tree f = OVL_CURRENT (fns);
883 tree t2 = TREE_TYPE (TREE_TYPE (f));
884 struct z_candidate *old_candidates = candidates;
886 /* If this is a template function, try to get an exact
887 match. */
888 if (TREE_CODE (f) == TEMPLATE_DECL)
890 candidates
891 = add_template_candidate (candidates,
893 NULL_TREE,
894 arglist,
895 build_reference_type (t),
896 LOOKUP_NORMAL,
897 DEDUCE_CONV);
899 if (candidates != old_candidates)
901 /* Now, see if the conversion function really returns
902 an lvalue of the appropriate type. From the
903 point of view of unification, simply returning an
904 rvalue of the right type is good enough. */
905 f = candidates->fn;
906 t2 = TREE_TYPE (TREE_TYPE (f));
907 if (TREE_CODE (t2) != REFERENCE_TYPE
908 || !reference_compatible_p (t, TREE_TYPE (t2)))
909 candidates = candidates->next;
912 else if (TREE_CODE (t2) == REFERENCE_TYPE
913 && reference_compatible_p (t, TREE_TYPE (t2)))
914 candidates
915 = add_function_candidate (candidates, f, arglist,
916 LOOKUP_NORMAL);
918 if (candidates != old_candidates)
919 candidates->basetype_path = TREE_PURPOSE (conversions);
921 fns = OVL_NEXT (fns);
925 /* If none of the conversion functions worked out, let our caller
926 know. */
927 if (!any_viable (candidates))
928 return NULL_TREE;
930 candidates = splice_viable (candidates);
931 cand = tourney (candidates);
932 if (!cand)
933 return NULL_TREE;
935 conv = build_conv (IDENTITY_CONV, s, expr);
936 conv = build_conv (USER_CONV,
937 non_reference (TREE_TYPE (TREE_TYPE (cand->fn))),
938 expr);
939 TREE_OPERAND (conv, 1) = build_expr_ptr_wrapper (cand);
940 ICS_USER_FLAG (conv) = 1;
941 if (cand->viable == -1)
942 ICS_BAD_FLAG (conv) = 1;
943 cand->second_conv = conv;
945 return conv;
948 /* A reference of the indicated TYPE is being bound directly to the
949 expression represented by the implicit conversion sequence CONV.
950 Return a conversion sequence for this binding. */
952 static tree
953 direct_reference_binding (type, conv)
954 tree type;
955 tree conv;
957 tree t = TREE_TYPE (type);
959 /* [over.ics.rank]
961 When a parameter of reference type binds directly
962 (_dcl.init.ref_) to an argument expression, the implicit
963 conversion sequence is the identity conversion, unless the
964 argument expression has a type that is a derived class of the
965 parameter type, in which case the implicit conversion sequence is
966 a derived-to-base Conversion.
968 If the parameter binds directly to the result of applying a
969 conversion function to the argument expression, the implicit
970 conversion sequence is a user-defined conversion sequence
971 (_over.ics.user_), with the second standard conversion sequence
972 either an identity conversion or, if the conversion function
973 returns an entity of a type that is a derived class of the
974 parameter type, a derived-to-base conversion. */
975 if (!same_type_p (TYPE_MAIN_VARIANT (t),
976 TYPE_MAIN_VARIANT (TREE_TYPE (conv))))
978 /* Represent the derived-to-base conversion. */
979 conv = build_conv (BASE_CONV, t, conv);
980 /* We will actually be binding to the base-class subobject in
981 the derived class, so we mark this conversion appropriately.
982 That way, convert_like knows not to generate a temporary. */
983 NEED_TEMPORARY_P (conv) = 0;
985 return build_conv (REF_BIND, type, conv);
988 /* Returns the conversion path from type FROM to reference type TO for
989 purposes of reference binding. For lvalue binding, either pass a
990 reference type to FROM or an lvalue expression to EXPR.
992 Currently does not distinguish in the generated trees between binding to
993 an lvalue and a temporary. Should it? */
995 static tree
996 reference_binding (rto, rfrom, expr, flags)
997 tree rto, rfrom, expr;
998 int flags;
1000 tree conv = NULL_TREE;
1001 tree to = TREE_TYPE (rto);
1002 tree from = rfrom;
1003 int related_p;
1004 int compatible_p;
1005 cp_lvalue_kind lvalue_p = clk_none;
1007 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1009 expr = instantiate_type (to, expr, 0);
1010 if (expr == error_mark_node)
1011 return NULL_TREE;
1012 from = TREE_TYPE (expr);
1015 if (TREE_CODE (from) == REFERENCE_TYPE)
1017 /* Anything with reference type is an lvalue. */
1018 lvalue_p = clk_ordinary;
1019 from = TREE_TYPE (from);
1021 else if (expr)
1022 lvalue_p = real_lvalue_p (expr);
1024 /* Figure out whether or not the types are reference-related and
1025 reference compatible. We have do do this after stripping
1026 references from FROM. */
1027 related_p = reference_related_p (to, from);
1028 compatible_p = reference_compatible_p (to, from);
1030 if (lvalue_p && compatible_p)
1032 /* [dcl.init.ref]
1034 If the intializer expression
1036 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1037 is reference-compatible with "cv2 T2,"
1039 the reference is bound directly to the initializer exprssion
1040 lvalue. */
1041 conv = build1 (IDENTITY_CONV, from, expr);
1042 conv = direct_reference_binding (rto, conv);
1043 if ((lvalue_p & clk_bitfield) != 0
1044 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1045 /* For the purposes of overload resolution, we ignore the fact
1046 this expression is a bitfield. (In particular,
1047 [over.ics.ref] says specifically that a function with a
1048 non-const reference parameter is viable even if the
1049 argument is a bitfield.)
1051 However, when we actually call the function we must create
1052 a temporary to which to bind the reference. If the
1053 reference is volatile, or isn't const, then we cannot make
1054 a temporary, so we just issue an error when the conversion
1055 actually occurs. */
1056 NEED_TEMPORARY_P (conv) = 1;
1057 return conv;
1059 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1061 /* [dcl.init.ref]
1063 If the initializer exprsesion
1065 -- has a class type (i.e., T2 is a class type) can be
1066 implicitly converted to an lvalue of type "cv3 T3," where
1067 "cv1 T1" is reference-compatible with "cv3 T3". (this
1068 conversion is selected by enumerating the applicable
1069 conversion functions (_over.match.ref_) and choosing the
1070 best one through overload resolution. (_over.match_).
1072 the reference is bound to the lvalue result of the conversion
1073 in the second case. */
1074 conv = convert_class_to_reference (to, from, expr);
1075 if (conv)
1076 return direct_reference_binding (rto, conv);
1079 /* From this point on, we conceptually need temporaries, even if we
1080 elide them. Only the cases above are "direct bindings". */
1081 if (flags & LOOKUP_NO_TEMP_BIND)
1082 return NULL_TREE;
1084 /* [over.ics.rank]
1086 When a parameter of reference type is not bound directly to an
1087 argument expression, the conversion sequence is the one required
1088 to convert the argument expression to the underlying type of the
1089 reference according to _over.best.ics_. Conceptually, this
1090 conversion sequence corresponds to copy-initializing a temporary
1091 of the underlying type with the argument expression. Any
1092 difference in top-level cv-qualification is subsumed by the
1093 initialization itself and does not constitute a conversion. */
1095 /* [dcl.init.ref]
1097 Otherwise, the reference shall be to a non-volatile const type. */
1098 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1099 return NULL_TREE;
1101 /* [dcl.init.ref]
1103 If the initializer expression is an rvalue, with T2 a class type,
1104 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1105 is bound in one of the following ways:
1107 -- The reference is bound to the object represented by the rvalue
1108 or to a sub-object within that object.
1110 In this case, the implicit conversion sequence is supposed to be
1111 same as we would obtain by generating a temporary. Fortunately,
1112 if the types are reference compatible, then this is either an
1113 identity conversion or the derived-to-base conversion, just as
1114 for direct binding. */
1115 if (CLASS_TYPE_P (from) && compatible_p)
1117 conv = build1 (IDENTITY_CONV, from, expr);
1118 return direct_reference_binding (rto, conv);
1121 /* [dcl.init.ref]
1123 Otherwise, a temporary of type "cv1 T1" is created and
1124 initialized from the initializer expression using the rules for a
1125 non-reference copy initialization. If T1 is reference-related to
1126 T2, cv1 must be the same cv-qualification as, or greater
1127 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1128 if (related_p && !at_least_as_qualified_p (to, from))
1129 return NULL_TREE;
1131 conv = implicit_conversion (to, from, expr, flags);
1132 if (!conv)
1133 return NULL_TREE;
1135 conv = build_conv (REF_BIND, rto, conv);
1136 /* This reference binding, unlike those above, requires the
1137 creation of a temporary. */
1138 NEED_TEMPORARY_P (conv) = 1;
1140 return conv;
1143 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1144 to type TO. The optional expression EXPR may affect the conversion.
1145 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1146 significant. */
1148 static tree
1149 implicit_conversion (to, from, expr, flags)
1150 tree to, from, expr;
1151 int flags;
1153 tree conv;
1154 struct z_candidate *cand;
1156 complete_type (from);
1157 complete_type (to);
1159 if (TREE_CODE (to) == REFERENCE_TYPE)
1160 conv = reference_binding (to, from, expr, flags);
1161 else
1162 conv = standard_conversion (to, from, expr);
1164 if (conv)
1166 else if (expr != NULL_TREE
1167 && (IS_AGGR_TYPE (non_reference (from))
1168 || IS_AGGR_TYPE (non_reference (to)))
1169 && (flags & LOOKUP_NO_CONVERSION) == 0)
1171 cand = build_user_type_conversion_1
1172 (to, expr, LOOKUP_ONLYCONVERTING);
1173 if (cand)
1174 conv = cand->second_conv;
1175 if ((! conv || ICS_BAD_FLAG (conv))
1176 && TREE_CODE (to) == REFERENCE_TYPE
1177 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1179 cand = build_user_type_conversion_1
1180 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1181 if (cand)
1183 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to)))
1184 ICS_BAD_FLAG (cand->second_conv) = 1;
1185 if (!conv || (ICS_BAD_FLAG (conv)
1186 > ICS_BAD_FLAG (cand->second_conv)))
1187 conv = build_conv (REF_BIND, to, cand->second_conv);
1192 return conv;
1195 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1196 functions. */
1198 static struct z_candidate *
1199 add_candidate (candidates, fn, convs, viable)
1200 struct z_candidate *candidates;
1201 tree fn, convs;
1202 int viable;
1204 struct z_candidate *cand
1205 = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
1207 cand->fn = fn;
1208 cand->convs = convs;
1209 cand->second_conv = NULL_TREE;
1210 cand->viable = viable;
1211 cand->basetype_path = NULL_TREE;
1212 cand->template = NULL_TREE;
1213 cand->warnings = NULL_TREE;
1214 cand->next = candidates;
1216 return cand;
1219 /* Create an overload candidate for the function or method FN called with
1220 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1221 to implicit_conversion. */
1223 static struct z_candidate *
1224 add_function_candidate (candidates, fn, arglist, flags)
1225 struct z_candidate *candidates;
1226 tree fn, arglist;
1227 int flags;
1229 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1230 int i, len;
1231 tree convs;
1232 tree parmnode, argnode;
1233 int viable = 1;
1235 /* The `this' and `in_chrg' arguments to constructors are not considered
1236 in overload resolution. */
1237 if (DECL_CONSTRUCTOR_P (fn))
1239 parmlist = TREE_CHAIN (parmlist);
1240 arglist = TREE_CHAIN (arglist);
1241 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1243 parmlist = TREE_CHAIN (parmlist);
1244 arglist = TREE_CHAIN (arglist);
1248 len = list_length (arglist);
1249 convs = make_scratch_vec (len);
1251 /* 13.3.2 - Viable functions [over.match.viable]
1252 First, to be a viable function, a candidate function shall have enough
1253 parameters to agree in number with the arguments in the list.
1255 We need to check this first; otherwise, checking the ICSes might cause
1256 us to produce an ill-formed template instantiation. */
1258 parmnode = parmlist;
1259 for (i = 0; i < len; ++i)
1261 if (parmnode == NULL_TREE || parmnode == void_list_node)
1262 break;
1263 parmnode = TREE_CHAIN (parmnode);
1266 if (i < len && parmnode)
1267 viable = 0;
1269 /* Make sure there are default args for the rest of the parms. */
1270 else for (; parmnode && parmnode != void_list_node;
1271 parmnode = TREE_CHAIN (parmnode))
1272 if (! TREE_PURPOSE (parmnode))
1274 viable = 0;
1275 break;
1278 if (! viable)
1279 goto out;
1281 /* Second, for F to be a viable function, there shall exist for each
1282 argument an implicit conversion sequence that converts that argument
1283 to the corresponding parameter of F. */
1285 parmnode = parmlist;
1286 argnode = arglist;
1288 for (i = 0; i < len; ++i)
1290 tree arg = TREE_VALUE (argnode);
1291 tree argtype = lvalue_type (arg);
1292 tree t;
1294 if (parmnode == void_list_node)
1295 break;
1297 if (parmnode)
1299 tree parmtype = TREE_VALUE (parmnode);
1301 /* [over.match.funcs] For conversion functions, the function is
1302 considered to be a member of the class of the implicit object
1303 argument for the purpose of defining the type of the implicit
1304 object parameter.
1306 Since build_over_call ignores the ICS for the `this' parameter,
1307 we can just change the parm type. */
1308 if (DECL_CONV_FN_P (fn) && i == 0)
1310 parmtype
1311 = build_qualified_type (TREE_TYPE (argtype),
1312 TYPE_QUALS (TREE_TYPE (parmtype)));
1313 parmtype = build_pointer_type (parmtype);
1316 t = implicit_conversion (parmtype, argtype, arg, flags);
1318 else
1320 t = build1 (IDENTITY_CONV, argtype, arg);
1321 ICS_ELLIPSIS_FLAG (t) = 1;
1324 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1325 && ! DECL_CONSTRUCTOR_P (fn))
1326 ICS_THIS_FLAG (t) = 1;
1328 TREE_VEC_ELT (convs, i) = t;
1329 if (! t)
1331 viable = 0;
1332 break;
1335 if (ICS_BAD_FLAG (t))
1336 viable = -1;
1338 if (parmnode)
1339 parmnode = TREE_CHAIN (parmnode);
1340 argnode = TREE_CHAIN (argnode);
1343 out:
1344 return add_candidate (candidates, fn, convs, viable);
1347 /* Create an overload candidate for the conversion function FN which will
1348 be invoked for expression OBJ, producing a pointer-to-function which
1349 will in turn be called with the argument list ARGLIST, and add it to
1350 CANDIDATES. FLAGS is passed on to implicit_conversion.
1352 Actually, we don't really care about FN; we care about the type it
1353 converts to. There may be multiple conversion functions that will
1354 convert to that type, and we rely on build_user_type_conversion_1 to
1355 choose the best one; so when we create our candidate, we record the type
1356 instead of the function. */
1358 static struct z_candidate *
1359 add_conv_candidate (candidates, fn, obj, arglist)
1360 struct z_candidate *candidates;
1361 tree fn, obj, arglist;
1363 tree totype = TREE_TYPE (TREE_TYPE (fn));
1364 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1365 int i, len = list_length (arglist) + 1;
1366 tree convs = make_scratch_vec (len);
1367 tree parmnode = parmlist;
1368 tree argnode = arglist;
1369 int viable = 1;
1370 int flags = LOOKUP_NORMAL;
1372 /* Don't bother looking up the same type twice. */
1373 if (candidates && candidates->fn == totype)
1374 return candidates;
1376 for (i = 0; i < len; ++i)
1378 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1379 tree argtype = lvalue_type (arg);
1380 tree t;
1382 if (i == 0)
1383 t = implicit_conversion (totype, argtype, arg, flags);
1384 else if (parmnode == void_list_node)
1385 break;
1386 else if (parmnode)
1387 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1388 else
1390 t = build1 (IDENTITY_CONV, argtype, arg);
1391 ICS_ELLIPSIS_FLAG (t) = 1;
1394 TREE_VEC_ELT (convs, i) = t;
1395 if (! t)
1396 break;
1398 if (ICS_BAD_FLAG (t))
1399 viable = -1;
1401 if (i == 0)
1402 continue;
1404 if (parmnode)
1405 parmnode = TREE_CHAIN (parmnode);
1406 argnode = TREE_CHAIN (argnode);
1409 if (i < len)
1410 viable = 0;
1412 for (; parmnode && parmnode != void_list_node;
1413 parmnode = TREE_CHAIN (parmnode))
1414 if (! TREE_PURPOSE (parmnode))
1416 viable = 0;
1417 break;
1420 return add_candidate (candidates, totype, convs, viable);
1423 static struct z_candidate *
1424 build_builtin_candidate (candidates, fnname, type1, type2,
1425 args, argtypes, flags)
1426 struct z_candidate *candidates;
1427 tree fnname, type1, type2, *args, *argtypes;
1428 int flags;
1431 tree t, convs;
1432 int viable = 1, i;
1433 tree types[2];
1435 types[0] = type1;
1436 types[1] = type2;
1438 convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1440 for (i = 0; i < 2; ++i)
1442 if (! args[i])
1443 break;
1445 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1446 if (! t)
1448 viable = 0;
1449 /* We need something for printing the candidate. */
1450 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1452 else if (ICS_BAD_FLAG (t))
1453 viable = 0;
1454 TREE_VEC_ELT (convs, i) = t;
1457 /* For COND_EXPR we rearranged the arguments; undo that now. */
1458 if (args[2])
1460 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1461 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1462 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1463 if (t)
1464 TREE_VEC_ELT (convs, 0) = t;
1465 else
1466 viable = 0;
1469 return add_candidate (candidates, fnname, convs, viable);
1472 static int
1473 is_complete (t)
1474 tree t;
1476 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1479 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1481 static int
1482 promoted_arithmetic_type_p (type)
1483 tree type;
1485 /* [over.built]
1487 In this section, the term promoted integral type is used to refer
1488 to those integral types which are preserved by integral promotion
1489 (including e.g. int and long but excluding e.g. char).
1490 Similarly, the term promoted arithmetic type refers to promoted
1491 integral types plus floating types. */
1492 return ((INTEGRAL_TYPE_P (type)
1493 && same_type_p (type_promotes_to (type), type))
1494 || TREE_CODE (type) == REAL_TYPE);
1497 /* Create any builtin operator overload candidates for the operator in
1498 question given the converted operand types TYPE1 and TYPE2. The other
1499 args are passed through from add_builtin_candidates to
1500 build_builtin_candidate. */
1502 static struct z_candidate *
1503 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1504 args, argtypes, flags)
1505 struct z_candidate *candidates;
1506 enum tree_code code, code2;
1507 tree fnname, type1, type2, *args, *argtypes;
1508 int flags;
1510 switch (code)
1512 case POSTINCREMENT_EXPR:
1513 case POSTDECREMENT_EXPR:
1514 args[1] = integer_zero_node;
1515 type2 = integer_type_node;
1516 break;
1517 default:
1518 break;
1521 switch (code)
1524 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1525 and VQ is either volatile or empty, there exist candidate operator
1526 functions of the form
1527 VQ T& operator++(VQ T&);
1528 T operator++(VQ T&, int);
1529 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1530 type other than bool, and VQ is either volatile or empty, there exist
1531 candidate operator functions of the form
1532 VQ T& operator--(VQ T&);
1533 T operator--(VQ T&, int);
1534 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1535 complete object type, and VQ is either volatile or empty, there exist
1536 candidate operator functions of the form
1537 T*VQ& operator++(T*VQ&);
1538 T*VQ& operator--(T*VQ&);
1539 T* operator++(T*VQ&, int);
1540 T* operator--(T*VQ&, int); */
1542 case POSTDECREMENT_EXPR:
1543 case PREDECREMENT_EXPR:
1544 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1545 return candidates;
1546 case POSTINCREMENT_EXPR:
1547 case PREINCREMENT_EXPR:
1548 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1549 || TYPE_PTROB_P (type1))
1551 type1 = build_reference_type (type1);
1552 break;
1554 return candidates;
1556 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1557 exist candidate operator functions of the form
1559 T& operator*(T*);
1561 8 For every function type T, there exist candidate operator functions of
1562 the form
1563 T& operator*(T*); */
1565 case INDIRECT_REF:
1566 if (TREE_CODE (type1) == POINTER_TYPE
1567 && (TYPE_PTROB_P (type1)
1568 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1569 break;
1570 return candidates;
1572 /* 9 For every type T, there exist candidate operator functions of the form
1573 T* operator+(T*);
1575 10For every promoted arithmetic type T, there exist candidate operator
1576 functions of the form
1577 T operator+(T);
1578 T operator-(T); */
1580 case CONVERT_EXPR: /* unary + */
1581 if (TREE_CODE (type1) == POINTER_TYPE
1582 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1583 break;
1584 case NEGATE_EXPR:
1585 if (ARITHMETIC_TYPE_P (type1))
1586 break;
1587 return candidates;
1589 /* 11For every promoted integral type T, there exist candidate operator
1590 functions of the form
1591 T operator~(T); */
1593 case BIT_NOT_EXPR:
1594 if (INTEGRAL_TYPE_P (type1))
1595 break;
1596 return candidates;
1598 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1599 is the same type as C2 or is a derived class of C2, T is a complete
1600 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1601 there exist candidate operator functions of the form
1602 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1603 where CV12 is the union of CV1 and CV2. */
1605 case MEMBER_REF:
1606 if (TREE_CODE (type1) == POINTER_TYPE
1607 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1609 tree c1 = TREE_TYPE (type1);
1610 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1611 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1612 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1614 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1615 && (TYPE_PTRMEMFUNC_P (type2)
1616 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1617 break;
1619 return candidates;
1621 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1622 didate operator functions of the form
1623 LR operator*(L, R);
1624 LR operator/(L, R);
1625 LR operator+(L, R);
1626 LR operator-(L, R);
1627 bool operator<(L, R);
1628 bool operator>(L, R);
1629 bool operator<=(L, R);
1630 bool operator>=(L, R);
1631 bool operator==(L, R);
1632 bool operator!=(L, R);
1633 where LR is the result of the usual arithmetic conversions between
1634 types L and R.
1636 14For every pair of types T and I, where T is a cv-qualified or cv-
1637 unqualified complete object type and I is a promoted integral type,
1638 there exist candidate operator functions of the form
1639 T* operator+(T*, I);
1640 T& operator[](T*, I);
1641 T* operator-(T*, I);
1642 T* operator+(I, T*);
1643 T& operator[](I, T*);
1645 15For every T, where T is a pointer to complete object type, there exist
1646 candidate operator functions of the form112)
1647 ptrdiff_t operator-(T, T);
1649 16For every pointer type T, there exist candidate operator functions of
1650 the form
1651 bool operator<(T, T);
1652 bool operator>(T, T);
1653 bool operator<=(T, T);
1654 bool operator>=(T, T);
1655 bool operator==(T, T);
1656 bool operator!=(T, T);
1658 17For every pointer to member type T, there exist candidate operator
1659 functions of the form
1660 bool operator==(T, T);
1661 bool operator!=(T, T); */
1663 case MINUS_EXPR:
1664 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1665 break;
1666 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1668 type2 = ptrdiff_type_node;
1669 break;
1671 case MULT_EXPR:
1672 case TRUNC_DIV_EXPR:
1673 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1674 break;
1675 return candidates;
1677 case EQ_EXPR:
1678 case NE_EXPR:
1679 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1680 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1681 break;
1682 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1683 && null_ptr_cst_p (args[1]))
1685 type2 = type1;
1686 break;
1688 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1689 && null_ptr_cst_p (args[0]))
1691 type1 = type2;
1692 break;
1694 case LT_EXPR:
1695 case GT_EXPR:
1696 case LE_EXPR:
1697 case GE_EXPR:
1698 case MAX_EXPR:
1699 case MIN_EXPR:
1700 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1701 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1702 break;
1703 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1705 type2 = type1;
1706 break;
1708 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1710 type1 = type2;
1711 break;
1713 return candidates;
1715 case PLUS_EXPR:
1716 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1717 break;
1718 case ARRAY_REF:
1719 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1721 type1 = ptrdiff_type_node;
1722 break;
1724 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1726 type2 = ptrdiff_type_node;
1727 break;
1729 return candidates;
1731 /* 18For every pair of promoted integral types L and R, there exist candi-
1732 date operator functions of the form
1733 LR operator%(L, R);
1734 LR operator&(L, R);
1735 LR operator^(L, R);
1736 LR operator|(L, R);
1737 L operator<<(L, R);
1738 L operator>>(L, R);
1739 where LR is the result of the usual arithmetic conversions between
1740 types L and R. */
1742 case TRUNC_MOD_EXPR:
1743 case BIT_AND_EXPR:
1744 case BIT_IOR_EXPR:
1745 case BIT_XOR_EXPR:
1746 case LSHIFT_EXPR:
1747 case RSHIFT_EXPR:
1748 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1749 break;
1750 return candidates;
1752 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1753 type, VQ is either volatile or empty, and R is a promoted arithmetic
1754 type, there exist candidate operator functions of the form
1755 VQ L& operator=(VQ L&, R);
1756 VQ L& operator*=(VQ L&, R);
1757 VQ L& operator/=(VQ L&, R);
1758 VQ L& operator+=(VQ L&, R);
1759 VQ L& operator-=(VQ L&, R);
1761 20For every pair T, VQ), where T is any type and VQ is either volatile
1762 or empty, there exist candidate operator functions of the form
1763 T*VQ& operator=(T*VQ&, T*);
1765 21For every pair T, VQ), where T is a pointer to member type and VQ is
1766 either volatile or empty, there exist candidate operator functions of
1767 the form
1768 VQ T& operator=(VQ T&, T);
1770 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1771 unqualified complete object type, VQ is either volatile or empty, and
1772 I is a promoted integral type, there exist candidate operator func-
1773 tions of the form
1774 T*VQ& operator+=(T*VQ&, I);
1775 T*VQ& operator-=(T*VQ&, I);
1777 23For every triple L, VQ, R), where L is an integral or enumeration
1778 type, VQ is either volatile or empty, and R is a promoted integral
1779 type, there exist candidate operator functions of the form
1781 VQ L& operator%=(VQ L&, R);
1782 VQ L& operator<<=(VQ L&, R);
1783 VQ L& operator>>=(VQ L&, R);
1784 VQ L& operator&=(VQ L&, R);
1785 VQ L& operator^=(VQ L&, R);
1786 VQ L& operator|=(VQ L&, R); */
1788 case MODIFY_EXPR:
1789 switch (code2)
1791 case PLUS_EXPR:
1792 case MINUS_EXPR:
1793 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1795 type2 = ptrdiff_type_node;
1796 break;
1798 case MULT_EXPR:
1799 case TRUNC_DIV_EXPR:
1800 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1801 break;
1802 return candidates;
1804 case TRUNC_MOD_EXPR:
1805 case BIT_AND_EXPR:
1806 case BIT_IOR_EXPR:
1807 case BIT_XOR_EXPR:
1808 case LSHIFT_EXPR:
1809 case RSHIFT_EXPR:
1810 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1811 break;
1812 return candidates;
1814 case NOP_EXPR:
1815 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1816 break;
1817 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1818 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1819 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1820 || ((TYPE_PTRMEMFUNC_P (type1)
1821 || TREE_CODE (type1) == POINTER_TYPE)
1822 && null_ptr_cst_p (args[1])))
1824 type2 = type1;
1825 break;
1827 return candidates;
1829 default:
1830 my_friendly_abort (367);
1832 type1 = build_reference_type (type1);
1833 break;
1835 case COND_EXPR:
1836 /* [over.builtin]
1838 For every pair of promoted arithmetic types L and R, there
1839 exist candidate operator functions of the form
1841 LR operator?(bool, L, R);
1843 where LR is the result of the usual arithmetic conversions
1844 between types L and R.
1846 For every type T, where T is a pointer or pointer-to-member
1847 type, there exist candidate operator functions of the form T
1848 operator?(bool, T, T); */
1850 if (promoted_arithmetic_type_p (type1)
1851 && promoted_arithmetic_type_p (type2))
1852 /* That's OK. */
1853 break;
1855 /* Otherwise, the types should be pointers. */
1856 if (!(TREE_CODE (type1) == POINTER_TYPE
1857 || TYPE_PTRMEM_P (type1)
1858 || TYPE_PTRMEMFUNC_P (type1))
1859 || !(TREE_CODE (type2) == POINTER_TYPE
1860 || TYPE_PTRMEM_P (type2)
1861 || TYPE_PTRMEMFUNC_P (type2)))
1862 return candidates;
1864 /* We don't check that the two types are the same; the logic
1865 below will actually create two candidates; one in which both
1866 parameter types are TYPE1, and one in which both parameter
1867 types are TYPE2. */
1868 break;
1870 /* These arguments do not make for a legal overloaded operator. */
1871 return candidates;
1873 default:
1874 my_friendly_abort (367);
1877 /* If we're dealing with two pointer types, we need candidates
1878 for both of them. */
1879 if (type2 && !same_type_p (type1, type2)
1880 && TREE_CODE (type1) == TREE_CODE (type2)
1881 && (TREE_CODE (type1) == REFERENCE_TYPE
1882 || (TREE_CODE (type1) == POINTER_TYPE
1883 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1884 || TYPE_PTRMEMFUNC_P (type1)
1885 || IS_AGGR_TYPE (type1)))
1887 candidates = build_builtin_candidate
1888 (candidates, fnname, type1, type1, args, argtypes, flags);
1889 return build_builtin_candidate
1890 (candidates, fnname, type2, type2, args, argtypes, flags);
1893 return build_builtin_candidate
1894 (candidates, fnname, type1, type2, args, argtypes, flags);
1897 tree
1898 type_decays_to (type)
1899 tree type;
1901 if (TREE_CODE (type) == ARRAY_TYPE)
1902 return build_pointer_type (TREE_TYPE (type));
1903 if (TREE_CODE (type) == FUNCTION_TYPE)
1904 return build_pointer_type (type);
1905 return type;
1908 /* There are three conditions of builtin candidates:
1910 1) bool-taking candidates. These are the same regardless of the input.
1911 2) pointer-pair taking candidates. These are generated for each type
1912 one of the input types converts to.
1913 3) arithmetic candidates. According to the WP, we should generate
1914 all of these, but I'm trying not to... */
1916 static struct z_candidate *
1917 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1918 struct z_candidate *candidates;
1919 enum tree_code code, code2;
1920 tree fnname, *args;
1921 int flags;
1923 int ref1, i;
1924 tree type, argtypes[3];
1925 /* TYPES[i] is the set of possible builtin-operator parameter types
1926 we will consider for the Ith argument. These are represented as
1927 a TREE_LIST; the TREE_VALUE of each node is the potential
1928 parameter type. */
1929 tree types[2];
1931 for (i = 0; i < 3; ++i)
1933 if (args[i])
1934 argtypes[i] = lvalue_type (args[i]);
1935 else
1936 argtypes[i] = NULL_TREE;
1939 switch (code)
1941 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1942 and VQ is either volatile or empty, there exist candidate operator
1943 functions of the form
1944 VQ T& operator++(VQ T&); */
1946 case POSTINCREMENT_EXPR:
1947 case PREINCREMENT_EXPR:
1948 case POSTDECREMENT_EXPR:
1949 case PREDECREMENT_EXPR:
1950 case MODIFY_EXPR:
1951 ref1 = 1;
1952 break;
1954 /* 24There also exist candidate operator functions of the form
1955 bool operator!(bool);
1956 bool operator&&(bool, bool);
1957 bool operator||(bool, bool); */
1959 case TRUTH_NOT_EXPR:
1960 return build_builtin_candidate
1961 (candidates, fnname, boolean_type_node,
1962 NULL_TREE, args, argtypes, flags);
1964 case TRUTH_ORIF_EXPR:
1965 case TRUTH_ANDIF_EXPR:
1966 return build_builtin_candidate
1967 (candidates, fnname, boolean_type_node,
1968 boolean_type_node, args, argtypes, flags);
1970 case ADDR_EXPR:
1971 case COMPOUND_EXPR:
1972 case COMPONENT_REF:
1973 return candidates;
1975 default:
1976 ref1 = 0;
1979 types[0] = types[1] = NULL_TREE;
1981 for (i = 0; i < 2; ++i)
1983 if (! args[i])
1985 else if (IS_AGGR_TYPE (argtypes[i]))
1987 tree convs;
1989 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1990 return candidates;
1992 convs = lookup_conversions (argtypes[i]);
1994 if (code == COND_EXPR)
1996 if (real_lvalue_p (args[i]))
1997 types[i] = scratch_tree_cons
1998 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2000 types[i] = scratch_tree_cons
2001 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2004 else if (! convs)
2005 return candidates;
2007 for (; convs; convs = TREE_CHAIN (convs))
2009 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2011 if (i == 0 && ref1
2012 && (TREE_CODE (type) != REFERENCE_TYPE
2013 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2014 continue;
2016 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2017 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
2019 type = non_reference (type);
2020 if (i != 0 || ! ref1)
2022 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2023 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2024 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
2025 if (INTEGRAL_TYPE_P (type))
2026 type = type_promotes_to (type);
2029 if (! value_member (type, types[i]))
2030 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
2033 else
2035 if (code == COND_EXPR && real_lvalue_p (args[i]))
2036 types[i] = scratch_tree_cons
2037 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2038 type = non_reference (argtypes[i]);
2039 if (i != 0 || ! ref1)
2041 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2042 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2043 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
2044 if (INTEGRAL_TYPE_P (type))
2045 type = type_promotes_to (type);
2047 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
2051 /* Run through the possible parameter types of both arguments,
2052 creating candidates with those parameter types. */
2053 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2055 if (types[1])
2056 for (type = types[1]; type; type = TREE_CHAIN (type))
2057 candidates = add_builtin_candidate
2058 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2059 TREE_VALUE (type), args, argtypes, flags);
2060 else
2061 candidates = add_builtin_candidate
2062 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2063 NULL_TREE, args, argtypes, flags);
2066 return candidates;
2070 /* If TMPL can be successfully instantiated as indicated by
2071 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2073 TMPL is the template. EXPLICIT_TARGS are any explicit template
2074 arguments. ARGLIST is the arguments provided at the call-site.
2075 The RETURN_TYPE is the desired type for conversion operators. If
2076 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
2077 OBJ is supplied, FLAGS are ignored, and OBJ is as for
2078 add_conv_candidate. */
2080 static struct z_candidate*
2081 add_template_candidate_real (candidates, tmpl, explicit_targs,
2082 arglist, return_type, flags,
2083 obj, strict)
2084 struct z_candidate *candidates;
2085 tree tmpl, explicit_targs, arglist, return_type;
2086 int flags;
2087 tree obj;
2088 unification_kind_t strict;
2090 int ntparms = DECL_NTPARMS (tmpl);
2091 tree targs = make_scratch_vec (ntparms);
2092 struct z_candidate *cand;
2093 int i;
2094 tree fn;
2096 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
2097 return_type, strict);
2099 if (i != 0)
2100 return candidates;
2102 fn = instantiate_template (tmpl, targs);
2103 if (fn == error_mark_node)
2104 return candidates;
2106 if (obj != NULL_TREE)
2107 /* Aha, this is a conversion function. */
2108 cand = add_conv_candidate (candidates, fn, obj, arglist);
2109 else
2110 cand = add_function_candidate (candidates, fn, arglist, flags);
2111 if (DECL_TI_TEMPLATE (fn) != tmpl)
2112 /* This situation can occur if a member template of a template
2113 class is specialized. Then, instantiate_template might return
2114 an instantiation of the specialization, in which case the
2115 DECL_TI_TEMPLATE field will point at the original
2116 specialization. For example:
2118 template <class T> struct S { template <class U> void f(U);
2119 template <> void f(int) {}; };
2120 S<double> sd;
2121 sd.f(3);
2123 Here, TMPL will be template <class U> S<double>::f(U).
2124 And, instantiate template will give us the specialization
2125 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2126 for this will point at template <class T> template <> S<T>::f(int),
2127 so that we can find the definition. For the purposes of
2128 overload resolution, however, we want the original TMPL. */
2129 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2130 else
2131 cand->template = DECL_TEMPLATE_INFO (fn);
2133 return cand;
2137 static struct z_candidate *
2138 add_template_candidate (candidates, tmpl, explicit_targs,
2139 arglist, return_type, flags, strict)
2140 struct z_candidate *candidates;
2141 tree tmpl, explicit_targs, arglist, return_type;
2142 int flags;
2143 unification_kind_t strict;
2145 return
2146 add_template_candidate_real (candidates, tmpl, explicit_targs,
2147 arglist, return_type, flags,
2148 NULL_TREE, strict);
2152 static struct z_candidate *
2153 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2154 struct z_candidate *candidates;
2155 tree tmpl, obj, arglist, return_type;
2157 return
2158 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
2159 return_type, 0, obj, DEDUCE_CONV);
2163 static int
2164 any_viable (cands)
2165 struct z_candidate *cands;
2167 for (; cands; cands = cands->next)
2168 if (pedantic ? cands->viable == 1 : cands->viable)
2169 return 1;
2170 return 0;
2173 static struct z_candidate *
2174 splice_viable (cands)
2175 struct z_candidate *cands;
2177 struct z_candidate **p = &cands;
2179 for (; *p; )
2181 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2182 p = &((*p)->next);
2183 else
2184 *p = (*p)->next;
2187 return cands;
2190 static tree
2191 build_this (obj)
2192 tree obj;
2194 /* Fix this to work on non-lvalues. */
2195 return build_unary_op (ADDR_EXPR, obj, 0);
2198 static void
2199 print_z_candidates (candidates)
2200 struct z_candidate *candidates;
2202 const char *str = "candidates are:";
2203 for (; candidates; candidates = candidates->next)
2205 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2207 if (candidates->fn == ansi_opname [COND_EXPR])
2208 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2209 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2210 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2211 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2212 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2213 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2214 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2215 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2216 else
2217 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2218 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2220 else if (TYPE_P (candidates->fn))
2221 cp_error ("%s %T <conversion>", str, candidates->fn);
2222 else
2223 cp_error_at ("%s %+#D%s", str, candidates->fn,
2224 candidates->viable == -1 ? " <near match>" : "");
2225 str = " ";
2229 /* Returns the best overload candidate to perform the requested
2230 conversion. This function is used for three the overloading situations
2231 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2232 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2233 per [dcl.init.ref], so we ignore temporary bindings. */
2235 static struct z_candidate *
2236 build_user_type_conversion_1 (totype, expr, flags)
2237 tree totype, expr;
2238 int flags;
2240 struct z_candidate *candidates, *cand;
2241 tree fromtype = TREE_TYPE (expr);
2242 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2243 tree args = NULL_TREE;
2244 tree templates = NULL_TREE;
2246 if (IS_AGGR_TYPE (totype))
2247 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2248 if (IS_AGGR_TYPE (fromtype)
2249 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2250 convs = lookup_conversions (fromtype);
2252 candidates = 0;
2253 flags |= LOOKUP_NO_CONVERSION;
2255 if (ctors)
2257 tree t = build_int_2 (0, 0);
2258 TREE_TYPE (t) = build_pointer_type (totype);
2259 args = build_scratch_list (NULL_TREE, expr);
2260 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2261 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2262 args = scratch_tree_cons (NULL_TREE, t, args);
2264 ctors = TREE_VALUE (ctors);
2266 for (; ctors; ctors = OVL_NEXT (ctors))
2268 tree ctor = OVL_CURRENT (ctors);
2269 if (DECL_NONCONVERTING_P (ctor))
2270 continue;
2272 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2274 templates = scratch_tree_cons (NULL_TREE, ctor, templates);
2275 candidates =
2276 add_template_candidate (candidates, ctor,
2277 NULL_TREE, args, NULL_TREE, flags,
2278 DEDUCE_CALL);
2280 else
2281 candidates = add_function_candidate (candidates, ctor,
2282 args, flags);
2284 if (candidates)
2286 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2287 candidates->basetype_path = TYPE_BINFO (totype);
2291 if (convs)
2292 args = build_scratch_list (NULL_TREE, build_this (expr));
2294 for (; convs; convs = TREE_CHAIN (convs))
2296 tree fns = TREE_VALUE (convs);
2297 int convflags = LOOKUP_NO_CONVERSION;
2298 tree ics;
2300 /* If we are called to convert to a reference type, we are trying to
2301 find an lvalue binding, so don't even consider temporaries. If
2302 we don't find an lvalue binding, the caller will try again to
2303 look for a temporary binding. */
2304 if (TREE_CODE (totype) == REFERENCE_TYPE)
2305 convflags |= LOOKUP_NO_TEMP_BIND;
2307 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2308 ics = implicit_conversion
2309 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2310 else
2311 /* We can't compute this yet. */
2312 ics = error_mark_node;
2314 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2315 /* ignore the near match. */;
2316 else if (ics)
2317 for (; fns; fns = OVL_NEXT (fns))
2319 tree fn = OVL_CURRENT (fns);
2320 struct z_candidate *old_candidates = candidates;
2322 if (TREE_CODE (fn) == TEMPLATE_DECL)
2324 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2325 candidates =
2326 add_template_candidate (candidates, fn, NULL_TREE,
2327 args, totype, flags,
2328 DEDUCE_CONV);
2330 else
2331 candidates = add_function_candidate (candidates, fn,
2332 args, flags);
2334 if (candidates != old_candidates)
2336 if (TREE_CODE (fn) == TEMPLATE_DECL)
2337 ics = implicit_conversion
2338 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2339 0, convflags);
2341 candidates->second_conv = ics;
2342 candidates->basetype_path = TREE_PURPOSE (convs);
2344 if (ics == NULL_TREE)
2345 candidates->viable = 0;
2346 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2347 candidates->viable = -1;
2352 if (! any_viable (candidates))
2354 #if 0
2355 if (flags & LOOKUP_COMPLAIN)
2357 if (candidates && ! candidates->next)
2358 /* say why this one won't work or try to be loose */;
2359 else
2360 cp_error ("no viable candidates");
2362 #endif
2364 return 0;
2367 candidates = splice_viable (candidates);
2368 cand = tourney (candidates);
2370 if (cand == 0)
2372 if (flags & LOOKUP_COMPLAIN)
2374 cp_error ("conversion from `%T' to `%T' is ambiguous",
2375 fromtype, totype);
2376 print_z_candidates (candidates);
2379 cand = candidates; /* any one will do */
2380 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2381 ICS_USER_FLAG (cand->second_conv) = 1;
2382 ICS_BAD_FLAG (cand->second_conv) = 1;
2384 return cand;
2387 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2388 p = &(TREE_OPERAND (*p, 0));
2390 /* Pedantically, normal function declarations are never considered
2391 to refer to template instantiations, so we only do this with
2392 -fguiding-decls. */
2393 if (flag_guiding_decls && templates && ! cand->template
2394 && !DECL_INITIAL (cand->fn)
2395 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2396 add_maybe_template (cand->fn, templates);
2398 *p = build
2399 (USER_CONV,
2400 (DECL_CONSTRUCTOR_P (cand->fn)
2401 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2402 expr, build_expr_ptr_wrapper (cand));
2403 ICS_USER_FLAG (cand->second_conv) = 1;
2404 if (cand->viable == -1)
2405 ICS_BAD_FLAG (cand->second_conv) = 1;
2407 return cand;
2410 tree
2411 build_user_type_conversion (totype, expr, flags)
2412 tree totype, expr;
2413 int flags;
2415 struct z_candidate *cand
2416 = build_user_type_conversion_1 (totype, expr, flags);
2418 if (cand)
2420 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2421 return error_mark_node;
2422 return convert_from_reference (convert_like (cand->second_conv, expr));
2424 return NULL_TREE;
2427 /* Do any initial processing on the arguments to a function call. */
2429 static tree
2430 resolve_args (args)
2431 tree args;
2433 tree t;
2434 for (t = args; t; t = TREE_CHAIN (t))
2436 if (TREE_VALUE (t) == error_mark_node)
2437 return error_mark_node;
2438 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2440 error ("invalid use of void expression");
2441 return error_mark_node;
2443 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2444 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2446 return args;
2449 tree
2450 build_new_function_call (fn, args)
2451 tree fn, args;
2453 struct z_candidate *candidates = 0, *cand;
2454 tree explicit_targs = NULL_TREE;
2455 int template_only = 0;
2457 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2459 explicit_targs = TREE_OPERAND (fn, 1);
2460 fn = TREE_OPERAND (fn, 0);
2461 template_only = 1;
2464 if (really_overloaded_fn (fn))
2466 tree t1;
2467 tree templates = NULL_TREE;
2469 args = resolve_args (args);
2471 if (args == error_mark_node)
2472 return error_mark_node;
2474 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2476 tree t = OVL_FUNCTION (t1);
2477 struct z_candidate *old_candidates = candidates;
2479 if (TREE_CODE (t) == TEMPLATE_DECL)
2481 templates = scratch_tree_cons (NULL_TREE, t, templates);
2482 candidates = add_template_candidate
2483 (candidates, t, explicit_targs, args, NULL_TREE,
2484 LOOKUP_NORMAL, DEDUCE_CALL);
2486 else if (! template_only)
2487 candidates = add_function_candidate
2488 (candidates, t, args, LOOKUP_NORMAL);
2490 if (candidates != old_candidates)
2491 candidates->basetype_path = DECL_REAL_CONTEXT (t);
2494 if (! any_viable (candidates))
2496 if (candidates && ! candidates->next)
2497 return build_function_call (candidates->fn, args);
2498 cp_error ("no matching function for call to `%D (%A)'",
2499 DECL_NAME (OVL_FUNCTION (fn)), args);
2500 if (candidates)
2501 print_z_candidates (candidates);
2502 return error_mark_node;
2504 candidates = splice_viable (candidates);
2505 cand = tourney (candidates);
2507 if (cand == 0)
2509 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2510 DECL_NAME (OVL_FUNCTION (fn)), args);
2511 print_z_candidates (candidates);
2512 return error_mark_node;
2515 /* Pedantically, normal function declarations are never considered
2516 to refer to template instantiations, so we only do this with
2517 -fguiding-decls. */
2518 if (flag_guiding_decls && templates && ! cand->template
2519 && ! DECL_INITIAL (cand->fn))
2520 add_maybe_template (cand->fn, templates);
2522 return build_over_call (cand, args, LOOKUP_NORMAL);
2525 /* This is not really overloaded. */
2526 fn = OVL_CURRENT (fn);
2528 return build_function_call (fn, args);
2531 static tree
2532 build_object_call (obj, args)
2533 tree obj, args;
2535 struct z_candidate *candidates = 0, *cand;
2536 tree fns, convs, mem_args = NULL_TREE;
2537 tree type = TREE_TYPE (obj);
2539 if (TYPE_PTRMEMFUNC_P (type))
2541 /* It's no good looking for an overloaded operator() on a
2542 pointer-to-member-function. */
2543 cp_error ("pointer-to-member function %E cannot be called", obj);
2544 cp_error ("without an object; consider using .* or ->*");
2545 return error_mark_node;
2548 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2549 if (fns == error_mark_node)
2550 return error_mark_node;
2552 args = resolve_args (args);
2554 if (args == error_mark_node)
2555 return error_mark_node;
2557 if (fns)
2559 tree base = TREE_PURPOSE (fns);
2560 mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2562 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2564 tree fn = OVL_CURRENT (fns);
2565 if (TREE_CODE (fn) == TEMPLATE_DECL)
2567 candidates
2568 = add_template_candidate (candidates, fn, NULL_TREE,
2569 mem_args, NULL_TREE,
2570 LOOKUP_NORMAL, DEDUCE_CALL);
2572 else
2573 candidates = add_function_candidate
2574 (candidates, fn, mem_args, LOOKUP_NORMAL);
2576 if (candidates)
2577 candidates->basetype_path = base;
2581 convs = lookup_conversions (type);
2583 for (; convs; convs = TREE_CHAIN (convs))
2585 tree fns = TREE_VALUE (convs);
2586 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2588 if ((TREE_CODE (totype) == POINTER_TYPE
2589 || TREE_CODE (totype) == REFERENCE_TYPE)
2590 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2591 for (; fns; fns = OVL_NEXT (fns))
2593 tree fn = OVL_CURRENT (fns);
2594 if (TREE_CODE (fn) == TEMPLATE_DECL)
2596 candidates = add_template_conv_candidate (candidates,
2598 obj,
2599 args,
2600 totype);
2602 else
2603 candidates = add_conv_candidate (candidates, fn, obj, args);
2605 if (candidates)
2606 candidates->basetype_path = TREE_PURPOSE (convs);
2610 if (! any_viable (candidates))
2612 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2613 print_z_candidates (candidates);
2614 return error_mark_node;
2617 candidates = splice_viable (candidates);
2618 cand = tourney (candidates);
2620 if (cand == 0)
2622 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2623 print_z_candidates (candidates);
2624 return error_mark_node;
2627 /* Since cand->fn will be a type, not a function, for a conversion
2628 function, we must be careful not to unconditionally look at
2629 DECL_NAME here. */
2630 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2631 && DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2632 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2634 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2636 /* FIXME */
2637 return build_function_call (obj, args);
2640 static void
2641 op_error (code, code2, arg1, arg2, arg3, problem)
2642 enum tree_code code, code2;
2643 tree arg1, arg2, arg3;
2644 const char *problem;
2646 const char * opname
2647 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2649 switch (code)
2651 case COND_EXPR:
2652 cp_error ("%s for `%T ? %T : %T'", problem,
2653 error_type (arg1), error_type (arg2), error_type (arg3));
2654 break;
2655 case POSTINCREMENT_EXPR:
2656 case POSTDECREMENT_EXPR:
2657 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2658 break;
2659 case ARRAY_REF:
2660 cp_error ("%s for `%T[%T]'", problem,
2661 error_type (arg1), error_type (arg2));
2662 break;
2663 default:
2664 if (arg2)
2665 cp_error ("%s for `%T %s %T'", problem,
2666 error_type (arg1), opname, error_type (arg2));
2667 else
2668 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2672 /* Return the implicit conversion sequence that could be used to
2673 convert E1 to E2 in [expr.cond]. */
2675 static tree
2676 conditional_conversion (e1, e2)
2677 tree e1;
2678 tree e2;
2680 tree t1 = non_reference (TREE_TYPE (e1));
2681 tree t2 = non_reference (TREE_TYPE (e2));
2682 tree conv;
2684 /* [expr.cond]
2686 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2687 implicitly converted (clause _conv_) to the type "reference to
2688 T2", subject to the constraint that in the conversion the
2689 reference must bind directly (_dcl.init.ref_) to E1. */
2690 if (real_lvalue_p (e2))
2692 conv = implicit_conversion (build_reference_type (t2),
2695 LOOKUP_NO_TEMP_BIND);
2696 if (conv)
2697 return conv;
2700 /* [expr.cond]
2702 If E1 and E2 have class type, and the underlying class types are
2703 the same or one is a base class of the other: E1 can be converted
2704 to match E2 if the class of T2 is the same type as, or a base
2705 class of, the class of T1, and the cv-qualification of T2 is the
2706 same cv-qualification as, or a greater cv-qualification than, the
2707 cv-qualification of T1. If the conversion is applied, E1 is
2708 changed to an rvalue of type T2 that still refers to the original
2709 source class object (or the appropriate subobject thereof). */
2710 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2711 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2712 TYPE_MAIN_VARIANT (t1)))
2714 if (at_least_as_qualified_p (t2, t1))
2716 conv = build1 (IDENTITY_CONV, t1, e1);
2717 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2718 TYPE_MAIN_VARIANT (t2)))
2719 conv = build_conv (BASE_CONV, t2, conv);
2720 return conv;
2722 else
2723 return NULL_TREE;
2726 /* [expr.cond]
2728 E1 can be converted to match E2 if E1 can be implicitly converted
2729 to the type that expression E2 would have if E2 were converted to
2730 an rvalue (or the type it has, if E2 is an rvalue). */
2731 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2734 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2735 arguments to the conditional expression. By the time this function
2736 is called, any suitable candidate functions are included in
2737 CANDIDATES. */
2739 tree
2740 build_conditional_expr (arg1, arg2, arg3)
2741 tree arg1;
2742 tree arg2;
2743 tree arg3;
2745 tree arg2_type;
2746 tree arg3_type;
2747 tree result;
2748 tree result_type = NULL_TREE;
2749 int lvalue_p = 1;
2750 struct z_candidate *candidates = 0;
2751 struct z_candidate *cand;
2753 /* As a G++ extension, the second argument to the conditional can be
2754 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2755 c'.) If the second operand is omitted, make sure it is
2756 calculated only once. */
2757 if (!arg2)
2759 if (pedantic)
2760 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
2761 arg1 = arg2 = save_expr (arg1);
2764 /* If something has already gone wrong, just pass that fact up the
2765 tree. */
2766 if (arg1 == error_mark_node
2767 || arg2 == error_mark_node
2768 || arg3 == error_mark_node
2769 || TREE_TYPE (arg1) == error_mark_node
2770 || TREE_TYPE (arg2) == error_mark_node
2771 || TREE_TYPE (arg3) == error_mark_node)
2772 return error_mark_node;
2774 /* [expr.cond]
2776 The first expr ession is implicitly converted to bool (clause
2777 _conv_). */
2778 arg1 = cp_convert (boolean_type_node, arg1);
2780 /* Convert from reference types to ordinary types; no expressions
2781 really have reference type in C++. */
2782 arg2 = convert_from_reference (arg2);
2783 arg3 = convert_from_reference (arg3);
2785 /* [expr.cond]
2787 If either the second or the third operand has type (possibly
2788 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2789 array-to-pointer (_conv.array_), and function-to-pointer
2790 (_conv.func_) standard conversions are performed on the second
2791 and third operands. */
2792 arg2_type = TREE_TYPE (arg2);
2793 arg3_type = TREE_TYPE (arg3);
2794 if (same_type_p (TYPE_MAIN_VARIANT (arg2_type), void_type_node)
2795 || same_type_p (TYPE_MAIN_VARIANT (arg3_type), void_type_node))
2797 int arg2_void_p;
2798 int arg3_void_p;
2800 /* Do the conversions. We don't these for `void' type arguments
2801 since it can't have any effect and since decay_conversion
2802 does not handle that case gracefully. */
2803 if (!same_type_p (TYPE_MAIN_VARIANT (arg2_type), void_type_node))
2804 arg2 = decay_conversion (arg2);
2805 if (!same_type_p (TYPE_MAIN_VARIANT (arg3_type), void_type_node))
2806 arg3 = decay_conversion (arg3);
2807 arg2_type = TREE_TYPE (arg2);
2808 arg3_type = TREE_TYPE (arg3);
2810 arg2_void_p = same_type_p (TYPE_MAIN_VARIANT (arg2_type),
2811 void_type_node);
2812 arg3_void_p = same_type_p (TYPE_MAIN_VARIANT (arg3_type),
2813 void_type_node);
2815 /* [expr.cond]
2817 One of the following shall hold:
2819 --The second or the third operand (but not both) is a
2820 throw-expression (_except.throw_); the result is of the
2821 type of the other and is an rvalue.
2823 --Both the second and the third operands have type void; the
2824 result is of type void and is an rvalue. */
2825 if ((TREE_CODE (arg2) == THROW_EXPR)
2826 ^ (TREE_CODE (arg3) == THROW_EXPR))
2827 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2828 ? arg3_type : arg2_type);
2829 else if (arg2_void_p && arg3_void_p)
2830 result_type = void_type_node;
2831 else
2833 cp_error ("`%E' has type `void' and is not a throw-expression",
2834 arg2_void_p ? arg2 : arg3);
2835 return error_mark_node;
2838 lvalue_p = 0;
2839 goto valid_operands;
2841 /* [expr.cond]
2843 Otherwise, if the second and third operand have different types,
2844 and either has (possibly cv-qualified) class type, an attempt is
2845 made to convert each of those operands to the type of the other. */
2846 else if (!same_type_p (arg2_type, arg3_type)
2847 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2849 tree conv2 = conditional_conversion (arg2, arg3);
2850 tree conv3 = conditional_conversion (arg3, arg2);
2852 /* [expr.cond]
2854 If both can be converted, or one can be converted but the
2855 conversion is ambiguous, the program is ill-formed. If
2856 neither can be converted, the operands are left unchanged and
2857 further checking is performed as described below. If exactly
2858 one conversion is possible, that conversion is applied to the
2859 chosen operand and the converted operand is used in place of
2860 the original operand for the remainder of this section. */
2861 if ((conv2 && !ICS_BAD_FLAG (conv2)
2862 && conv3 && !ICS_BAD_FLAG (conv3))
2863 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
2864 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
2866 cp_error ("operands to ?: have different types");
2867 return error_mark_node;
2869 else if (conv2 && !ICS_BAD_FLAG (conv2))
2871 arg2 = convert_like (conv2, arg2);
2872 arg2 = convert_from_reference (arg2);
2873 /* That may not quite have done the trick. If the two types
2874 are cv-qualified variants of one another, we will have
2875 just used an IDENTITY_CONV. (There's no conversion from
2876 an lvalue of one class type to an lvalue of another type,
2877 even a cv-qualified variant, and we don't want to lose
2878 lvalue-ness here.) So, we manually add a NOP_EXPR here
2879 if necessary. */
2880 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
2881 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
2882 arg2_type = TREE_TYPE (arg2);
2884 else if (conv3 && !ICS_BAD_FLAG (conv3))
2886 arg3 = convert_like (conv3, arg3);
2887 arg3 = convert_from_reference (arg3);
2888 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
2889 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
2890 arg3_type = TREE_TYPE (arg3);
2894 /* [expr.cond]
2896 If the second and third operands are lvalues and have the same
2897 type, the result is of that type and is an lvalue. */
2898 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
2899 same_type_p (arg2_type, arg3_type))
2901 result_type = arg2_type;
2902 goto valid_operands;
2905 /* [expr.cond]
2907 Otherwise, the result is an rvalue. If the second and third
2908 operand do not have the same type, and either has (possibly
2909 cv-qualified) class type, overload resolution is used to
2910 determine the conversions (if any) to be applied to the operands
2911 (_over.match.oper_, _over.built_). */
2912 lvalue_p = 0;
2913 if (!same_type_p (arg2_type, arg3_type)
2914 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2916 tree args[3];
2917 tree conv;
2919 /* Rearrange the arguments so that add_builtin_candidate only has
2920 to know about two args. In build_builtin_candidates, the
2921 arguments are unscrambled. */
2922 args[0] = arg2;
2923 args[1] = arg3;
2924 args[2] = arg1;
2925 candidates = add_builtin_candidates (candidates,
2926 COND_EXPR,
2927 NOP_EXPR,
2928 ansi_opname[COND_EXPR],
2929 args,
2930 LOOKUP_NORMAL);
2932 /* [expr.cond]
2934 If the overload resolution fails, the program is
2935 ill-formed. */
2936 if (!any_viable (candidates))
2938 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
2939 print_z_candidates (candidates);
2940 return error_mark_node;
2942 candidates = splice_viable (candidates);
2943 cand = tourney (candidates);
2944 if (!cand)
2946 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
2947 print_z_candidates (candidates);
2948 return error_mark_node;
2951 /* [expr.cond]
2953 Otherwise, the conversions thus determined are applied, and
2954 the converted operands are used in place of the original
2955 operands for the remainder of this section. */
2956 conv = TREE_VEC_ELT (cand->convs, 0);
2957 arg1 = convert_like (conv, arg1);
2958 conv = TREE_VEC_ELT (cand->convs, 1);
2959 arg2 = convert_like (conv, arg2);
2960 conv = TREE_VEC_ELT (cand->convs, 2);
2961 arg3 = convert_like (conv, arg3);
2964 /* [expr.cond]
2966 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
2967 and function-to-pointer (_conv.func_) standard conversions are
2968 performed on the second and third operands. */
2969 arg2 = decay_conversion (arg2);
2970 arg2_type = TREE_TYPE (arg2);
2971 arg3 = decay_conversion (arg3);
2972 arg3_type = TREE_TYPE (arg3);
2974 /* [expr.cond]
2976 After those conversions, one of the following shall hold:
2978 --The second and third operands have the same type; the result is of
2979 that type. */
2980 if (same_type_p (arg2_type, arg3_type))
2981 result_type = arg2_type;
2982 /* [expr.cond]
2984 --The second and third operands have arithmetic or enumeration
2985 type; the usual arithmetic conversions are performed to bring
2986 them to a common type, and the result is of that type. */
2987 else if ((ARITHMETIC_TYPE_P (arg2_type)
2988 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
2989 && (ARITHMETIC_TYPE_P (arg3_type)
2990 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
2992 /* In this case, there is always a common type. */
2993 result_type = type_after_usual_arithmetic_conversions (arg2_type,
2994 arg3_type);
2995 arg2 = perform_implicit_conversion (result_type, arg2);
2996 arg3 = perform_implicit_conversion (result_type, arg3);
2998 /* [expr.cond]
3000 --The second and third operands have pointer type, or one has
3001 pointer type and the other is a null pointer constant; pointer
3002 conversions (_conv.ptr_) and qualification conversions
3003 (_conv.qual_) are performed to bring them to their composite
3004 pointer type (_expr.rel_). The result is of the composite
3005 pointer type.
3007 --The second and third operands have pointer to member type, or
3008 one has pointer to member type and the other is a null pointer
3009 constant; pointer to member conversions (_conv.mem_) and
3010 qualification conversions (_conv.qual_) are performed to bring
3011 them to a common type, whose cv-qualification shall match the
3012 cv-qualification of either the second or the third operand.
3013 The result is of the common type. */
3014 else if ((null_ptr_cst_p (arg2)
3015 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3016 || TYPE_PTRMEMFUNC_P (arg3_type)))
3017 || (null_ptr_cst_p (arg3)
3018 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3019 || TYPE_PTRMEMFUNC_P (arg2_type)))
3020 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3021 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3022 || (TYPE_PTRMEMFUNC_P (arg2_type)
3023 && TYPE_PTRMEMFUNC_P (arg3_type)))
3025 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3026 arg3, "conditional expression");
3027 arg2 = perform_implicit_conversion (result_type, arg2);
3028 arg3 = perform_implicit_conversion (result_type, arg3);
3031 if (!result_type)
3033 cp_error ("operands to ?: have different types");
3034 return error_mark_node;
3037 valid_operands:
3038 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3039 /* Expand both sides into the same slot, hopefully the target of the
3040 ?: expression. */
3041 if (TREE_CODE (arg2) == TARGET_EXPR && TREE_CODE (arg3) == TARGET_EXPR)
3043 tree slot = build (VAR_DECL, result_type);
3044 layout_decl (slot, 0);
3045 result = build (TARGET_EXPR, result_type,
3046 slot, result, NULL_TREE, NULL_TREE);
3049 /* If this expression is an rvalue, but might be mistaken for an
3050 lvalue, we must add a NON_LVALUE_EXPR. */
3051 if (!lvalue_p && real_lvalue_p (result))
3052 result = build1 (NON_LVALUE_EXPR, result_type, result);
3054 return result;
3057 tree
3058 build_new_op (code, flags, arg1, arg2, arg3)
3059 enum tree_code code;
3060 int flags;
3061 tree arg1, arg2, arg3;
3063 struct z_candidate *candidates = 0, *cand;
3064 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3065 enum tree_code code2 = NOP_EXPR;
3066 tree templates = NULL_TREE;
3067 tree conv;
3069 if (arg1 == error_mark_node
3070 || arg2 == error_mark_node
3071 || arg3 == error_mark_node)
3072 return error_mark_node;
3074 /* This can happen if a template takes all non-type parameters, e.g.
3075 undeclared_template<1, 5, 72>a; */
3076 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3078 cp_error ("`%D' must be declared before use", arg1);
3079 return error_mark_node;
3082 if (code == MODIFY_EXPR)
3084 code2 = TREE_CODE (arg3);
3085 arg3 = NULL_TREE;
3086 fnname = ansi_assopname[code2];
3088 else
3089 fnname = ansi_opname[code];
3091 switch (code)
3093 case NEW_EXPR:
3094 case VEC_NEW_EXPR:
3095 case VEC_DELETE_EXPR:
3096 case DELETE_EXPR:
3097 /* Use build_op_new_call and build_op_delete_call instead. */
3098 my_friendly_abort (981018);
3100 case CALL_EXPR:
3101 return build_object_call (arg1, arg2);
3103 default:
3104 break;
3107 /* The comma operator can have void args. */
3108 if (TREE_CODE (arg1) == OFFSET_REF)
3109 arg1 = resolve_offset_ref (arg1);
3110 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
3111 arg2 = resolve_offset_ref (arg2);
3112 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
3113 arg3 = resolve_offset_ref (arg3);
3115 if (code == COND_EXPR)
3117 if (arg2 == NULL_TREE
3118 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3119 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3120 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3121 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3122 goto builtin;
3124 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3125 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3126 goto builtin;
3128 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3129 arg2 = integer_zero_node;
3131 if (arg2 && arg3)
3132 arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
3133 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
3134 else if (arg2)
3135 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
3136 else
3137 arglist = build_scratch_list (NULL_TREE, arg1);
3139 fns = lookup_function_nonclass (fnname, arglist);
3141 if (fns && TREE_CODE (fns) == TREE_LIST)
3142 fns = TREE_VALUE (fns);
3143 for (; fns; fns = OVL_NEXT (fns))
3145 tree fn = OVL_CURRENT (fns);
3146 if (TREE_CODE (fn) == TEMPLATE_DECL)
3148 templates = scratch_tree_cons (NULL_TREE, fn, templates);
3149 candidates
3150 = add_template_candidate (candidates, fn, NULL_TREE,
3151 arglist, TREE_TYPE (fnname),
3152 flags, DEDUCE_CALL);
3154 else
3155 candidates = add_function_candidate (candidates, fn, arglist, flags);
3158 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3160 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3161 if (fns == error_mark_node)
3162 return fns;
3164 else
3165 fns = NULL_TREE;
3167 if (fns)
3169 tree basetype = TREE_PURPOSE (fns);
3170 mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3171 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3173 tree fn = OVL_CURRENT (fns);
3174 tree this_arglist;
3176 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3177 this_arglist = mem_arglist;
3178 else
3179 this_arglist = arglist;
3181 if (TREE_CODE (fn) == TEMPLATE_DECL)
3183 /* A member template. */
3184 templates = scratch_tree_cons (NULL_TREE, fn, templates);
3185 candidates
3186 = add_template_candidate (candidates, fn, NULL_TREE,
3187 this_arglist, TREE_TYPE (fnname),
3188 flags, DEDUCE_CALL);
3190 else
3191 candidates = add_function_candidate
3192 (candidates, fn, this_arglist, flags);
3194 if (candidates)
3195 candidates->basetype_path = basetype;
3200 tree args[3];
3202 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3203 to know about two args; a builtin candidate will always have a first
3204 parameter of type bool. We'll handle that in
3205 build_builtin_candidate. */
3206 if (code == COND_EXPR)
3208 args[0] = arg2;
3209 args[1] = arg3;
3210 args[2] = arg1;
3212 else
3214 args[0] = arg1;
3215 args[1] = arg2;
3216 args[2] = NULL_TREE;
3219 candidates = add_builtin_candidates
3220 (candidates, code, code2, fnname, args, flags);
3223 if (! any_viable (candidates))
3225 switch (code)
3227 case POSTINCREMENT_EXPR:
3228 case POSTDECREMENT_EXPR:
3229 /* Look for an `operator++ (int)'. If they didn't have
3230 one, then we fall back to the old way of doing things. */
3231 if (flags & LOOKUP_COMPLAIN)
3232 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
3233 fnname, opname_tab [code]);
3234 if (code == POSTINCREMENT_EXPR)
3235 code = PREINCREMENT_EXPR;
3236 else
3237 code = PREDECREMENT_EXPR;
3238 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3240 /* The caller will deal with these. */
3241 case ADDR_EXPR:
3242 case COMPOUND_EXPR:
3243 case COMPONENT_REF:
3244 return NULL_TREE;
3246 default:
3247 break;
3249 if (flags & LOOKUP_COMPLAIN)
3251 op_error (code, code2, arg1, arg2, arg3, "no match");
3252 print_z_candidates (candidates);
3254 return error_mark_node;
3256 candidates = splice_viable (candidates);
3257 cand = tourney (candidates);
3259 if (cand == 0)
3261 if (flags & LOOKUP_COMPLAIN)
3263 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3264 print_z_candidates (candidates);
3266 return error_mark_node;
3269 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3271 extern int warn_synth;
3272 if (warn_synth
3273 && fnname == ansi_opname[MODIFY_EXPR]
3274 && DECL_ARTIFICIAL (cand->fn)
3275 && candidates->next
3276 && ! candidates->next->next)
3278 cp_warning ("using synthesized `%#D' for copy assignment",
3279 cand->fn);
3280 cp_warning_at (" where cfront would use `%#D'",
3281 cand == candidates
3282 ? candidates->next->fn
3283 : candidates->fn);
3286 /* Pedantically, normal function declarations are never considered
3287 to refer to template instantiations, so we only do this with
3288 -fguiding-decls. */
3289 if (flag_guiding_decls && templates && ! cand->template
3290 && ! DECL_INITIAL (cand->fn)
3291 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
3292 add_maybe_template (cand->fn, templates);
3294 return build_over_call
3295 (cand,
3296 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3297 ? mem_arglist : arglist,
3298 LOOKUP_NORMAL);
3301 /* Check for comparison of different enum types. */
3302 switch (code)
3304 case GT_EXPR:
3305 case LT_EXPR:
3306 case GE_EXPR:
3307 case LE_EXPR:
3308 case EQ_EXPR:
3309 case NE_EXPR:
3310 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3311 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3312 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3313 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3315 cp_warning ("comparison between `%#T' and `%#T'",
3316 TREE_TYPE (arg1), TREE_TYPE (arg2));
3318 break;
3319 default:
3320 break;
3323 /* We need to strip any leading REF_BIND so that bitfields don't cause
3324 errors. This should not remove any important conversions, because
3325 builtins don't apply to class objects directly. */
3326 conv = TREE_VEC_ELT (cand->convs, 0);
3327 if (TREE_CODE (conv) == REF_BIND)
3328 conv = TREE_OPERAND (conv, 0);
3329 arg1 = convert_like (conv, arg1);
3330 if (arg2)
3332 conv = TREE_VEC_ELT (cand->convs, 1);
3333 if (TREE_CODE (conv) == REF_BIND)
3334 conv = TREE_OPERAND (conv, 0);
3335 arg2 = convert_like (conv, arg2);
3337 if (arg3)
3339 conv = TREE_VEC_ELT (cand->convs, 2);
3340 if (TREE_CODE (conv) == REF_BIND)
3341 conv = TREE_OPERAND (conv, 0);
3342 arg3 = convert_like (conv, arg3);
3345 builtin:
3346 switch (code)
3348 case MODIFY_EXPR:
3349 return build_modify_expr (arg1, code2, arg2);
3351 case INDIRECT_REF:
3352 return build_indirect_ref (arg1, "unary *");
3354 case PLUS_EXPR:
3355 case MINUS_EXPR:
3356 case MULT_EXPR:
3357 case TRUNC_DIV_EXPR:
3358 case GT_EXPR:
3359 case LT_EXPR:
3360 case GE_EXPR:
3361 case LE_EXPR:
3362 case EQ_EXPR:
3363 case NE_EXPR:
3364 case MAX_EXPR:
3365 case MIN_EXPR:
3366 case LSHIFT_EXPR:
3367 case RSHIFT_EXPR:
3368 case TRUNC_MOD_EXPR:
3369 case BIT_AND_EXPR:
3370 case BIT_IOR_EXPR:
3371 case BIT_XOR_EXPR:
3372 case TRUTH_ANDIF_EXPR:
3373 case TRUTH_ORIF_EXPR:
3374 return build_binary_op_nodefault (code, arg1, arg2, code);
3376 case CONVERT_EXPR:
3377 case NEGATE_EXPR:
3378 case BIT_NOT_EXPR:
3379 case TRUTH_NOT_EXPR:
3380 case PREINCREMENT_EXPR:
3381 case POSTINCREMENT_EXPR:
3382 case PREDECREMENT_EXPR:
3383 case POSTDECREMENT_EXPR:
3384 case REALPART_EXPR:
3385 case IMAGPART_EXPR:
3386 return build_unary_op (code, arg1, candidates != 0);
3388 case ARRAY_REF:
3389 return build_array_ref (arg1, arg2);
3391 case COND_EXPR:
3392 return build_conditional_expr (arg1, arg2, arg3);
3394 case MEMBER_REF:
3395 return build_m_component_ref
3396 (build_indirect_ref (arg1, NULL_PTR), arg2);
3398 /* The caller will deal with these. */
3399 case ADDR_EXPR:
3400 case COMPONENT_REF:
3401 case COMPOUND_EXPR:
3402 return NULL_TREE;
3404 default:
3405 my_friendly_abort (367);
3406 return NULL_TREE;
3410 /* Build up a call to operator new. This has to be handled differently
3411 from other operators in the way lookup is handled; first members are
3412 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
3413 TYPE is the type to be created. ARGS are any new-placement args.
3414 FLAGS are the usual overloading flags. */
3416 tree
3417 build_op_new_call (code, type, args, flags)
3418 enum tree_code code;
3419 tree type, args;
3420 int flags;
3422 tree fnname = ansi_opname[code];
3424 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
3425 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
3427 return build_method_call (build_dummy_object (type),
3428 fnname, args, NULL_TREE, flags);
3430 else
3431 return build_new_function_call
3432 (lookup_function_nonclass (fnname, args), args);
3435 /* Build a call to operator delete. This has to be handled very specially,
3436 because the restrictions on what signatures match are different from all
3437 other call instances. For a normal delete, only a delete taking (void *)
3438 or (void *, size_t) is accepted. For a placement delete, only an exact
3439 match with the placement new is accepted.
3441 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3442 ADDR is the pointer to be deleted. For placement delete, it is also
3443 used to determine what the corresponding new looked like.
3444 SIZE is the size of the memory block to be deleted.
3445 FLAGS are the usual overloading flags.
3446 PLACEMENT is the corresponding placement new call, or 0. */
3448 tree
3449 build_op_delete_call (code, addr, size, flags, placement)
3450 enum tree_code code;
3451 tree addr, size, placement;
3452 int flags;
3454 tree fn, fns, fnname, fntype, argtypes, args, type;
3456 if (addr == error_mark_node)
3457 return error_mark_node;
3459 type = TREE_TYPE (TREE_TYPE (addr));
3460 fnname = ansi_opname[code];
3462 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3463 /* In [class.free]
3465 If the result of the lookup is ambiguous or inaccessible, or if
3466 the lookup selects a placement deallocation function, the
3467 program is ill-formed.
3469 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3471 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3472 if (fns == error_mark_node)
3473 return error_mark_node;
3475 else
3476 fns = NULL_TREE;
3478 if (fns == NULL_TREE)
3479 fns = lookup_name_nonclass (fnname);
3481 if (placement)
3483 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3485 /* Extract the function. */
3486 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3487 /* Then the second parm type. */
3488 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3490 /* Also the second argument. */
3491 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3493 else
3495 /* First try it without the size argument. */
3496 argtypes = void_list_node;
3497 args = NULL_TREE;
3500 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3501 fntype = build_function_type (void_type_node, argtypes);
3503 /* Strip const and volatile from addr. */
3504 if (type != TYPE_MAIN_VARIANT (type))
3505 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
3507 fn = instantiate_type (fntype, fns, 2);
3509 if (fn != error_mark_node)
3511 if (TREE_CODE (fns) == TREE_LIST)
3512 /* Member functions. */
3513 enforce_access (TREE_PURPOSE (fns), fn);
3514 return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
3517 /* If we are doing placement delete we do nothing if we don't find a
3518 matching op delete. */
3519 if (placement)
3520 return NULL_TREE;
3522 /* Normal delete; now try to find a match including the size argument. */
3523 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3524 tree_cons (NULL_TREE, sizetype, void_list_node));
3525 fntype = build_function_type (void_type_node, argtypes);
3527 fn = instantiate_type (fntype, fns, 2);
3529 if (fn != error_mark_node)
3531 if (BASELINK_P (fns))
3532 /* Member functions. */
3533 enforce_access (TREE_PURPOSE (fns), fn);
3534 return build_function_call
3535 (fn, expr_tree_cons (NULL_TREE, addr,
3536 build_expr_list (NULL_TREE, size)));
3539 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3540 destructor, in which case the error should be deferred
3541 until someone actually tries to delete one of these. */
3542 if (flags & LOOKUP_SPECULATIVELY)
3543 return NULL_TREE;
3545 cp_error ("no suitable operator delete for `%T'", type);
3546 return error_mark_node;
3549 /* If the current scope isn't allowed to access DECL along
3550 BASETYPE_PATH, give an error. The most derived class in
3551 BASETYPE_PATH is the one used to qualify DECL. */
3554 enforce_access (basetype_path, decl)
3555 tree basetype_path;
3556 tree decl;
3558 int accessible;
3560 accessible = accessible_p (basetype_path, decl);
3561 if (!accessible)
3563 if (TREE_PRIVATE (decl))
3564 cp_error_at ("`%+#D' is private", decl);
3565 else if (TREE_PROTECTED (decl))
3566 cp_error_at ("`%+#D' is protected", decl);
3567 else
3568 cp_error_at ("`%+#D' is inaccessible", decl);
3569 cp_error ("within this context");
3570 return 0;
3573 return 1;
3576 /* Perform the conversions in CONVS on the expression EXPR. */
3578 static tree
3579 convert_like (convs, expr)
3580 tree convs, expr;
3582 if (ICS_BAD_FLAG (convs)
3583 && TREE_CODE (convs) != USER_CONV
3584 && TREE_CODE (convs) != AMBIG_CONV
3585 && TREE_CODE (convs) != REF_BIND)
3587 tree t = convs;
3588 for (; t; t = TREE_OPERAND (t, 0))
3590 if (TREE_CODE (t) == USER_CONV)
3592 expr = convert_like (t, expr);
3593 break;
3595 else if (TREE_CODE (t) == AMBIG_CONV)
3596 return convert_like (t, expr);
3597 else if (TREE_CODE (t) == IDENTITY_CONV)
3598 break;
3600 return convert_for_initialization
3601 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3602 "conversion", NULL_TREE, 0);
3605 switch (TREE_CODE (convs))
3607 case USER_CONV:
3609 struct z_candidate *cand
3610 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3611 tree fn = cand->fn;
3612 tree args;
3614 if (DECL_CONSTRUCTOR_P (fn))
3616 tree t = build_int_2 (0, 0);
3617 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3619 args = build_scratch_list (NULL_TREE, expr);
3620 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3621 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3622 args = scratch_tree_cons (NULL_TREE, t, args);
3624 else
3625 args = build_this (expr);
3626 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3628 /* If this is a constructor or a function returning an aggr type,
3629 we need to build up a TARGET_EXPR. */
3630 if (DECL_CONSTRUCTOR_P (fn))
3631 expr = build_cplus_new (TREE_TYPE (convs), expr);
3633 return expr;
3635 case IDENTITY_CONV:
3636 if (type_unknown_p (expr))
3637 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3638 return expr;
3639 case AMBIG_CONV:
3640 /* Call build_user_type_conversion again for the error. */
3641 return build_user_type_conversion
3642 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3644 default:
3645 break;
3648 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3649 if (expr == error_mark_node)
3650 return error_mark_node;
3652 /* Convert a constant variable to its underlying value, unless we
3653 are about to bind it to a reference, in which case we need to
3654 leave it as an lvalue. */
3655 if (TREE_READONLY_DECL_P (expr) && TREE_CODE (convs) != REF_BIND)
3656 expr = decl_constant_value (expr);
3658 switch (TREE_CODE (convs))
3660 case RVALUE_CONV:
3661 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3662 return expr;
3663 /* else fall through */
3664 case BASE_CONV:
3665 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3667 /* We are going to bind a reference directly to a base-class
3668 subobject of EXPR. */
3669 tree base_ptr = build_pointer_type (TREE_TYPE (convs));
3671 /* Build an expression for `*((base*) &expr)'. */
3672 expr = build_unary_op (ADDR_EXPR, expr, 0);
3673 expr = perform_implicit_conversion (base_ptr, expr);
3674 expr = build_indirect_ref (expr, "implicit conversion");
3675 return expr;
3679 tree cvt_expr = build_user_type_conversion
3680 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3681 if (!cvt_expr)
3683 /* This can occur if, for example, the EXPR has incomplete
3684 type. We can't check for that before attempting the
3685 conversion because the type might be an incomplete
3686 array type, which is OK if some constructor for the
3687 destination type takes a pointer argument. */
3688 if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
3690 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
3691 incomplete_type_error (expr, TREE_TYPE (expr));
3692 else
3693 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3694 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3696 else
3697 cp_error ("could not convert `%E' to `%T'",
3698 expr, TREE_TYPE (convs));
3699 return error_mark_node;
3701 return cvt_expr;
3704 case REF_BIND:
3706 tree ref_type = TREE_TYPE (convs);
3708 /* If necessary, create a temporary. */
3709 if (NEED_TEMPORARY_P (convs))
3711 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3712 tree slot = build_decl (VAR_DECL, NULL_TREE, type);
3713 DECL_ARTIFICIAL (slot) = 1;
3714 expr = build (TARGET_EXPR, type, slot, expr,
3715 NULL_TREE, NULL_TREE);
3716 TREE_SIDE_EFFECTS (expr) = 1;
3719 /* Take the address of the thing to which we will bind the
3720 reference. */
3721 expr = build_unary_op (ADDR_EXPR, expr, 1);
3722 if (expr == error_mark_node)
3723 return error_mark_node;
3725 /* Convert it to a pointer to the type referred to by the
3726 reference. This will adjust the pointer if a derived to
3727 base conversion is being performed. */
3728 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3729 expr);
3730 /* Convert the pointer to the desired reference type. */
3731 expr = build1 (NOP_EXPR, ref_type, expr);
3733 return expr;
3736 case LVALUE_CONV:
3737 return decay_conversion (expr);
3739 case QUAL_CONV:
3740 /* Warn about deprecated conversion if appropriate. */
3741 string_conv_p (TREE_TYPE (convs), expr, 1);
3742 break;
3744 default:
3745 break;
3747 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3748 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3751 /* ARG is being passed to a varargs function. Perform any conversions
3752 required. Array/function to pointer decay must have already happened.
3753 Return the converted value. */
3755 tree
3756 convert_arg_to_ellipsis (arg)
3757 tree arg;
3759 if (! pod_type_p (TREE_TYPE (arg)))
3761 /* Undefined behaviour [expr.call] 5.2.2/7. */
3762 cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3763 TREE_TYPE (arg));
3766 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3767 && (TYPE_PRECISION (TREE_TYPE (arg))
3768 < TYPE_PRECISION (double_type_node)))
3769 /* Convert `float' to `double'. */
3770 arg = cp_convert (double_type_node, arg);
3771 else
3772 /* Convert `short' and `char' to full-size `int'. */
3773 arg = default_conversion (arg);
3775 arg = require_complete_type (arg);
3777 return arg;
3780 /* ARG is a default argument expression being passed to a parameter of
3781 the indicated TYPE, which is a parameter to FN. Do any required
3782 conversions. Return the converted value. */
3784 tree
3785 convert_default_arg (type, arg, fn)
3786 tree type;
3787 tree arg;
3788 tree fn;
3790 if (fn && DECL_TEMPLATE_INFO (fn))
3791 arg = tsubst_default_argument (fn, type, arg);
3793 arg = break_out_target_exprs (arg);
3795 if (TREE_CODE (arg) == CONSTRUCTOR)
3797 arg = digest_init (type, arg, 0);
3798 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3799 "default argument", 0, 0);
3801 else
3803 /* This could get clobbered by the following call. */
3804 if (TREE_HAS_CONSTRUCTOR (arg))
3805 arg = copy_node (arg);
3807 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3808 "default argument", 0, 0);
3809 if (PROMOTE_PROTOTYPES
3810 && (TREE_CODE (type) == INTEGER_TYPE
3811 || TREE_CODE (type) == ENUMERAL_TYPE)
3812 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3813 arg = default_conversion (arg);
3816 return arg;
3819 static tree
3820 build_over_call (cand, args, flags)
3821 struct z_candidate *cand;
3822 tree args;
3823 int flags;
3825 tree fn = cand->fn;
3826 tree convs = cand->convs;
3827 tree converted_args = NULL_TREE;
3828 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3829 tree conv, arg, val;
3830 int i = 0;
3831 int is_method = 0;
3833 /* Give any warnings we noticed during overload resolution. */
3834 if (cand->warnings)
3835 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3836 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3838 if (DECL_FUNCTION_MEMBER_P (fn))
3839 enforce_access (cand->basetype_path, fn);
3841 if (args && TREE_CODE (args) != TREE_LIST)
3842 args = build_scratch_list (NULL_TREE, args);
3843 arg = args;
3845 /* The implicit parameters to a constructor are not considered by overload
3846 resolution, and must be of the proper type. */
3847 if (DECL_CONSTRUCTOR_P (fn))
3849 converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3850 arg = TREE_CHAIN (arg);
3851 parm = TREE_CHAIN (parm);
3852 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3854 converted_args = expr_tree_cons
3855 (NULL_TREE, TREE_VALUE (arg), converted_args);
3856 arg = TREE_CHAIN (arg);
3857 parm = TREE_CHAIN (parm);
3860 /* Bypass access control for 'this' parameter. */
3861 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3863 tree parmtype = TREE_VALUE (parm);
3864 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3865 tree t;
3866 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3867 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3868 TREE_TYPE (argtype), fn);
3870 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3871 X is called for an object that is not of type X, or of a type
3872 derived from X, the behavior is undefined.
3874 So we can assume that anything passed as 'this' is non-null, and
3875 optimize accordingly. */
3876 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
3877 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3878 converted_args = expr_tree_cons (NULL_TREE, t, converted_args);
3879 parm = TREE_CHAIN (parm);
3880 arg = TREE_CHAIN (arg);
3881 ++i;
3882 is_method = 1;
3885 for (; arg && parm;
3886 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3888 tree type = TREE_VALUE (parm);
3890 conv = TREE_VEC_ELT (convs, i);
3891 if (ICS_BAD_FLAG (conv))
3893 tree t = conv;
3894 val = TREE_VALUE (arg);
3896 for (; t; t = TREE_OPERAND (t, 0))
3898 if (TREE_CODE (t) == USER_CONV
3899 || TREE_CODE (t) == AMBIG_CONV)
3901 val = convert_like (t, val);
3902 break;
3904 else if (TREE_CODE (t) == IDENTITY_CONV)
3905 break;
3907 val = convert_for_initialization
3908 (NULL_TREE, type, val, LOOKUP_NORMAL,
3909 "argument passing", fn, i - is_method);
3911 else
3913 /* Issue warnings about peculiar, but legal, uses of NULL. */
3914 if (ARITHMETIC_TYPE_P (TREE_VALUE (parm))
3915 && TREE_VALUE (arg) == null_node)
3916 cp_warning ("converting NULL to non-pointer type");
3918 val = convert_like (conv, TREE_VALUE (arg));
3921 if (PROMOTE_PROTOTYPES
3922 && (TREE_CODE (type) == INTEGER_TYPE
3923 || TREE_CODE (type) == ENUMERAL_TYPE)
3924 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3925 val = default_conversion (val);
3926 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3929 /* Default arguments */
3930 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3931 converted_args
3932 = expr_tree_cons (NULL_TREE,
3933 convert_default_arg (TREE_VALUE (parm),
3934 TREE_PURPOSE (parm),
3935 fn),
3936 converted_args);
3938 /* Ellipsis */
3939 for (; arg; arg = TREE_CHAIN (arg))
3940 converted_args
3941 = expr_tree_cons (NULL_TREE,
3942 convert_arg_to_ellipsis (TREE_VALUE (arg)),
3943 converted_args);
3945 converted_args = nreverse (converted_args);
3947 if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
3948 check_function_format (DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
3949 converted_args);
3951 /* Avoid actually calling copy constructors and copy assignment operators,
3952 if possible. */
3954 if (! flag_elide_constructors)
3955 /* Do things the hard way. */;
3956 else if (DECL_CONSTRUCTOR_P (fn)
3957 && TREE_VEC_LENGTH (convs) == 1
3958 && copy_args_p (fn))
3960 tree targ;
3961 arg = TREE_CHAIN (converted_args);
3962 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3963 arg = TREE_CHAIN (arg);
3964 arg = TREE_VALUE (arg);
3966 /* Pull out the real argument, disregarding const-correctness. */
3967 targ = arg;
3968 while (TREE_CODE (targ) == NOP_EXPR
3969 || TREE_CODE (targ) == NON_LVALUE_EXPR
3970 || TREE_CODE (targ) == CONVERT_EXPR)
3971 targ = TREE_OPERAND (targ, 0);
3972 if (TREE_CODE (targ) == ADDR_EXPR)
3974 targ = TREE_OPERAND (targ, 0);
3975 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3976 TYPE_MAIN_VARIANT (TREE_TYPE (targ))))
3977 targ = NULL_TREE;
3979 else
3980 targ = NULL_TREE;
3982 if (targ)
3983 arg = targ;
3984 else
3985 arg = build_indirect_ref (arg, 0);
3987 /* [class.copy]: the copy constructor is implicitly defined even if
3988 the implementation elided its use. */
3989 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3990 mark_used (fn);
3992 /* If we're creating a temp and we already have one, don't create a
3993 new one. If we're not creating a temp but we get one, use
3994 INIT_EXPR to collapse the temp into our target. Otherwise, if the
3995 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3996 temp or an INIT_EXPR otherwise. */
3997 if (integer_zerop (TREE_VALUE (args)))
3999 if (! real_lvalue_p (arg))
4000 return arg;
4001 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4003 val = build_decl (VAR_DECL, NULL_TREE, DECL_CONTEXT (fn));
4004 val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
4005 TREE_SIDE_EFFECTS (val) = 1;
4006 return val;
4009 else if (! real_lvalue_p (arg)
4010 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4012 tree address;
4013 tree to = stabilize_reference
4014 (build_indirect_ref (TREE_VALUE (args), 0));
4016 /* If we're initializing an empty class, then we actually
4017 have to use a MODIFY_EXPR rather than an INIT_EXPR. The
4018 reason is that the dummy padding member in the target may
4019 not actually be allocated if TO is a base class
4020 subobject. Since we've set TYPE_NONCOPIED_PARTS on the
4021 padding, a MODIFY_EXPR will preserve its value, which is
4022 the right thing to do if it's not really padding at all.
4024 It's not safe to just throw away the ARG if we're looking
4025 at an empty class because the ARG might contain a
4026 TARGET_EXPR which wants to be bound to TO. If it is not,
4027 expand_expr will assign a dummy slot for the TARGET_EXPR,
4028 and we will call a destructor for it, which is wrong,
4029 because we will also destroy TO, but will never have
4030 constructed it. */
4031 val = build (is_empty_class (DECL_CLASS_CONTEXT (fn))
4032 ? MODIFY_EXPR : INIT_EXPR,
4033 DECL_CONTEXT (fn), to, arg);
4034 TREE_SIDE_EFFECTS (val) = 1;
4035 address = build_unary_op (ADDR_EXPR, val, 0);
4036 /* Avoid a warning about this expression, if the address is
4037 never used. */
4038 TREE_USED (address) = 1;
4039 return address;
4042 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
4043 && copy_args_p (fn)
4044 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
4046 tree to = stabilize_reference
4047 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4049 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4051 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4052 TREE_SIDE_EFFECTS (val) = 1;
4053 return val;
4056 mark_used (fn);
4058 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4060 tree t, *p = &TREE_VALUE (converted_args);
4061 tree binfo = get_binfo
4062 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
4063 *p = convert_pointer_to_real (binfo, *p);
4064 if (TREE_SIDE_EFFECTS (*p))
4065 *p = save_expr (*p);
4066 t = build_pointer_type (TREE_TYPE (fn));
4067 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4068 TREE_TYPE (fn) = t;
4070 else if (DECL_INLINE (fn))
4071 fn = inline_conversion (fn);
4072 else
4073 fn = build_addr_func (fn);
4075 /* Recognize certain built-in functions so we can make tree-codes
4076 other than CALL_EXPR. We do this when it enables fold-const.c
4077 to do something useful. */
4079 if (TREE_CODE (fn) == ADDR_EXPR
4080 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4081 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4082 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
4084 case BUILT_IN_ABS:
4085 case BUILT_IN_LABS:
4086 case BUILT_IN_FABS:
4087 if (converted_args == 0)
4088 return integer_zero_node;
4089 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
4090 default:
4091 break;
4094 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
4095 if (TREE_CODE (TREE_TYPE (fn)) == VOID_TYPE)
4096 return fn;
4097 fn = require_complete_type (fn);
4098 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4099 fn = build_cplus_new (TREE_TYPE (fn), fn);
4100 return convert_from_reference (fn);
4103 static tree
4104 build_new_method_call (instance, name, args, basetype_path, flags)
4105 tree instance, name, args, basetype_path;
4106 int flags;
4108 struct z_candidate *candidates = 0, *cand;
4109 tree explicit_targs = NULL_TREE;
4110 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4111 tree pretty_name;
4112 tree user_args = args;
4113 tree templates = NULL_TREE;
4114 int template_only = 0;
4116 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4118 explicit_targs = TREE_OPERAND (name, 1);
4119 name = TREE_OPERAND (name, 0);
4120 if (TREE_CODE_CLASS (TREE_CODE (name)) == 'd')
4121 name = DECL_NAME (name);
4122 else
4124 if (TREE_CODE (name) == COMPONENT_REF)
4125 name = TREE_OPERAND (name, 1);
4126 if (TREE_CODE (name) == OVERLOAD)
4127 name = DECL_NAME (OVL_CURRENT (name));
4130 template_only = 1;
4133 /* If there is an extra argument for controlling virtual bases,
4134 remove it for error reporting. */
4135 if (flags & LOOKUP_HAS_IN_CHARGE)
4136 user_args = TREE_CHAIN (args);
4138 args = resolve_args (args);
4140 if (args == error_mark_node)
4141 return error_mark_node;
4143 if (instance == NULL_TREE)
4144 basetype = BINFO_TYPE (basetype_path);
4145 else
4147 if (TREE_CODE (instance) == OFFSET_REF)
4148 instance = resolve_offset_ref (instance);
4149 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4150 instance = convert_from_reference (instance);
4151 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4153 /* XXX this should be handled before we get here. */
4154 if (! IS_AGGR_TYPE (basetype))
4156 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4157 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4158 name, instance, basetype);
4160 return error_mark_node;
4164 if (basetype_path == NULL_TREE)
4165 basetype_path = TYPE_BINFO (basetype);
4167 if (instance)
4169 instance_ptr = build_this (instance);
4171 if (! template_only)
4173 /* XXX this should be handled before we get here. */
4174 fns = build_field_call (basetype_path, instance_ptr, name, args);
4175 if (fns)
4176 return fns;
4179 else
4181 instance_ptr = build_int_2 (0, 0);
4182 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4185 pretty_name
4186 = (name == ctor_identifier ? constructor_name (basetype) : name);
4188 fns = lookup_fnfields (basetype_path, name, 1);
4190 if (fns == error_mark_node)
4191 return error_mark_node;
4192 if (fns)
4194 tree fn = TREE_VALUE (fns);
4195 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
4196 && ! (flags & LOOKUP_HAS_IN_CHARGE))
4198 flags |= LOOKUP_HAS_IN_CHARGE;
4199 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
4201 mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
4202 for (; fn; fn = OVL_NEXT (fn))
4204 tree t = OVL_CURRENT (fn);
4205 tree this_arglist;
4207 /* We can end up here for copy-init of same or base class. */
4208 if (name == ctor_identifier
4209 && (flags & LOOKUP_ONLYCONVERTING)
4210 && DECL_NONCONVERTING_P (t))
4211 continue;
4212 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
4213 this_arglist = mem_args;
4214 else
4215 this_arglist = args;
4217 if (TREE_CODE (t) == TEMPLATE_DECL)
4219 /* A member template. */
4220 templates = scratch_tree_cons (NULL_TREE, t, templates);
4221 candidates =
4222 add_template_candidate (candidates, t, explicit_targs,
4223 this_arglist,
4224 TREE_TYPE (name), flags, DEDUCE_CALL);
4226 else if (! template_only)
4227 candidates = add_function_candidate (candidates, t,
4228 this_arglist, flags);
4230 if (candidates)
4231 candidates->basetype_path = TREE_PURPOSE (fns);
4235 if (! any_viable (candidates))
4237 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4238 if (flags & LOOKUP_SPECULATIVELY)
4239 return NULL_TREE;
4240 if (TYPE_SIZE (basetype) == 0)
4241 incomplete_type_error (instance_ptr, basetype);
4242 else
4243 cp_error ("no matching function for call to `%T::%D (%A)%V'",
4244 basetype, pretty_name, user_args,
4245 TREE_TYPE (TREE_TYPE (instance_ptr)));
4246 print_z_candidates (candidates);
4247 return error_mark_node;
4249 candidates = splice_viable (candidates);
4250 cand = tourney (candidates);
4252 if (cand == 0)
4254 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4255 user_args);
4256 print_z_candidates (candidates);
4257 return error_mark_node;
4260 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
4261 && instance == current_class_ref
4262 && DECL_CONSTRUCTOR_P (current_function_decl)
4263 && ! (flags & LOOKUP_NONVIRTUAL)
4264 && value_member (cand->fn, CLASSTYPE_ABSTRACT_VIRTUALS (basetype)))
4265 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
4266 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4267 && is_dummy_object (instance_ptr))
4269 cp_error ("cannot call member function `%D' without object", cand->fn);
4270 return error_mark_node;
4273 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4274 && ((instance == current_class_ref && (dtor_label || ctor_label))
4275 || resolves_to_fixed_type_p (instance, 0)))
4276 flags |= LOOKUP_NONVIRTUAL;
4278 /* Pedantically, normal function declarations are never considered
4279 to refer to template instantiations, so we only do this with
4280 -fguiding-decls. */
4281 if (flag_guiding_decls && templates && ! cand->template
4282 && ! DECL_INITIAL (cand->fn))
4283 add_maybe_template (cand->fn, templates);
4285 return build_over_call
4286 (cand,
4287 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
4288 flags);
4291 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4292 subsequence of ICS2. */
4294 static int
4295 is_subseq (ics1, ics2)
4296 tree ics1, ics2;
4298 /* We can assume that a conversion of the same code
4299 between the same types indicates a subsequence since we only get
4300 here if the types we are converting from are the same. */
4302 while (TREE_CODE (ics1) == RVALUE_CONV
4303 || TREE_CODE (ics1) == LVALUE_CONV)
4304 ics1 = TREE_OPERAND (ics1, 0);
4306 while (1)
4308 while (TREE_CODE (ics2) == RVALUE_CONV
4309 || TREE_CODE (ics2) == LVALUE_CONV)
4310 ics2 = TREE_OPERAND (ics2, 0);
4312 if (TREE_CODE (ics2) == USER_CONV
4313 || TREE_CODE (ics2) == AMBIG_CONV
4314 || TREE_CODE (ics2) == IDENTITY_CONV)
4315 /* At this point, ICS1 cannot be a proper subsequence of
4316 ICS2. We can get a USER_CONV when we are comparing the
4317 second standard conversion sequence of two user conversion
4318 sequences. */
4319 return 0;
4321 ics2 = TREE_OPERAND (ics2, 0);
4323 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4324 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4325 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4326 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4327 return 1;
4331 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4332 be any _TYPE nodes. */
4335 is_properly_derived_from (derived, base)
4336 tree derived;
4337 tree base;
4339 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4340 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4341 return 0;
4343 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4344 considers every class derived from itself. */
4345 return (!same_type_p (TYPE_MAIN_VARIANT (derived),
4346 TYPE_MAIN_VARIANT (base))
4347 && DERIVED_FROM_P (base, derived));
4350 /* We build the ICS for an implicit object parameter as a pointer
4351 conversion sequence. However, such a sequence should be compared
4352 as if it were a reference conversion sequence. If ICS is the
4353 implicit conversion sequence for an implicit object parameter,
4354 modify it accordingly. */
4356 static void
4357 maybe_handle_implicit_object (ics)
4358 tree* ics;
4360 if (ICS_THIS_FLAG (*ics))
4362 /* [over.match.funcs]
4364 For non-static member functions, the type of the
4365 implicit object parameter is "reference to cv X"
4366 where X is the class of which the function is a
4367 member and cv is the cv-qualification on the member
4368 function declaration. */
4369 tree t = *ics;
4370 tree reference_type;
4372 /* The `this' parameter is a pointer to a class type. Make the
4373 implict conversion talk about a reference to that same class
4374 type. */
4375 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4376 reference_type = build_reference_type (reference_type);
4378 if (TREE_CODE (t) == QUAL_CONV)
4379 t = TREE_OPERAND (t, 0);
4380 if (TREE_CODE (t) == PTR_CONV)
4381 t = TREE_OPERAND (t, 0);
4382 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4383 t = direct_reference_binding (reference_type, t);
4384 *ics = t;
4388 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
4389 to the type the reference originally referred to, and return 1.
4390 Otherwise, return 0. */
4392 static int
4393 maybe_handle_ref_bind (ics, target_type)
4394 tree* ics;
4395 tree* target_type;
4397 if (TREE_CODE (*ics) == REF_BIND)
4399 *target_type = TREE_TYPE (TREE_TYPE (*ics));
4400 *ics = TREE_OPERAND (*ics, 0);
4401 return 1;
4404 return 0;
4407 /* Compare two implicit conversion sequences according to the rules set out in
4408 [over.ics.rank]. Return values:
4410 1: ics1 is better than ics2
4411 -1: ics2 is better than ics1
4412 0: ics1 and ics2 are indistinguishable */
4414 static int
4415 compare_ics (ics1, ics2)
4416 tree ics1, ics2;
4418 tree from_type1;
4419 tree from_type2;
4420 tree to_type1;
4421 tree to_type2;
4422 tree deref_from_type1 = NULL_TREE;
4423 tree deref_from_type2 = NULL_TREE;
4424 tree deref_to_type1 = NULL_TREE;
4425 tree deref_to_type2 = NULL_TREE;
4427 /* REF_BINDING is non-zero if the result of the conversion sequence
4428 is a reference type. In that case TARGET_TYPE is the
4429 type referred to by the reference. */
4430 int ref_binding1;
4431 int ref_binding2;
4432 tree target_type1;
4433 tree target_type2;
4435 /* Handle implicit object parameters. */
4436 maybe_handle_implicit_object (&ics1);
4437 maybe_handle_implicit_object (&ics2);
4439 /* Handle reference parameters. */
4440 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
4441 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
4443 /* [over.ics.rank]
4445 When comparing the basic forms of implicit conversion sequences (as
4446 defined in _over.best.ics_)
4448 --a standard conversion sequence (_over.ics.scs_) is a better
4449 conversion sequence than a user-defined conversion sequence
4450 or an ellipsis conversion sequence, and
4452 --a user-defined conversion sequence (_over.ics.user_) is a
4453 better conversion sequence than an ellipsis conversion sequence
4454 (_over.ics.ellipsis_). */
4455 if (ICS_RANK (ics1) > ICS_RANK (ics2))
4456 return -1;
4457 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
4458 return 1;
4460 if (ICS_RANK (ics1) == BAD_RANK)
4462 /* Both ICS are bad. We try to make a decision based on what
4463 would have happenned if they'd been good. */
4464 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4465 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4466 return -1;
4467 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4468 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4469 return 1;
4471 /* We couldn't make up our minds; try to figure it out below. */
4474 if (ICS_ELLIPSIS_FLAG (ics1))
4475 /* Both conversions are ellipsis conversions. */
4476 return 0;
4478 /* User-defined conversion sequence U1 is a better conversion sequence
4479 than another user-defined conversion sequence U2 if they contain the
4480 same user-defined conversion operator or constructor and if the sec-
4481 ond standard conversion sequence of U1 is better than the second
4482 standard conversion sequence of U2. */
4484 if (ICS_USER_FLAG (ics1))
4486 tree t1, t2;
4488 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4489 if (TREE_CODE (t1) == AMBIG_CONV)
4490 return 0;
4491 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4492 if (TREE_CODE (t2) == AMBIG_CONV)
4493 return 0;
4495 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4496 return 0;
4498 /* We can just fall through here, after setting up
4499 FROM_TYPE1 and FROM_TYPE2. */
4500 from_type1 = TREE_TYPE (t1);
4501 from_type2 = TREE_TYPE (t2);
4503 else
4505 /* We're dealing with two standard conversion sequences.
4507 [over.ics.rank]
4509 Standard conversion sequence S1 is a better conversion
4510 sequence than standard conversion sequence S2 if
4512 --S1 is a proper subsequence of S2 (comparing the conversion
4513 sequences in the canonical form defined by _over.ics.scs_,
4514 excluding any Lvalue Transformation; the identity
4515 conversion sequence is considered to be a subsequence of
4516 any non-identity conversion sequence */
4518 from_type1 = ics1;
4519 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4520 from_type1 = TREE_OPERAND (from_type1, 0);
4521 from_type1 = TREE_TYPE (from_type1);
4523 from_type2 = ics2;
4524 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4525 from_type2 = TREE_OPERAND (from_type2, 0);
4526 from_type2 = TREE_TYPE (from_type2);
4529 if (same_type_p (from_type1, from_type2))
4531 if (is_subseq (ics1, ics2))
4532 return 1;
4533 if (is_subseq (ics2, ics1))
4534 return -1;
4536 /* Otherwise, one sequence cannot be a subsequence of the other; they
4537 don't start with the same type. This can happen when comparing the
4538 second standard conversion sequence in two user-defined conversion
4539 sequences. */
4541 /* [over.ics.rank]
4543 Or, if not that,
4545 --the rank of S1 is better than the rank of S2 (by the rules
4546 defined below):
4548 Standard conversion sequences are ordered by their ranks: an Exact
4549 Match is a better conversion than a Promotion, which is a better
4550 conversion than a Conversion.
4552 Two conversion sequences with the same rank are indistinguishable
4553 unless one of the following rules applies:
4555 --A conversion that is not a conversion of a pointer, or pointer
4556 to member, to bool is better than another conversion that is such
4557 a conversion.
4559 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4560 so that we do not have to check it explicitly. */
4561 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4562 return 1;
4563 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4564 return -1;
4566 to_type1 = TREE_TYPE (ics1);
4567 to_type2 = TREE_TYPE (ics2);
4569 if (TYPE_PTR_P (from_type1)
4570 && TYPE_PTR_P (from_type2)
4571 && TYPE_PTR_P (to_type1)
4572 && TYPE_PTR_P (to_type2))
4574 deref_from_type1 = TREE_TYPE (from_type1);
4575 deref_from_type2 = TREE_TYPE (from_type2);
4576 deref_to_type1 = TREE_TYPE (to_type1);
4577 deref_to_type2 = TREE_TYPE (to_type2);
4579 /* The rules for pointers to members A::* are just like the rules
4580 for pointers A*, except opposite: if B is derived from A then
4581 A::* converts to B::*, not vice versa. For that reason, we
4582 switch the from_ and to_ variables here. */
4583 else if (TYPE_PTRMEM_P (from_type1)
4584 && TYPE_PTRMEM_P (from_type2)
4585 && TYPE_PTRMEM_P (to_type1)
4586 && TYPE_PTRMEM_P (to_type2))
4588 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4589 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4590 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4591 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4593 else if (TYPE_PTRMEMFUNC_P (from_type1)
4594 && TYPE_PTRMEMFUNC_P (from_type2)
4595 && TYPE_PTRMEMFUNC_P (to_type1)
4596 && TYPE_PTRMEMFUNC_P (to_type2))
4598 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4599 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4600 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4601 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4604 if (deref_from_type1 != NULL_TREE
4605 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4606 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4608 /* This was one of the pointer or pointer-like conversions.
4610 [over.ics.rank]
4612 --If class B is derived directly or indirectly from class A,
4613 conversion of B* to A* is better than conversion of B* to
4614 void*, and conversion of A* to void* is better than
4615 conversion of B* to void*. */
4616 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4617 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4619 if (is_properly_derived_from (deref_from_type1,
4620 deref_from_type2))
4621 return -1;
4622 else if (is_properly_derived_from (deref_from_type2,
4623 deref_from_type1))
4624 return 1;
4626 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4627 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4629 if (same_type_p (deref_from_type1, deref_from_type2))
4631 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4633 if (is_properly_derived_from (deref_from_type1,
4634 deref_to_type1))
4635 return 1;
4637 /* We know that DEREF_TO_TYPE1 is `void' here. */
4638 else if (is_properly_derived_from (deref_from_type1,
4639 deref_to_type2))
4640 return -1;
4643 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4644 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4646 /* [over.ics.rank]
4648 --If class B is derived directly or indirectly from class A
4649 and class C is derived directly or indirectly from B,
4651 --conversion of C* to B* is better than conversion of C* to
4652 A*,
4654 --conversion of B* to A* is better than conversion of C* to
4655 A* */
4656 if (same_type_p (deref_from_type1, deref_from_type2))
4658 if (is_properly_derived_from (deref_to_type1,
4659 deref_to_type2))
4660 return 1;
4661 else if (is_properly_derived_from (deref_to_type2,
4662 deref_to_type1))
4663 return -1;
4665 else if (same_type_p (deref_to_type1, deref_to_type2))
4667 if (is_properly_derived_from (deref_from_type2,
4668 deref_from_type1))
4669 return 1;
4670 else if (is_properly_derived_from (deref_from_type1,
4671 deref_from_type2))
4672 return -1;
4676 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4677 && same_type_p (from_type1, from_type2))
4679 /* [over.ics.rank]
4681 --binding of an expression of type C to a reference of type
4682 B& is better than binding an expression of type C to a
4683 reference of type A&
4685 --conversion of C to B is better than conversion of C to A, */
4686 if (is_properly_derived_from (from_type1, to_type1)
4687 && is_properly_derived_from (from_type1, to_type2))
4689 if (is_properly_derived_from (to_type1, to_type2))
4690 return 1;
4691 else if (is_properly_derived_from (to_type2, to_type1))
4692 return -1;
4695 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4696 && same_type_p (to_type1, to_type2))
4698 /* [over.ics.rank]
4700 --binding of an expression of type B to a reference of type
4701 A& is better than binding an expression of type C to a
4702 reference of type A&,
4704 --onversion of B to A is better than conversion of C to A */
4705 if (is_properly_derived_from (from_type1, to_type1)
4706 && is_properly_derived_from (from_type2, to_type1))
4708 if (is_properly_derived_from (from_type2, from_type1))
4709 return 1;
4710 else if (is_properly_derived_from (from_type1, from_type2))
4711 return -1;
4715 /* [over.ics.rank]
4717 --S1 and S2 differ only in their qualification conversion and yield
4718 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4719 qualification signature of type T1 is a proper subset of the cv-
4720 qualification signature of type T2 */
4721 if (TREE_CODE (ics1) == QUAL_CONV
4722 && TREE_CODE (ics2) == QUAL_CONV
4723 && same_type_p (from_type1, from_type2))
4724 return comp_cv_qual_signature (to_type1, to_type2);
4726 /* [over.ics.rank]
4728 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4729 types to which the references refer are the same type except for
4730 top-level cv-qualifiers, and the type to which the reference
4731 initialized by S2 refers is more cv-qualified than the type to
4732 which the reference initialized by S1 refers */
4734 if (ref_binding1 && ref_binding2
4735 && same_type_p (TYPE_MAIN_VARIANT (to_type1),
4736 TYPE_MAIN_VARIANT (to_type2)))
4737 return comp_cv_qualification (target_type2, target_type1);
4739 /* Neither conversion sequence is better than the other. */
4740 return 0;
4743 /* The source type for this standard conversion sequence. */
4745 static tree
4746 source_type (t)
4747 tree t;
4749 for (;; t = TREE_OPERAND (t, 0))
4751 if (TREE_CODE (t) == USER_CONV
4752 || TREE_CODE (t) == AMBIG_CONV
4753 || TREE_CODE (t) == IDENTITY_CONV)
4754 return TREE_TYPE (t);
4756 my_friendly_abort (1823);
4759 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4760 a pointer to LOSER and re-running joust to produce the warning if WINNER
4761 is actually used. */
4763 static void
4764 add_warning (winner, loser)
4765 struct z_candidate *winner, *loser;
4767 winner->warnings = expr_tree_cons (NULL_PTR,
4768 build_expr_ptr_wrapper (loser),
4769 winner->warnings);
4772 /* Returns true iff functions are equivalent. Equivalent functions are
4773 not identical only if one is a function-local extern function.
4774 This assumes that function-locals don't have TREE_PERMANENT. */
4776 static inline int
4777 equal_functions (fn1, fn2)
4778 tree fn1;
4779 tree fn2;
4781 if (!TREE_PERMANENT (fn1) || !TREE_PERMANENT (fn2))
4782 return decls_match (fn1, fn2);
4783 return fn1 == fn2;
4786 /* Compare two candidates for overloading as described in
4787 [over.match.best]. Return values:
4789 1: cand1 is better than cand2
4790 -1: cand2 is better than cand1
4791 0: cand1 and cand2 are indistinguishable */
4793 static int
4794 joust (cand1, cand2, warn)
4795 struct z_candidate *cand1, *cand2;
4796 int warn;
4798 int winner = 0;
4799 int i, off1 = 0, off2 = 0, len;
4801 /* Candidates that involve bad conversions are always worse than those
4802 that don't. */
4803 if (cand1->viable > cand2->viable)
4804 return 1;
4805 if (cand1->viable < cand2->viable)
4806 return -1;
4808 /* If we have two pseudo-candidates for conversions to the same type,
4809 arbitrarily pick one. */
4810 if (TYPE_P (cand1->fn) && cand1->fn == cand2->fn)
4811 return 1;
4813 /* a viable function F1
4814 is defined to be a better function than another viable function F2 if
4815 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4816 ICSi(F2), and then */
4818 /* for some argument j, ICSj(F1) is a better conversion sequence than
4819 ICSj(F2) */
4821 /* For comparing static and non-static member functions, we ignore the
4822 implicit object parameter of the non-static function. The WP says to
4823 pretend that the static function has an object parm, but that won't
4824 work with operator overloading. */
4825 len = TREE_VEC_LENGTH (cand1->convs);
4826 if (len != TREE_VEC_LENGTH (cand2->convs))
4828 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4829 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4830 off2 = 1;
4831 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4832 && DECL_STATIC_FUNCTION_P (cand2->fn))
4834 off1 = 1;
4835 --len;
4837 else
4838 my_friendly_abort (42);
4841 for (i = 0; i < len; ++i)
4843 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4844 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4845 int comp = compare_ics (t1, t2);
4847 if (comp != 0)
4849 if (warn_sign_promo
4850 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4851 && TREE_CODE (t1) == STD_CONV
4852 && TREE_CODE (t2) == STD_CONV
4853 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4854 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4855 && (TYPE_PRECISION (TREE_TYPE (t1))
4856 == TYPE_PRECISION (TREE_TYPE (t2)))
4857 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4858 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4859 == ENUMERAL_TYPE)))
4861 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4862 tree type1, type2;
4863 struct z_candidate *w, *l;
4864 if (comp > 0)
4865 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4866 w = cand1, l = cand2;
4867 else
4868 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4869 w = cand2, l = cand1;
4871 if (warn)
4873 cp_warning ("passing `%T' chooses `%T' over `%T'",
4874 type, type1, type2);
4875 cp_warning (" in call to `%D'", w->fn);
4877 else
4878 add_warning (w, l);
4881 if (winner && comp != winner)
4883 winner = 0;
4884 goto tweak;
4886 winner = comp;
4890 /* warn about confusing overload resolution for user-defined conversions,
4891 either between a constructor and a conversion op, or between two
4892 conversion ops. */
4893 if (winner && cand1->second_conv
4894 && ((DECL_CONSTRUCTOR_P (cand1->fn)
4895 != DECL_CONSTRUCTOR_P (cand2->fn))
4896 /* Don't warn if the two conv ops convert to the same type... */
4897 || (! DECL_CONSTRUCTOR_P (cand1->fn)
4898 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
4899 TREE_TYPE (TREE_TYPE (cand2->fn))))))
4901 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4902 if (comp != winner)
4904 struct z_candidate *w, *l;
4905 if (winner == 1)
4906 w = cand1, l = cand2;
4907 else
4908 w = cand2, l = cand1;
4909 if (warn)
4911 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4912 if (! DECL_CONSTRUCTOR_P (w->fn))
4913 source = TREE_TYPE (source);
4914 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4915 cp_warning (" for conversion from `%T' to `%T'",
4916 source, TREE_TYPE (w->second_conv));
4917 cp_warning (" because conversion sequence for the argument is better");
4919 else
4920 add_warning (w, l);
4924 if (winner)
4925 return winner;
4927 /* or, if not that,
4928 F1 is a non-template function and F2 is a template function */
4930 if (! cand1->template && cand2->template)
4931 return 1;
4932 else if (cand1->template && ! cand2->template)
4933 return -1;
4934 else if (cand1->template && cand2->template)
4935 winner = more_specialized
4936 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4937 NULL_TREE);
4939 /* or, if not that,
4940 the context is an initialization by user-defined conversion (see
4941 _dcl.init_ and _over.match.user_) and the standard conversion
4942 sequence from the return type of F1 to the destination type (i.e.,
4943 the type of the entity being initialized) is a better conversion
4944 sequence than the standard conversion sequence from the return type
4945 of F2 to the destination type. */
4947 if (! winner && cand1->second_conv)
4948 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4950 /* If the built-in candidates are the same, arbitrarily pick one. */
4951 if (! winner && cand1->fn == cand2->fn
4952 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4954 for (i = 0; i < len; ++i)
4955 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4956 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
4957 break;
4958 if (i == TREE_VEC_LENGTH (cand1->convs))
4959 return 1;
4961 /* Kludge around broken overloading rules whereby
4962 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4963 that takes references and another that takes values. */
4964 if (cand1->fn == ansi_opname[COND_EXPR])
4966 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4967 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4968 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4969 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4971 if (same_type_p (t1, t2))
4973 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4974 return 1;
4975 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4976 return -1;
4981 /* If the two functions are the same (this can happen with declarations
4982 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
4983 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
4984 && equal_functions (cand1->fn, cand2->fn))
4985 return 1;
4987 tweak:
4989 /* Extension: If the worst conversion for one candidate is worse than the
4990 worst conversion for the other, take the first. */
4991 if (! winner && ! pedantic)
4993 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4995 for (i = 0; i < len; ++i)
4997 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4998 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4999 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5000 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5003 if (rank1 < rank2)
5004 return 1;
5005 if (rank1 > rank2)
5006 return -1;
5009 return winner;
5012 /* Given a list of candidates for overloading, find the best one, if any.
5013 This algorithm has a worst case of O(2n) (winner is last), and a best
5014 case of O(n/2) (totally ambiguous); much better than a sorting
5015 algorithm. */
5017 static struct z_candidate *
5018 tourney (candidates)
5019 struct z_candidate *candidates;
5021 struct z_candidate *champ = candidates, *challenger;
5022 int fate;
5023 int champ_compared_to_predecessor = 0;
5025 /* Walk through the list once, comparing each current champ to the next
5026 candidate, knocking out a candidate or two with each comparison. */
5028 for (challenger = champ->next; challenger; )
5030 fate = joust (champ, challenger, 0);
5031 if (fate == 1)
5032 challenger = challenger->next;
5033 else
5035 if (fate == 0)
5037 champ = challenger->next;
5038 if (champ == 0)
5039 return 0;
5040 champ_compared_to_predecessor = 0;
5042 else
5044 champ = challenger;
5045 champ_compared_to_predecessor = 1;
5048 challenger = champ->next;
5052 /* Make sure the champ is better than all the candidates it hasn't yet
5053 been compared to. */
5055 for (challenger = candidates;
5056 challenger != champ
5057 && !(champ_compared_to_predecessor && challenger->next == champ);
5058 challenger = challenger->next)
5060 fate = joust (champ, challenger, 0);
5061 if (fate != 1)
5062 return 0;
5065 return champ;
5068 /* Returns non-zero if things of type FROM can be converted to TO. */
5071 can_convert (to, from)
5072 tree to, from;
5074 return can_convert_arg (to, from, NULL_TREE);
5077 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5080 can_convert_arg (to, from, arg)
5081 tree to, from, arg;
5083 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5084 return (t && ! ICS_BAD_FLAG (t));
5087 /* Convert EXPR to TYPE. Return the converted expression. */
5089 tree
5090 perform_implicit_conversion (type, expr)
5091 tree type;
5092 tree expr;
5094 tree conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5095 LOOKUP_NORMAL);
5096 if (!conv || ICS_BAD_FLAG (conv))
5098 cp_error ("could not convert `%E' to `%T'", expr, type);
5099 return error_mark_node;
5102 return convert_like (conv, expr);
5105 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5106 initializing a variable of that TYPE. Return the converted
5107 expression. */
5109 tree
5110 initialize_reference (type, expr)
5111 tree type;
5112 tree expr;
5114 tree conv;
5116 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5117 if (!conv || ICS_BAD_FLAG (conv))
5119 cp_error ("could not convert `%E' to `%T'", expr, type);
5120 return error_mark_node;
5123 return convert_like (conv, expr);