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