PR c++/11645
[official-gcc.git] / gcc / cp / call.c
blob11e8d2e0cabdc05690f88fc435a7e9d2d16627dc
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) and
5 modified by Brendan Kehoe (brendan@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 /* High-level class interface. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "output.h"
34 #include "flags.h"
35 #include "rtl.h"
36 #include "toplev.h"
37 #include "expr.h"
38 #include "diagnostic.h"
39 #include "intl.h"
41 static tree build_field_call (tree, tree, tree);
42 static struct z_candidate * tourney (struct z_candidate *);
43 static int equal_functions (tree, tree);
44 static int joust (struct z_candidate *, struct z_candidate *, bool);
45 static int compare_ics (tree, tree);
46 static tree build_over_call (struct z_candidate *, int);
47 static tree build_java_interface_fn_ref (tree, tree);
48 #define convert_like(CONV, EXPR) \
49 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \
50 /*issue_conversion_warnings=*/true)
51 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
52 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
53 /*issue_conversion_warnings=*/true)
54 static tree convert_like_real (tree, tree, tree, int, int, bool);
55 static void op_error (enum tree_code, enum tree_code, tree, tree,
56 tree, const char *);
57 static tree build_object_call (tree, tree);
58 static tree resolve_args (tree);
59 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
60 static void print_z_candidate (const char *, struct z_candidate *);
61 static void print_z_candidates (struct z_candidate *);
62 static tree build_this (tree);
63 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
64 static bool any_strictly_viable (struct z_candidate *);
65 static struct z_candidate *add_template_candidate
66 (struct z_candidate **, tree, tree, tree, tree, tree,
67 tree, tree, int, unification_kind_t);
68 static struct z_candidate *add_template_candidate_real
69 (struct z_candidate **, tree, tree, tree, tree, tree,
70 tree, tree, int, tree, unification_kind_t);
71 static struct z_candidate *add_template_conv_candidate
72 (struct z_candidate **, tree, tree, tree, tree, tree, tree);
73 static void add_builtin_candidates
74 (struct z_candidate **, enum tree_code, enum tree_code,
75 tree, tree *, int);
76 static void add_builtin_candidate
77 (struct z_candidate **, enum tree_code, enum tree_code,
78 tree, tree, tree, tree *, tree *, int);
79 static bool is_complete (tree);
80 static void build_builtin_candidate
81 (struct z_candidate **, tree, tree, tree, tree *, tree *,
82 int);
83 static struct z_candidate *add_conv_candidate
84 (struct z_candidate **, tree, tree, tree, tree, tree);
85 static struct z_candidate *add_function_candidate
86 (struct z_candidate **, tree, tree, tree, tree, tree, int);
87 static tree implicit_conversion (tree, tree, tree, int);
88 static tree standard_conversion (tree, tree, tree);
89 static tree reference_binding (tree, tree, tree, int);
90 static tree build_conv (enum tree_code, tree, tree);
91 static bool is_subseq (tree, tree);
92 static tree maybe_handle_ref_bind (tree *);
93 static void maybe_handle_implicit_object (tree *);
94 static struct z_candidate *add_candidate
95 (struct z_candidate **, tree, tree, tree, tree, tree, int);
96 static tree source_type (tree);
97 static void add_warning (struct z_candidate *, struct z_candidate *);
98 static bool reference_related_p (tree, tree);
99 static bool reference_compatible_p (tree, tree);
100 static tree convert_class_to_reference (tree, tree, tree);
101 static tree direct_reference_binding (tree, tree);
102 static bool promoted_arithmetic_type_p (tree);
103 static tree conditional_conversion (tree, tree);
104 static char *name_as_c_string (tree, tree, bool *);
105 static tree call_builtin_trap (void);
106 static tree prep_operand (tree);
107 static void add_candidates (tree, tree, tree, bool, tree, tree,
108 int, struct z_candidate **);
109 static tree merge_conversion_sequences (tree, tree);
111 tree
112 build_vfield_ref (tree datum, tree type)
114 if (datum == error_mark_node)
115 return error_mark_node;
117 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
118 datum = convert_from_reference (datum);
120 if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
121 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
122 datum = convert_to_base (datum, type, /*check_access=*/false);
124 return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
125 datum, TYPE_VFIELD (type));
128 /* Build a call to a member of an object. I.e., one that overloads
129 operator ()(), or is a pointer-to-function or pointer-to-method. */
131 static tree
132 build_field_call (tree instance_ptr, tree decl, tree parms)
134 tree instance;
136 if (decl == error_mark_node || decl == NULL_TREE)
137 return decl;
139 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
141 /* If it's a field, try overloading operator (),
142 or calling if the field is a pointer-to-function. */
143 instance = build_indirect_ref (instance_ptr, NULL);
144 instance = build_class_member_access_expr (instance, decl,
145 /*access_path=*/NULL_TREE,
146 /*preserve_reference=*/false);
148 if (instance == error_mark_node)
149 return error_mark_node;
151 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
152 return build_new_op (CALL_EXPR, LOOKUP_NORMAL,
153 instance, parms, NULL_TREE);
154 else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
155 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
156 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
157 == FUNCTION_TYPE)))
158 return build_function_call (instance, parms);
161 return NULL_TREE;
164 /* Returns nonzero iff the destructor name specified in NAME
165 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
166 forms... */
168 bool
169 check_dtor_name (tree basetype, tree name)
171 name = TREE_OPERAND (name, 0);
173 /* Just accept something we've already complained about. */
174 if (name == error_mark_node)
175 return true;
177 if (TREE_CODE (name) == TYPE_DECL)
178 name = TREE_TYPE (name);
179 else if (TYPE_P (name))
180 /* OK */;
181 else if (TREE_CODE (name) == IDENTIFIER_NODE)
183 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
184 || (TREE_CODE (basetype) == ENUMERAL_TYPE
185 && name == TYPE_IDENTIFIER (basetype)))
186 name = basetype;
187 else
188 name = get_type_value (name);
190 /* In the case of:
192 template <class T> struct S { ~S(); };
193 int i;
194 i.~S();
196 NAME will be a class template. */
197 else if (DECL_CLASS_TEMPLATE_P (name))
198 return false;
199 else
200 abort ();
202 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
203 return true;
204 return false;
207 /* We want the address of a function or method. We avoid creating a
208 pointer-to-member function. */
210 tree
211 build_addr_func (tree function)
213 tree type = TREE_TYPE (function);
215 /* We have to do these by hand to avoid real pointer to member
216 functions. */
217 if (TREE_CODE (type) == METHOD_TYPE)
219 if (TREE_CODE (function) == OFFSET_REF)
221 tree object = build_address (TREE_OPERAND (function, 0));
222 return get_member_function_from_ptrfunc (&object,
223 TREE_OPERAND (function, 1));
225 function = build_address (function);
227 else
228 function = decay_conversion (function);
230 return function;
233 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
234 POINTER_TYPE to those. Note, pointer to member function types
235 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
237 tree
238 build_call (tree function, tree parms)
240 int is_constructor = 0;
241 int nothrow;
242 tree tmp;
243 tree decl;
244 tree result_type;
245 tree fntype;
247 function = build_addr_func (function);
249 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
251 sorry ("unable to call pointer to member function here");
252 return error_mark_node;
255 fntype = TREE_TYPE (TREE_TYPE (function));
256 result_type = TREE_TYPE (fntype);
258 if (TREE_CODE (function) == ADDR_EXPR
259 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
260 decl = TREE_OPERAND (function, 0);
261 else
262 decl = NULL_TREE;
264 /* We check both the decl and the type; a function may be known not to
265 throw without being declared throw(). */
266 nothrow = ((decl && TREE_NOTHROW (decl))
267 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
269 if (decl && TREE_THIS_VOLATILE (decl) && cfun)
270 current_function_returns_abnormally = 1;
272 if (decl && TREE_DEPRECATED (decl))
273 warn_deprecated_use (decl);
274 require_complete_eh_spec_types (fntype, decl);
276 if (decl && DECL_CONSTRUCTOR_P (decl))
277 is_constructor = 1;
279 if (decl && ! TREE_USED (decl))
281 /* We invoke build_call directly for several library functions.
282 These may have been declared normally if we're building libgcc,
283 so we can't just check DECL_ARTIFICIAL. */
284 if (DECL_ARTIFICIAL (decl)
285 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
286 mark_used (decl);
287 else
288 abort ();
291 /* Don't pass empty class objects by value. This is useful
292 for tags in STL, which are used to control overload resolution.
293 We don't need to handle other cases of copying empty classes. */
294 if (! decl || ! DECL_BUILT_IN (decl))
295 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
296 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
297 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
299 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
300 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
301 TREE_VALUE (tmp), t);
304 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
305 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
306 TREE_TYPE (function) = result_type;
307 TREE_SIDE_EFFECTS (function) = 1;
308 TREE_NOTHROW (function) = nothrow;
310 return function;
313 /* Build something of the form ptr->method (args)
314 or object.method (args). This can also build
315 calls to constructors, and find friends.
317 Member functions always take their class variable
318 as a pointer.
320 INSTANCE is a class instance.
322 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
324 PARMS help to figure out what that NAME really refers to.
326 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
327 down to the real instance type to use for access checking. We need this
328 information to get protected accesses correct.
330 FLAGS is the logical disjunction of zero or more LOOKUP_
331 flags. See cp-tree.h for more info.
333 If this is all OK, calls build_function_call with the resolved
334 member function.
336 This function must also handle being called to perform
337 initialization, promotion/coercion of arguments, and
338 instantiation of default parameters.
340 Note that NAME may refer to an instance variable name. If
341 `operator()()' is defined for the type of that field, then we return
342 that result. */
344 #ifdef GATHER_STATISTICS
345 extern int n_build_method_call;
346 #endif
348 tree
349 build_method_call (tree instance, tree name, tree parms,
350 tree basetype_path, int flags)
352 tree fn;
353 tree object_type;
354 tree template_args = NULL_TREE;
355 bool has_template_args = false;
357 #ifdef GATHER_STATISTICS
358 n_build_method_call++;
359 #endif
361 if (error_operand_p (instance)
362 || name == error_mark_node
363 || parms == error_mark_node)
364 return error_mark_node;
366 my_friendly_assert (!processing_template_decl, 20030707);
368 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
369 instance = convert_from_reference (instance);
370 object_type = TREE_TYPE (instance);
372 if (TREE_CODE (name) == BIT_NOT_EXPR)
374 tree instance_ptr;
376 if (parms)
377 error ("destructors take no parameters");
379 if (! check_dtor_name (object_type, name))
380 error
381 ("destructor name `~%T' does not match type `%T' of expression",
382 TREE_OPERAND (name, 0), object_type);
384 if (! TYPE_HAS_DESTRUCTOR (complete_type (object_type)))
385 return convert_to_void (instance, /*implicit=*/NULL);
386 instance = default_conversion (instance);
387 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
388 return build_delete (build_pointer_type (object_type),
389 instance_ptr, sfk_complete_destructor,
390 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
393 if (!CLASS_TYPE_P (object_type))
395 if ((flags & LOOKUP_COMPLAIN)
396 && TREE_TYPE (instance) != error_mark_node)
397 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
398 name, instance, object_type);
399 return error_mark_node;
402 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
404 template_args = TREE_OPERAND (name, 1);
405 has_template_args = true;
406 name = TREE_OPERAND (name, 0);
408 if (TREE_CODE (name) == OVERLOAD)
409 name = DECL_NAME (get_first_fn (name));
410 else if (DECL_P (name))
411 name = DECL_NAME (name);
412 if (has_template_args)
413 fn = lookup_fnfields (object_type, name, /*protect=*/2);
414 else
415 fn = lookup_member (object_type, name, /*protect=*/2, /*want_type=*/false);
417 if (fn && TREE_CODE (fn) == TREE_LIST)
419 error ("request for member `%D' is ambiguous", name);
420 print_candidates (fn);
421 return error_mark_node;
424 /* If the name could not be found, issue an error. */
425 if (!fn)
426 return unqualified_name_lookup_error (name);
428 if (BASELINK_P (fn) && has_template_args)
429 BASELINK_FUNCTIONS (fn)
430 = build_nt (TEMPLATE_ID_EXPR,
431 BASELINK_FUNCTIONS (fn),
432 template_args);
433 if (BASELINK_P (fn) && basetype_path)
434 BASELINK_ACCESS_BINFO (fn) = basetype_path;
436 return build_new_method_call (instance, fn, parms,
437 /*conversion_path=*/NULL_TREE, flags);
440 /* New overloading code. */
442 struct z_candidate GTY(()) {
443 /* The FUNCTION_DECL that will be called if this candidate is
444 selected by overload resolution. */
445 tree fn;
446 /* The arguments to use when calling this function. */
447 tree args;
448 /* The implicit conversion sequences for each of the arguments to
449 FN. */
450 tree convs;
451 /* If FN is a user-defined conversion, the standard conversion
452 sequence from the type returned by FN to the desired destination
453 type. */
454 tree second_conv;
455 int viable;
456 /* If FN is a member function, the binfo indicating the path used to
457 qualify the name of FN at the call site. This path is used to
458 determine whether or not FN is accessible if it is selected by
459 overload resolution. The DECL_CONTEXT of FN will always be a
460 (possibly improper) base of this binfo. */
461 tree access_path;
462 /* If FN is a non-static member function, the binfo indicating the
463 subobject to which the `this' pointer should be converted if FN
464 is selected by overload resolution. The type pointed to the by
465 the `this' pointer must correspond to the most derived class
466 indicated by the CONVERSION_PATH. */
467 tree conversion_path;
468 tree template;
469 tree warnings;
470 struct z_candidate *next;
473 #define IDENTITY_RANK 0
474 #define EXACT_RANK 1
475 #define PROMO_RANK 2
476 #define STD_RANK 3
477 #define PBOOL_RANK 4
478 #define USER_RANK 5
479 #define ELLIPSIS_RANK 6
480 #define BAD_RANK 7
482 #define ICS_RANK(NODE) \
483 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
484 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
485 : ICS_USER_FLAG (NODE) ? USER_RANK \
486 : ICS_STD_RANK (NODE))
488 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
490 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
491 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
492 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
493 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
495 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
496 should be created to hold the result of the conversion. */
497 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
499 #define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
500 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
502 bool
503 null_ptr_cst_p (tree t)
505 /* [conv.ptr]
507 A null pointer constant is an integral constant expression
508 (_expr.const_) rvalue of integer type that evaluates to zero. */
509 if (t == null_node
510 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
511 return true;
512 return false;
516 /* Returns nonzero if PARMLIST consists of only default parms and/or
517 ellipsis. */
519 bool
520 sufficient_parms_p (tree parmlist)
522 for (; parmlist && parmlist != void_list_node;
523 parmlist = TREE_CHAIN (parmlist))
524 if (!TREE_PURPOSE (parmlist))
525 return false;
526 return true;
529 static tree
530 build_conv (enum tree_code code, tree type, tree from)
532 tree t;
533 int rank = ICS_STD_RANK (from);
535 /* We can't use buildl1 here because CODE could be USER_CONV, which
536 takes two arguments. In that case, the caller is responsible for
537 filling in the second argument. */
538 t = make_node (code);
539 TREE_TYPE (t) = type;
540 TREE_OPERAND (t, 0) = from;
542 switch (code)
544 case PTR_CONV:
545 case PMEM_CONV:
546 case BASE_CONV:
547 case STD_CONV:
548 if (rank < STD_RANK)
549 rank = STD_RANK;
550 break;
552 case QUAL_CONV:
553 if (rank < EXACT_RANK)
554 rank = EXACT_RANK;
556 default:
557 break;
559 ICS_STD_RANK (t) = rank;
560 ICS_USER_FLAG (t) = (code == USER_CONV || ICS_USER_FLAG (from));
561 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
562 return t;
565 tree
566 strip_top_quals (tree t)
568 if (TREE_CODE (t) == ARRAY_TYPE)
569 return t;
570 return TYPE_MAIN_VARIANT (t);
573 /* Returns the standard conversion path (see [conv]) from type FROM to type
574 TO, if any. For proper handling of null pointer constants, you must
575 also pass the expression EXPR to convert from. */
577 static tree
578 standard_conversion (tree to, tree from, tree expr)
580 enum tree_code fcode, tcode;
581 tree conv;
582 bool fromref = false;
584 to = non_reference (to);
585 if (TREE_CODE (from) == REFERENCE_TYPE)
587 fromref = true;
588 from = TREE_TYPE (from);
590 to = strip_top_quals (to);
591 from = strip_top_quals (from);
593 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
594 && expr && type_unknown_p (expr))
596 expr = instantiate_type (to, expr, tf_none);
597 if (expr == error_mark_node)
598 return NULL_TREE;
599 from = TREE_TYPE (expr);
602 fcode = TREE_CODE (from);
603 tcode = TREE_CODE (to);
605 conv = build1 (IDENTITY_CONV, from, expr);
607 if (fcode == FUNCTION_TYPE)
609 from = build_pointer_type (from);
610 fcode = TREE_CODE (from);
611 conv = build_conv (LVALUE_CONV, from, conv);
613 else if (fcode == ARRAY_TYPE)
615 from = build_pointer_type (TREE_TYPE (from));
616 fcode = TREE_CODE (from);
617 conv = build_conv (LVALUE_CONV, from, conv);
619 else if (fromref || (expr && lvalue_p (expr)))
620 conv = build_conv (RVALUE_CONV, from, conv);
622 /* Allow conversion between `__complex__' data types */
623 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
625 /* The standard conversion sequence to convert FROM to TO is
626 the standard conversion sequence to perform componentwise
627 conversion. */
628 tree part_conv = standard_conversion
629 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
631 if (part_conv)
633 conv = build_conv (TREE_CODE (part_conv), to, conv);
634 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
636 else
637 conv = NULL_TREE;
639 return conv;
642 if (same_type_p (from, to))
643 return conv;
645 if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to))
646 && expr && null_ptr_cst_p (expr))
647 conv = build_conv (STD_CONV, to, conv);
648 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
649 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
651 /* For backwards brain damage compatibility, allow interconversion of
652 pointers and integers with a pedwarn. */
653 conv = build_conv (STD_CONV, to, conv);
654 ICS_BAD_FLAG (conv) = 1;
656 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE
657 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
659 /* For backwards brain damage compatibility, allow interconversion of
660 enums and integers with a pedwarn. */
661 conv = build_conv (STD_CONV, to, conv);
662 ICS_BAD_FLAG (conv) = 1;
664 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
665 || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
667 tree to_pointee;
668 tree from_pointee;
670 if (tcode == POINTER_TYPE
671 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
672 TREE_TYPE (to)))
674 else if (VOID_TYPE_P (TREE_TYPE (to))
675 && !TYPE_PTRMEM_P (from)
676 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
678 from = build_pointer_type
679 (cp_build_qualified_type (void_type_node,
680 cp_type_quals (TREE_TYPE (from))));
681 conv = build_conv (PTR_CONV, from, conv);
683 else if (TYPE_PTRMEM_P (from))
685 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
686 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
688 if (DERIVED_FROM_P (fbase, tbase)
689 && (same_type_ignoring_top_level_qualifiers_p
690 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
691 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
693 from = build_ptrmem_type (tbase,
694 TYPE_PTRMEM_POINTED_TO_TYPE (from));
695 conv = build_conv (PMEM_CONV, from, conv);
698 else if (IS_AGGR_TYPE (TREE_TYPE (from))
699 && IS_AGGR_TYPE (TREE_TYPE (to)))
701 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
703 from =
704 cp_build_qualified_type (TREE_TYPE (to),
705 cp_type_quals (TREE_TYPE (from)));
706 from = build_pointer_type (from);
707 conv = build_conv (PTR_CONV, from, conv);
711 if (tcode == POINTER_TYPE)
713 to_pointee = TREE_TYPE (to);
714 from_pointee = TREE_TYPE (from);
716 else
718 to_pointee = to;
719 from_pointee = from;
722 if (same_type_p (from, to))
723 /* OK */;
724 else if (comp_ptr_ttypes (to_pointee, from_pointee))
725 conv = build_conv (QUAL_CONV, to, conv);
726 else if (expr && string_conv_p (to, expr, 0))
727 /* converting from string constant to char *. */
728 conv = build_conv (QUAL_CONV, to, conv);
729 else if (ptr_reasonably_similar (to_pointee, from_pointee))
731 conv = build_conv (PTR_CONV, to, conv);
732 ICS_BAD_FLAG (conv) = 1;
734 else
735 return 0;
737 from = to;
739 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
741 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
742 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
743 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
744 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
746 if (!DERIVED_FROM_P (fbase, tbase)
747 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
748 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
749 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
750 || cp_type_quals (fbase) != cp_type_quals (tbase))
751 return 0;
753 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
754 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
755 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
756 from = build_ptrmemfunc_type (build_pointer_type (from));
757 conv = build_conv (PMEM_CONV, from, conv);
759 else if (tcode == BOOLEAN_TYPE)
761 /* [conv.bool]
763 An rvalue of arithmetic, enumeration, pointer, or pointer to
764 member type can be converted to an rvalue of type bool. */
765 if (ARITHMETIC_TYPE_P (from)
766 || fcode == ENUMERAL_TYPE
767 || fcode == POINTER_TYPE
768 || TYPE_PTR_TO_MEMBER_P (from))
770 conv = build_conv (STD_CONV, to, conv);
771 if (fcode == POINTER_TYPE
772 || TYPE_PTRMEM_P (from)
773 || (TYPE_PTRMEMFUNC_P (from)
774 && ICS_STD_RANK (conv) < PBOOL_RANK))
775 ICS_STD_RANK (conv) = PBOOL_RANK;
776 return conv;
779 return NULL_TREE;
781 /* We don't check for ENUMERAL_TYPE here because there are no standard
782 conversions to enum type. */
783 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
784 || tcode == REAL_TYPE)
786 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
787 return 0;
788 conv = build_conv (STD_CONV, to, conv);
790 /* Give this a better rank if it's a promotion. */
791 if (to == type_promotes_to (from)
792 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
793 ICS_STD_RANK (conv) = PROMO_RANK;
795 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
796 && is_properly_derived_from (from, to))
798 if (TREE_CODE (conv) == RVALUE_CONV)
799 conv = TREE_OPERAND (conv, 0);
800 conv = build_conv (BASE_CONV, to, conv);
801 /* The derived-to-base conversion indicates the initialization
802 of a parameter with base type from an object of a derived
803 type. A temporary object is created to hold the result of
804 the conversion. */
805 NEED_TEMPORARY_P (conv) = 1;
807 else
808 return 0;
810 return conv;
813 /* Returns nonzero if T1 is reference-related to T2. */
815 static bool
816 reference_related_p (tree t1, tree t2)
818 t1 = TYPE_MAIN_VARIANT (t1);
819 t2 = TYPE_MAIN_VARIANT (t2);
821 /* [dcl.init.ref]
823 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
824 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
825 of T2. */
826 return (same_type_p (t1, t2)
827 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
828 && DERIVED_FROM_P (t1, t2)));
831 /* Returns nonzero if T1 is reference-compatible with T2. */
833 static bool
834 reference_compatible_p (tree t1, tree t2)
836 /* [dcl.init.ref]
838 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
839 reference-related to T2 and cv1 is the same cv-qualification as,
840 or greater cv-qualification than, cv2. */
841 return (reference_related_p (t1, t2)
842 && at_least_as_qualified_p (t1, t2));
845 /* Determine whether or not the EXPR (of class type S) can be
846 converted to T as in [over.match.ref]. */
848 static tree
849 convert_class_to_reference (tree t, tree s, tree expr)
851 tree conversions;
852 tree arglist;
853 tree conv;
854 tree reference_type;
855 struct z_candidate *candidates;
856 struct z_candidate *cand;
857 bool any_viable_p;
859 conversions = lookup_conversions (s);
860 if (!conversions)
861 return NULL_TREE;
863 /* [over.match.ref]
865 Assuming that "cv1 T" is the underlying type of the reference
866 being initialized, and "cv S" is the type of the initializer
867 expression, with S a class type, the candidate functions are
868 selected as follows:
870 --The conversion functions of S and its base classes are
871 considered. Those that are not hidden within S and yield type
872 "reference to cv2 T2", where "cv1 T" is reference-compatible
873 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
875 The argument list has one argument, which is the initializer
876 expression. */
878 candidates = 0;
880 /* Conceptually, we should take the address of EXPR and put it in
881 the argument list. Unfortunately, however, that can result in
882 error messages, which we should not issue now because we are just
883 trying to find a conversion operator. Therefore, we use NULL,
884 cast to the appropriate type. */
885 arglist = build_int_2 (0, 0);
886 TREE_TYPE (arglist) = build_pointer_type (s);
887 arglist = build_tree_list (NULL_TREE, arglist);
889 reference_type = build_reference_type (t);
891 while (conversions)
893 tree fns = TREE_VALUE (conversions);
895 for (; fns; fns = OVL_NEXT (fns))
897 tree f = OVL_CURRENT (fns);
898 tree t2 = TREE_TYPE (TREE_TYPE (f));
900 cand = NULL;
902 /* If this is a template function, try to get an exact
903 match. */
904 if (TREE_CODE (f) == TEMPLATE_DECL)
906 cand = add_template_candidate (&candidates,
907 f, s,
908 NULL_TREE,
909 arglist,
910 reference_type,
911 TYPE_BINFO (s),
912 TREE_PURPOSE (conversions),
913 LOOKUP_NORMAL,
914 DEDUCE_CONV);
916 if (cand)
918 /* Now, see if the conversion function really returns
919 an lvalue of the appropriate type. From the
920 point of view of unification, simply returning an
921 rvalue of the right type is good enough. */
922 f = cand->fn;
923 t2 = TREE_TYPE (TREE_TYPE (f));
924 if (TREE_CODE (t2) != REFERENCE_TYPE
925 || !reference_compatible_p (t, TREE_TYPE (t2)))
927 candidates = candidates->next;
928 cand = NULL;
932 else if (TREE_CODE (t2) == REFERENCE_TYPE
933 && reference_compatible_p (t, TREE_TYPE (t2)))
934 cand = add_function_candidate (&candidates, f, s, arglist,
935 TYPE_BINFO (s),
936 TREE_PURPOSE (conversions),
937 LOOKUP_NORMAL);
939 if (cand)
940 /* Build a standard conversion sequence indicating the
941 binding from the reference type returned by the
942 function to the desired REFERENCE_TYPE. */
943 cand->second_conv
944 = (direct_reference_binding
945 (reference_type,
946 build1 (IDENTITY_CONV,
947 TREE_TYPE (TREE_TYPE (TREE_TYPE (cand->fn))),
948 NULL_TREE)));
950 conversions = TREE_CHAIN (conversions);
953 candidates = splice_viable (candidates, pedantic, &any_viable_p);
954 /* If none of the conversion functions worked out, let our caller
955 know. */
956 if (!any_viable_p)
957 return NULL_TREE;
959 cand = tourney (candidates);
960 if (!cand)
961 return NULL_TREE;
963 /* Now that we know that this is the function we're going to use fix
964 the dummy first argument. */
965 cand->args = tree_cons (NULL_TREE,
966 build_this (expr),
967 TREE_CHAIN (cand->args));
969 /* Build a user-defined conversion sequence representing the
970 conversion. */
971 conv = build_conv (USER_CONV,
972 TREE_TYPE (TREE_TYPE (cand->fn)),
973 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
974 TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
976 /* Merge it with the standard conversion sequence from the
977 conversion function's return type to the desired type. */
978 cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
980 if (cand->viable == -1)
981 ICS_BAD_FLAG (conv) = 1;
983 return cand->second_conv;
986 /* A reference of the indicated TYPE is being bound directly to the
987 expression represented by the implicit conversion sequence CONV.
988 Return a conversion sequence for this binding. */
990 static tree
991 direct_reference_binding (tree type, tree conv)
993 tree t;
995 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 20030306);
996 my_friendly_assert (TREE_CODE (TREE_TYPE (conv)) != REFERENCE_TYPE,
997 20030306);
999 t = TREE_TYPE (type);
1001 /* [over.ics.rank]
1003 When a parameter of reference type binds directly
1004 (_dcl.init.ref_) to an argument expression, the implicit
1005 conversion sequence is the identity conversion, unless the
1006 argument expression has a type that is a derived class of the
1007 parameter type, in which case the implicit conversion sequence is
1008 a derived-to-base Conversion.
1010 If the parameter binds directly to the result of applying a
1011 conversion function to the argument expression, the implicit
1012 conversion sequence is a user-defined conversion sequence
1013 (_over.ics.user_), with the second standard conversion sequence
1014 either an identity conversion or, if the conversion function
1015 returns an entity of a type that is a derived class of the
1016 parameter type, a derived-to-base conversion. */
1017 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1019 /* Represent the derived-to-base conversion. */
1020 conv = build_conv (BASE_CONV, t, conv);
1021 /* We will actually be binding to the base-class subobject in
1022 the derived class, so we mark this conversion appropriately.
1023 That way, convert_like knows not to generate a temporary. */
1024 NEED_TEMPORARY_P (conv) = 0;
1026 return build_conv (REF_BIND, type, conv);
1029 /* Returns the conversion path from type FROM to reference type TO for
1030 purposes of reference binding. For lvalue binding, either pass a
1031 reference type to FROM or an lvalue expression to EXPR. If the
1032 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1033 the conversion returned. */
1035 static tree
1036 reference_binding (tree rto, tree rfrom, tree expr, int flags)
1038 tree conv = NULL_TREE;
1039 tree to = TREE_TYPE (rto);
1040 tree from = rfrom;
1041 bool related_p;
1042 bool compatible_p;
1043 cp_lvalue_kind lvalue_p = clk_none;
1045 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1047 expr = instantiate_type (to, expr, tf_none);
1048 if (expr == error_mark_node)
1049 return NULL_TREE;
1050 from = TREE_TYPE (expr);
1053 if (TREE_CODE (from) == REFERENCE_TYPE)
1055 /* Anything with reference type is an lvalue. */
1056 lvalue_p = clk_ordinary;
1057 from = TREE_TYPE (from);
1059 else if (expr)
1060 lvalue_p = real_lvalue_p (expr);
1062 /* Figure out whether or not the types are reference-related and
1063 reference compatible. We have do do this after stripping
1064 references from FROM. */
1065 related_p = reference_related_p (to, from);
1066 compatible_p = reference_compatible_p (to, from);
1068 if (lvalue_p && compatible_p)
1070 /* [dcl.init.ref]
1072 If the initializer expression
1074 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1075 is reference-compatible with "cv2 T2,"
1077 the reference is bound directly to the initializer expression
1078 lvalue. */
1079 conv = build1 (IDENTITY_CONV, from, expr);
1080 conv = direct_reference_binding (rto, conv);
1081 if ((lvalue_p & clk_bitfield) != 0
1082 || ((lvalue_p & clk_packed) != 0 && !TYPE_PACKED (to)))
1083 /* For the purposes of overload resolution, we ignore the fact
1084 this expression is a bitfield or packed field. (In particular,
1085 [over.ics.ref] says specifically that a function with a
1086 non-const reference parameter is viable even if the
1087 argument is a bitfield.)
1089 However, when we actually call the function we must create
1090 a temporary to which to bind the reference. If the
1091 reference is volatile, or isn't const, then we cannot make
1092 a temporary, so we just issue an error when the conversion
1093 actually occurs. */
1094 NEED_TEMPORARY_P (conv) = 1;
1096 return conv;
1098 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1100 /* [dcl.init.ref]
1102 If the initializer expression
1104 -- has a class type (i.e., T2 is a class type) can be
1105 implicitly converted to an lvalue of type "cv3 T3," where
1106 "cv1 T1" is reference-compatible with "cv3 T3". (this
1107 conversion is selected by enumerating the applicable
1108 conversion functions (_over.match.ref_) and choosing the
1109 best one through overload resolution. (_over.match_).
1111 the reference is bound to the lvalue result of the conversion
1112 in the second case. */
1113 conv = convert_class_to_reference (to, from, expr);
1114 if (conv)
1115 return conv;
1118 /* From this point on, we conceptually need temporaries, even if we
1119 elide them. Only the cases above are "direct bindings". */
1120 if (flags & LOOKUP_NO_TEMP_BIND)
1121 return NULL_TREE;
1123 /* [over.ics.rank]
1125 When a parameter of reference type is not bound directly to an
1126 argument expression, the conversion sequence is the one required
1127 to convert the argument expression to the underlying type of the
1128 reference according to _over.best.ics_. Conceptually, this
1129 conversion sequence corresponds to copy-initializing a temporary
1130 of the underlying type with the argument expression. Any
1131 difference in top-level cv-qualification is subsumed by the
1132 initialization itself and does not constitute a conversion. */
1134 /* [dcl.init.ref]
1136 Otherwise, the reference shall be to a non-volatile const type. */
1137 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1138 return NULL_TREE;
1140 /* [dcl.init.ref]
1142 If the initializer expression is an rvalue, with T2 a class type,
1143 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1144 is bound in one of the following ways:
1146 -- The reference is bound to the object represented by the rvalue
1147 or to a sub-object within that object.
1149 -- ...
1151 We use the first alternative. The implicit conversion sequence
1152 is supposed to be same as we would obtain by generating a
1153 temporary. Fortunately, if the types are reference compatible,
1154 then this is either an identity conversion or the derived-to-base
1155 conversion, just as for direct binding. */
1156 if (CLASS_TYPE_P (from) && compatible_p)
1158 conv = build1 (IDENTITY_CONV, from, expr);
1159 return direct_reference_binding (rto, conv);
1162 /* [dcl.init.ref]
1164 Otherwise, a temporary of type "cv1 T1" is created and
1165 initialized from the initializer expression using the rules for a
1166 non-reference copy initialization. If T1 is reference-related to
1167 T2, cv1 must be the same cv-qualification as, or greater
1168 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1169 if (related_p && !at_least_as_qualified_p (to, from))
1170 return NULL_TREE;
1172 conv = implicit_conversion (to, from, expr, flags);
1173 if (!conv)
1174 return NULL_TREE;
1176 conv = build_conv (REF_BIND, rto, conv);
1177 /* This reference binding, unlike those above, requires the
1178 creation of a temporary. */
1179 NEED_TEMPORARY_P (conv) = 1;
1181 return conv;
1184 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1185 to type TO. The optional expression EXPR may affect the conversion.
1186 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1187 significant. */
1189 static tree
1190 implicit_conversion (tree to, tree from, tree expr, int flags)
1192 tree conv;
1194 if (from == error_mark_node || to == error_mark_node
1195 || expr == error_mark_node)
1196 return NULL_TREE;
1198 if (TREE_CODE (to) == REFERENCE_TYPE)
1199 conv = reference_binding (to, from, expr, flags);
1200 else
1201 conv = standard_conversion (to, from, expr);
1203 if (conv)
1204 return conv;
1206 if (expr != NULL_TREE
1207 && (IS_AGGR_TYPE (from)
1208 || IS_AGGR_TYPE (to))
1209 && (flags & LOOKUP_NO_CONVERSION) == 0)
1211 struct z_candidate *cand;
1213 cand = build_user_type_conversion_1
1214 (to, expr, LOOKUP_ONLYCONVERTING);
1215 if (cand)
1216 conv = cand->second_conv;
1218 /* We used to try to bind a reference to a temporary here, but that
1219 is now handled by the recursive call to this function at the end
1220 of reference_binding. */
1221 return conv;
1224 return NULL_TREE;
1227 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1228 functions. */
1230 static struct z_candidate *
1231 add_candidate (struct z_candidate **candidates,
1232 tree fn, tree args, tree convs, tree access_path,
1233 tree conversion_path, int viable)
1235 struct z_candidate *cand = ggc_alloc_cleared (sizeof (struct z_candidate));
1237 cand->fn = fn;
1238 cand->args = args;
1239 cand->convs = convs;
1240 cand->access_path = access_path;
1241 cand->conversion_path = conversion_path;
1242 cand->viable = viable;
1243 cand->next = *candidates;
1244 *candidates = cand;
1246 return cand;
1249 /* Create an overload candidate for the function or method FN called with
1250 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1251 to implicit_conversion.
1253 CTYPE, if non-NULL, is the type we want to pretend this function
1254 comes from for purposes of overload resolution. */
1256 static struct z_candidate *
1257 add_function_candidate (struct z_candidate **candidates,
1258 tree fn, tree ctype, tree arglist,
1259 tree access_path, tree conversion_path,
1260 int flags)
1262 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1263 int i, len;
1264 tree convs;
1265 tree parmnode, argnode;
1266 tree orig_arglist;
1267 int viable = 1;
1269 /* Built-in functions that haven't been declared don't really
1270 exist. */
1271 if (DECL_ANTICIPATED (fn))
1272 return NULL;
1274 /* The `this', `in_chrg' and VTT arguments to constructors are not
1275 considered in overload resolution. */
1276 if (DECL_CONSTRUCTOR_P (fn))
1278 parmlist = skip_artificial_parms_for (fn, parmlist);
1279 orig_arglist = arglist;
1280 arglist = skip_artificial_parms_for (fn, arglist);
1282 else
1283 orig_arglist = arglist;
1285 len = list_length (arglist);
1286 convs = make_tree_vec (len);
1288 /* 13.3.2 - Viable functions [over.match.viable]
1289 First, to be a viable function, a candidate function shall have enough
1290 parameters to agree in number with the arguments in the list.
1292 We need to check this first; otherwise, checking the ICSes might cause
1293 us to produce an ill-formed template instantiation. */
1295 parmnode = parmlist;
1296 for (i = 0; i < len; ++i)
1298 if (parmnode == NULL_TREE || parmnode == void_list_node)
1299 break;
1300 parmnode = TREE_CHAIN (parmnode);
1303 if (i < len && parmnode)
1304 viable = 0;
1306 /* Make sure there are default args for the rest of the parms. */
1307 else if (!sufficient_parms_p (parmnode))
1308 viable = 0;
1310 if (! viable)
1311 goto out;
1313 /* Second, for F to be a viable function, there shall exist for each
1314 argument an implicit conversion sequence that converts that argument
1315 to the corresponding parameter of F. */
1317 parmnode = parmlist;
1318 argnode = arglist;
1320 for (i = 0; i < len; ++i)
1322 tree arg = TREE_VALUE (argnode);
1323 tree argtype = lvalue_type (arg);
1324 tree t;
1325 int is_this;
1327 if (parmnode == void_list_node)
1328 break;
1330 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1331 && ! DECL_CONSTRUCTOR_P (fn));
1333 if (parmnode)
1335 tree parmtype = TREE_VALUE (parmnode);
1337 /* The type of the implicit object parameter ('this') for
1338 overload resolution is not always the same as for the
1339 function itself; conversion functions are considered to
1340 be members of the class being converted, and functions
1341 introduced by a using-declaration are considered to be
1342 members of the class that uses them.
1344 Since build_over_call ignores the ICS for the `this'
1345 parameter, we can just change the parm type. */
1346 if (ctype && is_this)
1348 parmtype
1349 = build_qualified_type (ctype,
1350 TYPE_QUALS (TREE_TYPE (parmtype)));
1351 parmtype = build_pointer_type (parmtype);
1354 t = implicit_conversion (parmtype, argtype, arg, flags);
1356 else
1358 t = build1 (IDENTITY_CONV, argtype, arg);
1359 ICS_ELLIPSIS_FLAG (t) = 1;
1362 if (t && is_this)
1363 ICS_THIS_FLAG (t) = 1;
1365 TREE_VEC_ELT (convs, i) = t;
1366 if (! t)
1368 viable = 0;
1369 break;
1372 if (ICS_BAD_FLAG (t))
1373 viable = -1;
1375 if (parmnode)
1376 parmnode = TREE_CHAIN (parmnode);
1377 argnode = TREE_CHAIN (argnode);
1380 out:
1381 return add_candidate (candidates, fn, orig_arglist, convs, access_path,
1382 conversion_path, viable);
1385 /* Create an overload candidate for the conversion function FN which will
1386 be invoked for expression OBJ, producing a pointer-to-function which
1387 will in turn be called with the argument list ARGLIST, and add it to
1388 CANDIDATES. FLAGS is passed on to implicit_conversion.
1390 Actually, we don't really care about FN; we care about the type it
1391 converts to. There may be multiple conversion functions that will
1392 convert to that type, and we rely on build_user_type_conversion_1 to
1393 choose the best one; so when we create our candidate, we record the type
1394 instead of the function. */
1396 static struct z_candidate *
1397 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
1398 tree arglist, tree access_path, tree conversion_path)
1400 tree totype = TREE_TYPE (TREE_TYPE (fn));
1401 int i, len, viable, flags;
1402 tree parmlist, convs, parmnode, argnode;
1404 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1405 parmlist = TREE_TYPE (parmlist);
1406 parmlist = TYPE_ARG_TYPES (parmlist);
1408 len = list_length (arglist) + 1;
1409 convs = make_tree_vec (len);
1410 parmnode = parmlist;
1411 argnode = arglist;
1412 viable = 1;
1413 flags = LOOKUP_NORMAL;
1415 /* Don't bother looking up the same type twice. */
1416 if (*candidates && (*candidates)->fn == totype)
1417 return NULL;
1419 for (i = 0; i < len; ++i)
1421 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1422 tree argtype = lvalue_type (arg);
1423 tree t;
1425 if (i == 0)
1426 t = implicit_conversion (totype, argtype, arg, flags);
1427 else if (parmnode == void_list_node)
1428 break;
1429 else if (parmnode)
1430 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1431 else
1433 t = build1 (IDENTITY_CONV, argtype, arg);
1434 ICS_ELLIPSIS_FLAG (t) = 1;
1437 TREE_VEC_ELT (convs, i) = t;
1438 if (! t)
1439 break;
1441 if (ICS_BAD_FLAG (t))
1442 viable = -1;
1444 if (i == 0)
1445 continue;
1447 if (parmnode)
1448 parmnode = TREE_CHAIN (parmnode);
1449 argnode = TREE_CHAIN (argnode);
1452 if (i < len)
1453 viable = 0;
1455 if (!sufficient_parms_p (parmnode))
1456 viable = 0;
1458 return add_candidate (candidates, totype, arglist, convs, access_path,
1459 conversion_path, viable);
1462 static void
1463 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
1464 tree type1, tree type2, tree *args, tree *argtypes,
1465 int flags)
1467 tree t, convs;
1468 int viable = 1, i;
1469 tree types[2];
1471 types[0] = type1;
1472 types[1] = type2;
1474 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1476 for (i = 0; i < 2; ++i)
1478 if (! args[i])
1479 break;
1481 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1482 if (! t)
1484 viable = 0;
1485 /* We need something for printing the candidate. */
1486 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1488 else if (ICS_BAD_FLAG (t))
1489 viable = 0;
1490 TREE_VEC_ELT (convs, i) = t;
1493 /* For COND_EXPR we rearranged the arguments; undo that now. */
1494 if (args[2])
1496 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1497 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1498 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1499 if (t)
1500 TREE_VEC_ELT (convs, 0) = t;
1501 else
1502 viable = 0;
1505 add_candidate (candidates, fnname, /*args=*/NULL_TREE, convs,
1506 /*access_path=*/NULL_TREE,
1507 /*conversion_path=*/NULL_TREE,
1508 viable);
1511 static bool
1512 is_complete (tree t)
1514 return COMPLETE_TYPE_P (complete_type (t));
1517 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1519 static bool
1520 promoted_arithmetic_type_p (tree type)
1522 /* [over.built]
1524 In this section, the term promoted integral type is used to refer
1525 to those integral types which are preserved by integral promotion
1526 (including e.g. int and long but excluding e.g. char).
1527 Similarly, the term promoted arithmetic type refers to promoted
1528 integral types plus floating types. */
1529 return ((INTEGRAL_TYPE_P (type)
1530 && same_type_p (type_promotes_to (type), type))
1531 || TREE_CODE (type) == REAL_TYPE);
1534 /* Create any builtin operator overload candidates for the operator in
1535 question given the converted operand types TYPE1 and TYPE2. The other
1536 args are passed through from add_builtin_candidates to
1537 build_builtin_candidate.
1539 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1540 If CODE is requires candidates operands of the same type of the kind
1541 of which TYPE1 and TYPE2 are, we add both candidates
1542 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1544 static void
1545 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
1546 enum tree_code code2, tree fnname, tree type1,
1547 tree type2, tree *args, tree *argtypes, int flags)
1549 switch (code)
1551 case POSTINCREMENT_EXPR:
1552 case POSTDECREMENT_EXPR:
1553 args[1] = integer_zero_node;
1554 type2 = integer_type_node;
1555 break;
1556 default:
1557 break;
1560 switch (code)
1563 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1564 and VQ is either volatile or empty, there exist candidate operator
1565 functions of the form
1566 VQ T& operator++(VQ T&);
1567 T operator++(VQ T&, int);
1568 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1569 type other than bool, and VQ is either volatile or empty, there exist
1570 candidate operator functions of the form
1571 VQ T& operator--(VQ T&);
1572 T operator--(VQ T&, int);
1573 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1574 complete object type, and VQ is either volatile or empty, there exist
1575 candidate operator functions of the form
1576 T*VQ& operator++(T*VQ&);
1577 T*VQ& operator--(T*VQ&);
1578 T* operator++(T*VQ&, int);
1579 T* operator--(T*VQ&, int); */
1581 case POSTDECREMENT_EXPR:
1582 case PREDECREMENT_EXPR:
1583 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1584 return;
1585 case POSTINCREMENT_EXPR:
1586 case PREINCREMENT_EXPR:
1587 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1589 type1 = build_reference_type (type1);
1590 break;
1592 return;
1594 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1595 exist candidate operator functions of the form
1597 T& operator*(T*);
1599 8 For every function type T, there exist candidate operator functions of
1600 the form
1601 T& operator*(T*); */
1603 case INDIRECT_REF:
1604 if (TREE_CODE (type1) == POINTER_TYPE
1605 && (TYPE_PTROB_P (type1)
1606 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1607 break;
1608 return;
1610 /* 9 For every type T, there exist candidate operator functions of the form
1611 T* operator+(T*);
1613 10For every promoted arithmetic type T, there exist candidate operator
1614 functions of the form
1615 T operator+(T);
1616 T operator-(T); */
1618 case CONVERT_EXPR: /* unary + */
1619 if (TREE_CODE (type1) == POINTER_TYPE)
1620 break;
1621 case NEGATE_EXPR:
1622 if (ARITHMETIC_TYPE_P (type1))
1623 break;
1624 return;
1626 /* 11For every promoted integral type T, there exist candidate operator
1627 functions of the form
1628 T operator~(T); */
1630 case BIT_NOT_EXPR:
1631 if (INTEGRAL_TYPE_P (type1))
1632 break;
1633 return;
1635 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1636 is the same type as C2 or is a derived class of C2, T is a complete
1637 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1638 there exist candidate operator functions of the form
1639 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1640 where CV12 is the union of CV1 and CV2. */
1642 case MEMBER_REF:
1643 if (TREE_CODE (type1) == POINTER_TYPE
1644 && TYPE_PTR_TO_MEMBER_P (type2))
1646 tree c1 = TREE_TYPE (type1);
1647 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
1649 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1650 && (TYPE_PTRMEMFUNC_P (type2)
1651 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1652 break;
1654 return;
1656 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1657 didate operator functions of the form
1658 LR operator*(L, R);
1659 LR operator/(L, R);
1660 LR operator+(L, R);
1661 LR operator-(L, R);
1662 bool operator<(L, R);
1663 bool operator>(L, R);
1664 bool operator<=(L, R);
1665 bool operator>=(L, R);
1666 bool operator==(L, R);
1667 bool operator!=(L, R);
1668 where LR is the result of the usual arithmetic conversions between
1669 types L and R.
1671 14For every pair of types T and I, where T is a cv-qualified or cv-
1672 unqualified complete object type and I is a promoted integral type,
1673 there exist candidate operator functions of the form
1674 T* operator+(T*, I);
1675 T& operator[](T*, I);
1676 T* operator-(T*, I);
1677 T* operator+(I, T*);
1678 T& operator[](I, T*);
1680 15For every T, where T is a pointer to complete object type, there exist
1681 candidate operator functions of the form112)
1682 ptrdiff_t operator-(T, T);
1684 16For every pointer or enumeration type T, there exist candidate operator
1685 functions of the form
1686 bool operator<(T, T);
1687 bool operator>(T, T);
1688 bool operator<=(T, T);
1689 bool operator>=(T, T);
1690 bool operator==(T, T);
1691 bool operator!=(T, T);
1693 17For every pointer to member type T, there exist candidate operator
1694 functions of the form
1695 bool operator==(T, T);
1696 bool operator!=(T, T); */
1698 case MINUS_EXPR:
1699 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1700 break;
1701 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1703 type2 = ptrdiff_type_node;
1704 break;
1706 case MULT_EXPR:
1707 case TRUNC_DIV_EXPR:
1708 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1709 break;
1710 return;
1712 case EQ_EXPR:
1713 case NE_EXPR:
1714 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1715 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1716 break;
1717 if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
1719 type2 = type1;
1720 break;
1722 if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
1724 type1 = type2;
1725 break;
1727 /* FALLTHROUGH */
1728 case LT_EXPR:
1729 case GT_EXPR:
1730 case LE_EXPR:
1731 case GE_EXPR:
1732 case MAX_EXPR:
1733 case MIN_EXPR:
1734 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1735 break;
1736 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1737 break;
1738 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1739 break;
1740 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1742 type2 = type1;
1743 break;
1745 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1747 type1 = type2;
1748 break;
1750 return;
1752 case PLUS_EXPR:
1753 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1754 break;
1755 case ARRAY_REF:
1756 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1758 type1 = ptrdiff_type_node;
1759 break;
1761 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1763 type2 = ptrdiff_type_node;
1764 break;
1766 return;
1768 /* 18For every pair of promoted integral types L and R, there exist candi-
1769 date operator functions of the form
1770 LR operator%(L, R);
1771 LR operator&(L, R);
1772 LR operator^(L, R);
1773 LR operator|(L, R);
1774 L operator<<(L, R);
1775 L operator>>(L, R);
1776 where LR is the result of the usual arithmetic conversions between
1777 types L and R. */
1779 case TRUNC_MOD_EXPR:
1780 case BIT_AND_EXPR:
1781 case BIT_IOR_EXPR:
1782 case BIT_XOR_EXPR:
1783 case LSHIFT_EXPR:
1784 case RSHIFT_EXPR:
1785 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1786 break;
1787 return;
1789 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1790 type, VQ is either volatile or empty, and R is a promoted arithmetic
1791 type, there exist candidate operator functions of the form
1792 VQ L& operator=(VQ L&, R);
1793 VQ L& operator*=(VQ L&, R);
1794 VQ L& operator/=(VQ L&, R);
1795 VQ L& operator+=(VQ L&, R);
1796 VQ L& operator-=(VQ L&, R);
1798 20For every pair T, VQ), where T is any type and VQ is either volatile
1799 or empty, there exist candidate operator functions of the form
1800 T*VQ& operator=(T*VQ&, T*);
1802 21For every pair T, VQ), where T is a pointer to member type and VQ is
1803 either volatile or empty, there exist candidate operator functions of
1804 the form
1805 VQ T& operator=(VQ T&, T);
1807 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1808 unqualified complete object type, VQ is either volatile or empty, and
1809 I is a promoted integral type, there exist candidate operator func-
1810 tions of the form
1811 T*VQ& operator+=(T*VQ&, I);
1812 T*VQ& operator-=(T*VQ&, I);
1814 23For every triple L, VQ, R), where L is an integral or enumeration
1815 type, VQ is either volatile or empty, and R is a promoted integral
1816 type, there exist candidate operator functions of the form
1818 VQ L& operator%=(VQ L&, R);
1819 VQ L& operator<<=(VQ L&, R);
1820 VQ L& operator>>=(VQ L&, R);
1821 VQ L& operator&=(VQ L&, R);
1822 VQ L& operator^=(VQ L&, R);
1823 VQ L& operator|=(VQ L&, R); */
1825 case MODIFY_EXPR:
1826 switch (code2)
1828 case PLUS_EXPR:
1829 case MINUS_EXPR:
1830 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1832 type2 = ptrdiff_type_node;
1833 break;
1835 case MULT_EXPR:
1836 case TRUNC_DIV_EXPR:
1837 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1838 break;
1839 return;
1841 case TRUNC_MOD_EXPR:
1842 case BIT_AND_EXPR:
1843 case BIT_IOR_EXPR:
1844 case BIT_XOR_EXPR:
1845 case LSHIFT_EXPR:
1846 case RSHIFT_EXPR:
1847 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1848 break;
1849 return;
1851 case NOP_EXPR:
1852 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1853 break;
1854 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1855 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1856 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1857 || ((TYPE_PTRMEMFUNC_P (type1)
1858 || TREE_CODE (type1) == POINTER_TYPE)
1859 && null_ptr_cst_p (args[1])))
1861 type2 = type1;
1862 break;
1864 return;
1866 default:
1867 abort ();
1869 type1 = build_reference_type (type1);
1870 break;
1872 case COND_EXPR:
1873 /* [over.built]
1875 For every pair of promoted arithmetic types L and R, there
1876 exist candidate operator functions of the form
1878 LR operator?(bool, L, R);
1880 where LR is the result of the usual arithmetic conversions
1881 between types L and R.
1883 For every type T, where T is a pointer or pointer-to-member
1884 type, there exist candidate operator functions of the form T
1885 operator?(bool, T, T); */
1887 if (promoted_arithmetic_type_p (type1)
1888 && promoted_arithmetic_type_p (type2))
1889 /* That's OK. */
1890 break;
1892 /* Otherwise, the types should be pointers. */
1893 if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
1894 || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
1895 return;
1897 /* We don't check that the two types are the same; the logic
1898 below will actually create two candidates; one in which both
1899 parameter types are TYPE1, and one in which both parameter
1900 types are TYPE2. */
1901 break;
1903 default:
1904 abort ();
1907 /* If we're dealing with two pointer types or two enumeral types,
1908 we need candidates for both of them. */
1909 if (type2 && !same_type_p (type1, type2)
1910 && TREE_CODE (type1) == TREE_CODE (type2)
1911 && (TREE_CODE (type1) == REFERENCE_TYPE
1912 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1913 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1914 || TYPE_PTRMEMFUNC_P (type1)
1915 || IS_AGGR_TYPE (type1)
1916 || TREE_CODE (type1) == ENUMERAL_TYPE))
1918 build_builtin_candidate
1919 (candidates, fnname, type1, type1, args, argtypes, flags);
1920 build_builtin_candidate
1921 (candidates, fnname, type2, type2, args, argtypes, flags);
1922 return;
1925 build_builtin_candidate
1926 (candidates, fnname, type1, type2, args, argtypes, flags);
1929 tree
1930 type_decays_to (tree type)
1932 if (TREE_CODE (type) == ARRAY_TYPE)
1933 return build_pointer_type (TREE_TYPE (type));
1934 if (TREE_CODE (type) == FUNCTION_TYPE)
1935 return build_pointer_type (type);
1936 return type;
1939 /* There are three conditions of builtin candidates:
1941 1) bool-taking candidates. These are the same regardless of the input.
1942 2) pointer-pair taking candidates. These are generated for each type
1943 one of the input types converts to.
1944 3) arithmetic candidates. According to the standard, we should generate
1945 all of these, but I'm trying not to...
1947 Here we generate a superset of the possible candidates for this particular
1948 case. That is a subset of the full set the standard defines, plus some
1949 other cases which the standard disallows. add_builtin_candidate will
1950 filter out the invalid set. */
1952 static void
1953 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
1954 enum tree_code code2, tree fnname, tree *args,
1955 int flags)
1957 int ref1, i;
1958 int enum_p = 0;
1959 tree type, argtypes[3];
1960 /* TYPES[i] is the set of possible builtin-operator parameter types
1961 we will consider for the Ith argument. These are represented as
1962 a TREE_LIST; the TREE_VALUE of each node is the potential
1963 parameter type. */
1964 tree types[2];
1966 for (i = 0; i < 3; ++i)
1968 if (args[i])
1969 argtypes[i] = lvalue_type (args[i]);
1970 else
1971 argtypes[i] = NULL_TREE;
1974 switch (code)
1976 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1977 and VQ is either volatile or empty, there exist candidate operator
1978 functions of the form
1979 VQ T& operator++(VQ T&); */
1981 case POSTINCREMENT_EXPR:
1982 case PREINCREMENT_EXPR:
1983 case POSTDECREMENT_EXPR:
1984 case PREDECREMENT_EXPR:
1985 case MODIFY_EXPR:
1986 ref1 = 1;
1987 break;
1989 /* 24There also exist candidate operator functions of the form
1990 bool operator!(bool);
1991 bool operator&&(bool, bool);
1992 bool operator||(bool, bool); */
1994 case TRUTH_NOT_EXPR:
1995 build_builtin_candidate
1996 (candidates, fnname, boolean_type_node,
1997 NULL_TREE, args, argtypes, flags);
1998 return;
2000 case TRUTH_ORIF_EXPR:
2001 case TRUTH_ANDIF_EXPR:
2002 build_builtin_candidate
2003 (candidates, fnname, boolean_type_node,
2004 boolean_type_node, args, argtypes, flags);
2005 return;
2007 case ADDR_EXPR:
2008 case COMPOUND_EXPR:
2009 case COMPONENT_REF:
2010 return;
2012 case COND_EXPR:
2013 case EQ_EXPR:
2014 case NE_EXPR:
2015 case LT_EXPR:
2016 case LE_EXPR:
2017 case GT_EXPR:
2018 case GE_EXPR:
2019 enum_p = 1;
2020 /* FALLTHROUGH */
2022 default:
2023 ref1 = 0;
2026 types[0] = types[1] = NULL_TREE;
2028 for (i = 0; i < 2; ++i)
2030 if (! args[i])
2032 else if (IS_AGGR_TYPE (argtypes[i]))
2034 tree convs;
2036 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2037 return;
2039 convs = lookup_conversions (argtypes[i]);
2041 if (code == COND_EXPR)
2043 if (real_lvalue_p (args[i]))
2044 types[i] = tree_cons
2045 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2047 types[i] = tree_cons
2048 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2051 else if (! convs)
2052 return;
2054 for (; convs; convs = TREE_CHAIN (convs))
2056 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2058 if (i == 0 && ref1
2059 && (TREE_CODE (type) != REFERENCE_TYPE
2060 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2061 continue;
2063 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2064 types[i] = tree_cons (NULL_TREE, type, types[i]);
2066 type = non_reference (type);
2067 if (i != 0 || ! ref1)
2069 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2070 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2071 types[i] = tree_cons (NULL_TREE, type, types[i]);
2072 if (INTEGRAL_TYPE_P (type))
2073 type = type_promotes_to (type);
2076 if (! value_member (type, types[i]))
2077 types[i] = tree_cons (NULL_TREE, type, types[i]);
2080 else
2082 if (code == COND_EXPR && real_lvalue_p (args[i]))
2083 types[i] = tree_cons
2084 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2085 type = non_reference (argtypes[i]);
2086 if (i != 0 || ! ref1)
2088 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2089 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2090 types[i] = tree_cons (NULL_TREE, type, types[i]);
2091 if (INTEGRAL_TYPE_P (type))
2092 type = type_promotes_to (type);
2094 types[i] = tree_cons (NULL_TREE, type, types[i]);
2098 /* Run through the possible parameter types of both arguments,
2099 creating candidates with those parameter types. */
2100 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2102 if (types[1])
2103 for (type = types[1]; type; type = TREE_CHAIN (type))
2104 add_builtin_candidate
2105 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2106 TREE_VALUE (type), args, argtypes, flags);
2107 else
2108 add_builtin_candidate
2109 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2110 NULL_TREE, args, argtypes, flags);
2113 return;
2117 /* If TMPL can be successfully instantiated as indicated by
2118 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2120 TMPL is the template. EXPLICIT_TARGS are any explicit template
2121 arguments. ARGLIST is the arguments provided at the call-site.
2122 The RETURN_TYPE is the desired type for conversion operators. If
2123 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2124 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2125 add_conv_candidate. */
2127 static struct z_candidate*
2128 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2129 tree ctype, tree explicit_targs, tree arglist,
2130 tree return_type, tree access_path,
2131 tree conversion_path, int flags, tree obj,
2132 unification_kind_t strict)
2134 int ntparms = DECL_NTPARMS (tmpl);
2135 tree targs = make_tree_vec (ntparms);
2136 tree args_without_in_chrg = arglist;
2137 struct z_candidate *cand;
2138 int i;
2139 tree fn;
2141 /* We don't do deduction on the in-charge parameter, the VTT
2142 parameter or 'this'. */
2143 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2144 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2146 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2147 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2148 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2149 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2151 i = fn_type_unification (tmpl, explicit_targs, targs,
2152 args_without_in_chrg,
2153 return_type, strict, -1);
2155 if (i != 0)
2156 return NULL;
2158 fn = instantiate_template (tmpl, targs, tf_none);
2159 if (fn == error_mark_node)
2160 return NULL;
2162 /* In [class.copy]:
2164 A member function template is never instantiated to perform the
2165 copy of a class object to an object of its class type.
2167 It's a little unclear what this means; the standard explicitly
2168 does allow a template to be used to copy a class. For example,
2171 struct A {
2172 A(A&);
2173 template <class T> A(const T&);
2175 const A f ();
2176 void g () { A a (f ()); }
2178 the member template will be used to make the copy. The section
2179 quoted above appears in the paragraph that forbids constructors
2180 whose only parameter is (a possibly cv-qualified variant of) the
2181 class type, and a logical interpretation is that the intent was
2182 to forbid the instantiation of member templates which would then
2183 have that form. */
2184 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2186 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2187 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2188 ctype))
2189 return NULL;
2192 if (obj != NULL_TREE)
2193 /* Aha, this is a conversion function. */
2194 cand = add_conv_candidate (candidates, fn, obj, access_path,
2195 conversion_path, arglist);
2196 else
2197 cand = add_function_candidate (candidates, fn, ctype,
2198 arglist, access_path,
2199 conversion_path, flags);
2200 if (DECL_TI_TEMPLATE (fn) != tmpl)
2201 /* This situation can occur if a member template of a template
2202 class is specialized. Then, instantiate_template might return
2203 an instantiation of the specialization, in which case the
2204 DECL_TI_TEMPLATE field will point at the original
2205 specialization. For example:
2207 template <class T> struct S { template <class U> void f(U);
2208 template <> void f(int) {}; };
2209 S<double> sd;
2210 sd.f(3);
2212 Here, TMPL will be template <class U> S<double>::f(U).
2213 And, instantiate template will give us the specialization
2214 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2215 for this will point at template <class T> template <> S<T>::f(int),
2216 so that we can find the definition. For the purposes of
2217 overload resolution, however, we want the original TMPL. */
2218 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2219 else
2220 cand->template = DECL_TEMPLATE_INFO (fn);
2222 return cand;
2226 static struct z_candidate *
2227 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2228 tree explicit_targs, tree arglist, tree return_type,
2229 tree access_path, tree conversion_path, int flags,
2230 unification_kind_t strict)
2232 return
2233 add_template_candidate_real (candidates, tmpl, ctype,
2234 explicit_targs, arglist, return_type,
2235 access_path, conversion_path,
2236 flags, NULL_TREE, strict);
2240 static struct z_candidate *
2241 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2242 tree obj, tree arglist, tree return_type,
2243 tree access_path, tree conversion_path)
2245 return
2246 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2247 arglist, return_type, access_path,
2248 conversion_path, 0, obj, DEDUCE_CONV);
2251 /* The CANDS are the set of candidates that were considered for
2252 overload resolution. Return the set of viable candidates. If none
2253 of the candidates were viable, set *ANY_VIABLE_P to true. STRICT_P
2254 is true if a candidate should be considered viable only if it is
2255 strictly viable. */
2257 static struct z_candidate*
2258 splice_viable (struct z_candidate *cands,
2259 bool strict_p,
2260 bool *any_viable_p)
2262 struct z_candidate *viable;
2263 struct z_candidate **last_viable;
2264 struct z_candidate **cand;
2266 viable = NULL;
2267 last_viable = &viable;
2268 *any_viable_p = false;
2270 cand = &cands;
2271 while (*cand)
2273 struct z_candidate *c = *cand;
2274 if (strict_p ? c->viable == 1 : c->viable)
2276 *last_viable = c;
2277 *cand = c->next;
2278 c->next = NULL;
2279 last_viable = &c->next;
2280 *any_viable_p = true;
2282 else
2283 cand = &c->next;
2286 return viable ? viable : cands;
2289 static bool
2290 any_strictly_viable (struct z_candidate *cands)
2292 for (; cands; cands = cands->next)
2293 if (cands->viable == 1)
2294 return true;
2295 return false;
2298 static tree
2299 build_this (tree obj)
2301 /* Fix this to work on non-lvalues. */
2302 return build_unary_op (ADDR_EXPR, obj, 0);
2305 /* Returns true iff functions are equivalent. Equivalent functions are
2306 not '==' only if one is a function-local extern function or if
2307 both are extern "C". */
2309 static inline int
2310 equal_functions (tree fn1, tree fn2)
2312 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2313 || DECL_EXTERN_C_FUNCTION_P (fn1))
2314 return decls_match (fn1, fn2);
2315 return fn1 == fn2;
2318 /* Print information about one overload candidate CANDIDATE. MSGSTR
2319 is the text to print before the candidate itself.
2321 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2322 to have been run through gettext by the caller. This wart makes
2323 life simpler in print_z_candidates and for the translators. */
2325 static void
2326 print_z_candidate (const char *msgstr, struct z_candidate *candidate)
2328 if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2330 if (TREE_VEC_LENGTH (candidate->convs) == 3)
2331 inform ("%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
2332 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2333 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)),
2334 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 2)));
2335 else if (TREE_VEC_LENGTH (candidate->convs) == 2)
2336 inform ("%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
2337 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2338 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)));
2339 else
2340 inform ("%s %D(%T) <built-in>", msgstr, candidate->fn,
2341 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)));
2343 else if (TYPE_P (candidate->fn))
2344 inform ("%s %T <conversion>", msgstr, candidate->fn);
2345 else if (candidate->viable == -1)
2346 inform ("%H%s %+#D <near match>",
2347 &DECL_SOURCE_LOCATION (candidate->fn), msgstr, candidate->fn);
2348 else
2349 inform ("%H%s %+#D",
2350 &DECL_SOURCE_LOCATION (candidate->fn), msgstr, candidate->fn);
2353 static void
2354 print_z_candidates (struct z_candidate *candidates)
2356 const char *str;
2357 struct z_candidate *cand1;
2358 struct z_candidate **cand2;
2360 /* There may be duplicates in the set of candidates. We put off
2361 checking this condition as long as possible, since we have no way
2362 to eliminate duplicates from a set of functions in less than n^2
2363 time. Now we are about to emit an error message, so it is more
2364 permissible to go slowly. */
2365 for (cand1 = candidates; cand1; cand1 = cand1->next)
2367 tree fn = cand1->fn;
2368 /* Skip builtin candidates and conversion functions. */
2369 if (TREE_CODE (fn) != FUNCTION_DECL)
2370 continue;
2371 cand2 = &cand1->next;
2372 while (*cand2)
2374 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2375 && equal_functions (fn, (*cand2)->fn))
2376 *cand2 = (*cand2)->next;
2377 else
2378 cand2 = &(*cand2)->next;
2382 if (!candidates)
2383 return;
2385 str = _("candidates are:");
2386 print_z_candidate (str, candidates);
2387 if (candidates->next)
2389 /* Indent successive candidates by the width of the translation
2390 of the above string. */
2391 size_t len = gcc_gettext_width (str) + 1;
2392 char *spaces = alloca (len);
2393 memset (spaces, ' ', len-1);
2394 spaces[len - 1] = '\0';
2396 candidates = candidates->next;
2399 print_z_candidate (spaces, candidates);
2400 candidates = candidates->next;
2402 while (candidates);
2406 /* USER_SEQ is a user-defined conversion sequence, beginning with a
2407 USER_CONV. STD_SEQ is the standard conversion sequence applied to
2408 the result of the conversion function to convert it to the final
2409 desired type. Merge the the two sequences into a single sequence,
2410 and return the merged sequence. */
2412 static tree
2413 merge_conversion_sequences (tree user_seq, tree std_seq)
2415 tree *t;
2417 my_friendly_assert (TREE_CODE (user_seq) == USER_CONV,
2418 20030306);
2420 /* Find the end of the second conversion sequence. */
2421 t = &(std_seq);
2422 while (TREE_CODE (*t) != IDENTITY_CONV)
2423 t = &TREE_OPERAND (*t, 0);
2425 /* Replace the identity conversion with the user conversion
2426 sequence. */
2427 *t = user_seq;
2429 /* The entire sequence is a user-conversion sequence. */
2430 ICS_USER_FLAG (std_seq) = 1;
2432 return std_seq;
2435 /* Returns the best overload candidate to perform the requested
2436 conversion. This function is used for three the overloading situations
2437 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2438 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2439 per [dcl.init.ref], so we ignore temporary bindings. */
2441 static struct z_candidate *
2442 build_user_type_conversion_1 (tree totype, tree expr, int flags)
2444 struct z_candidate *candidates, *cand;
2445 tree fromtype = TREE_TYPE (expr);
2446 tree ctors = NULL_TREE, convs = NULL_TREE;
2447 tree args = NULL_TREE;
2448 bool any_viable_p;
2450 /* We represent conversion within a hierarchy using RVALUE_CONV and
2451 BASE_CONV, as specified by [over.best.ics]; these become plain
2452 constructor calls, as specified in [dcl.init]. */
2453 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2454 || !DERIVED_FROM_P (totype, fromtype), 20011226);
2456 if (IS_AGGR_TYPE (totype))
2457 ctors = lookup_fnfields (TYPE_BINFO (totype),
2458 complete_ctor_identifier,
2461 if (IS_AGGR_TYPE (fromtype))
2462 convs = lookup_conversions (fromtype);
2464 candidates = 0;
2465 flags |= LOOKUP_NO_CONVERSION;
2467 if (ctors)
2469 tree t;
2471 ctors = BASELINK_FUNCTIONS (ctors);
2473 t = build_int_2 (0, 0);
2474 TREE_TYPE (t) = build_pointer_type (totype);
2475 args = build_tree_list (NULL_TREE, expr);
2476 /* We should never try to call the abstract or base constructor
2477 from here. */
2478 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2479 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2480 20011226);
2481 args = tree_cons (NULL_TREE, t, args);
2483 for (; ctors; ctors = OVL_NEXT (ctors))
2485 tree ctor = OVL_CURRENT (ctors);
2486 if (DECL_NONCONVERTING_P (ctor))
2487 continue;
2489 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2490 cand = add_template_candidate (&candidates, ctor, totype,
2491 NULL_TREE, args, NULL_TREE,
2492 TYPE_BINFO (totype),
2493 TYPE_BINFO (totype),
2494 flags,
2495 DEDUCE_CALL);
2496 else
2497 cand = add_function_candidate (&candidates, ctor, totype,
2498 args, TYPE_BINFO (totype),
2499 TYPE_BINFO (totype),
2500 flags);
2502 if (cand)
2503 cand->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2506 if (convs)
2507 args = build_tree_list (NULL_TREE, build_this (expr));
2509 for (; convs; convs = TREE_CHAIN (convs))
2511 tree fns;
2512 tree conversion_path = TREE_PURPOSE (convs);
2513 int convflags = LOOKUP_NO_CONVERSION;
2515 /* If we are called to convert to a reference type, we are trying to
2516 find an lvalue binding, so don't even consider temporaries. If
2517 we don't find an lvalue binding, the caller will try again to
2518 look for a temporary binding. */
2519 if (TREE_CODE (totype) == REFERENCE_TYPE)
2520 convflags |= LOOKUP_NO_TEMP_BIND;
2522 for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
2524 tree fn = OVL_CURRENT (fns);
2526 /* [over.match.funcs] For conversion functions, the function
2527 is considered to be a member of the class of the implicit
2528 object argument for the purpose of defining the type of
2529 the implicit object parameter.
2531 So we pass fromtype as CTYPE to add_*_candidate. */
2533 if (TREE_CODE (fn) == TEMPLATE_DECL)
2534 cand = add_template_candidate (&candidates, fn, fromtype,
2535 NULL_TREE,
2536 args, totype,
2537 TYPE_BINFO (fromtype),
2538 conversion_path,
2539 flags,
2540 DEDUCE_CONV);
2541 else
2542 cand = add_function_candidate (&candidates, fn, fromtype,
2543 args,
2544 TYPE_BINFO (fromtype),
2545 conversion_path,
2546 flags);
2548 if (cand)
2550 tree ics = implicit_conversion (totype,
2551 TREE_TYPE (TREE_TYPE (cand->fn)),
2552 0, convflags);
2554 cand->second_conv = ics;
2556 if (ics == NULL_TREE)
2557 cand->viable = 0;
2558 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2559 cand->viable = -1;
2564 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2565 if (!any_viable_p)
2566 return 0;
2568 cand = tourney (candidates);
2569 if (cand == 0)
2571 if (flags & LOOKUP_COMPLAIN)
2573 error ("conversion from `%T' to `%T' is ambiguous",
2574 fromtype, totype);
2575 print_z_candidates (candidates);
2578 cand = candidates; /* any one will do */
2579 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2580 ICS_USER_FLAG (cand->second_conv) = 1;
2581 if (!any_strictly_viable (candidates))
2582 ICS_BAD_FLAG (cand->second_conv) = 1;
2583 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2584 ambiguous conversion is no worse than another user-defined
2585 conversion. */
2587 return cand;
2590 /* Build the user conversion sequence. */
2591 convs = build_conv
2592 (USER_CONV,
2593 (DECL_CONSTRUCTOR_P (cand->fn)
2594 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2595 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
2596 TREE_OPERAND (convs, 1) = build_zc_wrapper (cand);
2598 /* Combine it with the second conversion sequence. */
2599 cand->second_conv = merge_conversion_sequences (convs,
2600 cand->second_conv);
2602 if (cand->viable == -1)
2603 ICS_BAD_FLAG (cand->second_conv) = 1;
2605 return cand;
2608 tree
2609 build_user_type_conversion (tree totype, tree expr, int flags)
2611 struct z_candidate *cand
2612 = build_user_type_conversion_1 (totype, expr, flags);
2614 if (cand)
2616 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2617 return error_mark_node;
2618 return convert_from_reference (convert_like (cand->second_conv, expr));
2620 return NULL_TREE;
2623 /* Do any initial processing on the arguments to a function call. */
2625 static tree
2626 resolve_args (tree args)
2628 tree t;
2629 for (t = args; t; t = TREE_CHAIN (t))
2631 tree arg = TREE_VALUE (t);
2633 if (arg == error_mark_node)
2634 return error_mark_node;
2635 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2637 error ("invalid use of void expression");
2638 return error_mark_node;
2640 arg = convert_from_reference (arg);
2641 TREE_VALUE (t) = arg;
2643 return args;
2646 /* Perform overload resolution on FN, which is called with the ARGS.
2648 Return the candidate function selected by overload resolution, or
2649 NULL if the event that overload resolution failed. In the case
2650 that overload resolution fails, *CANDIDATES will be the set of
2651 candidates considered, and ANY_VIABLE_P will be set to true or
2652 false to indicate whether or not any of the candidates were
2653 viable.
2655 The ARGS should already have gone through RESOLVE_ARGS before this
2656 function is called. */
2658 static struct z_candidate *
2659 perform_overload_resolution (tree fn,
2660 tree args,
2661 struct z_candidate **candidates,
2662 bool *any_viable_p)
2664 struct z_candidate *cand;
2665 tree explicit_targs = NULL_TREE;
2666 int template_only = 0;
2668 *candidates = NULL;
2669 *any_viable_p = true;
2671 /* Check FN and ARGS. */
2672 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2673 || TREE_CODE (fn) == TEMPLATE_DECL
2674 || TREE_CODE (fn) == OVERLOAD
2675 || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2676 20020712);
2677 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2678 20020712);
2680 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2682 explicit_targs = TREE_OPERAND (fn, 1);
2683 fn = TREE_OPERAND (fn, 0);
2684 template_only = 1;
2687 /* Add the various candidate functions. */
2688 add_candidates (fn, args, explicit_targs, template_only,
2689 /*conversion_path=*/NULL_TREE,
2690 /*access_path=*/NULL_TREE,
2691 LOOKUP_NORMAL,
2692 candidates);
2694 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
2695 if (!*any_viable_p)
2696 return NULL;
2698 cand = tourney (*candidates);
2699 return cand;
2702 /* Return an expression for a call to FN (a namespace-scope function,
2703 or a static member function) with the ARGS. */
2705 tree
2706 build_new_function_call (tree fn, tree args)
2708 struct z_candidate *candidates, *cand;
2709 bool any_viable_p;
2711 args = resolve_args (args);
2712 if (args == error_mark_node)
2713 return error_mark_node;
2715 cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p);
2717 if (!cand)
2719 if (!any_viable_p && candidates && ! candidates->next)
2720 return build_function_call (candidates->fn, args);
2721 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2722 fn = TREE_OPERAND (fn, 0);
2723 if (!any_viable_p)
2724 error ("no matching function for call to `%D(%A)'",
2725 DECL_NAME (OVL_CURRENT (fn)), args);
2726 else
2727 error ("call of overloaded `%D(%A)' is ambiguous",
2728 DECL_NAME (OVL_CURRENT (fn)), args);
2729 if (candidates)
2730 print_z_candidates (candidates);
2731 return error_mark_node;
2734 return build_over_call (cand, LOOKUP_NORMAL);
2737 /* Build a call to a global operator new. FNNAME is the name of the
2738 operator (either "operator new" or "operator new[]") and ARGS are
2739 the arguments provided. *SIZE points to the total number of bytes
2740 required by the allocation, and is updated if that is changed here.
2741 *COOKIE_SIZE is non-NULL if a cookie should be used. If this
2742 function determines that no cookie should be used, after all,
2743 *COOKIE_SIZE is set to NULL_TREE. */
2745 tree
2746 build_operator_new_call (tree fnname, tree args, tree *size, tree *cookie_size)
2748 tree fns;
2749 struct z_candidate *candidates;
2750 struct z_candidate *cand;
2751 bool any_viable_p;
2753 args = tree_cons (NULL_TREE, *size, args);
2754 args = resolve_args (args);
2755 if (args == error_mark_node)
2756 return args;
2758 fns = lookup_function_nonclass (fnname, args);
2760 /* Figure out what function is being called. */
2761 cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p);
2763 /* If no suitable function could be found, issue an error message
2764 and give up. */
2765 if (!cand)
2767 if (!any_viable_p)
2768 error ("no matching function for call to `%D(%A)'",
2769 DECL_NAME (OVL_CURRENT (fns)), args);
2770 else
2771 error ("call of overloaded `%D(%A)' is ambiguous",
2772 DECL_NAME (OVL_CURRENT (fns)), args);
2773 if (candidates)
2774 print_z_candidates (candidates);
2775 return error_mark_node;
2778 /* If a cookie is required, add some extra space. Whether
2779 or not a cookie is required cannot be determined until
2780 after we know which function was called. */
2781 if (*cookie_size)
2783 bool use_cookie = true;
2784 if (!abi_version_at_least (2))
2786 tree placement = TREE_CHAIN (args);
2787 /* In G++ 3.2, the check was implemented incorrectly; it
2788 looked at the placement expression, rather than the
2789 type of the function. */
2790 if (placement && !TREE_CHAIN (placement)
2791 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2792 ptr_type_node))
2793 use_cookie = false;
2795 else
2797 tree arg_types;
2799 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
2800 /* Skip the size_t parameter. */
2801 arg_types = TREE_CHAIN (arg_types);
2802 /* Check the remaining parameters (if any). */
2803 if (arg_types
2804 && TREE_CHAIN (arg_types) == void_list_node
2805 && same_type_p (TREE_VALUE (arg_types),
2806 ptr_type_node))
2807 use_cookie = false;
2809 /* If we need a cookie, adjust the number of bytes allocated. */
2810 if (use_cookie)
2812 /* Update the total size. */
2813 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
2814 /* Update the argument list to reflect the adjusted size. */
2815 TREE_VALUE (args) = *size;
2817 else
2818 *cookie_size = NULL_TREE;
2821 /* Build the CALL_EXPR. */
2822 return build_over_call (cand, LOOKUP_NORMAL);
2825 static tree
2826 build_object_call (tree obj, tree args)
2828 struct z_candidate *candidates = 0, *cand;
2829 tree fns, convs, mem_args = NULL_TREE;
2830 tree type = TREE_TYPE (obj);
2831 bool any_viable_p;
2833 if (TYPE_PTRMEMFUNC_P (type))
2835 /* It's no good looking for an overloaded operator() on a
2836 pointer-to-member-function. */
2837 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2838 return error_mark_node;
2841 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2842 if (fns == error_mark_node)
2843 return error_mark_node;
2845 args = resolve_args (args);
2847 if (args == error_mark_node)
2848 return error_mark_node;
2850 if (fns)
2852 tree base = BINFO_TYPE (BASELINK_BINFO (fns));
2853 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2855 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2857 tree fn = OVL_CURRENT (fns);
2858 if (TREE_CODE (fn) == TEMPLATE_DECL)
2859 add_template_candidate (&candidates, fn, base, NULL_TREE,
2860 mem_args, NULL_TREE,
2861 TYPE_BINFO (type),
2862 TYPE_BINFO (type),
2863 LOOKUP_NORMAL, DEDUCE_CALL);
2864 else
2865 add_function_candidate
2866 (&candidates, fn, base, mem_args, TYPE_BINFO (type),
2867 TYPE_BINFO (type), LOOKUP_NORMAL);
2871 convs = lookup_conversions (type);
2873 for (; convs; convs = TREE_CHAIN (convs))
2875 tree fns = TREE_VALUE (convs);
2876 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2878 if ((TREE_CODE (totype) == POINTER_TYPE
2879 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2880 || (TREE_CODE (totype) == REFERENCE_TYPE
2881 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2882 || (TREE_CODE (totype) == REFERENCE_TYPE
2883 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2884 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2885 for (; fns; fns = OVL_NEXT (fns))
2887 tree fn = OVL_CURRENT (fns);
2888 if (TREE_CODE (fn) == TEMPLATE_DECL)
2889 add_template_conv_candidate
2890 (&candidates, fn, obj, args, totype,
2891 /*access_path=*/NULL_TREE,
2892 /*conversion_path=*/NULL_TREE);
2893 else
2894 add_conv_candidate (&candidates, fn, obj, args,
2895 /*conversion_path=*/NULL_TREE,
2896 /*access_path=*/NULL_TREE);
2900 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2901 if (!any_viable_p)
2903 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2904 print_z_candidates (candidates);
2905 return error_mark_node;
2908 cand = tourney (candidates);
2909 if (cand == 0)
2911 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2912 print_z_candidates (candidates);
2913 return error_mark_node;
2916 /* Since cand->fn will be a type, not a function, for a conversion
2917 function, we must be careful not to unconditionally look at
2918 DECL_NAME here. */
2919 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2920 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2921 return build_over_call (cand, LOOKUP_NORMAL);
2923 obj = convert_like_with_context
2924 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2926 /* FIXME */
2927 return build_function_call (obj, args);
2930 static void
2931 op_error (enum tree_code code, enum tree_code code2,
2932 tree arg1, tree arg2, tree arg3, const char *problem)
2934 const char *opname;
2936 if (code == MODIFY_EXPR)
2937 opname = assignment_operator_name_info[code2].name;
2938 else
2939 opname = operator_name_info[code].name;
2941 switch (code)
2943 case COND_EXPR:
2944 error ("%s for ternary 'operator?:' in '%E ? %E : %E'",
2945 problem, arg1, arg2, arg3);
2946 break;
2948 case POSTINCREMENT_EXPR:
2949 case POSTDECREMENT_EXPR:
2950 error ("%s for 'operator%s' in '%E%s'", problem, opname, arg1, opname);
2951 break;
2953 case ARRAY_REF:
2954 error ("%s for 'operator[]' in '%E[%E]'", problem, arg1, arg2);
2955 break;
2957 case REALPART_EXPR:
2958 case IMAGPART_EXPR:
2959 error ("%s for '%s' in '%s %E'", problem, opname, opname, arg1);
2960 break;
2962 default:
2963 if (arg2)
2964 error ("%s for 'operator%s' in '%E %s %E'",
2965 problem, opname, arg1, opname, arg2);
2966 else
2967 error ("%s for 'operator%s' in '%s%E'",
2968 problem, opname, opname, arg1);
2969 break;
2973 /* Return the implicit conversion sequence that could be used to
2974 convert E1 to E2 in [expr.cond]. */
2976 static tree
2977 conditional_conversion (tree e1, tree e2)
2979 tree t1 = non_reference (TREE_TYPE (e1));
2980 tree t2 = non_reference (TREE_TYPE (e2));
2981 tree conv;
2982 bool good_base;
2984 /* [expr.cond]
2986 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2987 implicitly converted (clause _conv_) to the type "reference to
2988 T2", subject to the constraint that in the conversion the
2989 reference must bind directly (_dcl.init.ref_) to E1. */
2990 if (real_lvalue_p (e2))
2992 conv = implicit_conversion (build_reference_type (t2),
2995 LOOKUP_NO_TEMP_BIND);
2996 if (conv)
2997 return conv;
3000 /* [expr.cond]
3002 If E1 and E2 have class type, and the underlying class types are
3003 the same or one is a base class of the other: E1 can be converted
3004 to match E2 if the class of T2 is the same type as, or a base
3005 class of, the class of T1, and the cv-qualification of T2 is the
3006 same cv-qualification as, or a greater cv-qualification than, the
3007 cv-qualification of T1. If the conversion is applied, E1 is
3008 changed to an rvalue of type T2 that still refers to the original
3009 source class object (or the appropriate subobject thereof).
3011 FIXME we can't express an rvalue that refers to the original object;
3012 we have to create a new one. */
3013 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
3014 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
3016 if (good_base && at_least_as_qualified_p (t2, t1))
3018 conv = build1 (IDENTITY_CONV, t1, e1);
3019 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
3020 TYPE_MAIN_VARIANT (t2)))
3022 conv = build_conv (BASE_CONV, t2, conv);
3023 NEED_TEMPORARY_P (conv) = 1;
3025 else
3026 conv = build_conv (RVALUE_CONV, t2, conv);
3027 return conv;
3029 else
3030 return NULL_TREE;
3032 else
3033 /* [expr.cond]
3035 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
3036 converted to the type that expression E2 would have if E2 were
3037 converted to an rvalue (or the type it has, if E2 is an rvalue). */
3038 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
3041 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
3042 arguments to the conditional expression. */
3044 tree
3045 build_conditional_expr (tree arg1, tree arg2, tree arg3)
3047 tree arg2_type;
3048 tree arg3_type;
3049 tree result;
3050 tree result_type = NULL_TREE;
3051 bool lvalue_p = true;
3052 struct z_candidate *candidates = 0;
3053 struct z_candidate *cand;
3055 /* As a G++ extension, the second argument to the conditional can be
3056 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
3057 c'.) If the second operand is omitted, make sure it is
3058 calculated only once. */
3059 if (!arg2)
3061 if (pedantic)
3062 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
3064 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
3065 if (real_lvalue_p (arg1))
3066 arg2 = arg1 = stabilize_reference (arg1);
3067 else
3068 arg2 = arg1 = save_expr (arg1);
3071 /* [expr.cond]
3073 The first expr ession is implicitly converted to bool (clause
3074 _conv_). */
3075 arg1 = perform_implicit_conversion (boolean_type_node, arg1);
3077 /* If something has already gone wrong, just pass that fact up the
3078 tree. */
3079 if (error_operand_p (arg1)
3080 || error_operand_p (arg2)
3081 || error_operand_p (arg3))
3082 return error_mark_node;
3084 /* [expr.cond]
3086 If either the second or the third operand has type (possibly
3087 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3088 array-to-pointer (_conv.array_), and function-to-pointer
3089 (_conv.func_) standard conversions are performed on the second
3090 and third operands. */
3091 arg2_type = TREE_TYPE (arg2);
3092 arg3_type = TREE_TYPE (arg3);
3093 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3095 /* Do the conversions. We don't these for `void' type arguments
3096 since it can't have any effect and since decay_conversion
3097 does not handle that case gracefully. */
3098 if (!VOID_TYPE_P (arg2_type))
3099 arg2 = decay_conversion (arg2);
3100 if (!VOID_TYPE_P (arg3_type))
3101 arg3 = decay_conversion (arg3);
3102 arg2_type = TREE_TYPE (arg2);
3103 arg3_type = TREE_TYPE (arg3);
3105 /* [expr.cond]
3107 One of the following shall hold:
3109 --The second or the third operand (but not both) is a
3110 throw-expression (_except.throw_); the result is of the
3111 type of the other and is an rvalue.
3113 --Both the second and the third operands have type void; the
3114 result is of type void and is an rvalue. */
3115 if ((TREE_CODE (arg2) == THROW_EXPR)
3116 ^ (TREE_CODE (arg3) == THROW_EXPR))
3117 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
3118 ? arg3_type : arg2_type);
3119 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3120 result_type = void_type_node;
3121 else
3123 error ("`%E' has type `void' and is not a throw-expression",
3124 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3125 return error_mark_node;
3128 lvalue_p = false;
3129 goto valid_operands;
3131 /* [expr.cond]
3133 Otherwise, if the second and third operand have different types,
3134 and either has (possibly cv-qualified) class type, an attempt is
3135 made to convert each of those operands to the type of the other. */
3136 else if (!same_type_p (arg2_type, arg3_type)
3137 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3139 tree conv2 = conditional_conversion (arg2, arg3);
3140 tree conv3 = conditional_conversion (arg3, arg2);
3142 /* [expr.cond]
3144 If both can be converted, or one can be converted but the
3145 conversion is ambiguous, the program is ill-formed. If
3146 neither can be converted, the operands are left unchanged and
3147 further checking is performed as described below. If exactly
3148 one conversion is possible, that conversion is applied to the
3149 chosen operand and the converted operand is used in place of
3150 the original operand for the remainder of this section. */
3151 if ((conv2 && !ICS_BAD_FLAG (conv2)
3152 && conv3 && !ICS_BAD_FLAG (conv3))
3153 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3154 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3156 error ("operands to ?: have different types");
3157 return error_mark_node;
3159 else if (conv2 && !ICS_BAD_FLAG (conv2))
3161 arg2 = convert_like (conv2, arg2);
3162 arg2 = convert_from_reference (arg2);
3163 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
3164 abort ();
3165 arg2_type = TREE_TYPE (arg2);
3167 else if (conv3 && !ICS_BAD_FLAG (conv3))
3169 arg3 = convert_like (conv3, arg3);
3170 arg3 = convert_from_reference (arg3);
3171 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
3172 abort ();
3173 arg3_type = TREE_TYPE (arg3);
3177 /* [expr.cond]
3179 If the second and third operands are lvalues and have the same
3180 type, the result is of that type and is an lvalue. */
3181 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
3182 same_type_p (arg2_type, arg3_type))
3184 result_type = arg2_type;
3185 goto valid_operands;
3188 /* [expr.cond]
3190 Otherwise, the result is an rvalue. If the second and third
3191 operand do not have the same type, and either has (possibly
3192 cv-qualified) class type, overload resolution is used to
3193 determine the conversions (if any) to be applied to the operands
3194 (_over.match.oper_, _over.built_). */
3195 lvalue_p = false;
3196 if (!same_type_p (arg2_type, arg3_type)
3197 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3199 tree args[3];
3200 tree conv;
3201 bool any_viable_p;
3203 /* Rearrange the arguments so that add_builtin_candidate only has
3204 to know about two args. In build_builtin_candidates, the
3205 arguments are unscrambled. */
3206 args[0] = arg2;
3207 args[1] = arg3;
3208 args[2] = arg1;
3209 add_builtin_candidates (&candidates,
3210 COND_EXPR,
3211 NOP_EXPR,
3212 ansi_opname (COND_EXPR),
3213 args,
3214 LOOKUP_NORMAL);
3216 /* [expr.cond]
3218 If the overload resolution fails, the program is
3219 ill-formed. */
3220 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3221 if (!any_viable_p)
3223 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3224 print_z_candidates (candidates);
3225 return error_mark_node;
3227 cand = tourney (candidates);
3228 if (!cand)
3230 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3231 print_z_candidates (candidates);
3232 return error_mark_node;
3235 /* [expr.cond]
3237 Otherwise, the conversions thus determined are applied, and
3238 the converted operands are used in place of the original
3239 operands for the remainder of this section. */
3240 conv = TREE_VEC_ELT (cand->convs, 0);
3241 arg1 = convert_like (conv, arg1);
3242 conv = TREE_VEC_ELT (cand->convs, 1);
3243 arg2 = convert_like (conv, arg2);
3244 conv = TREE_VEC_ELT (cand->convs, 2);
3245 arg3 = convert_like (conv, arg3);
3248 /* [expr.cond]
3250 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3251 and function-to-pointer (_conv.func_) standard conversions are
3252 performed on the second and third operands.
3254 We need to force the lvalue-to-rvalue conversion here for class types,
3255 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3256 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3257 regions.
3259 We use ocp_convert rather than build_user_type_conversion because the
3260 latter returns NULL_TREE on failure, while the former gives an error. */
3262 arg2 = force_rvalue (arg2);
3263 arg2_type = TREE_TYPE (arg2);
3265 arg3 = force_rvalue (arg3);
3266 arg3_type = TREE_TYPE (arg3);
3268 if (arg2 == error_mark_node || arg3 == error_mark_node)
3269 return error_mark_node;
3271 /* [expr.cond]
3273 After those conversions, one of the following shall hold:
3275 --The second and third operands have the same type; the result is of
3276 that type. */
3277 if (same_type_p (arg2_type, arg3_type))
3278 result_type = arg2_type;
3279 /* [expr.cond]
3281 --The second and third operands have arithmetic or enumeration
3282 type; the usual arithmetic conversions are performed to bring
3283 them to a common type, and the result is of that type. */
3284 else if ((ARITHMETIC_TYPE_P (arg2_type)
3285 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3286 && (ARITHMETIC_TYPE_P (arg3_type)
3287 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3289 /* In this case, there is always a common type. */
3290 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3291 arg3_type);
3293 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3294 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3295 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3296 arg2_type, arg3_type);
3297 else if (extra_warnings
3298 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3299 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3300 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3301 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3302 warning ("enumeral and non-enumeral type in conditional expression");
3304 arg2 = perform_implicit_conversion (result_type, arg2);
3305 arg3 = perform_implicit_conversion (result_type, arg3);
3307 /* [expr.cond]
3309 --The second and third operands have pointer type, or one has
3310 pointer type and the other is a null pointer constant; pointer
3311 conversions (_conv.ptr_) and qualification conversions
3312 (_conv.qual_) are performed to bring them to their composite
3313 pointer type (_expr.rel_). The result is of the composite
3314 pointer type.
3316 --The second and third operands have pointer to member type, or
3317 one has pointer to member type and the other is a null pointer
3318 constant; pointer to member conversions (_conv.mem_) and
3319 qualification conversions (_conv.qual_) are performed to bring
3320 them to a common type, whose cv-qualification shall match the
3321 cv-qualification of either the second or the third operand.
3322 The result is of the common type. */
3323 else if ((null_ptr_cst_p (arg2)
3324 && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
3325 || (null_ptr_cst_p (arg3)
3326 && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
3327 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3328 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3329 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
3331 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3332 arg3, "conditional expression");
3333 if (result_type == error_mark_node)
3334 return error_mark_node;
3335 arg2 = perform_implicit_conversion (result_type, arg2);
3336 arg3 = perform_implicit_conversion (result_type, arg3);
3339 if (!result_type)
3341 error ("operands to ?: have different types");
3342 return error_mark_node;
3345 valid_operands:
3346 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3347 /* We can't use result_type below, as fold might have returned a
3348 throw_expr. */
3350 /* Expand both sides into the same slot, hopefully the target of the
3351 ?: expression. We used to check for TARGET_EXPRs here, but now we
3352 sometimes wrap them in NOP_EXPRs so the test would fail. */
3353 if (!lvalue_p && IS_AGGR_TYPE (TREE_TYPE (result)))
3354 result = get_target_expr (result);
3356 /* If this expression is an rvalue, but might be mistaken for an
3357 lvalue, we must add a NON_LVALUE_EXPR. */
3358 if (!lvalue_p && real_lvalue_p (result))
3359 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
3361 return result;
3364 /* OPERAND is an operand to an expression. Perform necessary steps
3365 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
3366 returned. */
3368 static tree
3369 prep_operand (tree operand)
3371 if (operand)
3373 operand = convert_from_reference (operand);
3374 if (CLASS_TYPE_P (TREE_TYPE (operand))
3375 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
3376 /* Make sure the template type is instantiated now. */
3377 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
3380 return operand;
3383 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
3384 OVERLOAD) to the CANDIDATES, returning an updated list of
3385 CANDIDATES. The ARGS are the arguments provided to the call,
3386 without any implicit object parameter. The EXPLICIT_TARGS are
3387 explicit template arguments provided. TEMPLATE_ONLY is true if
3388 only template fucntions should be considered. CONVERSION_PATH,
3389 ACCESS_PATH, and FLAGS are as for add_function_candidate. */
3391 static void
3392 add_candidates (tree fns, tree args,
3393 tree explicit_targs, bool template_only,
3394 tree conversion_path, tree access_path,
3395 int flags,
3396 struct z_candidate **candidates)
3398 tree ctype;
3399 tree non_static_args;
3401 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
3402 /* Delay creating the implicit this parameter until it is needed. */
3403 non_static_args = NULL_TREE;
3405 while (fns)
3407 tree fn;
3408 tree fn_args;
3410 fn = OVL_CURRENT (fns);
3411 /* Figure out which set of arguments to use. */
3412 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3414 /* If this function is a non-static member, prepend the implicit
3415 object parameter. */
3416 if (!non_static_args)
3417 non_static_args = tree_cons (NULL_TREE,
3418 build_this (TREE_VALUE (args)),
3419 TREE_CHAIN (args));
3420 fn_args = non_static_args;
3422 else
3423 /* Otherwise, just use the list of arguments provided. */
3424 fn_args = args;
3426 if (TREE_CODE (fn) == TEMPLATE_DECL)
3427 add_template_candidate (candidates,
3428 fn,
3429 ctype,
3430 explicit_targs,
3431 fn_args,
3432 NULL_TREE,
3433 access_path,
3434 conversion_path,
3435 flags,
3436 DEDUCE_CALL);
3437 else if (!template_only)
3438 add_function_candidate (candidates,
3440 ctype,
3441 fn_args,
3442 access_path,
3443 conversion_path,
3444 flags);
3445 fns = OVL_NEXT (fns);
3449 tree
3450 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3)
3452 struct z_candidate *candidates = 0, *cand;
3453 tree arglist, fnname;
3454 tree args[3];
3455 enum tree_code code2 = NOP_EXPR;
3456 tree conv;
3457 bool strict_p;
3458 bool any_viable_p;
3460 if (error_operand_p (arg1)
3461 || error_operand_p (arg2)
3462 || error_operand_p (arg3))
3463 return error_mark_node;
3465 if (code == MODIFY_EXPR)
3467 code2 = TREE_CODE (arg3);
3468 arg3 = NULL_TREE;
3469 fnname = ansi_assopname (code2);
3471 else
3472 fnname = ansi_opname (code);
3474 arg1 = prep_operand (arg1);
3476 switch (code)
3478 case NEW_EXPR:
3479 case VEC_NEW_EXPR:
3480 case VEC_DELETE_EXPR:
3481 case DELETE_EXPR:
3482 /* Use build_op_new_call and build_op_delete_call instead. */
3483 abort ();
3485 case CALL_EXPR:
3486 return build_object_call (arg1, arg2);
3488 default:
3489 break;
3492 arg2 = prep_operand (arg2);
3493 arg3 = prep_operand (arg3);
3495 if (code == COND_EXPR)
3497 if (arg2 == NULL_TREE
3498 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3499 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3500 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3501 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3502 goto builtin;
3504 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3505 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3506 goto builtin;
3508 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3509 arg2 = integer_zero_node;
3511 arglist = NULL_TREE;
3512 if (arg3)
3513 arglist = tree_cons (NULL_TREE, arg3, arglist);
3514 if (arg2)
3515 arglist = tree_cons (NULL_TREE, arg2, arglist);
3516 arglist = tree_cons (NULL_TREE, arg1, arglist);
3518 /* Add namespace-scope operators to the list of functions to
3519 consider. */
3520 add_candidates (lookup_function_nonclass (fnname, arglist),
3521 arglist, NULL_TREE, false, NULL_TREE, NULL_TREE,
3522 flags, &candidates);
3523 /* Add class-member operators to the candidate set. */
3524 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
3526 tree fns;
3528 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3529 if (fns == error_mark_node)
3530 return fns;
3531 if (fns)
3532 add_candidates (BASELINK_FUNCTIONS (fns), arglist,
3533 NULL_TREE, false,
3534 BASELINK_BINFO (fns),
3535 TYPE_BINFO (TREE_TYPE (arg1)),
3536 flags, &candidates);
3539 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3540 to know about two args; a builtin candidate will always have a first
3541 parameter of type bool. We'll handle that in
3542 build_builtin_candidate. */
3543 if (code == COND_EXPR)
3545 args[0] = arg2;
3546 args[1] = arg3;
3547 args[2] = arg1;
3549 else
3551 args[0] = arg1;
3552 args[1] = arg2;
3553 args[2] = NULL_TREE;
3556 add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
3558 switch (code)
3560 case COMPOUND_EXPR:
3561 case ADDR_EXPR:
3562 /* For these, the built-in candidates set is empty
3563 [over.match.oper]/3. We don't want non-strict matches
3564 because exact matches are always possible with built-in
3565 operators. The built-in candidate set for COMPONENT_REF
3566 would be empty too, but since there are no such built-in
3567 operators, we accept non-strict matches for them. */
3568 strict_p = true;
3569 break;
3571 default:
3572 strict_p = pedantic;
3573 break;
3576 candidates = splice_viable (candidates, strict_p, &any_viable_p);
3577 if (!any_viable_p)
3579 switch (code)
3581 case POSTINCREMENT_EXPR:
3582 case POSTDECREMENT_EXPR:
3583 /* Look for an `operator++ (int)'. If they didn't have
3584 one, then we fall back to the old way of doing things. */
3585 if (flags & LOOKUP_COMPLAIN)
3586 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3587 fnname,
3588 operator_name_info[code].name);
3589 if (code == POSTINCREMENT_EXPR)
3590 code = PREINCREMENT_EXPR;
3591 else
3592 code = PREDECREMENT_EXPR;
3593 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3595 /* The caller will deal with these. */
3596 case ADDR_EXPR:
3597 case COMPOUND_EXPR:
3598 case COMPONENT_REF:
3599 return NULL_TREE;
3601 default:
3602 break;
3604 if (flags & LOOKUP_COMPLAIN)
3606 op_error (code, code2, arg1, arg2, arg3, "no match");
3607 print_z_candidates (candidates);
3609 return error_mark_node;
3612 cand = tourney (candidates);
3613 if (cand == 0)
3615 if (flags & LOOKUP_COMPLAIN)
3617 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3618 print_z_candidates (candidates);
3620 return error_mark_node;
3623 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3625 if (warn_synth
3626 && fnname == ansi_assopname (NOP_EXPR)
3627 && DECL_ARTIFICIAL (cand->fn)
3628 && candidates->next
3629 && ! candidates->next->next)
3631 warning ("using synthesized `%#D' for copy assignment",
3632 cand->fn);
3633 cp_warning_at (" where cfront would use `%#D'",
3634 cand == candidates
3635 ? candidates->next->fn
3636 : candidates->fn);
3639 return build_over_call (cand, LOOKUP_NORMAL);
3642 /* Check for comparison of different enum types. */
3643 switch (code)
3645 case GT_EXPR:
3646 case LT_EXPR:
3647 case GE_EXPR:
3648 case LE_EXPR:
3649 case EQ_EXPR:
3650 case NE_EXPR:
3651 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3652 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3653 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3654 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3656 warning ("comparison between `%#T' and `%#T'",
3657 TREE_TYPE (arg1), TREE_TYPE (arg2));
3659 break;
3660 default:
3661 break;
3664 /* We need to strip any leading REF_BIND so that bitfields don't cause
3665 errors. This should not remove any important conversions, because
3666 builtins don't apply to class objects directly. */
3667 conv = TREE_VEC_ELT (cand->convs, 0);
3668 if (TREE_CODE (conv) == REF_BIND)
3669 conv = TREE_OPERAND (conv, 0);
3670 arg1 = convert_like (conv, arg1);
3671 if (arg2)
3673 conv = TREE_VEC_ELT (cand->convs, 1);
3674 if (TREE_CODE (conv) == REF_BIND)
3675 conv = TREE_OPERAND (conv, 0);
3676 arg2 = convert_like (conv, arg2);
3678 if (arg3)
3680 conv = TREE_VEC_ELT (cand->convs, 2);
3681 if (TREE_CODE (conv) == REF_BIND)
3682 conv = TREE_OPERAND (conv, 0);
3683 arg3 = convert_like (conv, arg3);
3686 builtin:
3687 switch (code)
3689 case MODIFY_EXPR:
3690 return build_modify_expr (arg1, code2, arg2);
3692 case INDIRECT_REF:
3693 return build_indirect_ref (arg1, "unary *");
3695 case PLUS_EXPR:
3696 case MINUS_EXPR:
3697 case MULT_EXPR:
3698 case TRUNC_DIV_EXPR:
3699 case GT_EXPR:
3700 case LT_EXPR:
3701 case GE_EXPR:
3702 case LE_EXPR:
3703 case EQ_EXPR:
3704 case NE_EXPR:
3705 case MAX_EXPR:
3706 case MIN_EXPR:
3707 case LSHIFT_EXPR:
3708 case RSHIFT_EXPR:
3709 case TRUNC_MOD_EXPR:
3710 case BIT_AND_EXPR:
3711 case BIT_IOR_EXPR:
3712 case BIT_XOR_EXPR:
3713 case TRUTH_ANDIF_EXPR:
3714 case TRUTH_ORIF_EXPR:
3715 return cp_build_binary_op (code, arg1, arg2);
3717 case CONVERT_EXPR:
3718 case NEGATE_EXPR:
3719 case BIT_NOT_EXPR:
3720 case TRUTH_NOT_EXPR:
3721 case PREINCREMENT_EXPR:
3722 case POSTINCREMENT_EXPR:
3723 case PREDECREMENT_EXPR:
3724 case POSTDECREMENT_EXPR:
3725 case REALPART_EXPR:
3726 case IMAGPART_EXPR:
3727 return build_unary_op (code, arg1, candidates != 0);
3729 case ARRAY_REF:
3730 return build_array_ref (arg1, arg2);
3732 case COND_EXPR:
3733 return build_conditional_expr (arg1, arg2, arg3);
3735 case MEMBER_REF:
3736 return build_m_component_ref
3737 (build_indirect_ref (arg1, NULL), arg2);
3739 /* The caller will deal with these. */
3740 case ADDR_EXPR:
3741 case COMPONENT_REF:
3742 case COMPOUND_EXPR:
3743 return NULL_TREE;
3745 default:
3746 abort ();
3747 return NULL_TREE;
3751 /* Build a call to operator delete. This has to be handled very specially,
3752 because the restrictions on what signatures match are different from all
3753 other call instances. For a normal delete, only a delete taking (void *)
3754 or (void *, size_t) is accepted. For a placement delete, only an exact
3755 match with the placement new is accepted.
3757 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3758 ADDR is the pointer to be deleted.
3759 SIZE is the size of the memory block to be deleted.
3760 FLAGS are the usual overloading flags.
3761 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3763 tree
3764 build_op_delete_call (enum tree_code code, tree addr, tree size,
3765 int flags, tree placement)
3767 tree fn = NULL_TREE;
3768 tree fns, fnname, argtypes, args, type;
3769 int pass;
3771 if (addr == error_mark_node)
3772 return error_mark_node;
3774 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
3776 fnname = ansi_opname (code);
3778 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3779 /* In [class.free]
3781 If the result of the lookup is ambiguous or inaccessible, or if
3782 the lookup selects a placement deallocation function, the
3783 program is ill-formed.
3785 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3787 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3788 if (fns == error_mark_node)
3789 return error_mark_node;
3791 else
3792 fns = NULL_TREE;
3794 if (fns == NULL_TREE)
3795 fns = lookup_name_nonclass (fnname);
3797 if (placement)
3799 tree alloc_fn;
3800 tree call_expr;
3802 /* Find the allocation function that is being called. */
3803 call_expr = placement;
3804 /* Sometimes we have a COMPOUND_EXPR, rather than a simple
3805 CALL_EXPR. */
3806 while (TREE_CODE (call_expr) == COMPOUND_EXPR)
3807 call_expr = TREE_OPERAND (call_expr, 1);
3808 /* Extract the function. */
3809 alloc_fn = get_callee_fndecl (call_expr);
3810 my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3811 /* Then the second parm type. */
3812 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3813 /* Also the second argument. */
3814 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3816 else
3818 /* First try it without the size argument. */
3819 argtypes = void_list_node;
3820 args = NULL_TREE;
3823 /* Strip const and volatile from addr. */
3824 addr = cp_convert (ptr_type_node, addr);
3826 /* We make two tries at finding a matching `operator delete'. On
3827 the first pass, we look for a one-operator (or placement)
3828 operator delete. If we're not doing placement delete, then on
3829 the second pass we look for a two-argument delete. */
3830 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3832 /* Go through the `operator delete' functions looking for one
3833 with a matching type. */
3834 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3835 fn;
3836 fn = OVL_NEXT (fn))
3838 tree t;
3840 /* The first argument must be "void *". */
3841 t = TYPE_ARG_TYPES (TREE_TYPE (OVL_CURRENT (fn)));
3842 if (!same_type_p (TREE_VALUE (t), ptr_type_node))
3843 continue;
3844 t = TREE_CHAIN (t);
3845 /* On the first pass, check the rest of the arguments. */
3846 if (pass == 0)
3848 while (argtypes && t)
3850 if (!same_type_p (TREE_VALUE (argtypes),
3851 TREE_VALUE (t)))
3852 break;
3853 argtypes = TREE_CHAIN (argtypes);
3854 t = TREE_CHAIN (t);
3856 if (!argtypes && !t)
3857 break;
3859 /* On the second pass, the second argument must be
3860 "size_t". */
3861 else if (pass == 1
3862 && same_type_p (TREE_VALUE (t), sizetype)
3863 && TREE_CHAIN (t) == void_list_node)
3864 break;
3867 /* If we found a match, we're done. */
3868 if (fn)
3869 break;
3872 /* If we have a matching function, call it. */
3873 if (fn)
3875 /* Make sure we have the actual function, and not an
3876 OVERLOAD. */
3877 fn = OVL_CURRENT (fn);
3879 /* If the FN is a member function, make sure that it is
3880 accessible. */
3881 if (DECL_CLASS_SCOPE_P (fn))
3882 perform_or_defer_access_check (TYPE_BINFO (type), fn);
3884 if (pass == 0)
3885 args = tree_cons (NULL_TREE, addr, args);
3886 else
3887 args = tree_cons (NULL_TREE, addr,
3888 build_tree_list (NULL_TREE, size));
3890 return build_function_call (fn, args);
3893 /* If we are doing placement delete we do nothing if we don't find a
3894 matching op delete. */
3895 if (placement)
3896 return NULL_TREE;
3898 error ("no suitable `operator %s' for `%T'",
3899 operator_name_info[(int)code].name, type);
3900 return error_mark_node;
3903 /* If the current scope isn't allowed to access DECL along
3904 BASETYPE_PATH, give an error. The most derived class in
3905 BASETYPE_PATH is the one used to qualify DECL. */
3907 bool
3908 enforce_access (tree basetype_path, tree decl)
3910 my_friendly_assert (TREE_CODE (basetype_path) == TREE_VEC, 20030624);
3912 if (!accessible_p (basetype_path, decl))
3914 if (TREE_PRIVATE (decl))
3915 cp_error_at ("`%+#D' is private", decl);
3916 else if (TREE_PROTECTED (decl))
3917 cp_error_at ("`%+#D' is protected", decl);
3918 else
3919 cp_error_at ("`%+#D' is inaccessible", decl);
3920 error ("within this context");
3921 return false;
3924 return true;
3927 /* Perform the conversions in CONVS on the expression EXPR. FN and
3928 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3929 indicates the `this' argument of a method. INNER is nonzero when
3930 being called to continue a conversion chain. It is negative when a
3931 reference binding will be applied, positive otherwise. If
3932 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
3933 conversions will be emitted if appropriate. */
3935 static tree
3936 convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner,
3937 bool issue_conversion_warnings)
3939 int savew, savee;
3941 tree totype = TREE_TYPE (convs);
3943 if (ICS_BAD_FLAG (convs)
3944 && TREE_CODE (convs) != USER_CONV
3945 && TREE_CODE (convs) != AMBIG_CONV
3946 && TREE_CODE (convs) != REF_BIND)
3948 tree t = convs;
3949 for (; t; t = TREE_OPERAND (t, 0))
3951 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3953 expr = convert_like_real (t, expr, fn, argnum, 1,
3954 /*issue_conversion_warnings=*/false);
3955 break;
3957 else if (TREE_CODE (t) == AMBIG_CONV)
3958 return convert_like_real (t, expr, fn, argnum, 1,
3959 /*issue_conversion_warnings=*/false);
3960 else if (TREE_CODE (t) == IDENTITY_CONV)
3961 break;
3963 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3964 if (fn)
3965 pedwarn (" initializing argument %P of `%D'", argnum, fn);
3966 return cp_convert (totype, expr);
3969 if (issue_conversion_warnings)
3970 expr = dubious_conversion_warnings
3971 (totype, expr, "argument", fn, argnum);
3972 switch (TREE_CODE (convs))
3974 case USER_CONV:
3976 struct z_candidate *cand = USER_CONV_CAND (convs);
3977 tree convfn = cand->fn;
3978 tree args;
3980 if (DECL_CONSTRUCTOR_P (convfn))
3982 tree t = build_int_2 (0, 0);
3983 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3985 args = build_tree_list (NULL_TREE, expr);
3986 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3987 || DECL_HAS_VTT_PARM_P (convfn))
3988 /* We should never try to call the abstract or base constructor
3989 from here. */
3990 abort ();
3991 args = tree_cons (NULL_TREE, t, args);
3993 else
3994 args = build_this (expr);
3995 expr = build_over_call (cand, LOOKUP_NORMAL);
3997 /* If this is a constructor or a function returning an aggr type,
3998 we need to build up a TARGET_EXPR. */
3999 if (DECL_CONSTRUCTOR_P (convfn))
4000 expr = build_cplus_new (totype, expr);
4002 /* The result of the call is then used to direct-initialize the object
4003 that is the destination of the copy-initialization. [dcl.init]
4005 Note that this step is not reflected in the conversion sequence;
4006 it affects the semantics when we actually perform the
4007 conversion, but is not considered during overload resolution.
4009 If the target is a class, that means call a ctor. */
4010 if (IS_AGGR_TYPE (totype)
4011 && (inner >= 0 || !lvalue_p (expr)))
4013 savew = warningcount, savee = errorcount;
4014 expr = build_special_member_call
4015 (NULL_TREE, complete_ctor_identifier,
4016 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
4017 /* Core issue 84, now a DR, says that we don't allow UDCs
4018 for these args (which deliberately breaks copy-init of an
4019 auto_ptr<Base> from an auto_ptr<Derived>). */
4020 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
4022 /* Tell the user where this failing constructor call came from. */
4023 if (fn)
4025 if (warningcount > savew)
4026 warning
4027 (" initializing argument %P of `%D' from result of `%D'",
4028 argnum, fn, convfn);
4029 else if (errorcount > savee)
4030 error
4031 (" initializing argument %P of `%D' from result of `%D'",
4032 argnum, fn, convfn);
4034 else
4036 if (warningcount > savew)
4037 warning (" initializing temporary from result of `%D'",
4038 convfn);
4039 else if (errorcount > savee)
4040 error (" initializing temporary from result of `%D'",
4041 convfn);
4043 expr = build_cplus_new (totype, expr);
4045 return expr;
4047 case IDENTITY_CONV:
4048 if (type_unknown_p (expr))
4049 expr = instantiate_type (totype, expr, tf_error | tf_warning);
4050 /* Convert a non-array constant variable to its underlying value, unless we
4051 are about to bind it to a reference, in which case we need to
4052 leave it as an lvalue. */
4053 if (inner >= 0
4054 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
4055 expr = decl_constant_value (expr);
4056 return expr;
4057 case AMBIG_CONV:
4058 /* Call build_user_type_conversion again for the error. */
4059 return build_user_type_conversion
4060 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
4062 default:
4063 break;
4066 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
4067 TREE_CODE (convs) == REF_BIND ? -1 : 1,
4068 /*issue_conversion_warnings=*/false);
4069 if (expr == error_mark_node)
4070 return error_mark_node;
4072 switch (TREE_CODE (convs))
4074 case RVALUE_CONV:
4075 if (! IS_AGGR_TYPE (totype))
4076 return expr;
4077 /* else fall through */
4078 case BASE_CONV:
4079 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
4081 /* We are going to bind a reference directly to a base-class
4082 subobject of EXPR. */
4083 tree base_ptr = build_pointer_type (totype);
4085 /* Build an expression for `*((base*) &expr)'. */
4086 expr = build_unary_op (ADDR_EXPR, expr, 0);
4087 expr = perform_implicit_conversion (base_ptr, expr);
4088 expr = build_indirect_ref (expr, "implicit conversion");
4089 return expr;
4092 /* Copy-initialization where the cv-unqualified version of the source
4093 type is the same class as, or a derived class of, the class of the
4094 destination [is treated as direct-initialization]. [dcl.init] */
4095 savew = warningcount, savee = errorcount;
4096 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4097 build_tree_list (NULL_TREE, expr),
4098 TYPE_BINFO (totype),
4099 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
4100 if (fn)
4102 if (warningcount > savew)
4103 warning (" initializing argument %P of `%D'", argnum, fn);
4104 else if (errorcount > savee)
4105 error (" initializing argument %P of `%D'", argnum, fn);
4107 return build_cplus_new (totype, expr);
4109 case REF_BIND:
4111 tree ref_type = totype;
4113 /* If necessary, create a temporary. */
4114 if (NEED_TEMPORARY_P (convs) || !non_cast_lvalue_p (expr))
4116 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
4118 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
4120 /* If the reference is volatile or non-const, we
4121 cannot create a temporary. */
4122 cp_lvalue_kind lvalue = real_lvalue_p (expr);
4124 if (lvalue & clk_bitfield)
4125 error ("cannot bind bitfield `%E' to `%T'",
4126 expr, ref_type);
4127 else if (lvalue & clk_packed)
4128 error ("cannot bind packed field `%E' to `%T'",
4129 expr, ref_type);
4130 else
4131 my_friendly_assert (0, 20030715);
4132 return error_mark_node;
4134 expr = build_target_expr_with_type (expr, type);
4137 /* Take the address of the thing to which we will bind the
4138 reference. */
4139 expr = build_unary_op (ADDR_EXPR, expr, 1);
4140 if (expr == error_mark_node)
4141 return error_mark_node;
4143 /* Convert it to a pointer to the type referred to by the
4144 reference. This will adjust the pointer if a derived to
4145 base conversion is being performed. */
4146 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4147 expr);
4148 /* Convert the pointer to the desired reference type. */
4149 return build_nop (ref_type, expr);
4152 case LVALUE_CONV:
4153 return decay_conversion (expr);
4155 case QUAL_CONV:
4156 /* Warn about deprecated conversion if appropriate. */
4157 string_conv_p (totype, expr, 1);
4158 break;
4160 default:
4161 break;
4163 return ocp_convert (totype, expr, CONV_IMPLICIT,
4164 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4167 /* Build a call to __builtin_trap which can be used in an expression. */
4169 static tree
4170 call_builtin_trap (void)
4172 tree fn = get_identifier ("__builtin_trap");
4173 if (IDENTIFIER_GLOBAL_VALUE (fn))
4174 fn = IDENTIFIER_GLOBAL_VALUE (fn);
4175 else
4176 abort ();
4178 fn = build_call (fn, NULL_TREE);
4179 fn = build (COMPOUND_EXPR, integer_type_node, fn, integer_zero_node);
4180 return fn;
4183 /* ARG is being passed to a varargs function. Perform any conversions
4184 required. Return the converted value. */
4186 tree
4187 convert_arg_to_ellipsis (tree arg)
4189 /* [expr.call]
4191 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
4192 standard conversions are performed. */
4193 arg = decay_conversion (arg);
4194 /* [expr.call]
4196 If the argument has integral or enumeration type that is subject
4197 to the integral promotions (_conv.prom_), or a floating point
4198 type that is subject to the floating point promotion
4199 (_conv.fpprom_), the value of the argument is converted to the
4200 promoted type before the call. */
4201 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4202 && (TYPE_PRECISION (TREE_TYPE (arg))
4203 < TYPE_PRECISION (double_type_node)))
4204 arg = cp_convert (double_type_node, arg);
4205 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
4206 arg = perform_integral_promotions (arg);
4208 arg = require_complete_type (arg);
4210 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
4212 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
4213 here and do a bitwise copy, but now cp_expr_size will abort if we
4214 try to do that. */
4215 warning ("cannot pass objects of non-POD type `%#T' through `...'; \
4216 call will abort at runtime",
4217 TREE_TYPE (arg));
4218 arg = call_builtin_trap ();
4221 return arg;
4224 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4226 tree
4227 build_x_va_arg (tree expr, tree type)
4229 if (processing_template_decl)
4230 return build_min (VA_ARG_EXPR, type, expr);
4232 type = complete_type_or_else (type, NULL_TREE);
4234 if (expr == error_mark_node || !type)
4235 return error_mark_node;
4237 if (! pod_type_p (type))
4239 /* Undefined behavior [expr.call] 5.2.2/7. */
4240 warning ("cannot receive objects of non-POD type `%#T' through `...'",
4241 type);
4244 return build_va_arg (expr, type);
4247 /* TYPE has been given to va_arg. Apply the default conversions which
4248 would have happened when passed via ellipsis. Return the promoted
4249 type, or the passed type if there is no change. */
4251 tree
4252 cxx_type_promotes_to (tree type)
4254 tree promote;
4256 if (TREE_CODE (type) == ARRAY_TYPE)
4257 return build_pointer_type (TREE_TYPE (type));
4259 if (TREE_CODE (type) == FUNCTION_TYPE)
4260 return build_pointer_type (type);
4262 promote = type_promotes_to (type);
4263 if (same_type_p (type, promote))
4264 promote = type;
4266 return promote;
4269 /* ARG is a default argument expression being passed to a parameter of
4270 the indicated TYPE, which is a parameter to FN. Do any required
4271 conversions. Return the converted value. */
4273 tree
4274 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
4276 /* If the ARG is an unparsed default argument expression, the
4277 conversion cannot be performed. */
4278 if (TREE_CODE (arg) == DEFAULT_ARG)
4280 error ("the default argument for parameter %d of `%D' has "
4281 "not yet been parsed",
4282 parmnum, fn);
4283 return error_mark_node;
4286 if (fn && DECL_TEMPLATE_INFO (fn))
4287 arg = tsubst_default_argument (fn, type, arg);
4289 arg = break_out_target_exprs (arg);
4291 if (TREE_CODE (arg) == CONSTRUCTOR)
4293 arg = digest_init (type, arg, 0);
4294 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4295 "default argument", fn, parmnum);
4297 else
4299 /* This could get clobbered by the following call. */
4300 if (TREE_HAS_CONSTRUCTOR (arg))
4301 arg = copy_node (arg);
4303 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4304 "default argument", fn, parmnum);
4305 arg = convert_for_arg_passing (type, arg);
4308 return arg;
4311 /* Returns the type which will really be used for passing an argument of
4312 type TYPE. */
4314 tree
4315 type_passed_as (tree type)
4317 /* Pass classes with copy ctors by invisible reference. */
4318 if (TREE_ADDRESSABLE (type))
4319 type = build_reference_type (type);
4320 else if (PROMOTE_PROTOTYPES
4321 && INTEGRAL_TYPE_P (type)
4322 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4323 type = integer_type_node;
4325 return type;
4328 /* Actually perform the appropriate conversion. */
4330 tree
4331 convert_for_arg_passing (tree type, tree val)
4333 if (val == error_mark_node)
4335 /* Pass classes with copy ctors by invisible reference. */
4336 else if (TREE_ADDRESSABLE (type))
4337 val = build1 (ADDR_EXPR, build_reference_type (type), val);
4338 else if (PROMOTE_PROTOTYPES
4339 && INTEGRAL_TYPE_P (type)
4340 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4341 val = perform_integral_promotions (val);
4342 return val;
4345 /* Subroutine of the various build_*_call functions. Overload resolution
4346 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4347 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4348 bitmask of various LOOKUP_* flags which apply to the call itself. */
4350 static tree
4351 build_over_call (struct z_candidate *cand, int flags)
4353 tree fn = cand->fn;
4354 tree args = cand->args;
4355 tree convs = cand->convs;
4356 tree converted_args = NULL_TREE;
4357 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4358 tree conv, arg, val;
4359 int i = 0;
4360 int is_method = 0;
4362 /* Give any warnings we noticed during overload resolution. */
4363 if (cand->warnings)
4364 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4365 joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
4367 if (DECL_FUNCTION_MEMBER_P (fn))
4368 perform_or_defer_access_check (cand->access_path, fn);
4370 if (args && TREE_CODE (args) != TREE_LIST)
4371 args = build_tree_list (NULL_TREE, args);
4372 arg = args;
4374 /* The implicit parameters to a constructor are not considered by overload
4375 resolution, and must be of the proper type. */
4376 if (DECL_CONSTRUCTOR_P (fn))
4378 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4379 arg = TREE_CHAIN (arg);
4380 parm = TREE_CHAIN (parm);
4381 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4382 /* We should never try to call the abstract constructor. */
4383 abort ();
4384 if (DECL_HAS_VTT_PARM_P (fn))
4386 converted_args = tree_cons
4387 (NULL_TREE, TREE_VALUE (arg), converted_args);
4388 arg = TREE_CHAIN (arg);
4389 parm = TREE_CHAIN (parm);
4392 /* Bypass access control for 'this' parameter. */
4393 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4395 tree parmtype = TREE_VALUE (parm);
4396 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4397 tree converted_arg;
4398 tree base_binfo;
4400 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4401 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4402 TREE_TYPE (argtype), fn);
4404 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4405 X is called for an object that is not of type X, or of a type
4406 derived from X, the behavior is undefined.
4408 So we can assume that anything passed as 'this' is non-null, and
4409 optimize accordingly. */
4410 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4411 /* Convert to the base in which the function was declared. */
4412 my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4413 converted_arg = build_base_path (PLUS_EXPR,
4414 TREE_VALUE (arg),
4415 cand->conversion_path,
4417 /* Check that the base class is accessible. */
4418 if (!accessible_base_p (TREE_TYPE (argtype),
4419 BINFO_TYPE (cand->conversion_path)))
4420 error ("`%T' is not an accessible base of `%T'",
4421 BINFO_TYPE (cand->conversion_path),
4422 TREE_TYPE (argtype));
4423 /* If fn was found by a using declaration, the conversion path
4424 will be to the derived class, not the base declaring fn. We
4425 must convert from derived to base. */
4426 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4427 TREE_TYPE (parmtype), ba_ignore, NULL);
4428 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4429 base_binfo, 1);
4431 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4432 parm = TREE_CHAIN (parm);
4433 arg = TREE_CHAIN (arg);
4434 ++i;
4435 is_method = 1;
4438 for (; arg && parm;
4439 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4441 tree type = TREE_VALUE (parm);
4443 conv = TREE_VEC_ELT (convs, i);
4444 val = convert_like_with_context
4445 (conv, TREE_VALUE (arg), fn, i - is_method);
4447 val = convert_for_arg_passing (type, val);
4448 converted_args = tree_cons (NULL_TREE, val, converted_args);
4451 /* Default arguments */
4452 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4453 converted_args
4454 = tree_cons (NULL_TREE,
4455 convert_default_arg (TREE_VALUE (parm),
4456 TREE_PURPOSE (parm),
4457 fn, i - is_method),
4458 converted_args);
4460 /* Ellipsis */
4461 for (; arg; arg = TREE_CHAIN (arg))
4462 converted_args
4463 = tree_cons (NULL_TREE,
4464 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4465 converted_args);
4467 converted_args = nreverse (converted_args);
4469 if (warn_format)
4470 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4471 converted_args);
4473 /* Avoid actually calling copy constructors and copy assignment operators,
4474 if possible. */
4476 if (! flag_elide_constructors)
4477 /* Do things the hard way. */;
4478 else if (TREE_VEC_LENGTH (convs) == 1
4479 && DECL_COPY_CONSTRUCTOR_P (fn))
4481 tree targ;
4482 arg = skip_artificial_parms_for (fn, converted_args);
4483 arg = TREE_VALUE (arg);
4485 /* Pull out the real argument, disregarding const-correctness. */
4486 targ = arg;
4487 while (TREE_CODE (targ) == NOP_EXPR
4488 || TREE_CODE (targ) == NON_LVALUE_EXPR
4489 || TREE_CODE (targ) == CONVERT_EXPR)
4490 targ = TREE_OPERAND (targ, 0);
4491 if (TREE_CODE (targ) == ADDR_EXPR)
4493 targ = TREE_OPERAND (targ, 0);
4494 if (!same_type_ignoring_top_level_qualifiers_p
4495 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4496 targ = NULL_TREE;
4498 else
4499 targ = NULL_TREE;
4501 if (targ)
4502 arg = targ;
4503 else
4504 arg = build_indirect_ref (arg, 0);
4506 /* [class.copy]: the copy constructor is implicitly defined even if
4507 the implementation elided its use. */
4508 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4509 mark_used (fn);
4511 /* If we're creating a temp and we already have one, don't create a
4512 new one. If we're not creating a temp but we get one, use
4513 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4514 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4515 temp or an INIT_EXPR otherwise. */
4516 if (integer_zerop (TREE_VALUE (args)))
4518 if (TREE_CODE (arg) == TARGET_EXPR)
4519 return arg;
4520 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4521 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4523 else if (TREE_CODE (arg) == TARGET_EXPR
4524 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4526 tree address;
4527 tree to = stabilize_reference
4528 (build_indirect_ref (TREE_VALUE (args), 0));
4530 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4531 address = build_unary_op (ADDR_EXPR, val, 0);
4532 /* Avoid a warning about this expression, if the address is
4533 never used. */
4534 TREE_USED (address) = 1;
4535 return address;
4538 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4539 && copy_fn_p (fn)
4540 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4542 tree to = stabilize_reference
4543 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4545 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4546 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4547 return val;
4550 mark_used (fn);
4552 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4554 tree t, *p = &TREE_VALUE (converted_args);
4555 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4556 DECL_CONTEXT (fn),
4557 ba_any, NULL);
4558 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4560 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4561 if (TREE_SIDE_EFFECTS (*p))
4562 *p = save_expr (*p);
4563 t = build_pointer_type (TREE_TYPE (fn));
4564 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4565 fn = build_java_interface_fn_ref (fn, *p);
4566 else
4567 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4568 TREE_TYPE (fn) = t;
4570 else if (DECL_INLINE (fn))
4571 fn = inline_conversion (fn);
4572 else
4573 fn = build_addr_func (fn);
4575 return build_cxx_call (fn, args, converted_args);
4578 /* Build and return a call to FN, using the the CONVERTED_ARGS. ARGS
4579 gives the original form of the arguments. This function performs
4580 no overload resolution, conversion, or other high-level
4581 operations. */
4583 tree
4584 build_cxx_call(tree fn, tree args, tree converted_args)
4586 tree fndecl;
4588 /* Recognize certain built-in functions so we can make tree-codes
4589 other than CALL_EXPR. We do this when it enables fold-const.c
4590 to do something useful. */
4591 if (TREE_CODE (fn) == ADDR_EXPR
4592 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4593 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4595 tree exp;
4596 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4597 if (exp)
4598 return exp;
4601 fn = build_call (fn, converted_args);
4603 /* If this call might throw an exception, note that fact. */
4604 fndecl = get_callee_fndecl (fn);
4605 if ((!fndecl || !TREE_NOTHROW (fndecl))
4606 && at_function_scope_p ()
4607 && cfun)
4608 cp_function_chain->can_throw = 1;
4610 /* Some built-in function calls will be evaluated at compile-time in
4611 fold (). */
4612 fn = fold (fn);
4614 if (VOID_TYPE_P (TREE_TYPE (fn)))
4615 return fn;
4617 fn = require_complete_type (fn);
4618 if (fn == error_mark_node)
4619 return error_mark_node;
4621 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4622 fn = build_cplus_new (TREE_TYPE (fn), fn);
4623 return convert_from_reference (fn);
4626 static GTY(()) tree java_iface_lookup_fn;
4628 /* Make an expression which yields the address of the Java interface
4629 method FN. This is achieved by generating a call to libjava's
4630 _Jv_LookupInterfaceMethodIdx(). */
4632 static tree
4633 build_java_interface_fn_ref (tree fn, tree instance)
4635 tree lookup_args, lookup_fn, method, idx;
4636 tree klass_ref, iface, iface_ref;
4637 int i;
4639 if (!java_iface_lookup_fn)
4641 tree endlink = build_void_list_node ();
4642 tree t = tree_cons (NULL_TREE, ptr_type_node,
4643 tree_cons (NULL_TREE, ptr_type_node,
4644 tree_cons (NULL_TREE, java_int_type_node,
4645 endlink)));
4646 java_iface_lookup_fn
4647 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4648 build_function_type (ptr_type_node, t),
4649 0, NOT_BUILT_IN, NULL, NULL_TREE);
4652 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4653 This is the first entry in the vtable. */
4654 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4655 integer_zero_node);
4657 /* Get the java.lang.Class pointer for the interface being called. */
4658 iface = DECL_CONTEXT (fn);
4659 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
4660 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4661 || DECL_CONTEXT (iface_ref) != iface)
4663 error ("could not find class$ field in java interface type `%T'",
4664 iface);
4665 return error_mark_node;
4667 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4669 /* Determine the itable index of FN. */
4670 i = 1;
4671 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4673 if (!DECL_VIRTUAL_P (method))
4674 continue;
4675 if (fn == method)
4676 break;
4677 i++;
4679 idx = build_int_2 (i, 0);
4681 lookup_args = tree_cons (NULL_TREE, klass_ref,
4682 tree_cons (NULL_TREE, iface_ref,
4683 build_tree_list (NULL_TREE, idx)));
4684 lookup_fn = build1 (ADDR_EXPR,
4685 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4686 java_iface_lookup_fn);
4687 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4690 /* Returns the value to use for the in-charge parameter when making a
4691 call to a function with the indicated NAME. */
4693 tree
4694 in_charge_arg_for_name (tree name)
4696 if (name == base_ctor_identifier
4697 || name == base_dtor_identifier)
4698 return integer_zero_node;
4699 else if (name == complete_ctor_identifier)
4700 return integer_one_node;
4701 else if (name == complete_dtor_identifier)
4702 return integer_two_node;
4703 else if (name == deleting_dtor_identifier)
4704 return integer_three_node;
4706 /* This function should only be called with one of the names listed
4707 above. */
4708 abort ();
4709 return NULL_TREE;
4712 /* Build a call to a constructor, destructor, or an assignment
4713 operator for INSTANCE, an expression with class type. NAME
4714 indicates the special member function to call; ARGS are the
4715 arguments. BINFO indicates the base of INSTANCE that is to be
4716 passed as the `this' parameter to the member function called.
4718 FLAGS are the LOOKUP_* flags to use when processing the call.
4720 If NAME indicates a complete object constructor, INSTANCE may be
4721 NULL_TREE. In this case, the caller will call build_cplus_new to
4722 store the newly constructed object into a VAR_DECL. */
4724 tree
4725 build_special_member_call (tree instance, tree name, tree args,
4726 tree binfo, int flags)
4728 tree fns;
4729 /* The type of the subobject to be constructed or destroyed. */
4730 tree class_type;
4732 my_friendly_assert (name == complete_ctor_identifier
4733 || name == base_ctor_identifier
4734 || name == complete_dtor_identifier
4735 || name == base_dtor_identifier
4736 || name == deleting_dtor_identifier
4737 || name == ansi_assopname (NOP_EXPR),
4738 20020712);
4739 my_friendly_assert (binfo != NULL_TREE, 20020712);
4741 class_type = BINFO_TYPE (binfo);
4743 /* Handle the special case where INSTANCE is NULL_TREE. */
4744 if (name == complete_ctor_identifier && !instance)
4746 instance = build_int_2 (0, 0);
4747 TREE_TYPE (instance) = build_pointer_type (class_type);
4748 instance = build1 (INDIRECT_REF, class_type, instance);
4750 else if (name == complete_dtor_identifier
4751 || name == base_dtor_identifier
4752 || name == deleting_dtor_identifier)
4753 my_friendly_assert (args == NULL_TREE, 20020712);
4755 my_friendly_assert (instance != NULL_TREE, 20020712);
4757 /* Resolve the name. */
4758 if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4759 return error_mark_node;
4761 fns = lookup_fnfields (binfo, name, 1);
4763 /* When making a call to a constructor or destructor for a subobject
4764 that uses virtual base classes, pass down a pointer to a VTT for
4765 the subobject. */
4766 if ((name == base_ctor_identifier
4767 || name == base_dtor_identifier)
4768 && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4770 tree vtt;
4771 tree sub_vtt;
4773 /* If the current function is a complete object constructor
4774 or destructor, then we fetch the VTT directly.
4775 Otherwise, we look it up using the VTT we were given. */
4776 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
4777 vtt = decay_conversion (vtt);
4778 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4779 build (EQ_EXPR, boolean_type_node,
4780 current_in_charge_parm, integer_zero_node),
4781 current_vtt_parm,
4782 vtt);
4783 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4784 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4785 BINFO_SUBVTT_INDEX (binfo));
4787 args = tree_cons (NULL_TREE, sub_vtt, args);
4790 return build_new_method_call (instance, fns, args, binfo, flags);
4793 /* Return the NAME, as a C string. The NAME indicates a function that
4794 is a member of TYPE. *FREE_P is set to true if the caller must
4795 free the memory returned.
4797 Rather than go through all of this, we should simply set the names
4798 of constructors and destructors appropriately, and dispense with
4799 ctor_identifier, dtor_identifier, etc. */
4801 static char *
4802 name_as_c_string (tree name, tree type, bool *free_p)
4804 char *pretty_name;
4806 /* Assume that we will not allocate memory. */
4807 *free_p = false;
4808 /* Constructors and destructors are special. */
4809 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4811 pretty_name
4812 = (char *) IDENTIFIER_POINTER (constructor_name (type));
4813 /* For a destructor, add the '~'. */
4814 if (name == complete_dtor_identifier
4815 || name == base_dtor_identifier
4816 || name == deleting_dtor_identifier)
4818 pretty_name = concat ("~", pretty_name, NULL);
4819 /* Remember that we need to free the memory allocated. */
4820 *free_p = true;
4823 else
4824 pretty_name = (char *) IDENTIFIER_POINTER (name);
4826 return pretty_name;
4829 /* Build a call to "INSTANCE.FN (ARGS)". */
4831 tree
4832 build_new_method_call (tree instance, tree fns, tree args,
4833 tree conversion_path, int flags)
4835 struct z_candidate *candidates = 0, *cand;
4836 tree explicit_targs = NULL_TREE;
4837 tree basetype = NULL_TREE;
4838 tree access_binfo;
4839 tree optype;
4840 tree mem_args = NULL_TREE, instance_ptr;
4841 tree name;
4842 tree user_args;
4843 tree call;
4844 tree fn;
4845 tree class_type;
4846 int template_only = 0;
4847 bool any_viable_p;
4848 tree orig_instance;
4849 tree orig_fns;
4850 tree orig_args;
4852 my_friendly_assert (instance != NULL_TREE, 20020729);
4854 if (error_operand_p (instance)
4855 || error_operand_p (fns)
4856 || args == error_mark_node)
4857 return error_mark_node;
4859 orig_instance = instance;
4860 orig_fns = fns;
4861 orig_args = args;
4863 if (processing_template_decl)
4865 instance = build_non_dependent_expr (instance);
4866 if (!BASELINK_P (fns)
4867 && TREE_CODE (fns) != PSEUDO_DTOR_EXPR
4868 && TREE_TYPE (fns) != unknown_type_node)
4869 fns = build_non_dependent_expr (fns);
4870 args = build_non_dependent_args (orig_args);
4873 /* Process the argument list. */
4874 user_args = args;
4875 args = resolve_args (args);
4876 if (args == error_mark_node)
4877 return error_mark_node;
4879 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4880 instance = convert_from_reference (instance);
4881 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4882 instance_ptr = build_this (instance);
4884 if (!BASELINK_P (fns))
4886 call = build_field_call (instance_ptr, fns, args);
4887 if (call)
4888 return call;
4889 error ("call to non-function `%D'", fns);
4890 return error_mark_node;
4893 if (!conversion_path)
4894 conversion_path = BASELINK_BINFO (fns);
4895 access_binfo = BASELINK_ACCESS_BINFO (fns);
4896 optype = BASELINK_OPTYPE (fns);
4897 fns = BASELINK_FUNCTIONS (fns);
4899 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4901 explicit_targs = TREE_OPERAND (fns, 1);
4902 fns = TREE_OPERAND (fns, 0);
4903 template_only = 1;
4906 my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
4907 || TREE_CODE (fns) == TEMPLATE_DECL
4908 || TREE_CODE (fns) == OVERLOAD,
4909 20020712);
4911 /* XXX this should be handled before we get here. */
4912 if (! IS_AGGR_TYPE (basetype))
4914 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4915 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4916 fns, instance, basetype);
4918 return error_mark_node;
4921 fn = get_first_fn (fns);
4922 name = DECL_NAME (fn);
4924 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4926 /* Callers should explicitly indicate whether they want to construct
4927 the complete object or just the part without virtual bases. */
4928 my_friendly_assert (name != ctor_identifier, 20000408);
4929 /* Similarly for destructors. */
4930 my_friendly_assert (name != dtor_identifier, 20000408);
4933 /* It's OK to call destructors on cv-qualified objects. Therefore,
4934 convert the INSTANCE_PTR to the unqualified type, if necessary. */
4935 if (DECL_DESTRUCTOR_P (fn))
4937 tree type = build_pointer_type (basetype);
4938 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
4939 instance_ptr = build_nop (type, instance_ptr);
4942 class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE);
4943 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4945 for (fn = fns; fn; fn = OVL_NEXT (fn))
4947 tree t = OVL_CURRENT (fn);
4948 tree this_arglist;
4950 /* We can end up here for copy-init of same or base class. */
4951 if ((flags & LOOKUP_ONLYCONVERTING)
4952 && DECL_NONCONVERTING_P (t))
4953 continue;
4955 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4956 this_arglist = mem_args;
4957 else
4958 this_arglist = args;
4960 if (TREE_CODE (t) == TEMPLATE_DECL)
4961 /* A member template. */
4962 add_template_candidate (&candidates, t,
4963 class_type,
4964 explicit_targs,
4965 this_arglist, optype,
4966 access_binfo,
4967 conversion_path,
4968 flags,
4969 DEDUCE_CALL);
4970 else if (! template_only)
4971 add_function_candidate (&candidates, t,
4972 class_type,
4973 this_arglist,
4974 access_binfo,
4975 conversion_path,
4976 flags);
4979 candidates = splice_viable (candidates, pedantic, &any_viable_p);
4980 if (!any_viable_p)
4982 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4983 if (flags & LOOKUP_SPECULATIVELY)
4984 return NULL_TREE;
4985 if (!COMPLETE_TYPE_P (basetype))
4986 cxx_incomplete_type_error (instance_ptr, basetype);
4987 else
4989 char *pretty_name;
4990 bool free_p;
4992 pretty_name = name_as_c_string (name, basetype, &free_p);
4993 error ("no matching function for call to `%T::%s(%A)%#V'",
4994 basetype, pretty_name, user_args,
4995 TREE_TYPE (TREE_TYPE (instance_ptr)));
4996 if (free_p)
4997 free (pretty_name);
4999 print_z_candidates (candidates);
5000 return error_mark_node;
5003 cand = tourney (candidates);
5004 if (cand == 0)
5006 char *pretty_name;
5007 bool free_p;
5009 pretty_name = name_as_c_string (name, basetype, &free_p);
5010 error ("call of overloaded `%s(%A)' is ambiguous", pretty_name,
5011 user_args);
5012 print_z_candidates (candidates);
5013 if (free_p)
5014 free (pretty_name);
5015 return error_mark_node;
5018 if (DECL_PURE_VIRTUAL_P (cand->fn)
5019 && instance == current_class_ref
5020 && (DECL_CONSTRUCTOR_P (current_function_decl)
5021 || DECL_DESTRUCTOR_P (current_function_decl))
5022 && ! (flags & LOOKUP_NONVIRTUAL)
5023 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
5024 error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
5025 "abstract virtual `%#D' called from constructor"
5026 : "abstract virtual `%#D' called from destructor"),
5027 cand->fn);
5028 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
5029 && is_dummy_object (instance_ptr))
5031 error ("cannot call member function `%D' without object", cand->fn);
5032 return error_mark_node;
5035 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
5036 && resolves_to_fixed_type_p (instance, 0))
5037 flags |= LOOKUP_NONVIRTUAL;
5039 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
5040 call = build_over_call (cand, flags);
5041 else
5043 call = build_over_call (cand, flags);
5044 /* In an expression of the form `a->f()' where `f' turns out to
5045 be a static member function, `a' is none-the-less evaluated. */
5046 if (!is_dummy_object (instance_ptr) && TREE_SIDE_EFFECTS (instance))
5047 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
5050 if (processing_template_decl && call != error_mark_node)
5051 return build_min (CALL_EXPR,
5052 TREE_TYPE (call),
5053 build_min_nt (COMPONENT_REF,
5054 orig_instance,
5055 orig_fns),
5056 orig_args);
5057 return call;
5060 /* Returns true iff standard conversion sequence ICS1 is a proper
5061 subsequence of ICS2. */
5063 static bool
5064 is_subseq (tree ics1, tree ics2)
5066 /* We can assume that a conversion of the same code
5067 between the same types indicates a subsequence since we only get
5068 here if the types we are converting from are the same. */
5070 while (TREE_CODE (ics1) == RVALUE_CONV
5071 || TREE_CODE (ics1) == LVALUE_CONV)
5072 ics1 = TREE_OPERAND (ics1, 0);
5074 while (1)
5076 while (TREE_CODE (ics2) == RVALUE_CONV
5077 || TREE_CODE (ics2) == LVALUE_CONV)
5078 ics2 = TREE_OPERAND (ics2, 0);
5080 if (TREE_CODE (ics2) == USER_CONV
5081 || TREE_CODE (ics2) == AMBIG_CONV
5082 || TREE_CODE (ics2) == IDENTITY_CONV)
5083 /* At this point, ICS1 cannot be a proper subsequence of
5084 ICS2. We can get a USER_CONV when we are comparing the
5085 second standard conversion sequence of two user conversion
5086 sequences. */
5087 return false;
5089 ics2 = TREE_OPERAND (ics2, 0);
5091 if (TREE_CODE (ics2) == TREE_CODE (ics1)
5092 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
5093 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
5094 TREE_TYPE (TREE_OPERAND (ics1, 0))))
5095 return true;
5099 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
5100 be any _TYPE nodes. */
5102 bool
5103 is_properly_derived_from (tree derived, tree base)
5105 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
5106 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
5107 return false;
5109 /* We only allow proper derivation here. The DERIVED_FROM_P macro
5110 considers every class derived from itself. */
5111 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
5112 && DERIVED_FROM_P (base, derived));
5115 /* We build the ICS for an implicit object parameter as a pointer
5116 conversion sequence. However, such a sequence should be compared
5117 as if it were a reference conversion sequence. If ICS is the
5118 implicit conversion sequence for an implicit object parameter,
5119 modify it accordingly. */
5121 static void
5122 maybe_handle_implicit_object (tree *ics)
5124 if (ICS_THIS_FLAG (*ics))
5126 /* [over.match.funcs]
5128 For non-static member functions, the type of the
5129 implicit object parameter is "reference to cv X"
5130 where X is the class of which the function is a
5131 member and cv is the cv-qualification on the member
5132 function declaration. */
5133 tree t = *ics;
5134 tree reference_type;
5136 /* The `this' parameter is a pointer to a class type. Make the
5137 implicit conversion talk about a reference to that same class
5138 type. */
5139 reference_type = TREE_TYPE (TREE_TYPE (*ics));
5140 reference_type = build_reference_type (reference_type);
5142 if (TREE_CODE (t) == QUAL_CONV)
5143 t = TREE_OPERAND (t, 0);
5144 if (TREE_CODE (t) == PTR_CONV)
5145 t = TREE_OPERAND (t, 0);
5146 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
5147 t = direct_reference_binding (reference_type, t);
5148 *ics = t;
5152 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5153 and return the type to which the reference refers. Otherwise,
5154 leave *ICS unchanged and return NULL_TREE. */
5156 static tree
5157 maybe_handle_ref_bind (tree *ics)
5159 if (TREE_CODE (*ics) == REF_BIND)
5161 tree old_ics = *ics;
5162 tree type = TREE_TYPE (TREE_TYPE (old_ics));
5163 *ics = TREE_OPERAND (old_ics, 0);
5164 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
5165 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
5166 return type;
5169 return NULL_TREE;
5172 /* Compare two implicit conversion sequences according to the rules set out in
5173 [over.ics.rank]. Return values:
5175 1: ics1 is better than ics2
5176 -1: ics2 is better than ics1
5177 0: ics1 and ics2 are indistinguishable */
5179 static int
5180 compare_ics (tree ics1, tree ics2)
5182 tree from_type1;
5183 tree from_type2;
5184 tree to_type1;
5185 tree to_type2;
5186 tree deref_from_type1 = NULL_TREE;
5187 tree deref_from_type2 = NULL_TREE;
5188 tree deref_to_type1 = NULL_TREE;
5189 tree deref_to_type2 = NULL_TREE;
5190 int rank1, rank2;
5192 /* REF_BINDING is nonzero if the result of the conversion sequence
5193 is a reference type. In that case TARGET_TYPE is the
5194 type referred to by the reference. */
5195 tree target_type1;
5196 tree target_type2;
5198 /* Handle implicit object parameters. */
5199 maybe_handle_implicit_object (&ics1);
5200 maybe_handle_implicit_object (&ics2);
5202 /* Handle reference parameters. */
5203 target_type1 = maybe_handle_ref_bind (&ics1);
5204 target_type2 = maybe_handle_ref_bind (&ics2);
5206 /* [over.ics.rank]
5208 When comparing the basic forms of implicit conversion sequences (as
5209 defined in _over.best.ics_)
5211 --a standard conversion sequence (_over.ics.scs_) is a better
5212 conversion sequence than a user-defined conversion sequence
5213 or an ellipsis conversion sequence, and
5215 --a user-defined conversion sequence (_over.ics.user_) is a
5216 better conversion sequence than an ellipsis conversion sequence
5217 (_over.ics.ellipsis_). */
5218 rank1 = ICS_RANK (ics1);
5219 rank2 = ICS_RANK (ics2);
5221 if (rank1 > rank2)
5222 return -1;
5223 else if (rank1 < rank2)
5224 return 1;
5226 if (rank1 == BAD_RANK)
5228 /* XXX Isn't this an extension? */
5229 /* Both ICS are bad. We try to make a decision based on what
5230 would have happenned if they'd been good. */
5231 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5232 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5233 return -1;
5234 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5235 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5236 return 1;
5238 /* We couldn't make up our minds; try to figure it out below. */
5241 if (ICS_ELLIPSIS_FLAG (ics1))
5242 /* Both conversions are ellipsis conversions. */
5243 return 0;
5245 /* User-defined conversion sequence U1 is a better conversion sequence
5246 than another user-defined conversion sequence U2 if they contain the
5247 same user-defined conversion operator or constructor and if the sec-
5248 ond standard conversion sequence of U1 is better than the second
5249 standard conversion sequence of U2. */
5251 if (ICS_USER_FLAG (ics1))
5253 tree t1, t2;
5255 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5256 if (TREE_CODE (t1) == AMBIG_CONV)
5257 return 0;
5258 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5259 if (TREE_CODE (t2) == AMBIG_CONV)
5260 return 0;
5262 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5263 return 0;
5265 /* We can just fall through here, after setting up
5266 FROM_TYPE1 and FROM_TYPE2. */
5267 from_type1 = TREE_TYPE (t1);
5268 from_type2 = TREE_TYPE (t2);
5270 else
5272 /* We're dealing with two standard conversion sequences.
5274 [over.ics.rank]
5276 Standard conversion sequence S1 is a better conversion
5277 sequence than standard conversion sequence S2 if
5279 --S1 is a proper subsequence of S2 (comparing the conversion
5280 sequences in the canonical form defined by _over.ics.scs_,
5281 excluding any Lvalue Transformation; the identity
5282 conversion sequence is considered to be a subsequence of
5283 any non-identity conversion sequence */
5285 from_type1 = ics1;
5286 while (TREE_CODE (from_type1) != IDENTITY_CONV)
5287 from_type1 = TREE_OPERAND (from_type1, 0);
5288 from_type1 = TREE_TYPE (from_type1);
5290 from_type2 = ics2;
5291 while (TREE_CODE (from_type2) != IDENTITY_CONV)
5292 from_type2 = TREE_OPERAND (from_type2, 0);
5293 from_type2 = TREE_TYPE (from_type2);
5296 if (same_type_p (from_type1, from_type2))
5298 if (is_subseq (ics1, ics2))
5299 return 1;
5300 if (is_subseq (ics2, ics1))
5301 return -1;
5303 /* Otherwise, one sequence cannot be a subsequence of the other; they
5304 don't start with the same type. This can happen when comparing the
5305 second standard conversion sequence in two user-defined conversion
5306 sequences. */
5308 /* [over.ics.rank]
5310 Or, if not that,
5312 --the rank of S1 is better than the rank of S2 (by the rules
5313 defined below):
5315 Standard conversion sequences are ordered by their ranks: an Exact
5316 Match is a better conversion than a Promotion, which is a better
5317 conversion than a Conversion.
5319 Two conversion sequences with the same rank are indistinguishable
5320 unless one of the following rules applies:
5322 --A conversion that is not a conversion of a pointer, or pointer
5323 to member, to bool is better than another conversion that is such
5324 a conversion.
5326 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5327 so that we do not have to check it explicitly. */
5328 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5329 return 1;
5330 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5331 return -1;
5333 to_type1 = TREE_TYPE (ics1);
5334 to_type2 = TREE_TYPE (ics2);
5336 if (TYPE_PTR_P (from_type1)
5337 && TYPE_PTR_P (from_type2)
5338 && TYPE_PTR_P (to_type1)
5339 && TYPE_PTR_P (to_type2))
5341 deref_from_type1 = TREE_TYPE (from_type1);
5342 deref_from_type2 = TREE_TYPE (from_type2);
5343 deref_to_type1 = TREE_TYPE (to_type1);
5344 deref_to_type2 = TREE_TYPE (to_type2);
5346 /* The rules for pointers to members A::* are just like the rules
5347 for pointers A*, except opposite: if B is derived from A then
5348 A::* converts to B::*, not vice versa. For that reason, we
5349 switch the from_ and to_ variables here. */
5350 else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
5351 && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
5352 || (TYPE_PTRMEMFUNC_P (from_type1)
5353 && TYPE_PTRMEMFUNC_P (from_type2)
5354 && TYPE_PTRMEMFUNC_P (to_type1)
5355 && TYPE_PTRMEMFUNC_P (to_type2)))
5357 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
5358 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
5359 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
5360 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
5363 if (deref_from_type1 != NULL_TREE
5364 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5365 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5367 /* This was one of the pointer or pointer-like conversions.
5369 [over.ics.rank]
5371 --If class B is derived directly or indirectly from class A,
5372 conversion of B* to A* is better than conversion of B* to
5373 void*, and conversion of A* to void* is better than
5374 conversion of B* to void*. */
5375 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5376 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5378 if (is_properly_derived_from (deref_from_type1,
5379 deref_from_type2))
5380 return -1;
5381 else if (is_properly_derived_from (deref_from_type2,
5382 deref_from_type1))
5383 return 1;
5385 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5386 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5388 if (same_type_p (deref_from_type1, deref_from_type2))
5390 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5392 if (is_properly_derived_from (deref_from_type1,
5393 deref_to_type1))
5394 return 1;
5396 /* We know that DEREF_TO_TYPE1 is `void' here. */
5397 else if (is_properly_derived_from (deref_from_type1,
5398 deref_to_type2))
5399 return -1;
5402 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5403 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5405 /* [over.ics.rank]
5407 --If class B is derived directly or indirectly from class A
5408 and class C is derived directly or indirectly from B,
5410 --conversion of C* to B* is better than conversion of C* to
5411 A*,
5413 --conversion of B* to A* is better than conversion of C* to
5414 A* */
5415 if (same_type_p (deref_from_type1, deref_from_type2))
5417 if (is_properly_derived_from (deref_to_type1,
5418 deref_to_type2))
5419 return 1;
5420 else if (is_properly_derived_from (deref_to_type2,
5421 deref_to_type1))
5422 return -1;
5424 else if (same_type_p (deref_to_type1, deref_to_type2))
5426 if (is_properly_derived_from (deref_from_type2,
5427 deref_from_type1))
5428 return 1;
5429 else if (is_properly_derived_from (deref_from_type1,
5430 deref_from_type2))
5431 return -1;
5435 else if (CLASS_TYPE_P (non_reference (from_type1))
5436 && same_type_p (from_type1, from_type2))
5438 tree from = non_reference (from_type1);
5440 /* [over.ics.rank]
5442 --binding of an expression of type C to a reference of type
5443 B& is better than binding an expression of type C to a
5444 reference of type A&
5446 --conversion of C to B is better than conversion of C to A, */
5447 if (is_properly_derived_from (from, to_type1)
5448 && is_properly_derived_from (from, to_type2))
5450 if (is_properly_derived_from (to_type1, to_type2))
5451 return 1;
5452 else if (is_properly_derived_from (to_type2, to_type1))
5453 return -1;
5456 else if (CLASS_TYPE_P (non_reference (to_type1))
5457 && same_type_p (to_type1, to_type2))
5459 tree to = non_reference (to_type1);
5461 /* [over.ics.rank]
5463 --binding of an expression of type B to a reference of type
5464 A& is better than binding an expression of type C to a
5465 reference of type A&,
5467 --onversion of B to A is better than conversion of C to A */
5468 if (is_properly_derived_from (from_type1, to)
5469 && is_properly_derived_from (from_type2, to))
5471 if (is_properly_derived_from (from_type2, from_type1))
5472 return 1;
5473 else if (is_properly_derived_from (from_type1, from_type2))
5474 return -1;
5478 /* [over.ics.rank]
5480 --S1 and S2 differ only in their qualification conversion and yield
5481 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5482 qualification signature of type T1 is a proper subset of the cv-
5483 qualification signature of type T2 */
5484 if (TREE_CODE (ics1) == QUAL_CONV
5485 && TREE_CODE (ics2) == QUAL_CONV
5486 && same_type_p (from_type1, from_type2))
5487 return comp_cv_qual_signature (to_type1, to_type2);
5489 /* [over.ics.rank]
5491 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5492 types to which the references refer are the same type except for
5493 top-level cv-qualifiers, and the type to which the reference
5494 initialized by S2 refers is more cv-qualified than the type to
5495 which the reference initialized by S1 refers */
5497 if (target_type1 && target_type2
5498 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5499 return comp_cv_qualification (target_type2, target_type1);
5501 /* Neither conversion sequence is better than the other. */
5502 return 0;
5505 /* The source type for this standard conversion sequence. */
5507 static tree
5508 source_type (tree t)
5510 for (;; t = TREE_OPERAND (t, 0))
5512 if (TREE_CODE (t) == USER_CONV
5513 || TREE_CODE (t) == AMBIG_CONV
5514 || TREE_CODE (t) == IDENTITY_CONV)
5515 return TREE_TYPE (t);
5517 abort ();
5520 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5521 a pointer to LOSER and re-running joust to produce the warning if WINNER
5522 is actually used. */
5524 static void
5525 add_warning (struct z_candidate *winner, struct z_candidate *loser)
5527 winner->warnings = tree_cons (NULL_TREE,
5528 build_zc_wrapper (loser),
5529 winner->warnings);
5532 /* Compare two candidates for overloading as described in
5533 [over.match.best]. Return values:
5535 1: cand1 is better than cand2
5536 -1: cand2 is better than cand1
5537 0: cand1 and cand2 are indistinguishable */
5539 static int
5540 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
5542 int winner = 0;
5543 int i, off1 = 0, off2 = 0, len;
5545 /* Candidates that involve bad conversions are always worse than those
5546 that don't. */
5547 if (cand1->viable > cand2->viable)
5548 return 1;
5549 if (cand1->viable < cand2->viable)
5550 return -1;
5552 /* If we have two pseudo-candidates for conversions to the same type,
5553 or two candidates for the same function, arbitrarily pick one. */
5554 if (cand1->fn == cand2->fn
5555 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5556 return 1;
5558 /* a viable function F1
5559 is defined to be a better function than another viable function F2 if
5560 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5561 ICSi(F2), and then */
5563 /* for some argument j, ICSj(F1) is a better conversion sequence than
5564 ICSj(F2) */
5566 /* For comparing static and non-static member functions, we ignore
5567 the implicit object parameter of the non-static function. The
5568 standard says to pretend that the static function has an object
5569 parm, but that won't work with operator overloading. */
5570 len = TREE_VEC_LENGTH (cand1->convs);
5571 if (len != TREE_VEC_LENGTH (cand2->convs))
5573 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5574 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5575 off2 = 1;
5576 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5577 && DECL_STATIC_FUNCTION_P (cand2->fn))
5579 off1 = 1;
5580 --len;
5582 else
5583 abort ();
5586 for (i = 0; i < len; ++i)
5588 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5589 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5590 int comp = compare_ics (t1, t2);
5592 if (comp != 0)
5594 if (warn_sign_promo
5595 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5596 && TREE_CODE (t1) == STD_CONV
5597 && TREE_CODE (t2) == STD_CONV
5598 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5599 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5600 && (TYPE_PRECISION (TREE_TYPE (t1))
5601 == TYPE_PRECISION (TREE_TYPE (t2)))
5602 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5603 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5604 == ENUMERAL_TYPE)))
5606 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5607 tree type1, type2;
5608 struct z_candidate *w, *l;
5609 if (comp > 0)
5610 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5611 w = cand1, l = cand2;
5612 else
5613 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5614 w = cand2, l = cand1;
5616 if (warn)
5618 warning ("passing `%T' chooses `%T' over `%T'",
5619 type, type1, type2);
5620 warning (" in call to `%D'", w->fn);
5622 else
5623 add_warning (w, l);
5626 if (winner && comp != winner)
5628 winner = 0;
5629 goto tweak;
5631 winner = comp;
5635 /* warn about confusing overload resolution for user-defined conversions,
5636 either between a constructor and a conversion op, or between two
5637 conversion ops. */
5638 if (winner && warn_conversion && cand1->second_conv
5639 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
5640 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
5642 struct z_candidate *w, *l;
5643 bool give_warning = false;
5645 if (winner == 1)
5646 w = cand1, l = cand2;
5647 else
5648 w = cand2, l = cand1;
5650 /* We don't want to complain about `X::operator T1 ()'
5651 beating `X::operator T2 () const', when T2 is a no less
5652 cv-qualified version of T1. */
5653 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
5654 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
5656 tree t = TREE_TYPE (TREE_TYPE (l->fn));
5657 tree f = TREE_TYPE (TREE_TYPE (w->fn));
5659 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
5661 t = TREE_TYPE (t);
5662 f = TREE_TYPE (f);
5664 if (!comp_ptr_ttypes (t, f))
5665 give_warning = true;
5667 else
5668 give_warning = true;
5670 if (!give_warning)
5671 /*NOP*/;
5672 else if (warn)
5674 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5675 if (! DECL_CONSTRUCTOR_P (w->fn))
5676 source = TREE_TYPE (source);
5677 warning ("choosing `%D' over `%D'", w->fn, l->fn);
5678 warning (" for conversion from `%T' to `%T'",
5679 source, TREE_TYPE (w->second_conv));
5680 warning (" because conversion sequence for the argument is better");
5682 else
5683 add_warning (w, l);
5686 if (winner)
5687 return winner;
5689 /* or, if not that,
5690 F1 is a non-template function and F2 is a template function
5691 specialization. */
5693 if (! cand1->template && cand2->template)
5694 return 1;
5695 else if (cand1->template && ! cand2->template)
5696 return -1;
5698 /* or, if not that,
5699 F1 and F2 are template functions and the function template for F1 is
5700 more specialized than the template for F2 according to the partial
5701 ordering rules. */
5703 if (cand1->template && cand2->template)
5705 winner = more_specialized
5706 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5707 DEDUCE_ORDER,
5708 /* Tell the deduction code how many real function arguments
5709 we saw, not counting the implicit 'this' argument. But,
5710 add_function_candidate() suppresses the "this" argument
5711 for constructors.
5713 [temp.func.order]: The presence of unused ellipsis and default
5714 arguments has no effect on the partial ordering of function
5715 templates. */
5716 TREE_VEC_LENGTH (cand1->convs)
5717 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5718 - DECL_CONSTRUCTOR_P (cand1->fn)));
5719 if (winner)
5720 return winner;
5723 /* or, if not that,
5724 the context is an initialization by user-defined conversion (see
5725 _dcl.init_ and _over.match.user_) and the standard conversion
5726 sequence from the return type of F1 to the destination type (i.e.,
5727 the type of the entity being initialized) is a better conversion
5728 sequence than the standard conversion sequence from the return type
5729 of F2 to the destination type. */
5731 if (cand1->second_conv)
5733 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5734 if (winner)
5735 return winner;
5738 /* Check whether we can discard a builtin candidate, either because we
5739 have two identical ones or matching builtin and non-builtin candidates.
5741 (Pedantically in the latter case the builtin which matched the user
5742 function should not be added to the overload set, but we spot it here.
5744 [over.match.oper]
5745 ... the builtin candidates include ...
5746 - do not have the same parameter type list as any non-template
5747 non-member candidate. */
5749 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5750 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5752 for (i = 0; i < len; ++i)
5753 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5754 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5755 break;
5756 if (i == TREE_VEC_LENGTH (cand1->convs))
5758 if (cand1->fn == cand2->fn)
5759 /* Two built-in candidates; arbitrarily pick one. */
5760 return 1;
5761 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5762 /* cand1 is built-in; prefer cand2. */
5763 return -1;
5764 else
5765 /* cand2 is built-in; prefer cand1. */
5766 return 1;
5770 /* If the two functions are the same (this can happen with declarations
5771 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5772 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5773 && equal_functions (cand1->fn, cand2->fn))
5774 return 1;
5776 tweak:
5778 /* Extension: If the worst conversion for one candidate is worse than the
5779 worst conversion for the other, take the first. */
5780 if (!pedantic)
5782 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5783 struct z_candidate *w = 0, *l = 0;
5785 for (i = 0; i < len; ++i)
5787 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5788 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5789 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5790 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5792 if (rank1 < rank2)
5793 winner = 1, w = cand1, l = cand2;
5794 if (rank1 > rank2)
5795 winner = -1, w = cand2, l = cand1;
5796 if (winner)
5798 if (warn)
5800 pedwarn ("\
5801 ISO C++ says that these are ambiguous, even \
5802 though the worst conversion for the first is better than \
5803 the worst conversion for the second:");
5804 print_z_candidate (_("candidate 1:"), w);
5805 print_z_candidate (_("candidate 2:"), l);
5807 else
5808 add_warning (w, l);
5809 return winner;
5813 my_friendly_assert (!winner, 20010121);
5814 return 0;
5817 /* Given a list of candidates for overloading, find the best one, if any.
5818 This algorithm has a worst case of O(2n) (winner is last), and a best
5819 case of O(n/2) (totally ambiguous); much better than a sorting
5820 algorithm. */
5822 static struct z_candidate *
5823 tourney (struct z_candidate *candidates)
5825 struct z_candidate *champ = candidates, *challenger;
5826 int fate;
5827 int champ_compared_to_predecessor = 0;
5829 /* Walk through the list once, comparing each current champ to the next
5830 candidate, knocking out a candidate or two with each comparison. */
5832 for (challenger = champ->next; challenger; )
5834 fate = joust (champ, challenger, 0);
5835 if (fate == 1)
5836 challenger = challenger->next;
5837 else
5839 if (fate == 0)
5841 champ = challenger->next;
5842 if (champ == 0)
5843 return 0;
5844 champ_compared_to_predecessor = 0;
5846 else
5848 champ = challenger;
5849 champ_compared_to_predecessor = 1;
5852 challenger = champ->next;
5856 /* Make sure the champ is better than all the candidates it hasn't yet
5857 been compared to. */
5859 for (challenger = candidates;
5860 challenger != champ
5861 && !(champ_compared_to_predecessor && challenger->next == champ);
5862 challenger = challenger->next)
5864 fate = joust (champ, challenger, 0);
5865 if (fate != 1)
5866 return 0;
5869 return champ;
5872 /* Returns nonzero if things of type FROM can be converted to TO. */
5874 bool
5875 can_convert (tree to, tree from)
5877 return can_convert_arg (to, from, NULL_TREE);
5880 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
5882 bool
5883 can_convert_arg (tree to, tree from, tree arg)
5885 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5886 return (t && ! ICS_BAD_FLAG (t));
5889 /* Like can_convert_arg, but allows dubious conversions as well. */
5891 bool
5892 can_convert_arg_bad (tree to, tree from, tree arg)
5894 return implicit_conversion (to, from, arg, LOOKUP_NORMAL) != 0;
5897 /* Convert EXPR to TYPE. Return the converted expression.
5899 Note that we allow bad conversions here because by the time we get to
5900 this point we are committed to doing the conversion. If we end up
5901 doing a bad conversion, convert_like will complain. */
5903 tree
5904 perform_implicit_conversion (tree type, tree expr)
5906 tree conv;
5908 if (error_operand_p (expr))
5909 return error_mark_node;
5910 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5911 LOOKUP_NORMAL);
5912 if (!conv)
5914 error ("could not convert `%E' to `%T'", expr, type);
5915 return error_mark_node;
5918 return convert_like (conv, expr);
5921 /* Convert EXPR to TYPE (as a direct-initialization) if that is
5922 permitted. If the conversion is valid, the converted expression is
5923 returned. Otherwise, NULL_TREE is returned. */
5925 tree
5926 perform_direct_initialization_if_possible (tree type, tree expr)
5928 tree conv;
5930 if (type == error_mark_node || error_operand_p (expr))
5931 return error_mark_node;
5932 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5933 LOOKUP_NORMAL);
5934 if (!conv || ICS_BAD_FLAG (conv))
5935 return NULL_TREE;
5936 return convert_like_real (conv, expr, NULL_TREE, 0, 0,
5937 /*issue_conversion_warnings=*/false);
5940 /* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference
5941 is being bound to a temporary. Create and return a new VAR_DECL
5942 with the indicated TYPE; this variable will store the value to
5943 which the reference is bound. */
5945 tree
5946 make_temporary_var_for_ref_to_temp (tree decl, tree type)
5948 tree var;
5950 /* Create the variable. */
5951 var = build_decl (VAR_DECL, NULL_TREE, type);
5952 DECL_ARTIFICIAL (var) = 1;
5953 TREE_USED (var) = 1;
5955 /* Register the variable. */
5956 if (TREE_STATIC (decl))
5958 /* Namespace-scope or local static; give it a mangled name. */
5959 tree name;
5961 TREE_STATIC (var) = 1;
5962 name = mangle_ref_init_variable (decl);
5963 DECL_NAME (var) = name;
5964 SET_DECL_ASSEMBLER_NAME (var, name);
5965 var = pushdecl_top_level (var);
5967 else
5969 /* Create a new cleanup level if necessary. */
5970 maybe_push_cleanup_level (type);
5971 /* Don't push unnamed temps. Do set DECL_CONTEXT, though. */
5972 DECL_CONTEXT (var) = current_function_decl;
5975 return var;
5978 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5979 initializing a variable of that TYPE. If DECL is non-NULL, it is
5980 the VAR_DECL being initialized with the EXPR. (In that case, the
5981 type of DECL will be TYPE.)
5983 Return the converted expression. */
5985 tree
5986 initialize_reference (tree type, tree expr, tree decl)
5988 tree conv;
5990 if (type == error_mark_node || error_operand_p (expr))
5991 return error_mark_node;
5993 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5994 if (!conv || ICS_BAD_FLAG (conv))
5996 if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
5997 && !real_lvalue_p (expr))
5998 error ("invalid initialization of non-const reference of "
5999 "type '%T' from a temporary of type '%T'",
6000 type, TREE_TYPE (expr));
6001 else
6002 error ("could not convert `%E' to `%T'", expr, type);
6003 return error_mark_node;
6006 /* If DECL is non-NULL, then this special rule applies:
6008 [class.temporary]
6010 The temporary to which the reference is bound or the temporary
6011 that is the complete object to which the reference is bound
6012 persists for the lifetime of the reference.
6014 The temporaries created during the evaluation of the expression
6015 initializing the reference, except the temporary to which the
6016 reference is bound, are destroyed at the end of the
6017 full-expression in which they are created.
6019 In that case, we store the converted expression into a new
6020 VAR_DECL in a new scope.
6022 However, we want to be careful not to create temporaries when
6023 they are not required. For example, given:
6025 struct B {};
6026 struct D : public B {};
6027 D f();
6028 const B& b = f();
6030 there is no need to copy the return value from "f"; we can just
6031 extend its lifetime. Similarly, given:
6033 struct S {};
6034 struct T { operator S(); };
6035 T t;
6036 const S& s = t;
6038 we can extend the lifetime of the return value of the conversion
6039 operator. */
6040 my_friendly_assert (TREE_CODE (conv) == REF_BIND, 20030302);
6041 if (decl)
6043 tree var;
6044 tree base_conv_type;
6046 /* Skip over the REF_BIND. */
6047 conv = TREE_OPERAND (conv, 0);
6048 /* If the next conversion is a BASE_CONV, skip that too -- but
6049 remember that the conversion was required. */
6050 if (TREE_CODE (conv) == BASE_CONV && !NEED_TEMPORARY_P (conv))
6052 base_conv_type = TREE_TYPE (conv);
6053 conv = TREE_OPERAND (conv, 0);
6055 else
6056 base_conv_type = NULL_TREE;
6057 /* Perform the remainder of the conversion. */
6058 expr = convert_like (conv, expr);
6059 if (!real_non_cast_lvalue_p (expr))
6061 tree init;
6062 tree type;
6064 /* Create the temporary variable. */
6065 type = TREE_TYPE (expr);
6066 var = make_temporary_var_for_ref_to_temp (decl, type);
6067 layout_decl (var, 0);
6068 if (at_function_scope_p ())
6070 tree cleanup;
6072 add_decl_stmt (var);
6073 cleanup = cxx_maybe_build_cleanup (var);
6074 if (cleanup)
6075 finish_decl_cleanup (var, cleanup);
6077 else
6079 rest_of_decl_compilation (var, NULL, /*toplev=*/1, at_eof);
6080 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6081 static_aggregates = tree_cons (NULL_TREE, var,
6082 static_aggregates);
6084 init = build (INIT_EXPR, type, var, expr);
6085 /* Use its address to initialize the reference variable. */
6086 expr = build_address (var);
6087 expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
6089 else
6090 /* Take the address of EXPR. */
6091 expr = build_unary_op (ADDR_EXPR, expr, 0);
6092 /* If a BASE_CONV was required, perform it now. */
6093 if (base_conv_type)
6094 expr = (perform_implicit_conversion
6095 (build_pointer_type (base_conv_type), expr));
6096 return build_nop (type, expr);
6099 /* Perform the conversion. */
6100 return convert_like (conv, expr);
6103 #include "gt-cp-call.h"