Add FR30 to list of targets for which -fpic is inappropriate.
[official-gcc.git] / gcc / cp / call.c
blob8cbc9dd17acfe93e7552f3071f163141d4f288a8
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 92-97, 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "output.h"
31 #include "flags.h"
32 #include "rtl.h"
33 #include "toplev.h"
34 #include "defaults.h"
36 #include "obstack.h"
37 #define obstack_chunk_alloc xmalloc
38 #define obstack_chunk_free free
40 extern int inhibit_warnings;
42 static tree build_new_method_call PROTO((tree, tree, tree, tree, int));
44 static tree build_field_call PROTO((tree, tree, tree, tree));
45 static struct z_candidate * tourney PROTO((struct z_candidate *));
46 static int equal_functions PROTO((tree, tree));
47 static int joust PROTO((struct z_candidate *, struct z_candidate *, int));
48 static int compare_ics PROTO((tree, tree));
49 static tree build_over_call PROTO((struct z_candidate *, tree, int));
50 static tree convert_like PROTO((tree, tree));
51 static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
52 tree, const char *));
53 static tree build_object_call PROTO((tree, tree));
54 static tree resolve_args PROTO((tree));
55 static struct z_candidate * build_user_type_conversion_1
56 PROTO ((tree, tree, int));
57 static void print_z_candidates PROTO((struct z_candidate *));
58 static tree build_this PROTO((tree));
59 static struct z_candidate * splice_viable PROTO((struct z_candidate *));
60 static int any_viable PROTO((struct z_candidate *));
61 static struct z_candidate * add_template_candidate
62 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
63 unification_kind_t));
64 static struct z_candidate * add_template_candidate_real
65 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
66 tree, unification_kind_t));
67 static struct z_candidate * add_template_conv_candidate
68 PROTO((struct z_candidate *, tree, tree, tree, tree));
69 static struct z_candidate * add_builtin_candidates
70 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
71 tree, tree *, int));
72 static struct z_candidate * add_builtin_candidate
73 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
74 tree, tree, tree, tree *, tree *, int));
75 static int is_complete PROTO((tree));
76 static struct z_candidate * build_builtin_candidate
77 PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
78 int));
79 static struct z_candidate * add_conv_candidate
80 PROTO((struct z_candidate *, tree, tree, tree));
81 static struct z_candidate * add_function_candidate
82 PROTO((struct z_candidate *, tree, tree, int));
83 static tree implicit_conversion PROTO((tree, tree, tree, int));
84 static tree standard_conversion PROTO((tree, tree, tree));
85 static tree reference_binding PROTO((tree, tree, tree, int));
86 static tree non_reference PROTO((tree));
87 static tree build_conv PROTO((enum tree_code, tree, tree));
88 static int is_subseq PROTO((tree, tree));
89 static int maybe_handle_ref_bind PROTO((tree*, tree*));
90 static void maybe_handle_implicit_object PROTO((tree*));
91 static struct z_candidate * add_candidate PROTO((struct z_candidate *,
92 tree, tree, int));
93 static tree source_type PROTO((tree));
94 static void add_warning PROTO((struct z_candidate *, struct z_candidate *));
95 static int reference_related_p PROTO ((tree, tree));
96 static int reference_compatible_p PROTO ((tree, tree));
97 static tree convert_class_to_reference PROTO ((tree, tree, tree));
98 static tree direct_reference_binding PROTO ((tree, tree));
99 static int promoted_arithmetic_type_p PROTO ((tree));
100 static tree conditional_conversion PROTO ((tree, tree));
102 tree
103 build_vfield_ref (datum, type)
104 tree datum, type;
106 tree rval;
108 if (datum == error_mark_node)
109 return error_mark_node;
111 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
112 datum = convert_from_reference (datum);
114 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
115 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
116 datum, CLASSTYPE_VFIELD (type));
117 else
118 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
120 return rval;
123 /* Build a call to a member of an object. I.e., one that overloads
124 operator ()(), or is a pointer-to-function or pointer-to-method. */
126 static tree
127 build_field_call (basetype_path, instance_ptr, name, parms)
128 tree basetype_path, instance_ptr, name, parms;
130 tree field, instance;
132 if (name == ctor_identifier || name == dtor_identifier)
133 return NULL_TREE;
135 /* Speed up the common case. */
136 if (instance_ptr == current_class_ptr
137 && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
138 return NULL_TREE;
140 field = lookup_field (basetype_path, name, 1, 0);
142 if (field == error_mark_node || field == NULL_TREE)
143 return field;
145 if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
147 /* If it's a field, try overloading operator (),
148 or calling if the field is a pointer-to-function. */
149 instance = build_indirect_ref (instance_ptr, NULL_PTR);
150 instance = build_component_ref_1 (instance, field, 0);
152 if (instance == error_mark_node)
153 return error_mark_node;
155 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
156 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
157 instance, parms, NULL_TREE);
158 else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
160 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
161 return build_function_call (instance, parms);
162 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
163 == METHOD_TYPE)
164 return build_function_call
165 (instance, tree_cons (NULL_TREE, instance_ptr, parms));
169 return NULL_TREE;
172 /* Returns nonzero iff the destructor name specified in NAME
173 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
174 forms... */
177 check_dtor_name (basetype, name)
178 tree basetype, name;
180 name = TREE_OPERAND (name, 0);
182 /* Just accept something we've already complained about. */
183 if (name == error_mark_node)
184 return 1;
186 if (TREE_CODE (name) == TYPE_DECL)
187 name = TREE_TYPE (name);
188 else if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
189 /* OK */;
190 else if (TREE_CODE (name) == IDENTIFIER_NODE)
192 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
193 || (TREE_CODE (basetype) == ENUMERAL_TYPE
194 && name == TYPE_IDENTIFIER (basetype)))
195 name = basetype;
196 else
197 name = get_type_value (name);
199 else
200 my_friendly_abort (980605);
202 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
203 return 1;
204 return 0;
207 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
208 This is how virtual function calls are avoided. */
210 tree
211 build_scoped_method_call (exp, basetype, name, parms)
212 tree exp, basetype, name, parms;
214 /* Because this syntactic form does not allow
215 a pointer to a base class to be `stolen',
216 we need not protect the derived->base conversion
217 that happens here.
219 @@ But we do have to check access privileges later. */
220 tree binfo, decl;
221 tree type = TREE_TYPE (exp);
223 if (type == error_mark_node
224 || basetype == error_mark_node)
225 return error_mark_node;
227 if (processing_template_decl)
229 if (TREE_CODE (name) == BIT_NOT_EXPR
230 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
232 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
233 if (type)
234 name = build_min_nt (BIT_NOT_EXPR, type);
236 name = build_min_nt (SCOPE_REF, basetype, name);
237 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
240 if (TREE_CODE (type) == REFERENCE_TYPE)
241 type = TREE_TYPE (type);
243 if (TREE_CODE (basetype) == TREE_VEC)
245 binfo = basetype;
246 basetype = BINFO_TYPE (binfo);
248 else
249 binfo = NULL_TREE;
251 /* Check the destructor call syntax. */
252 if (TREE_CODE (name) == BIT_NOT_EXPR)
254 /* We can get here if someone writes their destructor call like
255 `obj.NS::~T()'; this isn't really a scoped method call, so hand
256 it off. */
257 if (TREE_CODE (basetype) == NAMESPACE_DECL)
258 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
260 if (! check_dtor_name (basetype, name))
261 cp_error ("qualified type `%T' does not match destructor name `~%T'",
262 basetype, TREE_OPERAND (name, 0));
264 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
265 that explicit ~int is caught in the parser; this deals with typedefs
266 and template parms. */
267 if (! IS_AGGR_TYPE (basetype))
269 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
270 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
271 exp, basetype, type);
273 return cp_convert (void_type_node, exp);
277 if (! is_aggr_type (basetype, 1))
278 return error_mark_node;
280 if (! IS_AGGR_TYPE (type))
282 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
283 exp, type);
284 return error_mark_node;
287 if (! binfo)
289 binfo = get_binfo (basetype, type, 1);
290 if (binfo == error_mark_node)
291 return error_mark_node;
292 if (! binfo)
293 error_not_base_type (basetype, type);
296 if (binfo)
298 if (TREE_CODE (exp) == INDIRECT_REF)
299 decl = build_indirect_ref
300 (convert_pointer_to_real
301 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
302 else
303 decl = build_scoped_ref (exp, basetype);
305 /* Call to a destructor. */
306 if (TREE_CODE (name) == BIT_NOT_EXPR)
308 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
309 return cp_convert (void_type_node, exp);
311 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
312 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
316 /* Call to a method. */
317 return build_method_call (decl, name, parms, binfo,
318 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
320 return error_mark_node;
323 /* We want the address of a function or method. We avoid creating a
324 pointer-to-member function. */
326 tree
327 build_addr_func (function)
328 tree function;
330 tree type = TREE_TYPE (function);
332 /* We have to do these by hand to avoid real pointer to member
333 functions. */
334 if (TREE_CODE (type) == METHOD_TYPE)
336 tree addr;
338 type = build_pointer_type (type);
340 if (mark_addressable (function) == 0)
341 return error_mark_node;
343 addr = build1 (ADDR_EXPR, type, function);
345 /* Address of a static or external variable or function counts
346 as a constant */
347 if (staticp (function))
348 TREE_CONSTANT (addr) = 1;
350 function = addr;
352 else
353 function = default_conversion (function);
355 return function;
358 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
359 POINTER_TYPE to those. Note, pointer to member function types
360 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
362 tree
363 build_call (function, result_type, parms)
364 tree function, result_type, parms;
366 int is_constructor = 0;
367 tree tmp;
368 tree decl;
370 function = build_addr_func (function);
372 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
374 sorry ("unable to call pointer to member function here");
375 return error_mark_node;
378 if (TREE_CODE (function) == ADDR_EXPR
379 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
380 decl = TREE_OPERAND (function, 0);
381 else
382 decl = NULL_TREE;
384 if (decl && DECL_CONSTRUCTOR_P (decl))
385 is_constructor = 1;
387 if (decl)
388 my_friendly_assert (TREE_USED (decl), 990125);
390 /* Don't pass empty class objects by value. This is useful
391 for tags in STL, which are used to control overload resolution.
392 We don't need to handle other cases of copying empty classes. */
393 if (! decl || ! DECL_BUILT_IN (decl))
394 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
395 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
396 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
398 tree t = make_node (RTL_EXPR);
399 TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
400 RTL_EXPR_RTL (t) = const0_rtx;
401 RTL_EXPR_SEQUENCE (t) = NULL_RTX;
402 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
403 TREE_VALUE (tmp), t);
406 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
407 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
408 TREE_TYPE (function) = result_type;
409 TREE_SIDE_EFFECTS (function) = 1;
411 return function;
414 /* Build something of the form ptr->method (args)
415 or object.method (args). This can also build
416 calls to constructors, and find friends.
418 Member functions always take their class variable
419 as a pointer.
421 INSTANCE is a class instance.
423 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
425 PARMS help to figure out what that NAME really refers to.
427 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
428 down to the real instance type to use for access checking. We need this
429 information to get protected accesses correct. This parameter is used
430 by build_member_call.
432 FLAGS is the logical disjunction of zero or more LOOKUP_
433 flags. See cp-tree.h for more info.
435 If this is all OK, calls build_function_call with the resolved
436 member function.
438 This function must also handle being called to perform
439 initialization, promotion/coercion of arguments, and
440 instantiation of default parameters.
442 Note that NAME may refer to an instance variable name. If
443 `operator()()' is defined for the type of that field, then we return
444 that result. */
446 tree
447 build_method_call (instance, name, parms, basetype_path, flags)
448 tree instance, name, parms, basetype_path;
449 int flags;
451 tree basetype, instance_ptr;
453 #ifdef GATHER_STATISTICS
454 n_build_method_call++;
455 #endif
457 if (instance == error_mark_node
458 || name == error_mark_node
459 || parms == error_mark_node
460 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
461 return error_mark_node;
463 if (processing_template_decl)
465 /* We need to process template parm names here so that tsubst catches
466 them properly. Other type names can wait. */
467 if (TREE_CODE (name) == BIT_NOT_EXPR)
469 tree type = NULL_TREE;
471 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
472 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
473 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
474 type = TREE_TYPE (TREE_OPERAND (name, 0));
476 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
477 name = build_min_nt (BIT_NOT_EXPR, type);
480 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
483 if (TREE_CODE (name) == BIT_NOT_EXPR)
485 if (parms)
486 error ("destructors take no parameters");
487 basetype = TREE_TYPE (instance);
488 if (TREE_CODE (basetype) == REFERENCE_TYPE)
489 basetype = TREE_TYPE (basetype);
491 if (! check_dtor_name (basetype, name))
492 cp_error
493 ("destructor name `~%T' does not match type `%T' of expression",
494 TREE_OPERAND (name, 0), basetype);
496 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
497 return cp_convert (void_type_node, instance);
498 instance = default_conversion (instance);
499 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
500 return build_delete (build_pointer_type (basetype),
501 instance_ptr, integer_two_node,
502 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
505 return build_new_method_call (instance, name, parms, basetype_path, flags);
508 /* New overloading code. */
510 struct z_candidate {
511 tree fn;
512 tree convs;
513 tree second_conv;
514 int viable;
515 tree basetype_path;
516 tree template;
517 tree warnings;
518 struct z_candidate *next;
521 #define IDENTITY_RANK 0
522 #define EXACT_RANK 1
523 #define PROMO_RANK 2
524 #define STD_RANK 3
525 #define PBOOL_RANK 4
526 #define USER_RANK 5
527 #define ELLIPSIS_RANK 6
528 #define BAD_RANK 7
530 #define ICS_RANK(NODE) \
531 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
532 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
533 : ICS_USER_FLAG (NODE) ? USER_RANK \
534 : ICS_STD_RANK (NODE))
536 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
538 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
539 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
540 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
541 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
543 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
544 should be created to hold the result of the conversion. */
545 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
547 #define USER_CONV_CAND(NODE) \
548 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
549 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
552 null_ptr_cst_p (t)
553 tree t;
555 /* [conv.ptr]
557 A null pointer constant is an integral constant expression
558 (_expr.const_) rvalue of integer type that evaluates to zero. */
559 if (t == null_node
560 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
561 return 1;
562 return 0;
565 static tree
566 build_conv (code, type, from)
567 enum tree_code code;
568 tree type, from;
570 tree t = build1 (code, type, from);
571 int rank = ICS_STD_RANK (from);
572 switch (code)
574 case PTR_CONV:
575 case PMEM_CONV:
576 case BASE_CONV:
577 case STD_CONV:
578 if (rank < STD_RANK)
579 rank = STD_RANK;
580 break;
582 case QUAL_CONV:
583 if (rank < EXACT_RANK)
584 rank = EXACT_RANK;
586 default:
587 break;
589 ICS_STD_RANK (t) = rank;
590 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
591 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
592 return t;
595 static tree
596 non_reference (t)
597 tree t;
599 if (TREE_CODE (t) == REFERENCE_TYPE)
600 t = TREE_TYPE (t);
601 return t;
604 tree
605 strip_top_quals (t)
606 tree t;
608 if (TREE_CODE (t) == ARRAY_TYPE)
609 return t;
610 return TYPE_MAIN_VARIANT (t);
613 /* Returns the standard conversion path (see [conv]) from type FROM to type
614 TO, if any. For proper handling of null pointer constants, you must
615 also pass the expression EXPR to convert from. */
617 static tree
618 standard_conversion (to, from, expr)
619 tree to, from, expr;
621 enum tree_code fcode, tcode;
622 tree conv;
623 int fromref = 0;
625 if (TREE_CODE (to) == REFERENCE_TYPE)
626 to = TREE_TYPE (to);
627 if (TREE_CODE (from) == REFERENCE_TYPE)
629 fromref = 1;
630 from = TREE_TYPE (from);
632 to = strip_top_quals (to);
633 from = strip_top_quals (from);
635 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
636 && expr && type_unknown_p (expr))
638 expr = instantiate_type (to, expr, 0);
639 if (expr == error_mark_node)
640 return NULL_TREE;
641 from = TREE_TYPE (expr);
644 fcode = TREE_CODE (from);
645 tcode = TREE_CODE (to);
647 conv = build1 (IDENTITY_CONV, from, expr);
649 if (fcode == FUNCTION_TYPE)
651 from = build_pointer_type (from);
652 fcode = TREE_CODE (from);
653 conv = build_conv (LVALUE_CONV, from, conv);
655 else if (fcode == ARRAY_TYPE)
657 from = build_pointer_type (TREE_TYPE (from));
658 fcode = TREE_CODE (from);
659 conv = build_conv (LVALUE_CONV, from, conv);
661 else if (fromref || (expr && real_lvalue_p (expr)))
662 conv = build_conv (RVALUE_CONV, from, conv);
664 if (same_type_p (from, to))
665 return conv;
667 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
668 && expr && null_ptr_cst_p (expr))
670 conv = build_conv (STD_CONV, to, conv);
672 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
674 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
675 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
677 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
678 TYPE_MAIN_VARIANT (TREE_TYPE (to))))
680 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
681 && ufcode != FUNCTION_TYPE)
683 from = build_pointer_type
684 (cp_build_qualified_type (void_type_node,
685 CP_TYPE_QUALS (TREE_TYPE (from))));
686 conv = build_conv (PTR_CONV, from, conv);
688 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
690 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
691 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
693 if (DERIVED_FROM_P (fbase, tbase)
694 && (same_type_p
695 (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
696 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))))))
698 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
699 from = build_pointer_type (from);
700 conv = build_conv (PMEM_CONV, from, conv);
703 else if (IS_AGGR_TYPE (TREE_TYPE (from))
704 && IS_AGGR_TYPE (TREE_TYPE (to)))
706 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
708 from =
709 cp_build_qualified_type (TREE_TYPE (to),
710 CP_TYPE_QUALS (TREE_TYPE (from)));
711 from = build_pointer_type (from);
712 conv = build_conv (PTR_CONV, from, conv);
716 if (same_type_p (from, to))
717 /* OK */;
718 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
719 conv = build_conv (QUAL_CONV, to, conv);
720 else if (expr && string_conv_p (to, expr, 0))
721 /* converting from string constant to char *. */
722 conv = build_conv (QUAL_CONV, to, conv);
723 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
725 conv = build_conv (PTR_CONV, to, conv);
726 ICS_BAD_FLAG (conv) = 1;
728 else
729 return 0;
731 from = to;
733 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
735 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
736 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
737 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
738 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
740 if (! DERIVED_FROM_P (fbase, tbase)
741 || ! same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
742 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
743 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
744 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
745 return 0;
747 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
748 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
749 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
750 from = build_ptrmemfunc_type (build_pointer_type (from));
751 conv = build_conv (PMEM_CONV, from, conv);
753 else if (tcode == BOOLEAN_TYPE)
755 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
756 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
757 return 0;
759 conv = build_conv (STD_CONV, to, conv);
760 if (fcode == POINTER_TYPE
761 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
762 ICS_STD_RANK (conv) = PBOOL_RANK;
764 /* We don't check for ENUMERAL_TYPE here because there are no standard
765 conversions to enum type. */
766 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
767 || tcode == REAL_TYPE)
769 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
770 return 0;
771 conv = build_conv (STD_CONV, to, conv);
773 /* Give this a better rank if it's a promotion. */
774 if (to == type_promotes_to (from)
775 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
776 ICS_STD_RANK (conv) = PROMO_RANK;
778 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
779 && is_properly_derived_from (from, to))
781 if (TREE_CODE (conv) == RVALUE_CONV)
782 conv = TREE_OPERAND (conv, 0);
783 conv = build_conv (BASE_CONV, to, conv);
784 /* The derived-to-base conversion indicates the initialization
785 of a parameter with base type from an object of a derived
786 type. A temporary object is created to hold the result of
787 the conversion. */
788 NEED_TEMPORARY_P (conv) = 1;
790 else
791 return 0;
793 return conv;
796 /* Returns non-zero if T1 is reference-related to T2. */
798 static int
799 reference_related_p (t1, t2)
800 tree t1;
801 tree t2;
803 t1 = TYPE_MAIN_VARIANT (t1);
804 t2 = TYPE_MAIN_VARIANT (t2);
806 /* [dcl.init.ref]
808 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
809 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
810 of T2. */
811 return (same_type_p (t1, t2)
812 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
813 && DERIVED_FROM_P (t1, t2)));
816 /* Returns non-zero if T1 is reference-compatible with T2. */
818 static int
819 reference_compatible_p (t1, t2)
820 tree t1;
821 tree t2;
823 /* [dcl.init.ref]
825 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
826 reference-related to T2 and cv1 is the same cv-qualification as,
827 or greater cv-qualification than, cv2. */
828 return (reference_related_p (t1, t2)
829 && at_least_as_qualified_p (t1, t2));
832 /* Determine whether or not the EXPR (of class type S) can be
833 converted to T as in [over.match.ref]. */
835 static tree
836 convert_class_to_reference (t, s, expr)
837 tree t;
838 tree s;
839 tree expr;
841 tree conversions;
842 tree arglist;
843 tree conv;
844 struct z_candidate *candidates;
845 struct z_candidate *cand;
847 /* [over.match.ref]
849 Assuming that "cv1 T" is the underlying type of the reference
850 being initialized, and "cv S" is the type of the initializer
851 expression, with S a class type, the candidate functions are
852 selected as follows:
854 --The conversion functions of S and its base classes are
855 considered. Those that are not hidden within S and yield type
856 "reference to cv2 T2", where "cv1 T" is reference-compatible
857 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
859 The argument list has one argument, which is the initializer
860 expression. */
862 candidates = 0;
864 /* Conceptually, we should take the address of EXPR and put it in
865 the argument list. Unfortunately, however, that can result in
866 error messages, which we should not issue now because we are just
867 trying to find a conversion operator. Therefore, we use NULL,
868 cast to the appropriate type. */
869 arglist = build_int_2 (0, 0);
870 TREE_TYPE (arglist) = build_pointer_type (s);
871 arglist = build_scratch_list (NULL_TREE, arglist);
873 for (conversions = lookup_conversions (s);
874 conversions;
875 conversions = TREE_CHAIN (conversions))
877 tree fns = TREE_VALUE (conversions);
879 while (fns)
881 tree f = OVL_CURRENT (fns);
882 tree t2 = TREE_TYPE (TREE_TYPE (f));
883 struct z_candidate *old_candidates = candidates;
885 /* If this is a template function, try to get an exact
886 match. */
887 if (TREE_CODE (f) == TEMPLATE_DECL)
889 candidates
890 = add_template_candidate (candidates,
892 NULL_TREE,
893 arglist,
894 build_reference_type (t),
895 LOOKUP_NORMAL,
896 DEDUCE_CONV);
898 if (candidates != old_candidates)
900 /* Now, see if the conversion function really returns
901 an lvalue of the appropriate type. From the
902 point of view of unification, simply returning an
903 rvalue of the right type is good enough. */
904 f = candidates->fn;
905 t2 = TREE_TYPE (TREE_TYPE (f));
906 if (TREE_CODE (t2) != REFERENCE_TYPE
907 || !reference_compatible_p (t, TREE_TYPE (t2)))
908 candidates = candidates->next;
911 else if (TREE_CODE (t2) == REFERENCE_TYPE
912 && reference_compatible_p (t, TREE_TYPE (t2)))
913 candidates
914 = add_function_candidate (candidates, f, arglist,
915 LOOKUP_NORMAL);
917 if (candidates != old_candidates)
918 candidates->basetype_path = TREE_PURPOSE (conversions);
920 fns = OVL_NEXT (fns);
924 /* If none of the conversion functions worked out, let our caller
925 know. */
926 if (!any_viable (candidates))
927 return NULL_TREE;
929 candidates = splice_viable (candidates);
930 cand = tourney (candidates);
931 if (!cand)
932 return NULL_TREE;
934 conv = build_conv (IDENTITY_CONV, s, expr);
935 conv = build_conv (USER_CONV,
936 non_reference (TREE_TYPE (TREE_TYPE (cand->fn))),
937 expr);
938 TREE_OPERAND (conv, 1) = build_expr_ptr_wrapper (cand);
939 ICS_USER_FLAG (conv) = 1;
940 if (cand->viable == -1)
941 ICS_BAD_FLAG (conv) = 1;
942 cand->second_conv = conv;
944 return conv;
947 /* A reference of the indicated TYPE is being bound directly to the
948 expression represented by the implicit conversion sequence CONV.
949 Return a conversion sequence for this binding. */
951 static tree
952 direct_reference_binding (type, conv)
953 tree type;
954 tree conv;
956 tree t = TREE_TYPE (type);
958 /* [over.ics.rank]
960 When a parameter of reference type binds directly
961 (_dcl.init.ref_) to an argument expression, the implicit
962 conversion sequence is the identity conversion, unless the
963 argument expression has a type that is a derived class of the
964 parameter type, in which case the implicit conversion sequence is
965 a derived-to-base Conversion.
967 If the parameter binds directly to the result of applying a
968 conversion function to the argument expression, the implicit
969 conversion sequence is a user-defined conversion sequence
970 (_over.ics.user_), with the second standard conversion sequence
971 either an identity conversion or, if the conversion function
972 returns an entity of a type that is a derived class of the
973 parameter type, a derived-to-base conversion. */
974 if (!same_type_p (TYPE_MAIN_VARIANT (t),
975 TYPE_MAIN_VARIANT (TREE_TYPE (conv))))
977 /* Represent the derived-to-base conversion. */
978 conv = build_conv (BASE_CONV, t, conv);
979 /* We will actually be binding to the base-class subobject in
980 the derived class, so we mark this conversion appropriately.
981 That way, convert_like knows not to generate a temporary. */
982 NEED_TEMPORARY_P (conv) = 0;
984 return build_conv (REF_BIND, type, conv);
987 /* Returns the conversion path from type FROM to reference type TO for
988 purposes of reference binding. For lvalue binding, either pass a
989 reference type to FROM or an lvalue expression to EXPR. If the
990 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
991 the conversion returned. */
993 static tree
994 reference_binding (rto, rfrom, expr, flags)
995 tree rto, rfrom, expr;
996 int flags;
998 tree conv = NULL_TREE;
999 tree to = TREE_TYPE (rto);
1000 tree from = rfrom;
1001 int related_p;
1002 int compatible_p;
1003 cp_lvalue_kind lvalue_p = clk_none;
1005 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1007 expr = instantiate_type (to, expr, 0);
1008 if (expr == error_mark_node)
1009 return NULL_TREE;
1010 from = TREE_TYPE (expr);
1013 if (TREE_CODE (from) == REFERENCE_TYPE)
1015 /* Anything with reference type is an lvalue. */
1016 lvalue_p = clk_ordinary;
1017 from = TREE_TYPE (from);
1019 else if (expr)
1020 lvalue_p = real_lvalue_p (expr);
1022 /* Figure out whether or not the types are reference-related and
1023 reference compatible. We have do do this after stripping
1024 references from FROM. */
1025 related_p = reference_related_p (to, from);
1026 compatible_p = reference_compatible_p (to, from);
1028 if (lvalue_p && compatible_p)
1030 /* [dcl.init.ref]
1032 If the intializer expression
1034 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1035 is reference-compatible with "cv2 T2,"
1037 the reference is bound directly to the initializer exprssion
1038 lvalue. */
1039 conv = build1 (IDENTITY_CONV, from, expr);
1040 conv = direct_reference_binding (rto, conv);
1041 if ((lvalue_p & clk_bitfield) != 0
1042 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1043 /* For the purposes of overload resolution, we ignore the fact
1044 this expression is a bitfield. (In particular,
1045 [over.ics.ref] says specifically that a function with a
1046 non-const reference parameter is viable even if the
1047 argument is a bitfield.)
1049 However, when we actually call the function we must create
1050 a temporary to which to bind the reference. If the
1051 reference is volatile, or isn't const, then we cannot make
1052 a temporary, so we just issue an error when the conversion
1053 actually occurs. */
1054 NEED_TEMPORARY_P (conv) = 1;
1055 return conv;
1057 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1059 /* [dcl.init.ref]
1061 If the initializer exprsesion
1063 -- has a class type (i.e., T2 is a class type) can be
1064 implicitly converted to an lvalue of type "cv3 T3," where
1065 "cv1 T1" is reference-compatible with "cv3 T3". (this
1066 conversion is selected by enumerating the applicable
1067 conversion functions (_over.match.ref_) and choosing the
1068 best one through overload resolution. (_over.match_).
1070 the reference is bound to the lvalue result of the conversion
1071 in the second case. */
1072 conv = convert_class_to_reference (to, from, expr);
1073 if (conv)
1074 return direct_reference_binding (rto, conv);
1077 /* From this point on, we conceptually need temporaries, even if we
1078 elide them. Only the cases above are "direct bindings". */
1079 if (flags & LOOKUP_NO_TEMP_BIND)
1080 return NULL_TREE;
1082 /* [over.ics.rank]
1084 When a parameter of reference type is not bound directly to an
1085 argument expression, the conversion sequence is the one required
1086 to convert the argument expression to the underlying type of the
1087 reference according to _over.best.ics_. Conceptually, this
1088 conversion sequence corresponds to copy-initializing a temporary
1089 of the underlying type with the argument expression. Any
1090 difference in top-level cv-qualification is subsumed by the
1091 initialization itself and does not constitute a conversion. */
1093 /* [dcl.init.ref]
1095 Otherwise, the reference shall be to a non-volatile const type. */
1096 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1097 return NULL_TREE;
1099 /* [dcl.init.ref]
1101 If the initializer expression is an rvalue, with T2 a class type,
1102 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1103 is bound in one of the following ways:
1105 -- The reference is bound to the object represented by the rvalue
1106 or to a sub-object within that object.
1108 In this case, the implicit conversion sequence is supposed to be
1109 same as we would obtain by generating a temporary. Fortunately,
1110 if the types are reference compatible, then this is either an
1111 identity conversion or the derived-to-base conversion, just as
1112 for direct binding. */
1113 if (CLASS_TYPE_P (from) && compatible_p)
1115 conv = build1 (IDENTITY_CONV, from, expr);
1116 return direct_reference_binding (rto, conv);
1119 /* [dcl.init.ref]
1121 Otherwise, a temporary of type "cv1 T1" is created and
1122 initialized from the initializer expression using the rules for a
1123 non-reference copy initialization. If T1 is reference-related to
1124 T2, cv1 must be the same cv-qualification as, or greater
1125 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1126 if (related_p && !at_least_as_qualified_p (to, from))
1127 return NULL_TREE;
1129 conv = implicit_conversion (to, from, expr, flags);
1130 if (!conv)
1131 return NULL_TREE;
1133 conv = build_conv (REF_BIND, rto, conv);
1134 /* This reference binding, unlike those above, requires the
1135 creation of a temporary. */
1136 NEED_TEMPORARY_P (conv) = 1;
1138 return conv;
1141 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1142 to type TO. The optional expression EXPR may affect the conversion.
1143 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1144 significant. */
1146 static tree
1147 implicit_conversion (to, from, expr, flags)
1148 tree to, from, expr;
1149 int flags;
1151 tree conv;
1152 struct z_candidate *cand;
1154 /* Resolve expressions like `A::p' that we thought might become
1155 pointers-to-members. */
1156 if (expr && TREE_CODE (expr) == OFFSET_REF)
1158 expr = resolve_offset_ref (expr);
1159 from = TREE_TYPE (expr);
1162 if (from == error_mark_node || to == error_mark_node
1163 || expr == error_mark_node)
1164 return NULL_TREE;
1166 /* Make sure both the FROM and TO types are complete so that
1167 user-defined conversions are available. */
1168 complete_type (from);
1169 complete_type (to);
1171 if (TREE_CODE (to) == REFERENCE_TYPE)
1172 conv = reference_binding (to, from, expr, flags);
1173 else
1174 conv = standard_conversion (to, from, expr);
1176 if (conv)
1178 else if (expr != NULL_TREE
1179 && (IS_AGGR_TYPE (non_reference (from))
1180 || IS_AGGR_TYPE (non_reference (to)))
1181 && (flags & LOOKUP_NO_CONVERSION) == 0)
1183 cand = build_user_type_conversion_1
1184 (to, expr, LOOKUP_ONLYCONVERTING);
1185 if (cand)
1186 conv = cand->second_conv;
1187 if ((! conv || ICS_BAD_FLAG (conv))
1188 && TREE_CODE (to) == REFERENCE_TYPE
1189 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1191 cand = build_user_type_conversion_1
1192 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1193 if (cand)
1195 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to)))
1196 ICS_BAD_FLAG (cand->second_conv) = 1;
1197 if (!conv || (ICS_BAD_FLAG (conv)
1198 > ICS_BAD_FLAG (cand->second_conv)))
1199 conv = build_conv (REF_BIND, to, cand->second_conv);
1204 return conv;
1207 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1208 functions. */
1210 static struct z_candidate *
1211 add_candidate (candidates, fn, convs, viable)
1212 struct z_candidate *candidates;
1213 tree fn, convs;
1214 int viable;
1216 struct z_candidate *cand
1217 = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
1219 cand->fn = fn;
1220 cand->convs = convs;
1221 cand->second_conv = NULL_TREE;
1222 cand->viable = viable;
1223 cand->basetype_path = NULL_TREE;
1224 cand->template = NULL_TREE;
1225 cand->warnings = NULL_TREE;
1226 cand->next = candidates;
1228 return cand;
1231 /* Create an overload candidate for the function or method FN called with
1232 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1233 to implicit_conversion. */
1235 static struct z_candidate *
1236 add_function_candidate (candidates, fn, arglist, flags)
1237 struct z_candidate *candidates;
1238 tree fn, arglist;
1239 int flags;
1241 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1242 int i, len;
1243 tree convs;
1244 tree parmnode, argnode;
1245 int viable = 1;
1247 /* The `this' and `in_chrg' arguments to constructors are not considered
1248 in overload resolution. */
1249 if (DECL_CONSTRUCTOR_P (fn))
1251 parmlist = TREE_CHAIN (parmlist);
1252 arglist = TREE_CHAIN (arglist);
1253 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1255 parmlist = TREE_CHAIN (parmlist);
1256 arglist = TREE_CHAIN (arglist);
1260 len = list_length (arglist);
1261 convs = make_scratch_vec (len);
1263 /* 13.3.2 - Viable functions [over.match.viable]
1264 First, to be a viable function, a candidate function shall have enough
1265 parameters to agree in number with the arguments in the list.
1267 We need to check this first; otherwise, checking the ICSes might cause
1268 us to produce an ill-formed template instantiation. */
1270 parmnode = parmlist;
1271 for (i = 0; i < len; ++i)
1273 if (parmnode == NULL_TREE || parmnode == void_list_node)
1274 break;
1275 parmnode = TREE_CHAIN (parmnode);
1278 if (i < len && parmnode)
1279 viable = 0;
1281 /* Make sure there are default args for the rest of the parms. */
1282 else for (; parmnode && parmnode != void_list_node;
1283 parmnode = TREE_CHAIN (parmnode))
1284 if (! TREE_PURPOSE (parmnode))
1286 viable = 0;
1287 break;
1290 if (! viable)
1291 goto out;
1293 /* Second, for F to be a viable function, there shall exist for each
1294 argument an implicit conversion sequence that converts that argument
1295 to the corresponding parameter of F. */
1297 parmnode = parmlist;
1298 argnode = arglist;
1300 for (i = 0; i < len; ++i)
1302 tree arg = TREE_VALUE (argnode);
1303 tree argtype = lvalue_type (arg);
1304 tree t;
1306 if (parmnode == void_list_node)
1307 break;
1309 if (parmnode)
1311 tree parmtype = TREE_VALUE (parmnode);
1313 /* [over.match.funcs] For conversion functions, the function is
1314 considered to be a member of the class of the implicit object
1315 argument for the purpose of defining the type of the implicit
1316 object parameter.
1318 Since build_over_call ignores the ICS for the `this' parameter,
1319 we can just change the parm type. */
1320 if (DECL_CONV_FN_P (fn) && i == 0)
1322 parmtype
1323 = build_qualified_type (TREE_TYPE (argtype),
1324 TYPE_QUALS (TREE_TYPE (parmtype)));
1325 parmtype = build_pointer_type (parmtype);
1328 t = implicit_conversion (parmtype, argtype, arg, flags);
1330 else
1332 t = build1 (IDENTITY_CONV, argtype, arg);
1333 ICS_ELLIPSIS_FLAG (t) = 1;
1336 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1337 && ! DECL_CONSTRUCTOR_P (fn))
1338 ICS_THIS_FLAG (t) = 1;
1340 TREE_VEC_ELT (convs, i) = t;
1341 if (! t)
1343 viable = 0;
1344 break;
1347 if (ICS_BAD_FLAG (t))
1348 viable = -1;
1350 if (parmnode)
1351 parmnode = TREE_CHAIN (parmnode);
1352 argnode = TREE_CHAIN (argnode);
1355 out:
1356 return add_candidate (candidates, fn, convs, viable);
1359 /* Create an overload candidate for the conversion function FN which will
1360 be invoked for expression OBJ, producing a pointer-to-function which
1361 will in turn be called with the argument list ARGLIST, and add it to
1362 CANDIDATES. FLAGS is passed on to implicit_conversion.
1364 Actually, we don't really care about FN; we care about the type it
1365 converts to. There may be multiple conversion functions that will
1366 convert to that type, and we rely on build_user_type_conversion_1 to
1367 choose the best one; so when we create our candidate, we record the type
1368 instead of the function. */
1370 static struct z_candidate *
1371 add_conv_candidate (candidates, fn, obj, arglist)
1372 struct z_candidate *candidates;
1373 tree fn, obj, arglist;
1375 tree totype = TREE_TYPE (TREE_TYPE (fn));
1376 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1377 int i, len = list_length (arglist) + 1;
1378 tree convs = make_scratch_vec (len);
1379 tree parmnode = parmlist;
1380 tree argnode = arglist;
1381 int viable = 1;
1382 int flags = LOOKUP_NORMAL;
1384 /* Don't bother looking up the same type twice. */
1385 if (candidates && candidates->fn == totype)
1386 return candidates;
1388 for (i = 0; i < len; ++i)
1390 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1391 tree argtype = lvalue_type (arg);
1392 tree t;
1394 if (i == 0)
1395 t = implicit_conversion (totype, argtype, arg, flags);
1396 else if (parmnode == void_list_node)
1397 break;
1398 else if (parmnode)
1399 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1400 else
1402 t = build1 (IDENTITY_CONV, argtype, arg);
1403 ICS_ELLIPSIS_FLAG (t) = 1;
1406 TREE_VEC_ELT (convs, i) = t;
1407 if (! t)
1408 break;
1410 if (ICS_BAD_FLAG (t))
1411 viable = -1;
1413 if (i == 0)
1414 continue;
1416 if (parmnode)
1417 parmnode = TREE_CHAIN (parmnode);
1418 argnode = TREE_CHAIN (argnode);
1421 if (i < len)
1422 viable = 0;
1424 for (; parmnode && parmnode != void_list_node;
1425 parmnode = TREE_CHAIN (parmnode))
1426 if (! TREE_PURPOSE (parmnode))
1428 viable = 0;
1429 break;
1432 return add_candidate (candidates, totype, convs, viable);
1435 static struct z_candidate *
1436 build_builtin_candidate (candidates, fnname, type1, type2,
1437 args, argtypes, flags)
1438 struct z_candidate *candidates;
1439 tree fnname, type1, type2, *args, *argtypes;
1440 int flags;
1443 tree t, convs;
1444 int viable = 1, i;
1445 tree types[2];
1447 types[0] = type1;
1448 types[1] = type2;
1450 convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1452 for (i = 0; i < 2; ++i)
1454 if (! args[i])
1455 break;
1457 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1458 if (! t)
1460 viable = 0;
1461 /* We need something for printing the candidate. */
1462 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1464 else if (ICS_BAD_FLAG (t))
1465 viable = 0;
1466 TREE_VEC_ELT (convs, i) = t;
1469 /* For COND_EXPR we rearranged the arguments; undo that now. */
1470 if (args[2])
1472 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1473 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1474 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1475 if (t)
1476 TREE_VEC_ELT (convs, 0) = t;
1477 else
1478 viable = 0;
1481 return add_candidate (candidates, fnname, convs, viable);
1484 static int
1485 is_complete (t)
1486 tree t;
1488 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1491 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1493 static int
1494 promoted_arithmetic_type_p (type)
1495 tree type;
1497 /* [over.built]
1499 In this section, the term promoted integral type is used to refer
1500 to those integral types which are preserved by integral promotion
1501 (including e.g. int and long but excluding e.g. char).
1502 Similarly, the term promoted arithmetic type refers to promoted
1503 integral types plus floating types. */
1504 return ((INTEGRAL_TYPE_P (type)
1505 && same_type_p (type_promotes_to (type), type))
1506 || TREE_CODE (type) == REAL_TYPE);
1509 /* Create any builtin operator overload candidates for the operator in
1510 question given the converted operand types TYPE1 and TYPE2. The other
1511 args are passed through from add_builtin_candidates to
1512 build_builtin_candidate. */
1514 static struct z_candidate *
1515 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1516 args, argtypes, flags)
1517 struct z_candidate *candidates;
1518 enum tree_code code, code2;
1519 tree fnname, type1, type2, *args, *argtypes;
1520 int flags;
1522 switch (code)
1524 case POSTINCREMENT_EXPR:
1525 case POSTDECREMENT_EXPR:
1526 args[1] = integer_zero_node;
1527 type2 = integer_type_node;
1528 break;
1529 default:
1530 break;
1533 switch (code)
1536 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1537 and VQ is either volatile or empty, there exist candidate operator
1538 functions of the form
1539 VQ T& operator++(VQ T&);
1540 T operator++(VQ T&, int);
1541 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1542 type other than bool, and VQ is either volatile or empty, there exist
1543 candidate operator functions of the form
1544 VQ T& operator--(VQ T&);
1545 T operator--(VQ T&, int);
1546 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1547 complete object type, and VQ is either volatile or empty, there exist
1548 candidate operator functions of the form
1549 T*VQ& operator++(T*VQ&);
1550 T*VQ& operator--(T*VQ&);
1551 T* operator++(T*VQ&, int);
1552 T* operator--(T*VQ&, int); */
1554 case POSTDECREMENT_EXPR:
1555 case PREDECREMENT_EXPR:
1556 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1557 return candidates;
1558 case POSTINCREMENT_EXPR:
1559 case PREINCREMENT_EXPR:
1560 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1561 || TYPE_PTROB_P (type1))
1563 type1 = build_reference_type (type1);
1564 break;
1566 return candidates;
1568 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1569 exist candidate operator functions of the form
1571 T& operator*(T*);
1573 8 For every function type T, there exist candidate operator functions of
1574 the form
1575 T& operator*(T*); */
1577 case INDIRECT_REF:
1578 if (TREE_CODE (type1) == POINTER_TYPE
1579 && (TYPE_PTROB_P (type1)
1580 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1581 break;
1582 return candidates;
1584 /* 9 For every type T, there exist candidate operator functions of the form
1585 T* operator+(T*);
1587 10For every promoted arithmetic type T, there exist candidate operator
1588 functions of the form
1589 T operator+(T);
1590 T operator-(T); */
1592 case CONVERT_EXPR: /* unary + */
1593 if (TREE_CODE (type1) == POINTER_TYPE
1594 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1595 break;
1596 case NEGATE_EXPR:
1597 if (ARITHMETIC_TYPE_P (type1))
1598 break;
1599 return candidates;
1601 /* 11For every promoted integral type T, there exist candidate operator
1602 functions of the form
1603 T operator~(T); */
1605 case BIT_NOT_EXPR:
1606 if (INTEGRAL_TYPE_P (type1))
1607 break;
1608 return candidates;
1610 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1611 is the same type as C2 or is a derived class of C2, T is a complete
1612 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1613 there exist candidate operator functions of the form
1614 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1615 where CV12 is the union of CV1 and CV2. */
1617 case MEMBER_REF:
1618 if (TREE_CODE (type1) == POINTER_TYPE
1619 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1621 tree c1 = TREE_TYPE (type1);
1622 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1623 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1624 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1626 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1627 && (TYPE_PTRMEMFUNC_P (type2)
1628 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1629 break;
1631 return candidates;
1633 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1634 didate operator functions of the form
1635 LR operator*(L, R);
1636 LR operator/(L, R);
1637 LR operator+(L, R);
1638 LR operator-(L, R);
1639 bool operator<(L, R);
1640 bool operator>(L, R);
1641 bool operator<=(L, R);
1642 bool operator>=(L, R);
1643 bool operator==(L, R);
1644 bool operator!=(L, R);
1645 where LR is the result of the usual arithmetic conversions between
1646 types L and R.
1648 14For every pair of types T and I, where T is a cv-qualified or cv-
1649 unqualified complete object type and I is a promoted integral type,
1650 there exist candidate operator functions of the form
1651 T* operator+(T*, I);
1652 T& operator[](T*, I);
1653 T* operator-(T*, I);
1654 T* operator+(I, T*);
1655 T& operator[](I, T*);
1657 15For every T, where T is a pointer to complete object type, there exist
1658 candidate operator functions of the form112)
1659 ptrdiff_t operator-(T, T);
1661 16For every pointer type T, there exist candidate operator functions of
1662 the form
1663 bool operator<(T, T);
1664 bool operator>(T, T);
1665 bool operator<=(T, T);
1666 bool operator>=(T, T);
1667 bool operator==(T, T);
1668 bool operator!=(T, T);
1670 17For every pointer to member type T, there exist candidate operator
1671 functions of the form
1672 bool operator==(T, T);
1673 bool operator!=(T, T); */
1675 case MINUS_EXPR:
1676 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1677 break;
1678 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1680 type2 = ptrdiff_type_node;
1681 break;
1683 case MULT_EXPR:
1684 case TRUNC_DIV_EXPR:
1685 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1686 break;
1687 return candidates;
1689 case EQ_EXPR:
1690 case NE_EXPR:
1691 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1692 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1693 break;
1694 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1695 && null_ptr_cst_p (args[1]))
1697 type2 = type1;
1698 break;
1700 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1701 && null_ptr_cst_p (args[0]))
1703 type1 = type2;
1704 break;
1706 case LT_EXPR:
1707 case GT_EXPR:
1708 case LE_EXPR:
1709 case GE_EXPR:
1710 case MAX_EXPR:
1711 case MIN_EXPR:
1712 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1713 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1714 break;
1715 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1717 type2 = type1;
1718 break;
1720 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1722 type1 = type2;
1723 break;
1725 return candidates;
1727 case PLUS_EXPR:
1728 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1729 break;
1730 case ARRAY_REF:
1731 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1733 type1 = ptrdiff_type_node;
1734 break;
1736 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1738 type2 = ptrdiff_type_node;
1739 break;
1741 return candidates;
1743 /* 18For every pair of promoted integral types L and R, there exist candi-
1744 date operator functions of the form
1745 LR operator%(L, R);
1746 LR operator&(L, R);
1747 LR operator^(L, R);
1748 LR operator|(L, R);
1749 L operator<<(L, R);
1750 L operator>>(L, R);
1751 where LR is the result of the usual arithmetic conversions between
1752 types L and R. */
1754 case TRUNC_MOD_EXPR:
1755 case BIT_AND_EXPR:
1756 case BIT_IOR_EXPR:
1757 case BIT_XOR_EXPR:
1758 case LSHIFT_EXPR:
1759 case RSHIFT_EXPR:
1760 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1761 break;
1762 return candidates;
1764 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1765 type, VQ is either volatile or empty, and R is a promoted arithmetic
1766 type, there exist candidate operator functions of the form
1767 VQ L& operator=(VQ L&, R);
1768 VQ L& operator*=(VQ L&, R);
1769 VQ L& operator/=(VQ L&, R);
1770 VQ L& operator+=(VQ L&, R);
1771 VQ L& operator-=(VQ L&, R);
1773 20For every pair T, VQ), where T is any type and VQ is either volatile
1774 or empty, there exist candidate operator functions of the form
1775 T*VQ& operator=(T*VQ&, T*);
1777 21For every pair T, VQ), where T is a pointer to member type and VQ is
1778 either volatile or empty, there exist candidate operator functions of
1779 the form
1780 VQ T& operator=(VQ T&, T);
1782 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1783 unqualified complete object type, VQ is either volatile or empty, and
1784 I is a promoted integral type, there exist candidate operator func-
1785 tions of the form
1786 T*VQ& operator+=(T*VQ&, I);
1787 T*VQ& operator-=(T*VQ&, I);
1789 23For every triple L, VQ, R), where L is an integral or enumeration
1790 type, VQ is either volatile or empty, and R is a promoted integral
1791 type, there exist candidate operator functions of the form
1793 VQ L& operator%=(VQ L&, R);
1794 VQ L& operator<<=(VQ L&, R);
1795 VQ L& operator>>=(VQ L&, R);
1796 VQ L& operator&=(VQ L&, R);
1797 VQ L& operator^=(VQ L&, R);
1798 VQ L& operator|=(VQ L&, R); */
1800 case MODIFY_EXPR:
1801 switch (code2)
1803 case PLUS_EXPR:
1804 case MINUS_EXPR:
1805 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1807 type2 = ptrdiff_type_node;
1808 break;
1810 case MULT_EXPR:
1811 case TRUNC_DIV_EXPR:
1812 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1813 break;
1814 return candidates;
1816 case TRUNC_MOD_EXPR:
1817 case BIT_AND_EXPR:
1818 case BIT_IOR_EXPR:
1819 case BIT_XOR_EXPR:
1820 case LSHIFT_EXPR:
1821 case RSHIFT_EXPR:
1822 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1823 break;
1824 return candidates;
1826 case NOP_EXPR:
1827 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1828 break;
1829 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1830 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1831 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1832 || ((TYPE_PTRMEMFUNC_P (type1)
1833 || TREE_CODE (type1) == POINTER_TYPE)
1834 && null_ptr_cst_p (args[1])))
1836 type2 = type1;
1837 break;
1839 return candidates;
1841 default:
1842 my_friendly_abort (367);
1844 type1 = build_reference_type (type1);
1845 break;
1847 case COND_EXPR:
1848 /* [over.builtin]
1850 For every pair of promoted arithmetic types L and R, there
1851 exist candidate operator functions of the form
1853 LR operator?(bool, L, R);
1855 where LR is the result of the usual arithmetic conversions
1856 between types L and R.
1858 For every type T, where T is a pointer or pointer-to-member
1859 type, there exist candidate operator functions of the form T
1860 operator?(bool, T, T); */
1862 if (promoted_arithmetic_type_p (type1)
1863 && promoted_arithmetic_type_p (type2))
1864 /* That's OK. */
1865 break;
1867 /* Otherwise, the types should be pointers. */
1868 if (!(TREE_CODE (type1) == POINTER_TYPE
1869 || TYPE_PTRMEM_P (type1)
1870 || TYPE_PTRMEMFUNC_P (type1))
1871 || !(TREE_CODE (type2) == POINTER_TYPE
1872 || TYPE_PTRMEM_P (type2)
1873 || TYPE_PTRMEMFUNC_P (type2)))
1874 return candidates;
1876 /* We don't check that the two types are the same; the logic
1877 below will actually create two candidates; one in which both
1878 parameter types are TYPE1, and one in which both parameter
1879 types are TYPE2. */
1880 break;
1882 /* These arguments do not make for a legal overloaded operator. */
1883 return candidates;
1885 default:
1886 my_friendly_abort (367);
1889 /* If we're dealing with two pointer types, we need candidates
1890 for both of them. */
1891 if (type2 && !same_type_p (type1, type2)
1892 && TREE_CODE (type1) == TREE_CODE (type2)
1893 && (TREE_CODE (type1) == REFERENCE_TYPE
1894 || (TREE_CODE (type1) == POINTER_TYPE
1895 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1896 || TYPE_PTRMEMFUNC_P (type1)
1897 || IS_AGGR_TYPE (type1)))
1899 candidates = build_builtin_candidate
1900 (candidates, fnname, type1, type1, args, argtypes, flags);
1901 return build_builtin_candidate
1902 (candidates, fnname, type2, type2, args, argtypes, flags);
1905 return build_builtin_candidate
1906 (candidates, fnname, type1, type2, args, argtypes, flags);
1909 tree
1910 type_decays_to (type)
1911 tree type;
1913 if (TREE_CODE (type) == ARRAY_TYPE)
1914 return build_pointer_type (TREE_TYPE (type));
1915 if (TREE_CODE (type) == FUNCTION_TYPE)
1916 return build_pointer_type (type);
1917 return type;
1920 /* There are three conditions of builtin candidates:
1922 1) bool-taking candidates. These are the same regardless of the input.
1923 2) pointer-pair taking candidates. These are generated for each type
1924 one of the input types converts to.
1925 3) arithmetic candidates. According to the WP, we should generate
1926 all of these, but I'm trying not to... */
1928 static struct z_candidate *
1929 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1930 struct z_candidate *candidates;
1931 enum tree_code code, code2;
1932 tree fnname, *args;
1933 int flags;
1935 int ref1, i;
1936 tree type, argtypes[3];
1937 /* TYPES[i] is the set of possible builtin-operator parameter types
1938 we will consider for the Ith argument. These are represented as
1939 a TREE_LIST; the TREE_VALUE of each node is the potential
1940 parameter type. */
1941 tree types[2];
1943 for (i = 0; i < 3; ++i)
1945 if (args[i])
1946 argtypes[i] = lvalue_type (args[i]);
1947 else
1948 argtypes[i] = NULL_TREE;
1951 switch (code)
1953 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1954 and VQ is either volatile or empty, there exist candidate operator
1955 functions of the form
1956 VQ T& operator++(VQ T&); */
1958 case POSTINCREMENT_EXPR:
1959 case PREINCREMENT_EXPR:
1960 case POSTDECREMENT_EXPR:
1961 case PREDECREMENT_EXPR:
1962 case MODIFY_EXPR:
1963 ref1 = 1;
1964 break;
1966 /* 24There also exist candidate operator functions of the form
1967 bool operator!(bool);
1968 bool operator&&(bool, bool);
1969 bool operator||(bool, bool); */
1971 case TRUTH_NOT_EXPR:
1972 return build_builtin_candidate
1973 (candidates, fnname, boolean_type_node,
1974 NULL_TREE, args, argtypes, flags);
1976 case TRUTH_ORIF_EXPR:
1977 case TRUTH_ANDIF_EXPR:
1978 return build_builtin_candidate
1979 (candidates, fnname, boolean_type_node,
1980 boolean_type_node, args, argtypes, flags);
1982 case ADDR_EXPR:
1983 case COMPOUND_EXPR:
1984 case COMPONENT_REF:
1985 return candidates;
1987 default:
1988 ref1 = 0;
1991 types[0] = types[1] = NULL_TREE;
1993 for (i = 0; i < 2; ++i)
1995 if (! args[i])
1997 else if (IS_AGGR_TYPE (argtypes[i]))
1999 tree convs;
2001 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2002 return candidates;
2004 convs = lookup_conversions (argtypes[i]);
2006 if (code == COND_EXPR)
2008 if (real_lvalue_p (args[i]))
2009 types[i] = tree_cons
2010 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2012 types[i] = tree_cons
2013 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2016 else if (! convs)
2017 return candidates;
2019 for (; convs; convs = TREE_CHAIN (convs))
2021 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2023 if (i == 0 && ref1
2024 && (TREE_CODE (type) != REFERENCE_TYPE
2025 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2026 continue;
2028 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2029 types[i] = tree_cons (NULL_TREE, type, types[i]);
2031 type = non_reference (type);
2032 if (i != 0 || ! ref1)
2034 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2035 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2036 types[i] = tree_cons (NULL_TREE, type, types[i]);
2037 if (INTEGRAL_TYPE_P (type))
2038 type = type_promotes_to (type);
2041 if (! value_member (type, types[i]))
2042 types[i] = tree_cons (NULL_TREE, type, types[i]);
2045 else
2047 if (code == COND_EXPR && real_lvalue_p (args[i]))
2048 types[i] = tree_cons
2049 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2050 type = non_reference (argtypes[i]);
2051 if (i != 0 || ! ref1)
2053 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2054 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2055 types[i] = tree_cons (NULL_TREE, type, types[i]);
2056 if (INTEGRAL_TYPE_P (type))
2057 type = type_promotes_to (type);
2059 types[i] = tree_cons (NULL_TREE, type, types[i]);
2063 /* Run through the possible parameter types of both arguments,
2064 creating candidates with those parameter types. */
2065 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2067 if (types[1])
2068 for (type = types[1]; type; type = TREE_CHAIN (type))
2069 candidates = add_builtin_candidate
2070 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2071 TREE_VALUE (type), args, argtypes, flags);
2072 else
2073 candidates = add_builtin_candidate
2074 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2075 NULL_TREE, args, argtypes, flags);
2078 return candidates;
2082 /* If TMPL can be successfully instantiated as indicated by
2083 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2085 TMPL is the template. EXPLICIT_TARGS are any explicit template
2086 arguments. ARGLIST is the arguments provided at the call-site.
2087 The RETURN_TYPE is the desired type for conversion operators. If
2088 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
2089 OBJ is supplied, FLAGS are ignored, and OBJ is as for
2090 add_conv_candidate. */
2092 static struct z_candidate*
2093 add_template_candidate_real (candidates, tmpl, explicit_targs,
2094 arglist, return_type, flags,
2095 obj, strict)
2096 struct z_candidate *candidates;
2097 tree tmpl, explicit_targs, arglist, return_type;
2098 int flags;
2099 tree obj;
2100 unification_kind_t strict;
2102 int ntparms = DECL_NTPARMS (tmpl);
2103 tree targs = make_scratch_vec (ntparms);
2104 struct z_candidate *cand;
2105 int i;
2106 tree fn;
2108 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
2109 return_type, strict);
2111 if (i != 0)
2112 return candidates;
2114 fn = instantiate_template (tmpl, targs);
2115 if (fn == error_mark_node)
2116 return candidates;
2118 if (obj != NULL_TREE)
2119 /* Aha, this is a conversion function. */
2120 cand = add_conv_candidate (candidates, fn, obj, arglist);
2121 else
2122 cand = add_function_candidate (candidates, fn, arglist, flags);
2123 if (DECL_TI_TEMPLATE (fn) != tmpl)
2124 /* This situation can occur if a member template of a template
2125 class is specialized. Then, instantiate_template might return
2126 an instantiation of the specialization, in which case the
2127 DECL_TI_TEMPLATE field will point at the original
2128 specialization. For example:
2130 template <class T> struct S { template <class U> void f(U);
2131 template <> void f(int) {}; };
2132 S<double> sd;
2133 sd.f(3);
2135 Here, TMPL will be template <class U> S<double>::f(U).
2136 And, instantiate template will give us the specialization
2137 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2138 for this will point at template <class T> template <> S<T>::f(int),
2139 so that we can find the definition. For the purposes of
2140 overload resolution, however, we want the original TMPL. */
2141 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2142 else
2143 cand->template = DECL_TEMPLATE_INFO (fn);
2145 return cand;
2149 static struct z_candidate *
2150 add_template_candidate (candidates, tmpl, explicit_targs,
2151 arglist, return_type, flags, strict)
2152 struct z_candidate *candidates;
2153 tree tmpl, explicit_targs, arglist, return_type;
2154 int flags;
2155 unification_kind_t strict;
2157 return
2158 add_template_candidate_real (candidates, tmpl, explicit_targs,
2159 arglist, return_type, flags,
2160 NULL_TREE, strict);
2164 static struct z_candidate *
2165 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2166 struct z_candidate *candidates;
2167 tree tmpl, obj, arglist, return_type;
2169 return
2170 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
2171 return_type, 0, obj, DEDUCE_CONV);
2175 static int
2176 any_viable (cands)
2177 struct z_candidate *cands;
2179 for (; cands; cands = cands->next)
2180 if (pedantic ? cands->viable == 1 : cands->viable)
2181 return 1;
2182 return 0;
2185 static struct z_candidate *
2186 splice_viable (cands)
2187 struct z_candidate *cands;
2189 struct z_candidate **p = &cands;
2191 for (; *p; )
2193 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2194 p = &((*p)->next);
2195 else
2196 *p = (*p)->next;
2199 return cands;
2202 static tree
2203 build_this (obj)
2204 tree obj;
2206 /* Fix this to work on non-lvalues. */
2207 return build_unary_op (ADDR_EXPR, obj, 0);
2210 static void
2211 print_z_candidates (candidates)
2212 struct z_candidate *candidates;
2214 const char *str = "candidates are:";
2215 for (; candidates; candidates = candidates->next)
2217 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2219 if (candidates->fn == ansi_opname [COND_EXPR])
2220 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2221 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2222 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2223 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2224 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2225 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2226 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2227 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2228 else
2229 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2230 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2232 else if (TYPE_P (candidates->fn))
2233 cp_error ("%s %T <conversion>", str, candidates->fn);
2234 else
2235 cp_error_at ("%s %+#D%s", str, candidates->fn,
2236 candidates->viable == -1 ? " <near match>" : "");
2237 str = " ";
2241 /* Returns the best overload candidate to perform the requested
2242 conversion. This function is used for three the overloading situations
2243 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2244 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2245 per [dcl.init.ref], so we ignore temporary bindings. */
2247 static struct z_candidate *
2248 build_user_type_conversion_1 (totype, expr, flags)
2249 tree totype, expr;
2250 int flags;
2252 struct z_candidate *candidates, *cand;
2253 tree fromtype = TREE_TYPE (expr);
2254 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2255 tree args = NULL_TREE;
2256 tree templates = NULL_TREE;
2258 if (IS_AGGR_TYPE (totype))
2259 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2260 if (IS_AGGR_TYPE (fromtype)
2261 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2262 convs = lookup_conversions (fromtype);
2264 candidates = 0;
2265 flags |= LOOKUP_NO_CONVERSION;
2267 if (ctors)
2269 tree t = build_int_2 (0, 0);
2270 TREE_TYPE (t) = build_pointer_type (totype);
2271 args = build_scratch_list (NULL_TREE, expr);
2272 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2273 args = tree_cons (NULL_TREE, integer_one_node, args);
2274 args = tree_cons (NULL_TREE, t, args);
2276 ctors = TREE_VALUE (ctors);
2278 for (; ctors; ctors = OVL_NEXT (ctors))
2280 tree ctor = OVL_CURRENT (ctors);
2281 if (DECL_NONCONVERTING_P (ctor))
2282 continue;
2284 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2286 templates = tree_cons (NULL_TREE, ctor, templates);
2287 candidates =
2288 add_template_candidate (candidates, ctor,
2289 NULL_TREE, args, NULL_TREE, flags,
2290 DEDUCE_CALL);
2292 else
2293 candidates = add_function_candidate (candidates, ctor,
2294 args, flags);
2296 if (candidates)
2298 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2299 candidates->basetype_path = TYPE_BINFO (totype);
2303 if (convs)
2304 args = build_scratch_list (NULL_TREE, build_this (expr));
2306 for (; convs; convs = TREE_CHAIN (convs))
2308 tree fns = TREE_VALUE (convs);
2309 int convflags = LOOKUP_NO_CONVERSION;
2310 tree ics;
2312 /* If we are called to convert to a reference type, we are trying to
2313 find an lvalue binding, so don't even consider temporaries. If
2314 we don't find an lvalue binding, the caller will try again to
2315 look for a temporary binding. */
2316 if (TREE_CODE (totype) == REFERENCE_TYPE)
2317 convflags |= LOOKUP_NO_TEMP_BIND;
2319 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2320 ics = implicit_conversion
2321 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2322 else
2323 /* We can't compute this yet. */
2324 ics = error_mark_node;
2326 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2327 /* ignore the near match. */;
2328 else if (ics)
2329 for (; fns; fns = OVL_NEXT (fns))
2331 tree fn = OVL_CURRENT (fns);
2332 struct z_candidate *old_candidates = candidates;
2334 if (TREE_CODE (fn) == TEMPLATE_DECL)
2336 templates = tree_cons (NULL_TREE, fn, templates);
2337 candidates =
2338 add_template_candidate (candidates, fn, NULL_TREE,
2339 args, totype, flags,
2340 DEDUCE_CONV);
2342 else
2343 candidates = add_function_candidate (candidates, fn,
2344 args, flags);
2346 if (candidates != old_candidates)
2348 if (TREE_CODE (fn) == TEMPLATE_DECL)
2349 ics = implicit_conversion
2350 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2351 0, convflags);
2353 candidates->second_conv = ics;
2354 candidates->basetype_path = TREE_PURPOSE (convs);
2356 if (ics == NULL_TREE)
2357 candidates->viable = 0;
2358 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2359 candidates->viable = -1;
2364 if (! any_viable (candidates))
2366 #if 0
2367 if (flags & LOOKUP_COMPLAIN)
2369 if (candidates && ! candidates->next)
2370 /* say why this one won't work or try to be loose */;
2371 else
2372 cp_error ("no viable candidates");
2374 #endif
2376 return 0;
2379 candidates = splice_viable (candidates);
2380 cand = tourney (candidates);
2382 if (cand == 0)
2384 if (flags & LOOKUP_COMPLAIN)
2386 cp_error ("conversion from `%T' to `%T' is ambiguous",
2387 fromtype, totype);
2388 print_z_candidates (candidates);
2391 cand = candidates; /* any one will do */
2392 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2393 ICS_USER_FLAG (cand->second_conv) = 1;
2394 ICS_BAD_FLAG (cand->second_conv) = 1;
2396 return cand;
2399 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2400 p = &(TREE_OPERAND (*p, 0));
2402 /* Pedantically, normal function declarations are never considered
2403 to refer to template instantiations, so we only do this with
2404 -fguiding-decls. */
2405 if (flag_guiding_decls && templates && ! cand->template
2406 && !DECL_INITIAL (cand->fn)
2407 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2408 add_maybe_template (cand->fn, templates);
2410 *p = build
2411 (USER_CONV,
2412 (DECL_CONSTRUCTOR_P (cand->fn)
2413 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2414 expr, build_expr_ptr_wrapper (cand));
2415 ICS_USER_FLAG (cand->second_conv) = 1;
2416 if (cand->viable == -1)
2417 ICS_BAD_FLAG (cand->second_conv) = 1;
2419 return cand;
2422 tree
2423 build_user_type_conversion (totype, expr, flags)
2424 tree totype, expr;
2425 int flags;
2427 struct z_candidate *cand
2428 = build_user_type_conversion_1 (totype, expr, flags);
2430 if (cand)
2432 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2433 return error_mark_node;
2434 return convert_from_reference (convert_like (cand->second_conv, expr));
2436 return NULL_TREE;
2439 /* Do any initial processing on the arguments to a function call. */
2441 static tree
2442 resolve_args (args)
2443 tree args;
2445 tree t;
2446 for (t = args; t; t = TREE_CHAIN (t))
2448 if (TREE_VALUE (t) == error_mark_node)
2449 return error_mark_node;
2450 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2452 error ("invalid use of void expression");
2453 return error_mark_node;
2455 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2456 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2458 return args;
2461 tree
2462 build_new_function_call (fn, args)
2463 tree fn, args;
2465 struct z_candidate *candidates = 0, *cand;
2466 tree explicit_targs = NULL_TREE;
2467 int template_only = 0;
2469 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2471 explicit_targs = TREE_OPERAND (fn, 1);
2472 fn = TREE_OPERAND (fn, 0);
2473 template_only = 1;
2476 if (really_overloaded_fn (fn))
2478 tree t1;
2479 tree templates = NULL_TREE;
2481 args = resolve_args (args);
2483 if (args == error_mark_node)
2484 return error_mark_node;
2486 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2488 tree t = OVL_FUNCTION (t1);
2489 struct z_candidate *old_candidates = candidates;
2491 if (TREE_CODE (t) == TEMPLATE_DECL)
2493 templates = tree_cons (NULL_TREE, t, templates);
2494 candidates = add_template_candidate
2495 (candidates, t, explicit_targs, args, NULL_TREE,
2496 LOOKUP_NORMAL, DEDUCE_CALL);
2498 else if (! template_only)
2499 candidates = add_function_candidate
2500 (candidates, t, args, LOOKUP_NORMAL);
2502 if (candidates != old_candidates)
2503 candidates->basetype_path = DECL_REAL_CONTEXT (t);
2506 if (! any_viable (candidates))
2508 if (candidates && ! candidates->next)
2509 return build_function_call (candidates->fn, args);
2510 cp_error ("no matching function for call to `%D (%A)'",
2511 DECL_NAME (OVL_FUNCTION (fn)), args);
2512 if (candidates)
2513 print_z_candidates (candidates);
2514 return error_mark_node;
2516 candidates = splice_viable (candidates);
2517 cand = tourney (candidates);
2519 if (cand == 0)
2521 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2522 DECL_NAME (OVL_FUNCTION (fn)), args);
2523 print_z_candidates (candidates);
2524 return error_mark_node;
2527 /* Pedantically, normal function declarations are never considered
2528 to refer to template instantiations, so we only do this with
2529 -fguiding-decls. */
2530 if (flag_guiding_decls && templates && ! cand->template
2531 && ! DECL_INITIAL (cand->fn))
2532 add_maybe_template (cand->fn, templates);
2534 return build_over_call (cand, args, LOOKUP_NORMAL);
2537 /* This is not really overloaded. */
2538 fn = OVL_CURRENT (fn);
2540 return build_function_call (fn, args);
2543 static tree
2544 build_object_call (obj, args)
2545 tree obj, args;
2547 struct z_candidate *candidates = 0, *cand;
2548 tree fns, convs, mem_args = NULL_TREE;
2549 tree type = TREE_TYPE (obj);
2551 if (TYPE_PTRMEMFUNC_P (type))
2553 /* It's no good looking for an overloaded operator() on a
2554 pointer-to-member-function. */
2555 cp_error ("pointer-to-member function %E cannot be called", obj);
2556 cp_error ("without an object; consider using .* or ->*");
2557 return error_mark_node;
2560 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2561 if (fns == error_mark_node)
2562 return error_mark_node;
2564 args = resolve_args (args);
2566 if (args == error_mark_node)
2567 return error_mark_node;
2569 if (fns)
2571 tree base = TREE_PURPOSE (fns);
2572 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2574 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2576 tree fn = OVL_CURRENT (fns);
2577 if (TREE_CODE (fn) == TEMPLATE_DECL)
2579 candidates
2580 = add_template_candidate (candidates, fn, NULL_TREE,
2581 mem_args, NULL_TREE,
2582 LOOKUP_NORMAL, DEDUCE_CALL);
2584 else
2585 candidates = add_function_candidate
2586 (candidates, fn, mem_args, LOOKUP_NORMAL);
2588 if (candidates)
2589 candidates->basetype_path = base;
2593 convs = lookup_conversions (type);
2595 for (; convs; convs = TREE_CHAIN (convs))
2597 tree fns = TREE_VALUE (convs);
2598 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2600 if ((TREE_CODE (totype) == POINTER_TYPE
2601 || TREE_CODE (totype) == REFERENCE_TYPE)
2602 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2603 for (; fns; fns = OVL_NEXT (fns))
2605 tree fn = OVL_CURRENT (fns);
2606 if (TREE_CODE (fn) == TEMPLATE_DECL)
2608 candidates = add_template_conv_candidate (candidates,
2610 obj,
2611 args,
2612 totype);
2614 else
2615 candidates = add_conv_candidate (candidates, fn, obj, args);
2617 if (candidates)
2618 candidates->basetype_path = TREE_PURPOSE (convs);
2622 if (! any_viable (candidates))
2624 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2625 print_z_candidates (candidates);
2626 return error_mark_node;
2629 candidates = splice_viable (candidates);
2630 cand = tourney (candidates);
2632 if (cand == 0)
2634 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2635 print_z_candidates (candidates);
2636 return error_mark_node;
2639 /* Since cand->fn will be a type, not a function, for a conversion
2640 function, we must be careful not to unconditionally look at
2641 DECL_NAME here. */
2642 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2643 && DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2644 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2646 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2648 /* FIXME */
2649 return build_function_call (obj, args);
2652 static void
2653 op_error (code, code2, arg1, arg2, arg3, problem)
2654 enum tree_code code, code2;
2655 tree arg1, arg2, arg3;
2656 const char *problem;
2658 const char * opname
2659 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2661 switch (code)
2663 case COND_EXPR:
2664 cp_error ("%s for `%T ? %T : %T'", problem,
2665 error_type (arg1), error_type (arg2), error_type (arg3));
2666 break;
2667 case POSTINCREMENT_EXPR:
2668 case POSTDECREMENT_EXPR:
2669 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2670 break;
2671 case ARRAY_REF:
2672 cp_error ("%s for `%T[%T]'", problem,
2673 error_type (arg1), error_type (arg2));
2674 break;
2675 default:
2676 if (arg2)
2677 cp_error ("%s for `%T %s %T'", problem,
2678 error_type (arg1), opname, error_type (arg2));
2679 else
2680 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2684 /* Return the implicit conversion sequence that could be used to
2685 convert E1 to E2 in [expr.cond]. */
2687 static tree
2688 conditional_conversion (e1, e2)
2689 tree e1;
2690 tree e2;
2692 tree t1 = non_reference (TREE_TYPE (e1));
2693 tree t2 = non_reference (TREE_TYPE (e2));
2694 tree conv;
2696 /* [expr.cond]
2698 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2699 implicitly converted (clause _conv_) to the type "reference to
2700 T2", subject to the constraint that in the conversion the
2701 reference must bind directly (_dcl.init.ref_) to E1. */
2702 if (real_lvalue_p (e2))
2704 conv = implicit_conversion (build_reference_type (t2),
2707 LOOKUP_NO_TEMP_BIND);
2708 if (conv)
2709 return conv;
2712 /* [expr.cond]
2714 If E1 and E2 have class type, and the underlying class types are
2715 the same or one is a base class of the other: E1 can be converted
2716 to match E2 if the class of T2 is the same type as, or a base
2717 class of, the class of T1, and the cv-qualification of T2 is the
2718 same cv-qualification as, or a greater cv-qualification than, the
2719 cv-qualification of T1. If the conversion is applied, E1 is
2720 changed to an rvalue of type T2 that still refers to the original
2721 source class object (or the appropriate subobject thereof). */
2722 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2723 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2724 TYPE_MAIN_VARIANT (t1)))
2726 if (at_least_as_qualified_p (t2, t1))
2728 conv = build1 (IDENTITY_CONV, t1, e1);
2729 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2730 TYPE_MAIN_VARIANT (t2)))
2731 conv = build_conv (BASE_CONV, t2, conv);
2732 return conv;
2734 else
2735 return NULL_TREE;
2738 /* [expr.cond]
2740 E1 can be converted to match E2 if E1 can be implicitly converted
2741 to the type that expression E2 would have if E2 were converted to
2742 an rvalue (or the type it has, if E2 is an rvalue). */
2743 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2746 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2747 arguments to the conditional expression. By the time this function
2748 is called, any suitable candidate functions are included in
2749 CANDIDATES. */
2751 tree
2752 build_conditional_expr (arg1, arg2, arg3)
2753 tree arg1;
2754 tree arg2;
2755 tree arg3;
2757 tree arg2_type;
2758 tree arg3_type;
2759 tree result;
2760 tree result_type = NULL_TREE;
2761 int lvalue_p = 1;
2762 struct z_candidate *candidates = 0;
2763 struct z_candidate *cand;
2765 /* As a G++ extension, the second argument to the conditional can be
2766 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2767 c'.) If the second operand is omitted, make sure it is
2768 calculated only once. */
2769 if (!arg2)
2771 if (pedantic)
2772 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
2773 arg1 = arg2 = save_expr (arg1);
2776 /* If something has already gone wrong, just pass that fact up the
2777 tree. */
2778 if (arg1 == error_mark_node
2779 || arg2 == error_mark_node
2780 || arg3 == error_mark_node
2781 || TREE_TYPE (arg1) == error_mark_node
2782 || TREE_TYPE (arg2) == error_mark_node
2783 || TREE_TYPE (arg3) == error_mark_node)
2784 return error_mark_node;
2786 /* [expr.cond]
2788 The first expr ession is implicitly converted to bool (clause
2789 _conv_). */
2790 arg1 = cp_convert (boolean_type_node, arg1);
2792 /* Convert from reference types to ordinary types; no expressions
2793 really have reference type in C++. */
2794 arg2 = convert_from_reference (arg2);
2795 arg3 = convert_from_reference (arg3);
2797 /* [expr.cond]
2799 If either the second or the third operand has type (possibly
2800 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2801 array-to-pointer (_conv.array_), and function-to-pointer
2802 (_conv.func_) standard conversions are performed on the second
2803 and third operands. */
2804 arg2_type = TREE_TYPE (arg2);
2805 arg3_type = TREE_TYPE (arg3);
2806 if (same_type_p (TYPE_MAIN_VARIANT (arg2_type), void_type_node)
2807 || same_type_p (TYPE_MAIN_VARIANT (arg3_type), void_type_node))
2809 int arg2_void_p;
2810 int arg3_void_p;
2812 /* Do the conversions. We don't these for `void' type arguments
2813 since it can't have any effect and since decay_conversion
2814 does not handle that case gracefully. */
2815 if (!same_type_p (TYPE_MAIN_VARIANT (arg2_type), void_type_node))
2816 arg2 = decay_conversion (arg2);
2817 if (!same_type_p (TYPE_MAIN_VARIANT (arg3_type), void_type_node))
2818 arg3 = decay_conversion (arg3);
2819 arg2_type = TREE_TYPE (arg2);
2820 arg3_type = TREE_TYPE (arg3);
2822 arg2_void_p = same_type_p (TYPE_MAIN_VARIANT (arg2_type),
2823 void_type_node);
2824 arg3_void_p = same_type_p (TYPE_MAIN_VARIANT (arg3_type),
2825 void_type_node);
2827 /* [expr.cond]
2829 One of the following shall hold:
2831 --The second or the third operand (but not both) is a
2832 throw-expression (_except.throw_); the result is of the
2833 type of the other and is an rvalue.
2835 --Both the second and the third operands have type void; the
2836 result is of type void and is an rvalue. */
2837 if ((TREE_CODE (arg2) == THROW_EXPR)
2838 ^ (TREE_CODE (arg3) == THROW_EXPR))
2839 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2840 ? arg3_type : arg2_type);
2841 else if (arg2_void_p && arg3_void_p)
2842 result_type = void_type_node;
2843 else
2845 cp_error ("`%E' has type `void' and is not a throw-expression",
2846 arg2_void_p ? arg2 : arg3);
2847 return error_mark_node;
2850 lvalue_p = 0;
2851 goto valid_operands;
2853 /* [expr.cond]
2855 Otherwise, if the second and third operand have different types,
2856 and either has (possibly cv-qualified) class type, an attempt is
2857 made to convert each of those operands to the type of the other. */
2858 else if (!same_type_p (arg2_type, arg3_type)
2859 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2861 tree conv2 = conditional_conversion (arg2, arg3);
2862 tree conv3 = conditional_conversion (arg3, arg2);
2864 /* [expr.cond]
2866 If both can be converted, or one can be converted but the
2867 conversion is ambiguous, the program is ill-formed. If
2868 neither can be converted, the operands are left unchanged and
2869 further checking is performed as described below. If exactly
2870 one conversion is possible, that conversion is applied to the
2871 chosen operand and the converted operand is used in place of
2872 the original operand for the remainder of this section. */
2873 if ((conv2 && !ICS_BAD_FLAG (conv2)
2874 && conv3 && !ICS_BAD_FLAG (conv3))
2875 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
2876 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
2878 cp_error ("operands to ?: have different types");
2879 return error_mark_node;
2881 else if (conv2 && !ICS_BAD_FLAG (conv2))
2883 arg2 = convert_like (conv2, arg2);
2884 arg2 = convert_from_reference (arg2);
2885 /* That may not quite have done the trick. If the two types
2886 are cv-qualified variants of one another, we will have
2887 just used an IDENTITY_CONV. (There's no conversion from
2888 an lvalue of one class type to an lvalue of another type,
2889 even a cv-qualified variant, and we don't want to lose
2890 lvalue-ness here.) So, we manually add a NOP_EXPR here
2891 if necessary. */
2892 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
2893 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
2894 arg2_type = TREE_TYPE (arg2);
2896 else if (conv3 && !ICS_BAD_FLAG (conv3))
2898 arg3 = convert_like (conv3, arg3);
2899 arg3 = convert_from_reference (arg3);
2900 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
2901 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
2902 arg3_type = TREE_TYPE (arg3);
2906 /* [expr.cond]
2908 If the second and third operands are lvalues and have the same
2909 type, the result is of that type and is an lvalue. */
2910 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
2911 same_type_p (arg2_type, arg3_type))
2913 result_type = arg2_type;
2914 goto valid_operands;
2917 /* [expr.cond]
2919 Otherwise, the result is an rvalue. If the second and third
2920 operand do not have the same type, and either has (possibly
2921 cv-qualified) class type, overload resolution is used to
2922 determine the conversions (if any) to be applied to the operands
2923 (_over.match.oper_, _over.built_). */
2924 lvalue_p = 0;
2925 if (!same_type_p (arg2_type, arg3_type)
2926 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2928 tree args[3];
2929 tree conv;
2931 /* Rearrange the arguments so that add_builtin_candidate only has
2932 to know about two args. In build_builtin_candidates, the
2933 arguments are unscrambled. */
2934 args[0] = arg2;
2935 args[1] = arg3;
2936 args[2] = arg1;
2937 candidates = add_builtin_candidates (candidates,
2938 COND_EXPR,
2939 NOP_EXPR,
2940 ansi_opname[COND_EXPR],
2941 args,
2942 LOOKUP_NORMAL);
2944 /* [expr.cond]
2946 If the overload resolution fails, the program is
2947 ill-formed. */
2948 if (!any_viable (candidates))
2950 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
2951 print_z_candidates (candidates);
2952 return error_mark_node;
2954 candidates = splice_viable (candidates);
2955 cand = tourney (candidates);
2956 if (!cand)
2958 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
2959 print_z_candidates (candidates);
2960 return error_mark_node;
2963 /* [expr.cond]
2965 Otherwise, the conversions thus determined are applied, and
2966 the converted operands are used in place of the original
2967 operands for the remainder of this section. */
2968 conv = TREE_VEC_ELT (cand->convs, 0);
2969 arg1 = convert_like (conv, arg1);
2970 conv = TREE_VEC_ELT (cand->convs, 1);
2971 arg2 = convert_like (conv, arg2);
2972 conv = TREE_VEC_ELT (cand->convs, 2);
2973 arg3 = convert_like (conv, arg3);
2976 /* [expr.cond]
2978 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
2979 and function-to-pointer (_conv.func_) standard conversions are
2980 performed on the second and third operands.
2982 We need to force the lvalue-to-rvalue conversion here for class types,
2983 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
2984 that isn't wrapped with a TARGET_EXPR plays havoc with exception
2985 regions. */
2987 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
2988 arg2 = build_user_type_conversion (TREE_TYPE (arg2), arg2, LOOKUP_NORMAL);
2989 else
2990 arg2 = decay_conversion (arg2);
2991 arg2_type = TREE_TYPE (arg2);
2993 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
2994 arg3 = build_user_type_conversion (TREE_TYPE (arg3), arg3, LOOKUP_NORMAL);
2995 else
2996 arg3 = decay_conversion (arg3);
2997 arg3_type = TREE_TYPE (arg3);
2999 /* [expr.cond]
3001 After those conversions, one of the following shall hold:
3003 --The second and third operands have the same type; the result is of
3004 that type. */
3005 if (same_type_p (arg2_type, arg3_type))
3006 result_type = arg2_type;
3007 /* [expr.cond]
3009 --The second and third operands have arithmetic or enumeration
3010 type; the usual arithmetic conversions are performed to bring
3011 them to a common type, and the result is of that type. */
3012 else if ((ARITHMETIC_TYPE_P (arg2_type)
3013 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3014 && (ARITHMETIC_TYPE_P (arg3_type)
3015 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3017 /* In this case, there is always a common type. */
3018 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3019 arg3_type);
3021 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3022 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3023 cp_warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3024 arg2_type, arg3_type);
3025 else if (extra_warnings
3026 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3027 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3028 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3029 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3030 cp_warning ("enumeral and non-enumeral type in conditional expression");
3032 arg2 = perform_implicit_conversion (result_type, arg2);
3033 arg3 = perform_implicit_conversion (result_type, arg3);
3035 /* [expr.cond]
3037 --The second and third operands have pointer type, or one has
3038 pointer type and the other is a null pointer constant; pointer
3039 conversions (_conv.ptr_) and qualification conversions
3040 (_conv.qual_) are performed to bring them to their composite
3041 pointer type (_expr.rel_). The result is of the composite
3042 pointer type.
3044 --The second and third operands have pointer to member type, or
3045 one has pointer to member type and the other is a null pointer
3046 constant; pointer to member conversions (_conv.mem_) and
3047 qualification conversions (_conv.qual_) are performed to bring
3048 them to a common type, whose cv-qualification shall match the
3049 cv-qualification of either the second or the third operand.
3050 The result is of the common type. */
3051 else if ((null_ptr_cst_p (arg2)
3052 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3053 || TYPE_PTRMEMFUNC_P (arg3_type)))
3054 || (null_ptr_cst_p (arg3)
3055 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3056 || TYPE_PTRMEMFUNC_P (arg2_type)))
3057 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3058 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3059 || (TYPE_PTRMEMFUNC_P (arg2_type)
3060 && TYPE_PTRMEMFUNC_P (arg3_type)))
3062 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3063 arg3, "conditional expression");
3064 arg2 = perform_implicit_conversion (result_type, arg2);
3065 arg3 = perform_implicit_conversion (result_type, arg3);
3068 if (!result_type)
3070 cp_error ("operands to ?: have different types");
3071 return error_mark_node;
3074 valid_operands:
3075 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3076 /* Expand both sides into the same slot, hopefully the target of the
3077 ?: expression. We used to check for TARGET_EXPRs here, but now we
3078 sometimes wrap them in NOP_EXPRs so the test would fail. */
3079 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3081 tree slot = build (VAR_DECL, result_type);
3082 layout_decl (slot, 0);
3083 result = build_target_expr (slot, result);
3086 /* If this expression is an rvalue, but might be mistaken for an
3087 lvalue, we must add a NON_LVALUE_EXPR. */
3088 if (!lvalue_p && real_lvalue_p (result))
3089 result = build1 (NON_LVALUE_EXPR, result_type, result);
3091 return result;
3094 tree
3095 build_new_op (code, flags, arg1, arg2, arg3)
3096 enum tree_code code;
3097 int flags;
3098 tree arg1, arg2, arg3;
3100 struct z_candidate *candidates = 0, *cand;
3101 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3102 enum tree_code code2 = NOP_EXPR;
3103 tree templates = NULL_TREE;
3104 tree conv;
3106 if (arg1 == error_mark_node
3107 || arg2 == error_mark_node
3108 || arg3 == error_mark_node)
3109 return error_mark_node;
3111 /* This can happen if a template takes all non-type parameters, e.g.
3112 undeclared_template<1, 5, 72>a; */
3113 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3115 cp_error ("`%D' must be declared before use", arg1);
3116 return error_mark_node;
3119 if (code == MODIFY_EXPR)
3121 code2 = TREE_CODE (arg3);
3122 arg3 = NULL_TREE;
3123 fnname = ansi_assopname[code2];
3125 else
3126 fnname = ansi_opname[code];
3128 switch (code)
3130 case NEW_EXPR:
3131 case VEC_NEW_EXPR:
3132 case VEC_DELETE_EXPR:
3133 case DELETE_EXPR:
3134 /* Use build_op_new_call and build_op_delete_call instead. */
3135 my_friendly_abort (981018);
3137 case CALL_EXPR:
3138 return build_object_call (arg1, arg2);
3140 default:
3141 break;
3144 /* The comma operator can have void args. */
3145 if (TREE_CODE (arg1) == OFFSET_REF)
3146 arg1 = resolve_offset_ref (arg1);
3147 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
3148 arg2 = resolve_offset_ref (arg2);
3149 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
3150 arg3 = resolve_offset_ref (arg3);
3152 if (code == COND_EXPR)
3154 if (arg2 == NULL_TREE
3155 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3156 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3157 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3158 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3159 goto builtin;
3161 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3162 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3163 goto builtin;
3165 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3166 arg2 = integer_zero_node;
3168 if (arg2 && arg3)
3169 arglist = tree_cons (NULL_TREE, arg1, tree_cons
3170 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
3171 else if (arg2)
3172 arglist = tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
3173 else
3174 arglist = build_scratch_list (NULL_TREE, arg1);
3176 fns = lookup_function_nonclass (fnname, arglist);
3178 if (fns && TREE_CODE (fns) == TREE_LIST)
3179 fns = TREE_VALUE (fns);
3180 for (; fns; fns = OVL_NEXT (fns))
3182 tree fn = OVL_CURRENT (fns);
3183 if (TREE_CODE (fn) == TEMPLATE_DECL)
3185 templates = tree_cons (NULL_TREE, fn, templates);
3186 candidates
3187 = add_template_candidate (candidates, fn, NULL_TREE,
3188 arglist, TREE_TYPE (fnname),
3189 flags, DEDUCE_CALL);
3191 else
3192 candidates = add_function_candidate (candidates, fn, arglist, flags);
3195 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3197 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3198 if (fns == error_mark_node)
3199 return fns;
3201 else
3202 fns = NULL_TREE;
3204 if (fns)
3206 tree basetype = TREE_PURPOSE (fns);
3207 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3208 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3210 tree fn = OVL_CURRENT (fns);
3211 tree this_arglist;
3213 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3214 this_arglist = mem_arglist;
3215 else
3216 this_arglist = arglist;
3218 if (TREE_CODE (fn) == TEMPLATE_DECL)
3220 /* A member template. */
3221 templates = tree_cons (NULL_TREE, fn, templates);
3222 candidates
3223 = add_template_candidate (candidates, fn, NULL_TREE,
3224 this_arglist, TREE_TYPE (fnname),
3225 flags, DEDUCE_CALL);
3227 else
3228 candidates = add_function_candidate
3229 (candidates, fn, this_arglist, flags);
3231 if (candidates)
3232 candidates->basetype_path = basetype;
3237 tree args[3];
3239 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3240 to know about two args; a builtin candidate will always have a first
3241 parameter of type bool. We'll handle that in
3242 build_builtin_candidate. */
3243 if (code == COND_EXPR)
3245 args[0] = arg2;
3246 args[1] = arg3;
3247 args[2] = arg1;
3249 else
3251 args[0] = arg1;
3252 args[1] = arg2;
3253 args[2] = NULL_TREE;
3256 candidates = add_builtin_candidates
3257 (candidates, code, code2, fnname, args, flags);
3260 if (! any_viable (candidates))
3262 switch (code)
3264 case POSTINCREMENT_EXPR:
3265 case POSTDECREMENT_EXPR:
3266 /* Look for an `operator++ (int)'. If they didn't have
3267 one, then we fall back to the old way of doing things. */
3268 if (flags & LOOKUP_COMPLAIN)
3269 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
3270 fnname, opname_tab [code]);
3271 if (code == POSTINCREMENT_EXPR)
3272 code = PREINCREMENT_EXPR;
3273 else
3274 code = PREDECREMENT_EXPR;
3275 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3277 /* The caller will deal with these. */
3278 case ADDR_EXPR:
3279 case COMPOUND_EXPR:
3280 case COMPONENT_REF:
3281 return NULL_TREE;
3283 default:
3284 break;
3286 if (flags & LOOKUP_COMPLAIN)
3288 op_error (code, code2, arg1, arg2, arg3, "no match");
3289 print_z_candidates (candidates);
3291 return error_mark_node;
3293 candidates = splice_viable (candidates);
3294 cand = tourney (candidates);
3296 if (cand == 0)
3298 if (flags & LOOKUP_COMPLAIN)
3300 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3301 print_z_candidates (candidates);
3303 return error_mark_node;
3306 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3308 extern int warn_synth;
3309 if (warn_synth
3310 && fnname == ansi_opname[MODIFY_EXPR]
3311 && DECL_ARTIFICIAL (cand->fn)
3312 && candidates->next
3313 && ! candidates->next->next)
3315 cp_warning ("using synthesized `%#D' for copy assignment",
3316 cand->fn);
3317 cp_warning_at (" where cfront would use `%#D'",
3318 cand == candidates
3319 ? candidates->next->fn
3320 : candidates->fn);
3323 /* Pedantically, normal function declarations are never considered
3324 to refer to template instantiations, so we only do this with
3325 -fguiding-decls. */
3326 if (flag_guiding_decls && templates && ! cand->template
3327 && ! DECL_INITIAL (cand->fn)
3328 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
3329 add_maybe_template (cand->fn, templates);
3331 return build_over_call
3332 (cand,
3333 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3334 ? mem_arglist : arglist,
3335 LOOKUP_NORMAL);
3338 /* Check for comparison of different enum types. */
3339 switch (code)
3341 case GT_EXPR:
3342 case LT_EXPR:
3343 case GE_EXPR:
3344 case LE_EXPR:
3345 case EQ_EXPR:
3346 case NE_EXPR:
3347 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3348 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3349 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3350 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3352 cp_warning ("comparison between `%#T' and `%#T'",
3353 TREE_TYPE (arg1), TREE_TYPE (arg2));
3355 break;
3356 default:
3357 break;
3360 /* We need to strip any leading REF_BIND so that bitfields don't cause
3361 errors. This should not remove any important conversions, because
3362 builtins don't apply to class objects directly. */
3363 conv = TREE_VEC_ELT (cand->convs, 0);
3364 if (TREE_CODE (conv) == REF_BIND)
3365 conv = TREE_OPERAND (conv, 0);
3366 arg1 = convert_like (conv, arg1);
3367 if (arg2)
3369 conv = TREE_VEC_ELT (cand->convs, 1);
3370 if (TREE_CODE (conv) == REF_BIND)
3371 conv = TREE_OPERAND (conv, 0);
3372 arg2 = convert_like (conv, arg2);
3374 if (arg3)
3376 conv = TREE_VEC_ELT (cand->convs, 2);
3377 if (TREE_CODE (conv) == REF_BIND)
3378 conv = TREE_OPERAND (conv, 0);
3379 arg3 = convert_like (conv, arg3);
3382 builtin:
3383 switch (code)
3385 case MODIFY_EXPR:
3386 return build_modify_expr (arg1, code2, arg2);
3388 case INDIRECT_REF:
3389 return build_indirect_ref (arg1, "unary *");
3391 case PLUS_EXPR:
3392 case MINUS_EXPR:
3393 case MULT_EXPR:
3394 case TRUNC_DIV_EXPR:
3395 case GT_EXPR:
3396 case LT_EXPR:
3397 case GE_EXPR:
3398 case LE_EXPR:
3399 case EQ_EXPR:
3400 case NE_EXPR:
3401 case MAX_EXPR:
3402 case MIN_EXPR:
3403 case LSHIFT_EXPR:
3404 case RSHIFT_EXPR:
3405 case TRUNC_MOD_EXPR:
3406 case BIT_AND_EXPR:
3407 case BIT_IOR_EXPR:
3408 case BIT_XOR_EXPR:
3409 case TRUTH_ANDIF_EXPR:
3410 case TRUTH_ORIF_EXPR:
3411 return build_binary_op_nodefault (code, arg1, arg2, code);
3413 case CONVERT_EXPR:
3414 case NEGATE_EXPR:
3415 case BIT_NOT_EXPR:
3416 case TRUTH_NOT_EXPR:
3417 case PREINCREMENT_EXPR:
3418 case POSTINCREMENT_EXPR:
3419 case PREDECREMENT_EXPR:
3420 case POSTDECREMENT_EXPR:
3421 case REALPART_EXPR:
3422 case IMAGPART_EXPR:
3423 return build_unary_op (code, arg1, candidates != 0);
3425 case ARRAY_REF:
3426 return build_array_ref (arg1, arg2);
3428 case COND_EXPR:
3429 return build_conditional_expr (arg1, arg2, arg3);
3431 case MEMBER_REF:
3432 return build_m_component_ref
3433 (build_indirect_ref (arg1, NULL_PTR), arg2);
3435 /* The caller will deal with these. */
3436 case ADDR_EXPR:
3437 case COMPONENT_REF:
3438 case COMPOUND_EXPR:
3439 return NULL_TREE;
3441 default:
3442 my_friendly_abort (367);
3443 return NULL_TREE;
3447 /* Build up a call to operator new. This has to be handled differently
3448 from other operators in the way lookup is handled; first members are
3449 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
3450 TYPE is the type to be created. ARGS are any new-placement args.
3451 FLAGS are the usual overloading flags. */
3453 tree
3454 build_op_new_call (code, type, args, flags)
3455 enum tree_code code;
3456 tree type, args;
3457 int flags;
3459 tree fnname = ansi_opname[code];
3461 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
3462 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
3464 return build_method_call (build_dummy_object (type),
3465 fnname, args, NULL_TREE, flags);
3467 else
3468 return build_new_function_call
3469 (lookup_function_nonclass (fnname, args), args);
3472 /* Build a call to operator delete. This has to be handled very specially,
3473 because the restrictions on what signatures match are different from all
3474 other call instances. For a normal delete, only a delete taking (void *)
3475 or (void *, size_t) is accepted. For a placement delete, only an exact
3476 match with the placement new is accepted.
3478 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3479 ADDR is the pointer to be deleted. For placement delete, it is also
3480 used to determine what the corresponding new looked like.
3481 SIZE is the size of the memory block to be deleted.
3482 FLAGS are the usual overloading flags.
3483 PLACEMENT is the corresponding placement new call, or 0. */
3485 tree
3486 build_op_delete_call (code, addr, size, flags, placement)
3487 enum tree_code code;
3488 tree addr, size, placement;
3489 int flags;
3491 tree fn, fns, fnname, fntype, argtypes, args, type;
3493 if (addr == error_mark_node)
3494 return error_mark_node;
3496 type = TREE_TYPE (TREE_TYPE (addr));
3497 fnname = ansi_opname[code];
3499 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3500 /* In [class.free]
3502 If the result of the lookup is ambiguous or inaccessible, or if
3503 the lookup selects a placement deallocation function, the
3504 program is ill-formed.
3506 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3508 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3509 if (fns == error_mark_node)
3510 return error_mark_node;
3512 else
3513 fns = NULL_TREE;
3515 if (fns == NULL_TREE)
3516 fns = lookup_name_nonclass (fnname);
3518 if (placement)
3520 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3522 /* Extract the function. */
3523 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3524 /* Then the second parm type. */
3525 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3527 /* Also the second argument. */
3528 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3530 else
3532 /* First try it without the size argument. */
3533 argtypes = void_list_node;
3534 args = NULL_TREE;
3537 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3538 fntype = build_function_type (void_type_node, argtypes);
3540 /* Strip const and volatile from addr. */
3541 if (type != TYPE_MAIN_VARIANT (type))
3542 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
3544 fn = instantiate_type (fntype, fns, 2);
3546 if (fn != error_mark_node)
3548 if (TREE_CODE (fns) == TREE_LIST)
3549 /* Member functions. */
3550 enforce_access (TREE_PURPOSE (fns), fn);
3551 return build_function_call (fn, tree_cons (NULL_TREE, addr, args));
3554 /* If we are doing placement delete we do nothing if we don't find a
3555 matching op delete. */
3556 if (placement)
3557 return NULL_TREE;
3559 /* Normal delete; now try to find a match including the size argument. */
3560 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3561 tree_cons (NULL_TREE, sizetype, void_list_node));
3562 fntype = build_function_type (void_type_node, argtypes);
3564 fn = instantiate_type (fntype, fns, 2);
3566 if (fn != error_mark_node)
3568 if (BASELINK_P (fns))
3569 /* Member functions. */
3570 enforce_access (TREE_PURPOSE (fns), fn);
3571 return build_function_call
3572 (fn, tree_cons (NULL_TREE, addr,
3573 build_expr_list (NULL_TREE, size)));
3576 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3577 destructor, in which case the error should be deferred
3578 until someone actually tries to delete one of these. */
3579 if (flags & LOOKUP_SPECULATIVELY)
3580 return NULL_TREE;
3582 cp_error ("no suitable operator delete for `%T'", type);
3583 return error_mark_node;
3586 /* If the current scope isn't allowed to access DECL along
3587 BASETYPE_PATH, give an error. The most derived class in
3588 BASETYPE_PATH is the one used to qualify DECL. */
3591 enforce_access (basetype_path, decl)
3592 tree basetype_path;
3593 tree decl;
3595 int accessible;
3597 accessible = accessible_p (basetype_path, decl);
3598 if (!accessible)
3600 if (TREE_PRIVATE (decl))
3601 cp_error_at ("`%+#D' is private", decl);
3602 else if (TREE_PROTECTED (decl))
3603 cp_error_at ("`%+#D' is protected", decl);
3604 else
3605 cp_error_at ("`%+#D' is inaccessible", decl);
3606 cp_error ("within this context");
3607 return 0;
3610 return 1;
3613 /* Perform the conversions in CONVS on the expression EXPR. */
3615 static tree
3616 convert_like (convs, expr)
3617 tree convs, expr;
3619 if (ICS_BAD_FLAG (convs)
3620 && TREE_CODE (convs) != USER_CONV
3621 && TREE_CODE (convs) != AMBIG_CONV
3622 && TREE_CODE (convs) != REF_BIND)
3624 tree t = convs;
3625 for (; t; t = TREE_OPERAND (t, 0))
3627 if (TREE_CODE (t) == USER_CONV)
3629 expr = convert_like (t, expr);
3630 break;
3632 else if (TREE_CODE (t) == AMBIG_CONV)
3633 return convert_like (t, expr);
3634 else if (TREE_CODE (t) == IDENTITY_CONV)
3635 break;
3637 return convert_for_initialization
3638 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3639 "conversion", NULL_TREE, 0);
3642 switch (TREE_CODE (convs))
3644 case USER_CONV:
3646 struct z_candidate *cand
3647 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3648 tree fn = cand->fn;
3649 tree args;
3651 if (DECL_CONSTRUCTOR_P (fn))
3653 tree t = build_int_2 (0, 0);
3654 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3656 args = build_scratch_list (NULL_TREE, expr);
3657 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3658 args = tree_cons (NULL_TREE, integer_one_node, args);
3659 args = tree_cons (NULL_TREE, t, args);
3661 else
3662 args = build_this (expr);
3663 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3665 /* If this is a constructor or a function returning an aggr type,
3666 we need to build up a TARGET_EXPR. */
3667 if (DECL_CONSTRUCTOR_P (fn))
3668 expr = build_cplus_new (TREE_TYPE (convs), expr);
3670 return expr;
3672 case IDENTITY_CONV:
3673 if (type_unknown_p (expr))
3674 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3675 return expr;
3676 case AMBIG_CONV:
3677 /* Call build_user_type_conversion again for the error. */
3678 return build_user_type_conversion
3679 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3681 default:
3682 break;
3685 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3686 if (expr == error_mark_node)
3687 return error_mark_node;
3689 /* Convert a constant variable to its underlying value, unless we
3690 are about to bind it to a reference, in which case we need to
3691 leave it as an lvalue. */
3692 if (TREE_READONLY_DECL_P (expr) && TREE_CODE (convs) != REF_BIND)
3693 expr = decl_constant_value (expr);
3695 switch (TREE_CODE (convs))
3697 case RVALUE_CONV:
3698 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3699 return expr;
3700 /* else fall through */
3701 case BASE_CONV:
3702 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3704 /* We are going to bind a reference directly to a base-class
3705 subobject of EXPR. */
3706 tree base_ptr = build_pointer_type (TREE_TYPE (convs));
3708 /* Build an expression for `*((base*) &expr)'. */
3709 expr = build_unary_op (ADDR_EXPR, expr, 0);
3710 expr = perform_implicit_conversion (base_ptr, expr);
3711 expr = build_indirect_ref (expr, "implicit conversion");
3712 return expr;
3716 tree cvt_expr = build_user_type_conversion
3717 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3718 if (!cvt_expr)
3720 /* This can occur if, for example, the EXPR has incomplete
3721 type. We can't check for that before attempting the
3722 conversion because the type might be an incomplete
3723 array type, which is OK if some constructor for the
3724 destination type takes a pointer argument. */
3725 if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
3727 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
3728 incomplete_type_error (expr, TREE_TYPE (expr));
3729 else
3730 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3731 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3733 else
3734 cp_error ("could not convert `%E' to `%T'",
3735 expr, TREE_TYPE (convs));
3736 return error_mark_node;
3738 return cvt_expr;
3741 case REF_BIND:
3743 tree ref_type = TREE_TYPE (convs);
3745 /* If necessary, create a temporary. */
3746 if (NEED_TEMPORARY_P (convs))
3748 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3749 tree slot = build_decl (VAR_DECL, NULL_TREE, type);
3750 DECL_ARTIFICIAL (slot) = 1;
3751 expr = build_target_expr (slot, expr);
3752 TREE_SIDE_EFFECTS (expr) = 1;
3755 /* Take the address of the thing to which we will bind the
3756 reference. */
3757 expr = build_unary_op (ADDR_EXPR, expr, 1);
3758 if (expr == error_mark_node)
3759 return error_mark_node;
3761 /* Convert it to a pointer to the type referred to by the
3762 reference. This will adjust the pointer if a derived to
3763 base conversion is being performed. */
3764 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3765 expr);
3766 /* Convert the pointer to the desired reference type. */
3767 expr = build1 (NOP_EXPR, ref_type, expr);
3769 return expr;
3772 case LVALUE_CONV:
3773 return decay_conversion (expr);
3775 case QUAL_CONV:
3776 /* Warn about deprecated conversion if appropriate. */
3777 string_conv_p (TREE_TYPE (convs), expr, 1);
3778 break;
3780 default:
3781 break;
3783 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3784 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3787 /* ARG is being passed to a varargs function. Perform any conversions
3788 required. Array/function to pointer decay must have already happened.
3789 Return the converted value. */
3791 tree
3792 convert_arg_to_ellipsis (arg)
3793 tree arg;
3795 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3796 && (TYPE_PRECISION (TREE_TYPE (arg))
3797 < TYPE_PRECISION (double_type_node)))
3798 /* Convert `float' to `double'. */
3799 arg = cp_convert (double_type_node, arg);
3800 else
3801 /* Convert `short' and `char' to full-size `int'. */
3802 arg = default_conversion (arg);
3804 arg = require_complete_type (arg);
3806 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
3808 /* Undefined behaviour [expr.call] 5.2.2/7. */
3809 cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3810 TREE_TYPE (arg));
3813 return arg;
3816 /* ARG is a default argument expression being passed to a parameter of
3817 the indicated TYPE, which is a parameter to FN. Do any required
3818 conversions. Return the converted value. */
3820 tree
3821 convert_default_arg (type, arg, fn)
3822 tree type;
3823 tree arg;
3824 tree fn;
3826 if (fn && DECL_TEMPLATE_INFO (fn))
3827 arg = tsubst_default_argument (fn, type, arg);
3829 arg = break_out_target_exprs (arg);
3831 if (TREE_CODE (arg) == CONSTRUCTOR)
3833 arg = digest_init (type, arg, 0);
3834 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3835 "default argument", 0, 0);
3837 else
3839 /* This could get clobbered by the following call. */
3840 if (TREE_HAS_CONSTRUCTOR (arg))
3841 arg = copy_node (arg);
3843 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3844 "default argument", 0, 0);
3845 if (PROMOTE_PROTOTYPES
3846 && (TREE_CODE (type) == INTEGER_TYPE
3847 || TREE_CODE (type) == ENUMERAL_TYPE)
3848 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3849 arg = default_conversion (arg);
3852 return arg;
3855 static tree
3856 build_over_call (cand, args, flags)
3857 struct z_candidate *cand;
3858 tree args;
3859 int flags;
3861 tree fn = cand->fn;
3862 tree convs = cand->convs;
3863 tree converted_args = NULL_TREE;
3864 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3865 tree conv, arg, val;
3866 int i = 0;
3867 int is_method = 0;
3869 /* Give any warnings we noticed during overload resolution. */
3870 if (cand->warnings)
3871 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3872 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3874 if (DECL_FUNCTION_MEMBER_P (fn))
3875 enforce_access (cand->basetype_path, fn);
3877 if (args && TREE_CODE (args) != TREE_LIST)
3878 args = build_scratch_list (NULL_TREE, args);
3879 arg = args;
3881 /* The implicit parameters to a constructor are not considered by overload
3882 resolution, and must be of the proper type. */
3883 if (DECL_CONSTRUCTOR_P (fn))
3885 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3886 arg = TREE_CHAIN (arg);
3887 parm = TREE_CHAIN (parm);
3888 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3890 converted_args = tree_cons
3891 (NULL_TREE, TREE_VALUE (arg), converted_args);
3892 arg = TREE_CHAIN (arg);
3893 parm = TREE_CHAIN (parm);
3896 /* Bypass access control for 'this' parameter. */
3897 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3899 tree parmtype = TREE_VALUE (parm);
3900 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3901 tree t;
3902 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3903 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3904 TREE_TYPE (argtype), fn);
3906 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3907 X is called for an object that is not of type X, or of a type
3908 derived from X, the behavior is undefined.
3910 So we can assume that anything passed as 'this' is non-null, and
3911 optimize accordingly. */
3912 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
3913 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3914 converted_args = tree_cons (NULL_TREE, t, converted_args);
3915 parm = TREE_CHAIN (parm);
3916 arg = TREE_CHAIN (arg);
3917 ++i;
3918 is_method = 1;
3921 for (; arg && parm;
3922 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3924 tree type = TREE_VALUE (parm);
3926 conv = TREE_VEC_ELT (convs, i);
3927 if (ICS_BAD_FLAG (conv))
3929 tree t = conv;
3930 val = TREE_VALUE (arg);
3932 for (; t; t = TREE_OPERAND (t, 0))
3934 if (TREE_CODE (t) == USER_CONV
3935 || TREE_CODE (t) == AMBIG_CONV)
3937 val = convert_like (t, val);
3938 break;
3940 else if (TREE_CODE (t) == IDENTITY_CONV)
3941 break;
3943 val = convert_for_initialization
3944 (NULL_TREE, type, val, LOOKUP_NORMAL,
3945 "argument passing", fn, i - is_method);
3947 else
3949 /* Issue warnings about peculiar, but legal, uses of NULL. */
3950 if (ARITHMETIC_TYPE_P (TREE_VALUE (parm))
3951 && TREE_VALUE (arg) == null_node)
3952 cp_warning ("converting NULL to non-pointer type");
3954 val = convert_like (conv, TREE_VALUE (arg));
3957 if (PROMOTE_PROTOTYPES
3958 && (TREE_CODE (type) == INTEGER_TYPE
3959 || TREE_CODE (type) == ENUMERAL_TYPE)
3960 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3961 val = default_conversion (val);
3962 converted_args = tree_cons (NULL_TREE, val, converted_args);
3965 /* Default arguments */
3966 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3967 converted_args
3968 = tree_cons (NULL_TREE,
3969 convert_default_arg (TREE_VALUE (parm),
3970 TREE_PURPOSE (parm),
3971 fn),
3972 converted_args);
3974 /* Ellipsis */
3975 for (; arg; arg = TREE_CHAIN (arg))
3976 converted_args
3977 = tree_cons (NULL_TREE,
3978 convert_arg_to_ellipsis (TREE_VALUE (arg)),
3979 converted_args);
3981 converted_args = nreverse (converted_args);
3983 if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
3984 check_function_format (DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
3985 converted_args);
3987 /* Avoid actually calling copy constructors and copy assignment operators,
3988 if possible. */
3990 if (! flag_elide_constructors)
3991 /* Do things the hard way. */;
3992 else if (DECL_CONSTRUCTOR_P (fn)
3993 && TREE_VEC_LENGTH (convs) == 1
3994 && copy_args_p (fn))
3996 tree targ;
3997 arg = TREE_CHAIN (converted_args);
3998 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3999 arg = TREE_CHAIN (arg);
4000 arg = TREE_VALUE (arg);
4002 /* Pull out the real argument, disregarding const-correctness. */
4003 targ = arg;
4004 while (TREE_CODE (targ) == NOP_EXPR
4005 || TREE_CODE (targ) == NON_LVALUE_EXPR
4006 || TREE_CODE (targ) == CONVERT_EXPR)
4007 targ = TREE_OPERAND (targ, 0);
4008 if (TREE_CODE (targ) == ADDR_EXPR)
4010 targ = TREE_OPERAND (targ, 0);
4011 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
4012 TYPE_MAIN_VARIANT (TREE_TYPE (targ))))
4013 targ = NULL_TREE;
4015 else
4016 targ = NULL_TREE;
4018 if (targ)
4019 arg = targ;
4020 else
4021 arg = build_indirect_ref (arg, 0);
4023 /* [class.copy]: the copy constructor is implicitly defined even if
4024 the implementation elided its use. */
4025 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4026 mark_used (fn);
4028 /* If we're creating a temp and we already have one, don't create a
4029 new one. If we're not creating a temp but we get one, use
4030 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4031 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4032 temp or an INIT_EXPR otherwise. */
4033 if (integer_zerop (TREE_VALUE (args)))
4035 if (! real_lvalue_p (arg))
4036 return arg;
4037 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4039 val = build_decl (VAR_DECL, NULL_TREE, DECL_CONTEXT (fn));
4040 val = build_target_expr (val, arg);
4041 return val;
4044 else if (! real_lvalue_p (arg)
4045 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4047 tree address;
4048 tree to = stabilize_reference
4049 (build_indirect_ref (TREE_VALUE (args), 0));
4051 /* If we're initializing an empty class, then we actually
4052 have to use a MODIFY_EXPR rather than an INIT_EXPR. The
4053 reason is that the dummy padding member in the target may
4054 not actually be allocated if TO is a base class
4055 subobject. Since we've set TYPE_NONCOPIED_PARTS on the
4056 padding, a MODIFY_EXPR will preserve its value, which is
4057 the right thing to do if it's not really padding at all.
4059 It's not safe to just throw away the ARG if we're looking
4060 at an empty class because the ARG might contain a
4061 TARGET_EXPR which wants to be bound to TO. If it is not,
4062 expand_expr will assign a dummy slot for the TARGET_EXPR,
4063 and we will call a destructor for it, which is wrong,
4064 because we will also destroy TO, but will never have
4065 constructed it. */
4066 val = build (is_empty_class (DECL_CLASS_CONTEXT (fn))
4067 ? MODIFY_EXPR : INIT_EXPR,
4068 DECL_CONTEXT (fn), to, arg);
4069 TREE_SIDE_EFFECTS (val) = 1;
4070 address = build_unary_op (ADDR_EXPR, val, 0);
4071 /* Avoid a warning about this expression, if the address is
4072 never used. */
4073 TREE_USED (address) = 1;
4074 return address;
4077 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
4078 && copy_args_p (fn)
4079 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
4081 tree to = stabilize_reference
4082 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4084 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4086 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4087 TREE_SIDE_EFFECTS (val) = 1;
4088 return val;
4091 mark_used (fn);
4093 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4095 tree t, *p = &TREE_VALUE (converted_args);
4096 tree binfo = get_binfo
4097 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
4098 *p = convert_pointer_to_real (binfo, *p);
4099 if (TREE_SIDE_EFFECTS (*p))
4100 *p = save_expr (*p);
4101 t = build_pointer_type (TREE_TYPE (fn));
4102 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4103 TREE_TYPE (fn) = t;
4105 else if (DECL_INLINE (fn))
4106 fn = inline_conversion (fn);
4107 else
4108 fn = build_addr_func (fn);
4110 /* Recognize certain built-in functions so we can make tree-codes
4111 other than CALL_EXPR. We do this when it enables fold-const.c
4112 to do something useful. */
4114 if (TREE_CODE (fn) == ADDR_EXPR
4115 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4116 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4117 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
4119 case BUILT_IN_ABS:
4120 case BUILT_IN_LABS:
4121 case BUILT_IN_FABS:
4122 if (converted_args == 0)
4123 return integer_zero_node;
4124 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
4125 default:
4126 break;
4129 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
4130 if (TREE_CODE (TREE_TYPE (fn)) == VOID_TYPE)
4131 return fn;
4132 fn = require_complete_type (fn);
4133 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4134 fn = build_cplus_new (TREE_TYPE (fn), fn);
4135 return convert_from_reference (fn);
4138 static tree
4139 build_new_method_call (instance, name, args, basetype_path, flags)
4140 tree instance, name, args, basetype_path;
4141 int flags;
4143 struct z_candidate *candidates = 0, *cand;
4144 tree explicit_targs = NULL_TREE;
4145 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4146 tree pretty_name;
4147 tree user_args = args;
4148 tree templates = NULL_TREE;
4149 int template_only = 0;
4151 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4153 explicit_targs = TREE_OPERAND (name, 1);
4154 name = TREE_OPERAND (name, 0);
4155 if (TREE_CODE_CLASS (TREE_CODE (name)) == 'd')
4156 name = DECL_NAME (name);
4157 else
4159 if (TREE_CODE (name) == COMPONENT_REF)
4160 name = TREE_OPERAND (name, 1);
4161 if (TREE_CODE (name) == OVERLOAD)
4162 name = DECL_NAME (OVL_CURRENT (name));
4165 template_only = 1;
4168 /* If there is an extra argument for controlling virtual bases,
4169 remove it for error reporting. */
4170 if (flags & LOOKUP_HAS_IN_CHARGE)
4171 user_args = TREE_CHAIN (args);
4173 args = resolve_args (args);
4175 if (args == error_mark_node)
4176 return error_mark_node;
4178 if (instance == NULL_TREE)
4179 basetype = BINFO_TYPE (basetype_path);
4180 else
4182 if (TREE_CODE (instance) == OFFSET_REF)
4183 instance = resolve_offset_ref (instance);
4184 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4185 instance = convert_from_reference (instance);
4186 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4188 /* XXX this should be handled before we get here. */
4189 if (! IS_AGGR_TYPE (basetype))
4191 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4192 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4193 name, instance, basetype);
4195 return error_mark_node;
4199 if (basetype_path == NULL_TREE)
4200 basetype_path = TYPE_BINFO (basetype);
4202 if (instance)
4204 instance_ptr = build_this (instance);
4206 if (! template_only)
4208 /* XXX this should be handled before we get here. */
4209 fns = build_field_call (basetype_path, instance_ptr, name, args);
4210 if (fns)
4211 return fns;
4214 else
4216 instance_ptr = build_int_2 (0, 0);
4217 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4220 pretty_name
4221 = (name == ctor_identifier ? constructor_name (basetype) : name);
4223 fns = lookup_fnfields (basetype_path, name, 1);
4225 if (fns == error_mark_node)
4226 return error_mark_node;
4227 if (fns)
4229 tree fn = TREE_VALUE (fns);
4230 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
4231 && ! (flags & LOOKUP_HAS_IN_CHARGE))
4233 flags |= LOOKUP_HAS_IN_CHARGE;
4234 args = tree_cons (NULL_TREE, integer_one_node, args);
4236 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4237 for (; fn; fn = OVL_NEXT (fn))
4239 tree t = OVL_CURRENT (fn);
4240 tree this_arglist;
4242 /* We can end up here for copy-init of same or base class. */
4243 if (name == ctor_identifier
4244 && (flags & LOOKUP_ONLYCONVERTING)
4245 && DECL_NONCONVERTING_P (t))
4246 continue;
4247 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
4248 this_arglist = mem_args;
4249 else
4250 this_arglist = args;
4252 if (TREE_CODE (t) == TEMPLATE_DECL)
4254 /* A member template. */
4255 templates = tree_cons (NULL_TREE, t, templates);
4256 candidates =
4257 add_template_candidate (candidates, t, explicit_targs,
4258 this_arglist,
4259 TREE_TYPE (name), flags, DEDUCE_CALL);
4261 else if (! template_only)
4262 candidates = add_function_candidate (candidates, t,
4263 this_arglist, flags);
4265 if (candidates)
4266 candidates->basetype_path = TREE_PURPOSE (fns);
4270 if (! any_viable (candidates))
4272 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4273 if (flags & LOOKUP_SPECULATIVELY)
4274 return NULL_TREE;
4275 if (TYPE_SIZE (basetype) == 0)
4276 incomplete_type_error (instance_ptr, basetype);
4277 else
4278 cp_error ("no matching function for call to `%T::%D (%A)%V'",
4279 basetype, pretty_name, user_args,
4280 TREE_TYPE (TREE_TYPE (instance_ptr)));
4281 print_z_candidates (candidates);
4282 return error_mark_node;
4284 candidates = splice_viable (candidates);
4285 cand = tourney (candidates);
4287 if (cand == 0)
4289 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4290 user_args);
4291 print_z_candidates (candidates);
4292 return error_mark_node;
4295 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
4296 && instance == current_class_ref
4297 && DECL_CONSTRUCTOR_P (current_function_decl)
4298 && ! (flags & LOOKUP_NONVIRTUAL)
4299 && value_member (cand->fn, CLASSTYPE_ABSTRACT_VIRTUALS (basetype)))
4300 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
4301 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4302 && is_dummy_object (instance_ptr))
4304 cp_error ("cannot call member function `%D' without object", cand->fn);
4305 return error_mark_node;
4308 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4309 && ((instance == current_class_ref && (dtor_label || ctor_label))
4310 || resolves_to_fixed_type_p (instance, 0)))
4311 flags |= LOOKUP_NONVIRTUAL;
4313 /* Pedantically, normal function declarations are never considered
4314 to refer to template instantiations, so we only do this with
4315 -fguiding-decls. */
4316 if (flag_guiding_decls && templates && ! cand->template
4317 && ! DECL_INITIAL (cand->fn))
4318 add_maybe_template (cand->fn, templates);
4320 return build_over_call
4321 (cand,
4322 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
4323 flags);
4326 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4327 subsequence of ICS2. */
4329 static int
4330 is_subseq (ics1, ics2)
4331 tree ics1, ics2;
4333 /* We can assume that a conversion of the same code
4334 between the same types indicates a subsequence since we only get
4335 here if the types we are converting from are the same. */
4337 while (TREE_CODE (ics1) == RVALUE_CONV
4338 || TREE_CODE (ics1) == LVALUE_CONV)
4339 ics1 = TREE_OPERAND (ics1, 0);
4341 while (1)
4343 while (TREE_CODE (ics2) == RVALUE_CONV
4344 || TREE_CODE (ics2) == LVALUE_CONV)
4345 ics2 = TREE_OPERAND (ics2, 0);
4347 if (TREE_CODE (ics2) == USER_CONV
4348 || TREE_CODE (ics2) == AMBIG_CONV
4349 || TREE_CODE (ics2) == IDENTITY_CONV)
4350 /* At this point, ICS1 cannot be a proper subsequence of
4351 ICS2. We can get a USER_CONV when we are comparing the
4352 second standard conversion sequence of two user conversion
4353 sequences. */
4354 return 0;
4356 ics2 = TREE_OPERAND (ics2, 0);
4358 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4359 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4360 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4361 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4362 return 1;
4366 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4367 be any _TYPE nodes. */
4370 is_properly_derived_from (derived, base)
4371 tree derived;
4372 tree base;
4374 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4375 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4376 return 0;
4378 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4379 considers every class derived from itself. */
4380 return (!same_type_p (TYPE_MAIN_VARIANT (derived),
4381 TYPE_MAIN_VARIANT (base))
4382 && DERIVED_FROM_P (base, derived));
4385 /* We build the ICS for an implicit object parameter as a pointer
4386 conversion sequence. However, such a sequence should be compared
4387 as if it were a reference conversion sequence. If ICS is the
4388 implicit conversion sequence for an implicit object parameter,
4389 modify it accordingly. */
4391 static void
4392 maybe_handle_implicit_object (ics)
4393 tree* ics;
4395 if (ICS_THIS_FLAG (*ics))
4397 /* [over.match.funcs]
4399 For non-static member functions, the type of the
4400 implicit object parameter is "reference to cv X"
4401 where X is the class of which the function is a
4402 member and cv is the cv-qualification on the member
4403 function declaration. */
4404 tree t = *ics;
4405 tree reference_type;
4407 /* The `this' parameter is a pointer to a class type. Make the
4408 implict conversion talk about a reference to that same class
4409 type. */
4410 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4411 reference_type = build_reference_type (reference_type);
4413 if (TREE_CODE (t) == QUAL_CONV)
4414 t = TREE_OPERAND (t, 0);
4415 if (TREE_CODE (t) == PTR_CONV)
4416 t = TREE_OPERAND (t, 0);
4417 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4418 t = direct_reference_binding (reference_type, t);
4419 *ics = t;
4423 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
4424 to the type the reference originally referred to, and return 1.
4425 Otherwise, return 0. */
4427 static int
4428 maybe_handle_ref_bind (ics, target_type)
4429 tree* ics;
4430 tree* target_type;
4432 if (TREE_CODE (*ics) == REF_BIND)
4434 *target_type = TREE_TYPE (TREE_TYPE (*ics));
4435 *ics = TREE_OPERAND (*ics, 0);
4436 return 1;
4439 return 0;
4442 /* Compare two implicit conversion sequences according to the rules set out in
4443 [over.ics.rank]. Return values:
4445 1: ics1 is better than ics2
4446 -1: ics2 is better than ics1
4447 0: ics1 and ics2 are indistinguishable */
4449 static int
4450 compare_ics (ics1, ics2)
4451 tree ics1, ics2;
4453 tree from_type1;
4454 tree from_type2;
4455 tree to_type1;
4456 tree to_type2;
4457 tree deref_from_type1 = NULL_TREE;
4458 tree deref_from_type2 = NULL_TREE;
4459 tree deref_to_type1 = NULL_TREE;
4460 tree deref_to_type2 = NULL_TREE;
4462 /* REF_BINDING is non-zero if the result of the conversion sequence
4463 is a reference type. In that case TARGET_TYPE is the
4464 type referred to by the reference. */
4465 int ref_binding1;
4466 int ref_binding2;
4467 tree target_type1;
4468 tree target_type2;
4470 /* Handle implicit object parameters. */
4471 maybe_handle_implicit_object (&ics1);
4472 maybe_handle_implicit_object (&ics2);
4474 /* Handle reference parameters. */
4475 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
4476 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
4478 /* [over.ics.rank]
4480 When comparing the basic forms of implicit conversion sequences (as
4481 defined in _over.best.ics_)
4483 --a standard conversion sequence (_over.ics.scs_) is a better
4484 conversion sequence than a user-defined conversion sequence
4485 or an ellipsis conversion sequence, and
4487 --a user-defined conversion sequence (_over.ics.user_) is a
4488 better conversion sequence than an ellipsis conversion sequence
4489 (_over.ics.ellipsis_). */
4490 if (ICS_RANK (ics1) > ICS_RANK (ics2))
4491 return -1;
4492 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
4493 return 1;
4495 if (ICS_RANK (ics1) == BAD_RANK)
4497 /* Both ICS are bad. We try to make a decision based on what
4498 would have happenned if they'd been good. */
4499 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4500 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4501 return -1;
4502 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4503 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4504 return 1;
4506 /* We couldn't make up our minds; try to figure it out below. */
4509 if (ICS_ELLIPSIS_FLAG (ics1))
4510 /* Both conversions are ellipsis conversions. */
4511 return 0;
4513 /* User-defined conversion sequence U1 is a better conversion sequence
4514 than another user-defined conversion sequence U2 if they contain the
4515 same user-defined conversion operator or constructor and if the sec-
4516 ond standard conversion sequence of U1 is better than the second
4517 standard conversion sequence of U2. */
4519 if (ICS_USER_FLAG (ics1))
4521 tree t1, t2;
4523 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4524 if (TREE_CODE (t1) == AMBIG_CONV)
4525 return 0;
4526 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4527 if (TREE_CODE (t2) == AMBIG_CONV)
4528 return 0;
4530 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4531 return 0;
4533 /* We can just fall through here, after setting up
4534 FROM_TYPE1 and FROM_TYPE2. */
4535 from_type1 = TREE_TYPE (t1);
4536 from_type2 = TREE_TYPE (t2);
4538 else
4540 /* We're dealing with two standard conversion sequences.
4542 [over.ics.rank]
4544 Standard conversion sequence S1 is a better conversion
4545 sequence than standard conversion sequence S2 if
4547 --S1 is a proper subsequence of S2 (comparing the conversion
4548 sequences in the canonical form defined by _over.ics.scs_,
4549 excluding any Lvalue Transformation; the identity
4550 conversion sequence is considered to be a subsequence of
4551 any non-identity conversion sequence */
4553 from_type1 = ics1;
4554 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4555 from_type1 = TREE_OPERAND (from_type1, 0);
4556 from_type1 = TREE_TYPE (from_type1);
4558 from_type2 = ics2;
4559 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4560 from_type2 = TREE_OPERAND (from_type2, 0);
4561 from_type2 = TREE_TYPE (from_type2);
4564 if (same_type_p (from_type1, from_type2))
4566 if (is_subseq (ics1, ics2))
4567 return 1;
4568 if (is_subseq (ics2, ics1))
4569 return -1;
4571 /* Otherwise, one sequence cannot be a subsequence of the other; they
4572 don't start with the same type. This can happen when comparing the
4573 second standard conversion sequence in two user-defined conversion
4574 sequences. */
4576 /* [over.ics.rank]
4578 Or, if not that,
4580 --the rank of S1 is better than the rank of S2 (by the rules
4581 defined below):
4583 Standard conversion sequences are ordered by their ranks: an Exact
4584 Match is a better conversion than a Promotion, which is a better
4585 conversion than a Conversion.
4587 Two conversion sequences with the same rank are indistinguishable
4588 unless one of the following rules applies:
4590 --A conversion that is not a conversion of a pointer, or pointer
4591 to member, to bool is better than another conversion that is such
4592 a conversion.
4594 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4595 so that we do not have to check it explicitly. */
4596 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4597 return 1;
4598 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4599 return -1;
4601 to_type1 = TREE_TYPE (ics1);
4602 to_type2 = TREE_TYPE (ics2);
4604 if (TYPE_PTR_P (from_type1)
4605 && TYPE_PTR_P (from_type2)
4606 && TYPE_PTR_P (to_type1)
4607 && TYPE_PTR_P (to_type2))
4609 deref_from_type1 = TREE_TYPE (from_type1);
4610 deref_from_type2 = TREE_TYPE (from_type2);
4611 deref_to_type1 = TREE_TYPE (to_type1);
4612 deref_to_type2 = TREE_TYPE (to_type2);
4614 /* The rules for pointers to members A::* are just like the rules
4615 for pointers A*, except opposite: if B is derived from A then
4616 A::* converts to B::*, not vice versa. For that reason, we
4617 switch the from_ and to_ variables here. */
4618 else if (TYPE_PTRMEM_P (from_type1)
4619 && TYPE_PTRMEM_P (from_type2)
4620 && TYPE_PTRMEM_P (to_type1)
4621 && TYPE_PTRMEM_P (to_type2))
4623 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4624 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4625 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4626 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4628 else if (TYPE_PTRMEMFUNC_P (from_type1)
4629 && TYPE_PTRMEMFUNC_P (from_type2)
4630 && TYPE_PTRMEMFUNC_P (to_type1)
4631 && TYPE_PTRMEMFUNC_P (to_type2))
4633 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4634 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4635 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4636 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4639 if (deref_from_type1 != NULL_TREE
4640 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4641 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4643 /* This was one of the pointer or pointer-like conversions.
4645 [over.ics.rank]
4647 --If class B is derived directly or indirectly from class A,
4648 conversion of B* to A* is better than conversion of B* to
4649 void*, and conversion of A* to void* is better than
4650 conversion of B* to void*. */
4651 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4652 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4654 if (is_properly_derived_from (deref_from_type1,
4655 deref_from_type2))
4656 return -1;
4657 else if (is_properly_derived_from (deref_from_type2,
4658 deref_from_type1))
4659 return 1;
4661 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4662 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4664 if (same_type_p (deref_from_type1, deref_from_type2))
4666 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4668 if (is_properly_derived_from (deref_from_type1,
4669 deref_to_type1))
4670 return 1;
4672 /* We know that DEREF_TO_TYPE1 is `void' here. */
4673 else if (is_properly_derived_from (deref_from_type1,
4674 deref_to_type2))
4675 return -1;
4678 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4679 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4681 /* [over.ics.rank]
4683 --If class B is derived directly or indirectly from class A
4684 and class C is derived directly or indirectly from B,
4686 --conversion of C* to B* is better than conversion of C* to
4687 A*,
4689 --conversion of B* to A* is better than conversion of C* to
4690 A* */
4691 if (same_type_p (deref_from_type1, deref_from_type2))
4693 if (is_properly_derived_from (deref_to_type1,
4694 deref_to_type2))
4695 return 1;
4696 else if (is_properly_derived_from (deref_to_type2,
4697 deref_to_type1))
4698 return -1;
4700 else if (same_type_p (deref_to_type1, deref_to_type2))
4702 if (is_properly_derived_from (deref_from_type2,
4703 deref_from_type1))
4704 return 1;
4705 else if (is_properly_derived_from (deref_from_type1,
4706 deref_from_type2))
4707 return -1;
4711 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4712 && same_type_p (from_type1, from_type2))
4714 /* [over.ics.rank]
4716 --binding of an expression of type C to a reference of type
4717 B& is better than binding an expression of type C to a
4718 reference of type A&
4720 --conversion of C to B is better than conversion of C to A, */
4721 if (is_properly_derived_from (from_type1, to_type1)
4722 && is_properly_derived_from (from_type1, to_type2))
4724 if (is_properly_derived_from (to_type1, to_type2))
4725 return 1;
4726 else if (is_properly_derived_from (to_type2, to_type1))
4727 return -1;
4730 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4731 && same_type_p (to_type1, to_type2))
4733 /* [over.ics.rank]
4735 --binding of an expression of type B to a reference of type
4736 A& is better than binding an expression of type C to a
4737 reference of type A&,
4739 --onversion of B to A is better than conversion of C to A */
4740 if (is_properly_derived_from (from_type1, to_type1)
4741 && is_properly_derived_from (from_type2, to_type1))
4743 if (is_properly_derived_from (from_type2, from_type1))
4744 return 1;
4745 else if (is_properly_derived_from (from_type1, from_type2))
4746 return -1;
4750 /* [over.ics.rank]
4752 --S1 and S2 differ only in their qualification conversion and yield
4753 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4754 qualification signature of type T1 is a proper subset of the cv-
4755 qualification signature of type T2 */
4756 if (TREE_CODE (ics1) == QUAL_CONV
4757 && TREE_CODE (ics2) == QUAL_CONV
4758 && same_type_p (from_type1, from_type2))
4759 return comp_cv_qual_signature (to_type1, to_type2);
4761 /* [over.ics.rank]
4763 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4764 types to which the references refer are the same type except for
4765 top-level cv-qualifiers, and the type to which the reference
4766 initialized by S2 refers is more cv-qualified than the type to
4767 which the reference initialized by S1 refers */
4769 if (ref_binding1 && ref_binding2
4770 && same_type_p (TYPE_MAIN_VARIANT (to_type1),
4771 TYPE_MAIN_VARIANT (to_type2)))
4772 return comp_cv_qualification (target_type2, target_type1);
4774 /* Neither conversion sequence is better than the other. */
4775 return 0;
4778 /* The source type for this standard conversion sequence. */
4780 static tree
4781 source_type (t)
4782 tree t;
4784 for (;; t = TREE_OPERAND (t, 0))
4786 if (TREE_CODE (t) == USER_CONV
4787 || TREE_CODE (t) == AMBIG_CONV
4788 || TREE_CODE (t) == IDENTITY_CONV)
4789 return TREE_TYPE (t);
4791 my_friendly_abort (1823);
4794 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4795 a pointer to LOSER and re-running joust to produce the warning if WINNER
4796 is actually used. */
4798 static void
4799 add_warning (winner, loser)
4800 struct z_candidate *winner, *loser;
4802 winner->warnings = tree_cons (NULL_PTR,
4803 build_expr_ptr_wrapper (loser),
4804 winner->warnings);
4807 /* Returns true iff functions are equivalent. Equivalent functions are
4808 not identical only if one is a function-local extern function.
4809 This assumes that function-locals don't have TREE_PERMANENT. */
4811 static inline int
4812 equal_functions (fn1, fn2)
4813 tree fn1;
4814 tree fn2;
4816 if (!TREE_PERMANENT (fn1) || !TREE_PERMANENT (fn2))
4817 return decls_match (fn1, fn2);
4818 return fn1 == fn2;
4821 /* Compare two candidates for overloading as described in
4822 [over.match.best]. Return values:
4824 1: cand1 is better than cand2
4825 -1: cand2 is better than cand1
4826 0: cand1 and cand2 are indistinguishable */
4828 static int
4829 joust (cand1, cand2, warn)
4830 struct z_candidate *cand1, *cand2;
4831 int warn;
4833 int winner = 0;
4834 int i, off1 = 0, off2 = 0, len;
4836 /* Candidates that involve bad conversions are always worse than those
4837 that don't. */
4838 if (cand1->viable > cand2->viable)
4839 return 1;
4840 if (cand1->viable < cand2->viable)
4841 return -1;
4843 /* If we have two pseudo-candidates for conversions to the same type,
4844 arbitrarily pick one. */
4845 if (TYPE_P (cand1->fn) && cand1->fn == cand2->fn)
4846 return 1;
4848 /* a viable function F1
4849 is defined to be a better function than another viable function F2 if
4850 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4851 ICSi(F2), and then */
4853 /* for some argument j, ICSj(F1) is a better conversion sequence than
4854 ICSj(F2) */
4856 /* For comparing static and non-static member functions, we ignore the
4857 implicit object parameter of the non-static function. The WP says to
4858 pretend that the static function has an object parm, but that won't
4859 work with operator overloading. */
4860 len = TREE_VEC_LENGTH (cand1->convs);
4861 if (len != TREE_VEC_LENGTH (cand2->convs))
4863 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4864 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4865 off2 = 1;
4866 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4867 && DECL_STATIC_FUNCTION_P (cand2->fn))
4869 off1 = 1;
4870 --len;
4872 else
4873 my_friendly_abort (42);
4876 for (i = 0; i < len; ++i)
4878 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4879 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4880 int comp = compare_ics (t1, t2);
4882 if (comp != 0)
4884 if (warn_sign_promo
4885 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4886 && TREE_CODE (t1) == STD_CONV
4887 && TREE_CODE (t2) == STD_CONV
4888 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4889 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4890 && (TYPE_PRECISION (TREE_TYPE (t1))
4891 == TYPE_PRECISION (TREE_TYPE (t2)))
4892 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4893 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4894 == ENUMERAL_TYPE)))
4896 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4897 tree type1, type2;
4898 struct z_candidate *w, *l;
4899 if (comp > 0)
4900 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4901 w = cand1, l = cand2;
4902 else
4903 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4904 w = cand2, l = cand1;
4906 if (warn)
4908 cp_warning ("passing `%T' chooses `%T' over `%T'",
4909 type, type1, type2);
4910 cp_warning (" in call to `%D'", w->fn);
4912 else
4913 add_warning (w, l);
4916 if (winner && comp != winner)
4918 winner = 0;
4919 goto tweak;
4921 winner = comp;
4925 /* warn about confusing overload resolution for user-defined conversions,
4926 either between a constructor and a conversion op, or between two
4927 conversion ops. */
4928 if (winner && cand1->second_conv
4929 && ((DECL_CONSTRUCTOR_P (cand1->fn)
4930 != DECL_CONSTRUCTOR_P (cand2->fn))
4931 /* Don't warn if the two conv ops convert to the same type... */
4932 || (! DECL_CONSTRUCTOR_P (cand1->fn)
4933 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
4934 TREE_TYPE (TREE_TYPE (cand2->fn))))))
4936 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4937 if (comp != winner)
4939 struct z_candidate *w, *l;
4940 if (winner == 1)
4941 w = cand1, l = cand2;
4942 else
4943 w = cand2, l = cand1;
4944 if (warn)
4946 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4947 if (! DECL_CONSTRUCTOR_P (w->fn))
4948 source = TREE_TYPE (source);
4949 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4950 cp_warning (" for conversion from `%T' to `%T'",
4951 source, TREE_TYPE (w->second_conv));
4952 cp_warning (" because conversion sequence for the argument is better");
4954 else
4955 add_warning (w, l);
4959 if (winner)
4960 return winner;
4962 /* or, if not that,
4963 F1 is a non-template function and F2 is a template function */
4965 if (! cand1->template && cand2->template)
4966 return 1;
4967 else if (cand1->template && ! cand2->template)
4968 return -1;
4969 else if (cand1->template && cand2->template)
4970 winner = more_specialized
4971 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4972 NULL_TREE);
4974 /* or, if not that,
4975 the context is an initialization by user-defined conversion (see
4976 _dcl.init_ and _over.match.user_) and the standard conversion
4977 sequence from the return type of F1 to the destination type (i.e.,
4978 the type of the entity being initialized) is a better conversion
4979 sequence than the standard conversion sequence from the return type
4980 of F2 to the destination type. */
4982 if (! winner && cand1->second_conv)
4983 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4985 /* If the built-in candidates are the same, arbitrarily pick one. */
4986 if (! winner && cand1->fn == cand2->fn
4987 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4989 for (i = 0; i < len; ++i)
4990 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4991 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
4992 break;
4993 if (i == TREE_VEC_LENGTH (cand1->convs))
4994 return 1;
4996 /* Kludge around broken overloading rules whereby
4997 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4998 that takes references and another that takes values. */
4999 if (cand1->fn == ansi_opname[COND_EXPR])
5001 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
5002 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
5003 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
5004 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
5006 if (same_type_p (t1, t2))
5008 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
5009 return 1;
5010 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
5011 return -1;
5016 /* If the two functions are the same (this can happen with declarations
5017 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5018 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5019 && equal_functions (cand1->fn, cand2->fn))
5020 return 1;
5022 tweak:
5024 /* Extension: If the worst conversion for one candidate is worse than the
5025 worst conversion for the other, take the first. */
5026 if (! winner && ! pedantic)
5028 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5030 for (i = 0; i < len; ++i)
5032 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5033 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5034 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5035 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5038 if (rank1 < rank2)
5039 return 1;
5040 if (rank1 > rank2)
5041 return -1;
5044 return winner;
5047 /* Given a list of candidates for overloading, find the best one, if any.
5048 This algorithm has a worst case of O(2n) (winner is last), and a best
5049 case of O(n/2) (totally ambiguous); much better than a sorting
5050 algorithm. */
5052 static struct z_candidate *
5053 tourney (candidates)
5054 struct z_candidate *candidates;
5056 struct z_candidate *champ = candidates, *challenger;
5057 int fate;
5058 int champ_compared_to_predecessor = 0;
5060 /* Walk through the list once, comparing each current champ to the next
5061 candidate, knocking out a candidate or two with each comparison. */
5063 for (challenger = champ->next; challenger; )
5065 fate = joust (champ, challenger, 0);
5066 if (fate == 1)
5067 challenger = challenger->next;
5068 else
5070 if (fate == 0)
5072 champ = challenger->next;
5073 if (champ == 0)
5074 return 0;
5075 champ_compared_to_predecessor = 0;
5077 else
5079 champ = challenger;
5080 champ_compared_to_predecessor = 1;
5083 challenger = champ->next;
5087 /* Make sure the champ is better than all the candidates it hasn't yet
5088 been compared to. */
5090 for (challenger = candidates;
5091 challenger != champ
5092 && !(champ_compared_to_predecessor && challenger->next == champ);
5093 challenger = challenger->next)
5095 fate = joust (champ, challenger, 0);
5096 if (fate != 1)
5097 return 0;
5100 return champ;
5103 /* Returns non-zero if things of type FROM can be converted to TO. */
5106 can_convert (to, from)
5107 tree to, from;
5109 return can_convert_arg (to, from, NULL_TREE);
5112 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5115 can_convert_arg (to, from, arg)
5116 tree to, from, arg;
5118 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5119 return (t && ! ICS_BAD_FLAG (t));
5122 /* Convert EXPR to TYPE. Return the converted expression. */
5124 tree
5125 perform_implicit_conversion (type, expr)
5126 tree type;
5127 tree expr;
5129 tree conv;
5131 if (expr == error_mark_node)
5132 return error_mark_node;
5133 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5134 LOOKUP_NORMAL);
5135 if (!conv || ICS_BAD_FLAG (conv))
5137 cp_error ("could not convert `%E' to `%T'", expr, type);
5138 return error_mark_node;
5141 return convert_like (conv, expr);
5144 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5145 initializing a variable of that TYPE. Return the converted
5146 expression. */
5148 tree
5149 initialize_reference (type, expr)
5150 tree type;
5151 tree expr;
5153 tree conv;
5155 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5156 if (!conv || ICS_BAD_FLAG (conv))
5158 cp_error ("could not convert `%E' to `%T'", expr, type);
5159 return error_mark_node;
5162 return convert_like (conv, expr);