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