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