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