* cp-tree.h (make_temp_vec): Remove.
[official-gcc.git] / gcc / cp / call.c
blob38ce371d93a59a5d3d9ef864b850f8058de4fbe3
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 (TYPE_VFIELD (type)),
116 datum, TYPE_VFIELD (type));
117 else
118 rval = build_component_ref (datum, DECL_NAME (TYPE_VFIELD (type)), NULL_TREE, 0);
120 return rval;
123 /* Build a call to a member of an object. I.e., one that overloads
124 operator ()(), or is a pointer-to-function or pointer-to-method. */
126 static tree
127 build_field_call (basetype_path, instance_ptr, name, parms)
128 tree basetype_path, instance_ptr, name, parms;
130 tree field, instance;
132 if (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 = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
399 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
400 TREE_VALUE (tmp), t);
403 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
404 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
405 TREE_TYPE (function) = result_type;
406 TREE_SIDE_EFFECTS (function) = 1;
408 return function;
411 /* Build something of the form ptr->method (args)
412 or object.method (args). This can also build
413 calls to constructors, and find friends.
415 Member functions always take their class variable
416 as a pointer.
418 INSTANCE is a class instance.
420 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
422 PARMS help to figure out what that NAME really refers to.
424 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
425 down to the real instance type to use for access checking. We need this
426 information to get protected accesses correct. This parameter is used
427 by build_member_call.
429 FLAGS is the logical disjunction of zero or more LOOKUP_
430 flags. See cp-tree.h for more info.
432 If this is all OK, calls build_function_call with the resolved
433 member function.
435 This function must also handle being called to perform
436 initialization, promotion/coercion of arguments, and
437 instantiation of default parameters.
439 Note that NAME may refer to an instance variable name. If
440 `operator()()' is defined for the type of that field, then we return
441 that result. */
443 tree
444 build_method_call (instance, name, parms, basetype_path, flags)
445 tree instance, name, parms, basetype_path;
446 int flags;
448 tree basetype, instance_ptr;
450 #ifdef GATHER_STATISTICS
451 n_build_method_call++;
452 #endif
454 if (instance == error_mark_node
455 || name == error_mark_node
456 || parms == error_mark_node
457 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
458 return error_mark_node;
460 if (processing_template_decl)
462 /* We need to process template parm names here so that tsubst catches
463 them properly. Other type names can wait. */
464 if (TREE_CODE (name) == BIT_NOT_EXPR)
466 tree type = NULL_TREE;
468 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
469 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
470 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
471 type = TREE_TYPE (TREE_OPERAND (name, 0));
473 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
474 name = build_min_nt (BIT_NOT_EXPR, type);
477 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
480 if (TREE_CODE (name) == BIT_NOT_EXPR)
482 if (parms)
483 error ("destructors take no parameters");
484 basetype = TREE_TYPE (instance);
485 if (TREE_CODE (basetype) == REFERENCE_TYPE)
486 basetype = TREE_TYPE (basetype);
488 if (! check_dtor_name (basetype, name))
489 cp_error
490 ("destructor name `~%T' does not match type `%T' of expression",
491 TREE_OPERAND (name, 0), basetype);
493 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
494 return cp_convert (void_type_node, instance);
495 instance = default_conversion (instance);
496 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
497 return build_delete (build_pointer_type (basetype),
498 instance_ptr, integer_two_node,
499 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
502 return build_new_method_call (instance, name, parms, basetype_path, flags);
505 /* New overloading code. */
507 struct z_candidate {
508 tree fn;
509 tree convs;
510 tree second_conv;
511 int viable;
512 tree basetype_path;
513 tree template;
514 tree warnings;
515 struct z_candidate *next;
518 #define IDENTITY_RANK 0
519 #define EXACT_RANK 1
520 #define PROMO_RANK 2
521 #define STD_RANK 3
522 #define PBOOL_RANK 4
523 #define USER_RANK 5
524 #define ELLIPSIS_RANK 6
525 #define BAD_RANK 7
527 #define ICS_RANK(NODE) \
528 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
529 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
530 : ICS_USER_FLAG (NODE) ? USER_RANK \
531 : ICS_STD_RANK (NODE))
533 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
535 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
536 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
537 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
538 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
540 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
541 should be created to hold the result of the conversion. */
542 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
544 #define USER_CONV_CAND(NODE) \
545 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
546 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
549 null_ptr_cst_p (t)
550 tree t;
552 /* [conv.ptr]
554 A null pointer constant is an integral constant expression
555 (_expr.const_) rvalue of integer type that evaluates to zero. */
556 if (t == null_node
557 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
558 return 1;
559 return 0;
562 static tree
563 build_conv (code, type, from)
564 enum tree_code code;
565 tree type, from;
567 tree t = build1 (code, type, from);
568 int rank = ICS_STD_RANK (from);
569 switch (code)
571 case PTR_CONV:
572 case PMEM_CONV:
573 case BASE_CONV:
574 case STD_CONV:
575 if (rank < STD_RANK)
576 rank = STD_RANK;
577 break;
579 case QUAL_CONV:
580 if (rank < EXACT_RANK)
581 rank = EXACT_RANK;
583 default:
584 break;
586 ICS_STD_RANK (t) = rank;
587 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
588 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
589 return t;
592 static tree
593 non_reference (t)
594 tree t;
596 if (TREE_CODE (t) == REFERENCE_TYPE)
597 t = TREE_TYPE (t);
598 return t;
601 tree
602 strip_top_quals (t)
603 tree t;
605 if (TREE_CODE (t) == ARRAY_TYPE)
606 return t;
607 return TYPE_MAIN_VARIANT (t);
610 /* Returns the standard conversion path (see [conv]) from type FROM to type
611 TO, if any. For proper handling of null pointer constants, you must
612 also pass the expression EXPR to convert from. */
614 static tree
615 standard_conversion (to, from, expr)
616 tree to, from, expr;
618 enum tree_code fcode, tcode;
619 tree conv;
620 int fromref = 0;
622 if (TREE_CODE (to) == REFERENCE_TYPE)
623 to = TREE_TYPE (to);
624 if (TREE_CODE (from) == REFERENCE_TYPE)
626 fromref = 1;
627 from = TREE_TYPE (from);
629 to = strip_top_quals (to);
630 from = strip_top_quals (from);
632 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
633 && expr && type_unknown_p (expr))
635 expr = instantiate_type (to, expr, 0);
636 if (expr == error_mark_node)
637 return NULL_TREE;
638 from = TREE_TYPE (expr);
641 fcode = TREE_CODE (from);
642 tcode = TREE_CODE (to);
644 conv = build1 (IDENTITY_CONV, from, expr);
646 if (fcode == FUNCTION_TYPE)
648 from = build_pointer_type (from);
649 fcode = TREE_CODE (from);
650 conv = build_conv (LVALUE_CONV, from, conv);
652 else if (fcode == ARRAY_TYPE)
654 from = build_pointer_type (TREE_TYPE (from));
655 fcode = TREE_CODE (from);
656 conv = build_conv (LVALUE_CONV, from, conv);
658 else if (fromref || (expr && real_lvalue_p (expr)))
659 conv = build_conv (RVALUE_CONV, from, conv);
661 if (same_type_p (from, to))
662 return conv;
664 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
665 && expr && null_ptr_cst_p (expr))
667 conv = build_conv (STD_CONV, to, conv);
669 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
671 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
672 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
674 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
675 TYPE_MAIN_VARIANT (TREE_TYPE (to))))
677 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
678 && ufcode != FUNCTION_TYPE)
680 from = build_pointer_type
681 (cp_build_qualified_type (void_type_node,
682 CP_TYPE_QUALS (TREE_TYPE (from))));
683 conv = build_conv (PTR_CONV, from, conv);
685 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
687 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
688 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
690 if (DERIVED_FROM_P (fbase, tbase)
691 && (same_type_p
692 (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
693 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))))))
695 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
696 from = build_pointer_type (from);
697 conv = build_conv (PMEM_CONV, from, conv);
700 else if (IS_AGGR_TYPE (TREE_TYPE (from))
701 && IS_AGGR_TYPE (TREE_TYPE (to)))
703 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
705 from =
706 cp_build_qualified_type (TREE_TYPE (to),
707 CP_TYPE_QUALS (TREE_TYPE (from)));
708 from = build_pointer_type (from);
709 conv = build_conv (PTR_CONV, from, conv);
713 if (same_type_p (from, to))
714 /* OK */;
715 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
716 conv = build_conv (QUAL_CONV, to, conv);
717 else if (expr && string_conv_p (to, expr, 0))
718 /* converting from string constant to char *. */
719 conv = build_conv (QUAL_CONV, to, conv);
720 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
722 conv = build_conv (PTR_CONV, to, conv);
723 ICS_BAD_FLAG (conv) = 1;
725 else
726 return 0;
728 from = to;
730 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
732 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
733 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
734 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
735 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
737 if (! DERIVED_FROM_P (fbase, tbase)
738 || ! same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
739 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
740 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
741 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
742 return 0;
744 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
745 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
746 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
747 from = build_ptrmemfunc_type (build_pointer_type (from));
748 conv = build_conv (PMEM_CONV, from, conv);
750 else if (tcode == BOOLEAN_TYPE)
752 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
753 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
754 return 0;
756 conv = build_conv (STD_CONV, to, conv);
757 if (fcode == POINTER_TYPE
758 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
759 ICS_STD_RANK (conv) = PBOOL_RANK;
761 /* We don't check for ENUMERAL_TYPE here because there are no standard
762 conversions to enum type. */
763 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
764 || tcode == REAL_TYPE)
766 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
767 return 0;
768 conv = build_conv (STD_CONV, to, conv);
770 /* Give this a better rank if it's a promotion. */
771 if (to == type_promotes_to (from)
772 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
773 ICS_STD_RANK (conv) = PROMO_RANK;
775 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
776 && is_properly_derived_from (from, to))
778 if (TREE_CODE (conv) == RVALUE_CONV)
779 conv = TREE_OPERAND (conv, 0);
780 conv = build_conv (BASE_CONV, to, conv);
781 /* The derived-to-base conversion indicates the initialization
782 of a parameter with base type from an object of a derived
783 type. A temporary object is created to hold the result of
784 the conversion. */
785 NEED_TEMPORARY_P (conv) = 1;
787 else
788 return 0;
790 return conv;
793 /* Returns non-zero if T1 is reference-related to T2. */
795 static int
796 reference_related_p (t1, t2)
797 tree t1;
798 tree t2;
800 t1 = TYPE_MAIN_VARIANT (t1);
801 t2 = TYPE_MAIN_VARIANT (t2);
803 /* [dcl.init.ref]
805 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
806 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
807 of T2. */
808 return (same_type_p (t1, t2)
809 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
810 && DERIVED_FROM_P (t1, t2)));
813 /* Returns non-zero if T1 is reference-compatible with T2. */
815 static int
816 reference_compatible_p (t1, t2)
817 tree t1;
818 tree t2;
820 /* [dcl.init.ref]
822 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
823 reference-related to T2 and cv1 is the same cv-qualification as,
824 or greater cv-qualification than, cv2. */
825 return (reference_related_p (t1, t2)
826 && at_least_as_qualified_p (t1, t2));
829 /* Determine whether or not the EXPR (of class type S) can be
830 converted to T as in [over.match.ref]. */
832 static tree
833 convert_class_to_reference (t, s, expr)
834 tree t;
835 tree s;
836 tree expr;
838 tree conversions;
839 tree arglist;
840 tree conv;
841 struct z_candidate *candidates;
842 struct z_candidate *cand;
844 /* [over.match.ref]
846 Assuming that "cv1 T" is the underlying type of the reference
847 being initialized, and "cv S" is the type of the initializer
848 expression, with S a class type, the candidate functions are
849 selected as follows:
851 --The conversion functions of S and its base classes are
852 considered. Those that are not hidden within S and yield type
853 "reference to cv2 T2", where "cv1 T" is reference-compatible
854 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
856 The argument list has one argument, which is the initializer
857 expression. */
859 candidates = 0;
861 /* Conceptually, we should take the address of EXPR and put it in
862 the argument list. Unfortunately, however, that can result in
863 error messages, which we should not issue now because we are just
864 trying to find a conversion operator. Therefore, we use NULL,
865 cast to the appropriate type. */
866 arglist = build_int_2 (0, 0);
867 TREE_TYPE (arglist) = build_pointer_type (s);
868 arglist = build_scratch_list (NULL_TREE, arglist);
870 for (conversions = lookup_conversions (s);
871 conversions;
872 conversions = TREE_CHAIN (conversions))
874 tree fns = TREE_VALUE (conversions);
876 while (fns)
878 tree f = OVL_CURRENT (fns);
879 tree t2 = TREE_TYPE (TREE_TYPE (f));
880 struct z_candidate *old_candidates = candidates;
882 /* If this is a template function, try to get an exact
883 match. */
884 if (TREE_CODE (f) == TEMPLATE_DECL)
886 candidates
887 = add_template_candidate (candidates,
889 NULL_TREE,
890 arglist,
891 build_reference_type (t),
892 LOOKUP_NORMAL,
893 DEDUCE_CONV);
895 if (candidates != old_candidates)
897 /* Now, see if the conversion function really returns
898 an lvalue of the appropriate type. From the
899 point of view of unification, simply returning an
900 rvalue of the right type is good enough. */
901 f = candidates->fn;
902 t2 = TREE_TYPE (TREE_TYPE (f));
903 if (TREE_CODE (t2) != REFERENCE_TYPE
904 || !reference_compatible_p (t, TREE_TYPE (t2)))
905 candidates = candidates->next;
908 else if (TREE_CODE (t2) == REFERENCE_TYPE
909 && reference_compatible_p (t, TREE_TYPE (t2)))
910 candidates
911 = add_function_candidate (candidates, f, arglist,
912 LOOKUP_NORMAL);
914 if (candidates != old_candidates)
915 candidates->basetype_path = TREE_PURPOSE (conversions);
917 fns = OVL_NEXT (fns);
921 /* If none of the conversion functions worked out, let our caller
922 know. */
923 if (!any_viable (candidates))
924 return NULL_TREE;
926 candidates = splice_viable (candidates);
927 cand = tourney (candidates);
928 if (!cand)
929 return NULL_TREE;
931 conv = build_conv (IDENTITY_CONV, s, expr);
932 conv = build_conv (USER_CONV,
933 non_reference (TREE_TYPE (TREE_TYPE (cand->fn))),
934 expr);
935 TREE_OPERAND (conv, 1) = build_expr_ptr_wrapper (cand);
936 ICS_USER_FLAG (conv) = 1;
937 if (cand->viable == -1)
938 ICS_BAD_FLAG (conv) = 1;
939 cand->second_conv = conv;
941 return conv;
944 /* A reference of the indicated TYPE is being bound directly to the
945 expression represented by the implicit conversion sequence CONV.
946 Return a conversion sequence for this binding. */
948 static tree
949 direct_reference_binding (type, conv)
950 tree type;
951 tree conv;
953 tree t = TREE_TYPE (type);
955 /* [over.ics.rank]
957 When a parameter of reference type binds directly
958 (_dcl.init.ref_) to an argument expression, the implicit
959 conversion sequence is the identity conversion, unless the
960 argument expression has a type that is a derived class of the
961 parameter type, in which case the implicit conversion sequence is
962 a derived-to-base Conversion.
964 If the parameter binds directly to the result of applying a
965 conversion function to the argument expression, the implicit
966 conversion sequence is a user-defined conversion sequence
967 (_over.ics.user_), with the second standard conversion sequence
968 either an identity conversion or, if the conversion function
969 returns an entity of a type that is a derived class of the
970 parameter type, a derived-to-base conversion. */
971 if (!same_type_p (TYPE_MAIN_VARIANT (t),
972 TYPE_MAIN_VARIANT (TREE_TYPE (conv))))
974 /* Represent the derived-to-base conversion. */
975 conv = build_conv (BASE_CONV, t, conv);
976 /* We will actually be binding to the base-class subobject in
977 the derived class, so we mark this conversion appropriately.
978 That way, convert_like knows not to generate a temporary. */
979 NEED_TEMPORARY_P (conv) = 0;
981 return build_conv (REF_BIND, type, conv);
984 /* Returns the conversion path from type FROM to reference type TO for
985 purposes of reference binding. For lvalue binding, either pass a
986 reference type to FROM or an lvalue expression to EXPR. If the
987 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
988 the conversion returned. */
990 static tree
991 reference_binding (rto, rfrom, expr, flags)
992 tree rto, rfrom, expr;
993 int flags;
995 tree conv = NULL_TREE;
996 tree to = TREE_TYPE (rto);
997 tree from = rfrom;
998 int related_p;
999 int compatible_p;
1000 cp_lvalue_kind lvalue_p = clk_none;
1002 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1004 expr = instantiate_type (to, expr, 0);
1005 if (expr == error_mark_node)
1006 return NULL_TREE;
1007 from = TREE_TYPE (expr);
1010 if (TREE_CODE (from) == REFERENCE_TYPE)
1012 /* Anything with reference type is an lvalue. */
1013 lvalue_p = clk_ordinary;
1014 from = TREE_TYPE (from);
1016 else if (expr)
1017 lvalue_p = real_lvalue_p (expr);
1019 /* Figure out whether or not the types are reference-related and
1020 reference compatible. We have do do this after stripping
1021 references from FROM. */
1022 related_p = reference_related_p (to, from);
1023 compatible_p = reference_compatible_p (to, from);
1025 if (lvalue_p && compatible_p)
1027 /* [dcl.init.ref]
1029 If the intializer expression
1031 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1032 is reference-compatible with "cv2 T2,"
1034 the reference is bound directly to the initializer exprssion
1035 lvalue. */
1036 conv = build1 (IDENTITY_CONV, from, expr);
1037 conv = direct_reference_binding (rto, conv);
1038 if ((lvalue_p & clk_bitfield) != 0
1039 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1040 /* For the purposes of overload resolution, we ignore the fact
1041 this expression is a bitfield. (In particular,
1042 [over.ics.ref] says specifically that a function with a
1043 non-const reference parameter is viable even if the
1044 argument is a bitfield.)
1046 However, when we actually call the function we must create
1047 a temporary to which to bind the reference. If the
1048 reference is volatile, or isn't const, then we cannot make
1049 a temporary, so we just issue an error when the conversion
1050 actually occurs. */
1051 NEED_TEMPORARY_P (conv) = 1;
1052 return conv;
1054 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1056 /* [dcl.init.ref]
1058 If the initializer exprsesion
1060 -- has a class type (i.e., T2 is a class type) can be
1061 implicitly converted to an lvalue of type "cv3 T3," where
1062 "cv1 T1" is reference-compatible with "cv3 T3". (this
1063 conversion is selected by enumerating the applicable
1064 conversion functions (_over.match.ref_) and choosing the
1065 best one through overload resolution. (_over.match_).
1067 the reference is bound to the lvalue result of the conversion
1068 in the second case. */
1069 conv = convert_class_to_reference (to, from, expr);
1070 if (conv)
1071 return direct_reference_binding (rto, conv);
1074 /* From this point on, we conceptually need temporaries, even if we
1075 elide them. Only the cases above are "direct bindings". */
1076 if (flags & LOOKUP_NO_TEMP_BIND)
1077 return NULL_TREE;
1079 /* [over.ics.rank]
1081 When a parameter of reference type is not bound directly to an
1082 argument expression, the conversion sequence is the one required
1083 to convert the argument expression to the underlying type of the
1084 reference according to _over.best.ics_. Conceptually, this
1085 conversion sequence corresponds to copy-initializing a temporary
1086 of the underlying type with the argument expression. Any
1087 difference in top-level cv-qualification is subsumed by the
1088 initialization itself and does not constitute a conversion. */
1090 /* [dcl.init.ref]
1092 Otherwise, the reference shall be to a non-volatile const type. */
1093 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1094 return NULL_TREE;
1096 /* [dcl.init.ref]
1098 If the initializer expression is an rvalue, with T2 a class type,
1099 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1100 is bound in one of the following ways:
1102 -- The reference is bound to the object represented by the rvalue
1103 or to a sub-object within that object.
1105 In this case, the implicit conversion sequence is supposed to be
1106 same as we would obtain by generating a temporary. Fortunately,
1107 if the types are reference compatible, then this is either an
1108 identity conversion or the derived-to-base conversion, just as
1109 for direct binding. */
1110 if (CLASS_TYPE_P (from) && compatible_p)
1112 conv = build1 (IDENTITY_CONV, from, expr);
1113 return direct_reference_binding (rto, conv);
1116 /* [dcl.init.ref]
1118 Otherwise, a temporary of type "cv1 T1" is created and
1119 initialized from the initializer expression using the rules for a
1120 non-reference copy initialization. If T1 is reference-related to
1121 T2, cv1 must be the same cv-qualification as, or greater
1122 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1123 if (related_p && !at_least_as_qualified_p (to, from))
1124 return NULL_TREE;
1126 conv = implicit_conversion (to, from, expr, flags);
1127 if (!conv)
1128 return NULL_TREE;
1130 conv = build_conv (REF_BIND, rto, conv);
1131 /* This reference binding, unlike those above, requires the
1132 creation of a temporary. */
1133 NEED_TEMPORARY_P (conv) = 1;
1135 return conv;
1138 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1139 to type TO. The optional expression EXPR may affect the conversion.
1140 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1141 significant. */
1143 static tree
1144 implicit_conversion (to, from, expr, flags)
1145 tree to, from, expr;
1146 int flags;
1148 tree conv;
1149 struct z_candidate *cand;
1151 /* Resolve expressions like `A::p' that we thought might become
1152 pointers-to-members. */
1153 if (expr && TREE_CODE (expr) == OFFSET_REF)
1155 expr = resolve_offset_ref (expr);
1156 from = TREE_TYPE (expr);
1159 if (from == error_mark_node || to == error_mark_node
1160 || expr == error_mark_node)
1161 return NULL_TREE;
1163 /* Make sure both the FROM and TO types are complete so that
1164 user-defined conversions are available. */
1165 complete_type (from);
1166 complete_type (to);
1168 if (TREE_CODE (to) == REFERENCE_TYPE)
1169 conv = reference_binding (to, from, expr, flags);
1170 else
1171 conv = standard_conversion (to, from, expr);
1173 if (conv)
1175 else if (expr != NULL_TREE
1176 && (IS_AGGR_TYPE (non_reference (from))
1177 || IS_AGGR_TYPE (non_reference (to)))
1178 && (flags & LOOKUP_NO_CONVERSION) == 0)
1180 cand = build_user_type_conversion_1
1181 (to, expr, LOOKUP_ONLYCONVERTING);
1182 if (cand)
1183 conv = cand->second_conv;
1184 if ((! conv || ICS_BAD_FLAG (conv))
1185 && TREE_CODE (to) == REFERENCE_TYPE
1186 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1188 cand = build_user_type_conversion_1
1189 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1190 if (cand)
1192 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to)))
1193 ICS_BAD_FLAG (cand->second_conv) = 1;
1194 if (!conv || (ICS_BAD_FLAG (conv)
1195 > ICS_BAD_FLAG (cand->second_conv)))
1196 conv = build_conv (REF_BIND, to, cand->second_conv);
1201 return conv;
1204 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1205 functions. */
1207 static struct z_candidate *
1208 add_candidate (candidates, fn, convs, viable)
1209 struct z_candidate *candidates;
1210 tree fn, convs;
1211 int viable;
1213 struct z_candidate *cand
1214 = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
1216 cand->fn = fn;
1217 cand->convs = convs;
1218 cand->second_conv = NULL_TREE;
1219 cand->viable = viable;
1220 cand->basetype_path = NULL_TREE;
1221 cand->template = NULL_TREE;
1222 cand->warnings = NULL_TREE;
1223 cand->next = candidates;
1225 return cand;
1228 /* Create an overload candidate for the function or method FN called with
1229 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1230 to implicit_conversion. */
1232 static struct z_candidate *
1233 add_function_candidate (candidates, fn, arglist, flags)
1234 struct z_candidate *candidates;
1235 tree fn, arglist;
1236 int flags;
1238 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1239 int i, len;
1240 tree convs;
1241 tree parmnode, argnode;
1242 int viable = 1;
1244 /* The `this' and `in_chrg' arguments to constructors are not considered
1245 in overload resolution. */
1246 if (DECL_CONSTRUCTOR_P (fn))
1248 parmlist = TREE_CHAIN (parmlist);
1249 arglist = TREE_CHAIN (arglist);
1250 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1252 parmlist = TREE_CHAIN (parmlist);
1253 arglist = TREE_CHAIN (arglist);
1257 len = list_length (arglist);
1258 convs = make_tree_vec (len);
1260 /* 13.3.2 - Viable functions [over.match.viable]
1261 First, to be a viable function, a candidate function shall have enough
1262 parameters to agree in number with the arguments in the list.
1264 We need to check this first; otherwise, checking the ICSes might cause
1265 us to produce an ill-formed template instantiation. */
1267 parmnode = parmlist;
1268 for (i = 0; i < len; ++i)
1270 if (parmnode == NULL_TREE || parmnode == void_list_node)
1271 break;
1272 parmnode = TREE_CHAIN (parmnode);
1275 if (i < len && parmnode)
1276 viable = 0;
1278 /* Make sure there are default args for the rest of the parms. */
1279 else for (; parmnode && parmnode != void_list_node;
1280 parmnode = TREE_CHAIN (parmnode))
1281 if (! TREE_PURPOSE (parmnode))
1283 viable = 0;
1284 break;
1287 if (! viable)
1288 goto out;
1290 /* Second, for F to be a viable function, there shall exist for each
1291 argument an implicit conversion sequence that converts that argument
1292 to the corresponding parameter of F. */
1294 parmnode = parmlist;
1295 argnode = arglist;
1297 for (i = 0; i < len; ++i)
1299 tree arg = TREE_VALUE (argnode);
1300 tree argtype = lvalue_type (arg);
1301 tree t;
1303 if (parmnode == void_list_node)
1304 break;
1306 if (parmnode)
1308 tree parmtype = TREE_VALUE (parmnode);
1310 /* [over.match.funcs] For conversion functions, the function is
1311 considered to be a member of the class of the implicit object
1312 argument for the purpose of defining the type of the implicit
1313 object parameter.
1315 Since build_over_call ignores the ICS for the `this' parameter,
1316 we can just change the parm type. */
1317 if (DECL_CONV_FN_P (fn) && i == 0)
1319 parmtype
1320 = build_qualified_type (TREE_TYPE (argtype),
1321 TYPE_QUALS (TREE_TYPE (parmtype)));
1322 parmtype = build_pointer_type (parmtype);
1325 t = implicit_conversion (parmtype, argtype, arg, flags);
1327 else
1329 t = build1 (IDENTITY_CONV, argtype, arg);
1330 ICS_ELLIPSIS_FLAG (t) = 1;
1333 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1334 && ! DECL_CONSTRUCTOR_P (fn))
1335 ICS_THIS_FLAG (t) = 1;
1337 TREE_VEC_ELT (convs, i) = t;
1338 if (! t)
1340 viable = 0;
1341 break;
1344 if (ICS_BAD_FLAG (t))
1345 viable = -1;
1347 if (parmnode)
1348 parmnode = TREE_CHAIN (parmnode);
1349 argnode = TREE_CHAIN (argnode);
1352 out:
1353 return add_candidate (candidates, fn, convs, viable);
1356 /* Create an overload candidate for the conversion function FN which will
1357 be invoked for expression OBJ, producing a pointer-to-function which
1358 will in turn be called with the argument list ARGLIST, and add it to
1359 CANDIDATES. FLAGS is passed on to implicit_conversion.
1361 Actually, we don't really care about FN; we care about the type it
1362 converts to. There may be multiple conversion functions that will
1363 convert to that type, and we rely on build_user_type_conversion_1 to
1364 choose the best one; so when we create our candidate, we record the type
1365 instead of the function. */
1367 static struct z_candidate *
1368 add_conv_candidate (candidates, fn, obj, arglist)
1369 struct z_candidate *candidates;
1370 tree fn, obj, arglist;
1372 tree totype = TREE_TYPE (TREE_TYPE (fn));
1373 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1374 int i, len = list_length (arglist) + 1;
1375 tree convs = make_tree_vec (len);
1376 tree parmnode = parmlist;
1377 tree argnode = arglist;
1378 int viable = 1;
1379 int flags = LOOKUP_NORMAL;
1381 /* Don't bother looking up the same type twice. */
1382 if (candidates && candidates->fn == totype)
1383 return candidates;
1385 for (i = 0; i < len; ++i)
1387 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1388 tree argtype = lvalue_type (arg);
1389 tree t;
1391 if (i == 0)
1392 t = implicit_conversion (totype, argtype, arg, flags);
1393 else if (parmnode == void_list_node)
1394 break;
1395 else if (parmnode)
1396 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1397 else
1399 t = build1 (IDENTITY_CONV, argtype, arg);
1400 ICS_ELLIPSIS_FLAG (t) = 1;
1403 TREE_VEC_ELT (convs, i) = t;
1404 if (! t)
1405 break;
1407 if (ICS_BAD_FLAG (t))
1408 viable = -1;
1410 if (i == 0)
1411 continue;
1413 if (parmnode)
1414 parmnode = TREE_CHAIN (parmnode);
1415 argnode = TREE_CHAIN (argnode);
1418 if (i < len)
1419 viable = 0;
1421 for (; parmnode && parmnode != void_list_node;
1422 parmnode = TREE_CHAIN (parmnode))
1423 if (! TREE_PURPOSE (parmnode))
1425 viable = 0;
1426 break;
1429 return add_candidate (candidates, totype, convs, viable);
1432 static struct z_candidate *
1433 build_builtin_candidate (candidates, fnname, type1, type2,
1434 args, argtypes, flags)
1435 struct z_candidate *candidates;
1436 tree fnname, type1, type2, *args, *argtypes;
1437 int flags;
1440 tree t, convs;
1441 int viable = 1, i;
1442 tree types[2];
1444 types[0] = type1;
1445 types[1] = type2;
1447 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1449 for (i = 0; i < 2; ++i)
1451 if (! args[i])
1452 break;
1454 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1455 if (! t)
1457 viable = 0;
1458 /* We need something for printing the candidate. */
1459 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1461 else if (ICS_BAD_FLAG (t))
1462 viable = 0;
1463 TREE_VEC_ELT (convs, i) = t;
1466 /* For COND_EXPR we rearranged the arguments; undo that now. */
1467 if (args[2])
1469 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1470 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1471 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1472 if (t)
1473 TREE_VEC_ELT (convs, 0) = t;
1474 else
1475 viable = 0;
1478 return add_candidate (candidates, fnname, convs, viable);
1481 static int
1482 is_complete (t)
1483 tree t;
1485 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1488 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1490 static int
1491 promoted_arithmetic_type_p (type)
1492 tree type;
1494 /* [over.built]
1496 In this section, the term promoted integral type is used to refer
1497 to those integral types which are preserved by integral promotion
1498 (including e.g. int and long but excluding e.g. char).
1499 Similarly, the term promoted arithmetic type refers to promoted
1500 integral types plus floating types. */
1501 return ((INTEGRAL_TYPE_P (type)
1502 && same_type_p (type_promotes_to (type), type))
1503 || TREE_CODE (type) == REAL_TYPE);
1506 /* Create any builtin operator overload candidates for the operator in
1507 question given the converted operand types TYPE1 and TYPE2. The other
1508 args are passed through from add_builtin_candidates to
1509 build_builtin_candidate. */
1511 static struct z_candidate *
1512 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1513 args, argtypes, flags)
1514 struct z_candidate *candidates;
1515 enum tree_code code, code2;
1516 tree fnname, type1, type2, *args, *argtypes;
1517 int flags;
1519 switch (code)
1521 case POSTINCREMENT_EXPR:
1522 case POSTDECREMENT_EXPR:
1523 args[1] = integer_zero_node;
1524 type2 = integer_type_node;
1525 break;
1526 default:
1527 break;
1530 switch (code)
1533 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1534 and VQ is either volatile or empty, there exist candidate operator
1535 functions of the form
1536 VQ T& operator++(VQ T&);
1537 T operator++(VQ T&, int);
1538 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1539 type other than bool, and VQ is either volatile or empty, there exist
1540 candidate operator functions of the form
1541 VQ T& operator--(VQ T&);
1542 T operator--(VQ T&, int);
1543 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1544 complete object type, and VQ is either volatile or empty, there exist
1545 candidate operator functions of the form
1546 T*VQ& operator++(T*VQ&);
1547 T*VQ& operator--(T*VQ&);
1548 T* operator++(T*VQ&, int);
1549 T* operator--(T*VQ&, int); */
1551 case POSTDECREMENT_EXPR:
1552 case PREDECREMENT_EXPR:
1553 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1554 return candidates;
1555 case POSTINCREMENT_EXPR:
1556 case PREINCREMENT_EXPR:
1557 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1558 || TYPE_PTROB_P (type1))
1560 type1 = build_reference_type (type1);
1561 break;
1563 return candidates;
1565 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1566 exist candidate operator functions of the form
1568 T& operator*(T*);
1570 8 For every function type T, there exist candidate operator functions of
1571 the form
1572 T& operator*(T*); */
1574 case INDIRECT_REF:
1575 if (TREE_CODE (type1) == POINTER_TYPE
1576 && (TYPE_PTROB_P (type1)
1577 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1578 break;
1579 return candidates;
1581 /* 9 For every type T, there exist candidate operator functions of the form
1582 T* operator+(T*);
1584 10For every promoted arithmetic type T, there exist candidate operator
1585 functions of the form
1586 T operator+(T);
1587 T operator-(T); */
1589 case CONVERT_EXPR: /* unary + */
1590 if (TREE_CODE (type1) == POINTER_TYPE
1591 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1592 break;
1593 case NEGATE_EXPR:
1594 if (ARITHMETIC_TYPE_P (type1))
1595 break;
1596 return candidates;
1598 /* 11For every promoted integral type T, there exist candidate operator
1599 functions of the form
1600 T operator~(T); */
1602 case BIT_NOT_EXPR:
1603 if (INTEGRAL_TYPE_P (type1))
1604 break;
1605 return candidates;
1607 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1608 is the same type as C2 or is a derived class of C2, T is a complete
1609 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1610 there exist candidate operator functions of the form
1611 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1612 where CV12 is the union of CV1 and CV2. */
1614 case MEMBER_REF:
1615 if (TREE_CODE (type1) == POINTER_TYPE
1616 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1618 tree c1 = TREE_TYPE (type1);
1619 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1620 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1621 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1623 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1624 && (TYPE_PTRMEMFUNC_P (type2)
1625 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1626 break;
1628 return candidates;
1630 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1631 didate operator functions of the form
1632 LR operator*(L, R);
1633 LR operator/(L, R);
1634 LR operator+(L, R);
1635 LR operator-(L, R);
1636 bool operator<(L, R);
1637 bool operator>(L, R);
1638 bool operator<=(L, R);
1639 bool operator>=(L, R);
1640 bool operator==(L, R);
1641 bool operator!=(L, R);
1642 where LR is the result of the usual arithmetic conversions between
1643 types L and R.
1645 14For every pair of types T and I, where T is a cv-qualified or cv-
1646 unqualified complete object type and I is a promoted integral type,
1647 there exist candidate operator functions of the form
1648 T* operator+(T*, I);
1649 T& operator[](T*, I);
1650 T* operator-(T*, I);
1651 T* operator+(I, T*);
1652 T& operator[](I, T*);
1654 15For every T, where T is a pointer to complete object type, there exist
1655 candidate operator functions of the form112)
1656 ptrdiff_t operator-(T, T);
1658 16For every pointer type T, there exist candidate operator functions of
1659 the form
1660 bool operator<(T, T);
1661 bool operator>(T, T);
1662 bool operator<=(T, T);
1663 bool operator>=(T, T);
1664 bool operator==(T, T);
1665 bool operator!=(T, T);
1667 17For every pointer to member type T, there exist candidate operator
1668 functions of the form
1669 bool operator==(T, T);
1670 bool operator!=(T, T); */
1672 case MINUS_EXPR:
1673 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1674 break;
1675 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1677 type2 = ptrdiff_type_node;
1678 break;
1680 case MULT_EXPR:
1681 case TRUNC_DIV_EXPR:
1682 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1683 break;
1684 return candidates;
1686 case EQ_EXPR:
1687 case NE_EXPR:
1688 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1689 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1690 break;
1691 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1692 && null_ptr_cst_p (args[1]))
1694 type2 = type1;
1695 break;
1697 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1698 && null_ptr_cst_p (args[0]))
1700 type1 = type2;
1701 break;
1703 case LT_EXPR:
1704 case GT_EXPR:
1705 case LE_EXPR:
1706 case GE_EXPR:
1707 case MAX_EXPR:
1708 case MIN_EXPR:
1709 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1710 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1711 break;
1712 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1714 type2 = type1;
1715 break;
1717 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1719 type1 = type2;
1720 break;
1722 return candidates;
1724 case PLUS_EXPR:
1725 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1726 break;
1727 case ARRAY_REF:
1728 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1730 type1 = ptrdiff_type_node;
1731 break;
1733 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1735 type2 = ptrdiff_type_node;
1736 break;
1738 return candidates;
1740 /* 18For every pair of promoted integral types L and R, there exist candi-
1741 date operator functions of the form
1742 LR operator%(L, R);
1743 LR operator&(L, R);
1744 LR operator^(L, R);
1745 LR operator|(L, R);
1746 L operator<<(L, R);
1747 L operator>>(L, R);
1748 where LR is the result of the usual arithmetic conversions between
1749 types L and R. */
1751 case TRUNC_MOD_EXPR:
1752 case BIT_AND_EXPR:
1753 case BIT_IOR_EXPR:
1754 case BIT_XOR_EXPR:
1755 case LSHIFT_EXPR:
1756 case RSHIFT_EXPR:
1757 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1758 break;
1759 return candidates;
1761 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1762 type, VQ is either volatile or empty, and R is a promoted arithmetic
1763 type, there exist candidate operator functions of the form
1764 VQ L& operator=(VQ L&, R);
1765 VQ L& operator*=(VQ L&, R);
1766 VQ L& operator/=(VQ L&, R);
1767 VQ L& operator+=(VQ L&, R);
1768 VQ L& operator-=(VQ L&, R);
1770 20For every pair T, VQ), where T is any type and VQ is either volatile
1771 or empty, there exist candidate operator functions of the form
1772 T*VQ& operator=(T*VQ&, T*);
1774 21For every pair T, VQ), where T is a pointer to member type and VQ is
1775 either volatile or empty, there exist candidate operator functions of
1776 the form
1777 VQ T& operator=(VQ T&, T);
1779 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1780 unqualified complete object type, VQ is either volatile or empty, and
1781 I is a promoted integral type, there exist candidate operator func-
1782 tions of the form
1783 T*VQ& operator+=(T*VQ&, I);
1784 T*VQ& operator-=(T*VQ&, I);
1786 23For every triple L, VQ, R), where L is an integral or enumeration
1787 type, VQ is either volatile or empty, and R is a promoted integral
1788 type, there exist candidate operator functions of the form
1790 VQ L& operator%=(VQ L&, R);
1791 VQ L& operator<<=(VQ L&, R);
1792 VQ L& operator>>=(VQ L&, R);
1793 VQ L& operator&=(VQ L&, R);
1794 VQ L& operator^=(VQ L&, R);
1795 VQ L& operator|=(VQ L&, R); */
1797 case MODIFY_EXPR:
1798 switch (code2)
1800 case PLUS_EXPR:
1801 case MINUS_EXPR:
1802 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1804 type2 = ptrdiff_type_node;
1805 break;
1807 case MULT_EXPR:
1808 case TRUNC_DIV_EXPR:
1809 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1810 break;
1811 return candidates;
1813 case TRUNC_MOD_EXPR:
1814 case BIT_AND_EXPR:
1815 case BIT_IOR_EXPR:
1816 case BIT_XOR_EXPR:
1817 case LSHIFT_EXPR:
1818 case RSHIFT_EXPR:
1819 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1820 break;
1821 return candidates;
1823 case NOP_EXPR:
1824 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1825 break;
1826 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1827 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1828 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1829 || ((TYPE_PTRMEMFUNC_P (type1)
1830 || TREE_CODE (type1) == POINTER_TYPE)
1831 && null_ptr_cst_p (args[1])))
1833 type2 = type1;
1834 break;
1836 return candidates;
1838 default:
1839 my_friendly_abort (367);
1841 type1 = build_reference_type (type1);
1842 break;
1844 case COND_EXPR:
1845 /* [over.builtin]
1847 For every pair of promoted arithmetic types L and R, there
1848 exist candidate operator functions of the form
1850 LR operator?(bool, L, R);
1852 where LR is the result of the usual arithmetic conversions
1853 between types L and R.
1855 For every type T, where T is a pointer or pointer-to-member
1856 type, there exist candidate operator functions of the form T
1857 operator?(bool, T, T); */
1859 if (promoted_arithmetic_type_p (type1)
1860 && promoted_arithmetic_type_p (type2))
1861 /* That's OK. */
1862 break;
1864 /* Otherwise, the types should be pointers. */
1865 if (!(TREE_CODE (type1) == POINTER_TYPE
1866 || TYPE_PTRMEM_P (type1)
1867 || TYPE_PTRMEMFUNC_P (type1))
1868 || !(TREE_CODE (type2) == POINTER_TYPE
1869 || TYPE_PTRMEM_P (type2)
1870 || TYPE_PTRMEMFUNC_P (type2)))
1871 return candidates;
1873 /* We don't check that the two types are the same; the logic
1874 below will actually create two candidates; one in which both
1875 parameter types are TYPE1, and one in which both parameter
1876 types are TYPE2. */
1877 break;
1879 /* These arguments do not make for a legal overloaded operator. */
1880 return candidates;
1882 default:
1883 my_friendly_abort (367);
1886 /* If we're dealing with two pointer types, we need candidates
1887 for both of them. */
1888 if (type2 && !same_type_p (type1, type2)
1889 && TREE_CODE (type1) == TREE_CODE (type2)
1890 && (TREE_CODE (type1) == REFERENCE_TYPE
1891 || (TREE_CODE (type1) == POINTER_TYPE
1892 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1893 || TYPE_PTRMEMFUNC_P (type1)
1894 || IS_AGGR_TYPE (type1)))
1896 candidates = build_builtin_candidate
1897 (candidates, fnname, type1, type1, args, argtypes, flags);
1898 return build_builtin_candidate
1899 (candidates, fnname, type2, type2, args, argtypes, flags);
1902 return build_builtin_candidate
1903 (candidates, fnname, type1, type2, args, argtypes, flags);
1906 tree
1907 type_decays_to (type)
1908 tree type;
1910 if (TREE_CODE (type) == ARRAY_TYPE)
1911 return build_pointer_type (TREE_TYPE (type));
1912 if (TREE_CODE (type) == FUNCTION_TYPE)
1913 return build_pointer_type (type);
1914 return type;
1917 /* There are three conditions of builtin candidates:
1919 1) bool-taking candidates. These are the same regardless of the input.
1920 2) pointer-pair taking candidates. These are generated for each type
1921 one of the input types converts to.
1922 3) arithmetic candidates. According to the WP, we should generate
1923 all of these, but I'm trying not to... */
1925 static struct z_candidate *
1926 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1927 struct z_candidate *candidates;
1928 enum tree_code code, code2;
1929 tree fnname, *args;
1930 int flags;
1932 int ref1, i;
1933 tree type, argtypes[3];
1934 /* TYPES[i] is the set of possible builtin-operator parameter types
1935 we will consider for the Ith argument. These are represented as
1936 a TREE_LIST; the TREE_VALUE of each node is the potential
1937 parameter type. */
1938 tree types[2];
1940 for (i = 0; i < 3; ++i)
1942 if (args[i])
1943 argtypes[i] = lvalue_type (args[i]);
1944 else
1945 argtypes[i] = NULL_TREE;
1948 switch (code)
1950 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1951 and VQ is either volatile or empty, there exist candidate operator
1952 functions of the form
1953 VQ T& operator++(VQ T&); */
1955 case POSTINCREMENT_EXPR:
1956 case PREINCREMENT_EXPR:
1957 case POSTDECREMENT_EXPR:
1958 case PREDECREMENT_EXPR:
1959 case MODIFY_EXPR:
1960 ref1 = 1;
1961 break;
1963 /* 24There also exist candidate operator functions of the form
1964 bool operator!(bool);
1965 bool operator&&(bool, bool);
1966 bool operator||(bool, bool); */
1968 case TRUTH_NOT_EXPR:
1969 return build_builtin_candidate
1970 (candidates, fnname, boolean_type_node,
1971 NULL_TREE, args, argtypes, flags);
1973 case TRUTH_ORIF_EXPR:
1974 case TRUTH_ANDIF_EXPR:
1975 return build_builtin_candidate
1976 (candidates, fnname, boolean_type_node,
1977 boolean_type_node, args, argtypes, flags);
1979 case ADDR_EXPR:
1980 case COMPOUND_EXPR:
1981 case COMPONENT_REF:
1982 return candidates;
1984 default:
1985 ref1 = 0;
1988 types[0] = types[1] = NULL_TREE;
1990 for (i = 0; i < 2; ++i)
1992 if (! args[i])
1994 else if (IS_AGGR_TYPE (argtypes[i]))
1996 tree convs;
1998 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1999 return candidates;
2001 convs = lookup_conversions (argtypes[i]);
2003 if (code == COND_EXPR)
2005 if (real_lvalue_p (args[i]))
2006 types[i] = tree_cons
2007 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2009 types[i] = tree_cons
2010 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2013 else if (! convs)
2014 return candidates;
2016 for (; convs; convs = TREE_CHAIN (convs))
2018 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2020 if (i == 0 && ref1
2021 && (TREE_CODE (type) != REFERENCE_TYPE
2022 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2023 continue;
2025 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2026 types[i] = tree_cons (NULL_TREE, type, types[i]);
2028 type = non_reference (type);
2029 if (i != 0 || ! ref1)
2031 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2032 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2033 types[i] = tree_cons (NULL_TREE, type, types[i]);
2034 if (INTEGRAL_TYPE_P (type))
2035 type = type_promotes_to (type);
2038 if (! value_member (type, types[i]))
2039 types[i] = tree_cons (NULL_TREE, type, types[i]);
2042 else
2044 if (code == COND_EXPR && real_lvalue_p (args[i]))
2045 types[i] = tree_cons
2046 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2047 type = non_reference (argtypes[i]);
2048 if (i != 0 || ! ref1)
2050 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2051 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2052 types[i] = tree_cons (NULL_TREE, type, types[i]);
2053 if (INTEGRAL_TYPE_P (type))
2054 type = type_promotes_to (type);
2056 types[i] = tree_cons (NULL_TREE, type, types[i]);
2060 /* Run through the possible parameter types of both arguments,
2061 creating candidates with those parameter types. */
2062 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2064 if (types[1])
2065 for (type = types[1]; type; type = TREE_CHAIN (type))
2066 candidates = add_builtin_candidate
2067 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2068 TREE_VALUE (type), args, argtypes, flags);
2069 else
2070 candidates = add_builtin_candidate
2071 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2072 NULL_TREE, args, argtypes, flags);
2075 return candidates;
2079 /* If TMPL can be successfully instantiated as indicated by
2080 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2082 TMPL is the template. EXPLICIT_TARGS are any explicit template
2083 arguments. ARGLIST is the arguments provided at the call-site.
2084 The RETURN_TYPE is the desired type for conversion operators. If
2085 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
2086 OBJ is supplied, FLAGS are ignored, and OBJ is as for
2087 add_conv_candidate. */
2089 static struct z_candidate*
2090 add_template_candidate_real (candidates, tmpl, explicit_targs,
2091 arglist, return_type, flags,
2092 obj, strict)
2093 struct z_candidate *candidates;
2094 tree tmpl, explicit_targs, arglist, return_type;
2095 int flags;
2096 tree obj;
2097 unification_kind_t strict;
2099 int ntparms = DECL_NTPARMS (tmpl);
2100 tree targs = make_tree_vec (ntparms);
2101 struct z_candidate *cand;
2102 int i;
2103 tree fn;
2105 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
2106 return_type, strict);
2108 if (i != 0)
2109 return candidates;
2111 fn = instantiate_template (tmpl, targs);
2112 if (fn == error_mark_node)
2113 return candidates;
2115 if (obj != NULL_TREE)
2116 /* Aha, this is a conversion function. */
2117 cand = add_conv_candidate (candidates, fn, obj, arglist);
2118 else
2119 cand = add_function_candidate (candidates, fn, arglist, flags);
2120 if (DECL_TI_TEMPLATE (fn) != tmpl)
2121 /* This situation can occur if a member template of a template
2122 class is specialized. Then, instantiate_template might return
2123 an instantiation of the specialization, in which case the
2124 DECL_TI_TEMPLATE field will point at the original
2125 specialization. For example:
2127 template <class T> struct S { template <class U> void f(U);
2128 template <> void f(int) {}; };
2129 S<double> sd;
2130 sd.f(3);
2132 Here, TMPL will be template <class U> S<double>::f(U).
2133 And, instantiate template will give us the specialization
2134 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2135 for this will point at template <class T> template <> S<T>::f(int),
2136 so that we can find the definition. For the purposes of
2137 overload resolution, however, we want the original TMPL. */
2138 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2139 else
2140 cand->template = DECL_TEMPLATE_INFO (fn);
2142 return cand;
2146 static struct z_candidate *
2147 add_template_candidate (candidates, tmpl, explicit_targs,
2148 arglist, return_type, flags, strict)
2149 struct z_candidate *candidates;
2150 tree tmpl, explicit_targs, arglist, return_type;
2151 int flags;
2152 unification_kind_t strict;
2154 return
2155 add_template_candidate_real (candidates, tmpl, explicit_targs,
2156 arglist, return_type, flags,
2157 NULL_TREE, strict);
2161 static struct z_candidate *
2162 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2163 struct z_candidate *candidates;
2164 tree tmpl, obj, arglist, return_type;
2166 return
2167 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
2168 return_type, 0, obj, DEDUCE_CONV);
2172 static int
2173 any_viable (cands)
2174 struct z_candidate *cands;
2176 for (; cands; cands = cands->next)
2177 if (pedantic ? cands->viable == 1 : cands->viable)
2178 return 1;
2179 return 0;
2182 static struct z_candidate *
2183 splice_viable (cands)
2184 struct z_candidate *cands;
2186 struct z_candidate **p = &cands;
2188 for (; *p; )
2190 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2191 p = &((*p)->next);
2192 else
2193 *p = (*p)->next;
2196 return cands;
2199 static tree
2200 build_this (obj)
2201 tree obj;
2203 /* Fix this to work on non-lvalues. */
2204 return build_unary_op (ADDR_EXPR, obj, 0);
2207 static void
2208 print_z_candidates (candidates)
2209 struct z_candidate *candidates;
2211 const char *str = "candidates are:";
2212 for (; candidates; candidates = candidates->next)
2214 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2216 if (candidates->fn == ansi_opname [COND_EXPR])
2217 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2218 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2219 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2220 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2221 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2222 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2223 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2224 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2225 else
2226 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2227 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2229 else if (TYPE_P (candidates->fn))
2230 cp_error ("%s %T <conversion>", str, candidates->fn);
2231 else
2232 cp_error_at ("%s %+#D%s", str, candidates->fn,
2233 candidates->viable == -1 ? " <near match>" : "");
2234 str = " ";
2238 /* Returns the best overload candidate to perform the requested
2239 conversion. This function is used for three the overloading situations
2240 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2241 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2242 per [dcl.init.ref], so we ignore temporary bindings. */
2244 static struct z_candidate *
2245 build_user_type_conversion_1 (totype, expr, flags)
2246 tree totype, expr;
2247 int flags;
2249 struct z_candidate *candidates, *cand;
2250 tree fromtype = TREE_TYPE (expr);
2251 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2252 tree args = NULL_TREE;
2253 tree templates = NULL_TREE;
2255 if (IS_AGGR_TYPE (totype))
2256 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2257 if (IS_AGGR_TYPE (fromtype)
2258 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2259 convs = lookup_conversions (fromtype);
2261 candidates = 0;
2262 flags |= LOOKUP_NO_CONVERSION;
2264 if (ctors)
2266 tree t = build_int_2 (0, 0);
2267 TREE_TYPE (t) = build_pointer_type (totype);
2268 args = build_scratch_list (NULL_TREE, expr);
2269 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2270 args = tree_cons (NULL_TREE, integer_one_node, args);
2271 args = tree_cons (NULL_TREE, t, args);
2273 ctors = TREE_VALUE (ctors);
2275 for (; ctors; ctors = OVL_NEXT (ctors))
2277 tree ctor = OVL_CURRENT (ctors);
2278 if (DECL_NONCONVERTING_P (ctor))
2279 continue;
2281 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2283 templates = tree_cons (NULL_TREE, ctor, templates);
2284 candidates =
2285 add_template_candidate (candidates, ctor,
2286 NULL_TREE, args, NULL_TREE, flags,
2287 DEDUCE_CALL);
2289 else
2290 candidates = add_function_candidate (candidates, ctor,
2291 args, flags);
2293 if (candidates)
2295 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2296 candidates->basetype_path = TYPE_BINFO (totype);
2300 if (convs)
2301 args = build_scratch_list (NULL_TREE, build_this (expr));
2303 for (; convs; convs = TREE_CHAIN (convs))
2305 tree fns = TREE_VALUE (convs);
2306 int convflags = LOOKUP_NO_CONVERSION;
2307 tree ics;
2309 /* If we are called to convert to a reference type, we are trying to
2310 find an lvalue binding, so don't even consider temporaries. If
2311 we don't find an lvalue binding, the caller will try again to
2312 look for a temporary binding. */
2313 if (TREE_CODE (totype) == REFERENCE_TYPE)
2314 convflags |= LOOKUP_NO_TEMP_BIND;
2316 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2317 ics = implicit_conversion
2318 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2319 else
2320 /* We can't compute this yet. */
2321 ics = error_mark_node;
2323 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2324 /* ignore the near match. */;
2325 else if (ics)
2326 for (; fns; fns = OVL_NEXT (fns))
2328 tree fn = OVL_CURRENT (fns);
2329 struct z_candidate *old_candidates = candidates;
2331 if (TREE_CODE (fn) == TEMPLATE_DECL)
2333 templates = tree_cons (NULL_TREE, fn, templates);
2334 candidates =
2335 add_template_candidate (candidates, fn, NULL_TREE,
2336 args, totype, flags,
2337 DEDUCE_CONV);
2339 else
2340 candidates = add_function_candidate (candidates, fn,
2341 args, flags);
2343 if (candidates != old_candidates)
2345 if (TREE_CODE (fn) == TEMPLATE_DECL)
2346 ics = implicit_conversion
2347 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2348 0, convflags);
2350 candidates->second_conv = ics;
2351 candidates->basetype_path = TREE_PURPOSE (convs);
2353 if (ics == NULL_TREE)
2354 candidates->viable = 0;
2355 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2356 candidates->viable = -1;
2361 if (! any_viable (candidates))
2363 #if 0
2364 if (flags & LOOKUP_COMPLAIN)
2366 if (candidates && ! candidates->next)
2367 /* say why this one won't work or try to be loose */;
2368 else
2369 cp_error ("no viable candidates");
2371 #endif
2373 return 0;
2376 candidates = splice_viable (candidates);
2377 cand = tourney (candidates);
2379 if (cand == 0)
2381 if (flags & LOOKUP_COMPLAIN)
2383 cp_error ("conversion from `%T' to `%T' is ambiguous",
2384 fromtype, totype);
2385 print_z_candidates (candidates);
2388 cand = candidates; /* any one will do */
2389 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2390 ICS_USER_FLAG (cand->second_conv) = 1;
2391 ICS_BAD_FLAG (cand->second_conv) = 1;
2393 return cand;
2396 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2397 p = &(TREE_OPERAND (*p, 0));
2399 /* Pedantically, normal function declarations are never considered
2400 to refer to template instantiations, so we only do this with
2401 -fguiding-decls. */
2402 if (flag_guiding_decls && templates && ! cand->template
2403 && !DECL_INITIAL (cand->fn)
2404 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2405 add_maybe_template (cand->fn, templates);
2407 *p = build
2408 (USER_CONV,
2409 (DECL_CONSTRUCTOR_P (cand->fn)
2410 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2411 expr, build_expr_ptr_wrapper (cand));
2412 ICS_USER_FLAG (cand->second_conv) = 1;
2413 if (cand->viable == -1)
2414 ICS_BAD_FLAG (cand->second_conv) = 1;
2416 return cand;
2419 tree
2420 build_user_type_conversion (totype, expr, flags)
2421 tree totype, expr;
2422 int flags;
2424 struct z_candidate *cand
2425 = build_user_type_conversion_1 (totype, expr, flags);
2427 if (cand)
2429 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2430 return error_mark_node;
2431 return convert_from_reference (convert_like (cand->second_conv, expr));
2433 return NULL_TREE;
2436 /* Do any initial processing on the arguments to a function call. */
2438 static tree
2439 resolve_args (args)
2440 tree args;
2442 tree t;
2443 for (t = args; t; t = TREE_CHAIN (t))
2445 if (TREE_VALUE (t) == error_mark_node)
2446 return error_mark_node;
2447 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2449 error ("invalid use of void expression");
2450 return error_mark_node;
2452 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2453 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2455 return args;
2458 tree
2459 build_new_function_call (fn, args)
2460 tree fn, args;
2462 struct z_candidate *candidates = 0, *cand;
2463 tree explicit_targs = NULL_TREE;
2464 int template_only = 0;
2466 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2468 explicit_targs = TREE_OPERAND (fn, 1);
2469 fn = TREE_OPERAND (fn, 0);
2470 template_only = 1;
2473 if (really_overloaded_fn (fn))
2475 tree t1;
2476 tree templates = NULL_TREE;
2478 args = resolve_args (args);
2480 if (args == error_mark_node)
2481 return error_mark_node;
2483 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2485 tree t = OVL_FUNCTION (t1);
2486 struct z_candidate *old_candidates = candidates;
2488 if (TREE_CODE (t) == TEMPLATE_DECL)
2490 templates = tree_cons (NULL_TREE, t, templates);
2491 candidates = add_template_candidate
2492 (candidates, t, explicit_targs, args, NULL_TREE,
2493 LOOKUP_NORMAL, DEDUCE_CALL);
2495 else if (! template_only)
2496 candidates = add_function_candidate
2497 (candidates, t, args, LOOKUP_NORMAL);
2499 if (candidates != old_candidates)
2500 candidates->basetype_path = DECL_REAL_CONTEXT (t);
2503 if (! any_viable (candidates))
2505 if (candidates && ! candidates->next)
2506 return build_function_call (candidates->fn, args);
2507 cp_error ("no matching function for call to `%D (%A)'",
2508 DECL_NAME (OVL_FUNCTION (fn)), args);
2509 if (candidates)
2510 print_z_candidates (candidates);
2511 return error_mark_node;
2513 candidates = splice_viable (candidates);
2514 cand = tourney (candidates);
2516 if (cand == 0)
2518 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2519 DECL_NAME (OVL_FUNCTION (fn)), args);
2520 print_z_candidates (candidates);
2521 return error_mark_node;
2524 /* Pedantically, normal function declarations are never considered
2525 to refer to template instantiations, so we only do this with
2526 -fguiding-decls. */
2527 if (flag_guiding_decls && templates && ! cand->template
2528 && ! DECL_INITIAL (cand->fn))
2529 add_maybe_template (cand->fn, templates);
2531 return build_over_call (cand, args, LOOKUP_NORMAL);
2534 /* This is not really overloaded. */
2535 fn = OVL_CURRENT (fn);
2537 return build_function_call (fn, args);
2540 static tree
2541 build_object_call (obj, args)
2542 tree obj, args;
2544 struct z_candidate *candidates = 0, *cand;
2545 tree fns, convs, mem_args = NULL_TREE;
2546 tree type = TREE_TYPE (obj);
2548 if (TYPE_PTRMEMFUNC_P (type))
2550 /* It's no good looking for an overloaded operator() on a
2551 pointer-to-member-function. */
2552 cp_error ("pointer-to-member function %E cannot be called", obj);
2553 cp_error ("without an object; consider using .* or ->*");
2554 return error_mark_node;
2557 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2558 if (fns == error_mark_node)
2559 return error_mark_node;
2561 args = resolve_args (args);
2563 if (args == error_mark_node)
2564 return error_mark_node;
2566 if (fns)
2568 tree base = TREE_PURPOSE (fns);
2569 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2571 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2573 tree fn = OVL_CURRENT (fns);
2574 if (TREE_CODE (fn) == TEMPLATE_DECL)
2576 candidates
2577 = add_template_candidate (candidates, fn, NULL_TREE,
2578 mem_args, NULL_TREE,
2579 LOOKUP_NORMAL, DEDUCE_CALL);
2581 else
2582 candidates = add_function_candidate
2583 (candidates, fn, mem_args, LOOKUP_NORMAL);
2585 if (candidates)
2586 candidates->basetype_path = base;
2590 convs = lookup_conversions (type);
2592 for (; convs; convs = TREE_CHAIN (convs))
2594 tree fns = TREE_VALUE (convs);
2595 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2597 if ((TREE_CODE (totype) == POINTER_TYPE
2598 || TREE_CODE (totype) == REFERENCE_TYPE)
2599 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2600 for (; fns; fns = OVL_NEXT (fns))
2602 tree fn = OVL_CURRENT (fns);
2603 if (TREE_CODE (fn) == TEMPLATE_DECL)
2605 candidates = add_template_conv_candidate (candidates,
2607 obj,
2608 args,
2609 totype);
2611 else
2612 candidates = add_conv_candidate (candidates, fn, obj, args);
2614 if (candidates)
2615 candidates->basetype_path = TREE_PURPOSE (convs);
2619 if (! any_viable (candidates))
2621 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2622 print_z_candidates (candidates);
2623 return error_mark_node;
2626 candidates = splice_viable (candidates);
2627 cand = tourney (candidates);
2629 if (cand == 0)
2631 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2632 print_z_candidates (candidates);
2633 return error_mark_node;
2636 /* Since cand->fn will be a type, not a function, for a conversion
2637 function, we must be careful not to unconditionally look at
2638 DECL_NAME here. */
2639 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2640 && DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2641 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2643 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2645 /* FIXME */
2646 return build_function_call (obj, args);
2649 static void
2650 op_error (code, code2, arg1, arg2, arg3, problem)
2651 enum tree_code code, code2;
2652 tree arg1, arg2, arg3;
2653 const char *problem;
2655 const char * opname
2656 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2658 switch (code)
2660 case COND_EXPR:
2661 cp_error ("%s for `%T ? %T : %T'", problem,
2662 error_type (arg1), error_type (arg2), error_type (arg3));
2663 break;
2664 case POSTINCREMENT_EXPR:
2665 case POSTDECREMENT_EXPR:
2666 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2667 break;
2668 case ARRAY_REF:
2669 cp_error ("%s for `%T[%T]'", problem,
2670 error_type (arg1), error_type (arg2));
2671 break;
2672 default:
2673 if (arg2)
2674 cp_error ("%s for `%T %s %T'", problem,
2675 error_type (arg1), opname, error_type (arg2));
2676 else
2677 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2681 /* Return the implicit conversion sequence that could be used to
2682 convert E1 to E2 in [expr.cond]. */
2684 static tree
2685 conditional_conversion (e1, e2)
2686 tree e1;
2687 tree e2;
2689 tree t1 = non_reference (TREE_TYPE (e1));
2690 tree t2 = non_reference (TREE_TYPE (e2));
2691 tree conv;
2693 /* [expr.cond]
2695 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2696 implicitly converted (clause _conv_) to the type "reference to
2697 T2", subject to the constraint that in the conversion the
2698 reference must bind directly (_dcl.init.ref_) to E1. */
2699 if (real_lvalue_p (e2))
2701 conv = implicit_conversion (build_reference_type (t2),
2704 LOOKUP_NO_TEMP_BIND);
2705 if (conv)
2706 return conv;
2709 /* [expr.cond]
2711 If E1 and E2 have class type, and the underlying class types are
2712 the same or one is a base class of the other: E1 can be converted
2713 to match E2 if the class of T2 is the same type as, or a base
2714 class of, the class of T1, and the cv-qualification of T2 is the
2715 same cv-qualification as, or a greater cv-qualification than, the
2716 cv-qualification of T1. If the conversion is applied, E1 is
2717 changed to an rvalue of type T2 that still refers to the original
2718 source class object (or the appropriate subobject thereof). */
2719 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2720 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2721 TYPE_MAIN_VARIANT (t1)))
2723 if (at_least_as_qualified_p (t2, t1))
2725 conv = build1 (IDENTITY_CONV, t1, e1);
2726 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2727 TYPE_MAIN_VARIANT (t2)))
2728 conv = build_conv (BASE_CONV, t2, conv);
2729 return conv;
2731 else
2732 return NULL_TREE;
2735 /* [expr.cond]
2737 E1 can be converted to match E2 if E1 can be implicitly converted
2738 to the type that expression E2 would have if E2 were converted to
2739 an rvalue (or the type it has, if E2 is an rvalue). */
2740 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2743 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2744 arguments to the conditional expression. By the time this function
2745 is called, any suitable candidate functions are included in
2746 CANDIDATES. */
2748 tree
2749 build_conditional_expr (arg1, arg2, arg3)
2750 tree arg1;
2751 tree arg2;
2752 tree arg3;
2754 tree arg2_type;
2755 tree arg3_type;
2756 tree result;
2757 tree result_type = NULL_TREE;
2758 int lvalue_p = 1;
2759 struct z_candidate *candidates = 0;
2760 struct z_candidate *cand;
2762 /* As a G++ extension, the second argument to the conditional can be
2763 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2764 c'.) If the second operand is omitted, make sure it is
2765 calculated only once. */
2766 if (!arg2)
2768 if (pedantic)
2769 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
2770 arg1 = arg2 = save_expr (arg1);
2773 /* If something has already gone wrong, just pass that fact up the
2774 tree. */
2775 if (arg1 == error_mark_node
2776 || arg2 == error_mark_node
2777 || arg3 == error_mark_node
2778 || TREE_TYPE (arg1) == error_mark_node
2779 || TREE_TYPE (arg2) == error_mark_node
2780 || TREE_TYPE (arg3) == error_mark_node)
2781 return error_mark_node;
2783 /* [expr.cond]
2785 The first expr ession is implicitly converted to bool (clause
2786 _conv_). */
2787 arg1 = cp_convert (boolean_type_node, arg1);
2789 /* Convert from reference types to ordinary types; no expressions
2790 really have reference type in C++. */
2791 arg2 = convert_from_reference (arg2);
2792 arg3 = convert_from_reference (arg3);
2794 /* [expr.cond]
2796 If either the second or the third operand has type (possibly
2797 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2798 array-to-pointer (_conv.array_), and function-to-pointer
2799 (_conv.func_) standard conversions are performed on the second
2800 and third operands. */
2801 arg2_type = TREE_TYPE (arg2);
2802 arg3_type = TREE_TYPE (arg3);
2803 if (same_type_p (TYPE_MAIN_VARIANT (arg2_type), void_type_node)
2804 || same_type_p (TYPE_MAIN_VARIANT (arg3_type), void_type_node))
2806 int arg2_void_p;
2807 int arg3_void_p;
2809 /* Do the conversions. We don't these for `void' type arguments
2810 since it can't have any effect and since decay_conversion
2811 does not handle that case gracefully. */
2812 if (!same_type_p (TYPE_MAIN_VARIANT (arg2_type), void_type_node))
2813 arg2 = decay_conversion (arg2);
2814 if (!same_type_p (TYPE_MAIN_VARIANT (arg3_type), void_type_node))
2815 arg3 = decay_conversion (arg3);
2816 arg2_type = TREE_TYPE (arg2);
2817 arg3_type = TREE_TYPE (arg3);
2819 arg2_void_p = same_type_p (TYPE_MAIN_VARIANT (arg2_type),
2820 void_type_node);
2821 arg3_void_p = same_type_p (TYPE_MAIN_VARIANT (arg3_type),
2822 void_type_node);
2824 /* [expr.cond]
2826 One of the following shall hold:
2828 --The second or the third operand (but not both) is a
2829 throw-expression (_except.throw_); the result is of the
2830 type of the other and is an rvalue.
2832 --Both the second and the third operands have type void; the
2833 result is of type void and is an rvalue. */
2834 if ((TREE_CODE (arg2) == THROW_EXPR)
2835 ^ (TREE_CODE (arg3) == THROW_EXPR))
2836 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2837 ? arg3_type : arg2_type);
2838 else if (arg2_void_p && arg3_void_p)
2839 result_type = void_type_node;
2840 else
2842 cp_error ("`%E' has type `void' and is not a throw-expression",
2843 arg2_void_p ? arg2 : arg3);
2844 return error_mark_node;
2847 lvalue_p = 0;
2848 goto valid_operands;
2850 /* [expr.cond]
2852 Otherwise, if the second and third operand have different types,
2853 and either has (possibly cv-qualified) class type, an attempt is
2854 made to convert each of those operands to the type of the other. */
2855 else if (!same_type_p (arg2_type, arg3_type)
2856 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2858 tree conv2 = conditional_conversion (arg2, arg3);
2859 tree conv3 = conditional_conversion (arg3, arg2);
2861 /* [expr.cond]
2863 If both can be converted, or one can be converted but the
2864 conversion is ambiguous, the program is ill-formed. If
2865 neither can be converted, the operands are left unchanged and
2866 further checking is performed as described below. If exactly
2867 one conversion is possible, that conversion is applied to the
2868 chosen operand and the converted operand is used in place of
2869 the original operand for the remainder of this section. */
2870 if ((conv2 && !ICS_BAD_FLAG (conv2)
2871 && conv3 && !ICS_BAD_FLAG (conv3))
2872 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
2873 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
2875 cp_error ("operands to ?: have different types");
2876 return error_mark_node;
2878 else if (conv2 && !ICS_BAD_FLAG (conv2))
2880 arg2 = convert_like (conv2, arg2);
2881 arg2 = convert_from_reference (arg2);
2882 /* That may not quite have done the trick. If the two types
2883 are cv-qualified variants of one another, we will have
2884 just used an IDENTITY_CONV. (There's no conversion from
2885 an lvalue of one class type to an lvalue of another type,
2886 even a cv-qualified variant, and we don't want to lose
2887 lvalue-ness here.) So, we manually add a NOP_EXPR here
2888 if necessary. */
2889 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
2890 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
2891 arg2_type = TREE_TYPE (arg2);
2893 else if (conv3 && !ICS_BAD_FLAG (conv3))
2895 arg3 = convert_like (conv3, arg3);
2896 arg3 = convert_from_reference (arg3);
2897 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
2898 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
2899 arg3_type = TREE_TYPE (arg3);
2903 /* [expr.cond]
2905 If the second and third operands are lvalues and have the same
2906 type, the result is of that type and is an lvalue. */
2907 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
2908 same_type_p (arg2_type, arg3_type))
2910 result_type = arg2_type;
2911 goto valid_operands;
2914 /* [expr.cond]
2916 Otherwise, the result is an rvalue. If the second and third
2917 operand do not have the same type, and either has (possibly
2918 cv-qualified) class type, overload resolution is used to
2919 determine the conversions (if any) to be applied to the operands
2920 (_over.match.oper_, _over.built_). */
2921 lvalue_p = 0;
2922 if (!same_type_p (arg2_type, arg3_type)
2923 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2925 tree args[3];
2926 tree conv;
2928 /* Rearrange the arguments so that add_builtin_candidate only has
2929 to know about two args. In build_builtin_candidates, the
2930 arguments are unscrambled. */
2931 args[0] = arg2;
2932 args[1] = arg3;
2933 args[2] = arg1;
2934 candidates = add_builtin_candidates (candidates,
2935 COND_EXPR,
2936 NOP_EXPR,
2937 ansi_opname[COND_EXPR],
2938 args,
2939 LOOKUP_NORMAL);
2941 /* [expr.cond]
2943 If the overload resolution fails, the program is
2944 ill-formed. */
2945 if (!any_viable (candidates))
2947 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
2948 print_z_candidates (candidates);
2949 return error_mark_node;
2951 candidates = splice_viable (candidates);
2952 cand = tourney (candidates);
2953 if (!cand)
2955 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
2956 print_z_candidates (candidates);
2957 return error_mark_node;
2960 /* [expr.cond]
2962 Otherwise, the conversions thus determined are applied, and
2963 the converted operands are used in place of the original
2964 operands for the remainder of this section. */
2965 conv = TREE_VEC_ELT (cand->convs, 0);
2966 arg1 = convert_like (conv, arg1);
2967 conv = TREE_VEC_ELT (cand->convs, 1);
2968 arg2 = convert_like (conv, arg2);
2969 conv = TREE_VEC_ELT (cand->convs, 2);
2970 arg3 = convert_like (conv, arg3);
2973 /* [expr.cond]
2975 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
2976 and function-to-pointer (_conv.func_) standard conversions are
2977 performed on the second and third operands.
2979 We need to force the lvalue-to-rvalue conversion here for class types,
2980 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
2981 that isn't wrapped with a TARGET_EXPR plays havoc with exception
2982 regions. */
2984 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
2985 arg2 = build_user_type_conversion (TREE_TYPE (arg2), arg2, LOOKUP_NORMAL);
2986 else
2987 arg2 = decay_conversion (arg2);
2988 arg2_type = TREE_TYPE (arg2);
2990 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
2991 arg3 = build_user_type_conversion (TREE_TYPE (arg3), arg3, LOOKUP_NORMAL);
2992 else
2993 arg3 = decay_conversion (arg3);
2994 arg3_type = TREE_TYPE (arg3);
2996 /* [expr.cond]
2998 After those conversions, one of the following shall hold:
3000 --The second and third operands have the same type; the result is of
3001 that type. */
3002 if (same_type_p (arg2_type, arg3_type))
3003 result_type = arg2_type;
3004 /* [expr.cond]
3006 --The second and third operands have arithmetic or enumeration
3007 type; the usual arithmetic conversions are performed to bring
3008 them to a common type, and the result is of that type. */
3009 else if ((ARITHMETIC_TYPE_P (arg2_type)
3010 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3011 && (ARITHMETIC_TYPE_P (arg3_type)
3012 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3014 /* In this case, there is always a common type. */
3015 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3016 arg3_type);
3018 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3019 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3020 cp_warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3021 arg2_type, arg3_type);
3022 else if (extra_warnings
3023 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3024 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3025 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3026 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3027 cp_warning ("enumeral and non-enumeral type in conditional expression");
3029 arg2 = perform_implicit_conversion (result_type, arg2);
3030 arg3 = perform_implicit_conversion (result_type, arg3);
3032 /* [expr.cond]
3034 --The second and third operands have pointer type, or one has
3035 pointer type and the other is a null pointer constant; pointer
3036 conversions (_conv.ptr_) and qualification conversions
3037 (_conv.qual_) are performed to bring them to their composite
3038 pointer type (_expr.rel_). The result is of the composite
3039 pointer type.
3041 --The second and third operands have pointer to member type, or
3042 one has pointer to member type and the other is a null pointer
3043 constant; pointer to member conversions (_conv.mem_) and
3044 qualification conversions (_conv.qual_) are performed to bring
3045 them to a common type, whose cv-qualification shall match the
3046 cv-qualification of either the second or the third operand.
3047 The result is of the common type. */
3048 else if ((null_ptr_cst_p (arg2)
3049 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3050 || TYPE_PTRMEMFUNC_P (arg3_type)))
3051 || (null_ptr_cst_p (arg3)
3052 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3053 || TYPE_PTRMEMFUNC_P (arg2_type)))
3054 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3055 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3056 || (TYPE_PTRMEMFUNC_P (arg2_type)
3057 && TYPE_PTRMEMFUNC_P (arg3_type)))
3059 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3060 arg3, "conditional expression");
3061 arg2 = perform_implicit_conversion (result_type, arg2);
3062 arg3 = perform_implicit_conversion (result_type, arg3);
3065 if (!result_type)
3067 cp_error ("operands to ?: have different types");
3068 return error_mark_node;
3071 valid_operands:
3072 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3073 /* Expand both sides into the same slot, hopefully the target of the
3074 ?: expression. We used to check for TARGET_EXPRs here, but now we
3075 sometimes wrap them in NOP_EXPRs so the test would fail. */
3076 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3078 tree slot = build (VAR_DECL, result_type);
3079 layout_decl (slot, 0);
3080 result = build_target_expr (slot, result);
3083 /* If this expression is an rvalue, but might be mistaken for an
3084 lvalue, we must add a NON_LVALUE_EXPR. */
3085 if (!lvalue_p && real_lvalue_p (result))
3086 result = build1 (NON_LVALUE_EXPR, result_type, result);
3088 return result;
3091 tree
3092 build_new_op (code, flags, arg1, arg2, arg3)
3093 enum tree_code code;
3094 int flags;
3095 tree arg1, arg2, arg3;
3097 struct z_candidate *candidates = 0, *cand;
3098 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3099 enum tree_code code2 = NOP_EXPR;
3100 tree templates = NULL_TREE;
3101 tree conv;
3103 if (arg1 == error_mark_node
3104 || arg2 == error_mark_node
3105 || arg3 == error_mark_node)
3106 return error_mark_node;
3108 /* This can happen if a template takes all non-type parameters, e.g.
3109 undeclared_template<1, 5, 72>a; */
3110 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3112 cp_error ("`%D' must be declared before use", arg1);
3113 return error_mark_node;
3116 if (code == MODIFY_EXPR)
3118 code2 = TREE_CODE (arg3);
3119 arg3 = NULL_TREE;
3120 fnname = ansi_assopname[code2];
3122 else
3123 fnname = ansi_opname[code];
3125 switch (code)
3127 case NEW_EXPR:
3128 case VEC_NEW_EXPR:
3129 case VEC_DELETE_EXPR:
3130 case DELETE_EXPR:
3131 /* Use build_op_new_call and build_op_delete_call instead. */
3132 my_friendly_abort (981018);
3134 case CALL_EXPR:
3135 return build_object_call (arg1, arg2);
3137 default:
3138 break;
3141 /* The comma operator can have void args. */
3142 if (TREE_CODE (arg1) == OFFSET_REF)
3143 arg1 = resolve_offset_ref (arg1);
3144 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
3145 arg2 = resolve_offset_ref (arg2);
3146 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
3147 arg3 = resolve_offset_ref (arg3);
3149 if (code == COND_EXPR)
3151 if (arg2 == NULL_TREE
3152 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3153 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3154 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3155 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3156 goto builtin;
3158 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3159 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3160 goto builtin;
3162 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3163 arg2 = integer_zero_node;
3165 if (arg2 && arg3)
3166 arglist = tree_cons (NULL_TREE, arg1, tree_cons
3167 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
3168 else if (arg2)
3169 arglist = tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
3170 else
3171 arglist = build_scratch_list (NULL_TREE, arg1);
3173 fns = lookup_function_nonclass (fnname, arglist);
3175 if (fns && TREE_CODE (fns) == TREE_LIST)
3176 fns = TREE_VALUE (fns);
3177 for (; fns; fns = OVL_NEXT (fns))
3179 tree fn = OVL_CURRENT (fns);
3180 if (TREE_CODE (fn) == TEMPLATE_DECL)
3182 templates = tree_cons (NULL_TREE, fn, templates);
3183 candidates
3184 = add_template_candidate (candidates, fn, NULL_TREE,
3185 arglist, TREE_TYPE (fnname),
3186 flags, DEDUCE_CALL);
3188 else
3189 candidates = add_function_candidate (candidates, fn, arglist, flags);
3192 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3194 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3195 if (fns == error_mark_node)
3196 return fns;
3198 else
3199 fns = NULL_TREE;
3201 if (fns)
3203 tree basetype = TREE_PURPOSE (fns);
3204 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3205 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3207 tree fn = OVL_CURRENT (fns);
3208 tree this_arglist;
3210 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3211 this_arglist = mem_arglist;
3212 else
3213 this_arglist = arglist;
3215 if (TREE_CODE (fn) == TEMPLATE_DECL)
3217 /* A member template. */
3218 templates = tree_cons (NULL_TREE, fn, templates);
3219 candidates
3220 = add_template_candidate (candidates, fn, NULL_TREE,
3221 this_arglist, TREE_TYPE (fnname),
3222 flags, DEDUCE_CALL);
3224 else
3225 candidates = add_function_candidate
3226 (candidates, fn, this_arglist, flags);
3228 if (candidates)
3229 candidates->basetype_path = basetype;
3234 tree args[3];
3236 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3237 to know about two args; a builtin candidate will always have a first
3238 parameter of type bool. We'll handle that in
3239 build_builtin_candidate. */
3240 if (code == COND_EXPR)
3242 args[0] = arg2;
3243 args[1] = arg3;
3244 args[2] = arg1;
3246 else
3248 args[0] = arg1;
3249 args[1] = arg2;
3250 args[2] = NULL_TREE;
3253 candidates = add_builtin_candidates
3254 (candidates, code, code2, fnname, args, flags);
3257 if (! any_viable (candidates))
3259 switch (code)
3261 case POSTINCREMENT_EXPR:
3262 case POSTDECREMENT_EXPR:
3263 /* Look for an `operator++ (int)'. If they didn't have
3264 one, then we fall back to the old way of doing things. */
3265 if (flags & LOOKUP_COMPLAIN)
3266 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
3267 fnname, opname_tab [code]);
3268 if (code == POSTINCREMENT_EXPR)
3269 code = PREINCREMENT_EXPR;
3270 else
3271 code = PREDECREMENT_EXPR;
3272 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3274 /* The caller will deal with these. */
3275 case ADDR_EXPR:
3276 case COMPOUND_EXPR:
3277 case COMPONENT_REF:
3278 return NULL_TREE;
3280 default:
3281 break;
3283 if (flags & LOOKUP_COMPLAIN)
3285 op_error (code, code2, arg1, arg2, arg3, "no match");
3286 print_z_candidates (candidates);
3288 return error_mark_node;
3290 candidates = splice_viable (candidates);
3291 cand = tourney (candidates);
3293 if (cand == 0)
3295 if (flags & LOOKUP_COMPLAIN)
3297 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3298 print_z_candidates (candidates);
3300 return error_mark_node;
3303 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3305 extern int warn_synth;
3306 if (warn_synth
3307 && fnname == ansi_opname[MODIFY_EXPR]
3308 && DECL_ARTIFICIAL (cand->fn)
3309 && candidates->next
3310 && ! candidates->next->next)
3312 cp_warning ("using synthesized `%#D' for copy assignment",
3313 cand->fn);
3314 cp_warning_at (" where cfront would use `%#D'",
3315 cand == candidates
3316 ? candidates->next->fn
3317 : candidates->fn);
3320 /* Pedantically, normal function declarations are never considered
3321 to refer to template instantiations, so we only do this with
3322 -fguiding-decls. */
3323 if (flag_guiding_decls && templates && ! cand->template
3324 && ! DECL_INITIAL (cand->fn)
3325 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
3326 add_maybe_template (cand->fn, templates);
3328 return build_over_call
3329 (cand,
3330 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3331 ? mem_arglist : arglist,
3332 LOOKUP_NORMAL);
3335 /* Check for comparison of different enum types. */
3336 switch (code)
3338 case GT_EXPR:
3339 case LT_EXPR:
3340 case GE_EXPR:
3341 case LE_EXPR:
3342 case EQ_EXPR:
3343 case NE_EXPR:
3344 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3345 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3346 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3347 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3349 cp_warning ("comparison between `%#T' and `%#T'",
3350 TREE_TYPE (arg1), TREE_TYPE (arg2));
3352 break;
3353 default:
3354 break;
3357 /* We need to strip any leading REF_BIND so that bitfields don't cause
3358 errors. This should not remove any important conversions, because
3359 builtins don't apply to class objects directly. */
3360 conv = TREE_VEC_ELT (cand->convs, 0);
3361 if (TREE_CODE (conv) == REF_BIND)
3362 conv = TREE_OPERAND (conv, 0);
3363 arg1 = convert_like (conv, arg1);
3364 if (arg2)
3366 conv = TREE_VEC_ELT (cand->convs, 1);
3367 if (TREE_CODE (conv) == REF_BIND)
3368 conv = TREE_OPERAND (conv, 0);
3369 arg2 = convert_like (conv, arg2);
3371 if (arg3)
3373 conv = TREE_VEC_ELT (cand->convs, 2);
3374 if (TREE_CODE (conv) == REF_BIND)
3375 conv = TREE_OPERAND (conv, 0);
3376 arg3 = convert_like (conv, arg3);
3379 builtin:
3380 switch (code)
3382 case MODIFY_EXPR:
3383 return build_modify_expr (arg1, code2, arg2);
3385 case INDIRECT_REF:
3386 return build_indirect_ref (arg1, "unary *");
3388 case PLUS_EXPR:
3389 case MINUS_EXPR:
3390 case MULT_EXPR:
3391 case TRUNC_DIV_EXPR:
3392 case GT_EXPR:
3393 case LT_EXPR:
3394 case GE_EXPR:
3395 case LE_EXPR:
3396 case EQ_EXPR:
3397 case NE_EXPR:
3398 case MAX_EXPR:
3399 case MIN_EXPR:
3400 case LSHIFT_EXPR:
3401 case RSHIFT_EXPR:
3402 case TRUNC_MOD_EXPR:
3403 case BIT_AND_EXPR:
3404 case BIT_IOR_EXPR:
3405 case BIT_XOR_EXPR:
3406 case TRUTH_ANDIF_EXPR:
3407 case TRUTH_ORIF_EXPR:
3408 return build_binary_op_nodefault (code, arg1, arg2, code);
3410 case CONVERT_EXPR:
3411 case NEGATE_EXPR:
3412 case BIT_NOT_EXPR:
3413 case TRUTH_NOT_EXPR:
3414 case PREINCREMENT_EXPR:
3415 case POSTINCREMENT_EXPR:
3416 case PREDECREMENT_EXPR:
3417 case POSTDECREMENT_EXPR:
3418 case REALPART_EXPR:
3419 case IMAGPART_EXPR:
3420 return build_unary_op (code, arg1, candidates != 0);
3422 case ARRAY_REF:
3423 return build_array_ref (arg1, arg2);
3425 case COND_EXPR:
3426 return build_conditional_expr (arg1, arg2, arg3);
3428 case MEMBER_REF:
3429 return build_m_component_ref
3430 (build_indirect_ref (arg1, NULL_PTR), arg2);
3432 /* The caller will deal with these. */
3433 case ADDR_EXPR:
3434 case COMPONENT_REF:
3435 case COMPOUND_EXPR:
3436 return NULL_TREE;
3438 default:
3439 my_friendly_abort (367);
3440 return NULL_TREE;
3444 /* Build up a call to operator new. This has to be handled differently
3445 from other operators in the way lookup is handled; first members are
3446 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
3447 TYPE is the type to be created. ARGS are any new-placement args.
3448 FLAGS are the usual overloading flags. */
3450 tree
3451 build_op_new_call (code, type, args, flags)
3452 enum tree_code code;
3453 tree type, args;
3454 int flags;
3456 tree fnname = ansi_opname[code];
3458 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
3459 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
3461 return build_method_call (build_dummy_object (type),
3462 fnname, args, NULL_TREE, flags);
3464 else
3465 return build_new_function_call
3466 (lookup_function_nonclass (fnname, args), args);
3469 /* Build a call to operator delete. This has to be handled very specially,
3470 because the restrictions on what signatures match are different from all
3471 other call instances. For a normal delete, only a delete taking (void *)
3472 or (void *, size_t) is accepted. For a placement delete, only an exact
3473 match with the placement new is accepted.
3475 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3476 ADDR is the pointer to be deleted. For placement delete, it is also
3477 used to determine what the corresponding new looked like.
3478 SIZE is the size of the memory block to be deleted.
3479 FLAGS are the usual overloading flags.
3480 PLACEMENT is the corresponding placement new call, or 0. */
3482 tree
3483 build_op_delete_call (code, addr, size, flags, placement)
3484 enum tree_code code;
3485 tree addr, size, placement;
3486 int flags;
3488 tree fn, fns, fnname, fntype, argtypes, args, type;
3490 if (addr == error_mark_node)
3491 return error_mark_node;
3493 type = TREE_TYPE (TREE_TYPE (addr));
3494 fnname = ansi_opname[code];
3496 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3497 /* In [class.free]
3499 If the result of the lookup is ambiguous or inaccessible, or if
3500 the lookup selects a placement deallocation function, the
3501 program is ill-formed.
3503 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3505 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3506 if (fns == error_mark_node)
3507 return error_mark_node;
3509 else
3510 fns = NULL_TREE;
3512 if (fns == NULL_TREE)
3513 fns = lookup_name_nonclass (fnname);
3515 if (placement)
3517 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3519 /* Extract the function. */
3520 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3521 /* Then the second parm type. */
3522 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3524 /* Also the second argument. */
3525 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3527 else
3529 /* First try it without the size argument. */
3530 argtypes = void_list_node;
3531 args = NULL_TREE;
3534 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3535 fntype = build_function_type (void_type_node, argtypes);
3537 /* Strip const and volatile from addr. */
3538 if (type != TYPE_MAIN_VARIANT (type))
3539 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
3541 fn = instantiate_type (fntype, fns, 2);
3543 if (fn != error_mark_node)
3545 if (TREE_CODE (fns) == TREE_LIST)
3546 /* Member functions. */
3547 enforce_access (TREE_PURPOSE (fns), fn);
3548 return build_function_call (fn, tree_cons (NULL_TREE, addr, args));
3551 /* If we are doing placement delete we do nothing if we don't find a
3552 matching op delete. */
3553 if (placement)
3554 return NULL_TREE;
3556 /* Normal delete; now try to find a match including the size argument. */
3557 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3558 tree_cons (NULL_TREE, sizetype, void_list_node));
3559 fntype = build_function_type (void_type_node, argtypes);
3561 fn = instantiate_type (fntype, fns, 2);
3563 if (fn != error_mark_node)
3565 if (BASELINK_P (fns))
3566 /* Member functions. */
3567 enforce_access (TREE_PURPOSE (fns), fn);
3568 return build_function_call
3569 (fn, tree_cons (NULL_TREE, addr,
3570 build_expr_list (NULL_TREE, size)));
3573 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3574 destructor, in which case the error should be deferred
3575 until someone actually tries to delete one of these. */
3576 if (flags & LOOKUP_SPECULATIVELY)
3577 return NULL_TREE;
3579 cp_error ("no suitable operator delete for `%T'", type);
3580 return error_mark_node;
3583 /* If the current scope isn't allowed to access DECL along
3584 BASETYPE_PATH, give an error. The most derived class in
3585 BASETYPE_PATH is the one used to qualify DECL. */
3588 enforce_access (basetype_path, decl)
3589 tree basetype_path;
3590 tree decl;
3592 int accessible;
3594 accessible = accessible_p (basetype_path, decl);
3595 if (!accessible)
3597 if (TREE_PRIVATE (decl))
3598 cp_error_at ("`%+#D' is private", decl);
3599 else if (TREE_PROTECTED (decl))
3600 cp_error_at ("`%+#D' is protected", decl);
3601 else
3602 cp_error_at ("`%+#D' is inaccessible", decl);
3603 cp_error ("within this context");
3604 return 0;
3607 return 1;
3610 /* Perform the conversions in CONVS on the expression EXPR. */
3612 static tree
3613 convert_like (convs, expr)
3614 tree convs, expr;
3616 if (ICS_BAD_FLAG (convs)
3617 && TREE_CODE (convs) != USER_CONV
3618 && TREE_CODE (convs) != AMBIG_CONV
3619 && TREE_CODE (convs) != REF_BIND)
3621 tree t = convs;
3622 for (; t; t = TREE_OPERAND (t, 0))
3624 if (TREE_CODE (t) == USER_CONV)
3626 expr = convert_like (t, expr);
3627 break;
3629 else if (TREE_CODE (t) == AMBIG_CONV)
3630 return convert_like (t, expr);
3631 else if (TREE_CODE (t) == IDENTITY_CONV)
3632 break;
3634 return convert_for_initialization
3635 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3636 "conversion", NULL_TREE, 0);
3639 switch (TREE_CODE (convs))
3641 case USER_CONV:
3643 struct z_candidate *cand
3644 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3645 tree fn = cand->fn;
3646 tree args;
3648 if (DECL_CONSTRUCTOR_P (fn))
3650 tree t = build_int_2 (0, 0);
3651 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3653 args = build_scratch_list (NULL_TREE, expr);
3654 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3655 args = tree_cons (NULL_TREE, integer_one_node, args);
3656 args = tree_cons (NULL_TREE, t, args);
3658 else
3659 args = build_this (expr);
3660 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3662 /* If this is a constructor or a function returning an aggr type,
3663 we need to build up a TARGET_EXPR. */
3664 if (DECL_CONSTRUCTOR_P (fn))
3665 expr = build_cplus_new (TREE_TYPE (convs), expr);
3667 return expr;
3669 case IDENTITY_CONV:
3670 if (type_unknown_p (expr))
3671 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3672 return expr;
3673 case AMBIG_CONV:
3674 /* Call build_user_type_conversion again for the error. */
3675 return build_user_type_conversion
3676 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3678 default:
3679 break;
3682 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3683 if (expr == error_mark_node)
3684 return error_mark_node;
3686 /* Convert a constant variable to its underlying value, unless we
3687 are about to bind it to a reference, in which case we need to
3688 leave it as an lvalue. */
3689 if (TREE_READONLY_DECL_P (expr) && TREE_CODE (convs) != REF_BIND)
3690 expr = decl_constant_value (expr);
3692 switch (TREE_CODE (convs))
3694 case RVALUE_CONV:
3695 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3696 return expr;
3697 /* else fall through */
3698 case BASE_CONV:
3699 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3701 /* We are going to bind a reference directly to a base-class
3702 subobject of EXPR. */
3703 tree base_ptr = build_pointer_type (TREE_TYPE (convs));
3705 /* Build an expression for `*((base*) &expr)'. */
3706 expr = build_unary_op (ADDR_EXPR, expr, 0);
3707 expr = perform_implicit_conversion (base_ptr, expr);
3708 expr = build_indirect_ref (expr, "implicit conversion");
3709 return expr;
3713 tree cvt_expr = build_user_type_conversion
3714 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3715 if (!cvt_expr)
3717 /* This can occur if, for example, the EXPR has incomplete
3718 type. We can't check for that before attempting the
3719 conversion because the type might be an incomplete
3720 array type, which is OK if some constructor for the
3721 destination type takes a pointer argument. */
3722 if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
3724 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
3725 incomplete_type_error (expr, TREE_TYPE (expr));
3726 else
3727 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3728 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3730 else
3731 cp_error ("could not convert `%E' to `%T'",
3732 expr, TREE_TYPE (convs));
3733 return error_mark_node;
3735 return cvt_expr;
3738 case REF_BIND:
3740 tree ref_type = TREE_TYPE (convs);
3742 /* If necessary, create a temporary. */
3743 if (NEED_TEMPORARY_P (convs))
3745 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3746 tree slot = build_decl (VAR_DECL, NULL_TREE, type);
3747 DECL_ARTIFICIAL (slot) = 1;
3748 expr = build_target_expr (slot, expr);
3749 TREE_SIDE_EFFECTS (expr) = 1;
3752 /* Take the address of the thing to which we will bind the
3753 reference. */
3754 expr = build_unary_op (ADDR_EXPR, expr, 1);
3755 if (expr == error_mark_node)
3756 return error_mark_node;
3758 /* Convert it to a pointer to the type referred to by the
3759 reference. This will adjust the pointer if a derived to
3760 base conversion is being performed. */
3761 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3762 expr);
3763 /* Convert the pointer to the desired reference type. */
3764 expr = build1 (NOP_EXPR, ref_type, expr);
3766 return expr;
3769 case LVALUE_CONV:
3770 return decay_conversion (expr);
3772 case QUAL_CONV:
3773 /* Warn about deprecated conversion if appropriate. */
3774 string_conv_p (TREE_TYPE (convs), expr, 1);
3775 break;
3777 default:
3778 break;
3780 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3781 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3784 /* ARG is being passed to a varargs function. Perform any conversions
3785 required. Array/function to pointer decay must have already happened.
3786 Return the converted value. */
3788 tree
3789 convert_arg_to_ellipsis (arg)
3790 tree arg;
3792 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3793 && (TYPE_PRECISION (TREE_TYPE (arg))
3794 < TYPE_PRECISION (double_type_node)))
3795 /* Convert `float' to `double'. */
3796 arg = cp_convert (double_type_node, arg);
3797 else
3798 /* Convert `short' and `char' to full-size `int'. */
3799 arg = default_conversion (arg);
3801 arg = require_complete_type (arg);
3803 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
3805 /* Undefined behaviour [expr.call] 5.2.2/7. */
3806 cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3807 TREE_TYPE (arg));
3810 return arg;
3813 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
3815 tree
3816 build_x_va_arg (expr, type)
3817 tree expr;
3818 tree type;
3820 type = complete_type_or_else (type, NULL_TREE);
3822 if (expr == error_mark_node || !type)
3823 return error_mark_node;
3825 if (! pod_type_p (type))
3827 /* Undefined behaviour [expr.call] 5.2.2/7. */
3828 cp_warning ("cannot receive objects of non-POD type `%#T' through `...'",
3829 type);
3832 return build_va_arg (expr, type);
3835 /* TYPE has been given to va_arg. Apply the default conversions which would
3836 have happened when passed via ellipsis. Return the promoted type, or
3837 NULL_TREE, if there is no change. */
3839 tree
3840 convert_type_from_ellipsis (type)
3841 tree type;
3843 tree promote;
3845 if (TREE_CODE (type) == ARRAY_TYPE)
3846 promote = build_pointer_type (TREE_TYPE (type));
3847 else if (TREE_CODE (type) == FUNCTION_TYPE)
3848 promote = build_pointer_type (type);
3849 else
3850 promote = type_promotes_to (type);
3852 return same_type_p (type, promote) ? NULL_TREE : promote;
3855 /* ARG is a default argument expression being passed to a parameter of
3856 the indicated TYPE, which is a parameter to FN. Do any required
3857 conversions. Return the converted value. */
3859 tree
3860 convert_default_arg (type, arg, fn)
3861 tree type;
3862 tree arg;
3863 tree fn;
3865 if (fn && DECL_TEMPLATE_INFO (fn))
3866 arg = tsubst_default_argument (fn, type, arg);
3868 arg = break_out_target_exprs (arg);
3870 if (TREE_CODE (arg) == CONSTRUCTOR)
3872 arg = digest_init (type, arg, 0);
3873 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3874 "default argument", 0, 0);
3876 else
3878 /* This could get clobbered by the following call. */
3879 if (TREE_HAS_CONSTRUCTOR (arg))
3880 arg = copy_node (arg);
3882 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3883 "default argument", 0, 0);
3884 if (PROMOTE_PROTOTYPES
3885 && (TREE_CODE (type) == INTEGER_TYPE
3886 || TREE_CODE (type) == ENUMERAL_TYPE)
3887 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3888 arg = default_conversion (arg);
3891 return arg;
3894 static tree
3895 build_over_call (cand, args, flags)
3896 struct z_candidate *cand;
3897 tree args;
3898 int flags;
3900 tree fn = cand->fn;
3901 tree convs = cand->convs;
3902 tree converted_args = NULL_TREE;
3903 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3904 tree conv, arg, val;
3905 int i = 0;
3906 int is_method = 0;
3908 /* Give any warnings we noticed during overload resolution. */
3909 if (cand->warnings)
3910 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3911 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3913 if (DECL_FUNCTION_MEMBER_P (fn))
3914 enforce_access (cand->basetype_path, fn);
3916 if (args && TREE_CODE (args) != TREE_LIST)
3917 args = build_scratch_list (NULL_TREE, args);
3918 arg = args;
3920 /* The implicit parameters to a constructor are not considered by overload
3921 resolution, and must be of the proper type. */
3922 if (DECL_CONSTRUCTOR_P (fn))
3924 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3925 arg = TREE_CHAIN (arg);
3926 parm = TREE_CHAIN (parm);
3927 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3929 converted_args = tree_cons
3930 (NULL_TREE, TREE_VALUE (arg), converted_args);
3931 arg = TREE_CHAIN (arg);
3932 parm = TREE_CHAIN (parm);
3935 /* Bypass access control for 'this' parameter. */
3936 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3938 tree parmtype = TREE_VALUE (parm);
3939 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3940 tree t;
3941 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3942 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3943 TREE_TYPE (argtype), fn);
3945 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3946 X is called for an object that is not of type X, or of a type
3947 derived from X, the behavior is undefined.
3949 So we can assume that anything passed as 'this' is non-null, and
3950 optimize accordingly. */
3951 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
3952 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3953 converted_args = tree_cons (NULL_TREE, t, converted_args);
3954 parm = TREE_CHAIN (parm);
3955 arg = TREE_CHAIN (arg);
3956 ++i;
3957 is_method = 1;
3960 for (; arg && parm;
3961 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3963 tree type = TREE_VALUE (parm);
3965 conv = TREE_VEC_ELT (convs, i);
3966 if (ICS_BAD_FLAG (conv))
3968 tree t = conv;
3969 val = TREE_VALUE (arg);
3971 for (; t; t = TREE_OPERAND (t, 0))
3973 if (TREE_CODE (t) == USER_CONV
3974 || TREE_CODE (t) == AMBIG_CONV)
3976 val = convert_like (t, val);
3977 break;
3979 else if (TREE_CODE (t) == IDENTITY_CONV)
3980 break;
3982 val = convert_for_initialization
3983 (NULL_TREE, type, val, LOOKUP_NORMAL,
3984 "argument passing", fn, i - is_method);
3986 else
3988 /* Issue warnings about peculiar, but legal, uses of NULL. */
3989 if (ARITHMETIC_TYPE_P (TREE_VALUE (parm))
3990 && TREE_VALUE (arg) == null_node)
3991 cp_warning ("converting NULL to non-pointer type");
3993 val = convert_like (conv, TREE_VALUE (arg));
3996 if (PROMOTE_PROTOTYPES
3997 && (TREE_CODE (type) == INTEGER_TYPE
3998 || TREE_CODE (type) == ENUMERAL_TYPE)
3999 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4000 val = default_conversion (val);
4001 converted_args = tree_cons (NULL_TREE, val, converted_args);
4004 /* Default arguments */
4005 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
4006 converted_args
4007 = tree_cons (NULL_TREE,
4008 convert_default_arg (TREE_VALUE (parm),
4009 TREE_PURPOSE (parm),
4010 fn),
4011 converted_args);
4013 /* Ellipsis */
4014 for (; arg; arg = TREE_CHAIN (arg))
4015 converted_args
4016 = tree_cons (NULL_TREE,
4017 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4018 converted_args);
4020 converted_args = nreverse (converted_args);
4022 if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
4023 check_function_format (DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
4024 converted_args);
4026 /* Avoid actually calling copy constructors and copy assignment operators,
4027 if possible. */
4029 if (! flag_elide_constructors)
4030 /* Do things the hard way. */;
4031 else if (DECL_CONSTRUCTOR_P (fn)
4032 && TREE_VEC_LENGTH (convs) == 1
4033 && copy_args_p (fn))
4035 tree targ;
4036 arg = TREE_CHAIN (converted_args);
4037 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
4038 arg = TREE_CHAIN (arg);
4039 arg = TREE_VALUE (arg);
4041 /* Pull out the real argument, disregarding const-correctness. */
4042 targ = arg;
4043 while (TREE_CODE (targ) == NOP_EXPR
4044 || TREE_CODE (targ) == NON_LVALUE_EXPR
4045 || TREE_CODE (targ) == CONVERT_EXPR)
4046 targ = TREE_OPERAND (targ, 0);
4047 if (TREE_CODE (targ) == ADDR_EXPR)
4049 targ = TREE_OPERAND (targ, 0);
4050 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
4051 TYPE_MAIN_VARIANT (TREE_TYPE (targ))))
4052 targ = NULL_TREE;
4054 else
4055 targ = NULL_TREE;
4057 if (targ)
4058 arg = targ;
4059 else
4060 arg = build_indirect_ref (arg, 0);
4062 /* [class.copy]: the copy constructor is implicitly defined even if
4063 the implementation elided its use. */
4064 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4065 mark_used (fn);
4067 /* If we're creating a temp and we already have one, don't create a
4068 new one. If we're not creating a temp but we get one, use
4069 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4070 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4071 temp or an INIT_EXPR otherwise. */
4072 if (integer_zerop (TREE_VALUE (args)))
4074 if (! real_lvalue_p (arg))
4075 return arg;
4076 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4078 val = build_decl (VAR_DECL, NULL_TREE, DECL_CONTEXT (fn));
4079 val = build_target_expr (val, arg);
4080 return val;
4083 else if (! real_lvalue_p (arg)
4084 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4086 tree address;
4087 tree to = stabilize_reference
4088 (build_indirect_ref (TREE_VALUE (args), 0));
4090 /* If we're initializing an empty class, then we actually
4091 have to use a MODIFY_EXPR rather than an INIT_EXPR. The
4092 reason is that the dummy padding member in the target may
4093 not actually be allocated if TO is a base class
4094 subobject. Since we've set TYPE_NONCOPIED_PARTS on the
4095 padding, a MODIFY_EXPR will preserve its value, which is
4096 the right thing to do if it's not really padding at all.
4098 It's not safe to just throw away the ARG if we're looking
4099 at an empty class because the ARG might contain a
4100 TARGET_EXPR which wants to be bound to TO. If it is not,
4101 expand_expr will assign a dummy slot for the TARGET_EXPR,
4102 and we will call a destructor for it, which is wrong,
4103 because we will also destroy TO, but will never have
4104 constructed it. */
4105 val = build (is_empty_class (DECL_CLASS_CONTEXT (fn))
4106 ? MODIFY_EXPR : INIT_EXPR,
4107 DECL_CONTEXT (fn), to, arg);
4108 address = build_unary_op (ADDR_EXPR, val, 0);
4109 /* Avoid a warning about this expression, if the address is
4110 never used. */
4111 TREE_USED (address) = 1;
4112 return address;
4115 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
4116 && copy_args_p (fn)
4117 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
4119 tree to = stabilize_reference
4120 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4122 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4124 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4125 return val;
4128 mark_used (fn);
4130 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4132 tree t, *p = &TREE_VALUE (converted_args);
4133 tree binfo = get_binfo
4134 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
4135 *p = convert_pointer_to_real (binfo, *p);
4136 if (TREE_SIDE_EFFECTS (*p))
4137 *p = save_expr (*p);
4138 t = build_pointer_type (TREE_TYPE (fn));
4139 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4140 TREE_TYPE (fn) = t;
4142 else if (DECL_INLINE (fn))
4143 fn = inline_conversion (fn);
4144 else
4145 fn = build_addr_func (fn);
4147 /* Recognize certain built-in functions so we can make tree-codes
4148 other than CALL_EXPR. We do this when it enables fold-const.c
4149 to do something useful. */
4151 if (TREE_CODE (fn) == ADDR_EXPR
4152 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4153 && DECL_BUILT_IN (TREE_OPERAND (fn, 0))
4154 && DECL_BUILT_IN_CLASS (TREE_OPERAND (fn, 0)) == BUILT_IN_NORMAL)
4155 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
4157 case BUILT_IN_ABS:
4158 case BUILT_IN_LABS:
4159 case BUILT_IN_FABS:
4160 if (converted_args == 0)
4161 return integer_zero_node;
4162 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
4163 default:
4164 break;
4167 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
4168 if (TREE_CODE (TREE_TYPE (fn)) == VOID_TYPE)
4169 return fn;
4170 fn = require_complete_type (fn);
4171 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4172 fn = build_cplus_new (TREE_TYPE (fn), fn);
4173 return convert_from_reference (fn);
4176 static tree
4177 build_new_method_call (instance, name, args, basetype_path, flags)
4178 tree instance, name, args, basetype_path;
4179 int flags;
4181 struct z_candidate *candidates = 0, *cand;
4182 tree explicit_targs = NULL_TREE;
4183 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4184 tree pretty_name;
4185 tree user_args = args;
4186 tree templates = NULL_TREE;
4187 int template_only = 0;
4189 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4191 explicit_targs = TREE_OPERAND (name, 1);
4192 name = TREE_OPERAND (name, 0);
4193 if (TREE_CODE_CLASS (TREE_CODE (name)) == 'd')
4194 name = DECL_NAME (name);
4195 else
4197 if (TREE_CODE (name) == COMPONENT_REF)
4198 name = TREE_OPERAND (name, 1);
4199 if (TREE_CODE (name) == OVERLOAD)
4200 name = DECL_NAME (OVL_CURRENT (name));
4203 template_only = 1;
4206 /* If there is an extra argument for controlling virtual bases,
4207 remove it for error reporting. */
4208 if (flags & LOOKUP_HAS_IN_CHARGE)
4209 user_args = TREE_CHAIN (args);
4211 args = resolve_args (args);
4213 if (args == error_mark_node)
4214 return error_mark_node;
4216 if (instance == NULL_TREE)
4217 basetype = BINFO_TYPE (basetype_path);
4218 else
4220 if (TREE_CODE (instance) == OFFSET_REF)
4221 instance = resolve_offset_ref (instance);
4222 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4223 instance = convert_from_reference (instance);
4224 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4226 /* XXX this should be handled before we get here. */
4227 if (! IS_AGGR_TYPE (basetype))
4229 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4230 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4231 name, instance, basetype);
4233 return error_mark_node;
4237 if (basetype_path == NULL_TREE)
4238 basetype_path = TYPE_BINFO (basetype);
4240 if (instance)
4242 instance_ptr = build_this (instance);
4244 if (! template_only)
4246 /* XXX this should be handled before we get here. */
4247 fns = build_field_call (basetype_path, instance_ptr, name, args);
4248 if (fns)
4249 return fns;
4252 else
4254 instance_ptr = build_int_2 (0, 0);
4255 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4258 pretty_name
4259 = (name == ctor_identifier ? constructor_name (basetype) : name);
4261 fns = lookup_fnfields (basetype_path, name, 1);
4263 if (fns == error_mark_node)
4264 return error_mark_node;
4265 if (fns)
4267 tree fn = TREE_VALUE (fns);
4268 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
4269 && ! (flags & LOOKUP_HAS_IN_CHARGE))
4271 flags |= LOOKUP_HAS_IN_CHARGE;
4272 args = tree_cons (NULL_TREE, integer_one_node, args);
4274 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4275 for (; fn; fn = OVL_NEXT (fn))
4277 tree t = OVL_CURRENT (fn);
4278 tree this_arglist;
4280 /* We can end up here for copy-init of same or base class. */
4281 if (name == ctor_identifier
4282 && (flags & LOOKUP_ONLYCONVERTING)
4283 && DECL_NONCONVERTING_P (t))
4284 continue;
4285 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
4286 this_arglist = mem_args;
4287 else
4288 this_arglist = args;
4290 if (TREE_CODE (t) == TEMPLATE_DECL)
4292 /* A member template. */
4293 templates = tree_cons (NULL_TREE, t, templates);
4294 candidates =
4295 add_template_candidate (candidates, t, explicit_targs,
4296 this_arglist,
4297 TREE_TYPE (name), flags, DEDUCE_CALL);
4299 else if (! template_only)
4300 candidates = add_function_candidate (candidates, t,
4301 this_arglist, flags);
4303 if (candidates)
4304 candidates->basetype_path = TREE_PURPOSE (fns);
4308 if (! any_viable (candidates))
4310 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4311 if (flags & LOOKUP_SPECULATIVELY)
4312 return NULL_TREE;
4313 if (TYPE_SIZE (basetype) == 0)
4314 incomplete_type_error (instance_ptr, basetype);
4315 else
4316 cp_error ("no matching function for call to `%T::%D (%A)%V'",
4317 basetype, pretty_name, user_args,
4318 TREE_TYPE (TREE_TYPE (instance_ptr)));
4319 print_z_candidates (candidates);
4320 return error_mark_node;
4322 candidates = splice_viable (candidates);
4323 cand = tourney (candidates);
4325 if (cand == 0)
4327 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4328 user_args);
4329 print_z_candidates (candidates);
4330 return error_mark_node;
4333 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
4334 && instance == current_class_ref
4335 && DECL_CONSTRUCTOR_P (current_function_decl)
4336 && ! (flags & LOOKUP_NONVIRTUAL)
4337 && value_member (cand->fn, CLASSTYPE_ABSTRACT_VIRTUALS (basetype)))
4338 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
4339 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4340 && is_dummy_object (instance_ptr))
4342 cp_error ("cannot call member function `%D' without object", cand->fn);
4343 return error_mark_node;
4346 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4347 && ((instance == current_class_ref && (dtor_label || ctor_label))
4348 || resolves_to_fixed_type_p (instance, 0)))
4349 flags |= LOOKUP_NONVIRTUAL;
4351 /* Pedantically, normal function declarations are never considered
4352 to refer to template instantiations, so we only do this with
4353 -fguiding-decls. */
4354 if (flag_guiding_decls && templates && ! cand->template
4355 && ! DECL_INITIAL (cand->fn))
4356 add_maybe_template (cand->fn, templates);
4358 return build_over_call
4359 (cand,
4360 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
4361 flags);
4364 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4365 subsequence of ICS2. */
4367 static int
4368 is_subseq (ics1, ics2)
4369 tree ics1, ics2;
4371 /* We can assume that a conversion of the same code
4372 between the same types indicates a subsequence since we only get
4373 here if the types we are converting from are the same. */
4375 while (TREE_CODE (ics1) == RVALUE_CONV
4376 || TREE_CODE (ics1) == LVALUE_CONV)
4377 ics1 = TREE_OPERAND (ics1, 0);
4379 while (1)
4381 while (TREE_CODE (ics2) == RVALUE_CONV
4382 || TREE_CODE (ics2) == LVALUE_CONV)
4383 ics2 = TREE_OPERAND (ics2, 0);
4385 if (TREE_CODE (ics2) == USER_CONV
4386 || TREE_CODE (ics2) == AMBIG_CONV
4387 || TREE_CODE (ics2) == IDENTITY_CONV)
4388 /* At this point, ICS1 cannot be a proper subsequence of
4389 ICS2. We can get a USER_CONV when we are comparing the
4390 second standard conversion sequence of two user conversion
4391 sequences. */
4392 return 0;
4394 ics2 = TREE_OPERAND (ics2, 0);
4396 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4397 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4398 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4399 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4400 return 1;
4404 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4405 be any _TYPE nodes. */
4408 is_properly_derived_from (derived, base)
4409 tree derived;
4410 tree base;
4412 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4413 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4414 return 0;
4416 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4417 considers every class derived from itself. */
4418 return (!same_type_p (TYPE_MAIN_VARIANT (derived),
4419 TYPE_MAIN_VARIANT (base))
4420 && DERIVED_FROM_P (base, derived));
4423 /* We build the ICS for an implicit object parameter as a pointer
4424 conversion sequence. However, such a sequence should be compared
4425 as if it were a reference conversion sequence. If ICS is the
4426 implicit conversion sequence for an implicit object parameter,
4427 modify it accordingly. */
4429 static void
4430 maybe_handle_implicit_object (ics)
4431 tree* ics;
4433 if (ICS_THIS_FLAG (*ics))
4435 /* [over.match.funcs]
4437 For non-static member functions, the type of the
4438 implicit object parameter is "reference to cv X"
4439 where X is the class of which the function is a
4440 member and cv is the cv-qualification on the member
4441 function declaration. */
4442 tree t = *ics;
4443 tree reference_type;
4445 /* The `this' parameter is a pointer to a class type. Make the
4446 implict conversion talk about a reference to that same class
4447 type. */
4448 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4449 reference_type = build_reference_type (reference_type);
4451 if (TREE_CODE (t) == QUAL_CONV)
4452 t = TREE_OPERAND (t, 0);
4453 if (TREE_CODE (t) == PTR_CONV)
4454 t = TREE_OPERAND (t, 0);
4455 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4456 t = direct_reference_binding (reference_type, t);
4457 *ics = t;
4461 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
4462 to the type the reference originally referred to, and return 1.
4463 Otherwise, return 0. */
4465 static int
4466 maybe_handle_ref_bind (ics, target_type)
4467 tree* ics;
4468 tree* target_type;
4470 if (TREE_CODE (*ics) == REF_BIND)
4472 *target_type = TREE_TYPE (TREE_TYPE (*ics));
4473 *ics = TREE_OPERAND (*ics, 0);
4474 return 1;
4477 return 0;
4480 /* Compare two implicit conversion sequences according to the rules set out in
4481 [over.ics.rank]. Return values:
4483 1: ics1 is better than ics2
4484 -1: ics2 is better than ics1
4485 0: ics1 and ics2 are indistinguishable */
4487 static int
4488 compare_ics (ics1, ics2)
4489 tree ics1, ics2;
4491 tree from_type1;
4492 tree from_type2;
4493 tree to_type1;
4494 tree to_type2;
4495 tree deref_from_type1 = NULL_TREE;
4496 tree deref_from_type2 = NULL_TREE;
4497 tree deref_to_type1 = NULL_TREE;
4498 tree deref_to_type2 = NULL_TREE;
4500 /* REF_BINDING is non-zero if the result of the conversion sequence
4501 is a reference type. In that case TARGET_TYPE is the
4502 type referred to by the reference. */
4503 int ref_binding1;
4504 int ref_binding2;
4505 tree target_type1;
4506 tree target_type2;
4508 /* Handle implicit object parameters. */
4509 maybe_handle_implicit_object (&ics1);
4510 maybe_handle_implicit_object (&ics2);
4512 /* Handle reference parameters. */
4513 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
4514 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
4516 /* [over.ics.rank]
4518 When comparing the basic forms of implicit conversion sequences (as
4519 defined in _over.best.ics_)
4521 --a standard conversion sequence (_over.ics.scs_) is a better
4522 conversion sequence than a user-defined conversion sequence
4523 or an ellipsis conversion sequence, and
4525 --a user-defined conversion sequence (_over.ics.user_) is a
4526 better conversion sequence than an ellipsis conversion sequence
4527 (_over.ics.ellipsis_). */
4528 if (ICS_RANK (ics1) > ICS_RANK (ics2))
4529 return -1;
4530 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
4531 return 1;
4533 if (ICS_RANK (ics1) == BAD_RANK)
4535 /* Both ICS are bad. We try to make a decision based on what
4536 would have happenned if they'd been good. */
4537 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4538 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4539 return -1;
4540 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4541 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4542 return 1;
4544 /* We couldn't make up our minds; try to figure it out below. */
4547 if (ICS_ELLIPSIS_FLAG (ics1))
4548 /* Both conversions are ellipsis conversions. */
4549 return 0;
4551 /* User-defined conversion sequence U1 is a better conversion sequence
4552 than another user-defined conversion sequence U2 if they contain the
4553 same user-defined conversion operator or constructor and if the sec-
4554 ond standard conversion sequence of U1 is better than the second
4555 standard conversion sequence of U2. */
4557 if (ICS_USER_FLAG (ics1))
4559 tree t1, t2;
4561 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4562 if (TREE_CODE (t1) == AMBIG_CONV)
4563 return 0;
4564 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4565 if (TREE_CODE (t2) == AMBIG_CONV)
4566 return 0;
4568 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4569 return 0;
4571 /* We can just fall through here, after setting up
4572 FROM_TYPE1 and FROM_TYPE2. */
4573 from_type1 = TREE_TYPE (t1);
4574 from_type2 = TREE_TYPE (t2);
4576 else
4578 /* We're dealing with two standard conversion sequences.
4580 [over.ics.rank]
4582 Standard conversion sequence S1 is a better conversion
4583 sequence than standard conversion sequence S2 if
4585 --S1 is a proper subsequence of S2 (comparing the conversion
4586 sequences in the canonical form defined by _over.ics.scs_,
4587 excluding any Lvalue Transformation; the identity
4588 conversion sequence is considered to be a subsequence of
4589 any non-identity conversion sequence */
4591 from_type1 = ics1;
4592 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4593 from_type1 = TREE_OPERAND (from_type1, 0);
4594 from_type1 = TREE_TYPE (from_type1);
4596 from_type2 = ics2;
4597 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4598 from_type2 = TREE_OPERAND (from_type2, 0);
4599 from_type2 = TREE_TYPE (from_type2);
4602 if (same_type_p (from_type1, from_type2))
4604 if (is_subseq (ics1, ics2))
4605 return 1;
4606 if (is_subseq (ics2, ics1))
4607 return -1;
4609 /* Otherwise, one sequence cannot be a subsequence of the other; they
4610 don't start with the same type. This can happen when comparing the
4611 second standard conversion sequence in two user-defined conversion
4612 sequences. */
4614 /* [over.ics.rank]
4616 Or, if not that,
4618 --the rank of S1 is better than the rank of S2 (by the rules
4619 defined below):
4621 Standard conversion sequences are ordered by their ranks: an Exact
4622 Match is a better conversion than a Promotion, which is a better
4623 conversion than a Conversion.
4625 Two conversion sequences with the same rank are indistinguishable
4626 unless one of the following rules applies:
4628 --A conversion that is not a conversion of a pointer, or pointer
4629 to member, to bool is better than another conversion that is such
4630 a conversion.
4632 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4633 so that we do not have to check it explicitly. */
4634 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4635 return 1;
4636 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4637 return -1;
4639 to_type1 = TREE_TYPE (ics1);
4640 to_type2 = TREE_TYPE (ics2);
4642 if (TYPE_PTR_P (from_type1)
4643 && TYPE_PTR_P (from_type2)
4644 && TYPE_PTR_P (to_type1)
4645 && TYPE_PTR_P (to_type2))
4647 deref_from_type1 = TREE_TYPE (from_type1);
4648 deref_from_type2 = TREE_TYPE (from_type2);
4649 deref_to_type1 = TREE_TYPE (to_type1);
4650 deref_to_type2 = TREE_TYPE (to_type2);
4652 /* The rules for pointers to members A::* are just like the rules
4653 for pointers A*, except opposite: if B is derived from A then
4654 A::* converts to B::*, not vice versa. For that reason, we
4655 switch the from_ and to_ variables here. */
4656 else if (TYPE_PTRMEM_P (from_type1)
4657 && TYPE_PTRMEM_P (from_type2)
4658 && TYPE_PTRMEM_P (to_type1)
4659 && TYPE_PTRMEM_P (to_type2))
4661 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4662 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4663 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4664 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4666 else if (TYPE_PTRMEMFUNC_P (from_type1)
4667 && TYPE_PTRMEMFUNC_P (from_type2)
4668 && TYPE_PTRMEMFUNC_P (to_type1)
4669 && TYPE_PTRMEMFUNC_P (to_type2))
4671 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4672 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4673 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4674 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4677 if (deref_from_type1 != NULL_TREE
4678 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4679 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4681 /* This was one of the pointer or pointer-like conversions.
4683 [over.ics.rank]
4685 --If class B is derived directly or indirectly from class A,
4686 conversion of B* to A* is better than conversion of B* to
4687 void*, and conversion of A* to void* is better than
4688 conversion of B* to void*. */
4689 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4690 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4692 if (is_properly_derived_from (deref_from_type1,
4693 deref_from_type2))
4694 return -1;
4695 else if (is_properly_derived_from (deref_from_type2,
4696 deref_from_type1))
4697 return 1;
4699 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4700 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4702 if (same_type_p (deref_from_type1, deref_from_type2))
4704 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4706 if (is_properly_derived_from (deref_from_type1,
4707 deref_to_type1))
4708 return 1;
4710 /* We know that DEREF_TO_TYPE1 is `void' here. */
4711 else if (is_properly_derived_from (deref_from_type1,
4712 deref_to_type2))
4713 return -1;
4716 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4717 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4719 /* [over.ics.rank]
4721 --If class B is derived directly or indirectly from class A
4722 and class C is derived directly or indirectly from B,
4724 --conversion of C* to B* is better than conversion of C* to
4725 A*,
4727 --conversion of B* to A* is better than conversion of C* to
4728 A* */
4729 if (same_type_p (deref_from_type1, deref_from_type2))
4731 if (is_properly_derived_from (deref_to_type1,
4732 deref_to_type2))
4733 return 1;
4734 else if (is_properly_derived_from (deref_to_type2,
4735 deref_to_type1))
4736 return -1;
4738 else if (same_type_p (deref_to_type1, deref_to_type2))
4740 if (is_properly_derived_from (deref_from_type2,
4741 deref_from_type1))
4742 return 1;
4743 else if (is_properly_derived_from (deref_from_type1,
4744 deref_from_type2))
4745 return -1;
4749 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4750 && same_type_p (from_type1, from_type2))
4752 /* [over.ics.rank]
4754 --binding of an expression of type C to a reference of type
4755 B& is better than binding an expression of type C to a
4756 reference of type A&
4758 --conversion of C to B is better than conversion of C to A, */
4759 if (is_properly_derived_from (from_type1, to_type1)
4760 && is_properly_derived_from (from_type1, to_type2))
4762 if (is_properly_derived_from (to_type1, to_type2))
4763 return 1;
4764 else if (is_properly_derived_from (to_type2, to_type1))
4765 return -1;
4768 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4769 && same_type_p (to_type1, to_type2))
4771 /* [over.ics.rank]
4773 --binding of an expression of type B to a reference of type
4774 A& is better than binding an expression of type C to a
4775 reference of type A&,
4777 --onversion of B to A is better than conversion of C to A */
4778 if (is_properly_derived_from (from_type1, to_type1)
4779 && is_properly_derived_from (from_type2, to_type1))
4781 if (is_properly_derived_from (from_type2, from_type1))
4782 return 1;
4783 else if (is_properly_derived_from (from_type1, from_type2))
4784 return -1;
4788 /* [over.ics.rank]
4790 --S1 and S2 differ only in their qualification conversion and yield
4791 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4792 qualification signature of type T1 is a proper subset of the cv-
4793 qualification signature of type T2 */
4794 if (TREE_CODE (ics1) == QUAL_CONV
4795 && TREE_CODE (ics2) == QUAL_CONV
4796 && same_type_p (from_type1, from_type2))
4797 return comp_cv_qual_signature (to_type1, to_type2);
4799 /* [over.ics.rank]
4801 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4802 types to which the references refer are the same type except for
4803 top-level cv-qualifiers, and the type to which the reference
4804 initialized by S2 refers is more cv-qualified than the type to
4805 which the reference initialized by S1 refers */
4807 if (ref_binding1 && ref_binding2
4808 && same_type_p (TYPE_MAIN_VARIANT (to_type1),
4809 TYPE_MAIN_VARIANT (to_type2)))
4810 return comp_cv_qualification (target_type2, target_type1);
4812 /* Neither conversion sequence is better than the other. */
4813 return 0;
4816 /* The source type for this standard conversion sequence. */
4818 static tree
4819 source_type (t)
4820 tree t;
4822 for (;; t = TREE_OPERAND (t, 0))
4824 if (TREE_CODE (t) == USER_CONV
4825 || TREE_CODE (t) == AMBIG_CONV
4826 || TREE_CODE (t) == IDENTITY_CONV)
4827 return TREE_TYPE (t);
4829 my_friendly_abort (1823);
4832 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4833 a pointer to LOSER and re-running joust to produce the warning if WINNER
4834 is actually used. */
4836 static void
4837 add_warning (winner, loser)
4838 struct z_candidate *winner, *loser;
4840 winner->warnings = tree_cons (NULL_PTR,
4841 build_expr_ptr_wrapper (loser),
4842 winner->warnings);
4845 /* Returns true iff functions are equivalent. Equivalent functions are
4846 not identical only if one is a function-local extern function.
4847 This assumes that function-locals don't have TREE_PERMANENT. */
4849 static inline int
4850 equal_functions (fn1, fn2)
4851 tree fn1;
4852 tree fn2;
4854 if (!TREE_PERMANENT (fn1) || !TREE_PERMANENT (fn2))
4855 return decls_match (fn1, fn2);
4856 return fn1 == fn2;
4859 /* Compare two candidates for overloading as described in
4860 [over.match.best]. Return values:
4862 1: cand1 is better than cand2
4863 -1: cand2 is better than cand1
4864 0: cand1 and cand2 are indistinguishable */
4866 static int
4867 joust (cand1, cand2, warn)
4868 struct z_candidate *cand1, *cand2;
4869 int warn;
4871 int winner = 0;
4872 int i, off1 = 0, off2 = 0, len;
4874 /* Candidates that involve bad conversions are always worse than those
4875 that don't. */
4876 if (cand1->viable > cand2->viable)
4877 return 1;
4878 if (cand1->viable < cand2->viable)
4879 return -1;
4881 /* If we have two pseudo-candidates for conversions to the same type,
4882 arbitrarily pick one. */
4883 if (TYPE_P (cand1->fn) && cand1->fn == cand2->fn)
4884 return 1;
4886 /* a viable function F1
4887 is defined to be a better function than another viable function F2 if
4888 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4889 ICSi(F2), and then */
4891 /* for some argument j, ICSj(F1) is a better conversion sequence than
4892 ICSj(F2) */
4894 /* For comparing static and non-static member functions, we ignore the
4895 implicit object parameter of the non-static function. The WP says to
4896 pretend that the static function has an object parm, but that won't
4897 work with operator overloading. */
4898 len = TREE_VEC_LENGTH (cand1->convs);
4899 if (len != TREE_VEC_LENGTH (cand2->convs))
4901 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4902 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4903 off2 = 1;
4904 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4905 && DECL_STATIC_FUNCTION_P (cand2->fn))
4907 off1 = 1;
4908 --len;
4910 else
4911 my_friendly_abort (42);
4914 for (i = 0; i < len; ++i)
4916 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4917 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4918 int comp = compare_ics (t1, t2);
4920 if (comp != 0)
4922 if (warn_sign_promo
4923 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4924 && TREE_CODE (t1) == STD_CONV
4925 && TREE_CODE (t2) == STD_CONV
4926 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4927 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4928 && (TYPE_PRECISION (TREE_TYPE (t1))
4929 == TYPE_PRECISION (TREE_TYPE (t2)))
4930 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4931 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4932 == ENUMERAL_TYPE)))
4934 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4935 tree type1, type2;
4936 struct z_candidate *w, *l;
4937 if (comp > 0)
4938 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4939 w = cand1, l = cand2;
4940 else
4941 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4942 w = cand2, l = cand1;
4944 if (warn)
4946 cp_warning ("passing `%T' chooses `%T' over `%T'",
4947 type, type1, type2);
4948 cp_warning (" in call to `%D'", w->fn);
4950 else
4951 add_warning (w, l);
4954 if (winner && comp != winner)
4956 winner = 0;
4957 goto tweak;
4959 winner = comp;
4963 /* warn about confusing overload resolution for user-defined conversions,
4964 either between a constructor and a conversion op, or between two
4965 conversion ops. */
4966 if (winner && cand1->second_conv
4967 && ((DECL_CONSTRUCTOR_P (cand1->fn)
4968 != DECL_CONSTRUCTOR_P (cand2->fn))
4969 /* Don't warn if the two conv ops convert to the same type... */
4970 || (! DECL_CONSTRUCTOR_P (cand1->fn)
4971 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
4972 TREE_TYPE (TREE_TYPE (cand2->fn))))))
4974 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4975 if (comp != winner)
4977 struct z_candidate *w, *l;
4978 if (winner == 1)
4979 w = cand1, l = cand2;
4980 else
4981 w = cand2, l = cand1;
4982 if (warn)
4984 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4985 if (! DECL_CONSTRUCTOR_P (w->fn))
4986 source = TREE_TYPE (source);
4987 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4988 cp_warning (" for conversion from `%T' to `%T'",
4989 source, TREE_TYPE (w->second_conv));
4990 cp_warning (" because conversion sequence for the argument is better");
4992 else
4993 add_warning (w, l);
4997 if (winner)
4998 return winner;
5000 /* or, if not that,
5001 F1 is a non-template function and F2 is a template function */
5003 if (! cand1->template && cand2->template)
5004 return 1;
5005 else if (cand1->template && ! cand2->template)
5006 return -1;
5007 else if (cand1->template && cand2->template)
5008 winner = more_specialized
5009 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5010 NULL_TREE);
5012 /* or, if not that,
5013 the context is an initialization by user-defined conversion (see
5014 _dcl.init_ and _over.match.user_) and the standard conversion
5015 sequence from the return type of F1 to the destination type (i.e.,
5016 the type of the entity being initialized) is a better conversion
5017 sequence than the standard conversion sequence from the return type
5018 of F2 to the destination type. */
5020 if (! winner && cand1->second_conv)
5021 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5023 /* If the built-in candidates are the same, arbitrarily pick one. */
5024 if (! winner && cand1->fn == cand2->fn
5025 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5027 for (i = 0; i < len; ++i)
5028 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5029 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5030 break;
5031 if (i == TREE_VEC_LENGTH (cand1->convs))
5032 return 1;
5034 /* Kludge around broken overloading rules whereby
5035 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
5036 that takes references and another that takes values. */
5037 if (cand1->fn == ansi_opname[COND_EXPR])
5039 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
5040 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
5041 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
5042 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
5044 if (same_type_p (t1, t2))
5046 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
5047 return 1;
5048 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
5049 return -1;
5054 /* If the two functions are the same (this can happen with declarations
5055 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5056 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5057 && equal_functions (cand1->fn, cand2->fn))
5058 return 1;
5060 tweak:
5062 /* Extension: If the worst conversion for one candidate is worse than the
5063 worst conversion for the other, take the first. */
5064 if (! winner && ! pedantic)
5066 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5068 for (i = 0; i < len; ++i)
5070 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5071 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5072 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5073 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5076 if (rank1 < rank2)
5077 return 1;
5078 if (rank1 > rank2)
5079 return -1;
5082 return winner;
5085 /* Given a list of candidates for overloading, find the best one, if any.
5086 This algorithm has a worst case of O(2n) (winner is last), and a best
5087 case of O(n/2) (totally ambiguous); much better than a sorting
5088 algorithm. */
5090 static struct z_candidate *
5091 tourney (candidates)
5092 struct z_candidate *candidates;
5094 struct z_candidate *champ = candidates, *challenger;
5095 int fate;
5096 int champ_compared_to_predecessor = 0;
5098 /* Walk through the list once, comparing each current champ to the next
5099 candidate, knocking out a candidate or two with each comparison. */
5101 for (challenger = champ->next; challenger; )
5103 fate = joust (champ, challenger, 0);
5104 if (fate == 1)
5105 challenger = challenger->next;
5106 else
5108 if (fate == 0)
5110 champ = challenger->next;
5111 if (champ == 0)
5112 return 0;
5113 champ_compared_to_predecessor = 0;
5115 else
5117 champ = challenger;
5118 champ_compared_to_predecessor = 1;
5121 challenger = champ->next;
5125 /* Make sure the champ is better than all the candidates it hasn't yet
5126 been compared to. */
5128 for (challenger = candidates;
5129 challenger != champ
5130 && !(champ_compared_to_predecessor && challenger->next == champ);
5131 challenger = challenger->next)
5133 fate = joust (champ, challenger, 0);
5134 if (fate != 1)
5135 return 0;
5138 return champ;
5141 /* Returns non-zero if things of type FROM can be converted to TO. */
5144 can_convert (to, from)
5145 tree to, from;
5147 return can_convert_arg (to, from, NULL_TREE);
5150 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5153 can_convert_arg (to, from, arg)
5154 tree to, from, arg;
5156 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5157 return (t && ! ICS_BAD_FLAG (t));
5160 /* Convert EXPR to TYPE. Return the converted expression. */
5162 tree
5163 perform_implicit_conversion (type, expr)
5164 tree type;
5165 tree expr;
5167 tree conv;
5169 if (expr == error_mark_node)
5170 return error_mark_node;
5171 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5172 LOOKUP_NORMAL);
5173 if (!conv || ICS_BAD_FLAG (conv))
5175 cp_error ("could not convert `%E' to `%T'", expr, type);
5176 return error_mark_node;
5179 return convert_like (conv, expr);
5182 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5183 initializing a variable of that TYPE. Return the converted
5184 expression. */
5186 tree
5187 initialize_reference (type, expr)
5188 tree type;
5189 tree expr;
5191 tree conv;
5193 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5194 if (!conv || ICS_BAD_FLAG (conv))
5196 cp_error ("could not convert `%E' to `%T'", expr, type);
5197 return error_mark_node;
5200 return convert_like (conv, expr);