]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/call.c
re PR c++/13932 (duplicate warning message for conversion)
[gcc.git] / gcc / cp / call.c
CommitLineData
8d08fdba 1/* Functions related to invoking methods and overloaded functions.
3d938426 2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
521cc508 3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
8d08fdba 4 Contributed by Michael Tiemann (tiemann@cygnus.com) and
e5e809f4 5 modified by Brendan Kehoe (brendan@cygnus.com).
8d08fdba 6
f5adbb8d 7This file is part of GCC.
8d08fdba 8
f5adbb8d 9GCC is free software; you can redistribute it and/or modify
8d08fdba
MS
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2, or (at your option)
12any later version.
13
f5adbb8d 14GCC is distributed in the hope that it will be useful,
8d08fdba
MS
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
f5adbb8d 20along with GCC; see the file COPYING. If not, write to
e9fa0c7c
RK
21the Free Software Foundation, 59 Temple Place - Suite 330,
22Boston, MA 02111-1307, USA. */
8d08fdba
MS
23
24
e92cc029 25/* High-level class interface. */
8d08fdba
MS
26
27#include "config.h"
8d052bc7 28#include "system.h"
4977bab6
ZW
29#include "coretypes.h"
30#include "tm.h"
570221c2 31#include "tree.h"
8d08fdba 32#include "cp-tree.h"
e8abc66f 33#include "output.h"
8d08fdba 34#include "flags.h"
570221c2 35#include "rtl.h"
54f92bfb 36#include "toplev.h"
70a51bda 37#include "expr.h"
2a2b2d43 38#include "diagnostic.h"
d2a6f3c0 39#include "intl.h"
7d149679 40#include "target.h"
7b6d72fc 41#include "convert.h"
8d08fdba 42
94be8403
GDR
43static struct z_candidate * tourney (struct z_candidate *);
44static int equal_functions (tree, tree);
45static int joust (struct z_candidate *, struct z_candidate *, bool);
46static int compare_ics (tree, tree);
b80f8ef3 47static tree build_over_call (struct z_candidate *, int);
94be8403 48static tree build_java_interface_fn_ref (tree, tree);
3fe18f1d
MM
49#define convert_like(CONV, EXPR) \
50 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \
51 /*issue_conversion_warnings=*/true)
52#define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
53 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
54 /*issue_conversion_warnings=*/true)
55static tree convert_like_real (tree, tree, tree, int, int, bool);
94be8403
GDR
56static void op_error (enum tree_code, enum tree_code, tree, tree,
57 tree, const char *);
58static tree build_object_call (tree, tree);
59static tree resolve_args (tree);
60static struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
d2a6f3c0 61static void print_z_candidate (const char *, struct z_candidate *);
94be8403
GDR
62static void print_z_candidates (struct z_candidate *);
63static tree build_this (tree);
436f8a4c 64static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
94be8403
GDR
65static bool any_strictly_viable (struct z_candidate *);
66static struct z_candidate *add_template_candidate
7993382e 67 (struct z_candidate **, tree, tree, tree, tree, tree,
94be8403
GDR
68 tree, tree, int, unification_kind_t);
69static struct z_candidate *add_template_candidate_real
7993382e 70 (struct z_candidate **, tree, tree, tree, tree, tree,
94be8403
GDR
71 tree, tree, int, tree, unification_kind_t);
72static struct z_candidate *add_template_conv_candidate
7993382e
MM
73 (struct z_candidate **, tree, tree, tree, tree, tree, tree);
74static void add_builtin_candidates
75 (struct z_candidate **, enum tree_code, enum tree_code,
94be8403 76 tree, tree *, int);
7993382e
MM
77static void add_builtin_candidate
78 (struct z_candidate **, enum tree_code, enum tree_code,
94be8403
GDR
79 tree, tree, tree, tree *, tree *, int);
80static bool is_complete (tree);
7993382e
MM
81static void build_builtin_candidate
82 (struct z_candidate **, tree, tree, tree, tree *, tree *,
94be8403
GDR
83 int);
84static struct z_candidate *add_conv_candidate
7993382e 85 (struct z_candidate **, tree, tree, tree, tree, tree);
94be8403 86static struct z_candidate *add_function_candidate
7993382e 87 (struct z_candidate **, tree, tree, tree, tree, tree, int);
94be8403
GDR
88static tree implicit_conversion (tree, tree, tree, int);
89static tree standard_conversion (tree, tree, tree);
aa6e8ed3 90static tree reference_binding (tree, tree, tree, int);
94be8403
GDR
91static tree build_conv (enum tree_code, tree, tree);
92static bool is_subseq (tree, tree);
93static tree maybe_handle_ref_bind (tree *);
94static void maybe_handle_implicit_object (tree *);
4ba126e4 95static struct z_candidate *add_candidate
7993382e 96 (struct z_candidate **, tree, tree, tree, tree, tree, int);
94be8403
GDR
97static tree source_type (tree);
98static void add_warning (struct z_candidate *, struct z_candidate *);
99static bool reference_related_p (tree, tree);
100static bool reference_compatible_p (tree, tree);
101static tree convert_class_to_reference (tree, tree, tree);
102static tree direct_reference_binding (tree, tree);
103static bool promoted_arithmetic_type_p (tree);
104static tree conditional_conversion (tree, tree);
a723baf1 105static char *name_as_c_string (tree, tree, bool *);
a6f86b51 106static tree call_builtin_trap (tree);
14d22dd6 107static tree prep_operand (tree);
125e6594 108static void add_candidates (tree, tree, tree, bool, tree, tree,
7993382e 109 int, struct z_candidate **);
3d938426 110static tree merge_conversion_sequences (tree, tree);
a6f86b51 111static bool magic_varargs_p (tree);
49c249e1 112
277294d7 113tree
94be8403 114build_vfield_ref (tree datum, tree type)
8d08fdba 115{
277294d7
JM
116 if (datum == error_mark_node)
117 return error_mark_node;
e92cc029 118
277294d7
JM
119 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
120 datum = convert_from_reference (datum);
8d08fdba 121
50ad9642
MM
122 if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
123 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
124 datum = convert_to_base (datum, type, /*check_access=*/false);
8d08fdba 125
50ad9642
MM
126 return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
127 datum, TYPE_VFIELD (type));
8d08fdba
MS
128}
129
1c2c08a5
JM
130/* Returns nonzero iff the destructor name specified in NAME
131 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
132 forms... */
133
94be8403
GDR
134bool
135check_dtor_name (tree basetype, tree name)
1c2c08a5
JM
136{
137 name = TREE_OPERAND (name, 0);
138
ee996e9e 139 /* Just accept something we've already complained about. */
f3400fe2 140 if (name == error_mark_node)
94be8403 141 return true;
f3400fe2 142
1c2c08a5
JM
143 if (TREE_CODE (name) == TYPE_DECL)
144 name = TREE_TYPE (name);
2f939d94 145 else if (TYPE_P (name))
1c2c08a5
JM
146 /* OK */;
147 else if (TREE_CODE (name) == IDENTIFIER_NODE)
148 {
26877584
JM
149 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
150 || (TREE_CODE (basetype) == ENUMERAL_TYPE
151 && name == TYPE_IDENTIFIER (basetype)))
1c2c08a5
JM
152 name = basetype;
153 else
154 name = get_type_value (name);
155 }
8084b91e
MM
156 /* In the case of:
157
158 template <class T> struct S { ~S(); };
159 int i;
160 i.~S();
161
162 NAME will be a class template. */
163 else if (DECL_CLASS_TEMPLATE_P (name))
94be8403 164 return false;
1c2c08a5 165 else
a98facb0 166 abort ();
1c2c08a5
JM
167
168 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
94be8403
GDR
169 return true;
170 return false;
1c2c08a5
JM
171}
172
277294d7
JM
173/* We want the address of a function or method. We avoid creating a
174 pointer-to-member function. */
175
176tree
94be8403 177build_addr_func (tree function)
277294d7
JM
178{
179 tree type = TREE_TYPE (function);
878cd289 180
277294d7
JM
181 /* We have to do these by hand to avoid real pointer to member
182 functions. */
183 if (TREE_CODE (type) == METHOD_TYPE)
8d08fdba 184 {
d6b4ea85
MM
185 if (TREE_CODE (function) == OFFSET_REF)
186 {
187 tree object = build_address (TREE_OPERAND (function, 0));
188 return get_member_function_from_ptrfunc (&object,
189 TREE_OPERAND (function, 1));
190 }
191 function = build_address (function);
277294d7
JM
192 }
193 else
0a72704b 194 function = decay_conversion (function);
8d08fdba 195
277294d7
JM
196 return function;
197}
8d08fdba 198
277294d7
JM
199/* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
200 POINTER_TYPE to those. Note, pointer to member function types
201 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
8d08fdba
MS
202
203tree
94be8403 204build_call (tree function, tree parms)
8d08fdba 205{
277294d7 206 int is_constructor = 0;
12a22e76 207 int nothrow;
570221c2 208 tree tmp;
7c76b292 209 tree decl;
0c11ada6 210 tree result_type;
5aa3396c 211 tree fntype;
8d08fdba 212
277294d7 213 function = build_addr_func (function);
8d08fdba 214
277294d7 215 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
8d08fdba 216 {
277294d7 217 sorry ("unable to call pointer to member function here");
ce122a86 218 return error_mark_node;
8d08fdba 219 }
ce122a86 220
5aa3396c
JM
221 fntype = TREE_TYPE (TREE_TYPE (function));
222 result_type = TREE_TYPE (fntype);
0c11ada6 223
277294d7 224 if (TREE_CODE (function) == ADDR_EXPR
7c76b292
JM
225 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
226 decl = TREE_OPERAND (function, 0);
227 else
228 decl = NULL_TREE;
229
12a22e76
JM
230 /* We check both the decl and the type; a function may be known not to
231 throw without being declared throw(). */
232 nothrow = ((decl && TREE_NOTHROW (decl))
233 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
e23bd218 234
1a55127d 235 if (decl && TREE_THIS_VOLATILE (decl) && cfun)
efe49da0
JM
236 current_function_returns_abnormally = 1;
237
e23bd218
IR
238 if (decl && TREE_DEPRECATED (decl))
239 warn_deprecated_use (decl);
5aa3396c 240 require_complete_eh_spec_types (fntype, decl);
e23bd218 241
7c76b292 242 if (decl && DECL_CONSTRUCTOR_P (decl))
277294d7 243 is_constructor = 1;
8d08fdba 244
0c11ada6
JM
245 if (decl && ! TREE_USED (decl))
246 {
f49fad00
JM
247 /* We invoke build_call directly for several library functions.
248 These may have been declared normally if we're building libgcc,
249 so we can't just check DECL_ARTIFICIAL. */
250 if (DECL_ARTIFICIAL (decl)
251 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
0c11ada6
JM
252 mark_used (decl);
253 else
a98facb0 254 abort ();
0c11ada6 255 }
a6ecf8b6 256
7c76b292 257 /* Don't pass empty class objects by value. This is useful
570221c2
JM
258 for tags in STL, which are used to control overload resolution.
259 We don't need to handle other cases of copying empty classes. */
7c76b292
JM
260 if (! decl || ! DECL_BUILT_IN (decl))
261 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
262 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
263 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
264 {
699ed0ce 265 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
a59ca936
JM
266 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
267 TREE_VALUE (tmp), t);
7c76b292 268 }
570221c2 269
20b50672 270 function = build (CALL_EXPR, result_type, function, parms);
277294d7 271 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
12a22e76 272 TREE_NOTHROW (function) = nothrow;
277294d7
JM
273
274 return function;
275}
8d08fdba 276
277294d7
JM
277/* Build something of the form ptr->method (args)
278 or object.method (args). This can also build
279 calls to constructors, and find friends.
8d08fdba 280
277294d7
JM
281 Member functions always take their class variable
282 as a pointer.
8d08fdba 283
277294d7 284 INSTANCE is a class instance.
8d08fdba 285
277294d7 286 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
8d08fdba 287
277294d7 288 PARMS help to figure out what that NAME really refers to.
8d08fdba 289
277294d7
JM
290 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
291 down to the real instance type to use for access checking. We need this
d17811fd 292 information to get protected accesses correct.
8d08fdba 293
277294d7
JM
294 FLAGS is the logical disjunction of zero or more LOOKUP_
295 flags. See cp-tree.h for more info.
8d08fdba 296
277294d7
JM
297 If this is all OK, calls build_function_call with the resolved
298 member function.
a4443a08 299
277294d7
JM
300 This function must also handle being called to perform
301 initialization, promotion/coercion of arguments, and
302 instantiation of default parameters.
a4443a08 303
277294d7
JM
304 Note that NAME may refer to an instance variable name. If
305 `operator()()' is defined for the type of that field, then we return
306 that result. */
8d08fdba 307
c73964b2
MS
308/* New overloading code. */
309
e2500fed 310struct z_candidate GTY(()) {
4ba126e4
MM
311 /* The FUNCTION_DECL that will be called if this candidate is
312 selected by overload resolution. */
c73964b2 313 tree fn;
b80f8ef3
MM
314 /* The arguments to use when calling this function. */
315 tree args;
3d938426
MM
316 /* The implicit conversion sequences for each of the arguments to
317 FN. */
c73964b2 318 tree convs;
3d938426
MM
319 /* If FN is a user-defined conversion, the standard conversion
320 sequence from the type returned by FN to the desired destination
321 type. */
c73964b2
MS
322 tree second_conv;
323 int viable;
4ba126e4
MM
324 /* If FN is a member function, the binfo indicating the path used to
325 qualify the name of FN at the call site. This path is used to
326 determine whether or not FN is accessible if it is selected by
327 overload resolution. The DECL_CONTEXT of FN will always be a
328 (possibly improper) base of this binfo. */
329 tree access_path;
330 /* If FN is a non-static member function, the binfo indicating the
331 subobject to which the `this' pointer should be converted if FN
332 is selected by overload resolution. The type pointed to the by
333 the `this' pointer must correspond to the most derived class
334 indicated by the CONVERSION_PATH. */
335 tree conversion_path;
c73964b2 336 tree template;
5ffe581d 337 tree warnings;
c73964b2
MS
338 struct z_candidate *next;
339};
340
341#define IDENTITY_RANK 0
342#define EXACT_RANK 1
343#define PROMO_RANK 2
344#define STD_RANK 3
345#define PBOOL_RANK 4
346#define USER_RANK 5
347#define ELLIPSIS_RANK 6
d11ad92e 348#define BAD_RANK 7
c73964b2
MS
349
350#define ICS_RANK(NODE) \
f11f20b0 351 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
d11ad92e 352 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
c73964b2
MS
353 : ICS_USER_FLAG (NODE) ? USER_RANK \
354 : ICS_STD_RANK (NODE))
355
356#define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
357
358#define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
359#define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
d11ad92e
MS
360#define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
361#define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
c73964b2 362
27b8d0cd
MM
363/* In a REF_BIND or a BASE_CONV, this indicates that a temporary
364 should be created to hold the result of the conversion. */
f11f20b0 365#define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
27b8d0cd 366
4f8163b1
MM
367/* TRUE in an IDENTITY_CONV or BASE_CONV if the copy constructor must
368 be accessible, even though it is not being used. */
369#define CHECK_COPY_CONSTRUCTOR_P(NODE) TREE_LANG_FLAG_5 (NODE)
370
e2500fed 371#define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
5ffe581d 372#define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
c73964b2 373
94be8403
GDR
374bool
375null_ptr_cst_p (tree t)
c73964b2 376{
a7a64a77
MM
377 /* [conv.ptr]
378
379 A null pointer constant is an integral constant expression
380 (_expr.const_) rvalue of integer type that evaluates to zero. */
d11ad92e 381 if (t == null_node
a7a64a77 382 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
94be8403
GDR
383 return true;
384 return false;
c73964b2
MS
385}
386
a11d04b5 387
838dfd8a 388/* Returns nonzero if PARMLIST consists of only default parms and/or
00a17e31 389 ellipsis. */
a11d04b5 390
94be8403
GDR
391bool
392sufficient_parms_p (tree parmlist)
a11d04b5
NS
393{
394 for (; parmlist && parmlist != void_list_node;
395 parmlist = TREE_CHAIN (parmlist))
396 if (!TREE_PURPOSE (parmlist))
94be8403
GDR
397 return false;
398 return true;
a11d04b5
NS
399}
400
824b9a4c 401static tree
94be8403 402build_conv (enum tree_code code, tree type, tree from)
c73964b2 403{
519c9806 404 tree t;
c73964b2 405 int rank = ICS_STD_RANK (from);
519c9806 406
4cff6abe 407 /* We can't use buildl1 here because CODE could be USER_CONV, which
519c9806
MM
408 takes two arguments. In that case, the caller is responsible for
409 filling in the second argument. */
410 t = make_node (code);
411 TREE_TYPE (t) = type;
412 TREE_OPERAND (t, 0) = from;
413
c73964b2
MS
414 switch (code)
415 {
416 case PTR_CONV:
417 case PMEM_CONV:
418 case BASE_CONV:
419 case STD_CONV:
420 if (rank < STD_RANK)
421 rank = STD_RANK;
422 break;
423
c73964b2 424 case QUAL_CONV:
c73964b2
MS
425 if (rank < EXACT_RANK)
426 rank = EXACT_RANK;
427
428 default:
429 break;
430 }
431 ICS_STD_RANK (t) = rank;
3d938426 432 ICS_USER_FLAG (t) = (code == USER_CONV || ICS_USER_FLAG (from));
d11ad92e 433 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
c73964b2
MS
434 return t;
435}
436
a7a64a77 437tree
94be8403 438strip_top_quals (tree t)
de22184b
MS
439{
440 if (TREE_CODE (t) == ARRAY_TYPE)
441 return t;
7d149679 442 return cp_build_qualified_type (t, 0);
de22184b
MS
443}
444
c73964b2
MS
445/* Returns the standard conversion path (see [conv]) from type FROM to type
446 TO, if any. For proper handling of null pointer constants, you must
447 also pass the expression EXPR to convert from. */
448
824b9a4c 449static tree
94be8403 450standard_conversion (tree to, tree from, tree expr)
c73964b2
MS
451{
452 enum tree_code fcode, tcode;
453 tree conv;
94be8403 454 bool fromref = false;
de22184b 455
ee76b931 456 to = non_reference (to);
de22184b
MS
457 if (TREE_CODE (from) == REFERENCE_TYPE)
458 {
94be8403 459 fromref = true;
de22184b
MS
460 from = TREE_TYPE (from);
461 }
462 to = strip_top_quals (to);
463 from = strip_top_quals (from);
c73964b2 464
e6e174e5
JM
465 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
466 && expr && type_unknown_p (expr))
467 {
84583208 468 expr = instantiate_type (to, expr, tf_conv);
e6e174e5
JM
469 if (expr == error_mark_node)
470 return NULL_TREE;
471 from = TREE_TYPE (expr);
472 }
473
c73964b2
MS
474 fcode = TREE_CODE (from);
475 tcode = TREE_CODE (to);
476
477 conv = build1 (IDENTITY_CONV, from, expr);
478
c73964b2
MS
479 if (fcode == FUNCTION_TYPE)
480 {
481 from = build_pointer_type (from);
482 fcode = TREE_CODE (from);
483 conv = build_conv (LVALUE_CONV, from, conv);
484 }
485 else if (fcode == ARRAY_TYPE)
486 {
487 from = build_pointer_type (TREE_TYPE (from));
488 fcode = TREE_CODE (from);
489 conv = build_conv (LVALUE_CONV, from, conv);
490 }
583ca5a0 491 else if (fromref || (expr && lvalue_p (expr)))
de22184b
MS
492 conv = build_conv (RVALUE_CONV, from, conv);
493
04c06002 494 /* Allow conversion between `__complex__' data types. */
a04678ca
GDR
495 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
496 {
497 /* The standard conversion sequence to convert FROM to TO is
498 the standard conversion sequence to perform componentwise
499 conversion. */
500 tree part_conv = standard_conversion
501 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
502
503 if (part_conv)
504 {
505 conv = build_conv (TREE_CODE (part_conv), to, conv);
506 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
507 }
508 else
509 conv = NULL_TREE;
510
511 return conv;
512 }
513
a7a64a77 514 if (same_type_p (from, to))
de22184b 515 return conv;
c73964b2 516
a5ac359a 517 if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to))
c73964b2 518 && expr && null_ptr_cst_p (expr))
a5ac359a 519 conv = build_conv (STD_CONV, to, conv);
479d960b
AH
520 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE
521 && TREE_CODE (TREE_TYPE (to)) == VECTOR_TYPE
522 && TREE_CODE (TREE_TYPE (from)) == VECTOR_TYPE
523 && ((*targetm.vector_opaque_p) (TREE_TYPE (to))
524 || (*targetm.vector_opaque_p) (TREE_TYPE (from))))
525 conv = build_conv (STD_CONV, to, conv);
72a08131
JM
526 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
527 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
528 {
529 /* For backwards brain damage compatibility, allow interconversion of
530 pointers and integers with a pedwarn. */
531 conv = build_conv (STD_CONV, to, conv);
532 ICS_BAD_FLAG (conv) = 1;
533 }
7b6d72fc 534 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE)
8a2b77e7
JM
535 {
536 /* For backwards brain damage compatibility, allow interconversion of
537 enums and integers with a pedwarn. */
538 conv = build_conv (STD_CONV, to, conv);
539 ICS_BAD_FLAG (conv) = 1;
540 }
a5ac359a
MM
541 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
542 || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
c73964b2 543 {
a5ac359a
MM
544 tree to_pointee;
545 tree from_pointee;
c73964b2 546
a5ac359a
MM
547 if (tcode == POINTER_TYPE
548 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
549 TREE_TYPE (to)))
4d50dd69 550 ;
a5ac359a
MM
551 else if (VOID_TYPE_P (TREE_TYPE (to))
552 && !TYPE_PTRMEM_P (from)
553 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
c73964b2
MS
554 {
555 from = build_pointer_type
91063b51 556 (cp_build_qualified_type (void_type_node,
89d684bb 557 cp_type_quals (TREE_TYPE (from))));
798eed5e 558 conv = build_conv (PTR_CONV, from, conv);
c73964b2 559 }
a5ac359a 560 else if (TYPE_PTRMEM_P (from))
c73964b2 561 {
a5ac359a
MM
562 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
563 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
c73964b2 564
999cc24c 565 if (DERIVED_FROM_P (fbase, tbase)
9edc3913 566 && (same_type_ignoring_top_level_qualifiers_p
a5ac359a
MM
567 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
568 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
c73964b2 569 {
a5ac359a
MM
570 from = build_ptrmem_type (tbase,
571 TYPE_PTRMEM_POINTED_TO_TYPE (from));
798eed5e 572 conv = build_conv (PMEM_CONV, from, conv);
c73964b2 573 }
c73964b2
MS
574 }
575 else if (IS_AGGR_TYPE (TREE_TYPE (from))
385bce06
MM
576 && IS_AGGR_TYPE (TREE_TYPE (to))
577 /* [conv.ptr]
578
579 An rvalue of type "pointer to cv D," where D is a
580 class type, can be converted to an rvalue of type
581 "pointer to cv B," where B is a base class (clause
582 _class.derived_) of D. If B is an inaccessible
583 (clause _class.access_) or ambiguous
584 (_class.member.lookup_) base class of D, a program
585 that necessitates this conversion is ill-formed. */
586 /* Therefore, we use DERIVED_FROM_P, and not
587 ACESSIBLY_UNIQUELY_DERIVED_FROM_P, in this test. */
588 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
c73964b2 589 {
385bce06
MM
590 from =
591 cp_build_qualified_type (TREE_TYPE (to),
592 cp_type_quals (TREE_TYPE (from)));
593 from = build_pointer_type (from);
594 conv = build_conv (PTR_CONV, from, conv);
c73964b2 595 }
c73964b2 596
a5ac359a
MM
597 if (tcode == POINTER_TYPE)
598 {
599 to_pointee = TREE_TYPE (to);
600 from_pointee = TREE_TYPE (from);
601 }
602 else
603 {
b7a78333
MM
604 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
605 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
a5ac359a
MM
606 }
607
3bfdc719 608 if (same_type_p (from, to))
798eed5e 609 /* OK */;
a5ac359a 610 else if (comp_ptr_ttypes (to_pointee, from_pointee))
798eed5e 611 conv = build_conv (QUAL_CONV, to, conv);
d9cf7c82
JM
612 else if (expr && string_conv_p (to, expr, 0))
613 /* converting from string constant to char *. */
614 conv = build_conv (QUAL_CONV, to, conv);
a5ac359a 615 else if (ptr_reasonably_similar (to_pointee, from_pointee))
c73964b2 616 {
d11ad92e
MS
617 conv = build_conv (PTR_CONV, to, conv);
618 ICS_BAD_FLAG (conv) = 1;
c73964b2 619 }
d11ad92e
MS
620 else
621 return 0;
622
623 from = to;
c73964b2
MS
624 }
625 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
626 {
627 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
628 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
629 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
630 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
631
999cc24c 632 if (!DERIVED_FROM_P (fbase, tbase)
13f9714b
NS
633 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
634 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
635 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
89d684bb 636 || cp_type_quals (fbase) != cp_type_quals (tbase))
c73964b2
MS
637 return 0;
638
89d684bb 639 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
43dc123f
MM
640 from = build_method_type_directly (from,
641 TREE_TYPE (fromfn),
642 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
c73964b2
MS
643 from = build_ptrmemfunc_type (build_pointer_type (from));
644 conv = build_conv (PMEM_CONV, from, conv);
645 }
646 else if (tcode == BOOLEAN_TYPE)
647 {
a5ac359a 648 /* [conv.bool]
c73964b2 649
a5ac359a
MM
650 An rvalue of arithmetic, enumeration, pointer, or pointer to
651 member type can be converted to an rvalue of type bool. */
652 if (ARITHMETIC_TYPE_P (from)
653 || fcode == ENUMERAL_TYPE
654 || fcode == POINTER_TYPE
655 || TYPE_PTR_TO_MEMBER_P (from))
656 {
657 conv = build_conv (STD_CONV, to, conv);
658 if (fcode == POINTER_TYPE
659 || TYPE_PTRMEM_P (from)
660 || (TYPE_PTRMEMFUNC_P (from)
661 && ICS_STD_RANK (conv) < PBOOL_RANK))
662 ICS_STD_RANK (conv) = PBOOL_RANK;
663 return conv;
664 }
665
666 return NULL_TREE;
c73964b2
MS
667 }
668 /* We don't check for ENUMERAL_TYPE here because there are no standard
669 conversions to enum type. */
670 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
671 || tcode == REAL_TYPE)
672 {
673 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
674 return 0;
675 conv = build_conv (STD_CONV, to, conv);
676
677 /* Give this a better rank if it's a promotion. */
f3c2dfc6 678 if (same_type_p (to, type_promotes_to (from))
c73964b2
MS
679 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
680 ICS_STD_RANK (conv) = PROMO_RANK;
681 }
7d149679
AH
682 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
683 && ((*targetm.vector_opaque_p) (from)
684 || (*targetm.vector_opaque_p) (to)))
685 return build_conv (STD_CONV, to, conv);
c73964b2 686 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
a7a64a77 687 && is_properly_derived_from (from, to))
2dbfb418
JM
688 {
689 if (TREE_CODE (conv) == RVALUE_CONV)
690 conv = TREE_OPERAND (conv, 0);
691 conv = build_conv (BASE_CONV, to, conv);
27b8d0cd
MM
692 /* The derived-to-base conversion indicates the initialization
693 of a parameter with base type from an object of a derived
694 type. A temporary object is created to hold the result of
695 the conversion. */
696 NEED_TEMPORARY_P (conv) = 1;
2dbfb418 697 }
c73964b2
MS
698 else
699 return 0;
700
701 return conv;
702}
703
838dfd8a 704/* Returns nonzero if T1 is reference-related to T2. */
27b8d0cd 705
94be8403
GDR
706static bool
707reference_related_p (tree t1, tree t2)
27b8d0cd
MM
708{
709 t1 = TYPE_MAIN_VARIANT (t1);
710 t2 = TYPE_MAIN_VARIANT (t2);
711
712 /* [dcl.init.ref]
713
714 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
715 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
716 of T2. */
717 return (same_type_p (t1, t2)
718 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
719 && DERIVED_FROM_P (t1, t2)));
720}
721
838dfd8a 722/* Returns nonzero if T1 is reference-compatible with T2. */
27b8d0cd 723
94be8403
GDR
724static bool
725reference_compatible_p (tree t1, tree t2)
27b8d0cd
MM
726{
727 /* [dcl.init.ref]
728
729 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
730 reference-related to T2 and cv1 is the same cv-qualification as,
731 or greater cv-qualification than, cv2. */
732 return (reference_related_p (t1, t2)
733 && at_least_as_qualified_p (t1, t2));
734}
735
736/* Determine whether or not the EXPR (of class type S) can be
737 converted to T as in [over.match.ref]. */
738
739static tree
94be8403 740convert_class_to_reference (tree t, tree s, tree expr)
27b8d0cd
MM
741{
742 tree conversions;
743 tree arglist;
744 tree conv;
7993382e 745 tree reference_type;
27b8d0cd
MM
746 struct z_candidate *candidates;
747 struct z_candidate *cand;
436f8a4c 748 bool any_viable_p;
27b8d0cd 749
7993382e
MM
750 conversions = lookup_conversions (s);
751 if (!conversions)
752 return NULL_TREE;
753
27b8d0cd
MM
754 /* [over.match.ref]
755
756 Assuming that "cv1 T" is the underlying type of the reference
757 being initialized, and "cv S" is the type of the initializer
758 expression, with S a class type, the candidate functions are
759 selected as follows:
760
761 --The conversion functions of S and its base classes are
762 considered. Those that are not hidden within S and yield type
763 "reference to cv2 T2", where "cv1 T" is reference-compatible
764 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
765
766 The argument list has one argument, which is the initializer
767 expression. */
768
769 candidates = 0;
770
771 /* Conceptually, we should take the address of EXPR and put it in
772 the argument list. Unfortunately, however, that can result in
773 error messages, which we should not issue now because we are just
774 trying to find a conversion operator. Therefore, we use NULL,
775 cast to the appropriate type. */
776 arglist = build_int_2 (0, 0);
777 TREE_TYPE (arglist) = build_pointer_type (s);
051e6fd7 778 arglist = build_tree_list (NULL_TREE, arglist);
7993382e
MM
779
780 reference_type = build_reference_type (t);
781
782 while (conversions)
27b8d0cd
MM
783 {
784 tree fns = TREE_VALUE (conversions);
785
aa52c1ff 786 for (; fns; fns = OVL_NEXT (fns))
27b8d0cd
MM
787 {
788 tree f = OVL_CURRENT (fns);
789 tree t2 = TREE_TYPE (TREE_TYPE (f));
7993382e
MM
790
791 cand = NULL;
27b8d0cd
MM
792
793 /* If this is a template function, try to get an exact
794 match. */
795 if (TREE_CODE (f) == TEMPLATE_DECL)
796 {
7993382e
MM
797 cand = add_template_candidate (&candidates,
798 f, s,
799 NULL_TREE,
800 arglist,
801 reference_type,
802 TYPE_BINFO (s),
803 TREE_PURPOSE (conversions),
804 LOOKUP_NORMAL,
805 DEDUCE_CONV);
27b8d0cd 806
7993382e 807 if (cand)
27b8d0cd
MM
808 {
809 /* Now, see if the conversion function really returns
810 an lvalue of the appropriate type. From the
811 point of view of unification, simply returning an
812 rvalue of the right type is good enough. */
7993382e 813 f = cand->fn;
27b8d0cd
MM
814 t2 = TREE_TYPE (TREE_TYPE (f));
815 if (TREE_CODE (t2) != REFERENCE_TYPE
816 || !reference_compatible_p (t, TREE_TYPE (t2)))
7993382e
MM
817 {
818 candidates = candidates->next;
819 cand = NULL;
820 }
27b8d0cd
MM
821 }
822 }
823 else if (TREE_CODE (t2) == REFERENCE_TYPE
824 && reference_compatible_p (t, TREE_TYPE (t2)))
7993382e
MM
825 cand = add_function_candidate (&candidates, f, s, arglist,
826 TYPE_BINFO (s),
827 TREE_PURPOSE (conversions),
828 LOOKUP_NORMAL);
829
830 if (cand)
e9525111
MM
831 {
832 /* Build a standard conversion sequence indicating the
833 binding from the reference type returned by the
834 function to the desired REFERENCE_TYPE. */
835 cand->second_conv
836 = (direct_reference_binding
837 (reference_type,
838 build1 (IDENTITY_CONV,
839 TREE_TYPE (TREE_TYPE (TREE_TYPE (cand->fn))),
840 NULL_TREE)));
841 ICS_BAD_FLAG (cand->second_conv)
842 |= ICS_BAD_FLAG (TREE_VEC_ELT (cand->convs, 0));
843 }
27b8d0cd 844 }
7993382e 845 conversions = TREE_CHAIN (conversions);
27b8d0cd
MM
846 }
847
436f8a4c 848 candidates = splice_viable (candidates, pedantic, &any_viable_p);
27b8d0cd
MM
849 /* If none of the conversion functions worked out, let our caller
850 know. */
436f8a4c 851 if (!any_viable_p)
27b8d0cd 852 return NULL_TREE;
436f8a4c 853
27b8d0cd
MM
854 cand = tourney (candidates);
855 if (!cand)
856 return NULL_TREE;
857
b80f8ef3
MM
858 /* Now that we know that this is the function we're going to use fix
859 the dummy first argument. */
860 cand->args = tree_cons (NULL_TREE,
861 build_this (expr),
862 TREE_CHAIN (cand->args));
863
3d938426
MM
864 /* Build a user-defined conversion sequence representing the
865 conversion. */
866 conv = build_conv (USER_CONV,
867 TREE_TYPE (TREE_TYPE (cand->fn)),
868 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
869 TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
870
871 /* Merge it with the standard conversion sequence from the
872 conversion function's return type to the desired type. */
873 cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
874
27b8d0cd
MM
875 if (cand->viable == -1)
876 ICS_BAD_FLAG (conv) = 1;
7993382e 877
3d938426 878 return cand->second_conv;
27b8d0cd
MM
879}
880
881/* A reference of the indicated TYPE is being bound directly to the
882 expression represented by the implicit conversion sequence CONV.
883 Return a conversion sequence for this binding. */
884
885static tree
94be8403 886direct_reference_binding (tree type, tree conv)
27b8d0cd 887{
3d938426
MM
888 tree t;
889
890 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 20030306);
891 my_friendly_assert (TREE_CODE (TREE_TYPE (conv)) != REFERENCE_TYPE,
892 20030306);
893
894 t = TREE_TYPE (type);
27b8d0cd
MM
895
896 /* [over.ics.rank]
897
898 When a parameter of reference type binds directly
899 (_dcl.init.ref_) to an argument expression, the implicit
900 conversion sequence is the identity conversion, unless the
901 argument expression has a type that is a derived class of the
902 parameter type, in which case the implicit conversion sequence is
903 a derived-to-base Conversion.
904
905 If the parameter binds directly to the result of applying a
906 conversion function to the argument expression, the implicit
907 conversion sequence is a user-defined conversion sequence
908 (_over.ics.user_), with the second standard conversion sequence
909 either an identity conversion or, if the conversion function
910 returns an entity of a type that is a derived class of the
911 parameter type, a derived-to-base conversion. */
9edc3913 912 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
27b8d0cd
MM
913 {
914 /* Represent the derived-to-base conversion. */
915 conv = build_conv (BASE_CONV, t, conv);
916 /* We will actually be binding to the base-class subobject in
917 the derived class, so we mark this conversion appropriately.
918 That way, convert_like knows not to generate a temporary. */
919 NEED_TEMPORARY_P (conv) = 0;
920 }
921 return build_conv (REF_BIND, type, conv);
922}
923
c73964b2
MS
924/* Returns the conversion path from type FROM to reference type TO for
925 purposes of reference binding. For lvalue binding, either pass a
c7f9c6f5
MM
926 reference type to FROM or an lvalue expression to EXPR. If the
927 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
aa6e8ed3 928 the conversion returned. */
c73964b2 929
824b9a4c 930static tree
aa6e8ed3 931reference_binding (tree rto, tree rfrom, tree expr, int flags)
c73964b2 932{
27b8d0cd 933 tree conv = NULL_TREE;
c73964b2 934 tree to = TREE_TYPE (rto);
de22184b 935 tree from = rfrom;
94be8403
GDR
936 bool related_p;
937 bool compatible_p;
27b8d0cd 938 cp_lvalue_kind lvalue_p = clk_none;
c73964b2 939
e6e174e5
JM
940 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
941 {
c2ea3a40 942 expr = instantiate_type (to, expr, tf_none);
e6e174e5
JM
943 if (expr == error_mark_node)
944 return NULL_TREE;
945 from = TREE_TYPE (expr);
946 }
947
27b8d0cd
MM
948 if (TREE_CODE (from) == REFERENCE_TYPE)
949 {
950 /* Anything with reference type is an lvalue. */
951 lvalue_p = clk_ordinary;
952 from = TREE_TYPE (from);
953 }
954 else if (expr)
955 lvalue_p = real_lvalue_p (expr);
eb66be0e 956
b0385db8
MM
957 /* Figure out whether or not the types are reference-related and
958 reference compatible. We have do do this after stripping
959 references from FROM. */
960 related_p = reference_related_p (to, from);
961 compatible_p = reference_compatible_p (to, from);
962
27b8d0cd 963 if (lvalue_p && compatible_p)
c73964b2 964 {
27b8d0cd 965 /* [dcl.init.ref]
c73964b2 966
aba649ba 967 If the initializer expression
27b8d0cd
MM
968
969 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
970 is reference-compatible with "cv2 T2,"
971
34cd5ae7 972 the reference is bound directly to the initializer expression
27b8d0cd
MM
973 lvalue. */
974 conv = build1 (IDENTITY_CONV, from, expr);
975 conv = direct_reference_binding (rto, conv);
e0d1297c
NS
976 if ((lvalue_p & clk_bitfield) != 0
977 || ((lvalue_p & clk_packed) != 0 && !TYPE_PACKED (to)))
27b8d0cd 978 /* For the purposes of overload resolution, we ignore the fact
e0d1297c 979 this expression is a bitfield or packed field. (In particular,
27b8d0cd
MM
980 [over.ics.ref] says specifically that a function with a
981 non-const reference parameter is viable even if the
982 argument is a bitfield.)
983
984 However, when we actually call the function we must create
985 a temporary to which to bind the reference. If the
986 reference is volatile, or isn't const, then we cannot make
987 a temporary, so we just issue an error when the conversion
988 actually occurs. */
989 NEED_TEMPORARY_P (conv) = 1;
e0d1297c 990
27b8d0cd 991 return conv;
c73964b2 992 }
27b8d0cd 993 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
c73964b2 994 {
27b8d0cd
MM
995 /* [dcl.init.ref]
996
34cd5ae7 997 If the initializer expression
27b8d0cd
MM
998
999 -- has a class type (i.e., T2 is a class type) can be
1000 implicitly converted to an lvalue of type "cv3 T3," where
1001 "cv1 T1" is reference-compatible with "cv3 T3". (this
1002 conversion is selected by enumerating the applicable
1003 conversion functions (_over.match.ref_) and choosing the
1004 best one through overload resolution. (_over.match_).
1005
1006 the reference is bound to the lvalue result of the conversion
1007 in the second case. */
1008 conv = convert_class_to_reference (to, from, expr);
c73964b2 1009 if (conv)
7993382e 1010 return conv;
27b8d0cd 1011 }
c73964b2 1012
a7a64a77
MM
1013 /* From this point on, we conceptually need temporaries, even if we
1014 elide them. Only the cases above are "direct bindings". */
1015 if (flags & LOOKUP_NO_TEMP_BIND)
1016 return NULL_TREE;
1017
27b8d0cd
MM
1018 /* [over.ics.rank]
1019
1020 When a parameter of reference type is not bound directly to an
1021 argument expression, the conversion sequence is the one required
1022 to convert the argument expression to the underlying type of the
1023 reference according to _over.best.ics_. Conceptually, this
1024 conversion sequence corresponds to copy-initializing a temporary
1025 of the underlying type with the argument expression. Any
1026 difference in top-level cv-qualification is subsumed by the
1027 initialization itself and does not constitute a conversion. */
1028
1029 /* [dcl.init.ref]
1030
1031 Otherwise, the reference shall be to a non-volatile const type. */
1032 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1033 return NULL_TREE;
1034
1035 /* [dcl.init.ref]
1036
1037 If the initializer expression is an rvalue, with T2 a class type,
1038 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1039 is bound in one of the following ways:
1040
1041 -- The reference is bound to the object represented by the rvalue
1042 or to a sub-object within that object.
1043
aa6e8ed3 1044 -- ...
5e256996 1045
aa6e8ed3
MM
1046 We use the first alternative. The implicit conversion sequence
1047 is supposed to be same as we would obtain by generating a
1048 temporary. Fortunately, if the types are reference compatible,
1049 then this is either an identity conversion or the derived-to-base
1050 conversion, just as for direct binding. */
1051 if (CLASS_TYPE_P (from) && compatible_p)
27b8d0cd
MM
1052 {
1053 conv = build1 (IDENTITY_CONV, from, expr);
4f8163b1
MM
1054 conv = direct_reference_binding (rto, conv);
1055 CHECK_COPY_CONSTRUCTOR_P (TREE_OPERAND (conv, 0)) = 1;
1056 return conv;
faf5394a 1057 }
d11ad92e 1058
27b8d0cd
MM
1059 /* [dcl.init.ref]
1060
1061 Otherwise, a temporary of type "cv1 T1" is created and
1062 initialized from the initializer expression using the rules for a
1063 non-reference copy initialization. If T1 is reference-related to
1064 T2, cv1 must be the same cv-qualification as, or greater
1065 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1066 if (related_p && !at_least_as_qualified_p (to, from))
1067 return NULL_TREE;
1068
1069 conv = implicit_conversion (to, from, expr, flags);
1070 if (!conv)
1071 return NULL_TREE;
1072
1073 conv = build_conv (REF_BIND, rto, conv);
1074 /* This reference binding, unlike those above, requires the
1075 creation of a temporary. */
1076 NEED_TEMPORARY_P (conv) = 1;
1077
c73964b2
MS
1078 return conv;
1079}
1080
1081/* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1082 to type TO. The optional expression EXPR may affect the conversion.
1083 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1084 significant. */
1085
824b9a4c 1086static tree
94be8403 1087implicit_conversion (tree to, tree from, tree expr, int flags)
c73964b2
MS
1088{
1089 tree conv;
c73964b2 1090
5d73aa63
MM
1091 if (from == error_mark_node || to == error_mark_node
1092 || expr == error_mark_node)
1093 return NULL_TREE;
1094
c73964b2 1095 if (TREE_CODE (to) == REFERENCE_TYPE)
aa6e8ed3 1096 conv = reference_binding (to, from, expr, flags);
c73964b2 1097 else
de22184b 1098 conv = standard_conversion (to, from, expr);
c73964b2
MS
1099
1100 if (conv)
b80f8ef3
MM
1101 return conv;
1102
1103 if (expr != NULL_TREE
1104 && (IS_AGGR_TYPE (from)
1105 || IS_AGGR_TYPE (to))
1106 && (flags & LOOKUP_NO_CONVERSION) == 0)
c73964b2 1107 {
7993382e
MM
1108 struct z_candidate *cand;
1109
eb66be0e
MS
1110 cand = build_user_type_conversion_1
1111 (to, expr, LOOKUP_ONLYCONVERTING);
1112 if (cand)
1113 conv = cand->second_conv;
5e818b93
JM
1114
1115 /* We used to try to bind a reference to a temporary here, but that
1116 is now handled by the recursive call to this function at the end
1117 of reference_binding. */
b80f8ef3 1118 return conv;
c73964b2
MS
1119 }
1120
b80f8ef3 1121 return NULL_TREE;
c73964b2
MS
1122}
1123
5ffe581d
JM
1124/* Add a new entry to the list of candidates. Used by the add_*_candidate
1125 functions. */
1126
1127static struct z_candidate *
7993382e 1128add_candidate (struct z_candidate **candidates,
b80f8ef3
MM
1129 tree fn, tree args, tree convs, tree access_path,
1130 tree conversion_path, int viable)
5ffe581d 1131{
c68b0a84 1132 struct z_candidate *cand = ggc_alloc_cleared (sizeof (struct z_candidate));
5ffe581d
JM
1133
1134 cand->fn = fn;
b80f8ef3 1135 cand->args = args;
5ffe581d 1136 cand->convs = convs;
4ba126e4
MM
1137 cand->access_path = access_path;
1138 cand->conversion_path = conversion_path;
5ffe581d 1139 cand->viable = viable;
7993382e
MM
1140 cand->next = *candidates;
1141 *candidates = cand;
5ffe581d
JM
1142
1143 return cand;
1144}
1145
c73964b2
MS
1146/* Create an overload candidate for the function or method FN called with
1147 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
aa52c1ff
JM
1148 to implicit_conversion.
1149
1150 CTYPE, if non-NULL, is the type we want to pretend this function
1151 comes from for purposes of overload resolution. */
c73964b2
MS
1152
1153static struct z_candidate *
7993382e 1154add_function_candidate (struct z_candidate **candidates,
4ba126e4
MM
1155 tree fn, tree ctype, tree arglist,
1156 tree access_path, tree conversion_path,
1157 int flags)
c73964b2
MS
1158{
1159 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1160 int i, len;
1161 tree convs;
8f96c7ac 1162 tree parmnode, argnode;
b80f8ef3 1163 tree orig_arglist;
c73964b2 1164 int viable = 1;
c73964b2 1165
089d6ea7
MM
1166 /* Built-in functions that haven't been declared don't really
1167 exist. */
1168 if (DECL_ANTICIPATED (fn))
1169 return NULL;
1170
e0fff4b3
JM
1171 /* The `this', `in_chrg' and VTT arguments to constructors are not
1172 considered in overload resolution. */
c73964b2
MS
1173 if (DECL_CONSTRUCTOR_P (fn))
1174 {
e0fff4b3 1175 parmlist = skip_artificial_parms_for (fn, parmlist);
b80f8ef3 1176 orig_arglist = arglist;
e0fff4b3 1177 arglist = skip_artificial_parms_for (fn, arglist);
c73964b2 1178 }
b80f8ef3
MM
1179 else
1180 orig_arglist = arglist;
c73964b2 1181
8f96c7ac 1182 len = list_length (arglist);
f31c0a32 1183 convs = make_tree_vec (len);
8f96c7ac
JM
1184
1185 /* 13.3.2 - Viable functions [over.match.viable]
1186 First, to be a viable function, a candidate function shall have enough
1187 parameters to agree in number with the arguments in the list.
1188
1189 We need to check this first; otherwise, checking the ICSes might cause
1190 us to produce an ill-formed template instantiation. */
1191
1192 parmnode = parmlist;
1193 for (i = 0; i < len; ++i)
1194 {
1195 if (parmnode == NULL_TREE || parmnode == void_list_node)
1196 break;
1197 parmnode = TREE_CHAIN (parmnode);
1198 }
1199
1200 if (i < len && parmnode)
1201 viable = 0;
1202
1203 /* Make sure there are default args for the rest of the parms. */
a11d04b5
NS
1204 else if (!sufficient_parms_p (parmnode))
1205 viable = 0;
8f96c7ac
JM
1206
1207 if (! viable)
1208 goto out;
1209
1210 /* Second, for F to be a viable function, there shall exist for each
1211 argument an implicit conversion sequence that converts that argument
1212 to the corresponding parameter of F. */
1213
1214 parmnode = parmlist;
1215 argnode = arglist;
c73964b2
MS
1216
1217 for (i = 0; i < len; ++i)
1218 {
1219 tree arg = TREE_VALUE (argnode);
8cd4c175 1220 tree argtype = lvalue_type (arg);
c73964b2 1221 tree t;
aa52c1ff 1222 int is_this;
c73964b2 1223
c73964b2
MS
1224 if (parmnode == void_list_node)
1225 break;
aa45967f 1226
aa52c1ff
JM
1227 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1228 && ! DECL_CONSTRUCTOR_P (fn));
1229
aa45967f
JM
1230 if (parmnode)
1231 {
1232 tree parmtype = TREE_VALUE (parmnode);
1233
aa52c1ff
JM
1234 /* The type of the implicit object parameter ('this') for
1235 overload resolution is not always the same as for the
1236 function itself; conversion functions are considered to
1237 be members of the class being converted, and functions
1238 introduced by a using-declaration are considered to be
1239 members of the class that uses them.
aa45967f 1240
aa52c1ff
JM
1241 Since build_over_call ignores the ICS for the `this'
1242 parameter, we can just change the parm type. */
1243 if (ctype && is_this)
aa45967f
JM
1244 {
1245 parmtype
aa52c1ff 1246 = build_qualified_type (ctype,
aa45967f
JM
1247 TYPE_QUALS (TREE_TYPE (parmtype)));
1248 parmtype = build_pointer_type (parmtype);
1249 }
1250
1251 t = implicit_conversion (parmtype, argtype, arg, flags);
1252 }
c73964b2
MS
1253 else
1254 {
1255 t = build1 (IDENTITY_CONV, argtype, arg);
1256 ICS_ELLIPSIS_FLAG (t) = 1;
1257 }
1258
aa52c1ff 1259 if (t && is_this)
d11ad92e
MS
1260 ICS_THIS_FLAG (t) = 1;
1261
c73964b2
MS
1262 TREE_VEC_ELT (convs, i) = t;
1263 if (! t)
8f96c7ac
JM
1264 {
1265 viable = 0;
1266 break;
1267 }
c73964b2 1268
d11ad92e
MS
1269 if (ICS_BAD_FLAG (t))
1270 viable = -1;
1271
c73964b2
MS
1272 if (parmnode)
1273 parmnode = TREE_CHAIN (parmnode);
1274 argnode = TREE_CHAIN (argnode);
1275 }
1276
8f96c7ac 1277 out:
b80f8ef3 1278 return add_candidate (candidates, fn, orig_arglist, convs, access_path,
4ba126e4 1279 conversion_path, viable);
c73964b2
MS
1280}
1281
1282/* Create an overload candidate for the conversion function FN which will
1283 be invoked for expression OBJ, producing a pointer-to-function which
1284 will in turn be called with the argument list ARGLIST, and add it to
37b6eb34
JM
1285 CANDIDATES. FLAGS is passed on to implicit_conversion.
1286
1287 Actually, we don't really care about FN; we care about the type it
1288 converts to. There may be multiple conversion functions that will
1289 convert to that type, and we rely on build_user_type_conversion_1 to
1290 choose the best one; so when we create our candidate, we record the type
1291 instead of the function. */
c73964b2
MS
1292
1293static struct z_candidate *
7993382e 1294add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
94be8403 1295 tree arglist, tree access_path, tree conversion_path)
c73964b2
MS
1296{
1297 tree totype = TREE_TYPE (TREE_TYPE (fn));
477f6664
JM
1298 int i, len, viable, flags;
1299 tree parmlist, convs, parmnode, argnode;
1300
1301 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1302 parmlist = TREE_TYPE (parmlist);
1303 parmlist = TYPE_ARG_TYPES (parmlist);
1304
1305 len = list_length (arglist) + 1;
1306 convs = make_tree_vec (len);
1307 parmnode = parmlist;
1308 argnode = arglist;
1309 viable = 1;
1310 flags = LOOKUP_NORMAL;
c73964b2 1311
37b6eb34 1312 /* Don't bother looking up the same type twice. */
7993382e
MM
1313 if (*candidates && (*candidates)->fn == totype)
1314 return NULL;
37b6eb34 1315
c73964b2
MS
1316 for (i = 0; i < len; ++i)
1317 {
1318 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
d11ad92e 1319 tree argtype = lvalue_type (arg);
c73964b2
MS
1320 tree t;
1321
c73964b2
MS
1322 if (i == 0)
1323 t = implicit_conversion (totype, argtype, arg, flags);
1324 else if (parmnode == void_list_node)
1325 break;
1326 else if (parmnode)
1327 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1328 else
1329 {
1330 t = build1 (IDENTITY_CONV, argtype, arg);
1331 ICS_ELLIPSIS_FLAG (t) = 1;
1332 }
1333
1334 TREE_VEC_ELT (convs, i) = t;
1335 if (! t)
1336 break;
1337
d11ad92e
MS
1338 if (ICS_BAD_FLAG (t))
1339 viable = -1;
1340
c73964b2
MS
1341 if (i == 0)
1342 continue;
1343
1344 if (parmnode)
1345 parmnode = TREE_CHAIN (parmnode);
1346 argnode = TREE_CHAIN (argnode);
1347 }
1348
1349 if (i < len)
1350 viable = 0;
1351
a11d04b5
NS
1352 if (!sufficient_parms_p (parmnode))
1353 viable = 0;
c73964b2 1354
b80f8ef3 1355 return add_candidate (candidates, totype, arglist, convs, access_path,
4ba126e4 1356 conversion_path, viable);
c73964b2
MS
1357}
1358
7993382e
MM
1359static void
1360build_builtin_candidate (struct z_candidate **candidates, tree fnname,
94be8403
GDR
1361 tree type1, tree type2, tree *args, tree *argtypes,
1362 int flags)
c73964b2
MS
1363{
1364 tree t, convs;
1365 int viable = 1, i;
c73964b2
MS
1366 tree types[2];
1367
1368 types[0] = type1;
1369 types[1] = type2;
1370
f31c0a32 1371 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
c73964b2
MS
1372
1373 for (i = 0; i < 2; ++i)
1374 {
1375 if (! args[i])
1376 break;
1377
1378 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1379 if (! t)
1380 {
1381 viable = 0;
1382 /* We need something for printing the candidate. */
1383 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1384 }
d11ad92e
MS
1385 else if (ICS_BAD_FLAG (t))
1386 viable = 0;
c73964b2
MS
1387 TREE_VEC_ELT (convs, i) = t;
1388 }
1389
1390 /* For COND_EXPR we rearranged the arguments; undo that now. */
1391 if (args[2])
1392 {
1393 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1394 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1395 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1396 if (t)
1397 TREE_VEC_ELT (convs, 0) = t;
1398 else
1399 viable = 0;
1400 }
1401
7993382e
MM
1402 add_candidate (candidates, fnname, /*args=*/NULL_TREE, convs,
1403 /*access_path=*/NULL_TREE,
1404 /*conversion_path=*/NULL_TREE,
1405 viable);
c73964b2
MS
1406}
1407
94be8403
GDR
1408static bool
1409is_complete (tree t)
c73964b2 1410{
d0f062fb 1411 return COMPLETE_TYPE_P (complete_type (t));
c73964b2
MS
1412}
1413
838dfd8a 1414/* Returns nonzero if TYPE is a promoted arithmetic type. */
a7a64a77 1415
94be8403
GDR
1416static bool
1417promoted_arithmetic_type_p (tree type)
a7a64a77
MM
1418{
1419 /* [over.built]
1420
1421 In this section, the term promoted integral type is used to refer
1422 to those integral types which are preserved by integral promotion
1423 (including e.g. int and long but excluding e.g. char).
1424 Similarly, the term promoted arithmetic type refers to promoted
1425 integral types plus floating types. */
1426 return ((INTEGRAL_TYPE_P (type)
1427 && same_type_p (type_promotes_to (type), type))
1428 || TREE_CODE (type) == REAL_TYPE);
1429}
1430
c73964b2
MS
1431/* Create any builtin operator overload candidates for the operator in
1432 question given the converted operand types TYPE1 and TYPE2. The other
1433 args are passed through from add_builtin_candidates to
4cff6abe
NS
1434 build_builtin_candidate.
1435
1436 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1437 If CODE is requires candidates operands of the same type of the kind
1438 of which TYPE1 and TYPE2 are, we add both candidates
1439 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
c73964b2 1440
7993382e
MM
1441static void
1442add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
94be8403
GDR
1443 enum tree_code code2, tree fnname, tree type1,
1444 tree type2, tree *args, tree *argtypes, int flags)
c73964b2
MS
1445{
1446 switch (code)
1447 {
1448 case POSTINCREMENT_EXPR:
1449 case POSTDECREMENT_EXPR:
1450 args[1] = integer_zero_node;
1451 type2 = integer_type_node;
7f85441b
KG
1452 break;
1453 default:
1454 break;
c73964b2
MS
1455 }
1456
1457 switch (code)
1458 {
1459
1460/* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1461 and VQ is either volatile or empty, there exist candidate operator
1462 functions of the form
1463 VQ T& operator++(VQ T&);
1464 T operator++(VQ T&, int);
1465 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1466 type other than bool, and VQ is either volatile or empty, there exist
1467 candidate operator functions of the form
1468 VQ T& operator--(VQ T&);
1469 T operator--(VQ T&, int);
1470 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1471 complete object type, and VQ is either volatile or empty, there exist
1472 candidate operator functions of the form
1473 T*VQ& operator++(T*VQ&);
1474 T*VQ& operator--(T*VQ&);
1475 T* operator++(T*VQ&, int);
1476 T* operator--(T*VQ&, int); */
1477
1478 case POSTDECREMENT_EXPR:
1479 case PREDECREMENT_EXPR:
1480 if (TREE_CODE (type1) == BOOLEAN_TYPE)
7993382e 1481 return;
c73964b2
MS
1482 case POSTINCREMENT_EXPR:
1483 case PREINCREMENT_EXPR:
4cff6abe 1484 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
c73964b2
MS
1485 {
1486 type1 = build_reference_type (type1);
1487 break;
1488 }
7993382e 1489 return;
c73964b2
MS
1490
1491/* 7 For every cv-qualified or cv-unqualified complete object type T, there
1492 exist candidate operator functions of the form
1493
1494 T& operator*(T*);
1495
1496 8 For every function type T, there exist candidate operator functions of
1497 the form
1498 T& operator*(T*); */
1499
1500 case INDIRECT_REF:
1501 if (TREE_CODE (type1) == POINTER_TYPE
c11b6f21 1502 && (TYPE_PTROB_P (type1)
c73964b2
MS
1503 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1504 break;
7993382e 1505 return;
c73964b2
MS
1506
1507/* 9 For every type T, there exist candidate operator functions of the form
1508 T* operator+(T*);
1509
1510 10For every promoted arithmetic type T, there exist candidate operator
1511 functions of the form
1512 T operator+(T);
1513 T operator-(T); */
1514
1515 case CONVERT_EXPR: /* unary + */
a5ac359a 1516 if (TREE_CODE (type1) == POINTER_TYPE)
c73964b2
MS
1517 break;
1518 case NEGATE_EXPR:
1519 if (ARITHMETIC_TYPE_P (type1))
1520 break;
7993382e 1521 return;
c73964b2
MS
1522
1523/* 11For every promoted integral type T, there exist candidate operator
1524 functions of the form
1525 T operator~(T); */
1526
1527 case BIT_NOT_EXPR:
1528 if (INTEGRAL_TYPE_P (type1))
1529 break;
7993382e 1530 return;
c73964b2
MS
1531
1532/* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1533 is the same type as C2 or is a derived class of C2, T is a complete
1534 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1535 there exist candidate operator functions of the form
1536 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1537 where CV12 is the union of CV1 and CV2. */
1538
1539 case MEMBER_REF:
1540 if (TREE_CODE (type1) == POINTER_TYPE
a5ac359a 1541 && TYPE_PTR_TO_MEMBER_P (type2))
c73964b2
MS
1542 {
1543 tree c1 = TREE_TYPE (type1);
a5ac359a 1544 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
c73964b2
MS
1545
1546 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1547 && (TYPE_PTRMEMFUNC_P (type2)
1548 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1549 break;
1550 }
7993382e 1551 return;
c73964b2
MS
1552
1553/* 13For every pair of promoted arithmetic types L and R, there exist can-
1554 didate operator functions of the form
1555 LR operator*(L, R);
1556 LR operator/(L, R);
1557 LR operator+(L, R);
1558 LR operator-(L, R);
1559 bool operator<(L, R);
1560 bool operator>(L, R);
1561 bool operator<=(L, R);
1562 bool operator>=(L, R);
1563 bool operator==(L, R);
1564 bool operator!=(L, R);
1565 where LR is the result of the usual arithmetic conversions between
1566 types L and R.
1567
1568 14For every pair of types T and I, where T is a cv-qualified or cv-
1569 unqualified complete object type and I is a promoted integral type,
1570 there exist candidate operator functions of the form
1571 T* operator+(T*, I);
1572 T& operator[](T*, I);
1573 T* operator-(T*, I);
1574 T* operator+(I, T*);
1575 T& operator[](I, T*);
1576
1577 15For every T, where T is a pointer to complete object type, there exist
1578 candidate operator functions of the form112)
1579 ptrdiff_t operator-(T, T);
1580
e5596aef 1581 16For every pointer or enumeration type T, there exist candidate operator
4cff6abe 1582 functions of the form
c73964b2
MS
1583 bool operator<(T, T);
1584 bool operator>(T, T);
1585 bool operator<=(T, T);
1586 bool operator>=(T, T);
1587 bool operator==(T, T);
1588 bool operator!=(T, T);
1589
1590 17For every pointer to member type T, there exist candidate operator
1591 functions of the form
1592 bool operator==(T, T);
1593 bool operator!=(T, T); */
1594
1595 case MINUS_EXPR:
c11b6f21 1596 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
c73964b2 1597 break;
c11b6f21 1598 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
c73964b2
MS
1599 {
1600 type2 = ptrdiff_type_node;
1601 break;
1602 }
1603 case MULT_EXPR:
1604 case TRUNC_DIV_EXPR:
1605 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1606 break;
7993382e 1607 return;
c73964b2
MS
1608
1609 case EQ_EXPR:
1610 case NE_EXPR:
a703fb38
KG
1611 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1612 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
c73964b2 1613 break;
a5ac359a 1614 if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
c73964b2
MS
1615 {
1616 type2 = type1;
1617 break;
1618 }
a5ac359a 1619 if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
c73964b2
MS
1620 {
1621 type1 = type2;
1622 break;
1623 }
f4f206f4 1624 /* Fall through. */
c73964b2
MS
1625 case LT_EXPR:
1626 case GT_EXPR:
1627 case LE_EXPR:
1628 case GE_EXPR:
1629 case MAX_EXPR:
1630 case MIN_EXPR:
4cff6abe
NS
1631 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1632 break;
1633 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
c73964b2 1634 break;
4cff6abe
NS
1635 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1636 break;
c73964b2
MS
1637 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1638 {
1639 type2 = type1;
1640 break;
1641 }
1642 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1643 {
1644 type1 = type2;
1645 break;
1646 }
7993382e 1647 return;
c73964b2
MS
1648
1649 case PLUS_EXPR:
1650 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1651 break;
1652 case ARRAY_REF:
c11b6f21 1653 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
c73964b2
MS
1654 {
1655 type1 = ptrdiff_type_node;
1656 break;
1657 }
c11b6f21 1658 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
c73964b2
MS
1659 {
1660 type2 = ptrdiff_type_node;
1661 break;
1662 }
7993382e 1663 return;
c73964b2
MS
1664
1665/* 18For every pair of promoted integral types L and R, there exist candi-
1666 date operator functions of the form
1667 LR operator%(L, R);
1668 LR operator&(L, R);
1669 LR operator^(L, R);
1670 LR operator|(L, R);
1671 L operator<<(L, R);
1672 L operator>>(L, R);
1673 where LR is the result of the usual arithmetic conversions between
1674 types L and R. */
1675
1676 case TRUNC_MOD_EXPR:
1677 case BIT_AND_EXPR:
1678 case BIT_IOR_EXPR:
1679 case BIT_XOR_EXPR:
1680 case LSHIFT_EXPR:
1681 case RSHIFT_EXPR:
1682 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1683 break;
7993382e 1684 return;
c73964b2
MS
1685
1686/* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1687 type, VQ is either volatile or empty, and R is a promoted arithmetic
1688 type, there exist candidate operator functions of the form
1689 VQ L& operator=(VQ L&, R);
1690 VQ L& operator*=(VQ L&, R);
1691 VQ L& operator/=(VQ L&, R);
1692 VQ L& operator+=(VQ L&, R);
1693 VQ L& operator-=(VQ L&, R);
1694
1695 20For every pair T, VQ), where T is any type and VQ is either volatile
1696 or empty, there exist candidate operator functions of the form
1697 T*VQ& operator=(T*VQ&, T*);
1698
1699 21For every pair T, VQ), where T is a pointer to member type and VQ is
1700 either volatile or empty, there exist candidate operator functions of
1701 the form
1702 VQ T& operator=(VQ T&, T);
1703
1704 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1705 unqualified complete object type, VQ is either volatile or empty, and
1706 I is a promoted integral type, there exist candidate operator func-
1707 tions of the form
1708 T*VQ& operator+=(T*VQ&, I);
1709 T*VQ& operator-=(T*VQ&, I);
1710
1711 23For every triple L, VQ, R), where L is an integral or enumeration
1712 type, VQ is either volatile or empty, and R is a promoted integral
1713 type, there exist candidate operator functions of the form
1714
1715 VQ L& operator%=(VQ L&, R);
1716 VQ L& operator<<=(VQ L&, R);
1717 VQ L& operator>>=(VQ L&, R);
1718 VQ L& operator&=(VQ L&, R);
1719 VQ L& operator^=(VQ L&, R);
1720 VQ L& operator|=(VQ L&, R); */
1721
1722 case MODIFY_EXPR:
1723 switch (code2)
1724 {
1725 case PLUS_EXPR:
1726 case MINUS_EXPR:
c11b6f21 1727 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
c73964b2
MS
1728 {
1729 type2 = ptrdiff_type_node;
1730 break;
1731 }
1732 case MULT_EXPR:
1733 case TRUNC_DIV_EXPR:
1734 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1735 break;
7993382e 1736 return;
c73964b2
MS
1737
1738 case TRUNC_MOD_EXPR:
1739 case BIT_AND_EXPR:
1740 case BIT_IOR_EXPR:
1741 case BIT_XOR_EXPR:
1742 case LSHIFT_EXPR:
1743 case RSHIFT_EXPR:
1744 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1745 break;
7993382e 1746 return;
c73964b2
MS
1747
1748 case NOP_EXPR:
1749 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1750 break;
1751 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1752 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1753 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1754 || ((TYPE_PTRMEMFUNC_P (type1)
1755 || TREE_CODE (type1) == POINTER_TYPE)
1756 && null_ptr_cst_p (args[1])))
1757 {
1758 type2 = type1;
1759 break;
1760 }
7993382e 1761 return;
c73964b2
MS
1762
1763 default:
a98facb0 1764 abort ();
c73964b2
MS
1765 }
1766 type1 = build_reference_type (type1);
1767 break;
1768
1769 case COND_EXPR:
5b0c5896 1770 /* [over.built]
a7a64a77
MM
1771
1772 For every pair of promoted arithmetic types L and R, there
1773 exist candidate operator functions of the form
de22184b 1774
a7a64a77
MM
1775 LR operator?(bool, L, R);
1776
1777 where LR is the result of the usual arithmetic conversions
1778 between types L and R.
1779
1780 For every type T, where T is a pointer or pointer-to-member
1781 type, there exist candidate operator functions of the form T
1782 operator?(bool, T, T); */
1783
1784 if (promoted_arithmetic_type_p (type1)
1785 && promoted_arithmetic_type_p (type2))
1786 /* That's OK. */
c73964b2 1787 break;
a7a64a77
MM
1788
1789 /* Otherwise, the types should be pointers. */
a5ac359a
MM
1790 if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
1791 || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
7993382e 1792 return;
a7a64a77
MM
1793
1794 /* We don't check that the two types are the same; the logic
1795 below will actually create two candidates; one in which both
1796 parameter types are TYPE1, and one in which both parameter
1797 types are TYPE2. */
7993382e 1798 break;
c73964b2
MS
1799
1800 default:
a98facb0 1801 abort ();
c73964b2
MS
1802 }
1803
4cff6abe
NS
1804 /* If we're dealing with two pointer types or two enumeral types,
1805 we need candidates for both of them. */
a7a64a77 1806 if (type2 && !same_type_p (type1, type2)
c73964b2
MS
1807 && TREE_CODE (type1) == TREE_CODE (type2)
1808 && (TREE_CODE (type1) == REFERENCE_TYPE
a5ac359a
MM
1809 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1810 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
c73964b2 1811 || TYPE_PTRMEMFUNC_P (type1)
4cff6abe
NS
1812 || IS_AGGR_TYPE (type1)
1813 || TREE_CODE (type1) == ENUMERAL_TYPE))
c73964b2 1814 {
7993382e 1815 build_builtin_candidate
c73964b2 1816 (candidates, fnname, type1, type1, args, argtypes, flags);
7993382e 1817 build_builtin_candidate
c73964b2 1818 (candidates, fnname, type2, type2, args, argtypes, flags);
7993382e 1819 return;
c73964b2
MS
1820 }
1821
7993382e 1822 build_builtin_candidate
c73964b2
MS
1823 (candidates, fnname, type1, type2, args, argtypes, flags);
1824}
1825
1826tree
94be8403 1827type_decays_to (tree type)
c73964b2
MS
1828{
1829 if (TREE_CODE (type) == ARRAY_TYPE)
1830 return build_pointer_type (TREE_TYPE (type));
1831 if (TREE_CODE (type) == FUNCTION_TYPE)
1832 return build_pointer_type (type);
1833 return type;
1834}
1835
1836/* There are three conditions of builtin candidates:
1837
1838 1) bool-taking candidates. These are the same regardless of the input.
1839 2) pointer-pair taking candidates. These are generated for each type
1840 one of the input types converts to.
cab1f180 1841 3) arithmetic candidates. According to the standard, we should generate
4cff6abe
NS
1842 all of these, but I'm trying not to...
1843
1844 Here we generate a superset of the possible candidates for this particular
1845 case. That is a subset of the full set the standard defines, plus some
1846 other cases which the standard disallows. add_builtin_candidate will
0e339752 1847 filter out the invalid set. */
c73964b2 1848
7993382e
MM
1849static void
1850add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
94be8403
GDR
1851 enum tree_code code2, tree fnname, tree *args,
1852 int flags)
c73964b2
MS
1853{
1854 int ref1, i;
4cff6abe 1855 int enum_p = 0;
a7a64a77
MM
1856 tree type, argtypes[3];
1857 /* TYPES[i] is the set of possible builtin-operator parameter types
1858 we will consider for the Ith argument. These are represented as
1859 a TREE_LIST; the TREE_VALUE of each node is the potential
1860 parameter type. */
1861 tree types[2];
c73964b2
MS
1862
1863 for (i = 0; i < 3; ++i)
1864 {
1865 if (args[i])
d11ad92e 1866 argtypes[i] = lvalue_type (args[i]);
c73964b2
MS
1867 else
1868 argtypes[i] = NULL_TREE;
1869 }
1870
1871 switch (code)
1872 {
1873/* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1874 and VQ is either volatile or empty, there exist candidate operator
1875 functions of the form
1876 VQ T& operator++(VQ T&); */
1877
1878 case POSTINCREMENT_EXPR:
1879 case PREINCREMENT_EXPR:
1880 case POSTDECREMENT_EXPR:
1881 case PREDECREMENT_EXPR:
1882 case MODIFY_EXPR:
1883 ref1 = 1;
1884 break;
1885
1886/* 24There also exist candidate operator functions of the form
1887 bool operator!(bool);
1888 bool operator&&(bool, bool);
1889 bool operator||(bool, bool); */
1890
1891 case TRUTH_NOT_EXPR:
7993382e 1892 build_builtin_candidate
c73964b2
MS
1893 (candidates, fnname, boolean_type_node,
1894 NULL_TREE, args, argtypes, flags);
7993382e 1895 return;
c73964b2
MS
1896
1897 case TRUTH_ORIF_EXPR:
1898 case TRUTH_ANDIF_EXPR:
7993382e 1899 build_builtin_candidate
c73964b2
MS
1900 (candidates, fnname, boolean_type_node,
1901 boolean_type_node, args, argtypes, flags);
7993382e 1902 return;
c73964b2
MS
1903
1904 case ADDR_EXPR:
1905 case COMPOUND_EXPR:
1906 case COMPONENT_REF:
7993382e 1907 return;
c73964b2 1908
4cff6abe
NS
1909 case COND_EXPR:
1910 case EQ_EXPR:
1911 case NE_EXPR:
1912 case LT_EXPR:
1913 case LE_EXPR:
1914 case GT_EXPR:
1915 case GE_EXPR:
1916 enum_p = 1;
f4f206f4 1917 /* Fall through. */
4cff6abe 1918
c73964b2
MS
1919 default:
1920 ref1 = 0;
1921 }
1922
1923 types[0] = types[1] = NULL_TREE;
1924
1925 for (i = 0; i < 2; ++i)
1926 {
1927 if (! args[i])
1928 ;
1929 else if (IS_AGGR_TYPE (argtypes[i]))
1930 {
47898a19 1931 tree convs;
c73964b2 1932
02020185 1933 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
7993382e 1934 return;
02020185
JM
1935
1936 convs = lookup_conversions (argtypes[i]);
1937
c73964b2
MS
1938 if (code == COND_EXPR)
1939 {
1940 if (real_lvalue_p (args[i]))
e1b3e07d 1941 types[i] = tree_cons
c73964b2
MS
1942 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1943
e1b3e07d 1944 types[i] = tree_cons
c73964b2
MS
1945 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1946 }
02020185
JM
1947
1948 else if (! convs)
7993382e 1949 return;
c73964b2
MS
1950
1951 for (; convs; convs = TREE_CHAIN (convs))
1952 {
47898a19 1953 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
c73964b2
MS
1954
1955 if (i == 0 && ref1
1956 && (TREE_CODE (type) != REFERENCE_TYPE
91063b51 1957 || CP_TYPE_CONST_P (TREE_TYPE (type))))
c73964b2
MS
1958 continue;
1959
1960 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
e1b3e07d 1961 types[i] = tree_cons (NULL_TREE, type, types[i]);
c73964b2
MS
1962
1963 type = non_reference (type);
1964 if (i != 0 || ! ref1)
1965 {
1966 type = TYPE_MAIN_VARIANT (type_decays_to (type));
4cff6abe
NS
1967 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
1968 types[i] = tree_cons (NULL_TREE, type, types[i]);
c73964b2
MS
1969 if (INTEGRAL_TYPE_P (type))
1970 type = type_promotes_to (type);
1971 }
1972
1973 if (! value_member (type, types[i]))
e1b3e07d 1974 types[i] = tree_cons (NULL_TREE, type, types[i]);
c73964b2
MS
1975 }
1976 }
1977 else
1978 {
1979 if (code == COND_EXPR && real_lvalue_p (args[i]))
e1b3e07d 1980 types[i] = tree_cons
c73964b2
MS
1981 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1982 type = non_reference (argtypes[i]);
1983 if (i != 0 || ! ref1)
1984 {
1985 type = TYPE_MAIN_VARIANT (type_decays_to (type));
4cff6abe
NS
1986 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
1987 types[i] = tree_cons (NULL_TREE, type, types[i]);
c73964b2
MS
1988 if (INTEGRAL_TYPE_P (type))
1989 type = type_promotes_to (type);
1990 }
e1b3e07d 1991 types[i] = tree_cons (NULL_TREE, type, types[i]);
c73964b2
MS
1992 }
1993 }
1994
a7a64a77
MM
1995 /* Run through the possible parameter types of both arguments,
1996 creating candidates with those parameter types. */
c73964b2
MS
1997 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1998 {
1999 if (types[1])
2000 for (type = types[1]; type; type = TREE_CHAIN (type))
7993382e 2001 add_builtin_candidate
c73964b2
MS
2002 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2003 TREE_VALUE (type), args, argtypes, flags);
2004 else
7993382e 2005 add_builtin_candidate
c73964b2
MS
2006 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2007 NULL_TREE, args, argtypes, flags);
2008 }
2009
7993382e 2010 return;
c73964b2
MS
2011}
2012
e1467ff2 2013
386b8a85
JM
2014/* If TMPL can be successfully instantiated as indicated by
2015 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2016
e1467ff2
MM
2017 TMPL is the template. EXPLICIT_TARGS are any explicit template
2018 arguments. ARGLIST is the arguments provided at the call-site.
2019 The RETURN_TYPE is the desired type for conversion operators. If
aa52c1ff
JM
2020 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2021 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
e1467ff2
MM
2022 add_conv_candidate. */
2023
2024static struct z_candidate*
7993382e 2025add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
94be8403
GDR
2026 tree ctype, tree explicit_targs, tree arglist,
2027 tree return_type, tree access_path,
2028 tree conversion_path, int flags, tree obj,
2029 unification_kind_t strict)
c73964b2 2030{
98c1c668 2031 int ntparms = DECL_NTPARMS (tmpl);
f31c0a32 2032 tree targs = make_tree_vec (ntparms);
e5214479 2033 tree args_without_in_chrg = arglist;
c73964b2 2034 struct z_candidate *cand;
98c1c668 2035 int i;
c73964b2
MS
2036 tree fn;
2037
e5214479
JM
2038 /* We don't do deduction on the in-charge parameter, the VTT
2039 parameter or 'this'. */
2040 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2041 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2042
71a19881
MM
2043 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2044 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2045 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
e5214479 2046 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
71a19881
MM
2047
2048 i = fn_type_unification (tmpl, explicit_targs, targs,
2049 args_without_in_chrg,
e5214479 2050 return_type, strict, -1);
98c1c668 2051
c73964b2 2052 if (i != 0)
7993382e 2053 return NULL;
c73964b2 2054
3e4a3562 2055 fn = instantiate_template (tmpl, targs, tf_none);
c73964b2 2056 if (fn == error_mark_node)
7993382e 2057 return NULL;
c73964b2 2058
9928a3d5
MM
2059 /* In [class.copy]:
2060
2061 A member function template is never instantiated to perform the
2062 copy of a class object to an object of its class type.
2063
2064 It's a little unclear what this means; the standard explicitly
2065 does allow a template to be used to copy a class. For example,
2066 in:
2067
2068 struct A {
2069 A(A&);
2070 template <class T> A(const T&);
2071 };
2072 const A f ();
2073 void g () { A a (f ()); }
2074
2075 the member template will be used to make the copy. The section
2076 quoted above appears in the paragraph that forbids constructors
2077 whose only parameter is (a possibly cv-qualified variant of) the
2078 class type, and a logical interpretation is that the intent was
2079 to forbid the instantiation of member templates which would then
2080 have that form. */
2081 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2082 {
2083 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2084 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2085 ctype))
7993382e 2086 return NULL;
9928a3d5
MM
2087 }
2088
e1467ff2
MM
2089 if (obj != NULL_TREE)
2090 /* Aha, this is a conversion function. */
4ba126e4
MM
2091 cand = add_conv_candidate (candidates, fn, obj, access_path,
2092 conversion_path, arglist);
e1467ff2 2093 else
aa52c1ff 2094 cand = add_function_candidate (candidates, fn, ctype,
4ba126e4
MM
2095 arglist, access_path,
2096 conversion_path, flags);
e1467ff2
MM
2097 if (DECL_TI_TEMPLATE (fn) != tmpl)
2098 /* This situation can occur if a member template of a template
2099 class is specialized. Then, instantiate_template might return
2100 an instantiation of the specialization, in which case the
2101 DECL_TI_TEMPLATE field will point at the original
2102 specialization. For example:
2103
2104 template <class T> struct S { template <class U> void f(U);
2105 template <> void f(int) {}; };
2106 S<double> sd;
2107 sd.f(3);
2108
2109 Here, TMPL will be template <class U> S<double>::f(U).
2110 And, instantiate template will give us the specialization
2111 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2112 for this will point at template <class T> template <> S<T>::f(int),
2113 so that we can find the definition. For the purposes of
2114 overload resolution, however, we want the original TMPL. */
2115 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2116 else
2117 cand->template = DECL_TEMPLATE_INFO (fn);
2118
c73964b2
MS
2119 return cand;
2120}
2121
786b5245
MM
2122
2123static struct z_candidate *
7993382e 2124add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
94be8403
GDR
2125 tree explicit_targs, tree arglist, tree return_type,
2126 tree access_path, tree conversion_path, int flags,
2127 unification_kind_t strict)
786b5245 2128{
e1467ff2 2129 return
aa52c1ff 2130 add_template_candidate_real (candidates, tmpl, ctype,
4ba126e4
MM
2131 explicit_targs, arglist, return_type,
2132 access_path, conversion_path,
2133 flags, NULL_TREE, strict);
e1467ff2 2134}
786b5245 2135
786b5245 2136
e1467ff2 2137static struct z_candidate *
7993382e 2138add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
94be8403
GDR
2139 tree obj, tree arglist, tree return_type,
2140 tree access_path, tree conversion_path)
e1467ff2
MM
2141{
2142 return
aa52c1ff 2143 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
4ba126e4
MM
2144 arglist, return_type, access_path,
2145 conversion_path, 0, obj, DEDUCE_CONV);
786b5245
MM
2146}
2147
436f8a4c
MM
2148/* The CANDS are the set of candidates that were considered for
2149 overload resolution. Return the set of viable candidates. If none
2150 of the candidates were viable, set *ANY_VIABLE_P to true. STRICT_P
2151 is true if a candidate should be considered viable only if it is
2152 strictly viable. */
786b5245 2153
436f8a4c
MM
2154static struct z_candidate*
2155splice_viable (struct z_candidate *cands,
2156 bool strict_p,
2157 bool *any_viable_p)
c73964b2 2158{
436f8a4c
MM
2159 struct z_candidate *viable;
2160 struct z_candidate **last_viable;
2161 struct z_candidate **cand;
2162
2163 viable = NULL;
2164 last_viable = &viable;
2165 *any_viable_p = false;
2166
2167 cand = &cands;
2168 while (*cand)
2169 {
2170 struct z_candidate *c = *cand;
2171 if (strict_p ? c->viable == 1 : c->viable)
2172 {
2173 *last_viable = c;
2174 *cand = c->next;
2175 c->next = NULL;
2176 last_viable = &c->next;
2177 *any_viable_p = true;
2178 }
2179 else
2180 cand = &c->next;
2181 }
2182
2183 return viable ? viable : cands;
c73964b2
MS
2184}
2185
94be8403
GDR
2186static bool
2187any_strictly_viable (struct z_candidate *cands)
ecc42c14
AO
2188{
2189 for (; cands; cands = cands->next)
2190 if (cands->viable == 1)
94be8403
GDR
2191 return true;
2192 return false;
ecc42c14
AO
2193}
2194
824b9a4c 2195static tree
94be8403 2196build_this (tree obj)
c73964b2
MS
2197{
2198 /* Fix this to work on non-lvalues. */
6eabb241 2199 return build_unary_op (ADDR_EXPR, obj, 0);
c73964b2
MS
2200}
2201
436f8a4c
MM
2202/* Returns true iff functions are equivalent. Equivalent functions are
2203 not '==' only if one is a function-local extern function or if
2204 both are extern "C". */
2205
2206static inline int
2207equal_functions (tree fn1, tree fn2)
2208{
2209 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2210 || DECL_EXTERN_C_FUNCTION_P (fn1))
2211 return decls_match (fn1, fn2);
2212 return fn1 == fn2;
2213}
2214
d2a6f3c0
ZW
2215/* Print information about one overload candidate CANDIDATE. MSGSTR
2216 is the text to print before the candidate itself.
2217
2218 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2219 to have been run through gettext by the caller. This wart makes
2220 life simpler in print_z_candidates and for the translators. */
b9747e59
JM
2221
2222static void
d2a6f3c0 2223print_z_candidate (const char *msgstr, struct z_candidate *candidate)
b9747e59
JM
2224{
2225 if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2226 {
2227 if (TREE_VEC_LENGTH (candidate->convs) == 3)
d2a6f3c0
ZW
2228 inform ("%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
2229 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2230 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)),
2231 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 2)));
b9747e59 2232 else if (TREE_VEC_LENGTH (candidate->convs) == 2)
d2a6f3c0
ZW
2233 inform ("%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
2234 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2235 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)));
b9747e59 2236 else
d2a6f3c0
ZW
2237 inform ("%s %D(%T) <built-in>", msgstr, candidate->fn,
2238 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)));
b9747e59
JM
2239 }
2240 else if (TYPE_P (candidate->fn))
d2a6f3c0
ZW
2241 inform ("%s %T <conversion>", msgstr, candidate->fn);
2242 else if (candidate->viable == -1)
ddd2d57e 2243 inform ("%J%s %+#D <near match>", candidate->fn, msgstr, candidate->fn);
b9747e59 2244 else
ddd2d57e 2245 inform ("%J%s %+#D", candidate->fn, msgstr, candidate->fn);
b9747e59
JM
2246}
2247
c73964b2 2248static void
94be8403 2249print_z_candidates (struct z_candidate *candidates)
c73964b2 2250{
436f8a4c
MM
2251 const char *str;
2252 struct z_candidate *cand1;
2253 struct z_candidate **cand2;
2254
2255 /* There may be duplicates in the set of candidates. We put off
2256 checking this condition as long as possible, since we have no way
2257 to eliminate duplicates from a set of functions in less than n^2
2258 time. Now we are about to emit an error message, so it is more
2259 permissible to go slowly. */
2260 for (cand1 = candidates; cand1; cand1 = cand1->next)
2261 {
2262 tree fn = cand1->fn;
2263 /* Skip builtin candidates and conversion functions. */
2264 if (TREE_CODE (fn) != FUNCTION_DECL)
2265 continue;
2266 cand2 = &cand1->next;
2267 while (*cand2)
2268 {
2269 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2270 && equal_functions (fn, (*cand2)->fn))
2271 *cand2 = (*cand2)->next;
2272 else
2273 cand2 = &(*cand2)->next;
2274 }
2275 }
2276
d2a6f3c0
ZW
2277 if (!candidates)
2278 return;
2279
2280 str = _("candidates are:");
2281 print_z_candidate (str, candidates);
2282 if (candidates->next)
c73964b2 2283 {
2bd02043
ZW
2284 /* Indent successive candidates by the width of the translation
2285 of the above string. */
2286 size_t len = gcc_gettext_width (str) + 1;
d2a6f3c0
ZW
2287 char *spaces = alloca (len);
2288 memset (spaces, ' ', len-1);
9804b5b8 2289 spaces[len - 1] = '\0';
d2a6f3c0
ZW
2290
2291 candidates = candidates->next;
2292 do
2293 {
2294 print_z_candidate (spaces, candidates);
2295 candidates = candidates->next;
2296 }
2297 while (candidates);
c73964b2
MS
2298 }
2299}
2300
3d938426
MM
2301/* USER_SEQ is a user-defined conversion sequence, beginning with a
2302 USER_CONV. STD_SEQ is the standard conversion sequence applied to
2303 the result of the conversion function to convert it to the final
2304 desired type. Merge the the two sequences into a single sequence,
2305 and return the merged sequence. */
2306
2307static tree
2308merge_conversion_sequences (tree user_seq, tree std_seq)
2309{
2310 tree *t;
2311
2312 my_friendly_assert (TREE_CODE (user_seq) == USER_CONV,
2313 20030306);
2314
2315 /* Find the end of the second conversion sequence. */
2316 t = &(std_seq);
2317 while (TREE_CODE (*t) != IDENTITY_CONV)
2318 t = &TREE_OPERAND (*t, 0);
2319
2320 /* Replace the identity conversion with the user conversion
2321 sequence. */
2322 *t = user_seq;
2323
2324 /* The entire sequence is a user-conversion sequence. */
2325 ICS_USER_FLAG (std_seq) = 1;
2326
2327 return std_seq;
2328}
2329
c73964b2 2330/* Returns the best overload candidate to perform the requested
eb66be0e
MS
2331 conversion. This function is used for three the overloading situations
2332 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2333 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2334 per [dcl.init.ref], so we ignore temporary bindings. */
c73964b2
MS
2335
2336static struct z_candidate *
94be8403 2337build_user_type_conversion_1 (tree totype, tree expr, int flags)
c73964b2
MS
2338{
2339 struct z_candidate *candidates, *cand;
2340 tree fromtype = TREE_TYPE (expr);
3d938426 2341 tree ctors = NULL_TREE, convs = NULL_TREE;
a703fb38 2342 tree args = NULL_TREE;
436f8a4c 2343 bool any_viable_p;
c73964b2 2344
5e818b93
JM
2345 /* We represent conversion within a hierarchy using RVALUE_CONV and
2346 BASE_CONV, as specified by [over.best.ics]; these become plain
2347 constructor calls, as specified in [dcl.init]. */
41f5d4b1
NS
2348 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2349 || !DERIVED_FROM_P (totype, fromtype), 20011226);
5e818b93 2350
c73964b2 2351 if (IS_AGGR_TYPE (totype))
db9b2174 2352 ctors = lookup_fnfields (TYPE_BINFO (totype),
1f84ec23 2353 complete_ctor_identifier,
db9b2174
MM
2354 0);
2355
5e818b93 2356 if (IS_AGGR_TYPE (fromtype))
c73964b2
MS
2357 convs = lookup_conversions (fromtype);
2358
2359 candidates = 0;
2360 flags |= LOOKUP_NO_CONVERSION;
2361
2362 if (ctors)
2363 {
454fa7a7
MM
2364 tree t;
2365
50ad9642 2366 ctors = BASELINK_FUNCTIONS (ctors);
454fa7a7
MM
2367
2368 t = build_int_2 (0, 0);
c73964b2 2369 TREE_TYPE (t) = build_pointer_type (totype);
051e6fd7 2370 args = build_tree_list (NULL_TREE, expr);
41f5d4b1
NS
2371 /* We should never try to call the abstract or base constructor
2372 from here. */
2373 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2374 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2375 20011226);
e1b3e07d 2376 args = tree_cons (NULL_TREE, t, args);
c73964b2 2377 }
2c73f9f5 2378 for (; ctors; ctors = OVL_NEXT (ctors))
c73964b2 2379 {
2c73f9f5
ML
2380 tree ctor = OVL_CURRENT (ctors);
2381 if (DECL_NONCONVERTING_P (ctor))
c73964b2
MS
2382 continue;
2383
2c73f9f5 2384 if (TREE_CODE (ctor) == TEMPLATE_DECL)
7993382e
MM
2385 cand = add_template_candidate (&candidates, ctor, totype,
2386 NULL_TREE, args, NULL_TREE,
2387 TYPE_BINFO (totype),
2388 TYPE_BINFO (totype),
2389 flags,
2390 DEDUCE_CALL);
98c1c668 2391 else
7993382e
MM
2392 cand = add_function_candidate (&candidates, ctor, totype,
2393 args, TYPE_BINFO (totype),
2394 TYPE_BINFO (totype),
2395 flags);
98c1c668 2396
7993382e
MM
2397 if (cand)
2398 cand->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
c73964b2
MS
2399 }
2400
2401 if (convs)
051e6fd7 2402 args = build_tree_list (NULL_TREE, build_this (expr));
c73964b2
MS
2403
2404 for (; convs; convs = TREE_CHAIN (convs))
2405 {
4ba126e4
MM
2406 tree fns;
2407 tree conversion_path = TREE_PURPOSE (convs);
eb66be0e 2408 int convflags = LOOKUP_NO_CONVERSION;
eb66be0e
MS
2409
2410 /* If we are called to convert to a reference type, we are trying to
2411 find an lvalue binding, so don't even consider temporaries. If
2412 we don't find an lvalue binding, the caller will try again to
2413 look for a temporary binding. */
2414 if (TREE_CODE (totype) == REFERENCE_TYPE)
2415 convflags |= LOOKUP_NO_TEMP_BIND;
5dd236e2 2416
4ba126e4 2417 for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
5dd236e2
NS
2418 {
2419 tree fn = OVL_CURRENT (fns);
5dd236e2
NS
2420
2421 /* [over.match.funcs] For conversion functions, the function
2422 is considered to be a member of the class of the implicit
2423 object argument for the purpose of defining the type of
2424 the implicit object parameter.
eb66be0e 2425
5dd236e2
NS
2426 So we pass fromtype as CTYPE to add_*_candidate. */
2427
2428 if (TREE_CODE (fn) == TEMPLATE_DECL)
7993382e
MM
2429 cand = add_template_candidate (&candidates, fn, fromtype,
2430 NULL_TREE,
2431 args, totype,
2432 TYPE_BINFO (fromtype),
2433 conversion_path,
2434 flags,
2435 DEDUCE_CONV);
5dd236e2 2436 else
7993382e
MM
2437 cand = add_function_candidate (&candidates, fn, fromtype,
2438 args,
2439 TYPE_BINFO (fromtype),
2440 conversion_path,
2441 flags);
5dd236e2 2442
7993382e 2443 if (cand)
5dd236e2 2444 {
7993382e
MM
2445 tree ics = implicit_conversion (totype,
2446 TREE_TYPE (TREE_TYPE (cand->fn)),
2447 0, convflags);
5dd236e2 2448
7993382e 2449 cand->second_conv = ics;
5dd236e2
NS
2450
2451 if (ics == NULL_TREE)
7993382e 2452 cand->viable = 0;
5dd236e2 2453 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
7993382e 2454 cand->viable = -1;
5dd236e2
NS
2455 }
2456 }
c73964b2
MS
2457 }
2458
436f8a4c
MM
2459 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2460 if (!any_viable_p)
4ba126e4 2461 return 0;
c73964b2 2462
da20811c 2463 cand = tourney (candidates);
c73964b2
MS
2464 if (cand == 0)
2465 {
2466 if (flags & LOOKUP_COMPLAIN)
2467 {
33bd39a2 2468 error ("conversion from `%T' to `%T' is ambiguous",
c73964b2
MS
2469 fromtype, totype);
2470 print_z_candidates (candidates);
2471 }
2472
2473 cand = candidates; /* any one will do */
2474 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2475 ICS_USER_FLAG (cand->second_conv) = 1;
f576dfc4
JM
2476 if (!any_strictly_viable (candidates))
2477 ICS_BAD_FLAG (cand->second_conv) = 1;
2478 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2479 ambiguous conversion is no worse than another user-defined
2480 conversion. */
c73964b2
MS
2481
2482 return cand;
2483 }
2484
3d938426
MM
2485 /* Build the user conversion sequence. */
2486 convs = build_conv
c73964b2
MS
2487 (USER_CONV,
2488 (DECL_CONSTRUCTOR_P (cand->fn)
2489 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
3d938426
MM
2490 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
2491 TREE_OPERAND (convs, 1) = build_zc_wrapper (cand);
2492
2493 /* Combine it with the second conversion sequence. */
2494 cand->second_conv = merge_conversion_sequences (convs,
2495 cand->second_conv);
2496
faf5394a 2497 if (cand->viable == -1)
3d938426 2498 ICS_BAD_FLAG (cand->second_conv) = 1;
c73964b2
MS
2499
2500 return cand;
2501}
2502
2503tree
94be8403 2504build_user_type_conversion (tree totype, tree expr, int flags)
c73964b2
MS
2505{
2506 struct z_candidate *cand
2507 = build_user_type_conversion_1 (totype, expr, flags);
2508
2509 if (cand)
2510 {
2511 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2512 return error_mark_node;
5e818b93 2513 return convert_from_reference (convert_like (cand->second_conv, expr));
c73964b2
MS
2514 }
2515 return NULL_TREE;
2516}
2517
86e6f22f
JM
2518/* Do any initial processing on the arguments to a function call. */
2519
2520static tree
94be8403 2521resolve_args (tree args)
86e6f22f
JM
2522{
2523 tree t;
2524 for (t = args; t; t = TREE_CHAIN (t))
2525 {
648c2206
NS
2526 tree arg = TREE_VALUE (t);
2527
2528 if (arg == error_mark_node)
86e6f22f 2529 return error_mark_node;
648c2206 2530 else if (VOID_TYPE_P (TREE_TYPE (arg)))
86e6f22f 2531 {
8251199e 2532 error ("invalid use of void expression");
86e6f22f
JM
2533 return error_mark_node;
2534 }
648c2206
NS
2535 arg = convert_from_reference (arg);
2536 TREE_VALUE (t) = arg;
86e6f22f
JM
2537 }
2538 return args;
2539}
4ba126e4 2540
125e6594
MM
2541/* Perform overload resolution on FN, which is called with the ARGS.
2542
2543 Return the candidate function selected by overload resolution, or
2544 NULL if the event that overload resolution failed. In the case
2545 that overload resolution fails, *CANDIDATES will be the set of
2546 candidates considered, and ANY_VIABLE_P will be set to true or
2547 false to indicate whether or not any of the candidates were
2548 viable.
2549
2550 The ARGS should already have gone through RESOLVE_ARGS before this
2551 function is called. */
2552
2553static struct z_candidate *
2554perform_overload_resolution (tree fn,
2555 tree args,
2556 struct z_candidate **candidates,
2557 bool *any_viable_p)
c73964b2 2558{
125e6594 2559 struct z_candidate *cand;
386b8a85 2560 tree explicit_targs = NULL_TREE;
c32381b1 2561 int template_only = 0;
386b8a85 2562
125e6594
MM
2563 *candidates = NULL;
2564 *any_viable_p = true;
2565
4ba126e4
MM
2566 /* Check FN and ARGS. */
2567 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2568 || TREE_CODE (fn) == TEMPLATE_DECL
2569 || TREE_CODE (fn) == OVERLOAD
2570 || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2571 20020712);
2572 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2573 20020712);
2574
386b8a85
JM
2575 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2576 {
2577 explicit_targs = TREE_OPERAND (fn, 1);
2578 fn = TREE_OPERAND (fn, 0);
c32381b1 2579 template_only = 1;
386b8a85
JM
2580 }
2581
125e6594
MM
2582 /* Add the various candidate functions. */
2583 add_candidates (fn, args, explicit_targs, template_only,
2584 /*conversion_path=*/NULL_TREE,
2585 /*access_path=*/NULL_TREE,
2586 LOOKUP_NORMAL,
2587 candidates);
2588
436f8a4c
MM
2589 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
2590 if (!*any_viable_p)
2591 return NULL;
c73964b2 2592
125e6594 2593 cand = tourney (*candidates);
125e6594
MM
2594 return cand;
2595}
86e6f22f 2596
125e6594
MM
2597/* Return an expression for a call to FN (a namespace-scope function,
2598 or a static member function) with the ARGS. */
2599
2600tree
2601build_new_function_call (tree fn, tree args)
2602{
2603 struct z_candidate *candidates, *cand;
2604 bool any_viable_p;
8f032717 2605
125e6594
MM
2606 args = resolve_args (args);
2607 if (args == error_mark_node)
2608 return error_mark_node;
a723baf1 2609
125e6594 2610 cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p);
c73964b2 2611
125e6594
MM
2612 if (!cand)
2613 {
2614 if (!any_viable_p && candidates && ! candidates->next)
2615 return build_function_call (candidates->fn, args);
2616 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2617 fn = TREE_OPERAND (fn, 0);
2618 if (!any_viable_p)
2619 error ("no matching function for call to `%D(%A)'",
2620 DECL_NAME (OVL_CURRENT (fn)), args);
2621 else
2622 error ("call of overloaded `%D(%A)' is ambiguous",
436f8a4c 2623 DECL_NAME (OVL_CURRENT (fn)), args);
125e6594
MM
2624 if (candidates)
2625 print_z_candidates (candidates);
2626 return error_mark_node;
2627 }
c73964b2 2628
125e6594
MM
2629 return build_over_call (cand, LOOKUP_NORMAL);
2630}
c73964b2 2631
125e6594
MM
2632/* Build a call to a global operator new. FNNAME is the name of the
2633 operator (either "operator new" or "operator new[]") and ARGS are
2634 the arguments provided. *SIZE points to the total number of bytes
2635 required by the allocation, and is updated if that is changed here.
2636 *COOKIE_SIZE is non-NULL if a cookie should be used. If this
34cd5ae7 2637 function determines that no cookie should be used, after all,
9bcb9aae 2638 *COOKIE_SIZE is set to NULL_TREE. */
c73964b2 2639
125e6594
MM
2640tree
2641build_operator_new_call (tree fnname, tree args, tree *size, tree *cookie_size)
2642{
2643 tree fns;
2644 struct z_candidate *candidates;
2645 struct z_candidate *cand;
2646 bool any_viable_p;
2647
2648 args = tree_cons (NULL_TREE, *size, args);
2649 args = resolve_args (args);
2650 if (args == error_mark_node)
2651 return args;
2652
2653 fns = lookup_function_nonclass (fnname, args);
2c73f9f5 2654
125e6594
MM
2655 /* Figure out what function is being called. */
2656 cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p);
2657
2658 /* If no suitable function could be found, issue an error message
2659 and give up. */
2660 if (!cand)
2661 {
2662 if (!any_viable_p)
2663 error ("no matching function for call to `%D(%A)'",
2664 DECL_NAME (OVL_CURRENT (fns)), args);
2665 else
8e854b76 2666 error ("call of overloaded `%D(%A)' is ambiguous",
436f8a4c 2667 DECL_NAME (OVL_CURRENT (fns)), args);
125e6594
MM
2668 if (candidates)
2669 print_z_candidates (candidates);
2670 return error_mark_node;
2671 }
2672
2673 /* If a cookie is required, add some extra space. Whether
2674 or not a cookie is required cannot be determined until
2675 after we know which function was called. */
2676 if (*cookie_size)
2677 {
2678 bool use_cookie = true;
2679 if (!abi_version_at_least (2))
2680 {
2681 tree placement = TREE_CHAIN (args);
2682 /* In G++ 3.2, the check was implemented incorrectly; it
2683 looked at the placement expression, rather than the
2684 type of the function. */
2685 if (placement && !TREE_CHAIN (placement)
2686 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2687 ptr_type_node))
2688 use_cookie = false;
2689 }
2690 else
2691 {
2692 tree arg_types;
2693
2694 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
2695 /* Skip the size_t parameter. */
2696 arg_types = TREE_CHAIN (arg_types);
2697 /* Check the remaining parameters (if any). */
2698 if (arg_types
2699 && TREE_CHAIN (arg_types) == void_list_node
2700 && same_type_p (TREE_VALUE (arg_types),
2701 ptr_type_node))
2702 use_cookie = false;
2703 }
2704 /* If we need a cookie, adjust the number of bytes allocated. */
2705 if (use_cookie)
2706 {
2707 /* Update the total size. */
2708 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
2709 /* Update the argument list to reflect the adjusted size. */
2710 TREE_VALUE (args) = *size;
2711 }
2712 else
2713 *cookie_size = NULL_TREE;
2714 }
2715
2716 /* Build the CALL_EXPR. */
2717 return build_over_call (cand, LOOKUP_NORMAL);
c73964b2
MS
2718}
2719
bd6dd845 2720static tree
94be8403 2721build_object_call (tree obj, tree args)
c73964b2
MS
2722{
2723 struct z_candidate *candidates = 0, *cand;
a703fb38 2724 tree fns, convs, mem_args = NULL_TREE;
c73964b2 2725 tree type = TREE_TYPE (obj);
436f8a4c 2726 bool any_viable_p;
c73964b2 2727
297dcfb3
MM
2728 if (TYPE_PTRMEMFUNC_P (type))
2729 {
2730 /* It's no good looking for an overloaded operator() on a
2731 pointer-to-member-function. */
33bd39a2 2732 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
297dcfb3
MM
2733 return error_mark_node;
2734 }
2735
596ea4e5 2736 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
734e8cc5
MM
2737 if (fns == error_mark_node)
2738 return error_mark_node;
c73964b2 2739
86e6f22f
JM
2740 args = resolve_args (args);
2741
2742 if (args == error_mark_node)
2743 return error_mark_node;
2744
c73964b2
MS
2745 if (fns)
2746 {
50ad9642 2747 tree base = BINFO_TYPE (BASELINK_BINFO (fns));
e1b3e07d 2748 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
c73964b2 2749
50ad9642 2750 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
c73964b2 2751 {
2c73f9f5 2752 tree fn = OVL_CURRENT (fns);
786b5245 2753 if (TREE_CODE (fn) == TEMPLATE_DECL)
7993382e
MM
2754 add_template_candidate (&candidates, fn, base, NULL_TREE,
2755 mem_args, NULL_TREE,
2756 TYPE_BINFO (type),
2757 TYPE_BINFO (type),
2758 LOOKUP_NORMAL, DEDUCE_CALL);
786b5245 2759 else
7993382e
MM
2760 add_function_candidate
2761 (&candidates, fn, base, mem_args, TYPE_BINFO (type),
4ba126e4 2762 TYPE_BINFO (type), LOOKUP_NORMAL);
c73964b2
MS
2763 }
2764 }
2765
2766 convs = lookup_conversions (type);
2767
2768 for (; convs; convs = TREE_CHAIN (convs))
2769 {
2c73f9f5
ML
2770 tree fns = TREE_VALUE (convs);
2771 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
c73964b2 2772
59e76fc6 2773 if ((TREE_CODE (totype) == POINTER_TYPE
477f6664
JM
2774 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2775 || (TREE_CODE (totype) == REFERENCE_TYPE
2776 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2777 || (TREE_CODE (totype) == REFERENCE_TYPE
2778 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2779 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
d64db93f 2780 for (; fns; fns = OVL_NEXT (fns))
c73964b2 2781 {
d64db93f 2782 tree fn = OVL_CURRENT (fns);
786b5245 2783 if (TREE_CODE (fn) == TEMPLATE_DECL)
7993382e
MM
2784 add_template_conv_candidate
2785 (&candidates, fn, obj, args, totype,
2786 /*access_path=*/NULL_TREE,
2787 /*conversion_path=*/NULL_TREE);
786b5245 2788 else
7993382e
MM
2789 add_conv_candidate (&candidates, fn, obj, args,
2790 /*conversion_path=*/NULL_TREE,
2791 /*access_path=*/NULL_TREE);
c73964b2
MS
2792 }
2793 }
2794
436f8a4c
MM
2795 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2796 if (!any_viable_p)
c73964b2 2797 {
33bd39a2 2798 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
c73964b2
MS
2799 print_z_candidates (candidates);
2800 return error_mark_node;
2801 }
2802
da20811c 2803 cand = tourney (candidates);
c73964b2
MS
2804 if (cand == 0)
2805 {
33bd39a2 2806 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
c73964b2
MS
2807 print_z_candidates (candidates);
2808 return error_mark_node;
2809 }
2810
f0bcd168
MM
2811 /* Since cand->fn will be a type, not a function, for a conversion
2812 function, we must be careful not to unconditionally look at
2813 DECL_NAME here. */
2814 if (TREE_CODE (cand->fn) == FUNCTION_DECL
596ea4e5 2815 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
b80f8ef3 2816 return build_over_call (cand, LOOKUP_NORMAL);
c73964b2 2817
c3f08228
NS
2818 obj = convert_like_with_context
2819 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
c73964b2
MS
2820
2821 /* FIXME */
2822 return build_function_call (obj, args);
2823}
2824
2825static void
94be8403
GDR
2826op_error (enum tree_code code, enum tree_code code2,
2827 tree arg1, tree arg2, tree arg3, const char *problem)
c73964b2 2828{
cdb71673 2829 const char *opname;
596ea4e5
AS
2830
2831 if (code == MODIFY_EXPR)
2832 opname = assignment_operator_name_info[code2].name;
2833 else
2834 opname = operator_name_info[code].name;
c73964b2
MS
2835
2836 switch (code)
2837 {
2838 case COND_EXPR:
84cc377e
GDR
2839 error ("%s for ternary 'operator?:' in '%E ? %E : %E'",
2840 problem, arg1, arg2, arg3);
c73964b2 2841 break;
84cc377e 2842
c73964b2
MS
2843 case POSTINCREMENT_EXPR:
2844 case POSTDECREMENT_EXPR:
84cc377e 2845 error ("%s for 'operator%s' in '%E%s'", problem, opname, arg1, opname);
c73964b2 2846 break;
84cc377e 2847
c73964b2 2848 case ARRAY_REF:
84cc377e 2849 error ("%s for 'operator[]' in '%E[%E]'", problem, arg1, arg2);
c73964b2 2850 break;
19948e32
GDR
2851
2852 case REALPART_EXPR:
2853 case IMAGPART_EXPR:
2854 error ("%s for '%s' in '%s %E'", problem, opname, opname, arg1);
2855 break;
84cc377e 2856
c73964b2
MS
2857 default:
2858 if (arg2)
84cc377e
GDR
2859 error ("%s for 'operator%s' in '%E %s %E'",
2860 problem, opname, arg1, opname, arg2);
c73964b2 2861 else
84cc377e
GDR
2862 error ("%s for 'operator%s' in '%s%E'",
2863 problem, opname, opname, arg1);
2864 break;
c73964b2
MS
2865 }
2866}
2867
a7a64a77
MM
2868/* Return the implicit conversion sequence that could be used to
2869 convert E1 to E2 in [expr.cond]. */
2870
2871static tree
94be8403 2872conditional_conversion (tree e1, tree e2)
a7a64a77
MM
2873{
2874 tree t1 = non_reference (TREE_TYPE (e1));
2875 tree t2 = non_reference (TREE_TYPE (e2));
2876 tree conv;
9cefd2ca 2877 bool good_base;
a7a64a77
MM
2878
2879 /* [expr.cond]
2880
2881 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2882 implicitly converted (clause _conv_) to the type "reference to
2883 T2", subject to the constraint that in the conversion the
2884 reference must bind directly (_dcl.init.ref_) to E1. */
2885 if (real_lvalue_p (e2))
2886 {
2887 conv = implicit_conversion (build_reference_type (t2),
2888 t1,
2889 e1,
2890 LOOKUP_NO_TEMP_BIND);
2891 if (conv)
2892 return conv;
2893 }
2894
2895 /* [expr.cond]
2896
2897 If E1 and E2 have class type, and the underlying class types are
2898 the same or one is a base class of the other: E1 can be converted
2899 to match E2 if the class of T2 is the same type as, or a base
2900 class of, the class of T1, and the cv-qualification of T2 is the
2901 same cv-qualification as, or a greater cv-qualification than, the
2902 cv-qualification of T1. If the conversion is applied, E1 is
2903 changed to an rvalue of type T2 that still refers to the original
5f7262e6
JM
2904 source class object (or the appropriate subobject thereof).
2905
2906 FIXME we can't express an rvalue that refers to the original object;
2907 we have to create a new one. */
a7a64a77 2908 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
9cefd2ca 2909 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
a7a64a77 2910 {
9cefd2ca 2911 if (good_base && at_least_as_qualified_p (t2, t1))
a7a64a77
MM
2912 {
2913 conv = build1 (IDENTITY_CONV, t1, e1);
4f0aa416
MM
2914 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2915 TYPE_MAIN_VARIANT (t2)))
5f7262e6
JM
2916 {
2917 conv = build_conv (BASE_CONV, t2, conv);
2918 NEED_TEMPORARY_P (conv) = 1;
2919 }
2920 else
2921 conv = build_conv (RVALUE_CONV, t2, conv);
a7a64a77
MM
2922 return conv;
2923 }
2924 else
2925 return NULL_TREE;
2926 }
9cefd2ca
JM
2927 else
2928 /* [expr.cond]
a7a64a77 2929
9cefd2ca
JM
2930 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
2931 converted to the type that expression E2 would have if E2 were
2932 converted to an rvalue (or the type it has, if E2 is an rvalue). */
2933 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
a7a64a77
MM
2934}
2935
2936/* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
4ba126e4 2937 arguments to the conditional expression. */
a7a64a77
MM
2938
2939tree
94be8403 2940build_conditional_expr (tree arg1, tree arg2, tree arg3)
a7a64a77
MM
2941{
2942 tree arg2_type;
2943 tree arg3_type;
2944 tree result;
2945 tree result_type = NULL_TREE;
94be8403 2946 bool lvalue_p = true;
a7a64a77
MM
2947 struct z_candidate *candidates = 0;
2948 struct z_candidate *cand;
2949
2950 /* As a G++ extension, the second argument to the conditional can be
2951 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
09dd27d4
MM
2952 c'.) If the second operand is omitted, make sure it is
2953 calculated only once. */
a7a64a77
MM
2954 if (!arg2)
2955 {
2956 if (pedantic)
cb9a3ff8 2957 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
4e8dca1c
JM
2958
2959 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
2960 if (real_lvalue_p (arg1))
2961 arg2 = arg1 = stabilize_reference (arg1);
2962 else
2963 arg2 = arg1 = save_expr (arg1);
a7a64a77
MM
2964 }
2965
07c88314
MM
2966 /* [expr.cond]
2967
2968 The first expr ession is implicitly converted to bool (clause
2969 _conv_). */
6cf4d1bc 2970 arg1 = perform_implicit_conversion (boolean_type_node, arg1);
07c88314 2971
a7a64a77
MM
2972 /* If something has already gone wrong, just pass that fact up the
2973 tree. */
6cf4d1bc
MM
2974 if (error_operand_p (arg1)
2975 || error_operand_p (arg2)
2976 || error_operand_p (arg3))
a7a64a77
MM
2977 return error_mark_node;
2978
a7a64a77
MM
2979 /* [expr.cond]
2980
2981 If either the second or the third operand has type (possibly
2982 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2983 array-to-pointer (_conv.array_), and function-to-pointer
2984 (_conv.func_) standard conversions are performed on the second
2985 and third operands. */
2986 arg2_type = TREE_TYPE (arg2);
2987 arg3_type = TREE_TYPE (arg3);
b72801e2 2988 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
a7a64a77 2989 {
a7a64a77
MM
2990 /* Do the conversions. We don't these for `void' type arguments
2991 since it can't have any effect and since decay_conversion
2992 does not handle that case gracefully. */
b72801e2 2993 if (!VOID_TYPE_P (arg2_type))
a7a64a77 2994 arg2 = decay_conversion (arg2);
b72801e2 2995 if (!VOID_TYPE_P (arg3_type))
a7a64a77
MM
2996 arg3 = decay_conversion (arg3);
2997 arg2_type = TREE_TYPE (arg2);
2998 arg3_type = TREE_TYPE (arg3);
2999
a7a64a77
MM
3000 /* [expr.cond]
3001
3002 One of the following shall hold:
3003
3004 --The second or the third operand (but not both) is a
3005 throw-expression (_except.throw_); the result is of the
3006 type of the other and is an rvalue.
3007
3008 --Both the second and the third operands have type void; the
00a17e31 3009 result is of type void and is an rvalue. */
a7a64a77
MM
3010 if ((TREE_CODE (arg2) == THROW_EXPR)
3011 ^ (TREE_CODE (arg3) == THROW_EXPR))
3012 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
df39af7d 3013 ? arg3_type : arg2_type);
b72801e2 3014 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
a7a64a77
MM
3015 result_type = void_type_node;
3016 else
3017 {
33bd39a2 3018 error ("`%E' has type `void' and is not a throw-expression",
b72801e2 3019 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
a7a64a77
MM
3020 return error_mark_node;
3021 }
3022
94be8403 3023 lvalue_p = false;
a7a64a77
MM
3024 goto valid_operands;
3025 }
3026 /* [expr.cond]
3027
3028 Otherwise, if the second and third operand have different types,
3029 and either has (possibly cv-qualified) class type, an attempt is
3030 made to convert each of those operands to the type of the other. */
3031 else if (!same_type_p (arg2_type, arg3_type)
3032 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3033 {
3034 tree conv2 = conditional_conversion (arg2, arg3);
3035 tree conv3 = conditional_conversion (arg3, arg2);
3036
3037 /* [expr.cond]
3038
3039 If both can be converted, or one can be converted but the
3040 conversion is ambiguous, the program is ill-formed. If
3041 neither can be converted, the operands are left unchanged and
3042 further checking is performed as described below. If exactly
3043 one conversion is possible, that conversion is applied to the
3044 chosen operand and the converted operand is used in place of
3045 the original operand for the remainder of this section. */
3046 if ((conv2 && !ICS_BAD_FLAG (conv2)
3047 && conv3 && !ICS_BAD_FLAG (conv3))
3048 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3049 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3050 {
33bd39a2 3051 error ("operands to ?: have different types");
a7a64a77
MM
3052 return error_mark_node;
3053 }
3054 else if (conv2 && !ICS_BAD_FLAG (conv2))
3055 {
3056 arg2 = convert_like (conv2, arg2);
442aa4ec 3057 arg2 = convert_from_reference (arg2);
aa779cf3
JM
3058 if (!same_type_p (TREE_TYPE (arg2), arg3_type)
3059 && CLASS_TYPE_P (arg3_type))
3060 /* The types need to match if we're converting to a class type.
3061 If not, we don't care about cv-qual mismatches, since
3062 non-class rvalues are not cv-qualified. */
5f7262e6 3063 abort ();
a7a64a77
MM
3064 arg2_type = TREE_TYPE (arg2);
3065 }
3066 else if (conv3 && !ICS_BAD_FLAG (conv3))
3067 {
3068 arg3 = convert_like (conv3, arg3);
442aa4ec 3069 arg3 = convert_from_reference (arg3);
aa779cf3
JM
3070 if (!same_type_p (TREE_TYPE (arg3), arg2_type)
3071 && CLASS_TYPE_P (arg2_type))
5f7262e6 3072 abort ();
a7a64a77
MM
3073 arg3_type = TREE_TYPE (arg3);
3074 }
3075 }
3076
3077 /* [expr.cond]
3078
3079 If the second and third operands are lvalues and have the same
3080 type, the result is of that type and is an lvalue. */
d18a8251
MM
3081 if (real_lvalue_p (arg2)
3082 && real_lvalue_p (arg3)
3083 && same_type_p (arg2_type, arg3_type))
a7a64a77
MM
3084 {
3085 result_type = arg2_type;
3086 goto valid_operands;
3087 }
3088
3089 /* [expr.cond]
3090
3091 Otherwise, the result is an rvalue. If the second and third
3092 operand do not have the same type, and either has (possibly
3093 cv-qualified) class type, overload resolution is used to
3094 determine the conversions (if any) to be applied to the operands
3095 (_over.match.oper_, _over.built_). */
94be8403 3096 lvalue_p = false;
a7a64a77
MM
3097 if (!same_type_p (arg2_type, arg3_type)
3098 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3099 {
3100 tree args[3];
3101 tree conv;
436f8a4c 3102 bool any_viable_p;
a7a64a77
MM
3103
3104 /* Rearrange the arguments so that add_builtin_candidate only has
3105 to know about two args. In build_builtin_candidates, the
3106 arguments are unscrambled. */
3107 args[0] = arg2;
3108 args[1] = arg3;
3109 args[2] = arg1;
7993382e
MM
3110 add_builtin_candidates (&candidates,
3111 COND_EXPR,
3112 NOP_EXPR,
3113 ansi_opname (COND_EXPR),
3114 args,
3115 LOOKUP_NORMAL);
a7a64a77
MM
3116
3117 /* [expr.cond]
3118
3119 If the overload resolution fails, the program is
3120 ill-formed. */
436f8a4c
MM
3121 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3122 if (!any_viable_p)
a7a64a77
MM
3123 {
3124 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3125 print_z_candidates (candidates);
3126 return error_mark_node;
3127 }
a7a64a77
MM
3128 cand = tourney (candidates);
3129 if (!cand)
3130 {
3131 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3132 print_z_candidates (candidates);
3133 return error_mark_node;
3134 }
3135
3136 /* [expr.cond]
3137
3138 Otherwise, the conversions thus determined are applied, and
3139 the converted operands are used in place of the original
3140 operands for the remainder of this section. */
3141 conv = TREE_VEC_ELT (cand->convs, 0);
3142 arg1 = convert_like (conv, arg1);
3143 conv = TREE_VEC_ELT (cand->convs, 1);
3144 arg2 = convert_like (conv, arg2);
3145 conv = TREE_VEC_ELT (cand->convs, 2);
3146 arg3 = convert_like (conv, arg3);
3147 }
3148
3149 /* [expr.cond]
3150
3151 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3152 and function-to-pointer (_conv.func_) standard conversions are
50fd6343
JM
3153 performed on the second and third operands.
3154
3155 We need to force the lvalue-to-rvalue conversion here for class types,
3156 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3157 that isn't wrapped with a TARGET_EXPR plays havoc with exception
db5ae31b
JM
3158 regions.
3159
3160 We use ocp_convert rather than build_user_type_conversion because the
3161 latter returns NULL_TREE on failure, while the former gives an error. */
50fd6343 3162
f7b9026e 3163 arg2 = force_rvalue (arg2);
a7a64a77 3164 arg2_type = TREE_TYPE (arg2);
50fd6343 3165
f7b9026e 3166 arg3 = force_rvalue (arg3);
a7a64a77
MM
3167 arg3_type = TREE_TYPE (arg3);
3168
40260429
NS
3169 if (arg2 == error_mark_node || arg3 == error_mark_node)
3170 return error_mark_node;
3171
a7a64a77
MM
3172 /* [expr.cond]
3173
3174 After those conversions, one of the following shall hold:
3175
3176 --The second and third operands have the same type; the result is of
3177 that type. */
3178 if (same_type_p (arg2_type, arg3_type))
3179 result_type = arg2_type;
3180 /* [expr.cond]
3181
3182 --The second and third operands have arithmetic or enumeration
3183 type; the usual arithmetic conversions are performed to bring
3184 them to a common type, and the result is of that type. */
3185 else if ((ARITHMETIC_TYPE_P (arg2_type)
3186 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3187 && (ARITHMETIC_TYPE_P (arg3_type)
3188 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3189 {
3190 /* In this case, there is always a common type. */
3191 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3192 arg3_type);
1b4d752a
NS
3193
3194 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3195 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
33bd39a2 3196 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
1b4d752a
NS
3197 arg2_type, arg3_type);
3198 else if (extra_warnings
3199 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3200 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3201 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3202 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
33bd39a2 3203 warning ("enumeral and non-enumeral type in conditional expression");
1b4d752a 3204
4143af33
MM
3205 arg2 = perform_implicit_conversion (result_type, arg2);
3206 arg3 = perform_implicit_conversion (result_type, arg3);
a7a64a77
MM
3207 }
3208 /* [expr.cond]
3209
3210 --The second and third operands have pointer type, or one has
3211 pointer type and the other is a null pointer constant; pointer
3212 conversions (_conv.ptr_) and qualification conversions
3213 (_conv.qual_) are performed to bring them to their composite
3214 pointer type (_expr.rel_). The result is of the composite
3215 pointer type.
3216
3217 --The second and third operands have pointer to member type, or
3218 one has pointer to member type and the other is a null pointer
3219 constant; pointer to member conversions (_conv.mem_) and
3220 qualification conversions (_conv.qual_) are performed to bring
3221 them to a common type, whose cv-qualification shall match the
3222 cv-qualification of either the second or the third operand.
00a17e31 3223 The result is of the common type. */
a7a64a77 3224 else if ((null_ptr_cst_p (arg2)
a5ac359a 3225 && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
a7a64a77 3226 || (null_ptr_cst_p (arg3)
a5ac359a 3227 && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
a7a64a77
MM
3228 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3229 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
a5ac359a 3230 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
a7a64a77
MM
3231 {
3232 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3233 arg3, "conditional expression");
6cf4d1bc
MM
3234 if (result_type == error_mark_node)
3235 return error_mark_node;
4143af33
MM
3236 arg2 = perform_implicit_conversion (result_type, arg2);
3237 arg3 = perform_implicit_conversion (result_type, arg3);
a7a64a77
MM
3238 }
3239
3240 if (!result_type)
3241 {
33bd39a2 3242 error ("operands to ?: have different types");
a7a64a77
MM
3243 return error_mark_node;
3244 }
3245
3246 valid_operands:
3247 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
a65fd2d7
JM
3248 /* We can't use result_type below, as fold might have returned a
3249 throw_expr. */
3250
a7a64a77 3251 /* Expand both sides into the same slot, hopefully the target of the
50fd6343
JM
3252 ?: expression. We used to check for TARGET_EXPRs here, but now we
3253 sometimes wrap them in NOP_EXPRs so the test would fail. */
a65fd2d7
JM
3254 if (!lvalue_p && IS_AGGR_TYPE (TREE_TYPE (result)))
3255 result = get_target_expr (result);
a7a64a77
MM
3256
3257 /* If this expression is an rvalue, but might be mistaken for an
3258 lvalue, we must add a NON_LVALUE_EXPR. */
3259 if (!lvalue_p && real_lvalue_p (result))
a65fd2d7 3260 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
a7a64a77
MM
3261
3262 return result;
3263}
3264
14d22dd6
MM
3265/* OPERAND is an operand to an expression. Perform necessary steps
3266 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
3267 returned. */
3268
3269static tree
3270prep_operand (tree operand)
3271{
3272 if (operand)
3273 {
14d22dd6
MM
3274 operand = convert_from_reference (operand);
3275 if (CLASS_TYPE_P (TREE_TYPE (operand))
3276 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
3277 /* Make sure the template type is instantiated now. */
3278 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
3279 }
3280
3281 return operand;
3282}
3283
b80f8ef3
MM
3284/* Add each of the viable functions in FNS (a FUNCTION_DECL or
3285 OVERLOAD) to the CANDIDATES, returning an updated list of
3286 CANDIDATES. The ARGS are the arguments provided to the call,
125e6594
MM
3287 without any implicit object parameter. The EXPLICIT_TARGS are
3288 explicit template arguments provided. TEMPLATE_ONLY is true if
da1d7781 3289 only template functions should be considered. CONVERSION_PATH,
b80f8ef3
MM
3290 ACCESS_PATH, and FLAGS are as for add_function_candidate. */
3291
7993382e 3292static void
125e6594
MM
3293add_candidates (tree fns, tree args,
3294 tree explicit_targs, bool template_only,
b80f8ef3
MM
3295 tree conversion_path, tree access_path,
3296 int flags,
7993382e 3297 struct z_candidate **candidates)
b80f8ef3
MM
3298{
3299 tree ctype;
3300 tree non_static_args;
3301
3302 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
3303 /* Delay creating the implicit this parameter until it is needed. */
3304 non_static_args = NULL_TREE;
3305
3306 while (fns)
3307 {
3308 tree fn;
3309 tree fn_args;
3310
3311 fn = OVL_CURRENT (fns);
3312 /* Figure out which set of arguments to use. */
125e6594 3313 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
b80f8ef3
MM
3314 {
3315 /* If this function is a non-static member, prepend the implicit
3316 object parameter. */
3317 if (!non_static_args)
3318 non_static_args = tree_cons (NULL_TREE,
3319 build_this (TREE_VALUE (args)),
3320 TREE_CHAIN (args));
3321 fn_args = non_static_args;
3322 }
3323 else
3324 /* Otherwise, just use the list of arguments provided. */
3325 fn_args = args;
3326
3327 if (TREE_CODE (fn) == TEMPLATE_DECL)
7993382e
MM
3328 add_template_candidate (candidates,
3329 fn,
3330 ctype,
125e6594 3331 explicit_targs,
7993382e
MM
3332 fn_args,
3333 NULL_TREE,
3334 access_path,
3335 conversion_path,
3336 flags,
3337 DEDUCE_CALL);
125e6594 3338 else if (!template_only)
7993382e
MM
3339 add_function_candidate (candidates,
3340 fn,
3341 ctype,
3342 fn_args,
3343 access_path,
3344 conversion_path,
3345 flags);
b80f8ef3
MM
3346 fns = OVL_NEXT (fns);
3347 }
b80f8ef3
MM
3348}
3349
c73964b2 3350tree
94be8403 3351build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3)
c73964b2
MS
3352{
3353 struct z_candidate *candidates = 0, *cand;
b80f8ef3
MM
3354 tree arglist, fnname;
3355 tree args[3];
c73964b2 3356 enum tree_code code2 = NOP_EXPR;
7e3af374 3357 tree conv;
436f8a4c
MM
3358 bool strict_p;
3359 bool any_viable_p;
c73964b2 3360
a723baf1
MM
3361 if (error_operand_p (arg1)
3362 || error_operand_p (arg2)
3363 || error_operand_p (arg3))
c73964b2
MS
3364 return error_mark_node;
3365
3366 if (code == MODIFY_EXPR)
3367 {
3368 code2 = TREE_CODE (arg3);
3369 arg3 = NULL_TREE;
596ea4e5 3370 fnname = ansi_assopname (code2);
c73964b2
MS
3371 }
3372 else
596ea4e5 3373 fnname = ansi_opname (code);
c73964b2 3374
14d22dd6 3375 arg1 = prep_operand (arg1);
648c2206 3376
c73964b2
MS
3377 switch (code)
3378 {
3379 case NEW_EXPR:
3380 case VEC_NEW_EXPR:
c73964b2
MS
3381 case VEC_DELETE_EXPR:
3382 case DELETE_EXPR:
00a17e31 3383 /* Use build_op_new_call and build_op_delete_call instead. */
a98facb0 3384 abort ();
c73964b2
MS
3385
3386 case CALL_EXPR:
3387 return build_object_call (arg1, arg2);
7f85441b
KG
3388
3389 default:
3390 break;
c73964b2
MS
3391 }
3392
14d22dd6
MM
3393 arg2 = prep_operand (arg2);
3394 arg3 = prep_operand (arg3);
4b48a93e 3395
5156628f
MS
3396 if (code == COND_EXPR)
3397 {
beb53fb8
JM
3398 if (arg2 == NULL_TREE
3399 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
5156628f
MS
3400 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3401 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3402 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3403 goto builtin;
3404 }
3405 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3406 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
c73964b2
MS
3407 goto builtin;
3408
3409 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3410 arg2 = integer_zero_node;
3411
477558bf
NS
3412 arglist = NULL_TREE;
3413 if (arg3)
3414 arglist = tree_cons (NULL_TREE, arg3, arglist);
3415 if (arg2)
3416 arglist = tree_cons (NULL_TREE, arg2, arglist);
3417 arglist = tree_cons (NULL_TREE, arg1, arglist);
c73964b2 3418
b80f8ef3
MM
3419 /* Add namespace-scope operators to the list of functions to
3420 consider. */
7993382e 3421 add_candidates (lookup_function_nonclass (fnname, arglist),
125e6594 3422 arglist, NULL_TREE, false, NULL_TREE, NULL_TREE,
7993382e 3423 flags, &candidates);
b80f8ef3
MM
3424 /* Add class-member operators to the candidate set. */
3425 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
c73964b2 3426 {
b80f8ef3 3427 tree fns;
c73964b2 3428
734e8cc5
MM
3429 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3430 if (fns == error_mark_node)
3431 return fns;
b80f8ef3 3432 if (fns)
7993382e 3433 add_candidates (BASELINK_FUNCTIONS (fns), arglist,
125e6594 3434 NULL_TREE, false,
7993382e
MM
3435 BASELINK_BINFO (fns),
3436 TYPE_BINFO (TREE_TYPE (arg1)),
3437 flags, &candidates);
734e8cc5 3438 }
c73964b2 3439
b80f8ef3
MM
3440 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3441 to know about two args; a builtin candidate will always have a first
3442 parameter of type bool. We'll handle that in
3443 build_builtin_candidate. */
3444 if (code == COND_EXPR)
c73964b2 3445 {
b80f8ef3
MM
3446 args[0] = arg2;
3447 args[1] = arg3;
3448 args[2] = arg1;
3449 }
3450 else
3451 {
3452 args[0] = arg1;
3453 args[1] = arg2;
3454 args[2] = NULL_TREE;
c73964b2
MS
3455 }
3456
7993382e 3457 add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
c73964b2 3458
ecc42c14
AO
3459 switch (code)
3460 {
3461 case COMPOUND_EXPR:
3462 case ADDR_EXPR:
3463 /* For these, the built-in candidates set is empty
3464 [over.match.oper]/3. We don't want non-strict matches
3465 because exact matches are always possible with built-in
3466 operators. The built-in candidate set for COMPONENT_REF
3467 would be empty too, but since there are no such built-in
3468 operators, we accept non-strict matches for them. */
436f8a4c 3469 strict_p = true;
ecc42c14
AO
3470 break;
3471
3472 default:
436f8a4c 3473 strict_p = pedantic;
ecc42c14
AO
3474 break;
3475 }
3476
436f8a4c
MM
3477 candidates = splice_viable (candidates, strict_p, &any_viable_p);
3478 if (!any_viable_p)
c73964b2
MS
3479 {
3480 switch (code)
3481 {
3482 case POSTINCREMENT_EXPR:
3483 case POSTDECREMENT_EXPR:
3484 /* Look for an `operator++ (int)'. If they didn't have
3485 one, then we fall back to the old way of doing things. */
3486 if (flags & LOOKUP_COMPLAIN)
33bd39a2 3487 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
596ea4e5
AS
3488 fnname,
3489 operator_name_info[code].name);
c73964b2
MS
3490 if (code == POSTINCREMENT_EXPR)
3491 code = PREINCREMENT_EXPR;
3492 else
3493 code = PREDECREMENT_EXPR;
3494 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3495
3496 /* The caller will deal with these. */
3497 case ADDR_EXPR:
3498 case COMPOUND_EXPR:
3499 case COMPONENT_REF:
3500 return NULL_TREE;
7f85441b
KG
3501
3502 default:
3503 break;
c73964b2
MS
3504 }
3505 if (flags & LOOKUP_COMPLAIN)
3506 {
3507 op_error (code, code2, arg1, arg2, arg3, "no match");
3508 print_z_candidates (candidates);
3509 }
3510 return error_mark_node;
3511 }
c73964b2 3512
436f8a4c 3513 cand = tourney (candidates);
c73964b2
MS
3514 if (cand == 0)
3515 {
3516 if (flags & LOOKUP_COMPLAIN)
3517 {
3518 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3519 print_z_candidates (candidates);
3520 }
3521 return error_mark_node;
3522 }
3523
3524 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3525 {
c73964b2 3526 if (warn_synth
596ea4e5 3527 && fnname == ansi_assopname (NOP_EXPR)
c73964b2
MS
3528 && DECL_ARTIFICIAL (cand->fn)
3529 && candidates->next
3530 && ! candidates->next->next)
3531 {
33bd39a2 3532 warning ("using synthesized `%#D' for copy assignment",
c73964b2 3533 cand->fn);
8251199e 3534 cp_warning_at (" where cfront would use `%#D'",
c73964b2
MS
3535 cand == candidates
3536 ? candidates->next->fn
3537 : candidates->fn);
3538 }
3539
b80f8ef3 3540 return build_over_call (cand, LOOKUP_NORMAL);
c73964b2
MS
3541 }
3542
d11ad92e
MS
3543 /* Check for comparison of different enum types. */
3544 switch (code)
3545 {
3546 case GT_EXPR:
3547 case LT_EXPR:
3548 case GE_EXPR:
3549 case LE_EXPR:
3550 case EQ_EXPR:
3551 case NE_EXPR:
84663f74 3552 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
d11ad92e
MS
3553 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3554 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3555 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3556 {
33bd39a2 3557 warning ("comparison between `%#T' and `%#T'",
d11ad92e
MS
3558 TREE_TYPE (arg1), TREE_TYPE (arg2));
3559 }
7f85441b
KG
3560 break;
3561 default:
3562 break;
d11ad92e
MS
3563 }
3564
7e3af374
JM
3565 /* We need to strip any leading REF_BIND so that bitfields don't cause
3566 errors. This should not remove any important conversions, because
3567 builtins don't apply to class objects directly. */
3568 conv = TREE_VEC_ELT (cand->convs, 0);
3569 if (TREE_CODE (conv) == REF_BIND)
3570 conv = TREE_OPERAND (conv, 0);
3571 arg1 = convert_like (conv, arg1);
c73964b2 3572 if (arg2)
8be0a930
NS
3573 {
3574 conv = TREE_VEC_ELT (cand->convs, 1);
3575 if (TREE_CODE (conv) == REF_BIND)
3576 conv = TREE_OPERAND (conv, 0);
3577 arg2 = convert_like (conv, arg2);
3578 }
c73964b2 3579 if (arg3)
8be0a930
NS
3580 {
3581 conv = TREE_VEC_ELT (cand->convs, 2);
3582 if (TREE_CODE (conv) == REF_BIND)
3583 conv = TREE_OPERAND (conv, 0);
3584 arg3 = convert_like (conv, arg3);
3585 }
c73964b2
MS
3586
3587builtin:
3588 switch (code)
3589 {
3590 case MODIFY_EXPR:
3591 return build_modify_expr (arg1, code2, arg2);
3592
3593 case INDIRECT_REF:
3594 return build_indirect_ref (arg1, "unary *");
3595
3596 case PLUS_EXPR:
3597 case MINUS_EXPR:
3598 case MULT_EXPR:
3599 case TRUNC_DIV_EXPR:
3600 case GT_EXPR:
3601 case LT_EXPR:
3602 case GE_EXPR:
3603 case LE_EXPR:
3604 case EQ_EXPR:
3605 case NE_EXPR:
3606 case MAX_EXPR:
3607 case MIN_EXPR:
3608 case LSHIFT_EXPR:
3609 case RSHIFT_EXPR:
3610 case TRUNC_MOD_EXPR:
3611 case BIT_AND_EXPR:
3612 case BIT_IOR_EXPR:
3613 case BIT_XOR_EXPR:
3614 case TRUTH_ANDIF_EXPR:
3615 case TRUTH_ORIF_EXPR:
ab76ca54 3616 return cp_build_binary_op (code, arg1, arg2);
c73964b2
MS
3617
3618 case CONVERT_EXPR:
3619 case NEGATE_EXPR:
3620 case BIT_NOT_EXPR:
3621 case TRUTH_NOT_EXPR:
3622 case PREINCREMENT_EXPR:
3623 case POSTINCREMENT_EXPR:
3624 case PREDECREMENT_EXPR:
3625 case POSTDECREMENT_EXPR:
37c46b43
MS
3626 case REALPART_EXPR:
3627 case IMAGPART_EXPR:
c73964b2
MS
3628 return build_unary_op (code, arg1, candidates != 0);
3629
3630 case ARRAY_REF:
3631 return build_array_ref (arg1, arg2);
3632
3633 case COND_EXPR:
3634 return build_conditional_expr (arg1, arg2, arg3);
3635
3636 case MEMBER_REF:
3637 return build_m_component_ref
3e411c3f 3638 (build_indirect_ref (arg1, NULL), arg2);
c73964b2
MS
3639
3640 /* The caller will deal with these. */
3641 case ADDR_EXPR:
3642 case COMPONENT_REF:
3643 case COMPOUND_EXPR:
3644 return NULL_TREE;
3645
3646 default:
a98facb0 3647 abort ();
a703fb38 3648 return NULL_TREE;
c73964b2
MS
3649 }
3650}
3651
da4768fe
JM
3652/* Build a call to operator delete. This has to be handled very specially,
3653 because the restrictions on what signatures match are different from all
3654 other call instances. For a normal delete, only a delete taking (void *)
3655 or (void *, size_t) is accepted. For a placement delete, only an exact
3656 match with the placement new is accepted.
3657
3658 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
0ac7f923 3659 ADDR is the pointer to be deleted.
da4768fe 3660 SIZE is the size of the memory block to be deleted.
519ebd1e 3661 FLAGS are the usual overloading flags.
3f41ffd8 3662 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
da4768fe
JM
3663
3664tree
94be8403
GDR
3665build_op_delete_call (enum tree_code code, tree addr, tree size,
3666 int flags, tree placement)
da4768fe 3667{
ae0ed63a 3668 tree fn = NULL_TREE;
8f4b394d 3669 tree fns, fnname, argtypes, args, type;
52682a1b 3670 int pass;
da4768fe
JM
3671
3672 if (addr == error_mark_node)
3673 return error_mark_node;
3674
8d4ce389 3675 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
c3e899c1 3676
596ea4e5 3677 fnname = ansi_opname (code);
da4768fe
JM
3678
3679 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
734e8cc5
MM
3680 /* In [class.free]
3681
3682 If the result of the lookup is ambiguous or inaccessible, or if
3683 the lookup selects a placement deallocation function, the
3684 program is ill-formed.
3685
cd0be382 3686 Therefore, we ask lookup_fnfields to complain about ambiguity. */
734e8cc5
MM
3687 {
3688 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3689 if (fns == error_mark_node)
3690 return error_mark_node;
3691 }
da4768fe
JM
3692 else
3693 fns = NULL_TREE;
3694
519ebd1e 3695 if (fns == NULL_TREE)
da4768fe
JM
3696 fns = lookup_name_nonclass (fnname);
3697
da4768fe
JM
3698 if (placement)
3699 {
cd4e8331
MM
3700 tree alloc_fn;
3701 tree call_expr;
3702
00a17e31 3703 /* Find the allocation function that is being called. */
cd4e8331 3704 call_expr = placement;
519ebd1e 3705 /* Extract the function. */
cd4e8331
MM
3706 alloc_fn = get_callee_fndecl (call_expr);
3707 my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
519ebd1e 3708 /* Then the second parm type. */
cd4e8331 3709 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
519ebd1e 3710 /* Also the second argument. */
cd4e8331 3711 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
da4768fe
JM
3712 }
3713 else
3714 {
3715 /* First try it without the size argument. */
3716 argtypes = void_list_node;
3717 args = NULL_TREE;
3718 }
3719
da4768fe 3720 /* Strip const and volatile from addr. */
c3e899c1 3721 addr = cp_convert (ptr_type_node, addr);
da4768fe 3722
52682a1b 3723 /* We make two tries at finding a matching `operator delete'. On
8d4ce389 3724 the first pass, we look for a one-operator (or placement)
52682a1b
MM
3725 operator delete. If we're not doing placement delete, then on
3726 the second pass we look for a two-argument delete. */
3727 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
da4768fe 3728 {
3f41ffd8
MM
3729 /* Go through the `operator delete' functions looking for one
3730 with a matching type. */
da15dae6 3731 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3f41ffd8
MM
3732 fn;
3733 fn = OVL_NEXT (fn))
52682a1b 3734 {
3f41ffd8
MM
3735 tree t;
3736
8f4b394d
MM
3737 /* The first argument must be "void *". */
3738 t = TYPE_ARG_TYPES (TREE_TYPE (OVL_CURRENT (fn)));
3739 if (!same_type_p (TREE_VALUE (t), ptr_type_node))
3740 continue;
3741 t = TREE_CHAIN (t);
3742 /* On the first pass, check the rest of the arguments. */
3743 if (pass == 0)
3744 {
3745 while (argtypes && t)
3746 {
3747 if (!same_type_p (TREE_VALUE (argtypes),
3748 TREE_VALUE (t)))
3749 break;
3750 argtypes = TREE_CHAIN (argtypes);
3751 t = TREE_CHAIN (t);
3752 }
3753 if (!argtypes && !t)
3754 break;
3755 }
3756 /* On the second pass, the second argument must be
3757 "size_t". */
3758 else if (pass == 1
3759 && same_type_p (TREE_VALUE (t), sizetype)
3760 && TREE_CHAIN (t) == void_list_node)
3f41ffd8 3761 break;
52682a1b 3762 }
3f41ffd8
MM
3763
3764 /* If we found a match, we're done. */
3765 if (fn)
3766 break;
3767 }
3768
3769 /* If we have a matching function, call it. */
3770 if (fn)
3771 {
3772 /* Make sure we have the actual function, and not an
3773 OVERLOAD. */
3774 fn = OVL_CURRENT (fn);
3775
3776 /* If the FN is a member function, make sure that it is
3777 accessible. */
3778 if (DECL_CLASS_SCOPE_P (fn))
6df5158a 3779 perform_or_defer_access_check (TYPE_BINFO (type), fn);
3f41ffd8
MM
3780
3781 if (pass == 0)
3782 args = tree_cons (NULL_TREE, addr, args);
3783 else
3784 args = tree_cons (NULL_TREE, addr,
3785 build_tree_list (NULL_TREE, size));
3786
a6111661
JM
3787 if (placement)
3788 {
3789 /* The placement args might not be suitable for overload
3790 resolution at this point, so build the call directly. */
3791 mark_used (fn);
3792 return build_cxx_call (fn, args, args);
3793 }
3794 else
3795 return build_function_call (fn, args);
519ebd1e
JM
3796 }
3797
52682a1b
MM
3798 /* If we are doing placement delete we do nothing if we don't find a
3799 matching op delete. */
3800 if (placement)
519ebd1e 3801 return NULL_TREE;
da4768fe 3802
8d4ce389
NS
3803 error ("no suitable `operator %s' for `%T'",
3804 operator_name_info[(int)code].name, type);
da4768fe
JM
3805 return error_mark_node;
3806}
3807
38afd588 3808/* If the current scope isn't allowed to access DECL along
d6479fe7
MM
3809 BASETYPE_PATH, give an error. The most derived class in
3810 BASETYPE_PATH is the one used to qualify DECL. */
da4768fe 3811
94be8403
GDR
3812bool
3813enforce_access (tree basetype_path, tree decl)
c73964b2 3814{
6df5158a
NS
3815 my_friendly_assert (TREE_CODE (basetype_path) == TREE_VEC, 20030624);
3816
94be8403 3817 if (!accessible_p (basetype_path, decl))
c73964b2 3818 {
d6479fe7
MM
3819 if (TREE_PRIVATE (decl))
3820 cp_error_at ("`%+#D' is private", decl);
3821 else if (TREE_PROTECTED (decl))
3822 cp_error_at ("`%+#D' is protected", decl);
3823 else
3824 cp_error_at ("`%+#D' is inaccessible", decl);
33bd39a2 3825 error ("within this context");
94be8403 3826 return false;
c73964b2 3827 }
d6479fe7 3828
94be8403 3829 return true;
c73964b2
MS
3830}
3831
4f8163b1
MM
3832/* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
3833 bitwise or of LOOKUP_* values. If any errors are warnings are
3834 generated, set *DIAGNOSTIC_FN to "error" or "warning",
3835 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
3836 to NULL. */
3837
3838static tree
3839build_temp (tree expr, tree type, int flags,
3840 void (**diagnostic_fn)(const char *, ...))
3841{
3842 int savew, savee;
3843
3844 savew = warningcount, savee = errorcount;
3845 expr = build_special_member_call (NULL_TREE,
3846 complete_ctor_identifier,
3847 build_tree_list (NULL_TREE, expr),
3848 TYPE_BINFO (type),
3849 flags);
3850 if (warningcount > savew)
3851 *diagnostic_fn = warning;
3852 else if (errorcount > savee)
3853 *diagnostic_fn = error;
3854 else
3855 *diagnostic_fn = NULL;
3856 return expr;
3857}
3858
3859
3fe18f1d
MM
3860/* Perform the conversions in CONVS on the expression EXPR. FN and
3861 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
838dfd8a 3862 indicates the `this' argument of a method. INNER is nonzero when
78fe06c2 3863 being called to continue a conversion chain. It is negative when a
3fe18f1d
MM
3864 reference binding will be applied, positive otherwise. If
3865 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
3866 conversions will be emitted if appropriate. */
c73964b2
MS
3867
3868static tree
3fe18f1d
MM
3869convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner,
3870 bool issue_conversion_warnings)
c73964b2 3871{
5e818b93 3872 tree totype = TREE_TYPE (convs);
4f8163b1 3873 void (*diagnostic_fn)(const char *, ...);
5e818b93 3874
d2e5ee5c
MS
3875 if (ICS_BAD_FLAG (convs)
3876 && TREE_CODE (convs) != USER_CONV
27b8d0cd
MM
3877 && TREE_CODE (convs) != AMBIG_CONV
3878 && TREE_CODE (convs) != REF_BIND)
d11ad92e
MS
3879 {
3880 tree t = convs;
3881 for (; t; t = TREE_OPERAND (t, 0))
3882 {
8a2b77e7 3883 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
d11ad92e 3884 {
3fe18f1d
MM
3885 expr = convert_like_real (t, expr, fn, argnum, 1,
3886 /*issue_conversion_warnings=*/false);
d11ad92e
MS
3887 break;
3888 }
3889 else if (TREE_CODE (t) == AMBIG_CONV)
3fe18f1d
MM
3890 return convert_like_real (t, expr, fn, argnum, 1,
3891 /*issue_conversion_warnings=*/false);
d11ad92e
MS
3892 else if (TREE_CODE (t) == IDENTITY_CONV)
3893 break;
3894 }
33bd39a2 3895 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
72a08131 3896 if (fn)
33bd39a2 3897 pedwarn (" initializing argument %P of `%D'", argnum, fn);
72a08131 3898 return cp_convert (totype, expr);
d11ad92e 3899 }
c3f08228 3900
3fe18f1d 3901 if (issue_conversion_warnings)
c3f08228 3902 expr = dubious_conversion_warnings
f583e452 3903 (totype, expr, "converting", fn, argnum);
c73964b2
MS
3904 switch (TREE_CODE (convs))
3905 {
3906 case USER_CONV:
3907 {
7993382e 3908 struct z_candidate *cand = USER_CONV_CAND (convs);
5e818b93 3909 tree convfn = cand->fn;
c73964b2 3910 tree args;
c73964b2 3911
5e818b93 3912 if (DECL_CONSTRUCTOR_P (convfn))
c73964b2
MS
3913 {
3914 tree t = build_int_2 (0, 0);
5e818b93 3915 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
c73964b2 3916
051e6fd7 3917 args = build_tree_list (NULL_TREE, expr);
e0fff4b3
JM
3918 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3919 || DECL_HAS_VTT_PARM_P (convfn))
3920 /* We should never try to call the abstract or base constructor
3921 from here. */
3922 abort ();
e1b3e07d 3923 args = tree_cons (NULL_TREE, t, args);
c73964b2
MS
3924 }
3925 else
3926 args = build_this (expr);
b80f8ef3 3927 expr = build_over_call (cand, LOOKUP_NORMAL);
c73964b2
MS
3928
3929 /* If this is a constructor or a function returning an aggr type,
3930 we need to build up a TARGET_EXPR. */
5e818b93
JM
3931 if (DECL_CONSTRUCTOR_P (convfn))
3932 expr = build_cplus_new (totype, expr);
3933
3934 /* The result of the call is then used to direct-initialize the object
3935 that is the destination of the copy-initialization. [dcl.init]
3936
3937 Note that this step is not reflected in the conversion sequence;
3938 it affects the semantics when we actually perform the
3939 conversion, but is not considered during overload resolution.
c73964b2 3940
5e818b93 3941 If the target is a class, that means call a ctor. */
1b6bfcd2
MM
3942 if (IS_AGGR_TYPE (totype)
3943 && (inner >= 0 || !lvalue_p (expr)))
5e818b93 3944 {
4f8163b1
MM
3945 expr = (build_temp
3946 (expr, totype,
3947 /* Core issue 84, now a DR, says that we don't
3948 allow UDCs for these args (which deliberately
3949 breaks copy-init of an auto_ptr<Base> from an
3950 auto_ptr<Derived>). */
3951 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION,
3952 &diagnostic_fn));
3953
3954 if (diagnostic_fn)
5e818b93 3955 {
4f8163b1
MM
3956 if (fn)
3957 diagnostic_fn
5e818b93
JM
3958 (" initializing argument %P of `%D' from result of `%D'",
3959 argnum, fn, convfn);
4f8163b1
MM
3960 else
3961 diagnostic_fn
3962 (" initializing temporary from result of `%D'", convfn);
5e818b93
JM
3963 }
3964 expr = build_cplus_new (totype, expr);
3965 }
c73964b2
MS
3966 return expr;
3967 }
3968 case IDENTITY_CONV:
3969 if (type_unknown_p (expr))
c2ea3a40 3970 expr = instantiate_type (totype, expr, tf_error | tf_warning);
4e8dca1c
JM
3971 /* Convert a non-array constant variable to its underlying value, unless we
3972 are about to bind it to a reference, in which case we need to
3973 leave it as an lvalue. */
3974 if (inner >= 0
3975 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3976 expr = decl_constant_value (expr);
4f8163b1
MM
3977 if (CHECK_COPY_CONSTRUCTOR_P (convs))
3978 /* Generate a temporary copy purely to generate the required
3979 diagnostics. */
3980 build_temp (build_dummy_object (totype), totype,
3981 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
3982 &diagnostic_fn);
3983 return expr;
c73964b2
MS
3984 case AMBIG_CONV:
3985 /* Call build_user_type_conversion again for the error. */
3986 return build_user_type_conversion
5e818b93 3987 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
7f85441b
KG
3988
3989 default:
3990 break;
c73964b2
MS
3991 };
3992
78fe06c2 3993 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
3fe18f1d
MM
3994 TREE_CODE (convs) == REF_BIND ? -1 : 1,
3995 /*issue_conversion_warnings=*/false);
c73964b2
MS
3996 if (expr == error_mark_node)
3997 return error_mark_node;
3998
3999 switch (TREE_CODE (convs))
4000 {
c73964b2 4001 case RVALUE_CONV:
5e818b93 4002 if (! IS_AGGR_TYPE (totype))
de22184b 4003 return expr;
f4f206f4 4004 /* Else fall through. */
de22184b 4005 case BASE_CONV:
4f0aa416
MM
4006 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
4007 {
4008 /* We are going to bind a reference directly to a base-class
4009 subobject of EXPR. */
4f8163b1
MM
4010 if (CHECK_COPY_CONSTRUCTOR_P (convs))
4011 /* Generate a temporary copy purely to generate the required
4012 diagnostics. */
4013 build_temp (build_dummy_object (TREE_TYPE (expr)),
4014 TREE_TYPE (expr),
4015 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
4016 &diagnostic_fn);
4f0aa416
MM
4017 /* Build an expression for `*((base*) &expr)'. */
4018 expr = build_unary_op (ADDR_EXPR, expr, 0);
4f8163b1
MM
4019 expr = perform_implicit_conversion (build_pointer_type (totype),
4020 expr);
4f0aa416
MM
4021 expr = build_indirect_ref (expr, "implicit conversion");
4022 return expr;
4023 }
4024
5e818b93
JM
4025 /* Copy-initialization where the cv-unqualified version of the source
4026 type is the same class as, or a derived class of, the class of the
4027 destination [is treated as direct-initialization]. [dcl.init] */
4f8163b1
MM
4028 expr = build_temp (expr, totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
4029 &diagnostic_fn);
4030 if (diagnostic_fn && fn)
4031 diagnostic_fn (" initializing argument %P of `%D'", argnum, fn);
5e818b93 4032 return build_cplus_new (totype, expr);
41efda8f 4033
c73964b2 4034 case REF_BIND:
27b8d0cd 4035 {
5e818b93 4036 tree ref_type = totype;
27b8d0cd
MM
4037
4038 /* If necessary, create a temporary. */
d18a8251 4039 if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
27b8d0cd
MM
4040 {
4041 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
e0d1297c
NS
4042
4043 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
4044 {
4045 /* If the reference is volatile or non-const, we
4046 cannot create a temporary. */
4047 cp_lvalue_kind lvalue = real_lvalue_p (expr);
4048
4049 if (lvalue & clk_bitfield)
4050 error ("cannot bind bitfield `%E' to `%T'",
4051 expr, ref_type);
4052 else if (lvalue & clk_packed)
4053 error ("cannot bind packed field `%E' to `%T'",
4054 expr, ref_type);
4055 else
ffc76561 4056 error ("cannot bind rvalue `%E' to `%T'", expr, ref_type);
e0d1297c
NS
4057 return error_mark_node;
4058 }
c506ca22 4059 expr = build_target_expr_with_type (expr, type);
27b8d0cd
MM
4060 }
4061
4062 /* Take the address of the thing to which we will bind the
4063 reference. */
4064 expr = build_unary_op (ADDR_EXPR, expr, 1);
4065 if (expr == error_mark_node)
4066 return error_mark_node;
4067
4068 /* Convert it to a pointer to the type referred to by the
4069 reference. This will adjust the pointer if a derived to
4070 base conversion is being performed. */
4071 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4072 expr);
4073 /* Convert the pointer to the desired reference type. */
7993382e 4074 return build_nop (ref_type, expr);
27b8d0cd
MM
4075 }
4076
c73964b2
MS
4077 case LVALUE_CONV:
4078 return decay_conversion (expr);
7f85441b 4079
d9cf7c82
JM
4080 case QUAL_CONV:
4081 /* Warn about deprecated conversion if appropriate. */
5e818b93 4082 string_conv_p (totype, expr, 1);
d9cf7c82
JM
4083 break;
4084
7f85441b
KG
4085 default:
4086 break;
c73964b2 4087 }
5e818b93 4088 return ocp_convert (totype, expr, CONV_IMPLICIT,
37c46b43 4089 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
c73964b2
MS
4090}
4091
a6f86b51
JM
4092/* Build a call to __builtin_trap which can be used as an expression of
4093 type TYPE. */
1a55127d
JM
4094
4095static tree
a6f86b51 4096call_builtin_trap (tree type)
1a55127d 4097{
c003e212 4098 tree fn = IDENTIFIER_GLOBAL_VALUE (get_identifier ("__builtin_trap"));
1a55127d 4099
c003e212 4100 my_friendly_assert (fn != NULL, 20030927);
1a55127d 4101 fn = build_call (fn, NULL_TREE);
a6f86b51
JM
4102 fn = build (COMPOUND_EXPR, type, fn, error_mark_node);
4103 fn = force_target_expr (type, fn);
1a55127d
JM
4104 return fn;
4105}
4106
41efda8f 4107/* ARG is being passed to a varargs function. Perform any conversions
0a72704b 4108 required. Return the converted value. */
41efda8f
MM
4109
4110tree
94be8403 4111convert_arg_to_ellipsis (tree arg)
41efda8f 4112{
0a72704b
MM
4113 /* [expr.call]
4114
4115 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
4116 standard conversions are performed. */
4117 arg = decay_conversion (arg);
4118 /* [expr.call]
4119
4120 If the argument has integral or enumeration type that is subject
4121 to the integral promotions (_conv.prom_), or a floating point
4122 type that is subject to the floating point promotion
4123 (_conv.fpprom_), the value of the argument is converted to the
4124 promoted type before the call. */
41efda8f
MM
4125 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4126 && (TYPE_PRECISION (TREE_TYPE (arg))
4127 < TYPE_PRECISION (double_type_node)))
7b6d72fc 4128 arg = convert_to_real (double_type_node, arg);
0a72704b
MM
4129 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
4130 arg = perform_integral_promotions (arg);
41efda8f 4131
66543169
NS
4132 arg = require_complete_type (arg);
4133
5840af0f
GB
4134 if (arg != error_mark_node
4135 && !pod_type_p (TREE_TYPE (arg)))
1b4d752a 4136 {
838dfd8a 4137 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
a77a9a18 4138 here and do a bitwise copy, but now cp_expr_size will abort if we
5840af0f
GB
4139 try to do that.
4140 If the call appears in the context of a sizeof expression,
4141 there is no need to emit a warning, since the expression won't be
4142 evaluated. We keep the builtin_trap just as a safety check. */
4143 if (!skip_evaluation)
4144 warning ("cannot pass objects of non-POD type `%#T' through `...'; "
4145 "call will abort at runtime", TREE_TYPE (arg));
a6f86b51 4146 arg = call_builtin_trap (TREE_TYPE (arg));
1b4d752a
NS
4147 }
4148
41efda8f
MM
4149 return arg;
4150}
4151
356955cf
NS
4152/* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4153
4154tree
94be8403 4155build_x_va_arg (tree expr, tree type)
356955cf 4156{
ea333e1c
NS
4157 if (processing_template_decl)
4158 return build_min (VA_ARG_EXPR, type, expr);
4159
356955cf
NS
4160 type = complete_type_or_else (type, NULL_TREE);
4161
4162 if (expr == error_mark_node || !type)
4163 return error_mark_node;
4164
4165 if (! pod_type_p (type))
4166 {
838dfd8a 4167 /* Undefined behavior [expr.call] 5.2.2/7. */
a6f86b51
JM
4168 warning ("cannot receive objects of non-POD type `%#T' through `...'; \
4169call will abort at runtime",
4170 type);
4171 return call_builtin_trap (type);
356955cf
NS
4172 }
4173
4174 return build_va_arg (expr, type);
4175}
4176
ab393bf1
NB
4177/* TYPE has been given to va_arg. Apply the default conversions which
4178 would have happened when passed via ellipsis. Return the promoted
4179 type, or the passed type if there is no change. */
356955cf
NS
4180
4181tree
94be8403 4182cxx_type_promotes_to (tree type)
356955cf
NS
4183{
4184 tree promote;
ab393bf1 4185
a7e8c268
MM
4186 /* Perform the array-to-pointer and function-to-pointer
4187 conversions. */
4188 type = type_decays_to (type);
ab393bf1
NB
4189
4190 promote = type_promotes_to (type);
4191 if (same_type_p (type, promote))
4192 promote = type;
356955cf 4193
ab393bf1 4194 return promote;
356955cf
NS
4195}
4196
41efda8f 4197/* ARG is a default argument expression being passed to a parameter of
297e73d8
MM
4198 the indicated TYPE, which is a parameter to FN. Do any required
4199 conversions. Return the converted value. */
41efda8f
MM
4200
4201tree
94be8403 4202convert_default_arg (tree type, tree arg, tree fn, int parmnum)
c73964b2 4203{
a723baf1
MM
4204 /* If the ARG is an unparsed default argument expression, the
4205 conversion cannot be performed. */
96a1e32d
NS
4206 if (TREE_CODE (arg) == DEFAULT_ARG)
4207 {
a723baf1
MM
4208 error ("the default argument for parameter %d of `%D' has "
4209 "not yet been parsed",
4210 parmnum, fn);
4211 return error_mark_node;
96a1e32d
NS
4212 }
4213
297e73d8 4214 if (fn && DECL_TEMPLATE_INFO (fn))
9188c363 4215 arg = tsubst_default_argument (fn, type, arg);
297e73d8 4216
c73964b2
MS
4217 arg = break_out_target_exprs (arg);
4218
4219 if (TREE_CODE (arg) == CONSTRUCTOR)
4220 {
4221 arg = digest_init (type, arg, 0);
4222 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
c3f08228 4223 "default argument", fn, parmnum);
c73964b2
MS
4224 }
4225 else
4226 {
4227 /* This could get clobbered by the following call. */
4228 if (TREE_HAS_CONSTRUCTOR (arg))
4229 arg = copy_node (arg);
4230
4231 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
c3f08228 4232 "default argument", fn, parmnum);
8e51619a 4233 arg = convert_for_arg_passing (type, arg);
c73964b2
MS
4234 }
4235
4236 return arg;
4237}
4238
8e51619a
JM
4239/* Returns the type which will really be used for passing an argument of
4240 type TYPE. */
4241
4242tree
94be8403 4243type_passed_as (tree type)
8e51619a
JM
4244{
4245 /* Pass classes with copy ctors by invisible reference. */
4246 if (TREE_ADDRESSABLE (type))
4247 type = build_reference_type (type);
4248 else if (PROMOTE_PROTOTYPES
4249 && INTEGRAL_TYPE_P (type)
3f50d3dd 4250 && COMPLETE_TYPE_P (type)
560ad596
MM
4251 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4252 TYPE_SIZE (integer_type_node)))
8e51619a
JM
4253 type = integer_type_node;
4254
4255 return type;
4256}
4257
4258/* Actually perform the appropriate conversion. */
4259
4260tree
94be8403 4261convert_for_arg_passing (tree type, tree val)
8e51619a 4262{
c246c65d
JM
4263 if (val == error_mark_node)
4264 ;
8e51619a 4265 /* Pass classes with copy ctors by invisible reference. */
c246c65d
JM
4266 else if (TREE_ADDRESSABLE (type))
4267 val = build1 (ADDR_EXPR, build_reference_type (type), val);
8e51619a
JM
4268 else if (PROMOTE_PROTOTYPES
4269 && INTEGRAL_TYPE_P (type)
3f50d3dd 4270 && COMPLETE_TYPE_P (type)
560ad596
MM
4271 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4272 TYPE_SIZE (integer_type_node)))
0a72704b 4273 val = perform_integral_promotions (val);
8e51619a
JM
4274 return val;
4275}
4276
a6f86b51
JM
4277/* Returns true iff FN is a function with magic varargs, i.e. ones for
4278 which no conversions at all should be done. This is true for some
4279 builtins which don't act like normal functions. */
4280
4281static bool
4282magic_varargs_p (tree fn)
4283{
4284 if (DECL_BUILT_IN (fn))
4285 switch (DECL_FUNCTION_CODE (fn))
4286 {
4287 case BUILT_IN_CLASSIFY_TYPE:
4288 case BUILT_IN_CONSTANT_P:
4289 case BUILT_IN_NEXT_ARG:
4290 case BUILT_IN_STDARG_START:
4291 case BUILT_IN_VA_START:
4292 return true;
4293
4294 default:;
4295 }
4296
4297 return false;
4298}
4299
c050ec51
JM
4300/* Subroutine of the various build_*_call functions. Overload resolution
4301 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4302 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4303 bitmask of various LOOKUP_* flags which apply to the call itself. */
4304
c73964b2 4305static tree
b80f8ef3 4306build_over_call (struct z_candidate *cand, int flags)
c73964b2 4307{
5ffe581d 4308 tree fn = cand->fn;
b80f8ef3 4309 tree args = cand->args;
5ffe581d 4310 tree convs = cand->convs;
c73964b2
MS
4311 tree converted_args = NULL_TREE;
4312 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4313 tree conv, arg, val;
4314 int i = 0;
d11ad92e 4315 int is_method = 0;
c73964b2 4316
b7c707d1
MM
4317 /* In a template, there is no need to perform all of the work that
4318 is normally done. We are only interested in the type of the call
4319 expression, i.e., the return type of the function. Any semantic
4320 errors will be deferred until the template is instantiated. */
4321 if (processing_template_decl)
4322 {
4323 tree expr;
4324 tree return_type;
4325 return_type = TREE_TYPE (TREE_TYPE (fn));
4326 expr = build (CALL_EXPR, return_type, fn, args);
4327 if (!VOID_TYPE_P (return_type))
4328 require_complete_type (return_type);
4329 return convert_from_reference (expr);
4330 }
4331
5ffe581d
JM
4332 /* Give any warnings we noticed during overload resolution. */
4333 if (cand->warnings)
4334 for (val = cand->warnings; val; val = TREE_CHAIN (val))
e2500fed 4335 joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
5ffe581d
JM
4336
4337 if (DECL_FUNCTION_MEMBER_P (fn))
3dfa3500
KL
4338 {
4339 /* If FN is a template function, two cases must be considered.
4340 For example:
4341
4342 struct A {
4343 protected:
4344 template <class T> void f();
4345 };
4346 template <class T> struct B {
4347 protected:
4348 void g();
4349 };
4350 struct C : A, B<int> {
4351 using A::f; // #1
4352 using B<int>::g; // #2
4353 };
4354
4355 In case #1 where `A::f' is a member template, DECL_ACCESS is
4356 recorded in the primary template but not in its specialization.
4357 We check access of FN using its primary template.
4358
4359 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
4360 because it is a member of class template B, DECL_ACCESS is
4361 recorded in the specialization `B<int>::g'. We cannot use its
4362 primary template because `B<T>::g' and `B<int>::g' may have
4363 different access. */
4364 if (DECL_TEMPLATE_INFO (fn)
4365 && is_member_template (DECL_TI_TEMPLATE (fn)))
4366 perform_or_defer_access_check (cand->access_path,
4367 DECL_TI_TEMPLATE (fn));
4368 else
4369 perform_or_defer_access_check (cand->access_path, fn);
4370 }
5ffe581d 4371
c73964b2 4372 if (args && TREE_CODE (args) != TREE_LIST)
051e6fd7 4373 args = build_tree_list (NULL_TREE, args);
c73964b2
MS
4374 arg = args;
4375
4376 /* The implicit parameters to a constructor are not considered by overload
4377 resolution, and must be of the proper type. */
4378 if (DECL_CONSTRUCTOR_P (fn))
4379 {
e1b3e07d 4380 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
c73964b2
MS
4381 arg = TREE_CHAIN (arg);
4382 parm = TREE_CHAIN (parm);
454fa7a7 4383 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
e0fff4b3
JM
4384 /* We should never try to call the abstract constructor. */
4385 abort ();
4386 if (DECL_HAS_VTT_PARM_P (fn))
c73964b2 4387 {
e1b3e07d 4388 converted_args = tree_cons
c73964b2
MS
4389 (NULL_TREE, TREE_VALUE (arg), converted_args);
4390 arg = TREE_CHAIN (arg);
4391 parm = TREE_CHAIN (parm);
4392 }
4393 }
4394 /* Bypass access control for 'this' parameter. */
4395 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4396 {
d11ad92e
MS
4397 tree parmtype = TREE_VALUE (parm);
4398 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4ba126e4 4399 tree converted_arg;
3baab484
NS
4400 tree base_binfo;
4401
d11ad92e 4402 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
33bd39a2 4403 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
2642b9bf 4404 TREE_TYPE (argtype), fn);
91063b51 4405
51ddb82e
JM
4406 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4407 X is called for an object that is not of type X, or of a type
4408 derived from X, the behavior is undefined.
4409
4410 So we can assume that anything passed as 'this' is non-null, and
4411 optimize accordingly. */
6eabb241 4412 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4ba126e4
MM
4413 /* Convert to the base in which the function was declared. */
4414 my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4415 converted_arg = build_base_path (PLUS_EXPR,
4416 TREE_VALUE (arg),
4417 cand->conversion_path,
4418 1);
bd16cb25
MM
4419 /* Check that the base class is accessible. */
4420 if (!accessible_base_p (TREE_TYPE (argtype),
4421 BINFO_TYPE (cand->conversion_path)))
4422 error ("`%T' is not an accessible base of `%T'",
4423 BINFO_TYPE (cand->conversion_path),
4424 TREE_TYPE (argtype));
3baab484
NS
4425 /* If fn was found by a using declaration, the conversion path
4426 will be to the derived class, not the base declaring fn. We
4427 must convert from derived to base. */
4428 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4429 TREE_TYPE (parmtype), ba_ignore, NULL);
3baab484
NS
4430 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4431 base_binfo, 1);
4432
4ba126e4 4433 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
c73964b2
MS
4434 parm = TREE_CHAIN (parm);
4435 arg = TREE_CHAIN (arg);
4436 ++i;
d11ad92e 4437 is_method = 1;
c73964b2
MS
4438 }
4439
eb66be0e 4440 for (; arg && parm;
c73964b2
MS
4441 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4442 {
4443 tree type = TREE_VALUE (parm);
d11ad92e 4444
eb66be0e 4445 conv = TREE_VEC_ELT (convs, i);
72a08131
JM
4446 val = convert_like_with_context
4447 (conv, TREE_VALUE (arg), fn, i - is_method);
c73964b2 4448
8e51619a 4449 val = convert_for_arg_passing (type, val);
e1b3e07d 4450 converted_args = tree_cons (NULL_TREE, val, converted_args);
c73964b2
MS
4451 }
4452
4453 /* Default arguments */
c3f08228 4454 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
297e73d8 4455 converted_args
e1b3e07d
MM
4456 = tree_cons (NULL_TREE,
4457 convert_default_arg (TREE_VALUE (parm),
4458 TREE_PURPOSE (parm),
c3f08228 4459 fn, i - is_method),
e1b3e07d 4460 converted_args);
c73964b2
MS
4461
4462 /* Ellipsis */
4463 for (; arg; arg = TREE_CHAIN (arg))
a6f86b51
JM
4464 {
4465 tree a = TREE_VALUE (arg);
4466 if (magic_varargs_p (fn))
4467 /* Do no conversions for magic varargs. */;
4468 else
4469 a = convert_arg_to_ellipsis (a);
4470 converted_args = tree_cons (NULL_TREE, a, converted_args);
4471 }
c73964b2
MS
4472
4473 converted_args = nreverse (converted_args);
4474
80a497e4
JM
4475 if (warn_format)
4476 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4477 converted_args);
61cd552e 4478
c11b6f21
MS
4479 /* Avoid actually calling copy constructors and copy assignment operators,
4480 if possible. */
56ae6d77
JM
4481
4482 if (! flag_elide_constructors)
4483 /* Do things the hard way. */;
454fa7a7
MM
4484 else if (TREE_VEC_LENGTH (convs) == 1
4485 && DECL_COPY_CONSTRUCTOR_P (fn))
c11b6f21 4486 {
eb66be0e 4487 tree targ;
e0fff4b3 4488 arg = skip_artificial_parms_for (fn, converted_args);
67437d5b 4489 arg = TREE_VALUE (arg);
c11b6f21
MS
4490
4491 /* Pull out the real argument, disregarding const-correctness. */
eb66be0e
MS
4492 targ = arg;
4493 while (TREE_CODE (targ) == NOP_EXPR
4494 || TREE_CODE (targ) == NON_LVALUE_EXPR
4495 || TREE_CODE (targ) == CONVERT_EXPR)
4496 targ = TREE_OPERAND (targ, 0);
4497 if (TREE_CODE (targ) == ADDR_EXPR)
4498 {
4499 targ = TREE_OPERAND (targ, 0);
9edc3913
MM
4500 if (!same_type_ignoring_top_level_qualifiers_p
4501 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
c11b6f21
MS
4502 targ = NULL_TREE;
4503 }
eb66be0e
MS
4504 else
4505 targ = NULL_TREE;
c11b6f21
MS
4506
4507 if (targ)
4508 arg = targ;
4509 else
4510 arg = build_indirect_ref (arg, 0);
4511
bd6dd845
MS
4512 /* [class.copy]: the copy constructor is implicitly defined even if
4513 the implementation elided its use. */
4514 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4515 mark_used (fn);
4516
c11b6f21
MS
4517 /* If we're creating a temp and we already have one, don't create a
4518 new one. If we're not creating a temp but we get one, use
4519 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4520 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4521 temp or an INIT_EXPR otherwise. */
4522 if (integer_zerop (TREE_VALUE (args)))
4523 {
c246c65d 4524 if (TREE_CODE (arg) == TARGET_EXPR)
c11b6f21
MS
4525 return arg;
4526 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
c506ca22 4527 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
c11b6f21 4528 }
c246c65d 4529 else if (TREE_CODE (arg) == TARGET_EXPR
a77a9a18 4530 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
c11b6f21 4531 {
c1aa4de7 4532 tree address;
c11b6f21
MS
4533 tree to = stabilize_reference
4534 (build_indirect_ref (TREE_VALUE (args), 0));
a59ca936 4535
0830ae44 4536 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
c1aa4de7
MM
4537 address = build_unary_op (ADDR_EXPR, val, 0);
4538 /* Avoid a warning about this expression, if the address is
4539 never used. */
4540 TREE_USED (address) = 1;
4541 return address;
c11b6f21
MS
4542 }
4543 }
596ea4e5 4544 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
271e6f02 4545 && copy_fn_p (fn)
4f1c5b7d 4546 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
c11b6f21
MS
4547 {
4548 tree to = stabilize_reference
4549 (build_indirect_ref (TREE_VALUE (converted_args), 0));
a0c68737
NS
4550 tree type = TREE_TYPE (to);
4551 tree as_base = CLASSTYPE_AS_BASE (type);
a59ca936 4552
959d8796 4553 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
a0c68737
NS
4554 if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
4555 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4556 else
4557 {
4558 /* We must only copy the non-tail padding parts. Use
4559 CLASSTYPE_AS_BASE for the bitwise copy. */
88401f73
RH
4560 tree to_ptr, arg_ptr, to_as_base, arg_as_base, base_ptr_type;
4561 tree save_to;
4562
4563 to_ptr = save_expr (build_unary_op (ADDR_EXPR, to, 0));
4564 arg_ptr = build_unary_op (ADDR_EXPR, arg, 0);
a0c68737 4565
a0c68737 4566 base_ptr_type = build_pointer_type (as_base);
88401f73
RH
4567 to_as_base = build_nop (base_ptr_type, to_ptr);
4568 to_as_base = build_indirect_ref (to_as_base, 0);
4569 arg_as_base = build_nop (base_ptr_type, arg_ptr);
4570 arg_as_base = build_indirect_ref (arg_as_base, 0);
4571
4572 save_to = build_indirect_ref (to_ptr, 0);
4573
a0c68737 4574 val = build (MODIFY_EXPR, as_base, to_as_base, arg_as_base);
88401f73
RH
4575 val = convert_to_void (val, NULL);
4576 val = build (COMPOUND_EXPR, type, val, save_to);
4577 TREE_NO_UNUSED_WARNING (val) = 1;
a0c68737
NS
4578 }
4579
c11b6f21
MS
4580 return val;
4581 }
4582
bd6dd845
MS
4583 mark_used (fn);
4584
6eabb241 4585 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
c73964b2
MS
4586 {
4587 tree t, *p = &TREE_VALUE (converted_args);
338d90b8 4588 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
e93ee644 4589 DECL_CONTEXT (fn),
338d90b8
NS
4590 ba_any, NULL);
4591 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4592
4593 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
c73964b2
MS
4594 if (TREE_SIDE_EFFECTS (*p))
4595 *p = save_expr (*p);
4596 t = build_pointer_type (TREE_TYPE (fn));
60c87482
BM
4597 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4598 fn = build_java_interface_fn_ref (fn, *p);
4599 else
67231816 4600 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
c73964b2
MS
4601 TREE_TYPE (fn) = t;
4602 }
4603 else if (DECL_INLINE (fn))
4604 fn = inline_conversion (fn);
4605 else
4606 fn = build_addr_func (fn);
4607
b2dd096b
MM
4608 return build_cxx_call (fn, args, converted_args);
4609}
4610
4611/* Build and return a call to FN, using the the CONVERTED_ARGS. ARGS
4612 gives the original form of the arguments. This function performs
4613 no overload resolution, conversion, or other high-level
4614 operations. */
4615
4616tree
4617build_cxx_call(tree fn, tree args, tree converted_args)
4618{
4619 tree fndecl;
4620
387c47a3
AS
4621 /* Recognize certain built-in functions so we can make tree-codes
4622 other than CALL_EXPR. We do this when it enables fold-const.c
4623 to do something useful. */
387c47a3
AS
4624 if (TREE_CODE (fn) == ADDR_EXPR
4625 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
1eb8759b
RH
4626 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4627 {
4628 tree exp;
4629 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4630 if (exp)
4631 return exp;
4632 }
387c47a3 4633
b2dd096b
MM
4634 fn = build_call (fn, converted_args);
4635
4636 /* If this call might throw an exception, note that fact. */
4637 fndecl = get_callee_fndecl (fn);
374ca7f7
MM
4638 if ((!fndecl || !TREE_NOTHROW (fndecl))
4639 && at_function_scope_p ()
4640 && cfun)
b2dd096b
MM
4641 cp_function_chain->can_throw = 1;
4642
4643 /* Some built-in function calls will be evaluated at compile-time in
4644 fold (). */
4645 fn = fold (fn);
4646
2c92b94d 4647 if (VOID_TYPE_P (TREE_TYPE (fn)))
c73964b2 4648 return fn;
b2dd096b 4649
b82b76c6 4650 fn = require_complete_type (fn);
2c92b94d
NS
4651 if (fn == error_mark_node)
4652 return error_mark_node;
b2dd096b 4653
c73964b2
MS
4654 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4655 fn = build_cplus_new (TREE_TYPE (fn), fn);
b82b76c6 4656 return convert_from_reference (fn);
c73964b2
MS
4657}
4658
e2500fed 4659static GTY(()) tree java_iface_lookup_fn;
60c87482
BM
4660
4661/* Make an expression which yields the address of the Java interface
4662 method FN. This is achieved by generating a call to libjava's
4663 _Jv_LookupInterfaceMethodIdx(). */
4664
4665static tree
94be8403 4666build_java_interface_fn_ref (tree fn, tree instance)
60c87482
BM
4667{
4668 tree lookup_args, lookup_fn, method, idx;
4669 tree klass_ref, iface, iface_ref;
4670 int i;
4671
4672 if (!java_iface_lookup_fn)
4673 {
4674 tree endlink = build_void_list_node ();
4675 tree t = tree_cons (NULL_TREE, ptr_type_node,
4676 tree_cons (NULL_TREE, ptr_type_node,
4677 tree_cons (NULL_TREE, java_int_type_node,
4678 endlink)));
4679 java_iface_lookup_fn
4680 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4681 build_function_type (ptr_type_node, t),
6a2dd09a 4682 0, NOT_BUILT_IN, NULL, NULL_TREE);
60c87482
BM
4683 }
4684
4685 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
00a17e31 4686 This is the first entry in the vtable. */
60c87482
BM
4687 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4688 integer_zero_node);
4689
00a17e31 4690 /* Get the java.lang.Class pointer for the interface being called. */
60c87482 4691 iface = DECL_CONTEXT (fn);
86ac0575 4692 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
60c87482
BM
4693 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4694 || DECL_CONTEXT (iface_ref) != iface)
4695 {
33bd39a2 4696 error ("could not find class$ field in java interface type `%T'",
60c87482
BM
4697 iface);
4698 return error_mark_node;
4699 }
4700 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4701
00a17e31 4702 /* Determine the itable index of FN. */
60c87482
BM
4703 i = 1;
4704 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4705 {
4706 if (!DECL_VIRTUAL_P (method))
4707 continue;
4708 if (fn == method)
4709 break;
4710 i++;
4711 }
4712 idx = build_int_2 (i, 0);
4713
4714 lookup_args = tree_cons (NULL_TREE, klass_ref,
4715 tree_cons (NULL_TREE, iface_ref,
4716 build_tree_list (NULL_TREE, idx)));
4717 lookup_fn = build1 (ADDR_EXPR,
4718 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4719 java_iface_lookup_fn);
4720 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4721}
4722
298d6f60
MM
4723/* Returns the value to use for the in-charge parameter when making a
4724 call to a function with the indicated NAME. */
4725
4726tree
94be8403 4727in_charge_arg_for_name (tree name)
298d6f60
MM
4728{
4729 if (name == base_ctor_identifier
4730 || name == base_dtor_identifier)
4731 return integer_zero_node;
4732 else if (name == complete_ctor_identifier)
4733 return integer_one_node;
4734 else if (name == complete_dtor_identifier)
4735 return integer_two_node;
4736 else if (name == deleting_dtor_identifier)
4737 return integer_three_node;
4738
4739 /* This function should only be called with one of the names listed
4740 above. */
a98facb0 4741 abort ();
298d6f60
MM
4742 return NULL_TREE;
4743}
4744
4ba126e4
MM
4745/* Build a call to a constructor, destructor, or an assignment
4746 operator for INSTANCE, an expression with class type. NAME
4747 indicates the special member function to call; ARGS are the
4748 arguments. BINFO indicates the base of INSTANCE that is to be
4749 passed as the `this' parameter to the member function called.
4750
4751 FLAGS are the LOOKUP_* flags to use when processing the call.
4752
4753 If NAME indicates a complete object constructor, INSTANCE may be
4754 NULL_TREE. In this case, the caller will call build_cplus_new to
4755 store the newly constructed object into a VAR_DECL. */
4756
4757tree
4758build_special_member_call (tree instance, tree name, tree args,
4759 tree binfo, int flags)
4760{
4761 tree fns;
4762 /* The type of the subobject to be constructed or destroyed. */
4763 tree class_type;
4764
4765 my_friendly_assert (name == complete_ctor_identifier
4766 || name == base_ctor_identifier
4767 || name == complete_dtor_identifier
4768 || name == base_dtor_identifier
4769 || name == deleting_dtor_identifier
4770 || name == ansi_assopname (NOP_EXPR),
4771 20020712);
4772 my_friendly_assert (binfo != NULL_TREE, 20020712);
4773
4774 class_type = BINFO_TYPE (binfo);
4775
4776 /* Handle the special case where INSTANCE is NULL_TREE. */
4777 if (name == complete_ctor_identifier && !instance)
4778 {
4779 instance = build_int_2 (0, 0);
4780 TREE_TYPE (instance) = build_pointer_type (class_type);
4781 instance = build1 (INDIRECT_REF, class_type, instance);
4782 }
22ed7e5f
MM
4783 else
4784 {
4785 if (name == complete_dtor_identifier
4786 || name == base_dtor_identifier
4787 || name == deleting_dtor_identifier)
4788 my_friendly_assert (args == NULL_TREE, 20020712);
4789
4c2a4b90 4790 /* Convert to the base class, if necessary. */
22ed7e5f
MM
4791 if (!same_type_ignoring_top_level_qualifiers_p
4792 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
4c2a4b90
MM
4793 {
4794 if (name != ansi_assopname (NOP_EXPR))
4795 /* For constructors and destructors, either the base is
4796 non-virtual, or it is virtual but we are doing the
4797 conversion from a constructor or destructor for the
4798 complete object. In either case, we can convert
4799 statically. */
4800 instance = convert_to_base_statically (instance, binfo);
4801 else
4802 /* However, for assignment operators, we must convert
4803 dynamically if the base is virtual. */
4804 instance = build_base_path (PLUS_EXPR, instance,
4805 binfo, /*nonnull=*/1);
4806 }
22ed7e5f
MM
4807 }
4808
4ba126e4
MM
4809 my_friendly_assert (instance != NULL_TREE, 20020712);
4810
4811 /* Resolve the name. */
4812 if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4813 return error_mark_node;
4814
4815 fns = lookup_fnfields (binfo, name, 1);
4816
4817 /* When making a call to a constructor or destructor for a subobject
4818 that uses virtual base classes, pass down a pointer to a VTT for
4819 the subobject. */
4820 if ((name == base_ctor_identifier
4821 || name == base_dtor_identifier)
4822 && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4823 {
4824 tree vtt;
4825 tree sub_vtt;
4826
4827 /* If the current function is a complete object constructor
4828 or destructor, then we fetch the VTT directly.
4829 Otherwise, we look it up using the VTT we were given. */
548502d3 4830 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
4ba126e4
MM
4831 vtt = decay_conversion (vtt);
4832 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4833 build (EQ_EXPR, boolean_type_node,
4834 current_in_charge_parm, integer_zero_node),
4835 current_vtt_parm,
4836 vtt);
4ba126e4
MM
4837 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4838 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4839 BINFO_SUBVTT_INDEX (binfo));
4840
4841 args = tree_cons (NULL_TREE, sub_vtt, args);
4842 }
4843
22ed7e5f
MM
4844 return build_new_method_call (instance, fns, args,
4845 TYPE_BINFO (BINFO_TYPE (binfo)),
4846 flags);
4ba126e4
MM
4847}
4848
a723baf1
MM
4849/* Return the NAME, as a C string. The NAME indicates a function that
4850 is a member of TYPE. *FREE_P is set to true if the caller must
4851 free the memory returned.
4852
4853 Rather than go through all of this, we should simply set the names
4854 of constructors and destructors appropriately, and dispense with
4855 ctor_identifier, dtor_identifier, etc. */
4856
4857static char *
4858name_as_c_string (tree name, tree type, bool *free_p)
4859{
4860 char *pretty_name;
4861
4862 /* Assume that we will not allocate memory. */
4863 *free_p = false;
4864 /* Constructors and destructors are special. */
4865 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4866 {
4867 pretty_name
4868 = (char *) IDENTIFIER_POINTER (constructor_name (type));
4869 /* For a destructor, add the '~'. */
4870 if (name == complete_dtor_identifier
4871 || name == base_dtor_identifier
4872 || name == deleting_dtor_identifier)
4873 {
4874 pretty_name = concat ("~", pretty_name, NULL);
4875 /* Remember that we need to free the memory allocated. */
4876 *free_p = true;
4877 }
4878 }
144e414d
MM
4879 else if (IDENTIFIER_TYPENAME_P (name))
4880 {
4881 pretty_name = concat ("operator ",
4882 type_as_string (TREE_TYPE (name),
4883 TFF_PLAIN_IDENTIFIER),
4884 NULL);
4885 /* Remember that we need to free the memory allocated. */
4886 *free_p = true;
4887 }
a723baf1
MM
4888 else
4889 pretty_name = (char *) IDENTIFIER_POINTER (name);
4890
4891 return pretty_name;
4892}
4893
4ba126e4
MM
4894/* Build a call to "INSTANCE.FN (ARGS)". */
4895
4896tree
4897build_new_method_call (tree instance, tree fns, tree args,
4898 tree conversion_path, int flags)
c73964b2
MS
4899{
4900 struct z_candidate *candidates = 0, *cand;
386b8a85 4901 tree explicit_targs = NULL_TREE;
4ba126e4
MM
4902 tree basetype = NULL_TREE;
4903 tree access_binfo;
4904 tree optype;
4905 tree mem_args = NULL_TREE, instance_ptr;
a723baf1 4906 tree name;
71a19881 4907 tree user_args;
3c8c2a0a 4908 tree call;
a723baf1
MM
4909 tree fn;
4910 tree class_type;
c32381b1 4911 int template_only = 0;
436f8a4c 4912 bool any_viable_p;
d17811fd
MM
4913 tree orig_instance;
4914 tree orig_fns;
4915 tree orig_args;
824b9a4c 4916
4ba126e4 4917 my_friendly_assert (instance != NULL_TREE, 20020729);
8f032717 4918
a723baf1
MM
4919 if (error_operand_p (instance)
4920 || error_operand_p (fns)
4ba126e4
MM
4921 || args == error_mark_node)
4922 return error_mark_node;
386b8a85 4923
d17811fd
MM
4924 orig_instance = instance;
4925 orig_fns = fns;
4926 orig_args = args;
4927
4928 if (processing_template_decl)
4929 {
4930 instance = build_non_dependent_expr (instance);
4931 if (!BASELINK_P (fns)
4932 && TREE_CODE (fns) != PSEUDO_DTOR_EXPR
4933 && TREE_TYPE (fns) != unknown_type_node)
4934 fns = build_non_dependent_expr (fns);
4935 args = build_non_dependent_args (orig_args);
4936 }
4937
4ba126e4 4938 /* Process the argument list. */
9eb71d8c 4939 user_args = args;
86e6f22f 4940 args = resolve_args (args);
86e6f22f
JM
4941 if (args == error_mark_node)
4942 return error_mark_node;
d11ad92e 4943
4ba126e4
MM
4944 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4945 instance = convert_from_reference (instance);
4946 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4947 instance_ptr = build_this (instance);
4948
4949 if (!BASELINK_P (fns))
c73964b2 4950 {
4ba126e4
MM
4951 error ("call to non-function `%D'", fns);
4952 return error_mark_node;
4953 }
c73964b2 4954
4ba126e4
MM
4955 if (!conversion_path)
4956 conversion_path = BASELINK_BINFO (fns);
4957 access_binfo = BASELINK_ACCESS_BINFO (fns);
4958 optype = BASELINK_OPTYPE (fns);
4959 fns = BASELINK_FUNCTIONS (fns);
c73964b2 4960
4ba126e4
MM
4961 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4962 {
4963 explicit_targs = TREE_OPERAND (fns, 1);
4964 fns = TREE_OPERAND (fns, 0);
4965 template_only = 1;
c73964b2
MS
4966 }
4967
4ba126e4
MM
4968 my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
4969 || TREE_CODE (fns) == TEMPLATE_DECL
4970 || TREE_CODE (fns) == OVERLOAD,
4971 20020712);
c73964b2 4972
4ba126e4
MM
4973 /* XXX this should be handled before we get here. */
4974 if (! IS_AGGR_TYPE (basetype))
c73964b2 4975 {
4ba126e4
MM
4976 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4977 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4978 fns, instance, basetype);
c73964b2 4979
4ba126e4 4980 return error_mark_node;
c73964b2
MS
4981 }
4982
a723baf1
MM
4983 fn = get_first_fn (fns);
4984 name = DECL_NAME (fn);
9eb71d8c 4985
298d6f60 4986 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
9eb71d8c 4987 {
4ba126e4
MM
4988 /* Callers should explicitly indicate whether they want to construct
4989 the complete object or just the part without virtual bases. */
4990 my_friendly_assert (name != ctor_identifier, 20000408);
4991 /* Similarly for destructors. */
4992 my_friendly_assert (name != dtor_identifier, 20000408);
9eb71d8c 4993 }
c73964b2 4994
a723baf1
MM
4995 /* It's OK to call destructors on cv-qualified objects. Therefore,
4996 convert the INSTANCE_PTR to the unqualified type, if necessary. */
4997 if (DECL_DESTRUCTOR_P (fn))
c73964b2 4998 {
a723baf1
MM
4999 tree type = build_pointer_type (basetype);
5000 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
7993382e 5001 instance_ptr = build_nop (type, instance_ptr);
a723baf1 5002 }
4ba126e4 5003
a723baf1
MM
5004 class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE);
5005 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
98c1c668 5006
a723baf1
MM
5007 for (fn = fns; fn; fn = OVL_NEXT (fn))
5008 {
5009 tree t = OVL_CURRENT (fn);
5010 tree this_arglist;
71a19881 5011
a723baf1
MM
5012 /* We can end up here for copy-init of same or base class. */
5013 if ((flags & LOOKUP_ONLYCONVERTING)
5014 && DECL_NONCONVERTING_P (t))
5015 continue;
98c1c668 5016
a723baf1
MM
5017 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
5018 this_arglist = mem_args;
5019 else
5020 this_arglist = args;
5021
5022 if (TREE_CODE (t) == TEMPLATE_DECL)
7993382e
MM
5023 /* A member template. */
5024 add_template_candidate (&candidates, t,
5025 class_type,
5026 explicit_targs,
5027 this_arglist, optype,
5028 access_binfo,
5029 conversion_path,
5030 flags,
5031 DEDUCE_CALL);
a723baf1 5032 else if (! template_only)
7993382e
MM
5033 add_function_candidate (&candidates, t,
5034 class_type,
5035 this_arglist,
5036 access_binfo,
5037 conversion_path,
5038 flags);
c73964b2
MS
5039 }
5040
436f8a4c
MM
5041 candidates = splice_viable (candidates, pedantic, &any_viable_p);
5042 if (!any_viable_p)
c73964b2
MS
5043 {
5044 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
5045 if (flags & LOOKUP_SPECULATIVELY)
5046 return NULL_TREE;
d0f062fb 5047 if (!COMPLETE_TYPE_P (basetype))
7a228918 5048 cxx_incomplete_type_error (instance_ptr, basetype);
c27be9b9 5049 else
a723baf1
MM
5050 {
5051 char *pretty_name;
5052 bool free_p;
5053
5054 pretty_name = name_as_c_string (name, basetype, &free_p);
5055 error ("no matching function for call to `%T::%s(%A)%#V'",
5056 basetype, pretty_name, user_args,
5057 TREE_TYPE (TREE_TYPE (instance_ptr)));
5058 if (free_p)
5059 free (pretty_name);
5060 }
c73964b2
MS
5061 print_z_candidates (candidates);
5062 return error_mark_node;
5063 }
c73964b2 5064
436f8a4c 5065 cand = tourney (candidates);
c73964b2
MS
5066 if (cand == 0)
5067 {
a723baf1
MM
5068 char *pretty_name;
5069 bool free_p;
5070
5071 pretty_name = name_as_c_string (name, basetype, &free_p);
5e8a153a 5072 error ("call of overloaded `%s(%A)' is ambiguous", pretty_name,
a723baf1 5073 user_args);
c73964b2 5074 print_z_candidates (candidates);
a723baf1
MM
5075 if (free_p)
5076 free (pretty_name);
c73964b2
MS
5077 return error_mark_node;
5078 }
5079
fee7654e 5080 if (DECL_PURE_VIRTUAL_P (cand->fn)
c73964b2 5081 && instance == current_class_ref
1cb47a5c
ML
5082 && (DECL_CONSTRUCTOR_P (current_function_decl)
5083 || DECL_DESTRUCTOR_P (current_function_decl))
c73964b2 5084 && ! (flags & LOOKUP_NONVIRTUAL)
fee7654e 5085 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
33bd39a2 5086 error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
1cb47a5c
ML
5087 "abstract virtual `%#D' called from constructor"
5088 : "abstract virtual `%#D' called from destructor"),
5089 cand->fn);
c73964b2 5090 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
51924768 5091 && is_dummy_object (instance_ptr))
8f032717 5092 {
33bd39a2 5093 error ("cannot call member function `%D' without object", cand->fn);
8f032717
MM
5094 return error_mark_node;
5095 }
c73964b2
MS
5096
5097 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
a0de9d20 5098 && resolves_to_fixed_type_p (instance, 0))
c73964b2
MS
5099 flags |= LOOKUP_NONVIRTUAL;
5100
3c8c2a0a 5101 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
b80f8ef3 5102 call = build_over_call (cand, flags);
3c8c2a0a
JM
5103 else
5104 {
b80f8ef3 5105 call = build_over_call (cand, flags);
4ba126e4
MM
5106 /* In an expression of the form `a->f()' where `f' turns out to
5107 be a static member function, `a' is none-the-less evaluated. */
9f175208 5108 if (!is_dummy_object (instance_ptr) && TREE_SIDE_EFFECTS (instance))
3c8c2a0a
JM
5109 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
5110 }
20b50672 5111
d17811fd 5112 if (processing_template_decl && call != error_mark_node)
8e1daa34
NS
5113 return build_min_non_dep
5114 (CALL_EXPR, call,
5115 build_min_nt (COMPONENT_REF, orig_instance, orig_fns),
5116 orig_args);
3c8c2a0a 5117 return call;
c73964b2
MS
5118}
5119
94be8403 5120/* Returns true iff standard conversion sequence ICS1 is a proper
ceab47eb 5121 subsequence of ICS2. */
c73964b2 5122
94be8403
GDR
5123static bool
5124is_subseq (tree ics1, tree ics2)
c73964b2 5125{
ceab47eb
MM
5126 /* We can assume that a conversion of the same code
5127 between the same types indicates a subsequence since we only get
5128 here if the types we are converting from are the same. */
549121cd 5129
ceab47eb
MM
5130 while (TREE_CODE (ics1) == RVALUE_CONV
5131 || TREE_CODE (ics1) == LVALUE_CONV)
5132 ics1 = TREE_OPERAND (ics1, 0);
c73964b2 5133
ceab47eb 5134 while (1)
c73964b2 5135 {
ceab47eb
MM
5136 while (TREE_CODE (ics2) == RVALUE_CONV
5137 || TREE_CODE (ics2) == LVALUE_CONV)
5138 ics2 = TREE_OPERAND (ics2, 0);
c73964b2 5139
ceab47eb
MM
5140 if (TREE_CODE (ics2) == USER_CONV
5141 || TREE_CODE (ics2) == AMBIG_CONV
5142 || TREE_CODE (ics2) == IDENTITY_CONV)
5143 /* At this point, ICS1 cannot be a proper subsequence of
5144 ICS2. We can get a USER_CONV when we are comparing the
5145 second standard conversion sequence of two user conversion
5146 sequences. */
94be8403 5147 return false;
f62dbf03 5148
ceab47eb 5149 ics2 = TREE_OPERAND (ics2, 0);
653cc74a 5150
f62dbf03 5151 if (TREE_CODE (ics2) == TREE_CODE (ics1)
3bfdc719
MM
5152 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
5153 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
5154 TREE_TYPE (TREE_OPERAND (ics1, 0))))
94be8403 5155 return true;
f62dbf03
JM
5156 }
5157}
5158
838dfd8a 5159/* Returns nonzero iff DERIVED is derived from BASE. The inputs may
ceab47eb 5160 be any _TYPE nodes. */
c73964b2 5161
94be8403
GDR
5162bool
5163is_properly_derived_from (tree derived, tree base)
c73964b2 5164{
ceab47eb
MM
5165 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
5166 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
94be8403 5167 return false;
c73964b2 5168
ceab47eb
MM
5169 /* We only allow proper derivation here. The DERIVED_FROM_P macro
5170 considers every class derived from itself. */
9edc3913 5171 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
ceab47eb
MM
5172 && DERIVED_FROM_P (base, derived));
5173}
d11ad92e 5174
ceab47eb
MM
5175/* We build the ICS for an implicit object parameter as a pointer
5176 conversion sequence. However, such a sequence should be compared
5177 as if it were a reference conversion sequence. If ICS is the
5178 implicit conversion sequence for an implicit object parameter,
5179 modify it accordingly. */
d11ad92e 5180
ceab47eb 5181static void
94be8403 5182maybe_handle_implicit_object (tree *ics)
ceab47eb
MM
5183{
5184 if (ICS_THIS_FLAG (*ics))
d11ad92e 5185 {
ceab47eb
MM
5186 /* [over.match.funcs]
5187
5188 For non-static member functions, the type of the
5189 implicit object parameter is "reference to cv X"
5190 where X is the class of which the function is a
5191 member and cv is the cv-qualification on the member
5192 function declaration. */
5193 tree t = *ics;
b0385db8
MM
5194 tree reference_type;
5195
5196 /* The `this' parameter is a pointer to a class type. Make the
34cd5ae7 5197 implicit conversion talk about a reference to that same class
b0385db8
MM
5198 type. */
5199 reference_type = TREE_TYPE (TREE_TYPE (*ics));
5200 reference_type = build_reference_type (reference_type);
5201
00c15f8d
MM
5202 if (TREE_CODE (t) == QUAL_CONV)
5203 t = TREE_OPERAND (t, 0);
6633d636
MS
5204 if (TREE_CODE (t) == PTR_CONV)
5205 t = TREE_OPERAND (t, 0);
5206 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
b0385db8 5207 t = direct_reference_binding (reference_type, t);
ceab47eb 5208 *ics = t;
d11ad92e 5209 }
ceab47eb
MM
5210}
5211
2d2e8123
MM
5212/* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5213 and return the type to which the reference refers. Otherwise,
5214 leave *ICS unchanged and return NULL_TREE. */
ceab47eb 5215
2d2e8123 5216static tree
94be8403 5217maybe_handle_ref_bind (tree *ics)
ceab47eb
MM
5218{
5219 if (TREE_CODE (*ics) == REF_BIND)
d11ad92e 5220 {
351a0f00 5221 tree old_ics = *ics;
2d2e8123
MM
5222 tree type = TREE_TYPE (TREE_TYPE (old_ics));
5223 *ics = TREE_OPERAND (old_ics, 0);
351a0f00
JJ
5224 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
5225 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
2d2e8123 5226 return type;
d11ad92e 5227 }
351a0f00 5228
2d2e8123 5229 return NULL_TREE;
ceab47eb
MM
5230}
5231
5232/* Compare two implicit conversion sequences according to the rules set out in
5233 [over.ics.rank]. Return values:
d11ad92e 5234
ceab47eb
MM
5235 1: ics1 is better than ics2
5236 -1: ics2 is better than ics1
5237 0: ics1 and ics2 are indistinguishable */
5238
5239static int
94be8403 5240compare_ics (tree ics1, tree ics2)
ceab47eb
MM
5241{
5242 tree from_type1;
5243 tree from_type2;
5244 tree to_type1;
5245 tree to_type2;
5246 tree deref_from_type1 = NULL_TREE;
87603ed0
KG
5247 tree deref_from_type2 = NULL_TREE;
5248 tree deref_to_type1 = NULL_TREE;
5249 tree deref_to_type2 = NULL_TREE;
bea09693 5250 int rank1, rank2;
ceab47eb 5251
838dfd8a 5252 /* REF_BINDING is nonzero if the result of the conversion sequence
00c15f8d
MM
5253 is a reference type. In that case TARGET_TYPE is the
5254 type referred to by the reference. */
00c15f8d
MM
5255 tree target_type1;
5256 tree target_type2;
ceab47eb
MM
5257
5258 /* Handle implicit object parameters. */
5259 maybe_handle_implicit_object (&ics1);
5260 maybe_handle_implicit_object (&ics2);
5261
5262 /* Handle reference parameters. */
2d2e8123
MM
5263 target_type1 = maybe_handle_ref_bind (&ics1);
5264 target_type2 = maybe_handle_ref_bind (&ics2);
ceab47eb
MM
5265
5266 /* [over.ics.rank]
5267
5268 When comparing the basic forms of implicit conversion sequences (as
5269 defined in _over.best.ics_)
5270
5271 --a standard conversion sequence (_over.ics.scs_) is a better
5272 conversion sequence than a user-defined conversion sequence
5273 or an ellipsis conversion sequence, and
5274
5275 --a user-defined conversion sequence (_over.ics.user_) is a
5276 better conversion sequence than an ellipsis conversion sequence
5277 (_over.ics.ellipsis_). */
bea09693
NS
5278 rank1 = ICS_RANK (ics1);
5279 rank2 = ICS_RANK (ics2);
5280
5281 if (rank1 > rank2)
c73964b2 5282 return -1;
bea09693 5283 else if (rank1 < rank2)
c73964b2
MS
5284 return 1;
5285
bea09693 5286 if (rank1 == BAD_RANK)
d11ad92e 5287 {
bea09693 5288 /* XXX Isn't this an extension? */
ceab47eb 5289 /* Both ICS are bad. We try to make a decision based on what
cd0be382 5290 would have happened if they'd been good. */
d11ad92e
MS
5291 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5292 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5293 return -1;
5294 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5295 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5296 return 1;
5297
ceab47eb 5298 /* We couldn't make up our minds; try to figure it out below. */
d11ad92e
MS
5299 }
5300
ceab47eb
MM
5301 if (ICS_ELLIPSIS_FLAG (ics1))
5302 /* Both conversions are ellipsis conversions. */
5303 return 0;
5304
c73964b2
MS
5305 /* User-defined conversion sequence U1 is a better conversion sequence
5306 than another user-defined conversion sequence U2 if they contain the
5307 same user-defined conversion operator or constructor and if the sec-
5308 ond standard conversion sequence of U1 is better than the second
5309 standard conversion sequence of U2. */
5310
d11ad92e 5311 if (ICS_USER_FLAG (ics1))
c73964b2
MS
5312 {
5313 tree t1, t2;
5314
5315 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5316 if (TREE_CODE (t1) == AMBIG_CONV)
5317 return 0;
5318 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5319 if (TREE_CODE (t2) == AMBIG_CONV)
5320 return 0;
5321
5322 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5323 return 0;
c73964b2 5324
ceab47eb
MM
5325 /* We can just fall through here, after setting up
5326 FROM_TYPE1 and FROM_TYPE2. */
5327 from_type1 = TREE_TYPE (t1);
5328 from_type2 = TREE_TYPE (t2);
c73964b2 5329 }
ceab47eb
MM
5330 else
5331 {
5332 /* We're dealing with two standard conversion sequences.
c73964b2 5333
ceab47eb
MM
5334 [over.ics.rank]
5335
5336 Standard conversion sequence S1 is a better conversion
5337 sequence than standard conversion sequence S2 if
5338
5339 --S1 is a proper subsequence of S2 (comparing the conversion
5340 sequences in the canonical form defined by _over.ics.scs_,
5341 excluding any Lvalue Transformation; the identity
5342 conversion sequence is considered to be a subsequence of
5343 any non-identity conversion sequence */
5344
5345 from_type1 = ics1;
5346 while (TREE_CODE (from_type1) != IDENTITY_CONV)
5347 from_type1 = TREE_OPERAND (from_type1, 0);
5348 from_type1 = TREE_TYPE (from_type1);
5349
5350 from_type2 = ics2;
5351 while (TREE_CODE (from_type2) != IDENTITY_CONV)
5352 from_type2 = TREE_OPERAND (from_type2, 0);
5353 from_type2 = TREE_TYPE (from_type2);
5354 }
c73964b2 5355
3bfdc719 5356 if (same_type_p (from_type1, from_type2))
f62dbf03 5357 {
ceab47eb 5358 if (is_subseq (ics1, ics2))
f62dbf03 5359 return 1;
ceab47eb 5360 if (is_subseq (ics2, ics1))
f62dbf03 5361 return -1;
f62dbf03 5362 }
961ec1a5
JM
5363 /* Otherwise, one sequence cannot be a subsequence of the other; they
5364 don't start with the same type. This can happen when comparing the
5365 second standard conversion sequence in two user-defined conversion
5366 sequences. */
c73964b2 5367
ceab47eb 5368 /* [over.ics.rank]
c73964b2 5369
ceab47eb 5370 Or, if not that,
c73964b2 5371
ceab47eb
MM
5372 --the rank of S1 is better than the rank of S2 (by the rules
5373 defined below):
c73964b2 5374
ceab47eb
MM
5375 Standard conversion sequences are ordered by their ranks: an Exact
5376 Match is a better conversion than a Promotion, which is a better
5377 conversion than a Conversion.
c73964b2 5378
ceab47eb
MM
5379 Two conversion sequences with the same rank are indistinguishable
5380 unless one of the following rules applies:
c73964b2 5381
ceab47eb
MM
5382 --A conversion that is not a conversion of a pointer, or pointer
5383 to member, to bool is better than another conversion that is such
5384 a conversion.
c73964b2 5385
ceab47eb
MM
5386 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5387 so that we do not have to check it explicitly. */
5388 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5389 return 1;
5390 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5391 return -1;
c73964b2 5392
ceab47eb
MM
5393 to_type1 = TREE_TYPE (ics1);
5394 to_type2 = TREE_TYPE (ics2);
c73964b2 5395
ceab47eb
MM
5396 if (TYPE_PTR_P (from_type1)
5397 && TYPE_PTR_P (from_type2)
5398 && TYPE_PTR_P (to_type1)
5399 && TYPE_PTR_P (to_type2))
5400 {
5401 deref_from_type1 = TREE_TYPE (from_type1);
5402 deref_from_type2 = TREE_TYPE (from_type2);
5403 deref_to_type1 = TREE_TYPE (to_type1);
5404 deref_to_type2 = TREE_TYPE (to_type2);
5405 }
5406 /* The rules for pointers to members A::* are just like the rules
5407 for pointers A*, except opposite: if B is derived from A then
5408 A::* converts to B::*, not vice versa. For that reason, we
5409 switch the from_ and to_ variables here. */
a5ac359a
MM
5410 else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
5411 && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
5412 || (TYPE_PTRMEMFUNC_P (from_type1)
5413 && TYPE_PTRMEMFUNC_P (from_type2)
5414 && TYPE_PTRMEMFUNC_P (to_type1)
5415 && TYPE_PTRMEMFUNC_P (to_type2)))
5416 {
5417 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
5418 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
5419 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
5420 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
ceab47eb 5421 }
c73964b2 5422
ceab47eb
MM
5423 if (deref_from_type1 != NULL_TREE
5424 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5425 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5426 {
5427 /* This was one of the pointer or pointer-like conversions.
5428
5429 [over.ics.rank]
5430
5431 --If class B is derived directly or indirectly from class A,
5432 conversion of B* to A* is better than conversion of B* to
5433 void*, and conversion of A* to void* is better than
5434 conversion of B* to void*. */
5435 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5436 && TREE_CODE (deref_to_type2) == VOID_TYPE)
c73964b2 5437 {
ceab47eb
MM
5438 if (is_properly_derived_from (deref_from_type1,
5439 deref_from_type2))
c73964b2 5440 return -1;
ceab47eb
MM
5441 else if (is_properly_derived_from (deref_from_type2,
5442 deref_from_type1))
5443 return 1;
c73964b2 5444 }
ceab47eb
MM
5445 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5446 || TREE_CODE (deref_to_type2) == VOID_TYPE)
c73964b2 5447 {
3bfdc719 5448 if (same_type_p (deref_from_type1, deref_from_type2))
ceab47eb
MM
5449 {
5450 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5451 {
5452 if (is_properly_derived_from (deref_from_type1,
5453 deref_to_type1))
5454 return 1;
5455 }
5456 /* We know that DEREF_TO_TYPE1 is `void' here. */
5457 else if (is_properly_derived_from (deref_from_type1,
5458 deref_to_type2))
5459 return -1;
5460 }
c73964b2 5461 }
ceab47eb
MM
5462 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5463 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
c73964b2 5464 {
ceab47eb
MM
5465 /* [over.ics.rank]
5466
5467 --If class B is derived directly or indirectly from class A
5468 and class C is derived directly or indirectly from B,
5469
5470 --conversion of C* to B* is better than conversion of C* to
5471 A*,
5472
5473 --conversion of B* to A* is better than conversion of C* to
5474 A* */
3bfdc719 5475 if (same_type_p (deref_from_type1, deref_from_type2))
ceab47eb
MM
5476 {
5477 if (is_properly_derived_from (deref_to_type1,
5478 deref_to_type2))
5479 return 1;
5480 else if (is_properly_derived_from (deref_to_type2,
5481 deref_to_type1))
5482 return -1;
5483 }
3bfdc719 5484 else if (same_type_p (deref_to_type1, deref_to_type2))
ceab47eb
MM
5485 {
5486 if (is_properly_derived_from (deref_from_type2,
5487 deref_from_type1))
5488 return 1;
5489 else if (is_properly_derived_from (deref_from_type1,
5490 deref_from_type2))
5491 return -1;
5492 }
c73964b2 5493 }
ceab47eb 5494 }
2d2e8123 5495 else if (CLASS_TYPE_P (non_reference (from_type1))
3bfdc719 5496 && same_type_p (from_type1, from_type2))
ceab47eb 5497 {
2d2e8123
MM
5498 tree from = non_reference (from_type1);
5499
ceab47eb
MM
5500 /* [over.ics.rank]
5501
5502 --binding of an expression of type C to a reference of type
5503 B& is better than binding an expression of type C to a
5504 reference of type A&
5505
5506 --conversion of C to B is better than conversion of C to A, */
2d2e8123
MM
5507 if (is_properly_derived_from (from, to_type1)
5508 && is_properly_derived_from (from, to_type2))
c73964b2 5509 {
ceab47eb 5510 if (is_properly_derived_from (to_type1, to_type2))
c73964b2 5511 return 1;
ceab47eb 5512 else if (is_properly_derived_from (to_type2, to_type1))
c73964b2
MS
5513 return -1;
5514 }
5515 }
2d2e8123 5516 else if (CLASS_TYPE_P (non_reference (to_type1))
3bfdc719 5517 && same_type_p (to_type1, to_type2))
c73964b2 5518 {
2d2e8123
MM
5519 tree to = non_reference (to_type1);
5520
ceab47eb 5521 /* [over.ics.rank]
c73964b2 5522
ceab47eb
MM
5523 --binding of an expression of type B to a reference of type
5524 A& is better than binding an expression of type C to a
5525 reference of type A&,
5526
5527 --onversion of B to A is better than conversion of C to A */
2d2e8123
MM
5528 if (is_properly_derived_from (from_type1, to)
5529 && is_properly_derived_from (from_type2, to))
ceab47eb
MM
5530 {
5531 if (is_properly_derived_from (from_type2, from_type1))
5532 return 1;
5533 else if (is_properly_derived_from (from_type1, from_type2))
5534 return -1;
5535 }
c73964b2
MS
5536 }
5537
ceab47eb
MM
5538 /* [over.ics.rank]
5539
5540 --S1 and S2 differ only in their qualification conversion and yield
5541 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5542 qualification signature of type T1 is a proper subset of the cv-
5543 qualification signature of type T2 */
5544 if (TREE_CODE (ics1) == QUAL_CONV
5545 && TREE_CODE (ics2) == QUAL_CONV
3bfdc719 5546 && same_type_p (from_type1, from_type2))
ceab47eb
MM
5547 return comp_cv_qual_signature (to_type1, to_type2);
5548
5549 /* [over.ics.rank]
5550
5551 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5552 types to which the references refer are the same type except for
5553 top-level cv-qualifiers, and the type to which the reference
5554 initialized by S2 refers is more cv-qualified than the type to
5555 which the reference initialized by S1 refers */
5556
2d2e8123 5557 if (target_type1 && target_type2
9edc3913 5558 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
00c15f8d 5559 return comp_cv_qualification (target_type2, target_type1);
ceab47eb
MM
5560
5561 /* Neither conversion sequence is better than the other. */
c73964b2
MS
5562 return 0;
5563}
5564
03e70705
JM
5565/* The source type for this standard conversion sequence. */
5566
8e69329a 5567static tree
94be8403 5568source_type (tree t)
8e69329a
JM
5569{
5570 for (;; t = TREE_OPERAND (t, 0))
5571 {
5572 if (TREE_CODE (t) == USER_CONV
5573 || TREE_CODE (t) == AMBIG_CONV
5574 || TREE_CODE (t) == IDENTITY_CONV)
5575 return TREE_TYPE (t);
5576 }
a98facb0 5577 abort ();
8e69329a 5578}
5ffe581d
JM
5579
5580/* Note a warning about preferring WINNER to LOSER. We do this by storing
5581 a pointer to LOSER and re-running joust to produce the warning if WINNER
5582 is actually used. */
5583
5584static void
94be8403 5585add_warning (struct z_candidate *winner, struct z_candidate *loser)
5ffe581d 5586{
3e411c3f 5587 winner->warnings = tree_cons (NULL_TREE,
e2500fed 5588 build_zc_wrapper (loser),
e1b3e07d 5589 winner->warnings);
5ffe581d 5590}
8e69329a 5591
c73964b2
MS
5592/* Compare two candidates for overloading as described in
5593 [over.match.best]. Return values:
5594
5595 1: cand1 is better than cand2
5596 -1: cand2 is better than cand1
5597 0: cand1 and cand2 are indistinguishable */
5598
5599static int
94be8403 5600joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
c73964b2
MS
5601{
5602 int winner = 0;
5603 int i, off1 = 0, off2 = 0, len;
5604
d11ad92e
MS
5605 /* Candidates that involve bad conversions are always worse than those
5606 that don't. */
5607 if (cand1->viable > cand2->viable)
5608 return 1;
5609 if (cand1->viable < cand2->viable)
5610 return -1;
5611
37b6eb34 5612 /* If we have two pseudo-candidates for conversions to the same type,
6e9dcc25
JM
5613 or two candidates for the same function, arbitrarily pick one. */
5614 if (cand1->fn == cand2->fn
5615 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
37b6eb34
JM
5616 return 1;
5617
c73964b2
MS
5618 /* a viable function F1
5619 is defined to be a better function than another viable function F2 if
5620 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5621 ICSi(F2), and then */
5622
5623 /* for some argument j, ICSj(F1) is a better conversion sequence than
5624 ICSj(F2) */
5625
cab1f180
ML
5626 /* For comparing static and non-static member functions, we ignore
5627 the implicit object parameter of the non-static function. The
5628 standard says to pretend that the static function has an object
5629 parm, but that won't work with operator overloading. */
c73964b2
MS
5630 len = TREE_VEC_LENGTH (cand1->convs);
5631 if (len != TREE_VEC_LENGTH (cand2->convs))
5632 {
5633 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5634 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5635 off2 = 1;
5636 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5637 && DECL_STATIC_FUNCTION_P (cand2->fn))
5638 {
5639 off1 = 1;
5640 --len;
5641 }
5642 else
a98facb0 5643 abort ();
c73964b2
MS
5644 }
5645
5646 for (i = 0; i < len; ++i)
5647 {
da20811c
JM
5648 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5649 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5650 int comp = compare_ics (t1, t2);
c73964b2
MS
5651
5652 if (comp != 0)
5653 {
da20811c
JM
5654 if (warn_sign_promo
5655 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5656 && TREE_CODE (t1) == STD_CONV
5657 && TREE_CODE (t2) == STD_CONV
5658 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5659 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5660 && (TYPE_PRECISION (TREE_TYPE (t1))
5661 == TYPE_PRECISION (TREE_TYPE (t2)))
5662 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5663 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5664 == ENUMERAL_TYPE)))
5665 {
5666 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5667 tree type1, type2;
5ffe581d 5668 struct z_candidate *w, *l;
da20811c 5669 if (comp > 0)
5ffe581d
JM
5670 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5671 w = cand1, l = cand2;
da20811c 5672 else
5ffe581d
JM
5673 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5674 w = cand2, l = cand1;
da20811c 5675
5ffe581d
JM
5676 if (warn)
5677 {
33bd39a2 5678 warning ("passing `%T' chooses `%T' over `%T'",
5ffe581d 5679 type, type1, type2);
33bd39a2 5680 warning (" in call to `%D'", w->fn);
5ffe581d
JM
5681 }
5682 else
5683 add_warning (w, l);
da20811c
JM
5684 }
5685
c73964b2 5686 if (winner && comp != winner)
c11b6f21
MS
5687 {
5688 winner = 0;
5689 goto tweak;
5690 }
c73964b2
MS
5691 winner = comp;
5692 }
5693 }
5694
9a68c51f
JM
5695 /* warn about confusing overload resolution for user-defined conversions,
5696 either between a constructor and a conversion op, or between two
5697 conversion ops. */
2e2d4075 5698 if (winner && warn_conversion && cand1->second_conv
f8986275
NS
5699 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
5700 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
5701 {
5702 struct z_candidate *w, *l;
5703 bool give_warning = false;
5704
5705 if (winner == 1)
5706 w = cand1, l = cand2;
5707 else
5708 w = cand2, l = cand1;
5709
5710 /* We don't want to complain about `X::operator T1 ()'
5711 beating `X::operator T2 () const', when T2 is a no less
9bcb9aae 5712 cv-qualified version of T1. */
f8986275
NS
5713 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
5714 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
8e69329a 5715 {
f8986275
NS
5716 tree t = TREE_TYPE (TREE_TYPE (l->fn));
5717 tree f = TREE_TYPE (TREE_TYPE (w->fn));
5718
5719 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
5ffe581d 5720 {
f8986275
NS
5721 t = TREE_TYPE (t);
5722 f = TREE_TYPE (f);
5ffe581d 5723 }
f8986275
NS
5724 if (!comp_ptr_ttypes (t, f))
5725 give_warning = true;
5726 }
5727 else
5728 give_warning = true;
5729
5730 if (!give_warning)
5731 /*NOP*/;
2e2d4075 5732 else if (warn)
f8986275
NS
5733 {
5734 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5735 if (! DECL_CONSTRUCTOR_P (w->fn))
5736 source = TREE_TYPE (source);
5737 warning ("choosing `%D' over `%D'", w->fn, l->fn);
5738 warning (" for conversion from `%T' to `%T'",
5739 source, TREE_TYPE (w->second_conv));
5740 warning (" because conversion sequence for the argument is better");
8e69329a 5741 }
f8986275
NS
5742 else
5743 add_warning (w, l);
8e69329a
JM
5744 }
5745
c73964b2
MS
5746 if (winner)
5747 return winner;
5748
e5596aef
NS
5749 /* or, if not that,
5750 F1 is a non-template function and F2 is a template function
5751 specialization. */
5752
c73964b2
MS
5753 if (! cand1->template && cand2->template)
5754 return 1;
5755 else if (cand1->template && ! cand2->template)
5756 return -1;
e5596aef
NS
5757
5758 /* or, if not that,
5759 F1 and F2 are template functions and the function template for F1 is
5760 more specialized than the template for F2 according to the partial
5761 ordering rules. */
5762
5763 if (cand1->template && cand2->template)
4cff6abe
NS
5764 {
5765 winner = more_specialized
5766 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5767 DEDUCE_ORDER,
fe730161
JO
5768 /* Tell the deduction code how many real function arguments
5769 we saw, not counting the implicit 'this' argument. But,
5770 add_function_candidate() suppresses the "this" argument
5771 for constructors.
d9579a59
JM
5772
5773 [temp.func.order]: The presence of unused ellipsis and default
5774 arguments has no effect on the partial ordering of function
5775 templates. */
4cff6abe 5776 TREE_VEC_LENGTH (cand1->convs)
fe730161
JO
5777 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5778 - DECL_CONSTRUCTOR_P (cand1->fn)));
4cff6abe
NS
5779 if (winner)
5780 return winner;
5781 }
c73964b2
MS
5782
5783 /* or, if not that,
5784 the context is an initialization by user-defined conversion (see
5785 _dcl.init_ and _over.match.user_) and the standard conversion
5786 sequence from the return type of F1 to the destination type (i.e.,
5787 the type of the entity being initialized) is a better conversion
5788 sequence than the standard conversion sequence from the return type
5789 of F2 to the destination type. */
5790
4cff6abe
NS
5791 if (cand1->second_conv)
5792 {
5793 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5794 if (winner)
5795 return winner;
5796 }
5797
08ac397c
JM
5798 /* Check whether we can discard a builtin candidate, either because we
5799 have two identical ones or matching builtin and non-builtin candidates.
5800
5801 (Pedantically in the latter case the builtin which matched the user
5802 function should not be added to the overload set, but we spot it here.
5803
5804 [over.match.oper]
5805 ... the builtin candidates include ...
5806 - do not have the same parameter type list as any non-template
5807 non-member candidate. */
5808
5809 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5810 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
c73964b2 5811 {
c11b6f21 5812 for (i = 0; i < len; ++i)
3bfdc719
MM
5813 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5814 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
c73964b2
MS
5815 break;
5816 if (i == TREE_VEC_LENGTH (cand1->convs))
08ac397c
JM
5817 {
5818 if (cand1->fn == cand2->fn)
5819 /* Two built-in candidates; arbitrarily pick one. */
5820 return 1;
5821 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5822 /* cand1 is built-in; prefer cand2. */
5823 return -1;
5824 else
5825 /* cand2 is built-in; prefer cand1. */
5826 return 1;
5827 }
c73964b2
MS
5828 }
5829
2c169bab
JM
5830 /* If the two functions are the same (this can happen with declarations
5831 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5832 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5833 && equal_functions (cand1->fn, cand2->fn))
5834 return 1;
436f8a4c 5835
c11b6f21
MS
5836tweak:
5837
5838 /* Extension: If the worst conversion for one candidate is worse than the
5839 worst conversion for the other, take the first. */
4cff6abe 5840 if (!pedantic)
c11b6f21
MS
5841 {
5842 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
ae0ed63a 5843 struct z_candidate *w = 0, *l = 0;
c11b6f21
MS
5844
5845 for (i = 0; i < len; ++i)
5846 {
5847 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5848 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5849 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5850 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5851 }
c11b6f21 5852 if (rank1 < rank2)
f86fdf68 5853 winner = 1, w = cand1, l = cand2;
c11b6f21 5854 if (rank1 > rank2)
f86fdf68
NS
5855 winner = -1, w = cand2, l = cand1;
5856 if (winner)
5857 {
5858 if (warn)
5859 {
d2a6f3c0
ZW
5860 pedwarn ("\
5861ISO C++ says that these are ambiguous, even \
5862though the worst conversion for the first is better than \
5863the worst conversion for the second:");
5864 print_z_candidate (_("candidate 1:"), w);
5865 print_z_candidate (_("candidate 2:"), l);
f86fdf68
NS
5866 }
5867 else
5868 add_warning (w, l);
5869 return winner;
5870 }
c11b6f21
MS
5871 }
5872
4cff6abe
NS
5873 my_friendly_assert (!winner, 20010121);
5874 return 0;
c73964b2
MS
5875}
5876
5877/* Given a list of candidates for overloading, find the best one, if any.
5878 This algorithm has a worst case of O(2n) (winner is last), and a best
5879 case of O(n/2) (totally ambiguous); much better than a sorting
5880 algorithm. */
5881
5882static struct z_candidate *
94be8403 5883tourney (struct z_candidate *candidates)
c73964b2
MS
5884{
5885 struct z_candidate *champ = candidates, *challenger;
5886 int fate;
b265c11a 5887 int champ_compared_to_predecessor = 0;
c73964b2
MS
5888
5889 /* Walk through the list once, comparing each current champ to the next
5890 candidate, knocking out a candidate or two with each comparison. */
5891
5892 for (challenger = champ->next; challenger; )
5893 {
5ffe581d 5894 fate = joust (champ, challenger, 0);
c73964b2
MS
5895 if (fate == 1)
5896 challenger = challenger->next;
5897 else
5898 {
5899 if (fate == 0)
5900 {
5901 champ = challenger->next;
5902 if (champ == 0)
5903 return 0;
b265c11a 5904 champ_compared_to_predecessor = 0;
c73964b2
MS
5905 }
5906 else
b265c11a
MM
5907 {
5908 champ = challenger;
5909 champ_compared_to_predecessor = 1;
5910 }
c73964b2
MS
5911
5912 challenger = champ->next;
5913 }
5914 }
5915
5916 /* Make sure the champ is better than all the candidates it hasn't yet
b265c11a 5917 been compared to. */
c73964b2 5918
b265c11a
MM
5919 for (challenger = candidates;
5920 challenger != champ
5921 && !(champ_compared_to_predecessor && challenger->next == champ);
c73964b2
MS
5922 challenger = challenger->next)
5923 {
5ffe581d 5924 fate = joust (champ, challenger, 0);
c73964b2
MS
5925 if (fate != 1)
5926 return 0;
5927 }
5928
5929 return champ;
5930}
c11b6f21 5931
838dfd8a 5932/* Returns nonzero if things of type FROM can be converted to TO. */
4143af33 5933
94be8403
GDR
5934bool
5935can_convert (tree to, tree from)
c11b6f21 5936{
a7a64a77 5937 return can_convert_arg (to, from, NULL_TREE);
c11b6f21
MS
5938}
5939
838dfd8a 5940/* Returns nonzero if ARG (of type FROM) can be converted to TO. */
4143af33 5941
94be8403
GDR
5942bool
5943can_convert_arg (tree to, tree from, tree arg)
c11b6f21 5944{
277294d7
JM
5945 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5946 return (t && ! ICS_BAD_FLAG (t));
c11b6f21 5947}
27b8d0cd 5948
72a08131
JM
5949/* Like can_convert_arg, but allows dubious conversions as well. */
5950
94be8403
GDR
5951bool
5952can_convert_arg_bad (tree to, tree from, tree arg)
72a08131 5953{
94be8403 5954 return implicit_conversion (to, from, arg, LOOKUP_NORMAL) != 0;
72a08131
JM
5955}
5956
5957/* Convert EXPR to TYPE. Return the converted expression.
5958
5959 Note that we allow bad conversions here because by the time we get to
5960 this point we are committed to doing the conversion. If we end up
5961 doing a bad conversion, convert_like will complain. */
4143af33 5962
a7a64a77 5963tree
94be8403 5964perform_implicit_conversion (tree type, tree expr)
a7a64a77 5965{
b5534c65
NS
5966 tree conv;
5967
a723baf1 5968 if (error_operand_p (expr))
b5534c65
NS
5969 return error_mark_node;
5970 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5971 LOOKUP_NORMAL);
72a08131 5972 if (!conv)
a7a64a77 5973 {
33bd39a2 5974 error ("could not convert `%E' to `%T'", expr, type);
a7a64a77
MM
5975 return error_mark_node;
5976 }
5977
5978 return convert_like (conv, expr);
5979}
5980
3fe18f1d
MM
5981/* Convert EXPR to TYPE (as a direct-initialization) if that is
5982 permitted. If the conversion is valid, the converted expression is
ceeae2d1
MM
5983 returned. Otherwise, NULL_TREE is returned, except in the case
5984 that TYPE is a class type; in that case, an error is issued. */
3fe18f1d
MM
5985
5986tree
5987perform_direct_initialization_if_possible (tree type, tree expr)
5988{
5989 tree conv;
5990
5991 if (type == error_mark_node || error_operand_p (expr))
5992 return error_mark_node;
ceeae2d1
MM
5993 /* [dcl.init]
5994
5995 If the destination type is a (possibly cv-qualified) class type:
5996
5997 -- If the initialization is direct-initialization ...,
5998 constructors are considered. ... If no constructor applies, or
5999 the overload resolution is ambiguous, the initialization is
6000 ill-formed. */
6001 if (CLASS_TYPE_P (type))
385bce06
MM
6002 {
6003 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6004 build_tree_list (NULL_TREE, expr),
6005 TYPE_BINFO (type),
6006 LOOKUP_NORMAL);
6007 return build_cplus_new (type, expr);
6008 }
3fe18f1d
MM
6009 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6010 LOOKUP_NORMAL);
6011 if (!conv || ICS_BAD_FLAG (conv))
6012 return NULL_TREE;
6013 return convert_like_real (conv, expr, NULL_TREE, 0, 0,
6014 /*issue_conversion_warnings=*/false);
6015}
6016
7993382e
MM
6017/* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference
6018 is being bound to a temporary. Create and return a new VAR_DECL
aa6e8ed3
MM
6019 with the indicated TYPE; this variable will store the value to
6020 which the reference is bound. */
7993382e
MM
6021
6022tree
aa6e8ed3 6023make_temporary_var_for_ref_to_temp (tree decl, tree type)
7993382e 6024{
7993382e
MM
6025 tree var;
6026
7993382e
MM
6027 /* Create the variable. */
6028 var = build_decl (VAR_DECL, NULL_TREE, type);
6029 DECL_ARTIFICIAL (var) = 1;
6030 TREE_USED (var) = 1;
6031
6032 /* Register the variable. */
6033 if (TREE_STATIC (decl))
6034 {
6035 /* Namespace-scope or local static; give it a mangled name. */
6036 tree name;
6037
6038 TREE_STATIC (var) = 1;
6039 name = mangle_ref_init_variable (decl);
6040 DECL_NAME (var) = name;
6041 SET_DECL_ASSEMBLER_NAME (var, name);
6042 var = pushdecl_top_level (var);
6043 }
6044 else
6045 {
6046 /* Create a new cleanup level if necessary. */
6047 maybe_push_cleanup_level (type);
6048 /* Don't push unnamed temps. Do set DECL_CONTEXT, though. */
6049 DECL_CONTEXT (var) = current_function_decl;
6050 }
6051
6052 return var;
6053}
6054
27b8d0cd 6055/* Convert EXPR to the indicated reference TYPE, in a way suitable for
7e99327d 6056 initializing a variable of that TYPE. If DECL is non-NULL, it is
7993382e 6057 the VAR_DECL being initialized with the EXPR. (In that case, the
7e99327d
MM
6058 type of DECL will be TYPE.) If DECL is non-NULL, then CLEANUP must
6059 also be non-NULL, and with *CLEANUP initialized to NULL. Upon
6060 return, if *CLEANUP is no longer NULL, it will be a CLEANUP_STMT
6061 that should be inserted after the returned expression is used to
6062 initialize DECL.
7993382e
MM
6063
6064 Return the converted expression. */
27b8d0cd
MM
6065
6066tree
7e99327d 6067initialize_reference (tree type, tree expr, tree decl, tree *cleanup)
27b8d0cd
MM
6068{
6069 tree conv;
7993382e
MM
6070
6071 if (type == error_mark_node || error_operand_p (expr))
6072 return error_mark_node;
27b8d0cd 6073
aa6e8ed3 6074 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
27b8d0cd
MM
6075 if (!conv || ICS_BAD_FLAG (conv))
6076 {
f19319db
GDR
6077 if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
6078 && !real_lvalue_p (expr))
6079 error ("invalid initialization of non-const reference of "
6080 "type '%T' from a temporary of type '%T'",
6081 type, TREE_TYPE (expr));
6082 else
d04a575f
MM
6083 error ("invalid initialization of reference of type "
6084 "'%T' from expression of type '%T'", type,
6085 TREE_TYPE (expr));
27b8d0cd
MM
6086 return error_mark_node;
6087 }
6088
7993382e
MM
6089 /* If DECL is non-NULL, then this special rule applies:
6090
6091 [class.temporary]
6092
6093 The temporary to which the reference is bound or the temporary
aa6e8ed3 6094 that is the complete object to which the reference is bound
7993382e
MM
6095 persists for the lifetime of the reference.
6096
6097 The temporaries created during the evaluation of the expression
6098 initializing the reference, except the temporary to which the
6099 reference is bound, are destroyed at the end of the
6100 full-expression in which they are created.
6101
6102 In that case, we store the converted expression into a new
aa6e8ed3
MM
6103 VAR_DECL in a new scope.
6104
6105 However, we want to be careful not to create temporaries when
6106 they are not required. For example, given:
6107
6108 struct B {};
6109 struct D : public B {};
6110 D f();
6111 const B& b = f();
6112
6113 there is no need to copy the return value from "f"; we can just
6114 extend its lifetime. Similarly, given:
6115
6116 struct S {};
6117 struct T { operator S(); };
6118 T t;
6119 const S& s = t;
6120
170b020f 6121 we can extend the lifetime of the return value of the conversion
aa6e8ed3 6122 operator. */
7993382e 6123 my_friendly_assert (TREE_CODE (conv) == REF_BIND, 20030302);
aa6e8ed3 6124 if (decl)
7993382e
MM
6125 {
6126 tree var;
aa6e8ed3 6127 tree base_conv_type;
7993382e 6128
aa6e8ed3
MM
6129 /* Skip over the REF_BIND. */
6130 conv = TREE_OPERAND (conv, 0);
6131 /* If the next conversion is a BASE_CONV, skip that too -- but
6132 remember that the conversion was required. */
26f86471 6133 if (TREE_CODE (conv) == BASE_CONV && !NEED_TEMPORARY_P (conv))
aa6e8ed3 6134 {
4f8163b1
MM
6135 void (*diagnostic_fn) (const char *, ...);
6136 if (CHECK_COPY_CONSTRUCTOR_P (conv))
6137 /* Generate a temporary copy purely to generate the required
6138 diagnostics. */
6139 build_temp (build_dummy_object (TREE_TYPE (expr)),
6140 TREE_TYPE (expr),
6141 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6142 &diagnostic_fn);
aa6e8ed3
MM
6143 base_conv_type = TREE_TYPE (conv);
6144 conv = TREE_OPERAND (conv, 0);
6145 }
6146 else
6147 base_conv_type = NULL_TREE;
6148 /* Perform the remainder of the conversion. */
1b6bfcd2
MM
6149 expr = convert_like_real (conv, expr,
6150 /*fn=*/NULL_TREE, /*argnum=*/0,
6151 /*inner=*/-1,
6152 /*issue_conversion_warnings=*/true);
d18a8251 6153 if (!real_lvalue_p (expr))
aa6e8ed3 6154 {
170b020f
MM
6155 tree init;
6156 tree type;
6157
aa6e8ed3 6158 /* Create the temporary variable. */
170b020f
MM
6159 type = TREE_TYPE (expr);
6160 var = make_temporary_var_for_ref_to_temp (decl, type);
6161 layout_decl (var, 0);
7e99327d
MM
6162 /* Create the INIT_EXPR that will initialize the temporary
6163 variable. */
6164 init = build (INIT_EXPR, type, var, expr);
170b020f
MM
6165 if (at_function_scope_p ())
6166 {
170b020f 6167 add_decl_stmt (var);
7e99327d
MM
6168 *cleanup = cxx_maybe_build_cleanup (var);
6169 if (*cleanup)
6170 /* We must be careful to destroy the temporary only
6171 after its initialization has taken place. If the
6172 initialization throws an exception, then the
6173 destructor should not be run. We cannot simply
6174 transform INIT into something like:
6175
6176 (INIT, ({ CLEANUP_STMT; }))
6177
6178 because emit_local_var always treats the
6179 initializer as a full-expression. Thus, the
6180 destructor would run too early; it would run at the
6181 end of initializing the reference variable, rather
6182 than at the end of the block enclosing the
6183 reference variable.
6184
6185 The solution is to pass back a CLEANUP_STMT which
6186 the caller is responsible for attaching to the
6187 statement tree. */
6188 *cleanup = build_stmt (CLEANUP_STMT, var, *cleanup);
170b020f
MM
6189 }
6190 else
6191 {
6192 rest_of_decl_compilation (var, NULL, /*toplev=*/1, at_eof);
6193 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6194 static_aggregates = tree_cons (NULL_TREE, var,
6195 static_aggregates);
6196 }
aa6e8ed3
MM
6197 /* Use its address to initialize the reference variable. */
6198 expr = build_address (var);
170b020f 6199 expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
aa6e8ed3
MM
6200 }
6201 else
6202 /* Take the address of EXPR. */
6203 expr = build_unary_op (ADDR_EXPR, expr, 0);
6204 /* If a BASE_CONV was required, perform it now. */
6205 if (base_conv_type)
6206 expr = (perform_implicit_conversion
6207 (build_pointer_type (base_conv_type), expr));
6208 return build_nop (type, expr);
7993382e
MM
6209 }
6210
6211 /* Perform the conversion. */
27b8d0cd
MM
6212 return convert_like (conv, expr);
6213}
e2500fed
GK
6214
6215#include "gt-cp-call.h"
This page took 2.493003 seconds and 5 git commands to generate.