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