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