2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / cp / call.c
blob2dc4f2240c640e0154b5efa43e93efcb3ff653be
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) and
5 modified by Brendan Kehoe (brendan@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 /* High-level class interface. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "output.h"
34 #include "flags.h"
35 #include "rtl.h"
36 #include "toplev.h"
37 #include "expr.h"
38 #include "diagnostic.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "convert.h"
43 static tree build_field_call (tree, tree, tree);
44 static struct z_candidate * tourney (struct z_candidate *);
45 static int equal_functions (tree, tree);
46 static int joust (struct z_candidate *, struct z_candidate *, bool);
47 static int compare_ics (tree, tree);
48 static tree build_over_call (struct z_candidate *, int);
49 static tree build_java_interface_fn_ref (tree, tree);
50 #define convert_like(CONV, EXPR) \
51 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \
52 /*issue_conversion_warnings=*/true)
53 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
54 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
55 /*issue_conversion_warnings=*/true)
56 static tree convert_like_real (tree, tree, tree, int, int, bool);
57 static void op_error (enum tree_code, enum tree_code, tree, tree,
58 tree, const char *);
59 static tree build_object_call (tree, tree);
60 static tree resolve_args (tree);
61 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
62 static void print_z_candidate (const char *, struct z_candidate *);
63 static void print_z_candidates (struct z_candidate *);
64 static tree build_this (tree);
65 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
66 static bool any_strictly_viable (struct z_candidate *);
67 static struct z_candidate *add_template_candidate
68 (struct z_candidate **, tree, tree, tree, tree, tree,
69 tree, tree, int, unification_kind_t);
70 static struct z_candidate *add_template_candidate_real
71 (struct z_candidate **, tree, tree, tree, tree, tree,
72 tree, tree, int, tree, unification_kind_t);
73 static struct z_candidate *add_template_conv_candidate
74 (struct z_candidate **, tree, tree, tree, tree, tree, tree);
75 static void add_builtin_candidates
76 (struct z_candidate **, enum tree_code, enum tree_code,
77 tree, tree *, int);
78 static void add_builtin_candidate
79 (struct z_candidate **, enum tree_code, enum tree_code,
80 tree, tree, tree, tree *, tree *, int);
81 static bool is_complete (tree);
82 static void build_builtin_candidate
83 (struct z_candidate **, tree, tree, tree, tree *, tree *,
84 int);
85 static struct z_candidate *add_conv_candidate
86 (struct z_candidate **, tree, tree, tree, tree, tree);
87 static struct z_candidate *add_function_candidate
88 (struct z_candidate **, tree, tree, tree, tree, tree, int);
89 static tree implicit_conversion (tree, tree, tree, int);
90 static tree standard_conversion (tree, tree, tree);
91 static tree reference_binding (tree, tree, tree, int);
92 static tree build_conv (enum tree_code, tree, tree);
93 static bool is_subseq (tree, tree);
94 static tree maybe_handle_ref_bind (tree *);
95 static void maybe_handle_implicit_object (tree *);
96 static struct z_candidate *add_candidate
97 (struct z_candidate **, tree, tree, tree, tree, tree, int);
98 static tree source_type (tree);
99 static void add_warning (struct z_candidate *, struct z_candidate *);
100 static bool reference_related_p (tree, tree);
101 static bool reference_compatible_p (tree, tree);
102 static tree convert_class_to_reference (tree, tree, tree);
103 static tree direct_reference_binding (tree, tree);
104 static bool promoted_arithmetic_type_p (tree);
105 static tree conditional_conversion (tree, tree);
106 static char *name_as_c_string (tree, tree, bool *);
107 static tree call_builtin_trap (tree);
108 static tree prep_operand (tree);
109 static void add_candidates (tree, tree, tree, bool, tree, tree,
110 int, struct z_candidate **);
111 static tree merge_conversion_sequences (tree, tree);
112 static bool magic_varargs_p (tree);
114 tree
115 build_vfield_ref (tree datum, tree type)
117 if (datum == error_mark_node)
118 return error_mark_node;
120 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
121 datum = convert_from_reference (datum);
123 if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
124 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
125 datum = convert_to_base (datum, type, /*check_access=*/false);
127 return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
128 datum, TYPE_VFIELD (type));
131 /* Build a call to a member of an object. I.e., one that overloads
132 operator ()(), or is a pointer-to-function or pointer-to-method. */
134 static tree
135 build_field_call (tree instance_ptr, tree decl, tree parms)
137 tree instance;
139 if (decl == error_mark_node || decl == NULL_TREE)
140 return decl;
142 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
144 /* If it's a field, try overloading operator (),
145 or calling if the field is a pointer-to-function. */
146 instance = build_indirect_ref (instance_ptr, NULL);
147 instance = build_class_member_access_expr (instance, decl,
148 /*access_path=*/NULL_TREE,
149 /*preserve_reference=*/false);
151 if (instance == error_mark_node)
152 return error_mark_node;
154 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
155 return build_new_op (CALL_EXPR, LOOKUP_NORMAL,
156 instance, parms, NULL_TREE);
157 else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
158 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
159 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
160 == FUNCTION_TYPE)))
161 return build_function_call (instance, parms);
164 return NULL_TREE;
167 /* Returns nonzero iff the destructor name specified in NAME
168 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
169 forms... */
171 bool
172 check_dtor_name (tree basetype, tree name)
174 name = TREE_OPERAND (name, 0);
176 /* Just accept something we've already complained about. */
177 if (name == error_mark_node)
178 return true;
180 if (TREE_CODE (name) == TYPE_DECL)
181 name = TREE_TYPE (name);
182 else if (TYPE_P (name))
183 /* OK */;
184 else if (TREE_CODE (name) == IDENTIFIER_NODE)
186 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
187 || (TREE_CODE (basetype) == ENUMERAL_TYPE
188 && name == TYPE_IDENTIFIER (basetype)))
189 name = basetype;
190 else
191 name = get_type_value (name);
193 /* In the case of:
195 template <class T> struct S { ~S(); };
196 int i;
197 i.~S();
199 NAME will be a class template. */
200 else if (DECL_CLASS_TEMPLATE_P (name))
201 return false;
202 else
203 abort ();
205 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
206 return true;
207 return false;
210 /* We want the address of a function or method. We avoid creating a
211 pointer-to-member function. */
213 tree
214 build_addr_func (tree function)
216 tree type = TREE_TYPE (function);
218 /* We have to do these by hand to avoid real pointer to member
219 functions. */
220 if (TREE_CODE (type) == METHOD_TYPE)
222 if (TREE_CODE (function) == OFFSET_REF)
224 tree object = build_address (TREE_OPERAND (function, 0));
225 return get_member_function_from_ptrfunc (&object,
226 TREE_OPERAND (function, 1));
228 function = build_address (function);
230 else
231 function = decay_conversion (function);
233 return function;
236 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
237 POINTER_TYPE to those. Note, pointer to member function types
238 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
240 tree
241 build_call (tree function, tree parms)
243 int is_constructor = 0;
244 int nothrow;
245 tree tmp;
246 tree decl;
247 tree result_type;
248 tree fntype;
250 function = build_addr_func (function);
252 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
254 sorry ("unable to call pointer to member function here");
255 return error_mark_node;
258 fntype = TREE_TYPE (TREE_TYPE (function));
259 result_type = TREE_TYPE (fntype);
261 if (TREE_CODE (function) == ADDR_EXPR
262 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
263 decl = TREE_OPERAND (function, 0);
264 else
265 decl = NULL_TREE;
267 /* We check both the decl and the type; a function may be known not to
268 throw without being declared throw(). */
269 nothrow = ((decl && TREE_NOTHROW (decl))
270 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
272 if (decl && TREE_THIS_VOLATILE (decl) && cfun)
273 current_function_returns_abnormally = 1;
275 if (decl && TREE_DEPRECATED (decl))
276 warn_deprecated_use (decl);
277 require_complete_eh_spec_types (fntype, decl);
279 if (decl && DECL_CONSTRUCTOR_P (decl))
280 is_constructor = 1;
282 if (decl && ! TREE_USED (decl))
284 /* We invoke build_call directly for several library functions.
285 These may have been declared normally if we're building libgcc,
286 so we can't just check DECL_ARTIFICIAL. */
287 if (DECL_ARTIFICIAL (decl)
288 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
289 mark_used (decl);
290 else
291 abort ();
294 /* Don't pass empty class objects by value. This is useful
295 for tags in STL, which are used to control overload resolution.
296 We don't need to handle other cases of copying empty classes. */
297 if (! decl || ! DECL_BUILT_IN (decl))
298 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
299 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
300 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
302 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
303 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
304 TREE_VALUE (tmp), t);
307 function = build (CALL_EXPR, result_type, function, parms);
308 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
309 TREE_NOTHROW (function) = nothrow;
311 return function;
314 /* Build something of the form ptr->method (args)
315 or object.method (args). This can also build
316 calls to constructors, and find friends.
318 Member functions always take their class variable
319 as a pointer.
321 INSTANCE is a class instance.
323 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
325 PARMS help to figure out what that NAME really refers to.
327 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
328 down to the real instance type to use for access checking. We need this
329 information to get protected accesses correct.
331 FLAGS is the logical disjunction of zero or more LOOKUP_
332 flags. See cp-tree.h for more info.
334 If this is all OK, calls build_function_call with the resolved
335 member function.
337 This function must also handle being called to perform
338 initialization, promotion/coercion of arguments, and
339 instantiation of default parameters.
341 Note that NAME may refer to an instance variable name. If
342 `operator()()' is defined for the type of that field, then we return
343 that result. */
345 #ifdef GATHER_STATISTICS
346 extern int n_build_method_call;
347 #endif
349 tree
350 build_method_call (tree instance, tree name, tree parms,
351 tree basetype_path, int flags)
353 tree fn;
354 tree object_type;
355 tree template_args = NULL_TREE;
356 bool has_template_args = false;
358 #ifdef GATHER_STATISTICS
359 n_build_method_call++;
360 #endif
362 if (error_operand_p (instance)
363 || name == error_mark_node
364 || parms == error_mark_node)
365 return error_mark_node;
367 my_friendly_assert (!processing_template_decl, 20030707);
369 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
370 instance = convert_from_reference (instance);
371 object_type = TREE_TYPE (instance);
373 if (TREE_CODE (name) == BIT_NOT_EXPR)
375 tree instance_ptr;
377 if (parms)
378 error ("destructors take no parameters");
380 if (! check_dtor_name (object_type, name))
381 error
382 ("destructor name `~%T' does not match type `%T' of expression",
383 TREE_OPERAND (name, 0), object_type);
385 if (! TYPE_HAS_DESTRUCTOR (complete_type (object_type)))
386 return convert_to_void (instance, /*implicit=*/NULL);
387 instance = default_conversion (instance);
388 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
389 return build_delete (build_pointer_type (object_type),
390 instance_ptr, sfk_complete_destructor,
391 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
394 if (!CLASS_TYPE_P (object_type))
396 if ((flags & LOOKUP_COMPLAIN)
397 && TREE_TYPE (instance) != error_mark_node)
398 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
399 name, instance, object_type);
400 return error_mark_node;
403 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
405 template_args = TREE_OPERAND (name, 1);
406 has_template_args = true;
407 name = TREE_OPERAND (name, 0);
409 if (TREE_CODE (name) == OVERLOAD)
410 name = DECL_NAME (get_first_fn (name));
411 else if (DECL_P (name))
412 name = DECL_NAME (name);
413 if (has_template_args)
414 fn = lookup_fnfields (object_type, name, /*protect=*/2);
415 else
416 fn = lookup_member (object_type, name, /*protect=*/2, /*want_type=*/false);
418 if (fn && TREE_CODE (fn) == TREE_LIST)
420 error ("request for member `%D' is ambiguous", name);
421 print_candidates (fn);
422 return error_mark_node;
425 /* If the name could not be found, issue an error. */
426 if (!fn)
427 return unqualified_name_lookup_error (name);
429 if (BASELINK_P (fn) && has_template_args)
430 BASELINK_FUNCTIONS (fn)
431 = build_nt (TEMPLATE_ID_EXPR,
432 BASELINK_FUNCTIONS (fn),
433 template_args);
434 if (BASELINK_P (fn) && basetype_path)
435 BASELINK_ACCESS_BINFO (fn) = basetype_path;
437 return build_new_method_call (instance, fn, parms,
438 /*conversion_path=*/NULL_TREE, flags);
441 /* New overloading code. */
443 struct z_candidate GTY(()) {
444 /* The FUNCTION_DECL that will be called if this candidate is
445 selected by overload resolution. */
446 tree fn;
447 /* The arguments to use when calling this function. */
448 tree args;
449 /* The implicit conversion sequences for each of the arguments to
450 FN. */
451 tree convs;
452 /* If FN is a user-defined conversion, the standard conversion
453 sequence from the type returned by FN to the desired destination
454 type. */
455 tree second_conv;
456 int viable;
457 /* If FN is a member function, the binfo indicating the path used to
458 qualify the name of FN at the call site. This path is used to
459 determine whether or not FN is accessible if it is selected by
460 overload resolution. The DECL_CONTEXT of FN will always be a
461 (possibly improper) base of this binfo. */
462 tree access_path;
463 /* If FN is a non-static member function, the binfo indicating the
464 subobject to which the `this' pointer should be converted if FN
465 is selected by overload resolution. The type pointed to the by
466 the `this' pointer must correspond to the most derived class
467 indicated by the CONVERSION_PATH. */
468 tree conversion_path;
469 tree template;
470 tree warnings;
471 struct z_candidate *next;
474 #define IDENTITY_RANK 0
475 #define EXACT_RANK 1
476 #define PROMO_RANK 2
477 #define STD_RANK 3
478 #define PBOOL_RANK 4
479 #define USER_RANK 5
480 #define ELLIPSIS_RANK 6
481 #define BAD_RANK 7
483 #define ICS_RANK(NODE) \
484 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
485 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
486 : ICS_USER_FLAG (NODE) ? USER_RANK \
487 : ICS_STD_RANK (NODE))
489 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
491 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
492 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
493 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
494 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
496 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
497 should be created to hold the result of the conversion. */
498 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
500 #define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
501 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
503 bool
504 null_ptr_cst_p (tree t)
506 /* [conv.ptr]
508 A null pointer constant is an integral constant expression
509 (_expr.const_) rvalue of integer type that evaluates to zero. */
510 if (t == null_node
511 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
512 return true;
513 return false;
517 /* Returns nonzero if PARMLIST consists of only default parms and/or
518 ellipsis. */
520 bool
521 sufficient_parms_p (tree parmlist)
523 for (; parmlist && parmlist != void_list_node;
524 parmlist = TREE_CHAIN (parmlist))
525 if (!TREE_PURPOSE (parmlist))
526 return false;
527 return true;
530 static tree
531 build_conv (enum tree_code code, tree type, tree from)
533 tree t;
534 int rank = ICS_STD_RANK (from);
536 /* We can't use buildl1 here because CODE could be USER_CONV, which
537 takes two arguments. In that case, the caller is responsible for
538 filling in the second argument. */
539 t = make_node (code);
540 TREE_TYPE (t) = type;
541 TREE_OPERAND (t, 0) = from;
543 switch (code)
545 case PTR_CONV:
546 case PMEM_CONV:
547 case BASE_CONV:
548 case STD_CONV:
549 if (rank < STD_RANK)
550 rank = STD_RANK;
551 break;
553 case QUAL_CONV:
554 if (rank < EXACT_RANK)
555 rank = EXACT_RANK;
557 default:
558 break;
560 ICS_STD_RANK (t) = rank;
561 ICS_USER_FLAG (t) = (code == USER_CONV || ICS_USER_FLAG (from));
562 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
563 return t;
566 tree
567 strip_top_quals (tree t)
569 if (TREE_CODE (t) == ARRAY_TYPE)
570 return t;
571 return cp_build_qualified_type (t, 0);
574 /* Returns the standard conversion path (see [conv]) from type FROM to type
575 TO, if any. For proper handling of null pointer constants, you must
576 also pass the expression EXPR to convert from. */
578 static tree
579 standard_conversion (tree to, tree from, tree expr)
581 enum tree_code fcode, tcode;
582 tree conv;
583 bool fromref = false;
585 to = non_reference (to);
586 if (TREE_CODE (from) == REFERENCE_TYPE)
588 fromref = true;
589 from = TREE_TYPE (from);
591 to = strip_top_quals (to);
592 from = strip_top_quals (from);
594 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
595 && expr && type_unknown_p (expr))
597 expr = instantiate_type (to, expr, tf_conv);
598 if (expr == error_mark_node)
599 return NULL_TREE;
600 from = TREE_TYPE (expr);
603 fcode = TREE_CODE (from);
604 tcode = TREE_CODE (to);
606 conv = build1 (IDENTITY_CONV, from, expr);
608 if (fcode == FUNCTION_TYPE)
610 from = build_pointer_type (from);
611 fcode = TREE_CODE (from);
612 conv = build_conv (LVALUE_CONV, from, conv);
614 else if (fcode == ARRAY_TYPE)
616 from = build_pointer_type (TREE_TYPE (from));
617 fcode = TREE_CODE (from);
618 conv = build_conv (LVALUE_CONV, from, conv);
620 else if (fromref || (expr && lvalue_p (expr)))
621 conv = build_conv (RVALUE_CONV, from, conv);
623 /* Allow conversion between `__complex__' data types. */
624 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
626 /* The standard conversion sequence to convert FROM to TO is
627 the standard conversion sequence to perform componentwise
628 conversion. */
629 tree part_conv = standard_conversion
630 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
632 if (part_conv)
634 conv = build_conv (TREE_CODE (part_conv), to, conv);
635 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
637 else
638 conv = NULL_TREE;
640 return conv;
643 if (same_type_p (from, to))
644 return conv;
646 if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to))
647 && expr && null_ptr_cst_p (expr))
648 conv = build_conv (STD_CONV, to, conv);
649 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE
650 && TREE_CODE (TREE_TYPE (to)) == VECTOR_TYPE
651 && TREE_CODE (TREE_TYPE (from)) == VECTOR_TYPE
652 && ((*targetm.vector_opaque_p) (TREE_TYPE (to))
653 || (*targetm.vector_opaque_p) (TREE_TYPE (from))))
654 conv = build_conv (STD_CONV, to, conv);
655 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
656 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
658 /* For backwards brain damage compatibility, allow interconversion of
659 pointers and integers with a pedwarn. */
660 conv = build_conv (STD_CONV, to, conv);
661 ICS_BAD_FLAG (conv) = 1;
663 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE)
665 /* For backwards brain damage compatibility, allow interconversion of
666 enums and integers with a pedwarn. */
667 conv = build_conv (STD_CONV, to, conv);
668 ICS_BAD_FLAG (conv) = 1;
670 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
671 || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
673 tree to_pointee;
674 tree from_pointee;
676 if (tcode == POINTER_TYPE
677 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
678 TREE_TYPE (to)))
680 else if (VOID_TYPE_P (TREE_TYPE (to))
681 && !TYPE_PTRMEM_P (from)
682 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
684 from = build_pointer_type
685 (cp_build_qualified_type (void_type_node,
686 cp_type_quals (TREE_TYPE (from))));
687 conv = build_conv (PTR_CONV, from, conv);
689 else if (TYPE_PTRMEM_P (from))
691 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
692 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
694 if (DERIVED_FROM_P (fbase, tbase)
695 && (same_type_ignoring_top_level_qualifiers_p
696 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
697 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
699 from = build_ptrmem_type (tbase,
700 TYPE_PTRMEM_POINTED_TO_TYPE (from));
701 conv = build_conv (PMEM_CONV, from, conv);
704 else if (IS_AGGR_TYPE (TREE_TYPE (from))
705 && IS_AGGR_TYPE (TREE_TYPE (to))
706 /* [conv.ptr]
708 An rvalue of type "pointer to cv D," where D is a
709 class type, can be converted to an rvalue of type
710 "pointer to cv B," where B is a base class (clause
711 _class.derived_) of D. If B is an inaccessible
712 (clause _class.access_) or ambiguous
713 (_class.member.lookup_) base class of D, a program
714 that necessitates this conversion is ill-formed. */
715 /* Therefore, we use DERIVED_FROM_P, and not
716 ACESSIBLY_UNIQUELY_DERIVED_FROM_P, in this test. */
717 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
719 from =
720 cp_build_qualified_type (TREE_TYPE (to),
721 cp_type_quals (TREE_TYPE (from)));
722 from = build_pointer_type (from);
723 conv = build_conv (PTR_CONV, from, conv);
726 if (tcode == POINTER_TYPE)
728 to_pointee = TREE_TYPE (to);
729 from_pointee = TREE_TYPE (from);
731 else
733 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
734 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
737 if (same_type_p (from, to))
738 /* OK */;
739 else if (comp_ptr_ttypes (to_pointee, from_pointee))
740 conv = build_conv (QUAL_CONV, to, conv);
741 else if (expr && string_conv_p (to, expr, 0))
742 /* converting from string constant to char *. */
743 conv = build_conv (QUAL_CONV, to, conv);
744 else if (ptr_reasonably_similar (to_pointee, from_pointee))
746 conv = build_conv (PTR_CONV, to, conv);
747 ICS_BAD_FLAG (conv) = 1;
749 else
750 return 0;
752 from = to;
754 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
756 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
757 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
758 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
759 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
761 if (!DERIVED_FROM_P (fbase, tbase)
762 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
763 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
764 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
765 || cp_type_quals (fbase) != cp_type_quals (tbase))
766 return 0;
768 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
769 from = build_method_type_directly (from,
770 TREE_TYPE (fromfn),
771 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
772 from = build_ptrmemfunc_type (build_pointer_type (from));
773 conv = build_conv (PMEM_CONV, from, conv);
775 else if (tcode == BOOLEAN_TYPE)
777 /* [conv.bool]
779 An rvalue of arithmetic, enumeration, pointer, or pointer to
780 member type can be converted to an rvalue of type bool. */
781 if (ARITHMETIC_TYPE_P (from)
782 || fcode == ENUMERAL_TYPE
783 || fcode == POINTER_TYPE
784 || TYPE_PTR_TO_MEMBER_P (from))
786 conv = build_conv (STD_CONV, to, conv);
787 if (fcode == POINTER_TYPE
788 || TYPE_PTRMEM_P (from)
789 || (TYPE_PTRMEMFUNC_P (from)
790 && ICS_STD_RANK (conv) < PBOOL_RANK))
791 ICS_STD_RANK (conv) = PBOOL_RANK;
792 return conv;
795 return NULL_TREE;
797 /* We don't check for ENUMERAL_TYPE here because there are no standard
798 conversions to enum type. */
799 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
800 || tcode == REAL_TYPE)
802 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
803 return 0;
804 conv = build_conv (STD_CONV, to, conv);
806 /* Give this a better rank if it's a promotion. */
807 if (same_type_p (to, type_promotes_to (from))
808 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
809 ICS_STD_RANK (conv) = PROMO_RANK;
811 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
812 && ((*targetm.vector_opaque_p) (from)
813 || (*targetm.vector_opaque_p) (to)))
814 return build_conv (STD_CONV, to, conv);
815 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
816 && is_properly_derived_from (from, to))
818 if (TREE_CODE (conv) == RVALUE_CONV)
819 conv = TREE_OPERAND (conv, 0);
820 conv = build_conv (BASE_CONV, to, conv);
821 /* The derived-to-base conversion indicates the initialization
822 of a parameter with base type from an object of a derived
823 type. A temporary object is created to hold the result of
824 the conversion. */
825 NEED_TEMPORARY_P (conv) = 1;
827 else
828 return 0;
830 return conv;
833 /* Returns nonzero if T1 is reference-related to T2. */
835 static bool
836 reference_related_p (tree t1, tree t2)
838 t1 = TYPE_MAIN_VARIANT (t1);
839 t2 = TYPE_MAIN_VARIANT (t2);
841 /* [dcl.init.ref]
843 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
844 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
845 of T2. */
846 return (same_type_p (t1, t2)
847 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
848 && DERIVED_FROM_P (t1, t2)));
851 /* Returns nonzero if T1 is reference-compatible with T2. */
853 static bool
854 reference_compatible_p (tree t1, tree t2)
856 /* [dcl.init.ref]
858 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
859 reference-related to T2 and cv1 is the same cv-qualification as,
860 or greater cv-qualification than, cv2. */
861 return (reference_related_p (t1, t2)
862 && at_least_as_qualified_p (t1, t2));
865 /* Determine whether or not the EXPR (of class type S) can be
866 converted to T as in [over.match.ref]. */
868 static tree
869 convert_class_to_reference (tree t, tree s, tree expr)
871 tree conversions;
872 tree arglist;
873 tree conv;
874 tree reference_type;
875 struct z_candidate *candidates;
876 struct z_candidate *cand;
877 bool any_viable_p;
879 conversions = lookup_conversions (s);
880 if (!conversions)
881 return NULL_TREE;
883 /* [over.match.ref]
885 Assuming that "cv1 T" is the underlying type of the reference
886 being initialized, and "cv S" is the type of the initializer
887 expression, with S a class type, the candidate functions are
888 selected as follows:
890 --The conversion functions of S and its base classes are
891 considered. Those that are not hidden within S and yield type
892 "reference to cv2 T2", where "cv1 T" is reference-compatible
893 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
895 The argument list has one argument, which is the initializer
896 expression. */
898 candidates = 0;
900 /* Conceptually, we should take the address of EXPR and put it in
901 the argument list. Unfortunately, however, that can result in
902 error messages, which we should not issue now because we are just
903 trying to find a conversion operator. Therefore, we use NULL,
904 cast to the appropriate type. */
905 arglist = build_int_2 (0, 0);
906 TREE_TYPE (arglist) = build_pointer_type (s);
907 arglist = build_tree_list (NULL_TREE, arglist);
909 reference_type = build_reference_type (t);
911 while (conversions)
913 tree fns = TREE_VALUE (conversions);
915 for (; fns; fns = OVL_NEXT (fns))
917 tree f = OVL_CURRENT (fns);
918 tree t2 = TREE_TYPE (TREE_TYPE (f));
920 cand = NULL;
922 /* If this is a template function, try to get an exact
923 match. */
924 if (TREE_CODE (f) == TEMPLATE_DECL)
926 cand = add_template_candidate (&candidates,
927 f, s,
928 NULL_TREE,
929 arglist,
930 reference_type,
931 TYPE_BINFO (s),
932 TREE_PURPOSE (conversions),
933 LOOKUP_NORMAL,
934 DEDUCE_CONV);
936 if (cand)
938 /* Now, see if the conversion function really returns
939 an lvalue of the appropriate type. From the
940 point of view of unification, simply returning an
941 rvalue of the right type is good enough. */
942 f = cand->fn;
943 t2 = TREE_TYPE (TREE_TYPE (f));
944 if (TREE_CODE (t2) != REFERENCE_TYPE
945 || !reference_compatible_p (t, TREE_TYPE (t2)))
947 candidates = candidates->next;
948 cand = NULL;
952 else if (TREE_CODE (t2) == REFERENCE_TYPE
953 && reference_compatible_p (t, TREE_TYPE (t2)))
954 cand = add_function_candidate (&candidates, f, s, arglist,
955 TYPE_BINFO (s),
956 TREE_PURPOSE (conversions),
957 LOOKUP_NORMAL);
959 if (cand)
960 /* Build a standard conversion sequence indicating the
961 binding from the reference type returned by the
962 function to the desired REFERENCE_TYPE. */
963 cand->second_conv
964 = (direct_reference_binding
965 (reference_type,
966 build1 (IDENTITY_CONV,
967 TREE_TYPE (TREE_TYPE (TREE_TYPE (cand->fn))),
968 NULL_TREE)));
970 conversions = TREE_CHAIN (conversions);
973 candidates = splice_viable (candidates, pedantic, &any_viable_p);
974 /* If none of the conversion functions worked out, let our caller
975 know. */
976 if (!any_viable_p)
977 return NULL_TREE;
979 cand = tourney (candidates);
980 if (!cand)
981 return NULL_TREE;
983 /* Now that we know that this is the function we're going to use fix
984 the dummy first argument. */
985 cand->args = tree_cons (NULL_TREE,
986 build_this (expr),
987 TREE_CHAIN (cand->args));
989 /* Build a user-defined conversion sequence representing the
990 conversion. */
991 conv = build_conv (USER_CONV,
992 TREE_TYPE (TREE_TYPE (cand->fn)),
993 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
994 TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
996 /* Merge it with the standard conversion sequence from the
997 conversion function's return type to the desired type. */
998 cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
1000 if (cand->viable == -1)
1001 ICS_BAD_FLAG (conv) = 1;
1003 return cand->second_conv;
1006 /* A reference of the indicated TYPE is being bound directly to the
1007 expression represented by the implicit conversion sequence CONV.
1008 Return a conversion sequence for this binding. */
1010 static tree
1011 direct_reference_binding (tree type, tree conv)
1013 tree t;
1015 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 20030306);
1016 my_friendly_assert (TREE_CODE (TREE_TYPE (conv)) != REFERENCE_TYPE,
1017 20030306);
1019 t = TREE_TYPE (type);
1021 /* [over.ics.rank]
1023 When a parameter of reference type binds directly
1024 (_dcl.init.ref_) to an argument expression, the implicit
1025 conversion sequence is the identity conversion, unless the
1026 argument expression has a type that is a derived class of the
1027 parameter type, in which case the implicit conversion sequence is
1028 a derived-to-base Conversion.
1030 If the parameter binds directly to the result of applying a
1031 conversion function to the argument expression, the implicit
1032 conversion sequence is a user-defined conversion sequence
1033 (_over.ics.user_), with the second standard conversion sequence
1034 either an identity conversion or, if the conversion function
1035 returns an entity of a type that is a derived class of the
1036 parameter type, a derived-to-base conversion. */
1037 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1039 /* Represent the derived-to-base conversion. */
1040 conv = build_conv (BASE_CONV, t, conv);
1041 /* We will actually be binding to the base-class subobject in
1042 the derived class, so we mark this conversion appropriately.
1043 That way, convert_like knows not to generate a temporary. */
1044 NEED_TEMPORARY_P (conv) = 0;
1046 return build_conv (REF_BIND, type, conv);
1049 /* Returns the conversion path from type FROM to reference type TO for
1050 purposes of reference binding. For lvalue binding, either pass a
1051 reference type to FROM or an lvalue expression to EXPR. If the
1052 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1053 the conversion returned. */
1055 static tree
1056 reference_binding (tree rto, tree rfrom, tree expr, int flags)
1058 tree conv = NULL_TREE;
1059 tree to = TREE_TYPE (rto);
1060 tree from = rfrom;
1061 bool related_p;
1062 bool compatible_p;
1063 cp_lvalue_kind lvalue_p = clk_none;
1065 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1067 expr = instantiate_type (to, expr, tf_none);
1068 if (expr == error_mark_node)
1069 return NULL_TREE;
1070 from = TREE_TYPE (expr);
1073 if (TREE_CODE (from) == REFERENCE_TYPE)
1075 /* Anything with reference type is an lvalue. */
1076 lvalue_p = clk_ordinary;
1077 from = TREE_TYPE (from);
1079 else if (expr)
1080 lvalue_p = real_lvalue_p (expr);
1082 /* Figure out whether or not the types are reference-related and
1083 reference compatible. We have do do this after stripping
1084 references from FROM. */
1085 related_p = reference_related_p (to, from);
1086 compatible_p = reference_compatible_p (to, from);
1088 if (lvalue_p && compatible_p)
1090 /* [dcl.init.ref]
1092 If the initializer expression
1094 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1095 is reference-compatible with "cv2 T2,"
1097 the reference is bound directly to the initializer expression
1098 lvalue. */
1099 conv = build1 (IDENTITY_CONV, from, expr);
1100 conv = direct_reference_binding (rto, conv);
1101 if ((lvalue_p & clk_bitfield) != 0
1102 || ((lvalue_p & clk_packed) != 0 && !TYPE_PACKED (to)))
1103 /* For the purposes of overload resolution, we ignore the fact
1104 this expression is a bitfield or packed field. (In particular,
1105 [over.ics.ref] says specifically that a function with a
1106 non-const reference parameter is viable even if the
1107 argument is a bitfield.)
1109 However, when we actually call the function we must create
1110 a temporary to which to bind the reference. If the
1111 reference is volatile, or isn't const, then we cannot make
1112 a temporary, so we just issue an error when the conversion
1113 actually occurs. */
1114 NEED_TEMPORARY_P (conv) = 1;
1116 return conv;
1118 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1120 /* [dcl.init.ref]
1122 If the initializer expression
1124 -- has a class type (i.e., T2 is a class type) can be
1125 implicitly converted to an lvalue of type "cv3 T3," where
1126 "cv1 T1" is reference-compatible with "cv3 T3". (this
1127 conversion is selected by enumerating the applicable
1128 conversion functions (_over.match.ref_) and choosing the
1129 best one through overload resolution. (_over.match_).
1131 the reference is bound to the lvalue result of the conversion
1132 in the second case. */
1133 conv = convert_class_to_reference (to, from, expr);
1134 if (conv)
1135 return conv;
1138 /* From this point on, we conceptually need temporaries, even if we
1139 elide them. Only the cases above are "direct bindings". */
1140 if (flags & LOOKUP_NO_TEMP_BIND)
1141 return NULL_TREE;
1143 /* [over.ics.rank]
1145 When a parameter of reference type is not bound directly to an
1146 argument expression, the conversion sequence is the one required
1147 to convert the argument expression to the underlying type of the
1148 reference according to _over.best.ics_. Conceptually, this
1149 conversion sequence corresponds to copy-initializing a temporary
1150 of the underlying type with the argument expression. Any
1151 difference in top-level cv-qualification is subsumed by the
1152 initialization itself and does not constitute a conversion. */
1154 /* [dcl.init.ref]
1156 Otherwise, the reference shall be to a non-volatile const type. */
1157 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1158 return NULL_TREE;
1160 /* [dcl.init.ref]
1162 If the initializer expression is an rvalue, with T2 a class type,
1163 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1164 is bound in one of the following ways:
1166 -- The reference is bound to the object represented by the rvalue
1167 or to a sub-object within that object.
1169 -- ...
1171 We use the first alternative. The implicit conversion sequence
1172 is supposed to be same as we would obtain by generating a
1173 temporary. Fortunately, if the types are reference compatible,
1174 then this is either an identity conversion or the derived-to-base
1175 conversion, just as for direct binding. */
1176 if (CLASS_TYPE_P (from) && compatible_p)
1178 conv = build1 (IDENTITY_CONV, from, expr);
1179 return direct_reference_binding (rto, conv);
1182 /* [dcl.init.ref]
1184 Otherwise, a temporary of type "cv1 T1" is created and
1185 initialized from the initializer expression using the rules for a
1186 non-reference copy initialization. If T1 is reference-related to
1187 T2, cv1 must be the same cv-qualification as, or greater
1188 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1189 if (related_p && !at_least_as_qualified_p (to, from))
1190 return NULL_TREE;
1192 conv = implicit_conversion (to, from, expr, flags);
1193 if (!conv)
1194 return NULL_TREE;
1196 conv = build_conv (REF_BIND, rto, conv);
1197 /* This reference binding, unlike those above, requires the
1198 creation of a temporary. */
1199 NEED_TEMPORARY_P (conv) = 1;
1201 return conv;
1204 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1205 to type TO. The optional expression EXPR may affect the conversion.
1206 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1207 significant. */
1209 static tree
1210 implicit_conversion (tree to, tree from, tree expr, int flags)
1212 tree conv;
1214 if (from == error_mark_node || to == error_mark_node
1215 || expr == error_mark_node)
1216 return NULL_TREE;
1218 if (TREE_CODE (to) == REFERENCE_TYPE)
1219 conv = reference_binding (to, from, expr, flags);
1220 else
1221 conv = standard_conversion (to, from, expr);
1223 if (conv)
1224 return conv;
1226 if (expr != NULL_TREE
1227 && (IS_AGGR_TYPE (from)
1228 || IS_AGGR_TYPE (to))
1229 && (flags & LOOKUP_NO_CONVERSION) == 0)
1231 struct z_candidate *cand;
1233 cand = build_user_type_conversion_1
1234 (to, expr, LOOKUP_ONLYCONVERTING);
1235 if (cand)
1236 conv = cand->second_conv;
1238 /* We used to try to bind a reference to a temporary here, but that
1239 is now handled by the recursive call to this function at the end
1240 of reference_binding. */
1241 return conv;
1244 return NULL_TREE;
1247 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1248 functions. */
1250 static struct z_candidate *
1251 add_candidate (struct z_candidate **candidates,
1252 tree fn, tree args, tree convs, tree access_path,
1253 tree conversion_path, int viable)
1255 struct z_candidate *cand = ggc_alloc_cleared (sizeof (struct z_candidate));
1257 cand->fn = fn;
1258 cand->args = args;
1259 cand->convs = convs;
1260 cand->access_path = access_path;
1261 cand->conversion_path = conversion_path;
1262 cand->viable = viable;
1263 cand->next = *candidates;
1264 *candidates = cand;
1266 return cand;
1269 /* Create an overload candidate for the function or method FN called with
1270 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1271 to implicit_conversion.
1273 CTYPE, if non-NULL, is the type we want to pretend this function
1274 comes from for purposes of overload resolution. */
1276 static struct z_candidate *
1277 add_function_candidate (struct z_candidate **candidates,
1278 tree fn, tree ctype, tree arglist,
1279 tree access_path, tree conversion_path,
1280 int flags)
1282 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1283 int i, len;
1284 tree convs;
1285 tree parmnode, argnode;
1286 tree orig_arglist;
1287 int viable = 1;
1289 /* Built-in functions that haven't been declared don't really
1290 exist. */
1291 if (DECL_ANTICIPATED (fn))
1292 return NULL;
1294 /* The `this', `in_chrg' and VTT arguments to constructors are not
1295 considered in overload resolution. */
1296 if (DECL_CONSTRUCTOR_P (fn))
1298 parmlist = skip_artificial_parms_for (fn, parmlist);
1299 orig_arglist = arglist;
1300 arglist = skip_artificial_parms_for (fn, arglist);
1302 else
1303 orig_arglist = arglist;
1305 len = list_length (arglist);
1306 convs = make_tree_vec (len);
1308 /* 13.3.2 - Viable functions [over.match.viable]
1309 First, to be a viable function, a candidate function shall have enough
1310 parameters to agree in number with the arguments in the list.
1312 We need to check this first; otherwise, checking the ICSes might cause
1313 us to produce an ill-formed template instantiation. */
1315 parmnode = parmlist;
1316 for (i = 0; i < len; ++i)
1318 if (parmnode == NULL_TREE || parmnode == void_list_node)
1319 break;
1320 parmnode = TREE_CHAIN (parmnode);
1323 if (i < len && parmnode)
1324 viable = 0;
1326 /* Make sure there are default args for the rest of the parms. */
1327 else if (!sufficient_parms_p (parmnode))
1328 viable = 0;
1330 if (! viable)
1331 goto out;
1333 /* Second, for F to be a viable function, there shall exist for each
1334 argument an implicit conversion sequence that converts that argument
1335 to the corresponding parameter of F. */
1337 parmnode = parmlist;
1338 argnode = arglist;
1340 for (i = 0; i < len; ++i)
1342 tree arg = TREE_VALUE (argnode);
1343 tree argtype = lvalue_type (arg);
1344 tree t;
1345 int is_this;
1347 if (parmnode == void_list_node)
1348 break;
1350 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1351 && ! DECL_CONSTRUCTOR_P (fn));
1353 if (parmnode)
1355 tree parmtype = TREE_VALUE (parmnode);
1357 /* The type of the implicit object parameter ('this') for
1358 overload resolution is not always the same as for the
1359 function itself; conversion functions are considered to
1360 be members of the class being converted, and functions
1361 introduced by a using-declaration are considered to be
1362 members of the class that uses them.
1364 Since build_over_call ignores the ICS for the `this'
1365 parameter, we can just change the parm type. */
1366 if (ctype && is_this)
1368 parmtype
1369 = build_qualified_type (ctype,
1370 TYPE_QUALS (TREE_TYPE (parmtype)));
1371 parmtype = build_pointer_type (parmtype);
1374 t = implicit_conversion (parmtype, argtype, arg, flags);
1376 else
1378 t = build1 (IDENTITY_CONV, argtype, arg);
1379 ICS_ELLIPSIS_FLAG (t) = 1;
1382 if (t && is_this)
1383 ICS_THIS_FLAG (t) = 1;
1385 TREE_VEC_ELT (convs, i) = t;
1386 if (! t)
1388 viable = 0;
1389 break;
1392 if (ICS_BAD_FLAG (t))
1393 viable = -1;
1395 if (parmnode)
1396 parmnode = TREE_CHAIN (parmnode);
1397 argnode = TREE_CHAIN (argnode);
1400 out:
1401 return add_candidate (candidates, fn, orig_arglist, convs, access_path,
1402 conversion_path, viable);
1405 /* Create an overload candidate for the conversion function FN which will
1406 be invoked for expression OBJ, producing a pointer-to-function which
1407 will in turn be called with the argument list ARGLIST, and add it to
1408 CANDIDATES. FLAGS is passed on to implicit_conversion.
1410 Actually, we don't really care about FN; we care about the type it
1411 converts to. There may be multiple conversion functions that will
1412 convert to that type, and we rely on build_user_type_conversion_1 to
1413 choose the best one; so when we create our candidate, we record the type
1414 instead of the function. */
1416 static struct z_candidate *
1417 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
1418 tree arglist, tree access_path, tree conversion_path)
1420 tree totype = TREE_TYPE (TREE_TYPE (fn));
1421 int i, len, viable, flags;
1422 tree parmlist, convs, parmnode, argnode;
1424 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1425 parmlist = TREE_TYPE (parmlist);
1426 parmlist = TYPE_ARG_TYPES (parmlist);
1428 len = list_length (arglist) + 1;
1429 convs = make_tree_vec (len);
1430 parmnode = parmlist;
1431 argnode = arglist;
1432 viable = 1;
1433 flags = LOOKUP_NORMAL;
1435 /* Don't bother looking up the same type twice. */
1436 if (*candidates && (*candidates)->fn == totype)
1437 return NULL;
1439 for (i = 0; i < len; ++i)
1441 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1442 tree argtype = lvalue_type (arg);
1443 tree t;
1445 if (i == 0)
1446 t = implicit_conversion (totype, argtype, arg, flags);
1447 else if (parmnode == void_list_node)
1448 break;
1449 else if (parmnode)
1450 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1451 else
1453 t = build1 (IDENTITY_CONV, argtype, arg);
1454 ICS_ELLIPSIS_FLAG (t) = 1;
1457 TREE_VEC_ELT (convs, i) = t;
1458 if (! t)
1459 break;
1461 if (ICS_BAD_FLAG (t))
1462 viable = -1;
1464 if (i == 0)
1465 continue;
1467 if (parmnode)
1468 parmnode = TREE_CHAIN (parmnode);
1469 argnode = TREE_CHAIN (argnode);
1472 if (i < len)
1473 viable = 0;
1475 if (!sufficient_parms_p (parmnode))
1476 viable = 0;
1478 return add_candidate (candidates, totype, arglist, convs, access_path,
1479 conversion_path, viable);
1482 static void
1483 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
1484 tree type1, tree type2, tree *args, tree *argtypes,
1485 int flags)
1487 tree t, convs;
1488 int viable = 1, i;
1489 tree types[2];
1491 types[0] = type1;
1492 types[1] = type2;
1494 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1496 for (i = 0; i < 2; ++i)
1498 if (! args[i])
1499 break;
1501 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1502 if (! t)
1504 viable = 0;
1505 /* We need something for printing the candidate. */
1506 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1508 else if (ICS_BAD_FLAG (t))
1509 viable = 0;
1510 TREE_VEC_ELT (convs, i) = t;
1513 /* For COND_EXPR we rearranged the arguments; undo that now. */
1514 if (args[2])
1516 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1517 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1518 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1519 if (t)
1520 TREE_VEC_ELT (convs, 0) = t;
1521 else
1522 viable = 0;
1525 add_candidate (candidates, fnname, /*args=*/NULL_TREE, convs,
1526 /*access_path=*/NULL_TREE,
1527 /*conversion_path=*/NULL_TREE,
1528 viable);
1531 static bool
1532 is_complete (tree t)
1534 return COMPLETE_TYPE_P (complete_type (t));
1537 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1539 static bool
1540 promoted_arithmetic_type_p (tree type)
1542 /* [over.built]
1544 In this section, the term promoted integral type is used to refer
1545 to those integral types which are preserved by integral promotion
1546 (including e.g. int and long but excluding e.g. char).
1547 Similarly, the term promoted arithmetic type refers to promoted
1548 integral types plus floating types. */
1549 return ((INTEGRAL_TYPE_P (type)
1550 && same_type_p (type_promotes_to (type), type))
1551 || TREE_CODE (type) == REAL_TYPE);
1554 /* Create any builtin operator overload candidates for the operator in
1555 question given the converted operand types TYPE1 and TYPE2. The other
1556 args are passed through from add_builtin_candidates to
1557 build_builtin_candidate.
1559 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1560 If CODE is requires candidates operands of the same type of the kind
1561 of which TYPE1 and TYPE2 are, we add both candidates
1562 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1564 static void
1565 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
1566 enum tree_code code2, tree fnname, tree type1,
1567 tree type2, tree *args, tree *argtypes, int flags)
1569 switch (code)
1571 case POSTINCREMENT_EXPR:
1572 case POSTDECREMENT_EXPR:
1573 args[1] = integer_zero_node;
1574 type2 = integer_type_node;
1575 break;
1576 default:
1577 break;
1580 switch (code)
1583 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1584 and VQ is either volatile or empty, there exist candidate operator
1585 functions of the form
1586 VQ T& operator++(VQ T&);
1587 T operator++(VQ T&, int);
1588 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1589 type other than bool, and VQ is either volatile or empty, there exist
1590 candidate operator functions of the form
1591 VQ T& operator--(VQ T&);
1592 T operator--(VQ T&, int);
1593 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1594 complete object type, and VQ is either volatile or empty, there exist
1595 candidate operator functions of the form
1596 T*VQ& operator++(T*VQ&);
1597 T*VQ& operator--(T*VQ&);
1598 T* operator++(T*VQ&, int);
1599 T* operator--(T*VQ&, int); */
1601 case POSTDECREMENT_EXPR:
1602 case PREDECREMENT_EXPR:
1603 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1604 return;
1605 case POSTINCREMENT_EXPR:
1606 case PREINCREMENT_EXPR:
1607 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1609 type1 = build_reference_type (type1);
1610 break;
1612 return;
1614 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1615 exist candidate operator functions of the form
1617 T& operator*(T*);
1619 8 For every function type T, there exist candidate operator functions of
1620 the form
1621 T& operator*(T*); */
1623 case INDIRECT_REF:
1624 if (TREE_CODE (type1) == POINTER_TYPE
1625 && (TYPE_PTROB_P (type1)
1626 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1627 break;
1628 return;
1630 /* 9 For every type T, there exist candidate operator functions of the form
1631 T* operator+(T*);
1633 10For every promoted arithmetic type T, there exist candidate operator
1634 functions of the form
1635 T operator+(T);
1636 T operator-(T); */
1638 case CONVERT_EXPR: /* unary + */
1639 if (TREE_CODE (type1) == POINTER_TYPE)
1640 break;
1641 case NEGATE_EXPR:
1642 if (ARITHMETIC_TYPE_P (type1))
1643 break;
1644 return;
1646 /* 11For every promoted integral type T, there exist candidate operator
1647 functions of the form
1648 T operator~(T); */
1650 case BIT_NOT_EXPR:
1651 if (INTEGRAL_TYPE_P (type1))
1652 break;
1653 return;
1655 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1656 is the same type as C2 or is a derived class of C2, T is a complete
1657 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1658 there exist candidate operator functions of the form
1659 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1660 where CV12 is the union of CV1 and CV2. */
1662 case MEMBER_REF:
1663 if (TREE_CODE (type1) == POINTER_TYPE
1664 && TYPE_PTR_TO_MEMBER_P (type2))
1666 tree c1 = TREE_TYPE (type1);
1667 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
1669 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1670 && (TYPE_PTRMEMFUNC_P (type2)
1671 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1672 break;
1674 return;
1676 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1677 didate operator functions of the form
1678 LR operator*(L, R);
1679 LR operator/(L, R);
1680 LR operator+(L, R);
1681 LR operator-(L, R);
1682 bool operator<(L, R);
1683 bool operator>(L, R);
1684 bool operator<=(L, R);
1685 bool operator>=(L, R);
1686 bool operator==(L, R);
1687 bool operator!=(L, R);
1688 where LR is the result of the usual arithmetic conversions between
1689 types L and R.
1691 14For every pair of types T and I, where T is a cv-qualified or cv-
1692 unqualified complete object type and I is a promoted integral type,
1693 there exist candidate operator functions of the form
1694 T* operator+(T*, I);
1695 T& operator[](T*, I);
1696 T* operator-(T*, I);
1697 T* operator+(I, T*);
1698 T& operator[](I, T*);
1700 15For every T, where T is a pointer to complete object type, there exist
1701 candidate operator functions of the form112)
1702 ptrdiff_t operator-(T, T);
1704 16For every pointer or enumeration type T, there exist candidate operator
1705 functions of the form
1706 bool operator<(T, T);
1707 bool operator>(T, T);
1708 bool operator<=(T, T);
1709 bool operator>=(T, T);
1710 bool operator==(T, T);
1711 bool operator!=(T, T);
1713 17For every pointer to member type T, there exist candidate operator
1714 functions of the form
1715 bool operator==(T, T);
1716 bool operator!=(T, T); */
1718 case MINUS_EXPR:
1719 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1720 break;
1721 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1723 type2 = ptrdiff_type_node;
1724 break;
1726 case MULT_EXPR:
1727 case TRUNC_DIV_EXPR:
1728 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1729 break;
1730 return;
1732 case EQ_EXPR:
1733 case NE_EXPR:
1734 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1735 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1736 break;
1737 if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
1739 type2 = type1;
1740 break;
1742 if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
1744 type1 = type2;
1745 break;
1747 /* Fall through. */
1748 case LT_EXPR:
1749 case GT_EXPR:
1750 case LE_EXPR:
1751 case GE_EXPR:
1752 case MAX_EXPR:
1753 case MIN_EXPR:
1754 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1755 break;
1756 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1757 break;
1758 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1759 break;
1760 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1762 type2 = type1;
1763 break;
1765 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1767 type1 = type2;
1768 break;
1770 return;
1772 case PLUS_EXPR:
1773 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1774 break;
1775 case ARRAY_REF:
1776 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1778 type1 = ptrdiff_type_node;
1779 break;
1781 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1783 type2 = ptrdiff_type_node;
1784 break;
1786 return;
1788 /* 18For every pair of promoted integral types L and R, there exist candi-
1789 date operator functions of the form
1790 LR operator%(L, R);
1791 LR operator&(L, R);
1792 LR operator^(L, R);
1793 LR operator|(L, R);
1794 L operator<<(L, R);
1795 L operator>>(L, R);
1796 where LR is the result of the usual arithmetic conversions between
1797 types L and R. */
1799 case TRUNC_MOD_EXPR:
1800 case BIT_AND_EXPR:
1801 case BIT_IOR_EXPR:
1802 case BIT_XOR_EXPR:
1803 case LSHIFT_EXPR:
1804 case RSHIFT_EXPR:
1805 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1806 break;
1807 return;
1809 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1810 type, VQ is either volatile or empty, and R is a promoted arithmetic
1811 type, there exist candidate operator functions of the form
1812 VQ L& operator=(VQ L&, R);
1813 VQ L& operator*=(VQ L&, R);
1814 VQ L& operator/=(VQ L&, R);
1815 VQ L& operator+=(VQ L&, R);
1816 VQ L& operator-=(VQ L&, R);
1818 20For every pair T, VQ), where T is any type and VQ is either volatile
1819 or empty, there exist candidate operator functions of the form
1820 T*VQ& operator=(T*VQ&, T*);
1822 21For every pair T, VQ), where T is a pointer to member type and VQ is
1823 either volatile or empty, there exist candidate operator functions of
1824 the form
1825 VQ T& operator=(VQ T&, T);
1827 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1828 unqualified complete object type, VQ is either volatile or empty, and
1829 I is a promoted integral type, there exist candidate operator func-
1830 tions of the form
1831 T*VQ& operator+=(T*VQ&, I);
1832 T*VQ& operator-=(T*VQ&, I);
1834 23For every triple L, VQ, R), where L is an integral or enumeration
1835 type, VQ is either volatile or empty, and R is a promoted integral
1836 type, there exist candidate operator functions of the form
1838 VQ L& operator%=(VQ L&, R);
1839 VQ L& operator<<=(VQ L&, R);
1840 VQ L& operator>>=(VQ L&, R);
1841 VQ L& operator&=(VQ L&, R);
1842 VQ L& operator^=(VQ L&, R);
1843 VQ L& operator|=(VQ L&, R); */
1845 case MODIFY_EXPR:
1846 switch (code2)
1848 case PLUS_EXPR:
1849 case MINUS_EXPR:
1850 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1852 type2 = ptrdiff_type_node;
1853 break;
1855 case MULT_EXPR:
1856 case TRUNC_DIV_EXPR:
1857 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1858 break;
1859 return;
1861 case TRUNC_MOD_EXPR:
1862 case BIT_AND_EXPR:
1863 case BIT_IOR_EXPR:
1864 case BIT_XOR_EXPR:
1865 case LSHIFT_EXPR:
1866 case RSHIFT_EXPR:
1867 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1868 break;
1869 return;
1871 case NOP_EXPR:
1872 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1873 break;
1874 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1875 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1876 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1877 || ((TYPE_PTRMEMFUNC_P (type1)
1878 || TREE_CODE (type1) == POINTER_TYPE)
1879 && null_ptr_cst_p (args[1])))
1881 type2 = type1;
1882 break;
1884 return;
1886 default:
1887 abort ();
1889 type1 = build_reference_type (type1);
1890 break;
1892 case COND_EXPR:
1893 /* [over.built]
1895 For every pair of promoted arithmetic types L and R, there
1896 exist candidate operator functions of the form
1898 LR operator?(bool, L, R);
1900 where LR is the result of the usual arithmetic conversions
1901 between types L and R.
1903 For every type T, where T is a pointer or pointer-to-member
1904 type, there exist candidate operator functions of the form T
1905 operator?(bool, T, T); */
1907 if (promoted_arithmetic_type_p (type1)
1908 && promoted_arithmetic_type_p (type2))
1909 /* That's OK. */
1910 break;
1912 /* Otherwise, the types should be pointers. */
1913 if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
1914 || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
1915 return;
1917 /* We don't check that the two types are the same; the logic
1918 below will actually create two candidates; one in which both
1919 parameter types are TYPE1, and one in which both parameter
1920 types are TYPE2. */
1921 break;
1923 default:
1924 abort ();
1927 /* If we're dealing with two pointer types or two enumeral types,
1928 we need candidates for both of them. */
1929 if (type2 && !same_type_p (type1, type2)
1930 && TREE_CODE (type1) == TREE_CODE (type2)
1931 && (TREE_CODE (type1) == REFERENCE_TYPE
1932 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1933 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1934 || TYPE_PTRMEMFUNC_P (type1)
1935 || IS_AGGR_TYPE (type1)
1936 || TREE_CODE (type1) == ENUMERAL_TYPE))
1938 build_builtin_candidate
1939 (candidates, fnname, type1, type1, args, argtypes, flags);
1940 build_builtin_candidate
1941 (candidates, fnname, type2, type2, args, argtypes, flags);
1942 return;
1945 build_builtin_candidate
1946 (candidates, fnname, type1, type2, args, argtypes, flags);
1949 tree
1950 type_decays_to (tree type)
1952 if (TREE_CODE (type) == ARRAY_TYPE)
1953 return build_pointer_type (TREE_TYPE (type));
1954 if (TREE_CODE (type) == FUNCTION_TYPE)
1955 return build_pointer_type (type);
1956 return type;
1959 /* There are three conditions of builtin candidates:
1961 1) bool-taking candidates. These are the same regardless of the input.
1962 2) pointer-pair taking candidates. These are generated for each type
1963 one of the input types converts to.
1964 3) arithmetic candidates. According to the standard, we should generate
1965 all of these, but I'm trying not to...
1967 Here we generate a superset of the possible candidates for this particular
1968 case. That is a subset of the full set the standard defines, plus some
1969 other cases which the standard disallows. add_builtin_candidate will
1970 filter out the invalid set. */
1972 static void
1973 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
1974 enum tree_code code2, tree fnname, tree *args,
1975 int flags)
1977 int ref1, i;
1978 int enum_p = 0;
1979 tree type, argtypes[3];
1980 /* TYPES[i] is the set of possible builtin-operator parameter types
1981 we will consider for the Ith argument. These are represented as
1982 a TREE_LIST; the TREE_VALUE of each node is the potential
1983 parameter type. */
1984 tree types[2];
1986 for (i = 0; i < 3; ++i)
1988 if (args[i])
1989 argtypes[i] = lvalue_type (args[i]);
1990 else
1991 argtypes[i] = NULL_TREE;
1994 switch (code)
1996 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1997 and VQ is either volatile or empty, there exist candidate operator
1998 functions of the form
1999 VQ T& operator++(VQ T&); */
2001 case POSTINCREMENT_EXPR:
2002 case PREINCREMENT_EXPR:
2003 case POSTDECREMENT_EXPR:
2004 case PREDECREMENT_EXPR:
2005 case MODIFY_EXPR:
2006 ref1 = 1;
2007 break;
2009 /* 24There also exist candidate operator functions of the form
2010 bool operator!(bool);
2011 bool operator&&(bool, bool);
2012 bool operator||(bool, bool); */
2014 case TRUTH_NOT_EXPR:
2015 build_builtin_candidate
2016 (candidates, fnname, boolean_type_node,
2017 NULL_TREE, args, argtypes, flags);
2018 return;
2020 case TRUTH_ORIF_EXPR:
2021 case TRUTH_ANDIF_EXPR:
2022 build_builtin_candidate
2023 (candidates, fnname, boolean_type_node,
2024 boolean_type_node, args, argtypes, flags);
2025 return;
2027 case ADDR_EXPR:
2028 case COMPOUND_EXPR:
2029 case COMPONENT_REF:
2030 return;
2032 case COND_EXPR:
2033 case EQ_EXPR:
2034 case NE_EXPR:
2035 case LT_EXPR:
2036 case LE_EXPR:
2037 case GT_EXPR:
2038 case GE_EXPR:
2039 enum_p = 1;
2040 /* Fall through. */
2042 default:
2043 ref1 = 0;
2046 types[0] = types[1] = NULL_TREE;
2048 for (i = 0; i < 2; ++i)
2050 if (! args[i])
2052 else if (IS_AGGR_TYPE (argtypes[i]))
2054 tree convs;
2056 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2057 return;
2059 convs = lookup_conversions (argtypes[i]);
2061 if (code == COND_EXPR)
2063 if (real_lvalue_p (args[i]))
2064 types[i] = tree_cons
2065 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2067 types[i] = tree_cons
2068 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2071 else if (! convs)
2072 return;
2074 for (; convs; convs = TREE_CHAIN (convs))
2076 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2078 if (i == 0 && ref1
2079 && (TREE_CODE (type) != REFERENCE_TYPE
2080 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2081 continue;
2083 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2084 types[i] = tree_cons (NULL_TREE, type, types[i]);
2086 type = non_reference (type);
2087 if (i != 0 || ! ref1)
2089 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2090 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2091 types[i] = tree_cons (NULL_TREE, type, types[i]);
2092 if (INTEGRAL_TYPE_P (type))
2093 type = type_promotes_to (type);
2096 if (! value_member (type, types[i]))
2097 types[i] = tree_cons (NULL_TREE, type, types[i]);
2100 else
2102 if (code == COND_EXPR && real_lvalue_p (args[i]))
2103 types[i] = tree_cons
2104 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2105 type = non_reference (argtypes[i]);
2106 if (i != 0 || ! ref1)
2108 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2109 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2110 types[i] = tree_cons (NULL_TREE, type, types[i]);
2111 if (INTEGRAL_TYPE_P (type))
2112 type = type_promotes_to (type);
2114 types[i] = tree_cons (NULL_TREE, type, types[i]);
2118 /* Run through the possible parameter types of both arguments,
2119 creating candidates with those parameter types. */
2120 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2122 if (types[1])
2123 for (type = types[1]; type; type = TREE_CHAIN (type))
2124 add_builtin_candidate
2125 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2126 TREE_VALUE (type), args, argtypes, flags);
2127 else
2128 add_builtin_candidate
2129 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2130 NULL_TREE, args, argtypes, flags);
2133 return;
2137 /* If TMPL can be successfully instantiated as indicated by
2138 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2140 TMPL is the template. EXPLICIT_TARGS are any explicit template
2141 arguments. ARGLIST is the arguments provided at the call-site.
2142 The RETURN_TYPE is the desired type for conversion operators. If
2143 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2144 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2145 add_conv_candidate. */
2147 static struct z_candidate*
2148 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2149 tree ctype, tree explicit_targs, tree arglist,
2150 tree return_type, tree access_path,
2151 tree conversion_path, int flags, tree obj,
2152 unification_kind_t strict)
2154 int ntparms = DECL_NTPARMS (tmpl);
2155 tree targs = make_tree_vec (ntparms);
2156 tree args_without_in_chrg = arglist;
2157 struct z_candidate *cand;
2158 int i;
2159 tree fn;
2161 /* We don't do deduction on the in-charge parameter, the VTT
2162 parameter or 'this'. */
2163 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2164 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2166 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2167 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2168 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2169 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2171 i = fn_type_unification (tmpl, explicit_targs, targs,
2172 args_without_in_chrg,
2173 return_type, strict, -1);
2175 if (i != 0)
2176 return NULL;
2178 fn = instantiate_template (tmpl, targs, tf_none);
2179 if (fn == error_mark_node)
2180 return NULL;
2182 /* In [class.copy]:
2184 A member function template is never instantiated to perform the
2185 copy of a class object to an object of its class type.
2187 It's a little unclear what this means; the standard explicitly
2188 does allow a template to be used to copy a class. For example,
2191 struct A {
2192 A(A&);
2193 template <class T> A(const T&);
2195 const A f ();
2196 void g () { A a (f ()); }
2198 the member template will be used to make the copy. The section
2199 quoted above appears in the paragraph that forbids constructors
2200 whose only parameter is (a possibly cv-qualified variant of) the
2201 class type, and a logical interpretation is that the intent was
2202 to forbid the instantiation of member templates which would then
2203 have that form. */
2204 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2206 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2207 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2208 ctype))
2209 return NULL;
2212 if (obj != NULL_TREE)
2213 /* Aha, this is a conversion function. */
2214 cand = add_conv_candidate (candidates, fn, obj, access_path,
2215 conversion_path, arglist);
2216 else
2217 cand = add_function_candidate (candidates, fn, ctype,
2218 arglist, access_path,
2219 conversion_path, flags);
2220 if (DECL_TI_TEMPLATE (fn) != tmpl)
2221 /* This situation can occur if a member template of a template
2222 class is specialized. Then, instantiate_template might return
2223 an instantiation of the specialization, in which case the
2224 DECL_TI_TEMPLATE field will point at the original
2225 specialization. For example:
2227 template <class T> struct S { template <class U> void f(U);
2228 template <> void f(int) {}; };
2229 S<double> sd;
2230 sd.f(3);
2232 Here, TMPL will be template <class U> S<double>::f(U).
2233 And, instantiate template will give us the specialization
2234 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2235 for this will point at template <class T> template <> S<T>::f(int),
2236 so that we can find the definition. For the purposes of
2237 overload resolution, however, we want the original TMPL. */
2238 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2239 else
2240 cand->template = DECL_TEMPLATE_INFO (fn);
2242 return cand;
2246 static struct z_candidate *
2247 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2248 tree explicit_targs, tree arglist, tree return_type,
2249 tree access_path, tree conversion_path, int flags,
2250 unification_kind_t strict)
2252 return
2253 add_template_candidate_real (candidates, tmpl, ctype,
2254 explicit_targs, arglist, return_type,
2255 access_path, conversion_path,
2256 flags, NULL_TREE, strict);
2260 static struct z_candidate *
2261 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2262 tree obj, tree arglist, tree return_type,
2263 tree access_path, tree conversion_path)
2265 return
2266 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2267 arglist, return_type, access_path,
2268 conversion_path, 0, obj, DEDUCE_CONV);
2271 /* The CANDS are the set of candidates that were considered for
2272 overload resolution. Return the set of viable candidates. If none
2273 of the candidates were viable, set *ANY_VIABLE_P to true. STRICT_P
2274 is true if a candidate should be considered viable only if it is
2275 strictly viable. */
2277 static struct z_candidate*
2278 splice_viable (struct z_candidate *cands,
2279 bool strict_p,
2280 bool *any_viable_p)
2282 struct z_candidate *viable;
2283 struct z_candidate **last_viable;
2284 struct z_candidate **cand;
2286 viable = NULL;
2287 last_viable = &viable;
2288 *any_viable_p = false;
2290 cand = &cands;
2291 while (*cand)
2293 struct z_candidate *c = *cand;
2294 if (strict_p ? c->viable == 1 : c->viable)
2296 *last_viable = c;
2297 *cand = c->next;
2298 c->next = NULL;
2299 last_viable = &c->next;
2300 *any_viable_p = true;
2302 else
2303 cand = &c->next;
2306 return viable ? viable : cands;
2309 static bool
2310 any_strictly_viable (struct z_candidate *cands)
2312 for (; cands; cands = cands->next)
2313 if (cands->viable == 1)
2314 return true;
2315 return false;
2318 static tree
2319 build_this (tree obj)
2321 /* Fix this to work on non-lvalues. */
2322 return build_unary_op (ADDR_EXPR, obj, 0);
2325 /* Returns true iff functions are equivalent. Equivalent functions are
2326 not '==' only if one is a function-local extern function or if
2327 both are extern "C". */
2329 static inline int
2330 equal_functions (tree fn1, tree fn2)
2332 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2333 || DECL_EXTERN_C_FUNCTION_P (fn1))
2334 return decls_match (fn1, fn2);
2335 return fn1 == fn2;
2338 /* Print information about one overload candidate CANDIDATE. MSGSTR
2339 is the text to print before the candidate itself.
2341 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2342 to have been run through gettext by the caller. This wart makes
2343 life simpler in print_z_candidates and for the translators. */
2345 static void
2346 print_z_candidate (const char *msgstr, struct z_candidate *candidate)
2348 if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2350 if (TREE_VEC_LENGTH (candidate->convs) == 3)
2351 inform ("%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
2352 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2353 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)),
2354 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 2)));
2355 else if (TREE_VEC_LENGTH (candidate->convs) == 2)
2356 inform ("%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
2357 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2358 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)));
2359 else
2360 inform ("%s %D(%T) <built-in>", msgstr, candidate->fn,
2361 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)));
2363 else if (TYPE_P (candidate->fn))
2364 inform ("%s %T <conversion>", msgstr, candidate->fn);
2365 else if (candidate->viable == -1)
2366 inform ("%J%s %+#D <near match>", candidate->fn, msgstr, candidate->fn);
2367 else
2368 inform ("%J%s %+#D", candidate->fn, msgstr, candidate->fn);
2371 static void
2372 print_z_candidates (struct z_candidate *candidates)
2374 const char *str;
2375 struct z_candidate *cand1;
2376 struct z_candidate **cand2;
2378 /* There may be duplicates in the set of candidates. We put off
2379 checking this condition as long as possible, since we have no way
2380 to eliminate duplicates from a set of functions in less than n^2
2381 time. Now we are about to emit an error message, so it is more
2382 permissible to go slowly. */
2383 for (cand1 = candidates; cand1; cand1 = cand1->next)
2385 tree fn = cand1->fn;
2386 /* Skip builtin candidates and conversion functions. */
2387 if (TREE_CODE (fn) != FUNCTION_DECL)
2388 continue;
2389 cand2 = &cand1->next;
2390 while (*cand2)
2392 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2393 && equal_functions (fn, (*cand2)->fn))
2394 *cand2 = (*cand2)->next;
2395 else
2396 cand2 = &(*cand2)->next;
2400 if (!candidates)
2401 return;
2403 str = _("candidates are:");
2404 print_z_candidate (str, candidates);
2405 if (candidates->next)
2407 /* Indent successive candidates by the width of the translation
2408 of the above string. */
2409 size_t len = gcc_gettext_width (str) + 1;
2410 char *spaces = alloca (len);
2411 memset (spaces, ' ', len-1);
2412 spaces[len - 1] = '\0';
2414 candidates = candidates->next;
2417 print_z_candidate (spaces, candidates);
2418 candidates = candidates->next;
2420 while (candidates);
2424 /* USER_SEQ is a user-defined conversion sequence, beginning with a
2425 USER_CONV. STD_SEQ is the standard conversion sequence applied to
2426 the result of the conversion function to convert it to the final
2427 desired type. Merge the the two sequences into a single sequence,
2428 and return the merged sequence. */
2430 static tree
2431 merge_conversion_sequences (tree user_seq, tree std_seq)
2433 tree *t;
2435 my_friendly_assert (TREE_CODE (user_seq) == USER_CONV,
2436 20030306);
2438 /* Find the end of the second conversion sequence. */
2439 t = &(std_seq);
2440 while (TREE_CODE (*t) != IDENTITY_CONV)
2441 t = &TREE_OPERAND (*t, 0);
2443 /* Replace the identity conversion with the user conversion
2444 sequence. */
2445 *t = user_seq;
2447 /* The entire sequence is a user-conversion sequence. */
2448 ICS_USER_FLAG (std_seq) = 1;
2450 return std_seq;
2453 /* Returns the best overload candidate to perform the requested
2454 conversion. This function is used for three the overloading situations
2455 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2456 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2457 per [dcl.init.ref], so we ignore temporary bindings. */
2459 static struct z_candidate *
2460 build_user_type_conversion_1 (tree totype, tree expr, int flags)
2462 struct z_candidate *candidates, *cand;
2463 tree fromtype = TREE_TYPE (expr);
2464 tree ctors = NULL_TREE, convs = NULL_TREE;
2465 tree args = NULL_TREE;
2466 bool any_viable_p;
2468 /* We represent conversion within a hierarchy using RVALUE_CONV and
2469 BASE_CONV, as specified by [over.best.ics]; these become plain
2470 constructor calls, as specified in [dcl.init]. */
2471 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2472 || !DERIVED_FROM_P (totype, fromtype), 20011226);
2474 if (IS_AGGR_TYPE (totype))
2475 ctors = lookup_fnfields (TYPE_BINFO (totype),
2476 complete_ctor_identifier,
2479 if (IS_AGGR_TYPE (fromtype))
2480 convs = lookup_conversions (fromtype);
2482 candidates = 0;
2483 flags |= LOOKUP_NO_CONVERSION;
2485 if (ctors)
2487 tree t;
2489 ctors = BASELINK_FUNCTIONS (ctors);
2491 t = build_int_2 (0, 0);
2492 TREE_TYPE (t) = build_pointer_type (totype);
2493 args = build_tree_list (NULL_TREE, expr);
2494 /* We should never try to call the abstract or base constructor
2495 from here. */
2496 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2497 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2498 20011226);
2499 args = tree_cons (NULL_TREE, t, args);
2501 for (; ctors; ctors = OVL_NEXT (ctors))
2503 tree ctor = OVL_CURRENT (ctors);
2504 if (DECL_NONCONVERTING_P (ctor))
2505 continue;
2507 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2508 cand = add_template_candidate (&candidates, ctor, totype,
2509 NULL_TREE, args, NULL_TREE,
2510 TYPE_BINFO (totype),
2511 TYPE_BINFO (totype),
2512 flags,
2513 DEDUCE_CALL);
2514 else
2515 cand = add_function_candidate (&candidates, ctor, totype,
2516 args, TYPE_BINFO (totype),
2517 TYPE_BINFO (totype),
2518 flags);
2520 if (cand)
2521 cand->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2524 if (convs)
2525 args = build_tree_list (NULL_TREE, build_this (expr));
2527 for (; convs; convs = TREE_CHAIN (convs))
2529 tree fns;
2530 tree conversion_path = TREE_PURPOSE (convs);
2531 int convflags = LOOKUP_NO_CONVERSION;
2533 /* If we are called to convert to a reference type, we are trying to
2534 find an lvalue binding, so don't even consider temporaries. If
2535 we don't find an lvalue binding, the caller will try again to
2536 look for a temporary binding. */
2537 if (TREE_CODE (totype) == REFERENCE_TYPE)
2538 convflags |= LOOKUP_NO_TEMP_BIND;
2540 for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
2542 tree fn = OVL_CURRENT (fns);
2544 /* [over.match.funcs] For conversion functions, the function
2545 is considered to be a member of the class of the implicit
2546 object argument for the purpose of defining the type of
2547 the implicit object parameter.
2549 So we pass fromtype as CTYPE to add_*_candidate. */
2551 if (TREE_CODE (fn) == TEMPLATE_DECL)
2552 cand = add_template_candidate (&candidates, fn, fromtype,
2553 NULL_TREE,
2554 args, totype,
2555 TYPE_BINFO (fromtype),
2556 conversion_path,
2557 flags,
2558 DEDUCE_CONV);
2559 else
2560 cand = add_function_candidate (&candidates, fn, fromtype,
2561 args,
2562 TYPE_BINFO (fromtype),
2563 conversion_path,
2564 flags);
2566 if (cand)
2568 tree ics = implicit_conversion (totype,
2569 TREE_TYPE (TREE_TYPE (cand->fn)),
2570 0, convflags);
2572 cand->second_conv = ics;
2574 if (ics == NULL_TREE)
2575 cand->viable = 0;
2576 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2577 cand->viable = -1;
2582 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2583 if (!any_viable_p)
2584 return 0;
2586 cand = tourney (candidates);
2587 if (cand == 0)
2589 if (flags & LOOKUP_COMPLAIN)
2591 error ("conversion from `%T' to `%T' is ambiguous",
2592 fromtype, totype);
2593 print_z_candidates (candidates);
2596 cand = candidates; /* any one will do */
2597 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2598 ICS_USER_FLAG (cand->second_conv) = 1;
2599 if (!any_strictly_viable (candidates))
2600 ICS_BAD_FLAG (cand->second_conv) = 1;
2601 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2602 ambiguous conversion is no worse than another user-defined
2603 conversion. */
2605 return cand;
2608 /* Build the user conversion sequence. */
2609 convs = build_conv
2610 (USER_CONV,
2611 (DECL_CONSTRUCTOR_P (cand->fn)
2612 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2613 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
2614 TREE_OPERAND (convs, 1) = build_zc_wrapper (cand);
2616 /* Combine it with the second conversion sequence. */
2617 cand->second_conv = merge_conversion_sequences (convs,
2618 cand->second_conv);
2620 if (cand->viable == -1)
2621 ICS_BAD_FLAG (cand->second_conv) = 1;
2623 return cand;
2626 tree
2627 build_user_type_conversion (tree totype, tree expr, int flags)
2629 struct z_candidate *cand
2630 = build_user_type_conversion_1 (totype, expr, flags);
2632 if (cand)
2634 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2635 return error_mark_node;
2636 return convert_from_reference (convert_like (cand->second_conv, expr));
2638 return NULL_TREE;
2641 /* Do any initial processing on the arguments to a function call. */
2643 static tree
2644 resolve_args (tree args)
2646 tree t;
2647 for (t = args; t; t = TREE_CHAIN (t))
2649 tree arg = TREE_VALUE (t);
2651 if (arg == error_mark_node)
2652 return error_mark_node;
2653 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2655 error ("invalid use of void expression");
2656 return error_mark_node;
2658 arg = convert_from_reference (arg);
2659 TREE_VALUE (t) = arg;
2661 return args;
2664 /* Perform overload resolution on FN, which is called with the ARGS.
2666 Return the candidate function selected by overload resolution, or
2667 NULL if the event that overload resolution failed. In the case
2668 that overload resolution fails, *CANDIDATES will be the set of
2669 candidates considered, and ANY_VIABLE_P will be set to true or
2670 false to indicate whether or not any of the candidates were
2671 viable.
2673 The ARGS should already have gone through RESOLVE_ARGS before this
2674 function is called. */
2676 static struct z_candidate *
2677 perform_overload_resolution (tree fn,
2678 tree args,
2679 struct z_candidate **candidates,
2680 bool *any_viable_p)
2682 struct z_candidate *cand;
2683 tree explicit_targs = NULL_TREE;
2684 int template_only = 0;
2686 *candidates = NULL;
2687 *any_viable_p = true;
2689 /* Check FN and ARGS. */
2690 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2691 || TREE_CODE (fn) == TEMPLATE_DECL
2692 || TREE_CODE (fn) == OVERLOAD
2693 || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2694 20020712);
2695 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2696 20020712);
2698 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2700 explicit_targs = TREE_OPERAND (fn, 1);
2701 fn = TREE_OPERAND (fn, 0);
2702 template_only = 1;
2705 /* Add the various candidate functions. */
2706 add_candidates (fn, args, explicit_targs, template_only,
2707 /*conversion_path=*/NULL_TREE,
2708 /*access_path=*/NULL_TREE,
2709 LOOKUP_NORMAL,
2710 candidates);
2712 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
2713 if (!*any_viable_p)
2714 return NULL;
2716 cand = tourney (*candidates);
2717 return cand;
2720 /* Return an expression for a call to FN (a namespace-scope function,
2721 or a static member function) with the ARGS. */
2723 tree
2724 build_new_function_call (tree fn, tree args)
2726 struct z_candidate *candidates, *cand;
2727 bool any_viable_p;
2729 args = resolve_args (args);
2730 if (args == error_mark_node)
2731 return error_mark_node;
2733 cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p);
2735 if (!cand)
2737 if (!any_viable_p && candidates && ! candidates->next)
2738 return build_function_call (candidates->fn, args);
2739 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2740 fn = TREE_OPERAND (fn, 0);
2741 if (!any_viable_p)
2742 error ("no matching function for call to `%D(%A)'",
2743 DECL_NAME (OVL_CURRENT (fn)), args);
2744 else
2745 error ("call of overloaded `%D(%A)' is ambiguous",
2746 DECL_NAME (OVL_CURRENT (fn)), args);
2747 if (candidates)
2748 print_z_candidates (candidates);
2749 return error_mark_node;
2752 return build_over_call (cand, LOOKUP_NORMAL);
2755 /* Build a call to a global operator new. FNNAME is the name of the
2756 operator (either "operator new" or "operator new[]") and ARGS are
2757 the arguments provided. *SIZE points to the total number of bytes
2758 required by the allocation, and is updated if that is changed here.
2759 *COOKIE_SIZE is non-NULL if a cookie should be used. If this
2760 function determines that no cookie should be used, after all,
2761 *COOKIE_SIZE is set to NULL_TREE. */
2763 tree
2764 build_operator_new_call (tree fnname, tree args, tree *size, tree *cookie_size)
2766 tree fns;
2767 struct z_candidate *candidates;
2768 struct z_candidate *cand;
2769 bool any_viable_p;
2771 args = tree_cons (NULL_TREE, *size, args);
2772 args = resolve_args (args);
2773 if (args == error_mark_node)
2774 return args;
2776 fns = lookup_function_nonclass (fnname, args);
2778 /* Figure out what function is being called. */
2779 cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p);
2781 /* If no suitable function could be found, issue an error message
2782 and give up. */
2783 if (!cand)
2785 if (!any_viable_p)
2786 error ("no matching function for call to `%D(%A)'",
2787 DECL_NAME (OVL_CURRENT (fns)), args);
2788 else
2789 error ("call of overloaded `%D(%A)' is ambiguous",
2790 DECL_NAME (OVL_CURRENT (fns)), args);
2791 if (candidates)
2792 print_z_candidates (candidates);
2793 return error_mark_node;
2796 /* If a cookie is required, add some extra space. Whether
2797 or not a cookie is required cannot be determined until
2798 after we know which function was called. */
2799 if (*cookie_size)
2801 bool use_cookie = true;
2802 if (!abi_version_at_least (2))
2804 tree placement = TREE_CHAIN (args);
2805 /* In G++ 3.2, the check was implemented incorrectly; it
2806 looked at the placement expression, rather than the
2807 type of the function. */
2808 if (placement && !TREE_CHAIN (placement)
2809 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2810 ptr_type_node))
2811 use_cookie = false;
2813 else
2815 tree arg_types;
2817 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
2818 /* Skip the size_t parameter. */
2819 arg_types = TREE_CHAIN (arg_types);
2820 /* Check the remaining parameters (if any). */
2821 if (arg_types
2822 && TREE_CHAIN (arg_types) == void_list_node
2823 && same_type_p (TREE_VALUE (arg_types),
2824 ptr_type_node))
2825 use_cookie = false;
2827 /* If we need a cookie, adjust the number of bytes allocated. */
2828 if (use_cookie)
2830 /* Update the total size. */
2831 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
2832 /* Update the argument list to reflect the adjusted size. */
2833 TREE_VALUE (args) = *size;
2835 else
2836 *cookie_size = NULL_TREE;
2839 /* Build the CALL_EXPR. */
2840 return build_over_call (cand, LOOKUP_NORMAL);
2843 static tree
2844 build_object_call (tree obj, tree args)
2846 struct z_candidate *candidates = 0, *cand;
2847 tree fns, convs, mem_args = NULL_TREE;
2848 tree type = TREE_TYPE (obj);
2849 bool any_viable_p;
2851 if (TYPE_PTRMEMFUNC_P (type))
2853 /* It's no good looking for an overloaded operator() on a
2854 pointer-to-member-function. */
2855 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2856 return error_mark_node;
2859 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2860 if (fns == error_mark_node)
2861 return error_mark_node;
2863 args = resolve_args (args);
2865 if (args == error_mark_node)
2866 return error_mark_node;
2868 if (fns)
2870 tree base = BINFO_TYPE (BASELINK_BINFO (fns));
2871 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2873 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2875 tree fn = OVL_CURRENT (fns);
2876 if (TREE_CODE (fn) == TEMPLATE_DECL)
2877 add_template_candidate (&candidates, fn, base, NULL_TREE,
2878 mem_args, NULL_TREE,
2879 TYPE_BINFO (type),
2880 TYPE_BINFO (type),
2881 LOOKUP_NORMAL, DEDUCE_CALL);
2882 else
2883 add_function_candidate
2884 (&candidates, fn, base, mem_args, TYPE_BINFO (type),
2885 TYPE_BINFO (type), LOOKUP_NORMAL);
2889 convs = lookup_conversions (type);
2891 for (; convs; convs = TREE_CHAIN (convs))
2893 tree fns = TREE_VALUE (convs);
2894 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2896 if ((TREE_CODE (totype) == POINTER_TYPE
2897 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2898 || (TREE_CODE (totype) == REFERENCE_TYPE
2899 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2900 || (TREE_CODE (totype) == REFERENCE_TYPE
2901 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2902 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2903 for (; fns; fns = OVL_NEXT (fns))
2905 tree fn = OVL_CURRENT (fns);
2906 if (TREE_CODE (fn) == TEMPLATE_DECL)
2907 add_template_conv_candidate
2908 (&candidates, fn, obj, args, totype,
2909 /*access_path=*/NULL_TREE,
2910 /*conversion_path=*/NULL_TREE);
2911 else
2912 add_conv_candidate (&candidates, fn, obj, args,
2913 /*conversion_path=*/NULL_TREE,
2914 /*access_path=*/NULL_TREE);
2918 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2919 if (!any_viable_p)
2921 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2922 print_z_candidates (candidates);
2923 return error_mark_node;
2926 cand = tourney (candidates);
2927 if (cand == 0)
2929 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2930 print_z_candidates (candidates);
2931 return error_mark_node;
2934 /* Since cand->fn will be a type, not a function, for a conversion
2935 function, we must be careful not to unconditionally look at
2936 DECL_NAME here. */
2937 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2938 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2939 return build_over_call (cand, LOOKUP_NORMAL);
2941 obj = convert_like_with_context
2942 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2944 /* FIXME */
2945 return build_function_call (obj, args);
2948 static void
2949 op_error (enum tree_code code, enum tree_code code2,
2950 tree arg1, tree arg2, tree arg3, const char *problem)
2952 const char *opname;
2954 if (code == MODIFY_EXPR)
2955 opname = assignment_operator_name_info[code2].name;
2956 else
2957 opname = operator_name_info[code].name;
2959 switch (code)
2961 case COND_EXPR:
2962 error ("%s for ternary 'operator?:' in '%E ? %E : %E'",
2963 problem, arg1, arg2, arg3);
2964 break;
2966 case POSTINCREMENT_EXPR:
2967 case POSTDECREMENT_EXPR:
2968 error ("%s for 'operator%s' in '%E%s'", problem, opname, arg1, opname);
2969 break;
2971 case ARRAY_REF:
2972 error ("%s for 'operator[]' in '%E[%E]'", problem, arg1, arg2);
2973 break;
2975 case REALPART_EXPR:
2976 case IMAGPART_EXPR:
2977 error ("%s for '%s' in '%s %E'", problem, opname, opname, arg1);
2978 break;
2980 default:
2981 if (arg2)
2982 error ("%s for 'operator%s' in '%E %s %E'",
2983 problem, opname, arg1, opname, arg2);
2984 else
2985 error ("%s for 'operator%s' in '%s%E'",
2986 problem, opname, opname, arg1);
2987 break;
2991 /* Return the implicit conversion sequence that could be used to
2992 convert E1 to E2 in [expr.cond]. */
2994 static tree
2995 conditional_conversion (tree e1, tree e2)
2997 tree t1 = non_reference (TREE_TYPE (e1));
2998 tree t2 = non_reference (TREE_TYPE (e2));
2999 tree conv;
3000 bool good_base;
3002 /* [expr.cond]
3004 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
3005 implicitly converted (clause _conv_) to the type "reference to
3006 T2", subject to the constraint that in the conversion the
3007 reference must bind directly (_dcl.init.ref_) to E1. */
3008 if (real_lvalue_p (e2))
3010 conv = implicit_conversion (build_reference_type (t2),
3013 LOOKUP_NO_TEMP_BIND);
3014 if (conv)
3015 return conv;
3018 /* [expr.cond]
3020 If E1 and E2 have class type, and the underlying class types are
3021 the same or one is a base class of the other: E1 can be converted
3022 to match E2 if the class of T2 is the same type as, or a base
3023 class of, the class of T1, and the cv-qualification of T2 is the
3024 same cv-qualification as, or a greater cv-qualification than, the
3025 cv-qualification of T1. If the conversion is applied, E1 is
3026 changed to an rvalue of type T2 that still refers to the original
3027 source class object (or the appropriate subobject thereof).
3029 FIXME we can't express an rvalue that refers to the original object;
3030 we have to create a new one. */
3031 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
3032 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
3034 if (good_base && at_least_as_qualified_p (t2, t1))
3036 conv = build1 (IDENTITY_CONV, t1, e1);
3037 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
3038 TYPE_MAIN_VARIANT (t2)))
3040 conv = build_conv (BASE_CONV, t2, conv);
3041 NEED_TEMPORARY_P (conv) = 1;
3043 else
3044 conv = build_conv (RVALUE_CONV, t2, conv);
3045 return conv;
3047 else
3048 return NULL_TREE;
3050 else
3051 /* [expr.cond]
3053 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
3054 converted to the type that expression E2 would have if E2 were
3055 converted to an rvalue (or the type it has, if E2 is an rvalue). */
3056 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
3059 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
3060 arguments to the conditional expression. */
3062 tree
3063 build_conditional_expr (tree arg1, tree arg2, tree arg3)
3065 tree arg2_type;
3066 tree arg3_type;
3067 tree result;
3068 tree result_type = NULL_TREE;
3069 bool lvalue_p = true;
3070 struct z_candidate *candidates = 0;
3071 struct z_candidate *cand;
3073 /* As a G++ extension, the second argument to the conditional can be
3074 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
3075 c'.) If the second operand is omitted, make sure it is
3076 calculated only once. */
3077 if (!arg2)
3079 if (pedantic)
3080 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
3082 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
3083 if (real_lvalue_p (arg1))
3084 arg2 = arg1 = stabilize_reference (arg1);
3085 else
3086 arg2 = arg1 = save_expr (arg1);
3089 /* [expr.cond]
3091 The first expr ession is implicitly converted to bool (clause
3092 _conv_). */
3093 arg1 = perform_implicit_conversion (boolean_type_node, arg1);
3095 /* If something has already gone wrong, just pass that fact up the
3096 tree. */
3097 if (error_operand_p (arg1)
3098 || error_operand_p (arg2)
3099 || error_operand_p (arg3))
3100 return error_mark_node;
3102 /* [expr.cond]
3104 If either the second or the third operand has type (possibly
3105 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3106 array-to-pointer (_conv.array_), and function-to-pointer
3107 (_conv.func_) standard conversions are performed on the second
3108 and third operands. */
3109 arg2_type = TREE_TYPE (arg2);
3110 arg3_type = TREE_TYPE (arg3);
3111 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3113 /* Do the conversions. We don't these for `void' type arguments
3114 since it can't have any effect and since decay_conversion
3115 does not handle that case gracefully. */
3116 if (!VOID_TYPE_P (arg2_type))
3117 arg2 = decay_conversion (arg2);
3118 if (!VOID_TYPE_P (arg3_type))
3119 arg3 = decay_conversion (arg3);
3120 arg2_type = TREE_TYPE (arg2);
3121 arg3_type = TREE_TYPE (arg3);
3123 /* [expr.cond]
3125 One of the following shall hold:
3127 --The second or the third operand (but not both) is a
3128 throw-expression (_except.throw_); the result is of the
3129 type of the other and is an rvalue.
3131 --Both the second and the third operands have type void; the
3132 result is of type void and is an rvalue. */
3133 if ((TREE_CODE (arg2) == THROW_EXPR)
3134 ^ (TREE_CODE (arg3) == THROW_EXPR))
3135 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
3136 ? arg3_type : arg2_type);
3137 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3138 result_type = void_type_node;
3139 else
3141 error ("`%E' has type `void' and is not a throw-expression",
3142 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3143 return error_mark_node;
3146 lvalue_p = false;
3147 goto valid_operands;
3149 /* [expr.cond]
3151 Otherwise, if the second and third operand have different types,
3152 and either has (possibly cv-qualified) class type, an attempt is
3153 made to convert each of those operands to the type of the other. */
3154 else if (!same_type_p (arg2_type, arg3_type)
3155 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3157 tree conv2 = conditional_conversion (arg2, arg3);
3158 tree conv3 = conditional_conversion (arg3, arg2);
3160 /* [expr.cond]
3162 If both can be converted, or one can be converted but the
3163 conversion is ambiguous, the program is ill-formed. If
3164 neither can be converted, the operands are left unchanged and
3165 further checking is performed as described below. If exactly
3166 one conversion is possible, that conversion is applied to the
3167 chosen operand and the converted operand is used in place of
3168 the original operand for the remainder of this section. */
3169 if ((conv2 && !ICS_BAD_FLAG (conv2)
3170 && conv3 && !ICS_BAD_FLAG (conv3))
3171 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3172 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3174 error ("operands to ?: have different types");
3175 return error_mark_node;
3177 else if (conv2 && !ICS_BAD_FLAG (conv2))
3179 arg2 = convert_like (conv2, arg2);
3180 arg2 = convert_from_reference (arg2);
3181 if (!same_type_p (TREE_TYPE (arg2), arg3_type)
3182 && CLASS_TYPE_P (arg3_type))
3183 /* The types need to match if we're converting to a class type.
3184 If not, we don't care about cv-qual mismatches, since
3185 non-class rvalues are not cv-qualified. */
3186 abort ();
3187 arg2_type = TREE_TYPE (arg2);
3189 else if (conv3 && !ICS_BAD_FLAG (conv3))
3191 arg3 = convert_like (conv3, arg3);
3192 arg3 = convert_from_reference (arg3);
3193 if (!same_type_p (TREE_TYPE (arg3), arg2_type)
3194 && CLASS_TYPE_P (arg2_type))
3195 abort ();
3196 arg3_type = TREE_TYPE (arg3);
3200 /* [expr.cond]
3202 If the second and third operands are lvalues and have the same
3203 type, the result is of that type and is an lvalue. */
3204 if (real_lvalue_p (arg2)
3205 && real_lvalue_p (arg3)
3206 && same_type_p (arg2_type, arg3_type))
3208 result_type = arg2_type;
3209 goto valid_operands;
3212 /* [expr.cond]
3214 Otherwise, the result is an rvalue. If the second and third
3215 operand do not have the same type, and either has (possibly
3216 cv-qualified) class type, overload resolution is used to
3217 determine the conversions (if any) to be applied to the operands
3218 (_over.match.oper_, _over.built_). */
3219 lvalue_p = false;
3220 if (!same_type_p (arg2_type, arg3_type)
3221 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3223 tree args[3];
3224 tree conv;
3225 bool any_viable_p;
3227 /* Rearrange the arguments so that add_builtin_candidate only has
3228 to know about two args. In build_builtin_candidates, the
3229 arguments are unscrambled. */
3230 args[0] = arg2;
3231 args[1] = arg3;
3232 args[2] = arg1;
3233 add_builtin_candidates (&candidates,
3234 COND_EXPR,
3235 NOP_EXPR,
3236 ansi_opname (COND_EXPR),
3237 args,
3238 LOOKUP_NORMAL);
3240 /* [expr.cond]
3242 If the overload resolution fails, the program is
3243 ill-formed. */
3244 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3245 if (!any_viable_p)
3247 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3248 print_z_candidates (candidates);
3249 return error_mark_node;
3251 cand = tourney (candidates);
3252 if (!cand)
3254 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3255 print_z_candidates (candidates);
3256 return error_mark_node;
3259 /* [expr.cond]
3261 Otherwise, the conversions thus determined are applied, and
3262 the converted operands are used in place of the original
3263 operands for the remainder of this section. */
3264 conv = TREE_VEC_ELT (cand->convs, 0);
3265 arg1 = convert_like (conv, arg1);
3266 conv = TREE_VEC_ELT (cand->convs, 1);
3267 arg2 = convert_like (conv, arg2);
3268 conv = TREE_VEC_ELT (cand->convs, 2);
3269 arg3 = convert_like (conv, arg3);
3272 /* [expr.cond]
3274 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3275 and function-to-pointer (_conv.func_) standard conversions are
3276 performed on the second and third operands.
3278 We need to force the lvalue-to-rvalue conversion here for class types,
3279 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3280 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3281 regions.
3283 We use ocp_convert rather than build_user_type_conversion because the
3284 latter returns NULL_TREE on failure, while the former gives an error. */
3286 arg2 = force_rvalue (arg2);
3287 arg2_type = TREE_TYPE (arg2);
3289 arg3 = force_rvalue (arg3);
3290 arg3_type = TREE_TYPE (arg3);
3292 if (arg2 == error_mark_node || arg3 == error_mark_node)
3293 return error_mark_node;
3295 /* [expr.cond]
3297 After those conversions, one of the following shall hold:
3299 --The second and third operands have the same type; the result is of
3300 that type. */
3301 if (same_type_p (arg2_type, arg3_type))
3302 result_type = arg2_type;
3303 /* [expr.cond]
3305 --The second and third operands have arithmetic or enumeration
3306 type; the usual arithmetic conversions are performed to bring
3307 them to a common type, and the result is of that type. */
3308 else if ((ARITHMETIC_TYPE_P (arg2_type)
3309 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3310 && (ARITHMETIC_TYPE_P (arg3_type)
3311 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3313 /* In this case, there is always a common type. */
3314 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3315 arg3_type);
3317 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3318 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3319 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3320 arg2_type, arg3_type);
3321 else if (extra_warnings
3322 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3323 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3324 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3325 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3326 warning ("enumeral and non-enumeral type in conditional expression");
3328 arg2 = perform_implicit_conversion (result_type, arg2);
3329 arg3 = perform_implicit_conversion (result_type, arg3);
3331 /* [expr.cond]
3333 --The second and third operands have pointer type, or one has
3334 pointer type and the other is a null pointer constant; pointer
3335 conversions (_conv.ptr_) and qualification conversions
3336 (_conv.qual_) are performed to bring them to their composite
3337 pointer type (_expr.rel_). The result is of the composite
3338 pointer type.
3340 --The second and third operands have pointer to member type, or
3341 one has pointer to member type and the other is a null pointer
3342 constant; pointer to member conversions (_conv.mem_) and
3343 qualification conversions (_conv.qual_) are performed to bring
3344 them to a common type, whose cv-qualification shall match the
3345 cv-qualification of either the second or the third operand.
3346 The result is of the common type. */
3347 else if ((null_ptr_cst_p (arg2)
3348 && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
3349 || (null_ptr_cst_p (arg3)
3350 && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
3351 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3352 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3353 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
3355 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3356 arg3, "conditional expression");
3357 if (result_type == error_mark_node)
3358 return error_mark_node;
3359 arg2 = perform_implicit_conversion (result_type, arg2);
3360 arg3 = perform_implicit_conversion (result_type, arg3);
3363 if (!result_type)
3365 error ("operands to ?: have different types");
3366 return error_mark_node;
3369 valid_operands:
3370 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3371 /* We can't use result_type below, as fold might have returned a
3372 throw_expr. */
3374 /* Expand both sides into the same slot, hopefully the target of the
3375 ?: expression. We used to check for TARGET_EXPRs here, but now we
3376 sometimes wrap them in NOP_EXPRs so the test would fail. */
3377 if (!lvalue_p && IS_AGGR_TYPE (TREE_TYPE (result)))
3378 result = get_target_expr (result);
3380 /* If this expression is an rvalue, but might be mistaken for an
3381 lvalue, we must add a NON_LVALUE_EXPR. */
3382 if (!lvalue_p && real_lvalue_p (result))
3383 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
3385 return result;
3388 /* OPERAND is an operand to an expression. Perform necessary steps
3389 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
3390 returned. */
3392 static tree
3393 prep_operand (tree operand)
3395 if (operand)
3397 operand = convert_from_reference (operand);
3398 if (CLASS_TYPE_P (TREE_TYPE (operand))
3399 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
3400 /* Make sure the template type is instantiated now. */
3401 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
3404 return operand;
3407 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
3408 OVERLOAD) to the CANDIDATES, returning an updated list of
3409 CANDIDATES. The ARGS are the arguments provided to the call,
3410 without any implicit object parameter. The EXPLICIT_TARGS are
3411 explicit template arguments provided. TEMPLATE_ONLY is true if
3412 only template functions should be considered. CONVERSION_PATH,
3413 ACCESS_PATH, and FLAGS are as for add_function_candidate. */
3415 static void
3416 add_candidates (tree fns, tree args,
3417 tree explicit_targs, bool template_only,
3418 tree conversion_path, tree access_path,
3419 int flags,
3420 struct z_candidate **candidates)
3422 tree ctype;
3423 tree non_static_args;
3425 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
3426 /* Delay creating the implicit this parameter until it is needed. */
3427 non_static_args = NULL_TREE;
3429 while (fns)
3431 tree fn;
3432 tree fn_args;
3434 fn = OVL_CURRENT (fns);
3435 /* Figure out which set of arguments to use. */
3436 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3438 /* If this function is a non-static member, prepend the implicit
3439 object parameter. */
3440 if (!non_static_args)
3441 non_static_args = tree_cons (NULL_TREE,
3442 build_this (TREE_VALUE (args)),
3443 TREE_CHAIN (args));
3444 fn_args = non_static_args;
3446 else
3447 /* Otherwise, just use the list of arguments provided. */
3448 fn_args = args;
3450 if (TREE_CODE (fn) == TEMPLATE_DECL)
3451 add_template_candidate (candidates,
3452 fn,
3453 ctype,
3454 explicit_targs,
3455 fn_args,
3456 NULL_TREE,
3457 access_path,
3458 conversion_path,
3459 flags,
3460 DEDUCE_CALL);
3461 else if (!template_only)
3462 add_function_candidate (candidates,
3464 ctype,
3465 fn_args,
3466 access_path,
3467 conversion_path,
3468 flags);
3469 fns = OVL_NEXT (fns);
3473 tree
3474 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3)
3476 struct z_candidate *candidates = 0, *cand;
3477 tree arglist, fnname;
3478 tree args[3];
3479 enum tree_code code2 = NOP_EXPR;
3480 tree conv;
3481 bool strict_p;
3482 bool any_viable_p;
3484 if (error_operand_p (arg1)
3485 || error_operand_p (arg2)
3486 || error_operand_p (arg3))
3487 return error_mark_node;
3489 if (code == MODIFY_EXPR)
3491 code2 = TREE_CODE (arg3);
3492 arg3 = NULL_TREE;
3493 fnname = ansi_assopname (code2);
3495 else
3496 fnname = ansi_opname (code);
3498 arg1 = prep_operand (arg1);
3500 switch (code)
3502 case NEW_EXPR:
3503 case VEC_NEW_EXPR:
3504 case VEC_DELETE_EXPR:
3505 case DELETE_EXPR:
3506 /* Use build_op_new_call and build_op_delete_call instead. */
3507 abort ();
3509 case CALL_EXPR:
3510 return build_object_call (arg1, arg2);
3512 default:
3513 break;
3516 arg2 = prep_operand (arg2);
3517 arg3 = prep_operand (arg3);
3519 if (code == COND_EXPR)
3521 if (arg2 == NULL_TREE
3522 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3523 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3524 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3525 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3526 goto builtin;
3528 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3529 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3530 goto builtin;
3532 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3533 arg2 = integer_zero_node;
3535 arglist = NULL_TREE;
3536 if (arg3)
3537 arglist = tree_cons (NULL_TREE, arg3, arglist);
3538 if (arg2)
3539 arglist = tree_cons (NULL_TREE, arg2, arglist);
3540 arglist = tree_cons (NULL_TREE, arg1, arglist);
3542 /* Add namespace-scope operators to the list of functions to
3543 consider. */
3544 add_candidates (lookup_function_nonclass (fnname, arglist),
3545 arglist, NULL_TREE, false, NULL_TREE, NULL_TREE,
3546 flags, &candidates);
3547 /* Add class-member operators to the candidate set. */
3548 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
3550 tree fns;
3552 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3553 if (fns == error_mark_node)
3554 return fns;
3555 if (fns)
3556 add_candidates (BASELINK_FUNCTIONS (fns), arglist,
3557 NULL_TREE, false,
3558 BASELINK_BINFO (fns),
3559 TYPE_BINFO (TREE_TYPE (arg1)),
3560 flags, &candidates);
3563 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3564 to know about two args; a builtin candidate will always have a first
3565 parameter of type bool. We'll handle that in
3566 build_builtin_candidate. */
3567 if (code == COND_EXPR)
3569 args[0] = arg2;
3570 args[1] = arg3;
3571 args[2] = arg1;
3573 else
3575 args[0] = arg1;
3576 args[1] = arg2;
3577 args[2] = NULL_TREE;
3580 add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
3582 switch (code)
3584 case COMPOUND_EXPR:
3585 case ADDR_EXPR:
3586 /* For these, the built-in candidates set is empty
3587 [over.match.oper]/3. We don't want non-strict matches
3588 because exact matches are always possible with built-in
3589 operators. The built-in candidate set for COMPONENT_REF
3590 would be empty too, but since there are no such built-in
3591 operators, we accept non-strict matches for them. */
3592 strict_p = true;
3593 break;
3595 default:
3596 strict_p = pedantic;
3597 break;
3600 candidates = splice_viable (candidates, strict_p, &any_viable_p);
3601 if (!any_viable_p)
3603 switch (code)
3605 case POSTINCREMENT_EXPR:
3606 case POSTDECREMENT_EXPR:
3607 /* Look for an `operator++ (int)'. If they didn't have
3608 one, then we fall back to the old way of doing things. */
3609 if (flags & LOOKUP_COMPLAIN)
3610 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3611 fnname,
3612 operator_name_info[code].name);
3613 if (code == POSTINCREMENT_EXPR)
3614 code = PREINCREMENT_EXPR;
3615 else
3616 code = PREDECREMENT_EXPR;
3617 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3619 /* The caller will deal with these. */
3620 case ADDR_EXPR:
3621 case COMPOUND_EXPR:
3622 case COMPONENT_REF:
3623 return NULL_TREE;
3625 default:
3626 break;
3628 if (flags & LOOKUP_COMPLAIN)
3630 op_error (code, code2, arg1, arg2, arg3, "no match");
3631 print_z_candidates (candidates);
3633 return error_mark_node;
3636 cand = tourney (candidates);
3637 if (cand == 0)
3639 if (flags & LOOKUP_COMPLAIN)
3641 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3642 print_z_candidates (candidates);
3644 return error_mark_node;
3647 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3649 if (warn_synth
3650 && fnname == ansi_assopname (NOP_EXPR)
3651 && DECL_ARTIFICIAL (cand->fn)
3652 && candidates->next
3653 && ! candidates->next->next)
3655 warning ("using synthesized `%#D' for copy assignment",
3656 cand->fn);
3657 cp_warning_at (" where cfront would use `%#D'",
3658 cand == candidates
3659 ? candidates->next->fn
3660 : candidates->fn);
3663 return build_over_call (cand, LOOKUP_NORMAL);
3666 /* Check for comparison of different enum types. */
3667 switch (code)
3669 case GT_EXPR:
3670 case LT_EXPR:
3671 case GE_EXPR:
3672 case LE_EXPR:
3673 case EQ_EXPR:
3674 case NE_EXPR:
3675 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3676 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3677 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3678 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3680 warning ("comparison between `%#T' and `%#T'",
3681 TREE_TYPE (arg1), TREE_TYPE (arg2));
3683 break;
3684 default:
3685 break;
3688 /* We need to strip any leading REF_BIND so that bitfields don't cause
3689 errors. This should not remove any important conversions, because
3690 builtins don't apply to class objects directly. */
3691 conv = TREE_VEC_ELT (cand->convs, 0);
3692 if (TREE_CODE (conv) == REF_BIND)
3693 conv = TREE_OPERAND (conv, 0);
3694 arg1 = convert_like (conv, arg1);
3695 if (arg2)
3697 conv = TREE_VEC_ELT (cand->convs, 1);
3698 if (TREE_CODE (conv) == REF_BIND)
3699 conv = TREE_OPERAND (conv, 0);
3700 arg2 = convert_like (conv, arg2);
3702 if (arg3)
3704 conv = TREE_VEC_ELT (cand->convs, 2);
3705 if (TREE_CODE (conv) == REF_BIND)
3706 conv = TREE_OPERAND (conv, 0);
3707 arg3 = convert_like (conv, arg3);
3710 builtin:
3711 switch (code)
3713 case MODIFY_EXPR:
3714 return build_modify_expr (arg1, code2, arg2);
3716 case INDIRECT_REF:
3717 return build_indirect_ref (arg1, "unary *");
3719 case PLUS_EXPR:
3720 case MINUS_EXPR:
3721 case MULT_EXPR:
3722 case TRUNC_DIV_EXPR:
3723 case GT_EXPR:
3724 case LT_EXPR:
3725 case GE_EXPR:
3726 case LE_EXPR:
3727 case EQ_EXPR:
3728 case NE_EXPR:
3729 case MAX_EXPR:
3730 case MIN_EXPR:
3731 case LSHIFT_EXPR:
3732 case RSHIFT_EXPR:
3733 case TRUNC_MOD_EXPR:
3734 case BIT_AND_EXPR:
3735 case BIT_IOR_EXPR:
3736 case BIT_XOR_EXPR:
3737 case TRUTH_ANDIF_EXPR:
3738 case TRUTH_ORIF_EXPR:
3739 return cp_build_binary_op (code, arg1, arg2);
3741 case CONVERT_EXPR:
3742 case NEGATE_EXPR:
3743 case BIT_NOT_EXPR:
3744 case TRUTH_NOT_EXPR:
3745 case PREINCREMENT_EXPR:
3746 case POSTINCREMENT_EXPR:
3747 case PREDECREMENT_EXPR:
3748 case POSTDECREMENT_EXPR:
3749 case REALPART_EXPR:
3750 case IMAGPART_EXPR:
3751 return build_unary_op (code, arg1, candidates != 0);
3753 case ARRAY_REF:
3754 return build_array_ref (arg1, arg2);
3756 case COND_EXPR:
3757 return build_conditional_expr (arg1, arg2, arg3);
3759 case MEMBER_REF:
3760 return build_m_component_ref
3761 (build_indirect_ref (arg1, NULL), arg2);
3763 /* The caller will deal with these. */
3764 case ADDR_EXPR:
3765 case COMPONENT_REF:
3766 case COMPOUND_EXPR:
3767 return NULL_TREE;
3769 default:
3770 abort ();
3771 return NULL_TREE;
3775 /* Build a call to operator delete. This has to be handled very specially,
3776 because the restrictions on what signatures match are different from all
3777 other call instances. For a normal delete, only a delete taking (void *)
3778 or (void *, size_t) is accepted. For a placement delete, only an exact
3779 match with the placement new is accepted.
3781 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3782 ADDR is the pointer to be deleted.
3783 SIZE is the size of the memory block to be deleted.
3784 FLAGS are the usual overloading flags.
3785 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3787 tree
3788 build_op_delete_call (enum tree_code code, tree addr, tree size,
3789 int flags, tree placement)
3791 tree fn = NULL_TREE;
3792 tree fns, fnname, argtypes, args, type;
3793 int pass;
3795 if (addr == error_mark_node)
3796 return error_mark_node;
3798 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
3800 fnname = ansi_opname (code);
3802 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3803 /* In [class.free]
3805 If the result of the lookup is ambiguous or inaccessible, or if
3806 the lookup selects a placement deallocation function, the
3807 program is ill-formed.
3809 Therefore, we ask lookup_fnfields to complain about ambiguity. */
3811 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3812 if (fns == error_mark_node)
3813 return error_mark_node;
3815 else
3816 fns = NULL_TREE;
3818 if (fns == NULL_TREE)
3819 fns = lookup_name_nonclass (fnname);
3821 if (placement)
3823 tree alloc_fn;
3824 tree call_expr;
3826 /* Find the allocation function that is being called. */
3827 call_expr = placement;
3828 /* Extract the function. */
3829 alloc_fn = get_callee_fndecl (call_expr);
3830 my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3831 /* Then the second parm type. */
3832 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3833 /* Also the second argument. */
3834 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3836 else
3838 /* First try it without the size argument. */
3839 argtypes = void_list_node;
3840 args = NULL_TREE;
3843 /* Strip const and volatile from addr. */
3844 addr = cp_convert (ptr_type_node, addr);
3846 /* We make two tries at finding a matching `operator delete'. On
3847 the first pass, we look for a one-operator (or placement)
3848 operator delete. If we're not doing placement delete, then on
3849 the second pass we look for a two-argument delete. */
3850 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3852 /* Go through the `operator delete' functions looking for one
3853 with a matching type. */
3854 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3855 fn;
3856 fn = OVL_NEXT (fn))
3858 tree t;
3860 /* The first argument must be "void *". */
3861 t = TYPE_ARG_TYPES (TREE_TYPE (OVL_CURRENT (fn)));
3862 if (!same_type_p (TREE_VALUE (t), ptr_type_node))
3863 continue;
3864 t = TREE_CHAIN (t);
3865 /* On the first pass, check the rest of the arguments. */
3866 if (pass == 0)
3868 while (argtypes && t)
3870 if (!same_type_p (TREE_VALUE (argtypes),
3871 TREE_VALUE (t)))
3872 break;
3873 argtypes = TREE_CHAIN (argtypes);
3874 t = TREE_CHAIN (t);
3876 if (!argtypes && !t)
3877 break;
3879 /* On the second pass, the second argument must be
3880 "size_t". */
3881 else if (pass == 1
3882 && same_type_p (TREE_VALUE (t), sizetype)
3883 && TREE_CHAIN (t) == void_list_node)
3884 break;
3887 /* If we found a match, we're done. */
3888 if (fn)
3889 break;
3892 /* If we have a matching function, call it. */
3893 if (fn)
3895 /* Make sure we have the actual function, and not an
3896 OVERLOAD. */
3897 fn = OVL_CURRENT (fn);
3899 /* If the FN is a member function, make sure that it is
3900 accessible. */
3901 if (DECL_CLASS_SCOPE_P (fn))
3902 perform_or_defer_access_check (TYPE_BINFO (type), fn);
3904 if (pass == 0)
3905 args = tree_cons (NULL_TREE, addr, args);
3906 else
3907 args = tree_cons (NULL_TREE, addr,
3908 build_tree_list (NULL_TREE, size));
3910 if (placement)
3912 /* The placement args might not be suitable for overload
3913 resolution at this point, so build the call directly. */
3914 mark_used (fn);
3915 return build_cxx_call (fn, args, args);
3917 else
3918 return build_function_call (fn, args);
3921 /* If we are doing placement delete we do nothing if we don't find a
3922 matching op delete. */
3923 if (placement)
3924 return NULL_TREE;
3926 error ("no suitable `operator %s' for `%T'",
3927 operator_name_info[(int)code].name, type);
3928 return error_mark_node;
3931 /* If the current scope isn't allowed to access DECL along
3932 BASETYPE_PATH, give an error. The most derived class in
3933 BASETYPE_PATH is the one used to qualify DECL. */
3935 bool
3936 enforce_access (tree basetype_path, tree decl)
3938 my_friendly_assert (TREE_CODE (basetype_path) == TREE_VEC, 20030624);
3940 if (!accessible_p (basetype_path, decl))
3942 if (TREE_PRIVATE (decl))
3943 cp_error_at ("`%+#D' is private", decl);
3944 else if (TREE_PROTECTED (decl))
3945 cp_error_at ("`%+#D' is protected", decl);
3946 else
3947 cp_error_at ("`%+#D' is inaccessible", decl);
3948 error ("within this context");
3949 return false;
3952 return true;
3955 /* Perform the conversions in CONVS on the expression EXPR. FN and
3956 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3957 indicates the `this' argument of a method. INNER is nonzero when
3958 being called to continue a conversion chain. It is negative when a
3959 reference binding will be applied, positive otherwise. If
3960 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
3961 conversions will be emitted if appropriate. */
3963 static tree
3964 convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner,
3965 bool issue_conversion_warnings)
3967 int savew, savee;
3969 tree totype = TREE_TYPE (convs);
3971 if (ICS_BAD_FLAG (convs)
3972 && TREE_CODE (convs) != USER_CONV
3973 && TREE_CODE (convs) != AMBIG_CONV
3974 && TREE_CODE (convs) != REF_BIND)
3976 tree t = convs;
3977 for (; t; t = TREE_OPERAND (t, 0))
3979 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3981 expr = convert_like_real (t, expr, fn, argnum, 1,
3982 /*issue_conversion_warnings=*/false);
3983 break;
3985 else if (TREE_CODE (t) == AMBIG_CONV)
3986 return convert_like_real (t, expr, fn, argnum, 1,
3987 /*issue_conversion_warnings=*/false);
3988 else if (TREE_CODE (t) == IDENTITY_CONV)
3989 break;
3991 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3992 if (fn)
3993 pedwarn (" initializing argument %P of `%D'", argnum, fn);
3994 return cp_convert (totype, expr);
3997 if (issue_conversion_warnings)
3998 expr = dubious_conversion_warnings
3999 (totype, expr, "argument", fn, argnum);
4000 switch (TREE_CODE (convs))
4002 case USER_CONV:
4004 struct z_candidate *cand = USER_CONV_CAND (convs);
4005 tree convfn = cand->fn;
4006 tree args;
4008 if (DECL_CONSTRUCTOR_P (convfn))
4010 tree t = build_int_2 (0, 0);
4011 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
4013 args = build_tree_list (NULL_TREE, expr);
4014 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
4015 || DECL_HAS_VTT_PARM_P (convfn))
4016 /* We should never try to call the abstract or base constructor
4017 from here. */
4018 abort ();
4019 args = tree_cons (NULL_TREE, t, args);
4021 else
4022 args = build_this (expr);
4023 expr = build_over_call (cand, LOOKUP_NORMAL);
4025 /* If this is a constructor or a function returning an aggr type,
4026 we need to build up a TARGET_EXPR. */
4027 if (DECL_CONSTRUCTOR_P (convfn))
4028 expr = build_cplus_new (totype, expr);
4030 /* The result of the call is then used to direct-initialize the object
4031 that is the destination of the copy-initialization. [dcl.init]
4033 Note that this step is not reflected in the conversion sequence;
4034 it affects the semantics when we actually perform the
4035 conversion, but is not considered during overload resolution.
4037 If the target is a class, that means call a ctor. */
4038 if (IS_AGGR_TYPE (totype)
4039 && (inner >= 0 || !lvalue_p (expr)))
4041 savew = warningcount, savee = errorcount;
4042 expr = build_special_member_call
4043 (NULL_TREE, complete_ctor_identifier,
4044 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
4045 /* Core issue 84, now a DR, says that we don't allow UDCs
4046 for these args (which deliberately breaks copy-init of an
4047 auto_ptr<Base> from an auto_ptr<Derived>). */
4048 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
4050 /* Tell the user where this failing constructor call came from. */
4051 if (fn)
4053 if (warningcount > savew)
4054 warning
4055 (" initializing argument %P of `%D' from result of `%D'",
4056 argnum, fn, convfn);
4057 else if (errorcount > savee)
4058 error
4059 (" initializing argument %P of `%D' from result of `%D'",
4060 argnum, fn, convfn);
4062 else
4064 if (warningcount > savew)
4065 warning (" initializing temporary from result of `%D'",
4066 convfn);
4067 else if (errorcount > savee)
4068 error (" initializing temporary from result of `%D'",
4069 convfn);
4071 expr = build_cplus_new (totype, expr);
4073 return expr;
4075 case IDENTITY_CONV:
4076 if (type_unknown_p (expr))
4077 expr = instantiate_type (totype, expr, tf_error | tf_warning);
4078 /* Convert a non-array constant variable to its underlying value, unless we
4079 are about to bind it to a reference, in which case we need to
4080 leave it as an lvalue. */
4081 if (inner >= 0
4082 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
4083 expr = decl_constant_value (expr);
4084 return expr;
4085 case AMBIG_CONV:
4086 /* Call build_user_type_conversion again for the error. */
4087 return build_user_type_conversion
4088 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
4090 default:
4091 break;
4094 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
4095 TREE_CODE (convs) == REF_BIND ? -1 : 1,
4096 /*issue_conversion_warnings=*/false);
4097 if (expr == error_mark_node)
4098 return error_mark_node;
4100 switch (TREE_CODE (convs))
4102 case RVALUE_CONV:
4103 if (! IS_AGGR_TYPE (totype))
4104 return expr;
4105 /* Else fall through. */
4106 case BASE_CONV:
4107 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
4109 /* We are going to bind a reference directly to a base-class
4110 subobject of EXPR. */
4111 tree base_ptr = build_pointer_type (totype);
4113 /* Build an expression for `*((base*) &expr)'. */
4114 expr = build_unary_op (ADDR_EXPR, expr, 0);
4115 expr = perform_implicit_conversion (base_ptr, expr);
4116 expr = build_indirect_ref (expr, "implicit conversion");
4117 return expr;
4120 /* Copy-initialization where the cv-unqualified version of the source
4121 type is the same class as, or a derived class of, the class of the
4122 destination [is treated as direct-initialization]. [dcl.init] */
4123 savew = warningcount, savee = errorcount;
4124 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4125 build_tree_list (NULL_TREE, expr),
4126 TYPE_BINFO (totype),
4127 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
4128 if (fn)
4130 if (warningcount > savew)
4131 warning (" initializing argument %P of `%D'", argnum, fn);
4132 else if (errorcount > savee)
4133 error (" initializing argument %P of `%D'", argnum, fn);
4135 return build_cplus_new (totype, expr);
4137 case REF_BIND:
4139 tree ref_type = totype;
4141 /* If necessary, create a temporary. */
4142 if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
4144 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
4146 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
4148 /* If the reference is volatile or non-const, we
4149 cannot create a temporary. */
4150 cp_lvalue_kind lvalue = real_lvalue_p (expr);
4152 if (lvalue & clk_bitfield)
4153 error ("cannot bind bitfield `%E' to `%T'",
4154 expr, ref_type);
4155 else if (lvalue & clk_packed)
4156 error ("cannot bind packed field `%E' to `%T'",
4157 expr, ref_type);
4158 else
4159 error ("cannot bind rvalue `%E' to `%T'", expr, ref_type);
4160 return error_mark_node;
4162 expr = build_target_expr_with_type (expr, type);
4165 /* Take the address of the thing to which we will bind the
4166 reference. */
4167 expr = build_unary_op (ADDR_EXPR, expr, 1);
4168 if (expr == error_mark_node)
4169 return error_mark_node;
4171 /* Convert it to a pointer to the type referred to by the
4172 reference. This will adjust the pointer if a derived to
4173 base conversion is being performed. */
4174 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4175 expr);
4176 /* Convert the pointer to the desired reference type. */
4177 return build_nop (ref_type, expr);
4180 case LVALUE_CONV:
4181 return decay_conversion (expr);
4183 case QUAL_CONV:
4184 /* Warn about deprecated conversion if appropriate. */
4185 string_conv_p (totype, expr, 1);
4186 break;
4188 default:
4189 break;
4191 return ocp_convert (totype, expr, CONV_IMPLICIT,
4192 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4195 /* Build a call to __builtin_trap which can be used as an expression of
4196 type TYPE. */
4198 static tree
4199 call_builtin_trap (tree type)
4201 tree fn = IDENTIFIER_GLOBAL_VALUE (get_identifier ("__builtin_trap"));
4203 my_friendly_assert (fn != NULL, 20030927);
4204 fn = build_call (fn, NULL_TREE);
4205 fn = build (COMPOUND_EXPR, type, fn, error_mark_node);
4206 fn = force_target_expr (type, fn);
4207 return fn;
4210 /* ARG is being passed to a varargs function. Perform any conversions
4211 required. Return the converted value. */
4213 tree
4214 convert_arg_to_ellipsis (tree arg)
4216 /* [expr.call]
4218 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
4219 standard conversions are performed. */
4220 arg = decay_conversion (arg);
4221 /* [expr.call]
4223 If the argument has integral or enumeration type that is subject
4224 to the integral promotions (_conv.prom_), or a floating point
4225 type that is subject to the floating point promotion
4226 (_conv.fpprom_), the value of the argument is converted to the
4227 promoted type before the call. */
4228 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4229 && (TYPE_PRECISION (TREE_TYPE (arg))
4230 < TYPE_PRECISION (double_type_node)))
4231 arg = convert_to_real (double_type_node, arg);
4232 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
4233 arg = perform_integral_promotions (arg);
4235 arg = require_complete_type (arg);
4237 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
4239 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
4240 here and do a bitwise copy, but now cp_expr_size will abort if we
4241 try to do that. */
4242 warning ("cannot pass objects of non-POD type `%#T' through `...'; \
4243 call will abort at runtime",
4244 TREE_TYPE (arg));
4245 arg = call_builtin_trap (TREE_TYPE (arg));
4248 return arg;
4251 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4253 tree
4254 build_x_va_arg (tree expr, tree type)
4256 if (processing_template_decl)
4257 return build_min (VA_ARG_EXPR, type, expr);
4259 type = complete_type_or_else (type, NULL_TREE);
4261 if (expr == error_mark_node || !type)
4262 return error_mark_node;
4264 if (! pod_type_p (type))
4266 /* Undefined behavior [expr.call] 5.2.2/7. */
4267 warning ("cannot receive objects of non-POD type `%#T' through `...'; \
4268 call will abort at runtime",
4269 type);
4270 return call_builtin_trap (type);
4273 return build_va_arg (expr, type);
4276 /* TYPE has been given to va_arg. Apply the default conversions which
4277 would have happened when passed via ellipsis. Return the promoted
4278 type, or the passed type if there is no change. */
4280 tree
4281 cxx_type_promotes_to (tree type)
4283 tree promote;
4285 /* Perform the array-to-pointer and function-to-pointer
4286 conversions. */
4287 type = type_decays_to (type);
4289 promote = type_promotes_to (type);
4290 if (same_type_p (type, promote))
4291 promote = type;
4293 return promote;
4296 /* ARG is a default argument expression being passed to a parameter of
4297 the indicated TYPE, which is a parameter to FN. Do any required
4298 conversions. Return the converted value. */
4300 tree
4301 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
4303 /* If the ARG is an unparsed default argument expression, the
4304 conversion cannot be performed. */
4305 if (TREE_CODE (arg) == DEFAULT_ARG)
4307 error ("the default argument for parameter %d of `%D' has "
4308 "not yet been parsed",
4309 parmnum, fn);
4310 return error_mark_node;
4313 if (fn && DECL_TEMPLATE_INFO (fn))
4314 arg = tsubst_default_argument (fn, type, arg);
4316 arg = break_out_target_exprs (arg);
4318 if (TREE_CODE (arg) == CONSTRUCTOR)
4320 arg = digest_init (type, arg, 0);
4321 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4322 "default argument", fn, parmnum);
4324 else
4326 /* This could get clobbered by the following call. */
4327 if (TREE_HAS_CONSTRUCTOR (arg))
4328 arg = copy_node (arg);
4330 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4331 "default argument", fn, parmnum);
4332 arg = convert_for_arg_passing (type, arg);
4335 return arg;
4338 /* Returns the type which will really be used for passing an argument of
4339 type TYPE. */
4341 tree
4342 type_passed_as (tree type)
4344 /* Pass classes with copy ctors by invisible reference. */
4345 if (TREE_ADDRESSABLE (type))
4346 type = build_reference_type (type);
4347 else if (PROMOTE_PROTOTYPES
4348 && INTEGRAL_TYPE_P (type)
4349 && COMPLETE_TYPE_P (type)
4350 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4351 TYPE_SIZE (integer_type_node)))
4352 type = integer_type_node;
4354 return type;
4357 /* Actually perform the appropriate conversion. */
4359 tree
4360 convert_for_arg_passing (tree type, tree val)
4362 if (val == error_mark_node)
4364 /* Pass classes with copy ctors by invisible reference. */
4365 else if (TREE_ADDRESSABLE (type))
4366 val = build1 (ADDR_EXPR, build_reference_type (type), val);
4367 else if (PROMOTE_PROTOTYPES
4368 && INTEGRAL_TYPE_P (type)
4369 && COMPLETE_TYPE_P (type)
4370 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4371 TYPE_SIZE (integer_type_node)))
4372 val = perform_integral_promotions (val);
4373 return val;
4376 /* Returns true iff FN is a function with magic varargs, i.e. ones for
4377 which no conversions at all should be done. This is true for some
4378 builtins which don't act like normal functions. */
4380 static bool
4381 magic_varargs_p (tree fn)
4383 if (DECL_BUILT_IN (fn))
4384 switch (DECL_FUNCTION_CODE (fn))
4386 case BUILT_IN_CLASSIFY_TYPE:
4387 case BUILT_IN_CONSTANT_P:
4388 case BUILT_IN_NEXT_ARG:
4389 case BUILT_IN_STDARG_START:
4390 case BUILT_IN_VA_START:
4391 return true;
4393 default:;
4396 return false;
4399 /* Subroutine of the various build_*_call functions. Overload resolution
4400 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4401 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4402 bitmask of various LOOKUP_* flags which apply to the call itself. */
4404 static tree
4405 build_over_call (struct z_candidate *cand, int flags)
4407 tree fn = cand->fn;
4408 tree args = cand->args;
4409 tree convs = cand->convs;
4410 tree converted_args = NULL_TREE;
4411 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4412 tree conv, arg, val;
4413 int i = 0;
4414 int is_method = 0;
4416 /* Give any warnings we noticed during overload resolution. */
4417 if (cand->warnings)
4418 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4419 joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
4421 if (DECL_FUNCTION_MEMBER_P (fn))
4423 /* If FN is a template function, two cases must be considered.
4424 For example:
4426 struct A {
4427 protected:
4428 template <class T> void f();
4430 template <class T> struct B {
4431 protected:
4432 void g();
4434 struct C : A, B<int> {
4435 using A::f; // #1
4436 using B<int>::g; // #2
4439 In case #1 where `A::f' is a member template, DECL_ACCESS is
4440 recorded in the primary template but not in its specialization.
4441 We check access of FN using its primary template.
4443 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
4444 because it is a member of class template B, DECL_ACCESS is
4445 recorded in the specialization `B<int>::g'. We cannot use its
4446 primary template because `B<T>::g' and `B<int>::g' may have
4447 different access. */
4448 if (DECL_TEMPLATE_INFO (fn)
4449 && is_member_template (DECL_TI_TEMPLATE (fn)))
4450 perform_or_defer_access_check (cand->access_path,
4451 DECL_TI_TEMPLATE (fn));
4452 else
4453 perform_or_defer_access_check (cand->access_path, fn);
4456 if (args && TREE_CODE (args) != TREE_LIST)
4457 args = build_tree_list (NULL_TREE, args);
4458 arg = args;
4460 /* The implicit parameters to a constructor are not considered by overload
4461 resolution, and must be of the proper type. */
4462 if (DECL_CONSTRUCTOR_P (fn))
4464 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4465 arg = TREE_CHAIN (arg);
4466 parm = TREE_CHAIN (parm);
4467 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4468 /* We should never try to call the abstract constructor. */
4469 abort ();
4470 if (DECL_HAS_VTT_PARM_P (fn))
4472 converted_args = tree_cons
4473 (NULL_TREE, TREE_VALUE (arg), converted_args);
4474 arg = TREE_CHAIN (arg);
4475 parm = TREE_CHAIN (parm);
4478 /* Bypass access control for 'this' parameter. */
4479 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4481 tree parmtype = TREE_VALUE (parm);
4482 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4483 tree converted_arg;
4484 tree base_binfo;
4486 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4487 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4488 TREE_TYPE (argtype), fn);
4490 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4491 X is called for an object that is not of type X, or of a type
4492 derived from X, the behavior is undefined.
4494 So we can assume that anything passed as 'this' is non-null, and
4495 optimize accordingly. */
4496 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4497 /* Convert to the base in which the function was declared. */
4498 my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4499 converted_arg = build_base_path (PLUS_EXPR,
4500 TREE_VALUE (arg),
4501 cand->conversion_path,
4503 /* Check that the base class is accessible. */
4504 if (!accessible_base_p (TREE_TYPE (argtype),
4505 BINFO_TYPE (cand->conversion_path)))
4506 error ("`%T' is not an accessible base of `%T'",
4507 BINFO_TYPE (cand->conversion_path),
4508 TREE_TYPE (argtype));
4509 /* If fn was found by a using declaration, the conversion path
4510 will be to the derived class, not the base declaring fn. We
4511 must convert from derived to base. */
4512 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4513 TREE_TYPE (parmtype), ba_ignore, NULL);
4514 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4515 base_binfo, 1);
4517 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4518 parm = TREE_CHAIN (parm);
4519 arg = TREE_CHAIN (arg);
4520 ++i;
4521 is_method = 1;
4524 for (; arg && parm;
4525 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4527 tree type = TREE_VALUE (parm);
4529 conv = TREE_VEC_ELT (convs, i);
4530 val = convert_like_with_context
4531 (conv, TREE_VALUE (arg), fn, i - is_method);
4533 val = convert_for_arg_passing (type, val);
4534 converted_args = tree_cons (NULL_TREE, val, converted_args);
4537 /* Default arguments */
4538 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4539 converted_args
4540 = tree_cons (NULL_TREE,
4541 convert_default_arg (TREE_VALUE (parm),
4542 TREE_PURPOSE (parm),
4543 fn, i - is_method),
4544 converted_args);
4546 /* Ellipsis */
4547 for (; arg; arg = TREE_CHAIN (arg))
4549 tree a = TREE_VALUE (arg);
4550 if (magic_varargs_p (fn))
4551 /* Do no conversions for magic varargs. */;
4552 else
4553 a = convert_arg_to_ellipsis (a);
4554 converted_args = tree_cons (NULL_TREE, a, converted_args);
4557 converted_args = nreverse (converted_args);
4559 if (warn_format)
4560 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4561 converted_args);
4563 /* Avoid actually calling copy constructors and copy assignment operators,
4564 if possible. */
4566 if (! flag_elide_constructors)
4567 /* Do things the hard way. */;
4568 else if (TREE_VEC_LENGTH (convs) == 1
4569 && DECL_COPY_CONSTRUCTOR_P (fn))
4571 tree targ;
4572 arg = skip_artificial_parms_for (fn, converted_args);
4573 arg = TREE_VALUE (arg);
4575 /* Pull out the real argument, disregarding const-correctness. */
4576 targ = arg;
4577 while (TREE_CODE (targ) == NOP_EXPR
4578 || TREE_CODE (targ) == NON_LVALUE_EXPR
4579 || TREE_CODE (targ) == CONVERT_EXPR)
4580 targ = TREE_OPERAND (targ, 0);
4581 if (TREE_CODE (targ) == ADDR_EXPR)
4583 targ = TREE_OPERAND (targ, 0);
4584 if (!same_type_ignoring_top_level_qualifiers_p
4585 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4586 targ = NULL_TREE;
4588 else
4589 targ = NULL_TREE;
4591 if (targ)
4592 arg = targ;
4593 else
4594 arg = build_indirect_ref (arg, 0);
4596 /* [class.copy]: the copy constructor is implicitly defined even if
4597 the implementation elided its use. */
4598 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4599 mark_used (fn);
4601 /* If we're creating a temp and we already have one, don't create a
4602 new one. If we're not creating a temp but we get one, use
4603 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4604 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4605 temp or an INIT_EXPR otherwise. */
4606 if (integer_zerop (TREE_VALUE (args)))
4608 if (TREE_CODE (arg) == TARGET_EXPR)
4609 return arg;
4610 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4611 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4613 else if (TREE_CODE (arg) == TARGET_EXPR
4614 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4616 tree address;
4617 tree to = stabilize_reference
4618 (build_indirect_ref (TREE_VALUE (args), 0));
4620 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4621 address = build_unary_op (ADDR_EXPR, val, 0);
4622 /* Avoid a warning about this expression, if the address is
4623 never used. */
4624 TREE_USED (address) = 1;
4625 return address;
4628 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4629 && copy_fn_p (fn)
4630 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4632 tree to = stabilize_reference
4633 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4634 tree type = TREE_TYPE (to);
4635 tree as_base = CLASSTYPE_AS_BASE (type);
4637 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4638 if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
4639 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4640 else
4642 /* We must only copy the non-tail padding parts. Use
4643 CLASSTYPE_AS_BASE for the bitwise copy. */
4644 tree to_as_base, arg_as_base, base_ptr_type;
4646 to = save_expr (to);
4647 base_ptr_type = build_pointer_type (as_base);
4648 to_as_base = build_indirect_ref
4649 (build_nop (base_ptr_type, build_unary_op (ADDR_EXPR, to, 0)), 0);
4650 arg_as_base = build_indirect_ref
4651 (build_nop (base_ptr_type, build_unary_op (ADDR_EXPR, arg, 0)), 0);
4653 val = build (MODIFY_EXPR, as_base, to_as_base, arg_as_base);
4654 val = build (COMPOUND_EXPR, type, convert_to_void (val, NULL), to);
4655 TREE_USED (val) = 1;
4658 return val;
4661 mark_used (fn);
4663 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4665 tree t, *p = &TREE_VALUE (converted_args);
4666 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4667 DECL_CONTEXT (fn),
4668 ba_any, NULL);
4669 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4671 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4672 if (TREE_SIDE_EFFECTS (*p))
4673 *p = save_expr (*p);
4674 t = build_pointer_type (TREE_TYPE (fn));
4675 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4676 fn = build_java_interface_fn_ref (fn, *p);
4677 else
4678 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4679 TREE_TYPE (fn) = t;
4681 else if (DECL_INLINE (fn))
4682 fn = inline_conversion (fn);
4683 else
4684 fn = build_addr_func (fn);
4686 return build_cxx_call (fn, args, converted_args);
4689 /* Build and return a call to FN, using the the CONVERTED_ARGS. ARGS
4690 gives the original form of the arguments. This function performs
4691 no overload resolution, conversion, or other high-level
4692 operations. */
4694 tree
4695 build_cxx_call(tree fn, tree args, tree converted_args)
4697 tree fndecl;
4699 /* Recognize certain built-in functions so we can make tree-codes
4700 other than CALL_EXPR. We do this when it enables fold-const.c
4701 to do something useful. */
4702 if (TREE_CODE (fn) == ADDR_EXPR
4703 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4704 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4706 tree exp;
4707 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4708 if (exp)
4709 return exp;
4712 fn = build_call (fn, converted_args);
4714 /* If this call might throw an exception, note that fact. */
4715 fndecl = get_callee_fndecl (fn);
4716 if ((!fndecl || !TREE_NOTHROW (fndecl))
4717 && at_function_scope_p ()
4718 && cfun)
4719 cp_function_chain->can_throw = 1;
4721 /* Some built-in function calls will be evaluated at compile-time in
4722 fold (). */
4723 fn = fold (fn);
4725 if (VOID_TYPE_P (TREE_TYPE (fn)))
4726 return fn;
4728 fn = require_complete_type (fn);
4729 if (fn == error_mark_node)
4730 return error_mark_node;
4732 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4733 fn = build_cplus_new (TREE_TYPE (fn), fn);
4734 return convert_from_reference (fn);
4737 static GTY(()) tree java_iface_lookup_fn;
4739 /* Make an expression which yields the address of the Java interface
4740 method FN. This is achieved by generating a call to libjava's
4741 _Jv_LookupInterfaceMethodIdx(). */
4743 static tree
4744 build_java_interface_fn_ref (tree fn, tree instance)
4746 tree lookup_args, lookup_fn, method, idx;
4747 tree klass_ref, iface, iface_ref;
4748 int i;
4750 if (!java_iface_lookup_fn)
4752 tree endlink = build_void_list_node ();
4753 tree t = tree_cons (NULL_TREE, ptr_type_node,
4754 tree_cons (NULL_TREE, ptr_type_node,
4755 tree_cons (NULL_TREE, java_int_type_node,
4756 endlink)));
4757 java_iface_lookup_fn
4758 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4759 build_function_type (ptr_type_node, t),
4760 0, NOT_BUILT_IN, NULL, NULL_TREE);
4763 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4764 This is the first entry in the vtable. */
4765 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4766 integer_zero_node);
4768 /* Get the java.lang.Class pointer for the interface being called. */
4769 iface = DECL_CONTEXT (fn);
4770 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
4771 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4772 || DECL_CONTEXT (iface_ref) != iface)
4774 error ("could not find class$ field in java interface type `%T'",
4775 iface);
4776 return error_mark_node;
4778 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4780 /* Determine the itable index of FN. */
4781 i = 1;
4782 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4784 if (!DECL_VIRTUAL_P (method))
4785 continue;
4786 if (fn == method)
4787 break;
4788 i++;
4790 idx = build_int_2 (i, 0);
4792 lookup_args = tree_cons (NULL_TREE, klass_ref,
4793 tree_cons (NULL_TREE, iface_ref,
4794 build_tree_list (NULL_TREE, idx)));
4795 lookup_fn = build1 (ADDR_EXPR,
4796 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4797 java_iface_lookup_fn);
4798 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4801 /* Returns the value to use for the in-charge parameter when making a
4802 call to a function with the indicated NAME. */
4804 tree
4805 in_charge_arg_for_name (tree name)
4807 if (name == base_ctor_identifier
4808 || name == base_dtor_identifier)
4809 return integer_zero_node;
4810 else if (name == complete_ctor_identifier)
4811 return integer_one_node;
4812 else if (name == complete_dtor_identifier)
4813 return integer_two_node;
4814 else if (name == deleting_dtor_identifier)
4815 return integer_three_node;
4817 /* This function should only be called with one of the names listed
4818 above. */
4819 abort ();
4820 return NULL_TREE;
4823 /* Build a call to a constructor, destructor, or an assignment
4824 operator for INSTANCE, an expression with class type. NAME
4825 indicates the special member function to call; ARGS are the
4826 arguments. BINFO indicates the base of INSTANCE that is to be
4827 passed as the `this' parameter to the member function called.
4829 FLAGS are the LOOKUP_* flags to use when processing the call.
4831 If NAME indicates a complete object constructor, INSTANCE may be
4832 NULL_TREE. In this case, the caller will call build_cplus_new to
4833 store the newly constructed object into a VAR_DECL. */
4835 tree
4836 build_special_member_call (tree instance, tree name, tree args,
4837 tree binfo, int flags)
4839 tree fns;
4840 /* The type of the subobject to be constructed or destroyed. */
4841 tree class_type;
4843 my_friendly_assert (name == complete_ctor_identifier
4844 || name == base_ctor_identifier
4845 || name == complete_dtor_identifier
4846 || name == base_dtor_identifier
4847 || name == deleting_dtor_identifier
4848 || name == ansi_assopname (NOP_EXPR),
4849 20020712);
4850 my_friendly_assert (binfo != NULL_TREE, 20020712);
4852 class_type = BINFO_TYPE (binfo);
4854 /* Handle the special case where INSTANCE is NULL_TREE. */
4855 if (name == complete_ctor_identifier && !instance)
4857 instance = build_int_2 (0, 0);
4858 TREE_TYPE (instance) = build_pointer_type (class_type);
4859 instance = build1 (INDIRECT_REF, class_type, instance);
4861 else
4863 if (name == complete_dtor_identifier
4864 || name == base_dtor_identifier
4865 || name == deleting_dtor_identifier)
4866 my_friendly_assert (args == NULL_TREE, 20020712);
4868 /* We must perform the conversion here so that we do not
4869 subsequently check to see whether BINFO is an accessible
4870 base. (It is OK for a constructor to call a constructor in
4871 an inaccessible base as long as the constructor being called
4872 is accessible.) */
4873 if (!same_type_ignoring_top_level_qualifiers_p
4874 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
4875 instance = convert_to_base_statically (instance, binfo);
4878 my_friendly_assert (instance != NULL_TREE, 20020712);
4880 /* Resolve the name. */
4881 if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4882 return error_mark_node;
4884 fns = lookup_fnfields (binfo, name, 1);
4886 /* When making a call to a constructor or destructor for a subobject
4887 that uses virtual base classes, pass down a pointer to a VTT for
4888 the subobject. */
4889 if ((name == base_ctor_identifier
4890 || name == base_dtor_identifier)
4891 && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4893 tree vtt;
4894 tree sub_vtt;
4896 /* If the current function is a complete object constructor
4897 or destructor, then we fetch the VTT directly.
4898 Otherwise, we look it up using the VTT we were given. */
4899 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
4900 vtt = decay_conversion (vtt);
4901 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4902 build (EQ_EXPR, boolean_type_node,
4903 current_in_charge_parm, integer_zero_node),
4904 current_vtt_parm,
4905 vtt);
4906 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4907 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4908 BINFO_SUBVTT_INDEX (binfo));
4910 args = tree_cons (NULL_TREE, sub_vtt, args);
4913 return build_new_method_call (instance, fns, args,
4914 TYPE_BINFO (BINFO_TYPE (binfo)),
4915 flags);
4918 /* Return the NAME, as a C string. The NAME indicates a function that
4919 is a member of TYPE. *FREE_P is set to true if the caller must
4920 free the memory returned.
4922 Rather than go through all of this, we should simply set the names
4923 of constructors and destructors appropriately, and dispense with
4924 ctor_identifier, dtor_identifier, etc. */
4926 static char *
4927 name_as_c_string (tree name, tree type, bool *free_p)
4929 char *pretty_name;
4931 /* Assume that we will not allocate memory. */
4932 *free_p = false;
4933 /* Constructors and destructors are special. */
4934 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4936 pretty_name
4937 = (char *) IDENTIFIER_POINTER (constructor_name (type));
4938 /* For a destructor, add the '~'. */
4939 if (name == complete_dtor_identifier
4940 || name == base_dtor_identifier
4941 || name == deleting_dtor_identifier)
4943 pretty_name = concat ("~", pretty_name, NULL);
4944 /* Remember that we need to free the memory allocated. */
4945 *free_p = true;
4948 else
4949 pretty_name = (char *) IDENTIFIER_POINTER (name);
4951 return pretty_name;
4954 /* Build a call to "INSTANCE.FN (ARGS)". */
4956 tree
4957 build_new_method_call (tree instance, tree fns, tree args,
4958 tree conversion_path, int flags)
4960 struct z_candidate *candidates = 0, *cand;
4961 tree explicit_targs = NULL_TREE;
4962 tree basetype = NULL_TREE;
4963 tree access_binfo;
4964 tree optype;
4965 tree mem_args = NULL_TREE, instance_ptr;
4966 tree name;
4967 tree user_args;
4968 tree call;
4969 tree fn;
4970 tree class_type;
4971 int template_only = 0;
4972 bool any_viable_p;
4973 tree orig_instance;
4974 tree orig_fns;
4975 tree orig_args;
4977 my_friendly_assert (instance != NULL_TREE, 20020729);
4979 if (error_operand_p (instance)
4980 || error_operand_p (fns)
4981 || args == error_mark_node)
4982 return error_mark_node;
4984 orig_instance = instance;
4985 orig_fns = fns;
4986 orig_args = args;
4988 if (processing_template_decl)
4990 instance = build_non_dependent_expr (instance);
4991 if (!BASELINK_P (fns)
4992 && TREE_CODE (fns) != PSEUDO_DTOR_EXPR
4993 && TREE_TYPE (fns) != unknown_type_node)
4994 fns = build_non_dependent_expr (fns);
4995 args = build_non_dependent_args (orig_args);
4998 /* Process the argument list. */
4999 user_args = args;
5000 args = resolve_args (args);
5001 if (args == error_mark_node)
5002 return error_mark_node;
5004 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5005 instance = convert_from_reference (instance);
5006 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
5007 instance_ptr = build_this (instance);
5009 if (!BASELINK_P (fns))
5011 call = build_field_call (instance_ptr, fns, args);
5012 if (call)
5013 goto finish;
5014 error ("call to non-function `%D'", fns);
5015 return error_mark_node;
5018 if (!conversion_path)
5019 conversion_path = BASELINK_BINFO (fns);
5020 access_binfo = BASELINK_ACCESS_BINFO (fns);
5021 optype = BASELINK_OPTYPE (fns);
5022 fns = BASELINK_FUNCTIONS (fns);
5024 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
5026 explicit_targs = TREE_OPERAND (fns, 1);
5027 fns = TREE_OPERAND (fns, 0);
5028 template_only = 1;
5031 my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
5032 || TREE_CODE (fns) == TEMPLATE_DECL
5033 || TREE_CODE (fns) == OVERLOAD,
5034 20020712);
5036 /* XXX this should be handled before we get here. */
5037 if (! IS_AGGR_TYPE (basetype))
5039 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
5040 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
5041 fns, instance, basetype);
5043 return error_mark_node;
5046 fn = get_first_fn (fns);
5047 name = DECL_NAME (fn);
5049 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
5051 /* Callers should explicitly indicate whether they want to construct
5052 the complete object or just the part without virtual bases. */
5053 my_friendly_assert (name != ctor_identifier, 20000408);
5054 /* Similarly for destructors. */
5055 my_friendly_assert (name != dtor_identifier, 20000408);
5058 /* It's OK to call destructors on cv-qualified objects. Therefore,
5059 convert the INSTANCE_PTR to the unqualified type, if necessary. */
5060 if (DECL_DESTRUCTOR_P (fn))
5062 tree type = build_pointer_type (basetype);
5063 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
5064 instance_ptr = build_nop (type, instance_ptr);
5067 class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE);
5068 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
5070 for (fn = fns; fn; fn = OVL_NEXT (fn))
5072 tree t = OVL_CURRENT (fn);
5073 tree this_arglist;
5075 /* We can end up here for copy-init of same or base class. */
5076 if ((flags & LOOKUP_ONLYCONVERTING)
5077 && DECL_NONCONVERTING_P (t))
5078 continue;
5080 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
5081 this_arglist = mem_args;
5082 else
5083 this_arglist = args;
5085 if (TREE_CODE (t) == TEMPLATE_DECL)
5086 /* A member template. */
5087 add_template_candidate (&candidates, t,
5088 class_type,
5089 explicit_targs,
5090 this_arglist, optype,
5091 access_binfo,
5092 conversion_path,
5093 flags,
5094 DEDUCE_CALL);
5095 else if (! template_only)
5096 add_function_candidate (&candidates, t,
5097 class_type,
5098 this_arglist,
5099 access_binfo,
5100 conversion_path,
5101 flags);
5104 candidates = splice_viable (candidates, pedantic, &any_viable_p);
5105 if (!any_viable_p)
5107 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
5108 if (flags & LOOKUP_SPECULATIVELY)
5109 return NULL_TREE;
5110 if (!COMPLETE_TYPE_P (basetype))
5111 cxx_incomplete_type_error (instance_ptr, basetype);
5112 else
5114 char *pretty_name;
5115 bool free_p;
5117 pretty_name = name_as_c_string (name, basetype, &free_p);
5118 error ("no matching function for call to `%T::%s(%A)%#V'",
5119 basetype, pretty_name, user_args,
5120 TREE_TYPE (TREE_TYPE (instance_ptr)));
5121 if (free_p)
5122 free (pretty_name);
5124 print_z_candidates (candidates);
5125 return error_mark_node;
5128 cand = tourney (candidates);
5129 if (cand == 0)
5131 char *pretty_name;
5132 bool free_p;
5134 pretty_name = name_as_c_string (name, basetype, &free_p);
5135 error ("call of overloaded `%s(%A)' is ambiguous", pretty_name,
5136 user_args);
5137 print_z_candidates (candidates);
5138 if (free_p)
5139 free (pretty_name);
5140 return error_mark_node;
5143 if (DECL_PURE_VIRTUAL_P (cand->fn)
5144 && instance == current_class_ref
5145 && (DECL_CONSTRUCTOR_P (current_function_decl)
5146 || DECL_DESTRUCTOR_P (current_function_decl))
5147 && ! (flags & LOOKUP_NONVIRTUAL)
5148 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
5149 error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
5150 "abstract virtual `%#D' called from constructor"
5151 : "abstract virtual `%#D' called from destructor"),
5152 cand->fn);
5153 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
5154 && is_dummy_object (instance_ptr))
5156 error ("cannot call member function `%D' without object", cand->fn);
5157 return error_mark_node;
5160 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
5161 && resolves_to_fixed_type_p (instance, 0))
5162 flags |= LOOKUP_NONVIRTUAL;
5164 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
5165 call = build_over_call (cand, flags);
5166 else
5168 call = build_over_call (cand, flags);
5169 /* In an expression of the form `a->f()' where `f' turns out to
5170 be a static member function, `a' is none-the-less evaluated. */
5171 if (!is_dummy_object (instance_ptr) && TREE_SIDE_EFFECTS (instance))
5172 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
5174 finish:;
5176 if (processing_template_decl && call != error_mark_node)
5177 return build_min_non_dep
5178 (CALL_EXPR, call,
5179 build_min_nt (COMPONENT_REF, orig_instance, orig_fns),
5180 orig_args);
5181 return call;
5184 /* Returns true iff standard conversion sequence ICS1 is a proper
5185 subsequence of ICS2. */
5187 static bool
5188 is_subseq (tree ics1, tree ics2)
5190 /* We can assume that a conversion of the same code
5191 between the same types indicates a subsequence since we only get
5192 here if the types we are converting from are the same. */
5194 while (TREE_CODE (ics1) == RVALUE_CONV
5195 || TREE_CODE (ics1) == LVALUE_CONV)
5196 ics1 = TREE_OPERAND (ics1, 0);
5198 while (1)
5200 while (TREE_CODE (ics2) == RVALUE_CONV
5201 || TREE_CODE (ics2) == LVALUE_CONV)
5202 ics2 = TREE_OPERAND (ics2, 0);
5204 if (TREE_CODE (ics2) == USER_CONV
5205 || TREE_CODE (ics2) == AMBIG_CONV
5206 || TREE_CODE (ics2) == IDENTITY_CONV)
5207 /* At this point, ICS1 cannot be a proper subsequence of
5208 ICS2. We can get a USER_CONV when we are comparing the
5209 second standard conversion sequence of two user conversion
5210 sequences. */
5211 return false;
5213 ics2 = TREE_OPERAND (ics2, 0);
5215 if (TREE_CODE (ics2) == TREE_CODE (ics1)
5216 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
5217 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
5218 TREE_TYPE (TREE_OPERAND (ics1, 0))))
5219 return true;
5223 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
5224 be any _TYPE nodes. */
5226 bool
5227 is_properly_derived_from (tree derived, tree base)
5229 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
5230 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
5231 return false;
5233 /* We only allow proper derivation here. The DERIVED_FROM_P macro
5234 considers every class derived from itself. */
5235 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
5236 && DERIVED_FROM_P (base, derived));
5239 /* We build the ICS for an implicit object parameter as a pointer
5240 conversion sequence. However, such a sequence should be compared
5241 as if it were a reference conversion sequence. If ICS is the
5242 implicit conversion sequence for an implicit object parameter,
5243 modify it accordingly. */
5245 static void
5246 maybe_handle_implicit_object (tree *ics)
5248 if (ICS_THIS_FLAG (*ics))
5250 /* [over.match.funcs]
5252 For non-static member functions, the type of the
5253 implicit object parameter is "reference to cv X"
5254 where X is the class of which the function is a
5255 member and cv is the cv-qualification on the member
5256 function declaration. */
5257 tree t = *ics;
5258 tree reference_type;
5260 /* The `this' parameter is a pointer to a class type. Make the
5261 implicit conversion talk about a reference to that same class
5262 type. */
5263 reference_type = TREE_TYPE (TREE_TYPE (*ics));
5264 reference_type = build_reference_type (reference_type);
5266 if (TREE_CODE (t) == QUAL_CONV)
5267 t = TREE_OPERAND (t, 0);
5268 if (TREE_CODE (t) == PTR_CONV)
5269 t = TREE_OPERAND (t, 0);
5270 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
5271 t = direct_reference_binding (reference_type, t);
5272 *ics = t;
5276 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5277 and return the type to which the reference refers. Otherwise,
5278 leave *ICS unchanged and return NULL_TREE. */
5280 static tree
5281 maybe_handle_ref_bind (tree *ics)
5283 if (TREE_CODE (*ics) == REF_BIND)
5285 tree old_ics = *ics;
5286 tree type = TREE_TYPE (TREE_TYPE (old_ics));
5287 *ics = TREE_OPERAND (old_ics, 0);
5288 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
5289 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
5290 return type;
5293 return NULL_TREE;
5296 /* Compare two implicit conversion sequences according to the rules set out in
5297 [over.ics.rank]. Return values:
5299 1: ics1 is better than ics2
5300 -1: ics2 is better than ics1
5301 0: ics1 and ics2 are indistinguishable */
5303 static int
5304 compare_ics (tree ics1, tree ics2)
5306 tree from_type1;
5307 tree from_type2;
5308 tree to_type1;
5309 tree to_type2;
5310 tree deref_from_type1 = NULL_TREE;
5311 tree deref_from_type2 = NULL_TREE;
5312 tree deref_to_type1 = NULL_TREE;
5313 tree deref_to_type2 = NULL_TREE;
5314 int rank1, rank2;
5316 /* REF_BINDING is nonzero if the result of the conversion sequence
5317 is a reference type. In that case TARGET_TYPE is the
5318 type referred to by the reference. */
5319 tree target_type1;
5320 tree target_type2;
5322 /* Handle implicit object parameters. */
5323 maybe_handle_implicit_object (&ics1);
5324 maybe_handle_implicit_object (&ics2);
5326 /* Handle reference parameters. */
5327 target_type1 = maybe_handle_ref_bind (&ics1);
5328 target_type2 = maybe_handle_ref_bind (&ics2);
5330 /* [over.ics.rank]
5332 When comparing the basic forms of implicit conversion sequences (as
5333 defined in _over.best.ics_)
5335 --a standard conversion sequence (_over.ics.scs_) is a better
5336 conversion sequence than a user-defined conversion sequence
5337 or an ellipsis conversion sequence, and
5339 --a user-defined conversion sequence (_over.ics.user_) is a
5340 better conversion sequence than an ellipsis conversion sequence
5341 (_over.ics.ellipsis_). */
5342 rank1 = ICS_RANK (ics1);
5343 rank2 = ICS_RANK (ics2);
5345 if (rank1 > rank2)
5346 return -1;
5347 else if (rank1 < rank2)
5348 return 1;
5350 if (rank1 == BAD_RANK)
5352 /* XXX Isn't this an extension? */
5353 /* Both ICS are bad. We try to make a decision based on what
5354 would have happened if they'd been good. */
5355 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5356 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5357 return -1;
5358 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5359 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5360 return 1;
5362 /* We couldn't make up our minds; try to figure it out below. */
5365 if (ICS_ELLIPSIS_FLAG (ics1))
5366 /* Both conversions are ellipsis conversions. */
5367 return 0;
5369 /* User-defined conversion sequence U1 is a better conversion sequence
5370 than another user-defined conversion sequence U2 if they contain the
5371 same user-defined conversion operator or constructor and if the sec-
5372 ond standard conversion sequence of U1 is better than the second
5373 standard conversion sequence of U2. */
5375 if (ICS_USER_FLAG (ics1))
5377 tree t1, t2;
5379 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5380 if (TREE_CODE (t1) == AMBIG_CONV)
5381 return 0;
5382 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5383 if (TREE_CODE (t2) == AMBIG_CONV)
5384 return 0;
5386 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5387 return 0;
5389 /* We can just fall through here, after setting up
5390 FROM_TYPE1 and FROM_TYPE2. */
5391 from_type1 = TREE_TYPE (t1);
5392 from_type2 = TREE_TYPE (t2);
5394 else
5396 /* We're dealing with two standard conversion sequences.
5398 [over.ics.rank]
5400 Standard conversion sequence S1 is a better conversion
5401 sequence than standard conversion sequence S2 if
5403 --S1 is a proper subsequence of S2 (comparing the conversion
5404 sequences in the canonical form defined by _over.ics.scs_,
5405 excluding any Lvalue Transformation; the identity
5406 conversion sequence is considered to be a subsequence of
5407 any non-identity conversion sequence */
5409 from_type1 = ics1;
5410 while (TREE_CODE (from_type1) != IDENTITY_CONV)
5411 from_type1 = TREE_OPERAND (from_type1, 0);
5412 from_type1 = TREE_TYPE (from_type1);
5414 from_type2 = ics2;
5415 while (TREE_CODE (from_type2) != IDENTITY_CONV)
5416 from_type2 = TREE_OPERAND (from_type2, 0);
5417 from_type2 = TREE_TYPE (from_type2);
5420 if (same_type_p (from_type1, from_type2))
5422 if (is_subseq (ics1, ics2))
5423 return 1;
5424 if (is_subseq (ics2, ics1))
5425 return -1;
5427 /* Otherwise, one sequence cannot be a subsequence of the other; they
5428 don't start with the same type. This can happen when comparing the
5429 second standard conversion sequence in two user-defined conversion
5430 sequences. */
5432 /* [over.ics.rank]
5434 Or, if not that,
5436 --the rank of S1 is better than the rank of S2 (by the rules
5437 defined below):
5439 Standard conversion sequences are ordered by their ranks: an Exact
5440 Match is a better conversion than a Promotion, which is a better
5441 conversion than a Conversion.
5443 Two conversion sequences with the same rank are indistinguishable
5444 unless one of the following rules applies:
5446 --A conversion that is not a conversion of a pointer, or pointer
5447 to member, to bool is better than another conversion that is such
5448 a conversion.
5450 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5451 so that we do not have to check it explicitly. */
5452 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5453 return 1;
5454 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5455 return -1;
5457 to_type1 = TREE_TYPE (ics1);
5458 to_type2 = TREE_TYPE (ics2);
5460 if (TYPE_PTR_P (from_type1)
5461 && TYPE_PTR_P (from_type2)
5462 && TYPE_PTR_P (to_type1)
5463 && TYPE_PTR_P (to_type2))
5465 deref_from_type1 = TREE_TYPE (from_type1);
5466 deref_from_type2 = TREE_TYPE (from_type2);
5467 deref_to_type1 = TREE_TYPE (to_type1);
5468 deref_to_type2 = TREE_TYPE (to_type2);
5470 /* The rules for pointers to members A::* are just like the rules
5471 for pointers A*, except opposite: if B is derived from A then
5472 A::* converts to B::*, not vice versa. For that reason, we
5473 switch the from_ and to_ variables here. */
5474 else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
5475 && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
5476 || (TYPE_PTRMEMFUNC_P (from_type1)
5477 && TYPE_PTRMEMFUNC_P (from_type2)
5478 && TYPE_PTRMEMFUNC_P (to_type1)
5479 && TYPE_PTRMEMFUNC_P (to_type2)))
5481 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
5482 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
5483 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
5484 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
5487 if (deref_from_type1 != NULL_TREE
5488 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5489 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5491 /* This was one of the pointer or pointer-like conversions.
5493 [over.ics.rank]
5495 --If class B is derived directly or indirectly from class A,
5496 conversion of B* to A* is better than conversion of B* to
5497 void*, and conversion of A* to void* is better than
5498 conversion of B* to void*. */
5499 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5500 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5502 if (is_properly_derived_from (deref_from_type1,
5503 deref_from_type2))
5504 return -1;
5505 else if (is_properly_derived_from (deref_from_type2,
5506 deref_from_type1))
5507 return 1;
5509 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5510 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5512 if (same_type_p (deref_from_type1, deref_from_type2))
5514 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5516 if (is_properly_derived_from (deref_from_type1,
5517 deref_to_type1))
5518 return 1;
5520 /* We know that DEREF_TO_TYPE1 is `void' here. */
5521 else if (is_properly_derived_from (deref_from_type1,
5522 deref_to_type2))
5523 return -1;
5526 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5527 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5529 /* [over.ics.rank]
5531 --If class B is derived directly or indirectly from class A
5532 and class C is derived directly or indirectly from B,
5534 --conversion of C* to B* is better than conversion of C* to
5535 A*,
5537 --conversion of B* to A* is better than conversion of C* to
5538 A* */
5539 if (same_type_p (deref_from_type1, deref_from_type2))
5541 if (is_properly_derived_from (deref_to_type1,
5542 deref_to_type2))
5543 return 1;
5544 else if (is_properly_derived_from (deref_to_type2,
5545 deref_to_type1))
5546 return -1;
5548 else if (same_type_p (deref_to_type1, deref_to_type2))
5550 if (is_properly_derived_from (deref_from_type2,
5551 deref_from_type1))
5552 return 1;
5553 else if (is_properly_derived_from (deref_from_type1,
5554 deref_from_type2))
5555 return -1;
5559 else if (CLASS_TYPE_P (non_reference (from_type1))
5560 && same_type_p (from_type1, from_type2))
5562 tree from = non_reference (from_type1);
5564 /* [over.ics.rank]
5566 --binding of an expression of type C to a reference of type
5567 B& is better than binding an expression of type C to a
5568 reference of type A&
5570 --conversion of C to B is better than conversion of C to A, */
5571 if (is_properly_derived_from (from, to_type1)
5572 && is_properly_derived_from (from, to_type2))
5574 if (is_properly_derived_from (to_type1, to_type2))
5575 return 1;
5576 else if (is_properly_derived_from (to_type2, to_type1))
5577 return -1;
5580 else if (CLASS_TYPE_P (non_reference (to_type1))
5581 && same_type_p (to_type1, to_type2))
5583 tree to = non_reference (to_type1);
5585 /* [over.ics.rank]
5587 --binding of an expression of type B to a reference of type
5588 A& is better than binding an expression of type C to a
5589 reference of type A&,
5591 --onversion of B to A is better than conversion of C to A */
5592 if (is_properly_derived_from (from_type1, to)
5593 && is_properly_derived_from (from_type2, to))
5595 if (is_properly_derived_from (from_type2, from_type1))
5596 return 1;
5597 else if (is_properly_derived_from (from_type1, from_type2))
5598 return -1;
5602 /* [over.ics.rank]
5604 --S1 and S2 differ only in their qualification conversion and yield
5605 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5606 qualification signature of type T1 is a proper subset of the cv-
5607 qualification signature of type T2 */
5608 if (TREE_CODE (ics1) == QUAL_CONV
5609 && TREE_CODE (ics2) == QUAL_CONV
5610 && same_type_p (from_type1, from_type2))
5611 return comp_cv_qual_signature (to_type1, to_type2);
5613 /* [over.ics.rank]
5615 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5616 types to which the references refer are the same type except for
5617 top-level cv-qualifiers, and the type to which the reference
5618 initialized by S2 refers is more cv-qualified than the type to
5619 which the reference initialized by S1 refers */
5621 if (target_type1 && target_type2
5622 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5623 return comp_cv_qualification (target_type2, target_type1);
5625 /* Neither conversion sequence is better than the other. */
5626 return 0;
5629 /* The source type for this standard conversion sequence. */
5631 static tree
5632 source_type (tree t)
5634 for (;; t = TREE_OPERAND (t, 0))
5636 if (TREE_CODE (t) == USER_CONV
5637 || TREE_CODE (t) == AMBIG_CONV
5638 || TREE_CODE (t) == IDENTITY_CONV)
5639 return TREE_TYPE (t);
5641 abort ();
5644 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5645 a pointer to LOSER and re-running joust to produce the warning if WINNER
5646 is actually used. */
5648 static void
5649 add_warning (struct z_candidate *winner, struct z_candidate *loser)
5651 winner->warnings = tree_cons (NULL_TREE,
5652 build_zc_wrapper (loser),
5653 winner->warnings);
5656 /* Compare two candidates for overloading as described in
5657 [over.match.best]. Return values:
5659 1: cand1 is better than cand2
5660 -1: cand2 is better than cand1
5661 0: cand1 and cand2 are indistinguishable */
5663 static int
5664 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
5666 int winner = 0;
5667 int i, off1 = 0, off2 = 0, len;
5669 /* Candidates that involve bad conversions are always worse than those
5670 that don't. */
5671 if (cand1->viable > cand2->viable)
5672 return 1;
5673 if (cand1->viable < cand2->viable)
5674 return -1;
5676 /* If we have two pseudo-candidates for conversions to the same type,
5677 or two candidates for the same function, arbitrarily pick one. */
5678 if (cand1->fn == cand2->fn
5679 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5680 return 1;
5682 /* a viable function F1
5683 is defined to be a better function than another viable function F2 if
5684 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5685 ICSi(F2), and then */
5687 /* for some argument j, ICSj(F1) is a better conversion sequence than
5688 ICSj(F2) */
5690 /* For comparing static and non-static member functions, we ignore
5691 the implicit object parameter of the non-static function. The
5692 standard says to pretend that the static function has an object
5693 parm, but that won't work with operator overloading. */
5694 len = TREE_VEC_LENGTH (cand1->convs);
5695 if (len != TREE_VEC_LENGTH (cand2->convs))
5697 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5698 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5699 off2 = 1;
5700 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5701 && DECL_STATIC_FUNCTION_P (cand2->fn))
5703 off1 = 1;
5704 --len;
5706 else
5707 abort ();
5710 for (i = 0; i < len; ++i)
5712 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5713 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5714 int comp = compare_ics (t1, t2);
5716 if (comp != 0)
5718 if (warn_sign_promo
5719 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5720 && TREE_CODE (t1) == STD_CONV
5721 && TREE_CODE (t2) == STD_CONV
5722 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5723 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5724 && (TYPE_PRECISION (TREE_TYPE (t1))
5725 == TYPE_PRECISION (TREE_TYPE (t2)))
5726 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5727 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5728 == ENUMERAL_TYPE)))
5730 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5731 tree type1, type2;
5732 struct z_candidate *w, *l;
5733 if (comp > 0)
5734 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5735 w = cand1, l = cand2;
5736 else
5737 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5738 w = cand2, l = cand1;
5740 if (warn)
5742 warning ("passing `%T' chooses `%T' over `%T'",
5743 type, type1, type2);
5744 warning (" in call to `%D'", w->fn);
5746 else
5747 add_warning (w, l);
5750 if (winner && comp != winner)
5752 winner = 0;
5753 goto tweak;
5755 winner = comp;
5759 /* warn about confusing overload resolution for user-defined conversions,
5760 either between a constructor and a conversion op, or between two
5761 conversion ops. */
5762 if (winner && warn_conversion && cand1->second_conv
5763 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
5764 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
5766 struct z_candidate *w, *l;
5767 bool give_warning = false;
5769 if (winner == 1)
5770 w = cand1, l = cand2;
5771 else
5772 w = cand2, l = cand1;
5774 /* We don't want to complain about `X::operator T1 ()'
5775 beating `X::operator T2 () const', when T2 is a no less
5776 cv-qualified version of T1. */
5777 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
5778 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
5780 tree t = TREE_TYPE (TREE_TYPE (l->fn));
5781 tree f = TREE_TYPE (TREE_TYPE (w->fn));
5783 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
5785 t = TREE_TYPE (t);
5786 f = TREE_TYPE (f);
5788 if (!comp_ptr_ttypes (t, f))
5789 give_warning = true;
5791 else
5792 give_warning = true;
5794 if (!give_warning)
5795 /*NOP*/;
5796 else if (warn)
5798 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5799 if (! DECL_CONSTRUCTOR_P (w->fn))
5800 source = TREE_TYPE (source);
5801 warning ("choosing `%D' over `%D'", w->fn, l->fn);
5802 warning (" for conversion from `%T' to `%T'",
5803 source, TREE_TYPE (w->second_conv));
5804 warning (" because conversion sequence for the argument is better");
5806 else
5807 add_warning (w, l);
5810 if (winner)
5811 return winner;
5813 /* or, if not that,
5814 F1 is a non-template function and F2 is a template function
5815 specialization. */
5817 if (! cand1->template && cand2->template)
5818 return 1;
5819 else if (cand1->template && ! cand2->template)
5820 return -1;
5822 /* or, if not that,
5823 F1 and F2 are template functions and the function template for F1 is
5824 more specialized than the template for F2 according to the partial
5825 ordering rules. */
5827 if (cand1->template && cand2->template)
5829 winner = more_specialized
5830 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5831 DEDUCE_ORDER,
5832 /* Tell the deduction code how many real function arguments
5833 we saw, not counting the implicit 'this' argument. But,
5834 add_function_candidate() suppresses the "this" argument
5835 for constructors.
5837 [temp.func.order]: The presence of unused ellipsis and default
5838 arguments has no effect on the partial ordering of function
5839 templates. */
5840 TREE_VEC_LENGTH (cand1->convs)
5841 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5842 - DECL_CONSTRUCTOR_P (cand1->fn)));
5843 if (winner)
5844 return winner;
5847 /* or, if not that,
5848 the context is an initialization by user-defined conversion (see
5849 _dcl.init_ and _over.match.user_) and the standard conversion
5850 sequence from the return type of F1 to the destination type (i.e.,
5851 the type of the entity being initialized) is a better conversion
5852 sequence than the standard conversion sequence from the return type
5853 of F2 to the destination type. */
5855 if (cand1->second_conv)
5857 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5858 if (winner)
5859 return winner;
5862 /* Check whether we can discard a builtin candidate, either because we
5863 have two identical ones or matching builtin and non-builtin candidates.
5865 (Pedantically in the latter case the builtin which matched the user
5866 function should not be added to the overload set, but we spot it here.
5868 [over.match.oper]
5869 ... the builtin candidates include ...
5870 - do not have the same parameter type list as any non-template
5871 non-member candidate. */
5873 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5874 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5876 for (i = 0; i < len; ++i)
5877 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5878 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5879 break;
5880 if (i == TREE_VEC_LENGTH (cand1->convs))
5882 if (cand1->fn == cand2->fn)
5883 /* Two built-in candidates; arbitrarily pick one. */
5884 return 1;
5885 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5886 /* cand1 is built-in; prefer cand2. */
5887 return -1;
5888 else
5889 /* cand2 is built-in; prefer cand1. */
5890 return 1;
5894 /* If the two functions are the same (this can happen with declarations
5895 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5896 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5897 && equal_functions (cand1->fn, cand2->fn))
5898 return 1;
5900 tweak:
5902 /* Extension: If the worst conversion for one candidate is worse than the
5903 worst conversion for the other, take the first. */
5904 if (!pedantic)
5906 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5907 struct z_candidate *w = 0, *l = 0;
5909 for (i = 0; i < len; ++i)
5911 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5912 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5913 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5914 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5916 if (rank1 < rank2)
5917 winner = 1, w = cand1, l = cand2;
5918 if (rank1 > rank2)
5919 winner = -1, w = cand2, l = cand1;
5920 if (winner)
5922 if (warn)
5924 pedwarn ("\
5925 ISO C++ says that these are ambiguous, even \
5926 though the worst conversion for the first is better than \
5927 the worst conversion for the second:");
5928 print_z_candidate (_("candidate 1:"), w);
5929 print_z_candidate (_("candidate 2:"), l);
5931 else
5932 add_warning (w, l);
5933 return winner;
5937 my_friendly_assert (!winner, 20010121);
5938 return 0;
5941 /* Given a list of candidates for overloading, find the best one, if any.
5942 This algorithm has a worst case of O(2n) (winner is last), and a best
5943 case of O(n/2) (totally ambiguous); much better than a sorting
5944 algorithm. */
5946 static struct z_candidate *
5947 tourney (struct z_candidate *candidates)
5949 struct z_candidate *champ = candidates, *challenger;
5950 int fate;
5951 int champ_compared_to_predecessor = 0;
5953 /* Walk through the list once, comparing each current champ to the next
5954 candidate, knocking out a candidate or two with each comparison. */
5956 for (challenger = champ->next; challenger; )
5958 fate = joust (champ, challenger, 0);
5959 if (fate == 1)
5960 challenger = challenger->next;
5961 else
5963 if (fate == 0)
5965 champ = challenger->next;
5966 if (champ == 0)
5967 return 0;
5968 champ_compared_to_predecessor = 0;
5970 else
5972 champ = challenger;
5973 champ_compared_to_predecessor = 1;
5976 challenger = champ->next;
5980 /* Make sure the champ is better than all the candidates it hasn't yet
5981 been compared to. */
5983 for (challenger = candidates;
5984 challenger != champ
5985 && !(champ_compared_to_predecessor && challenger->next == champ);
5986 challenger = challenger->next)
5988 fate = joust (champ, challenger, 0);
5989 if (fate != 1)
5990 return 0;
5993 return champ;
5996 /* Returns nonzero if things of type FROM can be converted to TO. */
5998 bool
5999 can_convert (tree to, tree from)
6001 return can_convert_arg (to, from, NULL_TREE);
6004 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
6006 bool
6007 can_convert_arg (tree to, tree from, tree arg)
6009 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
6010 return (t && ! ICS_BAD_FLAG (t));
6013 /* Like can_convert_arg, but allows dubious conversions as well. */
6015 bool
6016 can_convert_arg_bad (tree to, tree from, tree arg)
6018 return implicit_conversion (to, from, arg, LOOKUP_NORMAL) != 0;
6021 /* Convert EXPR to TYPE. Return the converted expression.
6023 Note that we allow bad conversions here because by the time we get to
6024 this point we are committed to doing the conversion. If we end up
6025 doing a bad conversion, convert_like will complain. */
6027 tree
6028 perform_implicit_conversion (tree type, tree expr)
6030 tree conv;
6032 if (error_operand_p (expr))
6033 return error_mark_node;
6034 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6035 LOOKUP_NORMAL);
6036 if (!conv)
6038 error ("could not convert `%E' to `%T'", expr, type);
6039 return error_mark_node;
6042 return convert_like (conv, expr);
6045 /* Convert EXPR to TYPE (as a direct-initialization) if that is
6046 permitted. If the conversion is valid, the converted expression is
6047 returned. Otherwise, NULL_TREE is returned, except in the case
6048 that TYPE is a class type; in that case, an error is issued. */
6050 tree
6051 perform_direct_initialization_if_possible (tree type, tree expr)
6053 tree conv;
6055 if (type == error_mark_node || error_operand_p (expr))
6056 return error_mark_node;
6057 /* [dcl.init]
6059 If the destination type is a (possibly cv-qualified) class type:
6061 -- If the initialization is direct-initialization ...,
6062 constructors are considered. ... If no constructor applies, or
6063 the overload resolution is ambiguous, the initialization is
6064 ill-formed. */
6065 if (CLASS_TYPE_P (type))
6067 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6068 build_tree_list (NULL_TREE, expr),
6069 TYPE_BINFO (type),
6070 LOOKUP_NORMAL);
6071 return build_cplus_new (type, expr);
6073 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6074 LOOKUP_NORMAL);
6075 if (!conv || ICS_BAD_FLAG (conv))
6076 return NULL_TREE;
6077 return convert_like_real (conv, expr, NULL_TREE, 0, 0,
6078 /*issue_conversion_warnings=*/false);
6081 /* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference
6082 is being bound to a temporary. Create and return a new VAR_DECL
6083 with the indicated TYPE; this variable will store the value to
6084 which the reference is bound. */
6086 tree
6087 make_temporary_var_for_ref_to_temp (tree decl, tree type)
6089 tree var;
6091 /* Create the variable. */
6092 var = build_decl (VAR_DECL, NULL_TREE, type);
6093 DECL_ARTIFICIAL (var) = 1;
6094 TREE_USED (var) = 1;
6096 /* Register the variable. */
6097 if (TREE_STATIC (decl))
6099 /* Namespace-scope or local static; give it a mangled name. */
6100 tree name;
6102 TREE_STATIC (var) = 1;
6103 name = mangle_ref_init_variable (decl);
6104 DECL_NAME (var) = name;
6105 SET_DECL_ASSEMBLER_NAME (var, name);
6106 var = pushdecl_top_level (var);
6108 else
6110 /* Create a new cleanup level if necessary. */
6111 maybe_push_cleanup_level (type);
6112 /* Don't push unnamed temps. Do set DECL_CONTEXT, though. */
6113 DECL_CONTEXT (var) = current_function_decl;
6116 return var;
6119 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
6120 initializing a variable of that TYPE. If DECL is non-NULL, it is
6121 the VAR_DECL being initialized with the EXPR. (In that case, the
6122 type of DECL will be TYPE.) If DECL is non-NULL, then CLEANUP must
6123 also be non-NULL, and with *CLEANUP initialized to NULL. Upon
6124 return, if *CLEANUP is no longer NULL, it will be a CLEANUP_STMT
6125 that should be inserted after the returned expression is used to
6126 initialize DECL.
6128 Return the converted expression. */
6130 tree
6131 initialize_reference (tree type, tree expr, tree decl, tree *cleanup)
6133 tree conv;
6135 if (type == error_mark_node || error_operand_p (expr))
6136 return error_mark_node;
6138 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
6139 if (!conv || ICS_BAD_FLAG (conv))
6141 if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
6142 && !real_lvalue_p (expr))
6143 error ("invalid initialization of non-const reference of "
6144 "type '%T' from a temporary of type '%T'",
6145 type, TREE_TYPE (expr));
6146 else
6147 error ("invalid initialization of reference of type "
6148 "'%T' from expression of type '%T'", type,
6149 TREE_TYPE (expr));
6150 return error_mark_node;
6153 /* If DECL is non-NULL, then this special rule applies:
6155 [class.temporary]
6157 The temporary to which the reference is bound or the temporary
6158 that is the complete object to which the reference is bound
6159 persists for the lifetime of the reference.
6161 The temporaries created during the evaluation of the expression
6162 initializing the reference, except the temporary to which the
6163 reference is bound, are destroyed at the end of the
6164 full-expression in which they are created.
6166 In that case, we store the converted expression into a new
6167 VAR_DECL in a new scope.
6169 However, we want to be careful not to create temporaries when
6170 they are not required. For example, given:
6172 struct B {};
6173 struct D : public B {};
6174 D f();
6175 const B& b = f();
6177 there is no need to copy the return value from "f"; we can just
6178 extend its lifetime. Similarly, given:
6180 struct S {};
6181 struct T { operator S(); };
6182 T t;
6183 const S& s = t;
6185 we can extend the lifetime of the return value of the conversion
6186 operator. */
6187 my_friendly_assert (TREE_CODE (conv) == REF_BIND, 20030302);
6188 if (decl)
6190 tree var;
6191 tree base_conv_type;
6193 /* Skip over the REF_BIND. */
6194 conv = TREE_OPERAND (conv, 0);
6195 /* If the next conversion is a BASE_CONV, skip that too -- but
6196 remember that the conversion was required. */
6197 if (TREE_CODE (conv) == BASE_CONV && !NEED_TEMPORARY_P (conv))
6199 base_conv_type = TREE_TYPE (conv);
6200 conv = TREE_OPERAND (conv, 0);
6202 else
6203 base_conv_type = NULL_TREE;
6204 /* Perform the remainder of the conversion. */
6205 expr = convert_like (conv, expr);
6206 if (!real_lvalue_p (expr))
6208 tree init;
6209 tree type;
6211 /* Create the temporary variable. */
6212 type = TREE_TYPE (expr);
6213 var = make_temporary_var_for_ref_to_temp (decl, type);
6214 layout_decl (var, 0);
6215 /* Create the INIT_EXPR that will initialize the temporary
6216 variable. */
6217 init = build (INIT_EXPR, type, var, expr);
6218 if (at_function_scope_p ())
6220 add_decl_stmt (var);
6221 *cleanup = cxx_maybe_build_cleanup (var);
6222 if (*cleanup)
6223 /* We must be careful to destroy the temporary only
6224 after its initialization has taken place. If the
6225 initialization throws an exception, then the
6226 destructor should not be run. We cannot simply
6227 transform INIT into something like:
6229 (INIT, ({ CLEANUP_STMT; }))
6231 because emit_local_var always treats the
6232 initializer as a full-expression. Thus, the
6233 destructor would run too early; it would run at the
6234 end of initializing the reference variable, rather
6235 than at the end of the block enclosing the
6236 reference variable.
6238 The solution is to pass back a CLEANUP_STMT which
6239 the caller is responsible for attaching to the
6240 statement tree. */
6241 *cleanup = build_stmt (CLEANUP_STMT, var, *cleanup);
6243 else
6245 rest_of_decl_compilation (var, NULL, /*toplev=*/1, at_eof);
6246 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6247 static_aggregates = tree_cons (NULL_TREE, var,
6248 static_aggregates);
6250 /* Use its address to initialize the reference variable. */
6251 expr = build_address (var);
6252 expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
6254 else
6255 /* Take the address of EXPR. */
6256 expr = build_unary_op (ADDR_EXPR, expr, 0);
6257 /* If a BASE_CONV was required, perform it now. */
6258 if (base_conv_type)
6259 expr = (perform_implicit_conversion
6260 (build_pointer_type (base_conv_type), expr));
6261 return build_nop (type, expr);
6264 /* Perform the conversion. */
6265 return convert_like (conv, expr);
6268 #include "gt-cp-call.h"