* options.c (gfc_handle_module_path_options): Fix buffer overrun.
[official-gcc.git] / gcc / cp / call.c
blobc4892e949e45cf02e5dd9fd24799018ce88a6daa
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, 2004 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 /* The various kinds of conversion. */
45 typedef enum conversion_kind {
46 ck_identity,
47 ck_lvalue,
48 ck_qual,
49 ck_std,
50 ck_ptr,
51 ck_pmem,
52 ck_base,
53 ck_ref_bind,
54 ck_user,
55 ck_ambig,
56 ck_rvalue
57 } conversion_kind;
59 /* The rank of the conversion. Order of the enumerals matters; better
60 conversions should come earlier in the list. */
62 typedef enum conversion_rank {
63 cr_identity,
64 cr_exact,
65 cr_promotion,
66 cr_std,
67 cr_pbool,
68 cr_user,
69 cr_ellipsis,
70 cr_bad
71 } conversion_rank;
73 /* An implicit conversion sequence, in the sense of [over.best.ics].
74 The first conversion to be performed is at the end of the chain.
75 That conversion is always an cr_identity conversion. */
77 typedef struct conversion conversion;
78 struct conversion {
79 /* The kind of conversion represented by this step. */
80 conversion_kind kind;
81 /* The rank of this conversion. */
82 conversion_rank rank;
83 BOOL_BITFIELD user_conv_p : 1;
84 BOOL_BITFIELD ellipsis_p : 1;
85 BOOL_BITFIELD this_p : 1;
86 BOOL_BITFIELD bad_p : 1;
87 /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
88 temporary should be created to hold the result of the
89 conversion. */
90 BOOL_BITFIELD need_temporary_p : 1;
91 /* If KIND is ck_identity or ck_base_conv, true to indicate that the
92 copy constructor must be accessible, even though it is not being
93 used. */
94 BOOL_BITFIELD check_copy_constructor_p : 1;
95 /* The type of the expression resulting from the conversion. */
96 tree type;
97 union {
98 /* The next conversion in the chain. Since the conversions are
99 arranged from outermost to innermost, the NEXT conversion will
100 actually be performed before this conversion. This variant is
101 used only when KIND is neither ck_identity nor ck_ambig. */
102 conversion *next;
103 /* The expression at the beginning of the conversion chain. This
104 variant is used only if KIND is ck_identity or ck_ambig. */
105 tree expr;
106 } u;
107 /* The function candidate corresponding to this conversion
108 sequence. This field is only used if KIND is ck_user. */
109 struct z_candidate *cand;
112 #define CONVERSION_RANK(NODE) \
113 ((NODE)->bad_p ? cr_bad \
114 : (NODE)->ellipsis_p ? cr_ellipsis \
115 : (NODE)->user_conv_p ? cr_user \
116 : (NODE)->rank)
118 static struct obstack conversion_obstack;
119 static bool conversion_obstack_initialized;
121 static struct z_candidate * tourney (struct z_candidate *);
122 static int equal_functions (tree, tree);
123 static int joust (struct z_candidate *, struct z_candidate *, bool);
124 static int compare_ics (conversion *, conversion *);
125 static tree build_over_call (struct z_candidate *, int);
126 static tree build_java_interface_fn_ref (tree, tree);
127 #define convert_like(CONV, EXPR) \
128 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \
129 /*issue_conversion_warnings=*/true)
130 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
131 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
132 /*issue_conversion_warnings=*/true)
133 static tree convert_like_real (conversion *, tree, tree, int, int, bool);
134 static void op_error (enum tree_code, enum tree_code, tree, tree,
135 tree, const char *);
136 static tree build_object_call (tree, tree);
137 static tree resolve_args (tree);
138 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
139 static void print_z_candidate (const char *, struct z_candidate *);
140 static void print_z_candidates (struct z_candidate *);
141 static tree build_this (tree);
142 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
143 static bool any_strictly_viable (struct z_candidate *);
144 static struct z_candidate *add_template_candidate
145 (struct z_candidate **, tree, tree, tree, tree, tree,
146 tree, tree, int, unification_kind_t);
147 static struct z_candidate *add_template_candidate_real
148 (struct z_candidate **, tree, tree, tree, tree, tree,
149 tree, tree, int, tree, unification_kind_t);
150 static struct z_candidate *add_template_conv_candidate
151 (struct z_candidate **, tree, tree, tree, tree, tree, tree);
152 static void add_builtin_candidates
153 (struct z_candidate **, enum tree_code, enum tree_code,
154 tree, tree *, int);
155 static void add_builtin_candidate
156 (struct z_candidate **, enum tree_code, enum tree_code,
157 tree, tree, tree, tree *, tree *, int);
158 static bool is_complete (tree);
159 static void build_builtin_candidate
160 (struct z_candidate **, tree, tree, tree, tree *, tree *,
161 int);
162 static struct z_candidate *add_conv_candidate
163 (struct z_candidate **, tree, tree, tree, tree, tree);
164 static struct z_candidate *add_function_candidate
165 (struct z_candidate **, tree, tree, tree, tree, tree, int);
166 static conversion *implicit_conversion (tree, tree, tree, int);
167 static conversion *standard_conversion (tree, tree, tree);
168 static conversion *reference_binding (tree, tree, tree, int);
169 static conversion *build_conv (conversion_kind, tree, conversion *);
170 static bool is_subseq (conversion *, conversion *);
171 static tree maybe_handle_ref_bind (conversion **);
172 static void maybe_handle_implicit_object (conversion **);
173 static struct z_candidate *add_candidate
174 (struct z_candidate **, tree, tree, size_t,
175 conversion **, tree, tree, int);
176 static tree source_type (conversion *);
177 static void add_warning (struct z_candidate *, struct z_candidate *);
178 static bool reference_related_p (tree, tree);
179 static bool reference_compatible_p (tree, tree);
180 static conversion *convert_class_to_reference (tree, tree, tree);
181 static conversion *direct_reference_binding (tree, conversion *);
182 static bool promoted_arithmetic_type_p (tree);
183 static conversion *conditional_conversion (tree, tree);
184 static char *name_as_c_string (tree, tree, bool *);
185 static tree call_builtin_trap (tree);
186 static tree prep_operand (tree);
187 static void add_candidates (tree, tree, tree, bool, tree, tree,
188 int, struct z_candidate **);
189 static conversion *merge_conversion_sequences (conversion *, conversion *);
190 static bool magic_varargs_p (tree);
192 tree
193 build_vfield_ref (tree datum, tree type)
195 if (datum == error_mark_node)
196 return error_mark_node;
198 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
199 datum = convert_from_reference (datum);
201 if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
202 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
203 datum = convert_to_base (datum, type, /*check_access=*/false);
205 return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
206 datum, TYPE_VFIELD (type));
209 /* Returns nonzero iff the destructor name specified in NAME
210 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
211 forms... */
213 bool
214 check_dtor_name (tree basetype, tree name)
216 name = TREE_OPERAND (name, 0);
218 /* Just accept something we've already complained about. */
219 if (name == error_mark_node)
220 return true;
222 if (TREE_CODE (name) == TYPE_DECL)
223 name = TREE_TYPE (name);
224 else if (TYPE_P (name))
225 /* OK */;
226 else if (TREE_CODE (name) == IDENTIFIER_NODE)
228 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
229 || (TREE_CODE (basetype) == ENUMERAL_TYPE
230 && name == TYPE_IDENTIFIER (basetype)))
231 name = basetype;
232 else
233 name = get_type_value (name);
235 /* In the case of:
237 template <class T> struct S { ~S(); };
238 int i;
239 i.~S();
241 NAME will be a class template. */
242 else if (DECL_CLASS_TEMPLATE_P (name))
243 return false;
244 else
245 abort ();
247 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
248 return true;
249 return false;
252 /* We want the address of a function or method. We avoid creating a
253 pointer-to-member function. */
255 tree
256 build_addr_func (tree function)
258 tree type = TREE_TYPE (function);
260 /* We have to do these by hand to avoid real pointer to member
261 functions. */
262 if (TREE_CODE (type) == METHOD_TYPE)
264 if (TREE_CODE (function) == OFFSET_REF)
266 tree object = build_address (TREE_OPERAND (function, 0));
267 return get_member_function_from_ptrfunc (&object,
268 TREE_OPERAND (function, 1));
270 function = build_address (function);
272 else
273 function = decay_conversion (function);
275 return function;
278 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
279 POINTER_TYPE to those. Note, pointer to member function types
280 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
282 tree
283 build_call (tree function, tree parms)
285 int is_constructor = 0;
286 int nothrow;
287 tree tmp;
288 tree decl;
289 tree result_type;
290 tree fntype;
292 function = build_addr_func (function);
294 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
296 sorry ("unable to call pointer to member function here");
297 return error_mark_node;
300 fntype = TREE_TYPE (TREE_TYPE (function));
301 result_type = TREE_TYPE (fntype);
303 if (TREE_CODE (function) == ADDR_EXPR
304 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
305 decl = TREE_OPERAND (function, 0);
306 else
307 decl = NULL_TREE;
309 /* We check both the decl and the type; a function may be known not to
310 throw without being declared throw(). */
311 nothrow = ((decl && TREE_NOTHROW (decl))
312 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
314 if (decl && TREE_THIS_VOLATILE (decl) && cfun)
315 current_function_returns_abnormally = 1;
317 if (decl && TREE_DEPRECATED (decl))
318 warn_deprecated_use (decl);
319 require_complete_eh_spec_types (fntype, decl);
321 if (decl && DECL_CONSTRUCTOR_P (decl))
322 is_constructor = 1;
324 if (decl && ! TREE_USED (decl))
326 /* We invoke build_call directly for several library functions.
327 These may have been declared normally if we're building libgcc,
328 so we can't just check DECL_ARTIFICIAL. */
329 if (DECL_ARTIFICIAL (decl)
330 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
331 mark_used (decl);
332 else
333 abort ();
336 /* Don't pass empty class objects by value. This is useful
337 for tags in STL, which are used to control overload resolution.
338 We don't need to handle other cases of copying empty classes. */
339 if (! decl || ! DECL_BUILT_IN (decl))
340 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
341 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
342 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
344 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
345 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
346 TREE_VALUE (tmp), t);
349 function = build (CALL_EXPR, result_type, function, parms, NULL_TREE);
350 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
351 TREE_NOTHROW (function) = nothrow;
353 return function;
356 /* Build something of the form ptr->method (args)
357 or object.method (args). This can also build
358 calls to constructors, and find friends.
360 Member functions always take their class variable
361 as a pointer.
363 INSTANCE is a class instance.
365 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
367 PARMS help to figure out what that NAME really refers to.
369 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
370 down to the real instance type to use for access checking. We need this
371 information to get protected accesses correct.
373 FLAGS is the logical disjunction of zero or more LOOKUP_
374 flags. See cp-tree.h for more info.
376 If this is all OK, calls build_function_call with the resolved
377 member function.
379 This function must also handle being called to perform
380 initialization, promotion/coercion of arguments, and
381 instantiation of default parameters.
383 Note that NAME may refer to an instance variable name. If
384 `operator()()' is defined for the type of that field, then we return
385 that result. */
387 /* New overloading code. */
389 typedef struct z_candidate z_candidate;
391 typedef struct candidate_warning candidate_warning;
392 struct candidate_warning {
393 z_candidate *loser;
394 candidate_warning *next;
397 struct z_candidate {
398 /* The FUNCTION_DECL that will be called if this candidate is
399 selected by overload resolution. */
400 tree fn;
401 /* The arguments to use when calling this function. */
402 tree args;
403 /* The implicit conversion sequences for each of the arguments to
404 FN. */
405 conversion **convs;
406 /* The number of implicit conversion sequences. */
407 size_t num_convs;
408 /* If FN is a user-defined conversion, the standard conversion
409 sequence from the type returned by FN to the desired destination
410 type. */
411 conversion *second_conv;
412 int viable;
413 /* If FN is a member function, the binfo indicating the path used to
414 qualify the name of FN at the call site. This path is used to
415 determine whether or not FN is accessible if it is selected by
416 overload resolution. The DECL_CONTEXT of FN will always be a
417 (possibly improper) base of this binfo. */
418 tree access_path;
419 /* If FN is a non-static member function, the binfo indicating the
420 subobject to which the `this' pointer should be converted if FN
421 is selected by overload resolution. The type pointed to the by
422 the `this' pointer must correspond to the most derived class
423 indicated by the CONVERSION_PATH. */
424 tree conversion_path;
425 tree template;
426 candidate_warning *warnings;
427 z_candidate *next;
430 bool
431 null_ptr_cst_p (tree t)
433 /* [conv.ptr]
435 A null pointer constant is an integral constant expression
436 (_expr.const_) rvalue of integer type that evaluates to zero. */
437 if (t == null_node
438 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
439 return true;
440 return false;
444 /* Returns nonzero if PARMLIST consists of only default parms and/or
445 ellipsis. */
447 bool
448 sufficient_parms_p (tree parmlist)
450 for (; parmlist && parmlist != void_list_node;
451 parmlist = TREE_CHAIN (parmlist))
452 if (!TREE_PURPOSE (parmlist))
453 return false;
454 return true;
457 /* Allocate N bytes of memory from the conversion obstack. The memory
458 is zeroed before being returned. */
460 static void *
461 conversion_obstack_alloc (size_t n)
463 void *p;
464 if (!conversion_obstack_initialized)
466 gcc_obstack_init (&conversion_obstack);
467 conversion_obstack_initialized = true;
469 p = obstack_alloc (&conversion_obstack, n);
470 memset (p, 0, n);
471 return p;
474 /* Dynamically allocate a conversion. */
476 static conversion *
477 alloc_conversion (conversion_kind kind)
479 conversion *c;
480 c = conversion_obstack_alloc (sizeof (conversion));
481 c->kind = kind;
482 return c;
485 #ifdef ENABLE_CHECKING
487 /* Make sure that all memory on the conversion obstack has been
488 freed. */
490 void
491 validate_conversion_obstack (void)
493 if (conversion_obstack_initialized)
494 my_friendly_assert ((obstack_next_free (&conversion_obstack)
495 == obstack_base (&conversion_obstack)),
496 20040208);
499 #endif /* ENABLE_CHECKING */
501 /* Dynamically allocate an array of N conversions. */
503 static conversion **
504 alloc_conversions (size_t n)
506 return conversion_obstack_alloc (n * sizeof (conversion *));
509 static conversion *
510 build_conv (conversion_kind code, tree type, conversion *from)
512 conversion *t;
513 conversion_rank rank = CONVERSION_RANK (from);
515 /* We can't use buildl1 here because CODE could be USER_CONV, which
516 takes two arguments. In that case, the caller is responsible for
517 filling in the second argument. */
518 t = alloc_conversion (code);
519 t->type = type;
520 t->u.next = from;
522 switch (code)
524 case ck_ptr:
525 case ck_pmem:
526 case ck_base:
527 case ck_std:
528 if (rank < cr_std)
529 rank = cr_std;
530 break;
532 case ck_qual:
533 if (rank < cr_exact)
534 rank = cr_exact;
535 break;
537 default:
538 break;
540 t->rank = rank;
541 t->user_conv_p = (code == ck_user || from->user_conv_p);
542 t->bad_p = from->bad_p;
543 return t;
546 /* Build a representation of the identity conversion from EXPR to
547 itself. The TYPE should match the the type of EXPR, if EXPR is
548 non-NULL. */
550 static conversion *
551 build_identity_conv (tree type, tree expr)
553 conversion *c;
555 c = alloc_conversion (ck_identity);
556 c->type = type;
557 c->u.expr = expr;
559 return c;
562 /* Converting from EXPR to TYPE was ambiguous in the sense that there
563 were multiple user-defined conversions to accomplish the job.
564 Build a conversion that indicates that ambiguity. */
566 static conversion *
567 build_ambiguous_conv (tree type, tree expr)
569 conversion *c;
571 c = alloc_conversion (ck_ambig);
572 c->type = type;
573 c->u.expr = expr;
575 return c;
578 tree
579 strip_top_quals (tree t)
581 if (TREE_CODE (t) == ARRAY_TYPE)
582 return t;
583 return cp_build_qualified_type (t, 0);
586 /* Returns the standard conversion path (see [conv]) from type FROM to type
587 TO, if any. For proper handling of null pointer constants, you must
588 also pass the expression EXPR to convert from. */
590 static conversion *
591 standard_conversion (tree to, tree from, tree expr)
593 enum tree_code fcode, tcode;
594 conversion *conv;
595 bool fromref = false;
597 to = non_reference (to);
598 if (TREE_CODE (from) == REFERENCE_TYPE)
600 fromref = true;
601 from = TREE_TYPE (from);
603 to = strip_top_quals (to);
604 from = strip_top_quals (from);
606 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
607 && expr && type_unknown_p (expr))
609 expr = instantiate_type (to, expr, tf_conv);
610 if (expr == error_mark_node)
611 return NULL;
612 from = TREE_TYPE (expr);
615 fcode = TREE_CODE (from);
616 tcode = TREE_CODE (to);
618 conv = build_identity_conv (from, expr);
619 if (fcode == FUNCTION_TYPE)
621 from = build_pointer_type (from);
622 fcode = TREE_CODE (from);
623 conv = build_conv (ck_lvalue, from, conv);
625 else if (fcode == ARRAY_TYPE)
627 from = build_pointer_type (TREE_TYPE (from));
628 fcode = TREE_CODE (from);
629 conv = build_conv (ck_lvalue, from, conv);
631 else if (fromref || (expr && lvalue_p (expr)))
632 conv = build_conv (ck_rvalue, from, conv);
634 /* Allow conversion between `__complex__' data types. */
635 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
637 /* The standard conversion sequence to convert FROM to TO is
638 the standard conversion sequence to perform componentwise
639 conversion. */
640 conversion *part_conv = standard_conversion
641 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
643 if (part_conv)
645 conv = build_conv (part_conv->kind, to, conv);
646 conv->rank = part_conv->rank;
648 else
649 conv = NULL;
651 return conv;
654 if (same_type_p (from, to))
655 return conv;
657 if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to))
658 && expr && null_ptr_cst_p (expr))
659 conv = build_conv (ck_std, to, conv);
660 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE
661 && TREE_CODE (TREE_TYPE (to)) == VECTOR_TYPE
662 && TREE_CODE (TREE_TYPE (from)) == VECTOR_TYPE
663 && vector_types_convertible_p (TREE_TYPE (to), TREE_TYPE (from)))
664 conv = build_conv (ck_std, to, conv);
665 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
666 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
668 /* For backwards brain damage compatibility, allow interconversion of
669 pointers and integers with a pedwarn. */
670 conv = build_conv (ck_std, to, conv);
671 conv->bad_p = true;
673 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE)
675 /* For backwards brain damage compatibility, allow interconversion of
676 enums and integers with a pedwarn. */
677 conv = build_conv (ck_std, to, conv);
678 conv->bad_p = true;
680 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
681 || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
683 tree to_pointee;
684 tree from_pointee;
686 if (tcode == POINTER_TYPE
687 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
688 TREE_TYPE (to)))
690 else if (VOID_TYPE_P (TREE_TYPE (to))
691 && !TYPE_PTRMEM_P (from)
692 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
694 from = build_pointer_type
695 (cp_build_qualified_type (void_type_node,
696 cp_type_quals (TREE_TYPE (from))));
697 conv = build_conv (ck_ptr, from, conv);
699 else if (TYPE_PTRMEM_P (from))
701 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
702 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
704 if (DERIVED_FROM_P (fbase, tbase)
705 && (same_type_ignoring_top_level_qualifiers_p
706 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
707 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
709 from = build_ptrmem_type (tbase,
710 TYPE_PTRMEM_POINTED_TO_TYPE (from));
711 conv = build_conv (ck_pmem, from, conv);
714 else if (IS_AGGR_TYPE (TREE_TYPE (from))
715 && IS_AGGR_TYPE (TREE_TYPE (to))
716 /* [conv.ptr]
718 An rvalue of type "pointer to cv D," where D is a
719 class type, can be converted to an rvalue of type
720 "pointer to cv B," where B is a base class (clause
721 _class.derived_) of D. If B is an inaccessible
722 (clause _class.access_) or ambiguous
723 (_class.member.lookup_) base class of D, a program
724 that necessitates this conversion is ill-formed. */
725 /* Therefore, we use DERIVED_FROM_P, and not
726 ACCESSIBLY_UNIQUELY_DERIVED_FROM_P, in this test. */
727 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
729 from =
730 cp_build_qualified_type (TREE_TYPE (to),
731 cp_type_quals (TREE_TYPE (from)));
732 from = build_pointer_type (from);
733 conv = build_conv (ck_ptr, from, conv);
736 if (tcode == POINTER_TYPE)
738 to_pointee = TREE_TYPE (to);
739 from_pointee = TREE_TYPE (from);
741 else
743 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
744 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
747 if (same_type_p (from, to))
748 /* OK */;
749 else if (comp_ptr_ttypes (to_pointee, from_pointee))
750 conv = build_conv (ck_qual, to, conv);
751 else if (expr && string_conv_p (to, expr, 0))
752 /* converting from string constant to char *. */
753 conv = build_conv (ck_qual, to, conv);
754 else if (ptr_reasonably_similar (to_pointee, from_pointee))
756 conv = build_conv (ck_ptr, to, conv);
757 conv->bad_p = true;
759 else
760 return NULL;
762 from = to;
764 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
766 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
767 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
768 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
769 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
771 if (!DERIVED_FROM_P (fbase, tbase)
772 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
773 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
774 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
775 || cp_type_quals (fbase) != cp_type_quals (tbase))
776 return 0;
778 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
779 from = build_method_type_directly (from,
780 TREE_TYPE (fromfn),
781 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
782 from = build_ptrmemfunc_type (build_pointer_type (from));
783 conv = build_conv (ck_pmem, from, conv);
785 else if (tcode == BOOLEAN_TYPE)
787 /* [conv.bool]
789 An rvalue of arithmetic, enumeration, pointer, or pointer to
790 member type can be converted to an rvalue of type bool. */
791 if (ARITHMETIC_TYPE_P (from)
792 || fcode == ENUMERAL_TYPE
793 || fcode == POINTER_TYPE
794 || TYPE_PTR_TO_MEMBER_P (from))
796 conv = build_conv (ck_std, to, conv);
797 if (fcode == POINTER_TYPE
798 || TYPE_PTRMEM_P (from)
799 || (TYPE_PTRMEMFUNC_P (from)
800 && conv->rank < cr_pbool))
801 conv->rank = cr_pbool;
802 return conv;
805 return NULL;
807 /* We don't check for ENUMERAL_TYPE here because there are no standard
808 conversions to enum type. */
809 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
810 || tcode == REAL_TYPE)
812 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
813 return 0;
814 conv = build_conv (ck_std, to, conv);
816 /* Give this a better rank if it's a promotion. */
817 if (same_type_p (to, type_promotes_to (from))
818 && conv->u.next->rank <= cr_promotion)
819 conv->rank = cr_promotion;
821 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
822 && vector_types_convertible_p (from, to))
823 return build_conv (ck_std, to, conv);
824 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
825 && is_properly_derived_from (from, to))
827 if (conv->kind == ck_rvalue)
828 conv = conv->u.next;
829 conv = build_conv (ck_base, to, conv);
830 /* The derived-to-base conversion indicates the initialization
831 of a parameter with base type from an object of a derived
832 type. A temporary object is created to hold the result of
833 the conversion. */
834 conv->need_temporary_p = true;
836 else
837 return NULL;
839 return conv;
842 /* Returns nonzero if T1 is reference-related to T2. */
844 static bool
845 reference_related_p (tree t1, tree t2)
847 t1 = TYPE_MAIN_VARIANT (t1);
848 t2 = TYPE_MAIN_VARIANT (t2);
850 /* [dcl.init.ref]
852 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
853 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
854 of T2. */
855 return (same_type_p (t1, t2)
856 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
857 && DERIVED_FROM_P (t1, t2)));
860 /* Returns nonzero if T1 is reference-compatible with T2. */
862 static bool
863 reference_compatible_p (tree t1, tree t2)
865 /* [dcl.init.ref]
867 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
868 reference-related to T2 and cv1 is the same cv-qualification as,
869 or greater cv-qualification than, cv2. */
870 return (reference_related_p (t1, t2)
871 && at_least_as_qualified_p (t1, t2));
874 /* Determine whether or not the EXPR (of class type S) can be
875 converted to T as in [over.match.ref]. */
877 static conversion *
878 convert_class_to_reference (tree t, tree s, tree expr)
880 tree conversions;
881 tree arglist;
882 conversion *conv;
883 tree reference_type;
884 struct z_candidate *candidates;
885 struct z_candidate *cand;
886 bool any_viable_p;
888 conversions = lookup_conversions (s);
889 if (!conversions)
890 return NULL;
892 /* [over.match.ref]
894 Assuming that "cv1 T" is the underlying type of the reference
895 being initialized, and "cv S" is the type of the initializer
896 expression, with S a class type, the candidate functions are
897 selected as follows:
899 --The conversion functions of S and its base classes are
900 considered. Those that are not hidden within S and yield type
901 "reference to cv2 T2", where "cv1 T" is reference-compatible
902 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
904 The argument list has one argument, which is the initializer
905 expression. */
907 candidates = 0;
909 /* Conceptually, we should take the address of EXPR and put it in
910 the argument list. Unfortunately, however, that can result in
911 error messages, which we should not issue now because we are just
912 trying to find a conversion operator. Therefore, we use NULL,
913 cast to the appropriate type. */
914 arglist = build_int_2 (0, 0);
915 TREE_TYPE (arglist) = build_pointer_type (s);
916 arglist = build_tree_list (NULL_TREE, arglist);
918 reference_type = build_reference_type (t);
920 while (conversions)
922 tree fns = TREE_VALUE (conversions);
924 for (; fns; fns = OVL_NEXT (fns))
926 tree f = OVL_CURRENT (fns);
927 tree t2 = TREE_TYPE (TREE_TYPE (f));
929 cand = NULL;
931 /* If this is a template function, try to get an exact
932 match. */
933 if (TREE_CODE (f) == TEMPLATE_DECL)
935 cand = add_template_candidate (&candidates,
936 f, s,
937 NULL_TREE,
938 arglist,
939 reference_type,
940 TYPE_BINFO (s),
941 TREE_PURPOSE (conversions),
942 LOOKUP_NORMAL,
943 DEDUCE_CONV);
945 if (cand)
947 /* Now, see if the conversion function really returns
948 an lvalue of the appropriate type. From the
949 point of view of unification, simply returning an
950 rvalue of the right type is good enough. */
951 f = cand->fn;
952 t2 = TREE_TYPE (TREE_TYPE (f));
953 if (TREE_CODE (t2) != REFERENCE_TYPE
954 || !reference_compatible_p (t, TREE_TYPE (t2)))
956 candidates = candidates->next;
957 cand = NULL;
961 else if (TREE_CODE (t2) == REFERENCE_TYPE
962 && reference_compatible_p (t, TREE_TYPE (t2)))
963 cand = add_function_candidate (&candidates, f, s, arglist,
964 TYPE_BINFO (s),
965 TREE_PURPOSE (conversions),
966 LOOKUP_NORMAL);
968 if (cand)
970 conversion *identity_conv;
971 /* Build a standard conversion sequence indicating the
972 binding from the reference type returned by the
973 function to the desired REFERENCE_TYPE. */
974 identity_conv
975 = build_identity_conv (TREE_TYPE (TREE_TYPE
976 (TREE_TYPE (cand->fn))),
977 NULL_TREE);
978 cand->second_conv
979 = (direct_reference_binding
980 (reference_type, identity_conv));
981 cand->second_conv->bad_p |= cand->convs[0]->bad_p;
984 conversions = TREE_CHAIN (conversions);
987 candidates = splice_viable (candidates, pedantic, &any_viable_p);
988 /* If none of the conversion functions worked out, let our caller
989 know. */
990 if (!any_viable_p)
991 return NULL;
993 cand = tourney (candidates);
994 if (!cand)
995 return NULL;
997 /* Now that we know that this is the function we're going to use fix
998 the dummy first argument. */
999 cand->args = tree_cons (NULL_TREE,
1000 build_this (expr),
1001 TREE_CHAIN (cand->args));
1003 /* Build a user-defined conversion sequence representing the
1004 conversion. */
1005 conv = build_conv (ck_user,
1006 TREE_TYPE (TREE_TYPE (cand->fn)),
1007 build_identity_conv (TREE_TYPE (expr), expr));
1008 conv->cand = cand;
1010 /* Merge it with the standard conversion sequence from the
1011 conversion function's return type to the desired type. */
1012 cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
1014 if (cand->viable == -1)
1015 conv->bad_p = true;
1017 return cand->second_conv;
1020 /* A reference of the indicated TYPE is being bound directly to the
1021 expression represented by the implicit conversion sequence CONV.
1022 Return a conversion sequence for this binding. */
1024 static conversion *
1025 direct_reference_binding (tree type, conversion *conv)
1027 tree t;
1029 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 20030306);
1030 my_friendly_assert (TREE_CODE (conv->type) != REFERENCE_TYPE, 20030306);
1032 t = TREE_TYPE (type);
1034 /* [over.ics.rank]
1036 When a parameter of reference type binds directly
1037 (_dcl.init.ref_) to an argument expression, the implicit
1038 conversion sequence is the identity conversion, unless the
1039 argument expression has a type that is a derived class of the
1040 parameter type, in which case the implicit conversion sequence is
1041 a derived-to-base Conversion.
1043 If the parameter binds directly to the result of applying a
1044 conversion function to the argument expression, the implicit
1045 conversion sequence is a user-defined conversion sequence
1046 (_over.ics.user_), with the second standard conversion sequence
1047 either an identity conversion or, if the conversion function
1048 returns an entity of a type that is a derived class of the
1049 parameter type, a derived-to-base conversion. */
1050 if (!same_type_ignoring_top_level_qualifiers_p (t, conv->type))
1052 /* Represent the derived-to-base conversion. */
1053 conv = build_conv (ck_base, t, conv);
1054 /* We will actually be binding to the base-class subobject in
1055 the derived class, so we mark this conversion appropriately.
1056 That way, convert_like knows not to generate a temporary. */
1057 conv->need_temporary_p = false;
1059 return build_conv (ck_ref_bind, type, conv);
1062 /* Returns the conversion path from type FROM to reference type TO for
1063 purposes of reference binding. For lvalue binding, either pass a
1064 reference type to FROM or an lvalue expression to EXPR. If the
1065 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1066 the conversion returned. */
1068 static conversion *
1069 reference_binding (tree rto, tree rfrom, tree expr, int flags)
1071 conversion *conv = NULL;
1072 tree to = TREE_TYPE (rto);
1073 tree from = rfrom;
1074 bool related_p;
1075 bool compatible_p;
1076 cp_lvalue_kind lvalue_p = clk_none;
1078 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1080 expr = instantiate_type (to, expr, tf_none);
1081 if (expr == error_mark_node)
1082 return NULL;
1083 from = TREE_TYPE (expr);
1086 if (TREE_CODE (from) == REFERENCE_TYPE)
1088 /* Anything with reference type is an lvalue. */
1089 lvalue_p = clk_ordinary;
1090 from = TREE_TYPE (from);
1092 else if (expr)
1093 lvalue_p = real_lvalue_p (expr);
1095 /* Figure out whether or not the types are reference-related and
1096 reference compatible. We have do do this after stripping
1097 references from FROM. */
1098 related_p = reference_related_p (to, from);
1099 compatible_p = reference_compatible_p (to, from);
1101 if (lvalue_p && compatible_p)
1103 /* [dcl.init.ref]
1105 If the initializer expression
1107 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1108 is reference-compatible with "cv2 T2,"
1110 the reference is bound directly to the initializer expression
1111 lvalue. */
1112 conv = build_identity_conv (from, expr);
1113 conv = direct_reference_binding (rto, conv);
1114 if ((lvalue_p & clk_bitfield) != 0
1115 || ((lvalue_p & clk_packed) != 0 && !TYPE_PACKED (to)))
1116 /* For the purposes of overload resolution, we ignore the fact
1117 this expression is a bitfield or packed field. (In particular,
1118 [over.ics.ref] says specifically that a function with a
1119 non-const reference parameter is viable even if the
1120 argument is a bitfield.)
1122 However, when we actually call the function we must create
1123 a temporary to which to bind the reference. If the
1124 reference is volatile, or isn't const, then we cannot make
1125 a temporary, so we just issue an error when the conversion
1126 actually occurs. */
1127 conv->need_temporary_p = true;
1129 return conv;
1131 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1133 /* [dcl.init.ref]
1135 If the initializer expression
1137 -- has a class type (i.e., T2 is a class type) can be
1138 implicitly converted to an lvalue of type "cv3 T3," where
1139 "cv1 T1" is reference-compatible with "cv3 T3". (this
1140 conversion is selected by enumerating the applicable
1141 conversion functions (_over.match.ref_) and choosing the
1142 best one through overload resolution. (_over.match_).
1144 the reference is bound to the lvalue result of the conversion
1145 in the second case. */
1146 conv = convert_class_to_reference (to, from, expr);
1147 if (conv)
1148 return conv;
1151 /* From this point on, we conceptually need temporaries, even if we
1152 elide them. Only the cases above are "direct bindings". */
1153 if (flags & LOOKUP_NO_TEMP_BIND)
1154 return NULL;
1156 /* [over.ics.rank]
1158 When a parameter of reference type is not bound directly to an
1159 argument expression, the conversion sequence is the one required
1160 to convert the argument expression to the underlying type of the
1161 reference according to _over.best.ics_. Conceptually, this
1162 conversion sequence corresponds to copy-initializing a temporary
1163 of the underlying type with the argument expression. Any
1164 difference in top-level cv-qualification is subsumed by the
1165 initialization itself and does not constitute a conversion. */
1167 /* [dcl.init.ref]
1169 Otherwise, the reference shall be to a non-volatile const type. */
1170 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1171 return NULL;
1173 /* [dcl.init.ref]
1175 If the initializer expression is an rvalue, with T2 a class type,
1176 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1177 is bound in one of the following ways:
1179 -- The reference is bound to the object represented by the rvalue
1180 or to a sub-object within that object.
1182 -- ...
1184 We use the first alternative. The implicit conversion sequence
1185 is supposed to be same as we would obtain by generating a
1186 temporary. Fortunately, if the types are reference compatible,
1187 then this is either an identity conversion or the derived-to-base
1188 conversion, just as for direct binding. */
1189 if (CLASS_TYPE_P (from) && compatible_p)
1191 conv = build_identity_conv (from, expr);
1192 conv = direct_reference_binding (rto, conv);
1193 conv->u.next->check_copy_constructor_p = true;
1194 return conv;
1197 /* [dcl.init.ref]
1199 Otherwise, a temporary of type "cv1 T1" is created and
1200 initialized from the initializer expression using the rules for a
1201 non-reference copy initialization. If T1 is reference-related to
1202 T2, cv1 must be the same cv-qualification as, or greater
1203 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1204 if (related_p && !at_least_as_qualified_p (to, from))
1205 return NULL;
1207 conv = implicit_conversion (to, from, expr, flags);
1208 if (!conv)
1209 return NULL;
1211 conv = build_conv (ck_ref_bind, rto, conv);
1212 /* This reference binding, unlike those above, requires the
1213 creation of a temporary. */
1214 conv->need_temporary_p = true;
1216 return conv;
1219 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1220 to type TO. The optional expression EXPR may affect the conversion.
1221 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1222 significant. */
1224 static conversion *
1225 implicit_conversion (tree to, tree from, tree expr, int flags)
1227 conversion *conv;
1229 if (from == error_mark_node || to == error_mark_node
1230 || expr == error_mark_node)
1231 return NULL;
1233 if (TREE_CODE (to) == REFERENCE_TYPE)
1234 conv = reference_binding (to, from, expr, flags);
1235 else
1236 conv = standard_conversion (to, from, expr);
1238 if (conv)
1239 return conv;
1241 if (expr != NULL_TREE
1242 && (IS_AGGR_TYPE (from)
1243 || IS_AGGR_TYPE (to))
1244 && (flags & LOOKUP_NO_CONVERSION) == 0)
1246 struct z_candidate *cand;
1248 cand = build_user_type_conversion_1
1249 (to, expr, LOOKUP_ONLYCONVERTING);
1250 if (cand)
1251 conv = cand->second_conv;
1253 /* We used to try to bind a reference to a temporary here, but that
1254 is now handled by the recursive call to this function at the end
1255 of reference_binding. */
1256 return conv;
1259 return NULL;
1262 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1263 functions. */
1265 static struct z_candidate *
1266 add_candidate (struct z_candidate **candidates,
1267 tree fn, tree args,
1268 size_t num_convs, conversion **convs,
1269 tree access_path, tree conversion_path,
1270 int viable)
1272 struct z_candidate *cand
1273 = conversion_obstack_alloc (sizeof (struct z_candidate));
1275 cand->fn = fn;
1276 cand->args = args;
1277 cand->convs = convs;
1278 cand->num_convs = num_convs;
1279 cand->access_path = access_path;
1280 cand->conversion_path = conversion_path;
1281 cand->viable = viable;
1282 cand->next = *candidates;
1283 *candidates = cand;
1285 return cand;
1288 /* Create an overload candidate for the function or method FN called with
1289 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1290 to implicit_conversion.
1292 CTYPE, if non-NULL, is the type we want to pretend this function
1293 comes from for purposes of overload resolution. */
1295 static struct z_candidate *
1296 add_function_candidate (struct z_candidate **candidates,
1297 tree fn, tree ctype, tree arglist,
1298 tree access_path, tree conversion_path,
1299 int flags)
1301 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1302 int i, len;
1303 conversion **convs;
1304 tree parmnode, argnode;
1305 tree orig_arglist;
1306 int viable = 1;
1308 /* Built-in functions that haven't been declared don't really
1309 exist. */
1310 if (DECL_ANTICIPATED (fn))
1311 return NULL;
1313 /* The `this', `in_chrg' and VTT arguments to constructors are not
1314 considered in overload resolution. */
1315 if (DECL_CONSTRUCTOR_P (fn))
1317 parmlist = skip_artificial_parms_for (fn, parmlist);
1318 orig_arglist = arglist;
1319 arglist = skip_artificial_parms_for (fn, arglist);
1321 else
1322 orig_arglist = arglist;
1324 len = list_length (arglist);
1325 convs = alloc_conversions (len);
1327 /* 13.3.2 - Viable functions [over.match.viable]
1328 First, to be a viable function, a candidate function shall have enough
1329 parameters to agree in number with the arguments in the list.
1331 We need to check this first; otherwise, checking the ICSes might cause
1332 us to produce an ill-formed template instantiation. */
1334 parmnode = parmlist;
1335 for (i = 0; i < len; ++i)
1337 if (parmnode == NULL_TREE || parmnode == void_list_node)
1338 break;
1339 parmnode = TREE_CHAIN (parmnode);
1342 if (i < len && parmnode)
1343 viable = 0;
1345 /* Make sure there are default args for the rest of the parms. */
1346 else if (!sufficient_parms_p (parmnode))
1347 viable = 0;
1349 if (! viable)
1350 goto out;
1352 /* Second, for F to be a viable function, there shall exist for each
1353 argument an implicit conversion sequence that converts that argument
1354 to the corresponding parameter of F. */
1356 parmnode = parmlist;
1357 argnode = arglist;
1359 for (i = 0; i < len; ++i)
1361 tree arg = TREE_VALUE (argnode);
1362 tree argtype = lvalue_type (arg);
1363 conversion *t;
1364 int is_this;
1366 if (parmnode == void_list_node)
1367 break;
1369 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1370 && ! DECL_CONSTRUCTOR_P (fn));
1372 if (parmnode)
1374 tree parmtype = TREE_VALUE (parmnode);
1376 /* The type of the implicit object parameter ('this') for
1377 overload resolution is not always the same as for the
1378 function itself; conversion functions are considered to
1379 be members of the class being converted, and functions
1380 introduced by a using-declaration are considered to be
1381 members of the class that uses them.
1383 Since build_over_call ignores the ICS for the `this'
1384 parameter, we can just change the parm type. */
1385 if (ctype && is_this)
1387 parmtype
1388 = build_qualified_type (ctype,
1389 TYPE_QUALS (TREE_TYPE (parmtype)));
1390 parmtype = build_pointer_type (parmtype);
1393 t = implicit_conversion (parmtype, argtype, arg, flags);
1395 else
1397 t = build_identity_conv (argtype, arg);
1398 t->ellipsis_p = true;
1401 if (t && is_this)
1402 t->this_p = true;
1404 convs[i] = t;
1405 if (! t)
1407 viable = 0;
1408 break;
1411 if (t->bad_p)
1412 viable = -1;
1414 if (parmnode)
1415 parmnode = TREE_CHAIN (parmnode);
1416 argnode = TREE_CHAIN (argnode);
1419 out:
1420 return add_candidate (candidates, fn, orig_arglist, len, convs,
1421 access_path, conversion_path, viable);
1424 /* Create an overload candidate for the conversion function FN which will
1425 be invoked for expression OBJ, producing a pointer-to-function which
1426 will in turn be called with the argument list ARGLIST, and add it to
1427 CANDIDATES. FLAGS is passed on to implicit_conversion.
1429 Actually, we don't really care about FN; we care about the type it
1430 converts to. There may be multiple conversion functions that will
1431 convert to that type, and we rely on build_user_type_conversion_1 to
1432 choose the best one; so when we create our candidate, we record the type
1433 instead of the function. */
1435 static struct z_candidate *
1436 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
1437 tree arglist, tree access_path, tree conversion_path)
1439 tree totype = TREE_TYPE (TREE_TYPE (fn));
1440 int i, len, viable, flags;
1441 tree parmlist, parmnode, argnode;
1442 conversion **convs;
1444 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1445 parmlist = TREE_TYPE (parmlist);
1446 parmlist = TYPE_ARG_TYPES (parmlist);
1448 len = list_length (arglist) + 1;
1449 convs = alloc_conversions (len);
1450 parmnode = parmlist;
1451 argnode = arglist;
1452 viable = 1;
1453 flags = LOOKUP_NORMAL;
1455 /* Don't bother looking up the same type twice. */
1456 if (*candidates && (*candidates)->fn == totype)
1457 return NULL;
1459 for (i = 0; i < len; ++i)
1461 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1462 tree argtype = lvalue_type (arg);
1463 conversion *t;
1465 if (i == 0)
1466 t = implicit_conversion (totype, argtype, arg, flags);
1467 else if (parmnode == void_list_node)
1468 break;
1469 else if (parmnode)
1470 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1471 else
1473 t = build_identity_conv (argtype, arg);
1474 t->ellipsis_p = true;
1477 convs[i] = t;
1478 if (! t)
1479 break;
1481 if (t->bad_p)
1482 viable = -1;
1484 if (i == 0)
1485 continue;
1487 if (parmnode)
1488 parmnode = TREE_CHAIN (parmnode);
1489 argnode = TREE_CHAIN (argnode);
1492 if (i < len)
1493 viable = 0;
1495 if (!sufficient_parms_p (parmnode))
1496 viable = 0;
1498 return add_candidate (candidates, totype, arglist, len, convs,
1499 access_path, conversion_path, viable);
1502 static void
1503 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
1504 tree type1, tree type2, tree *args, tree *argtypes,
1505 int flags)
1507 conversion *t;
1508 conversion **convs;
1509 size_t num_convs;
1510 int viable = 1, i;
1511 tree types[2];
1513 types[0] = type1;
1514 types[1] = type2;
1516 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
1517 convs = alloc_conversions (num_convs);
1519 for (i = 0; i < 2; ++i)
1521 if (! args[i])
1522 break;
1524 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1525 if (! t)
1527 viable = 0;
1528 /* We need something for printing the candidate. */
1529 t = build_identity_conv (types[i], NULL_TREE);
1531 else if (t->bad_p)
1532 viable = 0;
1533 convs[i] = t;
1536 /* For COND_EXPR we rearranged the arguments; undo that now. */
1537 if (args[2])
1539 convs[2] = convs[1];
1540 convs[1] = convs[0];
1541 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1542 if (t)
1543 convs[0] = t;
1544 else
1545 viable = 0;
1548 add_candidate (candidates, fnname, /*args=*/NULL_TREE,
1549 num_convs, convs,
1550 /*access_path=*/NULL_TREE,
1551 /*conversion_path=*/NULL_TREE,
1552 viable);
1555 static bool
1556 is_complete (tree t)
1558 return COMPLETE_TYPE_P (complete_type (t));
1561 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1563 static bool
1564 promoted_arithmetic_type_p (tree type)
1566 /* [over.built]
1568 In this section, the term promoted integral type is used to refer
1569 to those integral types which are preserved by integral promotion
1570 (including e.g. int and long but excluding e.g. char).
1571 Similarly, the term promoted arithmetic type refers to promoted
1572 integral types plus floating types. */
1573 return ((INTEGRAL_TYPE_P (type)
1574 && same_type_p (type_promotes_to (type), type))
1575 || TREE_CODE (type) == REAL_TYPE);
1578 /* Create any builtin operator overload candidates for the operator in
1579 question given the converted operand types TYPE1 and TYPE2. The other
1580 args are passed through from add_builtin_candidates to
1581 build_builtin_candidate.
1583 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1584 If CODE is requires candidates operands of the same type of the kind
1585 of which TYPE1 and TYPE2 are, we add both candidates
1586 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1588 static void
1589 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
1590 enum tree_code code2, tree fnname, tree type1,
1591 tree type2, tree *args, tree *argtypes, int flags)
1593 switch (code)
1595 case POSTINCREMENT_EXPR:
1596 case POSTDECREMENT_EXPR:
1597 args[1] = integer_zero_node;
1598 type2 = integer_type_node;
1599 break;
1600 default:
1601 break;
1604 switch (code)
1607 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1608 and VQ is either volatile or empty, there exist candidate operator
1609 functions of the form
1610 VQ T& operator++(VQ T&);
1611 T operator++(VQ T&, int);
1612 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1613 type other than bool, and VQ is either volatile or empty, there exist
1614 candidate operator functions of the form
1615 VQ T& operator--(VQ T&);
1616 T operator--(VQ T&, int);
1617 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1618 complete object type, and VQ is either volatile or empty, there exist
1619 candidate operator functions of the form
1620 T*VQ& operator++(T*VQ&);
1621 T*VQ& operator--(T*VQ&);
1622 T* operator++(T*VQ&, int);
1623 T* operator--(T*VQ&, int); */
1625 case POSTDECREMENT_EXPR:
1626 case PREDECREMENT_EXPR:
1627 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1628 return;
1629 case POSTINCREMENT_EXPR:
1630 case PREINCREMENT_EXPR:
1631 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1633 type1 = build_reference_type (type1);
1634 break;
1636 return;
1638 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1639 exist candidate operator functions of the form
1641 T& operator*(T*);
1643 8 For every function type T, there exist candidate operator functions of
1644 the form
1645 T& operator*(T*); */
1647 case INDIRECT_REF:
1648 if (TREE_CODE (type1) == POINTER_TYPE
1649 && (TYPE_PTROB_P (type1)
1650 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1651 break;
1652 return;
1654 /* 9 For every type T, there exist candidate operator functions of the form
1655 T* operator+(T*);
1657 10For every promoted arithmetic type T, there exist candidate operator
1658 functions of the form
1659 T operator+(T);
1660 T operator-(T); */
1662 case CONVERT_EXPR: /* unary + */
1663 if (TREE_CODE (type1) == POINTER_TYPE)
1664 break;
1665 case NEGATE_EXPR:
1666 if (ARITHMETIC_TYPE_P (type1))
1667 break;
1668 return;
1670 /* 11For every promoted integral type T, there exist candidate operator
1671 functions of the form
1672 T operator~(T); */
1674 case BIT_NOT_EXPR:
1675 if (INTEGRAL_TYPE_P (type1))
1676 break;
1677 return;
1679 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1680 is the same type as C2 or is a derived class of C2, T is a complete
1681 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1682 there exist candidate operator functions of the form
1683 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1684 where CV12 is the union of CV1 and CV2. */
1686 case MEMBER_REF:
1687 if (TREE_CODE (type1) == POINTER_TYPE
1688 && TYPE_PTR_TO_MEMBER_P (type2))
1690 tree c1 = TREE_TYPE (type1);
1691 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
1693 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1694 && (TYPE_PTRMEMFUNC_P (type2)
1695 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1696 break;
1698 return;
1700 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1701 didate operator functions of the form
1702 LR operator*(L, R);
1703 LR operator/(L, R);
1704 LR operator+(L, R);
1705 LR operator-(L, R);
1706 bool operator<(L, R);
1707 bool operator>(L, R);
1708 bool operator<=(L, R);
1709 bool operator>=(L, R);
1710 bool operator==(L, R);
1711 bool operator!=(L, R);
1712 where LR is the result of the usual arithmetic conversions between
1713 types L and R.
1715 14For every pair of types T and I, where T is a cv-qualified or cv-
1716 unqualified complete object type and I is a promoted integral type,
1717 there exist candidate operator functions of the form
1718 T* operator+(T*, I);
1719 T& operator[](T*, I);
1720 T* operator-(T*, I);
1721 T* operator+(I, T*);
1722 T& operator[](I, T*);
1724 15For every T, where T is a pointer to complete object type, there exist
1725 candidate operator functions of the form112)
1726 ptrdiff_t operator-(T, T);
1728 16For every pointer or enumeration type T, there exist candidate operator
1729 functions of the form
1730 bool operator<(T, T);
1731 bool operator>(T, T);
1732 bool operator<=(T, T);
1733 bool operator>=(T, T);
1734 bool operator==(T, T);
1735 bool operator!=(T, T);
1737 17For every pointer to member type T, there exist candidate operator
1738 functions of the form
1739 bool operator==(T, T);
1740 bool operator!=(T, T); */
1742 case MINUS_EXPR:
1743 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1744 break;
1745 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1747 type2 = ptrdiff_type_node;
1748 break;
1750 case MULT_EXPR:
1751 case TRUNC_DIV_EXPR:
1752 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1753 break;
1754 return;
1756 case EQ_EXPR:
1757 case NE_EXPR:
1758 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1759 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1760 break;
1761 if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
1763 type2 = type1;
1764 break;
1766 if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
1768 type1 = type2;
1769 break;
1771 /* Fall through. */
1772 case LT_EXPR:
1773 case GT_EXPR:
1774 case LE_EXPR:
1775 case GE_EXPR:
1776 case MAX_EXPR:
1777 case MIN_EXPR:
1778 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1779 break;
1780 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1781 break;
1782 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1783 break;
1784 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1786 type2 = type1;
1787 break;
1789 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1791 type1 = type2;
1792 break;
1794 return;
1796 case PLUS_EXPR:
1797 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1798 break;
1799 case ARRAY_REF:
1800 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1802 type1 = ptrdiff_type_node;
1803 break;
1805 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1807 type2 = ptrdiff_type_node;
1808 break;
1810 return;
1812 /* 18For every pair of promoted integral types L and R, there exist candi-
1813 date operator functions of the form
1814 LR operator%(L, R);
1815 LR operator&(L, R);
1816 LR operator^(L, R);
1817 LR operator|(L, R);
1818 L operator<<(L, R);
1819 L operator>>(L, R);
1820 where LR is the result of the usual arithmetic conversions between
1821 types L and R. */
1823 case TRUNC_MOD_EXPR:
1824 case BIT_AND_EXPR:
1825 case BIT_IOR_EXPR:
1826 case BIT_XOR_EXPR:
1827 case LSHIFT_EXPR:
1828 case RSHIFT_EXPR:
1829 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1830 break;
1831 return;
1833 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1834 type, VQ is either volatile or empty, and R is a promoted arithmetic
1835 type, there exist candidate operator functions of the form
1836 VQ L& operator=(VQ L&, R);
1837 VQ L& operator*=(VQ L&, R);
1838 VQ L& operator/=(VQ L&, R);
1839 VQ L& operator+=(VQ L&, R);
1840 VQ L& operator-=(VQ L&, R);
1842 20For every pair T, VQ), where T is any type and VQ is either volatile
1843 or empty, there exist candidate operator functions of the form
1844 T*VQ& operator=(T*VQ&, T*);
1846 21For every pair T, VQ), where T is a pointer to member type and VQ is
1847 either volatile or empty, there exist candidate operator functions of
1848 the form
1849 VQ T& operator=(VQ T&, T);
1851 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1852 unqualified complete object type, VQ is either volatile or empty, and
1853 I is a promoted integral type, there exist candidate operator func-
1854 tions of the form
1855 T*VQ& operator+=(T*VQ&, I);
1856 T*VQ& operator-=(T*VQ&, I);
1858 23For every triple L, VQ, R), where L is an integral or enumeration
1859 type, VQ is either volatile or empty, and R is a promoted integral
1860 type, there exist candidate operator functions of the form
1862 VQ L& operator%=(VQ L&, R);
1863 VQ L& operator<<=(VQ L&, R);
1864 VQ L& operator>>=(VQ L&, R);
1865 VQ L& operator&=(VQ L&, R);
1866 VQ L& operator^=(VQ L&, R);
1867 VQ L& operator|=(VQ L&, R); */
1869 case MODIFY_EXPR:
1870 switch (code2)
1872 case PLUS_EXPR:
1873 case MINUS_EXPR:
1874 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1876 type2 = ptrdiff_type_node;
1877 break;
1879 case MULT_EXPR:
1880 case TRUNC_DIV_EXPR:
1881 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1882 break;
1883 return;
1885 case TRUNC_MOD_EXPR:
1886 case BIT_AND_EXPR:
1887 case BIT_IOR_EXPR:
1888 case BIT_XOR_EXPR:
1889 case LSHIFT_EXPR:
1890 case RSHIFT_EXPR:
1891 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1892 break;
1893 return;
1895 case NOP_EXPR:
1896 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1897 break;
1898 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1899 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1900 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1901 || ((TYPE_PTRMEMFUNC_P (type1)
1902 || TREE_CODE (type1) == POINTER_TYPE)
1903 && null_ptr_cst_p (args[1])))
1905 type2 = type1;
1906 break;
1908 return;
1910 default:
1911 abort ();
1913 type1 = build_reference_type (type1);
1914 break;
1916 case COND_EXPR:
1917 /* [over.built]
1919 For every pair of promoted arithmetic types L and R, there
1920 exist candidate operator functions of the form
1922 LR operator?(bool, L, R);
1924 where LR is the result of the usual arithmetic conversions
1925 between types L and R.
1927 For every type T, where T is a pointer or pointer-to-member
1928 type, there exist candidate operator functions of the form T
1929 operator?(bool, T, T); */
1931 if (promoted_arithmetic_type_p (type1)
1932 && promoted_arithmetic_type_p (type2))
1933 /* That's OK. */
1934 break;
1936 /* Otherwise, the types should be pointers. */
1937 if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
1938 || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
1939 return;
1941 /* We don't check that the two types are the same; the logic
1942 below will actually create two candidates; one in which both
1943 parameter types are TYPE1, and one in which both parameter
1944 types are TYPE2. */
1945 break;
1947 default:
1948 abort ();
1951 /* If we're dealing with two pointer types or two enumeral types,
1952 we need candidates for both of them. */
1953 if (type2 && !same_type_p (type1, type2)
1954 && TREE_CODE (type1) == TREE_CODE (type2)
1955 && (TREE_CODE (type1) == REFERENCE_TYPE
1956 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1957 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1958 || TYPE_PTRMEMFUNC_P (type1)
1959 || IS_AGGR_TYPE (type1)
1960 || TREE_CODE (type1) == ENUMERAL_TYPE))
1962 build_builtin_candidate
1963 (candidates, fnname, type1, type1, args, argtypes, flags);
1964 build_builtin_candidate
1965 (candidates, fnname, type2, type2, args, argtypes, flags);
1966 return;
1969 build_builtin_candidate
1970 (candidates, fnname, type1, type2, args, argtypes, flags);
1973 tree
1974 type_decays_to (tree type)
1976 if (TREE_CODE (type) == ARRAY_TYPE)
1977 return build_pointer_type (TREE_TYPE (type));
1978 if (TREE_CODE (type) == FUNCTION_TYPE)
1979 return build_pointer_type (type);
1980 return type;
1983 /* There are three conditions of builtin candidates:
1985 1) bool-taking candidates. These are the same regardless of the input.
1986 2) pointer-pair taking candidates. These are generated for each type
1987 one of the input types converts to.
1988 3) arithmetic candidates. According to the standard, we should generate
1989 all of these, but I'm trying not to...
1991 Here we generate a superset of the possible candidates for this particular
1992 case. That is a subset of the full set the standard defines, plus some
1993 other cases which the standard disallows. add_builtin_candidate will
1994 filter out the invalid set. */
1996 static void
1997 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
1998 enum tree_code code2, tree fnname, tree *args,
1999 int flags)
2001 int ref1, i;
2002 int enum_p = 0;
2003 tree type, argtypes[3];
2004 /* TYPES[i] is the set of possible builtin-operator parameter types
2005 we will consider for the Ith argument. These are represented as
2006 a TREE_LIST; the TREE_VALUE of each node is the potential
2007 parameter type. */
2008 tree types[2];
2010 for (i = 0; i < 3; ++i)
2012 if (args[i])
2013 argtypes[i] = lvalue_type (args[i]);
2014 else
2015 argtypes[i] = NULL_TREE;
2018 switch (code)
2020 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2021 and VQ is either volatile or empty, there exist candidate operator
2022 functions of the form
2023 VQ T& operator++(VQ T&); */
2025 case POSTINCREMENT_EXPR:
2026 case PREINCREMENT_EXPR:
2027 case POSTDECREMENT_EXPR:
2028 case PREDECREMENT_EXPR:
2029 case MODIFY_EXPR:
2030 ref1 = 1;
2031 break;
2033 /* 24There also exist candidate operator functions of the form
2034 bool operator!(bool);
2035 bool operator&&(bool, bool);
2036 bool operator||(bool, bool); */
2038 case TRUTH_NOT_EXPR:
2039 build_builtin_candidate
2040 (candidates, fnname, boolean_type_node,
2041 NULL_TREE, args, argtypes, flags);
2042 return;
2044 case TRUTH_ORIF_EXPR:
2045 case TRUTH_ANDIF_EXPR:
2046 build_builtin_candidate
2047 (candidates, fnname, boolean_type_node,
2048 boolean_type_node, args, argtypes, flags);
2049 return;
2051 case ADDR_EXPR:
2052 case COMPOUND_EXPR:
2053 case COMPONENT_REF:
2054 return;
2056 case COND_EXPR:
2057 case EQ_EXPR:
2058 case NE_EXPR:
2059 case LT_EXPR:
2060 case LE_EXPR:
2061 case GT_EXPR:
2062 case GE_EXPR:
2063 enum_p = 1;
2064 /* Fall through. */
2066 default:
2067 ref1 = 0;
2070 types[0] = types[1] = NULL_TREE;
2072 for (i = 0; i < 2; ++i)
2074 if (! args[i])
2076 else if (IS_AGGR_TYPE (argtypes[i]))
2078 tree convs;
2080 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2081 return;
2083 convs = lookup_conversions (argtypes[i]);
2085 if (code == COND_EXPR)
2087 if (real_lvalue_p (args[i]))
2088 types[i] = tree_cons
2089 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2091 types[i] = tree_cons
2092 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2095 else if (! convs)
2096 return;
2098 for (; convs; convs = TREE_CHAIN (convs))
2100 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2102 if (i == 0 && ref1
2103 && (TREE_CODE (type) != REFERENCE_TYPE
2104 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2105 continue;
2107 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2108 types[i] = tree_cons (NULL_TREE, type, types[i]);
2110 type = non_reference (type);
2111 if (i != 0 || ! ref1)
2113 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2114 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2115 types[i] = tree_cons (NULL_TREE, type, types[i]);
2116 if (INTEGRAL_TYPE_P (type))
2117 type = type_promotes_to (type);
2120 if (! value_member (type, types[i]))
2121 types[i] = tree_cons (NULL_TREE, type, types[i]);
2124 else
2126 if (code == COND_EXPR && real_lvalue_p (args[i]))
2127 types[i] = tree_cons
2128 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2129 type = non_reference (argtypes[i]);
2130 if (i != 0 || ! ref1)
2132 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2133 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2134 types[i] = tree_cons (NULL_TREE, type, types[i]);
2135 if (INTEGRAL_TYPE_P (type))
2136 type = type_promotes_to (type);
2138 types[i] = tree_cons (NULL_TREE, type, types[i]);
2142 /* Run through the possible parameter types of both arguments,
2143 creating candidates with those parameter types. */
2144 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2146 if (types[1])
2147 for (type = types[1]; type; type = TREE_CHAIN (type))
2148 add_builtin_candidate
2149 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2150 TREE_VALUE (type), args, argtypes, flags);
2151 else
2152 add_builtin_candidate
2153 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2154 NULL_TREE, args, argtypes, flags);
2157 return;
2161 /* If TMPL can be successfully instantiated as indicated by
2162 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2164 TMPL is the template. EXPLICIT_TARGS are any explicit template
2165 arguments. ARGLIST is the arguments provided at the call-site.
2166 The RETURN_TYPE is the desired type for conversion operators. If
2167 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2168 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2169 add_conv_candidate. */
2171 static struct z_candidate*
2172 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2173 tree ctype, tree explicit_targs, tree arglist,
2174 tree return_type, tree access_path,
2175 tree conversion_path, int flags, tree obj,
2176 unification_kind_t strict)
2178 int ntparms = DECL_NTPARMS (tmpl);
2179 tree targs = make_tree_vec (ntparms);
2180 tree args_without_in_chrg = arglist;
2181 struct z_candidate *cand;
2182 int i;
2183 tree fn;
2185 /* We don't do deduction on the in-charge parameter, the VTT
2186 parameter or 'this'. */
2187 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2188 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2190 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2191 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2192 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2193 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2195 i = fn_type_unification (tmpl, explicit_targs, targs,
2196 args_without_in_chrg,
2197 return_type, strict, -1);
2199 if (i != 0)
2200 return NULL;
2202 fn = instantiate_template (tmpl, targs, tf_none);
2203 if (fn == error_mark_node)
2204 return NULL;
2206 /* In [class.copy]:
2208 A member function template is never instantiated to perform the
2209 copy of a class object to an object of its class type.
2211 It's a little unclear what this means; the standard explicitly
2212 does allow a template to be used to copy a class. For example,
2215 struct A {
2216 A(A&);
2217 template <class T> A(const T&);
2219 const A f ();
2220 void g () { A a (f ()); }
2222 the member template will be used to make the copy. The section
2223 quoted above appears in the paragraph that forbids constructors
2224 whose only parameter is (a possibly cv-qualified variant of) the
2225 class type, and a logical interpretation is that the intent was
2226 to forbid the instantiation of member templates which would then
2227 have that form. */
2228 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2230 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2231 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2232 ctype))
2233 return NULL;
2236 if (obj != NULL_TREE)
2237 /* Aha, this is a conversion function. */
2238 cand = add_conv_candidate (candidates, fn, obj, access_path,
2239 conversion_path, arglist);
2240 else
2241 cand = add_function_candidate (candidates, fn, ctype,
2242 arglist, access_path,
2243 conversion_path, flags);
2244 if (DECL_TI_TEMPLATE (fn) != tmpl)
2245 /* This situation can occur if a member template of a template
2246 class is specialized. Then, instantiate_template might return
2247 an instantiation of the specialization, in which case the
2248 DECL_TI_TEMPLATE field will point at the original
2249 specialization. For example:
2251 template <class T> struct S { template <class U> void f(U);
2252 template <> void f(int) {}; };
2253 S<double> sd;
2254 sd.f(3);
2256 Here, TMPL will be template <class U> S<double>::f(U).
2257 And, instantiate template will give us the specialization
2258 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2259 for this will point at template <class T> template <> S<T>::f(int),
2260 so that we can find the definition. For the purposes of
2261 overload resolution, however, we want the original TMPL. */
2262 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2263 else
2264 cand->template = DECL_TEMPLATE_INFO (fn);
2266 return cand;
2270 static struct z_candidate *
2271 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2272 tree explicit_targs, tree arglist, tree return_type,
2273 tree access_path, tree conversion_path, int flags,
2274 unification_kind_t strict)
2276 return
2277 add_template_candidate_real (candidates, tmpl, ctype,
2278 explicit_targs, arglist, return_type,
2279 access_path, conversion_path,
2280 flags, NULL_TREE, strict);
2284 static struct z_candidate *
2285 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2286 tree obj, tree arglist, tree return_type,
2287 tree access_path, tree conversion_path)
2289 return
2290 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2291 arglist, return_type, access_path,
2292 conversion_path, 0, obj, DEDUCE_CONV);
2295 /* The CANDS are the set of candidates that were considered for
2296 overload resolution. Return the set of viable candidates. If none
2297 of the candidates were viable, set *ANY_VIABLE_P to true. STRICT_P
2298 is true if a candidate should be considered viable only if it is
2299 strictly viable. */
2301 static struct z_candidate*
2302 splice_viable (struct z_candidate *cands,
2303 bool strict_p,
2304 bool *any_viable_p)
2306 struct z_candidate *viable;
2307 struct z_candidate **last_viable;
2308 struct z_candidate **cand;
2310 viable = NULL;
2311 last_viable = &viable;
2312 *any_viable_p = false;
2314 cand = &cands;
2315 while (*cand)
2317 struct z_candidate *c = *cand;
2318 if (strict_p ? c->viable == 1 : c->viable)
2320 *last_viable = c;
2321 *cand = c->next;
2322 c->next = NULL;
2323 last_viable = &c->next;
2324 *any_viable_p = true;
2326 else
2327 cand = &c->next;
2330 return viable ? viable : cands;
2333 static bool
2334 any_strictly_viable (struct z_candidate *cands)
2336 for (; cands; cands = cands->next)
2337 if (cands->viable == 1)
2338 return true;
2339 return false;
2342 static tree
2343 build_this (tree obj)
2345 /* Fix this to work on non-lvalues. */
2346 return build_unary_op (ADDR_EXPR, obj, 0);
2349 /* Returns true iff functions are equivalent. Equivalent functions are
2350 not '==' only if one is a function-local extern function or if
2351 both are extern "C". */
2353 static inline int
2354 equal_functions (tree fn1, tree fn2)
2356 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2357 || DECL_EXTERN_C_FUNCTION_P (fn1))
2358 return decls_match (fn1, fn2);
2359 return fn1 == fn2;
2362 /* Print information about one overload candidate CANDIDATE. MSGSTR
2363 is the text to print before the candidate itself.
2365 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2366 to have been run through gettext by the caller. This wart makes
2367 life simpler in print_z_candidates and for the translators. */
2369 static void
2370 print_z_candidate (const char *msgstr, struct z_candidate *candidate)
2372 if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2374 if (candidate->num_convs == 3)
2375 inform ("%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
2376 candidate->convs[0]->type,
2377 candidate->convs[1]->type,
2378 candidate->convs[2]->type);
2379 else if (candidate->num_convs == 2)
2380 inform ("%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
2381 candidate->convs[0]->type,
2382 candidate->convs[1]->type);
2383 else
2384 inform ("%s %D(%T) <built-in>", msgstr, candidate->fn,
2385 candidate->convs[0]->type);
2387 else if (TYPE_P (candidate->fn))
2388 inform ("%s %T <conversion>", msgstr, candidate->fn);
2389 else if (candidate->viable == -1)
2390 inform ("%J%s %+#D <near match>", candidate->fn, msgstr, candidate->fn);
2391 else
2392 inform ("%J%s %+#D", candidate->fn, msgstr, candidate->fn);
2395 static void
2396 print_z_candidates (struct z_candidate *candidates)
2398 const char *str;
2399 struct z_candidate *cand1;
2400 struct z_candidate **cand2;
2402 /* There may be duplicates in the set of candidates. We put off
2403 checking this condition as long as possible, since we have no way
2404 to eliminate duplicates from a set of functions in less than n^2
2405 time. Now we are about to emit an error message, so it is more
2406 permissible to go slowly. */
2407 for (cand1 = candidates; cand1; cand1 = cand1->next)
2409 tree fn = cand1->fn;
2410 /* Skip builtin candidates and conversion functions. */
2411 if (TREE_CODE (fn) != FUNCTION_DECL)
2412 continue;
2413 cand2 = &cand1->next;
2414 while (*cand2)
2416 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2417 && equal_functions (fn, (*cand2)->fn))
2418 *cand2 = (*cand2)->next;
2419 else
2420 cand2 = &(*cand2)->next;
2424 if (!candidates)
2425 return;
2427 str = _("candidates are:");
2428 print_z_candidate (str, candidates);
2429 if (candidates->next)
2431 /* Indent successive candidates by the width of the translation
2432 of the above string. */
2433 size_t len = gcc_gettext_width (str) + 1;
2434 char *spaces = alloca (len);
2435 memset (spaces, ' ', len-1);
2436 spaces[len - 1] = '\0';
2438 candidates = candidates->next;
2441 print_z_candidate (spaces, candidates);
2442 candidates = candidates->next;
2444 while (candidates);
2448 /* USER_SEQ is a user-defined conversion sequence, beginning with a
2449 USER_CONV. STD_SEQ is the standard conversion sequence applied to
2450 the result of the conversion function to convert it to the final
2451 desired type. Merge the the two sequences into a single sequence,
2452 and return the merged sequence. */
2454 static conversion *
2455 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
2457 conversion **t;
2459 my_friendly_assert (user_seq->kind == ck_user, 20030306);
2461 /* Find the end of the second conversion sequence. */
2462 t = &(std_seq);
2463 while ((*t)->kind != ck_identity)
2464 t = &((*t)->u.next);
2466 /* Replace the identity conversion with the user conversion
2467 sequence. */
2468 *t = user_seq;
2470 /* The entire sequence is a user-conversion sequence. */
2471 std_seq->user_conv_p = true;
2473 return std_seq;
2476 /* Returns the best overload candidate to perform the requested
2477 conversion. This function is used for three the overloading situations
2478 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2479 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2480 per [dcl.init.ref], so we ignore temporary bindings. */
2482 static struct z_candidate *
2483 build_user_type_conversion_1 (tree totype, tree expr, int flags)
2485 struct z_candidate *candidates, *cand;
2486 tree fromtype = TREE_TYPE (expr);
2487 tree ctors = NULL_TREE;
2488 tree conv_fns = NULL_TREE;
2489 conversion *conv = NULL;
2490 tree args = NULL_TREE;
2491 bool any_viable_p;
2493 /* We represent conversion within a hierarchy using RVALUE_CONV and
2494 BASE_CONV, as specified by [over.best.ics]; these become plain
2495 constructor calls, as specified in [dcl.init]. */
2496 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2497 || !DERIVED_FROM_P (totype, fromtype), 20011226);
2499 if (IS_AGGR_TYPE (totype))
2500 ctors = lookup_fnfields (TYPE_BINFO (totype),
2501 complete_ctor_identifier,
2504 if (IS_AGGR_TYPE (fromtype))
2505 conv_fns = lookup_conversions (fromtype);
2507 candidates = 0;
2508 flags |= LOOKUP_NO_CONVERSION;
2510 if (ctors)
2512 tree t;
2514 ctors = BASELINK_FUNCTIONS (ctors);
2516 t = build_int_2 (0, 0);
2517 TREE_TYPE (t) = build_pointer_type (totype);
2518 args = build_tree_list (NULL_TREE, expr);
2519 /* We should never try to call the abstract or base constructor
2520 from here. */
2521 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2522 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2523 20011226);
2524 args = tree_cons (NULL_TREE, t, args);
2526 for (; ctors; ctors = OVL_NEXT (ctors))
2528 tree ctor = OVL_CURRENT (ctors);
2529 if (DECL_NONCONVERTING_P (ctor))
2530 continue;
2532 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2533 cand = add_template_candidate (&candidates, ctor, totype,
2534 NULL_TREE, args, NULL_TREE,
2535 TYPE_BINFO (totype),
2536 TYPE_BINFO (totype),
2537 flags,
2538 DEDUCE_CALL);
2539 else
2540 cand = add_function_candidate (&candidates, ctor, totype,
2541 args, TYPE_BINFO (totype),
2542 TYPE_BINFO (totype),
2543 flags);
2545 if (cand)
2546 cand->second_conv = build_identity_conv (totype, NULL_TREE);
2549 if (conv_fns)
2550 args = build_tree_list (NULL_TREE, build_this (expr));
2552 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
2554 tree fns;
2555 tree conversion_path = TREE_PURPOSE (conv_fns);
2556 int convflags = LOOKUP_NO_CONVERSION;
2558 /* If we are called to convert to a reference type, we are trying to
2559 find an lvalue binding, so don't even consider temporaries. If
2560 we don't find an lvalue binding, the caller will try again to
2561 look for a temporary binding. */
2562 if (TREE_CODE (totype) == REFERENCE_TYPE)
2563 convflags |= LOOKUP_NO_TEMP_BIND;
2565 for (fns = TREE_VALUE (conv_fns); fns; fns = OVL_NEXT (fns))
2567 tree fn = OVL_CURRENT (fns);
2569 /* [over.match.funcs] For conversion functions, the function
2570 is considered to be a member of the class of the implicit
2571 object argument for the purpose of defining the type of
2572 the implicit object parameter.
2574 So we pass fromtype as CTYPE to add_*_candidate. */
2576 if (TREE_CODE (fn) == TEMPLATE_DECL)
2577 cand = add_template_candidate (&candidates, fn, fromtype,
2578 NULL_TREE,
2579 args, totype,
2580 TYPE_BINFO (fromtype),
2581 conversion_path,
2582 flags,
2583 DEDUCE_CONV);
2584 else
2585 cand = add_function_candidate (&candidates, fn, fromtype,
2586 args,
2587 TYPE_BINFO (fromtype),
2588 conversion_path,
2589 flags);
2591 if (cand)
2593 conversion *ics
2594 = implicit_conversion (totype,
2595 TREE_TYPE (TREE_TYPE (cand->fn)),
2596 0, convflags);
2598 cand->second_conv = ics;
2600 if (!ics)
2601 cand->viable = 0;
2602 else if (candidates->viable == 1 && ics->bad_p)
2603 cand->viable = -1;
2608 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2609 if (!any_viable_p)
2610 return 0;
2612 cand = tourney (candidates);
2613 if (cand == 0)
2615 if (flags & LOOKUP_COMPLAIN)
2617 error ("conversion from `%T' to `%T' is ambiguous",
2618 fromtype, totype);
2619 print_z_candidates (candidates);
2622 cand = candidates; /* any one will do */
2623 cand->second_conv = build_ambiguous_conv (totype, expr);
2624 cand->second_conv->user_conv_p = true;
2625 if (!any_strictly_viable (candidates))
2626 cand->second_conv->bad_p = true;
2627 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2628 ambiguous conversion is no worse than another user-defined
2629 conversion. */
2631 return cand;
2634 /* Build the user conversion sequence. */
2635 conv = build_conv
2636 (ck_user,
2637 (DECL_CONSTRUCTOR_P (cand->fn)
2638 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2639 build_identity_conv (TREE_TYPE (expr), expr));
2640 conv->cand = cand;
2642 /* Combine it with the second conversion sequence. */
2643 cand->second_conv = merge_conversion_sequences (conv,
2644 cand->second_conv);
2646 if (cand->viable == -1)
2647 cand->second_conv->bad_p = true;
2649 return cand;
2652 tree
2653 build_user_type_conversion (tree totype, tree expr, int flags)
2655 struct z_candidate *cand
2656 = build_user_type_conversion_1 (totype, expr, flags);
2658 if (cand)
2660 if (cand->second_conv->kind == ck_ambig)
2661 return error_mark_node;
2662 return convert_from_reference (convert_like (cand->second_conv, expr));
2664 return NULL_TREE;
2667 /* Do any initial processing on the arguments to a function call. */
2669 static tree
2670 resolve_args (tree args)
2672 tree t;
2673 for (t = args; t; t = TREE_CHAIN (t))
2675 tree arg = TREE_VALUE (t);
2677 if (arg == error_mark_node)
2678 return error_mark_node;
2679 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2681 error ("invalid use of void expression");
2682 return error_mark_node;
2684 arg = convert_from_reference (arg);
2685 TREE_VALUE (t) = arg;
2687 return args;
2690 /* Perform overload resolution on FN, which is called with the ARGS.
2692 Return the candidate function selected by overload resolution, or
2693 NULL if the event that overload resolution failed. In the case
2694 that overload resolution fails, *CANDIDATES will be the set of
2695 candidates considered, and ANY_VIABLE_P will be set to true or
2696 false to indicate whether or not any of the candidates were
2697 viable.
2699 The ARGS should already have gone through RESOLVE_ARGS before this
2700 function is called. */
2702 static struct z_candidate *
2703 perform_overload_resolution (tree fn,
2704 tree args,
2705 struct z_candidate **candidates,
2706 bool *any_viable_p)
2708 struct z_candidate *cand;
2709 tree explicit_targs = NULL_TREE;
2710 int template_only = 0;
2712 *candidates = NULL;
2713 *any_viable_p = true;
2715 /* Check FN and ARGS. */
2716 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2717 || TREE_CODE (fn) == TEMPLATE_DECL
2718 || TREE_CODE (fn) == OVERLOAD
2719 || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2720 20020712);
2721 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2722 20020712);
2724 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2726 explicit_targs = TREE_OPERAND (fn, 1);
2727 fn = TREE_OPERAND (fn, 0);
2728 template_only = 1;
2731 /* Add the various candidate functions. */
2732 add_candidates (fn, args, explicit_targs, template_only,
2733 /*conversion_path=*/NULL_TREE,
2734 /*access_path=*/NULL_TREE,
2735 LOOKUP_NORMAL,
2736 candidates);
2738 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
2739 if (!*any_viable_p)
2740 return NULL;
2742 cand = tourney (*candidates);
2743 return cand;
2746 /* Return an expression for a call to FN (a namespace-scope function,
2747 or a static member function) with the ARGS. */
2749 tree
2750 build_new_function_call (tree fn, tree args)
2752 struct z_candidate *candidates, *cand;
2753 bool any_viable_p;
2754 void *p;
2755 tree result;
2757 args = resolve_args (args);
2758 if (args == error_mark_node)
2759 return error_mark_node;
2761 /* Get the high-water mark for the CONVERSION_OBSTACK. */
2762 p = conversion_obstack_alloc (0);
2764 cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p);
2766 if (!cand)
2768 if (!any_viable_p && candidates && ! candidates->next)
2769 return build_function_call (candidates->fn, args);
2770 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2771 fn = TREE_OPERAND (fn, 0);
2772 if (!any_viable_p)
2773 error ("no matching function for call to `%D(%A)'",
2774 DECL_NAME (OVL_CURRENT (fn)), args);
2775 else
2776 error ("call of overloaded `%D(%A)' is ambiguous",
2777 DECL_NAME (OVL_CURRENT (fn)), args);
2778 if (candidates)
2779 print_z_candidates (candidates);
2780 result = error_mark_node;
2782 else
2783 result = build_over_call (cand, LOOKUP_NORMAL);
2785 /* Free all the conversions we allocated. */
2786 obstack_free (&conversion_obstack, p);
2788 return result;
2791 /* Build a call to a global operator new. FNNAME is the name of the
2792 operator (either "operator new" or "operator new[]") and ARGS are
2793 the arguments provided. *SIZE points to the total number of bytes
2794 required by the allocation, and is updated if that is changed here.
2795 *COOKIE_SIZE is non-NULL if a cookie should be used. If this
2796 function determines that no cookie should be used, after all,
2797 *COOKIE_SIZE is set to NULL_TREE. */
2799 tree
2800 build_operator_new_call (tree fnname, tree args, tree *size, tree *cookie_size)
2802 tree fns;
2803 struct z_candidate *candidates;
2804 struct z_candidate *cand;
2805 bool any_viable_p;
2807 args = tree_cons (NULL_TREE, *size, args);
2808 args = resolve_args (args);
2809 if (args == error_mark_node)
2810 return args;
2812 fns = lookup_function_nonclass (fnname, args);
2814 /* Figure out what function is being called. */
2815 cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p);
2817 /* If no suitable function could be found, issue an error message
2818 and give up. */
2819 if (!cand)
2821 if (!any_viable_p)
2822 error ("no matching function for call to `%D(%A)'",
2823 DECL_NAME (OVL_CURRENT (fns)), args);
2824 else
2825 error ("call of overloaded `%D(%A)' is ambiguous",
2826 DECL_NAME (OVL_CURRENT (fns)), args);
2827 if (candidates)
2828 print_z_candidates (candidates);
2829 return error_mark_node;
2832 /* If a cookie is required, add some extra space. Whether
2833 or not a cookie is required cannot be determined until
2834 after we know which function was called. */
2835 if (*cookie_size)
2837 bool use_cookie = true;
2838 if (!abi_version_at_least (2))
2840 tree placement = TREE_CHAIN (args);
2841 /* In G++ 3.2, the check was implemented incorrectly; it
2842 looked at the placement expression, rather than the
2843 type of the function. */
2844 if (placement && !TREE_CHAIN (placement)
2845 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2846 ptr_type_node))
2847 use_cookie = false;
2849 else
2851 tree arg_types;
2853 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
2854 /* Skip the size_t parameter. */
2855 arg_types = TREE_CHAIN (arg_types);
2856 /* Check the remaining parameters (if any). */
2857 if (arg_types
2858 && TREE_CHAIN (arg_types) == void_list_node
2859 && same_type_p (TREE_VALUE (arg_types),
2860 ptr_type_node))
2861 use_cookie = false;
2863 /* If we need a cookie, adjust the number of bytes allocated. */
2864 if (use_cookie)
2866 /* Update the total size. */
2867 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
2868 /* Update the argument list to reflect the adjusted size. */
2869 TREE_VALUE (args) = *size;
2871 else
2872 *cookie_size = NULL_TREE;
2875 /* Build the CALL_EXPR. */
2876 return build_over_call (cand, LOOKUP_NORMAL);
2879 static tree
2880 build_object_call (tree obj, tree args)
2882 struct z_candidate *candidates = 0, *cand;
2883 tree fns, convs, mem_args = NULL_TREE;
2884 tree type = TREE_TYPE (obj);
2885 bool any_viable_p;
2886 tree result = NULL_TREE;
2887 void *p;
2889 if (TYPE_PTRMEMFUNC_P (type))
2891 /* It's no good looking for an overloaded operator() on a
2892 pointer-to-member-function. */
2893 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2894 return error_mark_node;
2897 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2898 if (fns == error_mark_node)
2899 return error_mark_node;
2901 args = resolve_args (args);
2903 if (args == error_mark_node)
2904 return error_mark_node;
2906 /* Get the high-water mark for the CONVERSION_OBSTACK. */
2907 p = conversion_obstack_alloc (0);
2909 if (fns)
2911 tree base = BINFO_TYPE (BASELINK_BINFO (fns));
2912 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2914 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2916 tree fn = OVL_CURRENT (fns);
2917 if (TREE_CODE (fn) == TEMPLATE_DECL)
2918 add_template_candidate (&candidates, fn, base, NULL_TREE,
2919 mem_args, NULL_TREE,
2920 TYPE_BINFO (type),
2921 TYPE_BINFO (type),
2922 LOOKUP_NORMAL, DEDUCE_CALL);
2923 else
2924 add_function_candidate
2925 (&candidates, fn, base, mem_args, TYPE_BINFO (type),
2926 TYPE_BINFO (type), LOOKUP_NORMAL);
2930 convs = lookup_conversions (type);
2932 for (; convs; convs = TREE_CHAIN (convs))
2934 tree fns = TREE_VALUE (convs);
2935 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2937 if ((TREE_CODE (totype) == POINTER_TYPE
2938 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2939 || (TREE_CODE (totype) == REFERENCE_TYPE
2940 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2941 || (TREE_CODE (totype) == REFERENCE_TYPE
2942 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2943 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2944 for (; fns; fns = OVL_NEXT (fns))
2946 tree fn = OVL_CURRENT (fns);
2947 if (TREE_CODE (fn) == TEMPLATE_DECL)
2948 add_template_conv_candidate
2949 (&candidates, fn, obj, args, totype,
2950 /*access_path=*/NULL_TREE,
2951 /*conversion_path=*/NULL_TREE);
2952 else
2953 add_conv_candidate (&candidates, fn, obj, args,
2954 /*conversion_path=*/NULL_TREE,
2955 /*access_path=*/NULL_TREE);
2959 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2960 if (!any_viable_p)
2962 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2963 print_z_candidates (candidates);
2964 result = error_mark_node;
2966 else
2968 cand = tourney (candidates);
2969 if (cand == 0)
2971 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2972 print_z_candidates (candidates);
2973 result = error_mark_node;
2975 /* Since cand->fn will be a type, not a function, for a conversion
2976 function, we must be careful not to unconditionally look at
2977 DECL_NAME here. */
2978 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
2979 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2980 result = build_over_call (cand, LOOKUP_NORMAL);
2981 else
2983 obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1);
2984 result = build_function_call (obj, args);
2988 /* Free all the conversions we allocated. */
2989 obstack_free (&conversion_obstack, p);
2991 return result;
2994 static void
2995 op_error (enum tree_code code, enum tree_code code2,
2996 tree arg1, tree arg2, tree arg3, const char *problem)
2998 const char *opname;
3000 if (code == MODIFY_EXPR)
3001 opname = assignment_operator_name_info[code2].name;
3002 else
3003 opname = operator_name_info[code].name;
3005 switch (code)
3007 case COND_EXPR:
3008 error ("%s for ternary 'operator?:' in '%E ? %E : %E'",
3009 problem, arg1, arg2, arg3);
3010 break;
3012 case POSTINCREMENT_EXPR:
3013 case POSTDECREMENT_EXPR:
3014 error ("%s for 'operator%s' in '%E%s'", problem, opname, arg1, opname);
3015 break;
3017 case ARRAY_REF:
3018 error ("%s for 'operator[]' in '%E[%E]'", problem, arg1, arg2);
3019 break;
3021 case REALPART_EXPR:
3022 case IMAGPART_EXPR:
3023 error ("%s for '%s' in '%s %E'", problem, opname, opname, arg1);
3024 break;
3026 default:
3027 if (arg2)
3028 error ("%s for 'operator%s' in '%E %s %E'",
3029 problem, opname, arg1, opname, arg2);
3030 else
3031 error ("%s for 'operator%s' in '%s%E'",
3032 problem, opname, opname, arg1);
3033 break;
3037 /* Return the implicit conversion sequence that could be used to
3038 convert E1 to E2 in [expr.cond]. */
3040 static conversion *
3041 conditional_conversion (tree e1, tree e2)
3043 tree t1 = non_reference (TREE_TYPE (e1));
3044 tree t2 = non_reference (TREE_TYPE (e2));
3045 conversion *conv;
3046 bool good_base;
3048 /* [expr.cond]
3050 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
3051 implicitly converted (clause _conv_) to the type "reference to
3052 T2", subject to the constraint that in the conversion the
3053 reference must bind directly (_dcl.init.ref_) to E1. */
3054 if (real_lvalue_p (e2))
3056 conv = implicit_conversion (build_reference_type (t2),
3059 LOOKUP_NO_TEMP_BIND);
3060 if (conv)
3061 return conv;
3064 /* [expr.cond]
3066 If E1 and E2 have class type, and the underlying class types are
3067 the same or one is a base class of the other: E1 can be converted
3068 to match E2 if the class of T2 is the same type as, or a base
3069 class of, the class of T1, and the cv-qualification of T2 is the
3070 same cv-qualification as, or a greater cv-qualification than, the
3071 cv-qualification of T1. If the conversion is applied, E1 is
3072 changed to an rvalue of type T2 that still refers to the original
3073 source class object (or the appropriate subobject thereof). */
3074 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
3075 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
3077 if (good_base && at_least_as_qualified_p (t2, t1))
3079 conv = build_identity_conv (t1, e1);
3080 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
3081 TYPE_MAIN_VARIANT (t2)))
3082 conv = build_conv (ck_base, t2, conv);
3083 else
3084 conv = build_conv (ck_rvalue, t2, conv);
3085 return conv;
3087 else
3088 return NULL;
3090 else
3091 /* [expr.cond]
3093 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
3094 converted to the type that expression E2 would have if E2 were
3095 converted to an rvalue (or the type it has, if E2 is an rvalue). */
3096 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
3099 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
3100 arguments to the conditional expression. */
3102 tree
3103 build_conditional_expr (tree arg1, tree arg2, tree arg3)
3105 tree arg2_type;
3106 tree arg3_type;
3107 tree result = NULL_TREE;
3108 tree result_type = NULL_TREE;
3109 bool lvalue_p = true;
3110 struct z_candidate *candidates = 0;
3111 struct z_candidate *cand;
3112 void *p;
3114 /* As a G++ extension, the second argument to the conditional can be
3115 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
3116 c'.) If the second operand is omitted, make sure it is
3117 calculated only once. */
3118 if (!arg2)
3120 if (pedantic)
3121 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
3123 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
3124 if (real_lvalue_p (arg1))
3125 arg2 = arg1 = stabilize_reference (arg1);
3126 else
3127 arg2 = arg1 = save_expr (arg1);
3130 /* [expr.cond]
3132 The first expr ession is implicitly converted to bool (clause
3133 _conv_). */
3134 arg1 = perform_implicit_conversion (boolean_type_node, arg1);
3136 /* If something has already gone wrong, just pass that fact up the
3137 tree. */
3138 if (error_operand_p (arg1)
3139 || error_operand_p (arg2)
3140 || error_operand_p (arg3))
3141 return error_mark_node;
3143 /* [expr.cond]
3145 If either the second or the third operand has type (possibly
3146 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3147 array-to-pointer (_conv.array_), and function-to-pointer
3148 (_conv.func_) standard conversions are performed on the second
3149 and third operands. */
3150 arg2_type = TREE_TYPE (arg2);
3151 arg3_type = TREE_TYPE (arg3);
3152 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3154 /* Do the conversions. We don't these for `void' type arguments
3155 since it can't have any effect and since decay_conversion
3156 does not handle that case gracefully. */
3157 if (!VOID_TYPE_P (arg2_type))
3158 arg2 = decay_conversion (arg2);
3159 if (!VOID_TYPE_P (arg3_type))
3160 arg3 = decay_conversion (arg3);
3161 arg2_type = TREE_TYPE (arg2);
3162 arg3_type = TREE_TYPE (arg3);
3164 /* [expr.cond]
3166 One of the following shall hold:
3168 --The second or the third operand (but not both) is a
3169 throw-expression (_except.throw_); the result is of the
3170 type of the other and is an rvalue.
3172 --Both the second and the third operands have type void; the
3173 result is of type void and is an rvalue.
3175 We must avoid calling force_rvalue for expressions of type
3176 "void" because it will complain that their value is being
3177 used. */
3178 if (TREE_CODE (arg2) == THROW_EXPR
3179 && TREE_CODE (arg3) != THROW_EXPR)
3181 if (!VOID_TYPE_P (arg3_type))
3182 arg3 = force_rvalue (arg3);
3183 arg3_type = TREE_TYPE (arg3);
3184 result_type = arg3_type;
3186 else if (TREE_CODE (arg2) != THROW_EXPR
3187 && TREE_CODE (arg3) == THROW_EXPR)
3189 if (!VOID_TYPE_P (arg2_type))
3190 arg2 = force_rvalue (arg2);
3191 arg2_type = TREE_TYPE (arg2);
3192 result_type = arg2_type;
3194 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3195 result_type = void_type_node;
3196 else
3198 error ("`%E' has type `void' and is not a throw-expression",
3199 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3200 return error_mark_node;
3203 lvalue_p = false;
3204 goto valid_operands;
3206 /* [expr.cond]
3208 Otherwise, if the second and third operand have different types,
3209 and either has (possibly cv-qualified) class type, an attempt is
3210 made to convert each of those operands to the type of the other. */
3211 else if (!same_type_p (arg2_type, arg3_type)
3212 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3214 conversion *conv2;
3215 conversion *conv3;
3217 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3218 p = conversion_obstack_alloc (0);
3220 conv2 = conditional_conversion (arg2, arg3);
3221 conv3 = conditional_conversion (arg3, arg2);
3223 /* [expr.cond]
3225 If both can be converted, or one can be converted but the
3226 conversion is ambiguous, the program is ill-formed. If
3227 neither can be converted, the operands are left unchanged and
3228 further checking is performed as described below. If exactly
3229 one conversion is possible, that conversion is applied to the
3230 chosen operand and the converted operand is used in place of
3231 the original operand for the remainder of this section. */
3232 if ((conv2 && !conv2->bad_p
3233 && conv3 && !conv3->bad_p)
3234 || (conv2 && conv2->kind == ck_ambig)
3235 || (conv3 && conv3->kind == ck_ambig))
3237 error ("operands to ?: have different types");
3238 result = error_mark_node;
3240 else if (conv2 && !conv2->bad_p)
3242 arg2 = convert_like (conv2, arg2);
3243 arg2 = convert_from_reference (arg2);
3244 arg2_type = TREE_TYPE (arg2);
3246 else if (conv3 && !conv3->bad_p)
3248 arg3 = convert_like (conv3, arg3);
3249 arg3 = convert_from_reference (arg3);
3250 arg3_type = TREE_TYPE (arg3);
3253 /* Free all the conversions we allocated. */
3254 obstack_free (&conversion_obstack, p);
3256 if (result)
3257 return result;
3259 /* If, after the conversion, both operands have class type,
3260 treat the cv-qualification of both operands as if it were the
3261 union of the cv-qualification of the operands.
3263 The standard is not clear about what to do in this
3264 circumstance. For example, if the first operand has type
3265 "const X" and the second operand has a user-defined
3266 conversion to "volatile X", what is the type of the second
3267 operand after this step? Making it be "const X" (matching
3268 the first operand) seems wrong, as that discards the
3269 qualification without actually performing a copy. Leaving it
3270 as "volatile X" seems wrong as that will result in the
3271 conditional expression failing altogether, even though,
3272 according to this step, the one operand could be converted to
3273 the type of the other. */
3274 if ((conv2 || conv3)
3275 && CLASS_TYPE_P (arg2_type)
3276 && TYPE_QUALS (arg2_type) != TYPE_QUALS (arg3_type))
3277 arg2_type = arg3_type =
3278 cp_build_qualified_type (arg2_type,
3279 TYPE_QUALS (arg2_type)
3280 | TYPE_QUALS (arg3_type));
3283 /* [expr.cond]
3285 If the second and third operands are lvalues and have the same
3286 type, the result is of that type and is an lvalue. */
3287 if (real_lvalue_p (arg2)
3288 && real_lvalue_p (arg3)
3289 && same_type_p (arg2_type, arg3_type))
3291 result_type = arg2_type;
3292 goto valid_operands;
3295 /* [expr.cond]
3297 Otherwise, the result is an rvalue. If the second and third
3298 operand do not have the same type, and either has (possibly
3299 cv-qualified) class type, overload resolution is used to
3300 determine the conversions (if any) to be applied to the operands
3301 (_over.match.oper_, _over.built_). */
3302 lvalue_p = false;
3303 if (!same_type_p (arg2_type, arg3_type)
3304 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3306 tree args[3];
3307 conversion *conv;
3308 bool any_viable_p;
3310 /* Rearrange the arguments so that add_builtin_candidate only has
3311 to know about two args. In build_builtin_candidates, the
3312 arguments are unscrambled. */
3313 args[0] = arg2;
3314 args[1] = arg3;
3315 args[2] = arg1;
3316 add_builtin_candidates (&candidates,
3317 COND_EXPR,
3318 NOP_EXPR,
3319 ansi_opname (COND_EXPR),
3320 args,
3321 LOOKUP_NORMAL);
3323 /* [expr.cond]
3325 If the overload resolution fails, the program is
3326 ill-formed. */
3327 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3328 if (!any_viable_p)
3330 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3331 print_z_candidates (candidates);
3332 return error_mark_node;
3334 cand = tourney (candidates);
3335 if (!cand)
3337 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3338 print_z_candidates (candidates);
3339 return error_mark_node;
3342 /* [expr.cond]
3344 Otherwise, the conversions thus determined are applied, and
3345 the converted operands are used in place of the original
3346 operands for the remainder of this section. */
3347 conv = cand->convs[0];
3348 arg1 = convert_like (conv, arg1);
3349 conv = cand->convs[1];
3350 arg2 = convert_like (conv, arg2);
3351 conv = cand->convs[2];
3352 arg3 = convert_like (conv, arg3);
3355 /* [expr.cond]
3357 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3358 and function-to-pointer (_conv.func_) standard conversions are
3359 performed on the second and third operands.
3361 We need to force the lvalue-to-rvalue conversion here for class types,
3362 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3363 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3364 regions. */
3366 arg2 = force_rvalue (arg2);
3367 if (!CLASS_TYPE_P (arg2_type))
3368 arg2_type = TREE_TYPE (arg2);
3370 arg3 = force_rvalue (arg3);
3371 if (!CLASS_TYPE_P (arg2_type))
3372 arg3_type = TREE_TYPE (arg3);
3374 if (arg2 == error_mark_node || arg3 == error_mark_node)
3375 return error_mark_node;
3377 /* [expr.cond]
3379 After those conversions, one of the following shall hold:
3381 --The second and third operands have the same type; the result is of
3382 that type. */
3383 if (same_type_p (arg2_type, arg3_type))
3384 result_type = arg2_type;
3385 /* [expr.cond]
3387 --The second and third operands have arithmetic or enumeration
3388 type; the usual arithmetic conversions are performed to bring
3389 them to a common type, and the result is of that type. */
3390 else if ((ARITHMETIC_TYPE_P (arg2_type)
3391 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3392 && (ARITHMETIC_TYPE_P (arg3_type)
3393 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3395 /* In this case, there is always a common type. */
3396 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3397 arg3_type);
3399 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3400 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3401 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3402 arg2_type, arg3_type);
3403 else if (extra_warnings
3404 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3405 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3406 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3407 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3408 warning ("enumeral and non-enumeral type in conditional expression");
3410 arg2 = perform_implicit_conversion (result_type, arg2);
3411 arg3 = perform_implicit_conversion (result_type, arg3);
3413 /* [expr.cond]
3415 --The second and third operands have pointer type, or one has
3416 pointer type and the other is a null pointer constant; pointer
3417 conversions (_conv.ptr_) and qualification conversions
3418 (_conv.qual_) are performed to bring them to their composite
3419 pointer type (_expr.rel_). The result is of the composite
3420 pointer type.
3422 --The second and third operands have pointer to member type, or
3423 one has pointer to member type and the other is a null pointer
3424 constant; pointer to member conversions (_conv.mem_) and
3425 qualification conversions (_conv.qual_) are performed to bring
3426 them to a common type, whose cv-qualification shall match the
3427 cv-qualification of either the second or the third operand.
3428 The result is of the common type. */
3429 else if ((null_ptr_cst_p (arg2)
3430 && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
3431 || (null_ptr_cst_p (arg3)
3432 && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
3433 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3434 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3435 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
3437 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3438 arg3, "conditional expression");
3439 if (result_type == error_mark_node)
3440 return error_mark_node;
3441 arg2 = perform_implicit_conversion (result_type, arg2);
3442 arg3 = perform_implicit_conversion (result_type, arg3);
3445 if (!result_type)
3447 error ("operands to ?: have different types");
3448 return error_mark_node;
3451 valid_operands:
3452 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3453 /* We can't use result_type below, as fold might have returned a
3454 throw_expr. */
3456 /* Expand both sides into the same slot, hopefully the target of the
3457 ?: expression. We used to check for TARGET_EXPRs here, but now we
3458 sometimes wrap them in NOP_EXPRs so the test would fail. */
3459 if (!lvalue_p && CLASS_TYPE_P (TREE_TYPE (result)))
3460 result = get_target_expr (result);
3462 /* If this expression is an rvalue, but might be mistaken for an
3463 lvalue, we must add a NON_LVALUE_EXPR. */
3464 if (!lvalue_p && real_lvalue_p (result))
3465 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
3467 return result;
3470 /* OPERAND is an operand to an expression. Perform necessary steps
3471 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
3472 returned. */
3474 static tree
3475 prep_operand (tree operand)
3477 if (operand)
3479 operand = convert_from_reference (operand);
3480 if (CLASS_TYPE_P (TREE_TYPE (operand))
3481 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
3482 /* Make sure the template type is instantiated now. */
3483 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
3486 return operand;
3489 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
3490 OVERLOAD) to the CANDIDATES, returning an updated list of
3491 CANDIDATES. The ARGS are the arguments provided to the call,
3492 without any implicit object parameter. The EXPLICIT_TARGS are
3493 explicit template arguments provided. TEMPLATE_ONLY is true if
3494 only template functions should be considered. CONVERSION_PATH,
3495 ACCESS_PATH, and FLAGS are as for add_function_candidate. */
3497 static void
3498 add_candidates (tree fns, tree args,
3499 tree explicit_targs, bool template_only,
3500 tree conversion_path, tree access_path,
3501 int flags,
3502 struct z_candidate **candidates)
3504 tree ctype;
3505 tree non_static_args;
3507 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
3508 /* Delay creating the implicit this parameter until it is needed. */
3509 non_static_args = NULL_TREE;
3511 while (fns)
3513 tree fn;
3514 tree fn_args;
3516 fn = OVL_CURRENT (fns);
3517 /* Figure out which set of arguments to use. */
3518 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3520 /* If this function is a non-static member, prepend the implicit
3521 object parameter. */
3522 if (!non_static_args)
3523 non_static_args = tree_cons (NULL_TREE,
3524 build_this (TREE_VALUE (args)),
3525 TREE_CHAIN (args));
3526 fn_args = non_static_args;
3528 else
3529 /* Otherwise, just use the list of arguments provided. */
3530 fn_args = args;
3532 if (TREE_CODE (fn) == TEMPLATE_DECL)
3533 add_template_candidate (candidates,
3534 fn,
3535 ctype,
3536 explicit_targs,
3537 fn_args,
3538 NULL_TREE,
3539 access_path,
3540 conversion_path,
3541 flags,
3542 DEDUCE_CALL);
3543 else if (!template_only)
3544 add_function_candidate (candidates,
3546 ctype,
3547 fn_args,
3548 access_path,
3549 conversion_path,
3550 flags);
3551 fns = OVL_NEXT (fns);
3555 tree
3556 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
3557 bool *overloaded_p)
3559 struct z_candidate *candidates = 0, *cand;
3560 tree arglist, fnname;
3561 tree args[3];
3562 tree result = NULL_TREE;
3563 bool result_valid_p = false;
3564 enum tree_code code2 = NOP_EXPR;
3565 conversion *conv;
3566 void *p;
3567 bool strict_p;
3568 bool any_viable_p;
3570 if (error_operand_p (arg1)
3571 || error_operand_p (arg2)
3572 || error_operand_p (arg3))
3573 return error_mark_node;
3575 if (code == MODIFY_EXPR)
3577 code2 = TREE_CODE (arg3);
3578 arg3 = NULL_TREE;
3579 fnname = ansi_assopname (code2);
3581 else
3582 fnname = ansi_opname (code);
3584 arg1 = prep_operand (arg1);
3586 switch (code)
3588 case NEW_EXPR:
3589 case VEC_NEW_EXPR:
3590 case VEC_DELETE_EXPR:
3591 case DELETE_EXPR:
3592 /* Use build_op_new_call and build_op_delete_call instead. */
3593 abort ();
3595 case CALL_EXPR:
3596 return build_object_call (arg1, arg2);
3598 default:
3599 break;
3602 arg2 = prep_operand (arg2);
3603 arg3 = prep_operand (arg3);
3605 if (code == COND_EXPR)
3607 if (arg2 == NULL_TREE
3608 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3609 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3610 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3611 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3612 goto builtin;
3614 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3615 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3616 goto builtin;
3618 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3619 arg2 = integer_zero_node;
3621 arglist = NULL_TREE;
3622 if (arg3)
3623 arglist = tree_cons (NULL_TREE, arg3, arglist);
3624 if (arg2)
3625 arglist = tree_cons (NULL_TREE, arg2, arglist);
3626 arglist = tree_cons (NULL_TREE, arg1, arglist);
3628 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3629 p = conversion_obstack_alloc (0);
3631 /* Add namespace-scope operators to the list of functions to
3632 consider. */
3633 add_candidates (lookup_function_nonclass (fnname, arglist),
3634 arglist, NULL_TREE, false, NULL_TREE, NULL_TREE,
3635 flags, &candidates);
3636 /* Add class-member operators to the candidate set. */
3637 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
3639 tree fns;
3641 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3642 if (fns == error_mark_node)
3644 result = error_mark_node;
3645 goto user_defined_result_ready;
3647 if (fns)
3648 add_candidates (BASELINK_FUNCTIONS (fns), arglist,
3649 NULL_TREE, false,
3650 BASELINK_BINFO (fns),
3651 TYPE_BINFO (TREE_TYPE (arg1)),
3652 flags, &candidates);
3655 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3656 to know about two args; a builtin candidate will always have a first
3657 parameter of type bool. We'll handle that in
3658 build_builtin_candidate. */
3659 if (code == COND_EXPR)
3661 args[0] = arg2;
3662 args[1] = arg3;
3663 args[2] = arg1;
3665 else
3667 args[0] = arg1;
3668 args[1] = arg2;
3669 args[2] = NULL_TREE;
3672 add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
3674 switch (code)
3676 case COMPOUND_EXPR:
3677 case ADDR_EXPR:
3678 /* For these, the built-in candidates set is empty
3679 [over.match.oper]/3. We don't want non-strict matches
3680 because exact matches are always possible with built-in
3681 operators. The built-in candidate set for COMPONENT_REF
3682 would be empty too, but since there are no such built-in
3683 operators, we accept non-strict matches for them. */
3684 strict_p = true;
3685 break;
3687 default:
3688 strict_p = pedantic;
3689 break;
3692 candidates = splice_viable (candidates, strict_p, &any_viable_p);
3693 if (!any_viable_p)
3695 switch (code)
3697 case POSTINCREMENT_EXPR:
3698 case POSTDECREMENT_EXPR:
3699 /* Look for an `operator++ (int)'. If they didn't have
3700 one, then we fall back to the old way of doing things. */
3701 if (flags & LOOKUP_COMPLAIN)
3702 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3703 fnname,
3704 operator_name_info[code].name);
3705 if (code == POSTINCREMENT_EXPR)
3706 code = PREINCREMENT_EXPR;
3707 else
3708 code = PREDECREMENT_EXPR;
3709 result = build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE,
3710 overloaded_p);
3711 break;
3713 /* The caller will deal with these. */
3714 case ADDR_EXPR:
3715 case COMPOUND_EXPR:
3716 case COMPONENT_REF:
3717 result = NULL_TREE;
3718 result_valid_p = true;
3719 break;
3721 default:
3722 if (flags & LOOKUP_COMPLAIN)
3724 op_error (code, code2, arg1, arg2, arg3, "no match");
3725 print_z_candidates (candidates);
3727 result = error_mark_node;
3728 break;
3731 else
3733 cand = tourney (candidates);
3734 if (cand == 0)
3736 if (flags & LOOKUP_COMPLAIN)
3738 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3739 print_z_candidates (candidates);
3741 result = error_mark_node;
3743 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3745 if (overloaded_p)
3746 *overloaded_p = true;
3748 if (warn_synth
3749 && fnname == ansi_assopname (NOP_EXPR)
3750 && DECL_ARTIFICIAL (cand->fn)
3751 && candidates->next
3752 && ! candidates->next->next)
3754 warning ("using synthesized `%#D' for copy assignment",
3755 cand->fn);
3756 cp_warning_at (" where cfront would use `%#D'",
3757 cand == candidates
3758 ? candidates->next->fn
3759 : candidates->fn);
3762 result = build_over_call (cand, LOOKUP_NORMAL);
3764 else
3766 /* Check for comparison of different enum types. */
3767 switch (code)
3769 case GT_EXPR:
3770 case LT_EXPR:
3771 case GE_EXPR:
3772 case LE_EXPR:
3773 case EQ_EXPR:
3774 case NE_EXPR:
3775 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3776 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3777 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3778 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3780 warning ("comparison between `%#T' and `%#T'",
3781 TREE_TYPE (arg1), TREE_TYPE (arg2));
3783 break;
3784 default:
3785 break;
3788 /* We need to strip any leading REF_BIND so that bitfields
3789 don't cause errors. This should not remove any important
3790 conversions, because builtins don't apply to class
3791 objects directly. */
3792 conv = cand->convs[0];
3793 if (conv->kind == ck_ref_bind)
3794 conv = conv->u.next;
3795 arg1 = convert_like (conv, arg1);
3796 if (arg2)
3798 conv = cand->convs[1];
3799 if (conv->kind == ck_ref_bind)
3800 conv = conv->u.next;
3801 arg2 = convert_like (conv, arg2);
3803 if (arg3)
3805 conv = cand->convs[2];
3806 if (conv->kind == ck_ref_bind)
3807 conv = conv->u.next;
3808 arg3 = convert_like (conv, arg3);
3813 user_defined_result_ready:
3815 /* Free all the conversions we allocated. */
3816 obstack_free (&conversion_obstack, p);
3818 if (result || result_valid_p)
3819 return result;
3821 builtin:
3822 switch (code)
3824 case MODIFY_EXPR:
3825 return build_modify_expr (arg1, code2, arg2);
3827 case INDIRECT_REF:
3828 return build_indirect_ref (arg1, "unary *");
3830 case PLUS_EXPR:
3831 case MINUS_EXPR:
3832 case MULT_EXPR:
3833 case TRUNC_DIV_EXPR:
3834 case GT_EXPR:
3835 case LT_EXPR:
3836 case GE_EXPR:
3837 case LE_EXPR:
3838 case EQ_EXPR:
3839 case NE_EXPR:
3840 case MAX_EXPR:
3841 case MIN_EXPR:
3842 case LSHIFT_EXPR:
3843 case RSHIFT_EXPR:
3844 case TRUNC_MOD_EXPR:
3845 case BIT_AND_EXPR:
3846 case BIT_IOR_EXPR:
3847 case BIT_XOR_EXPR:
3848 case TRUTH_ANDIF_EXPR:
3849 case TRUTH_ORIF_EXPR:
3850 return cp_build_binary_op (code, arg1, arg2);
3852 case CONVERT_EXPR:
3853 case NEGATE_EXPR:
3854 case BIT_NOT_EXPR:
3855 case TRUTH_NOT_EXPR:
3856 case PREINCREMENT_EXPR:
3857 case POSTINCREMENT_EXPR:
3858 case PREDECREMENT_EXPR:
3859 case POSTDECREMENT_EXPR:
3860 case REALPART_EXPR:
3861 case IMAGPART_EXPR:
3862 return build_unary_op (code, arg1, candidates != 0);
3864 case ARRAY_REF:
3865 return build_array_ref (arg1, arg2);
3867 case COND_EXPR:
3868 return build_conditional_expr (arg1, arg2, arg3);
3870 case MEMBER_REF:
3871 return build_m_component_ref
3872 (build_indirect_ref (arg1, NULL), arg2);
3874 /* The caller will deal with these. */
3875 case ADDR_EXPR:
3876 case COMPONENT_REF:
3877 case COMPOUND_EXPR:
3878 return NULL_TREE;
3880 default:
3881 abort ();
3882 return NULL_TREE;
3886 /* Build a call to operator delete. This has to be handled very specially,
3887 because the restrictions on what signatures match are different from all
3888 other call instances. For a normal delete, only a delete taking (void *)
3889 or (void *, size_t) is accepted. For a placement delete, only an exact
3890 match with the placement new is accepted.
3892 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3893 ADDR is the pointer to be deleted.
3894 SIZE is the size of the memory block to be deleted.
3895 GLOBAL_P is true if the delete-expression should not consider
3896 class-specific delete operators.
3897 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3899 tree
3900 build_op_delete_call (enum tree_code code, tree addr, tree size,
3901 bool global_p, tree placement)
3903 tree fn = NULL_TREE;
3904 tree fns, fnname, argtypes, args, type;
3905 int pass;
3907 if (addr == error_mark_node)
3908 return error_mark_node;
3910 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
3912 fnname = ansi_opname (code);
3914 if (IS_AGGR_TYPE (type) && !global_p)
3915 /* In [class.free]
3917 If the result of the lookup is ambiguous or inaccessible, or if
3918 the lookup selects a placement deallocation function, the
3919 program is ill-formed.
3921 Therefore, we ask lookup_fnfields to complain about ambiguity. */
3923 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3924 if (fns == error_mark_node)
3925 return error_mark_node;
3927 else
3928 fns = NULL_TREE;
3930 if (fns == NULL_TREE)
3931 fns = lookup_name_nonclass (fnname);
3933 if (placement)
3935 tree alloc_fn;
3936 tree call_expr;
3938 /* Find the allocation function that is being called. */
3939 call_expr = placement;
3940 /* Extract the function. */
3941 alloc_fn = get_callee_fndecl (call_expr);
3942 my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3943 /* Then the second parm type. */
3944 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3945 /* Also the second argument. */
3946 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3948 else
3950 /* First try it without the size argument. */
3951 argtypes = void_list_node;
3952 args = NULL_TREE;
3955 /* Strip const and volatile from addr. */
3956 addr = cp_convert (ptr_type_node, addr);
3958 /* We make two tries at finding a matching `operator delete'. On
3959 the first pass, we look for a one-operator (or placement)
3960 operator delete. If we're not doing placement delete, then on
3961 the second pass we look for a two-argument delete. */
3962 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3964 /* Go through the `operator delete' functions looking for one
3965 with a matching type. */
3966 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3967 fn;
3968 fn = OVL_NEXT (fn))
3970 tree t;
3972 /* The first argument must be "void *". */
3973 t = TYPE_ARG_TYPES (TREE_TYPE (OVL_CURRENT (fn)));
3974 if (!same_type_p (TREE_VALUE (t), ptr_type_node))
3975 continue;
3976 t = TREE_CHAIN (t);
3977 /* On the first pass, check the rest of the arguments. */
3978 if (pass == 0)
3980 tree a = argtypes;
3981 while (a && t)
3983 if (!same_type_p (TREE_VALUE (a), TREE_VALUE (t)))
3984 break;
3985 a = TREE_CHAIN (a);
3986 t = TREE_CHAIN (t);
3988 if (!a && !t)
3989 break;
3991 /* On the second pass, the second argument must be
3992 "size_t". */
3993 else if (pass == 1
3994 && same_type_p (TREE_VALUE (t), sizetype)
3995 && TREE_CHAIN (t) == void_list_node)
3996 break;
3999 /* If we found a match, we're done. */
4000 if (fn)
4001 break;
4004 /* If we have a matching function, call it. */
4005 if (fn)
4007 /* Make sure we have the actual function, and not an
4008 OVERLOAD. */
4009 fn = OVL_CURRENT (fn);
4011 /* If the FN is a member function, make sure that it is
4012 accessible. */
4013 if (DECL_CLASS_SCOPE_P (fn))
4014 perform_or_defer_access_check (TYPE_BINFO (type), fn);
4016 if (pass == 0)
4017 args = tree_cons (NULL_TREE, addr, args);
4018 else
4019 args = tree_cons (NULL_TREE, addr,
4020 build_tree_list (NULL_TREE, size));
4022 if (placement)
4024 /* The placement args might not be suitable for overload
4025 resolution at this point, so build the call directly. */
4026 mark_used (fn);
4027 return build_cxx_call (fn, args, args);
4029 else
4030 return build_function_call (fn, args);
4033 /* If we are doing placement delete we do nothing if we don't find a
4034 matching op delete. */
4035 if (placement)
4036 return NULL_TREE;
4038 error ("no suitable `operator %s' for `%T'",
4039 operator_name_info[(int)code].name, type);
4040 return error_mark_node;
4043 /* If the current scope isn't allowed to access DECL along
4044 BASETYPE_PATH, give an error. The most derived class in
4045 BASETYPE_PATH is the one used to qualify DECL. */
4047 bool
4048 enforce_access (tree basetype_path, tree decl)
4050 my_friendly_assert (TREE_CODE (basetype_path) == TREE_VEC, 20030624);
4052 if (!accessible_p (basetype_path, decl))
4054 if (TREE_PRIVATE (decl))
4055 cp_error_at ("`%+#D' is private", decl);
4056 else if (TREE_PROTECTED (decl))
4057 cp_error_at ("`%+#D' is protected", decl);
4058 else
4059 cp_error_at ("`%+#D' is inaccessible", decl);
4060 error ("within this context");
4061 return false;
4064 return true;
4067 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
4068 bitwise or of LOOKUP_* values. If any errors are warnings are
4069 generated, set *DIAGNOSTIC_FN to "error" or "warning",
4070 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
4071 to NULL. */
4073 static tree
4074 build_temp (tree expr, tree type, int flags,
4075 void (**diagnostic_fn)(const char *, ...))
4077 int savew, savee;
4079 savew = warningcount, savee = errorcount;
4080 expr = build_special_member_call (NULL_TREE,
4081 complete_ctor_identifier,
4082 build_tree_list (NULL_TREE, expr),
4083 TYPE_BINFO (type),
4084 flags);
4085 if (warningcount > savew)
4086 *diagnostic_fn = warning;
4087 else if (errorcount > savee)
4088 *diagnostic_fn = error;
4089 else
4090 *diagnostic_fn = NULL;
4091 return expr;
4095 /* Perform the conversions in CONVS on the expression EXPR. FN and
4096 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
4097 indicates the `this' argument of a method. INNER is nonzero when
4098 being called to continue a conversion chain. It is negative when a
4099 reference binding will be applied, positive otherwise. If
4100 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
4101 conversions will be emitted if appropriate. */
4103 static tree
4104 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
4105 int inner, bool issue_conversion_warnings)
4107 tree totype = convs->type;
4108 void (*diagnostic_fn)(const char *, ...);
4110 if (convs->bad_p
4111 && convs->kind != ck_user
4112 && convs->kind != ck_ambig
4113 && convs->kind != ck_ref_bind)
4115 conversion *t = convs;
4116 for (; t; t = convs->u.next)
4118 if (t->kind == ck_user || !t->bad_p)
4120 expr = convert_like_real (t, expr, fn, argnum, 1,
4121 /*issue_conversion_warnings=*/false);
4122 break;
4124 else if (t->kind == ck_ambig)
4125 return convert_like_real (t, expr, fn, argnum, 1,
4126 /*issue_conversion_warnings=*/false);
4127 else if (t->kind == ck_identity)
4128 break;
4130 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
4131 if (fn)
4132 pedwarn (" initializing argument %P of `%D'", argnum, fn);
4133 return cp_convert (totype, expr);
4136 if (issue_conversion_warnings)
4137 expr = dubious_conversion_warnings
4138 (totype, expr, "converting", fn, argnum);
4139 switch (convs->kind)
4141 case ck_user:
4143 struct z_candidate *cand = convs->cand;
4144 tree convfn = cand->fn;
4145 tree args;
4147 if (DECL_CONSTRUCTOR_P (convfn))
4149 tree t = build_int_2 (0, 0);
4150 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
4152 args = build_tree_list (NULL_TREE, expr);
4153 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
4154 || DECL_HAS_VTT_PARM_P (convfn))
4155 /* We should never try to call the abstract or base constructor
4156 from here. */
4157 abort ();
4158 args = tree_cons (NULL_TREE, t, args);
4160 else
4161 args = build_this (expr);
4162 expr = build_over_call (cand, LOOKUP_NORMAL);
4164 /* If this is a constructor or a function returning an aggr type,
4165 we need to build up a TARGET_EXPR. */
4166 if (DECL_CONSTRUCTOR_P (convfn))
4167 expr = build_cplus_new (totype, expr);
4169 /* The result of the call is then used to direct-initialize the object
4170 that is the destination of the copy-initialization. [dcl.init]
4172 Note that this step is not reflected in the conversion sequence;
4173 it affects the semantics when we actually perform the
4174 conversion, but is not considered during overload resolution.
4176 If the target is a class, that means call a ctor. */
4177 if (IS_AGGR_TYPE (totype)
4178 && (inner >= 0 || !lvalue_p (expr)))
4180 expr = (build_temp
4181 (expr, totype,
4182 /* Core issue 84, now a DR, says that we don't
4183 allow UDCs for these args (which deliberately
4184 breaks copy-init of an auto_ptr<Base> from an
4185 auto_ptr<Derived>). */
4186 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION,
4187 &diagnostic_fn));
4189 if (diagnostic_fn)
4191 if (fn)
4192 diagnostic_fn
4193 (" initializing argument %P of `%D' from result of `%D'",
4194 argnum, fn, convfn);
4195 else
4196 diagnostic_fn
4197 (" initializing temporary from result of `%D'", convfn);
4199 expr = build_cplus_new (totype, expr);
4201 return expr;
4203 case ck_identity:
4204 if (type_unknown_p (expr))
4205 expr = instantiate_type (totype, expr, tf_error | tf_warning);
4206 /* Convert a non-array constant variable to its underlying value, unless we
4207 are about to bind it to a reference, in which case we need to
4208 leave it as an lvalue. */
4209 if (inner >= 0
4210 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
4211 expr = decl_constant_value (expr);
4212 if (convs->check_copy_constructor_p)
4213 /* Generate a temporary copy purely to generate the required
4214 diagnostics. */
4215 build_temp
4216 (build_dummy_object
4217 (build_qualified_type (totype, TYPE_QUAL_CONST)),
4218 totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING, &diagnostic_fn);
4219 return expr;
4220 case ck_ambig:
4221 /* Call build_user_type_conversion again for the error. */
4222 return build_user_type_conversion
4223 (totype, convs->u.expr, LOOKUP_NORMAL);
4225 default:
4226 break;
4229 expr = convert_like_real (convs->u.next, expr, fn, argnum,
4230 convs->kind == ck_ref_bind ? -1 : 1,
4231 /*issue_conversion_warnings=*/false);
4232 if (expr == error_mark_node)
4233 return error_mark_node;
4235 switch (convs->kind)
4237 case ck_rvalue:
4238 if (! IS_AGGR_TYPE (totype))
4239 return expr;
4240 /* Else fall through. */
4241 case ck_base:
4242 if (convs->kind == ck_base && !convs->need_temporary_p)
4244 /* We are going to bind a reference directly to a base-class
4245 subobject of EXPR. */
4246 if (convs->check_copy_constructor_p)
4247 /* Generate a temporary copy purely to generate the required
4248 diagnostics. */
4249 build_temp (build_dummy_object (TREE_TYPE (expr)),
4250 TREE_TYPE (expr),
4251 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
4252 &diagnostic_fn);
4253 /* Build an expression for `*((base*) &expr)'. */
4254 expr = build_unary_op (ADDR_EXPR, expr, 0);
4255 expr = perform_implicit_conversion (build_pointer_type (totype),
4256 expr);
4257 expr = build_indirect_ref (expr, "implicit conversion");
4258 return expr;
4261 /* Copy-initialization where the cv-unqualified version of the source
4262 type is the same class as, or a derived class of, the class of the
4263 destination [is treated as direct-initialization]. [dcl.init] */
4264 expr = build_temp (expr, totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
4265 &diagnostic_fn);
4266 if (diagnostic_fn && fn)
4267 diagnostic_fn (" initializing argument %P of `%D'", argnum, fn);
4268 return build_cplus_new (totype, expr);
4270 case ck_ref_bind:
4272 tree ref_type = totype;
4274 /* If necessary, create a temporary. */
4275 if (convs->need_temporary_p || !lvalue_p (expr))
4277 tree type = convs->u.next->type;
4279 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
4281 /* If the reference is volatile or non-const, we
4282 cannot create a temporary. */
4283 cp_lvalue_kind lvalue = real_lvalue_p (expr);
4285 if (lvalue & clk_bitfield)
4286 error ("cannot bind bitfield `%E' to `%T'",
4287 expr, ref_type);
4288 else if (lvalue & clk_packed)
4289 error ("cannot bind packed field `%E' to `%T'",
4290 expr, ref_type);
4291 else
4292 error ("cannot bind rvalue `%E' to `%T'", expr, ref_type);
4293 return error_mark_node;
4295 expr = build_target_expr_with_type (expr, type);
4298 /* Take the address of the thing to which we will bind the
4299 reference. */
4300 expr = build_unary_op (ADDR_EXPR, expr, 1);
4301 if (expr == error_mark_node)
4302 return error_mark_node;
4304 /* Convert it to a pointer to the type referred to by the
4305 reference. This will adjust the pointer if a derived to
4306 base conversion is being performed. */
4307 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4308 expr);
4309 /* Convert the pointer to the desired reference type. */
4310 return build_nop (ref_type, expr);
4313 case ck_lvalue:
4314 return decay_conversion (expr);
4316 case ck_qual:
4317 /* Warn about deprecated conversion if appropriate. */
4318 string_conv_p (totype, expr, 1);
4319 break;
4321 default:
4322 break;
4324 return ocp_convert (totype, expr, CONV_IMPLICIT,
4325 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4328 /* Build a call to __builtin_trap which can be used as an expression of
4329 type TYPE. */
4331 static tree
4332 call_builtin_trap (tree type)
4334 tree fn = implicit_built_in_decls[BUILT_IN_TRAP];
4336 my_friendly_assert (fn != NULL, 20030927);
4337 fn = build_call (fn, NULL_TREE);
4338 fn = build (COMPOUND_EXPR, type, fn, error_mark_node);
4339 fn = force_target_expr (type, fn);
4340 return fn;
4343 /* ARG is being passed to a varargs function. Perform any conversions
4344 required. Return the converted value. */
4346 tree
4347 convert_arg_to_ellipsis (tree arg)
4349 /* [expr.call]
4351 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
4352 standard conversions are performed. */
4353 arg = decay_conversion (arg);
4354 /* [expr.call]
4356 If the argument has integral or enumeration type that is subject
4357 to the integral promotions (_conv.prom_), or a floating point
4358 type that is subject to the floating point promotion
4359 (_conv.fpprom_), the value of the argument is converted to the
4360 promoted type before the call. */
4361 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4362 && (TYPE_PRECISION (TREE_TYPE (arg))
4363 < TYPE_PRECISION (double_type_node)))
4364 arg = convert_to_real (double_type_node, arg);
4365 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
4366 arg = perform_integral_promotions (arg);
4368 arg = require_complete_type (arg);
4370 if (arg != error_mark_node
4371 && !pod_type_p (TREE_TYPE (arg)))
4373 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
4374 here and do a bitwise copy, but now cp_expr_size will abort if we
4375 try to do that.
4376 If the call appears in the context of a sizeof expression,
4377 there is no need to emit a warning, since the expression won't be
4378 evaluated. We keep the builtin_trap just as a safety check. */
4379 if (!skip_evaluation)
4380 warning ("cannot pass objects of non-POD type `%#T' through `...'; "
4381 "call will abort at runtime", TREE_TYPE (arg));
4382 arg = call_builtin_trap (TREE_TYPE (arg));
4385 return arg;
4388 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4390 tree
4391 build_x_va_arg (tree expr, tree type)
4393 if (processing_template_decl)
4394 return build_min (VA_ARG_EXPR, type, expr);
4396 type = complete_type_or_else (type, NULL_TREE);
4398 if (expr == error_mark_node || !type)
4399 return error_mark_node;
4401 if (! pod_type_p (type))
4403 /* Undefined behavior [expr.call] 5.2.2/7. */
4404 warning ("cannot receive objects of non-POD type `%#T' through `...'; \
4405 call will abort at runtime",
4406 type);
4407 return call_builtin_trap (type);
4410 return build_va_arg (expr, type);
4413 /* TYPE has been given to va_arg. Apply the default conversions which
4414 would have happened when passed via ellipsis. Return the promoted
4415 type, or the passed type if there is no change. */
4417 tree
4418 cxx_type_promotes_to (tree type)
4420 tree promote;
4422 /* Perform the array-to-pointer and function-to-pointer
4423 conversions. */
4424 type = type_decays_to (type);
4426 promote = type_promotes_to (type);
4427 if (same_type_p (type, promote))
4428 promote = type;
4430 return promote;
4433 /* ARG is a default argument expression being passed to a parameter of
4434 the indicated TYPE, which is a parameter to FN. Do any required
4435 conversions. Return the converted value. */
4437 tree
4438 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
4440 /* If the ARG is an unparsed default argument expression, the
4441 conversion cannot be performed. */
4442 if (TREE_CODE (arg) == DEFAULT_ARG)
4444 error ("the default argument for parameter %d of `%D' has "
4445 "not yet been parsed",
4446 parmnum, fn);
4447 return error_mark_node;
4450 if (fn && DECL_TEMPLATE_INFO (fn))
4451 arg = tsubst_default_argument (fn, type, arg);
4453 arg = break_out_target_exprs (arg);
4455 if (TREE_CODE (arg) == CONSTRUCTOR)
4457 arg = digest_init (type, arg, 0);
4458 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4459 "default argument", fn, parmnum);
4461 else
4463 /* This could get clobbered by the following call. */
4464 if (TREE_HAS_CONSTRUCTOR (arg))
4465 arg = copy_node (arg);
4467 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4468 "default argument", fn, parmnum);
4469 arg = convert_for_arg_passing (type, arg);
4472 return arg;
4475 /* Returns the type which will really be used for passing an argument of
4476 type TYPE. */
4478 tree
4479 type_passed_as (tree type)
4481 /* Pass classes with copy ctors by invisible reference. */
4482 if (TREE_ADDRESSABLE (type))
4483 type = build_reference_type (type);
4484 else if (targetm.calls.promote_prototypes (type)
4485 && INTEGRAL_TYPE_P (type)
4486 && COMPLETE_TYPE_P (type)
4487 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4488 TYPE_SIZE (integer_type_node)))
4489 type = integer_type_node;
4491 return type;
4494 /* Actually perform the appropriate conversion. */
4496 tree
4497 convert_for_arg_passing (tree type, tree val)
4499 if (val == error_mark_node)
4501 /* Pass classes with copy ctors by invisible reference. */
4502 else if (TREE_ADDRESSABLE (type))
4503 val = build1 (ADDR_EXPR, build_reference_type (type), val);
4504 else if (targetm.calls.promote_prototypes (type)
4505 && INTEGRAL_TYPE_P (type)
4506 && COMPLETE_TYPE_P (type)
4507 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4508 TYPE_SIZE (integer_type_node)))
4509 val = perform_integral_promotions (val);
4510 return val;
4513 /* Returns true iff FN is a function with magic varargs, i.e. ones for
4514 which no conversions at all should be done. This is true for some
4515 builtins which don't act like normal functions. */
4517 static bool
4518 magic_varargs_p (tree fn)
4520 if (DECL_BUILT_IN (fn))
4521 switch (DECL_FUNCTION_CODE (fn))
4523 case BUILT_IN_CLASSIFY_TYPE:
4524 case BUILT_IN_CONSTANT_P:
4525 case BUILT_IN_NEXT_ARG:
4526 case BUILT_IN_STDARG_START:
4527 case BUILT_IN_VA_START:
4528 return true;
4530 default:;
4533 return false;
4536 /* Subroutine of the various build_*_call functions. Overload resolution
4537 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4538 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4539 bitmask of various LOOKUP_* flags which apply to the call itself. */
4541 static tree
4542 build_over_call (struct z_candidate *cand, int flags)
4544 tree fn = cand->fn;
4545 tree args = cand->args;
4546 conversion **convs = cand->convs;
4547 conversion *conv;
4548 tree converted_args = NULL_TREE;
4549 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4550 tree arg, val;
4551 int i = 0;
4552 int is_method = 0;
4554 /* In a template, there is no need to perform all of the work that
4555 is normally done. We are only interested in the type of the call
4556 expression, i.e., the return type of the function. Any semantic
4557 errors will be deferred until the template is instantiated. */
4558 if (processing_template_decl)
4560 tree expr;
4561 tree return_type;
4562 return_type = TREE_TYPE (TREE_TYPE (fn));
4563 expr = build (CALL_EXPR, return_type, fn, args, NULL_TREE);
4564 if (!VOID_TYPE_P (return_type))
4565 require_complete_type (return_type);
4566 return convert_from_reference (expr);
4569 /* Give any warnings we noticed during overload resolution. */
4570 if (cand->warnings)
4572 struct candidate_warning *w;
4573 for (w = cand->warnings; w; w = w->next)
4574 joust (cand, w->loser, 1);
4577 if (DECL_FUNCTION_MEMBER_P (fn))
4579 /* If FN is a template function, two cases must be considered.
4580 For example:
4582 struct A {
4583 protected:
4584 template <class T> void f();
4586 template <class T> struct B {
4587 protected:
4588 void g();
4590 struct C : A, B<int> {
4591 using A::f; // #1
4592 using B<int>::g; // #2
4595 In case #1 where `A::f' is a member template, DECL_ACCESS is
4596 recorded in the primary template but not in its specialization.
4597 We check access of FN using its primary template.
4599 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
4600 because it is a member of class template B, DECL_ACCESS is
4601 recorded in the specialization `B<int>::g'. We cannot use its
4602 primary template because `B<T>::g' and `B<int>::g' may have
4603 different access. */
4604 if (DECL_TEMPLATE_INFO (fn)
4605 && is_member_template (DECL_TI_TEMPLATE (fn)))
4606 perform_or_defer_access_check (cand->access_path,
4607 DECL_TI_TEMPLATE (fn));
4608 else
4609 perform_or_defer_access_check (cand->access_path, fn);
4612 if (args && TREE_CODE (args) != TREE_LIST)
4613 args = build_tree_list (NULL_TREE, args);
4614 arg = args;
4616 /* The implicit parameters to a constructor are not considered by overload
4617 resolution, and must be of the proper type. */
4618 if (DECL_CONSTRUCTOR_P (fn))
4620 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4621 arg = TREE_CHAIN (arg);
4622 parm = TREE_CHAIN (parm);
4623 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4624 /* We should never try to call the abstract constructor. */
4625 abort ();
4626 if (DECL_HAS_VTT_PARM_P (fn))
4628 converted_args = tree_cons
4629 (NULL_TREE, TREE_VALUE (arg), converted_args);
4630 arg = TREE_CHAIN (arg);
4631 parm = TREE_CHAIN (parm);
4634 /* Bypass access control for 'this' parameter. */
4635 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4637 tree parmtype = TREE_VALUE (parm);
4638 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4639 tree converted_arg;
4640 tree base_binfo;
4642 if (convs[i]->bad_p)
4643 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4644 TREE_TYPE (argtype), fn);
4646 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4647 X is called for an object that is not of type X, or of a type
4648 derived from X, the behavior is undefined.
4650 So we can assume that anything passed as 'this' is non-null, and
4651 optimize accordingly. */
4652 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4653 /* Convert to the base in which the function was declared. */
4654 my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4655 converted_arg = build_base_path (PLUS_EXPR,
4656 TREE_VALUE (arg),
4657 cand->conversion_path,
4659 /* Check that the base class is accessible. */
4660 if (!accessible_base_p (TREE_TYPE (argtype),
4661 BINFO_TYPE (cand->conversion_path)))
4662 error ("`%T' is not an accessible base of `%T'",
4663 BINFO_TYPE (cand->conversion_path),
4664 TREE_TYPE (argtype));
4665 /* If fn was found by a using declaration, the conversion path
4666 will be to the derived class, not the base declaring fn. We
4667 must convert from derived to base. */
4668 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4669 TREE_TYPE (parmtype), ba_ignore, NULL);
4670 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4671 base_binfo, 1);
4673 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4674 parm = TREE_CHAIN (parm);
4675 arg = TREE_CHAIN (arg);
4676 ++i;
4677 is_method = 1;
4680 for (; arg && parm;
4681 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4683 tree type = TREE_VALUE (parm);
4685 conv = convs[i];
4686 val = convert_like_with_context
4687 (conv, TREE_VALUE (arg), fn, i - is_method);
4689 val = convert_for_arg_passing (type, val);
4690 converted_args = tree_cons (NULL_TREE, val, converted_args);
4693 /* Default arguments */
4694 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4695 converted_args
4696 = tree_cons (NULL_TREE,
4697 convert_default_arg (TREE_VALUE (parm),
4698 TREE_PURPOSE (parm),
4699 fn, i - is_method),
4700 converted_args);
4702 /* Ellipsis */
4703 for (; arg; arg = TREE_CHAIN (arg))
4705 tree a = TREE_VALUE (arg);
4706 if (magic_varargs_p (fn))
4707 /* Do no conversions for magic varargs. */;
4708 else
4709 a = convert_arg_to_ellipsis (a);
4710 converted_args = tree_cons (NULL_TREE, a, converted_args);
4713 converted_args = nreverse (converted_args);
4715 if (warn_format)
4716 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4717 converted_args);
4719 /* Avoid actually calling copy constructors and copy assignment operators,
4720 if possible. */
4722 if (! flag_elide_constructors)
4723 /* Do things the hard way. */;
4724 else if (cand->num_convs == 1 && DECL_COPY_CONSTRUCTOR_P (fn))
4726 tree targ;
4727 arg = skip_artificial_parms_for (fn, converted_args);
4728 arg = TREE_VALUE (arg);
4730 /* Pull out the real argument, disregarding const-correctness. */
4731 targ = arg;
4732 while (TREE_CODE (targ) == NOP_EXPR
4733 || TREE_CODE (targ) == NON_LVALUE_EXPR
4734 || TREE_CODE (targ) == CONVERT_EXPR)
4735 targ = TREE_OPERAND (targ, 0);
4736 if (TREE_CODE (targ) == ADDR_EXPR)
4738 targ = TREE_OPERAND (targ, 0);
4739 if (!same_type_ignoring_top_level_qualifiers_p
4740 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4741 targ = NULL_TREE;
4743 else
4744 targ = NULL_TREE;
4746 if (targ)
4747 arg = targ;
4748 else
4749 arg = build_indirect_ref (arg, 0);
4751 /* [class.copy]: the copy constructor is implicitly defined even if
4752 the implementation elided its use. */
4753 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4754 mark_used (fn);
4756 /* If we're creating a temp and we already have one, don't create a
4757 new one. If we're not creating a temp but we get one, use
4758 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4759 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4760 temp or an INIT_EXPR otherwise. */
4761 if (integer_zerop (TREE_VALUE (args)))
4763 if (TREE_CODE (arg) == TARGET_EXPR)
4764 return arg;
4765 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4766 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4768 else if (TREE_CODE (arg) == TARGET_EXPR
4769 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4771 tree to = stabilize_reference
4772 (build_indirect_ref (TREE_VALUE (args), 0));
4774 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4775 return val;
4778 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4779 && copy_fn_p (fn)
4780 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4782 tree to = stabilize_reference
4783 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4784 tree type = TREE_TYPE (to);
4785 tree as_base = CLASSTYPE_AS_BASE (type);
4787 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4788 if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
4789 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4790 else
4792 /* We must only copy the non-tail padding parts. Use
4793 CLASSTYPE_AS_BASE for the bitwise copy. */
4794 tree to_ptr, arg_ptr, to_as_base, arg_as_base, base_ptr_type;
4795 tree save_to;
4797 to_ptr = save_expr (build_unary_op (ADDR_EXPR, to, 0));
4798 arg_ptr = build_unary_op (ADDR_EXPR, arg, 0);
4800 base_ptr_type = build_pointer_type (as_base);
4801 to_as_base = build_nop (base_ptr_type, to_ptr);
4802 to_as_base = build_indirect_ref (to_as_base, 0);
4803 arg_as_base = build_nop (base_ptr_type, arg_ptr);
4804 arg_as_base = build_indirect_ref (arg_as_base, 0);
4806 save_to = build_indirect_ref (to_ptr, 0);
4808 val = build (MODIFY_EXPR, as_base, to_as_base, arg_as_base);
4809 val = convert_to_void (val, NULL);
4810 val = build (COMPOUND_EXPR, type, val, save_to);
4811 TREE_NO_WARNING (val) = 1;
4814 return val;
4817 mark_used (fn);
4819 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4821 tree t, *p = &TREE_VALUE (converted_args);
4822 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4823 DECL_CONTEXT (fn),
4824 ba_any, NULL);
4825 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4827 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4828 if (TREE_SIDE_EFFECTS (*p))
4829 *p = save_expr (*p);
4830 t = build_pointer_type (TREE_TYPE (fn));
4831 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4832 fn = build_java_interface_fn_ref (fn, *p);
4833 else
4834 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4835 TREE_TYPE (fn) = t;
4837 else if (DECL_INLINE (fn))
4838 fn = inline_conversion (fn);
4839 else
4840 fn = build_addr_func (fn);
4842 return build_cxx_call (fn, args, converted_args);
4845 /* Build and return a call to FN, using the the CONVERTED_ARGS. ARGS
4846 gives the original form of the arguments. This function performs
4847 no overload resolution, conversion, or other high-level
4848 operations. */
4850 tree
4851 build_cxx_call(tree fn, tree args, tree converted_args)
4853 tree fndecl;
4855 /* Recognize certain built-in functions so we can make tree-codes
4856 other than CALL_EXPR. We do this when it enables fold-const.c
4857 to do something useful. */
4858 if (TREE_CODE (fn) == ADDR_EXPR
4859 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4860 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4862 tree exp;
4863 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4864 if (exp)
4865 return exp;
4868 fn = build_call (fn, converted_args);
4870 /* If this call might throw an exception, note that fact. */
4871 fndecl = get_callee_fndecl (fn);
4872 if ((!fndecl || !TREE_NOTHROW (fndecl))
4873 && at_function_scope_p ()
4874 && cfun)
4875 cp_function_chain->can_throw = 1;
4877 /* Some built-in function calls will be evaluated at compile-time in
4878 fold (). */
4879 fn = fold (fn);
4881 if (VOID_TYPE_P (TREE_TYPE (fn)))
4882 return fn;
4884 fn = require_complete_type (fn);
4885 if (fn == error_mark_node)
4886 return error_mark_node;
4888 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4889 fn = build_cplus_new (TREE_TYPE (fn), fn);
4890 return convert_from_reference (fn);
4893 static GTY(()) tree java_iface_lookup_fn;
4895 /* Make an expression which yields the address of the Java interface
4896 method FN. This is achieved by generating a call to libjava's
4897 _Jv_LookupInterfaceMethodIdx(). */
4899 static tree
4900 build_java_interface_fn_ref (tree fn, tree instance)
4902 tree lookup_args, lookup_fn, method, idx;
4903 tree klass_ref, iface, iface_ref;
4904 int i;
4906 if (!java_iface_lookup_fn)
4908 tree endlink = build_void_list_node ();
4909 tree t = tree_cons (NULL_TREE, ptr_type_node,
4910 tree_cons (NULL_TREE, ptr_type_node,
4911 tree_cons (NULL_TREE, java_int_type_node,
4912 endlink)));
4913 java_iface_lookup_fn
4914 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4915 build_function_type (ptr_type_node, t),
4916 0, NOT_BUILT_IN, NULL, NULL_TREE);
4919 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4920 This is the first entry in the vtable. */
4921 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4922 integer_zero_node);
4924 /* Get the java.lang.Class pointer for the interface being called. */
4925 iface = DECL_CONTEXT (fn);
4926 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
4927 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4928 || DECL_CONTEXT (iface_ref) != iface)
4930 error ("could not find class$ field in java interface type `%T'",
4931 iface);
4932 return error_mark_node;
4934 iface_ref = build_address (iface_ref);
4935 iface_ref = convert (build_pointer_type (iface), iface_ref);
4937 /* Determine the itable index of FN. */
4938 i = 1;
4939 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4941 if (!DECL_VIRTUAL_P (method))
4942 continue;
4943 if (fn == method)
4944 break;
4945 i++;
4947 idx = build_int_2 (i, 0);
4949 lookup_args = tree_cons (NULL_TREE, klass_ref,
4950 tree_cons (NULL_TREE, iface_ref,
4951 build_tree_list (NULL_TREE, idx)));
4952 lookup_fn = build1 (ADDR_EXPR,
4953 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4954 java_iface_lookup_fn);
4955 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4958 /* Returns the value to use for the in-charge parameter when making a
4959 call to a function with the indicated NAME. */
4961 tree
4962 in_charge_arg_for_name (tree name)
4964 if (name == base_ctor_identifier
4965 || name == base_dtor_identifier)
4966 return integer_zero_node;
4967 else if (name == complete_ctor_identifier)
4968 return integer_one_node;
4969 else if (name == complete_dtor_identifier)
4970 return integer_two_node;
4971 else if (name == deleting_dtor_identifier)
4972 return integer_three_node;
4974 /* This function should only be called with one of the names listed
4975 above. */
4976 abort ();
4977 return NULL_TREE;
4980 /* Build a call to a constructor, destructor, or an assignment
4981 operator for INSTANCE, an expression with class type. NAME
4982 indicates the special member function to call; ARGS are the
4983 arguments. BINFO indicates the base of INSTANCE that is to be
4984 passed as the `this' parameter to the member function called.
4986 FLAGS are the LOOKUP_* flags to use when processing the call.
4988 If NAME indicates a complete object constructor, INSTANCE may be
4989 NULL_TREE. In this case, the caller will call build_cplus_new to
4990 store the newly constructed object into a VAR_DECL. */
4992 tree
4993 build_special_member_call (tree instance, tree name, tree args,
4994 tree binfo, int flags)
4996 tree fns;
4997 /* The type of the subobject to be constructed or destroyed. */
4998 tree class_type;
5000 my_friendly_assert (name == complete_ctor_identifier
5001 || name == base_ctor_identifier
5002 || name == complete_dtor_identifier
5003 || name == base_dtor_identifier
5004 || name == deleting_dtor_identifier
5005 || name == ansi_assopname (NOP_EXPR),
5006 20020712);
5007 my_friendly_assert (binfo != NULL_TREE, 20020712);
5009 class_type = BINFO_TYPE (binfo);
5011 /* Handle the special case where INSTANCE is NULL_TREE. */
5012 if (name == complete_ctor_identifier && !instance)
5014 instance = build_int_2 (0, 0);
5015 TREE_TYPE (instance) = build_pointer_type (class_type);
5016 instance = build1 (INDIRECT_REF, class_type, instance);
5018 else
5020 if (name == complete_dtor_identifier
5021 || name == base_dtor_identifier
5022 || name == deleting_dtor_identifier)
5023 my_friendly_assert (args == NULL_TREE, 20020712);
5025 /* Convert to the base class, if necessary. */
5026 if (!same_type_ignoring_top_level_qualifiers_p
5027 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
5029 if (name != ansi_assopname (NOP_EXPR))
5030 /* For constructors and destructors, either the base is
5031 non-virtual, or it is virtual but we are doing the
5032 conversion from a constructor or destructor for the
5033 complete object. In either case, we can convert
5034 statically. */
5035 instance = convert_to_base_statically (instance, binfo);
5036 else
5037 /* However, for assignment operators, we must convert
5038 dynamically if the base is virtual. */
5039 instance = build_base_path (PLUS_EXPR, instance,
5040 binfo, /*nonnull=*/1);
5044 my_friendly_assert (instance != NULL_TREE, 20020712);
5046 /* Resolve the name. */
5047 if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
5048 return error_mark_node;
5050 fns = lookup_fnfields (binfo, name, 1);
5052 /* When making a call to a constructor or destructor for a subobject
5053 that uses virtual base classes, pass down a pointer to a VTT for
5054 the subobject. */
5055 if ((name == base_ctor_identifier
5056 || name == base_dtor_identifier)
5057 && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
5059 tree vtt;
5060 tree sub_vtt;
5062 /* If the current function is a complete object constructor
5063 or destructor, then we fetch the VTT directly.
5064 Otherwise, we look it up using the VTT we were given. */
5065 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
5066 vtt = decay_conversion (vtt);
5067 vtt = build (COND_EXPR, TREE_TYPE (vtt),
5068 build (EQ_EXPR, boolean_type_node,
5069 current_in_charge_parm, integer_zero_node),
5070 current_vtt_parm,
5071 vtt);
5072 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
5073 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
5074 BINFO_SUBVTT_INDEX (binfo));
5076 args = tree_cons (NULL_TREE, sub_vtt, args);
5079 return build_new_method_call (instance, fns, args,
5080 TYPE_BINFO (BINFO_TYPE (binfo)),
5081 flags);
5084 /* Return the NAME, as a C string. The NAME indicates a function that
5085 is a member of TYPE. *FREE_P is set to true if the caller must
5086 free the memory returned.
5088 Rather than go through all of this, we should simply set the names
5089 of constructors and destructors appropriately, and dispense with
5090 ctor_identifier, dtor_identifier, etc. */
5092 static char *
5093 name_as_c_string (tree name, tree type, bool *free_p)
5095 char *pretty_name;
5097 /* Assume that we will not allocate memory. */
5098 *free_p = false;
5099 /* Constructors and destructors are special. */
5100 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
5102 pretty_name
5103 = (char *) IDENTIFIER_POINTER (constructor_name (type));
5104 /* For a destructor, add the '~'. */
5105 if (name == complete_dtor_identifier
5106 || name == base_dtor_identifier
5107 || name == deleting_dtor_identifier)
5109 pretty_name = concat ("~", pretty_name, NULL);
5110 /* Remember that we need to free the memory allocated. */
5111 *free_p = true;
5114 else if (IDENTIFIER_TYPENAME_P (name))
5116 pretty_name = concat ("operator ",
5117 type_as_string (TREE_TYPE (name),
5118 TFF_PLAIN_IDENTIFIER),
5119 NULL);
5120 /* Remember that we need to free the memory allocated. */
5121 *free_p = true;
5123 else
5124 pretty_name = (char *) IDENTIFIER_POINTER (name);
5126 return pretty_name;
5129 /* Build a call to "INSTANCE.FN (ARGS)". */
5131 tree
5132 build_new_method_call (tree instance, tree fns, tree args,
5133 tree conversion_path, int flags)
5135 struct z_candidate *candidates = 0, *cand;
5136 tree explicit_targs = NULL_TREE;
5137 tree basetype = NULL_TREE;
5138 tree access_binfo;
5139 tree optype;
5140 tree mem_args = NULL_TREE, instance_ptr;
5141 tree name;
5142 tree user_args;
5143 tree call;
5144 tree fn;
5145 tree class_type;
5146 int template_only = 0;
5147 bool any_viable_p;
5148 tree orig_instance;
5149 tree orig_fns;
5150 tree orig_args;
5151 void *p;
5153 my_friendly_assert (instance != NULL_TREE, 20020729);
5155 if (error_operand_p (instance)
5156 || error_operand_p (fns)
5157 || args == error_mark_node)
5158 return error_mark_node;
5160 orig_instance = instance;
5161 orig_fns = fns;
5162 orig_args = args;
5164 if (processing_template_decl)
5166 instance = build_non_dependent_expr (instance);
5167 if (!BASELINK_P (fns)
5168 && TREE_CODE (fns) != PSEUDO_DTOR_EXPR
5169 && TREE_TYPE (fns) != unknown_type_node)
5170 fns = build_non_dependent_expr (fns);
5171 args = build_non_dependent_args (orig_args);
5174 /* Process the argument list. */
5175 user_args = args;
5176 args = resolve_args (args);
5177 if (args == error_mark_node)
5178 return error_mark_node;
5180 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5181 instance = convert_from_reference (instance);
5182 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
5183 instance_ptr = build_this (instance);
5185 if (!BASELINK_P (fns))
5187 error ("call to non-function `%D'", fns);
5188 return error_mark_node;
5191 if (!conversion_path)
5192 conversion_path = BASELINK_BINFO (fns);
5193 access_binfo = BASELINK_ACCESS_BINFO (fns);
5194 optype = BASELINK_OPTYPE (fns);
5195 fns = BASELINK_FUNCTIONS (fns);
5197 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
5199 explicit_targs = TREE_OPERAND (fns, 1);
5200 fns = TREE_OPERAND (fns, 0);
5201 template_only = 1;
5204 my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
5205 || TREE_CODE (fns) == TEMPLATE_DECL
5206 || TREE_CODE (fns) == OVERLOAD,
5207 20020712);
5209 /* XXX this should be handled before we get here. */
5210 if (! IS_AGGR_TYPE (basetype))
5212 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
5213 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
5214 fns, instance, basetype);
5216 return error_mark_node;
5219 fn = get_first_fn (fns);
5220 name = DECL_NAME (fn);
5222 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
5224 /* Callers should explicitly indicate whether they want to construct
5225 the complete object or just the part without virtual bases. */
5226 my_friendly_assert (name != ctor_identifier, 20000408);
5227 /* Similarly for destructors. */
5228 my_friendly_assert (name != dtor_identifier, 20000408);
5231 /* It's OK to call destructors on cv-qualified objects. Therefore,
5232 convert the INSTANCE_PTR to the unqualified type, if necessary. */
5233 if (DECL_DESTRUCTOR_P (fn))
5235 tree type = build_pointer_type (basetype);
5236 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
5237 instance_ptr = build_nop (type, instance_ptr);
5240 class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE);
5241 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
5243 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5244 p = conversion_obstack_alloc (0);
5246 for (fn = fns; fn; fn = OVL_NEXT (fn))
5248 tree t = OVL_CURRENT (fn);
5249 tree this_arglist;
5251 /* We can end up here for copy-init of same or base class. */
5252 if ((flags & LOOKUP_ONLYCONVERTING)
5253 && DECL_NONCONVERTING_P (t))
5254 continue;
5256 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
5257 this_arglist = mem_args;
5258 else
5259 this_arglist = args;
5261 if (TREE_CODE (t) == TEMPLATE_DECL)
5262 /* A member template. */
5263 add_template_candidate (&candidates, t,
5264 class_type,
5265 explicit_targs,
5266 this_arglist, optype,
5267 access_binfo,
5268 conversion_path,
5269 flags,
5270 DEDUCE_CALL);
5271 else if (! template_only)
5272 add_function_candidate (&candidates, t,
5273 class_type,
5274 this_arglist,
5275 access_binfo,
5276 conversion_path,
5277 flags);
5280 candidates = splice_viable (candidates, pedantic, &any_viable_p);
5281 if (!any_viable_p)
5283 if (!COMPLETE_TYPE_P (basetype))
5284 cxx_incomplete_type_error (instance_ptr, basetype);
5285 else
5287 char *pretty_name;
5288 bool free_p;
5290 pretty_name = name_as_c_string (name, basetype, &free_p);
5291 error ("no matching function for call to `%T::%s(%A)%#V'",
5292 basetype, pretty_name, user_args,
5293 TREE_TYPE (TREE_TYPE (instance_ptr)));
5294 if (free_p)
5295 free (pretty_name);
5297 print_z_candidates (candidates);
5298 call = error_mark_node;
5300 else
5302 cand = tourney (candidates);
5303 if (cand == 0)
5305 char *pretty_name;
5306 bool free_p;
5308 pretty_name = name_as_c_string (name, basetype, &free_p);
5309 error ("call of overloaded `%s(%A)' is ambiguous", pretty_name,
5310 user_args);
5311 print_z_candidates (candidates);
5312 if (free_p)
5313 free (pretty_name);
5314 call = error_mark_node;
5316 else
5318 if (DECL_PURE_VIRTUAL_P (cand->fn)
5319 && instance == current_class_ref
5320 && (DECL_CONSTRUCTOR_P (current_function_decl)
5321 || DECL_DESTRUCTOR_P (current_function_decl))
5322 && ! (flags & LOOKUP_NONVIRTUAL)
5323 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
5324 error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
5325 "abstract virtual `%#D' called from constructor"
5326 : "abstract virtual `%#D' called from destructor"),
5327 cand->fn);
5328 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
5329 && is_dummy_object (instance_ptr))
5331 error ("cannot call member function `%D' without object",
5332 cand->fn);
5333 call = error_mark_node;
5335 else
5337 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
5338 && resolves_to_fixed_type_p (instance, 0))
5339 flags |= LOOKUP_NONVIRTUAL;
5341 call = build_over_call (cand, flags);
5343 /* In an expression of the form `a->f()' where `f' turns
5344 out to be a static member function, `a' is
5345 none-the-less evaluated. */
5346 if (TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE
5347 && !is_dummy_object (instance_ptr)
5348 && TREE_SIDE_EFFECTS (instance))
5349 call = build (COMPOUND_EXPR, TREE_TYPE (call),
5350 instance, call);
5355 if (processing_template_decl && call != error_mark_node)
5356 call = (build_min_non_dep
5357 (CALL_EXPR, call,
5358 build_min_nt (COMPONENT_REF, orig_instance, orig_fns),
5359 orig_args, NULL_TREE));
5361 /* Free all the conversions we allocated. */
5362 obstack_free (&conversion_obstack, p);
5364 return call;
5367 /* Returns true iff standard conversion sequence ICS1 is a proper
5368 subsequence of ICS2. */
5370 static bool
5371 is_subseq (conversion *ics1, conversion *ics2)
5373 /* We can assume that a conversion of the same code
5374 between the same types indicates a subsequence since we only get
5375 here if the types we are converting from are the same. */
5377 while (ics1->kind == ck_rvalue
5378 || ics1->kind == ck_lvalue)
5379 ics1 = ics1->u.next;
5381 while (1)
5383 while (ics2->kind == ck_rvalue
5384 || ics2->kind == ck_lvalue)
5385 ics2 = ics2->u.next;
5387 if (ics2->kind == ck_user
5388 || ics2->kind == ck_ambig
5389 || ics2->kind == ck_identity)
5390 /* At this point, ICS1 cannot be a proper subsequence of
5391 ICS2. We can get a USER_CONV when we are comparing the
5392 second standard conversion sequence of two user conversion
5393 sequences. */
5394 return false;
5396 ics2 = ics2->u.next;
5398 if (ics2->kind == ics1->kind
5399 && same_type_p (ics2->type, ics1->type)
5400 && same_type_p (ics2->u.next->type,
5401 ics1->u.next->type))
5402 return true;
5406 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
5407 be any _TYPE nodes. */
5409 bool
5410 is_properly_derived_from (tree derived, tree base)
5412 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
5413 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
5414 return false;
5416 /* We only allow proper derivation here. The DERIVED_FROM_P macro
5417 considers every class derived from itself. */
5418 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
5419 && DERIVED_FROM_P (base, derived));
5422 /* We build the ICS for an implicit object parameter as a pointer
5423 conversion sequence. However, such a sequence should be compared
5424 as if it were a reference conversion sequence. If ICS is the
5425 implicit conversion sequence for an implicit object parameter,
5426 modify it accordingly. */
5428 static void
5429 maybe_handle_implicit_object (conversion **ics)
5431 if ((*ics)->this_p)
5433 /* [over.match.funcs]
5435 For non-static member functions, the type of the
5436 implicit object parameter is "reference to cv X"
5437 where X is the class of which the function is a
5438 member and cv is the cv-qualification on the member
5439 function declaration. */
5440 conversion *t = *ics;
5441 tree reference_type;
5443 /* The `this' parameter is a pointer to a class type. Make the
5444 implicit conversion talk about a reference to that same class
5445 type. */
5446 reference_type = TREE_TYPE (t->type);
5447 reference_type = build_reference_type (reference_type);
5449 if (t->kind == ck_qual)
5450 t = t->u.next;
5451 if (t->kind == ck_ptr)
5452 t = t->u.next;
5453 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
5454 t = direct_reference_binding (reference_type, t);
5455 *ics = t;
5459 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5460 and return the type to which the reference refers. Otherwise,
5461 leave *ICS unchanged and return NULL_TREE. */
5463 static tree
5464 maybe_handle_ref_bind (conversion **ics)
5466 if ((*ics)->kind == ck_ref_bind)
5468 conversion *old_ics = *ics;
5469 tree type = TREE_TYPE (old_ics->type);
5470 *ics = old_ics->u.next;
5471 (*ics)->user_conv_p = old_ics->user_conv_p;
5472 (*ics)->bad_p = old_ics->bad_p;
5473 return type;
5476 return NULL_TREE;
5479 /* Compare two implicit conversion sequences according to the rules set out in
5480 [over.ics.rank]. Return values:
5482 1: ics1 is better than ics2
5483 -1: ics2 is better than ics1
5484 0: ics1 and ics2 are indistinguishable */
5486 static int
5487 compare_ics (conversion *ics1, conversion *ics2)
5489 tree from_type1;
5490 tree from_type2;
5491 tree to_type1;
5492 tree to_type2;
5493 tree deref_from_type1 = NULL_TREE;
5494 tree deref_from_type2 = NULL_TREE;
5495 tree deref_to_type1 = NULL_TREE;
5496 tree deref_to_type2 = NULL_TREE;
5497 conversion_rank rank1, rank2;
5499 /* REF_BINDING is nonzero if the result of the conversion sequence
5500 is a reference type. In that case TARGET_TYPE is the
5501 type referred to by the reference. */
5502 tree target_type1;
5503 tree target_type2;
5505 /* Handle implicit object parameters. */
5506 maybe_handle_implicit_object (&ics1);
5507 maybe_handle_implicit_object (&ics2);
5509 /* Handle reference parameters. */
5510 target_type1 = maybe_handle_ref_bind (&ics1);
5511 target_type2 = maybe_handle_ref_bind (&ics2);
5513 /* [over.ics.rank]
5515 When comparing the basic forms of implicit conversion sequences (as
5516 defined in _over.best.ics_)
5518 --a standard conversion sequence (_over.ics.scs_) is a better
5519 conversion sequence than a user-defined conversion sequence
5520 or an ellipsis conversion sequence, and
5522 --a user-defined conversion sequence (_over.ics.user_) is a
5523 better conversion sequence than an ellipsis conversion sequence
5524 (_over.ics.ellipsis_). */
5525 rank1 = CONVERSION_RANK (ics1);
5526 rank2 = CONVERSION_RANK (ics2);
5528 if (rank1 > rank2)
5529 return -1;
5530 else if (rank1 < rank2)
5531 return 1;
5533 if (rank1 == cr_bad)
5535 /* XXX Isn't this an extension? */
5536 /* Both ICS are bad. We try to make a decision based on what
5537 would have happened if they'd been good. */
5538 if (ics1->user_conv_p > ics2->user_conv_p
5539 || ics1->rank > ics2->rank)
5540 return -1;
5541 else if (ics1->user_conv_p < ics2->user_conv_p
5542 || ics1->rank < ics2->rank)
5543 return 1;
5545 /* We couldn't make up our minds; try to figure it out below. */
5548 if (ics1->ellipsis_p)
5549 /* Both conversions are ellipsis conversions. */
5550 return 0;
5552 /* User-defined conversion sequence U1 is a better conversion sequence
5553 than another user-defined conversion sequence U2 if they contain the
5554 same user-defined conversion operator or constructor and if the sec-
5555 ond standard conversion sequence of U1 is better than the second
5556 standard conversion sequence of U2. */
5558 if (ics1->user_conv_p)
5560 conversion *t1;
5561 conversion *t2;
5563 for (t1 = ics1; t1->kind != ck_user; t1 = t1->u.next)
5564 if (t1->kind == ck_ambig)
5565 return 0;
5566 for (t2 = ics2; t2->kind != ck_user; t2 = t2->u.next)
5567 if (t2->kind == ck_ambig)
5568 return 0;
5570 if (t1->cand->fn != t2->cand->fn)
5571 return 0;
5573 /* We can just fall through here, after setting up
5574 FROM_TYPE1 and FROM_TYPE2. */
5575 from_type1 = t1->type;
5576 from_type2 = t2->type;
5578 else
5580 conversion *t1;
5581 conversion *t2;
5583 /* We're dealing with two standard conversion sequences.
5585 [over.ics.rank]
5587 Standard conversion sequence S1 is a better conversion
5588 sequence than standard conversion sequence S2 if
5590 --S1 is a proper subsequence of S2 (comparing the conversion
5591 sequences in the canonical form defined by _over.ics.scs_,
5592 excluding any Lvalue Transformation; the identity
5593 conversion sequence is considered to be a subsequence of
5594 any non-identity conversion sequence */
5596 t1 = ics1;
5597 while (t1->kind != ck_identity)
5598 t1 = t1->u.next;
5599 from_type1 = t1->type;
5601 t2 = ics2;
5602 while (t2->kind != ck_identity)
5603 t2 = t2->u.next;
5604 from_type2 = t2->type;
5607 if (same_type_p (from_type1, from_type2))
5609 if (is_subseq (ics1, ics2))
5610 return 1;
5611 if (is_subseq (ics2, ics1))
5612 return -1;
5614 /* Otherwise, one sequence cannot be a subsequence of the other; they
5615 don't start with the same type. This can happen when comparing the
5616 second standard conversion sequence in two user-defined conversion
5617 sequences. */
5619 /* [over.ics.rank]
5621 Or, if not that,
5623 --the rank of S1 is better than the rank of S2 (by the rules
5624 defined below):
5626 Standard conversion sequences are ordered by their ranks: an Exact
5627 Match is a better conversion than a Promotion, which is a better
5628 conversion than a Conversion.
5630 Two conversion sequences with the same rank are indistinguishable
5631 unless one of the following rules applies:
5633 --A conversion that is not a conversion of a pointer, or pointer
5634 to member, to bool is better than another conversion that is such
5635 a conversion.
5637 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5638 so that we do not have to check it explicitly. */
5639 if (ics1->rank < ics2->rank)
5640 return 1;
5641 else if (ics2->rank < ics1->rank)
5642 return -1;
5644 to_type1 = ics1->type;
5645 to_type2 = ics2->type;
5647 if (TYPE_PTR_P (from_type1)
5648 && TYPE_PTR_P (from_type2)
5649 && TYPE_PTR_P (to_type1)
5650 && TYPE_PTR_P (to_type2))
5652 deref_from_type1 = TREE_TYPE (from_type1);
5653 deref_from_type2 = TREE_TYPE (from_type2);
5654 deref_to_type1 = TREE_TYPE (to_type1);
5655 deref_to_type2 = TREE_TYPE (to_type2);
5657 /* The rules for pointers to members A::* are just like the rules
5658 for pointers A*, except opposite: if B is derived from A then
5659 A::* converts to B::*, not vice versa. For that reason, we
5660 switch the from_ and to_ variables here. */
5661 else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
5662 && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
5663 || (TYPE_PTRMEMFUNC_P (from_type1)
5664 && TYPE_PTRMEMFUNC_P (from_type2)
5665 && TYPE_PTRMEMFUNC_P (to_type1)
5666 && TYPE_PTRMEMFUNC_P (to_type2)))
5668 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
5669 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
5670 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
5671 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
5674 if (deref_from_type1 != NULL_TREE
5675 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5676 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5678 /* This was one of the pointer or pointer-like conversions.
5680 [over.ics.rank]
5682 --If class B is derived directly or indirectly from class A,
5683 conversion of B* to A* is better than conversion of B* to
5684 void*, and conversion of A* to void* is better than
5685 conversion of B* to void*. */
5686 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5687 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5689 if (is_properly_derived_from (deref_from_type1,
5690 deref_from_type2))
5691 return -1;
5692 else if (is_properly_derived_from (deref_from_type2,
5693 deref_from_type1))
5694 return 1;
5696 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5697 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5699 if (same_type_p (deref_from_type1, deref_from_type2))
5701 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5703 if (is_properly_derived_from (deref_from_type1,
5704 deref_to_type1))
5705 return 1;
5707 /* We know that DEREF_TO_TYPE1 is `void' here. */
5708 else if (is_properly_derived_from (deref_from_type1,
5709 deref_to_type2))
5710 return -1;
5713 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5714 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5716 /* [over.ics.rank]
5718 --If class B is derived directly or indirectly from class A
5719 and class C is derived directly or indirectly from B,
5721 --conversion of C* to B* is better than conversion of C* to
5722 A*,
5724 --conversion of B* to A* is better than conversion of C* to
5725 A* */
5726 if (same_type_p (deref_from_type1, deref_from_type2))
5728 if (is_properly_derived_from (deref_to_type1,
5729 deref_to_type2))
5730 return 1;
5731 else if (is_properly_derived_from (deref_to_type2,
5732 deref_to_type1))
5733 return -1;
5735 else if (same_type_p (deref_to_type1, deref_to_type2))
5737 if (is_properly_derived_from (deref_from_type2,
5738 deref_from_type1))
5739 return 1;
5740 else if (is_properly_derived_from (deref_from_type1,
5741 deref_from_type2))
5742 return -1;
5746 else if (CLASS_TYPE_P (non_reference (from_type1))
5747 && same_type_p (from_type1, from_type2))
5749 tree from = non_reference (from_type1);
5751 /* [over.ics.rank]
5753 --binding of an expression of type C to a reference of type
5754 B& is better than binding an expression of type C to a
5755 reference of type A&
5757 --conversion of C to B is better than conversion of C to A, */
5758 if (is_properly_derived_from (from, to_type1)
5759 && is_properly_derived_from (from, to_type2))
5761 if (is_properly_derived_from (to_type1, to_type2))
5762 return 1;
5763 else if (is_properly_derived_from (to_type2, to_type1))
5764 return -1;
5767 else if (CLASS_TYPE_P (non_reference (to_type1))
5768 && same_type_p (to_type1, to_type2))
5770 tree to = non_reference (to_type1);
5772 /* [over.ics.rank]
5774 --binding of an expression of type B to a reference of type
5775 A& is better than binding an expression of type C to a
5776 reference of type A&,
5778 --conversion of B to A is better than conversion of C to A */
5779 if (is_properly_derived_from (from_type1, to)
5780 && is_properly_derived_from (from_type2, to))
5782 if (is_properly_derived_from (from_type2, from_type1))
5783 return 1;
5784 else if (is_properly_derived_from (from_type1, from_type2))
5785 return -1;
5789 /* [over.ics.rank]
5791 --S1 and S2 differ only in their qualification conversion and yield
5792 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5793 qualification signature of type T1 is a proper subset of the cv-
5794 qualification signature of type T2 */
5795 if (ics1->kind == ck_qual
5796 && ics2->kind == ck_qual
5797 && same_type_p (from_type1, from_type2))
5798 return comp_cv_qual_signature (to_type1, to_type2);
5800 /* [over.ics.rank]
5802 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5803 types to which the references refer are the same type except for
5804 top-level cv-qualifiers, and the type to which the reference
5805 initialized by S2 refers is more cv-qualified than the type to
5806 which the reference initialized by S1 refers */
5808 if (target_type1 && target_type2
5809 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5810 return comp_cv_qualification (target_type2, target_type1);
5812 /* Neither conversion sequence is better than the other. */
5813 return 0;
5816 /* The source type for this standard conversion sequence. */
5818 static tree
5819 source_type (conversion *t)
5821 for (;; t = t->u.next)
5823 if (t->kind == ck_user
5824 || t->kind == ck_ambig
5825 || t->kind == ck_identity)
5826 return t->type;
5828 abort ();
5831 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5832 a pointer to LOSER and re-running joust to produce the warning if WINNER
5833 is actually used. */
5835 static void
5836 add_warning (struct z_candidate *winner, struct z_candidate *loser)
5838 candidate_warning *cw;
5840 cw = conversion_obstack_alloc (sizeof (candidate_warning));
5841 cw->loser = loser;
5842 cw->next = winner->warnings;
5843 winner->warnings = cw;
5846 /* Compare two candidates for overloading as described in
5847 [over.match.best]. Return values:
5849 1: cand1 is better than cand2
5850 -1: cand2 is better than cand1
5851 0: cand1 and cand2 are indistinguishable */
5853 static int
5854 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
5856 int winner = 0;
5857 int off1 = 0, off2 = 0;
5858 size_t i;
5859 size_t len;
5861 /* Candidates that involve bad conversions are always worse than those
5862 that don't. */
5863 if (cand1->viable > cand2->viable)
5864 return 1;
5865 if (cand1->viable < cand2->viable)
5866 return -1;
5868 /* If we have two pseudo-candidates for conversions to the same type,
5869 or two candidates for the same function, arbitrarily pick one. */
5870 if (cand1->fn == cand2->fn
5871 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5872 return 1;
5874 /* a viable function F1
5875 is defined to be a better function than another viable function F2 if
5876 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5877 ICSi(F2), and then */
5879 /* for some argument j, ICSj(F1) is a better conversion sequence than
5880 ICSj(F2) */
5882 /* For comparing static and non-static member functions, we ignore
5883 the implicit object parameter of the non-static function. The
5884 standard says to pretend that the static function has an object
5885 parm, but that won't work with operator overloading. */
5886 len = cand1->num_convs;
5887 if (len != cand2->num_convs)
5889 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5890 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5891 off2 = 1;
5892 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5893 && DECL_STATIC_FUNCTION_P (cand2->fn))
5895 off1 = 1;
5896 --len;
5898 else
5899 abort ();
5902 for (i = 0; i < len; ++i)
5904 conversion *t1 = cand1->convs[i + off1];
5905 conversion *t2 = cand2->convs[i + off2];
5906 int comp = compare_ics (t1, t2);
5908 if (comp != 0)
5910 if (warn_sign_promo
5911 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
5912 == cr_std + cr_promotion)
5913 && t1->kind == ck_std
5914 && t2->kind == ck_std
5915 && TREE_CODE (t1->type) == INTEGER_TYPE
5916 && TREE_CODE (t2->type) == INTEGER_TYPE
5917 && (TYPE_PRECISION (t1->type)
5918 == TYPE_PRECISION (t2->type))
5919 && (TYPE_UNSIGNED (t1->u.next->type)
5920 || (TREE_CODE (t1->u.next->type)
5921 == ENUMERAL_TYPE)))
5923 tree type = t1->u.next->type;
5924 tree type1, type2;
5925 struct z_candidate *w, *l;
5926 if (comp > 0)
5927 type1 = t1->type, type2 = t2->type,
5928 w = cand1, l = cand2;
5929 else
5930 type1 = t2->type, type2 = t1->type,
5931 w = cand2, l = cand1;
5933 if (warn)
5935 warning ("passing `%T' chooses `%T' over `%T'",
5936 type, type1, type2);
5937 warning (" in call to `%D'", w->fn);
5939 else
5940 add_warning (w, l);
5943 if (winner && comp != winner)
5945 winner = 0;
5946 goto tweak;
5948 winner = comp;
5952 /* warn about confusing overload resolution for user-defined conversions,
5953 either between a constructor and a conversion op, or between two
5954 conversion ops. */
5955 if (winner && warn_conversion && cand1->second_conv
5956 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
5957 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
5959 struct z_candidate *w, *l;
5960 bool give_warning = false;
5962 if (winner == 1)
5963 w = cand1, l = cand2;
5964 else
5965 w = cand2, l = cand1;
5967 /* We don't want to complain about `X::operator T1 ()'
5968 beating `X::operator T2 () const', when T2 is a no less
5969 cv-qualified version of T1. */
5970 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
5971 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
5973 tree t = TREE_TYPE (TREE_TYPE (l->fn));
5974 tree f = TREE_TYPE (TREE_TYPE (w->fn));
5976 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
5978 t = TREE_TYPE (t);
5979 f = TREE_TYPE (f);
5981 if (!comp_ptr_ttypes (t, f))
5982 give_warning = true;
5984 else
5985 give_warning = true;
5987 if (!give_warning)
5988 /*NOP*/;
5989 else if (warn)
5991 tree source = source_type (w->convs[0]);
5992 if (! DECL_CONSTRUCTOR_P (w->fn))
5993 source = TREE_TYPE (source);
5994 warning ("choosing `%D' over `%D'", w->fn, l->fn);
5995 warning (" for conversion from `%T' to `%T'",
5996 source, w->second_conv->type);
5997 warning (" because conversion sequence for the argument is better");
5999 else
6000 add_warning (w, l);
6003 if (winner)
6004 return winner;
6006 /* or, if not that,
6007 F1 is a non-template function and F2 is a template function
6008 specialization. */
6010 if (! cand1->template && cand2->template)
6011 return 1;
6012 else if (cand1->template && ! cand2->template)
6013 return -1;
6015 /* or, if not that,
6016 F1 and F2 are template functions and the function template for F1 is
6017 more specialized than the template for F2 according to the partial
6018 ordering rules. */
6020 if (cand1->template && cand2->template)
6022 winner = more_specialized
6023 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
6024 DEDUCE_ORDER,
6025 /* Tell the deduction code how many real function arguments
6026 we saw, not counting the implicit 'this' argument. But,
6027 add_function_candidate() suppresses the "this" argument
6028 for constructors.
6030 [temp.func.order]: The presence of unused ellipsis and default
6031 arguments has no effect on the partial ordering of function
6032 templates. */
6033 cand1->num_convs
6034 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
6035 - DECL_CONSTRUCTOR_P (cand1->fn)));
6036 if (winner)
6037 return winner;
6040 /* or, if not that,
6041 the context is an initialization by user-defined conversion (see
6042 _dcl.init_ and _over.match.user_) and the standard conversion
6043 sequence from the return type of F1 to the destination type (i.e.,
6044 the type of the entity being initialized) is a better conversion
6045 sequence than the standard conversion sequence from the return type
6046 of F2 to the destination type. */
6048 if (cand1->second_conv)
6050 winner = compare_ics (cand1->second_conv, cand2->second_conv);
6051 if (winner)
6052 return winner;
6055 /* Check whether we can discard a builtin candidate, either because we
6056 have two identical ones or matching builtin and non-builtin candidates.
6058 (Pedantically in the latter case the builtin which matched the user
6059 function should not be added to the overload set, but we spot it here.
6061 [over.match.oper]
6062 ... the builtin candidates include ...
6063 - do not have the same parameter type list as any non-template
6064 non-member candidate. */
6066 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
6067 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
6069 for (i = 0; i < len; ++i)
6070 if (!same_type_p (cand1->convs[i]->type,
6071 cand2->convs[i]->type))
6072 break;
6073 if (i == cand1->num_convs)
6075 if (cand1->fn == cand2->fn)
6076 /* Two built-in candidates; arbitrarily pick one. */
6077 return 1;
6078 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
6079 /* cand1 is built-in; prefer cand2. */
6080 return -1;
6081 else
6082 /* cand2 is built-in; prefer cand1. */
6083 return 1;
6087 /* If the two functions are the same (this can happen with declarations
6088 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
6089 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
6090 && equal_functions (cand1->fn, cand2->fn))
6091 return 1;
6093 tweak:
6095 /* Extension: If the worst conversion for one candidate is worse than the
6096 worst conversion for the other, take the first. */
6097 if (!pedantic)
6099 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
6100 struct z_candidate *w = 0, *l = 0;
6102 for (i = 0; i < len; ++i)
6104 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
6105 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
6106 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
6107 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
6109 if (rank1 < rank2)
6110 winner = 1, w = cand1, l = cand2;
6111 if (rank1 > rank2)
6112 winner = -1, w = cand2, l = cand1;
6113 if (winner)
6115 if (warn)
6117 pedwarn ("\
6118 ISO C++ says that these are ambiguous, even \
6119 though the worst conversion for the first is better than \
6120 the worst conversion for the second:");
6121 print_z_candidate (_("candidate 1:"), w);
6122 print_z_candidate (_("candidate 2:"), l);
6124 else
6125 add_warning (w, l);
6126 return winner;
6130 my_friendly_assert (!winner, 20010121);
6131 return 0;
6134 /* Given a list of candidates for overloading, find the best one, if any.
6135 This algorithm has a worst case of O(2n) (winner is last), and a best
6136 case of O(n/2) (totally ambiguous); much better than a sorting
6137 algorithm. */
6139 static struct z_candidate *
6140 tourney (struct z_candidate *candidates)
6142 struct z_candidate *champ = candidates, *challenger;
6143 int fate;
6144 int champ_compared_to_predecessor = 0;
6146 /* Walk through the list once, comparing each current champ to the next
6147 candidate, knocking out a candidate or two with each comparison. */
6149 for (challenger = champ->next; challenger; )
6151 fate = joust (champ, challenger, 0);
6152 if (fate == 1)
6153 challenger = challenger->next;
6154 else
6156 if (fate == 0)
6158 champ = challenger->next;
6159 if (champ == 0)
6160 return 0;
6161 champ_compared_to_predecessor = 0;
6163 else
6165 champ = challenger;
6166 champ_compared_to_predecessor = 1;
6169 challenger = champ->next;
6173 /* Make sure the champ is better than all the candidates it hasn't yet
6174 been compared to. */
6176 for (challenger = candidates;
6177 challenger != champ
6178 && !(champ_compared_to_predecessor && challenger->next == champ);
6179 challenger = challenger->next)
6181 fate = joust (champ, challenger, 0);
6182 if (fate != 1)
6183 return 0;
6186 return champ;
6189 /* Returns nonzero if things of type FROM can be converted to TO. */
6191 bool
6192 can_convert (tree to, tree from)
6194 return can_convert_arg (to, from, NULL_TREE);
6197 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
6199 bool
6200 can_convert_arg (tree to, tree from, tree arg)
6202 conversion *t;
6203 void *p;
6204 bool ok_p;
6206 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6207 p = conversion_obstack_alloc (0);
6209 t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
6210 ok_p = (t && !t->bad_p);
6212 /* Free all the conversions we allocated. */
6213 obstack_free (&conversion_obstack, p);
6215 return ok_p;
6218 /* Like can_convert_arg, but allows dubious conversions as well. */
6220 bool
6221 can_convert_arg_bad (tree to, tree from, tree arg)
6223 conversion *t;
6224 void *p;
6226 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6227 p = conversion_obstack_alloc (0);
6228 /* Try to perform the conversion. */
6229 t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
6230 /* Free all the conversions we allocated. */
6231 obstack_free (&conversion_obstack, p);
6233 return t != NULL;
6236 /* Convert EXPR to TYPE. Return the converted expression.
6238 Note that we allow bad conversions here because by the time we get to
6239 this point we are committed to doing the conversion. If we end up
6240 doing a bad conversion, convert_like will complain. */
6242 tree
6243 perform_implicit_conversion (tree type, tree expr)
6245 conversion *conv;
6246 void *p;
6248 if (error_operand_p (expr))
6249 return error_mark_node;
6251 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6252 p = conversion_obstack_alloc (0);
6254 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6255 LOOKUP_NORMAL);
6256 if (!conv)
6258 error ("could not convert `%E' to `%T'", expr, type);
6259 expr = error_mark_node;
6261 else
6262 expr = convert_like (conv, expr);
6264 /* Free all the conversions we allocated. */
6265 obstack_free (&conversion_obstack, p);
6267 return expr;
6270 /* Convert EXPR to TYPE (as a direct-initialization) if that is
6271 permitted. If the conversion is valid, the converted expression is
6272 returned. Otherwise, NULL_TREE is returned, except in the case
6273 that TYPE is a class type; in that case, an error is issued. */
6275 tree
6276 perform_direct_initialization_if_possible (tree type, tree expr)
6278 conversion *conv;
6279 void *p;
6281 if (type == error_mark_node || error_operand_p (expr))
6282 return error_mark_node;
6283 /* [dcl.init]
6285 If the destination type is a (possibly cv-qualified) class type:
6287 -- If the initialization is direct-initialization ...,
6288 constructors are considered. ... If no constructor applies, or
6289 the overload resolution is ambiguous, the initialization is
6290 ill-formed. */
6291 if (CLASS_TYPE_P (type))
6293 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6294 build_tree_list (NULL_TREE, expr),
6295 TYPE_BINFO (type),
6296 LOOKUP_NORMAL);
6297 return build_cplus_new (type, expr);
6300 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6301 p = conversion_obstack_alloc (0);
6303 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6304 LOOKUP_NORMAL);
6305 if (!conv || conv->bad_p)
6306 expr = NULL_TREE;
6307 else
6308 expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
6309 /*issue_conversion_warnings=*/false);
6311 /* Free all the conversions we allocated. */
6312 obstack_free (&conversion_obstack, p);
6314 return expr;
6317 /* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference
6318 is being bound to a temporary. Create and return a new VAR_DECL
6319 with the indicated TYPE; this variable will store the value to
6320 which the reference is bound. */
6322 tree
6323 make_temporary_var_for_ref_to_temp (tree decl, tree type)
6325 tree var;
6327 /* Create the variable. */
6328 var = build_decl (VAR_DECL, NULL_TREE, type);
6329 DECL_ARTIFICIAL (var) = 1;
6330 TREE_USED (var) = 1;
6332 /* Register the variable. */
6333 if (TREE_STATIC (decl))
6335 /* Namespace-scope or local static; give it a mangled name. */
6336 tree name;
6338 TREE_STATIC (var) = 1;
6339 name = mangle_ref_init_variable (decl);
6340 DECL_NAME (var) = name;
6341 SET_DECL_ASSEMBLER_NAME (var, name);
6342 var = pushdecl_top_level (var);
6344 else
6346 /* Create a new cleanup level if necessary. */
6347 maybe_push_cleanup_level (type);
6348 /* Don't push unnamed temps. Do set DECL_CONTEXT, though. */
6349 DECL_CONTEXT (var) = current_function_decl;
6352 return var;
6355 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
6356 initializing a variable of that TYPE. If DECL is non-NULL, it is
6357 the VAR_DECL being initialized with the EXPR. (In that case, the
6358 type of DECL will be TYPE.) If DECL is non-NULL, then CLEANUP must
6359 also be non-NULL, and with *CLEANUP initialized to NULL. Upon
6360 return, if *CLEANUP is no longer NULL, it will be a CLEANUP_STMT
6361 that should be inserted after the returned expression is used to
6362 initialize DECL.
6364 Return the converted expression. */
6366 tree
6367 initialize_reference (tree type, tree expr, tree decl, tree *cleanup)
6369 conversion *conv;
6370 void *p;
6372 if (type == error_mark_node || error_operand_p (expr))
6373 return error_mark_node;
6375 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6376 p = conversion_obstack_alloc (0);
6378 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
6379 if (!conv || conv->bad_p)
6381 if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
6382 && !real_lvalue_p (expr))
6383 error ("invalid initialization of non-const reference of "
6384 "type '%T' from a temporary of type '%T'",
6385 type, TREE_TYPE (expr));
6386 else
6387 error ("invalid initialization of reference of type "
6388 "'%T' from expression of type '%T'", type,
6389 TREE_TYPE (expr));
6390 return error_mark_node;
6393 /* If DECL is non-NULL, then this special rule applies:
6395 [class.temporary]
6397 The temporary to which the reference is bound or the temporary
6398 that is the complete object to which the reference is bound
6399 persists for the lifetime of the reference.
6401 The temporaries created during the evaluation of the expression
6402 initializing the reference, except the temporary to which the
6403 reference is bound, are destroyed at the end of the
6404 full-expression in which they are created.
6406 In that case, we store the converted expression into a new
6407 VAR_DECL in a new scope.
6409 However, we want to be careful not to create temporaries when
6410 they are not required. For example, given:
6412 struct B {};
6413 struct D : public B {};
6414 D f();
6415 const B& b = f();
6417 there is no need to copy the return value from "f"; we can just
6418 extend its lifetime. Similarly, given:
6420 struct S {};
6421 struct T { operator S(); };
6422 T t;
6423 const S& s = t;
6425 we can extend the lifetime of the return value of the conversion
6426 operator. */
6427 my_friendly_assert (conv->kind == ck_ref_bind, 20030302);
6428 if (decl)
6430 tree var;
6431 tree base_conv_type;
6433 /* Skip over the REF_BIND. */
6434 conv = conv->u.next;
6435 /* If the next conversion is a BASE_CONV, skip that too -- but
6436 remember that the conversion was required. */
6437 if (conv->kind == ck_base && conv->need_temporary_p)
6439 void (*diagnostic_fn) (const char *, ...);
6440 if (conv->check_copy_constructor_p)
6441 /* Generate a temporary copy purely to generate the required
6442 diagnostics. */
6443 build_temp (build_dummy_object (TREE_TYPE (expr)),
6444 TREE_TYPE (expr),
6445 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6446 &diagnostic_fn);
6447 base_conv_type = conv->type;
6448 conv = conv->u.next;
6450 else
6451 base_conv_type = NULL_TREE;
6452 /* Perform the remainder of the conversion. */
6453 expr = convert_like_real (conv, expr,
6454 /*fn=*/NULL_TREE, /*argnum=*/0,
6455 /*inner=*/-1,
6456 /*issue_conversion_warnings=*/true);
6457 if (!real_lvalue_p (expr))
6459 tree init;
6460 tree type;
6462 /* Create the temporary variable. */
6463 type = TREE_TYPE (expr);
6464 var = make_temporary_var_for_ref_to_temp (decl, type);
6465 layout_decl (var, 0);
6466 /* If the rvalue is the result of a function call it will be
6467 a TARGET_EXPR. If it is some other construct (such as a
6468 member access expression where the underlying object is
6469 itself the result of a function call), turn it into a
6470 TARGET_EXPR here. It is important that EXPR be a
6471 TARGET_EXPR below since otherwise the INIT_EXPR will
6472 attempt to make a bitwise copy of EXPR to initialize
6473 VAR. */
6474 if (TREE_CODE (expr) != TARGET_EXPR)
6475 expr = get_target_expr (expr);
6476 /* Create the INIT_EXPR that will initialize the temporary
6477 variable. */
6478 init = build (INIT_EXPR, type, var, expr);
6479 if (at_function_scope_p ())
6481 add_decl_stmt (var);
6482 *cleanup = cxx_maybe_build_cleanup (var);
6483 if (*cleanup)
6484 /* We must be careful to destroy the temporary only
6485 after its initialization has taken place. If the
6486 initialization throws an exception, then the
6487 destructor should not be run. We cannot simply
6488 transform INIT into something like:
6490 (INIT, ({ CLEANUP_STMT; }))
6492 because emit_local_var always treats the
6493 initializer as a full-expression. Thus, the
6494 destructor would run too early; it would run at the
6495 end of initializing the reference variable, rather
6496 than at the end of the block enclosing the
6497 reference variable.
6499 The solution is to pass back a CLEANUP_STMT which
6500 the caller is responsible for attaching to the
6501 statement tree. */
6502 *cleanup = build_stmt (CLEANUP_STMT, var, *cleanup);
6504 else
6506 rest_of_decl_compilation (var, NULL, /*toplev=*/1, at_eof);
6507 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6508 static_aggregates = tree_cons (NULL_TREE, var,
6509 static_aggregates);
6511 /* Use its address to initialize the reference variable. */
6512 expr = build_address (var);
6513 expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
6515 else
6516 /* Take the address of EXPR. */
6517 expr = build_unary_op (ADDR_EXPR, expr, 0);
6518 /* If a BASE_CONV was required, perform it now. */
6519 if (base_conv_type)
6520 expr = (perform_implicit_conversion
6521 (build_pointer_type (base_conv_type), expr));
6522 expr = build_nop (type, expr);
6524 else
6525 /* Perform the conversion. */
6526 expr = convert_like (conv, expr);
6528 /* Free all the conversions we allocated. */
6529 obstack_free (&conversion_obstack, p);
6531 return expr;
6534 #include "gt-cp-call.h"