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